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