tftpd.c revision 130834
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 130834 2004-06-21 08:01:16Z brian $"; 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 332130834Sbrianstatic void 333130834Sbrianreduce_path(char *fn) 334130834Sbrian{ 335130834Sbrian char *slash, *ptr; 336130834Sbrian 337130834Sbrian /* Reduce all "/+./" to "/" (just in case we've got "/./../" later */ 338130834Sbrian while ((slash = strstr(fn, "/./")) != NULL) { 339130834Sbrian for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--) 340130834Sbrian ; 341130834Sbrian slash += 2; 342130834Sbrian while (*slash) 343130834Sbrian *++ptr = *++slash; 344130834Sbrian } 345130834Sbrian 346130834Sbrian /* Now reduce all "/something/+../" to "/" */ 347130834Sbrian while ((slash = strstr(fn, "/../")) != NULL) { 348130834Sbrian if (slash == fn) 349130834Sbrian break; 350130834Sbrian for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--) 351130834Sbrian ; 352130834Sbrian for (ptr--; ptr >= fn; ptr--) 353130834Sbrian if (*ptr == '/') 354130834Sbrian break; 355130834Sbrian if (ptr < fn) 356130834Sbrian break; 357130834Sbrian slash += 3; 358130834Sbrian while (*slash) 359130834Sbrian *++ptr = *++slash; 360130834Sbrian } 361130834Sbrian} 362130834Sbrian 3631592Srgrimesstruct formats; 36490333Simpint validate_access(char **, int); 36590333Simpvoid xmitfile(struct formats *); 36690333Simpvoid recvfile(struct formats *); 3671592Srgrimes 3681592Srgrimesstruct formats { 369112452Sdwmalone const char *f_mode; 37090333Simp int (*f_validate)(char **, int); 37190333Simp void (*f_send)(struct formats *); 37290333Simp void (*f_recv)(struct formats *); 3731592Srgrimes int f_convert; 3741592Srgrimes} formats[] = { 37540765Sdg { "netascii", validate_access, xmitfile, recvfile, 1 }, 37640765Sdg { "octet", validate_access, xmitfile, recvfile, 0 }, 3771592Srgrimes#ifdef notdef 3781592Srgrimes { "mail", validate_user, sendmail, recvmail, 1 }, 3791592Srgrimes#endif 380112452Sdwmalone { 0, NULL, NULL, NULL, 0 } 3811592Srgrimes}; 3821592Srgrimes 38384047Sobrienstruct options { 384112452Sdwmalone const char *o_type; 38584047Sobrien char *o_request; 38684047Sobrien int o_reply; /* turn into union if need be */ 38784047Sobrien} options[] = { 388112452Sdwmalone { "tsize", NULL, 0 }, /* OPT_TSIZE */ 389112452Sdwmalone { "timeout", NULL, 0 }, /* OPT_TIMEOUT */ 390112452Sdwmalone { NULL, NULL, 0 } 39184047Sobrien}; 39284047Sobrien 39384047Sobrienenum opt_enum { 39484047Sobrien OPT_TSIZE = 0, 39584047Sobrien OPT_TIMEOUT, 39684047Sobrien}; 39784047Sobrien 3981592Srgrimes/* 3991592Srgrimes * Handle initial connection protocol. 4001592Srgrimes */ 4011592Srgrimesvoid 40290333Simptftp(struct tftphdr *tp, int size) 4031592Srgrimes{ 40490333Simp char *cp; 40584047Sobrien int i, first = 1, has_options = 0, ecode; 40690333Simp struct formats *pf; 40784047Sobrien char *filename, *mode, *option, *ccp; 408130834Sbrian char fnbuf[PATH_MAX], resolved_fnbuf[PATH_MAX]; 4091592Srgrimes 410122916Ssobomax cp = tp->th_stuff; 4111592Srgrimesagain: 4121592Srgrimes while (cp < buf + size) { 4131592Srgrimes if (*cp == '\0') 4141592Srgrimes break; 4151592Srgrimes cp++; 4161592Srgrimes } 4171592Srgrimes if (*cp != '\0') { 4181592Srgrimes nak(EBADOP); 4191592Srgrimes exit(1); 4201592Srgrimes } 421122916Ssobomax i = cp - tp->th_stuff; 422122916Ssobomax if (i >= sizeof(fnbuf)) { 423122916Ssobomax nak(EBADOP); 424122916Ssobomax exit(1); 425122916Ssobomax } 426122916Ssobomax memcpy(fnbuf, tp->th_stuff, i); 427122916Ssobomax fnbuf[i] = '\0'; 428130834Sbrian reduce_path(fnbuf); 429122916Ssobomax filename = fnbuf; 4301592Srgrimes if (first) { 4311592Srgrimes mode = ++cp; 4321592Srgrimes first = 0; 4331592Srgrimes goto again; 4341592Srgrimes } 4351592Srgrimes for (cp = mode; *cp; cp++) 4361592Srgrimes if (isupper(*cp)) 4371592Srgrimes *cp = tolower(*cp); 4381592Srgrimes for (pf = formats; pf->f_mode; pf++) 4391592Srgrimes if (strcmp(pf->f_mode, mode) == 0) 4401592Srgrimes break; 4411592Srgrimes if (pf->f_mode == 0) { 4421592Srgrimes nak(EBADOP); 4431592Srgrimes exit(1); 4441592Srgrimes } 44584047Sobrien while (++cp < buf + size) { 44684047Sobrien for (i = 2, ccp = cp; i > 0; ccp++) { 44784047Sobrien if (ccp >= buf + size) { 44886765Sbenno /* 44986765Sbenno * Don't reject the request, just stop trying 45086765Sbenno * to parse the option and get on with it. 45186765Sbenno * Some Apple OpenFirmware versions have 45286765Sbenno * trailing garbage on the end of otherwise 45386765Sbenno * valid requests. 45486765Sbenno */ 45586765Sbenno goto option_fail; 45684047Sobrien } else if (*ccp == '\0') 45784047Sobrien i--; 45884047Sobrien } 45984047Sobrien for (option = cp; *cp; cp++) 46084047Sobrien if (isupper(*cp)) 46184047Sobrien *cp = tolower(*cp); 46284047Sobrien for (i = 0; options[i].o_type != NULL; i++) 46384047Sobrien if (strcmp(option, options[i].o_type) == 0) { 46484047Sobrien options[i].o_request = ++cp; 46584047Sobrien has_options = 1; 46684047Sobrien } 46784047Sobrien cp = ccp-1; 46884047Sobrien } 46984047Sobrien 47086765Sbennooption_fail: 47184047Sobrien if (options[OPT_TIMEOUT].o_request) { 47284047Sobrien int to = atoi(options[OPT_TIMEOUT].o_request); 47384047Sobrien if (to < 1 || to > 255) { 47484047Sobrien nak(EBADOP); 47584047Sobrien exit(1); 47684047Sobrien } 47784047Sobrien else if (to <= max_rexmtval) 47884047Sobrien options[OPT_TIMEOUT].o_reply = rexmtval = to; 47984047Sobrien else 48084047Sobrien options[OPT_TIMEOUT].o_request = NULL; 48184047Sobrien } 48284047Sobrien 4831592Srgrimes ecode = (*pf->f_validate)(&filename, tp->th_opcode); 484130834Sbrian if (has_options && ecode == 0) 48584047Sobrien oack(); 4861592Srgrimes if (logging) { 48794443Sume char hbuf[NI_MAXHOST]; 48845393Sbrian 48994443Sume getnameinfo((struct sockaddr *)&from, from.ss_len, 49094443Sume hbuf, sizeof(hbuf), NULL, 0, 49194443Sume NI_WITHSCOPEID); 49294443Sume syslog(LOG_INFO, "%s: %s request for %s: %s", hbuf, 4931592Srgrimes tp->th_opcode == WRQ ? "write" : "read", 4941592Srgrimes filename, errtomsg(ecode)); 4951592Srgrimes } 4961592Srgrimes if (ecode) { 4971592Srgrimes /* 4981592Srgrimes * Avoid storms of naks to a RRQ broadcast for a relative 4991592Srgrimes * bootfile pathname from a diskless Sun. 5001592Srgrimes */ 5011592Srgrimes if (suppress_naks && *filename != '/' && ecode == ENOTFOUND) 5021592Srgrimes exit(0); 5031592Srgrimes nak(ecode); 5041592Srgrimes exit(1); 5051592Srgrimes } 5061592Srgrimes if (tp->th_opcode == WRQ) 5071592Srgrimes (*pf->f_recv)(pf); 5081592Srgrimes else 5091592Srgrimes (*pf->f_send)(pf); 5101592Srgrimes exit(0); 5111592Srgrimes} 5121592Srgrimes 5131592Srgrimes 5141592SrgrimesFILE *file; 5151592Srgrimes 5161592Srgrimes/* 5171592Srgrimes * Validate file access. Since we 5181592Srgrimes * have no uid or gid, for now require 5191592Srgrimes * file to exist and be publicly 5201592Srgrimes * readable/writable. 5211592Srgrimes * If we were invoked with arguments 5221592Srgrimes * from inetd then the file must also be 5231592Srgrimes * in one of the given directory prefixes. 5241592Srgrimes * Note also, full path name must be 5251592Srgrimes * given as we have no login directory. 5261592Srgrimes */ 5271592Srgrimesint 52890333Simpvalidate_access(char **filep, int mode) 5291592Srgrimes{ 5301592Srgrimes struct stat stbuf; 5311592Srgrimes int fd; 5321592Srgrimes struct dirlist *dirp; 5331592Srgrimes static char pathname[MAXPATHLEN]; 5341592Srgrimes char *filename = *filep; 5351592Srgrimes 5361592Srgrimes /* 5371592Srgrimes * Prevent tricksters from getting around the directory restrictions 5381592Srgrimes */ 5391592Srgrimes if (strstr(filename, "/../")) 5401592Srgrimes return (EACCESS); 5411592Srgrimes 5421592Srgrimes if (*filename == '/') { 5431592Srgrimes /* 5441592Srgrimes * Allow the request if it's in one of the approved locations. 5451592Srgrimes * Special case: check the null prefix ("/") by looking 5461592Srgrimes * for length = 1 and relying on the arg. processing that 5471592Srgrimes * it's a /. 5481592Srgrimes */ 5491592Srgrimes for (dirp = dirs; dirp->name != NULL; dirp++) { 5501592Srgrimes if (dirp->len == 1 || 5511592Srgrimes (!strncmp(filename, dirp->name, dirp->len) && 5521592Srgrimes filename[dirp->len] == '/')) 5531592Srgrimes break; 5541592Srgrimes } 5551592Srgrimes /* If directory list is empty, allow access to any file */ 5561592Srgrimes if (dirp->name == NULL && dirp != dirs) 5571592Srgrimes return (EACCESS); 5581592Srgrimes if (stat(filename, &stbuf) < 0) 5591592Srgrimes return (errno == ENOENT ? ENOTFOUND : EACCESS); 5601592Srgrimes if ((stbuf.st_mode & S_IFMT) != S_IFREG) 5611592Srgrimes return (ENOTFOUND); 5621592Srgrimes if (mode == RRQ) { 5631592Srgrimes if ((stbuf.st_mode & S_IROTH) == 0) 5641592Srgrimes return (EACCESS); 5651592Srgrimes } else { 5661592Srgrimes if ((stbuf.st_mode & S_IWOTH) == 0) 5671592Srgrimes return (EACCESS); 5681592Srgrimes } 5691592Srgrimes } else { 5701592Srgrimes int err; 5711592Srgrimes 5721592Srgrimes /* 5731592Srgrimes * Relative file name: search the approved locations for it. 5746750Sjkh * Don't allow write requests that avoid directory 5751592Srgrimes * restrictions. 5761592Srgrimes */ 5771592Srgrimes 5786750Sjkh if (!strncmp(filename, "../", 3)) 5791592Srgrimes return (EACCESS); 5801592Srgrimes 5811592Srgrimes /* 5821592Srgrimes * If the file exists in one of the directories and isn't 5831592Srgrimes * readable, continue looking. However, change the error code 5841592Srgrimes * to give an indication that the file exists. 5851592Srgrimes */ 5861592Srgrimes err = ENOTFOUND; 5871592Srgrimes for (dirp = dirs; dirp->name != NULL; dirp++) { 58824193Simp snprintf(pathname, sizeof(pathname), "%s/%s", 58924193Simp dirp->name, filename); 5901592Srgrimes if (stat(pathname, &stbuf) == 0 && 5911592Srgrimes (stbuf.st_mode & S_IFMT) == S_IFREG) { 5921592Srgrimes if ((stbuf.st_mode & S_IROTH) != 0) { 5931592Srgrimes break; 5941592Srgrimes } 5951592Srgrimes err = EACCESS; 5961592Srgrimes } 5971592Srgrimes } 598129680Smdodd if (dirp->name != NULL) 599129680Smdodd *filep = filename = pathname; 600129680Smdodd else if (mode == RRQ) 6011592Srgrimes return (err); 6021592Srgrimes } 60384047Sobrien if (options[OPT_TSIZE].o_request) { 60484047Sobrien if (mode == RRQ) 60584047Sobrien options[OPT_TSIZE].o_reply = stbuf.st_size; 60684047Sobrien else 60784047Sobrien /* XXX Allows writes of all sizes. */ 60884047Sobrien options[OPT_TSIZE].o_reply = 60984047Sobrien atoi(options[OPT_TSIZE].o_request); 61084047Sobrien } 611129680Smdodd if (mode == RRQ) 612129680Smdodd fd = open(filename, O_RDONLY); 613129680Smdodd else { 614129680Smdodd if (create_new) 615129680Smdodd fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0666); 616129680Smdodd else 617129680Smdodd fd = open(filename, O_WRONLY|O_TRUNC); 618129680Smdodd } 6191592Srgrimes if (fd < 0) 6201592Srgrimes return (errno + 100); 6211592Srgrimes file = fdopen(fd, (mode == RRQ)? "r":"w"); 6221592Srgrimes if (file == NULL) { 623129683Smdodd close(fd); 624129683Smdodd return (errno + 100); 6251592Srgrimes } 6261592Srgrimes return (0); 6271592Srgrimes} 6281592Srgrimes 62984047Sobrienint timeouts; 6301592Srgrimesjmp_buf timeoutbuf; 6311592Srgrimes 6321592Srgrimesvoid 63390333Simptimer(int sig __unused) 6341592Srgrimes{ 63584047Sobrien if (++timeouts > MAX_TIMEOUTS) 6361592Srgrimes exit(1); 6371592Srgrimes longjmp(timeoutbuf, 1); 6381592Srgrimes} 6391592Srgrimes 6401592Srgrimes/* 6411592Srgrimes * Send the requested file. 6421592Srgrimes */ 6431592Srgrimesvoid 64490333Simpxmitfile(struct formats *pf) 6451592Srgrimes{ 646112452Sdwmalone struct tftphdr *dp; 64790333Simp struct tftphdr *ap; /* ack packet */ 64890333Simp int size, n; 64971926Sasmodai volatile unsigned short block; 6501592Srgrimes 6511592Srgrimes signal(SIGALRM, timer); 6521592Srgrimes dp = r_init(); 6531592Srgrimes ap = (struct tftphdr *)ackbuf; 6541592Srgrimes block = 1; 6551592Srgrimes do { 6561592Srgrimes size = readit(file, &dp, pf->f_convert); 6571592Srgrimes if (size < 0) { 6581592Srgrimes nak(errno + 100); 6591592Srgrimes goto abort; 6601592Srgrimes } 6611592Srgrimes dp->th_opcode = htons((u_short)DATA); 6621592Srgrimes dp->th_block = htons((u_short)block); 66384047Sobrien timeouts = 0; 6641592Srgrimes (void)setjmp(timeoutbuf); 6651592Srgrimes 6661592Srgrimessend_data: 66794299Sambrisko { 66894299Sambrisko int i, t = 1; 66994299Sambrisko for (i = 0; ; i++){ 67094299Sambrisko if (send(peer, dp, size + 4, 0) != size + 4) { 67194299Sambrisko sleep(t); 67294299Sambrisko t = (t < 32) ? t<< 1 : t; 67394299Sambrisko if (i >= 12) { 67494299Sambrisko syslog(LOG_ERR, "write: %m"); 67594299Sambrisko goto abort; 67694299Sambrisko } 67794299Sambrisko } 67894299Sambrisko break; 67994299Sambrisko } 6801592Srgrimes } 6811592Srgrimes read_ahead(file, pf->f_convert); 6821592Srgrimes for ( ; ; ) { 6831592Srgrimes alarm(rexmtval); /* read the ack */ 6841592Srgrimes n = recv(peer, ackbuf, sizeof (ackbuf), 0); 6851592Srgrimes alarm(0); 6861592Srgrimes if (n < 0) { 68731512Scharnier syslog(LOG_ERR, "read: %m"); 6881592Srgrimes goto abort; 6891592Srgrimes } 6901592Srgrimes ap->th_opcode = ntohs((u_short)ap->th_opcode); 6911592Srgrimes ap->th_block = ntohs((u_short)ap->th_block); 6921592Srgrimes 6931592Srgrimes if (ap->th_opcode == ERROR) 6941592Srgrimes goto abort; 6951592Srgrimes 6961592Srgrimes if (ap->th_opcode == ACK) { 6971592Srgrimes if (ap->th_block == block) 6981592Srgrimes break; 6991592Srgrimes /* Re-synchronize with the other side */ 7001592Srgrimes (void) synchnet(peer); 7011592Srgrimes if (ap->th_block == (block -1)) 7021592Srgrimes goto send_data; 7031592Srgrimes } 7041592Srgrimes 7051592Srgrimes } 7061592Srgrimes block++; 7071592Srgrimes } while (size == SEGSIZE); 7081592Srgrimesabort: 7091592Srgrimes (void) fclose(file); 7101592Srgrimes} 7111592Srgrimes 7121592Srgrimesvoid 71390333Simpjustquit(int sig __unused) 7141592Srgrimes{ 7151592Srgrimes exit(0); 7161592Srgrimes} 7171592Srgrimes 7181592Srgrimes 7191592Srgrimes/* 7201592Srgrimes * Receive a file. 7211592Srgrimes */ 7221592Srgrimesvoid 72390333Simprecvfile(struct formats *pf) 7241592Srgrimes{ 725112452Sdwmalone struct tftphdr *dp; 72690333Simp struct tftphdr *ap; /* ack buffer */ 72790333Simp int n, size; 72871926Sasmodai volatile unsigned short block; 7291592Srgrimes 7301592Srgrimes signal(SIGALRM, timer); 7311592Srgrimes dp = w_init(); 7321592Srgrimes ap = (struct tftphdr *)ackbuf; 7331592Srgrimes block = 0; 7341592Srgrimes do { 73584047Sobrien timeouts = 0; 7361592Srgrimes ap->th_opcode = htons((u_short)ACK); 7371592Srgrimes ap->th_block = htons((u_short)block); 7381592Srgrimes block++; 7391592Srgrimes (void) setjmp(timeoutbuf); 7401592Srgrimessend_ack: 7411592Srgrimes if (send(peer, ackbuf, 4, 0) != 4) { 74231512Scharnier syslog(LOG_ERR, "write: %m"); 7431592Srgrimes goto abort; 7441592Srgrimes } 7451592Srgrimes write_behind(file, pf->f_convert); 7461592Srgrimes for ( ; ; ) { 7471592Srgrimes alarm(rexmtval); 7481592Srgrimes n = recv(peer, dp, PKTSIZE, 0); 7491592Srgrimes alarm(0); 7501592Srgrimes if (n < 0) { /* really? */ 75131512Scharnier syslog(LOG_ERR, "read: %m"); 7521592Srgrimes goto abort; 7531592Srgrimes } 7541592Srgrimes dp->th_opcode = ntohs((u_short)dp->th_opcode); 7551592Srgrimes dp->th_block = ntohs((u_short)dp->th_block); 7561592Srgrimes if (dp->th_opcode == ERROR) 7571592Srgrimes goto abort; 7581592Srgrimes if (dp->th_opcode == DATA) { 7591592Srgrimes if (dp->th_block == block) { 7601592Srgrimes break; /* normal */ 7611592Srgrimes } 7621592Srgrimes /* Re-synchronize with the other side */ 7631592Srgrimes (void) synchnet(peer); 7641592Srgrimes if (dp->th_block == (block-1)) 7651592Srgrimes goto send_ack; /* rexmit */ 7661592Srgrimes } 7671592Srgrimes } 7681592Srgrimes /* size = write(file, dp->th_data, n - 4); */ 7691592Srgrimes size = writeit(file, &dp, n - 4, pf->f_convert); 7701592Srgrimes if (size != (n-4)) { /* ahem */ 7711592Srgrimes if (size < 0) nak(errno + 100); 7721592Srgrimes else nak(ENOSPACE); 7731592Srgrimes goto abort; 7741592Srgrimes } 7751592Srgrimes } while (size == SEGSIZE); 7761592Srgrimes write_behind(file, pf->f_convert); 7771592Srgrimes (void) fclose(file); /* close data file */ 7781592Srgrimes 7791592Srgrimes ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */ 7801592Srgrimes ap->th_block = htons((u_short)(block)); 7811592Srgrimes (void) send(peer, ackbuf, 4, 0); 7821592Srgrimes 7831592Srgrimes signal(SIGALRM, justquit); /* just quit on timeout */ 7841592Srgrimes alarm(rexmtval); 7851592Srgrimes n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */ 7861592Srgrimes alarm(0); 7871592Srgrimes if (n >= 4 && /* if read some data */ 7881592Srgrimes dp->th_opcode == DATA && /* and got a data block */ 7891592Srgrimes block == dp->th_block) { /* then my last ack was lost */ 7901592Srgrimes (void) send(peer, ackbuf, 4, 0); /* resend final ack */ 7911592Srgrimes } 7921592Srgrimesabort: 7931592Srgrimes return; 7941592Srgrimes} 7951592Srgrimes 7961592Srgrimesstruct errmsg { 7971592Srgrimes int e_code; 798112452Sdwmalone const char *e_msg; 7991592Srgrimes} errmsgs[] = { 8001592Srgrimes { EUNDEF, "Undefined error code" }, 8011592Srgrimes { ENOTFOUND, "File not found" }, 8021592Srgrimes { EACCESS, "Access violation" }, 8031592Srgrimes { ENOSPACE, "Disk full or allocation exceeded" }, 8041592Srgrimes { EBADOP, "Illegal TFTP operation" }, 8051592Srgrimes { EBADID, "Unknown transfer ID" }, 8061592Srgrimes { EEXISTS, "File already exists" }, 8071592Srgrimes { ENOUSER, "No such user" }, 80884047Sobrien { EOPTNEG, "Option negotiation" }, 8091592Srgrimes { -1, 0 } 8101592Srgrimes}; 8111592Srgrimes 812112452Sdwmalonestatic const char * 81390333Simperrtomsg(int error) 8141592Srgrimes{ 815112452Sdwmalone static char ebuf[20]; 81690333Simp struct errmsg *pe; 8171592Srgrimes if (error == 0) 8181592Srgrimes return "success"; 8191592Srgrimes for (pe = errmsgs; pe->e_code >= 0; pe++) 8201592Srgrimes if (pe->e_code == error) 8211592Srgrimes return pe->e_msg; 822112452Sdwmalone snprintf(ebuf, sizeof(buf), "error %d", error); 823112452Sdwmalone return ebuf; 8241592Srgrimes} 8251592Srgrimes 8261592Srgrimes/* 8271592Srgrimes * Send a nak packet (error message). 8281592Srgrimes * Error code passed in is one of the 8291592Srgrimes * standard TFTP codes, or a UNIX errno 8301592Srgrimes * offset by 100. 8311592Srgrimes */ 8321592Srgrimesstatic void 83390333Simpnak(int error) 8341592Srgrimes{ 83590333Simp struct tftphdr *tp; 8361592Srgrimes int length; 83790333Simp struct errmsg *pe; 8381592Srgrimes 8391592Srgrimes tp = (struct tftphdr *)buf; 8401592Srgrimes tp->th_opcode = htons((u_short)ERROR); 8411592Srgrimes tp->th_code = htons((u_short)error); 8421592Srgrimes for (pe = errmsgs; pe->e_code >= 0; pe++) 8431592Srgrimes if (pe->e_code == error) 8441592Srgrimes break; 8451592Srgrimes if (pe->e_code < 0) { 8461592Srgrimes pe->e_msg = strerror(error - 100); 8471592Srgrimes tp->th_code = EUNDEF; /* set 'undef' errorcode */ 8481592Srgrimes } 8491592Srgrimes strcpy(tp->th_msg, pe->e_msg); 8501592Srgrimes length = strlen(pe->e_msg); 8511592Srgrimes tp->th_msg[length] = '\0'; 8521592Srgrimes length += 5; 8531592Srgrimes if (send(peer, buf, length, 0) != length) 85431512Scharnier syslog(LOG_ERR, "nak: %m"); 8551592Srgrimes} 85684047Sobrien 85794443Sume/* translate IPv4 mapped IPv6 address to IPv4 address */ 85894443Sumestatic void 85994443Sumeunmappedaddr(struct sockaddr_in6 *sin6) 86094443Sume{ 86195496Sume struct sockaddr_in *sin4; 86295496Sume u_int32_t addr; 86395496Sume int port; 86494443Sume 86595496Sume if (sin6->sin6_family != AF_INET6 || 86695496Sume !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 86795496Sume return; 86895496Sume sin4 = (struct sockaddr_in *)sin6; 86995496Sume addr = *(u_int32_t *)&sin6->sin6_addr.s6_addr[12]; 87095496Sume port = sin6->sin6_port; 87195496Sume memset(sin4, 0, sizeof(struct sockaddr_in)); 87295496Sume sin4->sin_addr.s_addr = addr; 87395496Sume sin4->sin_port = port; 87495496Sume sin4->sin_family = AF_INET; 87595496Sume sin4->sin_len = sizeof(struct sockaddr_in); 87694443Sume} 87794443Sume 87884047Sobrien/* 87984047Sobrien * Send an oack packet (option acknowledgement). 88084047Sobrien */ 88184047Sobrienstatic void 88290333Simpoack(void) 88384047Sobrien{ 88484047Sobrien struct tftphdr *tp, *ap; 88584047Sobrien int size, i, n; 88684047Sobrien char *bp; 88784047Sobrien 88884047Sobrien tp = (struct tftphdr *)buf; 88984047Sobrien bp = buf + 2; 89084047Sobrien size = sizeof(buf) - 2; 89184047Sobrien tp->th_opcode = htons((u_short)OACK); 89284047Sobrien for (i = 0; options[i].o_type != NULL; i++) { 89384047Sobrien if (options[i].o_request) { 89484047Sobrien n = snprintf(bp, size, "%s%c%d", options[i].o_type, 89584047Sobrien 0, options[i].o_reply); 89684047Sobrien bp += n+1; 89784047Sobrien size -= n+1; 89884047Sobrien if (size < 0) { 89984047Sobrien syslog(LOG_ERR, "oack: buffer overflow"); 90084047Sobrien exit(1); 90184047Sobrien } 90284047Sobrien } 90384047Sobrien } 90484047Sobrien size = bp - buf; 90584047Sobrien ap = (struct tftphdr *)ackbuf; 90684047Sobrien signal(SIGALRM, timer); 90784047Sobrien timeouts = 0; 90884047Sobrien 90984047Sobrien (void)setjmp(timeoutbuf); 91084047Sobrien if (send(peer, buf, size, 0) != size) { 91184047Sobrien syslog(LOG_INFO, "oack: %m"); 91284047Sobrien exit(1); 91384047Sobrien } 91484047Sobrien 91584047Sobrien for (;;) { 91684047Sobrien alarm(rexmtval); 91784047Sobrien n = recv(peer, ackbuf, sizeof (ackbuf), 0); 91884047Sobrien alarm(0); 91984047Sobrien if (n < 0) { 92084047Sobrien syslog(LOG_ERR, "recv: %m"); 92184047Sobrien exit(1); 92284047Sobrien } 92384047Sobrien ap->th_opcode = ntohs((u_short)ap->th_opcode); 92484047Sobrien ap->th_block = ntohs((u_short)ap->th_block); 92584047Sobrien if (ap->th_opcode == ERROR) 92684047Sobrien exit(1); 92784047Sobrien if (ap->th_opcode == ACK && ap->th_block == 0) 92884047Sobrien break; 92984047Sobrien } 93084047Sobrien} 931