Home | History | Annotate | Line # | Download | only in tftpd
tftpd.c revision 1.15
      1 /*	$NetBSD: tftpd.c,v 1.15 1998/09/20 04:44:55 explorer Exp $	*/
      2 
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #if 0
     41 static char sccsid[] = "@(#)tftpd.c	8.1 (Berkeley) 6/4/93";
     42 #else
     43 __RCSID("$NetBSD: tftpd.c,v 1.15 1998/09/20 04:44:55 explorer Exp $");
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * Trivial file transfer protocol server.
     49  *
     50  * This version includes many modifications by Jim Guyton
     51  * <guyton@rand-unix>.
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/stat.h>
     57 #include <sys/socket.h>
     58 
     59 #include <signal.h>
     60 #include <fcntl.h>
     61 
     62 #include <netinet/in.h>
     63 #include <arpa/tftp.h>
     64 #include <arpa/inet.h>
     65 
     66 #include <ctype.h>
     67 #include <errno.h>
     68 #include <fcntl.h>
     69 #include <grp.h>
     70 #include <netdb.h>
     71 #include <pwd.h>
     72 #include <setjmp.h>
     73 #include <signal.h>
     74 #include <stdio.h>
     75 #include <stdlib.h>
     76 #include <string.h>
     77 #include <syslog.h>
     78 #include <unistd.h>
     79 
     80 #include "tftpsubs.h"
     81 
     82 #define	DEFAULTUSER	"nobody"
     83 
     84 #define	TIMEOUT		5
     85 
     86 extern	char *__progname;
     87 int	peer;
     88 int	rexmtval = TIMEOUT;
     89 int	maxtimeout = 5*TIMEOUT;
     90 
     91 #define	PKTSIZE	SEGSIZE+4
     92 char	buf[PKTSIZE];
     93 char	ackbuf[PKTSIZE];
     94 struct	sockaddr_in from;
     95 int	fromlen;
     96 
     97 /*
     98  * Null-terminated directory prefix list for absolute pathname requests and
     99  * search list for relative pathname requests.
    100  *
    101  * MAXDIRS should be at least as large as the number of arguments that
    102  * inetd allows (currently 20).
    103  */
    104 #define MAXDIRS	20
    105 static struct dirlist {
    106 	char	*name;
    107 	int	len;
    108 } dirs[MAXDIRS+1];
    109 static int	suppress_naks;
    110 static int	logging;
    111 static int	secure;
    112 static char	*securedir;
    113 
    114 struct formats;
    115 
    116 static void tftp __P((struct tftphdr *, int));
    117 static const char *errtomsg __P((int));
    118 static void nak __P((int));
    119 static char *verifyhost __P((struct sockaddr_in *));
    120 static void usage __P((void));
    121 void timer __P((int));
    122 void sendfile __P((struct formats *));
    123 void recvfile __P((struct formats *));
    124 void justquit __P((int));
    125 int validate_access __P((char **, int));
    126 int main __P((int, char **));
    127 
    128 struct formats {
    129 	char	*f_mode;
    130 	int	(*f_validate) __P((char **, int));
    131 	void	(*f_send) __P((struct formats *));
    132 	void	(*f_recv) __P((struct formats *));
    133 	int	f_convert;
    134 } formats[] = {
    135 	{ "netascii",	validate_access,	sendfile,	recvfile, 1 },
    136 	{ "octet",	validate_access,	sendfile,	recvfile, 0 },
    137 	{ 0 }
    138 };
    139 
    140 static void
    141 usage()
    142 {
    143 	syslog(LOG_ERR,
    144     "Usage: %s [-ln] [-u user] [-g group] [-s directory] [directory ...]",
    145 		    __progname);
    146 	exit(1);
    147 }
    148 
    149 int
    150 main(argc, argv)
    151 	int    argc;
    152 	char **argv;
    153 {
    154 	struct passwd *pwent;
    155 	struct group *grent;
    156 	struct tftphdr *tp;
    157 	int n = 0;
    158 	int ch, on;
    159 	int fd = 0;
    160 	struct sockaddr_in sin;
    161 	int len;
    162 	char *tgtuser, *tgtgroup, *ep;
    163 	uid_t curuid, tgtuid;
    164 	gid_t curgid, tgtgid;
    165 	long nid;
    166 
    167 	openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
    168 	tgtuser = DEFAULTUSER;
    169 	tgtgroup = NULL;
    170 	curuid = getuid();
    171 	curgid = getgid();
    172 
    173 	while ((ch = getopt(argc, argv, "g:lns:u:")) != -1)
    174 		switch (ch) {
    175 
    176 		case 'g':
    177 			tgtgroup = optarg;
    178 			break;
    179 
    180 		case 'l':
    181 			logging = 1;
    182 			break;
    183 
    184 		case 'n':
    185 			suppress_naks = 1;
    186 			break;
    187 
    188 		case 's':
    189 			secure = 1;
    190 			securedir = optarg;
    191 			break;
    192 
    193 		case 'u':
    194 			tgtuser = optarg;
    195 			break;
    196 
    197 		default:
    198 			usage();
    199 			break;
    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 
    216 	if (*tgtuser == '\0' || (tgtgroup != NULL && *tgtgroup == '\0'))
    217 		usage();
    218 
    219 	nid = (strtol(tgtuser, &ep, 10));
    220 	if (*ep == '\0') {
    221 		if (nid > UID_MAX) {
    222 			syslog(LOG_ERR, "uid %ld is too large", nid);
    223 			exit(1);
    224 		}
    225 		pwent = getpwuid((uid_t)nid);
    226 	} else
    227 		pwent = getpwnam(tgtuser);
    228 	if (pwent == NULL) {
    229 		syslog(LOG_ERR, "unknown user `%s'", tgtuser);
    230 		exit(1);
    231 	}
    232 	tgtuid = pwent->pw_uid;
    233 	tgtgid = pwent->pw_gid;
    234 
    235 	if (tgtgroup != NULL) {
    236 		nid = (strtol(tgtgroup, &ep, 10));
    237 		if (*ep == '\0') {
    238 			if (nid > GID_MAX) {
    239 				syslog(LOG_ERR, "gid %ld is too large", nid);
    240 				exit(1);
    241 			}
    242 			grent = getgrgid((gid_t)nid);
    243 		} else
    244 			grent = getgrnam(tgtgroup);
    245 		if (grent != NULL)
    246 			tgtgid = grent->gr_gid;
    247 		else {
    248 			syslog(LOG_ERR, "unknown group `%s'", tgtgroup);
    249 			exit(1);
    250 		}
    251 	}
    252 
    253 	if (secure) {
    254 		if (chdir(securedir) < 0) {
    255 			syslog(LOG_ERR, "chdir %s: %m", securedir);
    256 			exit(1);
    257 		}
    258 		if (chroot(".")) {
    259 			syslog(LOG_ERR, "chroot: %m");
    260 			exit(1);
    261 		}
    262 	}
    263 
    264 	syslog(LOG_DEBUG, "running as user `%s' (%d), group `%s' (%d)",
    265 	    tgtuser, tgtuid, tgtgroup ? tgtgroup : "(unspecified)" , tgtgid);
    266 	if (curgid != tgtgid) {
    267 		if (setgid(tgtgid)) {
    268 			syslog(LOG_ERR, "setgid to %d: %m", (int)tgtgid);
    269 			exit(1);
    270 		}
    271 		if (setgroups(0, NULL)) {
    272 			syslog(LOG_ERR, "setgroups: %m");
    273 			exit(1);
    274 		}
    275 	}
    276 
    277 	if (curuid != tgtuid) {
    278 		if (setuid(tgtuid)) {
    279 			syslog(LOG_ERR, "setuid to %d: %m", (int)tgtuid);
    280 			exit(1);
    281 		}
    282 	}
    283 
    284 	on = 1;
    285 	if (ioctl(fd, FIONBIO, &on) < 0) {
    286 		syslog(LOG_ERR, "ioctl(FIONBIO): %m");
    287 		exit(1);
    288 	}
    289 	fromlen = sizeof (from);
    290 	n = recvfrom(fd, buf, sizeof (buf), 0,
    291 	    (struct sockaddr *)&from, &fromlen);
    292 	if (n < 0) {
    293 		syslog(LOG_ERR, "recvfrom: %m");
    294 		exit(1);
    295 	}
    296 	/*
    297 	 * Now that we have read the message out of the UDP
    298 	 * socket, we fork and exit.  Thus, inetd will go back
    299 	 * to listening to the tftp port, and the next request
    300 	 * to come in will start up a new instance of tftpd.
    301 	 *
    302 	 * We do this so that inetd can run tftpd in "wait" mode.
    303 	 * The problem with tftpd running in "nowait" mode is that
    304 	 * inetd may get one or more successful "selects" on the
    305 	 * tftp port before we do our receive, so more than one
    306 	 * instance of tftpd may be started up.  Worse, if tftpd
    307 	 * break before doing the above "recvfrom", inetd would
    308 	 * spawn endless instances, clogging the system.
    309 	 */
    310 	{
    311 		int pid;
    312 		int i, j;
    313 
    314 		for (i = 1; i < 20; i++) {
    315 		    pid = fork();
    316 		    if (pid < 0) {
    317 				sleep(i);
    318 				/*
    319 				 * flush out to most recently sent request.
    320 				 *
    321 				 * This may drop some request, but those
    322 				 * will be resent by the clients when
    323 				 * they timeout.  The positive effect of
    324 				 * this flush is to (try to) prevent more
    325 				 * than one tftpd being started up to service
    326 				 * a single request from a single client.
    327 				 */
    328 				j = sizeof from;
    329 				i = recvfrom(fd, buf, sizeof (buf), 0,
    330 				    (struct sockaddr *)&from, &j);
    331 				if (i > 0) {
    332 					n = i;
    333 					fromlen = j;
    334 				}
    335 		    } else {
    336 				break;
    337 		    }
    338 		}
    339 		if (pid < 0) {
    340 			syslog(LOG_ERR, "fork: %m");
    341 			exit(1);
    342 		} else if (pid != 0) {
    343 			exit(0);
    344 		}
    345 	}
    346 
    347 	from.sin_len = sizeof(struct sockaddr_in);
    348 	from.sin_family = AF_INET;
    349 
    350 	/*
    351 	 * remember what address this was sent to, so we can respond on the
    352 	 * same interface
    353 	 */
    354 	len = sizeof(sin);
    355 	if (getsockname(fd, (struct sockaddr *)&sin, &len) == 0)
    356 		sin.sin_port = 0;
    357 	else {
    358 		memset(&sin, 0, sizeof(sin));
    359 		sin.sin_family = AF_INET;
    360 	}
    361 
    362 	alarm(0);
    363 	close(fd);
    364 	close(1);
    365 	peer = socket(AF_INET, SOCK_DGRAM, 0);
    366 	if (peer < 0) {
    367 		syslog(LOG_ERR, "socket: %m");
    368 		exit(1);
    369 	}
    370 	if (bind(peer, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    371 		syslog(LOG_ERR, "bind: %m");
    372 		exit(1);
    373 	}
    374 	if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
    375 		syslog(LOG_ERR, "connect: %m");
    376 		exit(1);
    377 	}
    378 	tp = (struct tftphdr *)buf;
    379 	tp->th_opcode = ntohs(tp->th_opcode);
    380 	if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
    381 		tftp(tp, n);
    382 	exit(1);
    383 }
    384 
    385 /*
    386  * Handle initial connection protocol.
    387  */
    388 static void
    389 tftp(tp, size)
    390 	struct tftphdr *tp;
    391 	int size;
    392 {
    393 	char *cp;
    394 	int first = 1, ecode;
    395 	struct formats *pf;
    396 	char *filename, *mode = NULL; /* XXX gcc */
    397 
    398 	filename = cp = tp->th_stuff;
    399 again:
    400 	while (cp < buf + size) {
    401 		if (*cp == '\0')
    402 			break;
    403 		cp++;
    404 	}
    405 	if (*cp != '\0') {
    406 		nak(EBADOP);
    407 		exit(1);
    408 	}
    409 	if (first) {
    410 		mode = ++cp;
    411 		first = 0;
    412 		goto again;
    413 	}
    414 	for (cp = mode; *cp; cp++)
    415 		if (isupper(*cp))
    416 			*cp = tolower(*cp);
    417 	for (pf = formats; pf->f_mode; pf++)
    418 		if (strcmp(pf->f_mode, mode) == 0)
    419 			break;
    420 	if (pf->f_mode == 0) {
    421 		nak(EBADOP);
    422 		exit(1);
    423 	}
    424 	ecode = (*pf->f_validate)(&filename, tp->th_opcode);
    425 	if (logging) {
    426 		syslog(LOG_INFO, "%s: %s request for %s: %s",
    427 			verifyhost(&from),
    428 			tp->th_opcode == WRQ ? "write" : "read",
    429 			filename, errtomsg(ecode));
    430 	}
    431 	if (ecode) {
    432 		/*
    433 		 * Avoid storms of naks to a RRQ broadcast for a relative
    434 		 * bootfile pathname from a diskless Sun.
    435 		 */
    436 		if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
    437 			exit(0);
    438 		nak(ecode);
    439 		exit(1);
    440 	}
    441 	if (tp->th_opcode == WRQ)
    442 		(*pf->f_recv)(pf);
    443 	else
    444 		(*pf->f_send)(pf);
    445 	exit(0);
    446 }
    447 
    448 
    449 FILE *file;
    450 
    451 /*
    452  * Validate file access.  Since we
    453  * have no uid or gid, for now require
    454  * file to exist and be publicly
    455  * readable/writable.
    456  * If we were invoked with arguments
    457  * from inetd then the file must also be
    458  * in one of the given directory prefixes.
    459  * Note also, full path name must be
    460  * given as we have no login directory.
    461  */
    462 int
    463 validate_access(filep, mode)
    464 	char **filep;
    465 	int mode;
    466 {
    467 	struct stat stbuf;
    468 	int	fd;
    469 	struct dirlist *dirp;
    470 	static char pathname[MAXPATHLEN];
    471 	char *filename = *filep;
    472 
    473 	/*
    474 	 * Prevent tricksters from getting around the directory restrictions
    475 	 */
    476 	if (strstr(filename, "/../"))
    477 		return (EACCESS);
    478 
    479 	if (*filename == '/') {
    480 		/*
    481 		 * Allow the request if it's in one of the approved locations.
    482 		 * Special case: check the null prefix ("/") by looking
    483 		 * for length = 1 and relying on the arg. processing that
    484 		 * it's a /.
    485 		 */
    486 		for (dirp = dirs; dirp->name != NULL; dirp++) {
    487 			if (dirp->len == 1 ||
    488 			    (!strncmp(filename, dirp->name, dirp->len) &&
    489 			     filename[dirp->len] == '/'))
    490 				    break;
    491 		}
    492 		/* If directory list is empty, allow access to any file */
    493 		if (dirp->name == NULL && dirp != dirs)
    494 			return (EACCESS);
    495 		if (stat(filename, &stbuf) < 0)
    496 			return (errno == ENOENT ? ENOTFOUND : EACCESS);
    497 		if (!S_ISREG(stbuf.st_mode))
    498 			return (ENOTFOUND);
    499 		if (mode == RRQ) {
    500 			if ((stbuf.st_mode & S_IROTH) == 0)
    501 				return (EACCESS);
    502 		} else {
    503 			if ((stbuf.st_mode & S_IWOTH) == 0)
    504 				return (EACCESS);
    505 		}
    506 	} else {
    507 		int err;
    508 
    509 		/*
    510 		 * Relative file name: search the approved locations for it.
    511 		 * Don't allow write requests or ones that avoid directory
    512 		 * restrictions.
    513 		 */
    514 
    515 		if (mode != RRQ || !strncmp(filename, "../", 3))
    516 			return (EACCESS);
    517 
    518 		/*
    519 		 * If the file exists in one of the directories and isn't
    520 		 * readable, continue looking. However, change the error code
    521 		 * to give an indication that the file exists.
    522 		 */
    523 		err = ENOTFOUND;
    524 		if (dirs[0].name != NULL) {
    525 			for (dirp = dirs; dirp->name != NULL; dirp++) {
    526 				snprintf(pathname, sizeof pathname, "%s/%s",
    527 				    dirp->name, filename);
    528 				if (stat(pathname, &stbuf) == 0 &&
    529 				    (stbuf.st_mode & S_IFMT) == S_IFREG) {
    530 					if ((stbuf.st_mode & S_IROTH) != 0)
    531 						break;
    532 					err = EACCESS;
    533 				}
    534 			}
    535 			if (dirp->name == NULL)
    536 				return (err);
    537 			*filep = filename = pathname;
    538 		} else
    539 			*filep = filename;
    540 	}
    541 	fd = open(filename, mode == RRQ ? 0 : 1);
    542 	if (fd < 0)
    543 		return (errno + 100);
    544 	file = fdopen(fd, (mode == RRQ)? "r":"w");
    545 	if (file == NULL) {
    546 		return errno+100;
    547 	}
    548 	return (0);
    549 }
    550 
    551 int	timeout;
    552 jmp_buf	timeoutbuf;
    553 
    554 void
    555 timer(dummy)
    556 	int dummy;
    557 {
    558 
    559 	timeout += rexmtval;
    560 	if (timeout >= maxtimeout)
    561 		exit(1);
    562 	longjmp(timeoutbuf, 1);
    563 }
    564 
    565 /*
    566  * Send the requested file.
    567  */
    568 void
    569 sendfile(pf)
    570 	struct formats *pf;
    571 {
    572 	struct tftphdr *dp;
    573 	struct tftphdr *ap;    /* ack packet */
    574 	int size, n;
    575 	volatile int block;
    576 
    577 	signal(SIGALRM, timer);
    578 	dp = r_init();
    579 	ap = (struct tftphdr *)ackbuf;
    580 	block = 1;
    581 	do {
    582 		size = readit(file, &dp, pf->f_convert);
    583 		if (size < 0) {
    584 			nak(errno + 100);
    585 			goto abort;
    586 		}
    587 		dp->th_opcode = htons((u_short)DATA);
    588 		dp->th_block = htons((u_short)block);
    589 		timeout = 0;
    590 		(void)setjmp(timeoutbuf);
    591 
    592 send_data:
    593 		if (send(peer, dp, size + 4, 0) != size + 4) {
    594 			syslog(LOG_ERR, "tftpd: write: %m");
    595 			goto abort;
    596 		}
    597 		read_ahead(file, pf->f_convert);
    598 		for ( ; ; ) {
    599 			alarm(rexmtval);        /* read the ack */
    600 			n = recv(peer, ackbuf, sizeof (ackbuf), 0);
    601 			alarm(0);
    602 			if (n < 0) {
    603 				syslog(LOG_ERR, "tftpd: read: %m");
    604 				goto abort;
    605 			}
    606 			ap->th_opcode = ntohs((u_short)ap->th_opcode);
    607 			ap->th_block = ntohs((u_short)ap->th_block);
    608 
    609 			if (ap->th_opcode == ERROR)
    610 				goto abort;
    611 
    612 			if (ap->th_opcode == ACK) {
    613 				if (ap->th_block == block)
    614 					break;
    615 				/* Re-synchronize with the other side */
    616 				(void) synchnet(peer);
    617 				if (ap->th_block == (block -1))
    618 					goto send_data;
    619 			}
    620 
    621 		}
    622 		block++;
    623 	} while (size == SEGSIZE);
    624 abort:
    625 	(void) fclose(file);
    626 }
    627 
    628 void
    629 justquit(dummy)
    630 	int dummy;
    631 {
    632 	exit(0);
    633 }
    634 
    635 /*
    636  * Receive a file.
    637  */
    638 void
    639 recvfile(pf)
    640 	struct formats *pf;
    641 {
    642 	struct tftphdr *dp;
    643 	struct tftphdr *ap;    /* ack buffer */
    644 	int n, size;
    645 	volatile int block;
    646 
    647 	signal(SIGALRM, timer);
    648 	dp = w_init();
    649 	ap = (struct tftphdr *)ackbuf;
    650 	block = 0;
    651 	do {
    652 		timeout = 0;
    653 		ap->th_opcode = htons((u_short)ACK);
    654 		ap->th_block = htons((u_short)block);
    655 		block++;
    656 		(void) setjmp(timeoutbuf);
    657 send_ack:
    658 		if (send(peer, ackbuf, 4, 0) != 4) {
    659 			syslog(LOG_ERR, "tftpd: write: %m");
    660 			goto abort;
    661 		}
    662 		write_behind(file, pf->f_convert);
    663 		for ( ; ; ) {
    664 			alarm(rexmtval);
    665 			n = recv(peer, dp, PKTSIZE, 0);
    666 			alarm(0);
    667 			if (n < 0) {            /* really? */
    668 				syslog(LOG_ERR, "tftpd: read: %m");
    669 				goto abort;
    670 			}
    671 			dp->th_opcode = ntohs((u_short)dp->th_opcode);
    672 			dp->th_block = ntohs((u_short)dp->th_block);
    673 			if (dp->th_opcode == ERROR)
    674 				goto abort;
    675 			if (dp->th_opcode == DATA) {
    676 				if (dp->th_block == block) {
    677 					break;   /* normal */
    678 				}
    679 				/* Re-synchronize with the other side */
    680 				(void) synchnet(peer);
    681 				if (dp->th_block == (block-1))
    682 					goto send_ack;          /* rexmit */
    683 			}
    684 		}
    685 		/*  size = write(file, dp->th_data, n - 4); */
    686 		size = writeit(file, &dp, n - 4, pf->f_convert);
    687 		if (size != (n-4)) {                    /* ahem */
    688 			if (size < 0) nak(errno + 100);
    689 			else nak(ENOSPACE);
    690 			goto abort;
    691 		}
    692 	} while (size == SEGSIZE);
    693 	write_behind(file, pf->f_convert);
    694 	(void) fclose(file);            /* close data file */
    695 
    696 	ap->th_opcode = htons((u_short)ACK);    /* send the "final" ack */
    697 	ap->th_block = htons((u_short)(block));
    698 	(void) send(peer, ackbuf, 4, 0);
    699 
    700 	signal(SIGALRM, justquit);      /* just quit on timeout */
    701 	alarm(rexmtval);
    702 	n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
    703 	alarm(0);
    704 	if (n >= 4 &&                   /* if read some data */
    705 	    dp->th_opcode == DATA &&    /* and got a data block */
    706 	    block == dp->th_block) {	/* then my last ack was lost */
    707 		(void) send(peer, ackbuf, 4, 0);     /* resend final ack */
    708 	}
    709 abort:
    710 	return;
    711 }
    712 
    713 const struct errmsg {
    714 	int	e_code;
    715 	const char *e_msg;
    716 } errmsgs[] = {
    717 	{ EUNDEF,	"Undefined error code" },
    718 	{ ENOTFOUND,	"File not found" },
    719 	{ EACCESS,	"Access violation" },
    720 	{ ENOSPACE,	"Disk full or allocation exceeded" },
    721 	{ EBADOP,	"Illegal TFTP operation" },
    722 	{ EBADID,	"Unknown transfer ID" },
    723 	{ EEXISTS,	"File already exists" },
    724 	{ ENOUSER,	"No such user" },
    725 	{ -1,		0 }
    726 };
    727 
    728 static const char *
    729 errtomsg(error)
    730 	int error;
    731 {
    732 	static char buf[20];
    733 	const struct errmsg *pe;
    734 
    735 	if (error == 0)
    736 		return "success";
    737 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    738 		if (pe->e_code == error)
    739 			return pe->e_msg;
    740 	sprintf(buf, "error %d", error);
    741 	return buf;
    742 }
    743 
    744 /*
    745  * Send a nak packet (error message).
    746  * Error code passed in is one of the
    747  * standard TFTP codes, or a UNIX errno
    748  * offset by 100.
    749  */
    750 static void
    751 nak(error)
    752 	int error;
    753 {
    754 	struct tftphdr *tp;
    755 	int length;
    756 	const struct errmsg *pe;
    757 
    758 	tp = (struct tftphdr *)buf;
    759 	tp->th_opcode = htons((u_short)ERROR);
    760 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    761 		if (pe->e_code == error)
    762 			break;
    763 	if (pe->e_code < 0) {
    764 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
    765 		strcpy(tp->th_msg, strerror(error - 100));
    766 	} else {
    767 		tp->th_code = htons((u_short)error);
    768 		strcpy(tp->th_msg, pe->e_msg);
    769 	}
    770 	length = strlen(pe->e_msg);
    771 	tp->th_msg[length] = '\0';
    772 	length += 5;
    773 	if (send(peer, buf, length, 0) != length)
    774 		syslog(LOG_ERR, "nak: %m");
    775 }
    776 
    777 static char *
    778 verifyhost(fromp)
    779 	struct sockaddr_in *fromp;
    780 {
    781 	struct hostent *hp;
    782 
    783 	hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (fromp->sin_addr),
    784 			    fromp->sin_family);
    785 	if (hp)
    786 		return hp->h_name;
    787 	else
    788 		return inet_ntoa(fromp->sin_addr);
    789 }
    790