1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)tftpd.c 5.13 (Berkeley) 02/26/91"; 16 #endif /* not lint */ 17 18 /* 19 * Trivial file transfer protocol server. 20 * 21 * This version includes many modifications by Jim Guyton <guyton@rand-unix> 22 */ 23 24 #include <sys/types.h> 25 #include <sys/ioctl.h> 26 #include <sys/stat.h> 27 #include <signal.h> 28 #include <fcntl.h> 29 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/tftp.h> 33 #include <netdb.h> 34 35 #include <setjmp.h> 36 #include <syslog.h> 37 #include <stdio.h> 38 #include <errno.h> 39 #include <ctype.h> 40 #include <string.h> 41 #include <stdlib.h> 42 43 #define TIMEOUT 5 44 45 extern int errno; 46 struct sockaddr_in sin = { AF_INET }; 47 int peer; 48 int rexmtval = TIMEOUT; 49 int maxtimeout = 5*TIMEOUT; 50 51 #define PKTSIZE SEGSIZE+4 52 char buf[PKTSIZE]; 53 char ackbuf[PKTSIZE]; 54 struct sockaddr_in from; 55 int fromlen; 56 57 #define MAXARG 4 58 char *dirs[MAXARG+1]; 59 60 main(ac, av) 61 char **av; 62 { 63 register struct tftphdr *tp; 64 register int n = 0; 65 int on = 1; 66 67 ac--; av++; 68 while (ac-- > 0 && n < MAXARG) 69 dirs[n++] = *av++; 70 openlog("tftpd", LOG_PID, LOG_DAEMON); 71 if (ioctl(0, FIONBIO, &on) < 0) { 72 syslog(LOG_ERR, "ioctl(FIONBIO): %m\n"); 73 exit(1); 74 } 75 fromlen = sizeof (from); 76 n = recvfrom(0, buf, sizeof (buf), 0, 77 (struct sockaddr *)&from, &fromlen); 78 if (n < 0) { 79 syslog(LOG_ERR, "recvfrom: %m\n"); 80 exit(1); 81 } 82 /* 83 * Now that we have read the message out of the UDP 84 * socket, we fork and exit. Thus, inetd will go back 85 * to listening to the tftp port, and the next request 86 * to come in will start up a new instance of tftpd. 87 * 88 * We do this so that inetd can run tftpd in "wait" mode. 89 * The problem with tftpd running in "nowait" mode is that 90 * inetd may get one or more successful "selects" on the 91 * tftp port before we do our receive, so more than one 92 * instance of tftpd may be started up. Worse, if tftpd 93 * break before doing the above "recvfrom", inetd would 94 * spawn endless instances, clogging the system. 95 */ 96 { 97 int pid; 98 int i, j; 99 100 for (i = 1; i < 20; i++) { 101 pid = fork(); 102 if (pid < 0) { 103 sleep(i); 104 /* 105 * flush out to most recently sent request. 106 * 107 * This may drop some request, but those 108 * will be resent by the clients when 109 * they timeout. The positive effect of 110 * this flush is to (try to) prevent more 111 * than one tftpd being started up to service 112 * a single request from a single client. 113 */ 114 j = sizeof from; 115 i = recvfrom(0, buf, sizeof (buf), 0, 116 (struct sockaddr *)&from, &j); 117 if (i > 0) { 118 n = i; 119 fromlen = j; 120 } 121 } else { 122 break; 123 } 124 } 125 if (pid < 0) { 126 syslog(LOG_ERR, "fork: %m\n"); 127 exit(1); 128 } else if (pid != 0) { 129 exit(0); 130 } 131 } 132 from.sin_family = AF_INET; 133 alarm(0); 134 close(0); 135 close(1); 136 peer = socket(AF_INET, SOCK_DGRAM, 0); 137 if (peer < 0) { 138 syslog(LOG_ERR, "socket: %m\n"); 139 exit(1); 140 } 141 if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) { 142 syslog(LOG_ERR, "bind: %m\n"); 143 exit(1); 144 } 145 if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) { 146 syslog(LOG_ERR, "connect: %m\n"); 147 exit(1); 148 } 149 tp = (struct tftphdr *)buf; 150 tp->th_opcode = ntohs(tp->th_opcode); 151 if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) 152 tftp(tp, n); 153 exit(1); 154 } 155 156 int validate_access(); 157 int sendfile(), recvfile(); 158 159 struct formats { 160 char *f_mode; 161 int (*f_validate)(); 162 int (*f_send)(); 163 int (*f_recv)(); 164 int f_convert; 165 } formats[] = { 166 { "netascii", validate_access, sendfile, recvfile, 1 }, 167 { "octet", validate_access, sendfile, recvfile, 0 }, 168 #ifdef notdef 169 { "mail", validate_user, sendmail, recvmail, 1 }, 170 #endif 171 { 0 } 172 }; 173 174 /* 175 * Handle initial connection protocol. 176 */ 177 tftp(tp, size) 178 struct tftphdr *tp; 179 int size; 180 { 181 register char *cp; 182 int first = 1, ecode; 183 register struct formats *pf; 184 char *filename, *mode; 185 186 filename = cp = tp->th_stuff; 187 again: 188 while (cp < buf + size) { 189 if (*cp == '\0') 190 break; 191 cp++; 192 } 193 if (*cp != '\0') { 194 nak(EBADOP); 195 exit(1); 196 } 197 if (first) { 198 mode = ++cp; 199 first = 0; 200 goto again; 201 } 202 for (cp = mode; *cp; cp++) 203 if (isupper(*cp)) 204 *cp = tolower(*cp); 205 for (pf = formats; pf->f_mode; pf++) 206 if (strcmp(pf->f_mode, mode) == 0) 207 break; 208 if (pf->f_mode == 0) { 209 nak(EBADOP); 210 exit(1); 211 } 212 ecode = (*pf->f_validate)(filename, tp->th_opcode); 213 if (ecode) { 214 nak(ecode); 215 exit(1); 216 } 217 if (tp->th_opcode == WRQ) 218 (*pf->f_recv)(pf); 219 else 220 (*pf->f_send)(pf); 221 exit(0); 222 } 223 224 225 FILE *file; 226 227 /* 228 * Validate file access. Since we 229 * have no uid or gid, for now require 230 * file to exist and be publicly 231 * readable/writable. 232 * If we were invoked with arguments 233 * from inetd then the file must also be 234 * in one of the given directory prefixes. 235 * Note also, full path name must be 236 * given as we have no login directory. 237 */ 238 validate_access(filename, mode) 239 char *filename; 240 int mode; 241 { 242 struct stat stbuf; 243 int fd; 244 char *cp, **dirp; 245 246 if (*filename != '/') 247 return (EACCESS); 248 /* 249 * prevent tricksters from getting around the directory restrictions 250 */ 251 for (cp = filename + 1; *cp; cp++) 252 if(*cp == '.' && strncmp(cp-1, "/../", 4) == 0) 253 return(EACCESS); 254 for (dirp = dirs; *dirp; dirp++) 255 if (strncmp(filename, *dirp, strlen(*dirp)) == 0) 256 break; 257 if (*dirp==0 && dirp!=dirs) 258 return (EACCESS); 259 if (stat(filename, &stbuf) < 0) 260 return (errno == ENOENT ? ENOTFOUND : EACCESS); 261 if (mode == RRQ) { 262 if ((stbuf.st_mode&(S_IREAD >> 6)) == 0) 263 return (EACCESS); 264 } else { 265 if ((stbuf.st_mode&(S_IWRITE >> 6)) == 0) 266 return (EACCESS); 267 } 268 fd = open(filename, mode == RRQ ? 0 : 1); 269 if (fd < 0) 270 return (errno + 100); 271 file = fdopen(fd, (mode == RRQ)? "r":"w"); 272 if (file == NULL) { 273 return errno+100; 274 } 275 return (0); 276 } 277 278 int timeout; 279 jmp_buf timeoutbuf; 280 281 void 282 timer() 283 { 284 285 timeout += rexmtval; 286 if (timeout >= maxtimeout) 287 exit(1); 288 longjmp(timeoutbuf, 1); 289 } 290 291 /* 292 * Send the requested file. 293 */ 294 sendfile(pf) 295 struct formats *pf; 296 { 297 struct tftphdr *dp, *r_init(); 298 register struct tftphdr *ap; /* ack packet */ 299 register int block = 1, size, n; 300 301 signal(SIGALRM, timer); 302 dp = r_init(); 303 ap = (struct tftphdr *)ackbuf; 304 do { 305 size = readit(file, &dp, pf->f_convert); 306 if (size < 0) { 307 nak(errno + 100); 308 goto abort; 309 } 310 dp->th_opcode = htons((u_short)DATA); 311 dp->th_block = htons((u_short)block); 312 timeout = 0; 313 (void) setjmp(timeoutbuf); 314 315 send_data: 316 if (send(peer, dp, size + 4, 0) != size + 4) { 317 syslog(LOG_ERR, "tftpd: write: %m\n"); 318 goto abort; 319 } 320 read_ahead(file, pf->f_convert); 321 for ( ; ; ) { 322 alarm(rexmtval); /* read the ack */ 323 n = recv(peer, ackbuf, sizeof (ackbuf), 0); 324 alarm(0); 325 if (n < 0) { 326 syslog(LOG_ERR, "tftpd: read: %m\n"); 327 goto abort; 328 } 329 ap->th_opcode = ntohs((u_short)ap->th_opcode); 330 ap->th_block = ntohs((u_short)ap->th_block); 331 332 if (ap->th_opcode == ERROR) 333 goto abort; 334 335 if (ap->th_opcode == ACK) { 336 if (ap->th_block == block) { 337 break; 338 } 339 /* Re-synchronize with the other side */ 340 (void) synchnet(peer); 341 if (ap->th_block == (block -1)) { 342 goto send_data; 343 } 344 } 345 346 } 347 block++; 348 } while (size == SEGSIZE); 349 abort: 350 (void) fclose(file); 351 } 352 353 void 354 justquit() 355 { 356 exit(0); 357 } 358 359 360 /* 361 * Receive a file. 362 */ 363 recvfile(pf) 364 struct formats *pf; 365 { 366 struct tftphdr *dp, *w_init(); 367 register struct tftphdr *ap; /* ack buffer */ 368 register int block = 0, n, size; 369 370 signal(SIGALRM, timer); 371 dp = w_init(); 372 ap = (struct tftphdr *)ackbuf; 373 do { 374 timeout = 0; 375 ap->th_opcode = htons((u_short)ACK); 376 ap->th_block = htons((u_short)block); 377 block++; 378 (void) setjmp(timeoutbuf); 379 send_ack: 380 if (send(peer, ackbuf, 4, 0) != 4) { 381 syslog(LOG_ERR, "tftpd: write: %m\n"); 382 goto abort; 383 } 384 write_behind(file, pf->f_convert); 385 for ( ; ; ) { 386 alarm(rexmtval); 387 n = recv(peer, dp, PKTSIZE, 0); 388 alarm(0); 389 if (n < 0) { /* really? */ 390 syslog(LOG_ERR, "tftpd: read: %m\n"); 391 goto abort; 392 } 393 dp->th_opcode = ntohs((u_short)dp->th_opcode); 394 dp->th_block = ntohs((u_short)dp->th_block); 395 if (dp->th_opcode == ERROR) 396 goto abort; 397 if (dp->th_opcode == DATA) { 398 if (dp->th_block == block) { 399 break; /* normal */ 400 } 401 /* Re-synchronize with the other side */ 402 (void) synchnet(peer); 403 if (dp->th_block == (block-1)) 404 goto send_ack; /* rexmit */ 405 } 406 } 407 /* size = write(file, dp->th_data, n - 4); */ 408 size = writeit(file, &dp, n - 4, pf->f_convert); 409 if (size != (n-4)) { /* ahem */ 410 if (size < 0) nak(errno + 100); 411 else nak(ENOSPACE); 412 goto abort; 413 } 414 } while (size == SEGSIZE); 415 write_behind(file, pf->f_convert); 416 (void) fclose(file); /* close data file */ 417 418 ap->th_opcode = htons((u_short)ACK); /* send the "final" ack */ 419 ap->th_block = htons((u_short)(block)); 420 (void) send(peer, ackbuf, 4, 0); 421 422 signal(SIGALRM, justquit); /* just quit on timeout */ 423 alarm(rexmtval); 424 n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */ 425 alarm(0); 426 if (n >= 4 && /* if read some data */ 427 dp->th_opcode == DATA && /* and got a data block */ 428 block == dp->th_block) { /* then my last ack was lost */ 429 (void) send(peer, ackbuf, 4, 0); /* resend final ack */ 430 } 431 abort: 432 return; 433 } 434 435 struct errmsg { 436 int e_code; 437 char *e_msg; 438 } errmsgs[] = { 439 { EUNDEF, "Undefined error code" }, 440 { ENOTFOUND, "File not found" }, 441 { EACCESS, "Access violation" }, 442 { ENOSPACE, "Disk full or allocation exceeded" }, 443 { EBADOP, "Illegal TFTP operation" }, 444 { EBADID, "Unknown transfer ID" }, 445 { EEXISTS, "File already exists" }, 446 { ENOUSER, "No such user" }, 447 { -1, 0 } 448 }; 449 450 /* 451 * Send a nak packet (error message). 452 * Error code passed in is one of the 453 * standard TFTP codes, or a UNIX errno 454 * offset by 100. 455 */ 456 nak(error) 457 int error; 458 { 459 register struct tftphdr *tp; 460 int length; 461 register struct errmsg *pe; 462 463 tp = (struct tftphdr *)buf; 464 tp->th_opcode = htons((u_short)ERROR); 465 tp->th_code = htons((u_short)error); 466 for (pe = errmsgs; pe->e_code >= 0; pe++) 467 if (pe->e_code == error) 468 break; 469 if (pe->e_code < 0) { 470 pe->e_msg = strerror(error - 100); 471 tp->th_code = EUNDEF; /* set 'undef' errorcode */ 472 } 473 strcpy(tp->th_msg, pe->e_msg); 474 length = strlen(pe->e_msg); 475 tp->th_msg[length] = '\0'; 476 length += 5; 477 if (send(peer, buf, length, 0) != length) 478 syslog(LOG_ERR, "nak: %m\n"); 479 } 480