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; 11863591ba5SYaroslav Tykhiy int dataport; 119ea022d16SRodney W. Grimes int logged_in; 120ea022d16SRodney W. Grimes struct passwd *pw; 121618b0bbaSMark Murray int ftpdebug; 122ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 123ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 124ea022d16SRodney W. Grimes int logging; 1254c450ad7SPaul Traina int restricted_data_ports = 1; 126a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1275a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 128ea022d16SRodney W. Grimes int guest; 129a5a4544eSPaul Traina int dochroot; 1305d7e0128SYaroslav Tykhiy int dowtmp = 1; 1313eb568f2SGuido van Rooij int stats; 1323eb568f2SGuido van Rooij int statfd = -1; 133ea022d16SRodney W. Grimes int type; 134ea022d16SRodney W. Grimes int form; 135ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 136ea022d16SRodney W. Grimes int mode; 137ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 138ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 139a4b77a2aSPoul-Henning Kamp int readonly=0; /* Server is in readonly mode. */ 140a4b77a2aSPoul-Henning Kamp int noepsv=0; /* EPSV command is disabled. */ 14162513e76SNik Clayton int noretr=0; /* RETR command is disabled. */ 1421cc9f0bbSSheldon Hearn int noguestretr=0; /* RETR command is disabled for anon users. */ 143d186bb12SMatthew N. Dodd int noguestmkd=0; /* MKD command is disabled for anon users. */ 144a117c345SYaroslav Tykhiy int noguestmod=1; /* anon users may not modify existing files. */ 14562513e76SNik Clayton 1464b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg; 147ea022d16SRodney W. Grimes sig_atomic_t transflag; 148ea022d16SRodney W. Grimes off_t file_size; 149ea022d16SRodney W. Grimes off_t byte_count; 150ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 151ea022d16SRodney W. Grimes #undef CMASK 152ea022d16SRodney W. Grimes #define CMASK 027 153ea022d16SRodney W. Grimes #endif 154ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 155ea022d16SRodney W. Grimes char tmpline[7]; 156ea4e54b9SDavid Nugent char *hostname; 157ad442344SDima Dorfman int epsvall = 0; 158ad442344SDima Dorfman 1590512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 160ea4e54b9SDavid Nugent char *ftpuser; 161ea4e54b9SDavid Nugent 162ea4e54b9SDavid Nugent static struct ftphost { 163ea4e54b9SDavid Nugent struct ftphost *next; 164f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 165ea4e54b9SDavid Nugent char *hostname; 166ea4e54b9SDavid Nugent char *anonuser; 167ea4e54b9SDavid Nugent char *statfile; 168ea4e54b9SDavid Nugent char *welcome; 169ea4e54b9SDavid Nugent char *loginmsg; 170ea4e54b9SDavid Nugent } *thishost, *firsthost; 171ea4e54b9SDavid Nugent 172ea4e54b9SDavid Nugent #endif 1739e9a43bdSBrian Somers char remotehost[MAXHOSTNAMELEN]; 1743eb568f2SGuido van Rooij char *ident = NULL; 175a5a4544eSPaul Traina 176a5a4544eSPaul Traina static char ttyline[20]; 177a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 178a5a4544eSPaul Traina 1795bc9d93dSMark Murray #ifdef USE_PAM 180e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*); 1815bc9d93dSMark Murray pam_handle_t *pamh = NULL; 18247499ecdSAndrey A. Chernov #endif 183fa1746c9SMark Murray 184fa1746c9SMark Murray static struct opie opiedata; 185fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 18647499ecdSAndrey A. Chernov static int pwok; 1879aca17cbSMark Murray 188105a3c98SJulian Elischer char *pid_file = NULL; 189105a3c98SJulian Elischer 190ea022d16SRodney W. Grimes /* 1916d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 1926d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 1936d10cb2fSJonathan Lemon */ 1946d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 1956d10cb2fSJonathan Lemon # 1966d10cb2fSJonathan Lemon 1976d10cb2fSJonathan Lemon /* 198ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 199ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 200ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 201ea022d16SRodney W. Grimes */ 202ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 203ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 204ea022d16SRodney W. Grimes 205ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 206ea022d16SRodney W. Grimes int swaitint = SWAITINT; 207ea022d16SRodney W. Grimes 208ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 209b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 210ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 211ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 212b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 213ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 214ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 215ea022d16SRodney W. Grimes 216ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 217ea022d16SRodney W. Grimes if (logging > 1) \ 218ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 219ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 220ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 221ea022d16SRodney W. Grimes if (logging > 1) \ 222ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 223ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 224ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 225ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 226ea022d16SRodney W. Grimes if (logging > 1) { \ 227ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 228ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 229ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 230ea022d16SRodney W. Grimes else \ 231ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 232ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 233ea022d16SRodney W. Grimes } 234ea022d16SRodney W. Grimes 235ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 236e4bc453cSWarner Losh static void inithosts(void); 237e4bc453cSWarner Losh static void selecthost(union sockunion *); 238ea4e54b9SDavid Nugent #endif 239e4bc453cSWarner Losh static void ack(char *); 240e4bc453cSWarner Losh static void sigurg(int); 241e4bc453cSWarner Losh static void myoob(void); 242e4bc453cSWarner Losh static int checkuser(char *, char *, int); 243e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *); 244e4bc453cSWarner Losh static void dolog(struct sockaddr *); 245e4bc453cSWarner Losh static char *curdir(void); 246e4bc453cSWarner Losh static void end_login(void); 247e4bc453cSWarner Losh static FILE *getdatasock(char *); 248a117c345SYaroslav Tykhiy static int guniquefd(char *, char **); 249e4bc453cSWarner Losh static void lostconn(int); 250e4bc453cSWarner Losh static void sigquit(int); 251e4bc453cSWarner Losh static int receive_data(FILE *, FILE *); 252e4bc453cSWarner Losh static int send_data(FILE *, FILE *, off_t, off_t, int); 253ea022d16SRodney W. Grimes static struct passwd * 254e4bc453cSWarner Losh sgetpwnam(char *); 255e4bc453cSWarner Losh static char *sgetsave(char *); 256e4bc453cSWarner Losh static void reapchild(int); 257e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t); 258e4648f05SYaroslav Tykhiy static char *doublequote(char *); 259ea022d16SRodney W. Grimes 260ea022d16SRodney W. Grimes static char * 261e4bc453cSWarner Losh curdir(void) 262ea022d16SRodney W. Grimes { 263ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 264ea022d16SRodney W. Grimes 265ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 266ea022d16SRodney W. Grimes return (""); 267ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 268ea022d16SRodney W. Grimes strcat(path, "/"); 269ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 270ea022d16SRodney W. Grimes return (guest ? path+1 : path); 271ea022d16SRodney W. Grimes } 272ea022d16SRodney W. Grimes 273ea022d16SRodney W. Grimes int 274e4bc453cSWarner Losh main(int argc, char *argv[], char **envp) 275ea022d16SRodney W. Grimes { 276ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 277ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 278ea022d16SRodney W. Grimes FILE *fd; 2794dd8b5abSYoshinobu Inoue int error; 2804dd8b5abSYoshinobu Inoue char *bindname = NULL; 28163591ba5SYaroslav Tykhiy const char *bindport = "ftp"; 2824dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2834dd8b5abSYoshinobu Inoue int enable_v4 = 0; 2844b82fc95SYaroslav Tykhiy struct sigaction sa; 285ea022d16SRodney W. Grimes 28634d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 2874b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask); 2884b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 28934d1ba5cSAndrey A. Chernov 290b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 291ea022d16SRodney W. Grimes /* 292ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 293ea022d16SRodney W. Grimes */ 294ea022d16SRodney W. Grimes Argv = argv; 295ea022d16SRodney W. Grimes while (*envp) 296ea022d16SRodney W. Grimes envp++; 297ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 298b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 299ea022d16SRodney W. Grimes 3003eb568f2SGuido van Rooij 30163591ba5SYaroslav Tykhiy while ((ch = getopt(argc, argv, "46a:AdDElmMoOp:P:rRSt:T:u:UvW")) != -1) { 302ea022d16SRodney W. Grimes switch (ch) { 3030e063efeSYaroslav Tykhiy case '4': 3040e063efeSYaroslav Tykhiy enable_v4 = 1; 3050e063efeSYaroslav Tykhiy if (family == AF_UNSPEC) 3060e063efeSYaroslav Tykhiy family = AF_INET; 3070e063efeSYaroslav Tykhiy break; 3080e063efeSYaroslav Tykhiy 3090e063efeSYaroslav Tykhiy case '6': 3100e063efeSYaroslav Tykhiy family = AF_INET6; 3110e063efeSYaroslav Tykhiy break; 3120e063efeSYaroslav Tykhiy 3130e063efeSYaroslav Tykhiy case 'a': 3140e063efeSYaroslav Tykhiy bindname = optarg; 3150e063efeSYaroslav Tykhiy break; 3160e063efeSYaroslav Tykhiy 3170e063efeSYaroslav Tykhiy case 'A': 3180e063efeSYaroslav Tykhiy anon_only = 1; 319cf09a206SDavid Greenman break; 320cf09a206SDavid Greenman 321ea022d16SRodney W. Grimes case 'd': 322618b0bbaSMark Murray ftpdebug++; 323ea022d16SRodney W. Grimes break; 324ea022d16SRodney W. Grimes 3250e063efeSYaroslav Tykhiy case 'D': 3260e063efeSYaroslav Tykhiy daemon_mode++; 3270e063efeSYaroslav Tykhiy break; 3280e063efeSYaroslav Tykhiy 329a4b77a2aSPoul-Henning Kamp case 'E': 330a4b77a2aSPoul-Henning Kamp noepsv = 1; 331a4b77a2aSPoul-Henning Kamp break; 332a4b77a2aSPoul-Henning Kamp 333ea022d16SRodney W. Grimes case 'l': 334ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 335ea022d16SRodney W. Grimes break; 336ea022d16SRodney W. Grimes 337a117c345SYaroslav Tykhiy case 'm': 338a117c345SYaroslav Tykhiy noguestmod = 0; 339a117c345SYaroslav Tykhiy break; 340a117c345SYaroslav Tykhiy 3410e063efeSYaroslav Tykhiy case 'M': 3420e063efeSYaroslav Tykhiy noguestmkd = 1; 3430e063efeSYaroslav Tykhiy break; 3440e063efeSYaroslav Tykhiy 3450e063efeSYaroslav Tykhiy case 'o': 3460e063efeSYaroslav Tykhiy noretr = 1; 3470e063efeSYaroslav Tykhiy break; 3480e063efeSYaroslav Tykhiy 3490e063efeSYaroslav Tykhiy case 'O': 3500e063efeSYaroslav Tykhiy noguestretr = 1; 3510e063efeSYaroslav Tykhiy break; 3520e063efeSYaroslav Tykhiy 3530e063efeSYaroslav Tykhiy case 'p': 3540e063efeSYaroslav Tykhiy pid_file = optarg; 3550e063efeSYaroslav Tykhiy break; 3560e063efeSYaroslav Tykhiy 35763591ba5SYaroslav Tykhiy case 'P': 35863591ba5SYaroslav Tykhiy bindport = optarg; 35963591ba5SYaroslav Tykhiy break; 36063591ba5SYaroslav Tykhiy 361a4b77a2aSPoul-Henning Kamp case 'r': 362a4b77a2aSPoul-Henning Kamp readonly = 1; 363a4b77a2aSPoul-Henning Kamp break; 364a4b77a2aSPoul-Henning Kamp 365a5a4544eSPaul Traina case 'R': 366a5a4544eSPaul Traina paranoid = 0; 367a5a4544eSPaul Traina break; 368a5a4544eSPaul Traina 369a5a4544eSPaul Traina case 'S': 370a5a4544eSPaul Traina stats++; 371a5a4544eSPaul Traina break; 372a5a4544eSPaul Traina 373ea022d16SRodney W. Grimes case 't': 374ea022d16SRodney W. Grimes timeout = atoi(optarg); 375ea022d16SRodney W. Grimes if (maxtimeout < timeout) 376ea022d16SRodney W. Grimes maxtimeout = timeout; 377ea022d16SRodney W. Grimes break; 378a5a4544eSPaul Traina 3790e063efeSYaroslav Tykhiy case 'T': 3800e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg); 3810e063efeSYaroslav Tykhiy if (timeout > maxtimeout) 3820e063efeSYaroslav Tykhiy timeout = maxtimeout; 383105a3c98SJulian Elischer break; 384105a3c98SJulian Elischer 385ea022d16SRodney W. Grimes case 'u': 386ea022d16SRodney W. Grimes { 387ea022d16SRodney W. Grimes long val = 0; 388ea022d16SRodney W. Grimes 389ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 390ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 391ea022d16SRodney W. Grimes warnx("bad value for -u"); 392ea022d16SRodney W. Grimes else 393ea022d16SRodney W. Grimes defumask = val; 394ea022d16SRodney W. Grimes break; 395ea022d16SRodney W. Grimes } 3960e063efeSYaroslav Tykhiy case 'U': 3970e063efeSYaroslav Tykhiy restricted_data_ports = 0; 3985a392aecSTorsten Blum break; 399ea022d16SRodney W. Grimes 400ea022d16SRodney W. Grimes case 'v': 40193bd9dc5SYaroslav Tykhiy ftpdebug++; 402ea022d16SRodney W. Grimes break; 403ea022d16SRodney W. Grimes 4045d7e0128SYaroslav Tykhiy case 'W': 4055d7e0128SYaroslav Tykhiy dowtmp = 0; 4065d7e0128SYaroslav Tykhiy break; 4075d7e0128SYaroslav Tykhiy 408ea022d16SRodney W. Grimes default: 409ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 410ea022d16SRodney W. Grimes break; 411ea022d16SRodney W. Grimes } 412ea022d16SRodney W. Grimes } 413cf09a206SDavid Greenman 414ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 415ea4e54b9SDavid Nugent inithosts(); 416ea4e54b9SDavid Nugent #endif 417ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 418cf09a206SDavid Greenman 419cf09a206SDavid Greenman /* 420cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 421cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 422cf09a206SDavid Greenman */ 423cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 424cf09a206SDavid Greenman 425cf09a206SDavid Greenman if (daemon_mode) { 426cf09a206SDavid Greenman int ctl_sock, fd; 4274dd8b5abSYoshinobu Inoue struct addrinfo hints, *res; 428cf09a206SDavid Greenman 429cf09a206SDavid Greenman /* 430cf09a206SDavid Greenman * Detach from parent. 431cf09a206SDavid Greenman */ 432cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 433cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 434cf09a206SDavid Greenman exit(1); 435cf09a206SDavid Greenman } 4364b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild; 4374b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 4384dd8b5abSYoshinobu Inoue /* init bind_sa */ 4394dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 4404dd8b5abSYoshinobu Inoue 4414dd8b5abSYoshinobu Inoue hints.ai_family = family == AF_UNSPEC ? AF_INET : family; 4424dd8b5abSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 4434dd8b5abSYoshinobu Inoue hints.ai_protocol = 0; 4444dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 44563591ba5SYaroslav Tykhiy error = getaddrinfo(bindname, bindport, &hints, &res); 4464dd8b5abSYoshinobu Inoue if (error) { 4474dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) { 4484dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 44963591ba5SYaroslav Tykhiy error = getaddrinfo(bindname, bindport, &hints, 4504dd8b5abSYoshinobu Inoue &res); 4514dd8b5abSYoshinobu Inoue } 4524dd8b5abSYoshinobu Inoue } 4534dd8b5abSYoshinobu Inoue if (error) { 4543fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", gai_strerror(error)); 4554dd8b5abSYoshinobu Inoue if (error == EAI_SYSTEM) 4563fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", strerror(errno)); 4574dd8b5abSYoshinobu Inoue exit(1); 4584dd8b5abSYoshinobu Inoue } 4594dd8b5abSYoshinobu Inoue if (res->ai_addr == NULL) { 4604dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 461cf09a206SDavid Greenman exit(1); 4622310b8c6SRuslan Ermilov } else 4632310b8c6SRuslan Ermilov family = res->ai_addr->sa_family; 464cf09a206SDavid Greenman /* 465cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 466cf09a206SDavid Greenman * listening. 467cf09a206SDavid Greenman */ 4684dd8b5abSYoshinobu Inoue ctl_sock = socket(family, SOCK_STREAM, 0); 469cf09a206SDavid Greenman if (ctl_sock < 0) { 470cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 471cf09a206SDavid Greenman exit(1); 472cf09a206SDavid Greenman } 473cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 47457d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 475406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, 476406d1ae9SYaroslav Tykhiy "control setsockopt (SO_REUSEADDR): %m"); 4774dd8b5abSYoshinobu Inoue if (family == AF_INET6 && enable_v4 == 0) { 478fc99a00cSHajimu UMEMOTO if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY, 47957d4ef07SYaroslav Tykhiy &on, sizeof (on)) < 0) 480406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, 481fc99a00cSHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m"); 4824dd8b5abSYoshinobu Inoue } 4834dd8b5abSYoshinobu Inoue memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len); 4844dd8b5abSYoshinobu Inoue if (bind(ctl_sock, (struct sockaddr *)&server_addr, 4854dd8b5abSYoshinobu Inoue server_addr.su_len) < 0) { 486cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 487cf09a206SDavid Greenman exit(1); 488cf09a206SDavid Greenman } 489cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 490cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 491cf09a206SDavid Greenman exit(1); 492cf09a206SDavid Greenman } 493cf09a206SDavid Greenman /* 494105a3c98SJulian Elischer * Atomically write process ID 495105a3c98SJulian Elischer */ 496105a3c98SJulian Elischer if (pid_file) 497105a3c98SJulian Elischer { 498105a3c98SJulian Elischer int fd; 499105a3c98SJulian Elischer char buf[20]; 500105a3c98SJulian Elischer 501105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 502105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 50385966371SWarner Losh if (fd < 0) { 504105a3c98SJulian Elischer if (errno == EAGAIN) 505105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 506105a3c98SJulian Elischer else 507105a3c98SJulian Elischer err(1, "%s", pid_file); 50885966371SWarner Losh } 509105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 510105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 511105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 512105a3c98SJulian Elischer err(1, "%s: write", pid_file); 513105a3c98SJulian Elischer /* Leave the pid file open and locked */ 514105a3c98SJulian Elischer } 515105a3c98SJulian Elischer /* 516cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 517cf09a206SDavid Greenman * children to handle them. 518cf09a206SDavid Greenman */ 519cf09a206SDavid Greenman while (1) { 5204dd8b5abSYoshinobu Inoue addrlen = server_addr.su_len; 521cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 522cf09a206SDavid Greenman if (fork() == 0) { 523cf09a206SDavid Greenman /* child */ 524cf09a206SDavid Greenman (void) dup2(fd, 0); 525cf09a206SDavid Greenman (void) dup2(fd, 1); 526cf09a206SDavid Greenman close(ctl_sock); 527cf09a206SDavid Greenman break; 528cf09a206SDavid Greenman } 529cf09a206SDavid Greenman close(fd); 530cf09a206SDavid Greenman } 531cf09a206SDavid Greenman } else { 532cf09a206SDavid Greenman addrlen = sizeof(his_addr); 533cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 534cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 535cf09a206SDavid Greenman exit(1); 536cf09a206SDavid Greenman } 537cf09a206SDavid Greenman } 538cf09a206SDavid Greenman 5394b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL; 5404b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 5414b82fc95SYaroslav Tykhiy 5424b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg; 5434b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 5444b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL); 5454b82fc95SYaroslav Tykhiy 5464b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */ 5474b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 5484b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit; 5494b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL); 5504b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL); 5514b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL); 5524b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL); 5534b82fc95SYaroslav Tykhiy 5544b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn; 5554b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL); 556ea022d16SRodney W. Grimes 557cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 558cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 559cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 560cf09a206SDavid Greenman exit(1); 561cf09a206SDavid Greenman } 56263591ba5SYaroslav Tykhiy dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */ 563ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 564ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5654dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 566ea4e54b9SDavid Nugent #endif 567cf09a206SDavid Greenman #ifdef IP_TOS 5684dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5694dd8b5abSYoshinobu Inoue { 570cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 57157d4ef07SYaroslav Tykhiy if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 572406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 5734dd8b5abSYoshinobu Inoue } 574cf09a206SDavid Greenman #endif 575dadb9fb3SDavid Greenman /* 576dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 577dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 578dadb9fb3SDavid Greenman */ 579dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 580406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 581dadb9fb3SDavid Greenman 5824dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 583cf09a206SDavid Greenman 584a5a4544eSPaul Traina /* set this here so klogin can use it... */ 585e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 586a5a4544eSPaul Traina 587ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 588ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 58957d4ef07SYaroslav Tykhiy if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 590406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 591ea022d16SRodney W. Grimes #endif 592ea022d16SRodney W. Grimes 593ea022d16SRodney W. Grimes #ifdef F_SETOWN 594ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 595ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 596ea022d16SRodney W. Grimes #endif 5974dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 598ea022d16SRodney W. Grimes /* 599ea022d16SRodney W. Grimes * Set up default state 600ea022d16SRodney W. Grimes */ 601ea022d16SRodney W. Grimes data = -1; 602ea022d16SRodney W. Grimes type = TYPE_A; 603ea022d16SRodney W. Grimes form = FORM_N; 604ea022d16SRodney W. Grimes stru = STRU_F; 605ea022d16SRodney W. Grimes mode = MODE_S; 606ea022d16SRodney W. Grimes tmpline[0] = '\0'; 607ea022d16SRodney W. Grimes 608ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 609ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 610ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 611ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 612ea022d16SRodney W. Grimes *cp = '\0'; 613ea022d16SRodney W. Grimes lreply(530, "%s", line); 614ea022d16SRodney W. Grimes } 615ea022d16SRodney W. Grimes (void) fflush(stdout); 616ea022d16SRodney W. Grimes (void) fclose(fd); 617ea022d16SRodney W. Grimes reply(530, "System not available."); 618ea022d16SRodney W. Grimes exit(0); 619ea022d16SRodney W. Grimes } 620ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 621ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 622ea4e54b9SDavid Nugent #else 623ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 624ea4e54b9SDavid Nugent #endif 625ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 626ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 627ea022d16SRodney W. Grimes *cp = '\0'; 628ea022d16SRodney W. Grimes lreply(220, "%s", line); 629ea022d16SRodney W. Grimes } 630ea022d16SRodney W. Grimes (void) fflush(stdout); 631ea022d16SRodney W. Grimes (void) fclose(fd); 632ea022d16SRodney W. Grimes /* reply(220,) must follow */ 633ea022d16SRodney W. Grimes } 634ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6350512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 636618b0bbaSMark Murray fatalerror("Ran out of memory."); 6379e9a43bdSBrian Somers (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 6389e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 639ea4e54b9SDavid Nugent #endif 640ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 641ea022d16SRodney W. Grimes for (;;) 642ea022d16SRodney W. Grimes (void) yyparse(); 643ea022d16SRodney W. Grimes /* NOTREACHED */ 644ea022d16SRodney W. Grimes } 645ea022d16SRodney W. Grimes 646ea022d16SRodney W. Grimes static void 647e4bc453cSWarner Losh lostconn(int signo) 648ea022d16SRodney W. Grimes { 649ea022d16SRodney W. Grimes 650618b0bbaSMark Murray if (ftpdebug) 651ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 652e02897faSPhilippe Charnier dologout(1); 653ea022d16SRodney W. Grimes } 654ea022d16SRodney W. Grimes 6554b82fc95SYaroslav Tykhiy static void 656e4bc453cSWarner Losh sigquit(int signo) 6574b82fc95SYaroslav Tykhiy { 6584b82fc95SYaroslav Tykhiy 6594b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo); 6604b82fc95SYaroslav Tykhiy dologout(1); 6614b82fc95SYaroslav Tykhiy } 6624b82fc95SYaroslav Tykhiy 663ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 664ea4e54b9SDavid Nugent /* 665ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 666ea4e54b9SDavid Nugent */ 667ea4e54b9SDavid Nugent 668ea4e54b9SDavid Nugent static void 669e4bc453cSWarner Losh inithosts(void) 670ea4e54b9SDavid Nugent { 67155b54aa7SYaroslav Tykhiy int insert; 672737d08f3SYaroslav Tykhiy size_t len; 673ea4e54b9SDavid Nugent FILE *fp; 674737d08f3SYaroslav Tykhiy char *cp, *mp, *line; 675737d08f3SYaroslav Tykhiy char *hostname; 67655b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 677ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 6784dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 679ea4e54b9SDavid Nugent 680ea4e54b9SDavid Nugent /* 681ea4e54b9SDavid Nugent * Fill in the default host information 682ea4e54b9SDavid Nugent */ 683737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 684618b0bbaSMark Murray fatalerror("Ran out of memory."); 685737d08f3SYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN) < 0) 686737d08f3SYaroslav Tykhiy hostname[0] = '\0'; 687737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0'; 688737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 689737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 690737d08f3SYaroslav Tykhiy hrp->hostname = hostname; 691f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 6924dd8b5abSYoshinobu Inoue 6934dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 6944dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 6954dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 696b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 697f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 698ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 699ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 700ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 701ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 702ea4e54b9SDavid Nugent hrp->next = NULL; 703ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 704ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 705ec009cf0SYaroslav Tykhiy int addrsize, gothost; 7064dd8b5abSYoshinobu Inoue void *addr; 7074dd8b5abSYoshinobu Inoue struct hostent *hp; 7084dd8b5abSYoshinobu Inoue 709737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) { 7104dd8b5abSYoshinobu Inoue int i, hp_error; 711ea4e54b9SDavid Nugent 712737d08f3SYaroslav Tykhiy /* skip comments */ 713737d08f3SYaroslav Tykhiy if (line[0] == '#') 714ea4e54b9SDavid Nugent continue; 715737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 716737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 717737d08f3SYaroslav Tykhiy mp = NULL; 718737d08f3SYaroslav Tykhiy } else { 719737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 720737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 721737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 722737d08f3SYaroslav Tykhiy mp[len] = '\0'; 723737d08f3SYaroslav Tykhiy line = mp; 724ea4e54b9SDavid Nugent } 725ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 726737d08f3SYaroslav Tykhiy /* skip empty lines */ 727737d08f3SYaroslav Tykhiy if (cp == NULL) 728737d08f3SYaroslav Tykhiy goto nextline; 72955b54aa7SYaroslav Tykhiy vhost = cp; 73055b54aa7SYaroslav Tykhiy 73155b54aa7SYaroslav Tykhiy /* set defaults */ 73255b54aa7SYaroslav Tykhiy anonuser = "ftp"; 73355b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE; 73455b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME; 73555b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG; 73655b54aa7SYaroslav Tykhiy 73755b54aa7SYaroslav Tykhiy /* 73855b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info 73955b54aa7SYaroslav Tykhiy * for all the addresses associated with 74055b54aa7SYaroslav Tykhiy * the virtual host name. 74155b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special: 74255b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed 74355b54aa7SYaroslav Tykhiy * later, after we know its canonical form. 74455b54aa7SYaroslav Tykhiy */ 74555b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 74655b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp))) 74755b54aa7SYaroslav Tykhiy switch (i) { 74855b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */ 74955b54aa7SYaroslav Tykhiy anonuser = cp; 75055b54aa7SYaroslav Tykhiy break; 75155b54aa7SYaroslav Tykhiy case 2: /* statistics file */ 75255b54aa7SYaroslav Tykhiy statfile = cp; 75355b54aa7SYaroslav Tykhiy break; 75455b54aa7SYaroslav Tykhiy case 3: /* welcome message */ 75555b54aa7SYaroslav Tykhiy welcome = cp; 75655b54aa7SYaroslav Tykhiy break; 75755b54aa7SYaroslav Tykhiy case 4: /* login message */ 75855b54aa7SYaroslav Tykhiy loginmsg = cp; 75955b54aa7SYaroslav Tykhiy break; 76055b54aa7SYaroslav Tykhiy default: /* programming error */ 76155b54aa7SYaroslav Tykhiy abort(); 76255b54aa7SYaroslav Tykhiy /* NOTREACHED */ 76355b54aa7SYaroslav Tykhiy } 7644dd8b5abSYoshinobu Inoue 7654dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 7664dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 7674dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 768b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 769737d08f3SYaroslav Tykhiy goto nextline; 7704dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 771b535a9bfSDavid Nugent ai = ai->ai_next) { 7724dd8b5abSYoshinobu Inoue 773b535a9bfSDavid Nugent gothost = 0; 774ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 775f38c6cadSYoshinobu Inoue struct addrinfo *hi; 776f38c6cadSYoshinobu Inoue 777f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 778f38c6cadSYoshinobu Inoue hi = hi->ai_next) 779f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 780f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7814dd8b5abSYoshinobu Inoue ai->ai_addr, 782b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 783b535a9bfSDavid Nugent gothost++; 784b535a9bfSDavid Nugent break; 785b535a9bfSDavid Nugent } 786b535a9bfSDavid Nugent if (gothost) 787ea4e54b9SDavid Nugent break; 788ea4e54b9SDavid Nugent } 789ea4e54b9SDavid Nugent if (hrp == NULL) { 790ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 791737d08f3SYaroslav Tykhiy goto nextline; 792b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 79355b54aa7SYaroslav Tykhiy insert = 1; 794f2fe752dSYaroslav Tykhiy } else { 7951f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res) 796b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 797f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */ 798f2fe752dSYaroslav Tykhiy } 799f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 800f38c6cadSYoshinobu Inoue 801ea4e54b9SDavid Nugent /* 802ea4e54b9SDavid Nugent * determine hostname to use. 8034dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 804ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 805ea4e54b9SDavid Nugent */ 8064dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 807f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 8084dd8b5abSYoshinobu Inoue case AF_INET: 8094b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 8104b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr); 8114dd8b5abSYoshinobu Inoue break; 8124dd8b5abSYoshinobu Inoue case AF_INET6: 8134b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 8144b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr); 8154dd8b5abSYoshinobu Inoue break; 8164dd8b5abSYoshinobu Inoue default: 8174dd8b5abSYoshinobu Inoue /* should not reach here */ 818f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 819f2fe752dSYaroslav Tykhiy if (insert) 820f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/ 821f2fe752dSYaroslav Tykhiy else 822f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/ 823737d08f3SYaroslav Tykhiy goto nextline; 8244dd8b5abSYoshinobu Inoue /* NOTREACHED */ 8254dd8b5abSYoshinobu Inoue } 82657d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize, 827f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 8284dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 82955b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) { 830ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 83155b54aa7SYaroslav Tykhiy vhost = hp->h_name; 832ea4e54b9SDavid Nugent else { 833ea4e54b9SDavid Nugent i = 0; 834ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 83555b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0) 836ea4e54b9SDavid Nugent ++i; 837ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 83855b54aa7SYaroslav Tykhiy vhost = hp->h_name; 839ea4e54b9SDavid Nugent } 840ea4e54b9SDavid Nugent } 841ea4e54b9SDavid Nugent } 842b1d8d5cdSYaroslav Tykhiy if (hrp->hostname && 843b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) { 844b1d8d5cdSYaroslav Tykhiy free(hrp->hostname); 845b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 846b1d8d5cdSYaroslav Tykhiy } 847b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL && 848f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) { 849f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 850f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */ 851f2fe752dSYaroslav Tykhiy if (hp) 852f2fe752dSYaroslav Tykhiy freehostent(hp); 85355b54aa7SYaroslav Tykhiy goto nextline; 854f2fe752dSYaroslav Tykhiy } 85555b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser; 85655b54aa7SYaroslav Tykhiy hrp->statfile = statfile; 85755b54aa7SYaroslav Tykhiy hrp->welcome = welcome; 85855b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg; 85955b54aa7SYaroslav Tykhiy if (insert) { 86055b54aa7SYaroslav Tykhiy hrp->next = NULL; 86155b54aa7SYaroslav Tykhiy lhrp->next = hrp; 86255b54aa7SYaroslav Tykhiy lhrp = hrp; 86355b54aa7SYaroslav Tykhiy } 864233c0f66SYaroslav Tykhiy if (hp) 8654dd8b5abSYoshinobu Inoue freehostent(hp); 8664dd8b5abSYoshinobu Inoue } 867737d08f3SYaroslav Tykhiy nextline: 868737d08f3SYaroslav Tykhiy if (mp) 869737d08f3SYaroslav Tykhiy free(mp); 870ea4e54b9SDavid Nugent } 871ea4e54b9SDavid Nugent (void) fclose(fp); 872ea4e54b9SDavid Nugent } 873ea4e54b9SDavid Nugent } 874ea4e54b9SDavid Nugent 875ea4e54b9SDavid Nugent static void 876e4bc453cSWarner Losh selecthost(union sockunion *su) 877ea4e54b9SDavid Nugent { 878ea4e54b9SDavid Nugent struct ftphost *hrp; 8794dd8b5abSYoshinobu Inoue u_int16_t port; 8804dd8b5abSYoshinobu Inoue #ifdef INET6 8814dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 8824dd8b5abSYoshinobu Inoue #endif 883f38c6cadSYoshinobu Inoue struct addrinfo *hi; 8844dd8b5abSYoshinobu Inoue 8854dd8b5abSYoshinobu Inoue #ifdef INET6 8864dd8b5abSYoshinobu Inoue /* 8874dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 8884dd8b5abSYoshinobu Inoue * specified in rfc2373. 8894dd8b5abSYoshinobu Inoue */ 8904dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 8914dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 8924dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 8934dd8b5abSYoshinobu Inoue #endif 894ea4e54b9SDavid Nugent 895ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 8964dd8b5abSYoshinobu Inoue port = su->su_port; 8974dd8b5abSYoshinobu Inoue su->su_port = 0; 898ea4e54b9SDavid Nugent while (hrp != NULL) { 899b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 900f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 901ea4e54b9SDavid Nugent thishost = hrp; 902ea4e54b9SDavid Nugent break; 903ea4e54b9SDavid Nugent } 9044dd8b5abSYoshinobu Inoue #ifdef INET6 9054dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 906f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 9074dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 908f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 9094dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 9104dd8b5abSYoshinobu Inoue thishost = hrp; 9114dd8b5abSYoshinobu Inoue break; 9124dd8b5abSYoshinobu Inoue } 9134dd8b5abSYoshinobu Inoue #endif 914f38c6cadSYoshinobu Inoue } 915ea4e54b9SDavid Nugent hrp = hrp->next; 916ea4e54b9SDavid Nugent } 9174dd8b5abSYoshinobu Inoue su->su_port = port; 918ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 919ea4e54b9SDavid Nugent hostname = thishost->hostname; 920ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 921ea4e54b9SDavid Nugent } 922ea4e54b9SDavid Nugent #endif 923ea4e54b9SDavid Nugent 924ea022d16SRodney W. Grimes /* 925ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 926ea022d16SRodney W. Grimes */ 927ea022d16SRodney W. Grimes static char * 928e4bc453cSWarner Losh sgetsave(char *s) 929ea022d16SRodney W. Grimes { 930ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 931ea022d16SRodney W. Grimes 932ea022d16SRodney W. Grimes if (new == NULL) { 933ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 934ea022d16SRodney W. Grimes dologout(1); 935ea022d16SRodney W. Grimes /* NOTREACHED */ 936ea022d16SRodney W. Grimes } 937ea022d16SRodney W. Grimes (void) strcpy(new, s); 938ea022d16SRodney W. Grimes return (new); 939ea022d16SRodney W. Grimes } 940ea022d16SRodney W. Grimes 941ea022d16SRodney W. Grimes /* 942ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 943ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 944ea022d16SRodney W. Grimes * (e.g., globbing). 945ea022d16SRodney W. Grimes */ 946ea022d16SRodney W. Grimes static struct passwd * 947e4bc453cSWarner Losh sgetpwnam(char *name) 948ea022d16SRodney W. Grimes { 949ea022d16SRodney W. Grimes static struct passwd save; 950ea022d16SRodney W. Grimes struct passwd *p; 951ea022d16SRodney W. Grimes 952ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 953ea022d16SRodney W. Grimes return (p); 954ea022d16SRodney W. Grimes if (save.pw_name) { 955ea022d16SRodney W. Grimes free(save.pw_name); 956ea022d16SRodney W. Grimes free(save.pw_passwd); 957ea022d16SRodney W. Grimes free(save.pw_gecos); 958ea022d16SRodney W. Grimes free(save.pw_dir); 959ea022d16SRodney W. Grimes free(save.pw_shell); 960ea022d16SRodney W. Grimes } 961ea022d16SRodney W. Grimes save = *p; 962ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 963ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 964ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 965ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 966ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 967ea022d16SRodney W. Grimes return (&save); 968ea022d16SRodney W. Grimes } 969ea022d16SRodney W. Grimes 970ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 971ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 97290906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 973ea022d16SRodney W. Grimes 974ea022d16SRodney W. Grimes /* 975ea022d16SRodney W. Grimes * USER command. 976ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 977ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 978ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 979ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 980ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 981ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 982ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 983ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 984ea022d16SRodney W. Grimes */ 985ea022d16SRodney W. Grimes void 986e4bc453cSWarner Losh user(char *name) 987ea022d16SRodney W. Grimes { 988ea022d16SRodney W. Grimes char *cp, *shell; 989ea022d16SRodney W. Grimes 990ea022d16SRodney W. Grimes if (logged_in) { 991ea022d16SRodney W. Grimes if (guest) { 992ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 993ea022d16SRodney W. Grimes return; 994a5a4544eSPaul Traina } else if (dochroot) { 995a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 996a5a4544eSPaul Traina return; 997ea022d16SRodney W. Grimes } 998ea022d16SRodney W. Grimes end_login(); 999ea022d16SRodney W. Grimes } 1000ea022d16SRodney W. Grimes 1001ea022d16SRodney W. Grimes guest = 0; 1002ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 10037edcb936SSteve Price if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 10047edcb936SSteve Price checkuser(_PATH_FTPUSERS, "anonymous", 0)) 1005ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1006ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1007ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 1008ea4e54b9SDavid Nugent #else 1009ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 1010ea4e54b9SDavid Nugent #endif 1011ea022d16SRodney W. Grimes guest = 1; 1012ea022d16SRodney W. Grimes askpasswd = 1; 1013ea022d16SRodney W. Grimes reply(331, 10142c60c54cSPaul Traina "Guest login ok, send your email address as password."); 1015ea022d16SRodney W. Grimes } else 1016ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 1017ea022d16SRodney W. Grimes if (!askpasswd && logging) 1018ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1019ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 1020ea022d16SRodney W. Grimes return; 1021ea022d16SRodney W. Grimes } 10225a392aecSTorsten Blum if (anon_only != 0) { 10235a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 10245a392aecSTorsten Blum return; 10255a392aecSTorsten Blum } 10265a392aecSTorsten Blum 10279aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 1028ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 1029ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 1030ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 1031ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 1032ea022d16SRodney W. Grimes break; 1033ea022d16SRodney W. Grimes endusershell(); 1034ea022d16SRodney W. Grimes 10357edcb936SSteve Price if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 1036ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1037ea022d16SRodney W. Grimes if (logging) 1038ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1039ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 1040ea022d16SRodney W. Grimes remotehost, name); 1041ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 1042ea022d16SRodney W. Grimes return; 1043ea022d16SRodney W. Grimes } 1044ea022d16SRodney W. Grimes } 1045ea022d16SRodney W. Grimes if (logging) 1046ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 104747499ecdSAndrey A. Chernov 104847499ecdSAndrey A. Chernov pwok = 0; 1049fa1746c9SMark Murray #ifdef USE_PAM 1050fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1051726040deSGuido van Rooij #endif 105247499ecdSAndrey A. Chernov if (opiechallenge(&opiedata, name, opieprompt) == 0) { 105347499ecdSAndrey A. Chernov pwok = (pw != NULL) && 105447499ecdSAndrey A. Chernov opieaccessfile(remotehost) && 105547499ecdSAndrey A. Chernov opiealways(pw->pw_dir); 105647499ecdSAndrey A. Chernov reply(331, "Response to %s %s for %s.", 105747499ecdSAndrey A. Chernov opieprompt, pwok ? "requested" : "required", name); 105847499ecdSAndrey A. Chernov } else { 105947499ecdSAndrey A. Chernov pwok = 1; 106047499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name); 106147499ecdSAndrey A. Chernov } 1062ea022d16SRodney W. Grimes askpasswd = 1; 1063ea022d16SRodney W. Grimes /* 1064ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 1065ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 1066ea022d16SRodney W. Grimes */ 1067ea022d16SRodney W. Grimes if (login_attempts) 1068ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 1069ea022d16SRodney W. Grimes } 1070ea022d16SRodney W. Grimes 1071ea022d16SRodney W. Grimes /* 1072a5a4544eSPaul Traina * Check if a user is in the file "fname" 1073ea022d16SRodney W. Grimes */ 1074ea022d16SRodney W. Grimes static int 1075e4bc453cSWarner Losh checkuser(char *fname, char *name, int pwset) 1076ea022d16SRodney W. Grimes { 1077ea022d16SRodney W. Grimes FILE *fd; 1078ea022d16SRodney W. Grimes int found = 0; 1079737d08f3SYaroslav Tykhiy size_t len; 1080737d08f3SYaroslav Tykhiy char *line, *mp, *p; 1081ea022d16SRodney W. Grimes 1082a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 1083737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) { 1084737d08f3SYaroslav Tykhiy /* skip comments */ 1085ea022d16SRodney W. Grimes if (line[0] == '#') 1086ea022d16SRodney W. Grimes continue; 1087737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 1088737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 1089737d08f3SYaroslav Tykhiy mp = NULL; 1090737d08f3SYaroslav Tykhiy } else { 1091737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 1092737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 1093737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 1094737d08f3SYaroslav Tykhiy mp[len] = '\0'; 1095737d08f3SYaroslav Tykhiy line = mp; 1096737d08f3SYaroslav Tykhiy } 1097737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */ 1098737d08f3SYaroslav Tykhiy p = strtok(line, " \t"); 1099737d08f3SYaroslav Tykhiy /* skip empty lines */ 1100737d08f3SYaroslav Tykhiy if (p == NULL) 1101737d08f3SYaroslav Tykhiy goto nextline; 110231fea7b8SDavid Nugent /* 110331fea7b8SDavid Nugent * if first chr is '@', check group membership 110431fea7b8SDavid Nugent */ 1105737d08f3SYaroslav Tykhiy if (p[0] == '@') { 110631fea7b8SDavid Nugent int i = 0; 110731fea7b8SDavid Nugent struct group *grp; 110831fea7b8SDavid Nugent 1109737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL) 1110737d08f3SYaroslav Tykhiy goto nextline; 11117edcb936SSteve Price /* 11127edcb936SSteve Price * Check user's default group 11137edcb936SSteve Price */ 11147edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 11157edcb936SSteve Price found = 1; 11167edcb936SSteve Price /* 11177edcb936SSteve Price * Check supplementary groups 11187edcb936SSteve Price */ 111931fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 112031fea7b8SDavid Nugent found = strcmp(name, 112131fea7b8SDavid Nugent grp->gr_mem[i++]) 112231fea7b8SDavid Nugent == 0; 1123ea022d16SRodney W. Grimes } 112431fea7b8SDavid Nugent /* 112531fea7b8SDavid Nugent * Otherwise, just check for username match 112631fea7b8SDavid Nugent */ 112731fea7b8SDavid Nugent else 1128737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0; 1129737d08f3SYaroslav Tykhiy nextline: 1130737d08f3SYaroslav Tykhiy if (mp) 1131737d08f3SYaroslav Tykhiy free(mp); 1132ea022d16SRodney W. Grimes } 1133ea022d16SRodney W. Grimes (void) fclose(fd); 1134ea022d16SRodney W. Grimes } 1135ea022d16SRodney W. Grimes return (found); 1136ea022d16SRodney W. Grimes } 1137ea022d16SRodney W. Grimes 1138ea022d16SRodney W. Grimes /* 1139ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1140ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1141ea022d16SRodney W. Grimes */ 1142ea022d16SRodney W. Grimes static void 1143e4bc453cSWarner Losh end_login(void) 1144ea022d16SRodney W. Grimes { 11455bc9d93dSMark Murray #ifdef USE_PAM 11465bc9d93dSMark Murray int e; 11475bc9d93dSMark Murray #endif 1148ea022d16SRodney W. Grimes 1149ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 11505d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) 115146948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 1152ea022d16SRodney W. Grimes pw = NULL; 1153b071c689SDavid Nugent #ifdef LOGIN_CAP 1154b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 1155d9e2c424SRobert Watson LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK| 1156d9e2c424SRobert Watson LOGIN_SETMAC); 1157b071c689SDavid Nugent #endif 11585bc9d93dSMark Murray #ifdef USE_PAM 11595bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 11605bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 11615bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 11625bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 11635bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 11645bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 11655bc9d93dSMark Murray pamh = NULL; 11665bc9d93dSMark Murray #endif 1167ea022d16SRodney W. Grimes logged_in = 0; 1168ea022d16SRodney W. Grimes guest = 0; 1169a5a4544eSPaul Traina dochroot = 0; 1170ea022d16SRodney W. Grimes } 1171ea022d16SRodney W. Grimes 11725bc9d93dSMark Murray #ifdef USE_PAM 11736c9134c0SMark Murray 11746c9134c0SMark Murray /* 11756c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 11766c9134c0SMark Murray * login.c 11776c9134c0SMark Murray */ 11786c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 11796c9134c0SMark Murray 11806c9134c0SMark Murray struct cred_t { 11816c9134c0SMark Murray const char *uname; /* user name */ 11826c9134c0SMark Murray const char *pass; /* password */ 11836c9134c0SMark Murray }; 11846c9134c0SMark Murray typedef struct cred_t cred_t; 11856c9134c0SMark Murray 11866c9134c0SMark Murray static int 11876c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 11886c9134c0SMark Murray struct pam_response **resp, void *appdata) 11896c9134c0SMark Murray { 11906c9134c0SMark Murray int i; 11916c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 119260769b19SDag-Erling Smørgrav struct pam_response *reply; 119360769b19SDag-Erling Smørgrav 119460769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply); 119560769b19SDag-Erling Smørgrav if (reply == NULL) 119660769b19SDag-Erling Smørgrav return PAM_BUF_ERR; 11976c9134c0SMark Murray 11986c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 11996c9134c0SMark Murray switch (msg[i]->msg_style) { 12006c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 12016c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12026c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 12036c9134c0SMark Murray /* PAM frees resp. */ 12046c9134c0SMark Murray break; 12056c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 12066c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12076c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 12086c9134c0SMark Murray /* PAM frees resp. */ 12096c9134c0SMark Murray break; 12106c9134c0SMark Murray case PAM_TEXT_INFO: 12116c9134c0SMark Murray case PAM_ERROR_MSG: 12126c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12136c9134c0SMark Murray reply[i].resp = NULL; 12146c9134c0SMark Murray break; 12156c9134c0SMark Murray default: /* unknown message style */ 12166c9134c0SMark Murray free(reply); 12176c9134c0SMark Murray return PAM_CONV_ERR; 12186c9134c0SMark Murray } 12196c9134c0SMark Murray } 12206c9134c0SMark Murray 12216c9134c0SMark Murray *resp = reply; 12226c9134c0SMark Murray return PAM_SUCCESS; 12236c9134c0SMark Murray } 12246c9134c0SMark Murray 12256c9134c0SMark Murray /* 12266c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 12276c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 12286c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 12296c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 12306c9134c0SMark Murray * fall back to a different authentication mechanism. 12316c9134c0SMark Murray */ 12326c9134c0SMark Murray static int 12336c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 12346c9134c0SMark Murray { 12356c9134c0SMark Murray pam_handle_t *pamh = NULL; 12366c9134c0SMark Murray const char *tmpl_user; 12376c9134c0SMark Murray const void *item; 12386c9134c0SMark Murray int rval; 12396c9134c0SMark Murray int e; 12406c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 12416c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 12426c9134c0SMark Murray 12436c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 12446c9134c0SMark Murray if (e != PAM_SUCCESS) { 12456c9134c0SMark Murray syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 12466c9134c0SMark Murray return -1; 12476c9134c0SMark Murray } 12486c9134c0SMark Murray 1249ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1250ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1251ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1252ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1253ea413ab7SGuido van Rooij return -1; 1254ea413ab7SGuido van Rooij } 1255ea413ab7SGuido van Rooij 12566c9134c0SMark Murray e = pam_authenticate(pamh, 0); 12576c9134c0SMark Murray switch (e) { 12586c9134c0SMark Murray case PAM_SUCCESS: 12596c9134c0SMark Murray /* 12606c9134c0SMark Murray * With PAM we support the concept of a "template" 12616c9134c0SMark Murray * user. The user enters a login name which is 12626c9134c0SMark Murray * authenticated by PAM, usually via a remote service 12636c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 12646c9134c0SMark Murray * succeeds, a different but related "template" name 12656c9134c0SMark Murray * is used for setting the credentials, shell, and 12666c9134c0SMark Murray * home directory. The name the user enters need only 12676c9134c0SMark Murray * exist on the remote authentication server, but the 12686c9134c0SMark Murray * template name must be present in the local password 12696c9134c0SMark Murray * database. 12706c9134c0SMark Murray * 12716c9134c0SMark Murray * This is supported by two various mechanisms in the 12726c9134c0SMark Murray * individual modules. However, from the application's 12736c9134c0SMark Murray * point of view, the template user is always passed 12746c9134c0SMark Murray * back as a changed value of the PAM_USER item. 12756c9134c0SMark Murray */ 12766c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 12776c9134c0SMark Murray PAM_SUCCESS) { 12786c9134c0SMark Murray tmpl_user = (const char *) item; 12796c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 12806c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 12816c9134c0SMark Murray } else 12826c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 12836c9134c0SMark Murray pam_strerror(pamh, e)); 12846c9134c0SMark Murray rval = 0; 12856c9134c0SMark Murray break; 12866c9134c0SMark Murray 12876c9134c0SMark Murray case PAM_AUTH_ERR: 12886c9134c0SMark Murray case PAM_USER_UNKNOWN: 12896c9134c0SMark Murray case PAM_MAXTRIES: 12906c9134c0SMark Murray rval = 1; 12916c9134c0SMark Murray break; 12926c9134c0SMark Murray 12936c9134c0SMark Murray default: 12945bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 12956c9134c0SMark Murray rval = -1; 12966c9134c0SMark Murray break; 12976c9134c0SMark Murray } 12986c9134c0SMark Murray 12995bc9d93dSMark Murray if (rval == 0) { 13005bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 13015bc9d93dSMark Murray if (e == PAM_NEW_AUTHTOK_REQD) { 13025bc9d93dSMark Murray e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); 13035bc9d93dSMark Murray if (e != PAM_SUCCESS) { 13045bc9d93dSMark Murray syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e)); 13055bc9d93dSMark Murray rval = 1; 13065bc9d93dSMark Murray } 13075bc9d93dSMark Murray } else if (e != PAM_SUCCESS) { 13085bc9d93dSMark Murray rval = 1; 13095bc9d93dSMark Murray } 13105bc9d93dSMark Murray } 13115bc9d93dSMark Murray 13125bc9d93dSMark Murray if (rval != 0) { 13136c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 13146c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 13155bc9d93dSMark Murray } 13165bc9d93dSMark Murray pamh = NULL; 13176c9134c0SMark Murray } 13186c9134c0SMark Murray return rval; 13196c9134c0SMark Murray } 13206c9134c0SMark Murray 13215bc9d93dSMark Murray #endif /* USE_PAM */ 13226c9134c0SMark Murray 1323ea022d16SRodney W. Grimes void 1324e4bc453cSWarner Losh pass(char *passwd) 1325ea022d16SRodney W. Grimes { 1326a5a4544eSPaul Traina int rval; 1327ea022d16SRodney W. Grimes FILE *fd; 1328b071c689SDavid Nugent #ifdef LOGIN_CAP 1329b071c689SDavid Nugent login_cap_t *lc = NULL; 1330b071c689SDavid Nugent #endif 13315bc9d93dSMark Murray #ifdef USE_PAM 13325bc9d93dSMark Murray int e; 13335bc9d93dSMark Murray #endif 133447499ecdSAndrey A. Chernov char *xpasswd; 1335ea022d16SRodney W. Grimes 1336ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1337ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1338ea022d16SRodney W. Grimes return; 1339ea022d16SRodney W. Grimes } 1340ea022d16SRodney W. Grimes askpasswd = 0; 1341ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1342a5a4544eSPaul Traina if (pw == NULL) { 1343a5a4544eSPaul Traina rval = 1; /* failure below */ 1344a5a4544eSPaul Traina goto skip; 1345a5a4544eSPaul Traina } 13465bc9d93dSMark Murray #ifdef USE_PAM 13476c9134c0SMark Murray rval = auth_pam(&pw, passwd); 1348f650a124SAndrey A. Chernov if (rval >= 0) { 1349f650a124SAndrey A. Chernov opieunlock(); 1350a5a4544eSPaul Traina goto skip; 1351f650a124SAndrey A. Chernov } 1352f650a124SAndrey A. Chernov #endif 135347499ecdSAndrey A. Chernov if (opieverify(&opiedata, passwd) == 0) 135447499ecdSAndrey A. Chernov xpasswd = pw->pw_passwd; 1355f650a124SAndrey A. Chernov else if (pwok) { 135647499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd); 1357f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1358f650a124SAndrey A. Chernov xpasswd = ":"; 1359f650a124SAndrey A. Chernov } else { 136047499ecdSAndrey A. Chernov rval = 1; 136147499ecdSAndrey A. Chernov goto skip; 136247499ecdSAndrey A. Chernov } 136347499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd); 1364f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1365a5a4544eSPaul Traina rval = 1; /* failure */ 1366a5a4544eSPaul Traina skip: 1367a5a4544eSPaul Traina /* 1368a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 13696c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1370a5a4544eSPaul Traina * succeeded. 1371a5a4544eSPaul Traina */ 1372a5a4544eSPaul Traina if (rval) { 1373ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1374ea022d16SRodney W. Grimes if (logging) 1375ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1376ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1377ea022d16SRodney W. Grimes remotehost, curname); 1378ea022d16SRodney W. Grimes pw = NULL; 1379ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1380ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1381ea022d16SRodney W. Grimes "repeated login failures from %s", 1382ea022d16SRodney W. Grimes remotehost); 1383ea022d16SRodney W. Grimes exit(0); 1384ea022d16SRodney W. Grimes } 1385ea022d16SRodney W. Grimes return; 1386ea022d16SRodney W. Grimes } 1387ea022d16SRodney W. Grimes } 1388ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1389ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 1390ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1391ea022d16SRodney W. Grimes return; 1392ea022d16SRodney W. Grimes } 1393b071c689SDavid Nugent /* May be overridden by login.conf */ 1394b071c689SDavid Nugent (void) umask(defumask); 1395b071c689SDavid Nugent #ifdef LOGIN_CAP 13965d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 1397b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 1398b071c689SDavid Nugent 13994dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 14004dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 1401b0f06defSHajimu UMEMOTO NI_NUMERICHOST); 1402b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1403b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1404b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1405b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1406b071c689SDavid Nugent pw->pw_name); 1407b071c689SDavid Nugent reply(530, "Permission denied.\n"); 1408b071c689SDavid Nugent pw = NULL; 1409b071c689SDavid Nugent return; 1410b071c689SDavid Nugent } 1411b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1412b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1413b071c689SDavid Nugent pw = NULL; 1414b071c689SDavid Nugent return; 1415b071c689SDavid Nugent } 1416b071c689SDavid Nugent } 1417b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1418e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1419d9e2c424SRobert Watson LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC); 1420b071c689SDavid Nugent #else 1421e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1422ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1423b071c689SDavid Nugent #endif 1424ea022d16SRodney W. Grimes 14255bc9d93dSMark Murray #ifdef USE_PAM 14265bc9d93dSMark Murray if (pamh) { 14275bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 14285bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 14295bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 14305bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 14315bc9d93dSMark Murray } 14325bc9d93dSMark Murray } 14335bc9d93dSMark Murray #endif 14345bc9d93dSMark Murray 1435ea022d16SRodney W. Grimes /* open wtmp before chroot */ 14365d7e0128SYaroslav Tykhiy if (dowtmp) 14375d7e0128SYaroslav Tykhiy ftpd_logwtmp(ttyline, pw->pw_name, 14385d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr); 1439ea022d16SRodney W. Grimes logged_in = 1; 1440ea022d16SRodney W. Grimes 1441a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1442ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1443ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1444ea4e54b9SDavid Nugent #else 14453eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1446ea4e54b9SDavid Nugent #endif 14473eb568f2SGuido van Rooij stats = 0; 14483eb568f2SGuido van Rooij 1449b071c689SDavid Nugent dochroot = 1450b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1451b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 1452b071c689SDavid Nugent #endif 14537edcb936SSteve Price checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1454ea022d16SRodney W. Grimes if (guest) { 1455ea022d16SRodney W. Grimes /* 1456ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 1457ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 1458ea022d16SRodney W. Grimes * outside the new root! 1459ea022d16SRodney W. Grimes */ 1460ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1461ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 1462ea022d16SRodney W. Grimes goto bad; 1463ea022d16SRodney W. Grimes } 1464a5a4544eSPaul Traina } else if (dochroot) { 1465a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1466a5a4544eSPaul Traina reply(550, "Can't change root."); 1467a5a4544eSPaul Traina goto bad; 1468a5a4544eSPaul Traina } 1469ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 1470ea022d16SRodney W. Grimes if (chdir("/") < 0) { 1471ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 1472ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 1473ea022d16SRodney W. Grimes goto bad; 1474ea022d16SRodney W. Grimes } else 1475ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 1476ea022d16SRodney W. Grimes } 1477ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1478ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1479ea022d16SRodney W. Grimes goto bad; 1480ea022d16SRodney W. Grimes } 148182c76939SDavid Greenman 148282c76939SDavid Greenman /* 1483ea022d16SRodney W. Grimes * Display a login message, if it exists. 1484ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1485ea022d16SRodney W. Grimes */ 1486ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1487ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1488ea4e54b9SDavid Nugent #else 1489ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1490ea4e54b9SDavid Nugent #endif 1491ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1492ea022d16SRodney W. Grimes 1493ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1494ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1495ea022d16SRodney W. Grimes *cp = '\0'; 1496ea022d16SRodney W. Grimes lreply(230, "%s", line); 1497ea022d16SRodney W. Grimes } 1498ea022d16SRodney W. Grimes (void) fflush(stdout); 1499ea022d16SRodney W. Grimes (void) fclose(fd); 1500ea022d16SRodney W. Grimes } 1501ea022d16SRodney W. Grimes if (guest) { 15023eb568f2SGuido van Rooij if (ident != NULL) 15033eb568f2SGuido van Rooij free(ident); 150461f891a6SPaul Traina ident = strdup(passwd); 150561f891a6SPaul Traina if (ident == NULL) 1506618b0bbaSMark Murray fatalerror("Ran out of memory."); 150761f891a6SPaul Traina 1508ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1509ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1510ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1511ea4e54b9SDavid Nugent if (thishost != firsthost) 1512ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1513b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname, 1514b3a0a7cdSMike Heffner passwd); 1515ea4e54b9SDavid Nugent else 1516ea4e54b9SDavid Nugent #endif 1517ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1518b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd); 1519b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1520ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1521ea022d16SRodney W. Grimes if (logging) 1522ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1523ea022d16SRodney W. Grimes remotehost, passwd); 1524ea022d16SRodney W. Grimes } else { 15253401a71fSDaniel O'Callaghan if (dochroot) 152611342ab1SYaroslav Tykhiy reply(230, "User %s logged in, " 152711342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name); 15283401a71fSDaniel O'Callaghan else 1529ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 15303401a71fSDaniel O'Callaghan 1531ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1532ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 15337a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name); 1534b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1535ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1536ea022d16SRodney W. Grimes if (logging) 1537ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1538ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1539ea022d16SRodney W. Grimes } 1540b071c689SDavid Nugent #ifdef LOGIN_CAP 1541b071c689SDavid Nugent login_close(lc); 1542b071c689SDavid Nugent #endif 1543ea022d16SRodney W. Grimes return; 1544ea022d16SRodney W. Grimes bad: 1545ea022d16SRodney W. Grimes /* Forget all about it... */ 1546b071c689SDavid Nugent #ifdef LOGIN_CAP 1547b071c689SDavid Nugent login_close(lc); 1548b071c689SDavid Nugent #endif 1549ea022d16SRodney W. Grimes end_login(); 1550ea022d16SRodney W. Grimes } 1551ea022d16SRodney W. Grimes 1552ea022d16SRodney W. Grimes void 1553e4bc453cSWarner Losh retrieve(char *cmd, char *name) 1554ea022d16SRodney W. Grimes { 1555ea022d16SRodney W. Grimes FILE *fin, *dout; 1556ea022d16SRodney W. Grimes struct stat st; 1557e4bc453cSWarner Losh int (*closefunc)(FILE *); 1558158a00b2SJohn Birrell time_t start; 1559ea022d16SRodney W. Grimes 1560ea022d16SRodney W. Grimes if (cmd == 0) { 1561ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1562ea022d16SRodney W. Grimes st.st_size = 0; 1563ea022d16SRodney W. Grimes } else { 1564ea022d16SRodney W. Grimes char line[BUFSIZ]; 1565ea022d16SRodney W. Grimes 1566e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1567ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1568ea022d16SRodney W. Grimes st.st_size = -1; 1569ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1570ea022d16SRodney W. Grimes } 1571ea022d16SRodney W. Grimes if (fin == NULL) { 1572ea022d16SRodney W. Grimes if (errno != 0) { 1573ea022d16SRodney W. Grimes perror_reply(550, name); 1574ea022d16SRodney W. Grimes if (cmd == 0) { 1575ea022d16SRodney W. Grimes LOGCMD("get", name); 1576ea022d16SRodney W. Grimes } 1577ea022d16SRodney W. Grimes } 1578ea022d16SRodney W. Grimes return; 1579ea022d16SRodney W. Grimes } 1580ea022d16SRodney W. Grimes byte_count = -1; 1581ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1582ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1583ea022d16SRodney W. Grimes goto done; 1584ea022d16SRodney W. Grimes } 1585ea022d16SRodney W. Grimes if (restart_point) { 1586ea022d16SRodney W. Grimes if (type == TYPE_A) { 1587ea022d16SRodney W. Grimes off_t i, n; 1588ea022d16SRodney W. Grimes int c; 1589ea022d16SRodney W. Grimes 1590ea022d16SRodney W. Grimes n = restart_point; 1591ea022d16SRodney W. Grimes i = 0; 1592ea022d16SRodney W. Grimes while (i++ < n) { 1593ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1594ea022d16SRodney W. Grimes perror_reply(550, name); 1595ea022d16SRodney W. Grimes goto done; 1596ea022d16SRodney W. Grimes } 1597ea022d16SRodney W. Grimes if (c == '\n') 1598ea022d16SRodney W. Grimes i++; 1599ea022d16SRodney W. Grimes } 1600ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1601ea022d16SRodney W. Grimes perror_reply(550, name); 1602ea022d16SRodney W. Grimes goto done; 1603ea022d16SRodney W. Grimes } 1604ea022d16SRodney W. Grimes } 1605ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1606ea022d16SRodney W. Grimes if (dout == NULL) 1607ea022d16SRodney W. Grimes goto done; 16083eb568f2SGuido van Rooij time(&start); 16099fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 16109fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 16113eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 16123eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1613ea022d16SRodney W. Grimes (void) fclose(dout); 1614ea022d16SRodney W. Grimes data = -1; 1615ea022d16SRodney W. Grimes pdata = -1; 1616ea022d16SRodney W. Grimes done: 1617ea022d16SRodney W. Grimes if (cmd == 0) 1618ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1619ea022d16SRodney W. Grimes (*closefunc)(fin); 1620ea022d16SRodney W. Grimes } 1621ea022d16SRodney W. Grimes 1622ea022d16SRodney W. Grimes void 1623e4bc453cSWarner Losh store(char *name, char *mode, int unique) 1624ea022d16SRodney W. Grimes { 1625a117c345SYaroslav Tykhiy int fd; 1626ea022d16SRodney W. Grimes FILE *fout, *din; 1627e4bc453cSWarner Losh int (*closefunc)(FILE *); 1628ea022d16SRodney W. Grimes 1629a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */ 1630a117c345SYaroslav Tykhiy if (unique) { 1631a117c345SYaroslav Tykhiy /* Programming error */ 1632a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE"); 1633a117c345SYaroslav Tykhiy unique = 0; 1634a117c345SYaroslav Tykhiy } 1635a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1636a117c345SYaroslav Tykhiy reply(550, "Appending to existing file denied"); 1637a117c345SYaroslav Tykhiy goto err; 1638a117c345SYaroslav Tykhiy } 1639a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */ 1640a117c345SYaroslav Tykhiy } 1641a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */ 1642a117c345SYaroslav Tykhiy restart_point = 0; 1643a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1644a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */ 1645a117c345SYaroslav Tykhiy reply(550, "Modifying existing file denied"); 1646a117c345SYaroslav Tykhiy goto err; 1647a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */ 1648a117c345SYaroslav Tykhiy unique = 1; 1649ea022d16SRodney W. Grimes } 1650ea022d16SRodney W. Grimes 1651ea022d16SRodney W. Grimes if (restart_point) 1652a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */ 1653a117c345SYaroslav Tykhiy if (unique) { 1654a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0) 1655a117c345SYaroslav Tykhiy goto err; 1656a117c345SYaroslav Tykhiy fout = fdopen(fd, mode); 1657a117c345SYaroslav Tykhiy } else 1658ea022d16SRodney W. Grimes fout = fopen(name, mode); 1659ea022d16SRodney W. Grimes closefunc = fclose; 1660ea022d16SRodney W. Grimes if (fout == NULL) { 1661ea022d16SRodney W. Grimes perror_reply(553, name); 1662a117c345SYaroslav Tykhiy goto err; 1663ea022d16SRodney W. Grimes } 1664ea022d16SRodney W. Grimes byte_count = -1; 1665ea022d16SRodney W. Grimes if (restart_point) { 1666ea022d16SRodney W. Grimes if (type == TYPE_A) { 1667ea022d16SRodney W. Grimes off_t i, n; 1668ea022d16SRodney W. Grimes int c; 1669ea022d16SRodney W. Grimes 1670ea022d16SRodney W. Grimes n = restart_point; 1671ea022d16SRodney W. Grimes i = 0; 1672ea022d16SRodney W. Grimes while (i++ < n) { 1673ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1674ea022d16SRodney W. Grimes perror_reply(550, name); 1675ea022d16SRodney W. Grimes goto done; 1676ea022d16SRodney W. Grimes } 1677ea022d16SRodney W. Grimes if (c == '\n') 1678ea022d16SRodney W. Grimes i++; 1679ea022d16SRodney W. Grimes } 1680ea022d16SRodney W. Grimes /* 1681ea022d16SRodney W. Grimes * We must do this seek to "current" position 1682ea022d16SRodney W. Grimes * because we are changing from reading to 1683ea022d16SRodney W. Grimes * writing. 1684ea022d16SRodney W. Grimes */ 1685e4a71114SAndrey A. Chernov if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) { 1686ea022d16SRodney W. Grimes perror_reply(550, name); 1687ea022d16SRodney W. Grimes goto done; 1688ea022d16SRodney W. Grimes } 1689ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1690ea022d16SRodney W. Grimes perror_reply(550, name); 1691ea022d16SRodney W. Grimes goto done; 1692ea022d16SRodney W. Grimes } 1693ea022d16SRodney W. Grimes } 1694ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1695ea022d16SRodney W. Grimes if (din == NULL) 1696ea022d16SRodney W. Grimes goto done; 1697ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1698ea022d16SRodney W. Grimes if (unique) 1699ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1700ea022d16SRodney W. Grimes name); 1701ea022d16SRodney W. Grimes else 1702ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1703ea022d16SRodney W. Grimes } 1704ea022d16SRodney W. Grimes (void) fclose(din); 1705ea022d16SRodney W. Grimes data = -1; 1706ea022d16SRodney W. Grimes pdata = -1; 1707ea022d16SRodney W. Grimes done: 17087c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); 1709ea022d16SRodney W. Grimes (*closefunc)(fout); 1710a117c345SYaroslav Tykhiy return; 1711a117c345SYaroslav Tykhiy err: 1712a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name); 1713a117c345SYaroslav Tykhiy return; 1714ea022d16SRodney W. Grimes } 1715ea022d16SRodney W. Grimes 1716ea022d16SRodney W. Grimes static FILE * 1717e4bc453cSWarner Losh getdatasock(char *mode) 1718ea022d16SRodney W. Grimes { 1719ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1720ea022d16SRodney W. Grimes 1721ea022d16SRodney W. Grimes if (data >= 0) 1722ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1723ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 17244dd8b5abSYoshinobu Inoue 17254dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1726ea022d16SRodney W. Grimes if (s < 0) 1727ea022d16SRodney W. Grimes goto bad; 172857d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1729406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1730ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 17314dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 173263591ba5SYaroslav Tykhiy data_source.su_port = htons(dataport); 1733ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1734ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 17354dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1736ea022d16SRodney W. Grimes break; 1737ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1738ea022d16SRodney W. Grimes goto bad; 1739ea022d16SRodney W. Grimes sleep(tries); 1740ea022d16SRodney W. Grimes } 1741ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1742ea022d16SRodney W. Grimes #ifdef IP_TOS 17434dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 17444dd8b5abSYoshinobu Inoue { 1745ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 174657d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1747406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 17484dd8b5abSYoshinobu Inoue } 1749ea022d16SRodney W. Grimes #endif 17509fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 17519fc5823aSGarrett Wollman /* 17529fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 17539fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 17549fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 17559fc5823aSGarrett Wollman * in heavy-load situations. 17569fc5823aSGarrett Wollman */ 17579fc5823aSGarrett Wollman on = 1; 175857d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1759406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 17609fc5823aSGarrett Wollman #endif 17619fc5823aSGarrett Wollman #ifdef SO_SNDBUF 17629fc5823aSGarrett Wollman on = 65536; 176357d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0) 1764406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m"); 17659fc5823aSGarrett Wollman #endif 17669fc5823aSGarrett Wollman 1767ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1768ea022d16SRodney W. Grimes bad: 1769ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1770ea022d16SRodney W. Grimes t = errno; 1771ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1772ea022d16SRodney W. Grimes (void) close(s); 1773ea022d16SRodney W. Grimes errno = t; 1774ea022d16SRodney W. Grimes return (NULL); 1775ea022d16SRodney W. Grimes } 1776ea022d16SRodney W. Grimes 1777ea022d16SRodney W. Grimes static FILE * 1778e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode) 1779ea022d16SRodney W. Grimes { 1780ea022d16SRodney W. Grimes char sizebuf[32]; 1781ea022d16SRodney W. Grimes FILE *file; 1782e5094456SCrist J. Clark int retry = 0, tos, conerrno; 1783ea022d16SRodney W. Grimes 1784ea022d16SRodney W. Grimes file_size = size; 1785ea022d16SRodney W. Grimes byte_count = 0; 1786ea022d16SRodney W. Grimes if (size != (off_t) -1) 1787e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1788ea022d16SRodney W. Grimes else 1789e760ef2cSWarner Losh *sizebuf = '\0'; 1790ea022d16SRodney W. Grimes if (pdata >= 0) { 17914dd8b5abSYoshinobu Inoue union sockunion from; 17924cd48bacSYaroslav Tykhiy int flags; 17934dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1794d6ed3c37SGuido van Rooij struct timeval timeout; 1795d6ed3c37SGuido van Rooij fd_set set; 1796ea022d16SRodney W. Grimes 1797d6ed3c37SGuido van Rooij FD_ZERO(&set); 1798d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1799d6ed3c37SGuido van Rooij 1800d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1801d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1802d6ed3c37SGuido van Rooij 18034cd48bacSYaroslav Tykhiy /* 18044cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode, 18054cd48bacSYaroslav Tykhiy * accept() will block after a successful select() 18064cd48bacSYaroslav Tykhiy * if the selected connection dies in between. 18074cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here. 18084cd48bacSYaroslav Tykhiy */ 18094cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 18104cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 18114cd48bacSYaroslav Tykhiy goto pdata_err; 18124cd48bacSYaroslav Tykhiy if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 || 18134cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 18144cd48bacSYaroslav Tykhiy goto pdata_err; 1815ea022d16SRodney W. Grimes (void) close(pdata); 1816ea022d16SRodney W. Grimes pdata = s; 18174cd48bacSYaroslav Tykhiy /* 1818f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag 1819f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it. 18204cd48bacSYaroslav Tykhiy */ 18214cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 18224cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 18234cd48bacSYaroslav Tykhiy goto pdata_err; 1824ea022d16SRodney W. Grimes #ifdef IP_TOS 18254dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 18264dd8b5abSYoshinobu Inoue { 1827a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 182857d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1829406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 18304dd8b5abSYoshinobu Inoue } 1831ea022d16SRodney W. Grimes #endif 1832ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1833ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1834ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 18354cd48bacSYaroslav Tykhiy pdata_err: 18364cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection."); 18374cd48bacSYaroslav Tykhiy (void) close(pdata); 18384cd48bacSYaroslav Tykhiy pdata = -1; 18394cd48bacSYaroslav Tykhiy return (NULL); 1840ea022d16SRodney W. Grimes } 1841ea022d16SRodney W. Grimes if (data >= 0) { 1842ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1843ea022d16SRodney W. Grimes name, sizebuf); 1844ea022d16SRodney W. Grimes usedefault = 1; 1845ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1846ea022d16SRodney W. Grimes } 1847ea022d16SRodney W. Grimes if (usedefault) 1848ea022d16SRodney W. Grimes data_dest = his_addr; 1849ea022d16SRodney W. Grimes usedefault = 1; 1850e5094456SCrist J. Clark do { 1851ea022d16SRodney W. Grimes file = getdatasock(mode); 1852ea022d16SRodney W. Grimes if (file == NULL) { 18534dd8b5abSYoshinobu Inoue char hostbuf[BUFSIZ], portbuf[BUFSIZ]; 18544dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&data_source, 18554dd8b5abSYoshinobu Inoue data_source.su_len, hostbuf, sizeof(hostbuf) - 1, 18564dd8b5abSYoshinobu Inoue portbuf, sizeof(portbuf), 1857b0f06defSHajimu UMEMOTO NI_NUMERICHOST|NI_NUMERICSERV); 18584dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 18594dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1860ea022d16SRodney W. Grimes return (NULL); 1861ea022d16SRodney W. Grimes } 1862ea022d16SRodney W. Grimes data = fileno(file); 1863e5094456SCrist J. Clark conerrno = 0; 1864e5094456SCrist J. Clark if (connect(data, (struct sockaddr *)&data_dest, 1865e5094456SCrist J. Clark data_dest.su_len) == 0) 1866e5094456SCrist J. Clark break; 1867e5094456SCrist J. Clark conerrno = errno; 1868ea022d16SRodney W. Grimes (void) fclose(file); 1869ea022d16SRodney W. Grimes data = -1; 1870e5094456SCrist J. Clark if (conerrno == EADDRINUSE) { 1871e5094456SCrist J. Clark sleep((unsigned) swaitint); 1872e5094456SCrist J. Clark retry += swaitint; 1873e5094456SCrist J. Clark } else { 1874e5094456SCrist J. Clark break; 1875e5094456SCrist J. Clark } 1876e5094456SCrist J. Clark } while (retry <= swaitmax); 1877e5094456SCrist J. Clark if (conerrno != 0) { 1878e5094456SCrist J. Clark perror_reply(425, "Can't build data connection"); 1879ea022d16SRodney W. Grimes return (NULL); 1880ea022d16SRodney W. Grimes } 1881ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1882ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1883ea022d16SRodney W. Grimes return (file); 1884ea022d16SRodney W. Grimes } 1885ea022d16SRodney W. Grimes 1886ea022d16SRodney W. Grimes /* 1887ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 18889fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1889ea022d16SRodney W. Grimes * 1890ea022d16SRodney W. Grimes * NB: Form isn't handled. 1891ea022d16SRodney W. Grimes */ 18924b82fc95SYaroslav Tykhiy static int 1893e4bc453cSWarner Losh send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg) 1894ea022d16SRodney W. Grimes { 1895f6f0c4b9SDan Moschuk int c, filefd, netfd; 1896f6f0c4b9SDan Moschuk char *buf; 1897f6f0c4b9SDan Moschuk off_t cnt; 1898ea022d16SRodney W. Grimes 1899ea022d16SRodney W. Grimes transflag++; 1900ea022d16SRodney W. Grimes switch (type) { 1901ea022d16SRodney W. Grimes 1902ea022d16SRodney W. Grimes case TYPE_A: 1903ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 19044b82fc95SYaroslav Tykhiy if (recvurg) 19054b82fc95SYaroslav Tykhiy goto got_oob; 1906ea022d16SRodney W. Grimes byte_count++; 1907ea022d16SRodney W. Grimes if (c == '\n') { 1908ea022d16SRodney W. Grimes if (ferror(outstr)) 1909ea022d16SRodney W. Grimes goto data_err; 1910ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1911ea022d16SRodney W. Grimes } 1912ea022d16SRodney W. Grimes (void) putc(c, outstr); 1913ea022d16SRodney W. Grimes } 19144b82fc95SYaroslav Tykhiy if (recvurg) 19154b82fc95SYaroslav Tykhiy goto got_oob; 1916ea022d16SRodney W. Grimes fflush(outstr); 1917ea022d16SRodney W. Grimes transflag = 0; 1918ea022d16SRodney W. Grimes if (ferror(instr)) 1919ea022d16SRodney W. Grimes goto file_err; 1920ea022d16SRodney W. Grimes if (ferror(outstr)) 1921ea022d16SRodney W. Grimes goto data_err; 1922ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 19234b82fc95SYaroslav Tykhiy return (0); 1924ea022d16SRodney W. Grimes 1925ea022d16SRodney W. Grimes case TYPE_I: 1926ea022d16SRodney W. Grimes case TYPE_L: 19279fc5823aSGarrett Wollman /* 19289fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 19299fc5823aSGarrett Wollman * are sending a regular file 19309fc5823aSGarrett Wollman */ 19319fc5823aSGarrett Wollman netfd = fileno(outstr); 19329fc5823aSGarrett Wollman filefd = fileno(instr); 19339fc5823aSGarrett Wollman 1934f6f0c4b9SDan Moschuk if (isreg) { 19359fc5823aSGarrett Wollman 1936f6f0c4b9SDan Moschuk off_t offset; 1937f6f0c4b9SDan Moschuk int err; 1938f6f0c4b9SDan Moschuk 1939f6f0c4b9SDan Moschuk err = cnt = offset = 0; 1940f6f0c4b9SDan Moschuk 1941492f1d9cSMaxim Konovalov while (err != -1 && filesize > 0) { 1942492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0, 1943f6f0c4b9SDan Moschuk (struct sf_hdtr *) NULL, &cnt, 0); 19443af48c42SMaxim Konovalov /* 19453af48c42SMaxim Konovalov * Calculate byte_count before OOB processing. 19463af48c42SMaxim Konovalov * It can be used in myoob() later. 19473af48c42SMaxim Konovalov */ 19483af48c42SMaxim Konovalov byte_count += cnt; 19494b82fc95SYaroslav Tykhiy if (recvurg) 19504b82fc95SYaroslav Tykhiy goto got_oob; 1951f6f0c4b9SDan Moschuk offset += cnt; 1952492f1d9cSMaxim Konovalov filesize -= cnt; 1953f6f0c4b9SDan Moschuk 1954f6f0c4b9SDan Moschuk if (err == -1) { 1955f6f0c4b9SDan Moschuk if (!cnt) 1956f6f0c4b9SDan Moschuk goto oldway; 1957f6f0c4b9SDan Moschuk 19589fc5823aSGarrett Wollman goto data_err; 1959f6f0c4b9SDan Moschuk } 1960f6f0c4b9SDan Moschuk } 1961f6f0c4b9SDan Moschuk 19620849c184SDan Moschuk transflag = 0; 19639fc5823aSGarrett Wollman reply(226, "Transfer complete."); 19644b82fc95SYaroslav Tykhiy return (0); 19659fc5823aSGarrett Wollman } 19669fc5823aSGarrett Wollman 19679fc5823aSGarrett Wollman oldway: 1968ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1969ea022d16SRodney W. Grimes transflag = 0; 1970ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 19714b82fc95SYaroslav Tykhiy return (-1); 1972ea022d16SRodney W. Grimes } 19739fc5823aSGarrett Wollman 1974ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1975ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1976ea022d16SRodney W. Grimes byte_count += cnt; 1977ea022d16SRodney W. Grimes transflag = 0; 1978ea022d16SRodney W. Grimes (void)free(buf); 1979ea022d16SRodney W. Grimes if (cnt != 0) { 1980ea022d16SRodney W. Grimes if (cnt < 0) 1981ea022d16SRodney W. Grimes goto file_err; 1982ea022d16SRodney W. Grimes goto data_err; 1983ea022d16SRodney W. Grimes } 1984ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 19854b82fc95SYaroslav Tykhiy return (0); 1986ea022d16SRodney W. Grimes default: 1987ea022d16SRodney W. Grimes transflag = 0; 1988ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 19894b82fc95SYaroslav Tykhiy return (-1); 1990ea022d16SRodney W. Grimes } 1991ea022d16SRodney W. Grimes 1992ea022d16SRodney W. Grimes data_err: 1993ea022d16SRodney W. Grimes transflag = 0; 1994ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 19954b82fc95SYaroslav Tykhiy return (-1); 1996ea022d16SRodney W. Grimes 1997ea022d16SRodney W. Grimes file_err: 1998ea022d16SRodney W. Grimes transflag = 0; 1999ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 20004b82fc95SYaroslav Tykhiy return (-1); 20014b82fc95SYaroslav Tykhiy 20024b82fc95SYaroslav Tykhiy got_oob: 20034b82fc95SYaroslav Tykhiy myoob(); 20044b82fc95SYaroslav Tykhiy recvurg = 0; 20054b82fc95SYaroslav Tykhiy transflag = 0; 20064b82fc95SYaroslav Tykhiy return (-1); 2007ea022d16SRodney W. Grimes } 2008ea022d16SRodney W. Grimes 2009ea022d16SRodney W. Grimes /* 2010ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 2011ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 2012ea022d16SRodney W. Grimes * 2013ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 2014ea022d16SRodney W. Grimes */ 2015ea022d16SRodney W. Grimes static int 2016e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr) 2017ea022d16SRodney W. Grimes { 2018ea022d16SRodney W. Grimes int c; 201961f891a6SPaul Traina int cnt, bare_lfs; 2020ea022d16SRodney W. Grimes char buf[BUFSIZ]; 2021ea022d16SRodney W. Grimes 2022ea022d16SRodney W. Grimes transflag++; 202361f891a6SPaul Traina bare_lfs = 0; 202461f891a6SPaul Traina 2025ea022d16SRodney W. Grimes switch (type) { 2026ea022d16SRodney W. Grimes 2027ea022d16SRodney W. Grimes case TYPE_I: 2028ea022d16SRodney W. Grimes case TYPE_L: 2029ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 20304b82fc95SYaroslav Tykhiy if (recvurg) 20314b82fc95SYaroslav Tykhiy goto got_oob; 2032ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 2033ea022d16SRodney W. Grimes goto file_err; 2034ea022d16SRodney W. Grimes byte_count += cnt; 2035ea022d16SRodney W. Grimes } 20364b82fc95SYaroslav Tykhiy if (recvurg) 20374b82fc95SYaroslav Tykhiy goto got_oob; 2038ea022d16SRodney W. Grimes if (cnt < 0) 2039ea022d16SRodney W. Grimes goto data_err; 2040ea022d16SRodney W. Grimes transflag = 0; 2041ea022d16SRodney W. Grimes return (0); 2042ea022d16SRodney W. Grimes 2043ea022d16SRodney W. Grimes case TYPE_E: 2044ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 2045ea022d16SRodney W. Grimes transflag = 0; 2046ea022d16SRodney W. Grimes return (-1); 2047ea022d16SRodney W. Grimes 2048ea022d16SRodney W. Grimes case TYPE_A: 2049ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 20504b82fc95SYaroslav Tykhiy if (recvurg) 20514b82fc95SYaroslav Tykhiy goto got_oob; 2052ea022d16SRodney W. Grimes byte_count++; 2053ea022d16SRodney W. Grimes if (c == '\n') 2054ea022d16SRodney W. Grimes bare_lfs++; 2055ea022d16SRodney W. Grimes while (c == '\r') { 2056ea022d16SRodney W. Grimes if (ferror(outstr)) 2057ea022d16SRodney W. Grimes goto data_err; 2058ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 2059ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 2060ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 2061ea022d16SRodney W. Grimes goto contin2; 2062ea022d16SRodney W. Grimes } 2063ea022d16SRodney W. Grimes } 2064ea022d16SRodney W. Grimes (void) putc(c, outstr); 2065ea022d16SRodney W. Grimes contin2: ; 2066ea022d16SRodney W. Grimes } 20674b82fc95SYaroslav Tykhiy if (recvurg) 20684b82fc95SYaroslav Tykhiy goto got_oob; 2069ea022d16SRodney W. Grimes fflush(outstr); 2070ea022d16SRodney W. Grimes if (ferror(instr)) 2071ea022d16SRodney W. Grimes goto data_err; 2072ea022d16SRodney W. Grimes if (ferror(outstr)) 2073ea022d16SRodney W. Grimes goto file_err; 2074ea022d16SRodney W. Grimes transflag = 0; 2075ea022d16SRodney W. Grimes if (bare_lfs) { 2076ea022d16SRodney W. Grimes lreply(226, 2077ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 2078ea022d16SRodney W. Grimes bare_lfs); 2079ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 2080ea022d16SRodney W. Grimes } 2081ea022d16SRodney W. Grimes return (0); 2082ea022d16SRodney W. Grimes default: 2083ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 2084ea022d16SRodney W. Grimes transflag = 0; 2085ea022d16SRodney W. Grimes return (-1); 2086ea022d16SRodney W. Grimes } 2087ea022d16SRodney W. Grimes 2088ea022d16SRodney W. Grimes data_err: 2089ea022d16SRodney W. Grimes transflag = 0; 2090ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 2091ea022d16SRodney W. Grimes return (-1); 2092ea022d16SRodney W. Grimes 2093ea022d16SRodney W. Grimes file_err: 2094ea022d16SRodney W. Grimes transflag = 0; 2095ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 2096ea022d16SRodney W. Grimes return (-1); 20974b82fc95SYaroslav Tykhiy 20984b82fc95SYaroslav Tykhiy got_oob: 20994b82fc95SYaroslav Tykhiy myoob(); 21004b82fc95SYaroslav Tykhiy recvurg = 0; 21014b82fc95SYaroslav Tykhiy transflag = 0; 21024b82fc95SYaroslav Tykhiy return (-1); 2103ea022d16SRodney W. Grimes } 2104ea022d16SRodney W. Grimes 2105ea022d16SRodney W. Grimes void 2106e4bc453cSWarner Losh statfilecmd(char *filename) 2107ea022d16SRodney W. Grimes { 2108ea022d16SRodney W. Grimes FILE *fin; 2109f8a581a0SYaroslav Tykhiy int atstart; 2110ea022d16SRodney W. Grimes int c; 2111ea022d16SRodney W. Grimes char line[LINE_MAX]; 2112ea022d16SRodney W. Grimes 2113af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2114ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 2115ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 2116f8a581a0SYaroslav Tykhiy atstart = 1; 2117ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 2118ea022d16SRodney W. Grimes if (c == '\n') { 2119ea022d16SRodney W. Grimes if (ferror(stdout)){ 2120ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 2121ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2122ea022d16SRodney W. Grimes dologout(1); 2123ea022d16SRodney W. Grimes /* NOTREACHED */ 2124ea022d16SRodney W. Grimes } 2125ea022d16SRodney W. Grimes if (ferror(fin)) { 2126ea022d16SRodney W. Grimes perror_reply(551, filename); 2127ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2128ea022d16SRodney W. Grimes return; 2129ea022d16SRodney W. Grimes } 2130ea022d16SRodney W. Grimes (void) putc('\r', stdout); 2131ea022d16SRodney W. Grimes } 2132f8a581a0SYaroslav Tykhiy /* 2133f8a581a0SYaroslav Tykhiy * RFC 959 says neutral text should be prepended before 2134f8a581a0SYaroslav Tykhiy * a leading 3-digit number followed by whitespace, but 2135f8a581a0SYaroslav Tykhiy * many ftp clients can be confused by any leading digits, 2136f8a581a0SYaroslav Tykhiy * as a matter of fact. 2137f8a581a0SYaroslav Tykhiy */ 2138f8a581a0SYaroslav Tykhiy if (atstart && isdigit(c)) 2139f8a581a0SYaroslav Tykhiy (void) putc(' ', stdout); 2140ea022d16SRodney W. Grimes (void) putc(c, stdout); 2141f8a581a0SYaroslav Tykhiy atstart = (c == '\n'); 2142ea022d16SRodney W. Grimes } 2143ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2144ea022d16SRodney W. Grimes reply(211, "End of Status"); 2145ea022d16SRodney W. Grimes } 2146ea022d16SRodney W. Grimes 2147ea022d16SRodney W. Grimes void 2148e4bc453cSWarner Losh statcmd(void) 2149ea022d16SRodney W. Grimes { 21504dd8b5abSYoshinobu Inoue union sockunion *su; 2151ea022d16SRodney W. Grimes u_char *a, *p; 2152b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST]; 21534dd8b5abSYoshinobu Inoue int ispassive; 2154ea022d16SRodney W. Grimes 2155a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname); 2156ea022d16SRodney W. Grimes printf(" %s\r\n", version); 2157ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 21584dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2159b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 21604dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 21614dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 21624dd8b5abSYoshinobu Inoue } 2163ea022d16SRodney W. Grimes printf("\r\n"); 2164ea022d16SRodney W. Grimes if (logged_in) { 2165ea022d16SRodney W. Grimes if (guest) 2166ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 2167ea022d16SRodney W. Grimes else 2168ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 2169ea022d16SRodney W. Grimes } else if (askpasswd) 2170ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 2171ea022d16SRodney W. Grimes else 2172ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 2173ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 2174ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 2175ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2176ea022d16SRodney W. Grimes if (type == TYPE_L) 217789fdc4e1SMike Barcroft #if CHAR_BIT == 8 217889fdc4e1SMike Barcroft printf(" %d", CHAR_BIT); 2179ea022d16SRodney W. Grimes #else 2180ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2181ea022d16SRodney W. Grimes #endif 2182ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2183ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2184ea022d16SRodney W. Grimes if (data != -1) 2185ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2186ea022d16SRodney W. Grimes else if (pdata != -1) { 21874dd8b5abSYoshinobu Inoue ispassive = 1; 21884dd8b5abSYoshinobu Inoue su = &pasv_addr; 2189ea022d16SRodney W. Grimes goto printaddr; 2190ea022d16SRodney W. Grimes } else if (usedefault == 0) { 21914dd8b5abSYoshinobu Inoue ispassive = 0; 21924dd8b5abSYoshinobu Inoue su = &data_dest; 2193ea022d16SRodney W. Grimes printaddr: 2194ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 21954dd8b5abSYoshinobu Inoue if (epsvall) { 21964dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 21974dd8b5abSYoshinobu Inoue goto epsvonly; 21984dd8b5abSYoshinobu Inoue } 21994dd8b5abSYoshinobu Inoue 22004dd8b5abSYoshinobu Inoue /* PORT/PASV */ 22014dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 22024dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 22034dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 22044dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 22054dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 22064dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 22074dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 22084dd8b5abSYoshinobu Inoue } 22094dd8b5abSYoshinobu Inoue 22104dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 22114dd8b5abSYoshinobu Inoue { 22124dd8b5abSYoshinobu Inoue int alen, af, i; 22134dd8b5abSYoshinobu Inoue 22144dd8b5abSYoshinobu Inoue switch (su->su_family) { 22154dd8b5abSYoshinobu Inoue case AF_INET: 22164dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 22174dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 22184dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 22194dd8b5abSYoshinobu Inoue af = 4; 22204dd8b5abSYoshinobu Inoue break; 22214dd8b5abSYoshinobu Inoue case AF_INET6: 22224dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 22234dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 22244dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 22254dd8b5abSYoshinobu Inoue af = 6; 22264dd8b5abSYoshinobu Inoue break; 22274dd8b5abSYoshinobu Inoue default: 22284dd8b5abSYoshinobu Inoue af = 0; 22294dd8b5abSYoshinobu Inoue break; 22304dd8b5abSYoshinobu Inoue } 22314dd8b5abSYoshinobu Inoue if (af) { 22324dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 22334dd8b5abSYoshinobu Inoue af, alen); 22344dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 22354dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 22364dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 22374dd8b5abSYoshinobu Inoue } 22384dd8b5abSYoshinobu Inoue } 22394dd8b5abSYoshinobu Inoue 22404dd8b5abSYoshinobu Inoue epsvonly:; 22414dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 22424dd8b5abSYoshinobu Inoue { 22434dd8b5abSYoshinobu Inoue int af; 22444dd8b5abSYoshinobu Inoue 22454dd8b5abSYoshinobu Inoue switch (su->su_family) { 22464dd8b5abSYoshinobu Inoue case AF_INET: 22474dd8b5abSYoshinobu Inoue af = 1; 22484dd8b5abSYoshinobu Inoue break; 22494dd8b5abSYoshinobu Inoue case AF_INET6: 22504dd8b5abSYoshinobu Inoue af = 2; 22514dd8b5abSYoshinobu Inoue break; 22524dd8b5abSYoshinobu Inoue default: 22534dd8b5abSYoshinobu Inoue af = 0; 22544dd8b5abSYoshinobu Inoue break; 22554dd8b5abSYoshinobu Inoue } 22564dd8b5abSYoshinobu Inoue if (af) { 2257b0f06defSHajimu UMEMOTO union sockunion tmp; 2258b0f06defSHajimu UMEMOTO 2259b0f06defSHajimu UMEMOTO tmp = *su; 2260b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6) 2261b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0; 2262b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 22634dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 22644dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 22654dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 22664dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 2267b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port)); 22684dd8b5abSYoshinobu Inoue } 22694dd8b5abSYoshinobu Inoue } 22704dd8b5abSYoshinobu Inoue } 2271ea022d16SRodney W. Grimes #undef UC 2272ea022d16SRodney W. Grimes } else 2273ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 2274ea022d16SRodney W. Grimes reply(211, "End of status"); 2275ea022d16SRodney W. Grimes } 2276ea022d16SRodney W. Grimes 2277ea022d16SRodney W. Grimes void 2278e4bc453cSWarner Losh fatalerror(char *s) 2279ea022d16SRodney W. Grimes { 2280ea022d16SRodney W. Grimes 2281ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 2282ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2283ea022d16SRodney W. Grimes dologout(0); 2284ea022d16SRodney W. Grimes /* NOTREACHED */ 2285ea022d16SRodney W. Grimes } 2286ea022d16SRodney W. Grimes 2287ea022d16SRodney W. Grimes void 2288ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2289ea022d16SRodney W. Grimes { 2290ea022d16SRodney W. Grimes va_list ap; 2291e4bc453cSWarner Losh 2292ea022d16SRodney W. Grimes va_start(ap, fmt); 2293ea022d16SRodney W. Grimes (void)printf("%d ", n); 2294ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2295ea022d16SRodney W. Grimes (void)printf("\r\n"); 2296ea022d16SRodney W. Grimes (void)fflush(stdout); 2297618b0bbaSMark Murray if (ftpdebug) { 2298ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 2299ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2300ea022d16SRodney W. Grimes } 2301ea022d16SRodney W. Grimes } 2302ea022d16SRodney W. Grimes 2303ea022d16SRodney W. Grimes void 2304ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2305ea022d16SRodney W. Grimes { 2306ea022d16SRodney W. Grimes va_list ap; 2307e4bc453cSWarner Losh 2308ea022d16SRodney W. Grimes va_start(ap, fmt); 2309ea022d16SRodney W. Grimes (void)printf("%d- ", n); 2310ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2311ea022d16SRodney W. Grimes (void)printf("\r\n"); 2312ea022d16SRodney W. Grimes (void)fflush(stdout); 2313618b0bbaSMark Murray if (ftpdebug) { 2314ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 2315ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2316ea022d16SRodney W. Grimes } 2317ea022d16SRodney W. Grimes } 2318ea022d16SRodney W. Grimes 2319ea022d16SRodney W. Grimes static void 2320e4bc453cSWarner Losh ack(char *s) 2321ea022d16SRodney W. Grimes { 2322ea022d16SRodney W. Grimes 2323ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2324ea022d16SRodney W. Grimes } 2325ea022d16SRodney W. Grimes 2326ea022d16SRodney W. Grimes void 2327e4bc453cSWarner Losh nack(char *s) 2328ea022d16SRodney W. Grimes { 2329ea022d16SRodney W. Grimes 2330ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2331ea022d16SRodney W. Grimes } 2332ea022d16SRodney W. Grimes 2333ea022d16SRodney W. Grimes /* ARGSUSED */ 2334ea022d16SRodney W. Grimes void 2335e4bc453cSWarner Losh yyerror(char *s) 2336ea022d16SRodney W. Grimes { 2337ea022d16SRodney W. Grimes char *cp; 2338ea022d16SRodney W. Grimes 23399aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2340ea022d16SRodney W. Grimes *cp = '\0'; 2341ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 2342ea022d16SRodney W. Grimes } 2343ea022d16SRodney W. Grimes 2344ea022d16SRodney W. Grimes void 2345e4bc453cSWarner Losh delete(char *name) 2346ea022d16SRodney W. Grimes { 2347ea022d16SRodney W. Grimes struct stat st; 2348ea022d16SRodney W. Grimes 2349ea022d16SRodney W. Grimes LOGCMD("delete", name); 23501b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2351ea022d16SRodney W. Grimes perror_reply(550, name); 2352ea022d16SRodney W. Grimes return; 2353ea022d16SRodney W. Grimes } 2354ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 2355ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2356ea022d16SRodney W. Grimes perror_reply(550, name); 2357ea022d16SRodney W. Grimes return; 2358ea022d16SRodney W. Grimes } 2359ea022d16SRodney W. Grimes goto done; 2360ea022d16SRodney W. Grimes } 2361ea022d16SRodney W. Grimes if (unlink(name) < 0) { 2362ea022d16SRodney W. Grimes perror_reply(550, name); 2363ea022d16SRodney W. Grimes return; 2364ea022d16SRodney W. Grimes } 2365ea022d16SRodney W. Grimes done: 2366ea022d16SRodney W. Grimes ack("DELE"); 2367ea022d16SRodney W. Grimes } 2368ea022d16SRodney W. Grimes 2369ea022d16SRodney W. Grimes void 2370e4bc453cSWarner Losh cwd(char *path) 2371ea022d16SRodney W. Grimes { 2372ea022d16SRodney W. Grimes 2373ea022d16SRodney W. Grimes if (chdir(path) < 0) 2374ea022d16SRodney W. Grimes perror_reply(550, path); 2375ea022d16SRodney W. Grimes else 2376ea022d16SRodney W. Grimes ack("CWD"); 2377ea022d16SRodney W. Grimes } 2378ea022d16SRodney W. Grimes 2379ea022d16SRodney W. Grimes void 2380e4bc453cSWarner Losh makedir(char *name) 2381ea022d16SRodney W. Grimes { 23822b748987SYaroslav Tykhiy char *s; 2383ea022d16SRodney W. Grimes 2384ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2385d186bb12SMatthew N. Dodd if (guest && noguestmkd) 2386d186bb12SMatthew N. Dodd reply(550, "%s: permission denied", name); 2387d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0) 2388ea022d16SRodney W. Grimes perror_reply(550, name); 23892b748987SYaroslav Tykhiy else { 23902b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL) 23912b748987SYaroslav Tykhiy fatalerror("Ran out of memory."); 23922b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s); 23932b748987SYaroslav Tykhiy free(s); 23942b748987SYaroslav Tykhiy } 2395ea022d16SRodney W. Grimes } 2396ea022d16SRodney W. Grimes 2397ea022d16SRodney W. Grimes void 2398e4bc453cSWarner Losh removedir(char *name) 2399ea022d16SRodney W. Grimes { 2400ea022d16SRodney W. Grimes 2401ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2402ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2403ea022d16SRodney W. Grimes perror_reply(550, name); 2404ea022d16SRodney W. Grimes else 2405ea022d16SRodney W. Grimes ack("RMD"); 2406ea022d16SRodney W. Grimes } 2407ea022d16SRodney W. Grimes 2408ea022d16SRodney W. Grimes void 2409e4bc453cSWarner Losh pwd(void) 2410ea022d16SRodney W. Grimes { 2411e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1]; 2412ea022d16SRodney W. Grimes 2413ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 2414ea022d16SRodney W. Grimes reply(550, "%s.", path); 2415e4648f05SYaroslav Tykhiy else { 2416e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL) 2417e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory."); 2418e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s); 2419e4648f05SYaroslav Tykhiy free(s); 2420e4648f05SYaroslav Tykhiy } 2421ea022d16SRodney W. Grimes } 2422ea022d16SRodney W. Grimes 2423ea022d16SRodney W. Grimes char * 2424e4bc453cSWarner Losh renamefrom(char *name) 2425ea022d16SRodney W. Grimes { 2426ea022d16SRodney W. Grimes struct stat st; 2427ea022d16SRodney W. Grimes 24281b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2429ea022d16SRodney W. Grimes perror_reply(550, name); 2430ea022d16SRodney W. Grimes return ((char *)0); 2431ea022d16SRodney W. Grimes } 2432ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 2433ea022d16SRodney W. Grimes return (name); 2434ea022d16SRodney W. Grimes } 2435ea022d16SRodney W. Grimes 2436ea022d16SRodney W. Grimes void 2437e4bc453cSWarner Losh renamecmd(char *from, char *to) 2438ea022d16SRodney W. Grimes { 243961f891a6SPaul Traina struct stat st; 2440ea022d16SRodney W. Grimes 2441ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 244261f891a6SPaul Traina 244361f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 244461f891a6SPaul Traina reply(550, "%s: permission denied", to); 244561f891a6SPaul Traina return; 244661f891a6SPaul Traina } 244761f891a6SPaul Traina 2448ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2449ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2450ea022d16SRodney W. Grimes else 2451ea022d16SRodney W. Grimes ack("RNTO"); 2452ea022d16SRodney W. Grimes } 2453ea022d16SRodney W. Grimes 2454ea022d16SRodney W. Grimes static void 2455e4bc453cSWarner Losh dolog(struct sockaddr *who) 2456ea022d16SRodney W. Grimes { 24574dd8b5abSYoshinobu Inoue int error; 24584dd8b5abSYoshinobu Inoue 24594dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 2460ea022d16SRodney W. Grimes 2461ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2462ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2463ea4e54b9SDavid Nugent if (thishost != firsthost) 2464ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2465ea4e54b9SDavid Nugent remotehost, hostname); 2466ea4e54b9SDavid Nugent else 2467ea4e54b9SDavid Nugent #endif 2468ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2469ea4e54b9SDavid Nugent remotehost); 2470b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2471ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2472ea022d16SRodney W. Grimes 2473ea4e54b9SDavid Nugent if (logging) { 2474ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2475ea4e54b9SDavid Nugent if (thishost != firsthost) 2476ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 2477ea4e54b9SDavid Nugent remotehost, hostname); 2478ea4e54b9SDavid Nugent else 2479ea4e54b9SDavid Nugent #endif 24804dd8b5abSYoshinobu Inoue { 24814dd8b5abSYoshinobu Inoue char who_name[MAXHOSTNAMELEN]; 24824dd8b5abSYoshinobu Inoue 24834dd8b5abSYoshinobu Inoue error = getnameinfo(who, who->sa_len, 24844dd8b5abSYoshinobu Inoue who_name, sizeof(who_name) - 1, 2485b0f06defSHajimu UMEMOTO NULL, 0, NI_NUMERICHOST); 2486f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 24874dd8b5abSYoshinobu Inoue error == 0 ? who_name : ""); 24884dd8b5abSYoshinobu Inoue } 2489ea022d16SRodney W. Grimes } 2490ea4e54b9SDavid Nugent } 2491ea022d16SRodney W. Grimes 2492ea022d16SRodney W. Grimes /* 2493ea022d16SRodney W. Grimes * Record logout in wtmp file 2494ea022d16SRodney W. Grimes * and exit with supplied status. 2495ea022d16SRodney W. Grimes */ 2496ea022d16SRodney W. Grimes void 2497e4bc453cSWarner Losh dologout(int status) 2498ea022d16SRodney W. Grimes { 24990b4df2eeSDavid Greenman /* 25000b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 25010b4df2eeSDavid Greenman * back to the main program loop. 25020b4df2eeSDavid Greenman */ 25030b4df2eeSDavid Greenman transflag = 0; 2504ea022d16SRodney W. Grimes 25055d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) { 2506ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 250746948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 2508ea022d16SRodney W. Grimes } 2509ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2510ea022d16SRodney W. Grimes _exit(status); 2511ea022d16SRodney W. Grimes } 2512ea022d16SRodney W. Grimes 2513ea022d16SRodney W. Grimes static void 2514e4bc453cSWarner Losh sigurg(int signo) 2515ea022d16SRodney W. Grimes { 25164b82fc95SYaroslav Tykhiy 25174b82fc95SYaroslav Tykhiy recvurg = 1; 25184b82fc95SYaroslav Tykhiy } 25194b82fc95SYaroslav Tykhiy 25204b82fc95SYaroslav Tykhiy static void 2521e4bc453cSWarner Losh myoob(void) 25224b82fc95SYaroslav Tykhiy { 2523ea022d16SRodney W. Grimes char *cp; 2524ea022d16SRodney W. Grimes 2525ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2526ea022d16SRodney W. Grimes if (!transflag) 2527ea022d16SRodney W. Grimes return; 2528ea022d16SRodney W. Grimes cp = tmpline; 2529ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2530ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2531ea022d16SRodney W. Grimes dologout(0); 2532ea022d16SRodney W. Grimes } 2533ea022d16SRodney W. Grimes upper(cp); 2534ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2535ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2536ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 2537ea022d16SRodney W. Grimes reply(226, "Abort successful"); 2538ea022d16SRodney W. Grimes } 2539ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 25409db4bbf3SMichael Haro tmpline[0] = '\0'; 2541ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 2542ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 2543ea022d16SRodney W. Grimes byte_count, file_size); 2544ea022d16SRodney W. Grimes else 2545ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 2546ea022d16SRodney W. Grimes } 2547ea022d16SRodney W. Grimes } 2548ea022d16SRodney W. Grimes 2549ea022d16SRodney W. Grimes /* 2550ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2551ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2552ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2553ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2554ea022d16SRodney W. Grimes */ 2555ea022d16SRodney W. Grimes void 2556e4bc453cSWarner Losh passive(void) 2557ea022d16SRodney W. Grimes { 25588af7c9a3SYaroslav Tykhiy int len, on; 2559ea022d16SRodney W. Grimes char *p, *a; 2560ea022d16SRodney W. Grimes 256161f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 256261f891a6SPaul Traina close(pdata); 256361f891a6SPaul Traina 25644dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2565ea022d16SRodney W. Grimes if (pdata < 0) { 2566ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2567ea022d16SRodney W. Grimes return; 2568ea022d16SRodney W. Grimes } 25698af7c9a3SYaroslav Tykhiy on = 1; 25708af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 25718af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 25724c450ad7SPaul Traina 25734c450ad7SPaul Traina (void) seteuid((uid_t)0); 257461f891a6SPaul Traina 2575dacc9752SPaul Traina #ifdef IP_PORTRANGE 25764dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 25778af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 2578dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2579dacc9752SPaul Traina 258040e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 258157d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 25824c450ad7SPaul Traina goto pasv_error; 25834c450ad7SPaul Traina } 2584dacc9752SPaul Traina #endif 25852db39860SNick Sayer #ifdef IPV6_PORTRANGE 25862db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 25878af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 25882db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 25892db39860SNick Sayer 25902db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 259157d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 25922db39860SNick Sayer goto pasv_error; 25932db39860SNick Sayer } 25942db39860SNick Sayer #endif 259540e9d39eSPeter Wemm 2596ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 25974dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 25984dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2599ea022d16SRodney W. Grimes goto pasv_error; 260061f891a6SPaul Traina 2601ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 26024c450ad7SPaul Traina 2603ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2604ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2605ea022d16SRodney W. Grimes goto pasv_error; 2606ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2607ea022d16SRodney W. Grimes goto pasv_error; 26084dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 26094dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 26104dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 26114dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 26124dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 26134dd8b5abSYoshinobu Inoue else 26144dd8b5abSYoshinobu Inoue goto pasv_error; 26154dd8b5abSYoshinobu Inoue 26164dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2617ea022d16SRodney W. Grimes 2618ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2619ea022d16SRodney W. Grimes 2620ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2621ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2622ea022d16SRodney W. Grimes return; 2623ea022d16SRodney W. Grimes 2624ea022d16SRodney W. Grimes pasv_error: 262561f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2626ea022d16SRodney W. Grimes (void) close(pdata); 2627ea022d16SRodney W. Grimes pdata = -1; 2628ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2629ea022d16SRodney W. Grimes return; 2630ea022d16SRodney W. Grimes } 2631ea022d16SRodney W. Grimes 2632ea022d16SRodney W. Grimes /* 26334dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 26344dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 26354dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 26364dd8b5abSYoshinobu Inoue */ 26374dd8b5abSYoshinobu Inoue 26384dd8b5abSYoshinobu Inoue void 2639e4bc453cSWarner Losh long_passive(char *cmd, int pf) 26404dd8b5abSYoshinobu Inoue { 26418af7c9a3SYaroslav Tykhiy int len, on; 26424dd8b5abSYoshinobu Inoue char *p, *a; 26434dd8b5abSYoshinobu Inoue 26444dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 26454dd8b5abSYoshinobu Inoue close(pdata); 26464dd8b5abSYoshinobu Inoue 26474dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 26484dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 26494dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 26504dd8b5abSYoshinobu Inoue case AF_INET: 26514dd8b5abSYoshinobu Inoue pf = 1; 26524dd8b5abSYoshinobu Inoue break; 26534dd8b5abSYoshinobu Inoue case AF_INET6: 26544dd8b5abSYoshinobu Inoue pf = 2; 26554dd8b5abSYoshinobu Inoue break; 26564dd8b5abSYoshinobu Inoue default: 26574dd8b5abSYoshinobu Inoue pf = 0; 26584dd8b5abSYoshinobu Inoue break; 26594dd8b5abSYoshinobu Inoue } 26604dd8b5abSYoshinobu Inoue /* 26614dd8b5abSYoshinobu Inoue * XXX 26624dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 26634dd8b5abSYoshinobu Inoue */ 26644dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 26654dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 26664dd8b5abSYoshinobu Inoue "use (%d)", pf); 26674dd8b5abSYoshinobu Inoue } else 26684dd8b5abSYoshinobu Inoue reply(501, "Network protocol mismatch"); /*XXX*/ 26694dd8b5abSYoshinobu Inoue 26704dd8b5abSYoshinobu Inoue return; 26714dd8b5abSYoshinobu Inoue } 26724dd8b5abSYoshinobu Inoue } 26734dd8b5abSYoshinobu Inoue 26744dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 26754dd8b5abSYoshinobu Inoue if (pdata < 0) { 26764dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 26774dd8b5abSYoshinobu Inoue return; 26784dd8b5abSYoshinobu Inoue } 26798af7c9a3SYaroslav Tykhiy on = 1; 26808af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 26818af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 26824dd8b5abSYoshinobu Inoue 26834dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)0); 26844dd8b5abSYoshinobu Inoue 26854dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 26864dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 26874dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 26884dd8b5abSYoshinobu Inoue 26892db39860SNick Sayer #ifdef IP_PORTRANGE 26902db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 26918af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 26922db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 26932db39860SNick Sayer 26942db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 269557d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 26962db39860SNick Sayer goto pasv_error; 26972db39860SNick Sayer } 26982db39860SNick Sayer #endif 26992db39860SNick Sayer #ifdef IPV6_PORTRANGE 27002db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 27018af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 27022db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 27032db39860SNick Sayer 27042db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 270557d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 27062db39860SNick Sayer goto pasv_error; 27072db39860SNick Sayer } 27082db39860SNick Sayer #endif 27092db39860SNick Sayer 27104dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 27114dd8b5abSYoshinobu Inoue goto pasv_error; 27124dd8b5abSYoshinobu Inoue 27134dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 27144dd8b5abSYoshinobu Inoue 27154dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 27164dd8b5abSYoshinobu Inoue goto pasv_error; 27174dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 27184dd8b5abSYoshinobu Inoue goto pasv_error; 27194dd8b5abSYoshinobu Inoue 27204dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 27214dd8b5abSYoshinobu Inoue 27224dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 27234dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 27244dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 27254dd8b5abSYoshinobu Inoue case AF_INET: 27264dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 27274dd8b5abSYoshinobu Inoue v4_reply: 27284dd8b5abSYoshinobu Inoue reply(228, 27294dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 27304dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 27314dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 27324dd8b5abSYoshinobu Inoue return; 27334dd8b5abSYoshinobu Inoue case AF_INET6: 27344dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 27354dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 27364dd8b5abSYoshinobu Inoue goto v4_reply; 27374dd8b5abSYoshinobu Inoue } 27384dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 27394dd8b5abSYoshinobu Inoue reply(228, 27404dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 27414dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 27424dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 27434dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 27444dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 27454dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 27464dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 27474dd8b5abSYoshinobu Inoue return; 27484dd8b5abSYoshinobu Inoue } 27494dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 27504dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 27514dd8b5abSYoshinobu Inoue case AF_INET: 27524dd8b5abSYoshinobu Inoue case AF_INET6: 27534dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 27544dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 27554dd8b5abSYoshinobu Inoue return; 27564dd8b5abSYoshinobu Inoue } 27574dd8b5abSYoshinobu Inoue } else { 27584dd8b5abSYoshinobu Inoue /* more proper error code? */ 27594dd8b5abSYoshinobu Inoue } 27604dd8b5abSYoshinobu Inoue 27614dd8b5abSYoshinobu Inoue pasv_error: 27624dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 27634dd8b5abSYoshinobu Inoue (void) close(pdata); 27644dd8b5abSYoshinobu Inoue pdata = -1; 27654dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 27664dd8b5abSYoshinobu Inoue return; 27674dd8b5abSYoshinobu Inoue } 27684dd8b5abSYoshinobu Inoue 27694dd8b5abSYoshinobu Inoue /* 2770a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local" 2771a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races. 2772a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 2773a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name. 2774a117c345SYaroslav Tykhiy * 2775ea022d16SRodney W. Grimes * Generates failure reply on error. 2776ea022d16SRodney W. Grimes */ 2777a117c345SYaroslav Tykhiy static int 2778a117c345SYaroslav Tykhiy guniquefd(char *local, char **name) 2779ea022d16SRodney W. Grimes { 2780ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2781ea022d16SRodney W. Grimes struct stat st; 2782ea022d16SRodney W. Grimes char *cp; 2783a117c345SYaroslav Tykhiy int count; 2784a117c345SYaroslav Tykhiy int fd; 2785ea022d16SRodney W. Grimes 2786ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2787ea022d16SRodney W. Grimes if (cp) 2788ea022d16SRodney W. Grimes *cp = '\0'; 2789ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2790ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2791a117c345SYaroslav Tykhiy return (-1); 2792ea022d16SRodney W. Grimes } 2793a117c345SYaroslav Tykhiy if (cp) { 2794a117c345SYaroslav Tykhiy /* 2795a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix. 2796a117c345SYaroslav Tykhiy * -4 is for /nn\0 2797a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix. 2798a117c345SYaroslav Tykhiy */ 2799a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) { 2800a117c345SYaroslav Tykhiy reply(553, "Pathname too long"); 2801a117c345SYaroslav Tykhiy return (-1); 2802a117c345SYaroslav Tykhiy } 2803ea022d16SRodney W. Grimes *cp = '/'; 2804a117c345SYaroslav Tykhiy } 2805e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2806e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2807ea022d16SRodney W. Grimes cp = new + strlen(new); 2808a117c345SYaroslav Tykhiy /* 2809a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly. 2810a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off 2811a117c345SYaroslav Tykhiy * by buffer size. 2812a117c345SYaroslav Tykhiy */ 2813a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/') 2814ea022d16SRodney W. Grimes *cp++ = '.'; 2815a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) { 2816a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */ 2817a117c345SYaroslav Tykhiy if (count) 2818ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2819a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local, 2820a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 2821a117c345SYaroslav Tykhiy *name = count ? new : local; 2822a117c345SYaroslav Tykhiy return (fd); 2823a117c345SYaroslav Tykhiy } 2824ea022d16SRodney W. Grimes } 2825ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2826a117c345SYaroslav Tykhiy return (-1); 2827ea022d16SRodney W. Grimes } 2828ea022d16SRodney W. Grimes 2829ea022d16SRodney W. Grimes /* 2830ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2831ea022d16SRodney W. Grimes */ 2832ea022d16SRodney W. Grimes void 2833e4bc453cSWarner Losh perror_reply(int code, char *string) 2834ea022d16SRodney W. Grimes { 2835ea022d16SRodney W. Grimes 2836ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2837ea022d16SRodney W. Grimes } 2838ea022d16SRodney W. Grimes 2839ea022d16SRodney W. Grimes static char *onefile[] = { 2840ea022d16SRodney W. Grimes "", 2841ea022d16SRodney W. Grimes 0 2842ea022d16SRodney W. Grimes }; 2843ea022d16SRodney W. Grimes 2844ea022d16SRodney W. Grimes void 2845e4bc453cSWarner Losh send_file_list(char *whichf) 2846ea022d16SRodney W. Grimes { 2847ea022d16SRodney W. Grimes struct stat st; 2848ea022d16SRodney W. Grimes DIR *dirp = NULL; 2849ea022d16SRodney W. Grimes struct dirent *dir; 2850ea022d16SRodney W. Grimes FILE *dout = NULL; 2851ea022d16SRodney W. Grimes char **dirlist, *dirname; 2852ea022d16SRodney W. Grimes int simple = 0; 2853ea022d16SRodney W. Grimes int freeglob = 0; 2854ea022d16SRodney W. Grimes glob_t gl; 2855ea022d16SRodney W. Grimes 2856ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 285712da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 2858ea022d16SRodney W. Grimes 2859ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 28606d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 286175dc5f1aSMike Heffner flags |= GLOB_LIMIT; 2862ea022d16SRodney W. Grimes freeglob = 1; 2863ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2864ea022d16SRodney W. Grimes reply(550, "not found"); 2865ea022d16SRodney W. Grimes goto out; 2866ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2867ea022d16SRodney W. Grimes errno = ENOENT; 2868ea022d16SRodney W. Grimes perror_reply(550, whichf); 2869ea022d16SRodney W. Grimes goto out; 2870ea022d16SRodney W. Grimes } 2871ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2872ea022d16SRodney W. Grimes } else { 2873ea022d16SRodney W. Grimes onefile[0] = whichf; 2874ea022d16SRodney W. Grimes dirlist = onefile; 2875ea022d16SRodney W. Grimes simple = 1; 2876ea022d16SRodney W. Grimes } 2877ea022d16SRodney W. Grimes 28789aca17cbSMark Murray while ((dirname = *dirlist++)) { 2879ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2880ea022d16SRodney W. Grimes /* 2881ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2882ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2883ea022d16SRodney W. Grimes */ 2884ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2885ea022d16SRodney W. Grimes transflag == 0) { 2886af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2887ea022d16SRodney W. Grimes goto out; 2888ea022d16SRodney W. Grimes } 2889ea022d16SRodney W. Grimes perror_reply(550, whichf); 2890ea022d16SRodney W. Grimes if (dout != NULL) { 2891ea022d16SRodney W. Grimes (void) fclose(dout); 2892ea022d16SRodney W. Grimes transflag = 0; 2893ea022d16SRodney W. Grimes data = -1; 2894ea022d16SRodney W. Grimes pdata = -1; 2895ea022d16SRodney W. Grimes } 2896ea022d16SRodney W. Grimes goto out; 2897ea022d16SRodney W. Grimes } 2898ea022d16SRodney W. Grimes 2899ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2900ea022d16SRodney W. Grimes if (dout == NULL) { 2901ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2902ea022d16SRodney W. Grimes if (dout == NULL) 2903ea022d16SRodney W. Grimes goto out; 2904ea022d16SRodney W. Grimes transflag++; 2905ea022d16SRodney W. Grimes } 2906ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2907ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2908ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2909ea022d16SRodney W. Grimes continue; 2910ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2911ea022d16SRodney W. Grimes continue; 2912ea022d16SRodney W. Grimes 2913ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 2914ea022d16SRodney W. Grimes continue; 2915ea022d16SRodney W. Grimes 2916ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 2917ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 2918ea022d16SRodney W. Grimes 29194b82fc95SYaroslav Tykhiy if (recvurg) { 29204b82fc95SYaroslav Tykhiy myoob(); 29214b82fc95SYaroslav Tykhiy recvurg = 0; 29224b82fc95SYaroslav Tykhiy transflag = 0; 29234b82fc95SYaroslav Tykhiy goto out; 29244b82fc95SYaroslav Tykhiy } 29254b82fc95SYaroslav Tykhiy 2926ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2927ea022d16SRodney W. Grimes continue; 2928ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2929ea022d16SRodney W. Grimes dir->d_namlen == 2) 2930ea022d16SRodney W. Grimes continue; 2931ea022d16SRodney W. Grimes 2932e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 2933e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 2934ea022d16SRodney W. Grimes 2935ea022d16SRodney W. Grimes /* 2936ea022d16SRodney W. Grimes * We have to do a stat to insure it's 2937ea022d16SRodney W. Grimes * not a directory or special file. 2938ea022d16SRodney W. Grimes */ 2939ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 2940ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 2941ea022d16SRodney W. Grimes if (dout == NULL) { 2942ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 2943ea022d16SRodney W. Grimes "w"); 2944ea022d16SRodney W. Grimes if (dout == NULL) 2945ea022d16SRodney W. Grimes goto out; 2946ea022d16SRodney W. Grimes transflag++; 2947ea022d16SRodney W. Grimes } 2948ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 2949ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 2950ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2951ea022d16SRodney W. Grimes else 2952ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 2953ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2954ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 2955ea022d16SRodney W. Grimes } 2956ea022d16SRodney W. Grimes } 2957ea022d16SRodney W. Grimes (void) closedir(dirp); 2958ea022d16SRodney W. Grimes } 2959ea022d16SRodney W. Grimes 2960ea022d16SRodney W. Grimes if (dout == NULL) 2961ea022d16SRodney W. Grimes reply(550, "No files found."); 2962ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 2963ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 2964ea022d16SRodney W. Grimes else 2965ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 2966ea022d16SRodney W. Grimes 2967ea022d16SRodney W. Grimes transflag = 0; 2968ea022d16SRodney W. Grimes if (dout != NULL) 2969ea022d16SRodney W. Grimes (void) fclose(dout); 2970ea022d16SRodney W. Grimes data = -1; 2971ea022d16SRodney W. Grimes pdata = -1; 2972ea022d16SRodney W. Grimes out: 2973ea022d16SRodney W. Grimes if (freeglob) { 2974ea022d16SRodney W. Grimes freeglob = 0; 2975ea022d16SRodney W. Grimes globfree(&gl); 2976ea022d16SRodney W. Grimes } 2977ea022d16SRodney W. Grimes } 2978ea022d16SRodney W. Grimes 2979cf09a206SDavid Greenman void 2980e4bc453cSWarner Losh reapchild(int signo) 2981cf09a206SDavid Greenman { 2982cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 2983cf09a206SDavid Greenman } 2984cf09a206SDavid Greenman 2985b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2986ea022d16SRodney W. Grimes /* 2987ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2988ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2989ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2990ea022d16SRodney W. Grimes */ 2991ea022d16SRodney W. Grimes void 2992ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2993ea022d16SRodney W. Grimes { 2994ea022d16SRodney W. Grimes int i; 2995ea022d16SRodney W. Grimes va_list ap; 2996ea022d16SRodney W. Grimes char *p, *bp, ch; 2997ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2998ea022d16SRodney W. Grimes 2999ea022d16SRodney W. Grimes va_start(ap, fmt); 3000ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3001ea022d16SRodney W. Grimes 3002ea022d16SRodney W. Grimes /* make ps print our process name */ 3003ea022d16SRodney W. Grimes p = Argv[0]; 3004ea022d16SRodney W. Grimes *p++ = '-'; 3005ea022d16SRodney W. Grimes 3006ea022d16SRodney W. Grimes i = strlen(buf); 3007ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 3008ea022d16SRodney W. Grimes i = LastArgv - p - 2; 3009ea022d16SRodney W. Grimes buf[i] = '\0'; 3010ea022d16SRodney W. Grimes } 3011ea022d16SRodney W. Grimes bp = buf; 3012ea022d16SRodney W. Grimes while (ch = *bp++) 3013ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 3014ea022d16SRodney W. Grimes *p++ = ch; 3015ea022d16SRodney W. Grimes while (p < LastArgv) 3016ea022d16SRodney W. Grimes *p++ = ' '; 3017ea022d16SRodney W. Grimes } 3018b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 30193eb568f2SGuido van Rooij 302061f891a6SPaul Traina static void 3021e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start) 30223eb568f2SGuido van Rooij { 30233eb568f2SGuido van Rooij char buf[1024]; 30243eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 3025158a00b2SJohn Birrell time_t now; 30263eb568f2SGuido van Rooij 30273eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 30283eb568f2SGuido van Rooij time(&now); 3029e4a71114SAndrey A. Chernov snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n", 30303eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 3031e4a71114SAndrey A. Chernov path, name, (long long)size, 3032e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 30333eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 30343eb568f2SGuido van Rooij } 30353eb568f2SGuido van Rooij } 3036e4648f05SYaroslav Tykhiy 3037e4648f05SYaroslav Tykhiy static char * 3038e4648f05SYaroslav Tykhiy doublequote(char *s) 3039e4648f05SYaroslav Tykhiy { 3040e4648f05SYaroslav Tykhiy int n; 3041e4648f05SYaroslav Tykhiy char *p, *s2; 3042e4648f05SYaroslav Tykhiy 3043e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++) 3044e4648f05SYaroslav Tykhiy if (*p == '"') 3045e4648f05SYaroslav Tykhiy n++; 3046e4648f05SYaroslav Tykhiy 3047e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL) 3048e4648f05SYaroslav Tykhiy return (NULL); 3049e4648f05SYaroslav Tykhiy 3050e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) { 3051e4648f05SYaroslav Tykhiy if ((*p = *s) == '"') 3052e4648f05SYaroslav Tykhiy *(++p) = '"'; 3053e4648f05SYaroslav Tykhiy } 3054e4648f05SYaroslav Tykhiy *p = '\0'; 3055e4648f05SYaroslav Tykhiy 3056e4648f05SYaroslav Tykhiy return (s2); 3057e4648f05SYaroslav Tykhiy } 3058