1ea022d16SRodney W. Grimes /* 2ea022d16SRodney W. Grimes * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 3ea022d16SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4ea022d16SRodney W. Grimes * 5ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions 7ea022d16SRodney W. Grimes * are met: 8ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13ea022d16SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14ea022d16SRodney W. Grimes * must display the following acknowledgement: 15ea022d16SRodney W. Grimes * This product includes software developed by the University of 16ea022d16SRodney W. Grimes * California, Berkeley and its contributors. 17ea022d16SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 19ea022d16SRodney W. Grimes * without specific prior written permission. 20ea022d16SRodney W. Grimes * 21ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ea022d16SRodney W. Grimes * SUCH DAMAGE. 32ea022d16SRodney W. Grimes */ 33ea022d16SRodney W. Grimes 349aca17cbSMark Murray #if 0 35ea022d16SRodney W. Grimes #ifndef lint 36ea022d16SRodney W. Grimes static char copyright[] = 37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 38ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 39ea022d16SRodney W. Grimes #endif /* not lint */ 409aca17cbSMark Murray #endif 41ea022d16SRodney W. Grimes 42ea022d16SRodney W. Grimes #ifndef lint 43e02897faSPhilippe Charnier #if 0 44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 459aca17cbSMark Murray #endif 46e02897faSPhilippe Charnier static const char rcsid[] = 477f3dea24SPeter Wemm "$FreeBSD$"; 48e02897faSPhilippe Charnier #endif /* not lint */ 49ea022d16SRodney W. Grimes 50ea022d16SRodney W. Grimes /* 51ea022d16SRodney W. Grimes * FTP server. 52ea022d16SRodney W. Grimes */ 53ea022d16SRodney W. Grimes #include <sys/param.h> 54ea022d16SRodney W. Grimes #include <sys/ioctl.h> 559fc5823aSGarrett Wollman #include <sys/mman.h> 56eb2fc780SGarrett Wollman #include <sys/socket.h> 57eb2fc780SGarrett Wollman #include <sys/stat.h> 58eb2fc780SGarrett Wollman #include <sys/time.h> 59eb2fc780SGarrett Wollman #include <sys/wait.h> 60ea022d16SRodney W. Grimes 61ea022d16SRodney W. Grimes #include <netinet/in.h> 62ea022d16SRodney W. Grimes #include <netinet/in_systm.h> 63ea022d16SRodney W. Grimes #include <netinet/ip.h> 649fc5823aSGarrett Wollman #include <netinet/tcp.h> 65ea022d16SRodney W. Grimes 66ea022d16SRodney W. Grimes #define FTP_NAMES 67ea022d16SRodney W. Grimes #include <arpa/ftp.h> 68ea022d16SRodney W. Grimes #include <arpa/inet.h> 69ea022d16SRodney W. Grimes #include <arpa/telnet.h> 70ea022d16SRodney W. Grimes 71ea022d16SRodney W. Grimes #include <ctype.h> 72ea022d16SRodney W. Grimes #include <dirent.h> 73ea022d16SRodney W. Grimes #include <err.h> 74ea022d16SRodney W. Grimes #include <errno.h> 75ea022d16SRodney W. Grimes #include <fcntl.h> 76ea022d16SRodney W. Grimes #include <glob.h> 77ea022d16SRodney W. Grimes #include <limits.h> 78ea022d16SRodney W. Grimes #include <netdb.h> 79ea022d16SRodney W. Grimes #include <pwd.h> 8031fea7b8SDavid Nugent #include <grp.h> 81fa1746c9SMark Murray #ifdef USE_PAM 82fa1746c9SMark Murray #include <opie.h> /* XXX */ 83fa1746c9SMark Murray #endif 84ea022d16SRodney W. Grimes #include <setjmp.h> 85ea022d16SRodney W. Grimes #include <signal.h> 86ea022d16SRodney W. Grimes #include <stdio.h> 87ea022d16SRodney W. Grimes #include <stdlib.h> 88ea022d16SRodney W. Grimes #include <string.h> 89ea022d16SRodney W. Grimes #include <syslog.h> 90ea022d16SRodney W. Grimes #include <time.h> 91ea022d16SRodney W. Grimes #include <unistd.h> 92b63e1fe2SPeter Wemm #include <libutil.h> 93b071c689SDavid Nugent #ifdef LOGIN_CAP 94b071c689SDavid Nugent #include <login_cap.h> 95b071c689SDavid Nugent #endif 96ea022d16SRodney W. Grimes 975bc9d93dSMark Murray #ifdef USE_PAM 986c9134c0SMark Murray #include <security/pam_appl.h> 996c9134c0SMark Murray #endif 1006c9134c0SMark Murray 101ea022d16SRodney W. Grimes #include "pathnames.h" 102ea022d16SRodney W. Grimes #include "extern.h" 103ea022d16SRodney W. Grimes 104ea022d16SRodney W. Grimes #if __STDC__ 105ea022d16SRodney W. Grimes #include <stdarg.h> 106ea022d16SRodney W. Grimes #else 107ea022d16SRodney W. Grimes #include <varargs.h> 108ea022d16SRodney W. Grimes #endif 109ea022d16SRodney W. Grimes 110af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 111af85d782SDavid Nugent #undef main 112ea022d16SRodney W. Grimes 1134dd8b5abSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */ 1144dd8b5abSYoshinobu Inoue #ifndef NI_WITHSCOPEID 1154dd8b5abSYoshinobu Inoue #define NI_WITHSCOPEID 0 1164dd8b5abSYoshinobu Inoue #endif 1174dd8b5abSYoshinobu Inoue 118ea022d16SRodney W. Grimes extern off_t restart_point; 119ea022d16SRodney W. Grimes extern char cbuf[]; 120ea022d16SRodney W. Grimes 1214dd8b5abSYoshinobu Inoue union sockunion server_addr; 1224dd8b5abSYoshinobu Inoue union sockunion ctrl_addr; 1234dd8b5abSYoshinobu Inoue union sockunion data_source; 1244dd8b5abSYoshinobu Inoue union sockunion data_dest; 1254dd8b5abSYoshinobu Inoue union sockunion his_addr; 1264dd8b5abSYoshinobu Inoue union sockunion pasv_addr; 127ea022d16SRodney W. Grimes 128cf09a206SDavid Greenman int daemon_mode; 129ea022d16SRodney W. Grimes int data; 130ea022d16SRodney W. Grimes jmp_buf errcatch, urgcatch; 131ea022d16SRodney W. Grimes int logged_in; 132ea022d16SRodney W. Grimes struct passwd *pw; 133618b0bbaSMark Murray int ftpdebug; 134ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 135ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 136ea022d16SRodney W. Grimes int logging; 1374c450ad7SPaul Traina int restricted_data_ports = 1; 138a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1395a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 140ea022d16SRodney W. Grimes int guest; 141a5a4544eSPaul Traina int dochroot; 1423eb568f2SGuido van Rooij int stats; 1433eb568f2SGuido van Rooij int statfd = -1; 144ea022d16SRodney W. Grimes int type; 145ea022d16SRodney W. Grimes int form; 146ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 147ea022d16SRodney W. Grimes int mode; 148ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 149ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 150a4b77a2aSPoul-Henning Kamp int readonly=0; /* Server is in readonly mode. */ 151a4b77a2aSPoul-Henning Kamp int noepsv=0; /* EPSV command is disabled. */ 15262513e76SNik Clayton int noretr=0; /* RETR command is disabled. */ 1531cc9f0bbSSheldon Hearn int noguestretr=0; /* RETR command is disabled for anon users. */ 15462513e76SNik Clayton 155ea022d16SRodney W. Grimes sig_atomic_t transflag; 156ea022d16SRodney W. Grimes off_t file_size; 157ea022d16SRodney W. Grimes off_t byte_count; 158ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 159ea022d16SRodney W. Grimes #undef CMASK 160ea022d16SRodney W. Grimes #define CMASK 027 161ea022d16SRodney W. Grimes #endif 162ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 163ea022d16SRodney W. Grimes char tmpline[7]; 164ea4e54b9SDavid Nugent char *hostname; 165ad442344SDima Dorfman int epsvall = 0; 166ad442344SDima Dorfman 1670512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 168ea4e54b9SDavid Nugent char *ftpuser; 169ea4e54b9SDavid Nugent 170ea4e54b9SDavid Nugent static struct ftphost { 171ea4e54b9SDavid Nugent struct ftphost *next; 172f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 173ea4e54b9SDavid Nugent char *hostname; 174ea4e54b9SDavid Nugent char *anonuser; 175ea4e54b9SDavid Nugent char *statfile; 176ea4e54b9SDavid Nugent char *welcome; 177ea4e54b9SDavid Nugent char *loginmsg; 178ea4e54b9SDavid Nugent } *thishost, *firsthost; 179ea4e54b9SDavid Nugent 180ea4e54b9SDavid Nugent #endif 1819e9a43bdSBrian Somers char remotehost[MAXHOSTNAMELEN]; 1823eb568f2SGuido van Rooij char *ident = NULL; 183a5a4544eSPaul Traina 184a5a4544eSPaul Traina static char ttyline[20]; 185a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 186a5a4544eSPaul Traina 1875bc9d93dSMark Murray #ifdef USE_PAM 1886c9134c0SMark Murray static int auth_pam __P((struct passwd**, const char*)); 1895bc9d93dSMark Murray pam_handle_t *pamh = NULL; 190fa1746c9SMark Murray 191fa1746c9SMark Murray /* Kluge because the conversation mechanism has not been threshed out */ 192fa1746c9SMark Murray static struct opie opiedata; 193fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 1949aca17cbSMark Murray #endif 1959aca17cbSMark Murray 196105a3c98SJulian Elischer char *pid_file = NULL; 197105a3c98SJulian Elischer 198ea022d16SRodney W. Grimes /* 1996d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 2006d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 2016d10cb2fSJonathan Lemon */ 2026d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 2036d10cb2fSJonathan Lemon # 2046d10cb2fSJonathan Lemon 2056d10cb2fSJonathan Lemon /* 206ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 207ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 208ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 209ea022d16SRodney W. Grimes */ 210ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 211ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 212ea022d16SRodney W. Grimes 213ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 214ea022d16SRodney W. Grimes int swaitint = SWAITINT; 215ea022d16SRodney W. Grimes 216ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 217b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 218ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 219ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 220b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 221ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 222ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 223ea022d16SRodney W. Grimes 224ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 225ea022d16SRodney W. Grimes if (logging > 1) \ 226ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 227ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 228ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 229ea022d16SRodney W. Grimes if (logging > 1) \ 230ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 231ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 232ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 233ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 234ea022d16SRodney W. Grimes if (logging > 1) { \ 235ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 236ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 237ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 238ea022d16SRodney W. Grimes else \ 239ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 240ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 241ea022d16SRodney W. Grimes } 242ea022d16SRodney W. Grimes 243ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 244ea4e54b9SDavid Nugent static void inithosts __P((void)); 2454dd8b5abSYoshinobu Inoue static void selecthost __P((union sockunion *)); 246ea4e54b9SDavid Nugent #endif 247ea022d16SRodney W. Grimes static void ack __P((char *)); 248ea022d16SRodney W. Grimes static void myoob __P((int)); 2497edcb936SSteve Price static int checkuser __P((char *, char *, int)); 250ea022d16SRodney W. Grimes static FILE *dataconn __P((char *, off_t, char *)); 2514dd8b5abSYoshinobu Inoue static void dolog __P((struct sockaddr *)); 252ea022d16SRodney W. Grimes static char *curdir __P((void)); 253ea022d16SRodney W. Grimes static void end_login __P((void)); 254ea022d16SRodney W. Grimes static FILE *getdatasock __P((char *)); 255ea022d16SRodney W. Grimes static char *gunique __P((char *)); 256ea022d16SRodney W. Grimes static void lostconn __P((int)); 257ea022d16SRodney W. Grimes static int receive_data __P((FILE *, FILE *)); 2589fc5823aSGarrett Wollman static void send_data __P((FILE *, FILE *, off_t, off_t, int)); 259ea022d16SRodney W. Grimes static struct passwd * 260ea022d16SRodney W. Grimes sgetpwnam __P((char *)); 261ea022d16SRodney W. Grimes static char *sgetsave __P((char *)); 262cf09a206SDavid Greenman static void reapchild __P((int)); 263e4a71114SAndrey A. Chernov static void logxfer __P((char *, off_t, time_t)); 264ea022d16SRodney W. Grimes 265ea022d16SRodney W. Grimes static char * 266ea022d16SRodney W. Grimes curdir() 267ea022d16SRodney W. Grimes { 268ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 269ea022d16SRodney W. Grimes 270ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 271ea022d16SRodney W. Grimes return (""); 272ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 273ea022d16SRodney W. Grimes strcat(path, "/"); 274ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 275ea022d16SRodney W. Grimes return (guest ? path+1 : path); 276ea022d16SRodney W. Grimes } 277ea022d16SRodney W. Grimes 278ea022d16SRodney W. Grimes int 279ea022d16SRodney W. Grimes main(argc, argv, envp) 280ea022d16SRodney W. Grimes int argc; 281ea022d16SRodney W. Grimes char *argv[]; 282ea022d16SRodney W. Grimes char **envp; 283ea022d16SRodney W. Grimes { 284ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 285ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 286ea022d16SRodney W. Grimes FILE *fd; 2874dd8b5abSYoshinobu Inoue int error; 2884dd8b5abSYoshinobu Inoue char *bindname = NULL; 2894dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2904dd8b5abSYoshinobu Inoue int enable_v4 = 0; 291ea022d16SRodney W. Grimes 29234d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 29334d1ba5cSAndrey A. Chernov 294b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 295ea022d16SRodney W. Grimes /* 296ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 297ea022d16SRodney W. Grimes */ 298ea022d16SRodney W. Grimes Argv = argv; 299ea022d16SRodney W. Grimes while (*envp) 300ea022d16SRodney W. Grimes envp++; 301ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 302b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 303ea022d16SRodney W. Grimes 3043eb568f2SGuido van Rooij 3051cc9f0bbSSheldon Hearn while ((ch = getopt(argc, argv, "AdlDESURrt:T:u:vOoa:p:46")) != -1) { 306ea022d16SRodney W. Grimes switch (ch) { 307cf09a206SDavid Greenman case 'D': 308cf09a206SDavid Greenman daemon_mode++; 309cf09a206SDavid Greenman break; 310cf09a206SDavid Greenman 311ea022d16SRodney W. Grimes case 'd': 312618b0bbaSMark Murray ftpdebug++; 313ea022d16SRodney W. Grimes break; 314ea022d16SRodney W. Grimes 315a4b77a2aSPoul-Henning Kamp case 'E': 316a4b77a2aSPoul-Henning Kamp noepsv = 1; 317a4b77a2aSPoul-Henning Kamp break; 318a4b77a2aSPoul-Henning Kamp 319ea022d16SRodney W. Grimes case 'l': 320ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 321ea022d16SRodney W. Grimes break; 322ea022d16SRodney W. Grimes 323a4b77a2aSPoul-Henning Kamp case 'r': 324a4b77a2aSPoul-Henning Kamp readonly = 1; 325a4b77a2aSPoul-Henning Kamp break; 326a4b77a2aSPoul-Henning Kamp 327a5a4544eSPaul Traina case 'R': 328a5a4544eSPaul Traina paranoid = 0; 329a5a4544eSPaul Traina break; 330a5a4544eSPaul Traina 331a5a4544eSPaul Traina case 'S': 332a5a4544eSPaul Traina stats++; 333a5a4544eSPaul Traina break; 334a5a4544eSPaul Traina 335a5a4544eSPaul Traina case 'T': 336a5a4544eSPaul Traina maxtimeout = atoi(optarg); 337a5a4544eSPaul Traina if (timeout > maxtimeout) 338a5a4544eSPaul Traina timeout = maxtimeout; 3394c450ad7SPaul Traina break; 3404c450ad7SPaul Traina 341ea022d16SRodney W. Grimes case 't': 342ea022d16SRodney W. Grimes timeout = atoi(optarg); 343ea022d16SRodney W. Grimes if (maxtimeout < timeout) 344ea022d16SRodney W. Grimes maxtimeout = timeout; 345ea022d16SRodney W. Grimes break; 346a5a4544eSPaul Traina 347a5a4544eSPaul Traina case 'U': 348a5a4544eSPaul Traina restricted_data_ports = 0; 349ea022d16SRodney W. Grimes break; 350ea022d16SRodney W. Grimes 351105a3c98SJulian Elischer case 'a': 3524dd8b5abSYoshinobu Inoue bindname = optarg; 353105a3c98SJulian Elischer break; 354105a3c98SJulian Elischer 355105a3c98SJulian Elischer case 'p': 356105a3c98SJulian Elischer pid_file = optarg; 357105a3c98SJulian Elischer break; 358105a3c98SJulian Elischer 359ea022d16SRodney W. Grimes case 'u': 360ea022d16SRodney W. Grimes { 361ea022d16SRodney W. Grimes long val = 0; 362ea022d16SRodney W. Grimes 363ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 364ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 365ea022d16SRodney W. Grimes warnx("bad value for -u"); 366ea022d16SRodney W. Grimes else 367ea022d16SRodney W. Grimes defumask = val; 368ea022d16SRodney W. Grimes break; 369ea022d16SRodney W. Grimes } 3705a392aecSTorsten Blum case 'A': 3715a392aecSTorsten Blum anon_only = 1; 3725a392aecSTorsten Blum break; 373ea022d16SRodney W. Grimes 374ea022d16SRodney W. Grimes case 'v': 375618b0bbaSMark Murray ftpdebug = 1; 376ea022d16SRodney W. Grimes break; 377ea022d16SRodney W. Grimes 3784dd8b5abSYoshinobu Inoue case '4': 3794dd8b5abSYoshinobu Inoue enable_v4 = 1; 3804dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) 3814dd8b5abSYoshinobu Inoue family = AF_INET; 3824dd8b5abSYoshinobu Inoue break; 3834dd8b5abSYoshinobu Inoue 3844dd8b5abSYoshinobu Inoue case '6': 3854dd8b5abSYoshinobu Inoue family = AF_INET6; 3864dd8b5abSYoshinobu Inoue break; 3874dd8b5abSYoshinobu Inoue 3881cc9f0bbSSheldon Hearn case 'O': 3891cc9f0bbSSheldon Hearn noguestretr = 1; 3901cc9f0bbSSheldon Hearn break; 3911cc9f0bbSSheldon Hearn 39262513e76SNik Clayton case 'o': 39362513e76SNik Clayton noretr = 1; 39462513e76SNik Clayton break; 39562513e76SNik Clayton 396ea022d16SRodney W. Grimes default: 397ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 398ea022d16SRodney W. Grimes break; 399ea022d16SRodney W. Grimes } 400ea022d16SRodney W. Grimes } 401cf09a206SDavid Greenman 402ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 403ea4e54b9SDavid Nugent inithosts(); 404ea4e54b9SDavid Nugent #endif 405ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 406cf09a206SDavid Greenman 407cf09a206SDavid Greenman /* 408cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 409cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 410cf09a206SDavid Greenman */ 411cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 412cf09a206SDavid Greenman 413cf09a206SDavid Greenman if (daemon_mode) { 414cf09a206SDavid Greenman int ctl_sock, fd; 4154dd8b5abSYoshinobu Inoue struct addrinfo hints, *res; 416cf09a206SDavid Greenman 417cf09a206SDavid Greenman /* 418cf09a206SDavid Greenman * Detach from parent. 419cf09a206SDavid Greenman */ 420cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 421cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 422cf09a206SDavid Greenman exit(1); 423cf09a206SDavid Greenman } 424cf09a206SDavid Greenman (void) signal(SIGCHLD, reapchild); 4254dd8b5abSYoshinobu Inoue /* init bind_sa */ 4264dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 4274dd8b5abSYoshinobu Inoue 4284dd8b5abSYoshinobu Inoue hints.ai_family = family == AF_UNSPEC ? AF_INET : family; 4294dd8b5abSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 4304dd8b5abSYoshinobu Inoue hints.ai_protocol = 0; 4314dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 4324dd8b5abSYoshinobu Inoue error = getaddrinfo(bindname, "ftp", &hints, &res); 4334dd8b5abSYoshinobu Inoue if (error) { 4344dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) { 4354dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 4364dd8b5abSYoshinobu Inoue error = getaddrinfo(bindname, "ftp", &hints, 4374dd8b5abSYoshinobu Inoue &res); 4384dd8b5abSYoshinobu Inoue } 4394dd8b5abSYoshinobu Inoue } 4404dd8b5abSYoshinobu Inoue if (error) { 4413fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", gai_strerror(error)); 4424dd8b5abSYoshinobu Inoue if (error == EAI_SYSTEM) 4433fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", strerror(errno)); 4444dd8b5abSYoshinobu Inoue exit(1); 4454dd8b5abSYoshinobu Inoue } 4464dd8b5abSYoshinobu Inoue if (res->ai_addr == NULL) { 4474dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 448cf09a206SDavid Greenman exit(1); 4492310b8c6SRuslan Ermilov } else 4502310b8c6SRuslan Ermilov family = res->ai_addr->sa_family; 451cf09a206SDavid Greenman /* 452cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 453cf09a206SDavid Greenman * listening. 454cf09a206SDavid Greenman */ 4554dd8b5abSYoshinobu Inoue ctl_sock = socket(family, SOCK_STREAM, 0); 456cf09a206SDavid Greenman if (ctl_sock < 0) { 457cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 458cf09a206SDavid Greenman exit(1); 459cf09a206SDavid Greenman } 460cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 461cf09a206SDavid Greenman (char *)&on, sizeof(on)) < 0) 4624dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "control setsockopt: %m"); 4634dd8b5abSYoshinobu Inoue #ifdef IPV6_BINDV6ONLY 4644dd8b5abSYoshinobu Inoue if (family == AF_INET6 && enable_v4 == 0) { 4654dd8b5abSYoshinobu Inoue if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_BINDV6ONLY, 4664dd8b5abSYoshinobu Inoue (char *)&on, sizeof (on)) < 0) 4674dd8b5abSYoshinobu Inoue syslog(LOG_ERR, 4684dd8b5abSYoshinobu Inoue "control setsockopt(IPV6_BINDV6ONLY): %m"); 4694dd8b5abSYoshinobu Inoue } 4704dd8b5abSYoshinobu Inoue #endif /* IPV6_BINDV6ONLY */ 4714dd8b5abSYoshinobu Inoue memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len); 4724dd8b5abSYoshinobu Inoue if (bind(ctl_sock, (struct sockaddr *)&server_addr, 4734dd8b5abSYoshinobu Inoue server_addr.su_len) < 0) { 474cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 475cf09a206SDavid Greenman exit(1); 476cf09a206SDavid Greenman } 477cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 478cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 479cf09a206SDavid Greenman exit(1); 480cf09a206SDavid Greenman } 481cf09a206SDavid Greenman /* 482105a3c98SJulian Elischer * Atomically write process ID 483105a3c98SJulian Elischer */ 484105a3c98SJulian Elischer if (pid_file) 485105a3c98SJulian Elischer { 486105a3c98SJulian Elischer int fd; 487105a3c98SJulian Elischer char buf[20]; 488105a3c98SJulian Elischer 489105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 490105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 49185966371SWarner Losh if (fd < 0) { 492105a3c98SJulian Elischer if (errno == EAGAIN) 493105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 494105a3c98SJulian Elischer else 495105a3c98SJulian Elischer err(1, "%s", pid_file); 49685966371SWarner Losh } 497105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 498105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 499105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 500105a3c98SJulian Elischer err(1, "%s: write", pid_file); 501105a3c98SJulian Elischer /* Leave the pid file open and locked */ 502105a3c98SJulian Elischer } 503105a3c98SJulian Elischer /* 504cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 505cf09a206SDavid Greenman * children to handle them. 506cf09a206SDavid Greenman */ 507cf09a206SDavid Greenman while (1) { 5084dd8b5abSYoshinobu Inoue addrlen = server_addr.su_len; 509cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 510cf09a206SDavid Greenman if (fork() == 0) { 511cf09a206SDavid Greenman /* child */ 512cf09a206SDavid Greenman (void) dup2(fd, 0); 513cf09a206SDavid Greenman (void) dup2(fd, 1); 514cf09a206SDavid Greenman close(ctl_sock); 515cf09a206SDavid Greenman break; 516cf09a206SDavid Greenman } 517cf09a206SDavid Greenman close(fd); 518cf09a206SDavid Greenman } 519cf09a206SDavid Greenman } else { 520cf09a206SDavid Greenman addrlen = sizeof(his_addr); 521cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 522cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 523cf09a206SDavid Greenman exit(1); 524cf09a206SDavid Greenman } 525cf09a206SDavid Greenman } 526cf09a206SDavid Greenman 527ea022d16SRodney W. Grimes (void) signal(SIGCHLD, SIG_IGN); 528cf09a206SDavid Greenman (void) signal(SIGPIPE, lostconn); 529158a00b2SJohn Birrell if (signal(SIGURG, myoob) == SIG_ERR) 530ea022d16SRodney W. Grimes syslog(LOG_ERR, "signal: %m"); 531ea022d16SRodney W. Grimes 532cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 533cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 534cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 535cf09a206SDavid Greenman exit(1); 536cf09a206SDavid Greenman } 537ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 538ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5394dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 540ea4e54b9SDavid Nugent #endif 541cf09a206SDavid Greenman #ifdef IP_TOS 5424dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5434dd8b5abSYoshinobu Inoue { 544cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 545cf09a206SDavid Greenman if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 546cf09a206SDavid Greenman syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 5474dd8b5abSYoshinobu Inoue } 548cf09a206SDavid Greenman #endif 549dadb9fb3SDavid Greenman /* 550dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 551dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 552dadb9fb3SDavid Greenman */ 553dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 554dadb9fb3SDavid Greenman syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m"); 555dadb9fb3SDavid Greenman 5564dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 557cf09a206SDavid Greenman 558a5a4544eSPaul Traina /* set this here so klogin can use it... */ 559e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 560a5a4544eSPaul Traina 561ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 562ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 563ea022d16SRodney W. Grimes if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 564ea022d16SRodney W. Grimes syslog(LOG_ERR, "setsockopt: %m"); 565ea022d16SRodney W. Grimes #endif 566ea022d16SRodney W. Grimes 567ea022d16SRodney W. Grimes #ifdef F_SETOWN 568ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 569ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 570ea022d16SRodney W. Grimes #endif 5714dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 572ea022d16SRodney W. Grimes /* 573ea022d16SRodney W. Grimes * Set up default state 574ea022d16SRodney W. Grimes */ 575ea022d16SRodney W. Grimes data = -1; 576ea022d16SRodney W. Grimes type = TYPE_A; 577ea022d16SRodney W. Grimes form = FORM_N; 578ea022d16SRodney W. Grimes stru = STRU_F; 579ea022d16SRodney W. Grimes mode = MODE_S; 580ea022d16SRodney W. Grimes tmpline[0] = '\0'; 581ea022d16SRodney W. Grimes 582ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 583ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 584ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 585ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 586ea022d16SRodney W. Grimes *cp = '\0'; 587ea022d16SRodney W. Grimes lreply(530, "%s", line); 588ea022d16SRodney W. Grimes } 589ea022d16SRodney W. Grimes (void) fflush(stdout); 590ea022d16SRodney W. Grimes (void) fclose(fd); 591ea022d16SRodney W. Grimes reply(530, "System not available."); 592ea022d16SRodney W. Grimes exit(0); 593ea022d16SRodney W. Grimes } 594ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 595ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 596ea4e54b9SDavid Nugent #else 597ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 598ea4e54b9SDavid Nugent #endif 599ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 600ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 601ea022d16SRodney W. Grimes *cp = '\0'; 602ea022d16SRodney W. Grimes lreply(220, "%s", line); 603ea022d16SRodney W. Grimes } 604ea022d16SRodney W. Grimes (void) fflush(stdout); 605ea022d16SRodney W. Grimes (void) fclose(fd); 606ea022d16SRodney W. Grimes /* reply(220,) must follow */ 607ea022d16SRodney W. Grimes } 608ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6090512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 610618b0bbaSMark Murray fatalerror("Ran out of memory."); 6119e9a43bdSBrian Somers (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 6129e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 613ea4e54b9SDavid Nugent #endif 614ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 615ea022d16SRodney W. Grimes (void) setjmp(errcatch); 616ea022d16SRodney W. Grimes for (;;) 617ea022d16SRodney W. Grimes (void) yyparse(); 618ea022d16SRodney W. Grimes /* NOTREACHED */ 619ea022d16SRodney W. Grimes } 620ea022d16SRodney W. Grimes 621ea022d16SRodney W. Grimes static void 622ea022d16SRodney W. Grimes lostconn(signo) 623ea022d16SRodney W. Grimes int signo; 624ea022d16SRodney W. Grimes { 625ea022d16SRodney W. Grimes 626618b0bbaSMark Murray if (ftpdebug) 627ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 628e02897faSPhilippe Charnier dologout(1); 629ea022d16SRodney W. Grimes } 630ea022d16SRodney W. Grimes 631ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 632ea4e54b9SDavid Nugent /* 633ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 634ea4e54b9SDavid Nugent */ 635ea4e54b9SDavid Nugent 636ea4e54b9SDavid Nugent static void 637ea4e54b9SDavid Nugent inithosts() 638ea4e54b9SDavid Nugent { 639ea4e54b9SDavid Nugent FILE *fp; 640ea4e54b9SDavid Nugent char *cp; 641ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 642ea4e54b9SDavid Nugent char line[1024]; 6434dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 644ea4e54b9SDavid Nugent 645ea4e54b9SDavid Nugent /* 646ea4e54b9SDavid Nugent * Fill in the default host information 647ea4e54b9SDavid Nugent */ 648ea4e54b9SDavid Nugent if (gethostname(line, sizeof(line)) < 0) 649ea4e54b9SDavid Nugent line[0] = '\0'; 650ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL || 651ea4e54b9SDavid Nugent (hrp->hostname = strdup(line)) == NULL) 652618b0bbaSMark Murray fatalerror("Ran out of memory."); 653f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 6544dd8b5abSYoshinobu Inoue 6554dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 6564dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 6574dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 6584dd8b5abSYoshinobu Inoue getaddrinfo(hrp->hostname, NULL, &hints, &res); 6594dd8b5abSYoshinobu Inoue if (res) 660f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 661ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 662ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 663ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 664ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 665ea4e54b9SDavid Nugent hrp->next = NULL; 666ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 667ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 668b535a9bfSDavid Nugent int addrsize, error, gothost; 6694dd8b5abSYoshinobu Inoue void *addr; 6704dd8b5abSYoshinobu Inoue struct hostent *hp; 6714dd8b5abSYoshinobu Inoue 672ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL) { 6734dd8b5abSYoshinobu Inoue int i, hp_error; 674ea4e54b9SDavid Nugent 675ea4e54b9SDavid Nugent if ((cp = strchr(line, '\n')) == NULL) { 676ea4e54b9SDavid Nugent /* ignore long lines */ 677ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL && 678ea4e54b9SDavid Nugent strchr(line, '\n') == NULL) 679ea4e54b9SDavid Nugent ; 680ea4e54b9SDavid Nugent continue; 681ea4e54b9SDavid Nugent } 682ea4e54b9SDavid Nugent *cp = '\0'; 683ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 684ea4e54b9SDavid Nugent /* skip comments and empty lines */ 685ea4e54b9SDavid Nugent if (cp == NULL || line[0] == '#') 686ea4e54b9SDavid Nugent continue; 6874dd8b5abSYoshinobu Inoue 6884dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 6894dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 6904dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 6914dd8b5abSYoshinobu Inoue error = getaddrinfo(cp, NULL, &hints, &res); 6924dd8b5abSYoshinobu Inoue if (error != NULL) 693ea4e54b9SDavid Nugent continue; 6944dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 695b535a9bfSDavid Nugent ai = ai->ai_next) { 6964dd8b5abSYoshinobu Inoue 697b535a9bfSDavid Nugent gothost = 0; 698ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 699f38c6cadSYoshinobu Inoue struct addrinfo *hi; 700f38c6cadSYoshinobu Inoue 701f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 702f38c6cadSYoshinobu Inoue hi = hi->ai_next) 703f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 704f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7054dd8b5abSYoshinobu Inoue ai->ai_addr, 706b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 707b535a9bfSDavid Nugent gothost++; 708b535a9bfSDavid Nugent break; 709b535a9bfSDavid Nugent } 710b535a9bfSDavid Nugent if (gothost) 711ea4e54b9SDavid Nugent break; 712ea4e54b9SDavid Nugent } 713ea4e54b9SDavid Nugent if (hrp == NULL) { 714ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 715ea4e54b9SDavid Nugent continue; 716ea4e54b9SDavid Nugent /* defaults */ 717ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 718ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 719ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 720ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 721ea4e54b9SDavid Nugent hrp->next = NULL; 722ea4e54b9SDavid Nugent lhrp->next = hrp; 723ea4e54b9SDavid Nugent lhrp = hrp; 724ea4e54b9SDavid Nugent } 725f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 726f38c6cadSYoshinobu Inoue 727ea4e54b9SDavid Nugent /* 728ea4e54b9SDavid Nugent * determine hostname to use. 7294dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 730ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 731ea4e54b9SDavid Nugent */ 7324dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 733f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 7344dd8b5abSYoshinobu Inoue case AF_INET: 735f38c6cadSYoshinobu Inoue addr = &((struct sockaddr_in *)&hrp->hostinfo->ai_addr)->sin_addr; 7364dd8b5abSYoshinobu Inoue addrsize = sizeof(struct sockaddr_in); 7374dd8b5abSYoshinobu Inoue break; 7384dd8b5abSYoshinobu Inoue case AF_INET6: 739f38c6cadSYoshinobu Inoue addr = &((struct sockaddr_in6 *)&hrp->hostinfo->ai_addr)->sin6_addr; 7404dd8b5abSYoshinobu Inoue addrsize = sizeof(struct sockaddr_in6); 7414dd8b5abSYoshinobu Inoue break; 7424dd8b5abSYoshinobu Inoue default: 7434dd8b5abSYoshinobu Inoue /* should not reach here */ 744f38c6cadSYoshinobu Inoue if (hrp->hostinfo != NULL) 745f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 7464dd8b5abSYoshinobu Inoue free(hrp); 7474dd8b5abSYoshinobu Inoue continue; 7484dd8b5abSYoshinobu Inoue /* NOTREACHED */ 7494dd8b5abSYoshinobu Inoue } 7504dd8b5abSYoshinobu Inoue if ((hp = getipnodebyaddr((char*)addr, addrsize, 751f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 7524dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 753ea4e54b9SDavid Nugent if (strcmp(cp, hp->h_name) != 0) { 754ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 755ea4e54b9SDavid Nugent cp = hp->h_name; 756ea4e54b9SDavid Nugent else { 757ea4e54b9SDavid Nugent i = 0; 758ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 759ea4e54b9SDavid Nugent strcmp(cp, hp->h_aliases[i]) != 0) 760ea4e54b9SDavid Nugent ++i; 761ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 762ea4e54b9SDavid Nugent cp = hp->h_name; 763ea4e54b9SDavid Nugent } 764ea4e54b9SDavid Nugent } 765ea4e54b9SDavid Nugent } 766ea4e54b9SDavid Nugent hrp->hostname = strdup(cp); 7674dd8b5abSYoshinobu Inoue freehostent(hp); 768ea4e54b9SDavid Nugent /* ok, now we now peel off the rest */ 769ea4e54b9SDavid Nugent i = 0; 770ea4e54b9SDavid Nugent while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) { 771ea4e54b9SDavid Nugent if (*cp != '-' && (cp = strdup(cp)) != NULL) { 772ea4e54b9SDavid Nugent switch (i) { 773ea4e54b9SDavid Nugent case 0: /* anon user permissions */ 774ea4e54b9SDavid Nugent hrp->anonuser = cp; 775ea4e54b9SDavid Nugent break; 776ea4e54b9SDavid Nugent case 1: /* statistics file */ 777ea4e54b9SDavid Nugent hrp->statfile = cp; 778ea4e54b9SDavid Nugent break; 779ea4e54b9SDavid Nugent case 2: /* welcome message */ 780ea4e54b9SDavid Nugent hrp->welcome = cp; 781ea4e54b9SDavid Nugent break; 782ea4e54b9SDavid Nugent case 3: /* login message */ 783ea4e54b9SDavid Nugent hrp->loginmsg = cp; 784ea4e54b9SDavid Nugent break; 785ea4e54b9SDavid Nugent } 786ea4e54b9SDavid Nugent } 787ea4e54b9SDavid Nugent ++i; 788ea4e54b9SDavid Nugent } 7894dd8b5abSYoshinobu Inoue /* XXX: re-initialization for getaddrinfo() loop */ 7904dd8b5abSYoshinobu Inoue cp = strtok(line, " \t"); 7914dd8b5abSYoshinobu Inoue } 792ea4e54b9SDavid Nugent } 793ea4e54b9SDavid Nugent (void) fclose(fp); 794ea4e54b9SDavid Nugent } 795ea4e54b9SDavid Nugent } 796ea4e54b9SDavid Nugent 797ea4e54b9SDavid Nugent static void 7984dd8b5abSYoshinobu Inoue selecthost(su) 7994dd8b5abSYoshinobu Inoue union sockunion *su; 800ea4e54b9SDavid Nugent { 801ea4e54b9SDavid Nugent struct ftphost *hrp; 8024dd8b5abSYoshinobu Inoue u_int16_t port; 8034dd8b5abSYoshinobu Inoue #ifdef INET6 8044dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 8054dd8b5abSYoshinobu Inoue #endif 806f38c6cadSYoshinobu Inoue struct addrinfo *hi; 8074dd8b5abSYoshinobu Inoue 8084dd8b5abSYoshinobu Inoue #ifdef INET6 8094dd8b5abSYoshinobu Inoue /* 8104dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 8114dd8b5abSYoshinobu Inoue * specified in rfc2373. 8124dd8b5abSYoshinobu Inoue */ 8134dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 8144dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 8154dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 8164dd8b5abSYoshinobu Inoue #endif 817ea4e54b9SDavid Nugent 818ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 8194dd8b5abSYoshinobu Inoue port = su->su_port; 8204dd8b5abSYoshinobu Inoue su->su_port = 0; 821ea4e54b9SDavid Nugent while (hrp != NULL) { 822b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 823f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 824ea4e54b9SDavid Nugent thishost = hrp; 825ea4e54b9SDavid Nugent break; 826ea4e54b9SDavid Nugent } 8274dd8b5abSYoshinobu Inoue #ifdef INET6 8284dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 829f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 8304dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 831f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 8324dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 8334dd8b5abSYoshinobu Inoue thishost = hrp; 8344dd8b5abSYoshinobu Inoue break; 8354dd8b5abSYoshinobu Inoue } 8364dd8b5abSYoshinobu Inoue #endif 837f38c6cadSYoshinobu Inoue } 838ea4e54b9SDavid Nugent hrp = hrp->next; 839ea4e54b9SDavid Nugent } 8404dd8b5abSYoshinobu Inoue su->su_port = port; 841ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 842ea4e54b9SDavid Nugent hostname = thishost->hostname; 843ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 844ea4e54b9SDavid Nugent } 845ea4e54b9SDavid Nugent #endif 846ea4e54b9SDavid Nugent 847ea022d16SRodney W. Grimes /* 848ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 849ea022d16SRodney W. Grimes */ 850ea022d16SRodney W. Grimes static char * 851ea022d16SRodney W. Grimes sgetsave(s) 852ea022d16SRodney W. Grimes char *s; 853ea022d16SRodney W. Grimes { 854ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 855ea022d16SRodney W. Grimes 856ea022d16SRodney W. Grimes if (new == NULL) { 857ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 858ea022d16SRodney W. Grimes dologout(1); 859ea022d16SRodney W. Grimes /* NOTREACHED */ 860ea022d16SRodney W. Grimes } 861ea022d16SRodney W. Grimes (void) strcpy(new, s); 862ea022d16SRodney W. Grimes return (new); 863ea022d16SRodney W. Grimes } 864ea022d16SRodney W. Grimes 865ea022d16SRodney W. Grimes /* 866ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 867ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 868ea022d16SRodney W. Grimes * (e.g., globbing). 869ea022d16SRodney W. Grimes */ 870ea022d16SRodney W. Grimes static struct passwd * 871ea022d16SRodney W. Grimes sgetpwnam(name) 872ea022d16SRodney W. Grimes char *name; 873ea022d16SRodney W. Grimes { 874ea022d16SRodney W. Grimes static struct passwd save; 875ea022d16SRodney W. Grimes struct passwd *p; 876ea022d16SRodney W. Grimes 877ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 878ea022d16SRodney W. Grimes return (p); 879ea022d16SRodney W. Grimes if (save.pw_name) { 880ea022d16SRodney W. Grimes free(save.pw_name); 881ea022d16SRodney W. Grimes free(save.pw_passwd); 882ea022d16SRodney W. Grimes free(save.pw_gecos); 883ea022d16SRodney W. Grimes free(save.pw_dir); 884ea022d16SRodney W. Grimes free(save.pw_shell); 885ea022d16SRodney W. Grimes } 886ea022d16SRodney W. Grimes save = *p; 887ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 888ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 889ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 890ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 891ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 892ea022d16SRodney W. Grimes return (&save); 893ea022d16SRodney W. Grimes } 894ea022d16SRodney W. Grimes 895ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 896ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 89790906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 898ea022d16SRodney W. Grimes 899ea022d16SRodney W. Grimes /* 900ea022d16SRodney W. Grimes * USER command. 901ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 902ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 903ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 904ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 905ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 906ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 907ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 908ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 909ea022d16SRodney W. Grimes */ 910ea022d16SRodney W. Grimes void 911ea022d16SRodney W. Grimes user(name) 912ea022d16SRodney W. Grimes char *name; 913ea022d16SRodney W. Grimes { 914ea022d16SRodney W. Grimes char *cp, *shell; 915ea022d16SRodney W. Grimes 916ea022d16SRodney W. Grimes if (logged_in) { 917ea022d16SRodney W. Grimes if (guest) { 918ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 919ea022d16SRodney W. Grimes return; 920a5a4544eSPaul Traina } else if (dochroot) { 921a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 922a5a4544eSPaul Traina return; 923ea022d16SRodney W. Grimes } 924ea022d16SRodney W. Grimes end_login(); 925ea022d16SRodney W. Grimes } 926ea022d16SRodney W. Grimes 927ea022d16SRodney W. Grimes guest = 0; 928ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 9297edcb936SSteve Price if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 9307edcb936SSteve Price checkuser(_PATH_FTPUSERS, "anonymous", 0)) 931ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 932ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 933ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 934ea4e54b9SDavid Nugent #else 935ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 936ea4e54b9SDavid Nugent #endif 937ea022d16SRodney W. Grimes guest = 1; 938ea022d16SRodney W. Grimes askpasswd = 1; 939ea022d16SRodney W. Grimes reply(331, 9402c60c54cSPaul Traina "Guest login ok, send your email address as password."); 941ea022d16SRodney W. Grimes } else 942ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 943ea022d16SRodney W. Grimes if (!askpasswd && logging) 944ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 945ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 946ea022d16SRodney W. Grimes return; 947ea022d16SRodney W. Grimes } 9485a392aecSTorsten Blum if (anon_only != 0) { 9495a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 9505a392aecSTorsten Blum return; 9515a392aecSTorsten Blum } 9525a392aecSTorsten Blum 9539aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 954ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 955ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 956ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 957ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 958ea022d16SRodney W. Grimes break; 959ea022d16SRodney W. Grimes endusershell(); 960ea022d16SRodney W. Grimes 9617edcb936SSteve Price if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 962ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 963ea022d16SRodney W. Grimes if (logging) 964ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 965ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 966ea022d16SRodney W. Grimes remotehost, name); 967ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 968ea022d16SRodney W. Grimes return; 969ea022d16SRodney W. Grimes } 970ea022d16SRodney W. Grimes } 971ea022d16SRodney W. Grimes if (logging) 972ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 973fa1746c9SMark Murray #ifdef USE_PAM 974fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 975fa1746c9SMark Murray opiechallenge(&opiedata, name, opieprompt); 976fa1746c9SMark Murray reply(331, "[ %s ] Password required for %s.", opieprompt, name); 977726040deSGuido van Rooij #else 978ea022d16SRodney W. Grimes reply(331, "Password required for %s.", name); 979726040deSGuido van Rooij #endif 980ea022d16SRodney W. Grimes askpasswd = 1; 981ea022d16SRodney W. Grimes /* 982ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 983ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 984ea022d16SRodney W. Grimes */ 985ea022d16SRodney W. Grimes if (login_attempts) 986ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 987ea022d16SRodney W. Grimes } 988ea022d16SRodney W. Grimes 989ea022d16SRodney W. Grimes /* 990a5a4544eSPaul Traina * Check if a user is in the file "fname" 991ea022d16SRodney W. Grimes */ 992ea022d16SRodney W. Grimes static int 9937edcb936SSteve Price checkuser(fname, name, pwset) 994a5a4544eSPaul Traina char *fname; 995ea022d16SRodney W. Grimes char *name; 9967edcb936SSteve Price int pwset; 997ea022d16SRodney W. Grimes { 998ea022d16SRodney W. Grimes FILE *fd; 999ea022d16SRodney W. Grimes int found = 0; 1000ea022d16SRodney W. Grimes char *p, line[BUFSIZ]; 1001ea022d16SRodney W. Grimes 1002a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 100331fea7b8SDavid Nugent while (!found && fgets(line, sizeof(line), fd) != NULL) 1004ea022d16SRodney W. Grimes if ((p = strchr(line, '\n')) != NULL) { 1005ea022d16SRodney W. Grimes *p = '\0'; 1006ea022d16SRodney W. Grimes if (line[0] == '#') 1007ea022d16SRodney W. Grimes continue; 100831fea7b8SDavid Nugent /* 100931fea7b8SDavid Nugent * if first chr is '@', check group membership 101031fea7b8SDavid Nugent */ 101131fea7b8SDavid Nugent if (line[0] == '@') { 101231fea7b8SDavid Nugent int i = 0; 101331fea7b8SDavid Nugent struct group *grp; 101431fea7b8SDavid Nugent 101531fea7b8SDavid Nugent if ((grp = getgrnam(line+1)) == NULL) 101631fea7b8SDavid Nugent continue; 10177edcb936SSteve Price /* 10187edcb936SSteve Price * Check user's default group 10197edcb936SSteve Price */ 10207edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 10217edcb936SSteve Price found = 1; 10227edcb936SSteve Price /* 10237edcb936SSteve Price * Check supplementary groups 10247edcb936SSteve Price */ 102531fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 102631fea7b8SDavid Nugent found = strcmp(name, 102731fea7b8SDavid Nugent grp->gr_mem[i++]) 102831fea7b8SDavid Nugent == 0; 1029ea022d16SRodney W. Grimes } 103031fea7b8SDavid Nugent /* 103131fea7b8SDavid Nugent * Otherwise, just check for username match 103231fea7b8SDavid Nugent */ 103331fea7b8SDavid Nugent else 103431fea7b8SDavid Nugent found = strcmp(line, name) == 0; 1035ea022d16SRodney W. Grimes } 1036ea022d16SRodney W. Grimes (void) fclose(fd); 1037ea022d16SRodney W. Grimes } 1038ea022d16SRodney W. Grimes return (found); 1039ea022d16SRodney W. Grimes } 1040ea022d16SRodney W. Grimes 1041ea022d16SRodney W. Grimes /* 1042ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1043ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1044ea022d16SRodney W. Grimes */ 1045ea022d16SRodney W. Grimes static void 1046ea022d16SRodney W. Grimes end_login() 1047ea022d16SRodney W. Grimes { 10485bc9d93dSMark Murray #ifdef USE_PAM 10495bc9d93dSMark Murray int e; 10505bc9d93dSMark Murray #endif 1051ea022d16SRodney W. Grimes 1052ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1053ea022d16SRodney W. Grimes if (logged_in) 1054986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 1055ea022d16SRodney W. Grimes pw = NULL; 1056b071c689SDavid Nugent #ifdef LOGIN_CAP 1057b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 1058b071c689SDavid Nugent LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1059b071c689SDavid Nugent #endif 10605bc9d93dSMark Murray #ifdef USE_PAM 10615bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 10625bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 10635bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 10645bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 10655bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 10665bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 10675bc9d93dSMark Murray pamh = NULL; 10685bc9d93dSMark Murray #endif 1069ea022d16SRodney W. Grimes logged_in = 0; 1070ea022d16SRodney W. Grimes guest = 0; 1071a5a4544eSPaul Traina dochroot = 0; 1072ea022d16SRodney W. Grimes } 1073ea022d16SRodney W. Grimes 10745bc9d93dSMark Murray #ifdef USE_PAM 10756c9134c0SMark Murray 10766c9134c0SMark Murray /* 10776c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 10786c9134c0SMark Murray * login.c 10796c9134c0SMark Murray */ 10806c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 10816c9134c0SMark Murray 10826c9134c0SMark Murray struct cred_t { 10836c9134c0SMark Murray const char *uname; /* user name */ 10846c9134c0SMark Murray const char *pass; /* password */ 10856c9134c0SMark Murray }; 10866c9134c0SMark Murray typedef struct cred_t cred_t; 10876c9134c0SMark Murray 10886c9134c0SMark Murray static int 10896c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 10906c9134c0SMark Murray struct pam_response **resp, void *appdata) 10916c9134c0SMark Murray { 10926c9134c0SMark Murray int i; 10936c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 10946c9134c0SMark Murray struct pam_response *reply = 10956c9134c0SMark Murray malloc(sizeof(struct pam_response) * num_msg); 10966c9134c0SMark Murray 10976c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 10986c9134c0SMark Murray switch (msg[i]->msg_style) { 10996c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 11006c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 11016c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 11026c9134c0SMark Murray /* PAM frees resp. */ 11036c9134c0SMark Murray break; 11046c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 11056c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 11066c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 11076c9134c0SMark Murray /* PAM frees resp. */ 11086c9134c0SMark Murray break; 11096c9134c0SMark Murray case PAM_TEXT_INFO: 11106c9134c0SMark Murray case PAM_ERROR_MSG: 11116c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 11126c9134c0SMark Murray reply[i].resp = NULL; 11136c9134c0SMark Murray break; 11146c9134c0SMark Murray default: /* unknown message style */ 11156c9134c0SMark Murray free(reply); 11166c9134c0SMark Murray return PAM_CONV_ERR; 11176c9134c0SMark Murray } 11186c9134c0SMark Murray } 11196c9134c0SMark Murray 11206c9134c0SMark Murray *resp = reply; 11216c9134c0SMark Murray return PAM_SUCCESS; 11226c9134c0SMark Murray } 11236c9134c0SMark Murray 11246c9134c0SMark Murray /* 11256c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 11266c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 11276c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 11286c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 11296c9134c0SMark Murray * fall back to a different authentication mechanism. 11306c9134c0SMark Murray */ 11316c9134c0SMark Murray static int 11326c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 11336c9134c0SMark Murray { 11346c9134c0SMark Murray pam_handle_t *pamh = NULL; 11356c9134c0SMark Murray const char *tmpl_user; 11366c9134c0SMark Murray const void *item; 11376c9134c0SMark Murray int rval; 11386c9134c0SMark Murray int e; 11396c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 11406c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 11416c9134c0SMark Murray 11426c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 11436c9134c0SMark Murray if (e != PAM_SUCCESS) { 11446c9134c0SMark Murray syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 11456c9134c0SMark Murray return -1; 11466c9134c0SMark Murray } 11476c9134c0SMark Murray 1148ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1149ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1150ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1151ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1152ea413ab7SGuido van Rooij return -1; 1153ea413ab7SGuido van Rooij } 1154ea413ab7SGuido van Rooij 11556c9134c0SMark Murray e = pam_authenticate(pamh, 0); 11566c9134c0SMark Murray switch (e) { 11576c9134c0SMark Murray case PAM_SUCCESS: 11586c9134c0SMark Murray /* 11596c9134c0SMark Murray * With PAM we support the concept of a "template" 11606c9134c0SMark Murray * user. The user enters a login name which is 11616c9134c0SMark Murray * authenticated by PAM, usually via a remote service 11626c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 11636c9134c0SMark Murray * succeeds, a different but related "template" name 11646c9134c0SMark Murray * is used for setting the credentials, shell, and 11656c9134c0SMark Murray * home directory. The name the user enters need only 11666c9134c0SMark Murray * exist on the remote authentication server, but the 11676c9134c0SMark Murray * template name must be present in the local password 11686c9134c0SMark Murray * database. 11696c9134c0SMark Murray * 11706c9134c0SMark Murray * This is supported by two various mechanisms in the 11716c9134c0SMark Murray * individual modules. However, from the application's 11726c9134c0SMark Murray * point of view, the template user is always passed 11736c9134c0SMark Murray * back as a changed value of the PAM_USER item. 11746c9134c0SMark Murray */ 11756c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 11766c9134c0SMark Murray PAM_SUCCESS) { 11776c9134c0SMark Murray tmpl_user = (const char *) item; 11786c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 11796c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 11806c9134c0SMark Murray } else 11816c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 11826c9134c0SMark Murray pam_strerror(pamh, e)); 11836c9134c0SMark Murray rval = 0; 11846c9134c0SMark Murray break; 11856c9134c0SMark Murray 11866c9134c0SMark Murray case PAM_AUTH_ERR: 11876c9134c0SMark Murray case PAM_USER_UNKNOWN: 11886c9134c0SMark Murray case PAM_MAXTRIES: 11896c9134c0SMark Murray rval = 1; 11906c9134c0SMark Murray break; 11916c9134c0SMark Murray 11926c9134c0SMark Murray default: 11935bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 11946c9134c0SMark Murray rval = -1; 11956c9134c0SMark Murray break; 11966c9134c0SMark Murray } 11976c9134c0SMark Murray 11985bc9d93dSMark Murray if (rval == 0) { 11995bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 12005bc9d93dSMark Murray if (e == PAM_NEW_AUTHTOK_REQD) { 12015bc9d93dSMark Murray e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); 12025bc9d93dSMark Murray if (e != PAM_SUCCESS) { 12035bc9d93dSMark Murray syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e)); 12045bc9d93dSMark Murray rval = 1; 12055bc9d93dSMark Murray } 12065bc9d93dSMark Murray } else if (e != PAM_SUCCESS) { 12075bc9d93dSMark Murray rval = 1; 12085bc9d93dSMark Murray } 12095bc9d93dSMark Murray } 12105bc9d93dSMark Murray 12115bc9d93dSMark Murray if (rval != 0) { 12126c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 12136c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 12145bc9d93dSMark Murray } 12155bc9d93dSMark Murray pamh = NULL; 12166c9134c0SMark Murray } 12176c9134c0SMark Murray return rval; 12186c9134c0SMark Murray } 12196c9134c0SMark Murray 12205bc9d93dSMark Murray #endif /* USE_PAM */ 12216c9134c0SMark Murray 1222ea022d16SRodney W. Grimes void 1223ea022d16SRodney W. Grimes pass(passwd) 1224ea022d16SRodney W. Grimes char *passwd; 1225ea022d16SRodney W. Grimes { 1226a5a4544eSPaul Traina int rval; 1227ea022d16SRodney W. Grimes FILE *fd; 1228b071c689SDavid Nugent #ifdef LOGIN_CAP 1229b071c689SDavid Nugent login_cap_t *lc = NULL; 1230b071c689SDavid Nugent #endif 12315bc9d93dSMark Murray #ifdef USE_PAM 12325bc9d93dSMark Murray int e; 12335bc9d93dSMark Murray #endif 1234ea022d16SRodney W. Grimes 1235ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1236ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1237ea022d16SRodney W. Grimes return; 1238ea022d16SRodney W. Grimes } 1239ea022d16SRodney W. Grimes askpasswd = 0; 1240ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1241a5a4544eSPaul Traina if (pw == NULL) { 1242a5a4544eSPaul Traina rval = 1; /* failure below */ 1243a5a4544eSPaul Traina goto skip; 1244a5a4544eSPaul Traina } 12455bc9d93dSMark Murray #ifdef USE_PAM 12466c9134c0SMark Murray rval = auth_pam(&pw, passwd); 12476c9134c0SMark Murray if (rval >= 0) 1248a5a4544eSPaul Traina goto skip; 1249a5a4544eSPaul Traina #endif 1250028f24cfSSheldon Hearn rval = strcmp(pw->pw_passwd, crypt(passwd, pw->pw_passwd)); 1251ea022d16SRodney W. Grimes /* The strcmp does not catch null passwords! */ 1252a5a4544eSPaul Traina if (*pw->pw_passwd == '\0' || 1253a5a4544eSPaul Traina (pw->pw_expire && time(NULL) >= pw->pw_expire)) 1254a5a4544eSPaul Traina rval = 1; /* failure */ 1255a5a4544eSPaul Traina skip: 1256a5a4544eSPaul Traina /* 1257a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 12586c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1259a5a4544eSPaul Traina * succeeded. 1260a5a4544eSPaul Traina */ 1261a5a4544eSPaul Traina if (rval) { 1262ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1263ea022d16SRodney W. Grimes if (logging) 1264ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1265ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1266ea022d16SRodney W. Grimes remotehost, curname); 1267ea022d16SRodney W. Grimes pw = NULL; 1268ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1269ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1270ea022d16SRodney W. Grimes "repeated login failures from %s", 1271ea022d16SRodney W. Grimes remotehost); 1272ea022d16SRodney W. Grimes exit(0); 1273ea022d16SRodney W. Grimes } 1274ea022d16SRodney W. Grimes return; 1275ea022d16SRodney W. Grimes } 1276ea022d16SRodney W. Grimes } 1277ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1278ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 1279ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1280ea022d16SRodney W. Grimes return; 1281ea022d16SRodney W. Grimes } 1282b071c689SDavid Nugent /* May be overridden by login.conf */ 1283b071c689SDavid Nugent (void) umask(defumask); 1284b071c689SDavid Nugent #ifdef LOGIN_CAP 12855d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 1286b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 1287b071c689SDavid Nugent 12884dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 12894dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 12904dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 1291b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1292b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1293b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1294b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1295b071c689SDavid Nugent pw->pw_name); 1296b071c689SDavid Nugent reply(530, "Permission denied.\n"); 1297b071c689SDavid Nugent pw = NULL; 1298b071c689SDavid Nugent return; 1299b071c689SDavid Nugent } 1300b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1301b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1302b071c689SDavid Nugent pw = NULL; 1303b071c689SDavid Nugent return; 1304b071c689SDavid Nugent } 1305b071c689SDavid Nugent } 1306b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1307e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1308e6fa0d43SDag-Erling Smørgrav LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1309b071c689SDavid Nugent #else 1310e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1311ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1312b071c689SDavid Nugent #endif 1313ea022d16SRodney W. Grimes 13145bc9d93dSMark Murray #ifdef USE_PAM 13155bc9d93dSMark Murray if (pamh) { 13165bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 13175bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 13185bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 13195bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 13205bc9d93dSMark Murray } 13215bc9d93dSMark Murray } 13225bc9d93dSMark Murray #endif 13235bc9d93dSMark Murray 1324ea022d16SRodney W. Grimes /* open wtmp before chroot */ 1325986a1172SThomas Gellekum ftpd_logwtmp(ttyline, pw->pw_name, remotehost); 1326ea022d16SRodney W. Grimes logged_in = 1; 1327ea022d16SRodney W. Grimes 1328a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1329ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1330ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1331ea4e54b9SDavid Nugent #else 13323eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1333ea4e54b9SDavid Nugent #endif 13343eb568f2SGuido van Rooij stats = 0; 13353eb568f2SGuido van Rooij 1336b071c689SDavid Nugent dochroot = 1337b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1338b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 1339b071c689SDavid Nugent #endif 13407edcb936SSteve Price checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1341ea022d16SRodney W. Grimes if (guest) { 1342ea022d16SRodney W. Grimes /* 1343ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 1344ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 1345ea022d16SRodney W. Grimes * outside the new root! 1346ea022d16SRodney W. Grimes */ 1347ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1348ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 1349ea022d16SRodney W. Grimes goto bad; 1350ea022d16SRodney W. Grimes } 1351a5a4544eSPaul Traina } else if (dochroot) { 1352a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1353a5a4544eSPaul Traina reply(550, "Can't change root."); 1354a5a4544eSPaul Traina goto bad; 1355a5a4544eSPaul Traina } 1356ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 1357ea022d16SRodney W. Grimes if (chdir("/") < 0) { 1358ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 1359ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 1360ea022d16SRodney W. Grimes goto bad; 1361ea022d16SRodney W. Grimes } else 1362ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 1363ea022d16SRodney W. Grimes } 1364ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1365ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1366ea022d16SRodney W. Grimes goto bad; 1367ea022d16SRodney W. Grimes } 136882c76939SDavid Greenman 136982c76939SDavid Greenman /* 1370ea022d16SRodney W. Grimes * Display a login message, if it exists. 1371ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1372ea022d16SRodney W. Grimes */ 1373ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1374ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1375ea4e54b9SDavid Nugent #else 1376ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1377ea4e54b9SDavid Nugent #endif 1378ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1379ea022d16SRodney W. Grimes 1380ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1381ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1382ea022d16SRodney W. Grimes *cp = '\0'; 1383ea022d16SRodney W. Grimes lreply(230, "%s", line); 1384ea022d16SRodney W. Grimes } 1385ea022d16SRodney W. Grimes (void) fflush(stdout); 1386ea022d16SRodney W. Grimes (void) fclose(fd); 1387ea022d16SRodney W. Grimes } 1388ea022d16SRodney W. Grimes if (guest) { 13893eb568f2SGuido van Rooij if (ident != NULL) 13903eb568f2SGuido van Rooij free(ident); 139161f891a6SPaul Traina ident = strdup(passwd); 139261f891a6SPaul Traina if (ident == NULL) 1393618b0bbaSMark Murray fatalerror("Ran out of memory."); 139461f891a6SPaul Traina 1395ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1396ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1397ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1398ea4e54b9SDavid Nugent if (thishost != firsthost) 1399ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1400ea4e54b9SDavid Nugent "%s: anonymous(%s)/%.*s", remotehost, hostname, 14016c9134c0SMark Murray (int)(sizeof(proctitle) - sizeof(remotehost) - 14026c9134c0SMark Murray sizeof(": anonymous/")), passwd); 1403ea4e54b9SDavid Nugent else 1404ea4e54b9SDavid Nugent #endif 1405ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1406ea022d16SRodney W. Grimes "%s: anonymous/%.*s", remotehost, 14076c9134c0SMark Murray (int)(sizeof(proctitle) - sizeof(remotehost) - 14086c9134c0SMark Murray sizeof(": anonymous/")), passwd); 1409b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1410ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1411ea022d16SRodney W. Grimes if (logging) 1412ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1413ea022d16SRodney W. Grimes remotehost, passwd); 1414ea022d16SRodney W. Grimes } else { 14153401a71fSDaniel O'Callaghan if (dochroot) 14163401a71fSDaniel O'Callaghan reply(230, "User %s logged in, access restrictions apply.", 14173401a71fSDaniel O'Callaghan pw->pw_name); 14183401a71fSDaniel O'Callaghan else 1419ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 14203401a71fSDaniel O'Callaghan 1421ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1422ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1423ea022d16SRodney W. Grimes "%s: %s", remotehost, pw->pw_name); 1424b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1425ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1426ea022d16SRodney W. Grimes if (logging) 1427ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1428ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1429ea022d16SRodney W. Grimes } 1430b071c689SDavid Nugent #ifdef LOGIN_CAP 1431b071c689SDavid Nugent login_close(lc); 1432b071c689SDavid Nugent #endif 1433ea022d16SRodney W. Grimes return; 1434ea022d16SRodney W. Grimes bad: 1435ea022d16SRodney W. Grimes /* Forget all about it... */ 1436b071c689SDavid Nugent #ifdef LOGIN_CAP 1437b071c689SDavid Nugent login_close(lc); 1438b071c689SDavid Nugent #endif 1439ea022d16SRodney W. Grimes end_login(); 1440ea022d16SRodney W. Grimes } 1441ea022d16SRodney W. Grimes 1442ea022d16SRodney W. Grimes void 1443ea022d16SRodney W. Grimes retrieve(cmd, name) 1444ea022d16SRodney W. Grimes char *cmd, *name; 1445ea022d16SRodney W. Grimes { 1446ea022d16SRodney W. Grimes FILE *fin, *dout; 1447ea022d16SRodney W. Grimes struct stat st; 1448ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1449158a00b2SJohn Birrell time_t start; 1450ea022d16SRodney W. Grimes 1451ea022d16SRodney W. Grimes if (cmd == 0) { 1452ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1453ea022d16SRodney W. Grimes st.st_size = 0; 1454ea022d16SRodney W. Grimes } else { 1455ea022d16SRodney W. Grimes char line[BUFSIZ]; 1456ea022d16SRodney W. Grimes 1457e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1458ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1459ea022d16SRodney W. Grimes st.st_size = -1; 1460ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1461ea022d16SRodney W. Grimes } 1462ea022d16SRodney W. Grimes if (fin == NULL) { 1463ea022d16SRodney W. Grimes if (errno != 0) { 1464ea022d16SRodney W. Grimes perror_reply(550, name); 1465ea022d16SRodney W. Grimes if (cmd == 0) { 1466ea022d16SRodney W. Grimes LOGCMD("get", name); 1467ea022d16SRodney W. Grimes } 1468ea022d16SRodney W. Grimes } 1469ea022d16SRodney W. Grimes return; 1470ea022d16SRodney W. Grimes } 1471ea022d16SRodney W. Grimes byte_count = -1; 1472ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1473ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1474ea022d16SRodney W. Grimes goto done; 1475ea022d16SRodney W. Grimes } 1476ea022d16SRodney W. Grimes if (restart_point) { 1477ea022d16SRodney W. Grimes if (type == TYPE_A) { 1478ea022d16SRodney W. Grimes off_t i, n; 1479ea022d16SRodney W. Grimes int c; 1480ea022d16SRodney W. Grimes 1481ea022d16SRodney W. Grimes n = restart_point; 1482ea022d16SRodney W. Grimes i = 0; 1483ea022d16SRodney W. Grimes while (i++ < n) { 1484ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1485ea022d16SRodney W. Grimes perror_reply(550, name); 1486ea022d16SRodney W. Grimes goto done; 1487ea022d16SRodney W. Grimes } 1488ea022d16SRodney W. Grimes if (c == '\n') 1489ea022d16SRodney W. Grimes i++; 1490ea022d16SRodney W. Grimes } 1491ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1492ea022d16SRodney W. Grimes perror_reply(550, name); 1493ea022d16SRodney W. Grimes goto done; 1494ea022d16SRodney W. Grimes } 1495ea022d16SRodney W. Grimes } 1496ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1497ea022d16SRodney W. Grimes if (dout == NULL) 1498ea022d16SRodney W. Grimes goto done; 14993eb568f2SGuido van Rooij time(&start); 15009fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 15019fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 15023eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 15033eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1504ea022d16SRodney W. Grimes (void) fclose(dout); 1505ea022d16SRodney W. Grimes data = -1; 1506ea022d16SRodney W. Grimes pdata = -1; 1507ea022d16SRodney W. Grimes done: 1508ea022d16SRodney W. Grimes if (cmd == 0) 1509ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1510ea022d16SRodney W. Grimes (*closefunc)(fin); 1511ea022d16SRodney W. Grimes } 1512ea022d16SRodney W. Grimes 1513ea022d16SRodney W. Grimes void 1514ea022d16SRodney W. Grimes store(name, mode, unique) 1515ea022d16SRodney W. Grimes char *name, *mode; 1516ea022d16SRodney W. Grimes int unique; 1517ea022d16SRodney W. Grimes { 1518ea022d16SRodney W. Grimes FILE *fout, *din; 1519ea022d16SRodney W. Grimes struct stat st; 1520ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1521ea022d16SRodney W. Grimes 152261f891a6SPaul Traina if ((unique || guest) && stat(name, &st) == 0 && 1523ea022d16SRodney W. Grimes (name = gunique(name)) == NULL) { 1524ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1525ea022d16SRodney W. Grimes return; 1526ea022d16SRodney W. Grimes } 1527ea022d16SRodney W. Grimes 1528ea022d16SRodney W. Grimes if (restart_point) 1529ea022d16SRodney W. Grimes mode = "r+"; 1530ea022d16SRodney W. Grimes fout = fopen(name, mode); 1531ea022d16SRodney W. Grimes closefunc = fclose; 1532ea022d16SRodney W. Grimes if (fout == NULL) { 1533ea022d16SRodney W. Grimes perror_reply(553, name); 1534ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1535ea022d16SRodney W. Grimes return; 1536ea022d16SRodney W. Grimes } 1537ea022d16SRodney W. Grimes byte_count = -1; 1538ea022d16SRodney W. Grimes if (restart_point) { 1539ea022d16SRodney W. Grimes if (type == TYPE_A) { 1540ea022d16SRodney W. Grimes off_t i, n; 1541ea022d16SRodney W. Grimes int c; 1542ea022d16SRodney W. Grimes 1543ea022d16SRodney W. Grimes n = restart_point; 1544ea022d16SRodney W. Grimes i = 0; 1545ea022d16SRodney W. Grimes while (i++ < n) { 1546ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1547ea022d16SRodney W. Grimes perror_reply(550, name); 1548ea022d16SRodney W. Grimes goto done; 1549ea022d16SRodney W. Grimes } 1550ea022d16SRodney W. Grimes if (c == '\n') 1551ea022d16SRodney W. Grimes i++; 1552ea022d16SRodney W. Grimes } 1553ea022d16SRodney W. Grimes /* 1554ea022d16SRodney W. Grimes * We must do this seek to "current" position 1555ea022d16SRodney W. Grimes * because we are changing from reading to 1556ea022d16SRodney W. Grimes * writing. 1557ea022d16SRodney W. Grimes */ 1558e4a71114SAndrey A. Chernov if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) { 1559ea022d16SRodney W. Grimes perror_reply(550, name); 1560ea022d16SRodney W. Grimes goto done; 1561ea022d16SRodney W. Grimes } 1562ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1563ea022d16SRodney W. Grimes perror_reply(550, name); 1564ea022d16SRodney W. Grimes goto done; 1565ea022d16SRodney W. Grimes } 1566ea022d16SRodney W. Grimes } 1567ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1568ea022d16SRodney W. Grimes if (din == NULL) 1569ea022d16SRodney W. Grimes goto done; 1570ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1571ea022d16SRodney W. Grimes if (unique) 1572ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1573ea022d16SRodney W. Grimes name); 1574ea022d16SRodney W. Grimes else 1575ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1576ea022d16SRodney W. Grimes } 1577ea022d16SRodney W. Grimes (void) fclose(din); 1578ea022d16SRodney W. Grimes data = -1; 1579ea022d16SRodney W. Grimes pdata = -1; 1580ea022d16SRodney W. Grimes done: 1581ea022d16SRodney W. Grimes LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1582ea022d16SRodney W. Grimes (*closefunc)(fout); 1583ea022d16SRodney W. Grimes } 1584ea022d16SRodney W. Grimes 1585ea022d16SRodney W. Grimes static FILE * 1586ea022d16SRodney W. Grimes getdatasock(mode) 1587ea022d16SRodney W. Grimes char *mode; 1588ea022d16SRodney W. Grimes { 1589ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1590ea022d16SRodney W. Grimes 1591ea022d16SRodney W. Grimes if (data >= 0) 1592ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1593ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 15944dd8b5abSYoshinobu Inoue 15954dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1596ea022d16SRodney W. Grimes if (s < 0) 1597ea022d16SRodney W. Grimes goto bad; 1598ea022d16SRodney W. Grimes if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1599ea022d16SRodney W. Grimes (char *) &on, sizeof(on)) < 0) 1600ea022d16SRodney W. Grimes goto bad; 1601ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 16024dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 16034dd8b5abSYoshinobu Inoue data_source.su_port = htons(20); /* ftp-data port */ 1604ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1605ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 16064dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1607ea022d16SRodney W. Grimes break; 1608ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1609ea022d16SRodney W. Grimes goto bad; 1610ea022d16SRodney W. Grimes sleep(tries); 1611ea022d16SRodney W. Grimes } 1612ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1613ea022d16SRodney W. Grimes #ifdef IP_TOS 16144dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 16154dd8b5abSYoshinobu Inoue { 1616ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 1617ea022d16SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1618ea022d16SRodney W. Grimes syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 16194dd8b5abSYoshinobu Inoue } 1620ea022d16SRodney W. Grimes #endif 16219fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 16229fc5823aSGarrett Wollman /* 16239fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 16249fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 16259fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 16269fc5823aSGarrett Wollman * in heavy-load situations. 16279fc5823aSGarrett Wollman */ 16289fc5823aSGarrett Wollman on = 1; 16299fc5823aSGarrett Wollman if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 16309fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 16319fc5823aSGarrett Wollman #endif 16329fc5823aSGarrett Wollman #ifdef SO_SNDBUF 16339fc5823aSGarrett Wollman on = 65536; 16349fc5823aSGarrett Wollman if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 16359fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 16369fc5823aSGarrett Wollman #endif 16379fc5823aSGarrett Wollman 1638ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1639ea022d16SRodney W. Grimes bad: 1640ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1641ea022d16SRodney W. Grimes t = errno; 1642ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1643ea022d16SRodney W. Grimes (void) close(s); 1644ea022d16SRodney W. Grimes errno = t; 1645ea022d16SRodney W. Grimes return (NULL); 1646ea022d16SRodney W. Grimes } 1647ea022d16SRodney W. Grimes 1648ea022d16SRodney W. Grimes static FILE * 1649ea022d16SRodney W. Grimes dataconn(name, size, mode) 1650ea022d16SRodney W. Grimes char *name; 1651ea022d16SRodney W. Grimes off_t size; 1652ea022d16SRodney W. Grimes char *mode; 1653ea022d16SRodney W. Grimes { 1654ea022d16SRodney W. Grimes char sizebuf[32]; 1655ea022d16SRodney W. Grimes FILE *file; 1656ea022d16SRodney W. Grimes int retry = 0, tos; 1657ea022d16SRodney W. Grimes 1658ea022d16SRodney W. Grimes file_size = size; 1659ea022d16SRodney W. Grimes byte_count = 0; 1660ea022d16SRodney W. Grimes if (size != (off_t) -1) 1661e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1662ea022d16SRodney W. Grimes else 1663e760ef2cSWarner Losh *sizebuf = '\0'; 1664ea022d16SRodney W. Grimes if (pdata >= 0) { 16654dd8b5abSYoshinobu Inoue union sockunion from; 16664dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1667d6ed3c37SGuido van Rooij struct timeval timeout; 1668d6ed3c37SGuido van Rooij fd_set set; 1669ea022d16SRodney W. Grimes 1670d6ed3c37SGuido van Rooij FD_ZERO(&set); 1671d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1672d6ed3c37SGuido van Rooij 1673d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1674d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1675d6ed3c37SGuido van Rooij 1676d6ed3c37SGuido van Rooij if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1677d6ed3c37SGuido van Rooij (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1678ea022d16SRodney W. Grimes reply(425, "Can't open data connection."); 1679ea022d16SRodney W. Grimes (void) close(pdata); 1680ea022d16SRodney W. Grimes pdata = -1; 1681ea022d16SRodney W. Grimes return (NULL); 1682ea022d16SRodney W. Grimes } 1683ea022d16SRodney W. Grimes (void) close(pdata); 1684ea022d16SRodney W. Grimes pdata = s; 1685ea022d16SRodney W. Grimes #ifdef IP_TOS 16864dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 16874dd8b5abSYoshinobu Inoue { 1688a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 1689ea022d16SRodney W. Grimes (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1690ea022d16SRodney W. Grimes sizeof(int)); 16914dd8b5abSYoshinobu Inoue } 1692ea022d16SRodney W. Grimes #endif 1693ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1694ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1695ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 1696ea022d16SRodney W. Grimes } 1697ea022d16SRodney W. Grimes if (data >= 0) { 1698ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1699ea022d16SRodney W. Grimes name, sizebuf); 1700ea022d16SRodney W. Grimes usedefault = 1; 1701ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1702ea022d16SRodney W. Grimes } 1703ea022d16SRodney W. Grimes if (usedefault) 1704ea022d16SRodney W. Grimes data_dest = his_addr; 1705ea022d16SRodney W. Grimes usedefault = 1; 1706ea022d16SRodney W. Grimes file = getdatasock(mode); 1707ea022d16SRodney W. Grimes if (file == NULL) { 17084dd8b5abSYoshinobu Inoue char hostbuf[BUFSIZ], portbuf[BUFSIZ]; 17094dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&data_source, 17104dd8b5abSYoshinobu Inoue data_source.su_len, hostbuf, sizeof(hostbuf) - 1, 17114dd8b5abSYoshinobu Inoue portbuf, sizeof(portbuf), 17124dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID); 17134dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 17144dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1715ea022d16SRodney W. Grimes return (NULL); 1716ea022d16SRodney W. Grimes } 1717ea022d16SRodney W. Grimes data = fileno(file); 1718ea022d16SRodney W. Grimes while (connect(data, (struct sockaddr *)&data_dest, 17194dd8b5abSYoshinobu Inoue data_dest.su_len) < 0) { 1720ea022d16SRodney W. Grimes if (errno == EADDRINUSE && retry < swaitmax) { 1721ea022d16SRodney W. Grimes sleep((unsigned) swaitint); 1722ea022d16SRodney W. Grimes retry += swaitint; 1723ea022d16SRodney W. Grimes continue; 1724ea022d16SRodney W. Grimes } 1725ea022d16SRodney W. Grimes perror_reply(425, "Can't build data connection"); 1726ea022d16SRodney W. Grimes (void) fclose(file); 1727ea022d16SRodney W. Grimes data = -1; 1728ea022d16SRodney W. Grimes return (NULL); 1729ea022d16SRodney W. Grimes } 1730ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1731ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1732ea022d16SRodney W. Grimes return (file); 1733ea022d16SRodney W. Grimes } 1734ea022d16SRodney W. Grimes 1735ea022d16SRodney W. Grimes /* 1736ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 17379fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1738ea022d16SRodney W. Grimes * 1739ea022d16SRodney W. Grimes * NB: Form isn't handled. 1740ea022d16SRodney W. Grimes */ 1741ea022d16SRodney W. Grimes static void 17429fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg) 1743ea022d16SRodney W. Grimes FILE *instr, *outstr; 1744ea022d16SRodney W. Grimes off_t blksize; 17459fc5823aSGarrett Wollman off_t filesize; 17469fc5823aSGarrett Wollman int isreg; 1747ea022d16SRodney W. Grimes { 1748f6f0c4b9SDan Moschuk int c, filefd, netfd; 1749f6f0c4b9SDan Moschuk char *buf; 17509fc5823aSGarrett Wollman size_t len; 1751f6f0c4b9SDan Moschuk off_t cnt; 1752ea022d16SRodney W. Grimes 1753ea022d16SRodney W. Grimes transflag++; 1754ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1755ea022d16SRodney W. Grimes transflag = 0; 1756ea022d16SRodney W. Grimes return; 1757ea022d16SRodney W. Grimes } 1758ea022d16SRodney W. Grimes switch (type) { 1759ea022d16SRodney W. Grimes 1760ea022d16SRodney W. Grimes case TYPE_A: 1761ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1762ea022d16SRodney W. Grimes byte_count++; 1763ea022d16SRodney W. Grimes if (c == '\n') { 1764ea022d16SRodney W. Grimes if (ferror(outstr)) 1765ea022d16SRodney W. Grimes goto data_err; 1766ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1767ea022d16SRodney W. Grimes } 1768ea022d16SRodney W. Grimes (void) putc(c, outstr); 1769ea022d16SRodney W. Grimes } 1770ea022d16SRodney W. Grimes fflush(outstr); 1771ea022d16SRodney W. Grimes transflag = 0; 1772ea022d16SRodney W. Grimes if (ferror(instr)) 1773ea022d16SRodney W. Grimes goto file_err; 1774ea022d16SRodney W. Grimes if (ferror(outstr)) 1775ea022d16SRodney W. Grimes goto data_err; 1776ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1777ea022d16SRodney W. Grimes return; 1778ea022d16SRodney W. Grimes 1779ea022d16SRodney W. Grimes case TYPE_I: 1780ea022d16SRodney W. Grimes case TYPE_L: 17819fc5823aSGarrett Wollman /* 17829fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 17839fc5823aSGarrett Wollman * are sending a regular file 17849fc5823aSGarrett Wollman */ 17859fc5823aSGarrett Wollman netfd = fileno(outstr); 17869fc5823aSGarrett Wollman filefd = fileno(instr); 17879fc5823aSGarrett Wollman 1788f6f0c4b9SDan Moschuk if (isreg) { 17899fc5823aSGarrett Wollman 1790f6f0c4b9SDan Moschuk off_t offset; 1791f6f0c4b9SDan Moschuk int err; 1792f6f0c4b9SDan Moschuk 1793f6f0c4b9SDan Moschuk len = filesize; 1794f6f0c4b9SDan Moschuk err = cnt = offset = 0; 1795f6f0c4b9SDan Moschuk 1796f6f0c4b9SDan Moschuk while (err != -1 && cnt < filesize) { 1797f6f0c4b9SDan Moschuk err = sendfile(filefd, netfd, offset, len, 1798f6f0c4b9SDan Moschuk (struct sf_hdtr *) NULL, &cnt, 0); 17991f15c0d6SDag-Erling Smørgrav byte_count += cnt; 1800f6f0c4b9SDan Moschuk offset += cnt; 1801f6f0c4b9SDan Moschuk len -= cnt; 1802f6f0c4b9SDan Moschuk 1803f6f0c4b9SDan Moschuk if (err == -1) { 1804f6f0c4b9SDan Moschuk if (!cnt) 1805f6f0c4b9SDan Moschuk goto oldway; 1806f6f0c4b9SDan Moschuk 18079fc5823aSGarrett Wollman goto data_err; 1808f6f0c4b9SDan Moschuk } 1809f6f0c4b9SDan Moschuk } 1810f6f0c4b9SDan Moschuk 18119fc5823aSGarrett Wollman reply(226, "Transfer complete."); 18129fc5823aSGarrett Wollman return; 18139fc5823aSGarrett Wollman } 18149fc5823aSGarrett Wollman 18159fc5823aSGarrett Wollman oldway: 1816ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1817ea022d16SRodney W. Grimes transflag = 0; 1818ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 1819ea022d16SRodney W. Grimes return; 1820ea022d16SRodney W. Grimes } 18219fc5823aSGarrett Wollman 1822ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1823ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1824ea022d16SRodney W. Grimes byte_count += cnt; 1825ea022d16SRodney W. Grimes transflag = 0; 1826ea022d16SRodney W. Grimes (void)free(buf); 1827ea022d16SRodney W. Grimes if (cnt != 0) { 1828ea022d16SRodney W. Grimes if (cnt < 0) 1829ea022d16SRodney W. Grimes goto file_err; 1830ea022d16SRodney W. Grimes goto data_err; 1831ea022d16SRodney W. Grimes } 1832ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1833ea022d16SRodney W. Grimes return; 1834ea022d16SRodney W. Grimes default: 1835ea022d16SRodney W. Grimes transflag = 0; 1836ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 1837ea022d16SRodney W. Grimes return; 1838ea022d16SRodney W. Grimes } 1839ea022d16SRodney W. Grimes 1840ea022d16SRodney W. Grimes data_err: 1841ea022d16SRodney W. Grimes transflag = 0; 1842ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 1843ea022d16SRodney W. Grimes return; 1844ea022d16SRodney W. Grimes 1845ea022d16SRodney W. Grimes file_err: 1846ea022d16SRodney W. Grimes transflag = 0; 1847ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 1848ea022d16SRodney W. Grimes } 1849ea022d16SRodney W. Grimes 1850ea022d16SRodney W. Grimes /* 1851ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 1852ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 1853ea022d16SRodney W. Grimes * 1854ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 1855ea022d16SRodney W. Grimes */ 1856ea022d16SRodney W. Grimes static int 1857ea022d16SRodney W. Grimes receive_data(instr, outstr) 1858ea022d16SRodney W. Grimes FILE *instr, *outstr; 1859ea022d16SRodney W. Grimes { 1860ea022d16SRodney W. Grimes int c; 186161f891a6SPaul Traina int cnt, bare_lfs; 1862ea022d16SRodney W. Grimes char buf[BUFSIZ]; 1863ea022d16SRodney W. Grimes 1864ea022d16SRodney W. Grimes transflag++; 1865ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1866ea022d16SRodney W. Grimes transflag = 0; 1867ea022d16SRodney W. Grimes return (-1); 1868ea022d16SRodney W. Grimes } 186961f891a6SPaul Traina 187061f891a6SPaul Traina bare_lfs = 0; 187161f891a6SPaul Traina 1872ea022d16SRodney W. Grimes switch (type) { 1873ea022d16SRodney W. Grimes 1874ea022d16SRodney W. Grimes case TYPE_I: 1875ea022d16SRodney W. Grimes case TYPE_L: 1876ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1877ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 1878ea022d16SRodney W. Grimes goto file_err; 1879ea022d16SRodney W. Grimes byte_count += cnt; 1880ea022d16SRodney W. Grimes } 1881ea022d16SRodney W. Grimes if (cnt < 0) 1882ea022d16SRodney W. Grimes goto data_err; 1883ea022d16SRodney W. Grimes transflag = 0; 1884ea022d16SRodney W. Grimes return (0); 1885ea022d16SRodney W. Grimes 1886ea022d16SRodney W. Grimes case TYPE_E: 1887ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 1888ea022d16SRodney W. Grimes transflag = 0; 1889ea022d16SRodney W. Grimes return (-1); 1890ea022d16SRodney W. Grimes 1891ea022d16SRodney W. Grimes case TYPE_A: 1892ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1893ea022d16SRodney W. Grimes byte_count++; 1894ea022d16SRodney W. Grimes if (c == '\n') 1895ea022d16SRodney W. Grimes bare_lfs++; 1896ea022d16SRodney W. Grimes while (c == '\r') { 1897ea022d16SRodney W. Grimes if (ferror(outstr)) 1898ea022d16SRodney W. Grimes goto data_err; 1899ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 1900ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 1901ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 1902ea022d16SRodney W. Grimes goto contin2; 1903ea022d16SRodney W. Grimes } 1904ea022d16SRodney W. Grimes } 1905ea022d16SRodney W. Grimes (void) putc(c, outstr); 1906ea022d16SRodney W. Grimes contin2: ; 1907ea022d16SRodney W. Grimes } 1908ea022d16SRodney W. Grimes fflush(outstr); 1909ea022d16SRodney W. Grimes if (ferror(instr)) 1910ea022d16SRodney W. Grimes goto data_err; 1911ea022d16SRodney W. Grimes if (ferror(outstr)) 1912ea022d16SRodney W. Grimes goto file_err; 1913ea022d16SRodney W. Grimes transflag = 0; 1914ea022d16SRodney W. Grimes if (bare_lfs) { 1915ea022d16SRodney W. Grimes lreply(226, 1916ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 1917ea022d16SRodney W. Grimes bare_lfs); 1918ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 1919ea022d16SRodney W. Grimes } 1920ea022d16SRodney W. Grimes return (0); 1921ea022d16SRodney W. Grimes default: 1922ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 1923ea022d16SRodney W. Grimes transflag = 0; 1924ea022d16SRodney W. Grimes return (-1); 1925ea022d16SRodney W. Grimes } 1926ea022d16SRodney W. Grimes 1927ea022d16SRodney W. Grimes data_err: 1928ea022d16SRodney W. Grimes transflag = 0; 1929ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 1930ea022d16SRodney W. Grimes return (-1); 1931ea022d16SRodney W. Grimes 1932ea022d16SRodney W. Grimes file_err: 1933ea022d16SRodney W. Grimes transflag = 0; 1934ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 1935ea022d16SRodney W. Grimes return (-1); 1936ea022d16SRodney W. Grimes } 1937ea022d16SRodney W. Grimes 1938ea022d16SRodney W. Grimes void 1939ea022d16SRodney W. Grimes statfilecmd(filename) 1940ea022d16SRodney W. Grimes char *filename; 1941ea022d16SRodney W. Grimes { 1942ea022d16SRodney W. Grimes FILE *fin; 1943ea022d16SRodney W. Grimes int c; 1944ea022d16SRodney W. Grimes char line[LINE_MAX]; 1945ea022d16SRodney W. Grimes 1946af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 1947ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 1948ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 1949ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 1950ea022d16SRodney W. Grimes if (c == '\n') { 1951ea022d16SRodney W. Grimes if (ferror(stdout)){ 1952ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 1953ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1954ea022d16SRodney W. Grimes dologout(1); 1955ea022d16SRodney W. Grimes /* NOTREACHED */ 1956ea022d16SRodney W. Grimes } 1957ea022d16SRodney W. Grimes if (ferror(fin)) { 1958ea022d16SRodney W. Grimes perror_reply(551, filename); 1959ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1960ea022d16SRodney W. Grimes return; 1961ea022d16SRodney W. Grimes } 1962ea022d16SRodney W. Grimes (void) putc('\r', stdout); 1963ea022d16SRodney W. Grimes } 1964ea022d16SRodney W. Grimes (void) putc(c, stdout); 1965ea022d16SRodney W. Grimes } 1966ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1967ea022d16SRodney W. Grimes reply(211, "End of Status"); 1968ea022d16SRodney W. Grimes } 1969ea022d16SRodney W. Grimes 1970ea022d16SRodney W. Grimes void 1971ea022d16SRodney W. Grimes statcmd() 1972ea022d16SRodney W. Grimes { 19734dd8b5abSYoshinobu Inoue union sockunion *su; 1974ea022d16SRodney W. Grimes u_char *a, *p; 19754dd8b5abSYoshinobu Inoue char hname[INET6_ADDRSTRLEN]; 19764dd8b5abSYoshinobu Inoue int ispassive; 1977ea022d16SRodney W. Grimes 1978ea022d16SRodney W. Grimes lreply(211, "%s FTP server status:", hostname, version); 1979ea022d16SRodney W. Grimes printf(" %s\r\n", version); 1980ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 19814dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 19824dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 19834dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID)) { 19844dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 19854dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 19864dd8b5abSYoshinobu Inoue } 1987ea022d16SRodney W. Grimes printf("\r\n"); 1988ea022d16SRodney W. Grimes if (logged_in) { 1989ea022d16SRodney W. Grimes if (guest) 1990ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 1991ea022d16SRodney W. Grimes else 1992ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 1993ea022d16SRodney W. Grimes } else if (askpasswd) 1994ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 1995ea022d16SRodney W. Grimes else 1996ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 1997ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 1998ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 1999ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2000ea022d16SRodney W. Grimes if (type == TYPE_L) 2001ea022d16SRodney W. Grimes #if NBBY == 8 2002ea022d16SRodney W. Grimes printf(" %d", NBBY); 2003ea022d16SRodney W. Grimes #else 2004ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2005ea022d16SRodney W. Grimes #endif 2006ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2007ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2008ea022d16SRodney W. Grimes if (data != -1) 2009ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2010ea022d16SRodney W. Grimes else if (pdata != -1) { 20114dd8b5abSYoshinobu Inoue ispassive = 1; 20124dd8b5abSYoshinobu Inoue su = &pasv_addr; 2013ea022d16SRodney W. Grimes goto printaddr; 2014ea022d16SRodney W. Grimes } else if (usedefault == 0) { 20154dd8b5abSYoshinobu Inoue ispassive = 0; 20164dd8b5abSYoshinobu Inoue su = &data_dest; 2017ea022d16SRodney W. Grimes printaddr: 2018ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 20194dd8b5abSYoshinobu Inoue if (epsvall) { 20204dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 20214dd8b5abSYoshinobu Inoue goto epsvonly; 20224dd8b5abSYoshinobu Inoue } 20234dd8b5abSYoshinobu Inoue 20244dd8b5abSYoshinobu Inoue /* PORT/PASV */ 20254dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 20264dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 20274dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 20284dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 20294dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 20304dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 20314dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 20324dd8b5abSYoshinobu Inoue } 20334dd8b5abSYoshinobu Inoue 20344dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 20354dd8b5abSYoshinobu Inoue { 20364dd8b5abSYoshinobu Inoue int alen, af, i; 20374dd8b5abSYoshinobu Inoue 20384dd8b5abSYoshinobu Inoue switch (su->su_family) { 20394dd8b5abSYoshinobu Inoue case AF_INET: 20404dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 20414dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 20424dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 20434dd8b5abSYoshinobu Inoue af = 4; 20444dd8b5abSYoshinobu Inoue break; 20454dd8b5abSYoshinobu Inoue case AF_INET6: 20464dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 20474dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 20484dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 20494dd8b5abSYoshinobu Inoue af = 6; 20504dd8b5abSYoshinobu Inoue break; 20514dd8b5abSYoshinobu Inoue default: 20524dd8b5abSYoshinobu Inoue af = 0; 20534dd8b5abSYoshinobu Inoue break; 20544dd8b5abSYoshinobu Inoue } 20554dd8b5abSYoshinobu Inoue if (af) { 20564dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 20574dd8b5abSYoshinobu Inoue af, alen); 20584dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 20594dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 20604dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 20614dd8b5abSYoshinobu Inoue } 20624dd8b5abSYoshinobu Inoue } 20634dd8b5abSYoshinobu Inoue 20644dd8b5abSYoshinobu Inoue epsvonly:; 20654dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 20664dd8b5abSYoshinobu Inoue { 20674dd8b5abSYoshinobu Inoue int af; 20684dd8b5abSYoshinobu Inoue 20694dd8b5abSYoshinobu Inoue switch (su->su_family) { 20704dd8b5abSYoshinobu Inoue case AF_INET: 20714dd8b5abSYoshinobu Inoue af = 1; 20724dd8b5abSYoshinobu Inoue break; 20734dd8b5abSYoshinobu Inoue case AF_INET6: 20744dd8b5abSYoshinobu Inoue af = 2; 20754dd8b5abSYoshinobu Inoue break; 20764dd8b5abSYoshinobu Inoue default: 20774dd8b5abSYoshinobu Inoue af = 0; 20784dd8b5abSYoshinobu Inoue break; 20794dd8b5abSYoshinobu Inoue } 20804dd8b5abSYoshinobu Inoue if (af) { 20814dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)su, su->su_len, 20824dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 20834dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 20844dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 20854dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 20864dd8b5abSYoshinobu Inoue af, hname, htons(su->su_port)); 20874dd8b5abSYoshinobu Inoue } 20884dd8b5abSYoshinobu Inoue } 20894dd8b5abSYoshinobu Inoue } 2090ea022d16SRodney W. Grimes #undef UC 2091ea022d16SRodney W. Grimes } else 2092ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 2093ea022d16SRodney W. Grimes reply(211, "End of status"); 2094ea022d16SRodney W. Grimes } 2095ea022d16SRodney W. Grimes 2096ea022d16SRodney W. Grimes void 2097618b0bbaSMark Murray fatalerror(s) 2098ea022d16SRodney W. Grimes char *s; 2099ea022d16SRodney W. Grimes { 2100ea022d16SRodney W. Grimes 2101ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 2102ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2103ea022d16SRodney W. Grimes dologout(0); 2104ea022d16SRodney W. Grimes /* NOTREACHED */ 2105ea022d16SRodney W. Grimes } 2106ea022d16SRodney W. Grimes 2107ea022d16SRodney W. Grimes void 2108ea022d16SRodney W. Grimes #if __STDC__ 2109ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2110ea022d16SRodney W. Grimes #else 2111ea022d16SRodney W. Grimes reply(n, fmt, va_alist) 2112ea022d16SRodney W. Grimes int n; 2113ea022d16SRodney W. Grimes char *fmt; 2114ea022d16SRodney W. Grimes va_dcl 2115ea022d16SRodney W. Grimes #endif 2116ea022d16SRodney W. Grimes { 2117ea022d16SRodney W. Grimes va_list ap; 2118ea022d16SRodney W. Grimes #if __STDC__ 2119ea022d16SRodney W. Grimes va_start(ap, fmt); 2120ea022d16SRodney W. Grimes #else 2121ea022d16SRodney W. Grimes va_start(ap); 2122ea022d16SRodney W. Grimes #endif 2123ea022d16SRodney W. Grimes (void)printf("%d ", n); 2124ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2125ea022d16SRodney W. Grimes (void)printf("\r\n"); 2126ea022d16SRodney W. Grimes (void)fflush(stdout); 2127618b0bbaSMark Murray if (ftpdebug) { 2128ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 2129ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2130ea022d16SRodney W. Grimes } 2131ea022d16SRodney W. Grimes } 2132ea022d16SRodney W. Grimes 2133ea022d16SRodney W. Grimes void 2134ea022d16SRodney W. Grimes #if __STDC__ 2135ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2136ea022d16SRodney W. Grimes #else 2137ea022d16SRodney W. Grimes lreply(n, fmt, va_alist) 2138ea022d16SRodney W. Grimes int n; 2139ea022d16SRodney W. Grimes char *fmt; 2140ea022d16SRodney W. Grimes va_dcl 2141ea022d16SRodney W. Grimes #endif 2142ea022d16SRodney W. Grimes { 2143ea022d16SRodney W. Grimes va_list ap; 2144ea022d16SRodney W. Grimes #if __STDC__ 2145ea022d16SRodney W. Grimes va_start(ap, fmt); 2146ea022d16SRodney W. Grimes #else 2147ea022d16SRodney W. Grimes va_start(ap); 2148ea022d16SRodney W. Grimes #endif 2149ea022d16SRodney W. Grimes (void)printf("%d- ", n); 2150ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2151ea022d16SRodney W. Grimes (void)printf("\r\n"); 2152ea022d16SRodney W. Grimes (void)fflush(stdout); 2153618b0bbaSMark Murray if (ftpdebug) { 2154ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 2155ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2156ea022d16SRodney W. Grimes } 2157ea022d16SRodney W. Grimes } 2158ea022d16SRodney W. Grimes 2159ea022d16SRodney W. Grimes static void 2160ea022d16SRodney W. Grimes ack(s) 2161ea022d16SRodney W. Grimes char *s; 2162ea022d16SRodney W. Grimes { 2163ea022d16SRodney W. Grimes 2164ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2165ea022d16SRodney W. Grimes } 2166ea022d16SRodney W. Grimes 2167ea022d16SRodney W. Grimes void 2168ea022d16SRodney W. Grimes nack(s) 2169ea022d16SRodney W. Grimes char *s; 2170ea022d16SRodney W. Grimes { 2171ea022d16SRodney W. Grimes 2172ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2173ea022d16SRodney W. Grimes } 2174ea022d16SRodney W. Grimes 2175ea022d16SRodney W. Grimes /* ARGSUSED */ 2176ea022d16SRodney W. Grimes void 2177ea022d16SRodney W. Grimes yyerror(s) 2178ea022d16SRodney W. Grimes char *s; 2179ea022d16SRodney W. Grimes { 2180ea022d16SRodney W. Grimes char *cp; 2181ea022d16SRodney W. Grimes 21829aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2183ea022d16SRodney W. Grimes *cp = '\0'; 2184ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 2185ea022d16SRodney W. Grimes } 2186ea022d16SRodney W. Grimes 2187ea022d16SRodney W. Grimes void 2188ea022d16SRodney W. Grimes delete(name) 2189ea022d16SRodney W. Grimes char *name; 2190ea022d16SRodney W. Grimes { 2191ea022d16SRodney W. Grimes struct stat st; 2192ea022d16SRodney W. Grimes 2193ea022d16SRodney W. Grimes LOGCMD("delete", name); 2194ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 2195ea022d16SRodney W. Grimes perror_reply(550, name); 2196ea022d16SRodney W. Grimes return; 2197ea022d16SRodney W. Grimes } 2198ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 2199ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2200ea022d16SRodney W. Grimes perror_reply(550, name); 2201ea022d16SRodney W. Grimes return; 2202ea022d16SRodney W. Grimes } 2203ea022d16SRodney W. Grimes goto done; 2204ea022d16SRodney W. Grimes } 2205ea022d16SRodney W. Grimes if (unlink(name) < 0) { 2206ea022d16SRodney W. Grimes perror_reply(550, name); 2207ea022d16SRodney W. Grimes return; 2208ea022d16SRodney W. Grimes } 2209ea022d16SRodney W. Grimes done: 2210ea022d16SRodney W. Grimes ack("DELE"); 2211ea022d16SRodney W. Grimes } 2212ea022d16SRodney W. Grimes 2213ea022d16SRodney W. Grimes void 2214ea022d16SRodney W. Grimes cwd(path) 2215ea022d16SRodney W. Grimes char *path; 2216ea022d16SRodney W. Grimes { 2217ea022d16SRodney W. Grimes 2218ea022d16SRodney W. Grimes if (chdir(path) < 0) 2219ea022d16SRodney W. Grimes perror_reply(550, path); 2220ea022d16SRodney W. Grimes else 2221ea022d16SRodney W. Grimes ack("CWD"); 2222ea022d16SRodney W. Grimes } 2223ea022d16SRodney W. Grimes 2224ea022d16SRodney W. Grimes void 2225ea022d16SRodney W. Grimes makedir(name) 2226ea022d16SRodney W. Grimes char *name; 2227ea022d16SRodney W. Grimes { 2228ea022d16SRodney W. Grimes 2229ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2230ea022d16SRodney W. Grimes if (mkdir(name, 0777) < 0) 2231ea022d16SRodney W. Grimes perror_reply(550, name); 2232ea022d16SRodney W. Grimes else 2233ea022d16SRodney W. Grimes reply(257, "MKD command successful."); 2234ea022d16SRodney W. Grimes } 2235ea022d16SRodney W. Grimes 2236ea022d16SRodney W. Grimes void 2237ea022d16SRodney W. Grimes removedir(name) 2238ea022d16SRodney W. Grimes char *name; 2239ea022d16SRodney W. Grimes { 2240ea022d16SRodney W. Grimes 2241ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2242ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2243ea022d16SRodney W. Grimes perror_reply(550, name); 2244ea022d16SRodney W. Grimes else 2245ea022d16SRodney W. Grimes ack("RMD"); 2246ea022d16SRodney W. Grimes } 2247ea022d16SRodney W. Grimes 2248ea022d16SRodney W. Grimes void 2249ea022d16SRodney W. Grimes pwd() 2250ea022d16SRodney W. Grimes { 2251ea022d16SRodney W. Grimes char path[MAXPATHLEN + 1]; 2252ea022d16SRodney W. Grimes 2253ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 2254ea022d16SRodney W. Grimes reply(550, "%s.", path); 2255ea022d16SRodney W. Grimes else 2256ea022d16SRodney W. Grimes reply(257, "\"%s\" is current directory.", path); 2257ea022d16SRodney W. Grimes } 2258ea022d16SRodney W. Grimes 2259ea022d16SRodney W. Grimes char * 2260ea022d16SRodney W. Grimes renamefrom(name) 2261ea022d16SRodney W. Grimes char *name; 2262ea022d16SRodney W. Grimes { 2263ea022d16SRodney W. Grimes struct stat st; 2264ea022d16SRodney W. Grimes 2265ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 2266ea022d16SRodney W. Grimes perror_reply(550, name); 2267ea022d16SRodney W. Grimes return ((char *)0); 2268ea022d16SRodney W. Grimes } 2269ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 2270ea022d16SRodney W. Grimes return (name); 2271ea022d16SRodney W. Grimes } 2272ea022d16SRodney W. Grimes 2273ea022d16SRodney W. Grimes void 2274ea022d16SRodney W. Grimes renamecmd(from, to) 2275ea022d16SRodney W. Grimes char *from, *to; 2276ea022d16SRodney W. Grimes { 227761f891a6SPaul Traina struct stat st; 2278ea022d16SRodney W. Grimes 2279ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 228061f891a6SPaul Traina 228161f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 228261f891a6SPaul Traina reply(550, "%s: permission denied", to); 228361f891a6SPaul Traina return; 228461f891a6SPaul Traina } 228561f891a6SPaul Traina 2286ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2287ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2288ea022d16SRodney W. Grimes else 2289ea022d16SRodney W. Grimes ack("RNTO"); 2290ea022d16SRodney W. Grimes } 2291ea022d16SRodney W. Grimes 2292ea022d16SRodney W. Grimes static void 22934dd8b5abSYoshinobu Inoue dolog(who) 22944dd8b5abSYoshinobu Inoue struct sockaddr *who; 2295ea022d16SRodney W. Grimes { 22964dd8b5abSYoshinobu Inoue int error; 22974dd8b5abSYoshinobu Inoue 22984dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 2299ea022d16SRodney W. Grimes 2300ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2301ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2302ea4e54b9SDavid Nugent if (thishost != firsthost) 2303ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2304ea4e54b9SDavid Nugent remotehost, hostname); 2305ea4e54b9SDavid Nugent else 2306ea4e54b9SDavid Nugent #endif 2307ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2308ea4e54b9SDavid Nugent remotehost); 2309b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2310ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2311ea022d16SRodney W. Grimes 2312ea4e54b9SDavid Nugent if (logging) { 2313ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2314ea4e54b9SDavid Nugent if (thishost != firsthost) 2315ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 2316ea4e54b9SDavid Nugent remotehost, hostname); 2317ea4e54b9SDavid Nugent else 2318ea4e54b9SDavid Nugent #endif 23194dd8b5abSYoshinobu Inoue { 23204dd8b5abSYoshinobu Inoue char who_name[MAXHOSTNAMELEN]; 23214dd8b5abSYoshinobu Inoue 23224dd8b5abSYoshinobu Inoue error = getnameinfo(who, who->sa_len, 23234dd8b5abSYoshinobu Inoue who_name, sizeof(who_name) - 1, 23244dd8b5abSYoshinobu Inoue NULL, 0, 23254dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 2326f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 23274dd8b5abSYoshinobu Inoue error == 0 ? who_name : ""); 23284dd8b5abSYoshinobu Inoue } 2329ea022d16SRodney W. Grimes } 2330ea4e54b9SDavid Nugent } 2331ea022d16SRodney W. Grimes 2332ea022d16SRodney W. Grimes /* 2333ea022d16SRodney W. Grimes * Record logout in wtmp file 2334ea022d16SRodney W. Grimes * and exit with supplied status. 2335ea022d16SRodney W. Grimes */ 2336ea022d16SRodney W. Grimes void 2337ea022d16SRodney W. Grimes dologout(status) 2338ea022d16SRodney W. Grimes int status; 2339ea022d16SRodney W. Grimes { 23400b4df2eeSDavid Greenman /* 23410b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 23420b4df2eeSDavid Greenman * back to the main program loop. 23430b4df2eeSDavid Greenman */ 23440b4df2eeSDavid Greenman transflag = 0; 2345ea022d16SRodney W. Grimes 2346ea022d16SRodney W. Grimes if (logged_in) { 2347ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 2348986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 2349ea022d16SRodney W. Grimes } 2350ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2351ea022d16SRodney W. Grimes _exit(status); 2352ea022d16SRodney W. Grimes } 2353ea022d16SRodney W. Grimes 2354ea022d16SRodney W. Grimes static void 2355ea022d16SRodney W. Grimes myoob(signo) 2356ea022d16SRodney W. Grimes int signo; 2357ea022d16SRodney W. Grimes { 2358ea022d16SRodney W. Grimes char *cp; 2359ea022d16SRodney W. Grimes 2360ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2361ea022d16SRodney W. Grimes if (!transflag) 2362ea022d16SRodney W. Grimes return; 2363ea022d16SRodney W. Grimes cp = tmpline; 2364ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2365ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2366ea022d16SRodney W. Grimes dologout(0); 2367ea022d16SRodney W. Grimes } 2368ea022d16SRodney W. Grimes upper(cp); 2369ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2370ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2371ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 2372ea022d16SRodney W. Grimes reply(226, "Abort successful"); 2373ea022d16SRodney W. Grimes longjmp(urgcatch, 1); 2374ea022d16SRodney W. Grimes } 2375ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 23769db4bbf3SMichael Haro tmpline[0] = '\0'; 2377ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 2378ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 2379ea022d16SRodney W. Grimes byte_count, file_size); 2380ea022d16SRodney W. Grimes else 2381ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 2382ea022d16SRodney W. Grimes } 2383ea022d16SRodney W. Grimes } 2384ea022d16SRodney W. Grimes 2385ea022d16SRodney W. Grimes /* 2386ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2387ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2388ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2389ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2390ea022d16SRodney W. Grimes */ 2391ea022d16SRodney W. Grimes void 2392ea022d16SRodney W. Grimes passive() 2393ea022d16SRodney W. Grimes { 2394dacc9752SPaul Traina int len; 2395ea022d16SRodney W. Grimes char *p, *a; 2396ea022d16SRodney W. Grimes 239761f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 239861f891a6SPaul Traina close(pdata); 239961f891a6SPaul Traina 24004dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2401ea022d16SRodney W. Grimes if (pdata < 0) { 2402ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2403ea022d16SRodney W. Grimes return; 2404ea022d16SRodney W. Grimes } 24054c450ad7SPaul Traina 24064c450ad7SPaul Traina (void) seteuid((uid_t)0); 240761f891a6SPaul Traina 2408dacc9752SPaul Traina #ifdef IP_PORTRANGE 24094dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 2410dacc9752SPaul Traina int on = restricted_data_ports ? IP_PORTRANGE_HIGH 2411dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2412dacc9752SPaul Traina 241340e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2414dacc9752SPaul Traina (char *)&on, sizeof(on)) < 0) 24154c450ad7SPaul Traina goto pasv_error; 24164c450ad7SPaul Traina } 2417dacc9752SPaul Traina #endif 24182db39860SNick Sayer #ifdef IPV6_PORTRANGE 24192db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 24202db39860SNick Sayer int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 24212db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 24222db39860SNick Sayer 24232db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 24242db39860SNick Sayer (char *)&on, sizeof(on)) < 0) 24252db39860SNick Sayer goto pasv_error; 24262db39860SNick Sayer } 24272db39860SNick Sayer #endif 242840e9d39eSPeter Wemm 2429ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 24304dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 24314dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2432ea022d16SRodney W. Grimes goto pasv_error; 243361f891a6SPaul Traina 2434ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 24354c450ad7SPaul Traina 2436ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2437ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2438ea022d16SRodney W. Grimes goto pasv_error; 2439ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2440ea022d16SRodney W. Grimes goto pasv_error; 24414dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 24424dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 24434dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 24444dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 24454dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 24464dd8b5abSYoshinobu Inoue else 24474dd8b5abSYoshinobu Inoue goto pasv_error; 24484dd8b5abSYoshinobu Inoue 24494dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2450ea022d16SRodney W. Grimes 2451ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2452ea022d16SRodney W. Grimes 2453ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2454ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2455ea022d16SRodney W. Grimes return; 2456ea022d16SRodney W. Grimes 2457ea022d16SRodney W. Grimes pasv_error: 245861f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2459ea022d16SRodney W. Grimes (void) close(pdata); 2460ea022d16SRodney W. Grimes pdata = -1; 2461ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2462ea022d16SRodney W. Grimes return; 2463ea022d16SRodney W. Grimes } 2464ea022d16SRodney W. Grimes 2465ea022d16SRodney W. Grimes /* 24664dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 24674dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 24684dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 24694dd8b5abSYoshinobu Inoue */ 24704dd8b5abSYoshinobu Inoue 24714dd8b5abSYoshinobu Inoue void 24724dd8b5abSYoshinobu Inoue long_passive(cmd, pf) 24734dd8b5abSYoshinobu Inoue char *cmd; 24744dd8b5abSYoshinobu Inoue int pf; 24754dd8b5abSYoshinobu Inoue { 24764dd8b5abSYoshinobu Inoue int len; 24774dd8b5abSYoshinobu Inoue char *p, *a; 24784dd8b5abSYoshinobu Inoue 24794dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 24804dd8b5abSYoshinobu Inoue close(pdata); 24814dd8b5abSYoshinobu Inoue 24824dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 24834dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 24844dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 24854dd8b5abSYoshinobu Inoue case AF_INET: 24864dd8b5abSYoshinobu Inoue pf = 1; 24874dd8b5abSYoshinobu Inoue break; 24884dd8b5abSYoshinobu Inoue case AF_INET6: 24894dd8b5abSYoshinobu Inoue pf = 2; 24904dd8b5abSYoshinobu Inoue break; 24914dd8b5abSYoshinobu Inoue default: 24924dd8b5abSYoshinobu Inoue pf = 0; 24934dd8b5abSYoshinobu Inoue break; 24944dd8b5abSYoshinobu Inoue } 24954dd8b5abSYoshinobu Inoue /* 24964dd8b5abSYoshinobu Inoue * XXX 24974dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 24984dd8b5abSYoshinobu Inoue */ 24994dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 25004dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 25014dd8b5abSYoshinobu Inoue "use (%d)", pf); 25024dd8b5abSYoshinobu Inoue } else 25034dd8b5abSYoshinobu Inoue reply(501, "Network protocol mismatch"); /*XXX*/ 25044dd8b5abSYoshinobu Inoue 25054dd8b5abSYoshinobu Inoue return; 25064dd8b5abSYoshinobu Inoue } 25074dd8b5abSYoshinobu Inoue } 25084dd8b5abSYoshinobu Inoue 25094dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 25104dd8b5abSYoshinobu Inoue if (pdata < 0) { 25114dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 25124dd8b5abSYoshinobu Inoue return; 25134dd8b5abSYoshinobu Inoue } 25144dd8b5abSYoshinobu Inoue 25154dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)0); 25164dd8b5abSYoshinobu Inoue 25174dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 25184dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 25194dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 25204dd8b5abSYoshinobu Inoue 25212db39860SNick Sayer #ifdef IP_PORTRANGE 25222db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 25232db39860SNick Sayer int on = restricted_data_ports ? IP_PORTRANGE_HIGH 25242db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 25252db39860SNick Sayer 25262db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 25272db39860SNick Sayer (char *)&on, sizeof(on)) < 0) 25282db39860SNick Sayer goto pasv_error; 25292db39860SNick Sayer } 25302db39860SNick Sayer #endif 25312db39860SNick Sayer #ifdef IPV6_PORTRANGE 25322db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 25332db39860SNick Sayer int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 25342db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 25352db39860SNick Sayer 25362db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 25372db39860SNick Sayer (char *)&on, sizeof(on)) < 0) 25382db39860SNick Sayer goto pasv_error; 25392db39860SNick Sayer } 25402db39860SNick Sayer #endif 25412db39860SNick Sayer 25424dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 25434dd8b5abSYoshinobu Inoue goto pasv_error; 25444dd8b5abSYoshinobu Inoue 25454dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 25464dd8b5abSYoshinobu Inoue 25474dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 25484dd8b5abSYoshinobu Inoue goto pasv_error; 25494dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 25504dd8b5abSYoshinobu Inoue goto pasv_error; 25514dd8b5abSYoshinobu Inoue 25524dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 25534dd8b5abSYoshinobu Inoue 25544dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 25554dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 25564dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 25574dd8b5abSYoshinobu Inoue case AF_INET: 25584dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 25594dd8b5abSYoshinobu Inoue v4_reply: 25604dd8b5abSYoshinobu Inoue reply(228, 25614dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 25624dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 25634dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 25644dd8b5abSYoshinobu Inoue return; 25654dd8b5abSYoshinobu Inoue case AF_INET6: 25664dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 25674dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 25684dd8b5abSYoshinobu Inoue goto v4_reply; 25694dd8b5abSYoshinobu Inoue } 25704dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 25714dd8b5abSYoshinobu Inoue reply(228, 25724dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 25734dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 25744dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 25754dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 25764dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 25774dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 25784dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 25794dd8b5abSYoshinobu Inoue return; 25804dd8b5abSYoshinobu Inoue } 25814dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 25824dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 25834dd8b5abSYoshinobu Inoue case AF_INET: 25844dd8b5abSYoshinobu Inoue case AF_INET6: 25854dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 25864dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 25874dd8b5abSYoshinobu Inoue return; 25884dd8b5abSYoshinobu Inoue } 25894dd8b5abSYoshinobu Inoue } else { 25904dd8b5abSYoshinobu Inoue /* more proper error code? */ 25914dd8b5abSYoshinobu Inoue } 25924dd8b5abSYoshinobu Inoue 25934dd8b5abSYoshinobu Inoue pasv_error: 25944dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 25954dd8b5abSYoshinobu Inoue (void) close(pdata); 25964dd8b5abSYoshinobu Inoue pdata = -1; 25974dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 25984dd8b5abSYoshinobu Inoue return; 25994dd8b5abSYoshinobu Inoue } 26004dd8b5abSYoshinobu Inoue 26014dd8b5abSYoshinobu Inoue /* 2602ea022d16SRodney W. Grimes * Generate unique name for file with basename "local". 2603ea022d16SRodney W. Grimes * The file named "local" is already known to exist. 2604ea022d16SRodney W. Grimes * Generates failure reply on error. 2605ea022d16SRodney W. Grimes */ 2606ea022d16SRodney W. Grimes static char * 2607ea022d16SRodney W. Grimes gunique(local) 2608ea022d16SRodney W. Grimes char *local; 2609ea022d16SRodney W. Grimes { 2610ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2611ea022d16SRodney W. Grimes struct stat st; 2612ea022d16SRodney W. Grimes int count; 2613ea022d16SRodney W. Grimes char *cp; 2614ea022d16SRodney W. Grimes 2615ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2616ea022d16SRodney W. Grimes if (cp) 2617ea022d16SRodney W. Grimes *cp = '\0'; 2618ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2619ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2620ea022d16SRodney W. Grimes return ((char *) 0); 2621ea022d16SRodney W. Grimes } 2622ea022d16SRodney W. Grimes if (cp) 2623ea022d16SRodney W. Grimes *cp = '/'; 2624e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2625e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2626ea022d16SRodney W. Grimes cp = new + strlen(new); 2627ea022d16SRodney W. Grimes *cp++ = '.'; 2628ea022d16SRodney W. Grimes for (count = 1; count < 100; count++) { 2629ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2630ea022d16SRodney W. Grimes if (stat(new, &st) < 0) 2631ea022d16SRodney W. Grimes return (new); 2632ea022d16SRodney W. Grimes } 2633ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2634ea022d16SRodney W. Grimes return (NULL); 2635ea022d16SRodney W. Grimes } 2636ea022d16SRodney W. Grimes 2637ea022d16SRodney W. Grimes /* 2638ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2639ea022d16SRodney W. Grimes */ 2640ea022d16SRodney W. Grimes void 2641ea022d16SRodney W. Grimes perror_reply(code, string) 2642ea022d16SRodney W. Grimes int code; 2643ea022d16SRodney W. Grimes char *string; 2644ea022d16SRodney W. Grimes { 2645ea022d16SRodney W. Grimes 2646ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2647ea022d16SRodney W. Grimes } 2648ea022d16SRodney W. Grimes 2649ea022d16SRodney W. Grimes static char *onefile[] = { 2650ea022d16SRodney W. Grimes "", 2651ea022d16SRodney W. Grimes 0 2652ea022d16SRodney W. Grimes }; 2653ea022d16SRodney W. Grimes 2654ea022d16SRodney W. Grimes void 2655ea022d16SRodney W. Grimes send_file_list(whichf) 2656ea022d16SRodney W. Grimes char *whichf; 2657ea022d16SRodney W. Grimes { 2658ea022d16SRodney W. Grimes struct stat st; 2659ea022d16SRodney W. Grimes DIR *dirp = NULL; 2660ea022d16SRodney W. Grimes struct dirent *dir; 2661ea022d16SRodney W. Grimes FILE *dout = NULL; 2662ea022d16SRodney W. Grimes char **dirlist, *dirname; 2663ea022d16SRodney W. Grimes int simple = 0; 2664ea022d16SRodney W. Grimes int freeglob = 0; 2665ea022d16SRodney W. Grimes glob_t gl; 2666ea022d16SRodney W. Grimes 2667ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 2668ea022d16SRodney W. Grimes int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 2669ea022d16SRodney W. Grimes 2670ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 26716d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 267275dc5f1aSMike Heffner flags |= GLOB_LIMIT; 2673ea022d16SRodney W. Grimes freeglob = 1; 2674ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2675ea022d16SRodney W. Grimes reply(550, "not found"); 2676ea022d16SRodney W. Grimes goto out; 2677ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2678ea022d16SRodney W. Grimes errno = ENOENT; 2679ea022d16SRodney W. Grimes perror_reply(550, whichf); 2680ea022d16SRodney W. Grimes goto out; 2681ea022d16SRodney W. Grimes } 2682ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2683ea022d16SRodney W. Grimes } else { 2684ea022d16SRodney W. Grimes onefile[0] = whichf; 2685ea022d16SRodney W. Grimes dirlist = onefile; 2686ea022d16SRodney W. Grimes simple = 1; 2687ea022d16SRodney W. Grimes } 2688ea022d16SRodney W. Grimes 2689ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 2690ea022d16SRodney W. Grimes transflag = 0; 2691ea022d16SRodney W. Grimes goto out; 2692ea022d16SRodney W. Grimes } 26939aca17cbSMark Murray while ((dirname = *dirlist++)) { 2694ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2695ea022d16SRodney W. Grimes /* 2696ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2697ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2698ea022d16SRodney W. Grimes */ 2699ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2700ea022d16SRodney W. Grimes transflag == 0) { 2701af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2702ea022d16SRodney W. Grimes goto out; 2703ea022d16SRodney W. Grimes } 2704ea022d16SRodney W. Grimes perror_reply(550, whichf); 2705ea022d16SRodney W. Grimes if (dout != NULL) { 2706ea022d16SRodney W. Grimes (void) fclose(dout); 2707ea022d16SRodney W. Grimes transflag = 0; 2708ea022d16SRodney W. Grimes data = -1; 2709ea022d16SRodney W. Grimes pdata = -1; 2710ea022d16SRodney W. Grimes } 2711ea022d16SRodney W. Grimes goto out; 2712ea022d16SRodney W. Grimes } 2713ea022d16SRodney W. Grimes 2714ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2715ea022d16SRodney W. Grimes if (dout == NULL) { 2716ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2717ea022d16SRodney W. Grimes if (dout == NULL) 2718ea022d16SRodney W. Grimes goto out; 2719ea022d16SRodney W. Grimes transflag++; 2720ea022d16SRodney W. Grimes } 2721ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2722ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2723ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2724ea022d16SRodney W. Grimes continue; 2725ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2726ea022d16SRodney W. Grimes continue; 2727ea022d16SRodney W. Grimes 2728ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 2729ea022d16SRodney W. Grimes continue; 2730ea022d16SRodney W. Grimes 2731ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 2732ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 2733ea022d16SRodney W. Grimes 2734ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2735ea022d16SRodney W. Grimes continue; 2736ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2737ea022d16SRodney W. Grimes dir->d_namlen == 2) 2738ea022d16SRodney W. Grimes continue; 2739ea022d16SRodney W. Grimes 2740e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 2741e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 2742ea022d16SRodney W. Grimes 2743ea022d16SRodney W. Grimes /* 2744ea022d16SRodney W. Grimes * We have to do a stat to insure it's 2745ea022d16SRodney W. Grimes * not a directory or special file. 2746ea022d16SRodney W. Grimes */ 2747ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 2748ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 2749ea022d16SRodney W. Grimes if (dout == NULL) { 2750ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 2751ea022d16SRodney W. Grimes "w"); 2752ea022d16SRodney W. Grimes if (dout == NULL) 2753ea022d16SRodney W. Grimes goto out; 2754ea022d16SRodney W. Grimes transflag++; 2755ea022d16SRodney W. Grimes } 2756ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 2757ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 2758ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2759ea022d16SRodney W. Grimes else 2760ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 2761ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2762ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 2763ea022d16SRodney W. Grimes } 2764ea022d16SRodney W. Grimes } 2765ea022d16SRodney W. Grimes (void) closedir(dirp); 2766ea022d16SRodney W. Grimes } 2767ea022d16SRodney W. Grimes 2768ea022d16SRodney W. Grimes if (dout == NULL) 2769ea022d16SRodney W. Grimes reply(550, "No files found."); 2770ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 2771ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 2772ea022d16SRodney W. Grimes else 2773ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 2774ea022d16SRodney W. Grimes 2775ea022d16SRodney W. Grimes transflag = 0; 2776ea022d16SRodney W. Grimes if (dout != NULL) 2777ea022d16SRodney W. Grimes (void) fclose(dout); 2778ea022d16SRodney W. Grimes data = -1; 2779ea022d16SRodney W. Grimes pdata = -1; 2780ea022d16SRodney W. Grimes out: 2781ea022d16SRodney W. Grimes if (freeglob) { 2782ea022d16SRodney W. Grimes freeglob = 0; 2783ea022d16SRodney W. Grimes globfree(&gl); 2784ea022d16SRodney W. Grimes } 2785ea022d16SRodney W. Grimes } 2786ea022d16SRodney W. Grimes 2787cf09a206SDavid Greenman void 2788cf09a206SDavid Greenman reapchild(signo) 2789cf09a206SDavid Greenman int signo; 2790cf09a206SDavid Greenman { 2791cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 2792cf09a206SDavid Greenman } 2793cf09a206SDavid Greenman 2794b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2795ea022d16SRodney W. Grimes /* 2796ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2797ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2798ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2799ea022d16SRodney W. Grimes */ 2800ea022d16SRodney W. Grimes void 2801ea022d16SRodney W. Grimes #if __STDC__ 2802ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2803ea022d16SRodney W. Grimes #else 2804ea022d16SRodney W. Grimes setproctitle(fmt, va_alist) 2805ea022d16SRodney W. Grimes char *fmt; 2806ea022d16SRodney W. Grimes va_dcl 2807ea022d16SRodney W. Grimes #endif 2808ea022d16SRodney W. Grimes { 2809ea022d16SRodney W. Grimes int i; 2810ea022d16SRodney W. Grimes va_list ap; 2811ea022d16SRodney W. Grimes char *p, *bp, ch; 2812ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2813ea022d16SRodney W. Grimes 2814ea022d16SRodney W. Grimes #if __STDC__ 2815ea022d16SRodney W. Grimes va_start(ap, fmt); 2816ea022d16SRodney W. Grimes #else 2817ea022d16SRodney W. Grimes va_start(ap); 2818ea022d16SRodney W. Grimes #endif 2819ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2820ea022d16SRodney W. Grimes 2821ea022d16SRodney W. Grimes /* make ps print our process name */ 2822ea022d16SRodney W. Grimes p = Argv[0]; 2823ea022d16SRodney W. Grimes *p++ = '-'; 2824ea022d16SRodney W. Grimes 2825ea022d16SRodney W. Grimes i = strlen(buf); 2826ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 2827ea022d16SRodney W. Grimes i = LastArgv - p - 2; 2828ea022d16SRodney W. Grimes buf[i] = '\0'; 2829ea022d16SRodney W. Grimes } 2830ea022d16SRodney W. Grimes bp = buf; 2831ea022d16SRodney W. Grimes while (ch = *bp++) 2832ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 2833ea022d16SRodney W. Grimes *p++ = ch; 2834ea022d16SRodney W. Grimes while (p < LastArgv) 2835ea022d16SRodney W. Grimes *p++ = ' '; 2836ea022d16SRodney W. Grimes } 2837b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 28383eb568f2SGuido van Rooij 283961f891a6SPaul Traina static void 28403eb568f2SGuido van Rooij logxfer(name, size, start) 28413eb568f2SGuido van Rooij char *name; 2842e4a71114SAndrey A. Chernov off_t size; 2843e4a71114SAndrey A. Chernov time_t start; 28443eb568f2SGuido van Rooij { 28453eb568f2SGuido van Rooij char buf[1024]; 28463eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 2847158a00b2SJohn Birrell time_t now; 28483eb568f2SGuido van Rooij 28493eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 28503eb568f2SGuido van Rooij time(&now); 2851e4a71114SAndrey A. Chernov snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n", 28523eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 2853e4a71114SAndrey A. Chernov path, name, (long long)size, 2854e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 28553eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 28563eb568f2SGuido van Rooij } 28573eb568f2SGuido van Rooij } 2858