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; 119c152df28SYaroslav Tykhiy int hostinfo = 1; /* print host-specific info in messages */ 120ea022d16SRodney W. Grimes int logged_in; 121ea022d16SRodney W. Grimes struct passwd *pw; 122ce9287fcSYaroslav Tykhiy char *homedir; 123618b0bbaSMark Murray int ftpdebug; 124ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 125ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 126ea022d16SRodney W. Grimes int logging; 1274c450ad7SPaul Traina int restricted_data_ports = 1; 128a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1295a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 130ea022d16SRodney W. Grimes int guest; 131a5a4544eSPaul Traina int dochroot; 1325d7e0128SYaroslav Tykhiy int dowtmp = 1; 1333eb568f2SGuido van Rooij int stats; 1343eb568f2SGuido van Rooij int statfd = -1; 135ea022d16SRodney W. Grimes int type; 136ea022d16SRodney W. Grimes int form; 137ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 138ea022d16SRodney W. Grimes int mode; 139ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 140ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 141a4b77a2aSPoul-Henning Kamp int readonly=0; /* Server is in readonly mode. */ 142a4b77a2aSPoul-Henning Kamp int noepsv=0; /* EPSV command is disabled. */ 14362513e76SNik Clayton int noretr=0; /* RETR command is disabled. */ 1441cc9f0bbSSheldon Hearn int noguestretr=0; /* RETR command is disabled for anon users. */ 145d186bb12SMatthew N. Dodd int noguestmkd=0; /* MKD command is disabled for anon users. */ 146a117c345SYaroslav Tykhiy int noguestmod=1; /* anon users may not modify existing files. */ 14762513e76SNik Clayton 1484b82fc95SYaroslav Tykhiy static volatile sig_atomic_t recvurg; 149ea022d16SRodney W. Grimes sig_atomic_t transflag; 150ea022d16SRodney W. Grimes off_t file_size; 151ea022d16SRodney W. Grimes off_t byte_count; 152ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 153ea022d16SRodney W. Grimes #undef CMASK 154ea022d16SRodney W. Grimes #define CMASK 027 155ea022d16SRodney W. Grimes #endif 156ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 157ea022d16SRodney W. Grimes char tmpline[7]; 158ea4e54b9SDavid Nugent char *hostname; 159ad442344SDima Dorfman int epsvall = 0; 160ad442344SDima Dorfman 1610512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 162ea4e54b9SDavid Nugent char *ftpuser; 163ea4e54b9SDavid Nugent 164ea4e54b9SDavid Nugent static struct ftphost { 165ea4e54b9SDavid Nugent struct ftphost *next; 166f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 167ea4e54b9SDavid Nugent char *hostname; 168ea4e54b9SDavid Nugent char *anonuser; 169ea4e54b9SDavid Nugent char *statfile; 170ea4e54b9SDavid Nugent char *welcome; 171ea4e54b9SDavid Nugent char *loginmsg; 172ea4e54b9SDavid Nugent } *thishost, *firsthost; 173ea4e54b9SDavid Nugent 174ea4e54b9SDavid Nugent #endif 1759e9a43bdSBrian Somers char remotehost[MAXHOSTNAMELEN]; 1763eb568f2SGuido van Rooij char *ident = NULL; 177a5a4544eSPaul Traina 178a5a4544eSPaul Traina static char ttyline[20]; 179a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 180a5a4544eSPaul Traina 1815bc9d93dSMark Murray #ifdef USE_PAM 182e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*); 1835bc9d93dSMark Murray pam_handle_t *pamh = NULL; 18447499ecdSAndrey A. Chernov #endif 185fa1746c9SMark Murray 186fa1746c9SMark Murray static struct opie opiedata; 187fa1746c9SMark Murray static char opieprompt[OPIE_CHALLENGE_MAX+1]; 18847499ecdSAndrey A. Chernov static int pwok; 1899aca17cbSMark Murray 190105a3c98SJulian Elischer char *pid_file = NULL; 191105a3c98SJulian Elischer 192ea022d16SRodney W. Grimes /* 1936d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return. 1946d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited. 1956d10cb2fSJonathan Lemon */ 1966d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384 1976d10cb2fSJonathan Lemon # 1986d10cb2fSJonathan Lemon 1996d10cb2fSJonathan Lemon /* 200ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 201ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 202ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 203ea022d16SRodney W. Grimes */ 204ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 205ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 206ea022d16SRodney W. Grimes 207ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 208ea022d16SRodney W. Grimes int swaitint = SWAITINT; 209ea022d16SRodney W. Grimes 210ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 211b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 212ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 213ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 214b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 215ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 216ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 217ea022d16SRodney W. Grimes 218ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 219ea022d16SRodney W. Grimes if (logging > 1) \ 220ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 221ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 222ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 223ea022d16SRodney W. Grimes if (logging > 1) \ 224ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 225ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 226ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 227ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 228ea022d16SRodney W. Grimes if (logging > 1) { \ 229ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 230ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 231ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 232ea022d16SRodney W. Grimes else \ 233ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 234ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 235ea022d16SRodney W. Grimes } 236ea022d16SRodney W. Grimes 237ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 238e4bc453cSWarner Losh static void inithosts(void); 239e4bc453cSWarner Losh static void selecthost(union sockunion *); 240ea4e54b9SDavid Nugent #endif 241e4bc453cSWarner Losh static void ack(char *); 242e4bc453cSWarner Losh static void sigurg(int); 243e4bc453cSWarner Losh static void myoob(void); 2448657b576SYaroslav Tykhiy static int checkuser(char *, char *, int, char **); 245e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *); 246e4bc453cSWarner Losh static void dolog(struct sockaddr *); 247e4bc453cSWarner Losh static char *curdir(void); 248e4bc453cSWarner Losh static void end_login(void); 249e4bc453cSWarner Losh static FILE *getdatasock(char *); 250a117c345SYaroslav Tykhiy static int guniquefd(char *, char **); 251e4bc453cSWarner Losh static void lostconn(int); 252e4bc453cSWarner Losh static void sigquit(int); 253e4bc453cSWarner Losh static int receive_data(FILE *, FILE *); 254e4bc453cSWarner Losh static int send_data(FILE *, FILE *, off_t, off_t, int); 255ea022d16SRodney W. Grimes static struct passwd * 256e4bc453cSWarner Losh sgetpwnam(char *); 257e4bc453cSWarner Losh static char *sgetsave(char *); 258e4bc453cSWarner Losh static void reapchild(int); 259e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t); 260e4648f05SYaroslav Tykhiy static char *doublequote(char *); 261ea022d16SRodney W. Grimes 262ea022d16SRodney W. Grimes static char * 263e4bc453cSWarner Losh curdir(void) 264ea022d16SRodney W. Grimes { 265ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 266ea022d16SRodney W. Grimes 267ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 268ea022d16SRodney W. Grimes return (""); 269ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 270ea022d16SRodney W. Grimes strcat(path, "/"); 271ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 272ea022d16SRodney W. Grimes return (guest ? path+1 : path); 273ea022d16SRodney W. Grimes } 274ea022d16SRodney W. Grimes 275ea022d16SRodney W. Grimes int 276e4bc453cSWarner Losh main(int argc, char *argv[], char **envp) 277ea022d16SRodney W. Grimes { 278ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 279ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 280ea022d16SRodney W. Grimes FILE *fd; 2814dd8b5abSYoshinobu Inoue int error; 2824dd8b5abSYoshinobu Inoue char *bindname = NULL; 28363591ba5SYaroslav Tykhiy const char *bindport = "ftp"; 2844dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2854dd8b5abSYoshinobu Inoue int enable_v4 = 0; 2864b82fc95SYaroslav Tykhiy struct sigaction sa; 287ea022d16SRodney W. Grimes 28834d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 2894b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask); 2904b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 29134d1ba5cSAndrey A. Chernov 292b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 293ea022d16SRodney W. Grimes /* 294ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 295ea022d16SRodney W. Grimes */ 296ea022d16SRodney W. Grimes Argv = argv; 297ea022d16SRodney W. Grimes while (*envp) 298ea022d16SRodney W. Grimes envp++; 299ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 300b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 301ea022d16SRodney W. Grimes 3023eb568f2SGuido van Rooij 303c152df28SYaroslav Tykhiy while ((ch = getopt(argc, argv, 304c152df28SYaroslav Tykhiy "46a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) { 305ea022d16SRodney W. Grimes switch (ch) { 3060e063efeSYaroslav Tykhiy case '4': 3070e063efeSYaroslav Tykhiy enable_v4 = 1; 3080e063efeSYaroslav Tykhiy if (family == AF_UNSPEC) 3090e063efeSYaroslav Tykhiy family = AF_INET; 3100e063efeSYaroslav Tykhiy break; 3110e063efeSYaroslav Tykhiy 3120e063efeSYaroslav Tykhiy case '6': 3130e063efeSYaroslav Tykhiy family = AF_INET6; 3140e063efeSYaroslav Tykhiy break; 3150e063efeSYaroslav Tykhiy 3160e063efeSYaroslav Tykhiy case 'a': 3170e063efeSYaroslav Tykhiy bindname = optarg; 3180e063efeSYaroslav Tykhiy break; 3190e063efeSYaroslav Tykhiy 3200e063efeSYaroslav Tykhiy case 'A': 3210e063efeSYaroslav Tykhiy anon_only = 1; 322cf09a206SDavid Greenman break; 323cf09a206SDavid Greenman 324ea022d16SRodney W. Grimes case 'd': 325618b0bbaSMark Murray ftpdebug++; 326ea022d16SRodney W. Grimes break; 327ea022d16SRodney W. Grimes 3280e063efeSYaroslav Tykhiy case 'D': 3290e063efeSYaroslav Tykhiy daemon_mode++; 3300e063efeSYaroslav Tykhiy break; 3310e063efeSYaroslav Tykhiy 332a4b77a2aSPoul-Henning Kamp case 'E': 333a4b77a2aSPoul-Henning Kamp noepsv = 1; 334a4b77a2aSPoul-Henning Kamp break; 335a4b77a2aSPoul-Henning Kamp 336c152df28SYaroslav Tykhiy case 'h': 337c152df28SYaroslav Tykhiy hostinfo = 0; 338c152df28SYaroslav Tykhiy break; 339c152df28SYaroslav Tykhiy 340ea022d16SRodney W. Grimes case 'l': 341ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 342ea022d16SRodney W. Grimes break; 343ea022d16SRodney W. Grimes 344a117c345SYaroslav Tykhiy case 'm': 345a117c345SYaroslav Tykhiy noguestmod = 0; 346a117c345SYaroslav Tykhiy break; 347a117c345SYaroslav Tykhiy 3480e063efeSYaroslav Tykhiy case 'M': 3490e063efeSYaroslav Tykhiy noguestmkd = 1; 3500e063efeSYaroslav Tykhiy break; 3510e063efeSYaroslav Tykhiy 3520e063efeSYaroslav Tykhiy case 'o': 3530e063efeSYaroslav Tykhiy noretr = 1; 3540e063efeSYaroslav Tykhiy break; 3550e063efeSYaroslav Tykhiy 3560e063efeSYaroslav Tykhiy case 'O': 3570e063efeSYaroslav Tykhiy noguestretr = 1; 3580e063efeSYaroslav Tykhiy break; 3590e063efeSYaroslav Tykhiy 3600e063efeSYaroslav Tykhiy case 'p': 3610e063efeSYaroslav Tykhiy pid_file = optarg; 3620e063efeSYaroslav Tykhiy break; 3630e063efeSYaroslav Tykhiy 36463591ba5SYaroslav Tykhiy case 'P': 36563591ba5SYaroslav Tykhiy bindport = optarg; 36663591ba5SYaroslav Tykhiy break; 36763591ba5SYaroslav Tykhiy 368a4b77a2aSPoul-Henning Kamp case 'r': 369a4b77a2aSPoul-Henning Kamp readonly = 1; 370a4b77a2aSPoul-Henning Kamp break; 371a4b77a2aSPoul-Henning Kamp 372a5a4544eSPaul Traina case 'R': 373a5a4544eSPaul Traina paranoid = 0; 374a5a4544eSPaul Traina break; 375a5a4544eSPaul Traina 376a5a4544eSPaul Traina case 'S': 377a5a4544eSPaul Traina stats++; 378a5a4544eSPaul Traina break; 379a5a4544eSPaul Traina 380ea022d16SRodney W. Grimes case 't': 381ea022d16SRodney W. Grimes timeout = atoi(optarg); 382ea022d16SRodney W. Grimes if (maxtimeout < timeout) 383ea022d16SRodney W. Grimes maxtimeout = timeout; 384ea022d16SRodney W. Grimes break; 385a5a4544eSPaul Traina 3860e063efeSYaroslav Tykhiy case 'T': 3870e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg); 3880e063efeSYaroslav Tykhiy if (timeout > maxtimeout) 3890e063efeSYaroslav Tykhiy timeout = maxtimeout; 390105a3c98SJulian Elischer break; 391105a3c98SJulian Elischer 392ea022d16SRodney W. Grimes case 'u': 393ea022d16SRodney W. Grimes { 394ea022d16SRodney W. Grimes long val = 0; 395ea022d16SRodney W. Grimes 396ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 397ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 398ea022d16SRodney W. Grimes warnx("bad value for -u"); 399ea022d16SRodney W. Grimes else 400ea022d16SRodney W. Grimes defumask = val; 401ea022d16SRodney W. Grimes break; 402ea022d16SRodney W. Grimes } 4030e063efeSYaroslav Tykhiy case 'U': 4040e063efeSYaroslav Tykhiy restricted_data_ports = 0; 4055a392aecSTorsten Blum break; 406ea022d16SRodney W. Grimes 407ea022d16SRodney W. Grimes case 'v': 40893bd9dc5SYaroslav Tykhiy ftpdebug++; 409ea022d16SRodney W. Grimes break; 410ea022d16SRodney W. Grimes 4115d7e0128SYaroslav Tykhiy case 'W': 4125d7e0128SYaroslav Tykhiy dowtmp = 0; 4135d7e0128SYaroslav Tykhiy break; 4145d7e0128SYaroslav Tykhiy 415ea022d16SRodney W. Grimes default: 416ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 417ea022d16SRodney W. Grimes break; 418ea022d16SRodney W. Grimes } 419ea022d16SRodney W. Grimes } 420cf09a206SDavid Greenman 421ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 422ea4e54b9SDavid Nugent inithosts(); 423ea4e54b9SDavid Nugent #endif 424ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 425cf09a206SDavid Greenman 426cf09a206SDavid Greenman /* 427cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 428cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 429cf09a206SDavid Greenman */ 430cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 431cf09a206SDavid Greenman 432cf09a206SDavid Greenman if (daemon_mode) { 433cf09a206SDavid Greenman int ctl_sock, fd; 4344dd8b5abSYoshinobu Inoue struct addrinfo hints, *res; 435cf09a206SDavid Greenman 436cf09a206SDavid Greenman /* 437cf09a206SDavid Greenman * Detach from parent. 438cf09a206SDavid Greenman */ 439cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 440cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 441cf09a206SDavid Greenman exit(1); 442cf09a206SDavid Greenman } 4434b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild; 4444b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 4454dd8b5abSYoshinobu Inoue /* init bind_sa */ 4464dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 4474dd8b5abSYoshinobu Inoue 4484dd8b5abSYoshinobu Inoue hints.ai_family = family == AF_UNSPEC ? AF_INET : family; 4494dd8b5abSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 4504dd8b5abSYoshinobu Inoue hints.ai_protocol = 0; 4514dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 45263591ba5SYaroslav Tykhiy error = getaddrinfo(bindname, bindport, &hints, &res); 4534dd8b5abSYoshinobu Inoue if (error) { 4544dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) { 4554dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 45663591ba5SYaroslav Tykhiy error = getaddrinfo(bindname, bindport, &hints, 4574dd8b5abSYoshinobu Inoue &res); 4584dd8b5abSYoshinobu Inoue } 4594dd8b5abSYoshinobu Inoue } 4604dd8b5abSYoshinobu Inoue if (error) { 4613fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", gai_strerror(error)); 4624dd8b5abSYoshinobu Inoue if (error == EAI_SYSTEM) 4633fb3b78fSKris Kennaway syslog(LOG_ERR, "%s", strerror(errno)); 4644dd8b5abSYoshinobu Inoue exit(1); 4654dd8b5abSYoshinobu Inoue } 4664dd8b5abSYoshinobu Inoue if (res->ai_addr == NULL) { 4674dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 468cf09a206SDavid Greenman exit(1); 4692310b8c6SRuslan Ermilov } else 4702310b8c6SRuslan Ermilov family = res->ai_addr->sa_family; 471cf09a206SDavid Greenman /* 472cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 473cf09a206SDavid Greenman * listening. 474cf09a206SDavid Greenman */ 4754dd8b5abSYoshinobu Inoue ctl_sock = socket(family, SOCK_STREAM, 0); 476cf09a206SDavid Greenman if (ctl_sock < 0) { 477cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 478cf09a206SDavid Greenman exit(1); 479cf09a206SDavid Greenman } 480cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 48157d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 482406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, 483406d1ae9SYaroslav Tykhiy "control setsockopt (SO_REUSEADDR): %m"); 4844dd8b5abSYoshinobu Inoue if (family == AF_INET6 && enable_v4 == 0) { 485fc99a00cSHajimu UMEMOTO if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_V6ONLY, 48657d4ef07SYaroslav Tykhiy &on, sizeof (on)) < 0) 487406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, 488fc99a00cSHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m"); 4894dd8b5abSYoshinobu Inoue } 4904dd8b5abSYoshinobu Inoue memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len); 4914dd8b5abSYoshinobu Inoue if (bind(ctl_sock, (struct sockaddr *)&server_addr, 4924dd8b5abSYoshinobu Inoue server_addr.su_len) < 0) { 493cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 494cf09a206SDavid Greenman exit(1); 495cf09a206SDavid Greenman } 496cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 497cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 498cf09a206SDavid Greenman exit(1); 499cf09a206SDavid Greenman } 500cf09a206SDavid Greenman /* 501105a3c98SJulian Elischer * Atomically write process ID 502105a3c98SJulian Elischer */ 503105a3c98SJulian Elischer if (pid_file) 504105a3c98SJulian Elischer { 505105a3c98SJulian Elischer int fd; 506105a3c98SJulian Elischer char buf[20]; 507105a3c98SJulian Elischer 508105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 509105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 51085966371SWarner Losh if (fd < 0) { 511105a3c98SJulian Elischer if (errno == EAGAIN) 512105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 513105a3c98SJulian Elischer else 514105a3c98SJulian Elischer err(1, "%s", pid_file); 51585966371SWarner Losh } 516105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 517105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 518105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 519105a3c98SJulian Elischer err(1, "%s: write", pid_file); 520105a3c98SJulian Elischer /* Leave the pid file open and locked */ 521105a3c98SJulian Elischer } 522105a3c98SJulian Elischer /* 523cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 524cf09a206SDavid Greenman * children to handle them. 525cf09a206SDavid Greenman */ 526cf09a206SDavid Greenman while (1) { 5274dd8b5abSYoshinobu Inoue addrlen = server_addr.su_len; 528cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 529cf09a206SDavid Greenman if (fork() == 0) { 530cf09a206SDavid Greenman /* child */ 531cf09a206SDavid Greenman (void) dup2(fd, 0); 532cf09a206SDavid Greenman (void) dup2(fd, 1); 533cf09a206SDavid Greenman close(ctl_sock); 534cf09a206SDavid Greenman break; 535cf09a206SDavid Greenman } 536cf09a206SDavid Greenman close(fd); 537cf09a206SDavid Greenman } 538cf09a206SDavid Greenman } else { 539cf09a206SDavid Greenman addrlen = sizeof(his_addr); 540cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 541cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 542cf09a206SDavid Greenman exit(1); 543cf09a206SDavid Greenman } 544cf09a206SDavid Greenman } 545cf09a206SDavid Greenman 5464b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL; 5474b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL); 5484b82fc95SYaroslav Tykhiy 5494b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg; 5504b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ 5514b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL); 5524b82fc95SYaroslav Tykhiy 5534b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */ 5544b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART; 5554b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit; 5564b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL); 5574b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL); 5584b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL); 5594b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL); 5604b82fc95SYaroslav Tykhiy 5614b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn; 5624b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL); 563ea022d16SRodney W. Grimes 564cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 565cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 566cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 567cf09a206SDavid Greenman exit(1); 568cf09a206SDavid Greenman } 56963591ba5SYaroslav Tykhiy dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */ 570ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 571ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5724dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 573ea4e54b9SDavid Nugent #endif 574cf09a206SDavid Greenman #ifdef IP_TOS 5754dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5764dd8b5abSYoshinobu Inoue { 577cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 57857d4ef07SYaroslav Tykhiy if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 579406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); 5804dd8b5abSYoshinobu Inoue } 581cf09a206SDavid Greenman #endif 582dadb9fb3SDavid Greenman /* 583dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 584dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 585dadb9fb3SDavid Greenman */ 586dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 587406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); 588dadb9fb3SDavid Greenman 5894dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 590cf09a206SDavid Greenman 591a5a4544eSPaul Traina /* set this here so klogin can use it... */ 592e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 593a5a4544eSPaul Traina 594ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 595ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 59657d4ef07SYaroslav Tykhiy if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) 597406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); 598ea022d16SRodney W. Grimes #endif 599ea022d16SRodney W. Grimes 600ea022d16SRodney W. Grimes #ifdef F_SETOWN 601ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 602ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 603ea022d16SRodney W. Grimes #endif 6044dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 605ea022d16SRodney W. Grimes /* 606ea022d16SRodney W. Grimes * Set up default state 607ea022d16SRodney W. Grimes */ 608ea022d16SRodney W. Grimes data = -1; 609ea022d16SRodney W. Grimes type = TYPE_A; 610ea022d16SRodney W. Grimes form = FORM_N; 611ea022d16SRodney W. Grimes stru = STRU_F; 612ea022d16SRodney W. Grimes mode = MODE_S; 613ea022d16SRodney W. Grimes tmpline[0] = '\0'; 614ea022d16SRodney W. Grimes 615ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 616ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 617ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 618ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 619ea022d16SRodney W. Grimes *cp = '\0'; 620ea022d16SRodney W. Grimes lreply(530, "%s", line); 621ea022d16SRodney W. Grimes } 622ea022d16SRodney W. Grimes (void) fflush(stdout); 623ea022d16SRodney W. Grimes (void) fclose(fd); 624ea022d16SRodney W. Grimes reply(530, "System not available."); 625ea022d16SRodney W. Grimes exit(0); 626ea022d16SRodney W. Grimes } 627ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 628ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 629ea4e54b9SDavid Nugent #else 630ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 631ea4e54b9SDavid Nugent #endif 632ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 633ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 634ea022d16SRodney W. Grimes *cp = '\0'; 635ea022d16SRodney W. Grimes lreply(220, "%s", line); 636ea022d16SRodney W. Grimes } 637ea022d16SRodney W. Grimes (void) fflush(stdout); 638ea022d16SRodney W. Grimes (void) fclose(fd); 639ea022d16SRodney W. Grimes /* reply(220,) must follow */ 640ea022d16SRodney W. Grimes } 641ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 6420512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 643618b0bbaSMark Murray fatalerror("Ran out of memory."); 6449e9a43bdSBrian Somers (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 6459e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 646ea4e54b9SDavid Nugent #endif 647c152df28SYaroslav Tykhiy if (hostinfo) 648ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 649c152df28SYaroslav Tykhiy else 650c152df28SYaroslav Tykhiy reply(220, "FTP server ready."); 651ea022d16SRodney W. Grimes for (;;) 652ea022d16SRodney W. Grimes (void) yyparse(); 653ea022d16SRodney W. Grimes /* NOTREACHED */ 654ea022d16SRodney W. Grimes } 655ea022d16SRodney W. Grimes 656ea022d16SRodney W. Grimes static void 657e4bc453cSWarner Losh lostconn(int signo) 658ea022d16SRodney W. Grimes { 659ea022d16SRodney W. Grimes 660618b0bbaSMark Murray if (ftpdebug) 661ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 662e02897faSPhilippe Charnier dologout(1); 663ea022d16SRodney W. Grimes } 664ea022d16SRodney W. Grimes 6654b82fc95SYaroslav Tykhiy static void 666e4bc453cSWarner Losh sigquit(int signo) 6674b82fc95SYaroslav Tykhiy { 6684b82fc95SYaroslav Tykhiy 6694b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo); 6704b82fc95SYaroslav Tykhiy dologout(1); 6714b82fc95SYaroslav Tykhiy } 6724b82fc95SYaroslav Tykhiy 673ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 674ea4e54b9SDavid Nugent /* 675ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 676ea4e54b9SDavid Nugent */ 677ea4e54b9SDavid Nugent 678ea4e54b9SDavid Nugent static void 679e4bc453cSWarner Losh inithosts(void) 680ea4e54b9SDavid Nugent { 68155b54aa7SYaroslav Tykhiy int insert; 682737d08f3SYaroslav Tykhiy size_t len; 683ea4e54b9SDavid Nugent FILE *fp; 684737d08f3SYaroslav Tykhiy char *cp, *mp, *line; 685737d08f3SYaroslav Tykhiy char *hostname; 68655b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg; 687ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 6884dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 689ea4e54b9SDavid Nugent 690ea4e54b9SDavid Nugent /* 691ea4e54b9SDavid Nugent * Fill in the default host information 692ea4e54b9SDavid Nugent */ 693737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 694618b0bbaSMark Murray fatalerror("Ran out of memory."); 695737d08f3SYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN) < 0) 696737d08f3SYaroslav Tykhiy hostname[0] = '\0'; 697737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0'; 698737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 699737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 700737d08f3SYaroslav Tykhiy hrp->hostname = hostname; 701f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 7024dd8b5abSYoshinobu Inoue 7034dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 7044dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 7054dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 706b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) 707f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 708ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 709ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 710ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 711ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 712ea4e54b9SDavid Nugent hrp->next = NULL; 713ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 714ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 715ec009cf0SYaroslav Tykhiy int addrsize, gothost; 7164dd8b5abSYoshinobu Inoue void *addr; 7174dd8b5abSYoshinobu Inoue struct hostent *hp; 7184dd8b5abSYoshinobu Inoue 719737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) { 7204dd8b5abSYoshinobu Inoue int i, hp_error; 721ea4e54b9SDavid Nugent 722737d08f3SYaroslav Tykhiy /* skip comments */ 723737d08f3SYaroslav Tykhiy if (line[0] == '#') 724ea4e54b9SDavid Nugent continue; 725737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 726737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 727737d08f3SYaroslav Tykhiy mp = NULL; 728737d08f3SYaroslav Tykhiy } else { 729737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 730737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 731737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 732737d08f3SYaroslav Tykhiy mp[len] = '\0'; 733737d08f3SYaroslav Tykhiy line = mp; 734ea4e54b9SDavid Nugent } 735ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 736737d08f3SYaroslav Tykhiy /* skip empty lines */ 737737d08f3SYaroslav Tykhiy if (cp == NULL) 738737d08f3SYaroslav Tykhiy goto nextline; 73955b54aa7SYaroslav Tykhiy vhost = cp; 74055b54aa7SYaroslav Tykhiy 74155b54aa7SYaroslav Tykhiy /* set defaults */ 74255b54aa7SYaroslav Tykhiy anonuser = "ftp"; 74355b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE; 74455b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME; 74555b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG; 74655b54aa7SYaroslav Tykhiy 74755b54aa7SYaroslav Tykhiy /* 74855b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info 74955b54aa7SYaroslav Tykhiy * for all the addresses associated with 75055b54aa7SYaroslav Tykhiy * the virtual host name. 75155b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special: 75255b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed 75355b54aa7SYaroslav Tykhiy * later, after we know its canonical form. 75455b54aa7SYaroslav Tykhiy */ 75555b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) 75655b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp))) 75755b54aa7SYaroslav Tykhiy switch (i) { 75855b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */ 75955b54aa7SYaroslav Tykhiy anonuser = cp; 76055b54aa7SYaroslav Tykhiy break; 76155b54aa7SYaroslav Tykhiy case 2: /* statistics file */ 76255b54aa7SYaroslav Tykhiy statfile = cp; 76355b54aa7SYaroslav Tykhiy break; 76455b54aa7SYaroslav Tykhiy case 3: /* welcome message */ 76555b54aa7SYaroslav Tykhiy welcome = cp; 76655b54aa7SYaroslav Tykhiy break; 76755b54aa7SYaroslav Tykhiy case 4: /* login message */ 76855b54aa7SYaroslav Tykhiy loginmsg = cp; 76955b54aa7SYaroslav Tykhiy break; 77055b54aa7SYaroslav Tykhiy default: /* programming error */ 77155b54aa7SYaroslav Tykhiy abort(); 77255b54aa7SYaroslav Tykhiy /* NOTREACHED */ 77355b54aa7SYaroslav Tykhiy } 7744dd8b5abSYoshinobu Inoue 7754dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 7764dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 7774dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 778b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0) 779737d08f3SYaroslav Tykhiy goto nextline; 7804dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 781b535a9bfSDavid Nugent ai = ai->ai_next) { 7824dd8b5abSYoshinobu Inoue 783b535a9bfSDavid Nugent gothost = 0; 784ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 785f38c6cadSYoshinobu Inoue struct addrinfo *hi; 786f38c6cadSYoshinobu Inoue 787f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 788f38c6cadSYoshinobu Inoue hi = hi->ai_next) 789f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 790f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 7914dd8b5abSYoshinobu Inoue ai->ai_addr, 792b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 793b535a9bfSDavid Nugent gothost++; 794b535a9bfSDavid Nugent break; 795b535a9bfSDavid Nugent } 796b535a9bfSDavid Nugent if (gothost) 797ea4e54b9SDavid Nugent break; 798ea4e54b9SDavid Nugent } 799ea4e54b9SDavid Nugent if (hrp == NULL) { 800ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 801737d08f3SYaroslav Tykhiy goto nextline; 802b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 80355b54aa7SYaroslav Tykhiy insert = 1; 804f2fe752dSYaroslav Tykhiy } else { 8051f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res) 806b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 807f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */ 808f2fe752dSYaroslav Tykhiy } 809f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 810f38c6cadSYoshinobu Inoue 811ea4e54b9SDavid Nugent /* 812ea4e54b9SDavid Nugent * determine hostname to use. 8134dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 814ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 815ea4e54b9SDavid Nugent */ 8164dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 817f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 8184dd8b5abSYoshinobu Inoue case AF_INET: 8194b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; 8204b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr); 8214dd8b5abSYoshinobu Inoue break; 8224dd8b5abSYoshinobu Inoue case AF_INET6: 8234b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; 8244b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr); 8254dd8b5abSYoshinobu Inoue break; 8264dd8b5abSYoshinobu Inoue default: 8274dd8b5abSYoshinobu Inoue /* should not reach here */ 828f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 829f2fe752dSYaroslav Tykhiy if (insert) 830f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/ 831f2fe752dSYaroslav Tykhiy else 832f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/ 833737d08f3SYaroslav Tykhiy goto nextline; 8344dd8b5abSYoshinobu Inoue /* NOTREACHED */ 8354dd8b5abSYoshinobu Inoue } 83657d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize, 837f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 8384dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 83955b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) { 840ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 84155b54aa7SYaroslav Tykhiy vhost = hp->h_name; 842ea4e54b9SDavid Nugent else { 843ea4e54b9SDavid Nugent i = 0; 844ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 84555b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0) 846ea4e54b9SDavid Nugent ++i; 847ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 84855b54aa7SYaroslav Tykhiy vhost = hp->h_name; 849ea4e54b9SDavid Nugent } 850ea4e54b9SDavid Nugent } 851ea4e54b9SDavid Nugent } 852b1d8d5cdSYaroslav Tykhiy if (hrp->hostname && 853b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) { 854b1d8d5cdSYaroslav Tykhiy free(hrp->hostname); 855b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL; 856b1d8d5cdSYaroslav Tykhiy } 857b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL && 858f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) { 859f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo); 860f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */ 861f2fe752dSYaroslav Tykhiy if (hp) 862f2fe752dSYaroslav Tykhiy freehostent(hp); 86355b54aa7SYaroslav Tykhiy goto nextline; 864f2fe752dSYaroslav Tykhiy } 86555b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser; 86655b54aa7SYaroslav Tykhiy hrp->statfile = statfile; 86755b54aa7SYaroslav Tykhiy hrp->welcome = welcome; 86855b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg; 86955b54aa7SYaroslav Tykhiy if (insert) { 87055b54aa7SYaroslav Tykhiy hrp->next = NULL; 87155b54aa7SYaroslav Tykhiy lhrp->next = hrp; 87255b54aa7SYaroslav Tykhiy lhrp = hrp; 87355b54aa7SYaroslav Tykhiy } 874233c0f66SYaroslav Tykhiy if (hp) 8754dd8b5abSYoshinobu Inoue freehostent(hp); 8764dd8b5abSYoshinobu Inoue } 877737d08f3SYaroslav Tykhiy nextline: 878737d08f3SYaroslav Tykhiy if (mp) 879737d08f3SYaroslav Tykhiy free(mp); 880ea4e54b9SDavid Nugent } 881ea4e54b9SDavid Nugent (void) fclose(fp); 882ea4e54b9SDavid Nugent } 883ea4e54b9SDavid Nugent } 884ea4e54b9SDavid Nugent 885ea4e54b9SDavid Nugent static void 886e4bc453cSWarner Losh selecthost(union sockunion *su) 887ea4e54b9SDavid Nugent { 888ea4e54b9SDavid Nugent struct ftphost *hrp; 8894dd8b5abSYoshinobu Inoue u_int16_t port; 8904dd8b5abSYoshinobu Inoue #ifdef INET6 8914dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 8924dd8b5abSYoshinobu Inoue #endif 893f38c6cadSYoshinobu Inoue struct addrinfo *hi; 8944dd8b5abSYoshinobu Inoue 8954dd8b5abSYoshinobu Inoue #ifdef INET6 8964dd8b5abSYoshinobu Inoue /* 8974dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 8984dd8b5abSYoshinobu Inoue * specified in rfc2373. 8994dd8b5abSYoshinobu Inoue */ 9004dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 9014dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 9024dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 9034dd8b5abSYoshinobu Inoue #endif 904ea4e54b9SDavid Nugent 905ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 9064dd8b5abSYoshinobu Inoue port = su->su_port; 9074dd8b5abSYoshinobu Inoue su->su_port = 0; 908ea4e54b9SDavid Nugent while (hrp != NULL) { 909b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 910f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 911ea4e54b9SDavid Nugent thishost = hrp; 912ea4e54b9SDavid Nugent break; 913ea4e54b9SDavid Nugent } 9144dd8b5abSYoshinobu Inoue #ifdef INET6 9154dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 916f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 9174dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 918f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 9194dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 9204dd8b5abSYoshinobu Inoue thishost = hrp; 9214dd8b5abSYoshinobu Inoue break; 9224dd8b5abSYoshinobu Inoue } 9234dd8b5abSYoshinobu Inoue #endif 924f38c6cadSYoshinobu Inoue } 925ea4e54b9SDavid Nugent hrp = hrp->next; 926ea4e54b9SDavid Nugent } 9274dd8b5abSYoshinobu Inoue su->su_port = port; 928ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 929ea4e54b9SDavid Nugent hostname = thishost->hostname; 930ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 931ea4e54b9SDavid Nugent } 932ea4e54b9SDavid Nugent #endif 933ea4e54b9SDavid Nugent 934ea022d16SRodney W. Grimes /* 935ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 936ea022d16SRodney W. Grimes */ 937ea022d16SRodney W. Grimes static char * 938e4bc453cSWarner Losh sgetsave(char *s) 939ea022d16SRodney W. Grimes { 940ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 941ea022d16SRodney W. Grimes 942ea022d16SRodney W. Grimes if (new == NULL) { 943ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 944ea022d16SRodney W. Grimes dologout(1); 945ea022d16SRodney W. Grimes /* NOTREACHED */ 946ea022d16SRodney W. Grimes } 947ea022d16SRodney W. Grimes (void) strcpy(new, s); 948ea022d16SRodney W. Grimes return (new); 949ea022d16SRodney W. Grimes } 950ea022d16SRodney W. Grimes 951ea022d16SRodney W. Grimes /* 952ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 953ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 954ea022d16SRodney W. Grimes * (e.g., globbing). 955ea022d16SRodney W. Grimes */ 956ea022d16SRodney W. Grimes static struct passwd * 957e4bc453cSWarner Losh sgetpwnam(char *name) 958ea022d16SRodney W. Grimes { 959ea022d16SRodney W. Grimes static struct passwd save; 960ea022d16SRodney W. Grimes struct passwd *p; 961ea022d16SRodney W. Grimes 962ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 963ea022d16SRodney W. Grimes return (p); 964ea022d16SRodney W. Grimes if (save.pw_name) { 965ea022d16SRodney W. Grimes free(save.pw_name); 966ea022d16SRodney W. Grimes free(save.pw_passwd); 967ea022d16SRodney W. Grimes free(save.pw_gecos); 968ea022d16SRodney W. Grimes free(save.pw_dir); 969ea022d16SRodney W. Grimes free(save.pw_shell); 970ea022d16SRodney W. Grimes } 971ea022d16SRodney W. Grimes save = *p; 972ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 973ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 974ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 975ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 976ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 977ea022d16SRodney W. Grimes return (&save); 978ea022d16SRodney W. Grimes } 979ea022d16SRodney W. Grimes 980ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 981ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 98290906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 983ea022d16SRodney W. Grimes 984ea022d16SRodney W. Grimes /* 985ea022d16SRodney W. Grimes * USER command. 986ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 987ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 988ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 989ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 990ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 991ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 992ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 993ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 994ea022d16SRodney W. Grimes */ 995ea022d16SRodney W. Grimes void 996e4bc453cSWarner Losh user(char *name) 997ea022d16SRodney W. Grimes { 998ea022d16SRodney W. Grimes char *cp, *shell; 999ea022d16SRodney W. Grimes 1000ea022d16SRodney W. Grimes if (logged_in) { 1001ea022d16SRodney W. Grimes if (guest) { 1002ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 1003ea022d16SRodney W. Grimes return; 1004a5a4544eSPaul Traina } else if (dochroot) { 1005a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 1006a5a4544eSPaul Traina return; 1007ea022d16SRodney W. Grimes } 1008ea022d16SRodney W. Grimes end_login(); 1009ea022d16SRodney W. Grimes } 1010ea022d16SRodney W. Grimes 1011ea022d16SRodney W. Grimes guest = 0; 1012ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 10138657b576SYaroslav Tykhiy if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL) || 10148657b576SYaroslav Tykhiy checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL)) 1015ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1016ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1017ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 1018ea4e54b9SDavid Nugent #else 1019ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 1020ea4e54b9SDavid Nugent #endif 1021ea022d16SRodney W. Grimes guest = 1; 1022ea022d16SRodney W. Grimes askpasswd = 1; 1023ea022d16SRodney W. Grimes reply(331, 10242c60c54cSPaul Traina "Guest login ok, send your email address as password."); 1025ea022d16SRodney W. Grimes } else 1026ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 1027ea022d16SRodney W. Grimes if (!askpasswd && logging) 1028ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1029ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 1030ea022d16SRodney W. Grimes return; 1031ea022d16SRodney W. Grimes } 10325a392aecSTorsten Blum if (anon_only != 0) { 10335a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 10345a392aecSTorsten Blum return; 10355a392aecSTorsten Blum } 10365a392aecSTorsten Blum 10379aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 1038ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 1039ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 1040ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 1041ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 1042ea022d16SRodney W. Grimes break; 1043ea022d16SRodney W. Grimes endusershell(); 1044ea022d16SRodney W. Grimes 10458657b576SYaroslav Tykhiy if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1, NULL)) { 1046ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 1047ea022d16SRodney W. Grimes if (logging) 1048ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1049ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 1050ea022d16SRodney W. Grimes remotehost, name); 1051ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 1052ea022d16SRodney W. Grimes return; 1053ea022d16SRodney W. Grimes } 1054ea022d16SRodney W. Grimes } 1055ea022d16SRodney W. Grimes if (logging) 1056ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 105747499ecdSAndrey A. Chernov 105847499ecdSAndrey A. Chernov pwok = 0; 1059fa1746c9SMark Murray #ifdef USE_PAM 1060fa1746c9SMark Murray /* XXX Kluge! The conversation mechanism needs to be fixed. */ 1061726040deSGuido van Rooij #endif 106247499ecdSAndrey A. Chernov if (opiechallenge(&opiedata, name, opieprompt) == 0) { 106347499ecdSAndrey A. Chernov pwok = (pw != NULL) && 106447499ecdSAndrey A. Chernov opieaccessfile(remotehost) && 106547499ecdSAndrey A. Chernov opiealways(pw->pw_dir); 106647499ecdSAndrey A. Chernov reply(331, "Response to %s %s for %s.", 106747499ecdSAndrey A. Chernov opieprompt, pwok ? "requested" : "required", name); 106847499ecdSAndrey A. Chernov } else { 106947499ecdSAndrey A. Chernov pwok = 1; 107047499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name); 107147499ecdSAndrey A. Chernov } 1072ea022d16SRodney W. Grimes askpasswd = 1; 1073ea022d16SRodney W. Grimes /* 1074ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 1075ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 1076ea022d16SRodney W. Grimes */ 1077ea022d16SRodney W. Grimes if (login_attempts) 1078ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 1079ea022d16SRodney W. Grimes } 1080ea022d16SRodney W. Grimes 1081ea022d16SRodney W. Grimes /* 10828657b576SYaroslav Tykhiy * Check if a user is in the file "fname", 10838657b576SYaroslav Tykhiy * return a pointer to a malloc'd string with the rest 10848657b576SYaroslav Tykhiy * of the matching line in "residue" if not NULL. 1085ea022d16SRodney W. Grimes */ 1086ea022d16SRodney W. Grimes static int 10878657b576SYaroslav Tykhiy checkuser(char *fname, char *name, int pwset, char **residue) 1088ea022d16SRodney W. Grimes { 1089ea022d16SRodney W. Grimes FILE *fd; 1090ea022d16SRodney W. Grimes int found = 0; 1091737d08f3SYaroslav Tykhiy size_t len; 1092737d08f3SYaroslav Tykhiy char *line, *mp, *p; 1093ea022d16SRodney W. Grimes 1094a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 1095737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) { 1096737d08f3SYaroslav Tykhiy /* skip comments */ 1097ea022d16SRodney W. Grimes if (line[0] == '#') 1098ea022d16SRodney W. Grimes continue; 1099737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') { 1100737d08f3SYaroslav Tykhiy line[len - 1] = '\0'; 1101737d08f3SYaroslav Tykhiy mp = NULL; 1102737d08f3SYaroslav Tykhiy } else { 1103737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL) 1104737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory."); 1105737d08f3SYaroslav Tykhiy memcpy(mp, line, len); 1106737d08f3SYaroslav Tykhiy mp[len] = '\0'; 1107737d08f3SYaroslav Tykhiy line = mp; 1108737d08f3SYaroslav Tykhiy } 1109737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */ 1110737d08f3SYaroslav Tykhiy p = strtok(line, " \t"); 1111737d08f3SYaroslav Tykhiy /* skip empty lines */ 1112737d08f3SYaroslav Tykhiy if (p == NULL) 1113737d08f3SYaroslav Tykhiy goto nextline; 111431fea7b8SDavid Nugent /* 111531fea7b8SDavid Nugent * if first chr is '@', check group membership 111631fea7b8SDavid Nugent */ 1117737d08f3SYaroslav Tykhiy if (p[0] == '@') { 111831fea7b8SDavid Nugent int i = 0; 111931fea7b8SDavid Nugent struct group *grp; 112031fea7b8SDavid Nugent 11218657b576SYaroslav Tykhiy if (p[1] == '\0') /* single @ matches anyone */ 11228657b576SYaroslav Tykhiy found = 1; 11238657b576SYaroslav Tykhiy else { 1124737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL) 1125737d08f3SYaroslav Tykhiy goto nextline; 11267edcb936SSteve Price /* 11277edcb936SSteve Price * Check user's default group 11287edcb936SSteve Price */ 11297edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 11307edcb936SSteve Price found = 1; 11317edcb936SSteve Price /* 11327edcb936SSteve Price * Check supplementary groups 11337edcb936SSteve Price */ 113431fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 113531fea7b8SDavid Nugent found = strcmp(name, 113631fea7b8SDavid Nugent grp->gr_mem[i++]) 113731fea7b8SDavid Nugent == 0; 1138ea022d16SRodney W. Grimes } 11398657b576SYaroslav Tykhiy } 114031fea7b8SDavid Nugent /* 114131fea7b8SDavid Nugent * Otherwise, just check for username match 114231fea7b8SDavid Nugent */ 114331fea7b8SDavid Nugent else 1144737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0; 11458657b576SYaroslav Tykhiy /* 11468657b576SYaroslav Tykhiy * Save the rest of line to "residue" if matched 11478657b576SYaroslav Tykhiy */ 11488657b576SYaroslav Tykhiy if (found && residue) { 11490ba71e24SYaroslav Tykhiy if ((p = strtok(NULL, "")) != NULL) 11500ba71e24SYaroslav Tykhiy p += strspn(p, " \t"); 11510ba71e24SYaroslav Tykhiy if (p && *p) { 11528657b576SYaroslav Tykhiy if ((*residue = strdup(p)) == NULL) 11538657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 11548657b576SYaroslav Tykhiy } else 11558657b576SYaroslav Tykhiy *residue = NULL; 11568657b576SYaroslav Tykhiy } 1157737d08f3SYaroslav Tykhiy nextline: 1158737d08f3SYaroslav Tykhiy if (mp) 1159737d08f3SYaroslav Tykhiy free(mp); 1160ea022d16SRodney W. Grimes } 1161ea022d16SRodney W. Grimes (void) fclose(fd); 1162ea022d16SRodney W. Grimes } 1163ea022d16SRodney W. Grimes return (found); 1164ea022d16SRodney W. Grimes } 1165ea022d16SRodney W. Grimes 1166ea022d16SRodney W. Grimes /* 1167ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1168ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1169ea022d16SRodney W. Grimes */ 1170ea022d16SRodney W. Grimes static void 1171e4bc453cSWarner Losh end_login(void) 1172ea022d16SRodney W. Grimes { 11735bc9d93dSMark Murray #ifdef USE_PAM 11745bc9d93dSMark Murray int e; 11755bc9d93dSMark Murray #endif 1176ea022d16SRodney W. Grimes 1177ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 11785d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) 117946948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 1180ea022d16SRodney W. Grimes pw = NULL; 1181b071c689SDavid Nugent #ifdef LOGIN_CAP 1182b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 1183d9e2c424SRobert Watson LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK| 1184d9e2c424SRobert Watson LOGIN_SETMAC); 1185b071c689SDavid Nugent #endif 11865bc9d93dSMark Murray #ifdef USE_PAM 11875bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) 11885bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 11895bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) 11905bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); 11915bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) 11925bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 11935bc9d93dSMark Murray pamh = NULL; 11945bc9d93dSMark Murray #endif 1195ea022d16SRodney W. Grimes logged_in = 0; 1196ea022d16SRodney W. Grimes guest = 0; 1197a5a4544eSPaul Traina dochroot = 0; 1198ea022d16SRodney W. Grimes } 1199ea022d16SRodney W. Grimes 12005bc9d93dSMark Murray #ifdef USE_PAM 12016c9134c0SMark Murray 12026c9134c0SMark Murray /* 12036c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 12046c9134c0SMark Murray * login.c 12056c9134c0SMark Murray */ 12066c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 12076c9134c0SMark Murray 12086c9134c0SMark Murray struct cred_t { 12096c9134c0SMark Murray const char *uname; /* user name */ 12106c9134c0SMark Murray const char *pass; /* password */ 12116c9134c0SMark Murray }; 12126c9134c0SMark Murray typedef struct cred_t cred_t; 12136c9134c0SMark Murray 12146c9134c0SMark Murray static int 12156c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 12166c9134c0SMark Murray struct pam_response **resp, void *appdata) 12176c9134c0SMark Murray { 12186c9134c0SMark Murray int i; 12196c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 122060769b19SDag-Erling Smørgrav struct pam_response *reply; 122160769b19SDag-Erling Smørgrav 122260769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply); 122360769b19SDag-Erling Smørgrav if (reply == NULL) 122460769b19SDag-Erling Smørgrav return PAM_BUF_ERR; 12256c9134c0SMark Murray 12266c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 12276c9134c0SMark Murray switch (msg[i]->msg_style) { 12286c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 12296c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12306c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 12316c9134c0SMark Murray /* PAM frees resp. */ 12326c9134c0SMark Murray break; 12336c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 12346c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12356c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 12366c9134c0SMark Murray /* PAM frees resp. */ 12376c9134c0SMark Murray break; 12386c9134c0SMark Murray case PAM_TEXT_INFO: 12396c9134c0SMark Murray case PAM_ERROR_MSG: 12406c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 12416c9134c0SMark Murray reply[i].resp = NULL; 12426c9134c0SMark Murray break; 12436c9134c0SMark Murray default: /* unknown message style */ 12446c9134c0SMark Murray free(reply); 12456c9134c0SMark Murray return PAM_CONV_ERR; 12466c9134c0SMark Murray } 12476c9134c0SMark Murray } 12486c9134c0SMark Murray 12496c9134c0SMark Murray *resp = reply; 12506c9134c0SMark Murray return PAM_SUCCESS; 12516c9134c0SMark Murray } 12526c9134c0SMark Murray 12536c9134c0SMark Murray /* 12546c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 12556c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 12566c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 12576c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 12586c9134c0SMark Murray * fall back to a different authentication mechanism. 12596c9134c0SMark Murray */ 12606c9134c0SMark Murray static int 12616c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 12626c9134c0SMark Murray { 12636c9134c0SMark Murray pam_handle_t *pamh = NULL; 12646c9134c0SMark Murray const char *tmpl_user; 12656c9134c0SMark Murray const void *item; 12666c9134c0SMark Murray int rval; 12676c9134c0SMark Murray int e; 12686c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 12696c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 12706c9134c0SMark Murray 12716c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 12726c9134c0SMark Murray if (e != PAM_SUCCESS) { 12736c9134c0SMark Murray syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 12746c9134c0SMark Murray return -1; 12756c9134c0SMark Murray } 12766c9134c0SMark Murray 1277ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1278ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1279ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1280ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1281ea413ab7SGuido van Rooij return -1; 1282ea413ab7SGuido van Rooij } 1283ea413ab7SGuido van Rooij 12846c9134c0SMark Murray e = pam_authenticate(pamh, 0); 12856c9134c0SMark Murray switch (e) { 12866c9134c0SMark Murray case PAM_SUCCESS: 12876c9134c0SMark Murray /* 12886c9134c0SMark Murray * With PAM we support the concept of a "template" 12896c9134c0SMark Murray * user. The user enters a login name which is 12906c9134c0SMark Murray * authenticated by PAM, usually via a remote service 12916c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 12926c9134c0SMark Murray * succeeds, a different but related "template" name 12936c9134c0SMark Murray * is used for setting the credentials, shell, and 12946c9134c0SMark Murray * home directory. The name the user enters need only 12956c9134c0SMark Murray * exist on the remote authentication server, but the 12966c9134c0SMark Murray * template name must be present in the local password 12976c9134c0SMark Murray * database. 12986c9134c0SMark Murray * 12996c9134c0SMark Murray * This is supported by two various mechanisms in the 13006c9134c0SMark Murray * individual modules. However, from the application's 13016c9134c0SMark Murray * point of view, the template user is always passed 13026c9134c0SMark Murray * back as a changed value of the PAM_USER item. 13036c9134c0SMark Murray */ 13046c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 13056c9134c0SMark Murray PAM_SUCCESS) { 13066c9134c0SMark Murray tmpl_user = (const char *) item; 13076c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 13086c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 13096c9134c0SMark Murray } else 13106c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 13116c9134c0SMark Murray pam_strerror(pamh, e)); 13126c9134c0SMark Murray rval = 0; 13136c9134c0SMark Murray break; 13146c9134c0SMark Murray 13156c9134c0SMark Murray case PAM_AUTH_ERR: 13166c9134c0SMark Murray case PAM_USER_UNKNOWN: 13176c9134c0SMark Murray case PAM_MAXTRIES: 13186c9134c0SMark Murray rval = 1; 13196c9134c0SMark Murray break; 13206c9134c0SMark Murray 13216c9134c0SMark Murray default: 13225bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); 13236c9134c0SMark Murray rval = -1; 13246c9134c0SMark Murray break; 13256c9134c0SMark Murray } 13266c9134c0SMark Murray 13275bc9d93dSMark Murray if (rval == 0) { 13285bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0); 13295bc9d93dSMark Murray if (e == PAM_NEW_AUTHTOK_REQD) { 13305bc9d93dSMark Murray e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); 13315bc9d93dSMark Murray if (e != PAM_SUCCESS) { 13325bc9d93dSMark Murray syslog(LOG_ERR, "pam_chauthtok: %s", pam_strerror(pamh, e)); 13335bc9d93dSMark Murray rval = 1; 13345bc9d93dSMark Murray } 13355bc9d93dSMark Murray } else if (e != PAM_SUCCESS) { 13365bc9d93dSMark Murray rval = 1; 13375bc9d93dSMark Murray } 13385bc9d93dSMark Murray } 13395bc9d93dSMark Murray 13405bc9d93dSMark Murray if (rval != 0) { 13416c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 13426c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 13435bc9d93dSMark Murray } 13445bc9d93dSMark Murray pamh = NULL; 13456c9134c0SMark Murray } 13466c9134c0SMark Murray return rval; 13476c9134c0SMark Murray } 13486c9134c0SMark Murray 13495bc9d93dSMark Murray #endif /* USE_PAM */ 13506c9134c0SMark Murray 1351ea022d16SRodney W. Grimes void 1352e4bc453cSWarner Losh pass(char *passwd) 1353ea022d16SRodney W. Grimes { 1354a5a4544eSPaul Traina int rval; 1355ea022d16SRodney W. Grimes FILE *fd; 1356b071c689SDavid Nugent #ifdef LOGIN_CAP 1357b071c689SDavid Nugent login_cap_t *lc = NULL; 1358b071c689SDavid Nugent #endif 13595bc9d93dSMark Murray #ifdef USE_PAM 13605bc9d93dSMark Murray int e; 13615bc9d93dSMark Murray #endif 1362ce9287fcSYaroslav Tykhiy char *chrootdir; 1363341e476eSYaroslav Tykhiy char *residue = NULL; 136447499ecdSAndrey A. Chernov char *xpasswd; 1365ea022d16SRodney W. Grimes 1366ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1367ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1368ea022d16SRodney W. Grimes return; 1369ea022d16SRodney W. Grimes } 1370ea022d16SRodney W. Grimes askpasswd = 0; 1371ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1372a5a4544eSPaul Traina if (pw == NULL) { 1373a5a4544eSPaul Traina rval = 1; /* failure below */ 1374a5a4544eSPaul Traina goto skip; 1375a5a4544eSPaul Traina } 13765bc9d93dSMark Murray #ifdef USE_PAM 13776c9134c0SMark Murray rval = auth_pam(&pw, passwd); 1378f650a124SAndrey A. Chernov if (rval >= 0) { 1379f650a124SAndrey A. Chernov opieunlock(); 1380a5a4544eSPaul Traina goto skip; 1381f650a124SAndrey A. Chernov } 1382f650a124SAndrey A. Chernov #endif 138347499ecdSAndrey A. Chernov if (opieverify(&opiedata, passwd) == 0) 138447499ecdSAndrey A. Chernov xpasswd = pw->pw_passwd; 1385f650a124SAndrey A. Chernov else if (pwok) { 138647499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd); 1387f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') 1388f650a124SAndrey A. Chernov xpasswd = ":"; 1389f650a124SAndrey A. Chernov } else { 139047499ecdSAndrey A. Chernov rval = 1; 139147499ecdSAndrey A. Chernov goto skip; 139247499ecdSAndrey A. Chernov } 139347499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd); 1394f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire) 1395a5a4544eSPaul Traina rval = 1; /* failure */ 1396a5a4544eSPaul Traina skip: 1397a5a4544eSPaul Traina /* 1398a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 13996c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1400a5a4544eSPaul Traina * succeeded. 1401a5a4544eSPaul Traina */ 1402a5a4544eSPaul Traina if (rval) { 1403ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1404ea022d16SRodney W. Grimes if (logging) 1405ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1406ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1407ea022d16SRodney W. Grimes remotehost, curname); 1408ea022d16SRodney W. Grimes pw = NULL; 1409ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1410ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1411ea022d16SRodney W. Grimes "repeated login failures from %s", 1412ea022d16SRodney W. Grimes remotehost); 1413ea022d16SRodney W. Grimes exit(0); 1414ea022d16SRodney W. Grimes } 1415ea022d16SRodney W. Grimes return; 1416ea022d16SRodney W. Grimes } 1417ea022d16SRodney W. Grimes } 1418ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1419ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 1420ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1421ea022d16SRodney W. Grimes return; 1422ea022d16SRodney W. Grimes } 1423b071c689SDavid Nugent /* May be overridden by login.conf */ 1424b071c689SDavid Nugent (void) umask(defumask); 1425b071c689SDavid Nugent #ifdef LOGIN_CAP 14265d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 1427b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 1428b071c689SDavid Nugent 14294dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 14304dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 1431b0f06defSHajimu UMEMOTO NI_NUMERICHOST); 1432b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1433b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1434b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1435b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1436b071c689SDavid Nugent pw->pw_name); 1437b071c689SDavid Nugent reply(530, "Permission denied.\n"); 1438b071c689SDavid Nugent pw = NULL; 1439b071c689SDavid Nugent return; 1440b071c689SDavid Nugent } 1441b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1442b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1443b071c689SDavid Nugent pw = NULL; 1444b071c689SDavid Nugent return; 1445b071c689SDavid Nugent } 1446b071c689SDavid Nugent } 1447b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1448e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1449d9e2c424SRobert Watson LOGIN_SETRESOURCES|LOGIN_SETUMASK|LOGIN_SETMAC); 1450b071c689SDavid Nugent #else 1451e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1452ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1453b071c689SDavid Nugent #endif 1454ea022d16SRodney W. Grimes 14555bc9d93dSMark Murray #ifdef USE_PAM 14565bc9d93dSMark Murray if (pamh) { 14575bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { 14585bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); 14595bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { 14605bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); 14615bc9d93dSMark Murray } 14625bc9d93dSMark Murray } 14635bc9d93dSMark Murray #endif 14645bc9d93dSMark Murray 1465ea022d16SRodney W. Grimes /* open wtmp before chroot */ 14665d7e0128SYaroslav Tykhiy if (dowtmp) 14675d7e0128SYaroslav Tykhiy ftpd_logwtmp(ttyline, pw->pw_name, 14685d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr); 1469ea022d16SRodney W. Grimes logged_in = 1; 1470ea022d16SRodney W. Grimes 1471a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1472ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1473ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1474ea4e54b9SDavid Nugent #else 14753eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1476ea4e54b9SDavid Nugent #endif 14773eb568f2SGuido van Rooij stats = 0; 14783eb568f2SGuido van Rooij 1479b071c689SDavid Nugent dochroot = 1480341e476eSYaroslav Tykhiy checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue) 1481b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 14828657b576SYaroslav Tykhiy || login_getcapbool(lc, "ftp-chroot", 0) 1483b071c689SDavid Nugent #endif 14848657b576SYaroslav Tykhiy ; 1485ce9287fcSYaroslav Tykhiy chrootdir = NULL; 1486ea022d16SRodney W. Grimes /* 1487ce9287fcSYaroslav Tykhiy * For a chrooted local user, 1488ce9287fcSYaroslav Tykhiy * a) see whether ftpchroot(5) specifies a chroot directory, 1489ce9287fcSYaroslav Tykhiy * b) extract the directory pathname from the line, 1490ce9287fcSYaroslav Tykhiy * c) expand it to the absolute pathname if necessary. 1491ea022d16SRodney W. Grimes */ 1492ce9287fcSYaroslav Tykhiy if (dochroot && residue && 1493341e476eSYaroslav Tykhiy (chrootdir = strtok(residue, " \t")) != NULL && 1494341e476eSYaroslav Tykhiy chrootdir[0] != '/') { 1495341e476eSYaroslav Tykhiy asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir); 1496341e476eSYaroslav Tykhiy if (chrootdir == NULL) 14978657b576SYaroslav Tykhiy fatalerror("Ran out of memory."); 1498ce9287fcSYaroslav Tykhiy 14998657b576SYaroslav Tykhiy } 1500ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1501ce9287fcSYaroslav Tykhiy /* 1502ce9287fcSYaroslav Tykhiy * If no chroot directory set yet, use the login directory. 1503ce9287fcSYaroslav Tykhiy * Copy it so it can be modified while pw->pw_dir stays intact. 1504ce9287fcSYaroslav Tykhiy */ 1505ce9287fcSYaroslav Tykhiy if (chrootdir == NULL && 1506ce9287fcSYaroslav Tykhiy (chrootdir = strdup(pw->pw_dir)) == NULL) 1507ce9287fcSYaroslav Tykhiy fatalerror("Ran out of memory."); 1508ce9287fcSYaroslav Tykhiy /* 1509ce9287fcSYaroslav Tykhiy * Check for the "/chroot/./home" syntax, 1510ce9287fcSYaroslav Tykhiy * separate the chroot and home directory pathnames. 1511ce9287fcSYaroslav Tykhiy */ 1512ce9287fcSYaroslav Tykhiy if ((homedir = strstr(chrootdir, "/./")) != NULL) { 1513ce9287fcSYaroslav Tykhiy *(homedir++) = '\0'; /* wipe '/' */ 1514ce9287fcSYaroslav Tykhiy homedir++; /* skip '.' */ 1515ce9287fcSYaroslav Tykhiy /* so chrootdir can be freed later */ 1516ce9287fcSYaroslav Tykhiy if ((homedir = strdup(homedir)) == NULL) 1517ce9287fcSYaroslav Tykhiy fatalerror("Ran out of memory."); 1518ce9287fcSYaroslav Tykhiy } else { 1519ce9287fcSYaroslav Tykhiy /* 1520ce9287fcSYaroslav Tykhiy * We MUST do a chdir() after the chroot. Otherwise 1521ce9287fcSYaroslav Tykhiy * the old current directory will be accessible as "." 1522ce9287fcSYaroslav Tykhiy * outside the new root! 1523ce9287fcSYaroslav Tykhiy */ 1524ce9287fcSYaroslav Tykhiy homedir = "/"; 1525ce9287fcSYaroslav Tykhiy } 1526ce9287fcSYaroslav Tykhiy /* 1527ce9287fcSYaroslav Tykhiy * Finally, do chroot() 1528ce9287fcSYaroslav Tykhiy */ 1529ce9287fcSYaroslav Tykhiy if (chroot(chrootdir) < 0) { 1530a5a4544eSPaul Traina reply(550, "Can't change root."); 1531a5a4544eSPaul Traina goto bad; 1532a5a4544eSPaul Traina } 1533ce9287fcSYaroslav Tykhiy } else /* real user w/o chroot */ 1534ce9287fcSYaroslav Tykhiy homedir = pw->pw_dir; 1535ce9287fcSYaroslav Tykhiy /* 1536ce9287fcSYaroslav Tykhiy * Set euid *before* doing chdir() so 1537ce9287fcSYaroslav Tykhiy * a) the user won't be carried to a directory that he couldn't reach 1538ce9287fcSYaroslav Tykhiy * on his own due to no permission to upper path components, 1539ce9287fcSYaroslav Tykhiy * b) NFS mounted homedirs w/restrictive permissions will be accessible 1540ce9287fcSYaroslav Tykhiy * (uid 0 has no root power over NFS if not mapped explicitly.) 1541ce9287fcSYaroslav Tykhiy */ 1542ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1543ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1544ea022d16SRodney W. Grimes goto bad; 1545ea022d16SRodney W. Grimes } 1546ce9287fcSYaroslav Tykhiy if (chdir(homedir) < 0) { 1547ce9287fcSYaroslav Tykhiy if (guest || dochroot) { 1548ce9287fcSYaroslav Tykhiy reply(550, "Can't change to base directory."); 1549ce9287fcSYaroslav Tykhiy goto bad; 1550ce9287fcSYaroslav Tykhiy } else { 1551ce9287fcSYaroslav Tykhiy if (chdir("/") < 0) { 1552ce9287fcSYaroslav Tykhiy reply(550, "Root is inaccessible."); 1553ce9287fcSYaroslav Tykhiy goto bad; 1554ce9287fcSYaroslav Tykhiy } 1555ce9287fcSYaroslav Tykhiy lreply(230, "No directory! Logging in with home=/"); 1556ce9287fcSYaroslav Tykhiy } 1557ce9287fcSYaroslav Tykhiy } 155882c76939SDavid Greenman 155982c76939SDavid Greenman /* 1560ea022d16SRodney W. Grimes * Display a login message, if it exists. 1561ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1562ea022d16SRodney W. Grimes */ 1563ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1564ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1565ea4e54b9SDavid Nugent #else 1566ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1567ea4e54b9SDavid Nugent #endif 1568ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1569ea022d16SRodney W. Grimes 1570ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1571ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1572ea022d16SRodney W. Grimes *cp = '\0'; 1573ea022d16SRodney W. Grimes lreply(230, "%s", line); 1574ea022d16SRodney W. Grimes } 1575ea022d16SRodney W. Grimes (void) fflush(stdout); 1576ea022d16SRodney W. Grimes (void) fclose(fd); 1577ea022d16SRodney W. Grimes } 1578ea022d16SRodney W. Grimes if (guest) { 15793eb568f2SGuido van Rooij if (ident != NULL) 15803eb568f2SGuido van Rooij free(ident); 158161f891a6SPaul Traina ident = strdup(passwd); 158261f891a6SPaul Traina if (ident == NULL) 1583618b0bbaSMark Murray fatalerror("Ran out of memory."); 158461f891a6SPaul Traina 1585ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1586ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1587ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1588ea4e54b9SDavid Nugent if (thishost != firsthost) 1589ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1590b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname, 1591b3a0a7cdSMike Heffner passwd); 1592ea4e54b9SDavid Nugent else 1593ea4e54b9SDavid Nugent #endif 1594ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1595b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd); 1596b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1597ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1598ea022d16SRodney W. Grimes if (logging) 1599ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1600ea022d16SRodney W. Grimes remotehost, passwd); 1601ea022d16SRodney W. Grimes } else { 16023401a71fSDaniel O'Callaghan if (dochroot) 160311342ab1SYaroslav Tykhiy reply(230, "User %s logged in, " 160411342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name); 16053401a71fSDaniel O'Callaghan else 1606ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 16073401a71fSDaniel O'Callaghan 1608ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1609ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 16107a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name); 1611b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1612ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1613ea022d16SRodney W. Grimes if (logging) 1614ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1615ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1616ea022d16SRodney W. Grimes } 1617b071c689SDavid Nugent #ifdef LOGIN_CAP 1618b071c689SDavid Nugent login_close(lc); 1619b071c689SDavid Nugent #endif 1620ce9287fcSYaroslav Tykhiy if (chrootdir) 1621ce9287fcSYaroslav Tykhiy free(chrootdir); 1622ce9287fcSYaroslav Tykhiy if (residue) 1623ce9287fcSYaroslav Tykhiy free(residue); 1624ea022d16SRodney W. Grimes return; 1625ea022d16SRodney W. Grimes bad: 1626ea022d16SRodney W. Grimes /* Forget all about it... */ 1627b071c689SDavid Nugent #ifdef LOGIN_CAP 1628b071c689SDavid Nugent login_close(lc); 1629b071c689SDavid Nugent #endif 1630ce9287fcSYaroslav Tykhiy if (chrootdir) 1631ce9287fcSYaroslav Tykhiy free(chrootdir); 1632ce9287fcSYaroslav Tykhiy if (residue) 1633ce9287fcSYaroslav Tykhiy free(residue); 1634ea022d16SRodney W. Grimes end_login(); 1635ea022d16SRodney W. Grimes } 1636ea022d16SRodney W. Grimes 1637ea022d16SRodney W. Grimes void 1638e4bc453cSWarner Losh retrieve(char *cmd, char *name) 1639ea022d16SRodney W. Grimes { 1640ea022d16SRodney W. Grimes FILE *fin, *dout; 1641ea022d16SRodney W. Grimes struct stat st; 1642e4bc453cSWarner Losh int (*closefunc)(FILE *); 1643158a00b2SJohn Birrell time_t start; 1644ea022d16SRodney W. Grimes 1645ea022d16SRodney W. Grimes if (cmd == 0) { 1646ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1647ea022d16SRodney W. Grimes st.st_size = 0; 1648ea022d16SRodney W. Grimes } else { 1649ea022d16SRodney W. Grimes char line[BUFSIZ]; 1650ea022d16SRodney W. Grimes 1651e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1652ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1653ea022d16SRodney W. Grimes st.st_size = -1; 1654ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1655ea022d16SRodney W. Grimes } 1656ea022d16SRodney W. Grimes if (fin == NULL) { 1657ea022d16SRodney W. Grimes if (errno != 0) { 1658ea022d16SRodney W. Grimes perror_reply(550, name); 1659ea022d16SRodney W. Grimes if (cmd == 0) { 1660ea022d16SRodney W. Grimes LOGCMD("get", name); 1661ea022d16SRodney W. Grimes } 1662ea022d16SRodney W. Grimes } 1663ea022d16SRodney W. Grimes return; 1664ea022d16SRodney W. Grimes } 1665ea022d16SRodney W. Grimes byte_count = -1; 1666ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1667ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1668ea022d16SRodney W. Grimes goto done; 1669ea022d16SRodney W. Grimes } 1670ea022d16SRodney W. Grimes if (restart_point) { 1671ea022d16SRodney W. Grimes if (type == TYPE_A) { 1672ea022d16SRodney W. Grimes off_t i, n; 1673ea022d16SRodney W. Grimes int c; 1674ea022d16SRodney W. Grimes 1675ea022d16SRodney W. Grimes n = restart_point; 1676ea022d16SRodney W. Grimes i = 0; 1677ea022d16SRodney W. Grimes while (i++ < n) { 1678ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1679ea022d16SRodney W. Grimes perror_reply(550, name); 1680ea022d16SRodney W. Grimes goto done; 1681ea022d16SRodney W. Grimes } 1682ea022d16SRodney W. Grimes if (c == '\n') 1683ea022d16SRodney W. Grimes i++; 1684ea022d16SRodney W. Grimes } 1685ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1686ea022d16SRodney W. Grimes perror_reply(550, name); 1687ea022d16SRodney W. Grimes goto done; 1688ea022d16SRodney W. Grimes } 1689ea022d16SRodney W. Grimes } 1690ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1691ea022d16SRodney W. Grimes if (dout == NULL) 1692ea022d16SRodney W. Grimes goto done; 16933eb568f2SGuido van Rooij time(&start); 16949fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 16959fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 16963eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 16973eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1698ea022d16SRodney W. Grimes (void) fclose(dout); 1699ea022d16SRodney W. Grimes data = -1; 1700ea022d16SRodney W. Grimes pdata = -1; 1701ea022d16SRodney W. Grimes done: 1702ea022d16SRodney W. Grimes if (cmd == 0) 1703ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1704ea022d16SRodney W. Grimes (*closefunc)(fin); 1705ea022d16SRodney W. Grimes } 1706ea022d16SRodney W. Grimes 1707ea022d16SRodney W. Grimes void 1708e4bc453cSWarner Losh store(char *name, char *mode, int unique) 1709ea022d16SRodney W. Grimes { 1710a117c345SYaroslav Tykhiy int fd; 1711ea022d16SRodney W. Grimes FILE *fout, *din; 1712e4bc453cSWarner Losh int (*closefunc)(FILE *); 1713ea022d16SRodney W. Grimes 1714a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */ 1715a117c345SYaroslav Tykhiy if (unique) { 1716a117c345SYaroslav Tykhiy /* Programming error */ 1717a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE"); 1718a117c345SYaroslav Tykhiy unique = 0; 1719a117c345SYaroslav Tykhiy } 1720a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1721a117c345SYaroslav Tykhiy reply(550, "Appending to existing file denied"); 1722a117c345SYaroslav Tykhiy goto err; 1723a117c345SYaroslav Tykhiy } 1724a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */ 1725a117c345SYaroslav Tykhiy } 1726a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */ 1727a117c345SYaroslav Tykhiy restart_point = 0; 1728a117c345SYaroslav Tykhiy if (guest && noguestmod) { 1729a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */ 1730a117c345SYaroslav Tykhiy reply(550, "Modifying existing file denied"); 1731a117c345SYaroslav Tykhiy goto err; 1732a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */ 1733a117c345SYaroslav Tykhiy unique = 1; 1734ea022d16SRodney W. Grimes } 1735ea022d16SRodney W. Grimes 1736ea022d16SRodney W. Grimes if (restart_point) 1737a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */ 1738a117c345SYaroslav Tykhiy if (unique) { 1739a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0) 1740a117c345SYaroslav Tykhiy goto err; 1741a117c345SYaroslav Tykhiy fout = fdopen(fd, mode); 1742a117c345SYaroslav Tykhiy } else 1743ea022d16SRodney W. Grimes fout = fopen(name, mode); 1744ea022d16SRodney W. Grimes closefunc = fclose; 1745ea022d16SRodney W. Grimes if (fout == NULL) { 1746ea022d16SRodney W. Grimes perror_reply(553, name); 1747a117c345SYaroslav Tykhiy goto err; 1748ea022d16SRodney W. Grimes } 1749ea022d16SRodney W. Grimes byte_count = -1; 1750ea022d16SRodney W. Grimes if (restart_point) { 1751ea022d16SRodney W. Grimes if (type == TYPE_A) { 1752ea022d16SRodney W. Grimes off_t i, n; 1753ea022d16SRodney W. Grimes int c; 1754ea022d16SRodney W. Grimes 1755ea022d16SRodney W. Grimes n = restart_point; 1756ea022d16SRodney W. Grimes i = 0; 1757ea022d16SRodney W. Grimes while (i++ < n) { 1758ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1759ea022d16SRodney W. Grimes perror_reply(550, name); 1760ea022d16SRodney W. Grimes goto done; 1761ea022d16SRodney W. Grimes } 1762ea022d16SRodney W. Grimes if (c == '\n') 1763ea022d16SRodney W. Grimes i++; 1764ea022d16SRodney W. Grimes } 1765ea022d16SRodney W. Grimes /* 1766ea022d16SRodney W. Grimes * We must do this seek to "current" position 1767ea022d16SRodney W. Grimes * because we are changing from reading to 1768ea022d16SRodney W. Grimes * writing. 1769ea022d16SRodney W. Grimes */ 1770e4a71114SAndrey A. Chernov if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) { 1771ea022d16SRodney W. Grimes perror_reply(550, name); 1772ea022d16SRodney W. Grimes goto done; 1773ea022d16SRodney W. Grimes } 1774ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1775ea022d16SRodney W. Grimes perror_reply(550, name); 1776ea022d16SRodney W. Grimes goto done; 1777ea022d16SRodney W. Grimes } 1778ea022d16SRodney W. Grimes } 1779ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1780ea022d16SRodney W. Grimes if (din == NULL) 1781ea022d16SRodney W. Grimes goto done; 1782ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1783ea022d16SRodney W. Grimes if (unique) 1784ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1785ea022d16SRodney W. Grimes name); 1786ea022d16SRodney W. Grimes else 1787ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1788ea022d16SRodney W. Grimes } 1789ea022d16SRodney W. Grimes (void) fclose(din); 1790ea022d16SRodney W. Grimes data = -1; 1791ea022d16SRodney W. Grimes pdata = -1; 1792ea022d16SRodney W. Grimes done: 17937c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); 1794ea022d16SRodney W. Grimes (*closefunc)(fout); 1795a117c345SYaroslav Tykhiy return; 1796a117c345SYaroslav Tykhiy err: 1797a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name); 1798a117c345SYaroslav Tykhiy return; 1799ea022d16SRodney W. Grimes } 1800ea022d16SRodney W. Grimes 1801ea022d16SRodney W. Grimes static FILE * 1802e4bc453cSWarner Losh getdatasock(char *mode) 1803ea022d16SRodney W. Grimes { 1804ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1805ea022d16SRodney W. Grimes 1806ea022d16SRodney W. Grimes if (data >= 0) 1807ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1808ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 18094dd8b5abSYoshinobu Inoue 18104dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1811ea022d16SRodney W. Grimes if (s < 0) 1812ea022d16SRodney W. Grimes goto bad; 181357d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 1814406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); 1815ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 18164dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 181763591ba5SYaroslav Tykhiy data_source.su_port = htons(dataport); 1818ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1819ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 18204dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1821ea022d16SRodney W. Grimes break; 1822ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1823ea022d16SRodney W. Grimes goto bad; 1824ea022d16SRodney W. Grimes sleep(tries); 1825ea022d16SRodney W. Grimes } 1826ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1827ea022d16SRodney W. Grimes #ifdef IP_TOS 18284dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 18294dd8b5abSYoshinobu Inoue { 1830ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 183157d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) 1832406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); 18334dd8b5abSYoshinobu Inoue } 1834ea022d16SRodney W. Grimes #endif 18359fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 18369fc5823aSGarrett Wollman /* 18379fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 18389fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 18399fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 18409fc5823aSGarrett Wollman * in heavy-load situations. 18419fc5823aSGarrett Wollman */ 18429fc5823aSGarrett Wollman on = 1; 184357d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) 1844406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); 18459fc5823aSGarrett Wollman #endif 18469fc5823aSGarrett Wollman #ifdef SO_SNDBUF 18479fc5823aSGarrett Wollman on = 65536; 184857d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &on, sizeof on) < 0) 1849406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_SNDBUF): %m"); 18509fc5823aSGarrett Wollman #endif 18519fc5823aSGarrett Wollman 1852ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1853ea022d16SRodney W. Grimes bad: 1854ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1855ea022d16SRodney W. Grimes t = errno; 1856ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1857ea022d16SRodney W. Grimes (void) close(s); 1858ea022d16SRodney W. Grimes errno = t; 1859ea022d16SRodney W. Grimes return (NULL); 1860ea022d16SRodney W. Grimes } 1861ea022d16SRodney W. Grimes 1862ea022d16SRodney W. Grimes static FILE * 1863e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode) 1864ea022d16SRodney W. Grimes { 1865ea022d16SRodney W. Grimes char sizebuf[32]; 1866ea022d16SRodney W. Grimes FILE *file; 1867e5094456SCrist J. Clark int retry = 0, tos, conerrno; 1868ea022d16SRodney W. Grimes 1869ea022d16SRodney W. Grimes file_size = size; 1870ea022d16SRodney W. Grimes byte_count = 0; 1871ea022d16SRodney W. Grimes if (size != (off_t) -1) 1872e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1873ea022d16SRodney W. Grimes else 1874e760ef2cSWarner Losh *sizebuf = '\0'; 1875ea022d16SRodney W. Grimes if (pdata >= 0) { 18764dd8b5abSYoshinobu Inoue union sockunion from; 18774cd48bacSYaroslav Tykhiy int flags; 18784dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1879d6ed3c37SGuido van Rooij struct timeval timeout; 1880d6ed3c37SGuido van Rooij fd_set set; 1881ea022d16SRodney W. Grimes 1882d6ed3c37SGuido van Rooij FD_ZERO(&set); 1883d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1884d6ed3c37SGuido van Rooij 1885d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1886d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1887d6ed3c37SGuido van Rooij 18884cd48bacSYaroslav Tykhiy /* 18894cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode, 18904cd48bacSYaroslav Tykhiy * accept() will block after a successful select() 18914cd48bacSYaroslav Tykhiy * if the selected connection dies in between. 18924cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here. 18934cd48bacSYaroslav Tykhiy */ 18944cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 18954cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) 18964cd48bacSYaroslav Tykhiy goto pdata_err; 18974cd48bacSYaroslav Tykhiy if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) <= 0 || 18984cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) 18994cd48bacSYaroslav Tykhiy goto pdata_err; 1900ea022d16SRodney W. Grimes (void) close(pdata); 1901ea022d16SRodney W. Grimes pdata = s; 19024cd48bacSYaroslav Tykhiy /* 1903f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag 1904f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it. 19054cd48bacSYaroslav Tykhiy */ 19064cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || 19074cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) 19084cd48bacSYaroslav Tykhiy goto pdata_err; 1909ea022d16SRodney W. Grimes #ifdef IP_TOS 19104dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 19114dd8b5abSYoshinobu Inoue { 1912a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 191357d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 1914406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); 19154dd8b5abSYoshinobu Inoue } 1916ea022d16SRodney W. Grimes #endif 1917ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1918ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1919ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 19204cd48bacSYaroslav Tykhiy pdata_err: 19214cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection."); 19224cd48bacSYaroslav Tykhiy (void) close(pdata); 19234cd48bacSYaroslav Tykhiy pdata = -1; 19244cd48bacSYaroslav Tykhiy return (NULL); 1925ea022d16SRodney W. Grimes } 1926ea022d16SRodney W. Grimes if (data >= 0) { 1927ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1928ea022d16SRodney W. Grimes name, sizebuf); 1929ea022d16SRodney W. Grimes usedefault = 1; 1930ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1931ea022d16SRodney W. Grimes } 1932ea022d16SRodney W. Grimes if (usedefault) 1933ea022d16SRodney W. Grimes data_dest = his_addr; 1934ea022d16SRodney W. Grimes usedefault = 1; 1935e5094456SCrist J. Clark do { 1936ea022d16SRodney W. Grimes file = getdatasock(mode); 1937ea022d16SRodney W. Grimes if (file == NULL) { 19384dd8b5abSYoshinobu Inoue char hostbuf[BUFSIZ], portbuf[BUFSIZ]; 19394dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&data_source, 19404dd8b5abSYoshinobu Inoue data_source.su_len, hostbuf, sizeof(hostbuf) - 1, 19414dd8b5abSYoshinobu Inoue portbuf, sizeof(portbuf), 1942b0f06defSHajimu UMEMOTO NI_NUMERICHOST|NI_NUMERICSERV); 19434dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 19444dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1945ea022d16SRodney W. Grimes return (NULL); 1946ea022d16SRodney W. Grimes } 1947ea022d16SRodney W. Grimes data = fileno(file); 1948e5094456SCrist J. Clark conerrno = 0; 1949e5094456SCrist J. Clark if (connect(data, (struct sockaddr *)&data_dest, 1950e5094456SCrist J. Clark data_dest.su_len) == 0) 1951e5094456SCrist J. Clark break; 1952e5094456SCrist J. Clark conerrno = errno; 1953ea022d16SRodney W. Grimes (void) fclose(file); 1954ea022d16SRodney W. Grimes data = -1; 1955e5094456SCrist J. Clark if (conerrno == EADDRINUSE) { 1956e5094456SCrist J. Clark sleep((unsigned) swaitint); 1957e5094456SCrist J. Clark retry += swaitint; 1958e5094456SCrist J. Clark } else { 1959e5094456SCrist J. Clark break; 1960e5094456SCrist J. Clark } 1961e5094456SCrist J. Clark } while (retry <= swaitmax); 1962e5094456SCrist J. Clark if (conerrno != 0) { 1963e5094456SCrist J. Clark perror_reply(425, "Can't build data connection"); 1964ea022d16SRodney W. Grimes return (NULL); 1965ea022d16SRodney W. Grimes } 1966ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1967ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1968ea022d16SRodney W. Grimes return (file); 1969ea022d16SRodney W. Grimes } 1970ea022d16SRodney W. Grimes 1971ea022d16SRodney W. Grimes /* 1972ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 19739fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1974ea022d16SRodney W. Grimes * 1975ea022d16SRodney W. Grimes * NB: Form isn't handled. 1976ea022d16SRodney W. Grimes */ 19774b82fc95SYaroslav Tykhiy static int 1978e4bc453cSWarner Losh send_data(FILE *instr, FILE *outstr, off_t blksize, off_t filesize, int isreg) 1979ea022d16SRodney W. Grimes { 1980f6f0c4b9SDan Moschuk int c, filefd, netfd; 1981f6f0c4b9SDan Moschuk char *buf; 1982f6f0c4b9SDan Moschuk off_t cnt; 1983ea022d16SRodney W. Grimes 1984ea022d16SRodney W. Grimes transflag++; 1985ea022d16SRodney W. Grimes switch (type) { 1986ea022d16SRodney W. Grimes 1987ea022d16SRodney W. Grimes case TYPE_A: 1988ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 19894b82fc95SYaroslav Tykhiy if (recvurg) 19904b82fc95SYaroslav Tykhiy goto got_oob; 1991ea022d16SRodney W. Grimes byte_count++; 1992ea022d16SRodney W. Grimes if (c == '\n') { 1993ea022d16SRodney W. Grimes if (ferror(outstr)) 1994ea022d16SRodney W. Grimes goto data_err; 1995ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1996ea022d16SRodney W. Grimes } 1997ea022d16SRodney W. Grimes (void) putc(c, outstr); 1998ea022d16SRodney W. Grimes } 19994b82fc95SYaroslav Tykhiy if (recvurg) 20004b82fc95SYaroslav Tykhiy goto got_oob; 2001ea022d16SRodney W. Grimes fflush(outstr); 2002ea022d16SRodney W. Grimes transflag = 0; 2003ea022d16SRodney W. Grimes if (ferror(instr)) 2004ea022d16SRodney W. Grimes goto file_err; 2005ea022d16SRodney W. Grimes if (ferror(outstr)) 2006ea022d16SRodney W. Grimes goto data_err; 2007ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 20084b82fc95SYaroslav Tykhiy return (0); 2009ea022d16SRodney W. Grimes 2010ea022d16SRodney W. Grimes case TYPE_I: 2011ea022d16SRodney W. Grimes case TYPE_L: 20129fc5823aSGarrett Wollman /* 20139fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 20149fc5823aSGarrett Wollman * are sending a regular file 20159fc5823aSGarrett Wollman */ 20169fc5823aSGarrett Wollman netfd = fileno(outstr); 20179fc5823aSGarrett Wollman filefd = fileno(instr); 20189fc5823aSGarrett Wollman 2019f6f0c4b9SDan Moschuk if (isreg) { 20209fc5823aSGarrett Wollman 2021f6f0c4b9SDan Moschuk off_t offset; 2022f6f0c4b9SDan Moschuk int err; 2023f6f0c4b9SDan Moschuk 2024f6f0c4b9SDan Moschuk err = cnt = offset = 0; 2025f6f0c4b9SDan Moschuk 2026492f1d9cSMaxim Konovalov while (err != -1 && filesize > 0) { 2027492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0, 2028f6f0c4b9SDan Moschuk (struct sf_hdtr *) NULL, &cnt, 0); 20293af48c42SMaxim Konovalov /* 20303af48c42SMaxim Konovalov * Calculate byte_count before OOB processing. 20313af48c42SMaxim Konovalov * It can be used in myoob() later. 20323af48c42SMaxim Konovalov */ 20333af48c42SMaxim Konovalov byte_count += cnt; 20344b82fc95SYaroslav Tykhiy if (recvurg) 20354b82fc95SYaroslav Tykhiy goto got_oob; 2036f6f0c4b9SDan Moschuk offset += cnt; 2037492f1d9cSMaxim Konovalov filesize -= cnt; 2038f6f0c4b9SDan Moschuk 2039f6f0c4b9SDan Moschuk if (err == -1) { 2040f6f0c4b9SDan Moschuk if (!cnt) 2041f6f0c4b9SDan Moschuk goto oldway; 2042f6f0c4b9SDan Moschuk 20439fc5823aSGarrett Wollman goto data_err; 2044f6f0c4b9SDan Moschuk } 2045f6f0c4b9SDan Moschuk } 2046f6f0c4b9SDan Moschuk 20470849c184SDan Moschuk transflag = 0; 20489fc5823aSGarrett Wollman reply(226, "Transfer complete."); 20494b82fc95SYaroslav Tykhiy return (0); 20509fc5823aSGarrett Wollman } 20519fc5823aSGarrett Wollman 20529fc5823aSGarrett Wollman oldway: 2053ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 2054ea022d16SRodney W. Grimes transflag = 0; 2055ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 20564b82fc95SYaroslav Tykhiy return (-1); 2057ea022d16SRodney W. Grimes } 20589fc5823aSGarrett Wollman 2059ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 2060ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 2061ea022d16SRodney W. Grimes byte_count += cnt; 2062ea022d16SRodney W. Grimes transflag = 0; 2063ea022d16SRodney W. Grimes (void)free(buf); 2064ea022d16SRodney W. Grimes if (cnt != 0) { 2065ea022d16SRodney W. Grimes if (cnt < 0) 2066ea022d16SRodney W. Grimes goto file_err; 2067ea022d16SRodney W. Grimes goto data_err; 2068ea022d16SRodney W. Grimes } 2069ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 20704b82fc95SYaroslav Tykhiy return (0); 2071ea022d16SRodney W. Grimes default: 2072ea022d16SRodney W. Grimes transflag = 0; 2073ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 20744b82fc95SYaroslav Tykhiy return (-1); 2075ea022d16SRodney W. Grimes } 2076ea022d16SRodney W. Grimes 2077ea022d16SRodney W. Grimes data_err: 2078ea022d16SRodney W. Grimes transflag = 0; 2079ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 20804b82fc95SYaroslav Tykhiy return (-1); 2081ea022d16SRodney W. Grimes 2082ea022d16SRodney W. Grimes file_err: 2083ea022d16SRodney W. Grimes transflag = 0; 2084ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 20854b82fc95SYaroslav Tykhiy return (-1); 20864b82fc95SYaroslav Tykhiy 20874b82fc95SYaroslav Tykhiy got_oob: 20884b82fc95SYaroslav Tykhiy myoob(); 20894b82fc95SYaroslav Tykhiy recvurg = 0; 20904b82fc95SYaroslav Tykhiy transflag = 0; 20914b82fc95SYaroslav Tykhiy return (-1); 2092ea022d16SRodney W. Grimes } 2093ea022d16SRodney W. Grimes 2094ea022d16SRodney W. Grimes /* 2095ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 2096ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 2097ea022d16SRodney W. Grimes * 2098ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 2099ea022d16SRodney W. Grimes */ 2100ea022d16SRodney W. Grimes static int 2101e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr) 2102ea022d16SRodney W. Grimes { 2103ea022d16SRodney W. Grimes int c; 210461f891a6SPaul Traina int cnt, bare_lfs; 2105ea022d16SRodney W. Grimes char buf[BUFSIZ]; 2106ea022d16SRodney W. Grimes 2107ea022d16SRodney W. Grimes transflag++; 210861f891a6SPaul Traina bare_lfs = 0; 210961f891a6SPaul Traina 2110ea022d16SRodney W. Grimes switch (type) { 2111ea022d16SRodney W. Grimes 2112ea022d16SRodney W. Grimes case TYPE_I: 2113ea022d16SRodney W. Grimes case TYPE_L: 2114ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 21154b82fc95SYaroslav Tykhiy if (recvurg) 21164b82fc95SYaroslav Tykhiy goto got_oob; 2117ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 2118ea022d16SRodney W. Grimes goto file_err; 2119ea022d16SRodney W. Grimes byte_count += cnt; 2120ea022d16SRodney W. Grimes } 21214b82fc95SYaroslav Tykhiy if (recvurg) 21224b82fc95SYaroslav Tykhiy goto got_oob; 2123ea022d16SRodney W. Grimes if (cnt < 0) 2124ea022d16SRodney W. Grimes goto data_err; 2125ea022d16SRodney W. Grimes transflag = 0; 2126ea022d16SRodney W. Grimes return (0); 2127ea022d16SRodney W. Grimes 2128ea022d16SRodney W. Grimes case TYPE_E: 2129ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 2130ea022d16SRodney W. Grimes transflag = 0; 2131ea022d16SRodney W. Grimes return (-1); 2132ea022d16SRodney W. Grimes 2133ea022d16SRodney W. Grimes case TYPE_A: 2134ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 21354b82fc95SYaroslav Tykhiy if (recvurg) 21364b82fc95SYaroslav Tykhiy goto got_oob; 2137ea022d16SRodney W. Grimes byte_count++; 2138ea022d16SRodney W. Grimes if (c == '\n') 2139ea022d16SRodney W. Grimes bare_lfs++; 2140ea022d16SRodney W. Grimes while (c == '\r') { 2141ea022d16SRodney W. Grimes if (ferror(outstr)) 2142ea022d16SRodney W. Grimes goto data_err; 2143ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 2144ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 2145ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 2146ea022d16SRodney W. Grimes goto contin2; 2147ea022d16SRodney W. Grimes } 2148ea022d16SRodney W. Grimes } 2149ea022d16SRodney W. Grimes (void) putc(c, outstr); 2150ea022d16SRodney W. Grimes contin2: ; 2151ea022d16SRodney W. Grimes } 21524b82fc95SYaroslav Tykhiy if (recvurg) 21534b82fc95SYaroslav Tykhiy goto got_oob; 2154ea022d16SRodney W. Grimes fflush(outstr); 2155ea022d16SRodney W. Grimes if (ferror(instr)) 2156ea022d16SRodney W. Grimes goto data_err; 2157ea022d16SRodney W. Grimes if (ferror(outstr)) 2158ea022d16SRodney W. Grimes goto file_err; 2159ea022d16SRodney W. Grimes transflag = 0; 2160ea022d16SRodney W. Grimes if (bare_lfs) { 2161ea022d16SRodney W. Grimes lreply(226, 2162ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 2163ea022d16SRodney W. Grimes bare_lfs); 2164ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 2165ea022d16SRodney W. Grimes } 2166ea022d16SRodney W. Grimes return (0); 2167ea022d16SRodney W. Grimes default: 2168ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 2169ea022d16SRodney W. Grimes transflag = 0; 2170ea022d16SRodney W. Grimes return (-1); 2171ea022d16SRodney W. Grimes } 2172ea022d16SRodney W. Grimes 2173ea022d16SRodney W. Grimes data_err: 2174ea022d16SRodney W. Grimes transflag = 0; 2175ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 2176ea022d16SRodney W. Grimes return (-1); 2177ea022d16SRodney W. Grimes 2178ea022d16SRodney W. Grimes file_err: 2179ea022d16SRodney W. Grimes transflag = 0; 2180ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 2181ea022d16SRodney W. Grimes return (-1); 21824b82fc95SYaroslav Tykhiy 21834b82fc95SYaroslav Tykhiy got_oob: 21844b82fc95SYaroslav Tykhiy myoob(); 21854b82fc95SYaroslav Tykhiy recvurg = 0; 21864b82fc95SYaroslav Tykhiy transflag = 0; 21874b82fc95SYaroslav Tykhiy return (-1); 2188ea022d16SRodney W. Grimes } 2189ea022d16SRodney W. Grimes 2190ea022d16SRodney W. Grimes void 2191e4bc453cSWarner Losh statfilecmd(char *filename) 2192ea022d16SRodney W. Grimes { 2193ea022d16SRodney W. Grimes FILE *fin; 2194f8a581a0SYaroslav Tykhiy int atstart; 2195ea022d16SRodney W. Grimes int c; 2196ea022d16SRodney W. Grimes char line[LINE_MAX]; 2197ea022d16SRodney W. Grimes 2198af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 2199ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 2200ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 2201f8a581a0SYaroslav Tykhiy atstart = 1; 2202ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 2203ea022d16SRodney W. Grimes if (c == '\n') { 2204ea022d16SRodney W. Grimes if (ferror(stdout)){ 2205ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 2206ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2207ea022d16SRodney W. Grimes dologout(1); 2208ea022d16SRodney W. Grimes /* NOTREACHED */ 2209ea022d16SRodney W. Grimes } 2210ea022d16SRodney W. Grimes if (ferror(fin)) { 2211ea022d16SRodney W. Grimes perror_reply(551, filename); 2212ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2213ea022d16SRodney W. Grimes return; 2214ea022d16SRodney W. Grimes } 2215ea022d16SRodney W. Grimes (void) putc('\r', stdout); 2216ea022d16SRodney W. Grimes } 2217f8a581a0SYaroslav Tykhiy /* 2218f8a581a0SYaroslav Tykhiy * RFC 959 says neutral text should be prepended before 2219f8a581a0SYaroslav Tykhiy * a leading 3-digit number followed by whitespace, but 2220f8a581a0SYaroslav Tykhiy * many ftp clients can be confused by any leading digits, 2221f8a581a0SYaroslav Tykhiy * as a matter of fact. 2222f8a581a0SYaroslav Tykhiy */ 2223f8a581a0SYaroslav Tykhiy if (atstart && isdigit(c)) 2224f8a581a0SYaroslav Tykhiy (void) putc(' ', stdout); 2225ea022d16SRodney W. Grimes (void) putc(c, stdout); 2226f8a581a0SYaroslav Tykhiy atstart = (c == '\n'); 2227ea022d16SRodney W. Grimes } 2228ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 2229ea022d16SRodney W. Grimes reply(211, "End of Status"); 2230ea022d16SRodney W. Grimes } 2231ea022d16SRodney W. Grimes 2232ea022d16SRodney W. Grimes void 2233e4bc453cSWarner Losh statcmd(void) 2234ea022d16SRodney W. Grimes { 22354dd8b5abSYoshinobu Inoue union sockunion *su; 2236ea022d16SRodney W. Grimes u_char *a, *p; 2237b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST]; 22384dd8b5abSYoshinobu Inoue int ispassive; 2239ea022d16SRodney W. Grimes 2240c152df28SYaroslav Tykhiy if (hostinfo) { 2241a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname); 2242ea022d16SRodney W. Grimes printf(" %s\r\n", version); 2243c152df28SYaroslav Tykhiy } else 2244c152df28SYaroslav Tykhiy lreply(211, "FTP server status:"); 2245ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 22464dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 2247b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { 22484dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 22494dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 22504dd8b5abSYoshinobu Inoue } 2251ea022d16SRodney W. Grimes printf("\r\n"); 2252ea022d16SRodney W. Grimes if (logged_in) { 2253ea022d16SRodney W. Grimes if (guest) 2254ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 2255ea022d16SRodney W. Grimes else 2256ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 2257ea022d16SRodney W. Grimes } else if (askpasswd) 2258ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 2259ea022d16SRodney W. Grimes else 2260ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 2261ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 2262ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 2263ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 2264ea022d16SRodney W. Grimes if (type == TYPE_L) 226589fdc4e1SMike Barcroft #if CHAR_BIT == 8 226689fdc4e1SMike Barcroft printf(" %d", CHAR_BIT); 2267ea022d16SRodney W. Grimes #else 2268ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 2269ea022d16SRodney W. Grimes #endif 2270ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 2271ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 2272ea022d16SRodney W. Grimes if (data != -1) 2273ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 2274ea022d16SRodney W. Grimes else if (pdata != -1) { 22754dd8b5abSYoshinobu Inoue ispassive = 1; 22764dd8b5abSYoshinobu Inoue su = &pasv_addr; 2277ea022d16SRodney W. Grimes goto printaddr; 2278ea022d16SRodney W. Grimes } else if (usedefault == 0) { 22794dd8b5abSYoshinobu Inoue ispassive = 0; 22804dd8b5abSYoshinobu Inoue su = &data_dest; 2281ea022d16SRodney W. Grimes printaddr: 2282ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 22834dd8b5abSYoshinobu Inoue if (epsvall) { 22844dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 22854dd8b5abSYoshinobu Inoue goto epsvonly; 22864dd8b5abSYoshinobu Inoue } 22874dd8b5abSYoshinobu Inoue 22884dd8b5abSYoshinobu Inoue /* PORT/PASV */ 22894dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 22904dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 22914dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 22924dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 22934dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 22944dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 22954dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 22964dd8b5abSYoshinobu Inoue } 22974dd8b5abSYoshinobu Inoue 22984dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 22994dd8b5abSYoshinobu Inoue { 23004dd8b5abSYoshinobu Inoue int alen, af, i; 23014dd8b5abSYoshinobu Inoue 23024dd8b5abSYoshinobu Inoue switch (su->su_family) { 23034dd8b5abSYoshinobu Inoue case AF_INET: 23044dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 23054dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 23064dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 23074dd8b5abSYoshinobu Inoue af = 4; 23084dd8b5abSYoshinobu Inoue break; 23094dd8b5abSYoshinobu Inoue case AF_INET6: 23104dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 23114dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 23124dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 23134dd8b5abSYoshinobu Inoue af = 6; 23144dd8b5abSYoshinobu Inoue break; 23154dd8b5abSYoshinobu Inoue default: 23164dd8b5abSYoshinobu Inoue af = 0; 23174dd8b5abSYoshinobu Inoue break; 23184dd8b5abSYoshinobu Inoue } 23194dd8b5abSYoshinobu Inoue if (af) { 23204dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 23214dd8b5abSYoshinobu Inoue af, alen); 23224dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 23234dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 23244dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 23254dd8b5abSYoshinobu Inoue } 23264dd8b5abSYoshinobu Inoue } 23274dd8b5abSYoshinobu Inoue 23284dd8b5abSYoshinobu Inoue epsvonly:; 23294dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 23304dd8b5abSYoshinobu Inoue { 23314dd8b5abSYoshinobu Inoue int af; 23324dd8b5abSYoshinobu Inoue 23334dd8b5abSYoshinobu Inoue switch (su->su_family) { 23344dd8b5abSYoshinobu Inoue case AF_INET: 23354dd8b5abSYoshinobu Inoue af = 1; 23364dd8b5abSYoshinobu Inoue break; 23374dd8b5abSYoshinobu Inoue case AF_INET6: 23384dd8b5abSYoshinobu Inoue af = 2; 23394dd8b5abSYoshinobu Inoue break; 23404dd8b5abSYoshinobu Inoue default: 23414dd8b5abSYoshinobu Inoue af = 0; 23424dd8b5abSYoshinobu Inoue break; 23434dd8b5abSYoshinobu Inoue } 23444dd8b5abSYoshinobu Inoue if (af) { 2345b0f06defSHajimu UMEMOTO union sockunion tmp; 2346b0f06defSHajimu UMEMOTO 2347b0f06defSHajimu UMEMOTO tmp = *su; 2348b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6) 2349b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0; 2350b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, 23514dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 23524dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 23534dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 23544dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 2355b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port)); 23564dd8b5abSYoshinobu Inoue } 23574dd8b5abSYoshinobu Inoue } 23584dd8b5abSYoshinobu Inoue } 2359ea022d16SRodney W. Grimes #undef UC 2360ea022d16SRodney W. Grimes } else 2361ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 2362ea022d16SRodney W. Grimes reply(211, "End of status"); 2363ea022d16SRodney W. Grimes } 2364ea022d16SRodney W. Grimes 2365ea022d16SRodney W. Grimes void 2366e4bc453cSWarner Losh fatalerror(char *s) 2367ea022d16SRodney W. Grimes { 2368ea022d16SRodney W. Grimes 2369ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 2370ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2371ea022d16SRodney W. Grimes dologout(0); 2372ea022d16SRodney W. Grimes /* NOTREACHED */ 2373ea022d16SRodney W. Grimes } 2374ea022d16SRodney W. Grimes 2375ea022d16SRodney W. Grimes void 2376ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2377ea022d16SRodney W. Grimes { 2378ea022d16SRodney W. Grimes va_list ap; 2379e4bc453cSWarner Losh 2380ea022d16SRodney W. Grimes va_start(ap, fmt); 2381ea022d16SRodney W. Grimes (void)printf("%d ", n); 2382ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2383ea022d16SRodney W. Grimes (void)printf("\r\n"); 2384ea022d16SRodney W. Grimes (void)fflush(stdout); 2385618b0bbaSMark Murray if (ftpdebug) { 2386ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 2387ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2388ea022d16SRodney W. Grimes } 2389ea022d16SRodney W. Grimes } 2390ea022d16SRodney W. Grimes 2391ea022d16SRodney W. Grimes void 2392ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2393ea022d16SRodney W. Grimes { 2394ea022d16SRodney W. Grimes va_list ap; 2395e4bc453cSWarner Losh 2396ea022d16SRodney W. Grimes va_start(ap, fmt); 2397ea022d16SRodney W. Grimes (void)printf("%d- ", n); 2398ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2399ea022d16SRodney W. Grimes (void)printf("\r\n"); 2400ea022d16SRodney W. Grimes (void)fflush(stdout); 2401618b0bbaSMark Murray if (ftpdebug) { 2402ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 2403ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2404ea022d16SRodney W. Grimes } 2405ea022d16SRodney W. Grimes } 2406ea022d16SRodney W. Grimes 2407ea022d16SRodney W. Grimes static void 2408e4bc453cSWarner Losh ack(char *s) 2409ea022d16SRodney W. Grimes { 2410ea022d16SRodney W. Grimes 2411ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2412ea022d16SRodney W. Grimes } 2413ea022d16SRodney W. Grimes 2414ea022d16SRodney W. Grimes void 2415e4bc453cSWarner Losh nack(char *s) 2416ea022d16SRodney W. Grimes { 2417ea022d16SRodney W. Grimes 2418ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2419ea022d16SRodney W. Grimes } 2420ea022d16SRodney W. Grimes 2421ea022d16SRodney W. Grimes /* ARGSUSED */ 2422ea022d16SRodney W. Grimes void 2423e4bc453cSWarner Losh yyerror(char *s) 2424ea022d16SRodney W. Grimes { 2425ea022d16SRodney W. Grimes char *cp; 2426ea022d16SRodney W. Grimes 24279aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2428ea022d16SRodney W. Grimes *cp = '\0'; 2429ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 2430ea022d16SRodney W. Grimes } 2431ea022d16SRodney W. Grimes 2432ea022d16SRodney W. Grimes void 2433e4bc453cSWarner Losh delete(char *name) 2434ea022d16SRodney W. Grimes { 2435ea022d16SRodney W. Grimes struct stat st; 2436ea022d16SRodney W. Grimes 2437ea022d16SRodney W. Grimes LOGCMD("delete", name); 24381b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2439ea022d16SRodney W. Grimes perror_reply(550, name); 2440ea022d16SRodney W. Grimes return; 2441ea022d16SRodney W. Grimes } 2442ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 2443ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2444ea022d16SRodney W. Grimes perror_reply(550, name); 2445ea022d16SRodney W. Grimes return; 2446ea022d16SRodney W. Grimes } 2447ea022d16SRodney W. Grimes goto done; 2448ea022d16SRodney W. Grimes } 2449ea022d16SRodney W. Grimes if (unlink(name) < 0) { 2450ea022d16SRodney W. Grimes perror_reply(550, name); 2451ea022d16SRodney W. Grimes return; 2452ea022d16SRodney W. Grimes } 2453ea022d16SRodney W. Grimes done: 2454ea022d16SRodney W. Grimes ack("DELE"); 2455ea022d16SRodney W. Grimes } 2456ea022d16SRodney W. Grimes 2457ea022d16SRodney W. Grimes void 2458e4bc453cSWarner Losh cwd(char *path) 2459ea022d16SRodney W. Grimes { 2460ea022d16SRodney W. Grimes 2461ea022d16SRodney W. Grimes if (chdir(path) < 0) 2462ea022d16SRodney W. Grimes perror_reply(550, path); 2463ea022d16SRodney W. Grimes else 2464ea022d16SRodney W. Grimes ack("CWD"); 2465ea022d16SRodney W. Grimes } 2466ea022d16SRodney W. Grimes 2467ea022d16SRodney W. Grimes void 2468e4bc453cSWarner Losh makedir(char *name) 2469ea022d16SRodney W. Grimes { 24702b748987SYaroslav Tykhiy char *s; 2471ea022d16SRodney W. Grimes 2472ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2473d186bb12SMatthew N. Dodd if (guest && noguestmkd) 2474d186bb12SMatthew N. Dodd reply(550, "%s: permission denied", name); 2475d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0) 2476ea022d16SRodney W. Grimes perror_reply(550, name); 24772b748987SYaroslav Tykhiy else { 24782b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL) 24792b748987SYaroslav Tykhiy fatalerror("Ran out of memory."); 24802b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s); 24812b748987SYaroslav Tykhiy free(s); 24822b748987SYaroslav Tykhiy } 2483ea022d16SRodney W. Grimes } 2484ea022d16SRodney W. Grimes 2485ea022d16SRodney W. Grimes void 2486e4bc453cSWarner Losh removedir(char *name) 2487ea022d16SRodney W. Grimes { 2488ea022d16SRodney W. Grimes 2489ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2490ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2491ea022d16SRodney W. Grimes perror_reply(550, name); 2492ea022d16SRodney W. Grimes else 2493ea022d16SRodney W. Grimes ack("RMD"); 2494ea022d16SRodney W. Grimes } 2495ea022d16SRodney W. Grimes 2496ea022d16SRodney W. Grimes void 2497e4bc453cSWarner Losh pwd(void) 2498ea022d16SRodney W. Grimes { 2499e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1]; 2500ea022d16SRodney W. Grimes 2501ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 2502ea022d16SRodney W. Grimes reply(550, "%s.", path); 2503e4648f05SYaroslav Tykhiy else { 2504e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL) 2505e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory."); 2506e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s); 2507e4648f05SYaroslav Tykhiy free(s); 2508e4648f05SYaroslav Tykhiy } 2509ea022d16SRodney W. Grimes } 2510ea022d16SRodney W. Grimes 2511ea022d16SRodney W. Grimes char * 2512e4bc453cSWarner Losh renamefrom(char *name) 2513ea022d16SRodney W. Grimes { 2514ea022d16SRodney W. Grimes struct stat st; 2515ea022d16SRodney W. Grimes 25161b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) { 2517ea022d16SRodney W. Grimes perror_reply(550, name); 2518ea022d16SRodney W. Grimes return ((char *)0); 2519ea022d16SRodney W. Grimes } 2520ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 2521ea022d16SRodney W. Grimes return (name); 2522ea022d16SRodney W. Grimes } 2523ea022d16SRodney W. Grimes 2524ea022d16SRodney W. Grimes void 2525e4bc453cSWarner Losh renamecmd(char *from, char *to) 2526ea022d16SRodney W. Grimes { 252761f891a6SPaul Traina struct stat st; 2528ea022d16SRodney W. Grimes 2529ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 253061f891a6SPaul Traina 253161f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 253261f891a6SPaul Traina reply(550, "%s: permission denied", to); 253361f891a6SPaul Traina return; 253461f891a6SPaul Traina } 253561f891a6SPaul Traina 2536ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2537ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2538ea022d16SRodney W. Grimes else 2539ea022d16SRodney W. Grimes ack("RNTO"); 2540ea022d16SRodney W. Grimes } 2541ea022d16SRodney W. Grimes 2542ea022d16SRodney W. Grimes static void 2543e4bc453cSWarner Losh dolog(struct sockaddr *who) 2544ea022d16SRodney W. Grimes { 25454dd8b5abSYoshinobu Inoue int error; 25464dd8b5abSYoshinobu Inoue 25474dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 2548ea022d16SRodney W. Grimes 2549ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2550ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2551ea4e54b9SDavid Nugent if (thishost != firsthost) 2552ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2553ea4e54b9SDavid Nugent remotehost, hostname); 2554ea4e54b9SDavid Nugent else 2555ea4e54b9SDavid Nugent #endif 2556ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2557ea4e54b9SDavid Nugent remotehost); 2558b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2559ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2560ea022d16SRodney W. Grimes 2561ea4e54b9SDavid Nugent if (logging) { 2562ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2563ea4e54b9SDavid Nugent if (thishost != firsthost) 2564ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 2565ea4e54b9SDavid Nugent remotehost, hostname); 2566ea4e54b9SDavid Nugent else 2567ea4e54b9SDavid Nugent #endif 25684dd8b5abSYoshinobu Inoue { 25694dd8b5abSYoshinobu Inoue char who_name[MAXHOSTNAMELEN]; 25704dd8b5abSYoshinobu Inoue 25714dd8b5abSYoshinobu Inoue error = getnameinfo(who, who->sa_len, 25724dd8b5abSYoshinobu Inoue who_name, sizeof(who_name) - 1, 2573b0f06defSHajimu UMEMOTO NULL, 0, NI_NUMERICHOST); 2574f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 25754dd8b5abSYoshinobu Inoue error == 0 ? who_name : ""); 25764dd8b5abSYoshinobu Inoue } 2577ea022d16SRodney W. Grimes } 2578ea4e54b9SDavid Nugent } 2579ea022d16SRodney W. Grimes 2580ea022d16SRodney W. Grimes /* 2581ea022d16SRodney W. Grimes * Record logout in wtmp file 2582ea022d16SRodney W. Grimes * and exit with supplied status. 2583ea022d16SRodney W. Grimes */ 2584ea022d16SRodney W. Grimes void 2585e4bc453cSWarner Losh dologout(int status) 2586ea022d16SRodney W. Grimes { 25870b4df2eeSDavid Greenman /* 25880b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 25890b4df2eeSDavid Greenman * back to the main program loop. 25900b4df2eeSDavid Greenman */ 25910b4df2eeSDavid Greenman transflag = 0; 2592ea022d16SRodney W. Grimes 25935d7e0128SYaroslav Tykhiy if (logged_in && dowtmp) { 2594ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 259546948173SHajimu UMEMOTO ftpd_logwtmp(ttyline, "", NULL); 2596ea022d16SRodney W. Grimes } 2597ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2598ea022d16SRodney W. Grimes _exit(status); 2599ea022d16SRodney W. Grimes } 2600ea022d16SRodney W. Grimes 2601ea022d16SRodney W. Grimes static void 2602e4bc453cSWarner Losh sigurg(int signo) 2603ea022d16SRodney W. Grimes { 26044b82fc95SYaroslav Tykhiy 26054b82fc95SYaroslav Tykhiy recvurg = 1; 26064b82fc95SYaroslav Tykhiy } 26074b82fc95SYaroslav Tykhiy 26084b82fc95SYaroslav Tykhiy static void 2609e4bc453cSWarner Losh myoob(void) 26104b82fc95SYaroslav Tykhiy { 2611ea022d16SRodney W. Grimes char *cp; 2612ea022d16SRodney W. Grimes 2613ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2614ea022d16SRodney W. Grimes if (!transflag) 2615ea022d16SRodney W. Grimes return; 2616ea022d16SRodney W. Grimes cp = tmpline; 2617ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2618ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2619ea022d16SRodney W. Grimes dologout(0); 2620ea022d16SRodney W. Grimes } 2621ea022d16SRodney W. Grimes upper(cp); 2622ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2623ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2624ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 2625ea022d16SRodney W. Grimes reply(226, "Abort successful"); 2626ea022d16SRodney W. Grimes } 2627ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 26289db4bbf3SMichael Haro tmpline[0] = '\0'; 2629ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 2630ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 2631ea022d16SRodney W. Grimes byte_count, file_size); 2632ea022d16SRodney W. Grimes else 2633ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 2634ea022d16SRodney W. Grimes } 2635ea022d16SRodney W. Grimes } 2636ea022d16SRodney W. Grimes 2637ea022d16SRodney W. Grimes /* 2638ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2639ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2640ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2641ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2642ea022d16SRodney W. Grimes */ 2643ea022d16SRodney W. Grimes void 2644e4bc453cSWarner Losh passive(void) 2645ea022d16SRodney W. Grimes { 26468af7c9a3SYaroslav Tykhiy int len, on; 2647ea022d16SRodney W. Grimes char *p, *a; 2648ea022d16SRodney W. Grimes 264961f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 265061f891a6SPaul Traina close(pdata); 265161f891a6SPaul Traina 26524dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2653ea022d16SRodney W. Grimes if (pdata < 0) { 2654ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2655ea022d16SRodney W. Grimes return; 2656ea022d16SRodney W. Grimes } 26578af7c9a3SYaroslav Tykhiy on = 1; 26588af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 26598af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 26604c450ad7SPaul Traina 26614c450ad7SPaul Traina (void) seteuid((uid_t)0); 266261f891a6SPaul Traina 2663dacc9752SPaul Traina #ifdef IP_PORTRANGE 26644dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 26658af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 2666dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2667dacc9752SPaul Traina 266840e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 266957d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 26704c450ad7SPaul Traina goto pasv_error; 26714c450ad7SPaul Traina } 2672dacc9752SPaul Traina #endif 26732db39860SNick Sayer #ifdef IPV6_PORTRANGE 26742db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 26758af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 26762db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 26772db39860SNick Sayer 26782db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 267957d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 26802db39860SNick Sayer goto pasv_error; 26812db39860SNick Sayer } 26822db39860SNick Sayer #endif 268340e9d39eSPeter Wemm 2684ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 26854dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 26864dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2687ea022d16SRodney W. Grimes goto pasv_error; 268861f891a6SPaul Traina 2689ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 26904c450ad7SPaul Traina 2691ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2692ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2693ea022d16SRodney W. Grimes goto pasv_error; 2694ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2695ea022d16SRodney W. Grimes goto pasv_error; 26964dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 26974dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 26984dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 26994dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 27004dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 27014dd8b5abSYoshinobu Inoue else 27024dd8b5abSYoshinobu Inoue goto pasv_error; 27034dd8b5abSYoshinobu Inoue 27044dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2705ea022d16SRodney W. Grimes 2706ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2707ea022d16SRodney W. Grimes 2708ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2709ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2710ea022d16SRodney W. Grimes return; 2711ea022d16SRodney W. Grimes 2712ea022d16SRodney W. Grimes pasv_error: 271361f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2714ea022d16SRodney W. Grimes (void) close(pdata); 2715ea022d16SRodney W. Grimes pdata = -1; 2716ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2717ea022d16SRodney W. Grimes return; 2718ea022d16SRodney W. Grimes } 2719ea022d16SRodney W. Grimes 2720ea022d16SRodney W. Grimes /* 27214dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 27224dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 27234dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 27244dd8b5abSYoshinobu Inoue */ 27254dd8b5abSYoshinobu Inoue 27264dd8b5abSYoshinobu Inoue void 2727e4bc453cSWarner Losh long_passive(char *cmd, int pf) 27284dd8b5abSYoshinobu Inoue { 27298af7c9a3SYaroslav Tykhiy int len, on; 27304dd8b5abSYoshinobu Inoue char *p, *a; 27314dd8b5abSYoshinobu Inoue 27324dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 27334dd8b5abSYoshinobu Inoue close(pdata); 27344dd8b5abSYoshinobu Inoue 27354dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 27364dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 27374dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 27384dd8b5abSYoshinobu Inoue case AF_INET: 27394dd8b5abSYoshinobu Inoue pf = 1; 27404dd8b5abSYoshinobu Inoue break; 27414dd8b5abSYoshinobu Inoue case AF_INET6: 27424dd8b5abSYoshinobu Inoue pf = 2; 27434dd8b5abSYoshinobu Inoue break; 27444dd8b5abSYoshinobu Inoue default: 27454dd8b5abSYoshinobu Inoue pf = 0; 27464dd8b5abSYoshinobu Inoue break; 27474dd8b5abSYoshinobu Inoue } 27484dd8b5abSYoshinobu Inoue /* 27494dd8b5abSYoshinobu Inoue * XXX 27504dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 27514dd8b5abSYoshinobu Inoue */ 27524dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 27534dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 27544dd8b5abSYoshinobu Inoue "use (%d)", pf); 27554dd8b5abSYoshinobu Inoue } else 27564dd8b5abSYoshinobu Inoue reply(501, "Network protocol mismatch"); /*XXX*/ 27574dd8b5abSYoshinobu Inoue 27584dd8b5abSYoshinobu Inoue return; 27594dd8b5abSYoshinobu Inoue } 27604dd8b5abSYoshinobu Inoue } 27614dd8b5abSYoshinobu Inoue 27624dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 27634dd8b5abSYoshinobu Inoue if (pdata < 0) { 27644dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 27654dd8b5abSYoshinobu Inoue return; 27664dd8b5abSYoshinobu Inoue } 27678af7c9a3SYaroslav Tykhiy on = 1; 27688af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 27698af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); 27704dd8b5abSYoshinobu Inoue 27714dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)0); 27724dd8b5abSYoshinobu Inoue 27734dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 27744dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 27754dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 27764dd8b5abSYoshinobu Inoue 27772db39860SNick Sayer #ifdef IP_PORTRANGE 27782db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 27798af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH 27802db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 27812db39860SNick Sayer 27822db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 278357d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 27842db39860SNick Sayer goto pasv_error; 27852db39860SNick Sayer } 27862db39860SNick Sayer #endif 27872db39860SNick Sayer #ifdef IPV6_PORTRANGE 27882db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 27898af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 27902db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 27912db39860SNick Sayer 27922db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 279357d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0) 27942db39860SNick Sayer goto pasv_error; 27952db39860SNick Sayer } 27962db39860SNick Sayer #endif 27972db39860SNick Sayer 27984dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 27994dd8b5abSYoshinobu Inoue goto pasv_error; 28004dd8b5abSYoshinobu Inoue 28014dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 28024dd8b5abSYoshinobu Inoue 28034dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 28044dd8b5abSYoshinobu Inoue goto pasv_error; 28054dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 28064dd8b5abSYoshinobu Inoue goto pasv_error; 28074dd8b5abSYoshinobu Inoue 28084dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 28094dd8b5abSYoshinobu Inoue 28104dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 28114dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 28124dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 28134dd8b5abSYoshinobu Inoue case AF_INET: 28144dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 28154dd8b5abSYoshinobu Inoue v4_reply: 28164dd8b5abSYoshinobu Inoue reply(228, 28174dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 28184dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 28194dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 28204dd8b5abSYoshinobu Inoue return; 28214dd8b5abSYoshinobu Inoue case AF_INET6: 28224dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 28234dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 28244dd8b5abSYoshinobu Inoue goto v4_reply; 28254dd8b5abSYoshinobu Inoue } 28264dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 28274dd8b5abSYoshinobu Inoue reply(228, 28284dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 28294dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 28304dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 28314dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 28324dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 28334dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 28344dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 28354dd8b5abSYoshinobu Inoue return; 28364dd8b5abSYoshinobu Inoue } 28374dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 28384dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 28394dd8b5abSYoshinobu Inoue case AF_INET: 28404dd8b5abSYoshinobu Inoue case AF_INET6: 28414dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 28424dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 28434dd8b5abSYoshinobu Inoue return; 28444dd8b5abSYoshinobu Inoue } 28454dd8b5abSYoshinobu Inoue } else { 28464dd8b5abSYoshinobu Inoue /* more proper error code? */ 28474dd8b5abSYoshinobu Inoue } 28484dd8b5abSYoshinobu Inoue 28494dd8b5abSYoshinobu Inoue pasv_error: 28504dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 28514dd8b5abSYoshinobu Inoue (void) close(pdata); 28524dd8b5abSYoshinobu Inoue pdata = -1; 28534dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 28544dd8b5abSYoshinobu Inoue return; 28554dd8b5abSYoshinobu Inoue } 28564dd8b5abSYoshinobu Inoue 28574dd8b5abSYoshinobu Inoue /* 2858a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local" 2859a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races. 2860a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99". 2861a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name. 2862a117c345SYaroslav Tykhiy * 2863ea022d16SRodney W. Grimes * Generates failure reply on error. 2864ea022d16SRodney W. Grimes */ 2865a117c345SYaroslav Tykhiy static int 2866a117c345SYaroslav Tykhiy guniquefd(char *local, char **name) 2867ea022d16SRodney W. Grimes { 2868ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2869ea022d16SRodney W. Grimes struct stat st; 2870ea022d16SRodney W. Grimes char *cp; 2871a117c345SYaroslav Tykhiy int count; 2872a117c345SYaroslav Tykhiy int fd; 2873ea022d16SRodney W. Grimes 2874ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2875ea022d16SRodney W. Grimes if (cp) 2876ea022d16SRodney W. Grimes *cp = '\0'; 2877ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2878ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2879a117c345SYaroslav Tykhiy return (-1); 2880ea022d16SRodney W. Grimes } 2881a117c345SYaroslav Tykhiy if (cp) { 2882a117c345SYaroslav Tykhiy /* 2883a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix. 2884a117c345SYaroslav Tykhiy * -4 is for /nn\0 2885a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix. 2886a117c345SYaroslav Tykhiy */ 2887a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) { 2888a117c345SYaroslav Tykhiy reply(553, "Pathname too long"); 2889a117c345SYaroslav Tykhiy return (-1); 2890a117c345SYaroslav Tykhiy } 2891ea022d16SRodney W. Grimes *cp = '/'; 2892a117c345SYaroslav Tykhiy } 2893e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2894e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2895ea022d16SRodney W. Grimes cp = new + strlen(new); 2896a117c345SYaroslav Tykhiy /* 2897a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly. 2898a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off 2899a117c345SYaroslav Tykhiy * by buffer size. 2900a117c345SYaroslav Tykhiy */ 2901a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/') 2902ea022d16SRodney W. Grimes *cp++ = '.'; 2903a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) { 2904a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */ 2905a117c345SYaroslav Tykhiy if (count) 2906ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2907a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local, 2908a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { 2909a117c345SYaroslav Tykhiy *name = count ? new : local; 2910a117c345SYaroslav Tykhiy return (fd); 2911a117c345SYaroslav Tykhiy } 2912ea022d16SRodney W. Grimes } 2913ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2914a117c345SYaroslav Tykhiy return (-1); 2915ea022d16SRodney W. Grimes } 2916ea022d16SRodney W. Grimes 2917ea022d16SRodney W. Grimes /* 2918ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2919ea022d16SRodney W. Grimes */ 2920ea022d16SRodney W. Grimes void 2921e4bc453cSWarner Losh perror_reply(int code, char *string) 2922ea022d16SRodney W. Grimes { 2923ea022d16SRodney W. Grimes 2924ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2925ea022d16SRodney W. Grimes } 2926ea022d16SRodney W. Grimes 2927ea022d16SRodney W. Grimes static char *onefile[] = { 2928ea022d16SRodney W. Grimes "", 2929ea022d16SRodney W. Grimes 0 2930ea022d16SRodney W. Grimes }; 2931ea022d16SRodney W. Grimes 2932ea022d16SRodney W. Grimes void 2933e4bc453cSWarner Losh send_file_list(char *whichf) 2934ea022d16SRodney W. Grimes { 2935ea022d16SRodney W. Grimes struct stat st; 2936ea022d16SRodney W. Grimes DIR *dirp = NULL; 2937ea022d16SRodney W. Grimes struct dirent *dir; 2938ea022d16SRodney W. Grimes FILE *dout = NULL; 2939ea022d16SRodney W. Grimes char **dirlist, *dirname; 2940ea022d16SRodney W. Grimes int simple = 0; 2941ea022d16SRodney W. Grimes int freeglob = 0; 2942ea022d16SRodney W. Grimes glob_t gl; 2943ea022d16SRodney W. Grimes 2944ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 294512da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 2946ea022d16SRodney W. Grimes 2947ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 29486d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 294975dc5f1aSMike Heffner flags |= GLOB_LIMIT; 2950ea022d16SRodney W. Grimes freeglob = 1; 2951ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2952ea022d16SRodney W. Grimes reply(550, "not found"); 2953ea022d16SRodney W. Grimes goto out; 2954ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2955ea022d16SRodney W. Grimes errno = ENOENT; 2956ea022d16SRodney W. Grimes perror_reply(550, whichf); 2957ea022d16SRodney W. Grimes goto out; 2958ea022d16SRodney W. Grimes } 2959ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2960ea022d16SRodney W. Grimes } else { 2961ea022d16SRodney W. Grimes onefile[0] = whichf; 2962ea022d16SRodney W. Grimes dirlist = onefile; 2963ea022d16SRodney W. Grimes simple = 1; 2964ea022d16SRodney W. Grimes } 2965ea022d16SRodney W. Grimes 29669aca17cbSMark Murray while ((dirname = *dirlist++)) { 2967ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2968ea022d16SRodney W. Grimes /* 2969ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2970ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2971ea022d16SRodney W. Grimes */ 2972ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2973ea022d16SRodney W. Grimes transflag == 0) { 2974af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2975ea022d16SRodney W. Grimes goto out; 2976ea022d16SRodney W. Grimes } 2977ea022d16SRodney W. Grimes perror_reply(550, whichf); 2978ea022d16SRodney W. Grimes if (dout != NULL) { 2979ea022d16SRodney W. Grimes (void) fclose(dout); 2980ea022d16SRodney W. Grimes transflag = 0; 2981ea022d16SRodney W. Grimes data = -1; 2982ea022d16SRodney W. Grimes pdata = -1; 2983ea022d16SRodney W. Grimes } 2984ea022d16SRodney W. Grimes goto out; 2985ea022d16SRodney W. Grimes } 2986ea022d16SRodney W. Grimes 2987ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2988ea022d16SRodney W. Grimes if (dout == NULL) { 2989ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2990ea022d16SRodney W. Grimes if (dout == NULL) 2991ea022d16SRodney W. Grimes goto out; 2992ea022d16SRodney W. Grimes transflag++; 2993ea022d16SRodney W. Grimes } 2994ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2995ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2996ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2997ea022d16SRodney W. Grimes continue; 2998ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2999ea022d16SRodney W. Grimes continue; 3000ea022d16SRodney W. Grimes 3001ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 3002ea022d16SRodney W. Grimes continue; 3003ea022d16SRodney W. Grimes 3004ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 3005ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 3006ea022d16SRodney W. Grimes 30074b82fc95SYaroslav Tykhiy if (recvurg) { 30084b82fc95SYaroslav Tykhiy myoob(); 30094b82fc95SYaroslav Tykhiy recvurg = 0; 30104b82fc95SYaroslav Tykhiy transflag = 0; 30114b82fc95SYaroslav Tykhiy goto out; 30124b82fc95SYaroslav Tykhiy } 30134b82fc95SYaroslav Tykhiy 3014ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 3015ea022d16SRodney W. Grimes continue; 3016ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 3017ea022d16SRodney W. Grimes dir->d_namlen == 2) 3018ea022d16SRodney W. Grimes continue; 3019ea022d16SRodney W. Grimes 3020e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 3021e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 3022ea022d16SRodney W. Grimes 3023ea022d16SRodney W. Grimes /* 3024ea022d16SRodney W. Grimes * We have to do a stat to insure it's 3025ea022d16SRodney W. Grimes * not a directory or special file. 3026ea022d16SRodney W. Grimes */ 3027ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 3028ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 3029ea022d16SRodney W. Grimes if (dout == NULL) { 3030ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 3031ea022d16SRodney W. Grimes "w"); 3032ea022d16SRodney W. Grimes if (dout == NULL) 3033ea022d16SRodney W. Grimes goto out; 3034ea022d16SRodney W. Grimes transflag++; 3035ea022d16SRodney W. Grimes } 3036ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 3037ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 3038ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3039ea022d16SRodney W. Grimes else 3040ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 3041ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 3042ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 3043ea022d16SRodney W. Grimes } 3044ea022d16SRodney W. Grimes } 3045ea022d16SRodney W. Grimes (void) closedir(dirp); 3046ea022d16SRodney W. Grimes } 3047ea022d16SRodney W. Grimes 3048ea022d16SRodney W. Grimes if (dout == NULL) 3049ea022d16SRodney W. Grimes reply(550, "No files found."); 3050ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 3051ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 3052ea022d16SRodney W. Grimes else 3053ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 3054ea022d16SRodney W. Grimes 3055ea022d16SRodney W. Grimes transflag = 0; 3056ea022d16SRodney W. Grimes if (dout != NULL) 3057ea022d16SRodney W. Grimes (void) fclose(dout); 3058ea022d16SRodney W. Grimes data = -1; 3059ea022d16SRodney W. Grimes pdata = -1; 3060ea022d16SRodney W. Grimes out: 3061ea022d16SRodney W. Grimes if (freeglob) { 3062ea022d16SRodney W. Grimes freeglob = 0; 3063ea022d16SRodney W. Grimes globfree(&gl); 3064ea022d16SRodney W. Grimes } 3065ea022d16SRodney W. Grimes } 3066ea022d16SRodney W. Grimes 3067cf09a206SDavid Greenman void 3068e4bc453cSWarner Losh reapchild(int signo) 3069cf09a206SDavid Greenman { 3070cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 3071cf09a206SDavid Greenman } 3072cf09a206SDavid Greenman 3073b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 3074ea022d16SRodney W. Grimes /* 3075ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 3076ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 3077ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 3078ea022d16SRodney W. Grimes */ 3079ea022d16SRodney W. Grimes void 3080ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 3081ea022d16SRodney W. Grimes { 3082ea022d16SRodney W. Grimes int i; 3083ea022d16SRodney W. Grimes va_list ap; 3084ea022d16SRodney W. Grimes char *p, *bp, ch; 3085ea022d16SRodney W. Grimes char buf[LINE_MAX]; 3086ea022d16SRodney W. Grimes 3087ea022d16SRodney W. Grimes va_start(ap, fmt); 3088ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 3089ea022d16SRodney W. Grimes 3090ea022d16SRodney W. Grimes /* make ps print our process name */ 3091ea022d16SRodney W. Grimes p = Argv[0]; 3092ea022d16SRodney W. Grimes *p++ = '-'; 3093ea022d16SRodney W. Grimes 3094ea022d16SRodney W. Grimes i = strlen(buf); 3095ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 3096ea022d16SRodney W. Grimes i = LastArgv - p - 2; 3097ea022d16SRodney W. Grimes buf[i] = '\0'; 3098ea022d16SRodney W. Grimes } 3099ea022d16SRodney W. Grimes bp = buf; 3100ea022d16SRodney W. Grimes while (ch = *bp++) 3101ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 3102ea022d16SRodney W. Grimes *p++ = ch; 3103ea022d16SRodney W. Grimes while (p < LastArgv) 3104ea022d16SRodney W. Grimes *p++ = ' '; 3105ea022d16SRodney W. Grimes } 3106b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 31073eb568f2SGuido van Rooij 310861f891a6SPaul Traina static void 3109e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start) 31103eb568f2SGuido van Rooij { 31113eb568f2SGuido van Rooij char buf[1024]; 31123eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 3113158a00b2SJohn Birrell time_t now; 31143eb568f2SGuido van Rooij 31153eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 31163eb568f2SGuido van Rooij time(&now); 3117e4a71114SAndrey A. Chernov snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%qd!%ld\n", 31183eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 3119e4a71114SAndrey A. Chernov path, name, (long long)size, 3120e4a71114SAndrey A. Chernov (long)(now - start + (now == start))); 31213eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 31223eb568f2SGuido van Rooij } 31233eb568f2SGuido van Rooij } 3124e4648f05SYaroslav Tykhiy 3125e4648f05SYaroslav Tykhiy static char * 3126e4648f05SYaroslav Tykhiy doublequote(char *s) 3127e4648f05SYaroslav Tykhiy { 3128e4648f05SYaroslav Tykhiy int n; 3129e4648f05SYaroslav Tykhiy char *p, *s2; 3130e4648f05SYaroslav Tykhiy 3131e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++) 3132e4648f05SYaroslav Tykhiy if (*p == '"') 3133e4648f05SYaroslav Tykhiy n++; 3134e4648f05SYaroslav Tykhiy 3135e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL) 3136e4648f05SYaroslav Tykhiy return (NULL); 3137e4648f05SYaroslav Tykhiy 3138e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) { 3139e4648f05SYaroslav Tykhiy if ((*p = *s) == '"') 3140e4648f05SYaroslav Tykhiy *(++p) = '"'; 3141e4648f05SYaroslav Tykhiy } 3142e4648f05SYaroslav Tykhiy *p = '\0'; 3143e4648f05SYaroslav Tykhiy 3144e4648f05SYaroslav Tykhiy return (s2); 3145e4648f05SYaroslav Tykhiy } 3146