xref: /freebsd/libexec/tftpd/tftpd.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tftpd.c	8.1 (Berkeley) 6/4/93";
41 #endif
42 #endif /* not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 /*
47  * Trivial file transfer protocol server.
48  *
49  * This version includes many modifications by Jim Guyton
50  * <guyton@rand-unix>.
51  */
52 
53 #include <sys/param.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <sys/stat.h>
57 #include <sys/time.h>
58 
59 #include <netinet/in.h>
60 #include <arpa/tftp.h>
61 
62 #include <ctype.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <netdb.h>
66 #include <pwd.h>
67 #include <stdint.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <syslog.h>
72 #include <time.h>
73 #include <unistd.h>
74 
75 #include "tftp-file.h"
76 #include "tftp-io.h"
77 #include "tftp-utils.h"
78 #include "tftp-transfer.h"
79 #include "tftp-options.h"
80 
81 #ifdef	LIBWRAP
82 #include <tcpd.h>
83 #endif
84 
85 static void	tftp_wrq(int peer, char *, ssize_t);
86 static void	tftp_rrq(int peer, char *, ssize_t);
87 
88 /*
89  * Null-terminated directory prefix list for absolute pathname requests and
90  * search list for relative pathname requests.
91  *
92  * MAXDIRS should be at least as large as the number of arguments that
93  * inetd allows (currently 20).
94  */
95 #define MAXDIRS	20
96 static struct dirlist {
97 	const char	*name;
98 	int	len;
99 } dirs[MAXDIRS+1];
100 static int	suppress_naks;
101 static int	logging;
102 static int	ipchroot;
103 static int	create_new = 0;
104 static const char *newfile_format = "%Y%m%d";
105 static int	increase_name = 0;
106 static mode_t	mask = S_IWGRP | S_IWOTH;
107 
108 struct formats;
109 static void	tftp_recvfile(int peer, const char *mode);
110 static void	tftp_xmitfile(int peer, const char *mode);
111 static int	validate_access(int peer, char **, int);
112 static char	peername[NI_MAXHOST];
113 
114 static FILE *file;
115 
116 static struct formats {
117 	const char	*f_mode;
118 	int	f_convert;
119 } formats[] = {
120 	{ "netascii",	1 },
121 	{ "octet",	0 },
122 	{ NULL,		0 }
123 };
124 
125 int
126 main(int argc, char *argv[])
127 {
128 	struct tftphdr *tp;
129 	int		peer;
130 	socklen_t	peerlen, len;
131 	ssize_t		n;
132 	int		ch;
133 	char		*chroot_dir = NULL;
134 	struct passwd	*nobody;
135 	const char	*chuser = "nobody";
136 	char		recvbuffer[MAXPKTSIZE];
137 	int		allow_ro = 1, allow_wo = 1, on = 1;
138 	pid_t		pid;
139 
140 	tzset();			/* syslog in localtime */
141 	acting_as_client = 0;
142 
143 	tftp_openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
144 	while ((ch = getopt(argc, argv, "cCd::F:lnoOp:s:u:U:wW")) != -1) {
145 		switch (ch) {
146 		case 'c':
147 			ipchroot = 1;
148 			break;
149 		case 'C':
150 			ipchroot = 2;
151 			break;
152 		case 'd':
153 			if (optarg == NULL)
154 				debug++;
155 			else if (atoi(optarg) != 0)
156 				debug += atoi(optarg);
157 			else
158 				debug |= debug_finds(optarg);
159 			break;
160 		case 'F':
161 			newfile_format = optarg;
162 			break;
163 		case 'l':
164 			logging = 1;
165 			break;
166 		case 'n':
167 			suppress_naks = 1;
168 			break;
169 		case 'o':
170 			options_rfc_enabled = 0;
171 			break;
172 		case 'O':
173 			options_extra_enabled = 0;
174 			break;
175 		case 'p':
176 			packetdroppercentage = atoi(optarg);
177 			tftp_log(LOG_INFO,
178 			    "Randomly dropping %d out of 100 packets",
179 			    packetdroppercentage);
180 			break;
181 		case 's':
182 			chroot_dir = optarg;
183 			break;
184 		case 'u':
185 			chuser = optarg;
186 			break;
187 		case 'U':
188 			mask = strtol(optarg, NULL, 0);
189 			break;
190 		case 'w':
191 			create_new = 1;
192 			break;
193 		case 'W':
194 			create_new = 1;
195 			increase_name = 1;
196 			break;
197 		default:
198 			tftp_log(LOG_WARNING,
199 				"ignoring unknown option -%c", ch);
200 		}
201 	}
202 	if (optind < argc) {
203 		struct dirlist *dirp;
204 
205 		/* Get list of directory prefixes. Skip relative pathnames. */
206 		for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
207 		     optind++) {
208 			if (argv[optind][0] == '/') {
209 				dirp->name = argv[optind];
210 				dirp->len  = strlen(dirp->name);
211 				dirp++;
212 			}
213 		}
214 	}
215 	else if (chroot_dir) {
216 		dirs->name = "/";
217 		dirs->len = 1;
218 	}
219 	if (ipchroot > 0 && chroot_dir == NULL) {
220 		tftp_log(LOG_ERR, "-c requires -s");
221 		exit(1);
222 	}
223 
224 	umask(mask);
225 
226 	if (ioctl(0, FIONBIO, &on) < 0) {
227 		tftp_log(LOG_ERR, "ioctl(FIONBIO): %s", strerror(errno));
228 		exit(1);
229 	}
230 
231 	/* Find out who we are talking to and what we are going to do */
232 	peerlen = sizeof(peer_sock);
233 	n = recvfrom(0, recvbuffer, MAXPKTSIZE, 0,
234 	    (struct sockaddr *)&peer_sock, &peerlen);
235 	if (n < 0) {
236 		tftp_log(LOG_ERR, "recvfrom: %s", strerror(errno));
237 		exit(1);
238 	}
239 	getnameinfo((struct sockaddr *)&peer_sock, peer_sock.ss_len,
240 	    peername, sizeof(peername), NULL, 0, NI_NUMERICHOST);
241 
242 	/*
243 	 * Now that we have read the message out of the UDP
244 	 * socket, we fork and exit.  Thus, inetd will go back
245 	 * to listening to the tftp port, and the next request
246 	 * to come in will start up a new instance of tftpd.
247 	 *
248 	 * We do this so that inetd can run tftpd in "wait" mode.
249 	 * The problem with tftpd running in "nowait" mode is that
250 	 * inetd may get one or more successful "selects" on the
251 	 * tftp port before we do our receive, so more than one
252 	 * instance of tftpd may be started up.  Worse, if tftpd
253 	 * break before doing the above "recvfrom", inetd would
254 	 * spawn endless instances, clogging the system.
255 	 */
256 	pid = fork();
257 	if (pid < 0) {
258 		tftp_log(LOG_ERR, "fork: %s", strerror(errno));
259 		exit(1);
260 	} else if (pid != 0) {
261 		exit(0);
262 	}
263 	/* child */
264 
265 #ifdef	LIBWRAP
266 	/*
267 	 * See if the client is allowed to talk to me.
268 	 * (This needs to be done before the chroot())
269 	 */
270 	{
271 		struct request_info req;
272 
273 		request_init(&req, RQ_CLIENT_ADDR, peername, 0);
274 		request_set(&req, RQ_DAEMON, "tftpd", 0);
275 
276 		if (hosts_access(&req) == 0) {
277 			if (debug & DEBUG_ACCESS)
278 				tftp_log(LOG_WARNING,
279 				    "Access denied by 'tftpd' entry "
280 				    "in /etc/hosts.allow");
281 
282 			/*
283 			 * Full access might be disabled, but maybe the
284 			 * client is allowed to do read-only access.
285 			 */
286 			request_set(&req, RQ_DAEMON, "tftpd-ro", 0);
287 			allow_ro = hosts_access(&req);
288 
289 			request_set(&req, RQ_DAEMON, "tftpd-wo", 0);
290 			allow_wo = hosts_access(&req);
291 
292 			if (allow_ro == 0 && allow_wo == 0) {
293 				tftp_log(LOG_WARNING,
294 				    "Unauthorized access from %s", peername);
295 				exit(1);
296 			}
297 
298 			if (debug & DEBUG_ACCESS) {
299 				if (allow_ro)
300 					tftp_log(LOG_WARNING,
301 					    "But allowed readonly access "
302 					    "via 'tftpd-ro' entry");
303 				if (allow_wo)
304 					tftp_log(LOG_WARNING,
305 					    "But allowed writeonly access "
306 					    "via 'tftpd-wo' entry");
307 			}
308 		} else
309 			if (debug & DEBUG_ACCESS)
310 				tftp_log(LOG_WARNING,
311 				    "Full access allowed"
312 				    "in /etc/hosts.allow");
313 	}
314 #endif
315 
316 	/*
317 	 * Since we exit here, we should do that only after the above
318 	 * recvfrom to keep inetd from constantly forking should there
319 	 * be a problem.  See the above comment about system clogging.
320 	 */
321 	if (chroot_dir) {
322 		if (ipchroot > 0) {
323 			char *tempchroot;
324 			struct stat sb;
325 			int statret;
326 			struct sockaddr_storage ss;
327 			char hbuf[NI_MAXHOST];
328 
329 			statret = -1;
330 			memcpy(&ss, &peer_sock, peer_sock.ss_len);
331 			unmappedaddr((struct sockaddr_in6 *)&ss);
332 			getnameinfo((struct sockaddr *)&ss, ss.ss_len,
333 				    hbuf, sizeof(hbuf), NULL, 0,
334 				    NI_NUMERICHOST);
335 			asprintf(&tempchroot, "%s/%s", chroot_dir, hbuf);
336 			if (ipchroot == 2)
337 				statret = stat(tempchroot, &sb);
338 			if (ipchroot == 1 ||
339 			    (statret == 0 && (sb.st_mode & S_IFDIR)))
340 				chroot_dir = tempchroot;
341 		}
342 		/* Must get this before chroot because /etc might go away */
343 		if ((nobody = getpwnam(chuser)) == NULL) {
344 			tftp_log(LOG_ERR, "%s: no such user", chuser);
345 			exit(1);
346 		}
347 		if (chroot(chroot_dir)) {
348 			tftp_log(LOG_ERR, "chroot: %s: %s",
349 			    chroot_dir, strerror(errno));
350 			exit(1);
351 		}
352 		if (chdir("/") != 0) {
353 			tftp_log(LOG_ERR, "chdir: %s", strerror(errno));
354 			exit(1);
355 		}
356 		if (setgroups(1, &nobody->pw_gid) != 0) {
357 			tftp_log(LOG_ERR, "setgroups failed");
358 			exit(1);
359 		}
360 		if (setuid(nobody->pw_uid) != 0) {
361 			tftp_log(LOG_ERR, "setuid failed");
362 			exit(1);
363 		}
364 	}
365 
366 	len = sizeof(me_sock);
367 	if (getsockname(0, (struct sockaddr *)&me_sock, &len) == 0) {
368 		switch (me_sock.ss_family) {
369 		case AF_INET:
370 			((struct sockaddr_in *)&me_sock)->sin_port = 0;
371 			break;
372 		case AF_INET6:
373 			((struct sockaddr_in6 *)&me_sock)->sin6_port = 0;
374 			break;
375 		default:
376 			/* unsupported */
377 			break;
378 		}
379 	} else {
380 		memset(&me_sock, 0, sizeof(me_sock));
381 		me_sock.ss_family = peer_sock.ss_family;
382 		me_sock.ss_len = peer_sock.ss_len;
383 	}
384 	close(STDIN_FILENO);
385 	close(STDOUT_FILENO);
386 	close(STDERR_FILENO);
387 	peer = socket(peer_sock.ss_family, SOCK_DGRAM, 0);
388 	if (peer < 0) {
389 		tftp_log(LOG_ERR, "socket: %s", strerror(errno));
390 		exit(1);
391 	}
392 	if (bind(peer, (struct sockaddr *)&me_sock, me_sock.ss_len) < 0) {
393 		tftp_log(LOG_ERR, "bind: %s", strerror(errno));
394 		exit(1);
395 	}
396 
397 	tp = (struct tftphdr *)recvbuffer;
398 	tp->th_opcode = ntohs(tp->th_opcode);
399 	if (tp->th_opcode == RRQ) {
400 		if (allow_ro)
401 			tftp_rrq(peer, tp->th_stuff, n - 1);
402 		else {
403 			tftp_log(LOG_WARNING,
404 			    "%s read access denied", peername);
405 			exit(1);
406 		}
407 	} else if (tp->th_opcode == WRQ) {
408 		if (allow_wo)
409 			tftp_wrq(peer, tp->th_stuff, n - 1);
410 		else {
411 			tftp_log(LOG_WARNING,
412 			    "%s write access denied", peername);
413 			exit(1);
414 		}
415 	} else
416 		send_error(peer, EBADOP);
417 	exit(1);
418 }
419 
420 static void
421 reduce_path(char *fn)
422 {
423 	char *slash, *ptr;
424 
425 	/* Reduce all "/+./" to "/" (just in case we've got "/./../" later */
426 	while ((slash = strstr(fn, "/./")) != NULL) {
427 		for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--)
428 			;
429 		slash += 2;
430 		while (*slash)
431 			*++ptr = *++slash;
432 	}
433 
434 	/* Now reduce all "/something/+../" to "/" */
435 	while ((slash = strstr(fn, "/../")) != NULL) {
436 		if (slash == fn)
437 			break;
438 		for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--)
439 			;
440 		for (ptr--; ptr >= fn; ptr--)
441 			if (*ptr == '/')
442 				break;
443 		if (ptr < fn)
444 			break;
445 		slash += 3;
446 		while (*slash)
447 			*++ptr = *++slash;
448 	}
449 }
450 
451 static char *
452 parse_header(int peer, char *recvbuffer, ssize_t size,
453 	char **filename, char **mode)
454 {
455 	char	*cp;
456 	int	i;
457 	struct formats *pf;
458 
459 	*mode = NULL;
460 	cp = recvbuffer;
461 
462 	i = get_field(peer, recvbuffer, size);
463 	if (i >= PATH_MAX) {
464 		tftp_log(LOG_ERR, "Bad option - filename too long");
465 		send_error(peer, EBADOP);
466 		exit(1);
467 	}
468 	*filename = recvbuffer;
469 	tftp_log(LOG_INFO, "Filename: '%s'", *filename);
470 	cp += i;
471 
472 	i = get_field(peer, cp, size);
473 	*mode = cp;
474 	cp += i;
475 
476 	/* Find the file transfer mode */
477 	for (cp = *mode; *cp; cp++)
478 		if (isupper(*cp))
479 			*cp = tolower(*cp);
480 	for (pf = formats; pf->f_mode; pf++)
481 		if (strcmp(pf->f_mode, *mode) == 0)
482 			break;
483 	if (pf->f_mode == NULL) {
484 		tftp_log(LOG_ERR,
485 		    "Bad option - Unknown transfer mode (%s)", *mode);
486 		send_error(peer, EBADOP);
487 		exit(1);
488 	}
489 	tftp_log(LOG_INFO, "Mode: '%s'", *mode);
490 
491 	return (cp + 1);
492 }
493 
494 /*
495  * WRQ - receive a file from the client
496  */
497 void
498 tftp_wrq(int peer, char *recvbuffer, ssize_t size)
499 {
500 	char *cp;
501 	int has_options = 0, ecode;
502 	char *filename, *mode;
503 	char fnbuf[PATH_MAX];
504 
505 	cp = parse_header(peer, recvbuffer, size, &filename, &mode);
506 	size -= (cp - recvbuffer) + 1;
507 
508 	strlcpy(fnbuf, filename, sizeof(fnbuf));
509 	reduce_path(fnbuf);
510 	filename = fnbuf;
511 
512 	if (size > 0) {
513 		if (options_rfc_enabled)
514 			has_options = !parse_options(peer, cp, size);
515 		else
516 			tftp_log(LOG_INFO, "Options found but not enabled");
517 	}
518 
519 	ecode = validate_access(peer, &filename, WRQ);
520 	if (ecode == 0) {
521 		if (has_options)
522 			send_oack(peer);
523 		else
524 			send_ack(peer, 0);
525 	}
526 	if (logging) {
527 		tftp_log(LOG_INFO, "%s: write request for %s: %s", peername,
528 			    filename, errtomsg(ecode));
529 	}
530 
531 	if (ecode) {
532 		send_error(peer, ecode);
533 		exit(1);
534 	}
535 	tftp_recvfile(peer, mode);
536 	exit(0);
537 }
538 
539 /*
540  * RRQ - send a file to the client
541  */
542 void
543 tftp_rrq(int peer, char *recvbuffer, ssize_t size)
544 {
545 	char *cp;
546 	int has_options = 0, ecode;
547 	char *filename, *mode;
548 	char	fnbuf[PATH_MAX];
549 
550 	cp = parse_header(peer, recvbuffer, size, &filename, &mode);
551 	size -= (cp - recvbuffer) + 1;
552 
553 	strlcpy(fnbuf, filename, sizeof(fnbuf));
554 	reduce_path(fnbuf);
555 	filename = fnbuf;
556 
557 	if (size > 0) {
558 		if (options_rfc_enabled)
559 			has_options = !parse_options(peer, cp, size);
560 		else
561 			tftp_log(LOG_INFO, "Options found but not enabled");
562 	}
563 
564 	ecode = validate_access(peer, &filename, RRQ);
565 	if (ecode == 0) {
566 		if (has_options) {
567 			int n;
568 			char lrecvbuffer[MAXPKTSIZE];
569 			struct tftphdr *rp = (struct tftphdr *)lrecvbuffer;
570 
571 			send_oack(peer);
572 			n = receive_packet(peer, lrecvbuffer, MAXPKTSIZE,
573 				NULL, timeoutpacket);
574 			if (n < 0) {
575 				if (debug & DEBUG_SIMPLE)
576 					tftp_log(LOG_DEBUG, "Aborting: %s",
577 					    rp_strerror(n));
578 				return;
579 			}
580 			if (rp->th_opcode != ACK) {
581 				if (debug & DEBUG_SIMPLE)
582 					tftp_log(LOG_DEBUG,
583 					    "Expected ACK, got %s on OACK",
584 					    packettype(rp->th_opcode));
585 				return;
586 			}
587 		}
588 	}
589 
590 	if (logging)
591 		tftp_log(LOG_INFO, "%s: read request for %s: %s", peername,
592 			    filename, errtomsg(ecode));
593 
594 	if (ecode) {
595 		/*
596 		 * Avoid storms of naks to a RRQ broadcast for a relative
597 		 * bootfile pathname from a diskless Sun.
598 		 */
599 		if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
600 			exit(0);
601 		send_error(peer, ecode);
602 		exit(1);
603 	}
604 	tftp_xmitfile(peer, mode);
605 }
606 
607 /*
608  * Find the next value for YYYYMMDD.nn when the file to be written should
609  * be unique. Due to the limitations of nn, we will fail if nn reaches 100.
610  * Besides, that is four updates per hour on a file, which is kind of
611  * execessive anyway.
612  */
613 static int
614 find_next_name(char *filename, int *fd)
615 {
616 	int i;
617 	time_t tval;
618 	size_t len;
619 	struct tm lt;
620 	char yyyymmdd[MAXPATHLEN];
621 	char newname[MAXPATHLEN];
622 
623 	/* Create the YYYYMMDD part of the filename */
624 	time(&tval);
625 	lt = *localtime(&tval);
626 	len = strftime(yyyymmdd, sizeof(yyyymmdd), newfile_format, &lt);
627 	if (len == 0) {
628 		syslog(LOG_WARNING,
629 			"Filename suffix too long (%d characters maximum)",
630 			MAXPATHLEN);
631 		return (EACCESS);
632 	}
633 
634 	/* Make sure the new filename is not too long */
635 	if (strlen(filename) > MAXPATHLEN - len - 5) {
636 		syslog(LOG_WARNING,
637 			"Filename too long (%zd characters, %zd maximum)",
638 			strlen(filename), MAXPATHLEN - len - 5);
639 		return (EACCESS);
640 	}
641 
642 	/* Find the first file which doesn't exist */
643 	for (i = 0; i < 100; i++) {
644 		sprintf(newname, "%s.%s.%02d", filename, yyyymmdd, i);
645 		*fd = open(newname,
646 		    O_WRONLY | O_CREAT | O_EXCL,
647 		    S_IRUSR | S_IWUSR | S_IRGRP |
648 		    S_IWGRP | S_IROTH | S_IWOTH);
649 		if (*fd > 0)
650 			return 0;
651 	}
652 
653 	return (EEXIST);
654 }
655 
656 /*
657  * Validate file access.  Since we
658  * have no uid or gid, for now require
659  * file to exist and be publicly
660  * readable/writable.
661  * If we were invoked with arguments
662  * from inetd then the file must also be
663  * in one of the given directory prefixes.
664  * Note also, full path name must be
665  * given as we have no login directory.
666  */
667 int
668 validate_access(int peer, char **filep, int mode)
669 {
670 	struct stat stbuf;
671 	int	fd;
672 	int	error;
673 	struct dirlist *dirp;
674 	static char pathname[MAXPATHLEN];
675 	char *filename = *filep;
676 
677 	/*
678 	 * Prevent tricksters from getting around the directory restrictions
679 	 */
680 	if (strstr(filename, "/../"))
681 		return (EACCESS);
682 
683 	if (*filename == '/') {
684 		/*
685 		 * Allow the request if it's in one of the approved locations.
686 		 * Special case: check the null prefix ("/") by looking
687 		 * for length = 1 and relying on the arg. processing that
688 		 * it's a /.
689 		 */
690 		for (dirp = dirs; dirp->name != NULL; dirp++) {
691 			if (dirp->len == 1 ||
692 			    (!strncmp(filename, dirp->name, dirp->len) &&
693 			     filename[dirp->len] == '/'))
694 				    break;
695 		}
696 		/* If directory list is empty, allow access to any file */
697 		if (dirp->name == NULL && dirp != dirs)
698 			return (EACCESS);
699 		if (stat(filename, &stbuf) < 0)
700 			return (errno == ENOENT ? ENOTFOUND : EACCESS);
701 		if ((stbuf.st_mode & S_IFMT) != S_IFREG)
702 			return (ENOTFOUND);
703 		if (mode == RRQ) {
704 			if ((stbuf.st_mode & S_IROTH) == 0)
705 				return (EACCESS);
706 		} else {
707 			if ((stbuf.st_mode & S_IWOTH) == 0)
708 				return (EACCESS);
709 		}
710 	} else {
711 		int err;
712 
713 		/*
714 		 * Relative file name: search the approved locations for it.
715 		 * Don't allow write requests that avoid directory
716 		 * restrictions.
717 		 */
718 
719 		if (!strncmp(filename, "../", 3))
720 			return (EACCESS);
721 
722 		/*
723 		 * If the file exists in one of the directories and isn't
724 		 * readable, continue looking. However, change the error code
725 		 * to give an indication that the file exists.
726 		 */
727 		err = ENOTFOUND;
728 		for (dirp = dirs; dirp->name != NULL; dirp++) {
729 			snprintf(pathname, sizeof(pathname), "%s/%s",
730 				dirp->name, filename);
731 			if (stat(pathname, &stbuf) == 0 &&
732 			    (stbuf.st_mode & S_IFMT) == S_IFREG) {
733 				if (mode == RRQ) {
734 					if ((stbuf.st_mode & S_IROTH) != 0)
735 						break;
736 				} else {
737 					if ((stbuf.st_mode & S_IWOTH) != 0)
738 						break;
739 				}
740 				err = EACCESS;
741 			}
742 		}
743 		if (dirp->name != NULL)
744 			*filep = filename = pathname;
745 		else if (mode == RRQ)
746 			return (err);
747 		else if (err != ENOTFOUND || !create_new)
748 			return (err);
749 	}
750 
751 	/*
752 	 * This option is handled here because it (might) require(s) the
753 	 * size of the file.
754 	 */
755 	option_tsize(peer, NULL, mode, &stbuf);
756 
757 	if (mode == RRQ)
758 		fd = open(filename, O_RDONLY);
759 	else {
760 		if (create_new) {
761 			if (increase_name) {
762 				error = find_next_name(filename, &fd);
763 				if (error > 0)
764 					return (error + 100);
765 			} else
766 				fd = open(filename,
767 				    O_WRONLY | O_TRUNC | O_CREAT,
768 				    S_IRUSR | S_IWUSR | S_IRGRP |
769 				    S_IWGRP | S_IROTH | S_IWOTH );
770 		} else
771 			fd = open(filename, O_WRONLY | O_TRUNC);
772 	}
773 	if (fd < 0)
774 		return (errno + 100);
775 	file = fdopen(fd, (mode == RRQ)? "r":"w");
776 	if (file == NULL) {
777 		close(fd);
778 		return (errno + 100);
779 	}
780 	return (0);
781 }
782 
783 static void
784 tftp_xmitfile(int peer, const char *mode)
785 {
786 	uint16_t block;
787 	time_t now;
788 	struct tftp_stats ts;
789 
790 	memset(&ts, 0, sizeof(ts));
791 	now = time(NULL);
792 	if (debug & DEBUG_SIMPLE)
793 		tftp_log(LOG_DEBUG, "Transmitting file");
794 
795 	read_init(0, file, mode);
796 	block = 1;
797 	tftp_send(peer, &block, &ts);
798 	read_close();
799 	if (debug & DEBUG_SIMPLE)
800 		tftp_log(LOG_INFO, "Sent %jd bytes in %jd seconds",
801 		    (intmax_t)ts.amount, (intmax_t)time(NULL) - now);
802 }
803 
804 static void
805 tftp_recvfile(int peer, const char *mode)
806 {
807 	uint16_t block;
808 	struct timeval now1, now2;
809 	struct tftp_stats ts;
810 
811 	gettimeofday(&now1, NULL);
812 	if (debug & DEBUG_SIMPLE)
813 		tftp_log(LOG_DEBUG, "Receiving file");
814 
815 	write_init(0, file, mode);
816 
817 	block = 0;
818 	tftp_receive(peer, &block, &ts, NULL, 0);
819 
820 	gettimeofday(&now2, NULL);
821 
822 	if (debug & DEBUG_SIMPLE) {
823 		double f;
824 		if (now1.tv_usec > now2.tv_usec) {
825 			now2.tv_usec += 1000000;
826 			now2.tv_sec--;
827 		}
828 
829 		f = now2.tv_sec - now1.tv_sec +
830 		    (now2.tv_usec - now1.tv_usec) / 100000.0;
831 		tftp_log(LOG_INFO,
832 		    "Download of %jd bytes in %d blocks completed after %0.1f seconds\n",
833 		    (intmax_t)ts.amount, block, f);
834 	}
835 
836 	return;
837 }
838