tftpd.c revision 337249
1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1983, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif /* not lint */ 35 36#ifndef lint 37#if 0 38static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93"; 39#endif 40#endif /* not lint */ 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: stable/10/libexec/tftpd/tftpd.c 337249 2018-08-03 14:19:09Z asomers $"); 43 44/* 45 * Trivial file transfer protocol server. 46 * 47 * This version includes many modifications by Jim Guyton 48 * <guyton@rand-unix>. 49 */ 50 51#include <sys/param.h> 52#include <sys/ioctl.h> 53#include <sys/stat.h> 54#include <sys/socket.h> 55 56#include <netinet/in.h> 57#include <arpa/tftp.h> 58 59#include <ctype.h> 60#include <errno.h> 61#include <fcntl.h> 62#include <netdb.h> 63#include <pwd.h> 64#include <stdint.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <string.h> 68#include <syslog.h> 69#include <unistd.h> 70 71#include "tftp-file.h" 72#include "tftp-io.h" 73#include "tftp-utils.h" 74#include "tftp-transfer.h" 75#include "tftp-options.h" 76 77#ifdef LIBWRAP 78#include <tcpd.h> 79#endif 80 81static void tftp_wrq(int peer, char *, ssize_t); 82static void tftp_rrq(int peer, char *, ssize_t); 83 84/* 85 * Null-terminated directory prefix list for absolute pathname requests and 86 * search list for relative pathname requests. 87 * 88 * MAXDIRS should be at least as large as the number of arguments that 89 * inetd allows (currently 20). 90 */ 91#define MAXDIRS 20 92static struct dirlist { 93 const char *name; 94 int len; 95} dirs[MAXDIRS+1]; 96static int suppress_naks; 97static int logging; 98static int ipchroot; 99static int create_new = 0; 100static const char *newfile_format = "%Y%m%d"; 101static int increase_name = 0; 102static mode_t mask = S_IWGRP | S_IWOTH; 103 104struct formats; 105static void tftp_recvfile(int peer, const char *mode); 106static void tftp_xmitfile(int peer, const char *mode); 107static int validate_access(int peer, char **, int); 108static char peername[NI_MAXHOST]; 109 110static FILE *file; 111 112static struct formats { 113 const char *f_mode; 114 int f_convert; 115} formats[] = { 116 { "netascii", 1 }, 117 { "octet", 0 }, 118 { NULL, 0 } 119}; 120 121int 122main(int argc, char *argv[]) 123{ 124 struct tftphdr *tp; 125 int peer; 126 socklen_t peerlen, len; 127 ssize_t n; 128 int ch; 129 char *chroot_dir = NULL; 130 struct passwd *nobody; 131 const char *chuser = "nobody"; 132 char recvbuffer[MAXPKTSIZE]; 133 int allow_ro = 1, allow_wo = 1; 134 135 tzset(); /* syslog in localtime */ 136 acting_as_client = 0; 137 138 tftp_openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 139 while ((ch = getopt(argc, argv, "cCd:F:lnoOp:s:u:U:wW")) != -1) { 140 switch (ch) { 141 case 'c': 142 ipchroot = 1; 143 break; 144 case 'C': 145 ipchroot = 2; 146 break; 147 case 'd': 148 if (atoi(optarg) != 0) 149 debug += atoi(optarg); 150 else 151 debug |= debug_finds(optarg); 152 break; 153 case 'F': 154 newfile_format = optarg; 155 break; 156 case 'l': 157 logging = 1; 158 break; 159 case 'n': 160 suppress_naks = 1; 161 break; 162 case 'o': 163 options_rfc_enabled = 0; 164 break; 165 case 'O': 166 options_extra_enabled = 0; 167 break; 168 case 'p': 169 packetdroppercentage = atoi(optarg); 170 tftp_log(LOG_INFO, 171 "Randomly dropping %d out of 100 packets", 172 packetdroppercentage); 173 break; 174 case 's': 175 chroot_dir = optarg; 176 break; 177 case 'u': 178 chuser = optarg; 179 break; 180 case 'U': 181 mask = strtol(optarg, NULL, 0); 182 break; 183 case 'w': 184 create_new = 1; 185 break; 186 case 'W': 187 create_new = 1; 188 increase_name = 1; 189 break; 190 default: 191 tftp_log(LOG_WARNING, 192 "ignoring unknown option -%c", ch); 193 } 194 } 195 if (optind < argc) { 196 struct dirlist *dirp; 197 198 /* Get list of directory prefixes. Skip relative pathnames. */ 199 for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS]; 200 optind++) { 201 if (argv[optind][0] == '/') { 202 dirp->name = argv[optind]; 203 dirp->len = strlen(dirp->name); 204 dirp++; 205 } 206 } 207 } 208 else if (chroot_dir) { 209 dirs->name = "/"; 210 dirs->len = 1; 211 } 212 if (ipchroot > 0 && chroot_dir == NULL) { 213 tftp_log(LOG_ERR, "-c requires -s"); 214 exit(1); 215 } 216 217 umask(mask); 218 219 { 220 int on = 1; 221 if (ioctl(0, FIONBIO, &on) < 0) { 222 tftp_log(LOG_ERR, "ioctl(FIONBIO): %s", strerror(errno)); 223 exit(1); 224 } 225 } 226 227 /* Find out who we are talking to and what we are going to do */ 228 peerlen = sizeof(peer_sock); 229 n = recvfrom(0, recvbuffer, MAXPKTSIZE, 0, 230 (struct sockaddr *)&peer_sock, &peerlen); 231 if (n < 0) { 232 tftp_log(LOG_ERR, "recvfrom: %s", strerror(errno)); 233 exit(1); 234 } 235 getnameinfo((struct sockaddr *)&peer_sock, peer_sock.ss_len, 236 peername, sizeof(peername), NULL, 0, NI_NUMERICHOST); 237 238 /* 239 * Now that we have read the message out of the UDP 240 * socket, we fork and exit. Thus, inetd will go back 241 * to listening to the tftp port, and the next request 242 * to come in will start up a new instance of tftpd. 243 * 244 * We do this so that inetd can run tftpd in "wait" mode. 245 * The problem with tftpd running in "nowait" mode is that 246 * inetd may get one or more successful "selects" on the 247 * tftp port before we do our receive, so more than one 248 * instance of tftpd may be started up. Worse, if tftpd 249 * break before doing the above "recvfrom", inetd would 250 * spawn endless instances, clogging the system. 251 */ 252 { 253 int i, pid; 254 255 for (i = 1; i < 20; i++) { 256 pid = fork(); 257 if (pid < 0) { 258 sleep(i); 259 /* 260 * flush out to most recently sent request. 261 * 262 * This may drop some request, but those 263 * will be resent by the clients when 264 * they timeout. The positive effect of 265 * this flush is to (try to) prevent more 266 * than one tftpd being started up to service 267 * a single request from a single client. 268 */ 269 peerlen = sizeof peer_sock; 270 i = recvfrom(0, recvbuffer, MAXPKTSIZE, 0, 271 (struct sockaddr *)&peer_sock, &peerlen); 272 if (i > 0) { 273 n = i; 274 } 275 } else { 276 break; 277 } 278 } 279 if (pid < 0) { 280 tftp_log(LOG_ERR, "fork: %s", strerror(errno)); 281 exit(1); 282 } else if (pid != 0) { 283 exit(0); 284 } 285 } 286 287#ifdef LIBWRAP 288 /* 289 * See if the client is allowed to talk to me. 290 * (This needs to be done before the chroot()) 291 */ 292 { 293 struct request_info req; 294 295 request_init(&req, RQ_CLIENT_ADDR, peername, 0); 296 request_set(&req, RQ_DAEMON, "tftpd", 0); 297 298 if (hosts_access(&req) == 0) { 299 if (debug&DEBUG_ACCESS) 300 tftp_log(LOG_WARNING, 301 "Access denied by 'tftpd' entry " 302 "in /etc/hosts.allow"); 303 304 /* 305 * Full access might be disabled, but maybe the 306 * client is allowed to do read-only access. 307 */ 308 request_set(&req, RQ_DAEMON, "tftpd-ro", 0); 309 allow_ro = hosts_access(&req); 310 311 request_set(&req, RQ_DAEMON, "tftpd-wo", 0); 312 allow_wo = hosts_access(&req); 313 314 if (allow_ro == 0 && allow_wo == 0) { 315 tftp_log(LOG_WARNING, 316 "Unauthorized access from %s", peername); 317 exit(1); 318 } 319 320 if (debug&DEBUG_ACCESS) { 321 if (allow_ro) 322 tftp_log(LOG_WARNING, 323 "But allowed readonly access " 324 "via 'tftpd-ro' entry"); 325 if (allow_wo) 326 tftp_log(LOG_WARNING, 327 "But allowed writeonly access " 328 "via 'tftpd-wo' entry"); 329 } 330 } else 331 if (debug&DEBUG_ACCESS) 332 tftp_log(LOG_WARNING, 333 "Full access allowed" 334 "in /etc/hosts.allow"); 335 } 336#endif 337 338 /* 339 * Since we exit here, we should do that only after the above 340 * recvfrom to keep inetd from constantly forking should there 341 * be a problem. See the above comment about system clogging. 342 */ 343 if (chroot_dir) { 344 if (ipchroot > 0) { 345 char *tempchroot; 346 struct stat sb; 347 int statret; 348 struct sockaddr_storage ss; 349 char hbuf[NI_MAXHOST]; 350 351 statret = -1; 352 memcpy(&ss, &peer_sock, peer_sock.ss_len); 353 unmappedaddr((struct sockaddr_in6 *)&ss); 354 getnameinfo((struct sockaddr *)&ss, ss.ss_len, 355 hbuf, sizeof(hbuf), NULL, 0, 356 NI_NUMERICHOST); 357 asprintf(&tempchroot, "%s/%s", chroot_dir, hbuf); 358 if (ipchroot == 2) 359 statret = stat(tempchroot, &sb); 360 if (ipchroot == 1 || 361 (statret == 0 && (sb.st_mode & S_IFDIR))) 362 chroot_dir = tempchroot; 363 } 364 /* Must get this before chroot because /etc might go away */ 365 if ((nobody = getpwnam(chuser)) == NULL) { 366 tftp_log(LOG_ERR, "%s: no such user", chuser); 367 exit(1); 368 } 369 if (chroot(chroot_dir)) { 370 tftp_log(LOG_ERR, "chroot: %s: %s", 371 chroot_dir, strerror(errno)); 372 exit(1); 373 } 374 chdir("/"); 375 setgroups(1, &nobody->pw_gid); 376 if (setuid(nobody->pw_uid) != 0) { 377 tftp_log(LOG_ERR, "setuid failed"); 378 exit(1); 379 } 380 } 381 382 len = sizeof(me_sock); 383 if (getsockname(0, (struct sockaddr *)&me_sock, &len) == 0) { 384 switch (me_sock.ss_family) { 385 case AF_INET: 386 ((struct sockaddr_in *)&me_sock)->sin_port = 0; 387 break; 388 case AF_INET6: 389 ((struct sockaddr_in6 *)&me_sock)->sin6_port = 0; 390 break; 391 default: 392 /* unsupported */ 393 break; 394 } 395 } else { 396 memset(&me_sock, 0, sizeof(me_sock)); 397 me_sock.ss_family = peer_sock.ss_family; 398 me_sock.ss_len = peer_sock.ss_len; 399 } 400 close(0); 401 close(1); 402 peer = socket(peer_sock.ss_family, SOCK_DGRAM, 0); 403 if (peer < 0) { 404 tftp_log(LOG_ERR, "socket: %s", strerror(errno)); 405 exit(1); 406 } 407 if (bind(peer, (struct sockaddr *)&me_sock, me_sock.ss_len) < 0) { 408 tftp_log(LOG_ERR, "bind: %s", strerror(errno)); 409 exit(1); 410 } 411 412 tp = (struct tftphdr *)recvbuffer; 413 tp->th_opcode = ntohs(tp->th_opcode); 414 if (tp->th_opcode == RRQ) { 415 if (allow_ro) 416 tftp_rrq(peer, tp->th_stuff, n - 1); 417 else { 418 tftp_log(LOG_WARNING, 419 "%s read access denied", peername); 420 exit(1); 421 } 422 } 423 if (tp->th_opcode == WRQ) { 424 if (allow_wo) 425 tftp_wrq(peer, tp->th_stuff, n - 1); 426 else { 427 tftp_log(LOG_WARNING, 428 "%s write access denied", peername); 429 exit(1); 430 } 431 } 432 exit(1); 433} 434 435static void 436reduce_path(char *fn) 437{ 438 char *slash, *ptr; 439 440 /* Reduce all "/+./" to "/" (just in case we've got "/./../" later */ 441 while ((slash = strstr(fn, "/./")) != NULL) { 442 for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--) 443 ; 444 slash += 2; 445 while (*slash) 446 *++ptr = *++slash; 447 } 448 449 /* Now reduce all "/something/+../" to "/" */ 450 while ((slash = strstr(fn, "/../")) != NULL) { 451 if (slash == fn) 452 break; 453 for (ptr = slash; ptr > fn && ptr[-1] == '/'; ptr--) 454 ; 455 for (ptr--; ptr >= fn; ptr--) 456 if (*ptr == '/') 457 break; 458 if (ptr < fn) 459 break; 460 slash += 3; 461 while (*slash) 462 *++ptr = *++slash; 463 } 464} 465 466static char * 467parse_header(int peer, char *recvbuffer, ssize_t size, 468 char **filename, char **mode) 469{ 470 char *cp; 471 int i; 472 struct formats *pf; 473 474 *mode = NULL; 475 cp = recvbuffer; 476 477 i = get_field(peer, recvbuffer, size); 478 if (i >= PATH_MAX) { 479 tftp_log(LOG_ERR, "Bad option - filename too long"); 480 send_error(peer, EBADOP); 481 exit(1); 482 } 483 *filename = recvbuffer; 484 tftp_log(LOG_INFO, "Filename: '%s'", *filename); 485 cp += i; 486 487 i = get_field(peer, cp, size); 488 *mode = cp; 489 cp += i; 490 491 /* Find the file transfer mode */ 492 for (cp = *mode; *cp; cp++) 493 if (isupper(*cp)) 494 *cp = tolower(*cp); 495 for (pf = formats; pf->f_mode; pf++) 496 if (strcmp(pf->f_mode, *mode) == 0) 497 break; 498 if (pf->f_mode == NULL) { 499 tftp_log(LOG_ERR, 500 "Bad option - Unknown transfer mode (%s)", *mode); 501 send_error(peer, EBADOP); 502 exit(1); 503 } 504 tftp_log(LOG_INFO, "Mode: '%s'", *mode); 505 506 return (cp + 1); 507} 508 509/* 510 * WRQ - receive a file from the client 511 */ 512void 513tftp_wrq(int peer, char *recvbuffer, ssize_t size) 514{ 515 char *cp; 516 int has_options = 0, ecode; 517 char *filename, *mode; 518 char fnbuf[PATH_MAX]; 519 520 cp = parse_header(peer, recvbuffer, size, &filename, &mode); 521 size -= (cp - recvbuffer) + 1; 522 523 strcpy(fnbuf, filename); 524 reduce_path(fnbuf); 525 filename = fnbuf; 526 527 if (size > 0) { 528 if (options_rfc_enabled) 529 has_options = !parse_options(peer, cp, size); 530 else 531 tftp_log(LOG_INFO, "Options found but not enabled"); 532 } 533 534 ecode = validate_access(peer, &filename, WRQ); 535 if (ecode == 0) { 536 if (has_options) 537 send_oack(peer); 538 else 539 send_ack(peer, 0); 540 } 541 if (logging) { 542 tftp_log(LOG_INFO, "%s: write request for %s: %s", peername, 543 filename, errtomsg(ecode)); 544 } 545 546 if (ecode) { 547 send_error(peer, ecode); 548 exit(1); 549 } 550 tftp_recvfile(peer, mode); 551 exit(0); 552} 553 554/* 555 * RRQ - send a file to the client 556 */ 557void 558tftp_rrq(int peer, char *recvbuffer, ssize_t size) 559{ 560 char *cp; 561 int has_options = 0, ecode; 562 char *filename, *mode; 563 char fnbuf[PATH_MAX]; 564 565 cp = parse_header(peer, recvbuffer, size, &filename, &mode); 566 size -= (cp - recvbuffer) + 1; 567 568 strcpy(fnbuf, filename); 569 reduce_path(fnbuf); 570 filename = fnbuf; 571 572 if (size > 0) { 573 if (options_rfc_enabled) 574 has_options = !parse_options(peer, cp, size); 575 else 576 tftp_log(LOG_INFO, "Options found but not enabled"); 577 } 578 579 ecode = validate_access(peer, &filename, RRQ); 580 if (ecode == 0) { 581 if (has_options) { 582 int n; 583 char lrecvbuffer[MAXPKTSIZE]; 584 struct tftphdr *rp = (struct tftphdr *)lrecvbuffer; 585 586 send_oack(peer); 587 n = receive_packet(peer, lrecvbuffer, MAXPKTSIZE, 588 NULL, timeoutpacket); 589 if (n < 0) { 590 if (debug&DEBUG_SIMPLE) 591 tftp_log(LOG_DEBUG, "Aborting: %s", 592 rp_strerror(n)); 593 return; 594 } 595 if (rp->th_opcode != ACK) { 596 if (debug&DEBUG_SIMPLE) 597 tftp_log(LOG_DEBUG, 598 "Expected ACK, got %s on OACK", 599 packettype(rp->th_opcode)); 600 return; 601 } 602 } 603 } 604 605 if (logging) 606 tftp_log(LOG_INFO, "%s: read request for %s: %s", peername, 607 filename, errtomsg(ecode)); 608 609 if (ecode) { 610 /* 611 * Avoid storms of naks to a RRQ broadcast for a relative 612 * bootfile pathname from a diskless Sun. 613 */ 614 if (suppress_naks && *filename != '/' && ecode == ENOTFOUND) 615 exit(0); 616 send_error(peer, ecode); 617 exit(1); 618 } 619 tftp_xmitfile(peer, mode); 620} 621 622/* 623 * Find the next value for YYYYMMDD.nn when the file to be written should 624 * be unique. Due to the limitations of nn, we will fail if nn reaches 100. 625 * Besides, that is four updates per hour on a file, which is kind of 626 * execessive anyway. 627 */ 628static int 629find_next_name(char *filename, int *fd) 630{ 631 int i; 632 time_t tval; 633 size_t len; 634 struct tm lt; 635 char yyyymmdd[MAXPATHLEN]; 636 char newname[MAXPATHLEN]; 637 638 /* Create the YYYYMMDD part of the filename */ 639 time(&tval); 640 lt = *localtime(&tval); 641 len = strftime(yyyymmdd, sizeof(yyyymmdd), newfile_format, <); 642 if (len == 0) { 643 syslog(LOG_WARNING, 644 "Filename suffix too long (%d characters maximum)", 645 MAXPATHLEN); 646 return (EACCESS); 647 } 648 649 /* Make sure the new filename is not too long */ 650 if (strlen(filename) > MAXPATHLEN - len - 5) { 651 syslog(LOG_WARNING, 652 "Filename too long (%zd characters, %zd maximum)", 653 strlen(filename), MAXPATHLEN - len - 5); 654 return (EACCESS); 655 } 656 657 /* Find the first file which doesn't exist */ 658 for (i = 0; i < 100; i++) { 659 sprintf(newname, "%s.%s.%02d", filename, yyyymmdd, i); 660 *fd = open(newname, 661 O_WRONLY | O_CREAT | O_EXCL, 662 S_IRUSR | S_IWUSR | S_IRGRP | 663 S_IWGRP | S_IROTH | S_IWOTH); 664 if (*fd > 0) 665 return 0; 666 } 667 668 return (EEXIST); 669} 670 671/* 672 * Validate file access. Since we 673 * have no uid or gid, for now require 674 * file to exist and be publicly 675 * readable/writable. 676 * If we were invoked with arguments 677 * from inetd then the file must also be 678 * in one of the given directory prefixes. 679 * Note also, full path name must be 680 * given as we have no login directory. 681 */ 682int 683validate_access(int peer, char **filep, int mode) 684{ 685 struct stat stbuf; 686 int fd; 687 int error; 688 struct dirlist *dirp; 689 static char pathname[MAXPATHLEN]; 690 char *filename = *filep; 691 692 /* 693 * Prevent tricksters from getting around the directory restrictions 694 */ 695 if (strstr(filename, "/../")) 696 return (EACCESS); 697 698 if (*filename == '/') { 699 /* 700 * Allow the request if it's in one of the approved locations. 701 * Special case: check the null prefix ("/") by looking 702 * for length = 1 and relying on the arg. processing that 703 * it's a /. 704 */ 705 for (dirp = dirs; dirp->name != NULL; dirp++) { 706 if (dirp->len == 1 || 707 (!strncmp(filename, dirp->name, dirp->len) && 708 filename[dirp->len] == '/')) 709 break; 710 } 711 /* If directory list is empty, allow access to any file */ 712 if (dirp->name == NULL && dirp != dirs) 713 return (EACCESS); 714 if (stat(filename, &stbuf) < 0) 715 return (errno == ENOENT ? ENOTFOUND : EACCESS); 716 if ((stbuf.st_mode & S_IFMT) != S_IFREG) 717 return (ENOTFOUND); 718 if (mode == RRQ) { 719 if ((stbuf.st_mode & S_IROTH) == 0) 720 return (EACCESS); 721 } else { 722 if ((stbuf.st_mode & S_IWOTH) == 0) 723 return (EACCESS); 724 } 725 } else { 726 int err; 727 728 /* 729 * Relative file name: search the approved locations for it. 730 * Don't allow write requests that avoid directory 731 * restrictions. 732 */ 733 734 if (!strncmp(filename, "../", 3)) 735 return (EACCESS); 736 737 /* 738 * If the file exists in one of the directories and isn't 739 * readable, continue looking. However, change the error code 740 * to give an indication that the file exists. 741 */ 742 err = ENOTFOUND; 743 for (dirp = dirs; dirp->name != NULL; dirp++) { 744 snprintf(pathname, sizeof(pathname), "%s/%s", 745 dirp->name, filename); 746 if (stat(pathname, &stbuf) == 0 && 747 (stbuf.st_mode & S_IFMT) == S_IFREG) { 748 if (mode == RRQ) { 749 if ((stbuf.st_mode & S_IROTH) != 0) 750 break; 751 } else { 752 if ((stbuf.st_mode & S_IWOTH) != 0) 753 break; 754 } 755 err = EACCESS; 756 } 757 } 758 if (dirp->name != NULL) 759 *filep = filename = pathname; 760 else if (mode == RRQ) 761 return (err); 762 else if (err != ENOTFOUND || !create_new) 763 return (err); 764 } 765 766 /* 767 * This option is handled here because it (might) require(s) the 768 * size of the file. 769 */ 770 option_tsize(peer, NULL, mode, &stbuf); 771 772 if (mode == RRQ) 773 fd = open(filename, O_RDONLY); 774 else { 775 if (create_new) { 776 if (increase_name) { 777 error = find_next_name(filename, &fd); 778 if (error > 0) 779 return (error + 100); 780 } else 781 fd = open(filename, 782 O_WRONLY | O_TRUNC | O_CREAT, 783 S_IRUSR | S_IWUSR | S_IRGRP | 784 S_IWGRP | S_IROTH | S_IWOTH ); 785 } else 786 fd = open(filename, O_WRONLY | O_TRUNC); 787 } 788 if (fd < 0) 789 return (errno + 100); 790 file = fdopen(fd, (mode == RRQ)? "r":"w"); 791 if (file == NULL) { 792 close(fd); 793 return (errno + 100); 794 } 795 return (0); 796} 797 798static void 799tftp_xmitfile(int peer, const char *mode) 800{ 801 uint16_t block; 802 time_t now; 803 struct tftp_stats ts; 804 805 now = time(NULL); 806 if (debug&DEBUG_SIMPLE) 807 tftp_log(LOG_DEBUG, "Transmitting file"); 808 809 read_init(0, file, mode); 810 block = 1; 811 tftp_send(peer, &block, &ts); 812 read_close(); 813 if (debug&DEBUG_SIMPLE) 814 tftp_log(LOG_INFO, "Sent %jd bytes in %jd seconds", 815 (intmax_t)ts.amount, (intmax_t)time(NULL) - now); 816} 817 818static void 819tftp_recvfile(int peer, const char *mode) 820{ 821 uint16_t block; 822 struct timeval now1, now2; 823 struct tftp_stats ts; 824 825 gettimeofday(&now1, NULL); 826 if (debug&DEBUG_SIMPLE) 827 tftp_log(LOG_DEBUG, "Receiving file"); 828 829 write_init(0, file, mode); 830 831 block = 0; 832 tftp_receive(peer, &block, &ts, NULL, 0); 833 834 gettimeofday(&now2, NULL); 835 836 if (debug&DEBUG_SIMPLE) { 837 double f; 838 if (now1.tv_usec > now2.tv_usec) { 839 now2.tv_usec += 1000000; 840 now2.tv_sec--; 841 } 842 843 f = now2.tv_sec - now1.tv_sec + 844 (now2.tv_usec - now1.tv_usec) / 100000.0; 845 tftp_log(LOG_INFO, 846 "Download of %jd bytes in %d blocks completed after %0.1f seconds\n", 847 (intmax_t)ts.amount, block, f); 848 } 849 850 return; 851} 852