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> 81ea022d16SRodney W. Grimes #include <setjmp.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 942c60c54cSPaul Traina #ifdef SKEY 952c60c54cSPaul Traina #include <skey.h> 962c60c54cSPaul Traina #endif 972c60c54cSPaul Traina 986c9134c0SMark Murray #if !defined(NOPAM) 996c9134c0SMark Murray #include <security/pam_appl.h> 1006c9134c0SMark Murray #endif 1016c9134c0SMark Murray 102ea022d16SRodney W. Grimes #include "pathnames.h" 103ea022d16SRodney W. Grimes #include "extern.h" 104ea022d16SRodney W. Grimes 105ea022d16SRodney W. Grimes #if __STDC__ 106ea022d16SRodney W. Grimes #include <stdarg.h> 107ea022d16SRodney W. Grimes #else 108ea022d16SRodney W. Grimes #include <varargs.h> 109ea022d16SRodney W. Grimes #endif 110ea022d16SRodney W. Grimes 111af85d782SDavid Nugent static char version[] = "Version 6.00LS"; 112af85d782SDavid Nugent #undef main 113ea022d16SRodney W. Grimes 1144dd8b5abSYoshinobu Inoue /* wrapper for KAME-special getnameinfo() */ 1154dd8b5abSYoshinobu Inoue #ifndef NI_WITHSCOPEID 1164dd8b5abSYoshinobu Inoue #define NI_WITHSCOPEID 0 1174dd8b5abSYoshinobu Inoue #endif 1184dd8b5abSYoshinobu Inoue 119ea022d16SRodney W. Grimes extern off_t restart_point; 120ea022d16SRodney W. Grimes extern char cbuf[]; 121ea022d16SRodney W. Grimes 1224dd8b5abSYoshinobu Inoue union sockunion server_addr; 1234dd8b5abSYoshinobu Inoue union sockunion ctrl_addr; 1244dd8b5abSYoshinobu Inoue union sockunion data_source; 1254dd8b5abSYoshinobu Inoue union sockunion data_dest; 1264dd8b5abSYoshinobu Inoue union sockunion his_addr; 1274dd8b5abSYoshinobu Inoue union sockunion pasv_addr; 128ea022d16SRodney W. Grimes 129cf09a206SDavid Greenman int daemon_mode; 130ea022d16SRodney W. Grimes int data; 131ea022d16SRodney W. Grimes jmp_buf errcatch, urgcatch; 132ea022d16SRodney W. Grimes int logged_in; 133ea022d16SRodney W. Grimes struct passwd *pw; 134ea022d16SRodney W. Grimes int debug; 135ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */ 136ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ 137ea022d16SRodney W. Grimes int logging; 1384c450ad7SPaul Traina int restricted_data_ports = 1; 139a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */ 1405a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */ 141ea022d16SRodney W. Grimes int guest; 142a5a4544eSPaul Traina int dochroot; 1433eb568f2SGuido van Rooij int stats; 1443eb568f2SGuido van Rooij int statfd = -1; 145ea022d16SRodney W. Grimes int type; 146ea022d16SRodney W. Grimes int form; 147ea022d16SRodney W. Grimes int stru; /* avoid C keyword */ 148ea022d16SRodney W. Grimes int mode; 149ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */ 150ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */ 151ea022d16SRodney W. Grimes sig_atomic_t transflag; 152ea022d16SRodney W. Grimes off_t file_size; 153ea022d16SRodney W. Grimes off_t byte_count; 154ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0 155ea022d16SRodney W. Grimes #undef CMASK 156ea022d16SRodney W. Grimes #define CMASK 027 157ea022d16SRodney W. Grimes #endif 158ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */ 159ea022d16SRodney W. Grimes char tmpline[7]; 160ea4e54b9SDavid Nugent char *hostname; 1610512556aSDavid Nugent #ifdef VIRTUAL_HOSTING 162ea4e54b9SDavid Nugent char *ftpuser; 163ea4e54b9SDavid Nugent 1644dd8b5abSYoshinobu Inoue int epsvall = 0; 1654dd8b5abSYoshinobu Inoue 166ea4e54b9SDavid Nugent static struct ftphost { 167ea4e54b9SDavid Nugent struct ftphost *next; 168f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo; 169ea4e54b9SDavid Nugent char *hostname; 170ea4e54b9SDavid Nugent char *anonuser; 171ea4e54b9SDavid Nugent char *statfile; 172ea4e54b9SDavid Nugent char *welcome; 173ea4e54b9SDavid Nugent char *loginmsg; 174ea4e54b9SDavid Nugent } *thishost, *firsthost; 175ea4e54b9SDavid Nugent 176ea4e54b9SDavid Nugent #endif 1779e9a43bdSBrian Somers char remotehost[MAXHOSTNAMELEN]; 1783eb568f2SGuido van Rooij char *ident = NULL; 179a5a4544eSPaul Traina 180a5a4544eSPaul Traina static char ttyline[20]; 181a5a4544eSPaul Traina char *tty = ttyline; /* for klogin */ 182a5a4544eSPaul Traina 1836c9134c0SMark Murray #if !defined(NOPAM) 1846c9134c0SMark Murray static int auth_pam __P((struct passwd**, const char*)); 1859aca17cbSMark Murray #endif 1869aca17cbSMark Murray 187105a3c98SJulian Elischer char *pid_file = NULL; 188105a3c98SJulian Elischer 189ea022d16SRodney W. Grimes /* 190ea022d16SRodney W. Grimes * Timeout intervals for retrying connections 191ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This 192ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP... 193ea022d16SRodney W. Grimes */ 194ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */ 195ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */ 196ea022d16SRodney W. Grimes 197ea022d16SRodney W. Grimes int swaitmax = SWAITMAX; 198ea022d16SRodney W. Grimes int swaitint = SWAITINT; 199ea022d16SRodney W. Grimes 200ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 201b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 202ea022d16SRodney W. Grimes char **Argv = NULL; /* pointer to argument vector */ 203ea022d16SRodney W. Grimes char *LastArgv = NULL; /* end of argv */ 204b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 205ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */ 206ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 207ea022d16SRodney W. Grimes 208726040deSGuido van Rooij #ifdef SKEY 209726040deSGuido van Rooij int pwok = 0; 210726040deSGuido van Rooij #endif 211726040deSGuido van Rooij 212ea022d16SRodney W. Grimes #define LOGCMD(cmd, file) \ 213ea022d16SRodney W. Grimes if (logging > 1) \ 214ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 215ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); 216ea022d16SRodney W. Grimes #define LOGCMD2(cmd, file1, file2) \ 217ea022d16SRodney W. Grimes if (logging > 1) \ 218ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s %s%s", cmd, \ 219ea022d16SRodney W. Grimes *(file1) == '/' ? "" : curdir(), file1, \ 220ea022d16SRodney W. Grimes *(file2) == '/' ? "" : curdir(), file2); 221ea022d16SRodney W. Grimes #define LOGBYTES(cmd, file, cnt) \ 222ea022d16SRodney W. Grimes if (logging > 1) { \ 223ea022d16SRodney W. Grimes if (cnt == (off_t)-1) \ 224ea022d16SRodney W. Grimes syslog(LOG_INFO,"%s %s%s", cmd, \ 225ea022d16SRodney W. Grimes *(file) == '/' ? "" : curdir(), file); \ 226ea022d16SRodney W. Grimes else \ 227ea022d16SRodney W. Grimes syslog(LOG_INFO, "%s %s%s = %qd bytes", \ 228ea022d16SRodney W. Grimes cmd, (*(file) == '/') ? "" : curdir(), file, cnt); \ 229ea022d16SRodney W. Grimes } 230ea022d16SRodney W. Grimes 231ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 232ea4e54b9SDavid Nugent static void inithosts __P((void)); 2334dd8b5abSYoshinobu Inoue static void selecthost __P((union sockunion *)); 234ea4e54b9SDavid Nugent #endif 235ea022d16SRodney W. Grimes static void ack __P((char *)); 236ea022d16SRodney W. Grimes static void myoob __P((int)); 2377edcb936SSteve Price static int checkuser __P((char *, char *, int)); 238ea022d16SRodney W. Grimes static FILE *dataconn __P((char *, off_t, char *)); 2394dd8b5abSYoshinobu Inoue static void dolog __P((struct sockaddr *)); 240ea022d16SRodney W. Grimes static char *curdir __P((void)); 241ea022d16SRodney W. Grimes static void end_login __P((void)); 242ea022d16SRodney W. Grimes static FILE *getdatasock __P((char *)); 243ea022d16SRodney W. Grimes static char *gunique __P((char *)); 244ea022d16SRodney W. Grimes static void lostconn __P((int)); 245ea022d16SRodney W. Grimes static int receive_data __P((FILE *, FILE *)); 2469fc5823aSGarrett Wollman static void send_data __P((FILE *, FILE *, off_t, off_t, int)); 247ea022d16SRodney W. Grimes static struct passwd * 248ea022d16SRodney W. Grimes sgetpwnam __P((char *)); 249ea022d16SRodney W. Grimes static char *sgetsave __P((char *)); 250cf09a206SDavid Greenman static void reapchild __P((int)); 25161f891a6SPaul Traina static void logxfer __P((char *, long, long)); 252ea022d16SRodney W. Grimes 253ea022d16SRodney W. Grimes static char * 254ea022d16SRodney W. Grimes curdir() 255ea022d16SRodney W. Grimes { 256ea022d16SRodney W. Grimes static char path[MAXPATHLEN+1+1]; /* path + '/' + '\0' */ 257ea022d16SRodney W. Grimes 258ea022d16SRodney W. Grimes if (getcwd(path, sizeof(path)-2) == NULL) 259ea022d16SRodney W. Grimes return (""); 260ea022d16SRodney W. Grimes if (path[1] != '\0') /* special case for root dir. */ 261ea022d16SRodney W. Grimes strcat(path, "/"); 262ea022d16SRodney W. Grimes /* For guest account, skip / since it's chrooted */ 263ea022d16SRodney W. Grimes return (guest ? path+1 : path); 264ea022d16SRodney W. Grimes } 265ea022d16SRodney W. Grimes 266ea022d16SRodney W. Grimes int 267ea022d16SRodney W. Grimes main(argc, argv, envp) 268ea022d16SRodney W. Grimes int argc; 269ea022d16SRodney W. Grimes char *argv[]; 270ea022d16SRodney W. Grimes char **envp; 271ea022d16SRodney W. Grimes { 272ea022d16SRodney W. Grimes int addrlen, ch, on = 1, tos; 273ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 274ea022d16SRodney W. Grimes FILE *fd; 2754dd8b5abSYoshinobu Inoue int error; 2764dd8b5abSYoshinobu Inoue char *bindname = NULL; 2774dd8b5abSYoshinobu Inoue int family = AF_UNSPEC; 2784dd8b5abSYoshinobu Inoue int enable_v4 = 0; 279ea022d16SRodney W. Grimes 28034d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */ 28134d1ba5cSAndrey A. Chernov 282b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 283ea022d16SRodney W. Grimes /* 284ea022d16SRodney W. Grimes * Save start and extent of argv for setproctitle. 285ea022d16SRodney W. Grimes */ 286ea022d16SRodney W. Grimes Argv = argv; 287ea022d16SRodney W. Grimes while (*envp) 288ea022d16SRodney W. Grimes envp++; 289ea022d16SRodney W. Grimes LastArgv = envp[-1] + strlen(envp[-1]); 290b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 291ea022d16SRodney W. Grimes 2923eb568f2SGuido van Rooij 2934dd8b5abSYoshinobu Inoue while ((ch = getopt(argc, argv, "AdlDSURt:T:u:va:p:46")) != -1) { 294ea022d16SRodney W. Grimes switch (ch) { 295cf09a206SDavid Greenman case 'D': 296cf09a206SDavid Greenman daemon_mode++; 297cf09a206SDavid Greenman break; 298cf09a206SDavid Greenman 299ea022d16SRodney W. Grimes case 'd': 300a5a4544eSPaul Traina debug++; 301ea022d16SRodney W. Grimes break; 302ea022d16SRodney W. Grimes 303ea022d16SRodney W. Grimes case 'l': 304ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */ 305ea022d16SRodney W. Grimes break; 306ea022d16SRodney W. Grimes 307a5a4544eSPaul Traina case 'R': 308a5a4544eSPaul Traina paranoid = 0; 309a5a4544eSPaul Traina break; 310a5a4544eSPaul Traina 311a5a4544eSPaul Traina case 'S': 312a5a4544eSPaul Traina stats++; 313a5a4544eSPaul Traina break; 314a5a4544eSPaul Traina 315a5a4544eSPaul Traina case 'T': 316a5a4544eSPaul Traina maxtimeout = atoi(optarg); 317a5a4544eSPaul Traina if (timeout > maxtimeout) 318a5a4544eSPaul Traina timeout = maxtimeout; 3194c450ad7SPaul Traina break; 3204c450ad7SPaul Traina 321ea022d16SRodney W. Grimes case 't': 322ea022d16SRodney W. Grimes timeout = atoi(optarg); 323ea022d16SRodney W. Grimes if (maxtimeout < timeout) 324ea022d16SRodney W. Grimes maxtimeout = timeout; 325ea022d16SRodney W. Grimes break; 326a5a4544eSPaul Traina 327a5a4544eSPaul Traina case 'U': 328a5a4544eSPaul Traina restricted_data_ports = 0; 329ea022d16SRodney W. Grimes break; 330ea022d16SRodney W. Grimes 331105a3c98SJulian Elischer case 'a': 3324dd8b5abSYoshinobu Inoue bindname = optarg; 333105a3c98SJulian Elischer break; 334105a3c98SJulian Elischer 335105a3c98SJulian Elischer case 'p': 336105a3c98SJulian Elischer pid_file = optarg; 337105a3c98SJulian Elischer break; 338105a3c98SJulian Elischer 339ea022d16SRodney W. Grimes case 'u': 340ea022d16SRodney W. Grimes { 341ea022d16SRodney W. Grimes long val = 0; 342ea022d16SRodney W. Grimes 343ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8); 344ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0) 345ea022d16SRodney W. Grimes warnx("bad value for -u"); 346ea022d16SRodney W. Grimes else 347ea022d16SRodney W. Grimes defumask = val; 348ea022d16SRodney W. Grimes break; 349ea022d16SRodney W. Grimes } 3505a392aecSTorsten Blum case 'A': 3515a392aecSTorsten Blum anon_only = 1; 3525a392aecSTorsten Blum break; 353ea022d16SRodney W. Grimes 354ea022d16SRodney W. Grimes case 'v': 355ea022d16SRodney W. Grimes debug = 1; 356ea022d16SRodney W. Grimes break; 357ea022d16SRodney W. Grimes 3584dd8b5abSYoshinobu Inoue case '4': 3594dd8b5abSYoshinobu Inoue enable_v4 = 1; 3604dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) 3614dd8b5abSYoshinobu Inoue family = AF_INET; 3624dd8b5abSYoshinobu Inoue break; 3634dd8b5abSYoshinobu Inoue 3644dd8b5abSYoshinobu Inoue case '6': 3654dd8b5abSYoshinobu Inoue family = AF_INET6; 3664dd8b5abSYoshinobu Inoue break; 3674dd8b5abSYoshinobu Inoue 368ea022d16SRodney W. Grimes default: 369ea022d16SRodney W. Grimes warnx("unknown flag -%c ignored", optopt); 370ea022d16SRodney W. Grimes break; 371ea022d16SRodney W. Grimes } 372ea022d16SRodney W. Grimes } 373cf09a206SDavid Greenman 374ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 375ea4e54b9SDavid Nugent inithosts(); 376ea4e54b9SDavid Nugent #endif 377ea022d16SRodney W. Grimes (void) freopen(_PATH_DEVNULL, "w", stderr); 378cf09a206SDavid Greenman 379cf09a206SDavid Greenman /* 380cf09a206SDavid Greenman * LOG_NDELAY sets up the logging connection immediately, 381cf09a206SDavid Greenman * necessary for anonymous ftp's that chroot and can't do it later. 382cf09a206SDavid Greenman */ 383cf09a206SDavid Greenman openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); 384cf09a206SDavid Greenman 385cf09a206SDavid Greenman if (daemon_mode) { 386cf09a206SDavid Greenman int ctl_sock, fd; 3874dd8b5abSYoshinobu Inoue struct addrinfo hints, *res; 388cf09a206SDavid Greenman 389cf09a206SDavid Greenman /* 390cf09a206SDavid Greenman * Detach from parent. 391cf09a206SDavid Greenman */ 392cf09a206SDavid Greenman if (daemon(1, 1) < 0) { 393cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon"); 394cf09a206SDavid Greenman exit(1); 395cf09a206SDavid Greenman } 396cf09a206SDavid Greenman (void) signal(SIGCHLD, reapchild); 3974dd8b5abSYoshinobu Inoue /* init bind_sa */ 3984dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 3994dd8b5abSYoshinobu Inoue 4004dd8b5abSYoshinobu Inoue hints.ai_family = family == AF_UNSPEC ? AF_INET : family; 4014dd8b5abSYoshinobu Inoue hints.ai_socktype = SOCK_STREAM; 4024dd8b5abSYoshinobu Inoue hints.ai_protocol = 0; 4034dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 4044dd8b5abSYoshinobu Inoue error = getaddrinfo(bindname, "ftp", &hints, &res); 4054dd8b5abSYoshinobu Inoue if (error) { 4064dd8b5abSYoshinobu Inoue if (family == AF_UNSPEC) { 4074dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 4084dd8b5abSYoshinobu Inoue error = getaddrinfo(bindname, "ftp", &hints, 4094dd8b5abSYoshinobu Inoue &res); 4104dd8b5abSYoshinobu Inoue } 4114dd8b5abSYoshinobu Inoue } 4124dd8b5abSYoshinobu Inoue if (error) { 4134dd8b5abSYoshinobu Inoue syslog(LOG_ERR, gai_strerror(error)); 4144dd8b5abSYoshinobu Inoue if (error == EAI_SYSTEM) 4154dd8b5abSYoshinobu Inoue syslog(LOG_ERR, strerror(errno)); 4164dd8b5abSYoshinobu Inoue exit(1); 4174dd8b5abSYoshinobu Inoue } 4184dd8b5abSYoshinobu Inoue if (res->ai_addr == NULL) { 4194dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "-a %s: getaddrinfo failed", hostname); 420cf09a206SDavid Greenman exit(1); 4212310b8c6SRuslan Ermilov } else 4222310b8c6SRuslan Ermilov family = res->ai_addr->sa_family; 423cf09a206SDavid Greenman /* 424cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start 425cf09a206SDavid Greenman * listening. 426cf09a206SDavid Greenman */ 4274dd8b5abSYoshinobu Inoue ctl_sock = socket(family, SOCK_STREAM, 0); 428cf09a206SDavid Greenman if (ctl_sock < 0) { 429cf09a206SDavid Greenman syslog(LOG_ERR, "control socket: %m"); 430cf09a206SDavid Greenman exit(1); 431cf09a206SDavid Greenman } 432cf09a206SDavid Greenman if (setsockopt(ctl_sock, SOL_SOCKET, SO_REUSEADDR, 433cf09a206SDavid Greenman (char *)&on, sizeof(on)) < 0) 4344dd8b5abSYoshinobu Inoue syslog(LOG_ERR, "control setsockopt: %m"); 4354dd8b5abSYoshinobu Inoue #ifdef IPV6_BINDV6ONLY 4364dd8b5abSYoshinobu Inoue if (family == AF_INET6 && enable_v4 == 0) { 4374dd8b5abSYoshinobu Inoue if (setsockopt(ctl_sock, IPPROTO_IPV6, IPV6_BINDV6ONLY, 4384dd8b5abSYoshinobu Inoue (char *)&on, sizeof (on)) < 0) 4394dd8b5abSYoshinobu Inoue syslog(LOG_ERR, 4404dd8b5abSYoshinobu Inoue "control setsockopt(IPV6_BINDV6ONLY): %m"); 4414dd8b5abSYoshinobu Inoue } 4424dd8b5abSYoshinobu Inoue #endif /* IPV6_BINDV6ONLY */ 4434dd8b5abSYoshinobu Inoue memcpy(&server_addr, res->ai_addr, res->ai_addr->sa_len); 4444dd8b5abSYoshinobu Inoue if (bind(ctl_sock, (struct sockaddr *)&server_addr, 4454dd8b5abSYoshinobu Inoue server_addr.su_len) < 0) { 446cf09a206SDavid Greenman syslog(LOG_ERR, "control bind: %m"); 447cf09a206SDavid Greenman exit(1); 448cf09a206SDavid Greenman } 449cf09a206SDavid Greenman if (listen(ctl_sock, 32) < 0) { 450cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m"); 451cf09a206SDavid Greenman exit(1); 452cf09a206SDavid Greenman } 453cf09a206SDavid Greenman /* 454105a3c98SJulian Elischer * Atomically write process ID 455105a3c98SJulian Elischer */ 456105a3c98SJulian Elischer if (pid_file) 457105a3c98SJulian Elischer { 458105a3c98SJulian Elischer int fd; 459105a3c98SJulian Elischer char buf[20]; 460105a3c98SJulian Elischer 461105a3c98SJulian Elischer fd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC 462105a3c98SJulian Elischer | O_NONBLOCK | O_EXLOCK, 0644); 46385966371SWarner Losh if (fd < 0) { 464105a3c98SJulian Elischer if (errno == EAGAIN) 465105a3c98SJulian Elischer errx(1, "%s: file locked", pid_file); 466105a3c98SJulian Elischer else 467105a3c98SJulian Elischer err(1, "%s", pid_file); 46885966371SWarner Losh } 469105a3c98SJulian Elischer snprintf(buf, sizeof(buf), 470105a3c98SJulian Elischer "%lu\n", (unsigned long) getpid()); 471105a3c98SJulian Elischer if (write(fd, buf, strlen(buf)) < 0) 472105a3c98SJulian Elischer err(1, "%s: write", pid_file); 473105a3c98SJulian Elischer /* Leave the pid file open and locked */ 474105a3c98SJulian Elischer } 475105a3c98SJulian Elischer /* 476cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off 477cf09a206SDavid Greenman * children to handle them. 478cf09a206SDavid Greenman */ 479cf09a206SDavid Greenman while (1) { 4804dd8b5abSYoshinobu Inoue addrlen = server_addr.su_len; 481cf09a206SDavid Greenman fd = accept(ctl_sock, (struct sockaddr *)&his_addr, &addrlen); 482cf09a206SDavid Greenman if (fork() == 0) { 483cf09a206SDavid Greenman /* child */ 484cf09a206SDavid Greenman (void) dup2(fd, 0); 485cf09a206SDavid Greenman (void) dup2(fd, 1); 486cf09a206SDavid Greenman close(ctl_sock); 487cf09a206SDavid Greenman break; 488cf09a206SDavid Greenman } 489cf09a206SDavid Greenman close(fd); 490cf09a206SDavid Greenman } 491cf09a206SDavid Greenman } else { 492cf09a206SDavid Greenman addrlen = sizeof(his_addr); 493cf09a206SDavid Greenman if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { 494cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); 495cf09a206SDavid Greenman exit(1); 496cf09a206SDavid Greenman } 497cf09a206SDavid Greenman } 498cf09a206SDavid Greenman 499ea022d16SRodney W. Grimes (void) signal(SIGCHLD, SIG_IGN); 500cf09a206SDavid Greenman (void) signal(SIGPIPE, lostconn); 501158a00b2SJohn Birrell if (signal(SIGURG, myoob) == SIG_ERR) 502ea022d16SRodney W. Grimes syslog(LOG_ERR, "signal: %m"); 503ea022d16SRodney W. Grimes 504cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr); 505cf09a206SDavid Greenman if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { 506cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); 507cf09a206SDavid Greenman exit(1); 508cf09a206SDavid Greenman } 509ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 510ea4e54b9SDavid Nugent /* select our identity from virtual host table */ 5114dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr); 512ea4e54b9SDavid Nugent #endif 513cf09a206SDavid Greenman #ifdef IP_TOS 5144dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) 5154dd8b5abSYoshinobu Inoue { 516cf09a206SDavid Greenman tos = IPTOS_LOWDELAY; 517cf09a206SDavid Greenman if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 518cf09a206SDavid Greenman syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 5194dd8b5abSYoshinobu Inoue } 520cf09a206SDavid Greenman #endif 521dadb9fb3SDavid Greenman /* 522dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait 523dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply. 524dadb9fb3SDavid Greenman */ 525dadb9fb3SDavid Greenman if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) 526dadb9fb3SDavid Greenman syslog(LOG_WARNING, "control setsockopt TCP_NODELAY: %m"); 527dadb9fb3SDavid Greenman 5284dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); 529cf09a206SDavid Greenman 530a5a4544eSPaul Traina /* set this here so klogin can use it... */ 531e760ef2cSWarner Losh (void)snprintf(ttyline, sizeof(ttyline), "ftp%d", getpid()); 532a5a4544eSPaul Traina 533ea022d16SRodney W. Grimes /* Try to handle urgent data inline */ 534ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE 535ea022d16SRodney W. Grimes if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) < 0) 536ea022d16SRodney W. Grimes syslog(LOG_ERR, "setsockopt: %m"); 537ea022d16SRodney W. Grimes #endif 538ea022d16SRodney W. Grimes 539ea022d16SRodney W. Grimes #ifdef F_SETOWN 540ea022d16SRodney W. Grimes if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) 541ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m"); 542ea022d16SRodney W. Grimes #endif 5434dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr); 544ea022d16SRodney W. Grimes /* 545ea022d16SRodney W. Grimes * Set up default state 546ea022d16SRodney W. Grimes */ 547ea022d16SRodney W. Grimes data = -1; 548ea022d16SRodney W. Grimes type = TYPE_A; 549ea022d16SRodney W. Grimes form = FORM_N; 550ea022d16SRodney W. Grimes stru = STRU_F; 551ea022d16SRodney W. Grimes mode = MODE_S; 552ea022d16SRodney W. Grimes tmpline[0] = '\0'; 553ea022d16SRodney W. Grimes 554ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */ 555ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { 556ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 557ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 558ea022d16SRodney W. Grimes *cp = '\0'; 559ea022d16SRodney W. Grimes lreply(530, "%s", line); 560ea022d16SRodney W. Grimes } 561ea022d16SRodney W. Grimes (void) fflush(stdout); 562ea022d16SRodney W. Grimes (void) fclose(fd); 563ea022d16SRodney W. Grimes reply(530, "System not available."); 564ea022d16SRodney W. Grimes exit(0); 565ea022d16SRodney W. Grimes } 566ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 567ea4e54b9SDavid Nugent if ((fd = fopen(thishost->welcome, "r")) != NULL) { 568ea4e54b9SDavid Nugent #else 569ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPWELCOME, "r")) != NULL) { 570ea4e54b9SDavid Nugent #endif 571ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 572ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 573ea022d16SRodney W. Grimes *cp = '\0'; 574ea022d16SRodney W. Grimes lreply(220, "%s", line); 575ea022d16SRodney W. Grimes } 576ea022d16SRodney W. Grimes (void) fflush(stdout); 577ea022d16SRodney W. Grimes (void) fclose(fd); 578ea022d16SRodney W. Grimes /* reply(220,) must follow */ 579ea022d16SRodney W. Grimes } 580ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING 5810512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) 5820512556aSDavid Nugent fatal("Ran out of memory."); 5839e9a43bdSBrian Somers (void) gethostname(hostname, MAXHOSTNAMELEN - 1); 5849e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0'; 585ea4e54b9SDavid Nugent #endif 586ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version); 587ea022d16SRodney W. Grimes (void) setjmp(errcatch); 588ea022d16SRodney W. Grimes for (;;) 589ea022d16SRodney W. Grimes (void) yyparse(); 590ea022d16SRodney W. Grimes /* NOTREACHED */ 591ea022d16SRodney W. Grimes } 592ea022d16SRodney W. Grimes 593ea022d16SRodney W. Grimes static void 594ea022d16SRodney W. Grimes lostconn(signo) 595ea022d16SRodney W. Grimes int signo; 596ea022d16SRodney W. Grimes { 597ea022d16SRodney W. Grimes 598ea022d16SRodney W. Grimes if (debug) 599ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection"); 600e02897faSPhilippe Charnier dologout(1); 601ea022d16SRodney W. Grimes } 602ea022d16SRodney W. Grimes 603ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 604ea4e54b9SDavid Nugent /* 605ea4e54b9SDavid Nugent * read in virtual host tables (if they exist) 606ea4e54b9SDavid Nugent */ 607ea4e54b9SDavid Nugent 608ea4e54b9SDavid Nugent static void 609ea4e54b9SDavid Nugent inithosts() 610ea4e54b9SDavid Nugent { 611ea4e54b9SDavid Nugent FILE *fp; 612ea4e54b9SDavid Nugent char *cp; 613ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp; 614ea4e54b9SDavid Nugent char line[1024]; 6154dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai; 616ea4e54b9SDavid Nugent 617ea4e54b9SDavid Nugent /* 618ea4e54b9SDavid Nugent * Fill in the default host information 619ea4e54b9SDavid Nugent */ 620ea4e54b9SDavid Nugent if (gethostname(line, sizeof(line)) < 0) 621ea4e54b9SDavid Nugent line[0] = '\0'; 622ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL || 623ea4e54b9SDavid Nugent (hrp->hostname = strdup(line)) == NULL) 624ea4e54b9SDavid Nugent fatal("Ran out of memory."); 625f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL; 6264dd8b5abSYoshinobu Inoue 6274dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints)); 6284dd8b5abSYoshinobu Inoue hints.ai_flags = AI_CANONNAME; 6294dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 6304dd8b5abSYoshinobu Inoue getaddrinfo(hrp->hostname, NULL, &hints, &res); 6314dd8b5abSYoshinobu Inoue if (res) 632f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 633ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 634ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 635ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 636ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 637ea4e54b9SDavid Nugent hrp->next = NULL; 638ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp; 639ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { 640b535a9bfSDavid Nugent int addrsize, error, gothost; 6414dd8b5abSYoshinobu Inoue void *addr; 6424dd8b5abSYoshinobu Inoue struct hostent *hp; 6434dd8b5abSYoshinobu Inoue 644ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL) { 6454dd8b5abSYoshinobu Inoue int i, hp_error; 646ea4e54b9SDavid Nugent 647ea4e54b9SDavid Nugent if ((cp = strchr(line, '\n')) == NULL) { 648ea4e54b9SDavid Nugent /* ignore long lines */ 649ea4e54b9SDavid Nugent while (fgets(line, sizeof(line), fp) != NULL && 650ea4e54b9SDavid Nugent strchr(line, '\n') == NULL) 651ea4e54b9SDavid Nugent ; 652ea4e54b9SDavid Nugent continue; 653ea4e54b9SDavid Nugent } 654ea4e54b9SDavid Nugent *cp = '\0'; 655ea4e54b9SDavid Nugent cp = strtok(line, " \t"); 656ea4e54b9SDavid Nugent /* skip comments and empty lines */ 657ea4e54b9SDavid Nugent if (cp == NULL || line[0] == '#') 658ea4e54b9SDavid Nugent continue; 6594dd8b5abSYoshinobu Inoue 6604dd8b5abSYoshinobu Inoue hints.ai_flags = 0; 6614dd8b5abSYoshinobu Inoue hints.ai_family = AF_UNSPEC; 6624dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE; 6634dd8b5abSYoshinobu Inoue error = getaddrinfo(cp, NULL, &hints, &res); 6644dd8b5abSYoshinobu Inoue if (error != NULL) 665ea4e54b9SDavid Nugent continue; 6664dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL; 667b535a9bfSDavid Nugent ai = ai->ai_next) { 6684dd8b5abSYoshinobu Inoue 669b535a9bfSDavid Nugent gothost = 0; 670ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { 671f38c6cadSYoshinobu Inoue struct addrinfo *hi; 672f38c6cadSYoshinobu Inoue 673f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL; 674f38c6cadSYoshinobu Inoue hi = hi->ai_next) 675f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen && 676f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr, 6774dd8b5abSYoshinobu Inoue ai->ai_addr, 678b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) { 679b535a9bfSDavid Nugent gothost++; 680b535a9bfSDavid Nugent break; 681b535a9bfSDavid Nugent } 682b535a9bfSDavid Nugent if (gothost) 683ea4e54b9SDavid Nugent break; 684ea4e54b9SDavid Nugent } 685ea4e54b9SDavid Nugent if (hrp == NULL) { 686ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL) 687ea4e54b9SDavid Nugent continue; 688ea4e54b9SDavid Nugent /* defaults */ 689ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE; 690ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME; 691ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG; 692ea4e54b9SDavid Nugent hrp->anonuser = "ftp"; 693ea4e54b9SDavid Nugent hrp->next = NULL; 694ea4e54b9SDavid Nugent lhrp->next = hrp; 695ea4e54b9SDavid Nugent lhrp = hrp; 696ea4e54b9SDavid Nugent } 697f38c6cadSYoshinobu Inoue hrp->hostinfo = res; 698f38c6cadSYoshinobu Inoue 699ea4e54b9SDavid Nugent /* 700ea4e54b9SDavid Nugent * determine hostname to use. 7014dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias 702ea4e54b9SDavid Nugent * otherwise fallback to primary hostname 703ea4e54b9SDavid Nugent */ 7044dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */ 705f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) { 7064dd8b5abSYoshinobu Inoue case AF_INET: 707f38c6cadSYoshinobu Inoue addr = &((struct sockaddr_in *)&hrp->hostinfo->ai_addr)->sin_addr; 7084dd8b5abSYoshinobu Inoue addrsize = sizeof(struct sockaddr_in); 7094dd8b5abSYoshinobu Inoue break; 7104dd8b5abSYoshinobu Inoue case AF_INET6: 711f38c6cadSYoshinobu Inoue addr = &((struct sockaddr_in6 *)&hrp->hostinfo->ai_addr)->sin6_addr; 7124dd8b5abSYoshinobu Inoue addrsize = sizeof(struct sockaddr_in6); 7134dd8b5abSYoshinobu Inoue break; 7144dd8b5abSYoshinobu Inoue default: 7154dd8b5abSYoshinobu Inoue /* should not reach here */ 716f38c6cadSYoshinobu Inoue if (hrp->hostinfo != NULL) 717f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo); 7184dd8b5abSYoshinobu Inoue free(hrp); 7194dd8b5abSYoshinobu Inoue continue; 7204dd8b5abSYoshinobu Inoue /* NOTREACHED */ 7214dd8b5abSYoshinobu Inoue } 7224dd8b5abSYoshinobu Inoue if ((hp = getipnodebyaddr((char*)addr, addrsize, 723f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family, 7244dd8b5abSYoshinobu Inoue &hp_error)) != NULL) { 725ea4e54b9SDavid Nugent if (strcmp(cp, hp->h_name) != 0) { 726ea4e54b9SDavid Nugent if (hp->h_aliases == NULL) 727ea4e54b9SDavid Nugent cp = hp->h_name; 728ea4e54b9SDavid Nugent else { 729ea4e54b9SDavid Nugent i = 0; 730ea4e54b9SDavid Nugent while (hp->h_aliases[i] && 731ea4e54b9SDavid Nugent strcmp(cp, hp->h_aliases[i]) != 0) 732ea4e54b9SDavid Nugent ++i; 733ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL) 734ea4e54b9SDavid Nugent cp = hp->h_name; 735ea4e54b9SDavid Nugent } 736ea4e54b9SDavid Nugent } 737ea4e54b9SDavid Nugent } 738ea4e54b9SDavid Nugent hrp->hostname = strdup(cp); 7394dd8b5abSYoshinobu Inoue freehostent(hp); 740ea4e54b9SDavid Nugent /* ok, now we now peel off the rest */ 741ea4e54b9SDavid Nugent i = 0; 742ea4e54b9SDavid Nugent while (i < 4 && (cp = strtok(NULL, " \t")) != NULL) { 743ea4e54b9SDavid Nugent if (*cp != '-' && (cp = strdup(cp)) != NULL) { 744ea4e54b9SDavid Nugent switch (i) { 745ea4e54b9SDavid Nugent case 0: /* anon user permissions */ 746ea4e54b9SDavid Nugent hrp->anonuser = cp; 747ea4e54b9SDavid Nugent break; 748ea4e54b9SDavid Nugent case 1: /* statistics file */ 749ea4e54b9SDavid Nugent hrp->statfile = cp; 750ea4e54b9SDavid Nugent break; 751ea4e54b9SDavid Nugent case 2: /* welcome message */ 752ea4e54b9SDavid Nugent hrp->welcome = cp; 753ea4e54b9SDavid Nugent break; 754ea4e54b9SDavid Nugent case 3: /* login message */ 755ea4e54b9SDavid Nugent hrp->loginmsg = cp; 756ea4e54b9SDavid Nugent break; 757ea4e54b9SDavid Nugent } 758ea4e54b9SDavid Nugent } 759ea4e54b9SDavid Nugent ++i; 760ea4e54b9SDavid Nugent } 7614dd8b5abSYoshinobu Inoue /* XXX: re-initialization for getaddrinfo() loop */ 7624dd8b5abSYoshinobu Inoue cp = strtok(line, " \t"); 7634dd8b5abSYoshinobu Inoue } 764ea4e54b9SDavid Nugent } 765ea4e54b9SDavid Nugent (void) fclose(fp); 766ea4e54b9SDavid Nugent } 767ea4e54b9SDavid Nugent } 768ea4e54b9SDavid Nugent 769ea4e54b9SDavid Nugent static void 7704dd8b5abSYoshinobu Inoue selecthost(su) 7714dd8b5abSYoshinobu Inoue union sockunion *su; 772ea4e54b9SDavid Nugent { 773ea4e54b9SDavid Nugent struct ftphost *hrp; 7744dd8b5abSYoshinobu Inoue u_int16_t port; 7754dd8b5abSYoshinobu Inoue #ifdef INET6 7764dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL; 7774dd8b5abSYoshinobu Inoue #endif 778f38c6cadSYoshinobu Inoue struct addrinfo *hi; 7794dd8b5abSYoshinobu Inoue 7804dd8b5abSYoshinobu Inoue #ifdef INET6 7814dd8b5abSYoshinobu Inoue /* 7824dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton, 7834dd8b5abSYoshinobu Inoue * specified in rfc2373. 7844dd8b5abSYoshinobu Inoue */ 7854dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 && 7864dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) 7874dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr; 7884dd8b5abSYoshinobu Inoue #endif 789ea4e54b9SDavid Nugent 790ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */ 7914dd8b5abSYoshinobu Inoue port = su->su_port; 7924dd8b5abSYoshinobu Inoue su->su_port = 0; 793ea4e54b9SDavid Nugent while (hrp != NULL) { 794b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { 795f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { 796ea4e54b9SDavid Nugent thishost = hrp; 797ea4e54b9SDavid Nugent break; 798ea4e54b9SDavid Nugent } 7994dd8b5abSYoshinobu Inoue #ifdef INET6 8004dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */ 801f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && 8024dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12], 803f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr, 8044dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) { 8054dd8b5abSYoshinobu Inoue thishost = hrp; 8064dd8b5abSYoshinobu Inoue break; 8074dd8b5abSYoshinobu Inoue } 8084dd8b5abSYoshinobu Inoue #endif 809f38c6cadSYoshinobu Inoue } 810ea4e54b9SDavid Nugent hrp = hrp->next; 811ea4e54b9SDavid Nugent } 8124dd8b5abSYoshinobu Inoue su->su_port = port; 813ea4e54b9SDavid Nugent /* setup static variables as appropriate */ 814ea4e54b9SDavid Nugent hostname = thishost->hostname; 815ea4e54b9SDavid Nugent ftpuser = thishost->anonuser; 816ea4e54b9SDavid Nugent } 817ea4e54b9SDavid Nugent #endif 818ea4e54b9SDavid Nugent 819ea022d16SRodney W. Grimes /* 820ea022d16SRodney W. Grimes * Helper function for sgetpwnam(). 821ea022d16SRodney W. Grimes */ 822ea022d16SRodney W. Grimes static char * 823ea022d16SRodney W. Grimes sgetsave(s) 824ea022d16SRodney W. Grimes char *s; 825ea022d16SRodney W. Grimes { 826ea022d16SRodney W. Grimes char *new = malloc((unsigned) strlen(s) + 1); 827ea022d16SRodney W. Grimes 828ea022d16SRodney W. Grimes if (new == NULL) { 829ea022d16SRodney W. Grimes perror_reply(421, "Local resource failure: malloc"); 830ea022d16SRodney W. Grimes dologout(1); 831ea022d16SRodney W. Grimes /* NOTREACHED */ 832ea022d16SRodney W. Grimes } 833ea022d16SRodney W. Grimes (void) strcpy(new, s); 834ea022d16SRodney W. Grimes return (new); 835ea022d16SRodney W. Grimes } 836ea022d16SRodney W. Grimes 837ea022d16SRodney W. Grimes /* 838ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since 839ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command 840ea022d16SRodney W. Grimes * (e.g., globbing). 841ea022d16SRodney W. Grimes */ 842ea022d16SRodney W. Grimes static struct passwd * 843ea022d16SRodney W. Grimes sgetpwnam(name) 844ea022d16SRodney W. Grimes char *name; 845ea022d16SRodney W. Grimes { 846ea022d16SRodney W. Grimes static struct passwd save; 847ea022d16SRodney W. Grimes struct passwd *p; 848ea022d16SRodney W. Grimes 849ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL) 850ea022d16SRodney W. Grimes return (p); 851ea022d16SRodney W. Grimes if (save.pw_name) { 852ea022d16SRodney W. Grimes free(save.pw_name); 853ea022d16SRodney W. Grimes free(save.pw_passwd); 854ea022d16SRodney W. Grimes free(save.pw_gecos); 855ea022d16SRodney W. Grimes free(save.pw_dir); 856ea022d16SRodney W. Grimes free(save.pw_shell); 857ea022d16SRodney W. Grimes } 858ea022d16SRodney W. Grimes save = *p; 859ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name); 860ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd); 861ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos); 862ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir); 863ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell); 864ea022d16SRodney W. Grimes return (&save); 865ea022d16SRodney W. Grimes } 866ea022d16SRodney W. Grimes 867ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */ 868ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */ 86990906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */ 870ea022d16SRodney W. Grimes 871ea022d16SRodney W. Grimes /* 872ea022d16SRodney W. Grimes * USER command. 873ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable; 874ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously, 875ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in 876ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. 877ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user 878ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard 879ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file 880ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. 881ea022d16SRodney W. Grimes */ 882ea022d16SRodney W. Grimes void 883ea022d16SRodney W. Grimes user(name) 884ea022d16SRodney W. Grimes char *name; 885ea022d16SRodney W. Grimes { 886ea022d16SRodney W. Grimes char *cp, *shell; 887ea022d16SRodney W. Grimes 888ea022d16SRodney W. Grimes if (logged_in) { 889ea022d16SRodney W. Grimes if (guest) { 890ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login."); 891ea022d16SRodney W. Grimes return; 892a5a4544eSPaul Traina } else if (dochroot) { 893a5a4544eSPaul Traina reply(530, "Can't change user from chroot user."); 894a5a4544eSPaul Traina return; 895ea022d16SRodney W. Grimes } 896ea022d16SRodney W. Grimes end_login(); 897ea022d16SRodney W. Grimes } 898ea022d16SRodney W. Grimes 899ea022d16SRodney W. Grimes guest = 0; 900ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { 9017edcb936SSteve Price if (checkuser(_PATH_FTPUSERS, "ftp", 0) || 9027edcb936SSteve Price checkuser(_PATH_FTPUSERS, "anonymous", 0)) 903ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 904ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 905ea4e54b9SDavid Nugent else if ((pw = sgetpwnam(thishost->anonuser)) != NULL) { 906ea4e54b9SDavid Nugent #else 907ea022d16SRodney W. Grimes else if ((pw = sgetpwnam("ftp")) != NULL) { 908ea4e54b9SDavid Nugent #endif 909ea022d16SRodney W. Grimes guest = 1; 910ea022d16SRodney W. Grimes askpasswd = 1; 911ea022d16SRodney W. Grimes reply(331, 9122c60c54cSPaul Traina "Guest login ok, send your email address as password."); 913ea022d16SRodney W. Grimes } else 914ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name); 915ea022d16SRodney W. Grimes if (!askpasswd && logging) 916ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 917ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); 918ea022d16SRodney W. Grimes return; 919ea022d16SRodney W. Grimes } 9205a392aecSTorsten Blum if (anon_only != 0) { 9215a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed."); 9225a392aecSTorsten Blum return; 9235a392aecSTorsten Blum } 9245a392aecSTorsten Blum 9259aca17cbSMark Murray if ((pw = sgetpwnam(name))) { 926ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0) 927ea022d16SRodney W. Grimes shell = _PATH_BSHELL; 928ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL) 929ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0) 930ea022d16SRodney W. Grimes break; 931ea022d16SRodney W. Grimes endusershell(); 932ea022d16SRodney W. Grimes 9337edcb936SSteve Price if (cp == NULL || checkuser(_PATH_FTPUSERS, name, 1)) { 934ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name); 935ea022d16SRodney W. Grimes if (logging) 936ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 937ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s", 938ea022d16SRodney W. Grimes remotehost, name); 939ea022d16SRodney W. Grimes pw = (struct passwd *) NULL; 940ea022d16SRodney W. Grimes return; 941ea022d16SRodney W. Grimes } 942ea022d16SRodney W. Grimes } 943ea022d16SRodney W. Grimes if (logging) 944ea022d16SRodney W. Grimes strncpy(curname, name, sizeof(curname)-1); 945726040deSGuido van Rooij #ifdef SKEY 946ea413ab7SGuido van Rooij pwok = skeyaccess(name, NULL, remotehost, remotehost); 94743658eacSAndrey A. Chernov reply(331, "%s", skey_challenge(name, pw, pwok)); 948726040deSGuido van Rooij #else 949ea022d16SRodney W. Grimes reply(331, "Password required for %s.", name); 950726040deSGuido van Rooij #endif 951ea022d16SRodney W. Grimes askpasswd = 1; 952ea022d16SRodney W. Grimes /* 953ea022d16SRodney W. Grimes * Delay before reading passwd after first failed 954ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs. 955ea022d16SRodney W. Grimes */ 956ea022d16SRodney W. Grimes if (login_attempts) 957ea022d16SRodney W. Grimes sleep((unsigned) login_attempts); 958ea022d16SRodney W. Grimes } 959ea022d16SRodney W. Grimes 960ea022d16SRodney W. Grimes /* 961a5a4544eSPaul Traina * Check if a user is in the file "fname" 962ea022d16SRodney W. Grimes */ 963ea022d16SRodney W. Grimes static int 9647edcb936SSteve Price checkuser(fname, name, pwset) 965a5a4544eSPaul Traina char *fname; 966ea022d16SRodney W. Grimes char *name; 9677edcb936SSteve Price int pwset; 968ea022d16SRodney W. Grimes { 969ea022d16SRodney W. Grimes FILE *fd; 970ea022d16SRodney W. Grimes int found = 0; 971ea022d16SRodney W. Grimes char *p, line[BUFSIZ]; 972ea022d16SRodney W. Grimes 973a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) { 97431fea7b8SDavid Nugent while (!found && fgets(line, sizeof(line), fd) != NULL) 975ea022d16SRodney W. Grimes if ((p = strchr(line, '\n')) != NULL) { 976ea022d16SRodney W. Grimes *p = '\0'; 977ea022d16SRodney W. Grimes if (line[0] == '#') 978ea022d16SRodney W. Grimes continue; 97931fea7b8SDavid Nugent /* 98031fea7b8SDavid Nugent * if first chr is '@', check group membership 98131fea7b8SDavid Nugent */ 98231fea7b8SDavid Nugent if (line[0] == '@') { 98331fea7b8SDavid Nugent int i = 0; 98431fea7b8SDavid Nugent struct group *grp; 98531fea7b8SDavid Nugent 98631fea7b8SDavid Nugent if ((grp = getgrnam(line+1)) == NULL) 98731fea7b8SDavid Nugent continue; 9887edcb936SSteve Price /* 9897edcb936SSteve Price * Check user's default group 9907edcb936SSteve Price */ 9917edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid) 9927edcb936SSteve Price found = 1; 9937edcb936SSteve Price /* 9947edcb936SSteve Price * Check supplementary groups 9957edcb936SSteve Price */ 99631fea7b8SDavid Nugent while (!found && grp->gr_mem[i]) 99731fea7b8SDavid Nugent found = strcmp(name, 99831fea7b8SDavid Nugent grp->gr_mem[i++]) 99931fea7b8SDavid Nugent == 0; 1000ea022d16SRodney W. Grimes } 100131fea7b8SDavid Nugent /* 100231fea7b8SDavid Nugent * Otherwise, just check for username match 100331fea7b8SDavid Nugent */ 100431fea7b8SDavid Nugent else 100531fea7b8SDavid Nugent found = strcmp(line, name) == 0; 1006ea022d16SRodney W. Grimes } 1007ea022d16SRodney W. Grimes (void) fclose(fd); 1008ea022d16SRodney W. Grimes } 1009ea022d16SRodney W. Grimes return (found); 1010ea022d16SRodney W. Grimes } 1011ea022d16SRodney W. Grimes 1012ea022d16SRodney W. Grimes /* 1013ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state; 1014ea022d16SRodney W. Grimes * used when USER command is given or login fails. 1015ea022d16SRodney W. Grimes */ 1016ea022d16SRodney W. Grimes static void 1017ea022d16SRodney W. Grimes end_login() 1018ea022d16SRodney W. Grimes { 1019ea022d16SRodney W. Grimes 1020ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 1021ea022d16SRodney W. Grimes if (logged_in) 1022986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 1023ea022d16SRodney W. Grimes pw = NULL; 1024b071c689SDavid Nugent #ifdef LOGIN_CAP 1025b071c689SDavid Nugent setusercontext(NULL, getpwuid(0), (uid_t)0, 1026b071c689SDavid Nugent LOGIN_SETPRIORITY|LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1027b071c689SDavid Nugent #endif 1028ea022d16SRodney W. Grimes logged_in = 0; 1029ea022d16SRodney W. Grimes guest = 0; 1030a5a4544eSPaul Traina dochroot = 0; 1031ea022d16SRodney W. Grimes } 1032ea022d16SRodney W. Grimes 10336c9134c0SMark Murray #if !defined(NOPAM) 10346c9134c0SMark Murray 10356c9134c0SMark Murray /* 10366c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and 10376c9134c0SMark Murray * login.c 10386c9134c0SMark Murray */ 10396c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL) 10406c9134c0SMark Murray 10416c9134c0SMark Murray struct cred_t { 10426c9134c0SMark Murray const char *uname; /* user name */ 10436c9134c0SMark Murray const char *pass; /* password */ 10446c9134c0SMark Murray }; 10456c9134c0SMark Murray typedef struct cred_t cred_t; 10466c9134c0SMark Murray 10476c9134c0SMark Murray static int 10486c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg, 10496c9134c0SMark Murray struct pam_response **resp, void *appdata) 10506c9134c0SMark Murray { 10516c9134c0SMark Murray int i; 10526c9134c0SMark Murray cred_t *cred = (cred_t *) appdata; 10536c9134c0SMark Murray struct pam_response *reply = 10546c9134c0SMark Murray malloc(sizeof(struct pam_response) * num_msg); 10556c9134c0SMark Murray 10566c9134c0SMark Murray for (i = 0; i < num_msg; i++) { 10576c9134c0SMark Murray switch (msg[i]->msg_style) { 10586c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */ 10596c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 10606c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname); 10616c9134c0SMark Murray /* PAM frees resp. */ 10626c9134c0SMark Murray break; 10636c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */ 10646c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 10656c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass); 10666c9134c0SMark Murray /* PAM frees resp. */ 10676c9134c0SMark Murray break; 10686c9134c0SMark Murray case PAM_TEXT_INFO: 10696c9134c0SMark Murray case PAM_ERROR_MSG: 10706c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS; 10716c9134c0SMark Murray reply[i].resp = NULL; 10726c9134c0SMark Murray break; 10736c9134c0SMark Murray default: /* unknown message style */ 10746c9134c0SMark Murray free(reply); 10756c9134c0SMark Murray return PAM_CONV_ERR; 10766c9134c0SMark Murray } 10776c9134c0SMark Murray } 10786c9134c0SMark Murray 10796c9134c0SMark Murray *resp = reply; 10806c9134c0SMark Murray return PAM_SUCCESS; 10816c9134c0SMark Murray } 10826c9134c0SMark Murray 10836c9134c0SMark Murray /* 10846c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is 10856c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system 10866c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this 10876c9134c0SMark Murray * function returns -1. This can be used as an indication that we should 10886c9134c0SMark Murray * fall back to a different authentication mechanism. 10896c9134c0SMark Murray */ 10906c9134c0SMark Murray static int 10916c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass) 10926c9134c0SMark Murray { 10936c9134c0SMark Murray pam_handle_t *pamh = NULL; 10946c9134c0SMark Murray const char *tmpl_user; 10956c9134c0SMark Murray const void *item; 10966c9134c0SMark Murray int rval; 10976c9134c0SMark Murray int e; 10986c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass }; 10996c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred }; 11006c9134c0SMark Murray 11016c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); 11026c9134c0SMark Murray if (e != PAM_SUCCESS) { 11036c9134c0SMark Murray syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e)); 11046c9134c0SMark Murray return -1; 11056c9134c0SMark Murray } 11066c9134c0SMark Murray 1107ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost); 1108ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) { 1109ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", 1110ea413ab7SGuido van Rooij pam_strerror(pamh, e)); 1111ea413ab7SGuido van Rooij return -1; 1112ea413ab7SGuido van Rooij } 1113ea413ab7SGuido van Rooij 11146c9134c0SMark Murray e = pam_authenticate(pamh, 0); 11156c9134c0SMark Murray switch (e) { 11166c9134c0SMark Murray case PAM_SUCCESS: 11176c9134c0SMark Murray /* 11186c9134c0SMark Murray * With PAM we support the concept of a "template" 11196c9134c0SMark Murray * user. The user enters a login name which is 11206c9134c0SMark Murray * authenticated by PAM, usually via a remote service 11216c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication 11226c9134c0SMark Murray * succeeds, a different but related "template" name 11236c9134c0SMark Murray * is used for setting the credentials, shell, and 11246c9134c0SMark Murray * home directory. The name the user enters need only 11256c9134c0SMark Murray * exist on the remote authentication server, but the 11266c9134c0SMark Murray * template name must be present in the local password 11276c9134c0SMark Murray * database. 11286c9134c0SMark Murray * 11296c9134c0SMark Murray * This is supported by two various mechanisms in the 11306c9134c0SMark Murray * individual modules. However, from the application's 11316c9134c0SMark Murray * point of view, the template user is always passed 11326c9134c0SMark Murray * back as a changed value of the PAM_USER item. 11336c9134c0SMark Murray */ 11346c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) == 11356c9134c0SMark Murray PAM_SUCCESS) { 11366c9134c0SMark Murray tmpl_user = (const char *) item; 11376c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0) 11386c9134c0SMark Murray *ppw = getpwnam(tmpl_user); 11396c9134c0SMark Murray } else 11406c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s", 11416c9134c0SMark Murray pam_strerror(pamh, e)); 11426c9134c0SMark Murray rval = 0; 11436c9134c0SMark Murray break; 11446c9134c0SMark Murray 11456c9134c0SMark Murray case PAM_AUTH_ERR: 11466c9134c0SMark Murray case PAM_USER_UNKNOWN: 11476c9134c0SMark Murray case PAM_MAXTRIES: 11486c9134c0SMark Murray rval = 1; 11496c9134c0SMark Murray break; 11506c9134c0SMark Murray 11516c9134c0SMark Murray default: 11526c9134c0SMark Murray syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e)); 11536c9134c0SMark Murray rval = -1; 11546c9134c0SMark Murray break; 11556c9134c0SMark Murray } 11566c9134c0SMark Murray 11576c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { 11586c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); 11596c9134c0SMark Murray rval = -1; 11606c9134c0SMark Murray } 11616c9134c0SMark Murray return rval; 11626c9134c0SMark Murray } 11636c9134c0SMark Murray 11646c9134c0SMark Murray #endif /* !defined(NOPAM) */ 11656c9134c0SMark Murray 1166ea022d16SRodney W. Grimes void 1167ea022d16SRodney W. Grimes pass(passwd) 1168ea022d16SRodney W. Grimes char *passwd; 1169ea022d16SRodney W. Grimes { 1170a5a4544eSPaul Traina int rval; 1171ea022d16SRodney W. Grimes FILE *fd; 1172b071c689SDavid Nugent #ifdef LOGIN_CAP 1173b071c689SDavid Nugent login_cap_t *lc = NULL; 1174b071c689SDavid Nugent #endif 1175ea022d16SRodney W. Grimes 1176ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) { 1177ea022d16SRodney W. Grimes reply(503, "Login with USER first."); 1178ea022d16SRodney W. Grimes return; 1179ea022d16SRodney W. Grimes } 1180ea022d16SRodney W. Grimes askpasswd = 0; 1181ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */ 1182a5a4544eSPaul Traina if (pw == NULL) { 1183a5a4544eSPaul Traina rval = 1; /* failure below */ 1184a5a4544eSPaul Traina goto skip; 1185a5a4544eSPaul Traina } 11866c9134c0SMark Murray #if !defined(NOPAM) 11876c9134c0SMark Murray rval = auth_pam(&pw, passwd); 11886c9134c0SMark Murray if (rval >= 0) 1189a5a4544eSPaul Traina goto skip; 1190a5a4544eSPaul Traina #endif 1191726040deSGuido van Rooij #ifdef SKEY 1192028f24cfSSheldon Hearn if (pwok) 1193028f24cfSSheldon Hearn rval = strcmp(pw->pw_passwd, 1194028f24cfSSheldon Hearn crypt(passwd, pw->pw_passwd)); 1195028f24cfSSheldon Hearn if (rval) 1196028f24cfSSheldon Hearn rval = strcmp(pw->pw_passwd, 1197028f24cfSSheldon Hearn skey_crypt(passwd, pw->pw_passwd, pw, pwok)); 1198726040deSGuido van Rooij #else 1199028f24cfSSheldon Hearn rval = strcmp(pw->pw_passwd, crypt(passwd, pw->pw_passwd)); 1200726040deSGuido van Rooij #endif 1201ea022d16SRodney W. Grimes /* The strcmp does not catch null passwords! */ 1202a5a4544eSPaul Traina if (*pw->pw_passwd == '\0' || 1203a5a4544eSPaul Traina (pw->pw_expire && time(NULL) >= pw->pw_expire)) 1204a5a4544eSPaul Traina rval = 1; /* failure */ 1205a5a4544eSPaul Traina skip: 1206a5a4544eSPaul Traina /* 1207a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check 12086c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication 1209a5a4544eSPaul Traina * succeeded. 1210a5a4544eSPaul Traina */ 1211a5a4544eSPaul Traina if (rval) { 1212ea022d16SRodney W. Grimes reply(530, "Login incorrect."); 1213ea022d16SRodney W. Grimes if (logging) 1214ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1215ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s", 1216ea022d16SRodney W. Grimes remotehost, curname); 1217ea022d16SRodney W. Grimes pw = NULL; 1218ea022d16SRodney W. Grimes if (login_attempts++ >= 5) { 1219ea022d16SRodney W. Grimes syslog(LOG_NOTICE, 1220ea022d16SRodney W. Grimes "repeated login failures from %s", 1221ea022d16SRodney W. Grimes remotehost); 1222ea022d16SRodney W. Grimes exit(0); 1223ea022d16SRodney W. Grimes } 1224ea022d16SRodney W. Grimes return; 1225ea022d16SRodney W. Grimes } 1226ea022d16SRodney W. Grimes } 1227028f24cfSSheldon Hearn #ifdef SKEY 1228028f24cfSSheldon Hearn pwok = 0; 1229028f24cfSSheldon Hearn #endif 1230ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */ 1231ea022d16SRodney W. Grimes if (setegid((gid_t)pw->pw_gid) < 0) { 1232ea022d16SRodney W. Grimes reply(550, "Can't set gid."); 1233ea022d16SRodney W. Grimes return; 1234ea022d16SRodney W. Grimes } 1235b071c689SDavid Nugent /* May be overridden by login.conf */ 1236b071c689SDavid Nugent (void) umask(defumask); 1237b071c689SDavid Nugent #ifdef LOGIN_CAP 12385d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) { 1239b071c689SDavid Nugent char remote_ip[MAXHOSTNAMELEN]; 1240b071c689SDavid Nugent 12414dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 12424dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0, 12434dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 1244b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0; 1245b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) { 1246b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH, 1247b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.", 1248b071c689SDavid Nugent pw->pw_name); 1249b071c689SDavid Nugent reply(530, "Permission denied.\n"); 1250b071c689SDavid Nugent pw = NULL; 1251b071c689SDavid Nugent return; 1252b071c689SDavid Nugent } 1253b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) { 1254b071c689SDavid Nugent reply(530, "Login not available right now.\n"); 1255b071c689SDavid Nugent pw = NULL; 1256b071c689SDavid Nugent return; 1257b071c689SDavid Nugent } 1258b071c689SDavid Nugent } 1259b071c689SDavid Nugent setusercontext(lc, pw, (uid_t)0, 1260e6fa0d43SDag-Erling Smørgrav LOGIN_SETLOGIN|LOGIN_SETGROUP|LOGIN_SETPRIORITY| 1261e6fa0d43SDag-Erling Smørgrav LOGIN_SETRESOURCES|LOGIN_SETUMASK); 1262b071c689SDavid Nugent #else 1263e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name); 1264ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid); 1265b071c689SDavid Nugent #endif 1266ea022d16SRodney W. Grimes 1267ea022d16SRodney W. Grimes /* open wtmp before chroot */ 1268986a1172SThomas Gellekum ftpd_logwtmp(ttyline, pw->pw_name, remotehost); 1269ea022d16SRodney W. Grimes logged_in = 1; 1270ea022d16SRodney W. Grimes 1271a5a4544eSPaul Traina if (guest && stats && statfd < 0) 1272ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1273ea4e54b9SDavid Nugent if ((statfd = open(thishost->statfile, O_WRONLY|O_APPEND)) < 0) 1274ea4e54b9SDavid Nugent #else 12753eb568f2SGuido van Rooij if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) < 0) 1276ea4e54b9SDavid Nugent #endif 12773eb568f2SGuido van Rooij stats = 0; 12783eb568f2SGuido van Rooij 1279b071c689SDavid Nugent dochroot = 1280b071c689SDavid Nugent #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ 1281b071c689SDavid Nugent login_getcapbool(lc, "ftp-chroot", 0) || 1282b071c689SDavid Nugent #endif 12837edcb936SSteve Price checkuser(_PATH_FTPCHROOT, pw->pw_name, 1); 1284ea022d16SRodney W. Grimes if (guest) { 1285ea022d16SRodney W. Grimes /* 1286ea022d16SRodney W. Grimes * We MUST do a chdir() after the chroot. Otherwise 1287ea022d16SRodney W. Grimes * the old current directory will be accessible as "." 1288ea022d16SRodney W. Grimes * outside the new root! 1289ea022d16SRodney W. Grimes */ 1290ea022d16SRodney W. Grimes if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1291ea022d16SRodney W. Grimes reply(550, "Can't set guest privileges."); 1292ea022d16SRodney W. Grimes goto bad; 1293ea022d16SRodney W. Grimes } 1294a5a4544eSPaul Traina } else if (dochroot) { 1295a5a4544eSPaul Traina if (chroot(pw->pw_dir) < 0 || chdir("/") < 0) { 1296a5a4544eSPaul Traina reply(550, "Can't change root."); 1297a5a4544eSPaul Traina goto bad; 1298a5a4544eSPaul Traina } 1299ea022d16SRodney W. Grimes } else if (chdir(pw->pw_dir) < 0) { 1300ea022d16SRodney W. Grimes if (chdir("/") < 0) { 1301ea022d16SRodney W. Grimes reply(530, "User %s: can't change directory to %s.", 1302ea022d16SRodney W. Grimes pw->pw_name, pw->pw_dir); 1303ea022d16SRodney W. Grimes goto bad; 1304ea022d16SRodney W. Grimes } else 1305ea022d16SRodney W. Grimes lreply(230, "No directory! Logging in with home=/"); 1306ea022d16SRodney W. Grimes } 1307ea022d16SRodney W. Grimes if (seteuid((uid_t)pw->pw_uid) < 0) { 1308ea022d16SRodney W. Grimes reply(550, "Can't set uid."); 1309ea022d16SRodney W. Grimes goto bad; 1310ea022d16SRodney W. Grimes } 131182c76939SDavid Greenman 131282c76939SDavid Greenman /* 1313ea022d16SRodney W. Grimes * Display a login message, if it exists. 1314ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message. 1315ea022d16SRodney W. Grimes */ 1316ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1317ea4e54b9SDavid Nugent if ((fd = fopen(thishost->loginmsg, "r")) != NULL) { 1318ea4e54b9SDavid Nugent #else 1319ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_FTPLOGINMESG, "r")) != NULL) { 1320ea4e54b9SDavid Nugent #endif 1321ea022d16SRodney W. Grimes char *cp, line[LINE_MAX]; 1322ea022d16SRodney W. Grimes 1323ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) { 1324ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL) 1325ea022d16SRodney W. Grimes *cp = '\0'; 1326ea022d16SRodney W. Grimes lreply(230, "%s", line); 1327ea022d16SRodney W. Grimes } 1328ea022d16SRodney W. Grimes (void) fflush(stdout); 1329ea022d16SRodney W. Grimes (void) fclose(fd); 1330ea022d16SRodney W. Grimes } 1331ea022d16SRodney W. Grimes if (guest) { 13323eb568f2SGuido van Rooij if (ident != NULL) 13333eb568f2SGuido van Rooij free(ident); 133461f891a6SPaul Traina ident = strdup(passwd); 133561f891a6SPaul Traina if (ident == NULL) 133661f891a6SPaul Traina fatal("Ran out of memory."); 133761f891a6SPaul Traina 1338ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply."); 1339ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1340ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 1341ea4e54b9SDavid Nugent if (thishost != firsthost) 1342ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), 1343ea4e54b9SDavid Nugent "%s: anonymous(%s)/%.*s", remotehost, hostname, 13446c9134c0SMark Murray (int)(sizeof(proctitle) - sizeof(remotehost) - 13456c9134c0SMark Murray sizeof(": anonymous/")), passwd); 1346ea4e54b9SDavid Nugent else 1347ea4e54b9SDavid Nugent #endif 1348ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1349ea022d16SRodney W. Grimes "%s: anonymous/%.*s", remotehost, 13506c9134c0SMark Murray (int)(sizeof(proctitle) - sizeof(remotehost) - 13516c9134c0SMark Murray sizeof(": anonymous/")), passwd); 1352b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1353ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1354ea022d16SRodney W. Grimes if (logging) 1355ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", 1356ea022d16SRodney W. Grimes remotehost, passwd); 1357ea022d16SRodney W. Grimes } else { 13583401a71fSDaniel O'Callaghan if (dochroot) 13593401a71fSDaniel O'Callaghan reply(230, "User %s logged in, access restrictions apply.", 13603401a71fSDaniel O'Callaghan pw->pw_name); 13613401a71fSDaniel O'Callaghan else 1362ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name); 13633401a71fSDaniel O'Callaghan 1364ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 1365ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle), 1366ea022d16SRodney W. Grimes "%s: %s", remotehost, pw->pw_name); 1367b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 1368ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 1369ea022d16SRodney W. Grimes if (logging) 1370ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", 1371ea022d16SRodney W. Grimes remotehost, pw->pw_name); 1372ea022d16SRodney W. Grimes } 1373b071c689SDavid Nugent #ifdef LOGIN_CAP 1374b071c689SDavid Nugent login_close(lc); 1375b071c689SDavid Nugent #endif 1376ea022d16SRodney W. Grimes return; 1377ea022d16SRodney W. Grimes bad: 1378ea022d16SRodney W. Grimes /* Forget all about it... */ 1379b071c689SDavid Nugent #ifdef LOGIN_CAP 1380b071c689SDavid Nugent login_close(lc); 1381b071c689SDavid Nugent #endif 1382ea022d16SRodney W. Grimes end_login(); 1383ea022d16SRodney W. Grimes } 1384ea022d16SRodney W. Grimes 1385ea022d16SRodney W. Grimes void 1386ea022d16SRodney W. Grimes retrieve(cmd, name) 1387ea022d16SRodney W. Grimes char *cmd, *name; 1388ea022d16SRodney W. Grimes { 1389ea022d16SRodney W. Grimes FILE *fin, *dout; 1390ea022d16SRodney W. Grimes struct stat st; 1391ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1392158a00b2SJohn Birrell time_t start; 1393ea022d16SRodney W. Grimes 1394ea022d16SRodney W. Grimes if (cmd == 0) { 1395ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose; 1396ea022d16SRodney W. Grimes st.st_size = 0; 1397ea022d16SRodney W. Grimes } else { 1398ea022d16SRodney W. Grimes char line[BUFSIZ]; 1399ea022d16SRodney W. Grimes 1400e760ef2cSWarner Losh (void) snprintf(line, sizeof(line), cmd, name), name = line; 1401ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; 1402ea022d16SRodney W. Grimes st.st_size = -1; 1403ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ; 1404ea022d16SRodney W. Grimes } 1405ea022d16SRodney W. Grimes if (fin == NULL) { 1406ea022d16SRodney W. Grimes if (errno != 0) { 1407ea022d16SRodney W. Grimes perror_reply(550, name); 1408ea022d16SRodney W. Grimes if (cmd == 0) { 1409ea022d16SRodney W. Grimes LOGCMD("get", name); 1410ea022d16SRodney W. Grimes } 1411ea022d16SRodney W. Grimes } 1412ea022d16SRodney W. Grimes return; 1413ea022d16SRodney W. Grimes } 1414ea022d16SRodney W. Grimes byte_count = -1; 1415ea022d16SRodney W. Grimes if (cmd == 0 && (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode))) { 1416ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name); 1417ea022d16SRodney W. Grimes goto done; 1418ea022d16SRodney W. Grimes } 1419ea022d16SRodney W. Grimes if (restart_point) { 1420ea022d16SRodney W. Grimes if (type == TYPE_A) { 1421ea022d16SRodney W. Grimes off_t i, n; 1422ea022d16SRodney W. Grimes int c; 1423ea022d16SRodney W. Grimes 1424ea022d16SRodney W. Grimes n = restart_point; 1425ea022d16SRodney W. Grimes i = 0; 1426ea022d16SRodney W. Grimes while (i++ < n) { 1427ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) { 1428ea022d16SRodney W. Grimes perror_reply(550, name); 1429ea022d16SRodney W. Grimes goto done; 1430ea022d16SRodney W. Grimes } 1431ea022d16SRodney W. Grimes if (c == '\n') 1432ea022d16SRodney W. Grimes i++; 1433ea022d16SRodney W. Grimes } 1434ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { 1435ea022d16SRodney W. Grimes perror_reply(550, name); 1436ea022d16SRodney W. Grimes goto done; 1437ea022d16SRodney W. Grimes } 1438ea022d16SRodney W. Grimes } 1439ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w"); 1440ea022d16SRodney W. Grimes if (dout == NULL) 1441ea022d16SRodney W. Grimes goto done; 14423eb568f2SGuido van Rooij time(&start); 14439fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size, 14449fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); 14453eb568f2SGuido van Rooij if (cmd == 0 && guest && stats) 14463eb568f2SGuido van Rooij logxfer(name, st.st_size, start); 1447ea022d16SRodney W. Grimes (void) fclose(dout); 1448ea022d16SRodney W. Grimes data = -1; 1449ea022d16SRodney W. Grimes pdata = -1; 1450ea022d16SRodney W. Grimes done: 1451ea022d16SRodney W. Grimes if (cmd == 0) 1452ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count); 1453ea022d16SRodney W. Grimes (*closefunc)(fin); 1454ea022d16SRodney W. Grimes } 1455ea022d16SRodney W. Grimes 1456ea022d16SRodney W. Grimes void 1457ea022d16SRodney W. Grimes store(name, mode, unique) 1458ea022d16SRodney W. Grimes char *name, *mode; 1459ea022d16SRodney W. Grimes int unique; 1460ea022d16SRodney W. Grimes { 1461ea022d16SRodney W. Grimes FILE *fout, *din; 1462ea022d16SRodney W. Grimes struct stat st; 1463ea022d16SRodney W. Grimes int (*closefunc) __P((FILE *)); 1464ea022d16SRodney W. Grimes 146561f891a6SPaul Traina if ((unique || guest) && stat(name, &st) == 0 && 1466ea022d16SRodney W. Grimes (name = gunique(name)) == NULL) { 1467ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1468ea022d16SRodney W. Grimes return; 1469ea022d16SRodney W. Grimes } 1470ea022d16SRodney W. Grimes 1471ea022d16SRodney W. Grimes if (restart_point) 1472ea022d16SRodney W. Grimes mode = "r+"; 1473ea022d16SRodney W. Grimes fout = fopen(name, mode); 1474ea022d16SRodney W. Grimes closefunc = fclose; 1475ea022d16SRodney W. Grimes if (fout == NULL) { 1476ea022d16SRodney W. Grimes perror_reply(553, name); 1477ea022d16SRodney W. Grimes LOGCMD(*mode == 'w' ? "put" : "append", name); 1478ea022d16SRodney W. Grimes return; 1479ea022d16SRodney W. Grimes } 1480ea022d16SRodney W. Grimes byte_count = -1; 1481ea022d16SRodney W. Grimes if (restart_point) { 1482ea022d16SRodney W. Grimes if (type == TYPE_A) { 1483ea022d16SRodney W. Grimes off_t i, n; 1484ea022d16SRodney W. Grimes int c; 1485ea022d16SRodney W. Grimes 1486ea022d16SRodney W. Grimes n = restart_point; 1487ea022d16SRodney W. Grimes i = 0; 1488ea022d16SRodney W. Grimes while (i++ < n) { 1489ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) { 1490ea022d16SRodney W. Grimes perror_reply(550, name); 1491ea022d16SRodney W. Grimes goto done; 1492ea022d16SRodney W. Grimes } 1493ea022d16SRodney W. Grimes if (c == '\n') 1494ea022d16SRodney W. Grimes i++; 1495ea022d16SRodney W. Grimes } 1496ea022d16SRodney W. Grimes /* 1497ea022d16SRodney W. Grimes * We must do this seek to "current" position 1498ea022d16SRodney W. Grimes * because we are changing from reading to 1499ea022d16SRodney W. Grimes * writing. 1500ea022d16SRodney W. Grimes */ 1501ea022d16SRodney W. Grimes if (fseek(fout, 0L, L_INCR) < 0) { 1502ea022d16SRodney W. Grimes perror_reply(550, name); 1503ea022d16SRodney W. Grimes goto done; 1504ea022d16SRodney W. Grimes } 1505ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { 1506ea022d16SRodney W. Grimes perror_reply(550, name); 1507ea022d16SRodney W. Grimes goto done; 1508ea022d16SRodney W. Grimes } 1509ea022d16SRodney W. Grimes } 1510ea022d16SRodney W. Grimes din = dataconn(name, (off_t)-1, "r"); 1511ea022d16SRodney W. Grimes if (din == NULL) 1512ea022d16SRodney W. Grimes goto done; 1513ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) { 1514ea022d16SRodney W. Grimes if (unique) 1515ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).", 1516ea022d16SRodney W. Grimes name); 1517ea022d16SRodney W. Grimes else 1518ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1519ea022d16SRodney W. Grimes } 1520ea022d16SRodney W. Grimes (void) fclose(din); 1521ea022d16SRodney W. Grimes data = -1; 1522ea022d16SRodney W. Grimes pdata = -1; 1523ea022d16SRodney W. Grimes done: 1524ea022d16SRodney W. Grimes LOGBYTES(*mode == 'w' ? "put" : "append", name, byte_count); 1525ea022d16SRodney W. Grimes (*closefunc)(fout); 1526ea022d16SRodney W. Grimes } 1527ea022d16SRodney W. Grimes 1528ea022d16SRodney W. Grimes static FILE * 1529ea022d16SRodney W. Grimes getdatasock(mode) 1530ea022d16SRodney W. Grimes char *mode; 1531ea022d16SRodney W. Grimes { 1532ea022d16SRodney W. Grimes int on = 1, s, t, tries; 1533ea022d16SRodney W. Grimes 1534ea022d16SRodney W. Grimes if (data >= 0) 1535ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1536ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 15374dd8b5abSYoshinobu Inoue 15384dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0); 1539ea022d16SRodney W. Grimes if (s < 0) 1540ea022d16SRodney W. Grimes goto bad; 1541ea022d16SRodney W. Grimes if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 1542ea022d16SRodney W. Grimes (char *) &on, sizeof(on)) < 0) 1543ea022d16SRodney W. Grimes goto bad; 1544ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */ 15454dd8b5abSYoshinobu Inoue data_source = ctrl_addr; 15464dd8b5abSYoshinobu Inoue data_source.su_port = htons(20); /* ftp-data port */ 1547ea022d16SRodney W. Grimes for (tries = 1; ; tries++) { 1548ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source, 15494dd8b5abSYoshinobu Inoue data_source.su_len) >= 0) 1550ea022d16SRodney W. Grimes break; 1551ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10) 1552ea022d16SRodney W. Grimes goto bad; 1553ea022d16SRodney W. Grimes sleep(tries); 1554ea022d16SRodney W. Grimes } 1555ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1556ea022d16SRodney W. Grimes #ifdef IP_TOS 15574dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET) 15584dd8b5abSYoshinobu Inoue { 1559ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT; 1560ea022d16SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1561ea022d16SRodney W. Grimes syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 15624dd8b5abSYoshinobu Inoue } 1563ea022d16SRodney W. Grimes #endif 15649fc5823aSGarrett Wollman #ifdef TCP_NOPUSH 15659fc5823aSGarrett Wollman /* 15669fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets 15679fc5823aSGarrett Wollman * at the boundaries of each write(). Should probably do a SO_SNDBUF 15689fc5823aSGarrett Wollman * to set the send buffer size as well, but that may not be desirable 15699fc5823aSGarrett Wollman * in heavy-load situations. 15709fc5823aSGarrett Wollman */ 15719fc5823aSGarrett Wollman on = 1; 15729fc5823aSGarrett Wollman if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, (char *)&on, sizeof on) < 0) 15739fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (TCP_NOPUSH): %m"); 15749fc5823aSGarrett Wollman #endif 15759fc5823aSGarrett Wollman #ifdef SO_SNDBUF 15769fc5823aSGarrett Wollman on = 65536; 15779fc5823aSGarrett Wollman if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&on, sizeof on) < 0) 15789fc5823aSGarrett Wollman syslog(LOG_WARNING, "setsockopt (SO_SNDBUF): %m"); 15799fc5823aSGarrett Wollman #endif 15809fc5823aSGarrett Wollman 1581ea022d16SRodney W. Grimes return (fdopen(s, mode)); 1582ea022d16SRodney W. Grimes bad: 1583ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */ 1584ea022d16SRodney W. Grimes t = errno; 1585ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 1586ea022d16SRodney W. Grimes (void) close(s); 1587ea022d16SRodney W. Grimes errno = t; 1588ea022d16SRodney W. Grimes return (NULL); 1589ea022d16SRodney W. Grimes } 1590ea022d16SRodney W. Grimes 1591ea022d16SRodney W. Grimes static FILE * 1592ea022d16SRodney W. Grimes dataconn(name, size, mode) 1593ea022d16SRodney W. Grimes char *name; 1594ea022d16SRodney W. Grimes off_t size; 1595ea022d16SRodney W. Grimes char *mode; 1596ea022d16SRodney W. Grimes { 1597ea022d16SRodney W. Grimes char sizebuf[32]; 1598ea022d16SRodney W. Grimes FILE *file; 1599ea022d16SRodney W. Grimes int retry = 0, tos; 1600ea022d16SRodney W. Grimes 1601ea022d16SRodney W. Grimes file_size = size; 1602ea022d16SRodney W. Grimes byte_count = 0; 1603ea022d16SRodney W. Grimes if (size != (off_t) -1) 1604e760ef2cSWarner Losh (void) snprintf(sizebuf, sizeof(sizebuf), " (%qd bytes)", size); 1605ea022d16SRodney W. Grimes else 1606e760ef2cSWarner Losh *sizebuf = '\0'; 1607ea022d16SRodney W. Grimes if (pdata >= 0) { 16084dd8b5abSYoshinobu Inoue union sockunion from; 16094dd8b5abSYoshinobu Inoue int s, fromlen = ctrl_addr.su_len; 1610d6ed3c37SGuido van Rooij struct timeval timeout; 1611d6ed3c37SGuido van Rooij fd_set set; 1612ea022d16SRodney W. Grimes 1613d6ed3c37SGuido van Rooij FD_ZERO(&set); 1614d6ed3c37SGuido van Rooij FD_SET(pdata, &set); 1615d6ed3c37SGuido van Rooij 1616d6ed3c37SGuido van Rooij timeout.tv_usec = 0; 1617d6ed3c37SGuido van Rooij timeout.tv_sec = 120; 1618d6ed3c37SGuido van Rooij 1619d6ed3c37SGuido van Rooij if (select(pdata+1, &set, (fd_set *) 0, (fd_set *) 0, &timeout) == 0 || 1620d6ed3c37SGuido van Rooij (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) { 1621ea022d16SRodney W. Grimes reply(425, "Can't open data connection."); 1622ea022d16SRodney W. Grimes (void) close(pdata); 1623ea022d16SRodney W. Grimes pdata = -1; 1624ea022d16SRodney W. Grimes return (NULL); 1625ea022d16SRodney W. Grimes } 1626ea022d16SRodney W. Grimes (void) close(pdata); 1627ea022d16SRodney W. Grimes pdata = s; 1628ea022d16SRodney W. Grimes #ifdef IP_TOS 16294dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET) 16304dd8b5abSYoshinobu Inoue { 1631a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT; 1632ea022d16SRodney W. Grimes (void) setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, 1633ea022d16SRodney W. Grimes sizeof(int)); 16344dd8b5abSYoshinobu Inoue } 1635ea022d16SRodney W. Grimes #endif 1636ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1637ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1638ea022d16SRodney W. Grimes return (fdopen(pdata, mode)); 1639ea022d16SRodney W. Grimes } 1640ea022d16SRodney W. Grimes if (data >= 0) { 1641ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.", 1642ea022d16SRodney W. Grimes name, sizebuf); 1643ea022d16SRodney W. Grimes usedefault = 1; 1644ea022d16SRodney W. Grimes return (fdopen(data, mode)); 1645ea022d16SRodney W. Grimes } 1646ea022d16SRodney W. Grimes if (usedefault) 1647ea022d16SRodney W. Grimes data_dest = his_addr; 1648ea022d16SRodney W. Grimes usedefault = 1; 1649ea022d16SRodney W. Grimes file = getdatasock(mode); 1650ea022d16SRodney W. Grimes if (file == NULL) { 16514dd8b5abSYoshinobu Inoue char hostbuf[BUFSIZ], portbuf[BUFSIZ]; 16524dd8b5abSYoshinobu Inoue getnameinfo((struct sockaddr *)&data_source, 16534dd8b5abSYoshinobu Inoue data_source.su_len, hostbuf, sizeof(hostbuf) - 1, 16544dd8b5abSYoshinobu Inoue portbuf, sizeof(portbuf), 16554dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID); 16564dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.", 16574dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno)); 1658ea022d16SRodney W. Grimes return (NULL); 1659ea022d16SRodney W. Grimes } 1660ea022d16SRodney W. Grimes data = fileno(file); 1661ea022d16SRodney W. Grimes while (connect(data, (struct sockaddr *)&data_dest, 16624dd8b5abSYoshinobu Inoue data_dest.su_len) < 0) { 1663ea022d16SRodney W. Grimes if (errno == EADDRINUSE && retry < swaitmax) { 1664ea022d16SRodney W. Grimes sleep((unsigned) swaitint); 1665ea022d16SRodney W. Grimes retry += swaitint; 1666ea022d16SRodney W. Grimes continue; 1667ea022d16SRodney W. Grimes } 1668ea022d16SRodney W. Grimes perror_reply(425, "Can't build data connection"); 1669ea022d16SRodney W. Grimes (void) fclose(file); 1670ea022d16SRodney W. Grimes data = -1; 1671ea022d16SRodney W. Grimes return (NULL); 1672ea022d16SRodney W. Grimes } 1673ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.", 1674ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); 1675ea022d16SRodney W. Grimes return (file); 1676ea022d16SRodney W. Grimes } 1677ea022d16SRodney W. Grimes 1678ea022d16SRodney W. Grimes /* 1679ea022d16SRodney W. Grimes * Tranfer the contents of "instr" to "outstr" peer using the appropriate 16809fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type. 1681ea022d16SRodney W. Grimes * 1682ea022d16SRodney W. Grimes * NB: Form isn't handled. 1683ea022d16SRodney W. Grimes */ 1684ea022d16SRodney W. Grimes static void 16859fc5823aSGarrett Wollman send_data(instr, outstr, blksize, filesize, isreg) 1686ea022d16SRodney W. Grimes FILE *instr, *outstr; 1687ea022d16SRodney W. Grimes off_t blksize; 16889fc5823aSGarrett Wollman off_t filesize; 16899fc5823aSGarrett Wollman int isreg; 1690ea022d16SRodney W. Grimes { 1691ea022d16SRodney W. Grimes int c, cnt, filefd, netfd; 16929fc5823aSGarrett Wollman char *buf, *bp; 16939fc5823aSGarrett Wollman size_t len; 1694ea022d16SRodney W. Grimes 1695ea022d16SRodney W. Grimes transflag++; 1696ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1697ea022d16SRodney W. Grimes transflag = 0; 1698ea022d16SRodney W. Grimes return; 1699ea022d16SRodney W. Grimes } 1700ea022d16SRodney W. Grimes switch (type) { 1701ea022d16SRodney W. Grimes 1702ea022d16SRodney W. Grimes case TYPE_A: 1703ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1704ea022d16SRodney W. Grimes byte_count++; 1705ea022d16SRodney W. Grimes if (c == '\n') { 1706ea022d16SRodney W. Grimes if (ferror(outstr)) 1707ea022d16SRodney W. Grimes goto data_err; 1708ea022d16SRodney W. Grimes (void) putc('\r', outstr); 1709ea022d16SRodney W. Grimes } 1710ea022d16SRodney W. Grimes (void) putc(c, outstr); 1711ea022d16SRodney W. Grimes } 1712ea022d16SRodney W. Grimes fflush(outstr); 1713ea022d16SRodney W. Grimes transflag = 0; 1714ea022d16SRodney W. Grimes if (ferror(instr)) 1715ea022d16SRodney W. Grimes goto file_err; 1716ea022d16SRodney W. Grimes if (ferror(outstr)) 1717ea022d16SRodney W. Grimes goto data_err; 1718ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1719ea022d16SRodney W. Grimes return; 1720ea022d16SRodney W. Grimes 1721ea022d16SRodney W. Grimes case TYPE_I: 1722ea022d16SRodney W. Grimes case TYPE_L: 17239fc5823aSGarrett Wollman /* 17249fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we 17259fc5823aSGarrett Wollman * are sending a regular file 17269fc5823aSGarrett Wollman */ 17279fc5823aSGarrett Wollman netfd = fileno(outstr); 17289fc5823aSGarrett Wollman filefd = fileno(instr); 17299fc5823aSGarrett Wollman 17309fc5823aSGarrett Wollman if (isreg && filesize < (off_t)16 * 1024 * 1024) { 17319fc5823aSGarrett Wollman buf = mmap(0, filesize, PROT_READ, MAP_SHARED, filefd, 17329fc5823aSGarrett Wollman (off_t)0); 17338abdc2ebSAlexander Langer if (buf == MAP_FAILED) { 17349fc5823aSGarrett Wollman syslog(LOG_WARNING, "mmap(%lu): %m", 17359fc5823aSGarrett Wollman (unsigned long)filesize); 17369fc5823aSGarrett Wollman goto oldway; 17379fc5823aSGarrett Wollman } 17389fc5823aSGarrett Wollman bp = buf; 17399fc5823aSGarrett Wollman len = filesize; 17409fc5823aSGarrett Wollman do { 17419fc5823aSGarrett Wollman cnt = write(netfd, bp, len); 17429fc5823aSGarrett Wollman len -= cnt; 17439fc5823aSGarrett Wollman bp += cnt; 17449fc5823aSGarrett Wollman if (cnt > 0) byte_count += cnt; 17459fc5823aSGarrett Wollman } while(cnt > 0 && len > 0); 17469fc5823aSGarrett Wollman 17479fc5823aSGarrett Wollman transflag = 0; 17489fc5823aSGarrett Wollman munmap(buf, (size_t)filesize); 17499fc5823aSGarrett Wollman if (cnt < 0) 17509fc5823aSGarrett Wollman goto data_err; 17519fc5823aSGarrett Wollman reply(226, "Transfer complete."); 17529fc5823aSGarrett Wollman return; 17539fc5823aSGarrett Wollman } 17549fc5823aSGarrett Wollman 17559fc5823aSGarrett Wollman oldway: 1756ea022d16SRodney W. Grimes if ((buf = malloc((u_int)blksize)) == NULL) { 1757ea022d16SRodney W. Grimes transflag = 0; 1758ea022d16SRodney W. Grimes perror_reply(451, "Local resource failure: malloc"); 1759ea022d16SRodney W. Grimes return; 1760ea022d16SRodney W. Grimes } 17619fc5823aSGarrett Wollman 1762ea022d16SRodney W. Grimes while ((cnt = read(filefd, buf, (u_int)blksize)) > 0 && 1763ea022d16SRodney W. Grimes write(netfd, buf, cnt) == cnt) 1764ea022d16SRodney W. Grimes byte_count += cnt; 1765ea022d16SRodney W. Grimes transflag = 0; 1766ea022d16SRodney W. Grimes (void)free(buf); 1767ea022d16SRodney W. Grimes if (cnt != 0) { 1768ea022d16SRodney W. Grimes if (cnt < 0) 1769ea022d16SRodney W. Grimes goto file_err; 1770ea022d16SRodney W. Grimes goto data_err; 1771ea022d16SRodney W. Grimes } 1772ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 1773ea022d16SRodney W. Grimes return; 1774ea022d16SRodney W. Grimes default: 1775ea022d16SRodney W. Grimes transflag = 0; 1776ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in send_data", type); 1777ea022d16SRodney W. Grimes return; 1778ea022d16SRodney W. Grimes } 1779ea022d16SRodney W. Grimes 1780ea022d16SRodney W. Grimes data_err: 1781ea022d16SRodney W. Grimes transflag = 0; 1782ea022d16SRodney W. Grimes perror_reply(426, "Data connection"); 1783ea022d16SRodney W. Grimes return; 1784ea022d16SRodney W. Grimes 1785ea022d16SRodney W. Grimes file_err: 1786ea022d16SRodney W. Grimes transflag = 0; 1787ea022d16SRodney W. Grimes perror_reply(551, "Error on input file"); 1788ea022d16SRodney W. Grimes } 1789ea022d16SRodney W. Grimes 1790ea022d16SRodney W. Grimes /* 1791ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of 1792ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type. 1793ea022d16SRodney W. Grimes * 1794ea022d16SRodney W. Grimes * N.B.: Form isn't handled. 1795ea022d16SRodney W. Grimes */ 1796ea022d16SRodney W. Grimes static int 1797ea022d16SRodney W. Grimes receive_data(instr, outstr) 1798ea022d16SRodney W. Grimes FILE *instr, *outstr; 1799ea022d16SRodney W. Grimes { 1800ea022d16SRodney W. Grimes int c; 180161f891a6SPaul Traina int cnt, bare_lfs; 1802ea022d16SRodney W. Grimes char buf[BUFSIZ]; 1803ea022d16SRodney W. Grimes 1804ea022d16SRodney W. Grimes transflag++; 1805ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 1806ea022d16SRodney W. Grimes transflag = 0; 1807ea022d16SRodney W. Grimes return (-1); 1808ea022d16SRodney W. Grimes } 180961f891a6SPaul Traina 181061f891a6SPaul Traina bare_lfs = 0; 181161f891a6SPaul Traina 1812ea022d16SRodney W. Grimes switch (type) { 1813ea022d16SRodney W. Grimes 1814ea022d16SRodney W. Grimes case TYPE_I: 1815ea022d16SRodney W. Grimes case TYPE_L: 1816ea022d16SRodney W. Grimes while ((cnt = read(fileno(instr), buf, sizeof(buf))) > 0) { 1817ea022d16SRodney W. Grimes if (write(fileno(outstr), buf, cnt) != cnt) 1818ea022d16SRodney W. Grimes goto file_err; 1819ea022d16SRodney W. Grimes byte_count += cnt; 1820ea022d16SRodney W. Grimes } 1821ea022d16SRodney W. Grimes if (cnt < 0) 1822ea022d16SRodney W. Grimes goto data_err; 1823ea022d16SRodney W. Grimes transflag = 0; 1824ea022d16SRodney W. Grimes return (0); 1825ea022d16SRodney W. Grimes 1826ea022d16SRodney W. Grimes case TYPE_E: 1827ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented."); 1828ea022d16SRodney W. Grimes transflag = 0; 1829ea022d16SRodney W. Grimes return (-1); 1830ea022d16SRodney W. Grimes 1831ea022d16SRodney W. Grimes case TYPE_A: 1832ea022d16SRodney W. Grimes while ((c = getc(instr)) != EOF) { 1833ea022d16SRodney W. Grimes byte_count++; 1834ea022d16SRodney W. Grimes if (c == '\n') 1835ea022d16SRodney W. Grimes bare_lfs++; 1836ea022d16SRodney W. Grimes while (c == '\r') { 1837ea022d16SRodney W. Grimes if (ferror(outstr)) 1838ea022d16SRodney W. Grimes goto data_err; 1839ea022d16SRodney W. Grimes if ((c = getc(instr)) != '\n') { 1840ea022d16SRodney W. Grimes (void) putc ('\r', outstr); 1841ea022d16SRodney W. Grimes if (c == '\0' || c == EOF) 1842ea022d16SRodney W. Grimes goto contin2; 1843ea022d16SRodney W. Grimes } 1844ea022d16SRodney W. Grimes } 1845ea022d16SRodney W. Grimes (void) putc(c, outstr); 1846ea022d16SRodney W. Grimes contin2: ; 1847ea022d16SRodney W. Grimes } 1848ea022d16SRodney W. Grimes fflush(outstr); 1849ea022d16SRodney W. Grimes if (ferror(instr)) 1850ea022d16SRodney W. Grimes goto data_err; 1851ea022d16SRodney W. Grimes if (ferror(outstr)) 1852ea022d16SRodney W. Grimes goto file_err; 1853ea022d16SRodney W. Grimes transflag = 0; 1854ea022d16SRodney W. Grimes if (bare_lfs) { 1855ea022d16SRodney W. Grimes lreply(226, 1856ea022d16SRodney W. Grimes "WARNING! %d bare linefeeds received in ASCII mode", 1857ea022d16SRodney W. Grimes bare_lfs); 1858ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n"); 1859ea022d16SRodney W. Grimes } 1860ea022d16SRodney W. Grimes return (0); 1861ea022d16SRodney W. Grimes default: 1862ea022d16SRodney W. Grimes reply(550, "Unimplemented TYPE %d in receive_data", type); 1863ea022d16SRodney W. Grimes transflag = 0; 1864ea022d16SRodney W. Grimes return (-1); 1865ea022d16SRodney W. Grimes } 1866ea022d16SRodney W. Grimes 1867ea022d16SRodney W. Grimes data_err: 1868ea022d16SRodney W. Grimes transflag = 0; 1869ea022d16SRodney W. Grimes perror_reply(426, "Data Connection"); 1870ea022d16SRodney W. Grimes return (-1); 1871ea022d16SRodney W. Grimes 1872ea022d16SRodney W. Grimes file_err: 1873ea022d16SRodney W. Grimes transflag = 0; 1874ea022d16SRodney W. Grimes perror_reply(452, "Error writing file"); 1875ea022d16SRodney W. Grimes return (-1); 1876ea022d16SRodney W. Grimes } 1877ea022d16SRodney W. Grimes 1878ea022d16SRodney W. Grimes void 1879ea022d16SRodney W. Grimes statfilecmd(filename) 1880ea022d16SRodney W. Grimes char *filename; 1881ea022d16SRodney W. Grimes { 1882ea022d16SRodney W. Grimes FILE *fin; 1883ea022d16SRodney W. Grimes int c; 1884ea022d16SRodney W. Grimes char line[LINE_MAX]; 1885ea022d16SRodney W. Grimes 1886af85d782SDavid Nugent (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); 1887ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"); 1888ea022d16SRodney W. Grimes lreply(211, "status of %s:", filename); 1889ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) { 1890ea022d16SRodney W. Grimes if (c == '\n') { 1891ea022d16SRodney W. Grimes if (ferror(stdout)){ 1892ea022d16SRodney W. Grimes perror_reply(421, "control connection"); 1893ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1894ea022d16SRodney W. Grimes dologout(1); 1895ea022d16SRodney W. Grimes /* NOTREACHED */ 1896ea022d16SRodney W. Grimes } 1897ea022d16SRodney W. Grimes if (ferror(fin)) { 1898ea022d16SRodney W. Grimes perror_reply(551, filename); 1899ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1900ea022d16SRodney W. Grimes return; 1901ea022d16SRodney W. Grimes } 1902ea022d16SRodney W. Grimes (void) putc('\r', stdout); 1903ea022d16SRodney W. Grimes } 1904ea022d16SRodney W. Grimes (void) putc(c, stdout); 1905ea022d16SRodney W. Grimes } 1906ea022d16SRodney W. Grimes (void) ftpd_pclose(fin); 1907ea022d16SRodney W. Grimes reply(211, "End of Status"); 1908ea022d16SRodney W. Grimes } 1909ea022d16SRodney W. Grimes 1910ea022d16SRodney W. Grimes void 1911ea022d16SRodney W. Grimes statcmd() 1912ea022d16SRodney W. Grimes { 19134dd8b5abSYoshinobu Inoue union sockunion *su; 1914ea022d16SRodney W. Grimes u_char *a, *p; 19154dd8b5abSYoshinobu Inoue char hname[INET6_ADDRSTRLEN]; 19164dd8b5abSYoshinobu Inoue int ispassive; 1917ea022d16SRodney W. Grimes 1918ea022d16SRodney W. Grimes lreply(211, "%s FTP server status:", hostname, version); 1919ea022d16SRodney W. Grimes printf(" %s\r\n", version); 1920ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost); 19214dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, 19224dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 19234dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID)) { 19244dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0) 19254dd8b5abSYoshinobu Inoue printf(" (%s)", hname); 19264dd8b5abSYoshinobu Inoue } 1927ea022d16SRodney W. Grimes printf("\r\n"); 1928ea022d16SRodney W. Grimes if (logged_in) { 1929ea022d16SRodney W. Grimes if (guest) 1930ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n"); 1931ea022d16SRodney W. Grimes else 1932ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name); 1933ea022d16SRodney W. Grimes } else if (askpasswd) 1934ea022d16SRodney W. Grimes printf(" Waiting for password\r\n"); 1935ea022d16SRodney W. Grimes else 1936ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n"); 1937ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]); 1938ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E) 1939ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]); 1940ea022d16SRodney W. Grimes if (type == TYPE_L) 1941ea022d16SRodney W. Grimes #if NBBY == 8 1942ea022d16SRodney W. Grimes printf(" %d", NBBY); 1943ea022d16SRodney W. Grimes #else 1944ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */ 1945ea022d16SRodney W. Grimes #endif 1946ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n", 1947ea022d16SRodney W. Grimes strunames[stru], modenames[mode]); 1948ea022d16SRodney W. Grimes if (data != -1) 1949ea022d16SRodney W. Grimes printf(" Data connection open\r\n"); 1950ea022d16SRodney W. Grimes else if (pdata != -1) { 19514dd8b5abSYoshinobu Inoue ispassive = 1; 19524dd8b5abSYoshinobu Inoue su = &pasv_addr; 1953ea022d16SRodney W. Grimes goto printaddr; 1954ea022d16SRodney W. Grimes } else if (usedefault == 0) { 19554dd8b5abSYoshinobu Inoue ispassive = 0; 19564dd8b5abSYoshinobu Inoue su = &data_dest; 1957ea022d16SRodney W. Grimes printaddr: 1958ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 19594dd8b5abSYoshinobu Inoue if (epsvall) { 19604dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n"); 19614dd8b5abSYoshinobu Inoue goto epsvonly; 19624dd8b5abSYoshinobu Inoue } 19634dd8b5abSYoshinobu Inoue 19644dd8b5abSYoshinobu Inoue /* PORT/PASV */ 19654dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) { 19664dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 19674dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 19684dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", 19694dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT", 19704dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 19714dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1])); 19724dd8b5abSYoshinobu Inoue } 19734dd8b5abSYoshinobu Inoue 19744dd8b5abSYoshinobu Inoue /* LPRT/LPSV */ 19754dd8b5abSYoshinobu Inoue { 19764dd8b5abSYoshinobu Inoue int alen, af, i; 19774dd8b5abSYoshinobu Inoue 19784dd8b5abSYoshinobu Inoue switch (su->su_family) { 19794dd8b5abSYoshinobu Inoue case AF_INET: 19804dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr; 19814dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port; 19824dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr); 19834dd8b5abSYoshinobu Inoue af = 4; 19844dd8b5abSYoshinobu Inoue break; 19854dd8b5abSYoshinobu Inoue case AF_INET6: 19864dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr; 19874dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port; 19884dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr); 19894dd8b5abSYoshinobu Inoue af = 6; 19904dd8b5abSYoshinobu Inoue break; 19914dd8b5abSYoshinobu Inoue default: 19924dd8b5abSYoshinobu Inoue af = 0; 19934dd8b5abSYoshinobu Inoue break; 19944dd8b5abSYoshinobu Inoue } 19954dd8b5abSYoshinobu Inoue if (af) { 19964dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", 19974dd8b5abSYoshinobu Inoue af, alen); 19984dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++) 19994dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i])); 20004dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); 20014dd8b5abSYoshinobu Inoue } 20024dd8b5abSYoshinobu Inoue } 20034dd8b5abSYoshinobu Inoue 20044dd8b5abSYoshinobu Inoue epsvonly:; 20054dd8b5abSYoshinobu Inoue /* EPRT/EPSV */ 20064dd8b5abSYoshinobu Inoue { 20074dd8b5abSYoshinobu Inoue int af; 20084dd8b5abSYoshinobu Inoue 20094dd8b5abSYoshinobu Inoue switch (su->su_family) { 20104dd8b5abSYoshinobu Inoue case AF_INET: 20114dd8b5abSYoshinobu Inoue af = 1; 20124dd8b5abSYoshinobu Inoue break; 20134dd8b5abSYoshinobu Inoue case AF_INET6: 20144dd8b5abSYoshinobu Inoue af = 2; 20154dd8b5abSYoshinobu Inoue break; 20164dd8b5abSYoshinobu Inoue default: 20174dd8b5abSYoshinobu Inoue af = 0; 20184dd8b5abSYoshinobu Inoue break; 20194dd8b5abSYoshinobu Inoue } 20204dd8b5abSYoshinobu Inoue if (af) { 20214dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)su, su->su_len, 20224dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0, 20234dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) { 20244dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n", 20254dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT", 20264dd8b5abSYoshinobu Inoue af, hname, htons(su->su_port)); 20274dd8b5abSYoshinobu Inoue } 20284dd8b5abSYoshinobu Inoue } 20294dd8b5abSYoshinobu Inoue } 2030ea022d16SRodney W. Grimes #undef UC 2031ea022d16SRodney W. Grimes } else 2032ea022d16SRodney W. Grimes printf(" No data connection\r\n"); 2033ea022d16SRodney W. Grimes reply(211, "End of status"); 2034ea022d16SRodney W. Grimes } 2035ea022d16SRodney W. Grimes 2036ea022d16SRodney W. Grimes void 2037ea022d16SRodney W. Grimes fatal(s) 2038ea022d16SRodney W. Grimes char *s; 2039ea022d16SRodney W. Grimes { 2040ea022d16SRodney W. Grimes 2041ea022d16SRodney W. Grimes reply(451, "Error in server: %s\n", s); 2042ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error."); 2043ea022d16SRodney W. Grimes dologout(0); 2044ea022d16SRodney W. Grimes /* NOTREACHED */ 2045ea022d16SRodney W. Grimes } 2046ea022d16SRodney W. Grimes 2047ea022d16SRodney W. Grimes void 2048ea022d16SRodney W. Grimes #if __STDC__ 2049ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...) 2050ea022d16SRodney W. Grimes #else 2051ea022d16SRodney W. Grimes reply(n, fmt, va_alist) 2052ea022d16SRodney W. Grimes int n; 2053ea022d16SRodney W. Grimes char *fmt; 2054ea022d16SRodney W. Grimes va_dcl 2055ea022d16SRodney W. Grimes #endif 2056ea022d16SRodney W. Grimes { 2057ea022d16SRodney W. Grimes va_list ap; 2058ea022d16SRodney W. Grimes #if __STDC__ 2059ea022d16SRodney W. Grimes va_start(ap, fmt); 2060ea022d16SRodney W. Grimes #else 2061ea022d16SRodney W. Grimes va_start(ap); 2062ea022d16SRodney W. Grimes #endif 2063ea022d16SRodney W. Grimes (void)printf("%d ", n); 2064ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2065ea022d16SRodney W. Grimes (void)printf("\r\n"); 2066ea022d16SRodney W. Grimes (void)fflush(stdout); 2067ea022d16SRodney W. Grimes if (debug) { 2068ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n); 2069ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2070ea022d16SRodney W. Grimes } 2071ea022d16SRodney W. Grimes } 2072ea022d16SRodney W. Grimes 2073ea022d16SRodney W. Grimes void 2074ea022d16SRodney W. Grimes #if __STDC__ 2075ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...) 2076ea022d16SRodney W. Grimes #else 2077ea022d16SRodney W. Grimes lreply(n, fmt, va_alist) 2078ea022d16SRodney W. Grimes int n; 2079ea022d16SRodney W. Grimes char *fmt; 2080ea022d16SRodney W. Grimes va_dcl 2081ea022d16SRodney W. Grimes #endif 2082ea022d16SRodney W. Grimes { 2083ea022d16SRodney W. Grimes va_list ap; 2084ea022d16SRodney W. Grimes #if __STDC__ 2085ea022d16SRodney W. Grimes va_start(ap, fmt); 2086ea022d16SRodney W. Grimes #else 2087ea022d16SRodney W. Grimes va_start(ap); 2088ea022d16SRodney W. Grimes #endif 2089ea022d16SRodney W. Grimes (void)printf("%d- ", n); 2090ea022d16SRodney W. Grimes (void)vprintf(fmt, ap); 2091ea022d16SRodney W. Grimes (void)printf("\r\n"); 2092ea022d16SRodney W. Grimes (void)fflush(stdout); 2093ea022d16SRodney W. Grimes if (debug) { 2094ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n); 2095ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap); 2096ea022d16SRodney W. Grimes } 2097ea022d16SRodney W. Grimes } 2098ea022d16SRodney W. Grimes 2099ea022d16SRodney W. Grimes static void 2100ea022d16SRodney W. Grimes ack(s) 2101ea022d16SRodney W. Grimes char *s; 2102ea022d16SRodney W. Grimes { 2103ea022d16SRodney W. Grimes 2104ea022d16SRodney W. Grimes reply(250, "%s command successful.", s); 2105ea022d16SRodney W. Grimes } 2106ea022d16SRodney W. Grimes 2107ea022d16SRodney W. Grimes void 2108ea022d16SRodney W. Grimes nack(s) 2109ea022d16SRodney W. Grimes char *s; 2110ea022d16SRodney W. Grimes { 2111ea022d16SRodney W. Grimes 2112ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s); 2113ea022d16SRodney W. Grimes } 2114ea022d16SRodney W. Grimes 2115ea022d16SRodney W. Grimes /* ARGSUSED */ 2116ea022d16SRodney W. Grimes void 2117ea022d16SRodney W. Grimes yyerror(s) 2118ea022d16SRodney W. Grimes char *s; 2119ea022d16SRodney W. Grimes { 2120ea022d16SRodney W. Grimes char *cp; 2121ea022d16SRodney W. Grimes 21229aca17cbSMark Murray if ((cp = strchr(cbuf,'\n'))) 2123ea022d16SRodney W. Grimes *cp = '\0'; 2124ea022d16SRodney W. Grimes reply(500, "'%s': command not understood.", cbuf); 2125ea022d16SRodney W. Grimes } 2126ea022d16SRodney W. Grimes 2127ea022d16SRodney W. Grimes void 2128ea022d16SRodney W. Grimes delete(name) 2129ea022d16SRodney W. Grimes char *name; 2130ea022d16SRodney W. Grimes { 2131ea022d16SRodney W. Grimes struct stat st; 2132ea022d16SRodney W. Grimes 2133ea022d16SRodney W. Grimes LOGCMD("delete", name); 2134ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 2135ea022d16SRodney W. Grimes perror_reply(550, name); 2136ea022d16SRodney W. Grimes return; 2137ea022d16SRodney W. Grimes } 2138ea022d16SRodney W. Grimes if ((st.st_mode&S_IFMT) == S_IFDIR) { 2139ea022d16SRodney W. Grimes if (rmdir(name) < 0) { 2140ea022d16SRodney W. Grimes perror_reply(550, name); 2141ea022d16SRodney W. Grimes return; 2142ea022d16SRodney W. Grimes } 2143ea022d16SRodney W. Grimes goto done; 2144ea022d16SRodney W. Grimes } 2145ea022d16SRodney W. Grimes if (unlink(name) < 0) { 2146ea022d16SRodney W. Grimes perror_reply(550, name); 2147ea022d16SRodney W. Grimes return; 2148ea022d16SRodney W. Grimes } 2149ea022d16SRodney W. Grimes done: 2150ea022d16SRodney W. Grimes ack("DELE"); 2151ea022d16SRodney W. Grimes } 2152ea022d16SRodney W. Grimes 2153ea022d16SRodney W. Grimes void 2154ea022d16SRodney W. Grimes cwd(path) 2155ea022d16SRodney W. Grimes char *path; 2156ea022d16SRodney W. Grimes { 2157ea022d16SRodney W. Grimes 2158ea022d16SRodney W. Grimes if (chdir(path) < 0) 2159ea022d16SRodney W. Grimes perror_reply(550, path); 2160ea022d16SRodney W. Grimes else 2161ea022d16SRodney W. Grimes ack("CWD"); 2162ea022d16SRodney W. Grimes } 2163ea022d16SRodney W. Grimes 2164ea022d16SRodney W. Grimes void 2165ea022d16SRodney W. Grimes makedir(name) 2166ea022d16SRodney W. Grimes char *name; 2167ea022d16SRodney W. Grimes { 2168ea022d16SRodney W. Grimes 2169ea022d16SRodney W. Grimes LOGCMD("mkdir", name); 2170ea022d16SRodney W. Grimes if (mkdir(name, 0777) < 0) 2171ea022d16SRodney W. Grimes perror_reply(550, name); 2172ea022d16SRodney W. Grimes else 2173ea022d16SRodney W. Grimes reply(257, "MKD command successful."); 2174ea022d16SRodney W. Grimes } 2175ea022d16SRodney W. Grimes 2176ea022d16SRodney W. Grimes void 2177ea022d16SRodney W. Grimes removedir(name) 2178ea022d16SRodney W. Grimes char *name; 2179ea022d16SRodney W. Grimes { 2180ea022d16SRodney W. Grimes 2181ea022d16SRodney W. Grimes LOGCMD("rmdir", name); 2182ea022d16SRodney W. Grimes if (rmdir(name) < 0) 2183ea022d16SRodney W. Grimes perror_reply(550, name); 2184ea022d16SRodney W. Grimes else 2185ea022d16SRodney W. Grimes ack("RMD"); 2186ea022d16SRodney W. Grimes } 2187ea022d16SRodney W. Grimes 2188ea022d16SRodney W. Grimes void 2189ea022d16SRodney W. Grimes pwd() 2190ea022d16SRodney W. Grimes { 2191ea022d16SRodney W. Grimes char path[MAXPATHLEN + 1]; 2192ea022d16SRodney W. Grimes 2193ea022d16SRodney W. Grimes if (getwd(path) == (char *)NULL) 2194ea022d16SRodney W. Grimes reply(550, "%s.", path); 2195ea022d16SRodney W. Grimes else 2196ea022d16SRodney W. Grimes reply(257, "\"%s\" is current directory.", path); 2197ea022d16SRodney W. Grimes } 2198ea022d16SRodney W. Grimes 2199ea022d16SRodney W. Grimes char * 2200ea022d16SRodney W. Grimes renamefrom(name) 2201ea022d16SRodney W. Grimes char *name; 2202ea022d16SRodney W. Grimes { 2203ea022d16SRodney W. Grimes struct stat st; 2204ea022d16SRodney W. Grimes 2205ea022d16SRodney W. Grimes if (stat(name, &st) < 0) { 2206ea022d16SRodney W. Grimes perror_reply(550, name); 2207ea022d16SRodney W. Grimes return ((char *)0); 2208ea022d16SRodney W. Grimes } 2209ea022d16SRodney W. Grimes reply(350, "File exists, ready for destination name"); 2210ea022d16SRodney W. Grimes return (name); 2211ea022d16SRodney W. Grimes } 2212ea022d16SRodney W. Grimes 2213ea022d16SRodney W. Grimes void 2214ea022d16SRodney W. Grimes renamecmd(from, to) 2215ea022d16SRodney W. Grimes char *from, *to; 2216ea022d16SRodney W. Grimes { 221761f891a6SPaul Traina struct stat st; 2218ea022d16SRodney W. Grimes 2219ea022d16SRodney W. Grimes LOGCMD2("rename", from, to); 222061f891a6SPaul Traina 222161f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) { 222261f891a6SPaul Traina reply(550, "%s: permission denied", to); 222361f891a6SPaul Traina return; 222461f891a6SPaul Traina } 222561f891a6SPaul Traina 2226ea022d16SRodney W. Grimes if (rename(from, to) < 0) 2227ea022d16SRodney W. Grimes perror_reply(550, "rename"); 2228ea022d16SRodney W. Grimes else 2229ea022d16SRodney W. Grimes ack("RNTO"); 2230ea022d16SRodney W. Grimes } 2231ea022d16SRodney W. Grimes 2232ea022d16SRodney W. Grimes static void 22334dd8b5abSYoshinobu Inoue dolog(who) 22344dd8b5abSYoshinobu Inoue struct sockaddr *who; 2235ea022d16SRodney W. Grimes { 22364dd8b5abSYoshinobu Inoue int error; 22374dd8b5abSYoshinobu Inoue 22384dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); 2239ea022d16SRodney W. Grimes 2240ea022d16SRodney W. Grimes #ifdef SETPROCTITLE 2241ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2242ea4e54b9SDavid Nugent if (thishost != firsthost) 2243ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", 2244ea4e54b9SDavid Nugent remotehost, hostname); 2245ea4e54b9SDavid Nugent else 2246ea4e54b9SDavid Nugent #endif 2247ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected", 2248ea4e54b9SDavid Nugent remotehost); 2249b63e1fe2SPeter Wemm setproctitle("%s", proctitle); 2250ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */ 2251ea022d16SRodney W. Grimes 2252ea4e54b9SDavid Nugent if (logging) { 2253ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING 2254ea4e54b9SDavid Nugent if (thishost != firsthost) 2255ea4e54b9SDavid Nugent syslog(LOG_INFO, "connection from %s (to %s)", 2256ea4e54b9SDavid Nugent remotehost, hostname); 2257ea4e54b9SDavid Nugent else 2258ea4e54b9SDavid Nugent #endif 22594dd8b5abSYoshinobu Inoue { 22604dd8b5abSYoshinobu Inoue char who_name[MAXHOSTNAMELEN]; 22614dd8b5abSYoshinobu Inoue 22624dd8b5abSYoshinobu Inoue error = getnameinfo(who, who->sa_len, 22634dd8b5abSYoshinobu Inoue who_name, sizeof(who_name) - 1, 22644dd8b5abSYoshinobu Inoue NULL, 0, 22654dd8b5abSYoshinobu Inoue NI_NUMERICHOST|NI_WITHSCOPEID); 2266f5c57d05SEivind Eklund syslog(LOG_INFO, "connection from %s (%s)", remotehost, 22674dd8b5abSYoshinobu Inoue error == 0 ? who_name : ""); 22684dd8b5abSYoshinobu Inoue } 2269ea022d16SRodney W. Grimes } 2270ea4e54b9SDavid Nugent } 2271ea022d16SRodney W. Grimes 2272ea022d16SRodney W. Grimes /* 2273ea022d16SRodney W. Grimes * Record logout in wtmp file 2274ea022d16SRodney W. Grimes * and exit with supplied status. 2275ea022d16SRodney W. Grimes */ 2276ea022d16SRodney W. Grimes void 2277ea022d16SRodney W. Grimes dologout(status) 2278ea022d16SRodney W. Grimes int status; 2279ea022d16SRodney W. Grimes { 22800b4df2eeSDavid Greenman /* 22810b4df2eeSDavid Greenman * Prevent reception of SIGURG from resulting in a resumption 22820b4df2eeSDavid Greenman * back to the main program loop. 22830b4df2eeSDavid Greenman */ 22840b4df2eeSDavid Greenman transflag = 0; 2285ea022d16SRodney W. Grimes 2286ea022d16SRodney W. Grimes if (logged_in) { 2287ea022d16SRodney W. Grimes (void) seteuid((uid_t)0); 2288986a1172SThomas Gellekum ftpd_logwtmp(ttyline, "", ""); 2289ea022d16SRodney W. Grimes } 2290ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */ 2291ea022d16SRodney W. Grimes _exit(status); 2292ea022d16SRodney W. Grimes } 2293ea022d16SRodney W. Grimes 2294ea022d16SRodney W. Grimes static void 2295ea022d16SRodney W. Grimes myoob(signo) 2296ea022d16SRodney W. Grimes int signo; 2297ea022d16SRodney W. Grimes { 2298ea022d16SRodney W. Grimes char *cp; 2299ea022d16SRodney W. Grimes 2300ea022d16SRodney W. Grimes /* only process if transfer occurring */ 2301ea022d16SRodney W. Grimes if (!transflag) 2302ea022d16SRodney W. Grimes return; 2303ea022d16SRodney W. Grimes cp = tmpline; 2304ea022d16SRodney W. Grimes if (getline(cp, 7, stdin) == NULL) { 2305ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye."); 2306ea022d16SRodney W. Grimes dologout(0); 2307ea022d16SRodney W. Grimes } 2308ea022d16SRodney W. Grimes upper(cp); 2309ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) { 2310ea022d16SRodney W. Grimes tmpline[0] = '\0'; 2311ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed."); 2312ea022d16SRodney W. Grimes reply(226, "Abort successful"); 2313ea022d16SRodney W. Grimes longjmp(urgcatch, 1); 2314ea022d16SRodney W. Grimes } 2315ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) { 23169db4bbf3SMichael Haro tmpline[0] = '\0'; 2317ea022d16SRodney W. Grimes if (file_size != (off_t) -1) 2318ea022d16SRodney W. Grimes reply(213, "Status: %qd of %qd bytes transferred", 2319ea022d16SRodney W. Grimes byte_count, file_size); 2320ea022d16SRodney W. Grimes else 2321ea022d16SRodney W. Grimes reply(213, "Status: %qd bytes transferred", byte_count); 2322ea022d16SRodney W. Grimes } 2323ea022d16SRodney W. Grimes } 2324ea022d16SRodney W. Grimes 2325ea022d16SRodney W. Grimes /* 2326ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to 2327ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as 2328ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation 2329ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89. 2330ea022d16SRodney W. Grimes */ 2331ea022d16SRodney W. Grimes void 2332ea022d16SRodney W. Grimes passive() 2333ea022d16SRodney W. Grimes { 2334dacc9752SPaul Traina int len; 2335ea022d16SRodney W. Grimes char *p, *a; 2336ea022d16SRodney W. Grimes 233761f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */ 233861f891a6SPaul Traina close(pdata); 233961f891a6SPaul Traina 23404dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 2341ea022d16SRodney W. Grimes if (pdata < 0) { 2342ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2343ea022d16SRodney W. Grimes return; 2344ea022d16SRodney W. Grimes } 23454c450ad7SPaul Traina 23464c450ad7SPaul Traina (void) seteuid((uid_t)0); 234761f891a6SPaul Traina 2348dacc9752SPaul Traina #ifdef IP_PORTRANGE 23494dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) { 2350dacc9752SPaul Traina int on = restricted_data_ports ? IP_PORTRANGE_HIGH 2351dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT; 2352dacc9752SPaul Traina 235340e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 2354dacc9752SPaul Traina (char *)&on, sizeof(on)) < 0) 23554c450ad7SPaul Traina goto pasv_error; 23564c450ad7SPaul Traina } 2357dacc9752SPaul Traina #endif 23582db39860SNick Sayer #ifdef IPV6_PORTRANGE 23592db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 23602db39860SNick Sayer int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 23612db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 23622db39860SNick Sayer 23632db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 23642db39860SNick Sayer (char *)&on, sizeof(on)) < 0) 23652db39860SNick Sayer goto pasv_error; 23662db39860SNick Sayer } 23672db39860SNick Sayer #endif 236840e9d39eSPeter Wemm 2369ea022d16SRodney W. Grimes pasv_addr = ctrl_addr; 23704dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 23714dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) 2372ea022d16SRodney W. Grimes goto pasv_error; 237361f891a6SPaul Traina 2374ea022d16SRodney W. Grimes (void) seteuid((uid_t)pw->pw_uid); 23754c450ad7SPaul Traina 2376ea022d16SRodney W. Grimes len = sizeof(pasv_addr); 2377ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 2378ea022d16SRodney W. Grimes goto pasv_error; 2379ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0) 2380ea022d16SRodney W. Grimes goto pasv_error; 23814dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET) 23824dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 23834dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 && 23844dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) 23854dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 23864dd8b5abSYoshinobu Inoue else 23874dd8b5abSYoshinobu Inoue goto pasv_error; 23884dd8b5abSYoshinobu Inoue 23894dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port; 2390ea022d16SRodney W. Grimes 2391ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff) 2392ea022d16SRodney W. Grimes 2393ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), 2394ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); 2395ea022d16SRodney W. Grimes return; 2396ea022d16SRodney W. Grimes 2397ea022d16SRodney W. Grimes pasv_error: 239861f891a6SPaul Traina (void) seteuid((uid_t)pw->pw_uid); 2399ea022d16SRodney W. Grimes (void) close(pdata); 2400ea022d16SRodney W. Grimes pdata = -1; 2401ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection"); 2402ea022d16SRodney W. Grimes return; 2403ea022d16SRodney W. Grimes } 2404ea022d16SRodney W. Grimes 2405ea022d16SRodney W. Grimes /* 24064dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639. 24074dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode 24084dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...) 24094dd8b5abSYoshinobu Inoue */ 24104dd8b5abSYoshinobu Inoue 24114dd8b5abSYoshinobu Inoue void 24124dd8b5abSYoshinobu Inoue long_passive(cmd, pf) 24134dd8b5abSYoshinobu Inoue char *cmd; 24144dd8b5abSYoshinobu Inoue int pf; 24154dd8b5abSYoshinobu Inoue { 24164dd8b5abSYoshinobu Inoue int len; 24174dd8b5abSYoshinobu Inoue char *p, *a; 24184dd8b5abSYoshinobu Inoue 24194dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */ 24204dd8b5abSYoshinobu Inoue close(pdata); 24214dd8b5abSYoshinobu Inoue 24224dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) { 24234dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) { 24244dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) { 24254dd8b5abSYoshinobu Inoue case AF_INET: 24264dd8b5abSYoshinobu Inoue pf = 1; 24274dd8b5abSYoshinobu Inoue break; 24284dd8b5abSYoshinobu Inoue case AF_INET6: 24294dd8b5abSYoshinobu Inoue pf = 2; 24304dd8b5abSYoshinobu Inoue break; 24314dd8b5abSYoshinobu Inoue default: 24324dd8b5abSYoshinobu Inoue pf = 0; 24334dd8b5abSYoshinobu Inoue break; 24344dd8b5abSYoshinobu Inoue } 24354dd8b5abSYoshinobu Inoue /* 24364dd8b5abSYoshinobu Inoue * XXX 24374dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this 24384dd8b5abSYoshinobu Inoue */ 24394dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) { 24404dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, " 24414dd8b5abSYoshinobu Inoue "use (%d)", pf); 24424dd8b5abSYoshinobu Inoue } else 24434dd8b5abSYoshinobu Inoue reply(501, "Network protocol mismatch"); /*XXX*/ 24444dd8b5abSYoshinobu Inoue 24454dd8b5abSYoshinobu Inoue return; 24464dd8b5abSYoshinobu Inoue } 24474dd8b5abSYoshinobu Inoue } 24484dd8b5abSYoshinobu Inoue 24494dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); 24504dd8b5abSYoshinobu Inoue if (pdata < 0) { 24514dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 24524dd8b5abSYoshinobu Inoue return; 24534dd8b5abSYoshinobu Inoue } 24544dd8b5abSYoshinobu Inoue 24554dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)0); 24564dd8b5abSYoshinobu Inoue 24574dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr; 24584dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0; 24594dd8b5abSYoshinobu Inoue len = pasv_addr.su_len; 24604dd8b5abSYoshinobu Inoue 24612db39860SNick Sayer #ifdef IP_PORTRANGE 24622db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) { 24632db39860SNick Sayer int on = restricted_data_ports ? IP_PORTRANGE_HIGH 24642db39860SNick Sayer : IP_PORTRANGE_DEFAULT; 24652db39860SNick Sayer 24662db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, 24672db39860SNick Sayer (char *)&on, sizeof(on)) < 0) 24682db39860SNick Sayer goto pasv_error; 24692db39860SNick Sayer } 24702db39860SNick Sayer #endif 24712db39860SNick Sayer #ifdef IPV6_PORTRANGE 24722db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) { 24732db39860SNick Sayer int on = restricted_data_ports ? IPV6_PORTRANGE_HIGH 24742db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT; 24752db39860SNick Sayer 24762db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, 24772db39860SNick Sayer (char *)&on, sizeof(on)) < 0) 24782db39860SNick Sayer goto pasv_error; 24792db39860SNick Sayer } 24802db39860SNick Sayer #endif 24812db39860SNick Sayer 24824dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) 24834dd8b5abSYoshinobu Inoue goto pasv_error; 24844dd8b5abSYoshinobu Inoue 24854dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 24864dd8b5abSYoshinobu Inoue 24874dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) 24884dd8b5abSYoshinobu Inoue goto pasv_error; 24894dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0) 24904dd8b5abSYoshinobu Inoue goto pasv_error; 24914dd8b5abSYoshinobu Inoue 24924dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff) 24934dd8b5abSYoshinobu Inoue 24944dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) { 24954dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port; 24964dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 24974dd8b5abSYoshinobu Inoue case AF_INET: 24984dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr; 24994dd8b5abSYoshinobu Inoue v4_reply: 25004dd8b5abSYoshinobu Inoue reply(228, 25014dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 25024dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 25034dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 25044dd8b5abSYoshinobu Inoue return; 25054dd8b5abSYoshinobu Inoue case AF_INET6: 25064dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { 25074dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; 25084dd8b5abSYoshinobu Inoue goto v4_reply; 25094dd8b5abSYoshinobu Inoue } 25104dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr; 25114dd8b5abSYoshinobu Inoue reply(228, 25124dd8b5abSYoshinobu Inoue "Entering Long Passive Mode " 25134dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 25144dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 25154dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), 25164dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), 25174dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 25184dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1])); 25194dd8b5abSYoshinobu Inoue return; 25204dd8b5abSYoshinobu Inoue } 25214dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) { 25224dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) { 25234dd8b5abSYoshinobu Inoue case AF_INET: 25244dd8b5abSYoshinobu Inoue case AF_INET6: 25254dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)", 25264dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port)); 25274dd8b5abSYoshinobu Inoue return; 25284dd8b5abSYoshinobu Inoue } 25294dd8b5abSYoshinobu Inoue } else { 25304dd8b5abSYoshinobu Inoue /* more proper error code? */ 25314dd8b5abSYoshinobu Inoue } 25324dd8b5abSYoshinobu Inoue 25334dd8b5abSYoshinobu Inoue pasv_error: 25344dd8b5abSYoshinobu Inoue (void) seteuid((uid_t)pw->pw_uid); 25354dd8b5abSYoshinobu Inoue (void) close(pdata); 25364dd8b5abSYoshinobu Inoue pdata = -1; 25374dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection"); 25384dd8b5abSYoshinobu Inoue return; 25394dd8b5abSYoshinobu Inoue } 25404dd8b5abSYoshinobu Inoue 25414dd8b5abSYoshinobu Inoue /* 2542ea022d16SRodney W. Grimes * Generate unique name for file with basename "local". 2543ea022d16SRodney W. Grimes * The file named "local" is already known to exist. 2544ea022d16SRodney W. Grimes * Generates failure reply on error. 2545ea022d16SRodney W. Grimes */ 2546ea022d16SRodney W. Grimes static char * 2547ea022d16SRodney W. Grimes gunique(local) 2548ea022d16SRodney W. Grimes char *local; 2549ea022d16SRodney W. Grimes { 2550ea022d16SRodney W. Grimes static char new[MAXPATHLEN]; 2551ea022d16SRodney W. Grimes struct stat st; 2552ea022d16SRodney W. Grimes int count; 2553ea022d16SRodney W. Grimes char *cp; 2554ea022d16SRodney W. Grimes 2555ea022d16SRodney W. Grimes cp = strrchr(local, '/'); 2556ea022d16SRodney W. Grimes if (cp) 2557ea022d16SRodney W. Grimes *cp = '\0'; 2558ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) { 2559ea022d16SRodney W. Grimes perror_reply(553, cp ? local : "."); 2560ea022d16SRodney W. Grimes return ((char *) 0); 2561ea022d16SRodney W. Grimes } 2562ea022d16SRodney W. Grimes if (cp) 2563ea022d16SRodney W. Grimes *cp = '/'; 2564e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */ 2565e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local); 2566ea022d16SRodney W. Grimes cp = new + strlen(new); 2567ea022d16SRodney W. Grimes *cp++ = '.'; 2568ea022d16SRodney W. Grimes for (count = 1; count < 100; count++) { 2569ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count); 2570ea022d16SRodney W. Grimes if (stat(new, &st) < 0) 2571ea022d16SRodney W. Grimes return (new); 2572ea022d16SRodney W. Grimes } 2573ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created."); 2574ea022d16SRodney W. Grimes return (NULL); 2575ea022d16SRodney W. Grimes } 2576ea022d16SRodney W. Grimes 2577ea022d16SRodney W. Grimes /* 2578ea022d16SRodney W. Grimes * Format and send reply containing system error number. 2579ea022d16SRodney W. Grimes */ 2580ea022d16SRodney W. Grimes void 2581ea022d16SRodney W. Grimes perror_reply(code, string) 2582ea022d16SRodney W. Grimes int code; 2583ea022d16SRodney W. Grimes char *string; 2584ea022d16SRodney W. Grimes { 2585ea022d16SRodney W. Grimes 2586ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno)); 2587ea022d16SRodney W. Grimes } 2588ea022d16SRodney W. Grimes 2589ea022d16SRodney W. Grimes static char *onefile[] = { 2590ea022d16SRodney W. Grimes "", 2591ea022d16SRodney W. Grimes 0 2592ea022d16SRodney W. Grimes }; 2593ea022d16SRodney W. Grimes 2594ea022d16SRodney W. Grimes void 2595ea022d16SRodney W. Grimes send_file_list(whichf) 2596ea022d16SRodney W. Grimes char *whichf; 2597ea022d16SRodney W. Grimes { 2598ea022d16SRodney W. Grimes struct stat st; 2599ea022d16SRodney W. Grimes DIR *dirp = NULL; 2600ea022d16SRodney W. Grimes struct dirent *dir; 2601ea022d16SRodney W. Grimes FILE *dout = NULL; 2602ea022d16SRodney W. Grimes char **dirlist, *dirname; 2603ea022d16SRodney W. Grimes int simple = 0; 2604ea022d16SRodney W. Grimes int freeglob = 0; 2605ea022d16SRodney W. Grimes glob_t gl; 2606ea022d16SRodney W. Grimes 2607ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) { 2608ea022d16SRodney W. Grimes int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 2609ea022d16SRodney W. Grimes 2610ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 2611ea022d16SRodney W. Grimes freeglob = 1; 2612ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) { 2613ea022d16SRodney W. Grimes reply(550, "not found"); 2614ea022d16SRodney W. Grimes goto out; 2615ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) { 2616ea022d16SRodney W. Grimes errno = ENOENT; 2617ea022d16SRodney W. Grimes perror_reply(550, whichf); 2618ea022d16SRodney W. Grimes goto out; 2619ea022d16SRodney W. Grimes } 2620ea022d16SRodney W. Grimes dirlist = gl.gl_pathv; 2621ea022d16SRodney W. Grimes } else { 2622ea022d16SRodney W. Grimes onefile[0] = whichf; 2623ea022d16SRodney W. Grimes dirlist = onefile; 2624ea022d16SRodney W. Grimes simple = 1; 2625ea022d16SRodney W. Grimes } 2626ea022d16SRodney W. Grimes 2627ea022d16SRodney W. Grimes if (setjmp(urgcatch)) { 2628ea022d16SRodney W. Grimes transflag = 0; 2629ea022d16SRodney W. Grimes goto out; 2630ea022d16SRodney W. Grimes } 26319aca17cbSMark Murray while ((dirname = *dirlist++)) { 2632ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) { 2633ea022d16SRodney W. Grimes /* 2634ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client 2635ea022d16SRodney W. Grimes * used NLST, do what the user meant. 2636ea022d16SRodney W. Grimes */ 2637ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL && 2638ea022d16SRodney W. Grimes transflag == 0) { 2639af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname); 2640ea022d16SRodney W. Grimes goto out; 2641ea022d16SRodney W. Grimes } 2642ea022d16SRodney W. Grimes perror_reply(550, whichf); 2643ea022d16SRodney W. Grimes if (dout != NULL) { 2644ea022d16SRodney W. Grimes (void) fclose(dout); 2645ea022d16SRodney W. Grimes transflag = 0; 2646ea022d16SRodney W. Grimes data = -1; 2647ea022d16SRodney W. Grimes pdata = -1; 2648ea022d16SRodney W. Grimes } 2649ea022d16SRodney W. Grimes goto out; 2650ea022d16SRodney W. Grimes } 2651ea022d16SRodney W. Grimes 2652ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) { 2653ea022d16SRodney W. Grimes if (dout == NULL) { 2654ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, "w"); 2655ea022d16SRodney W. Grimes if (dout == NULL) 2656ea022d16SRodney W. Grimes goto out; 2657ea022d16SRodney W. Grimes transflag++; 2658ea022d16SRodney W. Grimes } 2659ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname, 2660ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2661ea022d16SRodney W. Grimes byte_count += strlen(dirname) + 1; 2662ea022d16SRodney W. Grimes continue; 2663ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode)) 2664ea022d16SRodney W. Grimes continue; 2665ea022d16SRodney W. Grimes 2666ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL) 2667ea022d16SRodney W. Grimes continue; 2668ea022d16SRodney W. Grimes 2669ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) { 2670ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN]; 2671ea022d16SRodney W. Grimes 2672ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1) 2673ea022d16SRodney W. Grimes continue; 2674ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && 2675ea022d16SRodney W. Grimes dir->d_namlen == 2) 2676ea022d16SRodney W. Grimes continue; 2677ea022d16SRodney W. Grimes 2678e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf), 2679e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name); 2680ea022d16SRodney W. Grimes 2681ea022d16SRodney W. Grimes /* 2682ea022d16SRodney W. Grimes * We have to do a stat to insure it's 2683ea022d16SRodney W. Grimes * not a directory or special file. 2684ea022d16SRodney W. Grimes */ 2685ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 && 2686ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) { 2687ea022d16SRodney W. Grimes if (dout == NULL) { 2688ea022d16SRodney W. Grimes dout = dataconn("file list", (off_t)-1, 2689ea022d16SRodney W. Grimes "w"); 2690ea022d16SRodney W. Grimes if (dout == NULL) 2691ea022d16SRodney W. Grimes goto out; 2692ea022d16SRodney W. Grimes transflag++; 2693ea022d16SRodney W. Grimes } 2694ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/') 2695ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2], 2696ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2697ea022d16SRodney W. Grimes else 2698ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf, 2699ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : ""); 2700ea022d16SRodney W. Grimes byte_count += strlen(nbuf) + 1; 2701ea022d16SRodney W. Grimes } 2702ea022d16SRodney W. Grimes } 2703ea022d16SRodney W. Grimes (void) closedir(dirp); 2704ea022d16SRodney W. Grimes } 2705ea022d16SRodney W. Grimes 2706ea022d16SRodney W. Grimes if (dout == NULL) 2707ea022d16SRodney W. Grimes reply(550, "No files found."); 2708ea022d16SRodney W. Grimes else if (ferror(dout) != 0) 2709ea022d16SRodney W. Grimes perror_reply(550, "Data connection"); 2710ea022d16SRodney W. Grimes else 2711ea022d16SRodney W. Grimes reply(226, "Transfer complete."); 2712ea022d16SRodney W. Grimes 2713ea022d16SRodney W. Grimes transflag = 0; 2714ea022d16SRodney W. Grimes if (dout != NULL) 2715ea022d16SRodney W. Grimes (void) fclose(dout); 2716ea022d16SRodney W. Grimes data = -1; 2717ea022d16SRodney W. Grimes pdata = -1; 2718ea022d16SRodney W. Grimes out: 2719ea022d16SRodney W. Grimes if (freeglob) { 2720ea022d16SRodney W. Grimes freeglob = 0; 2721ea022d16SRodney W. Grimes globfree(&gl); 2722ea022d16SRodney W. Grimes } 2723ea022d16SRodney W. Grimes } 2724ea022d16SRodney W. Grimes 2725cf09a206SDavid Greenman void 2726cf09a206SDavid Greenman reapchild(signo) 2727cf09a206SDavid Greenman int signo; 2728cf09a206SDavid Greenman { 2729cf09a206SDavid Greenman while (wait3(NULL, WNOHANG, NULL) > 0); 2730cf09a206SDavid Greenman } 2731cf09a206SDavid Greenman 2732b63e1fe2SPeter Wemm #ifdef OLD_SETPROCTITLE 2733ea022d16SRodney W. Grimes /* 2734ea022d16SRodney W. Grimes * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) 2735ea022d16SRodney W. Grimes * Warning, since this is usually started from inetd.conf, it often doesn't 2736ea022d16SRodney W. Grimes * have much of an environment or arglist to overwrite. 2737ea022d16SRodney W. Grimes */ 2738ea022d16SRodney W. Grimes void 2739ea022d16SRodney W. Grimes #if __STDC__ 2740ea022d16SRodney W. Grimes setproctitle(const char *fmt, ...) 2741ea022d16SRodney W. Grimes #else 2742ea022d16SRodney W. Grimes setproctitle(fmt, va_alist) 2743ea022d16SRodney W. Grimes char *fmt; 2744ea022d16SRodney W. Grimes va_dcl 2745ea022d16SRodney W. Grimes #endif 2746ea022d16SRodney W. Grimes { 2747ea022d16SRodney W. Grimes int i; 2748ea022d16SRodney W. Grimes va_list ap; 2749ea022d16SRodney W. Grimes char *p, *bp, ch; 2750ea022d16SRodney W. Grimes char buf[LINE_MAX]; 2751ea022d16SRodney W. Grimes 2752ea022d16SRodney W. Grimes #if __STDC__ 2753ea022d16SRodney W. Grimes va_start(ap, fmt); 2754ea022d16SRodney W. Grimes #else 2755ea022d16SRodney W. Grimes va_start(ap); 2756ea022d16SRodney W. Grimes #endif 2757ea022d16SRodney W. Grimes (void)vsnprintf(buf, sizeof(buf), fmt, ap); 2758ea022d16SRodney W. Grimes 2759ea022d16SRodney W. Grimes /* make ps print our process name */ 2760ea022d16SRodney W. Grimes p = Argv[0]; 2761ea022d16SRodney W. Grimes *p++ = '-'; 2762ea022d16SRodney W. Grimes 2763ea022d16SRodney W. Grimes i = strlen(buf); 2764ea022d16SRodney W. Grimes if (i > LastArgv - p - 2) { 2765ea022d16SRodney W. Grimes i = LastArgv - p - 2; 2766ea022d16SRodney W. Grimes buf[i] = '\0'; 2767ea022d16SRodney W. Grimes } 2768ea022d16SRodney W. Grimes bp = buf; 2769ea022d16SRodney W. Grimes while (ch = *bp++) 2770ea022d16SRodney W. Grimes if (ch != '\n' && ch != '\r') 2771ea022d16SRodney W. Grimes *p++ = ch; 2772ea022d16SRodney W. Grimes while (p < LastArgv) 2773ea022d16SRodney W. Grimes *p++ = ' '; 2774ea022d16SRodney W. Grimes } 2775b63e1fe2SPeter Wemm #endif /* OLD_SETPROCTITLE */ 27763eb568f2SGuido van Rooij 277761f891a6SPaul Traina static void 27783eb568f2SGuido van Rooij logxfer(name, size, start) 27793eb568f2SGuido van Rooij char *name; 27803eb568f2SGuido van Rooij long size; 27813eb568f2SGuido van Rooij long start; 27823eb568f2SGuido van Rooij { 27833eb568f2SGuido van Rooij char buf[1024]; 27843eb568f2SGuido van Rooij char path[MAXPATHLEN + 1]; 2785158a00b2SJohn Birrell time_t now; 27863eb568f2SGuido van Rooij 27873eb568f2SGuido van Rooij if (statfd >= 0 && getwd(path) != NULL) { 27883eb568f2SGuido van Rooij time(&now); 278961f891a6SPaul Traina snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s/%s!%ld!%ld\n", 27903eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost, 27913eb568f2SGuido van Rooij path, name, size, now - start + (now == start)); 27923eb568f2SGuido van Rooij write(statfd, buf, strlen(buf)); 27933eb568f2SGuido van Rooij } 27943eb568f2SGuido van Rooij } 2795