Home | History | Annotate | Line # | Download | only in tftpd
tftpd.c revision 1.6
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1983 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1983 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.3  mycroft /*static char sccsid[] = "from: @(#)tftpd.c	5.13 (Berkeley) 2/26/91";*/
     42  1.6  mycroft static char rcsid[] = "$Id: tftpd.c,v 1.6 1995/06/03 22:48:30 mycroft Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd /*
     46  1.1      cgd  * Trivial file transfer protocol server.
     47  1.1      cgd  *
     48  1.1      cgd  * This version includes many modifications by Jim Guyton <guyton@rand-unix>
     49  1.1      cgd  */
     50  1.1      cgd 
     51  1.1      cgd #include <sys/types.h>
     52  1.1      cgd #include <sys/ioctl.h>
     53  1.1      cgd #include <sys/stat.h>
     54  1.1      cgd #include <signal.h>
     55  1.1      cgd #include <fcntl.h>
     56  1.1      cgd 
     57  1.1      cgd #include <sys/socket.h>
     58  1.1      cgd #include <netinet/in.h>
     59  1.1      cgd #include <arpa/tftp.h>
     60  1.1      cgd #include <netdb.h>
     61  1.1      cgd 
     62  1.1      cgd #include <setjmp.h>
     63  1.1      cgd #include <syslog.h>
     64  1.1      cgd #include <stdio.h>
     65  1.1      cgd #include <errno.h>
     66  1.1      cgd #include <ctype.h>
     67  1.1      cgd #include <string.h>
     68  1.1      cgd #include <stdlib.h>
     69  1.1      cgd 
     70  1.1      cgd #define	TIMEOUT		5
     71  1.1      cgd 
     72  1.1      cgd extern	int errno;
     73  1.5      cgd extern	char *__progname;
     74  1.2  mycroft struct	sockaddr_in s_in = { AF_INET };
     75  1.1      cgd int	peer;
     76  1.1      cgd int	rexmtval = TIMEOUT;
     77  1.1      cgd int	maxtimeout = 5*TIMEOUT;
     78  1.1      cgd 
     79  1.1      cgd #define	PKTSIZE	SEGSIZE+4
     80  1.1      cgd char	buf[PKTSIZE];
     81  1.1      cgd char	ackbuf[PKTSIZE];
     82  1.1      cgd struct	sockaddr_in from;
     83  1.1      cgd int	fromlen;
     84  1.1      cgd 
     85  1.1      cgd #define MAXARG	4
     86  1.1      cgd char	*dirs[MAXARG+1];
     87  1.1      cgd 
     88  1.4  mycroft int	secure = 0;
     89  1.4  mycroft 
     90  1.5      cgd static void
     91  1.5      cgd usage()
     92  1.5      cgd {
     93  1.5      cgd 	syslog(LOG_ERR, "Usage: %s [-s] [directory ...]\n", __progname);
     94  1.5      cgd 	exit(1);
     95  1.5      cgd }
     96  1.5      cgd 
     97  1.5      cgd main(argc, argv)
     98  1.5      cgd 	int    argc;
     99  1.5      cgd 	char **argv;
    100  1.1      cgd {
    101  1.1      cgd 	register struct tftphdr *tp;
    102  1.1      cgd 	register int n = 0;
    103  1.1      cgd 	int on = 1;
    104  1.5      cgd 	int fd = 0;
    105  1.5      cgd 	int c;
    106  1.1      cgd 
    107  1.1      cgd 	openlog("tftpd", LOG_PID, LOG_DAEMON);
    108  1.5      cgd 
    109  1.5      cgd 	while ((c = getopt(argc, argv, "s")) != -1)
    110  1.5      cgd 		switch (c) {
    111  1.5      cgd 		case 's':
    112  1.5      cgd 			secure = 1;
    113  1.5      cgd 			break;
    114  1.5      cgd 
    115  1.5      cgd 		default:
    116  1.5      cgd 			usage();
    117  1.5      cgd 			break;
    118  1.5      cgd 		}
    119  1.5      cgd 
    120  1.5      cgd 	for (; optind != argc; optind++) {
    121  1.4  mycroft 		if (!secure) {
    122  1.4  mycroft 			if (n >= MAXARG) {
    123  1.4  mycroft 				syslog(LOG_ERR, "too many directories\n");
    124  1.4  mycroft 				exit(1);
    125  1.4  mycroft 			} else
    126  1.5      cgd 				dirs[n++] = argv[optind];
    127  1.4  mycroft 		}
    128  1.5      cgd 		if (chdir(argv[optind])) {
    129  1.5      cgd 			syslog(LOG_ERR, "%s: %m\n", argv[optind]);
    130  1.4  mycroft 			exit(1);
    131  1.4  mycroft 		}
    132  1.4  mycroft 	}
    133  1.5      cgd 
    134  1.4  mycroft 	if (secure && chroot(".")) {
    135  1.4  mycroft 		syslog(LOG_ERR, "chroot: %m\n");
    136  1.4  mycroft 		exit(1);
    137  1.4  mycroft 	}
    138  1.5      cgd 
    139  1.5      cgd 	if (ioctl(fd, FIONBIO, &on) < 0) {
    140  1.1      cgd 		syslog(LOG_ERR, "ioctl(FIONBIO): %m\n");
    141  1.1      cgd 		exit(1);
    142  1.1      cgd 	}
    143  1.1      cgd 	fromlen = sizeof (from);
    144  1.5      cgd 	n = recvfrom(fd, buf, sizeof (buf), 0,
    145  1.1      cgd 	    (struct sockaddr *)&from, &fromlen);
    146  1.1      cgd 	if (n < 0) {
    147  1.1      cgd 		syslog(LOG_ERR, "recvfrom: %m\n");
    148  1.1      cgd 		exit(1);
    149  1.1      cgd 	}
    150  1.1      cgd 	/*
    151  1.1      cgd 	 * Now that we have read the message out of the UDP
    152  1.1      cgd 	 * socket, we fork and exit.  Thus, inetd will go back
    153  1.1      cgd 	 * to listening to the tftp port, and the next request
    154  1.1      cgd 	 * to come in will start up a new instance of tftpd.
    155  1.1      cgd 	 *
    156  1.1      cgd 	 * We do this so that inetd can run tftpd in "wait" mode.
    157  1.1      cgd 	 * The problem with tftpd running in "nowait" mode is that
    158  1.1      cgd 	 * inetd may get one or more successful "selects" on the
    159  1.1      cgd 	 * tftp port before we do our receive, so more than one
    160  1.1      cgd 	 * instance of tftpd may be started up.  Worse, if tftpd
    161  1.1      cgd 	 * break before doing the above "recvfrom", inetd would
    162  1.1      cgd 	 * spawn endless instances, clogging the system.
    163  1.1      cgd 	 */
    164  1.1      cgd 	{
    165  1.1      cgd 		int pid;
    166  1.1      cgd 		int i, j;
    167  1.1      cgd 
    168  1.1      cgd 		for (i = 1; i < 20; i++) {
    169  1.1      cgd 		    pid = fork();
    170  1.1      cgd 		    if (pid < 0) {
    171  1.1      cgd 				sleep(i);
    172  1.1      cgd 				/*
    173  1.1      cgd 				 * flush out to most recently sent request.
    174  1.1      cgd 				 *
    175  1.1      cgd 				 * This may drop some request, but those
    176  1.1      cgd 				 * will be resent by the clients when
    177  1.1      cgd 				 * they timeout.  The positive effect of
    178  1.1      cgd 				 * this flush is to (try to) prevent more
    179  1.1      cgd 				 * than one tftpd being started up to service
    180  1.1      cgd 				 * a single request from a single client.
    181  1.1      cgd 				 */
    182  1.1      cgd 				j = sizeof from;
    183  1.5      cgd 				i = recvfrom(fd, buf, sizeof (buf), 0,
    184  1.1      cgd 				    (struct sockaddr *)&from, &j);
    185  1.1      cgd 				if (i > 0) {
    186  1.1      cgd 					n = i;
    187  1.1      cgd 					fromlen = j;
    188  1.1      cgd 				}
    189  1.1      cgd 		    } else {
    190  1.1      cgd 				break;
    191  1.1      cgd 		    }
    192  1.1      cgd 		}
    193  1.1      cgd 		if (pid < 0) {
    194  1.1      cgd 			syslog(LOG_ERR, "fork: %m\n");
    195  1.1      cgd 			exit(1);
    196  1.1      cgd 		} else if (pid != 0) {
    197  1.1      cgd 			exit(0);
    198  1.1      cgd 		}
    199  1.1      cgd 	}
    200  1.6  mycroft 	from.sin_len = sizeof(struct sockaddr_in);
    201  1.1      cgd 	from.sin_family = AF_INET;
    202  1.1      cgd 	alarm(0);
    203  1.5      cgd 	close(fd);
    204  1.1      cgd 	close(1);
    205  1.1      cgd 	peer = socket(AF_INET, SOCK_DGRAM, 0);
    206  1.1      cgd 	if (peer < 0) {
    207  1.1      cgd 		syslog(LOG_ERR, "socket: %m\n");
    208  1.1      cgd 		exit(1);
    209  1.1      cgd 	}
    210  1.2  mycroft 	if (bind(peer, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
    211  1.1      cgd 		syslog(LOG_ERR, "bind: %m\n");
    212  1.1      cgd 		exit(1);
    213  1.1      cgd 	}
    214  1.1      cgd 	if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
    215  1.1      cgd 		syslog(LOG_ERR, "connect: %m\n");
    216  1.1      cgd 		exit(1);
    217  1.1      cgd 	}
    218  1.1      cgd 	tp = (struct tftphdr *)buf;
    219  1.1      cgd 	tp->th_opcode = ntohs(tp->th_opcode);
    220  1.1      cgd 	if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
    221  1.1      cgd 		tftp(tp, n);
    222  1.1      cgd 	exit(1);
    223  1.1      cgd }
    224  1.1      cgd 
    225  1.1      cgd int	validate_access();
    226  1.1      cgd int	sendfile(), recvfile();
    227  1.1      cgd 
    228  1.1      cgd struct formats {
    229  1.1      cgd 	char	*f_mode;
    230  1.1      cgd 	int	(*f_validate)();
    231  1.1      cgd 	int	(*f_send)();
    232  1.1      cgd 	int	(*f_recv)();
    233  1.1      cgd 	int	f_convert;
    234  1.1      cgd } formats[] = {
    235  1.1      cgd 	{ "netascii",	validate_access,	sendfile,	recvfile, 1 },
    236  1.1      cgd 	{ "octet",	validate_access,	sendfile,	recvfile, 0 },
    237  1.1      cgd #ifdef notdef
    238  1.1      cgd 	{ "mail",	validate_user,		sendmail,	recvmail, 1 },
    239  1.1      cgd #endif
    240  1.1      cgd 	{ 0 }
    241  1.1      cgd };
    242  1.1      cgd 
    243  1.1      cgd /*
    244  1.1      cgd  * Handle initial connection protocol.
    245  1.1      cgd  */
    246  1.1      cgd tftp(tp, size)
    247  1.1      cgd 	struct tftphdr *tp;
    248  1.1      cgd 	int size;
    249  1.1      cgd {
    250  1.1      cgd 	register char *cp;
    251  1.1      cgd 	int first = 1, ecode;
    252  1.1      cgd 	register struct formats *pf;
    253  1.1      cgd 	char *filename, *mode;
    254  1.1      cgd 
    255  1.1      cgd 	filename = cp = tp->th_stuff;
    256  1.1      cgd again:
    257  1.1      cgd 	while (cp < buf + size) {
    258  1.1      cgd 		if (*cp == '\0')
    259  1.1      cgd 			break;
    260  1.1      cgd 		cp++;
    261  1.1      cgd 	}
    262  1.1      cgd 	if (*cp != '\0') {
    263  1.1      cgd 		nak(EBADOP);
    264  1.1      cgd 		exit(1);
    265  1.1      cgd 	}
    266  1.1      cgd 	if (first) {
    267  1.1      cgd 		mode = ++cp;
    268  1.1      cgd 		first = 0;
    269  1.1      cgd 		goto again;
    270  1.1      cgd 	}
    271  1.1      cgd 	for (cp = mode; *cp; cp++)
    272  1.1      cgd 		if (isupper(*cp))
    273  1.1      cgd 			*cp = tolower(*cp);
    274  1.1      cgd 	for (pf = formats; pf->f_mode; pf++)
    275  1.1      cgd 		if (strcmp(pf->f_mode, mode) == 0)
    276  1.1      cgd 			break;
    277  1.1      cgd 	if (pf->f_mode == 0) {
    278  1.1      cgd 		nak(EBADOP);
    279  1.1      cgd 		exit(1);
    280  1.1      cgd 	}
    281  1.1      cgd 	ecode = (*pf->f_validate)(filename, tp->th_opcode);
    282  1.1      cgd 	if (ecode) {
    283  1.1      cgd 		nak(ecode);
    284  1.1      cgd 		exit(1);
    285  1.1      cgd 	}
    286  1.1      cgd 	if (tp->th_opcode == WRQ)
    287  1.1      cgd 		(*pf->f_recv)(pf);
    288  1.1      cgd 	else
    289  1.1      cgd 		(*pf->f_send)(pf);
    290  1.1      cgd 	exit(0);
    291  1.1      cgd }
    292  1.1      cgd 
    293  1.1      cgd 
    294  1.1      cgd FILE *file;
    295  1.1      cgd 
    296  1.1      cgd /*
    297  1.1      cgd  * Validate file access.  Since we
    298  1.1      cgd  * have no uid or gid, for now require
    299  1.1      cgd  * file to exist and be publicly
    300  1.1      cgd  * readable/writable.
    301  1.1      cgd  * If we were invoked with arguments
    302  1.1      cgd  * from inetd then the file must also be
    303  1.1      cgd  * in one of the given directory prefixes.
    304  1.1      cgd  * Note also, full path name must be
    305  1.1      cgd  * given as we have no login directory.
    306  1.1      cgd  */
    307  1.1      cgd validate_access(filename, mode)
    308  1.1      cgd 	char *filename;
    309  1.1      cgd 	int mode;
    310  1.1      cgd {
    311  1.1      cgd 	struct stat stbuf;
    312  1.1      cgd 	int	fd;
    313  1.1      cgd 	char *cp, **dirp;
    314  1.1      cgd 
    315  1.4  mycroft 	if (!secure) {
    316  1.4  mycroft 		if (*filename != '/')
    317  1.4  mycroft 			return (EACCESS);
    318  1.4  mycroft 		/*
    319  1.4  mycroft 		 * prevent tricksters from getting around the directory
    320  1.4  mycroft 		 * restrictions
    321  1.4  mycroft 		 */
    322  1.4  mycroft 		for (cp = filename + 1; *cp; cp++)
    323  1.4  mycroft 			if(*cp == '.' && strncmp(cp-1, "/../", 4) == 0)
    324  1.4  mycroft 				return(EACCESS);
    325  1.4  mycroft 		for (dirp = dirs; *dirp; dirp++)
    326  1.4  mycroft 			if (strncmp(filename, *dirp, strlen(*dirp)) == 0)
    327  1.4  mycroft 				break;
    328  1.4  mycroft 		if (*dirp==0 && dirp!=dirs)
    329  1.4  mycroft 			return (EACCESS);
    330  1.4  mycroft 	}
    331  1.1      cgd 	if (stat(filename, &stbuf) < 0)
    332  1.1      cgd 		return (errno == ENOENT ? ENOTFOUND : EACCESS);
    333  1.1      cgd 	if (mode == RRQ) {
    334  1.1      cgd 		if ((stbuf.st_mode&(S_IREAD >> 6)) == 0)
    335  1.1      cgd 			return (EACCESS);
    336  1.1      cgd 	} else {
    337  1.1      cgd 		if ((stbuf.st_mode&(S_IWRITE >> 6)) == 0)
    338  1.1      cgd 			return (EACCESS);
    339  1.1      cgd 	}
    340  1.1      cgd 	fd = open(filename, mode == RRQ ? 0 : 1);
    341  1.1      cgd 	if (fd < 0)
    342  1.1      cgd 		return (errno + 100);
    343  1.1      cgd 	file = fdopen(fd, (mode == RRQ)? "r":"w");
    344  1.1      cgd 	if (file == NULL) {
    345  1.1      cgd 		return errno+100;
    346  1.1      cgd 	}
    347  1.1      cgd 	return (0);
    348  1.1      cgd }
    349  1.1      cgd 
    350  1.1      cgd int	timeout;
    351  1.1      cgd jmp_buf	timeoutbuf;
    352  1.1      cgd 
    353  1.1      cgd void
    354  1.1      cgd timer()
    355  1.1      cgd {
    356  1.1      cgd 
    357  1.1      cgd 	timeout += rexmtval;
    358  1.1      cgd 	if (timeout >= maxtimeout)
    359  1.1      cgd 		exit(1);
    360  1.1      cgd 	longjmp(timeoutbuf, 1);
    361  1.1      cgd }
    362  1.1      cgd 
    363  1.1      cgd /*
    364  1.1      cgd  * Send the requested file.
    365  1.1      cgd  */
    366  1.1      cgd sendfile(pf)
    367  1.1      cgd 	struct formats *pf;
    368  1.1      cgd {
    369  1.1      cgd 	struct tftphdr *dp, *r_init();
    370  1.1      cgd 	register struct tftphdr *ap;    /* ack packet */
    371  1.1      cgd 	register int block = 1, size, n;
    372  1.1      cgd 
    373  1.1      cgd 	signal(SIGALRM, timer);
    374  1.1      cgd 	dp = r_init();
    375  1.1      cgd 	ap = (struct tftphdr *)ackbuf;
    376  1.1      cgd 	do {
    377  1.1      cgd 		size = readit(file, &dp, pf->f_convert);
    378  1.1      cgd 		if (size < 0) {
    379  1.1      cgd 			nak(errno + 100);
    380  1.1      cgd 			goto abort;
    381  1.1      cgd 		}
    382  1.1      cgd 		dp->th_opcode = htons((u_short)DATA);
    383  1.1      cgd 		dp->th_block = htons((u_short)block);
    384  1.1      cgd 		timeout = 0;
    385  1.1      cgd 		(void) setjmp(timeoutbuf);
    386  1.1      cgd 
    387  1.1      cgd send_data:
    388  1.1      cgd 		if (send(peer, dp, size + 4, 0) != size + 4) {
    389  1.1      cgd 			syslog(LOG_ERR, "tftpd: write: %m\n");
    390  1.1      cgd 			goto abort;
    391  1.1      cgd 		}
    392  1.1      cgd 		read_ahead(file, pf->f_convert);
    393  1.1      cgd 		for ( ; ; ) {
    394  1.1      cgd 			alarm(rexmtval);        /* read the ack */
    395  1.1      cgd 			n = recv(peer, ackbuf, sizeof (ackbuf), 0);
    396  1.1      cgd 			alarm(0);
    397  1.1      cgd 			if (n < 0) {
    398  1.1      cgd 				syslog(LOG_ERR, "tftpd: read: %m\n");
    399  1.1      cgd 				goto abort;
    400  1.1      cgd 			}
    401  1.1      cgd 			ap->th_opcode = ntohs((u_short)ap->th_opcode);
    402  1.1      cgd 			ap->th_block = ntohs((u_short)ap->th_block);
    403  1.1      cgd 
    404  1.1      cgd 			if (ap->th_opcode == ERROR)
    405  1.1      cgd 				goto abort;
    406  1.1      cgd 
    407  1.1      cgd 			if (ap->th_opcode == ACK) {
    408  1.1      cgd 				if (ap->th_block == block) {
    409  1.1      cgd 					break;
    410  1.1      cgd 				}
    411  1.1      cgd 				/* Re-synchronize with the other side */
    412  1.1      cgd 				(void) synchnet(peer);
    413  1.1      cgd 				if (ap->th_block == (block -1)) {
    414  1.1      cgd 					goto send_data;
    415  1.1      cgd 				}
    416  1.1      cgd 			}
    417  1.1      cgd 
    418  1.1      cgd 		}
    419  1.1      cgd 		block++;
    420  1.1      cgd 	} while (size == SEGSIZE);
    421  1.1      cgd abort:
    422  1.1      cgd 	(void) fclose(file);
    423  1.1      cgd }
    424  1.1      cgd 
    425  1.1      cgd void
    426  1.1      cgd justquit()
    427  1.1      cgd {
    428  1.1      cgd 	exit(0);
    429  1.1      cgd }
    430  1.1      cgd 
    431  1.1      cgd 
    432  1.1      cgd /*
    433  1.1      cgd  * Receive a file.
    434  1.1      cgd  */
    435  1.1      cgd recvfile(pf)
    436  1.1      cgd 	struct formats *pf;
    437  1.1      cgd {
    438  1.1      cgd 	struct tftphdr *dp, *w_init();
    439  1.1      cgd 	register struct tftphdr *ap;    /* ack buffer */
    440  1.1      cgd 	register int block = 0, n, size;
    441  1.1      cgd 
    442  1.1      cgd 	signal(SIGALRM, timer);
    443  1.1      cgd 	dp = w_init();
    444  1.1      cgd 	ap = (struct tftphdr *)ackbuf;
    445  1.1      cgd 	do {
    446  1.1      cgd 		timeout = 0;
    447  1.1      cgd 		ap->th_opcode = htons((u_short)ACK);
    448  1.1      cgd 		ap->th_block = htons((u_short)block);
    449  1.1      cgd 		block++;
    450  1.1      cgd 		(void) setjmp(timeoutbuf);
    451  1.1      cgd send_ack:
    452  1.1      cgd 		if (send(peer, ackbuf, 4, 0) != 4) {
    453  1.1      cgd 			syslog(LOG_ERR, "tftpd: write: %m\n");
    454  1.1      cgd 			goto abort;
    455  1.1      cgd 		}
    456  1.1      cgd 		write_behind(file, pf->f_convert);
    457  1.1      cgd 		for ( ; ; ) {
    458  1.1      cgd 			alarm(rexmtval);
    459  1.1      cgd 			n = recv(peer, dp, PKTSIZE, 0);
    460  1.1      cgd 			alarm(0);
    461  1.1      cgd 			if (n < 0) {            /* really? */
    462  1.1      cgd 				syslog(LOG_ERR, "tftpd: read: %m\n");
    463  1.1      cgd 				goto abort;
    464  1.1      cgd 			}
    465  1.1      cgd 			dp->th_opcode = ntohs((u_short)dp->th_opcode);
    466  1.1      cgd 			dp->th_block = ntohs((u_short)dp->th_block);
    467  1.1      cgd 			if (dp->th_opcode == ERROR)
    468  1.1      cgd 				goto abort;
    469  1.1      cgd 			if (dp->th_opcode == DATA) {
    470  1.1      cgd 				if (dp->th_block == block) {
    471  1.1      cgd 					break;   /* normal */
    472  1.1      cgd 				}
    473  1.1      cgd 				/* Re-synchronize with the other side */
    474  1.1      cgd 				(void) synchnet(peer);
    475  1.1      cgd 				if (dp->th_block == (block-1))
    476  1.1      cgd 					goto send_ack;          /* rexmit */
    477  1.1      cgd 			}
    478  1.1      cgd 		}
    479  1.1      cgd 		/*  size = write(file, dp->th_data, n - 4); */
    480  1.1      cgd 		size = writeit(file, &dp, n - 4, pf->f_convert);
    481  1.1      cgd 		if (size != (n-4)) {                    /* ahem */
    482  1.1      cgd 			if (size < 0) nak(errno + 100);
    483  1.1      cgd 			else nak(ENOSPACE);
    484  1.1      cgd 			goto abort;
    485  1.1      cgd 		}
    486  1.1      cgd 	} while (size == SEGSIZE);
    487  1.1      cgd 	write_behind(file, pf->f_convert);
    488  1.1      cgd 	(void) fclose(file);            /* close data file */
    489  1.1      cgd 
    490  1.1      cgd 	ap->th_opcode = htons((u_short)ACK);    /* send the "final" ack */
    491  1.1      cgd 	ap->th_block = htons((u_short)(block));
    492  1.1      cgd 	(void) send(peer, ackbuf, 4, 0);
    493  1.1      cgd 
    494  1.1      cgd 	signal(SIGALRM, justquit);      /* just quit on timeout */
    495  1.1      cgd 	alarm(rexmtval);
    496  1.1      cgd 	n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
    497  1.1      cgd 	alarm(0);
    498  1.1      cgd 	if (n >= 4 &&                   /* if read some data */
    499  1.1      cgd 	    dp->th_opcode == DATA &&    /* and got a data block */
    500  1.1      cgd 	    block == dp->th_block) {	/* then my last ack was lost */
    501  1.1      cgd 		(void) send(peer, ackbuf, 4, 0);     /* resend final ack */
    502  1.1      cgd 	}
    503  1.1      cgd abort:
    504  1.1      cgd 	return;
    505  1.1      cgd }
    506  1.1      cgd 
    507  1.1      cgd struct errmsg {
    508  1.1      cgd 	int	e_code;
    509  1.1      cgd 	char	*e_msg;
    510  1.1      cgd } errmsgs[] = {
    511  1.1      cgd 	{ EUNDEF,	"Undefined error code" },
    512  1.1      cgd 	{ ENOTFOUND,	"File not found" },
    513  1.1      cgd 	{ EACCESS,	"Access violation" },
    514  1.1      cgd 	{ ENOSPACE,	"Disk full or allocation exceeded" },
    515  1.1      cgd 	{ EBADOP,	"Illegal TFTP operation" },
    516  1.1      cgd 	{ EBADID,	"Unknown transfer ID" },
    517  1.1      cgd 	{ EEXISTS,	"File already exists" },
    518  1.1      cgd 	{ ENOUSER,	"No such user" },
    519  1.1      cgd 	{ -1,		0 }
    520  1.1      cgd };
    521  1.1      cgd 
    522  1.1      cgd /*
    523  1.1      cgd  * Send a nak packet (error message).
    524  1.1      cgd  * Error code passed in is one of the
    525  1.1      cgd  * standard TFTP codes, or a UNIX errno
    526  1.1      cgd  * offset by 100.
    527  1.1      cgd  */
    528  1.1      cgd nak(error)
    529  1.1      cgd 	int error;
    530  1.1      cgd {
    531  1.1      cgd 	register struct tftphdr *tp;
    532  1.1      cgd 	int length;
    533  1.1      cgd 	register struct errmsg *pe;
    534  1.1      cgd 
    535  1.1      cgd 	tp = (struct tftphdr *)buf;
    536  1.1      cgd 	tp->th_opcode = htons((u_short)ERROR);
    537  1.1      cgd 	tp->th_code = htons((u_short)error);
    538  1.1      cgd 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    539  1.1      cgd 		if (pe->e_code == error)
    540  1.1      cgd 			break;
    541  1.1      cgd 	if (pe->e_code < 0) {
    542  1.1      cgd 		pe->e_msg = strerror(error - 100);
    543  1.1      cgd 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
    544  1.1      cgd 	}
    545  1.1      cgd 	strcpy(tp->th_msg, pe->e_msg);
    546  1.1      cgd 	length = strlen(pe->e_msg);
    547  1.1      cgd 	tp->th_msg[length] = '\0';
    548  1.1      cgd 	length += 5;
    549  1.1      cgd 	if (send(peer, buf, length, 0) != length)
    550  1.1      cgd 		syslog(LOG_ERR, "nak: %m\n");
    551  1.1      cgd }
    552