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 1504b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg; 151ea022d16SRodney W. Grimes sig_atomic_t transflag; 152ea022d16SRodney W. Grimes off_t file_size; 153ea022d16SRodney W. Grimes off_t byte_count; 154ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 155ea022d16SRodney W. Grimes #undef CMASK 156ea022d16SRodney W. Grimes #define CMASK 027 157ea022d16SRodney W. Grimes #endif 158ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 159ea022d16SRodney W. Grimes char tmpline[7]; 160ea4e54b9SDavid Nugent char *hostname; 161ad442344SDima Dorfman int epsvall = 0; 162ad442344SDima Dorfman 1630512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 164ea4e54b9SDavid Nugent char *ftpuser; 165ea4e54b9SDavid Nugent 166ea4e54b9SDavid Nugent static struct ftphost { 167ea4e54b9SDavid Nugent struct ftphost *next; 168f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 169ea4e54b9SDavid Nugent char *hostname; 170ea4e54b9SDavid Nugent char *anonuser; 171ea4e54b9SDavid Nugent char *statfile; 172ea4e54b9SDavid Nugent char *welcome; 173ea4e54b9SDavid Nugent char *loginmsg; 174ea4e54b9SDavid Nugent } *thishost, *firsthost; 175ea4e54b9SDavid Nugent 176ea4e54b9SDavid Nugent #endif 17704683b2cSYaroslav Tykhiy char remotehost[NI_MAXHOST]; 1783eb568f2SGuido van Rooij char *ident = NULL; 179a5a4544eSPaul Traina 180a5a4544eSPaul Traina static char ttyline[20]; 181a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 182a5a4544eSPaul Traina 1835bc9d93dSMark Murray #ifdef USE_PAM 184e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*); 1855bc9d93dSMark Murray pam_handle_t *pamh = NULL; 18647499ecdSAndrey A. Chernov #endif 187fa1746c9SMark Murray 188fa1746c9SMark Murray static struct opie opiedata; 189fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 19047499ecdSAndrey A. Chernov static int pwok; 1919aca17cbSMark Murray 192105a3c98SJulian Elischer char *pid_file = NULL; 193105a3c98SJulian Elischer 194ea022d16SRodney W. Grimes /* 1956d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 1966d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 1976d10cb2fSJonathan Lemon */ 1986d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 1996d10cb2fSJonathan Lemon # 2006d10cb2fSJonathan Lemon 2016d10cb2fSJonathan Lemon /* 202ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 203ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 204ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 205ea022d16SRodney W. Grimes */ 206ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 207ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 208ea022d16SRodney W. Grimes 209ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 210ea022d16SRodney W. Grimes int swaitint = SWAITINT; 211ea022d16SRodney W. Grimes 212ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 213b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 214ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 215ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 216b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 217ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 218ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 219ea022d16SRodney W. Grimes 2206b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file) logcmd((cmd), (file), NULL, -1) 2216b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2) logcmd((cmd), (file1), (file2), -1) 2226b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt) logcmd((cmd), (file), NULL, (cnt)) 223ea022d16SRodney W. Grimes 224ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 225e4bc453cSWarner Losh static void inithosts(void); 226e4bc453cSWarner Losh static void selecthost(union sockunion *); 227ea4e54b9SDavid Nugent #endif 228e4bc453cSWarner Losh static void ack(char *); 229e4bc453cSWarner Losh static void sigurg(int); 230e4bc453cSWarner Losh static void myoob(void); 2318657b576SYaroslav Tykhiy static int checkuser(char *, char *, int, char **); 232e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *); 233e4bc453cSWarner Losh static void dolog(struct sockaddr *); 234e4bc453cSWarner Losh static void end_login(void); 235e4bc453cSWarner Losh static FILE *getdatasock(char *); 236a117c345SYaroslav Tykhiy static int guniquefd(char *, char **); 237e4bc453cSWarner Losh static void lostconn(int); 238e4bc453cSWarner Losh static void sigquit(int); 239e4bc453cSWarner Losh static int receive_data(FILE *, FILE *); 2406e4b0a55SYaroslav Tykhiy static int send_data(FILE *, FILE *, size_t, off_t, int); 241ea022d16SRodney W. Grimes static struct passwd * 242e4bc453cSWarner Losh sgetpwnam(char *); 243e4bc453cSWarner Losh static char *sgetsave(char *); 244e4bc453cSWarner Losh static void reapchild(int); 245eb5b2bb3SYaroslav Tykhiy static void appendf(char **, char *, ...) __printflike(2, 3); 2466b2dee6bSYaroslav Tykhiy static void logcmd(char *, char *, char *, off_t); 247e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t); 248e4648f05SYaroslav Tykhiy static char *doublequote(char *); 249206fe568SHajimu UMEMOTO static int *socksetup(int, char *, const char *); 250ea022d16SRodney W. Grimes 251ea022d16SRodney W. Grimes int 252e4bc453cSWarner Losh main(int argc, char *argv[], char **envp) 253ea022d16SRodney W. Grimes { 254ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 255ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 256ea022d16SRodney W. Grimes FILE *fd; 2574dd8b5abSYoshinobu Inoue char *bindname = NULL; 25863591ba5SYaroslav Tykhiy const char *bindport = "ftp"; 2594dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2604b82fc95SYaroslav Tykhiy struct sigaction sa; 261ea022d16SRodney W. Grimes 26234d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 2634b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask); 2644b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 26534d1ba5cSAndrey A. Chernov 266b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 267ea022d16SRodney W. Grimes /* 268ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 269ea022d16SRodney W. Grimes */ 270ea022d16SRodney W. Grimes Argv = argv; 271ea022d16SRodney W. Grimes while (*envp) 272ea022d16SRodney W. Grimes envp++; 273ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 274b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 275ea022d16SRodney W. Grimes 2766c98f401SYaroslav Tykhiy /* 2776c98f401SYaroslav Tykhiy * Prevent diagnostic messages from appearing on stderr. 2786c98f401SYaroslav Tykhiy * We run as a daemon or from inetd; in both cases, there's 2796c98f401SYaroslav Tykhiy * more reason in logging to syslog. 2806c98f401SYaroslav Tykhiy */ 2816c98f401SYaroslav Tykhiy (void) freopen(_PATH_DEVNULL, "w", stderr); 2826c98f401SYaroslav Tykhiy opterr = 0; 2836c98f401SYaroslav Tykhiy 2846c98f401SYaroslav Tykhiy /* 2856c98f401SYaroslav Tykhiy * LOG_NDELAY sets up the logging connection immediately, 2866c98f401SYaroslav Tykhiy * necessary for anonymous ftp's that chroot and can't do it later. 2876c98f401SYaroslav Tykhiy */ 2886c98f401SYaroslav Tykhiy openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 2893eb568f2SGuido van Rooij 290c152df28SYaroslav Tykhiy while ((ch = getopt(argc, argv, 291c152df28SYaroslav Tykhiy "46a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) { 292ea022d16SRodney W. Grimes switch (ch) { 2930e063efeSYaroslav Tykhiy case '4': 294206fe568SHajimu UMEMOTO family = (family == AF_INET6) ? AF_UNSPEC : AF_INET; 2950e063efeSYaroslav Tykhiy break; 2960e063efeSYaroslav Tykhiy 2970e063efeSYaroslav Tykhiy case '6': 298206fe568SHajimu UMEMOTO family = (family == AF_INET) ? AF_UNSPEC : AF_INET6; 2990e063efeSYaroslav Tykhiy break; 3000e063efeSYaroslav Tykhiy 3010e063efeSYaroslav Tykhiy case 'a': 3020e063efeSYaroslav Tykhiy bindname = optarg; 3030e063efeSYaroslav Tykhiy break; 3040e063efeSYaroslav Tykhiy 3050e063efeSYaroslav Tykhiy case 'A': 3060e063efeSYaroslav Tykhiy anon_only = 1; 307cf09a206SDavid Greenman break; 308cf09a206SDavid Greenman 309ea022d16SRodney W. Grimes case 'd': 310618b0bbaSMark Murray ftpdebug++; 311ea022d16SRodney W. Grimes break; 312ea022d16SRodney W. Grimes 3130e063efeSYaroslav Tykhiy case 'D': 3140e063efeSYaroslav Tykhiy daemon_mode++; 3150e063efeSYaroslav Tykhiy break; 3160e063efeSYaroslav Tykhiy 317a4b77a2aSPoul-Henning Kamp case 'E': 318a4b77a2aSPoul-Henning Kamp noepsv = 1; 319a4b77a2aSPoul-Henning Kamp break; 320a4b77a2aSPoul-Henning Kamp 321c152df28SYaroslav Tykhiy case 'h': 322c152df28SYaroslav Tykhiy hostinfo = 0; 323c152df28SYaroslav Tykhiy break; 324c152df28SYaroslav Tykhiy 325ea022d16SRodney W. Grimes case 'l': 326ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 327ea022d16SRodney W. Grimes break; 328ea022d16SRodney W. Grimes 329a117c345SYaroslav Tykhiy case 'm': 330a117c345SYaroslav Tykhiy noguestmod = 0; 331a117c345SYaroslav Tykhiy break; 332a117c345SYaroslav Tykhiy 3330e063efeSYaroslav Tykhiy case 'M': 3340e063efeSYaroslav Tykhiy noguestmkd = 1; 3350e063efeSYaroslav Tykhiy break; 3360e063efeSYaroslav Tykhiy 3370e063efeSYaroslav Tykhiy case 'o': 3380e063efeSYaroslav Tykhiy noretr = 1; 3390e063efeSYaroslav Tykhiy break; 3400e063efeSYaroslav Tykhiy 3410e063efeSYaroslav Tykhiy case 'O': 3420e063efeSYaroslav Tykhiy noguestretr = 1; 3430e063efeSYaroslav Tykhiy break; 3440e063efeSYaroslav Tykhiy 3450e063efeSYaroslav Tykhiy case 'p': 3460e063efeSYaroslav Tykhiy pid_file = optarg; 3470e063efeSYaroslav Tykhiy break; 3480e063efeSYaroslav Tykhiy 34963591ba5SYaroslav Tykhiy case 'P': 35063591ba5SYaroslav Tykhiy bindport = optarg; 35163591ba5SYaroslav Tykhiy break; 35263591ba5SYaroslav Tykhiy 353a4b77a2aSPoul-Henning Kamp case 'r': 354a4b77a2aSPoul-Henning Kamp readonly = 1; 355a4b77a2aSPoul-Henning Kamp break; 356a4b77a2aSPoul-Henning Kamp 357a5a4544eSPaul Traina case 'R': 358a5a4544eSPaul Traina paranoid = 0; 359a5a4544eSPaul Traina break; 360a5a4544eSPaul Traina 361a5a4544eSPaul Traina case 'S': 362a5a4544eSPaul Traina stats++; 363a5a4544eSPaul Traina break; 364a5a4544eSPaul Traina 365ea022d16SRodney W. Grimes case 't': 366ea022d16SRodney W. Grimes timeout = atoi(optarg); 367ea022d16SRodney W. Grimes if (maxtimeout < timeout) 368ea022d16SRodney W. Grimes maxtimeout = timeout; 369ea022d16SRodney W. Grimes break; 370a5a4544eSPaul Traina 3710e063efeSYaroslav Tykhiy case 'T': 3720e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg); 3730e063efeSYaroslav Tykhiy if (timeout > maxtimeout) 3740e063efeSYaroslav Tykhiy timeout = maxtimeout; 375105a3c98SJulian Elischer break; 376105a3c98SJulian Elischer 377ea022d16SRodney W. Grimes case 'u': 378ea022d16SRodney W. Grimes { 379ea022d16SRodney W. Grimes long val = 0; 380ea022d16SRodney W. Grimes 381ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 382ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 3836c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "bad value for -u"); 384ea022d16SRodney W. Grimes else 385ea022d16SRodney W. Grimes defumask = val; 386ea022d16SRodney W. Grimes break; 387ea022d16SRodney W. Grimes } 3880e063efeSYaroslav Tykhiy case 'U': 3890e063efeSYaroslav Tykhiy restricted_data_ports = 0; 3905a392aecSTorsten Blum break; 391ea022d16SRodney W. Grimes 392ea022d16SRodney W. Grimes case 'v': 39393bd9dc5SYaroslav Tykhiy ftpdebug++; 394ea022d16SRodney W. Grimes break; 395ea022d16SRodney W. Grimes 3965d7e0128SYaroslav Tykhiy case 'W': 3975d7e0128SYaroslav Tykhiy dowtmp = 0; 3985d7e0128SYaroslav Tykhiy break; 3995d7e0128SYaroslav Tykhiy 400ea022d16SRodney W. Grimes default: 4016c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "unknown flag -%c ignored", optopt); 402ea022d16SRodney W. Grimes break; 403ea022d16SRodney W. Grimes } 404ea022d16SRodney W. Grimes } 405cf09a206SDavid Greenman 406ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 407ea4e54b9SDavid Nugent inithosts(); 408ea4e54b9SDavid Nugent #endif 409cf09a206SDavid Greenman 410cf09a206SDavid Greenman if (daemon_mode) { 411206fe568SHajimu UMEMOTO int *ctl_sock, fd, maxfd = -1, nfds, i; 412206fe568SHajimu UMEMOTO fd_set defreadfds, readfds; 413206fe568SHajimu UMEMOTO pid_t pid; 414cf09a206SDavid Greenman 415cf09a206SDavid Greenman /* 416cf09a206SDavid Greenman * Detach from parent. 417cf09a206SDavid Greenman */ 418cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 419cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 420cf09a206SDavid Greenman exit(1); 421cf09a206SDavid Greenman } 4224b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild; 4234b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 4244dd8b5abSYoshinobu Inoue 425cf09a206SDavid Greenman /* 426cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 427cf09a206SDavid Greenman * listening. 428cf09a206SDavid Greenman */ 429206fe568SHajimu UMEMOTO ctl_sock = socksetup(family, bindname, bindport); 430206fe568SHajimu UMEMOTO if (ctl_sock == NULL) 431cf09a206SDavid Greenman exit(1); 432206fe568SHajimu UMEMOTO 433206fe568SHajimu UMEMOTO FD_ZERO(&defreadfds); 434206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++) { 435206fe568SHajimu UMEMOTO FD_SET(ctl_sock[i], &defreadfds); 436206fe568SHajimu UMEMOTO if (listen(ctl_sock[i], 32) < 0) { 437cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 438cf09a206SDavid Greenman exit(1); 439cf09a206SDavid Greenman } 440206fe568SHajimu UMEMOTO if (maxfd < ctl_sock[i]) 441206fe568SHajimu UMEMOTO maxfd = ctl_sock[i]; 442206fe568SHajimu UMEMOTO } 443206fe568SHajimu UMEMOTO 444cf09a206SDavid Greenman /* 445105a3c98SJulian Elischer * Atomically write process ID 446105a3c98SJulian Elischer */ 447105a3c98SJulian Elischer if (pid_file) 448105a3c98SJulian Elischer { 449105a3c98SJulian Elischer int fd; 450105a3c98SJulian Elischer char buf[20]; 451105a3c98SJulian Elischer 452105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 453105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 45485966371SWarner Losh if (fd < 0) { 455105a3c98SJulian Elischer if (errno == EAGAIN) 456105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 457105a3c98SJulian Elischer else 458105a3c98SJulian Elischer err(1, "%s", pid_file); 45985966371SWarner Losh } 460105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 461105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 462105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 463105a3c98SJulian Elischer err(1, "%s: write", pid_file); 464105a3c98SJulian Elischer /* Leave the pid file open and locked */ 465105a3c98SJulian Elischer } 466105a3c98SJulian Elischer /* 467cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 468cf09a206SDavid Greenman * children to handle them. 469cf09a206SDavid Greenman */ 470cf09a206SDavid Greenman while (1) { 471206fe568SHajimu UMEMOTO FD_COPY(&defreadfds, &readfds); 472206fe568SHajimu UMEMOTO nfds = select(maxfd + 1, &readfds, NULL, NULL, 0); 473206fe568SHajimu UMEMOTO if (nfds <= 0) { 474206fe568SHajimu UMEMOTO if (nfds < 0 && errno != EINTR) 475206fe568SHajimu UMEMOTO syslog(LOG_WARNING, "select: %m"); 476206fe568SHajimu UMEMOTO continue; 477206fe568SHajimu UMEMOTO } 478206fe568SHajimu UMEMOTO 479206fe568SHajimu UMEMOTO pid = -1; 480206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++) 481206fe568SHajimu UMEMOTO if (FD_ISSET(ctl_sock[i], &readfds)) { 482206fe568SHajimu UMEMOTO addrlen = sizeof(his_addr); 483206fe568SHajimu UMEMOTO fd = accept(ctl_sock[i], 484206fe568SHajimu UMEMOTO (struct sockaddr *)&his_addr, 485206fe568SHajimu UMEMOTO &addrlen); 48640e67765SMaxim Konovalov if (fd >= 0) { 487206fe568SHajimu UMEMOTO if ((pid = fork()) == 0) { 488cf09a206SDavid Greenman /* child */ 489cf09a206SDavid Greenman (void) dup2(fd, 0); 490cf09a206SDavid Greenman (void) dup2(fd, 1); 491206fe568SHajimu UMEMOTO close(ctl_sock[i]); 492206fe568SHajimu UMEMOTO } else 493cf09a206SDavid Greenman close(fd); 494cf09a206SDavid Greenman } 49540e67765SMaxim Konovalov } 496206fe568SHajimu UMEMOTO if (pid == 0) 497206fe568SHajimu UMEMOTO break; 498206fe568SHajimu UMEMOTO } 499cf09a206SDavid Greenman } else { 500cf09a206SDavid Greenman addrlen = sizeof(his_addr); 501cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 502cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 503cf09a206SDavid Greenman exit(1); 504cf09a206SDavid Greenman } 505cf09a206SDavid Greenman } 506cf09a206SDavid Greenman 5074b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL; 5084b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 5094b82fc95SYaroslav Tykhiy 5104b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg; 5114b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 5124b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL); 5134b82fc95SYaroslav Tykhiy 5144b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */ 5154b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 5164b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit; 5174b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL); 5184b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL); 5194b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL); 5204b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL); 5214b82fc95SYaroslav Tykhiy 5224b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn; 5234b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL); 524ea022d16SRodney W. Grimes 525cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 526cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 527cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 528cf09a206SDavid Greenman exit(1); 529cf09a206SDavid Greenman } 53063591ba5SYaroslav Tykhiy dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */ 531ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 532ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5334dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 534ea4e54b9SDavid Nugent #endif 535cf09a206SDavid Greenman #ifdef IP_TOS 5364dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5374dd8b5abSYoshinobu Inoue { 538cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 53957d4ef07SYaroslav Tykhiy if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 540406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 5414dd8b5abSYoshinobu Inoue } 542cf09a206SDavid Greenman #endif 543dadb9fb3SDavid Greenman /* 544dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 545dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 546dadb9fb3SDavid Greenman */ 547dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 548406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 549dadb9fb3SDavid Greenman 5504dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 551cf09a206SDavid Greenman 552a5a4544eSPaul Traina /* set this here so klogin can use it... */ 553e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 554a5a4544eSPaul Traina 555ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 556ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 55757d4ef07SYaroslav Tykhiy if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 558406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 559ea022d16SRodney W. Grimes #endif 560ea022d16SRodney W. Grimes 561ea022d16SRodney W. Grimes #ifdef F_SETOWN 562ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 563ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 564ea022d16SRodney W. Grimes #endif 5654dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 566ea022d16SRodney W. Grimes /* 567ea022d16SRodney W. Grimes * Set up default state 568ea022d16SRodney W. Grimes */ 569ea022d16SRodney W. Grimes data = -1; 570ea022d16SRodney W. Grimes type = TYPE_A; 571ea022d16SRodney W. Grimes form = FORM_N; 572ea022d16SRodney W. Grimes stru = STRU_F; 573ea022d16SRodney W. Grimes mode = MODE_S; 574ea022d16SRodney W. Grimes tmpline[0] = '\0'; 575ea022d16SRodney W. Grimes 576ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 577ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 578ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 579ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 580ea022d16SRodney W. Grimes *cp = '\0'; 581ea022d16SRodney W. Grimes lreply(530, "%s", line); 582ea022d16SRodney W. Grimes } 583ea022d16SRodney W. Grimes (void) fflush(stdout); 584ea022d16SRodney W. Grimes (void) fclose(fd); 585ea022d16SRodney W. Grimes reply(530, "System not available."); 586ea022d16SRodney W. Grimes exit(0); 587ea022d16SRodney W. Grimes } 588ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 58963047c6fSDavid E. O'Brien fd = fopen(thishost->welcome, "r"); 590ea4e54b9SDavid Nugent #else 59163047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPWELCOME, "r"); 592ea4e54b9SDavid Nugent #endif 59363047c6fSDavid E. O'Brien if (fd != NULL) { 594ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 595ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 596ea022d16SRodney W. Grimes *cp = '\0'; 597ea022d16SRodney W. Grimes lreply(220, "%s", line); 598ea022d16SRodney W. Grimes } 599ea022d16SRodney W. Grimes (void) fflush(stdout); 600ea022d16SRodney W. Grimes (void) fclose(fd); 601ea022d16SRodney W. Grimes /* reply(220,) must follow */ 602ea022d16SRodney W. Grimes } 603ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6040512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 605618b0bbaSMark Murray fatalerror("Ran out of memory."); 60604683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) 60704683b2cSYaroslav Tykhiy hostname[0] = '\0'; 6089e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 609ea4e54b9SDavid Nugent #endif 610c152df28SYaroslav Tykhiy if (hostinfo) 611ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 612c152df28SYaroslav Tykhiy else 613c152df28SYaroslav Tykhiy reply(220, "FTP server ready."); 614ea022d16SRodney W. Grimes for (;;) 615ea022d16SRodney W. Grimes (void) yyparse(); 616ea022d16SRodney W. Grimes /* NOTREACHED */ 617ea022d16SRodney W. Grimes } 618ea022d16SRodney W. Grimes 619ea022d16SRodney W. Grimes static void 620e4bc453cSWarner Losh lostconn(int signo) 621ea022d16SRodney W. Grimes { 622ea022d16SRodney W. Grimes 623618b0bbaSMark Murray if (ftpdebug) 624ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 625e02897faSPhilippe Charnier dologout(1); 626ea022d16SRodney W. Grimes } 627ea022d16SRodney W. Grimes 6284b82fc95SYaroslav Tykhiy static void 629e4bc453cSWarner Losh sigquit(int signo) 6304b82fc95SYaroslav Tykhiy { 6314b82fc95SYaroslav Tykhiy 6324b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo); 6334b82fc95SYaroslav Tykhiy dologout(1); 6344b82fc95SYaroslav Tykhiy } 6354b82fc95SYaroslav Tykhiy 636ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 637ea4e54b9SDavid Nugent /* 638ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 639ea4e54b9SDavid Nugent */ 640ea4e54b9SDavid Nugent 641ea4e54b9SDavid Nugent static void 642e4bc453cSWarner Losh inithosts(void) 643ea4e54b9SDavid Nugent { 64455b54aa7SYaroslav Tykhiy int insert; 645737d08f3SYaroslav Tykhiy size_t len; 646ea4e54b9SDavid Nugent FILE *fp; 647737d08f3SYaroslav Tykhiy char *cp, *mp, *line; 648737d08f3SYaroslav Tykhiy char *hostname; 64955b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 650ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 6514dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 652ea4e54b9SDavid Nugent 653ea4e54b9SDavid Nugent /* 654ea4e54b9SDavid Nugent * Fill in the default host information 655ea4e54b9SDavid Nugent */ 656737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 657618b0bbaSMark Murray fatalerror("Ran out of memory."); 65804683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) 659737d08f3SYaroslav Tykhiy hostname[0] = '\0'; 660737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0'; 661737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 662737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 663737d08f3SYaroslav Tykhiy hrp->hostname = hostname; 664f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 6654dd8b5abSYoshinobu Inoue 6664dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 6674dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 6684dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 669b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 670f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 671ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 672ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 673ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 674ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 675ea4e54b9SDavid Nugent hrp->next = NULL; 676ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 677ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 678ec009cf0SYaroslav Tykhiy int addrsize, gothost; 6794dd8b5abSYoshinobu Inoue void *addr; 6804dd8b5abSYoshinobu Inoue struct hostent *hp; 6814dd8b5abSYoshinobu Inoue 682737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) { 6834dd8b5abSYoshinobu Inoue int i, hp_error; 684ea4e54b9SDavid Nugent 685737d08f3SYaroslav Tykhiy /* skip comments */ 686737d08f3SYaroslav Tykhiy if (line[0] == '#') 687ea4e54b9SDavid Nugent continue; 688737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 689737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 690737d08f3SYaroslav Tykhiy mp = NULL; 691737d08f3SYaroslav Tykhiy } else { 692737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 693737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 694737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 695737d08f3SYaroslav Tykhiy mp[len] = '\0'; 696737d08f3SYaroslav Tykhiy line = mp; 697ea4e54b9SDavid Nugent } 698ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 699737d08f3SYaroslav Tykhiy /* skip empty lines */ 700737d08f3SYaroslav Tykhiy if (cp == NULL) 701737d08f3SYaroslav Tykhiy goto nextline; 70255b54aa7SYaroslav Tykhiy vhost = cp; 70355b54aa7SYaroslav Tykhiy 70455b54aa7SYaroslav Tykhiy /* set defaults */ 70555b54aa7SYaroslav Tykhiy anonuser = "ftp"; 70655b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE; 70755b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME; 70855b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG; 70955b54aa7SYaroslav Tykhiy 71055b54aa7SYaroslav Tykhiy /* 71155b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info 71255b54aa7SYaroslav Tykhiy * for all the addresses associated with 71355b54aa7SYaroslav Tykhiy * the virtual host name. 71455b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special: 71555b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed 71655b54aa7SYaroslav Tykhiy * later, after we know its canonical form. 71755b54aa7SYaroslav Tykhiy */ 71855b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 71955b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp))) 72055b54aa7SYaroslav Tykhiy switch (i) { 72155b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */ 72255b54aa7SYaroslav Tykhiy anonuser = cp; 72355b54aa7SYaroslav Tykhiy break; 72455b54aa7SYaroslav Tykhiy case 2: /* statistics file */ 72555b54aa7SYaroslav Tykhiy statfile = cp; 72655b54aa7SYaroslav Tykhiy break; 72755b54aa7SYaroslav Tykhiy case 3: /* welcome message */ 72855b54aa7SYaroslav Tykhiy welcome = cp; 72955b54aa7SYaroslav Tykhiy break; 73055b54aa7SYaroslav Tykhiy case 4: /* login message */ 73155b54aa7SYaroslav Tykhiy loginmsg = cp; 73255b54aa7SYaroslav Tykhiy break; 73355b54aa7SYaroslav Tykhiy default: /* programming error */ 73455b54aa7SYaroslav Tykhiy abort(); 73555b54aa7SYaroslav Tykhiy /* NOTREACHED */ 73655b54aa7SYaroslav Tykhiy } 7374dd8b5abSYoshinobu Inoue 7384dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 7394dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 7404dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 741b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 742737d08f3SYaroslav Tykhiy goto nextline; 7434dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 744b535a9bfSDavid Nugent ai = ai->ai_next) { 7454dd8b5abSYoshinobu Inoue 746b535a9bfSDavid Nugent gothost = 0; 747ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 748f38c6cadSYoshinobu Inoue struct addrinfo *hi; 749f38c6cadSYoshinobu Inoue 750f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 751f38c6cadSYoshinobu Inoue hi = hi->ai_next) 752f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 753f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7544dd8b5abSYoshinobu Inoue ai->ai_addr, 755b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 756b535a9bfSDavid Nugent gothost++; 757b535a9bfSDavid Nugent break; 758b535a9bfSDavid Nugent } 759b535a9bfSDavid Nugent if (gothost) 760ea4e54b9SDavid Nugent break; 761ea4e54b9SDavid Nugent } 762ea4e54b9SDavid Nugent if (hrp == NULL) { 763ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 764737d08f3SYaroslav Tykhiy goto nextline; 765b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 76655b54aa7SYaroslav Tykhiy insert = 1; 767f2fe752dSYaroslav Tykhiy } else { 7681f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res) 769b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 770f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */ 771f2fe752dSYaroslav Tykhiy } 772f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 773f38c6cadSYoshinobu Inoue 774ea4e54b9SDavid Nugent /* 775ea4e54b9SDavid Nugent * determine hostname to use. 7764dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 777ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 778ea4e54b9SDavid Nugent */ 7794dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 780f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 7814dd8b5abSYoshinobu Inoue case AF_INET: 7824b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 7834b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr); 7844dd8b5abSYoshinobu Inoue break; 7854dd8b5abSYoshinobu Inoue case AF_INET6: 7864b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 7874b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr); 7884dd8b5abSYoshinobu Inoue break; 7894dd8b5abSYoshinobu Inoue default: 7904dd8b5abSYoshinobu Inoue /* should not reach here */ 791f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 792f2fe752dSYaroslav Tykhiy if (insert) 793f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/ 794f2fe752dSYaroslav Tykhiy else 795f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/ 796737d08f3SYaroslav Tykhiy goto nextline; 7974dd8b5abSYoshinobu Inoue /* NOTREACHED */ 7984dd8b5abSYoshinobu Inoue } 79957d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize, 800f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 8014dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 80255b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) { 803ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 80455b54aa7SYaroslav Tykhiy vhost = hp->h_name; 805ea4e54b9SDavid Nugent else { 806ea4e54b9SDavid Nugent i = 0; 807ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 80855b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0) 809ea4e54b9SDavid Nugent ++i; 810ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 81155b54aa7SYaroslav Tykhiy vhost = hp->h_name; 812ea4e54b9SDavid Nugent } 813ea4e54b9SDavid Nugent } 814ea4e54b9SDavid Nugent } 815b1d8d5cdSYaroslav Tykhiy if (hrp->hostname && 816b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) { 817b1d8d5cdSYaroslav Tykhiy free(hrp->hostname); 818b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 819b1d8d5cdSYaroslav Tykhiy } 820b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL && 821f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) { 822f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 823f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */ 824f2fe752dSYaroslav Tykhiy if (hp) 825f2fe752dSYaroslav Tykhiy freehostent(hp); 82655b54aa7SYaroslav Tykhiy goto nextline; 827f2fe752dSYaroslav Tykhiy } 82855b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser; 82955b54aa7SYaroslav Tykhiy hrp->statfile = statfile; 83055b54aa7SYaroslav Tykhiy hrp->welcome = welcome; 83155b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg; 83255b54aa7SYaroslav Tykhiy if (insert) { 83355b54aa7SYaroslav Tykhiy hrp->next = NULL; 83455b54aa7SYaroslav Tykhiy lhrp->next = hrp; 83555b54aa7SYaroslav Tykhiy lhrp = hrp; 83655b54aa7SYaroslav Tykhiy } 837233c0f66SYaroslav Tykhiy if (hp) 8384dd8b5abSYoshinobu Inoue freehostent(hp); 8394dd8b5abSYoshinobu Inoue } 840737d08f3SYaroslav Tykhiy nextline: 841737d08f3SYaroslav Tykhiy if (mp) 842737d08f3SYaroslav Tykhiy free(mp); 843ea4e54b9SDavid Nugent } 844ea4e54b9SDavid Nugent (void) fclose(fp); 845ea4e54b9SDavid Nugent } 846ea4e54b9SDavid Nugent } 847ea4e54b9SDavid Nugent 848ea4e54b9SDavid Nugent static void 849e4bc453cSWarner Losh selecthost(union sockunion *su) 850ea4e54b9SDavid Nugent { 851ea4e54b9SDavid Nugent struct ftphost *hrp; 8524dd8b5abSYoshinobu Inoue u_int16_t port; 8534dd8b5abSYoshinobu Inoue #ifdef INET6 8544dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 8554dd8b5abSYoshinobu Inoue #endif 856f38c6cadSYoshinobu Inoue struct addrinfo *hi; 8574dd8b5abSYoshinobu Inoue 8584dd8b5abSYoshinobu Inoue #ifdef INET6 8594dd8b5abSYoshinobu Inoue /* 8604dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 8614dd8b5abSYoshinobu Inoue * specified in rfc2373. 8624dd8b5abSYoshinobu Inoue */ 8634dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 8644dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 8654dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 8664dd8b5abSYoshinobu Inoue #endif 867ea4e54b9SDavid Nugent 868ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 8694dd8b5abSYoshinobu Inoue port = su->su_port; 8704dd8b5abSYoshinobu Inoue su->su_port = 0; 871ea4e54b9SDavid Nugent while (hrp != NULL) { 872b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 873f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 874ea4e54b9SDavid Nugent thishost = hrp; 875ebd83647SYaroslav Tykhiy goto found; 876ea4e54b9SDavid Nugent } 8774dd8b5abSYoshinobu Inoue #ifdef INET6 8784dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 879f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 8804dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 881f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 8824dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 8834dd8b5abSYoshinobu Inoue thishost = hrp; 884ebd83647SYaroslav Tykhiy goto found; 8854dd8b5abSYoshinobu Inoue } 8864dd8b5abSYoshinobu Inoue #endif 887f38c6cadSYoshinobu Inoue } 888ea4e54b9SDavid Nugent hrp = hrp->next; 889ea4e54b9SDavid Nugent } 890ebd83647SYaroslav Tykhiy found: 8914dd8b5abSYoshinobu Inoue su->su_port = port; 892ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 893ea4e54b9SDavid Nugent hostname = thishost->hostname; 894ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 895ea4e54b9SDavid Nugent } 896ea4e54b9SDavid Nugent #endif 897ea4e54b9SDavid Nugent 898ea022d16SRodney W. Grimes /* 899ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 900ea022d16SRodney W. Grimes */ 901ea022d16SRodney W. Grimes static char * 902e4bc453cSWarner Losh sgetsave(char *s) 903ea022d16SRodney W. Grimes { 904e3765043SYaroslav Tykhiy char *new = malloc(strlen(s) + 1); 905ea022d16SRodney W. Grimes 906ea022d16SRodney W. Grimes if (new == NULL) { 90702c97492SYaroslav Tykhiy reply(421, "Ran out of memory."); 908ea022d16SRodney W. Grimes dologout(1); 909ea022d16SRodney W. Grimes /* NOTREACHED */ 910ea022d16SRodney W. Grimes } 911ea022d16SRodney W. Grimes (void) strcpy(new, s); 912ea022d16SRodney W. Grimes return (new); 913ea022d16SRodney W. Grimes } 914ea022d16SRodney W. Grimes 915ea022d16SRodney W. Grimes /* 916ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 917ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 918ea022d16SRodney W. Grimes * (e.g., globbing). 919c29b9b47SYaroslav Tykhiy * NB: The data returned by sgetpwnam() will remain valid until 920c29b9b47SYaroslav Tykhiy * the next call to this function. Its difference from getpwnam() 921c29b9b47SYaroslav Tykhiy * is that sgetpwnam() is known to be called from ftpd code only. 922ea022d16SRodney W. Grimes */ 923ea022d16SRodney W. Grimes static struct passwd * 924e4bc453cSWarner Losh sgetpwnam(char *name) 925ea022d16SRodney W. Grimes { 926ea022d16SRodney W. Grimes static struct passwd save; 927ea022d16SRodney W. Grimes struct passwd *p; 928ea022d16SRodney W. Grimes 929ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 930ea022d16SRodney W. Grimes return (p); 931ea022d16SRodney W. Grimes if (save.pw_name) { 932ea022d16SRodney W. Grimes free(save.pw_name); 933ea022d16SRodney W. Grimes free(save.pw_passwd); 934ea022d16SRodney W. Grimes free(save.pw_gecos); 935ea022d16SRodney W. Grimes free(save.pw_dir); 936ea022d16SRodney W. Grimes free(save.pw_shell); 937ea022d16SRodney W. Grimes } 938ea022d16SRodney W. Grimes save = *p; 939ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 940ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 941ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 942ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 943ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 944ea022d16SRodney W. Grimes return (&save); 945ea022d16SRodney W. Grimes } 946ea022d16SRodney W. Grimes 947ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 948ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 94990906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 950ea022d16SRodney W. Grimes 951ea022d16SRodney W. Grimes /* 952ea022d16SRodney W. Grimes * USER command. 953ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 954ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 955ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 956ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 957ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 958ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 959ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 960ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 961ea022d16SRodney W. Grimes */ 962ea022d16SRodney W. Grimes void 963e4bc453cSWarner Losh user(char *name) 964ea022d16SRodney W. Grimes { 965ea022d16SRodney W. Grimes char *cp, *shell; 966ea022d16SRodney W. Grimes 967ea022d16SRodney W. Grimes if (logged_in) { 968ea022d16SRodney W. Grimes if (guest) { 969ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 970ea022d16SRodney W. Grimes return; 971a5a4544eSPaul Traina } else if (dochroot) { 972a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 973a5a4544eSPaul Traina return; 974ea022d16SRodney W. Grimes } 975ea022d16SRodney W. Grimes end_login(); 976ea022d16SRodney W. Grimes } 977ea022d16SRodney W. Grimes 978ea022d16SRodney W. Grimes guest = 0; 97963047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING 98063047c6fSDavid E. O'Brien pw = sgetpwnam(thishost->anonuser); 98163047c6fSDavid E. O'Brien #else 98263047c6fSDavid E. O'Brien pw = sgetpwnam("ftp"); 98363047c6fSDavid E. O'Brien #endif 984ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 9858657b576SYaroslav Tykhiy if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL) || 9868657b576SYaroslav Tykhiy checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL)) 987ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 98863047c6fSDavid E. O'Brien else if (pw != NULL) { 989ea022d16SRodney W. Grimes guest = 1; 990ea022d16SRodney W. Grimes askpasswd = 1; 991ea022d16SRodney W. Grimes reply(331, 9922c60c54cSPaul Traina "Guest login ok, send your email address as password."); 993ea022d16SRodney W. Grimes } else 994ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 995ea022d16SRodney W. Grimes if (!askpasswd && logging) 996ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 997ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 998ea022d16SRodney W. Grimes return; 999ea022d16SRodney W. Grimes } 10005a392aecSTorsten Blum if (anon_only != 0) { 10015a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 10025a392aecSTorsten Blum return; 10035a392aecSTorsten Blum } 10045a392aecSTorsten Blum 10059aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 1006ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 1007ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 1008c433c9daSPhilippe Charnier setusershell(); 1009ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 1010ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 1011ea022d16SRodney W. Grimes break; 1012ea022d16SRodney W. Grimes endusershell(); 1013ea022d16SRodney W. Grimes 10148657b576SYaroslav Tykhiy if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1, NULL)) { 1015ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1016ea022d16SRodney W. Grimes if (logging) 1017ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1018ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 1019ea022d16SRodney W. Grimes remotehost, name); 10207e295315SYaroslav Tykhiy pw = NULL; 1021ea022d16SRodney W. Grimes return; 1022ea022d16SRodney W. Grimes } 1023ea022d16SRodney W. Grimes } 1024ea022d16SRodney W. Grimes if (logging) 1025ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 102647499ecdSAndrey A. Chernov 102747499ecdSAndrey A. Chernov pwok = 0; 1028fa1746c9SMark Murray #ifdef USE_PAM 1029fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1030726040deSGuido van Rooij #endif 103147499ecdSAndrey A. Chernov if (opiechallenge(&opiedata, name, opieprompt) == 0) { 103247499ecdSAndrey A. Chernov pwok = (pw != NULL) && 103347499ecdSAndrey A. Chernov opieaccessfile(remotehost) && 103447499ecdSAndrey A. Chernov opiealways(pw->pw_dir); 103547499ecdSAndrey A. Chernov reply(331, "Response to %s %s for %s.", 103647499ecdSAndrey A. Chernov opieprompt, pwok ? "requested" : "required", name); 103747499ecdSAndrey A. Chernov } else { 103847499ecdSAndrey A. Chernov pwok = 1; 103947499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name); 104047499ecdSAndrey A. Chernov } 1041ea022d16SRodney W. Grimes askpasswd = 1; 1042ea022d16SRodney W. Grimes /* 1043ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 1044ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 1045ea022d16SRodney W. Grimes */ 1046ea022d16SRodney W. Grimes if (login_attempts) 1047e3765043SYaroslav Tykhiy sleep(login_attempts); 1048ea022d16SRodney W. Grimes } 1049ea022d16SRodney W. Grimes 1050ea022d16SRodney W. Grimes /* 10518657b576SYaroslav Tykhiy * Check if a user is in the file "fname", 10528657b576SYaroslav Tykhiy * return a pointer to a malloc'd string with the rest 10538657b576SYaroslav Tykhiy * of the matching line in "residue" if not NULL. 1054ea022d16SRodney W. Grimes */ 1055ea022d16SRodney W. Grimes static int 10568657b576SYaroslav Tykhiy checkuser(char *fname, char *name, int pwset, char **residue) 1057ea022d16SRodney W. Grimes { 1058ea022d16SRodney W. Grimes FILE *fd; 1059ea022d16SRodney W. Grimes int found = 0; 1060737d08f3SYaroslav Tykhiy size_t len; 1061737d08f3SYaroslav Tykhiy char *line, *mp, *p; 1062ea022d16SRodney W. Grimes 1063a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 1064737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) { 1065737d08f3SYaroslav Tykhiy /* skip comments */ 1066ea022d16SRodney W. Grimes if (line[0] == '#') 1067ea022d16SRodney W. Grimes continue; 1068737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 1069737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 1070737d08f3SYaroslav Tykhiy mp = NULL; 1071737d08f3SYaroslav Tykhiy } else { 1072737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 1073737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 1074737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 1075737d08f3SYaroslav Tykhiy mp[len] = '\0'; 1076737d08f3SYaroslav Tykhiy line = mp; 1077737d08f3SYaroslav Tykhiy } 1078737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */ 1079737d08f3SYaroslav Tykhiy p = strtok(line, " \t"); 1080737d08f3SYaroslav Tykhiy /* skip empty lines */ 1081737d08f3SYaroslav Tykhiy if (p == NULL) 1082737d08f3SYaroslav Tykhiy goto nextline; 108331fea7b8SDavid Nugent /* 108431fea7b8SDavid Nugent * if first chr is '@', check group membership 108531fea7b8SDavid Nugent */ 1086737d08f3SYaroslav Tykhiy if (p[0] == '@') { 108731fea7b8SDavid Nugent int i = 0; 108831fea7b8SDavid Nugent struct group *grp; 108931fea7b8SDavid Nugent 10908657b576SYaroslav Tykhiy if (p[1] == '\0') /* single @ matches anyone */ 10918657b576SYaroslav Tykhiy found = 1; 10928657b576SYaroslav Tykhiy else { 1093737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL) 1094737d08f3SYaroslav Tykhiy goto nextline; 10957edcb936SSteve Price /* 10967edcb936SSteve Price * Check user's default group 10977edcb936SSteve Price */ 10987edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 10997edcb936SSteve Price found = 1; 11007edcb936SSteve Price /* 11017edcb936SSteve Price * Check supplementary groups 11027edcb936SSteve Price */ 110331fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 110431fea7b8SDavid Nugent found = strcmp(name, 110531fea7b8SDavid Nugent grp->gr_mem[i++]) 110631fea7b8SDavid Nugent == 0; 1107ea022d16SRodney W. Grimes } 11088657b576SYaroslav Tykhiy } 110931fea7b8SDavid Nugent /* 111031fea7b8SDavid Nugent * Otherwise, just check for username match 111131fea7b8SDavid Nugent */ 111231fea7b8SDavid Nugent else 1113737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0; 11148657b576SYaroslav Tykhiy /* 11158657b576SYaroslav Tykhiy * Save the rest of line to "residue" if matched 11168657b576SYaroslav Tykhiy */ 11178657b576SYaroslav Tykhiy if (found && residue) { 11180ba71e24SYaroslav Tykhiy if ((p = strtok(NULL, "")) != NULL) 11190ba71e24SYaroslav Tykhiy p += strspn(p, " \t"); 11200ba71e24SYaroslav Tykhiy if (p && *p) { 11218657b576SYaroslav Tykhiy if ((*residue = strdup(p)) == NULL) 11228657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 11238657b576SYaroslav Tykhiy } else 11248657b576SYaroslav Tykhiy *residue = NULL; 11258657b576SYaroslav Tykhiy } 1126737d08f3SYaroslav Tykhiy nextline: 1127737d08f3SYaroslav Tykhiy if (mp) 1128737d08f3SYaroslav Tykhiy free(mp); 1129ea022d16SRodney W. Grimes } 1130ea022d16SRodney W. Grimes (void) fclose(fd); 1131ea022d16SRodney W. Grimes } 1132ea022d16SRodney W. Grimes return (found); 1133ea022d16SRodney W. Grimes } 1134ea022d16SRodney W. Grimes 1135ea022d16SRodney W. Grimes /* 1136ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1137ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1138ea022d16SRodney W. Grimes */ 1139ea022d16SRodney W. Grimes static void 1140e4bc453cSWarner Losh end_login(void) 1141ea022d16SRodney W. Grimes { 11425bc9d93dSMark Murray #ifdef USE_PAM 11435bc9d93dSMark Murray int e; 11445bc9d93dSMark Murray #endif 1145ea022d16SRodney W. Grimes 114652e7ee74SYaroslav Tykhiy (void) seteuid(0); 11475d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) 114846948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 1149ea022d16SRodney W. Grimes pw = NULL; 1150b071c689SDavid Nugent #ifdef LOGIN_CAP 115152e7ee74SYaroslav Tykhiy setusercontext(NULL, getpwuid(0), 0, 1152d9e2c424SRobert Watson LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK| 1153d9e2c424SRobert Watson LOGIN_SETMAC); 1154b071c689SDavid Nugent #endif 11555bc9d93dSMark Murray #ifdef USE_PAM 1156de45162dSYaroslav Tykhiy if (pamh) { 11575bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 11585bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 11595bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 11605bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 11615bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 11625bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 11635bc9d93dSMark Murray pamh = NULL; 1164de45162dSYaroslav Tykhiy } 11655bc9d93dSMark Murray #endif 1166ea022d16SRodney W. Grimes logged_in = 0; 1167ea022d16SRodney W. Grimes guest = 0; 1168a5a4544eSPaul Traina dochroot = 0; 1169ea022d16SRodney W. Grimes } 1170ea022d16SRodney W. Grimes 11715bc9d93dSMark Murray #ifdef USE_PAM 11726c9134c0SMark Murray 11736c9134c0SMark Murray /* 11746c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 11756c9134c0SMark Murray * login.c 11766c9134c0SMark Murray */ 11776c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 11786c9134c0SMark Murray 11796c9134c0SMark Murray struct cred_t { 11806c9134c0SMark Murray const char *uname; /* user name */ 11816c9134c0SMark Murray const char *pass; /* password */ 11826c9134c0SMark Murray }; 11836c9134c0SMark Murray typedef struct cred_t cred_t; 11846c9134c0SMark Murray 11856c9134c0SMark Murray static int 11866c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 11876c9134c0SMark Murray struct pam_response **resp, void *appdata) 11886c9134c0SMark Murray { 11896c9134c0SMark Murray int i; 11906c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 119160769b19SDag-Erling Smørgrav struct pam_response *reply; 119260769b19SDag-Erling Smørgrav 119360769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply); 119460769b19SDag-Erling Smørgrav if (reply == NULL) 119560769b19SDag-Erling Smørgrav return PAM_BUF_ERR; 11966c9134c0SMark Murray 11976c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 11986c9134c0SMark Murray switch (msg[i]->msg_style) { 11996c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 12006c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12016c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 12026c9134c0SMark Murray /* PAM frees resp. */ 12036c9134c0SMark Murray break; 12046c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 12056c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12066c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 12076c9134c0SMark Murray /* PAM frees resp. */ 12086c9134c0SMark Murray break; 12096c9134c0SMark Murray case PAM_TEXT_INFO: 12106c9134c0SMark Murray case PAM_ERROR_MSG: 12116c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12126c9134c0SMark Murray reply[i].resp = NULL; 12136c9134c0SMark Murray break; 12146c9134c0SMark Murray default: /* unknown message style */ 12156c9134c0SMark Murray free(reply); 12166c9134c0SMark Murray return PAM_CONV_ERR; 12176c9134c0SMark Murray } 12186c9134c0SMark Murray } 12196c9134c0SMark Murray 12206c9134c0SMark Murray *resp = reply; 12216c9134c0SMark Murray return PAM_SUCCESS; 12226c9134c0SMark Murray } 12236c9134c0SMark Murray 12246c9134c0SMark Murray /* 12256c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 12266c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 12276c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 12286c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 12296c9134c0SMark Murray * fall back to a different authentication mechanism. 12306c9134c0SMark Murray */ 12316c9134c0SMark Murray static int 12326c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 12336c9134c0SMark Murray { 12346c9134c0SMark Murray const char *tmpl_user; 12356c9134c0SMark Murray const void *item; 12366c9134c0SMark Murray int rval; 12376c9134c0SMark Murray int e; 12386c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 12396c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 12406c9134c0SMark Murray 12416c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 12426c9134c0SMark Murray if (e != PAM_SUCCESS) { 1243545ea864SYaroslav Tykhiy /* 1244545ea864SYaroslav Tykhiy * In OpenPAM, it's OK to pass NULL to pam_strerror() 1245545ea864SYaroslav Tykhiy * if context creation has failed in the first place. 1246545ea864SYaroslav Tykhiy */ 1247545ea864SYaroslav Tykhiy syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e)); 12486c9134c0SMark Murray return -1; 12496c9134c0SMark Murray } 12506c9134c0SMark Murray 1251ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1252ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1253ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1254ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1255de45162dSYaroslav Tykhiy if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 1256de45162dSYaroslav Tykhiy syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 1257de45162dSYaroslav Tykhiy } 1258de45162dSYaroslav Tykhiy pamh = NULL; 1259ea413ab7SGuido van Rooij return -1; 1260ea413ab7SGuido van Rooij } 1261ea413ab7SGuido van Rooij 12626c9134c0SMark Murray e = pam_authenticate(pamh, 0); 12636c9134c0SMark Murray switch (e) { 12646c9134c0SMark Murray case PAM_SUCCESS: 12656c9134c0SMark Murray /* 12666c9134c0SMark Murray * With PAM we support the concept of a "template" 12676c9134c0SMark Murray * user. The user enters a login name which is 12686c9134c0SMark Murray * authenticated by PAM, usually via a remote service 12696c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 12706c9134c0SMark Murray * succeeds, a different but related "template" name 12716c9134c0SMark Murray * is used for setting the credentials, shell, and 12726c9134c0SMark Murray * home directory. The name the user enters need only 12736c9134c0SMark Murray * exist on the remote authentication server, but the 12746c9134c0SMark Murray * template name must be present in the local password 12756c9134c0SMark Murray * database. 12766c9134c0SMark Murray * 12776c9134c0SMark Murray * This is supported by two various mechanisms in the 12786c9134c0SMark Murray * individual modules. However, from the application's 12796c9134c0SMark Murray * point of view, the template user is always passed 12806c9134c0SMark Murray * back as a changed value of the PAM_USER item. 12816c9134c0SMark Murray */ 12826c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 12836c9134c0SMark Murray PAM_SUCCESS) { 12846c9134c0SMark Murray tmpl_user = (const char *) item; 12856c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 12866c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 12876c9134c0SMark Murray } else 12886c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 12896c9134c0SMark Murray pam_strerror(pamh, e)); 12906c9134c0SMark Murray rval = 0; 12916c9134c0SMark Murray break; 12926c9134c0SMark Murray 12936c9134c0SMark Murray case PAM_AUTH_ERR: 12946c9134c0SMark Murray case PAM_USER_UNKNOWN: 12956c9134c0SMark Murray case PAM_MAXTRIES: 12966c9134c0SMark Murray rval = 1; 12976c9134c0SMark Murray break; 12986c9134c0SMark Murray 12996c9134c0SMark Murray default: 13005bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 13016c9134c0SMark Murray rval = -1; 13026c9134c0SMark Murray break; 13036c9134c0SMark Murray } 13046c9134c0SMark Murray 13055bc9d93dSMark Murray if (rval == 0) { 13065bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 13075bc9d93dSMark Murray if (e != PAM_SUCCESS) { 13084cbc4ad6SYaroslav Tykhiy syslog(LOG_ERR, "pam_acct_mgmt: %s", 13094cbc4ad6SYaroslav Tykhiy pam_strerror(pamh, e)); 13105bc9d93dSMark Murray rval = 1; 13115bc9d93dSMark Murray } 13125bc9d93dSMark Murray } 13135bc9d93dSMark Murray 13145bc9d93dSMark Murray if (rval != 0) { 13156c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 13166c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 13175bc9d93dSMark Murray } 13185bc9d93dSMark Murray pamh = NULL; 13196c9134c0SMark Murray } 13206c9134c0SMark Murray return rval; 13216c9134c0SMark Murray } 13226c9134c0SMark Murray 13235bc9d93dSMark Murray #endif /* USE_PAM */ 13246c9134c0SMark Murray 1325ea022d16SRodney W. Grimes void 1326e4bc453cSWarner Losh pass(char *passwd) 1327ea022d16SRodney W. Grimes { 1328a5a4544eSPaul Traina int rval; 1329ea022d16SRodney W. Grimes FILE *fd; 1330b071c689SDavid Nugent #ifdef LOGIN_CAP 1331b071c689SDavid Nugent login_cap_t *lc = NULL; 1332b071c689SDavid Nugent #endif 13335bc9d93dSMark Murray #ifdef USE_PAM 13345bc9d93dSMark Murray int e; 13355bc9d93dSMark Murray #endif 1336341e476eSYaroslav Tykhiy char *residue = NULL; 133747499ecdSAndrey A. Chernov char *xpasswd; 1338ea022d16SRodney W. Grimes 1339ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1340ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1341ea022d16SRodney W. Grimes return; 1342ea022d16SRodney W. Grimes } 1343ea022d16SRodney W. Grimes askpasswd = 0; 1344ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1345a5a4544eSPaul Traina if (pw == NULL) { 1346a5a4544eSPaul Traina rval = 1; /* failure below */ 1347a5a4544eSPaul Traina goto skip; 1348a5a4544eSPaul Traina } 13495bc9d93dSMark Murray #ifdef USE_PAM 13506c9134c0SMark Murray rval = auth_pam(&pw, passwd); 1351f650a124SAndrey A. Chernov if (rval >= 0) { 1352f650a124SAndrey A. Chernov opieunlock(); 1353a5a4544eSPaul Traina goto skip; 1354f650a124SAndrey A. Chernov } 1355f650a124SAndrey A. Chernov #endif 135647499ecdSAndrey A. Chernov if (opieverify(&opiedata, passwd) == 0) 135747499ecdSAndrey A. Chernov xpasswd = pw->pw_passwd; 1358f650a124SAndrey A. Chernov else if (pwok) { 135947499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd); 1360f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1361f650a124SAndrey A. Chernov xpasswd = ":"; 1362f650a124SAndrey A. Chernov } else { 136347499ecdSAndrey A. Chernov rval = 1; 136447499ecdSAndrey A. Chernov goto skip; 136547499ecdSAndrey A. Chernov } 136647499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd); 1367f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1368a5a4544eSPaul Traina rval = 1; /* failure */ 1369a5a4544eSPaul Traina skip: 1370a5a4544eSPaul Traina /* 1371a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 13726c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1373a5a4544eSPaul Traina * succeeded. 1374a5a4544eSPaul Traina */ 1375a5a4544eSPaul Traina if (rval) { 1376ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1377b8939f6fSYaroslav Tykhiy if (logging) { 1378ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1379b8939f6fSYaroslav Tykhiy "FTP LOGIN FAILED FROM %s", 1380b8939f6fSYaroslav Tykhiy remotehost); 1381b8939f6fSYaroslav Tykhiy syslog(LOG_AUTHPRIV | LOG_NOTICE, 1382ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1383ea022d16SRodney W. Grimes remotehost, curname); 1384b8939f6fSYaroslav Tykhiy } 1385ea022d16SRodney W. Grimes pw = NULL; 1386ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1387ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1388ea022d16SRodney W. Grimes "repeated login failures from %s", 1389ea022d16SRodney W. Grimes remotehost); 1390ea022d16SRodney W. Grimes exit(0); 1391ea022d16SRodney W. Grimes } 1392ea022d16SRodney W. Grimes return; 1393ea022d16SRodney W. Grimes } 1394ea022d16SRodney W. Grimes } 1395ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1396c4536e21SYaroslav Tykhiy if (setegid(pw->pw_gid) < 0) { 1397ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1398ea022d16SRodney W. Grimes return; 1399ea022d16SRodney W. Grimes } 1400b071c689SDavid Nugent /* May be overridden by login.conf */ 1401b071c689SDavid Nugent (void) umask(defumask); 1402b071c689SDavid Nugent #ifdef LOGIN_CAP 14035d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 140404683b2cSYaroslav Tykhiy char remote_ip[NI_MAXHOST]; 1405b071c689SDavid Nugent 140604683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 14074dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 140804683b2cSYaroslav Tykhiy NI_NUMERICHOST)) 140904683b2cSYaroslav Tykhiy *remote_ip = 0; 1410b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1411b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1412b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1413b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1414b071c689SDavid Nugent pw->pw_name); 14154a3e5acdSYaroslav Tykhiy reply(530, "Permission denied."); 1416b071c689SDavid Nugent pw = NULL; 1417b071c689SDavid Nugent return; 1418b071c689SDavid Nugent } 1419b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 14204a3e5acdSYaroslav Tykhiy reply(530, "Login not available right now."); 1421b071c689SDavid Nugent pw = NULL; 1422b071c689SDavid Nugent return; 1423b071c689SDavid Nugent } 1424b071c689SDavid Nugent } 142552e7ee74SYaroslav Tykhiy setusercontext(lc, pw, 0, 1426e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1427d9e2c424SRobert Watson LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC); 1428b071c689SDavid Nugent #else 1429e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1430ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1431b071c689SDavid Nugent #endif 1432ea022d16SRodney W. Grimes 14335bc9d93dSMark Murray #ifdef USE_PAM 14345bc9d93dSMark Murray if (pamh) { 14355bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 14365bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 14375bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 14385bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 14395bc9d93dSMark Murray } 14405bc9d93dSMark Murray } 14415bc9d93dSMark Murray #endif 14425bc9d93dSMark Murray 1443ea022d16SRodney W. Grimes /* open wtmp before chroot */ 14445d7e0128SYaroslav Tykhiy if (dowtmp) 14455d7e0128SYaroslav Tykhiy ftpd_logwtmp(ttyline, pw->pw_name, 14465d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr); 1447ea022d16SRodney W. Grimes logged_in = 1; 1448ea022d16SRodney W. Grimes 1449a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1450ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 145163047c6fSDavid E. O'Brien statfd = open(thishost->statfile, O_WRONLY|O_APPEND); 1452ea4e54b9SDavid Nugent #else 145363047c6fSDavid E. O'Brien statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND); 1454ea4e54b9SDavid Nugent #endif 145563047c6fSDavid E. O'Brien if (statfd < 0) 14563eb568f2SGuido van Rooij stats = 0; 14573eb568f2SGuido van Rooij 1458b071c689SDavid Nugent dochroot = 1459341e476eSYaroslav Tykhiy checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue) 1460b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 14618657b576SYaroslav Tykhiy || login_getcapbool(lc, "ftp-chroot", 0) 1462b071c689SDavid Nugent #endif 14638657b576SYaroslav Tykhiy ; 1464ce9287fcSYaroslav Tykhiy chrootdir = NULL; 1465ea022d16SRodney W. Grimes /* 1466ce9287fcSYaroslav Tykhiy * For a chrooted local user, 1467ce9287fcSYaroslav Tykhiy * a) see whether ftpchroot(5) specifies a chroot directory, 1468ce9287fcSYaroslav Tykhiy * b) extract the directory pathname from the line, 1469ce9287fcSYaroslav Tykhiy * c) expand it to the absolute pathname if necessary. 1470ea022d16SRodney W. Grimes */ 1471ce9287fcSYaroslav Tykhiy if (dochroot && residue && 147239bce482SYaroslav Tykhiy (chrootdir = strtok(residue, " \t")) != NULL) { 147339bce482SYaroslav Tykhiy if (chrootdir[0] != '/') 1474341e476eSYaroslav Tykhiy asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir); 147539bce482SYaroslav Tykhiy else 1476215a9f9dSYaroslav Tykhiy chrootdir = strdup(chrootdir); /* make it permanent */ 1477341e476eSYaroslav Tykhiy if (chrootdir == NULL) 14788657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 14798657b576SYaroslav Tykhiy } 1480ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1481ce9287fcSYaroslav Tykhiy /* 1482ce9287fcSYaroslav Tykhiy * If no chroot directory set yet, use the login directory. 1483ce9287fcSYaroslav Tykhiy * Copy it so it can be modified while pw->pw_dir stays intact. 1484ce9287fcSYaroslav Tykhiy */ 1485ce9287fcSYaroslav Tykhiy if (chrootdir == NULL && 1486ce9287fcSYaroslav Tykhiy (chrootdir = strdup(pw->pw_dir)) == NULL) 1487ce9287fcSYaroslav Tykhiy fatalerror("Ran out of memory."); 1488ce9287fcSYaroslav Tykhiy /* 1489ce9287fcSYaroslav Tykhiy * Check for the "/chroot/./home" syntax, 1490ce9287fcSYaroslav Tykhiy * separate the chroot and home directory pathnames. 1491ce9287fcSYaroslav Tykhiy */ 1492ce9287fcSYaroslav Tykhiy if ((homedir = strstr(chrootdir, "/./")) != NULL) { 1493ce9287fcSYaroslav Tykhiy *(homedir++) = '\0'; /* wipe '/' */ 1494ce9287fcSYaroslav Tykhiy homedir++; /* skip '.' */ 1495ce9287fcSYaroslav Tykhiy } else { 1496ce9287fcSYaroslav Tykhiy /* 1497ce9287fcSYaroslav Tykhiy * We MUST do a chdir() after the chroot. Otherwise 1498ce9287fcSYaroslav Tykhiy * the old current directory will be accessible as "." 1499ce9287fcSYaroslav Tykhiy * outside the new root! 1500ce9287fcSYaroslav Tykhiy */ 1501ce9287fcSYaroslav Tykhiy homedir = "/"; 1502ce9287fcSYaroslav Tykhiy } 1503ce9287fcSYaroslav Tykhiy /* 1504ce9287fcSYaroslav Tykhiy * Finally, do chroot() 1505ce9287fcSYaroslav Tykhiy */ 1506ce9287fcSYaroslav Tykhiy if (chroot(chrootdir) < 0) { 1507a5a4544eSPaul Traina reply(550, "Can't change root."); 1508a5a4544eSPaul Traina goto bad; 1509a5a4544eSPaul Traina } 1510ce9287fcSYaroslav Tykhiy } else /* real user w/o chroot */ 1511ce9287fcSYaroslav Tykhiy homedir = pw->pw_dir; 1512ce9287fcSYaroslav Tykhiy /* 1513ce9287fcSYaroslav Tykhiy * Set euid *before* doing chdir() so 1514ce9287fcSYaroslav Tykhiy * a) the user won't be carried to a directory that he couldn't reach 1515ce9287fcSYaroslav Tykhiy * on his own due to no permission to upper path components, 1516ce9287fcSYaroslav Tykhiy * b) NFS mounted homedirs w/restrictive permissions will be accessible 1517ce9287fcSYaroslav Tykhiy * (uid 0 has no root power over NFS if not mapped explicitly.) 1518ce9287fcSYaroslav Tykhiy */ 151952e7ee74SYaroslav Tykhiy if (seteuid(pw->pw_uid) < 0) { 1520ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1521ea022d16SRodney W. Grimes goto bad; 1522ea022d16SRodney W. Grimes } 1523ce9287fcSYaroslav Tykhiy if (chdir(homedir) < 0) { 1524ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1525ce9287fcSYaroslav Tykhiy reply(550, "Can't change to base directory."); 1526ce9287fcSYaroslav Tykhiy goto bad; 1527ce9287fcSYaroslav Tykhiy } else { 1528ce9287fcSYaroslav Tykhiy if (chdir("/") < 0) { 1529ce9287fcSYaroslav Tykhiy reply(550, "Root is inaccessible."); 1530ce9287fcSYaroslav Tykhiy goto bad; 1531ce9287fcSYaroslav Tykhiy } 153202c97492SYaroslav Tykhiy lreply(230, "No directory! Logging in with home=/."); 1533ce9287fcSYaroslav Tykhiy } 1534ce9287fcSYaroslav Tykhiy } 153582c76939SDavid Greenman 153682c76939SDavid Greenman /* 1537ea022d16SRodney W. Grimes * Display a login message, if it exists. 1538ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1539ea022d16SRodney W. Grimes */ 1540ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 154163047c6fSDavid E. O'Brien fd = fopen(thishost->loginmsg, "r"); 1542ea4e54b9SDavid Nugent #else 154363047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPLOGINMESG, "r"); 1544ea4e54b9SDavid Nugent #endif 154563047c6fSDavid E. O'Brien if (fd != NULL) { 1546ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1547ea022d16SRodney W. Grimes 1548ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1549ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1550ea022d16SRodney W. Grimes *cp = '\0'; 1551ea022d16SRodney W. Grimes lreply(230, "%s", line); 1552ea022d16SRodney W. Grimes } 1553ea022d16SRodney W. Grimes (void) fflush(stdout); 1554ea022d16SRodney W. Grimes (void) fclose(fd); 1555ea022d16SRodney W. Grimes } 1556ea022d16SRodney W. Grimes if (guest) { 15573eb568f2SGuido van Rooij if (ident != NULL) 15583eb568f2SGuido van Rooij free(ident); 155961f891a6SPaul Traina ident = strdup(passwd); 156061f891a6SPaul Traina if (ident == NULL) 1561618b0bbaSMark Murray fatalerror("Ran out of memory."); 156261f891a6SPaul Traina 1563ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1564ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1565ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1566ea4e54b9SDavid Nugent if (thishost != firsthost) 1567ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1568b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname, 1569b3a0a7cdSMike Heffner passwd); 1570ea4e54b9SDavid Nugent else 1571ea4e54b9SDavid Nugent #endif 1572ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1573b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd); 1574b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1575ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1576ea022d16SRodney W. Grimes if (logging) 1577ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1578ea022d16SRodney W. Grimes remotehost, passwd); 1579ea022d16SRodney W. Grimes } else { 15803401a71fSDaniel O'Callaghan if (dochroot) 158111342ab1SYaroslav Tykhiy reply(230, "User %s logged in, " 158211342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name); 15833401a71fSDaniel O'Callaghan else 1584ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 15853401a71fSDaniel O'Callaghan 1586ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1587ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 15887a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name); 1589b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1590ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1591ea022d16SRodney W. Grimes if (logging) 1592ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1593ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1594ea022d16SRodney W. Grimes } 1595e897216fSYaroslav Tykhiy if (guest || dochroot) 1596e897216fSYaroslav Tykhiy syslog(LOG_INFO, "session root changed to %s", chrootdir); 1597b071c689SDavid Nugent #ifdef LOGIN_CAP 1598b071c689SDavid Nugent login_close(lc); 1599b071c689SDavid Nugent #endif 1600ce9287fcSYaroslav Tykhiy if (residue) 1601ce9287fcSYaroslav Tykhiy free(residue); 1602ea022d16SRodney W. Grimes return; 1603ea022d16SRodney W. Grimes bad: 1604ea022d16SRodney W. Grimes /* Forget all about it... */ 1605b071c689SDavid Nugent #ifdef LOGIN_CAP 1606b071c689SDavid Nugent login_close(lc); 1607b071c689SDavid Nugent #endif 1608ce9287fcSYaroslav Tykhiy if (residue) 1609ce9287fcSYaroslav Tykhiy free(residue); 1610ea022d16SRodney W. Grimes end_login(); 1611ea022d16SRodney W. Grimes } 1612ea022d16SRodney W. Grimes 1613ea022d16SRodney W. Grimes void 1614e4bc453cSWarner Losh retrieve(char *cmd, char *name) 1615ea022d16SRodney W. Grimes { 1616ea022d16SRodney W. Grimes FILE *fin, *dout; 1617ea022d16SRodney W. Grimes struct stat st; 1618e4bc453cSWarner Losh int (*closefunc)(FILE *); 1619158a00b2SJohn Birrell time_t start; 1620ea022d16SRodney W. Grimes 1621ea022d16SRodney W. Grimes if (cmd == 0) { 1622ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1623ea022d16SRodney W. Grimes st.st_size = 0; 1624ea022d16SRodney W. Grimes } else { 1625ea022d16SRodney W. Grimes char line[BUFSIZ]; 1626ea022d16SRodney W. Grimes 1627e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1628ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1629ea022d16SRodney W. Grimes st.st_size = -1; 1630ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1631ea022d16SRodney W. Grimes } 1632ea022d16SRodney W. Grimes if (fin == NULL) { 1633ea022d16SRodney W. Grimes if (errno != 0) { 1634ea022d16SRodney W. Grimes perror_reply(550, name); 1635ea022d16SRodney W. Grimes if (cmd == 0) { 1636ea022d16SRodney W. Grimes LOGCMD("get", name); 1637ea022d16SRodney W. Grimes } 1638ea022d16SRodney W. Grimes } 1639ea022d16SRodney W. Grimes return; 1640ea022d16SRodney W. Grimes } 1641ea022d16SRodney W. Grimes byte_count = -1; 1642ea701226SYaroslav Tykhiy if (cmd == 0) { 1643ea701226SYaroslav Tykhiy if (fstat(fileno(fin), &st) < 0) { 1644ea701226SYaroslav Tykhiy perror_reply(550, name); 1645ea701226SYaroslav Tykhiy goto done; 1646ea701226SYaroslav Tykhiy } 1647ea701226SYaroslav Tykhiy if (!S_ISREG(st.st_mode)) { 164810e89104SYaroslav Tykhiy /* 164910e89104SYaroslav Tykhiy * Never sending a raw directory is a workaround 165010e89104SYaroslav Tykhiy * for buggy clients that will attempt to RETR 165110e89104SYaroslav Tykhiy * a directory before listing it, e.g., Mozilla. 165210e89104SYaroslav Tykhiy * Preventing a guest from getting irregular files 165310e89104SYaroslav Tykhiy * is a simple security measure. 165410e89104SYaroslav Tykhiy */ 165510e89104SYaroslav Tykhiy if (S_ISDIR(st.st_mode) || guest) { 1656ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1657ea022d16SRodney W. Grimes goto done; 1658ea022d16SRodney W. Grimes } 1659ea701226SYaroslav Tykhiy st.st_size = -1; 1660ea701226SYaroslav Tykhiy /* st.st_blksize is set for all descriptor types */ 1661ea701226SYaroslav Tykhiy } 1662ea701226SYaroslav Tykhiy } 1663ea022d16SRodney W. Grimes if (restart_point) { 1664ea022d16SRodney W. Grimes if (type == TYPE_A) { 1665ea022d16SRodney W. Grimes off_t i, n; 1666ea022d16SRodney W. Grimes int c; 1667ea022d16SRodney W. Grimes 1668ea022d16SRodney W. Grimes n = restart_point; 1669ea022d16SRodney W. Grimes i = 0; 1670ea022d16SRodney W. Grimes while (i++ < n) { 1671ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1672ea022d16SRodney W. Grimes perror_reply(550, name); 1673ea022d16SRodney W. Grimes goto done; 1674ea022d16SRodney W. Grimes } 1675ea022d16SRodney W. Grimes if (c == '\n') 1676ea022d16SRodney W. Grimes i++; 1677ea022d16SRodney W. Grimes } 1678ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1679ea022d16SRodney W. Grimes perror_reply(550, name); 1680ea022d16SRodney W. Grimes goto done; 1681ea022d16SRodney W. Grimes } 1682ea022d16SRodney W. Grimes } 1683ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1684ea022d16SRodney W. Grimes if (dout == NULL) 1685ea022d16SRodney W. Grimes goto done; 16863eb568f2SGuido van Rooij time(&start); 16879fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 16889fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 1689c999732bSYaroslav Tykhiy if (cmd == 0 && guest && stats && byte_count > 0) 1690c999732bSYaroslav Tykhiy logxfer(name, byte_count, start); 1691ea022d16SRodney W. Grimes (void) fclose(dout); 1692ea022d16SRodney W. Grimes data = -1; 1693ea022d16SRodney W. Grimes pdata = -1; 1694ea022d16SRodney W. Grimes done: 1695ea022d16SRodney W. Grimes if (cmd == 0) 1696ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1697ea022d16SRodney W. Grimes (*closefunc)(fin); 1698ea022d16SRodney W. Grimes } 1699ea022d16SRodney W. Grimes 1700ea022d16SRodney W. Grimes void 1701e4bc453cSWarner Losh store(char *name, char *mode, int unique) 1702ea022d16SRodney W. Grimes { 1703a117c345SYaroslav Tykhiy int fd; 1704ea022d16SRodney W. Grimes FILE *fout, *din; 1705e4bc453cSWarner Losh int (*closefunc)(FILE *); 1706ea022d16SRodney W. Grimes 1707a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */ 1708a117c345SYaroslav Tykhiy if (unique) { 1709a117c345SYaroslav Tykhiy /* Programming error */ 1710a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE"); 1711a117c345SYaroslav Tykhiy unique = 0; 1712a117c345SYaroslav Tykhiy } 1713a117c345SYaroslav Tykhiy if (guest && noguestmod) { 171402c97492SYaroslav Tykhiy reply(550, "Appending to existing file denied."); 1715a117c345SYaroslav Tykhiy goto err; 1716a117c345SYaroslav Tykhiy } 1717a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */ 1718a117c345SYaroslav Tykhiy } 1719a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */ 1720a117c345SYaroslav Tykhiy restart_point = 0; 1721a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1722a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */ 172302c97492SYaroslav Tykhiy reply(550, "Modifying existing file denied."); 1724a117c345SYaroslav Tykhiy goto err; 1725a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */ 1726a117c345SYaroslav Tykhiy unique = 1; 1727ea022d16SRodney W. Grimes } 1728ea022d16SRodney W. Grimes 1729ea022d16SRodney W. Grimes if (restart_point) 1730a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */ 1731a117c345SYaroslav Tykhiy if (unique) { 1732a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0) 1733a117c345SYaroslav Tykhiy goto err; 1734a117c345SYaroslav Tykhiy fout = fdopen(fd, mode); 1735a117c345SYaroslav Tykhiy } else 1736ea022d16SRodney W. Grimes fout = fopen(name, mode); 1737ea022d16SRodney W. Grimes closefunc = fclose; 1738ea022d16SRodney W. Grimes if (fout == NULL) { 1739ea022d16SRodney W. Grimes perror_reply(553, name); 1740a117c345SYaroslav Tykhiy goto err; 1741ea022d16SRodney W. Grimes } 1742ea022d16SRodney W. Grimes byte_count = -1; 1743ea022d16SRodney W. Grimes if (restart_point) { 1744ea022d16SRodney W. Grimes if (type == TYPE_A) { 1745ea022d16SRodney W. Grimes off_t i, n; 1746ea022d16SRodney W. Grimes int c; 1747ea022d16SRodney W. Grimes 1748ea022d16SRodney W. Grimes n = restart_point; 1749ea022d16SRodney W. Grimes i = 0; 1750ea022d16SRodney W. Grimes while (i++ < n) { 1751ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1752ea022d16SRodney W. Grimes perror_reply(550, name); 1753ea022d16SRodney W. Grimes goto done; 1754ea022d16SRodney W. Grimes } 1755ea022d16SRodney W. Grimes if (c == '\n') 1756ea022d16SRodney W. Grimes i++; 1757ea022d16SRodney W. Grimes } 1758ea022d16SRodney W. Grimes /* 1759ea022d16SRodney W. Grimes * We must do this seek to "current" position 1760ea022d16SRodney W. Grimes * because we are changing from reading to 1761ea022d16SRodney W. Grimes * writing. 1762ea022d16SRodney W. Grimes */ 17630e519c96SYaroslav Tykhiy if (fseeko(fout, 0, SEEK_CUR) < 0) { 1764ea022d16SRodney W. Grimes perror_reply(550, name); 1765ea022d16SRodney W. Grimes goto done; 1766ea022d16SRodney W. Grimes } 1767ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1768ea022d16SRodney W. Grimes perror_reply(550, name); 1769ea022d16SRodney W. Grimes goto done; 1770ea022d16SRodney W. Grimes } 1771ea022d16SRodney W. Grimes } 17720e519c96SYaroslav Tykhiy din = dataconn(name, -1, "r"); 1773ea022d16SRodney W. Grimes if (din == NULL) 1774ea022d16SRodney W. Grimes goto done; 1775ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1776ea022d16SRodney W. Grimes if (unique) 1777ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1778ea022d16SRodney W. Grimes name); 1779ea022d16SRodney W. Grimes else 1780ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1781ea022d16SRodney W. Grimes } 1782ea022d16SRodney W. Grimes (void) fclose(din); 1783ea022d16SRodney W. Grimes data = -1; 1784ea022d16SRodney W. Grimes pdata = -1; 1785ea022d16SRodney W. Grimes done: 17867c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); 1787ea022d16SRodney W. Grimes (*closefunc)(fout); 1788a117c345SYaroslav Tykhiy return; 1789a117c345SYaroslav Tykhiy err: 1790a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name); 1791a117c345SYaroslav Tykhiy return; 1792ea022d16SRodney W. Grimes } 1793ea022d16SRodney W. Grimes 1794ea022d16SRodney W. Grimes static FILE * 1795e4bc453cSWarner Losh getdatasock(char *mode) 1796ea022d16SRodney W. Grimes { 1797ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1798ea022d16SRodney W. Grimes 1799ea022d16SRodney W. Grimes if (data >= 0) 1800ea022d16SRodney W. Grimes return (fdopen(data, mode)); 18014dd8b5abSYoshinobu Inoue 18024dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1803ea022d16SRodney W. Grimes if (s < 0) 1804ea022d16SRodney W. Grimes goto bad; 180557d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1806406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1807ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 18084dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 180963591ba5SYaroslav Tykhiy data_source.su_port = htons(dataport); 181052e7ee74SYaroslav Tykhiy (void) seteuid(0); 1811ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 18129ec7612aSYaroslav Tykhiy /* 18139ec7612aSYaroslav Tykhiy * We should loop here since it's possible that 18149ec7612aSYaroslav Tykhiy * another ftpd instance has passed this point and is 18159ec7612aSYaroslav Tykhiy * trying to open a data connection in active mode now. 18169ec7612aSYaroslav Tykhiy * Until the other connection is opened, we'll be getting 18179ec7612aSYaroslav Tykhiy * EADDRINUSE because no SOCK_STREAM sockets in the system 18189ec7612aSYaroslav Tykhiy * can share both local and remote addresses, localIP:20 18199ec7612aSYaroslav Tykhiy * and *:* in this case. 18209ec7612aSYaroslav Tykhiy */ 1821ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 18224dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1823ea022d16SRodney W. Grimes break; 1824ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1825ea022d16SRodney W. Grimes goto bad; 1826ea022d16SRodney W. Grimes sleep(tries); 1827ea022d16SRodney W. Grimes } 182852e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 1829ea022d16SRodney W. Grimes #ifdef IP_TOS 18304dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 18314dd8b5abSYoshinobu Inoue { 1832ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 183357d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1834406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 18354dd8b5abSYoshinobu Inoue } 1836ea022d16SRodney W. Grimes #endif 18379fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 18389fc5823aSGarrett Wollman /* 18399fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 18409fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 18419fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 18429fc5823aSGarrett Wollman * in heavy-load situations. 18439fc5823aSGarrett Wollman */ 18449fc5823aSGarrett Wollman on = 1; 184557d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1846406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 18479fc5823aSGarrett Wollman #endif 18489fc5823aSGarrett Wollman #ifdef SO_SNDBUF 18499fc5823aSGarrett Wollman on = 65536; 185057d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0) 1851406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m"); 18529fc5823aSGarrett Wollman #endif 18539fc5823aSGarrett Wollman 1854ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1855ea022d16SRodney W. Grimes bad: 1856ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1857ea022d16SRodney W. Grimes t = errno; 185852e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 1859ea022d16SRodney W. Grimes (void) close(s); 1860ea022d16SRodney W. Grimes errno = t; 1861ea022d16SRodney W. Grimes return (NULL); 1862ea022d16SRodney W. Grimes } 1863ea022d16SRodney W. Grimes 1864ea022d16SRodney W. Grimes static FILE * 1865e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode) 1866ea022d16SRodney W. Grimes { 1867ea022d16SRodney W. Grimes char sizebuf[32]; 1868ea022d16SRodney W. Grimes FILE *file; 1869e5094456SCrist J. Clark int retry = 0, tos, conerrno; 1870ea022d16SRodney W. Grimes 1871ea022d16SRodney W. Grimes file_size = size; 1872ea022d16SRodney W. Grimes byte_count = 0; 18730e519c96SYaroslav Tykhiy if (size != -1) 1874a57e1ef0SYaroslav Tykhiy (void) snprintf(sizebuf, sizeof(sizebuf), 1875a57e1ef0SYaroslav Tykhiy " (%jd bytes)", (intmax_t)size); 1876ea022d16SRodney W. Grimes else 1877e760ef2cSWarner Losh *sizebuf = '\0'; 1878ea022d16SRodney W. Grimes if (pdata >= 0) { 18794dd8b5abSYoshinobu Inoue union sockunion from; 18804cd48bacSYaroslav Tykhiy int flags; 18814dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1882d6ed3c37SGuido van Rooij struct timeval timeout; 1883d6ed3c37SGuido van Rooij fd_set set; 1884ea022d16SRodney W. Grimes 1885d6ed3c37SGuido van Rooij FD_ZERO(&set); 1886d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1887d6ed3c37SGuido van Rooij 1888d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1889d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1890d6ed3c37SGuido van Rooij 18914cd48bacSYaroslav Tykhiy /* 18924cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode, 18934cd48bacSYaroslav Tykhiy * accept() will block after a successful select() 18944cd48bacSYaroslav Tykhiy * if the selected connection dies in between. 18954cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here. 18964cd48bacSYaroslav Tykhiy */ 18974cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 18984cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 18994cd48bacSYaroslav Tykhiy goto pdata_err; 1900aa5a9d3fSYaroslav Tykhiy if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 || 19014cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 19024cd48bacSYaroslav Tykhiy goto pdata_err; 1903ea022d16SRodney W. Grimes (void) close(pdata); 1904ea022d16SRodney W. Grimes pdata = s; 19054cd48bacSYaroslav Tykhiy /* 1906f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag 1907f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it. 19084cd48bacSYaroslav Tykhiy */ 19094cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 19104cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 19114cd48bacSYaroslav Tykhiy goto pdata_err; 1912ea022d16SRodney W. Grimes #ifdef IP_TOS 19134dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 19144dd8b5abSYoshinobu Inoue { 1915a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 191657d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1917406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 19184dd8b5abSYoshinobu Inoue } 1919ea022d16SRodney W. Grimes #endif 1920ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1921ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1922ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 19234cd48bacSYaroslav Tykhiy pdata_err: 19244cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection."); 19254cd48bacSYaroslav Tykhiy (void) close(pdata); 19264cd48bacSYaroslav Tykhiy pdata = -1; 19274cd48bacSYaroslav Tykhiy return (NULL); 1928ea022d16SRodney W. Grimes } 1929ea022d16SRodney W. Grimes if (data >= 0) { 1930ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1931ea022d16SRodney W. Grimes name, sizebuf); 1932ea022d16SRodney W. Grimes usedefault = 1; 1933ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1934ea022d16SRodney W. Grimes } 1935ea022d16SRodney W. Grimes if (usedefault) 1936ea022d16SRodney W. Grimes data_dest = his_addr; 1937ea022d16SRodney W. Grimes usedefault = 1; 1938e5094456SCrist J. Clark do { 1939ea022d16SRodney W. Grimes file = getdatasock(mode); 1940ea022d16SRodney W. Grimes if (file == NULL) { 194104683b2cSYaroslav Tykhiy char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV]; 194204683b2cSYaroslav Tykhiy 194304683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&data_source, 194404683b2cSYaroslav Tykhiy data_source.su_len, 194504683b2cSYaroslav Tykhiy hostbuf, sizeof(hostbuf) - 1, 194604683b2cSYaroslav Tykhiy portbuf, sizeof(portbuf) - 1, 194704683b2cSYaroslav Tykhiy NI_NUMERICHOST|NI_NUMERICSERV)) 194804683b2cSYaroslav Tykhiy *hostbuf = *portbuf = 0; 194904683b2cSYaroslav Tykhiy hostbuf[sizeof(hostbuf) - 1] = 0; 195004683b2cSYaroslav Tykhiy portbuf[sizeof(portbuf) - 1] = 0; 19514dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 19524dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1953ea022d16SRodney W. Grimes return (NULL); 1954ea022d16SRodney W. Grimes } 1955ea022d16SRodney W. Grimes data = fileno(file); 1956e5094456SCrist J. Clark conerrno = 0; 1957e5094456SCrist J. Clark if (connect(data, (struct sockaddr *)&data_dest, 1958e5094456SCrist J. Clark data_dest.su_len) == 0) 1959e5094456SCrist J. Clark break; 1960e5094456SCrist J. Clark conerrno = errno; 1961ea022d16SRodney W. Grimes (void) fclose(file); 1962ea022d16SRodney W. Grimes data = -1; 1963e5094456SCrist J. Clark if (conerrno == EADDRINUSE) { 1964e3765043SYaroslav Tykhiy sleep(swaitint); 1965e5094456SCrist J. Clark retry += swaitint; 1966e5094456SCrist J. Clark } else { 1967e5094456SCrist J. Clark break; 1968e5094456SCrist J. Clark } 1969e5094456SCrist J. Clark } while (retry <= swaitmax); 1970e5094456SCrist J. Clark if (conerrno != 0) { 197182c03024SYaroslav Tykhiy reply(425, "Can't build data connection: %s.", 197282c03024SYaroslav Tykhiy strerror(conerrno)); 1973ea022d16SRodney W. Grimes return (NULL); 1974ea022d16SRodney W. Grimes } 1975ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1976ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1977ea022d16SRodney W. Grimes return (file); 1978ea022d16SRodney W. Grimes } 1979ea022d16SRodney W. Grimes 1980ea022d16SRodney W. Grimes /* 1981ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 19829fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1983ea022d16SRodney W. Grimes * 1984ea022d16SRodney W. Grimes * NB: Form isn't handled. 1985ea022d16SRodney W. Grimes */ 19864b82fc95SYaroslav Tykhiy static int 19876e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg) 1988ea022d16SRodney W. Grimes { 1989db1c2da3SYaroslav Tykhiy int c, cp, filefd, netfd; 1990f6f0c4b9SDan Moschuk char *buf; 1991f6f0c4b9SDan Moschuk off_t cnt; 1992ea022d16SRodney W. Grimes 1993ea022d16SRodney W. Grimes transflag++; 1994ea022d16SRodney W. Grimes switch (type) { 1995ea022d16SRodney W. Grimes 1996ea022d16SRodney W. Grimes case TYPE_A: 1997db1c2da3SYaroslav Tykhiy cp = '\0'; 1998ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 19994b82fc95SYaroslav Tykhiy if (recvurg) 20004b82fc95SYaroslav Tykhiy goto got_oob; 2001ea022d16SRodney W. Grimes byte_count++; 2002db1c2da3SYaroslav Tykhiy if (c == '\n' && cp != '\r') { 2003ea022d16SRodney W. Grimes if (ferror(outstr)) 2004ea022d16SRodney W. Grimes goto data_err; 2005ea022d16SRodney W. Grimes (void) putc('\r', outstr); 2006ea022d16SRodney W. Grimes } 2007ea022d16SRodney W. Grimes (void) putc(c, outstr); 2008db1c2da3SYaroslav Tykhiy cp = c; 2009ea022d16SRodney W. Grimes } 20104b82fc95SYaroslav Tykhiy if (recvurg) 20114b82fc95SYaroslav Tykhiy goto got_oob; 2012ea022d16SRodney W. Grimes fflush(outstr); 2013ea022d16SRodney W. Grimes transflag = 0; 2014ea022d16SRodney W. Grimes if (ferror(instr)) 2015ea022d16SRodney W. Grimes goto file_err; 2016ea022d16SRodney W. Grimes if (ferror(outstr)) 2017ea022d16SRodney W. Grimes goto data_err; 2018ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 20194b82fc95SYaroslav Tykhiy return (0); 2020ea022d16SRodney W. Grimes 2021ea022d16SRodney W. Grimes case TYPE_I: 2022ea022d16SRodney W. Grimes case TYPE_L: 20239fc5823aSGarrett Wollman /* 20249fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 20259fc5823aSGarrett Wollman * are sending a regular file 20269fc5823aSGarrett Wollman */ 20279fc5823aSGarrett Wollman netfd = fileno(outstr); 20289fc5823aSGarrett Wollman filefd = fileno(instr); 20299fc5823aSGarrett Wollman 2030f6f0c4b9SDan Moschuk if (isreg) { 20319fc5823aSGarrett Wollman 20322e22b914SYaroslav Tykhiy char *msg = "Transfer complete."; 2033f6f0c4b9SDan Moschuk off_t offset; 2034f6f0c4b9SDan Moschuk int err; 2035f6f0c4b9SDan Moschuk 20362f492fc8SYaroslav Tykhiy cnt = offset = 0; 2037f6f0c4b9SDan Moschuk 20382f492fc8SYaroslav Tykhiy while (filesize > 0) { 2039492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0, 20407e295315SYaroslav Tykhiy NULL, &cnt, 0); 20413af48c42SMaxim Konovalov /* 20423af48c42SMaxim Konovalov * Calculate byte_count before OOB processing. 20433af48c42SMaxim Konovalov * It can be used in myoob() later. 20443af48c42SMaxim Konovalov */ 20453af48c42SMaxim Konovalov byte_count += cnt; 20464b82fc95SYaroslav Tykhiy if (recvurg) 20474b82fc95SYaroslav Tykhiy goto got_oob; 2048f6f0c4b9SDan Moschuk offset += cnt; 2049492f1d9cSMaxim Konovalov filesize -= cnt; 2050f6f0c4b9SDan Moschuk 2051f6f0c4b9SDan Moschuk if (err == -1) { 2052b4585cc1SYaroslav Tykhiy if (cnt == 0 && offset == 0) 2053f6f0c4b9SDan Moschuk goto oldway; 2054f6f0c4b9SDan Moschuk 20559fc5823aSGarrett Wollman goto data_err; 2056f6f0c4b9SDan Moschuk } 20572e22b914SYaroslav Tykhiy 20582e22b914SYaroslav Tykhiy /* 20592e22b914SYaroslav Tykhiy * We hit the EOF prematurely. 20602e22b914SYaroslav Tykhiy * Perhaps the file was externally truncated. 20612e22b914SYaroslav Tykhiy */ 20622e22b914SYaroslav Tykhiy if (cnt == 0) { 20632e22b914SYaroslav Tykhiy msg = "Transfer finished due to " 20642e22b914SYaroslav Tykhiy "premature end of file."; 20652e22b914SYaroslav Tykhiy break; 20662e22b914SYaroslav Tykhiy } 2067f6f0c4b9SDan Moschuk } 2068f6f0c4b9SDan Moschuk 20690849c184SDan Moschuk transflag = 0; 20702e22b914SYaroslav Tykhiy reply(226, msg); 20714b82fc95SYaroslav Tykhiy return (0); 20729fc5823aSGarrett Wollman } 20739fc5823aSGarrett Wollman 20749fc5823aSGarrett Wollman oldway: 2075e3765043SYaroslav Tykhiy if ((buf = malloc(blksize)) == NULL) { 2076ea022d16SRodney W. Grimes transflag = 0; 207702c97492SYaroslav Tykhiy reply(451, "Ran out of memory."); 20784b82fc95SYaroslav Tykhiy return (-1); 2079ea022d16SRodney W. Grimes } 20809fc5823aSGarrett Wollman 2081e3765043SYaroslav Tykhiy while ((cnt = read(filefd, buf, blksize)) > 0 && 2082ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 2083ea022d16SRodney W. Grimes byte_count += cnt; 2084ea022d16SRodney W. Grimes transflag = 0; 2085ea022d16SRodney W. Grimes (void)free(buf); 2086ea022d16SRodney W. Grimes if (cnt != 0) { 2087ea022d16SRodney W. Grimes if (cnt < 0) 2088ea022d16SRodney W. Grimes goto file_err; 2089ea022d16SRodney W. Grimes goto data_err; 2090ea022d16SRodney W. Grimes } 2091ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 20924b82fc95SYaroslav Tykhiy return (0); 2093ea022d16SRodney W. Grimes default: 2094ea022d16SRodney W. Grimes transflag = 0; 209502c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in send_data.", type); 20964b82fc95SYaroslav Tykhiy return (-1); 2097ea022d16SRodney W. Grimes } 2098ea022d16SRodney W. Grimes 2099ea022d16SRodney W. Grimes data_err: 2100ea022d16SRodney W. Grimes transflag = 0; 2101ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 21024b82fc95SYaroslav Tykhiy return (-1); 2103ea022d16SRodney W. Grimes 2104ea022d16SRodney W. Grimes file_err: 2105ea022d16SRodney W. Grimes transflag = 0; 2106ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 21074b82fc95SYaroslav Tykhiy return (-1); 21084b82fc95SYaroslav Tykhiy 21094b82fc95SYaroslav Tykhiy got_oob: 21104b82fc95SYaroslav Tykhiy myoob(); 21114b82fc95SYaroslav Tykhiy recvurg = 0; 21124b82fc95SYaroslav Tykhiy transflag = 0; 21134b82fc95SYaroslav Tykhiy return (-1); 2114ea022d16SRodney W. Grimes } 2115ea022d16SRodney W. Grimes 2116ea022d16SRodney W. Grimes /* 2117ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 2118ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 2119ea022d16SRodney W. Grimes * 2120ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 2121ea022d16SRodney W. Grimes */ 2122ea022d16SRodney W. Grimes static int 2123e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr) 2124ea022d16SRodney W. Grimes { 2125ea022d16SRodney W. Grimes int c; 212661f891a6SPaul Traina int cnt, bare_lfs; 2127ea022d16SRodney W. Grimes char buf[BUFSIZ]; 2128ea022d16SRodney W. Grimes 2129ea022d16SRodney W. Grimes transflag++; 213061f891a6SPaul Traina bare_lfs = 0; 213161f891a6SPaul Traina 2132ea022d16SRodney W. Grimes switch (type) { 2133ea022d16SRodney W. Grimes 2134ea022d16SRodney W. Grimes case TYPE_I: 2135ea022d16SRodney W. Grimes case TYPE_L: 2136ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 21374b82fc95SYaroslav Tykhiy if (recvurg) 21384b82fc95SYaroslav Tykhiy goto got_oob; 2139ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 2140ea022d16SRodney W. Grimes goto file_err; 2141ea022d16SRodney W. Grimes byte_count += cnt; 2142ea022d16SRodney W. Grimes } 21434b82fc95SYaroslav Tykhiy if (recvurg) 21444b82fc95SYaroslav Tykhiy goto got_oob; 2145ea022d16SRodney W. Grimes if (cnt < 0) 2146ea022d16SRodney W. Grimes goto data_err; 2147ea022d16SRodney W. Grimes transflag = 0; 2148ea022d16SRodney W. Grimes return (0); 2149ea022d16SRodney W. Grimes 2150ea022d16SRodney W. Grimes case TYPE_E: 2151ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 2152ea022d16SRodney W. Grimes transflag = 0; 2153ea022d16SRodney W. Grimes return (-1); 2154ea022d16SRodney W. Grimes 2155ea022d16SRodney W. Grimes case TYPE_A: 2156ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 21574b82fc95SYaroslav Tykhiy if (recvurg) 21584b82fc95SYaroslav Tykhiy goto got_oob; 2159ea022d16SRodney W. Grimes byte_count++; 2160ea022d16SRodney W. Grimes if (c == '\n') 2161ea022d16SRodney W. Grimes bare_lfs++; 2162ea022d16SRodney W. Grimes while (c == '\r') { 2163ea022d16SRodney W. Grimes if (ferror(outstr)) 2164ea022d16SRodney W. Grimes goto data_err; 2165ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 2166ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 2167ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 2168ea022d16SRodney W. Grimes goto contin2; 2169ea022d16SRodney W. Grimes } 2170ea022d16SRodney W. Grimes } 2171ea022d16SRodney W. Grimes (void) putc(c, outstr); 2172ea022d16SRodney W. Grimes contin2: ; 2173ea022d16SRodney W. Grimes } 21744b82fc95SYaroslav Tykhiy if (recvurg) 21754b82fc95SYaroslav Tykhiy goto got_oob; 2176ea022d16SRodney W. Grimes fflush(outstr); 2177ea022d16SRodney W. Grimes if (ferror(instr)) 2178ea022d16SRodney W. Grimes goto data_err; 2179ea022d16SRodney W. Grimes if (ferror(outstr)) 2180ea022d16SRodney W. Grimes goto file_err; 2181ea022d16SRodney W. Grimes transflag = 0; 2182ea022d16SRodney W. Grimes if (bare_lfs) { 2183ea022d16SRodney W. Grimes lreply(226, 218402c97492SYaroslav Tykhiy "WARNING! %d bare linefeeds received in ASCII mode.", 2185ea022d16SRodney W. Grimes bare_lfs); 2186ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 2187ea022d16SRodney W. Grimes } 2188ea022d16SRodney W. Grimes return (0); 2189ea022d16SRodney W. Grimes default: 219002c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in receive_data.", type); 2191ea022d16SRodney W. Grimes transflag = 0; 2192ea022d16SRodney W. Grimes return (-1); 2193ea022d16SRodney W. Grimes } 2194ea022d16SRodney W. Grimes 2195ea022d16SRodney W. Grimes data_err: 2196ea022d16SRodney W. Grimes transflag = 0; 219702c97492SYaroslav Tykhiy perror_reply(426, "Data connection"); 2198ea022d16SRodney W. Grimes return (-1); 2199ea022d16SRodney W. Grimes 2200ea022d16SRodney W. Grimes file_err: 2201ea022d16SRodney W. Grimes transflag = 0; 220202c97492SYaroslav Tykhiy perror_reply(452, "Error writing to file"); 2203ea022d16SRodney W. Grimes return (-1); 22044b82fc95SYaroslav Tykhiy 22054b82fc95SYaroslav Tykhiy got_oob: 22064b82fc95SYaroslav Tykhiy myoob(); 22074b82fc95SYaroslav Tykhiy recvurg = 0; 22084b82fc95SYaroslav Tykhiy transflag = 0; 22094b82fc95SYaroslav Tykhiy return (-1); 2210ea022d16SRodney W. Grimes } 2211ea022d16SRodney W. Grimes 2212ea022d16SRodney W. Grimes void 2213e4bc453cSWarner Losh statfilecmd(char *filename) 2214ea022d16SRodney W. Grimes { 2215ea022d16SRodney W. Grimes FILE *fin; 2216f8a581a0SYaroslav Tykhiy int atstart; 221741c57b48SYaroslav Tykhiy int c, code; 2218ea022d16SRodney W. Grimes char line[LINE_MAX]; 221941c57b48SYaroslav Tykhiy struct stat st; 2220ea022d16SRodney W. Grimes 222141c57b48SYaroslav Tykhiy code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213; 2222af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2223ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 22243b48b877SYaroslav Tykhiy lreply(code, "Status of %s:", filename); 2225f8a581a0SYaroslav Tykhiy atstart = 1; 2226ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 2227ea022d16SRodney W. Grimes if (c == '\n') { 2228ea022d16SRodney W. Grimes if (ferror(stdout)){ 222902c97492SYaroslav Tykhiy perror_reply(421, "Control connection"); 2230ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2231ea022d16SRodney W. Grimes dologout(1); 2232ea022d16SRodney W. Grimes /* NOTREACHED */ 2233ea022d16SRodney W. Grimes } 2234ea022d16SRodney W. Grimes if (ferror(fin)) { 2235ea022d16SRodney W. Grimes perror_reply(551, filename); 2236ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2237ea022d16SRodney W. Grimes return; 2238ea022d16SRodney W. Grimes } 2239ea022d16SRodney W. Grimes (void) putc('\r', stdout); 2240ea022d16SRodney W. Grimes } 2241f8a581a0SYaroslav Tykhiy /* 2242f8a581a0SYaroslav Tykhiy * RFC 959 says neutral text should be prepended before 2243f8a581a0SYaroslav Tykhiy * a leading 3-digit number followed by whitespace, but 2244f8a581a0SYaroslav Tykhiy * many ftp clients can be confused by any leading digits, 2245f8a581a0SYaroslav Tykhiy * as a matter of fact. 2246f8a581a0SYaroslav Tykhiy */ 2247f8a581a0SYaroslav Tykhiy if (atstart && isdigit(c)) 2248f8a581a0SYaroslav Tykhiy (void) putc(' ', stdout); 2249ea022d16SRodney W. Grimes (void) putc(c, stdout); 2250f8a581a0SYaroslav Tykhiy atstart = (c == '\n'); 2251ea022d16SRodney W. Grimes } 2252ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 225302c97492SYaroslav Tykhiy reply(code, "End of status."); 2254ea022d16SRodney W. Grimes } 2255ea022d16SRodney W. Grimes 2256ea022d16SRodney W. Grimes void 2257e4bc453cSWarner Losh statcmd(void) 2258ea022d16SRodney W. Grimes { 22594dd8b5abSYoshinobu Inoue union sockunion *su; 2260ea022d16SRodney W. Grimes u_char *a, *p; 2261b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST]; 22624dd8b5abSYoshinobu Inoue int ispassive; 2263ea022d16SRodney W. Grimes 2264c152df28SYaroslav Tykhiy if (hostinfo) { 2265a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname); 2266ea022d16SRodney W. Grimes printf(" %s\r\n", version); 2267c152df28SYaroslav Tykhiy } else 2268c152df28SYaroslav Tykhiy lreply(211, "FTP server status:"); 2269ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 22704dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2271b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 227204683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0; 22734dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 22744dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 22754dd8b5abSYoshinobu Inoue } 2276ea022d16SRodney W. Grimes printf("\r\n"); 2277ea022d16SRodney W. Grimes if (logged_in) { 2278ea022d16SRodney W. Grimes if (guest) 2279ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 2280ea022d16SRodney W. Grimes else 2281ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 2282ea022d16SRodney W. Grimes } else if (askpasswd) 2283ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 2284ea022d16SRodney W. Grimes else 2285ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 2286ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 2287ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 2288ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2289ea022d16SRodney W. Grimes if (type == TYPE_L) 229089fdc4e1SMike Barcroft #if CHAR_BIT == 8 229189fdc4e1SMike Barcroft printf(" %d", CHAR_BIT); 2292ea022d16SRodney W. Grimes #else 2293ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2294ea022d16SRodney W. Grimes #endif 2295ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2296ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2297ea022d16SRodney W. Grimes if (data != -1) 2298ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2299ea022d16SRodney W. Grimes else if (pdata != -1) { 23004dd8b5abSYoshinobu Inoue ispassive = 1; 23014dd8b5abSYoshinobu Inoue su = &pasv_addr; 2302ea022d16SRodney W. Grimes goto printaddr; 2303ea022d16SRodney W. Grimes } else if (usedefault == 0) { 23044dd8b5abSYoshinobu Inoue ispassive = 0; 23054dd8b5abSYoshinobu Inoue su = &data_dest; 2306ea022d16SRodney W. Grimes printaddr: 2307ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 23084dd8b5abSYoshinobu Inoue if (epsvall) { 23094dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 23104dd8b5abSYoshinobu Inoue goto epsvonly; 23114dd8b5abSYoshinobu Inoue } 23124dd8b5abSYoshinobu Inoue 23134dd8b5abSYoshinobu Inoue /* PORT/PASV */ 23144dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 23154dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 23164dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 23174dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 23184dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 23194dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 23204dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 23214dd8b5abSYoshinobu Inoue } 23224dd8b5abSYoshinobu Inoue 23234dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 23244dd8b5abSYoshinobu Inoue { 23254dd8b5abSYoshinobu Inoue int alen, af, i; 23264dd8b5abSYoshinobu Inoue 23274dd8b5abSYoshinobu Inoue switch (su->su_family) { 23284dd8b5abSYoshinobu Inoue case AF_INET: 23294dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 23304dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 23314dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 23324dd8b5abSYoshinobu Inoue af = 4; 23334dd8b5abSYoshinobu Inoue break; 23344dd8b5abSYoshinobu Inoue case AF_INET6: 23354dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 23364dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 23374dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 23384dd8b5abSYoshinobu Inoue af = 6; 23394dd8b5abSYoshinobu Inoue break; 23404dd8b5abSYoshinobu Inoue default: 23414dd8b5abSYoshinobu Inoue af = 0; 23424dd8b5abSYoshinobu Inoue break; 23434dd8b5abSYoshinobu Inoue } 23444dd8b5abSYoshinobu Inoue if (af) { 23454dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 23464dd8b5abSYoshinobu Inoue af, alen); 23474dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 23484dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 23494dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 23504dd8b5abSYoshinobu Inoue } 23514dd8b5abSYoshinobu Inoue } 23524dd8b5abSYoshinobu Inoue 23534dd8b5abSYoshinobu Inoue epsvonly:; 23544dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 23554dd8b5abSYoshinobu Inoue { 23564dd8b5abSYoshinobu Inoue int af; 23574dd8b5abSYoshinobu Inoue 23584dd8b5abSYoshinobu Inoue switch (su->su_family) { 23594dd8b5abSYoshinobu Inoue case AF_INET: 23604dd8b5abSYoshinobu Inoue af = 1; 23614dd8b5abSYoshinobu Inoue break; 23624dd8b5abSYoshinobu Inoue case AF_INET6: 23634dd8b5abSYoshinobu Inoue af = 2; 23644dd8b5abSYoshinobu Inoue break; 23654dd8b5abSYoshinobu Inoue default: 23664dd8b5abSYoshinobu Inoue af = 0; 23674dd8b5abSYoshinobu Inoue break; 23684dd8b5abSYoshinobu Inoue } 23694dd8b5abSYoshinobu Inoue if (af) { 2370b0f06defSHajimu UMEMOTO union sockunion tmp; 2371b0f06defSHajimu UMEMOTO 2372b0f06defSHajimu UMEMOTO tmp = *su; 2373b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6) 2374b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0; 2375b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 23764dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 23774dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 237804683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0; 23794dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 23804dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 2381b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port)); 23824dd8b5abSYoshinobu Inoue } 23834dd8b5abSYoshinobu Inoue } 23844dd8b5abSYoshinobu Inoue } 2385ea022d16SRodney W. Grimes #undef UC 2386ea022d16SRodney W. Grimes } else 2387ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 238802c97492SYaroslav Tykhiy reply(211, "End of status."); 2389ea022d16SRodney W. Grimes } 2390ea022d16SRodney W. Grimes 2391ea022d16SRodney W. Grimes void 2392e4bc453cSWarner Losh fatalerror(char *s) 2393ea022d16SRodney W. Grimes { 2394ea022d16SRodney W. Grimes 23954a3e5acdSYaroslav Tykhiy reply(451, "Error in server: %s", s); 2396ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2397ea022d16SRodney W. Grimes dologout(0); 2398ea022d16SRodney W. Grimes /* NOTREACHED */ 2399ea022d16SRodney W. Grimes } 2400ea022d16SRodney W. Grimes 2401ea022d16SRodney W. Grimes void 2402ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2403ea022d16SRodney W. Grimes { 2404ea022d16SRodney W. Grimes va_list ap; 2405e4bc453cSWarner Losh 2406ea022d16SRodney W. Grimes (void)printf("%d ", n); 24079cbb335cSTim J. Robbins va_start(ap, fmt); 2408ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 24099cbb335cSTim J. Robbins va_end(ap); 2410ea022d16SRodney W. Grimes (void)printf("\r\n"); 2411ea022d16SRodney W. Grimes (void)fflush(stdout); 2412618b0bbaSMark Murray if (ftpdebug) { 2413ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 24149cbb335cSTim J. Robbins va_start(ap, fmt); 2415ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 24169cbb335cSTim J. Robbins va_end(ap); 2417ea022d16SRodney W. Grimes } 2418ea022d16SRodney W. Grimes } 2419ea022d16SRodney W. Grimes 2420ea022d16SRodney W. Grimes void 2421ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2422ea022d16SRodney W. Grimes { 2423ea022d16SRodney W. Grimes va_list ap; 2424e4bc453cSWarner Losh 2425ea022d16SRodney W. Grimes (void)printf("%d- ", n); 24269cbb335cSTim J. Robbins va_start(ap, fmt); 2427ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 24289cbb335cSTim J. Robbins va_end(ap); 2429ea022d16SRodney W. Grimes (void)printf("\r\n"); 2430ea022d16SRodney W. Grimes (void)fflush(stdout); 2431618b0bbaSMark Murray if (ftpdebug) { 2432ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 24339cbb335cSTim J. Robbins va_start(ap, fmt); 2434ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 24359cbb335cSTim J. Robbins va_end(ap); 2436ea022d16SRodney W. Grimes } 2437ea022d16SRodney W. Grimes } 2438ea022d16SRodney W. Grimes 2439ea022d16SRodney W. Grimes static void 2440e4bc453cSWarner Losh ack(char *s) 2441ea022d16SRodney W. Grimes { 2442ea022d16SRodney W. Grimes 2443ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2444ea022d16SRodney W. Grimes } 2445ea022d16SRodney W. Grimes 2446ea022d16SRodney W. Grimes void 2447e4bc453cSWarner Losh nack(char *s) 2448ea022d16SRodney W. Grimes { 2449ea022d16SRodney W. Grimes 2450ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2451ea022d16SRodney W. Grimes } 2452ea022d16SRodney W. Grimes 2453ea022d16SRodney W. Grimes /* ARGSUSED */ 2454ea022d16SRodney W. Grimes void 2455e4bc453cSWarner Losh yyerror(char *s) 2456ea022d16SRodney W. Grimes { 2457ea022d16SRodney W. Grimes char *cp; 2458ea022d16SRodney W. Grimes 24599aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2460ea022d16SRodney W. Grimes *cp = '\0'; 246102c97492SYaroslav Tykhiy reply(500, "%s: command not understood.", cbuf); 2462ea022d16SRodney W. Grimes } 2463ea022d16SRodney W. Grimes 2464ea022d16SRodney W. Grimes void 2465e4bc453cSWarner Losh delete(char *name) 2466ea022d16SRodney W. Grimes { 2467ea022d16SRodney W. Grimes struct stat st; 2468ea022d16SRodney W. Grimes 2469ea022d16SRodney W. Grimes LOGCMD("delete", name); 24701b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2471ea022d16SRodney W. Grimes perror_reply(550, name); 2472ea022d16SRodney W. Grimes return; 2473ea022d16SRodney W. Grimes } 2474ac4f2391SYaroslav Tykhiy if (S_ISDIR(st.st_mode)) { 2475ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2476ea022d16SRodney W. Grimes perror_reply(550, name); 2477ea022d16SRodney W. Grimes return; 2478ea022d16SRodney W. Grimes } 2479ea022d16SRodney W. Grimes goto done; 2480ea022d16SRodney W. Grimes } 24813f8b9cfeSYaroslav Tykhiy if (guest && noguestmod) { 248202c97492SYaroslav Tykhiy reply(550, "Operation not permitted."); 24833f8b9cfeSYaroslav Tykhiy return; 24843f8b9cfeSYaroslav Tykhiy } 24853f8b9cfeSYaroslav Tykhiy if (unlink(name) < 0) { 2486ea022d16SRodney W. Grimes perror_reply(550, name); 2487ea022d16SRodney W. Grimes return; 2488ea022d16SRodney W. Grimes } 2489ea022d16SRodney W. Grimes done: 2490ea022d16SRodney W. Grimes ack("DELE"); 2491ea022d16SRodney W. Grimes } 2492ea022d16SRodney W. Grimes 2493ea022d16SRodney W. Grimes void 2494e4bc453cSWarner Losh cwd(char *path) 2495ea022d16SRodney W. Grimes { 2496ea022d16SRodney W. Grimes 2497ea022d16SRodney W. Grimes if (chdir(path) < 0) 2498ea022d16SRodney W. Grimes perror_reply(550, path); 2499ea022d16SRodney W. Grimes else 2500ea022d16SRodney W. Grimes ack("CWD"); 2501ea022d16SRodney W. Grimes } 2502ea022d16SRodney W. Grimes 2503ea022d16SRodney W. Grimes void 2504e4bc453cSWarner Losh makedir(char *name) 2505ea022d16SRodney W. Grimes { 25062b748987SYaroslav Tykhiy char *s; 2507ea022d16SRodney W. Grimes 2508ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2509d186bb12SMatthew N. Dodd if (guest && noguestmkd) 2510405e2987SYaroslav Tykhiy reply(550, "Operation not permitted."); 2511d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0) 2512ea022d16SRodney W. Grimes perror_reply(550, name); 25132b748987SYaroslav Tykhiy else { 25142b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL) 25152b748987SYaroslav Tykhiy fatalerror("Ran out of memory."); 25162b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s); 25172b748987SYaroslav Tykhiy free(s); 25182b748987SYaroslav Tykhiy } 2519ea022d16SRodney W. Grimes } 2520ea022d16SRodney W. Grimes 2521ea022d16SRodney W. Grimes void 2522e4bc453cSWarner Losh removedir(char *name) 2523ea022d16SRodney W. Grimes { 2524ea022d16SRodney W. Grimes 2525ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2526ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2527ea022d16SRodney W. Grimes perror_reply(550, name); 2528ea022d16SRodney W. Grimes else 2529ea022d16SRodney W. Grimes ack("RMD"); 2530ea022d16SRodney W. Grimes } 2531ea022d16SRodney W. Grimes 2532ea022d16SRodney W. Grimes void 2533e4bc453cSWarner Losh pwd(void) 2534ea022d16SRodney W. Grimes { 2535e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1]; 2536ea022d16SRodney W. Grimes 2537de9b6c03SYaroslav Tykhiy if (getcwd(path, sizeof(path)) == NULL) 253875933089SYaroslav Tykhiy perror_reply(550, "Get current directory"); 2539e4648f05SYaroslav Tykhiy else { 2540e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL) 2541e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory."); 2542e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s); 2543e4648f05SYaroslav Tykhiy free(s); 2544e4648f05SYaroslav Tykhiy } 2545ea022d16SRodney W. Grimes } 2546ea022d16SRodney W. Grimes 2547ea022d16SRodney W. Grimes char * 2548e4bc453cSWarner Losh renamefrom(char *name) 2549ea022d16SRodney W. Grimes { 2550ea022d16SRodney W. Grimes struct stat st; 2551ea022d16SRodney W. Grimes 2552b943b3c4SYaroslav Tykhiy if (guest && noguestmod) { 255302c97492SYaroslav Tykhiy reply(550, "Operation not permitted."); 2554b943b3c4SYaroslav Tykhiy return (NULL); 2555b943b3c4SYaroslav Tykhiy } 25561b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2557ea022d16SRodney W. Grimes perror_reply(550, name); 2558385f9bf0SYaroslav Tykhiy return (NULL); 2559ea022d16SRodney W. Grimes } 256002c97492SYaroslav Tykhiy reply(350, "File exists, ready for destination name."); 2561ea022d16SRodney W. Grimes return (name); 2562ea022d16SRodney W. Grimes } 2563ea022d16SRodney W. Grimes 2564ea022d16SRodney W. Grimes void 2565e4bc453cSWarner Losh renamecmd(char *from, char *to) 2566ea022d16SRodney W. Grimes { 256761f891a6SPaul Traina struct stat st; 2568ea022d16SRodney W. Grimes 2569ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 257061f891a6SPaul Traina 257161f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 257202c97492SYaroslav Tykhiy reply(550, "%s: permission denied.", to); 257361f891a6SPaul Traina return; 257461f891a6SPaul Traina } 257561f891a6SPaul Traina 2576ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2577ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2578ea022d16SRodney W. Grimes else 2579ea022d16SRodney W. Grimes ack("RNTO"); 2580ea022d16SRodney W. Grimes } 2581ea022d16SRodney W. Grimes 2582ea022d16SRodney W. Grimes static void 2583e4bc453cSWarner Losh dolog(struct sockaddr *who) 2584ea022d16SRodney W. Grimes { 25857cdd3cb7SYaroslav Tykhiy char who_name[NI_MAXHOST]; 25864dd8b5abSYoshinobu Inoue 25874dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 258804683b2cSYaroslav Tykhiy remotehost[sizeof(remotehost) - 1] = 0; 25897cdd3cb7SYaroslav Tykhiy if (getnameinfo(who, who->sa_len, 25907cdd3cb7SYaroslav Tykhiy who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST)) 25917cdd3cb7SYaroslav Tykhiy *who_name = 0; 25927cdd3cb7SYaroslav Tykhiy who_name[sizeof(who_name) - 1] = 0; 2593ea022d16SRodney W. Grimes 2594ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2595ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2596ea4e54b9SDavid Nugent if (thishost != firsthost) 2597ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2598ea4e54b9SDavid Nugent remotehost, hostname); 2599ea4e54b9SDavid Nugent else 2600ea4e54b9SDavid Nugent #endif 2601ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2602ea4e54b9SDavid Nugent remotehost); 2603b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2604ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2605ea022d16SRodney W. Grimes 2606ea4e54b9SDavid Nugent if (logging) { 2607ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2608ea4e54b9SDavid Nugent if (thishost != firsthost) 26097cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s) to %s", 26107cdd3cb7SYaroslav Tykhiy remotehost, who_name, hostname); 2611ea4e54b9SDavid Nugent else 2612ea4e54b9SDavid Nugent #endif 26137cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s)", 26147cdd3cb7SYaroslav Tykhiy remotehost, who_name); 2615ea022d16SRodney W. Grimes } 2616ea4e54b9SDavid Nugent } 2617ea022d16SRodney W. Grimes 2618ea022d16SRodney W. Grimes /* 2619ea022d16SRodney W. Grimes * Record logout in wtmp file 2620ea022d16SRodney W. Grimes * and exit with supplied status. 2621ea022d16SRodney W. Grimes */ 2622ea022d16SRodney W. Grimes void 2623e4bc453cSWarner Losh dologout(int status) 2624ea022d16SRodney W. Grimes { 26250b4df2eeSDavid Greenman /* 26260b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 26270b4df2eeSDavid Greenman * back to the main program loop. 26280b4df2eeSDavid Greenman */ 26290b4df2eeSDavid Greenman transflag = 0; 2630ea022d16SRodney W. Grimes 26315d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) { 263252e7ee74SYaroslav Tykhiy (void) seteuid(0); 263346948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 2634ea022d16SRodney W. Grimes } 2635ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2636ea022d16SRodney W. Grimes _exit(status); 2637ea022d16SRodney W. Grimes } 2638ea022d16SRodney W. Grimes 2639ea022d16SRodney W. Grimes static void 2640e4bc453cSWarner Losh sigurg(int signo) 2641ea022d16SRodney W. Grimes { 26424b82fc95SYaroslav Tykhiy 26434b82fc95SYaroslav Tykhiy recvurg = 1; 26444b82fc95SYaroslav Tykhiy } 26454b82fc95SYaroslav Tykhiy 26464b82fc95SYaroslav Tykhiy static void 2647e4bc453cSWarner Losh myoob(void) 26484b82fc95SYaroslav Tykhiy { 2649ea022d16SRodney W. Grimes char *cp; 2650ea022d16SRodney W. Grimes 2651ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2652ea022d16SRodney W. Grimes if (!transflag) 2653ea022d16SRodney W. Grimes return; 2654ea022d16SRodney W. Grimes cp = tmpline; 2655ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2656ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2657ea022d16SRodney W. Grimes dologout(0); 2658ea022d16SRodney W. Grimes } 2659ea022d16SRodney W. Grimes upper(cp); 2660ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2661ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2662ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 266302c97492SYaroslav Tykhiy reply(226, "Abort successful."); 2664ea022d16SRodney W. Grimes } 2665ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 26669db4bbf3SMichael Haro tmpline[0] = '\0'; 26670e519c96SYaroslav Tykhiy if (file_size != -1) 266802c97492SYaroslav Tykhiy reply(213, "Status: %jd of %jd bytes transferred.", 2669a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count, (intmax_t)file_size); 2670ea022d16SRodney W. Grimes else 267102c97492SYaroslav Tykhiy reply(213, "Status: %jd bytes transferred.", 2672a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count); 2673ea022d16SRodney W. Grimes } 2674ea022d16SRodney W. Grimes } 2675ea022d16SRodney W. Grimes 2676ea022d16SRodney W. Grimes /* 2677ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2678ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2679ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2680ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2681ea022d16SRodney W. Grimes */ 2682ea022d16SRodney W. Grimes void 2683e4bc453cSWarner Losh passive(void) 2684ea022d16SRodney W. Grimes { 26858af7c9a3SYaroslav Tykhiy int len, on; 2686ea022d16SRodney W. Grimes char *p, *a; 2687ea022d16SRodney W. Grimes 268861f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 268961f891a6SPaul Traina close(pdata); 269061f891a6SPaul Traina 26914dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2692ea022d16SRodney W. Grimes if (pdata < 0) { 2693ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2694ea022d16SRodney W. Grimes return; 2695ea022d16SRodney W. Grimes } 26968af7c9a3SYaroslav Tykhiy on = 1; 26978af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 26988af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 26994c450ad7SPaul Traina 270052e7ee74SYaroslav Tykhiy (void) seteuid(0); 270161f891a6SPaul Traina 2702dacc9752SPaul Traina #ifdef IP_PORTRANGE 27034dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 27048af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 2705dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2706dacc9752SPaul Traina 270740e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 270857d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 27094c450ad7SPaul Traina goto pasv_error; 27104c450ad7SPaul Traina } 2711dacc9752SPaul Traina #endif 27122db39860SNick Sayer #ifdef IPV6_PORTRANGE 27132db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 27148af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 27152db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 27162db39860SNick Sayer 27172db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 271857d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 27192db39860SNick Sayer goto pasv_error; 27202db39860SNick Sayer } 27212db39860SNick Sayer #endif 272240e9d39eSPeter Wemm 2723ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 27244dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 27254dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2726ea022d16SRodney W. Grimes goto pasv_error; 272761f891a6SPaul Traina 272852e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 27294c450ad7SPaul Traina 2730ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2731ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2732ea022d16SRodney W. Grimes goto pasv_error; 2733ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2734ea022d16SRodney W. Grimes goto pasv_error; 27354dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 27364dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 27374dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 27384dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 27394dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 27404dd8b5abSYoshinobu Inoue else 27414dd8b5abSYoshinobu Inoue goto pasv_error; 27424dd8b5abSYoshinobu Inoue 27434dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2744ea022d16SRodney W. Grimes 2745ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2746ea022d16SRodney W. Grimes 2747ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2748ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2749ea022d16SRodney W. Grimes return; 2750ea022d16SRodney W. Grimes 2751ea022d16SRodney W. Grimes pasv_error: 275252e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 2753ea022d16SRodney W. Grimes (void) close(pdata); 2754ea022d16SRodney W. Grimes pdata = -1; 2755ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2756ea022d16SRodney W. Grimes return; 2757ea022d16SRodney W. Grimes } 2758ea022d16SRodney W. Grimes 2759ea022d16SRodney W. Grimes /* 27604dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 27614dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 27624dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 27634dd8b5abSYoshinobu Inoue */ 27644dd8b5abSYoshinobu Inoue 27654dd8b5abSYoshinobu Inoue void 2766e4bc453cSWarner Losh long_passive(char *cmd, int pf) 27674dd8b5abSYoshinobu Inoue { 27688af7c9a3SYaroslav Tykhiy int len, on; 27694dd8b5abSYoshinobu Inoue char *p, *a; 27704dd8b5abSYoshinobu Inoue 27714dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 27724dd8b5abSYoshinobu Inoue close(pdata); 27734dd8b5abSYoshinobu Inoue 27744dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 27754dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 27764dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 27774dd8b5abSYoshinobu Inoue case AF_INET: 27784dd8b5abSYoshinobu Inoue pf = 1; 27794dd8b5abSYoshinobu Inoue break; 27804dd8b5abSYoshinobu Inoue case AF_INET6: 27814dd8b5abSYoshinobu Inoue pf = 2; 27824dd8b5abSYoshinobu Inoue break; 27834dd8b5abSYoshinobu Inoue default: 27844dd8b5abSYoshinobu Inoue pf = 0; 27854dd8b5abSYoshinobu Inoue break; 27864dd8b5abSYoshinobu Inoue } 27874dd8b5abSYoshinobu Inoue /* 27884dd8b5abSYoshinobu Inoue * XXX 27894dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 27904dd8b5abSYoshinobu Inoue */ 27914dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 27924dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 27934dd8b5abSYoshinobu Inoue "use (%d)", pf); 27944dd8b5abSYoshinobu Inoue } else 279502c97492SYaroslav Tykhiy reply(501, "Network protocol mismatch."); /*XXX*/ 27964dd8b5abSYoshinobu Inoue 27974dd8b5abSYoshinobu Inoue return; 27984dd8b5abSYoshinobu Inoue } 27994dd8b5abSYoshinobu Inoue } 28004dd8b5abSYoshinobu Inoue 28014dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 28024dd8b5abSYoshinobu Inoue if (pdata < 0) { 28034dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 28044dd8b5abSYoshinobu Inoue return; 28054dd8b5abSYoshinobu Inoue } 28068af7c9a3SYaroslav Tykhiy on = 1; 28078af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 28088af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 28094dd8b5abSYoshinobu Inoue 281052e7ee74SYaroslav Tykhiy (void) seteuid(0); 28114dd8b5abSYoshinobu Inoue 28124dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 28134dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 28144dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 28154dd8b5abSYoshinobu Inoue 28162db39860SNick Sayer #ifdef IP_PORTRANGE 28172db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 28188af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 28192db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 28202db39860SNick Sayer 28212db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 282257d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 28232db39860SNick Sayer goto pasv_error; 28242db39860SNick Sayer } 28252db39860SNick Sayer #endif 28262db39860SNick Sayer #ifdef IPV6_PORTRANGE 28272db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 28288af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 28292db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 28302db39860SNick Sayer 28312db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 283257d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 28332db39860SNick Sayer goto pasv_error; 28342db39860SNick Sayer } 28352db39860SNick Sayer #endif 28362db39860SNick Sayer 28374dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 28384dd8b5abSYoshinobu Inoue goto pasv_error; 28394dd8b5abSYoshinobu Inoue 284052e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 28414dd8b5abSYoshinobu Inoue 28424dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 28434dd8b5abSYoshinobu Inoue goto pasv_error; 28444dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 28454dd8b5abSYoshinobu Inoue goto pasv_error; 28464dd8b5abSYoshinobu Inoue 28474dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 28484dd8b5abSYoshinobu Inoue 28494dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 28504dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 28514dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 28524dd8b5abSYoshinobu Inoue case AF_INET: 28534dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 28544dd8b5abSYoshinobu Inoue v4_reply: 28554dd8b5abSYoshinobu Inoue reply(228, 28564dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 28574dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 28584dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 28594dd8b5abSYoshinobu Inoue return; 28604dd8b5abSYoshinobu Inoue case AF_INET6: 28614dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 28624dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 28634dd8b5abSYoshinobu Inoue goto v4_reply; 28644dd8b5abSYoshinobu Inoue } 28654dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 28664dd8b5abSYoshinobu Inoue reply(228, 28674dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 28684dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 28694dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 28704dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 28714dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 28724dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 28734dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 28744dd8b5abSYoshinobu Inoue return; 28754dd8b5abSYoshinobu Inoue } 28764dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 28774dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 28784dd8b5abSYoshinobu Inoue case AF_INET: 28794dd8b5abSYoshinobu Inoue case AF_INET6: 28804dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 28814dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 28824dd8b5abSYoshinobu Inoue return; 28834dd8b5abSYoshinobu Inoue } 28844dd8b5abSYoshinobu Inoue } else { 28854dd8b5abSYoshinobu Inoue /* more proper error code? */ 28864dd8b5abSYoshinobu Inoue } 28874dd8b5abSYoshinobu Inoue 28884dd8b5abSYoshinobu Inoue pasv_error: 288952e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid); 28904dd8b5abSYoshinobu Inoue (void) close(pdata); 28914dd8b5abSYoshinobu Inoue pdata = -1; 28924dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 28934dd8b5abSYoshinobu Inoue return; 28944dd8b5abSYoshinobu Inoue } 28954dd8b5abSYoshinobu Inoue 28964dd8b5abSYoshinobu Inoue /* 2897a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local" 2898a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races. 2899a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 2900a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name. 2901a117c345SYaroslav Tykhiy * 2902ea022d16SRodney W. Grimes * Generates failure reply on error. 2903ea022d16SRodney W. Grimes */ 2904a117c345SYaroslav Tykhiy static int 2905a117c345SYaroslav Tykhiy guniquefd(char *local, char **name) 2906ea022d16SRodney W. Grimes { 2907ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2908ea022d16SRodney W. Grimes struct stat st; 2909ea022d16SRodney W. Grimes char *cp; 2910a117c345SYaroslav Tykhiy int count; 2911a117c345SYaroslav Tykhiy int fd; 2912ea022d16SRodney W. Grimes 2913ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2914ea022d16SRodney W. Grimes if (cp) 2915ea022d16SRodney W. Grimes *cp = '\0'; 2916ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2917ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2918a117c345SYaroslav Tykhiy return (-1); 2919ea022d16SRodney W. Grimes } 2920a117c345SYaroslav Tykhiy if (cp) { 2921a117c345SYaroslav Tykhiy /* 2922a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix. 2923a117c345SYaroslav Tykhiy * -4 is for /nn\0 2924a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix. 2925a117c345SYaroslav Tykhiy */ 2926a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) { 292702c97492SYaroslav Tykhiy reply(553, "Pathname too long."); 2928a117c345SYaroslav Tykhiy return (-1); 2929a117c345SYaroslav Tykhiy } 2930ea022d16SRodney W. Grimes *cp = '/'; 2931a117c345SYaroslav Tykhiy } 2932e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2933e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2934ea022d16SRodney W. Grimes cp = new + strlen(new); 2935a117c345SYaroslav Tykhiy /* 2936a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly. 2937a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off 2938a117c345SYaroslav Tykhiy * by buffer size. 2939a117c345SYaroslav Tykhiy */ 2940a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/') 2941ea022d16SRodney W. Grimes *cp++ = '.'; 2942a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) { 2943a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */ 2944a117c345SYaroslav Tykhiy if (count) 2945ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2946a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local, 2947a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 2948a117c345SYaroslav Tykhiy *name = count ? new : local; 2949a117c345SYaroslav Tykhiy return (fd); 2950a117c345SYaroslav Tykhiy } 295188b70721SYaroslav Tykhiy if (errno != EEXIST) { 295250618d61SYaroslav Tykhiy perror_reply(553, count ? new : local); 295388b70721SYaroslav Tykhiy return (-1); 295488b70721SYaroslav Tykhiy } 2955ea022d16SRodney W. Grimes } 2956ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2957a117c345SYaroslav Tykhiy return (-1); 2958ea022d16SRodney W. Grimes } 2959ea022d16SRodney W. Grimes 2960ea022d16SRodney W. Grimes /* 2961ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2962ea022d16SRodney W. Grimes */ 2963ea022d16SRodney W. Grimes void 2964e4bc453cSWarner Losh perror_reply(int code, char *string) 2965ea022d16SRodney W. Grimes { 2966ea022d16SRodney W. Grimes 2967ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2968ea022d16SRodney W. Grimes } 2969ea022d16SRodney W. Grimes 2970ea022d16SRodney W. Grimes static char *onefile[] = { 2971ea022d16SRodney W. Grimes "", 2972ea022d16SRodney W. Grimes 0 2973ea022d16SRodney W. Grimes }; 2974ea022d16SRodney W. Grimes 2975ea022d16SRodney W. Grimes void 2976e4bc453cSWarner Losh send_file_list(char *whichf) 2977ea022d16SRodney W. Grimes { 2978ea022d16SRodney W. Grimes struct stat st; 2979ea022d16SRodney W. Grimes DIR *dirp = NULL; 2980ea022d16SRodney W. Grimes struct dirent *dir; 2981ea022d16SRodney W. Grimes FILE *dout = NULL; 2982ea022d16SRodney W. Grimes char **dirlist, *dirname; 2983ea022d16SRodney W. Grimes int simple = 0; 2984ea022d16SRodney W. Grimes int freeglob = 0; 2985ea022d16SRodney W. Grimes glob_t gl; 2986ea022d16SRodney W. Grimes 2987ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 298812da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 2989ea022d16SRodney W. Grimes 2990ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 29916d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 299275dc5f1aSMike Heffner flags |= GLOB_LIMIT; 2993ea022d16SRodney W. Grimes freeglob = 1; 2994ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 299502c97492SYaroslav Tykhiy reply(550, "No matching files found."); 2996ea022d16SRodney W. Grimes goto out; 2997ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2998ea022d16SRodney W. Grimes errno = ENOENT; 2999ea022d16SRodney W. Grimes perror_reply(550, whichf); 3000ea022d16SRodney W. Grimes goto out; 3001ea022d16SRodney W. Grimes } 3002ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 3003ea022d16SRodney W. Grimes } else { 3004ea022d16SRodney W. Grimes onefile[0] = whichf; 3005ea022d16SRodney W. Grimes dirlist = onefile; 3006ea022d16SRodney W. Grimes simple = 1; 3007ea022d16SRodney W. Grimes } 3008ea022d16SRodney W. Grimes 30099aca17cbSMark Murray while ((dirname = *dirlist++)) { 3010ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 3011ea022d16SRodney W. Grimes /* 3012ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 3013ea022d16SRodney W. Grimes * used NLST, do what the user meant. 3014ea022d16SRodney W. Grimes */ 3015ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 3016ea022d16SRodney W. Grimes transflag == 0) { 3017af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 3018ea022d16SRodney W. Grimes goto out; 3019ea022d16SRodney W. Grimes } 3020ea022d16SRodney W. Grimes perror_reply(550, whichf); 3021ea022d16SRodney W. Grimes if (dout != NULL) { 3022ea022d16SRodney W. Grimes (void) fclose(dout); 3023ea022d16SRodney W. Grimes transflag = 0; 3024ea022d16SRodney W. Grimes data = -1; 3025ea022d16SRodney W. Grimes pdata = -1; 3026ea022d16SRodney W. Grimes } 3027ea022d16SRodney W. Grimes goto out; 3028ea022d16SRodney W. Grimes } 3029ea022d16SRodney W. Grimes 3030ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 3031ea022d16SRodney W. Grimes if (dout == NULL) { 30320e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w"); 3033ea022d16SRodney W. Grimes if (dout == NULL) 3034ea022d16SRodney W. Grimes goto out; 3035ea022d16SRodney W. Grimes transflag++; 3036ea022d16SRodney W. Grimes } 3037ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 3038ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3039ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 3040ea022d16SRodney W. Grimes continue; 3041ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 3042ea022d16SRodney W. Grimes continue; 3043ea022d16SRodney W. Grimes 3044ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 3045ea022d16SRodney W. Grimes continue; 3046ea022d16SRodney W. Grimes 3047ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 3048ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 3049ea022d16SRodney W. Grimes 30504b82fc95SYaroslav Tykhiy if (recvurg) { 30514b82fc95SYaroslav Tykhiy myoob(); 30524b82fc95SYaroslav Tykhiy recvurg = 0; 30534b82fc95SYaroslav Tykhiy transflag = 0; 30544b82fc95SYaroslav Tykhiy goto out; 30554b82fc95SYaroslav Tykhiy } 30564b82fc95SYaroslav Tykhiy 3057ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 3058ea022d16SRodney W. Grimes continue; 3059ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 3060ea022d16SRodney W. Grimes dir->d_namlen == 2) 3061ea022d16SRodney W. Grimes continue; 3062ea022d16SRodney W. Grimes 3063e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 3064e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 3065ea022d16SRodney W. Grimes 3066ea022d16SRodney W. Grimes /* 3067ea022d16SRodney W. Grimes * We have to do a stat to insure it's 3068ea022d16SRodney W. Grimes * not a directory or special file. 3069ea022d16SRodney W. Grimes */ 3070ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 3071ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 3072ea022d16SRodney W. Grimes if (dout == NULL) { 30730e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w"); 3074ea022d16SRodney W. Grimes if (dout == NULL) 3075ea022d16SRodney W. Grimes goto out; 3076ea022d16SRodney W. Grimes transflag++; 3077ea022d16SRodney W. Grimes } 3078ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 3079ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 3080ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3081ea022d16SRodney W. Grimes else 3082ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 3083ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3084ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 3085ea022d16SRodney W. Grimes } 3086ea022d16SRodney W. Grimes } 3087ea022d16SRodney W. Grimes (void) closedir(dirp); 3088ea022d16SRodney W. Grimes } 3089ea022d16SRodney W. Grimes 3090ea022d16SRodney W. Grimes if (dout == NULL) 3091ea022d16SRodney W. Grimes reply(550, "No files found."); 3092ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 3093ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 3094ea022d16SRodney W. Grimes else 3095ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 3096ea022d16SRodney W. Grimes 3097ea022d16SRodney W. Grimes transflag = 0; 3098ea022d16SRodney W. Grimes if (dout != NULL) 3099ea022d16SRodney W. Grimes (void) fclose(dout); 3100ea022d16SRodney W. Grimes data = -1; 3101ea022d16SRodney W. Grimes pdata = -1; 3102ea022d16SRodney W. Grimes out: 3103ea022d16SRodney W. Grimes if (freeglob) { 3104ea022d16SRodney W. Grimes freeglob = 0; 3105ea022d16SRodney W. Grimes globfree(&gl); 3106ea022d16SRodney W. Grimes } 3107ea022d16SRodney W. Grimes } 3108ea022d16SRodney W. Grimes 3109cf09a206SDavid Greenman void 3110e4bc453cSWarner Losh reapchild(int signo) 3111cf09a206SDavid Greenman { 3112de9b6c03SYaroslav Tykhiy while (waitpid(-1, NULL, WNOHANG) > 0); 3113cf09a206SDavid Greenman } 3114cf09a206SDavid Greenman 3115b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 3116ea022d16SRodney W. Grimes /* 3117ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 3118ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 3119ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 3120ea022d16SRodney W. Grimes */ 3121ea022d16SRodney W. Grimes void 3122ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 3123ea022d16SRodney W. Grimes { 3124ea022d16SRodney W. Grimes int i; 3125ea022d16SRodney W. Grimes va_list ap; 3126ea022d16SRodney W. Grimes char *p, *bp, ch; 3127ea022d16SRodney W. Grimes char buf[LINE_MAX]; 3128ea022d16SRodney W. Grimes 3129ea022d16SRodney W. Grimes va_start(ap, fmt); 3130ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3131ea022d16SRodney W. Grimes 3132ea022d16SRodney W. Grimes /* make ps print our process name */ 3133ea022d16SRodney W. Grimes p = Argv[0]; 3134ea022d16SRodney W. Grimes *p++ = '-'; 3135ea022d16SRodney W. Grimes 3136ea022d16SRodney W. Grimes i = strlen(buf); 3137ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 3138ea022d16SRodney W. Grimes i = LastArgv - p - 2; 3139ea022d16SRodney W. Grimes buf[i] = '\0'; 3140ea022d16SRodney W. Grimes } 3141ea022d16SRodney W. Grimes bp = buf; 3142ea022d16SRodney W. Grimes while (ch = *bp++) 3143ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 3144ea022d16SRodney W. Grimes *p++ = ch; 3145ea022d16SRodney W. Grimes while (p < LastArgv) 3146ea022d16SRodney W. Grimes *p++ = ' '; 3147ea022d16SRodney W. Grimes } 3148b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 31493eb568f2SGuido van Rooij 315061f891a6SPaul Traina static void 31516b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...) 31526b2dee6bSYaroslav Tykhiy { 31536b2dee6bSYaroslav Tykhiy va_list ap; 31546b2dee6bSYaroslav Tykhiy char *ostr, *p; 31556b2dee6bSYaroslav Tykhiy 31566b2dee6bSYaroslav Tykhiy va_start(ap, fmt); 31576b2dee6bSYaroslav Tykhiy vasprintf(&p, fmt, ap); 31586b2dee6bSYaroslav Tykhiy va_end(ap); 31596b2dee6bSYaroslav Tykhiy if (p == NULL) 31606b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory."); 31616b2dee6bSYaroslav Tykhiy if (*strp == NULL) 31626b2dee6bSYaroslav Tykhiy *strp = p; 31636b2dee6bSYaroslav Tykhiy else { 31646b2dee6bSYaroslav Tykhiy ostr = *strp; 31656b2dee6bSYaroslav Tykhiy asprintf(strp, "%s%s", ostr, p); 31666b2dee6bSYaroslav Tykhiy if (*strp == NULL) 31676b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory."); 31686b2dee6bSYaroslav Tykhiy free(ostr); 31696b2dee6bSYaroslav Tykhiy } 31706b2dee6bSYaroslav Tykhiy } 31716b2dee6bSYaroslav Tykhiy 31726b2dee6bSYaroslav Tykhiy static void 31736b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt) 31746b2dee6bSYaroslav Tykhiy { 31756b2dee6bSYaroslav Tykhiy char *msg = NULL; 31766b2dee6bSYaroslav Tykhiy char wd[MAXPATHLEN + 1]; 31776b2dee6bSYaroslav Tykhiy 31786b2dee6bSYaroslav Tykhiy if (logging <= 1) 31796b2dee6bSYaroslav Tykhiy return; 3180e897216fSYaroslav Tykhiy 31816b2dee6bSYaroslav Tykhiy if (getcwd(wd, sizeof(wd) - 1) == NULL) 31826b2dee6bSYaroslav Tykhiy strcpy(wd, strerror(errno)); 31836b2dee6bSYaroslav Tykhiy 31846b2dee6bSYaroslav Tykhiy appendf(&msg, "%s", cmd); 31856b2dee6bSYaroslav Tykhiy if (file1) 31866b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file1); 31876b2dee6bSYaroslav Tykhiy if (file2) 31886b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file2); 31896b2dee6bSYaroslav Tykhiy if (cnt >= 0) 31906b2dee6bSYaroslav Tykhiy appendf(&msg, " = %jd bytes", (intmax_t)cnt); 3191e897216fSYaroslav Tykhiy appendf(&msg, " (wd: %s", wd); 3192215a9f9dSYaroslav Tykhiy if (guest || dochroot) 3193e897216fSYaroslav Tykhiy appendf(&msg, "; chrooted"); 3194e897216fSYaroslav Tykhiy appendf(&msg, ")"); 31956b2dee6bSYaroslav Tykhiy syslog(LOG_INFO, "%s", msg); 31966b2dee6bSYaroslav Tykhiy free(msg); 31976b2dee6bSYaroslav Tykhiy } 31986b2dee6bSYaroslav Tykhiy 31996b2dee6bSYaroslav Tykhiy static void 3200e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start) 32013eb568f2SGuido van Rooij { 32028c1c21f2SYaroslav Tykhiy char buf[MAXPATHLEN + 1024]; 32033eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 3204158a00b2SJohn Birrell time_t now; 32053eb568f2SGuido van Rooij 32068c1c21f2SYaroslav Tykhiy if (statfd >= 0) { 32073eb568f2SGuido van Rooij time(&now); 32088c1c21f2SYaroslav Tykhiy if (realpath(name, path) == NULL) { 32098c1c21f2SYaroslav Tykhiy syslog(LOG_NOTICE, "realpath failed on %s: %m", path); 32108c1c21f2SYaroslav Tykhiy return; 32118c1c21f2SYaroslav Tykhiy } 32128c1c21f2SYaroslav Tykhiy snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n", 32133eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 32148c1c21f2SYaroslav Tykhiy path, (intmax_t)size, 3215e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 32163eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 32173eb568f2SGuido van Rooij } 32183eb568f2SGuido van Rooij } 3219e4648f05SYaroslav Tykhiy 3220e4648f05SYaroslav Tykhiy static char * 3221e4648f05SYaroslav Tykhiy doublequote(char *s) 3222e4648f05SYaroslav Tykhiy { 3223e4648f05SYaroslav Tykhiy int n; 3224e4648f05SYaroslav Tykhiy char *p, *s2; 3225e4648f05SYaroslav Tykhiy 3226e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++) 3227e4648f05SYaroslav Tykhiy if (*p == '"') 3228e4648f05SYaroslav Tykhiy n++; 3229e4648f05SYaroslav Tykhiy 3230e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL) 3231e4648f05SYaroslav Tykhiy return (NULL); 3232e4648f05SYaroslav Tykhiy 3233e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) { 3234e4648f05SYaroslav Tykhiy if ((*p = *s) == '"') 3235e4648f05SYaroslav Tykhiy *(++p) = '"'; 3236e4648f05SYaroslav Tykhiy } 3237e4648f05SYaroslav Tykhiy *p = '\0'; 3238e4648f05SYaroslav Tykhiy 3239e4648f05SYaroslav Tykhiy return (s2); 3240e4648f05SYaroslav Tykhiy } 3241206fe568SHajimu UMEMOTO 3242206fe568SHajimu UMEMOTO /* setup server socket for specified address family */ 3243206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */ 3244206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */ 3245206fe568SHajimu UMEMOTO static int * 3246206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport) 3247206fe568SHajimu UMEMOTO { 3248206fe568SHajimu UMEMOTO struct addrinfo hints, *res, *r; 3249206fe568SHajimu UMEMOTO int error, maxs, *s, *socks; 3250206fe568SHajimu UMEMOTO const int on = 1; 3251206fe568SHajimu UMEMOTO 3252206fe568SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 3253206fe568SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE; 3254206fe568SHajimu UMEMOTO hints.ai_family = af; 3255206fe568SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM; 3256206fe568SHajimu UMEMOTO error = getaddrinfo(bindname, bindport, &hints, &res); 3257206fe568SHajimu UMEMOTO if (error) { 3258206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", gai_strerror(error)); 3259206fe568SHajimu UMEMOTO if (error == EAI_SYSTEM) 3260206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", strerror(errno)); 3261206fe568SHajimu UMEMOTO return NULL; 3262206fe568SHajimu UMEMOTO } 3263206fe568SHajimu UMEMOTO 3264206fe568SHajimu UMEMOTO /* Count max number of sockets we may open */ 3265206fe568SHajimu UMEMOTO for (maxs = 0, r = res; r; r = r->ai_next, maxs++) 3266206fe568SHajimu UMEMOTO ; 3267206fe568SHajimu UMEMOTO socks = malloc((maxs + 1) * sizeof(int)); 3268206fe568SHajimu UMEMOTO if (!socks) { 3269206fe568SHajimu UMEMOTO freeaddrinfo(res); 3270206fe568SHajimu UMEMOTO syslog(LOG_ERR, "couldn't allocate memory for sockets"); 3271206fe568SHajimu UMEMOTO return NULL; 3272206fe568SHajimu UMEMOTO } 3273206fe568SHajimu UMEMOTO 3274206fe568SHajimu UMEMOTO *socks = 0; /* num of sockets counter at start of array */ 3275206fe568SHajimu UMEMOTO s = socks + 1; 3276206fe568SHajimu UMEMOTO for (r = res; r; r = r->ai_next) { 3277206fe568SHajimu UMEMOTO *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); 3278206fe568SHajimu UMEMOTO if (*s < 0) { 3279206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control socket: %m"); 3280206fe568SHajimu UMEMOTO continue; 3281206fe568SHajimu UMEMOTO } 3282206fe568SHajimu UMEMOTO if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, 3283206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0) 3284206fe568SHajimu UMEMOTO syslog(LOG_WARNING, 3285206fe568SHajimu UMEMOTO "control setsockopt (SO_REUSEADDR): %m"); 3286206fe568SHajimu UMEMOTO if (r->ai_family == AF_INET6) { 3287206fe568SHajimu UMEMOTO if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, 3288206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0) 3289206fe568SHajimu UMEMOTO syslog(LOG_WARNING, 3290206fe568SHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m"); 3291206fe568SHajimu UMEMOTO } 3292206fe568SHajimu UMEMOTO if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { 3293206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control bind: %m"); 3294206fe568SHajimu UMEMOTO close(*s); 3295206fe568SHajimu UMEMOTO continue; 3296206fe568SHajimu UMEMOTO } 3297206fe568SHajimu UMEMOTO (*socks)++; 3298206fe568SHajimu UMEMOTO s++; 3299206fe568SHajimu UMEMOTO } 3300206fe568SHajimu UMEMOTO 3301206fe568SHajimu UMEMOTO if (res) 3302206fe568SHajimu UMEMOTO freeaddrinfo(res); 3303206fe568SHajimu UMEMOTO 3304206fe568SHajimu UMEMOTO if (*socks == 0) { 3305206fe568SHajimu UMEMOTO syslog(LOG_ERR, "control socket: Couldn't bind to any socket"); 3306206fe568SHajimu UMEMOTO free(socks); 3307206fe568SHajimu UMEMOTO return NULL; 3308206fe568SHajimu UMEMOTO } 3309206fe568SHajimu UMEMOTO return(socks); 3310206fe568SHajimu UMEMOTO } 3311