1 /* 2 * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if 0 35 #ifndef lint 36 static char copyright[] = 37 "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 #endif 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 45 #endif 46 static const char rcsid[] = 47 "$FreeBSD$"; 48 #endif /* not lint */ 49 50 /* 51 * FTP server. 52 */ 53 #include <sys/param.h> 54 #include <sys/ioctl.h> 55 #include <sys/mman.h> 56 #include <sys/socket.h> 57 #include <sys/stat.h> 58 #include <sys/time.h> 59 #include <sys/wait.h> 60 61 #include <netinet/in.h> 62 #include <netinet/in_systm.h> 63 #include <netinet/ip.h> 64 #include <netinet/tcp.h> 65 66 #define FTP_NAMES 67 #include <arpa/ftp.h> 68 #include <arpa/inet.h> 69 #include <arpa/telnet.h> 70 71 #include <ctype.h> 72 #include <dirent.h> 73 #include <err.h> 74 #include <errno.h> 75 #include <fcntl.h> 76 #include <glob.h> 77 #include <limits.h> 78 #include <netdb.h> 79 #include <pwd.h> 80 #include <grp.h> 81 #include <opie.h> 82 #include <signal.h> 83 #include <stdio.h> 84 #include <stdlib.h> 85 #include <string.h> 86 #include <syslog.h> 87 #include <time.h> 88 #include <unistd.h> 89 #include <libutil.h> 90 #ifdef LOGIN_CAP 91 #include <login_cap.h> 92 #endif 93 94 #ifdef USE_PAM 95 #include <security/pam_appl.h> 96 #endif 97 98 #include "pathnames.h" 99 #include "extern.h" 100 101 #include <stdarg.h> 102 103 static char version[] = "Version 6.00LS"; 104 #undef main 105 106 extern off_t restart_point; 107 extern char cbuf[]; 108 109 union sockunion server_addr; 110 union sockunion ctrl_addr; 111 union sockunion data_source; 112 union sockunion data_dest; 113 union sockunion his_addr; 114 union sockunion pasv_addr; 115 116 int daemon_mode; 117 int data; 118 int logged_in; 119 struct passwd *pw; 120 int ftpdebug; 121 int timeout = 900; /* timeout after 15 minutes of inactivity */ 122 int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 123 int logging; 124 int restricted_data_ports = 1; 125 int paranoid = 1; /* be extra careful about security */ 126 int anon_only = 0; /* Only anonymous ftp allowed */ 127 int guest; 128 int dochroot; 129 int stats; 130 int statfd = -1; 131 int type; 132 int form; 133 int stru; /* avoid C keyword */ 134 int mode; 135 int usedefault = 1; /* for data transfers */ 136 int pdata = -1; /* for passive mode */ 137 int readonly=0; /* Server is in readonly mode. */ 138 int noepsv=0; /* EPSV command is disabled. */ 139 int noretr=0; /* RETR command is disabled. */ 140 int noguestretr=0; /* RETR command is disabled for anon users. */ 141 int noguestmkd=0; /* MKD command is disabled for anon users. */ 142 int noguestmod=1; /* anon users may not modify existing files. */ 143 144 static volatile sig_atomic_t recvurg; 145 sig_atomic_t transflag; 146 off_t file_size; 147 off_t byte_count; 148 #if !defined(CMASK) || CMASK == 0 149 #undef CMASK 150 #define CMASK 027 151 #endif 152 int defumask = CMASK; /* default umask value */ 153 char tmpline[7]; 154 char *hostname; 155 int epsvall = 0; 156 157 #ifdef VIRTUAL_HOSTING 158 char *ftpuser; 159 160 static struct ftphost { 161 struct ftphost *next; 162 struct addrinfo *hostinfo; 163 char *hostname; 164 char *anonuser; 165 char *statfile; 166 char *welcome; 167 char *loginmsg; 168 } *thishost, *firsthost; 169 170 #endif 171 char remotehost[MAXHOSTNAMELEN]; 172 char *ident = NULL; 173 174 static char ttyline[20]; 175 char *tty = ttyline; /* for klogin */ 176 177 #ifdef USE_PAM 178 static int auth_pam(struct passwd**, const char*); 179 pam_handle_t *pamh = NULL; 180 #endif 181 182 static struct opie opiedata; 183 static char opieprompt[OPIE_CHALLENGE_MAX+1]; 184 static int pwok; 185 186 char *pid_file = NULL; 187 188 /* 189 * Limit number of pathnames that glob can return. 190 * A limit of 0 indicates the number of pathnames is unlimited. 191 */ 192 #define MAXGLOBARGS 16384 193 # 194 195 /* 196 * Timeout intervals for retrying connections 197 * to hosts that don't accept PORT cmds. This 198 * is a kludge, but given the problems with TCP... 199 */ 200 #define SWAITMAX 90 /* wait at most 90 seconds */ 201 #define SWAITINT 5 /* interval between retries */ 202 203 int swaitmax = SWAITMAX; 204 int swaitint = SWAITINT; 205 206 #ifdef SETPROCTITLE 207 #ifdef OLD_SETPROCTITLE 208 char **Argv = NULL; /* pointer to argument vector */ 209 char *LastArgv = NULL; /* end of argv */ 210 #endif /* OLD_SETPROCTITLE */ 211 char proctitle[LINE_MAX]; /* initial part of title */ 212 #endif /* SETPROCTITLE */ 213 214 #define LOGCMD(cmd, file) \ 215 if (logging > 1) \ 216 syslog(LOG_INFO,"%s %s%s", cmd, \ 217 *(file) == '/' ? "" : curdir(), file); 218 #define LOGCMD2(cmd, file1, file2) \ 219 if (logging > 1) \ 220 syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 221 *(file1) == '/' ? "" : curdir(), file1, \ 222 *(file2) == '/' ? "" : curdir(), file2); 223 #define LOGBYTES(cmd, file, cnt) \ 224 if (logging > 1) { \ 225 if (cnt == (off_t)-1) \ 226 syslog(LOG_INFO,"%s %s%s", cmd, \ 227 *(file) == '/' ? "" : curdir(), file); \ 228 else \ 229 syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 230 cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 231 } 232 233 #ifdef VIRTUAL_HOSTING 234 static void inithosts(void); 235 static void selecthost(union sockunion *); 236 #endif 237 static void ack(char *); 238 static void sigurg(int); 239 static void myoob(void); 240 static int checkuser(char *, char *, int); 241 static FILE *dataconn(char *, off_t, char *); 242 static void dolog(struct sockaddr *); 243 static char *curdir(void); 244 static void end_login(void); 245 static FILE *getdatasock(char *); 246 static int guniquefd(char *, char **); 247 static void lostconn(int); 248 static void sigquit(int); 249 static int receive_data(FILE *, FILE *); 250 static int send_data(FILE *, FILE *, off_t, off_t, int); 251 static struct passwd * 252 sgetpwnam(char *); 253 static char *sgetsave(char *); 254 static void reapchild(int); 255 static void logxfer(char *, off_t, time_t); 256 static char *doublequote(char *); 257 258 static char * 259 curdir(void) 260 { 261 static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 262 263 if (getcwd(path, sizeof(path)-2) == NULL) 264 return (""); 265 if (path[1] != '\0') /* special case for root dir. */ 266 strcat(path, "/"); 267 /* For guest account, skip / since it's chrooted */ 268 return (guest ? path+1 : path); 269 } 270 271 int 272 main(int argc, char *argv[], char **envp) 273 { 274 int addrlen, ch, on = 1, tos; 275 char *cp, line[LINE_MAX]; 276 FILE *fd; 277 int error; 278 char *bindname = NULL; 279 int family = AF_UNSPEC; 280 int enable_v4 = 0; 281 struct sigaction sa; 282 283 tzset(); /* in case no timezone database in ~ftp */ 284 sigemptyset(&sa.sa_mask); 285 sa.sa_flags = SA_RESTART; 286 287 #ifdef OLD_SETPROCTITLE 288 /* 289 * Save start and extent of argv for setproctitle. 290 */ 291 Argv = argv; 292 while (*envp) 293 envp++; 294 LastArgv = envp[-1] + strlen(envp[-1]); 295 #endif /* OLD_SETPROCTITLE */ 296 297 298 while ((ch = getopt(argc, argv, "46a:AdDElmMoOp:rRSt:T:u:Uv")) != -1) { 299 switch (ch) { 300 case '4': 301 enable_v4 = 1; 302 if (family == AF_UNSPEC) 303 family = AF_INET; 304 break; 305 306 case '6': 307 family = AF_INET6; 308 break; 309 310 case 'a': 311 bindname = optarg; 312 break; 313 314 case 'A': 315 anon_only = 1; 316 break; 317 318 case 'd': 319 ftpdebug++; 320 break; 321 322 case 'D': 323 daemon_mode++; 324 break; 325 326 case 'E': 327 noepsv = 1; 328 break; 329 330 case 'l': 331 logging++; /* > 1 == extra logging */ 332 break; 333 334 case 'm': 335 noguestmod = 0; 336 break; 337 338 case 'M': 339 noguestmkd = 1; 340 break; 341 342 case 'o': 343 noretr = 1; 344 break; 345 346 case 'O': 347 noguestretr = 1; 348 break; 349 350 case 'p': 351 pid_file = optarg; 352 break; 353 354 case 'r': 355 readonly = 1; 356 break; 357 358 case 'R': 359 paranoid = 0; 360 break; 361 362 case 'S': 363 stats++; 364 break; 365 366 case 't': 367 timeout = atoi(optarg); 368 if (maxtimeout < timeout) 369 maxtimeout = timeout; 370 break; 371 372 case 'T': 373 maxtimeout = atoi(optarg); 374 if (timeout > maxtimeout) 375 timeout = maxtimeout; 376 break; 377 378 case 'u': 379 { 380 long val = 0; 381 382 val = strtol(optarg, &optarg, 8); 383 if (*optarg != '\0' || val < 0) 384 warnx("bad value for -u"); 385 else 386 defumask = val; 387 break; 388 } 389 case 'U': 390 restricted_data_ports = 0; 391 break; 392 393 case 'v': 394 ftpdebug++; 395 break; 396 397 default: 398 warnx("unknown flag -%c ignored", optopt); 399 break; 400 } 401 } 402 403 #ifdef VIRTUAL_HOSTING 404 inithosts(); 405 #endif 406 (void) freopen(_PATH_DEVNULL, "w", stderr); 407 408 /* 409 * LOG_NDELAY sets up the logging connection immediately, 410 * necessary for anonymous ftp's that chroot and can't do it later. 411 */ 412 openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 413 414 if (daemon_mode) { 415 int ctl_sock, fd; 416 struct addrinfo hints, *res; 417 418 /* 419 * Detach from parent. 420 */ 421 if (daemon(1, 1) < 0) { 422 syslog(LOG_ERR, "failed to become a daemon"); 423 exit(1); 424 } 425 sa.sa_handler = reapchild; 426 (void)sigaction(SIGCHLD, &sa, NULL); 427 /* init bind_sa */ 428 memset(&hints, 0, sizeof(hints)); 429 430 hints.ai_family = family == AF_UNSPEC ? AF_INET : family; 431 hints.ai_socktype = SOCK_STREAM; 432 hints.ai_protocol = 0; 433 hints.ai_flags = AI_PASSIVE; 434 error = getaddrinfo(bindname, "ftp", &hints, &res); 435 if (error) { 436 if (family == AF_UNSPEC) { 437 hints.ai_family = AF_UNSPEC; 438 error = getaddrinfo(bindname, "ftp", &hints, 439 &res); 440 } 441 } 442 if (error) { 443 syslog(LOG_ERR, "%s", gai_strerror(error)); 444 if (error == EAI_SYSTEM) 445 syslog(LOG_ERR, "%s", strerror(errno)); 446 exit(1); 447 } 448 if (res->ai_addr == NULL) { 449 syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 450 exit(1); 451 } else 452 family = res->ai_addr->sa_family; 453 /* 454 * Open a socket, bind it to the FTP port, and start 455 * listening. 456 */ 457 ctl_sock = socket(family, SOCK_STREAM, 0); 458 if (ctl_sock < 0) { 459 syslog(LOG_ERR, "control socket: %m"); 460 exit(1); 461 } 462 if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 463 &on, sizeof(on)) < 0) 464 syslog(LOG_WARNING, 465 "control setsockopt (SO_REUSEADDR): %m"); 466 if (family == AF_INET6 && enable_v4 == 0) { 467 if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY, 468 &on, sizeof (on)) < 0) 469 syslog(LOG_WARNING, 470 "control setsockopt (IPV6_V6ONLY): %m"); 471 } 472 memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len); 473 if (bind(ctl_sock, (struct sockaddr *)&server_addr, 474 server_addr.su_len) < 0) { 475 syslog(LOG_ERR, "control bind: %m"); 476 exit(1); 477 } 478 if (listen(ctl_sock, 32) < 0) { 479 syslog(LOG_ERR, "control listen: %m"); 480 exit(1); 481 } 482 /* 483 * Atomically write process ID 484 */ 485 if (pid_file) 486 { 487 int fd; 488 char buf[20]; 489 490 fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 491 | O_NONBLOCK | O_EXLOCK, 0644); 492 if (fd < 0) { 493 if (errno == EAGAIN) 494 errx(1, "%s: file locked", pid_file); 495 else 496 err(1, "%s", pid_file); 497 } 498 snprintf(buf, sizeof(buf), 499 "%lu\n", (unsigned long) getpid()); 500 if (write(fd, buf, strlen(buf)) < 0) 501 err(1, "%s: write", pid_file); 502 /* Leave the pid file open and locked */ 503 } 504 /* 505 * Loop forever accepting connection requests and forking off 506 * children to handle them. 507 */ 508 while (1) { 509 addrlen = server_addr.su_len; 510 fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 511 if (fork() == 0) { 512 /* child */ 513 (void) dup2(fd, 0); 514 (void) dup2(fd, 1); 515 close(ctl_sock); 516 break; 517 } 518 close(fd); 519 } 520 } else { 521 addrlen = sizeof(his_addr); 522 if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 523 syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 524 exit(1); 525 } 526 } 527 528 sa.sa_handler = SIG_DFL; 529 (void)sigaction(SIGCHLD, &sa, NULL); 530 531 sa.sa_handler = sigurg; 532 sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 533 (void)sigaction(SIGURG, &sa, NULL); 534 535 sigfillset(&sa.sa_mask); /* block all signals in handler */ 536 sa.sa_flags = SA_RESTART; 537 sa.sa_handler = sigquit; 538 (void)sigaction(SIGHUP, &sa, NULL); 539 (void)sigaction(SIGINT, &sa, NULL); 540 (void)sigaction(SIGQUIT, &sa, NULL); 541 (void)sigaction(SIGTERM, &sa, NULL); 542 543 sa.sa_handler = lostconn; 544 (void)sigaction(SIGPIPE, &sa, NULL); 545 546 addrlen = sizeof(ctrl_addr); 547 if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 548 syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 549 exit(1); 550 } 551 #ifdef VIRTUAL_HOSTING 552 /* select our identity from virtual host table */ 553 selecthost(&ctrl_addr); 554 #endif 555 #ifdef IP_TOS 556 if (ctrl_addr.su_family == AF_INET) 557 { 558 tos = IPTOS_LOWDELAY; 559 if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 560 syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 561 } 562 #endif 563 /* 564 * Disable Nagle on the control channel so that we don't have to wait 565 * for peer's ACK before issuing our next reply. 566 */ 567 if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 568 syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 569 570 data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 571 572 /* set this here so klogin can use it... */ 573 (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 574 575 /* Try to handle urgent data inline */ 576 #ifdef SO_OOBINLINE 577 if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 578 syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 579 #endif 580 581 #ifdef F_SETOWN 582 if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 583 syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 584 #endif 585 dolog((struct sockaddr *)&his_addr); 586 /* 587 * Set up default state 588 */ 589 data = -1; 590 type = TYPE_A; 591 form = FORM_N; 592 stru = STRU_F; 593 mode = MODE_S; 594 tmpline[0] = '\0'; 595 596 /* If logins are disabled, print out the message. */ 597 if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 598 while (fgets(line, sizeof(line), fd) != NULL) { 599 if ((cp = strchr(line, '\n')) != NULL) 600 *cp = '\0'; 601 lreply(530, "%s", line); 602 } 603 (void) fflush(stdout); 604 (void) fclose(fd); 605 reply(530, "System not available."); 606 exit(0); 607 } 608 #ifdef VIRTUAL_HOSTING 609 if ((fd = fopen(thishost->welcome, "r")) != NULL) { 610 #else 611 if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 612 #endif 613 while (fgets(line, sizeof(line), fd) != NULL) { 614 if ((cp = strchr(line, '\n')) != NULL) 615 *cp = '\0'; 616 lreply(220, "%s", line); 617 } 618 (void) fflush(stdout); 619 (void) fclose(fd); 620 /* reply(220,) must follow */ 621 } 622 #ifndef VIRTUAL_HOSTING 623 if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 624 fatalerror("Ran out of memory."); 625 (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 626 hostname[MAXHOSTNAMELEN - 1] = '\0'; 627 #endif 628 reply(220, "%s FTP server (%s) ready.", hostname, version); 629 for (;;) 630 (void) yyparse(); 631 /* NOTREACHED */ 632 } 633 634 static void 635 lostconn(int signo) 636 { 637 638 if (ftpdebug) 639 syslog(LOG_DEBUG, "lost connection"); 640 dologout(1); 641 } 642 643 static void 644 sigquit(int signo) 645 { 646 647 syslog(LOG_ERR, "got signal %d", signo); 648 dologout(1); 649 } 650 651 #ifdef VIRTUAL_HOSTING 652 /* 653 * read in virtual host tables (if they exist) 654 */ 655 656 static void 657 inithosts(void) 658 { 659 int insert; 660 size_t len; 661 FILE *fp; 662 char *cp, *mp, *line; 663 char *hostname; 664 char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 665 struct ftphost *hrp, *lhrp; 666 struct addrinfo hints, *res, *ai; 667 668 /* 669 * Fill in the default host information 670 */ 671 if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 672 fatalerror("Ran out of memory."); 673 if (gethostname(hostname, MAXHOSTNAMELEN) < 0) 674 hostname[0] = '\0'; 675 hostname[MAXHOSTNAMELEN - 1] = '\0'; 676 if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 677 fatalerror("Ran out of memory."); 678 hrp->hostname = hostname; 679 hrp->hostinfo = NULL; 680 681 memset(&hints, 0, sizeof(hints)); 682 hints.ai_flags = AI_CANONNAME; 683 hints.ai_family = AF_UNSPEC; 684 if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 685 hrp->hostinfo = res; 686 hrp->statfile = _PATH_FTPDSTATFILE; 687 hrp->welcome = _PATH_FTPWELCOME; 688 hrp->loginmsg = _PATH_FTPLOGINMESG; 689 hrp->anonuser = "ftp"; 690 hrp->next = NULL; 691 thishost = firsthost = lhrp = hrp; 692 if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 693 int addrsize, error, gothost; 694 void *addr; 695 struct hostent *hp; 696 697 while ((line = fgetln(fp, &len)) != NULL) { 698 int i, hp_error; 699 700 /* skip comments */ 701 if (line[0] == '#') 702 continue; 703 if (line[len - 1] == '\n') { 704 line[len - 1] = '\0'; 705 mp = NULL; 706 } else { 707 if ((mp = malloc(len + 1)) == NULL) 708 fatalerror("Ran out of memory."); 709 memcpy(mp, line, len); 710 mp[len] = '\0'; 711 line = mp; 712 } 713 cp = strtok(line, " \t"); 714 /* skip empty lines */ 715 if (cp == NULL) 716 goto nextline; 717 vhost = cp; 718 719 /* set defaults */ 720 anonuser = "ftp"; 721 statfile = _PATH_FTPDSTATFILE; 722 welcome = _PATH_FTPWELCOME; 723 loginmsg = _PATH_FTPLOGINMESG; 724 725 /* 726 * Preparse the line so we can use its info 727 * for all the addresses associated with 728 * the virtual host name. 729 * Field 0, the virtual host name, is special: 730 * it's already parsed off and will be strdup'ed 731 * later, after we know its canonical form. 732 */ 733 for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 734 if (*cp != '-' && (cp = strdup(cp))) 735 switch (i) { 736 case 1: /* anon user permissions */ 737 anonuser = cp; 738 break; 739 case 2: /* statistics file */ 740 statfile = cp; 741 break; 742 case 3: /* welcome message */ 743 welcome = cp; 744 break; 745 case 4: /* login message */ 746 loginmsg = cp; 747 break; 748 default: /* programming error */ 749 abort(); 750 /* NOTREACHED */ 751 } 752 753 hints.ai_flags = 0; 754 hints.ai_family = AF_UNSPEC; 755 hints.ai_flags = AI_PASSIVE; 756 if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 757 goto nextline; 758 for (ai = res; ai != NULL && ai->ai_addr != NULL; 759 ai = ai->ai_next) { 760 761 gothost = 0; 762 for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 763 struct addrinfo *hi; 764 765 for (hi = hrp->hostinfo; hi != NULL; 766 hi = hi->ai_next) 767 if (hi->ai_addrlen == ai->ai_addrlen && 768 memcmp(hi->ai_addr, 769 ai->ai_addr, 770 ai->ai_addr->sa_len) == 0) { 771 gothost++; 772 break; 773 } 774 if (gothost) 775 break; 776 } 777 if (hrp == NULL) { 778 if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 779 goto nextline; 780 hrp->hostname = NULL; 781 hrp->hostinfo = NULL; 782 insert = 1; 783 } else 784 insert = 0; /* host already in the chain */ 785 if (hrp->hostinfo) 786 freeaddrinfo(hrp->hostinfo); 787 hrp->hostinfo = res; 788 789 /* 790 * determine hostname to use. 791 * force defined name if there is a valid alias 792 * otherwise fallback to primary hostname 793 */ 794 /* XXX: getaddrinfo() can't do alias check */ 795 switch(hrp->hostinfo->ai_family) { 796 case AF_INET: 797 addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 798 addrsize = sizeof(struct in_addr); 799 break; 800 case AF_INET6: 801 addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 802 addrsize = sizeof(struct in6_addr); 803 break; 804 default: 805 /* should not reach here */ 806 if (hrp->hostinfo != NULL) 807 freeaddrinfo(hrp->hostinfo); 808 free(hrp); 809 goto nextline; 810 /* NOTREACHED */ 811 } 812 if ((hp = getipnodebyaddr(addr, addrsize, 813 hrp->hostinfo->ai_family, 814 &hp_error)) != NULL) { 815 if (strcmp(vhost, hp->h_name) != 0) { 816 if (hp->h_aliases == NULL) 817 vhost = hp->h_name; 818 else { 819 i = 0; 820 while (hp->h_aliases[i] && 821 strcmp(vhost, hp->h_aliases[i]) != 0) 822 ++i; 823 if (hp->h_aliases[i] == NULL) 824 vhost = hp->h_name; 825 } 826 } 827 } 828 if (hrp->hostname && 829 strcmp(hrp->hostname, vhost) != 0) { 830 free(hrp->hostname); 831 hrp->hostname = NULL; 832 } 833 if (hrp->hostname == NULL && 834 (hrp->hostname = strdup(vhost)) == NULL) 835 goto nextline; 836 hrp->anonuser = anonuser; 837 hrp->statfile = statfile; 838 hrp->welcome = welcome; 839 hrp->loginmsg = loginmsg; 840 if (insert) { 841 hrp->next = NULL; 842 lhrp->next = hrp; 843 lhrp = hrp; 844 } 845 if (hp) 846 freehostent(hp); 847 } 848 nextline: 849 if (mp) 850 free(mp); 851 } 852 (void) fclose(fp); 853 } 854 } 855 856 static void 857 selecthost(union sockunion *su) 858 { 859 struct ftphost *hrp; 860 u_int16_t port; 861 #ifdef INET6 862 struct in6_addr *mapped_in6 = NULL; 863 #endif 864 struct addrinfo *hi; 865 866 #ifdef INET6 867 /* 868 * XXX IPv4 mapped IPv6 addr consideraton, 869 * specified in rfc2373. 870 */ 871 if (su->su_family == AF_INET6 && 872 IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 873 mapped_in6 = &su->su_sin6.sin6_addr; 874 #endif 875 876 hrp = thishost = firsthost; /* default */ 877 port = su->su_port; 878 su->su_port = 0; 879 while (hrp != NULL) { 880 for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 881 if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 882 thishost = hrp; 883 break; 884 } 885 #ifdef INET6 886 /* XXX IPv4 mapped IPv6 addr consideraton */ 887 if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 888 (memcmp(&mapped_in6->s6_addr[12], 889 &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 890 sizeof(struct in_addr)) == 0)) { 891 thishost = hrp; 892 break; 893 } 894 #endif 895 } 896 hrp = hrp->next; 897 } 898 su->su_port = port; 899 /* setup static variables as appropriate */ 900 hostname = thishost->hostname; 901 ftpuser = thishost->anonuser; 902 } 903 #endif 904 905 /* 906 * Helper function for sgetpwnam(). 907 */ 908 static char * 909 sgetsave(char *s) 910 { 911 char *new = malloc((unsigned) strlen(s) + 1); 912 913 if (new == NULL) { 914 perror_reply(421, "Local resource failure: malloc"); 915 dologout(1); 916 /* NOTREACHED */ 917 } 918 (void) strcpy(new, s); 919 return (new); 920 } 921 922 /* 923 * Save the result of a getpwnam. Used for USER command, since 924 * the data returned must not be clobbered by any other command 925 * (e.g., globbing). 926 */ 927 static struct passwd * 928 sgetpwnam(char *name) 929 { 930 static struct passwd save; 931 struct passwd *p; 932 933 if ((p = getpwnam(name)) == NULL) 934 return (p); 935 if (save.pw_name) { 936 free(save.pw_name); 937 free(save.pw_passwd); 938 free(save.pw_gecos); 939 free(save.pw_dir); 940 free(save.pw_shell); 941 } 942 save = *p; 943 save.pw_name = sgetsave(p->pw_name); 944 save.pw_passwd = sgetsave(p->pw_passwd); 945 save.pw_gecos = sgetsave(p->pw_gecos); 946 save.pw_dir = sgetsave(p->pw_dir); 947 save.pw_shell = sgetsave(p->pw_shell); 948 return (&save); 949 } 950 951 static int login_attempts; /* number of failed login attempts */ 952 static int askpasswd; /* had user command, ask for passwd */ 953 static char curname[MAXLOGNAME]; /* current USER name */ 954 955 /* 956 * USER command. 957 * Sets global passwd pointer pw if named account exists and is acceptable; 958 * sets askpasswd if a PASS command is expected. If logged in previously, 959 * need to reset state. If name is "ftp" or "anonymous", the name is not in 960 * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 961 * If account doesn't exist, ask for passwd anyway. Otherwise, check user 962 * requesting login privileges. Disallow anyone who does not have a standard 963 * shell as returned by getusershell(). Disallow anyone mentioned in the file 964 * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 965 */ 966 void 967 user(char *name) 968 { 969 char *cp, *shell; 970 971 if (logged_in) { 972 if (guest) { 973 reply(530, "Can't change user from guest login."); 974 return; 975 } else if (dochroot) { 976 reply(530, "Can't change user from chroot user."); 977 return; 978 } 979 end_login(); 980 } 981 982 guest = 0; 983 if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 984 if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 985 checkuser(_PATH_FTPUSERS, "anonymous", 0)) 986 reply(530, "User %s access denied.", name); 987 #ifdef VIRTUAL_HOSTING 988 else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 989 #else 990 else if ((pw = sgetpwnam("ftp")) != NULL) { 991 #endif 992 guest = 1; 993 askpasswd = 1; 994 reply(331, 995 "Guest login ok, send your email address as password."); 996 } else 997 reply(530, "User %s unknown.", name); 998 if (!askpasswd && logging) 999 syslog(LOG_NOTICE, 1000 "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 1001 return; 1002 } 1003 if (anon_only != 0) { 1004 reply(530, "Sorry, only anonymous ftp allowed."); 1005 return; 1006 } 1007 1008 if ((pw = sgetpwnam(name))) { 1009 if ((shell = pw->pw_shell) == NULL || *shell == 0) 1010 shell = _PATH_BSHELL; 1011 while ((cp = getusershell()) != NULL) 1012 if (strcmp(cp, shell) == 0) 1013 break; 1014 endusershell(); 1015 1016 if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 1017 reply(530, "User %s access denied.", name); 1018 if (logging) 1019 syslog(LOG_NOTICE, 1020 "FTP LOGIN REFUSED FROM %s, %s", 1021 remotehost, name); 1022 pw = (struct passwd *) NULL; 1023 return; 1024 } 1025 } 1026 if (logging) 1027 strncpy(curname, name, sizeof(curname)-1); 1028 1029 pwok = 0; 1030 #ifdef USE_PAM 1031 /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1032 #endif 1033 if (opiechallenge(&opiedata, name, opieprompt) == 0) { 1034 pwok = (pw != NULL) && 1035 opieaccessfile(remotehost) && 1036 opiealways(pw->pw_dir); 1037 reply(331, "Response to %s %s for %s.", 1038 opieprompt, pwok ? "requested" : "required", name); 1039 } else { 1040 pwok = 1; 1041 reply(331, "Password required for %s.", name); 1042 } 1043 askpasswd = 1; 1044 /* 1045 * Delay before reading passwd after first failed 1046 * attempt to slow down passwd-guessing programs. 1047 */ 1048 if (login_attempts) 1049 sleep((unsigned) login_attempts); 1050 } 1051 1052 /* 1053 * Check if a user is in the file "fname" 1054 */ 1055 static int 1056 checkuser(char *fname, char *name, int pwset) 1057 { 1058 FILE *fd; 1059 int found = 0; 1060 size_t len; 1061 char *line, *mp, *p; 1062 1063 if ((fd = fopen(fname, "r")) != NULL) { 1064 while (!found && (line = fgetln(fd, &len)) != NULL) { 1065 /* skip comments */ 1066 if (line[0] == '#') 1067 continue; 1068 if (line[len - 1] == '\n') { 1069 line[len - 1] = '\0'; 1070 mp = NULL; 1071 } else { 1072 if ((mp = malloc(len + 1)) == NULL) 1073 fatalerror("Ran out of memory."); 1074 memcpy(mp, line, len); 1075 mp[len] = '\0'; 1076 line = mp; 1077 } 1078 /* avoid possible leading and trailing whitespace */ 1079 p = strtok(line, " \t"); 1080 /* skip empty lines */ 1081 if (p == NULL) 1082 goto nextline; 1083 /* 1084 * if first chr is '@', check group membership 1085 */ 1086 if (p[0] == '@') { 1087 int i = 0; 1088 struct group *grp; 1089 1090 if ((grp = getgrnam(p+1)) == NULL) 1091 goto nextline; 1092 /* 1093 * Check user's default group 1094 */ 1095 if (pwset && grp->gr_gid == pw->pw_gid) 1096 found = 1; 1097 /* 1098 * Check supplementary groups 1099 */ 1100 while (!found && grp->gr_mem[i]) 1101 found = strcmp(name, 1102 grp->gr_mem[i++]) 1103 == 0; 1104 } 1105 /* 1106 * Otherwise, just check for username match 1107 */ 1108 else 1109 found = strcmp(p, name) == 0; 1110 nextline: 1111 if (mp) 1112 free(mp); 1113 } 1114 (void) fclose(fd); 1115 } 1116 return (found); 1117 } 1118 1119 /* 1120 * Terminate login as previous user, if any, resetting state; 1121 * used when USER command is given or login fails. 1122 */ 1123 static void 1124 end_login(void) 1125 { 1126 #ifdef USE_PAM 1127 int e; 1128 #endif 1129 1130 (void) seteuid((uid_t)0); 1131 if (logged_in) 1132 ftpd_logwtmp(ttyline, "", NULL); 1133 pw = NULL; 1134 #ifdef LOGIN_CAP 1135 setusercontext(NULL, getpwuid(0), (uid_t)0, 1136 LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1137 #endif 1138 #ifdef USE_PAM 1139 if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 1140 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 1141 if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 1142 syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 1143 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 1144 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 1145 pamh = NULL; 1146 #endif 1147 logged_in = 0; 1148 guest = 0; 1149 dochroot = 0; 1150 } 1151 1152 #ifdef USE_PAM 1153 1154 /* 1155 * the following code is stolen from imap-uw PAM authentication module and 1156 * login.c 1157 */ 1158 #define COPY_STRING(s) (s ? strdup(s) : NULL) 1159 1160 struct cred_t { 1161 const char *uname; /* user name */ 1162 const char *pass; /* password */ 1163 }; 1164 typedef struct cred_t cred_t; 1165 1166 static int 1167 auth_conv(int num_msg, const struct pam_message **msg, 1168 struct pam_response **resp, void *appdata) 1169 { 1170 int i; 1171 cred_t *cred = (cred_t *) appdata; 1172 struct pam_response *reply; 1173 1174 reply = calloc(num_msg, sizeof *reply); 1175 if (reply == NULL) 1176 return PAM_BUF_ERR; 1177 1178 for (i = 0; i < num_msg; i++) { 1179 switch (msg[i]->msg_style) { 1180 case PAM_PROMPT_ECHO_ON: /* assume want user name */ 1181 reply[i].resp_retcode = PAM_SUCCESS; 1182 reply[i].resp = COPY_STRING(cred->uname); 1183 /* PAM frees resp. */ 1184 break; 1185 case PAM_PROMPT_ECHO_OFF: /* assume want password */ 1186 reply[i].resp_retcode = PAM_SUCCESS; 1187 reply[i].resp = COPY_STRING(cred->pass); 1188 /* PAM frees resp. */ 1189 break; 1190 case PAM_TEXT_INFO: 1191 case PAM_ERROR_MSG: 1192 reply[i].resp_retcode = PAM_SUCCESS; 1193 reply[i].resp = NULL; 1194 break; 1195 default: /* unknown message style */ 1196 free(reply); 1197 return PAM_CONV_ERR; 1198 } 1199 } 1200 1201 *resp = reply; 1202 return PAM_SUCCESS; 1203 } 1204 1205 /* 1206 * Attempt to authenticate the user using PAM. Returns 0 if the user is 1207 * authenticated, or 1 if not authenticated. If some sort of PAM system 1208 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 1209 * function returns -1. This can be used as an indication that we should 1210 * fall back to a different authentication mechanism. 1211 */ 1212 static int 1213 auth_pam(struct passwd **ppw, const char *pass) 1214 { 1215 pam_handle_t *pamh = NULL; 1216 const char *tmpl_user; 1217 const void *item; 1218 int rval; 1219 int e; 1220 cred_t auth_cred = { (*ppw)->pw_name, pass }; 1221 struct pam_conv conv = { &auth_conv, &auth_cred }; 1222 1223 e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 1224 if (e != PAM_SUCCESS) { 1225 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 1226 return -1; 1227 } 1228 1229 e = pam_set_item(pamh, PAM_RHOST, remotehost); 1230 if (e != PAM_SUCCESS) { 1231 syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1232 pam_strerror(pamh, e)); 1233 return -1; 1234 } 1235 1236 e = pam_authenticate(pamh, 0); 1237 switch (e) { 1238 case PAM_SUCCESS: 1239 /* 1240 * With PAM we support the concept of a "template" 1241 * user. The user enters a login name which is 1242 * authenticated by PAM, usually via a remote service 1243 * such as RADIUS or TACACS+. If authentication 1244 * succeeds, a different but related "template" name 1245 * is used for setting the credentials, shell, and 1246 * home directory. The name the user enters need only 1247 * exist on the remote authentication server, but the 1248 * template name must be present in the local password 1249 * database. 1250 * 1251 * This is supported by two various mechanisms in the 1252 * individual modules. However, from the application's 1253 * point of view, the template user is always passed 1254 * back as a changed value of the PAM_USER item. 1255 */ 1256 if ((e = pam_get_item(pamh, PAM_USER, &item)) == 1257 PAM_SUCCESS) { 1258 tmpl_user = (const char *) item; 1259 if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 1260 *ppw = getpwnam(tmpl_user); 1261 } else 1262 syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 1263 pam_strerror(pamh, e)); 1264 rval = 0; 1265 break; 1266 1267 case PAM_AUTH_ERR: 1268 case PAM_USER_UNKNOWN: 1269 case PAM_MAXTRIES: 1270 rval = 1; 1271 break; 1272 1273 default: 1274 syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 1275 rval = -1; 1276 break; 1277 } 1278 1279 if (rval == 0) { 1280 e = pam_acct_mgmt(pamh, 0); 1281 if (e == PAM_NEW_AUTHTOK_REQD) { 1282 e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); 1283 if (e != PAM_SUCCESS) { 1284 syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e)); 1285 rval = 1; 1286 } 1287 } else if (e != PAM_SUCCESS) { 1288 rval = 1; 1289 } 1290 } 1291 1292 if (rval != 0) { 1293 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 1294 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 1295 } 1296 pamh = NULL; 1297 } 1298 return rval; 1299 } 1300 1301 #endif /* USE_PAM */ 1302 1303 void 1304 pass(char *passwd) 1305 { 1306 int rval; 1307 FILE *fd; 1308 #ifdef LOGIN_CAP 1309 login_cap_t *lc = NULL; 1310 #endif 1311 #ifdef USE_PAM 1312 int e; 1313 #endif 1314 char *xpasswd; 1315 1316 if (logged_in || askpasswd == 0) { 1317 reply(503, "Login with USER first."); 1318 return; 1319 } 1320 askpasswd = 0; 1321 if (!guest) { /* "ftp" is only account allowed no password */ 1322 if (pw == NULL) { 1323 rval = 1; /* failure below */ 1324 goto skip; 1325 } 1326 #ifdef USE_PAM 1327 rval = auth_pam(&pw, passwd); 1328 if (rval >= 0) { 1329 opieunlock(); 1330 goto skip; 1331 } 1332 #endif 1333 if (opieverify(&opiedata, passwd) == 0) 1334 xpasswd = pw->pw_passwd; 1335 else if (pwok) { 1336 xpasswd = crypt(passwd, pw->pw_passwd); 1337 if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1338 xpasswd = ":"; 1339 } else { 1340 rval = 1; 1341 goto skip; 1342 } 1343 rval = strcmp(pw->pw_passwd, xpasswd); 1344 if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1345 rval = 1; /* failure */ 1346 skip: 1347 /* 1348 * If rval == 1, the user failed the authentication check 1349 * above. If rval == 0, either PAM or local authentication 1350 * succeeded. 1351 */ 1352 if (rval) { 1353 reply(530, "Login incorrect."); 1354 if (logging) 1355 syslog(LOG_NOTICE, 1356 "FTP LOGIN FAILED FROM %s, %s", 1357 remotehost, curname); 1358 pw = NULL; 1359 if (login_attempts++ >= 5) { 1360 syslog(LOG_NOTICE, 1361 "repeated login failures from %s", 1362 remotehost); 1363 exit(0); 1364 } 1365 return; 1366 } 1367 } 1368 login_attempts = 0; /* this time successful */ 1369 if (setegid((gid_t)pw->pw_gid) < 0) { 1370 reply(550, "Can't set gid."); 1371 return; 1372 } 1373 /* May be overridden by login.conf */ 1374 (void) umask(defumask); 1375 #ifdef LOGIN_CAP 1376 if ((lc = login_getpwclass(pw)) != NULL) { 1377 char remote_ip[MAXHOSTNAMELEN]; 1378 1379 getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 1380 remote_ip, sizeof(remote_ip) - 1, NULL, 0, 1381 NI_NUMERICHOST); 1382 remote_ip[sizeof(remote_ip) - 1] = 0; 1383 if (!auth_hostok(lc, remotehost, remote_ip)) { 1384 syslog(LOG_INFO|LOG_AUTH, 1385 "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1386 pw->pw_name); 1387 reply(530, "Permission denied.\n"); 1388 pw = NULL; 1389 return; 1390 } 1391 if (!auth_timeok(lc, time(NULL))) { 1392 reply(530, "Login not available right now.\n"); 1393 pw = NULL; 1394 return; 1395 } 1396 } 1397 setusercontext(lc, pw, (uid_t)0, 1398 LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1399 LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1400 #else 1401 setlogin(pw->pw_name); 1402 (void) initgroups(pw->pw_name, pw->pw_gid); 1403 #endif 1404 1405 #ifdef USE_PAM 1406 if (pamh) { 1407 if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 1408 syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 1409 } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 1410 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 1411 } 1412 } 1413 #endif 1414 1415 /* open wtmp before chroot */ 1416 ftpd_logwtmp(ttyline, pw->pw_name, (struct sockaddr *)&his_addr); 1417 logged_in = 1; 1418 1419 if (guest && stats && statfd < 0) 1420 #ifdef VIRTUAL_HOSTING 1421 if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1422 #else 1423 if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1424 #endif 1425 stats = 0; 1426 1427 dochroot = 1428 #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1429 login_getcapbool(lc, "ftp-chroot", 0) || 1430 #endif 1431 checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1432 if (guest) { 1433 /* 1434 * We MUST do a chdir() after the chroot. Otherwise 1435 * the old current directory will be accessible as "." 1436 * outside the new root! 1437 */ 1438 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1439 reply(550, "Can't set guest privileges."); 1440 goto bad; 1441 } 1442 } else if (dochroot) { 1443 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1444 reply(550, "Can't change root."); 1445 goto bad; 1446 } 1447 } else if (chdir(pw->pw_dir) < 0) { 1448 if (chdir("/") < 0) { 1449 reply(530, "User %s: can't change directory to %s.", 1450 pw->pw_name, pw->pw_dir); 1451 goto bad; 1452 } else 1453 lreply(230, "No directory! Logging in with home=/"); 1454 } 1455 if (seteuid((uid_t)pw->pw_uid) < 0) { 1456 reply(550, "Can't set uid."); 1457 goto bad; 1458 } 1459 1460 /* 1461 * Display a login message, if it exists. 1462 * N.B. reply(230,) must follow the message. 1463 */ 1464 #ifdef VIRTUAL_HOSTING 1465 if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1466 #else 1467 if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1468 #endif 1469 char *cp, line[LINE_MAX]; 1470 1471 while (fgets(line, sizeof(line), fd) != NULL) { 1472 if ((cp = strchr(line, '\n')) != NULL) 1473 *cp = '\0'; 1474 lreply(230, "%s", line); 1475 } 1476 (void) fflush(stdout); 1477 (void) fclose(fd); 1478 } 1479 if (guest) { 1480 if (ident != NULL) 1481 free(ident); 1482 ident = strdup(passwd); 1483 if (ident == NULL) 1484 fatalerror("Ran out of memory."); 1485 1486 reply(230, "Guest login ok, access restrictions apply."); 1487 #ifdef SETPROCTITLE 1488 #ifdef VIRTUAL_HOSTING 1489 if (thishost != firsthost) 1490 snprintf(proctitle, sizeof(proctitle), 1491 "%s: anonymous(%s)/%s", remotehost, hostname, 1492 passwd); 1493 else 1494 #endif 1495 snprintf(proctitle, sizeof(proctitle), 1496 "%s: anonymous/%s", remotehost, passwd); 1497 setproctitle("%s", proctitle); 1498 #endif /* SETPROCTITLE */ 1499 if (logging) 1500 syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1501 remotehost, passwd); 1502 } else { 1503 if (dochroot) 1504 reply(230, "User %s logged in, " 1505 "access restrictions apply.", pw->pw_name); 1506 else 1507 reply(230, "User %s logged in.", pw->pw_name); 1508 1509 #ifdef SETPROCTITLE 1510 snprintf(proctitle, sizeof(proctitle), 1511 "%s: user/%s", remotehost, pw->pw_name); 1512 setproctitle("%s", proctitle); 1513 #endif /* SETPROCTITLE */ 1514 if (logging) 1515 syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1516 remotehost, pw->pw_name); 1517 } 1518 #ifdef LOGIN_CAP 1519 login_close(lc); 1520 #endif 1521 return; 1522 bad: 1523 /* Forget all about it... */ 1524 #ifdef LOGIN_CAP 1525 login_close(lc); 1526 #endif 1527 end_login(); 1528 } 1529 1530 void 1531 retrieve(char *cmd, char *name) 1532 { 1533 FILE *fin, *dout; 1534 struct stat st; 1535 int (*closefunc)(FILE *); 1536 time_t start; 1537 1538 if (cmd == 0) { 1539 fin = fopen(name, "r"), closefunc = fclose; 1540 st.st_size = 0; 1541 } else { 1542 char line[BUFSIZ]; 1543 1544 (void) snprintf(line, sizeof(line), cmd, name), name = line; 1545 fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1546 st.st_size = -1; 1547 st.st_blksize = BUFSIZ; 1548 } 1549 if (fin == NULL) { 1550 if (errno != 0) { 1551 perror_reply(550, name); 1552 if (cmd == 0) { 1553 LOGCMD("get", name); 1554 } 1555 } 1556 return; 1557 } 1558 byte_count = -1; 1559 if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1560 reply(550, "%s: not a plain file.", name); 1561 goto done; 1562 } 1563 if (restart_point) { 1564 if (type == TYPE_A) { 1565 off_t i, n; 1566 int c; 1567 1568 n = restart_point; 1569 i = 0; 1570 while (i++ < n) { 1571 if ((c=getc(fin)) == EOF) { 1572 perror_reply(550, name); 1573 goto done; 1574 } 1575 if (c == '\n') 1576 i++; 1577 } 1578 } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1579 perror_reply(550, name); 1580 goto done; 1581 } 1582 } 1583 dout = dataconn(name, st.st_size, "w"); 1584 if (dout == NULL) 1585 goto done; 1586 time(&start); 1587 send_data(fin, dout, st.st_blksize, st.st_size, 1588 restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 1589 if (cmd == 0 && guest && stats) 1590 logxfer(name, st.st_size, start); 1591 (void) fclose(dout); 1592 data = -1; 1593 pdata = -1; 1594 done: 1595 if (cmd == 0) 1596 LOGBYTES("get", name, byte_count); 1597 (*closefunc)(fin); 1598 } 1599 1600 void 1601 store(char *name, char *mode, int unique) 1602 { 1603 int fd; 1604 FILE *fout, *din; 1605 struct stat st; 1606 int (*closefunc)(FILE *); 1607 1608 if (*mode == 'a') { /* APPE */ 1609 if (unique) { 1610 /* Programming error */ 1611 syslog(LOG_ERR, "Internal: unique flag to APPE"); 1612 unique = 0; 1613 } 1614 if (guest && noguestmod) { 1615 reply(550, "Appending to existing file denied"); 1616 goto err; 1617 } 1618 restart_point = 0; /* not affected by preceding REST */ 1619 } 1620 if (unique) /* STOU overrides REST */ 1621 restart_point = 0; 1622 if (guest && noguestmod) { 1623 if (restart_point) { /* guest STOR w/REST */ 1624 reply(550, "Modifying existing file denied"); 1625 goto err; 1626 } else /* treat guest STOR as STOU */ 1627 unique = 1; 1628 } 1629 1630 if (restart_point) 1631 mode = "r+"; /* so ASCII manual seek can work */ 1632 if (unique) { 1633 if ((fd = guniquefd(name, &name)) < 0) 1634 goto err; 1635 fout = fdopen(fd, mode); 1636 } else 1637 fout = fopen(name, mode); 1638 closefunc = fclose; 1639 if (fout == NULL) { 1640 perror_reply(553, name); 1641 goto err; 1642 } 1643 byte_count = -1; 1644 if (restart_point) { 1645 if (type == TYPE_A) { 1646 off_t i, n; 1647 int c; 1648 1649 n = restart_point; 1650 i = 0; 1651 while (i++ < n) { 1652 if ((c=getc(fout)) == EOF) { 1653 perror_reply(550, name); 1654 goto done; 1655 } 1656 if (c == '\n') 1657 i++; 1658 } 1659 /* 1660 * We must do this seek to "current" position 1661 * because we are changing from reading to 1662 * writing. 1663 */ 1664 if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) { 1665 perror_reply(550, name); 1666 goto done; 1667 } 1668 } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1669 perror_reply(550, name); 1670 goto done; 1671 } 1672 } 1673 din = dataconn(name, (off_t)-1, "r"); 1674 if (din == NULL) 1675 goto done; 1676 if (receive_data(din, fout) == 0) { 1677 if (unique) 1678 reply(226, "Transfer complete (unique file name:%s).", 1679 name); 1680 else 1681 reply(226, "Transfer complete."); 1682 } 1683 (void) fclose(din); 1684 data = -1; 1685 pdata = -1; 1686 done: 1687 LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1688 (*closefunc)(fout); 1689 return; 1690 err: 1691 LOGCMD(*mode == 'a' ? "append" : "put" , name); 1692 return; 1693 } 1694 1695 static FILE * 1696 getdatasock(char *mode) 1697 { 1698 int on = 1, s, t, tries; 1699 1700 if (data >= 0) 1701 return (fdopen(data, mode)); 1702 (void) seteuid((uid_t)0); 1703 1704 s = socket(data_dest.su_family, SOCK_STREAM, 0); 1705 if (s < 0) 1706 goto bad; 1707 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1708 syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1709 /* anchor socket to avoid multi-homing problems */ 1710 data_source = ctrl_addr; 1711 data_source.su_port = htons(20); /* ftp-data port */ 1712 for (tries = 1; ; tries++) { 1713 if (bind(s, (struct sockaddr *)&data_source, 1714 data_source.su_len) >= 0) 1715 break; 1716 if (errno != EADDRINUSE || tries > 10) 1717 goto bad; 1718 sleep(tries); 1719 } 1720 (void) seteuid((uid_t)pw->pw_uid); 1721 #ifdef IP_TOS 1722 if (data_source.su_family == AF_INET) 1723 { 1724 on = IPTOS_THROUGHPUT; 1725 if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1726 syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 1727 } 1728 #endif 1729 #ifdef TCP_NOPUSH 1730 /* 1731 * Turn off push flag to keep sender TCP from sending short packets 1732 * at the boundaries of each write(). Should probably do a SO_SNDBUF 1733 * to set the send buffer size as well, but that may not be desirable 1734 * in heavy-load situations. 1735 */ 1736 on = 1; 1737 if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1738 syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 1739 #endif 1740 #ifdef SO_SNDBUF 1741 on = 65536; 1742 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0) 1743 syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m"); 1744 #endif 1745 1746 return (fdopen(s, mode)); 1747 bad: 1748 /* Return the real value of errno (close may change it) */ 1749 t = errno; 1750 (void) seteuid((uid_t)pw->pw_uid); 1751 (void) close(s); 1752 errno = t; 1753 return (NULL); 1754 } 1755 1756 static FILE * 1757 dataconn(char *name, off_t size, char *mode) 1758 { 1759 char sizebuf[32]; 1760 FILE *file; 1761 int retry = 0, tos; 1762 1763 file_size = size; 1764 byte_count = 0; 1765 if (size != (off_t) -1) 1766 (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1767 else 1768 *sizebuf = '\0'; 1769 if (pdata >= 0) { 1770 union sockunion from; 1771 int flags; 1772 int s, fromlen = ctrl_addr.su_len; 1773 struct timeval timeout; 1774 fd_set set; 1775 1776 FD_ZERO(&set); 1777 FD_SET(pdata, &set); 1778 1779 timeout.tv_usec = 0; 1780 timeout.tv_sec = 120; 1781 1782 /* 1783 * Granted a socket is in the blocking I/O mode, 1784 * accept() will block after a successful select() 1785 * if the selected connection dies in between. 1786 * Therefore set the non-blocking I/O flag here. 1787 */ 1788 if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 1789 fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 1790 goto pdata_err; 1791 if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 || 1792 (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 1793 goto pdata_err; 1794 (void) close(pdata); 1795 pdata = s; 1796 /* 1797 * Unset the inherited non-blocking I/O flag 1798 * on the child socket so stdio can work on it. 1799 */ 1800 if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 1801 fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 1802 goto pdata_err; 1803 #ifdef IP_TOS 1804 if (from.su_family == AF_INET) 1805 { 1806 tos = IPTOS_THROUGHPUT; 1807 if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1808 syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 1809 } 1810 #endif 1811 reply(150, "Opening %s mode data connection for '%s'%s.", 1812 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1813 return (fdopen(pdata, mode)); 1814 pdata_err: 1815 reply(425, "Can't open data connection."); 1816 (void) close(pdata); 1817 pdata = -1; 1818 return (NULL); 1819 } 1820 if (data >= 0) { 1821 reply(125, "Using existing data connection for '%s'%s.", 1822 name, sizebuf); 1823 usedefault = 1; 1824 return (fdopen(data, mode)); 1825 } 1826 if (usedefault) 1827 data_dest = his_addr; 1828 usedefault = 1; 1829 file = getdatasock(mode); 1830 if (file == NULL) { 1831 char hostbuf[BUFSIZ], portbuf[BUFSIZ]; 1832 getnameinfo((struct sockaddr *)&data_source, 1833 data_source.su_len, hostbuf, sizeof(hostbuf) - 1, 1834 portbuf, sizeof(portbuf), 1835 NI_NUMERICHOST|NI_NUMERICSERV); 1836 reply(425, "Can't create data socket (%s,%s): %s.", 1837 hostbuf, portbuf, strerror(errno)); 1838 return (NULL); 1839 } 1840 data = fileno(file); 1841 while (connect(data, (struct sockaddr *)&data_dest, 1842 data_dest.su_len) < 0) { 1843 if (errno == EADDRINUSE && retry < swaitmax) { 1844 sleep((unsigned) swaitint); 1845 retry += swaitint; 1846 continue; 1847 } 1848 perror_reply(425, "Can't build data connection"); 1849 (void) fclose(file); 1850 data = -1; 1851 return (NULL); 1852 } 1853 reply(150, "Opening %s mode data connection for '%s'%s.", 1854 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1855 return (file); 1856 } 1857 1858 /* 1859 * Tranfer the contents of "instr" to "outstr" peer using the appropriate 1860 * encapsulation of the data subject to Mode, Structure, and Type. 1861 * 1862 * NB: Form isn't handled. 1863 */ 1864 static int 1865 send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg) 1866 { 1867 int c, filefd, netfd; 1868 char *buf; 1869 off_t cnt; 1870 1871 transflag++; 1872 switch (type) { 1873 1874 case TYPE_A: 1875 while ((c = getc(instr)) != EOF) { 1876 if (recvurg) 1877 goto got_oob; 1878 byte_count++; 1879 if (c == '\n') { 1880 if (ferror(outstr)) 1881 goto data_err; 1882 (void) putc('\r', outstr); 1883 } 1884 (void) putc(c, outstr); 1885 } 1886 if (recvurg) 1887 goto got_oob; 1888 fflush(outstr); 1889 transflag = 0; 1890 if (ferror(instr)) 1891 goto file_err; 1892 if (ferror(outstr)) 1893 goto data_err; 1894 reply(226, "Transfer complete."); 1895 return (0); 1896 1897 case TYPE_I: 1898 case TYPE_L: 1899 /* 1900 * isreg is only set if we are not doing restart and we 1901 * are sending a regular file 1902 */ 1903 netfd = fileno(outstr); 1904 filefd = fileno(instr); 1905 1906 if (isreg) { 1907 1908 off_t offset; 1909 int err; 1910 1911 err = cnt = offset = 0; 1912 1913 while (err != -1 && filesize > 0) { 1914 err = sendfile(filefd, netfd, offset, 0, 1915 (struct sf_hdtr *) NULL, &cnt, 0); 1916 /* 1917 * Calculate byte_count before OOB processing. 1918 * It can be used in myoob() later. 1919 */ 1920 byte_count += cnt; 1921 if (recvurg) 1922 goto got_oob; 1923 offset += cnt; 1924 filesize -= cnt; 1925 1926 if (err == -1) { 1927 if (!cnt) 1928 goto oldway; 1929 1930 goto data_err; 1931 } 1932 } 1933 1934 transflag = 0; 1935 reply(226, "Transfer complete."); 1936 return (0); 1937 } 1938 1939 oldway: 1940 if ((buf = malloc((u_int)blksize)) == NULL) { 1941 transflag = 0; 1942 perror_reply(451, "Local resource failure: malloc"); 1943 return (-1); 1944 } 1945 1946 while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1947 write(netfd, buf, cnt) == cnt) 1948 byte_count += cnt; 1949 transflag = 0; 1950 (void)free(buf); 1951 if (cnt != 0) { 1952 if (cnt < 0) 1953 goto file_err; 1954 goto data_err; 1955 } 1956 reply(226, "Transfer complete."); 1957 return (0); 1958 default: 1959 transflag = 0; 1960 reply(550, "Unimplemented TYPE %d in send_data", type); 1961 return (-1); 1962 } 1963 1964 data_err: 1965 transflag = 0; 1966 perror_reply(426, "Data connection"); 1967 return (-1); 1968 1969 file_err: 1970 transflag = 0; 1971 perror_reply(551, "Error on input file"); 1972 return (-1); 1973 1974 got_oob: 1975 myoob(); 1976 recvurg = 0; 1977 transflag = 0; 1978 return (-1); 1979 } 1980 1981 /* 1982 * Transfer data from peer to "outstr" using the appropriate encapulation of 1983 * the data subject to Mode, Structure, and Type. 1984 * 1985 * N.B.: Form isn't handled. 1986 */ 1987 static int 1988 receive_data(FILE *instr, FILE *outstr) 1989 { 1990 int c; 1991 int cnt, bare_lfs; 1992 char buf[BUFSIZ]; 1993 1994 transflag++; 1995 bare_lfs = 0; 1996 1997 switch (type) { 1998 1999 case TYPE_I: 2000 case TYPE_L: 2001 while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 2002 if (recvurg) 2003 goto got_oob; 2004 if (write(fileno(outstr), buf, cnt) != cnt) 2005 goto file_err; 2006 byte_count += cnt; 2007 } 2008 if (recvurg) 2009 goto got_oob; 2010 if (cnt < 0) 2011 goto data_err; 2012 transflag = 0; 2013 return (0); 2014 2015 case TYPE_E: 2016 reply(553, "TYPE E not implemented."); 2017 transflag = 0; 2018 return (-1); 2019 2020 case TYPE_A: 2021 while ((c = getc(instr)) != EOF) { 2022 if (recvurg) 2023 goto got_oob; 2024 byte_count++; 2025 if (c == '\n') 2026 bare_lfs++; 2027 while (c == '\r') { 2028 if (ferror(outstr)) 2029 goto data_err; 2030 if ((c = getc(instr)) != '\n') { 2031 (void) putc ('\r', outstr); 2032 if (c == '\0' || c == EOF) 2033 goto contin2; 2034 } 2035 } 2036 (void) putc(c, outstr); 2037 contin2: ; 2038 } 2039 if (recvurg) 2040 goto got_oob; 2041 fflush(outstr); 2042 if (ferror(instr)) 2043 goto data_err; 2044 if (ferror(outstr)) 2045 goto file_err; 2046 transflag = 0; 2047 if (bare_lfs) { 2048 lreply(226, 2049 "WARNING! %d bare linefeeds received in ASCII mode", 2050 bare_lfs); 2051 (void)printf(" File may not have transferred correctly.\r\n"); 2052 } 2053 return (0); 2054 default: 2055 reply(550, "Unimplemented TYPE %d in receive_data", type); 2056 transflag = 0; 2057 return (-1); 2058 } 2059 2060 data_err: 2061 transflag = 0; 2062 perror_reply(426, "Data Connection"); 2063 return (-1); 2064 2065 file_err: 2066 transflag = 0; 2067 perror_reply(452, "Error writing file"); 2068 return (-1); 2069 2070 got_oob: 2071 myoob(); 2072 recvurg = 0; 2073 transflag = 0; 2074 return (-1); 2075 } 2076 2077 void 2078 statfilecmd(char *filename) 2079 { 2080 FILE *fin; 2081 int c; 2082 char line[LINE_MAX]; 2083 2084 (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2085 fin = ftpd_popen(line, "r"); 2086 lreply(211, "status of %s:", filename); 2087 while ((c = getc(fin)) != EOF) { 2088 if (c == '\n') { 2089 if (ferror(stdout)){ 2090 perror_reply(421, "control connection"); 2091 (void) ftpd_pclose(fin); 2092 dologout(1); 2093 /* NOTREACHED */ 2094 } 2095 if (ferror(fin)) { 2096 perror_reply(551, filename); 2097 (void) ftpd_pclose(fin); 2098 return; 2099 } 2100 (void) putc('\r', stdout); 2101 } 2102 (void) putc(c, stdout); 2103 } 2104 (void) ftpd_pclose(fin); 2105 reply(211, "End of Status"); 2106 } 2107 2108 void 2109 statcmd(void) 2110 { 2111 union sockunion *su; 2112 u_char *a, *p; 2113 char hname[NI_MAXHOST]; 2114 int ispassive; 2115 2116 lreply(211, "%s FTP server status:", hostname, version); 2117 printf(" %s\r\n", version); 2118 printf(" Connected to %s", remotehost); 2119 if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2120 hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 2121 if (strcmp(hname, remotehost) != 0) 2122 printf(" (%s)", hname); 2123 } 2124 printf("\r\n"); 2125 if (logged_in) { 2126 if (guest) 2127 printf(" Logged in anonymously\r\n"); 2128 else 2129 printf(" Logged in as %s\r\n", pw->pw_name); 2130 } else if (askpasswd) 2131 printf(" Waiting for password\r\n"); 2132 else 2133 printf(" Waiting for user name\r\n"); 2134 printf(" TYPE: %s", typenames[type]); 2135 if (type == TYPE_A || type == TYPE_E) 2136 printf(", FORM: %s", formnames[form]); 2137 if (type == TYPE_L) 2138 #if NBBY == 8 2139 printf(" %d", NBBY); 2140 #else 2141 printf(" %d", bytesize); /* need definition! */ 2142 #endif 2143 printf("; STRUcture: %s; transfer MODE: %s\r\n", 2144 strunames[stru], modenames[mode]); 2145 if (data != -1) 2146 printf(" Data connection open\r\n"); 2147 else if (pdata != -1) { 2148 ispassive = 1; 2149 su = &pasv_addr; 2150 goto printaddr; 2151 } else if (usedefault == 0) { 2152 ispassive = 0; 2153 su = &data_dest; 2154 printaddr: 2155 #define UC(b) (((int) b) & 0xff) 2156 if (epsvall) { 2157 printf(" EPSV only mode (EPSV ALL)\r\n"); 2158 goto epsvonly; 2159 } 2160 2161 /* PORT/PASV */ 2162 if (su->su_family == AF_INET) { 2163 a = (u_char *) &su->su_sin.sin_addr; 2164 p = (u_char *) &su->su_sin.sin_port; 2165 printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 2166 ispassive ? "PASV" : "PORT", 2167 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 2168 UC(p[0]), UC(p[1])); 2169 } 2170 2171 /* LPRT/LPSV */ 2172 { 2173 int alen, af, i; 2174 2175 switch (su->su_family) { 2176 case AF_INET: 2177 a = (u_char *) &su->su_sin.sin_addr; 2178 p = (u_char *) &su->su_sin.sin_port; 2179 alen = sizeof(su->su_sin.sin_addr); 2180 af = 4; 2181 break; 2182 case AF_INET6: 2183 a = (u_char *) &su->su_sin6.sin6_addr; 2184 p = (u_char *) &su->su_sin6.sin6_port; 2185 alen = sizeof(su->su_sin6.sin6_addr); 2186 af = 6; 2187 break; 2188 default: 2189 af = 0; 2190 break; 2191 } 2192 if (af) { 2193 printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 2194 af, alen); 2195 for (i = 0; i < alen; i++) 2196 printf("%d,", UC(a[i])); 2197 printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 2198 } 2199 } 2200 2201 epsvonly:; 2202 /* EPRT/EPSV */ 2203 { 2204 int af; 2205 2206 switch (su->su_family) { 2207 case AF_INET: 2208 af = 1; 2209 break; 2210 case AF_INET6: 2211 af = 2; 2212 break; 2213 default: 2214 af = 0; 2215 break; 2216 } 2217 if (af) { 2218 union sockunion tmp; 2219 2220 tmp = *su; 2221 if (tmp.su_family == AF_INET6) 2222 tmp.su_sin6.sin6_scope_id = 0; 2223 if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 2224 hname, sizeof(hname) - 1, NULL, 0, 2225 NI_NUMERICHOST)) { 2226 printf(" %s |%d|%s|%d|\r\n", 2227 ispassive ? "EPSV" : "EPRT", 2228 af, hname, htons(tmp.su_port)); 2229 } 2230 } 2231 } 2232 #undef UC 2233 } else 2234 printf(" No data connection\r\n"); 2235 reply(211, "End of status"); 2236 } 2237 2238 void 2239 fatalerror(char *s) 2240 { 2241 2242 reply(451, "Error in server: %s\n", s); 2243 reply(221, "Closing connection due to server error."); 2244 dologout(0); 2245 /* NOTREACHED */ 2246 } 2247 2248 void 2249 reply(int n, const char *fmt, ...) 2250 { 2251 va_list ap; 2252 2253 va_start(ap, fmt); 2254 (void)printf("%d ", n); 2255 (void)vprintf(fmt, ap); 2256 (void)printf("\r\n"); 2257 (void)fflush(stdout); 2258 if (ftpdebug) { 2259 syslog(LOG_DEBUG, "<--- %d ", n); 2260 vsyslog(LOG_DEBUG, fmt, ap); 2261 } 2262 } 2263 2264 void 2265 lreply(int n, const char *fmt, ...) 2266 { 2267 va_list ap; 2268 2269 va_start(ap, fmt); 2270 (void)printf("%d- ", n); 2271 (void)vprintf(fmt, ap); 2272 (void)printf("\r\n"); 2273 (void)fflush(stdout); 2274 if (ftpdebug) { 2275 syslog(LOG_DEBUG, "<--- %d- ", n); 2276 vsyslog(LOG_DEBUG, fmt, ap); 2277 } 2278 } 2279 2280 static void 2281 ack(char *s) 2282 { 2283 2284 reply(250, "%s command successful.", s); 2285 } 2286 2287 void 2288 nack(char *s) 2289 { 2290 2291 reply(502, "%s command not implemented.", s); 2292 } 2293 2294 /* ARGSUSED */ 2295 void 2296 yyerror(char *s) 2297 { 2298 char *cp; 2299 2300 if ((cp = strchr(cbuf,'\n'))) 2301 *cp = '\0'; 2302 reply(500, "'%s': command not understood.", cbuf); 2303 } 2304 2305 void 2306 delete(char *name) 2307 { 2308 struct stat st; 2309 2310 LOGCMD("delete", name); 2311 if (lstat(name, &st) < 0) { 2312 perror_reply(550, name); 2313 return; 2314 } 2315 if ((st.st_mode&S_IFMT) == S_IFDIR) { 2316 if (rmdir(name) < 0) { 2317 perror_reply(550, name); 2318 return; 2319 } 2320 goto done; 2321 } 2322 if (unlink(name) < 0) { 2323 perror_reply(550, name); 2324 return; 2325 } 2326 done: 2327 ack("DELE"); 2328 } 2329 2330 void 2331 cwd(char *path) 2332 { 2333 2334 if (chdir(path) < 0) 2335 perror_reply(550, path); 2336 else 2337 ack("CWD"); 2338 } 2339 2340 void 2341 makedir(char *name) 2342 { 2343 char *s; 2344 2345 LOGCMD("mkdir", name); 2346 if (guest && noguestmkd) 2347 reply(550, "%s: permission denied", name); 2348 else if (mkdir(name, 0777) < 0) 2349 perror_reply(550, name); 2350 else { 2351 if ((s = doublequote(name)) == NULL) 2352 fatalerror("Ran out of memory."); 2353 reply(257, "\"%s\" directory created.", s); 2354 free(s); 2355 } 2356 } 2357 2358 void 2359 removedir(char *name) 2360 { 2361 2362 LOGCMD("rmdir", name); 2363 if (rmdir(name) < 0) 2364 perror_reply(550, name); 2365 else 2366 ack("RMD"); 2367 } 2368 2369 void 2370 pwd(void) 2371 { 2372 char *s, path[MAXPATHLEN + 1]; 2373 2374 if (getwd(path) == (char *)NULL) 2375 reply(550, "%s.", path); 2376 else { 2377 if ((s = doublequote(path)) == NULL) 2378 fatalerror("Ran out of memory."); 2379 reply(257, "\"%s\" is current directory.", s); 2380 free(s); 2381 } 2382 } 2383 2384 char * 2385 renamefrom(char *name) 2386 { 2387 struct stat st; 2388 2389 if (lstat(name, &st) < 0) { 2390 perror_reply(550, name); 2391 return ((char *)0); 2392 } 2393 reply(350, "File exists, ready for destination name"); 2394 return (name); 2395 } 2396 2397 void 2398 renamecmd(char *from, char *to) 2399 { 2400 struct stat st; 2401 2402 LOGCMD2("rename", from, to); 2403 2404 if (guest && (stat(to, &st) == 0)) { 2405 reply(550, "%s: permission denied", to); 2406 return; 2407 } 2408 2409 if (rename(from, to) < 0) 2410 perror_reply(550, "rename"); 2411 else 2412 ack("RNTO"); 2413 } 2414 2415 static void 2416 dolog(struct sockaddr *who) 2417 { 2418 int error; 2419 2420 realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 2421 2422 #ifdef SETPROCTITLE 2423 #ifdef VIRTUAL_HOSTING 2424 if (thishost != firsthost) 2425 snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2426 remotehost, hostname); 2427 else 2428 #endif 2429 snprintf(proctitle, sizeof(proctitle), "%s: connected", 2430 remotehost); 2431 setproctitle("%s", proctitle); 2432 #endif /* SETPROCTITLE */ 2433 2434 if (logging) { 2435 #ifdef VIRTUAL_HOSTING 2436 if (thishost != firsthost) 2437 syslog(LOG_INFO, "connection from %s (to %s)", 2438 remotehost, hostname); 2439 else 2440 #endif 2441 { 2442 char who_name[MAXHOSTNAMELEN]; 2443 2444 error = getnameinfo(who, who->sa_len, 2445 who_name, sizeof(who_name) - 1, 2446 NULL, 0, NI_NUMERICHOST); 2447 syslog(LOG_INFO, "connection from %s (%s)", remotehost, 2448 error == 0 ? who_name : ""); 2449 } 2450 } 2451 } 2452 2453 /* 2454 * Record logout in wtmp file 2455 * and exit with supplied status. 2456 */ 2457 void 2458 dologout(int status) 2459 { 2460 /* 2461 * Prevent reception of SIGURG from resulting in a resumption 2462 * back to the main program loop. 2463 */ 2464 transflag = 0; 2465 2466 if (logged_in) { 2467 (void) seteuid((uid_t)0); 2468 ftpd_logwtmp(ttyline, "", NULL); 2469 } 2470 /* beware of flushing buffers after a SIGPIPE */ 2471 _exit(status); 2472 } 2473 2474 static void 2475 sigurg(int signo) 2476 { 2477 2478 recvurg = 1; 2479 } 2480 2481 static void 2482 myoob(void) 2483 { 2484 char *cp; 2485 2486 /* only process if transfer occurring */ 2487 if (!transflag) 2488 return; 2489 cp = tmpline; 2490 if (getline(cp, 7, stdin) == NULL) { 2491 reply(221, "You could at least say goodbye."); 2492 dologout(0); 2493 } 2494 upper(cp); 2495 if (strcmp(cp, "ABOR\r\n") == 0) { 2496 tmpline[0] = '\0'; 2497 reply(426, "Transfer aborted. Data connection closed."); 2498 reply(226, "Abort successful"); 2499 } 2500 if (strcmp(cp, "STAT\r\n") == 0) { 2501 tmpline[0] = '\0'; 2502 if (file_size != (off_t) -1) 2503 reply(213, "Status: %qd of %qd bytes transferred", 2504 byte_count, file_size); 2505 else 2506 reply(213, "Status: %qd bytes transferred", byte_count); 2507 } 2508 } 2509 2510 /* 2511 * Note: a response of 425 is not mentioned as a possible response to 2512 * the PASV command in RFC959. However, it has been blessed as 2513 * a legitimate response by Jon Postel in a telephone conversation 2514 * with Rick Adams on 25 Jan 89. 2515 */ 2516 void 2517 passive(void) 2518 { 2519 int len, on; 2520 char *p, *a; 2521 2522 if (pdata >= 0) /* close old port if one set */ 2523 close(pdata); 2524 2525 pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2526 if (pdata < 0) { 2527 perror_reply(425, "Can't open passive connection"); 2528 return; 2529 } 2530 on = 1; 2531 if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 2532 syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 2533 2534 (void) seteuid((uid_t)0); 2535 2536 #ifdef IP_PORTRANGE 2537 if (ctrl_addr.su_family == AF_INET) { 2538 on = restricted_data_ports ? IP_PORTRANGE_HIGH 2539 : IP_PORTRANGE_DEFAULT; 2540 2541 if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2542 &on, sizeof(on)) < 0) 2543 goto pasv_error; 2544 } 2545 #endif 2546 #ifdef IPV6_PORTRANGE 2547 if (ctrl_addr.su_family == AF_INET6) { 2548 on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 2549 : IPV6_PORTRANGE_DEFAULT; 2550 2551 if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 2552 &on, sizeof(on)) < 0) 2553 goto pasv_error; 2554 } 2555 #endif 2556 2557 pasv_addr = ctrl_addr; 2558 pasv_addr.su_port = 0; 2559 if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2560 goto pasv_error; 2561 2562 (void) seteuid((uid_t)pw->pw_uid); 2563 2564 len = sizeof(pasv_addr); 2565 if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2566 goto pasv_error; 2567 if (listen(pdata, 1) < 0) 2568 goto pasv_error; 2569 if (pasv_addr.su_family == AF_INET) 2570 a = (char *) &pasv_addr.su_sin.sin_addr; 2571 else if (pasv_addr.su_family == AF_INET6 && 2572 IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 2573 a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 2574 else 2575 goto pasv_error; 2576 2577 p = (char *) &pasv_addr.su_port; 2578 2579 #define UC(b) (((int) b) & 0xff) 2580 2581 reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2582 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2583 return; 2584 2585 pasv_error: 2586 (void) seteuid((uid_t)pw->pw_uid); 2587 (void) close(pdata); 2588 pdata = -1; 2589 perror_reply(425, "Can't open passive connection"); 2590 return; 2591 } 2592 2593 /* 2594 * Long Passive defined in RFC 1639. 2595 * 228 Entering Long Passive Mode 2596 * (af, hal, h1, h2, h3,..., pal, p1, p2...) 2597 */ 2598 2599 void 2600 long_passive(char *cmd, int pf) 2601 { 2602 int len, on; 2603 char *p, *a; 2604 2605 if (pdata >= 0) /* close old port if one set */ 2606 close(pdata); 2607 2608 if (pf != PF_UNSPEC) { 2609 if (ctrl_addr.su_family != pf) { 2610 switch (ctrl_addr.su_family) { 2611 case AF_INET: 2612 pf = 1; 2613 break; 2614 case AF_INET6: 2615 pf = 2; 2616 break; 2617 default: 2618 pf = 0; 2619 break; 2620 } 2621 /* 2622 * XXX 2623 * only EPRT/EPSV ready clients will understand this 2624 */ 2625 if (strcmp(cmd, "EPSV") == 0 && pf) { 2626 reply(522, "Network protocol mismatch, " 2627 "use (%d)", pf); 2628 } else 2629 reply(501, "Network protocol mismatch"); /*XXX*/ 2630 2631 return; 2632 } 2633 } 2634 2635 pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2636 if (pdata < 0) { 2637 perror_reply(425, "Can't open passive connection"); 2638 return; 2639 } 2640 on = 1; 2641 if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 2642 syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 2643 2644 (void) seteuid((uid_t)0); 2645 2646 pasv_addr = ctrl_addr; 2647 pasv_addr.su_port = 0; 2648 len = pasv_addr.su_len; 2649 2650 #ifdef IP_PORTRANGE 2651 if (ctrl_addr.su_family == AF_INET) { 2652 on = restricted_data_ports ? IP_PORTRANGE_HIGH 2653 : IP_PORTRANGE_DEFAULT; 2654 2655 if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2656 &on, sizeof(on)) < 0) 2657 goto pasv_error; 2658 } 2659 #endif 2660 #ifdef IPV6_PORTRANGE 2661 if (ctrl_addr.su_family == AF_INET6) { 2662 on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 2663 : IPV6_PORTRANGE_DEFAULT; 2664 2665 if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 2666 &on, sizeof(on)) < 0) 2667 goto pasv_error; 2668 } 2669 #endif 2670 2671 if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 2672 goto pasv_error; 2673 2674 (void) seteuid((uid_t)pw->pw_uid); 2675 2676 if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2677 goto pasv_error; 2678 if (listen(pdata, 1) < 0) 2679 goto pasv_error; 2680 2681 #define UC(b) (((int) b) & 0xff) 2682 2683 if (strcmp(cmd, "LPSV") == 0) { 2684 p = (char *)&pasv_addr.su_port; 2685 switch (pasv_addr.su_family) { 2686 case AF_INET: 2687 a = (char *) &pasv_addr.su_sin.sin_addr; 2688 v4_reply: 2689 reply(228, 2690 "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 2691 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 2692 2, UC(p[0]), UC(p[1])); 2693 return; 2694 case AF_INET6: 2695 if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 2696 a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 2697 goto v4_reply; 2698 } 2699 a = (char *) &pasv_addr.su_sin6.sin6_addr; 2700 reply(228, 2701 "Entering Long Passive Mode " 2702 "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 2703 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 2704 UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 2705 UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 2706 UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 2707 2, UC(p[0]), UC(p[1])); 2708 return; 2709 } 2710 } else if (strcmp(cmd, "EPSV") == 0) { 2711 switch (pasv_addr.su_family) { 2712 case AF_INET: 2713 case AF_INET6: 2714 reply(229, "Entering Extended Passive Mode (|||%d|)", 2715 ntohs(pasv_addr.su_port)); 2716 return; 2717 } 2718 } else { 2719 /* more proper error code? */ 2720 } 2721 2722 pasv_error: 2723 (void) seteuid((uid_t)pw->pw_uid); 2724 (void) close(pdata); 2725 pdata = -1; 2726 perror_reply(425, "Can't open passive connection"); 2727 return; 2728 } 2729 2730 /* 2731 * Generate unique name for file with basename "local" 2732 * and open the file in order to avoid possible races. 2733 * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 2734 * Return descriptor to the file, set "name" to its name. 2735 * 2736 * Generates failure reply on error. 2737 */ 2738 static int 2739 guniquefd(char *local, char **name) 2740 { 2741 static char new[MAXPATHLEN]; 2742 struct stat st; 2743 char *cp; 2744 int count; 2745 int fd; 2746 2747 cp = strrchr(local, '/'); 2748 if (cp) 2749 *cp = '\0'; 2750 if (stat(cp ? local : ".", &st) < 0) { 2751 perror_reply(553, cp ? local : "."); 2752 return (-1); 2753 } 2754 if (cp) { 2755 /* 2756 * Let not overwrite dirname with counter suffix. 2757 * -4 is for /nn\0 2758 * In this extreme case dot won't be put in front of suffix. 2759 */ 2760 if (strlen(local) > sizeof(new) - 4) { 2761 reply(553, "Pathname too long"); 2762 return (-1); 2763 } 2764 *cp = '/'; 2765 } 2766 /* -4 is for the .nn<null> we put on the end below */ 2767 (void) snprintf(new, sizeof(new) - 4, "%s", local); 2768 cp = new + strlen(new); 2769 /* 2770 * Don't generate dotfile unless requested explicitly. 2771 * This covers the case when basename gets truncated off 2772 * by buffer size. 2773 */ 2774 if (cp > new && cp[-1] != '/') 2775 *cp++ = '.'; 2776 for (count = 0; count < 100; count++) { 2777 /* At count 0 try unmodified name */ 2778 if (count) 2779 (void)sprintf(cp, "%d", count); 2780 if ((fd = open(count ? new : local, 2781 O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 2782 *name = count ? new : local; 2783 return (fd); 2784 } 2785 } 2786 reply(452, "Unique file name cannot be created."); 2787 return (-1); 2788 } 2789 2790 /* 2791 * Format and send reply containing system error number. 2792 */ 2793 void 2794 perror_reply(int code, char *string) 2795 { 2796 2797 reply(code, "%s: %s.", string, strerror(errno)); 2798 } 2799 2800 static char *onefile[] = { 2801 "", 2802 0 2803 }; 2804 2805 void 2806 send_file_list(char *whichf) 2807 { 2808 struct stat st; 2809 DIR *dirp = NULL; 2810 struct dirent *dir; 2811 FILE *dout = NULL; 2812 char **dirlist, *dirname; 2813 int simple = 0; 2814 int freeglob = 0; 2815 glob_t gl; 2816 2817 if (strpbrk(whichf, "~{[*?") != NULL) { 2818 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 2819 2820 memset(&gl, 0, sizeof(gl)); 2821 gl.gl_matchc = MAXGLOBARGS; 2822 flags |= GLOB_LIMIT; 2823 freeglob = 1; 2824 if (glob(whichf, flags, 0, &gl)) { 2825 reply(550, "not found"); 2826 goto out; 2827 } else if (gl.gl_pathc == 0) { 2828 errno = ENOENT; 2829 perror_reply(550, whichf); 2830 goto out; 2831 } 2832 dirlist = gl.gl_pathv; 2833 } else { 2834 onefile[0] = whichf; 2835 dirlist = onefile; 2836 simple = 1; 2837 } 2838 2839 while ((dirname = *dirlist++)) { 2840 if (stat(dirname, &st) < 0) { 2841 /* 2842 * If user typed "ls -l", etc, and the client 2843 * used NLST, do what the user meant. 2844 */ 2845 if (dirname[0] == '-' && *dirlist == NULL && 2846 transflag == 0) { 2847 retrieve(_PATH_LS " %s", dirname); 2848 goto out; 2849 } 2850 perror_reply(550, whichf); 2851 if (dout != NULL) { 2852 (void) fclose(dout); 2853 transflag = 0; 2854 data = -1; 2855 pdata = -1; 2856 } 2857 goto out; 2858 } 2859 2860 if (S_ISREG(st.st_mode)) { 2861 if (dout == NULL) { 2862 dout = dataconn("file list", (off_t)-1, "w"); 2863 if (dout == NULL) 2864 goto out; 2865 transflag++; 2866 } 2867 fprintf(dout, "%s%s\n", dirname, 2868 type == TYPE_A ? "\r" : ""); 2869 byte_count += strlen(dirname) + 1; 2870 continue; 2871 } else if (!S_ISDIR(st.st_mode)) 2872 continue; 2873 2874 if ((dirp = opendir(dirname)) == NULL) 2875 continue; 2876 2877 while ((dir = readdir(dirp)) != NULL) { 2878 char nbuf[MAXPATHLEN]; 2879 2880 if (recvurg) { 2881 myoob(); 2882 recvurg = 0; 2883 transflag = 0; 2884 goto out; 2885 } 2886 2887 if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2888 continue; 2889 if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2890 dir->d_namlen == 2) 2891 continue; 2892 2893 snprintf(nbuf, sizeof(nbuf), 2894 "%s/%s", dirname, dir->d_name); 2895 2896 /* 2897 * We have to do a stat to insure it's 2898 * not a directory or special file. 2899 */ 2900 if (simple || (stat(nbuf, &st) == 0 && 2901 S_ISREG(st.st_mode))) { 2902 if (dout == NULL) { 2903 dout = dataconn("file list", (off_t)-1, 2904 "w"); 2905 if (dout == NULL) 2906 goto out; 2907 transflag++; 2908 } 2909 if (nbuf[0] == '.' && nbuf[1] == '/') 2910 fprintf(dout, "%s%s\n", &nbuf[2], 2911 type == TYPE_A ? "\r" : ""); 2912 else 2913 fprintf(dout, "%s%s\n", nbuf, 2914 type == TYPE_A ? "\r" : ""); 2915 byte_count += strlen(nbuf) + 1; 2916 } 2917 } 2918 (void) closedir(dirp); 2919 } 2920 2921 if (dout == NULL) 2922 reply(550, "No files found."); 2923 else if (ferror(dout) != 0) 2924 perror_reply(550, "Data connection"); 2925 else 2926 reply(226, "Transfer complete."); 2927 2928 transflag = 0; 2929 if (dout != NULL) 2930 (void) fclose(dout); 2931 data = -1; 2932 pdata = -1; 2933 out: 2934 if (freeglob) { 2935 freeglob = 0; 2936 globfree(&gl); 2937 } 2938 } 2939 2940 void 2941 reapchild(int signo) 2942 { 2943 while (wait3(NULL, WNOHANG, NULL) > 0); 2944 } 2945 2946 #ifdef OLD_SETPROCTITLE 2947 /* 2948 * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2949 * Warning, since this is usually started from inetd.conf, it often doesn't 2950 * have much of an environment or arglist to overwrite. 2951 */ 2952 void 2953 setproctitle(const char *fmt, ...) 2954 { 2955 int i; 2956 va_list ap; 2957 char *p, *bp, ch; 2958 char buf[LINE_MAX]; 2959 2960 va_start(ap, fmt); 2961 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2962 2963 /* make ps print our process name */ 2964 p = Argv[0]; 2965 *p++ = '-'; 2966 2967 i = strlen(buf); 2968 if (i > LastArgv - p - 2) { 2969 i = LastArgv - p - 2; 2970 buf[i] = '\0'; 2971 } 2972 bp = buf; 2973 while (ch = *bp++) 2974 if (ch != '\n' && ch != '\r') 2975 *p++ = ch; 2976 while (p < LastArgv) 2977 *p++ = ' '; 2978 } 2979 #endif /* OLD_SETPROCTITLE */ 2980 2981 static void 2982 logxfer(char *name, off_t size, time_t start) 2983 { 2984 char buf[1024]; 2985 char path[MAXPATHLEN + 1]; 2986 time_t now; 2987 2988 if (statfd >= 0 && getwd(path) != NULL) { 2989 time(&now); 2990 snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n", 2991 ctime(&now)+4, ident, remotehost, 2992 path, name, (long long)size, 2993 (long)(now - start + (now == start))); 2994 write(statfd, buf, strlen(buf)); 2995 } 2996 } 2997 2998 static char * 2999 doublequote(char *s) 3000 { 3001 int n; 3002 char *p, *s2; 3003 3004 for (p = s, n = 0; *p; p++) 3005 if (*p == '"') 3006 n++; 3007 3008 if ((s2 = malloc(p - s + n + 1)) == NULL) 3009 return (NULL); 3010 3011 for (p = s2; *s; s++, p++) { 3012 if ((*p = *s) == '"') 3013 *(++p) = '"'; 3014 } 3015 *p = '\0'; 3016 3017 return (s2); 3018 } 3019