1ea022d16SRodney W. Grimes /* 2ea022d16SRodney W. Grimes * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 3ea022d16SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4ea022d16SRodney W. Grimes * 5ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions 7ea022d16SRodney W. Grimes * are met: 8ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13ea022d16SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14ea022d16SRodney W. Grimes * must display the following acknowledgement: 15ea022d16SRodney W. Grimes * This product includes software developed by the University of 16ea022d16SRodney W. Grimes * California, Berkeley and its contributors. 17ea022d16SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 19ea022d16SRodney W. Grimes * without specific prior written permission. 20ea022d16SRodney W. Grimes * 21ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ea022d16SRodney W. Grimes * SUCH DAMAGE. 32ea022d16SRodney W. Grimes */ 33ea022d16SRodney W. Grimes 349aca17cbSMark Murray #if 0 35ea022d16SRodney W. Grimes #ifndef lint 36ea022d16SRodney W. Grimes static char copyright[] = 37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 38ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 39ea022d16SRodney W. Grimes #endif /* not lint */ 409aca17cbSMark Murray #endif 41ea022d16SRodney W. Grimes 42ea022d16SRodney W. Grimes #ifndef lint 43e02897faSPhilippe Charnier #if 0 44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 459aca17cbSMark Murray #endif 46e02897faSPhilippe Charnier #endif /* not lint */ 47ea022d16SRodney W. Grimes 480c4b401fSYaroslav Tykhiy #include <sys/cdefs.h> 490c4b401fSYaroslav Tykhiy __FBSDID("$FreeBSD$"); 500c4b401fSYaroslav Tykhiy 51ea022d16SRodney W. Grimes /* 52ea022d16SRodney W. Grimes * FTP server. 53ea022d16SRodney W. Grimes */ 54ea022d16SRodney W. Grimes #include <sys/param.h> 55ea022d16SRodney W. Grimes #include <sys/ioctl.h> 569fc5823aSGarrett Wollman #include <sys/mman.h> 57eb2fc780SGarrett Wollman #include <sys/socket.h> 58eb2fc780SGarrett Wollman #include <sys/stat.h> 59eb2fc780SGarrett Wollman #include <sys/time.h> 60eb2fc780SGarrett Wollman #include <sys/wait.h> 61ea022d16SRodney W. Grimes 62ea022d16SRodney W. Grimes #include <netinet/in.h> 63ea022d16SRodney W. Grimes #include <netinet/in_systm.h> 64ea022d16SRodney W. Grimes #include <netinet/ip.h> 659fc5823aSGarrett Wollman #include <netinet/tcp.h> 66ea022d16SRodney W. Grimes 67ea022d16SRodney W. Grimes #define FTP_NAMES 68ea022d16SRodney W. Grimes #include <arpa/ftp.h> 69ea022d16SRodney W. Grimes #include <arpa/inet.h> 70ea022d16SRodney W. Grimes #include <arpa/telnet.h> 71ea022d16SRodney W. Grimes 72ea022d16SRodney W. Grimes #include <ctype.h> 73ea022d16SRodney W. Grimes #include <dirent.h> 74ea022d16SRodney W. Grimes #include <err.h> 75ea022d16SRodney W. Grimes #include <errno.h> 76ea022d16SRodney W. Grimes #include <fcntl.h> 77ea022d16SRodney W. Grimes #include <glob.h> 78ea022d16SRodney W. Grimes #include <limits.h> 79ea022d16SRodney W. Grimes #include <netdb.h> 80ea022d16SRodney W. Grimes #include <pwd.h> 8131fea7b8SDavid Nugent #include <grp.h> 8247499ecdSAndrey A. Chernov #include <opie.h> 83ea022d16SRodney W. Grimes #include <signal.h> 84a57e1ef0SYaroslav Tykhiy #include <stdint.h> 85ea022d16SRodney W. Grimes #include <stdio.h> 86ea022d16SRodney W. Grimes #include <stdlib.h> 87ea022d16SRodney W. Grimes #include <string.h> 88ea022d16SRodney W. Grimes #include <syslog.h> 89ea022d16SRodney W. Grimes #include <time.h> 90ea022d16SRodney W. Grimes #include <unistd.h> 91b63e1fe2SPeter Wemm #include <libutil.h> 92b071c689SDavid Nugent #ifdef LOGIN_CAP 93b071c689SDavid Nugent #include <login_cap.h> 94b071c689SDavid Nugent #endif 95ea022d16SRodney W. Grimes 965bc9d93dSMark Murray #ifdef USE_PAM 976c9134c0SMark Murray #include <security/pam_appl.h> 986c9134c0SMark Murray #endif 996c9134c0SMark Murray 100ea022d16SRodney W. Grimes #include "pathnames.h" 101ea022d16SRodney W. Grimes #include "extern.h" 102ea022d16SRodney W. Grimes 103ea022d16SRodney W. Grimes #include <stdarg.h> 104ea022d16SRodney W. Grimes 105af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 106af85d782SDavid Nugent #undef main 107ea022d16SRodney W. Grimes 108ea022d16SRodney W. Grimes extern off_t restart_point; 109ea022d16SRodney W. Grimes extern char cbuf[]; 110ea022d16SRodney W. Grimes 1114dd8b5abSYoshinobu Inoue union sockunion ctrl_addr; 1124dd8b5abSYoshinobu Inoue union sockunion data_source; 1134dd8b5abSYoshinobu Inoue union sockunion data_dest; 1144dd8b5abSYoshinobu Inoue union sockunion his_addr; 1154dd8b5abSYoshinobu Inoue union sockunion pasv_addr; 116ea022d16SRodney W. Grimes 117cf09a206SDavid Greenman int daemon_mode; 118ea022d16SRodney W. Grimes int data; 11963591ba5SYaroslav Tykhiy int dataport; 120c152df28SYaroslav Tykhiy int hostinfo = 1; /* print host-specific info in messages */ 121ea022d16SRodney W. Grimes int logged_in; 122ea022d16SRodney W. Grimes struct passwd *pw; 123ce9287fcSYaroslav Tykhiy char *homedir; 124618b0bbaSMark Murray int ftpdebug; 125ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 126ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 127ea022d16SRodney W. Grimes int logging; 1284c450ad7SPaul Traina int restricted_data_ports = 1; 129a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1305a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 131ea022d16SRodney W. Grimes int guest; 132a5a4544eSPaul Traina int dochroot; 133215a9f9dSYaroslav Tykhiy char *chrootdir; 1345d7e0128SYaroslav Tykhiy int dowtmp = 1; 1353eb568f2SGuido van Rooij int stats; 1363eb568f2SGuido van Rooij int statfd = -1; 137ea022d16SRodney W. Grimes int type; 138ea022d16SRodney W. Grimes int form; 139ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 140ea022d16SRodney W. Grimes int mode; 141ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 142ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 143a4b77a2aSPoul-Henning Kamp int readonly = 0; /* Server is in readonly mode. */ 144a4b77a2aSPoul-Henning Kamp int noepsv = 0; /* EPSV command is disabled. */ 14562513e76SNik Clayton int noretr = 0; /* RETR command is disabled. */ 1461cc9f0bbSSheldon Hearn int noguestretr = 0; /* RETR command is disabled for anon users. */ 147d186bb12SMatthew N. Dodd int noguestmkd = 0; /* MKD command is disabled for anon users. */ 148a117c345SYaroslav Tykhiy int noguestmod = 1; /* anon users may not modify existing files. */ 14962513e76SNik Clayton 150ea022d16SRodney W. Grimes off_t file_size; 151ea022d16SRodney W. Grimes off_t byte_count; 152ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 153ea022d16SRodney W. Grimes #undef CMASK 154ea022d16SRodney W. Grimes #define CMASK 027 155ea022d16SRodney W. Grimes #endif 156ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 157ea022d16SRodney W. Grimes char tmpline[7]; 158ea4e54b9SDavid Nugent char *hostname; 159ad442344SDima Dorfman int epsvall = 0; 160ad442344SDima Dorfman 1610512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 162ea4e54b9SDavid Nugent char *ftpuser; 163ea4e54b9SDavid Nugent 164ea4e54b9SDavid Nugent static struct ftphost { 165ea4e54b9SDavid Nugent struct ftphost *next; 166f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 167ea4e54b9SDavid Nugent char *hostname; 168ea4e54b9SDavid Nugent char *anonuser; 169ea4e54b9SDavid Nugent char *statfile; 170ea4e54b9SDavid Nugent char *welcome; 171ea4e54b9SDavid Nugent char *loginmsg; 172ea4e54b9SDavid Nugent } *thishost, *firsthost; 173ea4e54b9SDavid Nugent 174ea4e54b9SDavid Nugent #endif 17504683b2cSYaroslav Tykhiy char remotehost[NI_MAXHOST]; 1763eb568f2SGuido van Rooij char *ident = NULL; 177a5a4544eSPaul Traina 178a5a4544eSPaul Traina static char ttyline[20]; 179a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 180a5a4544eSPaul Traina 1815bc9d93dSMark Murray #ifdef USE_PAM 182e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*); 1835bc9d93dSMark Murray pam_handle_t *pamh = NULL; 18447499ecdSAndrey A. Chernov #endif 185fa1746c9SMark Murray 186fa1746c9SMark Murray static struct opie opiedata; 187fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 18847499ecdSAndrey A. Chernov static int pwok; 1899aca17cbSMark Murray 190105a3c98SJulian Elischer char *pid_file = NULL; 191105a3c98SJulian Elischer 192ea022d16SRodney W. Grimes /* 1936d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 1946d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 1956d10cb2fSJonathan Lemon */ 1966d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 1976d10cb2fSJonathan Lemon # 1986d10cb2fSJonathan Lemon 1996d10cb2fSJonathan Lemon /* 200ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 201ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 202ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 203ea022d16SRodney W. Grimes */ 204ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 205ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 206ea022d16SRodney W. Grimes 207ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 208ea022d16SRodney W. Grimes int swaitint = SWAITINT; 209ea022d16SRodney W. Grimes 210ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 211b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 212ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 213ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 214b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 215ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 216ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 217ea022d16SRodney W. Grimes 2186b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file) logcmd((cmd), (file), NULL, -1) 2196b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2) logcmd((cmd), (file1), (file2), -1) 2206b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt) logcmd((cmd), (file), NULL, (cnt)) 221ea022d16SRodney W. Grimes 2224cd51076SYaroslav Tykhiy static volatile sig_atomic_t recvurg; 2234cd51076SYaroslav Tykhiy static int transflag; /* NB: for debugging only */ 2244cd51076SYaroslav Tykhiy 2254cd51076SYaroslav Tykhiy #define STARTXFER flagxfer(1) 2264cd51076SYaroslav Tykhiy #define ENDXFER flagxfer(0) 2274cd51076SYaroslav Tykhiy 2284cd51076SYaroslav Tykhiy #define START_UNSAFE maskurg(1) 2294cd51076SYaroslav Tykhiy #define END_UNSAFE maskurg(0) 2304cd51076SYaroslav Tykhiy 2314cd51076SYaroslav Tykhiy /* It's OK to put an `else' clause after this macro. */ 2324cd51076SYaroslav Tykhiy #define CHECKOOB(action) \ 2334cd51076SYaroslav Tykhiy if (recvurg) { \ 2344cd51076SYaroslav Tykhiy recvurg = 0; \ 2354cd51076SYaroslav Tykhiy if (myoob()) { \ 2364cd51076SYaroslav Tykhiy ENDXFER; \ 2374cd51076SYaroslav Tykhiy action; \ 2384cd51076SYaroslav Tykhiy } \ 2394cd51076SYaroslav Tykhiy } 2404cd51076SYaroslav Tykhiy 241ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 242e4bc453cSWarner Losh static void inithosts(void); 243e4bc453cSWarner Losh static void selecthost(union sockunion *); 244ea4e54b9SDavid Nugent #endif 245e4bc453cSWarner Losh static void ack(char *); 246e4bc453cSWarner Losh static void sigurg(int); 2474cd51076SYaroslav Tykhiy static void maskurg(int); 2484cd51076SYaroslav Tykhiy static void flagxfer(int); 2494cd51076SYaroslav Tykhiy static int myoob(void); 2508657b576SYaroslav Tykhiy static int checkuser(char *, char *, int, char **); 251e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *); 252e4bc453cSWarner Losh static void dolog(struct sockaddr *); 253e4bc453cSWarner Losh static void end_login(void); 254e4bc453cSWarner Losh static FILE *getdatasock(char *); 255a117c345SYaroslav Tykhiy static int guniquefd(char *, char **); 256e4bc453cSWarner Losh static void lostconn(int); 257e4bc453cSWarner Losh static void sigquit(int); 258e4bc453cSWarner Losh static int receive_data(FILE *, FILE *); 2596e4b0a55SYaroslav Tykhiy static int send_data(FILE *, FILE *, size_t, off_t, int); 260ea022d16SRodney W. Grimes static struct passwd * 261e4bc453cSWarner Losh sgetpwnam(char *); 262e4bc453cSWarner Losh static char *sgetsave(char *); 263e4bc453cSWarner Losh static void reapchild(int); 264eb5b2bb3SYaroslav Tykhiy static void appendf(char **, char *, ...) __printflike(2, 3); 2656b2dee6bSYaroslav Tykhiy static void logcmd(char *, char *, char *, off_t); 266e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t); 267e4648f05SYaroslav Tykhiy static char *doublequote(char *); 268206fe568SHajimu UMEMOTO static int *socksetup(int, char *, const char *); 269ea022d16SRodney W. Grimes 270ea022d16SRodney W. Grimes int 271e4bc453cSWarner Losh main(int argc, char *argv[], char **envp) 272ea022d16SRodney W. Grimes { 273ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 274ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 275ea022d16SRodney W. Grimes FILE *fd; 2764dd8b5abSYoshinobu Inoue char *bindname = NULL; 27763591ba5SYaroslav Tykhiy const char *bindport = "ftp"; 2784dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2794b82fc95SYaroslav Tykhiy struct sigaction sa; 280ea022d16SRodney W. Grimes 28134d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 2824b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask); 2834b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 28434d1ba5cSAndrey A. Chernov 285b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 286ea022d16SRodney W. Grimes /* 287ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 288ea022d16SRodney W. Grimes */ 289ea022d16SRodney W. Grimes Argv = argv; 290ea022d16SRodney W. Grimes while (*envp) 291ea022d16SRodney W. Grimes envp++; 292ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 293b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 294ea022d16SRodney W. Grimes 2956c98f401SYaroslav Tykhiy /* 2966c98f401SYaroslav Tykhiy * Prevent diagnostic messages from appearing on stderr. 2976c98f401SYaroslav Tykhiy * We run as a daemon or from inetd; in both cases, there's 2986c98f401SYaroslav Tykhiy * more reason in logging to syslog. 2996c98f401SYaroslav Tykhiy */ 3006c98f401SYaroslav Tykhiy (void) freopen(_PATH_DEVNULL, "w", stderr); 3016c98f401SYaroslav Tykhiy opterr = 0; 3026c98f401SYaroslav Tykhiy 3036c98f401SYaroslav Tykhiy /* 3046c98f401SYaroslav Tykhiy * LOG_NDELAY sets up the logging connection immediately, 3056c98f401SYaroslav Tykhiy * necessary for anonymous ftp's that chroot and can't do it later. 3066c98f401SYaroslav Tykhiy */ 3076c98f401SYaroslav Tykhiy openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 3083eb568f2SGuido van Rooij 309c152df28SYaroslav Tykhiy while ((ch = getopt(argc, argv, 310c152df28SYaroslav Tykhiy "46a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) { 311ea022d16SRodney W. Grimes switch (ch) { 3120e063efeSYaroslav Tykhiy case '4': 313206fe568SHajimu UMEMOTO family = (family == AF_INET6) ? AF_UNSPEC : AF_INET; 3140e063efeSYaroslav Tykhiy break; 3150e063efeSYaroslav Tykhiy 3160e063efeSYaroslav Tykhiy case '6': 317206fe568SHajimu UMEMOTO family = (family == AF_INET) ? AF_UNSPEC : AF_INET6; 3180e063efeSYaroslav Tykhiy break; 3190e063efeSYaroslav 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 328ea022d16SRodney W. Grimes case 'd': 329618b0bbaSMark Murray ftpdebug++; 330ea022d16SRodney W. Grimes break; 331ea022d16SRodney W. Grimes 3320e063efeSYaroslav Tykhiy case 'D': 3330e063efeSYaroslav Tykhiy daemon_mode++; 3340e063efeSYaroslav Tykhiy break; 3350e063efeSYaroslav Tykhiy 336a4b77a2aSPoul-Henning Kamp case 'E': 337a4b77a2aSPoul-Henning Kamp noepsv = 1; 338a4b77a2aSPoul-Henning Kamp break; 339a4b77a2aSPoul-Henning Kamp 340c152df28SYaroslav Tykhiy case 'h': 341c152df28SYaroslav Tykhiy hostinfo = 0; 342c152df28SYaroslav Tykhiy break; 343c152df28SYaroslav Tykhiy 344ea022d16SRodney W. Grimes case 'l': 345ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 346ea022d16SRodney W. Grimes break; 347ea022d16SRodney W. Grimes 348a117c345SYaroslav Tykhiy case 'm': 349a117c345SYaroslav Tykhiy noguestmod = 0; 350a117c345SYaroslav Tykhiy break; 351a117c345SYaroslav Tykhiy 3520e063efeSYaroslav Tykhiy case 'M': 3530e063efeSYaroslav Tykhiy noguestmkd = 1; 3540e063efeSYaroslav Tykhiy break; 3550e063efeSYaroslav Tykhiy 3560e063efeSYaroslav Tykhiy case 'o': 3570e063efeSYaroslav Tykhiy noretr = 1; 3580e063efeSYaroslav Tykhiy break; 3590e063efeSYaroslav Tykhiy 3600e063efeSYaroslav Tykhiy case 'O': 3610e063efeSYaroslav Tykhiy noguestretr = 1; 3620e063efeSYaroslav Tykhiy break; 3630e063efeSYaroslav Tykhiy 3640e063efeSYaroslav Tykhiy case 'p': 3650e063efeSYaroslav Tykhiy pid_file = optarg; 3660e063efeSYaroslav Tykhiy break; 3670e063efeSYaroslav Tykhiy 36863591ba5SYaroslav Tykhiy case 'P': 36963591ba5SYaroslav Tykhiy bindport = optarg; 37063591ba5SYaroslav Tykhiy break; 37163591ba5SYaroslav Tykhiy 372a4b77a2aSPoul-Henning Kamp case 'r': 373a4b77a2aSPoul-Henning Kamp readonly = 1; 374a4b77a2aSPoul-Henning Kamp break; 375a4b77a2aSPoul-Henning Kamp 376a5a4544eSPaul Traina case 'R': 377a5a4544eSPaul Traina paranoid = 0; 378a5a4544eSPaul Traina break; 379a5a4544eSPaul Traina 380a5a4544eSPaul Traina case 'S': 381a5a4544eSPaul Traina stats++; 382a5a4544eSPaul Traina break; 383a5a4544eSPaul Traina 384ea022d16SRodney W. Grimes case 't': 385ea022d16SRodney W. Grimes timeout = atoi(optarg); 386ea022d16SRodney W. Grimes if (maxtimeout < timeout) 387ea022d16SRodney W. Grimes maxtimeout = timeout; 388ea022d16SRodney W. Grimes break; 389a5a4544eSPaul Traina 3900e063efeSYaroslav Tykhiy case 'T': 3910e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg); 3920e063efeSYaroslav Tykhiy if (timeout > maxtimeout) 3930e063efeSYaroslav Tykhiy timeout = maxtimeout; 394105a3c98SJulian Elischer break; 395105a3c98SJulian Elischer 396ea022d16SRodney W. Grimes case 'u': 397ea022d16SRodney W. Grimes { 398ea022d16SRodney W. Grimes long val = 0; 399ea022d16SRodney W. Grimes 400ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 401ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 4026c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "bad value for -u"); 403ea022d16SRodney W. Grimes else 404ea022d16SRodney W. Grimes defumask = val; 405ea022d16SRodney W. Grimes break; 406ea022d16SRodney W. Grimes } 4070e063efeSYaroslav Tykhiy case 'U': 4080e063efeSYaroslav Tykhiy restricted_data_ports = 0; 4095a392aecSTorsten Blum break; 410ea022d16SRodney W. Grimes 411ea022d16SRodney W. Grimes case 'v': 41293bd9dc5SYaroslav Tykhiy ftpdebug++; 413ea022d16SRodney W. Grimes break; 414ea022d16SRodney W. Grimes 4155d7e0128SYaroslav Tykhiy case 'W': 4165d7e0128SYaroslav Tykhiy dowtmp = 0; 4175d7e0128SYaroslav Tykhiy break; 4185d7e0128SYaroslav Tykhiy 419ea022d16SRodney W. Grimes default: 4206c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "unknown flag -%c ignored", optopt); 421ea022d16SRodney W. Grimes break; 422ea022d16SRodney W. Grimes } 423ea022d16SRodney W. Grimes } 424cf09a206SDavid Greenman 425ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 426ea4e54b9SDavid Nugent inithosts(); 427ea4e54b9SDavid Nugent #endif 428cf09a206SDavid Greenman 429cf09a206SDavid Greenman if (daemon_mode) { 430206fe568SHajimu UMEMOTO int *ctl_sock, fd, maxfd = -1, nfds, i; 431206fe568SHajimu UMEMOTO fd_set defreadfds, readfds; 432206fe568SHajimu UMEMOTO pid_t pid; 433cf09a206SDavid Greenman 434cf09a206SDavid Greenman /* 435cf09a206SDavid Greenman * Detach from parent. 436cf09a206SDavid Greenman */ 437cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 438cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 439cf09a206SDavid Greenman exit(1); 440cf09a206SDavid Greenman } 4414b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild; 4424b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 4434dd8b5abSYoshinobu Inoue 444cf09a206SDavid Greenman /* 445cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 446cf09a206SDavid Greenman * listening. 447cf09a206SDavid Greenman */ 448206fe568SHajimu UMEMOTO ctl_sock = socksetup(family, bindname, bindport); 449206fe568SHajimu UMEMOTO if (ctl_sock == NULL) 450cf09a206SDavid Greenman exit(1); 451206fe568SHajimu UMEMOTO 452206fe568SHajimu UMEMOTO FD_ZERO(&defreadfds); 453206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++) { 454206fe568SHajimu UMEMOTO FD_SET(ctl_sock[i], &defreadfds); 455206fe568SHajimu UMEMOTO if (listen(ctl_sock[i], 32) < 0) { 456cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 457cf09a206SDavid Greenman exit(1); 458cf09a206SDavid Greenman } 459206fe568SHajimu UMEMOTO if (maxfd < ctl_sock[i]) 460206fe568SHajimu UMEMOTO maxfd = ctl_sock[i]; 461206fe568SHajimu UMEMOTO } 462206fe568SHajimu UMEMOTO 463cf09a206SDavid Greenman /* 464105a3c98SJulian Elischer * Atomically write process ID 465105a3c98SJulian Elischer */ 466105a3c98SJulian Elischer if (pid_file) 467105a3c98SJulian Elischer { 468105a3c98SJulian Elischer int fd; 469105a3c98SJulian Elischer char buf[20]; 470105a3c98SJulian Elischer 471105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 472105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 47385966371SWarner Losh if (fd < 0) { 474105a3c98SJulian Elischer if (errno == EAGAIN) 4751850cfa1SYaroslav Tykhiy syslog(LOG_ERR, 4761850cfa1SYaroslav Tykhiy "%s: already locked", pid_file); 477105a3c98SJulian Elischer else 4781850cfa1SYaroslav Tykhiy syslog(LOG_ERR, "%s: %m", pid_file); 4791850cfa1SYaroslav Tykhiy exit(1); 48085966371SWarner Losh } 481105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 482105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 4831850cfa1SYaroslav Tykhiy if (write(fd, buf, strlen(buf)) < 0) { 4841850cfa1SYaroslav Tykhiy syslog(LOG_ERR, "%s: write: %m", pid_file); 4851850cfa1SYaroslav Tykhiy exit(1); 4861850cfa1SYaroslav Tykhiy } 487105a3c98SJulian Elischer /* Leave the pid file open and locked */ 488105a3c98SJulian Elischer } 489105a3c98SJulian Elischer /* 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); 50940e67765SMaxim Konovalov if (fd >= 0) { 510206fe568SHajimu UMEMOTO if ((pid = fork()) == 0) { 511cf09a206SDavid Greenman /* child */ 512cf09a206SDavid Greenman (void) dup2(fd, 0); 513cf09a206SDavid Greenman (void) dup2(fd, 1); 514206fe568SHajimu UMEMOTO close(ctl_sock[i]); 515206fe568SHajimu UMEMOTO } else 516cf09a206SDavid Greenman close(fd); 517cf09a206SDavid Greenman } 51840e67765SMaxim Konovalov } 519206fe568SHajimu UMEMOTO if (pid == 0) 520206fe568SHajimu UMEMOTO break; 521206fe568SHajimu UMEMOTO } 522cf09a206SDavid Greenman } else { 523cf09a206SDavid Greenman addrlen = sizeof(his_addr); 524cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 525cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 526cf09a206SDavid Greenman exit(1); 527cf09a206SDavid Greenman } 528cf09a206SDavid Greenman } 529cf09a206SDavid Greenman 5304b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL; 5314b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 5324b82fc95SYaroslav Tykhiy 5334b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg; 5344b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 5354b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL); 5364b82fc95SYaroslav Tykhiy 5374b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */ 5384b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 5394b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit; 5404b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL); 5414b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL); 5424b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL); 5434b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL); 5444b82fc95SYaroslav Tykhiy 5454b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn; 5464b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL); 547ea022d16SRodney W. Grimes 548cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 549cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 550cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 551cf09a206SDavid Greenman exit(1); 552cf09a206SDavid Greenman } 55363591ba5SYaroslav Tykhiy dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */ 554ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 555ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5564dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 557ea4e54b9SDavid Nugent #endif 558cf09a206SDavid Greenman #ifdef IP_TOS 5594dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5604dd8b5abSYoshinobu Inoue { 561cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 56257d4ef07SYaroslav Tykhiy if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 563406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 5644dd8b5abSYoshinobu Inoue } 565cf09a206SDavid Greenman #endif 566dadb9fb3SDavid Greenman /* 567dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 568dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 569dadb9fb3SDavid Greenman */ 570dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 571406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 572dadb9fb3SDavid Greenman 5734dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 574cf09a206SDavid Greenman 575a5a4544eSPaul Traina /* set this here so klogin can use it... */ 576e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 577a5a4544eSPaul Traina 578ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 579ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 58057d4ef07SYaroslav Tykhiy if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 581406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 582ea022d16SRodney W. Grimes #endif 583ea022d16SRodney W. Grimes 584ea022d16SRodney W. Grimes #ifdef F_SETOWN 585ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 586ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 587ea022d16SRodney W. Grimes #endif 5884dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 589ea022d16SRodney W. Grimes /* 590ea022d16SRodney W. Grimes * Set up default state 591ea022d16SRodney W. Grimes */ 592ea022d16SRodney W. Grimes data = -1; 593ea022d16SRodney W. Grimes type = TYPE_A; 594ea022d16SRodney W. Grimes form = FORM_N; 595ea022d16SRodney W. Grimes stru = STRU_F; 596ea022d16SRodney W. Grimes mode = MODE_S; 597ea022d16SRodney W. Grimes tmpline[0] = '\0'; 598ea022d16SRodney W. Grimes 599ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 600ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 601ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 602ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 603ea022d16SRodney W. Grimes *cp = '\0'; 604ea022d16SRodney W. Grimes lreply(530, "%s", line); 605ea022d16SRodney W. Grimes } 606ea022d16SRodney W. Grimes (void) fflush(stdout); 607ea022d16SRodney W. Grimes (void) fclose(fd); 608ea022d16SRodney W. Grimes reply(530, "System not available."); 609ea022d16SRodney W. Grimes exit(0); 610ea022d16SRodney W. Grimes } 611ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 61263047c6fSDavid E. O'Brien fd = fopen(thishost->welcome, "r"); 613ea4e54b9SDavid Nugent #else 61463047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPWELCOME, "r"); 615ea4e54b9SDavid Nugent #endif 61663047c6fSDavid E. O'Brien if (fd != NULL) { 617ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 618ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 619ea022d16SRodney W. Grimes *cp = '\0'; 620ea022d16SRodney W. Grimes lreply(220, "%s", line); 621ea022d16SRodney W. Grimes } 622ea022d16SRodney W. Grimes (void) fflush(stdout); 623ea022d16SRodney W. Grimes (void) fclose(fd); 624ea022d16SRodney W. Grimes /* reply(220,) must follow */ 625ea022d16SRodney W. Grimes } 626ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6270512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 628618b0bbaSMark Murray fatalerror("Ran out of memory."); 62904683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) 63004683b2cSYaroslav Tykhiy hostname[0] = '\0'; 6319e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 632ea4e54b9SDavid Nugent #endif 633c152df28SYaroslav Tykhiy if (hostinfo) 634ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 635c152df28SYaroslav Tykhiy else 636c152df28SYaroslav Tykhiy reply(220, "FTP server ready."); 637ea022d16SRodney W. Grimes for (;;) 638ea022d16SRodney W. Grimes (void) yyparse(); 639ea022d16SRodney W. Grimes /* NOTREACHED */ 640ea022d16SRodney W. Grimes } 641ea022d16SRodney W. Grimes 642ea022d16SRodney W. Grimes static void 643e4bc453cSWarner Losh lostconn(int signo) 644ea022d16SRodney W. Grimes { 645ea022d16SRodney W. Grimes 646618b0bbaSMark Murray if (ftpdebug) 647ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 648e02897faSPhilippe Charnier dologout(1); 649ea022d16SRodney W. Grimes } 650ea022d16SRodney W. Grimes 6514b82fc95SYaroslav Tykhiy static void 652e4bc453cSWarner Losh sigquit(int signo) 6534b82fc95SYaroslav Tykhiy { 6544b82fc95SYaroslav Tykhiy 6554b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo); 6564b82fc95SYaroslav Tykhiy dologout(1); 6574b82fc95SYaroslav Tykhiy } 6584b82fc95SYaroslav Tykhiy 659ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 660ea4e54b9SDavid Nugent /* 661ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 662ea4e54b9SDavid Nugent */ 663ea4e54b9SDavid Nugent 664ea4e54b9SDavid Nugent static void 665e4bc453cSWarner Losh inithosts(void) 666ea4e54b9SDavid Nugent { 66755b54aa7SYaroslav Tykhiy int insert; 668737d08f3SYaroslav Tykhiy size_t len; 669ea4e54b9SDavid Nugent FILE *fp; 670737d08f3SYaroslav Tykhiy char *cp, *mp, *line; 671737d08f3SYaroslav Tykhiy char *hostname; 67255b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 673ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 6744dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 675ea4e54b9SDavid Nugent 676ea4e54b9SDavid Nugent /* 677ea4e54b9SDavid Nugent * Fill in the default host information 678ea4e54b9SDavid Nugent */ 679737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 680618b0bbaSMark Murray fatalerror("Ran out of memory."); 68104683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) 682737d08f3SYaroslav Tykhiy hostname[0] = '\0'; 683737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0'; 684737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 685737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 686737d08f3SYaroslav Tykhiy hrp->hostname = hostname; 687f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 6884dd8b5abSYoshinobu Inoue 6894dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 6904dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 6914dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 692b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 693f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 694ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 695ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 696ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 697ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 698ea4e54b9SDavid Nugent hrp->next = NULL; 699ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 700ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 701ec009cf0SYaroslav Tykhiy int addrsize, gothost; 7024dd8b5abSYoshinobu Inoue void *addr; 7034dd8b5abSYoshinobu Inoue struct hostent *hp; 7044dd8b5abSYoshinobu Inoue 705737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) { 7064dd8b5abSYoshinobu Inoue int i, hp_error; 707ea4e54b9SDavid Nugent 708737d08f3SYaroslav Tykhiy /* skip comments */ 709737d08f3SYaroslav Tykhiy if (line[0] == '#') 710ea4e54b9SDavid Nugent continue; 711737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 712737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 713737d08f3SYaroslav Tykhiy mp = NULL; 714737d08f3SYaroslav Tykhiy } else { 715737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 716737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 717737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 718737d08f3SYaroslav Tykhiy mp[len] = '\0'; 719737d08f3SYaroslav Tykhiy line = mp; 720ea4e54b9SDavid Nugent } 721ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 722737d08f3SYaroslav Tykhiy /* skip empty lines */ 723737d08f3SYaroslav Tykhiy if (cp == NULL) 724737d08f3SYaroslav Tykhiy goto nextline; 72555b54aa7SYaroslav Tykhiy vhost = cp; 72655b54aa7SYaroslav Tykhiy 72755b54aa7SYaroslav Tykhiy /* set defaults */ 72855b54aa7SYaroslav Tykhiy anonuser = "ftp"; 72955b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE; 73055b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME; 73155b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG; 73255b54aa7SYaroslav Tykhiy 73355b54aa7SYaroslav Tykhiy /* 73455b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info 73555b54aa7SYaroslav Tykhiy * for all the addresses associated with 73655b54aa7SYaroslav Tykhiy * the virtual host name. 73755b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special: 73855b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed 73955b54aa7SYaroslav Tykhiy * later, after we know its canonical form. 74055b54aa7SYaroslav Tykhiy */ 74155b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 74255b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp))) 74355b54aa7SYaroslav Tykhiy switch (i) { 74455b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */ 74555b54aa7SYaroslav Tykhiy anonuser = cp; 74655b54aa7SYaroslav Tykhiy break; 74755b54aa7SYaroslav Tykhiy case 2: /* statistics file */ 74855b54aa7SYaroslav Tykhiy statfile = cp; 74955b54aa7SYaroslav Tykhiy break; 75055b54aa7SYaroslav Tykhiy case 3: /* welcome message */ 75155b54aa7SYaroslav Tykhiy welcome = cp; 75255b54aa7SYaroslav Tykhiy break; 75355b54aa7SYaroslav Tykhiy case 4: /* login message */ 75455b54aa7SYaroslav Tykhiy loginmsg = cp; 75555b54aa7SYaroslav Tykhiy break; 75655b54aa7SYaroslav Tykhiy default: /* programming error */ 75755b54aa7SYaroslav Tykhiy abort(); 75855b54aa7SYaroslav Tykhiy /* NOTREACHED */ 75955b54aa7SYaroslav Tykhiy } 7604dd8b5abSYoshinobu Inoue 7614dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 7624dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 7634dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 764b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 765737d08f3SYaroslav Tykhiy goto nextline; 7664dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 767b535a9bfSDavid Nugent ai = ai->ai_next) { 7684dd8b5abSYoshinobu Inoue 769b535a9bfSDavid Nugent gothost = 0; 770ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 771f38c6cadSYoshinobu Inoue struct addrinfo *hi; 772f38c6cadSYoshinobu Inoue 773f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 774f38c6cadSYoshinobu Inoue hi = hi->ai_next) 775f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 776f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7774dd8b5abSYoshinobu Inoue ai->ai_addr, 778b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 779b535a9bfSDavid Nugent gothost++; 780b535a9bfSDavid Nugent break; 781b535a9bfSDavid Nugent } 782b535a9bfSDavid Nugent if (gothost) 783ea4e54b9SDavid Nugent break; 784ea4e54b9SDavid Nugent } 785ea4e54b9SDavid Nugent if (hrp == NULL) { 786ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 787737d08f3SYaroslav Tykhiy goto nextline; 788b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 78955b54aa7SYaroslav Tykhiy insert = 1; 790f2fe752dSYaroslav Tykhiy } else { 7911f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res) 792b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 793f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */ 794f2fe752dSYaroslav Tykhiy } 795f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 796f38c6cadSYoshinobu Inoue 797ea4e54b9SDavid Nugent /* 798ea4e54b9SDavid Nugent * determine hostname to use. 7994dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 800ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 801ea4e54b9SDavid Nugent */ 8024dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 803f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 8044dd8b5abSYoshinobu Inoue case AF_INET: 8054b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 8064b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr); 8074dd8b5abSYoshinobu Inoue break; 8084dd8b5abSYoshinobu Inoue case AF_INET6: 8094b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 8104b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr); 8114dd8b5abSYoshinobu Inoue break; 8124dd8b5abSYoshinobu Inoue default: 8134dd8b5abSYoshinobu Inoue /* should not reach here */ 814f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 815f2fe752dSYaroslav Tykhiy if (insert) 816f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/ 817f2fe752dSYaroslav Tykhiy else 818f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/ 819737d08f3SYaroslav Tykhiy goto nextline; 8204dd8b5abSYoshinobu Inoue /* NOTREACHED */ 8214dd8b5abSYoshinobu Inoue } 82257d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize, 823f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 8244dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 82555b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) { 826ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 82755b54aa7SYaroslav Tykhiy vhost = hp->h_name; 828ea4e54b9SDavid Nugent else { 829ea4e54b9SDavid Nugent i = 0; 830ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 83155b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0) 832ea4e54b9SDavid Nugent ++i; 833ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 83455b54aa7SYaroslav Tykhiy vhost = hp->h_name; 835ea4e54b9SDavid Nugent } 836ea4e54b9SDavid Nugent } 837ea4e54b9SDavid Nugent } 838b1d8d5cdSYaroslav Tykhiy if (hrp->hostname && 839b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) { 840b1d8d5cdSYaroslav Tykhiy free(hrp->hostname); 841b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 842b1d8d5cdSYaroslav Tykhiy } 843b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL && 844f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) { 845f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 846f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */ 847f2fe752dSYaroslav Tykhiy if (hp) 848f2fe752dSYaroslav Tykhiy freehostent(hp); 84955b54aa7SYaroslav Tykhiy goto nextline; 850f2fe752dSYaroslav Tykhiy } 85155b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser; 85255b54aa7SYaroslav Tykhiy hrp->statfile = statfile; 85355b54aa7SYaroslav Tykhiy hrp->welcome = welcome; 85455b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg; 85555b54aa7SYaroslav Tykhiy if (insert) { 85655b54aa7SYaroslav Tykhiy hrp->next = NULL; 85755b54aa7SYaroslav Tykhiy lhrp->next = hrp; 85855b54aa7SYaroslav Tykhiy lhrp = hrp; 85955b54aa7SYaroslav Tykhiy } 860233c0f66SYaroslav Tykhiy if (hp) 8614dd8b5abSYoshinobu Inoue freehostent(hp); 8624dd8b5abSYoshinobu Inoue } 863737d08f3SYaroslav Tykhiy nextline: 864737d08f3SYaroslav Tykhiy if (mp) 865737d08f3SYaroslav Tykhiy free(mp); 866ea4e54b9SDavid Nugent } 867ea4e54b9SDavid Nugent (void) fclose(fp); 868ea4e54b9SDavid Nugent } 869ea4e54b9SDavid Nugent } 870ea4e54b9SDavid Nugent 871ea4e54b9SDavid Nugent static void 872e4bc453cSWarner Losh selecthost(union sockunion *su) 873ea4e54b9SDavid Nugent { 874ea4e54b9SDavid Nugent struct ftphost *hrp; 8754dd8b5abSYoshinobu Inoue u_int16_t port; 8764dd8b5abSYoshinobu Inoue #ifdef INET6 8774dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 8784dd8b5abSYoshinobu Inoue #endif 879f38c6cadSYoshinobu Inoue struct addrinfo *hi; 8804dd8b5abSYoshinobu Inoue 8814dd8b5abSYoshinobu Inoue #ifdef INET6 8824dd8b5abSYoshinobu Inoue /* 8834dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 8844dd8b5abSYoshinobu Inoue * specified in rfc2373. 8854dd8b5abSYoshinobu Inoue */ 8864dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 8874dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 8884dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 8894dd8b5abSYoshinobu Inoue #endif 890ea4e54b9SDavid Nugent 891ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 8924dd8b5abSYoshinobu Inoue port = su->su_port; 8934dd8b5abSYoshinobu Inoue su->su_port = 0; 894ea4e54b9SDavid Nugent while (hrp != NULL) { 895b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 896f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 897ea4e54b9SDavid Nugent thishost = hrp; 898ebd83647SYaroslav Tykhiy goto found; 899ea4e54b9SDavid Nugent } 9004dd8b5abSYoshinobu Inoue #ifdef INET6 9014dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 902f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 9034dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 904f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 9054dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 9064dd8b5abSYoshinobu Inoue thishost = hrp; 907ebd83647SYaroslav Tykhiy goto found; 9084dd8b5abSYoshinobu Inoue } 9094dd8b5abSYoshinobu Inoue #endif 910f38c6cadSYoshinobu Inoue } 911ea4e54b9SDavid Nugent hrp = hrp->next; 912ea4e54b9SDavid Nugent } 913ebd83647SYaroslav Tykhiy found: 9144dd8b5abSYoshinobu Inoue su->su_port = port; 915ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 916ea4e54b9SDavid Nugent hostname = thishost->hostname; 917ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 918ea4e54b9SDavid Nugent } 919ea4e54b9SDavid Nugent #endif 920ea4e54b9SDavid Nugent 921ea022d16SRodney W. Grimes /* 922ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 923ea022d16SRodney W. Grimes */ 924ea022d16SRodney W. Grimes static char * 925e4bc453cSWarner Losh sgetsave(char *s) 926ea022d16SRodney W. Grimes { 927e3765043SYaroslav Tykhiy char *new = malloc(strlen(s) + 1); 928ea022d16SRodney W. Grimes 929ea022d16SRodney W. Grimes if (new == NULL) { 93002c97492SYaroslav Tykhiy reply(421, "Ran out of memory."); 931ea022d16SRodney W. Grimes dologout(1); 932ea022d16SRodney W. Grimes /* NOTREACHED */ 933ea022d16SRodney W. Grimes } 934ea022d16SRodney W. Grimes (void) strcpy(new, s); 935ea022d16SRodney W. Grimes return (new); 936ea022d16SRodney W. Grimes } 937ea022d16SRodney W. Grimes 938ea022d16SRodney W. Grimes /* 939ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 940ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 941ea022d16SRodney W. Grimes * (e.g., globbing). 942c29b9b47SYaroslav Tykhiy * NB: The data returned by sgetpwnam() will remain valid until 943c29b9b47SYaroslav Tykhiy * the next call to this function. Its difference from getpwnam() 944c29b9b47SYaroslav Tykhiy * is that sgetpwnam() is known to be called from ftpd code only. 945ea022d16SRodney W. Grimes */ 946ea022d16SRodney W. Grimes static struct passwd * 947e4bc453cSWarner Losh sgetpwnam(char *name) 948ea022d16SRodney W. Grimes { 949ea022d16SRodney W. Grimes static struct passwd save; 950ea022d16SRodney W. Grimes struct passwd *p; 951ea022d16SRodney W. Grimes 952ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 953ea022d16SRodney W. Grimes return (p); 954ea022d16SRodney W. Grimes if (save.pw_name) { 955ea022d16SRodney W. Grimes free(save.pw_name); 956ea022d16SRodney W. Grimes free(save.pw_passwd); 957ea022d16SRodney W. Grimes free(save.pw_gecos); 958ea022d16SRodney W. Grimes free(save.pw_dir); 959ea022d16SRodney W. Grimes free(save.pw_shell); 960ea022d16SRodney W. Grimes } 961ea022d16SRodney W. Grimes save = *p; 962ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 963ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 964ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 965ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 966ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 967ea022d16SRodney W. Grimes return (&save); 968ea022d16SRodney W. Grimes } 969ea022d16SRodney W. Grimes 970ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 971ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 97290906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 973ea022d16SRodney W. Grimes 974ea022d16SRodney W. Grimes /* 975ea022d16SRodney W. Grimes * USER command. 976ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 977ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 978ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 979ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 980ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 981ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 982ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 983ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 984ea022d16SRodney W. Grimes */ 985ea022d16SRodney W. Grimes void 986e4bc453cSWarner Losh user(char *name) 987ea022d16SRodney W. Grimes { 988ea022d16SRodney W. Grimes char *cp, *shell; 989ea022d16SRodney W. Grimes 990ea022d16SRodney W. Grimes if (logged_in) { 991ea022d16SRodney W. Grimes if (guest) { 992ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 993ea022d16SRodney W. Grimes return; 994a5a4544eSPaul Traina } else if (dochroot) { 995a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 996a5a4544eSPaul Traina return; 997ea022d16SRodney W. Grimes } 998ea022d16SRodney W. Grimes end_login(); 999ea022d16SRodney W. Grimes } 1000ea022d16SRodney W. Grimes 1001ea022d16SRodney W. Grimes guest = 0; 100263047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING 100363047c6fSDavid E. O'Brien pw = sgetpwnam(thishost->anonuser); 100463047c6fSDavid E. O'Brien #else 100563047c6fSDavid E. O'Brien pw = sgetpwnam("ftp"); 100663047c6fSDavid E. O'Brien #endif 1007ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 10088657b576SYaroslav Tykhiy if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL) || 10098657b576SYaroslav Tykhiy checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL)) 1010ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 101163047c6fSDavid E. O'Brien else if (pw != NULL) { 1012ea022d16SRodney W. Grimes guest = 1; 1013ea022d16SRodney W. Grimes askpasswd = 1; 1014ea022d16SRodney W. Grimes reply(331, 10152c60c54cSPaul Traina "Guest login ok, send your email address as password."); 1016ea022d16SRodney W. Grimes } else 1017ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 1018ea022d16SRodney W. Grimes if (!askpasswd && logging) 1019ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1020ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 1021ea022d16SRodney W. Grimes return; 1022ea022d16SRodney W. Grimes } 10235a392aecSTorsten Blum if (anon_only != 0) { 10245a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 10255a392aecSTorsten Blum return; 10265a392aecSTorsten Blum } 10275a392aecSTorsten Blum 10289aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 1029ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 1030ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 1031c433c9daSPhilippe Charnier setusershell(); 1032ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 1033ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 1034ea022d16SRodney W. Grimes break; 1035ea022d16SRodney W. Grimes endusershell(); 1036ea022d16SRodney W. Grimes 10378657b576SYaroslav Tykhiy if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1, NULL)) { 1038ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1039ea022d16SRodney W. Grimes if (logging) 1040ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1041ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 1042ea022d16SRodney W. Grimes remotehost, name); 10437e295315SYaroslav Tykhiy pw = NULL; 1044ea022d16SRodney W. Grimes return; 1045ea022d16SRodney W. Grimes } 1046ea022d16SRodney W. Grimes } 1047ea022d16SRodney W. Grimes if (logging) 1048ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 104947499ecdSAndrey A. Chernov 105047499ecdSAndrey A. Chernov pwok = 0; 1051fa1746c9SMark Murray #ifdef USE_PAM 1052fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1053726040deSGuido van Rooij #endif 105447499ecdSAndrey A. Chernov if (opiechallenge(&opiedata, name, opieprompt) == 0) { 105547499ecdSAndrey A. Chernov pwok = (pw != NULL) && 105647499ecdSAndrey A. Chernov opieaccessfile(remotehost) && 105747499ecdSAndrey A. Chernov opiealways(pw->pw_dir); 105847499ecdSAndrey A. Chernov reply(331, "Response to %s %s for %s.", 105947499ecdSAndrey A. Chernov opieprompt, pwok ? "requested" : "required", name); 106047499ecdSAndrey A. Chernov } else { 106147499ecdSAndrey A. Chernov pwok = 1; 106247499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name); 106347499ecdSAndrey A. Chernov } 1064ea022d16SRodney W. Grimes askpasswd = 1; 1065ea022d16SRodney W. Grimes /* 1066ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 1067ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 1068ea022d16SRodney W. Grimes */ 1069ea022d16SRodney W. Grimes if (login_attempts) 1070e3765043SYaroslav Tykhiy sleep(login_attempts); 1071ea022d16SRodney W. Grimes } 1072ea022d16SRodney W. Grimes 1073ea022d16SRodney W. Grimes /* 10748657b576SYaroslav Tykhiy * Check if a user is in the file "fname", 10758657b576SYaroslav Tykhiy * return a pointer to a malloc'd string with the rest 10768657b576SYaroslav Tykhiy * of the matching line in "residue" if not NULL. 1077ea022d16SRodney W. Grimes */ 1078ea022d16SRodney W. Grimes static int 10798657b576SYaroslav Tykhiy checkuser(char *fname, char *name, int pwset, char **residue) 1080ea022d16SRodney W. Grimes { 1081ea022d16SRodney W. Grimes FILE *fd; 1082ea022d16SRodney W. Grimes int found = 0; 1083737d08f3SYaroslav Tykhiy size_t len; 1084737d08f3SYaroslav Tykhiy char *line, *mp, *p; 1085ea022d16SRodney W. Grimes 1086a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 1087737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) { 1088737d08f3SYaroslav Tykhiy /* skip comments */ 1089ea022d16SRodney W. Grimes if (line[0] == '#') 1090ea022d16SRodney W. Grimes continue; 1091737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 1092737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 1093737d08f3SYaroslav Tykhiy mp = NULL; 1094737d08f3SYaroslav Tykhiy } else { 1095737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 1096737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 1097737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 1098737d08f3SYaroslav Tykhiy mp[len] = '\0'; 1099737d08f3SYaroslav Tykhiy line = mp; 1100737d08f3SYaroslav Tykhiy } 1101737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */ 1102737d08f3SYaroslav Tykhiy p = strtok(line, " \t"); 1103737d08f3SYaroslav Tykhiy /* skip empty lines */ 1104737d08f3SYaroslav Tykhiy if (p == NULL) 1105737d08f3SYaroslav Tykhiy goto nextline; 110631fea7b8SDavid Nugent /* 110731fea7b8SDavid Nugent * if first chr is '@', check group membership 110831fea7b8SDavid Nugent */ 1109737d08f3SYaroslav Tykhiy if (p[0] == '@') { 111031fea7b8SDavid Nugent int i = 0; 111131fea7b8SDavid Nugent struct group *grp; 111231fea7b8SDavid Nugent 11138657b576SYaroslav Tykhiy if (p[1] == '\0') /* single @ matches anyone */ 11148657b576SYaroslav Tykhiy found = 1; 11158657b576SYaroslav Tykhiy else { 1116737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL) 1117737d08f3SYaroslav Tykhiy goto nextline; 11187edcb936SSteve Price /* 11197edcb936SSteve Price * Check user's default group 11207edcb936SSteve Price */ 11217edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 11227edcb936SSteve Price found = 1; 11237edcb936SSteve Price /* 11247edcb936SSteve Price * Check supplementary groups 11257edcb936SSteve Price */ 112631fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 112731fea7b8SDavid Nugent found = strcmp(name, 112831fea7b8SDavid Nugent grp->gr_mem[i++]) 112931fea7b8SDavid Nugent == 0; 1130ea022d16SRodney W. Grimes } 11318657b576SYaroslav Tykhiy } 113231fea7b8SDavid Nugent /* 113331fea7b8SDavid Nugent * Otherwise, just check for username match 113431fea7b8SDavid Nugent */ 113531fea7b8SDavid Nugent else 1136737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0; 11378657b576SYaroslav Tykhiy /* 11388657b576SYaroslav Tykhiy * Save the rest of line to "residue" if matched 11398657b576SYaroslav Tykhiy */ 11408657b576SYaroslav Tykhiy if (found && residue) { 11410ba71e24SYaroslav Tykhiy if ((p = strtok(NULL, "")) != NULL) 11420ba71e24SYaroslav Tykhiy p += strspn(p, " \t"); 11430ba71e24SYaroslav Tykhiy if (p && *p) { 11448657b576SYaroslav Tykhiy if ((*residue = strdup(p)) == NULL) 11458657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 11468657b576SYaroslav Tykhiy } else 11478657b576SYaroslav Tykhiy *residue = NULL; 11488657b576SYaroslav Tykhiy } 1149737d08f3SYaroslav Tykhiy nextline: 1150737d08f3SYaroslav Tykhiy if (mp) 1151737d08f3SYaroslav Tykhiy free(mp); 1152ea022d16SRodney W. Grimes } 1153ea022d16SRodney W. Grimes (void) fclose(fd); 1154ea022d16SRodney W. Grimes } 1155ea022d16SRodney W. Grimes return (found); 1156ea022d16SRodney W. Grimes } 1157ea022d16SRodney W. Grimes 1158ea022d16SRodney W. Grimes /* 1159ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1160ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1161ea022d16SRodney W. Grimes */ 1162ea022d16SRodney W. Grimes static void 1163e4bc453cSWarner Losh end_login(void) 1164ea022d16SRodney W. Grimes { 11655bc9d93dSMark Murray #ifdef USE_PAM 11665bc9d93dSMark Murray int e; 11675bc9d93dSMark Murray #endif 1168ea022d16SRodney W. Grimes 116952e7ee74SYaroslav Tykhiy (void) seteuid(0); 11705d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) 117146948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 1172ea022d16SRodney W. Grimes pw = NULL; 1173b071c689SDavid Nugent #ifdef LOGIN_CAP 117452e7ee74SYaroslav Tykhiy setusercontext(NULL, getpwuid(0), 0, 1175d9e2c424SRobert Watson LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK| 1176d9e2c424SRobert Watson LOGIN_SETMAC); 1177b071c689SDavid Nugent #endif 11785bc9d93dSMark Murray #ifdef USE_PAM 1179de45162dSYaroslav Tykhiy if (pamh) { 11805bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 11815bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 11825bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 11835bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 11845bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 11855bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 11865bc9d93dSMark Murray pamh = NULL; 1187de45162dSYaroslav Tykhiy } 11885bc9d93dSMark Murray #endif 1189ea022d16SRodney W. Grimes logged_in = 0; 1190ea022d16SRodney W. Grimes guest = 0; 1191a5a4544eSPaul Traina dochroot = 0; 1192ea022d16SRodney W. Grimes } 1193ea022d16SRodney W. Grimes 11945bc9d93dSMark Murray #ifdef USE_PAM 11956c9134c0SMark Murray 11966c9134c0SMark Murray /* 11976c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 11986c9134c0SMark Murray * login.c 11996c9134c0SMark Murray */ 12006c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 12016c9134c0SMark Murray 12026c9134c0SMark Murray struct cred_t { 12036c9134c0SMark Murray const char *uname; /* user name */ 12046c9134c0SMark Murray const char *pass; /* password */ 12056c9134c0SMark Murray }; 12066c9134c0SMark Murray typedef struct cred_t cred_t; 12076c9134c0SMark Murray 12086c9134c0SMark Murray static int 12096c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 12106c9134c0SMark Murray struct pam_response **resp, void *appdata) 12116c9134c0SMark Murray { 12126c9134c0SMark Murray int i; 12136c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 121460769b19SDag-Erling Smørgrav struct pam_response *reply; 121560769b19SDag-Erling Smørgrav 121660769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply); 121760769b19SDag-Erling Smørgrav if (reply == NULL) 121860769b19SDag-Erling Smørgrav return PAM_BUF_ERR; 12196c9134c0SMark Murray 12206c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 12216c9134c0SMark Murray switch (msg[i]->msg_style) { 12226c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 12236c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12246c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 12256c9134c0SMark Murray /* PAM frees resp. */ 12266c9134c0SMark Murray break; 12276c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 12286c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12296c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 12306c9134c0SMark Murray /* PAM frees resp. */ 12316c9134c0SMark Murray break; 12326c9134c0SMark Murray case PAM_TEXT_INFO: 12336c9134c0SMark Murray case PAM_ERROR_MSG: 12346c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12356c9134c0SMark Murray reply[i].resp = NULL; 12366c9134c0SMark Murray break; 12376c9134c0SMark Murray default: /* unknown message style */ 12386c9134c0SMark Murray free(reply); 12396c9134c0SMark Murray return PAM_CONV_ERR; 12406c9134c0SMark Murray } 12416c9134c0SMark Murray } 12426c9134c0SMark Murray 12436c9134c0SMark Murray *resp = reply; 12446c9134c0SMark Murray return PAM_SUCCESS; 12456c9134c0SMark Murray } 12466c9134c0SMark Murray 12476c9134c0SMark Murray /* 12486c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 12496c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 12506c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 12516c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 12526c9134c0SMark Murray * fall back to a different authentication mechanism. 12536c9134c0SMark Murray */ 12546c9134c0SMark Murray static int 12556c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 12566c9134c0SMark Murray { 12576c9134c0SMark Murray const char *tmpl_user; 12586c9134c0SMark Murray const void *item; 12596c9134c0SMark Murray int rval; 12606c9134c0SMark Murray int e; 12616c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 12626c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 12636c9134c0SMark Murray 12646c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 12656c9134c0SMark Murray if (e != PAM_SUCCESS) { 1266545ea864SYaroslav Tykhiy /* 1267545ea864SYaroslav Tykhiy * In OpenPAM, it's OK to pass NULL to pam_strerror() 1268545ea864SYaroslav Tykhiy * if context creation has failed in the first place. 1269545ea864SYaroslav Tykhiy */ 1270545ea864SYaroslav Tykhiy syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e)); 12716c9134c0SMark Murray return -1; 12726c9134c0SMark Murray } 12736c9134c0SMark Murray 1274ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1275ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1276ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1277ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1278de45162dSYaroslav Tykhiy if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 1279de45162dSYaroslav Tykhiy syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 1280de45162dSYaroslav Tykhiy } 1281de45162dSYaroslav Tykhiy pamh = NULL; 1282ea413ab7SGuido van Rooij return -1; 1283ea413ab7SGuido van Rooij } 1284ea413ab7SGuido van Rooij 12856c9134c0SMark Murray e = pam_authenticate(pamh, 0); 12866c9134c0SMark Murray switch (e) { 12876c9134c0SMark Murray case PAM_SUCCESS: 12886c9134c0SMark Murray /* 12896c9134c0SMark Murray * With PAM we support the concept of a "template" 12906c9134c0SMark Murray * user. The user enters a login name which is 12916c9134c0SMark Murray * authenticated by PAM, usually via a remote service 12926c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 12936c9134c0SMark Murray * succeeds, a different but related "template" name 12946c9134c0SMark Murray * is used for setting the credentials, shell, and 12956c9134c0SMark Murray * home directory. The name the user enters need only 12966c9134c0SMark Murray * exist on the remote authentication server, but the 12976c9134c0SMark Murray * template name must be present in the local password 12986c9134c0SMark Murray * database. 12996c9134c0SMark Murray * 13006c9134c0SMark Murray * This is supported by two various mechanisms in the 13016c9134c0SMark Murray * individual modules. However, from the application's 13026c9134c0SMark Murray * point of view, the template user is always passed 13036c9134c0SMark Murray * back as a changed value of the PAM_USER item. 13046c9134c0SMark Murray */ 13056c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 13066c9134c0SMark Murray PAM_SUCCESS) { 13076c9134c0SMark Murray tmpl_user = (const char *) item; 13086c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 13096c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 13106c9134c0SMark Murray } else 13116c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 13126c9134c0SMark Murray pam_strerror(pamh, e)); 13136c9134c0SMark Murray rval = 0; 13146c9134c0SMark Murray break; 13156c9134c0SMark Murray 13166c9134c0SMark Murray case PAM_AUTH_ERR: 13176c9134c0SMark Murray case PAM_USER_UNKNOWN: 13186c9134c0SMark Murray case PAM_MAXTRIES: 13196c9134c0SMark Murray rval = 1; 13206c9134c0SMark Murray break; 13216c9134c0SMark Murray 13226c9134c0SMark Murray default: 13235bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 13246c9134c0SMark Murray rval = -1; 13256c9134c0SMark Murray break; 13266c9134c0SMark Murray } 13276c9134c0SMark Murray 13285bc9d93dSMark Murray if (rval == 0) { 13295bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 13305bc9d93dSMark Murray if (e != PAM_SUCCESS) { 13314cbc4ad6SYaroslav Tykhiy syslog(LOG_ERR, "pam_acct_mgmt: %s", 13324cbc4ad6SYaroslav Tykhiy pam_strerror(pamh, e)); 13335bc9d93dSMark Murray rval = 1; 13345bc9d93dSMark Murray } 13355bc9d93dSMark Murray } 13365bc9d93dSMark Murray 13375bc9d93dSMark Murray if (rval != 0) { 13386c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 13396c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 13405bc9d93dSMark Murray } 13415bc9d93dSMark Murray pamh = NULL; 13426c9134c0SMark Murray } 13436c9134c0SMark Murray return rval; 13446c9134c0SMark Murray } 13456c9134c0SMark Murray 13465bc9d93dSMark Murray #endif /* USE_PAM */ 13476c9134c0SMark Murray 1348ea022d16SRodney W. Grimes void 1349e4bc453cSWarner Losh pass(char *passwd) 1350ea022d16SRodney W. Grimes { 1351a5a4544eSPaul Traina int rval; 1352ea022d16SRodney W. Grimes FILE *fd; 1353b071c689SDavid Nugent #ifdef LOGIN_CAP 1354b071c689SDavid Nugent login_cap_t *lc = NULL; 1355b071c689SDavid Nugent #endif 13565bc9d93dSMark Murray #ifdef USE_PAM 13575bc9d93dSMark Murray int e; 13585bc9d93dSMark Murray #endif 1359341e476eSYaroslav Tykhiy char *residue = NULL; 136047499ecdSAndrey A. Chernov char *xpasswd; 1361ea022d16SRodney W. Grimes 1362ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1363ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1364ea022d16SRodney W. Grimes return; 1365ea022d16SRodney W. Grimes } 1366ea022d16SRodney W. Grimes askpasswd = 0; 1367ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1368a5a4544eSPaul Traina if (pw == NULL) { 1369a5a4544eSPaul Traina rval = 1; /* failure below */ 1370a5a4544eSPaul Traina goto skip; 1371a5a4544eSPaul Traina } 13725bc9d93dSMark Murray #ifdef USE_PAM 13736c9134c0SMark Murray rval = auth_pam(&pw, passwd); 1374f650a124SAndrey A. Chernov if (rval >= 0) { 1375f650a124SAndrey A. Chernov opieunlock(); 1376a5a4544eSPaul Traina goto skip; 1377f650a124SAndrey A. Chernov } 1378f650a124SAndrey A. Chernov #endif 137947499ecdSAndrey A. Chernov if (opieverify(&opiedata, passwd) == 0) 138047499ecdSAndrey A. Chernov xpasswd = pw->pw_passwd; 1381f650a124SAndrey A. Chernov else if (pwok) { 138247499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd); 1383f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1384f650a124SAndrey A. Chernov xpasswd = ":"; 1385f650a124SAndrey A. Chernov } else { 138647499ecdSAndrey A. Chernov rval = 1; 138747499ecdSAndrey A. Chernov goto skip; 138847499ecdSAndrey A. Chernov } 138947499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd); 1390f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1391a5a4544eSPaul Traina rval = 1; /* failure */ 1392a5a4544eSPaul Traina skip: 1393a5a4544eSPaul Traina /* 1394a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 13956c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1396a5a4544eSPaul Traina * succeeded. 1397a5a4544eSPaul Traina */ 1398a5a4544eSPaul Traina if (rval) { 1399ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1400b8939f6fSYaroslav Tykhiy if (logging) { 1401ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1402b8939f6fSYaroslav Tykhiy "FTP LOGIN FAILED FROM %s", 1403b8939f6fSYaroslav Tykhiy remotehost); 1404b8939f6fSYaroslav Tykhiy syslog(LOG_AUTHPRIV | LOG_NOTICE, 1405ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1406ea022d16SRodney W. Grimes remotehost, curname); 1407b8939f6fSYaroslav Tykhiy } 1408ea022d16SRodney W. Grimes pw = NULL; 1409ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1410ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1411ea022d16SRodney W. Grimes "repeated login failures from %s", 1412ea022d16SRodney W. Grimes remotehost); 1413ea022d16SRodney W. Grimes exit(0); 1414ea022d16SRodney W. Grimes } 1415ea022d16SRodney W. Grimes return; 1416ea022d16SRodney W. Grimes } 1417ea022d16SRodney W. Grimes } 1418ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1419c4536e21SYaroslav Tykhiy if (setegid(pw->pw_gid) < 0) { 1420ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1421ea022d16SRodney W. Grimes return; 1422ea022d16SRodney W. Grimes } 1423b071c689SDavid Nugent /* May be overridden by login.conf */ 1424b071c689SDavid Nugent (void) umask(defumask); 1425b071c689SDavid Nugent #ifdef LOGIN_CAP 14265d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 142704683b2cSYaroslav Tykhiy char remote_ip[NI_MAXHOST]; 1428b071c689SDavid Nugent 142904683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 14304dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 143104683b2cSYaroslav Tykhiy NI_NUMERICHOST)) 143204683b2cSYaroslav Tykhiy *remote_ip = 0; 1433b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1434b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1435b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1436b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1437b071c689SDavid Nugent pw->pw_name); 14384a3e5acdSYaroslav Tykhiy reply(530, "Permission denied."); 1439b071c689SDavid Nugent pw = NULL; 1440b071c689SDavid Nugent return; 1441b071c689SDavid Nugent } 1442b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 14434a3e5acdSYaroslav Tykhiy reply(530, "Login not available right now."); 1444b071c689SDavid Nugent pw = NULL; 1445b071c689SDavid Nugent return; 1446b071c689SDavid Nugent } 1447b071c689SDavid Nugent } 144852e7ee74SYaroslav Tykhiy setusercontext(lc, pw, 0, 1449e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1450d9e2c424SRobert Watson LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC); 1451b071c689SDavid Nugent #else 1452e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1453ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1454b071c689SDavid Nugent #endif 1455ea022d16SRodney W. Grimes 14565bc9d93dSMark Murray #ifdef USE_PAM 14575bc9d93dSMark Murray if (pamh) { 14585bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 14595bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 14605bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 14615bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 14625bc9d93dSMark Murray } 14635bc9d93dSMark Murray } 14645bc9d93dSMark Murray #endif 14655bc9d93dSMark Murray 1466ea022d16SRodney W. Grimes /* open wtmp before chroot */ 14675d7e0128SYaroslav Tykhiy if (dowtmp) 14685d7e0128SYaroslav Tykhiy ftpd_logwtmp(ttyline, pw->pw_name, 14695d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr); 1470ea022d16SRodney W. Grimes logged_in = 1; 1471ea022d16SRodney W. Grimes 1472a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1473ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 147463047c6fSDavid E. O'Brien statfd = open(thishost->statfile, O_WRONLY|O_APPEND); 1475ea4e54b9SDavid Nugent #else 147663047c6fSDavid E. O'Brien statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND); 1477ea4e54b9SDavid Nugent #endif 147863047c6fSDavid E. O'Brien if (statfd < 0) 14793eb568f2SGuido van Rooij stats = 0; 14803eb568f2SGuido van Rooij 1481b071c689SDavid Nugent dochroot = 1482341e476eSYaroslav Tykhiy checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue) 1483b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 14848657b576SYaroslav Tykhiy || login_getcapbool(lc, "ftp-chroot", 0) 1485b071c689SDavid Nugent #endif 14868657b576SYaroslav Tykhiy ; 1487ce9287fcSYaroslav Tykhiy chrootdir = NULL; 1488ea022d16SRodney W. Grimes /* 1489ce9287fcSYaroslav Tykhiy * For a chrooted local user, 1490ce9287fcSYaroslav Tykhiy * a) see whether ftpchroot(5) specifies a chroot directory, 1491ce9287fcSYaroslav Tykhiy * b) extract the directory pathname from the line, 1492ce9287fcSYaroslav Tykhiy * c) expand it to the absolute pathname if necessary. 1493ea022d16SRodney W. Grimes */ 1494ce9287fcSYaroslav Tykhiy if (dochroot && residue && 149539bce482SYaroslav Tykhiy (chrootdir = strtok(residue, " \t")) != NULL) { 149639bce482SYaroslav Tykhiy if (chrootdir[0] != '/') 1497341e476eSYaroslav Tykhiy asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir); 149839bce482SYaroslav Tykhiy else 1499215a9f9dSYaroslav Tykhiy chrootdir = strdup(chrootdir); /* make it permanent */ 1500341e476eSYaroslav Tykhiy if (chrootdir == NULL) 15018657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 15028657b576SYaroslav Tykhiy } 1503ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1504ce9287fcSYaroslav Tykhiy /* 1505ce9287fcSYaroslav Tykhiy * If no chroot directory set yet, use the login directory. 1506ce9287fcSYaroslav Tykhiy * Copy it so it can be modified while pw->pw_dir stays intact. 1507ce9287fcSYaroslav Tykhiy */ 1508ce9287fcSYaroslav Tykhiy if (chrootdir == NULL && 1509ce9287fcSYaroslav Tykhiy (chrootdir = strdup(pw->pw_dir)) == NULL) 1510ce9287fcSYaroslav Tykhiy fatalerror("Ran out of memory."); 1511ce9287fcSYaroslav Tykhiy /* 1512ce9287fcSYaroslav Tykhiy * Check for the "/chroot/./home" syntax, 1513ce9287fcSYaroslav Tykhiy * separate the chroot and home directory pathnames. 1514ce9287fcSYaroslav Tykhiy */ 1515ce9287fcSYaroslav Tykhiy if ((homedir = strstr(chrootdir, "/./")) != NULL) { 1516ce9287fcSYaroslav Tykhiy *(homedir++) = '\0'; /* wipe '/' */ 1517ce9287fcSYaroslav Tykhiy homedir++; /* skip '.' */ 1518ce9287fcSYaroslav Tykhiy } else { 1519ce9287fcSYaroslav Tykhiy /* 1520ce9287fcSYaroslav Tykhiy * We MUST do a chdir() after the chroot. Otherwise 1521ce9287fcSYaroslav Tykhiy * the old current directory will be accessible as "." 1522ce9287fcSYaroslav Tykhiy * outside the new root! 1523ce9287fcSYaroslav Tykhiy */ 1524ce9287fcSYaroslav Tykhiy homedir = "/"; 1525ce9287fcSYaroslav Tykhiy } 1526ce9287fcSYaroslav Tykhiy /* 1527ce9287fcSYaroslav Tykhiy * Finally, do chroot() 1528ce9287fcSYaroslav Tykhiy */ 1529ce9287fcSYaroslav Tykhiy if (chroot(chrootdir) < 0) { 1530a5a4544eSPaul Traina reply(550, "Can't change root."); 1531a5a4544eSPaul Traina goto bad; 1532a5a4544eSPaul Traina } 1533ce9287fcSYaroslav Tykhiy } else /* real user w/o chroot */ 1534ce9287fcSYaroslav Tykhiy homedir = pw->pw_dir; 1535ce9287fcSYaroslav Tykhiy /* 1536ce9287fcSYaroslav Tykhiy * Set euid *before* doing chdir() so 1537ce9287fcSYaroslav Tykhiy * a) the user won't be carried to a directory that he couldn't reach 1538ce9287fcSYaroslav Tykhiy * on his own due to no permission to upper path components, 1539ce9287fcSYaroslav Tykhiy * b) NFS mounted homedirs w/restrictive permissions will be accessible 1540ce9287fcSYaroslav Tykhiy * (uid 0 has no root power over NFS if not mapped explicitly.) 1541ce9287fcSYaroslav Tykhiy */ 154252e7ee74SYaroslav Tykhiy if (seteuid(pw->pw_uid) < 0) { 1543ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1544ea022d16SRodney W. Grimes goto bad; 1545ea022d16SRodney W. Grimes } 1546ce9287fcSYaroslav Tykhiy if (chdir(homedir) < 0) { 1547ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1548ce9287fcSYaroslav Tykhiy reply(550, "Can't change to base directory."); 1549ce9287fcSYaroslav Tykhiy goto bad; 1550ce9287fcSYaroslav Tykhiy } else { 1551ce9287fcSYaroslav Tykhiy if (chdir("/") < 0) { 1552ce9287fcSYaroslav Tykhiy reply(550, "Root is inaccessible."); 1553ce9287fcSYaroslav Tykhiy goto bad; 1554ce9287fcSYaroslav Tykhiy } 155502c97492SYaroslav Tykhiy lreply(230, "No directory! Logging in with home=/."); 1556ce9287fcSYaroslav Tykhiy } 1557ce9287fcSYaroslav Tykhiy } 155882c76939SDavid Greenman 155982c76939SDavid Greenman /* 1560ea022d16SRodney W. Grimes * Display a login message, if it exists. 1561ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1562ea022d16SRodney W. Grimes */ 1563ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 156463047c6fSDavid E. O'Brien fd = fopen(thishost->loginmsg, "r"); 1565ea4e54b9SDavid Nugent #else 156663047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPLOGINMESG, "r"); 1567ea4e54b9SDavid Nugent #endif 156863047c6fSDavid E. O'Brien if (fd != NULL) { 1569ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1570ea022d16SRodney W. Grimes 1571ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1572ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1573ea022d16SRodney W. Grimes *cp = '\0'; 1574ea022d16SRodney W. Grimes lreply(230, "%s", line); 1575ea022d16SRodney W. Grimes } 1576ea022d16SRodney W. Grimes (void) fflush(stdout); 1577ea022d16SRodney W. Grimes (void) fclose(fd); 1578ea022d16SRodney W. Grimes } 1579ea022d16SRodney W. Grimes if (guest) { 15803eb568f2SGuido van Rooij if (ident != NULL) 15813eb568f2SGuido van Rooij free(ident); 158261f891a6SPaul Traina ident = strdup(passwd); 158361f891a6SPaul Traina if (ident == NULL) 1584618b0bbaSMark Murray fatalerror("Ran out of memory."); 158561f891a6SPaul Traina 1586ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1587ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1588ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1589ea4e54b9SDavid Nugent if (thishost != firsthost) 1590ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1591b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname, 1592b3a0a7cdSMike Heffner passwd); 1593ea4e54b9SDavid Nugent else 1594ea4e54b9SDavid Nugent #endif 1595ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1596b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd); 1597b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1598ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1599ea022d16SRodney W. Grimes if (logging) 1600ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1601ea022d16SRodney W. Grimes remotehost, passwd); 1602ea022d16SRodney W. Grimes } else { 16033401a71fSDaniel O'Callaghan if (dochroot) 160411342ab1SYaroslav Tykhiy reply(230, "User %s logged in, " 160511342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name); 16063401a71fSDaniel O'Callaghan else 1607ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 16083401a71fSDaniel O'Callaghan 1609ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1610ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 16117a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name); 1612b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1613ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1614ea022d16SRodney W. Grimes if (logging) 1615ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1616ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1617ea022d16SRodney W. Grimes } 1618e897216fSYaroslav Tykhiy if (guest || dochroot) 1619e897216fSYaroslav Tykhiy syslog(LOG_INFO, "session root changed to %s", chrootdir); 1620b071c689SDavid Nugent #ifdef LOGIN_CAP 1621b071c689SDavid Nugent login_close(lc); 1622b071c689SDavid Nugent #endif 1623ce9287fcSYaroslav Tykhiy if (residue) 1624ce9287fcSYaroslav Tykhiy free(residue); 1625ea022d16SRodney W. Grimes return; 1626ea022d16SRodney W. Grimes bad: 1627ea022d16SRodney W. Grimes /* Forget all about it... */ 1628b071c689SDavid Nugent #ifdef LOGIN_CAP 1629b071c689SDavid Nugent login_close(lc); 1630b071c689SDavid Nugent #endif 1631ce9287fcSYaroslav Tykhiy if (residue) 1632ce9287fcSYaroslav Tykhiy free(residue); 1633ea022d16SRodney W. Grimes end_login(); 1634ea022d16SRodney W. Grimes } 1635ea022d16SRodney W. Grimes 1636ea022d16SRodney W. Grimes void 1637e4bc453cSWarner Losh retrieve(char *cmd, char *name) 1638ea022d16SRodney W. Grimes { 1639ea022d16SRodney W. Grimes FILE *fin, *dout; 1640ea022d16SRodney W. Grimes struct stat st; 1641e4bc453cSWarner Losh int (*closefunc)(FILE *); 1642158a00b2SJohn Birrell time_t start; 1643ea022d16SRodney W. Grimes 1644ea022d16SRodney W. Grimes if (cmd == 0) { 1645ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1646ea022d16SRodney W. Grimes st.st_size = 0; 1647ea022d16SRodney W. Grimes } else { 1648ea022d16SRodney W. Grimes char line[BUFSIZ]; 1649ea022d16SRodney W. Grimes 1650e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1651ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1652ea022d16SRodney W. Grimes st.st_size = -1; 1653ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1654ea022d16SRodney W. Grimes } 1655ea022d16SRodney W. Grimes if (fin == NULL) { 1656ea022d16SRodney W. Grimes if (errno != 0) { 1657ea022d16SRodney W. Grimes perror_reply(550, name); 1658ea022d16SRodney W. Grimes if (cmd == 0) { 1659ea022d16SRodney W. Grimes LOGCMD("get", name); 1660ea022d16SRodney W. Grimes } 1661ea022d16SRodney W. Grimes } 1662ea022d16SRodney W. Grimes return; 1663ea022d16SRodney W. Grimes } 1664ea022d16SRodney W. Grimes byte_count = -1; 1665ea701226SYaroslav Tykhiy if (cmd == 0) { 1666ea701226SYaroslav Tykhiy if (fstat(fileno(fin), &st) < 0) { 1667ea701226SYaroslav Tykhiy perror_reply(550, name); 1668ea701226SYaroslav Tykhiy goto done; 1669ea701226SYaroslav Tykhiy } 1670ea701226SYaroslav Tykhiy if (!S_ISREG(st.st_mode)) { 167110e89104SYaroslav Tykhiy /* 167210e89104SYaroslav Tykhiy * Never sending a raw directory is a workaround 167310e89104SYaroslav Tykhiy * for buggy clients that will attempt to RETR 167410e89104SYaroslav Tykhiy * a directory before listing it, e.g., Mozilla. 167510e89104SYaroslav Tykhiy * Preventing a guest from getting irregular files 167610e89104SYaroslav Tykhiy * is a simple security measure. 167710e89104SYaroslav Tykhiy */ 167810e89104SYaroslav Tykhiy if (S_ISDIR(st.st_mode) || guest) { 1679ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1680ea022d16SRodney W. Grimes goto done; 1681ea022d16SRodney W. Grimes } 1682ea701226SYaroslav Tykhiy st.st_size = -1; 1683ea701226SYaroslav Tykhiy /* st.st_blksize is set for all descriptor types */ 1684ea701226SYaroslav Tykhiy } 1685ea701226SYaroslav Tykhiy } 1686ea022d16SRodney W. Grimes if (restart_point) { 1687ea022d16SRodney W. Grimes if (type == TYPE_A) { 1688ea022d16SRodney W. Grimes off_t i, n; 1689ea022d16SRodney W. Grimes int c; 1690ea022d16SRodney W. Grimes 1691ea022d16SRodney W. Grimes n = restart_point; 1692ea022d16SRodney W. Grimes i = 0; 1693ea022d16SRodney W. Grimes while (i++ < n) { 1694ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1695ea022d16SRodney W. Grimes perror_reply(550, name); 1696ea022d16SRodney W. Grimes goto done; 1697ea022d16SRodney W. Grimes } 1698ea022d16SRodney W. Grimes if (c == '\n') 1699ea022d16SRodney W. Grimes i++; 1700ea022d16SRodney W. Grimes } 1701ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1702ea022d16SRodney W. Grimes perror_reply(550, name); 1703ea022d16SRodney W. Grimes goto done; 1704ea022d16SRodney W. Grimes } 1705ea022d16SRodney W. Grimes } 1706ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1707ea022d16SRodney W. Grimes if (dout == NULL) 1708ea022d16SRodney W. Grimes goto done; 17093eb568f2SGuido van Rooij time(&start); 17109fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 17119fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 1712c999732bSYaroslav Tykhiy if (cmd == 0 && guest && stats && byte_count > 0) 1713c999732bSYaroslav Tykhiy logxfer(name, byte_count, start); 1714ea022d16SRodney W. Grimes (void) fclose(dout); 1715ea022d16SRodney W. Grimes data = -1; 1716ea022d16SRodney W. Grimes pdata = -1; 1717ea022d16SRodney W. Grimes done: 1718ea022d16SRodney W. Grimes if (cmd == 0) 1719ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1720ea022d16SRodney W. Grimes (*closefunc)(fin); 1721ea022d16SRodney W. Grimes } 1722ea022d16SRodney W. Grimes 1723ea022d16SRodney W. Grimes void 1724e4bc453cSWarner Losh store(char *name, char *mode, int unique) 1725ea022d16SRodney W. Grimes { 1726a117c345SYaroslav Tykhiy int fd; 1727ea022d16SRodney W. Grimes FILE *fout, *din; 1728e4bc453cSWarner Losh int (*closefunc)(FILE *); 1729ea022d16SRodney W. Grimes 1730a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */ 1731a117c345SYaroslav Tykhiy if (unique) { 1732a117c345SYaroslav Tykhiy /* Programming error */ 1733a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE"); 1734a117c345SYaroslav Tykhiy unique = 0; 1735a117c345SYaroslav Tykhiy } 1736a117c345SYaroslav Tykhiy if (guest && noguestmod) { 173702c97492SYaroslav Tykhiy reply(550, "Appending to existing file denied."); 1738a117c345SYaroslav Tykhiy goto err; 1739a117c345SYaroslav Tykhiy } 1740a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */ 1741a117c345SYaroslav Tykhiy } 1742a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */ 1743a117c345SYaroslav Tykhiy restart_point = 0; 1744a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1745a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */ 174602c97492SYaroslav Tykhiy reply(550, "Modifying existing file denied."); 1747a117c345SYaroslav Tykhiy goto err; 1748a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */ 1749a117c345SYaroslav Tykhiy unique = 1; 1750ea022d16SRodney W. Grimes } 1751ea022d16SRodney W. Grimes 1752ea022d16SRodney W. Grimes if (restart_point) 1753a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */ 1754a117c345SYaroslav Tykhiy if (unique) { 1755a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0) 1756a117c345SYaroslav Tykhiy goto err; 1757a117c345SYaroslav Tykhiy fout = fdopen(fd, mode); 1758a117c345SYaroslav Tykhiy } else 1759ea022d16SRodney W. Grimes fout = fopen(name, mode); 1760ea022d16SRodney W. Grimes closefunc = fclose; 1761ea022d16SRodney W. Grimes if (fout == NULL) { 1762ea022d16SRodney W. Grimes perror_reply(553, name); 1763a117c345SYaroslav Tykhiy goto err; 1764ea022d16SRodney W. Grimes } 1765ea022d16SRodney W. Grimes byte_count = -1; 1766ea022d16SRodney W. Grimes if (restart_point) { 1767ea022d16SRodney W. Grimes if (type == TYPE_A) { 1768ea022d16SRodney W. Grimes off_t i, n; 1769ea022d16SRodney W. Grimes int c; 1770ea022d16SRodney W. Grimes 1771ea022d16SRodney W. Grimes n = restart_point; 1772ea022d16SRodney W. Grimes i = 0; 1773ea022d16SRodney W. Grimes while (i++ < n) { 1774ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1775ea022d16SRodney W. Grimes perror_reply(550, name); 1776ea022d16SRodney W. Grimes goto done; 1777ea022d16SRodney W. Grimes } 1778ea022d16SRodney W. Grimes if (c == '\n') 1779ea022d16SRodney W. Grimes i++; 1780ea022d16SRodney W. Grimes } 1781ea022d16SRodney W. Grimes /* 1782ea022d16SRodney W. Grimes * We must do this seek to "current" position 1783ea022d16SRodney W. Grimes * because we are changing from reading to 1784ea022d16SRodney W. Grimes * writing. 1785ea022d16SRodney W. Grimes */ 17860e519c96SYaroslav Tykhiy if (fseeko(fout, 0, SEEK_CUR) < 0) { 1787ea022d16SRodney W. Grimes perror_reply(550, name); 1788ea022d16SRodney W. Grimes goto done; 1789ea022d16SRodney W. Grimes } 1790ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1791ea022d16SRodney W. Grimes perror_reply(550, name); 1792ea022d16SRodney W. Grimes goto done; 1793ea022d16SRodney W. Grimes } 1794ea022d16SRodney W. Grimes } 17950e519c96SYaroslav Tykhiy din = dataconn(name, -1, "r"); 1796ea022d16SRodney W. Grimes if (din == NULL) 1797ea022d16SRodney W. Grimes goto done; 1798ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1799ea022d16SRodney W. Grimes if (unique) 1800ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1801ea022d16SRodney W. Grimes name); 1802ea022d16SRodney W. Grimes else 1803ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1804ea022d16SRodney W. Grimes } 1805ea022d16SRodney W. Grimes (void) fclose(din); 1806ea022d16SRodney W. Grimes data = -1; 1807ea022d16SRodney W. Grimes pdata = -1; 1808ea022d16SRodney W. Grimes done: 18097c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); 1810ea022d16SRodney W. Grimes (*closefunc)(fout); 1811a117c345SYaroslav Tykhiy return; 1812a117c345SYaroslav Tykhiy err: 1813a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name); 1814a117c345SYaroslav Tykhiy return; 1815ea022d16SRodney W. Grimes } 1816ea022d16SRodney W. Grimes 1817ea022d16SRodney W. Grimes static FILE * 1818e4bc453cSWarner Losh getdatasock(char *mode) 1819ea022d16SRodney W. Grimes { 1820ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1821ea022d16SRodney W. Grimes 1822ea022d16SRodney W. Grimes if (data >= 0) 1823ea022d16SRodney W. Grimes return (fdopen(data, mode)); 18244dd8b5abSYoshinobu Inoue 18254dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1826ea022d16SRodney W. Grimes if (s < 0) 1827ea022d16SRodney W. Grimes goto bad; 182857d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1829406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1830ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 18314dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 183263591ba5SYaroslav Tykhiy data_source.su_port = htons(dataport); 183352e7ee74SYaroslav Tykhiy (void) seteuid(0); 1834ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 18359ec7612aSYaroslav Tykhiy /* 18369ec7612aSYaroslav Tykhiy * We should loop here since it's possible that 18379ec7612aSYaroslav Tykhiy * another ftpd instance has passed this point and is 18389ec7612aSYaroslav Tykhiy * trying to open a data connection in active mode now. 18399ec7612aSYaroslav Tykhiy * Until the other connection is opened, we'll be getting 18409ec7612aSYaroslav Tykhiy * EADDRINUSE because no SOCK_STREAM sockets in the system 18419ec7612aSYaroslav Tykhiy * can share both local and remote addresses, localIP:20 18429ec7612aSYaroslav Tykhiy * and *:* in this case. 18439ec7612aSYaroslav Tykhiy */ 1844ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 18454dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1846ea022d16SRodney W. Grimes break; 1847ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1848ea022d16SRodney W. Grimes goto bad; 1849ea022d16SRodney W. Grimes sleep(tries); 1850ea022d16SRodney W. Grimes } 185152e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 1852ea022d16SRodney W. Grimes #ifdef IP_TOS 18534dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 18544dd8b5abSYoshinobu Inoue { 1855ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 185657d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1857406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 18584dd8b5abSYoshinobu Inoue } 1859ea022d16SRodney W. Grimes #endif 18609fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 18619fc5823aSGarrett Wollman /* 18629fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 18639fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 18649fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 18659fc5823aSGarrett Wollman * in heavy-load situations. 18669fc5823aSGarrett Wollman */ 18679fc5823aSGarrett Wollman on = 1; 186857d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1869406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 18709fc5823aSGarrett Wollman #endif 18719fc5823aSGarrett Wollman #ifdef SO_SNDBUF 18729fc5823aSGarrett Wollman on = 65536; 187357d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0) 1874406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m"); 18759fc5823aSGarrett Wollman #endif 18769fc5823aSGarrett Wollman 1877ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1878ea022d16SRodney W. Grimes bad: 1879ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1880ea022d16SRodney W. Grimes t = errno; 188152e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 1882ea022d16SRodney W. Grimes (void) close(s); 1883ea022d16SRodney W. Grimes errno = t; 1884ea022d16SRodney W. Grimes return (NULL); 1885ea022d16SRodney W. Grimes } 1886ea022d16SRodney W. Grimes 1887ea022d16SRodney W. Grimes static FILE * 1888e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode) 1889ea022d16SRodney W. Grimes { 1890ea022d16SRodney W. Grimes char sizebuf[32]; 1891ea022d16SRodney W. Grimes FILE *file; 1892e5094456SCrist J. Clark int retry = 0, tos, conerrno; 1893ea022d16SRodney W. Grimes 1894ea022d16SRodney W. Grimes file_size = size; 1895ea022d16SRodney W. Grimes byte_count = 0; 18960e519c96SYaroslav Tykhiy if (size != -1) 1897a57e1ef0SYaroslav Tykhiy (void) snprintf(sizebuf, sizeof(sizebuf), 1898a57e1ef0SYaroslav Tykhiy " (%jd bytes)", (intmax_t)size); 1899ea022d16SRodney W. Grimes else 1900e760ef2cSWarner Losh *sizebuf = '\0'; 1901ea022d16SRodney W. Grimes if (pdata >= 0) { 19024dd8b5abSYoshinobu Inoue union sockunion from; 19034cd48bacSYaroslav Tykhiy int flags; 19044dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1905d6ed3c37SGuido van Rooij struct timeval timeout; 1906d6ed3c37SGuido van Rooij fd_set set; 1907ea022d16SRodney W. Grimes 1908d6ed3c37SGuido van Rooij FD_ZERO(&set); 1909d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1910d6ed3c37SGuido van Rooij 1911d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1912d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1913d6ed3c37SGuido van Rooij 19144cd48bacSYaroslav Tykhiy /* 19154cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode, 19164cd48bacSYaroslav Tykhiy * accept() will block after a successful select() 19174cd48bacSYaroslav Tykhiy * if the selected connection dies in between. 19184cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here. 19194cd48bacSYaroslav Tykhiy */ 19204cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 19214cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 19224cd48bacSYaroslav Tykhiy goto pdata_err; 1923aa5a9d3fSYaroslav Tykhiy if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 || 19244cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 19254cd48bacSYaroslav Tykhiy goto pdata_err; 1926ea022d16SRodney W. Grimes (void) close(pdata); 1927ea022d16SRodney W. Grimes pdata = s; 19284cd48bacSYaroslav Tykhiy /* 1929f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag 1930f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it. 19314cd48bacSYaroslav Tykhiy */ 19324cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 19334cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 19344cd48bacSYaroslav Tykhiy goto pdata_err; 1935ea022d16SRodney W. Grimes #ifdef IP_TOS 19364dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 19374dd8b5abSYoshinobu Inoue { 1938a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 193957d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1940406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 19414dd8b5abSYoshinobu Inoue } 1942ea022d16SRodney W. Grimes #endif 1943ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1944ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1945ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 19464cd48bacSYaroslav Tykhiy pdata_err: 19474cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection."); 19484cd48bacSYaroslav Tykhiy (void) close(pdata); 19494cd48bacSYaroslav Tykhiy pdata = -1; 19504cd48bacSYaroslav Tykhiy return (NULL); 1951ea022d16SRodney W. Grimes } 1952ea022d16SRodney W. Grimes if (data >= 0) { 1953ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1954ea022d16SRodney W. Grimes name, sizebuf); 1955ea022d16SRodney W. Grimes usedefault = 1; 1956ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1957ea022d16SRodney W. Grimes } 1958ea022d16SRodney W. Grimes if (usedefault) 1959ea022d16SRodney W. Grimes data_dest = his_addr; 1960ea022d16SRodney W. Grimes usedefault = 1; 1961e5094456SCrist J. Clark do { 1962ea022d16SRodney W. Grimes file = getdatasock(mode); 1963ea022d16SRodney W. Grimes if (file == NULL) { 196404683b2cSYaroslav Tykhiy char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV]; 196504683b2cSYaroslav Tykhiy 196604683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&data_source, 196704683b2cSYaroslav Tykhiy data_source.su_len, 196804683b2cSYaroslav Tykhiy hostbuf, sizeof(hostbuf) - 1, 196904683b2cSYaroslav Tykhiy portbuf, sizeof(portbuf) - 1, 197004683b2cSYaroslav Tykhiy NI_NUMERICHOST|NI_NUMERICSERV)) 197104683b2cSYaroslav Tykhiy *hostbuf = *portbuf = 0; 197204683b2cSYaroslav Tykhiy hostbuf[sizeof(hostbuf) - 1] = 0; 197304683b2cSYaroslav Tykhiy portbuf[sizeof(portbuf) - 1] = 0; 19744dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 19754dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1976ea022d16SRodney W. Grimes return (NULL); 1977ea022d16SRodney W. Grimes } 1978ea022d16SRodney W. Grimes data = fileno(file); 1979e5094456SCrist J. Clark conerrno = 0; 1980e5094456SCrist J. Clark if (connect(data, (struct sockaddr *)&data_dest, 1981e5094456SCrist J. Clark data_dest.su_len) == 0) 1982e5094456SCrist J. Clark break; 1983e5094456SCrist J. Clark conerrno = errno; 1984ea022d16SRodney W. Grimes (void) fclose(file); 1985ea022d16SRodney W. Grimes data = -1; 1986e5094456SCrist J. Clark if (conerrno == EADDRINUSE) { 1987e3765043SYaroslav Tykhiy sleep(swaitint); 1988e5094456SCrist J. Clark retry += swaitint; 1989e5094456SCrist J. Clark } else { 1990e5094456SCrist J. Clark break; 1991e5094456SCrist J. Clark } 1992e5094456SCrist J. Clark } while (retry <= swaitmax); 1993e5094456SCrist J. Clark if (conerrno != 0) { 199482c03024SYaroslav Tykhiy reply(425, "Can't build data connection: %s.", 199582c03024SYaroslav Tykhiy strerror(conerrno)); 1996ea022d16SRodney W. Grimes return (NULL); 1997ea022d16SRodney W. Grimes } 1998ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1999ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 2000ea022d16SRodney W. Grimes return (file); 2001ea022d16SRodney W. Grimes } 2002ea022d16SRodney W. Grimes 2003ea022d16SRodney W. Grimes /* 20044cd51076SYaroslav Tykhiy * A helper macro to avoid code duplication 20054cd51076SYaroslav Tykhiy * in send_data() and receive_data(). 20064cd51076SYaroslav Tykhiy * 20074cd51076SYaroslav Tykhiy * XXX We have to block SIGURG during putc() because BSD stdio 20084cd51076SYaroslav Tykhiy * is unable to restart interrupted write operations and hence 20094cd51076SYaroslav Tykhiy * the entire buffer contents will be lost as soon as a write() 20104cd51076SYaroslav Tykhiy * call indicates EINTR to stdio. 20114cd51076SYaroslav Tykhiy */ 20124cd51076SYaroslav Tykhiy #define FTPD_PUTC(ch, file, label) \ 20134cd51076SYaroslav Tykhiy do { \ 20144cd51076SYaroslav Tykhiy int ret; \ 20154cd51076SYaroslav Tykhiy \ 20164cd51076SYaroslav Tykhiy do { \ 20174cd51076SYaroslav Tykhiy START_UNSAFE; \ 20184cd51076SYaroslav Tykhiy ret = putc((ch), (file)); \ 20194cd51076SYaroslav Tykhiy END_UNSAFE; \ 20204cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) \ 20214cd51076SYaroslav Tykhiy else if (ferror(file)) \ 20224cd51076SYaroslav Tykhiy goto label; \ 20234cd51076SYaroslav Tykhiy clearerr(file); \ 20244cd51076SYaroslav Tykhiy } while (ret == EOF); \ 20254cd51076SYaroslav Tykhiy } while (0) 20264cd51076SYaroslav Tykhiy 20274cd51076SYaroslav Tykhiy /* 2028ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 20299fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 2030ea022d16SRodney W. Grimes * 2031ea022d16SRodney W. Grimes * NB: Form isn't handled. 2032ea022d16SRodney W. Grimes */ 20334b82fc95SYaroslav Tykhiy static int 20346e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg) 2035ea022d16SRodney W. Grimes { 2036db1c2da3SYaroslav Tykhiy int c, cp, filefd, netfd; 2037f6f0c4b9SDan Moschuk char *buf; 2038ea022d16SRodney W. Grimes 20394cd51076SYaroslav Tykhiy STARTXFER; 20404cd51076SYaroslav Tykhiy 2041ea022d16SRodney W. Grimes switch (type) { 2042ea022d16SRodney W. Grimes 2043ea022d16SRodney W. Grimes case TYPE_A: 20444cd51076SYaroslav Tykhiy cp = EOF; 20454cd51076SYaroslav Tykhiy for (;;) { 20464cd51076SYaroslav Tykhiy c = getc(instr); 20474cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 20484cd51076SYaroslav Tykhiy else if (c == EOF && ferror(instr)) 20494cd51076SYaroslav Tykhiy goto file_err; 20504cd51076SYaroslav Tykhiy if (c == EOF) { 20514cd51076SYaroslav Tykhiy if (ferror(instr)) { /* resume after OOB */ 20524cd51076SYaroslav Tykhiy clearerr(instr); 20534cd51076SYaroslav Tykhiy continue; 2054ea022d16SRodney W. Grimes } 20554cd51076SYaroslav Tykhiy if (feof(instr)) /* EOF */ 20564cd51076SYaroslav Tykhiy break; 20574cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: impossible condition" 20584cd51076SYaroslav Tykhiy " on file after getc()"); 20594cd51076SYaroslav Tykhiy goto file_err; 20604cd51076SYaroslav Tykhiy } 20614cd51076SYaroslav Tykhiy if (c == '\n' && cp != '\r') { 20624cd51076SYaroslav Tykhiy FTPD_PUTC('\r', outstr, data_err); 20634cd51076SYaroslav Tykhiy byte_count++; 20644cd51076SYaroslav Tykhiy } 20654cd51076SYaroslav Tykhiy FTPD_PUTC(c, outstr, data_err); 20664cd51076SYaroslav Tykhiy byte_count++; 2067db1c2da3SYaroslav Tykhiy cp = c; 2068ea022d16SRodney W. Grimes } 20694cd51076SYaroslav Tykhiy #ifdef notyet /* BSD stdio isn't ready for that */ 20704cd51076SYaroslav Tykhiy while (fflush(outstr) == EOF) { 20714cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 20724cd51076SYaroslav Tykhiy else 2073ea022d16SRodney W. Grimes goto data_err; 20744cd51076SYaroslav Tykhiy clearerr(outstr); 20754cd51076SYaroslav Tykhiy } 20764cd51076SYaroslav Tykhiy ENDXFER; 20774cd51076SYaroslav Tykhiy #else 20784cd51076SYaroslav Tykhiy ENDXFER; 20794cd51076SYaroslav Tykhiy if (fflush(outstr) == EOF) 20804cd51076SYaroslav Tykhiy goto data_err; 20814cd51076SYaroslav Tykhiy #endif 2082ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 20834b82fc95SYaroslav Tykhiy return (0); 2084ea022d16SRodney W. Grimes 2085ea022d16SRodney W. Grimes case TYPE_I: 2086ea022d16SRodney W. Grimes case TYPE_L: 20879fc5823aSGarrett Wollman /* 20889fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 20899fc5823aSGarrett Wollman * are sending a regular file 20909fc5823aSGarrett Wollman */ 20919fc5823aSGarrett Wollman netfd = fileno(outstr); 20929fc5823aSGarrett Wollman filefd = fileno(instr); 20939fc5823aSGarrett Wollman 2094f6f0c4b9SDan Moschuk if (isreg) { 20952e22b914SYaroslav Tykhiy char *msg = "Transfer complete."; 20964cd51076SYaroslav Tykhiy off_t cnt, offset; 2097f6f0c4b9SDan Moschuk int err; 2098f6f0c4b9SDan Moschuk 20992f492fc8SYaroslav Tykhiy cnt = offset = 0; 2100f6f0c4b9SDan Moschuk 21012f492fc8SYaroslav Tykhiy while (filesize > 0) { 2102492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0, 21037e295315SYaroslav Tykhiy NULL, &cnt, 0); 21043af48c42SMaxim Konovalov /* 21053af48c42SMaxim Konovalov * Calculate byte_count before OOB processing. 21063af48c42SMaxim Konovalov * It can be used in myoob() later. 21073af48c42SMaxim Konovalov */ 21083af48c42SMaxim Konovalov byte_count += cnt; 2109f6f0c4b9SDan Moschuk offset += cnt; 2110492f1d9cSMaxim Konovalov filesize -= cnt; 21114cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 21124cd51076SYaroslav Tykhiy else if (err == -1) { 21134cd51076SYaroslav Tykhiy if (errno != EINTR && 21144cd51076SYaroslav Tykhiy cnt == 0 && offset == 0) 2115f6f0c4b9SDan Moschuk goto oldway; 21169fc5823aSGarrett Wollman goto data_err; 2117f6f0c4b9SDan Moschuk } 21184cd51076SYaroslav Tykhiy if (err == -1) /* resume after OOB */ 21194cd51076SYaroslav Tykhiy continue; 21202e22b914SYaroslav Tykhiy /* 21212e22b914SYaroslav Tykhiy * We hit the EOF prematurely. 21222e22b914SYaroslav Tykhiy * Perhaps the file was externally truncated. 21232e22b914SYaroslav Tykhiy */ 21242e22b914SYaroslav Tykhiy if (cnt == 0) { 21252e22b914SYaroslav Tykhiy msg = "Transfer finished due to " 21262e22b914SYaroslav Tykhiy "premature end of file."; 21272e22b914SYaroslav Tykhiy break; 21282e22b914SYaroslav Tykhiy } 2129f6f0c4b9SDan Moschuk } 21304cd51076SYaroslav Tykhiy ENDXFER; 21312e22b914SYaroslav Tykhiy reply(226, msg); 21324b82fc95SYaroslav Tykhiy return (0); 21339fc5823aSGarrett Wollman } 21349fc5823aSGarrett Wollman 21359fc5823aSGarrett Wollman oldway: 2136e3765043SYaroslav Tykhiy if ((buf = malloc(blksize)) == NULL) { 21374cd51076SYaroslav Tykhiy ENDXFER; 213802c97492SYaroslav Tykhiy reply(451, "Ran out of memory."); 21394b82fc95SYaroslav Tykhiy return (-1); 2140ea022d16SRodney W. Grimes } 21419fc5823aSGarrett Wollman 21424cd51076SYaroslav Tykhiy for (;;) { 21434cd51076SYaroslav Tykhiy int cnt, len; 21444cd51076SYaroslav Tykhiy char *bp; 21454cd51076SYaroslav Tykhiy 21464cd51076SYaroslav Tykhiy cnt = read(filefd, buf, blksize); 21474cd51076SYaroslav Tykhiy CHECKOOB(free(buf); return (-1)) 21484cd51076SYaroslav Tykhiy else if (cnt < 0) { 21498efc8b18SYaroslav Tykhiy free(buf); 2150ea022d16SRodney W. Grimes goto file_err; 21514cd51076SYaroslav Tykhiy } 21524cd51076SYaroslav Tykhiy if (cnt < 0) /* resume after OOB */ 21534cd51076SYaroslav Tykhiy continue; 21544cd51076SYaroslav Tykhiy if (cnt == 0) /* EOF */ 21554cd51076SYaroslav Tykhiy break; 21564cd51076SYaroslav Tykhiy for (len = cnt, bp = buf; len > 0;) { 21574cd51076SYaroslav Tykhiy cnt = write(netfd, bp, len); 21584cd51076SYaroslav Tykhiy CHECKOOB(free(buf); return (-1)) 21594cd51076SYaroslav Tykhiy else if (cnt < 0) { 21604cd51076SYaroslav Tykhiy free(buf); 2161ea022d16SRodney W. Grimes goto data_err; 2162ea022d16SRodney W. Grimes } 21634cd51076SYaroslav Tykhiy if (cnt <= 0) 21644cd51076SYaroslav Tykhiy continue; 21654cd51076SYaroslav Tykhiy len -= cnt; 21664cd51076SYaroslav Tykhiy bp += cnt; 21674cd51076SYaroslav Tykhiy byte_count += cnt; 21684cd51076SYaroslav Tykhiy } 21694cd51076SYaroslav Tykhiy } 21704cd51076SYaroslav Tykhiy ENDXFER; 21714cd51076SYaroslav Tykhiy free(buf); 2172ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 21734b82fc95SYaroslav Tykhiy return (0); 2174ea022d16SRodney W. Grimes default: 21754cd51076SYaroslav Tykhiy ENDXFER; 217602c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in send_data.", type); 21774b82fc95SYaroslav Tykhiy return (-1); 2178ea022d16SRodney W. Grimes } 2179ea022d16SRodney W. Grimes 2180ea022d16SRodney W. Grimes data_err: 21814cd51076SYaroslav Tykhiy ENDXFER; 2182ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 21834b82fc95SYaroslav Tykhiy return (-1); 2184ea022d16SRodney W. Grimes 2185ea022d16SRodney W. Grimes file_err: 21864cd51076SYaroslav Tykhiy ENDXFER; 2187ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 21884b82fc95SYaroslav Tykhiy return (-1); 2189ea022d16SRodney W. Grimes } 2190ea022d16SRodney W. Grimes 2191ea022d16SRodney W. Grimes /* 2192ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 2193ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 2194ea022d16SRodney W. Grimes * 2195ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 2196ea022d16SRodney W. Grimes */ 2197ea022d16SRodney W. Grimes static int 2198e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr) 2199ea022d16SRodney W. Grimes { 22004cd51076SYaroslav Tykhiy int c, cp; 22014cd51076SYaroslav Tykhiy int bare_lfs = 0; 2202ea022d16SRodney W. Grimes 22034cd51076SYaroslav Tykhiy STARTXFER; 220461f891a6SPaul Traina 2205ea022d16SRodney W. Grimes switch (type) { 2206ea022d16SRodney W. Grimes 2207ea022d16SRodney W. Grimes case TYPE_I: 2208ea022d16SRodney W. Grimes case TYPE_L: 22094cd51076SYaroslav Tykhiy for (;;) { 22104cd51076SYaroslav Tykhiy int cnt, len; 22114cd51076SYaroslav Tykhiy char *bp; 22124cd51076SYaroslav Tykhiy char buf[BUFSIZ]; 22134cd51076SYaroslav Tykhiy 22144cd51076SYaroslav Tykhiy cnt = read(fileno(instr), buf, sizeof(buf)); 22154cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22164cd51076SYaroslav Tykhiy else if (cnt < 0) 22174cd51076SYaroslav Tykhiy goto data_err; 22184cd51076SYaroslav Tykhiy if (cnt < 0) /* resume after OOB */ 22194cd51076SYaroslav Tykhiy continue; 22204cd51076SYaroslav Tykhiy if (cnt == 0) /* EOF */ 22214cd51076SYaroslav Tykhiy break; 22224cd51076SYaroslav Tykhiy for (len = cnt, bp = buf; len > 0;) { 22234cd51076SYaroslav Tykhiy cnt = write(fileno(outstr), bp, len); 22244cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22254cd51076SYaroslav Tykhiy else if (cnt < 0) 2226ea022d16SRodney W. Grimes goto file_err; 22274cd51076SYaroslav Tykhiy if (cnt <= 0) 22284cd51076SYaroslav Tykhiy continue; 22294cd51076SYaroslav Tykhiy len -= cnt; 22304cd51076SYaroslav Tykhiy bp += cnt; 2231ea022d16SRodney W. Grimes byte_count += cnt; 2232ea022d16SRodney W. Grimes } 22334cd51076SYaroslav Tykhiy } 22344cd51076SYaroslav Tykhiy ENDXFER; 2235ea022d16SRodney W. Grimes return (0); 2236ea022d16SRodney W. Grimes 2237ea022d16SRodney W. Grimes case TYPE_E: 22384cd51076SYaroslav Tykhiy ENDXFER; 2239ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 2240ea022d16SRodney W. Grimes return (-1); 2241ea022d16SRodney W. Grimes 2242ea022d16SRodney W. Grimes case TYPE_A: 22434cd51076SYaroslav Tykhiy cp = EOF; 22444cd51076SYaroslav Tykhiy for (;;) { 22454cd51076SYaroslav Tykhiy c = getc(instr); 22464cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22474cd51076SYaroslav Tykhiy else if (c == EOF && ferror(instr)) 22484cd51076SYaroslav Tykhiy goto data_err; 22494cd51076SYaroslav Tykhiy if (c == EOF && ferror(instr)) { /* resume after OOB */ 22504cd51076SYaroslav Tykhiy clearerr(instr); 22514cd51076SYaroslav Tykhiy continue; 22524cd51076SYaroslav Tykhiy } 22534cd51076SYaroslav Tykhiy 22544cd51076SYaroslav Tykhiy if (cp == '\r') { 22554cd51076SYaroslav Tykhiy if (c != '\n') 22564cd51076SYaroslav Tykhiy FTPD_PUTC('\r', outstr, file_err); 22574cd51076SYaroslav Tykhiy } else 2258ea022d16SRodney W. Grimes if (c == '\n') 2259ea022d16SRodney W. Grimes bare_lfs++; 22604cd51076SYaroslav Tykhiy if (c == '\r') { 22614cd51076SYaroslav Tykhiy byte_count++; 22624cd51076SYaroslav Tykhiy cp = c; 22634cd51076SYaroslav Tykhiy continue; 22644cd51076SYaroslav Tykhiy } 22654cd51076SYaroslav Tykhiy 22664cd51076SYaroslav Tykhiy /* Check for EOF here in order not to lose last \r. */ 22674cd51076SYaroslav Tykhiy if (c == EOF) { 22684cd51076SYaroslav Tykhiy if (feof(instr)) /* EOF */ 22694cd51076SYaroslav Tykhiy break; 22704cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: impossible condition" 22714cd51076SYaroslav Tykhiy " on data stream after getc()"); 2272ea022d16SRodney W. Grimes goto data_err; 2273ea022d16SRodney W. Grimes } 22744cd51076SYaroslav Tykhiy 22754cd51076SYaroslav Tykhiy byte_count++; 22764cd51076SYaroslav Tykhiy FTPD_PUTC(c, outstr, file_err); 22774cd51076SYaroslav Tykhiy cp = c; 2278ea022d16SRodney W. Grimes } 22794cd51076SYaroslav Tykhiy #ifdef notyet /* BSD stdio isn't ready for that */ 22804cd51076SYaroslav Tykhiy while (fflush(outstr) == EOF) { 22814cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) 22824cd51076SYaroslav Tykhiy else 2283ea022d16SRodney W. Grimes goto file_err; 22844cd51076SYaroslav Tykhiy clearerr(outstr); 22854cd51076SYaroslav Tykhiy } 22864cd51076SYaroslav Tykhiy ENDXFER; 22874cd51076SYaroslav Tykhiy #else 22884cd51076SYaroslav Tykhiy ENDXFER; 22894cd51076SYaroslav Tykhiy if (fflush(outstr) == EOF) 22904cd51076SYaroslav Tykhiy goto file_err; 22914cd51076SYaroslav Tykhiy #endif 2292ea022d16SRodney W. Grimes if (bare_lfs) { 2293ea022d16SRodney W. Grimes lreply(226, 229402c97492SYaroslav Tykhiy "WARNING! %d bare linefeeds received in ASCII mode.", 2295ea022d16SRodney W. Grimes bare_lfs); 2296ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 2297ea022d16SRodney W. Grimes } 2298ea022d16SRodney W. Grimes return (0); 2299ea022d16SRodney W. Grimes default: 23004cd51076SYaroslav Tykhiy ENDXFER; 230102c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in receive_data.", type); 2302ea022d16SRodney W. Grimes return (-1); 2303ea022d16SRodney W. Grimes } 2304ea022d16SRodney W. Grimes 2305ea022d16SRodney W. Grimes data_err: 23064cd51076SYaroslav Tykhiy ENDXFER; 230702c97492SYaroslav Tykhiy perror_reply(426, "Data connection"); 2308ea022d16SRodney W. Grimes return (-1); 2309ea022d16SRodney W. Grimes 2310ea022d16SRodney W. Grimes file_err: 23114cd51076SYaroslav Tykhiy ENDXFER; 231202c97492SYaroslav Tykhiy perror_reply(452, "Error writing to file"); 2313ea022d16SRodney W. Grimes return (-1); 2314ea022d16SRodney W. Grimes } 2315ea022d16SRodney W. Grimes 2316ea022d16SRodney W. Grimes void 2317e4bc453cSWarner Losh statfilecmd(char *filename) 2318ea022d16SRodney W. Grimes { 2319ea022d16SRodney W. Grimes FILE *fin; 2320f8a581a0SYaroslav Tykhiy int atstart; 232141c57b48SYaroslav Tykhiy int c, code; 2322ea022d16SRodney W. Grimes char line[LINE_MAX]; 232341c57b48SYaroslav Tykhiy struct stat st; 2324ea022d16SRodney W. Grimes 232541c57b48SYaroslav Tykhiy code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213; 2326af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2327ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 23283b48b877SYaroslav Tykhiy lreply(code, "Status of %s:", filename); 2329f8a581a0SYaroslav Tykhiy atstart = 1; 2330ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 2331ea022d16SRodney W. Grimes if (c == '\n') { 2332ea022d16SRodney W. Grimes if (ferror(stdout)){ 233302c97492SYaroslav Tykhiy perror_reply(421, "Control connection"); 2334ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2335ea022d16SRodney W. Grimes dologout(1); 2336ea022d16SRodney W. Grimes /* NOTREACHED */ 2337ea022d16SRodney W. Grimes } 2338ea022d16SRodney W. Grimes if (ferror(fin)) { 2339ea022d16SRodney W. Grimes perror_reply(551, filename); 2340ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2341ea022d16SRodney W. Grimes return; 2342ea022d16SRodney W. Grimes } 2343ea022d16SRodney W. Grimes (void) putc('\r', stdout); 2344ea022d16SRodney W. Grimes } 2345f8a581a0SYaroslav Tykhiy /* 2346f8a581a0SYaroslav Tykhiy * RFC 959 says neutral text should be prepended before 2347f8a581a0SYaroslav Tykhiy * a leading 3-digit number followed by whitespace, but 2348f8a581a0SYaroslav Tykhiy * many ftp clients can be confused by any leading digits, 2349f8a581a0SYaroslav Tykhiy * as a matter of fact. 2350f8a581a0SYaroslav Tykhiy */ 2351f8a581a0SYaroslav Tykhiy if (atstart && isdigit(c)) 2352f8a581a0SYaroslav Tykhiy (void) putc(' ', stdout); 2353ea022d16SRodney W. Grimes (void) putc(c, stdout); 2354f8a581a0SYaroslav Tykhiy atstart = (c == '\n'); 2355ea022d16SRodney W. Grimes } 2356ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 235702c97492SYaroslav Tykhiy reply(code, "End of status."); 2358ea022d16SRodney W. Grimes } 2359ea022d16SRodney W. Grimes 2360ea022d16SRodney W. Grimes void 2361e4bc453cSWarner Losh statcmd(void) 2362ea022d16SRodney W. Grimes { 23634dd8b5abSYoshinobu Inoue union sockunion *su; 2364ea022d16SRodney W. Grimes u_char *a, *p; 2365b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST]; 23664dd8b5abSYoshinobu Inoue int ispassive; 2367ea022d16SRodney W. Grimes 2368c152df28SYaroslav Tykhiy if (hostinfo) { 2369a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname); 2370ea022d16SRodney W. Grimes printf(" %s\r\n", version); 2371c152df28SYaroslav Tykhiy } else 2372c152df28SYaroslav Tykhiy lreply(211, "FTP server status:"); 2373ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 23744dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2375b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 237604683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0; 23774dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 23784dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 23794dd8b5abSYoshinobu Inoue } 2380ea022d16SRodney W. Grimes printf("\r\n"); 2381ea022d16SRodney W. Grimes if (logged_in) { 2382ea022d16SRodney W. Grimes if (guest) 2383ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 2384ea022d16SRodney W. Grimes else 2385ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 2386ea022d16SRodney W. Grimes } else if (askpasswd) 2387ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 2388ea022d16SRodney W. Grimes else 2389ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 2390ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 2391ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 2392ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2393ea022d16SRodney W. Grimes if (type == TYPE_L) 239489fdc4e1SMike Barcroft #if CHAR_BIT == 8 239589fdc4e1SMike Barcroft printf(" %d", CHAR_BIT); 2396ea022d16SRodney W. Grimes #else 2397ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2398ea022d16SRodney W. Grimes #endif 2399ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2400ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2401ea022d16SRodney W. Grimes if (data != -1) 2402ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2403ea022d16SRodney W. Grimes else if (pdata != -1) { 24044dd8b5abSYoshinobu Inoue ispassive = 1; 24054dd8b5abSYoshinobu Inoue su = &pasv_addr; 2406ea022d16SRodney W. Grimes goto printaddr; 2407ea022d16SRodney W. Grimes } else if (usedefault == 0) { 24084dd8b5abSYoshinobu Inoue ispassive = 0; 24094dd8b5abSYoshinobu Inoue su = &data_dest; 2410ea022d16SRodney W. Grimes printaddr: 2411ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 24124dd8b5abSYoshinobu Inoue if (epsvall) { 24134dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 24144dd8b5abSYoshinobu Inoue goto epsvonly; 24154dd8b5abSYoshinobu Inoue } 24164dd8b5abSYoshinobu Inoue 24174dd8b5abSYoshinobu Inoue /* PORT/PASV */ 24184dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 24194dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 24204dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 24214dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 24224dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 24234dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 24244dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 24254dd8b5abSYoshinobu Inoue } 24264dd8b5abSYoshinobu Inoue 24274dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 24284dd8b5abSYoshinobu Inoue { 24294dd8b5abSYoshinobu Inoue int alen, af, i; 24304dd8b5abSYoshinobu Inoue 24314dd8b5abSYoshinobu Inoue switch (su->su_family) { 24324dd8b5abSYoshinobu Inoue case AF_INET: 24334dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 24344dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 24354dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 24364dd8b5abSYoshinobu Inoue af = 4; 24374dd8b5abSYoshinobu Inoue break; 24384dd8b5abSYoshinobu Inoue case AF_INET6: 24394dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 24404dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 24414dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 24424dd8b5abSYoshinobu Inoue af = 6; 24434dd8b5abSYoshinobu Inoue break; 24444dd8b5abSYoshinobu Inoue default: 24454dd8b5abSYoshinobu Inoue af = 0; 24464dd8b5abSYoshinobu Inoue break; 24474dd8b5abSYoshinobu Inoue } 24484dd8b5abSYoshinobu Inoue if (af) { 24494dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 24504dd8b5abSYoshinobu Inoue af, alen); 24514dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 24524dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 24534dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 24544dd8b5abSYoshinobu Inoue } 24554dd8b5abSYoshinobu Inoue } 24564dd8b5abSYoshinobu Inoue 24574dd8b5abSYoshinobu Inoue epsvonly:; 24584dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 24594dd8b5abSYoshinobu Inoue { 24604dd8b5abSYoshinobu Inoue int af; 24614dd8b5abSYoshinobu Inoue 24624dd8b5abSYoshinobu Inoue switch (su->su_family) { 24634dd8b5abSYoshinobu Inoue case AF_INET: 24644dd8b5abSYoshinobu Inoue af = 1; 24654dd8b5abSYoshinobu Inoue break; 24664dd8b5abSYoshinobu Inoue case AF_INET6: 24674dd8b5abSYoshinobu Inoue af = 2; 24684dd8b5abSYoshinobu Inoue break; 24694dd8b5abSYoshinobu Inoue default: 24704dd8b5abSYoshinobu Inoue af = 0; 24714dd8b5abSYoshinobu Inoue break; 24724dd8b5abSYoshinobu Inoue } 24734dd8b5abSYoshinobu Inoue if (af) { 2474b0f06defSHajimu UMEMOTO union sockunion tmp; 2475b0f06defSHajimu UMEMOTO 2476b0f06defSHajimu UMEMOTO tmp = *su; 2477b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6) 2478b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0; 2479b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 24804dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 24814dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 248204683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0; 24834dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 24844dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 2485b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port)); 24864dd8b5abSYoshinobu Inoue } 24874dd8b5abSYoshinobu Inoue } 24884dd8b5abSYoshinobu Inoue } 2489ea022d16SRodney W. Grimes #undef UC 2490ea022d16SRodney W. Grimes } else 2491ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 249202c97492SYaroslav Tykhiy reply(211, "End of status."); 2493ea022d16SRodney W. Grimes } 2494ea022d16SRodney W. Grimes 2495ea022d16SRodney W. Grimes void 2496e4bc453cSWarner Losh fatalerror(char *s) 2497ea022d16SRodney W. Grimes { 2498ea022d16SRodney W. Grimes 24994a3e5acdSYaroslav Tykhiy reply(451, "Error in server: %s", s); 2500ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2501ea022d16SRodney W. Grimes dologout(0); 2502ea022d16SRodney W. Grimes /* NOTREACHED */ 2503ea022d16SRodney W. Grimes } 2504ea022d16SRodney W. Grimes 2505ea022d16SRodney W. Grimes void 2506ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2507ea022d16SRodney W. Grimes { 2508ea022d16SRodney W. Grimes va_list ap; 2509e4bc453cSWarner Losh 2510ea022d16SRodney W. Grimes (void)printf("%d ", n); 25119cbb335cSTim J. Robbins va_start(ap, fmt); 2512ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 25139cbb335cSTim J. Robbins va_end(ap); 2514ea022d16SRodney W. Grimes (void)printf("\r\n"); 2515ea022d16SRodney W. Grimes (void)fflush(stdout); 2516618b0bbaSMark Murray if (ftpdebug) { 2517ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 25189cbb335cSTim J. Robbins va_start(ap, fmt); 2519ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 25209cbb335cSTim J. Robbins va_end(ap); 2521ea022d16SRodney W. Grimes } 2522ea022d16SRodney W. Grimes } 2523ea022d16SRodney W. Grimes 2524ea022d16SRodney W. Grimes void 2525ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2526ea022d16SRodney W. Grimes { 2527ea022d16SRodney W. Grimes va_list ap; 2528e4bc453cSWarner Losh 2529ea022d16SRodney W. Grimes (void)printf("%d- ", n); 25309cbb335cSTim J. Robbins va_start(ap, fmt); 2531ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 25329cbb335cSTim J. Robbins va_end(ap); 2533ea022d16SRodney W. Grimes (void)printf("\r\n"); 2534ea022d16SRodney W. Grimes (void)fflush(stdout); 2535618b0bbaSMark Murray if (ftpdebug) { 2536ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 25379cbb335cSTim J. Robbins va_start(ap, fmt); 2538ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 25399cbb335cSTim J. Robbins va_end(ap); 2540ea022d16SRodney W. Grimes } 2541ea022d16SRodney W. Grimes } 2542ea022d16SRodney W. Grimes 2543ea022d16SRodney W. Grimes static void 2544e4bc453cSWarner Losh ack(char *s) 2545ea022d16SRodney W. Grimes { 2546ea022d16SRodney W. Grimes 2547ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2548ea022d16SRodney W. Grimes } 2549ea022d16SRodney W. Grimes 2550ea022d16SRodney W. Grimes void 2551e4bc453cSWarner Losh nack(char *s) 2552ea022d16SRodney W. Grimes { 2553ea022d16SRodney W. Grimes 2554ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2555ea022d16SRodney W. Grimes } 2556ea022d16SRodney W. Grimes 2557ea022d16SRodney W. Grimes /* ARGSUSED */ 2558ea022d16SRodney W. Grimes void 2559e4bc453cSWarner Losh yyerror(char *s) 2560ea022d16SRodney W. Grimes { 2561ea022d16SRodney W. Grimes char *cp; 2562ea022d16SRodney W. Grimes 25639aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2564ea022d16SRodney W. Grimes *cp = '\0'; 256502c97492SYaroslav Tykhiy reply(500, "%s: command not understood.", cbuf); 2566ea022d16SRodney W. Grimes } 2567ea022d16SRodney W. Grimes 2568ea022d16SRodney W. Grimes void 2569e4bc453cSWarner Losh delete(char *name) 2570ea022d16SRodney W. Grimes { 2571ea022d16SRodney W. Grimes struct stat st; 2572ea022d16SRodney W. Grimes 2573ea022d16SRodney W. Grimes LOGCMD("delete", name); 25741b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2575ea022d16SRodney W. Grimes perror_reply(550, name); 2576ea022d16SRodney W. Grimes return; 2577ea022d16SRodney W. Grimes } 2578ac4f2391SYaroslav Tykhiy if (S_ISDIR(st.st_mode)) { 2579ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2580ea022d16SRodney W. Grimes perror_reply(550, name); 2581ea022d16SRodney W. Grimes return; 2582ea022d16SRodney W. Grimes } 2583ea022d16SRodney W. Grimes goto done; 2584ea022d16SRodney W. Grimes } 25853f8b9cfeSYaroslav Tykhiy if (guest && noguestmod) { 258602c97492SYaroslav Tykhiy reply(550, "Operation not permitted."); 25873f8b9cfeSYaroslav Tykhiy return; 25883f8b9cfeSYaroslav Tykhiy } 25893f8b9cfeSYaroslav Tykhiy if (unlink(name) < 0) { 2590ea022d16SRodney W. Grimes perror_reply(550, name); 2591ea022d16SRodney W. Grimes return; 2592ea022d16SRodney W. Grimes } 2593ea022d16SRodney W. Grimes done: 2594ea022d16SRodney W. Grimes ack("DELE"); 2595ea022d16SRodney W. Grimes } 2596ea022d16SRodney W. Grimes 2597ea022d16SRodney W. Grimes void 2598e4bc453cSWarner Losh cwd(char *path) 2599ea022d16SRodney W. Grimes { 2600ea022d16SRodney W. Grimes 2601ea022d16SRodney W. Grimes if (chdir(path) < 0) 2602ea022d16SRodney W. Grimes perror_reply(550, path); 2603ea022d16SRodney W. Grimes else 2604ea022d16SRodney W. Grimes ack("CWD"); 2605ea022d16SRodney W. Grimes } 2606ea022d16SRodney W. Grimes 2607ea022d16SRodney W. Grimes void 2608e4bc453cSWarner Losh makedir(char *name) 2609ea022d16SRodney W. Grimes { 26102b748987SYaroslav Tykhiy char *s; 2611ea022d16SRodney W. Grimes 2612ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2613d186bb12SMatthew N. Dodd if (guest && noguestmkd) 2614405e2987SYaroslav Tykhiy reply(550, "Operation not permitted."); 2615d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0) 2616ea022d16SRodney W. Grimes perror_reply(550, name); 26172b748987SYaroslav Tykhiy else { 26182b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL) 26192b748987SYaroslav Tykhiy fatalerror("Ran out of memory."); 26202b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s); 26212b748987SYaroslav Tykhiy free(s); 26222b748987SYaroslav Tykhiy } 2623ea022d16SRodney W. Grimes } 2624ea022d16SRodney W. Grimes 2625ea022d16SRodney W. Grimes void 2626e4bc453cSWarner Losh removedir(char *name) 2627ea022d16SRodney W. Grimes { 2628ea022d16SRodney W. Grimes 2629ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2630ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2631ea022d16SRodney W. Grimes perror_reply(550, name); 2632ea022d16SRodney W. Grimes else 2633ea022d16SRodney W. Grimes ack("RMD"); 2634ea022d16SRodney W. Grimes } 2635ea022d16SRodney W. Grimes 2636ea022d16SRodney W. Grimes void 2637e4bc453cSWarner Losh pwd(void) 2638ea022d16SRodney W. Grimes { 2639e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1]; 2640ea022d16SRodney W. Grimes 2641de9b6c03SYaroslav Tykhiy if (getcwd(path, sizeof(path)) == NULL) 264275933089SYaroslav Tykhiy perror_reply(550, "Get current directory"); 2643e4648f05SYaroslav Tykhiy else { 2644e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL) 2645e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory."); 2646e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s); 2647e4648f05SYaroslav Tykhiy free(s); 2648e4648f05SYaroslav Tykhiy } 2649ea022d16SRodney W. Grimes } 2650ea022d16SRodney W. Grimes 2651ea022d16SRodney W. Grimes char * 2652e4bc453cSWarner Losh renamefrom(char *name) 2653ea022d16SRodney W. Grimes { 2654ea022d16SRodney W. Grimes struct stat st; 2655ea022d16SRodney W. Grimes 2656b943b3c4SYaroslav Tykhiy if (guest && noguestmod) { 265702c97492SYaroslav Tykhiy reply(550, "Operation not permitted."); 2658b943b3c4SYaroslav Tykhiy return (NULL); 2659b943b3c4SYaroslav Tykhiy } 26601b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2661ea022d16SRodney W. Grimes perror_reply(550, name); 2662385f9bf0SYaroslav Tykhiy return (NULL); 2663ea022d16SRodney W. Grimes } 266402c97492SYaroslav Tykhiy reply(350, "File exists, ready for destination name."); 2665ea022d16SRodney W. Grimes return (name); 2666ea022d16SRodney W. Grimes } 2667ea022d16SRodney W. Grimes 2668ea022d16SRodney W. Grimes void 2669e4bc453cSWarner Losh renamecmd(char *from, char *to) 2670ea022d16SRodney W. Grimes { 267161f891a6SPaul Traina struct stat st; 2672ea022d16SRodney W. Grimes 2673ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 267461f891a6SPaul Traina 267561f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 267602c97492SYaroslav Tykhiy reply(550, "%s: permission denied.", to); 267761f891a6SPaul Traina return; 267861f891a6SPaul Traina } 267961f891a6SPaul Traina 2680ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2681ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2682ea022d16SRodney W. Grimes else 2683ea022d16SRodney W. Grimes ack("RNTO"); 2684ea022d16SRodney W. Grimes } 2685ea022d16SRodney W. Grimes 2686ea022d16SRodney W. Grimes static void 2687e4bc453cSWarner Losh dolog(struct sockaddr *who) 2688ea022d16SRodney W. Grimes { 26897cdd3cb7SYaroslav Tykhiy char who_name[NI_MAXHOST]; 26904dd8b5abSYoshinobu Inoue 26914dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 269204683b2cSYaroslav Tykhiy remotehost[sizeof(remotehost) - 1] = 0; 26937cdd3cb7SYaroslav Tykhiy if (getnameinfo(who, who->sa_len, 26947cdd3cb7SYaroslav Tykhiy who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST)) 26957cdd3cb7SYaroslav Tykhiy *who_name = 0; 26967cdd3cb7SYaroslav Tykhiy who_name[sizeof(who_name) - 1] = 0; 2697ea022d16SRodney W. Grimes 2698ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2699ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2700ea4e54b9SDavid Nugent if (thishost != firsthost) 2701ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2702ea4e54b9SDavid Nugent remotehost, hostname); 2703ea4e54b9SDavid Nugent else 2704ea4e54b9SDavid Nugent #endif 2705ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2706ea4e54b9SDavid Nugent remotehost); 2707b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2708ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2709ea022d16SRodney W. Grimes 2710ea4e54b9SDavid Nugent if (logging) { 2711ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2712ea4e54b9SDavid Nugent if (thishost != firsthost) 27137cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s) to %s", 27147cdd3cb7SYaroslav Tykhiy remotehost, who_name, hostname); 2715ea4e54b9SDavid Nugent else 2716ea4e54b9SDavid Nugent #endif 27177cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s)", 27187cdd3cb7SYaroslav Tykhiy remotehost, who_name); 2719ea022d16SRodney W. Grimes } 2720ea4e54b9SDavid Nugent } 2721ea022d16SRodney W. Grimes 2722ea022d16SRodney W. Grimes /* 2723ea022d16SRodney W. Grimes * Record logout in wtmp file 2724ea022d16SRodney W. Grimes * and exit with supplied status. 2725ea022d16SRodney W. Grimes */ 2726ea022d16SRodney W. Grimes void 2727e4bc453cSWarner Losh dologout(int status) 2728ea022d16SRodney W. Grimes { 2729ea022d16SRodney W. Grimes 27305d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) { 273152e7ee74SYaroslav Tykhiy (void) seteuid(0); 273246948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 2733ea022d16SRodney W. Grimes } 2734ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2735ea022d16SRodney W. Grimes _exit(status); 2736ea022d16SRodney W. Grimes } 2737ea022d16SRodney W. Grimes 2738ea022d16SRodney W. Grimes static void 2739e4bc453cSWarner Losh sigurg(int signo) 2740ea022d16SRodney W. Grimes { 27414b82fc95SYaroslav Tykhiy 27424b82fc95SYaroslav Tykhiy recvurg = 1; 27434b82fc95SYaroslav Tykhiy } 27444b82fc95SYaroslav Tykhiy 27454b82fc95SYaroslav Tykhiy static void 27464cd51076SYaroslav Tykhiy maskurg(int flag) 27474cd51076SYaroslav Tykhiy { 27484cd51076SYaroslav Tykhiy int oerrno; 27494cd51076SYaroslav Tykhiy sigset_t sset; 27504cd51076SYaroslav Tykhiy 27514cd51076SYaroslav Tykhiy if (!transflag) { 27524cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: maskurg() while no transfer"); 27534cd51076SYaroslav Tykhiy return; 27544cd51076SYaroslav Tykhiy } 27554cd51076SYaroslav Tykhiy oerrno = errno; 27564cd51076SYaroslav Tykhiy sigemptyset(&sset); 27574cd51076SYaroslav Tykhiy sigaddset(&sset, SIGURG); 27584cd51076SYaroslav Tykhiy sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL); 27594cd51076SYaroslav Tykhiy errno = oerrno; 27604cd51076SYaroslav Tykhiy } 27614cd51076SYaroslav Tykhiy 27624cd51076SYaroslav Tykhiy static void 27634cd51076SYaroslav Tykhiy flagxfer(int flag) 27644cd51076SYaroslav Tykhiy { 27654cd51076SYaroslav Tykhiy 27664cd51076SYaroslav Tykhiy maskurg(!flag); 27674cd51076SYaroslav Tykhiy if (flag) { 27684cd51076SYaroslav Tykhiy recvurg = 0; 27694cd51076SYaroslav Tykhiy transflag = 1; 27704cd51076SYaroslav Tykhiy } else 27714cd51076SYaroslav Tykhiy transflag = 0; 27724cd51076SYaroslav Tykhiy } 27734cd51076SYaroslav Tykhiy 27744cd51076SYaroslav Tykhiy /* 27754cd51076SYaroslav Tykhiy * Returns 0 if OK to resume or -1 if abort requested. 27764cd51076SYaroslav Tykhiy */ 27774cd51076SYaroslav Tykhiy static int 2778e4bc453cSWarner Losh myoob(void) 27794b82fc95SYaroslav Tykhiy { 2780ea022d16SRodney W. Grimes char *cp; 2781ea022d16SRodney W. Grimes 27824cd51076SYaroslav Tykhiy if (!transflag) { 27834cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: myoob() while no transfer"); 27844cd51076SYaroslav Tykhiy return (0); 27854cd51076SYaroslav Tykhiy } 2786ea022d16SRodney W. Grimes cp = tmpline; 2787ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2788ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2789ea022d16SRodney W. Grimes dologout(0); 2790ea022d16SRodney W. Grimes } 2791ea022d16SRodney W. Grimes upper(cp); 2792ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2793ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2794ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 279502c97492SYaroslav Tykhiy reply(226, "Abort successful."); 27964cd51076SYaroslav Tykhiy return (-1); 2797ea022d16SRodney W. Grimes } 2798ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 27999db4bbf3SMichael Haro tmpline[0] = '\0'; 28000e519c96SYaroslav Tykhiy if (file_size != -1) 280102c97492SYaroslav Tykhiy reply(213, "Status: %jd of %jd bytes transferred.", 2802a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count, (intmax_t)file_size); 2803ea022d16SRodney W. Grimes else 280402c97492SYaroslav Tykhiy reply(213, "Status: %jd bytes transferred.", 2805a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count); 2806ea022d16SRodney W. Grimes } 28074cd51076SYaroslav Tykhiy return (0); 2808ea022d16SRodney W. Grimes } 2809ea022d16SRodney W. Grimes 2810ea022d16SRodney W. Grimes /* 2811ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2812ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2813ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2814ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2815ea022d16SRodney W. Grimes */ 2816ea022d16SRodney W. Grimes void 2817e4bc453cSWarner Losh passive(void) 2818ea022d16SRodney W. Grimes { 28198af7c9a3SYaroslav Tykhiy int len, on; 2820ea022d16SRodney W. Grimes char *p, *a; 2821ea022d16SRodney W. Grimes 282261f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 282361f891a6SPaul Traina close(pdata); 282461f891a6SPaul Traina 28254dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2826ea022d16SRodney W. Grimes if (pdata < 0) { 2827ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2828ea022d16SRodney W. Grimes return; 2829ea022d16SRodney W. Grimes } 28308af7c9a3SYaroslav Tykhiy on = 1; 28318af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 28328af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 28334c450ad7SPaul Traina 283452e7ee74SYaroslav Tykhiy (void) seteuid(0); 283561f891a6SPaul Traina 2836dacc9752SPaul Traina #ifdef IP_PORTRANGE 28374dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 28388af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 2839dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2840dacc9752SPaul Traina 284140e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 284257d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 28434c450ad7SPaul Traina goto pasv_error; 28444c450ad7SPaul Traina } 2845dacc9752SPaul Traina #endif 28462db39860SNick Sayer #ifdef IPV6_PORTRANGE 28472db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 28488af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 28492db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 28502db39860SNick Sayer 28512db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 285257d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 28532db39860SNick Sayer goto pasv_error; 28542db39860SNick Sayer } 28552db39860SNick Sayer #endif 285640e9d39eSPeter Wemm 2857ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 28584dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 28594dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2860ea022d16SRodney W. Grimes goto pasv_error; 286161f891a6SPaul Traina 286252e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 28634c450ad7SPaul Traina 2864ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2865ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2866ea022d16SRodney W. Grimes goto pasv_error; 2867ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2868ea022d16SRodney W. Grimes goto pasv_error; 28694dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 28704dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 28714dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 28724dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 28734dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 28744dd8b5abSYoshinobu Inoue else 28754dd8b5abSYoshinobu Inoue goto pasv_error; 28764dd8b5abSYoshinobu Inoue 28774dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2878ea022d16SRodney W. Grimes 2879ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2880ea022d16SRodney W. Grimes 2881ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2882ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2883ea022d16SRodney W. Grimes return; 2884ea022d16SRodney W. Grimes 2885ea022d16SRodney W. Grimes pasv_error: 288652e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 2887ea022d16SRodney W. Grimes (void) close(pdata); 2888ea022d16SRodney W. Grimes pdata = -1; 2889ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2890ea022d16SRodney W. Grimes return; 2891ea022d16SRodney W. Grimes } 2892ea022d16SRodney W. Grimes 2893ea022d16SRodney W. Grimes /* 28944dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 28954dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 28964dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 28974dd8b5abSYoshinobu Inoue */ 28984dd8b5abSYoshinobu Inoue 28994dd8b5abSYoshinobu Inoue void 2900e4bc453cSWarner Losh long_passive(char *cmd, int pf) 29014dd8b5abSYoshinobu Inoue { 29028af7c9a3SYaroslav Tykhiy int len, on; 29034dd8b5abSYoshinobu Inoue char *p, *a; 29044dd8b5abSYoshinobu Inoue 29054dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 29064dd8b5abSYoshinobu Inoue close(pdata); 29074dd8b5abSYoshinobu Inoue 29084dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 29094dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 29104dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 29114dd8b5abSYoshinobu Inoue case AF_INET: 29124dd8b5abSYoshinobu Inoue pf = 1; 29134dd8b5abSYoshinobu Inoue break; 29144dd8b5abSYoshinobu Inoue case AF_INET6: 29154dd8b5abSYoshinobu Inoue pf = 2; 29164dd8b5abSYoshinobu Inoue break; 29174dd8b5abSYoshinobu Inoue default: 29184dd8b5abSYoshinobu Inoue pf = 0; 29194dd8b5abSYoshinobu Inoue break; 29204dd8b5abSYoshinobu Inoue } 29214dd8b5abSYoshinobu Inoue /* 29224dd8b5abSYoshinobu Inoue * XXX 29234dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 29244dd8b5abSYoshinobu Inoue */ 29254dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 29264dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 29274dd8b5abSYoshinobu Inoue "use (%d)", pf); 29284dd8b5abSYoshinobu Inoue } else 292902c97492SYaroslav Tykhiy reply(501, "Network protocol mismatch."); /*XXX*/ 29304dd8b5abSYoshinobu Inoue 29314dd8b5abSYoshinobu Inoue return; 29324dd8b5abSYoshinobu Inoue } 29334dd8b5abSYoshinobu Inoue } 29344dd8b5abSYoshinobu Inoue 29354dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 29364dd8b5abSYoshinobu Inoue if (pdata < 0) { 29374dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 29384dd8b5abSYoshinobu Inoue return; 29394dd8b5abSYoshinobu Inoue } 29408af7c9a3SYaroslav Tykhiy on = 1; 29418af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 29428af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 29434dd8b5abSYoshinobu Inoue 294452e7ee74SYaroslav Tykhiy (void) seteuid(0); 29454dd8b5abSYoshinobu Inoue 29464dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 29474dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 29484dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 29494dd8b5abSYoshinobu Inoue 29502db39860SNick Sayer #ifdef IP_PORTRANGE 29512db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 29528af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 29532db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 29542db39860SNick Sayer 29552db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 295657d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 29572db39860SNick Sayer goto pasv_error; 29582db39860SNick Sayer } 29592db39860SNick Sayer #endif 29602db39860SNick Sayer #ifdef IPV6_PORTRANGE 29612db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 29628af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 29632db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 29642db39860SNick Sayer 29652db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 296657d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 29672db39860SNick Sayer goto pasv_error; 29682db39860SNick Sayer } 29692db39860SNick Sayer #endif 29702db39860SNick Sayer 29714dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 29724dd8b5abSYoshinobu Inoue goto pasv_error; 29734dd8b5abSYoshinobu Inoue 297452e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 29754dd8b5abSYoshinobu Inoue 29764dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 29774dd8b5abSYoshinobu Inoue goto pasv_error; 29784dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 29794dd8b5abSYoshinobu Inoue goto pasv_error; 29804dd8b5abSYoshinobu Inoue 29814dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 29824dd8b5abSYoshinobu Inoue 29834dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 29844dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 29854dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 29864dd8b5abSYoshinobu Inoue case AF_INET: 29874dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 29884dd8b5abSYoshinobu Inoue v4_reply: 29894dd8b5abSYoshinobu Inoue reply(228, 29904dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 29914dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 29924dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 29934dd8b5abSYoshinobu Inoue return; 29944dd8b5abSYoshinobu Inoue case AF_INET6: 29954dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 29964dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 29974dd8b5abSYoshinobu Inoue goto v4_reply; 29984dd8b5abSYoshinobu Inoue } 29994dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 30004dd8b5abSYoshinobu Inoue reply(228, 30014dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 30024dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 30034dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 30044dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 30054dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 30064dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 30074dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 30084dd8b5abSYoshinobu Inoue return; 30094dd8b5abSYoshinobu Inoue } 30104dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 30114dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 30124dd8b5abSYoshinobu Inoue case AF_INET: 30134dd8b5abSYoshinobu Inoue case AF_INET6: 30144dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 30154dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 30164dd8b5abSYoshinobu Inoue return; 30174dd8b5abSYoshinobu Inoue } 30184dd8b5abSYoshinobu Inoue } else { 30194dd8b5abSYoshinobu Inoue /* more proper error code? */ 30204dd8b5abSYoshinobu Inoue } 30214dd8b5abSYoshinobu Inoue 30224dd8b5abSYoshinobu Inoue pasv_error: 302352e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 30244dd8b5abSYoshinobu Inoue (void) close(pdata); 30254dd8b5abSYoshinobu Inoue pdata = -1; 30264dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 30274dd8b5abSYoshinobu Inoue return; 30284dd8b5abSYoshinobu Inoue } 30294dd8b5abSYoshinobu Inoue 30304dd8b5abSYoshinobu Inoue /* 3031a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local" 3032a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races. 3033a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 3034a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name. 3035a117c345SYaroslav Tykhiy * 3036ea022d16SRodney W. Grimes * Generates failure reply on error. 3037ea022d16SRodney W. Grimes */ 3038a117c345SYaroslav Tykhiy static int 3039a117c345SYaroslav Tykhiy guniquefd(char *local, char **name) 3040ea022d16SRodney W. Grimes { 3041ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 3042ea022d16SRodney W. Grimes struct stat st; 3043ea022d16SRodney W. Grimes char *cp; 3044a117c345SYaroslav Tykhiy int count; 3045a117c345SYaroslav Tykhiy int fd; 3046ea022d16SRodney W. Grimes 3047ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 3048ea022d16SRodney W. Grimes if (cp) 3049ea022d16SRodney W. Grimes *cp = '\0'; 3050ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 3051ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 3052a117c345SYaroslav Tykhiy return (-1); 3053ea022d16SRodney W. Grimes } 3054a117c345SYaroslav Tykhiy if (cp) { 3055a117c345SYaroslav Tykhiy /* 3056a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix. 3057a117c345SYaroslav Tykhiy * -4 is for /nn\0 3058a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix. 3059a117c345SYaroslav Tykhiy */ 3060a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) { 306102c97492SYaroslav Tykhiy reply(553, "Pathname too long."); 3062a117c345SYaroslav Tykhiy return (-1); 3063a117c345SYaroslav Tykhiy } 3064ea022d16SRodney W. Grimes *cp = '/'; 3065a117c345SYaroslav Tykhiy } 3066e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 3067e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 3068ea022d16SRodney W. Grimes cp = new + strlen(new); 3069a117c345SYaroslav Tykhiy /* 3070a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly. 3071a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off 3072a117c345SYaroslav Tykhiy * by buffer size. 3073a117c345SYaroslav Tykhiy */ 3074a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/') 3075ea022d16SRodney W. Grimes *cp++ = '.'; 3076a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) { 3077a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */ 3078a117c345SYaroslav Tykhiy if (count) 3079ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 3080a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local, 3081a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 3082a117c345SYaroslav Tykhiy *name = count ? new : local; 3083a117c345SYaroslav Tykhiy return (fd); 3084a117c345SYaroslav Tykhiy } 308588b70721SYaroslav Tykhiy if (errno != EEXIST) { 308650618d61SYaroslav Tykhiy perror_reply(553, count ? new : local); 308788b70721SYaroslav Tykhiy return (-1); 308888b70721SYaroslav Tykhiy } 3089ea022d16SRodney W. Grimes } 3090ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 3091a117c345SYaroslav Tykhiy return (-1); 3092ea022d16SRodney W. Grimes } 3093ea022d16SRodney W. Grimes 3094ea022d16SRodney W. Grimes /* 3095ea022d16SRodney W. Grimes * Format and send reply containing system error number. 3096ea022d16SRodney W. Grimes */ 3097ea022d16SRodney W. Grimes void 3098e4bc453cSWarner Losh perror_reply(int code, char *string) 3099ea022d16SRodney W. Grimes { 3100ea022d16SRodney W. Grimes 3101ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 3102ea022d16SRodney W. Grimes } 3103ea022d16SRodney W. Grimes 3104ea022d16SRodney W. Grimes static char *onefile[] = { 3105ea022d16SRodney W. Grimes "", 3106ea022d16SRodney W. Grimes 0 3107ea022d16SRodney W. Grimes }; 3108ea022d16SRodney W. Grimes 3109ea022d16SRodney W. Grimes void 3110e4bc453cSWarner Losh send_file_list(char *whichf) 3111ea022d16SRodney W. Grimes { 3112ea022d16SRodney W. Grimes struct stat st; 3113ea022d16SRodney W. Grimes DIR *dirp = NULL; 3114ea022d16SRodney W. Grimes struct dirent *dir; 3115ea022d16SRodney W. Grimes FILE *dout = NULL; 3116ea022d16SRodney W. Grimes char **dirlist, *dirname; 3117ea022d16SRodney W. Grimes int simple = 0; 3118ea022d16SRodney W. Grimes int freeglob = 0; 3119ea022d16SRodney W. Grimes glob_t gl; 3120ea022d16SRodney W. Grimes 3121ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 312212da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 3123ea022d16SRodney W. Grimes 3124ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 31256d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 312675dc5f1aSMike Heffner flags |= GLOB_LIMIT; 3127ea022d16SRodney W. Grimes freeglob = 1; 3128ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 312902c97492SYaroslav Tykhiy reply(550, "No matching files found."); 3130ea022d16SRodney W. Grimes goto out; 3131ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 3132ea022d16SRodney W. Grimes errno = ENOENT; 3133ea022d16SRodney W. Grimes perror_reply(550, whichf); 3134ea022d16SRodney W. Grimes goto out; 3135ea022d16SRodney W. Grimes } 3136ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 3137ea022d16SRodney W. Grimes } else { 3138ea022d16SRodney W. Grimes onefile[0] = whichf; 3139ea022d16SRodney W. Grimes dirlist = onefile; 3140ea022d16SRodney W. Grimes simple = 1; 3141ea022d16SRodney W. Grimes } 3142ea022d16SRodney W. Grimes 31439aca17cbSMark Murray while ((dirname = *dirlist++)) { 3144ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 3145ea022d16SRodney W. Grimes /* 3146ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 3147ea022d16SRodney W. Grimes * used NLST, do what the user meant. 3148ea022d16SRodney W. Grimes */ 3149ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 31504cd51076SYaroslav Tykhiy dout == NULL) 3151af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 31524cd51076SYaroslav Tykhiy else 3153ea022d16SRodney W. Grimes perror_reply(550, whichf); 3154ea022d16SRodney W. Grimes goto out; 3155ea022d16SRodney W. Grimes } 3156ea022d16SRodney W. Grimes 3157ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 3158ea022d16SRodney W. Grimes if (dout == NULL) { 31590e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w"); 3160ea022d16SRodney W. Grimes if (dout == NULL) 3161ea022d16SRodney W. Grimes goto out; 31624cd51076SYaroslav Tykhiy STARTXFER; 3163ea022d16SRodney W. Grimes } 31644cd51076SYaroslav Tykhiy START_UNSAFE; 3165ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 3166ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 31674cd51076SYaroslav Tykhiy END_UNSAFE; 31684cd51076SYaroslav Tykhiy if (ferror(dout)) 31694cd51076SYaroslav Tykhiy goto data_err; 31704cd51076SYaroslav Tykhiy byte_count += strlen(dirname) + 31714cd51076SYaroslav Tykhiy (type == TYPE_A ? 2 : 1); 31724cd51076SYaroslav Tykhiy CHECKOOB(goto abrt); 3173ea022d16SRodney W. Grimes continue; 3174ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 3175ea022d16SRodney W. Grimes continue; 3176ea022d16SRodney W. Grimes 3177ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 3178ea022d16SRodney W. Grimes continue; 3179ea022d16SRodney W. Grimes 3180ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 3181ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 3182ea022d16SRodney W. Grimes 31834cd51076SYaroslav Tykhiy CHECKOOB(goto abrt); 31844b82fc95SYaroslav Tykhiy 3185ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 3186ea022d16SRodney W. Grimes continue; 3187ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 3188ea022d16SRodney W. Grimes dir->d_namlen == 2) 3189ea022d16SRodney W. Grimes continue; 3190ea022d16SRodney W. Grimes 3191e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 3192e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 3193ea022d16SRodney W. Grimes 3194ea022d16SRodney W. Grimes /* 3195ea022d16SRodney W. Grimes * We have to do a stat to insure it's 3196ea022d16SRodney W. Grimes * not a directory or special file. 3197ea022d16SRodney W. Grimes */ 3198ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 3199ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 3200ea022d16SRodney W. Grimes if (dout == NULL) { 32010e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w"); 3202ea022d16SRodney W. Grimes if (dout == NULL) 3203ea022d16SRodney W. Grimes goto out; 32044cd51076SYaroslav Tykhiy STARTXFER; 3205ea022d16SRodney W. Grimes } 32064cd51076SYaroslav Tykhiy START_UNSAFE; 3207ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 3208ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 3209ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3210ea022d16SRodney W. Grimes else 3211ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 3212ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 32134cd51076SYaroslav Tykhiy END_UNSAFE; 32144cd51076SYaroslav Tykhiy if (ferror(dout)) 32154cd51076SYaroslav Tykhiy goto data_err; 32164cd51076SYaroslav Tykhiy byte_count += strlen(nbuf) + 32174cd51076SYaroslav Tykhiy (type == TYPE_A ? 2 : 1); 32184cd51076SYaroslav Tykhiy CHECKOOB(goto abrt); 3219ea022d16SRodney W. Grimes } 3220ea022d16SRodney W. Grimes } 3221ea022d16SRodney W. Grimes (void) closedir(dirp); 32224cd51076SYaroslav Tykhiy dirp = NULL; 3223ea022d16SRodney W. Grimes } 3224ea022d16SRodney W. Grimes 3225ea022d16SRodney W. Grimes if (dout == NULL) 3226ea022d16SRodney W. Grimes reply(550, "No files found."); 32274cd51076SYaroslav Tykhiy else if (ferror(dout)) 32284cd51076SYaroslav Tykhiy data_err: perror_reply(550, "Data connection"); 3229ea022d16SRodney W. Grimes else 3230ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 32314cd51076SYaroslav Tykhiy out: 32324cd51076SYaroslav Tykhiy if (dout) { 32334cd51076SYaroslav Tykhiy ENDXFER; 32344cd51076SYaroslav Tykhiy abrt: 3235ea022d16SRodney W. Grimes (void) fclose(dout); 3236ea022d16SRodney W. Grimes data = -1; 3237ea022d16SRodney W. Grimes pdata = -1; 32384cd51076SYaroslav Tykhiy } 32394cd51076SYaroslav Tykhiy if (dirp) 32404cd51076SYaroslav Tykhiy (void) closedir(dirp); 3241ea022d16SRodney W. Grimes if (freeglob) { 3242ea022d16SRodney W. Grimes freeglob = 0; 3243ea022d16SRodney W. Grimes globfree(&gl); 3244ea022d16SRodney W. Grimes } 3245ea022d16SRodney W. Grimes } 3246ea022d16SRodney W. Grimes 3247cf09a206SDavid Greenman void 3248e4bc453cSWarner Losh reapchild(int signo) 3249cf09a206SDavid Greenman { 3250de9b6c03SYaroslav Tykhiy while (waitpid(-1, NULL, WNOHANG) > 0); 3251cf09a206SDavid Greenman } 3252cf09a206SDavid Greenman 3253b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 3254ea022d16SRodney W. Grimes /* 3255ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 3256ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 3257ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 3258ea022d16SRodney W. Grimes */ 3259ea022d16SRodney W. Grimes void 3260ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 3261ea022d16SRodney W. Grimes { 3262ea022d16SRodney W. Grimes int i; 3263ea022d16SRodney W. Grimes va_list ap; 3264ea022d16SRodney W. Grimes char *p, *bp, ch; 3265ea022d16SRodney W. Grimes char buf[LINE_MAX]; 3266ea022d16SRodney W. Grimes 3267ea022d16SRodney W. Grimes va_start(ap, fmt); 3268ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3269ea022d16SRodney W. Grimes 3270ea022d16SRodney W. Grimes /* make ps print our process name */ 3271ea022d16SRodney W. Grimes p = Argv[0]; 3272ea022d16SRodney W. Grimes *p++ = '-'; 3273ea022d16SRodney W. Grimes 3274ea022d16SRodney W. Grimes i = strlen(buf); 3275ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 3276ea022d16SRodney W. Grimes i = LastArgv - p - 2; 3277ea022d16SRodney W. Grimes buf[i] = '\0'; 3278ea022d16SRodney W. Grimes } 3279ea022d16SRodney W. Grimes bp = buf; 3280ea022d16SRodney W. Grimes while (ch = *bp++) 3281ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 3282ea022d16SRodney W. Grimes *p++ = ch; 3283ea022d16SRodney W. Grimes while (p < LastArgv) 3284ea022d16SRodney W. Grimes *p++ = ' '; 3285ea022d16SRodney W. Grimes } 3286b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 32873eb568f2SGuido van Rooij 328861f891a6SPaul Traina static void 32896b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...) 32906b2dee6bSYaroslav Tykhiy { 32916b2dee6bSYaroslav Tykhiy va_list ap; 32926b2dee6bSYaroslav Tykhiy char *ostr, *p; 32936b2dee6bSYaroslav Tykhiy 32946b2dee6bSYaroslav Tykhiy va_start(ap, fmt); 32956b2dee6bSYaroslav Tykhiy vasprintf(&p, fmt, ap); 32966b2dee6bSYaroslav Tykhiy va_end(ap); 32976b2dee6bSYaroslav Tykhiy if (p == NULL) 32986b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory."); 32996b2dee6bSYaroslav Tykhiy if (*strp == NULL) 33006b2dee6bSYaroslav Tykhiy *strp = p; 33016b2dee6bSYaroslav Tykhiy else { 33026b2dee6bSYaroslav Tykhiy ostr = *strp; 33036b2dee6bSYaroslav Tykhiy asprintf(strp, "%s%s", ostr, p); 33046b2dee6bSYaroslav Tykhiy if (*strp == NULL) 33056b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory."); 33066b2dee6bSYaroslav Tykhiy free(ostr); 33076b2dee6bSYaroslav Tykhiy } 33086b2dee6bSYaroslav Tykhiy } 33096b2dee6bSYaroslav Tykhiy 33106b2dee6bSYaroslav Tykhiy static void 33116b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt) 33126b2dee6bSYaroslav Tykhiy { 33136b2dee6bSYaroslav Tykhiy char *msg = NULL; 33146b2dee6bSYaroslav Tykhiy char wd[MAXPATHLEN + 1]; 33156b2dee6bSYaroslav Tykhiy 33166b2dee6bSYaroslav Tykhiy if (logging <= 1) 33176b2dee6bSYaroslav Tykhiy return; 3318e897216fSYaroslav Tykhiy 33196b2dee6bSYaroslav Tykhiy if (getcwd(wd, sizeof(wd) - 1) == NULL) 33206b2dee6bSYaroslav Tykhiy strcpy(wd, strerror(errno)); 33216b2dee6bSYaroslav Tykhiy 33226b2dee6bSYaroslav Tykhiy appendf(&msg, "%s", cmd); 33236b2dee6bSYaroslav Tykhiy if (file1) 33246b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file1); 33256b2dee6bSYaroslav Tykhiy if (file2) 33266b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file2); 33276b2dee6bSYaroslav Tykhiy if (cnt >= 0) 33286b2dee6bSYaroslav Tykhiy appendf(&msg, " = %jd bytes", (intmax_t)cnt); 3329e897216fSYaroslav Tykhiy appendf(&msg, " (wd: %s", wd); 3330215a9f9dSYaroslav Tykhiy if (guest || dochroot) 3331e897216fSYaroslav Tykhiy appendf(&msg, "; chrooted"); 3332e897216fSYaroslav Tykhiy appendf(&msg, ")"); 33336b2dee6bSYaroslav Tykhiy syslog(LOG_INFO, "%s", msg); 33346b2dee6bSYaroslav Tykhiy free(msg); 33356b2dee6bSYaroslav Tykhiy } 33366b2dee6bSYaroslav Tykhiy 33376b2dee6bSYaroslav Tykhiy static void 3338e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start) 33393eb568f2SGuido van Rooij { 33408c1c21f2SYaroslav Tykhiy char buf[MAXPATHLEN + 1024]; 33413eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 3342158a00b2SJohn Birrell time_t now; 33433eb568f2SGuido van Rooij 33448c1c21f2SYaroslav Tykhiy if (statfd >= 0) { 33453eb568f2SGuido van Rooij time(&now); 33468c1c21f2SYaroslav Tykhiy if (realpath(name, path) == NULL) { 33478c1c21f2SYaroslav Tykhiy syslog(LOG_NOTICE, "realpath failed on %s: %m", path); 33488c1c21f2SYaroslav Tykhiy return; 33498c1c21f2SYaroslav Tykhiy } 33508c1c21f2SYaroslav Tykhiy snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n", 33513eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 33528c1c21f2SYaroslav Tykhiy path, (intmax_t)size, 3353e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 33543eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 33553eb568f2SGuido van Rooij } 33563eb568f2SGuido van Rooij } 3357e4648f05SYaroslav Tykhiy 3358e4648f05SYaroslav Tykhiy static char * 3359e4648f05SYaroslav Tykhiy doublequote(char *s) 3360e4648f05SYaroslav Tykhiy { 3361e4648f05SYaroslav Tykhiy int n; 3362e4648f05SYaroslav Tykhiy char *p, *s2; 3363e4648f05SYaroslav Tykhiy 3364e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++) 3365e4648f05SYaroslav Tykhiy if (*p == '"') 3366e4648f05SYaroslav Tykhiy n++; 3367e4648f05SYaroslav Tykhiy 3368e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL) 3369e4648f05SYaroslav Tykhiy return (NULL); 3370e4648f05SYaroslav Tykhiy 3371e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) { 3372e4648f05SYaroslav Tykhiy if ((*p = *s) == '"') 3373e4648f05SYaroslav Tykhiy *(++p) = '"'; 3374e4648f05SYaroslav Tykhiy } 3375e4648f05SYaroslav Tykhiy *p = '\0'; 3376e4648f05SYaroslav Tykhiy 3377e4648f05SYaroslav Tykhiy return (s2); 3378e4648f05SYaroslav Tykhiy } 3379206fe568SHajimu UMEMOTO 3380206fe568SHajimu UMEMOTO /* setup server socket for specified address family */ 3381206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */ 3382206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */ 3383206fe568SHajimu UMEMOTO static int * 3384206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport) 3385206fe568SHajimu UMEMOTO { 3386206fe568SHajimu UMEMOTO struct addrinfo hints, *res, *r; 3387206fe568SHajimu UMEMOTO int error, maxs, *s, *socks; 3388206fe568SHajimu UMEMOTO const int on = 1; 3389206fe568SHajimu UMEMOTO 3390206fe568SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 3391206fe568SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE; 3392206fe568SHajimu UMEMOTO hints.ai_family = af; 3393206fe568SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM; 3394206fe568SHajimu UMEMOTO error = getaddrinfo(bindname, bindport, &hints, &res); 3395206fe568SHajimu UMEMOTO if (error) { 3396206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", gai_strerror(error)); 3397206fe568SHajimu UMEMOTO if (error == EAI_SYSTEM) 3398206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", strerror(errno)); 3399206fe568SHajimu UMEMOTO return NULL; 3400206fe568SHajimu UMEMOTO } 3401206fe568SHajimu UMEMOTO 3402206fe568SHajimu UMEMOTO /* Count max number of sockets we may open */ 3403206fe568SHajimu UMEMOTO for (maxs = 0, r = res; r; r = r->ai_next, maxs++) 3404206fe568SHajimu UMEMOTO ; 3405206fe568SHajimu UMEMOTO socks = malloc((maxs + 1) * sizeof(int)); 3406206fe568SHajimu UMEMOTO if (!socks) { 3407206fe568SHajimu UMEMOTO freeaddrinfo(res); 3408206fe568SHajimu UMEMOTO syslog(LOG_ERR, "couldn't allocate memory for sockets"); 3409206fe568SHajimu UMEMOTO return NULL; 3410206fe568SHajimu UMEMOTO } 3411206fe568SHajimu UMEMOTO 3412206fe568SHajimu UMEMOTO *socks = 0; /* num of sockets counter at start of array */ 3413206fe568SHajimu UMEMOTO s = socks + 1; 3414206fe568SHajimu UMEMOTO for (r = res; r; r = r->ai_next) { 3415206fe568SHajimu UMEMOTO *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 3416206fe568SHajimu UMEMOTO if (*s < 0) { 3417206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control socket: %m"); 3418206fe568SHajimu UMEMOTO continue; 3419206fe568SHajimu UMEMOTO } 3420206fe568SHajimu UMEMOTO if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, 3421206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0) 3422206fe568SHajimu UMEMOTO syslog(LOG_WARNING, 3423206fe568SHajimu UMEMOTO "control setsockopt (SO_REUSEADDR): %m"); 3424206fe568SHajimu UMEMOTO if (r->ai_family == AF_INET6) { 3425206fe568SHajimu UMEMOTO if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, 3426206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0) 3427206fe568SHajimu UMEMOTO syslog(LOG_WARNING, 3428206fe568SHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m"); 3429206fe568SHajimu UMEMOTO } 3430206fe568SHajimu UMEMOTO if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 3431206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control bind: %m"); 3432206fe568SHajimu UMEMOTO close(*s); 3433206fe568SHajimu UMEMOTO continue; 3434206fe568SHajimu UMEMOTO } 3435206fe568SHajimu UMEMOTO (*socks)++; 3436206fe568SHajimu UMEMOTO s++; 3437206fe568SHajimu UMEMOTO } 3438206fe568SHajimu UMEMOTO 3439206fe568SHajimu UMEMOTO if (res) 3440206fe568SHajimu UMEMOTO freeaddrinfo(res); 3441206fe568SHajimu UMEMOTO 3442206fe568SHajimu UMEMOTO if (*socks == 0) { 3443206fe568SHajimu UMEMOTO syslog(LOG_ERR, "control socket: Couldn't bind to any socket"); 3444206fe568SHajimu UMEMOTO free(socks); 3445206fe568SHajimu UMEMOTO return NULL; 3446206fe568SHajimu UMEMOTO } 3447206fe568SHajimu UMEMOTO return(socks); 3448206fe568SHajimu UMEMOTO } 3449