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[] = 47158a00b2SJohn Birrell "$Id: ftpd.c,v 1.50 1998/05/25 03:45:35 steve Exp $"; 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 97ea022d16SRodney W. Grimes #include "pathnames.h" 98ea022d16SRodney W. Grimes #include "extern.h" 99ea022d16SRodney W. Grimes 100ea022d16SRodney W. Grimes #if __STDC__ 101ea022d16SRodney W. Grimes #include <stdarg.h> 102ea022d16SRodney W. Grimes #else 103ea022d16SRodney W. Grimes #include <varargs.h> 104ea022d16SRodney W. Grimes #endif 105ea022d16SRodney W. Grimes 106af85d782SDavid Nugent #ifdef INTERNAL_LS 107af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 108af85d782SDavid Nugent #undef main 109af85d782SDavid Nugent #else 110ea022d16SRodney W. Grimes static char version[] = "Version 6.00"; 111af85d782SDavid Nugent #endif 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 169ea022d16SRodney W. Grimes 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 1759aca17cbSMark Murray #ifdef KERBEROS 1769aca17cbSMark Murray int klogin __P((struct passwd *, char *, char *, char *)); 1779aca17cbSMark Murray #endif 1789aca17cbSMark Murray 179105a3c98SJulian Elischer struct in_addr bind_address; 180105a3c98SJulian Elischer char *pid_file = NULL; 181105a3c98SJulian Elischer 182a5a4544eSPaul Traina #if defined(KERBEROS) 183a5a4544eSPaul Traina int notickets = 1; 1849aca17cbSMark Murray int noticketsdontcomplain = 1; 185a5a4544eSPaul Traina char *krbtkfile_env = NULL; 1863eb568f2SGuido van Rooij #endif 187ea022d16SRodney W. Grimes 188ea022d16SRodney W. Grimes /* 189ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 190ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 191ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 192ea022d16SRodney W. Grimes */ 193ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 194ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 195ea022d16SRodney W. Grimes 196ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 197ea022d16SRodney W. Grimes int swaitint = SWAITINT; 198ea022d16SRodney W. Grimes 199ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 200b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 201ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 202ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 203b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 204ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 205ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 206ea022d16SRodney W. Grimes 207726040deSGuido van Rooij #ifdef SKEY 208726040deSGuido van Rooij int pwok = 0; 2092c60c54cSPaul Traina char addr_string[20]; /* XXX */ 210726040deSGuido van Rooij #endif 211726040deSGuido van Rooij 212ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 213ea022d16SRodney W. Grimes if (logging > 1) \ 214ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 215ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 216ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 217ea022d16SRodney W. Grimes if (logging > 1) \ 218ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 219ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 220ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 221ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 222ea022d16SRodney W. Grimes if (logging > 1) { \ 223ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 224ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 225ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 226ea022d16SRodney W. Grimes else \ 227ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 228ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 229ea022d16SRodney W. Grimes } 230ea022d16SRodney W. Grimes 231ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 232ea4e54b9SDavid Nugent static void inithosts __P((void)); 233ea4e54b9SDavid Nugent static void selecthost __P((struct in_addr *)); 234ea4e54b9SDavid Nugent #endif 235ea022d16SRodney W. Grimes static void ack __P((char *)); 236ea022d16SRodney W. Grimes static void myoob __P((int)); 2377edcb936SSteve Price static int checkuser __P((char *, char *, int)); 238ea022d16SRodney W. Grimes static FILE *dataconn __P((char *, off_t, char *)); 239ea022d16SRodney W. Grimes static void dolog __P((struct sockaddr_in *)); 240ea022d16SRodney W. Grimes static char *curdir __P((void)); 241ea022d16SRodney W. Grimes static void end_login __P((void)); 242ea022d16SRodney W. Grimes static FILE *getdatasock __P((char *)); 243ea022d16SRodney W. Grimes static char *gunique __P((char *)); 244ea022d16SRodney W. Grimes static void lostconn __P((int)); 245ea022d16SRodney W. Grimes static int receive_data __P((FILE *, FILE *)); 2469fc5823aSGarrett Wollman static void send_data __P((FILE *, FILE *, off_t, off_t, int)); 247ea022d16SRodney W. Grimes static struct passwd * 248ea022d16SRodney W. Grimes sgetpwnam __P((char *)); 249ea022d16SRodney W. Grimes static char *sgetsave __P((char *)); 250cf09a206SDavid Greenman static void reapchild __P((int)); 25161f891a6SPaul Traina static void logxfer __P((char *, long, long)); 252ea022d16SRodney W. Grimes 253ea022d16SRodney W. Grimes static char * 254ea022d16SRodney W. Grimes curdir() 255ea022d16SRodney W. Grimes { 256ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 257ea022d16SRodney W. Grimes 258ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 259ea022d16SRodney W. Grimes return (""); 260ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 261ea022d16SRodney W. Grimes strcat(path, "/"); 262ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 263ea022d16SRodney W. Grimes return (guest ? path+1 : path); 264ea022d16SRodney W. Grimes } 265ea022d16SRodney W. Grimes 266ea022d16SRodney W. Grimes int 267ea022d16SRodney W. Grimes main(argc, argv, envp) 268ea022d16SRodney W. Grimes int argc; 269ea022d16SRodney W. Grimes char *argv[]; 270ea022d16SRodney W. Grimes char **envp; 271ea022d16SRodney W. Grimes { 272ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 273ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 274ea022d16SRodney W. Grimes FILE *fd; 275ea022d16SRodney W. Grimes 27634d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 27734d1ba5cSAndrey A. Chernov 278b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 279ea022d16SRodney W. Grimes /* 280ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 281ea022d16SRodney W. Grimes */ 282ea022d16SRodney W. Grimes Argv = argv; 283ea022d16SRodney W. Grimes while (*envp) 284ea022d16SRodney W. Grimes envp++; 285ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 286b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 287ea022d16SRodney W. Grimes 2883eb568f2SGuido van Rooij 289105a3c98SJulian Elischer bind_address.s_addr = htonl(INADDR_ANY); 29091477cc4SWarner Losh while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:")) != -1) { 291ea022d16SRodney W. Grimes switch (ch) { 292cf09a206SDavid Greenman case 'D': 293cf09a206SDavid Greenman daemon_mode++; 294cf09a206SDavid Greenman break; 295cf09a206SDavid Greenman 296ea022d16SRodney W. Grimes case 'd': 297a5a4544eSPaul Traina debug++; 298ea022d16SRodney W. Grimes break; 299ea022d16SRodney W. Grimes 300ea022d16SRodney W. Grimes case 'l': 301ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 302ea022d16SRodney W. Grimes break; 303ea022d16SRodney W. Grimes 304a5a4544eSPaul Traina case 'R': 305a5a4544eSPaul Traina paranoid = 0; 306a5a4544eSPaul Traina break; 307a5a4544eSPaul Traina 308a5a4544eSPaul Traina case 'S': 309a5a4544eSPaul Traina stats++; 310a5a4544eSPaul Traina break; 311a5a4544eSPaul Traina 312a5a4544eSPaul Traina case 'T': 313a5a4544eSPaul Traina maxtimeout = atoi(optarg); 314a5a4544eSPaul Traina if (timeout > maxtimeout) 315a5a4544eSPaul Traina timeout = maxtimeout; 3164c450ad7SPaul Traina break; 3174c450ad7SPaul Traina 318ea022d16SRodney W. Grimes case 't': 319ea022d16SRodney W. Grimes timeout = atoi(optarg); 320ea022d16SRodney W. Grimes if (maxtimeout < timeout) 321ea022d16SRodney W. Grimes maxtimeout = timeout; 322ea022d16SRodney W. Grimes break; 323a5a4544eSPaul Traina 324a5a4544eSPaul Traina case 'U': 325a5a4544eSPaul Traina restricted_data_ports = 0; 326ea022d16SRodney W. Grimes break; 327ea022d16SRodney W. Grimes 328105a3c98SJulian Elischer case 'a': 329105a3c98SJulian Elischer if (!inet_aton(optarg, &bind_address)) 330105a3c98SJulian Elischer errx(1, "invalid address for -a"); 331105a3c98SJulian Elischer break; 332105a3c98SJulian Elischer 333105a3c98SJulian Elischer case 'p': 334105a3c98SJulian Elischer pid_file = optarg; 335105a3c98SJulian Elischer break; 336105a3c98SJulian Elischer 337ea022d16SRodney W. Grimes case 'u': 338ea022d16SRodney W. Grimes { 339ea022d16SRodney W. Grimes long val = 0; 340ea022d16SRodney W. Grimes 341ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 342ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 343ea022d16SRodney W. Grimes warnx("bad value for -u"); 344ea022d16SRodney W. Grimes else 345ea022d16SRodney W. Grimes defumask = val; 346ea022d16SRodney W. Grimes break; 347ea022d16SRodney W. Grimes } 3485a392aecSTorsten Blum case 'A': 3495a392aecSTorsten Blum anon_only = 1; 3505a392aecSTorsten Blum break; 351ea022d16SRodney W. Grimes 352ea022d16SRodney W. Grimes case 'v': 353ea022d16SRodney W. Grimes debug = 1; 354ea022d16SRodney W. Grimes break; 355ea022d16SRodney W. Grimes 356ea022d16SRodney W. Grimes default: 357ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 358ea022d16SRodney W. Grimes break; 359ea022d16SRodney W. Grimes } 360ea022d16SRodney W. Grimes } 361cf09a206SDavid Greenman 362ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 363ea4e54b9SDavid Nugent inithosts(); 364ea4e54b9SDavid Nugent #endif 365ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 366cf09a206SDavid Greenman 367cf09a206SDavid Greenman /* 368cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 369cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 370cf09a206SDavid Greenman */ 371cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 372cf09a206SDavid Greenman 373cf09a206SDavid Greenman if (daemon_mode) { 374cf09a206SDavid Greenman int ctl_sock, fd; 375cf09a206SDavid Greenman struct servent *sv; 376cf09a206SDavid Greenman 377cf09a206SDavid Greenman /* 378cf09a206SDavid Greenman * Detach from parent. 379cf09a206SDavid Greenman */ 380cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 381cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 382cf09a206SDavid Greenman exit(1); 383cf09a206SDavid Greenman } 384cf09a206SDavid Greenman (void) signal(SIGCHLD, reapchild); 385cf09a206SDavid Greenman /* 386cf09a206SDavid Greenman * Get port number for ftp/tcp. 387cf09a206SDavid Greenman */ 388cf09a206SDavid Greenman sv = getservbyname("ftp", "tcp"); 389cf09a206SDavid Greenman if (sv == NULL) { 390cf09a206SDavid Greenman syslog(LOG_ERR, "getservbyname for ftp failed"); 391cf09a206SDavid Greenman exit(1); 392cf09a206SDavid Greenman } 393cf09a206SDavid Greenman /* 394cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 395cf09a206SDavid Greenman * listening. 396cf09a206SDavid Greenman */ 397cf09a206SDavid Greenman ctl_sock = socket(AF_INET, SOCK_STREAM, 0); 398cf09a206SDavid Greenman if (ctl_sock < 0) { 399cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 400cf09a206SDavid Greenman exit(1); 401cf09a206SDavid Greenman } 402cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 403cf09a206SDavid Greenman (char *)&on, sizeof(on)) < 0) 404cf09a206SDavid Greenman syslog(LOG_ERR, "control setsockopt: %m");; 405cf09a206SDavid Greenman server_addr.sin_family = AF_INET; 406105a3c98SJulian Elischer server_addr.sin_addr = bind_address; 407cf09a206SDavid Greenman server_addr.sin_port = sv->s_port; 408cf09a206SDavid Greenman if (bind(ctl_sock, (struct sockaddr *)&server_addr, sizeof(server_addr))) { 409cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 410cf09a206SDavid Greenman exit(1); 411cf09a206SDavid Greenman } 412cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 413cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 414cf09a206SDavid Greenman exit(1); 415cf09a206SDavid Greenman } 416cf09a206SDavid Greenman /* 417105a3c98SJulian Elischer * Atomically write process ID 418105a3c98SJulian Elischer */ 419105a3c98SJulian Elischer if (pid_file) 420105a3c98SJulian Elischer { 421105a3c98SJulian Elischer int fd; 422105a3c98SJulian Elischer char buf[20]; 423105a3c98SJulian Elischer 424105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 425105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 426105a3c98SJulian Elischer if (fd < 0) 427105a3c98SJulian Elischer if (errno == EAGAIN) 428105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 429105a3c98SJulian Elischer else 430105a3c98SJulian Elischer err(1, "%s", pid_file); 431105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 432105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 433105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 434105a3c98SJulian Elischer err(1, "%s: write", pid_file); 435105a3c98SJulian Elischer /* Leave the pid file open and locked */ 436105a3c98SJulian Elischer } 437105a3c98SJulian Elischer /* 438cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 439cf09a206SDavid Greenman * children to handle them. 440cf09a206SDavid Greenman */ 441cf09a206SDavid Greenman while (1) { 442cf09a206SDavid Greenman addrlen = sizeof(his_addr); 443cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 444cf09a206SDavid Greenman if (fork() == 0) { 445cf09a206SDavid Greenman /* child */ 446cf09a206SDavid Greenman (void) dup2(fd, 0); 447cf09a206SDavid Greenman (void) dup2(fd, 1); 448cf09a206SDavid Greenman close(ctl_sock); 449cf09a206SDavid Greenman break; 450cf09a206SDavid Greenman } 451cf09a206SDavid Greenman close(fd); 452cf09a206SDavid Greenman } 453cf09a206SDavid Greenman } else { 454cf09a206SDavid Greenman addrlen = sizeof(his_addr); 455cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 456cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 457cf09a206SDavid Greenman exit(1); 458cf09a206SDavid Greenman } 459cf09a206SDavid Greenman } 460cf09a206SDavid Greenman 461ea022d16SRodney W. Grimes (void) signal(SIGCHLD, SIG_IGN); 462cf09a206SDavid Greenman (void) signal(SIGPIPE, lostconn); 463158a00b2SJohn Birrell if (signal(SIGURG, myoob) == SIG_ERR) 464ea022d16SRodney W. Grimes syslog(LOG_ERR, "signal: %m"); 465ea022d16SRodney W. Grimes 466cf09a206SDavid Greenman #ifdef SKEY 46761f891a6SPaul Traina strncpy(addr_string, inet_ntoa(his_addr.sin_addr), sizeof(addr_string)); 468cf09a206SDavid Greenman #endif 469cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 470cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 471cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 472cf09a206SDavid Greenman exit(1); 473cf09a206SDavid Greenman } 474ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 475ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 476ea4e54b9SDavid Nugent selecthost(&ctrl_addr.sin_addr); 477ea4e54b9SDavid Nugent #endif 478cf09a206SDavid Greenman #ifdef IP_TOS 479cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 480cf09a206SDavid Greenman if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 481cf09a206SDavid Greenman syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 482cf09a206SDavid Greenman #endif 483dadb9fb3SDavid Greenman /* 484dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 485dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 486dadb9fb3SDavid Greenman */ 487dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 488dadb9fb3SDavid Greenman syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m"); 489dadb9fb3SDavid Greenman 490cf09a206SDavid Greenman data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 491cf09a206SDavid Greenman 492a5a4544eSPaul Traina /* set this here so klogin can use it... */ 493e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 494a5a4544eSPaul Traina 495ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 496ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 497ea022d16SRodney W. Grimes if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 498ea022d16SRodney W. Grimes syslog(LOG_ERR, "setsockopt: %m"); 499ea022d16SRodney W. Grimes #endif 500ea022d16SRodney W. Grimes 501ea022d16SRodney W. Grimes #ifdef F_SETOWN 502ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 503ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 504ea022d16SRodney W. Grimes #endif 505ea022d16SRodney W. Grimes dolog(&his_addr); 506ea022d16SRodney W. Grimes /* 507ea022d16SRodney W. Grimes * Set up default state 508ea022d16SRodney W. Grimes */ 509ea022d16SRodney W. Grimes data = -1; 510ea022d16SRodney W. Grimes type = TYPE_A; 511ea022d16SRodney W. Grimes form = FORM_N; 512ea022d16SRodney W. Grimes stru = STRU_F; 513ea022d16SRodney W. Grimes mode = MODE_S; 514ea022d16SRodney W. Grimes tmpline[0] = '\0'; 515ea022d16SRodney W. Grimes 516ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 517ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 518ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 519ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 520ea022d16SRodney W. Grimes *cp = '\0'; 521ea022d16SRodney W. Grimes lreply(530, "%s", line); 522ea022d16SRodney W. Grimes } 523ea022d16SRodney W. Grimes (void) fflush(stdout); 524ea022d16SRodney W. Grimes (void) fclose(fd); 525ea022d16SRodney W. Grimes reply(530, "System not available."); 526ea022d16SRodney W. Grimes exit(0); 527ea022d16SRodney W. Grimes } 528ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 529ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 530ea4e54b9SDavid Nugent #else 531ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 532ea4e54b9SDavid Nugent #endif 533ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 534ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 535ea022d16SRodney W. Grimes *cp = '\0'; 536ea022d16SRodney W. Grimes lreply(220, "%s", line); 537ea022d16SRodney W. Grimes } 538ea022d16SRodney W. Grimes (void) fflush(stdout); 539ea022d16SRodney W. Grimes (void) fclose(fd); 540ea022d16SRodney W. Grimes /* reply(220,) must follow */ 541ea022d16SRodney W. Grimes } 542ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 5430512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 5440512556aSDavid Nugent fatal("Ran out of memory."); 5450512556aSDavid Nugent (void) gethostname(hostname, MAXHOSTNAMELEN); 546ea4e54b9SDavid Nugent #endif 547ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 548ea022d16SRodney W. Grimes (void) setjmp(errcatch); 549ea022d16SRodney W. Grimes for (;;) 550ea022d16SRodney W. Grimes (void) yyparse(); 551ea022d16SRodney W. Grimes /* NOTREACHED */ 552ea022d16SRodney W. Grimes } 553ea022d16SRodney W. Grimes 554ea022d16SRodney W. Grimes static void 555ea022d16SRodney W. Grimes lostconn(signo) 556ea022d16SRodney W. Grimes int signo; 557ea022d16SRodney W. Grimes { 558ea022d16SRodney W. Grimes 559ea022d16SRodney W. Grimes if (debug) 560ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 561e02897faSPhilippe Charnier dologout(1); 562ea022d16SRodney W. Grimes } 563ea022d16SRodney W. Grimes 564ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 565ea4e54b9SDavid Nugent /* 566ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 567ea4e54b9SDavid Nugent */ 568ea4e54b9SDavid Nugent 569ea4e54b9SDavid Nugent static void 570ea4e54b9SDavid Nugent inithosts() 571ea4e54b9SDavid Nugent { 572ea4e54b9SDavid Nugent FILE *fp; 573ea4e54b9SDavid Nugent char *cp; 574ea4e54b9SDavid Nugent struct hostent *hp; 575ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 576ea4e54b9SDavid Nugent char line[1024]; 577ea4e54b9SDavid Nugent 578ea4e54b9SDavid Nugent /* 579ea4e54b9SDavid Nugent * Fill in the default host information 580ea4e54b9SDavid Nugent */ 581ea4e54b9SDavid Nugent if (gethostname(line, sizeof(line)) < 0) 582ea4e54b9SDavid Nugent line[0] = '\0'; 583ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL || 584ea4e54b9SDavid Nugent (hrp->hostname = strdup(line)) == NULL) 585ea4e54b9SDavid Nugent fatal("Ran out of memory."); 586ea4e54b9SDavid Nugent memset(&hrp->hostaddr, 0, sizeof hrp->hostaddr); 587ea4e54b9SDavid Nugent if ((hp = gethostbyname(hrp->hostname)) != NULL) 588ea4e54b9SDavid Nugent (void) memcpy(&hrp->hostaddr, 589ea4e54b9SDavid Nugent hp->h_addr_list[0], 590ea4e54b9SDavid Nugent sizeof(hrp->hostaddr)); 591ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 592ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 593ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 594ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 595ea4e54b9SDavid Nugent hrp->next = NULL; 596ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 597ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 598ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL) { 599ea4e54b9SDavid Nugent int i; 600ea4e54b9SDavid Nugent 601ea4e54b9SDavid Nugent if ((cp = strchr(line, '\n')) == NULL) { 602ea4e54b9SDavid Nugent /* ignore long lines */ 603ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL && 604ea4e54b9SDavid Nugent strchr(line, '\n') == NULL) 605ea4e54b9SDavid Nugent ; 606ea4e54b9SDavid Nugent continue; 607ea4e54b9SDavid Nugent } 608ea4e54b9SDavid Nugent *cp = '\0'; 609ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 610ea4e54b9SDavid Nugent /* skip comments and empty lines */ 611ea4e54b9SDavid Nugent if (cp == NULL || line[0] == '#') 612ea4e54b9SDavid Nugent continue; 613ea4e54b9SDavid Nugent /* first, try a standard gethostbyname() */ 614ea4e54b9SDavid Nugent if ((hp = gethostbyname(cp)) == NULL) 615ea4e54b9SDavid Nugent continue; 616ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 617ea4e54b9SDavid Nugent if (memcmp(&hrp->hostaddr, 618ea4e54b9SDavid Nugent hp->h_addr_list[0], 619ea4e54b9SDavid Nugent sizeof(hrp->hostaddr)) == 0) 620ea4e54b9SDavid Nugent break; 621ea4e54b9SDavid Nugent } 622ea4e54b9SDavid Nugent if (hrp == NULL) { 623ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 624ea4e54b9SDavid Nugent continue; 625ea4e54b9SDavid Nugent /* defaults */ 626ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 627ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 628ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 629ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 630ea4e54b9SDavid Nugent hrp->next = NULL; 631ea4e54b9SDavid Nugent lhrp->next = hrp; 632ea4e54b9SDavid Nugent lhrp = hrp; 633ea4e54b9SDavid Nugent } 634ea4e54b9SDavid Nugent (void) memcpy(&hrp->hostaddr, 635ea4e54b9SDavid Nugent hp->h_addr_list[0], 636ea4e54b9SDavid Nugent sizeof(hrp->hostaddr)); 637ea4e54b9SDavid Nugent /* 638ea4e54b9SDavid Nugent * determine hostname to use. 639ea4e54b9SDavid Nugent * force defined name if it is a valid alias 640ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 641ea4e54b9SDavid Nugent */ 642ea4e54b9SDavid Nugent if ((hp = gethostbyaddr((char*)&hrp->hostaddr, 643ea4e54b9SDavid Nugent sizeof(hrp->hostaddr), 644ea4e54b9SDavid Nugent AF_INET)) != NULL) { 645ea4e54b9SDavid Nugent if (strcmp(cp, hp->h_name) != 0) { 646ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 647ea4e54b9SDavid Nugent cp = hp->h_name; 648ea4e54b9SDavid Nugent else { 649ea4e54b9SDavid Nugent i = 0; 650ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 651ea4e54b9SDavid Nugent strcmp(cp, hp->h_aliases[i]) != 0) 652ea4e54b9SDavid Nugent ++i; 653ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 654ea4e54b9SDavid Nugent cp = hp->h_name; 655ea4e54b9SDavid Nugent } 656ea4e54b9SDavid Nugent } 657ea4e54b9SDavid Nugent } 658ea4e54b9SDavid Nugent hrp->hostname = strdup(cp); 659ea4e54b9SDavid Nugent /* ok, now we now peel off the rest */ 660ea4e54b9SDavid Nugent i = 0; 661ea4e54b9SDavid Nugent while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) { 662ea4e54b9SDavid Nugent if (*cp != '-' && (cp = strdup(cp)) != NULL) { 663ea4e54b9SDavid Nugent switch (i) { 664ea4e54b9SDavid Nugent case 0: /* anon user permissions */ 665ea4e54b9SDavid Nugent hrp->anonuser = cp; 666ea4e54b9SDavid Nugent break; 667ea4e54b9SDavid Nugent case 1: /* statistics file */ 668ea4e54b9SDavid Nugent hrp->statfile = cp; 669ea4e54b9SDavid Nugent break; 670ea4e54b9SDavid Nugent case 2: /* welcome message */ 671ea4e54b9SDavid Nugent hrp->welcome = cp; 672ea4e54b9SDavid Nugent break; 673ea4e54b9SDavid Nugent case 3: /* login message */ 674ea4e54b9SDavid Nugent hrp->loginmsg = cp; 675ea4e54b9SDavid Nugent break; 676ea4e54b9SDavid Nugent } 677ea4e54b9SDavid Nugent } 678ea4e54b9SDavid Nugent ++i; 679ea4e54b9SDavid Nugent } 680ea4e54b9SDavid Nugent } 681ea4e54b9SDavid Nugent (void) fclose(fp); 682ea4e54b9SDavid Nugent } 683ea4e54b9SDavid Nugent } 684ea4e54b9SDavid Nugent 685ea4e54b9SDavid Nugent static void 686ea4e54b9SDavid Nugent selecthost(a) 687ea4e54b9SDavid Nugent struct in_addr *a; 688ea4e54b9SDavid Nugent { 689ea4e54b9SDavid Nugent struct ftphost *hrp; 690ea4e54b9SDavid Nugent 691ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 692ea4e54b9SDavid Nugent while (hrp != NULL) { 693ea4e54b9SDavid Nugent if (memcmp(a, &hrp->hostaddr, sizeof(hrp->hostaddr)) == 0) { 694ea4e54b9SDavid Nugent thishost = hrp; 695ea4e54b9SDavid Nugent break; 696ea4e54b9SDavid Nugent } 697ea4e54b9SDavid Nugent hrp = hrp->next; 698ea4e54b9SDavid Nugent } 699ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 700ea4e54b9SDavid Nugent hostname = thishost->hostname; 701ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 702ea4e54b9SDavid Nugent } 703ea4e54b9SDavid Nugent #endif 704ea4e54b9SDavid Nugent 705ea022d16SRodney W. Grimes /* 706ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 707ea022d16SRodney W. Grimes */ 708ea022d16SRodney W. Grimes static char * 709ea022d16SRodney W. Grimes sgetsave(s) 710ea022d16SRodney W. Grimes char *s; 711ea022d16SRodney W. Grimes { 712ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 713ea022d16SRodney W. Grimes 714ea022d16SRodney W. Grimes if (new == NULL) { 715ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 716ea022d16SRodney W. Grimes dologout(1); 717ea022d16SRodney W. Grimes /* NOTREACHED */ 718ea022d16SRodney W. Grimes } 719ea022d16SRodney W. Grimes (void) strcpy(new, s); 720ea022d16SRodney W. Grimes return (new); 721ea022d16SRodney W. Grimes } 722ea022d16SRodney W. Grimes 723ea022d16SRodney W. Grimes /* 724ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 725ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 726ea022d16SRodney W. Grimes * (e.g., globbing). 727ea022d16SRodney W. Grimes */ 728ea022d16SRodney W. Grimes static struct passwd * 729ea022d16SRodney W. Grimes sgetpwnam(name) 730ea022d16SRodney W. Grimes char *name; 731ea022d16SRodney W. Grimes { 732ea022d16SRodney W. Grimes static struct passwd save; 733ea022d16SRodney W. Grimes struct passwd *p; 734ea022d16SRodney W. Grimes 735ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 736ea022d16SRodney W. Grimes return (p); 737ea022d16SRodney W. Grimes if (save.pw_name) { 738ea022d16SRodney W. Grimes free(save.pw_name); 739ea022d16SRodney W. Grimes free(save.pw_passwd); 740ea022d16SRodney W. Grimes free(save.pw_gecos); 741ea022d16SRodney W. Grimes free(save.pw_dir); 742ea022d16SRodney W. Grimes free(save.pw_shell); 743ea022d16SRodney W. Grimes } 744ea022d16SRodney W. Grimes save = *p; 745ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 746ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 747ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 748ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 749ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 750ea022d16SRodney W. Grimes return (&save); 751ea022d16SRodney W. Grimes } 752ea022d16SRodney W. Grimes 753ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 754ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 755ea022d16SRodney W. Grimes static char curname[10]; /* current USER name */ 756ea022d16SRodney W. Grimes 757ea022d16SRodney W. Grimes /* 758ea022d16SRodney W. Grimes * USER command. 759ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 760ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 761ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 762ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 763ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 764ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 765ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 766ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 767ea022d16SRodney W. Grimes */ 768ea022d16SRodney W. Grimes void 769ea022d16SRodney W. Grimes user(name) 770ea022d16SRodney W. Grimes char *name; 771ea022d16SRodney W. Grimes { 772ea022d16SRodney W. Grimes char *cp, *shell; 773ea022d16SRodney W. Grimes 774ea022d16SRodney W. Grimes if (logged_in) { 775ea022d16SRodney W. Grimes if (guest) { 776ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 777ea022d16SRodney W. Grimes return; 778a5a4544eSPaul Traina } else if (dochroot) { 779a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 780a5a4544eSPaul Traina return; 781ea022d16SRodney W. Grimes } 782ea022d16SRodney W. Grimes end_login(); 783ea022d16SRodney W. Grimes } 784ea022d16SRodney W. Grimes 785ea022d16SRodney W. Grimes guest = 0; 786ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 7877edcb936SSteve Price if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 7887edcb936SSteve Price checkuser(_PATH_FTPUSERS, "anonymous", 0)) 789ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 790ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 791ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 792ea4e54b9SDavid Nugent #else 793ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 794ea4e54b9SDavid Nugent #endif 795ea022d16SRodney W. Grimes guest = 1; 796ea022d16SRodney W. Grimes askpasswd = 1; 797ea022d16SRodney W. Grimes reply(331, 7982c60c54cSPaul Traina "Guest login ok, send your email address as password."); 799ea022d16SRodney W. Grimes } else 800ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 801ea022d16SRodney W. Grimes if (!askpasswd && logging) 802ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 803ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 804ea022d16SRodney W. Grimes return; 805ea022d16SRodney W. Grimes } 8065a392aecSTorsten Blum if (anon_only != 0) { 8075a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 8085a392aecSTorsten Blum return; 8095a392aecSTorsten Blum } 8105a392aecSTorsten Blum 8119aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 812ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 813ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 814ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 815ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 816ea022d16SRodney W. Grimes break; 817ea022d16SRodney W. Grimes endusershell(); 818ea022d16SRodney W. Grimes 8197edcb936SSteve Price if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 820ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 821ea022d16SRodney W. Grimes if (logging) 822ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 823ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 824ea022d16SRodney W. Grimes remotehost, name); 825ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 826ea022d16SRodney W. Grimes return; 827ea022d16SRodney W. Grimes } 828ea022d16SRodney W. Grimes } 829ea022d16SRodney W. Grimes if (logging) 830ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 831726040deSGuido van Rooij #ifdef SKEY 8322c60c54cSPaul Traina pwok = skeyaccess(name, NULL, remotehost, addr_string); 83343658eacSAndrey A. Chernov reply(331, "%s", skey_challenge(name, pw, pwok)); 834726040deSGuido van Rooij #else 835ea022d16SRodney W. Grimes reply(331, "Password required for %s.", name); 836726040deSGuido van Rooij #endif 837ea022d16SRodney W. Grimes askpasswd = 1; 838ea022d16SRodney W. Grimes /* 839ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 840ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 841ea022d16SRodney W. Grimes */ 842ea022d16SRodney W. Grimes if (login_attempts) 843ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 844ea022d16SRodney W. Grimes } 845ea022d16SRodney W. Grimes 846ea022d16SRodney W. Grimes /* 847a5a4544eSPaul Traina * Check if a user is in the file "fname" 848ea022d16SRodney W. Grimes */ 849ea022d16SRodney W. Grimes static int 8507edcb936SSteve Price checkuser(fname, name, pwset) 851a5a4544eSPaul Traina char *fname; 852ea022d16SRodney W. Grimes char *name; 8537edcb936SSteve Price int pwset; 854ea022d16SRodney W. Grimes { 855ea022d16SRodney W. Grimes FILE *fd; 856ea022d16SRodney W. Grimes int found = 0; 857ea022d16SRodney W. Grimes char *p, line[BUFSIZ]; 858ea022d16SRodney W. Grimes 859a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 86031fea7b8SDavid Nugent while (!found && fgets(line, sizeof(line), fd) != NULL) 861ea022d16SRodney W. Grimes if ((p = strchr(line, '\n')) != NULL) { 862ea022d16SRodney W. Grimes *p = '\0'; 863ea022d16SRodney W. Grimes if (line[0] == '#') 864ea022d16SRodney W. Grimes continue; 86531fea7b8SDavid Nugent /* 86631fea7b8SDavid Nugent * if first chr is '@', check group membership 86731fea7b8SDavid Nugent */ 86831fea7b8SDavid Nugent if (line[0] == '@') { 86931fea7b8SDavid Nugent int i = 0; 87031fea7b8SDavid Nugent struct group *grp; 87131fea7b8SDavid Nugent 87231fea7b8SDavid Nugent if ((grp = getgrnam(line+1)) == NULL) 87331fea7b8SDavid Nugent continue; 8747edcb936SSteve Price /* 8757edcb936SSteve Price * Check user's default group 8767edcb936SSteve Price */ 8777edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 8787edcb936SSteve Price found = 1; 8797edcb936SSteve Price /* 8807edcb936SSteve Price * Check supplementary groups 8817edcb936SSteve Price */ 88231fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 88331fea7b8SDavid Nugent found = strcmp(name, 88431fea7b8SDavid Nugent grp->gr_mem[i++]) 88531fea7b8SDavid Nugent == 0; 886ea022d16SRodney W. Grimes } 88731fea7b8SDavid Nugent /* 88831fea7b8SDavid Nugent * Otherwise, just check for username match 88931fea7b8SDavid Nugent */ 89031fea7b8SDavid Nugent else 89131fea7b8SDavid Nugent found = strcmp(line, name) == 0; 892ea022d16SRodney W. Grimes } 893ea022d16SRodney W. Grimes (void) fclose(fd); 894ea022d16SRodney W. Grimes } 895ea022d16SRodney W. Grimes return (found); 896ea022d16SRodney W. Grimes } 897ea022d16SRodney W. Grimes 898ea022d16SRodney W. Grimes /* 899ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 900ea022d16SRodney W. Grimes * used when USER command is given or login fails. 901ea022d16SRodney W. Grimes */ 902ea022d16SRodney W. Grimes static void 903ea022d16SRodney W. Grimes end_login() 904ea022d16SRodney W. Grimes { 905ea022d16SRodney W. Grimes 906ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 907ea022d16SRodney W. Grimes if (logged_in) 908986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 909ea022d16SRodney W. Grimes pw = NULL; 910b071c689SDavid Nugent #ifdef LOGIN_CAP 911b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 912b071c689SDavid Nugent LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 913b071c689SDavid Nugent #endif 914ea022d16SRodney W. Grimes logged_in = 0; 915ea022d16SRodney W. Grimes guest = 0; 916a5a4544eSPaul Traina dochroot = 0; 917ea022d16SRodney W. Grimes } 918ea022d16SRodney W. Grimes 919ea022d16SRodney W. Grimes void 920ea022d16SRodney W. Grimes pass(passwd) 921ea022d16SRodney W. Grimes char *passwd; 922ea022d16SRodney W. Grimes { 923a5a4544eSPaul Traina int rval; 924ea022d16SRodney W. Grimes FILE *fd; 925b071c689SDavid Nugent #ifdef LOGIN_CAP 926b071c689SDavid Nugent login_cap_t *lc = NULL; 927b071c689SDavid Nugent #endif 92882c76939SDavid Greenman static char homedir[MAXPATHLEN]; 929ea022d16SRodney W. Grimes 930ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 931ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 932ea022d16SRodney W. Grimes return; 933ea022d16SRodney W. Grimes } 934ea022d16SRodney W. Grimes askpasswd = 0; 935ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 936a5a4544eSPaul Traina if (pw == NULL) { 937a5a4544eSPaul Traina rval = 1; /* failure below */ 938a5a4544eSPaul Traina goto skip; 939a5a4544eSPaul Traina } 940a5a4544eSPaul Traina #if defined(KERBEROS) 941a5a4544eSPaul Traina rval = klogin(pw, "", hostname, passwd); 942a5a4544eSPaul Traina if (rval == 0) 943a5a4544eSPaul Traina goto skip; 944a5a4544eSPaul Traina #endif 945726040deSGuido van Rooij #ifdef SKEY 946a5a4544eSPaul Traina rval = strcmp(skey_crypt(passwd, pw->pw_passwd, pw, pwok), 9470bb6e9edSPoul-Henning Kamp pw->pw_passwd); 948bb56d435SPaul Traina pwok = 0; 949726040deSGuido van Rooij #else 9503cde2031SPoul-Henning Kamp rval = strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd); 951726040deSGuido van Rooij #endif 952ea022d16SRodney W. Grimes /* The strcmp does not catch null passwords! */ 953a5a4544eSPaul Traina if (*pw->pw_passwd == '\0' || 954a5a4544eSPaul Traina (pw->pw_expire && time(NULL) >= pw->pw_expire)) 955a5a4544eSPaul Traina rval = 1; /* failure */ 956a5a4544eSPaul Traina skip: 957a5a4544eSPaul Traina /* 958a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 959a5a4544eSPaul Traina * above. If rval == 0, either Kerberos or local authentication 960a5a4544eSPaul Traina * succeeded. 961a5a4544eSPaul Traina */ 962a5a4544eSPaul Traina if (rval) { 963ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 964ea022d16SRodney W. Grimes if (logging) 965ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 966ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 967ea022d16SRodney W. Grimes remotehost, curname); 968ea022d16SRodney W. Grimes pw = NULL; 969ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 970ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 971ea022d16SRodney W. Grimes "repeated login failures from %s", 972ea022d16SRodney W. Grimes remotehost); 973ea022d16SRodney W. Grimes exit(0); 974ea022d16SRodney W. Grimes } 975ea022d16SRodney W. Grimes return; 976ea022d16SRodney W. Grimes } 977ea022d16SRodney W. Grimes } 978ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 979ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 980ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 981ea022d16SRodney W. Grimes return; 982ea022d16SRodney W. Grimes } 983b071c689SDavid Nugent /* May be overridden by login.conf */ 984b071c689SDavid Nugent (void) umask(defumask); 985b071c689SDavid Nugent #ifdef LOGIN_CAP 9865d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 987b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 988b071c689SDavid Nugent 989b071c689SDavid Nugent strncpy(remote_ip, inet_ntoa(his_addr.sin_addr), 990b071c689SDavid Nugent sizeof(remote_ip) - 1); 991b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 992b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 993b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 994b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 995b071c689SDavid Nugent pw->pw_name); 996b071c689SDavid Nugent reply(530, "Permission denied.\n"); 997b071c689SDavid Nugent pw = NULL; 998b071c689SDavid Nugent return; 999b071c689SDavid Nugent } 1000b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1001b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1002b071c689SDavid Nugent pw = NULL; 1003b071c689SDavid Nugent return; 1004b071c689SDavid Nugent } 1005b071c689SDavid Nugent } 1006b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1007b071c689SDavid Nugent LOGIN_SETGROUP|LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1008b071c689SDavid Nugent #else 1009ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1010b071c689SDavid Nugent #endif 1011ea022d16SRodney W. Grimes 1012ea022d16SRodney W. Grimes /* open wtmp before chroot */ 1013986a1172SThomas Gellekum ftpd_logwtmp(ttyline, pw->pw_name, remotehost); 1014ea022d16SRodney W. Grimes logged_in = 1; 1015ea022d16SRodney W. Grimes 1016a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1017ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1018ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1019ea4e54b9SDavid Nugent #else 10203eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1021ea4e54b9SDavid Nugent #endif 10223eb568f2SGuido van Rooij stats = 0; 10233eb568f2SGuido van Rooij 1024b071c689SDavid Nugent dochroot = 1025b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1026b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 1027b071c689SDavid Nugent #endif 10287edcb936SSteve Price checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1029ea022d16SRodney W. Grimes if (guest) { 1030ea022d16SRodney W. Grimes /* 1031ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 1032ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 1033ea022d16SRodney W. Grimes * outside the new root! 1034ea022d16SRodney W. Grimes */ 1035ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1036ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 1037ea022d16SRodney W. Grimes goto bad; 1038ea022d16SRodney W. Grimes } 1039a5a4544eSPaul Traina } else if (dochroot) { 1040a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1041a5a4544eSPaul Traina reply(550, "Can't change root."); 1042a5a4544eSPaul Traina goto bad; 1043a5a4544eSPaul Traina } 1044ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 1045ea022d16SRodney W. Grimes if (chdir("/") < 0) { 1046ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 1047ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 1048ea022d16SRodney W. Grimes goto bad; 1049ea022d16SRodney W. Grimes } else 1050ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 1051ea022d16SRodney W. Grimes } 1052ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1053ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1054ea022d16SRodney W. Grimes goto bad; 1055ea022d16SRodney W. Grimes } 105682c76939SDavid Greenman 105782c76939SDavid Greenman /* 105882c76939SDavid Greenman * Set home directory so that use of ~ (tilde) works correctly. 105982c76939SDavid Greenman */ 106095645563SDavid Greenman if (getcwd(homedir, MAXPATHLEN) != NULL) 106195645563SDavid Greenman setenv("HOME", homedir, 1); 106282c76939SDavid Greenman 1063ea022d16SRodney W. Grimes /* 1064ea022d16SRodney W. Grimes * Display a login message, if it exists. 1065ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1066ea022d16SRodney W. Grimes */ 1067ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1068ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1069ea4e54b9SDavid Nugent #else 1070ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1071ea4e54b9SDavid Nugent #endif 1072ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1073ea022d16SRodney W. Grimes 1074ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1075ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1076ea022d16SRodney W. Grimes *cp = '\0'; 1077ea022d16SRodney W. Grimes lreply(230, "%s", line); 1078ea022d16SRodney W. Grimes } 1079ea022d16SRodney W. Grimes (void) fflush(stdout); 1080ea022d16SRodney W. Grimes (void) fclose(fd); 1081ea022d16SRodney W. Grimes } 1082ea022d16SRodney W. Grimes if (guest) { 10833eb568f2SGuido van Rooij if (ident != NULL) 10843eb568f2SGuido van Rooij free(ident); 108561f891a6SPaul Traina ident = strdup(passwd); 108661f891a6SPaul Traina if (ident == NULL) 108761f891a6SPaul Traina fatal("Ran out of memory."); 108861f891a6SPaul Traina 1089ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1090ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1091ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1092ea4e54b9SDavid Nugent if (thishost != firsthost) 1093ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1094ea4e54b9SDavid Nugent "%s: anonymous(%s)/%.*s", remotehost, hostname, 1095ea4e54b9SDavid Nugent sizeof(proctitle) - sizeof(remotehost) - 1096ea4e54b9SDavid Nugent sizeof(": anonymous/"), passwd); 1097ea4e54b9SDavid Nugent else 1098ea4e54b9SDavid Nugent #endif 1099ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1100ea022d16SRodney W. Grimes "%s: anonymous/%.*s", remotehost, 1101ea022d16SRodney W. Grimes sizeof(proctitle) - sizeof(remotehost) - 1102ea022d16SRodney W. Grimes sizeof(": anonymous/"), passwd); 1103b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1104ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1105ea022d16SRodney W. Grimes if (logging) 1106ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1107ea022d16SRodney W. Grimes remotehost, passwd); 1108ea022d16SRodney W. Grimes } else { 11093401a71fSDaniel O'Callaghan if (dochroot) 11103401a71fSDaniel O'Callaghan reply(230, "User %s logged in, access restrictions apply.", 11113401a71fSDaniel O'Callaghan pw->pw_name); 11123401a71fSDaniel O'Callaghan else 1113ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 11143401a71fSDaniel O'Callaghan 1115ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1116ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1117ea022d16SRodney W. Grimes "%s: %s", remotehost, pw->pw_name); 1118b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1119ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1120ea022d16SRodney W. Grimes if (logging) 1121ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1122ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1123ea022d16SRodney W. Grimes } 1124b071c689SDavid Nugent #ifdef LOGIN_CAP 1125b071c689SDavid Nugent login_close(lc); 1126b071c689SDavid Nugent #endif 1127ea022d16SRodney W. Grimes return; 1128ea022d16SRodney W. Grimes bad: 1129ea022d16SRodney W. Grimes /* Forget all about it... */ 1130b071c689SDavid Nugent #ifdef LOGIN_CAP 1131b071c689SDavid Nugent login_close(lc); 1132b071c689SDavid Nugent #endif 1133ea022d16SRodney W. Grimes end_login(); 1134ea022d16SRodney W. Grimes } 1135ea022d16SRodney W. Grimes 1136ea022d16SRodney W. Grimes void 1137ea022d16SRodney W. Grimes retrieve(cmd, name) 1138ea022d16SRodney W. Grimes char *cmd, *name; 1139ea022d16SRodney W. Grimes { 1140ea022d16SRodney W. Grimes FILE *fin, *dout; 1141ea022d16SRodney W. Grimes struct stat st; 1142ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1143158a00b2SJohn Birrell time_t start; 1144ea022d16SRodney W. Grimes 1145ea022d16SRodney W. Grimes if (cmd == 0) { 1146ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1147ea022d16SRodney W. Grimes st.st_size = 0; 1148ea022d16SRodney W. Grimes } else { 1149ea022d16SRodney W. Grimes char line[BUFSIZ]; 1150ea022d16SRodney W. Grimes 1151e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1152ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1153ea022d16SRodney W. Grimes st.st_size = -1; 1154ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1155ea022d16SRodney W. Grimes } 1156ea022d16SRodney W. Grimes if (fin == NULL) { 1157ea022d16SRodney W. Grimes if (errno != 0) { 1158ea022d16SRodney W. Grimes perror_reply(550, name); 1159ea022d16SRodney W. Grimes if (cmd == 0) { 1160ea022d16SRodney W. Grimes LOGCMD("get", name); 1161ea022d16SRodney W. Grimes } 1162ea022d16SRodney W. Grimes } 1163ea022d16SRodney W. Grimes return; 1164ea022d16SRodney W. Grimes } 1165ea022d16SRodney W. Grimes byte_count = -1; 1166ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1167ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1168ea022d16SRodney W. Grimes goto done; 1169ea022d16SRodney W. Grimes } 1170ea022d16SRodney W. Grimes if (restart_point) { 1171ea022d16SRodney W. Grimes if (type == TYPE_A) { 1172ea022d16SRodney W. Grimes off_t i, n; 1173ea022d16SRodney W. Grimes int c; 1174ea022d16SRodney W. Grimes 1175ea022d16SRodney W. Grimes n = restart_point; 1176ea022d16SRodney W. Grimes i = 0; 1177ea022d16SRodney W. Grimes while (i++ < n) { 1178ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1179ea022d16SRodney W. Grimes perror_reply(550, name); 1180ea022d16SRodney W. Grimes goto done; 1181ea022d16SRodney W. Grimes } 1182ea022d16SRodney W. Grimes if (c == '\n') 1183ea022d16SRodney W. Grimes i++; 1184ea022d16SRodney W. Grimes } 1185ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1186ea022d16SRodney W. Grimes perror_reply(550, name); 1187ea022d16SRodney W. Grimes goto done; 1188ea022d16SRodney W. Grimes } 1189ea022d16SRodney W. Grimes } 1190ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1191ea022d16SRodney W. Grimes if (dout == NULL) 1192ea022d16SRodney W. Grimes goto done; 11933eb568f2SGuido van Rooij time(&start); 11949fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 11959fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 11963eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 11973eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1198ea022d16SRodney W. Grimes (void) fclose(dout); 1199ea022d16SRodney W. Grimes data = -1; 1200ea022d16SRodney W. Grimes pdata = -1; 1201ea022d16SRodney W. Grimes done: 1202ea022d16SRodney W. Grimes if (cmd == 0) 1203ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1204ea022d16SRodney W. Grimes (*closefunc)(fin); 1205ea022d16SRodney W. Grimes } 1206ea022d16SRodney W. Grimes 1207ea022d16SRodney W. Grimes void 1208ea022d16SRodney W. Grimes store(name, mode, unique) 1209ea022d16SRodney W. Grimes char *name, *mode; 1210ea022d16SRodney W. Grimes int unique; 1211ea022d16SRodney W. Grimes { 1212ea022d16SRodney W. Grimes FILE *fout, *din; 1213ea022d16SRodney W. Grimes struct stat st; 1214ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1215ea022d16SRodney W. Grimes 121661f891a6SPaul Traina if ((unique || guest) && stat(name, &st) == 0 && 1217ea022d16SRodney W. Grimes (name = gunique(name)) == NULL) { 1218ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1219ea022d16SRodney W. Grimes return; 1220ea022d16SRodney W. Grimes } 1221ea022d16SRodney W. Grimes 1222ea022d16SRodney W. Grimes if (restart_point) 1223ea022d16SRodney W. Grimes mode = "r+"; 1224ea022d16SRodney W. Grimes fout = fopen(name, mode); 1225ea022d16SRodney W. Grimes closefunc = fclose; 1226ea022d16SRodney W. Grimes if (fout == NULL) { 1227ea022d16SRodney W. Grimes perror_reply(553, name); 1228ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1229ea022d16SRodney W. Grimes return; 1230ea022d16SRodney W. Grimes } 1231ea022d16SRodney W. Grimes byte_count = -1; 1232ea022d16SRodney W. Grimes if (restart_point) { 1233ea022d16SRodney W. Grimes if (type == TYPE_A) { 1234ea022d16SRodney W. Grimes off_t i, n; 1235ea022d16SRodney W. Grimes int c; 1236ea022d16SRodney W. Grimes 1237ea022d16SRodney W. Grimes n = restart_point; 1238ea022d16SRodney W. Grimes i = 0; 1239ea022d16SRodney W. Grimes while (i++ < n) { 1240ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1241ea022d16SRodney W. Grimes perror_reply(550, name); 1242ea022d16SRodney W. Grimes goto done; 1243ea022d16SRodney W. Grimes } 1244ea022d16SRodney W. Grimes if (c == '\n') 1245ea022d16SRodney W. Grimes i++; 1246ea022d16SRodney W. Grimes } 1247ea022d16SRodney W. Grimes /* 1248ea022d16SRodney W. Grimes * We must do this seek to "current" position 1249ea022d16SRodney W. Grimes * because we are changing from reading to 1250ea022d16SRodney W. Grimes * writing. 1251ea022d16SRodney W. Grimes */ 1252ea022d16SRodney W. Grimes if (fseek(fout, 0L, L_INCR) < 0) { 1253ea022d16SRodney W. Grimes perror_reply(550, name); 1254ea022d16SRodney W. Grimes goto done; 1255ea022d16SRodney W. Grimes } 1256ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1257ea022d16SRodney W. Grimes perror_reply(550, name); 1258ea022d16SRodney W. Grimes goto done; 1259ea022d16SRodney W. Grimes } 1260ea022d16SRodney W. Grimes } 1261ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1262ea022d16SRodney W. Grimes if (din == NULL) 1263ea022d16SRodney W. Grimes goto done; 1264ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1265ea022d16SRodney W. Grimes if (unique) 1266ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1267ea022d16SRodney W. Grimes name); 1268ea022d16SRodney W. Grimes else 1269ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1270ea022d16SRodney W. Grimes } 1271ea022d16SRodney W. Grimes (void) fclose(din); 1272ea022d16SRodney W. Grimes data = -1; 1273ea022d16SRodney W. Grimes pdata = -1; 1274ea022d16SRodney W. Grimes done: 1275ea022d16SRodney W. Grimes LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1276ea022d16SRodney W. Grimes (*closefunc)(fout); 1277ea022d16SRodney W. Grimes } 1278ea022d16SRodney W. Grimes 1279ea022d16SRodney W. Grimes static FILE * 1280ea022d16SRodney W. Grimes getdatasock(mode) 1281ea022d16SRodney W. Grimes char *mode; 1282ea022d16SRodney W. Grimes { 1283ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1284ea022d16SRodney W. Grimes 1285ea022d16SRodney W. Grimes if (data >= 0) 1286ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1287ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1288ea022d16SRodney W. Grimes s = socket(AF_INET, SOCK_STREAM, 0); 1289ea022d16SRodney W. Grimes if (s < 0) 1290ea022d16SRodney W. Grimes goto bad; 1291ea022d16SRodney W. Grimes if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1292ea022d16SRodney W. Grimes (char *) &on, sizeof(on)) < 0) 1293ea022d16SRodney W. Grimes goto bad; 1294ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 1295a5a4544eSPaul Traina data_source.sin_len = sizeof(struct sockaddr_in); 1296ea022d16SRodney W. Grimes data_source.sin_family = AF_INET; 1297ea022d16SRodney W. Grimes data_source.sin_addr = ctrl_addr.sin_addr; 1298ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1299ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 1300ea022d16SRodney W. Grimes sizeof(data_source)) >= 0) 1301ea022d16SRodney W. Grimes break; 1302ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1303ea022d16SRodney W. Grimes goto bad; 1304ea022d16SRodney W. Grimes sleep(tries); 1305ea022d16SRodney W. Grimes } 1306ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1307ea022d16SRodney W. Grimes #ifdef IP_TOS 1308ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 1309ea022d16SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1310ea022d16SRodney W. Grimes syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 1311ea022d16SRodney W. Grimes #endif 13129fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 13139fc5823aSGarrett Wollman /* 13149fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 13159fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 13169fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 13179fc5823aSGarrett Wollman * in heavy-load situations. 13189fc5823aSGarrett Wollman */ 13199fc5823aSGarrett Wollman on = 1; 13209fc5823aSGarrett Wollman if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 13219fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 13229fc5823aSGarrett Wollman #endif 13239fc5823aSGarrett Wollman #ifdef SO_SNDBUF 13249fc5823aSGarrett Wollman on = 65536; 13259fc5823aSGarrett Wollman if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 13269fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 13279fc5823aSGarrett Wollman #endif 13289fc5823aSGarrett Wollman 1329ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1330ea022d16SRodney W. Grimes bad: 1331ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1332ea022d16SRodney W. Grimes t = errno; 1333ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1334ea022d16SRodney W. Grimes (void) close(s); 1335ea022d16SRodney W. Grimes errno = t; 1336ea022d16SRodney W. Grimes return (NULL); 1337ea022d16SRodney W. Grimes } 1338ea022d16SRodney W. Grimes 1339ea022d16SRodney W. Grimes static FILE * 1340ea022d16SRodney W. Grimes dataconn(name, size, mode) 1341ea022d16SRodney W. Grimes char *name; 1342ea022d16SRodney W. Grimes off_t size; 1343ea022d16SRodney W. Grimes char *mode; 1344ea022d16SRodney W. Grimes { 1345ea022d16SRodney W. Grimes char sizebuf[32]; 1346ea022d16SRodney W. Grimes FILE *file; 1347ea022d16SRodney W. Grimes int retry = 0, tos; 1348ea022d16SRodney W. Grimes 1349ea022d16SRodney W. Grimes file_size = size; 1350ea022d16SRodney W. Grimes byte_count = 0; 1351ea022d16SRodney W. Grimes if (size != (off_t) -1) 1352e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1353ea022d16SRodney W. Grimes else 1354e760ef2cSWarner Losh *sizebuf = '\0'; 1355ea022d16SRodney W. Grimes if (pdata >= 0) { 1356ea022d16SRodney W. Grimes struct sockaddr_in from; 1357ea022d16SRodney W. Grimes int s, fromlen = sizeof(from); 1358d6ed3c37SGuido van Rooij struct timeval timeout; 1359d6ed3c37SGuido van Rooij fd_set set; 1360ea022d16SRodney W. Grimes 1361d6ed3c37SGuido van Rooij FD_ZERO(&set); 1362d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1363d6ed3c37SGuido van Rooij 1364d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1365d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1366d6ed3c37SGuido van Rooij 1367d6ed3c37SGuido van Rooij if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1368d6ed3c37SGuido van Rooij (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1369ea022d16SRodney W. Grimes reply(425, "Can't open data connection."); 1370ea022d16SRodney W. Grimes (void) close(pdata); 1371ea022d16SRodney W. Grimes pdata = -1; 1372ea022d16SRodney W. Grimes return (NULL); 1373ea022d16SRodney W. Grimes } 1374ea022d16SRodney W. Grimes (void) close(pdata); 1375ea022d16SRodney W. Grimes pdata = s; 1376ea022d16SRodney W. Grimes #ifdef IP_TOS 1377a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 1378ea022d16SRodney W. Grimes (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1379ea022d16SRodney W. Grimes sizeof(int)); 1380ea022d16SRodney W. Grimes #endif 1381ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1382ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1383ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 1384ea022d16SRodney W. Grimes } 1385ea022d16SRodney W. Grimes if (data >= 0) { 1386ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1387ea022d16SRodney W. Grimes name, sizebuf); 1388ea022d16SRodney W. Grimes usedefault = 1; 1389ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1390ea022d16SRodney W. Grimes } 1391ea022d16SRodney W. Grimes if (usedefault) 1392ea022d16SRodney W. Grimes data_dest = his_addr; 1393ea022d16SRodney W. Grimes usedefault = 1; 1394ea022d16SRodney W. Grimes file = getdatasock(mode); 1395ea022d16SRodney W. Grimes if (file == NULL) { 1396ea022d16SRodney W. Grimes reply(425, "Can't create data socket (%s,%d): %s.", 1397ea022d16SRodney W. Grimes inet_ntoa(data_source.sin_addr), 1398ea022d16SRodney W. Grimes ntohs(data_source.sin_port), strerror(errno)); 1399ea022d16SRodney W. Grimes return (NULL); 1400ea022d16SRodney W. Grimes } 1401ea022d16SRodney W. Grimes data = fileno(file); 1402ea022d16SRodney W. Grimes while (connect(data, (struct sockaddr *)&data_dest, 1403ea022d16SRodney W. Grimes sizeof(data_dest)) < 0) { 1404ea022d16SRodney W. Grimes if (errno == EADDRINUSE && retry < swaitmax) { 1405ea022d16SRodney W. Grimes sleep((unsigned) swaitint); 1406ea022d16SRodney W. Grimes retry += swaitint; 1407ea022d16SRodney W. Grimes continue; 1408ea022d16SRodney W. Grimes } 1409ea022d16SRodney W. Grimes perror_reply(425, "Can't build data connection"); 1410ea022d16SRodney W. Grimes (void) fclose(file); 1411ea022d16SRodney W. Grimes data = -1; 1412ea022d16SRodney W. Grimes return (NULL); 1413ea022d16SRodney W. Grimes } 1414ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1415ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1416ea022d16SRodney W. Grimes return (file); 1417ea022d16SRodney W. Grimes } 1418ea022d16SRodney W. Grimes 1419ea022d16SRodney W. Grimes /* 1420ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 14219fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1422ea022d16SRodney W. Grimes * 1423ea022d16SRodney W. Grimes * NB: Form isn't handled. 1424ea022d16SRodney W. Grimes */ 1425ea022d16SRodney W. Grimes static void 14269fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg) 1427ea022d16SRodney W. Grimes FILE *instr, *outstr; 1428ea022d16SRodney W. Grimes off_t blksize; 14299fc5823aSGarrett Wollman off_t filesize; 14309fc5823aSGarrett Wollman int isreg; 1431ea022d16SRodney W. Grimes { 1432ea022d16SRodney W. Grimes int c, cnt, filefd, netfd; 14339fc5823aSGarrett Wollman char *buf, *bp; 14349fc5823aSGarrett Wollman size_t len; 1435ea022d16SRodney W. Grimes 1436ea022d16SRodney W. Grimes transflag++; 1437ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1438ea022d16SRodney W. Grimes transflag = 0; 1439ea022d16SRodney W. Grimes return; 1440ea022d16SRodney W. Grimes } 1441ea022d16SRodney W. Grimes switch (type) { 1442ea022d16SRodney W. Grimes 1443ea022d16SRodney W. Grimes case TYPE_A: 1444ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1445ea022d16SRodney W. Grimes byte_count++; 1446ea022d16SRodney W. Grimes if (c == '\n') { 1447ea022d16SRodney W. Grimes if (ferror(outstr)) 1448ea022d16SRodney W. Grimes goto data_err; 1449ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1450ea022d16SRodney W. Grimes } 1451ea022d16SRodney W. Grimes (void) putc(c, outstr); 1452ea022d16SRodney W. Grimes } 1453ea022d16SRodney W. Grimes fflush(outstr); 1454ea022d16SRodney W. Grimes transflag = 0; 1455ea022d16SRodney W. Grimes if (ferror(instr)) 1456ea022d16SRodney W. Grimes goto file_err; 1457ea022d16SRodney W. Grimes if (ferror(outstr)) 1458ea022d16SRodney W. Grimes goto data_err; 1459ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1460ea022d16SRodney W. Grimes return; 1461ea022d16SRodney W. Grimes 1462ea022d16SRodney W. Grimes case TYPE_I: 1463ea022d16SRodney W. Grimes case TYPE_L: 14649fc5823aSGarrett Wollman /* 14659fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 14669fc5823aSGarrett Wollman * are sending a regular file 14679fc5823aSGarrett Wollman */ 14689fc5823aSGarrett Wollman netfd = fileno(outstr); 14699fc5823aSGarrett Wollman filefd = fileno(instr); 14709fc5823aSGarrett Wollman 14719fc5823aSGarrett Wollman if (isreg && filesize < (off_t)16 * 1024 * 1024) { 14729fc5823aSGarrett Wollman buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, 14739fc5823aSGarrett Wollman (off_t)0); 14748abdc2ebSAlexander Langer if (buf == MAP_FAILED) { 14759fc5823aSGarrett Wollman syslog(LOG_WARNING, "mmap(%lu): %m", 14769fc5823aSGarrett Wollman (unsigned long)filesize); 14779fc5823aSGarrett Wollman goto oldway; 14789fc5823aSGarrett Wollman } 14799fc5823aSGarrett Wollman bp = buf; 14809fc5823aSGarrett Wollman len = filesize; 14819fc5823aSGarrett Wollman do { 14829fc5823aSGarrett Wollman cnt = write(netfd, bp, len); 14839fc5823aSGarrett Wollman len -= cnt; 14849fc5823aSGarrett Wollman bp += cnt; 14859fc5823aSGarrett Wollman if (cnt > 0) byte_count += cnt; 14869fc5823aSGarrett Wollman } while(cnt > 0 && len > 0); 14879fc5823aSGarrett Wollman 14889fc5823aSGarrett Wollman transflag = 0; 14899fc5823aSGarrett Wollman munmap(buf, (size_t)filesize); 14909fc5823aSGarrett Wollman if (cnt < 0) 14919fc5823aSGarrett Wollman goto data_err; 14929fc5823aSGarrett Wollman reply(226, "Transfer complete."); 14939fc5823aSGarrett Wollman return; 14949fc5823aSGarrett Wollman } 14959fc5823aSGarrett Wollman 14969fc5823aSGarrett Wollman oldway: 1497ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1498ea022d16SRodney W. Grimes transflag = 0; 1499ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 1500ea022d16SRodney W. Grimes return; 1501ea022d16SRodney W. Grimes } 15029fc5823aSGarrett Wollman 1503ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1504ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1505ea022d16SRodney W. Grimes byte_count += cnt; 1506ea022d16SRodney W. Grimes transflag = 0; 1507ea022d16SRodney W. Grimes (void)free(buf); 1508ea022d16SRodney W. Grimes if (cnt != 0) { 1509ea022d16SRodney W. Grimes if (cnt < 0) 1510ea022d16SRodney W. Grimes goto file_err; 1511ea022d16SRodney W. Grimes goto data_err; 1512ea022d16SRodney W. Grimes } 1513ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1514ea022d16SRodney W. Grimes return; 1515ea022d16SRodney W. Grimes default: 1516ea022d16SRodney W. Grimes transflag = 0; 1517ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 1518ea022d16SRodney W. Grimes return; 1519ea022d16SRodney W. Grimes } 1520ea022d16SRodney W. Grimes 1521ea022d16SRodney W. Grimes data_err: 1522ea022d16SRodney W. Grimes transflag = 0; 1523ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 1524ea022d16SRodney W. Grimes return; 1525ea022d16SRodney W. Grimes 1526ea022d16SRodney W. Grimes file_err: 1527ea022d16SRodney W. Grimes transflag = 0; 1528ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 1529ea022d16SRodney W. Grimes } 1530ea022d16SRodney W. Grimes 1531ea022d16SRodney W. Grimes /* 1532ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 1533ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 1534ea022d16SRodney W. Grimes * 1535ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 1536ea022d16SRodney W. Grimes */ 1537ea022d16SRodney W. Grimes static int 1538ea022d16SRodney W. Grimes receive_data(instr, outstr) 1539ea022d16SRodney W. Grimes FILE *instr, *outstr; 1540ea022d16SRodney W. Grimes { 1541ea022d16SRodney W. Grimes int c; 154261f891a6SPaul Traina int cnt, bare_lfs; 1543ea022d16SRodney W. Grimes char buf[BUFSIZ]; 1544ea022d16SRodney W. Grimes 1545ea022d16SRodney W. Grimes transflag++; 1546ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1547ea022d16SRodney W. Grimes transflag = 0; 1548ea022d16SRodney W. Grimes return (-1); 1549ea022d16SRodney W. Grimes } 155061f891a6SPaul Traina 155161f891a6SPaul Traina bare_lfs = 0; 155261f891a6SPaul Traina 1553ea022d16SRodney W. Grimes switch (type) { 1554ea022d16SRodney W. Grimes 1555ea022d16SRodney W. Grimes case TYPE_I: 1556ea022d16SRodney W. Grimes case TYPE_L: 1557ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1558ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 1559ea022d16SRodney W. Grimes goto file_err; 1560ea022d16SRodney W. Grimes byte_count += cnt; 1561ea022d16SRodney W. Grimes } 1562ea022d16SRodney W. Grimes if (cnt < 0) 1563ea022d16SRodney W. Grimes goto data_err; 1564ea022d16SRodney W. Grimes transflag = 0; 1565ea022d16SRodney W. Grimes return (0); 1566ea022d16SRodney W. Grimes 1567ea022d16SRodney W. Grimes case TYPE_E: 1568ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 1569ea022d16SRodney W. Grimes transflag = 0; 1570ea022d16SRodney W. Grimes return (-1); 1571ea022d16SRodney W. Grimes 1572ea022d16SRodney W. Grimes case TYPE_A: 1573ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1574ea022d16SRodney W. Grimes byte_count++; 1575ea022d16SRodney W. Grimes if (c == '\n') 1576ea022d16SRodney W. Grimes bare_lfs++; 1577ea022d16SRodney W. Grimes while (c == '\r') { 1578ea022d16SRodney W. Grimes if (ferror(outstr)) 1579ea022d16SRodney W. Grimes goto data_err; 1580ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 1581ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 1582ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 1583ea022d16SRodney W. Grimes goto contin2; 1584ea022d16SRodney W. Grimes } 1585ea022d16SRodney W. Grimes } 1586ea022d16SRodney W. Grimes (void) putc(c, outstr); 1587ea022d16SRodney W. Grimes contin2: ; 1588ea022d16SRodney W. Grimes } 1589ea022d16SRodney W. Grimes fflush(outstr); 1590ea022d16SRodney W. Grimes if (ferror(instr)) 1591ea022d16SRodney W. Grimes goto data_err; 1592ea022d16SRodney W. Grimes if (ferror(outstr)) 1593ea022d16SRodney W. Grimes goto file_err; 1594ea022d16SRodney W. Grimes transflag = 0; 1595ea022d16SRodney W. Grimes if (bare_lfs) { 1596ea022d16SRodney W. Grimes lreply(226, 1597ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 1598ea022d16SRodney W. Grimes bare_lfs); 1599ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 1600ea022d16SRodney W. Grimes } 1601ea022d16SRodney W. Grimes return (0); 1602ea022d16SRodney W. Grimes default: 1603ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 1604ea022d16SRodney W. Grimes transflag = 0; 1605ea022d16SRodney W. Grimes return (-1); 1606ea022d16SRodney W. Grimes } 1607ea022d16SRodney W. Grimes 1608ea022d16SRodney W. Grimes data_err: 1609ea022d16SRodney W. Grimes transflag = 0; 1610ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 1611ea022d16SRodney W. Grimes return (-1); 1612ea022d16SRodney W. Grimes 1613ea022d16SRodney W. Grimes file_err: 1614ea022d16SRodney W. Grimes transflag = 0; 1615ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 1616ea022d16SRodney W. Grimes return (-1); 1617ea022d16SRodney W. Grimes } 1618ea022d16SRodney W. Grimes 1619ea022d16SRodney W. Grimes void 1620ea022d16SRodney W. Grimes statfilecmd(filename) 1621ea022d16SRodney W. Grimes char *filename; 1622ea022d16SRodney W. Grimes { 1623ea022d16SRodney W. Grimes FILE *fin; 1624ea022d16SRodney W. Grimes int c; 1625ea022d16SRodney W. Grimes char line[LINE_MAX]; 1626ea022d16SRodney W. Grimes 1627af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 1628ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 1629ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 1630ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 1631ea022d16SRodney W. Grimes if (c == '\n') { 1632ea022d16SRodney W. Grimes if (ferror(stdout)){ 1633ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 1634ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1635ea022d16SRodney W. Grimes dologout(1); 1636ea022d16SRodney W. Grimes /* NOTREACHED */ 1637ea022d16SRodney W. Grimes } 1638ea022d16SRodney W. Grimes if (ferror(fin)) { 1639ea022d16SRodney W. Grimes perror_reply(551, filename); 1640ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1641ea022d16SRodney W. Grimes return; 1642ea022d16SRodney W. Grimes } 1643ea022d16SRodney W. Grimes (void) putc('\r', stdout); 1644ea022d16SRodney W. Grimes } 1645ea022d16SRodney W. Grimes (void) putc(c, stdout); 1646ea022d16SRodney W. Grimes } 1647ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1648ea022d16SRodney W. Grimes reply(211, "End of Status"); 1649ea022d16SRodney W. Grimes } 1650ea022d16SRodney W. Grimes 1651ea022d16SRodney W. Grimes void 1652ea022d16SRodney W. Grimes statcmd() 1653ea022d16SRodney W. Grimes { 1654ea022d16SRodney W. Grimes struct sockaddr_in *sin; 1655ea022d16SRodney W. Grimes u_char *a, *p; 1656ea022d16SRodney W. Grimes 1657ea022d16SRodney W. Grimes lreply(211, "%s FTP server status:", hostname, version); 1658ea022d16SRodney W. Grimes printf(" %s\r\n", version); 1659ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 1660ea022d16SRodney W. Grimes if (!isdigit(remotehost[0])) 1661ea022d16SRodney W. Grimes printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 1662ea022d16SRodney W. Grimes printf("\r\n"); 1663ea022d16SRodney W. Grimes if (logged_in) { 1664ea022d16SRodney W. Grimes if (guest) 1665ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 1666ea022d16SRodney W. Grimes else 1667ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 1668ea022d16SRodney W. Grimes } else if (askpasswd) 1669ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 1670ea022d16SRodney W. Grimes else 1671ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 1672ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 1673ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 1674ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 1675ea022d16SRodney W. Grimes if (type == TYPE_L) 1676ea022d16SRodney W. Grimes #if NBBY == 8 1677ea022d16SRodney W. Grimes printf(" %d", NBBY); 1678ea022d16SRodney W. Grimes #else 1679ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 1680ea022d16SRodney W. Grimes #endif 1681ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 1682ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 1683ea022d16SRodney W. Grimes if (data != -1) 1684ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 1685ea022d16SRodney W. Grimes else if (pdata != -1) { 1686ea022d16SRodney W. Grimes printf(" in Passive mode"); 1687ea022d16SRodney W. Grimes sin = &pasv_addr; 1688ea022d16SRodney W. Grimes goto printaddr; 1689ea022d16SRodney W. Grimes } else if (usedefault == 0) { 1690ea022d16SRodney W. Grimes printf(" PORT"); 1691ea022d16SRodney W. Grimes sin = &data_dest; 1692ea022d16SRodney W. Grimes printaddr: 1693ea022d16SRodney W. Grimes a = (u_char *) &sin->sin_addr; 1694ea022d16SRodney W. Grimes p = (u_char *) &sin->sin_port; 1695ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 1696ea022d16SRodney W. Grimes printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 1697ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1698ea022d16SRodney W. Grimes #undef UC 1699ea022d16SRodney W. Grimes } else 1700ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 1701ea022d16SRodney W. Grimes reply(211, "End of status"); 1702ea022d16SRodney W. Grimes } 1703ea022d16SRodney W. Grimes 1704ea022d16SRodney W. Grimes void 1705ea022d16SRodney W. Grimes fatal(s) 1706ea022d16SRodney W. Grimes char *s; 1707ea022d16SRodney W. Grimes { 1708ea022d16SRodney W. Grimes 1709ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 1710ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 1711ea022d16SRodney W. Grimes dologout(0); 1712ea022d16SRodney W. Grimes /* NOTREACHED */ 1713ea022d16SRodney W. Grimes } 1714ea022d16SRodney W. Grimes 1715ea022d16SRodney W. Grimes void 1716ea022d16SRodney W. Grimes #if __STDC__ 1717ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 1718ea022d16SRodney W. Grimes #else 1719ea022d16SRodney W. Grimes reply(n, fmt, va_alist) 1720ea022d16SRodney W. Grimes int n; 1721ea022d16SRodney W. Grimes char *fmt; 1722ea022d16SRodney W. Grimes va_dcl 1723ea022d16SRodney W. Grimes #endif 1724ea022d16SRodney W. Grimes { 1725ea022d16SRodney W. Grimes va_list ap; 1726ea022d16SRodney W. Grimes #if __STDC__ 1727ea022d16SRodney W. Grimes va_start(ap, fmt); 1728ea022d16SRodney W. Grimes #else 1729ea022d16SRodney W. Grimes va_start(ap); 1730ea022d16SRodney W. Grimes #endif 1731ea022d16SRodney W. Grimes (void)printf("%d ", n); 1732ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 1733ea022d16SRodney W. Grimes (void)printf("\r\n"); 1734ea022d16SRodney W. Grimes (void)fflush(stdout); 1735ea022d16SRodney W. Grimes if (debug) { 1736ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 1737ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 1738ea022d16SRodney W. Grimes } 1739ea022d16SRodney W. Grimes } 1740ea022d16SRodney W. Grimes 1741ea022d16SRodney W. Grimes void 1742ea022d16SRodney W. Grimes #if __STDC__ 1743ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 1744ea022d16SRodney W. Grimes #else 1745ea022d16SRodney W. Grimes lreply(n, fmt, va_alist) 1746ea022d16SRodney W. Grimes int n; 1747ea022d16SRodney W. Grimes char *fmt; 1748ea022d16SRodney W. Grimes va_dcl 1749ea022d16SRodney W. Grimes #endif 1750ea022d16SRodney W. Grimes { 1751ea022d16SRodney W. Grimes va_list ap; 1752ea022d16SRodney W. Grimes #if __STDC__ 1753ea022d16SRodney W. Grimes va_start(ap, fmt); 1754ea022d16SRodney W. Grimes #else 1755ea022d16SRodney W. Grimes va_start(ap); 1756ea022d16SRodney W. Grimes #endif 1757ea022d16SRodney W. Grimes (void)printf("%d- ", n); 1758ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 1759ea022d16SRodney W. Grimes (void)printf("\r\n"); 1760ea022d16SRodney W. Grimes (void)fflush(stdout); 1761ea022d16SRodney W. Grimes if (debug) { 1762ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 1763ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 1764ea022d16SRodney W. Grimes } 1765ea022d16SRodney W. Grimes } 1766ea022d16SRodney W. Grimes 1767ea022d16SRodney W. Grimes static void 1768ea022d16SRodney W. Grimes ack(s) 1769ea022d16SRodney W. Grimes char *s; 1770ea022d16SRodney W. Grimes { 1771ea022d16SRodney W. Grimes 1772ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 1773ea022d16SRodney W. Grimes } 1774ea022d16SRodney W. Grimes 1775ea022d16SRodney W. Grimes void 1776ea022d16SRodney W. Grimes nack(s) 1777ea022d16SRodney W. Grimes char *s; 1778ea022d16SRodney W. Grimes { 1779ea022d16SRodney W. Grimes 1780ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 1781ea022d16SRodney W. Grimes } 1782ea022d16SRodney W. Grimes 1783ea022d16SRodney W. Grimes /* ARGSUSED */ 1784ea022d16SRodney W. Grimes void 1785ea022d16SRodney W. Grimes yyerror(s) 1786ea022d16SRodney W. Grimes char *s; 1787ea022d16SRodney W. Grimes { 1788ea022d16SRodney W. Grimes char *cp; 1789ea022d16SRodney W. Grimes 17909aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 1791ea022d16SRodney W. Grimes *cp = '\0'; 1792ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 1793ea022d16SRodney W. Grimes } 1794ea022d16SRodney W. Grimes 1795ea022d16SRodney W. Grimes void 1796ea022d16SRodney W. Grimes delete(name) 1797ea022d16SRodney W. Grimes char *name; 1798ea022d16SRodney W. Grimes { 1799ea022d16SRodney W. Grimes struct stat st; 1800ea022d16SRodney W. Grimes 1801ea022d16SRodney W. Grimes LOGCMD("delete", name); 1802ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 1803ea022d16SRodney W. Grimes perror_reply(550, name); 1804ea022d16SRodney W. Grimes return; 1805ea022d16SRodney W. Grimes } 1806ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 1807ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 1808ea022d16SRodney W. Grimes perror_reply(550, name); 1809ea022d16SRodney W. Grimes return; 1810ea022d16SRodney W. Grimes } 1811ea022d16SRodney W. Grimes goto done; 1812ea022d16SRodney W. Grimes } 1813ea022d16SRodney W. Grimes if (unlink(name) < 0) { 1814ea022d16SRodney W. Grimes perror_reply(550, name); 1815ea022d16SRodney W. Grimes return; 1816ea022d16SRodney W. Grimes } 1817ea022d16SRodney W. Grimes done: 1818ea022d16SRodney W. Grimes ack("DELE"); 1819ea022d16SRodney W. Grimes } 1820ea022d16SRodney W. Grimes 1821ea022d16SRodney W. Grimes void 1822ea022d16SRodney W. Grimes cwd(path) 1823ea022d16SRodney W. Grimes char *path; 1824ea022d16SRodney W. Grimes { 1825ea022d16SRodney W. Grimes 1826ea022d16SRodney W. Grimes if (chdir(path) < 0) 1827ea022d16SRodney W. Grimes perror_reply(550, path); 1828ea022d16SRodney W. Grimes else 1829ea022d16SRodney W. Grimes ack("CWD"); 1830ea022d16SRodney W. Grimes } 1831ea022d16SRodney W. Grimes 1832ea022d16SRodney W. Grimes void 1833ea022d16SRodney W. Grimes makedir(name) 1834ea022d16SRodney W. Grimes char *name; 1835ea022d16SRodney W. Grimes { 1836ea022d16SRodney W. Grimes 1837ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 1838ea022d16SRodney W. Grimes if (mkdir(name, 0777) < 0) 1839ea022d16SRodney W. Grimes perror_reply(550, name); 1840ea022d16SRodney W. Grimes else 1841ea022d16SRodney W. Grimes reply(257, "MKD command successful."); 1842ea022d16SRodney W. Grimes } 1843ea022d16SRodney W. Grimes 1844ea022d16SRodney W. Grimes void 1845ea022d16SRodney W. Grimes removedir(name) 1846ea022d16SRodney W. Grimes char *name; 1847ea022d16SRodney W. Grimes { 1848ea022d16SRodney W. Grimes 1849ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 1850ea022d16SRodney W. Grimes if (rmdir(name) < 0) 1851ea022d16SRodney W. Grimes perror_reply(550, name); 1852ea022d16SRodney W. Grimes else 1853ea022d16SRodney W. Grimes ack("RMD"); 1854ea022d16SRodney W. Grimes } 1855ea022d16SRodney W. Grimes 1856ea022d16SRodney W. Grimes void 1857ea022d16SRodney W. Grimes pwd() 1858ea022d16SRodney W. Grimes { 1859ea022d16SRodney W. Grimes char path[MAXPATHLEN + 1]; 1860ea022d16SRodney W. Grimes 1861ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 1862ea022d16SRodney W. Grimes reply(550, "%s.", path); 1863ea022d16SRodney W. Grimes else 1864ea022d16SRodney W. Grimes reply(257, "\"%s\" is current directory.", path); 1865ea022d16SRodney W. Grimes } 1866ea022d16SRodney W. Grimes 1867ea022d16SRodney W. Grimes char * 1868ea022d16SRodney W. Grimes renamefrom(name) 1869ea022d16SRodney W. Grimes char *name; 1870ea022d16SRodney W. Grimes { 1871ea022d16SRodney W. Grimes struct stat st; 1872ea022d16SRodney W. Grimes 1873ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 1874ea022d16SRodney W. Grimes perror_reply(550, name); 1875ea022d16SRodney W. Grimes return ((char *)0); 1876ea022d16SRodney W. Grimes } 1877ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 1878ea022d16SRodney W. Grimes return (name); 1879ea022d16SRodney W. Grimes } 1880ea022d16SRodney W. Grimes 1881ea022d16SRodney W. Grimes void 1882ea022d16SRodney W. Grimes renamecmd(from, to) 1883ea022d16SRodney W. Grimes char *from, *to; 1884ea022d16SRodney W. Grimes { 188561f891a6SPaul Traina struct stat st; 1886ea022d16SRodney W. Grimes 1887ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 188861f891a6SPaul Traina 188961f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 189061f891a6SPaul Traina reply(550, "%s: permission denied", to); 189161f891a6SPaul Traina return; 189261f891a6SPaul Traina } 189361f891a6SPaul Traina 1894ea022d16SRodney W. Grimes if (rename(from, to) < 0) 1895ea022d16SRodney W. Grimes perror_reply(550, "rename"); 1896ea022d16SRodney W. Grimes else 1897ea022d16SRodney W. Grimes ack("RNTO"); 1898ea022d16SRodney W. Grimes } 1899ea022d16SRodney W. Grimes 1900ea022d16SRodney W. Grimes static void 1901ea022d16SRodney W. Grimes dolog(sin) 1902ea022d16SRodney W. Grimes struct sockaddr_in *sin; 1903ea022d16SRodney W. Grimes { 1904ea022d16SRodney W. Grimes struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 1905ea022d16SRodney W. Grimes sizeof(struct in_addr), AF_INET); 1906ea022d16SRodney W. Grimes 1907ea022d16SRodney W. Grimes if (hp) 1908ea022d16SRodney W. Grimes (void) strncpy(remotehost, hp->h_name, sizeof(remotehost)); 1909ea022d16SRodney W. Grimes else 1910ea022d16SRodney W. Grimes (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 1911ea022d16SRodney W. Grimes sizeof(remotehost)); 1912ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1913ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1914ea4e54b9SDavid Nugent if (thishost != firsthost) 1915ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 1916ea4e54b9SDavid Nugent remotehost, hostname); 1917ea4e54b9SDavid Nugent else 1918ea4e54b9SDavid Nugent #endif 1919ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 1920ea4e54b9SDavid Nugent remotehost); 1921b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1922ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1923ea022d16SRodney W. Grimes 1924ea4e54b9SDavid Nugent if (logging) { 1925ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1926ea4e54b9SDavid Nugent if (thishost != firsthost) 1927ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 1928ea4e54b9SDavid Nugent remotehost, hostname); 1929ea4e54b9SDavid Nugent else 1930ea4e54b9SDavid Nugent #endif 1931f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 1932f5c57d05SEivind Eklund inet_ntoa(sin->sin_addr)); 1933ea022d16SRodney W. Grimes } 1934ea4e54b9SDavid Nugent } 1935ea022d16SRodney W. Grimes 1936ea022d16SRodney W. Grimes /* 1937ea022d16SRodney W. Grimes * Record logout in wtmp file 1938ea022d16SRodney W. Grimes * and exit with supplied status. 1939ea022d16SRodney W. Grimes */ 1940ea022d16SRodney W. Grimes void 1941ea022d16SRodney W. Grimes dologout(status) 1942ea022d16SRodney W. Grimes int status; 1943ea022d16SRodney W. Grimes { 19440b4df2eeSDavid Greenman /* 19450b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 19460b4df2eeSDavid Greenman * back to the main program loop. 19470b4df2eeSDavid Greenman */ 19480b4df2eeSDavid Greenman transflag = 0; 1949ea022d16SRodney W. Grimes 1950ea022d16SRodney W. Grimes if (logged_in) { 1951ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1952986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 1953a5a4544eSPaul Traina #if defined(KERBEROS) 1954a5a4544eSPaul Traina if (!notickets && krbtkfile_env) 1955a5a4544eSPaul Traina unlink(krbtkfile_env); 1956a5a4544eSPaul Traina #endif 1957ea022d16SRodney W. Grimes } 1958ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 1959ea022d16SRodney W. Grimes _exit(status); 1960ea022d16SRodney W. Grimes } 1961ea022d16SRodney W. Grimes 1962ea022d16SRodney W. Grimes static void 1963ea022d16SRodney W. Grimes myoob(signo) 1964ea022d16SRodney W. Grimes int signo; 1965ea022d16SRodney W. Grimes { 1966ea022d16SRodney W. Grimes char *cp; 1967ea022d16SRodney W. Grimes 1968ea022d16SRodney W. Grimes /* only process if transfer occurring */ 1969ea022d16SRodney W. Grimes if (!transflag) 1970ea022d16SRodney W. Grimes return; 1971ea022d16SRodney W. Grimes cp = tmpline; 1972ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 1973ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 1974ea022d16SRodney W. Grimes dologout(0); 1975ea022d16SRodney W. Grimes } 1976ea022d16SRodney W. Grimes upper(cp); 1977ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 1978ea022d16SRodney W. Grimes tmpline[0] = '\0'; 1979ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 1980ea022d16SRodney W. Grimes reply(226, "Abort successful"); 1981ea022d16SRodney W. Grimes longjmp(urgcatch, 1); 1982ea022d16SRodney W. Grimes } 1983ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 1984ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 1985ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 1986ea022d16SRodney W. Grimes byte_count, file_size); 1987ea022d16SRodney W. Grimes else 1988ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 1989ea022d16SRodney W. Grimes } 1990ea022d16SRodney W. Grimes } 1991ea022d16SRodney W. Grimes 1992ea022d16SRodney W. Grimes /* 1993ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 1994ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 1995ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 1996ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 1997ea022d16SRodney W. Grimes */ 1998ea022d16SRodney W. Grimes void 1999ea022d16SRodney W. Grimes passive() 2000ea022d16SRodney W. Grimes { 2001dacc9752SPaul Traina int len; 2002ea022d16SRodney W. Grimes char *p, *a; 2003ea022d16SRodney W. Grimes 200461f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 200561f891a6SPaul Traina close(pdata); 200661f891a6SPaul Traina 2007ea022d16SRodney W. Grimes pdata = socket(AF_INET, SOCK_STREAM, 0); 2008ea022d16SRodney W. Grimes if (pdata < 0) { 2009ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2010ea022d16SRodney W. Grimes return; 2011ea022d16SRodney W. Grimes } 20124c450ad7SPaul Traina 20134c450ad7SPaul Traina (void) seteuid((uid_t)0); 201461f891a6SPaul Traina 2015dacc9752SPaul Traina #ifdef IP_PORTRANGE 2016dacc9752SPaul Traina { 2017dacc9752SPaul Traina int on = restricted_data_ports ? IP_PORTRANGE_HIGH 2018dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2019dacc9752SPaul Traina 202040e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2021dacc9752SPaul Traina (char *)&on, sizeof(on)) < 0) 20224c450ad7SPaul Traina goto pasv_error; 20234c450ad7SPaul Traina } 2024dacc9752SPaul Traina #endif 202540e9d39eSPeter Wemm 2026ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 2027ea022d16SRodney W. Grimes pasv_addr.sin_port = 0; 20284c450ad7SPaul Traina if (bind(pdata, (struct sockaddr *)&pasv_addr, 202961f891a6SPaul Traina sizeof(pasv_addr)) < 0) 2030ea022d16SRodney W. Grimes goto pasv_error; 203161f891a6SPaul Traina 2032ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 20334c450ad7SPaul Traina 2034ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2035ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2036ea022d16SRodney W. Grimes goto pasv_error; 2037ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2038ea022d16SRodney W. Grimes goto pasv_error; 2039ea022d16SRodney W. Grimes a = (char *) &pasv_addr.sin_addr; 2040ea022d16SRodney W. Grimes p = (char *) &pasv_addr.sin_port; 2041ea022d16SRodney W. Grimes 2042ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2043ea022d16SRodney W. Grimes 2044ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2045ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2046ea022d16SRodney W. Grimes return; 2047ea022d16SRodney W. Grimes 2048ea022d16SRodney W. Grimes pasv_error: 204961f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2050ea022d16SRodney W. Grimes (void) close(pdata); 2051ea022d16SRodney W. Grimes pdata = -1; 2052ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2053ea022d16SRodney W. Grimes return; 2054ea022d16SRodney W. Grimes } 2055ea022d16SRodney W. Grimes 2056ea022d16SRodney W. Grimes /* 2057ea022d16SRodney W. Grimes * Generate unique name for file with basename "local". 2058ea022d16SRodney W. Grimes * The file named "local" is already known to exist. 2059ea022d16SRodney W. Grimes * Generates failure reply on error. 2060ea022d16SRodney W. Grimes */ 2061ea022d16SRodney W. Grimes static char * 2062ea022d16SRodney W. Grimes gunique(local) 2063ea022d16SRodney W. Grimes char *local; 2064ea022d16SRodney W. Grimes { 2065ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2066ea022d16SRodney W. Grimes struct stat st; 2067ea022d16SRodney W. Grimes int count; 2068ea022d16SRodney W. Grimes char *cp; 2069ea022d16SRodney W. Grimes 2070ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2071ea022d16SRodney W. Grimes if (cp) 2072ea022d16SRodney W. Grimes *cp = '\0'; 2073ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2074ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2075ea022d16SRodney W. Grimes return ((char *) 0); 2076ea022d16SRodney W. Grimes } 2077ea022d16SRodney W. Grimes if (cp) 2078ea022d16SRodney W. Grimes *cp = '/'; 2079e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2080e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2081ea022d16SRodney W. Grimes cp = new + strlen(new); 2082ea022d16SRodney W. Grimes *cp++ = '.'; 2083ea022d16SRodney W. Grimes for (count = 1; count < 100; count++) { 2084ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2085ea022d16SRodney W. Grimes if (stat(new, &st) < 0) 2086ea022d16SRodney W. Grimes return (new); 2087ea022d16SRodney W. Grimes } 2088ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2089ea022d16SRodney W. Grimes return (NULL); 2090ea022d16SRodney W. Grimes } 2091ea022d16SRodney W. Grimes 2092ea022d16SRodney W. Grimes /* 2093ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2094ea022d16SRodney W. Grimes */ 2095ea022d16SRodney W. Grimes void 2096ea022d16SRodney W. Grimes perror_reply(code, string) 2097ea022d16SRodney W. Grimes int code; 2098ea022d16SRodney W. Grimes char *string; 2099ea022d16SRodney W. Grimes { 2100ea022d16SRodney W. Grimes 2101ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2102ea022d16SRodney W. Grimes } 2103ea022d16SRodney W. Grimes 2104ea022d16SRodney W. Grimes static char *onefile[] = { 2105ea022d16SRodney W. Grimes "", 2106ea022d16SRodney W. Grimes 0 2107ea022d16SRodney W. Grimes }; 2108ea022d16SRodney W. Grimes 2109ea022d16SRodney W. Grimes void 2110ea022d16SRodney W. Grimes send_file_list(whichf) 2111ea022d16SRodney W. Grimes char *whichf; 2112ea022d16SRodney W. Grimes { 2113ea022d16SRodney W. Grimes struct stat st; 2114ea022d16SRodney W. Grimes DIR *dirp = NULL; 2115ea022d16SRodney W. Grimes struct dirent *dir; 2116ea022d16SRodney W. Grimes FILE *dout = NULL; 2117ea022d16SRodney W. Grimes char **dirlist, *dirname; 2118ea022d16SRodney W. Grimes int simple = 0; 2119ea022d16SRodney W. Grimes int freeglob = 0; 2120ea022d16SRodney W. Grimes glob_t gl; 2121ea022d16SRodney W. Grimes 2122ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 2123ea022d16SRodney W. Grimes int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 2124ea022d16SRodney W. Grimes 2125ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 2126ea022d16SRodney W. Grimes freeglob = 1; 2127ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2128ea022d16SRodney W. Grimes reply(550, "not found"); 2129ea022d16SRodney W. Grimes goto out; 2130ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2131ea022d16SRodney W. Grimes errno = ENOENT; 2132ea022d16SRodney W. Grimes perror_reply(550, whichf); 2133ea022d16SRodney W. Grimes goto out; 2134ea022d16SRodney W. Grimes } 2135ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2136ea022d16SRodney W. Grimes } else { 2137ea022d16SRodney W. Grimes onefile[0] = whichf; 2138ea022d16SRodney W. Grimes dirlist = onefile; 2139ea022d16SRodney W. Grimes simple = 1; 2140ea022d16SRodney W. Grimes } 2141ea022d16SRodney W. Grimes 2142ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 2143ea022d16SRodney W. Grimes transflag = 0; 2144ea022d16SRodney W. Grimes goto out; 2145ea022d16SRodney W. Grimes } 21469aca17cbSMark Murray while ((dirname = *dirlist++)) { 2147ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2148ea022d16SRodney W. Grimes /* 2149ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2150ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2151ea022d16SRodney W. Grimes */ 2152ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2153ea022d16SRodney W. Grimes transflag == 0) { 2154af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2155ea022d16SRodney W. Grimes goto out; 2156ea022d16SRodney W. Grimes } 2157ea022d16SRodney W. Grimes perror_reply(550, whichf); 2158ea022d16SRodney W. Grimes if (dout != NULL) { 2159ea022d16SRodney W. Grimes (void) fclose(dout); 2160ea022d16SRodney W. Grimes transflag = 0; 2161ea022d16SRodney W. Grimes data = -1; 2162ea022d16SRodney W. Grimes pdata = -1; 2163ea022d16SRodney W. Grimes } 2164ea022d16SRodney W. Grimes goto out; 2165ea022d16SRodney W. Grimes } 2166ea022d16SRodney W. Grimes 2167ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2168ea022d16SRodney W. Grimes if (dout == NULL) { 2169ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2170ea022d16SRodney W. Grimes if (dout == NULL) 2171ea022d16SRodney W. Grimes goto out; 2172ea022d16SRodney W. Grimes transflag++; 2173ea022d16SRodney W. Grimes } 2174ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2175ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2176ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2177ea022d16SRodney W. Grimes continue; 2178ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2179ea022d16SRodney W. Grimes continue; 2180ea022d16SRodney W. Grimes 2181ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 2182ea022d16SRodney W. Grimes continue; 2183ea022d16SRodney W. Grimes 2184ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 2185ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 2186ea022d16SRodney W. Grimes 2187ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2188ea022d16SRodney W. Grimes continue; 2189ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2190ea022d16SRodney W. Grimes dir->d_namlen == 2) 2191ea022d16SRodney W. Grimes continue; 2192ea022d16SRodney W. Grimes 2193e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 2194e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 2195ea022d16SRodney W. Grimes 2196ea022d16SRodney W. Grimes /* 2197ea022d16SRodney W. Grimes * We have to do a stat to insure it's 2198ea022d16SRodney W. Grimes * not a directory or special file. 2199ea022d16SRodney W. Grimes */ 2200ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 2201ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 2202ea022d16SRodney W. Grimes if (dout == NULL) { 2203ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 2204ea022d16SRodney W. Grimes "w"); 2205ea022d16SRodney W. Grimes if (dout == NULL) 2206ea022d16SRodney W. Grimes goto out; 2207ea022d16SRodney W. Grimes transflag++; 2208ea022d16SRodney W. Grimes } 2209ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 2210ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 2211ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2212ea022d16SRodney W. Grimes else 2213ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 2214ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2215ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 2216ea022d16SRodney W. Grimes } 2217ea022d16SRodney W. Grimes } 2218ea022d16SRodney W. Grimes (void) closedir(dirp); 2219ea022d16SRodney W. Grimes } 2220ea022d16SRodney W. Grimes 2221ea022d16SRodney W. Grimes if (dout == NULL) 2222ea022d16SRodney W. Grimes reply(550, "No files found."); 2223ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 2224ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 2225ea022d16SRodney W. Grimes else 2226ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 2227ea022d16SRodney W. Grimes 2228ea022d16SRodney W. Grimes transflag = 0; 2229ea022d16SRodney W. Grimes if (dout != NULL) 2230ea022d16SRodney W. Grimes (void) fclose(dout); 2231ea022d16SRodney W. Grimes data = -1; 2232ea022d16SRodney W. Grimes pdata = -1; 2233ea022d16SRodney W. Grimes out: 2234ea022d16SRodney W. Grimes if (freeglob) { 2235ea022d16SRodney W. Grimes freeglob = 0; 2236ea022d16SRodney W. Grimes globfree(&gl); 2237ea022d16SRodney W. Grimes } 2238ea022d16SRodney W. Grimes } 2239ea022d16SRodney W. Grimes 2240cf09a206SDavid Greenman void 2241cf09a206SDavid Greenman reapchild(signo) 2242cf09a206SDavid Greenman int signo; 2243cf09a206SDavid Greenman { 2244cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 2245cf09a206SDavid Greenman } 2246cf09a206SDavid Greenman 2247b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2248ea022d16SRodney W. Grimes /* 2249ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2250ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2251ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2252ea022d16SRodney W. Grimes */ 2253ea022d16SRodney W. Grimes void 2254ea022d16SRodney W. Grimes #if __STDC__ 2255ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2256ea022d16SRodney W. Grimes #else 2257ea022d16SRodney W. Grimes setproctitle(fmt, va_alist) 2258ea022d16SRodney W. Grimes char *fmt; 2259ea022d16SRodney W. Grimes va_dcl 2260ea022d16SRodney W. Grimes #endif 2261ea022d16SRodney W. Grimes { 2262ea022d16SRodney W. Grimes int i; 2263ea022d16SRodney W. Grimes va_list ap; 2264ea022d16SRodney W. Grimes char *p, *bp, ch; 2265ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2266ea022d16SRodney W. Grimes 2267ea022d16SRodney W. Grimes #if __STDC__ 2268ea022d16SRodney W. Grimes va_start(ap, fmt); 2269ea022d16SRodney W. Grimes #else 2270ea022d16SRodney W. Grimes va_start(ap); 2271ea022d16SRodney W. Grimes #endif 2272ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2273ea022d16SRodney W. Grimes 2274ea022d16SRodney W. Grimes /* make ps print our process name */ 2275ea022d16SRodney W. Grimes p = Argv[0]; 2276ea022d16SRodney W. Grimes *p++ = '-'; 2277ea022d16SRodney W. Grimes 2278ea022d16SRodney W. Grimes i = strlen(buf); 2279ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 2280ea022d16SRodney W. Grimes i = LastArgv - p - 2; 2281ea022d16SRodney W. Grimes buf[i] = '\0'; 2282ea022d16SRodney W. Grimes } 2283ea022d16SRodney W. Grimes bp = buf; 2284ea022d16SRodney W. Grimes while (ch = *bp++) 2285ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 2286ea022d16SRodney W. Grimes *p++ = ch; 2287ea022d16SRodney W. Grimes while (p < LastArgv) 2288ea022d16SRodney W. Grimes *p++ = ' '; 2289ea022d16SRodney W. Grimes } 2290b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 22913eb568f2SGuido van Rooij 229261f891a6SPaul Traina static void 22933eb568f2SGuido van Rooij logxfer(name, size, start) 22943eb568f2SGuido van Rooij char *name; 22953eb568f2SGuido van Rooij long size; 22963eb568f2SGuido van Rooij long start; 22973eb568f2SGuido van Rooij { 22983eb568f2SGuido van Rooij char buf[1024]; 22993eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 2300158a00b2SJohn Birrell time_t now; 23013eb568f2SGuido van Rooij 23023eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 23033eb568f2SGuido van Rooij time(&now); 230461f891a6SPaul Traina snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n", 23053eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 23063eb568f2SGuido van Rooij path, name, size, now - start + (now == start)); 23073eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 23083eb568f2SGuido van Rooij } 23093eb568f2SGuido van Rooij } 2310