ftpd.c revision 50476
121308Sache/* 221308Sache * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 321308Sache * The Regents of the University of California. All rights reserved. 421308Sache * 521308Sache * Redistribution and use in source and binary forms, with or without 621308Sache * modification, are permitted provided that the following conditions 721308Sache * are met: 821308Sache * 1. Redistributions of source code must retain the above copyright 921308Sache * notice, this list of conditions and the following disclaimer. 1058310Sache * 2. Redistributions in binary form must reproduce the above copyright 1121308Sache * notice, this list of conditions and the following disclaimer in the 1221308Sache * documentation and/or other materials provided with the distribution. 1321308Sache * 3. All advertising materials mentioning features or use of this software 1421308Sache * must display the following acknowledgement: 1521308Sache * This product includes software developed by the University of 1621308Sache * California, Berkeley and its contributors. 1721308Sache * 4. Neither the name of the University nor the names of its contributors 1821308Sache * may be used to endorse or promote products derived from this software 1921308Sache * without specific prior written permission. 2021308Sache * 2158310Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2221308Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2321308Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2421308Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2521308Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2621308Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2721308Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2821308Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2921308Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3021308Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3121308Sache * SUCH DAMAGE. 3221308Sache */ 3321308Sache 3421308Sache#if 0 3521308Sache#ifndef lint 3621308Sachestatic char copyright[] = 3721308Sache"@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 3821308Sache The Regents of the University of California. All rights reserved.\n"; 3921308Sache#endif /* not lint */ 4021308Sache#endif 4121308Sache 4221308Sache#ifndef lint 4321308Sache#if 0 4421308Sachestatic char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 4521308Sache#endif 4621308Sachestatic const char rcsid[] = 4721308Sache "$FreeBSD: head/libexec/ftpd/ftpd.c 50476 1999-08-28 00:22:10Z peter $"; 4821308Sache#endif /* not lint */ 4958310Sache 5058310Sache/* 5121308Sache * FTP server. 5221308Sache */ 5321308Sache#include <sys/param.h> 5421308Sache#include <sys/stat.h> 5521308Sache#include <sys/ioctl.h> 5621308Sache#include <sys/socket.h> 5721308Sache#include <sys/wait.h> 5821308Sache#include <sys/mman.h> 5921308Sache 6021308Sache#include <netinet/in.h> 6121308Sache#include <netinet/in_systm.h> 6221308Sache#include <netinet/ip.h> 6321308Sache#include <netinet/tcp.h> 6421308Sache 6521308Sache#define FTP_NAMES 6621308Sache#include <arpa/ftp.h> 6721308Sache#include <arpa/inet.h> 6821308Sache#include <arpa/telnet.h> 6921308Sache 7021308Sache#include <ctype.h> 7121308Sache#include <dirent.h> 7221308Sache#include <err.h> 73119610Sache#include <errno.h> 74119610Sache#include <fcntl.h> 75119610Sache#include <glob.h> 76119610Sache#include <limits.h> 77119610Sache#include <netdb.h> 7821308Sache#include <pwd.h> 7921308Sache#include <grp.h> 8021308Sache#include <setjmp.h> 8121308Sache#include <signal.h> 8221308Sache#include <stdio.h> 8321308Sache#include <stdlib.h> 8421308Sache#include <string.h> 8521308Sache#include <syslog.h> 8621308Sache#include <time.h> 8721308Sache#include <unistd.h> 8821308Sache#include <libutil.h> 8921308Sache#ifdef LOGIN_CAP 9021308Sache#include <login_cap.h> 9121308Sache#endif 9221308Sache 9321308Sache#ifdef SKEY 9421308Sache#include <skey.h> 9521308Sache#endif 9621308Sache 9721308Sache#include "pathnames.h" 9821308Sache#include "extern.h" 9921308Sache 10021308Sache#if __STDC__ 10121308Sache#include <stdarg.h> 10221308Sache#else 10321308Sache#include <varargs.h> 10421308Sache#endif 10521308Sache 10621308Sachestatic char version[] = "Version 6.00LS"; 10721308Sache#undef main 10821308Sache 10921308Sacheextern off_t restart_point; 11021308Sacheextern char cbuf[]; 11121308Sache 11221308Sachestruct sockaddr_in server_addr; 11321308Sachestruct sockaddr_in ctrl_addr; 11421308Sachestruct sockaddr_in data_source; 11521308Sachestruct sockaddr_in data_dest; 11621308Sachestruct sockaddr_in his_addr; 11721308Sachestruct sockaddr_in pasv_addr; 11821308Sache 11921308Sacheint daemon_mode; 12021308Sacheint data; 12121308Sachejmp_buf errcatch, urgcatch; 12221308Sacheint logged_in; 12321308Sachestruct passwd *pw; 12421308Sacheint debug; 12521308Sacheint timeout = 900; /* timeout after 15 minutes of inactivity */ 12621308Sacheint maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 12721308Sacheint logging; 12821308Sacheint restricted_data_ports = 1; 12921308Sacheint paranoid = 1; /* be extra careful about security */ 13021308Sacheint anon_only = 0; /* Only anonymous ftp allowed */ 13121308Sacheint guest; 13221308Sacheint dochroot; 13321308Sacheint stats; 13421308Sacheint statfd = -1; 13521308Sacheint type; 13621308Sacheint form; 137119610Sacheint stru; /* avoid C keyword */ 13821308Sacheint mode; 13921308Sacheint usedefault = 1; /* for data transfers */ 14021308Sacheint pdata = -1; /* for passive mode */ 14121308Sachesig_atomic_t transflag; 14221308Sacheoff_t file_size; 14321308Sacheoff_t byte_count; 14421308Sache#if !defined(CMASK) || CMASK == 0 14521308Sache#undef CMASK 14621308Sache#define CMASK 027 14721308Sache#endif 14821308Sacheint defumask = CMASK; /* default umask value */ 14921308Sachechar tmpline[7]; 15021308Sachechar *hostname; 15121308Sache#ifdef VIRTUAL_HOSTING 15221308Sachechar *ftpuser; 15321308Sache 15421308Sachestatic struct ftphost { 15521308Sache struct ftphost *next; 15621308Sache struct in_addr hostaddr; 15721308Sache char *hostname; 15821308Sache char *anonuser; 15921308Sache char *statfile; 16021308Sache char *welcome; 16121308Sache char *loginmsg; 16221308Sache} *thishost, *firsthost; 16321308Sache 16421308Sache#endif 16521308Sachechar remotehost[MAXHOSTNAMELEN]; 16621308Sachechar *ident = NULL; 16721308Sache 16821308Sachestatic char ttyline[20]; 16921308Sachechar *tty = ttyline; /* for klogin */ 17021308Sache 17121308Sache#ifdef KERBEROS 17221308Sacheint klogin __P((struct passwd *, char *, char *, char *)); 17321308Sache#endif 17421308Sache 17521308Sachestruct in_addr bind_address; 17621308Sachechar *pid_file = NULL; 17721308Sache 17821308Sache#if defined(KERBEROS) 17921308Sacheint notickets = 1; 18021308Sacheint noticketsdontcomplain = 1; 18121308Sachechar *krbtkfile_env = NULL; 18221308Sache#endif 18321308Sache 18421308Sache/* 18521308Sache * Timeout intervals for retrying connections 18621308Sache * to hosts that don't accept PORT cmds. This 18721308Sache * is a kludge, but given the problems with TCP... 18821308Sache */ 18921308Sache#define SWAITMAX 90 /* wait at most 90 seconds */ 19021308Sache#define SWAITINT 5 /* interval between retries */ 19121308Sache 19221308Sacheint swaitmax = SWAITMAX; 19321308Sacheint swaitint = SWAITINT; 19421308Sache 19521308Sache#ifdef SETPROCTITLE 19621308Sache#ifdef OLD_SETPROCTITLE 19721308Sachechar **Argv = NULL; /* pointer to argument vector */ 19821308Sachechar *LastArgv = NULL; /* end of argv */ 19921308Sache#endif /* OLD_SETPROCTITLE */ 20021308Sachechar proctitle[LINE_MAX]; /* initial part of title */ 20121308Sache#endif /* SETPROCTITLE */ 20221308Sache 20321308Sache#ifdef SKEY 204119610Sacheint pwok = 0; 20521308Sachechar addr_string[20]; /* XXX */ 20621308Sache#endif 20721308Sache 20821308Sache#define LOGCMD(cmd, file) \ 20921308Sache if (logging > 1) \ 210119610Sache syslog(LOG_INFO,"%s %s%s", cmd, \ 21121308Sache *(file) == '/' ? "" : curdir(), file); 21221308Sache#define LOGCMD2(cmd, file1, file2) \ 21321308Sache if (logging > 1) \ 21421308Sache syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 21521308Sache *(file1) == '/' ? "" : curdir(), file1, \ 21621308Sache *(file2) == '/' ? "" : curdir(), file2); 217119610Sache#define LOGBYTES(cmd, file, cnt) \ 218119610Sache if (logging > 1) { \ 21921308Sache if (cnt == (off_t)-1) \ 22021308Sache syslog(LOG_INFO,"%s %s%s", cmd, \ 22121308Sache *(file) == '/' ? "" : curdir(), file); \ 22221308Sache else \ 22321308Sache syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 22421308Sache cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 22521308Sache } 22621308Sache 22721308Sache#ifdef VIRTUAL_HOSTING 228119610Sachestatic void inithosts __P((void)); 22921308Sachestatic void selecthost __P((struct in_addr *)); 23021308Sache#endif 23121308Sachestatic void ack __P((char *)); 23221308Sachestatic void myoob __P((int)); 23321308Sachestatic int checkuser __P((char *, char *, int)); 234119610Sachestatic FILE *dataconn __P((char *, off_t, char *)); 23521308Sachestatic void dolog __P((struct sockaddr_in *)); 23621308Sachestatic char *curdir __P((void)); 23721308Sachestatic void end_login __P((void)); 23821308Sachestatic FILE *getdatasock __P((char *)); 239119610Sachestatic char *gunique __P((char *)); 240119610Sachestatic void lostconn __P((int)); 241119610Sachestatic int receive_data __P((FILE *, FILE *)); 24221308Sachestatic void send_data __P((FILE *, FILE *, off_t, off_t, int)); 24321308Sachestatic struct passwd * 24421308Sache sgetpwnam __P((char *)); 24521308Sachestatic char *sgetsave __P((char *)); 24621308Sachestatic void reapchild __P((int)); 24721308Sachestatic void logxfer __P((char *, long, long)); 24821308Sache 24921308Sachestatic char * 25021308Sachecurdir() 25121308Sache{ 252119610Sache static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 25321308Sache 25421308Sache if (getcwd(path, sizeof(path)-2) == NULL) 25521308Sache return (""); 25621308Sache if (path[1] != '\0') /* special case for root dir. */ 25721308Sache strcat(path, "/"); 258119610Sache /* For guest account, skip / since it's chrooted */ 25921308Sache return (guest ? path+1 : path); 26021308Sache} 26121308Sache 26221308Sacheint 263119610Sachemain(argc, argv, envp) 264119610Sache int argc; 26521308Sache char *argv[]; 26621308Sache char **envp; 26721308Sache{ 26821308Sache int addrlen, ch, on = 1, tos; 26921308Sache char *cp, line[LINE_MAX]; 27021308Sache FILE *fd; 27121308Sache 27221308Sache tzset(); /* in case no timezone database in ~ftp */ 27321308Sache 27421308Sache#ifdef OLD_SETPROCTITLE 275119610Sache /* 27621308Sache * Save start and extent of argv for setproctitle. 27721308Sache */ 27821308Sache Argv = argv; 27921308Sache while (*envp) 28021308Sache envp++; 28121308Sache LastArgv = envp[-1] + strlen(envp[-1]); 28275406Sache#endif /* OLD_SETPROCTITLE */ 28321308Sache 28421308Sache 285119610Sache bind_address.s_addr = htonl(INADDR_ANY); 28621308Sache while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:")) != -1) { 287119610Sache switch (ch) { 288119610Sache case 'D': 289119610Sache daemon_mode++; 290119610Sache break; 29121308Sache 29221308Sache case 'd': 29321308Sache debug++; 29421308Sache break; 29521308Sache 29621308Sache case 'l': 29721308Sache logging++; /* > 1 == extra logging */ 29821308Sache break; 29921308Sache 30021308Sache case 'R': 30121308Sache paranoid = 0; 30221308Sache break; 30321308Sache 304119610Sache case 'S': 30521308Sache stats++; 30621308Sache break; 30721308Sache 30821308Sache case 'T': 30921308Sache maxtimeout = atoi(optarg); 31021308Sache if (timeout > maxtimeout) 31121308Sache timeout = maxtimeout; 31221308Sache break; 31321308Sache 31421308Sache case 't': 31521308Sache timeout = atoi(optarg); 31621308Sache if (maxtimeout < timeout) 31721308Sache maxtimeout = timeout; 31821308Sache break; 31921308Sache 32021308Sache case 'U': 32121308Sache restricted_data_ports = 0; 32275406Sache break; 32321308Sache 32421308Sache case 'a': 32521308Sache if (!inet_aton(optarg, &bind_address)) 32621308Sache errx(1, "invalid address for -a"); 32721308Sache break; 32821308Sache 32921308Sache case 'p': 33021308Sache pid_file = optarg; 33121308Sache break; 33221308Sache 33321308Sache case 'u': 33421308Sache { 33521308Sache long val = 0; 33621308Sache 33721308Sache val = strtol(optarg, &optarg, 8); 33821308Sache if (*optarg != '\0' || val < 0) 339119610Sache warnx("bad value for -u"); 340119610Sache else 34121308Sache defumask = val; 342136644Sache break; 34321308Sache } 34421308Sache case 'A': 34521308Sache anon_only = 1; 346136644Sache break; 347136644Sache 348136644Sache case 'v': 349136644Sache debug = 1; 350136644Sache break; 351136644Sache 352136644Sache default: 353136644Sache warnx("unknown flag -%c ignored", optopt); 354136644Sache break; 355136644Sache } 356136644Sache } 357136644Sache 358136644Sache#ifdef VIRTUAL_HOSTING 359136644Sache inithosts(); 360136644Sache#endif 361136644Sache (void) freopen(_PATH_DEVNULL, "w", stderr); 362136644Sache 363136644Sache /* 364136644Sache * LOG_NDELAY sets up the logging connection immediately, 365136644Sache * necessary for anonymous ftp's that chroot and can't do it later. 366136644Sache */ 367136644Sache openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 368136644Sache 369136644Sache if (daemon_mode) { 370136644Sache int ctl_sock, fd; 371136644Sache struct servent *sv; 372136644Sache 373136644Sache /* 374136644Sache * Detach from parent. 375136644Sache */ 376136644Sache if (daemon(1, 1) < 0) { 377136644Sache syslog(LOG_ERR, "failed to become a daemon"); 378136644Sache exit(1); 379136644Sache } 380136644Sache (void) signal(SIGCHLD, reapchild); 381136644Sache /* 382136644Sache * Get port number for ftp/tcp. 383136644Sache */ 384136644Sache sv = getservbyname("ftp", "tcp"); 385136644Sache if (sv == NULL) { 38621308Sache syslog(LOG_ERR, "getservbyname for ftp failed"); 38721308Sache exit(1); 38821308Sache } 38921308Sache /* 39021308Sache * Open a socket, bind it to the FTP port, and start 39121308Sache * listening. 39221308Sache */ 39321308Sache ctl_sock = socket(AF_INET, SOCK_STREAM, 0); 39421308Sache if (ctl_sock < 0) { 39521308Sache syslog(LOG_ERR, "control socket: %m"); 39621308Sache exit(1); 39775406Sache } 39821308Sache if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 39921308Sache (char *)&on, sizeof(on)) < 0) 40021308Sache syslog(LOG_ERR, "control setsockopt: %m");; 40121308Sache server_addr.sin_family = AF_INET; 402119610Sache server_addr.sin_addr = bind_address; 403119610Sache server_addr.sin_port = sv->s_port; 40421308Sache if (bind(ctl_sock, (struct sockaddr *)&server_addr, sizeof(server_addr))) { 40521308Sache syslog(LOG_ERR, "control bind: %m"); 40621308Sache exit(1); 40721308Sache } 40821308Sache if (listen(ctl_sock, 32) < 0) { 40921308Sache syslog(LOG_ERR, "control listen: %m"); 41021308Sache exit(1); 41121308Sache } 41221308Sache /* 41321308Sache * Atomically write process ID 41421308Sache */ 41521308Sache if (pid_file) 416119610Sache { 41721308Sache int fd; 418119610Sache char buf[20]; 419119610Sache 420119610Sache fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 421119610Sache | O_NONBLOCK | O_EXLOCK, 0644); 42221308Sache if (fd < 0) { 42321308Sache if (errno == EAGAIN) 42421308Sache errx(1, "%s: file locked", pid_file); 42521308Sache else 42621308Sache err(1, "%s", pid_file); 42721308Sache } 42821308Sache snprintf(buf, sizeof(buf), 42921308Sache "%lu\n", (unsigned long) getpid()); 43021308Sache if (write(fd, buf, strlen(buf)) < 0) 43121308Sache err(1, "%s: write", pid_file); 43221308Sache /* Leave the pid file open and locked */ 43321308Sache } 43421308Sache /* 43521308Sache * Loop forever accepting connection requests and forking off 43621308Sache * children to handle them. 43721308Sache */ 43821308Sache while (1) { 43921308Sache addrlen = sizeof(his_addr); 44021308Sache fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 44158310Sache if (fork() == 0) { 44226497Sache /* child */ 44358310Sache (void) dup2(fd, 0); 44426497Sache (void) dup2(fd, 1); 44526497Sache close(ctl_sock); 44658310Sache break; 44726497Sache } 44821308Sache close(fd); 44921308Sache } 45021308Sache } else { 45121308Sache addrlen = sizeof(his_addr); 45221308Sache if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 45321308Sache syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 45421308Sache exit(1); 45521308Sache } 45621308Sache } 45721308Sache 45821308Sache (void) signal(SIGCHLD, SIG_IGN); 45921308Sache (void) signal(SIGPIPE, lostconn); 46021308Sache if (signal(SIGURG, myoob) == SIG_ERR) 46121308Sache syslog(LOG_ERR, "signal: %m"); 46221308Sache 46321308Sache#ifdef SKEY 46421308Sache strncpy(addr_string, inet_ntoa(his_addr.sin_addr), sizeof(addr_string)); 46521308Sache#endif 46621308Sache addrlen = sizeof(ctrl_addr); 46721308Sache if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 46821308Sache syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 46921308Sache exit(1); 47021308Sache } 47121308Sache#ifdef VIRTUAL_HOSTING 47221308Sache /* select our identity from virtual host table */ 47321308Sache selecthost(&ctrl_addr.sin_addr); 47421308Sache#endif 47521308Sache#ifdef IP_TOS 47621308Sache tos = IPTOS_LOWDELAY; 47721308Sache if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 47821308Sache syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 47921308Sache#endif 48021308Sache /* 48121308Sache * Disable Nagle on the control channel so that we don't have to wait 48221308Sache * for peer's ACK before issuing our next reply. 48321308Sache */ 48421308Sache if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 48521308Sache syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m"); 48621308Sache 48721308Sache data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 48821308Sache 48921308Sache /* set this here so klogin can use it... */ 49021308Sache (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 49121308Sache 49221308Sache /* Try to handle urgent data inline */ 49321308Sache#ifdef SO_OOBINLINE 49421308Sache if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 49521308Sache syslog(LOG_ERR, "setsockopt: %m"); 49621308Sache#endif 49721308Sache 49821308Sache#ifdef F_SETOWN 49921308Sache if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 50021308Sache syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 50121308Sache#endif 50221308Sache dolog(&his_addr); 50321308Sache /* 50421308Sache * Set up default state 50521308Sache */ 50621308Sache data = -1; 50721308Sache type = TYPE_A; 50821308Sache form = FORM_N; 50921308Sache stru = STRU_F; 51021308Sache mode = MODE_S; 51121308Sache tmpline[0] = '\0'; 51221308Sache 51321308Sache /* If logins are disabled, print out the message. */ 51421308Sache if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 51521308Sache while (fgets(line, sizeof(line), fd) != NULL) { 51621308Sache if ((cp = strchr(line, '\n')) != NULL) 51721308Sache *cp = '\0'; 51821308Sache lreply(530, "%s", line); 51921308Sache } 52021308Sache (void) fflush(stdout); 52121308Sache (void) fclose(fd); 52221308Sache reply(530, "System not available."); 52321308Sache exit(0); 52421308Sache } 52521308Sache#ifdef VIRTUAL_HOSTING 52621308Sache if ((fd = fopen(thishost->welcome, "r")) != NULL) { 52721308Sache#else 52821308Sache if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 52921308Sache#endif 53021308Sache while (fgets(line, sizeof(line), fd) != NULL) { 53121308Sache if ((cp = strchr(line, '\n')) != NULL) 53221308Sache *cp = '\0'; 53321308Sache lreply(220, "%s", line); 53421308Sache } 53521308Sache (void) fflush(stdout); 53621308Sache (void) fclose(fd); 53721308Sache /* reply(220,) must follow */ 53821308Sache } 53921308Sache#ifndef VIRTUAL_HOSTING 54021308Sache if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 54121308Sache fatal("Ran out of memory."); 54221308Sache (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 54321308Sache hostname[MAXHOSTNAMELEN - 1] = '\0'; 54421308Sache#endif 54521308Sache reply(220, "%s FTP server (%s) ready.", hostname, version); 54621308Sache (void) setjmp(errcatch); 54721308Sache for (;;) 54821308Sache (void) yyparse(); 54921308Sache /* NOTREACHED */ 55021308Sache} 55121308Sache 55221308Sachestatic void 55335486Sachelostconn(signo) 55435486Sache int signo; 55535486Sache{ 55635486Sache 55735486Sache if (debug) 55821308Sache syslog(LOG_DEBUG, "lost connection"); 55921308Sache dologout(1); 56021308Sache} 56158310Sache 56221308Sache#ifdef VIRTUAL_HOSTING 56358310Sache/* 56458310Sache * read in virtual host tables (if they exist) 56535486Sache */ 56635486Sache 56735486Sachestatic void 56835486Sacheinithosts() 56935486Sache{ 57035486Sache FILE *fp; 57121308Sache char *cp; 57258310Sache struct hostent *hp; 57358310Sache struct ftphost *hrp, *lhrp; 57458310Sache char line[1024]; 57558310Sache 57635486Sache /* 57775406Sache * Fill in the default host information 57821308Sache */ 57921308Sache if (gethostname(line, sizeof(line)) < 0) 58021308Sache line[0] = '\0'; 58121308Sache if ((hrp = malloc(sizeof(struct ftphost))) == NULL || 58221308Sache (hrp->hostname = strdup(line)) == NULL) 58321308Sache fatal("Ran out of memory."); 58475406Sache memset(&hrp->hostaddr, 0, sizeof hrp->hostaddr); 585165670Sache if ((hp = gethostbyname(hrp->hostname)) != NULL) 58621308Sache (void) memcpy(&hrp->hostaddr, 58721308Sache hp->h_addr_list[0], 58821308Sache sizeof(hrp->hostaddr)); 58921308Sache hrp->statfile = _PATH_FTPDSTATFILE; 59021308Sache hrp->welcome = _PATH_FTPWELCOME; 591119610Sache hrp->loginmsg = _PATH_FTPLOGINMESG; 592119610Sache hrp->anonuser = "ftp"; 59321308Sache hrp->next = NULL; 59421308Sache thishost = firsthost = lhrp = hrp; 59521308Sache if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 59621308Sache while (fgets(line, sizeof(line), fp) != NULL) { 59721308Sache int i; 59826497Sache 59921308Sache if ((cp = strchr(line, '\n')) == NULL) { 60021308Sache /* ignore long lines */ 60121308Sache while (fgets(line, sizeof(line), fp) != NULL && 60221308Sache strchr(line, '\n') == NULL) 60321308Sache ; 60421308Sache continue; 60521308Sache } 60621308Sache *cp = '\0'; 60721308Sache cp = strtok(line, " \t"); 60821308Sache /* skip comments and empty lines */ 60921308Sache if (cp == NULL || line[0] == '#') 61035486Sache continue; 61135486Sache /* first, try a standard gethostbyname() */ 61235486Sache if ((hp = gethostbyname(cp)) == NULL) 61335486Sache continue; 61435486Sache for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 61535486Sache if (memcmp(&hrp->hostaddr, 61635486Sache hp->h_addr_list[0], 61735486Sache sizeof(hrp->hostaddr)) == 0) 61821308Sache break; 61921308Sache } 62021308Sache if (hrp == NULL) { 62121308Sache if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 62221308Sache continue; 62321308Sache /* defaults */ 62421308Sache hrp->statfile = _PATH_FTPDSTATFILE; 62535486Sache hrp->welcome = _PATH_FTPWELCOME; 62635486Sache hrp->loginmsg = _PATH_FTPLOGINMESG; 62735486Sache hrp->anonuser = "ftp"; 62835486Sache hrp->next = NULL; 62947558Sache lhrp->next = hrp; 63047558Sache lhrp = hrp; 63135486Sache } 63235486Sache (void) memcpy(&hrp->hostaddr, 63335486Sache hp->h_addr_list[0], 63435486Sache sizeof(hrp->hostaddr)); 63535486Sache /* 63635486Sache * determine hostname to use. 63735486Sache * force defined name if it is a valid alias 63835486Sache * otherwise fallback to primary hostname 63921308Sache */ 64035486Sache if ((hp = gethostbyaddr((char*)&hrp->hostaddr, 64147558Sache sizeof(hrp->hostaddr), 64247558Sache AF_INET)) != NULL) { 64335486Sache if (strcmp(cp, hp->h_name) != 0) { 64435486Sache if (hp->h_aliases == NULL) 64535486Sache cp = hp->h_name; 64635486Sache else { 64735486Sache i = 0; 64835486Sache while (hp->h_aliases[i] && 64935486Sache strcmp(cp, hp->h_aliases[i]) != 0) 65035486Sache ++i; 65147558Sache if (hp->h_aliases[i] == NULL) 65235486Sache cp = hp->h_name; 65347558Sache } 65447558Sache } 65547558Sache } 65647558Sache hrp->hostname = strdup(cp); 65721308Sache /* ok, now we now peel off the rest */ 65835486Sache i = 0; 65935486Sache while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) { 66075406Sache if (*cp != '-' && (cp = strdup(cp)) != NULL) { 66135486Sache switch (i) { 66235486Sache case 0: /* anon user permissions */ 66335486Sache hrp->anonuser = cp; 66435486Sache break; 66535486Sache case 1: /* statistics file */ 66635486Sache hrp->statfile = cp; 66735486Sache break; 66835486Sache case 2: /* welcome message */ 66935486Sache hrp->welcome = cp; 67035486Sache break; 67135486Sache case 3: /* login message */ 67235486Sache hrp->loginmsg = cp; 67335486Sache break; 67435486Sache } 67535486Sache } 67635486Sache ++i; 67735486Sache } 67835486Sache } 67935486Sache (void) fclose(fp); 680119610Sache } 68135486Sache} 68235486Sache 68335486Sachestatic void 68435486Sacheselecthost(a) 68535486Sache struct in_addr *a; 686119610Sache{ 68735486Sache struct ftphost *hrp; 68835486Sache 68935486Sache hrp = thishost = firsthost; /* default */ 69035486Sache while (hrp != NULL) { 69135486Sache if (memcmp(a, &hrp->hostaddr, sizeof(hrp->hostaddr)) == 0) { 69235486Sache thishost = hrp; 69335486Sache break; 69475406Sache } 695 hrp = hrp->next; 696 } 697 /* setup static variables as appropriate */ 698 hostname = thishost->hostname; 699 ftpuser = thishost->anonuser; 700} 701#endif 702 703/* 704 * Helper function for sgetpwnam(). 705 */ 706static char * 707sgetsave(s) 708 char *s; 709{ 710 char *new = malloc((unsigned) strlen(s) + 1); 711 712 if (new == NULL) { 713 perror_reply(421, "Local resource failure: malloc"); 714 dologout(1); 715 /* NOTREACHED */ 716 } 717 (void) strcpy(new, s); 718 return (new); 719} 720 721/* 722 * Save the result of a getpwnam. Used for USER command, since 723 * the data returned must not be clobbered by any other command 724 * (e.g., globbing). 725 */ 726static struct passwd * 727sgetpwnam(name) 728 char *name; 729{ 730 static struct passwd save; 731 struct passwd *p; 732 733 if ((p = getpwnam(name)) == NULL) 734 return (p); 735 if (save.pw_name) { 736 free(save.pw_name); 737 free(save.pw_passwd); 738 free(save.pw_gecos); 739 free(save.pw_dir); 740 free(save.pw_shell); 741 } 742 save = *p; 743 save.pw_name = sgetsave(p->pw_name); 744 save.pw_passwd = sgetsave(p->pw_passwd); 745 save.pw_gecos = sgetsave(p->pw_gecos); 746 save.pw_dir = sgetsave(p->pw_dir); 747 save.pw_shell = sgetsave(p->pw_shell); 748 return (&save); 749} 750 751static int login_attempts; /* number of failed login attempts */ 752static int askpasswd; /* had user command, ask for passwd */ 753static char curname[10]; /* current USER name */ 754 755/* 756 * USER command. 757 * Sets global passwd pointer pw if named account exists and is acceptable; 758 * sets askpasswd if a PASS command is expected. If logged in previously, 759 * need to reset state. If name is "ftp" or "anonymous", the name is not in 760 * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 761 * If account doesn't exist, ask for passwd anyway. Otherwise, check user 762 * requesting login privileges. Disallow anyone who does not have a standard 763 * shell as returned by getusershell(). Disallow anyone mentioned in the file 764 * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 765 */ 766void 767user(name) 768 char *name; 769{ 770 char *cp, *shell; 771 772 if (logged_in) { 773 if (guest) { 774 reply(530, "Can't change user from guest login."); 775 return; 776 } else if (dochroot) { 777 reply(530, "Can't change user from chroot user."); 778 return; 779 } 780 end_login(); 781 } 782 783 guest = 0; 784 if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 785 if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 786 checkuser(_PATH_FTPUSERS, "anonymous", 0)) 787 reply(530, "User %s access denied.", name); 788#ifdef VIRTUAL_HOSTING 789 else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 790#else 791 else if ((pw = sgetpwnam("ftp")) != NULL) { 792#endif 793 guest = 1; 794 askpasswd = 1; 795 reply(331, 796 "Guest login ok, send your email address as password."); 797 } else 798 reply(530, "User %s unknown.", name); 799 if (!askpasswd && logging) 800 syslog(LOG_NOTICE, 801 "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 802 return; 803 } 804 if (anon_only != 0) { 805 reply(530, "Sorry, only anonymous ftp allowed."); 806 return; 807 } 808 809 if ((pw = sgetpwnam(name))) { 810 if ((shell = pw->pw_shell) == NULL || *shell == 0) 811 shell = _PATH_BSHELL; 812 while ((cp = getusershell()) != NULL) 813 if (strcmp(cp, shell) == 0) 814 break; 815 endusershell(); 816 817 if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 818 reply(530, "User %s access denied.", name); 819 if (logging) 820 syslog(LOG_NOTICE, 821 "FTP LOGIN REFUSED FROM %s, %s", 822 remotehost, name); 823 pw = (struct passwd *) NULL; 824 return; 825 } 826 } 827 if (logging) 828 strncpy(curname, name, sizeof(curname)-1); 829#ifdef SKEY 830 pwok = skeyaccess(name, NULL, remotehost, addr_string); 831 reply(331, "%s", skey_challenge(name, pw, pwok)); 832#else 833 reply(331, "Password required for %s.", name); 834#endif 835 askpasswd = 1; 836 /* 837 * Delay before reading passwd after first failed 838 * attempt to slow down passwd-guessing programs. 839 */ 840 if (login_attempts) 841 sleep((unsigned) login_attempts); 842} 843 844/* 845 * Check if a user is in the file "fname" 846 */ 847static int 848checkuser(fname, name, pwset) 849 char *fname; 850 char *name; 851 int pwset; 852{ 853 FILE *fd; 854 int found = 0; 855 char *p, line[BUFSIZ]; 856 857 if ((fd = fopen(fname, "r")) != NULL) { 858 while (!found && fgets(line, sizeof(line), fd) != NULL) 859 if ((p = strchr(line, '\n')) != NULL) { 860 *p = '\0'; 861 if (line[0] == '#') 862 continue; 863 /* 864 * if first chr is '@', check group membership 865 */ 866 if (line[0] == '@') { 867 int i = 0; 868 struct group *grp; 869 870 if ((grp = getgrnam(line+1)) == NULL) 871 continue; 872 /* 873 * Check user's default group 874 */ 875 if (pwset && grp->gr_gid == pw->pw_gid) 876 found = 1; 877 /* 878 * Check supplementary groups 879 */ 880 while (!found && grp->gr_mem[i]) 881 found = strcmp(name, 882 grp->gr_mem[i++]) 883 == 0; 884 } 885 /* 886 * Otherwise, just check for username match 887 */ 888 else 889 found = strcmp(line, name) == 0; 890 } 891 (void) fclose(fd); 892 } 893 return (found); 894} 895 896/* 897 * Terminate login as previous user, if any, resetting state; 898 * used when USER command is given or login fails. 899 */ 900static void 901end_login() 902{ 903 904 (void) seteuid((uid_t)0); 905 if (logged_in) 906 ftpd_logwtmp(ttyline, "", ""); 907 pw = NULL; 908#ifdef LOGIN_CAP 909 setusercontext(NULL, getpwuid(0), (uid_t)0, 910 LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 911#endif 912 logged_in = 0; 913 guest = 0; 914 dochroot = 0; 915} 916 917void 918pass(passwd) 919 char *passwd; 920{ 921 int rval; 922 FILE *fd; 923#ifdef LOGIN_CAP 924 login_cap_t *lc = NULL; 925#endif 926 927 if (logged_in || askpasswd == 0) { 928 reply(503, "Login with USER first."); 929 return; 930 } 931 askpasswd = 0; 932 if (!guest) { /* "ftp" is only account allowed no password */ 933 if (pw == NULL) { 934 rval = 1; /* failure below */ 935 goto skip; 936 } 937#if defined(KERBEROS) 938 rval = klogin(pw, "", hostname, passwd); 939 if (rval == 0) 940 goto skip; 941#endif 942#ifdef SKEY 943 rval = strcmp(skey_crypt(passwd, pw->pw_passwd, pw, pwok), 944 pw->pw_passwd); 945 pwok = 0; 946#else 947 rval = strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd); 948#endif 949 /* The strcmp does not catch null passwords! */ 950 if (*pw->pw_passwd == '\0' || 951 (pw->pw_expire && time(NULL) >= pw->pw_expire)) 952 rval = 1; /* failure */ 953skip: 954 /* 955 * If rval == 1, the user failed the authentication check 956 * above. If rval == 0, either Kerberos or local authentication 957 * succeeded. 958 */ 959 if (rval) { 960 reply(530, "Login incorrect."); 961 if (logging) 962 syslog(LOG_NOTICE, 963 "FTP LOGIN FAILED FROM %s, %s", 964 remotehost, curname); 965 pw = NULL; 966 if (login_attempts++ >= 5) { 967 syslog(LOG_NOTICE, 968 "repeated login failures from %s", 969 remotehost); 970 exit(0); 971 } 972 return; 973 } 974 } 975 login_attempts = 0; /* this time successful */ 976 if (setegid((gid_t)pw->pw_gid) < 0) { 977 reply(550, "Can't set gid."); 978 return; 979 } 980 /* May be overridden by login.conf */ 981 (void) umask(defumask); 982#ifdef LOGIN_CAP 983 if ((lc = login_getpwclass(pw)) != NULL) { 984 char remote_ip[MAXHOSTNAMELEN]; 985 986 strncpy(remote_ip, inet_ntoa(his_addr.sin_addr), 987 sizeof(remote_ip) - 1); 988 remote_ip[sizeof(remote_ip) - 1] = 0; 989 if (!auth_hostok(lc, remotehost, remote_ip)) { 990 syslog(LOG_INFO|LOG_AUTH, 991 "FTP LOGIN FAILED (HOST) as %s: permission denied.", 992 pw->pw_name); 993 reply(530, "Permission denied.\n"); 994 pw = NULL; 995 return; 996 } 997 if (!auth_timeok(lc, time(NULL))) { 998 reply(530, "Login not available right now.\n"); 999 pw = NULL; 1000 return; 1001 } 1002 } 1003 setusercontext(lc, pw, (uid_t)0, 1004 LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1005 LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1006#else 1007 setlogin(pw->pw_name); 1008 (void) initgroups(pw->pw_name, pw->pw_gid); 1009#endif 1010 1011 /* open wtmp before chroot */ 1012 ftpd_logwtmp(ttyline, pw->pw_name, remotehost); 1013 logged_in = 1; 1014 1015 if (guest && stats && statfd < 0) 1016#ifdef VIRTUAL_HOSTING 1017 if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1018#else 1019 if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1020#endif 1021 stats = 0; 1022 1023 dochroot = 1024#ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1025 login_getcapbool(lc, "ftp-chroot", 0) || 1026#endif 1027 checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1028 if (guest) { 1029 /* 1030 * We MUST do a chdir() after the chroot. Otherwise 1031 * the old current directory will be accessible as "." 1032 * outside the new root! 1033 */ 1034 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1035 reply(550, "Can't set guest privileges."); 1036 goto bad; 1037 } 1038 } else if (dochroot) { 1039 if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1040 reply(550, "Can't change root."); 1041 goto bad; 1042 } 1043 } else if (chdir(pw->pw_dir) < 0) { 1044 if (chdir("/") < 0) { 1045 reply(530, "User %s: can't change directory to %s.", 1046 pw->pw_name, pw->pw_dir); 1047 goto bad; 1048 } else 1049 lreply(230, "No directory! Logging in with home=/"); 1050 } 1051 if (seteuid((uid_t)pw->pw_uid) < 0) { 1052 reply(550, "Can't set uid."); 1053 goto bad; 1054 } 1055 1056 /* 1057 * Display a login message, if it exists. 1058 * N.B. reply(230,) must follow the message. 1059 */ 1060#ifdef VIRTUAL_HOSTING 1061 if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1062#else 1063 if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1064#endif 1065 char *cp, line[LINE_MAX]; 1066 1067 while (fgets(line, sizeof(line), fd) != NULL) { 1068 if ((cp = strchr(line, '\n')) != NULL) 1069 *cp = '\0'; 1070 lreply(230, "%s", line); 1071 } 1072 (void) fflush(stdout); 1073 (void) fclose(fd); 1074 } 1075 if (guest) { 1076 if (ident != NULL) 1077 free(ident); 1078 ident = strdup(passwd); 1079 if (ident == NULL) 1080 fatal("Ran out of memory."); 1081 1082 reply(230, "Guest login ok, access restrictions apply."); 1083#ifdef SETPROCTITLE 1084#ifdef VIRTUAL_HOSTING 1085 if (thishost != firsthost) 1086 snprintf(proctitle, sizeof(proctitle), 1087 "%s: anonymous(%s)/%.*s", remotehost, hostname, 1088 sizeof(proctitle) - sizeof(remotehost) - 1089 sizeof(": anonymous/"), passwd); 1090 else 1091#endif 1092 snprintf(proctitle, sizeof(proctitle), 1093 "%s: anonymous/%.*s", remotehost, 1094 sizeof(proctitle) - sizeof(remotehost) - 1095 sizeof(": anonymous/"), passwd); 1096 setproctitle("%s", proctitle); 1097#endif /* SETPROCTITLE */ 1098 if (logging) 1099 syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1100 remotehost, passwd); 1101 } else { 1102 if (dochroot) 1103 reply(230, "User %s logged in, access restrictions apply.", 1104 pw->pw_name); 1105 else 1106 reply(230, "User %s logged in.", pw->pw_name); 1107 1108#ifdef SETPROCTITLE 1109 snprintf(proctitle, sizeof(proctitle), 1110 "%s: %s", remotehost, pw->pw_name); 1111 setproctitle("%s", proctitle); 1112#endif /* SETPROCTITLE */ 1113 if (logging) 1114 syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1115 remotehost, pw->pw_name); 1116 } 1117#ifdef LOGIN_CAP 1118 login_close(lc); 1119#endif 1120 return; 1121bad: 1122 /* Forget all about it... */ 1123#ifdef LOGIN_CAP 1124 login_close(lc); 1125#endif 1126 end_login(); 1127} 1128 1129void 1130retrieve(cmd, name) 1131 char *cmd, *name; 1132{ 1133 FILE *fin, *dout; 1134 struct stat st; 1135 int (*closefunc) __P((FILE *)); 1136 time_t start; 1137 1138 if (cmd == 0) { 1139 fin = fopen(name, "r"), closefunc = fclose; 1140 st.st_size = 0; 1141 } else { 1142 char line[BUFSIZ]; 1143 1144 (void) snprintf(line, sizeof(line), cmd, name), name = line; 1145 fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1146 st.st_size = -1; 1147 st.st_blksize = BUFSIZ; 1148 } 1149 if (fin == NULL) { 1150 if (errno != 0) { 1151 perror_reply(550, name); 1152 if (cmd == 0) { 1153 LOGCMD("get", name); 1154 } 1155 } 1156 return; 1157 } 1158 byte_count = -1; 1159 if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1160 reply(550, "%s: not a plain file.", name); 1161 goto done; 1162 } 1163 if (restart_point) { 1164 if (type == TYPE_A) { 1165 off_t i, n; 1166 int c; 1167 1168 n = restart_point; 1169 i = 0; 1170 while (i++ < n) { 1171 if ((c=getc(fin)) == EOF) { 1172 perror_reply(550, name); 1173 goto done; 1174 } 1175 if (c == '\n') 1176 i++; 1177 } 1178 } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1179 perror_reply(550, name); 1180 goto done; 1181 } 1182 } 1183 dout = dataconn(name, st.st_size, "w"); 1184 if (dout == NULL) 1185 goto done; 1186 time(&start); 1187 send_data(fin, dout, st.st_blksize, st.st_size, 1188 restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 1189 if (cmd == 0 && guest && stats) 1190 logxfer(name, st.st_size, start); 1191 (void) fclose(dout); 1192 data = -1; 1193 pdata = -1; 1194done: 1195 if (cmd == 0) 1196 LOGBYTES("get", name, byte_count); 1197 (*closefunc)(fin); 1198} 1199 1200void 1201store(name, mode, unique) 1202 char *name, *mode; 1203 int unique; 1204{ 1205 FILE *fout, *din; 1206 struct stat st; 1207 int (*closefunc) __P((FILE *)); 1208 1209 if ((unique || guest) && stat(name, &st) == 0 && 1210 (name = gunique(name)) == NULL) { 1211 LOGCMD(*mode == 'w' ? "put" : "append", name); 1212 return; 1213 } 1214 1215 if (restart_point) 1216 mode = "r+"; 1217 fout = fopen(name, mode); 1218 closefunc = fclose; 1219 if (fout == NULL) { 1220 perror_reply(553, name); 1221 LOGCMD(*mode == 'w' ? "put" : "append", name); 1222 return; 1223 } 1224 byte_count = -1; 1225 if (restart_point) { 1226 if (type == TYPE_A) { 1227 off_t i, n; 1228 int c; 1229 1230 n = restart_point; 1231 i = 0; 1232 while (i++ < n) { 1233 if ((c=getc(fout)) == EOF) { 1234 perror_reply(550, name); 1235 goto done; 1236 } 1237 if (c == '\n') 1238 i++; 1239 } 1240 /* 1241 * We must do this seek to "current" position 1242 * because we are changing from reading to 1243 * writing. 1244 */ 1245 if (fseek(fout, 0L, L_INCR) < 0) { 1246 perror_reply(550, name); 1247 goto done; 1248 } 1249 } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1250 perror_reply(550, name); 1251 goto done; 1252 } 1253 } 1254 din = dataconn(name, (off_t)-1, "r"); 1255 if (din == NULL) 1256 goto done; 1257 if (receive_data(din, fout) == 0) { 1258 if (unique) 1259 reply(226, "Transfer complete (unique file name:%s).", 1260 name); 1261 else 1262 reply(226, "Transfer complete."); 1263 } 1264 (void) fclose(din); 1265 data = -1; 1266 pdata = -1; 1267done: 1268 LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1269 (*closefunc)(fout); 1270} 1271 1272static FILE * 1273getdatasock(mode) 1274 char *mode; 1275{ 1276 int on = 1, s, t, tries; 1277 1278 if (data >= 0) 1279 return (fdopen(data, mode)); 1280 (void) seteuid((uid_t)0); 1281 s = socket(AF_INET, SOCK_STREAM, 0); 1282 if (s < 0) 1283 goto bad; 1284 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1285 (char *) &on, sizeof(on)) < 0) 1286 goto bad; 1287 /* anchor socket to avoid multi-homing problems */ 1288 data_source.sin_len = sizeof(struct sockaddr_in); 1289 data_source.sin_family = AF_INET; 1290 data_source.sin_addr = ctrl_addr.sin_addr; 1291 for (tries = 1; ; tries++) { 1292 if (bind(s, (struct sockaddr *)&data_source, 1293 sizeof(data_source)) >= 0) 1294 break; 1295 if (errno != EADDRINUSE || tries > 10) 1296 goto bad; 1297 sleep(tries); 1298 } 1299 (void) seteuid((uid_t)pw->pw_uid); 1300#ifdef IP_TOS 1301 on = IPTOS_THROUGHPUT; 1302 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1303 syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 1304#endif 1305#ifdef TCP_NOPUSH 1306 /* 1307 * Turn off push flag to keep sender TCP from sending short packets 1308 * at the boundaries of each write(). Should probably do a SO_SNDBUF 1309 * to set the send buffer size as well, but that may not be desirable 1310 * in heavy-load situations. 1311 */ 1312 on = 1; 1313 if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 1314 syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 1315#endif 1316#ifdef SO_SNDBUF 1317 on = 65536; 1318 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 1319 syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 1320#endif 1321 1322 return (fdopen(s, mode)); 1323bad: 1324 /* Return the real value of errno (close may change it) */ 1325 t = errno; 1326 (void) seteuid((uid_t)pw->pw_uid); 1327 (void) close(s); 1328 errno = t; 1329 return (NULL); 1330} 1331 1332static FILE * 1333dataconn(name, size, mode) 1334 char *name; 1335 off_t size; 1336 char *mode; 1337{ 1338 char sizebuf[32]; 1339 FILE *file; 1340 int retry = 0, tos; 1341 1342 file_size = size; 1343 byte_count = 0; 1344 if (size != (off_t) -1) 1345 (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1346 else 1347 *sizebuf = '\0'; 1348 if (pdata >= 0) { 1349 struct sockaddr_in from; 1350 int s, fromlen = sizeof(from); 1351 struct timeval timeout; 1352 fd_set set; 1353 1354 FD_ZERO(&set); 1355 FD_SET(pdata, &set); 1356 1357 timeout.tv_usec = 0; 1358 timeout.tv_sec = 120; 1359 1360 if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1361 (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1362 reply(425, "Can't open data connection."); 1363 (void) close(pdata); 1364 pdata = -1; 1365 return (NULL); 1366 } 1367 (void) close(pdata); 1368 pdata = s; 1369#ifdef IP_TOS 1370 tos = IPTOS_THROUGHPUT; 1371 (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1372 sizeof(int)); 1373#endif 1374 reply(150, "Opening %s mode data connection for '%s'%s.", 1375 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1376 return (fdopen(pdata, mode)); 1377 } 1378 if (data >= 0) { 1379 reply(125, "Using existing data connection for '%s'%s.", 1380 name, sizebuf); 1381 usedefault = 1; 1382 return (fdopen(data, mode)); 1383 } 1384 if (usedefault) 1385 data_dest = his_addr; 1386 usedefault = 1; 1387 file = getdatasock(mode); 1388 if (file == NULL) { 1389 reply(425, "Can't create data socket (%s,%d): %s.", 1390 inet_ntoa(data_source.sin_addr), 1391 ntohs(data_source.sin_port), strerror(errno)); 1392 return (NULL); 1393 } 1394 data = fileno(file); 1395 while (connect(data, (struct sockaddr *)&data_dest, 1396 sizeof(data_dest)) < 0) { 1397 if (errno == EADDRINUSE && retry < swaitmax) { 1398 sleep((unsigned) swaitint); 1399 retry += swaitint; 1400 continue; 1401 } 1402 perror_reply(425, "Can't build data connection"); 1403 (void) fclose(file); 1404 data = -1; 1405 return (NULL); 1406 } 1407 reply(150, "Opening %s mode data connection for '%s'%s.", 1408 type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1409 return (file); 1410} 1411 1412/* 1413 * Tranfer the contents of "instr" to "outstr" peer using the appropriate 1414 * encapsulation of the data subject to Mode, Structure, and Type. 1415 * 1416 * NB: Form isn't handled. 1417 */ 1418static void 1419send_data(instr, outstr, blksize, filesize, isreg) 1420 FILE *instr, *outstr; 1421 off_t blksize; 1422 off_t filesize; 1423 int isreg; 1424{ 1425 int c, cnt, filefd, netfd; 1426 char *buf, *bp; 1427 size_t len; 1428 1429 transflag++; 1430 if (setjmp(urgcatch)) { 1431 transflag = 0; 1432 return; 1433 } 1434 switch (type) { 1435 1436 case TYPE_A: 1437 while ((c = getc(instr)) != EOF) { 1438 byte_count++; 1439 if (c == '\n') { 1440 if (ferror(outstr)) 1441 goto data_err; 1442 (void) putc('\r', outstr); 1443 } 1444 (void) putc(c, outstr); 1445 } 1446 fflush(outstr); 1447 transflag = 0; 1448 if (ferror(instr)) 1449 goto file_err; 1450 if (ferror(outstr)) 1451 goto data_err; 1452 reply(226, "Transfer complete."); 1453 return; 1454 1455 case TYPE_I: 1456 case TYPE_L: 1457 /* 1458 * isreg is only set if we are not doing restart and we 1459 * are sending a regular file 1460 */ 1461 netfd = fileno(outstr); 1462 filefd = fileno(instr); 1463 1464 if (isreg && filesize < (off_t)16 * 1024 * 1024) { 1465 buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, 1466 (off_t)0); 1467 if (buf == MAP_FAILED) { 1468 syslog(LOG_WARNING, "mmap(%lu): %m", 1469 (unsigned long)filesize); 1470 goto oldway; 1471 } 1472 bp = buf; 1473 len = filesize; 1474 do { 1475 cnt = write(netfd, bp, len); 1476 len -= cnt; 1477 bp += cnt; 1478 if (cnt > 0) byte_count += cnt; 1479 } while(cnt > 0 && len > 0); 1480 1481 transflag = 0; 1482 munmap(buf, (size_t)filesize); 1483 if (cnt < 0) 1484 goto data_err; 1485 reply(226, "Transfer complete."); 1486 return; 1487 } 1488 1489oldway: 1490 if ((buf = malloc((u_int)blksize)) == NULL) { 1491 transflag = 0; 1492 perror_reply(451, "Local resource failure: malloc"); 1493 return; 1494 } 1495 1496 while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1497 write(netfd, buf, cnt) == cnt) 1498 byte_count += cnt; 1499 transflag = 0; 1500 (void)free(buf); 1501 if (cnt != 0) { 1502 if (cnt < 0) 1503 goto file_err; 1504 goto data_err; 1505 } 1506 reply(226, "Transfer complete."); 1507 return; 1508 default: 1509 transflag = 0; 1510 reply(550, "Unimplemented TYPE %d in send_data", type); 1511 return; 1512 } 1513 1514data_err: 1515 transflag = 0; 1516 perror_reply(426, "Data connection"); 1517 return; 1518 1519file_err: 1520 transflag = 0; 1521 perror_reply(551, "Error on input file"); 1522} 1523 1524/* 1525 * Transfer data from peer to "outstr" using the appropriate encapulation of 1526 * the data subject to Mode, Structure, and Type. 1527 * 1528 * N.B.: Form isn't handled. 1529 */ 1530static int 1531receive_data(instr, outstr) 1532 FILE *instr, *outstr; 1533{ 1534 int c; 1535 int cnt, bare_lfs; 1536 char buf[BUFSIZ]; 1537 1538 transflag++; 1539 if (setjmp(urgcatch)) { 1540 transflag = 0; 1541 return (-1); 1542 } 1543 1544 bare_lfs = 0; 1545 1546 switch (type) { 1547 1548 case TYPE_I: 1549 case TYPE_L: 1550 while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1551 if (write(fileno(outstr), buf, cnt) != cnt) 1552 goto file_err; 1553 byte_count += cnt; 1554 } 1555 if (cnt < 0) 1556 goto data_err; 1557 transflag = 0; 1558 return (0); 1559 1560 case TYPE_E: 1561 reply(553, "TYPE E not implemented."); 1562 transflag = 0; 1563 return (-1); 1564 1565 case TYPE_A: 1566 while ((c = getc(instr)) != EOF) { 1567 byte_count++; 1568 if (c == '\n') 1569 bare_lfs++; 1570 while (c == '\r') { 1571 if (ferror(outstr)) 1572 goto data_err; 1573 if ((c = getc(instr)) != '\n') { 1574 (void) putc ('\r', outstr); 1575 if (c == '\0' || c == EOF) 1576 goto contin2; 1577 } 1578 } 1579 (void) putc(c, outstr); 1580 contin2: ; 1581 } 1582 fflush(outstr); 1583 if (ferror(instr)) 1584 goto data_err; 1585 if (ferror(outstr)) 1586 goto file_err; 1587 transflag = 0; 1588 if (bare_lfs) { 1589 lreply(226, 1590 "WARNING! %d bare linefeeds received in ASCII mode", 1591 bare_lfs); 1592 (void)printf(" File may not have transferred correctly.\r\n"); 1593 } 1594 return (0); 1595 default: 1596 reply(550, "Unimplemented TYPE %d in receive_data", type); 1597 transflag = 0; 1598 return (-1); 1599 } 1600 1601data_err: 1602 transflag = 0; 1603 perror_reply(426, "Data Connection"); 1604 return (-1); 1605 1606file_err: 1607 transflag = 0; 1608 perror_reply(452, "Error writing file"); 1609 return (-1); 1610} 1611 1612void 1613statfilecmd(filename) 1614 char *filename; 1615{ 1616 FILE *fin; 1617 int c; 1618 char line[LINE_MAX]; 1619 1620 (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 1621 fin = ftpd_popen(line, "r"); 1622 lreply(211, "status of %s:", filename); 1623 while ((c = getc(fin)) != EOF) { 1624 if (c == '\n') { 1625 if (ferror(stdout)){ 1626 perror_reply(421, "control connection"); 1627 (void) ftpd_pclose(fin); 1628 dologout(1); 1629 /* NOTREACHED */ 1630 } 1631 if (ferror(fin)) { 1632 perror_reply(551, filename); 1633 (void) ftpd_pclose(fin); 1634 return; 1635 } 1636 (void) putc('\r', stdout); 1637 } 1638 (void) putc(c, stdout); 1639 } 1640 (void) ftpd_pclose(fin); 1641 reply(211, "End of Status"); 1642} 1643 1644void 1645statcmd() 1646{ 1647 struct sockaddr_in *sin; 1648 u_char *a, *p; 1649 1650 lreply(211, "%s FTP server status:", hostname, version); 1651 printf(" %s\r\n", version); 1652 printf(" Connected to %s", remotehost); 1653 if (!isdigit(remotehost[0])) 1654 printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 1655 printf("\r\n"); 1656 if (logged_in) { 1657 if (guest) 1658 printf(" Logged in anonymously\r\n"); 1659 else 1660 printf(" Logged in as %s\r\n", pw->pw_name); 1661 } else if (askpasswd) 1662 printf(" Waiting for password\r\n"); 1663 else 1664 printf(" Waiting for user name\r\n"); 1665 printf(" TYPE: %s", typenames[type]); 1666 if (type == TYPE_A || type == TYPE_E) 1667 printf(", FORM: %s", formnames[form]); 1668 if (type == TYPE_L) 1669#if NBBY == 8 1670 printf(" %d", NBBY); 1671#else 1672 printf(" %d", bytesize); /* need definition! */ 1673#endif 1674 printf("; STRUcture: %s; transfer MODE: %s\r\n", 1675 strunames[stru], modenames[mode]); 1676 if (data != -1) 1677 printf(" Data connection open\r\n"); 1678 else if (pdata != -1) { 1679 printf(" in Passive mode"); 1680 sin = &pasv_addr; 1681 goto printaddr; 1682 } else if (usedefault == 0) { 1683 printf(" PORT"); 1684 sin = &data_dest; 1685printaddr: 1686 a = (u_char *) &sin->sin_addr; 1687 p = (u_char *) &sin->sin_port; 1688#define UC(b) (((int) b) & 0xff) 1689 printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 1690 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1691#undef UC 1692 } else 1693 printf(" No data connection\r\n"); 1694 reply(211, "End of status"); 1695} 1696 1697void 1698fatal(s) 1699 char *s; 1700{ 1701 1702 reply(451, "Error in server: %s\n", s); 1703 reply(221, "Closing connection due to server error."); 1704 dologout(0); 1705 /* NOTREACHED */ 1706} 1707 1708void 1709#if __STDC__ 1710reply(int n, const char *fmt, ...) 1711#else 1712reply(n, fmt, va_alist) 1713 int n; 1714 char *fmt; 1715 va_dcl 1716#endif 1717{ 1718 va_list ap; 1719#if __STDC__ 1720 va_start(ap, fmt); 1721#else 1722 va_start(ap); 1723#endif 1724 (void)printf("%d ", n); 1725 (void)vprintf(fmt, ap); 1726 (void)printf("\r\n"); 1727 (void)fflush(stdout); 1728 if (debug) { 1729 syslog(LOG_DEBUG, "<--- %d ", n); 1730 vsyslog(LOG_DEBUG, fmt, ap); 1731 } 1732} 1733 1734void 1735#if __STDC__ 1736lreply(int n, const char *fmt, ...) 1737#else 1738lreply(n, fmt, va_alist) 1739 int n; 1740 char *fmt; 1741 va_dcl 1742#endif 1743{ 1744 va_list ap; 1745#if __STDC__ 1746 va_start(ap, fmt); 1747#else 1748 va_start(ap); 1749#endif 1750 (void)printf("%d- ", n); 1751 (void)vprintf(fmt, ap); 1752 (void)printf("\r\n"); 1753 (void)fflush(stdout); 1754 if (debug) { 1755 syslog(LOG_DEBUG, "<--- %d- ", n); 1756 vsyslog(LOG_DEBUG, fmt, ap); 1757 } 1758} 1759 1760static void 1761ack(s) 1762 char *s; 1763{ 1764 1765 reply(250, "%s command successful.", s); 1766} 1767 1768void 1769nack(s) 1770 char *s; 1771{ 1772 1773 reply(502, "%s command not implemented.", s); 1774} 1775 1776/* ARGSUSED */ 1777void 1778yyerror(s) 1779 char *s; 1780{ 1781 char *cp; 1782 1783 if ((cp = strchr(cbuf,'\n'))) 1784 *cp = '\0'; 1785 reply(500, "'%s': command not understood.", cbuf); 1786} 1787 1788void 1789delete(name) 1790 char *name; 1791{ 1792 struct stat st; 1793 1794 LOGCMD("delete", name); 1795 if (stat(name, &st) < 0) { 1796 perror_reply(550, name); 1797 return; 1798 } 1799 if ((st.st_mode&S_IFMT) == S_IFDIR) { 1800 if (rmdir(name) < 0) { 1801 perror_reply(550, name); 1802 return; 1803 } 1804 goto done; 1805 } 1806 if (unlink(name) < 0) { 1807 perror_reply(550, name); 1808 return; 1809 } 1810done: 1811 ack("DELE"); 1812} 1813 1814void 1815cwd(path) 1816 char *path; 1817{ 1818 1819 if (chdir(path) < 0) 1820 perror_reply(550, path); 1821 else 1822 ack("CWD"); 1823} 1824 1825void 1826makedir(name) 1827 char *name; 1828{ 1829 1830 LOGCMD("mkdir", name); 1831 if (mkdir(name, 0777) < 0) 1832 perror_reply(550, name); 1833 else 1834 reply(257, "MKD command successful."); 1835} 1836 1837void 1838removedir(name) 1839 char *name; 1840{ 1841 1842 LOGCMD("rmdir", name); 1843 if (rmdir(name) < 0) 1844 perror_reply(550, name); 1845 else 1846 ack("RMD"); 1847} 1848 1849void 1850pwd() 1851{ 1852 char path[MAXPATHLEN + 1]; 1853 1854 if (getwd(path) == (char *)NULL) 1855 reply(550, "%s.", path); 1856 else 1857 reply(257, "\"%s\" is current directory.", path); 1858} 1859 1860char * 1861renamefrom(name) 1862 char *name; 1863{ 1864 struct stat st; 1865 1866 if (stat(name, &st) < 0) { 1867 perror_reply(550, name); 1868 return ((char *)0); 1869 } 1870 reply(350, "File exists, ready for destination name"); 1871 return (name); 1872} 1873 1874void 1875renamecmd(from, to) 1876 char *from, *to; 1877{ 1878 struct stat st; 1879 1880 LOGCMD2("rename", from, to); 1881 1882 if (guest && (stat(to, &st) == 0)) { 1883 reply(550, "%s: permission denied", to); 1884 return; 1885 } 1886 1887 if (rename(from, to) < 0) 1888 perror_reply(550, "rename"); 1889 else 1890 ack("RNTO"); 1891} 1892 1893static void 1894dolog(sin) 1895 struct sockaddr_in *sin; 1896{ 1897 realhostname(remotehost, sizeof(remotehost) - 1, &sin->sin_addr); 1898 1899#ifdef SETPROCTITLE 1900#ifdef VIRTUAL_HOSTING 1901 if (thishost != firsthost) 1902 snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 1903 remotehost, hostname); 1904 else 1905#endif 1906 snprintf(proctitle, sizeof(proctitle), "%s: connected", 1907 remotehost); 1908 setproctitle("%s", proctitle); 1909#endif /* SETPROCTITLE */ 1910 1911 if (logging) { 1912#ifdef VIRTUAL_HOSTING 1913 if (thishost != firsthost) 1914 syslog(LOG_INFO, "connection from %s (to %s)", 1915 remotehost, hostname); 1916 else 1917#endif 1918 syslog(LOG_INFO, "connection from %s (%s)", remotehost, 1919 inet_ntoa(sin->sin_addr)); 1920 } 1921} 1922 1923/* 1924 * Record logout in wtmp file 1925 * and exit with supplied status. 1926 */ 1927void 1928dologout(status) 1929 int status; 1930{ 1931 /* 1932 * Prevent reception of SIGURG from resulting in a resumption 1933 * back to the main program loop. 1934 */ 1935 transflag = 0; 1936 1937 if (logged_in) { 1938 (void) seteuid((uid_t)0); 1939 ftpd_logwtmp(ttyline, "", ""); 1940#if defined(KERBEROS) 1941 if (!notickets && krbtkfile_env) 1942 unlink(krbtkfile_env); 1943#endif 1944 } 1945 /* beware of flushing buffers after a SIGPIPE */ 1946 _exit(status); 1947} 1948 1949static void 1950myoob(signo) 1951 int signo; 1952{ 1953 char *cp; 1954 1955 /* only process if transfer occurring */ 1956 if (!transflag) 1957 return; 1958 cp = tmpline; 1959 if (getline(cp, 7, stdin) == NULL) { 1960 reply(221, "You could at least say goodbye."); 1961 dologout(0); 1962 } 1963 upper(cp); 1964 if (strcmp(cp, "ABOR\r\n") == 0) { 1965 tmpline[0] = '\0'; 1966 reply(426, "Transfer aborted. Data connection closed."); 1967 reply(226, "Abort successful"); 1968 longjmp(urgcatch, 1); 1969 } 1970 if (strcmp(cp, "STAT\r\n") == 0) { 1971 if (file_size != (off_t) -1) 1972 reply(213, "Status: %qd of %qd bytes transferred", 1973 byte_count, file_size); 1974 else 1975 reply(213, "Status: %qd bytes transferred", byte_count); 1976 } 1977} 1978 1979/* 1980 * Note: a response of 425 is not mentioned as a possible response to 1981 * the PASV command in RFC959. However, it has been blessed as 1982 * a legitimate response by Jon Postel in a telephone conversation 1983 * with Rick Adams on 25 Jan 89. 1984 */ 1985void 1986passive() 1987{ 1988 int len; 1989 char *p, *a; 1990 1991 if (pdata >= 0) /* close old port if one set */ 1992 close(pdata); 1993 1994 pdata = socket(AF_INET, SOCK_STREAM, 0); 1995 if (pdata < 0) { 1996 perror_reply(425, "Can't open passive connection"); 1997 return; 1998 } 1999 2000 (void) seteuid((uid_t)0); 2001 2002#ifdef IP_PORTRANGE 2003 { 2004 int on = restricted_data_ports ? IP_PORTRANGE_HIGH 2005 : IP_PORTRANGE_DEFAULT; 2006 2007 if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2008 (char *)&on, sizeof(on)) < 0) 2009 goto pasv_error; 2010 } 2011#endif 2012 2013 pasv_addr = ctrl_addr; 2014 pasv_addr.sin_port = 0; 2015 if (bind(pdata, (struct sockaddr *)&pasv_addr, 2016 sizeof(pasv_addr)) < 0) 2017 goto pasv_error; 2018 2019 (void) seteuid((uid_t)pw->pw_uid); 2020 2021 len = sizeof(pasv_addr); 2022 if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2023 goto pasv_error; 2024 if (listen(pdata, 1) < 0) 2025 goto pasv_error; 2026 a = (char *) &pasv_addr.sin_addr; 2027 p = (char *) &pasv_addr.sin_port; 2028 2029#define UC(b) (((int) b) & 0xff) 2030 2031 reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2032 UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2033 return; 2034 2035pasv_error: 2036 (void) seteuid((uid_t)pw->pw_uid); 2037 (void) close(pdata); 2038 pdata = -1; 2039 perror_reply(425, "Can't open passive connection"); 2040 return; 2041} 2042 2043/* 2044 * Generate unique name for file with basename "local". 2045 * The file named "local" is already known to exist. 2046 * Generates failure reply on error. 2047 */ 2048static char * 2049gunique(local) 2050 char *local; 2051{ 2052 static char new[MAXPATHLEN]; 2053 struct stat st; 2054 int count; 2055 char *cp; 2056 2057 cp = strrchr(local, '/'); 2058 if (cp) 2059 *cp = '\0'; 2060 if (stat(cp ? local : ".", &st) < 0) { 2061 perror_reply(553, cp ? local : "."); 2062 return ((char *) 0); 2063 } 2064 if (cp) 2065 *cp = '/'; 2066 /* -4 is for the .nn<null> we put on the end below */ 2067 (void) snprintf(new, sizeof(new) - 4, "%s", local); 2068 cp = new + strlen(new); 2069 *cp++ = '.'; 2070 for (count = 1; count < 100; count++) { 2071 (void)sprintf(cp, "%d", count); 2072 if (stat(new, &st) < 0) 2073 return (new); 2074 } 2075 reply(452, "Unique file name cannot be created."); 2076 return (NULL); 2077} 2078 2079/* 2080 * Format and send reply containing system error number. 2081 */ 2082void 2083perror_reply(code, string) 2084 int code; 2085 char *string; 2086{ 2087 2088 reply(code, "%s: %s.", string, strerror(errno)); 2089} 2090 2091static char *onefile[] = { 2092 "", 2093 0 2094}; 2095 2096void 2097send_file_list(whichf) 2098 char *whichf; 2099{ 2100 struct stat st; 2101 DIR *dirp = NULL; 2102 struct dirent *dir; 2103 FILE *dout = NULL; 2104 char **dirlist, *dirname; 2105 int simple = 0; 2106 int freeglob = 0; 2107 glob_t gl; 2108 2109 if (strpbrk(whichf, "~{[*?") != NULL) { 2110 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 2111 2112 memset(&gl, 0, sizeof(gl)); 2113 freeglob = 1; 2114 if (glob(whichf, flags, 0, &gl)) { 2115 reply(550, "not found"); 2116 goto out; 2117 } else if (gl.gl_pathc == 0) { 2118 errno = ENOENT; 2119 perror_reply(550, whichf); 2120 goto out; 2121 } 2122 dirlist = gl.gl_pathv; 2123 } else { 2124 onefile[0] = whichf; 2125 dirlist = onefile; 2126 simple = 1; 2127 } 2128 2129 if (setjmp(urgcatch)) { 2130 transflag = 0; 2131 goto out; 2132 } 2133 while ((dirname = *dirlist++)) { 2134 if (stat(dirname, &st) < 0) { 2135 /* 2136 * If user typed "ls -l", etc, and the client 2137 * used NLST, do what the user meant. 2138 */ 2139 if (dirname[0] == '-' && *dirlist == NULL && 2140 transflag == 0) { 2141 retrieve(_PATH_LS " %s", dirname); 2142 goto out; 2143 } 2144 perror_reply(550, whichf); 2145 if (dout != NULL) { 2146 (void) fclose(dout); 2147 transflag = 0; 2148 data = -1; 2149 pdata = -1; 2150 } 2151 goto out; 2152 } 2153 2154 if (S_ISREG(st.st_mode)) { 2155 if (dout == NULL) { 2156 dout = dataconn("file list", (off_t)-1, "w"); 2157 if (dout == NULL) 2158 goto out; 2159 transflag++; 2160 } 2161 fprintf(dout, "%s%s\n", dirname, 2162 type == TYPE_A ? "\r" : ""); 2163 byte_count += strlen(dirname) + 1; 2164 continue; 2165 } else if (!S_ISDIR(st.st_mode)) 2166 continue; 2167 2168 if ((dirp = opendir(dirname)) == NULL) 2169 continue; 2170 2171 while ((dir = readdir(dirp)) != NULL) { 2172 char nbuf[MAXPATHLEN]; 2173 2174 if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2175 continue; 2176 if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2177 dir->d_namlen == 2) 2178 continue; 2179 2180 snprintf(nbuf, sizeof(nbuf), 2181 "%s/%s", dirname, dir->d_name); 2182 2183 /* 2184 * We have to do a stat to insure it's 2185 * not a directory or special file. 2186 */ 2187 if (simple || (stat(nbuf, &st) == 0 && 2188 S_ISREG(st.st_mode))) { 2189 if (dout == NULL) { 2190 dout = dataconn("file list", (off_t)-1, 2191 "w"); 2192 if (dout == NULL) 2193 goto out; 2194 transflag++; 2195 } 2196 if (nbuf[0] == '.' && nbuf[1] == '/') 2197 fprintf(dout, "%s%s\n", &nbuf[2], 2198 type == TYPE_A ? "\r" : ""); 2199 else 2200 fprintf(dout, "%s%s\n", nbuf, 2201 type == TYPE_A ? "\r" : ""); 2202 byte_count += strlen(nbuf) + 1; 2203 } 2204 } 2205 (void) closedir(dirp); 2206 } 2207 2208 if (dout == NULL) 2209 reply(550, "No files found."); 2210 else if (ferror(dout) != 0) 2211 perror_reply(550, "Data connection"); 2212 else 2213 reply(226, "Transfer complete."); 2214 2215 transflag = 0; 2216 if (dout != NULL) 2217 (void) fclose(dout); 2218 data = -1; 2219 pdata = -1; 2220out: 2221 if (freeglob) { 2222 freeglob = 0; 2223 globfree(&gl); 2224 } 2225} 2226 2227void 2228reapchild(signo) 2229 int signo; 2230{ 2231 while (wait3(NULL, WNOHANG, NULL) > 0); 2232} 2233 2234#ifdef OLD_SETPROCTITLE 2235/* 2236 * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2237 * Warning, since this is usually started from inetd.conf, it often doesn't 2238 * have much of an environment or arglist to overwrite. 2239 */ 2240void 2241#if __STDC__ 2242setproctitle(const char *fmt, ...) 2243#else 2244setproctitle(fmt, va_alist) 2245 char *fmt; 2246 va_dcl 2247#endif 2248{ 2249 int i; 2250 va_list ap; 2251 char *p, *bp, ch; 2252 char buf[LINE_MAX]; 2253 2254#if __STDC__ 2255 va_start(ap, fmt); 2256#else 2257 va_start(ap); 2258#endif 2259 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2260 2261 /* make ps print our process name */ 2262 p = Argv[0]; 2263 *p++ = '-'; 2264 2265 i = strlen(buf); 2266 if (i > LastArgv - p - 2) { 2267 i = LastArgv - p - 2; 2268 buf[i] = '\0'; 2269 } 2270 bp = buf; 2271 while (ch = *bp++) 2272 if (ch != '\n' && ch != '\r') 2273 *p++ = ch; 2274 while (p < LastArgv) 2275 *p++ = ' '; 2276} 2277#endif /* OLD_SETPROCTITLE */ 2278 2279static void 2280logxfer(name, size, start) 2281 char *name; 2282 long size; 2283 long start; 2284{ 2285 char buf[1024]; 2286 char path[MAXPATHLEN + 1]; 2287 time_t now; 2288 2289 if (statfd >= 0 && getwd(path) != NULL) { 2290 time(&now); 2291 snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n", 2292 ctime(&now)+4, ident, remotehost, 2293 path, name, size, now - start + (now == start)); 2294 write(statfd, buf, strlen(buf)); 2295 } 2296} 2297