Home | History | Annotate | Line # | Download | only in tftpd
tftpd.c revision 1.3
      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.3  mycroft static char rcsid[] = "$Id: tftpd.c,v 1.3 1993/08/01 18:28:53 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.2  mycroft struct	sockaddr_in s_in = { AF_INET };
     74  1.1      cgd int	peer;
     75  1.1      cgd int	rexmtval = TIMEOUT;
     76  1.1      cgd int	maxtimeout = 5*TIMEOUT;
     77  1.1      cgd 
     78  1.1      cgd #define	PKTSIZE	SEGSIZE+4
     79  1.1      cgd char	buf[PKTSIZE];
     80  1.1      cgd char	ackbuf[PKTSIZE];
     81  1.1      cgd struct	sockaddr_in from;
     82  1.1      cgd int	fromlen;
     83  1.1      cgd 
     84  1.1      cgd #define MAXARG	4
     85  1.1      cgd char	*dirs[MAXARG+1];
     86  1.1      cgd 
     87  1.1      cgd main(ac, av)
     88  1.1      cgd 	char **av;
     89  1.1      cgd {
     90  1.1      cgd 	register struct tftphdr *tp;
     91  1.1      cgd 	register int n = 0;
     92  1.1      cgd 	int on = 1;
     93  1.1      cgd 
     94  1.1      cgd 	ac--; av++;
     95  1.1      cgd 	while (ac-- > 0 && n < MAXARG)
     96  1.1      cgd 		dirs[n++] = *av++;
     97  1.1      cgd 	openlog("tftpd", LOG_PID, LOG_DAEMON);
     98  1.1      cgd 	if (ioctl(0, FIONBIO, &on) < 0) {
     99  1.1      cgd 		syslog(LOG_ERR, "ioctl(FIONBIO): %m\n");
    100  1.1      cgd 		exit(1);
    101  1.1      cgd 	}
    102  1.1      cgd 	fromlen = sizeof (from);
    103  1.1      cgd 	n = recvfrom(0, buf, sizeof (buf), 0,
    104  1.1      cgd 	    (struct sockaddr *)&from, &fromlen);
    105  1.1      cgd 	if (n < 0) {
    106  1.1      cgd 		syslog(LOG_ERR, "recvfrom: %m\n");
    107  1.1      cgd 		exit(1);
    108  1.1      cgd 	}
    109  1.1      cgd 	/*
    110  1.1      cgd 	 * Now that we have read the message out of the UDP
    111  1.1      cgd 	 * socket, we fork and exit.  Thus, inetd will go back
    112  1.1      cgd 	 * to listening to the tftp port, and the next request
    113  1.1      cgd 	 * to come in will start up a new instance of tftpd.
    114  1.1      cgd 	 *
    115  1.1      cgd 	 * We do this so that inetd can run tftpd in "wait" mode.
    116  1.1      cgd 	 * The problem with tftpd running in "nowait" mode is that
    117  1.1      cgd 	 * inetd may get one or more successful "selects" on the
    118  1.1      cgd 	 * tftp port before we do our receive, so more than one
    119  1.1      cgd 	 * instance of tftpd may be started up.  Worse, if tftpd
    120  1.1      cgd 	 * break before doing the above "recvfrom", inetd would
    121  1.1      cgd 	 * spawn endless instances, clogging the system.
    122  1.1      cgd 	 */
    123  1.1      cgd 	{
    124  1.1      cgd 		int pid;
    125  1.1      cgd 		int i, j;
    126  1.1      cgd 
    127  1.1      cgd 		for (i = 1; i < 20; i++) {
    128  1.1      cgd 		    pid = fork();
    129  1.1      cgd 		    if (pid < 0) {
    130  1.1      cgd 				sleep(i);
    131  1.1      cgd 				/*
    132  1.1      cgd 				 * flush out to most recently sent request.
    133  1.1      cgd 				 *
    134  1.1      cgd 				 * This may drop some request, but those
    135  1.1      cgd 				 * will be resent by the clients when
    136  1.1      cgd 				 * they timeout.  The positive effect of
    137  1.1      cgd 				 * this flush is to (try to) prevent more
    138  1.1      cgd 				 * than one tftpd being started up to service
    139  1.1      cgd 				 * a single request from a single client.
    140  1.1      cgd 				 */
    141  1.1      cgd 				j = sizeof from;
    142  1.1      cgd 				i = recvfrom(0, buf, sizeof (buf), 0,
    143  1.1      cgd 				    (struct sockaddr *)&from, &j);
    144  1.1      cgd 				if (i > 0) {
    145  1.1      cgd 					n = i;
    146  1.1      cgd 					fromlen = j;
    147  1.1      cgd 				}
    148  1.1      cgd 		    } else {
    149  1.1      cgd 				break;
    150  1.1      cgd 		    }
    151  1.1      cgd 		}
    152  1.1      cgd 		if (pid < 0) {
    153  1.1      cgd 			syslog(LOG_ERR, "fork: %m\n");
    154  1.1      cgd 			exit(1);
    155  1.1      cgd 		} else if (pid != 0) {
    156  1.1      cgd 			exit(0);
    157  1.1      cgd 		}
    158  1.1      cgd 	}
    159  1.1      cgd 	from.sin_family = AF_INET;
    160  1.1      cgd 	alarm(0);
    161  1.1      cgd 	close(0);
    162  1.1      cgd 	close(1);
    163  1.1      cgd 	peer = socket(AF_INET, SOCK_DGRAM, 0);
    164  1.1      cgd 	if (peer < 0) {
    165  1.1      cgd 		syslog(LOG_ERR, "socket: %m\n");
    166  1.1      cgd 		exit(1);
    167  1.1      cgd 	}
    168  1.2  mycroft 	if (bind(peer, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
    169  1.1      cgd 		syslog(LOG_ERR, "bind: %m\n");
    170  1.1      cgd 		exit(1);
    171  1.1      cgd 	}
    172  1.1      cgd 	if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
    173  1.1      cgd 		syslog(LOG_ERR, "connect: %m\n");
    174  1.1      cgd 		exit(1);
    175  1.1      cgd 	}
    176  1.1      cgd 	tp = (struct tftphdr *)buf;
    177  1.1      cgd 	tp->th_opcode = ntohs(tp->th_opcode);
    178  1.1      cgd 	if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
    179  1.1      cgd 		tftp(tp, n);
    180  1.1      cgd 	exit(1);
    181  1.1      cgd }
    182  1.1      cgd 
    183  1.1      cgd int	validate_access();
    184  1.1      cgd int	sendfile(), recvfile();
    185  1.1      cgd 
    186  1.1      cgd struct formats {
    187  1.1      cgd 	char	*f_mode;
    188  1.1      cgd 	int	(*f_validate)();
    189  1.1      cgd 	int	(*f_send)();
    190  1.1      cgd 	int	(*f_recv)();
    191  1.1      cgd 	int	f_convert;
    192  1.1      cgd } formats[] = {
    193  1.1      cgd 	{ "netascii",	validate_access,	sendfile,	recvfile, 1 },
    194  1.1      cgd 	{ "octet",	validate_access,	sendfile,	recvfile, 0 },
    195  1.1      cgd #ifdef notdef
    196  1.1      cgd 	{ "mail",	validate_user,		sendmail,	recvmail, 1 },
    197  1.1      cgd #endif
    198  1.1      cgd 	{ 0 }
    199  1.1      cgd };
    200  1.1      cgd 
    201  1.1      cgd /*
    202  1.1      cgd  * Handle initial connection protocol.
    203  1.1      cgd  */
    204  1.1      cgd tftp(tp, size)
    205  1.1      cgd 	struct tftphdr *tp;
    206  1.1      cgd 	int size;
    207  1.1      cgd {
    208  1.1      cgd 	register char *cp;
    209  1.1      cgd 	int first = 1, ecode;
    210  1.1      cgd 	register struct formats *pf;
    211  1.1      cgd 	char *filename, *mode;
    212  1.1      cgd 
    213  1.1      cgd 	filename = cp = tp->th_stuff;
    214  1.1      cgd again:
    215  1.1      cgd 	while (cp < buf + size) {
    216  1.1      cgd 		if (*cp == '\0')
    217  1.1      cgd 			break;
    218  1.1      cgd 		cp++;
    219  1.1      cgd 	}
    220  1.1      cgd 	if (*cp != '\0') {
    221  1.1      cgd 		nak(EBADOP);
    222  1.1      cgd 		exit(1);
    223  1.1      cgd 	}
    224  1.1      cgd 	if (first) {
    225  1.1      cgd 		mode = ++cp;
    226  1.1      cgd 		first = 0;
    227  1.1      cgd 		goto again;
    228  1.1      cgd 	}
    229  1.1      cgd 	for (cp = mode; *cp; cp++)
    230  1.1      cgd 		if (isupper(*cp))
    231  1.1      cgd 			*cp = tolower(*cp);
    232  1.1      cgd 	for (pf = formats; pf->f_mode; pf++)
    233  1.1      cgd 		if (strcmp(pf->f_mode, mode) == 0)
    234  1.1      cgd 			break;
    235  1.1      cgd 	if (pf->f_mode == 0) {
    236  1.1      cgd 		nak(EBADOP);
    237  1.1      cgd 		exit(1);
    238  1.1      cgd 	}
    239  1.1      cgd 	ecode = (*pf->f_validate)(filename, tp->th_opcode);
    240  1.1      cgd 	if (ecode) {
    241  1.1      cgd 		nak(ecode);
    242  1.1      cgd 		exit(1);
    243  1.1      cgd 	}
    244  1.1      cgd 	if (tp->th_opcode == WRQ)
    245  1.1      cgd 		(*pf->f_recv)(pf);
    246  1.1      cgd 	else
    247  1.1      cgd 		(*pf->f_send)(pf);
    248  1.1      cgd 	exit(0);
    249  1.1      cgd }
    250  1.1      cgd 
    251  1.1      cgd 
    252  1.1      cgd FILE *file;
    253  1.1      cgd 
    254  1.1      cgd /*
    255  1.1      cgd  * Validate file access.  Since we
    256  1.1      cgd  * have no uid or gid, for now require
    257  1.1      cgd  * file to exist and be publicly
    258  1.1      cgd  * readable/writable.
    259  1.1      cgd  * If we were invoked with arguments
    260  1.1      cgd  * from inetd then the file must also be
    261  1.1      cgd  * in one of the given directory prefixes.
    262  1.1      cgd  * Note also, full path name must be
    263  1.1      cgd  * given as we have no login directory.
    264  1.1      cgd  */
    265  1.1      cgd validate_access(filename, mode)
    266  1.1      cgd 	char *filename;
    267  1.1      cgd 	int mode;
    268  1.1      cgd {
    269  1.1      cgd 	struct stat stbuf;
    270  1.1      cgd 	int	fd;
    271  1.1      cgd 	char *cp, **dirp;
    272  1.1      cgd 
    273  1.1      cgd 	if (*filename != '/')
    274  1.1      cgd 		return (EACCESS);
    275  1.1      cgd 	/*
    276  1.1      cgd 	 * prevent tricksters from getting around the directory restrictions
    277  1.1      cgd 	 */
    278  1.1      cgd 	for (cp = filename + 1; *cp; cp++)
    279  1.1      cgd 		if(*cp == '.' && strncmp(cp-1, "/../", 4) == 0)
    280  1.1      cgd 			return(EACCESS);
    281  1.1      cgd 	for (dirp = dirs; *dirp; dirp++)
    282  1.1      cgd 		if (strncmp(filename, *dirp, strlen(*dirp)) == 0)
    283  1.1      cgd 			break;
    284  1.1      cgd 	if (*dirp==0 && dirp!=dirs)
    285  1.1      cgd 		return (EACCESS);
    286  1.1      cgd 	if (stat(filename, &stbuf) < 0)
    287  1.1      cgd 		return (errno == ENOENT ? ENOTFOUND : EACCESS);
    288  1.1      cgd 	if (mode == RRQ) {
    289  1.1      cgd 		if ((stbuf.st_mode&(S_IREAD >> 6)) == 0)
    290  1.1      cgd 			return (EACCESS);
    291  1.1      cgd 	} else {
    292  1.1      cgd 		if ((stbuf.st_mode&(S_IWRITE >> 6)) == 0)
    293  1.1      cgd 			return (EACCESS);
    294  1.1      cgd 	}
    295  1.1      cgd 	fd = open(filename, mode == RRQ ? 0 : 1);
    296  1.1      cgd 	if (fd < 0)
    297  1.1      cgd 		return (errno + 100);
    298  1.1      cgd 	file = fdopen(fd, (mode == RRQ)? "r":"w");
    299  1.1      cgd 	if (file == NULL) {
    300  1.1      cgd 		return errno+100;
    301  1.1      cgd 	}
    302  1.1      cgd 	return (0);
    303  1.1      cgd }
    304  1.1      cgd 
    305  1.1      cgd int	timeout;
    306  1.1      cgd jmp_buf	timeoutbuf;
    307  1.1      cgd 
    308  1.1      cgd void
    309  1.1      cgd timer()
    310  1.1      cgd {
    311  1.1      cgd 
    312  1.1      cgd 	timeout += rexmtval;
    313  1.1      cgd 	if (timeout >= maxtimeout)
    314  1.1      cgd 		exit(1);
    315  1.1      cgd 	longjmp(timeoutbuf, 1);
    316  1.1      cgd }
    317  1.1      cgd 
    318  1.1      cgd /*
    319  1.1      cgd  * Send the requested file.
    320  1.1      cgd  */
    321  1.1      cgd sendfile(pf)
    322  1.1      cgd 	struct formats *pf;
    323  1.1      cgd {
    324  1.1      cgd 	struct tftphdr *dp, *r_init();
    325  1.1      cgd 	register struct tftphdr *ap;    /* ack packet */
    326  1.1      cgd 	register int block = 1, size, n;
    327  1.1      cgd 
    328  1.1      cgd 	signal(SIGALRM, timer);
    329  1.1      cgd 	dp = r_init();
    330  1.1      cgd 	ap = (struct tftphdr *)ackbuf;
    331  1.1      cgd 	do {
    332  1.1      cgd 		size = readit(file, &dp, pf->f_convert);
    333  1.1      cgd 		if (size < 0) {
    334  1.1      cgd 			nak(errno + 100);
    335  1.1      cgd 			goto abort;
    336  1.1      cgd 		}
    337  1.1      cgd 		dp->th_opcode = htons((u_short)DATA);
    338  1.1      cgd 		dp->th_block = htons((u_short)block);
    339  1.1      cgd 		timeout = 0;
    340  1.1      cgd 		(void) setjmp(timeoutbuf);
    341  1.1      cgd 
    342  1.1      cgd send_data:
    343  1.1      cgd 		if (send(peer, dp, size + 4, 0) != size + 4) {
    344  1.1      cgd 			syslog(LOG_ERR, "tftpd: write: %m\n");
    345  1.1      cgd 			goto abort;
    346  1.1      cgd 		}
    347  1.1      cgd 		read_ahead(file, pf->f_convert);
    348  1.1      cgd 		for ( ; ; ) {
    349  1.1      cgd 			alarm(rexmtval);        /* read the ack */
    350  1.1      cgd 			n = recv(peer, ackbuf, sizeof (ackbuf), 0);
    351  1.1      cgd 			alarm(0);
    352  1.1      cgd 			if (n < 0) {
    353  1.1      cgd 				syslog(LOG_ERR, "tftpd: read: %m\n");
    354  1.1      cgd 				goto abort;
    355  1.1      cgd 			}
    356  1.1      cgd 			ap->th_opcode = ntohs((u_short)ap->th_opcode);
    357  1.1      cgd 			ap->th_block = ntohs((u_short)ap->th_block);
    358  1.1      cgd 
    359  1.1      cgd 			if (ap->th_opcode == ERROR)
    360  1.1      cgd 				goto abort;
    361  1.1      cgd 
    362  1.1      cgd 			if (ap->th_opcode == ACK) {
    363  1.1      cgd 				if (ap->th_block == block) {
    364  1.1      cgd 					break;
    365  1.1      cgd 				}
    366  1.1      cgd 				/* Re-synchronize with the other side */
    367  1.1      cgd 				(void) synchnet(peer);
    368  1.1      cgd 				if (ap->th_block == (block -1)) {
    369  1.1      cgd 					goto send_data;
    370  1.1      cgd 				}
    371  1.1      cgd 			}
    372  1.1      cgd 
    373  1.1      cgd 		}
    374  1.1      cgd 		block++;
    375  1.1      cgd 	} while (size == SEGSIZE);
    376  1.1      cgd abort:
    377  1.1      cgd 	(void) fclose(file);
    378  1.1      cgd }
    379  1.1      cgd 
    380  1.1      cgd void
    381  1.1      cgd justquit()
    382  1.1      cgd {
    383  1.1      cgd 	exit(0);
    384  1.1      cgd }
    385  1.1      cgd 
    386  1.1      cgd 
    387  1.1      cgd /*
    388  1.1      cgd  * Receive a file.
    389  1.1      cgd  */
    390  1.1      cgd recvfile(pf)
    391  1.1      cgd 	struct formats *pf;
    392  1.1      cgd {
    393  1.1      cgd 	struct tftphdr *dp, *w_init();
    394  1.1      cgd 	register struct tftphdr *ap;    /* ack buffer */
    395  1.1      cgd 	register int block = 0, n, size;
    396  1.1      cgd 
    397  1.1      cgd 	signal(SIGALRM, timer);
    398  1.1      cgd 	dp = w_init();
    399  1.1      cgd 	ap = (struct tftphdr *)ackbuf;
    400  1.1      cgd 	do {
    401  1.1      cgd 		timeout = 0;
    402  1.1      cgd 		ap->th_opcode = htons((u_short)ACK);
    403  1.1      cgd 		ap->th_block = htons((u_short)block);
    404  1.1      cgd 		block++;
    405  1.1      cgd 		(void) setjmp(timeoutbuf);
    406  1.1      cgd send_ack:
    407  1.1      cgd 		if (send(peer, ackbuf, 4, 0) != 4) {
    408  1.1      cgd 			syslog(LOG_ERR, "tftpd: write: %m\n");
    409  1.1      cgd 			goto abort;
    410  1.1      cgd 		}
    411  1.1      cgd 		write_behind(file, pf->f_convert);
    412  1.1      cgd 		for ( ; ; ) {
    413  1.1      cgd 			alarm(rexmtval);
    414  1.1      cgd 			n = recv(peer, dp, PKTSIZE, 0);
    415  1.1      cgd 			alarm(0);
    416  1.1      cgd 			if (n < 0) {            /* really? */
    417  1.1      cgd 				syslog(LOG_ERR, "tftpd: read: %m\n");
    418  1.1      cgd 				goto abort;
    419  1.1      cgd 			}
    420  1.1      cgd 			dp->th_opcode = ntohs((u_short)dp->th_opcode);
    421  1.1      cgd 			dp->th_block = ntohs((u_short)dp->th_block);
    422  1.1      cgd 			if (dp->th_opcode == ERROR)
    423  1.1      cgd 				goto abort;
    424  1.1      cgd 			if (dp->th_opcode == DATA) {
    425  1.1      cgd 				if (dp->th_block == block) {
    426  1.1      cgd 					break;   /* normal */
    427  1.1      cgd 				}
    428  1.1      cgd 				/* Re-synchronize with the other side */
    429  1.1      cgd 				(void) synchnet(peer);
    430  1.1      cgd 				if (dp->th_block == (block-1))
    431  1.1      cgd 					goto send_ack;          /* rexmit */
    432  1.1      cgd 			}
    433  1.1      cgd 		}
    434  1.1      cgd 		/*  size = write(file, dp->th_data, n - 4); */
    435  1.1      cgd 		size = writeit(file, &dp, n - 4, pf->f_convert);
    436  1.1      cgd 		if (size != (n-4)) {                    /* ahem */
    437  1.1      cgd 			if (size < 0) nak(errno + 100);
    438  1.1      cgd 			else nak(ENOSPACE);
    439  1.1      cgd 			goto abort;
    440  1.1      cgd 		}
    441  1.1      cgd 	} while (size == SEGSIZE);
    442  1.1      cgd 	write_behind(file, pf->f_convert);
    443  1.1      cgd 	(void) fclose(file);            /* close data file */
    444  1.1      cgd 
    445  1.1      cgd 	ap->th_opcode = htons((u_short)ACK);    /* send the "final" ack */
    446  1.1      cgd 	ap->th_block = htons((u_short)(block));
    447  1.1      cgd 	(void) send(peer, ackbuf, 4, 0);
    448  1.1      cgd 
    449  1.1      cgd 	signal(SIGALRM, justquit);      /* just quit on timeout */
    450  1.1      cgd 	alarm(rexmtval);
    451  1.1      cgd 	n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
    452  1.1      cgd 	alarm(0);
    453  1.1      cgd 	if (n >= 4 &&                   /* if read some data */
    454  1.1      cgd 	    dp->th_opcode == DATA &&    /* and got a data block */
    455  1.1      cgd 	    block == dp->th_block) {	/* then my last ack was lost */
    456  1.1      cgd 		(void) send(peer, ackbuf, 4, 0);     /* resend final ack */
    457  1.1      cgd 	}
    458  1.1      cgd abort:
    459  1.1      cgd 	return;
    460  1.1      cgd }
    461  1.1      cgd 
    462  1.1      cgd struct errmsg {
    463  1.1      cgd 	int	e_code;
    464  1.1      cgd 	char	*e_msg;
    465  1.1      cgd } errmsgs[] = {
    466  1.1      cgd 	{ EUNDEF,	"Undefined error code" },
    467  1.1      cgd 	{ ENOTFOUND,	"File not found" },
    468  1.1      cgd 	{ EACCESS,	"Access violation" },
    469  1.1      cgd 	{ ENOSPACE,	"Disk full or allocation exceeded" },
    470  1.1      cgd 	{ EBADOP,	"Illegal TFTP operation" },
    471  1.1      cgd 	{ EBADID,	"Unknown transfer ID" },
    472  1.1      cgd 	{ EEXISTS,	"File already exists" },
    473  1.1      cgd 	{ ENOUSER,	"No such user" },
    474  1.1      cgd 	{ -1,		0 }
    475  1.1      cgd };
    476  1.1      cgd 
    477  1.1      cgd /*
    478  1.1      cgd  * Send a nak packet (error message).
    479  1.1      cgd  * Error code passed in is one of the
    480  1.1      cgd  * standard TFTP codes, or a UNIX errno
    481  1.1      cgd  * offset by 100.
    482  1.1      cgd  */
    483  1.1      cgd nak(error)
    484  1.1      cgd 	int error;
    485  1.1      cgd {
    486  1.1      cgd 	register struct tftphdr *tp;
    487  1.1      cgd 	int length;
    488  1.1      cgd 	register struct errmsg *pe;
    489  1.1      cgd 
    490  1.1      cgd 	tp = (struct tftphdr *)buf;
    491  1.1      cgd 	tp->th_opcode = htons((u_short)ERROR);
    492  1.1      cgd 	tp->th_code = htons((u_short)error);
    493  1.1      cgd 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    494  1.1      cgd 		if (pe->e_code == error)
    495  1.1      cgd 			break;
    496  1.1      cgd 	if (pe->e_code < 0) {
    497  1.1      cgd 		pe->e_msg = strerror(error - 100);
    498  1.1      cgd 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
    499  1.1      cgd 	}
    500  1.1      cgd 	strcpy(tp->th_msg, pe->e_msg);
    501  1.1      cgd 	length = strlen(pe->e_msg);
    502  1.1      cgd 	tp->th_msg[length] = '\0';
    503  1.1      cgd 	length += 5;
    504  1.1      cgd 	if (send(peer, buf, length, 0) != length)
    505  1.1      cgd 		syslog(LOG_ERR, "nak: %m\n");
    506  1.1      cgd }
    507