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 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 42 #endif /* not lint */ 43 44 /* 45 * FTP server. 46 */ 47 #include <sys/param.h> 48 #include <sys/stat.h> 49 #include <sys/ioctl.h> 50 #include <sys/socket.h> 51 #include <sys/wait.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 57 #define FTP_NAMES 58 #include <arpa/ftp.h> 59 #include <arpa/inet.h> 60 #include <arpa/telnet.h> 61 62 #include <ctype.h> 63 #include <dirent.h> 64 #include <err.h> 65 #include <errno.h> 66 #include <fcntl.h> 67 #include <glob.h> 68 #include <limits.h> 69 #include <netdb.h> 70 #include <pwd.h> 71 #include <setjmp.h> 72 #include <signal.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <syslog.h> 77 #include <time.h> 78 #include <unistd.h> 79 80 #include "pathnames.h" 81 #include "extern.h" 82 83 #if __STDC__ 84 #include <stdarg.h> 85 #else 86 #include <varargs.h> 87 #endif 88 89 static char version[] = "Version 6.00"; 90 91 extern off_t restart_point; 92 extern char cbuf[]; 93 94 struct sockaddr_in ctrl_addr; 95 struct sockaddr_in data_source; 96 struct sockaddr_in data_dest; 97 struct sockaddr_in his_addr; 98 struct sockaddr_in pasv_addr; 99 100 int data; 101 jmp_buf errcatch, urgcatch; 102 int logged_in; 103 struct passwd *pw; 104 int debug; 105 int timeout = 900; /* timeout after 15 minutes of inactivity */ 106 int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 107 int logging; 108 int guest; 109 int type; 110 int form; 111 int stru; /* avoid C keyword */ 112 int mode; 113 int usedefault = 1; /* for data transfers */ 114 int pdata = -1; /* for passive mode */ 115 sig_atomic_t transflag; 116 off_t file_size; 117 off_t byte_count; 118 #if !defined(CMASK) || CMASK == 0 119 #undef CMASK 120 #define CMASK 027 121 #endif 122 int defumask = CMASK; /* default umask value */ 123 char tmpline[7]; 124 char hostname[MAXHOSTNAMELEN]; 125 char remotehost[MAXHOSTNAMELEN]; 126 127 /* 128 * Timeout intervals for retrying connections 129 * to hosts that don't accept PORT cmds. This 130 * is a kludge, but given the problems with TCP... 131 */ 132 #define SWAITMAX 90 /* wait at most 90 seconds */ 133 #define SWAITINT 5 /* interval between retries */ 134 135 int swaitmax = SWAITMAX; 136 int swaitint = SWAITINT; 137 138 #ifdef SETPROCTITLE 139 char **Argv = NULL; /* pointer to argument vector */ 140 char *LastArgv = NULL; /* end of argv */ 141 char proctitle[LINE_MAX]; /* initial part of title */ 142 #endif /* SETPROCTITLE */ 143 144 #define LOGCMD(cmd, file) \ 145 if (logging > 1) \ 146 syslog(LOG_INFO,"%s %s%s", cmd, \ 147 *(file) == '/' ? "" : curdir(), file); 148 #define LOGCMD2(cmd, file1, file2) \ 149 if (logging > 1) \ 150 syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 151 *(file1) == '/' ? "" : curdir(), file1, \ 152 *(file2) == '/' ? "" : curdir(), file2); 153 #define LOGBYTES(cmd, file, cnt) \ 154 if (logging > 1) { \ 155 if (cnt == (off_t)-1) \ 156 syslog(LOG_INFO,"%s %s%s", cmd, \ 157 *(file) == '/' ? "" : curdir(), file); \ 158 else \ 159 syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 160 cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 161 } 162 163 static void ack __P((char *)); 164 static void myoob __P((int)); 165 static int checkuser __P((char *)); 166 static FILE *dataconn __P((char *, off_t, char *)); 167 static void dolog __P((struct sockaddr_in *)); 168 static char *curdir __P((void)); 169 static void end_login __P((void)); 170 static FILE *getdatasock __P((char *)); 171 static char *gunique __P((char *)); 172 static void lostconn __P((int)); 173 static int receive_data __P((FILE *, FILE *)); 174 static void send_data __P((FILE *, FILE *, off_t)); 175 static struct passwd * 176 sgetpwnam __P((char *)); 177 static char *sgetsave __P((char *)); 178 179 static char * 180 curdir() 181 { 182 static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 183 184 if (getcwd(path, sizeof(path)-2) == NULL) 185 return (""); 186 if (path[1] != '\0') /* special case for root dir. */ 187 strcat(path, "/"); 188 /* For guest account, skip / since it's chrooted */ 189 return (guest ? path+1 : path); 190 } 191 192 int 193 main(argc, argv, envp) 194 int argc; 195 char *argv[]; 196 char **envp; 197 { 198 int addrlen, ch, on = 1, tos; 199 char *cp, line[LINE_MAX]; 200 FILE *fd; 201 202 /* 203 * LOG_NDELAY sets up the logging connection immediately, 204 * necessary for anonymous ftp's that chroot and can't do it later. 205 */ 206 openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 207 addrlen = sizeof(his_addr); 208 if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 209 syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 210 exit(1); 211 } 212 addrlen = sizeof(ctrl_addr); 213 if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 214 syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 215 exit(1); 216 } 217 #ifdef IP_TOS 218 tos = IPTOS_LOWDELAY; 219 if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 220 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 221 #endif 222 data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 223 debug = 0; 224 #ifdef SETPROCTITLE 225 /* 226 * Save start and extent of argv for setproctitle. 227 */ 228 Argv = argv; 229 while (*envp) 230 envp++; 231 LastArgv = envp[-1] + strlen(envp[-1]); 232 #endif /* SETPROCTITLE */ 233 234 while ((ch = getopt(argc, argv, "dlt:T:u:v")) != EOF) { 235 switch (ch) { 236 case 'd': 237 debug = 1; 238 break; 239 240 case 'l': 241 logging++; /* > 1 == extra logging */ 242 break; 243 244 case 't': 245 timeout = atoi(optarg); 246 if (maxtimeout < timeout) 247 maxtimeout = timeout; 248 break; 249 250 case 'T': 251 maxtimeout = atoi(optarg); 252 if (timeout > maxtimeout) 253 timeout = maxtimeout; 254 break; 255 256 case 'u': 257 { 258 long val = 0; 259 260 val = strtol(optarg, &optarg, 8); 261 if (*optarg != '\0' || val < 0) 262 warnx("bad value for -u"); 263 else 264 defumask = val; 265 break; 266 } 267 268 case 'v': 269 debug = 1; 270 break; 271 272 default: 273 warnx("unknown flag -%c ignored", optopt); 274 break; 275 } 276 } 277 (void) freopen(_PATH_DEVNULL, "w", stderr); 278 (void) signal(SIGPIPE, lostconn); 279 (void) signal(SIGCHLD, SIG_IGN); 280 if ((int)signal(SIGURG, myoob) < 0) 281 syslog(LOG_ERR, "signal: %m"); 282 283 /* Try to handle urgent data inline */ 284 #ifdef SO_OOBINLINE 285 if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 286 syslog(LOG_ERR, "setsockopt: %m"); 287 #endif 288 289 #ifdef F_SETOWN 290 if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 291 syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 292 #endif 293 dolog(&his_addr); 294 /* 295 * Set up default state 296 */ 297 data = -1; 298 type = TYPE_A; 299 form = FORM_N; 300 stru = STRU_F; 301 mode = MODE_S; 302 tmpline[0] = '\0'; 303 304 /* If logins are disabled, print out the message. */ 305 if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 306 while (fgets(line, sizeof(line), fd) != NULL) { 307 if ((cp = strchr(line, '\n')) != NULL) 308 *cp = '\0'; 309 lreply(530, "%s", line); 310 } 311 (void) fflush(stdout); 312 (void) fclose(fd); 313 reply(530, "System not available."); 314 exit(0); 315 } 316 if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 317 while (fgets(line, sizeof(line), fd) != NULL) { 318 if ((cp = strchr(line, '\n')) != NULL) 319 *cp = '\0'; 320 lreply(220, "%s", line); 321 } 322 (void) fflush(stdout); 323 (void) fclose(fd); 324 /* reply(220,) must follow */ 325 } 326 (void) gethostname(hostname, sizeof(hostname)); 327 reply(220, "%s FTP server (%s) ready.", hostname, version); 328 (void) setjmp(errcatch); 329 for (;;) 330 (void) yyparse(); 331 /* NOTREACHED */ 332 } 333 334 static void 335 lostconn(signo) 336 int signo; 337 { 338 339 if (debug) 340 syslog(LOG_DEBUG, "lost connection"); 341 dologout(-1); 342 } 343 344 static char ttyline[20]; 345 346 /* 347 * Helper function for sgetpwnam(). 348 */ 349 static char * 350 sgetsave(s) 351 char *s; 352 { 353 char *new = malloc((unsigned) strlen(s) + 1); 354 355 if (new == NULL) { 356 perror_reply(421, "Local resource failure: malloc"); 357 dologout(1); 358 /* NOTREACHED */ 359 } 360 (void) strcpy(new, s); 361 return (new); 362 } 363 364 /* 365 * Save the result of a getpwnam. Used for USER command, since 366 * the data returned must not be clobbered by any other command 367 * (e.g., globbing). 368 */ 369 static struct passwd * 370 sgetpwnam(name) 371 char *name; 372 { 373 static struct passwd save; 374 struct passwd *p; 375 376 if ((p = getpwnam(name)) == NULL) 377 return (p); 378 if (save.pw_name) { 379 free(save.pw_name); 380 free(save.pw_passwd); 381 free(save.pw_gecos); 382 free(save.pw_dir); 383 free(save.pw_shell); 384 } 385 save = *p; 386 save.pw_name = sgetsave(p->pw_name); 387 save.pw_passwd = sgetsave(p->pw_passwd); 388 save.pw_gecos = sgetsave(p->pw_gecos); 389 save.pw_dir = sgetsave(p->pw_dir); 390 save.pw_shell = sgetsave(p->pw_shell); 391 return (&save); 392 } 393 394 static int login_attempts; /* number of failed login attempts */ 395 static int askpasswd; /* had user command, ask for passwd */ 396 static char curname[10]; /* current USER name */ 397 398 /* 399 * USER command. 400 * Sets global passwd pointer pw if named account exists and is acceptable; 401 * sets askpasswd if a PASS command is expected. If logged in previously, 402 * need to reset state. If name is "ftp" or "anonymous", the name is not in 403 * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 404 * If account doesn't exist, ask for passwd anyway. Otherwise, check user 405 * requesting login privileges. Disallow anyone who does not have a standard 406 * shell as returned by getusershell(). Disallow anyone mentioned in the file 407 * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 408 */ 409 void 410 user(name) 411 char *name; 412 { 413 char *cp, *shell; 414 415 if (logged_in) { 416 if (guest) { 417 reply(530, "Can't change user from guest login."); 418 return; 419 } 420 end_login(); 421 } 422 423 guest = 0; 424 if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 425 if (checkuser("ftp") || checkuser("anonymous")) 426 reply(530, "User %s access denied.", name); 427 else if ((pw = sgetpwnam("ftp")) != NULL) { 428 guest = 1; 429 askpasswd = 1; 430 reply(331, 431 "Guest login ok, type your name as password."); 432 } else 433 reply(530, "User %s unknown.", name); 434 if (!askpasswd && logging) 435 syslog(LOG_NOTICE, 436 "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 437 return; 438 } 439 if (pw = sgetpwnam(name)) { 440 if ((shell = pw->pw_shell) == NULL || *shell == 0) 441 shell = _PATH_BSHELL; 442 while ((cp = getusershell()) != NULL) 443 if (strcmp(cp, shell) == 0) 444 break; 445 endusershell(); 446 447 if (cp == NULL || checkuser(name)) { 448 reply(530, "User %s access denied.", name); 449 if (logging) 450 syslog(LOG_NOTICE, 451 "FTP LOGIN REFUSED FROM %s, %s", 452 remotehost, name); 453 pw = (struct passwd *) NULL; 454 return; 455 } 456 } 457 if (logging) 458 strncpy(curname, name, sizeof(curname)-1); 459 reply(331, "Password required for %s.", name); 460 askpasswd = 1; 461 /* 462 * Delay before reading passwd after first failed 463 * attempt to slow down passwd-guessing programs. 464 */ 465 if (login_attempts) 466 sleep((unsigned) login_attempts); 467 } 468 469 /* 470 * Check if a user is in the file _PATH_FTPUSERS 471 */ 472 static int 473 checkuser(name) 474 char *name; 475 { 476 FILE *fd; 477 int found = 0; 478 char *p, line[BUFSIZ]; 479 480 if ((fd = fopen(_PATH_FTPUSERS, "r")) != NULL) { 481 while (fgets(line, sizeof(line), fd) != NULL) 482 if ((p = strchr(line, '\n')) != NULL) { 483 *p = '\0'; 484 if (line[0] == '#') 485 continue; 486 if (strcmp(p, name) == 0) { 487 found = 1; 488 break; 489 } 490 } 491 (void) fclose(fd); 492 } 493 return (found); 494 } 495 496 /* 497 * Terminate login as previous user, if any, resetting state; 498 * used when USER command is given or login fails. 499 */ 500 static void 501 end_login() 502 { 503 504 (void) seteuid((uid_t)0); 505 if (logged_in) 506 logwtmp(ttyline, "", ""); 507 pw = NULL; 508 logged_in = 0; 509 guest = 0; 510 } 511 512 void 513 pass(passwd) 514 char *passwd; 515 { 516 char *salt, *xpasswd; 517 FILE *fd; 518 519 if (logged_in || askpasswd == 0) { 520 reply(503, "Login with USER first."); 521 return; 522 } 523 askpasswd = 0; 524 if (!guest) { /* "ftp" is only account allowed no password */ 525 if (pw == NULL) 526 salt = "xx"; 527 else 528 salt = pw->pw_passwd; 529 xpasswd = crypt(passwd, salt); 530 /* The strcmp does not catch null passwords! */ 531 if (pw == NULL || *pw->pw_passwd == '\0' || 532 strcmp(xpasswd, pw->pw_passwd)) { 533 reply(530, "Login incorrect."); 534 if (logging) 535 syslog(LOG_NOTICE, 536 "FTP LOGIN FAILED FROM %s, %s", 537 remotehost, curname); 538 pw = NULL; 539 if (login_attempts++ >= 5) { 540 syslog(LOG_NOTICE, 541 "repeated login failures from %s", 542 remotehost); 543 exit(0); 544 } 545 return; 546 } 547 } 548 login_attempts = 0; /* this time successful */ 549 if (setegid((gid_t)pw->pw_gid) < 0) { 550 reply(550, "Can't set gid."); 551 return; 552 } 553 (void) initgroups(pw->pw_name, pw->pw_gid); 554 555 /* open wtmp before chroot */ 556 (void)sprintf(ttyline, "ftp%d", getpid()); 557 logwtmp(ttyline, pw->pw_name, remotehost); 558 logged_in = 1; 559 560 if (guest) { 561 /* 562 * We MUST do a chdir() after the chroot. Otherwise 563 * the old current directory will be accessible as "." 564 * outside the new root! 565 */ 566 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 567 reply(550, "Can't set guest privileges."); 568 goto bad; 569 } 570 } else if (chdir(pw->pw_dir) < 0) { 571 if (chdir("/") < 0) { 572 reply(530, "User %s: can't change directory to %s.", 573 pw->pw_name, pw->pw_dir); 574 goto bad; 575 } else 576 lreply(230, "No directory! Logging in with home=/"); 577 } 578 if (seteuid((uid_t)pw->pw_uid) < 0) { 579 reply(550, "Can't set uid."); 580 goto bad; 581 } 582 /* 583 * Display a login message, if it exists. 584 * N.B. reply(230,) must follow the message. 585 */ 586 if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 587 char *cp, line[LINE_MAX]; 588 589 while (fgets(line, sizeof(line), fd) != NULL) { 590 if ((cp = strchr(line, '\n')) != NULL) 591 *cp = '\0'; 592 lreply(230, "%s", line); 593 } 594 (void) fflush(stdout); 595 (void) fclose(fd); 596 } 597 if (guest) { 598 reply(230, "Guest login ok, access restrictions apply."); 599 #ifdef SETPROCTITLE 600 snprintf(proctitle, sizeof(proctitle), 601 "%s: anonymous/%.*s", remotehost, 602 sizeof(proctitle) - sizeof(remotehost) - 603 sizeof(": anonymous/"), passwd); 604 setproctitle(proctitle); 605 #endif /* SETPROCTITLE */ 606 if (logging) 607 syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 608 remotehost, passwd); 609 } else { 610 reply(230, "User %s logged in.", pw->pw_name); 611 #ifdef SETPROCTITLE 612 snprintf(proctitle, sizeof(proctitle), 613 "%s: %s", remotehost, pw->pw_name); 614 setproctitle(proctitle); 615 #endif /* SETPROCTITLE */ 616 if (logging) 617 syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 618 remotehost, pw->pw_name); 619 } 620 (void) umask(defumask); 621 return; 622 bad: 623 /* Forget all about it... */ 624 end_login(); 625 } 626 627 void 628 retrieve(cmd, name) 629 char *cmd, *name; 630 { 631 FILE *fin, *dout; 632 struct stat st; 633 int (*closefunc) __P((FILE *)); 634 635 if (cmd == 0) { 636 fin = fopen(name, "r"), closefunc = fclose; 637 st.st_size = 0; 638 } else { 639 char line[BUFSIZ]; 640 641 (void) sprintf(line, cmd, name), name = line; 642 fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 643 st.st_size = -1; 644 st.st_blksize = BUFSIZ; 645 } 646 if (fin == NULL) { 647 if (errno != 0) { 648 perror_reply(550, name); 649 if (cmd == 0) { 650 LOGCMD("get", name); 651 } 652 } 653 return; 654 } 655 byte_count = -1; 656 if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 657 reply(550, "%s: not a plain file.", name); 658 goto done; 659 } 660 if (restart_point) { 661 if (type == TYPE_A) { 662 off_t i, n; 663 int c; 664 665 n = restart_point; 666 i = 0; 667 while (i++ < n) { 668 if ((c=getc(fin)) == EOF) { 669 perror_reply(550, name); 670 goto done; 671 } 672 if (c == '\n') 673 i++; 674 } 675 } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 676 perror_reply(550, name); 677 goto done; 678 } 679 } 680 dout = dataconn(name, st.st_size, "w"); 681 if (dout == NULL) 682 goto done; 683 send_data(fin, dout, st.st_blksize); 684 (void) fclose(dout); 685 data = -1; 686 pdata = -1; 687 done: 688 if (cmd == 0) 689 LOGBYTES("get", name, byte_count); 690 (*closefunc)(fin); 691 } 692 693 void 694 store(name, mode, unique) 695 char *name, *mode; 696 int unique; 697 { 698 FILE *fout, *din; 699 struct stat st; 700 int (*closefunc) __P((FILE *)); 701 702 if (unique && stat(name, &st) == 0 && 703 (name = gunique(name)) == NULL) { 704 LOGCMD(*mode == 'w' ? "put" : "append", name); 705 return; 706 } 707 708 if (restart_point) 709 mode = "r+"; 710 fout = fopen(name, mode); 711 closefunc = fclose; 712 if (fout == NULL) { 713 perror_reply(553, name); 714 LOGCMD(*mode == 'w' ? "put" : "append", name); 715 return; 716 } 717 byte_count = -1; 718 if (restart_point) { 719 if (type == TYPE_A) { 720 off_t i, n; 721 int c; 722 723 n = restart_point; 724 i = 0; 725 while (i++ < n) { 726 if ((c=getc(fout)) == EOF) { 727 perror_reply(550, name); 728 goto done; 729 } 730 if (c == '\n') 731 i++; 732 } 733 /* 734 * We must do this seek to "current" position 735 * because we are changing from reading to 736 * writing. 737 */ 738 if (fseek(fout, 0L, L_INCR) < 0) { 739 perror_reply(550, name); 740 goto done; 741 } 742 } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 743 perror_reply(550, name); 744 goto done; 745 } 746 } 747 din = dataconn(name, (off_t)-1, "r"); 748 if (din == NULL) 749 goto done; 750 if (receive_data(din, fout) == 0) { 751 if (unique) 752 reply(226, "Transfer complete (unique file name:%s).", 753 name); 754 else 755 reply(226, "Transfer complete."); 756 } 757 (void) fclose(din); 758 data = -1; 759 pdata = -1; 760 done: 761 LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 762 (*closefunc)(fout); 763 } 764 765 static FILE * 766 getdatasock(mode) 767 char *mode; 768 { 769 int on = 1, s, t, tries; 770 771 if (data >= 0) 772 return (fdopen(data, mode)); 773 (void) seteuid((uid_t)0); 774 s = socket(AF_INET, SOCK_STREAM, 0); 775 if (s < 0) 776 goto bad; 777 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 778 (char *) &on, sizeof(on)) < 0) 779 goto bad; 780 /* anchor socket to avoid multi-homing problems */ 781 data_source.sin_family = AF_INET; 782 data_source.sin_addr = ctrl_addr.sin_addr; 783 for (tries = 1; ; tries++) { 784 if (bind(s, (struct sockaddr *)&data_source, 785 sizeof(data_source)) >= 0) 786 break; 787 if (errno != EADDRINUSE || tries > 10) 788 goto bad; 789 sleep(tries); 790 } 791 (void) seteuid((uid_t)pw->pw_uid); 792 #ifdef IP_TOS 793 on = IPTOS_THROUGHPUT; 794 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 795 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 796 #endif 797 return (fdopen(s, mode)); 798 bad: 799 /* Return the real value of errno (close may change it) */ 800 t = errno; 801 (void) seteuid((uid_t)pw->pw_uid); 802 (void) close(s); 803 errno = t; 804 return (NULL); 805 } 806 807 static FILE * 808 dataconn(name, size, mode) 809 char *name; 810 off_t size; 811 char *mode; 812 { 813 char sizebuf[32]; 814 FILE *file; 815 int retry = 0, tos; 816 817 file_size = size; 818 byte_count = 0; 819 if (size != (off_t) -1) 820 (void) sprintf(sizebuf, " (%qd bytes)", size); 821 else 822 (void) strcpy(sizebuf, ""); 823 if (pdata >= 0) { 824 struct sockaddr_in from; 825 int s, fromlen = sizeof(from); 826 827 s = accept(pdata, (struct sockaddr *)&from, &fromlen); 828 if (s < 0) { 829 reply(425, "Can't open data connection."); 830 (void) close(pdata); 831 pdata = -1; 832 return (NULL); 833 } 834 (void) close(pdata); 835 pdata = s; 836 #ifdef IP_TOS 837 tos = IPTOS_LOWDELAY; 838 (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 839 sizeof(int)); 840 #endif 841 reply(150, "Opening %s mode data connection for '%s'%s.", 842 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 843 return (fdopen(pdata, mode)); 844 } 845 if (data >= 0) { 846 reply(125, "Using existing data connection for '%s'%s.", 847 name, sizebuf); 848 usedefault = 1; 849 return (fdopen(data, mode)); 850 } 851 if (usedefault) 852 data_dest = his_addr; 853 usedefault = 1; 854 file = getdatasock(mode); 855 if (file == NULL) { 856 reply(425, "Can't create data socket (%s,%d): %s.", 857 inet_ntoa(data_source.sin_addr), 858 ntohs(data_source.sin_port), strerror(errno)); 859 return (NULL); 860 } 861 data = fileno(file); 862 while (connect(data, (struct sockaddr *)&data_dest, 863 sizeof(data_dest)) < 0) { 864 if (errno == EADDRINUSE && retry < swaitmax) { 865 sleep((unsigned) swaitint); 866 retry += swaitint; 867 continue; 868 } 869 perror_reply(425, "Can't build data connection"); 870 (void) fclose(file); 871 data = -1; 872 return (NULL); 873 } 874 reply(150, "Opening %s mode data connection for '%s'%s.", 875 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 876 return (file); 877 } 878 879 /* 880 * Tranfer the contents of "instr" to "outstr" peer using the appropriate 881 * encapsulation of the data subject * to Mode, Structure, and Type. 882 * 883 * NB: Form isn't handled. 884 */ 885 static void 886 send_data(instr, outstr, blksize) 887 FILE *instr, *outstr; 888 off_t blksize; 889 { 890 int c, cnt, filefd, netfd; 891 char *buf; 892 893 transflag++; 894 if (setjmp(urgcatch)) { 895 transflag = 0; 896 return; 897 } 898 switch (type) { 899 900 case TYPE_A: 901 while ((c = getc(instr)) != EOF) { 902 byte_count++; 903 if (c == '\n') { 904 if (ferror(outstr)) 905 goto data_err; 906 (void) putc('\r', outstr); 907 } 908 (void) putc(c, outstr); 909 } 910 fflush(outstr); 911 transflag = 0; 912 if (ferror(instr)) 913 goto file_err; 914 if (ferror(outstr)) 915 goto data_err; 916 reply(226, "Transfer complete."); 917 return; 918 919 case TYPE_I: 920 case TYPE_L: 921 if ((buf = malloc((u_int)blksize)) == NULL) { 922 transflag = 0; 923 perror_reply(451, "Local resource failure: malloc"); 924 return; 925 } 926 netfd = fileno(outstr); 927 filefd = fileno(instr); 928 while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 929 write(netfd, buf, cnt) == cnt) 930 byte_count += cnt; 931 transflag = 0; 932 (void)free(buf); 933 if (cnt != 0) { 934 if (cnt < 0) 935 goto file_err; 936 goto data_err; 937 } 938 reply(226, "Transfer complete."); 939 return; 940 default: 941 transflag = 0; 942 reply(550, "Unimplemented TYPE %d in send_data", type); 943 return; 944 } 945 946 data_err: 947 transflag = 0; 948 perror_reply(426, "Data connection"); 949 return; 950 951 file_err: 952 transflag = 0; 953 perror_reply(551, "Error on input file"); 954 } 955 956 /* 957 * Transfer data from peer to "outstr" using the appropriate encapulation of 958 * the data subject to Mode, Structure, and Type. 959 * 960 * N.B.: Form isn't handled. 961 */ 962 static int 963 receive_data(instr, outstr) 964 FILE *instr, *outstr; 965 { 966 int c; 967 int cnt, bare_lfs = 0; 968 char buf[BUFSIZ]; 969 970 transflag++; 971 if (setjmp(urgcatch)) { 972 transflag = 0; 973 return (-1); 974 } 975 switch (type) { 976 977 case TYPE_I: 978 case TYPE_L: 979 while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 980 if (write(fileno(outstr), buf, cnt) != cnt) 981 goto file_err; 982 byte_count += cnt; 983 } 984 if (cnt < 0) 985 goto data_err; 986 transflag = 0; 987 return (0); 988 989 case TYPE_E: 990 reply(553, "TYPE E not implemented."); 991 transflag = 0; 992 return (-1); 993 994 case TYPE_A: 995 while ((c = getc(instr)) != EOF) { 996 byte_count++; 997 if (c == '\n') 998 bare_lfs++; 999 while (c == '\r') { 1000 if (ferror(outstr)) 1001 goto data_err; 1002 if ((c = getc(instr)) != '\n') { 1003 (void) putc ('\r', outstr); 1004 if (c == '\0' || c == EOF) 1005 goto contin2; 1006 } 1007 } 1008 (void) putc(c, outstr); 1009 contin2: ; 1010 } 1011 fflush(outstr); 1012 if (ferror(instr)) 1013 goto data_err; 1014 if (ferror(outstr)) 1015 goto file_err; 1016 transflag = 0; 1017 if (bare_lfs) { 1018 lreply(226, 1019 "WARNING! %d bare linefeeds received in ASCII mode", 1020 bare_lfs); 1021 (void)printf(" File may not have transferred correctly.\r\n"); 1022 } 1023 return (0); 1024 default: 1025 reply(550, "Unimplemented TYPE %d in receive_data", type); 1026 transflag = 0; 1027 return (-1); 1028 } 1029 1030 data_err: 1031 transflag = 0; 1032 perror_reply(426, "Data Connection"); 1033 return (-1); 1034 1035 file_err: 1036 transflag = 0; 1037 perror_reply(452, "Error writing file"); 1038 return (-1); 1039 } 1040 1041 void 1042 statfilecmd(filename) 1043 char *filename; 1044 { 1045 FILE *fin; 1046 int c; 1047 char line[LINE_MAX]; 1048 1049 (void)snprintf(line, sizeof(line), "/bin/ls -lgA %s", filename); 1050 fin = ftpd_popen(line, "r"); 1051 lreply(211, "status of %s:", filename); 1052 while ((c = getc(fin)) != EOF) { 1053 if (c == '\n') { 1054 if (ferror(stdout)){ 1055 perror_reply(421, "control connection"); 1056 (void) ftpd_pclose(fin); 1057 dologout(1); 1058 /* NOTREACHED */ 1059 } 1060 if (ferror(fin)) { 1061 perror_reply(551, filename); 1062 (void) ftpd_pclose(fin); 1063 return; 1064 } 1065 (void) putc('\r', stdout); 1066 } 1067 (void) putc(c, stdout); 1068 } 1069 (void) ftpd_pclose(fin); 1070 reply(211, "End of Status"); 1071 } 1072 1073 void 1074 statcmd() 1075 { 1076 struct sockaddr_in *sin; 1077 u_char *a, *p; 1078 1079 lreply(211, "%s FTP server status:", hostname, version); 1080 printf(" %s\r\n", version); 1081 printf(" Connected to %s", remotehost); 1082 if (!isdigit(remotehost[0])) 1083 printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 1084 printf("\r\n"); 1085 if (logged_in) { 1086 if (guest) 1087 printf(" Logged in anonymously\r\n"); 1088 else 1089 printf(" Logged in as %s\r\n", pw->pw_name); 1090 } else if (askpasswd) 1091 printf(" Waiting for password\r\n"); 1092 else 1093 printf(" Waiting for user name\r\n"); 1094 printf(" TYPE: %s", typenames[type]); 1095 if (type == TYPE_A || type == TYPE_E) 1096 printf(", FORM: %s", formnames[form]); 1097 if (type == TYPE_L) 1098 #if NBBY == 8 1099 printf(" %d", NBBY); 1100 #else 1101 printf(" %d", bytesize); /* need definition! */ 1102 #endif 1103 printf("; STRUcture: %s; transfer MODE: %s\r\n", 1104 strunames[stru], modenames[mode]); 1105 if (data != -1) 1106 printf(" Data connection open\r\n"); 1107 else if (pdata != -1) { 1108 printf(" in Passive mode"); 1109 sin = &pasv_addr; 1110 goto printaddr; 1111 } else if (usedefault == 0) { 1112 printf(" PORT"); 1113 sin = &data_dest; 1114 printaddr: 1115 a = (u_char *) &sin->sin_addr; 1116 p = (u_char *) &sin->sin_port; 1117 #define UC(b) (((int) b) & 0xff) 1118 printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 1119 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1120 #undef UC 1121 } else 1122 printf(" No data connection\r\n"); 1123 reply(211, "End of status"); 1124 } 1125 1126 void 1127 fatal(s) 1128 char *s; 1129 { 1130 1131 reply(451, "Error in server: %s\n", s); 1132 reply(221, "Closing connection due to server error."); 1133 dologout(0); 1134 /* NOTREACHED */ 1135 } 1136 1137 void 1138 #if __STDC__ 1139 reply(int n, const char *fmt, ...) 1140 #else 1141 reply(n, fmt, va_alist) 1142 int n; 1143 char *fmt; 1144 va_dcl 1145 #endif 1146 { 1147 va_list ap; 1148 #if __STDC__ 1149 va_start(ap, fmt); 1150 #else 1151 va_start(ap); 1152 #endif 1153 (void)printf("%d ", n); 1154 (void)vprintf(fmt, ap); 1155 (void)printf("\r\n"); 1156 (void)fflush(stdout); 1157 if (debug) { 1158 syslog(LOG_DEBUG, "<--- %d ", n); 1159 vsyslog(LOG_DEBUG, fmt, ap); 1160 } 1161 } 1162 1163 void 1164 #if __STDC__ 1165 lreply(int n, const char *fmt, ...) 1166 #else 1167 lreply(n, fmt, va_alist) 1168 int n; 1169 char *fmt; 1170 va_dcl 1171 #endif 1172 { 1173 va_list ap; 1174 #if __STDC__ 1175 va_start(ap, fmt); 1176 #else 1177 va_start(ap); 1178 #endif 1179 (void)printf("%d- ", n); 1180 (void)vprintf(fmt, ap); 1181 (void)printf("\r\n"); 1182 (void)fflush(stdout); 1183 if (debug) { 1184 syslog(LOG_DEBUG, "<--- %d- ", n); 1185 vsyslog(LOG_DEBUG, fmt, ap); 1186 } 1187 } 1188 1189 static void 1190 ack(s) 1191 char *s; 1192 { 1193 1194 reply(250, "%s command successful.", s); 1195 } 1196 1197 void 1198 nack(s) 1199 char *s; 1200 { 1201 1202 reply(502, "%s command not implemented.", s); 1203 } 1204 1205 /* ARGSUSED */ 1206 void 1207 yyerror(s) 1208 char *s; 1209 { 1210 char *cp; 1211 1212 if (cp = strchr(cbuf,'\n')) 1213 *cp = '\0'; 1214 reply(500, "'%s': command not understood.", cbuf); 1215 } 1216 1217 void 1218 delete(name) 1219 char *name; 1220 { 1221 struct stat st; 1222 1223 LOGCMD("delete", name); 1224 if (stat(name, &st) < 0) { 1225 perror_reply(550, name); 1226 return; 1227 } 1228 if ((st.st_mode&S_IFMT) == S_IFDIR) { 1229 if (rmdir(name) < 0) { 1230 perror_reply(550, name); 1231 return; 1232 } 1233 goto done; 1234 } 1235 if (unlink(name) < 0) { 1236 perror_reply(550, name); 1237 return; 1238 } 1239 done: 1240 ack("DELE"); 1241 } 1242 1243 void 1244 cwd(path) 1245 char *path; 1246 { 1247 1248 if (chdir(path) < 0) 1249 perror_reply(550, path); 1250 else 1251 ack("CWD"); 1252 } 1253 1254 void 1255 makedir(name) 1256 char *name; 1257 { 1258 1259 LOGCMD("mkdir", name); 1260 if (mkdir(name, 0777) < 0) 1261 perror_reply(550, name); 1262 else 1263 reply(257, "MKD command successful."); 1264 } 1265 1266 void 1267 removedir(name) 1268 char *name; 1269 { 1270 1271 LOGCMD("rmdir", name); 1272 if (rmdir(name) < 0) 1273 perror_reply(550, name); 1274 else 1275 ack("RMD"); 1276 } 1277 1278 void 1279 pwd() 1280 { 1281 char path[MAXPATHLEN + 1]; 1282 1283 if (getwd(path) == (char *)NULL) 1284 reply(550, "%s.", path); 1285 else 1286 reply(257, "\"%s\" is current directory.", path); 1287 } 1288 1289 char * 1290 renamefrom(name) 1291 char *name; 1292 { 1293 struct stat st; 1294 1295 if (stat(name, &st) < 0) { 1296 perror_reply(550, name); 1297 return ((char *)0); 1298 } 1299 reply(350, "File exists, ready for destination name"); 1300 return (name); 1301 } 1302 1303 void 1304 renamecmd(from, to) 1305 char *from, *to; 1306 { 1307 1308 LOGCMD2("rename", from, to); 1309 if (rename(from, to) < 0) 1310 perror_reply(550, "rename"); 1311 else 1312 ack("RNTO"); 1313 } 1314 1315 static void 1316 dolog(sin) 1317 struct sockaddr_in *sin; 1318 { 1319 struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 1320 sizeof(struct in_addr), AF_INET); 1321 1322 if (hp) 1323 (void) strncpy(remotehost, hp->h_name, sizeof(remotehost)); 1324 else 1325 (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 1326 sizeof(remotehost)); 1327 #ifdef SETPROCTITLE 1328 snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost); 1329 setproctitle(proctitle); 1330 #endif /* SETPROCTITLE */ 1331 1332 if (logging) 1333 syslog(LOG_INFO, "connection from %s", remotehost); 1334 } 1335 1336 /* 1337 * Record logout in wtmp file 1338 * and exit with supplied status. 1339 */ 1340 void 1341 dologout(status) 1342 int status; 1343 { 1344 1345 if (logged_in) { 1346 (void) seteuid((uid_t)0); 1347 logwtmp(ttyline, "", ""); 1348 } 1349 /* beware of flushing buffers after a SIGPIPE */ 1350 _exit(status); 1351 } 1352 1353 static void 1354 myoob(signo) 1355 int signo; 1356 { 1357 char *cp; 1358 1359 /* only process if transfer occurring */ 1360 if (!transflag) 1361 return; 1362 cp = tmpline; 1363 if (getline(cp, 7, stdin) == NULL) { 1364 reply(221, "You could at least say goodbye."); 1365 dologout(0); 1366 } 1367 upper(cp); 1368 if (strcmp(cp, "ABOR\r\n") == 0) { 1369 tmpline[0] = '\0'; 1370 reply(426, "Transfer aborted. Data connection closed."); 1371 reply(226, "Abort successful"); 1372 longjmp(urgcatch, 1); 1373 } 1374 if (strcmp(cp, "STAT\r\n") == 0) { 1375 if (file_size != (off_t) -1) 1376 reply(213, "Status: %qd of %qd bytes transferred", 1377 byte_count, file_size); 1378 else 1379 reply(213, "Status: %qd bytes transferred", byte_count); 1380 } 1381 } 1382 1383 /* 1384 * Note: a response of 425 is not mentioned as a possible response to 1385 * the PASV command in RFC959. However, it has been blessed as 1386 * a legitimate response by Jon Postel in a telephone conversation 1387 * with Rick Adams on 25 Jan 89. 1388 */ 1389 void 1390 passive() 1391 { 1392 int len; 1393 char *p, *a; 1394 1395 pdata = socket(AF_INET, SOCK_STREAM, 0); 1396 if (pdata < 0) { 1397 perror_reply(425, "Can't open passive connection"); 1398 return; 1399 } 1400 pasv_addr = ctrl_addr; 1401 pasv_addr.sin_port = 0; 1402 (void) seteuid((uid_t)0); 1403 if (bind(pdata, (struct sockaddr *)&pasv_addr, sizeof(pasv_addr)) < 0) { 1404 (void) seteuid((uid_t)pw->pw_uid); 1405 goto pasv_error; 1406 } 1407 (void) seteuid((uid_t)pw->pw_uid); 1408 len = sizeof(pasv_addr); 1409 if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 1410 goto pasv_error; 1411 if (listen(pdata, 1) < 0) 1412 goto pasv_error; 1413 a = (char *) &pasv_addr.sin_addr; 1414 p = (char *) &pasv_addr.sin_port; 1415 1416 #define UC(b) (((int) b) & 0xff) 1417 1418 reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 1419 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1420 return; 1421 1422 pasv_error: 1423 (void) close(pdata); 1424 pdata = -1; 1425 perror_reply(425, "Can't open passive connection"); 1426 return; 1427 } 1428 1429 /* 1430 * Generate unique name for file with basename "local". 1431 * The file named "local" is already known to exist. 1432 * Generates failure reply on error. 1433 */ 1434 static char * 1435 gunique(local) 1436 char *local; 1437 { 1438 static char new[MAXPATHLEN]; 1439 struct stat st; 1440 int count; 1441 char *cp; 1442 1443 cp = strrchr(local, '/'); 1444 if (cp) 1445 *cp = '\0'; 1446 if (stat(cp ? local : ".", &st) < 0) { 1447 perror_reply(553, cp ? local : "."); 1448 return ((char *) 0); 1449 } 1450 if (cp) 1451 *cp = '/'; 1452 (void) strcpy(new, local); 1453 cp = new + strlen(new); 1454 *cp++ = '.'; 1455 for (count = 1; count < 100; count++) { 1456 (void)sprintf(cp, "%d", count); 1457 if (stat(new, &st) < 0) 1458 return (new); 1459 } 1460 reply(452, "Unique file name cannot be created."); 1461 return (NULL); 1462 } 1463 1464 /* 1465 * Format and send reply containing system error number. 1466 */ 1467 void 1468 perror_reply(code, string) 1469 int code; 1470 char *string; 1471 { 1472 1473 reply(code, "%s: %s.", string, strerror(errno)); 1474 } 1475 1476 static char *onefile[] = { 1477 "", 1478 0 1479 }; 1480 1481 void 1482 send_file_list(whichf) 1483 char *whichf; 1484 { 1485 struct stat st; 1486 DIR *dirp = NULL; 1487 struct dirent *dir; 1488 FILE *dout = NULL; 1489 char **dirlist, *dirname; 1490 int simple = 0; 1491 int freeglob = 0; 1492 glob_t gl; 1493 1494 if (strpbrk(whichf, "~{[*?") != NULL) { 1495 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 1496 1497 memset(&gl, 0, sizeof(gl)); 1498 freeglob = 1; 1499 if (glob(whichf, flags, 0, &gl)) { 1500 reply(550, "not found"); 1501 goto out; 1502 } else if (gl.gl_pathc == 0) { 1503 errno = ENOENT; 1504 perror_reply(550, whichf); 1505 goto out; 1506 } 1507 dirlist = gl.gl_pathv; 1508 } else { 1509 onefile[0] = whichf; 1510 dirlist = onefile; 1511 simple = 1; 1512 } 1513 1514 if (setjmp(urgcatch)) { 1515 transflag = 0; 1516 goto out; 1517 } 1518 while (dirname = *dirlist++) { 1519 if (stat(dirname, &st) < 0) { 1520 /* 1521 * If user typed "ls -l", etc, and the client 1522 * used NLST, do what the user meant. 1523 */ 1524 if (dirname[0] == '-' && *dirlist == NULL && 1525 transflag == 0) { 1526 retrieve("/bin/ls %s", dirname); 1527 goto out; 1528 } 1529 perror_reply(550, whichf); 1530 if (dout != NULL) { 1531 (void) fclose(dout); 1532 transflag = 0; 1533 data = -1; 1534 pdata = -1; 1535 } 1536 goto out; 1537 } 1538 1539 if (S_ISREG(st.st_mode)) { 1540 if (dout == NULL) { 1541 dout = dataconn("file list", (off_t)-1, "w"); 1542 if (dout == NULL) 1543 goto out; 1544 transflag++; 1545 } 1546 fprintf(dout, "%s%s\n", dirname, 1547 type == TYPE_A ? "\r" : ""); 1548 byte_count += strlen(dirname) + 1; 1549 continue; 1550 } else if (!S_ISDIR(st.st_mode)) 1551 continue; 1552 1553 if ((dirp = opendir(dirname)) == NULL) 1554 continue; 1555 1556 while ((dir = readdir(dirp)) != NULL) { 1557 char nbuf[MAXPATHLEN]; 1558 1559 if (dir->d_name[0] == '.' && dir->d_namlen == 1) 1560 continue; 1561 if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 1562 dir->d_namlen == 2) 1563 continue; 1564 1565 sprintf(nbuf, "%s/%s", dirname, dir->d_name); 1566 1567 /* 1568 * We have to do a stat to insure it's 1569 * not a directory or special file. 1570 */ 1571 if (simple || (stat(nbuf, &st) == 0 && 1572 S_ISREG(st.st_mode))) { 1573 if (dout == NULL) { 1574 dout = dataconn("file list", (off_t)-1, 1575 "w"); 1576 if (dout == NULL) 1577 goto out; 1578 transflag++; 1579 } 1580 if (nbuf[0] == '.' && nbuf[1] == '/') 1581 fprintf(dout, "%s%s\n", &nbuf[2], 1582 type == TYPE_A ? "\r" : ""); 1583 else 1584 fprintf(dout, "%s%s\n", nbuf, 1585 type == TYPE_A ? "\r" : ""); 1586 byte_count += strlen(nbuf) + 1; 1587 } 1588 } 1589 (void) closedir(dirp); 1590 } 1591 1592 if (dout == NULL) 1593 reply(550, "No files found."); 1594 else if (ferror(dout) != 0) 1595 perror_reply(550, "Data connection"); 1596 else 1597 reply(226, "Transfer complete."); 1598 1599 transflag = 0; 1600 if (dout != NULL) 1601 (void) fclose(dout); 1602 data = -1; 1603 pdata = -1; 1604 out: 1605 if (freeglob) { 1606 freeglob = 0; 1607 globfree(&gl); 1608 } 1609 } 1610 1611 #ifdef SETPROCTITLE 1612 /* 1613 * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 1614 * Warning, since this is usually started from inetd.conf, it often doesn't 1615 * have much of an environment or arglist to overwrite. 1616 */ 1617 void 1618 #if __STDC__ 1619 setproctitle(const char *fmt, ...) 1620 #else 1621 setproctitle(fmt, va_alist) 1622 char *fmt; 1623 va_dcl 1624 #endif 1625 { 1626 int i; 1627 va_list ap; 1628 char *p, *bp, ch; 1629 char buf[LINE_MAX]; 1630 1631 #if __STDC__ 1632 va_start(ap, fmt); 1633 #else 1634 va_start(ap); 1635 #endif 1636 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 1637 1638 /* make ps print our process name */ 1639 p = Argv[0]; 1640 *p++ = '-'; 1641 1642 i = strlen(buf); 1643 if (i > LastArgv - p - 2) { 1644 i = LastArgv - p - 2; 1645 buf[i] = '\0'; 1646 } 1647 bp = buf; 1648 while (ch = *bp++) 1649 if (ch != '\n' && ch != '\r') 1650 *p++ = ch; 1651 while (p < LastArgv) 1652 *p++ = ' '; 1653 } 1654 #endif /* SETPROCTITLE */ 1655