xref: /titanic_54/usr/src/cmd/cmd-inet/usr.sbin/in.tftpd.c (revision 294f57870cccad147dad29221c0e628be2f458dc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*294f5787Sas198278  * Common Development and Distribution License (the "License").
6*294f5787Sas198278  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  *
21*294f5787Sas198278  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
277c478bd9Sstevel@tonic-gate  * All Rights Reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California.
337c478bd9Sstevel@tonic-gate  * All Rights Reserved.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Trivial file transfer protocol server.  A top level process runs in
447c478bd9Sstevel@tonic-gate  * an infinite loop fielding new TFTP requests.  A child process,
457c478bd9Sstevel@tonic-gate  * communicating via a pipe with the top level process, sends delayed
467c478bd9Sstevel@tonic-gate  * NAKs for those that we can't handle.  A new child process is created
477c478bd9Sstevel@tonic-gate  * to service each request that we can handle.  The top level process
487c478bd9Sstevel@tonic-gate  * exits after a period of time during which no new requests are
497c478bd9Sstevel@tonic-gate  * received.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/socket.h>
547c478bd9Sstevel@tonic-gate #include <sys/wait.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <sys/time.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <netinet/in.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
617c478bd9Sstevel@tonic-gate #include <dirent.h>
627c478bd9Sstevel@tonic-gate #include <signal.h>
637c478bd9Sstevel@tonic-gate #include <stdio.h>
647c478bd9Sstevel@tonic-gate #include <stdlib.h>
657c478bd9Sstevel@tonic-gate #include <unistd.h>
667c478bd9Sstevel@tonic-gate #include <errno.h>
677c478bd9Sstevel@tonic-gate #include <ctype.h>
687c478bd9Sstevel@tonic-gate #include <netdb.h>
697c478bd9Sstevel@tonic-gate #include <setjmp.h>
707c478bd9Sstevel@tonic-gate #include <syslog.h>
717c478bd9Sstevel@tonic-gate #include <sys/param.h>
727c478bd9Sstevel@tonic-gate #include <fcntl.h>
737c478bd9Sstevel@tonic-gate #include <pwd.h>
747c478bd9Sstevel@tonic-gate #include <string.h>
757c478bd9Sstevel@tonic-gate #include <priv_utils.h>
767c478bd9Sstevel@tonic-gate #include "tftpcommon.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	TIMEOUT		5
797c478bd9Sstevel@tonic-gate #define	DELAY_SECS	3
807c478bd9Sstevel@tonic-gate #define	DALLYSECS 60
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	SYSLOG_MSG(message) \
837c478bd9Sstevel@tonic-gate 	(syslog((((errno == ENETUNREACH) || (errno == EHOSTUNREACH) || \
847c478bd9Sstevel@tonic-gate 		(errno == ECONNREFUSED)) ? LOG_WARNING : LOG_ERR), message))
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int			rexmtval = TIMEOUT;
877c478bd9Sstevel@tonic-gate static int			maxtimeout = 5*TIMEOUT;
887c478bd9Sstevel@tonic-gate static int			securetftp;
897c478bd9Sstevel@tonic-gate static int			debug;
907c478bd9Sstevel@tonic-gate static int			disable_pnp;
917c478bd9Sstevel@tonic-gate static int			standalone;
927c478bd9Sstevel@tonic-gate static uid_t			uid_nobody = UID_NOBODY;
937c478bd9Sstevel@tonic-gate static uid_t			gid_nobody = GID_NOBODY;
947c478bd9Sstevel@tonic-gate static int			reqsock = -1;
957c478bd9Sstevel@tonic-gate 				/* file descriptor of request socket */
967c478bd9Sstevel@tonic-gate static socklen_t		fromlen;
977c478bd9Sstevel@tonic-gate static socklen_t		fromplen;
987c478bd9Sstevel@tonic-gate static struct sockaddr_storage	client;
997c478bd9Sstevel@tonic-gate static struct sockaddr_in6 	*sin6_ptr;
1007c478bd9Sstevel@tonic-gate static struct sockaddr_in	*sin_ptr;
1017c478bd9Sstevel@tonic-gate static struct sockaddr_in6	*from6_ptr;
1027c478bd9Sstevel@tonic-gate static struct sockaddr_in	*from_ptr;
1037c478bd9Sstevel@tonic-gate static int			addrfmly;
1047c478bd9Sstevel@tonic-gate static int			peer;
1057c478bd9Sstevel@tonic-gate static off_t			tsize;
1067c478bd9Sstevel@tonic-gate static tftpbuf			ackbuf;
1077c478bd9Sstevel@tonic-gate static struct sockaddr_storage	from;
1087c478bd9Sstevel@tonic-gate static boolean_t		tsize_set;
1097c478bd9Sstevel@tonic-gate static pid_t			child;
1107c478bd9Sstevel@tonic-gate 				/* pid of child handling delayed replys */
1117c478bd9Sstevel@tonic-gate static int			delay_fd [2];
1127c478bd9Sstevel@tonic-gate 				/* pipe for communicating with child */
1137c478bd9Sstevel@tonic-gate static FILE			*file;
1147c478bd9Sstevel@tonic-gate static char			*filename;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static union {
1177c478bd9Sstevel@tonic-gate 	struct tftphdr	hdr;
1187c478bd9Sstevel@tonic-gate 	char		data[SEGSIZE + 4];
1197c478bd9Sstevel@tonic-gate } buf;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static union {
1227c478bd9Sstevel@tonic-gate 	struct tftphdr	hdr;
1237c478bd9Sstevel@tonic-gate 	char		data[SEGSIZE];
1247c478bd9Sstevel@tonic-gate } oackbuf;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate struct	delay_info {
1277c478bd9Sstevel@tonic-gate 	long	timestamp;		/* time request received */
1287c478bd9Sstevel@tonic-gate 	int	ecode;			/* error code to return */
1297c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage from;	/* address of client */
1307c478bd9Sstevel@tonic-gate };
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate int	blocksize = SEGSIZE;	/* Number of data bytes in a DATA packet */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Default directory for unqualified names
1367c478bd9Sstevel@tonic-gate  * Used by TFTP boot procedures
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static char	*homedir = "/tftpboot";
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate struct formats {
1417c478bd9Sstevel@tonic-gate 	char	*f_mode;
1427c478bd9Sstevel@tonic-gate 	int	(*f_validate)(int);
1437c478bd9Sstevel@tonic-gate 	void	(*f_send)(struct formats *, int);
1447c478bd9Sstevel@tonic-gate 	void	(*f_recv)(struct formats *, int);
1457c478bd9Sstevel@tonic-gate 	int	f_convert;
1467c478bd9Sstevel@tonic-gate };
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static void	delayed_responder(void);
1497c478bd9Sstevel@tonic-gate static void	tftp(struct tftphdr *, int);
1507c478bd9Sstevel@tonic-gate static int	validate_filename(int);
1517c478bd9Sstevel@tonic-gate static void	tftpd_sendfile(struct formats *, int);
1527c478bd9Sstevel@tonic-gate static void	tftpd_recvfile(struct formats *, int);
1537c478bd9Sstevel@tonic-gate static void	nak(int);
1547c478bd9Sstevel@tonic-gate static char	*blksize_handler(int, char *, int *);
1557c478bd9Sstevel@tonic-gate static char	*timeout_handler(int, char *, int *);
1567c478bd9Sstevel@tonic-gate static char	*tsize_handler(int, char *, int *);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static struct formats formats[] = {
1597c478bd9Sstevel@tonic-gate 	{ "netascii",	validate_filename, tftpd_sendfile, tftpd_recvfile, 1 },
1607c478bd9Sstevel@tonic-gate 	{ "octet",	validate_filename, tftpd_sendfile, tftpd_recvfile, 0 },
1617c478bd9Sstevel@tonic-gate 	{ NULL }
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate struct options {
1657c478bd9Sstevel@tonic-gate 	char	*opt_name;
1667c478bd9Sstevel@tonic-gate 	char	*(*opt_handler)(int, char *, int *);
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static struct options options[] = {
1707c478bd9Sstevel@tonic-gate 	{ "blksize",	blksize_handler },
1717c478bd9Sstevel@tonic-gate 	{ "timeout",	timeout_handler },
1727c478bd9Sstevel@tonic-gate 	{ "tsize",	tsize_handler },
1737c478bd9Sstevel@tonic-gate 	{ NULL }
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static char		optbuf[MAX_OPTVAL_LEN];
1777c478bd9Sstevel@tonic-gate static int		timeout;
1787c478bd9Sstevel@tonic-gate static sigjmp_buf	timeoutbuf;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate int
1817c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	struct tftphdr *tp;
1847c478bd9Sstevel@tonic-gate 	int n;
1857c478bd9Sstevel@tonic-gate 	int c;
1867c478bd9Sstevel@tonic-gate 	struct	passwd *pwd;		/* for "nobody" entry */
1877c478bd9Sstevel@tonic-gate 	struct in_addr ipv4addr;
1887c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
1897c478bd9Sstevel@tonic-gate 	socklen_t addrlen;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	openlog("tftpd", LOG_PID, LOG_DAEMON);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	pwd = getpwnam("nobody");
1947c478bd9Sstevel@tonic-gate 	if (pwd != NULL) {
1957c478bd9Sstevel@tonic-gate 		uid_nobody = pwd->pw_uid;
1967c478bd9Sstevel@tonic-gate 		gid_nobody = pwd->pw_gid;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	(void) __init_daemon_priv(
2007c478bd9Sstevel@tonic-gate 		PU_LIMITPRIVS,
2017c478bd9Sstevel@tonic-gate 		uid_nobody, gid_nobody,
2027c478bd9Sstevel@tonic-gate 		PRIV_PROC_FORK, PRIV_PROC_CHROOT, NULL);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 *  Limit set is still "all."  Trim it down to just what we need:
2067c478bd9Sstevel@tonic-gate 	 *  fork and chroot.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET,
2097c478bd9Sstevel@tonic-gate 	    PRIV_ALLSETS, PRIV_PROC_FORK, PRIV_PROC_CHROOT, NULL);
2107c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, NULL);
2117c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_INHERITABLE, NULL);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dspS")) != EOF)
2147c478bd9Sstevel@tonic-gate 		switch (c) {
2157c478bd9Sstevel@tonic-gate 		case 'd':		/* enable debug */
2167c478bd9Sstevel@tonic-gate 			debug++;
2177c478bd9Sstevel@tonic-gate 			continue;
2187c478bd9Sstevel@tonic-gate 		case 's':		/* secure daemon */
2197c478bd9Sstevel@tonic-gate 			securetftp = 1;
2207c478bd9Sstevel@tonic-gate 			continue;
2217c478bd9Sstevel@tonic-gate 		case 'p':		/* disable name pnp mapping */
2227c478bd9Sstevel@tonic-gate 			disable_pnp = 1;
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 		case 'S':
2257c478bd9Sstevel@tonic-gate 			standalone = 1;
2267c478bd9Sstevel@tonic-gate 			continue;
2277c478bd9Sstevel@tonic-gate 		case '?':
2287c478bd9Sstevel@tonic-gate 		default:
2297c478bd9Sstevel@tonic-gate usage:
2307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2317c478bd9Sstevel@tonic-gate 			    "usage:  %s [-spd] [home-directory]\n", argv[0]);
2327c478bd9Sstevel@tonic-gate 			for (; optind < argc; optind++)
2337c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "bad argument %s",
2347c478bd9Sstevel@tonic-gate 				    argv[optind]);
2357c478bd9Sstevel@tonic-gate 			exit(1);
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (optind < argc)
2397c478bd9Sstevel@tonic-gate 		if (optind == argc - 1 && *argv [optind] == '/')
2407c478bd9Sstevel@tonic-gate 			homedir = argv [optind];
2417c478bd9Sstevel@tonic-gate 		else
2427c478bd9Sstevel@tonic-gate 			goto usage;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (pipe(delay_fd) < 0) {
2457c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "pipe (main): %m");
2467c478bd9Sstevel@tonic-gate 		exit(1);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, SIG_IGN); /* no zombies please */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (standalone) {
2527c478bd9Sstevel@tonic-gate 		socklen_t clientlen;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		sin6_ptr = (struct sockaddr_in6 *)&client;
2557c478bd9Sstevel@tonic-gate 		clientlen = sizeof (struct sockaddr_in6);
2567c478bd9Sstevel@tonic-gate 		reqsock = socket(AF_INET6, SOCK_DGRAM, 0);
2577c478bd9Sstevel@tonic-gate 		if (reqsock == -1) {
2587c478bd9Sstevel@tonic-gate 			perror("socket");
2597c478bd9Sstevel@tonic-gate 			exit(1);
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 		(void) memset(&client, 0, clientlen);
2627c478bd9Sstevel@tonic-gate 		sin6_ptr->sin6_family = AF_INET6;
2637c478bd9Sstevel@tonic-gate 		sin6_ptr->sin6_port = htons(IPPORT_TFTP);
2647c478bd9Sstevel@tonic-gate 		if (bind(reqsock, (struct sockaddr *)&client,
2657c478bd9Sstevel@tonic-gate 		    clientlen) == -1) {
2667c478bd9Sstevel@tonic-gate 			perror("bind");
2677c478bd9Sstevel@tonic-gate 			exit(1);
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		if (debug)
2707c478bd9Sstevel@tonic-gate 			(void) puts("running in standalone mode...");
2717c478bd9Sstevel@tonic-gate 	} else {
2727c478bd9Sstevel@tonic-gate 		/* request socket passed on fd 0 by inetd */
2737c478bd9Sstevel@tonic-gate 		reqsock = 0;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	if (debug) {
2767c478bd9Sstevel@tonic-gate 		int on = 1;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		(void) setsockopt(reqsock, SOL_SOCKET, SO_DEBUG,
2797c478bd9Sstevel@tonic-gate 		    (char *)&on, sizeof (on));
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	(void) chdir(homedir);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL);
2857c478bd9Sstevel@tonic-gate 	if ((child = fork()) < 0) {
2867c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "fork (main): %m");
2877c478bd9Sstevel@tonic-gate 		exit(1);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, NULL);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (child == 0) {
2927c478bd9Sstevel@tonic-gate 		(void) priv_set(PRIV_SET, PRIV_ALLSETS, NULL);
2937c478bd9Sstevel@tonic-gate 		delayed_responder();
2947c478bd9Sstevel@tonic-gate 	} /* child */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* close read side of pipe */
2977c478bd9Sstevel@tonic-gate 	(void) close(delay_fd[0]);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * Top level handling of incomming tftp requests.  Read a request
3027c478bd9Sstevel@tonic-gate 	 * and pass it off to be handled.  If request is valid, handling
3037c478bd9Sstevel@tonic-gate 	 * forks off and parent returns to this loop.  If no new requests
3047c478bd9Sstevel@tonic-gate 	 * are received for DALLYSECS, exit and return to inetd.
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	for (;;) {
3087c478bd9Sstevel@tonic-gate 		fd_set readfds;
3097c478bd9Sstevel@tonic-gate 		struct timeval dally;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		FD_ZERO(&readfds);
3127c478bd9Sstevel@tonic-gate 		FD_SET(reqsock, &readfds);
3137c478bd9Sstevel@tonic-gate 		dally.tv_sec = DALLYSECS;
3147c478bd9Sstevel@tonic-gate 		dally.tv_usec = 0;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		n = select(reqsock + 1, &readfds, NULL, NULL, &dally);
3177c478bd9Sstevel@tonic-gate 		if (n < 0) {
3187c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
3197c478bd9Sstevel@tonic-gate 				continue;
3207c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "select: %m");
3217c478bd9Sstevel@tonic-gate 			(void) kill(child, SIGKILL);
3227c478bd9Sstevel@tonic-gate 			exit(1);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		if (n == 0) {
3257c478bd9Sstevel@tonic-gate 			/* Select timed out.  Its time to die. */
3267c478bd9Sstevel@tonic-gate 			if (standalone)
3277c478bd9Sstevel@tonic-gate 				continue;
3287c478bd9Sstevel@tonic-gate 			else {
3297c478bd9Sstevel@tonic-gate 				(void) kill(child, SIGKILL);
3307c478bd9Sstevel@tonic-gate 				exit(0);
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		addrlen = sizeof (from);
3347c478bd9Sstevel@tonic-gate 		if (getsockname(reqsock, (struct sockaddr  *)&from,
3357c478bd9Sstevel@tonic-gate 		    &addrlen) < 0) {
3367c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "getsockname: %m");
3377c478bd9Sstevel@tonic-gate 			exit(1);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		switch (from.ss_family) {
3417c478bd9Sstevel@tonic-gate 		case AF_INET:
3427c478bd9Sstevel@tonic-gate 			fromlen = (socklen_t)sizeof (struct sockaddr_in);
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		case AF_INET6:
3457c478bd9Sstevel@tonic-gate 			fromlen = (socklen_t)sizeof (struct sockaddr_in6);
3467c478bd9Sstevel@tonic-gate 			break;
3477c478bd9Sstevel@tonic-gate 		default:
3487c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
3497c478bd9Sstevel@tonic-gate 			    "Unknown address Family on peer connection %d",
3507c478bd9Sstevel@tonic-gate 			    from.ss_family);
3517c478bd9Sstevel@tonic-gate 			exit(1);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		n = recvfrom(reqsock, &buf, sizeof (buf), 0,
3557c478bd9Sstevel@tonic-gate 			(struct sockaddr *)&from, &fromlen);
3567c478bd9Sstevel@tonic-gate 		if (n < 0) {
3577c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
3587c478bd9Sstevel@tonic-gate 				continue;
3597c478bd9Sstevel@tonic-gate 			if (standalone)
3607c478bd9Sstevel@tonic-gate 				perror("recvfrom");
3617c478bd9Sstevel@tonic-gate 			else
3627c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "recvfrom: %m");
3637c478bd9Sstevel@tonic-gate 			(void) kill(child, SIGKILL);
3647c478bd9Sstevel@tonic-gate 			exit(1);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		(void) alarm(0);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		switch (from.ss_family) {
3707c478bd9Sstevel@tonic-gate 		case AF_INET:
3717c478bd9Sstevel@tonic-gate 			addrfmly = AF_INET;
3727c478bd9Sstevel@tonic-gate 			fromplen = sizeof (struct sockaddr_in);
3737c478bd9Sstevel@tonic-gate 			sin_ptr = (struct sockaddr_in *)&client;
3747c478bd9Sstevel@tonic-gate 			(void) memset(&client, 0, fromplen);
3757c478bd9Sstevel@tonic-gate 			sin_ptr->sin_family = AF_INET;
3767c478bd9Sstevel@tonic-gate 			break;
3777c478bd9Sstevel@tonic-gate 		case AF_INET6:
3787c478bd9Sstevel@tonic-gate 			addrfmly = AF_INET6;
3797c478bd9Sstevel@tonic-gate 			fromplen = sizeof (struct sockaddr_in6);
3807c478bd9Sstevel@tonic-gate 			sin6_ptr = (struct sockaddr_in6 *)&client;
3817c478bd9Sstevel@tonic-gate 			(void) memset(&client, 0, fromplen);
3827c478bd9Sstevel@tonic-gate 			sin6_ptr->sin6_family = AF_INET6;
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		default:
3857c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
3867c478bd9Sstevel@tonic-gate 			    "Unknown address Family on peer connection");
3877c478bd9Sstevel@tonic-gate 			exit(1);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		peer = socket(addrfmly, SOCK_DGRAM, 0);
3907c478bd9Sstevel@tonic-gate 		if (peer < 0) {
3917c478bd9Sstevel@tonic-gate 			if (standalone)
3927c478bd9Sstevel@tonic-gate 				perror("socket (main)");
3937c478bd9Sstevel@tonic-gate 			else
3947c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "socket (main): %m");
3957c478bd9Sstevel@tonic-gate 			(void) kill(child, SIGKILL);
3967c478bd9Sstevel@tonic-gate 			exit(1);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		if (debug) {
3997c478bd9Sstevel@tonic-gate 			int on = 1;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 			(void) setsockopt(peer, SOL_SOCKET, SO_DEBUG,
4027c478bd9Sstevel@tonic-gate 			    (char *)&on, sizeof (on));
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		if (bind(peer, (struct sockaddr *)&client, fromplen) < 0) {
4067c478bd9Sstevel@tonic-gate 			if (standalone)
4077c478bd9Sstevel@tonic-gate 				perror("bind (main)");
4087c478bd9Sstevel@tonic-gate 			else
4097c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "bind (main): %m");
4107c478bd9Sstevel@tonic-gate 			(void) kill(child, SIGKILL);
4117c478bd9Sstevel@tonic-gate 			exit(1);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		if (standalone && debug) {
4147c478bd9Sstevel@tonic-gate 			sin6_ptr = (struct sockaddr_in6 *)&client;
4157c478bd9Sstevel@tonic-gate 			from6_ptr = (struct sockaddr_in6 *)&from;
4167c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&from6_ptr->sin6_addr)) {
4177c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&from6_ptr->sin6_addr,
4187c478bd9Sstevel@tonic-gate 				    &ipv4addr);
4197c478bd9Sstevel@tonic-gate 				(void) inet_ntop(AF_INET, &ipv4addr, abuf,
4207c478bd9Sstevel@tonic-gate 				    sizeof (abuf));
4217c478bd9Sstevel@tonic-gate 			} else {
4227c478bd9Sstevel@tonic-gate 				(void) inet_ntop(AF_INET6,
4237c478bd9Sstevel@tonic-gate 				    &from6_ptr->sin6_addr, abuf,
4247c478bd9Sstevel@tonic-gate 				    sizeof (abuf));
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 			/* get local port */
4277c478bd9Sstevel@tonic-gate 			if (getsockname(peer, (struct sockaddr *)&client,
4287c478bd9Sstevel@tonic-gate 			    &fromplen) < 0)
4297c478bd9Sstevel@tonic-gate 				perror("getsockname (main)");
4307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4317c478bd9Sstevel@tonic-gate 			    "request from %s port %d; local port %d\n",
4327c478bd9Sstevel@tonic-gate 			    abuf, from6_ptr->sin6_port, sin6_ptr->sin6_port);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		tp = &buf.hdr;
4357c478bd9Sstevel@tonic-gate 		tp->th_opcode = ntohs((ushort_t)tp->th_opcode);
4367c478bd9Sstevel@tonic-gate 		if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
4377c478bd9Sstevel@tonic-gate 			tftp(tp, n);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		(void) close(peer);
4407c478bd9Sstevel@tonic-gate 		(void) fclose(file);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
4447c478bd9Sstevel@tonic-gate 	return (0);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static void
4487c478bd9Sstevel@tonic-gate delayed_responder(void)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	struct delay_info dinfo;
4517c478bd9Sstevel@tonic-gate 	long now;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* we don't use the descriptors passed in to the parent */
4547c478bd9Sstevel@tonic-gate 	(void) close(0);
4557c478bd9Sstevel@tonic-gate 	(void) close(1);
4567c478bd9Sstevel@tonic-gate 	if (standalone)
4577c478bd9Sstevel@tonic-gate 		(void) close(reqsock);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/* close write side of pipe */
4607c478bd9Sstevel@tonic-gate 	(void) close(delay_fd[1]);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	for (;;) {
4637c478bd9Sstevel@tonic-gate 		int n;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		if ((n = read(delay_fd[0], &dinfo,
4667c478bd9Sstevel@tonic-gate 		    sizeof (dinfo))) != sizeof (dinfo)) {
4677c478bd9Sstevel@tonic-gate 			if (n < 0) {
4687c478bd9Sstevel@tonic-gate 				if (errno == EINTR)
4697c478bd9Sstevel@tonic-gate 					continue;
4707c478bd9Sstevel@tonic-gate 				if (standalone)
4717c478bd9Sstevel@tonic-gate 					perror("read from pipe "
4727c478bd9Sstevel@tonic-gate 					    "(delayed responder)");
4737c478bd9Sstevel@tonic-gate 				else
4747c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "read from pipe: %m");
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 			exit(1);
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		switch (dinfo.from.ss_family) {
4797c478bd9Sstevel@tonic-gate 		case AF_INET:
4807c478bd9Sstevel@tonic-gate 			addrfmly = AF_INET;
4817c478bd9Sstevel@tonic-gate 			fromplen = sizeof (struct sockaddr_in);
4827c478bd9Sstevel@tonic-gate 			sin_ptr = (struct sockaddr_in *)&client;
4837c478bd9Sstevel@tonic-gate 			(void) memset(&client, 0, fromplen);
4847c478bd9Sstevel@tonic-gate 			sin_ptr->sin_family = AF_INET;
4857c478bd9Sstevel@tonic-gate 			break;
4867c478bd9Sstevel@tonic-gate 		case AF_INET6:
4877c478bd9Sstevel@tonic-gate 			addrfmly = AF_INET6;
4887c478bd9Sstevel@tonic-gate 			fromplen = sizeof (struct sockaddr_in6);
4897c478bd9Sstevel@tonic-gate 			sin6_ptr = (struct sockaddr_in6 *)&client;
4907c478bd9Sstevel@tonic-gate 			(void) memset(&client, 0, fromplen);
4917c478bd9Sstevel@tonic-gate 			sin6_ptr->sin6_family = AF_INET6;
4927c478bd9Sstevel@tonic-gate 			break;
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 		peer = socket(addrfmly, SOCK_DGRAM, 0);
4957c478bd9Sstevel@tonic-gate 		if (peer == -1) {
4967c478bd9Sstevel@tonic-gate 			if (standalone)
4977c478bd9Sstevel@tonic-gate 				perror("socket (delayed responder)");
4987c478bd9Sstevel@tonic-gate 			else
4997c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "socket (delay): %m");
5007c478bd9Sstevel@tonic-gate 			exit(1);
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 		if (debug) {
5037c478bd9Sstevel@tonic-gate 			int on = 1;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 			(void) setsockopt(peer, SOL_SOCKET, SO_DEBUG,
5067c478bd9Sstevel@tonic-gate 			    (char *)&on, sizeof (on));
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		if (bind(peer, (struct sockaddr *)&client, fromplen) < 0) {
5107c478bd9Sstevel@tonic-gate 			if (standalone)
5117c478bd9Sstevel@tonic-gate 				perror("bind (delayed responder)");
5127c478bd9Sstevel@tonic-gate 			else
5137c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "bind (delay): %m");
5147c478bd9Sstevel@tonic-gate 			exit(1);
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		if (client.ss_family == AF_INET) {
5177c478bd9Sstevel@tonic-gate 			from_ptr = (struct sockaddr_in *)&dinfo.from;
5187c478bd9Sstevel@tonic-gate 			from_ptr->sin_family = AF_INET;
5197c478bd9Sstevel@tonic-gate 		} else {
5207c478bd9Sstevel@tonic-gate 			from6_ptr = (struct sockaddr_in6 *)&dinfo.from;
5217c478bd9Sstevel@tonic-gate 			from6_ptr->sin6_family = AF_INET6;
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 		/*
5247c478bd9Sstevel@tonic-gate 		 * Since a request hasn't been received from the client
5257c478bd9Sstevel@tonic-gate 		 * before the delayed responder process is forked, the
5267c478bd9Sstevel@tonic-gate 		 * from variable is uninitialized.  So set it to contain
5277c478bd9Sstevel@tonic-gate 		 * the client address.
5287c478bd9Sstevel@tonic-gate 		 */
5297c478bd9Sstevel@tonic-gate 		from = dinfo.from;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		/*
5327c478bd9Sstevel@tonic-gate 		 * only sleep if DELAY_SECS has not elapsed since
5337c478bd9Sstevel@tonic-gate 		 * original request was received.  Ensure that `now'
5347c478bd9Sstevel@tonic-gate 		 * is not earlier than `dinfo.timestamp'
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		now = time(0);
5377c478bd9Sstevel@tonic-gate 		if ((uint_t)(now - dinfo.timestamp) < DELAY_SECS)
5387c478bd9Sstevel@tonic-gate 			(void) sleep(DELAY_SECS - (now - dinfo.timestamp));
5397c478bd9Sstevel@tonic-gate 		nak(dinfo.ecode);
5407c478bd9Sstevel@tonic-gate 		(void) close(peer);
5417c478bd9Sstevel@tonic-gate 	} /* for */
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * Handle the Blocksize option.
5487c478bd9Sstevel@tonic-gate  * Return the blksize option value string to include in the OACK reply.
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5517c478bd9Sstevel@tonic-gate static char *
5527c478bd9Sstevel@tonic-gate blksize_handler(int opcode, char *optval, int *errcode)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	char *endp;
5557c478bd9Sstevel@tonic-gate 	int value;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	*errcode = -1;
5587c478bd9Sstevel@tonic-gate 	errno = 0;
5597c478bd9Sstevel@tonic-gate 	value = (int)strtol(optval, &endp, 10);
5607c478bd9Sstevel@tonic-gate 	if (errno != 0 || value < MIN_BLKSIZE || *endp != '\0')
5617c478bd9Sstevel@tonic-gate 		return (NULL);
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * As the blksize value in the OACK reply can be less than the value
5647c478bd9Sstevel@tonic-gate 	 * requested, to support broken clients if the value requested is larger
5657c478bd9Sstevel@tonic-gate 	 * than allowed in the RFC, reply with the maximum value permitted.
5667c478bd9Sstevel@tonic-gate 	 */
5677c478bd9Sstevel@tonic-gate 	if (value > MAX_BLKSIZE)
5687c478bd9Sstevel@tonic-gate 		value = MAX_BLKSIZE;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	blocksize = value;
5717c478bd9Sstevel@tonic-gate 	(void) snprintf(optbuf, sizeof (optbuf), "%d", blocksize);
5727c478bd9Sstevel@tonic-gate 	return (optbuf);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * Handle the Timeout Interval option.
5777c478bd9Sstevel@tonic-gate  * Return the timeout option value string to include in the OACK reply.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5807c478bd9Sstevel@tonic-gate static char *
5817c478bd9Sstevel@tonic-gate timeout_handler(int opcode, char *optval, int *errcode)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	char *endp;
5847c478bd9Sstevel@tonic-gate 	int value;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	*errcode = -1;
5877c478bd9Sstevel@tonic-gate 	errno = 0;
5887c478bd9Sstevel@tonic-gate 	value = (int)strtol(optval, &endp, 10);
5897c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0')
5907c478bd9Sstevel@tonic-gate 		return (NULL);
5917c478bd9Sstevel@tonic-gate 	/*
5927c478bd9Sstevel@tonic-gate 	 * The timeout value in the OACK reply must match the value specified
5937c478bd9Sstevel@tonic-gate 	 * by the client, so if an invalid timeout is requested don't include
5947c478bd9Sstevel@tonic-gate 	 * the timeout option in the OACK reply.
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (value < MIN_TIMEOUT || value > MAX_TIMEOUT)
5977c478bd9Sstevel@tonic-gate 		return (NULL);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	rexmtval = value;
6007c478bd9Sstevel@tonic-gate 	maxtimeout = 5 * rexmtval;
6017c478bd9Sstevel@tonic-gate 	(void) snprintf(optbuf, sizeof (optbuf), "%d", rexmtval);
6027c478bd9Sstevel@tonic-gate 	return (optbuf);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Handle the Transfer Size option.
6077c478bd9Sstevel@tonic-gate  * Return the tsize option value string to include in the OACK reply.
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate static char *
6107c478bd9Sstevel@tonic-gate tsize_handler(int opcode, char *optval, int *errcode)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	char *endp;
6137c478bd9Sstevel@tonic-gate 	longlong_t value;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	*errcode = -1;
6167c478bd9Sstevel@tonic-gate 	errno = 0;
6177c478bd9Sstevel@tonic-gate 	value = strtoll(optval, &endp, 10);
6187c478bd9Sstevel@tonic-gate 	if (errno != 0 || value < 0 || *endp != '\0')
6197c478bd9Sstevel@tonic-gate 		return (NULL);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (opcode == RRQ) {
6227c478bd9Sstevel@tonic-gate 		if (tsize_set == B_FALSE)
6237c478bd9Sstevel@tonic-gate 			return (NULL);
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * The tsize value should be 0 for a read request, but to
6267c478bd9Sstevel@tonic-gate 		 * support broken clients we don't check that it is.
6277c478bd9Sstevel@tonic-gate 		 */
6287c478bd9Sstevel@tonic-gate 	} else {
6297c478bd9Sstevel@tonic-gate #if _FILE_OFFSET_BITS == 32
6307c478bd9Sstevel@tonic-gate 		if (value > MAXOFF_T) {
6317c478bd9Sstevel@tonic-gate 			*errcode = ENOSPACE;
6327c478bd9Sstevel@tonic-gate 			return (NULL);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate #endif
6357c478bd9Sstevel@tonic-gate 		tsize = value;
6367c478bd9Sstevel@tonic-gate 		tsize_set = B_TRUE;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	(void) snprintf(optbuf, sizeof (optbuf), OFF_T_FMT, tsize);
6397c478bd9Sstevel@tonic-gate 	return (optbuf);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * Process any options included by the client in the request packet.
6447c478bd9Sstevel@tonic-gate  * Return the size of the OACK reply packet built or 0 for no OACK reply.
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate static int
6477c478bd9Sstevel@tonic-gate process_options(int opcode, char *opts, char *endopts)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	char *cp, *optname, *optval, *ostr, *oackend;
6507c478bd9Sstevel@tonic-gate 	struct tftphdr *oackp;
6517c478bd9Sstevel@tonic-gate 	int i, errcode;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * To continue to interoperate with broken TFTP clients, ignore
6557c478bd9Sstevel@tonic-gate 	 * null padding appended to requests which don't include options.
6567c478bd9Sstevel@tonic-gate 	 */
6577c478bd9Sstevel@tonic-gate 	cp = opts;
6587c478bd9Sstevel@tonic-gate 	while ((cp < endopts) && (*cp == '\0'))
6597c478bd9Sstevel@tonic-gate 		cp++;
6607c478bd9Sstevel@tonic-gate 	if (cp == endopts)
6617c478bd9Sstevel@tonic-gate 		return (0);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * Construct an Option ACKnowledgement packet if any requested option
6657c478bd9Sstevel@tonic-gate 	 * is recognized.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	oackp = &oackbuf.hdr;
6687c478bd9Sstevel@tonic-gate 	oackend = oackbuf.data + sizeof (oackbuf.data);
6697c478bd9Sstevel@tonic-gate 	oackp->th_opcode = htons((ushort_t)OACK);
6707c478bd9Sstevel@tonic-gate 	cp = (char *)&oackp->th_stuff;
6717c478bd9Sstevel@tonic-gate 	while (opts < endopts) {
6727c478bd9Sstevel@tonic-gate 		optname = opts;
6737c478bd9Sstevel@tonic-gate 		if ((optval = next_field(optname, endopts)) == NULL) {
6747c478bd9Sstevel@tonic-gate 			nak(EOPTNEG);
6757c478bd9Sstevel@tonic-gate 			exit(1);
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		if ((opts = next_field(optval, endopts)) == NULL) {
6787c478bd9Sstevel@tonic-gate 			nak(EOPTNEG);
6797c478bd9Sstevel@tonic-gate 			exit(1);
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 		for (i = 0; options[i].opt_name != NULL; i++) {
6827c478bd9Sstevel@tonic-gate 			if (strcasecmp(optname, options[i].opt_name) == 0)
6837c478bd9Sstevel@tonic-gate 				break;
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 		if (options[i].opt_name != NULL) {
6867c478bd9Sstevel@tonic-gate 			ostr = options[i].opt_handler(opcode, optval, &errcode);
6877c478bd9Sstevel@tonic-gate 			if (ostr != NULL) {
6887c478bd9Sstevel@tonic-gate 				cp += strlcpy(cp, options[i].opt_name,
6897c478bd9Sstevel@tonic-gate 				    oackend - cp) + 1;
6907c478bd9Sstevel@tonic-gate 				if (cp <= oackend)
6917c478bd9Sstevel@tonic-gate 					cp += strlcpy(cp, ostr, oackend - cp)
6927c478bd9Sstevel@tonic-gate 					    + 1;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 				if (cp > oackend) {
6957c478bd9Sstevel@tonic-gate 					nak(EOPTNEG);
6967c478bd9Sstevel@tonic-gate 					exit(1);
6977c478bd9Sstevel@tonic-gate 				}
6987c478bd9Sstevel@tonic-gate 			} else if (errcode >= 0) {
6997c478bd9Sstevel@tonic-gate 				nak(errcode);
7007c478bd9Sstevel@tonic-gate 				exit(1);
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 	if (cp != (char *)&oackp->th_stuff)
7057c478bd9Sstevel@tonic-gate 		return (cp - oackbuf.data);
7067c478bd9Sstevel@tonic-gate 	return (0);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * Handle access errors caused by client requests.
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate static void
7147c478bd9Sstevel@tonic-gate delay_exit(int ecode)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	struct delay_info dinfo;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * The most likely cause of an error here is that
7207c478bd9Sstevel@tonic-gate 	 * someone has broadcast an RRQ packet because s/he's
7217c478bd9Sstevel@tonic-gate 	 * trying to boot and doesn't know who the server is.
7227c478bd9Sstevel@tonic-gate 	 * Rather then sending an ERROR packet immediately, we
7237c478bd9Sstevel@tonic-gate 	 * wait a while so that the real server has a better chance
7247c478bd9Sstevel@tonic-gate 	 * of getting through (in case client has lousy Ethernet
7257c478bd9Sstevel@tonic-gate 	 * interface).  We write to a child that handles delayed
7267c478bd9Sstevel@tonic-gate 	 * ERROR packets to avoid delaying service to new
7277c478bd9Sstevel@tonic-gate 	 * requests.  Of course, we would rather just not answer
7287c478bd9Sstevel@tonic-gate 	 * RRQ packets that are broadcasted, but there's no way
7297c478bd9Sstevel@tonic-gate 	 * for a user process to determine this.
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	dinfo.timestamp = time(0);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * If running in secure mode, we map all errors to EACCESS
7367c478bd9Sstevel@tonic-gate 	 * so that the client gets no information about which files
7377c478bd9Sstevel@tonic-gate 	 * or directories exist.
7387c478bd9Sstevel@tonic-gate 	 */
7397c478bd9Sstevel@tonic-gate 	if (securetftp)
7407c478bd9Sstevel@tonic-gate 		dinfo.ecode = EACCESS;
7417c478bd9Sstevel@tonic-gate 	else
7427c478bd9Sstevel@tonic-gate 		dinfo.ecode = ecode;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	dinfo.from = from;
7457c478bd9Sstevel@tonic-gate 	if (write(delay_fd[1], &dinfo, sizeof (dinfo)) !=
7467c478bd9Sstevel@tonic-gate 	    sizeof (dinfo)) {
7477c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "delayed write failed.");
7487c478bd9Sstevel@tonic-gate 		(void) kill(child, SIGKILL);
7497c478bd9Sstevel@tonic-gate 		exit(1);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 	exit(0);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * Handle initial connection protocol.
7567c478bd9Sstevel@tonic-gate  */
7577c478bd9Sstevel@tonic-gate static void
7587c478bd9Sstevel@tonic-gate tftp(struct tftphdr *tp, int size)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	char *cp;
7617c478bd9Sstevel@tonic-gate 	int readmode, ecode;
7627c478bd9Sstevel@tonic-gate 	struct formats *pf;
7637c478bd9Sstevel@tonic-gate 	char *mode;
7647c478bd9Sstevel@tonic-gate 	int fd;
7657c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
7667c478bd9Sstevel@tonic-gate 	int oacklen;
7677c478bd9Sstevel@tonic-gate 	struct stat statb;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	readmode = (tp->th_opcode == RRQ);
7707c478bd9Sstevel@tonic-gate 	filename = (char *)&tp->th_stuff;
7717c478bd9Sstevel@tonic-gate 	mode = next_field(filename, &buf.data[size]);
7727c478bd9Sstevel@tonic-gate 	cp = (mode != NULL) ? next_field(mode, &buf.data[size]) : NULL;
7737c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
7747c478bd9Sstevel@tonic-gate 		nak(EBADOP);
7757c478bd9Sstevel@tonic-gate 		exit(1);
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 	if (debug && standalone) {
7787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s for %s %s ",
7797c478bd9Sstevel@tonic-gate 		    readmode ? "RRQ" : "WRQ", filename, mode);
7807c478bd9Sstevel@tonic-gate 		print_options(stderr, cp, size + buf.data - cp);
7817c478bd9Sstevel@tonic-gate 		(void) putc('\n', stderr);
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 	for (pf = formats; pf->f_mode != NULL; pf++)
7847c478bd9Sstevel@tonic-gate 		if (strcasecmp(pf->f_mode, mode) == 0)
7857c478bd9Sstevel@tonic-gate 			break;
7867c478bd9Sstevel@tonic-gate 	if (pf->f_mode == NULL) {
7877c478bd9Sstevel@tonic-gate 		nak(EBADOP);
7887c478bd9Sstevel@tonic-gate 		exit(1);
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/*
7927c478bd9Sstevel@tonic-gate 	 * XXX fork a new process to handle this request before
7937c478bd9Sstevel@tonic-gate 	 * chroot(), otherwise the parent won't be able to create a
7947c478bd9Sstevel@tonic-gate 	 * new socket as that requires library access to system files
7957c478bd9Sstevel@tonic-gate 	 * and devices.
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, PRIV_PROC_FORK, NULL);
7987c478bd9Sstevel@tonic-gate 	switch (fork()) {
7997c478bd9Sstevel@tonic-gate 	case -1:
8007c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "fork (tftp): %m");
8017c478bd9Sstevel@tonic-gate 		(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, NULL);
8027c478bd9Sstevel@tonic-gate 		return;
8037c478bd9Sstevel@tonic-gate 	case 0:
8047c478bd9Sstevel@tonic-gate 		(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, NULL);
8057c478bd9Sstevel@tonic-gate 		break;
8067c478bd9Sstevel@tonic-gate 	default:
8077c478bd9Sstevel@tonic-gate 		(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, NULL);
8087c478bd9Sstevel@tonic-gate 		return;
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * Try to see if we can access the file.  The access can still
8137c478bd9Sstevel@tonic-gate 	 * fail later if we are running in secure mode because of
8147c478bd9Sstevel@tonic-gate 	 * the chroot() call.  We only want to execute the chroot()  once.
8157c478bd9Sstevel@tonic-gate 	 */
8167c478bd9Sstevel@tonic-gate 	if (securetftp && firsttime) {
8177c478bd9Sstevel@tonic-gate 		(void) priv_set(
8187c478bd9Sstevel@tonic-gate 		    PRIV_SET, PRIV_EFFECTIVE, PRIV_PROC_CHROOT, NULL);
8197c478bd9Sstevel@tonic-gate 		if (chroot(homedir) == -1) {
8207c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
8217c478bd9Sstevel@tonic-gate 			    "tftpd: cannot chroot to directory %s: %m\n",
8227c478bd9Sstevel@tonic-gate 			    homedir);
8237c478bd9Sstevel@tonic-gate 			delay_exit(EACCESS);
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 		else
8267c478bd9Sstevel@tonic-gate 		{
8277c478bd9Sstevel@tonic-gate 			firsttime = B_FALSE;
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 		(void) priv_set(PRIV_SET, PRIV_EFFECTIVE, NULL);
8307c478bd9Sstevel@tonic-gate 		(void) chdir("/");  /* cd to  new root */
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_SET, PRIV_ALLSETS, NULL);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	ecode = (*pf->f_validate)(tp->th_opcode);
8357c478bd9Sstevel@tonic-gate 	if (ecode != 0)
8367c478bd9Sstevel@tonic-gate 		delay_exit(ecode);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/* we don't use the descriptors passed in to the parent */
8397c478bd9Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
8407c478bd9Sstevel@tonic-gate 	(void) close(STDOUT_FILENO);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * Try to open file as low-priv setuid/setgid.  Note that
8447c478bd9Sstevel@tonic-gate 	 * a chroot() has already been done.
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 	fd = open(filename,
8477c478bd9Sstevel@tonic-gate 	    (readmode ? O_RDONLY : (O_WRONLY|O_TRUNC)) | O_NONBLOCK);
8487c478bd9Sstevel@tonic-gate 	if ((fd < 0) || (fstat(fd, &statb) < 0))
8497c478bd9Sstevel@tonic-gate 		delay_exit((errno == ENOENT) ? ENOTFOUND : EACCESS);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (((statb.st_mode & ((readmode) ? S_IROTH : S_IWOTH)) == 0) ||
8527c478bd9Sstevel@tonic-gate 	    ((statb.st_mode & S_IFMT) != S_IFREG))
8537c478bd9Sstevel@tonic-gate 		delay_exit(EACCESS);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	file = fdopen(fd, readmode ? "r" : "w");
8567c478bd9Sstevel@tonic-gate 	if (file == NULL)
8577c478bd9Sstevel@tonic-gate 		delay_exit(errno + 100);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/* Don't know the size of transfers which involve conversion */
8607c478bd9Sstevel@tonic-gate 	tsize_set = (readmode && (pf->f_convert == 0));
8617c478bd9Sstevel@tonic-gate 	if (tsize_set)
8627c478bd9Sstevel@tonic-gate 		tsize = statb.st_size;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/* Deal with any options sent by the client */
8657c478bd9Sstevel@tonic-gate 	oacklen = process_options(tp->th_opcode, cp, buf.data + size);
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (tp->th_opcode == WRQ)
8687c478bd9Sstevel@tonic-gate 		(*pf->f_recv)(pf, oacklen);
8697c478bd9Sstevel@tonic-gate 	else
8707c478bd9Sstevel@tonic-gate 		(*pf->f_send)(pf, oacklen);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	exit(0);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate  *	Maybe map filename into another one.
8777c478bd9Sstevel@tonic-gate  *
8787c478bd9Sstevel@tonic-gate  *	For PNP, we get TFTP boot requests for filenames like
8797c478bd9Sstevel@tonic-gate  *	<Unknown Hex IP Addr>.<Architecture Name>.   We must
8807c478bd9Sstevel@tonic-gate  *	map these to 'pnp.<Architecture Name>'.  Note that
8817c478bd9Sstevel@tonic-gate  *	uppercase is mapped to lowercase in the architecture names.
8827c478bd9Sstevel@tonic-gate  *
8837c478bd9Sstevel@tonic-gate  *	For names <Hex IP Addr> there are two cases.  First,
8847c478bd9Sstevel@tonic-gate  *	it may be a buggy prom that omits the architecture code.
8857c478bd9Sstevel@tonic-gate  *	So first check if <Hex IP Addr>.<arch> is on the filesystem.
8867c478bd9Sstevel@tonic-gate  *	Second, this is how most Sun3s work; assume <arch> is sun3.
8877c478bd9Sstevel@tonic-gate  */
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate static char *
8907c478bd9Sstevel@tonic-gate pnp_check(char *origname)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate 	static char buf [MAXNAMLEN + 1];
8937c478bd9Sstevel@tonic-gate 	char *arch, *s, *bufend;
8947c478bd9Sstevel@tonic-gate 	in_addr_t ipaddr;
8957c478bd9Sstevel@tonic-gate 	int len = (origname ? strlen(origname) : 0);
8967c478bd9Sstevel@tonic-gate 	DIR *dir;
8977c478bd9Sstevel@tonic-gate 	struct dirent *dp;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (securetftp || disable_pnp || len < 8 || len > 14)
9007c478bd9Sstevel@tonic-gate 		return (NULL);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9037c478bd9Sstevel@tonic-gate 	 * XXX see if this cable allows pnp; if not, return NULL
9047c478bd9Sstevel@tonic-gate 	 * Requires YP support for determining this!
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	ipaddr = htonl(strtol(origname, &arch, 16));
9087c478bd9Sstevel@tonic-gate 	if ((arch == NULL) || (len > 8 && *arch != '.'))
9097c478bd9Sstevel@tonic-gate 		return (NULL);
9107c478bd9Sstevel@tonic-gate 	if (len == 8)
9117c478bd9Sstevel@tonic-gate 		arch = "SUN3";
9127c478bd9Sstevel@tonic-gate 	else
9137c478bd9Sstevel@tonic-gate 		arch++;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/*
9167c478bd9Sstevel@tonic-gate 	 * Allow <Hex IP Addr>* filename request to to be
9177c478bd9Sstevel@tonic-gate 	 * satisfied by <Hex IP Addr><Any Suffix> rather
9187c478bd9Sstevel@tonic-gate 	 * than enforcing this to be Sun3 systems.  Also serves
9197c478bd9Sstevel@tonic-gate 	 * to make case of suffix a don't-care.
9207c478bd9Sstevel@tonic-gate 	 */
9217c478bd9Sstevel@tonic-gate 	if ((dir = opendir(homedir)) == NULL)
9227c478bd9Sstevel@tonic-gate 		return (NULL);
9237c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dir)) != NULL) {
9247c478bd9Sstevel@tonic-gate 		if (strncmp(origname, dp->d_name, 8) == 0) {
9257c478bd9Sstevel@tonic-gate 			(void) strlcpy(buf, dp->d_name, sizeof (buf));
9267c478bd9Sstevel@tonic-gate 			(void) closedir(dir);
9277c478bd9Sstevel@tonic-gate 			return (buf);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 	(void) closedir(dir);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * XXX maybe call YP master for most current data iff
9347c478bd9Sstevel@tonic-gate 	 * pnp is enabled.
9357c478bd9Sstevel@tonic-gate 	 */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * only do mapping PNP boot file name for machines that
9397c478bd9Sstevel@tonic-gate 	 * are not in the hosts database.
9407c478bd9Sstevel@tonic-gate 	 */
9417c478bd9Sstevel@tonic-gate 	if (gethostbyaddr((char *)&ipaddr, sizeof (ipaddr), AF_INET) != NULL)
9427c478bd9Sstevel@tonic-gate 		return (NULL);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	s = buf + strlcpy(buf, "pnp.", sizeof (buf));
9457c478bd9Sstevel@tonic-gate 	bufend = &buf[sizeof (buf) - 1];
9467c478bd9Sstevel@tonic-gate 	while ((*arch != '\0') && (s < bufend))
9477c478bd9Sstevel@tonic-gate 		*s++ = tolower (*arch++);
9487c478bd9Sstevel@tonic-gate 	*s = '\0';
9497c478bd9Sstevel@tonic-gate 	return (buf);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate  * Try to validate filename. If the filename doesn't exist try PNP mapping.
9557c478bd9Sstevel@tonic-gate  */
9567c478bd9Sstevel@tonic-gate static int
9577c478bd9Sstevel@tonic-gate validate_filename(int mode)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	struct stat stbuf;
9607c478bd9Sstevel@tonic-gate 	char *origfile;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (stat(filename, &stbuf) < 0) {
9637c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
9647c478bd9Sstevel@tonic-gate 			return (EACCESS);
9657c478bd9Sstevel@tonic-gate 		if (mode == WRQ)
9667c478bd9Sstevel@tonic-gate 			return (ENOTFOUND);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		/* try to map requested filename into a pnp filename */
9697c478bd9Sstevel@tonic-gate 		origfile = filename;
9707c478bd9Sstevel@tonic-gate 		filename = pnp_check(origfile);
9717c478bd9Sstevel@tonic-gate 		if (filename == NULL)
9727c478bd9Sstevel@tonic-gate 			return (ENOTFOUND);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		if (stat(filename, &stbuf) < 0)
9757c478bd9Sstevel@tonic-gate 			return (errno == ENOENT ? ENOTFOUND : EACCESS);
9767c478bd9Sstevel@tonic-gate 		syslog(LOG_NOTICE, "%s -> %s\n", origfile, filename);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	return (0);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /* ARGSUSED */
9837c478bd9Sstevel@tonic-gate static void
9847c478bd9Sstevel@tonic-gate timer(int signum)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	timeout += rexmtval;
9877c478bd9Sstevel@tonic-gate 	if (timeout >= maxtimeout)
9887c478bd9Sstevel@tonic-gate 		exit(1);
9897c478bd9Sstevel@tonic-gate 	siglongjmp(timeoutbuf, 1);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * Send the requested file.
9947c478bd9Sstevel@tonic-gate  */
9957c478bd9Sstevel@tonic-gate static void
9967c478bd9Sstevel@tonic-gate tftpd_sendfile(struct formats *pf, int oacklen)
9977c478bd9Sstevel@tonic-gate {
9987c478bd9Sstevel@tonic-gate 	struct tftphdr *dp;
999*294f5787Sas198278 	volatile ushort_t block = 1;
10007c478bd9Sstevel@tonic-gate 	int size, n, serrno;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (oacklen != 0) {
10037c478bd9Sstevel@tonic-gate 		(void) sigset(SIGALRM, timer);
10047c478bd9Sstevel@tonic-gate 		timeout = 0;
10057c478bd9Sstevel@tonic-gate 		(void) sigsetjmp(timeoutbuf, 1);
10067c478bd9Sstevel@tonic-gate 		if (debug && standalone) {
10077c478bd9Sstevel@tonic-gate 			(void) fputs("Sending OACK ", stderr);
10087c478bd9Sstevel@tonic-gate 			print_options(stderr, (char *)&oackbuf.hdr.th_stuff,
10097c478bd9Sstevel@tonic-gate 			    oacklen - 2);
10107c478bd9Sstevel@tonic-gate 			(void) putc('\n', stderr);
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 		if (sendto(peer, &oackbuf, oacklen, 0,
10137c478bd9Sstevel@tonic-gate 		    (struct sockaddr *)&from, fromplen) != oacklen) {
10147c478bd9Sstevel@tonic-gate 			if (debug && standalone) {
10157c478bd9Sstevel@tonic-gate 				serrno = errno;
10167c478bd9Sstevel@tonic-gate 				perror("sendto (oack)");
10177c478bd9Sstevel@tonic-gate 				errno = serrno;
10187c478bd9Sstevel@tonic-gate 			}
10197c478bd9Sstevel@tonic-gate 			SYSLOG_MSG("sendto (oack): %m");
10207c478bd9Sstevel@tonic-gate 			goto abort;
10217c478bd9Sstevel@tonic-gate 		}
10227c478bd9Sstevel@tonic-gate 		(void) alarm(rexmtval); /* read the ack */
10237c478bd9Sstevel@tonic-gate 		for (;;) {
10247c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
10257c478bd9Sstevel@tonic-gate 			n = recv(peer, &ackbuf, sizeof (ackbuf), 0);
10267c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
10277c478bd9Sstevel@tonic-gate 			if (n < 0) {
10287c478bd9Sstevel@tonic-gate 				if (errno == EINTR)
10297c478bd9Sstevel@tonic-gate 					continue;
10307c478bd9Sstevel@tonic-gate 				serrno = errno;
10317c478bd9Sstevel@tonic-gate 				SYSLOG_MSG("recv (ack): %m");
10327c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
10337c478bd9Sstevel@tonic-gate 					errno = serrno;
10347c478bd9Sstevel@tonic-gate 					perror("recv (ack)");
10357c478bd9Sstevel@tonic-gate 				}
10367c478bd9Sstevel@tonic-gate 				goto abort;
10377c478bd9Sstevel@tonic-gate 			}
10387c478bd9Sstevel@tonic-gate 			ackbuf.tb_hdr.th_opcode =
10397c478bd9Sstevel@tonic-gate 			    ntohs((ushort_t)ackbuf.tb_hdr.th_opcode);
10407c478bd9Sstevel@tonic-gate 			ackbuf.tb_hdr.th_block =
10417c478bd9Sstevel@tonic-gate 			    ntohs((ushort_t)ackbuf.tb_hdr.th_block);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 			if (ackbuf.tb_hdr.th_opcode == ERROR) {
10447c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
10457c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
10467c478bd9Sstevel@tonic-gate 					    "received ERROR %d",
10477c478bd9Sstevel@tonic-gate 					    ackbuf.tb_hdr.th_code);
10487c478bd9Sstevel@tonic-gate 					if (n > 4)
10497c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
10507c478bd9Sstevel@tonic-gate 						    " %.*s", n - 4,
10517c478bd9Sstevel@tonic-gate 						    ackbuf.tb_hdr.th_msg);
10527c478bd9Sstevel@tonic-gate 					(void) putc('\n', stderr);
10537c478bd9Sstevel@tonic-gate 				}
10547c478bd9Sstevel@tonic-gate 				goto abort;
10557c478bd9Sstevel@tonic-gate 			}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 			if (ackbuf.tb_hdr.th_opcode == ACK) {
10587c478bd9Sstevel@tonic-gate 				if (debug && standalone)
10597c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
10607c478bd9Sstevel@tonic-gate 					    "received ACK for block %d\n",
10617c478bd9Sstevel@tonic-gate 					    ackbuf.tb_hdr.th_block);
10627c478bd9Sstevel@tonic-gate 				if (ackbuf.tb_hdr.th_block == 0)
10637c478bd9Sstevel@tonic-gate 					break;
10647c478bd9Sstevel@tonic-gate 				/*
10657c478bd9Sstevel@tonic-gate 				 * Don't resend the OACK, avoids getting stuck
10667c478bd9Sstevel@tonic-gate 				 * in an OACK/ACK loop if the client keeps
10677c478bd9Sstevel@tonic-gate 				 * replying with a bad ACK. Client will either
10687c478bd9Sstevel@tonic-gate 				 * send a good ACK or timeout sending bad ones.
10697c478bd9Sstevel@tonic-gate 				 */
10707c478bd9Sstevel@tonic-gate 			}
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 		cancel_alarm();
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 	dp = r_init();
10757c478bd9Sstevel@tonic-gate 	do {
10767c478bd9Sstevel@tonic-gate 		(void) sigset(SIGALRM, timer);
10777c478bd9Sstevel@tonic-gate 		size = readit(file, &dp, pf->f_convert);
10787c478bd9Sstevel@tonic-gate 		if (size < 0) {
10797c478bd9Sstevel@tonic-gate 			nak(errno + 100);
10807c478bd9Sstevel@tonic-gate 			goto abort;
10817c478bd9Sstevel@tonic-gate 		}
10827c478bd9Sstevel@tonic-gate 		dp->th_opcode = htons((ushort_t)DATA);
10837c478bd9Sstevel@tonic-gate 		dp->th_block = htons((ushort_t)block);
10847c478bd9Sstevel@tonic-gate 		timeout = 0;
10857c478bd9Sstevel@tonic-gate 		(void) sigsetjmp(timeoutbuf, 1);
10867c478bd9Sstevel@tonic-gate 		if (debug && standalone)
10877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Sending DATA block %d\n",
10887c478bd9Sstevel@tonic-gate 			    block);
10897c478bd9Sstevel@tonic-gate 		if (sendto(peer, dp, size + 4, 0,
10907c478bd9Sstevel@tonic-gate 		    (struct sockaddr *)&from,  fromplen) != size + 4) {
10917c478bd9Sstevel@tonic-gate 			if (debug && standalone) {
10927c478bd9Sstevel@tonic-gate 				serrno = errno;
10937c478bd9Sstevel@tonic-gate 				perror("sendto (data)");
10947c478bd9Sstevel@tonic-gate 				errno = serrno;
10957c478bd9Sstevel@tonic-gate 			}
10967c478bd9Sstevel@tonic-gate 			SYSLOG_MSG("sendto (data): %m");
10977c478bd9Sstevel@tonic-gate 			goto abort;
10987c478bd9Sstevel@tonic-gate 		}
10997c478bd9Sstevel@tonic-gate 		read_ahead(file, pf->f_convert);
11007c478bd9Sstevel@tonic-gate 		(void) alarm(rexmtval); /* read the ack */
11017c478bd9Sstevel@tonic-gate 		for (;;) {
11027c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
11037c478bd9Sstevel@tonic-gate 			n = recv(peer, &ackbuf, sizeof (ackbuf), 0);
11047c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
11057c478bd9Sstevel@tonic-gate 			if (n < 0) {
11067c478bd9Sstevel@tonic-gate 				if (errno == EINTR)
11077c478bd9Sstevel@tonic-gate 					continue;
11087c478bd9Sstevel@tonic-gate 				serrno = errno;
11097c478bd9Sstevel@tonic-gate 				SYSLOG_MSG("recv (ack): %m");
11107c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
11117c478bd9Sstevel@tonic-gate 					errno = serrno;
11127c478bd9Sstevel@tonic-gate 					perror("recv (ack)");
11137c478bd9Sstevel@tonic-gate 				}
11147c478bd9Sstevel@tonic-gate 				goto abort;
11157c478bd9Sstevel@tonic-gate 			}
11167c478bd9Sstevel@tonic-gate 			ackbuf.tb_hdr.th_opcode =
11177c478bd9Sstevel@tonic-gate 			    ntohs((ushort_t)ackbuf.tb_hdr.th_opcode);
11187c478bd9Sstevel@tonic-gate 			ackbuf.tb_hdr.th_block =
11197c478bd9Sstevel@tonic-gate 			    ntohs((ushort_t)ackbuf.tb_hdr.th_block);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 			if (ackbuf.tb_hdr.th_opcode == ERROR) {
11227c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
11237c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
11247c478bd9Sstevel@tonic-gate 					    "received ERROR %d",
11257c478bd9Sstevel@tonic-gate 					    ackbuf.tb_hdr.th_code);
11267c478bd9Sstevel@tonic-gate 					if (n > 4)
11277c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
11287c478bd9Sstevel@tonic-gate 						    " %.*s", n - 4,
11297c478bd9Sstevel@tonic-gate 						    ackbuf.tb_hdr.th_msg);
11307c478bd9Sstevel@tonic-gate 					(void) putc('\n', stderr);
11317c478bd9Sstevel@tonic-gate 				}
11327c478bd9Sstevel@tonic-gate 				goto abort;
11337c478bd9Sstevel@tonic-gate 			}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 			if (ackbuf.tb_hdr.th_opcode == ACK) {
11367c478bd9Sstevel@tonic-gate 				if (debug && standalone)
11377c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
11387c478bd9Sstevel@tonic-gate 						"received ACK for block %d\n",
11397c478bd9Sstevel@tonic-gate 						ackbuf.tb_hdr.th_block);
11407c478bd9Sstevel@tonic-gate 				if (ackbuf.tb_hdr.th_block == block) {
11417c478bd9Sstevel@tonic-gate 					break;
11427c478bd9Sstevel@tonic-gate 				}
11437c478bd9Sstevel@tonic-gate 				/*
11447c478bd9Sstevel@tonic-gate 				 * Never resend the current DATA packet on
11457c478bd9Sstevel@tonic-gate 				 * receipt of a duplicate ACK, doing so would
11467c478bd9Sstevel@tonic-gate 				 * cause the "Sorcerer's Apprentice Syndrome".
11477c478bd9Sstevel@tonic-gate 				 */
11487c478bd9Sstevel@tonic-gate 			}
11497c478bd9Sstevel@tonic-gate 		}
11507c478bd9Sstevel@tonic-gate 		cancel_alarm();
11517c478bd9Sstevel@tonic-gate 		block++;
11527c478bd9Sstevel@tonic-gate 	} while (size == blocksize);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate abort:
11557c478bd9Sstevel@tonic-gate 	cancel_alarm();
11567c478bd9Sstevel@tonic-gate 	(void) fclose(file);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /* ARGSUSED */
11607c478bd9Sstevel@tonic-gate static void
11617c478bd9Sstevel@tonic-gate justquit(int signum)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	exit(0);
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate /*
11677c478bd9Sstevel@tonic-gate  * Receive a file.
11687c478bd9Sstevel@tonic-gate  */
11697c478bd9Sstevel@tonic-gate static void
11707c478bd9Sstevel@tonic-gate tftpd_recvfile(struct formats *pf, int oacklen)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	struct tftphdr *dp;
11737c478bd9Sstevel@tonic-gate 	struct tftphdr *ap;    /* ack buffer */
1174*294f5787Sas198278 	ushort_t block = 0;
1175*294f5787Sas198278 	int n, size, acklen, serrno;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	dp = w_init();
11787c478bd9Sstevel@tonic-gate 	ap = &ackbuf.tb_hdr;
11797c478bd9Sstevel@tonic-gate 	do {
11807c478bd9Sstevel@tonic-gate 		(void) sigset(SIGALRM, timer);
11817c478bd9Sstevel@tonic-gate 		timeout = 0;
11827c478bd9Sstevel@tonic-gate 		if (oacklen == 0) {
11837c478bd9Sstevel@tonic-gate 			ap->th_opcode = htons((ushort_t)ACK);
11847c478bd9Sstevel@tonic-gate 			ap->th_block = htons((ushort_t)block);
11857c478bd9Sstevel@tonic-gate 			acklen = 4;
11867c478bd9Sstevel@tonic-gate 		} else {
11877c478bd9Sstevel@tonic-gate 			/* copy OACK packet to the ack buffer ready to send */
11887c478bd9Sstevel@tonic-gate 			(void) memcpy(&ackbuf, &oackbuf, oacklen);
11897c478bd9Sstevel@tonic-gate 			acklen = oacklen;
11907c478bd9Sstevel@tonic-gate 			oacklen = 0;
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 		block++;
11937c478bd9Sstevel@tonic-gate 		(void) sigsetjmp(timeoutbuf, 1);
11947c478bd9Sstevel@tonic-gate send_ack:
11957c478bd9Sstevel@tonic-gate 		if (debug && standalone) {
11967c478bd9Sstevel@tonic-gate 			if (ap->th_opcode == htons((ushort_t)ACK)) {
11977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11987c478bd9Sstevel@tonic-gate 				    "Sending ACK for block %d\n", block - 1);
11997c478bd9Sstevel@tonic-gate 			} else {
12007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "Sending OACK ");
12017c478bd9Sstevel@tonic-gate 				print_options(stderr, (char *)&ap->th_stuff,
12027c478bd9Sstevel@tonic-gate 				    acklen - 2);
12037c478bd9Sstevel@tonic-gate 				(void) putc('\n', stderr);
12047c478bd9Sstevel@tonic-gate 			}
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate 		if (sendto(peer, &ackbuf, acklen, 0, (struct sockaddr *)&from,
12077c478bd9Sstevel@tonic-gate 		    fromplen) != acklen) {
12087c478bd9Sstevel@tonic-gate 			if (ap->th_opcode == htons((ushort_t)ACK)) {
12097c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
12107c478bd9Sstevel@tonic-gate 					serrno = errno;
12117c478bd9Sstevel@tonic-gate 					perror("sendto (ack)");
12127c478bd9Sstevel@tonic-gate 					errno = serrno;
12137c478bd9Sstevel@tonic-gate 				}
12147c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "sendto (ack): %m\n");
12157c478bd9Sstevel@tonic-gate 			} else {
12167c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
12177c478bd9Sstevel@tonic-gate 					serrno = errno;
12187c478bd9Sstevel@tonic-gate 					perror("sendto (oack)");
12197c478bd9Sstevel@tonic-gate 					errno = serrno;
12207c478bd9Sstevel@tonic-gate 				}
12217c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "sendto (oack): %m\n");
12227c478bd9Sstevel@tonic-gate 			}
12237c478bd9Sstevel@tonic-gate 			goto abort;
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 		if (write_behind(file, pf->f_convert) < 0) {
12267c478bd9Sstevel@tonic-gate 			nak(errno + 100);
12277c478bd9Sstevel@tonic-gate 			goto abort;
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		(void) alarm(rexmtval);
12307c478bd9Sstevel@tonic-gate 		for (;;) {
12317c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
12327c478bd9Sstevel@tonic-gate 			n = recv(peer, dp, blocksize + 4, 0);
12337c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
12347c478bd9Sstevel@tonic-gate 			if (n < 0) { /* really? */
12357c478bd9Sstevel@tonic-gate 				if (errno == EINTR)
12367c478bd9Sstevel@tonic-gate 					continue;
12377c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "recv (data): %m");
12387c478bd9Sstevel@tonic-gate 				goto abort;
12397c478bd9Sstevel@tonic-gate 			}
12407c478bd9Sstevel@tonic-gate 			dp->th_opcode = ntohs((ushort_t)dp->th_opcode);
12417c478bd9Sstevel@tonic-gate 			dp->th_block = ntohs((ushort_t)dp->th_block);
12427c478bd9Sstevel@tonic-gate 			if (dp->th_opcode == ERROR) {
12437c478bd9Sstevel@tonic-gate 				cancel_alarm();
12447c478bd9Sstevel@tonic-gate 				if (debug && standalone) {
12457c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12467c478bd9Sstevel@tonic-gate 					    "received ERROR %d", dp->th_code);
12477c478bd9Sstevel@tonic-gate 					if (n > 4)
12487c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
12497c478bd9Sstevel@tonic-gate 						    " %.*s", n - 4, dp->th_msg);
12507c478bd9Sstevel@tonic-gate 					(void) putc('\n', stderr);
12517c478bd9Sstevel@tonic-gate 				}
12527c478bd9Sstevel@tonic-gate 				return;
12537c478bd9Sstevel@tonic-gate 			}
12547c478bd9Sstevel@tonic-gate 			if (dp->th_opcode == DATA) {
12557c478bd9Sstevel@tonic-gate 				if (debug && standalone)
12567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12577c478bd9Sstevel@tonic-gate 						"Received DATA block %d\n",
12587c478bd9Sstevel@tonic-gate 						dp->th_block);
12597c478bd9Sstevel@tonic-gate 				if (dp->th_block == block) {
12607c478bd9Sstevel@tonic-gate 					break;   /* normal */
12617c478bd9Sstevel@tonic-gate 				}
12627c478bd9Sstevel@tonic-gate 				/* Re-synchronize with the other side */
12637c478bd9Sstevel@tonic-gate 				if (synchnet(peer) < 0) {
12647c478bd9Sstevel@tonic-gate 					nak(errno + 100);
12657c478bd9Sstevel@tonic-gate 					goto abort;
12667c478bd9Sstevel@tonic-gate 				}
12677c478bd9Sstevel@tonic-gate 				if (dp->th_block == (block-1))
12687c478bd9Sstevel@tonic-gate 					goto send_ack; /* rexmit */
12697c478bd9Sstevel@tonic-gate 			}
12707c478bd9Sstevel@tonic-gate 		}
12717c478bd9Sstevel@tonic-gate 		cancel_alarm();
12727c478bd9Sstevel@tonic-gate 		/*  size = write(file, dp->th_data, n - 4); */
12737c478bd9Sstevel@tonic-gate 		size = writeit(file, &dp, n - 4, pf->f_convert);
12747c478bd9Sstevel@tonic-gate 		if (size != (n - 4)) {
12757c478bd9Sstevel@tonic-gate 			nak((size < 0) ? (errno + 100) : ENOSPACE);
12767c478bd9Sstevel@tonic-gate 			goto abort;
12777c478bd9Sstevel@tonic-gate 		}
12787c478bd9Sstevel@tonic-gate 	} while (size == blocksize);
12797c478bd9Sstevel@tonic-gate 	if (write_behind(file, pf->f_convert) < 0) {
12807c478bd9Sstevel@tonic-gate 		nak(errno + 100);
12817c478bd9Sstevel@tonic-gate 		goto abort;
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 	n = fclose(file);	/* close data file */
12847c478bd9Sstevel@tonic-gate 	file = NULL;
12857c478bd9Sstevel@tonic-gate 	if (n == EOF) {
12867c478bd9Sstevel@tonic-gate 		nak(errno + 100);
12877c478bd9Sstevel@tonic-gate 		goto abort;
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	ap->th_opcode = htons((ushort_t)ACK);    /* send the "final" ack */
12917c478bd9Sstevel@tonic-gate 	ap->th_block = htons((ushort_t)(block));
12927c478bd9Sstevel@tonic-gate 	if (debug && standalone)
12937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Sending ACK for block %d\n", block);
12947c478bd9Sstevel@tonic-gate 	if (sendto(peer, &ackbuf, 4, 0, (struct sockaddr *)&from,
12957c478bd9Sstevel@tonic-gate 	    fromplen) == -1) {
12967c478bd9Sstevel@tonic-gate 		if (debug && standalone)
12977c478bd9Sstevel@tonic-gate 			perror("sendto (ack)");
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 	(void) sigset(SIGALRM, justquit); /* just quit on timeout */
13007c478bd9Sstevel@tonic-gate 	(void) alarm(rexmtval);
13017c478bd9Sstevel@tonic-gate 	/* normally times out and quits */
13027c478bd9Sstevel@tonic-gate 	n = recv(peer, dp, blocksize + 4, 0);
13037c478bd9Sstevel@tonic-gate 	(void) alarm(0);
13047c478bd9Sstevel@tonic-gate 	dp->th_opcode = ntohs((ushort_t)dp->th_opcode);
13057c478bd9Sstevel@tonic-gate 	dp->th_block = ntohs((ushort_t)dp->th_block);
13067c478bd9Sstevel@tonic-gate 	if (n >= 4 &&		/* if read some data */
13077c478bd9Sstevel@tonic-gate 	    dp->th_opcode == DATA && /* and got a data block */
13087c478bd9Sstevel@tonic-gate 	    block == dp->th_block) {	/* then my last ack was lost */
13097c478bd9Sstevel@tonic-gate 		if (debug && standalone) {
13107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Sending ACK for block %d\n",
13117c478bd9Sstevel@tonic-gate 			    block);
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 		/* resend final ack */
13147c478bd9Sstevel@tonic-gate 		if (sendto(peer, &ackbuf, 4, 0, (struct sockaddr *)&from,
13157c478bd9Sstevel@tonic-gate 		    fromplen) == -1) {
13167c478bd9Sstevel@tonic-gate 			if (debug && standalone)
13177c478bd9Sstevel@tonic-gate 				perror("sendto (last ack)");
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate abort:
13227c478bd9Sstevel@tonic-gate 	cancel_alarm();
13237c478bd9Sstevel@tonic-gate 	if (file != NULL)
13247c478bd9Sstevel@tonic-gate 		(void) fclose(file);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate /*
13287c478bd9Sstevel@tonic-gate  * Send a nak packet (error message).
13297c478bd9Sstevel@tonic-gate  * Error code passed in is one of the
13307c478bd9Sstevel@tonic-gate  * standard TFTP codes, or a UNIX errno
13317c478bd9Sstevel@tonic-gate  * offset by 100.
13327c478bd9Sstevel@tonic-gate  * Handles connected as well as unconnected peer.
13337c478bd9Sstevel@tonic-gate  */
13347c478bd9Sstevel@tonic-gate static void
13357c478bd9Sstevel@tonic-gate nak(int error)
13367c478bd9Sstevel@tonic-gate {
13377c478bd9Sstevel@tonic-gate 	struct tftphdr *tp;
13387c478bd9Sstevel@tonic-gate 	int length;
13397c478bd9Sstevel@tonic-gate 	struct errmsg *pe;
13407c478bd9Sstevel@tonic-gate 	int ret;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	tp = &buf.hdr;
13437c478bd9Sstevel@tonic-gate 	tp->th_opcode = htons((ushort_t)ERROR);
13447c478bd9Sstevel@tonic-gate 	tp->th_code = htons((ushort_t)error);
13457c478bd9Sstevel@tonic-gate 	for (pe = errmsgs; pe->e_code >= 0; pe++)
13467c478bd9Sstevel@tonic-gate 		if (pe->e_code == error)
13477c478bd9Sstevel@tonic-gate 			break;
13487c478bd9Sstevel@tonic-gate 	if (pe->e_code < 0) {
13497c478bd9Sstevel@tonic-gate 		pe->e_msg = strerror(error - 100);
13507c478bd9Sstevel@tonic-gate 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
13517c478bd9Sstevel@tonic-gate 	}
13527c478bd9Sstevel@tonic-gate 	(void) strlcpy(tp->th_msg, (pe->e_msg != NULL) ? pe->e_msg : "UNKNOWN",
13537c478bd9Sstevel@tonic-gate 	    sizeof (buf) - sizeof (struct tftphdr));
13547c478bd9Sstevel@tonic-gate 	length = strlen(tp->th_msg);
13557c478bd9Sstevel@tonic-gate 	length += sizeof (struct tftphdr);
13567c478bd9Sstevel@tonic-gate 	if (debug && standalone)
13577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Sending NAK: %s\n", tp->th_msg);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	ret = sendto(peer, &buf, length, 0, (struct sockaddr *)&from,
13607c478bd9Sstevel@tonic-gate 	    fromplen);
13617c478bd9Sstevel@tonic-gate 	if (ret == -1 && errno == EISCONN) {
13627c478bd9Sstevel@tonic-gate 		/* Try without an address */
13637c478bd9Sstevel@tonic-gate 		ret = send(peer, &buf, length, 0);
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 	if (ret == -1) {
13667c478bd9Sstevel@tonic-gate 		if (standalone)
13677c478bd9Sstevel@tonic-gate 			perror("sendto (nak)");
13687c478bd9Sstevel@tonic-gate 		else
13697c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "tftpd: nak: %m\n");
13707c478bd9Sstevel@tonic-gate 	} else if (ret != length) {
13717c478bd9Sstevel@tonic-gate 		if (standalone)
13727c478bd9Sstevel@tonic-gate 			perror("sendto (nak) lost data");
13737c478bd9Sstevel@tonic-gate 		else
13747c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "tftpd: nak: %d lost\n", length - ret);
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate }
1377