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. 135efaea4cSChristian Brueffer * 3. Neither the name of the University nor the names of its contributors 14ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 15ea022d16SRodney W. Grimes * without specific prior written permission. 16ea022d16SRodney W. Grimes * 17ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27ea022d16SRodney W. Grimes * SUCH DAMAGE. 28ea022d16SRodney W. Grimes */ 29ea022d16SRodney W. Grimes 309aca17cbSMark Murray #if 0 31ea022d16SRodney W. Grimes #ifndef lint 32ea022d16SRodney W. Grimes static char copyright[] = 33ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 34ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 35ea022d16SRodney W. Grimes #endif /* not lint */ 369aca17cbSMark Murray #endif 37ea022d16SRodney W. Grimes 38ea022d16SRodney W. Grimes #ifndef lint 39e02897faSPhilippe Charnier #if 0 40ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 419aca17cbSMark Murray #endif 42e02897faSPhilippe Charnier #endif /* not lint */ 43ea022d16SRodney W. Grimes 440c4b401fSYaroslav Tykhiy #include <sys/cdefs.h> 450c4b401fSYaroslav Tykhiy __FBSDID("$FreeBSD$"); 460c4b401fSYaroslav Tykhiy 47ea022d16SRodney W. Grimes /* 48ea022d16SRodney W. Grimes * FTP server. 49ea022d16SRodney W. Grimes */ 50ea022d16SRodney W. Grimes #include <sys/param.h> 51ea022d16SRodney W. Grimes #include <sys/ioctl.h> 529fc5823aSGarrett Wollman #include <sys/mman.h> 53eb2fc780SGarrett Wollman #include <sys/socket.h> 54eb2fc780SGarrett Wollman #include <sys/stat.h> 55eb2fc780SGarrett Wollman #include <sys/time.h> 56eb2fc780SGarrett Wollman #include <sys/wait.h> 57ea022d16SRodney W. Grimes 58ea022d16SRodney W. Grimes #include <netinet/in.h> 59ea022d16SRodney W. Grimes #include <netinet/in_systm.h> 60ea022d16SRodney W. Grimes #include <netinet/ip.h> 619fc5823aSGarrett Wollman #include <netinet/tcp.h> 62ea022d16SRodney W. Grimes 63ea022d16SRodney W. Grimes #define FTP_NAMES 64ea022d16SRodney W. Grimes #include <arpa/ftp.h> 65ea022d16SRodney W. Grimes #include <arpa/inet.h> 66ea022d16SRodney W. Grimes #include <arpa/telnet.h> 67ea022d16SRodney W. Grimes 68ea022d16SRodney W. Grimes #include <ctype.h> 69ea022d16SRodney W. Grimes #include <dirent.h> 70ea022d16SRodney W. Grimes #include <err.h> 71ea022d16SRodney W. Grimes #include <errno.h> 72ea022d16SRodney W. Grimes #include <fcntl.h> 73ea022d16SRodney W. Grimes #include <glob.h> 74ea022d16SRodney W. Grimes #include <limits.h> 75ea022d16SRodney W. Grimes #include <netdb.h> 76ea022d16SRodney W. Grimes #include <pwd.h> 7731fea7b8SDavid Nugent #include <grp.h> 7847499ecdSAndrey A. Chernov #include <opie.h> 79ea022d16SRodney W. Grimes #include <signal.h> 80a57e1ef0SYaroslav Tykhiy #include <stdint.h> 81ea022d16SRodney W. Grimes #include <stdio.h> 82ea022d16SRodney W. Grimes #include <stdlib.h> 83ea022d16SRodney W. Grimes #include <string.h> 84ea022d16SRodney W. Grimes #include <syslog.h> 85ea022d16SRodney W. Grimes #include <time.h> 86ea022d16SRodney W. Grimes #include <unistd.h> 87b63e1fe2SPeter Wemm #include <libutil.h> 88b071c689SDavid Nugent #ifdef LOGIN_CAP 89b071c689SDavid Nugent #include <login_cap.h> 90b071c689SDavid Nugent #endif 91ea022d16SRodney W. Grimes 925bc9d93dSMark Murray #ifdef USE_PAM 936c9134c0SMark Murray #include <security/pam_appl.h> 946c9134c0SMark Murray #endif 956c9134c0SMark Murray 963656f229SKurt Lidl #include "blacklist_client.h" 97ea022d16SRodney W. Grimes #include "pathnames.h" 98ea022d16SRodney W. Grimes #include "extern.h" 99ea022d16SRodney W. Grimes 100ea022d16SRodney W. Grimes #include <stdarg.h> 101ea022d16SRodney W. Grimes 102af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 103af85d782SDavid Nugent #undef main 104ea022d16SRodney W. Grimes 1054dd8b5abSYoshinobu Inoue union sockunion ctrl_addr; 1064dd8b5abSYoshinobu Inoue union sockunion data_source; 1074dd8b5abSYoshinobu Inoue union sockunion data_dest; 1084dd8b5abSYoshinobu Inoue union sockunion his_addr; 1094dd8b5abSYoshinobu Inoue union sockunion pasv_addr; 110ea022d16SRodney W. Grimes 111cf09a206SDavid Greenman int daemon_mode; 112ea022d16SRodney W. Grimes int data; 11363591ba5SYaroslav Tykhiy int dataport; 114c152df28SYaroslav Tykhiy int hostinfo = 1; /* print host-specific info in messages */ 115ea022d16SRodney W. Grimes int logged_in; 116ea022d16SRodney W. Grimes struct passwd *pw; 117ce9287fcSYaroslav Tykhiy char *homedir; 118618b0bbaSMark Murray int ftpdebug; 119ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 120ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 121ea022d16SRodney W. Grimes int logging; 1224c450ad7SPaul Traina int restricted_data_ports = 1; 123a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1245a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 1252ea42282SYaroslav Tykhiy int assumeutf8 = 0; /* Assume that server file names are in UTF-8 */ 126ea022d16SRodney W. Grimes int guest; 127a5a4544eSPaul Traina int dochroot; 128215a9f9dSYaroslav Tykhiy char *chrootdir; 1295d7e0128SYaroslav Tykhiy int dowtmp = 1; 1303eb568f2SGuido van Rooij int stats; 1313eb568f2SGuido van Rooij int statfd = -1; 132ea022d16SRodney W. Grimes int type; 133ea022d16SRodney W. Grimes int form; 134ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 135ea022d16SRodney W. Grimes int mode; 136ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 137ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 138a4b77a2aSPoul-Henning Kamp int readonly = 0; /* Server is in readonly mode. */ 139a4b77a2aSPoul-Henning Kamp int noepsv = 0; /* EPSV command is disabled. */ 14062513e76SNik Clayton int noretr = 0; /* RETR command is disabled. */ 1411cc9f0bbSSheldon Hearn int noguestretr = 0; /* RETR command is disabled for anon users. */ 142d186bb12SMatthew N. Dodd int noguestmkd = 0; /* MKD command is disabled for anon users. */ 143a117c345SYaroslav Tykhiy int noguestmod = 1; /* anon users may not modify existing files. */ 144e07d11b6SKurt Lidl int use_blacklist = 0; 14562513e76SNik Clayton 146ea022d16SRodney W. Grimes off_t file_size; 147ea022d16SRodney W. Grimes off_t byte_count; 148ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 149ea022d16SRodney W. Grimes #undef CMASK 150ea022d16SRodney W. Grimes #define CMASK 027 151ea022d16SRodney W. Grimes #endif 152ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 153ea022d16SRodney W. Grimes char tmpline[7]; 154ea4e54b9SDavid Nugent char *hostname; 155ad442344SDima Dorfman int epsvall = 0; 156ad442344SDima Dorfman 1570512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 158ea4e54b9SDavid Nugent char *ftpuser; 159ea4e54b9SDavid Nugent 160ea4e54b9SDavid Nugent static struct ftphost { 161ea4e54b9SDavid Nugent struct ftphost *next; 162f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 163ea4e54b9SDavid Nugent char *hostname; 164ea4e54b9SDavid Nugent char *anonuser; 165ea4e54b9SDavid Nugent char *statfile; 166ea4e54b9SDavid Nugent char *welcome; 167ea4e54b9SDavid Nugent char *loginmsg; 168ea4e54b9SDavid Nugent } *thishost, *firsthost; 169ea4e54b9SDavid Nugent 170ea4e54b9SDavid Nugent #endif 17104683b2cSYaroslav Tykhiy char remotehost[NI_MAXHOST]; 1723eb568f2SGuido van Rooij char *ident = NULL; 173a5a4544eSPaul Traina 17480643af0SEd Schouten static char wtmpid[20]; 175a5a4544eSPaul Traina 1765bc9d93dSMark Murray #ifdef USE_PAM 177e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*); 1785bc9d93dSMark Murray pam_handle_t *pamh = NULL; 17947499ecdSAndrey A. Chernov #endif 180fa1746c9SMark Murray 181fa1746c9SMark Murray static struct opie opiedata; 182fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 18347499ecdSAndrey A. Chernov static int pwok; 1849aca17cbSMark Murray 185125b9635SYaroslav Tykhiy char *pid_file = NULL; /* means default location to pidfile(3) */ 186105a3c98SJulian Elischer 187ea022d16SRodney W. Grimes /* 1886d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 1896d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 1906d10cb2fSJonathan Lemon */ 1916d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 1926d10cb2fSJonathan Lemon # 1936d10cb2fSJonathan Lemon 1946d10cb2fSJonathan Lemon /* 195ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 196ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 197ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 198ea022d16SRodney W. Grimes */ 199ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 200ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 201ea022d16SRodney W. Grimes 202ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 203ea022d16SRodney W. Grimes int swaitint = SWAITINT; 204ea022d16SRodney W. Grimes 205ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 206b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 207ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 208ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 209b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 210ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 211ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 212ea022d16SRodney W. Grimes 2136b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file) logcmd((cmd), (file), NULL, -1) 2146b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2) logcmd((cmd), (file1), (file2), -1) 2156b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt) logcmd((cmd), (file), NULL, (cnt)) 216ea022d16SRodney W. Grimes 2174cd51076SYaroslav Tykhiy static volatile sig_atomic_t recvurg; 2184cd51076SYaroslav Tykhiy static int transflag; /* NB: for debugging only */ 2194cd51076SYaroslav Tykhiy 2204cd51076SYaroslav Tykhiy #define STARTXFER flagxfer(1) 2214cd51076SYaroslav Tykhiy #define ENDXFER flagxfer(0) 2224cd51076SYaroslav Tykhiy 2234cd51076SYaroslav Tykhiy #define START_UNSAFE maskurg(1) 2244cd51076SYaroslav Tykhiy #define END_UNSAFE maskurg(0) 2254cd51076SYaroslav Tykhiy 2264cd51076SYaroslav Tykhiy /* It's OK to put an `else' clause after this macro. */ 2274cd51076SYaroslav Tykhiy #define CHECKOOB(action) \ 2284cd51076SYaroslav Tykhiy if (recvurg) { \ 2294cd51076SYaroslav Tykhiy recvurg = 0; \ 2304cd51076SYaroslav Tykhiy if (myoob()) { \ 2314cd51076SYaroslav Tykhiy ENDXFER; \ 2324cd51076SYaroslav Tykhiy action; \ 2334cd51076SYaroslav Tykhiy } \ 2344cd51076SYaroslav Tykhiy } 2354cd51076SYaroslav Tykhiy 236ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2372c9fd5f2SHajimu UMEMOTO static void inithosts(int); 238e4bc453cSWarner Losh static void selecthost(union sockunion *); 239ea4e54b9SDavid Nugent #endif 240e4bc453cSWarner Losh static void ack(char *); 241e4bc453cSWarner Losh static void sigurg(int); 2424cd51076SYaroslav Tykhiy static void maskurg(int); 2434cd51076SYaroslav Tykhiy static void flagxfer(int); 2444cd51076SYaroslav Tykhiy static int myoob(void); 245cefb6785SChristian S.J. Peron static int checkuser(char *, char *, int, char **, int *); 246e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *); 247e4bc453cSWarner Losh static void dolog(struct sockaddr *); 248e4bc453cSWarner Losh static void end_login(void); 249e4bc453cSWarner Losh static FILE *getdatasock(char *); 250a117c345SYaroslav Tykhiy static int guniquefd(char *, char **); 251e4bc453cSWarner Losh static void lostconn(int); 252e4bc453cSWarner Losh static void sigquit(int); 253e4bc453cSWarner Losh static int receive_data(FILE *, FILE *); 2546e4b0a55SYaroslav Tykhiy static int send_data(FILE *, FILE *, size_t, off_t, int); 255ea022d16SRodney W. Grimes static struct passwd * 256e4bc453cSWarner Losh sgetpwnam(char *); 257e4bc453cSWarner Losh static char *sgetsave(char *); 258e4bc453cSWarner Losh static void reapchild(int); 259eb5b2bb3SYaroslav Tykhiy static void appendf(char **, char *, ...) __printflike(2, 3); 2606b2dee6bSYaroslav Tykhiy static void logcmd(char *, char *, char *, off_t); 261e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t); 262e4648f05SYaroslav Tykhiy static char *doublequote(char *); 263206fe568SHajimu UMEMOTO static int *socksetup(int, char *, const char *); 264ea022d16SRodney W. Grimes 265ea022d16SRodney W. Grimes int 266e4bc453cSWarner Losh main(int argc, char *argv[], char **envp) 267ea022d16SRodney W. Grimes { 26878e3eed0SStefan Farfeleder socklen_t addrlen; 269504422faSKurt Lidl int ch, on = 1, tos, s = STDIN_FILENO; 270ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 271ea022d16SRodney W. Grimes FILE *fd; 2724dd8b5abSYoshinobu Inoue char *bindname = NULL; 27363591ba5SYaroslav Tykhiy const char *bindport = "ftp"; 2744dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2754b82fc95SYaroslav Tykhiy struct sigaction sa; 276ea022d16SRodney W. Grimes 27734d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 2784b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask); 2794b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 28034d1ba5cSAndrey A. Chernov 281b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 282ea022d16SRodney W. Grimes /* 283ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 284ea022d16SRodney W. Grimes */ 285ea022d16SRodney W. Grimes Argv = argv; 286ea022d16SRodney W. Grimes while (*envp) 287ea022d16SRodney W. Grimes envp++; 288ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 289b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 290ea022d16SRodney W. Grimes 2916c98f401SYaroslav Tykhiy /* 2926c98f401SYaroslav Tykhiy * Prevent diagnostic messages from appearing on stderr. 2936c98f401SYaroslav Tykhiy * We run as a daemon or from inetd; in both cases, there's 2946c98f401SYaroslav Tykhiy * more reason in logging to syslog. 2956c98f401SYaroslav Tykhiy */ 2966c98f401SYaroslav Tykhiy (void) freopen(_PATH_DEVNULL, "w", stderr); 2976c98f401SYaroslav Tykhiy opterr = 0; 2986c98f401SYaroslav Tykhiy 2996c98f401SYaroslav Tykhiy /* 3006c98f401SYaroslav Tykhiy * LOG_NDELAY sets up the logging connection immediately, 3016c98f401SYaroslav Tykhiy * necessary for anonymous ftp's that chroot and can't do it later. 3026c98f401SYaroslav Tykhiy */ 3036c98f401SYaroslav Tykhiy openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 3043eb568f2SGuido van Rooij 305c152df28SYaroslav Tykhiy while ((ch = getopt(argc, argv, 306e07d11b6SKurt Lidl "468a:ABdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) { 307ea022d16SRodney W. Grimes switch (ch) { 3080e063efeSYaroslav Tykhiy case '4': 309206fe568SHajimu UMEMOTO family = (family == AF_INET6) ? AF_UNSPEC : AF_INET; 3100e063efeSYaroslav Tykhiy break; 3110e063efeSYaroslav Tykhiy 3120e063efeSYaroslav Tykhiy case '6': 313206fe568SHajimu UMEMOTO family = (family == AF_INET) ? AF_UNSPEC : AF_INET6; 3140e063efeSYaroslav Tykhiy break; 3150e063efeSYaroslav Tykhiy 3162ea42282SYaroslav Tykhiy case '8': 3172ea42282SYaroslav Tykhiy assumeutf8 = 1; 3182ea42282SYaroslav Tykhiy break; 3192ea42282SYaroslav Tykhiy 3200e063efeSYaroslav Tykhiy case 'a': 3210e063efeSYaroslav Tykhiy bindname = optarg; 3220e063efeSYaroslav Tykhiy break; 3230e063efeSYaroslav Tykhiy 3240e063efeSYaroslav Tykhiy case 'A': 3250e063efeSYaroslav Tykhiy anon_only = 1; 326cf09a206SDavid Greenman break; 327cf09a206SDavid Greenman 328e07d11b6SKurt Lidl case 'B': 329e07d11b6SKurt Lidl #ifdef USE_BLACKLIST 330e07d11b6SKurt Lidl use_blacklist = 1; 331e07d11b6SKurt Lidl #else 332e07d11b6SKurt Lidl syslog(LOG_WARNING, "not compiled with USE_BLACKLIST support"); 333e07d11b6SKurt Lidl #endif 334e07d11b6SKurt Lidl break; 335e07d11b6SKurt Lidl 336ea022d16SRodney W. Grimes case 'd': 337618b0bbaSMark Murray ftpdebug++; 338ea022d16SRodney W. Grimes break; 339ea022d16SRodney W. Grimes 3400e063efeSYaroslav Tykhiy case 'D': 3410e063efeSYaroslav Tykhiy daemon_mode++; 3420e063efeSYaroslav Tykhiy break; 3430e063efeSYaroslav Tykhiy 344a4b77a2aSPoul-Henning Kamp case 'E': 345a4b77a2aSPoul-Henning Kamp noepsv = 1; 346a4b77a2aSPoul-Henning Kamp break; 347a4b77a2aSPoul-Henning Kamp 348c152df28SYaroslav Tykhiy case 'h': 349c152df28SYaroslav Tykhiy hostinfo = 0; 350c152df28SYaroslav Tykhiy break; 351c152df28SYaroslav Tykhiy 352ea022d16SRodney W. Grimes case 'l': 353ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 354ea022d16SRodney W. Grimes break; 355ea022d16SRodney W. Grimes 356a117c345SYaroslav Tykhiy case 'm': 357a117c345SYaroslav Tykhiy noguestmod = 0; 358a117c345SYaroslav Tykhiy break; 359a117c345SYaroslav Tykhiy 3600e063efeSYaroslav Tykhiy case 'M': 3610e063efeSYaroslav Tykhiy noguestmkd = 1; 3620e063efeSYaroslav Tykhiy break; 3630e063efeSYaroslav Tykhiy 3640e063efeSYaroslav Tykhiy case 'o': 3650e063efeSYaroslav Tykhiy noretr = 1; 3660e063efeSYaroslav Tykhiy break; 3670e063efeSYaroslav Tykhiy 3680e063efeSYaroslav Tykhiy case 'O': 3690e063efeSYaroslav Tykhiy noguestretr = 1; 3700e063efeSYaroslav Tykhiy break; 3710e063efeSYaroslav Tykhiy 3720e063efeSYaroslav Tykhiy case 'p': 3730e063efeSYaroslav Tykhiy pid_file = optarg; 3740e063efeSYaroslav Tykhiy break; 3750e063efeSYaroslav Tykhiy 37663591ba5SYaroslav Tykhiy case 'P': 37763591ba5SYaroslav Tykhiy bindport = optarg; 37863591ba5SYaroslav Tykhiy break; 37963591ba5SYaroslav Tykhiy 380a4b77a2aSPoul-Henning Kamp case 'r': 381a4b77a2aSPoul-Henning Kamp readonly = 1; 382a4b77a2aSPoul-Henning Kamp break; 383a4b77a2aSPoul-Henning Kamp 384a5a4544eSPaul Traina case 'R': 385a5a4544eSPaul Traina paranoid = 0; 386a5a4544eSPaul Traina break; 387a5a4544eSPaul Traina 388a5a4544eSPaul Traina case 'S': 389a5a4544eSPaul Traina stats++; 390a5a4544eSPaul Traina break; 391a5a4544eSPaul Traina 392ea022d16SRodney W. Grimes case 't': 393ea022d16SRodney W. Grimes timeout = atoi(optarg); 394ea022d16SRodney W. Grimes if (maxtimeout < timeout) 395ea022d16SRodney W. Grimes maxtimeout = timeout; 396ea022d16SRodney W. Grimes break; 397a5a4544eSPaul Traina 3980e063efeSYaroslav Tykhiy case 'T': 3990e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg); 4000e063efeSYaroslav Tykhiy if (timeout > maxtimeout) 4010e063efeSYaroslav Tykhiy timeout = maxtimeout; 402105a3c98SJulian Elischer break; 403105a3c98SJulian Elischer 404ea022d16SRodney W. Grimes case 'u': 405ea022d16SRodney W. Grimes { 406ea022d16SRodney W. Grimes long val = 0; 407ea022d16SRodney W. Grimes 408ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 409ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 4106c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "bad value for -u"); 411ea022d16SRodney W. Grimes else 412ea022d16SRodney W. Grimes defumask = val; 413ea022d16SRodney W. Grimes break; 414ea022d16SRodney W. Grimes } 4150e063efeSYaroslav Tykhiy case 'U': 4160e063efeSYaroslav Tykhiy restricted_data_ports = 0; 4175a392aecSTorsten Blum break; 418ea022d16SRodney W. Grimes 419ea022d16SRodney W. Grimes case 'v': 42093bd9dc5SYaroslav Tykhiy ftpdebug++; 421ea022d16SRodney W. Grimes break; 422ea022d16SRodney W. Grimes 4235d7e0128SYaroslav Tykhiy case 'W': 4245d7e0128SYaroslav Tykhiy dowtmp = 0; 4255d7e0128SYaroslav Tykhiy break; 4265d7e0128SYaroslav Tykhiy 427ea022d16SRodney W. Grimes default: 4286c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "unknown flag -%c ignored", optopt); 429ea022d16SRodney W. Grimes break; 430ea022d16SRodney W. Grimes } 431ea022d16SRodney W. Grimes } 432cf09a206SDavid Greenman 433*de8d85c9SEugene Grosbein /* handle filesize limit gracefully */ 434*de8d85c9SEugene Grosbein sa.sa_handler = SIG_IGN; 435*de8d85c9SEugene Grosbein (void)sigaction(SIGXFSZ, &sa, NULL); 436*de8d85c9SEugene Grosbein 437cf09a206SDavid Greenman if (daemon_mode) { 438206fe568SHajimu UMEMOTO int *ctl_sock, fd, maxfd = -1, nfds, i; 439206fe568SHajimu UMEMOTO fd_set defreadfds, readfds; 440206fe568SHajimu UMEMOTO pid_t pid; 441125b9635SYaroslav Tykhiy struct pidfh *pfh; 442125b9635SYaroslav Tykhiy 443125b9635SYaroslav Tykhiy if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) { 444125b9635SYaroslav Tykhiy if (errno == EEXIST) { 445125b9635SYaroslav Tykhiy syslog(LOG_ERR, "%s already running, pid %d", 446125b9635SYaroslav Tykhiy getprogname(), (int)pid); 447125b9635SYaroslav Tykhiy exit(1); 448125b9635SYaroslav Tykhiy } 449125b9635SYaroslav Tykhiy syslog(LOG_WARNING, "pidfile_open: %m"); 450125b9635SYaroslav Tykhiy } 451cf09a206SDavid Greenman 452cf09a206SDavid Greenman /* 453cf09a206SDavid Greenman * Detach from parent. 454cf09a206SDavid Greenman */ 455cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 456cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 457cf09a206SDavid Greenman exit(1); 458cf09a206SDavid Greenman } 459125b9635SYaroslav Tykhiy 460125b9635SYaroslav Tykhiy if (pfh != NULL && pidfile_write(pfh) == -1) 461125b9635SYaroslav Tykhiy syslog(LOG_WARNING, "pidfile_write: %m"); 462125b9635SYaroslav Tykhiy 4634b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild; 4644b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 4654dd8b5abSYoshinobu Inoue 4662c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING 4672c9fd5f2SHajimu UMEMOTO inithosts(family); 4682c9fd5f2SHajimu UMEMOTO #endif 4692c9fd5f2SHajimu UMEMOTO 470cf09a206SDavid Greenman /* 471cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 472cf09a206SDavid Greenman * listening. 473cf09a206SDavid Greenman */ 474206fe568SHajimu UMEMOTO ctl_sock = socksetup(family, bindname, bindport); 475206fe568SHajimu UMEMOTO if (ctl_sock == NULL) 476cf09a206SDavid Greenman exit(1); 477206fe568SHajimu UMEMOTO 478206fe568SHajimu UMEMOTO FD_ZERO(&defreadfds); 479206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++) { 480206fe568SHajimu UMEMOTO FD_SET(ctl_sock[i], &defreadfds); 481206fe568SHajimu UMEMOTO if (listen(ctl_sock[i], 32) < 0) { 482cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 483cf09a206SDavid Greenman exit(1); 484cf09a206SDavid Greenman } 485206fe568SHajimu UMEMOTO if (maxfd < ctl_sock[i]) 486206fe568SHajimu UMEMOTO maxfd = ctl_sock[i]; 487206fe568SHajimu UMEMOTO } 488206fe568SHajimu UMEMOTO 489cf09a206SDavid Greenman /* 490cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 491cf09a206SDavid Greenman * children to handle them. 492cf09a206SDavid Greenman */ 493cf09a206SDavid Greenman while (1) { 494206fe568SHajimu UMEMOTO FD_COPY(&defreadfds, &readfds); 495206fe568SHajimu UMEMOTO nfds = select(maxfd + 1, &readfds, NULL, NULL, 0); 496206fe568SHajimu UMEMOTO if (nfds <= 0) { 497206fe568SHajimu UMEMOTO if (nfds < 0 && errno != EINTR) 498206fe568SHajimu UMEMOTO syslog(LOG_WARNING, "select: %m"); 499206fe568SHajimu UMEMOTO continue; 500206fe568SHajimu UMEMOTO } 501206fe568SHajimu UMEMOTO 502206fe568SHajimu UMEMOTO pid = -1; 503206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++) 504206fe568SHajimu UMEMOTO if (FD_ISSET(ctl_sock[i], &readfds)) { 505206fe568SHajimu UMEMOTO addrlen = sizeof(his_addr); 506206fe568SHajimu UMEMOTO fd = accept(ctl_sock[i], 507206fe568SHajimu UMEMOTO (struct sockaddr *)&his_addr, 508206fe568SHajimu UMEMOTO &addrlen); 509a599a64aSYaroslav Tykhiy if (fd == -1) { 510a599a64aSYaroslav Tykhiy syslog(LOG_WARNING, 511a599a64aSYaroslav Tykhiy "accept: %m"); 512a599a64aSYaroslav Tykhiy continue; 513cf09a206SDavid Greenman } 514a599a64aSYaroslav Tykhiy switch (pid = fork()) { 515a599a64aSYaroslav Tykhiy case 0: 5168eb0508fSYaroslav Tykhiy /* child */ 517504422faSKurt Lidl (void) dup2(fd, s); 518504422faSKurt Lidl (void) dup2(fd, STDOUT_FILENO); 519a599a64aSYaroslav Tykhiy (void) close(fd); 520a599a64aSYaroslav Tykhiy for (i = 1; i <= *ctl_sock; i++) 5218eb0508fSYaroslav Tykhiy close(ctl_sock[i]); 522125b9635SYaroslav Tykhiy if (pfh != NULL) 523125b9635SYaroslav Tykhiy pidfile_close(pfh); 524a599a64aSYaroslav Tykhiy goto gotchild; 525a599a64aSYaroslav Tykhiy case -1: 526a599a64aSYaroslav Tykhiy syslog(LOG_WARNING, "fork: %m"); 527a599a64aSYaroslav Tykhiy /* FALLTHROUGH */ 528a599a64aSYaroslav Tykhiy default: 529a599a64aSYaroslav Tykhiy close(fd); 530a599a64aSYaroslav Tykhiy } 531206fe568SHajimu UMEMOTO } 532125b9635SYaroslav Tykhiy } 533cf09a206SDavid Greenman } else { 534cf09a206SDavid Greenman addrlen = sizeof(his_addr); 535504422faSKurt Lidl if (getpeername(s, (struct sockaddr *)&his_addr, &addrlen) < 0) { 536cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 537cf09a206SDavid Greenman exit(1); 538cf09a206SDavid Greenman } 5392c9fd5f2SHajimu UMEMOTO 5402c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING 5412c9fd5f2SHajimu UMEMOTO if (his_addr.su_family == AF_INET6 && 5422c9fd5f2SHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr)) 5432c9fd5f2SHajimu UMEMOTO family = AF_INET; 5442c9fd5f2SHajimu UMEMOTO else 5452c9fd5f2SHajimu UMEMOTO family = his_addr.su_family; 5462c9fd5f2SHajimu UMEMOTO inithosts(family); 5472c9fd5f2SHajimu UMEMOTO #endif 548cf09a206SDavid Greenman } 549cf09a206SDavid Greenman 550a599a64aSYaroslav Tykhiy gotchild: 5514b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL; 5524b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 5534b82fc95SYaroslav Tykhiy 5544b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg; 5554b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 5564b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL); 5574b82fc95SYaroslav Tykhiy 5584b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */ 5594b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 5604b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit; 5614b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL); 5624b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL); 5634b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL); 5644b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL); 5654b82fc95SYaroslav Tykhiy 5664b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn; 5674b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL); 568ea022d16SRodney W. Grimes 569cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 570504422faSKurt Lidl if (getsockname(s, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 571cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 572cf09a206SDavid Greenman exit(1); 573cf09a206SDavid Greenman } 57463591ba5SYaroslav Tykhiy dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */ 575ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 576ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5774dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 578ea4e54b9SDavid Nugent #endif 579cf09a206SDavid Greenman #ifdef IP_TOS 5804dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5814dd8b5abSYoshinobu Inoue { 582cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 583504422faSKurt Lidl if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 584406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 5854dd8b5abSYoshinobu Inoue } 586cf09a206SDavid Greenman #endif 587dadb9fb3SDavid Greenman /* 588dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 589dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 590dadb9fb3SDavid Greenman */ 591504422faSKurt Lidl if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 592406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 593dadb9fb3SDavid Greenman 5944dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 595cf09a206SDavid Greenman 59680643af0SEd Schouten (void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid()); 597a5a4544eSPaul Traina 598ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 599ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 600504422faSKurt Lidl if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 601406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 602ea022d16SRodney W. Grimes #endif 603ea022d16SRodney W. Grimes 604ea022d16SRodney W. Grimes #ifdef F_SETOWN 605504422faSKurt Lidl if (fcntl(s, F_SETOWN, getpid()) == -1) 606ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 607ea022d16SRodney W. Grimes #endif 6084dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 609ea022d16SRodney W. Grimes /* 610ea022d16SRodney W. Grimes * Set up default state 611ea022d16SRodney W. Grimes */ 612ea022d16SRodney W. Grimes data = -1; 613ea022d16SRodney W. Grimes type = TYPE_A; 614ea022d16SRodney W. Grimes form = FORM_N; 615ea022d16SRodney W. Grimes stru = STRU_F; 616ea022d16SRodney W. Grimes mode = MODE_S; 617ea022d16SRodney W. Grimes tmpline[0] = '\0'; 618ea022d16SRodney W. Grimes 619ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 620ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 621ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 622ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 623ea022d16SRodney W. Grimes *cp = '\0'; 624ea022d16SRodney W. Grimes lreply(530, "%s", line); 625ea022d16SRodney W. Grimes } 626ea022d16SRodney W. Grimes (void) fflush(stdout); 627ea022d16SRodney W. Grimes (void) fclose(fd); 628ea022d16SRodney W. Grimes reply(530, "System not available."); 629ea022d16SRodney W. Grimes exit(0); 630ea022d16SRodney W. Grimes } 631ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 63263047c6fSDavid E. O'Brien fd = fopen(thishost->welcome, "r"); 633ea4e54b9SDavid Nugent #else 63463047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPWELCOME, "r"); 635ea4e54b9SDavid Nugent #endif 63663047c6fSDavid E. O'Brien if (fd != NULL) { 637ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 638ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 639ea022d16SRodney W. Grimes *cp = '\0'; 640ea022d16SRodney W. Grimes lreply(220, "%s", line); 641ea022d16SRodney W. Grimes } 642ea022d16SRodney W. Grimes (void) fflush(stdout); 643ea022d16SRodney W. Grimes (void) fclose(fd); 644ea022d16SRodney W. Grimes /* reply(220,) must follow */ 645ea022d16SRodney W. Grimes } 646ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6470512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 648618b0bbaSMark Murray fatalerror("Ran out of memory."); 64904683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) 65004683b2cSYaroslav Tykhiy hostname[0] = '\0'; 6519e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 652ea4e54b9SDavid Nugent #endif 653c152df28SYaroslav Tykhiy if (hostinfo) 654ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 655c152df28SYaroslav Tykhiy else 656c152df28SYaroslav Tykhiy reply(220, "FTP server ready."); 657e07d11b6SKurt Lidl BLACKLIST_INIT(); 658ea022d16SRodney W. Grimes for (;;) 659ea022d16SRodney W. Grimes (void) yyparse(); 660ea022d16SRodney W. Grimes /* NOTREACHED */ 661ea022d16SRodney W. Grimes } 662ea022d16SRodney W. Grimes 663ea022d16SRodney W. Grimes static void 664e4bc453cSWarner Losh lostconn(int signo) 665ea022d16SRodney W. Grimes { 666ea022d16SRodney W. Grimes 667618b0bbaSMark Murray if (ftpdebug) 668ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 669e02897faSPhilippe Charnier dologout(1); 670ea022d16SRodney W. Grimes } 671ea022d16SRodney W. Grimes 6724b82fc95SYaroslav Tykhiy static void 673e4bc453cSWarner Losh sigquit(int signo) 6744b82fc95SYaroslav Tykhiy { 6754b82fc95SYaroslav Tykhiy 6764b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo); 6774b82fc95SYaroslav Tykhiy dologout(1); 6784b82fc95SYaroslav Tykhiy } 6794b82fc95SYaroslav Tykhiy 680ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 681ea4e54b9SDavid Nugent /* 682ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 683ea4e54b9SDavid Nugent */ 684ea4e54b9SDavid Nugent 685ea4e54b9SDavid Nugent static void 6862c9fd5f2SHajimu UMEMOTO inithosts(int family) 687ea4e54b9SDavid Nugent { 68855b54aa7SYaroslav Tykhiy int insert; 689737d08f3SYaroslav Tykhiy size_t len; 690ea4e54b9SDavid Nugent FILE *fp; 691737d08f3SYaroslav Tykhiy char *cp, *mp, *line; 692737d08f3SYaroslav Tykhiy char *hostname; 69355b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 694ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 6954dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 696ea4e54b9SDavid Nugent 697ea4e54b9SDavid Nugent /* 698ea4e54b9SDavid Nugent * Fill in the default host information 699ea4e54b9SDavid Nugent */ 700737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 701618b0bbaSMark Murray fatalerror("Ran out of memory."); 70204683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) 703737d08f3SYaroslav Tykhiy hostname[0] = '\0'; 704737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0'; 705737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 706737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 707737d08f3SYaroslav Tykhiy hrp->hostname = hostname; 708f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 7094dd8b5abSYoshinobu Inoue 7104dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 7112c9fd5f2SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE; 7122c9fd5f2SHajimu UMEMOTO hints.ai_family = family; 7132c9fd5f2SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM; 714b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 715f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 716ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 717ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 718ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 719ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 720ea4e54b9SDavid Nugent hrp->next = NULL; 721ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 722ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 723ec009cf0SYaroslav Tykhiy int addrsize, gothost; 7244dd8b5abSYoshinobu Inoue void *addr; 7254dd8b5abSYoshinobu Inoue struct hostent *hp; 7264dd8b5abSYoshinobu Inoue 727737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) { 7284dd8b5abSYoshinobu Inoue int i, hp_error; 729ea4e54b9SDavid Nugent 730737d08f3SYaroslav Tykhiy /* skip comments */ 731737d08f3SYaroslav Tykhiy if (line[0] == '#') 732ea4e54b9SDavid Nugent continue; 733737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 734737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 735737d08f3SYaroslav Tykhiy mp = NULL; 736737d08f3SYaroslav Tykhiy } else { 737737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 738737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 739737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 740737d08f3SYaroslav Tykhiy mp[len] = '\0'; 741737d08f3SYaroslav Tykhiy line = mp; 742ea4e54b9SDavid Nugent } 743ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 744737d08f3SYaroslav Tykhiy /* skip empty lines */ 745737d08f3SYaroslav Tykhiy if (cp == NULL) 746737d08f3SYaroslav Tykhiy goto nextline; 74755b54aa7SYaroslav Tykhiy vhost = cp; 74855b54aa7SYaroslav Tykhiy 74955b54aa7SYaroslav Tykhiy /* set defaults */ 75055b54aa7SYaroslav Tykhiy anonuser = "ftp"; 75155b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE; 75255b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME; 75355b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG; 75455b54aa7SYaroslav Tykhiy 75555b54aa7SYaroslav Tykhiy /* 75655b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info 75755b54aa7SYaroslav Tykhiy * for all the addresses associated with 75855b54aa7SYaroslav Tykhiy * the virtual host name. 75955b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special: 76055b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed 76155b54aa7SYaroslav Tykhiy * later, after we know its canonical form. 76255b54aa7SYaroslav Tykhiy */ 76355b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 76455b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp))) 76555b54aa7SYaroslav Tykhiy switch (i) { 76655b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */ 76755b54aa7SYaroslav Tykhiy anonuser = cp; 76855b54aa7SYaroslav Tykhiy break; 76955b54aa7SYaroslav Tykhiy case 2: /* statistics file */ 77055b54aa7SYaroslav Tykhiy statfile = cp; 77155b54aa7SYaroslav Tykhiy break; 77255b54aa7SYaroslav Tykhiy case 3: /* welcome message */ 77355b54aa7SYaroslav Tykhiy welcome = cp; 77455b54aa7SYaroslav Tykhiy break; 77555b54aa7SYaroslav Tykhiy case 4: /* login message */ 77655b54aa7SYaroslav Tykhiy loginmsg = cp; 77755b54aa7SYaroslav Tykhiy break; 77855b54aa7SYaroslav Tykhiy default: /* programming error */ 77955b54aa7SYaroslav Tykhiy abort(); 78055b54aa7SYaroslav Tykhiy /* NOTREACHED */ 78155b54aa7SYaroslav Tykhiy } 7824dd8b5abSYoshinobu Inoue 7834dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 7842c9fd5f2SHajimu UMEMOTO hints.ai_family = family; 7852c9fd5f2SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM; 786b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 787737d08f3SYaroslav Tykhiy goto nextline; 7884dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 789b535a9bfSDavid Nugent ai = ai->ai_next) { 7904dd8b5abSYoshinobu Inoue 791b535a9bfSDavid Nugent gothost = 0; 792ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 793f38c6cadSYoshinobu Inoue struct addrinfo *hi; 794f38c6cadSYoshinobu Inoue 795f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 796f38c6cadSYoshinobu Inoue hi = hi->ai_next) 797f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 798f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7994dd8b5abSYoshinobu Inoue ai->ai_addr, 800b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 801b535a9bfSDavid Nugent gothost++; 802b535a9bfSDavid Nugent break; 803b535a9bfSDavid Nugent } 804b535a9bfSDavid Nugent if (gothost) 805ea4e54b9SDavid Nugent break; 806ea4e54b9SDavid Nugent } 807ea4e54b9SDavid Nugent if (hrp == NULL) { 808ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 809737d08f3SYaroslav Tykhiy goto nextline; 810b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 81155b54aa7SYaroslav Tykhiy insert = 1; 812f2fe752dSYaroslav Tykhiy } else { 8131f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res) 814b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 815f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */ 816f2fe752dSYaroslav Tykhiy } 817f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 818f38c6cadSYoshinobu Inoue 819ea4e54b9SDavid Nugent /* 820ea4e54b9SDavid Nugent * determine hostname to use. 8214dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 822ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 823ea4e54b9SDavid Nugent */ 8244dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 825f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 8264dd8b5abSYoshinobu Inoue case AF_INET: 8274b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 8284b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr); 8294dd8b5abSYoshinobu Inoue break; 8304dd8b5abSYoshinobu Inoue case AF_INET6: 8314b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 8324b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr); 8334dd8b5abSYoshinobu Inoue break; 8344dd8b5abSYoshinobu Inoue default: 8354dd8b5abSYoshinobu Inoue /* should not reach here */ 836f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 837f2fe752dSYaroslav Tykhiy if (insert) 838f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/ 839f2fe752dSYaroslav Tykhiy else 840f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/ 841737d08f3SYaroslav Tykhiy goto nextline; 8424dd8b5abSYoshinobu Inoue /* NOTREACHED */ 8434dd8b5abSYoshinobu Inoue } 84457d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize, 845f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 8464dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 84755b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) { 848ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 84955b54aa7SYaroslav Tykhiy vhost = hp->h_name; 850ea4e54b9SDavid Nugent else { 851ea4e54b9SDavid Nugent i = 0; 852ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 85355b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0) 854ea4e54b9SDavid Nugent ++i; 855ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 85655b54aa7SYaroslav Tykhiy vhost = hp->h_name; 857ea4e54b9SDavid Nugent } 858ea4e54b9SDavid Nugent } 859ea4e54b9SDavid Nugent } 860b1d8d5cdSYaroslav Tykhiy if (hrp->hostname && 861b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) { 862b1d8d5cdSYaroslav Tykhiy free(hrp->hostname); 863b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 864b1d8d5cdSYaroslav Tykhiy } 865b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL && 866f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) { 867f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 868f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */ 869f2fe752dSYaroslav Tykhiy if (hp) 870f2fe752dSYaroslav Tykhiy freehostent(hp); 87155b54aa7SYaroslav Tykhiy goto nextline; 872f2fe752dSYaroslav Tykhiy } 87355b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser; 87455b54aa7SYaroslav Tykhiy hrp->statfile = statfile; 87555b54aa7SYaroslav Tykhiy hrp->welcome = welcome; 87655b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg; 87755b54aa7SYaroslav Tykhiy if (insert) { 87855b54aa7SYaroslav Tykhiy hrp->next = NULL; 87955b54aa7SYaroslav Tykhiy lhrp->next = hrp; 88055b54aa7SYaroslav Tykhiy lhrp = hrp; 88155b54aa7SYaroslav Tykhiy } 882233c0f66SYaroslav Tykhiy if (hp) 8834dd8b5abSYoshinobu Inoue freehostent(hp); 8844dd8b5abSYoshinobu Inoue } 885737d08f3SYaroslav Tykhiy nextline: 886737d08f3SYaroslav Tykhiy if (mp) 887737d08f3SYaroslav Tykhiy free(mp); 888ea4e54b9SDavid Nugent } 889ea4e54b9SDavid Nugent (void) fclose(fp); 890ea4e54b9SDavid Nugent } 891ea4e54b9SDavid Nugent } 892ea4e54b9SDavid Nugent 893ea4e54b9SDavid Nugent static void 894e4bc453cSWarner Losh selecthost(union sockunion *su) 895ea4e54b9SDavid Nugent { 896ea4e54b9SDavid Nugent struct ftphost *hrp; 8974dd8b5abSYoshinobu Inoue u_int16_t port; 8984dd8b5abSYoshinobu Inoue #ifdef INET6 8994dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 9004dd8b5abSYoshinobu Inoue #endif 901f38c6cadSYoshinobu Inoue struct addrinfo *hi; 9024dd8b5abSYoshinobu Inoue 9034dd8b5abSYoshinobu Inoue #ifdef INET6 9044dd8b5abSYoshinobu Inoue /* 9054dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 9064dd8b5abSYoshinobu Inoue * specified in rfc2373. 9074dd8b5abSYoshinobu Inoue */ 9084dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 9094dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 9104dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 9114dd8b5abSYoshinobu Inoue #endif 912ea4e54b9SDavid Nugent 913ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 9144dd8b5abSYoshinobu Inoue port = su->su_port; 9154dd8b5abSYoshinobu Inoue su->su_port = 0; 916ea4e54b9SDavid Nugent while (hrp != NULL) { 917b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 918f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 919ea4e54b9SDavid Nugent thishost = hrp; 920ebd83647SYaroslav Tykhiy goto found; 921ea4e54b9SDavid Nugent } 9224dd8b5abSYoshinobu Inoue #ifdef INET6 9234dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 924f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 9254dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 926f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 9274dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 9284dd8b5abSYoshinobu Inoue thishost = hrp; 929ebd83647SYaroslav Tykhiy goto found; 9304dd8b5abSYoshinobu Inoue } 9314dd8b5abSYoshinobu Inoue #endif 932f38c6cadSYoshinobu Inoue } 933ea4e54b9SDavid Nugent hrp = hrp->next; 934ea4e54b9SDavid Nugent } 935ebd83647SYaroslav Tykhiy found: 9364dd8b5abSYoshinobu Inoue su->su_port = port; 937ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 938ea4e54b9SDavid Nugent hostname = thishost->hostname; 939ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 940ea4e54b9SDavid Nugent } 941ea4e54b9SDavid Nugent #endif 942ea4e54b9SDavid Nugent 943ea022d16SRodney W. Grimes /* 944ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 945ea022d16SRodney W. Grimes */ 946ea022d16SRodney W. Grimes static char * 947e4bc453cSWarner Losh sgetsave(char *s) 948ea022d16SRodney W. Grimes { 949e3765043SYaroslav Tykhiy char *new = malloc(strlen(s) + 1); 950ea022d16SRodney W. Grimes 951ea022d16SRodney W. Grimes if (new == NULL) { 95202c97492SYaroslav Tykhiy reply(421, "Ran out of memory."); 953ea022d16SRodney W. Grimes dologout(1); 954ea022d16SRodney W. Grimes /* NOTREACHED */ 955ea022d16SRodney W. Grimes } 956ea022d16SRodney W. Grimes (void) strcpy(new, s); 957ea022d16SRodney W. Grimes return (new); 958ea022d16SRodney W. Grimes } 959ea022d16SRodney W. Grimes 960ea022d16SRodney W. Grimes /* 961ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 962ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 963ea022d16SRodney W. Grimes * (e.g., globbing). 964c29b9b47SYaroslav Tykhiy * NB: The data returned by sgetpwnam() will remain valid until 965c29b9b47SYaroslav Tykhiy * the next call to this function. Its difference from getpwnam() 966c29b9b47SYaroslav Tykhiy * is that sgetpwnam() is known to be called from ftpd code only. 967ea022d16SRodney W. Grimes */ 968ea022d16SRodney W. Grimes static struct passwd * 969e4bc453cSWarner Losh sgetpwnam(char *name) 970ea022d16SRodney W. Grimes { 971ea022d16SRodney W. Grimes static struct passwd save; 972ea022d16SRodney W. Grimes struct passwd *p; 973ea022d16SRodney W. Grimes 974ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 975ea022d16SRodney W. Grimes return (p); 976ea022d16SRodney W. Grimes if (save.pw_name) { 977ea022d16SRodney W. Grimes free(save.pw_name); 978ea022d16SRodney W. Grimes free(save.pw_passwd); 97903d34cccSChristian Brueffer free(save.pw_class); 980ea022d16SRodney W. Grimes free(save.pw_gecos); 981ea022d16SRodney W. Grimes free(save.pw_dir); 982ea022d16SRodney W. Grimes free(save.pw_shell); 983ea022d16SRodney W. Grimes } 984ea022d16SRodney W. Grimes save = *p; 985ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 986ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 98703d34cccSChristian Brueffer save.pw_class = sgetsave(p->pw_class); 988ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 989ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 990ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 991ea022d16SRodney W. Grimes return (&save); 992ea022d16SRodney W. Grimes } 993ea022d16SRodney W. Grimes 994ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 995ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 99690906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 997ea022d16SRodney W. Grimes 998ea022d16SRodney W. Grimes /* 999ea022d16SRodney W. Grimes * USER command. 1000ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 1001ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 1002ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 1003ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 1004ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 1005ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 1006ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 1007ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 1008ea022d16SRodney W. Grimes */ 1009ea022d16SRodney W. Grimes void 1010e4bc453cSWarner Losh user(char *name) 1011ea022d16SRodney W. Grimes { 1012cefb6785SChristian S.J. Peron int ecode; 1013ea022d16SRodney W. Grimes char *cp, *shell; 1014ea022d16SRodney W. Grimes 1015ea022d16SRodney W. Grimes if (logged_in) { 1016ea022d16SRodney W. Grimes if (guest) { 1017ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 1018ea022d16SRodney W. Grimes return; 1019a5a4544eSPaul Traina } else if (dochroot) { 1020a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 1021a5a4544eSPaul Traina return; 1022ea022d16SRodney W. Grimes } 1023ea022d16SRodney W. Grimes end_login(); 1024ea022d16SRodney W. Grimes } 1025ea022d16SRodney W. Grimes 1026ea022d16SRodney W. Grimes guest = 0; 102763047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING 102863047c6fSDavid E. O'Brien pw = sgetpwnam(thishost->anonuser); 102963047c6fSDavid E. O'Brien #else 103063047c6fSDavid E. O'Brien pw = sgetpwnam("ftp"); 103163047c6fSDavid E. O'Brien #endif 1032ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 1033cefb6785SChristian S.J. Peron if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) || 1034cefb6785SChristian S.J. Peron (ecode != 0 && ecode != ENOENT)) 1035cefb6785SChristian S.J. Peron reply(530, "User %s access denied.", name); 1036cefb6785SChristian S.J. Peron else if (checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL, &ecode) || 1037cefb6785SChristian S.J. Peron (ecode != 0 && ecode != ENOENT)) 1038ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 103963047c6fSDavid E. O'Brien else if (pw != NULL) { 1040ea022d16SRodney W. Grimes guest = 1; 1041ea022d16SRodney W. Grimes askpasswd = 1; 1042ea022d16SRodney W. Grimes reply(331, 10432c60c54cSPaul Traina "Guest login ok, send your email address as password."); 1044ea022d16SRodney W. Grimes } else 1045ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 1046ea022d16SRodney W. Grimes if (!askpasswd && logging) 1047ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1048ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 1049ea022d16SRodney W. Grimes return; 1050ea022d16SRodney W. Grimes } 10515a392aecSTorsten Blum if (anon_only != 0) { 10525a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 10535a392aecSTorsten Blum return; 10545a392aecSTorsten Blum } 10555a392aecSTorsten Blum 10569aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 1057ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 1058ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 1059c433c9daSPhilippe Charnier setusershell(); 1060ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 1061ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 1062ea022d16SRodney W. Grimes break; 1063ea022d16SRodney W. Grimes endusershell(); 1064ea022d16SRodney W. Grimes 1065cefb6785SChristian S.J. Peron if (cp == NULL || 1066cefb6785SChristian S.J. Peron (checkuser(_PATH_FTPUSERS, name, 1, NULL, &ecode) || 1067cefb6785SChristian S.J. Peron (ecode != 0 && ecode != ENOENT))) { 1068ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1069ea022d16SRodney W. Grimes if (logging) 1070ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1071ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 1072ea022d16SRodney W. Grimes remotehost, name); 10737e295315SYaroslav Tykhiy pw = NULL; 1074ea022d16SRodney W. Grimes return; 1075ea022d16SRodney W. Grimes } 1076ea022d16SRodney W. Grimes } 1077ea022d16SRodney W. Grimes if (logging) 1078ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 107947499ecdSAndrey A. Chernov 108047499ecdSAndrey A. Chernov pwok = 0; 1081fa1746c9SMark Murray #ifdef USE_PAM 1082fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1083726040deSGuido van Rooij #endif 108447499ecdSAndrey A. Chernov if (opiechallenge(&opiedata, name, opieprompt) == 0) { 108547499ecdSAndrey A. Chernov pwok = (pw != NULL) && 108647499ecdSAndrey A. Chernov opieaccessfile(remotehost) && 108747499ecdSAndrey A. Chernov opiealways(pw->pw_dir); 108847499ecdSAndrey A. Chernov reply(331, "Response to %s %s for %s.", 108947499ecdSAndrey A. Chernov opieprompt, pwok ? "requested" : "required", name); 109047499ecdSAndrey A. Chernov } else { 109147499ecdSAndrey A. Chernov pwok = 1; 109247499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name); 109347499ecdSAndrey A. Chernov } 1094ea022d16SRodney W. Grimes askpasswd = 1; 1095ea022d16SRodney W. Grimes /* 1096ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 1097ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 1098ea022d16SRodney W. Grimes */ 1099ea022d16SRodney W. Grimes if (login_attempts) 1100e3765043SYaroslav Tykhiy sleep(login_attempts); 1101ea022d16SRodney W. Grimes } 1102ea022d16SRodney W. Grimes 1103ea022d16SRodney W. Grimes /* 11048657b576SYaroslav Tykhiy * Check if a user is in the file "fname", 11058657b576SYaroslav Tykhiy * return a pointer to a malloc'd string with the rest 11068657b576SYaroslav Tykhiy * of the matching line in "residue" if not NULL. 1107ea022d16SRodney W. Grimes */ 1108ea022d16SRodney W. Grimes static int 1109cefb6785SChristian S.J. Peron checkuser(char *fname, char *name, int pwset, char **residue, int *ecode) 1110ea022d16SRodney W. Grimes { 1111ea022d16SRodney W. Grimes FILE *fd; 1112ea022d16SRodney W. Grimes int found = 0; 1113737d08f3SYaroslav Tykhiy size_t len; 1114737d08f3SYaroslav Tykhiy char *line, *mp, *p; 1115ea022d16SRodney W. Grimes 1116cefb6785SChristian S.J. Peron if (ecode != NULL) 1117cefb6785SChristian S.J. Peron *ecode = 0; 1118a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 1119737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) { 1120737d08f3SYaroslav Tykhiy /* skip comments */ 1121ea022d16SRodney W. Grimes if (line[0] == '#') 1122ea022d16SRodney W. Grimes continue; 1123737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 1124737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 1125737d08f3SYaroslav Tykhiy mp = NULL; 1126737d08f3SYaroslav Tykhiy } else { 1127737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 1128737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 1129737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 1130737d08f3SYaroslav Tykhiy mp[len] = '\0'; 1131737d08f3SYaroslav Tykhiy line = mp; 1132737d08f3SYaroslav Tykhiy } 1133737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */ 1134737d08f3SYaroslav Tykhiy p = strtok(line, " \t"); 1135737d08f3SYaroslav Tykhiy /* skip empty lines */ 1136737d08f3SYaroslav Tykhiy if (p == NULL) 1137737d08f3SYaroslav Tykhiy goto nextline; 113831fea7b8SDavid Nugent /* 113931fea7b8SDavid Nugent * if first chr is '@', check group membership 114031fea7b8SDavid Nugent */ 1141737d08f3SYaroslav Tykhiy if (p[0] == '@') { 114231fea7b8SDavid Nugent int i = 0; 114331fea7b8SDavid Nugent struct group *grp; 114431fea7b8SDavid Nugent 11458657b576SYaroslav Tykhiy if (p[1] == '\0') /* single @ matches anyone */ 11468657b576SYaroslav Tykhiy found = 1; 11478657b576SYaroslav Tykhiy else { 1148737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL) 1149737d08f3SYaroslav Tykhiy goto nextline; 11507edcb936SSteve Price /* 11517edcb936SSteve Price * Check user's default group 11527edcb936SSteve Price */ 11537edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 11547edcb936SSteve Price found = 1; 11557edcb936SSteve Price /* 11567edcb936SSteve Price * Check supplementary groups 11577edcb936SSteve Price */ 115831fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 115931fea7b8SDavid Nugent found = strcmp(name, 116031fea7b8SDavid Nugent grp->gr_mem[i++]) 116131fea7b8SDavid Nugent == 0; 1162ea022d16SRodney W. Grimes } 11638657b576SYaroslav Tykhiy } 116431fea7b8SDavid Nugent /* 116531fea7b8SDavid Nugent * Otherwise, just check for username match 116631fea7b8SDavid Nugent */ 116731fea7b8SDavid Nugent else 1168737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0; 11698657b576SYaroslav Tykhiy /* 11708657b576SYaroslav Tykhiy * Save the rest of line to "residue" if matched 11718657b576SYaroslav Tykhiy */ 11728657b576SYaroslav Tykhiy if (found && residue) { 11730ba71e24SYaroslav Tykhiy if ((p = strtok(NULL, "")) != NULL) 11740ba71e24SYaroslav Tykhiy p += strspn(p, " \t"); 11750ba71e24SYaroslav Tykhiy if (p && *p) { 11768657b576SYaroslav Tykhiy if ((*residue = strdup(p)) == NULL) 11778657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 11788657b576SYaroslav Tykhiy } else 11798657b576SYaroslav Tykhiy *residue = NULL; 11808657b576SYaroslav Tykhiy } 1181737d08f3SYaroslav Tykhiy nextline: 1182737d08f3SYaroslav Tykhiy if (mp) 1183737d08f3SYaroslav Tykhiy free(mp); 1184ea022d16SRodney W. Grimes } 1185ea022d16SRodney W. Grimes (void) fclose(fd); 1186cefb6785SChristian S.J. Peron } else if (ecode != NULL) 1187cefb6785SChristian S.J. Peron *ecode = errno; 1188ea022d16SRodney W. Grimes return (found); 1189ea022d16SRodney W. Grimes } 1190ea022d16SRodney W. Grimes 1191ea022d16SRodney W. Grimes /* 1192ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1193ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1194ea022d16SRodney W. Grimes */ 1195ea022d16SRodney W. Grimes static void 1196e4bc453cSWarner Losh end_login(void) 1197ea022d16SRodney W. Grimes { 11985bc9d93dSMark Murray #ifdef USE_PAM 11995bc9d93dSMark Murray int e; 12005bc9d93dSMark Murray #endif 1201ea022d16SRodney W. Grimes 120252e7ee74SYaroslav Tykhiy (void) seteuid(0); 1203b071c689SDavid Nugent #ifdef LOGIN_CAP 1204e81b1c71SEdward Tomasz Napierala setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN | 1205e81b1c71SEdward Tomasz Napierala LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH | 1206e81b1c71SEdward Tomasz Napierala LOGIN_SETENV)); 1207b071c689SDavid Nugent #endif 1208*de8d85c9SEugene Grosbein if (logged_in && dowtmp) 1209*de8d85c9SEugene Grosbein ftpd_logwtmp(wtmpid, NULL, NULL); 1210*de8d85c9SEugene Grosbein pw = NULL; 12115bc9d93dSMark Murray #ifdef USE_PAM 1212de45162dSYaroslav Tykhiy if (pamh) { 12135bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 12145bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 12155bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 12165bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 12175bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 12185bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 12195bc9d93dSMark Murray pamh = NULL; 1220de45162dSYaroslav Tykhiy } 12215bc9d93dSMark Murray #endif 1222ea022d16SRodney W. Grimes logged_in = 0; 1223ea022d16SRodney W. Grimes guest = 0; 1224a5a4544eSPaul Traina dochroot = 0; 1225ea022d16SRodney W. Grimes } 1226ea022d16SRodney W. Grimes 12275bc9d93dSMark Murray #ifdef USE_PAM 12286c9134c0SMark Murray 12296c9134c0SMark Murray /* 12306c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 12316c9134c0SMark Murray * login.c 12326c9134c0SMark Murray */ 12336c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 12346c9134c0SMark Murray 12356c9134c0SMark Murray struct cred_t { 12366c9134c0SMark Murray const char *uname; /* user name */ 12376c9134c0SMark Murray const char *pass; /* password */ 12386c9134c0SMark Murray }; 12396c9134c0SMark Murray typedef struct cred_t cred_t; 12406c9134c0SMark Murray 12416c9134c0SMark Murray static int 12426c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 12436c9134c0SMark Murray struct pam_response **resp, void *appdata) 12446c9134c0SMark Murray { 12456c9134c0SMark Murray int i; 12466c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 124760769b19SDag-Erling Smørgrav struct pam_response *reply; 124860769b19SDag-Erling Smørgrav 124960769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply); 125060769b19SDag-Erling Smørgrav if (reply == NULL) 125160769b19SDag-Erling Smørgrav return PAM_BUF_ERR; 12526c9134c0SMark Murray 12536c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 12546c9134c0SMark Murray switch (msg[i]->msg_style) { 12556c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 12566c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12576c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 12586c9134c0SMark Murray /* PAM frees resp. */ 12596c9134c0SMark Murray break; 12606c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 12616c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12626c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 12636c9134c0SMark Murray /* PAM frees resp. */ 12646c9134c0SMark Murray break; 12656c9134c0SMark Murray case PAM_TEXT_INFO: 12666c9134c0SMark Murray case PAM_ERROR_MSG: 12676c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12686c9134c0SMark Murray reply[i].resp = NULL; 12696c9134c0SMark Murray break; 12706c9134c0SMark Murray default: /* unknown message style */ 12716c9134c0SMark Murray free(reply); 12726c9134c0SMark Murray return PAM_CONV_ERR; 12736c9134c0SMark Murray } 12746c9134c0SMark Murray } 12756c9134c0SMark Murray 12766c9134c0SMark Murray *resp = reply; 12776c9134c0SMark Murray return PAM_SUCCESS; 12786c9134c0SMark Murray } 12796c9134c0SMark Murray 12806c9134c0SMark Murray /* 12816c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 12826c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 12836c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 12846c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 12856c9134c0SMark Murray * fall back to a different authentication mechanism. 12866c9134c0SMark Murray */ 12876c9134c0SMark Murray static int 12886c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 12896c9134c0SMark Murray { 12906c9134c0SMark Murray const char *tmpl_user; 12916c9134c0SMark Murray const void *item; 12926c9134c0SMark Murray int rval; 12936c9134c0SMark Murray int e; 12946c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 12956c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 12966c9134c0SMark Murray 12976c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 12986c9134c0SMark Murray if (e != PAM_SUCCESS) { 1299545ea864SYaroslav Tykhiy /* 1300545ea864SYaroslav Tykhiy * In OpenPAM, it's OK to pass NULL to pam_strerror() 1301545ea864SYaroslav Tykhiy * if context creation has failed in the first place. 1302545ea864SYaroslav Tykhiy */ 1303545ea864SYaroslav Tykhiy syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e)); 13046c9134c0SMark Murray return -1; 13056c9134c0SMark Murray } 13066c9134c0SMark Murray 1307ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1308ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1309ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1310ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1311de45162dSYaroslav Tykhiy if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 1312de45162dSYaroslav Tykhiy syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 1313de45162dSYaroslav Tykhiy } 1314de45162dSYaroslav Tykhiy pamh = NULL; 1315ea413ab7SGuido van Rooij return -1; 1316ea413ab7SGuido van Rooij } 1317ea413ab7SGuido van Rooij 13186c9134c0SMark Murray e = pam_authenticate(pamh, 0); 13196c9134c0SMark Murray switch (e) { 13206c9134c0SMark Murray case PAM_SUCCESS: 13216c9134c0SMark Murray /* 13226c9134c0SMark Murray * With PAM we support the concept of a "template" 13236c9134c0SMark Murray * user. The user enters a login name which is 13246c9134c0SMark Murray * authenticated by PAM, usually via a remote service 13256c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 13266c9134c0SMark Murray * succeeds, a different but related "template" name 13276c9134c0SMark Murray * is used for setting the credentials, shell, and 13286c9134c0SMark Murray * home directory. The name the user enters need only 13296c9134c0SMark Murray * exist on the remote authentication server, but the 13306c9134c0SMark Murray * template name must be present in the local password 13316c9134c0SMark Murray * database. 13326c9134c0SMark Murray * 13336c9134c0SMark Murray * This is supported by two various mechanisms in the 13346c9134c0SMark Murray * individual modules. However, from the application's 13356c9134c0SMark Murray * point of view, the template user is always passed 13366c9134c0SMark Murray * back as a changed value of the PAM_USER item. 13376c9134c0SMark Murray */ 13386c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 13396c9134c0SMark Murray PAM_SUCCESS) { 13406c9134c0SMark Murray tmpl_user = (const char *) item; 13416c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 13426c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 13436c9134c0SMark Murray } else 13446c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 13456c9134c0SMark Murray pam_strerror(pamh, e)); 13466c9134c0SMark Murray rval = 0; 13476c9134c0SMark Murray break; 13486c9134c0SMark Murray 13496c9134c0SMark Murray case PAM_AUTH_ERR: 13506c9134c0SMark Murray case PAM_USER_UNKNOWN: 13516c9134c0SMark Murray case PAM_MAXTRIES: 13526c9134c0SMark Murray rval = 1; 13536c9134c0SMark Murray break; 13546c9134c0SMark Murray 13556c9134c0SMark Murray default: 13565bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 13576c9134c0SMark Murray rval = -1; 13586c9134c0SMark Murray break; 13596c9134c0SMark Murray } 13606c9134c0SMark Murray 13615bc9d93dSMark Murray if (rval == 0) { 13625bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 13635bc9d93dSMark Murray if (e != PAM_SUCCESS) { 13644cbc4ad6SYaroslav Tykhiy syslog(LOG_ERR, "pam_acct_mgmt: %s", 13654cbc4ad6SYaroslav Tykhiy pam_strerror(pamh, e)); 13665bc9d93dSMark Murray rval = 1; 13675bc9d93dSMark Murray } 13685bc9d93dSMark Murray } 13695bc9d93dSMark Murray 13705bc9d93dSMark Murray if (rval != 0) { 13716c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 13726c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 13735bc9d93dSMark Murray } 13745bc9d93dSMark Murray pamh = NULL; 13756c9134c0SMark Murray } 13766c9134c0SMark Murray return rval; 13776c9134c0SMark Murray } 13786c9134c0SMark Murray 13795bc9d93dSMark Murray #endif /* USE_PAM */ 13806c9134c0SMark Murray 1381ea022d16SRodney W. Grimes void 1382e4bc453cSWarner Losh pass(char *passwd) 1383ea022d16SRodney W. Grimes { 1384cefb6785SChristian S.J. Peron int rval, ecode; 1385ea022d16SRodney W. Grimes FILE *fd; 1386b071c689SDavid Nugent #ifdef LOGIN_CAP 1387b071c689SDavid Nugent login_cap_t *lc = NULL; 1388b071c689SDavid Nugent #endif 13895bc9d93dSMark Murray #ifdef USE_PAM 13905bc9d93dSMark Murray int e; 13915bc9d93dSMark Murray #endif 1392341e476eSYaroslav Tykhiy char *residue = NULL; 139347499ecdSAndrey A. Chernov char *xpasswd; 1394ea022d16SRodney W. Grimes 1395ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1396ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1397ea022d16SRodney W. Grimes return; 1398ea022d16SRodney W. Grimes } 1399ea022d16SRodney W. Grimes askpasswd = 0; 1400ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1401a5a4544eSPaul Traina if (pw == NULL) { 1402a5a4544eSPaul Traina rval = 1; /* failure below */ 1403a5a4544eSPaul Traina goto skip; 1404a5a4544eSPaul Traina } 14055bc9d93dSMark Murray #ifdef USE_PAM 14066c9134c0SMark Murray rval = auth_pam(&pw, passwd); 1407f650a124SAndrey A. Chernov if (rval >= 0) { 1408f650a124SAndrey A. Chernov opieunlock(); 1409a5a4544eSPaul Traina goto skip; 1410f650a124SAndrey A. Chernov } 1411f650a124SAndrey A. Chernov #endif 141247499ecdSAndrey A. Chernov if (opieverify(&opiedata, passwd) == 0) 141347499ecdSAndrey A. Chernov xpasswd = pw->pw_passwd; 1414f650a124SAndrey A. Chernov else if (pwok) { 141547499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd); 1416f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1417f650a124SAndrey A. Chernov xpasswd = ":"; 1418f650a124SAndrey A. Chernov } else { 141947499ecdSAndrey A. Chernov rval = 1; 142047499ecdSAndrey A. Chernov goto skip; 142147499ecdSAndrey A. Chernov } 142247499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd); 1423f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1424a5a4544eSPaul Traina rval = 1; /* failure */ 1425a5a4544eSPaul Traina skip: 1426a5a4544eSPaul Traina /* 1427a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 14286c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1429a5a4544eSPaul Traina * succeeded. 1430a5a4544eSPaul Traina */ 1431a5a4544eSPaul Traina if (rval) { 1432ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1433e07d11b6SKurt Lidl BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL, STDIN_FILENO, "Login incorrect"); 1434b8939f6fSYaroslav Tykhiy if (logging) { 1435ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1436b8939f6fSYaroslav Tykhiy "FTP LOGIN FAILED FROM %s", 1437b8939f6fSYaroslav Tykhiy remotehost); 1438b8939f6fSYaroslav Tykhiy syslog(LOG_AUTHPRIV | LOG_NOTICE, 1439ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1440ea022d16SRodney W. Grimes remotehost, curname); 1441b8939f6fSYaroslav Tykhiy } 1442ea022d16SRodney W. Grimes pw = NULL; 1443ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1444ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1445ea022d16SRodney W. Grimes "repeated login failures from %s", 1446ea022d16SRodney W. Grimes remotehost); 1447ea022d16SRodney W. Grimes exit(0); 1448ea022d16SRodney W. Grimes } 1449ea022d16SRodney W. Grimes return; 1450e07d11b6SKurt Lidl } else { 1451e07d11b6SKurt Lidl BLACKLIST_NOTIFY(BLACKLIST_AUTH_OK, STDIN_FILENO, "Login successful"); 1452ea022d16SRodney W. Grimes } 1453ea022d16SRodney W. Grimes } 1454ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1455c4536e21SYaroslav Tykhiy if (setegid(pw->pw_gid) < 0) { 1456ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1457ea022d16SRodney W. Grimes return; 1458ea022d16SRodney W. Grimes } 1459b071c689SDavid Nugent /* May be overridden by login.conf */ 1460b071c689SDavid Nugent (void) umask(defumask); 1461b071c689SDavid Nugent #ifdef LOGIN_CAP 14625d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 146304683b2cSYaroslav Tykhiy char remote_ip[NI_MAXHOST]; 1464b071c689SDavid Nugent 146504683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 14664dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 146704683b2cSYaroslav Tykhiy NI_NUMERICHOST)) 146804683b2cSYaroslav Tykhiy *remote_ip = 0; 1469b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1470b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1471b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1472b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1473b071c689SDavid Nugent pw->pw_name); 14744a3e5acdSYaroslav Tykhiy reply(530, "Permission denied."); 1475b071c689SDavid Nugent pw = NULL; 1476b071c689SDavid Nugent return; 1477b071c689SDavid Nugent } 1478b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 14794a3e5acdSYaroslav Tykhiy reply(530, "Login not available right now."); 1480b071c689SDavid Nugent pw = NULL; 1481b071c689SDavid Nugent return; 1482b071c689SDavid Nugent } 1483b071c689SDavid Nugent } 1484e81b1c71SEdward Tomasz Napierala setusercontext(lc, pw, 0, LOGIN_SETALL & 1485*de8d85c9SEugene Grosbein ~(LOGIN_SETRESOURCES | LOGIN_SETUSER | LOGIN_SETPATH | LOGIN_SETENV)); 1486b071c689SDavid Nugent #else 1487e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1488ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1489b071c689SDavid Nugent #endif 1490ea022d16SRodney W. Grimes 14915bc9d93dSMark Murray #ifdef USE_PAM 14925bc9d93dSMark Murray if (pamh) { 14935bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 14945bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 14955bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 14965bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 14975bc9d93dSMark Murray } 14985bc9d93dSMark Murray } 14995bc9d93dSMark Murray #endif 15005bc9d93dSMark Murray 150180643af0SEd Schouten dochroot = 1502cefb6785SChristian S.J. Peron checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue, &ecode) 150380643af0SEd Schouten #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 150480643af0SEd Schouten || login_getcapbool(lc, "ftp-chroot", 0) 150580643af0SEd Schouten #endif 150680643af0SEd Schouten ; 1507cefb6785SChristian S.J. Peron /* 1508cefb6785SChristian S.J. Peron * It is possible that checkuser() failed to open the chroot file. 1509cefb6785SChristian S.J. Peron * If this is the case, report that logins are un-available, since we 1510cefb6785SChristian S.J. Peron * have no way of checking whether or not the user should be chrooted. 1511cefb6785SChristian S.J. Peron * We ignore ENOENT since it is not required that this file be present. 1512cefb6785SChristian S.J. Peron */ 1513cefb6785SChristian S.J. Peron if (ecode != 0 && ecode != ENOENT) { 1514cefb6785SChristian S.J. Peron reply(530, "Login not available right now."); 1515cefb6785SChristian S.J. Peron return; 1516cefb6785SChristian S.J. Peron } 151780643af0SEd Schouten chrootdir = NULL; 151880643af0SEd Schouten 15199f37b1a2SEd Schouten /* Disable wtmp logging when chrooting. */ 15209f37b1a2SEd Schouten if (dochroot || guest) 15219f37b1a2SEd Schouten dowtmp = 0; 15229f37b1a2SEd Schouten if (dowtmp) 152380643af0SEd Schouten ftpd_logwtmp(wtmpid, pw->pw_name, 15245d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr); 1525ea022d16SRodney W. Grimes logged_in = 1; 1526ea022d16SRodney W. Grimes 1527*de8d85c9SEugene Grosbein #ifdef LOGIN_CAP 1528*de8d85c9SEugene Grosbein setusercontext(lc, pw, 0, LOGIN_SETRESOURCES); 1529*de8d85c9SEugene Grosbein #endif 1530*de8d85c9SEugene Grosbein 1531a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1532ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 153363047c6fSDavid E. O'Brien statfd = open(thishost->statfile, O_WRONLY|O_APPEND); 1534ea4e54b9SDavid Nugent #else 153563047c6fSDavid E. O'Brien statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND); 1536ea4e54b9SDavid Nugent #endif 153763047c6fSDavid E. O'Brien if (statfd < 0) 15383eb568f2SGuido van Rooij stats = 0; 15393eb568f2SGuido van Rooij 1540ea022d16SRodney W. Grimes /* 1541ce9287fcSYaroslav Tykhiy * For a chrooted local user, 1542ce9287fcSYaroslav Tykhiy * a) see whether ftpchroot(5) specifies a chroot directory, 1543ce9287fcSYaroslav Tykhiy * b) extract the directory pathname from the line, 1544ce9287fcSYaroslav Tykhiy * c) expand it to the absolute pathname if necessary. 1545ea022d16SRodney W. Grimes */ 1546ce9287fcSYaroslav Tykhiy if (dochroot && residue && 154739bce482SYaroslav Tykhiy (chrootdir = strtok(residue, " \t")) != NULL) { 154839bce482SYaroslav Tykhiy if (chrootdir[0] != '/') 1549341e476eSYaroslav Tykhiy asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir); 155039bce482SYaroslav Tykhiy else 1551215a9f9dSYaroslav Tykhiy chrootdir = strdup(chrootdir); /* make it permanent */ 1552341e476eSYaroslav Tykhiy if (chrootdir == NULL) 15538657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 15548657b576SYaroslav Tykhiy } 1555ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1556ce9287fcSYaroslav Tykhiy /* 1557ce9287fcSYaroslav Tykhiy * If no chroot directory set yet, use the login directory. 1558ce9287fcSYaroslav Tykhiy * Copy it so it can be modified while pw->pw_dir stays intact. 1559ce9287fcSYaroslav Tykhiy */ 1560ce9287fcSYaroslav Tykhiy if (chrootdir == NULL && 1561ce9287fcSYaroslav Tykhiy (chrootdir = strdup(pw->pw_dir)) == NULL) 1562ce9287fcSYaroslav Tykhiy fatalerror("Ran out of memory."); 1563ce9287fcSYaroslav Tykhiy /* 1564ce9287fcSYaroslav Tykhiy * Check for the "/chroot/./home" syntax, 1565ce9287fcSYaroslav Tykhiy * separate the chroot and home directory pathnames. 1566ce9287fcSYaroslav Tykhiy */ 1567ce9287fcSYaroslav Tykhiy if ((homedir = strstr(chrootdir, "/./")) != NULL) { 1568ce9287fcSYaroslav Tykhiy *(homedir++) = '\0'; /* wipe '/' */ 1569ce9287fcSYaroslav Tykhiy homedir++; /* skip '.' */ 1570ce9287fcSYaroslav Tykhiy } else { 1571ce9287fcSYaroslav Tykhiy /* 1572ce9287fcSYaroslav Tykhiy * We MUST do a chdir() after the chroot. Otherwise 1573ce9287fcSYaroslav Tykhiy * the old current directory will be accessible as "." 1574ce9287fcSYaroslav Tykhiy * outside the new root! 1575ce9287fcSYaroslav Tykhiy */ 1576ce9287fcSYaroslav Tykhiy homedir = "/"; 1577ce9287fcSYaroslav Tykhiy } 1578ce9287fcSYaroslav Tykhiy /* 1579ce9287fcSYaroslav Tykhiy * Finally, do chroot() 1580ce9287fcSYaroslav Tykhiy */ 1581ce9287fcSYaroslav Tykhiy if (chroot(chrootdir) < 0) { 1582a5a4544eSPaul Traina reply(550, "Can't change root."); 1583a5a4544eSPaul Traina goto bad; 1584a5a4544eSPaul Traina } 15853e65b9c6SColin Percival __FreeBSD_libc_enter_restricted_mode(); 1586ce9287fcSYaroslav Tykhiy } else /* real user w/o chroot */ 1587ce9287fcSYaroslav Tykhiy homedir = pw->pw_dir; 1588ce9287fcSYaroslav Tykhiy /* 1589ce9287fcSYaroslav Tykhiy * Set euid *before* doing chdir() so 1590ce9287fcSYaroslav Tykhiy * a) the user won't be carried to a directory that he couldn't reach 1591ce9287fcSYaroslav Tykhiy * on his own due to no permission to upper path components, 1592ce9287fcSYaroslav Tykhiy * b) NFS mounted homedirs w/restrictive permissions will be accessible 1593ce9287fcSYaroslav Tykhiy * (uid 0 has no root power over NFS if not mapped explicitly.) 1594ce9287fcSYaroslav Tykhiy */ 159552e7ee74SYaroslav Tykhiy if (seteuid(pw->pw_uid) < 0) { 1596ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1597ea022d16SRodney W. Grimes goto bad; 1598ea022d16SRodney W. Grimes } 1599ce9287fcSYaroslav Tykhiy if (chdir(homedir) < 0) { 1600ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1601ce9287fcSYaroslav Tykhiy reply(550, "Can't change to base directory."); 1602ce9287fcSYaroslav Tykhiy goto bad; 1603ce9287fcSYaroslav Tykhiy } else { 1604ce9287fcSYaroslav Tykhiy if (chdir("/") < 0) { 1605ce9287fcSYaroslav Tykhiy reply(550, "Root is inaccessible."); 1606ce9287fcSYaroslav Tykhiy goto bad; 1607ce9287fcSYaroslav Tykhiy } 160802c97492SYaroslav Tykhiy lreply(230, "No directory! Logging in with home=/."); 1609ce9287fcSYaroslav Tykhiy } 1610ce9287fcSYaroslav Tykhiy } 161182c76939SDavid Greenman 161282c76939SDavid Greenman /* 1613ea022d16SRodney W. Grimes * Display a login message, if it exists. 1614ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1615ea022d16SRodney W. Grimes */ 1616ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 161763047c6fSDavid E. O'Brien fd = fopen(thishost->loginmsg, "r"); 1618ea4e54b9SDavid Nugent #else 161963047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPLOGINMESG, "r"); 1620ea4e54b9SDavid Nugent #endif 162163047c6fSDavid E. O'Brien if (fd != NULL) { 1622ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1623ea022d16SRodney W. Grimes 1624ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1625ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1626ea022d16SRodney W. Grimes *cp = '\0'; 1627ea022d16SRodney W. Grimes lreply(230, "%s", line); 1628ea022d16SRodney W. Grimes } 1629ea022d16SRodney W. Grimes (void) fflush(stdout); 1630ea022d16SRodney W. Grimes (void) fclose(fd); 1631ea022d16SRodney W. Grimes } 1632ea022d16SRodney W. Grimes if (guest) { 16333eb568f2SGuido van Rooij if (ident != NULL) 16343eb568f2SGuido van Rooij free(ident); 163561f891a6SPaul Traina ident = strdup(passwd); 163661f891a6SPaul Traina if (ident == NULL) 1637618b0bbaSMark Murray fatalerror("Ran out of memory."); 163861f891a6SPaul Traina 1639ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1640ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1641ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1642ea4e54b9SDavid Nugent if (thishost != firsthost) 1643ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1644b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname, 1645b3a0a7cdSMike Heffner passwd); 1646ea4e54b9SDavid Nugent else 1647ea4e54b9SDavid Nugent #endif 1648ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1649b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd); 1650b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1651ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1652ea022d16SRodney W. Grimes if (logging) 1653ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1654ea022d16SRodney W. Grimes remotehost, passwd); 1655ea022d16SRodney W. Grimes } else { 16563401a71fSDaniel O'Callaghan if (dochroot) 165711342ab1SYaroslav Tykhiy reply(230, "User %s logged in, " 165811342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name); 16593401a71fSDaniel O'Callaghan else 1660ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 16613401a71fSDaniel O'Callaghan 1662ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1663ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 16647a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name); 1665b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1666ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1667ea022d16SRodney W. Grimes if (logging) 1668ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1669ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1670ea022d16SRodney W. Grimes } 1671220223fdSYaroslav Tykhiy if (logging && (guest || dochroot)) 1672e897216fSYaroslav Tykhiy syslog(LOG_INFO, "session root changed to %s", chrootdir); 1673b071c689SDavid Nugent #ifdef LOGIN_CAP 1674b071c689SDavid Nugent login_close(lc); 1675b071c689SDavid Nugent #endif 1676ce9287fcSYaroslav Tykhiy if (residue) 1677ce9287fcSYaroslav Tykhiy free(residue); 1678ea022d16SRodney W. Grimes return; 1679ea022d16SRodney W. Grimes bad: 1680ea022d16SRodney W. Grimes /* Forget all about it... */ 1681b071c689SDavid Nugent #ifdef LOGIN_CAP 1682b071c689SDavid Nugent login_close(lc); 1683b071c689SDavid Nugent #endif 1684ce9287fcSYaroslav Tykhiy if (residue) 1685ce9287fcSYaroslav Tykhiy free(residue); 1686ea022d16SRodney W. Grimes end_login(); 1687ea022d16SRodney W. Grimes } 1688ea022d16SRodney W. Grimes 1689ea022d16SRodney W. Grimes void 1690e4bc453cSWarner Losh retrieve(char *cmd, char *name) 1691ea022d16SRodney W. Grimes { 1692ea022d16SRodney W. Grimes FILE *fin, *dout; 1693ea022d16SRodney W. Grimes struct stat st; 1694e4bc453cSWarner Losh int (*closefunc)(FILE *); 1695158a00b2SJohn Birrell time_t start; 16968877d1dbSDon Lewis char line[BUFSIZ]; 1697ea022d16SRodney W. Grimes 1698ea022d16SRodney W. Grimes if (cmd == 0) { 1699ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1700ea022d16SRodney W. Grimes st.st_size = 0; 1701ea022d16SRodney W. Grimes } else { 17028877d1dbSDon Lewis (void) snprintf(line, sizeof(line), cmd, name); 17038877d1dbSDon Lewis name = line; 1704ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1705ea022d16SRodney W. Grimes st.st_size = -1; 1706ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1707ea022d16SRodney W. Grimes } 1708ea022d16SRodney W. Grimes if (fin == NULL) { 1709ea022d16SRodney W. Grimes if (errno != 0) { 1710ea022d16SRodney W. Grimes perror_reply(550, name); 1711ea022d16SRodney W. Grimes if (cmd == 0) { 1712ea022d16SRodney W. Grimes LOGCMD("get", name); 1713ea022d16SRodney W. Grimes } 1714ea022d16SRodney W. Grimes } 1715ea022d16SRodney W. Grimes return; 1716ea022d16SRodney W. Grimes } 1717ea022d16SRodney W. Grimes byte_count = -1; 1718ea701226SYaroslav Tykhiy if (cmd == 0) { 1719ea701226SYaroslav Tykhiy if (fstat(fileno(fin), &st) < 0) { 1720ea701226SYaroslav Tykhiy perror_reply(550, name); 1721ea701226SYaroslav Tykhiy goto done; 1722ea701226SYaroslav Tykhiy } 1723ea701226SYaroslav Tykhiy if (!S_ISREG(st.st_mode)) { 172410e89104SYaroslav Tykhiy /* 172510e89104SYaroslav Tykhiy * Never sending a raw directory is a workaround 172610e89104SYaroslav Tykhiy * for buggy clients that will attempt to RETR 172710e89104SYaroslav Tykhiy * a directory before listing it, e.g., Mozilla. 172810e89104SYaroslav Tykhiy * Preventing a guest from getting irregular files 172910e89104SYaroslav Tykhiy * is a simple security measure. 173010e89104SYaroslav Tykhiy */ 173110e89104SYaroslav Tykhiy if (S_ISDIR(st.st_mode) || guest) { 1732ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1733ea022d16SRodney W. Grimes goto done; 1734ea022d16SRodney W. Grimes } 1735ea701226SYaroslav Tykhiy st.st_size = -1; 1736ea701226SYaroslav Tykhiy /* st.st_blksize is set for all descriptor types */ 1737ea701226SYaroslav Tykhiy } 1738ea701226SYaroslav Tykhiy } 1739ea022d16SRodney W. Grimes if (restart_point) { 1740ea022d16SRodney W. Grimes if (type == TYPE_A) { 1741ea022d16SRodney W. Grimes off_t i, n; 1742ea022d16SRodney W. Grimes int c; 1743ea022d16SRodney W. Grimes 1744ea022d16SRodney W. Grimes n = restart_point; 1745ea022d16SRodney W. Grimes i = 0; 1746ea022d16SRodney W. Grimes while (i++ < n) { 1747ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1748ea022d16SRodney W. Grimes perror_reply(550, name); 1749ea022d16SRodney W. Grimes goto done; 1750ea022d16SRodney W. Grimes } 1751ea022d16SRodney W. Grimes if (c == '\n') 1752ea022d16SRodney W. Grimes i++; 1753ea022d16SRodney W. Grimes } 1754ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1755ea022d16SRodney W. Grimes perror_reply(550, name); 1756ea022d16SRodney W. Grimes goto done; 1757ea022d16SRodney W. Grimes } 1758ea022d16SRodney W. Grimes } 1759ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1760ea022d16SRodney W. Grimes if (dout == NULL) 1761ea022d16SRodney W. Grimes goto done; 17623eb568f2SGuido van Rooij time(&start); 17639fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 17649fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 1765c999732bSYaroslav Tykhiy if (cmd == 0 && guest && stats && byte_count > 0) 1766c999732bSYaroslav Tykhiy logxfer(name, byte_count, start); 1767ea022d16SRodney W. Grimes (void) fclose(dout); 1768ea022d16SRodney W. Grimes data = -1; 1769ea022d16SRodney W. Grimes pdata = -1; 1770ea022d16SRodney W. Grimes done: 1771ea022d16SRodney W. Grimes if (cmd == 0) 1772ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1773ea022d16SRodney W. Grimes (*closefunc)(fin); 1774ea022d16SRodney W. Grimes } 1775ea022d16SRodney W. Grimes 1776ea022d16SRodney W. Grimes void 1777e4bc453cSWarner Losh store(char *name, char *mode, int unique) 1778ea022d16SRodney W. Grimes { 1779a117c345SYaroslav Tykhiy int fd; 1780ea022d16SRodney W. Grimes FILE *fout, *din; 1781e4bc453cSWarner Losh int (*closefunc)(FILE *); 1782ea022d16SRodney W. Grimes 1783a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */ 1784a117c345SYaroslav Tykhiy if (unique) { 1785a117c345SYaroslav Tykhiy /* Programming error */ 1786a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE"); 1787a117c345SYaroslav Tykhiy unique = 0; 1788a117c345SYaroslav Tykhiy } 1789a117c345SYaroslav Tykhiy if (guest && noguestmod) { 179002c97492SYaroslav Tykhiy reply(550, "Appending to existing file denied."); 1791a117c345SYaroslav Tykhiy goto err; 1792a117c345SYaroslav Tykhiy } 1793a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */ 1794a117c345SYaroslav Tykhiy } 1795a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */ 1796a117c345SYaroslav Tykhiy restart_point = 0; 1797a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1798a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */ 179902c97492SYaroslav Tykhiy reply(550, "Modifying existing file denied."); 1800a117c345SYaroslav Tykhiy goto err; 1801a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */ 1802a117c345SYaroslav Tykhiy unique = 1; 1803ea022d16SRodney W. Grimes } 1804ea022d16SRodney W. Grimes 1805ea022d16SRodney W. Grimes if (restart_point) 1806a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */ 1807a117c345SYaroslav Tykhiy if (unique) { 1808a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0) 1809a117c345SYaroslav Tykhiy goto err; 1810a117c345SYaroslav Tykhiy fout = fdopen(fd, mode); 1811a117c345SYaroslav Tykhiy } else 1812ea022d16SRodney W. Grimes fout = fopen(name, mode); 1813ea022d16SRodney W. Grimes closefunc = fclose; 1814ea022d16SRodney W. Grimes if (fout == NULL) { 1815ea022d16SRodney W. Grimes perror_reply(553, name); 1816a117c345SYaroslav Tykhiy goto err; 1817ea022d16SRodney W. Grimes } 1818ea022d16SRodney W. Grimes byte_count = -1; 1819ea022d16SRodney W. Grimes if (restart_point) { 1820ea022d16SRodney W. Grimes if (type == TYPE_A) { 1821ea022d16SRodney W. Grimes off_t i, n; 1822ea022d16SRodney W. Grimes int c; 1823ea022d16SRodney W. Grimes 1824ea022d16SRodney W. Grimes n = restart_point; 1825ea022d16SRodney W. Grimes i = 0; 1826ea022d16SRodney W. Grimes while (i++ < n) { 1827ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1828ea022d16SRodney W. Grimes perror_reply(550, name); 1829ea022d16SRodney W. Grimes goto done; 1830ea022d16SRodney W. Grimes } 1831ea022d16SRodney W. Grimes if (c == '\n') 1832ea022d16SRodney W. Grimes i++; 1833ea022d16SRodney W. Grimes } 1834ea022d16SRodney W. Grimes /* 1835ea022d16SRodney W. Grimes * We must do this seek to "current" position 1836ea022d16SRodney W. Grimes * because we are changing from reading to 1837ea022d16SRodney W. Grimes * writing. 1838ea022d16SRodney W. Grimes */ 18390e519c96SYaroslav Tykhiy if (fseeko(fout, 0, SEEK_CUR) < 0) { 1840ea022d16SRodney W. Grimes perror_reply(550, name); 1841ea022d16SRodney W. Grimes goto done; 1842ea022d16SRodney W. Grimes } 1843ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1844ea022d16SRodney W. Grimes perror_reply(550, name); 1845ea022d16SRodney W. Grimes goto done; 1846ea022d16SRodney W. Grimes } 1847ea022d16SRodney W. Grimes } 18480e519c96SYaroslav Tykhiy din = dataconn(name, -1, "r"); 1849ea022d16SRodney W. Grimes if (din == NULL) 1850ea022d16SRodney W. Grimes goto done; 1851ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1852ea022d16SRodney W. Grimes if (unique) 1853ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1854ea022d16SRodney W. Grimes name); 1855ea022d16SRodney W. Grimes else 1856ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1857ea022d16SRodney W. Grimes } 1858ea022d16SRodney W. Grimes (void) fclose(din); 1859ea022d16SRodney W. Grimes data = -1; 1860ea022d16SRodney W. Grimes pdata = -1; 1861ea022d16SRodney W. Grimes done: 18627c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); 1863ea022d16SRodney W. Grimes (*closefunc)(fout); 1864a117c345SYaroslav Tykhiy return; 1865a117c345SYaroslav Tykhiy err: 1866a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name); 1867a117c345SYaroslav Tykhiy return; 1868ea022d16SRodney W. Grimes } 1869ea022d16SRodney W. Grimes 1870ea022d16SRodney W. Grimes static FILE * 1871e4bc453cSWarner Losh getdatasock(char *mode) 1872ea022d16SRodney W. Grimes { 1873ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1874ea022d16SRodney W. Grimes 1875ea022d16SRodney W. Grimes if (data >= 0) 1876ea022d16SRodney W. Grimes return (fdopen(data, mode)); 18774dd8b5abSYoshinobu Inoue 18784dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1879ea022d16SRodney W. Grimes if (s < 0) 1880ea022d16SRodney W. Grimes goto bad; 188157d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1882406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1883ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 18844dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 188563591ba5SYaroslav Tykhiy data_source.su_port = htons(dataport); 188652e7ee74SYaroslav Tykhiy (void) seteuid(0); 1887ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 18889ec7612aSYaroslav Tykhiy /* 18899ec7612aSYaroslav Tykhiy * We should loop here since it's possible that 18909ec7612aSYaroslav Tykhiy * another ftpd instance has passed this point and is 18919ec7612aSYaroslav Tykhiy * trying to open a data connection in active mode now. 18929ec7612aSYaroslav Tykhiy * Until the other connection is opened, we'll be getting 18939ec7612aSYaroslav Tykhiy * EADDRINUSE because no SOCK_STREAM sockets in the system 18949ec7612aSYaroslav Tykhiy * can share both local and remote addresses, localIP:20 18959ec7612aSYaroslav Tykhiy * and *:* in this case. 18969ec7612aSYaroslav Tykhiy */ 1897ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 18984dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1899ea022d16SRodney W. Grimes break; 1900ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1901ea022d16SRodney W. Grimes goto bad; 1902ea022d16SRodney W. Grimes sleep(tries); 1903ea022d16SRodney W. Grimes } 190452e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 1905ea022d16SRodney W. Grimes #ifdef IP_TOS 19064dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 19074dd8b5abSYoshinobu Inoue { 1908ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 190957d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1910406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 19114dd8b5abSYoshinobu Inoue } 1912ea022d16SRodney W. Grimes #endif 19139fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 19149fc5823aSGarrett Wollman /* 19159fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 191632072720SYaroslav Tykhiy * at the boundaries of each write(). 19179fc5823aSGarrett Wollman */ 19189fc5823aSGarrett Wollman on = 1; 191957d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1920406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 19219fc5823aSGarrett Wollman #endif 1922ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1923ea022d16SRodney W. Grimes bad: 1924ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1925ea022d16SRodney W. Grimes t = errno; 192652e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 1927ea022d16SRodney W. Grimes (void) close(s); 1928ea022d16SRodney W. Grimes errno = t; 1929ea022d16SRodney W. Grimes return (NULL); 1930ea022d16SRodney W. Grimes } 1931ea022d16SRodney W. Grimes 1932ea022d16SRodney W. Grimes static FILE * 1933e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode) 1934ea022d16SRodney W. Grimes { 1935ea022d16SRodney W. Grimes char sizebuf[32]; 1936ea022d16SRodney W. Grimes FILE *file; 1937e5094456SCrist J. Clark int retry = 0, tos, conerrno; 1938ea022d16SRodney W. Grimes 1939ea022d16SRodney W. Grimes file_size = size; 1940ea022d16SRodney W. Grimes byte_count = 0; 19410e519c96SYaroslav Tykhiy if (size != -1) 1942a57e1ef0SYaroslav Tykhiy (void) snprintf(sizebuf, sizeof(sizebuf), 1943a57e1ef0SYaroslav Tykhiy " (%jd bytes)", (intmax_t)size); 1944ea022d16SRodney W. Grimes else 1945e760ef2cSWarner Losh *sizebuf = '\0'; 1946ea022d16SRodney W. Grimes if (pdata >= 0) { 19474dd8b5abSYoshinobu Inoue union sockunion from; 194878e3eed0SStefan Farfeleder socklen_t fromlen = ctrl_addr.su_len; 194978e3eed0SStefan Farfeleder int flags, s; 1950d6ed3c37SGuido van Rooij struct timeval timeout; 1951d6ed3c37SGuido van Rooij fd_set set; 1952ea022d16SRodney W. Grimes 1953d6ed3c37SGuido van Rooij FD_ZERO(&set); 1954d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1955d6ed3c37SGuido van Rooij 1956d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1957d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1958d6ed3c37SGuido van Rooij 19594cd48bacSYaroslav Tykhiy /* 19604cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode, 19614cd48bacSYaroslav Tykhiy * accept() will block after a successful select() 19624cd48bacSYaroslav Tykhiy * if the selected connection dies in between. 19634cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here. 19644cd48bacSYaroslav Tykhiy */ 19654cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 19664cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 19674cd48bacSYaroslav Tykhiy goto pdata_err; 1968aa5a9d3fSYaroslav Tykhiy if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 || 19694cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 19704cd48bacSYaroslav Tykhiy goto pdata_err; 1971ea022d16SRodney W. Grimes (void) close(pdata); 1972ea022d16SRodney W. Grimes pdata = s; 19734cd48bacSYaroslav Tykhiy /* 1974f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag 1975f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it. 19764cd48bacSYaroslav Tykhiy */ 19774cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 19784cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 19794cd48bacSYaroslav Tykhiy goto pdata_err; 1980ea022d16SRodney W. Grimes #ifdef IP_TOS 19814dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 19824dd8b5abSYoshinobu Inoue { 1983a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 198457d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1985406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 19864dd8b5abSYoshinobu Inoue } 1987ea022d16SRodney W. Grimes #endif 1988ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1989ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1990ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 19914cd48bacSYaroslav Tykhiy pdata_err: 19924cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection."); 19934cd48bacSYaroslav Tykhiy (void) close(pdata); 19944cd48bacSYaroslav Tykhiy pdata = -1; 19954cd48bacSYaroslav Tykhiy return (NULL); 1996ea022d16SRodney W. Grimes } 1997ea022d16SRodney W. Grimes if (data >= 0) { 1998ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1999ea022d16SRodney W. Grimes name, sizebuf); 2000ea022d16SRodney W. Grimes usedefault = 1; 2001ea022d16SRodney W. Grimes return (fdopen(data, mode)); 2002ea022d16SRodney W. Grimes } 2003ea022d16SRodney W. Grimes if (usedefault) 2004ea022d16SRodney W. Grimes data_dest = his_addr; 2005ea022d16SRodney W. Grimes usedefault = 1; 2006e5094456SCrist J. Clark do { 2007ea022d16SRodney W. Grimes file = getdatasock(mode); 2008ea022d16SRodney W. Grimes if (file == NULL) { 200904683b2cSYaroslav Tykhiy char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV]; 201004683b2cSYaroslav Tykhiy 201104683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&data_source, 201204683b2cSYaroslav Tykhiy data_source.su_len, 201304683b2cSYaroslav Tykhiy hostbuf, sizeof(hostbuf) - 1, 201404683b2cSYaroslav Tykhiy portbuf, sizeof(portbuf) - 1, 201504683b2cSYaroslav Tykhiy NI_NUMERICHOST|NI_NUMERICSERV)) 201604683b2cSYaroslav Tykhiy *hostbuf = *portbuf = 0; 201704683b2cSYaroslav Tykhiy hostbuf[sizeof(hostbuf) - 1] = 0; 201804683b2cSYaroslav Tykhiy portbuf[sizeof(portbuf) - 1] = 0; 20194dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 20204dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 2021ea022d16SRodney W. Grimes return (NULL); 2022ea022d16SRodney W. Grimes } 2023ea022d16SRodney W. Grimes data = fileno(file); 2024e5094456SCrist J. Clark conerrno = 0; 2025e5094456SCrist J. Clark if (connect(data, (struct sockaddr *)&data_dest, 2026e5094456SCrist J. Clark data_dest.su_len) == 0) 2027e5094456SCrist J. Clark break; 2028e5094456SCrist J. Clark conerrno = errno; 2029ea022d16SRodney W. Grimes (void) fclose(file); 2030ea022d16SRodney W. Grimes data = -1; 2031e5094456SCrist J. Clark if (conerrno == EADDRINUSE) { 2032e3765043SYaroslav Tykhiy sleep(swaitint); 2033e5094456SCrist J. Clark retry += swaitint; 2034e5094456SCrist J. Clark } else { 2035e5094456SCrist J. Clark break; 2036e5094456SCrist J. Clark } 2037e5094456SCrist J. Clark } while (retry <= swaitmax); 2038e5094456SCrist J. Clark if (conerrno != 0) { 203982c03024SYaroslav Tykhiy reply(425, "Can't build data connection: %s.", 204082c03024SYaroslav Tykhiy strerror(conerrno)); 2041ea022d16SRodney W. Grimes return (NULL); 2042ea022d16SRodney W. Grimes } 2043ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 2044ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 2045ea022d16SRodney W. Grimes return (file); 2046ea022d16SRodney W. Grimes } 2047ea022d16SRodney W. Grimes 2048ea022d16SRodney W. Grimes /* 20494cd51076SYaroslav Tykhiy * A helper macro to avoid code duplication 20504cd51076SYaroslav Tykhiy * in send_data() and receive_data(). 20514cd51076SYaroslav Tykhiy * 20524cd51076SYaroslav Tykhiy * XXX We have to block SIGURG during putc() because BSD stdio 20534cd51076SYaroslav Tykhiy * is unable to restart interrupted write operations and hence 20544cd51076SYaroslav Tykhiy * the entire buffer contents will be lost as soon as a write() 20554cd51076SYaroslav Tykhiy * call indicates EINTR to stdio. 20564cd51076SYaroslav Tykhiy */ 20574cd51076SYaroslav Tykhiy #define FTPD_PUTC(ch, file, label) \ 20584cd51076SYaroslav Tykhiy do { \ 20594cd51076SYaroslav Tykhiy int ret; \ 20604cd51076SYaroslav Tykhiy \ 20614cd51076SYaroslav Tykhiy do { \ 20624cd51076SYaroslav Tykhiy START_UNSAFE; \ 20634cd51076SYaroslav Tykhiy ret = putc((ch), (file)); \ 20644cd51076SYaroslav Tykhiy END_UNSAFE; \ 20654cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) \ 20664cd51076SYaroslav Tykhiy else if (ferror(file)) \ 20674cd51076SYaroslav Tykhiy goto label; \ 20684cd51076SYaroslav Tykhiy clearerr(file); \ 20694cd51076SYaroslav Tykhiy } while (ret == EOF); \ 20704cd51076SYaroslav Tykhiy } while (0) 20714cd51076SYaroslav Tykhiy 20724cd51076SYaroslav Tykhiy /* 2073ec489d64SPedro F. Giffuni * Transfer the contents of "instr" to "outstr" peer using the appropriate 20749fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 2075ea022d16SRodney W. Grimes * 2076ea022d16SRodney W. Grimes * NB: Form isn't handled. 2077ea022d16SRodney W. Grimes */ 20784b82fc95SYaroslav Tykhiy static int 20796e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg) 2080ea022d16SRodney W. Grimes { 2081db1c2da3SYaroslav Tykhiy int c, cp, filefd, netfd; 2082f6f0c4b9SDan Moschuk char *buf; 2083ea022d16SRodney W. Grimes 20844cd51076SYaroslav Tykhiy STARTXFER; 20854cd51076SYaroslav Tykhiy 2086ea022d16SRodney W. Grimes switch (type) { 2087ea022d16SRodney W. Grimes 2088ea022d16SRodney W. Grimes case TYPE_A: 20894cd51076SYaroslav Tykhiy cp = EOF; 20904cd51076SYaroslav Tykhiy for (;;) { 20914cd51076SYaroslav Tykhiy c = getc(instr); 20924cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 20934cd51076SYaroslav Tykhiy else if (c == EOF && ferror(instr)) 20944cd51076SYaroslav Tykhiy goto file_err; 20954cd51076SYaroslav Tykhiy if (c == EOF) { 20964cd51076SYaroslav Tykhiy if (ferror(instr)) { /* resume after OOB */ 20974cd51076SYaroslav Tykhiy clearerr(instr); 20984cd51076SYaroslav Tykhiy continue; 2099ea022d16SRodney W. Grimes } 21004cd51076SYaroslav Tykhiy if (feof(instr)) /* EOF */ 21014cd51076SYaroslav Tykhiy break; 21024cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: impossible condition" 21034cd51076SYaroslav Tykhiy " on file after getc()"); 21044cd51076SYaroslav Tykhiy goto file_err; 21054cd51076SYaroslav Tykhiy } 21064cd51076SYaroslav Tykhiy if (c == '\n' && cp != '\r') { 21074cd51076SYaroslav Tykhiy FTPD_PUTC('\r', outstr, data_err); 21084cd51076SYaroslav Tykhiy byte_count++; 21094cd51076SYaroslav Tykhiy } 21104cd51076SYaroslav Tykhiy FTPD_PUTC(c, outstr, data_err); 21114cd51076SYaroslav Tykhiy byte_count++; 2112db1c2da3SYaroslav Tykhiy cp = c; 2113ea022d16SRodney W. Grimes } 21144cd51076SYaroslav Tykhiy #ifdef notyet /* BSD stdio isn't ready for that */ 21154cd51076SYaroslav Tykhiy while (fflush(outstr) == EOF) { 21164cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 21174cd51076SYaroslav Tykhiy else 2118ea022d16SRodney W. Grimes goto data_err; 21194cd51076SYaroslav Tykhiy clearerr(outstr); 21204cd51076SYaroslav Tykhiy } 21214cd51076SYaroslav Tykhiy ENDXFER; 21224cd51076SYaroslav Tykhiy #else 21234cd51076SYaroslav Tykhiy ENDXFER; 21244cd51076SYaroslav Tykhiy if (fflush(outstr) == EOF) 21254cd51076SYaroslav Tykhiy goto data_err; 21264cd51076SYaroslav Tykhiy #endif 2127ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 21284b82fc95SYaroslav Tykhiy return (0); 2129ea022d16SRodney W. Grimes 2130ea022d16SRodney W. Grimes case TYPE_I: 2131ea022d16SRodney W. Grimes case TYPE_L: 21329fc5823aSGarrett Wollman /* 21339fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 21349fc5823aSGarrett Wollman * are sending a regular file 21359fc5823aSGarrett Wollman */ 21369fc5823aSGarrett Wollman netfd = fileno(outstr); 21379fc5823aSGarrett Wollman filefd = fileno(instr); 21389fc5823aSGarrett Wollman 2139f6f0c4b9SDan Moschuk if (isreg) { 21402e22b914SYaroslav Tykhiy char *msg = "Transfer complete."; 21414cd51076SYaroslav Tykhiy off_t cnt, offset; 2142f6f0c4b9SDan Moschuk int err; 2143f6f0c4b9SDan Moschuk 21442f492fc8SYaroslav Tykhiy cnt = offset = 0; 2145f6f0c4b9SDan Moschuk 21462f492fc8SYaroslav Tykhiy while (filesize > 0) { 2147492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0, 21487e295315SYaroslav Tykhiy NULL, &cnt, 0); 21493af48c42SMaxim Konovalov /* 21503af48c42SMaxim Konovalov * Calculate byte_count before OOB processing. 21513af48c42SMaxim Konovalov * It can be used in myoob() later. 21523af48c42SMaxim Konovalov */ 21533af48c42SMaxim Konovalov byte_count += cnt; 2154f6f0c4b9SDan Moschuk offset += cnt; 2155492f1d9cSMaxim Konovalov filesize -= cnt; 21564cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 21574cd51076SYaroslav Tykhiy else if (err == -1) { 21584cd51076SYaroslav Tykhiy if (errno != EINTR && 21594cd51076SYaroslav Tykhiy cnt == 0 && offset == 0) 2160f6f0c4b9SDan Moschuk goto oldway; 21619fc5823aSGarrett Wollman goto data_err; 2162f6f0c4b9SDan Moschuk } 21634cd51076SYaroslav Tykhiy if (err == -1) /* resume after OOB */ 21644cd51076SYaroslav Tykhiy continue; 21652e22b914SYaroslav Tykhiy /* 21662e22b914SYaroslav Tykhiy * We hit the EOF prematurely. 21672e22b914SYaroslav Tykhiy * Perhaps the file was externally truncated. 21682e22b914SYaroslav Tykhiy */ 21692e22b914SYaroslav Tykhiy if (cnt == 0) { 21702e22b914SYaroslav Tykhiy msg = "Transfer finished due to " 21712e22b914SYaroslav Tykhiy "premature end of file."; 21722e22b914SYaroslav Tykhiy break; 21732e22b914SYaroslav Tykhiy } 2174f6f0c4b9SDan Moschuk } 21754cd51076SYaroslav Tykhiy ENDXFER; 217662f390ecSEd Maste reply(226, "%s", msg); 21774b82fc95SYaroslav Tykhiy return (0); 21789fc5823aSGarrett Wollman } 21799fc5823aSGarrett Wollman 21809fc5823aSGarrett Wollman oldway: 2181e3765043SYaroslav Tykhiy if ((buf = malloc(blksize)) == NULL) { 21824cd51076SYaroslav Tykhiy ENDXFER; 218302c97492SYaroslav Tykhiy reply(451, "Ran out of memory."); 21844b82fc95SYaroslav Tykhiy return (-1); 2185ea022d16SRodney W. Grimes } 21869fc5823aSGarrett Wollman 21874cd51076SYaroslav Tykhiy for (;;) { 21884cd51076SYaroslav Tykhiy int cnt, len; 21894cd51076SYaroslav Tykhiy char *bp; 21904cd51076SYaroslav Tykhiy 21914cd51076SYaroslav Tykhiy cnt = read(filefd, buf, blksize); 21924cd51076SYaroslav Tykhiy CHECKOOB(free(buf); return (-1)) 21934cd51076SYaroslav Tykhiy else if (cnt < 0) { 21948efc8b18SYaroslav Tykhiy free(buf); 2195ea022d16SRodney W. Grimes goto file_err; 21964cd51076SYaroslav Tykhiy } 21974cd51076SYaroslav Tykhiy if (cnt < 0) /* resume after OOB */ 21984cd51076SYaroslav Tykhiy continue; 21994cd51076SYaroslav Tykhiy if (cnt == 0) /* EOF */ 22004cd51076SYaroslav Tykhiy break; 22014cd51076SYaroslav Tykhiy for (len = cnt, bp = buf; len > 0;) { 22024cd51076SYaroslav Tykhiy cnt = write(netfd, bp, len); 22034cd51076SYaroslav Tykhiy CHECKOOB(free(buf); return (-1)) 22044cd51076SYaroslav Tykhiy else if (cnt < 0) { 22054cd51076SYaroslav Tykhiy free(buf); 2206ea022d16SRodney W. Grimes goto data_err; 2207ea022d16SRodney W. Grimes } 22084cd51076SYaroslav Tykhiy if (cnt <= 0) 22094cd51076SYaroslav Tykhiy continue; 22104cd51076SYaroslav Tykhiy len -= cnt; 22114cd51076SYaroslav Tykhiy bp += cnt; 22124cd51076SYaroslav Tykhiy byte_count += cnt; 22134cd51076SYaroslav Tykhiy } 22144cd51076SYaroslav Tykhiy } 22154cd51076SYaroslav Tykhiy ENDXFER; 22164cd51076SYaroslav Tykhiy free(buf); 2217ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 22184b82fc95SYaroslav Tykhiy return (0); 2219ea022d16SRodney W. Grimes default: 22204cd51076SYaroslav Tykhiy ENDXFER; 222102c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in send_data.", type); 22224b82fc95SYaroslav Tykhiy return (-1); 2223ea022d16SRodney W. Grimes } 2224ea022d16SRodney W. Grimes 2225ea022d16SRodney W. Grimes data_err: 22264cd51076SYaroslav Tykhiy ENDXFER; 2227ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 22284b82fc95SYaroslav Tykhiy return (-1); 2229ea022d16SRodney W. Grimes 2230ea022d16SRodney W. Grimes file_err: 22314cd51076SYaroslav Tykhiy ENDXFER; 2232ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 22334b82fc95SYaroslav Tykhiy return (-1); 2234ea022d16SRodney W. Grimes } 2235ea022d16SRodney W. Grimes 2236ea022d16SRodney W. Grimes /* 2237ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 2238ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 2239ea022d16SRodney W. Grimes * 2240ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 2241ea022d16SRodney W. Grimes */ 2242ea022d16SRodney W. Grimes static int 2243e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr) 2244ea022d16SRodney W. Grimes { 22454cd51076SYaroslav Tykhiy int c, cp; 22464cd51076SYaroslav Tykhiy int bare_lfs = 0; 2247ea022d16SRodney W. Grimes 22484cd51076SYaroslav Tykhiy STARTXFER; 224961f891a6SPaul Traina 2250ea022d16SRodney W. Grimes switch (type) { 2251ea022d16SRodney W. Grimes 2252ea022d16SRodney W. Grimes case TYPE_I: 2253ea022d16SRodney W. Grimes case TYPE_L: 22544cd51076SYaroslav Tykhiy for (;;) { 22554cd51076SYaroslav Tykhiy int cnt, len; 22564cd51076SYaroslav Tykhiy char *bp; 22574cd51076SYaroslav Tykhiy char buf[BUFSIZ]; 22584cd51076SYaroslav Tykhiy 22594cd51076SYaroslav Tykhiy cnt = read(fileno(instr), buf, sizeof(buf)); 22604cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22614cd51076SYaroslav Tykhiy else if (cnt < 0) 22624cd51076SYaroslav Tykhiy goto data_err; 22634cd51076SYaroslav Tykhiy if (cnt < 0) /* resume after OOB */ 22644cd51076SYaroslav Tykhiy continue; 22654cd51076SYaroslav Tykhiy if (cnt == 0) /* EOF */ 22664cd51076SYaroslav Tykhiy break; 22674cd51076SYaroslav Tykhiy for (len = cnt, bp = buf; len > 0;) { 22684cd51076SYaroslav Tykhiy cnt = write(fileno(outstr), bp, len); 22694cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22704cd51076SYaroslav Tykhiy else if (cnt < 0) 2271ea022d16SRodney W. Grimes goto file_err; 22724cd51076SYaroslav Tykhiy if (cnt <= 0) 22734cd51076SYaroslav Tykhiy continue; 22744cd51076SYaroslav Tykhiy len -= cnt; 22754cd51076SYaroslav Tykhiy bp += cnt; 2276ea022d16SRodney W. Grimes byte_count += cnt; 2277ea022d16SRodney W. Grimes } 22784cd51076SYaroslav Tykhiy } 22794cd51076SYaroslav Tykhiy ENDXFER; 2280ea022d16SRodney W. Grimes return (0); 2281ea022d16SRodney W. Grimes 2282ea022d16SRodney W. Grimes case TYPE_E: 22834cd51076SYaroslav Tykhiy ENDXFER; 2284ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 2285ea022d16SRodney W. Grimes return (-1); 2286ea022d16SRodney W. Grimes 2287ea022d16SRodney W. Grimes case TYPE_A: 22884cd51076SYaroslav Tykhiy cp = EOF; 22894cd51076SYaroslav Tykhiy for (;;) { 22904cd51076SYaroslav Tykhiy c = getc(instr); 22914cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22924cd51076SYaroslav Tykhiy else if (c == EOF && ferror(instr)) 22934cd51076SYaroslav Tykhiy goto data_err; 22944cd51076SYaroslav Tykhiy if (c == EOF && ferror(instr)) { /* resume after OOB */ 22954cd51076SYaroslav Tykhiy clearerr(instr); 22964cd51076SYaroslav Tykhiy continue; 22974cd51076SYaroslav Tykhiy } 22984cd51076SYaroslav Tykhiy 22994cd51076SYaroslav Tykhiy if (cp == '\r') { 23004cd51076SYaroslav Tykhiy if (c != '\n') 23014cd51076SYaroslav Tykhiy FTPD_PUTC('\r', outstr, file_err); 23024cd51076SYaroslav Tykhiy } else 2303ea022d16SRodney W. Grimes if (c == '\n') 2304ea022d16SRodney W. Grimes bare_lfs++; 23054cd51076SYaroslav Tykhiy if (c == '\r') { 23064cd51076SYaroslav Tykhiy byte_count++; 23074cd51076SYaroslav Tykhiy cp = c; 23084cd51076SYaroslav Tykhiy continue; 23094cd51076SYaroslav Tykhiy } 23104cd51076SYaroslav Tykhiy 23114cd51076SYaroslav Tykhiy /* Check for EOF here in order not to lose last \r. */ 23124cd51076SYaroslav Tykhiy if (c == EOF) { 23134cd51076SYaroslav Tykhiy if (feof(instr)) /* EOF */ 23144cd51076SYaroslav Tykhiy break; 23154cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: impossible condition" 23164cd51076SYaroslav Tykhiy " on data stream after getc()"); 2317ea022d16SRodney W. Grimes goto data_err; 2318ea022d16SRodney W. Grimes } 23194cd51076SYaroslav Tykhiy 23204cd51076SYaroslav Tykhiy byte_count++; 23214cd51076SYaroslav Tykhiy FTPD_PUTC(c, outstr, file_err); 23224cd51076SYaroslav Tykhiy cp = c; 2323ea022d16SRodney W. Grimes } 23244cd51076SYaroslav Tykhiy #ifdef notyet /* BSD stdio isn't ready for that */ 23254cd51076SYaroslav Tykhiy while (fflush(outstr) == EOF) { 23264cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 23274cd51076SYaroslav Tykhiy else 2328ea022d16SRodney W. Grimes goto file_err; 23294cd51076SYaroslav Tykhiy clearerr(outstr); 23304cd51076SYaroslav Tykhiy } 23314cd51076SYaroslav Tykhiy ENDXFER; 23324cd51076SYaroslav Tykhiy #else 23334cd51076SYaroslav Tykhiy ENDXFER; 23344cd51076SYaroslav Tykhiy if (fflush(outstr) == EOF) 23354cd51076SYaroslav Tykhiy goto file_err; 23364cd51076SYaroslav Tykhiy #endif 2337ea022d16SRodney W. Grimes if (bare_lfs) { 2338ea022d16SRodney W. Grimes lreply(226, 233902c97492SYaroslav Tykhiy "WARNING! %d bare linefeeds received in ASCII mode.", 2340ea022d16SRodney W. Grimes bare_lfs); 2341ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 2342ea022d16SRodney W. Grimes } 2343ea022d16SRodney W. Grimes return (0); 2344ea022d16SRodney W. Grimes default: 23454cd51076SYaroslav Tykhiy ENDXFER; 234602c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in receive_data.", type); 2347ea022d16SRodney W. Grimes return (-1); 2348ea022d16SRodney W. Grimes } 2349ea022d16SRodney W. Grimes 2350ea022d16SRodney W. Grimes data_err: 23514cd51076SYaroslav Tykhiy ENDXFER; 235202c97492SYaroslav Tykhiy perror_reply(426, "Data connection"); 2353ea022d16SRodney W. Grimes return (-1); 2354ea022d16SRodney W. Grimes 2355ea022d16SRodney W. Grimes file_err: 23564cd51076SYaroslav Tykhiy ENDXFER; 235702c97492SYaroslav Tykhiy perror_reply(452, "Error writing to file"); 2358ea022d16SRodney W. Grimes return (-1); 2359ea022d16SRodney W. Grimes } 2360ea022d16SRodney W. Grimes 2361ea022d16SRodney W. Grimes void 2362e4bc453cSWarner Losh statfilecmd(char *filename) 2363ea022d16SRodney W. Grimes { 2364ea022d16SRodney W. Grimes FILE *fin; 2365f8a581a0SYaroslav Tykhiy int atstart; 236641c57b48SYaroslav Tykhiy int c, code; 2367ea022d16SRodney W. Grimes char line[LINE_MAX]; 236841c57b48SYaroslav Tykhiy struct stat st; 2369ea022d16SRodney W. Grimes 237041c57b48SYaroslav Tykhiy code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213; 2371af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2372ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 2373763e8c96SEd Maste if (fin == NULL) { 2374763e8c96SEd Maste perror_reply(551, filename); 2375763e8c96SEd Maste return; 2376763e8c96SEd Maste } 23773b48b877SYaroslav Tykhiy lreply(code, "Status of %s:", filename); 2378f8a581a0SYaroslav Tykhiy atstart = 1; 2379ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 2380ea022d16SRodney W. Grimes if (c == '\n') { 2381ea022d16SRodney W. Grimes if (ferror(stdout)){ 238202c97492SYaroslav Tykhiy perror_reply(421, "Control connection"); 2383ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2384ea022d16SRodney W. Grimes dologout(1); 2385ea022d16SRodney W. Grimes /* NOTREACHED */ 2386ea022d16SRodney W. Grimes } 2387ea022d16SRodney W. Grimes if (ferror(fin)) { 2388ea022d16SRodney W. Grimes perror_reply(551, filename); 2389ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2390ea022d16SRodney W. Grimes return; 2391ea022d16SRodney W. Grimes } 2392ea022d16SRodney W. Grimes (void) putc('\r', stdout); 2393ea022d16SRodney W. Grimes } 2394f8a581a0SYaroslav Tykhiy /* 2395f8a581a0SYaroslav Tykhiy * RFC 959 says neutral text should be prepended before 2396f8a581a0SYaroslav Tykhiy * a leading 3-digit number followed by whitespace, but 2397f8a581a0SYaroslav Tykhiy * many ftp clients can be confused by any leading digits, 2398f8a581a0SYaroslav Tykhiy * as a matter of fact. 2399f8a581a0SYaroslav Tykhiy */ 2400f8a581a0SYaroslav Tykhiy if (atstart && isdigit(c)) 2401f8a581a0SYaroslav Tykhiy (void) putc(' ', stdout); 2402ea022d16SRodney W. Grimes (void) putc(c, stdout); 2403f8a581a0SYaroslav Tykhiy atstart = (c == '\n'); 2404ea022d16SRodney W. Grimes } 2405ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 240602c97492SYaroslav Tykhiy reply(code, "End of status."); 2407ea022d16SRodney W. Grimes } 2408ea022d16SRodney W. Grimes 2409ea022d16SRodney W. Grimes void 2410e4bc453cSWarner Losh statcmd(void) 2411ea022d16SRodney W. Grimes { 24124dd8b5abSYoshinobu Inoue union sockunion *su; 2413ea022d16SRodney W. Grimes u_char *a, *p; 2414b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST]; 24154dd8b5abSYoshinobu Inoue int ispassive; 2416ea022d16SRodney W. Grimes 2417c152df28SYaroslav Tykhiy if (hostinfo) { 2418a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname); 2419ea022d16SRodney W. Grimes printf(" %s\r\n", version); 2420c152df28SYaroslav Tykhiy } else 2421c152df28SYaroslav Tykhiy lreply(211, "FTP server status:"); 2422ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 24234dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2424b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 242504683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0; 24264dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 24274dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 24284dd8b5abSYoshinobu Inoue } 2429ea022d16SRodney W. Grimes printf("\r\n"); 2430ea022d16SRodney W. Grimes if (logged_in) { 2431ea022d16SRodney W. Grimes if (guest) 2432ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 2433ea022d16SRodney W. Grimes else 2434ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 2435ea022d16SRodney W. Grimes } else if (askpasswd) 2436ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 2437ea022d16SRodney W. Grimes else 2438ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 2439ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 2440ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 2441ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2442ea022d16SRodney W. Grimes if (type == TYPE_L) 244389fdc4e1SMike Barcroft #if CHAR_BIT == 8 244489fdc4e1SMike Barcroft printf(" %d", CHAR_BIT); 2445ea022d16SRodney W. Grimes #else 2446ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2447ea022d16SRodney W. Grimes #endif 2448ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2449ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2450ea022d16SRodney W. Grimes if (data != -1) 2451ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2452ea022d16SRodney W. Grimes else if (pdata != -1) { 24534dd8b5abSYoshinobu Inoue ispassive = 1; 24544dd8b5abSYoshinobu Inoue su = &pasv_addr; 2455ea022d16SRodney W. Grimes goto printaddr; 2456ea022d16SRodney W. Grimes } else if (usedefault == 0) { 24574dd8b5abSYoshinobu Inoue ispassive = 0; 24584dd8b5abSYoshinobu Inoue su = &data_dest; 2459ea022d16SRodney W. Grimes printaddr: 2460ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 24614dd8b5abSYoshinobu Inoue if (epsvall) { 24624dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 24634dd8b5abSYoshinobu Inoue goto epsvonly; 24644dd8b5abSYoshinobu Inoue } 24654dd8b5abSYoshinobu Inoue 24664dd8b5abSYoshinobu Inoue /* PORT/PASV */ 24674dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 24684dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 24694dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 24704dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 24714dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 24724dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 24734dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 24744dd8b5abSYoshinobu Inoue } 24754dd8b5abSYoshinobu Inoue 24764dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 24774dd8b5abSYoshinobu Inoue { 24784dd8b5abSYoshinobu Inoue int alen, af, i; 24794dd8b5abSYoshinobu Inoue 24804dd8b5abSYoshinobu Inoue switch (su->su_family) { 24814dd8b5abSYoshinobu Inoue case AF_INET: 24824dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 24834dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 24844dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 24854dd8b5abSYoshinobu Inoue af = 4; 24864dd8b5abSYoshinobu Inoue break; 24874dd8b5abSYoshinobu Inoue case AF_INET6: 24884dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 24894dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 24904dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 24914dd8b5abSYoshinobu Inoue af = 6; 24924dd8b5abSYoshinobu Inoue break; 24934dd8b5abSYoshinobu Inoue default: 24944dd8b5abSYoshinobu Inoue af = 0; 24954dd8b5abSYoshinobu Inoue break; 24964dd8b5abSYoshinobu Inoue } 24974dd8b5abSYoshinobu Inoue if (af) { 24984dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 24994dd8b5abSYoshinobu Inoue af, alen); 25004dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 25014dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 25024dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 25034dd8b5abSYoshinobu Inoue } 25044dd8b5abSYoshinobu Inoue } 25054dd8b5abSYoshinobu Inoue 25064dd8b5abSYoshinobu Inoue epsvonly:; 25074dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 25084dd8b5abSYoshinobu Inoue { 25094dd8b5abSYoshinobu Inoue int af; 25104dd8b5abSYoshinobu Inoue 25114dd8b5abSYoshinobu Inoue switch (su->su_family) { 25124dd8b5abSYoshinobu Inoue case AF_INET: 25134dd8b5abSYoshinobu Inoue af = 1; 25144dd8b5abSYoshinobu Inoue break; 25154dd8b5abSYoshinobu Inoue case AF_INET6: 25164dd8b5abSYoshinobu Inoue af = 2; 25174dd8b5abSYoshinobu Inoue break; 25184dd8b5abSYoshinobu Inoue default: 25194dd8b5abSYoshinobu Inoue af = 0; 25204dd8b5abSYoshinobu Inoue break; 25214dd8b5abSYoshinobu Inoue } 25224dd8b5abSYoshinobu Inoue if (af) { 2523b0f06defSHajimu UMEMOTO union sockunion tmp; 2524b0f06defSHajimu UMEMOTO 2525b0f06defSHajimu UMEMOTO tmp = *su; 2526b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6) 2527b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0; 2528b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 25294dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 25304dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 253104683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0; 25324dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 25334dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 2534b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port)); 25354dd8b5abSYoshinobu Inoue } 25364dd8b5abSYoshinobu Inoue } 25374dd8b5abSYoshinobu Inoue } 2538ea022d16SRodney W. Grimes #undef UC 2539ea022d16SRodney W. Grimes } else 2540ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 254102c97492SYaroslav Tykhiy reply(211, "End of status."); 2542ea022d16SRodney W. Grimes } 2543ea022d16SRodney W. Grimes 2544ea022d16SRodney W. Grimes void 2545e4bc453cSWarner Losh fatalerror(char *s) 2546ea022d16SRodney W. Grimes { 2547ea022d16SRodney W. Grimes 25484a3e5acdSYaroslav Tykhiy reply(451, "Error in server: %s", s); 2549ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2550ea022d16SRodney W. Grimes dologout(0); 2551ea022d16SRodney W. Grimes /* NOTREACHED */ 2552ea022d16SRodney W. Grimes } 2553ea022d16SRodney W. Grimes 2554ea022d16SRodney W. Grimes void 2555ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2556ea022d16SRodney W. Grimes { 2557ea022d16SRodney W. Grimes va_list ap; 2558e4bc453cSWarner Losh 2559ea022d16SRodney W. Grimes (void)printf("%d ", n); 25609cbb335cSTim J. Robbins va_start(ap, fmt); 2561ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 25629cbb335cSTim J. Robbins va_end(ap); 2563ea022d16SRodney W. Grimes (void)printf("\r\n"); 2564ea022d16SRodney W. Grimes (void)fflush(stdout); 2565618b0bbaSMark Murray if (ftpdebug) { 2566ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 25679cbb335cSTim J. Robbins va_start(ap, fmt); 2568ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 25699cbb335cSTim J. Robbins va_end(ap); 2570ea022d16SRodney W. Grimes } 2571ea022d16SRodney W. Grimes } 2572ea022d16SRodney W. Grimes 2573ea022d16SRodney W. Grimes void 2574ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2575ea022d16SRodney W. Grimes { 2576ea022d16SRodney W. Grimes va_list ap; 2577e4bc453cSWarner Losh 2578ea022d16SRodney W. Grimes (void)printf("%d- ", n); 25799cbb335cSTim J. Robbins va_start(ap, fmt); 2580ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 25819cbb335cSTim J. Robbins va_end(ap); 2582ea022d16SRodney W. Grimes (void)printf("\r\n"); 2583ea022d16SRodney W. Grimes (void)fflush(stdout); 2584618b0bbaSMark Murray if (ftpdebug) { 2585ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 25869cbb335cSTim J. Robbins va_start(ap, fmt); 2587ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 25889cbb335cSTim J. Robbins va_end(ap); 2589ea022d16SRodney W. Grimes } 2590ea022d16SRodney W. Grimes } 2591ea022d16SRodney W. Grimes 2592ea022d16SRodney W. Grimes static void 2593e4bc453cSWarner Losh ack(char *s) 2594ea022d16SRodney W. Grimes { 2595ea022d16SRodney W. Grimes 2596ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2597ea022d16SRodney W. Grimes } 2598ea022d16SRodney W. Grimes 2599ea022d16SRodney W. Grimes void 2600e4bc453cSWarner Losh nack(char *s) 2601ea022d16SRodney W. Grimes { 2602ea022d16SRodney W. Grimes 2603ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2604ea022d16SRodney W. Grimes } 2605ea022d16SRodney W. Grimes 2606ea022d16SRodney W. Grimes /* ARGSUSED */ 2607ea022d16SRodney W. Grimes void 2608e4bc453cSWarner Losh yyerror(char *s) 2609ea022d16SRodney W. Grimes { 2610ea022d16SRodney W. Grimes char *cp; 2611ea022d16SRodney W. Grimes 26129aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2613ea022d16SRodney W. Grimes *cp = '\0'; 261402c97492SYaroslav Tykhiy reply(500, "%s: command not understood.", cbuf); 2615ea022d16SRodney W. Grimes } 2616ea022d16SRodney W. Grimes 2617ea022d16SRodney W. Grimes void 2618e4bc453cSWarner Losh delete(char *name) 2619ea022d16SRodney W. Grimes { 2620ea022d16SRodney W. Grimes struct stat st; 2621ea022d16SRodney W. Grimes 2622ea022d16SRodney W. Grimes LOGCMD("delete", name); 26231b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2624ea022d16SRodney W. Grimes perror_reply(550, name); 2625ea022d16SRodney W. Grimes return; 2626ea022d16SRodney W. Grimes } 2627ac4f2391SYaroslav Tykhiy if (S_ISDIR(st.st_mode)) { 2628ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2629ea022d16SRodney W. Grimes perror_reply(550, name); 2630ea022d16SRodney W. Grimes return; 2631ea022d16SRodney W. Grimes } 2632ea022d16SRodney W. Grimes goto done; 2633ea022d16SRodney W. Grimes } 26343f8b9cfeSYaroslav Tykhiy if (guest && noguestmod) { 263502c97492SYaroslav Tykhiy reply(550, "Operation not permitted."); 26363f8b9cfeSYaroslav Tykhiy return; 26373f8b9cfeSYaroslav Tykhiy } 26383f8b9cfeSYaroslav Tykhiy if (unlink(name) < 0) { 2639ea022d16SRodney W. Grimes perror_reply(550, name); 2640ea022d16SRodney W. Grimes return; 2641ea022d16SRodney W. Grimes } 2642ea022d16SRodney W. Grimes done: 2643ea022d16SRodney W. Grimes ack("DELE"); 2644ea022d16SRodney W. Grimes } 2645ea022d16SRodney W. Grimes 2646ea022d16SRodney W. Grimes void 2647e4bc453cSWarner Losh cwd(char *path) 2648ea022d16SRodney W. Grimes { 2649ea022d16SRodney W. Grimes 2650ea022d16SRodney W. Grimes if (chdir(path) < 0) 2651ea022d16SRodney W. Grimes perror_reply(550, path); 2652ea022d16SRodney W. Grimes else 2653ea022d16SRodney W. Grimes ack("CWD"); 2654ea022d16SRodney W. Grimes } 2655ea022d16SRodney W. Grimes 2656ea022d16SRodney W. Grimes void 2657e4bc453cSWarner Losh makedir(char *name) 2658ea022d16SRodney W. Grimes { 26592b748987SYaroslav Tykhiy char *s; 2660ea022d16SRodney W. Grimes 2661ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2662d186bb12SMatthew N. Dodd if (guest && noguestmkd) 2663405e2987SYaroslav Tykhiy reply(550, "Operation not permitted."); 2664d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0) 2665ea022d16SRodney W. Grimes perror_reply(550, name); 26662b748987SYaroslav Tykhiy else { 26672b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL) 26682b748987SYaroslav Tykhiy fatalerror("Ran out of memory."); 26692b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s); 26702b748987SYaroslav Tykhiy free(s); 26712b748987SYaroslav Tykhiy } 2672ea022d16SRodney W. Grimes } 2673ea022d16SRodney W. Grimes 2674ea022d16SRodney W. Grimes void 2675e4bc453cSWarner Losh removedir(char *name) 2676ea022d16SRodney W. Grimes { 2677ea022d16SRodney W. Grimes 2678ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2679ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2680ea022d16SRodney W. Grimes perror_reply(550, name); 2681ea022d16SRodney W. Grimes else 2682ea022d16SRodney W. Grimes ack("RMD"); 2683ea022d16SRodney W. Grimes } 2684ea022d16SRodney W. Grimes 2685ea022d16SRodney W. Grimes void 2686e4bc453cSWarner Losh pwd(void) 2687ea022d16SRodney W. Grimes { 2688e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1]; 2689ea022d16SRodney W. Grimes 2690de9b6c03SYaroslav Tykhiy if (getcwd(path, sizeof(path)) == NULL) 269175933089SYaroslav Tykhiy perror_reply(550, "Get current directory"); 2692e4648f05SYaroslav Tykhiy else { 2693e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL) 2694e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory."); 2695e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s); 2696e4648f05SYaroslav Tykhiy free(s); 2697e4648f05SYaroslav Tykhiy } 2698ea022d16SRodney W. Grimes } 2699ea022d16SRodney W. Grimes 2700ea022d16SRodney W. Grimes char * 2701e4bc453cSWarner Losh renamefrom(char *name) 2702ea022d16SRodney W. Grimes { 2703ea022d16SRodney W. Grimes struct stat st; 2704ea022d16SRodney W. Grimes 2705b943b3c4SYaroslav Tykhiy if (guest && noguestmod) { 270602c97492SYaroslav Tykhiy reply(550, "Operation not permitted."); 2707b943b3c4SYaroslav Tykhiy return (NULL); 2708b943b3c4SYaroslav Tykhiy } 27091b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2710ea022d16SRodney W. Grimes perror_reply(550, name); 2711385f9bf0SYaroslav Tykhiy return (NULL); 2712ea022d16SRodney W. Grimes } 271302c97492SYaroslav Tykhiy reply(350, "File exists, ready for destination name."); 2714ea022d16SRodney W. Grimes return (name); 2715ea022d16SRodney W. Grimes } 2716ea022d16SRodney W. Grimes 2717ea022d16SRodney W. Grimes void 2718e4bc453cSWarner Losh renamecmd(char *from, char *to) 2719ea022d16SRodney W. Grimes { 272061f891a6SPaul Traina struct stat st; 2721ea022d16SRodney W. Grimes 2722ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 272361f891a6SPaul Traina 272461f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 272502c97492SYaroslav Tykhiy reply(550, "%s: permission denied.", to); 272661f891a6SPaul Traina return; 272761f891a6SPaul Traina } 272861f891a6SPaul Traina 2729ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2730ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2731ea022d16SRodney W. Grimes else 2732ea022d16SRodney W. Grimes ack("RNTO"); 2733ea022d16SRodney W. Grimes } 2734ea022d16SRodney W. Grimes 2735ea022d16SRodney W. Grimes static void 2736e4bc453cSWarner Losh dolog(struct sockaddr *who) 2737ea022d16SRodney W. Grimes { 27387cdd3cb7SYaroslav Tykhiy char who_name[NI_MAXHOST]; 27394dd8b5abSYoshinobu Inoue 27404dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 274104683b2cSYaroslav Tykhiy remotehost[sizeof(remotehost) - 1] = 0; 27427cdd3cb7SYaroslav Tykhiy if (getnameinfo(who, who->sa_len, 27437cdd3cb7SYaroslav Tykhiy who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST)) 27447cdd3cb7SYaroslav Tykhiy *who_name = 0; 27457cdd3cb7SYaroslav Tykhiy who_name[sizeof(who_name) - 1] = 0; 2746ea022d16SRodney W. Grimes 2747ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2748ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2749ea4e54b9SDavid Nugent if (thishost != firsthost) 2750ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2751ea4e54b9SDavid Nugent remotehost, hostname); 2752ea4e54b9SDavid Nugent else 2753ea4e54b9SDavid Nugent #endif 2754ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2755ea4e54b9SDavid Nugent remotehost); 2756b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2757ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2758ea022d16SRodney W. Grimes 2759ea4e54b9SDavid Nugent if (logging) { 2760ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2761ea4e54b9SDavid Nugent if (thishost != firsthost) 27627cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s) to %s", 27637cdd3cb7SYaroslav Tykhiy remotehost, who_name, hostname); 2764ea4e54b9SDavid Nugent else 2765ea4e54b9SDavid Nugent #endif 27667cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s)", 27677cdd3cb7SYaroslav Tykhiy remotehost, who_name); 2768ea022d16SRodney W. Grimes } 2769ea4e54b9SDavid Nugent } 2770ea022d16SRodney W. Grimes 2771ea022d16SRodney W. Grimes /* 2772ea022d16SRodney W. Grimes * Record logout in wtmp file 2773ea022d16SRodney W. Grimes * and exit with supplied status. 2774ea022d16SRodney W. Grimes */ 2775ea022d16SRodney W. Grimes void 2776e4bc453cSWarner Losh dologout(int status) 2777ea022d16SRodney W. Grimes { 2778ea022d16SRodney W. Grimes 27799f37b1a2SEd Schouten if (logged_in && dowtmp) { 278052e7ee74SYaroslav Tykhiy (void) seteuid(0); 2781*de8d85c9SEugene Grosbein #ifdef LOGIN_CAP 2782*de8d85c9SEugene Grosbein setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN | 2783*de8d85c9SEugene Grosbein LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH | 2784*de8d85c9SEugene Grosbein LOGIN_SETENV)); 2785*de8d85c9SEugene Grosbein #endif 27869f37b1a2SEd Schouten ftpd_logwtmp(wtmpid, NULL, NULL); 2787ea022d16SRodney W. Grimes } 2788ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2789ea022d16SRodney W. Grimes _exit(status); 2790ea022d16SRodney W. Grimes } 2791ea022d16SRodney W. Grimes 2792ea022d16SRodney W. Grimes static void 2793e4bc453cSWarner Losh sigurg(int signo) 2794ea022d16SRodney W. Grimes { 27954b82fc95SYaroslav Tykhiy 27964b82fc95SYaroslav Tykhiy recvurg = 1; 27974b82fc95SYaroslav Tykhiy } 27984b82fc95SYaroslav Tykhiy 27994b82fc95SYaroslav Tykhiy static void 28004cd51076SYaroslav Tykhiy maskurg(int flag) 28014cd51076SYaroslav Tykhiy { 28024cd51076SYaroslav Tykhiy int oerrno; 28034cd51076SYaroslav Tykhiy sigset_t sset; 28044cd51076SYaroslav Tykhiy 28054cd51076SYaroslav Tykhiy if (!transflag) { 28064cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: maskurg() while no transfer"); 28074cd51076SYaroslav Tykhiy return; 28084cd51076SYaroslav Tykhiy } 28094cd51076SYaroslav Tykhiy oerrno = errno; 28104cd51076SYaroslav Tykhiy sigemptyset(&sset); 28114cd51076SYaroslav Tykhiy sigaddset(&sset, SIGURG); 28124cd51076SYaroslav Tykhiy sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL); 28134cd51076SYaroslav Tykhiy errno = oerrno; 28144cd51076SYaroslav Tykhiy } 28154cd51076SYaroslav Tykhiy 28164cd51076SYaroslav Tykhiy static void 28174cd51076SYaroslav Tykhiy flagxfer(int flag) 28184cd51076SYaroslav Tykhiy { 28194cd51076SYaroslav Tykhiy 28204cd51076SYaroslav Tykhiy if (flag) { 2821f9036ce6SYaroslav Tykhiy if (transflag) 2822f9036ce6SYaroslav Tykhiy syslog(LOG_ERR, "Internal: flagxfer(1): " 2823f9036ce6SYaroslav Tykhiy "transfer already under way"); 28244cd51076SYaroslav Tykhiy transflag = 1; 282591ae7779SYaroslav Tykhiy maskurg(0); 282691ae7779SYaroslav Tykhiy recvurg = 0; 282791ae7779SYaroslav Tykhiy } else { 2828f9036ce6SYaroslav Tykhiy if (!transflag) 2829f9036ce6SYaroslav Tykhiy syslog(LOG_ERR, "Internal: flagxfer(0): " 2830f9036ce6SYaroslav Tykhiy "no active transfer"); 283191ae7779SYaroslav Tykhiy maskurg(1); 28324cd51076SYaroslav Tykhiy transflag = 0; 28334cd51076SYaroslav Tykhiy } 283491ae7779SYaroslav Tykhiy } 28354cd51076SYaroslav Tykhiy 28364cd51076SYaroslav Tykhiy /* 28374cd51076SYaroslav Tykhiy * Returns 0 if OK to resume or -1 if abort requested. 28384cd51076SYaroslav Tykhiy */ 28394cd51076SYaroslav Tykhiy static int 2840e4bc453cSWarner Losh myoob(void) 28414b82fc95SYaroslav Tykhiy { 2842ea022d16SRodney W. Grimes char *cp; 2843f0b40b1cSColin Percival int ret; 2844ea022d16SRodney W. Grimes 28454cd51076SYaroslav Tykhiy if (!transflag) { 28464cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: myoob() while no transfer"); 28474cd51076SYaroslav Tykhiy return (0); 28484cd51076SYaroslav Tykhiy } 2849ea022d16SRodney W. Grimes cp = tmpline; 2850f03ef840SBaptiste Daroussin ret = get_line(cp, 7, stdin); 2851f0b40b1cSColin Percival if (ret == -1) { 2852ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2853ea022d16SRodney W. Grimes dologout(0); 2854f0b40b1cSColin Percival } else if (ret == -2) { 2855f0b40b1cSColin Percival /* Ignore truncated command. */ 2856f0b40b1cSColin Percival return (0); 2857ea022d16SRodney W. Grimes } 2858ea022d16SRodney W. Grimes upper(cp); 2859ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2860ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2861ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 286202c97492SYaroslav Tykhiy reply(226, "Abort successful."); 28634cd51076SYaroslav Tykhiy return (-1); 2864ea022d16SRodney W. Grimes } 2865ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 28669db4bbf3SMichael Haro tmpline[0] = '\0'; 28670e519c96SYaroslav Tykhiy if (file_size != -1) 286802c97492SYaroslav Tykhiy reply(213, "Status: %jd of %jd bytes transferred.", 2869a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count, (intmax_t)file_size); 2870ea022d16SRodney W. Grimes else 287102c97492SYaroslav Tykhiy reply(213, "Status: %jd bytes transferred.", 2872a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count); 2873ea022d16SRodney W. Grimes } 28744cd51076SYaroslav Tykhiy return (0); 2875ea022d16SRodney W. Grimes } 2876ea022d16SRodney W. Grimes 2877ea022d16SRodney W. Grimes /* 2878ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2879ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2880ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2881ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2882ea022d16SRodney W. Grimes */ 2883ea022d16SRodney W. Grimes void 2884e4bc453cSWarner Losh passive(void) 2885ea022d16SRodney W. Grimes { 288678e3eed0SStefan Farfeleder socklen_t len; 288778e3eed0SStefan Farfeleder int on; 2888ea022d16SRodney W. Grimes char *p, *a; 2889ea022d16SRodney W. Grimes 289061f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 289161f891a6SPaul Traina close(pdata); 289261f891a6SPaul Traina 28934dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2894ea022d16SRodney W. Grimes if (pdata < 0) { 2895ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2896ea022d16SRodney W. Grimes return; 2897ea022d16SRodney W. Grimes } 28988af7c9a3SYaroslav Tykhiy on = 1; 28998af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 29008af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 29014c450ad7SPaul Traina 290252e7ee74SYaroslav Tykhiy (void) seteuid(0); 290361f891a6SPaul Traina 2904dacc9752SPaul Traina #ifdef IP_PORTRANGE 29054dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 29068af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 2907dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2908dacc9752SPaul Traina 290940e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 291057d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 29114c450ad7SPaul Traina goto pasv_error; 29124c450ad7SPaul Traina } 2913dacc9752SPaul Traina #endif 29142db39860SNick Sayer #ifdef IPV6_PORTRANGE 29152db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 29168af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 29172db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 29182db39860SNick Sayer 29192db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 292057d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 29212db39860SNick Sayer goto pasv_error; 29222db39860SNick Sayer } 29232db39860SNick Sayer #endif 292440e9d39eSPeter Wemm 2925ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 29264dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 29274dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2928ea022d16SRodney W. Grimes goto pasv_error; 292961f891a6SPaul Traina 293052e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 29314c450ad7SPaul Traina 2932ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2933ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2934ea022d16SRodney W. Grimes goto pasv_error; 2935ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2936ea022d16SRodney W. Grimes goto pasv_error; 29374dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 29384dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 29394dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 29404dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 29414dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 29424dd8b5abSYoshinobu Inoue else 29434dd8b5abSYoshinobu Inoue goto pasv_error; 29444dd8b5abSYoshinobu Inoue 29454dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2946ea022d16SRodney W. Grimes 2947ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2948ea022d16SRodney W. Grimes 2949ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2950ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2951ea022d16SRodney W. Grimes return; 2952ea022d16SRodney W. Grimes 2953ea022d16SRodney W. Grimes pasv_error: 295452e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 2955ea022d16SRodney W. Grimes (void) close(pdata); 2956ea022d16SRodney W. Grimes pdata = -1; 2957ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2958ea022d16SRodney W. Grimes return; 2959ea022d16SRodney W. Grimes } 2960ea022d16SRodney W. Grimes 2961ea022d16SRodney W. Grimes /* 29624dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 29634dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 29644dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 29654dd8b5abSYoshinobu Inoue */ 29664dd8b5abSYoshinobu Inoue 29674dd8b5abSYoshinobu Inoue void 2968e4bc453cSWarner Losh long_passive(char *cmd, int pf) 29694dd8b5abSYoshinobu Inoue { 297078e3eed0SStefan Farfeleder socklen_t len; 297178e3eed0SStefan Farfeleder int on; 29724dd8b5abSYoshinobu Inoue char *p, *a; 29734dd8b5abSYoshinobu Inoue 29744dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 29754dd8b5abSYoshinobu Inoue close(pdata); 29764dd8b5abSYoshinobu Inoue 29774dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 29784dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 29794dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 29804dd8b5abSYoshinobu Inoue case AF_INET: 29814dd8b5abSYoshinobu Inoue pf = 1; 29824dd8b5abSYoshinobu Inoue break; 29834dd8b5abSYoshinobu Inoue case AF_INET6: 29844dd8b5abSYoshinobu Inoue pf = 2; 29854dd8b5abSYoshinobu Inoue break; 29864dd8b5abSYoshinobu Inoue default: 29874dd8b5abSYoshinobu Inoue pf = 0; 29884dd8b5abSYoshinobu Inoue break; 29894dd8b5abSYoshinobu Inoue } 29904dd8b5abSYoshinobu Inoue /* 29914dd8b5abSYoshinobu Inoue * XXX 29924dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 29934dd8b5abSYoshinobu Inoue */ 29944dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 29954dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 29964dd8b5abSYoshinobu Inoue "use (%d)", pf); 29974dd8b5abSYoshinobu Inoue } else 299802c97492SYaroslav Tykhiy reply(501, "Network protocol mismatch."); /*XXX*/ 29994dd8b5abSYoshinobu Inoue 30004dd8b5abSYoshinobu Inoue return; 30014dd8b5abSYoshinobu Inoue } 30024dd8b5abSYoshinobu Inoue } 30034dd8b5abSYoshinobu Inoue 30044dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 30054dd8b5abSYoshinobu Inoue if (pdata < 0) { 30064dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 30074dd8b5abSYoshinobu Inoue return; 30084dd8b5abSYoshinobu Inoue } 30098af7c9a3SYaroslav Tykhiy on = 1; 30108af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 30118af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 30124dd8b5abSYoshinobu Inoue 301352e7ee74SYaroslav Tykhiy (void) seteuid(0); 30144dd8b5abSYoshinobu Inoue 30154dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 30164dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 30174dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 30184dd8b5abSYoshinobu Inoue 30192db39860SNick Sayer #ifdef IP_PORTRANGE 30202db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 30218af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 30222db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 30232db39860SNick Sayer 30242db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 302557d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 30262db39860SNick Sayer goto pasv_error; 30272db39860SNick Sayer } 30282db39860SNick Sayer #endif 30292db39860SNick Sayer #ifdef IPV6_PORTRANGE 30302db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 30318af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 30322db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 30332db39860SNick Sayer 30342db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 303557d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 30362db39860SNick Sayer goto pasv_error; 30372db39860SNick Sayer } 30382db39860SNick Sayer #endif 30392db39860SNick Sayer 30404dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 30414dd8b5abSYoshinobu Inoue goto pasv_error; 30424dd8b5abSYoshinobu Inoue 304352e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 30444dd8b5abSYoshinobu Inoue 30454dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 30464dd8b5abSYoshinobu Inoue goto pasv_error; 30474dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 30484dd8b5abSYoshinobu Inoue goto pasv_error; 30494dd8b5abSYoshinobu Inoue 30504dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 30514dd8b5abSYoshinobu Inoue 30524dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 30534dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 30544dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 30554dd8b5abSYoshinobu Inoue case AF_INET: 30564dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 30574dd8b5abSYoshinobu Inoue v4_reply: 30584dd8b5abSYoshinobu Inoue reply(228, 30594dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 30604dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 30614dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 30624dd8b5abSYoshinobu Inoue return; 30634dd8b5abSYoshinobu Inoue case AF_INET6: 30644dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 30654dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 30664dd8b5abSYoshinobu Inoue goto v4_reply; 30674dd8b5abSYoshinobu Inoue } 30684dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 30694dd8b5abSYoshinobu Inoue reply(228, 30704dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 30714dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 30724dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 30734dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 30744dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 30754dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 30764dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 30774dd8b5abSYoshinobu Inoue return; 30784dd8b5abSYoshinobu Inoue } 30794dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 30804dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 30814dd8b5abSYoshinobu Inoue case AF_INET: 30824dd8b5abSYoshinobu Inoue case AF_INET6: 30834dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 30844dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 30854dd8b5abSYoshinobu Inoue return; 30864dd8b5abSYoshinobu Inoue } 30874dd8b5abSYoshinobu Inoue } else { 30884dd8b5abSYoshinobu Inoue /* more proper error code? */ 30894dd8b5abSYoshinobu Inoue } 30904dd8b5abSYoshinobu Inoue 30914dd8b5abSYoshinobu Inoue pasv_error: 309252e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 30934dd8b5abSYoshinobu Inoue (void) close(pdata); 30944dd8b5abSYoshinobu Inoue pdata = -1; 30954dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 30964dd8b5abSYoshinobu Inoue return; 30974dd8b5abSYoshinobu Inoue } 30984dd8b5abSYoshinobu Inoue 30994dd8b5abSYoshinobu Inoue /* 3100a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local" 3101a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races. 3102a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 3103a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name. 3104a117c345SYaroslav Tykhiy * 3105ea022d16SRodney W. Grimes * Generates failure reply on error. 3106ea022d16SRodney W. Grimes */ 3107a117c345SYaroslav Tykhiy static int 3108a117c345SYaroslav Tykhiy guniquefd(char *local, char **name) 3109ea022d16SRodney W. Grimes { 3110ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 3111ea022d16SRodney W. Grimes struct stat st; 3112ea022d16SRodney W. Grimes char *cp; 3113a117c345SYaroslav Tykhiy int count; 3114a117c345SYaroslav Tykhiy int fd; 3115ea022d16SRodney W. Grimes 3116ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 3117ea022d16SRodney W. Grimes if (cp) 3118ea022d16SRodney W. Grimes *cp = '\0'; 3119ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 3120ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 3121a117c345SYaroslav Tykhiy return (-1); 3122ea022d16SRodney W. Grimes } 3123a117c345SYaroslav Tykhiy if (cp) { 3124a117c345SYaroslav Tykhiy /* 3125a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix. 3126a117c345SYaroslav Tykhiy * -4 is for /nn\0 3127a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix. 3128a117c345SYaroslav Tykhiy */ 3129a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) { 313002c97492SYaroslav Tykhiy reply(553, "Pathname too long."); 3131a117c345SYaroslav Tykhiy return (-1); 3132a117c345SYaroslav Tykhiy } 3133ea022d16SRodney W. Grimes *cp = '/'; 3134a117c345SYaroslav Tykhiy } 3135e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 3136e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 3137ea022d16SRodney W. Grimes cp = new + strlen(new); 3138a117c345SYaroslav Tykhiy /* 3139a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly. 3140a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off 3141a117c345SYaroslav Tykhiy * by buffer size. 3142a117c345SYaroslav Tykhiy */ 3143a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/') 3144ea022d16SRodney W. Grimes *cp++ = '.'; 3145a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) { 3146a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */ 3147a117c345SYaroslav Tykhiy if (count) 3148ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 3149a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local, 3150a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 3151a117c345SYaroslav Tykhiy *name = count ? new : local; 3152a117c345SYaroslav Tykhiy return (fd); 3153a117c345SYaroslav Tykhiy } 315488b70721SYaroslav Tykhiy if (errno != EEXIST) { 315550618d61SYaroslav Tykhiy perror_reply(553, count ? new : local); 315688b70721SYaroslav Tykhiy return (-1); 315788b70721SYaroslav Tykhiy } 3158ea022d16SRodney W. Grimes } 3159ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 3160a117c345SYaroslav Tykhiy return (-1); 3161ea022d16SRodney W. Grimes } 3162ea022d16SRodney W. Grimes 3163ea022d16SRodney W. Grimes /* 3164ea022d16SRodney W. Grimes * Format and send reply containing system error number. 3165ea022d16SRodney W. Grimes */ 3166ea022d16SRodney W. Grimes void 3167e4bc453cSWarner Losh perror_reply(int code, char *string) 3168ea022d16SRodney W. Grimes { 3169ea022d16SRodney W. Grimes 3170ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 3171ea022d16SRodney W. Grimes } 3172ea022d16SRodney W. Grimes 3173ea022d16SRodney W. Grimes static char *onefile[] = { 3174ea022d16SRodney W. Grimes "", 3175ea022d16SRodney W. Grimes 0 3176ea022d16SRodney W. Grimes }; 3177ea022d16SRodney W. Grimes 3178ea022d16SRodney W. Grimes void 3179e4bc453cSWarner Losh send_file_list(char *whichf) 3180ea022d16SRodney W. Grimes { 3181ea022d16SRodney W. Grimes struct stat st; 3182ea022d16SRodney W. Grimes DIR *dirp = NULL; 3183ea022d16SRodney W. Grimes struct dirent *dir; 3184ea022d16SRodney W. Grimes FILE *dout = NULL; 3185ea022d16SRodney W. Grimes char **dirlist, *dirname; 3186ea022d16SRodney W. Grimes int simple = 0; 3187ea022d16SRodney W. Grimes int freeglob = 0; 3188ea022d16SRodney W. Grimes glob_t gl; 3189ea022d16SRodney W. Grimes 3190ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 319112da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 3192ea022d16SRodney W. Grimes 3193ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 31946d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 319575dc5f1aSMike Heffner flags |= GLOB_LIMIT; 3196ea022d16SRodney W. Grimes freeglob = 1; 3197ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 319802c97492SYaroslav Tykhiy reply(550, "No matching files found."); 3199ea022d16SRodney W. Grimes goto out; 3200ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 3201ea022d16SRodney W. Grimes errno = ENOENT; 3202ea022d16SRodney W. Grimes perror_reply(550, whichf); 3203ea022d16SRodney W. Grimes goto out; 3204ea022d16SRodney W. Grimes } 3205ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 3206ea022d16SRodney W. Grimes } else { 3207ea022d16SRodney W. Grimes onefile[0] = whichf; 3208ea022d16SRodney W. Grimes dirlist = onefile; 3209ea022d16SRodney W. Grimes simple = 1; 3210ea022d16SRodney W. Grimes } 3211ea022d16SRodney W. Grimes 32129aca17cbSMark Murray while ((dirname = *dirlist++)) { 3213ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 3214ea022d16SRodney W. Grimes /* 3215ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 3216ea022d16SRodney W. Grimes * used NLST, do what the user meant. 3217ea022d16SRodney W. Grimes */ 3218ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 32194cd51076SYaroslav Tykhiy dout == NULL) 3220af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 32214cd51076SYaroslav Tykhiy else 3222ea022d16SRodney W. Grimes perror_reply(550, whichf); 3223ea022d16SRodney W. Grimes goto out; 3224ea022d16SRodney W. Grimes } 3225ea022d16SRodney W. Grimes 3226ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 3227ea022d16SRodney W. Grimes if (dout == NULL) { 32280e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w"); 3229ea022d16SRodney W. Grimes if (dout == NULL) 3230ea022d16SRodney W. Grimes goto out; 32314cd51076SYaroslav Tykhiy STARTXFER; 3232ea022d16SRodney W. Grimes } 32334cd51076SYaroslav Tykhiy START_UNSAFE; 3234ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 3235ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 32364cd51076SYaroslav Tykhiy END_UNSAFE; 32374cd51076SYaroslav Tykhiy if (ferror(dout)) 32384cd51076SYaroslav Tykhiy goto data_err; 32394cd51076SYaroslav Tykhiy byte_count += strlen(dirname) + 32404cd51076SYaroslav Tykhiy (type == TYPE_A ? 2 : 1); 32414cd51076SYaroslav Tykhiy CHECKOOB(goto abrt); 3242ea022d16SRodney W. Grimes continue; 3243ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 3244ea022d16SRodney W. Grimes continue; 3245ea022d16SRodney W. Grimes 3246ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 3247ea022d16SRodney W. Grimes continue; 3248ea022d16SRodney W. Grimes 3249ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 3250ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 3251ea022d16SRodney W. Grimes 32524cd51076SYaroslav Tykhiy CHECKOOB(goto abrt); 32534b82fc95SYaroslav Tykhiy 3254ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 3255ea022d16SRodney W. Grimes continue; 3256ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 3257ea022d16SRodney W. Grimes dir->d_namlen == 2) 3258ea022d16SRodney W. Grimes continue; 3259ea022d16SRodney W. Grimes 3260e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 3261e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 3262ea022d16SRodney W. Grimes 3263ea022d16SRodney W. Grimes /* 3264ea022d16SRodney W. Grimes * We have to do a stat to insure it's 3265ea022d16SRodney W. Grimes * not a directory or special file. 3266ea022d16SRodney W. Grimes */ 3267ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 3268ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 3269ea022d16SRodney W. Grimes if (dout == NULL) { 32700e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w"); 3271ea022d16SRodney W. Grimes if (dout == NULL) 3272ea022d16SRodney W. Grimes goto out; 32734cd51076SYaroslav Tykhiy STARTXFER; 3274ea022d16SRodney W. Grimes } 32754cd51076SYaroslav Tykhiy START_UNSAFE; 3276ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 3277ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 3278ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3279ea022d16SRodney W. Grimes else 3280ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 3281ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 32824cd51076SYaroslav Tykhiy END_UNSAFE; 32834cd51076SYaroslav Tykhiy if (ferror(dout)) 32844cd51076SYaroslav Tykhiy goto data_err; 32854cd51076SYaroslav Tykhiy byte_count += strlen(nbuf) + 32864cd51076SYaroslav Tykhiy (type == TYPE_A ? 2 : 1); 32874cd51076SYaroslav Tykhiy CHECKOOB(goto abrt); 3288ea022d16SRodney W. Grimes } 3289ea022d16SRodney W. Grimes } 3290ea022d16SRodney W. Grimes (void) closedir(dirp); 32914cd51076SYaroslav Tykhiy dirp = NULL; 3292ea022d16SRodney W. Grimes } 3293ea022d16SRodney W. Grimes 3294ea022d16SRodney W. Grimes if (dout == NULL) 3295ea022d16SRodney W. Grimes reply(550, "No files found."); 32964cd51076SYaroslav Tykhiy else if (ferror(dout)) 32974cd51076SYaroslav Tykhiy data_err: perror_reply(550, "Data connection"); 3298ea022d16SRodney W. Grimes else 3299ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 33004cd51076SYaroslav Tykhiy out: 33014cd51076SYaroslav Tykhiy if (dout) { 33024cd51076SYaroslav Tykhiy ENDXFER; 33034cd51076SYaroslav Tykhiy abrt: 3304ea022d16SRodney W. Grimes (void) fclose(dout); 3305ea022d16SRodney W. Grimes data = -1; 3306ea022d16SRodney W. Grimes pdata = -1; 33074cd51076SYaroslav Tykhiy } 33084cd51076SYaroslav Tykhiy if (dirp) 33094cd51076SYaroslav Tykhiy (void) closedir(dirp); 3310ea022d16SRodney W. Grimes if (freeglob) { 3311ea022d16SRodney W. Grimes freeglob = 0; 3312ea022d16SRodney W. Grimes globfree(&gl); 3313ea022d16SRodney W. Grimes } 3314ea022d16SRodney W. Grimes } 3315ea022d16SRodney W. Grimes 3316cf09a206SDavid Greenman void 3317e4bc453cSWarner Losh reapchild(int signo) 3318cf09a206SDavid Greenman { 3319de9b6c03SYaroslav Tykhiy while (waitpid(-1, NULL, WNOHANG) > 0); 3320cf09a206SDavid Greenman } 3321cf09a206SDavid Greenman 3322b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 3323ea022d16SRodney W. Grimes /* 3324ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 3325ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 3326ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 3327ea022d16SRodney W. Grimes */ 3328ea022d16SRodney W. Grimes void 3329ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 3330ea022d16SRodney W. Grimes { 3331ea022d16SRodney W. Grimes int i; 3332ea022d16SRodney W. Grimes va_list ap; 3333ea022d16SRodney W. Grimes char *p, *bp, ch; 3334ea022d16SRodney W. Grimes char buf[LINE_MAX]; 3335ea022d16SRodney W. Grimes 3336ea022d16SRodney W. Grimes va_start(ap, fmt); 3337ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3338ea022d16SRodney W. Grimes 3339ea022d16SRodney W. Grimes /* make ps print our process name */ 3340ea022d16SRodney W. Grimes p = Argv[0]; 3341ea022d16SRodney W. Grimes *p++ = '-'; 3342ea022d16SRodney W. Grimes 3343ea022d16SRodney W. Grimes i = strlen(buf); 3344ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 3345ea022d16SRodney W. Grimes i = LastArgv - p - 2; 3346ea022d16SRodney W. Grimes buf[i] = '\0'; 3347ea022d16SRodney W. Grimes } 3348ea022d16SRodney W. Grimes bp = buf; 3349ea022d16SRodney W. Grimes while (ch = *bp++) 3350ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 3351ea022d16SRodney W. Grimes *p++ = ch; 3352ea022d16SRodney W. Grimes while (p < LastArgv) 3353ea022d16SRodney W. Grimes *p++ = ' '; 3354ea022d16SRodney W. Grimes } 3355b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 33563eb568f2SGuido van Rooij 335761f891a6SPaul Traina static void 33586b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...) 33596b2dee6bSYaroslav Tykhiy { 33606b2dee6bSYaroslav Tykhiy va_list ap; 33616b2dee6bSYaroslav Tykhiy char *ostr, *p; 33626b2dee6bSYaroslav Tykhiy 33636b2dee6bSYaroslav Tykhiy va_start(ap, fmt); 33646b2dee6bSYaroslav Tykhiy vasprintf(&p, fmt, ap); 33656b2dee6bSYaroslav Tykhiy va_end(ap); 33666b2dee6bSYaroslav Tykhiy if (p == NULL) 33676b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory."); 33686b2dee6bSYaroslav Tykhiy if (*strp == NULL) 33696b2dee6bSYaroslav Tykhiy *strp = p; 33706b2dee6bSYaroslav Tykhiy else { 33716b2dee6bSYaroslav Tykhiy ostr = *strp; 33726b2dee6bSYaroslav Tykhiy asprintf(strp, "%s%s", ostr, p); 33736b2dee6bSYaroslav Tykhiy if (*strp == NULL) 33746b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory."); 33756b2dee6bSYaroslav Tykhiy free(ostr); 33766b2dee6bSYaroslav Tykhiy } 33776b2dee6bSYaroslav Tykhiy } 33786b2dee6bSYaroslav Tykhiy 33796b2dee6bSYaroslav Tykhiy static void 33806b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt) 33816b2dee6bSYaroslav Tykhiy { 33826b2dee6bSYaroslav Tykhiy char *msg = NULL; 33836b2dee6bSYaroslav Tykhiy char wd[MAXPATHLEN + 1]; 33846b2dee6bSYaroslav Tykhiy 33856b2dee6bSYaroslav Tykhiy if (logging <= 1) 33866b2dee6bSYaroslav Tykhiy return; 3387e897216fSYaroslav Tykhiy 33886b2dee6bSYaroslav Tykhiy if (getcwd(wd, sizeof(wd) - 1) == NULL) 33896b2dee6bSYaroslav Tykhiy strcpy(wd, strerror(errno)); 33906b2dee6bSYaroslav Tykhiy 33916b2dee6bSYaroslav Tykhiy appendf(&msg, "%s", cmd); 33926b2dee6bSYaroslav Tykhiy if (file1) 33936b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file1); 33946b2dee6bSYaroslav Tykhiy if (file2) 33956b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file2); 33966b2dee6bSYaroslav Tykhiy if (cnt >= 0) 33976b2dee6bSYaroslav Tykhiy appendf(&msg, " = %jd bytes", (intmax_t)cnt); 3398e897216fSYaroslav Tykhiy appendf(&msg, " (wd: %s", wd); 3399215a9f9dSYaroslav Tykhiy if (guest || dochroot) 3400e897216fSYaroslav Tykhiy appendf(&msg, "; chrooted"); 3401e897216fSYaroslav Tykhiy appendf(&msg, ")"); 34026b2dee6bSYaroslav Tykhiy syslog(LOG_INFO, "%s", msg); 34036b2dee6bSYaroslav Tykhiy free(msg); 34046b2dee6bSYaroslav Tykhiy } 34056b2dee6bSYaroslav Tykhiy 34066b2dee6bSYaroslav Tykhiy static void 3407e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start) 34083eb568f2SGuido van Rooij { 34098c1c21f2SYaroslav Tykhiy char buf[MAXPATHLEN + 1024]; 34103eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 3411158a00b2SJohn Birrell time_t now; 34123eb568f2SGuido van Rooij 34138c1c21f2SYaroslav Tykhiy if (statfd >= 0) { 34143eb568f2SGuido van Rooij time(&now); 34158c1c21f2SYaroslav Tykhiy if (realpath(name, path) == NULL) { 34168c1c21f2SYaroslav Tykhiy syslog(LOG_NOTICE, "realpath failed on %s: %m", path); 34178c1c21f2SYaroslav Tykhiy return; 34188c1c21f2SYaroslav Tykhiy } 34198c1c21f2SYaroslav Tykhiy snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n", 34203eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 34218c1c21f2SYaroslav Tykhiy path, (intmax_t)size, 3422e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 34233eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 34243eb568f2SGuido van Rooij } 34253eb568f2SGuido van Rooij } 3426e4648f05SYaroslav Tykhiy 3427e4648f05SYaroslav Tykhiy static char * 3428e4648f05SYaroslav Tykhiy doublequote(char *s) 3429e4648f05SYaroslav Tykhiy { 3430e4648f05SYaroslav Tykhiy int n; 3431e4648f05SYaroslav Tykhiy char *p, *s2; 3432e4648f05SYaroslav Tykhiy 3433e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++) 3434e4648f05SYaroslav Tykhiy if (*p == '"') 3435e4648f05SYaroslav Tykhiy n++; 3436e4648f05SYaroslav Tykhiy 3437e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL) 3438e4648f05SYaroslav Tykhiy return (NULL); 3439e4648f05SYaroslav Tykhiy 3440e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) { 3441e4648f05SYaroslav Tykhiy if ((*p = *s) == '"') 3442e4648f05SYaroslav Tykhiy *(++p) = '"'; 3443e4648f05SYaroslav Tykhiy } 3444e4648f05SYaroslav Tykhiy *p = '\0'; 3445e4648f05SYaroslav Tykhiy 3446e4648f05SYaroslav Tykhiy return (s2); 3447e4648f05SYaroslav Tykhiy } 3448206fe568SHajimu UMEMOTO 3449206fe568SHajimu UMEMOTO /* setup server socket for specified address family */ 3450206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */ 3451206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */ 3452206fe568SHajimu UMEMOTO static int * 3453206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport) 3454206fe568SHajimu UMEMOTO { 3455206fe568SHajimu UMEMOTO struct addrinfo hints, *res, *r; 3456206fe568SHajimu UMEMOTO int error, maxs, *s, *socks; 3457206fe568SHajimu UMEMOTO const int on = 1; 3458206fe568SHajimu UMEMOTO 3459206fe568SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 3460206fe568SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE; 3461206fe568SHajimu UMEMOTO hints.ai_family = af; 3462206fe568SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM; 3463206fe568SHajimu UMEMOTO error = getaddrinfo(bindname, bindport, &hints, &res); 3464206fe568SHajimu UMEMOTO if (error) { 3465206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", gai_strerror(error)); 3466206fe568SHajimu UMEMOTO if (error == EAI_SYSTEM) 3467206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", strerror(errno)); 3468206fe568SHajimu UMEMOTO return NULL; 3469206fe568SHajimu UMEMOTO } 3470206fe568SHajimu UMEMOTO 3471206fe568SHajimu UMEMOTO /* Count max number of sockets we may open */ 3472206fe568SHajimu UMEMOTO for (maxs = 0, r = res; r; r = r->ai_next, maxs++) 3473206fe568SHajimu UMEMOTO ; 3474206fe568SHajimu UMEMOTO socks = malloc((maxs + 1) * sizeof(int)); 3475206fe568SHajimu UMEMOTO if (!socks) { 3476206fe568SHajimu UMEMOTO freeaddrinfo(res); 3477206fe568SHajimu UMEMOTO syslog(LOG_ERR, "couldn't allocate memory for sockets"); 3478206fe568SHajimu UMEMOTO return NULL; 3479206fe568SHajimu UMEMOTO } 3480206fe568SHajimu UMEMOTO 3481206fe568SHajimu UMEMOTO *socks = 0; /* num of sockets counter at start of array */ 3482206fe568SHajimu UMEMOTO s = socks + 1; 3483206fe568SHajimu UMEMOTO for (r = res; r; r = r->ai_next) { 3484206fe568SHajimu UMEMOTO *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 3485206fe568SHajimu UMEMOTO if (*s < 0) { 3486206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control socket: %m"); 3487206fe568SHajimu UMEMOTO continue; 3488206fe568SHajimu UMEMOTO } 3489206fe568SHajimu UMEMOTO if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, 3490206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0) 3491206fe568SHajimu UMEMOTO syslog(LOG_WARNING, 3492206fe568SHajimu UMEMOTO "control setsockopt (SO_REUSEADDR): %m"); 3493206fe568SHajimu UMEMOTO if (r->ai_family == AF_INET6) { 3494206fe568SHajimu UMEMOTO if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, 3495206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0) 3496206fe568SHajimu UMEMOTO syslog(LOG_WARNING, 3497206fe568SHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m"); 3498206fe568SHajimu UMEMOTO } 3499206fe568SHajimu UMEMOTO if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 3500206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control bind: %m"); 3501206fe568SHajimu UMEMOTO close(*s); 3502206fe568SHajimu UMEMOTO continue; 3503206fe568SHajimu UMEMOTO } 3504206fe568SHajimu UMEMOTO (*socks)++; 3505206fe568SHajimu UMEMOTO s++; 3506206fe568SHajimu UMEMOTO } 3507206fe568SHajimu UMEMOTO 3508206fe568SHajimu UMEMOTO if (res) 3509206fe568SHajimu UMEMOTO freeaddrinfo(res); 3510206fe568SHajimu UMEMOTO 3511206fe568SHajimu UMEMOTO if (*socks == 0) { 3512206fe568SHajimu UMEMOTO syslog(LOG_ERR, "control socket: Couldn't bind to any socket"); 3513206fe568SHajimu UMEMOTO free(socks); 3514206fe568SHajimu UMEMOTO return NULL; 3515206fe568SHajimu UMEMOTO } 3516206fe568SHajimu UMEMOTO return(socks); 3517206fe568SHajimu UMEMOTO } 3518