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. 3295645563SDavid Greenman * 33af85d782SDavid Nugent * $Id: ftpd.c,v 1.35 1997/04/23 04:56:39 davidn Exp $ 34ea022d16SRodney W. Grimes */ 35ea022d16SRodney W. Grimes 369aca17cbSMark Murray #if 0 37ea022d16SRodney W. Grimes #ifndef lint 38ea022d16SRodney W. Grimes static char copyright[] = 39ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 40ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 41ea022d16SRodney W. Grimes #endif /* not lint */ 429aca17cbSMark Murray #endif 43ea022d16SRodney W. Grimes 449aca17cbSMark Murray #if 0 45ea022d16SRodney W. Grimes #ifndef lint 46ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 47ea022d16SRodney W. Grimes #endif /* not lint */ 489aca17cbSMark Murray #endif 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> 79ea022d16SRodney W. Grimes #include <setjmp.h> 80ea022d16SRodney W. Grimes #include <signal.h> 81ea022d16SRodney W. Grimes #include <stdio.h> 82ea022d16SRodney W. Grimes #include <stdlib.h> 83ea022d16SRodney W. Grimes #include <string.h> 84ea022d16SRodney W. Grimes #include <syslog.h> 85ea022d16SRodney W. Grimes #include <time.h> 86ea022d16SRodney W. Grimes #include <unistd.h> 87b63e1fe2SPeter Wemm #include <libutil.h> 88b071c689SDavid Nugent #ifdef LOGIN_CAP 89b071c689SDavid Nugent #include <login_cap.h> 90b071c689SDavid Nugent #endif 91ea022d16SRodney W. Grimes 922c60c54cSPaul Traina #ifdef SKEY 932c60c54cSPaul Traina #include <skey.h> 942c60c54cSPaul Traina #endif 952c60c54cSPaul Traina 96ea022d16SRodney W. Grimes #include "pathnames.h" 97ea022d16SRodney W. Grimes #include "extern.h" 98ea022d16SRodney W. Grimes 99ea022d16SRodney W. Grimes #if __STDC__ 100ea022d16SRodney W. Grimes #include <stdarg.h> 101ea022d16SRodney W. Grimes #else 102ea022d16SRodney W. Grimes #include <varargs.h> 103ea022d16SRodney W. Grimes #endif 104ea022d16SRodney W. Grimes 105af85d782SDavid Nugent #ifdef INTERNAL_LS 106af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 107af85d782SDavid Nugent #undef main 108af85d782SDavid Nugent #else 109ea022d16SRodney W. Grimes static char version[] = "Version 6.00"; 110af85d782SDavid Nugent #endif 111ea022d16SRodney W. Grimes 112ea022d16SRodney W. Grimes extern off_t restart_point; 113ea022d16SRodney W. Grimes extern char cbuf[]; 114ea022d16SRodney W. Grimes 115cf09a206SDavid Greenman struct sockaddr_in server_addr; 116ea022d16SRodney W. Grimes struct sockaddr_in ctrl_addr; 117ea022d16SRodney W. Grimes struct sockaddr_in data_source; 118ea022d16SRodney W. Grimes struct sockaddr_in data_dest; 119ea022d16SRodney W. Grimes struct sockaddr_in his_addr; 120ea022d16SRodney W. Grimes struct sockaddr_in pasv_addr; 121ea022d16SRodney W. Grimes 122cf09a206SDavid Greenman int daemon_mode; 123ea022d16SRodney W. Grimes int data; 124ea022d16SRodney W. Grimes jmp_buf errcatch, urgcatch; 125ea022d16SRodney W. Grimes int logged_in; 126ea022d16SRodney W. Grimes struct passwd *pw; 127ea022d16SRodney W. Grimes int debug; 128ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 129ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 130ea022d16SRodney W. Grimes int logging; 1314c450ad7SPaul Traina int restricted_data_ports = 1; 132a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1335a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 134ea022d16SRodney W. Grimes int guest; 135a5a4544eSPaul Traina int dochroot; 1363eb568f2SGuido van Rooij int stats; 1373eb568f2SGuido van Rooij int statfd = -1; 138ea022d16SRodney W. Grimes int type; 139ea022d16SRodney W. Grimes int form; 140ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 141ea022d16SRodney W. Grimes int mode; 142ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 143ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 144ea022d16SRodney W. Grimes sig_atomic_t transflag; 145ea022d16SRodney W. Grimes off_t file_size; 146ea022d16SRodney W. Grimes off_t byte_count; 147ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 148ea022d16SRodney W. Grimes #undef CMASK 149ea022d16SRodney W. Grimes #define CMASK 027 150ea022d16SRodney W. Grimes #endif 151ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 152ea022d16SRodney W. Grimes char tmpline[7]; 153ea022d16SRodney W. Grimes char hostname[MAXHOSTNAMELEN]; 154ea022d16SRodney W. Grimes char remotehost[MAXHOSTNAMELEN]; 1553eb568f2SGuido van Rooij char *ident = NULL; 156a5a4544eSPaul Traina 157a5a4544eSPaul Traina static char ttyline[20]; 158a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 159a5a4544eSPaul Traina 1609aca17cbSMark Murray #ifdef KERBEROS 1619aca17cbSMark Murray int klogin __P((struct passwd *, char *, char *, char *)); 1629aca17cbSMark Murray #endif 1639aca17cbSMark Murray 164105a3c98SJulian Elischer struct in_addr bind_address; 165105a3c98SJulian Elischer char *pid_file = NULL; 166105a3c98SJulian Elischer 167a5a4544eSPaul Traina #if defined(KERBEROS) 168a5a4544eSPaul Traina int notickets = 1; 1699aca17cbSMark Murray int noticketsdontcomplain = 1; 170a5a4544eSPaul Traina char *krbtkfile_env = NULL; 1713eb568f2SGuido van Rooij #endif 172ea022d16SRodney W. Grimes 173ea022d16SRodney W. Grimes /* 174ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 175ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 176ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 177ea022d16SRodney W. Grimes */ 178ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 179ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 180ea022d16SRodney W. Grimes 181ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 182ea022d16SRodney W. Grimes int swaitint = SWAITINT; 183ea022d16SRodney W. Grimes 184ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 185b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 186ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 187ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 188b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 189ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 190ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 191ea022d16SRodney W. Grimes 192726040deSGuido van Rooij #ifdef SKEY 193726040deSGuido van Rooij int pwok = 0; 1942c60c54cSPaul Traina char addr_string[20]; /* XXX */ 195726040deSGuido van Rooij #endif 196726040deSGuido van Rooij 197ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 198ea022d16SRodney W. Grimes if (logging > 1) \ 199ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 200ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 201ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 202ea022d16SRodney W. Grimes if (logging > 1) \ 203ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 204ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 205ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 206ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 207ea022d16SRodney W. Grimes if (logging > 1) { \ 208ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 209ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 210ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 211ea022d16SRodney W. Grimes else \ 212ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 213ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 214ea022d16SRodney W. Grimes } 215ea022d16SRodney W. Grimes 216ea022d16SRodney W. Grimes static void ack __P((char *)); 217ea022d16SRodney W. Grimes static void myoob __P((int)); 218a5a4544eSPaul Traina static int checkuser __P((char *, char *)); 219ea022d16SRodney W. Grimes static FILE *dataconn __P((char *, off_t, char *)); 220ea022d16SRodney W. Grimes static void dolog __P((struct sockaddr_in *)); 221ea022d16SRodney W. Grimes static char *curdir __P((void)); 222ea022d16SRodney W. Grimes static void end_login __P((void)); 223ea022d16SRodney W. Grimes static FILE *getdatasock __P((char *)); 224ea022d16SRodney W. Grimes static char *gunique __P((char *)); 225ea022d16SRodney W. Grimes static void lostconn __P((int)); 226ea022d16SRodney W. Grimes static int receive_data __P((FILE *, FILE *)); 2279fc5823aSGarrett Wollman static void send_data __P((FILE *, FILE *, off_t, off_t, int)); 228ea022d16SRodney W. Grimes static struct passwd * 229ea022d16SRodney W. Grimes sgetpwnam __P((char *)); 230ea022d16SRodney W. Grimes static char *sgetsave __P((char *)); 231cf09a206SDavid Greenman static void reapchild __P((int)); 23261f891a6SPaul Traina static void logxfer __P((char *, long, long)); 233ea022d16SRodney W. Grimes 234ea022d16SRodney W. Grimes static char * 235ea022d16SRodney W. Grimes curdir() 236ea022d16SRodney W. Grimes { 237ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 238ea022d16SRodney W. Grimes 239ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 240ea022d16SRodney W. Grimes return (""); 241ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 242ea022d16SRodney W. Grimes strcat(path, "/"); 243ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 244ea022d16SRodney W. Grimes return (guest ? path+1 : path); 245ea022d16SRodney W. Grimes } 246ea022d16SRodney W. Grimes 247ea022d16SRodney W. Grimes int 248ea022d16SRodney W. Grimes main(argc, argv, envp) 249ea022d16SRodney W. Grimes int argc; 250ea022d16SRodney W. Grimes char *argv[]; 251ea022d16SRodney W. Grimes char **envp; 252ea022d16SRodney W. Grimes { 253ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 254ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 255ea022d16SRodney W. Grimes FILE *fd; 256ea022d16SRodney W. Grimes 2572c60c54cSPaul Traina tzset(); /* in case no timezone database in ~ftp */ 2582c60c54cSPaul Traina 259b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 260ea022d16SRodney W. Grimes /* 261ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 262ea022d16SRodney W. Grimes */ 263ea022d16SRodney W. Grimes Argv = argv; 264ea022d16SRodney W. Grimes while (*envp) 265ea022d16SRodney W. Grimes envp++; 266ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 267b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 268ea022d16SRodney W. Grimes 2693eb568f2SGuido van Rooij 270105a3c98SJulian Elischer bind_address.s_addr = htonl(INADDR_ANY); 27191477cc4SWarner Losh while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:")) != -1) { 272ea022d16SRodney W. Grimes switch (ch) { 273cf09a206SDavid Greenman case 'D': 274cf09a206SDavid Greenman daemon_mode++; 275cf09a206SDavid Greenman break; 276cf09a206SDavid Greenman 277ea022d16SRodney W. Grimes case 'd': 278a5a4544eSPaul Traina debug++; 279ea022d16SRodney W. Grimes break; 280ea022d16SRodney W. Grimes 281ea022d16SRodney W. Grimes case 'l': 282ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 283ea022d16SRodney W. Grimes break; 284ea022d16SRodney W. Grimes 285a5a4544eSPaul Traina case 'R': 286a5a4544eSPaul Traina paranoid = 0; 287a5a4544eSPaul Traina break; 288a5a4544eSPaul Traina 289a5a4544eSPaul Traina case 'S': 290a5a4544eSPaul Traina stats++; 291a5a4544eSPaul Traina break; 292a5a4544eSPaul Traina 293a5a4544eSPaul Traina case 'T': 294a5a4544eSPaul Traina maxtimeout = atoi(optarg); 295a5a4544eSPaul Traina if (timeout > maxtimeout) 296a5a4544eSPaul Traina timeout = maxtimeout; 2974c450ad7SPaul Traina break; 2984c450ad7SPaul Traina 299ea022d16SRodney W. Grimes case 't': 300ea022d16SRodney W. Grimes timeout = atoi(optarg); 301ea022d16SRodney W. Grimes if (maxtimeout < timeout) 302ea022d16SRodney W. Grimes maxtimeout = timeout; 303ea022d16SRodney W. Grimes break; 304a5a4544eSPaul Traina 305a5a4544eSPaul Traina case 'U': 306a5a4544eSPaul Traina restricted_data_ports = 0; 307ea022d16SRodney W. Grimes break; 308ea022d16SRodney W. Grimes 309105a3c98SJulian Elischer case 'a': 310105a3c98SJulian Elischer if (!inet_aton(optarg, &bind_address)) 311105a3c98SJulian Elischer errx(1, "invalid address for -a"); 312105a3c98SJulian Elischer break; 313105a3c98SJulian Elischer 314105a3c98SJulian Elischer case 'p': 315105a3c98SJulian Elischer pid_file = optarg; 316105a3c98SJulian Elischer break; 317105a3c98SJulian Elischer 318ea022d16SRodney W. Grimes case 'u': 319ea022d16SRodney W. Grimes { 320ea022d16SRodney W. Grimes long val = 0; 321ea022d16SRodney W. Grimes 322ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 323ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 324ea022d16SRodney W. Grimes warnx("bad value for -u"); 325ea022d16SRodney W. Grimes else 326ea022d16SRodney W. Grimes defumask = val; 327ea022d16SRodney W. Grimes break; 328ea022d16SRodney W. Grimes } 3295a392aecSTorsten Blum case 'A': 3305a392aecSTorsten Blum anon_only = 1; 3315a392aecSTorsten Blum break; 332ea022d16SRodney W. Grimes 333ea022d16SRodney W. Grimes case 'v': 334ea022d16SRodney W. Grimes debug = 1; 335ea022d16SRodney W. Grimes break; 336ea022d16SRodney W. Grimes 337ea022d16SRodney W. Grimes default: 338ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 339ea022d16SRodney W. Grimes break; 340ea022d16SRodney W. Grimes } 341ea022d16SRodney W. Grimes } 342cf09a206SDavid Greenman 343ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 344cf09a206SDavid Greenman 345cf09a206SDavid Greenman /* 346cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 347cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 348cf09a206SDavid Greenman */ 349cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 350cf09a206SDavid Greenman 351cf09a206SDavid Greenman if (daemon_mode) { 352cf09a206SDavid Greenman int ctl_sock, fd; 353cf09a206SDavid Greenman struct servent *sv; 354cf09a206SDavid Greenman 355cf09a206SDavid Greenman /* 356cf09a206SDavid Greenman * Detach from parent. 357cf09a206SDavid Greenman */ 358cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 359cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 360cf09a206SDavid Greenman exit(1); 361cf09a206SDavid Greenman } 362cf09a206SDavid Greenman (void) signal(SIGCHLD, reapchild); 363cf09a206SDavid Greenman /* 364cf09a206SDavid Greenman * Get port number for ftp/tcp. 365cf09a206SDavid Greenman */ 366cf09a206SDavid Greenman sv = getservbyname("ftp", "tcp"); 367cf09a206SDavid Greenman if (sv == NULL) { 368cf09a206SDavid Greenman syslog(LOG_ERR, "getservbyname for ftp failed"); 369cf09a206SDavid Greenman exit(1); 370cf09a206SDavid Greenman } 371cf09a206SDavid Greenman /* 372cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 373cf09a206SDavid Greenman * listening. 374cf09a206SDavid Greenman */ 375cf09a206SDavid Greenman ctl_sock = socket(AF_INET, SOCK_STREAM, 0); 376cf09a206SDavid Greenman if (ctl_sock < 0) { 377cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 378cf09a206SDavid Greenman exit(1); 379cf09a206SDavid Greenman } 380cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 381cf09a206SDavid Greenman (char *)&on, sizeof(on)) < 0) 382cf09a206SDavid Greenman syslog(LOG_ERR, "control setsockopt: %m");; 383cf09a206SDavid Greenman server_addr.sin_family = AF_INET; 384105a3c98SJulian Elischer server_addr.sin_addr = bind_address; 385cf09a206SDavid Greenman server_addr.sin_port = sv->s_port; 386cf09a206SDavid Greenman if (bind(ctl_sock, (struct sockaddr *)&server_addr, sizeof(server_addr))) { 387cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 388cf09a206SDavid Greenman exit(1); 389cf09a206SDavid Greenman } 390cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 391cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 392cf09a206SDavid Greenman exit(1); 393cf09a206SDavid Greenman } 394cf09a206SDavid Greenman /* 395105a3c98SJulian Elischer * Atomically write process ID 396105a3c98SJulian Elischer */ 397105a3c98SJulian Elischer if (pid_file) 398105a3c98SJulian Elischer { 399105a3c98SJulian Elischer int fd; 400105a3c98SJulian Elischer char buf[20]; 401105a3c98SJulian Elischer 402105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 403105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 404105a3c98SJulian Elischer if (fd < 0) 405105a3c98SJulian Elischer if (errno == EAGAIN) 406105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 407105a3c98SJulian Elischer else 408105a3c98SJulian Elischer err(1, "%s", pid_file); 409105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 410105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 411105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 412105a3c98SJulian Elischer err(1, "%s: write", pid_file); 413105a3c98SJulian Elischer /* Leave the pid file open and locked */ 414105a3c98SJulian Elischer } 415105a3c98SJulian Elischer /* 416cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 417cf09a206SDavid Greenman * children to handle them. 418cf09a206SDavid Greenman */ 419cf09a206SDavid Greenman while (1) { 420cf09a206SDavid Greenman addrlen = sizeof(his_addr); 421cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 422cf09a206SDavid Greenman if (fork() == 0) { 423cf09a206SDavid Greenman /* child */ 424cf09a206SDavid Greenman (void) dup2(fd, 0); 425cf09a206SDavid Greenman (void) dup2(fd, 1); 426cf09a206SDavid Greenman close(ctl_sock); 427cf09a206SDavid Greenman break; 428cf09a206SDavid Greenman } 429cf09a206SDavid Greenman close(fd); 430cf09a206SDavid Greenman } 431cf09a206SDavid Greenman } else { 432cf09a206SDavid Greenman addrlen = sizeof(his_addr); 433cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 434cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 435cf09a206SDavid Greenman exit(1); 436cf09a206SDavid Greenman } 437cf09a206SDavid Greenman } 438cf09a206SDavid Greenman 439ea022d16SRodney W. Grimes (void) signal(SIGCHLD, SIG_IGN); 440cf09a206SDavid Greenman (void) signal(SIGPIPE, lostconn); 441ea022d16SRodney W. Grimes if ((int)signal(SIGURG, myoob) < 0) 442ea022d16SRodney W. Grimes syslog(LOG_ERR, "signal: %m"); 443ea022d16SRodney W. Grimes 444cf09a206SDavid Greenman #ifdef SKEY 44561f891a6SPaul Traina strncpy(addr_string, inet_ntoa(his_addr.sin_addr), sizeof(addr_string)); 446cf09a206SDavid Greenman #endif 447cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 448cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 449cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 450cf09a206SDavid Greenman exit(1); 451cf09a206SDavid Greenman } 452cf09a206SDavid Greenman #ifdef IP_TOS 453cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 454cf09a206SDavid Greenman if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 455cf09a206SDavid Greenman syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 456cf09a206SDavid Greenman #endif 457cf09a206SDavid Greenman data_source.sin_port = htons(ntohs(ctrl_addr.sin_port) - 1); 458cf09a206SDavid Greenman 459a5a4544eSPaul Traina /* set this here so klogin can use it... */ 460a5a4544eSPaul Traina (void)sprintf(ttyline, "ftp%d", getpid()); 461a5a4544eSPaul Traina 462ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 463ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 464ea022d16SRodney W. Grimes if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 465ea022d16SRodney W. Grimes syslog(LOG_ERR, "setsockopt: %m"); 466ea022d16SRodney W. Grimes #endif 467ea022d16SRodney W. Grimes 468ea022d16SRodney W. Grimes #ifdef F_SETOWN 469ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 470ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 471ea022d16SRodney W. Grimes #endif 472ea022d16SRodney W. Grimes dolog(&his_addr); 473ea022d16SRodney W. Grimes /* 474ea022d16SRodney W. Grimes * Set up default state 475ea022d16SRodney W. Grimes */ 476ea022d16SRodney W. Grimes data = -1; 477ea022d16SRodney W. Grimes type = TYPE_A; 478ea022d16SRodney W. Grimes form = FORM_N; 479ea022d16SRodney W. Grimes stru = STRU_F; 480ea022d16SRodney W. Grimes mode = MODE_S; 481ea022d16SRodney W. Grimes tmpline[0] = '\0'; 482ea022d16SRodney W. Grimes 483ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 484ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 485ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 486ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 487ea022d16SRodney W. Grimes *cp = '\0'; 488ea022d16SRodney W. Grimes lreply(530, "%s", line); 489ea022d16SRodney W. Grimes } 490ea022d16SRodney W. Grimes (void) fflush(stdout); 491ea022d16SRodney W. Grimes (void) fclose(fd); 492ea022d16SRodney W. Grimes reply(530, "System not available."); 493ea022d16SRodney W. Grimes exit(0); 494ea022d16SRodney W. Grimes } 495ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 496ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 497ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 498ea022d16SRodney W. Grimes *cp = '\0'; 499ea022d16SRodney W. Grimes lreply(220, "%s", line); 500ea022d16SRodney W. Grimes } 501ea022d16SRodney W. Grimes (void) fflush(stdout); 502ea022d16SRodney W. Grimes (void) fclose(fd); 503ea022d16SRodney W. Grimes /* reply(220,) must follow */ 504ea022d16SRodney W. Grimes } 505ea022d16SRodney W. Grimes (void) gethostname(hostname, sizeof(hostname)); 506ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 507ea022d16SRodney W. Grimes (void) setjmp(errcatch); 508ea022d16SRodney W. Grimes for (;;) 509ea022d16SRodney W. Grimes (void) yyparse(); 510ea022d16SRodney W. Grimes /* NOTREACHED */ 511ea022d16SRodney W. Grimes } 512ea022d16SRodney W. Grimes 513ea022d16SRodney W. Grimes static void 514ea022d16SRodney W. Grimes lostconn(signo) 515ea022d16SRodney W. Grimes int signo; 516ea022d16SRodney W. Grimes { 517ea022d16SRodney W. Grimes 518ea022d16SRodney W. Grimes if (debug) 519ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 520ea022d16SRodney W. Grimes dologout(-1); 521ea022d16SRodney W. Grimes } 522ea022d16SRodney W. Grimes 523ea022d16SRodney W. Grimes /* 524ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 525ea022d16SRodney W. Grimes */ 526ea022d16SRodney W. Grimes static char * 527ea022d16SRodney W. Grimes sgetsave(s) 528ea022d16SRodney W. Grimes char *s; 529ea022d16SRodney W. Grimes { 530ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 531ea022d16SRodney W. Grimes 532ea022d16SRodney W. Grimes if (new == NULL) { 533ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 534ea022d16SRodney W. Grimes dologout(1); 535ea022d16SRodney W. Grimes /* NOTREACHED */ 536ea022d16SRodney W. Grimes } 537ea022d16SRodney W. Grimes (void) strcpy(new, s); 538ea022d16SRodney W. Grimes return (new); 539ea022d16SRodney W. Grimes } 540ea022d16SRodney W. Grimes 541ea022d16SRodney W. Grimes /* 542ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 543ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 544ea022d16SRodney W. Grimes * (e.g., globbing). 545ea022d16SRodney W. Grimes */ 546ea022d16SRodney W. Grimes static struct passwd * 547ea022d16SRodney W. Grimes sgetpwnam(name) 548ea022d16SRodney W. Grimes char *name; 549ea022d16SRodney W. Grimes { 550ea022d16SRodney W. Grimes static struct passwd save; 551ea022d16SRodney W. Grimes struct passwd *p; 552ea022d16SRodney W. Grimes 553ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 554ea022d16SRodney W. Grimes return (p); 555ea022d16SRodney W. Grimes if (save.pw_name) { 556ea022d16SRodney W. Grimes free(save.pw_name); 557ea022d16SRodney W. Grimes free(save.pw_passwd); 558ea022d16SRodney W. Grimes free(save.pw_gecos); 559ea022d16SRodney W. Grimes free(save.pw_dir); 560ea022d16SRodney W. Grimes free(save.pw_shell); 561ea022d16SRodney W. Grimes } 562ea022d16SRodney W. Grimes save = *p; 563ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 564ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 565ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 566ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 567ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 568ea022d16SRodney W. Grimes return (&save); 569ea022d16SRodney W. Grimes } 570ea022d16SRodney W. Grimes 571ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 572ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 573ea022d16SRodney W. Grimes static char curname[10]; /* current USER name */ 574ea022d16SRodney W. Grimes 575ea022d16SRodney W. Grimes /* 576ea022d16SRodney W. Grimes * USER command. 577ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 578ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 579ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 580ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 581ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 582ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 583ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 584ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 585ea022d16SRodney W. Grimes */ 586ea022d16SRodney W. Grimes void 587ea022d16SRodney W. Grimes user(name) 588ea022d16SRodney W. Grimes char *name; 589ea022d16SRodney W. Grimes { 590ea022d16SRodney W. Grimes char *cp, *shell; 591ea022d16SRodney W. Grimes 592ea022d16SRodney W. Grimes if (logged_in) { 593ea022d16SRodney W. Grimes if (guest) { 594ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 595ea022d16SRodney W. Grimes return; 596a5a4544eSPaul Traina } else if (dochroot) { 597a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 598a5a4544eSPaul Traina return; 599ea022d16SRodney W. Grimes } 600ea022d16SRodney W. Grimes end_login(); 601ea022d16SRodney W. Grimes } 602ea022d16SRodney W. Grimes 603ea022d16SRodney W. Grimes guest = 0; 604ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 605a5a4544eSPaul Traina if (checkuser(_PATH_FTPUSERS, "ftp") || 606a5a4544eSPaul Traina checkuser(_PATH_FTPUSERS, "anonymous")) 607ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 608ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 609ea022d16SRodney W. Grimes guest = 1; 610ea022d16SRodney W. Grimes askpasswd = 1; 611ea022d16SRodney W. Grimes reply(331, 6122c60c54cSPaul Traina "Guest login ok, send your email address as password."); 613ea022d16SRodney W. Grimes } else 614ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 615ea022d16SRodney W. Grimes if (!askpasswd && logging) 616ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 617ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 618ea022d16SRodney W. Grimes return; 619ea022d16SRodney W. Grimes } 6205a392aecSTorsten Blum if (anon_only != 0) { 6215a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 6225a392aecSTorsten Blum return; 6235a392aecSTorsten Blum } 6245a392aecSTorsten Blum 6259aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 626ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 627ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 628ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 629ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 630ea022d16SRodney W. Grimes break; 631ea022d16SRodney W. Grimes endusershell(); 632ea022d16SRodney W. Grimes 633a5a4544eSPaul Traina if (cp == NULL || checkuser(_PATH_FTPUSERS, name)) { 634ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 635ea022d16SRodney W. Grimes if (logging) 636ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 637ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 638ea022d16SRodney W. Grimes remotehost, name); 639ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 640ea022d16SRodney W. Grimes return; 641ea022d16SRodney W. Grimes } 642ea022d16SRodney W. Grimes } 643ea022d16SRodney W. Grimes if (logging) 644ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 645726040deSGuido van Rooij #ifdef SKEY 6462c60c54cSPaul Traina pwok = skeyaccess(name, NULL, remotehost, addr_string); 64743658eacSAndrey A. Chernov reply(331, "%s", skey_challenge(name, pw, pwok)); 648726040deSGuido van Rooij #else 649ea022d16SRodney W. Grimes reply(331, "Password required for %s.", name); 650726040deSGuido van Rooij #endif 651ea022d16SRodney W. Grimes askpasswd = 1; 652ea022d16SRodney W. Grimes /* 653ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 654ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 655ea022d16SRodney W. Grimes */ 656ea022d16SRodney W. Grimes if (login_attempts) 657ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 658ea022d16SRodney W. Grimes } 659ea022d16SRodney W. Grimes 660ea022d16SRodney W. Grimes /* 661a5a4544eSPaul Traina * Check if a user is in the file "fname" 662ea022d16SRodney W. Grimes */ 663ea022d16SRodney W. Grimes static int 664a5a4544eSPaul Traina checkuser(fname, name) 665a5a4544eSPaul Traina char *fname; 666ea022d16SRodney W. Grimes char *name; 667ea022d16SRodney W. Grimes { 668ea022d16SRodney W. Grimes FILE *fd; 669ea022d16SRodney W. Grimes int found = 0; 670ea022d16SRodney W. Grimes char *p, line[BUFSIZ]; 671ea022d16SRodney W. Grimes 672a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 673ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) 674ea022d16SRodney W. Grimes if ((p = strchr(line, '\n')) != NULL) { 675ea022d16SRodney W. Grimes *p = '\0'; 676ea022d16SRodney W. Grimes if (line[0] == '#') 677ea022d16SRodney W. Grimes continue; 678348c7a12SDavid Greenman if (strcmp(line, name) == 0) { 679ea022d16SRodney W. Grimes found = 1; 680ea022d16SRodney W. Grimes break; 681ea022d16SRodney W. Grimes } 682ea022d16SRodney W. Grimes } 683ea022d16SRodney W. Grimes (void) fclose(fd); 684ea022d16SRodney W. Grimes } 685ea022d16SRodney W. Grimes return (found); 686ea022d16SRodney W. Grimes } 687ea022d16SRodney W. Grimes 688ea022d16SRodney W. Grimes /* 689ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 690ea022d16SRodney W. Grimes * used when USER command is given or login fails. 691ea022d16SRodney W. Grimes */ 692ea022d16SRodney W. Grimes static void 693ea022d16SRodney W. Grimes end_login() 694ea022d16SRodney W. Grimes { 695ea022d16SRodney W. Grimes 696ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 697ea022d16SRodney W. Grimes if (logged_in) 698ea022d16SRodney W. Grimes logwtmp(ttyline, "", ""); 699ea022d16SRodney W. Grimes pw = NULL; 700b071c689SDavid Nugent #ifdef LOGIN_CAP 701b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 702b071c689SDavid Nugent LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 703b071c689SDavid Nugent #endif 704ea022d16SRodney W. Grimes logged_in = 0; 705ea022d16SRodney W. Grimes guest = 0; 706a5a4544eSPaul Traina dochroot = 0; 707ea022d16SRodney W. Grimes } 708ea022d16SRodney W. Grimes 709ea022d16SRodney W. Grimes void 710ea022d16SRodney W. Grimes pass(passwd) 711ea022d16SRodney W. Grimes char *passwd; 712ea022d16SRodney W. Grimes { 713a5a4544eSPaul Traina int rval; 714ea022d16SRodney W. Grimes FILE *fd; 715b071c689SDavid Nugent #ifdef LOGIN_CAP 716b071c689SDavid Nugent login_cap_t *lc = NULL; 717b071c689SDavid Nugent #endif 71882c76939SDavid Greenman static char homedir[MAXPATHLEN]; 719ea022d16SRodney W. Grimes 720ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 721ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 722ea022d16SRodney W. Grimes return; 723ea022d16SRodney W. Grimes } 724ea022d16SRodney W. Grimes askpasswd = 0; 725ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 726a5a4544eSPaul Traina if (pw == NULL) { 727a5a4544eSPaul Traina rval = 1; /* failure below */ 728a5a4544eSPaul Traina goto skip; 729a5a4544eSPaul Traina } 730a5a4544eSPaul Traina #if defined(KERBEROS) 731a5a4544eSPaul Traina rval = klogin(pw, "", hostname, passwd); 732a5a4544eSPaul Traina if (rval == 0) 733a5a4544eSPaul Traina goto skip; 734a5a4544eSPaul Traina #endif 735726040deSGuido van Rooij #ifdef SKEY 736a5a4544eSPaul Traina rval = strcmp(skey_crypt(passwd, pw->pw_passwd, pw, pwok), 7370bb6e9edSPoul-Henning Kamp pw->pw_passwd); 738bb56d435SPaul Traina pwok = 0; 739726040deSGuido van Rooij #else 7403cde2031SPoul-Henning Kamp rval = strcmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd); 741726040deSGuido van Rooij #endif 742ea022d16SRodney W. Grimes /* The strcmp does not catch null passwords! */ 743a5a4544eSPaul Traina if (*pw->pw_passwd == '\0' || 744a5a4544eSPaul Traina (pw->pw_expire && time(NULL) >= pw->pw_expire)) 745a5a4544eSPaul Traina rval = 1; /* failure */ 746a5a4544eSPaul Traina skip: 747a5a4544eSPaul Traina /* 748a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 749a5a4544eSPaul Traina * above. If rval == 0, either Kerberos or local authentication 750a5a4544eSPaul Traina * succeeded. 751a5a4544eSPaul Traina */ 752a5a4544eSPaul Traina if (rval) { 753ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 754ea022d16SRodney W. Grimes if (logging) 755ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 756ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 757ea022d16SRodney W. Grimes remotehost, curname); 758ea022d16SRodney W. Grimes pw = NULL; 759ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 760ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 761ea022d16SRodney W. Grimes "repeated login failures from %s", 762ea022d16SRodney W. Grimes remotehost); 763ea022d16SRodney W. Grimes exit(0); 764ea022d16SRodney W. Grimes } 765ea022d16SRodney W. Grimes return; 766ea022d16SRodney W. Grimes } 767ea022d16SRodney W. Grimes } 768ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 769ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 770ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 771ea022d16SRodney W. Grimes return; 772ea022d16SRodney W. Grimes } 773b071c689SDavid Nugent /* May be overridden by login.conf */ 774b071c689SDavid Nugent (void) umask(defumask); 775b071c689SDavid Nugent #ifdef LOGIN_CAP 776b071c689SDavid Nugent if ((lc = login_getclass(pw)) != NULL) { 777b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 778b071c689SDavid Nugent 779b071c689SDavid Nugent strncpy(remote_ip, inet_ntoa(his_addr.sin_addr), 780b071c689SDavid Nugent sizeof(remote_ip) - 1); 781b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 782b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 783b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 784b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 785b071c689SDavid Nugent pw->pw_name); 786b071c689SDavid Nugent reply(530, "Permission denied.\n"); 787b071c689SDavid Nugent pw = NULL; 788b071c689SDavid Nugent return; 789b071c689SDavid Nugent } 790b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 791b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 792b071c689SDavid Nugent pw = NULL; 793b071c689SDavid Nugent return; 794b071c689SDavid Nugent } 795b071c689SDavid Nugent } 796b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 797b071c689SDavid Nugent LOGIN_SETGROUP|LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 798b071c689SDavid Nugent #else 799ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 800b071c689SDavid Nugent #endif 801ea022d16SRodney W. Grimes 802ea022d16SRodney W. Grimes /* open wtmp before chroot */ 803ea022d16SRodney W. Grimes logwtmp(ttyline, pw->pw_name, remotehost); 804ea022d16SRodney W. Grimes logged_in = 1; 805ea022d16SRodney W. Grimes 806a5a4544eSPaul Traina if (guest && stats && statfd < 0) 8073eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 8083eb568f2SGuido van Rooij stats = 0; 8093eb568f2SGuido van Rooij 810b071c689SDavid Nugent dochroot = 811b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 812b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 813b071c689SDavid Nugent #endif 814b071c689SDavid Nugent checkuser(_PATH_FTPCHROOT, pw->pw_name); 815ea022d16SRodney W. Grimes if (guest) { 816ea022d16SRodney W. Grimes /* 817ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 818ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 819ea022d16SRodney W. Grimes * outside the new root! 820ea022d16SRodney W. Grimes */ 821ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 822ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 823ea022d16SRodney W. Grimes goto bad; 824ea022d16SRodney W. Grimes } 825a5a4544eSPaul Traina } else if (dochroot) { 826a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 827a5a4544eSPaul Traina reply(550, "Can't change root."); 828a5a4544eSPaul Traina goto bad; 829a5a4544eSPaul Traina } 830ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 831ea022d16SRodney W. Grimes if (chdir("/") < 0) { 832ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 833ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 834ea022d16SRodney W. Grimes goto bad; 835ea022d16SRodney W. Grimes } else 836ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 837ea022d16SRodney W. Grimes } 838ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 839ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 840ea022d16SRodney W. Grimes goto bad; 841ea022d16SRodney W. Grimes } 84282c76939SDavid Greenman 84382c76939SDavid Greenman /* 84482c76939SDavid Greenman * Set home directory so that use of ~ (tilde) works correctly. 84582c76939SDavid Greenman */ 84695645563SDavid Greenman if (getcwd(homedir, MAXPATHLEN) != NULL) 84795645563SDavid Greenman setenv("HOME", homedir, 1); 84882c76939SDavid Greenman 849ea022d16SRodney W. Grimes /* 850ea022d16SRodney W. Grimes * Display a login message, if it exists. 851ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 852ea022d16SRodney W. Grimes */ 853ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 854ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 855ea022d16SRodney W. Grimes 856ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 857ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 858ea022d16SRodney W. Grimes *cp = '\0'; 859ea022d16SRodney W. Grimes lreply(230, "%s", line); 860ea022d16SRodney W. Grimes } 861ea022d16SRodney W. Grimes (void) fflush(stdout); 862ea022d16SRodney W. Grimes (void) fclose(fd); 863ea022d16SRodney W. Grimes } 864ea022d16SRodney W. Grimes if (guest) { 8653eb568f2SGuido van Rooij if (ident != NULL) 8663eb568f2SGuido van Rooij free(ident); 86761f891a6SPaul Traina ident = strdup(passwd); 86861f891a6SPaul Traina if (ident == NULL) 86961f891a6SPaul Traina fatal("Ran out of memory."); 87061f891a6SPaul Traina 871ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 872ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 873ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 874ea022d16SRodney W. Grimes "%s: anonymous/%.*s", remotehost, 875ea022d16SRodney W. Grimes sizeof(proctitle) - sizeof(remotehost) - 876ea022d16SRodney W. Grimes sizeof(": anonymous/"), passwd); 877b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 878ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 879ea022d16SRodney W. Grimes if (logging) 880ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 881ea022d16SRodney W. Grimes remotehost, passwd); 882ea022d16SRodney W. Grimes } else { 883ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 884ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 885ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 886ea022d16SRodney W. Grimes "%s: %s", remotehost, pw->pw_name); 887b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 888ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 889ea022d16SRodney W. Grimes if (logging) 890ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 891ea022d16SRodney W. Grimes remotehost, pw->pw_name); 892ea022d16SRodney W. Grimes } 893b071c689SDavid Nugent #ifdef LOGIN_CAP 894b071c689SDavid Nugent login_close(lc); 895b071c689SDavid Nugent #endif 896ea022d16SRodney W. Grimes return; 897ea022d16SRodney W. Grimes bad: 898ea022d16SRodney W. Grimes /* Forget all about it... */ 899b071c689SDavid Nugent #ifdef LOGIN_CAP 900b071c689SDavid Nugent login_close(lc); 901b071c689SDavid Nugent #endif 902ea022d16SRodney W. Grimes end_login(); 903ea022d16SRodney W. Grimes } 904ea022d16SRodney W. Grimes 905ea022d16SRodney W. Grimes void 906ea022d16SRodney W. Grimes retrieve(cmd, name) 907ea022d16SRodney W. Grimes char *cmd, *name; 908ea022d16SRodney W. Grimes { 909ea022d16SRodney W. Grimes FILE *fin, *dout; 910ea022d16SRodney W. Grimes struct stat st; 911ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 9123eb568f2SGuido van Rooij long start; 913ea022d16SRodney W. Grimes 914ea022d16SRodney W. Grimes if (cmd == 0) { 915ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 916ea022d16SRodney W. Grimes st.st_size = 0; 917ea022d16SRodney W. Grimes } else { 918ea022d16SRodney W. Grimes char line[BUFSIZ]; 919ea022d16SRodney W. Grimes 920ea022d16SRodney W. Grimes (void) sprintf(line, cmd, name), name = line; 921ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 922ea022d16SRodney W. Grimes st.st_size = -1; 923ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 924ea022d16SRodney W. Grimes } 925ea022d16SRodney W. Grimes if (fin == NULL) { 926ea022d16SRodney W. Grimes if (errno != 0) { 927ea022d16SRodney W. Grimes perror_reply(550, name); 928ea022d16SRodney W. Grimes if (cmd == 0) { 929ea022d16SRodney W. Grimes LOGCMD("get", name); 930ea022d16SRodney W. Grimes } 931ea022d16SRodney W. Grimes } 932ea022d16SRodney W. Grimes return; 933ea022d16SRodney W. Grimes } 934ea022d16SRodney W. Grimes byte_count = -1; 935ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 936ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 937ea022d16SRodney W. Grimes goto done; 938ea022d16SRodney W. Grimes } 939ea022d16SRodney W. Grimes if (restart_point) { 940ea022d16SRodney W. Grimes if (type == TYPE_A) { 941ea022d16SRodney W. Grimes off_t i, n; 942ea022d16SRodney W. Grimes int c; 943ea022d16SRodney W. Grimes 944ea022d16SRodney W. Grimes n = restart_point; 945ea022d16SRodney W. Grimes i = 0; 946ea022d16SRodney W. Grimes while (i++ < n) { 947ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 948ea022d16SRodney W. Grimes perror_reply(550, name); 949ea022d16SRodney W. Grimes goto done; 950ea022d16SRodney W. Grimes } 951ea022d16SRodney W. Grimes if (c == '\n') 952ea022d16SRodney W. Grimes i++; 953ea022d16SRodney W. Grimes } 954ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 955ea022d16SRodney W. Grimes perror_reply(550, name); 956ea022d16SRodney W. Grimes goto done; 957ea022d16SRodney W. Grimes } 958ea022d16SRodney W. Grimes } 959ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 960ea022d16SRodney W. Grimes if (dout == NULL) 961ea022d16SRodney W. Grimes goto done; 9623eb568f2SGuido van Rooij time(&start); 9639fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 9649fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 9653eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 9663eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 967ea022d16SRodney W. Grimes (void) fclose(dout); 968ea022d16SRodney W. Grimes data = -1; 969ea022d16SRodney W. Grimes pdata = -1; 970ea022d16SRodney W. Grimes done: 971ea022d16SRodney W. Grimes if (cmd == 0) 972ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 973ea022d16SRodney W. Grimes (*closefunc)(fin); 974ea022d16SRodney W. Grimes } 975ea022d16SRodney W. Grimes 976ea022d16SRodney W. Grimes void 977ea022d16SRodney W. Grimes store(name, mode, unique) 978ea022d16SRodney W. Grimes char *name, *mode; 979ea022d16SRodney W. Grimes int unique; 980ea022d16SRodney W. Grimes { 981ea022d16SRodney W. Grimes FILE *fout, *din; 982ea022d16SRodney W. Grimes struct stat st; 983ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 984ea022d16SRodney W. Grimes 98561f891a6SPaul Traina if ((unique || guest) && stat(name, &st) == 0 && 986ea022d16SRodney W. Grimes (name = gunique(name)) == NULL) { 987ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 988ea022d16SRodney W. Grimes return; 989ea022d16SRodney W. Grimes } 990ea022d16SRodney W. Grimes 991ea022d16SRodney W. Grimes if (restart_point) 992ea022d16SRodney W. Grimes mode = "r+"; 993ea022d16SRodney W. Grimes fout = fopen(name, mode); 994ea022d16SRodney W. Grimes closefunc = fclose; 995ea022d16SRodney W. Grimes if (fout == NULL) { 996ea022d16SRodney W. Grimes perror_reply(553, name); 997ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 998ea022d16SRodney W. Grimes return; 999ea022d16SRodney W. Grimes } 1000ea022d16SRodney W. Grimes byte_count = -1; 1001ea022d16SRodney W. Grimes if (restart_point) { 1002ea022d16SRodney W. Grimes if (type == TYPE_A) { 1003ea022d16SRodney W. Grimes off_t i, n; 1004ea022d16SRodney W. Grimes int c; 1005ea022d16SRodney W. Grimes 1006ea022d16SRodney W. Grimes n = restart_point; 1007ea022d16SRodney W. Grimes i = 0; 1008ea022d16SRodney W. Grimes while (i++ < n) { 1009ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1010ea022d16SRodney W. Grimes perror_reply(550, name); 1011ea022d16SRodney W. Grimes goto done; 1012ea022d16SRodney W. Grimes } 1013ea022d16SRodney W. Grimes if (c == '\n') 1014ea022d16SRodney W. Grimes i++; 1015ea022d16SRodney W. Grimes } 1016ea022d16SRodney W. Grimes /* 1017ea022d16SRodney W. Grimes * We must do this seek to "current" position 1018ea022d16SRodney W. Grimes * because we are changing from reading to 1019ea022d16SRodney W. Grimes * writing. 1020ea022d16SRodney W. Grimes */ 1021ea022d16SRodney W. Grimes if (fseek(fout, 0L, L_INCR) < 0) { 1022ea022d16SRodney W. Grimes perror_reply(550, name); 1023ea022d16SRodney W. Grimes goto done; 1024ea022d16SRodney W. Grimes } 1025ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1026ea022d16SRodney W. Grimes perror_reply(550, name); 1027ea022d16SRodney W. Grimes goto done; 1028ea022d16SRodney W. Grimes } 1029ea022d16SRodney W. Grimes } 1030ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1031ea022d16SRodney W. Grimes if (din == NULL) 1032ea022d16SRodney W. Grimes goto done; 1033ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1034ea022d16SRodney W. Grimes if (unique) 1035ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1036ea022d16SRodney W. Grimes name); 1037ea022d16SRodney W. Grimes else 1038ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1039ea022d16SRodney W. Grimes } 1040ea022d16SRodney W. Grimes (void) fclose(din); 1041ea022d16SRodney W. Grimes data = -1; 1042ea022d16SRodney W. Grimes pdata = -1; 1043ea022d16SRodney W. Grimes done: 1044ea022d16SRodney W. Grimes LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1045ea022d16SRodney W. Grimes (*closefunc)(fout); 1046ea022d16SRodney W. Grimes } 1047ea022d16SRodney W. Grimes 1048ea022d16SRodney W. Grimes static FILE * 1049ea022d16SRodney W. Grimes getdatasock(mode) 1050ea022d16SRodney W. Grimes char *mode; 1051ea022d16SRodney W. Grimes { 1052ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1053ea022d16SRodney W. Grimes 1054ea022d16SRodney W. Grimes if (data >= 0) 1055ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1056ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1057ea022d16SRodney W. Grimes s = socket(AF_INET, SOCK_STREAM, 0); 1058ea022d16SRodney W. Grimes if (s < 0) 1059ea022d16SRodney W. Grimes goto bad; 1060ea022d16SRodney W. Grimes if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1061ea022d16SRodney W. Grimes (char *) &on, sizeof(on)) < 0) 1062ea022d16SRodney W. Grimes goto bad; 1063ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 1064a5a4544eSPaul Traina data_source.sin_len = sizeof(struct sockaddr_in); 1065ea022d16SRodney W. Grimes data_source.sin_family = AF_INET; 1066ea022d16SRodney W. Grimes data_source.sin_addr = ctrl_addr.sin_addr; 1067ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1068ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 1069ea022d16SRodney W. Grimes sizeof(data_source)) >= 0) 1070ea022d16SRodney W. Grimes break; 1071ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1072ea022d16SRodney W. Grimes goto bad; 1073ea022d16SRodney W. Grimes sleep(tries); 1074ea022d16SRodney W. Grimes } 1075ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1076ea022d16SRodney W. Grimes #ifdef IP_TOS 1077ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 1078ea022d16SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1079ea022d16SRodney W. Grimes syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 1080ea022d16SRodney W. Grimes #endif 10819fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 10829fc5823aSGarrett Wollman /* 10839fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 10849fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 10859fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 10869fc5823aSGarrett Wollman * in heavy-load situations. 10879fc5823aSGarrett Wollman */ 10889fc5823aSGarrett Wollman on = 1; 10899fc5823aSGarrett Wollman if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 10909fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 10919fc5823aSGarrett Wollman #endif 10929fc5823aSGarrett Wollman #ifdef SO_SNDBUF 10939fc5823aSGarrett Wollman on = 65536; 10949fc5823aSGarrett Wollman if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 10959fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 10969fc5823aSGarrett Wollman #endif 10979fc5823aSGarrett Wollman 1098ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1099ea022d16SRodney W. Grimes bad: 1100ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1101ea022d16SRodney W. Grimes t = errno; 1102ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1103ea022d16SRodney W. Grimes (void) close(s); 1104ea022d16SRodney W. Grimes errno = t; 1105ea022d16SRodney W. Grimes return (NULL); 1106ea022d16SRodney W. Grimes } 1107ea022d16SRodney W. Grimes 1108ea022d16SRodney W. Grimes static FILE * 1109ea022d16SRodney W. Grimes dataconn(name, size, mode) 1110ea022d16SRodney W. Grimes char *name; 1111ea022d16SRodney W. Grimes off_t size; 1112ea022d16SRodney W. Grimes char *mode; 1113ea022d16SRodney W. Grimes { 1114ea022d16SRodney W. Grimes char sizebuf[32]; 1115ea022d16SRodney W. Grimes FILE *file; 1116ea022d16SRodney W. Grimes int retry = 0, tos; 1117ea022d16SRodney W. Grimes 1118ea022d16SRodney W. Grimes file_size = size; 1119ea022d16SRodney W. Grimes byte_count = 0; 1120ea022d16SRodney W. Grimes if (size != (off_t) -1) 1121ea022d16SRodney W. Grimes (void) sprintf(sizebuf, " (%qd bytes)", size); 1122ea022d16SRodney W. Grimes else 1123ea022d16SRodney W. Grimes (void) strcpy(sizebuf, ""); 1124ea022d16SRodney W. Grimes if (pdata >= 0) { 1125ea022d16SRodney W. Grimes struct sockaddr_in from; 1126ea022d16SRodney W. Grimes int s, fromlen = sizeof(from); 1127d6ed3c37SGuido van Rooij struct timeval timeout; 1128d6ed3c37SGuido van Rooij fd_set set; 1129ea022d16SRodney W. Grimes 1130d6ed3c37SGuido van Rooij FD_ZERO(&set); 1131d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1132d6ed3c37SGuido van Rooij 1133d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1134d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1135d6ed3c37SGuido van Rooij 1136d6ed3c37SGuido van Rooij if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1137d6ed3c37SGuido van Rooij (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1138ea022d16SRodney W. Grimes reply(425, "Can't open data connection."); 1139ea022d16SRodney W. Grimes (void) close(pdata); 1140ea022d16SRodney W. Grimes pdata = -1; 1141ea022d16SRodney W. Grimes return (NULL); 1142ea022d16SRodney W. Grimes } 1143ea022d16SRodney W. Grimes (void) close(pdata); 1144ea022d16SRodney W. Grimes pdata = s; 1145ea022d16SRodney W. Grimes #ifdef IP_TOS 1146a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 1147ea022d16SRodney W. Grimes (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1148ea022d16SRodney W. Grimes sizeof(int)); 1149ea022d16SRodney W. Grimes #endif 1150ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1151ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1152ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 1153ea022d16SRodney W. Grimes } 1154ea022d16SRodney W. Grimes if (data >= 0) { 1155ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1156ea022d16SRodney W. Grimes name, sizebuf); 1157ea022d16SRodney W. Grimes usedefault = 1; 1158ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1159ea022d16SRodney W. Grimes } 1160ea022d16SRodney W. Grimes if (usedefault) 1161ea022d16SRodney W. Grimes data_dest = his_addr; 1162ea022d16SRodney W. Grimes usedefault = 1; 1163ea022d16SRodney W. Grimes file = getdatasock(mode); 1164ea022d16SRodney W. Grimes if (file == NULL) { 1165ea022d16SRodney W. Grimes reply(425, "Can't create data socket (%s,%d): %s.", 1166ea022d16SRodney W. Grimes inet_ntoa(data_source.sin_addr), 1167ea022d16SRodney W. Grimes ntohs(data_source.sin_port), strerror(errno)); 1168ea022d16SRodney W. Grimes return (NULL); 1169ea022d16SRodney W. Grimes } 1170ea022d16SRodney W. Grimes data = fileno(file); 1171ea022d16SRodney W. Grimes while (connect(data, (struct sockaddr *)&data_dest, 1172ea022d16SRodney W. Grimes sizeof(data_dest)) < 0) { 1173ea022d16SRodney W. Grimes if (errno == EADDRINUSE && retry < swaitmax) { 1174ea022d16SRodney W. Grimes sleep((unsigned) swaitint); 1175ea022d16SRodney W. Grimes retry += swaitint; 1176ea022d16SRodney W. Grimes continue; 1177ea022d16SRodney W. Grimes } 1178ea022d16SRodney W. Grimes perror_reply(425, "Can't build data connection"); 1179ea022d16SRodney W. Grimes (void) fclose(file); 1180ea022d16SRodney W. Grimes data = -1; 1181ea022d16SRodney W. Grimes return (NULL); 1182ea022d16SRodney W. Grimes } 1183ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1184ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1185ea022d16SRodney W. Grimes return (file); 1186ea022d16SRodney W. Grimes } 1187ea022d16SRodney W. Grimes 1188ea022d16SRodney W. Grimes /* 1189ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 11909fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1191ea022d16SRodney W. Grimes * 1192ea022d16SRodney W. Grimes * NB: Form isn't handled. 1193ea022d16SRodney W. Grimes */ 1194ea022d16SRodney W. Grimes static void 11959fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg) 1196ea022d16SRodney W. Grimes FILE *instr, *outstr; 1197ea022d16SRodney W. Grimes off_t blksize; 11989fc5823aSGarrett Wollman off_t filesize; 11999fc5823aSGarrett Wollman int isreg; 1200ea022d16SRodney W. Grimes { 1201ea022d16SRodney W. Grimes int c, cnt, filefd, netfd; 12029fc5823aSGarrett Wollman char *buf, *bp; 12039fc5823aSGarrett Wollman size_t len; 1204ea022d16SRodney W. Grimes 1205ea022d16SRodney W. Grimes transflag++; 1206ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1207ea022d16SRodney W. Grimes transflag = 0; 1208ea022d16SRodney W. Grimes return; 1209ea022d16SRodney W. Grimes } 1210ea022d16SRodney W. Grimes switch (type) { 1211ea022d16SRodney W. Grimes 1212ea022d16SRodney W. Grimes case TYPE_A: 1213ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1214ea022d16SRodney W. Grimes byte_count++; 1215ea022d16SRodney W. Grimes if (c == '\n') { 1216ea022d16SRodney W. Grimes if (ferror(outstr)) 1217ea022d16SRodney W. Grimes goto data_err; 1218ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1219ea022d16SRodney W. Grimes } 1220ea022d16SRodney W. Grimes (void) putc(c, outstr); 1221ea022d16SRodney W. Grimes } 1222ea022d16SRodney W. Grimes fflush(outstr); 1223ea022d16SRodney W. Grimes transflag = 0; 1224ea022d16SRodney W. Grimes if (ferror(instr)) 1225ea022d16SRodney W. Grimes goto file_err; 1226ea022d16SRodney W. Grimes if (ferror(outstr)) 1227ea022d16SRodney W. Grimes goto data_err; 1228ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1229ea022d16SRodney W. Grimes return; 1230ea022d16SRodney W. Grimes 1231ea022d16SRodney W. Grimes case TYPE_I: 1232ea022d16SRodney W. Grimes case TYPE_L: 12339fc5823aSGarrett Wollman /* 12349fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 12359fc5823aSGarrett Wollman * are sending a regular file 12369fc5823aSGarrett Wollman */ 12379fc5823aSGarrett Wollman netfd = fileno(outstr); 12389fc5823aSGarrett Wollman filefd = fileno(instr); 12399fc5823aSGarrett Wollman 12409fc5823aSGarrett Wollman if (isreg && filesize < (off_t)16 * 1024 * 1024) { 12419fc5823aSGarrett Wollman buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, 12429fc5823aSGarrett Wollman (off_t)0); 12438abdc2ebSAlexander Langer if (buf == MAP_FAILED) { 12449fc5823aSGarrett Wollman syslog(LOG_WARNING, "mmap(%lu): %m", 12459fc5823aSGarrett Wollman (unsigned long)filesize); 12469fc5823aSGarrett Wollman goto oldway; 12479fc5823aSGarrett Wollman } 12489fc5823aSGarrett Wollman bp = buf; 12499fc5823aSGarrett Wollman len = filesize; 12509fc5823aSGarrett Wollman do { 12519fc5823aSGarrett Wollman cnt = write(netfd, bp, len); 12529fc5823aSGarrett Wollman len -= cnt; 12539fc5823aSGarrett Wollman bp += cnt; 12549fc5823aSGarrett Wollman if (cnt > 0) byte_count += cnt; 12559fc5823aSGarrett Wollman } while(cnt > 0 && len > 0); 12569fc5823aSGarrett Wollman 12579fc5823aSGarrett Wollman transflag = 0; 12589fc5823aSGarrett Wollman munmap(buf, (size_t)filesize); 12599fc5823aSGarrett Wollman if (cnt < 0) 12609fc5823aSGarrett Wollman goto data_err; 12619fc5823aSGarrett Wollman reply(226, "Transfer complete."); 12629fc5823aSGarrett Wollman return; 12639fc5823aSGarrett Wollman } 12649fc5823aSGarrett Wollman 12659fc5823aSGarrett Wollman oldway: 1266ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1267ea022d16SRodney W. Grimes transflag = 0; 1268ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 1269ea022d16SRodney W. Grimes return; 1270ea022d16SRodney W. Grimes } 12719fc5823aSGarrett Wollman 1272ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1273ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1274ea022d16SRodney W. Grimes byte_count += cnt; 1275ea022d16SRodney W. Grimes transflag = 0; 1276ea022d16SRodney W. Grimes (void)free(buf); 1277ea022d16SRodney W. Grimes if (cnt != 0) { 1278ea022d16SRodney W. Grimes if (cnt < 0) 1279ea022d16SRodney W. Grimes goto file_err; 1280ea022d16SRodney W. Grimes goto data_err; 1281ea022d16SRodney W. Grimes } 1282ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1283ea022d16SRodney W. Grimes return; 1284ea022d16SRodney W. Grimes default: 1285ea022d16SRodney W. Grimes transflag = 0; 1286ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 1287ea022d16SRodney W. Grimes return; 1288ea022d16SRodney W. Grimes } 1289ea022d16SRodney W. Grimes 1290ea022d16SRodney W. Grimes data_err: 1291ea022d16SRodney W. Grimes transflag = 0; 1292ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 1293ea022d16SRodney W. Grimes return; 1294ea022d16SRodney W. Grimes 1295ea022d16SRodney W. Grimes file_err: 1296ea022d16SRodney W. Grimes transflag = 0; 1297ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 1298ea022d16SRodney W. Grimes } 1299ea022d16SRodney W. Grimes 1300ea022d16SRodney W. Grimes /* 1301ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 1302ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 1303ea022d16SRodney W. Grimes * 1304ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 1305ea022d16SRodney W. Grimes */ 1306ea022d16SRodney W. Grimes static int 1307ea022d16SRodney W. Grimes receive_data(instr, outstr) 1308ea022d16SRodney W. Grimes FILE *instr, *outstr; 1309ea022d16SRodney W. Grimes { 1310ea022d16SRodney W. Grimes int c; 131161f891a6SPaul Traina int cnt, bare_lfs; 1312ea022d16SRodney W. Grimes char buf[BUFSIZ]; 1313ea022d16SRodney W. Grimes 1314ea022d16SRodney W. Grimes transflag++; 1315ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1316ea022d16SRodney W. Grimes transflag = 0; 1317ea022d16SRodney W. Grimes return (-1); 1318ea022d16SRodney W. Grimes } 131961f891a6SPaul Traina 132061f891a6SPaul Traina bare_lfs = 0; 132161f891a6SPaul Traina 1322ea022d16SRodney W. Grimes switch (type) { 1323ea022d16SRodney W. Grimes 1324ea022d16SRodney W. Grimes case TYPE_I: 1325ea022d16SRodney W. Grimes case TYPE_L: 1326ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1327ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 1328ea022d16SRodney W. Grimes goto file_err; 1329ea022d16SRodney W. Grimes byte_count += cnt; 1330ea022d16SRodney W. Grimes } 1331ea022d16SRodney W. Grimes if (cnt < 0) 1332ea022d16SRodney W. Grimes goto data_err; 1333ea022d16SRodney W. Grimes transflag = 0; 1334ea022d16SRodney W. Grimes return (0); 1335ea022d16SRodney W. Grimes 1336ea022d16SRodney W. Grimes case TYPE_E: 1337ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 1338ea022d16SRodney W. Grimes transflag = 0; 1339ea022d16SRodney W. Grimes return (-1); 1340ea022d16SRodney W. Grimes 1341ea022d16SRodney W. Grimes case TYPE_A: 1342ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1343ea022d16SRodney W. Grimes byte_count++; 1344ea022d16SRodney W. Grimes if (c == '\n') 1345ea022d16SRodney W. Grimes bare_lfs++; 1346ea022d16SRodney W. Grimes while (c == '\r') { 1347ea022d16SRodney W. Grimes if (ferror(outstr)) 1348ea022d16SRodney W. Grimes goto data_err; 1349ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 1350ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 1351ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 1352ea022d16SRodney W. Grimes goto contin2; 1353ea022d16SRodney W. Grimes } 1354ea022d16SRodney W. Grimes } 1355ea022d16SRodney W. Grimes (void) putc(c, outstr); 1356ea022d16SRodney W. Grimes contin2: ; 1357ea022d16SRodney W. Grimes } 1358ea022d16SRodney W. Grimes fflush(outstr); 1359ea022d16SRodney W. Grimes if (ferror(instr)) 1360ea022d16SRodney W. Grimes goto data_err; 1361ea022d16SRodney W. Grimes if (ferror(outstr)) 1362ea022d16SRodney W. Grimes goto file_err; 1363ea022d16SRodney W. Grimes transflag = 0; 1364ea022d16SRodney W. Grimes if (bare_lfs) { 1365ea022d16SRodney W. Grimes lreply(226, 1366ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 1367ea022d16SRodney W. Grimes bare_lfs); 1368ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 1369ea022d16SRodney W. Grimes } 1370ea022d16SRodney W. Grimes return (0); 1371ea022d16SRodney W. Grimes default: 1372ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 1373ea022d16SRodney W. Grimes transflag = 0; 1374ea022d16SRodney W. Grimes return (-1); 1375ea022d16SRodney W. Grimes } 1376ea022d16SRodney W. Grimes 1377ea022d16SRodney W. Grimes data_err: 1378ea022d16SRodney W. Grimes transflag = 0; 1379ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 1380ea022d16SRodney W. Grimes return (-1); 1381ea022d16SRodney W. Grimes 1382ea022d16SRodney W. Grimes file_err: 1383ea022d16SRodney W. Grimes transflag = 0; 1384ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 1385ea022d16SRodney W. Grimes return (-1); 1386ea022d16SRodney W. Grimes } 1387ea022d16SRodney W. Grimes 1388ea022d16SRodney W. Grimes void 1389ea022d16SRodney W. Grimes statfilecmd(filename) 1390ea022d16SRodney W. Grimes char *filename; 1391ea022d16SRodney W. Grimes { 1392ea022d16SRodney W. Grimes FILE *fin; 1393ea022d16SRodney W. Grimes int c; 1394ea022d16SRodney W. Grimes char line[LINE_MAX]; 1395ea022d16SRodney W. Grimes 1396af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 1397ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 1398ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 1399ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 1400ea022d16SRodney W. Grimes if (c == '\n') { 1401ea022d16SRodney W. Grimes if (ferror(stdout)){ 1402ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 1403ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1404ea022d16SRodney W. Grimes dologout(1); 1405ea022d16SRodney W. Grimes /* NOTREACHED */ 1406ea022d16SRodney W. Grimes } 1407ea022d16SRodney W. Grimes if (ferror(fin)) { 1408ea022d16SRodney W. Grimes perror_reply(551, filename); 1409ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1410ea022d16SRodney W. Grimes return; 1411ea022d16SRodney W. Grimes } 1412ea022d16SRodney W. Grimes (void) putc('\r', stdout); 1413ea022d16SRodney W. Grimes } 1414ea022d16SRodney W. Grimes (void) putc(c, stdout); 1415ea022d16SRodney W. Grimes } 1416ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1417ea022d16SRodney W. Grimes reply(211, "End of Status"); 1418ea022d16SRodney W. Grimes } 1419ea022d16SRodney W. Grimes 1420ea022d16SRodney W. Grimes void 1421ea022d16SRodney W. Grimes statcmd() 1422ea022d16SRodney W. Grimes { 1423ea022d16SRodney W. Grimes struct sockaddr_in *sin; 1424ea022d16SRodney W. Grimes u_char *a, *p; 1425ea022d16SRodney W. Grimes 1426ea022d16SRodney W. Grimes lreply(211, "%s FTP server status:", hostname, version); 1427ea022d16SRodney W. Grimes printf(" %s\r\n", version); 1428ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 1429ea022d16SRodney W. Grimes if (!isdigit(remotehost[0])) 1430ea022d16SRodney W. Grimes printf(" (%s)", inet_ntoa(his_addr.sin_addr)); 1431ea022d16SRodney W. Grimes printf("\r\n"); 1432ea022d16SRodney W. Grimes if (logged_in) { 1433ea022d16SRodney W. Grimes if (guest) 1434ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 1435ea022d16SRodney W. Grimes else 1436ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 1437ea022d16SRodney W. Grimes } else if (askpasswd) 1438ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 1439ea022d16SRodney W. Grimes else 1440ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 1441ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 1442ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 1443ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 1444ea022d16SRodney W. Grimes if (type == TYPE_L) 1445ea022d16SRodney W. Grimes #if NBBY == 8 1446ea022d16SRodney W. Grimes printf(" %d", NBBY); 1447ea022d16SRodney W. Grimes #else 1448ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 1449ea022d16SRodney W. Grimes #endif 1450ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 1451ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 1452ea022d16SRodney W. Grimes if (data != -1) 1453ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 1454ea022d16SRodney W. Grimes else if (pdata != -1) { 1455ea022d16SRodney W. Grimes printf(" in Passive mode"); 1456ea022d16SRodney W. Grimes sin = &pasv_addr; 1457ea022d16SRodney W. Grimes goto printaddr; 1458ea022d16SRodney W. Grimes } else if (usedefault == 0) { 1459ea022d16SRodney W. Grimes printf(" PORT"); 1460ea022d16SRodney W. Grimes sin = &data_dest; 1461ea022d16SRodney W. Grimes printaddr: 1462ea022d16SRodney W. Grimes a = (u_char *) &sin->sin_addr; 1463ea022d16SRodney W. Grimes p = (u_char *) &sin->sin_port; 1464ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 1465ea022d16SRodney W. Grimes printf(" (%d,%d,%d,%d,%d,%d)\r\n", UC(a[0]), 1466ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1467ea022d16SRodney W. Grimes #undef UC 1468ea022d16SRodney W. Grimes } else 1469ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 1470ea022d16SRodney W. Grimes reply(211, "End of status"); 1471ea022d16SRodney W. Grimes } 1472ea022d16SRodney W. Grimes 1473ea022d16SRodney W. Grimes void 1474ea022d16SRodney W. Grimes fatal(s) 1475ea022d16SRodney W. Grimes char *s; 1476ea022d16SRodney W. Grimes { 1477ea022d16SRodney W. Grimes 1478ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 1479ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 1480ea022d16SRodney W. Grimes dologout(0); 1481ea022d16SRodney W. Grimes /* NOTREACHED */ 1482ea022d16SRodney W. Grimes } 1483ea022d16SRodney W. Grimes 1484ea022d16SRodney W. Grimes void 1485ea022d16SRodney W. Grimes #if __STDC__ 1486ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 1487ea022d16SRodney W. Grimes #else 1488ea022d16SRodney W. Grimes reply(n, fmt, va_alist) 1489ea022d16SRodney W. Grimes int n; 1490ea022d16SRodney W. Grimes char *fmt; 1491ea022d16SRodney W. Grimes va_dcl 1492ea022d16SRodney W. Grimes #endif 1493ea022d16SRodney W. Grimes { 1494ea022d16SRodney W. Grimes va_list ap; 1495ea022d16SRodney W. Grimes #if __STDC__ 1496ea022d16SRodney W. Grimes va_start(ap, fmt); 1497ea022d16SRodney W. Grimes #else 1498ea022d16SRodney W. Grimes va_start(ap); 1499ea022d16SRodney W. Grimes #endif 1500ea022d16SRodney W. Grimes (void)printf("%d ", n); 1501ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 1502ea022d16SRodney W. Grimes (void)printf("\r\n"); 1503ea022d16SRodney W. Grimes (void)fflush(stdout); 1504ea022d16SRodney W. Grimes if (debug) { 1505ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 1506ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 1507ea022d16SRodney W. Grimes } 1508ea022d16SRodney W. Grimes } 1509ea022d16SRodney W. Grimes 1510ea022d16SRodney W. Grimes void 1511ea022d16SRodney W. Grimes #if __STDC__ 1512ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 1513ea022d16SRodney W. Grimes #else 1514ea022d16SRodney W. Grimes lreply(n, fmt, va_alist) 1515ea022d16SRodney W. Grimes int n; 1516ea022d16SRodney W. Grimes char *fmt; 1517ea022d16SRodney W. Grimes va_dcl 1518ea022d16SRodney W. Grimes #endif 1519ea022d16SRodney W. Grimes { 1520ea022d16SRodney W. Grimes va_list ap; 1521ea022d16SRodney W. Grimes #if __STDC__ 1522ea022d16SRodney W. Grimes va_start(ap, fmt); 1523ea022d16SRodney W. Grimes #else 1524ea022d16SRodney W. Grimes va_start(ap); 1525ea022d16SRodney W. Grimes #endif 1526ea022d16SRodney W. Grimes (void)printf("%d- ", n); 1527ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 1528ea022d16SRodney W. Grimes (void)printf("\r\n"); 1529ea022d16SRodney W. Grimes (void)fflush(stdout); 1530ea022d16SRodney W. Grimes if (debug) { 1531ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 1532ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 1533ea022d16SRodney W. Grimes } 1534ea022d16SRodney W. Grimes } 1535ea022d16SRodney W. Grimes 1536ea022d16SRodney W. Grimes static void 1537ea022d16SRodney W. Grimes ack(s) 1538ea022d16SRodney W. Grimes char *s; 1539ea022d16SRodney W. Grimes { 1540ea022d16SRodney W. Grimes 1541ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 1542ea022d16SRodney W. Grimes } 1543ea022d16SRodney W. Grimes 1544ea022d16SRodney W. Grimes void 1545ea022d16SRodney W. Grimes nack(s) 1546ea022d16SRodney W. Grimes char *s; 1547ea022d16SRodney W. Grimes { 1548ea022d16SRodney W. Grimes 1549ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 1550ea022d16SRodney W. Grimes } 1551ea022d16SRodney W. Grimes 1552ea022d16SRodney W. Grimes /* ARGSUSED */ 1553ea022d16SRodney W. Grimes void 1554ea022d16SRodney W. Grimes yyerror(s) 1555ea022d16SRodney W. Grimes char *s; 1556ea022d16SRodney W. Grimes { 1557ea022d16SRodney W. Grimes char *cp; 1558ea022d16SRodney W. Grimes 15599aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 1560ea022d16SRodney W. Grimes *cp = '\0'; 1561ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 1562ea022d16SRodney W. Grimes } 1563ea022d16SRodney W. Grimes 1564ea022d16SRodney W. Grimes void 1565ea022d16SRodney W. Grimes delete(name) 1566ea022d16SRodney W. Grimes char *name; 1567ea022d16SRodney W. Grimes { 1568ea022d16SRodney W. Grimes struct stat st; 1569ea022d16SRodney W. Grimes 1570ea022d16SRodney W. Grimes LOGCMD("delete", name); 1571ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 1572ea022d16SRodney W. Grimes perror_reply(550, name); 1573ea022d16SRodney W. Grimes return; 1574ea022d16SRodney W. Grimes } 1575ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 1576ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 1577ea022d16SRodney W. Grimes perror_reply(550, name); 1578ea022d16SRodney W. Grimes return; 1579ea022d16SRodney W. Grimes } 1580ea022d16SRodney W. Grimes goto done; 1581ea022d16SRodney W. Grimes } 1582ea022d16SRodney W. Grimes if (unlink(name) < 0) { 1583ea022d16SRodney W. Grimes perror_reply(550, name); 1584ea022d16SRodney W. Grimes return; 1585ea022d16SRodney W. Grimes } 1586ea022d16SRodney W. Grimes done: 1587ea022d16SRodney W. Grimes ack("DELE"); 1588ea022d16SRodney W. Grimes } 1589ea022d16SRodney W. Grimes 1590ea022d16SRodney W. Grimes void 1591ea022d16SRodney W. Grimes cwd(path) 1592ea022d16SRodney W. Grimes char *path; 1593ea022d16SRodney W. Grimes { 1594ea022d16SRodney W. Grimes 1595ea022d16SRodney W. Grimes if (chdir(path) < 0) 1596ea022d16SRodney W. Grimes perror_reply(550, path); 1597ea022d16SRodney W. Grimes else 1598ea022d16SRodney W. Grimes ack("CWD"); 1599ea022d16SRodney W. Grimes } 1600ea022d16SRodney W. Grimes 1601ea022d16SRodney W. Grimes void 1602ea022d16SRodney W. Grimes makedir(name) 1603ea022d16SRodney W. Grimes char *name; 1604ea022d16SRodney W. Grimes { 1605ea022d16SRodney W. Grimes 1606ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 1607ea022d16SRodney W. Grimes if (mkdir(name, 0777) < 0) 1608ea022d16SRodney W. Grimes perror_reply(550, name); 1609ea022d16SRodney W. Grimes else 1610ea022d16SRodney W. Grimes reply(257, "MKD command successful."); 1611ea022d16SRodney W. Grimes } 1612ea022d16SRodney W. Grimes 1613ea022d16SRodney W. Grimes void 1614ea022d16SRodney W. Grimes removedir(name) 1615ea022d16SRodney W. Grimes char *name; 1616ea022d16SRodney W. Grimes { 1617ea022d16SRodney W. Grimes 1618ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 1619ea022d16SRodney W. Grimes if (rmdir(name) < 0) 1620ea022d16SRodney W. Grimes perror_reply(550, name); 1621ea022d16SRodney W. Grimes else 1622ea022d16SRodney W. Grimes ack("RMD"); 1623ea022d16SRodney W. Grimes } 1624ea022d16SRodney W. Grimes 1625ea022d16SRodney W. Grimes void 1626ea022d16SRodney W. Grimes pwd() 1627ea022d16SRodney W. Grimes { 1628ea022d16SRodney W. Grimes char path[MAXPATHLEN + 1]; 1629ea022d16SRodney W. Grimes 1630ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 1631ea022d16SRodney W. Grimes reply(550, "%s.", path); 1632ea022d16SRodney W. Grimes else 1633ea022d16SRodney W. Grimes reply(257, "\"%s\" is current directory.", path); 1634ea022d16SRodney W. Grimes } 1635ea022d16SRodney W. Grimes 1636ea022d16SRodney W. Grimes char * 1637ea022d16SRodney W. Grimes renamefrom(name) 1638ea022d16SRodney W. Grimes char *name; 1639ea022d16SRodney W. Grimes { 1640ea022d16SRodney W. Grimes struct stat st; 1641ea022d16SRodney W. Grimes 1642ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 1643ea022d16SRodney W. Grimes perror_reply(550, name); 1644ea022d16SRodney W. Grimes return ((char *)0); 1645ea022d16SRodney W. Grimes } 1646ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 1647ea022d16SRodney W. Grimes return (name); 1648ea022d16SRodney W. Grimes } 1649ea022d16SRodney W. Grimes 1650ea022d16SRodney W. Grimes void 1651ea022d16SRodney W. Grimes renamecmd(from, to) 1652ea022d16SRodney W. Grimes char *from, *to; 1653ea022d16SRodney W. Grimes { 165461f891a6SPaul Traina struct stat st; 1655ea022d16SRodney W. Grimes 1656ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 165761f891a6SPaul Traina 165861f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 165961f891a6SPaul Traina reply(550, "%s: permission denied", to); 166061f891a6SPaul Traina return; 166161f891a6SPaul Traina } 166261f891a6SPaul Traina 1663ea022d16SRodney W. Grimes if (rename(from, to) < 0) 1664ea022d16SRodney W. Grimes perror_reply(550, "rename"); 1665ea022d16SRodney W. Grimes else 1666ea022d16SRodney W. Grimes ack("RNTO"); 1667ea022d16SRodney W. Grimes } 1668ea022d16SRodney W. Grimes 1669ea022d16SRodney W. Grimes static void 1670ea022d16SRodney W. Grimes dolog(sin) 1671ea022d16SRodney W. Grimes struct sockaddr_in *sin; 1672ea022d16SRodney W. Grimes { 1673ea022d16SRodney W. Grimes struct hostent *hp = gethostbyaddr((char *)&sin->sin_addr, 1674ea022d16SRodney W. Grimes sizeof(struct in_addr), AF_INET); 1675ea022d16SRodney W. Grimes 1676ea022d16SRodney W. Grimes if (hp) 1677ea022d16SRodney W. Grimes (void) strncpy(remotehost, hp->h_name, sizeof(remotehost)); 1678ea022d16SRodney W. Grimes else 1679ea022d16SRodney W. Grimes (void) strncpy(remotehost, inet_ntoa(sin->sin_addr), 1680ea022d16SRodney W. Grimes sizeof(remotehost)); 1681ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1682ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost); 1683b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1684ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1685ea022d16SRodney W. Grimes 1686ea022d16SRodney W. Grimes if (logging) 1687ea022d16SRodney W. Grimes syslog(LOG_INFO, "connection from %s", remotehost); 1688ea022d16SRodney W. Grimes } 1689ea022d16SRodney W. Grimes 1690ea022d16SRodney W. Grimes /* 1691ea022d16SRodney W. Grimes * Record logout in wtmp file 1692ea022d16SRodney W. Grimes * and exit with supplied status. 1693ea022d16SRodney W. Grimes */ 1694ea022d16SRodney W. Grimes void 1695ea022d16SRodney W. Grimes dologout(status) 1696ea022d16SRodney W. Grimes int status; 1697ea022d16SRodney W. Grimes { 16980b4df2eeSDavid Greenman /* 16990b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 17000b4df2eeSDavid Greenman * back to the main program loop. 17010b4df2eeSDavid Greenman */ 17020b4df2eeSDavid Greenman transflag = 0; 1703ea022d16SRodney W. Grimes 1704ea022d16SRodney W. Grimes if (logged_in) { 1705ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1706ea022d16SRodney W. Grimes logwtmp(ttyline, "", ""); 1707a5a4544eSPaul Traina #if defined(KERBEROS) 1708a5a4544eSPaul Traina if (!notickets && krbtkfile_env) 1709a5a4544eSPaul Traina unlink(krbtkfile_env); 1710a5a4544eSPaul Traina #endif 1711ea022d16SRodney W. Grimes } 1712ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 1713ea022d16SRodney W. Grimes _exit(status); 1714ea022d16SRodney W. Grimes } 1715ea022d16SRodney W. Grimes 1716ea022d16SRodney W. Grimes static void 1717ea022d16SRodney W. Grimes myoob(signo) 1718ea022d16SRodney W. Grimes int signo; 1719ea022d16SRodney W. Grimes { 1720ea022d16SRodney W. Grimes char *cp; 1721ea022d16SRodney W. Grimes 1722ea022d16SRodney W. Grimes /* only process if transfer occurring */ 1723ea022d16SRodney W. Grimes if (!transflag) 1724ea022d16SRodney W. Grimes return; 1725ea022d16SRodney W. Grimes cp = tmpline; 1726ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 1727ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 1728ea022d16SRodney W. Grimes dologout(0); 1729ea022d16SRodney W. Grimes } 1730ea022d16SRodney W. Grimes upper(cp); 1731ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 1732ea022d16SRodney W. Grimes tmpline[0] = '\0'; 1733ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 1734ea022d16SRodney W. Grimes reply(226, "Abort successful"); 1735ea022d16SRodney W. Grimes longjmp(urgcatch, 1); 1736ea022d16SRodney W. Grimes } 1737ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 1738ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 1739ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 1740ea022d16SRodney W. Grimes byte_count, file_size); 1741ea022d16SRodney W. Grimes else 1742ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 1743ea022d16SRodney W. Grimes } 1744ea022d16SRodney W. Grimes } 1745ea022d16SRodney W. Grimes 1746ea022d16SRodney W. Grimes /* 1747ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 1748ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 1749ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 1750ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 1751ea022d16SRodney W. Grimes */ 1752ea022d16SRodney W. Grimes void 1753ea022d16SRodney W. Grimes passive() 1754ea022d16SRodney W. Grimes { 1755dacc9752SPaul Traina int len; 1756ea022d16SRodney W. Grimes char *p, *a; 1757ea022d16SRodney W. Grimes 175861f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 175961f891a6SPaul Traina close(pdata); 176061f891a6SPaul Traina 1761ea022d16SRodney W. Grimes pdata = socket(AF_INET, SOCK_STREAM, 0); 1762ea022d16SRodney W. Grimes if (pdata < 0) { 1763ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 1764ea022d16SRodney W. Grimes return; 1765ea022d16SRodney W. Grimes } 17664c450ad7SPaul Traina 17674c450ad7SPaul Traina (void) seteuid((uid_t)0); 176861f891a6SPaul Traina 1769dacc9752SPaul Traina #ifdef IP_PORTRANGE 1770dacc9752SPaul Traina { 1771dacc9752SPaul Traina int on = restricted_data_ports ? IP_PORTRANGE_HIGH 1772dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 1773dacc9752SPaul Traina 177440e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 1775dacc9752SPaul Traina (char *)&on, sizeof(on)) < 0) 17764c450ad7SPaul Traina goto pasv_error; 17774c450ad7SPaul Traina } 1778dacc9752SPaul Traina #endif 177940e9d39eSPeter Wemm 1780ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 1781ea022d16SRodney W. Grimes pasv_addr.sin_port = 0; 17824c450ad7SPaul Traina if (bind(pdata, (struct sockaddr *)&pasv_addr, 178361f891a6SPaul Traina sizeof(pasv_addr)) < 0) 1784ea022d16SRodney W. Grimes goto pasv_error; 178561f891a6SPaul Traina 1786ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 17874c450ad7SPaul Traina 1788ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 1789ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 1790ea022d16SRodney W. Grimes goto pasv_error; 1791ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 1792ea022d16SRodney W. Grimes goto pasv_error; 1793ea022d16SRodney W. Grimes a = (char *) &pasv_addr.sin_addr; 1794ea022d16SRodney W. Grimes p = (char *) &pasv_addr.sin_port; 1795ea022d16SRodney W. Grimes 1796ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 1797ea022d16SRodney W. Grimes 1798ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 1799ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 1800ea022d16SRodney W. Grimes return; 1801ea022d16SRodney W. Grimes 1802ea022d16SRodney W. Grimes pasv_error: 180361f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 1804ea022d16SRodney W. Grimes (void) close(pdata); 1805ea022d16SRodney W. Grimes pdata = -1; 1806ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 1807ea022d16SRodney W. Grimes return; 1808ea022d16SRodney W. Grimes } 1809ea022d16SRodney W. Grimes 1810ea022d16SRodney W. Grimes /* 1811ea022d16SRodney W. Grimes * Generate unique name for file with basename "local". 1812ea022d16SRodney W. Grimes * The file named "local" is already known to exist. 1813ea022d16SRodney W. Grimes * Generates failure reply on error. 1814ea022d16SRodney W. Grimes */ 1815ea022d16SRodney W. Grimes static char * 1816ea022d16SRodney W. Grimes gunique(local) 1817ea022d16SRodney W. Grimes char *local; 1818ea022d16SRodney W. Grimes { 1819ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 1820ea022d16SRodney W. Grimes struct stat st; 1821ea022d16SRodney W. Grimes int count; 1822ea022d16SRodney W. Grimes char *cp; 1823ea022d16SRodney W. Grimes 1824ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 1825ea022d16SRodney W. Grimes if (cp) 1826ea022d16SRodney W. Grimes *cp = '\0'; 1827ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 1828ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 1829ea022d16SRodney W. Grimes return ((char *) 0); 1830ea022d16SRodney W. Grimes } 1831ea022d16SRodney W. Grimes if (cp) 1832ea022d16SRodney W. Grimes *cp = '/'; 183361f891a6SPaul Traina (void) snprintf(new, sizeof(new), "%s", local); 1834ea022d16SRodney W. Grimes cp = new + strlen(new); 1835ea022d16SRodney W. Grimes *cp++ = '.'; 1836ea022d16SRodney W. Grimes for (count = 1; count < 100; count++) { 1837ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 1838ea022d16SRodney W. Grimes if (stat(new, &st) < 0) 1839ea022d16SRodney W. Grimes return (new); 1840ea022d16SRodney W. Grimes } 1841ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 1842ea022d16SRodney W. Grimes return (NULL); 1843ea022d16SRodney W. Grimes } 1844ea022d16SRodney W. Grimes 1845ea022d16SRodney W. Grimes /* 1846ea022d16SRodney W. Grimes * Format and send reply containing system error number. 1847ea022d16SRodney W. Grimes */ 1848ea022d16SRodney W. Grimes void 1849ea022d16SRodney W. Grimes perror_reply(code, string) 1850ea022d16SRodney W. Grimes int code; 1851ea022d16SRodney W. Grimes char *string; 1852ea022d16SRodney W. Grimes { 1853ea022d16SRodney W. Grimes 1854ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 1855ea022d16SRodney W. Grimes } 1856ea022d16SRodney W. Grimes 1857ea022d16SRodney W. Grimes static char *onefile[] = { 1858ea022d16SRodney W. Grimes "", 1859ea022d16SRodney W. Grimes 0 1860ea022d16SRodney W. Grimes }; 1861ea022d16SRodney W. Grimes 1862ea022d16SRodney W. Grimes void 1863ea022d16SRodney W. Grimes send_file_list(whichf) 1864ea022d16SRodney W. Grimes char *whichf; 1865ea022d16SRodney W. Grimes { 1866ea022d16SRodney W. Grimes struct stat st; 1867ea022d16SRodney W. Grimes DIR *dirp = NULL; 1868ea022d16SRodney W. Grimes struct dirent *dir; 1869ea022d16SRodney W. Grimes FILE *dout = NULL; 1870ea022d16SRodney W. Grimes char **dirlist, *dirname; 1871ea022d16SRodney W. Grimes int simple = 0; 1872ea022d16SRodney W. Grimes int freeglob = 0; 1873ea022d16SRodney W. Grimes glob_t gl; 1874ea022d16SRodney W. Grimes 1875ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 1876ea022d16SRodney W. Grimes int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 1877ea022d16SRodney W. Grimes 1878ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 1879ea022d16SRodney W. Grimes freeglob = 1; 1880ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 1881ea022d16SRodney W. Grimes reply(550, "not found"); 1882ea022d16SRodney W. Grimes goto out; 1883ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 1884ea022d16SRodney W. Grimes errno = ENOENT; 1885ea022d16SRodney W. Grimes perror_reply(550, whichf); 1886ea022d16SRodney W. Grimes goto out; 1887ea022d16SRodney W. Grimes } 1888ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 1889ea022d16SRodney W. Grimes } else { 1890ea022d16SRodney W. Grimes onefile[0] = whichf; 1891ea022d16SRodney W. Grimes dirlist = onefile; 1892ea022d16SRodney W. Grimes simple = 1; 1893ea022d16SRodney W. Grimes } 1894ea022d16SRodney W. Grimes 1895ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1896ea022d16SRodney W. Grimes transflag = 0; 1897ea022d16SRodney W. Grimes goto out; 1898ea022d16SRodney W. Grimes } 18999aca17cbSMark Murray while ((dirname = *dirlist++)) { 1900ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 1901ea022d16SRodney W. Grimes /* 1902ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 1903ea022d16SRodney W. Grimes * used NLST, do what the user meant. 1904ea022d16SRodney W. Grimes */ 1905ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 1906ea022d16SRodney W. Grimes transflag == 0) { 1907af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 1908ea022d16SRodney W. Grimes goto out; 1909ea022d16SRodney W. Grimes } 1910ea022d16SRodney W. Grimes perror_reply(550, whichf); 1911ea022d16SRodney W. Grimes if (dout != NULL) { 1912ea022d16SRodney W. Grimes (void) fclose(dout); 1913ea022d16SRodney W. Grimes transflag = 0; 1914ea022d16SRodney W. Grimes data = -1; 1915ea022d16SRodney W. Grimes pdata = -1; 1916ea022d16SRodney W. Grimes } 1917ea022d16SRodney W. Grimes goto out; 1918ea022d16SRodney W. Grimes } 1919ea022d16SRodney W. Grimes 1920ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 1921ea022d16SRodney W. Grimes if (dout == NULL) { 1922ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 1923ea022d16SRodney W. Grimes if (dout == NULL) 1924ea022d16SRodney W. Grimes goto out; 1925ea022d16SRodney W. Grimes transflag++; 1926ea022d16SRodney W. Grimes } 1927ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 1928ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 1929ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 1930ea022d16SRodney W. Grimes continue; 1931ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 1932ea022d16SRodney W. Grimes continue; 1933ea022d16SRodney W. Grimes 1934ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 1935ea022d16SRodney W. Grimes continue; 1936ea022d16SRodney W. Grimes 1937ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 1938ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 1939ea022d16SRodney W. Grimes 1940ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 1941ea022d16SRodney W. Grimes continue; 1942ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 1943ea022d16SRodney W. Grimes dir->d_namlen == 2) 1944ea022d16SRodney W. Grimes continue; 1945ea022d16SRodney W. Grimes 1946ea022d16SRodney W. Grimes sprintf(nbuf, "%s/%s", dirname, dir->d_name); 1947ea022d16SRodney W. Grimes 1948ea022d16SRodney W. Grimes /* 1949ea022d16SRodney W. Grimes * We have to do a stat to insure it's 1950ea022d16SRodney W. Grimes * not a directory or special file. 1951ea022d16SRodney W. Grimes */ 1952ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 1953ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 1954ea022d16SRodney W. Grimes if (dout == NULL) { 1955ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 1956ea022d16SRodney W. Grimes "w"); 1957ea022d16SRodney W. Grimes if (dout == NULL) 1958ea022d16SRodney W. Grimes goto out; 1959ea022d16SRodney W. Grimes transflag++; 1960ea022d16SRodney W. Grimes } 1961ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 1962ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 1963ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 1964ea022d16SRodney W. Grimes else 1965ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 1966ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 1967ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 1968ea022d16SRodney W. Grimes } 1969ea022d16SRodney W. Grimes } 1970ea022d16SRodney W. Grimes (void) closedir(dirp); 1971ea022d16SRodney W. Grimes } 1972ea022d16SRodney W. Grimes 1973ea022d16SRodney W. Grimes if (dout == NULL) 1974ea022d16SRodney W. Grimes reply(550, "No files found."); 1975ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 1976ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 1977ea022d16SRodney W. Grimes else 1978ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1979ea022d16SRodney W. Grimes 1980ea022d16SRodney W. Grimes transflag = 0; 1981ea022d16SRodney W. Grimes if (dout != NULL) 1982ea022d16SRodney W. Grimes (void) fclose(dout); 1983ea022d16SRodney W. Grimes data = -1; 1984ea022d16SRodney W. Grimes pdata = -1; 1985ea022d16SRodney W. Grimes out: 1986ea022d16SRodney W. Grimes if (freeglob) { 1987ea022d16SRodney W. Grimes freeglob = 0; 1988ea022d16SRodney W. Grimes globfree(&gl); 1989ea022d16SRodney W. Grimes } 1990ea022d16SRodney W. Grimes } 1991ea022d16SRodney W. Grimes 1992cf09a206SDavid Greenman void 1993cf09a206SDavid Greenman reapchild(signo) 1994cf09a206SDavid Greenman int signo; 1995cf09a206SDavid Greenman { 1996cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 1997cf09a206SDavid Greenman } 1998cf09a206SDavid Greenman 1999b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2000ea022d16SRodney W. Grimes /* 2001ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2002ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2003ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2004ea022d16SRodney W. Grimes */ 2005ea022d16SRodney W. Grimes void 2006ea022d16SRodney W. Grimes #if __STDC__ 2007ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2008ea022d16SRodney W. Grimes #else 2009ea022d16SRodney W. Grimes setproctitle(fmt, va_alist) 2010ea022d16SRodney W. Grimes char *fmt; 2011ea022d16SRodney W. Grimes va_dcl 2012ea022d16SRodney W. Grimes #endif 2013ea022d16SRodney W. Grimes { 2014ea022d16SRodney W. Grimes int i; 2015ea022d16SRodney W. Grimes va_list ap; 2016ea022d16SRodney W. Grimes char *p, *bp, ch; 2017ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2018ea022d16SRodney W. Grimes 2019ea022d16SRodney W. Grimes #if __STDC__ 2020ea022d16SRodney W. Grimes va_start(ap, fmt); 2021ea022d16SRodney W. Grimes #else 2022ea022d16SRodney W. Grimes va_start(ap); 2023ea022d16SRodney W. Grimes #endif 2024ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2025ea022d16SRodney W. Grimes 2026ea022d16SRodney W. Grimes /* make ps print our process name */ 2027ea022d16SRodney W. Grimes p = Argv[0]; 2028ea022d16SRodney W. Grimes *p++ = '-'; 2029ea022d16SRodney W. Grimes 2030ea022d16SRodney W. Grimes i = strlen(buf); 2031ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 2032ea022d16SRodney W. Grimes i = LastArgv - p - 2; 2033ea022d16SRodney W. Grimes buf[i] = '\0'; 2034ea022d16SRodney W. Grimes } 2035ea022d16SRodney W. Grimes bp = buf; 2036ea022d16SRodney W. Grimes while (ch = *bp++) 2037ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 2038ea022d16SRodney W. Grimes *p++ = ch; 2039ea022d16SRodney W. Grimes while (p < LastArgv) 2040ea022d16SRodney W. Grimes *p++ = ' '; 2041ea022d16SRodney W. Grimes } 2042b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 20433eb568f2SGuido van Rooij 204461f891a6SPaul Traina static void 20453eb568f2SGuido van Rooij logxfer(name, size, start) 20463eb568f2SGuido van Rooij char *name; 20473eb568f2SGuido van Rooij long size; 20483eb568f2SGuido van Rooij long start; 20493eb568f2SGuido van Rooij { 20503eb568f2SGuido van Rooij char buf[1024]; 20513eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 20523eb568f2SGuido van Rooij long now; 20533eb568f2SGuido van Rooij 20543eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 20553eb568f2SGuido van Rooij time(&now); 205661f891a6SPaul Traina snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n", 20573eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 20583eb568f2SGuido van Rooij path, name, size, now - start + (now == start)); 20593eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 20603eb568f2SGuido van Rooij } 20613eb568f2SGuido van Rooij } 2062