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