Home | History | Annotate | Line # | Download | only in tftpd
tftpd.c revision 1.18.8.4
      1  1.18.8.4       jmc /*	$NetBSD: tftpd.c,v 1.18.8.4 2004/04/09 03:07:03 jmc 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.8.4       jmc __RCSID("$NetBSD: tftpd.c,v 1.18.8.4 2004/04/09 03:07:03 jmc 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 <netinet/in.h>
     60       1.1       cgd #include <arpa/tftp.h>
     61       1.9       mrg #include <arpa/inet.h>
     62       1.9       mrg 
     63       1.9       mrg #include <ctype.h>
     64       1.9       mrg #include <errno.h>
     65       1.9       mrg #include <fcntl.h>
     66  1.18.8.4       jmc #include <fcntl.h>
     67      1.14     lukem #include <grp.h>
     68       1.1       cgd #include <netdb.h>
     69      1.14     lukem #include <pwd.h>
     70       1.1       cgd #include <setjmp.h>
     71       1.9       mrg #include <signal.h>
     72  1.18.8.4       jmc #include <signal.h>
     73       1.1       cgd #include <stdio.h>
     74       1.9       mrg #include <stdlib.h>
     75       1.1       cgd #include <string.h>
     76       1.9       mrg #include <syslog.h>
     77       1.9       mrg #include <unistd.h>
     78       1.9       mrg 
     79       1.9       mrg #include "tftpsubs.h"
     80       1.1       cgd 
     81      1.14     lukem #define	DEFAULTUSER	"nobody"
     82       1.7       jtc 
     83       1.1       cgd #define	TIMEOUT		5
     84       1.1       cgd 
     85       1.5       cgd extern	char *__progname;
     86       1.1       cgd int	peer;
     87       1.1       cgd int	rexmtval = TIMEOUT;
     88       1.1       cgd int	maxtimeout = 5*TIMEOUT;
     89       1.1       cgd 
     90  1.18.8.4       jmc char	buf[MAXPKTSIZE];
     91       1.1       cgd char	ackbuf[PKTSIZE];
     92  1.18.8.4       jmc char	oackbuf[PKTSIZE];
     93      1.18    itojun struct	sockaddr_storage from;
     94       1.1       cgd int	fromlen;
     95  1.18.8.4       jmc int	debug;
     96  1.18.8.4       jmc 
     97  1.18.8.4       jmc int	tftp_opt_tsize = 0;
     98  1.18.8.4       jmc int	tftp_blksize = SEGSIZE;
     99  1.18.8.4       jmc int	tftp_tsize = 0;
    100       1.1       cgd 
    101       1.9       mrg /*
    102       1.9       mrg  * Null-terminated directory prefix list for absolute pathname requests and
    103       1.9       mrg  * search list for relative pathname requests.
    104       1.9       mrg  *
    105       1.9       mrg  * MAXDIRS should be at least as large as the number of arguments that
    106       1.9       mrg  * inetd allows (currently 20).
    107       1.9       mrg  */
    108       1.9       mrg #define MAXDIRS	20
    109       1.9       mrg static struct dirlist {
    110       1.9       mrg 	char	*name;
    111       1.9       mrg 	int	len;
    112       1.9       mrg } dirs[MAXDIRS+1];
    113       1.9       mrg static int	suppress_naks;
    114       1.9       mrg static int	logging;
    115       1.9       mrg static int	secure;
    116       1.9       mrg static char	*securedir;
    117       1.9       mrg 
    118       1.9       mrg struct formats;
    119       1.9       mrg 
    120  1.18.8.4       jmc static const char *errtomsg(int);
    121  1.18.8.4       jmc static void	 nak(int);
    122  1.18.8.4       jmc static void	 tftp(struct tftphdr *, int);
    123  1.18.8.4       jmc static void	 usage(void);
    124  1.18.8.4       jmc static char	*verifyhost(struct sockaddr *);
    125  1.18.8.4       jmc void	justquit(int);
    126  1.18.8.4       jmc int	main(int, char **);
    127  1.18.8.4       jmc void	recvfile(struct formats *, int, int);
    128  1.18.8.4       jmc void	sendfile(struct formats *, int, int);
    129  1.18.8.4       jmc void	timer(int);
    130  1.18.8.4       jmc static const char *opcode(int);
    131  1.18.8.4       jmc int	validate_access(char **, int);
    132       1.1       cgd 
    133       1.9       mrg struct formats {
    134  1.18.8.4       jmc 	const char	*f_mode;
    135  1.18.8.4       jmc 	int		(*f_validate)(char **, int);
    136  1.18.8.4       jmc 	void		(*f_send)(struct formats *, int, int);
    137  1.18.8.4       jmc 	void		(*f_recv)(struct formats *, int, int);
    138  1.18.8.4       jmc 	int		f_convert;
    139       1.9       mrg } formats[] = {
    140       1.9       mrg 	{ "netascii",	validate_access,	sendfile,	recvfile, 1 },
    141       1.9       mrg 	{ "octet",	validate_access,	sendfile,	recvfile, 0 },
    142       1.9       mrg 	{ 0 }
    143       1.9       mrg };
    144       1.4   mycroft 
    145       1.5       cgd static void
    146  1.18.8.4       jmc usage(void)
    147       1.5       cgd {
    148  1.18.8.4       jmc 
    149      1.14     lukem 	syslog(LOG_ERR,
    150  1.18.8.4       jmc     "Usage: %s [-dln] [-u user] [-g group] [-s directory] [directory ...]",		    __progname);
    151       1.5       cgd 	exit(1);
    152       1.5       cgd }
    153       1.5       cgd 
    154       1.9       mrg int
    155  1.18.8.4       jmc main(int argc, char *argv[])
    156       1.1       cgd {
    157      1.18    itojun 	struct sockaddr_storage me;
    158  1.18.8.4       jmc 	struct passwd	*pwent;
    159  1.18.8.4       jmc 	struct group	*grent;
    160  1.18.8.4       jmc 	struct tftphdr	*tp;
    161  1.18.8.4       jmc 	char		*tgtuser, *tgtgroup, *ep;
    162  1.18.8.4       jmc 	int	n, ch, on, fd;
    163  1.18.8.4       jmc 	int	len, soopt;
    164  1.18.8.4       jmc 	uid_t	curuid, tgtuid;
    165  1.18.8.4       jmc 	gid_t	curgid, tgtgid;
    166  1.18.8.4       jmc 	long	nid;
    167       1.1       cgd 
    168  1.18.8.4       jmc 	n = 0;
    169  1.18.8.4       jmc 	fd = 0;
    170      1.11     lukem 	openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
    171      1.14     lukem 	tgtuser = DEFAULTUSER;
    172      1.14     lukem 	tgtgroup = NULL;
    173      1.14     lukem 	curuid = getuid();
    174      1.14     lukem 	curgid = getgid();
    175       1.5       cgd 
    176  1.18.8.4       jmc 	while ((ch = getopt(argc, argv, "dg:lns:u:")) != -1)
    177       1.9       mrg 		switch (ch) {
    178  1.18.8.4       jmc 		case 'd':
    179  1.18.8.4       jmc 			debug++;
    180  1.18.8.4       jmc 			break;
    181      1.14     lukem 
    182      1.14     lukem 		case 'g':
    183      1.14     lukem 			tgtgroup = optarg;
    184      1.14     lukem 			break;
    185      1.14     lukem 
    186       1.9       mrg 		case 'l':
    187       1.9       mrg 			logging = 1;
    188       1.9       mrg 			break;
    189       1.9       mrg 
    190       1.9       mrg 		case 'n':
    191       1.9       mrg 			suppress_naks = 1;
    192       1.9       mrg 			break;
    193       1.9       mrg 
    194       1.5       cgd 		case 's':
    195       1.5       cgd 			secure = 1;
    196       1.9       mrg 			securedir = optarg;
    197       1.5       cgd 			break;
    198       1.5       cgd 
    199      1.14     lukem 		case 'u':
    200      1.14     lukem 			tgtuser = optarg;
    201      1.14     lukem 			break;
    202      1.14     lukem 
    203       1.5       cgd 		default:
    204       1.5       cgd 			usage();
    205       1.5       cgd 			break;
    206       1.5       cgd 		}
    207       1.5       cgd 
    208       1.9       mrg 	if (optind < argc) {
    209       1.9       mrg 		struct dirlist *dirp;
    210       1.9       mrg 
    211       1.9       mrg 		/* Get list of directory prefixes. Skip relative pathnames. */
    212       1.9       mrg 		for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
    213       1.9       mrg 		     optind++) {
    214       1.9       mrg 			if (argv[optind][0] == '/') {
    215       1.9       mrg 				dirp->name = argv[optind];
    216       1.9       mrg 				dirp->len  = strlen(dirp->name);
    217       1.9       mrg 				dirp++;
    218       1.9       mrg 			}
    219       1.9       mrg 		}
    220       1.9       mrg 	}
    221       1.9       mrg 
    222      1.14     lukem 	if (*tgtuser == '\0' || (tgtgroup != NULL && *tgtgroup == '\0'))
    223      1.14     lukem 		usage();
    224      1.14     lukem 
    225      1.14     lukem 	nid = (strtol(tgtuser, &ep, 10));
    226      1.14     lukem 	if (*ep == '\0') {
    227      1.14     lukem 		if (nid > UID_MAX) {
    228      1.14     lukem 			syslog(LOG_ERR, "uid %ld is too large", nid);
    229      1.14     lukem 			exit(1);
    230      1.14     lukem 		}
    231      1.14     lukem 		pwent = getpwuid((uid_t)nid);
    232      1.14     lukem 	} else
    233      1.14     lukem 		pwent = getpwnam(tgtuser);
    234      1.14     lukem 	if (pwent == NULL) {
    235      1.14     lukem 		syslog(LOG_ERR, "unknown user `%s'", tgtuser);
    236      1.14     lukem 		exit(1);
    237      1.14     lukem 	}
    238      1.14     lukem 	tgtuid = pwent->pw_uid;
    239      1.14     lukem 	tgtgid = pwent->pw_gid;
    240      1.14     lukem 
    241      1.14     lukem 	if (tgtgroup != NULL) {
    242      1.14     lukem 		nid = (strtol(tgtgroup, &ep, 10));
    243      1.14     lukem 		if (*ep == '\0') {
    244      1.14     lukem 			if (nid > GID_MAX) {
    245      1.14     lukem 				syslog(LOG_ERR, "gid %ld is too large", nid);
    246      1.14     lukem 				exit(1);
    247      1.14     lukem 			}
    248      1.14     lukem 			grent = getgrgid((gid_t)nid);
    249      1.14     lukem 		} else
    250      1.14     lukem 			grent = getgrnam(tgtgroup);
    251      1.14     lukem 		if (grent != NULL)
    252      1.14     lukem 			tgtgid = grent->gr_gid;
    253      1.14     lukem 		else {
    254      1.14     lukem 			syslog(LOG_ERR, "unknown group `%s'", tgtgroup);
    255      1.14     lukem 			exit(1);
    256      1.14     lukem 		}
    257      1.14     lukem 	}
    258      1.14     lukem 
    259       1.9       mrg 	if (secure) {
    260       1.9       mrg 		if (chdir(securedir) < 0) {
    261       1.9       mrg 			syslog(LOG_ERR, "chdir %s: %m", securedir);
    262       1.9       mrg 			exit(1);
    263       1.4   mycroft 		}
    264       1.9       mrg 		if (chroot(".")) {
    265      1.14     lukem 			syslog(LOG_ERR, "chroot: %m");
    266       1.4   mycroft 			exit(1);
    267       1.4   mycroft 		}
    268       1.4   mycroft 	}
    269       1.5       cgd 
    270  1.18.8.4       jmc 	if (logging)
    271  1.18.8.4       jmc 		syslog(LOG_DEBUG, "running as user `%s' (%d), group `%s' (%d)",
    272  1.18.8.4       jmc 		    tgtuser, tgtuid, tgtgroup ? tgtgroup : "(unspecified)",
    273  1.18.8.4       jmc 		    tgtgid);
    274      1.14     lukem 	if (curgid != tgtgid) {
    275      1.14     lukem 		if (setgid(tgtgid)) {
    276      1.14     lukem 			syslog(LOG_ERR, "setgid to %d: %m", (int)tgtgid);
    277      1.14     lukem 			exit(1);
    278      1.14     lukem 		}
    279      1.14     lukem 		if (setgroups(0, NULL)) {
    280      1.14     lukem 			syslog(LOG_ERR, "setgroups: %m");
    281      1.14     lukem 			exit(1);
    282      1.14     lukem 		}
    283       1.8       mrg 	}
    284       1.8       mrg 
    285      1.14     lukem 	if (curuid != tgtuid) {
    286      1.14     lukem 		if (setuid(tgtuid)) {
    287      1.14     lukem 			syslog(LOG_ERR, "setuid to %d: %m", (int)tgtuid);
    288      1.14     lukem 			exit(1);
    289      1.14     lukem 		}
    290       1.4   mycroft 	}
    291       1.5       cgd 
    292       1.9       mrg 	on = 1;
    293       1.5       cgd 	if (ioctl(fd, FIONBIO, &on) < 0) {
    294      1.14     lukem 		syslog(LOG_ERR, "ioctl(FIONBIO): %m");
    295       1.1       cgd 		exit(1);
    296       1.1       cgd 	}
    297       1.1       cgd 	fromlen = sizeof (from);
    298       1.5       cgd 	n = recvfrom(fd, buf, sizeof (buf), 0,
    299       1.1       cgd 	    (struct sockaddr *)&from, &fromlen);
    300       1.1       cgd 	if (n < 0) {
    301      1.14     lukem 		syslog(LOG_ERR, "recvfrom: %m");
    302       1.1       cgd 		exit(1);
    303       1.1       cgd 	}
    304       1.1       cgd 	/*
    305       1.1       cgd 	 * Now that we have read the message out of the UDP
    306       1.1       cgd 	 * socket, we fork and exit.  Thus, inetd will go back
    307       1.1       cgd 	 * to listening to the tftp port, and the next request
    308       1.1       cgd 	 * to come in will start up a new instance of tftpd.
    309       1.1       cgd 	 *
    310       1.1       cgd 	 * We do this so that inetd can run tftpd in "wait" mode.
    311       1.1       cgd 	 * The problem with tftpd running in "nowait" mode is that
    312       1.1       cgd 	 * inetd may get one or more successful "selects" on the
    313       1.1       cgd 	 * tftp port before we do our receive, so more than one
    314       1.1       cgd 	 * instance of tftpd may be started up.  Worse, if tftpd
    315       1.1       cgd 	 * break before doing the above "recvfrom", inetd would
    316       1.1       cgd 	 * spawn endless instances, clogging the system.
    317       1.1       cgd 	 */
    318       1.1       cgd 	{
    319       1.1       cgd 		int pid;
    320       1.1       cgd 		int i, j;
    321       1.1       cgd 
    322       1.1       cgd 		for (i = 1; i < 20; i++) {
    323       1.1       cgd 		    pid = fork();
    324       1.1       cgd 		    if (pid < 0) {
    325       1.1       cgd 				sleep(i);
    326       1.1       cgd 				/*
    327       1.1       cgd 				 * flush out to most recently sent request.
    328       1.1       cgd 				 *
    329       1.1       cgd 				 * This may drop some request, but those
    330       1.1       cgd 				 * will be resent by the clients when
    331       1.1       cgd 				 * they timeout.  The positive effect of
    332       1.1       cgd 				 * this flush is to (try to) prevent more
    333       1.1       cgd 				 * than one tftpd being started up to service
    334       1.1       cgd 				 * a single request from a single client.
    335       1.1       cgd 				 */
    336       1.1       cgd 				j = sizeof from;
    337       1.5       cgd 				i = recvfrom(fd, buf, sizeof (buf), 0,
    338       1.1       cgd 				    (struct sockaddr *)&from, &j);
    339       1.1       cgd 				if (i > 0) {
    340       1.1       cgd 					n = i;
    341       1.1       cgd 					fromlen = j;
    342       1.1       cgd 				}
    343       1.1       cgd 		    } else {
    344       1.1       cgd 				break;
    345       1.1       cgd 		    }
    346       1.1       cgd 		}
    347       1.1       cgd 		if (pid < 0) {
    348      1.14     lukem 			syslog(LOG_ERR, "fork: %m");
    349       1.1       cgd 			exit(1);
    350       1.1       cgd 		} else if (pid != 0) {
    351       1.1       cgd 			exit(0);
    352       1.1       cgd 		}
    353       1.1       cgd 	}
    354      1.15  explorer 
    355      1.15  explorer 	/*
    356      1.15  explorer 	 * remember what address this was sent to, so we can respond on the
    357      1.15  explorer 	 * same interface
    358      1.15  explorer 	 */
    359      1.18    itojun 	len = sizeof(me);
    360      1.18    itojun 	if (getsockname(fd, (struct sockaddr *)&me, &len) == 0) {
    361      1.18    itojun 		switch (me.ss_family) {
    362      1.18    itojun 		case AF_INET:
    363      1.18    itojun 			((struct sockaddr_in *)&me)->sin_port = 0;
    364      1.18    itojun 			break;
    365      1.18    itojun 		case AF_INET6:
    366      1.18    itojun 			((struct sockaddr_in6 *)&me)->sin6_port = 0;
    367      1.18    itojun 			break;
    368      1.18    itojun 		default:
    369      1.18    itojun 			/* unsupported */
    370      1.18    itojun 			break;
    371      1.18    itojun 		}
    372      1.18    itojun 	} else {
    373      1.18    itojun 		memset(&me, 0, sizeof(me));
    374      1.18    itojun 		me.ss_family = from.ss_family;
    375      1.18    itojun 		me.ss_len = from.ss_len;
    376      1.15  explorer 	}
    377      1.15  explorer 
    378       1.1       cgd 	alarm(0);
    379       1.5       cgd 	close(fd);
    380       1.1       cgd 	close(1);
    381      1.18    itojun 	peer = socket(from.ss_family, SOCK_DGRAM, 0);
    382       1.1       cgd 	if (peer < 0) {
    383      1.14     lukem 		syslog(LOG_ERR, "socket: %m");
    384       1.1       cgd 		exit(1);
    385       1.1       cgd 	}
    386      1.18    itojun 	if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) {
    387      1.14     lukem 		syslog(LOG_ERR, "bind: %m");
    388       1.1       cgd 		exit(1);
    389       1.1       cgd 	}
    390      1.18    itojun 	if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) {
    391      1.14     lukem 		syslog(LOG_ERR, "connect: %m");
    392       1.1       cgd 		exit(1);
    393       1.1       cgd 	}
    394  1.18.8.4       jmc 	soopt = 65536;	/* larger than we'll ever need */
    395  1.18.8.4       jmc 	if (setsockopt(peer, SOL_SOCKET, SO_SNDBUF, (void *) &soopt, sizeof(soopt)) < 0) {
    396  1.18.8.4       jmc 		syslog(LOG_ERR, "set SNDBUF: %m");
    397  1.18.8.4       jmc 		exit(1);
    398  1.18.8.4       jmc 	}
    399  1.18.8.4       jmc 	if (setsockopt(peer, SOL_SOCKET, SO_RCVBUF, (void *) &soopt, sizeof(soopt)) < 0) {
    400  1.18.8.4       jmc 		syslog(LOG_ERR, "set RCVBUF: %m");
    401  1.18.8.4       jmc 		exit(1);
    402  1.18.8.4       jmc 	}
    403  1.18.8.4       jmc 
    404       1.1       cgd 	tp = (struct tftphdr *)buf;
    405       1.1       cgd 	tp->th_opcode = ntohs(tp->th_opcode);
    406       1.1       cgd 	if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
    407       1.1       cgd 		tftp(tp, n);
    408       1.1       cgd 	exit(1);
    409       1.1       cgd }
    410       1.1       cgd 
    411  1.18.8.4       jmc static int
    412  1.18.8.4       jmc blk_handler(struct tftphdr *tp, char *opt, char *val, char *ack,
    413  1.18.8.4       jmc 	    int *ackl, int *ec)
    414  1.18.8.4       jmc {
    415  1.18.8.4       jmc 	unsigned long bsize;
    416  1.18.8.4       jmc 	char *endp;
    417  1.18.8.4       jmc 	int l;
    418  1.18.8.4       jmc 
    419  1.18.8.4       jmc 	/*
    420  1.18.8.4       jmc 	 * On these failures, we could just ignore the blocksize option.
    421  1.18.8.4       jmc 	 * Perhaps that should be a command-line option.
    422  1.18.8.4       jmc 	 */
    423  1.18.8.4       jmc 	errno = 0;
    424  1.18.8.4       jmc 	bsize = strtoul(val, &endp, 10);
    425  1.18.8.4       jmc 	if ((bsize == ULONG_MAX && errno == ERANGE) || *endp) {
    426  1.18.8.4       jmc 		syslog(LOG_NOTICE, "%s: %s request for %s: "
    427  1.18.8.4       jmc 			"illegal value %s for blksize option",
    428  1.18.8.4       jmc 			verifyhost((struct sockaddr *)&from),
    429  1.18.8.4       jmc 			tp->th_opcode == WRQ ? "write" : "read",
    430  1.18.8.4       jmc 			tp->th_stuff, val);
    431  1.18.8.4       jmc 		return 0;
    432  1.18.8.4       jmc 	}
    433  1.18.8.4       jmc 	if (bsize < 8 || bsize > 65464) {
    434  1.18.8.4       jmc 		syslog(LOG_NOTICE, "%s: %s request for %s: "
    435  1.18.8.4       jmc 			"out of range value %s for blksize option",
    436  1.18.8.4       jmc 			verifyhost((struct sockaddr *)&from),
    437  1.18.8.4       jmc 			tp->th_opcode == WRQ ? "write" : "read",
    438  1.18.8.4       jmc 			tp->th_stuff, val);
    439  1.18.8.4       jmc 		return 0;
    440  1.18.8.4       jmc 	}
    441  1.18.8.4       jmc 
    442  1.18.8.4       jmc 	tftp_blksize = bsize;
    443  1.18.8.4       jmc 	strcpy(ack + *ackl, "blksize");
    444  1.18.8.4       jmc 	*ackl += 8;
    445  1.18.8.4       jmc 	l = sprintf(ack + *ackl, "%lu", bsize);
    446  1.18.8.4       jmc 	*ackl += l + 1;
    447  1.18.8.4       jmc 
    448  1.18.8.4       jmc 	return 0;
    449  1.18.8.4       jmc }
    450  1.18.8.4       jmc 
    451  1.18.8.4       jmc static int
    452  1.18.8.4       jmc timeout_handler(struct tftphdr *tp, char *opt, char *val, char *ack,
    453  1.18.8.4       jmc 		int *ackl, int *ec)
    454  1.18.8.4       jmc {
    455  1.18.8.4       jmc 	unsigned long tout;
    456  1.18.8.4       jmc 	char *endp;
    457  1.18.8.4       jmc 	int l;
    458  1.18.8.4       jmc 
    459  1.18.8.4       jmc 	errno = 0;
    460  1.18.8.4       jmc 	tout = strtoul(val, &endp, 10);
    461  1.18.8.4       jmc 	if ((tout == ULONG_MAX && errno == ERANGE) || *endp) {
    462  1.18.8.4       jmc 		syslog(LOG_NOTICE, "%s: %s request for %s: "
    463  1.18.8.4       jmc 			"illegal value %s for timeout option",
    464  1.18.8.4       jmc 			verifyhost((struct sockaddr *)&from),
    465  1.18.8.4       jmc 			tp->th_opcode == WRQ ? "write" : "read",
    466  1.18.8.4       jmc 			tp->th_stuff, val);
    467  1.18.8.4       jmc 		return 0;
    468  1.18.8.4       jmc 	}
    469  1.18.8.4       jmc 	if (tout < 1 || tout > 255) {
    470  1.18.8.4       jmc 		syslog(LOG_NOTICE, "%s: %s request for %s: "
    471  1.18.8.4       jmc 			"out of range value %s for timeout option",
    472  1.18.8.4       jmc 			verifyhost((struct sockaddr *)&from),
    473  1.18.8.4       jmc 			tp->th_opcode == WRQ ? "write" : "read",
    474  1.18.8.4       jmc 			tp->th_stuff, val);
    475  1.18.8.4       jmc 		return 0;
    476  1.18.8.4       jmc 	}
    477  1.18.8.4       jmc 
    478  1.18.8.4       jmc 	rexmtval = tout;
    479  1.18.8.4       jmc 	strcpy(ack + *ackl, "timeout");
    480  1.18.8.4       jmc 	*ackl += 8;
    481  1.18.8.4       jmc 	l = sprintf(ack + *ackl, "%lu", tout);
    482  1.18.8.4       jmc 	*ackl += l + 1;
    483  1.18.8.4       jmc 
    484  1.18.8.4       jmc 	/*
    485  1.18.8.4       jmc 	 * Arbitrarily pick a maximum timeout on a request to 3
    486  1.18.8.4       jmc 	 * retransmissions if the interval timeout is more than
    487  1.18.8.4       jmc 	 * one minute.  Longest possible timeout is therefore
    488  1.18.8.4       jmc 	 * 3 * 255 - 1, or 764 seconds.
    489  1.18.8.4       jmc 	 */
    490  1.18.8.4       jmc 	if (rexmtval > 60) {
    491  1.18.8.4       jmc 		maxtimeout = rexmtval * 3;
    492  1.18.8.4       jmc 	} else {
    493  1.18.8.4       jmc 		maxtimeout = rexmtval * 5;
    494  1.18.8.4       jmc 	}
    495  1.18.8.4       jmc 
    496  1.18.8.4       jmc 	return 0;
    497  1.18.8.4       jmc }
    498  1.18.8.4       jmc 
    499  1.18.8.4       jmc static int
    500  1.18.8.4       jmc tsize_handler(struct tftphdr *tp, char *opt, char *val, char *ack,
    501  1.18.8.4       jmc 	      int *ackl, int *ec)
    502  1.18.8.4       jmc {
    503  1.18.8.4       jmc 	unsigned long fsize;
    504  1.18.8.4       jmc 	char *endp;
    505  1.18.8.4       jmc 
    506  1.18.8.4       jmc 	/*
    507  1.18.8.4       jmc 	 * Maximum file even with extended tftp is 65535 blocks of
    508  1.18.8.4       jmc 	 * length 65464, or 4290183240 octets (4784056 less than 2^32).
    509  1.18.8.4       jmc 	 * unsigned long is at least 32 bits on all NetBSD archs.
    510  1.18.8.4       jmc 	 */
    511  1.18.8.4       jmc 
    512  1.18.8.4       jmc 	errno = 0;
    513  1.18.8.4       jmc 	fsize = strtoul(val, &endp, 10);
    514  1.18.8.4       jmc 	if ((fsize == ULONG_MAX && errno == ERANGE) || *endp) {
    515  1.18.8.4       jmc 		syslog(LOG_NOTICE, "%s: %s request for %s: "
    516  1.18.8.4       jmc 			"illegal value %s for tsize option",
    517  1.18.8.4       jmc 			verifyhost((struct sockaddr *)&from),
    518  1.18.8.4       jmc 			tp->th_opcode == WRQ ? "write" : "read",
    519  1.18.8.4       jmc 			tp->th_stuff, val);
    520  1.18.8.4       jmc 		return 0;
    521  1.18.8.4       jmc 	}
    522  1.18.8.4       jmc 	if (fsize > (unsigned long) 65535 * 65464) {
    523  1.18.8.4       jmc 		syslog(LOG_NOTICE, "%s: %s request for %s: "
    524  1.18.8.4       jmc 			"out of range value %s for tsize option",
    525  1.18.8.4       jmc 			verifyhost((struct sockaddr *)&from),
    526  1.18.8.4       jmc 			tp->th_opcode == WRQ ? "write" : "read",
    527  1.18.8.4       jmc 			tp->th_stuff, val);
    528  1.18.8.4       jmc 		return 0;
    529  1.18.8.4       jmc 	}
    530  1.18.8.4       jmc 
    531  1.18.8.4       jmc 	tftp_opt_tsize = 1;
    532  1.18.8.4       jmc 	tftp_tsize = fsize;
    533  1.18.8.4       jmc 	/*
    534  1.18.8.4       jmc 	 * We will report this later -- either replying with the fsize (WRQ)
    535  1.18.8.4       jmc 	 * or replying with the actual filesize (RRQ).
    536  1.18.8.4       jmc 	 */
    537  1.18.8.4       jmc 
    538  1.18.8.4       jmc 	return 0;
    539  1.18.8.4       jmc }
    540  1.18.8.4       jmc 
    541  1.18.8.4       jmc struct tftp_options {
    542  1.18.8.4       jmc 	char *o_name;
    543  1.18.8.4       jmc 	int (*o_handler)(struct tftphdr *, char *, char *, char *,
    544  1.18.8.4       jmc 			 int *, int *);
    545  1.18.8.4       jmc } options[] = {
    546  1.18.8.4       jmc 	{ "blksize", blk_handler },
    547  1.18.8.4       jmc 	{ "timeout", timeout_handler },
    548  1.18.8.4       jmc 	{ "tsize", tsize_handler },
    549  1.18.8.4       jmc 	{ NULL, NULL }
    550  1.18.8.4       jmc };
    551  1.18.8.4       jmc 
    552  1.18.8.4       jmc /*
    553  1.18.8.4       jmc  * Get options for an extended tftp session.  Stuff the ones we
    554  1.18.8.4       jmc  * recognize in oackbuf.
    555  1.18.8.4       jmc  */
    556  1.18.8.4       jmc static int
    557  1.18.8.4       jmc get_options(struct tftphdr *tp, char *cp, int size, char *ackb,
    558  1.18.8.4       jmc     int *alen, int *err)
    559  1.18.8.4       jmc {
    560  1.18.8.4       jmc 	struct tftp_options *op;
    561  1.18.8.4       jmc 	char *option, *value, *endp;
    562  1.18.8.4       jmc 	int r, rv=0, ec=0;
    563  1.18.8.4       jmc 
    564  1.18.8.4       jmc 	endp = cp + size;
    565  1.18.8.4       jmc 	while (cp < endp) {
    566  1.18.8.4       jmc 		option = cp;
    567  1.18.8.4       jmc 		while (*cp && cp < endp) {
    568  1.18.8.4       jmc 			*cp = tolower(*cp);
    569  1.18.8.4       jmc 			cp++;
    570  1.18.8.4       jmc 		}
    571  1.18.8.4       jmc 		if (*cp) {
    572  1.18.8.4       jmc 			/* if we have garbage at the end, just ignore it */
    573  1.18.8.4       jmc 			break;
    574  1.18.8.4       jmc 		}
    575  1.18.8.4       jmc 		cp++;	/* skip over NUL */
    576  1.18.8.4       jmc 		value = cp;
    577  1.18.8.4       jmc 		while (*cp && cp < endp) {
    578  1.18.8.4       jmc 			cp++;
    579  1.18.8.4       jmc 		}
    580  1.18.8.4       jmc 		if (*cp) {
    581  1.18.8.4       jmc 			/* if we have garbage at the end, just ignore it */
    582  1.18.8.4       jmc 			break;
    583  1.18.8.4       jmc 		}
    584  1.18.8.4       jmc 		cp++;
    585  1.18.8.4       jmc 		for (op = options; op->o_name; op++) {
    586  1.18.8.4       jmc 			if (strcmp(op->o_name, option) == 0)
    587  1.18.8.4       jmc 				break;
    588  1.18.8.4       jmc 		}
    589  1.18.8.4       jmc 		if (op->o_name) {
    590  1.18.8.4       jmc 			r = op->o_handler(tp, option, value, ackb, alen, &ec);
    591  1.18.8.4       jmc 			if (r < 0) {
    592  1.18.8.4       jmc 				rv = -1;
    593  1.18.8.4       jmc 				break;
    594  1.18.8.4       jmc 			}
    595  1.18.8.4       jmc 			rv++;
    596  1.18.8.4       jmc 		} /* else ignore unknown options */
    597  1.18.8.4       jmc 	}
    598  1.18.8.4       jmc 
    599  1.18.8.4       jmc 	if (rv < 0)
    600  1.18.8.4       jmc 		*err = ec;
    601  1.18.8.4       jmc 
    602  1.18.8.4       jmc 	return rv;
    603  1.18.8.4       jmc }
    604  1.18.8.4       jmc 
    605       1.1       cgd /*
    606       1.1       cgd  * Handle initial connection protocol.
    607       1.1       cgd  */
    608       1.9       mrg static void
    609  1.18.8.4       jmc tftp(struct tftphdr *tp, int size)
    610       1.1       cgd {
    611      1.14     lukem 	struct formats *pf;
    612  1.18.8.4       jmc 	char	*cp;
    613  1.18.8.4       jmc 	char	*filename, *mode;
    614  1.18.8.4       jmc 	int	 first, ecode, alen, etftp=0, r;
    615  1.18.8.4       jmc 
    616  1.18.8.4       jmc 	first = 1;
    617  1.18.8.4       jmc 	mode = NULL;
    618       1.1       cgd 
    619       1.1       cgd 	filename = cp = tp->th_stuff;
    620       1.1       cgd again:
    621       1.1       cgd 	while (cp < buf + size) {
    622       1.1       cgd 		if (*cp == '\0')
    623       1.1       cgd 			break;
    624       1.1       cgd 		cp++;
    625       1.1       cgd 	}
    626       1.1       cgd 	if (*cp != '\0') {
    627       1.1       cgd 		nak(EBADOP);
    628       1.1       cgd 		exit(1);
    629       1.1       cgd 	}
    630       1.1       cgd 	if (first) {
    631       1.1       cgd 		mode = ++cp;
    632       1.1       cgd 		first = 0;
    633       1.1       cgd 		goto again;
    634       1.1       cgd 	}
    635       1.1       cgd 	for (cp = mode; *cp; cp++)
    636       1.1       cgd 		if (isupper(*cp))
    637       1.1       cgd 			*cp = tolower(*cp);
    638       1.1       cgd 	for (pf = formats; pf->f_mode; pf++)
    639       1.1       cgd 		if (strcmp(pf->f_mode, mode) == 0)
    640       1.1       cgd 			break;
    641       1.1       cgd 	if (pf->f_mode == 0) {
    642       1.1       cgd 		nak(EBADOP);
    643       1.1       cgd 		exit(1);
    644       1.1       cgd 	}
    645  1.18.8.4       jmc 	/*
    646  1.18.8.4       jmc 	 * cp currently points to the NUL byte following the mode.
    647  1.18.8.4       jmc 	 *
    648  1.18.8.4       jmc 	 * If we have some valid options, then let's assume that we're
    649  1.18.8.4       jmc 	 * now dealing with an extended tftp session.  Note that if we
    650  1.18.8.4       jmc 	 * don't get any options, then we *must* assume that we do not
    651  1.18.8.4       jmc 	 * have an extended tftp session.  If we get options, we fill
    652  1.18.8.4       jmc 	 * in the ack buf to acknowledge them.  If we skip that, then
    653  1.18.8.4       jmc 	 * the client *must* assume that we are not using an extended
    654  1.18.8.4       jmc 	 * session.
    655  1.18.8.4       jmc 	 */
    656  1.18.8.4       jmc 	size -= (++cp - (char *) tp);
    657  1.18.8.4       jmc 	if (size > 0 && *cp) {
    658  1.18.8.4       jmc 		alen = 2; /* Skip over opcode */
    659  1.18.8.4       jmc 		r = get_options(tp, cp, size, oackbuf, &alen, &ecode);
    660  1.18.8.4       jmc 		if (r > 0) {
    661  1.18.8.4       jmc 			etftp = 1;
    662  1.18.8.4       jmc 		} else if (r < 0) {
    663  1.18.8.4       jmc 			nak(ecode);
    664  1.18.8.4       jmc 			exit(1);
    665  1.18.8.4       jmc 		}
    666  1.18.8.4       jmc 	}
    667       1.9       mrg 	ecode = (*pf->f_validate)(&filename, tp->th_opcode);
    668       1.9       mrg 	if (logging) {
    669       1.9       mrg 		syslog(LOG_INFO, "%s: %s request for %s: %s",
    670      1.18    itojun 			verifyhost((struct sockaddr *)&from),
    671       1.9       mrg 			tp->th_opcode == WRQ ? "write" : "read",
    672       1.9       mrg 			filename, errtomsg(ecode));
    673       1.9       mrg 	}
    674       1.1       cgd 	if (ecode) {
    675       1.9       mrg 		/*
    676       1.9       mrg 		 * Avoid storms of naks to a RRQ broadcast for a relative
    677       1.9       mrg 		 * bootfile pathname from a diskless Sun.
    678       1.9       mrg 		 */
    679       1.9       mrg 		if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
    680       1.9       mrg 			exit(0);
    681       1.1       cgd 		nak(ecode);
    682       1.1       cgd 		exit(1);
    683       1.1       cgd 	}
    684  1.18.8.4       jmc 
    685  1.18.8.4       jmc 	if (etftp) {
    686  1.18.8.4       jmc 		struct tftphdr *oack_h;
    687  1.18.8.4       jmc 
    688  1.18.8.4       jmc 		if (tftp_opt_tsize) {
    689  1.18.8.4       jmc 			int l;
    690  1.18.8.4       jmc 
    691  1.18.8.4       jmc 			strcpy(oackbuf + alen, "tsize");
    692  1.18.8.4       jmc 			alen += 6;
    693  1.18.8.4       jmc 			l = sprintf(oackbuf + alen, "%u", tftp_tsize);
    694  1.18.8.4       jmc 			alen += l + 1;
    695  1.18.8.4       jmc 		}
    696  1.18.8.4       jmc 		oack_h = (struct tftphdr *) oackbuf;
    697  1.18.8.4       jmc 		oack_h->th_opcode = htons(OACK);
    698  1.18.8.4       jmc 	}
    699  1.18.8.4       jmc 
    700       1.1       cgd 	if (tp->th_opcode == WRQ)
    701  1.18.8.4       jmc 		(*pf->f_recv)(pf, etftp, alen);
    702       1.1       cgd 	else
    703  1.18.8.4       jmc 		(*pf->f_send)(pf, etftp, alen);
    704       1.1       cgd 	exit(0);
    705       1.1       cgd }
    706       1.1       cgd 
    707       1.1       cgd 
    708       1.1       cgd FILE *file;
    709       1.1       cgd 
    710       1.1       cgd /*
    711       1.1       cgd  * Validate file access.  Since we
    712       1.1       cgd  * have no uid or gid, for now require
    713       1.1       cgd  * file to exist and be publicly
    714       1.1       cgd  * readable/writable.
    715       1.1       cgd  * If we were invoked with arguments
    716       1.1       cgd  * from inetd then the file must also be
    717       1.1       cgd  * in one of the given directory prefixes.
    718       1.1       cgd  */
    719       1.9       mrg int
    720  1.18.8.4       jmc validate_access(char **filep, int mode)
    721  1.18.8.4       jmc {
    722  1.18.8.4       jmc 	struct stat	 stbuf;
    723  1.18.8.4       jmc 	struct dirlist	*dirp;
    724  1.18.8.4       jmc 	static char	 pathname[MAXPATHLEN];
    725  1.18.8.4       jmc 	char		*filename;
    726  1.18.8.4       jmc 	int		 fd;
    727  1.18.8.4       jmc 
    728  1.18.8.4       jmc 	filename = *filep;
    729       1.1       cgd 
    730       1.9       mrg 	/*
    731       1.9       mrg 	 * Prevent tricksters from getting around the directory restrictions
    732       1.9       mrg 	 */
    733       1.9       mrg 	if (strstr(filename, "/../"))
    734       1.9       mrg 		return (EACCESS);
    735       1.9       mrg 
    736       1.9       mrg 	if (*filename == '/') {
    737       1.4   mycroft 		/*
    738       1.9       mrg 		 * Allow the request if it's in one of the approved locations.
    739       1.9       mrg 		 * Special case: check the null prefix ("/") by looking
    740       1.9       mrg 		 * for length = 1 and relying on the arg. processing that
    741       1.9       mrg 		 * it's a /.
    742       1.4   mycroft 		 */
    743       1.9       mrg 		for (dirp = dirs; dirp->name != NULL; dirp++) {
    744       1.9       mrg 			if (dirp->len == 1 ||
    745       1.9       mrg 			    (!strncmp(filename, dirp->name, dirp->len) &&
    746       1.9       mrg 			     filename[dirp->len] == '/'))
    747       1.9       mrg 				    break;
    748       1.9       mrg 		}
    749       1.9       mrg 		/* If directory list is empty, allow access to any file */
    750       1.9       mrg 		if (dirp->name == NULL && dirp != dirs)
    751       1.1       cgd 			return (EACCESS);
    752       1.9       mrg 		if (stat(filename, &stbuf) < 0)
    753       1.9       mrg 			return (errno == ENOENT ? ENOTFOUND : EACCESS);
    754      1.10   mycroft 		if (!S_ISREG(stbuf.st_mode))
    755       1.9       mrg 			return (ENOTFOUND);
    756       1.9       mrg 		if (mode == RRQ) {
    757       1.9       mrg 			if ((stbuf.st_mode & S_IROTH) == 0)
    758       1.9       mrg 				return (EACCESS);
    759       1.9       mrg 		} else {
    760       1.9       mrg 			if ((stbuf.st_mode & S_IWOTH) == 0)
    761       1.9       mrg 				return (EACCESS);
    762       1.9       mrg 		}
    763       1.1       cgd 	} else {
    764       1.9       mrg 		/*
    765       1.9       mrg 		 * Relative file name: search the approved locations for it.
    766       1.9       mrg 		 */
    767       1.9       mrg 
    768      1.16     aidan 		if (!strncmp(filename, "../", 3))
    769       1.1       cgd 			return (EACCESS);
    770       1.9       mrg 
    771       1.9       mrg 		/*
    772      1.16     aidan 		 * Find the first file that exists in any of the directories,
    773      1.16     aidan 		 * check access on it.
    774       1.9       mrg 		 */
    775       1.9       mrg 		if (dirs[0].name != NULL) {
    776       1.9       mrg 			for (dirp = dirs; dirp->name != NULL; dirp++) {
    777       1.9       mrg 				snprintf(pathname, sizeof pathname, "%s/%s",
    778       1.9       mrg 				    dirp->name, filename);
    779       1.9       mrg 				if (stat(pathname, &stbuf) == 0 &&
    780       1.9       mrg 				    (stbuf.st_mode & S_IFMT) == S_IFREG) {
    781      1.16     aidan 					break;
    782       1.9       mrg 				}
    783       1.9       mrg 			}
    784       1.9       mrg 			if (dirp->name == NULL)
    785      1.16     aidan 				return (ENOTFOUND);
    786      1.16     aidan 			if (mode == RRQ && !(stbuf.st_mode & S_IROTH))
    787      1.16     aidan 				return (EACCESS);
    788      1.16     aidan 			if (mode == WRQ && !(stbuf.st_mode & S_IWOTH))
    789      1.16     aidan 				return (EACCESS);
    790       1.9       mrg 			*filep = filename = pathname;
    791      1.16     aidan 		} else {
    792      1.16     aidan 			/*
    793      1.16     aidan 			 * If there's no directory list, take our cue from the
    794      1.16     aidan 			 * absolute file request check above (*filename == '/'),
    795      1.16     aidan 			 * and allow access to anything.
    796      1.16     aidan 			 */
    797      1.16     aidan 			if (stat(filename, &stbuf) < 0)
    798      1.16     aidan 				return (errno == ENOENT ? ENOTFOUND : EACCESS);
    799      1.16     aidan 			if (!S_ISREG(stbuf.st_mode))
    800      1.16     aidan 				return (ENOTFOUND);
    801      1.16     aidan 			if (mode == RRQ) {
    802      1.16     aidan 				if ((stbuf.st_mode & S_IROTH) == 0)
    803      1.16     aidan 					return (EACCESS);
    804      1.16     aidan 			} else {
    805      1.16     aidan 				if ((stbuf.st_mode & S_IWOTH) == 0)
    806      1.16     aidan 					return (EACCESS);
    807      1.16     aidan 			}
    808       1.9       mrg 			*filep = filename;
    809      1.16     aidan 		}
    810       1.1       cgd 	}
    811  1.18.8.4       jmc 
    812  1.18.8.4       jmc 	if (tftp_opt_tsize && mode == RRQ)
    813  1.18.8.4       jmc 		tftp_tsize = (unsigned long) stbuf.st_size;
    814  1.18.8.4       jmc 
    815      1.17    carrel 	fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY | O_TRUNC);
    816       1.1       cgd 	if (fd < 0)
    817       1.1       cgd 		return (errno + 100);
    818       1.1       cgd 	file = fdopen(fd, (mode == RRQ)? "r":"w");
    819       1.1       cgd 	if (file == NULL) {
    820      1.16     aidan 		close(fd);
    821  1.18.8.4       jmc 		return (errno + 100);
    822       1.1       cgd 	}
    823       1.1       cgd 	return (0);
    824       1.1       cgd }
    825       1.1       cgd 
    826       1.1       cgd int	timeout;
    827       1.1       cgd jmp_buf	timeoutbuf;
    828       1.1       cgd 
    829       1.1       cgd void
    830  1.18.8.4       jmc timer(int dummy)
    831       1.1       cgd {
    832       1.1       cgd 
    833       1.1       cgd 	timeout += rexmtval;
    834       1.1       cgd 	if (timeout >= maxtimeout)
    835       1.1       cgd 		exit(1);
    836       1.1       cgd 	longjmp(timeoutbuf, 1);
    837       1.1       cgd }
    838       1.1       cgd 
    839  1.18.8.4       jmc static const char *
    840  1.18.8.4       jmc opcode(int code)
    841  1.18.8.4       jmc {
    842  1.18.8.4       jmc 	static char buf[64];
    843  1.18.8.4       jmc 
    844  1.18.8.4       jmc 	switch (code) {
    845  1.18.8.4       jmc 	case RRQ:
    846  1.18.8.4       jmc 		return "RRQ";
    847  1.18.8.4       jmc 	case WRQ:
    848  1.18.8.4       jmc 		return "WRQ";
    849  1.18.8.4       jmc 	case DATA:
    850  1.18.8.4       jmc 		return "DATA";
    851  1.18.8.4       jmc 	case ACK:
    852  1.18.8.4       jmc 		return "ACK";
    853  1.18.8.4       jmc 	case ERROR:
    854  1.18.8.4       jmc 		return "ERROR";
    855  1.18.8.4       jmc 	case OACK:
    856  1.18.8.4       jmc 		return "OACK";
    857  1.18.8.4       jmc 	default:
    858  1.18.8.4       jmc 		(void)snprintf(buf, sizeof(buf), "*code %d*", code);
    859  1.18.8.4       jmc 		return buf;
    860  1.18.8.4       jmc 	}
    861  1.18.8.4       jmc }
    862  1.18.8.4       jmc 
    863       1.1       cgd /*
    864       1.1       cgd  * Send the requested file.
    865       1.1       cgd  */
    866       1.9       mrg void
    867  1.18.8.4       jmc sendfile(struct formats *pf, int etftp, int acklength)
    868       1.1       cgd {
    869  1.18.8.3        he 	volatile unsigned int block;
    870  1.18.8.4       jmc 	struct tftphdr	*dp;
    871  1.18.8.4       jmc 	struct tftphdr	*ap;    /* ack packet */
    872  1.18.8.4       jmc 	int		 size, n;
    873       1.1       cgd 
    874       1.1       cgd 	signal(SIGALRM, timer);
    875       1.1       cgd 	ap = (struct tftphdr *)ackbuf;
    876  1.18.8.4       jmc 	if (etftp) {
    877  1.18.8.4       jmc 		dp = (struct tftphdr *)oackbuf;
    878  1.18.8.4       jmc 		size = acklength - 4;
    879  1.18.8.4       jmc 		block = 0;
    880  1.18.8.4       jmc 	} else {
    881  1.18.8.4       jmc 		dp = r_init();
    882  1.18.8.4       jmc 		size = 0;
    883  1.18.8.4       jmc 		block = 1;
    884  1.18.8.4       jmc 	}
    885  1.18.8.4       jmc 
    886       1.1       cgd 	do {
    887  1.18.8.4       jmc 		if (block > 0) {
    888  1.18.8.4       jmc 			size = readit(file, &dp, tftp_blksize, pf->f_convert);
    889  1.18.8.4       jmc 			if (size < 0) {
    890  1.18.8.4       jmc 				nak(errno + 100);
    891  1.18.8.4       jmc 				goto abort;
    892  1.18.8.4       jmc 			}
    893  1.18.8.4       jmc 			dp->th_opcode = htons((u_short)DATA);
    894  1.18.8.4       jmc 			dp->th_block = htons((u_short)block);
    895       1.1       cgd 		}
    896       1.1       cgd 		timeout = 0;
    897       1.9       mrg 		(void)setjmp(timeoutbuf);
    898       1.1       cgd 
    899       1.1       cgd send_data:
    900  1.18.8.4       jmc 		if (!etftp && debug)
    901  1.18.8.4       jmc 			syslog(LOG_DEBUG, "Send DATA %u", block);
    902  1.18.8.4       jmc 		if ((n = send(peer, dp, size + 4, 0)) != size + 4) {
    903      1.14     lukem 			syslog(LOG_ERR, "tftpd: write: %m");
    904       1.1       cgd 			goto abort;
    905       1.1       cgd 		}
    906  1.18.8.4       jmc 		if (block)
    907  1.18.8.4       jmc 			read_ahead(file, tftp_blksize, pf->f_convert);
    908       1.1       cgd 		for ( ; ; ) {
    909       1.1       cgd 			alarm(rexmtval);        /* read the ack */
    910  1.18.8.4       jmc 			n = recv(peer, ackbuf, tftp_blksize, 0);
    911       1.1       cgd 			alarm(0);
    912       1.1       cgd 			if (n < 0) {
    913      1.14     lukem 				syslog(LOG_ERR, "tftpd: read: %m");
    914       1.1       cgd 				goto abort;
    915       1.1       cgd 			}
    916       1.1       cgd 			ap->th_opcode = ntohs((u_short)ap->th_opcode);
    917       1.1       cgd 			ap->th_block = ntohs((u_short)ap->th_block);
    918  1.18.8.4       jmc 			switch (ap->th_opcode) {
    919  1.18.8.4       jmc 			case ERROR:
    920       1.1       cgd 				goto abort;
    921       1.9       mrg 
    922  1.18.8.4       jmc 			case ACK:
    923  1.18.8.4       jmc 				if (ap->th_block == 0) {
    924  1.18.8.4       jmc 					etftp = 0;
    925  1.18.8.4       jmc 					acklength = 0;
    926  1.18.8.4       jmc 					dp = r_init();
    927  1.18.8.4       jmc 					goto done;
    928  1.18.8.4       jmc 				}
    929       1.9       mrg 				if (ap->th_block == block)
    930  1.18.8.4       jmc 					goto done;
    931  1.18.8.4       jmc 				if (debug)
    932  1.18.8.4       jmc 					syslog(LOG_DEBUG, "Resync ACK %u != %u",
    933  1.18.8.4       jmc 					    (unsigned int)ap->th_block, block);
    934       1.1       cgd 				/* Re-synchronize with the other side */
    935  1.18.8.4       jmc 				(void) synchnet(peer, tftp_blksize);
    936       1.9       mrg 				if (ap->th_block == (block -1))
    937       1.1       cgd 					goto send_data;
    938  1.18.8.4       jmc 			default:
    939  1.18.8.4       jmc 				syslog(LOG_INFO, "Received %s in sendfile\n",
    940  1.18.8.4       jmc 				    opcode(dp->th_opcode));
    941       1.1       cgd 			}
    942       1.1       cgd 
    943       1.1       cgd 		}
    944  1.18.8.4       jmc done:
    945  1.18.8.4       jmc 		if (debug)
    946  1.18.8.4       jmc 			syslog(LOG_DEBUG, "Received ACK for block %u", block);
    947       1.1       cgd 		block++;
    948  1.18.8.4       jmc 	} while (size == tftp_blksize || block == 1);
    949       1.1       cgd abort:
    950       1.1       cgd 	(void) fclose(file);
    951       1.1       cgd }
    952       1.1       cgd 
    953       1.1       cgd void
    954  1.18.8.4       jmc justquit(int dummy)
    955       1.1       cgd {
    956  1.18.8.4       jmc 
    957       1.1       cgd 	exit(0);
    958       1.1       cgd }
    959       1.1       cgd 
    960       1.1       cgd /*
    961       1.1       cgd  * Receive a file.
    962       1.1       cgd  */
    963       1.9       mrg void
    964  1.18.8.4       jmc recvfile(struct formats *pf, int etftp, int acklength)
    965       1.1       cgd {
    966  1.18.8.3        he 	volatile unsigned int block;
    967  1.18.8.4       jmc 	struct tftphdr	*dp;
    968  1.18.8.4       jmc 	struct tftphdr	*ap;    /* ack buffer */
    969  1.18.8.4       jmc 	int		 n, size;
    970       1.1       cgd 
    971       1.1       cgd 	signal(SIGALRM, timer);
    972       1.1       cgd 	dp = w_init();
    973  1.18.8.4       jmc 	ap = (struct tftphdr *)oackbuf;
    974       1.9       mrg 	block = 0;
    975       1.1       cgd 	do {
    976       1.1       cgd 		timeout = 0;
    977  1.18.8.4       jmc 		if (etftp == 0) {
    978  1.18.8.4       jmc 			ap = (struct tftphdr *)ackbuf;
    979  1.18.8.4       jmc 			ap->th_opcode = htons((u_short)ACK);
    980  1.18.8.4       jmc 			ap->th_block = htons((u_short)block);
    981  1.18.8.4       jmc 			acklength = 4;
    982  1.18.8.4       jmc 		}
    983  1.18.8.4       jmc 		if (debug)
    984  1.18.8.4       jmc 			syslog(LOG_DEBUG, "Sending ACK for block %u\n", block);
    985       1.1       cgd 		block++;
    986       1.1       cgd 		(void) setjmp(timeoutbuf);
    987       1.1       cgd send_ack:
    988  1.18.8.4       jmc 		if (send(peer, ap, acklength, 0) != acklength) {
    989      1.14     lukem 			syslog(LOG_ERR, "tftpd: write: %m");
    990       1.1       cgd 			goto abort;
    991       1.1       cgd 		}
    992       1.1       cgd 		write_behind(file, pf->f_convert);
    993       1.1       cgd 		for ( ; ; ) {
    994       1.1       cgd 			alarm(rexmtval);
    995  1.18.8.4       jmc 			n = recv(peer, dp, tftp_blksize + 4, 0);
    996       1.1       cgd 			alarm(0);
    997       1.1       cgd 			if (n < 0) {            /* really? */
    998      1.14     lukem 				syslog(LOG_ERR, "tftpd: read: %m");
    999       1.1       cgd 				goto abort;
   1000       1.1       cgd 			}
   1001  1.18.8.4       jmc 			etftp = 0;
   1002       1.1       cgd 			dp->th_opcode = ntohs((u_short)dp->th_opcode);
   1003       1.1       cgd 			dp->th_block = ntohs((u_short)dp->th_block);
   1004  1.18.8.4       jmc 			if (debug)
   1005  1.18.8.4       jmc 				syslog(LOG_DEBUG, "Received %s for block %u",
   1006  1.18.8.4       jmc 				    opcode(dp->th_opcode),
   1007  1.18.8.4       jmc 				    (unsigned int)dp->th_block);
   1008  1.18.8.4       jmc 
   1009  1.18.8.4       jmc 			switch (dp->th_opcode) {
   1010  1.18.8.4       jmc 			case ERROR:
   1011       1.1       cgd 				goto abort;
   1012  1.18.8.4       jmc 			case DATA:
   1013  1.18.8.4       jmc 				if (dp->th_block == block)
   1014  1.18.8.4       jmc 					goto done;   /* normal */
   1015  1.18.8.4       jmc 				if (debug)
   1016  1.18.8.4       jmc 					syslog(LOG_DEBUG, "Resync %u != %u",
   1017  1.18.8.4       jmc 					    (unsigned int)dp->th_block, block);
   1018       1.1       cgd 				/* Re-synchronize with the other side */
   1019  1.18.8.4       jmc 				(void) synchnet(peer, tftp_blksize);
   1020       1.1       cgd 				if (dp->th_block == (block-1))
   1021       1.1       cgd 					goto send_ack;          /* rexmit */
   1022  1.18.8.4       jmc 				break;
   1023  1.18.8.4       jmc 			default:
   1024  1.18.8.4       jmc 				syslog(LOG_INFO, "Received %s in recvfile\n",
   1025  1.18.8.4       jmc 				    opcode(dp->th_opcode));
   1026  1.18.8.4       jmc 				break;
   1027       1.1       cgd 			}
   1028       1.1       cgd 		}
   1029  1.18.8.4       jmc done:
   1030  1.18.8.4       jmc 		if (debug)
   1031  1.18.8.4       jmc 			syslog(LOG_DEBUG, "Got block %u", block);
   1032       1.1       cgd 		/*  size = write(file, dp->th_data, n - 4); */
   1033       1.1       cgd 		size = writeit(file, &dp, n - 4, pf->f_convert);
   1034       1.1       cgd 		if (size != (n-4)) {                    /* ahem */
   1035       1.1       cgd 			if (size < 0) nak(errno + 100);
   1036       1.1       cgd 			else nak(ENOSPACE);
   1037       1.1       cgd 			goto abort;
   1038       1.1       cgd 		}
   1039  1.18.8.4       jmc 	} while (size == tftp_blksize);
   1040       1.1       cgd 	write_behind(file, pf->f_convert);
   1041       1.1       cgd 	(void) fclose(file);            /* close data file */
   1042       1.1       cgd 
   1043       1.1       cgd 	ap->th_opcode = htons((u_short)ACK);    /* send the "final" ack */
   1044       1.1       cgd 	ap->th_block = htons((u_short)(block));
   1045  1.18.8.4       jmc 	if (debug)
   1046  1.18.8.4       jmc 		syslog(LOG_DEBUG, "Send final ACK %u", block);
   1047       1.1       cgd 	(void) send(peer, ackbuf, 4, 0);
   1048       1.1       cgd 
   1049       1.1       cgd 	signal(SIGALRM, justquit);      /* just quit on timeout */
   1050       1.1       cgd 	alarm(rexmtval);
   1051       1.1       cgd 	n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
   1052       1.1       cgd 	alarm(0);
   1053       1.1       cgd 	if (n >= 4 &&                   /* if read some data */
   1054       1.1       cgd 	    dp->th_opcode == DATA &&    /* and got a data block */
   1055       1.1       cgd 	    block == dp->th_block) {	/* then my last ack was lost */
   1056       1.1       cgd 		(void) send(peer, ackbuf, 4, 0);     /* resend final ack */
   1057       1.1       cgd 	}
   1058       1.1       cgd abort:
   1059       1.1       cgd 	return;
   1060       1.1       cgd }
   1061       1.1       cgd 
   1062      1.13   mycroft const struct errmsg {
   1063  1.18.8.4       jmc 	int		 e_code;
   1064  1.18.8.4       jmc 	const char	*e_msg;
   1065       1.1       cgd } errmsgs[] = {
   1066       1.1       cgd 	{ EUNDEF,	"Undefined error code" },
   1067       1.1       cgd 	{ ENOTFOUND,	"File not found" },
   1068       1.1       cgd 	{ EACCESS,	"Access violation" },
   1069       1.1       cgd 	{ ENOSPACE,	"Disk full or allocation exceeded" },
   1070       1.1       cgd 	{ EBADOP,	"Illegal TFTP operation" },
   1071       1.1       cgd 	{ EBADID,	"Unknown transfer ID" },
   1072       1.1       cgd 	{ EEXISTS,	"File already exists" },
   1073       1.1       cgd 	{ ENOUSER,	"No such user" },
   1074  1.18.8.4       jmc 	{ EOPTNEG,	"Option negotiation failed" },
   1075       1.1       cgd 	{ -1,		0 }
   1076       1.1       cgd };
   1077       1.1       cgd 
   1078      1.13   mycroft static const char *
   1079  1.18.8.4       jmc errtomsg(int error)
   1080       1.9       mrg {
   1081  1.18.8.4       jmc 	static char ebuf[20];
   1082      1.14     lukem 	const struct errmsg *pe;
   1083       1.9       mrg 
   1084       1.9       mrg 	if (error == 0)
   1085  1.18.8.4       jmc 		return ("success");
   1086       1.9       mrg 	for (pe = errmsgs; pe->e_code >= 0; pe++)
   1087       1.9       mrg 		if (pe->e_code == error)
   1088  1.18.8.4       jmc 			return (pe->e_msg);
   1089  1.18.8.4       jmc 	snprintf(ebuf, sizeof(ebuf), "error %d", error);
   1090  1.18.8.4       jmc 	return (ebuf);
   1091       1.9       mrg }
   1092       1.9       mrg 
   1093       1.1       cgd /*
   1094       1.1       cgd  * Send a nak packet (error message).
   1095       1.1       cgd  * Error code passed in is one of the
   1096       1.1       cgd  * standard TFTP codes, or a UNIX errno
   1097       1.1       cgd  * offset by 100.
   1098       1.1       cgd  */
   1099       1.9       mrg static void
   1100  1.18.8.4       jmc nak(int error)
   1101       1.1       cgd {
   1102      1.14     lukem 	const struct errmsg *pe;
   1103  1.18.8.4       jmc 	struct tftphdr *tp;
   1104  1.18.8.4       jmc 	int	length;
   1105  1.18.8.4       jmc 	size_t	msglen;
   1106       1.1       cgd 
   1107       1.1       cgd 	tp = (struct tftphdr *)buf;
   1108       1.1       cgd 	tp->th_opcode = htons((u_short)ERROR);
   1109  1.18.8.2        he 	msglen = sizeof(buf) - (&tp->th_msg[0] - buf);
   1110       1.1       cgd 	for (pe = errmsgs; pe->e_code >= 0; pe++)
   1111       1.1       cgd 		if (pe->e_code == error)
   1112       1.1       cgd 			break;
   1113       1.1       cgd 	if (pe->e_code < 0) {
   1114       1.1       cgd 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
   1115  1.18.8.2        he 		strlcpy(tp->th_msg, strerror(error - 100), msglen);
   1116      1.13   mycroft 	} else {
   1117      1.13   mycroft 		tp->th_code = htons((u_short)error);
   1118  1.18.8.2        he 		strlcpy(tp->th_msg, pe->e_msg, msglen);
   1119       1.1       cgd 	}
   1120  1.18.8.4       jmc 	if (debug)
   1121  1.18.8.4       jmc 		syslog(LOG_DEBUG, "Send NACK %s", tp->th_msg);
   1122  1.18.8.2        he 	length = strlen(tp->th_msg);
   1123  1.18.8.2        he 	msglen = &tp->th_msg[length + 1] - buf;
   1124  1.18.8.2        he 	if (send(peer, buf, msglen, 0) != msglen)
   1125      1.14     lukem 		syslog(LOG_ERR, "nak: %m");
   1126       1.9       mrg }
   1127       1.9       mrg 
   1128       1.9       mrg static char *
   1129  1.18.8.4       jmc verifyhost(struct sockaddr *fromp)
   1130       1.9       mrg {
   1131      1.18    itojun 	static char hbuf[MAXHOSTNAMELEN];
   1132       1.9       mrg 
   1133  1.18.8.1        he 	if (getnameinfo(fromp, fromp->sa_len, hbuf, sizeof(hbuf), NULL, 0, 0))
   1134  1.18.8.1        he 		strlcpy(hbuf, "?", sizeof(hbuf));
   1135  1.18.8.4       jmc 	return (hbuf);
   1136       1.1       cgd }
   1137