1ea022d16SRodney W. Grimes /* 2ea022d16SRodney W. Grimes * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 3ea022d16SRodney W. Grimes * The Regents of the University of California. All rights reserved. 4ea022d16SRodney W. Grimes * 5ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions 7ea022d16SRodney W. Grimes * are met: 8ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13ea022d16SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14ea022d16SRodney W. Grimes * must display the following acknowledgement: 15ea022d16SRodney W. Grimes * This product includes software developed by the University of 16ea022d16SRodney W. Grimes * California, Berkeley and its contributors. 17ea022d16SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 19ea022d16SRodney W. Grimes * without specific prior written permission. 20ea022d16SRodney W. Grimes * 21ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ea022d16SRodney W. Grimes * SUCH DAMAGE. 32ea022d16SRodney W. Grimes */ 33ea022d16SRodney W. Grimes 349aca17cbSMark Murray #if 0 35ea022d16SRodney W. Grimes #ifndef lint 36ea022d16SRodney W. Grimes static char copyright[] = 37ea022d16SRodney W. Grimes "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ 38ea022d16SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 39ea022d16SRodney W. Grimes #endif /* not lint */ 409aca17cbSMark Murray #endif 41ea022d16SRodney W. Grimes 42ea022d16SRodney W. Grimes #ifndef lint 43e02897faSPhilippe Charnier #if 0 44ea022d16SRodney W. Grimes static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; 459aca17cbSMark Murray #endif 46e02897faSPhilippe Charnier static const char rcsid[] = 477f3dea24SPeter Wemm "$FreeBSD$"; 48e02897faSPhilippe Charnier #endif /* not lint */ 49ea022d16SRodney W. Grimes 50ea022d16SRodney W. Grimes /* 51ea022d16SRodney W. Grimes * FTP server. 52ea022d16SRodney W. Grimes */ 53ea022d16SRodney W. Grimes #include <sys/param.h> 54ea022d16SRodney W. Grimes #include <sys/ioctl.h> 559fc5823aSGarrett Wollman #include <sys/mman.h> 56eb2fc780SGarrett Wollman #include <sys/socket.h> 57eb2fc780SGarrett Wollman #include <sys/stat.h> 58eb2fc780SGarrett Wollman #include <sys/time.h> 59eb2fc780SGarrett Wollman #include <sys/wait.h> 60ea022d16SRodney W. Grimes 61ea022d16SRodney W. Grimes #include <netinet/in.h> 62ea022d16SRodney W. Grimes #include <netinet/in_systm.h> 63ea022d16SRodney W. Grimes #include <netinet/ip.h> 649fc5823aSGarrett Wollman #include <netinet/tcp.h> 65ea022d16SRodney W. Grimes 66ea022d16SRodney W. Grimes #define FTP_NAMES 67ea022d16SRodney W. Grimes #include <arpa/ftp.h> 68ea022d16SRodney W. Grimes #include <arpa/inet.h> 69ea022d16SRodney W. Grimes #include <arpa/telnet.h> 70ea022d16SRodney W. Grimes 71ea022d16SRodney W. Grimes #include <ctype.h> 72ea022d16SRodney W. Grimes #include <dirent.h> 73ea022d16SRodney W. Grimes #include <err.h> 74ea022d16SRodney W. Grimes #include <errno.h> 75ea022d16SRodney W. Grimes #include <fcntl.h> 76ea022d16SRodney W. Grimes #include <glob.h> 77ea022d16SRodney W. Grimes #include <limits.h> 78ea022d16SRodney W. Grimes #include <netdb.h> 79ea022d16SRodney W. Grimes #include <pwd.h> 8031fea7b8SDavid Nugent #include <grp.h> 8147499ecdSAndrey A. Chernov #include <opie.h> 82ea022d16SRodney W. Grimes #include <signal.h> 83ea022d16SRodney W. Grimes #include <stdio.h> 84ea022d16SRodney W. Grimes #include <stdlib.h> 85ea022d16SRodney W. Grimes #include <string.h> 86ea022d16SRodney W. Grimes #include <syslog.h> 87ea022d16SRodney W. Grimes #include <time.h> 88ea022d16SRodney W. Grimes #include <unistd.h> 89b63e1fe2SPeter Wemm #include <libutil.h> 90b071c689SDavid Nugent #ifdef LOGIN_CAP 91b071c689SDavid Nugent #include <login_cap.h> 92b071c689SDavid Nugent #endif 93ea022d16SRodney W. Grimes 945bc9d93dSMark Murray #ifdef USE_PAM 956c9134c0SMark Murray #include <security/pam_appl.h> 966c9134c0SMark Murray #endif 976c9134c0SMark Murray 98ea022d16SRodney W. Grimes #include "pathnames.h" 99ea022d16SRodney W. Grimes #include "extern.h" 100ea022d16SRodney W. Grimes 101ea022d16SRodney W. Grimes #include <stdarg.h> 102ea022d16SRodney W. Grimes 103af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 104af85d782SDavid Nugent #undef main 105ea022d16SRodney W. Grimes 106ea022d16SRodney W. Grimes extern off_t restart_point; 107ea022d16SRodney W. Grimes extern char cbuf[]; 108ea022d16SRodney W. Grimes 1094dd8b5abSYoshinobu Inoue union sockunion server_addr; 1104dd8b5abSYoshinobu Inoue union sockunion ctrl_addr; 1114dd8b5abSYoshinobu Inoue union sockunion data_source; 1124dd8b5abSYoshinobu Inoue union sockunion data_dest; 1134dd8b5abSYoshinobu Inoue union sockunion his_addr; 1144dd8b5abSYoshinobu Inoue union sockunion pasv_addr; 115ea022d16SRodney W. Grimes 116cf09a206SDavid Greenman int daemon_mode; 117ea022d16SRodney W. Grimes int data; 118ea022d16SRodney W. Grimes int logged_in; 119ea022d16SRodney W. Grimes struct passwd *pw; 120618b0bbaSMark Murray int ftpdebug; 121ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 122ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 123ea022d16SRodney W. Grimes int logging; 1244c450ad7SPaul Traina int restricted_data_ports = 1; 125a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1265a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 127ea022d16SRodney W. Grimes int guest; 128a5a4544eSPaul Traina int dochroot; 1295d7e0128SYaroslav Tykhiy int dowtmp = 1; 1303eb568f2SGuido van Rooij int stats; 1313eb568f2SGuido van Rooij int statfd = -1; 132ea022d16SRodney W. Grimes int type; 133ea022d16SRodney W. Grimes int form; 134ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 135ea022d16SRodney W. Grimes int mode; 136ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 137ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 138a4b77a2aSPoul-Henning Kamp int readonly=0; /* Server is in readonly mode. */ 139a4b77a2aSPoul-Henning Kamp int noepsv=0; /* EPSV command is disabled. */ 14062513e76SNik Clayton int noretr=0; /* RETR command is disabled. */ 1411cc9f0bbSSheldon Hearn int noguestretr=0; /* RETR command is disabled for anon users. */ 142d186bb12SMatthew N. Dodd int noguestmkd=0; /* MKD command is disabled for anon users. */ 143a117c345SYaroslav Tykhiy int noguestmod=1; /* anon users may not modify existing files. */ 14462513e76SNik Clayton 1454b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg; 146ea022d16SRodney W. Grimes sig_atomic_t transflag; 147ea022d16SRodney W. Grimes off_t file_size; 148ea022d16SRodney W. Grimes off_t byte_count; 149ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 150ea022d16SRodney W. Grimes #undef CMASK 151ea022d16SRodney W. Grimes #define CMASK 027 152ea022d16SRodney W. Grimes #endif 153ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 154ea022d16SRodney W. Grimes char tmpline[7]; 155ea4e54b9SDavid Nugent char *hostname; 156ad442344SDima Dorfman int epsvall = 0; 157ad442344SDima Dorfman 1580512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 159ea4e54b9SDavid Nugent char *ftpuser; 160ea4e54b9SDavid Nugent 161ea4e54b9SDavid Nugent static struct ftphost { 162ea4e54b9SDavid Nugent struct ftphost *next; 163f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 164ea4e54b9SDavid Nugent char *hostname; 165ea4e54b9SDavid Nugent char *anonuser; 166ea4e54b9SDavid Nugent char *statfile; 167ea4e54b9SDavid Nugent char *welcome; 168ea4e54b9SDavid Nugent char *loginmsg; 169ea4e54b9SDavid Nugent } *thishost, *firsthost; 170ea4e54b9SDavid Nugent 171ea4e54b9SDavid Nugent #endif 1729e9a43bdSBrian Somers char remotehost[MAXHOSTNAMELEN]; 1733eb568f2SGuido van Rooij char *ident = NULL; 174a5a4544eSPaul Traina 175a5a4544eSPaul Traina static char ttyline[20]; 176a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 177a5a4544eSPaul Traina 1785bc9d93dSMark Murray #ifdef USE_PAM 179e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*); 1805bc9d93dSMark Murray pam_handle_t *pamh = NULL; 18147499ecdSAndrey A. Chernov #endif 182fa1746c9SMark Murray 183fa1746c9SMark Murray static struct opie opiedata; 184fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 18547499ecdSAndrey A. Chernov static int pwok; 1869aca17cbSMark Murray 187105a3c98SJulian Elischer char *pid_file = NULL; 188105a3c98SJulian Elischer 189ea022d16SRodney W. Grimes /* 1906d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 1916d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 1926d10cb2fSJonathan Lemon */ 1936d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 1946d10cb2fSJonathan Lemon # 1956d10cb2fSJonathan Lemon 1966d10cb2fSJonathan Lemon /* 197ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 198ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 199ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 200ea022d16SRodney W. Grimes */ 201ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 202ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 203ea022d16SRodney W. Grimes 204ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 205ea022d16SRodney W. Grimes int swaitint = SWAITINT; 206ea022d16SRodney W. Grimes 207ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 208b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 209ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 210ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 211b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 212ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 213ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 214ea022d16SRodney W. Grimes 215ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 216ea022d16SRodney W. Grimes if (logging > 1) \ 217ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 218ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 219ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 220ea022d16SRodney W. Grimes if (logging > 1) \ 221ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 222ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 223ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 224ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 225ea022d16SRodney W. Grimes if (logging > 1) { \ 226ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 227ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 228ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 229ea022d16SRodney W. Grimes else \ 230ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 231ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 232ea022d16SRodney W. Grimes } 233ea022d16SRodney W. Grimes 234ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 235e4bc453cSWarner Losh static void inithosts(void); 236e4bc453cSWarner Losh static void selecthost(union sockunion *); 237ea4e54b9SDavid Nugent #endif 238e4bc453cSWarner Losh static void ack(char *); 239e4bc453cSWarner Losh static void sigurg(int); 240e4bc453cSWarner Losh static void myoob(void); 241e4bc453cSWarner Losh static int checkuser(char *, char *, int); 242e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *); 243e4bc453cSWarner Losh static void dolog(struct sockaddr *); 244e4bc453cSWarner Losh static char *curdir(void); 245e4bc453cSWarner Losh static void end_login(void); 246e4bc453cSWarner Losh static FILE *getdatasock(char *); 247a117c345SYaroslav Tykhiy static int guniquefd(char *, char **); 248e4bc453cSWarner Losh static void lostconn(int); 249e4bc453cSWarner Losh static void sigquit(int); 250e4bc453cSWarner Losh static int receive_data(FILE *, FILE *); 251e4bc453cSWarner Losh static int send_data(FILE *, FILE *, off_t, off_t, int); 252ea022d16SRodney W. Grimes static struct passwd * 253e4bc453cSWarner Losh sgetpwnam(char *); 254e4bc453cSWarner Losh static char *sgetsave(char *); 255e4bc453cSWarner Losh static void reapchild(int); 256e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t); 257e4648f05SYaroslav Tykhiy static char *doublequote(char *); 258ea022d16SRodney W. Grimes 259ea022d16SRodney W. Grimes static char * 260e4bc453cSWarner Losh curdir(void) 261ea022d16SRodney W. Grimes { 262ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 263ea022d16SRodney W. Grimes 264ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 265ea022d16SRodney W. Grimes return (""); 266ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 267ea022d16SRodney W. Grimes strcat(path, "/"); 268ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 269ea022d16SRodney W. Grimes return (guest ? path+1 : path); 270ea022d16SRodney W. Grimes } 271ea022d16SRodney W. Grimes 272ea022d16SRodney W. Grimes int 273e4bc453cSWarner Losh main(int argc, char *argv[], char **envp) 274ea022d16SRodney W. Grimes { 275ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 276ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 277ea022d16SRodney W. Grimes FILE *fd; 2784dd8b5abSYoshinobu Inoue int error; 2794dd8b5abSYoshinobu Inoue char *bindname = NULL; 2804dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2814dd8b5abSYoshinobu Inoue int enable_v4 = 0; 2824b82fc95SYaroslav Tykhiy struct sigaction sa; 283ea022d16SRodney W. Grimes 28434d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 2854b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask); 2864b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 28734d1ba5cSAndrey A. Chernov 288b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 289ea022d16SRodney W. Grimes /* 290ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 291ea022d16SRodney W. Grimes */ 292ea022d16SRodney W. Grimes Argv = argv; 293ea022d16SRodney W. Grimes while (*envp) 294ea022d16SRodney W. Grimes envp++; 295ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 296b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 297ea022d16SRodney W. Grimes 2983eb568f2SGuido van Rooij 2995d7e0128SYaroslav Tykhiy while ((ch = getopt(argc, argv, "46a:AdDElmMoOp:rRSt:T:u:UvW")) != -1) { 300ea022d16SRodney W. Grimes switch (ch) { 3010e063efeSYaroslav Tykhiy case '4': 3020e063efeSYaroslav Tykhiy enable_v4 = 1; 3030e063efeSYaroslav Tykhiy if (family == AF_UNSPEC) 3040e063efeSYaroslav Tykhiy family = AF_INET; 3050e063efeSYaroslav Tykhiy break; 3060e063efeSYaroslav Tykhiy 3070e063efeSYaroslav Tykhiy case '6': 3080e063efeSYaroslav Tykhiy family = AF_INET6; 3090e063efeSYaroslav Tykhiy break; 3100e063efeSYaroslav Tykhiy 3110e063efeSYaroslav Tykhiy case 'a': 3120e063efeSYaroslav Tykhiy bindname = optarg; 3130e063efeSYaroslav Tykhiy break; 3140e063efeSYaroslav Tykhiy 3150e063efeSYaroslav Tykhiy case 'A': 3160e063efeSYaroslav Tykhiy anon_only = 1; 317cf09a206SDavid Greenman break; 318cf09a206SDavid Greenman 319ea022d16SRodney W. Grimes case 'd': 320618b0bbaSMark Murray ftpdebug++; 321ea022d16SRodney W. Grimes break; 322ea022d16SRodney W. Grimes 3230e063efeSYaroslav Tykhiy case 'D': 3240e063efeSYaroslav Tykhiy daemon_mode++; 3250e063efeSYaroslav Tykhiy break; 3260e063efeSYaroslav Tykhiy 327a4b77a2aSPoul-Henning Kamp case 'E': 328a4b77a2aSPoul-Henning Kamp noepsv = 1; 329a4b77a2aSPoul-Henning Kamp break; 330a4b77a2aSPoul-Henning Kamp 331ea022d16SRodney W. Grimes case 'l': 332ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 333ea022d16SRodney W. Grimes break; 334ea022d16SRodney W. Grimes 335a117c345SYaroslav Tykhiy case 'm': 336a117c345SYaroslav Tykhiy noguestmod = 0; 337a117c345SYaroslav Tykhiy break; 338a117c345SYaroslav Tykhiy 3390e063efeSYaroslav Tykhiy case 'M': 3400e063efeSYaroslav Tykhiy noguestmkd = 1; 3410e063efeSYaroslav Tykhiy break; 3420e063efeSYaroslav Tykhiy 3430e063efeSYaroslav Tykhiy case 'o': 3440e063efeSYaroslav Tykhiy noretr = 1; 3450e063efeSYaroslav Tykhiy break; 3460e063efeSYaroslav Tykhiy 3470e063efeSYaroslav Tykhiy case 'O': 3480e063efeSYaroslav Tykhiy noguestretr = 1; 3490e063efeSYaroslav Tykhiy break; 3500e063efeSYaroslav Tykhiy 3510e063efeSYaroslav Tykhiy case 'p': 3520e063efeSYaroslav Tykhiy pid_file = optarg; 3530e063efeSYaroslav Tykhiy break; 3540e063efeSYaroslav Tykhiy 355a4b77a2aSPoul-Henning Kamp case 'r': 356a4b77a2aSPoul-Henning Kamp readonly = 1; 357a4b77a2aSPoul-Henning Kamp break; 358a4b77a2aSPoul-Henning Kamp 359a5a4544eSPaul Traina case 'R': 360a5a4544eSPaul Traina paranoid = 0; 361a5a4544eSPaul Traina break; 362a5a4544eSPaul Traina 363a5a4544eSPaul Traina case 'S': 364a5a4544eSPaul Traina stats++; 365a5a4544eSPaul Traina break; 366a5a4544eSPaul Traina 367ea022d16SRodney W. Grimes case 't': 368ea022d16SRodney W. Grimes timeout = atoi(optarg); 369ea022d16SRodney W. Grimes if (maxtimeout < timeout) 370ea022d16SRodney W. Grimes maxtimeout = timeout; 371ea022d16SRodney W. Grimes break; 372a5a4544eSPaul Traina 3730e063efeSYaroslav Tykhiy case 'T': 3740e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg); 3750e063efeSYaroslav Tykhiy if (timeout > maxtimeout) 3760e063efeSYaroslav Tykhiy timeout = maxtimeout; 377105a3c98SJulian Elischer break; 378105a3c98SJulian Elischer 379ea022d16SRodney W. Grimes case 'u': 380ea022d16SRodney W. Grimes { 381ea022d16SRodney W. Grimes long val = 0; 382ea022d16SRodney W. Grimes 383ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 384ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 385ea022d16SRodney W. Grimes warnx("bad value for -u"); 386ea022d16SRodney W. Grimes else 387ea022d16SRodney W. Grimes defumask = val; 388ea022d16SRodney W. Grimes break; 389ea022d16SRodney W. Grimes } 3900e063efeSYaroslav Tykhiy case 'U': 3910e063efeSYaroslav Tykhiy restricted_data_ports = 0; 3925a392aecSTorsten Blum break; 393ea022d16SRodney W. Grimes 394ea022d16SRodney W. Grimes case 'v': 39593bd9dc5SYaroslav Tykhiy ftpdebug++; 396ea022d16SRodney W. Grimes break; 397ea022d16SRodney W. Grimes 3985d7e0128SYaroslav Tykhiy case 'W': 3995d7e0128SYaroslav Tykhiy dowtmp = 0; 4005d7e0128SYaroslav Tykhiy break; 4015d7e0128SYaroslav Tykhiy 402ea022d16SRodney W. Grimes default: 403ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 404ea022d16SRodney W. Grimes break; 405ea022d16SRodney W. Grimes } 406ea022d16SRodney W. Grimes } 407cf09a206SDavid Greenman 408ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 409ea4e54b9SDavid Nugent inithosts(); 410ea4e54b9SDavid Nugent #endif 411ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 412cf09a206SDavid Greenman 413cf09a206SDavid Greenman /* 414cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 415cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 416cf09a206SDavid Greenman */ 417cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 418cf09a206SDavid Greenman 419cf09a206SDavid Greenman if (daemon_mode) { 420cf09a206SDavid Greenman int ctl_sock, fd; 4214dd8b5abSYoshinobu Inoue struct addrinfo hints, *res; 422cf09a206SDavid Greenman 423cf09a206SDavid Greenman /* 424cf09a206SDavid Greenman * Detach from parent. 425cf09a206SDavid Greenman */ 426cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 427cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 428cf09a206SDavid Greenman exit(1); 429cf09a206SDavid Greenman } 4304b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild; 4314b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 4324dd8b5abSYoshinobu Inoue /* init bind_sa */ 4334dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 4344dd8b5abSYoshinobu Inoue 4354dd8b5abSYoshinobu Inoue hints.ai_family = family == AF_UNSPEC ? AF_INET : family; 4364dd8b5abSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 4374dd8b5abSYoshinobu Inoue hints.ai_protocol = 0; 4384dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 4394dd8b5abSYoshinobu Inoue error = getaddrinfo(bindname, "ftp", &hints, &res); 4404dd8b5abSYoshinobu Inoue if (error) { 4414dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) { 4424dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 4434dd8b5abSYoshinobu Inoue error = getaddrinfo(bindname, "ftp", &hints, 4444dd8b5abSYoshinobu Inoue &res); 4454dd8b5abSYoshinobu Inoue } 4464dd8b5abSYoshinobu Inoue } 4474dd8b5abSYoshinobu Inoue if (error) { 4483fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", gai_strerror(error)); 4494dd8b5abSYoshinobu Inoue if (error == EAI_SYSTEM) 4503fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", strerror(errno)); 4514dd8b5abSYoshinobu Inoue exit(1); 4524dd8b5abSYoshinobu Inoue } 4534dd8b5abSYoshinobu Inoue if (res->ai_addr == NULL) { 4544dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 455cf09a206SDavid Greenman exit(1); 4562310b8c6SRuslan Ermilov } else 4572310b8c6SRuslan Ermilov family = res->ai_addr->sa_family; 458cf09a206SDavid Greenman /* 459cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 460cf09a206SDavid Greenman * listening. 461cf09a206SDavid Greenman */ 4624dd8b5abSYoshinobu Inoue ctl_sock = socket(family, SOCK_STREAM, 0); 463cf09a206SDavid Greenman if (ctl_sock < 0) { 464cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 465cf09a206SDavid Greenman exit(1); 466cf09a206SDavid Greenman } 467cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 46857d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 469406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, 470406d1ae9SYaroslav Tykhiy "control setsockopt (SO_REUSEADDR): %m"); 4714dd8b5abSYoshinobu Inoue if (family == AF_INET6 && enable_v4 == 0) { 472fc99a00cSHajimu UMEMOTO if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY, 47357d4ef07SYaroslav Tykhiy &on, sizeof (on)) < 0) 474406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, 475fc99a00cSHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m"); 4764dd8b5abSYoshinobu Inoue } 4774dd8b5abSYoshinobu Inoue memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len); 4784dd8b5abSYoshinobu Inoue if (bind(ctl_sock, (struct sockaddr *)&server_addr, 4794dd8b5abSYoshinobu Inoue server_addr.su_len) < 0) { 480cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 481cf09a206SDavid Greenman exit(1); 482cf09a206SDavid Greenman } 483cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 484cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 485cf09a206SDavid Greenman exit(1); 486cf09a206SDavid Greenman } 487cf09a206SDavid Greenman /* 488105a3c98SJulian Elischer * Atomically write process ID 489105a3c98SJulian Elischer */ 490105a3c98SJulian Elischer if (pid_file) 491105a3c98SJulian Elischer { 492105a3c98SJulian Elischer int fd; 493105a3c98SJulian Elischer char buf[20]; 494105a3c98SJulian Elischer 495105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 496105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 49785966371SWarner Losh if (fd < 0) { 498105a3c98SJulian Elischer if (errno == EAGAIN) 499105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 500105a3c98SJulian Elischer else 501105a3c98SJulian Elischer err(1, "%s", pid_file); 50285966371SWarner Losh } 503105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 504105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 505105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 506105a3c98SJulian Elischer err(1, "%s: write", pid_file); 507105a3c98SJulian Elischer /* Leave the pid file open and locked */ 508105a3c98SJulian Elischer } 509105a3c98SJulian Elischer /* 510cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 511cf09a206SDavid Greenman * children to handle them. 512cf09a206SDavid Greenman */ 513cf09a206SDavid Greenman while (1) { 5144dd8b5abSYoshinobu Inoue addrlen = server_addr.su_len; 515cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 516cf09a206SDavid Greenman if (fork() == 0) { 517cf09a206SDavid Greenman /* child */ 518cf09a206SDavid Greenman (void) dup2(fd, 0); 519cf09a206SDavid Greenman (void) dup2(fd, 1); 520cf09a206SDavid Greenman close(ctl_sock); 521cf09a206SDavid Greenman break; 522cf09a206SDavid Greenman } 523cf09a206SDavid Greenman close(fd); 524cf09a206SDavid Greenman } 525cf09a206SDavid Greenman } else { 526cf09a206SDavid Greenman addrlen = sizeof(his_addr); 527cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 528cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 529cf09a206SDavid Greenman exit(1); 530cf09a206SDavid Greenman } 531cf09a206SDavid Greenman } 532cf09a206SDavid Greenman 5334b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL; 5344b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 5354b82fc95SYaroslav Tykhiy 5364b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg; 5374b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 5384b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL); 5394b82fc95SYaroslav Tykhiy 5404b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */ 5414b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 5424b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit; 5434b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL); 5444b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL); 5454b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL); 5464b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL); 5474b82fc95SYaroslav Tykhiy 5484b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn; 5494b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL); 550ea022d16SRodney W. Grimes 551cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 552cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 553cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 554cf09a206SDavid Greenman exit(1); 555cf09a206SDavid Greenman } 556ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 557ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5584dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 559ea4e54b9SDavid Nugent #endif 560cf09a206SDavid Greenman #ifdef IP_TOS 5614dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5624dd8b5abSYoshinobu Inoue { 563cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 56457d4ef07SYaroslav Tykhiy if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 565406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 5664dd8b5abSYoshinobu Inoue } 567cf09a206SDavid Greenman #endif 568dadb9fb3SDavid Greenman /* 569dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 570dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 571dadb9fb3SDavid Greenman */ 572dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 573406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 574dadb9fb3SDavid Greenman 5754dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 576cf09a206SDavid Greenman 577a5a4544eSPaul Traina /* set this here so klogin can use it... */ 578e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 579a5a4544eSPaul Traina 580ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 581ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 58257d4ef07SYaroslav Tykhiy if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 583406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 584ea022d16SRodney W. Grimes #endif 585ea022d16SRodney W. Grimes 586ea022d16SRodney W. Grimes #ifdef F_SETOWN 587ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 588ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 589ea022d16SRodney W. Grimes #endif 5904dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 591ea022d16SRodney W. Grimes /* 592ea022d16SRodney W. Grimes * Set up default state 593ea022d16SRodney W. Grimes */ 594ea022d16SRodney W. Grimes data = -1; 595ea022d16SRodney W. Grimes type = TYPE_A; 596ea022d16SRodney W. Grimes form = FORM_N; 597ea022d16SRodney W. Grimes stru = STRU_F; 598ea022d16SRodney W. Grimes mode = MODE_S; 599ea022d16SRodney W. Grimes tmpline[0] = '\0'; 600ea022d16SRodney W. Grimes 601ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 602ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 603ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 604ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 605ea022d16SRodney W. Grimes *cp = '\0'; 606ea022d16SRodney W. Grimes lreply(530, "%s", line); 607ea022d16SRodney W. Grimes } 608ea022d16SRodney W. Grimes (void) fflush(stdout); 609ea022d16SRodney W. Grimes (void) fclose(fd); 610ea022d16SRodney W. Grimes reply(530, "System not available."); 611ea022d16SRodney W. Grimes exit(0); 612ea022d16SRodney W. Grimes } 613ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 614ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 615ea4e54b9SDavid Nugent #else 616ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 617ea4e54b9SDavid Nugent #endif 618ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 619ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 620ea022d16SRodney W. Grimes *cp = '\0'; 621ea022d16SRodney W. Grimes lreply(220, "%s", line); 622ea022d16SRodney W. Grimes } 623ea022d16SRodney W. Grimes (void) fflush(stdout); 624ea022d16SRodney W. Grimes (void) fclose(fd); 625ea022d16SRodney W. Grimes /* reply(220,) must follow */ 626ea022d16SRodney W. Grimes } 627ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6280512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 629618b0bbaSMark Murray fatalerror("Ran out of memory."); 6309e9a43bdSBrian Somers (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 6319e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 632ea4e54b9SDavid Nugent #endif 633ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 634ea022d16SRodney W. Grimes for (;;) 635ea022d16SRodney W. Grimes (void) yyparse(); 636ea022d16SRodney W. Grimes /* NOTREACHED */ 637ea022d16SRodney W. Grimes } 638ea022d16SRodney W. Grimes 639ea022d16SRodney W. Grimes static void 640e4bc453cSWarner Losh lostconn(int signo) 641ea022d16SRodney W. Grimes { 642ea022d16SRodney W. Grimes 643618b0bbaSMark Murray if (ftpdebug) 644ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 645e02897faSPhilippe Charnier dologout(1); 646ea022d16SRodney W. Grimes } 647ea022d16SRodney W. Grimes 6484b82fc95SYaroslav Tykhiy static void 649e4bc453cSWarner Losh sigquit(int signo) 6504b82fc95SYaroslav Tykhiy { 6514b82fc95SYaroslav Tykhiy 6524b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo); 6534b82fc95SYaroslav Tykhiy dologout(1); 6544b82fc95SYaroslav Tykhiy } 6554b82fc95SYaroslav Tykhiy 656ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 657ea4e54b9SDavid Nugent /* 658ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 659ea4e54b9SDavid Nugent */ 660ea4e54b9SDavid Nugent 661ea4e54b9SDavid Nugent static void 662e4bc453cSWarner Losh inithosts(void) 663ea4e54b9SDavid Nugent { 66455b54aa7SYaroslav Tykhiy int insert; 665737d08f3SYaroslav Tykhiy size_t len; 666ea4e54b9SDavid Nugent FILE *fp; 667737d08f3SYaroslav Tykhiy char *cp, *mp, *line; 668737d08f3SYaroslav Tykhiy char *hostname; 66955b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 670ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 6714dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 672ea4e54b9SDavid Nugent 673ea4e54b9SDavid Nugent /* 674ea4e54b9SDavid Nugent * Fill in the default host information 675ea4e54b9SDavid Nugent */ 676737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 677618b0bbaSMark Murray fatalerror("Ran out of memory."); 678737d08f3SYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN) < 0) 679737d08f3SYaroslav Tykhiy hostname[0] = '\0'; 680737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0'; 681737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 682737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 683737d08f3SYaroslav Tykhiy hrp->hostname = hostname; 684f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 6854dd8b5abSYoshinobu Inoue 6864dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 6874dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 6884dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 689b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 690f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 691ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 692ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 693ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 694ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 695ea4e54b9SDavid Nugent hrp->next = NULL; 696ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 697ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 698ec009cf0SYaroslav Tykhiy int addrsize, gothost; 6994dd8b5abSYoshinobu Inoue void *addr; 7004dd8b5abSYoshinobu Inoue struct hostent *hp; 7014dd8b5abSYoshinobu Inoue 702737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) { 7034dd8b5abSYoshinobu Inoue int i, hp_error; 704ea4e54b9SDavid Nugent 705737d08f3SYaroslav Tykhiy /* skip comments */ 706737d08f3SYaroslav Tykhiy if (line[0] == '#') 707ea4e54b9SDavid Nugent continue; 708737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 709737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 710737d08f3SYaroslav Tykhiy mp = NULL; 711737d08f3SYaroslav Tykhiy } else { 712737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 713737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 714737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 715737d08f3SYaroslav Tykhiy mp[len] = '\0'; 716737d08f3SYaroslav Tykhiy line = mp; 717ea4e54b9SDavid Nugent } 718ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 719737d08f3SYaroslav Tykhiy /* skip empty lines */ 720737d08f3SYaroslav Tykhiy if (cp == NULL) 721737d08f3SYaroslav Tykhiy goto nextline; 72255b54aa7SYaroslav Tykhiy vhost = cp; 72355b54aa7SYaroslav Tykhiy 72455b54aa7SYaroslav Tykhiy /* set defaults */ 72555b54aa7SYaroslav Tykhiy anonuser = "ftp"; 72655b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE; 72755b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME; 72855b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG; 72955b54aa7SYaroslav Tykhiy 73055b54aa7SYaroslav Tykhiy /* 73155b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info 73255b54aa7SYaroslav Tykhiy * for all the addresses associated with 73355b54aa7SYaroslav Tykhiy * the virtual host name. 73455b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special: 73555b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed 73655b54aa7SYaroslav Tykhiy * later, after we know its canonical form. 73755b54aa7SYaroslav Tykhiy */ 73855b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 73955b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp))) 74055b54aa7SYaroslav Tykhiy switch (i) { 74155b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */ 74255b54aa7SYaroslav Tykhiy anonuser = cp; 74355b54aa7SYaroslav Tykhiy break; 74455b54aa7SYaroslav Tykhiy case 2: /* statistics file */ 74555b54aa7SYaroslav Tykhiy statfile = cp; 74655b54aa7SYaroslav Tykhiy break; 74755b54aa7SYaroslav Tykhiy case 3: /* welcome message */ 74855b54aa7SYaroslav Tykhiy welcome = cp; 74955b54aa7SYaroslav Tykhiy break; 75055b54aa7SYaroslav Tykhiy case 4: /* login message */ 75155b54aa7SYaroslav Tykhiy loginmsg = cp; 75255b54aa7SYaroslav Tykhiy break; 75355b54aa7SYaroslav Tykhiy default: /* programming error */ 75455b54aa7SYaroslav Tykhiy abort(); 75555b54aa7SYaroslav Tykhiy /* NOTREACHED */ 75655b54aa7SYaroslav Tykhiy } 7574dd8b5abSYoshinobu Inoue 7584dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 7594dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 7604dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 761b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 762737d08f3SYaroslav Tykhiy goto nextline; 7634dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 764b535a9bfSDavid Nugent ai = ai->ai_next) { 7654dd8b5abSYoshinobu Inoue 766b535a9bfSDavid Nugent gothost = 0; 767ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 768f38c6cadSYoshinobu Inoue struct addrinfo *hi; 769f38c6cadSYoshinobu Inoue 770f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 771f38c6cadSYoshinobu Inoue hi = hi->ai_next) 772f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 773f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7744dd8b5abSYoshinobu Inoue ai->ai_addr, 775b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 776b535a9bfSDavid Nugent gothost++; 777b535a9bfSDavid Nugent break; 778b535a9bfSDavid Nugent } 779b535a9bfSDavid Nugent if (gothost) 780ea4e54b9SDavid Nugent break; 781ea4e54b9SDavid Nugent } 782ea4e54b9SDavid Nugent if (hrp == NULL) { 783ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 784737d08f3SYaroslav Tykhiy goto nextline; 785b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 78655b54aa7SYaroslav Tykhiy insert = 1; 787f2fe752dSYaroslav Tykhiy } else { 7881f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res) 789b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 790f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */ 791f2fe752dSYaroslav Tykhiy } 792f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 793f38c6cadSYoshinobu Inoue 794ea4e54b9SDavid Nugent /* 795ea4e54b9SDavid Nugent * determine hostname to use. 7964dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 797ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 798ea4e54b9SDavid Nugent */ 7994dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 800f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 8014dd8b5abSYoshinobu Inoue case AF_INET: 8024b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 8034b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr); 8044dd8b5abSYoshinobu Inoue break; 8054dd8b5abSYoshinobu Inoue case AF_INET6: 8064b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 8074b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr); 8084dd8b5abSYoshinobu Inoue break; 8094dd8b5abSYoshinobu Inoue default: 8104dd8b5abSYoshinobu Inoue /* should not reach here */ 811f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 812f2fe752dSYaroslav Tykhiy if (insert) 813f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/ 814f2fe752dSYaroslav Tykhiy else 815f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/ 816737d08f3SYaroslav Tykhiy goto nextline; 8174dd8b5abSYoshinobu Inoue /* NOTREACHED */ 8184dd8b5abSYoshinobu Inoue } 81957d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize, 820f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 8214dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 82255b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) { 823ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 82455b54aa7SYaroslav Tykhiy vhost = hp->h_name; 825ea4e54b9SDavid Nugent else { 826ea4e54b9SDavid Nugent i = 0; 827ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 82855b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0) 829ea4e54b9SDavid Nugent ++i; 830ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 83155b54aa7SYaroslav Tykhiy vhost = hp->h_name; 832ea4e54b9SDavid Nugent } 833ea4e54b9SDavid Nugent } 834ea4e54b9SDavid Nugent } 835b1d8d5cdSYaroslav Tykhiy if (hrp->hostname && 836b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) { 837b1d8d5cdSYaroslav Tykhiy free(hrp->hostname); 838b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 839b1d8d5cdSYaroslav Tykhiy } 840b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL && 841f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) { 842f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 843f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */ 844f2fe752dSYaroslav Tykhiy if (hp) 845f2fe752dSYaroslav Tykhiy freehostent(hp); 84655b54aa7SYaroslav Tykhiy goto nextline; 847f2fe752dSYaroslav Tykhiy } 84855b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser; 84955b54aa7SYaroslav Tykhiy hrp->statfile = statfile; 85055b54aa7SYaroslav Tykhiy hrp->welcome = welcome; 85155b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg; 85255b54aa7SYaroslav Tykhiy if (insert) { 85355b54aa7SYaroslav Tykhiy hrp->next = NULL; 85455b54aa7SYaroslav Tykhiy lhrp->next = hrp; 85555b54aa7SYaroslav Tykhiy lhrp = hrp; 85655b54aa7SYaroslav Tykhiy } 857233c0f66SYaroslav Tykhiy if (hp) 8584dd8b5abSYoshinobu Inoue freehostent(hp); 8594dd8b5abSYoshinobu Inoue } 860737d08f3SYaroslav Tykhiy nextline: 861737d08f3SYaroslav Tykhiy if (mp) 862737d08f3SYaroslav Tykhiy free(mp); 863ea4e54b9SDavid Nugent } 864ea4e54b9SDavid Nugent (void) fclose(fp); 865ea4e54b9SDavid Nugent } 866ea4e54b9SDavid Nugent } 867ea4e54b9SDavid Nugent 868ea4e54b9SDavid Nugent static void 869e4bc453cSWarner Losh selecthost(union sockunion *su) 870ea4e54b9SDavid Nugent { 871ea4e54b9SDavid Nugent struct ftphost *hrp; 8724dd8b5abSYoshinobu Inoue u_int16_t port; 8734dd8b5abSYoshinobu Inoue #ifdef INET6 8744dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 8754dd8b5abSYoshinobu Inoue #endif 876f38c6cadSYoshinobu Inoue struct addrinfo *hi; 8774dd8b5abSYoshinobu Inoue 8784dd8b5abSYoshinobu Inoue #ifdef INET6 8794dd8b5abSYoshinobu Inoue /* 8804dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 8814dd8b5abSYoshinobu Inoue * specified in rfc2373. 8824dd8b5abSYoshinobu Inoue */ 8834dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 8844dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 8854dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 8864dd8b5abSYoshinobu Inoue #endif 887ea4e54b9SDavid Nugent 888ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 8894dd8b5abSYoshinobu Inoue port = su->su_port; 8904dd8b5abSYoshinobu Inoue su->su_port = 0; 891ea4e54b9SDavid Nugent while (hrp != NULL) { 892b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 893f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 894ea4e54b9SDavid Nugent thishost = hrp; 895ea4e54b9SDavid Nugent break; 896ea4e54b9SDavid Nugent } 8974dd8b5abSYoshinobu Inoue #ifdef INET6 8984dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 899f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 9004dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 901f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 9024dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 9034dd8b5abSYoshinobu Inoue thishost = hrp; 9044dd8b5abSYoshinobu Inoue break; 9054dd8b5abSYoshinobu Inoue } 9064dd8b5abSYoshinobu Inoue #endif 907f38c6cadSYoshinobu Inoue } 908ea4e54b9SDavid Nugent hrp = hrp->next; 909ea4e54b9SDavid Nugent } 9104dd8b5abSYoshinobu Inoue su->su_port = port; 911ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 912ea4e54b9SDavid Nugent hostname = thishost->hostname; 913ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 914ea4e54b9SDavid Nugent } 915ea4e54b9SDavid Nugent #endif 916ea4e54b9SDavid Nugent 917ea022d16SRodney W. Grimes /* 918ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 919ea022d16SRodney W. Grimes */ 920ea022d16SRodney W. Grimes static char * 921e4bc453cSWarner Losh sgetsave(char *s) 922ea022d16SRodney W. Grimes { 923ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 924ea022d16SRodney W. Grimes 925ea022d16SRodney W. Grimes if (new == NULL) { 926ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 927ea022d16SRodney W. Grimes dologout(1); 928ea022d16SRodney W. Grimes /* NOTREACHED */ 929ea022d16SRodney W. Grimes } 930ea022d16SRodney W. Grimes (void) strcpy(new, s); 931ea022d16SRodney W. Grimes return (new); 932ea022d16SRodney W. Grimes } 933ea022d16SRodney W. Grimes 934ea022d16SRodney W. Grimes /* 935ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 936ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 937ea022d16SRodney W. Grimes * (e.g., globbing). 938ea022d16SRodney W. Grimes */ 939ea022d16SRodney W. Grimes static struct passwd * 940e4bc453cSWarner Losh sgetpwnam(char *name) 941ea022d16SRodney W. Grimes { 942ea022d16SRodney W. Grimes static struct passwd save; 943ea022d16SRodney W. Grimes struct passwd *p; 944ea022d16SRodney W. Grimes 945ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 946ea022d16SRodney W. Grimes return (p); 947ea022d16SRodney W. Grimes if (save.pw_name) { 948ea022d16SRodney W. Grimes free(save.pw_name); 949ea022d16SRodney W. Grimes free(save.pw_passwd); 950ea022d16SRodney W. Grimes free(save.pw_gecos); 951ea022d16SRodney W. Grimes free(save.pw_dir); 952ea022d16SRodney W. Grimes free(save.pw_shell); 953ea022d16SRodney W. Grimes } 954ea022d16SRodney W. Grimes save = *p; 955ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 956ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 957ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 958ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 959ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 960ea022d16SRodney W. Grimes return (&save); 961ea022d16SRodney W. Grimes } 962ea022d16SRodney W. Grimes 963ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 964ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 96590906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 966ea022d16SRodney W. Grimes 967ea022d16SRodney W. Grimes /* 968ea022d16SRodney W. Grimes * USER command. 969ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 970ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 971ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 972ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 973ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 974ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 975ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 976ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 977ea022d16SRodney W. Grimes */ 978ea022d16SRodney W. Grimes void 979e4bc453cSWarner Losh user(char *name) 980ea022d16SRodney W. Grimes { 981ea022d16SRodney W. Grimes char *cp, *shell; 982ea022d16SRodney W. Grimes 983ea022d16SRodney W. Grimes if (logged_in) { 984ea022d16SRodney W. Grimes if (guest) { 985ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 986ea022d16SRodney W. Grimes return; 987a5a4544eSPaul Traina } else if (dochroot) { 988a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 989a5a4544eSPaul Traina return; 990ea022d16SRodney W. Grimes } 991ea022d16SRodney W. Grimes end_login(); 992ea022d16SRodney W. Grimes } 993ea022d16SRodney W. Grimes 994ea022d16SRodney W. Grimes guest = 0; 995ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 9967edcb936SSteve Price if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 9977edcb936SSteve Price checkuser(_PATH_FTPUSERS, "anonymous", 0)) 998ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 999ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1000ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 1001ea4e54b9SDavid Nugent #else 1002ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 1003ea4e54b9SDavid Nugent #endif 1004ea022d16SRodney W. Grimes guest = 1; 1005ea022d16SRodney W. Grimes askpasswd = 1; 1006ea022d16SRodney W. Grimes reply(331, 10072c60c54cSPaul Traina "Guest login ok, send your email address as password."); 1008ea022d16SRodney W. Grimes } else 1009ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 1010ea022d16SRodney W. Grimes if (!askpasswd && logging) 1011ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1012ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 1013ea022d16SRodney W. Grimes return; 1014ea022d16SRodney W. Grimes } 10155a392aecSTorsten Blum if (anon_only != 0) { 10165a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 10175a392aecSTorsten Blum return; 10185a392aecSTorsten Blum } 10195a392aecSTorsten Blum 10209aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 1021ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 1022ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 1023ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 1024ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 1025ea022d16SRodney W. Grimes break; 1026ea022d16SRodney W. Grimes endusershell(); 1027ea022d16SRodney W. Grimes 10287edcb936SSteve Price if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 1029ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1030ea022d16SRodney W. Grimes if (logging) 1031ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1032ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 1033ea022d16SRodney W. Grimes remotehost, name); 1034ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 1035ea022d16SRodney W. Grimes return; 1036ea022d16SRodney W. Grimes } 1037ea022d16SRodney W. Grimes } 1038ea022d16SRodney W. Grimes if (logging) 1039ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 104047499ecdSAndrey A. Chernov 104147499ecdSAndrey A. Chernov pwok = 0; 1042fa1746c9SMark Murray #ifdef USE_PAM 1043fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1044726040deSGuido van Rooij #endif 104547499ecdSAndrey A. Chernov if (opiechallenge(&opiedata, name, opieprompt) == 0) { 104647499ecdSAndrey A. Chernov pwok = (pw != NULL) && 104747499ecdSAndrey A. Chernov opieaccessfile(remotehost) && 104847499ecdSAndrey A. Chernov opiealways(pw->pw_dir); 104947499ecdSAndrey A. Chernov reply(331, "Response to %s %s for %s.", 105047499ecdSAndrey A. Chernov opieprompt, pwok ? "requested" : "required", name); 105147499ecdSAndrey A. Chernov } else { 105247499ecdSAndrey A. Chernov pwok = 1; 105347499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name); 105447499ecdSAndrey A. Chernov } 1055ea022d16SRodney W. Grimes askpasswd = 1; 1056ea022d16SRodney W. Grimes /* 1057ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 1058ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 1059ea022d16SRodney W. Grimes */ 1060ea022d16SRodney W. Grimes if (login_attempts) 1061ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 1062ea022d16SRodney W. Grimes } 1063ea022d16SRodney W. Grimes 1064ea022d16SRodney W. Grimes /* 1065a5a4544eSPaul Traina * Check if a user is in the file "fname" 1066ea022d16SRodney W. Grimes */ 1067ea022d16SRodney W. Grimes static int 1068e4bc453cSWarner Losh checkuser(char *fname, char *name, int pwset) 1069ea022d16SRodney W. Grimes { 1070ea022d16SRodney W. Grimes FILE *fd; 1071ea022d16SRodney W. Grimes int found = 0; 1072737d08f3SYaroslav Tykhiy size_t len; 1073737d08f3SYaroslav Tykhiy char *line, *mp, *p; 1074ea022d16SRodney W. Grimes 1075a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 1076737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) { 1077737d08f3SYaroslav Tykhiy /* skip comments */ 1078ea022d16SRodney W. Grimes if (line[0] == '#') 1079ea022d16SRodney W. Grimes continue; 1080737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 1081737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 1082737d08f3SYaroslav Tykhiy mp = NULL; 1083737d08f3SYaroslav Tykhiy } else { 1084737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 1085737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 1086737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 1087737d08f3SYaroslav Tykhiy mp[len] = '\0'; 1088737d08f3SYaroslav Tykhiy line = mp; 1089737d08f3SYaroslav Tykhiy } 1090737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */ 1091737d08f3SYaroslav Tykhiy p = strtok(line, " \t"); 1092737d08f3SYaroslav Tykhiy /* skip empty lines */ 1093737d08f3SYaroslav Tykhiy if (p == NULL) 1094737d08f3SYaroslav Tykhiy goto nextline; 109531fea7b8SDavid Nugent /* 109631fea7b8SDavid Nugent * if first chr is '@', check group membership 109731fea7b8SDavid Nugent */ 1098737d08f3SYaroslav Tykhiy if (p[0] == '@') { 109931fea7b8SDavid Nugent int i = 0; 110031fea7b8SDavid Nugent struct group *grp; 110131fea7b8SDavid Nugent 1102737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL) 1103737d08f3SYaroslav Tykhiy goto nextline; 11047edcb936SSteve Price /* 11057edcb936SSteve Price * Check user's default group 11067edcb936SSteve Price */ 11077edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 11087edcb936SSteve Price found = 1; 11097edcb936SSteve Price /* 11107edcb936SSteve Price * Check supplementary groups 11117edcb936SSteve Price */ 111231fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 111331fea7b8SDavid Nugent found = strcmp(name, 111431fea7b8SDavid Nugent grp->gr_mem[i++]) 111531fea7b8SDavid Nugent == 0; 1116ea022d16SRodney W. Grimes } 111731fea7b8SDavid Nugent /* 111831fea7b8SDavid Nugent * Otherwise, just check for username match 111931fea7b8SDavid Nugent */ 112031fea7b8SDavid Nugent else 1121737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0; 1122737d08f3SYaroslav Tykhiy nextline: 1123737d08f3SYaroslav Tykhiy if (mp) 1124737d08f3SYaroslav Tykhiy free(mp); 1125ea022d16SRodney W. Grimes } 1126ea022d16SRodney W. Grimes (void) fclose(fd); 1127ea022d16SRodney W. Grimes } 1128ea022d16SRodney W. Grimes return (found); 1129ea022d16SRodney W. Grimes } 1130ea022d16SRodney W. Grimes 1131ea022d16SRodney W. Grimes /* 1132ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1133ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1134ea022d16SRodney W. Grimes */ 1135ea022d16SRodney W. Grimes static void 1136e4bc453cSWarner Losh end_login(void) 1137ea022d16SRodney W. Grimes { 11385bc9d93dSMark Murray #ifdef USE_PAM 11395bc9d93dSMark Murray int e; 11405bc9d93dSMark Murray #endif 1141ea022d16SRodney W. Grimes 1142ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 11435d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) 114446948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 1145ea022d16SRodney W. Grimes pw = NULL; 1146b071c689SDavid Nugent #ifdef LOGIN_CAP 1147b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 1148d9e2c424SRobert Watson LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK| 1149d9e2c424SRobert Watson LOGIN_SETMAC); 1150b071c689SDavid Nugent #endif 11515bc9d93dSMark Murray #ifdef USE_PAM 11525bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 11535bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 11545bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 11555bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 11565bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 11575bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 11585bc9d93dSMark Murray pamh = NULL; 11595bc9d93dSMark Murray #endif 1160ea022d16SRodney W. Grimes logged_in = 0; 1161ea022d16SRodney W. Grimes guest = 0; 1162a5a4544eSPaul Traina dochroot = 0; 1163ea022d16SRodney W. Grimes } 1164ea022d16SRodney W. Grimes 11655bc9d93dSMark Murray #ifdef USE_PAM 11666c9134c0SMark Murray 11676c9134c0SMark Murray /* 11686c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 11696c9134c0SMark Murray * login.c 11706c9134c0SMark Murray */ 11716c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 11726c9134c0SMark Murray 11736c9134c0SMark Murray struct cred_t { 11746c9134c0SMark Murray const char *uname; /* user name */ 11756c9134c0SMark Murray const char *pass; /* password */ 11766c9134c0SMark Murray }; 11776c9134c0SMark Murray typedef struct cred_t cred_t; 11786c9134c0SMark Murray 11796c9134c0SMark Murray static int 11806c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 11816c9134c0SMark Murray struct pam_response **resp, void *appdata) 11826c9134c0SMark Murray { 11836c9134c0SMark Murray int i; 11846c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 118560769b19SDag-Erling Smørgrav struct pam_response *reply; 118660769b19SDag-Erling Smørgrav 118760769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply); 118860769b19SDag-Erling Smørgrav if (reply == NULL) 118960769b19SDag-Erling Smørgrav return PAM_BUF_ERR; 11906c9134c0SMark Murray 11916c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 11926c9134c0SMark Murray switch (msg[i]->msg_style) { 11936c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 11946c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 11956c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 11966c9134c0SMark Murray /* PAM frees resp. */ 11976c9134c0SMark Murray break; 11986c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 11996c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12006c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 12016c9134c0SMark Murray /* PAM frees resp. */ 12026c9134c0SMark Murray break; 12036c9134c0SMark Murray case PAM_TEXT_INFO: 12046c9134c0SMark Murray case PAM_ERROR_MSG: 12056c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12066c9134c0SMark Murray reply[i].resp = NULL; 12076c9134c0SMark Murray break; 12086c9134c0SMark Murray default: /* unknown message style */ 12096c9134c0SMark Murray free(reply); 12106c9134c0SMark Murray return PAM_CONV_ERR; 12116c9134c0SMark Murray } 12126c9134c0SMark Murray } 12136c9134c0SMark Murray 12146c9134c0SMark Murray *resp = reply; 12156c9134c0SMark Murray return PAM_SUCCESS; 12166c9134c0SMark Murray } 12176c9134c0SMark Murray 12186c9134c0SMark Murray /* 12196c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 12206c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 12216c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 12226c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 12236c9134c0SMark Murray * fall back to a different authentication mechanism. 12246c9134c0SMark Murray */ 12256c9134c0SMark Murray static int 12266c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 12276c9134c0SMark Murray { 12286c9134c0SMark Murray pam_handle_t *pamh = NULL; 12296c9134c0SMark Murray const char *tmpl_user; 12306c9134c0SMark Murray const void *item; 12316c9134c0SMark Murray int rval; 12326c9134c0SMark Murray int e; 12336c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 12346c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 12356c9134c0SMark Murray 12366c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 12376c9134c0SMark Murray if (e != PAM_SUCCESS) { 12386c9134c0SMark Murray syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 12396c9134c0SMark Murray return -1; 12406c9134c0SMark Murray } 12416c9134c0SMark Murray 1242ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1243ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1244ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1245ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1246ea413ab7SGuido van Rooij return -1; 1247ea413ab7SGuido van Rooij } 1248ea413ab7SGuido van Rooij 12496c9134c0SMark Murray e = pam_authenticate(pamh, 0); 12506c9134c0SMark Murray switch (e) { 12516c9134c0SMark Murray case PAM_SUCCESS: 12526c9134c0SMark Murray /* 12536c9134c0SMark Murray * With PAM we support the concept of a "template" 12546c9134c0SMark Murray * user. The user enters a login name which is 12556c9134c0SMark Murray * authenticated by PAM, usually via a remote service 12566c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 12576c9134c0SMark Murray * succeeds, a different but related "template" name 12586c9134c0SMark Murray * is used for setting the credentials, shell, and 12596c9134c0SMark Murray * home directory. The name the user enters need only 12606c9134c0SMark Murray * exist on the remote authentication server, but the 12616c9134c0SMark Murray * template name must be present in the local password 12626c9134c0SMark Murray * database. 12636c9134c0SMark Murray * 12646c9134c0SMark Murray * This is supported by two various mechanisms in the 12656c9134c0SMark Murray * individual modules. However, from the application's 12666c9134c0SMark Murray * point of view, the template user is always passed 12676c9134c0SMark Murray * back as a changed value of the PAM_USER item. 12686c9134c0SMark Murray */ 12696c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 12706c9134c0SMark Murray PAM_SUCCESS) { 12716c9134c0SMark Murray tmpl_user = (const char *) item; 12726c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 12736c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 12746c9134c0SMark Murray } else 12756c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 12766c9134c0SMark Murray pam_strerror(pamh, e)); 12776c9134c0SMark Murray rval = 0; 12786c9134c0SMark Murray break; 12796c9134c0SMark Murray 12806c9134c0SMark Murray case PAM_AUTH_ERR: 12816c9134c0SMark Murray case PAM_USER_UNKNOWN: 12826c9134c0SMark Murray case PAM_MAXTRIES: 12836c9134c0SMark Murray rval = 1; 12846c9134c0SMark Murray break; 12856c9134c0SMark Murray 12866c9134c0SMark Murray default: 12875bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 12886c9134c0SMark Murray rval = -1; 12896c9134c0SMark Murray break; 12906c9134c0SMark Murray } 12916c9134c0SMark Murray 12925bc9d93dSMark Murray if (rval == 0) { 12935bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 12945bc9d93dSMark Murray if (e == PAM_NEW_AUTHTOK_REQD) { 12955bc9d93dSMark Murray e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); 12965bc9d93dSMark Murray if (e != PAM_SUCCESS) { 12975bc9d93dSMark Murray syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e)); 12985bc9d93dSMark Murray rval = 1; 12995bc9d93dSMark Murray } 13005bc9d93dSMark Murray } else if (e != PAM_SUCCESS) { 13015bc9d93dSMark Murray rval = 1; 13025bc9d93dSMark Murray } 13035bc9d93dSMark Murray } 13045bc9d93dSMark Murray 13055bc9d93dSMark Murray if (rval != 0) { 13066c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 13076c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 13085bc9d93dSMark Murray } 13095bc9d93dSMark Murray pamh = NULL; 13106c9134c0SMark Murray } 13116c9134c0SMark Murray return rval; 13126c9134c0SMark Murray } 13136c9134c0SMark Murray 13145bc9d93dSMark Murray #endif /* USE_PAM */ 13156c9134c0SMark Murray 1316ea022d16SRodney W. Grimes void 1317e4bc453cSWarner Losh pass(char *passwd) 1318ea022d16SRodney W. Grimes { 1319a5a4544eSPaul Traina int rval; 1320ea022d16SRodney W. Grimes FILE *fd; 1321b071c689SDavid Nugent #ifdef LOGIN_CAP 1322b071c689SDavid Nugent login_cap_t *lc = NULL; 1323b071c689SDavid Nugent #endif 13245bc9d93dSMark Murray #ifdef USE_PAM 13255bc9d93dSMark Murray int e; 13265bc9d93dSMark Murray #endif 132747499ecdSAndrey A. Chernov char *xpasswd; 1328ea022d16SRodney W. Grimes 1329ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1330ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1331ea022d16SRodney W. Grimes return; 1332ea022d16SRodney W. Grimes } 1333ea022d16SRodney W. Grimes askpasswd = 0; 1334ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1335a5a4544eSPaul Traina if (pw == NULL) { 1336a5a4544eSPaul Traina rval = 1; /* failure below */ 1337a5a4544eSPaul Traina goto skip; 1338a5a4544eSPaul Traina } 13395bc9d93dSMark Murray #ifdef USE_PAM 13406c9134c0SMark Murray rval = auth_pam(&pw, passwd); 1341f650a124SAndrey A. Chernov if (rval >= 0) { 1342f650a124SAndrey A. Chernov opieunlock(); 1343a5a4544eSPaul Traina goto skip; 1344f650a124SAndrey A. Chernov } 1345f650a124SAndrey A. Chernov #endif 134647499ecdSAndrey A. Chernov if (opieverify(&opiedata, passwd) == 0) 134747499ecdSAndrey A. Chernov xpasswd = pw->pw_passwd; 1348f650a124SAndrey A. Chernov else if (pwok) { 134947499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd); 1350f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1351f650a124SAndrey A. Chernov xpasswd = ":"; 1352f650a124SAndrey A. Chernov } else { 135347499ecdSAndrey A. Chernov rval = 1; 135447499ecdSAndrey A. Chernov goto skip; 135547499ecdSAndrey A. Chernov } 135647499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd); 1357f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1358a5a4544eSPaul Traina rval = 1; /* failure */ 1359a5a4544eSPaul Traina skip: 1360a5a4544eSPaul Traina /* 1361a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 13626c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1363a5a4544eSPaul Traina * succeeded. 1364a5a4544eSPaul Traina */ 1365a5a4544eSPaul Traina if (rval) { 1366ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1367ea022d16SRodney W. Grimes if (logging) 1368ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1369ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1370ea022d16SRodney W. Grimes remotehost, curname); 1371ea022d16SRodney W. Grimes pw = NULL; 1372ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1373ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1374ea022d16SRodney W. Grimes "repeated login failures from %s", 1375ea022d16SRodney W. Grimes remotehost); 1376ea022d16SRodney W. Grimes exit(0); 1377ea022d16SRodney W. Grimes } 1378ea022d16SRodney W. Grimes return; 1379ea022d16SRodney W. Grimes } 1380ea022d16SRodney W. Grimes } 1381ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1382ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 1383ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1384ea022d16SRodney W. Grimes return; 1385ea022d16SRodney W. Grimes } 1386b071c689SDavid Nugent /* May be overridden by login.conf */ 1387b071c689SDavid Nugent (void) umask(defumask); 1388b071c689SDavid Nugent #ifdef LOGIN_CAP 13895d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 1390b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 1391b071c689SDavid Nugent 13924dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 13934dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 1394b0f06defSHajimu UMEMOTO NI_NUMERICHOST); 1395b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1396b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1397b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1398b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1399b071c689SDavid Nugent pw->pw_name); 1400b071c689SDavid Nugent reply(530, "Permission denied.\n"); 1401b071c689SDavid Nugent pw = NULL; 1402b071c689SDavid Nugent return; 1403b071c689SDavid Nugent } 1404b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1405b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1406b071c689SDavid Nugent pw = NULL; 1407b071c689SDavid Nugent return; 1408b071c689SDavid Nugent } 1409b071c689SDavid Nugent } 1410b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1411e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1412d9e2c424SRobert Watson LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC); 1413b071c689SDavid Nugent #else 1414e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1415ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1416b071c689SDavid Nugent #endif 1417ea022d16SRodney W. Grimes 14185bc9d93dSMark Murray #ifdef USE_PAM 14195bc9d93dSMark Murray if (pamh) { 14205bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 14215bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 14225bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 14235bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 14245bc9d93dSMark Murray } 14255bc9d93dSMark Murray } 14265bc9d93dSMark Murray #endif 14275bc9d93dSMark Murray 1428ea022d16SRodney W. Grimes /* open wtmp before chroot */ 14295d7e0128SYaroslav Tykhiy if (dowtmp) 14305d7e0128SYaroslav Tykhiy ftpd_logwtmp(ttyline, pw->pw_name, 14315d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr); 1432ea022d16SRodney W. Grimes logged_in = 1; 1433ea022d16SRodney W. Grimes 1434a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1435ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1436ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1437ea4e54b9SDavid Nugent #else 14383eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1439ea4e54b9SDavid Nugent #endif 14403eb568f2SGuido van Rooij stats = 0; 14413eb568f2SGuido van Rooij 1442b071c689SDavid Nugent dochroot = 1443b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1444b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 1445b071c689SDavid Nugent #endif 14467edcb936SSteve Price checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1447ea022d16SRodney W. Grimes if (guest) { 1448ea022d16SRodney W. Grimes /* 1449ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 1450ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 1451ea022d16SRodney W. Grimes * outside the new root! 1452ea022d16SRodney W. Grimes */ 1453ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1454ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 1455ea022d16SRodney W. Grimes goto bad; 1456ea022d16SRodney W. Grimes } 1457a5a4544eSPaul Traina } else if (dochroot) { 1458a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1459a5a4544eSPaul Traina reply(550, "Can't change root."); 1460a5a4544eSPaul Traina goto bad; 1461a5a4544eSPaul Traina } 1462ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 1463ea022d16SRodney W. Grimes if (chdir("/") < 0) { 1464ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 1465ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 1466ea022d16SRodney W. Grimes goto bad; 1467ea022d16SRodney W. Grimes } else 1468ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 1469ea022d16SRodney W. Grimes } 1470ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1471ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1472ea022d16SRodney W. Grimes goto bad; 1473ea022d16SRodney W. Grimes } 147482c76939SDavid Greenman 147582c76939SDavid Greenman /* 1476ea022d16SRodney W. Grimes * Display a login message, if it exists. 1477ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1478ea022d16SRodney W. Grimes */ 1479ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1480ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1481ea4e54b9SDavid Nugent #else 1482ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1483ea4e54b9SDavid Nugent #endif 1484ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1485ea022d16SRodney W. Grimes 1486ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1487ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1488ea022d16SRodney W. Grimes *cp = '\0'; 1489ea022d16SRodney W. Grimes lreply(230, "%s", line); 1490ea022d16SRodney W. Grimes } 1491ea022d16SRodney W. Grimes (void) fflush(stdout); 1492ea022d16SRodney W. Grimes (void) fclose(fd); 1493ea022d16SRodney W. Grimes } 1494ea022d16SRodney W. Grimes if (guest) { 14953eb568f2SGuido van Rooij if (ident != NULL) 14963eb568f2SGuido van Rooij free(ident); 149761f891a6SPaul Traina ident = strdup(passwd); 149861f891a6SPaul Traina if (ident == NULL) 1499618b0bbaSMark Murray fatalerror("Ran out of memory."); 150061f891a6SPaul Traina 1501ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1502ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1503ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1504ea4e54b9SDavid Nugent if (thishost != firsthost) 1505ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1506b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname, 1507b3a0a7cdSMike Heffner passwd); 1508ea4e54b9SDavid Nugent else 1509ea4e54b9SDavid Nugent #endif 1510ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1511b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd); 1512b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1513ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1514ea022d16SRodney W. Grimes if (logging) 1515ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1516ea022d16SRodney W. Grimes remotehost, passwd); 1517ea022d16SRodney W. Grimes } else { 15183401a71fSDaniel O'Callaghan if (dochroot) 151911342ab1SYaroslav Tykhiy reply(230, "User %s logged in, " 152011342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name); 15213401a71fSDaniel O'Callaghan else 1522ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 15233401a71fSDaniel O'Callaghan 1524ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1525ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 15267a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name); 1527b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1528ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1529ea022d16SRodney W. Grimes if (logging) 1530ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1531ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1532ea022d16SRodney W. Grimes } 1533b071c689SDavid Nugent #ifdef LOGIN_CAP 1534b071c689SDavid Nugent login_close(lc); 1535b071c689SDavid Nugent #endif 1536ea022d16SRodney W. Grimes return; 1537ea022d16SRodney W. Grimes bad: 1538ea022d16SRodney W. Grimes /* Forget all about it... */ 1539b071c689SDavid Nugent #ifdef LOGIN_CAP 1540b071c689SDavid Nugent login_close(lc); 1541b071c689SDavid Nugent #endif 1542ea022d16SRodney W. Grimes end_login(); 1543ea022d16SRodney W. Grimes } 1544ea022d16SRodney W. Grimes 1545ea022d16SRodney W. Grimes void 1546e4bc453cSWarner Losh retrieve(char *cmd, char *name) 1547ea022d16SRodney W. Grimes { 1548ea022d16SRodney W. Grimes FILE *fin, *dout; 1549ea022d16SRodney W. Grimes struct stat st; 1550e4bc453cSWarner Losh int (*closefunc)(FILE *); 1551158a00b2SJohn Birrell time_t start; 1552ea022d16SRodney W. Grimes 1553ea022d16SRodney W. Grimes if (cmd == 0) { 1554ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1555ea022d16SRodney W. Grimes st.st_size = 0; 1556ea022d16SRodney W. Grimes } else { 1557ea022d16SRodney W. Grimes char line[BUFSIZ]; 1558ea022d16SRodney W. Grimes 1559e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1560ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1561ea022d16SRodney W. Grimes st.st_size = -1; 1562ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1563ea022d16SRodney W. Grimes } 1564ea022d16SRodney W. Grimes if (fin == NULL) { 1565ea022d16SRodney W. Grimes if (errno != 0) { 1566ea022d16SRodney W. Grimes perror_reply(550, name); 1567ea022d16SRodney W. Grimes if (cmd == 0) { 1568ea022d16SRodney W. Grimes LOGCMD("get", name); 1569ea022d16SRodney W. Grimes } 1570ea022d16SRodney W. Grimes } 1571ea022d16SRodney W. Grimes return; 1572ea022d16SRodney W. Grimes } 1573ea022d16SRodney W. Grimes byte_count = -1; 1574ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1575ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1576ea022d16SRodney W. Grimes goto done; 1577ea022d16SRodney W. Grimes } 1578ea022d16SRodney W. Grimes if (restart_point) { 1579ea022d16SRodney W. Grimes if (type == TYPE_A) { 1580ea022d16SRodney W. Grimes off_t i, n; 1581ea022d16SRodney W. Grimes int c; 1582ea022d16SRodney W. Grimes 1583ea022d16SRodney W. Grimes n = restart_point; 1584ea022d16SRodney W. Grimes i = 0; 1585ea022d16SRodney W. Grimes while (i++ < n) { 1586ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1587ea022d16SRodney W. Grimes perror_reply(550, name); 1588ea022d16SRodney W. Grimes goto done; 1589ea022d16SRodney W. Grimes } 1590ea022d16SRodney W. Grimes if (c == '\n') 1591ea022d16SRodney W. Grimes i++; 1592ea022d16SRodney W. Grimes } 1593ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1594ea022d16SRodney W. Grimes perror_reply(550, name); 1595ea022d16SRodney W. Grimes goto done; 1596ea022d16SRodney W. Grimes } 1597ea022d16SRodney W. Grimes } 1598ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1599ea022d16SRodney W. Grimes if (dout == NULL) 1600ea022d16SRodney W. Grimes goto done; 16013eb568f2SGuido van Rooij time(&start); 16029fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 16039fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 16043eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 16053eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1606ea022d16SRodney W. Grimes (void) fclose(dout); 1607ea022d16SRodney W. Grimes data = -1; 1608ea022d16SRodney W. Grimes pdata = -1; 1609ea022d16SRodney W. Grimes done: 1610ea022d16SRodney W. Grimes if (cmd == 0) 1611ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1612ea022d16SRodney W. Grimes (*closefunc)(fin); 1613ea022d16SRodney W. Grimes } 1614ea022d16SRodney W. Grimes 1615ea022d16SRodney W. Grimes void 1616e4bc453cSWarner Losh store(char *name, char *mode, int unique) 1617ea022d16SRodney W. Grimes { 1618a117c345SYaroslav Tykhiy int fd; 1619ea022d16SRodney W. Grimes FILE *fout, *din; 1620e4bc453cSWarner Losh int (*closefunc)(FILE *); 1621ea022d16SRodney W. Grimes 1622a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */ 1623a117c345SYaroslav Tykhiy if (unique) { 1624a117c345SYaroslav Tykhiy /* Programming error */ 1625a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE"); 1626a117c345SYaroslav Tykhiy unique = 0; 1627a117c345SYaroslav Tykhiy } 1628a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1629a117c345SYaroslav Tykhiy reply(550, "Appending to existing file denied"); 1630a117c345SYaroslav Tykhiy goto err; 1631a117c345SYaroslav Tykhiy } 1632a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */ 1633a117c345SYaroslav Tykhiy } 1634a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */ 1635a117c345SYaroslav Tykhiy restart_point = 0; 1636a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1637a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */ 1638a117c345SYaroslav Tykhiy reply(550, "Modifying existing file denied"); 1639a117c345SYaroslav Tykhiy goto err; 1640a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */ 1641a117c345SYaroslav Tykhiy unique = 1; 1642ea022d16SRodney W. Grimes } 1643ea022d16SRodney W. Grimes 1644ea022d16SRodney W. Grimes if (restart_point) 1645a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */ 1646a117c345SYaroslav Tykhiy if (unique) { 1647a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0) 1648a117c345SYaroslav Tykhiy goto err; 1649a117c345SYaroslav Tykhiy fout = fdopen(fd, mode); 1650a117c345SYaroslav Tykhiy } else 1651ea022d16SRodney W. Grimes fout = fopen(name, mode); 1652ea022d16SRodney W. Grimes closefunc = fclose; 1653ea022d16SRodney W. Grimes if (fout == NULL) { 1654ea022d16SRodney W. Grimes perror_reply(553, name); 1655a117c345SYaroslav Tykhiy goto err; 1656ea022d16SRodney W. Grimes } 1657ea022d16SRodney W. Grimes byte_count = -1; 1658ea022d16SRodney W. Grimes if (restart_point) { 1659ea022d16SRodney W. Grimes if (type == TYPE_A) { 1660ea022d16SRodney W. Grimes off_t i, n; 1661ea022d16SRodney W. Grimes int c; 1662ea022d16SRodney W. Grimes 1663ea022d16SRodney W. Grimes n = restart_point; 1664ea022d16SRodney W. Grimes i = 0; 1665ea022d16SRodney W. Grimes while (i++ < n) { 1666ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1667ea022d16SRodney W. Grimes perror_reply(550, name); 1668ea022d16SRodney W. Grimes goto done; 1669ea022d16SRodney W. Grimes } 1670ea022d16SRodney W. Grimes if (c == '\n') 1671ea022d16SRodney W. Grimes i++; 1672ea022d16SRodney W. Grimes } 1673ea022d16SRodney W. Grimes /* 1674ea022d16SRodney W. Grimes * We must do this seek to "current" position 1675ea022d16SRodney W. Grimes * because we are changing from reading to 1676ea022d16SRodney W. Grimes * writing. 1677ea022d16SRodney W. Grimes */ 1678e4a71114SAndrey A. Chernov if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) { 1679ea022d16SRodney W. Grimes perror_reply(550, name); 1680ea022d16SRodney W. Grimes goto done; 1681ea022d16SRodney W. Grimes } 1682ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1683ea022d16SRodney W. Grimes perror_reply(550, name); 1684ea022d16SRodney W. Grimes goto done; 1685ea022d16SRodney W. Grimes } 1686ea022d16SRodney W. Grimes } 1687ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1688ea022d16SRodney W. Grimes if (din == NULL) 1689ea022d16SRodney W. Grimes goto done; 1690ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1691ea022d16SRodney W. Grimes if (unique) 1692ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1693ea022d16SRodney W. Grimes name); 1694ea022d16SRodney W. Grimes else 1695ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1696ea022d16SRodney W. Grimes } 1697ea022d16SRodney W. Grimes (void) fclose(din); 1698ea022d16SRodney W. Grimes data = -1; 1699ea022d16SRodney W. Grimes pdata = -1; 1700ea022d16SRodney W. Grimes done: 17017c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); 1702ea022d16SRodney W. Grimes (*closefunc)(fout); 1703a117c345SYaroslav Tykhiy return; 1704a117c345SYaroslav Tykhiy err: 1705a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name); 1706a117c345SYaroslav Tykhiy return; 1707ea022d16SRodney W. Grimes } 1708ea022d16SRodney W. Grimes 1709ea022d16SRodney W. Grimes static FILE * 1710e4bc453cSWarner Losh getdatasock(char *mode) 1711ea022d16SRodney W. Grimes { 1712ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1713ea022d16SRodney W. Grimes 1714ea022d16SRodney W. Grimes if (data >= 0) 1715ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1716ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 17174dd8b5abSYoshinobu Inoue 17184dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1719ea022d16SRodney W. Grimes if (s < 0) 1720ea022d16SRodney W. Grimes goto bad; 172157d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1722406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1723ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 17244dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 17254dd8b5abSYoshinobu Inoue data_source.su_port = htons(20); /* ftp-data port */ 1726ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1727ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 17284dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1729ea022d16SRodney W. Grimes break; 1730ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1731ea022d16SRodney W. Grimes goto bad; 1732ea022d16SRodney W. Grimes sleep(tries); 1733ea022d16SRodney W. Grimes } 1734ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1735ea022d16SRodney W. Grimes #ifdef IP_TOS 17364dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 17374dd8b5abSYoshinobu Inoue { 1738ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 173957d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1740406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 17414dd8b5abSYoshinobu Inoue } 1742ea022d16SRodney W. Grimes #endif 17439fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 17449fc5823aSGarrett Wollman /* 17459fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 17469fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 17479fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 17489fc5823aSGarrett Wollman * in heavy-load situations. 17499fc5823aSGarrett Wollman */ 17509fc5823aSGarrett Wollman on = 1; 175157d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1752406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 17539fc5823aSGarrett Wollman #endif 17549fc5823aSGarrett Wollman #ifdef SO_SNDBUF 17559fc5823aSGarrett Wollman on = 65536; 175657d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0) 1757406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m"); 17589fc5823aSGarrett Wollman #endif 17599fc5823aSGarrett Wollman 1760ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1761ea022d16SRodney W. Grimes bad: 1762ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1763ea022d16SRodney W. Grimes t = errno; 1764ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1765ea022d16SRodney W. Grimes (void) close(s); 1766ea022d16SRodney W. Grimes errno = t; 1767ea022d16SRodney W. Grimes return (NULL); 1768ea022d16SRodney W. Grimes } 1769ea022d16SRodney W. Grimes 1770ea022d16SRodney W. Grimes static FILE * 1771e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode) 1772ea022d16SRodney W. Grimes { 1773ea022d16SRodney W. Grimes char sizebuf[32]; 1774ea022d16SRodney W. Grimes FILE *file; 1775ea022d16SRodney W. Grimes int retry = 0, tos; 1776ea022d16SRodney W. Grimes 1777ea022d16SRodney W. Grimes file_size = size; 1778ea022d16SRodney W. Grimes byte_count = 0; 1779ea022d16SRodney W. Grimes if (size != (off_t) -1) 1780e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1781ea022d16SRodney W. Grimes else 1782e760ef2cSWarner Losh *sizebuf = '\0'; 1783ea022d16SRodney W. Grimes if (pdata >= 0) { 17844dd8b5abSYoshinobu Inoue union sockunion from; 17854cd48bacSYaroslav Tykhiy int flags; 17864dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1787d6ed3c37SGuido van Rooij struct timeval timeout; 1788d6ed3c37SGuido van Rooij fd_set set; 1789ea022d16SRodney W. Grimes 1790d6ed3c37SGuido van Rooij FD_ZERO(&set); 1791d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1792d6ed3c37SGuido van Rooij 1793d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1794d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1795d6ed3c37SGuido van Rooij 17964cd48bacSYaroslav Tykhiy /* 17974cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode, 17984cd48bacSYaroslav Tykhiy * accept() will block after a successful select() 17994cd48bacSYaroslav Tykhiy * if the selected connection dies in between. 18004cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here. 18014cd48bacSYaroslav Tykhiy */ 18024cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 18034cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 18044cd48bacSYaroslav Tykhiy goto pdata_err; 18054cd48bacSYaroslav Tykhiy if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 || 18064cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 18074cd48bacSYaroslav Tykhiy goto pdata_err; 1808ea022d16SRodney W. Grimes (void) close(pdata); 1809ea022d16SRodney W. Grimes pdata = s; 18104cd48bacSYaroslav Tykhiy /* 1811f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag 1812f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it. 18134cd48bacSYaroslav Tykhiy */ 18144cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 18154cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 18164cd48bacSYaroslav Tykhiy goto pdata_err; 1817ea022d16SRodney W. Grimes #ifdef IP_TOS 18184dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 18194dd8b5abSYoshinobu Inoue { 1820a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 182157d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1822406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 18234dd8b5abSYoshinobu Inoue } 1824ea022d16SRodney W. Grimes #endif 1825ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1826ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1827ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 18284cd48bacSYaroslav Tykhiy pdata_err: 18294cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection."); 18304cd48bacSYaroslav Tykhiy (void) close(pdata); 18314cd48bacSYaroslav Tykhiy pdata = -1; 18324cd48bacSYaroslav Tykhiy return (NULL); 1833ea022d16SRodney W. Grimes } 1834ea022d16SRodney W. Grimes if (data >= 0) { 1835ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1836ea022d16SRodney W. Grimes name, sizebuf); 1837ea022d16SRodney W. Grimes usedefault = 1; 1838ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1839ea022d16SRodney W. Grimes } 1840ea022d16SRodney W. Grimes if (usedefault) 1841ea022d16SRodney W. Grimes data_dest = his_addr; 1842ea022d16SRodney W. Grimes usedefault = 1; 1843ea022d16SRodney W. Grimes file = getdatasock(mode); 1844ea022d16SRodney W. Grimes if (file == NULL) { 18454dd8b5abSYoshinobu Inoue char hostbuf[BUFSIZ], portbuf[BUFSIZ]; 18464dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&data_source, 18474dd8b5abSYoshinobu Inoue data_source.su_len, hostbuf, sizeof(hostbuf) - 1, 18484dd8b5abSYoshinobu Inoue portbuf, sizeof(portbuf), 1849b0f06defSHajimu UMEMOTO NI_NUMERICHOST|NI_NUMERICSERV); 18504dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 18514dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1852ea022d16SRodney W. Grimes return (NULL); 1853ea022d16SRodney W. Grimes } 1854ea022d16SRodney W. Grimes data = fileno(file); 1855ea022d16SRodney W. Grimes while (connect(data, (struct sockaddr *)&data_dest, 18564dd8b5abSYoshinobu Inoue data_dest.su_len) < 0) { 1857ea022d16SRodney W. Grimes if (errno == EADDRINUSE && retry < swaitmax) { 1858ea022d16SRodney W. Grimes sleep((unsigned) swaitint); 1859ea022d16SRodney W. Grimes retry += swaitint; 1860ea022d16SRodney W. Grimes continue; 1861ea022d16SRodney W. Grimes } 1862ea022d16SRodney W. Grimes perror_reply(425, "Can't build data connection"); 1863ea022d16SRodney W. Grimes (void) fclose(file); 1864ea022d16SRodney W. Grimes data = -1; 1865ea022d16SRodney W. Grimes return (NULL); 1866ea022d16SRodney W. Grimes } 1867ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1868ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1869ea022d16SRodney W. Grimes return (file); 1870ea022d16SRodney W. Grimes } 1871ea022d16SRodney W. Grimes 1872ea022d16SRodney W. Grimes /* 1873ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 18749fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1875ea022d16SRodney W. Grimes * 1876ea022d16SRodney W. Grimes * NB: Form isn't handled. 1877ea022d16SRodney W. Grimes */ 18784b82fc95SYaroslav Tykhiy static int 1879e4bc453cSWarner Losh send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg) 1880ea022d16SRodney W. Grimes { 1881f6f0c4b9SDan Moschuk int c, filefd, netfd; 1882f6f0c4b9SDan Moschuk char *buf; 1883f6f0c4b9SDan Moschuk off_t cnt; 1884ea022d16SRodney W. Grimes 1885ea022d16SRodney W. Grimes transflag++; 1886ea022d16SRodney W. Grimes switch (type) { 1887ea022d16SRodney W. Grimes 1888ea022d16SRodney W. Grimes case TYPE_A: 1889ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 18904b82fc95SYaroslav Tykhiy if (recvurg) 18914b82fc95SYaroslav Tykhiy goto got_oob; 1892ea022d16SRodney W. Grimes byte_count++; 1893ea022d16SRodney W. Grimes if (c == '\n') { 1894ea022d16SRodney W. Grimes if (ferror(outstr)) 1895ea022d16SRodney W. Grimes goto data_err; 1896ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1897ea022d16SRodney W. Grimes } 1898ea022d16SRodney W. Grimes (void) putc(c, outstr); 1899ea022d16SRodney W. Grimes } 19004b82fc95SYaroslav Tykhiy if (recvurg) 19014b82fc95SYaroslav Tykhiy goto got_oob; 1902ea022d16SRodney W. Grimes fflush(outstr); 1903ea022d16SRodney W. Grimes transflag = 0; 1904ea022d16SRodney W. Grimes if (ferror(instr)) 1905ea022d16SRodney W. Grimes goto file_err; 1906ea022d16SRodney W. Grimes if (ferror(outstr)) 1907ea022d16SRodney W. Grimes goto data_err; 1908ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 19094b82fc95SYaroslav Tykhiy return (0); 1910ea022d16SRodney W. Grimes 1911ea022d16SRodney W. Grimes case TYPE_I: 1912ea022d16SRodney W. Grimes case TYPE_L: 19139fc5823aSGarrett Wollman /* 19149fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 19159fc5823aSGarrett Wollman * are sending a regular file 19169fc5823aSGarrett Wollman */ 19179fc5823aSGarrett Wollman netfd = fileno(outstr); 19189fc5823aSGarrett Wollman filefd = fileno(instr); 19199fc5823aSGarrett Wollman 1920f6f0c4b9SDan Moschuk if (isreg) { 19219fc5823aSGarrett Wollman 1922f6f0c4b9SDan Moschuk off_t offset; 1923f6f0c4b9SDan Moschuk int err; 1924f6f0c4b9SDan Moschuk 1925f6f0c4b9SDan Moschuk err = cnt = offset = 0; 1926f6f0c4b9SDan Moschuk 1927492f1d9cSMaxim Konovalov while (err != -1 && filesize > 0) { 1928492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0, 1929f6f0c4b9SDan Moschuk (struct sf_hdtr *) NULL, &cnt, 0); 19303af48c42SMaxim Konovalov /* 19313af48c42SMaxim Konovalov * Calculate byte_count before OOB processing. 19323af48c42SMaxim Konovalov * It can be used in myoob() later. 19333af48c42SMaxim Konovalov */ 19343af48c42SMaxim Konovalov byte_count += cnt; 19354b82fc95SYaroslav Tykhiy if (recvurg) 19364b82fc95SYaroslav Tykhiy goto got_oob; 1937f6f0c4b9SDan Moschuk offset += cnt; 1938492f1d9cSMaxim Konovalov filesize -= cnt; 1939f6f0c4b9SDan Moschuk 1940f6f0c4b9SDan Moschuk if (err == -1) { 1941f6f0c4b9SDan Moschuk if (!cnt) 1942f6f0c4b9SDan Moschuk goto oldway; 1943f6f0c4b9SDan Moschuk 19449fc5823aSGarrett Wollman goto data_err; 1945f6f0c4b9SDan Moschuk } 1946f6f0c4b9SDan Moschuk } 1947f6f0c4b9SDan Moschuk 19480849c184SDan Moschuk transflag = 0; 19499fc5823aSGarrett Wollman reply(226, "Transfer complete."); 19504b82fc95SYaroslav Tykhiy return (0); 19519fc5823aSGarrett Wollman } 19529fc5823aSGarrett Wollman 19539fc5823aSGarrett Wollman oldway: 1954ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1955ea022d16SRodney W. Grimes transflag = 0; 1956ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 19574b82fc95SYaroslav Tykhiy return (-1); 1958ea022d16SRodney W. Grimes } 19599fc5823aSGarrett Wollman 1960ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1961ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1962ea022d16SRodney W. Grimes byte_count += cnt; 1963ea022d16SRodney W. Grimes transflag = 0; 1964ea022d16SRodney W. Grimes (void)free(buf); 1965ea022d16SRodney W. Grimes if (cnt != 0) { 1966ea022d16SRodney W. Grimes if (cnt < 0) 1967ea022d16SRodney W. Grimes goto file_err; 1968ea022d16SRodney W. Grimes goto data_err; 1969ea022d16SRodney W. Grimes } 1970ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 19714b82fc95SYaroslav Tykhiy return (0); 1972ea022d16SRodney W. Grimes default: 1973ea022d16SRodney W. Grimes transflag = 0; 1974ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 19754b82fc95SYaroslav Tykhiy return (-1); 1976ea022d16SRodney W. Grimes } 1977ea022d16SRodney W. Grimes 1978ea022d16SRodney W. Grimes data_err: 1979ea022d16SRodney W. Grimes transflag = 0; 1980ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 19814b82fc95SYaroslav Tykhiy return (-1); 1982ea022d16SRodney W. Grimes 1983ea022d16SRodney W. Grimes file_err: 1984ea022d16SRodney W. Grimes transflag = 0; 1985ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 19864b82fc95SYaroslav Tykhiy return (-1); 19874b82fc95SYaroslav Tykhiy 19884b82fc95SYaroslav Tykhiy got_oob: 19894b82fc95SYaroslav Tykhiy myoob(); 19904b82fc95SYaroslav Tykhiy recvurg = 0; 19914b82fc95SYaroslav Tykhiy transflag = 0; 19924b82fc95SYaroslav Tykhiy return (-1); 1993ea022d16SRodney W. Grimes } 1994ea022d16SRodney W. Grimes 1995ea022d16SRodney W. Grimes /* 1996ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 1997ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 1998ea022d16SRodney W. Grimes * 1999ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 2000ea022d16SRodney W. Grimes */ 2001ea022d16SRodney W. Grimes static int 2002e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr) 2003ea022d16SRodney W. Grimes { 2004ea022d16SRodney W. Grimes int c; 200561f891a6SPaul Traina int cnt, bare_lfs; 2006ea022d16SRodney W. Grimes char buf[BUFSIZ]; 2007ea022d16SRodney W. Grimes 2008ea022d16SRodney W. Grimes transflag++; 200961f891a6SPaul Traina bare_lfs = 0; 201061f891a6SPaul Traina 2011ea022d16SRodney W. Grimes switch (type) { 2012ea022d16SRodney W. Grimes 2013ea022d16SRodney W. Grimes case TYPE_I: 2014ea022d16SRodney W. Grimes case TYPE_L: 2015ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 20164b82fc95SYaroslav Tykhiy if (recvurg) 20174b82fc95SYaroslav Tykhiy goto got_oob; 2018ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 2019ea022d16SRodney W. Grimes goto file_err; 2020ea022d16SRodney W. Grimes byte_count += cnt; 2021ea022d16SRodney W. Grimes } 20224b82fc95SYaroslav Tykhiy if (recvurg) 20234b82fc95SYaroslav Tykhiy goto got_oob; 2024ea022d16SRodney W. Grimes if (cnt < 0) 2025ea022d16SRodney W. Grimes goto data_err; 2026ea022d16SRodney W. Grimes transflag = 0; 2027ea022d16SRodney W. Grimes return (0); 2028ea022d16SRodney W. Grimes 2029ea022d16SRodney W. Grimes case TYPE_E: 2030ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 2031ea022d16SRodney W. Grimes transflag = 0; 2032ea022d16SRodney W. Grimes return (-1); 2033ea022d16SRodney W. Grimes 2034ea022d16SRodney W. Grimes case TYPE_A: 2035ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 20364b82fc95SYaroslav Tykhiy if (recvurg) 20374b82fc95SYaroslav Tykhiy goto got_oob; 2038ea022d16SRodney W. Grimes byte_count++; 2039ea022d16SRodney W. Grimes if (c == '\n') 2040ea022d16SRodney W. Grimes bare_lfs++; 2041ea022d16SRodney W. Grimes while (c == '\r') { 2042ea022d16SRodney W. Grimes if (ferror(outstr)) 2043ea022d16SRodney W. Grimes goto data_err; 2044ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 2045ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 2046ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 2047ea022d16SRodney W. Grimes goto contin2; 2048ea022d16SRodney W. Grimes } 2049ea022d16SRodney W. Grimes } 2050ea022d16SRodney W. Grimes (void) putc(c, outstr); 2051ea022d16SRodney W. Grimes contin2: ; 2052ea022d16SRodney W. Grimes } 20534b82fc95SYaroslav Tykhiy if (recvurg) 20544b82fc95SYaroslav Tykhiy goto got_oob; 2055ea022d16SRodney W. Grimes fflush(outstr); 2056ea022d16SRodney W. Grimes if (ferror(instr)) 2057ea022d16SRodney W. Grimes goto data_err; 2058ea022d16SRodney W. Grimes if (ferror(outstr)) 2059ea022d16SRodney W. Grimes goto file_err; 2060ea022d16SRodney W. Grimes transflag = 0; 2061ea022d16SRodney W. Grimes if (bare_lfs) { 2062ea022d16SRodney W. Grimes lreply(226, 2063ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 2064ea022d16SRodney W. Grimes bare_lfs); 2065ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 2066ea022d16SRodney W. Grimes } 2067ea022d16SRodney W. Grimes return (0); 2068ea022d16SRodney W. Grimes default: 2069ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 2070ea022d16SRodney W. Grimes transflag = 0; 2071ea022d16SRodney W. Grimes return (-1); 2072ea022d16SRodney W. Grimes } 2073ea022d16SRodney W. Grimes 2074ea022d16SRodney W. Grimes data_err: 2075ea022d16SRodney W. Grimes transflag = 0; 2076ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 2077ea022d16SRodney W. Grimes return (-1); 2078ea022d16SRodney W. Grimes 2079ea022d16SRodney W. Grimes file_err: 2080ea022d16SRodney W. Grimes transflag = 0; 2081ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 2082ea022d16SRodney W. Grimes return (-1); 20834b82fc95SYaroslav Tykhiy 20844b82fc95SYaroslav Tykhiy got_oob: 20854b82fc95SYaroslav Tykhiy myoob(); 20864b82fc95SYaroslav Tykhiy recvurg = 0; 20874b82fc95SYaroslav Tykhiy transflag = 0; 20884b82fc95SYaroslav Tykhiy return (-1); 2089ea022d16SRodney W. Grimes } 2090ea022d16SRodney W. Grimes 2091ea022d16SRodney W. Grimes void 2092e4bc453cSWarner Losh statfilecmd(char *filename) 2093ea022d16SRodney W. Grimes { 2094ea022d16SRodney W. Grimes FILE *fin; 2095ea022d16SRodney W. Grimes int c; 2096ea022d16SRodney W. Grimes char line[LINE_MAX]; 2097ea022d16SRodney W. Grimes 2098af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2099ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 2100ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 2101ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 2102ea022d16SRodney W. Grimes if (c == '\n') { 2103ea022d16SRodney W. Grimes if (ferror(stdout)){ 2104ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 2105ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2106ea022d16SRodney W. Grimes dologout(1); 2107ea022d16SRodney W. Grimes /* NOTREACHED */ 2108ea022d16SRodney W. Grimes } 2109ea022d16SRodney W. Grimes if (ferror(fin)) { 2110ea022d16SRodney W. Grimes perror_reply(551, filename); 2111ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2112ea022d16SRodney W. Grimes return; 2113ea022d16SRodney W. Grimes } 2114ea022d16SRodney W. Grimes (void) putc('\r', stdout); 2115ea022d16SRodney W. Grimes } 2116ea022d16SRodney W. Grimes (void) putc(c, stdout); 2117ea022d16SRodney W. Grimes } 2118ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2119ea022d16SRodney W. Grimes reply(211, "End of Status"); 2120ea022d16SRodney W. Grimes } 2121ea022d16SRodney W. Grimes 2122ea022d16SRodney W. Grimes void 2123e4bc453cSWarner Losh statcmd(void) 2124ea022d16SRodney W. Grimes { 21254dd8b5abSYoshinobu Inoue union sockunion *su; 2126ea022d16SRodney W. Grimes u_char *a, *p; 2127b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST]; 21284dd8b5abSYoshinobu Inoue int ispassive; 2129ea022d16SRodney W. Grimes 2130a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname); 2131ea022d16SRodney W. Grimes printf(" %s\r\n", version); 2132ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 21334dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2134b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 21354dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 21364dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 21374dd8b5abSYoshinobu Inoue } 2138ea022d16SRodney W. Grimes printf("\r\n"); 2139ea022d16SRodney W. Grimes if (logged_in) { 2140ea022d16SRodney W. Grimes if (guest) 2141ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 2142ea022d16SRodney W. Grimes else 2143ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 2144ea022d16SRodney W. Grimes } else if (askpasswd) 2145ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 2146ea022d16SRodney W. Grimes else 2147ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 2148ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 2149ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 2150ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2151ea022d16SRodney W. Grimes if (type == TYPE_L) 215289fdc4e1SMike Barcroft #if CHAR_BIT == 8 215389fdc4e1SMike Barcroft printf(" %d", CHAR_BIT); 2154ea022d16SRodney W. Grimes #else 2155ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2156ea022d16SRodney W. Grimes #endif 2157ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2158ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2159ea022d16SRodney W. Grimes if (data != -1) 2160ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2161ea022d16SRodney W. Grimes else if (pdata != -1) { 21624dd8b5abSYoshinobu Inoue ispassive = 1; 21634dd8b5abSYoshinobu Inoue su = &pasv_addr; 2164ea022d16SRodney W. Grimes goto printaddr; 2165ea022d16SRodney W. Grimes } else if (usedefault == 0) { 21664dd8b5abSYoshinobu Inoue ispassive = 0; 21674dd8b5abSYoshinobu Inoue su = &data_dest; 2168ea022d16SRodney W. Grimes printaddr: 2169ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 21704dd8b5abSYoshinobu Inoue if (epsvall) { 21714dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 21724dd8b5abSYoshinobu Inoue goto epsvonly; 21734dd8b5abSYoshinobu Inoue } 21744dd8b5abSYoshinobu Inoue 21754dd8b5abSYoshinobu Inoue /* PORT/PASV */ 21764dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 21774dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 21784dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 21794dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 21804dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 21814dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 21824dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 21834dd8b5abSYoshinobu Inoue } 21844dd8b5abSYoshinobu Inoue 21854dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 21864dd8b5abSYoshinobu Inoue { 21874dd8b5abSYoshinobu Inoue int alen, af, i; 21884dd8b5abSYoshinobu Inoue 21894dd8b5abSYoshinobu Inoue switch (su->su_family) { 21904dd8b5abSYoshinobu Inoue case AF_INET: 21914dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 21924dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 21934dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 21944dd8b5abSYoshinobu Inoue af = 4; 21954dd8b5abSYoshinobu Inoue break; 21964dd8b5abSYoshinobu Inoue case AF_INET6: 21974dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 21984dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 21994dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 22004dd8b5abSYoshinobu Inoue af = 6; 22014dd8b5abSYoshinobu Inoue break; 22024dd8b5abSYoshinobu Inoue default: 22034dd8b5abSYoshinobu Inoue af = 0; 22044dd8b5abSYoshinobu Inoue break; 22054dd8b5abSYoshinobu Inoue } 22064dd8b5abSYoshinobu Inoue if (af) { 22074dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 22084dd8b5abSYoshinobu Inoue af, alen); 22094dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 22104dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 22114dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 22124dd8b5abSYoshinobu Inoue } 22134dd8b5abSYoshinobu Inoue } 22144dd8b5abSYoshinobu Inoue 22154dd8b5abSYoshinobu Inoue epsvonly:; 22164dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 22174dd8b5abSYoshinobu Inoue { 22184dd8b5abSYoshinobu Inoue int af; 22194dd8b5abSYoshinobu Inoue 22204dd8b5abSYoshinobu Inoue switch (su->su_family) { 22214dd8b5abSYoshinobu Inoue case AF_INET: 22224dd8b5abSYoshinobu Inoue af = 1; 22234dd8b5abSYoshinobu Inoue break; 22244dd8b5abSYoshinobu Inoue case AF_INET6: 22254dd8b5abSYoshinobu Inoue af = 2; 22264dd8b5abSYoshinobu Inoue break; 22274dd8b5abSYoshinobu Inoue default: 22284dd8b5abSYoshinobu Inoue af = 0; 22294dd8b5abSYoshinobu Inoue break; 22304dd8b5abSYoshinobu Inoue } 22314dd8b5abSYoshinobu Inoue if (af) { 2232b0f06defSHajimu UMEMOTO union sockunion tmp; 2233b0f06defSHajimu UMEMOTO 2234b0f06defSHajimu UMEMOTO tmp = *su; 2235b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6) 2236b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0; 2237b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 22384dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 22394dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 22404dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 22414dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 2242b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port)); 22434dd8b5abSYoshinobu Inoue } 22444dd8b5abSYoshinobu Inoue } 22454dd8b5abSYoshinobu Inoue } 2246ea022d16SRodney W. Grimes #undef UC 2247ea022d16SRodney W. Grimes } else 2248ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 2249ea022d16SRodney W. Grimes reply(211, "End of status"); 2250ea022d16SRodney W. Grimes } 2251ea022d16SRodney W. Grimes 2252ea022d16SRodney W. Grimes void 2253e4bc453cSWarner Losh fatalerror(char *s) 2254ea022d16SRodney W. Grimes { 2255ea022d16SRodney W. Grimes 2256ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 2257ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2258ea022d16SRodney W. Grimes dologout(0); 2259ea022d16SRodney W. Grimes /* NOTREACHED */ 2260ea022d16SRodney W. Grimes } 2261ea022d16SRodney W. Grimes 2262ea022d16SRodney W. Grimes void 2263ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2264ea022d16SRodney W. Grimes { 2265ea022d16SRodney W. Grimes va_list ap; 2266e4bc453cSWarner Losh 2267ea022d16SRodney W. Grimes va_start(ap, fmt); 2268ea022d16SRodney W. Grimes (void)printf("%d ", n); 2269ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2270ea022d16SRodney W. Grimes (void)printf("\r\n"); 2271ea022d16SRodney W. Grimes (void)fflush(stdout); 2272618b0bbaSMark Murray if (ftpdebug) { 2273ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 2274ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2275ea022d16SRodney W. Grimes } 2276ea022d16SRodney W. Grimes } 2277ea022d16SRodney W. Grimes 2278ea022d16SRodney W. Grimes void 2279ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2280ea022d16SRodney W. Grimes { 2281ea022d16SRodney W. Grimes va_list ap; 2282e4bc453cSWarner Losh 2283ea022d16SRodney W. Grimes va_start(ap, fmt); 2284ea022d16SRodney W. Grimes (void)printf("%d- ", n); 2285ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2286ea022d16SRodney W. Grimes (void)printf("\r\n"); 2287ea022d16SRodney W. Grimes (void)fflush(stdout); 2288618b0bbaSMark Murray if (ftpdebug) { 2289ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 2290ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2291ea022d16SRodney W. Grimes } 2292ea022d16SRodney W. Grimes } 2293ea022d16SRodney W. Grimes 2294ea022d16SRodney W. Grimes static void 2295e4bc453cSWarner Losh ack(char *s) 2296ea022d16SRodney W. Grimes { 2297ea022d16SRodney W. Grimes 2298ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2299ea022d16SRodney W. Grimes } 2300ea022d16SRodney W. Grimes 2301ea022d16SRodney W. Grimes void 2302e4bc453cSWarner Losh nack(char *s) 2303ea022d16SRodney W. Grimes { 2304ea022d16SRodney W. Grimes 2305ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2306ea022d16SRodney W. Grimes } 2307ea022d16SRodney W. Grimes 2308ea022d16SRodney W. Grimes /* ARGSUSED */ 2309ea022d16SRodney W. Grimes void 2310e4bc453cSWarner Losh yyerror(char *s) 2311ea022d16SRodney W. Grimes { 2312ea022d16SRodney W. Grimes char *cp; 2313ea022d16SRodney W. Grimes 23149aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2315ea022d16SRodney W. Grimes *cp = '\0'; 2316ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 2317ea022d16SRodney W. Grimes } 2318ea022d16SRodney W. Grimes 2319ea022d16SRodney W. Grimes void 2320e4bc453cSWarner Losh delete(char *name) 2321ea022d16SRodney W. Grimes { 2322ea022d16SRodney W. Grimes struct stat st; 2323ea022d16SRodney W. Grimes 2324ea022d16SRodney W. Grimes LOGCMD("delete", name); 23251b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2326ea022d16SRodney W. Grimes perror_reply(550, name); 2327ea022d16SRodney W. Grimes return; 2328ea022d16SRodney W. Grimes } 2329ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 2330ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2331ea022d16SRodney W. Grimes perror_reply(550, name); 2332ea022d16SRodney W. Grimes return; 2333ea022d16SRodney W. Grimes } 2334ea022d16SRodney W. Grimes goto done; 2335ea022d16SRodney W. Grimes } 2336ea022d16SRodney W. Grimes if (unlink(name) < 0) { 2337ea022d16SRodney W. Grimes perror_reply(550, name); 2338ea022d16SRodney W. Grimes return; 2339ea022d16SRodney W. Grimes } 2340ea022d16SRodney W. Grimes done: 2341ea022d16SRodney W. Grimes ack("DELE"); 2342ea022d16SRodney W. Grimes } 2343ea022d16SRodney W. Grimes 2344ea022d16SRodney W. Grimes void 2345e4bc453cSWarner Losh cwd(char *path) 2346ea022d16SRodney W. Grimes { 2347ea022d16SRodney W. Grimes 2348ea022d16SRodney W. Grimes if (chdir(path) < 0) 2349ea022d16SRodney W. Grimes perror_reply(550, path); 2350ea022d16SRodney W. Grimes else 2351ea022d16SRodney W. Grimes ack("CWD"); 2352ea022d16SRodney W. Grimes } 2353ea022d16SRodney W. Grimes 2354ea022d16SRodney W. Grimes void 2355e4bc453cSWarner Losh makedir(char *name) 2356ea022d16SRodney W. Grimes { 23572b748987SYaroslav Tykhiy char *s; 2358ea022d16SRodney W. Grimes 2359ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2360d186bb12SMatthew N. Dodd if (guest && noguestmkd) 2361d186bb12SMatthew N. Dodd reply(550, "%s: permission denied", name); 2362d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0) 2363ea022d16SRodney W. Grimes perror_reply(550, name); 23642b748987SYaroslav Tykhiy else { 23652b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL) 23662b748987SYaroslav Tykhiy fatalerror("Ran out of memory."); 23672b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s); 23682b748987SYaroslav Tykhiy free(s); 23692b748987SYaroslav Tykhiy } 2370ea022d16SRodney W. Grimes } 2371ea022d16SRodney W. Grimes 2372ea022d16SRodney W. Grimes void 2373e4bc453cSWarner Losh removedir(char *name) 2374ea022d16SRodney W. Grimes { 2375ea022d16SRodney W. Grimes 2376ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2377ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2378ea022d16SRodney W. Grimes perror_reply(550, name); 2379ea022d16SRodney W. Grimes else 2380ea022d16SRodney W. Grimes ack("RMD"); 2381ea022d16SRodney W. Grimes } 2382ea022d16SRodney W. Grimes 2383ea022d16SRodney W. Grimes void 2384e4bc453cSWarner Losh pwd(void) 2385ea022d16SRodney W. Grimes { 2386e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1]; 2387ea022d16SRodney W. Grimes 2388ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 2389ea022d16SRodney W. Grimes reply(550, "%s.", path); 2390e4648f05SYaroslav Tykhiy else { 2391e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL) 2392e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory."); 2393e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s); 2394e4648f05SYaroslav Tykhiy free(s); 2395e4648f05SYaroslav Tykhiy } 2396ea022d16SRodney W. Grimes } 2397ea022d16SRodney W. Grimes 2398ea022d16SRodney W. Grimes char * 2399e4bc453cSWarner Losh renamefrom(char *name) 2400ea022d16SRodney W. Grimes { 2401ea022d16SRodney W. Grimes struct stat st; 2402ea022d16SRodney W. Grimes 24031b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2404ea022d16SRodney W. Grimes perror_reply(550, name); 2405ea022d16SRodney W. Grimes return ((char *)0); 2406ea022d16SRodney W. Grimes } 2407ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 2408ea022d16SRodney W. Grimes return (name); 2409ea022d16SRodney W. Grimes } 2410ea022d16SRodney W. Grimes 2411ea022d16SRodney W. Grimes void 2412e4bc453cSWarner Losh renamecmd(char *from, char *to) 2413ea022d16SRodney W. Grimes { 241461f891a6SPaul Traina struct stat st; 2415ea022d16SRodney W. Grimes 2416ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 241761f891a6SPaul Traina 241861f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 241961f891a6SPaul Traina reply(550, "%s: permission denied", to); 242061f891a6SPaul Traina return; 242161f891a6SPaul Traina } 242261f891a6SPaul Traina 2423ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2424ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2425ea022d16SRodney W. Grimes else 2426ea022d16SRodney W. Grimes ack("RNTO"); 2427ea022d16SRodney W. Grimes } 2428ea022d16SRodney W. Grimes 2429ea022d16SRodney W. Grimes static void 2430e4bc453cSWarner Losh dolog(struct sockaddr *who) 2431ea022d16SRodney W. Grimes { 24324dd8b5abSYoshinobu Inoue int error; 24334dd8b5abSYoshinobu Inoue 24344dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 2435ea022d16SRodney W. Grimes 2436ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2437ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2438ea4e54b9SDavid Nugent if (thishost != firsthost) 2439ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2440ea4e54b9SDavid Nugent remotehost, hostname); 2441ea4e54b9SDavid Nugent else 2442ea4e54b9SDavid Nugent #endif 2443ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2444ea4e54b9SDavid Nugent remotehost); 2445b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2446ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2447ea022d16SRodney W. Grimes 2448ea4e54b9SDavid Nugent if (logging) { 2449ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2450ea4e54b9SDavid Nugent if (thishost != firsthost) 2451ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 2452ea4e54b9SDavid Nugent remotehost, hostname); 2453ea4e54b9SDavid Nugent else 2454ea4e54b9SDavid Nugent #endif 24554dd8b5abSYoshinobu Inoue { 24564dd8b5abSYoshinobu Inoue char who_name[MAXHOSTNAMELEN]; 24574dd8b5abSYoshinobu Inoue 24584dd8b5abSYoshinobu Inoue error = getnameinfo(who, who->sa_len, 24594dd8b5abSYoshinobu Inoue who_name, sizeof(who_name) - 1, 2460b0f06defSHajimu UMEMOTO NULL, 0, NI_NUMERICHOST); 2461f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 24624dd8b5abSYoshinobu Inoue error == 0 ? who_name : ""); 24634dd8b5abSYoshinobu Inoue } 2464ea022d16SRodney W. Grimes } 2465ea4e54b9SDavid Nugent } 2466ea022d16SRodney W. Grimes 2467ea022d16SRodney W. Grimes /* 2468ea022d16SRodney W. Grimes * Record logout in wtmp file 2469ea022d16SRodney W. Grimes * and exit with supplied status. 2470ea022d16SRodney W. Grimes */ 2471ea022d16SRodney W. Grimes void 2472e4bc453cSWarner Losh dologout(int status) 2473ea022d16SRodney W. Grimes { 24740b4df2eeSDavid Greenman /* 24750b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 24760b4df2eeSDavid Greenman * back to the main program loop. 24770b4df2eeSDavid Greenman */ 24780b4df2eeSDavid Greenman transflag = 0; 2479ea022d16SRodney W. Grimes 24805d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) { 2481ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 248246948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 2483ea022d16SRodney W. Grimes } 2484ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2485ea022d16SRodney W. Grimes _exit(status); 2486ea022d16SRodney W. Grimes } 2487ea022d16SRodney W. Grimes 2488ea022d16SRodney W. Grimes static void 2489e4bc453cSWarner Losh sigurg(int signo) 2490ea022d16SRodney W. Grimes { 24914b82fc95SYaroslav Tykhiy 24924b82fc95SYaroslav Tykhiy recvurg = 1; 24934b82fc95SYaroslav Tykhiy } 24944b82fc95SYaroslav Tykhiy 24954b82fc95SYaroslav Tykhiy static void 2496e4bc453cSWarner Losh myoob(void) 24974b82fc95SYaroslav Tykhiy { 2498ea022d16SRodney W. Grimes char *cp; 2499ea022d16SRodney W. Grimes 2500ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2501ea022d16SRodney W. Grimes if (!transflag) 2502ea022d16SRodney W. Grimes return; 2503ea022d16SRodney W. Grimes cp = tmpline; 2504ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2505ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2506ea022d16SRodney W. Grimes dologout(0); 2507ea022d16SRodney W. Grimes } 2508ea022d16SRodney W. Grimes upper(cp); 2509ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2510ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2511ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 2512ea022d16SRodney W. Grimes reply(226, "Abort successful"); 2513ea022d16SRodney W. Grimes } 2514ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 25159db4bbf3SMichael Haro tmpline[0] = '\0'; 2516ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 2517ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 2518ea022d16SRodney W. Grimes byte_count, file_size); 2519ea022d16SRodney W. Grimes else 2520ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 2521ea022d16SRodney W. Grimes } 2522ea022d16SRodney W. Grimes } 2523ea022d16SRodney W. Grimes 2524ea022d16SRodney W. Grimes /* 2525ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2526ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2527ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2528ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2529ea022d16SRodney W. Grimes */ 2530ea022d16SRodney W. Grimes void 2531e4bc453cSWarner Losh passive(void) 2532ea022d16SRodney W. Grimes { 25338af7c9a3SYaroslav Tykhiy int len, on; 2534ea022d16SRodney W. Grimes char *p, *a; 2535ea022d16SRodney W. Grimes 253661f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 253761f891a6SPaul Traina close(pdata); 253861f891a6SPaul Traina 25394dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2540ea022d16SRodney W. Grimes if (pdata < 0) { 2541ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2542ea022d16SRodney W. Grimes return; 2543ea022d16SRodney W. Grimes } 25448af7c9a3SYaroslav Tykhiy on = 1; 25458af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 25468af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 25474c450ad7SPaul Traina 25484c450ad7SPaul Traina (void) seteuid((uid_t)0); 254961f891a6SPaul Traina 2550dacc9752SPaul Traina #ifdef IP_PORTRANGE 25514dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 25528af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 2553dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2554dacc9752SPaul Traina 255540e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 255657d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 25574c450ad7SPaul Traina goto pasv_error; 25584c450ad7SPaul Traina } 2559dacc9752SPaul Traina #endif 25602db39860SNick Sayer #ifdef IPV6_PORTRANGE 25612db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 25628af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 25632db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 25642db39860SNick Sayer 25652db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 256657d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 25672db39860SNick Sayer goto pasv_error; 25682db39860SNick Sayer } 25692db39860SNick Sayer #endif 257040e9d39eSPeter Wemm 2571ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 25724dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 25734dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2574ea022d16SRodney W. Grimes goto pasv_error; 257561f891a6SPaul Traina 2576ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 25774c450ad7SPaul Traina 2578ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2579ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2580ea022d16SRodney W. Grimes goto pasv_error; 2581ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2582ea022d16SRodney W. Grimes goto pasv_error; 25834dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 25844dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 25854dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 25864dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 25874dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 25884dd8b5abSYoshinobu Inoue else 25894dd8b5abSYoshinobu Inoue goto pasv_error; 25904dd8b5abSYoshinobu Inoue 25914dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2592ea022d16SRodney W. Grimes 2593ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2594ea022d16SRodney W. Grimes 2595ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2596ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2597ea022d16SRodney W. Grimes return; 2598ea022d16SRodney W. Grimes 2599ea022d16SRodney W. Grimes pasv_error: 260061f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2601ea022d16SRodney W. Grimes (void) close(pdata); 2602ea022d16SRodney W. Grimes pdata = -1; 2603ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2604ea022d16SRodney W. Grimes return; 2605ea022d16SRodney W. Grimes } 2606ea022d16SRodney W. Grimes 2607ea022d16SRodney W. Grimes /* 26084dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 26094dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 26104dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 26114dd8b5abSYoshinobu Inoue */ 26124dd8b5abSYoshinobu Inoue 26134dd8b5abSYoshinobu Inoue void 2614e4bc453cSWarner Losh long_passive(char *cmd, int pf) 26154dd8b5abSYoshinobu Inoue { 26168af7c9a3SYaroslav Tykhiy int len, on; 26174dd8b5abSYoshinobu Inoue char *p, *a; 26184dd8b5abSYoshinobu Inoue 26194dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 26204dd8b5abSYoshinobu Inoue close(pdata); 26214dd8b5abSYoshinobu Inoue 26224dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 26234dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 26244dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 26254dd8b5abSYoshinobu Inoue case AF_INET: 26264dd8b5abSYoshinobu Inoue pf = 1; 26274dd8b5abSYoshinobu Inoue break; 26284dd8b5abSYoshinobu Inoue case AF_INET6: 26294dd8b5abSYoshinobu Inoue pf = 2; 26304dd8b5abSYoshinobu Inoue break; 26314dd8b5abSYoshinobu Inoue default: 26324dd8b5abSYoshinobu Inoue pf = 0; 26334dd8b5abSYoshinobu Inoue break; 26344dd8b5abSYoshinobu Inoue } 26354dd8b5abSYoshinobu Inoue /* 26364dd8b5abSYoshinobu Inoue * XXX 26374dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 26384dd8b5abSYoshinobu Inoue */ 26394dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 26404dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 26414dd8b5abSYoshinobu Inoue "use (%d)", pf); 26424dd8b5abSYoshinobu Inoue } else 26434dd8b5abSYoshinobu Inoue reply(501, "Network protocol mismatch"); /*XXX*/ 26444dd8b5abSYoshinobu Inoue 26454dd8b5abSYoshinobu Inoue return; 26464dd8b5abSYoshinobu Inoue } 26474dd8b5abSYoshinobu Inoue } 26484dd8b5abSYoshinobu Inoue 26494dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 26504dd8b5abSYoshinobu Inoue if (pdata < 0) { 26514dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 26524dd8b5abSYoshinobu Inoue return; 26534dd8b5abSYoshinobu Inoue } 26548af7c9a3SYaroslav Tykhiy on = 1; 26558af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 26568af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 26574dd8b5abSYoshinobu Inoue 26584dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)0); 26594dd8b5abSYoshinobu Inoue 26604dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 26614dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 26624dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 26634dd8b5abSYoshinobu Inoue 26642db39860SNick Sayer #ifdef IP_PORTRANGE 26652db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 26668af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 26672db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 26682db39860SNick Sayer 26692db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 267057d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 26712db39860SNick Sayer goto pasv_error; 26722db39860SNick Sayer } 26732db39860SNick Sayer #endif 26742db39860SNick Sayer #ifdef IPV6_PORTRANGE 26752db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 26768af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 26772db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 26782db39860SNick Sayer 26792db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 268057d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 26812db39860SNick Sayer goto pasv_error; 26822db39860SNick Sayer } 26832db39860SNick Sayer #endif 26842db39860SNick Sayer 26854dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 26864dd8b5abSYoshinobu Inoue goto pasv_error; 26874dd8b5abSYoshinobu Inoue 26884dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 26894dd8b5abSYoshinobu Inoue 26904dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 26914dd8b5abSYoshinobu Inoue goto pasv_error; 26924dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 26934dd8b5abSYoshinobu Inoue goto pasv_error; 26944dd8b5abSYoshinobu Inoue 26954dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 26964dd8b5abSYoshinobu Inoue 26974dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 26984dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 26994dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 27004dd8b5abSYoshinobu Inoue case AF_INET: 27014dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 27024dd8b5abSYoshinobu Inoue v4_reply: 27034dd8b5abSYoshinobu Inoue reply(228, 27044dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 27054dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 27064dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 27074dd8b5abSYoshinobu Inoue return; 27084dd8b5abSYoshinobu Inoue case AF_INET6: 27094dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 27104dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 27114dd8b5abSYoshinobu Inoue goto v4_reply; 27124dd8b5abSYoshinobu Inoue } 27134dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 27144dd8b5abSYoshinobu Inoue reply(228, 27154dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 27164dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 27174dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 27184dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 27194dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 27204dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 27214dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 27224dd8b5abSYoshinobu Inoue return; 27234dd8b5abSYoshinobu Inoue } 27244dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 27254dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 27264dd8b5abSYoshinobu Inoue case AF_INET: 27274dd8b5abSYoshinobu Inoue case AF_INET6: 27284dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 27294dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 27304dd8b5abSYoshinobu Inoue return; 27314dd8b5abSYoshinobu Inoue } 27324dd8b5abSYoshinobu Inoue } else { 27334dd8b5abSYoshinobu Inoue /* more proper error code? */ 27344dd8b5abSYoshinobu Inoue } 27354dd8b5abSYoshinobu Inoue 27364dd8b5abSYoshinobu Inoue pasv_error: 27374dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 27384dd8b5abSYoshinobu Inoue (void) close(pdata); 27394dd8b5abSYoshinobu Inoue pdata = -1; 27404dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 27414dd8b5abSYoshinobu Inoue return; 27424dd8b5abSYoshinobu Inoue } 27434dd8b5abSYoshinobu Inoue 27444dd8b5abSYoshinobu Inoue /* 2745a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local" 2746a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races. 2747a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 2748a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name. 2749a117c345SYaroslav Tykhiy * 2750ea022d16SRodney W. Grimes * Generates failure reply on error. 2751ea022d16SRodney W. Grimes */ 2752a117c345SYaroslav Tykhiy static int 2753a117c345SYaroslav Tykhiy guniquefd(char *local, char **name) 2754ea022d16SRodney W. Grimes { 2755ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2756ea022d16SRodney W. Grimes struct stat st; 2757ea022d16SRodney W. Grimes char *cp; 2758a117c345SYaroslav Tykhiy int count; 2759a117c345SYaroslav Tykhiy int fd; 2760ea022d16SRodney W. Grimes 2761ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2762ea022d16SRodney W. Grimes if (cp) 2763ea022d16SRodney W. Grimes *cp = '\0'; 2764ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2765ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2766a117c345SYaroslav Tykhiy return (-1); 2767ea022d16SRodney W. Grimes } 2768a117c345SYaroslav Tykhiy if (cp) { 2769a117c345SYaroslav Tykhiy /* 2770a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix. 2771a117c345SYaroslav Tykhiy * -4 is for /nn\0 2772a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix. 2773a117c345SYaroslav Tykhiy */ 2774a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) { 2775a117c345SYaroslav Tykhiy reply(553, "Pathname too long"); 2776a117c345SYaroslav Tykhiy return (-1); 2777a117c345SYaroslav Tykhiy } 2778ea022d16SRodney W. Grimes *cp = '/'; 2779a117c345SYaroslav Tykhiy } 2780e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2781e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2782ea022d16SRodney W. Grimes cp = new + strlen(new); 2783a117c345SYaroslav Tykhiy /* 2784a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly. 2785a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off 2786a117c345SYaroslav Tykhiy * by buffer size. 2787a117c345SYaroslav Tykhiy */ 2788a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/') 2789ea022d16SRodney W. Grimes *cp++ = '.'; 2790a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) { 2791a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */ 2792a117c345SYaroslav Tykhiy if (count) 2793ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2794a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local, 2795a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 2796a117c345SYaroslav Tykhiy *name = count ? new : local; 2797a117c345SYaroslav Tykhiy return (fd); 2798a117c345SYaroslav Tykhiy } 2799ea022d16SRodney W. Grimes } 2800ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2801a117c345SYaroslav Tykhiy return (-1); 2802ea022d16SRodney W. Grimes } 2803ea022d16SRodney W. Grimes 2804ea022d16SRodney W. Grimes /* 2805ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2806ea022d16SRodney W. Grimes */ 2807ea022d16SRodney W. Grimes void 2808e4bc453cSWarner Losh perror_reply(int code, char *string) 2809ea022d16SRodney W. Grimes { 2810ea022d16SRodney W. Grimes 2811ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2812ea022d16SRodney W. Grimes } 2813ea022d16SRodney W. Grimes 2814ea022d16SRodney W. Grimes static char *onefile[] = { 2815ea022d16SRodney W. Grimes "", 2816ea022d16SRodney W. Grimes 0 2817ea022d16SRodney W. Grimes }; 2818ea022d16SRodney W. Grimes 2819ea022d16SRodney W. Grimes void 2820e4bc453cSWarner Losh send_file_list(char *whichf) 2821ea022d16SRodney W. Grimes { 2822ea022d16SRodney W. Grimes struct stat st; 2823ea022d16SRodney W. Grimes DIR *dirp = NULL; 2824ea022d16SRodney W. Grimes struct dirent *dir; 2825ea022d16SRodney W. Grimes FILE *dout = NULL; 2826ea022d16SRodney W. Grimes char **dirlist, *dirname; 2827ea022d16SRodney W. Grimes int simple = 0; 2828ea022d16SRodney W. Grimes int freeglob = 0; 2829ea022d16SRodney W. Grimes glob_t gl; 2830ea022d16SRodney W. Grimes 2831ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 283212da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 2833ea022d16SRodney W. Grimes 2834ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 28356d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 283675dc5f1aSMike Heffner flags |= GLOB_LIMIT; 2837ea022d16SRodney W. Grimes freeglob = 1; 2838ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2839ea022d16SRodney W. Grimes reply(550, "not found"); 2840ea022d16SRodney W. Grimes goto out; 2841ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2842ea022d16SRodney W. Grimes errno = ENOENT; 2843ea022d16SRodney W. Grimes perror_reply(550, whichf); 2844ea022d16SRodney W. Grimes goto out; 2845ea022d16SRodney W. Grimes } 2846ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2847ea022d16SRodney W. Grimes } else { 2848ea022d16SRodney W. Grimes onefile[0] = whichf; 2849ea022d16SRodney W. Grimes dirlist = onefile; 2850ea022d16SRodney W. Grimes simple = 1; 2851ea022d16SRodney W. Grimes } 2852ea022d16SRodney W. Grimes 28539aca17cbSMark Murray while ((dirname = *dirlist++)) { 2854ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2855ea022d16SRodney W. Grimes /* 2856ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2857ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2858ea022d16SRodney W. Grimes */ 2859ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2860ea022d16SRodney W. Grimes transflag == 0) { 2861af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2862ea022d16SRodney W. Grimes goto out; 2863ea022d16SRodney W. Grimes } 2864ea022d16SRodney W. Grimes perror_reply(550, whichf); 2865ea022d16SRodney W. Grimes if (dout != NULL) { 2866ea022d16SRodney W. Grimes (void) fclose(dout); 2867ea022d16SRodney W. Grimes transflag = 0; 2868ea022d16SRodney W. Grimes data = -1; 2869ea022d16SRodney W. Grimes pdata = -1; 2870ea022d16SRodney W. Grimes } 2871ea022d16SRodney W. Grimes goto out; 2872ea022d16SRodney W. Grimes } 2873ea022d16SRodney W. Grimes 2874ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2875ea022d16SRodney W. Grimes if (dout == NULL) { 2876ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2877ea022d16SRodney W. Grimes if (dout == NULL) 2878ea022d16SRodney W. Grimes goto out; 2879ea022d16SRodney W. Grimes transflag++; 2880ea022d16SRodney W. Grimes } 2881ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2882ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2883ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2884ea022d16SRodney W. Grimes continue; 2885ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2886ea022d16SRodney W. Grimes continue; 2887ea022d16SRodney W. Grimes 2888ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 2889ea022d16SRodney W. Grimes continue; 2890ea022d16SRodney W. Grimes 2891ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 2892ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 2893ea022d16SRodney W. Grimes 28944b82fc95SYaroslav Tykhiy if (recvurg) { 28954b82fc95SYaroslav Tykhiy myoob(); 28964b82fc95SYaroslav Tykhiy recvurg = 0; 28974b82fc95SYaroslav Tykhiy transflag = 0; 28984b82fc95SYaroslav Tykhiy goto out; 28994b82fc95SYaroslav Tykhiy } 29004b82fc95SYaroslav Tykhiy 2901ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2902ea022d16SRodney W. Grimes continue; 2903ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2904ea022d16SRodney W. Grimes dir->d_namlen == 2) 2905ea022d16SRodney W. Grimes continue; 2906ea022d16SRodney W. Grimes 2907e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 2908e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 2909ea022d16SRodney W. Grimes 2910ea022d16SRodney W. Grimes /* 2911ea022d16SRodney W. Grimes * We have to do a stat to insure it's 2912ea022d16SRodney W. Grimes * not a directory or special file. 2913ea022d16SRodney W. Grimes */ 2914ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 2915ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 2916ea022d16SRodney W. Grimes if (dout == NULL) { 2917ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 2918ea022d16SRodney W. Grimes "w"); 2919ea022d16SRodney W. Grimes if (dout == NULL) 2920ea022d16SRodney W. Grimes goto out; 2921ea022d16SRodney W. Grimes transflag++; 2922ea022d16SRodney W. Grimes } 2923ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 2924ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 2925ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2926ea022d16SRodney W. Grimes else 2927ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 2928ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2929ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 2930ea022d16SRodney W. Grimes } 2931ea022d16SRodney W. Grimes } 2932ea022d16SRodney W. Grimes (void) closedir(dirp); 2933ea022d16SRodney W. Grimes } 2934ea022d16SRodney W. Grimes 2935ea022d16SRodney W. Grimes if (dout == NULL) 2936ea022d16SRodney W. Grimes reply(550, "No files found."); 2937ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 2938ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 2939ea022d16SRodney W. Grimes else 2940ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 2941ea022d16SRodney W. Grimes 2942ea022d16SRodney W. Grimes transflag = 0; 2943ea022d16SRodney W. Grimes if (dout != NULL) 2944ea022d16SRodney W. Grimes (void) fclose(dout); 2945ea022d16SRodney W. Grimes data = -1; 2946ea022d16SRodney W. Grimes pdata = -1; 2947ea022d16SRodney W. Grimes out: 2948ea022d16SRodney W. Grimes if (freeglob) { 2949ea022d16SRodney W. Grimes freeglob = 0; 2950ea022d16SRodney W. Grimes globfree(&gl); 2951ea022d16SRodney W. Grimes } 2952ea022d16SRodney W. Grimes } 2953ea022d16SRodney W. Grimes 2954cf09a206SDavid Greenman void 2955e4bc453cSWarner Losh reapchild(int signo) 2956cf09a206SDavid Greenman { 2957cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 2958cf09a206SDavid Greenman } 2959cf09a206SDavid Greenman 2960b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2961ea022d16SRodney W. Grimes /* 2962ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2963ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2964ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2965ea022d16SRodney W. Grimes */ 2966ea022d16SRodney W. Grimes void 2967ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2968ea022d16SRodney W. Grimes { 2969ea022d16SRodney W. Grimes int i; 2970ea022d16SRodney W. Grimes va_list ap; 2971ea022d16SRodney W. Grimes char *p, *bp, ch; 2972ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2973ea022d16SRodney W. Grimes 2974ea022d16SRodney W. Grimes va_start(ap, fmt); 2975ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2976ea022d16SRodney W. Grimes 2977ea022d16SRodney W. Grimes /* make ps print our process name */ 2978ea022d16SRodney W. Grimes p = Argv[0]; 2979ea022d16SRodney W. Grimes *p++ = '-'; 2980ea022d16SRodney W. Grimes 2981ea022d16SRodney W. Grimes i = strlen(buf); 2982ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 2983ea022d16SRodney W. Grimes i = LastArgv - p - 2; 2984ea022d16SRodney W. Grimes buf[i] = '\0'; 2985ea022d16SRodney W. Grimes } 2986ea022d16SRodney W. Grimes bp = buf; 2987ea022d16SRodney W. Grimes while (ch = *bp++) 2988ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 2989ea022d16SRodney W. Grimes *p++ = ch; 2990ea022d16SRodney W. Grimes while (p < LastArgv) 2991ea022d16SRodney W. Grimes *p++ = ' '; 2992ea022d16SRodney W. Grimes } 2993b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 29943eb568f2SGuido van Rooij 299561f891a6SPaul Traina static void 2996e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start) 29973eb568f2SGuido van Rooij { 29983eb568f2SGuido van Rooij char buf[1024]; 29993eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 3000158a00b2SJohn Birrell time_t now; 30013eb568f2SGuido van Rooij 30023eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 30033eb568f2SGuido van Rooij time(&now); 3004e4a71114SAndrey A. Chernov snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n", 30053eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 3006e4a71114SAndrey A. Chernov path, name, (long long)size, 3007e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 30083eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 30093eb568f2SGuido van Rooij } 30103eb568f2SGuido van Rooij } 3011e4648f05SYaroslav Tykhiy 3012e4648f05SYaroslav Tykhiy static char * 3013e4648f05SYaroslav Tykhiy doublequote(char *s) 3014e4648f05SYaroslav Tykhiy { 3015e4648f05SYaroslav Tykhiy int n; 3016e4648f05SYaroslav Tykhiy char *p, *s2; 3017e4648f05SYaroslav Tykhiy 3018e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++) 3019e4648f05SYaroslav Tykhiy if (*p == '"') 3020e4648f05SYaroslav Tykhiy n++; 3021e4648f05SYaroslav Tykhiy 3022e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL) 3023e4648f05SYaroslav Tykhiy return (NULL); 3024e4648f05SYaroslav Tykhiy 3025e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) { 3026e4648f05SYaroslav Tykhiy if ((*p = *s) == '"') 3027e4648f05SYaroslav Tykhiy *(++p) = '"'; 3028e4648f05SYaroslav Tykhiy } 3029e4648f05SYaroslav Tykhiy *p = '\0'; 3030e4648f05SYaroslav Tykhiy 3031e4648f05SYaroslav Tykhiy return (s2); 3032e4648f05SYaroslav Tykhiy } 3033