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