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