1ea022d16SRodney W. Grimes /* 2ea022d16SRodney W. Grimes * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 3ea022d16SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4ea022d16SRodney W. Grimes * 5ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions 7ea022d16SRodney W. Grimes * are met: 8ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13ea022d16SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14ea022d16SRodney W. Grimes * must display the following acknowledgement: 15ea022d16SRodney W. Grimes * This product includes software developed by the University of 16ea022d16SRodney W. Grimes * California, Berkeley and its contributors. 17ea022d16SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 19ea022d16SRodney W. Grimes * without specific prior written permission. 20ea022d16SRodney W. Grimes * 21ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ea022d16SRodney W. Grimes * SUCH DAMAGE. 32ea022d16SRodney W. Grimes */ 33ea022d16SRodney W. Grimes 349aca17cbSMark Murray #if 0 35ea022d16SRodney W. Grimes #ifndef lint 36ea022d16SRodney W. Grimes static char copyright[] = 37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 38ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 39ea022d16SRodney W. Grimes #endif /* not lint */ 409aca17cbSMark Murray #endif 41ea022d16SRodney W. Grimes 42ea022d16SRodney W. Grimes #ifndef lint 43e02897faSPhilippe Charnier #if 0 44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 459aca17cbSMark Murray #endif 46e02897faSPhilippe Charnier static const char rcsid[] = 477f3dea24SPeter Wemm "$FreeBSD$"; 48e02897faSPhilippe Charnier #endif /* not lint */ 49ea022d16SRodney W. Grimes 50ea022d16SRodney W. Grimes /* 51ea022d16SRodney W. Grimes * FTP server. 52ea022d16SRodney W. Grimes */ 53ea022d16SRodney W. Grimes #include <sys/param.h> 54ea022d16SRodney W. Grimes #include <sys/stat.h> 55ea022d16SRodney W. Grimes #include <sys/ioctl.h> 56ea022d16SRodney W. Grimes #include <sys/socket.h> 57ea022d16SRodney W. Grimes #include <sys/wait.h> 589fc5823aSGarrett Wollman #include <sys/mman.h> 59ea022d16SRodney W. Grimes 60ea022d16SRodney W. Grimes #include <netinet/in.h> 61ea022d16SRodney W. Grimes #include <netinet/in_systm.h> 62ea022d16SRodney W. Grimes #include <netinet/ip.h> 639fc5823aSGarrett Wollman #include <netinet/tcp.h> 64ea022d16SRodney W. Grimes 65ea022d16SRodney W. Grimes #define FTP_NAMES 66ea022d16SRodney W. Grimes #include <arpa/ftp.h> 67ea022d16SRodney W. Grimes #include <arpa/inet.h> 68ea022d16SRodney W. Grimes #include <arpa/telnet.h> 69ea022d16SRodney W. Grimes 70ea022d16SRodney W. Grimes #include <ctype.h> 71ea022d16SRodney W. Grimes #include <dirent.h> 72ea022d16SRodney W. Grimes #include <err.h> 73ea022d16SRodney W. Grimes #include <errno.h> 74ea022d16SRodney W. Grimes #include <fcntl.h> 75ea022d16SRodney W. Grimes #include <glob.h> 76ea022d16SRodney W. Grimes #include <limits.h> 77ea022d16SRodney W. Grimes #include <netdb.h> 78ea022d16SRodney W. Grimes #include <pwd.h> 7931fea7b8SDavid Nugent #include <grp.h> 80ea022d16SRodney W. Grimes #include <setjmp.h> 81ea022d16SRodney W. Grimes #include <signal.h> 82ea022d16SRodney W. Grimes #include <stdio.h> 83ea022d16SRodney W. Grimes #include <stdlib.h> 84ea022d16SRodney W. Grimes #include <string.h> 85ea022d16SRodney W. Grimes #include <syslog.h> 86ea022d16SRodney W. Grimes #include <time.h> 87ea022d16SRodney W. Grimes #include <unistd.h> 88b63e1fe2SPeter Wemm #include <libutil.h> 89b071c689SDavid Nugent #ifdef LOGIN_CAP 90b071c689SDavid Nugent #include <login_cap.h> 91b071c689SDavid Nugent #endif 92ea022d16SRodney W. Grimes 932c60c54cSPaul Traina #ifdef SKEY 942c60c54cSPaul Traina #include <skey.h> 952c60c54cSPaul Traina #endif 962c60c54cSPaul Traina 976c9134c0SMark Murray #if !defined(NOPAM) 986c9134c0SMark Murray #include <security/pam_appl.h> 996c9134c0SMark Murray #endif 1006c9134c0SMark Murray 101ea022d16SRodney W. Grimes #include "pathnames.h" 102ea022d16SRodney W. Grimes #include "extern.h" 103ea022d16SRodney W. Grimes 104ea022d16SRodney W. Grimes #if __STDC__ 105ea022d16SRodney W. Grimes #include <stdarg.h> 106ea022d16SRodney W. Grimes #else 107ea022d16SRodney W. Grimes #include <varargs.h> 108ea022d16SRodney W. Grimes #endif 109ea022d16SRodney W. Grimes 110af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 111af85d782SDavid Nugent #undef main 112ea022d16SRodney W. Grimes 113ea022d16SRodney W. Grimes extern off_t restart_point; 114ea022d16SRodney W. Grimes extern char cbuf[]; 115ea022d16SRodney W. Grimes 116cf09a206SDavid Greenman struct sockaddr_in server_addr; 117ea022d16SRodney W. Grimes struct sockaddr_in ctrl_addr; 118ea022d16SRodney W. Grimes struct sockaddr_in data_source; 119ea022d16SRodney W. Grimes struct sockaddr_in data_dest; 120ea022d16SRodney W. Grimes struct sockaddr_in his_addr; 121ea022d16SRodney W. Grimes struct sockaddr_in pasv_addr; 122ea022d16SRodney W. Grimes 123cf09a206SDavid Greenman int daemon_mode; 124ea022d16SRodney W. Grimes int data; 125ea022d16SRodney W. Grimes jmp_buf errcatch, urgcatch; 126ea022d16SRodney W. Grimes int logged_in; 127ea022d16SRodney W. Grimes struct passwd *pw; 128ea022d16SRodney W. Grimes int debug; 129ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 130ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 131ea022d16SRodney W. Grimes int logging; 1324c450ad7SPaul Traina int restricted_data_ports = 1; 133a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1345a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 135ea022d16SRodney W. Grimes int guest; 136a5a4544eSPaul Traina int dochroot; 1373eb568f2SGuido van Rooij int stats; 1383eb568f2SGuido van Rooij int statfd = -1; 139ea022d16SRodney W. Grimes int type; 140ea022d16SRodney W. Grimes int form; 141ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 142ea022d16SRodney W. Grimes int mode; 143ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 144ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 145ea022d16SRodney W. Grimes sig_atomic_t transflag; 146ea022d16SRodney W. Grimes off_t file_size; 147ea022d16SRodney W. Grimes off_t byte_count; 148ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 149ea022d16SRodney W. Grimes #undef CMASK 150ea022d16SRodney W. Grimes #define CMASK 027 151ea022d16SRodney W. Grimes #endif 152ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 153ea022d16SRodney W. Grimes char tmpline[7]; 154ea4e54b9SDavid Nugent char *hostname; 1550512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 156ea4e54b9SDavid Nugent char *ftpuser; 157ea4e54b9SDavid Nugent 158ea4e54b9SDavid Nugent static struct ftphost { 159ea4e54b9SDavid Nugent struct ftphost *next; 160ea4e54b9SDavid Nugent struct in_addr hostaddr; 161ea4e54b9SDavid Nugent char *hostname; 162ea4e54b9SDavid Nugent char *anonuser; 163ea4e54b9SDavid Nugent char *statfile; 164ea4e54b9SDavid Nugent char *welcome; 165ea4e54b9SDavid Nugent char *loginmsg; 166ea4e54b9SDavid Nugent } *thishost, *firsthost; 167ea4e54b9SDavid Nugent 168ea4e54b9SDavid Nugent #endif 1699e9a43bdSBrian Somers char remotehost[MAXHOSTNAMELEN]; 1703eb568f2SGuido van Rooij char *ident = NULL; 171a5a4544eSPaul Traina 172a5a4544eSPaul Traina static char ttyline[20]; 173a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 174a5a4544eSPaul Traina 1756c9134c0SMark Murray #if !defined(NOPAM) 1766c9134c0SMark Murray static int auth_pam __P((struct passwd**, const char*)); 1779aca17cbSMark Murray #endif 1789aca17cbSMark Murray 179105a3c98SJulian Elischer struct in_addr bind_address; 180105a3c98SJulian Elischer char *pid_file = NULL; 181105a3c98SJulian Elischer 182ea022d16SRodney W. Grimes /* 183ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 184ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 185ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 186ea022d16SRodney W. Grimes */ 187ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 188ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 189ea022d16SRodney W. Grimes 190ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 191ea022d16SRodney W. Grimes int swaitint = SWAITINT; 192ea022d16SRodney W. Grimes 193ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 194b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 195ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 196ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 197b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 198ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 199ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 200ea022d16SRodney W. Grimes 201726040deSGuido van Rooij #ifdef SKEY 202726040deSGuido van Rooij int pwok = 0; 2032c60c54cSPaul Traina char addr_string[20]; /* XXX */ 204726040deSGuido van Rooij #endif 205726040deSGuido van Rooij 206ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 207ea022d16SRodney W. Grimes if (logging > 1) \ 208ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 209ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 210ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 211ea022d16SRodney W. Grimes if (logging > 1) \ 212ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 213ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 214ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 215ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 216ea022d16SRodney W. Grimes if (logging > 1) { \ 217ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 218ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 219ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 220ea022d16SRodney W. Grimes else \ 221ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 222ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 223ea022d16SRodney W. Grimes } 224ea022d16SRodney W. Grimes 225ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 226ea4e54b9SDavid Nugent static void inithosts __P((void)); 227ea4e54b9SDavid Nugent static void selecthost __P((struct in_addr *)); 228ea4e54b9SDavid Nugent #endif 229ea022d16SRodney W. Grimes static void ack __P((char *)); 230ea022d16SRodney W. Grimes static void myoob __P((int)); 2317edcb936SSteve Price static int checkuser __P((char *, char *, int)); 232ea022d16SRodney W. Grimes static FILE *dataconn __P((char *, off_t, char *)); 233ea022d16SRodney W. Grimes static void dolog __P((struct sockaddr_in *)); 234ea022d16SRodney W. Grimes static char *curdir __P((void)); 235ea022d16SRodney W. Grimes static void end_login __P((void)); 236ea022d16SRodney W. Grimes static FILE *getdatasock __P((char *)); 237ea022d16SRodney W. Grimes static char *gunique __P((char *)); 238ea022d16SRodney W. Grimes static void lostconn __P((int)); 239ea022d16SRodney W. Grimes static int receive_data __P((FILE *, FILE *)); 2409fc5823aSGarrett Wollman static void send_data __P((FILE *, FILE *, off_t, off_t, int)); 241ea022d16SRodney W. Grimes static struct passwd * 242ea022d16SRodney W. Grimes sgetpwnam __P((char *)); 243ea022d16SRodney W. Grimes static char *sgetsave __P((char *)); 244cf09a206SDavid Greenman static void reapchild __P((int)); 24561f891a6SPaul Traina static void logxfer __P((char *, long, long)); 246ea022d16SRodney W. Grimes 247ea022d16SRodney W. Grimes static char * 248ea022d16SRodney W. Grimes curdir() 249ea022d16SRodney W. Grimes { 250ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 251ea022d16SRodney W. Grimes 252ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 253ea022d16SRodney W. Grimes return (""); 254ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 255ea022d16SRodney W. Grimes strcat(path, "/"); 256ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 257ea022d16SRodney W. Grimes return (guest ? path+1 : path); 258ea022d16SRodney W. Grimes } 259ea022d16SRodney W. Grimes 260ea022d16SRodney W. Grimes int 261ea022d16SRodney W. Grimes main(argc, argv, envp) 262ea022d16SRodney W. Grimes int argc; 263ea022d16SRodney W. Grimes char *argv[]; 264ea022d16SRodney W. Grimes char **envp; 265ea022d16SRodney W. Grimes { 266ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 267ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 268ea022d16SRodney W. Grimes FILE *fd; 269ea022d16SRodney W. Grimes 27034d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 27134d1ba5cSAndrey A. Chernov 272b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 273ea022d16SRodney W. Grimes /* 274ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 275ea022d16SRodney W. Grimes */ 276ea022d16SRodney W. Grimes Argv = argv; 277ea022d16SRodney W. Grimes while (*envp) 278ea022d16SRodney W. Grimes envp++; 279ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 280b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 281ea022d16SRodney W. Grimes 2823eb568f2SGuido van Rooij 283105a3c98SJulian Elischer bind_address.s_addr = htonl(INADDR_ANY); 28491477cc4SWarner Losh while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:")) != -1) { 285ea022d16SRodney W. Grimes switch (ch) { 286cf09a206SDavid Greenman case 'D': 287cf09a206SDavid Greenman daemon_mode++; 288cf09a206SDavid Greenman break; 289cf09a206SDavid Greenman 290ea022d16SRodney W. Grimes case 'd': 291a5a4544eSPaul Traina debug++; 292ea022d16SRodney W. Grimes break; 293ea022d16SRodney W. Grimes 294ea022d16SRodney W. Grimes case 'l': 295ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 296ea022d16SRodney W. Grimes break; 297ea022d16SRodney W. Grimes 298a5a4544eSPaul Traina case 'R': 299a5a4544eSPaul Traina paranoid = 0; 300a5a4544eSPaul Traina break; 301a5a4544eSPaul Traina 302a5a4544eSPaul Traina case 'S': 303a5a4544eSPaul Traina stats++; 304a5a4544eSPaul Traina break; 305a5a4544eSPaul Traina 306a5a4544eSPaul Traina case 'T': 307a5a4544eSPaul Traina maxtimeout = atoi(optarg); 308a5a4544eSPaul Traina if (timeout > maxtimeout) 309a5a4544eSPaul Traina timeout = maxtimeout; 3104c450ad7SPaul Traina break; 3114c450ad7SPaul Traina 312ea022d16SRodney W. Grimes case 't': 313ea022d16SRodney W. Grimes timeout = atoi(optarg); 314ea022d16SRodney W. Grimes if (maxtimeout < timeout) 315ea022d16SRodney W. Grimes maxtimeout = timeout; 316ea022d16SRodney W. Grimes break; 317a5a4544eSPaul Traina 318a5a4544eSPaul Traina case 'U': 319a5a4544eSPaul Traina restricted_data_ports = 0; 320ea022d16SRodney W. Grimes break; 321ea022d16SRodney W. Grimes 322105a3c98SJulian Elischer case 'a': 323105a3c98SJulian Elischer if (!inet_aton(optarg, &bind_address)) 324105a3c98SJulian Elischer errx(1, "invalid address for -a"); 325105a3c98SJulian Elischer break; 326105a3c98SJulian Elischer 327105a3c98SJulian Elischer case 'p': 328105a3c98SJulian Elischer pid_file = optarg; 329105a3c98SJulian Elischer break; 330105a3c98SJulian Elischer 331ea022d16SRodney W. Grimes case 'u': 332ea022d16SRodney W. Grimes { 333ea022d16SRodney W. Grimes long val = 0; 334ea022d16SRodney W. Grimes 335ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 336ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 337ea022d16SRodney W. Grimes warnx("bad value for -u"); 338ea022d16SRodney W. Grimes else 339ea022d16SRodney W. Grimes defumask = val; 340ea022d16SRodney W. Grimes break; 341ea022d16SRodney W. Grimes } 3425a392aecSTorsten Blum case 'A': 3435a392aecSTorsten Blum anon_only = 1; 3445a392aecSTorsten Blum break; 345ea022d16SRodney W. Grimes 346ea022d16SRodney W. Grimes case 'v': 347ea022d16SRodney W. Grimes debug = 1; 348ea022d16SRodney W. Grimes break; 349ea022d16SRodney W. Grimes 350ea022d16SRodney W. Grimes default: 351ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 352ea022d16SRodney W. Grimes break; 353ea022d16SRodney W. Grimes } 354ea022d16SRodney W. Grimes } 355cf09a206SDavid Greenman 356ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 357ea4e54b9SDavid Nugent inithosts(); 358ea4e54b9SDavid Nugent #endif 359ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 360cf09a206SDavid Greenman 361cf09a206SDavid Greenman /* 362cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 363cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 364cf09a206SDavid Greenman */ 365cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 366cf09a206SDavid Greenman 367cf09a206SDavid Greenman if (daemon_mode) { 368cf09a206SDavid Greenman int ctl_sock, fd; 369cf09a206SDavid Greenman struct servent *sv; 370cf09a206SDavid Greenman 371cf09a206SDavid Greenman /* 372cf09a206SDavid Greenman * Detach from parent. 373cf09a206SDavid Greenman */ 374cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 375cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 376cf09a206SDavid Greenman exit(1); 377cf09a206SDavid Greenman } 378cf09a206SDavid Greenman (void) signal(SIGCHLD, reapchild); 379cf09a206SDavid Greenman /* 380cf09a206SDavid Greenman * Get port number for ftp/tcp. 381cf09a206SDavid Greenman */ 382cf09a206SDavid Greenman sv = getservbyname("ftp", "tcp"); 383cf09a206SDavid Greenman if (sv == NULL) { 384cf09a206SDavid Greenman syslog(LOG_ERR, "getservbyname for ftp failed"); 385cf09a206SDavid Greenman exit(1); 386cf09a206SDavid Greenman } 387cf09a206SDavid Greenman /* 388cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 389cf09a206SDavid Greenman * listening. 390cf09a206SDavid Greenman */ 391cf09a206SDavid Greenman ctl_sock = socket(AF_INET, SOCK_STREAM, 0); 392cf09a206SDavid Greenman if (ctl_sock < 0) { 393cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 394cf09a206SDavid Greenman exit(1); 395cf09a206SDavid Greenman } 396cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 397cf09a206SDavid Greenman (char *)&on, sizeof(on)) < 0) 398cf09a206SDavid Greenman syslog(LOG_ERR, "control setsockopt: %m");; 399cf09a206SDavid Greenman server_addr.sin_family = AF_INET; 400105a3c98SJulian Elischer server_addr.sin_addr = bind_address; 401cf09a206SDavid Greenman server_addr.sin_port = sv->s_port; 402cf09a206SDavid Greenman if (bind(ctl_sock, (struct sockaddr *)&server_addr, sizeof(server_addr))) { 403cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 404cf09a206SDavid Greenman exit(1); 405cf09a206SDavid Greenman } 406cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 407cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 408cf09a206SDavid Greenman exit(1); 409cf09a206SDavid Greenman } 410cf09a206SDavid Greenman /* 411105a3c98SJulian Elischer * Atomically write process ID 412105a3c98SJulian Elischer */ 413105a3c98SJulian Elischer if (pid_file) 414105a3c98SJulian Elischer { 415105a3c98SJulian Elischer int fd; 416105a3c98SJulian Elischer char buf[20]; 417105a3c98SJulian Elischer 418105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 419105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 42085966371SWarner Losh if (fd < 0) { 421105a3c98SJulian Elischer if (errno == EAGAIN) 422105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 423105a3c98SJulian Elischer else 424105a3c98SJulian Elischer err(1, "%s", pid_file); 42585966371SWarner Losh } 426105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 427105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 428105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 429105a3c98SJulian Elischer err(1, "%s: write", pid_file); 430105a3c98SJulian Elischer /* Leave the pid file open and locked */ 431105a3c98SJulian Elischer } 432105a3c98SJulian Elischer /* 433cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 434cf09a206SDavid Greenman * children to handle them. 435cf09a206SDavid Greenman */ 436cf09a206SDavid Greenman while (1) { 437cf09a206SDavid Greenman addrlen = sizeof(his_addr); 438cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 439cf09a206SDavid Greenman if (fork() == 0) { 440cf09a206SDavid Greenman /* child */ 441cf09a206SDavid Greenman (void) dup2(fd, 0); 442cf09a206SDavid Greenman (void) dup2(fd, 1); 443cf09a206SDavid Greenman close(ctl_sock); 444cf09a206SDavid Greenman break; 445cf09a206SDavid Greenman } 446cf09a206SDavid Greenman close(fd); 447cf09a206SDavid Greenman } 448cf09a206SDavid Greenman } else { 449cf09a206SDavid Greenman addrlen = sizeof(his_addr); 450cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 451cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 452cf09a206SDavid Greenman exit(1); 453cf09a206SDavid Greenman } 454cf09a206SDavid Greenman } 455cf09a206SDavid Greenman 456ea022d16SRodney W. Grimes (void) signal(SIGCHLD, SIG_IGN); 457cf09a206SDavid Greenman (void) signal(SIGPIPE, lostconn); 458158a00b2SJohn Birrell if (signal(SIGURG, myoob) == SIG_ERR) 459ea022d16SRodney W. Grimes syslog(LOG_ERR, "signal: %m"); 460ea022d16SRodney W. Grimes 461cf09a206SDavid Greenman #ifdef SKEY 46261f891a6SPaul Traina strncpy(addr_string, inet_ntoa(his_addr.sin_addr), sizeof(addr_string)); 463cf09a206SDavid Greenman #endif 464cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 465cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 466cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 467cf09a206SDavid Greenman exit(1); 468cf09a206SDavid Greenman } 469ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 470ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 471ea4e54b9SDavid Nugent selecthost(&ctrl_addr.sin_addr); 472ea4e54b9SDavid Nugent #endif 473cf09a206SDavid Greenman #ifdef IP_TOS 474cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 475cf09a206SDavid Greenman if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 476cf09a206SDavid Greenman syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 477cf09a206SDavid Greenman #endif 478dadb9fb3SDavid Greenman /* 479dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 480dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 481dadb9fb3SDavid Greenman */ 482dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 483dadb9fb3SDavid Greenman syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m"); 484dadb9fb3SDavid Greenman 485cf09a206SDavid Greenman data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 486cf09a206SDavid Greenman 487a5a4544eSPaul Traina /* set this here so klogin can use it... */ 488e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 489a5a4544eSPaul Traina 490ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 491ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 492ea022d16SRodney W. Grimes if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 493ea022d16SRodney W. Grimes syslog(LOG_ERR, "setsockopt: %m"); 494ea022d16SRodney W. Grimes #endif 495ea022d16SRodney W. Grimes 496ea022d16SRodney W. Grimes #ifdef F_SETOWN 497ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 498ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 499ea022d16SRodney W. Grimes #endif 500ea022d16SRodney W. Grimes dolog(&his_addr); 501ea022d16SRodney W. Grimes /* 502ea022d16SRodney W. Grimes * Set up default state 503ea022d16SRodney W. Grimes */ 504ea022d16SRodney W. Grimes data = -1; 505ea022d16SRodney W. Grimes type = TYPE_A; 506ea022d16SRodney W. Grimes form = FORM_N; 507ea022d16SRodney W. Grimes stru = STRU_F; 508ea022d16SRodney W. Grimes mode = MODE_S; 509ea022d16SRodney W. Grimes tmpline[0] = '\0'; 510ea022d16SRodney W. Grimes 511ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 512ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 513ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 514ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 515ea022d16SRodney W. Grimes *cp = '\0'; 516ea022d16SRodney W. Grimes lreply(530, "%s", line); 517ea022d16SRodney W. Grimes } 518ea022d16SRodney W. Grimes (void) fflush(stdout); 519ea022d16SRodney W. Grimes (void) fclose(fd); 520ea022d16SRodney W. Grimes reply(530, "System not available."); 521ea022d16SRodney W. Grimes exit(0); 522ea022d16SRodney W. Grimes } 523ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 524ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 525ea4e54b9SDavid Nugent #else 526ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 527ea4e54b9SDavid Nugent #endif 528ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 529ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 530ea022d16SRodney W. Grimes *cp = '\0'; 531ea022d16SRodney W. Grimes lreply(220, "%s", line); 532ea022d16SRodney W. Grimes } 533ea022d16SRodney W. Grimes (void) fflush(stdout); 534ea022d16SRodney W. Grimes (void) fclose(fd); 535ea022d16SRodney W. Grimes /* reply(220,) must follow */ 536ea022d16SRodney W. Grimes } 537ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 5380512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 5390512556aSDavid Nugent fatal("Ran out of memory."); 5409e9a43bdSBrian Somers (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 5419e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 542ea4e54b9SDavid Nugent #endif 543ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 544ea022d16SRodney W. Grimes (void) setjmp(errcatch); 545ea022d16SRodney W. Grimes for (;;) 546ea022d16SRodney W. Grimes (void) yyparse(); 547ea022d16SRodney W. Grimes /* NOTREACHED */ 548ea022d16SRodney W. Grimes } 549ea022d16SRodney W. Grimes 550ea022d16SRodney W. Grimes static void 551ea022d16SRodney W. Grimes lostconn(signo) 552ea022d16SRodney W. Grimes int signo; 553ea022d16SRodney W. Grimes { 554ea022d16SRodney W. Grimes 555ea022d16SRodney W. Grimes if (debug) 556ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 557e02897faSPhilippe Charnier dologout(1); 558ea022d16SRodney W. Grimes } 559ea022d16SRodney W. Grimes 560ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 561ea4e54b9SDavid Nugent /* 562ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 563ea4e54b9SDavid Nugent */ 564ea4e54b9SDavid Nugent 565ea4e54b9SDavid Nugent static void 566ea4e54b9SDavid Nugent inithosts() 567ea4e54b9SDavid Nugent { 568ea4e54b9SDavid Nugent FILE *fp; 569ea4e54b9SDavid Nugent char *cp; 570ea4e54b9SDavid Nugent struct hostent *hp; 571ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 572ea4e54b9SDavid Nugent char line[1024]; 573ea4e54b9SDavid Nugent 574ea4e54b9SDavid Nugent /* 575ea4e54b9SDavid Nugent * Fill in the default host information 576ea4e54b9SDavid Nugent */ 577ea4e54b9SDavid Nugent if (gethostname(line, sizeof(line)) < 0) 578ea4e54b9SDavid Nugent line[0] = '\0'; 579ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL || 580ea4e54b9SDavid Nugent (hrp->hostname = strdup(line)) == NULL) 581ea4e54b9SDavid Nugent fatal("Ran out of memory."); 582ea4e54b9SDavid Nugent memset(&hrp->hostaddr, 0, sizeof hrp->hostaddr); 583ea4e54b9SDavid Nugent if ((hp = gethostbyname(hrp->hostname)) != NULL) 584ea4e54b9SDavid Nugent (void) memcpy(&hrp->hostaddr, 585ea4e54b9SDavid Nugent hp->h_addr_list[0], 586ea4e54b9SDavid Nugent sizeof(hrp->hostaddr)); 587ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 588ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 589ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 590ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 591ea4e54b9SDavid Nugent hrp->next = NULL; 592ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 593ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 594ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL) { 595ea4e54b9SDavid Nugent int i; 596ea4e54b9SDavid Nugent 597ea4e54b9SDavid Nugent if ((cp = strchr(line, '\n')) == NULL) { 598ea4e54b9SDavid Nugent /* ignore long lines */ 599ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL && 600ea4e54b9SDavid Nugent strchr(line, '\n') == NULL) 601ea4e54b9SDavid Nugent ; 602ea4e54b9SDavid Nugent continue; 603ea4e54b9SDavid Nugent } 604ea4e54b9SDavid Nugent *cp = '\0'; 605ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 606ea4e54b9SDavid Nugent /* skip comments and empty lines */ 607ea4e54b9SDavid Nugent if (cp == NULL || line[0] == '#') 608ea4e54b9SDavid Nugent continue; 609ea4e54b9SDavid Nugent /* first, try a standard gethostbyname() */ 610ea4e54b9SDavid Nugent if ((hp = gethostbyname(cp)) == NULL) 611ea4e54b9SDavid Nugent continue; 612ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 613ea4e54b9SDavid Nugent if (memcmp(&hrp->hostaddr, 614ea4e54b9SDavid Nugent hp->h_addr_list[0], 615ea4e54b9SDavid Nugent sizeof(hrp->hostaddr)) == 0) 616ea4e54b9SDavid Nugent break; 617ea4e54b9SDavid Nugent } 618ea4e54b9SDavid Nugent if (hrp == NULL) { 619ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 620ea4e54b9SDavid Nugent continue; 621ea4e54b9SDavid Nugent /* defaults */ 622ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 623ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 624ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 625ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 626ea4e54b9SDavid Nugent hrp->next = NULL; 627ea4e54b9SDavid Nugent lhrp->next = hrp; 628ea4e54b9SDavid Nugent lhrp = hrp; 629ea4e54b9SDavid Nugent } 630ea4e54b9SDavid Nugent (void) memcpy(&hrp->hostaddr, 631ea4e54b9SDavid Nugent hp->h_addr_list[0], 632ea4e54b9SDavid Nugent sizeof(hrp->hostaddr)); 633ea4e54b9SDavid Nugent /* 634ea4e54b9SDavid Nugent * determine hostname to use. 635ea4e54b9SDavid Nugent * force defined name if it is a valid alias 636ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 637ea4e54b9SDavid Nugent */ 638ea4e54b9SDavid Nugent if ((hp = gethostbyaddr((char*)&hrp->hostaddr, 639ea4e54b9SDavid Nugent sizeof(hrp->hostaddr), 640ea4e54b9SDavid Nugent AF_INET)) != NULL) { 641ea4e54b9SDavid Nugent if (strcmp(cp, hp->h_name) != 0) { 642ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 643ea4e54b9SDavid Nugent cp = hp->h_name; 644ea4e54b9SDavid Nugent else { 645ea4e54b9SDavid Nugent i = 0; 646ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 647ea4e54b9SDavid Nugent strcmp(cp, hp->h_aliases[i]) != 0) 648ea4e54b9SDavid Nugent ++i; 649ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 650ea4e54b9SDavid Nugent cp = hp->h_name; 651ea4e54b9SDavid Nugent } 652ea4e54b9SDavid Nugent } 653ea4e54b9SDavid Nugent } 654ea4e54b9SDavid Nugent hrp->hostname = strdup(cp); 655ea4e54b9SDavid Nugent /* ok, now we now peel off the rest */ 656ea4e54b9SDavid Nugent i = 0; 657ea4e54b9SDavid Nugent while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) { 658ea4e54b9SDavid Nugent if (*cp != '-' && (cp = strdup(cp)) != NULL) { 659ea4e54b9SDavid Nugent switch (i) { 660ea4e54b9SDavid Nugent case 0: /* anon user permissions */ 661ea4e54b9SDavid Nugent hrp->anonuser = cp; 662ea4e54b9SDavid Nugent break; 663ea4e54b9SDavid Nugent case 1: /* statistics file */ 664ea4e54b9SDavid Nugent hrp->statfile = cp; 665ea4e54b9SDavid Nugent break; 666ea4e54b9SDavid Nugent case 2: /* welcome message */ 667ea4e54b9SDavid Nugent hrp->welcome = cp; 668ea4e54b9SDavid Nugent break; 669ea4e54b9SDavid Nugent case 3: /* login message */ 670ea4e54b9SDavid Nugent hrp->loginmsg = cp; 671ea4e54b9SDavid Nugent break; 672ea4e54b9SDavid Nugent } 673ea4e54b9SDavid Nugent } 674ea4e54b9SDavid Nugent ++i; 675ea4e54b9SDavid Nugent } 676ea4e54b9SDavid Nugent } 677ea4e54b9SDavid Nugent (void) fclose(fp); 678ea4e54b9SDavid Nugent } 679ea4e54b9SDavid Nugent } 680ea4e54b9SDavid Nugent 681ea4e54b9SDavid Nugent static void 682ea4e54b9SDavid Nugent selecthost(a) 683ea4e54b9SDavid Nugent struct in_addr *a; 684ea4e54b9SDavid Nugent { 685ea4e54b9SDavid Nugent struct ftphost *hrp; 686ea4e54b9SDavid Nugent 687ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 688ea4e54b9SDavid Nugent while (hrp != NULL) { 689ea4e54b9SDavid Nugent if (memcmp(a, &hrp->hostaddr, sizeof(hrp->hostaddr)) == 0) { 690ea4e54b9SDavid Nugent thishost = hrp; 691ea4e54b9SDavid Nugent break; 692ea4e54b9SDavid Nugent } 693ea4e54b9SDavid Nugent hrp = hrp->next; 694ea4e54b9SDavid Nugent } 695ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 696ea4e54b9SDavid Nugent hostname = thishost->hostname; 697ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 698ea4e54b9SDavid Nugent } 699ea4e54b9SDavid Nugent #endif 700ea4e54b9SDavid Nugent 701ea022d16SRodney W. Grimes /* 702ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 703ea022d16SRodney W. Grimes */ 704ea022d16SRodney W. Grimes static char * 705ea022d16SRodney W. Grimes sgetsave(s) 706ea022d16SRodney W. Grimes char *s; 707ea022d16SRodney W. Grimes { 708ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 709ea022d16SRodney W. Grimes 710ea022d16SRodney W. Grimes if (new == NULL) { 711ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 712ea022d16SRodney W. Grimes dologout(1); 713ea022d16SRodney W. Grimes /* NOTREACHED */ 714ea022d16SRodney W. Grimes } 715ea022d16SRodney W. Grimes (void) strcpy(new, s); 716ea022d16SRodney W. Grimes return (new); 717ea022d16SRodney W. Grimes } 718ea022d16SRodney W. Grimes 719ea022d16SRodney W. Grimes /* 720ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 721ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 722ea022d16SRodney W. Grimes * (e.g., globbing). 723ea022d16SRodney W. Grimes */ 724ea022d16SRodney W. Grimes static struct passwd * 725ea022d16SRodney W. Grimes sgetpwnam(name) 726ea022d16SRodney W. Grimes char *name; 727ea022d16SRodney W. Grimes { 728ea022d16SRodney W. Grimes static struct passwd save; 729ea022d16SRodney W. Grimes struct passwd *p; 730ea022d16SRodney W. Grimes 731ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 732ea022d16SRodney W. Grimes return (p); 733ea022d16SRodney W. Grimes if (save.pw_name) { 734ea022d16SRodney W. Grimes free(save.pw_name); 735ea022d16SRodney W. Grimes free(save.pw_passwd); 736ea022d16SRodney W. Grimes free(save.pw_gecos); 737ea022d16SRodney W. Grimes free(save.pw_dir); 738ea022d16SRodney W. Grimes free(save.pw_shell); 739ea022d16SRodney W. Grimes } 740ea022d16SRodney W. Grimes save = *p; 741ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 742ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 743ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 744ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 745ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 746ea022d16SRodney W. Grimes return (&save); 747ea022d16SRodney W. Grimes } 748ea022d16SRodney W. Grimes 749ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 750ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 751ea022d16SRodney W. Grimes static char curname[10]; /* current USER name */ 752ea022d16SRodney W. Grimes 753ea022d16SRodney W. Grimes /* 754ea022d16SRodney W. Grimes * USER command. 755ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 756ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 757ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 758ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 759ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 760ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 761ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 762ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 763ea022d16SRodney W. Grimes */ 764ea022d16SRodney W. Grimes void 765ea022d16SRodney W. Grimes user(name) 766ea022d16SRodney W. Grimes char *name; 767ea022d16SRodney W. Grimes { 768ea022d16SRodney W. Grimes char *cp, *shell; 769ea022d16SRodney W. Grimes 770ea022d16SRodney W. Grimes if (logged_in) { 771ea022d16SRodney W. Grimes if (guest) { 772ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 773ea022d16SRodney W. Grimes return; 774a5a4544eSPaul Traina } else if (dochroot) { 775a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 776a5a4544eSPaul Traina return; 777ea022d16SRodney W. Grimes } 778ea022d16SRodney W. Grimes end_login(); 779ea022d16SRodney W. Grimes } 780ea022d16SRodney W. Grimes 781ea022d16SRodney W. Grimes guest = 0; 782ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 7837edcb936SSteve Price if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 7847edcb936SSteve Price checkuser(_PATH_FTPUSERS, "anonymous", 0)) 785ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 786ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 787ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 788ea4e54b9SDavid Nugent #else 789ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 790ea4e54b9SDavid Nugent #endif 791ea022d16SRodney W. Grimes guest = 1; 792ea022d16SRodney W. Grimes askpasswd = 1; 793ea022d16SRodney W. Grimes reply(331, 7942c60c54cSPaul Traina "Guest login ok, send your email address as password."); 795ea022d16SRodney W. Grimes } else 796ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 797ea022d16SRodney W. Grimes if (!askpasswd && logging) 798ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 799ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 800ea022d16SRodney W. Grimes return; 801ea022d16SRodney W. Grimes } 8025a392aecSTorsten Blum if (anon_only != 0) { 8035a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 8045a392aecSTorsten Blum return; 8055a392aecSTorsten Blum } 8065a392aecSTorsten Blum 8079aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 808ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 809ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 810ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 811ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 812ea022d16SRodney W. Grimes break; 813ea022d16SRodney W. Grimes endusershell(); 814ea022d16SRodney W. Grimes 8157edcb936SSteve Price if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 816ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 817ea022d16SRodney W. Grimes if (logging) 818ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 819ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 820ea022d16SRodney W. Grimes remotehost, name); 821ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 822ea022d16SRodney W. Grimes return; 823ea022d16SRodney W. Grimes } 824ea022d16SRodney W. Grimes } 825ea022d16SRodney W. Grimes if (logging) 826ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 827726040deSGuido van Rooij #ifdef SKEY 8282c60c54cSPaul Traina pwok = skeyaccess(name, NULL, remotehost, addr_string); 82943658eacSAndrey A. Chernov reply(331, "%s", skey_challenge(name, pw, pwok)); 830726040deSGuido van Rooij #else 831ea022d16SRodney W. Grimes reply(331, "Password required for %s.", name); 832726040deSGuido van Rooij #endif 833ea022d16SRodney W. Grimes askpasswd = 1; 834ea022d16SRodney W. Grimes /* 835ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 836ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 837ea022d16SRodney W. Grimes */ 838ea022d16SRodney W. Grimes if (login_attempts) 839ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 840ea022d16SRodney W. Grimes } 841ea022d16SRodney W. Grimes 842ea022d16SRodney W. Grimes /* 843a5a4544eSPaul Traina * Check if a user is in the file "fname" 844ea022d16SRodney W. Grimes */ 845ea022d16SRodney W. Grimes static int 8467edcb936SSteve Price checkuser(fname, name, pwset) 847a5a4544eSPaul Traina char *fname; 848ea022d16SRodney W. Grimes char *name; 8497edcb936SSteve Price int pwset; 850ea022d16SRodney W. Grimes { 851ea022d16SRodney W. Grimes FILE *fd; 852ea022d16SRodney W. Grimes int found = 0; 853ea022d16SRodney W. Grimes char *p, line[BUFSIZ]; 854ea022d16SRodney W. Grimes 855a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 85631fea7b8SDavid Nugent while (!found && fgets(line, sizeof(line), fd) != NULL) 857ea022d16SRodney W. Grimes if ((p = strchr(line, '\n')) != NULL) { 858ea022d16SRodney W. Grimes *p = '\0'; 859ea022d16SRodney W. Grimes if (line[0] == '#') 860ea022d16SRodney W. Grimes continue; 86131fea7b8SDavid Nugent /* 86231fea7b8SDavid Nugent * if first chr is '@', check group membership 86331fea7b8SDavid Nugent */ 86431fea7b8SDavid Nugent if (line[0] == '@') { 86531fea7b8SDavid Nugent int i = 0; 86631fea7b8SDavid Nugent struct group *grp; 86731fea7b8SDavid Nugent 86831fea7b8SDavid Nugent if ((grp = getgrnam(line+1)) == NULL) 86931fea7b8SDavid Nugent continue; 8707edcb936SSteve Price /* 8717edcb936SSteve Price * Check user's default group 8727edcb936SSteve Price */ 8737edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 8747edcb936SSteve Price found = 1; 8757edcb936SSteve Price /* 8767edcb936SSteve Price * Check supplementary groups 8777edcb936SSteve Price */ 87831fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 87931fea7b8SDavid Nugent found = strcmp(name, 88031fea7b8SDavid Nugent grp->gr_mem[i++]) 88131fea7b8SDavid Nugent == 0; 882ea022d16SRodney W. Grimes } 88331fea7b8SDavid Nugent /* 88431fea7b8SDavid Nugent * Otherwise, just check for username match 88531fea7b8SDavid Nugent */ 88631fea7b8SDavid Nugent else 88731fea7b8SDavid Nugent found = strcmp(line, name) == 0; 888ea022d16SRodney W. Grimes } 889ea022d16SRodney W. Grimes (void) fclose(fd); 890ea022d16SRodney W. Grimes } 891ea022d16SRodney W. Grimes return (found); 892ea022d16SRodney W. Grimes } 893ea022d16SRodney W. Grimes 894ea022d16SRodney W. Grimes /* 895ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 896ea022d16SRodney W. Grimes * used when USER command is given or login fails. 897ea022d16SRodney W. Grimes */ 898ea022d16SRodney W. Grimes static void 899ea022d16SRodney W. Grimes end_login() 900ea022d16SRodney W. Grimes { 901ea022d16SRodney W. Grimes 902ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 903ea022d16SRodney W. Grimes if (logged_in) 904986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 905ea022d16SRodney W. Grimes pw = NULL; 906b071c689SDavid Nugent #ifdef LOGIN_CAP 907b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 908b071c689SDavid Nugent LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 909b071c689SDavid Nugent #endif 910ea022d16SRodney W. Grimes logged_in = 0; 911ea022d16SRodney W. Grimes guest = 0; 912a5a4544eSPaul Traina dochroot = 0; 913ea022d16SRodney W. Grimes } 914ea022d16SRodney W. Grimes 9156c9134c0SMark Murray #if !defined(NOPAM) 9166c9134c0SMark Murray 9176c9134c0SMark Murray /* 9186c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 9196c9134c0SMark Murray * login.c 9206c9134c0SMark Murray */ 9216c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 9226c9134c0SMark Murray 9236c9134c0SMark Murray struct cred_t { 9246c9134c0SMark Murray const char *uname; /* user name */ 9256c9134c0SMark Murray const char *pass; /* password */ 9266c9134c0SMark Murray }; 9276c9134c0SMark Murray typedef struct cred_t cred_t; 9286c9134c0SMark Murray 9296c9134c0SMark Murray static int 9306c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 9316c9134c0SMark Murray struct pam_response **resp, void *appdata) 9326c9134c0SMark Murray { 9336c9134c0SMark Murray int i; 9346c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 9356c9134c0SMark Murray struct pam_response *reply = 9366c9134c0SMark Murray malloc(sizeof(struct pam_response) * num_msg); 9376c9134c0SMark Murray 9386c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 9396c9134c0SMark Murray switch (msg[i]->msg_style) { 9406c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 9416c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 9426c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 9436c9134c0SMark Murray /* PAM frees resp. */ 9446c9134c0SMark Murray break; 9456c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 9466c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 9476c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 9486c9134c0SMark Murray /* PAM frees resp. */ 9496c9134c0SMark Murray break; 9506c9134c0SMark Murray case PAM_TEXT_INFO: 9516c9134c0SMark Murray case PAM_ERROR_MSG: 9526c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 9536c9134c0SMark Murray reply[i].resp = NULL; 9546c9134c0SMark Murray break; 9556c9134c0SMark Murray default: /* unknown message style */ 9566c9134c0SMark Murray free(reply); 9576c9134c0SMark Murray return PAM_CONV_ERR; 9586c9134c0SMark Murray } 9596c9134c0SMark Murray } 9606c9134c0SMark Murray 9616c9134c0SMark Murray *resp = reply; 9626c9134c0SMark Murray return PAM_SUCCESS; 9636c9134c0SMark Murray } 9646c9134c0SMark Murray 9656c9134c0SMark Murray /* 9666c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 9676c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 9686c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 9696c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 9706c9134c0SMark Murray * fall back to a different authentication mechanism. 9716c9134c0SMark Murray */ 9726c9134c0SMark Murray static int 9736c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 9746c9134c0SMark Murray { 9756c9134c0SMark Murray pam_handle_t *pamh = NULL; 9766c9134c0SMark Murray const char *tmpl_user; 9776c9134c0SMark Murray const void *item; 9786c9134c0SMark Murray int rval; 9796c9134c0SMark Murray int e; 9806c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 9816c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 9826c9134c0SMark Murray 9836c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 9846c9134c0SMark Murray if (e != PAM_SUCCESS) { 9856c9134c0SMark Murray syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 9866c9134c0SMark Murray return -1; 9876c9134c0SMark Murray } 9886c9134c0SMark Murray 9896c9134c0SMark Murray e = pam_authenticate(pamh, 0); 9906c9134c0SMark Murray switch (e) { 9916c9134c0SMark Murray case PAM_SUCCESS: 9926c9134c0SMark Murray /* 9936c9134c0SMark Murray * With PAM we support the concept of a "template" 9946c9134c0SMark Murray * user. The user enters a login name which is 9956c9134c0SMark Murray * authenticated by PAM, usually via a remote service 9966c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 9976c9134c0SMark Murray * succeeds, a different but related "template" name 9986c9134c0SMark Murray * is used for setting the credentials, shell, and 9996c9134c0SMark Murray * home directory. The name the user enters need only 10006c9134c0SMark Murray * exist on the remote authentication server, but the 10016c9134c0SMark Murray * template name must be present in the local password 10026c9134c0SMark Murray * database. 10036c9134c0SMark Murray * 10046c9134c0SMark Murray * This is supported by two various mechanisms in the 10056c9134c0SMark Murray * individual modules. However, from the application's 10066c9134c0SMark Murray * point of view, the template user is always passed 10076c9134c0SMark Murray * back as a changed value of the PAM_USER item. 10086c9134c0SMark Murray */ 10096c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 10106c9134c0SMark Murray PAM_SUCCESS) { 10116c9134c0SMark Murray tmpl_user = (const char *) item; 10126c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 10136c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 10146c9134c0SMark Murray } else 10156c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 10166c9134c0SMark Murray pam_strerror(pamh, e)); 10176c9134c0SMark Murray rval = 0; 10186c9134c0SMark Murray break; 10196c9134c0SMark Murray 10206c9134c0SMark Murray case PAM_AUTH_ERR: 10216c9134c0SMark Murray case PAM_USER_UNKNOWN: 10226c9134c0SMark Murray case PAM_MAXTRIES: 10236c9134c0SMark Murray rval = 1; 10246c9134c0SMark Murray break; 10256c9134c0SMark Murray 10266c9134c0SMark Murray default: 10276c9134c0SMark Murray syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e)); 10286c9134c0SMark Murray rval = -1; 10296c9134c0SMark Murray break; 10306c9134c0SMark Murray } 10316c9134c0SMark Murray 10326c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 10336c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 10346c9134c0SMark Murray rval = -1; 10356c9134c0SMark Murray } 10366c9134c0SMark Murray return rval; 10376c9134c0SMark Murray } 10386c9134c0SMark Murray 10396c9134c0SMark Murray #endif /* !defined(NOPAM) */ 10406c9134c0SMark Murray 1041ea022d16SRodney W. Grimes void 1042ea022d16SRodney W. Grimes pass(passwd) 1043ea022d16SRodney W. Grimes char *passwd; 1044ea022d16SRodney W. Grimes { 1045a5a4544eSPaul Traina int rval; 1046ea022d16SRodney W. Grimes FILE *fd; 1047b071c689SDavid Nugent #ifdef LOGIN_CAP 1048b071c689SDavid Nugent login_cap_t *lc = NULL; 1049b071c689SDavid Nugent #endif 1050ea022d16SRodney W. Grimes 1051ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1052ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1053ea022d16SRodney W. Grimes return; 1054ea022d16SRodney W. Grimes } 1055ea022d16SRodney W. Grimes askpasswd = 0; 1056ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1057a5a4544eSPaul Traina if (pw == NULL) { 1058a5a4544eSPaul Traina rval = 1; /* failure below */ 1059a5a4544eSPaul Traina goto skip; 1060a5a4544eSPaul Traina } 10616c9134c0SMark Murray #if !defined(NOPAM) 10626c9134c0SMark Murray rval = auth_pam(&pw, passwd); 10636c9134c0SMark Murray if (rval >= 0) 1064a5a4544eSPaul Traina goto skip; 1065a5a4544eSPaul Traina #endif 1066726040deSGuido van Rooij #ifdef SKEY 1067a5a4544eSPaul Traina rval = strcmp(skey_crypt(passwd, pw->pw_passwd, pw, pwok), 10680bb6e9edSPoul-Henning Kamp pw->pw_passwd); 1069bb56d435SPaul Traina pwok = 0; 1070726040deSGuido van Rooij #else 10713cde2031SPoul-Henning Kamp rval = strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd); 1072726040deSGuido van Rooij #endif 1073ea022d16SRodney W. Grimes /* The strcmp does not catch null passwords! */ 1074a5a4544eSPaul Traina if (*pw->pw_passwd == '\0' || 1075a5a4544eSPaul Traina (pw->pw_expire && time(NULL) >= pw->pw_expire)) 1076a5a4544eSPaul Traina rval = 1; /* failure */ 1077a5a4544eSPaul Traina skip: 1078a5a4544eSPaul Traina /* 1079a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 10806c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1081a5a4544eSPaul Traina * succeeded. 1082a5a4544eSPaul Traina */ 1083a5a4544eSPaul Traina if (rval) { 1084ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1085ea022d16SRodney W. Grimes if (logging) 1086ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1087ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1088ea022d16SRodney W. Grimes remotehost, curname); 1089ea022d16SRodney W. Grimes pw = NULL; 1090ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1091ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1092ea022d16SRodney W. Grimes "repeated login failures from %s", 1093ea022d16SRodney W. Grimes remotehost); 1094ea022d16SRodney W. Grimes exit(0); 1095ea022d16SRodney W. Grimes } 1096ea022d16SRodney W. Grimes return; 1097ea022d16SRodney W. Grimes } 1098ea022d16SRodney W. Grimes } 1099ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1100ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 1101ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1102ea022d16SRodney W. Grimes return; 1103ea022d16SRodney W. Grimes } 1104b071c689SDavid Nugent /* May be overridden by login.conf */ 1105b071c689SDavid Nugent (void) umask(defumask); 1106b071c689SDavid Nugent #ifdef LOGIN_CAP 11075d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 1108b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 1109b071c689SDavid Nugent 1110b071c689SDavid Nugent strncpy(remote_ip, inet_ntoa(his_addr.sin_addr), 1111b071c689SDavid Nugent sizeof(remote_ip) - 1); 1112b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1113b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1114b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1115b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1116b071c689SDavid Nugent pw->pw_name); 1117b071c689SDavid Nugent reply(530, "Permission denied.\n"); 1118b071c689SDavid Nugent pw = NULL; 1119b071c689SDavid Nugent return; 1120b071c689SDavid Nugent } 1121b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1122b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1123b071c689SDavid Nugent pw = NULL; 1124b071c689SDavid Nugent return; 1125b071c689SDavid Nugent } 1126b071c689SDavid Nugent } 1127b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1128e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1129e6fa0d43SDag-Erling Smørgrav LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1130b071c689SDavid Nugent #else 1131e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1132ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1133b071c689SDavid Nugent #endif 1134ea022d16SRodney W. Grimes 1135ea022d16SRodney W. Grimes /* open wtmp before chroot */ 1136986a1172SThomas Gellekum ftpd_logwtmp(ttyline, pw->pw_name, remotehost); 1137ea022d16SRodney W. Grimes logged_in = 1; 1138ea022d16SRodney W. Grimes 1139a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1140ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1141ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1142ea4e54b9SDavid Nugent #else 11433eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1144ea4e54b9SDavid Nugent #endif 11453eb568f2SGuido van Rooij stats = 0; 11463eb568f2SGuido van Rooij 1147b071c689SDavid Nugent dochroot = 1148b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1149b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 1150b071c689SDavid Nugent #endif 11517edcb936SSteve Price checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1152ea022d16SRodney W. Grimes if (guest) { 1153ea022d16SRodney W. Grimes /* 1154ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 1155ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 1156ea022d16SRodney W. Grimes * outside the new root! 1157ea022d16SRodney W. Grimes */ 1158ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1159ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 1160ea022d16SRodney W. Grimes goto bad; 1161ea022d16SRodney W. Grimes } 1162a5a4544eSPaul Traina } else if (dochroot) { 1163a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1164a5a4544eSPaul Traina reply(550, "Can't change root."); 1165a5a4544eSPaul Traina goto bad; 1166a5a4544eSPaul Traina } 1167ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 1168ea022d16SRodney W. Grimes if (chdir("/") < 0) { 1169ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 1170ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 1171ea022d16SRodney W. Grimes goto bad; 1172ea022d16SRodney W. Grimes } else 1173ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 1174ea022d16SRodney W. Grimes } 1175ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1176ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1177ea022d16SRodney W. Grimes goto bad; 1178ea022d16SRodney W. Grimes } 117982c76939SDavid Greenman 118082c76939SDavid Greenman /* 1181ea022d16SRodney W. Grimes * Display a login message, if it exists. 1182ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1183ea022d16SRodney W. Grimes */ 1184ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1185ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1186ea4e54b9SDavid Nugent #else 1187ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1188ea4e54b9SDavid Nugent #endif 1189ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1190ea022d16SRodney W. Grimes 1191ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1192ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1193ea022d16SRodney W. Grimes *cp = '\0'; 1194ea022d16SRodney W. Grimes lreply(230, "%s", line); 1195ea022d16SRodney W. Grimes } 1196ea022d16SRodney W. Grimes (void) fflush(stdout); 1197ea022d16SRodney W. Grimes (void) fclose(fd); 1198ea022d16SRodney W. Grimes } 1199ea022d16SRodney W. Grimes if (guest) { 12003eb568f2SGuido van Rooij if (ident != NULL) 12013eb568f2SGuido van Rooij free(ident); 120261f891a6SPaul Traina ident = strdup(passwd); 120361f891a6SPaul Traina if (ident == NULL) 120461f891a6SPaul Traina fatal("Ran out of memory."); 120561f891a6SPaul Traina 1206ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1207ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1208ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1209ea4e54b9SDavid Nugent if (thishost != firsthost) 1210ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1211ea4e54b9SDavid Nugent "%s: anonymous(%s)/%.*s", remotehost, hostname, 12126c9134c0SMark Murray (int)(sizeof(proctitle) - sizeof(remotehost) - 12136c9134c0SMark Murray sizeof(": anonymous/")), passwd); 1214ea4e54b9SDavid Nugent else 1215ea4e54b9SDavid Nugent #endif 1216ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1217ea022d16SRodney W. Grimes "%s: anonymous/%.*s", remotehost, 12186c9134c0SMark Murray (int)(sizeof(proctitle) - sizeof(remotehost) - 12196c9134c0SMark Murray sizeof(": anonymous/")), passwd); 1220b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1221ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1222ea022d16SRodney W. Grimes if (logging) 1223ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1224ea022d16SRodney W. Grimes remotehost, passwd); 1225ea022d16SRodney W. Grimes } else { 12263401a71fSDaniel O'Callaghan if (dochroot) 12273401a71fSDaniel O'Callaghan reply(230, "User %s logged in, access restrictions apply.", 12283401a71fSDaniel O'Callaghan pw->pw_name); 12293401a71fSDaniel O'Callaghan else 1230ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 12313401a71fSDaniel O'Callaghan 1232ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1233ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1234ea022d16SRodney W. Grimes "%s: %s", remotehost, pw->pw_name); 1235b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1236ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1237ea022d16SRodney W. Grimes if (logging) 1238ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1239ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1240ea022d16SRodney W. Grimes } 1241b071c689SDavid Nugent #ifdef LOGIN_CAP 1242b071c689SDavid Nugent login_close(lc); 1243b071c689SDavid Nugent #endif 1244ea022d16SRodney W. Grimes return; 1245ea022d16SRodney W. Grimes bad: 1246ea022d16SRodney W. Grimes /* Forget all about it... */ 1247b071c689SDavid Nugent #ifdef LOGIN_CAP 1248b071c689SDavid Nugent login_close(lc); 1249b071c689SDavid Nugent #endif 1250ea022d16SRodney W. Grimes end_login(); 1251ea022d16SRodney W. Grimes } 1252ea022d16SRodney W. Grimes 1253ea022d16SRodney W. Grimes void 1254ea022d16SRodney W. Grimes retrieve(cmd, name) 1255ea022d16SRodney W. Grimes char *cmd, *name; 1256ea022d16SRodney W. Grimes { 1257ea022d16SRodney W. Grimes FILE *fin, *dout; 1258ea022d16SRodney W. Grimes struct stat st; 1259ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1260158a00b2SJohn Birrell time_t start; 1261ea022d16SRodney W. Grimes 1262ea022d16SRodney W. Grimes if (cmd == 0) { 1263ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1264ea022d16SRodney W. Grimes st.st_size = 0; 1265ea022d16SRodney W. Grimes } else { 1266ea022d16SRodney W. Grimes char line[BUFSIZ]; 1267ea022d16SRodney W. Grimes 1268e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1269ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1270ea022d16SRodney W. Grimes st.st_size = -1; 1271ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1272ea022d16SRodney W. Grimes } 1273ea022d16SRodney W. Grimes if (fin == NULL) { 1274ea022d16SRodney W. Grimes if (errno != 0) { 1275ea022d16SRodney W. Grimes perror_reply(550, name); 1276ea022d16SRodney W. Grimes if (cmd == 0) { 1277ea022d16SRodney W. Grimes LOGCMD("get", name); 1278ea022d16SRodney W. Grimes } 1279ea022d16SRodney W. Grimes } 1280ea022d16SRodney W. Grimes return; 1281ea022d16SRodney W. Grimes } 1282ea022d16SRodney W. Grimes byte_count = -1; 1283ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1284ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1285ea022d16SRodney W. Grimes goto done; 1286ea022d16SRodney W. Grimes } 1287ea022d16SRodney W. Grimes if (restart_point) { 1288ea022d16SRodney W. Grimes if (type == TYPE_A) { 1289ea022d16SRodney W. Grimes off_t i, n; 1290ea022d16SRodney W. Grimes int c; 1291ea022d16SRodney W. Grimes 1292ea022d16SRodney W. Grimes n = restart_point; 1293ea022d16SRodney W. Grimes i = 0; 1294ea022d16SRodney W. Grimes while (i++ < n) { 1295ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1296ea022d16SRodney W. Grimes perror_reply(550, name); 1297ea022d16SRodney W. Grimes goto done; 1298ea022d16SRodney W. Grimes } 1299ea022d16SRodney W. Grimes if (c == '\n') 1300ea022d16SRodney W. Grimes i++; 1301ea022d16SRodney W. Grimes } 1302ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1303ea022d16SRodney W. Grimes perror_reply(550, name); 1304ea022d16SRodney W. Grimes goto done; 1305ea022d16SRodney W. Grimes } 1306ea022d16SRodney W. Grimes } 1307ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1308ea022d16SRodney W. Grimes if (dout == NULL) 1309ea022d16SRodney W. Grimes goto done; 13103eb568f2SGuido van Rooij time(&start); 13119fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 13129fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 13133eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 13143eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1315ea022d16SRodney W. Grimes (void) fclose(dout); 1316ea022d16SRodney W. Grimes data = -1; 1317ea022d16SRodney W. Grimes pdata = -1; 1318ea022d16SRodney W. Grimes done: 1319ea022d16SRodney W. Grimes if (cmd == 0) 1320ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1321ea022d16SRodney W. Grimes (*closefunc)(fin); 1322ea022d16SRodney W. Grimes } 1323ea022d16SRodney W. Grimes 1324ea022d16SRodney W. Grimes void 1325ea022d16SRodney W. Grimes store(name, mode, unique) 1326ea022d16SRodney W. Grimes char *name, *mode; 1327ea022d16SRodney W. Grimes int unique; 1328ea022d16SRodney W. Grimes { 1329ea022d16SRodney W. Grimes FILE *fout, *din; 1330ea022d16SRodney W. Grimes struct stat st; 1331ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1332ea022d16SRodney W. Grimes 133361f891a6SPaul Traina if ((unique || guest) && stat(name, &st) == 0 && 1334ea022d16SRodney W. Grimes (name = gunique(name)) == NULL) { 1335ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1336ea022d16SRodney W. Grimes return; 1337ea022d16SRodney W. Grimes } 1338ea022d16SRodney W. Grimes 1339ea022d16SRodney W. Grimes if (restart_point) 1340ea022d16SRodney W. Grimes mode = "r+"; 1341ea022d16SRodney W. Grimes fout = fopen(name, mode); 1342ea022d16SRodney W. Grimes closefunc = fclose; 1343ea022d16SRodney W. Grimes if (fout == NULL) { 1344ea022d16SRodney W. Grimes perror_reply(553, name); 1345ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1346ea022d16SRodney W. Grimes return; 1347ea022d16SRodney W. Grimes } 1348ea022d16SRodney W. Grimes byte_count = -1; 1349ea022d16SRodney W. Grimes if (restart_point) { 1350ea022d16SRodney W. Grimes if (type == TYPE_A) { 1351ea022d16SRodney W. Grimes off_t i, n; 1352ea022d16SRodney W. Grimes int c; 1353ea022d16SRodney W. Grimes 1354ea022d16SRodney W. Grimes n = restart_point; 1355ea022d16SRodney W. Grimes i = 0; 1356ea022d16SRodney W. Grimes while (i++ < n) { 1357ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1358ea022d16SRodney W. Grimes perror_reply(550, name); 1359ea022d16SRodney W. Grimes goto done; 1360ea022d16SRodney W. Grimes } 1361ea022d16SRodney W. Grimes if (c == '\n') 1362ea022d16SRodney W. Grimes i++; 1363ea022d16SRodney W. Grimes } 1364ea022d16SRodney W. Grimes /* 1365ea022d16SRodney W. Grimes * We must do this seek to "current" position 1366ea022d16SRodney W. Grimes * because we are changing from reading to 1367ea022d16SRodney W. Grimes * writing. 1368ea022d16SRodney W. Grimes */ 1369ea022d16SRodney W. Grimes if (fseek(fout, 0L, L_INCR) < 0) { 1370ea022d16SRodney W. Grimes perror_reply(550, name); 1371ea022d16SRodney W. Grimes goto done; 1372ea022d16SRodney W. Grimes } 1373ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1374ea022d16SRodney W. Grimes perror_reply(550, name); 1375ea022d16SRodney W. Grimes goto done; 1376ea022d16SRodney W. Grimes } 1377ea022d16SRodney W. Grimes } 1378ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1379ea022d16SRodney W. Grimes if (din == NULL) 1380ea022d16SRodney W. Grimes goto done; 1381ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1382ea022d16SRodney W. Grimes if (unique) 1383ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1384ea022d16SRodney W. Grimes name); 1385ea022d16SRodney W. Grimes else 1386ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1387ea022d16SRodney W. Grimes } 1388ea022d16SRodney W. Grimes (void) fclose(din); 1389ea022d16SRodney W. Grimes data = -1; 1390ea022d16SRodney W. Grimes pdata = -1; 1391ea022d16SRodney W. Grimes done: 1392ea022d16SRodney W. Grimes LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1393ea022d16SRodney W. Grimes (*closefunc)(fout); 1394ea022d16SRodney W. Grimes } 1395ea022d16SRodney W. Grimes 1396ea022d16SRodney W. Grimes static FILE * 1397ea022d16SRodney W. Grimes getdatasock(mode) 1398ea022d16SRodney W. Grimes char *mode; 1399ea022d16SRodney W. Grimes { 1400ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1401ea022d16SRodney W. Grimes 1402ea022d16SRodney W. Grimes if (data >= 0) 1403ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1404ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1405ea022d16SRodney W. Grimes s = socket(AF_INET, SOCK_STREAM, 0); 1406ea022d16SRodney W. Grimes if (s < 0) 1407ea022d16SRodney W. Grimes goto bad; 1408ea022d16SRodney W. Grimes if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1409ea022d16SRodney W. Grimes (char *) &on, sizeof(on)) < 0) 1410ea022d16SRodney W. Grimes goto bad; 1411ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 1412a5a4544eSPaul Traina data_source.sin_len = sizeof(struct sockaddr_in); 1413ea022d16SRodney W. Grimes data_source.sin_family = AF_INET; 1414ea022d16SRodney W. Grimes data_source.sin_addr = ctrl_addr.sin_addr; 1415ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1416ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 1417ea022d16SRodney W. Grimes sizeof(data_source)) >= 0) 1418ea022d16SRodney W. Grimes break; 1419ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1420ea022d16SRodney W. Grimes goto bad; 1421ea022d16SRodney W. Grimes sleep(tries); 1422ea022d16SRodney W. Grimes } 1423ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1424ea022d16SRodney W. Grimes #ifdef IP_TOS 1425ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 1426ea022d16SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1427ea022d16SRodney W. Grimes syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 1428ea022d16SRodney W. Grimes #endif 14299fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 14309fc5823aSGarrett Wollman /* 14319fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 14329fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 14339fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 14349fc5823aSGarrett Wollman * in heavy-load situations. 14359fc5823aSGarrett Wollman */ 14369fc5823aSGarrett Wollman on = 1; 14379fc5823aSGarrett Wollman if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 14389fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 14399fc5823aSGarrett Wollman #endif 14409fc5823aSGarrett Wollman #ifdef SO_SNDBUF 14419fc5823aSGarrett Wollman on = 65536; 14429fc5823aSGarrett Wollman if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 14439fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 14449fc5823aSGarrett Wollman #endif 14459fc5823aSGarrett Wollman 1446ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1447ea022d16SRodney W. Grimes bad: 1448ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1449ea022d16SRodney W. Grimes t = errno; 1450ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1451ea022d16SRodney W. Grimes (void) close(s); 1452ea022d16SRodney W. Grimes errno = t; 1453ea022d16SRodney W. Grimes return (NULL); 1454ea022d16SRodney W. Grimes } 1455ea022d16SRodney W. Grimes 1456ea022d16SRodney W. Grimes static FILE * 1457ea022d16SRodney W. Grimes dataconn(name, size, mode) 1458ea022d16SRodney W. Grimes char *name; 1459ea022d16SRodney W. Grimes off_t size; 1460ea022d16SRodney W. Grimes char *mode; 1461ea022d16SRodney W. Grimes { 1462ea022d16SRodney W. Grimes char sizebuf[32]; 1463ea022d16SRodney W. Grimes FILE *file; 1464ea022d16SRodney W. Grimes int retry = 0, tos; 1465ea022d16SRodney W. Grimes 1466ea022d16SRodney W. Grimes file_size = size; 1467ea022d16SRodney W. Grimes byte_count = 0; 1468ea022d16SRodney W. Grimes if (size != (off_t) -1) 1469e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1470ea022d16SRodney W. Grimes else 1471e760ef2cSWarner Losh *sizebuf = '\0'; 1472ea022d16SRodney W. Grimes if (pdata >= 0) { 1473ea022d16SRodney W. Grimes struct sockaddr_in from; 1474ea022d16SRodney W. Grimes int s, fromlen = sizeof(from); 1475d6ed3c37SGuido van Rooij struct timeval timeout; 1476d6ed3c37SGuido van Rooij fd_set set; 1477ea022d16SRodney W. Grimes 1478d6ed3c37SGuido van Rooij FD_ZERO(&set); 1479d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1480d6ed3c37SGuido van Rooij 1481d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1482d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1483d6ed3c37SGuido van Rooij 1484d6ed3c37SGuido van Rooij if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1485d6ed3c37SGuido van Rooij (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1486ea022d16SRodney W. Grimes reply(425, "Can't open data connection."); 1487ea022d16SRodney W. Grimes (void) close(pdata); 1488ea022d16SRodney W. Grimes pdata = -1; 1489ea022d16SRodney W. Grimes return (NULL); 1490ea022d16SRodney W. Grimes } 1491ea022d16SRodney W. Grimes (void) close(pdata); 1492ea022d16SRodney W. Grimes pdata = s; 1493ea022d16SRodney W. Grimes #ifdef IP_TOS 1494a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 1495ea022d16SRodney W. Grimes (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1496ea022d16SRodney W. Grimes sizeof(int)); 1497ea022d16SRodney W. Grimes #endif 1498ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1499ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1500ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 1501ea022d16SRodney W. Grimes } 1502ea022d16SRodney W. Grimes if (data >= 0) { 1503ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1504ea022d16SRodney W. Grimes name, sizebuf); 1505ea022d16SRodney W. Grimes usedefault = 1; 1506ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1507ea022d16SRodney W. Grimes } 1508ea022d16SRodney W. Grimes if (usedefault) 1509ea022d16SRodney W. Grimes data_dest = his_addr; 1510ea022d16SRodney W. Grimes usedefault = 1; 1511ea022d16SRodney W. Grimes file = getdatasock(mode); 1512ea022d16SRodney W. Grimes if (file == NULL) { 1513ea022d16SRodney W. Grimes reply(425, "Can't create data socket (%s,%d): %s.", 1514ea022d16SRodney W. Grimes inet_ntoa(data_source.sin_addr), 1515ea022d16SRodney W. Grimes ntohs(data_source.sin_port), strerror(errno)); 1516ea022d16SRodney W. Grimes return (NULL); 1517ea022d16SRodney W. Grimes } 1518ea022d16SRodney W. Grimes data = fileno(file); 1519ea022d16SRodney W. Grimes while (connect(data, (struct sockaddr *)&data_dest, 1520ea022d16SRodney W. Grimes sizeof(data_dest)) < 0) { 1521ea022d16SRodney W. Grimes if (errno == EADDRINUSE && retry < swaitmax) { 1522ea022d16SRodney W. Grimes sleep((unsigned) swaitint); 1523ea022d16SRodney W. Grimes retry += swaitint; 1524ea022d16SRodney W. Grimes continue; 1525ea022d16SRodney W. Grimes } 1526ea022d16SRodney W. Grimes perror_reply(425, "Can't build data connection"); 1527ea022d16SRodney W. Grimes (void) fclose(file); 1528ea022d16SRodney W. Grimes data = -1; 1529ea022d16SRodney W. Grimes return (NULL); 1530ea022d16SRodney W. Grimes } 1531ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1532ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1533ea022d16SRodney W. Grimes return (file); 1534ea022d16SRodney W. Grimes } 1535ea022d16SRodney W. Grimes 1536ea022d16SRodney W. Grimes /* 1537ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 15389fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1539ea022d16SRodney W. Grimes * 1540ea022d16SRodney W. Grimes * NB: Form isn't handled. 1541ea022d16SRodney W. Grimes */ 1542ea022d16SRodney W. Grimes static void 15439fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg) 1544ea022d16SRodney W. Grimes FILE *instr, *outstr; 1545ea022d16SRodney W. Grimes off_t blksize; 15469fc5823aSGarrett Wollman off_t filesize; 15479fc5823aSGarrett Wollman int isreg; 1548ea022d16SRodney W. Grimes { 1549ea022d16SRodney W. Grimes int c, cnt, filefd, netfd; 15509fc5823aSGarrett Wollman char *buf, *bp; 15519fc5823aSGarrett Wollman size_t len; 1552ea022d16SRodney W. Grimes 1553ea022d16SRodney W. Grimes transflag++; 1554ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1555ea022d16SRodney W. Grimes transflag = 0; 1556ea022d16SRodney W. Grimes return; 1557ea022d16SRodney W. Grimes } 1558ea022d16SRodney W. Grimes switch (type) { 1559ea022d16SRodney W. Grimes 1560ea022d16SRodney W. Grimes case TYPE_A: 1561ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1562ea022d16SRodney W. Grimes byte_count++; 1563ea022d16SRodney W. Grimes if (c == '\n') { 1564ea022d16SRodney W. Grimes if (ferror(outstr)) 1565ea022d16SRodney W. Grimes goto data_err; 1566ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1567ea022d16SRodney W. Grimes } 1568ea022d16SRodney W. Grimes (void) putc(c, outstr); 1569ea022d16SRodney W. Grimes } 1570ea022d16SRodney W. Grimes fflush(outstr); 1571ea022d16SRodney W. Grimes transflag = 0; 1572ea022d16SRodney W. Grimes if (ferror(instr)) 1573ea022d16SRodney W. Grimes goto file_err; 1574ea022d16SRodney W. Grimes if (ferror(outstr)) 1575ea022d16SRodney W. Grimes goto data_err; 1576ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1577ea022d16SRodney W. Grimes return; 1578ea022d16SRodney W. Grimes 1579ea022d16SRodney W. Grimes case TYPE_I: 1580ea022d16SRodney W. Grimes case TYPE_L: 15819fc5823aSGarrett Wollman /* 15829fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 15839fc5823aSGarrett Wollman * are sending a regular file 15849fc5823aSGarrett Wollman */ 15859fc5823aSGarrett Wollman netfd = fileno(outstr); 15869fc5823aSGarrett Wollman filefd = fileno(instr); 15879fc5823aSGarrett Wollman 15889fc5823aSGarrett Wollman if (isreg && filesize < (off_t)16 * 1024 * 1024) { 15899fc5823aSGarrett Wollman buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, 15909fc5823aSGarrett Wollman (off_t)0); 15918abdc2ebSAlexander Langer if (buf == MAP_FAILED) { 15929fc5823aSGarrett Wollman syslog(LOG_WARNING, "mmap(%lu): %m", 15939fc5823aSGarrett Wollman (unsigned long)filesize); 15949fc5823aSGarrett Wollman goto oldway; 15959fc5823aSGarrett Wollman } 15969fc5823aSGarrett Wollman bp = buf; 15979fc5823aSGarrett Wollman len = filesize; 15989fc5823aSGarrett Wollman do { 15999fc5823aSGarrett Wollman cnt = write(netfd, bp, len); 16009fc5823aSGarrett Wollman len -= cnt; 16019fc5823aSGarrett Wollman bp += cnt; 16029fc5823aSGarrett Wollman if (cnt > 0) byte_count += cnt; 16039fc5823aSGarrett Wollman } while(cnt > 0 && len > 0); 16049fc5823aSGarrett Wollman 16059fc5823aSGarrett Wollman transflag = 0; 16069fc5823aSGarrett Wollman munmap(buf, (size_t)filesize); 16079fc5823aSGarrett Wollman if (cnt < 0) 16089fc5823aSGarrett Wollman goto data_err; 16099fc5823aSGarrett Wollman reply(226, "Transfer complete."); 16109fc5823aSGarrett Wollman return; 16119fc5823aSGarrett Wollman } 16129fc5823aSGarrett Wollman 16139fc5823aSGarrett Wollman oldway: 1614ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1615ea022d16SRodney W. Grimes transflag = 0; 1616ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 1617ea022d16SRodney W. Grimes return; 1618ea022d16SRodney W. Grimes } 16199fc5823aSGarrett Wollman 1620ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1621ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1622ea022d16SRodney W. Grimes byte_count += cnt; 1623ea022d16SRodney W. Grimes transflag = 0; 1624ea022d16SRodney W. Grimes (void)free(buf); 1625ea022d16SRodney W. Grimes if (cnt != 0) { 1626ea022d16SRodney W. Grimes if (cnt < 0) 1627ea022d16SRodney W. Grimes goto file_err; 1628ea022d16SRodney W. Grimes goto data_err; 1629ea022d16SRodney W. Grimes } 1630ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1631ea022d16SRodney W. Grimes return; 1632ea022d16SRodney W. Grimes default: 1633ea022d16SRodney W. Grimes transflag = 0; 1634ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 1635ea022d16SRodney W. Grimes return; 1636ea022d16SRodney W. Grimes } 1637ea022d16SRodney W. Grimes 1638ea022d16SRodney W. Grimes data_err: 1639ea022d16SRodney W. Grimes transflag = 0; 1640ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 1641ea022d16SRodney W. Grimes return; 1642ea022d16SRodney W. Grimes 1643ea022d16SRodney W. Grimes file_err: 1644ea022d16SRodney W. Grimes transflag = 0; 1645ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 1646ea022d16SRodney W. Grimes } 1647ea022d16SRodney W. Grimes 1648ea022d16SRodney W. Grimes /* 1649ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 1650ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 1651ea022d16SRodney W. Grimes * 1652ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 1653ea022d16SRodney W. Grimes */ 1654ea022d16SRodney W. Grimes static int 1655ea022d16SRodney W. Grimes receive_data(instr, outstr) 1656ea022d16SRodney W. Grimes FILE *instr, *outstr; 1657ea022d16SRodney W. Grimes { 1658ea022d16SRodney W. Grimes int c; 165961f891a6SPaul Traina int cnt, bare_lfs; 1660ea022d16SRodney W. Grimes char buf[BUFSIZ]; 1661ea022d16SRodney W. Grimes 1662ea022d16SRodney W. Grimes transflag++; 1663ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1664ea022d16SRodney W. Grimes transflag = 0; 1665ea022d16SRodney W. Grimes return (-1); 1666ea022d16SRodney W. Grimes } 166761f891a6SPaul Traina 166861f891a6SPaul Traina bare_lfs = 0; 166961f891a6SPaul Traina 1670ea022d16SRodney W. Grimes switch (type) { 1671ea022d16SRodney W. Grimes 1672ea022d16SRodney W. Grimes case TYPE_I: 1673ea022d16SRodney W. Grimes case TYPE_L: 1674ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1675ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 1676ea022d16SRodney W. Grimes goto file_err; 1677ea022d16SRodney W. Grimes byte_count += cnt; 1678ea022d16SRodney W. Grimes } 1679ea022d16SRodney W. Grimes if (cnt < 0) 1680ea022d16SRodney W. Grimes goto data_err; 1681ea022d16SRodney W. Grimes transflag = 0; 1682ea022d16SRodney W. Grimes return (0); 1683ea022d16SRodney W. Grimes 1684ea022d16SRodney W. Grimes case TYPE_E: 1685ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 1686ea022d16SRodney W. Grimes transflag = 0; 1687ea022d16SRodney W. Grimes return (-1); 1688ea022d16SRodney W. Grimes 1689ea022d16SRodney W. Grimes case TYPE_A: 1690ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1691ea022d16SRodney W. Grimes byte_count++; 1692ea022d16SRodney W. Grimes if (c == '\n') 1693ea022d16SRodney W. Grimes bare_lfs++; 1694ea022d16SRodney W. Grimes while (c == '\r') { 1695ea022d16SRodney W. Grimes if (ferror(outstr)) 1696ea022d16SRodney W. Grimes goto data_err; 1697ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 1698ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 1699ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 1700ea022d16SRodney W. Grimes goto contin2; 1701ea022d16SRodney W. Grimes } 1702ea022d16SRodney W. Grimes } 1703ea022d16SRodney W. Grimes (void) putc(c, outstr); 1704ea022d16SRodney W. Grimes contin2: ; 1705ea022d16SRodney W. Grimes } 1706ea022d16SRodney W. Grimes fflush(outstr); 1707ea022d16SRodney W. Grimes if (ferror(instr)) 1708ea022d16SRodney W. Grimes goto data_err; 1709ea022d16SRodney W. Grimes if (ferror(outstr)) 1710ea022d16SRodney W. Grimes goto file_err; 1711ea022d16SRodney W. Grimes transflag = 0; 1712ea022d16SRodney W. Grimes if (bare_lfs) { 1713ea022d16SRodney W. Grimes lreply(226, 1714ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 1715ea022d16SRodney W. Grimes bare_lfs); 1716ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 1717ea022d16SRodney W. Grimes } 1718ea022d16SRodney W. Grimes return (0); 1719ea022d16SRodney W. Grimes default: 1720ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 1721ea022d16SRodney W. Grimes transflag = 0; 1722ea022d16SRodney W. Grimes return (-1); 1723ea022d16SRodney W. Grimes } 1724ea022d16SRodney W. Grimes 1725ea022d16SRodney W. Grimes data_err: 1726ea022d16SRodney W. Grimes transflag = 0; 1727ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 1728ea022d16SRodney W. Grimes return (-1); 1729ea022d16SRodney W. Grimes 1730ea022d16SRodney W. Grimes file_err: 1731ea022d16SRodney W. Grimes transflag = 0; 1732ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 1733ea022d16SRodney W. Grimes return (-1); 1734ea022d16SRodney W. Grimes } 1735ea022d16SRodney W. Grimes 1736ea022d16SRodney W. Grimes void 1737ea022d16SRodney W. Grimes statfilecmd(filename) 1738ea022d16SRodney W. Grimes char *filename; 1739ea022d16SRodney W. Grimes { 1740ea022d16SRodney W. Grimes FILE *fin; 1741ea022d16SRodney W. Grimes int c; 1742ea022d16SRodney W. Grimes char line[LINE_MAX]; 1743ea022d16SRodney W. Grimes 1744af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 1745ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 1746ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 1747ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 1748ea022d16SRodney W. Grimes if (c == '\n') { 1749ea022d16SRodney W. Grimes if (ferror(stdout)){ 1750ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 1751ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1752ea022d16SRodney W. Grimes dologout(1); 1753ea022d16SRodney W. Grimes /* NOTREACHED */ 1754ea022d16SRodney W. Grimes } 1755ea022d16SRodney W. Grimes if (ferror(fin)) { 1756ea022d16SRodney W. Grimes perror_reply(551, filename); 1757ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1758ea022d16SRodney W. Grimes return; 1759ea022d16SRodney W. Grimes } 1760ea022d16SRodney W. Grimes (void) putc('\r', stdout); 1761ea022d16SRodney W. Grimes } 1762ea022d16SRodney W. Grimes (void) putc(c, stdout); 1763ea022d16SRodney W. Grimes } 1764ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1765ea022d16SRodney W. Grimes reply(211, "End of Status"); 1766ea022d16SRodney W. Grimes } 1767ea022d16SRodney W. Grimes 1768ea022d16SRodney W. Grimes void 1769ea022d16SRodney W. Grimes statcmd() 1770ea022d16SRodney W. Grimes { 1771ea022d16SRodney W. Grimes struct sockaddr_in *sin; 1772ea022d16SRodney W. Grimes u_char *a, *p; 1773ea022d16SRodney W. Grimes 1774ea022d16SRodney W. Grimes lreply(211, "%s FTP server status:", hostname, version); 1775ea022d16SRodney W. Grimes printf(" %s\r\n", version); 1776ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 1777ea022d16SRodney W. Grimes if (!isdigit(remotehost[0])) 1778ea022d16SRodney W. Grimes printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 1779ea022d16SRodney W. Grimes printf("\r\n"); 1780ea022d16SRodney W. Grimes if (logged_in) { 1781ea022d16SRodney W. Grimes if (guest) 1782ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 1783ea022d16SRodney W. Grimes else 1784ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 1785ea022d16SRodney W. Grimes } else if (askpasswd) 1786ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 1787ea022d16SRodney W. Grimes else 1788ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 1789ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 1790ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 1791ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 1792ea022d16SRodney W. Grimes if (type == TYPE_L) 1793ea022d16SRodney W. Grimes #if NBBY == 8 1794ea022d16SRodney W. Grimes printf(" %d", NBBY); 1795ea022d16SRodney W. Grimes #else 1796ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 1797ea022d16SRodney W. Grimes #endif 1798ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 1799ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 1800ea022d16SRodney W. Grimes if (data != -1) 1801ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 1802ea022d16SRodney W. Grimes else if (pdata != -1) { 1803ea022d16SRodney W. Grimes printf(" in Passive mode"); 1804ea022d16SRodney W. Grimes sin = &pasv_addr; 1805ea022d16SRodney W. Grimes goto printaddr; 1806ea022d16SRodney W. Grimes } else if (usedefault == 0) { 1807ea022d16SRodney W. Grimes printf(" PORT"); 1808ea022d16SRodney W. Grimes sin = &data_dest; 1809ea022d16SRodney W. Grimes printaddr: 1810ea022d16SRodney W. Grimes a = (u_char *) &sin->sin_addr; 1811ea022d16SRodney W. Grimes p = (u_char *) &sin->sin_port; 1812ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 1813ea022d16SRodney W. Grimes printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 1814ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1815ea022d16SRodney W. Grimes #undef UC 1816ea022d16SRodney W. Grimes } else 1817ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 1818ea022d16SRodney W. Grimes reply(211, "End of status"); 1819ea022d16SRodney W. Grimes } 1820ea022d16SRodney W. Grimes 1821ea022d16SRodney W. Grimes void 1822ea022d16SRodney W. Grimes fatal(s) 1823ea022d16SRodney W. Grimes char *s; 1824ea022d16SRodney W. Grimes { 1825ea022d16SRodney W. Grimes 1826ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 1827ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 1828ea022d16SRodney W. Grimes dologout(0); 1829ea022d16SRodney W. Grimes /* NOTREACHED */ 1830ea022d16SRodney W. Grimes } 1831ea022d16SRodney W. Grimes 1832ea022d16SRodney W. Grimes void 1833ea022d16SRodney W. Grimes #if __STDC__ 1834ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 1835ea022d16SRodney W. Grimes #else 1836ea022d16SRodney W. Grimes reply(n, fmt, va_alist) 1837ea022d16SRodney W. Grimes int n; 1838ea022d16SRodney W. Grimes char *fmt; 1839ea022d16SRodney W. Grimes va_dcl 1840ea022d16SRodney W. Grimes #endif 1841ea022d16SRodney W. Grimes { 1842ea022d16SRodney W. Grimes va_list ap; 1843ea022d16SRodney W. Grimes #if __STDC__ 1844ea022d16SRodney W. Grimes va_start(ap, fmt); 1845ea022d16SRodney W. Grimes #else 1846ea022d16SRodney W. Grimes va_start(ap); 1847ea022d16SRodney W. Grimes #endif 1848ea022d16SRodney W. Grimes (void)printf("%d ", n); 1849ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 1850ea022d16SRodney W. Grimes (void)printf("\r\n"); 1851ea022d16SRodney W. Grimes (void)fflush(stdout); 1852ea022d16SRodney W. Grimes if (debug) { 1853ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 1854ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 1855ea022d16SRodney W. Grimes } 1856ea022d16SRodney W. Grimes } 1857ea022d16SRodney W. Grimes 1858ea022d16SRodney W. Grimes void 1859ea022d16SRodney W. Grimes #if __STDC__ 1860ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 1861ea022d16SRodney W. Grimes #else 1862ea022d16SRodney W. Grimes lreply(n, fmt, va_alist) 1863ea022d16SRodney W. Grimes int n; 1864ea022d16SRodney W. Grimes char *fmt; 1865ea022d16SRodney W. Grimes va_dcl 1866ea022d16SRodney W. Grimes #endif 1867ea022d16SRodney W. Grimes { 1868ea022d16SRodney W. Grimes va_list ap; 1869ea022d16SRodney W. Grimes #if __STDC__ 1870ea022d16SRodney W. Grimes va_start(ap, fmt); 1871ea022d16SRodney W. Grimes #else 1872ea022d16SRodney W. Grimes va_start(ap); 1873ea022d16SRodney W. Grimes #endif 1874ea022d16SRodney W. Grimes (void)printf("%d- ", n); 1875ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 1876ea022d16SRodney W. Grimes (void)printf("\r\n"); 1877ea022d16SRodney W. Grimes (void)fflush(stdout); 1878ea022d16SRodney W. Grimes if (debug) { 1879ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 1880ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 1881ea022d16SRodney W. Grimes } 1882ea022d16SRodney W. Grimes } 1883ea022d16SRodney W. Grimes 1884ea022d16SRodney W. Grimes static void 1885ea022d16SRodney W. Grimes ack(s) 1886ea022d16SRodney W. Grimes char *s; 1887ea022d16SRodney W. Grimes { 1888ea022d16SRodney W. Grimes 1889ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 1890ea022d16SRodney W. Grimes } 1891ea022d16SRodney W. Grimes 1892ea022d16SRodney W. Grimes void 1893ea022d16SRodney W. Grimes nack(s) 1894ea022d16SRodney W. Grimes char *s; 1895ea022d16SRodney W. Grimes { 1896ea022d16SRodney W. Grimes 1897ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 1898ea022d16SRodney W. Grimes } 1899ea022d16SRodney W. Grimes 1900ea022d16SRodney W. Grimes /* ARGSUSED */ 1901ea022d16SRodney W. Grimes void 1902ea022d16SRodney W. Grimes yyerror(s) 1903ea022d16SRodney W. Grimes char *s; 1904ea022d16SRodney W. Grimes { 1905ea022d16SRodney W. Grimes char *cp; 1906ea022d16SRodney W. Grimes 19079aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 1908ea022d16SRodney W. Grimes *cp = '\0'; 1909ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 1910ea022d16SRodney W. Grimes } 1911ea022d16SRodney W. Grimes 1912ea022d16SRodney W. Grimes void 1913ea022d16SRodney W. Grimes delete(name) 1914ea022d16SRodney W. Grimes char *name; 1915ea022d16SRodney W. Grimes { 1916ea022d16SRodney W. Grimes struct stat st; 1917ea022d16SRodney W. Grimes 1918ea022d16SRodney W. Grimes LOGCMD("delete", name); 1919ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 1920ea022d16SRodney W. Grimes perror_reply(550, name); 1921ea022d16SRodney W. Grimes return; 1922ea022d16SRodney W. Grimes } 1923ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 1924ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 1925ea022d16SRodney W. Grimes perror_reply(550, name); 1926ea022d16SRodney W. Grimes return; 1927ea022d16SRodney W. Grimes } 1928ea022d16SRodney W. Grimes goto done; 1929ea022d16SRodney W. Grimes } 1930ea022d16SRodney W. Grimes if (unlink(name) < 0) { 1931ea022d16SRodney W. Grimes perror_reply(550, name); 1932ea022d16SRodney W. Grimes return; 1933ea022d16SRodney W. Grimes } 1934ea022d16SRodney W. Grimes done: 1935ea022d16SRodney W. Grimes ack("DELE"); 1936ea022d16SRodney W. Grimes } 1937ea022d16SRodney W. Grimes 1938ea022d16SRodney W. Grimes void 1939ea022d16SRodney W. Grimes cwd(path) 1940ea022d16SRodney W. Grimes char *path; 1941ea022d16SRodney W. Grimes { 1942ea022d16SRodney W. Grimes 1943ea022d16SRodney W. Grimes if (chdir(path) < 0) 1944ea022d16SRodney W. Grimes perror_reply(550, path); 1945ea022d16SRodney W. Grimes else 1946ea022d16SRodney W. Grimes ack("CWD"); 1947ea022d16SRodney W. Grimes } 1948ea022d16SRodney W. Grimes 1949ea022d16SRodney W. Grimes void 1950ea022d16SRodney W. Grimes makedir(name) 1951ea022d16SRodney W. Grimes char *name; 1952ea022d16SRodney W. Grimes { 1953ea022d16SRodney W. Grimes 1954ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 1955ea022d16SRodney W. Grimes if (mkdir(name, 0777) < 0) 1956ea022d16SRodney W. Grimes perror_reply(550, name); 1957ea022d16SRodney W. Grimes else 1958ea022d16SRodney W. Grimes reply(257, "MKD command successful."); 1959ea022d16SRodney W. Grimes } 1960ea022d16SRodney W. Grimes 1961ea022d16SRodney W. Grimes void 1962ea022d16SRodney W. Grimes removedir(name) 1963ea022d16SRodney W. Grimes char *name; 1964ea022d16SRodney W. Grimes { 1965ea022d16SRodney W. Grimes 1966ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 1967ea022d16SRodney W. Grimes if (rmdir(name) < 0) 1968ea022d16SRodney W. Grimes perror_reply(550, name); 1969ea022d16SRodney W. Grimes else 1970ea022d16SRodney W. Grimes ack("RMD"); 1971ea022d16SRodney W. Grimes } 1972ea022d16SRodney W. Grimes 1973ea022d16SRodney W. Grimes void 1974ea022d16SRodney W. Grimes pwd() 1975ea022d16SRodney W. Grimes { 1976ea022d16SRodney W. Grimes char path[MAXPATHLEN + 1]; 1977ea022d16SRodney W. Grimes 1978ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 1979ea022d16SRodney W. Grimes reply(550, "%s.", path); 1980ea022d16SRodney W. Grimes else 1981ea022d16SRodney W. Grimes reply(257, "\"%s\" is current directory.", path); 1982ea022d16SRodney W. Grimes } 1983ea022d16SRodney W. Grimes 1984ea022d16SRodney W. Grimes char * 1985ea022d16SRodney W. Grimes renamefrom(name) 1986ea022d16SRodney W. Grimes char *name; 1987ea022d16SRodney W. Grimes { 1988ea022d16SRodney W. Grimes struct stat st; 1989ea022d16SRodney W. Grimes 1990ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 1991ea022d16SRodney W. Grimes perror_reply(550, name); 1992ea022d16SRodney W. Grimes return ((char *)0); 1993ea022d16SRodney W. Grimes } 1994ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 1995ea022d16SRodney W. Grimes return (name); 1996ea022d16SRodney W. Grimes } 1997ea022d16SRodney W. Grimes 1998ea022d16SRodney W. Grimes void 1999ea022d16SRodney W. Grimes renamecmd(from, to) 2000ea022d16SRodney W. Grimes char *from, *to; 2001ea022d16SRodney W. Grimes { 200261f891a6SPaul Traina struct stat st; 2003ea022d16SRodney W. Grimes 2004ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 200561f891a6SPaul Traina 200661f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 200761f891a6SPaul Traina reply(550, "%s: permission denied", to); 200861f891a6SPaul Traina return; 200961f891a6SPaul Traina } 201061f891a6SPaul Traina 2011ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2012ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2013ea022d16SRodney W. Grimes else 2014ea022d16SRodney W. Grimes ack("RNTO"); 2015ea022d16SRodney W. Grimes } 2016ea022d16SRodney W. Grimes 2017ea022d16SRodney W. Grimes static void 2018ea022d16SRodney W. Grimes dolog(sin) 2019ea022d16SRodney W. Grimes struct sockaddr_in *sin; 2020ea022d16SRodney W. Grimes { 20219e9a43bdSBrian Somers realhostname(remotehost, sizeof(remotehost) - 1, &sin->sin_addr); 2022ea022d16SRodney W. Grimes 2023ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2024ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2025ea4e54b9SDavid Nugent if (thishost != firsthost) 2026ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2027ea4e54b9SDavid Nugent remotehost, hostname); 2028ea4e54b9SDavid Nugent else 2029ea4e54b9SDavid Nugent #endif 2030ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2031ea4e54b9SDavid Nugent remotehost); 2032b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2033ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2034ea022d16SRodney W. Grimes 2035ea4e54b9SDavid Nugent if (logging) { 2036ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2037ea4e54b9SDavid Nugent if (thishost != firsthost) 2038ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 2039ea4e54b9SDavid Nugent remotehost, hostname); 2040ea4e54b9SDavid Nugent else 2041ea4e54b9SDavid Nugent #endif 2042f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 2043f5c57d05SEivind Eklund inet_ntoa(sin->sin_addr)); 2044ea022d16SRodney W. Grimes } 2045ea4e54b9SDavid Nugent } 2046ea022d16SRodney W. Grimes 2047ea022d16SRodney W. Grimes /* 2048ea022d16SRodney W. Grimes * Record logout in wtmp file 2049ea022d16SRodney W. Grimes * and exit with supplied status. 2050ea022d16SRodney W. Grimes */ 2051ea022d16SRodney W. Grimes void 2052ea022d16SRodney W. Grimes dologout(status) 2053ea022d16SRodney W. Grimes int status; 2054ea022d16SRodney W. Grimes { 20550b4df2eeSDavid Greenman /* 20560b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 20570b4df2eeSDavid Greenman * back to the main program loop. 20580b4df2eeSDavid Greenman */ 20590b4df2eeSDavid Greenman transflag = 0; 2060ea022d16SRodney W. Grimes 2061ea022d16SRodney W. Grimes if (logged_in) { 2062ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 2063986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 2064ea022d16SRodney W. Grimes } 2065ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2066ea022d16SRodney W. Grimes _exit(status); 2067ea022d16SRodney W. Grimes } 2068ea022d16SRodney W. Grimes 2069ea022d16SRodney W. Grimes static void 2070ea022d16SRodney W. Grimes myoob(signo) 2071ea022d16SRodney W. Grimes int signo; 2072ea022d16SRodney W. Grimes { 2073ea022d16SRodney W. Grimes char *cp; 2074ea022d16SRodney W. Grimes 2075ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2076ea022d16SRodney W. Grimes if (!transflag) 2077ea022d16SRodney W. Grimes return; 2078ea022d16SRodney W. Grimes cp = tmpline; 2079ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2080ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2081ea022d16SRodney W. Grimes dologout(0); 2082ea022d16SRodney W. Grimes } 2083ea022d16SRodney W. Grimes upper(cp); 2084ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2085ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2086ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 2087ea022d16SRodney W. Grimes reply(226, "Abort successful"); 2088ea022d16SRodney W. Grimes longjmp(urgcatch, 1); 2089ea022d16SRodney W. Grimes } 2090ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 20919db4bbf3SMichael Haro tmpline[0] = '\0'; 2092ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 2093ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 2094ea022d16SRodney W. Grimes byte_count, file_size); 2095ea022d16SRodney W. Grimes else 2096ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 2097ea022d16SRodney W. Grimes } 2098ea022d16SRodney W. Grimes } 2099ea022d16SRodney W. Grimes 2100ea022d16SRodney W. Grimes /* 2101ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2102ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2103ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2104ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2105ea022d16SRodney W. Grimes */ 2106ea022d16SRodney W. Grimes void 2107ea022d16SRodney W. Grimes passive() 2108ea022d16SRodney W. Grimes { 2109dacc9752SPaul Traina int len; 2110ea022d16SRodney W. Grimes char *p, *a; 2111ea022d16SRodney W. Grimes 211261f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 211361f891a6SPaul Traina close(pdata); 211461f891a6SPaul Traina 2115ea022d16SRodney W. Grimes pdata = socket(AF_INET, SOCK_STREAM, 0); 2116ea022d16SRodney W. Grimes if (pdata < 0) { 2117ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2118ea022d16SRodney W. Grimes return; 2119ea022d16SRodney W. Grimes } 21204c450ad7SPaul Traina 21214c450ad7SPaul Traina (void) seteuid((uid_t)0); 212261f891a6SPaul Traina 2123dacc9752SPaul Traina #ifdef IP_PORTRANGE 2124dacc9752SPaul Traina { 2125dacc9752SPaul Traina int on = restricted_data_ports ? IP_PORTRANGE_HIGH 2126dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2127dacc9752SPaul Traina 212840e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2129dacc9752SPaul Traina (char *)&on, sizeof(on)) < 0) 21304c450ad7SPaul Traina goto pasv_error; 21314c450ad7SPaul Traina } 2132dacc9752SPaul Traina #endif 213340e9d39eSPeter Wemm 2134ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 2135ea022d16SRodney W. Grimes pasv_addr.sin_port = 0; 21364c450ad7SPaul Traina if (bind(pdata, (struct sockaddr *)&pasv_addr, 213761f891a6SPaul Traina sizeof(pasv_addr)) < 0) 2138ea022d16SRodney W. Grimes goto pasv_error; 213961f891a6SPaul Traina 2140ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 21414c450ad7SPaul Traina 2142ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2143ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2144ea022d16SRodney W. Grimes goto pasv_error; 2145ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2146ea022d16SRodney W. Grimes goto pasv_error; 2147ea022d16SRodney W. Grimes a = (char *) &pasv_addr.sin_addr; 2148ea022d16SRodney W. Grimes p = (char *) &pasv_addr.sin_port; 2149ea022d16SRodney W. Grimes 2150ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2151ea022d16SRodney W. Grimes 2152ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2153ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2154ea022d16SRodney W. Grimes return; 2155ea022d16SRodney W. Grimes 2156ea022d16SRodney W. Grimes pasv_error: 215761f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2158ea022d16SRodney W. Grimes (void) close(pdata); 2159ea022d16SRodney W. Grimes pdata = -1; 2160ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2161ea022d16SRodney W. Grimes return; 2162ea022d16SRodney W. Grimes } 2163ea022d16SRodney W. Grimes 2164ea022d16SRodney W. Grimes /* 2165ea022d16SRodney W. Grimes * Generate unique name for file with basename "local". 2166ea022d16SRodney W. Grimes * The file named "local" is already known to exist. 2167ea022d16SRodney W. Grimes * Generates failure reply on error. 2168ea022d16SRodney W. Grimes */ 2169ea022d16SRodney W. Grimes static char * 2170ea022d16SRodney W. Grimes gunique(local) 2171ea022d16SRodney W. Grimes char *local; 2172ea022d16SRodney W. Grimes { 2173ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2174ea022d16SRodney W. Grimes struct stat st; 2175ea022d16SRodney W. Grimes int count; 2176ea022d16SRodney W. Grimes char *cp; 2177ea022d16SRodney W. Grimes 2178ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2179ea022d16SRodney W. Grimes if (cp) 2180ea022d16SRodney W. Grimes *cp = '\0'; 2181ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2182ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2183ea022d16SRodney W. Grimes return ((char *) 0); 2184ea022d16SRodney W. Grimes } 2185ea022d16SRodney W. Grimes if (cp) 2186ea022d16SRodney W. Grimes *cp = '/'; 2187e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2188e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2189ea022d16SRodney W. Grimes cp = new + strlen(new); 2190ea022d16SRodney W. Grimes *cp++ = '.'; 2191ea022d16SRodney W. Grimes for (count = 1; count < 100; count++) { 2192ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2193ea022d16SRodney W. Grimes if (stat(new, &st) < 0) 2194ea022d16SRodney W. Grimes return (new); 2195ea022d16SRodney W. Grimes } 2196ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2197ea022d16SRodney W. Grimes return (NULL); 2198ea022d16SRodney W. Grimes } 2199ea022d16SRodney W. Grimes 2200ea022d16SRodney W. Grimes /* 2201ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2202ea022d16SRodney W. Grimes */ 2203ea022d16SRodney W. Grimes void 2204ea022d16SRodney W. Grimes perror_reply(code, string) 2205ea022d16SRodney W. Grimes int code; 2206ea022d16SRodney W. Grimes char *string; 2207ea022d16SRodney W. Grimes { 2208ea022d16SRodney W. Grimes 2209ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2210ea022d16SRodney W. Grimes } 2211ea022d16SRodney W. Grimes 2212ea022d16SRodney W. Grimes static char *onefile[] = { 2213ea022d16SRodney W. Grimes "", 2214ea022d16SRodney W. Grimes 0 2215ea022d16SRodney W. Grimes }; 2216ea022d16SRodney W. Grimes 2217ea022d16SRodney W. Grimes void 2218ea022d16SRodney W. Grimes send_file_list(whichf) 2219ea022d16SRodney W. Grimes char *whichf; 2220ea022d16SRodney W. Grimes { 2221ea022d16SRodney W. Grimes struct stat st; 2222ea022d16SRodney W. Grimes DIR *dirp = NULL; 2223ea022d16SRodney W. Grimes struct dirent *dir; 2224ea022d16SRodney W. Grimes FILE *dout = NULL; 2225ea022d16SRodney W. Grimes char **dirlist, *dirname; 2226ea022d16SRodney W. Grimes int simple = 0; 2227ea022d16SRodney W. Grimes int freeglob = 0; 2228ea022d16SRodney W. Grimes glob_t gl; 2229ea022d16SRodney W. Grimes 2230ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 2231ea022d16SRodney W. Grimes int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 2232ea022d16SRodney W. Grimes 2233ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 2234ea022d16SRodney W. Grimes freeglob = 1; 2235ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2236ea022d16SRodney W. Grimes reply(550, "not found"); 2237ea022d16SRodney W. Grimes goto out; 2238ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2239ea022d16SRodney W. Grimes errno = ENOENT; 2240ea022d16SRodney W. Grimes perror_reply(550, whichf); 2241ea022d16SRodney W. Grimes goto out; 2242ea022d16SRodney W. Grimes } 2243ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2244ea022d16SRodney W. Grimes } else { 2245ea022d16SRodney W. Grimes onefile[0] = whichf; 2246ea022d16SRodney W. Grimes dirlist = onefile; 2247ea022d16SRodney W. Grimes simple = 1; 2248ea022d16SRodney W. Grimes } 2249ea022d16SRodney W. Grimes 2250ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 2251ea022d16SRodney W. Grimes transflag = 0; 2252ea022d16SRodney W. Grimes goto out; 2253ea022d16SRodney W. Grimes } 22549aca17cbSMark Murray while ((dirname = *dirlist++)) { 2255ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2256ea022d16SRodney W. Grimes /* 2257ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2258ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2259ea022d16SRodney W. Grimes */ 2260ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2261ea022d16SRodney W. Grimes transflag == 0) { 2262af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2263ea022d16SRodney W. Grimes goto out; 2264ea022d16SRodney W. Grimes } 2265ea022d16SRodney W. Grimes perror_reply(550, whichf); 2266ea022d16SRodney W. Grimes if (dout != NULL) { 2267ea022d16SRodney W. Grimes (void) fclose(dout); 2268ea022d16SRodney W. Grimes transflag = 0; 2269ea022d16SRodney W. Grimes data = -1; 2270ea022d16SRodney W. Grimes pdata = -1; 2271ea022d16SRodney W. Grimes } 2272ea022d16SRodney W. Grimes goto out; 2273ea022d16SRodney W. Grimes } 2274ea022d16SRodney W. Grimes 2275ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2276ea022d16SRodney W. Grimes if (dout == NULL) { 2277ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2278ea022d16SRodney W. Grimes if (dout == NULL) 2279ea022d16SRodney W. Grimes goto out; 2280ea022d16SRodney W. Grimes transflag++; 2281ea022d16SRodney W. Grimes } 2282ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2283ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2284ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2285ea022d16SRodney W. Grimes continue; 2286ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2287ea022d16SRodney W. Grimes continue; 2288ea022d16SRodney W. Grimes 2289ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 2290ea022d16SRodney W. Grimes continue; 2291ea022d16SRodney W. Grimes 2292ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 2293ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 2294ea022d16SRodney W. Grimes 2295ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2296ea022d16SRodney W. Grimes continue; 2297ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2298ea022d16SRodney W. Grimes dir->d_namlen == 2) 2299ea022d16SRodney W. Grimes continue; 2300ea022d16SRodney W. Grimes 2301e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 2302e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 2303ea022d16SRodney W. Grimes 2304ea022d16SRodney W. Grimes /* 2305ea022d16SRodney W. Grimes * We have to do a stat to insure it's 2306ea022d16SRodney W. Grimes * not a directory or special file. 2307ea022d16SRodney W. Grimes */ 2308ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 2309ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 2310ea022d16SRodney W. Grimes if (dout == NULL) { 2311ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 2312ea022d16SRodney W. Grimes "w"); 2313ea022d16SRodney W. Grimes if (dout == NULL) 2314ea022d16SRodney W. Grimes goto out; 2315ea022d16SRodney W. Grimes transflag++; 2316ea022d16SRodney W. Grimes } 2317ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 2318ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 2319ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2320ea022d16SRodney W. Grimes else 2321ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 2322ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2323ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 2324ea022d16SRodney W. Grimes } 2325ea022d16SRodney W. Grimes } 2326ea022d16SRodney W. Grimes (void) closedir(dirp); 2327ea022d16SRodney W. Grimes } 2328ea022d16SRodney W. Grimes 2329ea022d16SRodney W. Grimes if (dout == NULL) 2330ea022d16SRodney W. Grimes reply(550, "No files found."); 2331ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 2332ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 2333ea022d16SRodney W. Grimes else 2334ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 2335ea022d16SRodney W. Grimes 2336ea022d16SRodney W. Grimes transflag = 0; 2337ea022d16SRodney W. Grimes if (dout != NULL) 2338ea022d16SRodney W. Grimes (void) fclose(dout); 2339ea022d16SRodney W. Grimes data = -1; 2340ea022d16SRodney W. Grimes pdata = -1; 2341ea022d16SRodney W. Grimes out: 2342ea022d16SRodney W. Grimes if (freeglob) { 2343ea022d16SRodney W. Grimes freeglob = 0; 2344ea022d16SRodney W. Grimes globfree(&gl); 2345ea022d16SRodney W. Grimes } 2346ea022d16SRodney W. Grimes } 2347ea022d16SRodney W. Grimes 2348cf09a206SDavid Greenman void 2349cf09a206SDavid Greenman reapchild(signo) 2350cf09a206SDavid Greenman int signo; 2351cf09a206SDavid Greenman { 2352cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 2353cf09a206SDavid Greenman } 2354cf09a206SDavid Greenman 2355b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2356ea022d16SRodney W. Grimes /* 2357ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2358ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2359ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2360ea022d16SRodney W. Grimes */ 2361ea022d16SRodney W. Grimes void 2362ea022d16SRodney W. Grimes #if __STDC__ 2363ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2364ea022d16SRodney W. Grimes #else 2365ea022d16SRodney W. Grimes setproctitle(fmt, va_alist) 2366ea022d16SRodney W. Grimes char *fmt; 2367ea022d16SRodney W. Grimes va_dcl 2368ea022d16SRodney W. Grimes #endif 2369ea022d16SRodney W. Grimes { 2370ea022d16SRodney W. Grimes int i; 2371ea022d16SRodney W. Grimes va_list ap; 2372ea022d16SRodney W. Grimes char *p, *bp, ch; 2373ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2374ea022d16SRodney W. Grimes 2375ea022d16SRodney W. Grimes #if __STDC__ 2376ea022d16SRodney W. Grimes va_start(ap, fmt); 2377ea022d16SRodney W. Grimes #else 2378ea022d16SRodney W. Grimes va_start(ap); 2379ea022d16SRodney W. Grimes #endif 2380ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2381ea022d16SRodney W. Grimes 2382ea022d16SRodney W. Grimes /* make ps print our process name */ 2383ea022d16SRodney W. Grimes p = Argv[0]; 2384ea022d16SRodney W. Grimes *p++ = '-'; 2385ea022d16SRodney W. Grimes 2386ea022d16SRodney W. Grimes i = strlen(buf); 2387ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 2388ea022d16SRodney W. Grimes i = LastArgv - p - 2; 2389ea022d16SRodney W. Grimes buf[i] = '\0'; 2390ea022d16SRodney W. Grimes } 2391ea022d16SRodney W. Grimes bp = buf; 2392ea022d16SRodney W. Grimes while (ch = *bp++) 2393ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 2394ea022d16SRodney W. Grimes *p++ = ch; 2395ea022d16SRodney W. Grimes while (p < LastArgv) 2396ea022d16SRodney W. Grimes *p++ = ' '; 2397ea022d16SRodney W. Grimes } 2398b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 23993eb568f2SGuido van Rooij 240061f891a6SPaul Traina static void 24013eb568f2SGuido van Rooij logxfer(name, size, start) 24023eb568f2SGuido van Rooij char *name; 24033eb568f2SGuido van Rooij long size; 24043eb568f2SGuido van Rooij long start; 24053eb568f2SGuido van Rooij { 24063eb568f2SGuido van Rooij char buf[1024]; 24073eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 2408158a00b2SJohn Birrell time_t now; 24093eb568f2SGuido van Rooij 24103eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 24113eb568f2SGuido van Rooij time(&now); 241261f891a6SPaul Traina snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n", 24133eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 24143eb568f2SGuido van Rooij path, name, size, now - start + (now == start)); 24153eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 24163eb568f2SGuido van Rooij } 24173eb568f2SGuido van Rooij } 2418