Home | History | Annotate | Line # | Download | only in tftpd
tftpd.c revision 1.18
      1  1.18    itojun /*	$NetBSD: tftpd.c,v 1.18 1999/07/12 20:17:09 itojun 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.18    itojun __RCSID("$NetBSD: tftpd.c,v 1.18 1999/07/12 20:17:09 itojun 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.14     lukem #include <grp.h>
     70   1.1       cgd #include <netdb.h>
     71  1.14     lukem #include <pwd.h>
     72   1.1       cgd #include <setjmp.h>
     73   1.9       mrg #include <signal.h>
     74   1.1       cgd #include <stdio.h>
     75   1.9       mrg #include <stdlib.h>
     76   1.1       cgd #include <string.h>
     77   1.9       mrg #include <syslog.h>
     78   1.9       mrg #include <unistd.h>
     79   1.9       mrg 
     80   1.9       mrg #include "tftpsubs.h"
     81   1.1       cgd 
     82  1.14     lukem #define	DEFAULTUSER	"nobody"
     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.18    itojun struct	sockaddr_storage 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.13   mycroft static const char *errtomsg __P((int));
    118   1.9       mrg static void nak __P((int));
    119  1.18    itojun static char *verifyhost __P((struct sockaddr *));
    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 	{ 0 }
    138   1.9       mrg };
    139   1.4   mycroft 
    140   1.5       cgd static void
    141   1.5       cgd usage()
    142   1.5       cgd {
    143  1.14     lukem 	syslog(LOG_ERR,
    144  1.14     lukem     "Usage: %s [-ln] [-u user] [-g group] [-s directory] [directory ...]",
    145  1.14     lukem 		    __progname);
    146   1.5       cgd 	exit(1);
    147   1.5       cgd }
    148   1.5       cgd 
    149   1.9       mrg int
    150   1.5       cgd main(argc, argv)
    151   1.5       cgd 	int    argc;
    152   1.5       cgd 	char **argv;
    153   1.1       cgd {
    154  1.14     lukem 	struct passwd *pwent;
    155  1.14     lukem 	struct group *grent;
    156  1.14     lukem 	struct tftphdr *tp;
    157  1.14     lukem 	int n = 0;
    158   1.9       mrg 	int ch, on;
    159   1.5       cgd 	int fd = 0;
    160  1.18    itojun 	struct sockaddr_storage me;
    161  1.15  explorer 	int len;
    162  1.14     lukem 	char *tgtuser, *tgtgroup, *ep;
    163  1.14     lukem 	uid_t curuid, tgtuid;
    164  1.14     lukem 	gid_t curgid, tgtgid;
    165  1.14     lukem 	long nid;
    166   1.1       cgd 
    167  1.11     lukem 	openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
    168  1.14     lukem 	tgtuser = DEFAULTUSER;
    169  1.14     lukem 	tgtgroup = NULL;
    170  1.14     lukem 	curuid = getuid();
    171  1.14     lukem 	curgid = getgid();
    172   1.5       cgd 
    173  1.14     lukem 	while ((ch = getopt(argc, argv, "g:lns:u:")) != -1)
    174   1.9       mrg 		switch (ch) {
    175  1.14     lukem 
    176  1.14     lukem 		case 'g':
    177  1.14     lukem 			tgtgroup = optarg;
    178  1.14     lukem 			break;
    179  1.14     lukem 
    180   1.9       mrg 		case 'l':
    181   1.9       mrg 			logging = 1;
    182   1.9       mrg 			break;
    183   1.9       mrg 
    184   1.9       mrg 		case 'n':
    185   1.9       mrg 			suppress_naks = 1;
    186   1.9       mrg 			break;
    187   1.9       mrg 
    188   1.5       cgd 		case 's':
    189   1.5       cgd 			secure = 1;
    190   1.9       mrg 			securedir = optarg;
    191   1.5       cgd 			break;
    192   1.5       cgd 
    193  1.14     lukem 		case 'u':
    194  1.14     lukem 			tgtuser = optarg;
    195  1.14     lukem 			break;
    196  1.14     lukem 
    197   1.5       cgd 		default:
    198   1.5       cgd 			usage();
    199   1.5       cgd 			break;
    200   1.5       cgd 		}
    201   1.5       cgd 
    202   1.9       mrg 	if (optind < argc) {
    203   1.9       mrg 		struct dirlist *dirp;
    204   1.9       mrg 
    205   1.9       mrg 		/* Get list of directory prefixes. Skip relative pathnames. */
    206   1.9       mrg 		for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
    207   1.9       mrg 		     optind++) {
    208   1.9       mrg 			if (argv[optind][0] == '/') {
    209   1.9       mrg 				dirp->name = argv[optind];
    210   1.9       mrg 				dirp->len  = strlen(dirp->name);
    211   1.9       mrg 				dirp++;
    212   1.9       mrg 			}
    213   1.9       mrg 		}
    214   1.9       mrg 	}
    215   1.9       mrg 
    216  1.14     lukem 	if (*tgtuser == '\0' || (tgtgroup != NULL && *tgtgroup == '\0'))
    217  1.14     lukem 		usage();
    218  1.14     lukem 
    219  1.14     lukem 	nid = (strtol(tgtuser, &ep, 10));
    220  1.14     lukem 	if (*ep == '\0') {
    221  1.14     lukem 		if (nid > UID_MAX) {
    222  1.14     lukem 			syslog(LOG_ERR, "uid %ld is too large", nid);
    223  1.14     lukem 			exit(1);
    224  1.14     lukem 		}
    225  1.14     lukem 		pwent = getpwuid((uid_t)nid);
    226  1.14     lukem 	} else
    227  1.14     lukem 		pwent = getpwnam(tgtuser);
    228  1.14     lukem 	if (pwent == NULL) {
    229  1.14     lukem 		syslog(LOG_ERR, "unknown user `%s'", tgtuser);
    230  1.14     lukem 		exit(1);
    231  1.14     lukem 	}
    232  1.14     lukem 	tgtuid = pwent->pw_uid;
    233  1.14     lukem 	tgtgid = pwent->pw_gid;
    234  1.14     lukem 
    235  1.14     lukem 	if (tgtgroup != NULL) {
    236  1.14     lukem 		nid = (strtol(tgtgroup, &ep, 10));
    237  1.14     lukem 		if (*ep == '\0') {
    238  1.14     lukem 			if (nid > GID_MAX) {
    239  1.14     lukem 				syslog(LOG_ERR, "gid %ld is too large", nid);
    240  1.14     lukem 				exit(1);
    241  1.14     lukem 			}
    242  1.14     lukem 			grent = getgrgid((gid_t)nid);
    243  1.14     lukem 		} else
    244  1.14     lukem 			grent = getgrnam(tgtgroup);
    245  1.14     lukem 		if (grent != NULL)
    246  1.14     lukem 			tgtgid = grent->gr_gid;
    247  1.14     lukem 		else {
    248  1.14     lukem 			syslog(LOG_ERR, "unknown group `%s'", tgtgroup);
    249  1.14     lukem 			exit(1);
    250  1.14     lukem 		}
    251  1.14     lukem 	}
    252  1.14     lukem 
    253   1.9       mrg 	if (secure) {
    254   1.9       mrg 		if (chdir(securedir) < 0) {
    255   1.9       mrg 			syslog(LOG_ERR, "chdir %s: %m", securedir);
    256   1.9       mrg 			exit(1);
    257   1.4   mycroft 		}
    258   1.9       mrg 		if (chroot(".")) {
    259  1.14     lukem 			syslog(LOG_ERR, "chroot: %m");
    260   1.4   mycroft 			exit(1);
    261   1.4   mycroft 		}
    262   1.4   mycroft 	}
    263   1.5       cgd 
    264  1.14     lukem 	syslog(LOG_DEBUG, "running as user `%s' (%d), group `%s' (%d)",
    265  1.14     lukem 	    tgtuser, tgtuid, tgtgroup ? tgtgroup : "(unspecified)" , tgtgid);
    266  1.14     lukem 	if (curgid != tgtgid) {
    267  1.14     lukem 		if (setgid(tgtgid)) {
    268  1.14     lukem 			syslog(LOG_ERR, "setgid to %d: %m", (int)tgtgid);
    269  1.14     lukem 			exit(1);
    270  1.14     lukem 		}
    271  1.14     lukem 		if (setgroups(0, NULL)) {
    272  1.14     lukem 			syslog(LOG_ERR, "setgroups: %m");
    273  1.14     lukem 			exit(1);
    274  1.14     lukem 		}
    275   1.8       mrg 	}
    276   1.8       mrg 
    277  1.14     lukem 	if (curuid != tgtuid) {
    278  1.14     lukem 		if (setuid(tgtuid)) {
    279  1.14     lukem 			syslog(LOG_ERR, "setuid to %d: %m", (int)tgtuid);
    280  1.14     lukem 			exit(1);
    281  1.14     lukem 		}
    282   1.4   mycroft 	}
    283   1.5       cgd 
    284   1.9       mrg 	on = 1;
    285   1.5       cgd 	if (ioctl(fd, FIONBIO, &on) < 0) {
    286  1.14     lukem 		syslog(LOG_ERR, "ioctl(FIONBIO): %m");
    287   1.1       cgd 		exit(1);
    288   1.1       cgd 	}
    289   1.1       cgd 	fromlen = sizeof (from);
    290   1.5       cgd 	n = recvfrom(fd, buf, sizeof (buf), 0,
    291   1.1       cgd 	    (struct sockaddr *)&from, &fromlen);
    292   1.1       cgd 	if (n < 0) {
    293  1.14     lukem 		syslog(LOG_ERR, "recvfrom: %m");
    294   1.1       cgd 		exit(1);
    295   1.1       cgd 	}
    296   1.1       cgd 	/*
    297   1.1       cgd 	 * Now that we have read the message out of the UDP
    298   1.1       cgd 	 * socket, we fork and exit.  Thus, inetd will go back
    299   1.1       cgd 	 * to listening to the tftp port, and the next request
    300   1.1       cgd 	 * to come in will start up a new instance of tftpd.
    301   1.1       cgd 	 *
    302   1.1       cgd 	 * We do this so that inetd can run tftpd in "wait" mode.
    303   1.1       cgd 	 * The problem with tftpd running in "nowait" mode is that
    304   1.1       cgd 	 * inetd may get one or more successful "selects" on the
    305   1.1       cgd 	 * tftp port before we do our receive, so more than one
    306   1.1       cgd 	 * instance of tftpd may be started up.  Worse, if tftpd
    307   1.1       cgd 	 * break before doing the above "recvfrom", inetd would
    308   1.1       cgd 	 * spawn endless instances, clogging the system.
    309   1.1       cgd 	 */
    310   1.1       cgd 	{
    311   1.1       cgd 		int pid;
    312   1.1       cgd 		int i, j;
    313   1.1       cgd 
    314   1.1       cgd 		for (i = 1; i < 20; i++) {
    315   1.1       cgd 		    pid = fork();
    316   1.1       cgd 		    if (pid < 0) {
    317   1.1       cgd 				sleep(i);
    318   1.1       cgd 				/*
    319   1.1       cgd 				 * flush out to most recently sent request.
    320   1.1       cgd 				 *
    321   1.1       cgd 				 * This may drop some request, but those
    322   1.1       cgd 				 * will be resent by the clients when
    323   1.1       cgd 				 * they timeout.  The positive effect of
    324   1.1       cgd 				 * this flush is to (try to) prevent more
    325   1.1       cgd 				 * than one tftpd being started up to service
    326   1.1       cgd 				 * a single request from a single client.
    327   1.1       cgd 				 */
    328   1.1       cgd 				j = sizeof from;
    329   1.5       cgd 				i = recvfrom(fd, buf, sizeof (buf), 0,
    330   1.1       cgd 				    (struct sockaddr *)&from, &j);
    331   1.1       cgd 				if (i > 0) {
    332   1.1       cgd 					n = i;
    333   1.1       cgd 					fromlen = j;
    334   1.1       cgd 				}
    335   1.1       cgd 		    } else {
    336   1.1       cgd 				break;
    337   1.1       cgd 		    }
    338   1.1       cgd 		}
    339   1.1       cgd 		if (pid < 0) {
    340  1.14     lukem 			syslog(LOG_ERR, "fork: %m");
    341   1.1       cgd 			exit(1);
    342   1.1       cgd 		} else if (pid != 0) {
    343   1.1       cgd 			exit(0);
    344   1.1       cgd 		}
    345   1.1       cgd 	}
    346  1.15  explorer 
    347  1.15  explorer 	/*
    348  1.15  explorer 	 * remember what address this was sent to, so we can respond on the
    349  1.15  explorer 	 * same interface
    350  1.15  explorer 	 */
    351  1.18    itojun 	len = sizeof(me);
    352  1.18    itojun 	if (getsockname(fd, (struct sockaddr *)&me, &len) == 0) {
    353  1.18    itojun 		switch (me.ss_family) {
    354  1.18    itojun 		case AF_INET:
    355  1.18    itojun 			((struct sockaddr_in *)&me)->sin_port = 0;
    356  1.18    itojun 			break;
    357  1.18    itojun 		case AF_INET6:
    358  1.18    itojun 			((struct sockaddr_in6 *)&me)->sin6_port = 0;
    359  1.18    itojun 			break;
    360  1.18    itojun 		default:
    361  1.18    itojun 			/* unsupported */
    362  1.18    itojun 			break;
    363  1.18    itojun 		}
    364  1.18    itojun 	} else {
    365  1.18    itojun 		memset(&me, 0, sizeof(me));
    366  1.18    itojun 		me.ss_family = from.ss_family;
    367  1.18    itojun 		me.ss_len = from.ss_len;
    368  1.15  explorer 	}
    369  1.15  explorer 
    370   1.1       cgd 	alarm(0);
    371   1.5       cgd 	close(fd);
    372   1.1       cgd 	close(1);
    373  1.18    itojun 	peer = socket(from.ss_family, SOCK_DGRAM, 0);
    374   1.1       cgd 	if (peer < 0) {
    375  1.14     lukem 		syslog(LOG_ERR, "socket: %m");
    376   1.1       cgd 		exit(1);
    377   1.1       cgd 	}
    378  1.18    itojun 	if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) {
    379  1.14     lukem 		syslog(LOG_ERR, "bind: %m");
    380   1.1       cgd 		exit(1);
    381   1.1       cgd 	}
    382  1.18    itojun 	if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) {
    383  1.14     lukem 		syslog(LOG_ERR, "connect: %m");
    384   1.1       cgd 		exit(1);
    385   1.1       cgd 	}
    386   1.1       cgd 	tp = (struct tftphdr *)buf;
    387   1.1       cgd 	tp->th_opcode = ntohs(tp->th_opcode);
    388   1.1       cgd 	if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
    389   1.1       cgd 		tftp(tp, n);
    390   1.1       cgd 	exit(1);
    391   1.1       cgd }
    392   1.1       cgd 
    393   1.1       cgd /*
    394   1.1       cgd  * Handle initial connection protocol.
    395   1.1       cgd  */
    396   1.9       mrg static void
    397   1.1       cgd tftp(tp, size)
    398   1.1       cgd 	struct tftphdr *tp;
    399   1.1       cgd 	int size;
    400   1.1       cgd {
    401  1.14     lukem 	char *cp;
    402   1.1       cgd 	int first = 1, ecode;
    403  1.14     lukem 	struct formats *pf;
    404   1.9       mrg 	char *filename, *mode = NULL; /* XXX gcc */
    405   1.1       cgd 
    406   1.1       cgd 	filename = cp = tp->th_stuff;
    407   1.1       cgd again:
    408   1.1       cgd 	while (cp < buf + size) {
    409   1.1       cgd 		if (*cp == '\0')
    410   1.1       cgd 			break;
    411   1.1       cgd 		cp++;
    412   1.1       cgd 	}
    413   1.1       cgd 	if (*cp != '\0') {
    414   1.1       cgd 		nak(EBADOP);
    415   1.1       cgd 		exit(1);
    416   1.1       cgd 	}
    417   1.1       cgd 	if (first) {
    418   1.1       cgd 		mode = ++cp;
    419   1.1       cgd 		first = 0;
    420   1.1       cgd 		goto again;
    421   1.1       cgd 	}
    422   1.1       cgd 	for (cp = mode; *cp; cp++)
    423   1.1       cgd 		if (isupper(*cp))
    424   1.1       cgd 			*cp = tolower(*cp);
    425   1.1       cgd 	for (pf = formats; pf->f_mode; pf++)
    426   1.1       cgd 		if (strcmp(pf->f_mode, mode) == 0)
    427   1.1       cgd 			break;
    428   1.1       cgd 	if (pf->f_mode == 0) {
    429   1.1       cgd 		nak(EBADOP);
    430   1.1       cgd 		exit(1);
    431   1.1       cgd 	}
    432   1.9       mrg 	ecode = (*pf->f_validate)(&filename, tp->th_opcode);
    433   1.9       mrg 	if (logging) {
    434   1.9       mrg 		syslog(LOG_INFO, "%s: %s request for %s: %s",
    435  1.18    itojun 			verifyhost((struct sockaddr *)&from),
    436   1.9       mrg 			tp->th_opcode == WRQ ? "write" : "read",
    437   1.9       mrg 			filename, errtomsg(ecode));
    438   1.9       mrg 	}
    439   1.1       cgd 	if (ecode) {
    440   1.9       mrg 		/*
    441   1.9       mrg 		 * Avoid storms of naks to a RRQ broadcast for a relative
    442   1.9       mrg 		 * bootfile pathname from a diskless Sun.
    443   1.9       mrg 		 */
    444   1.9       mrg 		if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
    445   1.9       mrg 			exit(0);
    446   1.1       cgd 		nak(ecode);
    447   1.1       cgd 		exit(1);
    448   1.1       cgd 	}
    449   1.1       cgd 	if (tp->th_opcode == WRQ)
    450   1.1       cgd 		(*pf->f_recv)(pf);
    451   1.1       cgd 	else
    452   1.1       cgd 		(*pf->f_send)(pf);
    453   1.1       cgd 	exit(0);
    454   1.1       cgd }
    455   1.1       cgd 
    456   1.1       cgd 
    457   1.1       cgd FILE *file;
    458   1.1       cgd 
    459   1.1       cgd /*
    460   1.1       cgd  * Validate file access.  Since we
    461   1.1       cgd  * have no uid or gid, for now require
    462   1.1       cgd  * file to exist and be publicly
    463   1.1       cgd  * readable/writable.
    464   1.1       cgd  * If we were invoked with arguments
    465   1.1       cgd  * from inetd then the file must also be
    466   1.1       cgd  * in one of the given directory prefixes.
    467   1.1       cgd  */
    468   1.9       mrg int
    469   1.9       mrg validate_access(filep, mode)
    470   1.9       mrg 	char **filep;
    471   1.1       cgd 	int mode;
    472   1.1       cgd {
    473   1.1       cgd 	struct stat stbuf;
    474   1.1       cgd 	int	fd;
    475   1.9       mrg 	struct dirlist *dirp;
    476   1.9       mrg 	static char pathname[MAXPATHLEN];
    477   1.9       mrg 	char *filename = *filep;
    478   1.1       cgd 
    479   1.9       mrg 	/*
    480   1.9       mrg 	 * Prevent tricksters from getting around the directory restrictions
    481   1.9       mrg 	 */
    482   1.9       mrg 	if (strstr(filename, "/../"))
    483   1.9       mrg 		return (EACCESS);
    484   1.9       mrg 
    485   1.9       mrg 	if (*filename == '/') {
    486   1.4   mycroft 		/*
    487   1.9       mrg 		 * Allow the request if it's in one of the approved locations.
    488   1.9       mrg 		 * Special case: check the null prefix ("/") by looking
    489   1.9       mrg 		 * for length = 1 and relying on the arg. processing that
    490   1.9       mrg 		 * it's a /.
    491   1.4   mycroft 		 */
    492   1.9       mrg 		for (dirp = dirs; dirp->name != NULL; dirp++) {
    493   1.9       mrg 			if (dirp->len == 1 ||
    494   1.9       mrg 			    (!strncmp(filename, dirp->name, dirp->len) &&
    495   1.9       mrg 			     filename[dirp->len] == '/'))
    496   1.9       mrg 				    break;
    497   1.9       mrg 		}
    498   1.9       mrg 		/* If directory list is empty, allow access to any file */
    499   1.9       mrg 		if (dirp->name == NULL && dirp != dirs)
    500   1.1       cgd 			return (EACCESS);
    501   1.9       mrg 		if (stat(filename, &stbuf) < 0)
    502   1.9       mrg 			return (errno == ENOENT ? ENOTFOUND : EACCESS);
    503  1.10   mycroft 		if (!S_ISREG(stbuf.st_mode))
    504   1.9       mrg 			return (ENOTFOUND);
    505   1.9       mrg 		if (mode == RRQ) {
    506   1.9       mrg 			if ((stbuf.st_mode & S_IROTH) == 0)
    507   1.9       mrg 				return (EACCESS);
    508   1.9       mrg 		} else {
    509   1.9       mrg 			if ((stbuf.st_mode & S_IWOTH) == 0)
    510   1.9       mrg 				return (EACCESS);
    511   1.9       mrg 		}
    512   1.1       cgd 	} else {
    513   1.9       mrg 		/*
    514   1.9       mrg 		 * Relative file name: search the approved locations for it.
    515   1.9       mrg 		 */
    516   1.9       mrg 
    517  1.16     aidan 		if (!strncmp(filename, "../", 3))
    518   1.1       cgd 			return (EACCESS);
    519   1.9       mrg 
    520   1.9       mrg 		/*
    521  1.16     aidan 		 * Find the first file that exists in any of the directories,
    522  1.16     aidan 		 * check access on it.
    523   1.9       mrg 		 */
    524   1.9       mrg 		if (dirs[0].name != NULL) {
    525   1.9       mrg 			for (dirp = dirs; dirp->name != NULL; dirp++) {
    526   1.9       mrg 				snprintf(pathname, sizeof pathname, "%s/%s",
    527   1.9       mrg 				    dirp->name, filename);
    528   1.9       mrg 				if (stat(pathname, &stbuf) == 0 &&
    529   1.9       mrg 				    (stbuf.st_mode & S_IFMT) == S_IFREG) {
    530  1.16     aidan 					break;
    531   1.9       mrg 				}
    532   1.9       mrg 			}
    533   1.9       mrg 			if (dirp->name == NULL)
    534  1.16     aidan 				return (ENOTFOUND);
    535  1.16     aidan 			if (mode == RRQ && !(stbuf.st_mode & S_IROTH))
    536  1.16     aidan 				return (EACCESS);
    537  1.16     aidan 			if (mode == WRQ && !(stbuf.st_mode & S_IWOTH))
    538  1.16     aidan 				return (EACCESS);
    539   1.9       mrg 			*filep = filename = pathname;
    540  1.16     aidan 		} else {
    541  1.16     aidan 			/*
    542  1.16     aidan 			 * If there's no directory list, take our cue from the
    543  1.16     aidan 			 * absolute file request check above (*filename == '/'),
    544  1.16     aidan 			 * and allow access to anything.
    545  1.16     aidan 			 */
    546  1.16     aidan 			if (stat(filename, &stbuf) < 0)
    547  1.16     aidan 				return (errno == ENOENT ? ENOTFOUND : EACCESS);
    548  1.16     aidan 			if (!S_ISREG(stbuf.st_mode))
    549  1.16     aidan 				return (ENOTFOUND);
    550  1.16     aidan 			if (mode == RRQ) {
    551  1.16     aidan 				if ((stbuf.st_mode & S_IROTH) == 0)
    552  1.16     aidan 					return (EACCESS);
    553  1.16     aidan 			} else {
    554  1.16     aidan 				if ((stbuf.st_mode & S_IWOTH) == 0)
    555  1.16     aidan 					return (EACCESS);
    556  1.16     aidan 			}
    557   1.9       mrg 			*filep = filename;
    558  1.16     aidan 		}
    559   1.1       cgd 	}
    560  1.17    carrel 	fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY | O_TRUNC);
    561   1.1       cgd 	if (fd < 0)
    562   1.1       cgd 		return (errno + 100);
    563   1.1       cgd 	file = fdopen(fd, (mode == RRQ)? "r":"w");
    564   1.1       cgd 	if (file == NULL) {
    565  1.16     aidan 		close(fd);
    566   1.1       cgd 		return errno+100;
    567   1.1       cgd 	}
    568   1.1       cgd 	return (0);
    569   1.1       cgd }
    570   1.1       cgd 
    571   1.1       cgd int	timeout;
    572   1.1       cgd jmp_buf	timeoutbuf;
    573   1.1       cgd 
    574   1.1       cgd void
    575   1.9       mrg timer(dummy)
    576   1.9       mrg 	int dummy;
    577   1.1       cgd {
    578   1.1       cgd 
    579   1.1       cgd 	timeout += rexmtval;
    580   1.1       cgd 	if (timeout >= maxtimeout)
    581   1.1       cgd 		exit(1);
    582   1.1       cgd 	longjmp(timeoutbuf, 1);
    583   1.1       cgd }
    584   1.1       cgd 
    585   1.1       cgd /*
    586   1.1       cgd  * Send the requested file.
    587   1.1       cgd  */
    588   1.9       mrg void
    589   1.1       cgd sendfile(pf)
    590   1.1       cgd 	struct formats *pf;
    591   1.1       cgd {
    592   1.9       mrg 	struct tftphdr *dp;
    593  1.14     lukem 	struct tftphdr *ap;    /* ack packet */
    594  1.14     lukem 	int size, n;
    595   1.9       mrg 	volatile int block;
    596   1.1       cgd 
    597   1.1       cgd 	signal(SIGALRM, timer);
    598   1.1       cgd 	dp = r_init();
    599   1.1       cgd 	ap = (struct tftphdr *)ackbuf;
    600   1.9       mrg 	block = 1;
    601   1.1       cgd 	do {
    602   1.1       cgd 		size = readit(file, &dp, pf->f_convert);
    603   1.1       cgd 		if (size < 0) {
    604   1.1       cgd 			nak(errno + 100);
    605   1.1       cgd 			goto abort;
    606   1.1       cgd 		}
    607   1.1       cgd 		dp->th_opcode = htons((u_short)DATA);
    608   1.1       cgd 		dp->th_block = htons((u_short)block);
    609   1.1       cgd 		timeout = 0;
    610   1.9       mrg 		(void)setjmp(timeoutbuf);
    611   1.1       cgd 
    612   1.1       cgd send_data:
    613   1.1       cgd 		if (send(peer, dp, size + 4, 0) != size + 4) {
    614  1.14     lukem 			syslog(LOG_ERR, "tftpd: write: %m");
    615   1.1       cgd 			goto abort;
    616   1.1       cgd 		}
    617   1.1       cgd 		read_ahead(file, pf->f_convert);
    618   1.1       cgd 		for ( ; ; ) {
    619   1.1       cgd 			alarm(rexmtval);        /* read the ack */
    620   1.1       cgd 			n = recv(peer, ackbuf, sizeof (ackbuf), 0);
    621   1.1       cgd 			alarm(0);
    622   1.1       cgd 			if (n < 0) {
    623  1.14     lukem 				syslog(LOG_ERR, "tftpd: read: %m");
    624   1.1       cgd 				goto abort;
    625   1.1       cgd 			}
    626   1.1       cgd 			ap->th_opcode = ntohs((u_short)ap->th_opcode);
    627   1.1       cgd 			ap->th_block = ntohs((u_short)ap->th_block);
    628   1.1       cgd 
    629   1.1       cgd 			if (ap->th_opcode == ERROR)
    630   1.1       cgd 				goto abort;
    631   1.9       mrg 
    632   1.1       cgd 			if (ap->th_opcode == ACK) {
    633   1.9       mrg 				if (ap->th_block == block)
    634   1.1       cgd 					break;
    635   1.1       cgd 				/* Re-synchronize with the other side */
    636   1.1       cgd 				(void) synchnet(peer);
    637   1.9       mrg 				if (ap->th_block == (block -1))
    638   1.1       cgd 					goto send_data;
    639   1.1       cgd 			}
    640   1.1       cgd 
    641   1.1       cgd 		}
    642   1.1       cgd 		block++;
    643   1.1       cgd 	} while (size == SEGSIZE);
    644   1.1       cgd abort:
    645   1.1       cgd 	(void) fclose(file);
    646   1.1       cgd }
    647   1.1       cgd 
    648   1.1       cgd void
    649   1.9       mrg justquit(dummy)
    650   1.9       mrg 	int dummy;
    651   1.1       cgd {
    652   1.1       cgd 	exit(0);
    653   1.1       cgd }
    654   1.1       cgd 
    655   1.1       cgd /*
    656   1.1       cgd  * Receive a file.
    657   1.1       cgd  */
    658   1.9       mrg void
    659   1.1       cgd recvfile(pf)
    660   1.1       cgd 	struct formats *pf;
    661   1.1       cgd {
    662   1.9       mrg 	struct tftphdr *dp;
    663  1.14     lukem 	struct tftphdr *ap;    /* ack buffer */
    664  1.14     lukem 	int n, size;
    665   1.9       mrg 	volatile int block;
    666   1.1       cgd 
    667   1.1       cgd 	signal(SIGALRM, timer);
    668   1.1       cgd 	dp = w_init();
    669   1.1       cgd 	ap = (struct tftphdr *)ackbuf;
    670   1.9       mrg 	block = 0;
    671   1.1       cgd 	do {
    672   1.1       cgd 		timeout = 0;
    673   1.1       cgd 		ap->th_opcode = htons((u_short)ACK);
    674   1.1       cgd 		ap->th_block = htons((u_short)block);
    675   1.1       cgd 		block++;
    676   1.1       cgd 		(void) setjmp(timeoutbuf);
    677   1.1       cgd send_ack:
    678   1.1       cgd 		if (send(peer, ackbuf, 4, 0) != 4) {
    679  1.14     lukem 			syslog(LOG_ERR, "tftpd: write: %m");
    680   1.1       cgd 			goto abort;
    681   1.1       cgd 		}
    682   1.1       cgd 		write_behind(file, pf->f_convert);
    683   1.1       cgd 		for ( ; ; ) {
    684   1.1       cgd 			alarm(rexmtval);
    685   1.1       cgd 			n = recv(peer, dp, PKTSIZE, 0);
    686   1.1       cgd 			alarm(0);
    687   1.1       cgd 			if (n < 0) {            /* really? */
    688  1.14     lukem 				syslog(LOG_ERR, "tftpd: read: %m");
    689   1.1       cgd 				goto abort;
    690   1.1       cgd 			}
    691   1.1       cgd 			dp->th_opcode = ntohs((u_short)dp->th_opcode);
    692   1.1       cgd 			dp->th_block = ntohs((u_short)dp->th_block);
    693   1.1       cgd 			if (dp->th_opcode == ERROR)
    694   1.1       cgd 				goto abort;
    695   1.1       cgd 			if (dp->th_opcode == DATA) {
    696   1.1       cgd 				if (dp->th_block == block) {
    697   1.1       cgd 					break;   /* normal */
    698   1.1       cgd 				}
    699   1.1       cgd 				/* Re-synchronize with the other side */
    700   1.1       cgd 				(void) synchnet(peer);
    701   1.1       cgd 				if (dp->th_block == (block-1))
    702   1.1       cgd 					goto send_ack;          /* rexmit */
    703   1.1       cgd 			}
    704   1.1       cgd 		}
    705   1.1       cgd 		/*  size = write(file, dp->th_data, n - 4); */
    706   1.1       cgd 		size = writeit(file, &dp, n - 4, pf->f_convert);
    707   1.1       cgd 		if (size != (n-4)) {                    /* ahem */
    708   1.1       cgd 			if (size < 0) nak(errno + 100);
    709   1.1       cgd 			else nak(ENOSPACE);
    710   1.1       cgd 			goto abort;
    711   1.1       cgd 		}
    712   1.1       cgd 	} while (size == SEGSIZE);
    713   1.1       cgd 	write_behind(file, pf->f_convert);
    714   1.1       cgd 	(void) fclose(file);            /* close data file */
    715   1.1       cgd 
    716   1.1       cgd 	ap->th_opcode = htons((u_short)ACK);    /* send the "final" ack */
    717   1.1       cgd 	ap->th_block = htons((u_short)(block));
    718   1.1       cgd 	(void) send(peer, ackbuf, 4, 0);
    719   1.1       cgd 
    720   1.1       cgd 	signal(SIGALRM, justquit);      /* just quit on timeout */
    721   1.1       cgd 	alarm(rexmtval);
    722   1.1       cgd 	n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
    723   1.1       cgd 	alarm(0);
    724   1.1       cgd 	if (n >= 4 &&                   /* if read some data */
    725   1.1       cgd 	    dp->th_opcode == DATA &&    /* and got a data block */
    726   1.1       cgd 	    block == dp->th_block) {	/* then my last ack was lost */
    727   1.1       cgd 		(void) send(peer, ackbuf, 4, 0);     /* resend final ack */
    728   1.1       cgd 	}
    729   1.1       cgd abort:
    730   1.1       cgd 	return;
    731   1.1       cgd }
    732   1.1       cgd 
    733  1.13   mycroft const struct errmsg {
    734   1.1       cgd 	int	e_code;
    735  1.13   mycroft 	const char *e_msg;
    736   1.1       cgd } errmsgs[] = {
    737   1.1       cgd 	{ EUNDEF,	"Undefined error code" },
    738   1.1       cgd 	{ ENOTFOUND,	"File not found" },
    739   1.1       cgd 	{ EACCESS,	"Access violation" },
    740   1.1       cgd 	{ ENOSPACE,	"Disk full or allocation exceeded" },
    741   1.1       cgd 	{ EBADOP,	"Illegal TFTP operation" },
    742   1.1       cgd 	{ EBADID,	"Unknown transfer ID" },
    743   1.1       cgd 	{ EEXISTS,	"File already exists" },
    744   1.1       cgd 	{ ENOUSER,	"No such user" },
    745   1.1       cgd 	{ -1,		0 }
    746   1.1       cgd };
    747   1.1       cgd 
    748  1.13   mycroft static const char *
    749   1.9       mrg errtomsg(error)
    750   1.9       mrg 	int error;
    751   1.9       mrg {
    752   1.9       mrg 	static char buf[20];
    753  1.14     lukem 	const struct errmsg *pe;
    754   1.9       mrg 
    755   1.9       mrg 	if (error == 0)
    756   1.9       mrg 		return "success";
    757   1.9       mrg 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    758   1.9       mrg 		if (pe->e_code == error)
    759   1.9       mrg 			return pe->e_msg;
    760   1.9       mrg 	sprintf(buf, "error %d", error);
    761   1.9       mrg 	return buf;
    762   1.9       mrg }
    763   1.9       mrg 
    764   1.1       cgd /*
    765   1.1       cgd  * Send a nak packet (error message).
    766   1.1       cgd  * Error code passed in is one of the
    767   1.1       cgd  * standard TFTP codes, or a UNIX errno
    768   1.1       cgd  * offset by 100.
    769   1.1       cgd  */
    770   1.9       mrg static void
    771   1.1       cgd nak(error)
    772   1.1       cgd 	int error;
    773   1.1       cgd {
    774  1.14     lukem 	struct tftphdr *tp;
    775   1.1       cgd 	int length;
    776  1.14     lukem 	const struct errmsg *pe;
    777   1.1       cgd 
    778   1.1       cgd 	tp = (struct tftphdr *)buf;
    779   1.1       cgd 	tp->th_opcode = htons((u_short)ERROR);
    780   1.1       cgd 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    781   1.1       cgd 		if (pe->e_code == error)
    782   1.1       cgd 			break;
    783   1.1       cgd 	if (pe->e_code < 0) {
    784   1.1       cgd 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
    785  1.13   mycroft 		strcpy(tp->th_msg, strerror(error - 100));
    786  1.13   mycroft 	} else {
    787  1.13   mycroft 		tp->th_code = htons((u_short)error);
    788  1.13   mycroft 		strcpy(tp->th_msg, pe->e_msg);
    789   1.1       cgd 	}
    790   1.1       cgd 	length = strlen(pe->e_msg);
    791   1.1       cgd 	tp->th_msg[length] = '\0';
    792   1.1       cgd 	length += 5;
    793   1.1       cgd 	if (send(peer, buf, length, 0) != length)
    794  1.14     lukem 		syslog(LOG_ERR, "nak: %m");
    795   1.9       mrg }
    796   1.9       mrg 
    797   1.9       mrg static char *
    798   1.9       mrg verifyhost(fromp)
    799  1.18    itojun 	struct sockaddr *fromp;
    800   1.9       mrg {
    801  1.18    itojun 	static char hbuf[MAXHOSTNAMELEN];
    802   1.9       mrg 
    803  1.18    itojun 	getnameinfo(fromp, fromp->sa_len, hbuf, sizeof(hbuf), NULL, 0, 0);
    804  1.18    itojun 	return hbuf;
    805   1.1       cgd }
    806