xref: /freebsd/libexec/tftpd/tftpd.c (revision 79c342aaf86feb4efbd15383f54e4fe7bdc9da7b)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4ea022d16SRodney W. Grimes  * Copyright (c) 1983, 1993
5ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6ea022d16SRodney W. Grimes  *
7ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
9ea022d16SRodney W. Grimes  * are met:
10ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
155efaea4cSChristian Brueffer  * 3. Neither the name of the University nor the names of its contributors
16ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17ea022d16SRodney W. Grimes  *    without specific prior written permission.
18ea022d16SRodney W. Grimes  *
19ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ea022d16SRodney W. Grimes  * SUCH DAMAGE.
30ea022d16SRodney W. Grimes  */
31ea022d16SRodney W. Grimes 
32ea022d16SRodney W. Grimes /*
33ea022d16SRodney W. Grimes  * Trivial file transfer protocol server.
34ea022d16SRodney W. Grimes  *
35ea022d16SRodney W. Grimes  * This version includes many modifications by Jim Guyton
36ea022d16SRodney W. Grimes  * <guyton@rand-unix>.
37ea022d16SRodney W. Grimes  */
38ea022d16SRodney W. Grimes 
39ea022d16SRodney W. Grimes #include <sys/param.h>
40ea022d16SRodney W. Grimes #include <sys/ioctl.h>
41ea022d16SRodney W. Grimes #include <sys/socket.h>
42eb0292d9SDag-Erling Smørgrav #include <sys/stat.h>
43eb0292d9SDag-Erling Smørgrav #include <sys/time.h>
44ea022d16SRodney W. Grimes 
45ea022d16SRodney W. Grimes #include <netinet/in.h>
46ea022d16SRodney W. Grimes #include <arpa/tftp.h>
47ea022d16SRodney W. Grimes 
48ea022d16SRodney W. Grimes #include <ctype.h>
49ea022d16SRodney W. Grimes #include <errno.h>
50ea022d16SRodney W. Grimes #include <fcntl.h>
51ea022d16SRodney W. Grimes #include <netdb.h>
52a8faeabcSPhilippe Charnier #include <pwd.h>
53b713097aSMarius Strobl #include <stdint.h>
54ea022d16SRodney W. Grimes #include <stdio.h>
55ea022d16SRodney W. Grimes #include <stdlib.h>
56ea022d16SRodney W. Grimes #include <string.h>
57ea022d16SRodney W. Grimes #include <syslog.h>
58eb0292d9SDag-Erling Smørgrav #include <time.h>
59ea022d16SRodney W. Grimes #include <unistd.h>
60ea022d16SRodney W. Grimes 
615276e639SWarner Losh #include "tftp-file.h"
625276e639SWarner Losh #include "tftp-io.h"
635276e639SWarner Losh #include "tftp-utils.h"
645276e639SWarner Losh #include "tftp-transfer.h"
655276e639SWarner Losh #include "tftp-options.h"
66ea022d16SRodney W. Grimes 
674eb4663bSEnji Cooper #ifdef	LIBWRAP
684eb4663bSEnji Cooper #include <tcpd.h>
694eb4663bSEnji Cooper #endif
704eb4663bSEnji Cooper 
711ed44fccSDag-Erling Smørgrav static void	tftp_wrq(int peer, char *, size_t);
721ed44fccSDag-Erling Smørgrav static void	tftp_rrq(int peer, char *, size_t);
73ea022d16SRodney W. Grimes 
74ea022d16SRodney W. Grimes /*
75ea022d16SRodney W. Grimes  * Null-terminated directory prefix list for absolute pathname requests and
76ea022d16SRodney W. Grimes  * search list for relative pathname requests.
77ea022d16SRodney W. Grimes  *
78ea022d16SRodney W. Grimes  * MAXDIRS should be at least as large as the number of arguments that
79ea022d16SRodney W. Grimes  * inetd allows (currently 20).
80ea022d16SRodney W. Grimes  */
81ea022d16SRodney W. Grimes #define MAXDIRS	20
82ea022d16SRodney W. Grimes static struct dirlist {
83f49c0dc0SDavid Malone 	const char	*name;
841ed44fccSDag-Erling Smørgrav 	size_t		len;
85ea022d16SRodney W. Grimes } dirs[MAXDIRS+1];
86ea022d16SRodney W. Grimes static int	suppress_naks;
87ea022d16SRodney W. Grimes static int	logging;
881ed0e5d2SBill Fumerola static int	ipchroot;
89273a307dSEugene Grosbein static int	check_woth = 1;
90eff77877SMatthew N. Dodd static int	create_new = 0;
9104ebad38SMarius Strobl static const char *newfile_format = "%Y%m%d";
92dba0fd30SEdwin Groothuis static int	increase_name = 0;
93eff77877SMatthew N. Dodd static mode_t	mask = S_IWGRP | S_IWOTH;
94ea022d16SRodney W. Grimes 
955276e639SWarner Losh struct formats;
965276e639SWarner Losh static void	tftp_recvfile(int peer, const char *mode);
975276e639SWarner Losh static void	tftp_xmitfile(int peer, const char *mode);
985276e639SWarner Losh static int	validate_access(int peer, char **, int);
995276e639SWarner Losh static char	peername[NI_MAXHOST];
100f49c0dc0SDavid Malone 
101ae824d80SEd Schouten static FILE *file;
1025276e639SWarner Losh 
103ae824d80SEd Schouten static struct formats {
1045276e639SWarner Losh 	const char	*f_mode;
1055276e639SWarner Losh 	int	f_convert;
1065276e639SWarner Losh } formats[] = {
1075276e639SWarner Losh 	{ "netascii",	1 },
1085276e639SWarner Losh 	{ "octet",	0 },
1095276e639SWarner Losh 	{ NULL,		0 }
1105276e639SWarner Losh };
111ea022d16SRodney W. Grimes 
112ea022d16SRodney W. Grimes int
main(int argc,char * argv[])113dc4c3024SWarner Losh main(int argc, char *argv[])
114ea022d16SRodney W. Grimes {
115dc4c3024SWarner Losh 	struct tftphdr *tp;
1165276e639SWarner Losh 	int		peer;
1175276e639SWarner Losh 	socklen_t	peerlen, len;
1185276e639SWarner Losh 	ssize_t		n;
1195276e639SWarner Losh 	int		ch;
1208ea31785SWarner Losh 	char		*chroot_dir = NULL;
1218ea31785SWarner Losh 	struct passwd	*nobody;
122f49c0dc0SDavid Malone 	const char	*chuser = "nobody";
1235276e639SWarner Losh 	char		recvbuffer[MAXPKTSIZE];
124*79c342aaSMark Johnston 	int		allow_ro = 1, allow_wo = 1, block = 0, on = 1;
125b4736c90SDag-Erling Smørgrav 	pid_t		pid;
126ea022d16SRodney W. Grimes 
1273ec73cf1SBrian Somers 	tzset();			/* syslog in localtime */
1285276e639SWarner Losh 	acting_as_client = 0;
1293ec73cf1SBrian Somers 
1305276e639SWarner Losh 	tftp_openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
131*79c342aaSMark Johnston 	while ((ch = getopt(argc, argv, "bcCd::F:lnoOp:s:Su:U:wW")) != -1) {
132ea022d16SRodney W. Grimes 		switch (ch) {
133*79c342aaSMark Johnston 		case 'b':
134*79c342aaSMark Johnston 			block = 1;
135*79c342aaSMark Johnston 			break;
1361ed0e5d2SBill Fumerola 		case 'c':
1371ed0e5d2SBill Fumerola 			ipchroot = 1;
1381ed0e5d2SBill Fumerola 			break;
1391ed0e5d2SBill Fumerola 		case 'C':
1401ed0e5d2SBill Fumerola 			ipchroot = 2;
1411ed0e5d2SBill Fumerola 			break;
1425276e639SWarner Losh 		case 'd':
1439f6f6494SDag-Erling Smørgrav 			if (optarg == NULL)
1449f6f6494SDag-Erling Smørgrav 				debug++;
1459f6f6494SDag-Erling Smørgrav 			else if (atoi(optarg) != 0)
1465276e639SWarner Losh 				debug += atoi(optarg);
1475276e639SWarner Losh 			else
1485276e639SWarner Losh 				debug |= debug_finds(optarg);
1495276e639SWarner Losh 			break;
150dba0fd30SEdwin Groothuis 		case 'F':
151dba0fd30SEdwin Groothuis 			newfile_format = optarg;
152dba0fd30SEdwin Groothuis 			break;
153ea022d16SRodney W. Grimes 		case 'l':
154ea022d16SRodney W. Grimes 			logging = 1;
155ea022d16SRodney W. Grimes 			break;
156ea022d16SRodney W. Grimes 		case 'n':
157ea022d16SRodney W. Grimes 			suppress_naks = 1;
158ea022d16SRodney W. Grimes 			break;
1595276e639SWarner Losh 		case 'o':
1605276e639SWarner Losh 			options_rfc_enabled = 0;
1615276e639SWarner Losh 			break;
1625276e639SWarner Losh 		case 'O':
1635276e639SWarner Losh 			options_extra_enabled = 0;
1645276e639SWarner Losh 			break;
1655276e639SWarner Losh 		case 'p':
1664d09eb87SDag-Erling Smørgrav 			packetdroppercentage = (unsigned int)atoi(optarg);
1675276e639SWarner Losh 			tftp_log(LOG_INFO,
1685276e639SWarner Losh 			    "Randomly dropping %d out of 100 packets",
1695276e639SWarner Losh 			    packetdroppercentage);
1705276e639SWarner Losh 			break;
1718ea31785SWarner Losh 		case 's':
1728ea31785SWarner Losh 			chroot_dir = optarg;
1738ea31785SWarner Losh 			break;
174273a307dSEugene Grosbein 		case 'S':
175273a307dSEugene Grosbein 			check_woth = -1;
176273a307dSEugene Grosbein 			break;
177f62eaadfSGarrett Wollman 		case 'u':
178f62eaadfSGarrett Wollman 			chuser = optarg;
179f62eaadfSGarrett Wollman 			break;
180eff77877SMatthew N. Dodd 		case 'U':
181eff77877SMatthew N. Dodd 			mask = strtol(optarg, NULL, 0);
182eff77877SMatthew N. Dodd 			break;
183eff77877SMatthew N. Dodd 		case 'w':
184eff77877SMatthew N. Dodd 			create_new = 1;
185eff77877SMatthew N. Dodd 			break;
186dba0fd30SEdwin Groothuis 		case 'W':
187dba0fd30SEdwin Groothuis 			create_new = 1;
188dba0fd30SEdwin Groothuis 			increase_name = 1;
189dba0fd30SEdwin Groothuis 			break;
190ea022d16SRodney W. Grimes 		default:
1915276e639SWarner Losh 			tftp_log(LOG_WARNING,
1925276e639SWarner Losh 				"ignoring unknown option -%c", ch);
193ea022d16SRodney W. Grimes 		}
194ea022d16SRodney W. Grimes 	}
195ea022d16SRodney W. Grimes 	if (optind < argc) {
196ea022d16SRodney W. Grimes 		struct dirlist *dirp;
197ea022d16SRodney W. Grimes 
198ea022d16SRodney W. Grimes 		/* Get list of directory prefixes. Skip relative pathnames. */
199ea022d16SRodney W. Grimes 		for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
200ea022d16SRodney W. Grimes 		     optind++) {
201ea022d16SRodney W. Grimes 			if (argv[optind][0] == '/') {
202ea022d16SRodney W. Grimes 				dirp->name = argv[optind];
203ea022d16SRodney W. Grimes 				dirp->len  = strlen(dirp->name);
204ea022d16SRodney W. Grimes 				dirp++;
205ea022d16SRodney W. Grimes 			}
206ea022d16SRodney W. Grimes 		}
207ea022d16SRodney W. Grimes 	}
2088ea31785SWarner Losh 	else if (chroot_dir) {
2098ea31785SWarner Losh 		dirs->name = "/";
2108ea31785SWarner Losh 		dirs->len = 1;
2118ea31785SWarner Losh 	}
212a273f3aeSBill Fumerola 	if (ipchroot > 0 && chroot_dir == NULL) {
2135276e639SWarner Losh 		tftp_log(LOG_ERR, "-c requires -s");
2141ed0e5d2SBill Fumerola 		exit(1);
2151ed0e5d2SBill Fumerola 	}
216ea022d16SRodney W. Grimes 
217eff77877SMatthew N. Dodd 	umask(mask);
218eff77877SMatthew N. Dodd 
2195276e639SWarner Losh 	/* Find out who we are talking to and what we are going to do */
2205276e639SWarner Losh 	peerlen = sizeof(peer_sock);
221*79c342aaSMark Johnston 	n = recvfrom(0, recvbuffer, MAXPKTSIZE, block ? 0 : MSG_DONTWAIT,
2225276e639SWarner Losh 	    (struct sockaddr *)&peer_sock, &peerlen);
223ea022d16SRodney W. Grimes 	if (n < 0) {
2245276e639SWarner Losh 		tftp_log(LOG_ERR, "recvfrom: %s", strerror(errno));
225ea022d16SRodney W. Grimes 		exit(1);
226ea022d16SRodney W. Grimes 	}
2275276e639SWarner Losh 	getnameinfo((struct sockaddr *)&peer_sock, peer_sock.ss_len,
2285276e639SWarner Losh 	    peername, sizeof(peername), NULL, 0, NI_NUMERICHOST);
2299f231af3SDag-Erling Smørgrav 	if ((size_t)n < 4 /* tftphdr */) {
2309f231af3SDag-Erling Smørgrav 		tftp_log(LOG_ERR, "Rejecting %zd-byte request from %s",
2319f231af3SDag-Erling Smørgrav 		    n, peername);
2329f231af3SDag-Erling Smørgrav 		exit(1);
2339f231af3SDag-Erling Smørgrav 	}
2345276e639SWarner Losh 
235*79c342aaSMark Johnston 	if (ioctl(0, FIONBIO, &on) < 0) {
236*79c342aaSMark Johnston 		tftp_log(LOG_ERR, "ioctl(FIONBIO): %s", strerror(errno));
237*79c342aaSMark Johnston 		exit(1);
238*79c342aaSMark Johnston 	}
239*79c342aaSMark Johnston 
240ea022d16SRodney W. Grimes 	/*
241ea022d16SRodney W. Grimes 	 * Now that we have read the message out of the UDP
242ea022d16SRodney W. Grimes 	 * socket, we fork and exit.  Thus, inetd will go back
243ea022d16SRodney W. Grimes 	 * to listening to the tftp port, and the next request
244ea022d16SRodney W. Grimes 	 * to come in will start up a new instance of tftpd.
245ea022d16SRodney W. Grimes 	 *
246ea022d16SRodney W. Grimes 	 * We do this so that inetd can run tftpd in "wait" mode.
247ea022d16SRodney W. Grimes 	 * The problem with tftpd running in "nowait" mode is that
248ea022d16SRodney W. Grimes 	 * inetd may get one or more successful "selects" on the
249ea022d16SRodney W. Grimes 	 * tftp port before we do our receive, so more than one
250ea022d16SRodney W. Grimes 	 * instance of tftpd may be started up.  Worse, if tftpd
251ea022d16SRodney W. Grimes 	 * break before doing the above "recvfrom", inetd would
252ea022d16SRodney W. Grimes 	 * spawn endless instances, clogging the system.
253ea022d16SRodney W. Grimes 	 */
254ea022d16SRodney W. Grimes 	pid = fork();
255ea022d16SRodney W. Grimes 	if (pid < 0) {
2565276e639SWarner Losh 		tftp_log(LOG_ERR, "fork: %s", strerror(errno));
257ea022d16SRodney W. Grimes 		exit(1);
258ea022d16SRodney W. Grimes 	} else if (pid != 0) {
259ea022d16SRodney W. Grimes 		exit(0);
260ea022d16SRodney W. Grimes 	}
261b4736c90SDag-Erling Smørgrav 	/* child */
2628ea31785SWarner Losh 
2634eb4663bSEnji Cooper #ifdef	LIBWRAP
2648ea31785SWarner Losh 	/*
2655276e639SWarner Losh 	 * See if the client is allowed to talk to me.
2665276e639SWarner Losh 	 * (This needs to be done before the chroot())
2675276e639SWarner Losh 	 */
2685276e639SWarner Losh 	{
2695276e639SWarner Losh 		struct request_info req;
2705276e639SWarner Losh 
2715276e639SWarner Losh 		request_init(&req, RQ_CLIENT_ADDR, peername, 0);
2725276e639SWarner Losh 		request_set(&req, RQ_DAEMON, "tftpd", 0);
2735276e639SWarner Losh 
2745276e639SWarner Losh 		if (hosts_access(&req) == 0) {
2755276e639SWarner Losh 			if (debug & DEBUG_ACCESS)
2765276e639SWarner Losh 				tftp_log(LOG_WARNING,
2775276e639SWarner Losh 				    "Access denied by 'tftpd' entry "
2785276e639SWarner Losh 				    "in /etc/hosts.allow");
2795276e639SWarner Losh 
2805276e639SWarner Losh 			/*
2815276e639SWarner Losh 			 * Full access might be disabled, but maybe the
2825276e639SWarner Losh 			 * client is allowed to do read-only access.
2835276e639SWarner Losh 			 */
2845276e639SWarner Losh 			request_set(&req, RQ_DAEMON, "tftpd-ro", 0);
2855276e639SWarner Losh 			allow_ro = hosts_access(&req);
2865276e639SWarner Losh 
2875276e639SWarner Losh 			request_set(&req, RQ_DAEMON, "tftpd-wo", 0);
2885276e639SWarner Losh 			allow_wo = hosts_access(&req);
2895276e639SWarner Losh 
2905276e639SWarner Losh 			if (allow_ro == 0 && allow_wo == 0) {
2915276e639SWarner Losh 				tftp_log(LOG_WARNING,
2925276e639SWarner Losh 				    "Unauthorized access from %s", peername);
2935276e639SWarner Losh 				exit(1);
2945276e639SWarner Losh 			}
2955276e639SWarner Losh 
2965276e639SWarner Losh 			if (debug & DEBUG_ACCESS) {
2975276e639SWarner Losh 				if (allow_ro)
2985276e639SWarner Losh 					tftp_log(LOG_WARNING,
2995276e639SWarner Losh 					    "But allowed readonly access "
3005276e639SWarner Losh 					    "via 'tftpd-ro' entry");
3015276e639SWarner Losh 				if (allow_wo)
3025276e639SWarner Losh 					tftp_log(LOG_WARNING,
3035276e639SWarner Losh 					    "But allowed writeonly access "
3045276e639SWarner Losh 					    "via 'tftpd-wo' entry");
3055276e639SWarner Losh 			}
3065276e639SWarner Losh 		} else
3075276e639SWarner Losh 			if (debug & DEBUG_ACCESS)
3085276e639SWarner Losh 				tftp_log(LOG_WARNING,
3095276e639SWarner Losh 				    "Full access allowed"
3105276e639SWarner Losh 				    "in /etc/hosts.allow");
3115276e639SWarner Losh 	}
3124eb4663bSEnji Cooper #endif
3135276e639SWarner Losh 
3145276e639SWarner Losh 	/*
3158ea31785SWarner Losh 	 * Since we exit here, we should do that only after the above
3168ea31785SWarner Losh 	 * recvfrom to keep inetd from constantly forking should there
3178ea31785SWarner Losh 	 * be a problem.  See the above comment about system clogging.
3188ea31785SWarner Losh 	 */
3198ea31785SWarner Losh 	if (chroot_dir) {
320a273f3aeSBill Fumerola 		if (ipchroot > 0) {
3211ed0e5d2SBill Fumerola 			char *tempchroot;
3221ed0e5d2SBill Fumerola 			struct stat sb;
3231ed0e5d2SBill Fumerola 			int statret;
3244dac6235SHajimu UMEMOTO 			struct sockaddr_storage ss;
3254dac6235SHajimu UMEMOTO 			char hbuf[NI_MAXHOST];
3261ed0e5d2SBill Fumerola 
3275276e639SWarner Losh 			statret = -1;
3285276e639SWarner Losh 			memcpy(&ss, &peer_sock, peer_sock.ss_len);
3294dac6235SHajimu UMEMOTO 			unmappedaddr((struct sockaddr_in6 *)&ss);
3304dac6235SHajimu UMEMOTO 			getnameinfo((struct sockaddr *)&ss, ss.ss_len,
3314dac6235SHajimu UMEMOTO 				    hbuf, sizeof(hbuf), NULL, 0,
3324f101318SHajimu UMEMOTO 				    NI_NUMERICHOST);
3334dac6235SHajimu UMEMOTO 			asprintf(&tempchroot, "%s/%s", chroot_dir, hbuf);
334a273f3aeSBill Fumerola 			if (ipchroot == 2)
3351ed0e5d2SBill Fumerola 				statret = stat(tempchroot, &sb);
336a273f3aeSBill Fumerola 			if (ipchroot == 1 ||
337a273f3aeSBill Fumerola 			    (statret == 0 && (sb.st_mode & S_IFDIR)))
3381ed0e5d2SBill Fumerola 				chroot_dir = tempchroot;
3391ed0e5d2SBill Fumerola 		}
3408ea31785SWarner Losh 		/* Must get this before chroot because /etc might go away */
341f62eaadfSGarrett Wollman 		if ((nobody = getpwnam(chuser)) == NULL) {
3425276e639SWarner Losh 			tftp_log(LOG_ERR, "%s: no such user", chuser);
3438ea31785SWarner Losh 			exit(1);
3448ea31785SWarner Losh 		}
3458ea31785SWarner Losh 		if (chroot(chroot_dir)) {
3465276e639SWarner Losh 			tftp_log(LOG_ERR, "chroot: %s: %s",
3475276e639SWarner Losh 			    chroot_dir, strerror(errno));
3488ea31785SWarner Losh 			exit(1);
3498ea31785SWarner Losh 		}
3500aabff28SMark Johnston 		if (chdir("/") != 0) {
3510aabff28SMark Johnston 			tftp_log(LOG_ERR, "chdir: %s", strerror(errno));
3520aabff28SMark Johnston 			exit(1);
3530aabff28SMark Johnston 		}
3543c0fa265SAlan Somers 		if (setgroups(1, &nobody->pw_gid) != 0) {
3553c0fa265SAlan Somers 			tftp_log(LOG_ERR, "setgroups failed");
3563c0fa265SAlan Somers 			exit(1);
3573c0fa265SAlan Somers 		}
35850e04779SEitan Adler 		if (setuid(nobody->pw_uid) != 0) {
35950e04779SEitan Adler 			tftp_log(LOG_ERR, "setuid failed");
36050e04779SEitan Adler 			exit(1);
36150e04779SEitan Adler 		}
362273a307dSEugene Grosbein 		if (check_woth == -1)
363273a307dSEugene Grosbein 			check_woth = 0;
3648ea31785SWarner Losh 	}
365273a307dSEugene Grosbein 	if (check_woth == -1)
366273a307dSEugene Grosbein 		check_woth = 1;
3678ea31785SWarner Losh 
3685276e639SWarner Losh 	len = sizeof(me_sock);
3695276e639SWarner Losh 	if (getsockname(0, (struct sockaddr *)&me_sock, &len) == 0) {
3705276e639SWarner Losh 		switch (me_sock.ss_family) {
3714dac6235SHajimu UMEMOTO 		case AF_INET:
3725276e639SWarner Losh 			((struct sockaddr_in *)&me_sock)->sin_port = 0;
3734dac6235SHajimu UMEMOTO 			break;
3744dac6235SHajimu UMEMOTO 		case AF_INET6:
3755276e639SWarner Losh 			((struct sockaddr_in6 *)&me_sock)->sin6_port = 0;
3764dac6235SHajimu UMEMOTO 			break;
3774dac6235SHajimu UMEMOTO 		default:
3784dac6235SHajimu UMEMOTO 			/* unsupported */
3794dac6235SHajimu UMEMOTO 			break;
3804dac6235SHajimu UMEMOTO 		}
3814dac6235SHajimu UMEMOTO 	} else {
3825276e639SWarner Losh 		memset(&me_sock, 0, sizeof(me_sock));
3835276e639SWarner Losh 		me_sock.ss_family = peer_sock.ss_family;
3845276e639SWarner Losh 		me_sock.ss_len = peer_sock.ss_len;
3854dac6235SHajimu UMEMOTO 	}
38677e83935SDag-Erling Smørgrav 	close(STDIN_FILENO);
38777e83935SDag-Erling Smørgrav 	close(STDOUT_FILENO);
38877e83935SDag-Erling Smørgrav 	close(STDERR_FILENO);
3895276e639SWarner Losh 	peer = socket(peer_sock.ss_family, SOCK_DGRAM, 0);
390ea022d16SRodney W. Grimes 	if (peer < 0) {
3915276e639SWarner Losh 		tftp_log(LOG_ERR, "socket: %s", strerror(errno));
392ea022d16SRodney W. Grimes 		exit(1);
393ea022d16SRodney W. Grimes 	}
3945276e639SWarner Losh 	if (bind(peer, (struct sockaddr *)&me_sock, me_sock.ss_len) < 0) {
3955276e639SWarner Losh 		tftp_log(LOG_ERR, "bind: %s", strerror(errno));
396ea022d16SRodney W. Grimes 		exit(1);
397ea022d16SRodney W. Grimes 	}
3985276e639SWarner Losh 
3995276e639SWarner Losh 	tp = (struct tftphdr *)recvbuffer;
400ea022d16SRodney W. Grimes 	tp->th_opcode = ntohs(tp->th_opcode);
4015276e639SWarner Losh 	if (tp->th_opcode == RRQ) {
4025276e639SWarner Losh 		if (allow_ro)
4031ed44fccSDag-Erling Smørgrav 			tftp_rrq(peer, tp->th_stuff, (size_t)n - 1);
4045276e639SWarner Losh 		else {
4055276e639SWarner Losh 			tftp_log(LOG_WARNING,
4065276e639SWarner Losh 			    "%s read access denied", peername);
4075276e639SWarner Losh 			exit(1);
4085276e639SWarner Losh 		}
4096301d647SAlan Somers 	} else if (tp->th_opcode == WRQ) {
4105276e639SWarner Losh 		if (allow_wo)
4111ed44fccSDag-Erling Smørgrav 			tftp_wrq(peer, tp->th_stuff, (size_t)n - 1);
4125276e639SWarner Losh 		else {
4135276e639SWarner Losh 			tftp_log(LOG_WARNING,
4145276e639SWarner Losh 			    "%s write access denied", peername);
4155276e639SWarner Losh 			exit(1);
4165276e639SWarner Losh 		}
4176301d647SAlan Somers 	} else
4186301d647SAlan Somers 		send_error(peer, EBADOP);
419ea022d16SRodney W. Grimes 	exit(1);
420ea022d16SRodney W. Grimes }
421ea022d16SRodney W. Grimes 
4227bc7e0c8SBrian Somers static void
reduce_path(char * fn)4237bc7e0c8SBrian Somers reduce_path(char *fn)
4247bc7e0c8SBrian Somers {
4257bc7e0c8SBrian Somers 	char *slash, *ptr;
4267bc7e0c8SBrian Somers 
4277bc7e0c8SBrian Somers 	/* Reduce all "/+./" to "/" (just in case we've got "/./../" later */
4287bc7e0c8SBrian Somers 	while ((slash = strstr(fn, "/./")) != NULL) {
4297bc7e0c8SBrian Somers 		for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--)
4307bc7e0c8SBrian Somers 			;
4317bc7e0c8SBrian Somers 		slash += 2;
4327bc7e0c8SBrian Somers 		while (*slash)
4337bc7e0c8SBrian Somers 			*++ptr = *++slash;
4347bc7e0c8SBrian Somers 	}
4357bc7e0c8SBrian Somers 
4367bc7e0c8SBrian Somers 	/* Now reduce all "/something/+../" to "/" */
4377bc7e0c8SBrian Somers 	while ((slash = strstr(fn, "/../")) != NULL) {
4387bc7e0c8SBrian Somers 		if (slash == fn)
4397bc7e0c8SBrian Somers 			break;
4407bc7e0c8SBrian Somers 		for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--)
4417bc7e0c8SBrian Somers 			;
4427bc7e0c8SBrian Somers 		for (ptr--; ptr >= fn; ptr--)
4437bc7e0c8SBrian Somers 			if (*ptr == '/')
4447bc7e0c8SBrian Somers 				break;
4457bc7e0c8SBrian Somers 		if (ptr < fn)
4467bc7e0c8SBrian Somers 			break;
4477bc7e0c8SBrian Somers 		slash += 3;
4487bc7e0c8SBrian Somers 		while (*slash)
4497bc7e0c8SBrian Somers 			*++ptr = *++slash;
4507bc7e0c8SBrian Somers 	}
4517bc7e0c8SBrian Somers }
4527bc7e0c8SBrian Somers 
4535276e639SWarner Losh static char *
parse_header(int peer,char * recvbuffer,size_t size,char ** filename,char ** mode)4541ed44fccSDag-Erling Smørgrav parse_header(int peer, char *recvbuffer, size_t size,
4555276e639SWarner Losh 	char **filename, char **mode)
456ea022d16SRodney W. Grimes {
457dc4c3024SWarner Losh 	struct formats *pf;
4584d09eb87SDag-Erling Smørgrav 	char	*cp;
4594d09eb87SDag-Erling Smørgrav 	size_t	i;
460ea022d16SRodney W. Grimes 
4615276e639SWarner Losh 	*mode = NULL;
4625276e639SWarner Losh 	cp = recvbuffer;
4635276e639SWarner Losh 
4645276e639SWarner Losh 	i = get_field(peer, recvbuffer, size);
4655276e639SWarner Losh 	if (i >= PATH_MAX) {
4665276e639SWarner Losh 		tftp_log(LOG_ERR, "Bad option - filename too long");
4675276e639SWarner Losh 		send_error(peer, EBADOP);
468ea022d16SRodney W. Grimes 		exit(1);
469ea022d16SRodney W. Grimes 	}
4705276e639SWarner Losh 	*filename = recvbuffer;
4715276e639SWarner Losh 	tftp_log(LOG_INFO, "Filename: '%s'", *filename);
4725276e639SWarner Losh 	cp += i;
4735276e639SWarner Losh 
4745276e639SWarner Losh 	i = get_field(peer, cp, size);
4755276e639SWarner Losh 	*mode = cp;
4765276e639SWarner Losh 
4775276e639SWarner Losh 	/* Find the file transfer mode */
4784d09eb87SDag-Erling Smørgrav 	for (; *cp; cp++)
4794d09eb87SDag-Erling Smørgrav 		if (isupper((unsigned char)*cp))
4804d09eb87SDag-Erling Smørgrav 			*cp = tolower((unsigned char)*cp);
481ea022d16SRodney W. Grimes 	for (pf = formats; pf->f_mode; pf++)
4825276e639SWarner Losh 		if (strcmp(pf->f_mode, *mode) == 0)
483ea022d16SRodney W. Grimes 			break;
4845276e639SWarner Losh 	if (pf->f_mode == NULL) {
4855276e639SWarner Losh 		tftp_log(LOG_ERR,
4865276e639SWarner Losh 		    "Bad option - Unknown transfer mode (%s)", *mode);
4875276e639SWarner Losh 		send_error(peer, EBADOP);
488ea022d16SRodney W. Grimes 		exit(1);
489ea022d16SRodney W. Grimes 	}
4905276e639SWarner Losh 	tftp_log(LOG_INFO, "Mode: '%s'", *mode);
4915276e639SWarner Losh 
4925276e639SWarner Losh 	return (cp + 1);
4935276e639SWarner Losh }
4945276e639SWarner Losh 
49514f0ab1cSBenno Rice /*
4965276e639SWarner Losh  * WRQ - receive a file from the client
49714f0ab1cSBenno Rice  */
4985276e639SWarner Losh void
tftp_wrq(int peer,char * recvbuffer,size_t size)4991ed44fccSDag-Erling Smørgrav tftp_wrq(int peer, char *recvbuffer, size_t size)
5005276e639SWarner Losh {
5015276e639SWarner Losh 	char *cp;
5025276e639SWarner Losh 	int has_options = 0, ecode;
5035276e639SWarner Losh 	char *filename, *mode;
5045276e639SWarner Losh 	char fnbuf[PATH_MAX];
505c9374115SDavid E. O'Brien 
5065276e639SWarner Losh 	cp = parse_header(peer, recvbuffer, size, &filename, &mode);
5075276e639SWarner Losh 	size -= (cp - recvbuffer) + 1;
5085276e639SWarner Losh 
5093c0fa265SAlan Somers 	strlcpy(fnbuf, filename, sizeof(fnbuf));
5105276e639SWarner Losh 	reduce_path(fnbuf);
5115276e639SWarner Losh 	filename = fnbuf;
5125276e639SWarner Losh 
5135276e639SWarner Losh 	if (size > 0) {
5145276e639SWarner Losh 		if (options_rfc_enabled)
5155276e639SWarner Losh 			has_options = !parse_options(peer, cp, size);
516c9374115SDavid E. O'Brien 		else
5175276e639SWarner Losh 			tftp_log(LOG_INFO, "Options found but not enabled");
518c9374115SDavid E. O'Brien 	}
519c9374115SDavid E. O'Brien 
5205276e639SWarner Losh 	ecode = validate_access(peer, &filename, WRQ);
5215276e639SWarner Losh 	if (ecode == 0) {
5225276e639SWarner Losh 		if (has_options)
5235276e639SWarner Losh 			send_oack(peer);
5245276e639SWarner Losh 		else
5255276e639SWarner Losh 			send_ack(peer, 0);
5265276e639SWarner Losh 	}
527ea022d16SRodney W. Grimes 	if (logging) {
5285276e639SWarner Losh 		tftp_log(LOG_INFO, "%s: write request for %s: %s", peername,
529ea022d16SRodney W. Grimes 			    filename, errtomsg(ecode));
530ea022d16SRodney W. Grimes 	}
5315276e639SWarner Losh 
532b7da179eSAlan Somers 	if (ecode) {
533b7da179eSAlan Somers 		send_error(peer, ecode);
534b7da179eSAlan Somers 		exit(1);
535b7da179eSAlan Somers 	}
5365276e639SWarner Losh 	tftp_recvfile(peer, mode);
5375276e639SWarner Losh 	exit(0);
5385276e639SWarner Losh }
5395276e639SWarner Losh 
5405276e639SWarner Losh /*
5415276e639SWarner Losh  * RRQ - send a file to the client
5425276e639SWarner Losh  */
5435276e639SWarner Losh void
tftp_rrq(int peer,char * recvbuffer,size_t size)5441ed44fccSDag-Erling Smørgrav tftp_rrq(int peer, char *recvbuffer, size_t size)
5455276e639SWarner Losh {
5465276e639SWarner Losh 	char *cp;
5475276e639SWarner Losh 	int has_options = 0, ecode;
5485276e639SWarner Losh 	char *filename, *mode;
5495276e639SWarner Losh 	char	fnbuf[PATH_MAX];
5505276e639SWarner Losh 
5515276e639SWarner Losh 	cp = parse_header(peer, recvbuffer, size, &filename, &mode);
5525276e639SWarner Losh 	size -= (cp - recvbuffer) + 1;
5535276e639SWarner Losh 
5543c0fa265SAlan Somers 	strlcpy(fnbuf, filename, sizeof(fnbuf));
5555276e639SWarner Losh 	reduce_path(fnbuf);
5565276e639SWarner Losh 	filename = fnbuf;
5575276e639SWarner Losh 
5585276e639SWarner Losh 	if (size > 0) {
5595276e639SWarner Losh 		if (options_rfc_enabled)
5605276e639SWarner Losh 			has_options = !parse_options(peer, cp, size);
5615276e639SWarner Losh 		else
5625276e639SWarner Losh 			tftp_log(LOG_INFO, "Options found but not enabled");
5635276e639SWarner Losh 	}
5645276e639SWarner Losh 
5655276e639SWarner Losh 	ecode = validate_access(peer, &filename, RRQ);
5665276e639SWarner Losh 	if (ecode == 0) {
5675276e639SWarner Losh 		if (has_options) {
5685276e639SWarner Losh 			int n;
5695276e639SWarner Losh 			char lrecvbuffer[MAXPKTSIZE];
5705276e639SWarner Losh 			struct tftphdr *rp = (struct tftphdr *)lrecvbuffer;
5715276e639SWarner Losh 
5725276e639SWarner Losh 			send_oack(peer);
5735276e639SWarner Losh 			n = receive_packet(peer, lrecvbuffer, MAXPKTSIZE,
5745276e639SWarner Losh 				NULL, timeoutpacket);
5755276e639SWarner Losh 			if (n < 0) {
5765276e639SWarner Losh 				if (debug & DEBUG_SIMPLE)
5775276e639SWarner Losh 					tftp_log(LOG_DEBUG, "Aborting: %s",
5785276e639SWarner Losh 					    rp_strerror(n));
5795276e639SWarner Losh 				return;
5805276e639SWarner Losh 			}
5815276e639SWarner Losh 			if (rp->th_opcode != ACK) {
5825276e639SWarner Losh 				if (debug & DEBUG_SIMPLE)
5835276e639SWarner Losh 					tftp_log(LOG_DEBUG,
5845276e639SWarner Losh 					    "Expected ACK, got %s on OACK",
5855276e639SWarner Losh 					    packettype(rp->th_opcode));
5865276e639SWarner Losh 				return;
5875276e639SWarner Losh 			}
5885276e639SWarner Losh 		}
5895276e639SWarner Losh 	}
5905276e639SWarner Losh 
5915276e639SWarner Losh 	if (logging)
5925276e639SWarner Losh 		tftp_log(LOG_INFO, "%s: read request for %s: %s", peername,
5935276e639SWarner Losh 			    filename, errtomsg(ecode));
5945276e639SWarner Losh 
595ea022d16SRodney W. Grimes 	if (ecode) {
596ea022d16SRodney W. Grimes 		/*
597ea022d16SRodney W. Grimes 		 * Avoid storms of naks to a RRQ broadcast for a relative
598ea022d16SRodney W. Grimes 		 * bootfile pathname from a diskless Sun.
599ea022d16SRodney W. Grimes 		 */
600ea022d16SRodney W. Grimes 		if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
601ea022d16SRodney W. Grimes 			exit(0);
6025276e639SWarner Losh 		send_error(peer, ecode);
603ea022d16SRodney W. Grimes 		exit(1);
604ea022d16SRodney W. Grimes 	}
6055276e639SWarner Losh 	tftp_xmitfile(peer, mode);
606ea022d16SRodney W. Grimes }
607ea022d16SRodney W. Grimes 
608ea022d16SRodney W. Grimes /*
609dba0fd30SEdwin Groothuis  * Find the next value for YYYYMMDD.nn when the file to be written should
610dba0fd30SEdwin Groothuis  * be unique. Due to the limitations of nn, we will fail if nn reaches 100.
611dba0fd30SEdwin Groothuis  * Besides, that is four updates per hour on a file, which is kind of
612dba0fd30SEdwin Groothuis  * execessive anyway.
613dba0fd30SEdwin Groothuis  */
614dba0fd30SEdwin Groothuis static int
find_next_name(char * filename,int * fd)615dba0fd30SEdwin Groothuis find_next_name(char *filename, int *fd)
616dba0fd30SEdwin Groothuis {
61725945af4SDag-Erling Smørgrav 	/*
61825945af4SDag-Erling Smørgrav 	 * GCC "knows" that we might write all of yyyymmdd plus the static
61925945af4SDag-Erling Smørgrav 	 * elemenents in the format into into newname and thus complains
62025945af4SDag-Erling Smørgrav 	 * unless we reduce the size.  This array is still too big, but since
62125945af4SDag-Erling Smørgrav 	 * the format is user supplied, it's not clear what a better limit
62225945af4SDag-Erling Smørgrav 	 * value would be and this is sufficent to silence the warnings.
62325945af4SDag-Erling Smørgrav 	 */
62425945af4SDag-Erling Smørgrav 	static const int suffix_len = strlen("..00");
62525945af4SDag-Erling Smørgrav 	char yyyymmdd[MAXPATHLEN - suffix_len];
626dba0fd30SEdwin Groothuis 	char newname[MAXPATHLEN];
62725945af4SDag-Erling Smørgrav 	int i, ret;
62825945af4SDag-Erling Smørgrav 	time_t tval;
62925945af4SDag-Erling Smørgrav 	size_t len, namelen;
63025945af4SDag-Erling Smørgrav 	struct tm lt;
631dba0fd30SEdwin Groothuis 
632dba0fd30SEdwin Groothuis 	/* Create the YYYYMMDD part of the filename */
633dba0fd30SEdwin Groothuis 	time(&tval);
634dba0fd30SEdwin Groothuis 	lt = *localtime(&tval);
635dba0fd30SEdwin Groothuis 	len = strftime(yyyymmdd, sizeof(yyyymmdd), newfile_format, &lt);
636dba0fd30SEdwin Groothuis 	if (len == 0) {
637dba0fd30SEdwin Groothuis 		syslog(LOG_WARNING,
63825945af4SDag-Erling Smørgrav 			"Filename suffix too long (%zu characters maximum)",
63925945af4SDag-Erling Smørgrav 			sizeof(yyyymmdd) - 1);
640dba0fd30SEdwin Groothuis 		return (EACCESS);
641dba0fd30SEdwin Groothuis 	}
642dba0fd30SEdwin Groothuis 
643dba0fd30SEdwin Groothuis 	/* Make sure the new filename is not too long */
64425945af4SDag-Erling Smørgrav 	namelen = strlen(filename);
64525945af4SDag-Erling Smørgrav 	if (namelen >= sizeof(newname) - len - suffix_len) {
646dba0fd30SEdwin Groothuis 		syslog(LOG_WARNING,
64725945af4SDag-Erling Smørgrav 			"Filename too long (%zu characters, %zu maximum)",
64825945af4SDag-Erling Smørgrav 			namelen,
64925945af4SDag-Erling Smørgrav 			sizeof(newname) - len - suffix_len - 1);
650dba0fd30SEdwin Groothuis 		return (EACCESS);
651dba0fd30SEdwin Groothuis 	}
652dba0fd30SEdwin Groothuis 
653dba0fd30SEdwin Groothuis 	/* Find the first file which doesn't exist */
654dba0fd30SEdwin Groothuis 	for (i = 0; i < 100; i++) {
65525945af4SDag-Erling Smørgrav 		ret = snprintf(newname, sizeof(newname), "%s.%s.%02d",
65625945af4SDag-Erling Smørgrav 		    filename, yyyymmdd, i);
65725945af4SDag-Erling Smørgrav 		/*
65825945af4SDag-Erling Smørgrav 		 * Size checked above so this can't happen, we'd use a
65925945af4SDag-Erling Smørgrav 		 * (void) cast, but gcc intentionally ignores that if
66025945af4SDag-Erling Smørgrav 		 * snprintf has __attribute__((warn_unused_result)).
66125945af4SDag-Erling Smørgrav 		 */
66225945af4SDag-Erling Smørgrav 		if (ret < 0 || (size_t)ret >= sizeof(newname))
66325945af4SDag-Erling Smørgrav 			__unreachable();
66425945af4SDag-Erling Smørgrav 		*fd = open(newname, O_WRONLY | O_CREAT | O_EXCL, 0666);
665dba0fd30SEdwin Groothuis 		if (*fd > 0)
666dba0fd30SEdwin Groothuis 			return 0;
667dba0fd30SEdwin Groothuis 	}
668dba0fd30SEdwin Groothuis 
669dba0fd30SEdwin Groothuis 	return (EEXIST);
670dba0fd30SEdwin Groothuis }
671dba0fd30SEdwin Groothuis 
672dba0fd30SEdwin Groothuis /*
673ea022d16SRodney W. Grimes  * Validate file access.  Since we
674ea022d16SRodney W. Grimes  * have no uid or gid, for now require
675ea022d16SRodney W. Grimes  * file to exist and be publicly
676ea022d16SRodney W. Grimes  * readable/writable.
677ea022d16SRodney W. Grimes  * If we were invoked with arguments
678ea022d16SRodney W. Grimes  * from inetd then the file must also be
679ea022d16SRodney W. Grimes  * in one of the given directory prefixes.
680ea022d16SRodney W. Grimes  * Note also, full path name must be
681ea022d16SRodney W. Grimes  * given as we have no login directory.
682ea022d16SRodney W. Grimes  */
683ea022d16SRodney W. Grimes int
validate_access(int peer,char ** filep,int mode)6845276e639SWarner Losh validate_access(int peer, char **filep, int mode)
685ea022d16SRodney W. Grimes {
686ea022d16SRodney W. Grimes 	static char pathname[MAXPATHLEN];
687c15290fbSDag-Erling Smørgrav 	struct stat sb;
688c15290fbSDag-Erling Smørgrav 	struct dirlist *dirp;
689ea022d16SRodney W. Grimes 	char *filename = *filep;
690c15290fbSDag-Erling Smørgrav 	int	err, fd;
691ea022d16SRodney W. Grimes 
692ea022d16SRodney W. Grimes 	/*
693ea022d16SRodney W. Grimes 	 * Prevent tricksters from getting around the directory restrictions
694ea022d16SRodney W. Grimes 	 */
695c15290fbSDag-Erling Smørgrav 	if (strncmp(filename, "../", 3) == 0 ||
696c15290fbSDag-Erling Smørgrav 	    strstr(filename, "/../") != NULL)
697ea022d16SRodney W. Grimes 		return (EACCESS);
698ea022d16SRodney W. Grimes 
699ea022d16SRodney W. Grimes 	if (*filename == '/') {
700ea022d16SRodney W. Grimes 		/*
701c15290fbSDag-Erling Smørgrav 		 * Absolute file name: allow the request if it's in one of the
702c15290fbSDag-Erling Smørgrav 		 * approved locations.
703ea022d16SRodney W. Grimes 		 */
704ea022d16SRodney W. Grimes 		for (dirp = dirs; dirp->name != NULL; dirp++) {
70521b5829dSDag-Erling Smørgrav 			if (dirp->len == 1)
706c15290fbSDag-Erling Smørgrav 				/* Only "/" can have len 1 */
70721b5829dSDag-Erling Smørgrav 				break;
70821b5829dSDag-Erling Smørgrav 			if (strncmp(filename, dirp->name, dirp->len) == 0 &&
70921b5829dSDag-Erling Smørgrav 			    filename[dirp->len] == '/')
710ea022d16SRodney W. Grimes 				break;
711ea022d16SRodney W. Grimes 		}
712ea022d16SRodney W. Grimes 		/* If directory list is empty, allow access to any file */
713ea022d16SRodney W. Grimes 		if (dirp->name == NULL && dirp != dirs)
714ea022d16SRodney W. Grimes 			return (EACCESS);
715c15290fbSDag-Erling Smørgrav 		if (stat(filename, &sb) != 0)
716ea022d16SRodney W. Grimes 			return (errno == ENOENT ? ENOTFOUND : EACCESS);
717c15290fbSDag-Erling Smørgrav 		if (!S_ISREG(sb.st_mode))
718ea022d16SRodney W. Grimes 			return (ENOTFOUND);
719ea022d16SRodney W. Grimes 		if (mode == RRQ) {
720c15290fbSDag-Erling Smørgrav 			if ((sb.st_mode & S_IROTH) == 0)
721ea022d16SRodney W. Grimes 				return (EACCESS);
722ea022d16SRodney W. Grimes 		} else {
723c15290fbSDag-Erling Smørgrav 			if (check_woth && (sb.st_mode & S_IWOTH) == 0)
724ea022d16SRodney W. Grimes 				return (EACCESS);
725ea022d16SRodney W. Grimes 		}
726ea022d16SRodney W. Grimes 	} else {
727ea022d16SRodney W. Grimes 		/*
728ea022d16SRodney W. Grimes 		 * Relative file name: search the approved locations for it.
729ea022d16SRodney W. Grimes 		 * If the file exists in one of the directories and isn't
730ea022d16SRodney W. Grimes 		 * readable, continue looking. However, change the error code
731ea022d16SRodney W. Grimes 		 * to give an indication that the file exists.
732ea022d16SRodney W. Grimes 		 */
733ea022d16SRodney W. Grimes 		err = ENOTFOUND;
734ea022d16SRodney W. Grimes 		for (dirp = dirs; dirp->name != NULL; dirp++) {
735fca08b7cSWarner Losh 			snprintf(pathname, sizeof(pathname), "%s/%s",
736fca08b7cSWarner Losh 			    dirp->name, filename);
737c15290fbSDag-Erling Smørgrav 			if (stat(pathname, &sb) != 0)
738c15290fbSDag-Erling Smørgrav 				continue;
739c15290fbSDag-Erling Smørgrav 			if (!S_ISREG(sb.st_mode))
740c15290fbSDag-Erling Smørgrav 				continue;
741ea022d16SRodney W. Grimes 			err = EACCESS;
742c15290fbSDag-Erling Smørgrav 			if (mode == RRQ) {
743c15290fbSDag-Erling Smørgrav 				if ((sb.st_mode & S_IROTH) == 0)
744c15290fbSDag-Erling Smørgrav 					continue;
745c15290fbSDag-Erling Smørgrav 			} else {
746c15290fbSDag-Erling Smørgrav 				if (check_woth && (sb.st_mode & S_IWOTH) == 0)
747c15290fbSDag-Erling Smørgrav 					continue;
748ea022d16SRodney W. Grimes 			}
749c15290fbSDag-Erling Smørgrav 			break;
750ea022d16SRodney W. Grimes 		}
751eff77877SMatthew N. Dodd 		if (dirp->name != NULL)
752ea022d16SRodney W. Grimes 			*filep = filename = pathname;
753eff77877SMatthew N. Dodd 		else if (mode == RRQ)
754eff77877SMatthew N. Dodd 			return (err);
755d89aca76SAlan Somers 		else if (err != ENOTFOUND || !create_new)
756d89aca76SAlan Somers 			return (err);
757ea022d16SRodney W. Grimes 	}
7585276e639SWarner Losh 
7595276e639SWarner Losh 	/*
7605276e639SWarner Losh 	 * This option is handled here because it (might) require(s) the
7615276e639SWarner Losh 	 * size of the file.
7625276e639SWarner Losh 	 */
763c15290fbSDag-Erling Smørgrav 	option_tsize(peer, NULL, mode, &sb);
7645276e639SWarner Losh 
765c15290fbSDag-Erling Smørgrav 	if (mode == RRQ) {
766eff77877SMatthew N. Dodd 		fd = open(filename, O_RDONLY);
767c15290fbSDag-Erling Smørgrav 	} else if (create_new) {
768dba0fd30SEdwin Groothuis 		if (increase_name) {
769c15290fbSDag-Erling Smørgrav 			err = find_next_name(filename, &fd);
770c15290fbSDag-Erling Smørgrav 			if (err > 0)
771c15290fbSDag-Erling Smørgrav 				return (err + 100);
772c15290fbSDag-Erling Smørgrav 		} else {
773dba0fd30SEdwin Groothuis 			fd = open(filename,
774dba0fd30SEdwin Groothuis 			    O_WRONLY | O_TRUNC | O_CREAT,
775dba0fd30SEdwin Groothuis 			    S_IRUSR | S_IWUSR | S_IRGRP |
776dba0fd30SEdwin Groothuis 			    S_IWGRP | S_IROTH | S_IWOTH );
777c15290fbSDag-Erling Smørgrav 		}
778c15290fbSDag-Erling Smørgrav 	} else {
779eff77877SMatthew N. Dodd 		fd = open(filename, O_WRONLY | O_TRUNC);
780eff77877SMatthew N. Dodd 	}
781ea022d16SRodney W. Grimes 	if (fd < 0)
782ea022d16SRodney W. Grimes 		return (errno + 100);
783c15290fbSDag-Erling Smørgrav 	file = fdopen(fd, mode == RRQ ? "r" : "w");
784ea022d16SRodney W. Grimes 	if (file == NULL) {
785e99c7b0dSMatthew N. Dodd 		close(fd);
786e99c7b0dSMatthew N. Dodd 		return (errno + 100);
787ea022d16SRodney W. Grimes 	}
788ea022d16SRodney W. Grimes 	return (0);
789ea022d16SRodney W. Grimes }
790ea022d16SRodney W. Grimes 
7915276e639SWarner Losh static void
tftp_xmitfile(int peer,const char * mode)7925276e639SWarner Losh tftp_xmitfile(int peer, const char *mode)
793ea022d16SRodney W. Grimes {
7945276e639SWarner Losh 	uint16_t block;
7955276e639SWarner Losh 	time_t now;
7965276e639SWarner Losh 	struct tftp_stats ts;
797ea022d16SRodney W. Grimes 
7983c0fa265SAlan Somers 	memset(&ts, 0, sizeof(ts));
7995276e639SWarner Losh 	now = time(NULL);
8005276e639SWarner Losh 	if (debug & DEBUG_SIMPLE)
8015276e639SWarner Losh 		tftp_log(LOG_DEBUG, "Transmitting file");
802ea022d16SRodney W. Grimes 
8035276e639SWarner Losh 	read_init(0, file, mode);
804ea022d16SRodney W. Grimes 	block = 1;
8055276e639SWarner Losh 	tftp_send(peer, &block, &ts);
8065276e639SWarner Losh 	read_close();
8075276e639SWarner Losh 	if (debug & DEBUG_SIMPLE)
808b713097aSMarius Strobl 		tftp_log(LOG_INFO, "Sent %jd bytes in %jd seconds",
809b713097aSMarius Strobl 		    (intmax_t)ts.amount, (intmax_t)time(NULL) - now);
810ea022d16SRodney W. Grimes }
811ea022d16SRodney W. Grimes 
8125276e639SWarner Losh static void
tftp_recvfile(int peer,const char * mode)8135276e639SWarner Losh tftp_recvfile(int peer, const char *mode)
814ff93f08cSDoug Ambrisko {
8155276e639SWarner Losh 	uint16_t block;
8165276e639SWarner Losh 	struct timeval now1, now2;
8175276e639SWarner Losh 	struct tftp_stats ts;
818ea022d16SRodney W. Grimes 
8195276e639SWarner Losh 	gettimeofday(&now1, NULL);
8205276e639SWarner Losh 	if (debug & DEBUG_SIMPLE)
8215276e639SWarner Losh 		tftp_log(LOG_DEBUG, "Receiving file");
822ea022d16SRodney W. Grimes 
8235276e639SWarner Losh 	write_init(0, file, mode);
824ea022d16SRodney W. Grimes 
825ea022d16SRodney W. Grimes 	block = 0;
8265276e639SWarner Losh 	tftp_receive(peer, &block, &ts, NULL, 0);
827ea022d16SRodney W. Grimes 
82804ebad38SMarius Strobl 	gettimeofday(&now2, NULL);
829ea022d16SRodney W. Grimes 
8305276e639SWarner Losh 	if (debug & DEBUG_SIMPLE) {
8315276e639SWarner Losh 		double f;
8325276e639SWarner Losh 		if (now1.tv_usec > now2.tv_usec) {
8335276e639SWarner Losh 			now2.tv_usec += 1000000;
8345276e639SWarner Losh 			now2.tv_sec--;
835ea022d16SRodney W. Grimes 		}
8365276e639SWarner Losh 
8375276e639SWarner Losh 		f = now2.tv_sec - now1.tv_sec +
8385276e639SWarner Losh 		    (now2.tv_usec - now1.tv_usec) / 100000.0;
8395276e639SWarner Losh 		tftp_log(LOG_INFO,
840b713097aSMarius Strobl 		    "Download of %jd bytes in %d blocks completed after %0.1f seconds\n",
841b713097aSMarius Strobl 		    (intmax_t)ts.amount, block, f);
8425276e639SWarner Losh 	}
8435276e639SWarner Losh 
844ea022d16SRodney W. Grimes 	return;
845ea022d16SRodney W. Grimes }
846