1ea022d16SRodney W. Grimes /* 2ea022d16SRodney W. Grimes * Copyright (c) 1983, 1993 3ea022d16SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4ea022d16SRodney W. Grimes * 5ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions 7ea022d16SRodney W. Grimes * are met: 8ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13ea022d16SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14ea022d16SRodney W. Grimes * must display the following acknowledgement: 15ea022d16SRodney W. Grimes * This product includes software developed by the University of 16ea022d16SRodney W. Grimes * California, Berkeley and its contributors. 17ea022d16SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 19ea022d16SRodney W. Grimes * without specific prior written permission. 20ea022d16SRodney W. Grimes * 21ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ea022d16SRodney W. Grimes * SUCH DAMAGE. 32ea022d16SRodney W. Grimes */ 33ea022d16SRodney W. Grimes 34ea022d16SRodney W. Grimes #ifndef lint 35a8faeabcSPhilippe Charnier static const char copyright[] = 36ea022d16SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\ 37ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 38ea022d16SRodney W. Grimes #endif /* not lint */ 39ea022d16SRodney W. Grimes 40ea022d16SRodney W. Grimes #ifndef lint 41a8faeabcSPhilippe Charnier #if 0 42ea022d16SRodney W. Grimes static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93"; 43a8faeabcSPhilippe Charnier #endif 44a8faeabcSPhilippe Charnier static const char rcsid[] = 45a8faeabcSPhilippe Charnier "$Id$"; 46ea022d16SRodney W. Grimes #endif /* not lint */ 47ea022d16SRodney W. Grimes 48ea022d16SRodney W. Grimes /* 49ea022d16SRodney W. Grimes * Trivial file transfer protocol server. 50ea022d16SRodney W. Grimes * 51ea022d16SRodney W. Grimes * This version includes many modifications by Jim Guyton 52ea022d16SRodney W. Grimes * <guyton@rand-unix>. 53ea022d16SRodney W. Grimes */ 54ea022d16SRodney W. Grimes 55ea022d16SRodney W. Grimes #include <sys/param.h> 56ea022d16SRodney W. Grimes #include <sys/ioctl.h> 57ea022d16SRodney W. Grimes #include <sys/stat.h> 58ea022d16SRodney W. Grimes #include <sys/socket.h> 598ea31785SWarner Losh #include <sys/types.h> 60ea022d16SRodney W. Grimes 61ea022d16SRodney W. Grimes #include <netinet/in.h> 62ea022d16SRodney W. Grimes #include <arpa/tftp.h> 63ea022d16SRodney W. Grimes #include <arpa/inet.h> 64ea022d16SRodney W. Grimes 65ea022d16SRodney W. Grimes #include <ctype.h> 66ea022d16SRodney W. Grimes #include <errno.h> 67ea022d16SRodney W. Grimes #include <fcntl.h> 68ea022d16SRodney W. Grimes #include <netdb.h> 69a8faeabcSPhilippe Charnier #include <pwd.h> 70ea022d16SRodney W. Grimes #include <setjmp.h> 71ea022d16SRodney W. Grimes #include <signal.h> 72ea022d16SRodney W. Grimes #include <stdio.h> 73ea022d16SRodney W. Grimes #include <stdlib.h> 74ea022d16SRodney W. Grimes #include <string.h> 75ea022d16SRodney W. Grimes #include <syslog.h> 76ea022d16SRodney W. Grimes #include <unistd.h> 77ea022d16SRodney W. Grimes 78ea022d16SRodney W. Grimes #include "tftpsubs.h" 79ea022d16SRodney W. Grimes 80ea022d16SRodney W. Grimes #define TIMEOUT 5 81ea022d16SRodney W. Grimes 82ea022d16SRodney W. Grimes int peer; 83ea022d16SRodney W. Grimes int rexmtval = TIMEOUT; 84ea022d16SRodney W. Grimes int maxtimeout = 5*TIMEOUT; 85ea022d16SRodney W. Grimes 86ea022d16SRodney W. Grimes #define PKTSIZE SEGSIZE+4 87ea022d16SRodney W. Grimes char buf[PKTSIZE]; 88ea022d16SRodney W. Grimes char ackbuf[PKTSIZE]; 89ea022d16SRodney W. Grimes struct sockaddr_in from; 90ea022d16SRodney W. Grimes int fromlen; 91ea022d16SRodney W. Grimes 92ea022d16SRodney W. Grimes void tftp __P((struct tftphdr *, int)); 93ea022d16SRodney W. Grimes 94ea022d16SRodney W. Grimes /* 95ea022d16SRodney W. Grimes * Null-terminated directory prefix list for absolute pathname requests and 96ea022d16SRodney W. Grimes * search list for relative pathname requests. 97ea022d16SRodney W. Grimes * 98ea022d16SRodney W. Grimes * MAXDIRS should be at least as large as the number of arguments that 99ea022d16SRodney W. Grimes * inetd allows (currently 20). 100ea022d16SRodney W. Grimes */ 101ea022d16SRodney W. Grimes #define MAXDIRS 20 102ea022d16SRodney W. Grimes static struct dirlist { 103ea022d16SRodney W. Grimes char *name; 104ea022d16SRodney W. Grimes int len; 105ea022d16SRodney W. Grimes } dirs[MAXDIRS+1]; 106ea022d16SRodney W. Grimes static int suppress_naks; 107ea022d16SRodney W. Grimes static int logging; 108ea022d16SRodney W. Grimes 109ea022d16SRodney W. Grimes static char *errtomsg __P((int)); 110ea022d16SRodney W. Grimes static void nak __P((int)); 111ea022d16SRodney W. Grimes static char *verifyhost __P((struct sockaddr_in *)); 112ea022d16SRodney W. Grimes 113ea022d16SRodney W. Grimes int 114ea022d16SRodney W. Grimes main(argc, argv) 115ea022d16SRodney W. Grimes int argc; 116ea022d16SRodney W. Grimes char *argv[]; 117ea022d16SRodney W. Grimes { 118ea022d16SRodney W. Grimes register struct tftphdr *tp; 119ea022d16SRodney W. Grimes register int n; 120ea022d16SRodney W. Grimes int ch, on; 121ea022d16SRodney W. Grimes struct sockaddr_in sin; 1228ea31785SWarner Losh char *chroot_dir = NULL; 1238ea31785SWarner Losh struct passwd *nobody; 124ea022d16SRodney W. Grimes 125ea022d16SRodney W. Grimes openlog("tftpd", LOG_PID, LOG_FTP); 12691477cc4SWarner Losh while ((ch = getopt(argc, argv, "lns:")) != -1) { 127ea022d16SRodney W. Grimes switch (ch) { 128ea022d16SRodney W. Grimes case 'l': 129ea022d16SRodney W. Grimes logging = 1; 130ea022d16SRodney W. Grimes break; 131ea022d16SRodney W. Grimes case 'n': 132ea022d16SRodney W. Grimes suppress_naks = 1; 133ea022d16SRodney W. Grimes break; 1348ea31785SWarner Losh case 's': 1358ea31785SWarner Losh chroot_dir = optarg; 1368ea31785SWarner Losh break; 137ea022d16SRodney W. Grimes default: 138ea022d16SRodney W. Grimes syslog(LOG_WARNING, "ignoring unknown option -%c", ch); 139ea022d16SRodney W. Grimes } 140ea022d16SRodney W. Grimes } 141ea022d16SRodney W. Grimes if (optind < argc) { 142ea022d16SRodney W. Grimes struct dirlist *dirp; 143ea022d16SRodney W. Grimes 144ea022d16SRodney W. Grimes /* Get list of directory prefixes. Skip relative pathnames. */ 145ea022d16SRodney W. Grimes for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS]; 146ea022d16SRodney W. Grimes optind++) { 147ea022d16SRodney W. Grimes if (argv[optind][0] == '/') { 148ea022d16SRodney W. Grimes dirp->name = argv[optind]; 149ea022d16SRodney W. Grimes dirp->len = strlen(dirp->name); 150ea022d16SRodney W. Grimes dirp++; 151ea022d16SRodney W. Grimes } 152ea022d16SRodney W. Grimes } 153ea022d16SRodney W. Grimes } 1548ea31785SWarner Losh else if (chroot_dir) { 1558ea31785SWarner Losh dirs->name = "/"; 1568ea31785SWarner Losh dirs->len = 1; 1578ea31785SWarner Losh } 158ea022d16SRodney W. Grimes 159ea022d16SRodney W. Grimes on = 1; 160ea022d16SRodney W. Grimes if (ioctl(0, FIONBIO, &on) < 0) { 161a8faeabcSPhilippe Charnier syslog(LOG_ERR, "ioctl(FIONBIO): %m"); 162ea022d16SRodney W. Grimes exit(1); 163ea022d16SRodney W. Grimes } 164ea022d16SRodney W. Grimes fromlen = sizeof (from); 165ea022d16SRodney W. Grimes n = recvfrom(0, buf, sizeof (buf), 0, 166ea022d16SRodney W. Grimes (struct sockaddr *)&from, &fromlen); 167ea022d16SRodney W. Grimes if (n < 0) { 168a8faeabcSPhilippe Charnier syslog(LOG_ERR, "recvfrom: %m"); 169ea022d16SRodney W. Grimes exit(1); 170ea022d16SRodney W. Grimes } 171ea022d16SRodney W. Grimes /* 172ea022d16SRodney W. Grimes * Now that we have read the message out of the UDP 173ea022d16SRodney W. Grimes * socket, we fork and exit. Thus, inetd will go back 174ea022d16SRodney W. Grimes * to listening to the tftp port, and the next request 175ea022d16SRodney W. Grimes * to come in will start up a new instance of tftpd. 176ea022d16SRodney W. Grimes * 177ea022d16SRodney W. Grimes * We do this so that inetd can run tftpd in "wait" mode. 178ea022d16SRodney W. Grimes * The problem with tftpd running in "nowait" mode is that 179ea022d16SRodney W. Grimes * inetd may get one or more successful "selects" on the 180ea022d16SRodney W. Grimes * tftp port before we do our receive, so more than one 181ea022d16SRodney W. Grimes * instance of tftpd may be started up. Worse, if tftpd 182ea022d16SRodney W. Grimes * break before doing the above "recvfrom", inetd would 183ea022d16SRodney W. Grimes * spawn endless instances, clogging the system. 184ea022d16SRodney W. Grimes */ 185ea022d16SRodney W. Grimes { 186ea022d16SRodney W. Grimes int pid; 187ea022d16SRodney W. Grimes int i, j; 188ea022d16SRodney W. Grimes 189ea022d16SRodney W. Grimes for (i = 1; i < 20; i++) { 190ea022d16SRodney W. Grimes pid = fork(); 191ea022d16SRodney W. Grimes if (pid < 0) { 192ea022d16SRodney W. Grimes sleep(i); 193ea022d16SRodney W. Grimes /* 194ea022d16SRodney W. Grimes * flush out to most recently sent request. 195ea022d16SRodney W. Grimes * 196ea022d16SRodney W. Grimes * This may drop some request, but those 197ea022d16SRodney W. Grimes * will be resent by the clients when 198ea022d16SRodney W. Grimes * they timeout. The positive effect of 199ea022d16SRodney W. Grimes * this flush is to (try to) prevent more 200ea022d16SRodney W. Grimes * than one tftpd being started up to service 201ea022d16SRodney W. Grimes * a single request from a single client. 202ea022d16SRodney W. Grimes */ 203ea022d16SRodney W. Grimes j = sizeof from; 204ea022d16SRodney W. Grimes i = recvfrom(0, buf, sizeof (buf), 0, 205ea022d16SRodney W. Grimes (struct sockaddr *)&from, &j); 206ea022d16SRodney W. Grimes if (i > 0) { 207ea022d16SRodney W. Grimes n = i; 208ea022d16SRodney W. Grimes fromlen = j; 209ea022d16SRodney W. Grimes } 210ea022d16SRodney W. Grimes } else { 211ea022d16SRodney W. Grimes break; 212ea022d16SRodney W. Grimes } 213ea022d16SRodney W. Grimes } 214ea022d16SRodney W. Grimes if (pid < 0) { 215a8faeabcSPhilippe Charnier syslog(LOG_ERR, "fork: %m"); 216ea022d16SRodney W. Grimes exit(1); 217ea022d16SRodney W. Grimes } else if (pid != 0) { 218ea022d16SRodney W. Grimes exit(0); 219ea022d16SRodney W. Grimes } 220ea022d16SRodney W. Grimes } 2218ea31785SWarner Losh 2228ea31785SWarner Losh /* 2238ea31785SWarner Losh * Since we exit here, we should do that only after the above 2248ea31785SWarner Losh * recvfrom to keep inetd from constantly forking should there 2258ea31785SWarner Losh * be a problem. See the above comment about system clogging. 2268ea31785SWarner Losh */ 2278ea31785SWarner Losh if (chroot_dir) { 2288ea31785SWarner Losh /* Must get this before chroot because /etc might go away */ 2298ea31785SWarner Losh if ((nobody = getpwnam("nobody")) == NULL) { 2308ea31785SWarner Losh syslog(LOG_ERR, "nobody: no such user"); 2318ea31785SWarner Losh exit(1); 2328ea31785SWarner Losh } 2338ea31785SWarner Losh if (chroot(chroot_dir)) { 2348ea31785SWarner Losh syslog(LOG_ERR, "chroot: %s: %m", chroot_dir); 2358ea31785SWarner Losh exit(1); 2368ea31785SWarner Losh } 2378ea31785SWarner Losh chdir( "/" ); 2388ea31785SWarner Losh setuid(nobody->pw_uid); 2398ea31785SWarner Losh } 2408ea31785SWarner Losh 241ea022d16SRodney W. Grimes from.sin_family = AF_INET; 242ea022d16SRodney W. Grimes alarm(0); 243ea022d16SRodney W. Grimes close(0); 244ea022d16SRodney W. Grimes close(1); 245ea022d16SRodney W. Grimes peer = socket(AF_INET, SOCK_DGRAM, 0); 246ea022d16SRodney W. Grimes if (peer < 0) { 247a8faeabcSPhilippe Charnier syslog(LOG_ERR, "socket: %m"); 248ea022d16SRodney W. Grimes exit(1); 249ea022d16SRodney W. Grimes } 250ea022d16SRodney W. Grimes memset(&sin, 0, sizeof(sin)); 251ea022d16SRodney W. Grimes sin.sin_family = AF_INET; 252ea022d16SRodney W. Grimes if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) { 253a8faeabcSPhilippe Charnier syslog(LOG_ERR, "bind: %m"); 254ea022d16SRodney W. Grimes exit(1); 255ea022d16SRodney W. Grimes } 256ea022d16SRodney W. Grimes if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) { 257a8faeabcSPhilippe Charnier syslog(LOG_ERR, "connect: %m"); 258ea022d16SRodney W. Grimes exit(1); 259ea022d16SRodney W. Grimes } 260ea022d16SRodney W. Grimes tp = (struct tftphdr *)buf; 261ea022d16SRodney W. Grimes tp->th_opcode = ntohs(tp->th_opcode); 262ea022d16SRodney W. Grimes if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) 263ea022d16SRodney W. Grimes tftp(tp, n); 264ea022d16SRodney W. Grimes exit(1); 265ea022d16SRodney W. Grimes } 266ea022d16SRodney W. Grimes 267ea022d16SRodney W. Grimes struct formats; 268ea022d16SRodney W. Grimes int validate_access __P((char **, int)); 269ea022d16SRodney W. Grimes void sendfile __P((struct formats *)); 270ea022d16SRodney W. Grimes void recvfile __P((struct formats *)); 271ea022d16SRodney W. Grimes 272ea022d16SRodney W. Grimes struct formats { 273ea022d16SRodney W. Grimes char *f_mode; 274ea022d16SRodney W. Grimes int (*f_validate) __P((char **, int)); 275ea022d16SRodney W. Grimes void (*f_send) __P((struct formats *)); 276ea022d16SRodney W. Grimes void (*f_recv) __P((struct formats *)); 277ea022d16SRodney W. Grimes int f_convert; 278ea022d16SRodney W. Grimes } formats[] = { 279ea022d16SRodney W. Grimes { "netascii", validate_access, sendfile, recvfile, 1 }, 280ea022d16SRodney W. Grimes { "octet", validate_access, sendfile, recvfile, 0 }, 281ea022d16SRodney W. Grimes #ifdef notdef 282ea022d16SRodney W. Grimes { "mail", validate_user, sendmail, recvmail, 1 }, 283ea022d16SRodney W. Grimes #endif 284ea022d16SRodney W. Grimes { 0 } 285ea022d16SRodney W. Grimes }; 286ea022d16SRodney W. Grimes 287ea022d16SRodney W. Grimes /* 288ea022d16SRodney W. Grimes * Handle initial connection protocol. 289ea022d16SRodney W. Grimes */ 290ea022d16SRodney W. Grimes void 291ea022d16SRodney W. Grimes tftp(tp, size) 292ea022d16SRodney W. Grimes struct tftphdr *tp; 293ea022d16SRodney W. Grimes int size; 294ea022d16SRodney W. Grimes { 295ea022d16SRodney W. Grimes register char *cp; 296ea022d16SRodney W. Grimes int first = 1, ecode; 297ea022d16SRodney W. Grimes register struct formats *pf; 298ea022d16SRodney W. Grimes char *filename, *mode; 299ea022d16SRodney W. Grimes 300ea022d16SRodney W. Grimes filename = cp = tp->th_stuff; 301ea022d16SRodney W. Grimes again: 302ea022d16SRodney W. Grimes while (cp < buf + size) { 303ea022d16SRodney W. Grimes if (*cp == '\0') 304ea022d16SRodney W. Grimes break; 305ea022d16SRodney W. Grimes cp++; 306ea022d16SRodney W. Grimes } 307ea022d16SRodney W. Grimes if (*cp != '\0') { 308ea022d16SRodney W. Grimes nak(EBADOP); 309ea022d16SRodney W. Grimes exit(1); 310ea022d16SRodney W. Grimes } 311ea022d16SRodney W. Grimes if (first) { 312ea022d16SRodney W. Grimes mode = ++cp; 313ea022d16SRodney W. Grimes first = 0; 314ea022d16SRodney W. Grimes goto again; 315ea022d16SRodney W. Grimes } 316ea022d16SRodney W. Grimes for (cp = mode; *cp; cp++) 317ea022d16SRodney W. Grimes if (isupper(*cp)) 318ea022d16SRodney W. Grimes *cp = tolower(*cp); 319ea022d16SRodney W. Grimes for (pf = formats; pf->f_mode; pf++) 320ea022d16SRodney W. Grimes if (strcmp(pf->f_mode, mode) == 0) 321ea022d16SRodney W. Grimes break; 322ea022d16SRodney W. Grimes if (pf->f_mode == 0) { 323ea022d16SRodney W. Grimes nak(EBADOP); 324ea022d16SRodney W. Grimes exit(1); 325ea022d16SRodney W. Grimes } 326ea022d16SRodney W. Grimes ecode = (*pf->f_validate)(&filename, tp->th_opcode); 327ea022d16SRodney W. Grimes if (logging) { 328ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s: %s request for %s: %s", 329ea022d16SRodney W. Grimes verifyhost(&from), 330ea022d16SRodney W. Grimes tp->th_opcode == WRQ ? "write" : "read", 331ea022d16SRodney W. Grimes filename, errtomsg(ecode)); 332ea022d16SRodney W. Grimes } 333ea022d16SRodney W. Grimes if (ecode) { 334ea022d16SRodney W. Grimes /* 335ea022d16SRodney W. Grimes * Avoid storms of naks to a RRQ broadcast for a relative 336ea022d16SRodney W. Grimes * bootfile pathname from a diskless Sun. 337ea022d16SRodney W. Grimes */ 338ea022d16SRodney W. Grimes if (suppress_naks && *filename != '/' && ecode == ENOTFOUND) 339ea022d16SRodney W. Grimes exit(0); 340ea022d16SRodney W. Grimes nak(ecode); 341ea022d16SRodney W. Grimes exit(1); 342ea022d16SRodney W. Grimes } 343ea022d16SRodney W. Grimes if (tp->th_opcode == WRQ) 344ea022d16SRodney W. Grimes (*pf->f_recv)(pf); 345ea022d16SRodney W. Grimes else 346ea022d16SRodney W. Grimes (*pf->f_send)(pf); 347ea022d16SRodney W. Grimes exit(0); 348ea022d16SRodney W. Grimes } 349ea022d16SRodney W. Grimes 350ea022d16SRodney W. Grimes 351ea022d16SRodney W. Grimes FILE *file; 352ea022d16SRodney W. Grimes 353ea022d16SRodney W. Grimes /* 354ea022d16SRodney W. Grimes * Validate file access. Since we 355ea022d16SRodney W. Grimes * have no uid or gid, for now require 356ea022d16SRodney W. Grimes * file to exist and be publicly 357ea022d16SRodney W. Grimes * readable/writable. 358ea022d16SRodney W. Grimes * If we were invoked with arguments 359ea022d16SRodney W. Grimes * from inetd then the file must also be 360ea022d16SRodney W. Grimes * in one of the given directory prefixes. 361ea022d16SRodney W. Grimes * Note also, full path name must be 362ea022d16SRodney W. Grimes * given as we have no login directory. 363ea022d16SRodney W. Grimes */ 364ea022d16SRodney W. Grimes int 365ea022d16SRodney W. Grimes validate_access(filep, mode) 366ea022d16SRodney W. Grimes char **filep; 367ea022d16SRodney W. Grimes int mode; 368ea022d16SRodney W. Grimes { 369ea022d16SRodney W. Grimes struct stat stbuf; 370ea022d16SRodney W. Grimes int fd; 371ea022d16SRodney W. Grimes struct dirlist *dirp; 372ea022d16SRodney W. Grimes static char pathname[MAXPATHLEN]; 373ea022d16SRodney W. Grimes char *filename = *filep; 374ea022d16SRodney W. Grimes 375ea022d16SRodney W. Grimes /* 376ea022d16SRodney W. Grimes * Prevent tricksters from getting around the directory restrictions 377ea022d16SRodney W. Grimes */ 378ea022d16SRodney W. Grimes if (strstr(filename, "/../")) 379ea022d16SRodney W. Grimes return (EACCESS); 380ea022d16SRodney W. Grimes 381ea022d16SRodney W. Grimes if (*filename == '/') { 382ea022d16SRodney W. Grimes /* 383ea022d16SRodney W. Grimes * Allow the request if it's in one of the approved locations. 384ea022d16SRodney W. Grimes * Special case: check the null prefix ("/") by looking 385ea022d16SRodney W. Grimes * for length = 1 and relying on the arg. processing that 386ea022d16SRodney W. Grimes * it's a /. 387ea022d16SRodney W. Grimes */ 388ea022d16SRodney W. Grimes for (dirp = dirs; dirp->name != NULL; dirp++) { 389ea022d16SRodney W. Grimes if (dirp->len == 1 || 390ea022d16SRodney W. Grimes (!strncmp(filename, dirp->name, dirp->len) && 391ea022d16SRodney W. Grimes filename[dirp->len] == '/')) 392ea022d16SRodney W. Grimes break; 393ea022d16SRodney W. Grimes } 394ea022d16SRodney W. Grimes /* If directory list is empty, allow access to any file */ 395ea022d16SRodney W. Grimes if (dirp->name == NULL && dirp != dirs) 396ea022d16SRodney W. Grimes return (EACCESS); 397ea022d16SRodney W. Grimes if (stat(filename, &stbuf) < 0) 398ea022d16SRodney W. Grimes return (errno == ENOENT ? ENOTFOUND : EACCESS); 399ea022d16SRodney W. Grimes if ((stbuf.st_mode & S_IFMT) != S_IFREG) 400ea022d16SRodney W. Grimes return (ENOTFOUND); 401ea022d16SRodney W. Grimes if (mode == RRQ) { 402ea022d16SRodney W. Grimes if ((stbuf.st_mode & S_IROTH) == 0) 403ea022d16SRodney W. Grimes return (EACCESS); 404ea022d16SRodney W. Grimes } else { 405ea022d16SRodney W. Grimes if ((stbuf.st_mode & S_IWOTH) == 0) 406ea022d16SRodney W. Grimes return (EACCESS); 407ea022d16SRodney W. Grimes } 408ea022d16SRodney W. Grimes } else { 409ea022d16SRodney W. Grimes int err; 410ea022d16SRodney W. Grimes 411ea022d16SRodney W. Grimes /* 412ea022d16SRodney W. Grimes * Relative file name: search the approved locations for it. 41323adc6b8SJordan K. Hubbard * Don't allow write requests that avoid directory 414ea022d16SRodney W. Grimes * restrictions. 415ea022d16SRodney W. Grimes */ 416ea022d16SRodney W. Grimes 41723adc6b8SJordan K. Hubbard if (!strncmp(filename, "../", 3)) 418ea022d16SRodney W. Grimes return (EACCESS); 419ea022d16SRodney W. Grimes 420ea022d16SRodney W. Grimes /* 421ea022d16SRodney W. Grimes * If the file exists in one of the directories and isn't 422ea022d16SRodney W. Grimes * readable, continue looking. However, change the error code 423ea022d16SRodney W. Grimes * to give an indication that the file exists. 424ea022d16SRodney W. Grimes */ 425ea022d16SRodney W. Grimes err = ENOTFOUND; 426ea022d16SRodney W. Grimes for (dirp = dirs; dirp->name != NULL; dirp++) { 427fca08b7cSWarner Losh snprintf(pathname, sizeof(pathname), "%s/%s", 428fca08b7cSWarner Losh dirp->name, filename); 429ea022d16SRodney W. Grimes if (stat(pathname, &stbuf) == 0 && 430ea022d16SRodney W. Grimes (stbuf.st_mode & S_IFMT) == S_IFREG) { 431ea022d16SRodney W. Grimes if ((stbuf.st_mode & S_IROTH) != 0) { 432ea022d16SRodney W. Grimes break; 433ea022d16SRodney W. Grimes } 434ea022d16SRodney W. Grimes err = EACCESS; 435ea022d16SRodney W. Grimes } 436ea022d16SRodney W. Grimes } 437ea022d16SRodney W. Grimes if (dirp->name == NULL) 438ea022d16SRodney W. Grimes return (err); 439ea022d16SRodney W. Grimes *filep = filename = pathname; 440ea022d16SRodney W. Grimes } 44184e1b7d2SJoerg Wunsch fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY|O_TRUNC); 442ea022d16SRodney W. Grimes if (fd < 0) 443ea022d16SRodney W. Grimes return (errno + 100); 444ea022d16SRodney W. Grimes file = fdopen(fd, (mode == RRQ)? "r":"w"); 445ea022d16SRodney W. Grimes if (file == NULL) { 446ea022d16SRodney W. Grimes return errno+100; 447ea022d16SRodney W. Grimes } 448ea022d16SRodney W. Grimes return (0); 449ea022d16SRodney W. Grimes } 450ea022d16SRodney W. Grimes 451ea022d16SRodney W. Grimes int timeout; 452ea022d16SRodney W. Grimes jmp_buf timeoutbuf; 453ea022d16SRodney W. Grimes 454ea022d16SRodney W. Grimes void 455ea022d16SRodney W. Grimes timer() 456ea022d16SRodney W. Grimes { 457ea022d16SRodney W. Grimes 458ea022d16SRodney W. Grimes timeout += rexmtval; 459ea022d16SRodney W. Grimes if (timeout >= maxtimeout) 460ea022d16SRodney W. Grimes exit(1); 461ea022d16SRodney W. Grimes longjmp(timeoutbuf, 1); 462ea022d16SRodney W. Grimes } 463ea022d16SRodney W. Grimes 464ea022d16SRodney W. Grimes /* 465ea022d16SRodney W. Grimes * Send the requested file. 466ea022d16SRodney W. Grimes */ 467ea022d16SRodney W. Grimes void 468ea022d16SRodney W. Grimes sendfile(pf) 469ea022d16SRodney W. Grimes struct formats *pf; 470ea022d16SRodney W. Grimes { 471ea022d16SRodney W. Grimes struct tftphdr *dp, *r_init(); 472ea022d16SRodney W. Grimes register struct tftphdr *ap; /* ack packet */ 473ea022d16SRodney W. Grimes register int size, n; 474ea022d16SRodney W. Grimes volatile int block; 475ea022d16SRodney W. Grimes 476ea022d16SRodney W. Grimes signal(SIGALRM, timer); 477ea022d16SRodney W. Grimes dp = r_init(); 478ea022d16SRodney W. Grimes ap = (struct tftphdr *)ackbuf; 479ea022d16SRodney W. Grimes block = 1; 480ea022d16SRodney W. Grimes do { 481ea022d16SRodney W. Grimes size = readit(file, &dp, pf->f_convert); 482ea022d16SRodney W. Grimes if (size < 0) { 483ea022d16SRodney W. Grimes nak(errno + 100); 484ea022d16SRodney W. Grimes goto abort; 485ea022d16SRodney W. Grimes } 486ea022d16SRodney W. Grimes dp->th_opcode = htons((u_short)DATA); 487ea022d16SRodney W. Grimes dp->th_block = htons((u_short)block); 488ea022d16SRodney W. Grimes timeout = 0; 489ea022d16SRodney W. Grimes (void)setjmp(timeoutbuf); 490ea022d16SRodney W. Grimes 491ea022d16SRodney W. Grimes send_data: 492ea022d16SRodney W. Grimes if (send(peer, dp, size + 4, 0) != size + 4) { 493a8faeabcSPhilippe Charnier syslog(LOG_ERR, "write: %m"); 494ea022d16SRodney W. Grimes goto abort; 495ea022d16SRodney W. Grimes } 496ea022d16SRodney W. Grimes read_ahead(file, pf->f_convert); 497ea022d16SRodney W. Grimes for ( ; ; ) { 498ea022d16SRodney W. Grimes alarm(rexmtval); /* read the ack */ 499ea022d16SRodney W. Grimes n = recv(peer, ackbuf, sizeof (ackbuf), 0); 500ea022d16SRodney W. Grimes alarm(0); 501ea022d16SRodney W. Grimes if (n < 0) { 502a8faeabcSPhilippe Charnier syslog(LOG_ERR, "read: %m"); 503ea022d16SRodney W. Grimes goto abort; 504ea022d16SRodney W. Grimes } 505ea022d16SRodney W. Grimes ap->th_opcode = ntohs((u_short)ap->th_opcode); 506ea022d16SRodney W. Grimes ap->th_block = ntohs((u_short)ap->th_block); 507ea022d16SRodney W. Grimes 508ea022d16SRodney W. Grimes if (ap->th_opcode == ERROR) 509ea022d16SRodney W. Grimes goto abort; 510ea022d16SRodney W. Grimes 511ea022d16SRodney W. Grimes if (ap->th_opcode == ACK) { 512ea022d16SRodney W. Grimes if (ap->th_block == block) 513ea022d16SRodney W. Grimes break; 514ea022d16SRodney W. Grimes /* Re-synchronize with the other side */ 515ea022d16SRodney W. Grimes (void) synchnet(peer); 516ea022d16SRodney W. Grimes if (ap->th_block == (block -1)) 517ea022d16SRodney W. Grimes goto send_data; 518ea022d16SRodney W. Grimes } 519ea022d16SRodney W. Grimes 520ea022d16SRodney W. Grimes } 521ea022d16SRodney W. Grimes block++; 522ea022d16SRodney W. Grimes } while (size == SEGSIZE); 523ea022d16SRodney W. Grimes abort: 524ea022d16SRodney W. Grimes (void) fclose(file); 525ea022d16SRodney W. Grimes } 526ea022d16SRodney W. Grimes 527ea022d16SRodney W. Grimes void 528ea022d16SRodney W. Grimes justquit() 529ea022d16SRodney W. Grimes { 530ea022d16SRodney W. Grimes exit(0); 531ea022d16SRodney W. Grimes } 532ea022d16SRodney W. Grimes 533ea022d16SRodney W. Grimes 534ea022d16SRodney W. Grimes /* 535ea022d16SRodney W. Grimes * Receive a file. 536ea022d16SRodney W. Grimes */ 537ea022d16SRodney W. Grimes void 538ea022d16SRodney W. Grimes recvfile(pf) 539ea022d16SRodney W. Grimes struct formats *pf; 540ea022d16SRodney W. Grimes { 541ea022d16SRodney W. Grimes struct tftphdr *dp, *w_init(); 542ea022d16SRodney W. Grimes register struct tftphdr *ap; /* ack buffer */ 543ea022d16SRodney W. Grimes register int n, size; 544ea022d16SRodney W. Grimes volatile int block; 545ea022d16SRodney W. Grimes 546ea022d16SRodney W. Grimes signal(SIGALRM, timer); 547ea022d16SRodney W. Grimes dp = w_init(); 548ea022d16SRodney W. Grimes ap = (struct tftphdr *)ackbuf; 549ea022d16SRodney W. Grimes block = 0; 550ea022d16SRodney W. Grimes do { 551ea022d16SRodney W. Grimes timeout = 0; 552ea022d16SRodney W. Grimes ap->th_opcode = htons((u_short)ACK); 553ea022d16SRodney W. Grimes ap->th_block = htons((u_short)block); 554ea022d16SRodney W. Grimes block++; 555ea022d16SRodney W. Grimes (void) setjmp(timeoutbuf); 556ea022d16SRodney W. Grimes send_ack: 557ea022d16SRodney W. Grimes if (send(peer, ackbuf, 4, 0) != 4) { 558a8faeabcSPhilippe Charnier syslog(LOG_ERR, "write: %m"); 559ea022d16SRodney W. Grimes goto abort; 560ea022d16SRodney W. Grimes } 561ea022d16SRodney W. Grimes write_behind(file, pf->f_convert); 562ea022d16SRodney W. Grimes for ( ; ; ) { 563ea022d16SRodney W. Grimes alarm(rexmtval); 564ea022d16SRodney W. Grimes n = recv(peer, dp, PKTSIZE, 0); 565ea022d16SRodney W. Grimes alarm(0); 566ea022d16SRodney W. Grimes if (n < 0) { /* really? */ 567a8faeabcSPhilippe Charnier syslog(LOG_ERR, "read: %m"); 568ea022d16SRodney W. Grimes goto abort; 569ea022d16SRodney W. Grimes } 570ea022d16SRodney W. Grimes dp->th_opcode = ntohs((u_short)dp->th_opcode); 571ea022d16SRodney W. Grimes dp->th_block = ntohs((u_short)dp->th_block); 572ea022d16SRodney W. Grimes if (dp->th_opcode == ERROR) 573ea022d16SRodney W. Grimes goto abort; 574ea022d16SRodney W. Grimes if (dp->th_opcode == DATA) { 575ea022d16SRodney W. Grimes if (dp->th_block == block) { 576ea022d16SRodney W. Grimes break; /* normal */ 577ea022d16SRodney W. Grimes } 578ea022d16SRodney W. Grimes /* Re-synchronize with the other side */ 579ea022d16SRodney W. Grimes (void) synchnet(peer); 580ea022d16SRodney W. Grimes if (dp->th_block == (block-1)) 581ea022d16SRodney W. Grimes goto send_ack; /* rexmit */ 582ea022d16SRodney W. Grimes } 583ea022d16SRodney W. Grimes } 584ea022d16SRodney W. Grimes /* size = write(file, dp->th_data, n - 4); */ 585ea022d16SRodney W. Grimes size = writeit(file, &dp, n - 4, pf->f_convert); 586ea022d16SRodney W. Grimes if (size != (n-4)) { /* ahem */ 587ea022d16SRodney W. Grimes if (size < 0) nak(errno + 100); 588ea022d16SRodney W. Grimes else nak(ENOSPACE); 589ea022d16SRodney W. Grimes goto abort; 590ea022d16SRodney W. Grimes } 591ea022d16SRodney W. Grimes } while (size == SEGSIZE); 592ea022d16SRodney W. Grimes write_behind(file, pf->f_convert); 593ea022d16SRodney W. Grimes (void) fclose(file); /* close data file */ 594ea022d16SRodney W. Grimes 595ea022d16SRodney W. Grimes ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */ 596ea022d16SRodney W. Grimes ap->th_block = htons((u_short)(block)); 597ea022d16SRodney W. Grimes (void) send(peer, ackbuf, 4, 0); 598ea022d16SRodney W. Grimes 599ea022d16SRodney W. Grimes signal(SIGALRM, justquit); /* just quit on timeout */ 600ea022d16SRodney W. Grimes alarm(rexmtval); 601ea022d16SRodney W. Grimes n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */ 602ea022d16SRodney W. Grimes alarm(0); 603ea022d16SRodney W. Grimes if (n >= 4 && /* if read some data */ 604ea022d16SRodney W. Grimes dp->th_opcode == DATA && /* and got a data block */ 605ea022d16SRodney W. Grimes block == dp->th_block) { /* then my last ack was lost */ 606ea022d16SRodney W. Grimes (void) send(peer, ackbuf, 4, 0); /* resend final ack */ 607ea022d16SRodney W. Grimes } 608ea022d16SRodney W. Grimes abort: 609ea022d16SRodney W. Grimes return; 610ea022d16SRodney W. Grimes } 611ea022d16SRodney W. Grimes 612ea022d16SRodney W. Grimes struct errmsg { 613ea022d16SRodney W. Grimes int e_code; 614ea022d16SRodney W. Grimes char *e_msg; 615ea022d16SRodney W. Grimes } errmsgs[] = { 616ea022d16SRodney W. Grimes { EUNDEF, "Undefined error code" }, 617ea022d16SRodney W. Grimes { ENOTFOUND, "File not found" }, 618ea022d16SRodney W. Grimes { EACCESS, "Access violation" }, 619ea022d16SRodney W. Grimes { ENOSPACE, "Disk full or allocation exceeded" }, 620ea022d16SRodney W. Grimes { EBADOP, "Illegal TFTP operation" }, 621ea022d16SRodney W. Grimes { EBADID, "Unknown transfer ID" }, 622ea022d16SRodney W. Grimes { EEXISTS, "File already exists" }, 623ea022d16SRodney W. Grimes { ENOUSER, "No such user" }, 624ea022d16SRodney W. Grimes { -1, 0 } 625ea022d16SRodney W. Grimes }; 626ea022d16SRodney W. Grimes 627ea022d16SRodney W. Grimes static char * 628ea022d16SRodney W. Grimes errtomsg(error) 629ea022d16SRodney W. Grimes int error; 630ea022d16SRodney W. Grimes { 631ea022d16SRodney W. Grimes static char buf[20]; 632ea022d16SRodney W. Grimes register struct errmsg *pe; 633ea022d16SRodney W. Grimes if (error == 0) 634ea022d16SRodney W. Grimes return "success"; 635ea022d16SRodney W. Grimes for (pe = errmsgs; pe->e_code >= 0; pe++) 636ea022d16SRodney W. Grimes if (pe->e_code == error) 637ea022d16SRodney W. Grimes return pe->e_msg; 638fca08b7cSWarner Losh snprintf(buf, sizeof(buf), "error %d", error); 639ea022d16SRodney W. Grimes return buf; 640ea022d16SRodney W. Grimes } 641ea022d16SRodney W. Grimes 642ea022d16SRodney W. Grimes /* 643ea022d16SRodney W. Grimes * Send a nak packet (error message). 644ea022d16SRodney W. Grimes * Error code passed in is one of the 645ea022d16SRodney W. Grimes * standard TFTP codes, or a UNIX errno 646ea022d16SRodney W. Grimes * offset by 100. 647ea022d16SRodney W. Grimes */ 648ea022d16SRodney W. Grimes static void 649ea022d16SRodney W. Grimes nak(error) 650ea022d16SRodney W. Grimes int error; 651ea022d16SRodney W. Grimes { 652ea022d16SRodney W. Grimes register struct tftphdr *tp; 653ea022d16SRodney W. Grimes int length; 654ea022d16SRodney W. Grimes register struct errmsg *pe; 655ea022d16SRodney W. Grimes 656ea022d16SRodney W. Grimes tp = (struct tftphdr *)buf; 657ea022d16SRodney W. Grimes tp->th_opcode = htons((u_short)ERROR); 658ea022d16SRodney W. Grimes tp->th_code = htons((u_short)error); 659ea022d16SRodney W. Grimes for (pe = errmsgs; pe->e_code >= 0; pe++) 660ea022d16SRodney W. Grimes if (pe->e_code == error) 661ea022d16SRodney W. Grimes break; 662ea022d16SRodney W. Grimes if (pe->e_code < 0) { 663ea022d16SRodney W. Grimes pe->e_msg = strerror(error - 100); 664ea022d16SRodney W. Grimes tp->th_code = EUNDEF; /* set 'undef' errorcode */ 665ea022d16SRodney W. Grimes } 666ea022d16SRodney W. Grimes strcpy(tp->th_msg, pe->e_msg); 667ea022d16SRodney W. Grimes length = strlen(pe->e_msg); 668ea022d16SRodney W. Grimes tp->th_msg[length] = '\0'; 669ea022d16SRodney W. Grimes length += 5; 670ea022d16SRodney W. Grimes if (send(peer, buf, length, 0) != length) 671a8faeabcSPhilippe Charnier syslog(LOG_ERR, "nak: %m"); 672ea022d16SRodney W. Grimes } 673ea022d16SRodney W. Grimes 674ea022d16SRodney W. Grimes static char * 675ea022d16SRodney W. Grimes verifyhost(fromp) 676ea022d16SRodney W. Grimes struct sockaddr_in *fromp; 677ea022d16SRodney W. Grimes { 678ea022d16SRodney W. Grimes struct hostent *hp; 679ea022d16SRodney W. Grimes 680ea022d16SRodney W. Grimes hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (fromp->sin_addr), 681ea022d16SRodney W. Grimes fromp->sin_family); 682ea022d16SRodney W. Grimes if (hp) 683ea022d16SRodney W. Grimes return hp->h_name; 684ea022d16SRodney W. Grimes else 685ea022d16SRodney W. Grimes return inet_ntoa(fromp->sin_addr); 686ea022d16SRodney W. Grimes } 687