tftpd.c revision 129683
11592Srgrimes/* 21592Srgrimes * Copyright (c) 1983, 1993 31592Srgrimes * The Regents of the University of California. All rights reserved. 41592Srgrimes * 51592Srgrimes * Redistribution and use in source and binary forms, with or without 61592Srgrimes * modification, are permitted provided that the following conditions 71592Srgrimes * are met: 81592Srgrimes * 1. Redistributions of source code must retain the above copyright 91592Srgrimes * notice, this list of conditions and the following disclaimer. 101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111592Srgrimes * notice, this list of conditions and the following disclaimer in the 121592Srgrimes * documentation and/or other materials provided with the distribution. 131592Srgrimes * 3. All advertising materials mentioning features or use of this software 141592Srgrimes * must display the following acknowledgement: 151592Srgrimes * This product includes software developed by the University of 161592Srgrimes * California, Berkeley and its contributors. 171592Srgrimes * 4. Neither the name of the University nor the names of its contributors 181592Srgrimes * may be used to endorse or promote products derived from this software 191592Srgrimes * without specific prior written permission. 201592Srgrimes * 211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241592Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311592Srgrimes * SUCH DAMAGE. 321592Srgrimes */ 331592Srgrimes 341592Srgrimes#ifndef lint 3531512Scharnierstatic const char copyright[] = 361592Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 371592Srgrimes The Regents of the University of California. All rights reserved.\n"; 381592Srgrimes#endif /* not lint */ 391592Srgrimes 401592Srgrimes#ifndef lint 4131512Scharnier#if 0 421592Srgrimesstatic char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93"; 4331512Scharnier#endif 4431512Scharnierstatic const char rcsid[] = 4550476Speter "$FreeBSD: head/libexec/tftpd/tftpd.c 129683 2004-05-25 01:40:27Z mdodd $"; 461592Srgrimes#endif /* not lint */ 471592Srgrimes 481592Srgrimes/* 491592Srgrimes * Trivial file transfer protocol server. 501592Srgrimes * 511592Srgrimes * This version includes many modifications by Jim Guyton 521592Srgrimes * <guyton@rand-unix>. 531592Srgrimes */ 541592Srgrimes 551592Srgrimes#include <sys/param.h> 561592Srgrimes#include <sys/ioctl.h> 571592Srgrimes#include <sys/stat.h> 581592Srgrimes#include <sys/socket.h> 5918458Simp#include <sys/types.h> 601592Srgrimes 611592Srgrimes#include <netinet/in.h> 621592Srgrimes#include <arpa/tftp.h> 631592Srgrimes#include <arpa/inet.h> 641592Srgrimes 651592Srgrimes#include <ctype.h> 661592Srgrimes#include <errno.h> 671592Srgrimes#include <fcntl.h> 6845393Sbrian#include <libutil.h> 691592Srgrimes#include <netdb.h> 7031512Scharnier#include <pwd.h> 711592Srgrimes#include <setjmp.h> 721592Srgrimes#include <signal.h> 731592Srgrimes#include <stdio.h> 741592Srgrimes#include <stdlib.h> 751592Srgrimes#include <string.h> 761592Srgrimes#include <syslog.h> 771592Srgrimes#include <unistd.h> 781592Srgrimes 791592Srgrimes#include "tftpsubs.h" 801592Srgrimes 811592Srgrimes#define TIMEOUT 5 8284047Sobrien#define MAX_TIMEOUTS 5 831592Srgrimes 841592Srgrimesint peer; 851592Srgrimesint rexmtval = TIMEOUT; 8684047Sobrienint max_rexmtval = 2*TIMEOUT; 871592Srgrimes 881592Srgrimes#define PKTSIZE SEGSIZE+4 891592Srgrimeschar buf[PKTSIZE]; 901592Srgrimeschar ackbuf[PKTSIZE]; 9194443Sumestruct sockaddr_storage from; 921592Srgrimesint fromlen; 931592Srgrimes 9490333Simpvoid tftp(struct tftphdr *, int); 9594443Sumestatic void unmappedaddr(struct sockaddr_in6 *); 961592Srgrimes 971592Srgrimes/* 981592Srgrimes * Null-terminated directory prefix list for absolute pathname requests and 991592Srgrimes * search list for relative pathname requests. 1001592Srgrimes * 1011592Srgrimes * MAXDIRS should be at least as large as the number of arguments that 1021592Srgrimes * inetd allows (currently 20). 1031592Srgrimes */ 1041592Srgrimes#define MAXDIRS 20 1051592Srgrimesstatic struct dirlist { 106112452Sdwmalone const char *name; 1071592Srgrimes int len; 1081592Srgrimes} dirs[MAXDIRS+1]; 1091592Srgrimesstatic int suppress_naks; 1101592Srgrimesstatic int logging; 11171616Sbillfstatic int ipchroot; 112129680Smdoddstatic int create_new = 0; 113129680Smdoddstatic mode_t mask = S_IWGRP|S_IWOTH; 1141592Srgrimes 115112452Sdwmalonestatic const char *errtomsg(int); 11690333Simpstatic void nak(int); 117112452Sdwmalonestatic void oack(void); 1181592Srgrimes 119112452Sdwmalonestatic void timer(int); 120112452Sdwmalonestatic void justquit(int); 121112452Sdwmalone 1221592Srgrimesint 12390333Simpmain(int argc, char *argv[]) 1241592Srgrimes{ 12590333Simp struct tftphdr *tp; 12690333Simp int n; 1271592Srgrimes int ch, on; 12894443Sume struct sockaddr_storage me; 12994443Sume int len; 13018458Simp char *chroot_dir = NULL; 13118458Simp struct passwd *nobody; 132112452Sdwmalone const char *chuser = "nobody"; 1331592Srgrimes 13435152Sphk openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 135129680Smdodd while ((ch = getopt(argc, argv, "cClns:u:Uw")) != -1) { 1361592Srgrimes switch (ch) { 13771616Sbillf case 'c': 13871616Sbillf ipchroot = 1; 13971616Sbillf break; 14071616Sbillf case 'C': 14171616Sbillf ipchroot = 2; 14271616Sbillf break; 1431592Srgrimes case 'l': 1441592Srgrimes logging = 1; 1451592Srgrimes break; 1461592Srgrimes case 'n': 1471592Srgrimes suppress_naks = 1; 1481592Srgrimes break; 14918458Simp case 's': 15018458Simp chroot_dir = optarg; 15118458Simp break; 15265850Swollman case 'u': 15365850Swollman chuser = optarg; 15465850Swollman break; 155129680Smdodd case 'U': 156129680Smdodd mask = strtol(optarg, NULL, 0); 157129680Smdodd break; 158129680Smdodd case 'w': 159129680Smdodd create_new = 1; 160129680Smdodd break; 1611592Srgrimes default: 1621592Srgrimes syslog(LOG_WARNING, "ignoring unknown option -%c", ch); 1631592Srgrimes } 1641592Srgrimes } 1651592Srgrimes if (optind < argc) { 1661592Srgrimes struct dirlist *dirp; 1671592Srgrimes 1681592Srgrimes /* Get list of directory prefixes. Skip relative pathnames. */ 1691592Srgrimes for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS]; 1701592Srgrimes optind++) { 1711592Srgrimes if (argv[optind][0] == '/') { 1721592Srgrimes dirp->name = argv[optind]; 1731592Srgrimes dirp->len = strlen(dirp->name); 1741592Srgrimes dirp++; 1751592Srgrimes } 1761592Srgrimes } 1771592Srgrimes } 17818458Simp else if (chroot_dir) { 17918458Simp dirs->name = "/"; 18018458Simp dirs->len = 1; 18118458Simp } 182113714Sbillf if (ipchroot > 0 && chroot_dir == NULL) { 18371616Sbillf syslog(LOG_ERR, "-c requires -s"); 18471616Sbillf exit(1); 18571616Sbillf } 1861592Srgrimes 187129680Smdodd umask(mask); 188129680Smdodd 1891592Srgrimes on = 1; 1901592Srgrimes if (ioctl(0, FIONBIO, &on) < 0) { 19131512Scharnier syslog(LOG_ERR, "ioctl(FIONBIO): %m"); 1921592Srgrimes exit(1); 1931592Srgrimes } 1941592Srgrimes fromlen = sizeof (from); 1951592Srgrimes n = recvfrom(0, buf, sizeof (buf), 0, 1961592Srgrimes (struct sockaddr *)&from, &fromlen); 1971592Srgrimes if (n < 0) { 19831512Scharnier syslog(LOG_ERR, "recvfrom: %m"); 1991592Srgrimes exit(1); 2001592Srgrimes } 2011592Srgrimes /* 2021592Srgrimes * Now that we have read the message out of the UDP 2031592Srgrimes * socket, we fork and exit. Thus, inetd will go back 2041592Srgrimes * to listening to the tftp port, and the next request 2051592Srgrimes * to come in will start up a new instance of tftpd. 2061592Srgrimes * 2071592Srgrimes * We do this so that inetd can run tftpd in "wait" mode. 2081592Srgrimes * The problem with tftpd running in "nowait" mode is that 2091592Srgrimes * inetd may get one or more successful "selects" on the 2101592Srgrimes * tftp port before we do our receive, so more than one 2111592Srgrimes * instance of tftpd may be started up. Worse, if tftpd 2121592Srgrimes * break before doing the above "recvfrom", inetd would 2131592Srgrimes * spawn endless instances, clogging the system. 2141592Srgrimes */ 2151592Srgrimes { 2161592Srgrimes int pid; 2171592Srgrimes int i, j; 2181592Srgrimes 2191592Srgrimes for (i = 1; i < 20; i++) { 2201592Srgrimes pid = fork(); 2211592Srgrimes if (pid < 0) { 2221592Srgrimes sleep(i); 2231592Srgrimes /* 2241592Srgrimes * flush out to most recently sent request. 2251592Srgrimes * 2261592Srgrimes * This may drop some request, but those 2271592Srgrimes * will be resent by the clients when 2281592Srgrimes * they timeout. The positive effect of 2291592Srgrimes * this flush is to (try to) prevent more 2301592Srgrimes * than one tftpd being started up to service 2311592Srgrimes * a single request from a single client. 2321592Srgrimes */ 2331592Srgrimes j = sizeof from; 2341592Srgrimes i = recvfrom(0, buf, sizeof (buf), 0, 2351592Srgrimes (struct sockaddr *)&from, &j); 2361592Srgrimes if (i > 0) { 2371592Srgrimes n = i; 2381592Srgrimes fromlen = j; 2391592Srgrimes } 2401592Srgrimes } else { 2411592Srgrimes break; 2421592Srgrimes } 2431592Srgrimes } 2441592Srgrimes if (pid < 0) { 24531512Scharnier syslog(LOG_ERR, "fork: %m"); 2461592Srgrimes exit(1); 2471592Srgrimes } else if (pid != 0) { 2481592Srgrimes exit(0); 2491592Srgrimes } 2501592Srgrimes } 25118458Simp 25218458Simp /* 25318458Simp * Since we exit here, we should do that only after the above 25418458Simp * recvfrom to keep inetd from constantly forking should there 25518458Simp * be a problem. See the above comment about system clogging. 25618458Simp */ 25718458Simp if (chroot_dir) { 258113714Sbillf if (ipchroot > 0) { 25971616Sbillf char *tempchroot; 26071616Sbillf struct stat sb; 26171616Sbillf int statret; 26294443Sume struct sockaddr_storage ss; 26394443Sume char hbuf[NI_MAXHOST]; 26471616Sbillf 26594443Sume memcpy(&ss, &from, from.ss_len); 26694443Sume unmappedaddr((struct sockaddr_in6 *)&ss); 26794443Sume getnameinfo((struct sockaddr *)&ss, ss.ss_len, 26894443Sume hbuf, sizeof(hbuf), NULL, 0, 26994443Sume NI_NUMERICHOST | NI_WITHSCOPEID); 27094443Sume asprintf(&tempchroot, "%s/%s", chroot_dir, hbuf); 271113714Sbillf if (ipchroot == 2) 272113714Sbillf statret = stat(tempchroot, &sb); 273113714Sbillf if (ipchroot == 1 || 274113714Sbillf (statret == 0 && (sb.st_mode & S_IFDIR))) 27571616Sbillf chroot_dir = tempchroot; 27671616Sbillf } 27718458Simp /* Must get this before chroot because /etc might go away */ 27865850Swollman if ((nobody = getpwnam(chuser)) == NULL) { 27965850Swollman syslog(LOG_ERR, "%s: no such user", chuser); 28018458Simp exit(1); 28118458Simp } 28218458Simp if (chroot(chroot_dir)) { 28318458Simp syslog(LOG_ERR, "chroot: %s: %m", chroot_dir); 28418458Simp exit(1); 28518458Simp } 28618458Simp chdir( "/" ); 28718458Simp setuid(nobody->pw_uid); 28885299Sobrien setgroups(1, &nobody->pw_gid); 28918458Simp } 29018458Simp 29194443Sume len = sizeof(me); 29294443Sume if (getsockname(0, (struct sockaddr *)&me, &len) == 0) { 29394443Sume switch (me.ss_family) { 29494443Sume case AF_INET: 29594443Sume ((struct sockaddr_in *)&me)->sin_port = 0; 29694443Sume break; 29794443Sume case AF_INET6: 29894443Sume ((struct sockaddr_in6 *)&me)->sin6_port = 0; 29994443Sume break; 30094443Sume default: 30194443Sume /* unsupported */ 30294443Sume break; 30394443Sume } 30494443Sume } else { 30594443Sume memset(&me, 0, sizeof(me)); 30694443Sume me.ss_family = from.ss_family; 30794443Sume me.ss_len = from.ss_len; 30894443Sume } 3091592Srgrimes alarm(0); 3101592Srgrimes close(0); 3111592Srgrimes close(1); 31294443Sume peer = socket(from.ss_family, SOCK_DGRAM, 0); 3131592Srgrimes if (peer < 0) { 31431512Scharnier syslog(LOG_ERR, "socket: %m"); 3151592Srgrimes exit(1); 3161592Srgrimes } 31794443Sume if (bind(peer, (struct sockaddr *)&me, me.ss_len) < 0) { 31831512Scharnier syslog(LOG_ERR, "bind: %m"); 3191592Srgrimes exit(1); 3201592Srgrimes } 32194443Sume if (connect(peer, (struct sockaddr *)&from, from.ss_len) < 0) { 32231512Scharnier syslog(LOG_ERR, "connect: %m"); 3231592Srgrimes exit(1); 3241592Srgrimes } 3251592Srgrimes tp = (struct tftphdr *)buf; 3261592Srgrimes tp->th_opcode = ntohs(tp->th_opcode); 3271592Srgrimes if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) 3281592Srgrimes tftp(tp, n); 3291592Srgrimes exit(1); 3301592Srgrimes} 3311592Srgrimes 3321592Srgrimesstruct formats; 33390333Simpint validate_access(char **, int); 33490333Simpvoid xmitfile(struct formats *); 33590333Simpvoid recvfile(struct formats *); 3361592Srgrimes 3371592Srgrimesstruct formats { 338112452Sdwmalone const char *f_mode; 33990333Simp int (*f_validate)(char **, int); 34090333Simp void (*f_send)(struct formats *); 34190333Simp void (*f_recv)(struct formats *); 3421592Srgrimes int f_convert; 3431592Srgrimes} formats[] = { 34440765Sdg { "netascii", validate_access, xmitfile, recvfile, 1 }, 34540765Sdg { "octet", validate_access, xmitfile, recvfile, 0 }, 3461592Srgrimes#ifdef notdef 3471592Srgrimes { "mail", validate_user, sendmail, recvmail, 1 }, 3481592Srgrimes#endif 349112452Sdwmalone { 0, NULL, NULL, NULL, 0 } 3501592Srgrimes}; 3511592Srgrimes 35284047Sobrienstruct options { 353112452Sdwmalone const char *o_type; 35484047Sobrien char *o_request; 35584047Sobrien int o_reply; /* turn into union if need be */ 35684047Sobrien} options[] = { 357112452Sdwmalone { "tsize", NULL, 0 }, /* OPT_TSIZE */ 358112452Sdwmalone { "timeout", NULL, 0 }, /* OPT_TIMEOUT */ 359112452Sdwmalone { NULL, NULL, 0 } 36084047Sobrien}; 36184047Sobrien 36284047Sobrienenum opt_enum { 36384047Sobrien OPT_TSIZE = 0, 36484047Sobrien OPT_TIMEOUT, 36584047Sobrien}; 36684047Sobrien 3671592Srgrimes/* 3681592Srgrimes * Handle initial connection protocol. 3691592Srgrimes */ 3701592Srgrimesvoid 37190333Simptftp(struct tftphdr *tp, int size) 3721592Srgrimes{ 37390333Simp char *cp; 37484047Sobrien int i, first = 1, has_options = 0, ecode; 37590333Simp struct formats *pf; 37684047Sobrien char *filename, *mode, *option, *ccp; 377122916Ssobomax char fnbuf[MAXPATHLEN]; 3781592Srgrimes 379122916Ssobomax cp = tp->th_stuff; 3801592Srgrimesagain: 3811592Srgrimes while (cp < buf + size) { 3821592Srgrimes if (*cp == '\0') 3831592Srgrimes break; 3841592Srgrimes cp++; 3851592Srgrimes } 3861592Srgrimes if (*cp != '\0') { 3871592Srgrimes nak(EBADOP); 3881592Srgrimes exit(1); 3891592Srgrimes } 390122916Ssobomax i = cp - tp->th_stuff; 391122916Ssobomax if (i >= sizeof(fnbuf)) { 392122916Ssobomax nak(EBADOP); 393122916Ssobomax exit(1); 394122916Ssobomax } 395122916Ssobomax memcpy(fnbuf, tp->th_stuff, i); 396122916Ssobomax fnbuf[i] = '\0'; 397122916Ssobomax filename = fnbuf; 3981592Srgrimes if (first) { 3991592Srgrimes mode = ++cp; 4001592Srgrimes first = 0; 4011592Srgrimes goto again; 4021592Srgrimes } 4031592Srgrimes for (cp = mode; *cp; cp++) 4041592Srgrimes if (isupper(*cp)) 4051592Srgrimes *cp = tolower(*cp); 4061592Srgrimes for (pf = formats; pf->f_mode; pf++) 4071592Srgrimes if (strcmp(pf->f_mode, mode) == 0) 4081592Srgrimes break; 4091592Srgrimes if (pf->f_mode == 0) { 4101592Srgrimes nak(EBADOP); 4111592Srgrimes exit(1); 4121592Srgrimes } 41384047Sobrien while (++cp < buf + size) { 41484047Sobrien for (i = 2, ccp = cp; i > 0; ccp++) { 41584047Sobrien if (ccp >= buf + size) { 41686765Sbenno /* 41786765Sbenno * Don't reject the request, just stop trying 41886765Sbenno * to parse the option and get on with it. 41986765Sbenno * Some Apple OpenFirmware versions have 42086765Sbenno * trailing garbage on the end of otherwise 42186765Sbenno * valid requests. 42286765Sbenno */ 42386765Sbenno goto option_fail; 42484047Sobrien } else if (*ccp == '\0') 42584047Sobrien i--; 42684047Sobrien } 42784047Sobrien for (option = cp; *cp; cp++) 42884047Sobrien if (isupper(*cp)) 42984047Sobrien *cp = tolower(*cp); 43084047Sobrien for (i = 0; options[i].o_type != NULL; i++) 43184047Sobrien if (strcmp(option, options[i].o_type) == 0) { 43284047Sobrien options[i].o_request = ++cp; 43384047Sobrien has_options = 1; 43484047Sobrien } 43584047Sobrien cp = ccp-1; 43684047Sobrien } 43784047Sobrien 43886765Sbennooption_fail: 43984047Sobrien if (options[OPT_TIMEOUT].o_request) { 44084047Sobrien int to = atoi(options[OPT_TIMEOUT].o_request); 44184047Sobrien if (to < 1 || to > 255) { 44284047Sobrien nak(EBADOP); 44384047Sobrien exit(1); 44484047Sobrien } 44584047Sobrien else if (to <= max_rexmtval) 44684047Sobrien options[OPT_TIMEOUT].o_reply = rexmtval = to; 44784047Sobrien else 44884047Sobrien options[OPT_TIMEOUT].o_request = NULL; 44984047Sobrien } 45084047Sobrien 4511592Srgrimes ecode = (*pf->f_validate)(&filename, tp->th_opcode); 45284047Sobrien if (has_options) 45384047Sobrien oack(); 4541592Srgrimes if (logging) { 45594443Sume char hbuf[NI_MAXHOST]; 45645393Sbrian 45794443Sume getnameinfo((struct sockaddr *)&from, from.ss_len, 45894443Sume hbuf, sizeof(hbuf), NULL, 0, 45994443Sume NI_WITHSCOPEID); 46094443Sume syslog(LOG_INFO, "%s: %s request for %s: %s", hbuf, 4611592Srgrimes tp->th_opcode == WRQ ? "write" : "read", 4621592Srgrimes filename, errtomsg(ecode)); 4631592Srgrimes } 4641592Srgrimes if (ecode) { 4651592Srgrimes /* 4661592Srgrimes * Avoid storms of naks to a RRQ broadcast for a relative 4671592Srgrimes * bootfile pathname from a diskless Sun. 4681592Srgrimes */ 4691592Srgrimes if (suppress_naks && *filename != '/' && ecode == ENOTFOUND) 4701592Srgrimes exit(0); 4711592Srgrimes nak(ecode); 4721592Srgrimes exit(1); 4731592Srgrimes } 4741592Srgrimes if (tp->th_opcode == WRQ) 4751592Srgrimes (*pf->f_recv)(pf); 4761592Srgrimes else 4771592Srgrimes (*pf->f_send)(pf); 4781592Srgrimes exit(0); 4791592Srgrimes} 4801592Srgrimes 4811592Srgrimes 4821592SrgrimesFILE *file; 4831592Srgrimes 4841592Srgrimes/* 4851592Srgrimes * Validate file access. Since we 4861592Srgrimes * have no uid or gid, for now require 4871592Srgrimes * file to exist and be publicly 4881592Srgrimes * readable/writable. 4891592Srgrimes * If we were invoked with arguments 4901592Srgrimes * from inetd then the file must also be 4911592Srgrimes * in one of the given directory prefixes. 4921592Srgrimes * Note also, full path name must be 4931592Srgrimes * given as we have no login directory. 4941592Srgrimes */ 4951592Srgrimesint 49690333Simpvalidate_access(char **filep, int mode) 4971592Srgrimes{ 4981592Srgrimes struct stat stbuf; 4991592Srgrimes int fd; 5001592Srgrimes struct dirlist *dirp; 5011592Srgrimes static char pathname[MAXPATHLEN]; 5021592Srgrimes char *filename = *filep; 5031592Srgrimes 5041592Srgrimes /* 5051592Srgrimes * Prevent tricksters from getting around the directory restrictions 5061592Srgrimes */ 5071592Srgrimes if (strstr(filename, "/../")) 5081592Srgrimes return (EACCESS); 5091592Srgrimes 5101592Srgrimes if (*filename == '/') { 5111592Srgrimes /* 5121592Srgrimes * Allow the request if it's in one of the approved locations. 5131592Srgrimes * Special case: check the null prefix ("/") by looking 5141592Srgrimes * for length = 1 and relying on the arg. processing that 5151592Srgrimes * it's a /. 5161592Srgrimes */ 5171592Srgrimes for (dirp = dirs; dirp->name != NULL; dirp++) { 5181592Srgrimes if (dirp->len == 1 || 5191592Srgrimes (!strncmp(filename, dirp->name, dirp->len) && 5201592Srgrimes filename[dirp->len] == '/')) 5211592Srgrimes break; 5221592Srgrimes } 5231592Srgrimes /* If directory list is empty, allow access to any file */ 5241592Srgrimes if (dirp->name == NULL && dirp != dirs) 5251592Srgrimes return (EACCESS); 5261592Srgrimes if (stat(filename, &stbuf) < 0) 5271592Srgrimes return (errno == ENOENT ? ENOTFOUND : EACCESS); 5281592Srgrimes if ((stbuf.st_mode & S_IFMT) != S_IFREG) 5291592Srgrimes return (ENOTFOUND); 5301592Srgrimes if (mode == RRQ) { 5311592Srgrimes if ((stbuf.st_mode & S_IROTH) == 0) 5321592Srgrimes return (EACCESS); 5331592Srgrimes } else { 5341592Srgrimes if ((stbuf.st_mode & S_IWOTH) == 0) 5351592Srgrimes return (EACCESS); 5361592Srgrimes } 5371592Srgrimes } else { 5381592Srgrimes int err; 5391592Srgrimes 5401592Srgrimes /* 5411592Srgrimes * Relative file name: search the approved locations for it. 5426750Sjkh * Don't allow write requests that avoid directory 5431592Srgrimes * restrictions. 5441592Srgrimes */ 5451592Srgrimes 5466750Sjkh if (!strncmp(filename, "../", 3)) 5471592Srgrimes return (EACCESS); 5481592Srgrimes 5491592Srgrimes /* 5501592Srgrimes * If the file exists in one of the directories and isn't 5511592Srgrimes * readable, continue looking. However, change the error code 5521592Srgrimes * to give an indication that the file exists. 5531592Srgrimes */ 5541592Srgrimes err = ENOTFOUND; 5551592Srgrimes for (dirp = dirs; dirp->name != NULL; dirp++) { 55624193Simp snprintf(pathname, sizeof(pathname), "%s/%s", 55724193Simp dirp->name, filename); 5581592Srgrimes if (stat(pathname, &stbuf) == 0 && 5591592Srgrimes (stbuf.st_mode & S_IFMT) == S_IFREG) { 5601592Srgrimes if ((stbuf.st_mode & S_IROTH) != 0) { 5611592Srgrimes break; 5621592Srgrimes } 5631592Srgrimes err = EACCESS; 5641592Srgrimes } 5651592Srgrimes } 566129680Smdodd if (dirp->name != NULL) 567129680Smdodd *filep = filename = pathname; 568129680Smdodd else if (mode == RRQ) 5691592Srgrimes return (err); 5701592Srgrimes } 57184047Sobrien if (options[OPT_TSIZE].o_request) { 57284047Sobrien if (mode == RRQ) 57384047Sobrien options[OPT_TSIZE].o_reply = stbuf.st_size; 57484047Sobrien else 57584047Sobrien /* XXX Allows writes of all sizes. */ 57684047Sobrien options[OPT_TSIZE].o_reply = 57784047Sobrien atoi(options[OPT_TSIZE].o_request); 57884047Sobrien } 579129680Smdodd if (mode == RRQ) 580129680Smdodd fd = open(filename, O_RDONLY); 581129680Smdodd else { 582129680Smdodd if (create_new) 583129680Smdodd fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0666); 584129680Smdodd else 585129680Smdodd fd = open(filename, O_WRONLY|O_TRUNC); 586129680Smdodd } 5871592Srgrimes if (fd < 0) 5881592Srgrimes return (errno + 100); 5891592Srgrimes file = fdopen(fd, (mode == RRQ)? "r":"w"); 5901592Srgrimes if (file == NULL) { 591129683Smdodd close(fd); 592129683Smdodd return (errno + 100); 5931592Srgrimes } 5941592Srgrimes return (0); 5951592Srgrimes} 5961592Srgrimes 59784047Sobrienint timeouts; 5981592Srgrimesjmp_buf timeoutbuf; 5991592Srgrimes 6001592Srgrimesvoid 60190333Simptimer(int sig __unused) 6021592Srgrimes{ 60384047Sobrien if (++timeouts > MAX_TIMEOUTS) 6041592Srgrimes exit(1); 6051592Srgrimes longjmp(timeoutbuf, 1); 6061592Srgrimes} 6071592Srgrimes 6081592Srgrimes/* 6091592Srgrimes * Send the requested file. 6101592Srgrimes */ 6111592Srgrimesvoid 61290333Simpxmitfile(struct formats *pf) 6131592Srgrimes{ 614112452Sdwmalone struct tftphdr *dp; 61590333Simp struct tftphdr *ap; /* ack packet */ 61690333Simp int size, n; 61771926Sasmodai volatile unsigned short block; 6181592Srgrimes 6191592Srgrimes signal(SIGALRM, timer); 6201592Srgrimes dp = r_init(); 6211592Srgrimes ap = (struct tftphdr *)ackbuf; 6221592Srgrimes block = 1; 6231592Srgrimes do { 6241592Srgrimes size = readit(file, &dp, pf->f_convert); 6251592Srgrimes if (size < 0) { 6261592Srgrimes nak(errno + 100); 6271592Srgrimes goto abort; 6281592Srgrimes } 6291592Srgrimes dp->th_opcode = htons((u_short)DATA); 6301592Srgrimes dp->th_block = htons((u_short)block); 63184047Sobrien timeouts = 0; 6321592Srgrimes (void)setjmp(timeoutbuf); 6331592Srgrimes 6341592Srgrimessend_data: 63594299Sambrisko { 63694299Sambrisko int i, t = 1; 63794299Sambrisko for (i = 0; ; i++){ 63894299Sambrisko if (send(peer, dp, size + 4, 0) != size + 4) { 63994299Sambrisko sleep(t); 64094299Sambrisko t = (t < 32) ? t<< 1 : t; 64194299Sambrisko if (i >= 12) { 64294299Sambrisko syslog(LOG_ERR, "write: %m"); 64394299Sambrisko goto abort; 64494299Sambrisko } 64594299Sambrisko } 64694299Sambrisko break; 64794299Sambrisko } 6481592Srgrimes } 6491592Srgrimes read_ahead(file, pf->f_convert); 6501592Srgrimes for ( ; ; ) { 6511592Srgrimes alarm(rexmtval); /* read the ack */ 6521592Srgrimes n = recv(peer, ackbuf, sizeof (ackbuf), 0); 6531592Srgrimes alarm(0); 6541592Srgrimes if (n < 0) { 65531512Scharnier syslog(LOG_ERR, "read: %m"); 6561592Srgrimes goto abort; 6571592Srgrimes } 6581592Srgrimes ap->th_opcode = ntohs((u_short)ap->th_opcode); 6591592Srgrimes ap->th_block = ntohs((u_short)ap->th_block); 6601592Srgrimes 6611592Srgrimes if (ap->th_opcode == ERROR) 6621592Srgrimes goto abort; 6631592Srgrimes 6641592Srgrimes if (ap->th_opcode == ACK) { 6651592Srgrimes if (ap->th_block == block) 6661592Srgrimes break; 6671592Srgrimes /* Re-synchronize with the other side */ 6681592Srgrimes (void) synchnet(peer); 6691592Srgrimes if (ap->th_block == (block -1)) 6701592Srgrimes goto send_data; 6711592Srgrimes } 6721592Srgrimes 6731592Srgrimes } 6741592Srgrimes block++; 6751592Srgrimes } while (size == SEGSIZE); 6761592Srgrimesabort: 6771592Srgrimes (void) fclose(file); 6781592Srgrimes} 6791592Srgrimes 6801592Srgrimesvoid 68190333Simpjustquit(int sig __unused) 6821592Srgrimes{ 6831592Srgrimes exit(0); 6841592Srgrimes} 6851592Srgrimes 6861592Srgrimes 6871592Srgrimes/* 6881592Srgrimes * Receive a file. 6891592Srgrimes */ 6901592Srgrimesvoid 69190333Simprecvfile(struct formats *pf) 6921592Srgrimes{ 693112452Sdwmalone struct tftphdr *dp; 69490333Simp struct tftphdr *ap; /* ack buffer */ 69590333Simp int n, size; 69671926Sasmodai volatile unsigned short block; 6971592Srgrimes 6981592Srgrimes signal(SIGALRM, timer); 6991592Srgrimes dp = w_init(); 7001592Srgrimes ap = (struct tftphdr *)ackbuf; 7011592Srgrimes block = 0; 7021592Srgrimes do { 70384047Sobrien timeouts = 0; 7041592Srgrimes ap->th_opcode = htons((u_short)ACK); 7051592Srgrimes ap->th_block = htons((u_short)block); 7061592Srgrimes block++; 7071592Srgrimes (void) setjmp(timeoutbuf); 7081592Srgrimessend_ack: 7091592Srgrimes if (send(peer, ackbuf, 4, 0) != 4) { 71031512Scharnier syslog(LOG_ERR, "write: %m"); 7111592Srgrimes goto abort; 7121592Srgrimes } 7131592Srgrimes write_behind(file, pf->f_convert); 7141592Srgrimes for ( ; ; ) { 7151592Srgrimes alarm(rexmtval); 7161592Srgrimes n = recv(peer, dp, PKTSIZE, 0); 7171592Srgrimes alarm(0); 7181592Srgrimes if (n < 0) { /* really? */ 71931512Scharnier syslog(LOG_ERR, "read: %m"); 7201592Srgrimes goto abort; 7211592Srgrimes } 7221592Srgrimes dp->th_opcode = ntohs((u_short)dp->th_opcode); 7231592Srgrimes dp->th_block = ntohs((u_short)dp->th_block); 7241592Srgrimes if (dp->th_opcode == ERROR) 7251592Srgrimes goto abort; 7261592Srgrimes if (dp->th_opcode == DATA) { 7271592Srgrimes if (dp->th_block == block) { 7281592Srgrimes break; /* normal */ 7291592Srgrimes } 7301592Srgrimes /* Re-synchronize with the other side */ 7311592Srgrimes (void) synchnet(peer); 7321592Srgrimes if (dp->th_block == (block-1)) 7331592Srgrimes goto send_ack; /* rexmit */ 7341592Srgrimes } 7351592Srgrimes } 7361592Srgrimes /* size = write(file, dp->th_data, n - 4); */ 7371592Srgrimes size = writeit(file, &dp, n - 4, pf->f_convert); 7381592Srgrimes if (size != (n-4)) { /* ahem */ 7391592Srgrimes if (size < 0) nak(errno + 100); 7401592Srgrimes else nak(ENOSPACE); 7411592Srgrimes goto abort; 7421592Srgrimes } 7431592Srgrimes } while (size == SEGSIZE); 7441592Srgrimes write_behind(file, pf->f_convert); 7451592Srgrimes (void) fclose(file); /* close data file */ 7461592Srgrimes 7471592Srgrimes ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */ 7481592Srgrimes ap->th_block = htons((u_short)(block)); 7491592Srgrimes (void) send(peer, ackbuf, 4, 0); 7501592Srgrimes 7511592Srgrimes signal(SIGALRM, justquit); /* just quit on timeout */ 7521592Srgrimes alarm(rexmtval); 7531592Srgrimes n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */ 7541592Srgrimes alarm(0); 7551592Srgrimes if (n >= 4 && /* if read some data */ 7561592Srgrimes dp->th_opcode == DATA && /* and got a data block */ 7571592Srgrimes block == dp->th_block) { /* then my last ack was lost */ 7581592Srgrimes (void) send(peer, ackbuf, 4, 0); /* resend final ack */ 7591592Srgrimes } 7601592Srgrimesabort: 7611592Srgrimes return; 7621592Srgrimes} 7631592Srgrimes 7641592Srgrimesstruct errmsg { 7651592Srgrimes int e_code; 766112452Sdwmalone const char *e_msg; 7671592Srgrimes} errmsgs[] = { 7681592Srgrimes { EUNDEF, "Undefined error code" }, 7691592Srgrimes { ENOTFOUND, "File not found" }, 7701592Srgrimes { EACCESS, "Access violation" }, 7711592Srgrimes { ENOSPACE, "Disk full or allocation exceeded" }, 7721592Srgrimes { EBADOP, "Illegal TFTP operation" }, 7731592Srgrimes { EBADID, "Unknown transfer ID" }, 7741592Srgrimes { EEXISTS, "File already exists" }, 7751592Srgrimes { ENOUSER, "No such user" }, 77684047Sobrien { EOPTNEG, "Option negotiation" }, 7771592Srgrimes { -1, 0 } 7781592Srgrimes}; 7791592Srgrimes 780112452Sdwmalonestatic const char * 78190333Simperrtomsg(int error) 7821592Srgrimes{ 783112452Sdwmalone static char ebuf[20]; 78490333Simp struct errmsg *pe; 7851592Srgrimes if (error == 0) 7861592Srgrimes return "success"; 7871592Srgrimes for (pe = errmsgs; pe->e_code >= 0; pe++) 7881592Srgrimes if (pe->e_code == error) 7891592Srgrimes return pe->e_msg; 790112452Sdwmalone snprintf(ebuf, sizeof(buf), "error %d", error); 791112452Sdwmalone return ebuf; 7921592Srgrimes} 7931592Srgrimes 7941592Srgrimes/* 7951592Srgrimes * Send a nak packet (error message). 7961592Srgrimes * Error code passed in is one of the 7971592Srgrimes * standard TFTP codes, or a UNIX errno 7981592Srgrimes * offset by 100. 7991592Srgrimes */ 8001592Srgrimesstatic void 80190333Simpnak(int error) 8021592Srgrimes{ 80390333Simp struct tftphdr *tp; 8041592Srgrimes int length; 80590333Simp struct errmsg *pe; 8061592Srgrimes 8071592Srgrimes tp = (struct tftphdr *)buf; 8081592Srgrimes tp->th_opcode = htons((u_short)ERROR); 8091592Srgrimes tp->th_code = htons((u_short)error); 8101592Srgrimes for (pe = errmsgs; pe->e_code >= 0; pe++) 8111592Srgrimes if (pe->e_code == error) 8121592Srgrimes break; 8131592Srgrimes if (pe->e_code < 0) { 8141592Srgrimes pe->e_msg = strerror(error - 100); 8151592Srgrimes tp->th_code = EUNDEF; /* set 'undef' errorcode */ 8161592Srgrimes } 8171592Srgrimes strcpy(tp->th_msg, pe->e_msg); 8181592Srgrimes length = strlen(pe->e_msg); 8191592Srgrimes tp->th_msg[length] = '\0'; 8201592Srgrimes length += 5; 8211592Srgrimes if (send(peer, buf, length, 0) != length) 82231512Scharnier syslog(LOG_ERR, "nak: %m"); 8231592Srgrimes} 82484047Sobrien 82594443Sume/* translate IPv4 mapped IPv6 address to IPv4 address */ 82694443Sumestatic void 82794443Sumeunmappedaddr(struct sockaddr_in6 *sin6) 82894443Sume{ 82995496Sume struct sockaddr_in *sin4; 83095496Sume u_int32_t addr; 83195496Sume int port; 83294443Sume 83395496Sume if (sin6->sin6_family != AF_INET6 || 83495496Sume !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 83595496Sume return; 83695496Sume sin4 = (struct sockaddr_in *)sin6; 83795496Sume addr = *(u_int32_t *)&sin6->sin6_addr.s6_addr[12]; 83895496Sume port = sin6->sin6_port; 83995496Sume memset(sin4, 0, sizeof(struct sockaddr_in)); 84095496Sume sin4->sin_addr.s_addr = addr; 84195496Sume sin4->sin_port = port; 84295496Sume sin4->sin_family = AF_INET; 84395496Sume sin4->sin_len = sizeof(struct sockaddr_in); 84494443Sume} 84594443Sume 84684047Sobrien/* 84784047Sobrien * Send an oack packet (option acknowledgement). 84884047Sobrien */ 84984047Sobrienstatic void 85090333Simpoack(void) 85184047Sobrien{ 85284047Sobrien struct tftphdr *tp, *ap; 85384047Sobrien int size, i, n; 85484047Sobrien char *bp; 85584047Sobrien 85684047Sobrien tp = (struct tftphdr *)buf; 85784047Sobrien bp = buf + 2; 85884047Sobrien size = sizeof(buf) - 2; 85984047Sobrien tp->th_opcode = htons((u_short)OACK); 86084047Sobrien for (i = 0; options[i].o_type != NULL; i++) { 86184047Sobrien if (options[i].o_request) { 86284047Sobrien n = snprintf(bp, size, "%s%c%d", options[i].o_type, 86384047Sobrien 0, options[i].o_reply); 86484047Sobrien bp += n+1; 86584047Sobrien size -= n+1; 86684047Sobrien if (size < 0) { 86784047Sobrien syslog(LOG_ERR, "oack: buffer overflow"); 86884047Sobrien exit(1); 86984047Sobrien } 87084047Sobrien } 87184047Sobrien } 87284047Sobrien size = bp - buf; 87384047Sobrien ap = (struct tftphdr *)ackbuf; 87484047Sobrien signal(SIGALRM, timer); 87584047Sobrien timeouts = 0; 87684047Sobrien 87784047Sobrien (void)setjmp(timeoutbuf); 87884047Sobrien if (send(peer, buf, size, 0) != size) { 87984047Sobrien syslog(LOG_INFO, "oack: %m"); 88084047Sobrien exit(1); 88184047Sobrien } 88284047Sobrien 88384047Sobrien for (;;) { 88484047Sobrien alarm(rexmtval); 88584047Sobrien n = recv(peer, ackbuf, sizeof (ackbuf), 0); 88684047Sobrien alarm(0); 88784047Sobrien if (n < 0) { 88884047Sobrien syslog(LOG_ERR, "recv: %m"); 88984047Sobrien exit(1); 89084047Sobrien } 89184047Sobrien ap->th_opcode = ntohs((u_short)ap->th_opcode); 89284047Sobrien ap->th_block = ntohs((u_short)ap->th_block); 89384047Sobrien if (ap->th_opcode == ERROR) 89484047Sobrien exit(1); 89584047Sobrien if (ap->th_opcode == ACK && ap->th_block == 0) 89684047Sobrien break; 89784047Sobrien } 89884047Sobrien} 899