18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4ea022d16SRodney W. Grimes * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
5ea022d16SRodney W. Grimes * The Regents of the University of California. All rights reserved.
6ea022d16SRodney W. Grimes *
7ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
8ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions
9ea022d16SRodney W. Grimes * are met:
10ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
12ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
13ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
14ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution.
155efaea4cSChristian Brueffer * 3. Neither the name of the University nor the names of its contributors
16ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software
17ea022d16SRodney W. Grimes * without specific prior written permission.
18ea022d16SRodney W. Grimes *
19ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ea022d16SRodney W. Grimes * SUCH DAMAGE.
30ea022d16SRodney W. Grimes */
31ea022d16SRodney W. Grimes
32ea022d16SRodney W. Grimes /*
33ea022d16SRodney W. Grimes * FTP server.
34ea022d16SRodney W. Grimes */
35ea022d16SRodney W. Grimes #include <sys/param.h>
36ea022d16SRodney W. Grimes #include <sys/ioctl.h>
379fc5823aSGarrett Wollman #include <sys/mman.h>
38eb2fc780SGarrett Wollman #include <sys/socket.h>
39eb2fc780SGarrett Wollman #include <sys/stat.h>
40eb2fc780SGarrett Wollman #include <sys/time.h>
41eb2fc780SGarrett Wollman #include <sys/wait.h>
42ea022d16SRodney W. Grimes
43ea022d16SRodney W. Grimes #include <netinet/in.h>
44ea022d16SRodney W. Grimes #include <netinet/in_systm.h>
45ea022d16SRodney W. Grimes #include <netinet/ip.h>
469fc5823aSGarrett Wollman #include <netinet/tcp.h>
47ea022d16SRodney W. Grimes
48ea022d16SRodney W. Grimes #define FTP_NAMES
49ea022d16SRodney W. Grimes #include <arpa/ftp.h>
50ea022d16SRodney W. Grimes #include <arpa/inet.h>
51ea022d16SRodney W. Grimes #include <arpa/telnet.h>
52ea022d16SRodney W. Grimes
53ea022d16SRodney W. Grimes #include <ctype.h>
54ea022d16SRodney W. Grimes #include <dirent.h>
55ea022d16SRodney W. Grimes #include <err.h>
56ea022d16SRodney W. Grimes #include <errno.h>
57ea022d16SRodney W. Grimes #include <fcntl.h>
58ea022d16SRodney W. Grimes #include <glob.h>
59ea022d16SRodney W. Grimes #include <limits.h>
60ea022d16SRodney W. Grimes #include <netdb.h>
61ea022d16SRodney W. Grimes #include <pwd.h>
6231fea7b8SDavid Nugent #include <grp.h>
63ea022d16SRodney W. Grimes #include <signal.h>
64a57e1ef0SYaroslav Tykhiy #include <stdint.h>
65ea022d16SRodney W. Grimes #include <stdio.h>
66ea022d16SRodney W. Grimes #include <stdlib.h>
67ea022d16SRodney W. Grimes #include <string.h>
68ea022d16SRodney W. Grimes #include <syslog.h>
69ea022d16SRodney W. Grimes #include <time.h>
70ea022d16SRodney W. Grimes #include <unistd.h>
71b63e1fe2SPeter Wemm #include <libutil.h>
72b071c689SDavid Nugent #ifdef LOGIN_CAP
73b071c689SDavid Nugent #include <login_cap.h>
74b071c689SDavid Nugent #endif
75ea022d16SRodney W. Grimes
765bc9d93dSMark Murray #ifdef USE_PAM
776c9134c0SMark Murray #include <security/pam_appl.h>
786c9134c0SMark Murray #endif
796c9134c0SMark Murray
803656f229SKurt Lidl #include "blacklist_client.h"
81ea022d16SRodney W. Grimes #include "pathnames.h"
82ea022d16SRodney W. Grimes #include "extern.h"
83ea022d16SRodney W. Grimes
84ea022d16SRodney W. Grimes #include <stdarg.h>
85ea022d16SRodney W. Grimes
86af85d782SDavid Nugent static char version[] = "Version 6.00LS";
87af85d782SDavid Nugent #undef main
88ea022d16SRodney W. Grimes
894dd8b5abSYoshinobu Inoue union sockunion ctrl_addr;
904dd8b5abSYoshinobu Inoue union sockunion data_source;
914dd8b5abSYoshinobu Inoue union sockunion data_dest;
924dd8b5abSYoshinobu Inoue union sockunion his_addr;
934dd8b5abSYoshinobu Inoue union sockunion pasv_addr;
94ea022d16SRodney W. Grimes
95cf09a206SDavid Greenman int daemon_mode;
96ea022d16SRodney W. Grimes int data;
9763591ba5SYaroslav Tykhiy int dataport;
98c152df28SYaroslav Tykhiy int hostinfo = 1; /* print host-specific info in messages */
99ea022d16SRodney W. Grimes int logged_in;
100ea022d16SRodney W. Grimes struct passwd *pw;
101ce9287fcSYaroslav Tykhiy char *homedir;
102618b0bbaSMark Murray int ftpdebug;
103ea022d16SRodney W. Grimes int timeout = 900; /* timeout after 15 minutes of inactivity */
104ea022d16SRodney W. Grimes int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
105ea022d16SRodney W. Grimes int logging;
1064c450ad7SPaul Traina int restricted_data_ports = 1;
107a5a4544eSPaul Traina int paranoid = 1; /* be extra careful about security */
1085a392aecSTorsten Blum int anon_only = 0; /* Only anonymous ftp allowed */
1092ea42282SYaroslav Tykhiy int assumeutf8 = 0; /* Assume that server file names are in UTF-8 */
110ea022d16SRodney W. Grimes int guest;
111a5a4544eSPaul Traina int dochroot;
112215a9f9dSYaroslav Tykhiy char *chrootdir;
1135d7e0128SYaroslav Tykhiy int dowtmp = 1;
1143eb568f2SGuido van Rooij int stats;
1153eb568f2SGuido van Rooij int statfd = -1;
116ea022d16SRodney W. Grimes int type;
117ea022d16SRodney W. Grimes int form;
118ea022d16SRodney W. Grimes int stru; /* avoid C keyword */
119ea022d16SRodney W. Grimes int mode;
120ea022d16SRodney W. Grimes int usedefault = 1; /* for data transfers */
121ea022d16SRodney W. Grimes int pdata = -1; /* for passive mode */
122a4b77a2aSPoul-Henning Kamp int readonly = 0; /* Server is in readonly mode. */
123a4b77a2aSPoul-Henning Kamp int noepsv = 0; /* EPSV command is disabled. */
12462513e76SNik Clayton int noretr = 0; /* RETR command is disabled. */
1251cc9f0bbSSheldon Hearn int noguestretr = 0; /* RETR command is disabled for anon users. */
126d186bb12SMatthew N. Dodd int noguestmkd = 0; /* MKD command is disabled for anon users. */
127a117c345SYaroslav Tykhiy int noguestmod = 1; /* anon users may not modify existing files. */
128e07d11b6SKurt Lidl int use_blacklist = 0;
12962513e76SNik Clayton
130ea022d16SRodney W. Grimes off_t file_size;
131ea022d16SRodney W. Grimes off_t byte_count;
132ea022d16SRodney W. Grimes #if !defined(CMASK) || CMASK == 0
133ea022d16SRodney W. Grimes #undef CMASK
134ea022d16SRodney W. Grimes #define CMASK 027
135ea022d16SRodney W. Grimes #endif
136ea022d16SRodney W. Grimes int defumask = CMASK; /* default umask value */
137ea022d16SRodney W. Grimes char tmpline[7];
138ea4e54b9SDavid Nugent char *hostname;
139ad442344SDima Dorfman int epsvall = 0;
140ad442344SDima Dorfman
1410512556aSDavid Nugent #ifdef VIRTUAL_HOSTING
142ea4e54b9SDavid Nugent char *ftpuser;
143ea4e54b9SDavid Nugent
144ea4e54b9SDavid Nugent static struct ftphost {
145ea4e54b9SDavid Nugent struct ftphost *next;
146f38c6cadSYoshinobu Inoue struct addrinfo *hostinfo;
147ea4e54b9SDavid Nugent char *hostname;
148ea4e54b9SDavid Nugent char *anonuser;
149ea4e54b9SDavid Nugent char *statfile;
150ea4e54b9SDavid Nugent char *welcome;
151ea4e54b9SDavid Nugent char *loginmsg;
152ea4e54b9SDavid Nugent } *thishost, *firsthost;
153ea4e54b9SDavid Nugent
154ea4e54b9SDavid Nugent #endif
15504683b2cSYaroslav Tykhiy char remotehost[NI_MAXHOST];
1563eb568f2SGuido van Rooij char *ident = NULL;
157a5a4544eSPaul Traina
15880643af0SEd Schouten static char wtmpid[20];
159a5a4544eSPaul Traina
1605bc9d93dSMark Murray #ifdef USE_PAM
161e4bc453cSWarner Losh static int auth_pam(struct passwd**, const char*);
1625bc9d93dSMark Murray pam_handle_t *pamh = NULL;
16347499ecdSAndrey A. Chernov #endif
164fa1746c9SMark Murray
165125b9635SYaroslav Tykhiy char *pid_file = NULL; /* means default location to pidfile(3) */
166105a3c98SJulian Elischer
167ea022d16SRodney W. Grimes /*
1686d10cb2fSJonathan Lemon * Limit number of pathnames that glob can return.
1696d10cb2fSJonathan Lemon * A limit of 0 indicates the number of pathnames is unlimited.
1706d10cb2fSJonathan Lemon */
1716d10cb2fSJonathan Lemon #define MAXGLOBARGS 16384
1726d10cb2fSJonathan Lemon #
1736d10cb2fSJonathan Lemon
1746d10cb2fSJonathan Lemon /*
175ea022d16SRodney W. Grimes * Timeout intervals for retrying connections
176ea022d16SRodney W. Grimes * to hosts that don't accept PORT cmds. This
177ea022d16SRodney W. Grimes * is a kludge, but given the problems with TCP...
178ea022d16SRodney W. Grimes */
179ea022d16SRodney W. Grimes #define SWAITMAX 90 /* wait at most 90 seconds */
180ea022d16SRodney W. Grimes #define SWAITINT 5 /* interval between retries */
181ea022d16SRodney W. Grimes
182ea022d16SRodney W. Grimes int swaitmax = SWAITMAX;
183ea022d16SRodney W. Grimes int swaitint = SWAITINT;
184ea022d16SRodney W. Grimes
185ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
186ea022d16SRodney W. Grimes char proctitle[LINE_MAX]; /* initial part of title */
187ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
188ea022d16SRodney W. Grimes
1896b2dee6bSYaroslav Tykhiy #define LOGCMD(cmd, file) logcmd((cmd), (file), NULL, -1)
1906b2dee6bSYaroslav Tykhiy #define LOGCMD2(cmd, file1, file2) logcmd((cmd), (file1), (file2), -1)
1916b2dee6bSYaroslav Tykhiy #define LOGBYTES(cmd, file, cnt) logcmd((cmd), (file), NULL, (cnt))
192ea022d16SRodney W. Grimes
1934cd51076SYaroslav Tykhiy static volatile sig_atomic_t recvurg;
1944cd51076SYaroslav Tykhiy static int transflag; /* NB: for debugging only */
1954cd51076SYaroslav Tykhiy
1964cd51076SYaroslav Tykhiy #define STARTXFER flagxfer(1)
1974cd51076SYaroslav Tykhiy #define ENDXFER flagxfer(0)
1984cd51076SYaroslav Tykhiy
1994cd51076SYaroslav Tykhiy #define START_UNSAFE maskurg(1)
2004cd51076SYaroslav Tykhiy #define END_UNSAFE maskurg(0)
2014cd51076SYaroslav Tykhiy
2024cd51076SYaroslav Tykhiy /* It's OK to put an `else' clause after this macro. */
2034cd51076SYaroslav Tykhiy #define CHECKOOB(action) \
2044cd51076SYaroslav Tykhiy if (recvurg) { \
2054cd51076SYaroslav Tykhiy recvurg = 0; \
2064cd51076SYaroslav Tykhiy if (myoob()) { \
2074cd51076SYaroslav Tykhiy ENDXFER; \
2084cd51076SYaroslav Tykhiy action; \
2094cd51076SYaroslav Tykhiy } \
2104cd51076SYaroslav Tykhiy }
2114cd51076SYaroslav Tykhiy
212ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2132c9fd5f2SHajimu UMEMOTO static void inithosts(int);
214e4bc453cSWarner Losh static void selecthost(union sockunion *);
215ea4e54b9SDavid Nugent #endif
216e4bc453cSWarner Losh static void ack(char *);
217e4bc453cSWarner Losh static void sigurg(int);
2184cd51076SYaroslav Tykhiy static void maskurg(int);
2194cd51076SYaroslav Tykhiy static void flagxfer(int);
2204cd51076SYaroslav Tykhiy static int myoob(void);
221cefb6785SChristian S.J. Peron static int checkuser(char *, char *, int, char **, int *);
222e4bc453cSWarner Losh static FILE *dataconn(char *, off_t, char *);
223e4bc453cSWarner Losh static void dolog(struct sockaddr *);
224e4bc453cSWarner Losh static void end_login(void);
225e4bc453cSWarner Losh static FILE *getdatasock(char *);
226a117c345SYaroslav Tykhiy static int guniquefd(char *, char **);
227e4bc453cSWarner Losh static void lostconn(int);
228e4bc453cSWarner Losh static void sigquit(int);
229e4bc453cSWarner Losh static int receive_data(FILE *, FILE *);
2306e4b0a55SYaroslav Tykhiy static int send_data(FILE *, FILE *, size_t, off_t, int);
231ea022d16SRodney W. Grimes static struct passwd *
232e4bc453cSWarner Losh sgetpwnam(char *);
233e4bc453cSWarner Losh static char *sgetsave(char *);
234e4bc453cSWarner Losh static void reapchild(int);
235eb5b2bb3SYaroslav Tykhiy static void appendf(char **, char *, ...) __printflike(2, 3);
2366b2dee6bSYaroslav Tykhiy static void logcmd(char *, char *, char *, off_t);
237e4bc453cSWarner Losh static void logxfer(char *, off_t, time_t);
238e4648f05SYaroslav Tykhiy static char *doublequote(char *);
239206fe568SHajimu UMEMOTO static int *socksetup(int, char *, const char *);
240ea022d16SRodney W. Grimes
241ea022d16SRodney W. Grimes int
main(int argc,char * argv[],char ** envp)242e4bc453cSWarner Losh main(int argc, char *argv[], char **envp)
243ea022d16SRodney W. Grimes {
24478e3eed0SStefan Farfeleder socklen_t addrlen;
245504422faSKurt Lidl int ch, on = 1, tos, s = STDIN_FILENO;
246ea022d16SRodney W. Grimes char *cp, line[LINE_MAX];
247ea022d16SRodney W. Grimes FILE *fd;
2484dd8b5abSYoshinobu Inoue char *bindname = NULL;
24963591ba5SYaroslav Tykhiy const char *bindport = "ftp";
2504dd8b5abSYoshinobu Inoue int family = AF_UNSPEC;
2514b82fc95SYaroslav Tykhiy struct sigaction sa;
252ea022d16SRodney W. Grimes
25334d1ba5cSAndrey A. Chernov tzset(); /* in case no timezone database in ~ftp */
2544b82fc95SYaroslav Tykhiy sigemptyset(&sa.sa_mask);
2554b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART;
25634d1ba5cSAndrey A. Chernov
2576c98f401SYaroslav Tykhiy /*
2586c98f401SYaroslav Tykhiy * Prevent diagnostic messages from appearing on stderr.
2596c98f401SYaroslav Tykhiy * We run as a daemon or from inetd; in both cases, there's
2606c98f401SYaroslav Tykhiy * more reason in logging to syslog.
2616c98f401SYaroslav Tykhiy */
2626c98f401SYaroslav Tykhiy (void) freopen(_PATH_DEVNULL, "w", stderr);
2636c98f401SYaroslav Tykhiy opterr = 0;
2646c98f401SYaroslav Tykhiy
2656c98f401SYaroslav Tykhiy /*
2666c98f401SYaroslav Tykhiy * LOG_NDELAY sets up the logging connection immediately,
2676c98f401SYaroslav Tykhiy * necessary for anonymous ftp's that chroot and can't do it later.
2686c98f401SYaroslav Tykhiy */
2696c98f401SYaroslav Tykhiy openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
2703eb568f2SGuido van Rooij
271c152df28SYaroslav Tykhiy while ((ch = getopt(argc, argv,
272e07d11b6SKurt Lidl "468a:ABdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
273ea022d16SRodney W. Grimes switch (ch) {
2740e063efeSYaroslav Tykhiy case '4':
275206fe568SHajimu UMEMOTO family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
2760e063efeSYaroslav Tykhiy break;
2770e063efeSYaroslav Tykhiy
2780e063efeSYaroslav Tykhiy case '6':
279206fe568SHajimu UMEMOTO family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
2800e063efeSYaroslav Tykhiy break;
2810e063efeSYaroslav Tykhiy
2822ea42282SYaroslav Tykhiy case '8':
2832ea42282SYaroslav Tykhiy assumeutf8 = 1;
2842ea42282SYaroslav Tykhiy break;
2852ea42282SYaroslav Tykhiy
2860e063efeSYaroslav Tykhiy case 'a':
2870e063efeSYaroslav Tykhiy bindname = optarg;
2880e063efeSYaroslav Tykhiy break;
2890e063efeSYaroslav Tykhiy
2900e063efeSYaroslav Tykhiy case 'A':
2910e063efeSYaroslav Tykhiy anon_only = 1;
292cf09a206SDavid Greenman break;
293cf09a206SDavid Greenman
294e07d11b6SKurt Lidl case 'B':
295e07d11b6SKurt Lidl #ifdef USE_BLACKLIST
296e07d11b6SKurt Lidl use_blacklist = 1;
297e07d11b6SKurt Lidl #else
298e07d11b6SKurt Lidl syslog(LOG_WARNING, "not compiled with USE_BLACKLIST support");
299e07d11b6SKurt Lidl #endif
300e07d11b6SKurt Lidl break;
301e07d11b6SKurt Lidl
302ea022d16SRodney W. Grimes case 'd':
303618b0bbaSMark Murray ftpdebug++;
304ea022d16SRodney W. Grimes break;
305ea022d16SRodney W. Grimes
3060e063efeSYaroslav Tykhiy case 'D':
3070e063efeSYaroslav Tykhiy daemon_mode++;
3080e063efeSYaroslav Tykhiy break;
3090e063efeSYaroslav Tykhiy
310a4b77a2aSPoul-Henning Kamp case 'E':
311a4b77a2aSPoul-Henning Kamp noepsv = 1;
312a4b77a2aSPoul-Henning Kamp break;
313a4b77a2aSPoul-Henning Kamp
314c152df28SYaroslav Tykhiy case 'h':
315c152df28SYaroslav Tykhiy hostinfo = 0;
316c152df28SYaroslav Tykhiy break;
317c152df28SYaroslav Tykhiy
318ea022d16SRodney W. Grimes case 'l':
319ea022d16SRodney W. Grimes logging++; /* > 1 == extra logging */
320ea022d16SRodney W. Grimes break;
321ea022d16SRodney W. Grimes
322a117c345SYaroslav Tykhiy case 'm':
323a117c345SYaroslav Tykhiy noguestmod = 0;
324a117c345SYaroslav Tykhiy break;
325a117c345SYaroslav Tykhiy
3260e063efeSYaroslav Tykhiy case 'M':
3270e063efeSYaroslav Tykhiy noguestmkd = 1;
3280e063efeSYaroslav Tykhiy break;
3290e063efeSYaroslav Tykhiy
3300e063efeSYaroslav Tykhiy case 'o':
3310e063efeSYaroslav Tykhiy noretr = 1;
3320e063efeSYaroslav Tykhiy break;
3330e063efeSYaroslav Tykhiy
3340e063efeSYaroslav Tykhiy case 'O':
3350e063efeSYaroslav Tykhiy noguestretr = 1;
3360e063efeSYaroslav Tykhiy break;
3370e063efeSYaroslav Tykhiy
3380e063efeSYaroslav Tykhiy case 'p':
3390e063efeSYaroslav Tykhiy pid_file = optarg;
3400e063efeSYaroslav Tykhiy break;
3410e063efeSYaroslav Tykhiy
34263591ba5SYaroslav Tykhiy case 'P':
34363591ba5SYaroslav Tykhiy bindport = optarg;
34463591ba5SYaroslav Tykhiy break;
34563591ba5SYaroslav Tykhiy
346a4b77a2aSPoul-Henning Kamp case 'r':
347a4b77a2aSPoul-Henning Kamp readonly = 1;
348a4b77a2aSPoul-Henning Kamp break;
349a4b77a2aSPoul-Henning Kamp
350a5a4544eSPaul Traina case 'R':
351a5a4544eSPaul Traina paranoid = 0;
352a5a4544eSPaul Traina break;
353a5a4544eSPaul Traina
354a5a4544eSPaul Traina case 'S':
355a5a4544eSPaul Traina stats++;
356a5a4544eSPaul Traina break;
357a5a4544eSPaul Traina
358ea022d16SRodney W. Grimes case 't':
359ea022d16SRodney W. Grimes timeout = atoi(optarg);
360ea022d16SRodney W. Grimes if (maxtimeout < timeout)
361ea022d16SRodney W. Grimes maxtimeout = timeout;
362ea022d16SRodney W. Grimes break;
363a5a4544eSPaul Traina
3640e063efeSYaroslav Tykhiy case 'T':
3650e063efeSYaroslav Tykhiy maxtimeout = atoi(optarg);
3660e063efeSYaroslav Tykhiy if (timeout > maxtimeout)
3670e063efeSYaroslav Tykhiy timeout = maxtimeout;
368105a3c98SJulian Elischer break;
369105a3c98SJulian Elischer
370ea022d16SRodney W. Grimes case 'u':
371ea022d16SRodney W. Grimes {
372ea022d16SRodney W. Grimes long val = 0;
373ea022d16SRodney W. Grimes
374ea022d16SRodney W. Grimes val = strtol(optarg, &optarg, 8);
375ea022d16SRodney W. Grimes if (*optarg != '\0' || val < 0)
3766c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "bad value for -u");
377ea022d16SRodney W. Grimes else
378ea022d16SRodney W. Grimes defumask = val;
379ea022d16SRodney W. Grimes break;
380ea022d16SRodney W. Grimes }
3810e063efeSYaroslav Tykhiy case 'U':
3820e063efeSYaroslav Tykhiy restricted_data_ports = 0;
3835a392aecSTorsten Blum break;
384ea022d16SRodney W. Grimes
385ea022d16SRodney W. Grimes case 'v':
38693bd9dc5SYaroslav Tykhiy ftpdebug++;
387ea022d16SRodney W. Grimes break;
388ea022d16SRodney W. Grimes
3895d7e0128SYaroslav Tykhiy case 'W':
3905d7e0128SYaroslav Tykhiy dowtmp = 0;
3915d7e0128SYaroslav Tykhiy break;
3925d7e0128SYaroslav Tykhiy
393ea022d16SRodney W. Grimes default:
3946c98f401SYaroslav Tykhiy syslog(LOG_WARNING, "unknown flag -%c ignored", optopt);
395ea022d16SRodney W. Grimes break;
396ea022d16SRodney W. Grimes }
397ea022d16SRodney W. Grimes }
398cf09a206SDavid Greenman
399de8d85c9SEugene Grosbein /* handle filesize limit gracefully */
400de8d85c9SEugene Grosbein sa.sa_handler = SIG_IGN;
401de8d85c9SEugene Grosbein (void)sigaction(SIGXFSZ, &sa, NULL);
402de8d85c9SEugene Grosbein
403cf09a206SDavid Greenman if (daemon_mode) {
404206fe568SHajimu UMEMOTO int *ctl_sock, fd, maxfd = -1, nfds, i;
405206fe568SHajimu UMEMOTO fd_set defreadfds, readfds;
406206fe568SHajimu UMEMOTO pid_t pid;
407125b9635SYaroslav Tykhiy struct pidfh *pfh;
408125b9635SYaroslav Tykhiy
409125b9635SYaroslav Tykhiy if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) {
410125b9635SYaroslav Tykhiy if (errno == EEXIST) {
411125b9635SYaroslav Tykhiy syslog(LOG_ERR, "%s already running, pid %d",
412125b9635SYaroslav Tykhiy getprogname(), (int)pid);
413125b9635SYaroslav Tykhiy exit(1);
414125b9635SYaroslav Tykhiy }
415125b9635SYaroslav Tykhiy syslog(LOG_WARNING, "pidfile_open: %m");
416125b9635SYaroslav Tykhiy }
417cf09a206SDavid Greenman
418cf09a206SDavid Greenman /*
419cf09a206SDavid Greenman * Detach from parent.
420cf09a206SDavid Greenman */
421cf09a206SDavid Greenman if (daemon(1, 1) < 0) {
422cf09a206SDavid Greenman syslog(LOG_ERR, "failed to become a daemon");
423cf09a206SDavid Greenman exit(1);
424cf09a206SDavid Greenman }
425125b9635SYaroslav Tykhiy
426125b9635SYaroslav Tykhiy if (pfh != NULL && pidfile_write(pfh) == -1)
427125b9635SYaroslav Tykhiy syslog(LOG_WARNING, "pidfile_write: %m");
428125b9635SYaroslav Tykhiy
4294b82fc95SYaroslav Tykhiy sa.sa_handler = reapchild;
4304b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL);
4314dd8b5abSYoshinobu Inoue
4322c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
4332c9fd5f2SHajimu UMEMOTO inithosts(family);
4342c9fd5f2SHajimu UMEMOTO #endif
4352c9fd5f2SHajimu UMEMOTO
436cf09a206SDavid Greenman /*
437cf09a206SDavid Greenman * Open a socket, bind it to the FTP port, and start
438cf09a206SDavid Greenman * listening.
439cf09a206SDavid Greenman */
440206fe568SHajimu UMEMOTO ctl_sock = socksetup(family, bindname, bindport);
441206fe568SHajimu UMEMOTO if (ctl_sock == NULL)
442cf09a206SDavid Greenman exit(1);
443206fe568SHajimu UMEMOTO
444206fe568SHajimu UMEMOTO FD_ZERO(&defreadfds);
445206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++) {
446206fe568SHajimu UMEMOTO FD_SET(ctl_sock[i], &defreadfds);
447206fe568SHajimu UMEMOTO if (listen(ctl_sock[i], 32) < 0) {
448cf09a206SDavid Greenman syslog(LOG_ERR, "control listen: %m");
449cf09a206SDavid Greenman exit(1);
450cf09a206SDavid Greenman }
451206fe568SHajimu UMEMOTO if (maxfd < ctl_sock[i])
452206fe568SHajimu UMEMOTO maxfd = ctl_sock[i];
453206fe568SHajimu UMEMOTO }
454206fe568SHajimu UMEMOTO
455cf09a206SDavid Greenman /*
456cf09a206SDavid Greenman * Loop forever accepting connection requests and forking off
457cf09a206SDavid Greenman * children to handle them.
458cf09a206SDavid Greenman */
459cf09a206SDavid Greenman while (1) {
460206fe568SHajimu UMEMOTO FD_COPY(&defreadfds, &readfds);
461206fe568SHajimu UMEMOTO nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
462206fe568SHajimu UMEMOTO if (nfds <= 0) {
463206fe568SHajimu UMEMOTO if (nfds < 0 && errno != EINTR)
464206fe568SHajimu UMEMOTO syslog(LOG_WARNING, "select: %m");
465206fe568SHajimu UMEMOTO continue;
466206fe568SHajimu UMEMOTO }
467206fe568SHajimu UMEMOTO
468206fe568SHajimu UMEMOTO pid = -1;
469206fe568SHajimu UMEMOTO for (i = 1; i <= *ctl_sock; i++)
470206fe568SHajimu UMEMOTO if (FD_ISSET(ctl_sock[i], &readfds)) {
471206fe568SHajimu UMEMOTO addrlen = sizeof(his_addr);
472206fe568SHajimu UMEMOTO fd = accept(ctl_sock[i],
473206fe568SHajimu UMEMOTO (struct sockaddr *)&his_addr,
474206fe568SHajimu UMEMOTO &addrlen);
475a599a64aSYaroslav Tykhiy if (fd == -1) {
476a599a64aSYaroslav Tykhiy syslog(LOG_WARNING,
477a599a64aSYaroslav Tykhiy "accept: %m");
478a599a64aSYaroslav Tykhiy continue;
479cf09a206SDavid Greenman }
480a599a64aSYaroslav Tykhiy switch (pid = fork()) {
481a599a64aSYaroslav Tykhiy case 0:
4828eb0508fSYaroslav Tykhiy /* child */
483504422faSKurt Lidl (void) dup2(fd, s);
484504422faSKurt Lidl (void) dup2(fd, STDOUT_FILENO);
485a599a64aSYaroslav Tykhiy (void) close(fd);
486a599a64aSYaroslav Tykhiy for (i = 1; i <= *ctl_sock; i++)
4878eb0508fSYaroslav Tykhiy close(ctl_sock[i]);
488125b9635SYaroslav Tykhiy if (pfh != NULL)
489125b9635SYaroslav Tykhiy pidfile_close(pfh);
490a599a64aSYaroslav Tykhiy goto gotchild;
491a599a64aSYaroslav Tykhiy case -1:
492a599a64aSYaroslav Tykhiy syslog(LOG_WARNING, "fork: %m");
493a599a64aSYaroslav Tykhiy /* FALLTHROUGH */
494a599a64aSYaroslav Tykhiy default:
495a599a64aSYaroslav Tykhiy close(fd);
496a599a64aSYaroslav Tykhiy }
497206fe568SHajimu UMEMOTO }
498125b9635SYaroslav Tykhiy }
499cf09a206SDavid Greenman } else {
500cf09a206SDavid Greenman addrlen = sizeof(his_addr);
501504422faSKurt Lidl if (getpeername(s, (struct sockaddr *)&his_addr, &addrlen) < 0) {
502cf09a206SDavid Greenman syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
503cf09a206SDavid Greenman exit(1);
504cf09a206SDavid Greenman }
5052c9fd5f2SHajimu UMEMOTO
5062c9fd5f2SHajimu UMEMOTO #ifdef VIRTUAL_HOSTING
5072c9fd5f2SHajimu UMEMOTO if (his_addr.su_family == AF_INET6 &&
5082c9fd5f2SHajimu UMEMOTO IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
5092c9fd5f2SHajimu UMEMOTO family = AF_INET;
5102c9fd5f2SHajimu UMEMOTO else
5112c9fd5f2SHajimu UMEMOTO family = his_addr.su_family;
5122c9fd5f2SHajimu UMEMOTO inithosts(family);
5132c9fd5f2SHajimu UMEMOTO #endif
514cf09a206SDavid Greenman }
515cf09a206SDavid Greenman
516a599a64aSYaroslav Tykhiy gotchild:
5174b82fc95SYaroslav Tykhiy sa.sa_handler = SIG_DFL;
5184b82fc95SYaroslav Tykhiy (void)sigaction(SIGCHLD, &sa, NULL);
5194b82fc95SYaroslav Tykhiy
5204b82fc95SYaroslav Tykhiy sa.sa_handler = sigurg;
5214b82fc95SYaroslav Tykhiy sa.sa_flags = 0; /* don't restart syscalls for SIGURG */
5224b82fc95SYaroslav Tykhiy (void)sigaction(SIGURG, &sa, NULL);
5234b82fc95SYaroslav Tykhiy
5244b82fc95SYaroslav Tykhiy sigfillset(&sa.sa_mask); /* block all signals in handler */
5254b82fc95SYaroslav Tykhiy sa.sa_flags = SA_RESTART;
5264b82fc95SYaroslav Tykhiy sa.sa_handler = sigquit;
5274b82fc95SYaroslav Tykhiy (void)sigaction(SIGHUP, &sa, NULL);
5284b82fc95SYaroslav Tykhiy (void)sigaction(SIGINT, &sa, NULL);
5294b82fc95SYaroslav Tykhiy (void)sigaction(SIGQUIT, &sa, NULL);
5304b82fc95SYaroslav Tykhiy (void)sigaction(SIGTERM, &sa, NULL);
5314b82fc95SYaroslav Tykhiy
5324b82fc95SYaroslav Tykhiy sa.sa_handler = lostconn;
5334b82fc95SYaroslav Tykhiy (void)sigaction(SIGPIPE, &sa, NULL);
534ea022d16SRodney W. Grimes
535cf09a206SDavid Greenman addrlen = sizeof(ctrl_addr);
536504422faSKurt Lidl if (getsockname(s, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
537cf09a206SDavid Greenman syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
538cf09a206SDavid Greenman exit(1);
539cf09a206SDavid Greenman }
54063591ba5SYaroslav Tykhiy dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
541ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
542ea4e54b9SDavid Nugent /* select our identity from virtual host table */
5434dd8b5abSYoshinobu Inoue selecthost(&ctrl_addr);
544ea4e54b9SDavid Nugent #endif
545cf09a206SDavid Greenman #ifdef IP_TOS
5464dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET)
5474dd8b5abSYoshinobu Inoue {
548cf09a206SDavid Greenman tos = IPTOS_LOWDELAY;
549504422faSKurt Lidl if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
550406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
5514dd8b5abSYoshinobu Inoue }
552cf09a206SDavid Greenman #endif
553dadb9fb3SDavid Greenman /*
554dadb9fb3SDavid Greenman * Disable Nagle on the control channel so that we don't have to wait
555dadb9fb3SDavid Greenman * for peer's ACK before issuing our next reply.
556dadb9fb3SDavid Greenman */
557504422faSKurt Lidl if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
558406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
559dadb9fb3SDavid Greenman
5604dd8b5abSYoshinobu Inoue data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
561cf09a206SDavid Greenman
56280643af0SEd Schouten (void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid());
563a5a4544eSPaul Traina
564ea022d16SRodney W. Grimes /* Try to handle urgent data inline */
565ea022d16SRodney W. Grimes #ifdef SO_OOBINLINE
566504422faSKurt Lidl if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
567406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
568ea022d16SRodney W. Grimes #endif
569ea022d16SRodney W. Grimes
570ea022d16SRodney W. Grimes #ifdef F_SETOWN
571504422faSKurt Lidl if (fcntl(s, F_SETOWN, getpid()) == -1)
572ea022d16SRodney W. Grimes syslog(LOG_ERR, "fcntl F_SETOWN: %m");
573ea022d16SRodney W. Grimes #endif
5744dd8b5abSYoshinobu Inoue dolog((struct sockaddr *)&his_addr);
575ea022d16SRodney W. Grimes /*
576ea022d16SRodney W. Grimes * Set up default state
577ea022d16SRodney W. Grimes */
578ea022d16SRodney W. Grimes data = -1;
579ea022d16SRodney W. Grimes type = TYPE_A;
580ea022d16SRodney W. Grimes form = FORM_N;
581ea022d16SRodney W. Grimes stru = STRU_F;
582ea022d16SRodney W. Grimes mode = MODE_S;
583ea022d16SRodney W. Grimes tmpline[0] = '\0';
584ea022d16SRodney W. Grimes
585ea022d16SRodney W. Grimes /* If logins are disabled, print out the message. */
586ea022d16SRodney W. Grimes if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
587ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) {
588ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL)
589ea022d16SRodney W. Grimes *cp = '\0';
590ea022d16SRodney W. Grimes lreply(530, "%s", line);
591ea022d16SRodney W. Grimes }
592ea022d16SRodney W. Grimes (void) fflush(stdout);
593ea022d16SRodney W. Grimes (void) fclose(fd);
594ea022d16SRodney W. Grimes reply(530, "System not available.");
595ea022d16SRodney W. Grimes exit(0);
596ea022d16SRodney W. Grimes }
597ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
59863047c6fSDavid E. O'Brien fd = fopen(thishost->welcome, "r");
599ea4e54b9SDavid Nugent #else
60063047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPWELCOME, "r");
601ea4e54b9SDavid Nugent #endif
60263047c6fSDavid E. O'Brien if (fd != NULL) {
603ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) {
604ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL)
605ea022d16SRodney W. Grimes *cp = '\0';
606ea022d16SRodney W. Grimes lreply(220, "%s", line);
607ea022d16SRodney W. Grimes }
608ea022d16SRodney W. Grimes (void) fflush(stdout);
609ea022d16SRodney W. Grimes (void) fclose(fd);
610ea022d16SRodney W. Grimes /* reply(220,) must follow */
611ea022d16SRodney W. Grimes }
612ea4e54b9SDavid Nugent #ifndef VIRTUAL_HOSTING
6130512556aSDavid Nugent if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
614618b0bbaSMark Murray fatalerror("Ran out of memory.");
61504683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
61604683b2cSYaroslav Tykhiy hostname[0] = '\0';
6179e9a43bdSBrian Somers hostname[MAXHOSTNAMELEN - 1] = '\0';
618ea4e54b9SDavid Nugent #endif
619c152df28SYaroslav Tykhiy if (hostinfo)
620ea022d16SRodney W. Grimes reply(220, "%s FTP server (%s) ready.", hostname, version);
621c152df28SYaroslav Tykhiy else
622c152df28SYaroslav Tykhiy reply(220, "FTP server ready.");
623e07d11b6SKurt Lidl BLACKLIST_INIT();
624ea022d16SRodney W. Grimes for (;;)
625ea022d16SRodney W. Grimes (void) yyparse();
626ea022d16SRodney W. Grimes /* NOTREACHED */
627ea022d16SRodney W. Grimes }
628ea022d16SRodney W. Grimes
629ea022d16SRodney W. Grimes static void
lostconn(int signo)630e4bc453cSWarner Losh lostconn(int signo)
631ea022d16SRodney W. Grimes {
632ea022d16SRodney W. Grimes
633618b0bbaSMark Murray if (ftpdebug)
634ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "lost connection");
635e02897faSPhilippe Charnier dologout(1);
636ea022d16SRodney W. Grimes }
637ea022d16SRodney W. Grimes
6384b82fc95SYaroslav Tykhiy static void
sigquit(int signo)639e4bc453cSWarner Losh sigquit(int signo)
6404b82fc95SYaroslav Tykhiy {
6414b82fc95SYaroslav Tykhiy
6424b82fc95SYaroslav Tykhiy syslog(LOG_ERR, "got signal %d", signo);
6434b82fc95SYaroslav Tykhiy dologout(1);
6444b82fc95SYaroslav Tykhiy }
6454b82fc95SYaroslav Tykhiy
646ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
647ea4e54b9SDavid Nugent /*
648ea4e54b9SDavid Nugent * read in virtual host tables (if they exist)
649ea4e54b9SDavid Nugent */
650ea4e54b9SDavid Nugent
651ea4e54b9SDavid Nugent static void
inithosts(int family)6522c9fd5f2SHajimu UMEMOTO inithosts(int family)
653ea4e54b9SDavid Nugent {
65455b54aa7SYaroslav Tykhiy int insert;
655737d08f3SYaroslav Tykhiy size_t len;
656ea4e54b9SDavid Nugent FILE *fp;
657737d08f3SYaroslav Tykhiy char *cp, *mp, *line;
658737d08f3SYaroslav Tykhiy char *hostname;
65955b54aa7SYaroslav Tykhiy char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
660ea4e54b9SDavid Nugent struct ftphost *hrp, *lhrp;
6614dd8b5abSYoshinobu Inoue struct addrinfo hints, *res, *ai;
662ea4e54b9SDavid Nugent
663ea4e54b9SDavid Nugent /*
664ea4e54b9SDavid Nugent * Fill in the default host information
665ea4e54b9SDavid Nugent */
666737d08f3SYaroslav Tykhiy if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
667618b0bbaSMark Murray fatalerror("Ran out of memory.");
66804683b2cSYaroslav Tykhiy if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
669737d08f3SYaroslav Tykhiy hostname[0] = '\0';
670737d08f3SYaroslav Tykhiy hostname[MAXHOSTNAMELEN - 1] = '\0';
671737d08f3SYaroslav Tykhiy if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
672737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory.");
673737d08f3SYaroslav Tykhiy hrp->hostname = hostname;
674f38c6cadSYoshinobu Inoue hrp->hostinfo = NULL;
6754dd8b5abSYoshinobu Inoue
6764dd8b5abSYoshinobu Inoue memset(&hints, 0, sizeof(hints));
6772c9fd5f2SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE;
6782c9fd5f2SHajimu UMEMOTO hints.ai_family = family;
6792c9fd5f2SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM;
680b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
681f38c6cadSYoshinobu Inoue hrp->hostinfo = res;
682ea4e54b9SDavid Nugent hrp->statfile = _PATH_FTPDSTATFILE;
683ea4e54b9SDavid Nugent hrp->welcome = _PATH_FTPWELCOME;
684ea4e54b9SDavid Nugent hrp->loginmsg = _PATH_FTPLOGINMESG;
685ea4e54b9SDavid Nugent hrp->anonuser = "ftp";
686ea4e54b9SDavid Nugent hrp->next = NULL;
687ea4e54b9SDavid Nugent thishost = firsthost = lhrp = hrp;
688ea4e54b9SDavid Nugent if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
689ec009cf0SYaroslav Tykhiy int addrsize, gothost;
6904dd8b5abSYoshinobu Inoue void *addr;
6914dd8b5abSYoshinobu Inoue struct hostent *hp;
6924dd8b5abSYoshinobu Inoue
693737d08f3SYaroslav Tykhiy while ((line = fgetln(fp, &len)) != NULL) {
6944dd8b5abSYoshinobu Inoue int i, hp_error;
695ea4e54b9SDavid Nugent
696737d08f3SYaroslav Tykhiy /* skip comments */
697737d08f3SYaroslav Tykhiy if (line[0] == '#')
698ea4e54b9SDavid Nugent continue;
699737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') {
700737d08f3SYaroslav Tykhiy line[len - 1] = '\0';
701737d08f3SYaroslav Tykhiy mp = NULL;
702737d08f3SYaroslav Tykhiy } else {
703737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL)
704737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory.");
705737d08f3SYaroslav Tykhiy memcpy(mp, line, len);
706737d08f3SYaroslav Tykhiy mp[len] = '\0';
707737d08f3SYaroslav Tykhiy line = mp;
708ea4e54b9SDavid Nugent }
709ea4e54b9SDavid Nugent cp = strtok(line, " \t");
710737d08f3SYaroslav Tykhiy /* skip empty lines */
711737d08f3SYaroslav Tykhiy if (cp == NULL)
712737d08f3SYaroslav Tykhiy goto nextline;
71355b54aa7SYaroslav Tykhiy vhost = cp;
71455b54aa7SYaroslav Tykhiy
71555b54aa7SYaroslav Tykhiy /* set defaults */
71655b54aa7SYaroslav Tykhiy anonuser = "ftp";
71755b54aa7SYaroslav Tykhiy statfile = _PATH_FTPDSTATFILE;
71855b54aa7SYaroslav Tykhiy welcome = _PATH_FTPWELCOME;
71955b54aa7SYaroslav Tykhiy loginmsg = _PATH_FTPLOGINMESG;
72055b54aa7SYaroslav Tykhiy
72155b54aa7SYaroslav Tykhiy /*
72255b54aa7SYaroslav Tykhiy * Preparse the line so we can use its info
72355b54aa7SYaroslav Tykhiy * for all the addresses associated with
72455b54aa7SYaroslav Tykhiy * the virtual host name.
72555b54aa7SYaroslav Tykhiy * Field 0, the virtual host name, is special:
72655b54aa7SYaroslav Tykhiy * it's already parsed off and will be strdup'ed
72755b54aa7SYaroslav Tykhiy * later, after we know its canonical form.
72855b54aa7SYaroslav Tykhiy */
72955b54aa7SYaroslav Tykhiy for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
73055b54aa7SYaroslav Tykhiy if (*cp != '-' && (cp = strdup(cp)))
73155b54aa7SYaroslav Tykhiy switch (i) {
73255b54aa7SYaroslav Tykhiy case 1: /* anon user permissions */
73355b54aa7SYaroslav Tykhiy anonuser = cp;
73455b54aa7SYaroslav Tykhiy break;
73555b54aa7SYaroslav Tykhiy case 2: /* statistics file */
73655b54aa7SYaroslav Tykhiy statfile = cp;
73755b54aa7SYaroslav Tykhiy break;
73855b54aa7SYaroslav Tykhiy case 3: /* welcome message */
73955b54aa7SYaroslav Tykhiy welcome = cp;
74055b54aa7SYaroslav Tykhiy break;
74155b54aa7SYaroslav Tykhiy case 4: /* login message */
74255b54aa7SYaroslav Tykhiy loginmsg = cp;
74355b54aa7SYaroslav Tykhiy break;
74455b54aa7SYaroslav Tykhiy default: /* programming error */
74555b54aa7SYaroslav Tykhiy abort();
74655b54aa7SYaroslav Tykhiy /* NOTREACHED */
74755b54aa7SYaroslav Tykhiy }
7484dd8b5abSYoshinobu Inoue
7494dd8b5abSYoshinobu Inoue hints.ai_flags = AI_PASSIVE;
7502c9fd5f2SHajimu UMEMOTO hints.ai_family = family;
7512c9fd5f2SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM;
752b1d8d5cdSYaroslav Tykhiy if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
753737d08f3SYaroslav Tykhiy goto nextline;
7544dd8b5abSYoshinobu Inoue for (ai = res; ai != NULL && ai->ai_addr != NULL;
755b535a9bfSDavid Nugent ai = ai->ai_next) {
7564dd8b5abSYoshinobu Inoue
757b535a9bfSDavid Nugent gothost = 0;
758ea4e54b9SDavid Nugent for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
759f38c6cadSYoshinobu Inoue struct addrinfo *hi;
760f38c6cadSYoshinobu Inoue
761f38c6cadSYoshinobu Inoue for (hi = hrp->hostinfo; hi != NULL;
762f38c6cadSYoshinobu Inoue hi = hi->ai_next)
763f38c6cadSYoshinobu Inoue if (hi->ai_addrlen == ai->ai_addrlen &&
764f38c6cadSYoshinobu Inoue memcmp(hi->ai_addr,
7654dd8b5abSYoshinobu Inoue ai->ai_addr,
766b535a9bfSDavid Nugent ai->ai_addr->sa_len) == 0) {
767b535a9bfSDavid Nugent gothost++;
768b535a9bfSDavid Nugent break;
769b535a9bfSDavid Nugent }
770b535a9bfSDavid Nugent if (gothost)
771ea4e54b9SDavid Nugent break;
772ea4e54b9SDavid Nugent }
773ea4e54b9SDavid Nugent if (hrp == NULL) {
774ea4e54b9SDavid Nugent if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
775737d08f3SYaroslav Tykhiy goto nextline;
776b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL;
77755b54aa7SYaroslav Tykhiy insert = 1;
778f2fe752dSYaroslav Tykhiy } else {
7791f75c13eSYaroslav Tykhiy if (hrp->hostinfo && hrp->hostinfo != res)
780b1d8d5cdSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo);
781f2fe752dSYaroslav Tykhiy insert = 0; /* host already in the chain */
782f2fe752dSYaroslav Tykhiy }
783f38c6cadSYoshinobu Inoue hrp->hostinfo = res;
784f38c6cadSYoshinobu Inoue
785ea4e54b9SDavid Nugent /*
786ea4e54b9SDavid Nugent * determine hostname to use.
7874dd8b5abSYoshinobu Inoue * force defined name if there is a valid alias
788ea4e54b9SDavid Nugent * otherwise fallback to primary hostname
789ea4e54b9SDavid Nugent */
7904dd8b5abSYoshinobu Inoue /* XXX: getaddrinfo() can't do alias check */
791f38c6cadSYoshinobu Inoue switch(hrp->hostinfo->ai_family) {
7924dd8b5abSYoshinobu Inoue case AF_INET:
7934b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
7944b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in_addr);
7954dd8b5abSYoshinobu Inoue break;
7964dd8b5abSYoshinobu Inoue case AF_INET6:
7974b4cc4c6SYaroslav Tykhiy addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
7984b4cc4c6SYaroslav Tykhiy addrsize = sizeof(struct in6_addr);
7994dd8b5abSYoshinobu Inoue break;
8004dd8b5abSYoshinobu Inoue default:
8014dd8b5abSYoshinobu Inoue /* should not reach here */
802f38c6cadSYoshinobu Inoue freeaddrinfo(hrp->hostinfo);
803f2fe752dSYaroslav Tykhiy if (insert)
804f2fe752dSYaroslav Tykhiy free(hrp); /*not in chain, can free*/
805f2fe752dSYaroslav Tykhiy else
806f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /*mark as blank*/
807737d08f3SYaroslav Tykhiy goto nextline;
8084dd8b5abSYoshinobu Inoue /* NOTREACHED */
8094dd8b5abSYoshinobu Inoue }
81057d4ef07SYaroslav Tykhiy if ((hp = getipnodebyaddr(addr, addrsize,
811f38c6cadSYoshinobu Inoue hrp->hostinfo->ai_family,
8124dd8b5abSYoshinobu Inoue &hp_error)) != NULL) {
81355b54aa7SYaroslav Tykhiy if (strcmp(vhost, hp->h_name) != 0) {
814ea4e54b9SDavid Nugent if (hp->h_aliases == NULL)
81555b54aa7SYaroslav Tykhiy vhost = hp->h_name;
816ea4e54b9SDavid Nugent else {
817ea4e54b9SDavid Nugent i = 0;
818ea4e54b9SDavid Nugent while (hp->h_aliases[i] &&
81955b54aa7SYaroslav Tykhiy strcmp(vhost, hp->h_aliases[i]) != 0)
820ea4e54b9SDavid Nugent ++i;
821ea4e54b9SDavid Nugent if (hp->h_aliases[i] == NULL)
82255b54aa7SYaroslav Tykhiy vhost = hp->h_name;
823ea4e54b9SDavid Nugent }
824ea4e54b9SDavid Nugent }
825ea4e54b9SDavid Nugent }
826b1d8d5cdSYaroslav Tykhiy if (hrp->hostname &&
827b1d8d5cdSYaroslav Tykhiy strcmp(hrp->hostname, vhost) != 0) {
828b1d8d5cdSYaroslav Tykhiy free(hrp->hostname);
829b1d8d5cdSYaroslav Tykhiy hrp->hostname = NULL;
830b1d8d5cdSYaroslav Tykhiy }
831b1d8d5cdSYaroslav Tykhiy if (hrp->hostname == NULL &&
832f2fe752dSYaroslav Tykhiy (hrp->hostname = strdup(vhost)) == NULL) {
833f2fe752dSYaroslav Tykhiy freeaddrinfo(hrp->hostinfo);
834f2fe752dSYaroslav Tykhiy hrp->hostinfo = NULL; /* mark as blank */
835f2fe752dSYaroslav Tykhiy if (hp)
836f2fe752dSYaroslav Tykhiy freehostent(hp);
83755b54aa7SYaroslav Tykhiy goto nextline;
838f2fe752dSYaroslav Tykhiy }
83955b54aa7SYaroslav Tykhiy hrp->anonuser = anonuser;
84055b54aa7SYaroslav Tykhiy hrp->statfile = statfile;
84155b54aa7SYaroslav Tykhiy hrp->welcome = welcome;
84255b54aa7SYaroslav Tykhiy hrp->loginmsg = loginmsg;
84355b54aa7SYaroslav Tykhiy if (insert) {
84455b54aa7SYaroslav Tykhiy hrp->next = NULL;
84555b54aa7SYaroslav Tykhiy lhrp->next = hrp;
84655b54aa7SYaroslav Tykhiy lhrp = hrp;
84755b54aa7SYaroslav Tykhiy }
848233c0f66SYaroslav Tykhiy if (hp)
8494dd8b5abSYoshinobu Inoue freehostent(hp);
8504dd8b5abSYoshinobu Inoue }
851737d08f3SYaroslav Tykhiy nextline:
852737d08f3SYaroslav Tykhiy if (mp)
853737d08f3SYaroslav Tykhiy free(mp);
854ea4e54b9SDavid Nugent }
855ea4e54b9SDavid Nugent (void) fclose(fp);
856ea4e54b9SDavid Nugent }
857ea4e54b9SDavid Nugent }
858ea4e54b9SDavid Nugent
859ea4e54b9SDavid Nugent static void
selecthost(union sockunion * su)860e4bc453cSWarner Losh selecthost(union sockunion *su)
861ea4e54b9SDavid Nugent {
862ea4e54b9SDavid Nugent struct ftphost *hrp;
8634dd8b5abSYoshinobu Inoue u_int16_t port;
8644dd8b5abSYoshinobu Inoue #ifdef INET6
8654dd8b5abSYoshinobu Inoue struct in6_addr *mapped_in6 = NULL;
8664dd8b5abSYoshinobu Inoue #endif
867f38c6cadSYoshinobu Inoue struct addrinfo *hi;
8684dd8b5abSYoshinobu Inoue
8694dd8b5abSYoshinobu Inoue #ifdef INET6
8704dd8b5abSYoshinobu Inoue /*
8714dd8b5abSYoshinobu Inoue * XXX IPv4 mapped IPv6 addr consideraton,
8724dd8b5abSYoshinobu Inoue * specified in rfc2373.
8734dd8b5abSYoshinobu Inoue */
8744dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET6 &&
8754dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
8764dd8b5abSYoshinobu Inoue mapped_in6 = &su->su_sin6.sin6_addr;
8774dd8b5abSYoshinobu Inoue #endif
878ea4e54b9SDavid Nugent
879ea4e54b9SDavid Nugent hrp = thishost = firsthost; /* default */
8804dd8b5abSYoshinobu Inoue port = su->su_port;
8814dd8b5abSYoshinobu Inoue su->su_port = 0;
882ea4e54b9SDavid Nugent while (hrp != NULL) {
883b535a9bfSDavid Nugent for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
884f38c6cadSYoshinobu Inoue if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
885ea4e54b9SDavid Nugent thishost = hrp;
886ebd83647SYaroslav Tykhiy goto found;
887ea4e54b9SDavid Nugent }
8884dd8b5abSYoshinobu Inoue #ifdef INET6
8894dd8b5abSYoshinobu Inoue /* XXX IPv4 mapped IPv6 addr consideraton */
890f38c6cadSYoshinobu Inoue if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
8914dd8b5abSYoshinobu Inoue (memcmp(&mapped_in6->s6_addr[12],
892f38c6cadSYoshinobu Inoue &((struct sockaddr_in *)hi->ai_addr)->sin_addr,
8934dd8b5abSYoshinobu Inoue sizeof(struct in_addr)) == 0)) {
8944dd8b5abSYoshinobu Inoue thishost = hrp;
895ebd83647SYaroslav Tykhiy goto found;
8964dd8b5abSYoshinobu Inoue }
8974dd8b5abSYoshinobu Inoue #endif
898f38c6cadSYoshinobu Inoue }
899ea4e54b9SDavid Nugent hrp = hrp->next;
900ea4e54b9SDavid Nugent }
901ebd83647SYaroslav Tykhiy found:
9024dd8b5abSYoshinobu Inoue su->su_port = port;
903ea4e54b9SDavid Nugent /* setup static variables as appropriate */
904ea4e54b9SDavid Nugent hostname = thishost->hostname;
905ea4e54b9SDavid Nugent ftpuser = thishost->anonuser;
906ea4e54b9SDavid Nugent }
907ea4e54b9SDavid Nugent #endif
908ea4e54b9SDavid Nugent
909ea022d16SRodney W. Grimes /*
910ea022d16SRodney W. Grimes * Helper function for sgetpwnam().
911ea022d16SRodney W. Grimes */
912ea022d16SRodney W. Grimes static char *
sgetsave(char * s)913e4bc453cSWarner Losh sgetsave(char *s)
914ea022d16SRodney W. Grimes {
915e3765043SYaroslav Tykhiy char *new = malloc(strlen(s) + 1);
916ea022d16SRodney W. Grimes
917ea022d16SRodney W. Grimes if (new == NULL) {
91802c97492SYaroslav Tykhiy reply(421, "Ran out of memory.");
919ea022d16SRodney W. Grimes dologout(1);
920ea022d16SRodney W. Grimes /* NOTREACHED */
921ea022d16SRodney W. Grimes }
922ea022d16SRodney W. Grimes (void) strcpy(new, s);
923ea022d16SRodney W. Grimes return (new);
924ea022d16SRodney W. Grimes }
925ea022d16SRodney W. Grimes
926ea022d16SRodney W. Grimes /*
927ea022d16SRodney W. Grimes * Save the result of a getpwnam. Used for USER command, since
928ea022d16SRodney W. Grimes * the data returned must not be clobbered by any other command
929ea022d16SRodney W. Grimes * (e.g., globbing).
930c29b9b47SYaroslav Tykhiy * NB: The data returned by sgetpwnam() will remain valid until
931c29b9b47SYaroslav Tykhiy * the next call to this function. Its difference from getpwnam()
932c29b9b47SYaroslav Tykhiy * is that sgetpwnam() is known to be called from ftpd code only.
933ea022d16SRodney W. Grimes */
934ea022d16SRodney W. Grimes static struct passwd *
sgetpwnam(char * name)935e4bc453cSWarner Losh sgetpwnam(char *name)
936ea022d16SRodney W. Grimes {
937ea022d16SRodney W. Grimes static struct passwd save;
938ea022d16SRodney W. Grimes struct passwd *p;
939ea022d16SRodney W. Grimes
940ea022d16SRodney W. Grimes if ((p = getpwnam(name)) == NULL)
941ea022d16SRodney W. Grimes return (p);
942ea022d16SRodney W. Grimes if (save.pw_name) {
943ea022d16SRodney W. Grimes free(save.pw_name);
944ea022d16SRodney W. Grimes free(save.pw_passwd);
94503d34cccSChristian Brueffer free(save.pw_class);
946ea022d16SRodney W. Grimes free(save.pw_gecos);
947ea022d16SRodney W. Grimes free(save.pw_dir);
948ea022d16SRodney W. Grimes free(save.pw_shell);
949ea022d16SRodney W. Grimes }
950ea022d16SRodney W. Grimes save = *p;
951ea022d16SRodney W. Grimes save.pw_name = sgetsave(p->pw_name);
952ea022d16SRodney W. Grimes save.pw_passwd = sgetsave(p->pw_passwd);
95303d34cccSChristian Brueffer save.pw_class = sgetsave(p->pw_class);
954ea022d16SRodney W. Grimes save.pw_gecos = sgetsave(p->pw_gecos);
955ea022d16SRodney W. Grimes save.pw_dir = sgetsave(p->pw_dir);
956ea022d16SRodney W. Grimes save.pw_shell = sgetsave(p->pw_shell);
957ea022d16SRodney W. Grimes return (&save);
958ea022d16SRodney W. Grimes }
959ea022d16SRodney W. Grimes
960ea022d16SRodney W. Grimes static int login_attempts; /* number of failed login attempts */
961ea022d16SRodney W. Grimes static int askpasswd; /* had user command, ask for passwd */
96290906a46SSheldon Hearn static char curname[MAXLOGNAME]; /* current USER name */
963ea022d16SRodney W. Grimes
964ea022d16SRodney W. Grimes /*
965ea022d16SRodney W. Grimes * USER command.
966ea022d16SRodney W. Grimes * Sets global passwd pointer pw if named account exists and is acceptable;
967ea022d16SRodney W. Grimes * sets askpasswd if a PASS command is expected. If logged in previously,
968ea022d16SRodney W. Grimes * need to reset state. If name is "ftp" or "anonymous", the name is not in
969ea022d16SRodney W. Grimes * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
970ea022d16SRodney W. Grimes * If account doesn't exist, ask for passwd anyway. Otherwise, check user
971ea022d16SRodney W. Grimes * requesting login privileges. Disallow anyone who does not have a standard
972ea022d16SRodney W. Grimes * shell as returned by getusershell(). Disallow anyone mentioned in the file
973ea022d16SRodney W. Grimes * _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
974ea022d16SRodney W. Grimes */
975ea022d16SRodney W. Grimes void
user(char * name)976e4bc453cSWarner Losh user(char *name)
977ea022d16SRodney W. Grimes {
978cefb6785SChristian S.J. Peron int ecode;
979ea022d16SRodney W. Grimes char *cp, *shell;
980ea022d16SRodney W. Grimes
981ea022d16SRodney W. Grimes if (logged_in) {
982ea022d16SRodney W. Grimes if (guest) {
983ea022d16SRodney W. Grimes reply(530, "Can't change user from guest login.");
984ea022d16SRodney W. Grimes return;
985a5a4544eSPaul Traina } else if (dochroot) {
986a5a4544eSPaul Traina reply(530, "Can't change user from chroot user.");
987a5a4544eSPaul Traina return;
988ea022d16SRodney W. Grimes }
989ea022d16SRodney W. Grimes end_login();
990ea022d16SRodney W. Grimes }
991ea022d16SRodney W. Grimes
992ea022d16SRodney W. Grimes guest = 0;
99363047c6fSDavid E. O'Brien #ifdef VIRTUAL_HOSTING
99463047c6fSDavid E. O'Brien pw = sgetpwnam(thishost->anonuser);
99563047c6fSDavid E. O'Brien #else
99663047c6fSDavid E. O'Brien pw = sgetpwnam("ftp");
99763047c6fSDavid E. O'Brien #endif
998ea022d16SRodney W. Grimes if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
999cefb6785SChristian S.J. Peron if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) ||
1000cefb6785SChristian S.J. Peron (ecode != 0 && ecode != ENOENT))
1001cefb6785SChristian S.J. Peron reply(530, "User %s access denied.", name);
1002cefb6785SChristian S.J. Peron else if (checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL, &ecode) ||
1003cefb6785SChristian S.J. Peron (ecode != 0 && ecode != ENOENT))
1004ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name);
100563047c6fSDavid E. O'Brien else if (pw != NULL) {
1006ea022d16SRodney W. Grimes guest = 1;
1007ea022d16SRodney W. Grimes askpasswd = 1;
1008ea022d16SRodney W. Grimes reply(331,
10092c60c54cSPaul Traina "Guest login ok, send your email address as password.");
1010ea022d16SRodney W. Grimes } else
1011ea022d16SRodney W. Grimes reply(530, "User %s unknown.", name);
1012ea022d16SRodney W. Grimes if (!askpasswd && logging)
1013ea022d16SRodney W. Grimes syslog(LOG_NOTICE,
1014ea022d16SRodney W. Grimes "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
1015ea022d16SRodney W. Grimes return;
1016ea022d16SRodney W. Grimes }
10175a392aecSTorsten Blum if (anon_only != 0) {
10185a392aecSTorsten Blum reply(530, "Sorry, only anonymous ftp allowed.");
10195a392aecSTorsten Blum return;
10205a392aecSTorsten Blum }
10215a392aecSTorsten Blum
10229aca17cbSMark Murray if ((pw = sgetpwnam(name))) {
1023ea022d16SRodney W. Grimes if ((shell = pw->pw_shell) == NULL || *shell == 0)
1024ea022d16SRodney W. Grimes shell = _PATH_BSHELL;
1025c433c9daSPhilippe Charnier setusershell();
1026ea022d16SRodney W. Grimes while ((cp = getusershell()) != NULL)
1027ea022d16SRodney W. Grimes if (strcmp(cp, shell) == 0)
1028ea022d16SRodney W. Grimes break;
1029ea022d16SRodney W. Grimes endusershell();
1030ea022d16SRodney W. Grimes
1031cefb6785SChristian S.J. Peron if (cp == NULL ||
1032cefb6785SChristian S.J. Peron (checkuser(_PATH_FTPUSERS, name, 1, NULL, &ecode) ||
1033cefb6785SChristian S.J. Peron (ecode != 0 && ecode != ENOENT))) {
1034ea022d16SRodney W. Grimes reply(530, "User %s access denied.", name);
1035ea022d16SRodney W. Grimes if (logging)
1036ea022d16SRodney W. Grimes syslog(LOG_NOTICE,
1037ea022d16SRodney W. Grimes "FTP LOGIN REFUSED FROM %s, %s",
1038ea022d16SRodney W. Grimes remotehost, name);
10397e295315SYaroslav Tykhiy pw = NULL;
1040ea022d16SRodney W. Grimes return;
1041ea022d16SRodney W. Grimes }
1042ea022d16SRodney W. Grimes }
1043ea022d16SRodney W. Grimes if (logging)
104469097cd8SXin LI strlcpy(curname, name, sizeof(curname));
104547499ecdSAndrey A. Chernov
104647499ecdSAndrey A. Chernov reply(331, "Password required for %s.", name);
1047ea022d16SRodney W. Grimes askpasswd = 1;
1048ea022d16SRodney W. Grimes /*
1049ea022d16SRodney W. Grimes * Delay before reading passwd after first failed
1050ea022d16SRodney W. Grimes * attempt to slow down passwd-guessing programs.
1051ea022d16SRodney W. Grimes */
1052ea022d16SRodney W. Grimes if (login_attempts)
1053e3765043SYaroslav Tykhiy sleep(login_attempts);
1054ea022d16SRodney W. Grimes }
1055ea022d16SRodney W. Grimes
1056ea022d16SRodney W. Grimes /*
10578657b576SYaroslav Tykhiy * Check if a user is in the file "fname",
10588657b576SYaroslav Tykhiy * return a pointer to a malloc'd string with the rest
10598657b576SYaroslav Tykhiy * of the matching line in "residue" if not NULL.
1060ea022d16SRodney W. Grimes */
1061ea022d16SRodney W. Grimes static int
checkuser(char * fname,char * name,int pwset,char ** residue,int * ecode)1062cefb6785SChristian S.J. Peron checkuser(char *fname, char *name, int pwset, char **residue, int *ecode)
1063ea022d16SRodney W. Grimes {
1064ea022d16SRodney W. Grimes FILE *fd;
1065ea022d16SRodney W. Grimes int found = 0;
1066737d08f3SYaroslav Tykhiy size_t len;
1067737d08f3SYaroslav Tykhiy char *line, *mp, *p;
1068ea022d16SRodney W. Grimes
1069cefb6785SChristian S.J. Peron if (ecode != NULL)
1070cefb6785SChristian S.J. Peron *ecode = 0;
1071a5a4544eSPaul Traina if ((fd = fopen(fname, "r")) != NULL) {
1072737d08f3SYaroslav Tykhiy while (!found && (line = fgetln(fd, &len)) != NULL) {
1073737d08f3SYaroslav Tykhiy /* skip comments */
1074ea022d16SRodney W. Grimes if (line[0] == '#')
1075ea022d16SRodney W. Grimes continue;
1076737d08f3SYaroslav Tykhiy if (line[len - 1] == '\n') {
1077737d08f3SYaroslav Tykhiy line[len - 1] = '\0';
1078737d08f3SYaroslav Tykhiy mp = NULL;
1079737d08f3SYaroslav Tykhiy } else {
1080737d08f3SYaroslav Tykhiy if ((mp = malloc(len + 1)) == NULL)
1081737d08f3SYaroslav Tykhiy fatalerror("Ran out of memory.");
1082737d08f3SYaroslav Tykhiy memcpy(mp, line, len);
1083737d08f3SYaroslav Tykhiy mp[len] = '\0';
1084737d08f3SYaroslav Tykhiy line = mp;
1085737d08f3SYaroslav Tykhiy }
1086737d08f3SYaroslav Tykhiy /* avoid possible leading and trailing whitespace */
1087737d08f3SYaroslav Tykhiy p = strtok(line, " \t");
1088737d08f3SYaroslav Tykhiy /* skip empty lines */
1089737d08f3SYaroslav Tykhiy if (p == NULL)
1090737d08f3SYaroslav Tykhiy goto nextline;
109131fea7b8SDavid Nugent /*
109231fea7b8SDavid Nugent * if first chr is '@', check group membership
109331fea7b8SDavid Nugent */
1094737d08f3SYaroslav Tykhiy if (p[0] == '@') {
109531fea7b8SDavid Nugent int i = 0;
109631fea7b8SDavid Nugent struct group *grp;
109731fea7b8SDavid Nugent
10988657b576SYaroslav Tykhiy if (p[1] == '\0') /* single @ matches anyone */
10998657b576SYaroslav Tykhiy found = 1;
11008657b576SYaroslav Tykhiy else {
1101737d08f3SYaroslav Tykhiy if ((grp = getgrnam(p+1)) == NULL)
1102737d08f3SYaroslav Tykhiy goto nextline;
11037edcb936SSteve Price /*
11047edcb936SSteve Price * Check user's default group
11057edcb936SSteve Price */
11067edcb936SSteve Price if (pwset && grp->gr_gid == pw->pw_gid)
11077edcb936SSteve Price found = 1;
11087edcb936SSteve Price /*
11097edcb936SSteve Price * Check supplementary groups
11107edcb936SSteve Price */
111131fea7b8SDavid Nugent while (!found && grp->gr_mem[i])
111231fea7b8SDavid Nugent found = strcmp(name,
111331fea7b8SDavid Nugent grp->gr_mem[i++])
111431fea7b8SDavid Nugent == 0;
1115ea022d16SRodney W. Grimes }
11168657b576SYaroslav Tykhiy }
111731fea7b8SDavid Nugent /*
111831fea7b8SDavid Nugent * Otherwise, just check for username match
111931fea7b8SDavid Nugent */
112031fea7b8SDavid Nugent else
1121737d08f3SYaroslav Tykhiy found = strcmp(p, name) == 0;
11228657b576SYaroslav Tykhiy /*
11238657b576SYaroslav Tykhiy * Save the rest of line to "residue" if matched
11248657b576SYaroslav Tykhiy */
11258657b576SYaroslav Tykhiy if (found && residue) {
11260ba71e24SYaroslav Tykhiy if ((p = strtok(NULL, "")) != NULL)
11270ba71e24SYaroslav Tykhiy p += strspn(p, " \t");
11280ba71e24SYaroslav Tykhiy if (p && *p) {
11298657b576SYaroslav Tykhiy if ((*residue = strdup(p)) == NULL)
11308657b576SYaroslav Tykhiy fatalerror("Ran out of memory.");
11318657b576SYaroslav Tykhiy } else
11328657b576SYaroslav Tykhiy *residue = NULL;
11338657b576SYaroslav Tykhiy }
1134737d08f3SYaroslav Tykhiy nextline:
1135737d08f3SYaroslav Tykhiy if (mp)
1136737d08f3SYaroslav Tykhiy free(mp);
1137ea022d16SRodney W. Grimes }
1138ea022d16SRodney W. Grimes (void) fclose(fd);
1139cefb6785SChristian S.J. Peron } else if (ecode != NULL)
1140cefb6785SChristian S.J. Peron *ecode = errno;
1141ea022d16SRodney W. Grimes return (found);
1142ea022d16SRodney W. Grimes }
1143ea022d16SRodney W. Grimes
1144ea022d16SRodney W. Grimes /*
1145ea022d16SRodney W. Grimes * Terminate login as previous user, if any, resetting state;
1146ea022d16SRodney W. Grimes * used when USER command is given or login fails.
1147ea022d16SRodney W. Grimes */
1148ea022d16SRodney W. Grimes static void
end_login(void)1149e4bc453cSWarner Losh end_login(void)
1150ea022d16SRodney W. Grimes {
11515bc9d93dSMark Murray #ifdef USE_PAM
11525bc9d93dSMark Murray int e;
11535bc9d93dSMark Murray #endif
1154ea022d16SRodney W. Grimes
115552e7ee74SYaroslav Tykhiy (void) seteuid(0);
1156b071c689SDavid Nugent #ifdef LOGIN_CAP
1157e81b1c71SEdward Tomasz Napierala setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN |
1158e81b1c71SEdward Tomasz Napierala LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH |
1159e81b1c71SEdward Tomasz Napierala LOGIN_SETENV));
1160b071c689SDavid Nugent #endif
1161de8d85c9SEugene Grosbein if (logged_in && dowtmp)
1162de8d85c9SEugene Grosbein ftpd_logwtmp(wtmpid, NULL, NULL);
1163de8d85c9SEugene Grosbein pw = NULL;
11645bc9d93dSMark Murray #ifdef USE_PAM
1165de45162dSYaroslav Tykhiy if (pamh) {
11665bc9d93dSMark Murray if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS)
11675bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
11685bc9d93dSMark Murray if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS)
11695bc9d93dSMark Murray syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e));
11705bc9d93dSMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS)
11715bc9d93dSMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
11725bc9d93dSMark Murray pamh = NULL;
1173de45162dSYaroslav Tykhiy }
11745bc9d93dSMark Murray #endif
1175ea022d16SRodney W. Grimes logged_in = 0;
1176ea022d16SRodney W. Grimes guest = 0;
1177a5a4544eSPaul Traina dochroot = 0;
1178ea022d16SRodney W. Grimes }
1179ea022d16SRodney W. Grimes
11805bc9d93dSMark Murray #ifdef USE_PAM
11816c9134c0SMark Murray
11826c9134c0SMark Murray /*
11836c9134c0SMark Murray * the following code is stolen from imap-uw PAM authentication module and
11846c9134c0SMark Murray * login.c
11856c9134c0SMark Murray */
11866c9134c0SMark Murray #define COPY_STRING(s) (s ? strdup(s) : NULL)
11876c9134c0SMark Murray
11886c9134c0SMark Murray struct cred_t {
11896c9134c0SMark Murray const char *uname; /* user name */
11906c9134c0SMark Murray const char *pass; /* password */
11916c9134c0SMark Murray };
11926c9134c0SMark Murray typedef struct cred_t cred_t;
11936c9134c0SMark Murray
11946c9134c0SMark Murray static int
auth_conv(int num_msg,const struct pam_message ** msg,struct pam_response ** resp,void * appdata)11956c9134c0SMark Murray auth_conv(int num_msg, const struct pam_message **msg,
11966c9134c0SMark Murray struct pam_response **resp, void *appdata)
11976c9134c0SMark Murray {
11986c9134c0SMark Murray int i;
11996c9134c0SMark Murray cred_t *cred = (cred_t *) appdata;
120060769b19SDag-Erling Smørgrav struct pam_response *reply;
120160769b19SDag-Erling Smørgrav
120260769b19SDag-Erling Smørgrav reply = calloc(num_msg, sizeof *reply);
120360769b19SDag-Erling Smørgrav if (reply == NULL)
120460769b19SDag-Erling Smørgrav return PAM_BUF_ERR;
12056c9134c0SMark Murray
12066c9134c0SMark Murray for (i = 0; i < num_msg; i++) {
12076c9134c0SMark Murray switch (msg[i]->msg_style) {
12086c9134c0SMark Murray case PAM_PROMPT_ECHO_ON: /* assume want user name */
12096c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS;
12106c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->uname);
12116c9134c0SMark Murray /* PAM frees resp. */
12126c9134c0SMark Murray break;
12136c9134c0SMark Murray case PAM_PROMPT_ECHO_OFF: /* assume want password */
12146c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS;
12156c9134c0SMark Murray reply[i].resp = COPY_STRING(cred->pass);
12166c9134c0SMark Murray /* PAM frees resp. */
12176c9134c0SMark Murray break;
12186c9134c0SMark Murray case PAM_TEXT_INFO:
12196c9134c0SMark Murray case PAM_ERROR_MSG:
12206c9134c0SMark Murray reply[i].resp_retcode = PAM_SUCCESS;
12216c9134c0SMark Murray reply[i].resp = NULL;
12226c9134c0SMark Murray break;
12236c9134c0SMark Murray default: /* unknown message style */
12246c9134c0SMark Murray free(reply);
12256c9134c0SMark Murray return PAM_CONV_ERR;
12266c9134c0SMark Murray }
12276c9134c0SMark Murray }
12286c9134c0SMark Murray
12296c9134c0SMark Murray *resp = reply;
12306c9134c0SMark Murray return PAM_SUCCESS;
12316c9134c0SMark Murray }
12326c9134c0SMark Murray
12336c9134c0SMark Murray /*
12346c9134c0SMark Murray * Attempt to authenticate the user using PAM. Returns 0 if the user is
12356c9134c0SMark Murray * authenticated, or 1 if not authenticated. If some sort of PAM system
12366c9134c0SMark Murray * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
12376c9134c0SMark Murray * function returns -1. This can be used as an indication that we should
12386c9134c0SMark Murray * fall back to a different authentication mechanism.
12396c9134c0SMark Murray */
12406c9134c0SMark Murray static int
auth_pam(struct passwd ** ppw,const char * pass)12416c9134c0SMark Murray auth_pam(struct passwd **ppw, const char *pass)
12426c9134c0SMark Murray {
12436c9134c0SMark Murray const char *tmpl_user;
12446c9134c0SMark Murray const void *item;
12456c9134c0SMark Murray int rval;
12466c9134c0SMark Murray int e;
12476c9134c0SMark Murray cred_t auth_cred = { (*ppw)->pw_name, pass };
12486c9134c0SMark Murray struct pam_conv conv = { &auth_conv, &auth_cred };
12496c9134c0SMark Murray
12506c9134c0SMark Murray e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh);
12516c9134c0SMark Murray if (e != PAM_SUCCESS) {
1252545ea864SYaroslav Tykhiy /*
1253545ea864SYaroslav Tykhiy * In OpenPAM, it's OK to pass NULL to pam_strerror()
1254545ea864SYaroslav Tykhiy * if context creation has failed in the first place.
1255545ea864SYaroslav Tykhiy */
1256545ea864SYaroslav Tykhiy syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e));
12576c9134c0SMark Murray return -1;
12586c9134c0SMark Murray }
12596c9134c0SMark Murray
1260ea413ab7SGuido van Rooij e = pam_set_item(pamh, PAM_RHOST, remotehost);
1261ea413ab7SGuido van Rooij if (e != PAM_SUCCESS) {
1262ea413ab7SGuido van Rooij syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
1263ea413ab7SGuido van Rooij pam_strerror(pamh, e));
1264de45162dSYaroslav Tykhiy if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
1265de45162dSYaroslav Tykhiy syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
1266de45162dSYaroslav Tykhiy }
1267de45162dSYaroslav Tykhiy pamh = NULL;
1268ea413ab7SGuido van Rooij return -1;
1269ea413ab7SGuido van Rooij }
1270ea413ab7SGuido van Rooij
12716c9134c0SMark Murray e = pam_authenticate(pamh, 0);
12726c9134c0SMark Murray switch (e) {
12736c9134c0SMark Murray case PAM_SUCCESS:
12746c9134c0SMark Murray /*
12756c9134c0SMark Murray * With PAM we support the concept of a "template"
12766c9134c0SMark Murray * user. The user enters a login name which is
12776c9134c0SMark Murray * authenticated by PAM, usually via a remote service
12786c9134c0SMark Murray * such as RADIUS or TACACS+. If authentication
12796c9134c0SMark Murray * succeeds, a different but related "template" name
12806c9134c0SMark Murray * is used for setting the credentials, shell, and
12816c9134c0SMark Murray * home directory. The name the user enters need only
12826c9134c0SMark Murray * exist on the remote authentication server, but the
12836c9134c0SMark Murray * template name must be present in the local password
12846c9134c0SMark Murray * database.
12856c9134c0SMark Murray *
12866c9134c0SMark Murray * This is supported by two various mechanisms in the
12876c9134c0SMark Murray * individual modules. However, from the application's
12886c9134c0SMark Murray * point of view, the template user is always passed
12896c9134c0SMark Murray * back as a changed value of the PAM_USER item.
12906c9134c0SMark Murray */
12916c9134c0SMark Murray if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
12926c9134c0SMark Murray PAM_SUCCESS) {
12936c9134c0SMark Murray tmpl_user = (const char *) item;
12946c9134c0SMark Murray if (strcmp((*ppw)->pw_name, tmpl_user) != 0)
12956c9134c0SMark Murray *ppw = getpwnam(tmpl_user);
12966c9134c0SMark Murray } else
12976c9134c0SMark Murray syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
12986c9134c0SMark Murray pam_strerror(pamh, e));
12996c9134c0SMark Murray rval = 0;
13006c9134c0SMark Murray break;
13016c9134c0SMark Murray
13026c9134c0SMark Murray case PAM_AUTH_ERR:
13036c9134c0SMark Murray case PAM_USER_UNKNOWN:
13046c9134c0SMark Murray case PAM_MAXTRIES:
13056c9134c0SMark Murray rval = 1;
13066c9134c0SMark Murray break;
13076c9134c0SMark Murray
13086c9134c0SMark Murray default:
13095bc9d93dSMark Murray syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
13106c9134c0SMark Murray rval = -1;
13116c9134c0SMark Murray break;
13126c9134c0SMark Murray }
13136c9134c0SMark Murray
13145bc9d93dSMark Murray if (rval == 0) {
13155bc9d93dSMark Murray e = pam_acct_mgmt(pamh, 0);
13165bc9d93dSMark Murray if (e != PAM_SUCCESS) {
13174cbc4ad6SYaroslav Tykhiy syslog(LOG_ERR, "pam_acct_mgmt: %s",
13184cbc4ad6SYaroslav Tykhiy pam_strerror(pamh, e));
13195bc9d93dSMark Murray rval = 1;
13205bc9d93dSMark Murray }
13215bc9d93dSMark Murray }
13225bc9d93dSMark Murray
13235bc9d93dSMark Murray if (rval != 0) {
13246c9134c0SMark Murray if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
13256c9134c0SMark Murray syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
13265bc9d93dSMark Murray }
13275bc9d93dSMark Murray pamh = NULL;
13286c9134c0SMark Murray }
13296c9134c0SMark Murray return rval;
13306c9134c0SMark Murray }
13316c9134c0SMark Murray
13325bc9d93dSMark Murray #endif /* USE_PAM */
13336c9134c0SMark Murray
1334ea022d16SRodney W. Grimes void
pass(char * passwd)1335e4bc453cSWarner Losh pass(char *passwd)
1336ea022d16SRodney W. Grimes {
1337cefb6785SChristian S.J. Peron int rval, ecode;
1338ea022d16SRodney W. Grimes FILE *fd;
1339b071c689SDavid Nugent #ifdef LOGIN_CAP
1340b071c689SDavid Nugent login_cap_t *lc = NULL;
1341b071c689SDavid Nugent #endif
13425bc9d93dSMark Murray #ifdef USE_PAM
13435bc9d93dSMark Murray int e;
13445bc9d93dSMark Murray #endif
1345341e476eSYaroslav Tykhiy char *residue = NULL;
134647499ecdSAndrey A. Chernov char *xpasswd;
1347ea022d16SRodney W. Grimes
1348ea022d16SRodney W. Grimes if (logged_in || askpasswd == 0) {
1349ea022d16SRodney W. Grimes reply(503, "Login with USER first.");
1350ea022d16SRodney W. Grimes return;
1351ea022d16SRodney W. Grimes }
1352ea022d16SRodney W. Grimes askpasswd = 0;
1353ea022d16SRodney W. Grimes if (!guest) { /* "ftp" is only account allowed no password */
1354a5a4544eSPaul Traina if (pw == NULL) {
1355a5a4544eSPaul Traina rval = 1; /* failure below */
1356a5a4544eSPaul Traina goto skip;
1357a5a4544eSPaul Traina }
13585bc9d93dSMark Murray #ifdef USE_PAM
13596c9134c0SMark Murray rval = auth_pam(&pw, passwd);
1360f650a124SAndrey A. Chernov if (rval >= 0) {
1361a5a4544eSPaul Traina goto skip;
1362f650a124SAndrey A. Chernov }
1363f650a124SAndrey A. Chernov #endif
136447499ecdSAndrey A. Chernov xpasswd = crypt(passwd, pw->pw_passwd);
1365f650a124SAndrey A. Chernov if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0')
1366f650a124SAndrey A. Chernov xpasswd = ":";
136747499ecdSAndrey A. Chernov rval = strcmp(pw->pw_passwd, xpasswd);
1368f650a124SAndrey A. Chernov if (pw->pw_expire && time(NULL) >= pw->pw_expire)
1369a5a4544eSPaul Traina rval = 1; /* failure */
1370a5a4544eSPaul Traina skip:
1371a5a4544eSPaul Traina /*
1372a5a4544eSPaul Traina * If rval == 1, the user failed the authentication check
13736c9134c0SMark Murray * above. If rval == 0, either PAM or local authentication
1374a5a4544eSPaul Traina * succeeded.
1375a5a4544eSPaul Traina */
1376a5a4544eSPaul Traina if (rval) {
1377ea022d16SRodney W. Grimes reply(530, "Login incorrect.");
1378e07d11b6SKurt Lidl BLACKLIST_NOTIFY(BLACKLIST_AUTH_FAIL, STDIN_FILENO, "Login incorrect");
1379b8939f6fSYaroslav Tykhiy if (logging) {
1380ea022d16SRodney W. Grimes syslog(LOG_NOTICE,
1381b8939f6fSYaroslav Tykhiy "FTP LOGIN FAILED FROM %s",
1382b8939f6fSYaroslav Tykhiy remotehost);
1383b8939f6fSYaroslav Tykhiy syslog(LOG_AUTHPRIV | LOG_NOTICE,
1384ea022d16SRodney W. Grimes "FTP LOGIN FAILED FROM %s, %s",
1385ea022d16SRodney W. Grimes remotehost, curname);
1386b8939f6fSYaroslav Tykhiy }
1387ea022d16SRodney W. Grimes pw = NULL;
1388ea022d16SRodney W. Grimes if (login_attempts++ >= 5) {
1389ea022d16SRodney W. Grimes syslog(LOG_NOTICE,
1390ea022d16SRodney W. Grimes "repeated login failures from %s",
1391ea022d16SRodney W. Grimes remotehost);
1392ea022d16SRodney W. Grimes exit(0);
1393ea022d16SRodney W. Grimes }
1394ea022d16SRodney W. Grimes return;
1395e07d11b6SKurt Lidl } else {
1396e07d11b6SKurt Lidl BLACKLIST_NOTIFY(BLACKLIST_AUTH_OK, STDIN_FILENO, "Login successful");
1397ea022d16SRodney W. Grimes }
1398ea022d16SRodney W. Grimes }
1399ea022d16SRodney W. Grimes login_attempts = 0; /* this time successful */
1400c4536e21SYaroslav Tykhiy if (setegid(pw->pw_gid) < 0) {
1401ea022d16SRodney W. Grimes reply(550, "Can't set gid.");
1402ea022d16SRodney W. Grimes return;
1403ea022d16SRodney W. Grimes }
1404b071c689SDavid Nugent /* May be overridden by login.conf */
1405b071c689SDavid Nugent (void) umask(defumask);
1406b071c689SDavid Nugent #ifdef LOGIN_CAP
14075d0bfe39SDavid Nugent if ((lc = login_getpwclass(pw)) != NULL) {
140804683b2cSYaroslav Tykhiy char remote_ip[NI_MAXHOST];
1409b071c689SDavid Nugent
141004683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
14114dd8b5abSYoshinobu Inoue remote_ip, sizeof(remote_ip) - 1, NULL, 0,
141204683b2cSYaroslav Tykhiy NI_NUMERICHOST))
141304683b2cSYaroslav Tykhiy *remote_ip = 0;
1414b071c689SDavid Nugent remote_ip[sizeof(remote_ip) - 1] = 0;
1415b071c689SDavid Nugent if (!auth_hostok(lc, remotehost, remote_ip)) {
1416b071c689SDavid Nugent syslog(LOG_INFO|LOG_AUTH,
1417b071c689SDavid Nugent "FTP LOGIN FAILED (HOST) as %s: permission denied.",
1418b071c689SDavid Nugent pw->pw_name);
14194a3e5acdSYaroslav Tykhiy reply(530, "Permission denied.");
1420b071c689SDavid Nugent pw = NULL;
1421b071c689SDavid Nugent return;
1422b071c689SDavid Nugent }
1423b071c689SDavid Nugent if (!auth_timeok(lc, time(NULL))) {
14244a3e5acdSYaroslav Tykhiy reply(530, "Login not available right now.");
1425b071c689SDavid Nugent pw = NULL;
1426b071c689SDavid Nugent return;
1427b071c689SDavid Nugent }
1428b071c689SDavid Nugent }
1429e81b1c71SEdward Tomasz Napierala setusercontext(lc, pw, 0, LOGIN_SETALL &
1430de8d85c9SEugene Grosbein ~(LOGIN_SETRESOURCES | LOGIN_SETUSER | LOGIN_SETPATH | LOGIN_SETENV));
1431b071c689SDavid Nugent #else
1432e6fa0d43SDag-Erling Smørgrav setlogin(pw->pw_name);
1433ea022d16SRodney W. Grimes (void) initgroups(pw->pw_name, pw->pw_gid);
1434b071c689SDavid Nugent #endif
1435ea022d16SRodney W. Grimes
14365bc9d93dSMark Murray #ifdef USE_PAM
14375bc9d93dSMark Murray if (pamh) {
14385bc9d93dSMark Murray if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
14395bc9d93dSMark Murray syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e));
14405bc9d93dSMark Murray } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
14415bc9d93dSMark Murray syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e));
14425bc9d93dSMark Murray }
14435bc9d93dSMark Murray }
14445bc9d93dSMark Murray #endif
14455bc9d93dSMark Murray
144680643af0SEd Schouten dochroot =
1447cefb6785SChristian S.J. Peron checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue, &ecode)
144880643af0SEd Schouten #ifdef LOGIN_CAP /* Allow login.conf configuration as well */
144980643af0SEd Schouten || login_getcapbool(lc, "ftp-chroot", 0)
145080643af0SEd Schouten #endif
145180643af0SEd Schouten ;
1452cefb6785SChristian S.J. Peron /*
1453cefb6785SChristian S.J. Peron * It is possible that checkuser() failed to open the chroot file.
1454cefb6785SChristian S.J. Peron * If this is the case, report that logins are un-available, since we
1455cefb6785SChristian S.J. Peron * have no way of checking whether or not the user should be chrooted.
1456cefb6785SChristian S.J. Peron * We ignore ENOENT since it is not required that this file be present.
1457cefb6785SChristian S.J. Peron */
1458cefb6785SChristian S.J. Peron if (ecode != 0 && ecode != ENOENT) {
1459cefb6785SChristian S.J. Peron reply(530, "Login not available right now.");
1460cefb6785SChristian S.J. Peron return;
1461cefb6785SChristian S.J. Peron }
146280643af0SEd Schouten chrootdir = NULL;
146380643af0SEd Schouten
14649f37b1a2SEd Schouten /* Disable wtmp logging when chrooting. */
14659f37b1a2SEd Schouten if (dochroot || guest)
14669f37b1a2SEd Schouten dowtmp = 0;
14679f37b1a2SEd Schouten if (dowtmp)
146880643af0SEd Schouten ftpd_logwtmp(wtmpid, pw->pw_name,
14695d7e0128SYaroslav Tykhiy (struct sockaddr *)&his_addr);
1470ea022d16SRodney W. Grimes logged_in = 1;
1471ea022d16SRodney W. Grimes
1472de8d85c9SEugene Grosbein #ifdef LOGIN_CAP
1473de8d85c9SEugene Grosbein setusercontext(lc, pw, 0, LOGIN_SETRESOURCES);
1474de8d85c9SEugene Grosbein #endif
1475de8d85c9SEugene Grosbein
14763c0c1e01SMark Johnston if (guest && stats && statfd < 0) {
1477ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
147863047c6fSDavid E. O'Brien statfd = open(thishost->statfile, O_WRONLY|O_APPEND);
1479ea4e54b9SDavid Nugent #else
148063047c6fSDavid E. O'Brien statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND);
1481ea4e54b9SDavid Nugent #endif
148263047c6fSDavid E. O'Brien if (statfd < 0)
14833eb568f2SGuido van Rooij stats = 0;
14843c0c1e01SMark Johnston }
14853eb568f2SGuido van Rooij
1486ea022d16SRodney W. Grimes /*
1487ce9287fcSYaroslav Tykhiy * For a chrooted local user,
1488ce9287fcSYaroslav Tykhiy * a) see whether ftpchroot(5) specifies a chroot directory,
1489ce9287fcSYaroslav Tykhiy * b) extract the directory pathname from the line,
1490ce9287fcSYaroslav Tykhiy * c) expand it to the absolute pathname if necessary.
1491ea022d16SRodney W. Grimes */
1492ce9287fcSYaroslav Tykhiy if (dochroot && residue &&
149339bce482SYaroslav Tykhiy (chrootdir = strtok(residue, " \t")) != NULL) {
149439bce482SYaroslav Tykhiy if (chrootdir[0] != '/')
1495341e476eSYaroslav Tykhiy asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir);
149639bce482SYaroslav Tykhiy else
1497215a9f9dSYaroslav Tykhiy chrootdir = strdup(chrootdir); /* make it permanent */
1498341e476eSYaroslav Tykhiy if (chrootdir == NULL)
14998657b576SYaroslav Tykhiy fatalerror("Ran out of memory.");
15008657b576SYaroslav Tykhiy }
1501ce9287fcSYaroslav Tykhiy if (guest || dochroot) {
1502ce9287fcSYaroslav Tykhiy /*
1503ce9287fcSYaroslav Tykhiy * If no chroot directory set yet, use the login directory.
1504ce9287fcSYaroslav Tykhiy * Copy it so it can be modified while pw->pw_dir stays intact.
1505ce9287fcSYaroslav Tykhiy */
1506ce9287fcSYaroslav Tykhiy if (chrootdir == NULL &&
1507ce9287fcSYaroslav Tykhiy (chrootdir = strdup(pw->pw_dir)) == NULL)
1508ce9287fcSYaroslav Tykhiy fatalerror("Ran out of memory.");
1509ce9287fcSYaroslav Tykhiy /*
1510ce9287fcSYaroslav Tykhiy * Check for the "/chroot/./home" syntax,
1511ce9287fcSYaroslav Tykhiy * separate the chroot and home directory pathnames.
1512ce9287fcSYaroslav Tykhiy */
1513ce9287fcSYaroslav Tykhiy if ((homedir = strstr(chrootdir, "/./")) != NULL) {
1514ce9287fcSYaroslav Tykhiy *(homedir++) = '\0'; /* wipe '/' */
1515ce9287fcSYaroslav Tykhiy homedir++; /* skip '.' */
1516ce9287fcSYaroslav Tykhiy } else {
1517ce9287fcSYaroslav Tykhiy /*
1518ce9287fcSYaroslav Tykhiy * We MUST do a chdir() after the chroot. Otherwise
1519ce9287fcSYaroslav Tykhiy * the old current directory will be accessible as "."
1520ce9287fcSYaroslav Tykhiy * outside the new root!
1521ce9287fcSYaroslav Tykhiy */
1522ce9287fcSYaroslav Tykhiy homedir = "/";
1523ce9287fcSYaroslav Tykhiy }
1524ce9287fcSYaroslav Tykhiy /*
1525ce9287fcSYaroslav Tykhiy * Finally, do chroot()
1526ce9287fcSYaroslav Tykhiy */
1527ce9287fcSYaroslav Tykhiy if (chroot(chrootdir) < 0) {
1528a5a4544eSPaul Traina reply(550, "Can't change root.");
1529a5a4544eSPaul Traina goto bad;
1530a5a4544eSPaul Traina }
15313e65b9c6SColin Percival __FreeBSD_libc_enter_restricted_mode();
1532ce9287fcSYaroslav Tykhiy } else /* real user w/o chroot */
1533ce9287fcSYaroslav Tykhiy homedir = pw->pw_dir;
1534ce9287fcSYaroslav Tykhiy /*
1535ce9287fcSYaroslav Tykhiy * Set euid *before* doing chdir() so
1536ce9287fcSYaroslav Tykhiy * a) the user won't be carried to a directory that he couldn't reach
1537ce9287fcSYaroslav Tykhiy * on his own due to no permission to upper path components,
1538ce9287fcSYaroslav Tykhiy * b) NFS mounted homedirs w/restrictive permissions will be accessible
1539ce9287fcSYaroslav Tykhiy * (uid 0 has no root power over NFS if not mapped explicitly.)
1540ce9287fcSYaroslav Tykhiy */
154152e7ee74SYaroslav Tykhiy if (seteuid(pw->pw_uid) < 0) {
15422ac43100SMark Johnston if (guest || dochroot) {
15432ac43100SMark Johnston fatalerror("Can't set uid.");
15442ac43100SMark Johnston } else {
1545ea022d16SRodney W. Grimes reply(550, "Can't set uid.");
1546ea022d16SRodney W. Grimes goto bad;
1547ea022d16SRodney W. Grimes }
15482ac43100SMark Johnston }
15492ac43100SMark Johnston /*
15502ac43100SMark Johnston * Do not allow the session to live if we're chroot()'ed and chdir()
15512ac43100SMark Johnston * fails. Otherwise the chroot jail can be escaped.
15522ac43100SMark Johnston */
1553ce9287fcSYaroslav Tykhiy if (chdir(homedir) < 0) {
1554ce9287fcSYaroslav Tykhiy if (guest || dochroot) {
15552ac43100SMark Johnston fatalerror("Can't change to base directory.");
1556ce9287fcSYaroslav Tykhiy } else {
1557ce9287fcSYaroslav Tykhiy if (chdir("/") < 0) {
1558ce9287fcSYaroslav Tykhiy reply(550, "Root is inaccessible.");
1559ce9287fcSYaroslav Tykhiy goto bad;
1560ce9287fcSYaroslav Tykhiy }
156102c97492SYaroslav Tykhiy lreply(230, "No directory! Logging in with home=/.");
1562ce9287fcSYaroslav Tykhiy }
1563ce9287fcSYaroslav Tykhiy }
156482c76939SDavid Greenman
156582c76939SDavid Greenman /*
1566ea022d16SRodney W. Grimes * Display a login message, if it exists.
1567ea022d16SRodney W. Grimes * N.B. reply(230,) must follow the message.
1568ea022d16SRodney W. Grimes */
1569ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
157063047c6fSDavid E. O'Brien fd = fopen(thishost->loginmsg, "r");
1571ea4e54b9SDavid Nugent #else
157263047c6fSDavid E. O'Brien fd = fopen(_PATH_FTPLOGINMESG, "r");
1573ea4e54b9SDavid Nugent #endif
157463047c6fSDavid E. O'Brien if (fd != NULL) {
1575ea022d16SRodney W. Grimes char *cp, line[LINE_MAX];
1576ea022d16SRodney W. Grimes
1577ea022d16SRodney W. Grimes while (fgets(line, sizeof(line), fd) != NULL) {
1578ea022d16SRodney W. Grimes if ((cp = strchr(line, '\n')) != NULL)
1579ea022d16SRodney W. Grimes *cp = '\0';
1580ea022d16SRodney W. Grimes lreply(230, "%s", line);
1581ea022d16SRodney W. Grimes }
1582ea022d16SRodney W. Grimes (void) fflush(stdout);
1583ea022d16SRodney W. Grimes (void) fclose(fd);
1584ea022d16SRodney W. Grimes }
1585ea022d16SRodney W. Grimes if (guest) {
15863eb568f2SGuido van Rooij if (ident != NULL)
15873eb568f2SGuido van Rooij free(ident);
158861f891a6SPaul Traina ident = strdup(passwd);
158961f891a6SPaul Traina if (ident == NULL)
1590618b0bbaSMark Murray fatalerror("Ran out of memory.");
159161f891a6SPaul Traina
1592ea022d16SRodney W. Grimes reply(230, "Guest login ok, access restrictions apply.");
1593ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1594ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
1595ea4e54b9SDavid Nugent if (thishost != firsthost)
1596ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle),
1597b3a0a7cdSMike Heffner "%s: anonymous(%s)/%s", remotehost, hostname,
1598b3a0a7cdSMike Heffner passwd);
1599ea4e54b9SDavid Nugent else
1600ea4e54b9SDavid Nugent #endif
1601ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle),
1602b3a0a7cdSMike Heffner "%s: anonymous/%s", remotehost, passwd);
1603b63e1fe2SPeter Wemm setproctitle("%s", proctitle);
1604ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1605ea022d16SRodney W. Grimes if (logging)
1606ea022d16SRodney W. Grimes syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s",
1607ea022d16SRodney W. Grimes remotehost, passwd);
1608ea022d16SRodney W. Grimes } else {
16093401a71fSDaniel O'Callaghan if (dochroot)
161011342ab1SYaroslav Tykhiy reply(230, "User %s logged in, "
161111342ab1SYaroslav Tykhiy "access restrictions apply.", pw->pw_name);
16123401a71fSDaniel O'Callaghan else
1613ea022d16SRodney W. Grimes reply(230, "User %s logged in.", pw->pw_name);
16143401a71fSDaniel O'Callaghan
1615ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
1616ea022d16SRodney W. Grimes snprintf(proctitle, sizeof(proctitle),
16177a29d7daSYaroslav Tykhiy "%s: user/%s", remotehost, pw->pw_name);
1618b63e1fe2SPeter Wemm setproctitle("%s", proctitle);
1619ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
1620ea022d16SRodney W. Grimes if (logging)
1621ea022d16SRodney W. Grimes syslog(LOG_INFO, "FTP LOGIN FROM %s as %s",
1622ea022d16SRodney W. Grimes remotehost, pw->pw_name);
1623ea022d16SRodney W. Grimes }
1624220223fdSYaroslav Tykhiy if (logging && (guest || dochroot))
1625e897216fSYaroslav Tykhiy syslog(LOG_INFO, "session root changed to %s", chrootdir);
1626b071c689SDavid Nugent #ifdef LOGIN_CAP
1627b071c689SDavid Nugent login_close(lc);
1628b071c689SDavid Nugent #endif
1629ce9287fcSYaroslav Tykhiy if (residue)
1630ce9287fcSYaroslav Tykhiy free(residue);
1631ea022d16SRodney W. Grimes return;
1632ea022d16SRodney W. Grimes bad:
1633ea022d16SRodney W. Grimes /* Forget all about it... */
1634b071c689SDavid Nugent #ifdef LOGIN_CAP
1635b071c689SDavid Nugent login_close(lc);
1636b071c689SDavid Nugent #endif
1637ce9287fcSYaroslav Tykhiy if (residue)
1638ce9287fcSYaroslav Tykhiy free(residue);
1639ea022d16SRodney W. Grimes end_login();
1640ea022d16SRodney W. Grimes }
1641ea022d16SRodney W. Grimes
1642ea022d16SRodney W. Grimes void
retrieve(char * cmd,char * name)1643e4bc453cSWarner Losh retrieve(char *cmd, char *name)
1644ea022d16SRodney W. Grimes {
1645ea022d16SRodney W. Grimes FILE *fin, *dout;
1646ea022d16SRodney W. Grimes struct stat st;
1647e4bc453cSWarner Losh int (*closefunc)(FILE *);
1648158a00b2SJohn Birrell time_t start;
16498877d1dbSDon Lewis char line[BUFSIZ];
1650ea022d16SRodney W. Grimes
1651ea022d16SRodney W. Grimes if (cmd == 0) {
1652ea022d16SRodney W. Grimes fin = fopen(name, "r"), closefunc = fclose;
1653ea022d16SRodney W. Grimes st.st_size = 0;
1654ea022d16SRodney W. Grimes } else {
16558877d1dbSDon Lewis (void) snprintf(line, sizeof(line), cmd, name);
16568877d1dbSDon Lewis name = line;
1657ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose;
1658ea022d16SRodney W. Grimes st.st_size = -1;
1659ea022d16SRodney W. Grimes st.st_blksize = BUFSIZ;
1660ea022d16SRodney W. Grimes }
1661ea022d16SRodney W. Grimes if (fin == NULL) {
1662ea022d16SRodney W. Grimes if (errno != 0) {
1663ea022d16SRodney W. Grimes perror_reply(550, name);
1664ea022d16SRodney W. Grimes if (cmd == 0) {
1665ea022d16SRodney W. Grimes LOGCMD("get", name);
1666ea022d16SRodney W. Grimes }
1667ea022d16SRodney W. Grimes }
1668ea022d16SRodney W. Grimes return;
1669ea022d16SRodney W. Grimes }
1670ea022d16SRodney W. Grimes byte_count = -1;
1671ea701226SYaroslav Tykhiy if (cmd == 0) {
1672ea701226SYaroslav Tykhiy if (fstat(fileno(fin), &st) < 0) {
1673ea701226SYaroslav Tykhiy perror_reply(550, name);
1674ea701226SYaroslav Tykhiy goto done;
1675ea701226SYaroslav Tykhiy }
1676ea701226SYaroslav Tykhiy if (!S_ISREG(st.st_mode)) {
167710e89104SYaroslav Tykhiy /*
167810e89104SYaroslav Tykhiy * Never sending a raw directory is a workaround
167910e89104SYaroslav Tykhiy * for buggy clients that will attempt to RETR
168010e89104SYaroslav Tykhiy * a directory before listing it, e.g., Mozilla.
168110e89104SYaroslav Tykhiy * Preventing a guest from getting irregular files
168210e89104SYaroslav Tykhiy * is a simple security measure.
168310e89104SYaroslav Tykhiy */
168410e89104SYaroslav Tykhiy if (S_ISDIR(st.st_mode) || guest) {
1685ea022d16SRodney W. Grimes reply(550, "%s: not a plain file.", name);
1686ea022d16SRodney W. Grimes goto done;
1687ea022d16SRodney W. Grimes }
1688ea701226SYaroslav Tykhiy st.st_size = -1;
1689ea701226SYaroslav Tykhiy /* st.st_blksize is set for all descriptor types */
1690ea701226SYaroslav Tykhiy }
1691ea701226SYaroslav Tykhiy }
1692ea022d16SRodney W. Grimes if (restart_point) {
1693ea022d16SRodney W. Grimes if (type == TYPE_A) {
1694ea022d16SRodney W. Grimes off_t i, n;
1695ea022d16SRodney W. Grimes int c;
1696ea022d16SRodney W. Grimes
1697ea022d16SRodney W. Grimes n = restart_point;
1698ea022d16SRodney W. Grimes i = 0;
1699ea022d16SRodney W. Grimes while (i++ < n) {
1700ea022d16SRodney W. Grimes if ((c=getc(fin)) == EOF) {
1701ea022d16SRodney W. Grimes perror_reply(550, name);
1702ea022d16SRodney W. Grimes goto done;
1703ea022d16SRodney W. Grimes }
1704ea022d16SRodney W. Grimes if (c == '\n')
1705ea022d16SRodney W. Grimes i++;
1706ea022d16SRodney W. Grimes }
1707ea022d16SRodney W. Grimes } else if (lseek(fileno(fin), restart_point, L_SET) < 0) {
1708ea022d16SRodney W. Grimes perror_reply(550, name);
1709ea022d16SRodney W. Grimes goto done;
1710ea022d16SRodney W. Grimes }
1711ea022d16SRodney W. Grimes }
1712ea022d16SRodney W. Grimes dout = dataconn(name, st.st_size, "w");
1713ea022d16SRodney W. Grimes if (dout == NULL)
1714ea022d16SRodney W. Grimes goto done;
17153eb568f2SGuido van Rooij time(&start);
17169fc5823aSGarrett Wollman send_data(fin, dout, st.st_blksize, st.st_size,
17179fc5823aSGarrett Wollman restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode));
1718c999732bSYaroslav Tykhiy if (cmd == 0 && guest && stats && byte_count > 0)
1719c999732bSYaroslav Tykhiy logxfer(name, byte_count, start);
1720ea022d16SRodney W. Grimes (void) fclose(dout);
1721ea022d16SRodney W. Grimes data = -1;
1722ea022d16SRodney W. Grimes pdata = -1;
1723ea022d16SRodney W. Grimes done:
1724ea022d16SRodney W. Grimes if (cmd == 0)
1725ea022d16SRodney W. Grimes LOGBYTES("get", name, byte_count);
1726ea022d16SRodney W. Grimes (*closefunc)(fin);
1727ea022d16SRodney W. Grimes }
1728ea022d16SRodney W. Grimes
1729ea022d16SRodney W. Grimes void
store(char * name,char * mode,int unique)1730e4bc453cSWarner Losh store(char *name, char *mode, int unique)
1731ea022d16SRodney W. Grimes {
1732a117c345SYaroslav Tykhiy int fd;
1733ea022d16SRodney W. Grimes FILE *fout, *din;
1734e4bc453cSWarner Losh int (*closefunc)(FILE *);
1735ea022d16SRodney W. Grimes
1736a117c345SYaroslav Tykhiy if (*mode == 'a') { /* APPE */
1737a117c345SYaroslav Tykhiy if (unique) {
1738a117c345SYaroslav Tykhiy /* Programming error */
1739a117c345SYaroslav Tykhiy syslog(LOG_ERR, "Internal: unique flag to APPE");
1740a117c345SYaroslav Tykhiy unique = 0;
1741a117c345SYaroslav Tykhiy }
1742a117c345SYaroslav Tykhiy if (guest && noguestmod) {
174302c97492SYaroslav Tykhiy reply(550, "Appending to existing file denied.");
1744a117c345SYaroslav Tykhiy goto err;
1745a117c345SYaroslav Tykhiy }
1746a117c345SYaroslav Tykhiy restart_point = 0; /* not affected by preceding REST */
1747a117c345SYaroslav Tykhiy }
1748a117c345SYaroslav Tykhiy if (unique) /* STOU overrides REST */
1749a117c345SYaroslav Tykhiy restart_point = 0;
1750a117c345SYaroslav Tykhiy if (guest && noguestmod) {
1751a117c345SYaroslav Tykhiy if (restart_point) { /* guest STOR w/REST */
175202c97492SYaroslav Tykhiy reply(550, "Modifying existing file denied.");
1753a117c345SYaroslav Tykhiy goto err;
1754a117c345SYaroslav Tykhiy } else /* treat guest STOR as STOU */
1755a117c345SYaroslav Tykhiy unique = 1;
1756ea022d16SRodney W. Grimes }
1757ea022d16SRodney W. Grimes
1758ea022d16SRodney W. Grimes if (restart_point)
1759a117c345SYaroslav Tykhiy mode = "r+"; /* so ASCII manual seek can work */
1760a117c345SYaroslav Tykhiy if (unique) {
1761a117c345SYaroslav Tykhiy if ((fd = guniquefd(name, &name)) < 0)
1762a117c345SYaroslav Tykhiy goto err;
1763a117c345SYaroslav Tykhiy fout = fdopen(fd, mode);
1764a117c345SYaroslav Tykhiy } else
1765ea022d16SRodney W. Grimes fout = fopen(name, mode);
1766ea022d16SRodney W. Grimes closefunc = fclose;
1767ea022d16SRodney W. Grimes if (fout == NULL) {
1768ea022d16SRodney W. Grimes perror_reply(553, name);
1769a117c345SYaroslav Tykhiy goto err;
1770ea022d16SRodney W. Grimes }
1771ea022d16SRodney W. Grimes byte_count = -1;
1772ea022d16SRodney W. Grimes if (restart_point) {
1773ea022d16SRodney W. Grimes if (type == TYPE_A) {
1774ea022d16SRodney W. Grimes off_t i, n;
1775ea022d16SRodney W. Grimes int c;
1776ea022d16SRodney W. Grimes
1777ea022d16SRodney W. Grimes n = restart_point;
1778ea022d16SRodney W. Grimes i = 0;
1779ea022d16SRodney W. Grimes while (i++ < n) {
1780ea022d16SRodney W. Grimes if ((c=getc(fout)) == EOF) {
1781ea022d16SRodney W. Grimes perror_reply(550, name);
1782ea022d16SRodney W. Grimes goto done;
1783ea022d16SRodney W. Grimes }
1784ea022d16SRodney W. Grimes if (c == '\n')
1785ea022d16SRodney W. Grimes i++;
1786ea022d16SRodney W. Grimes }
1787ea022d16SRodney W. Grimes /*
1788ea022d16SRodney W. Grimes * We must do this seek to "current" position
1789ea022d16SRodney W. Grimes * because we are changing from reading to
1790ea022d16SRodney W. Grimes * writing.
1791ea022d16SRodney W. Grimes */
17920e519c96SYaroslav Tykhiy if (fseeko(fout, 0, SEEK_CUR) < 0) {
1793ea022d16SRodney W. Grimes perror_reply(550, name);
1794ea022d16SRodney W. Grimes goto done;
1795ea022d16SRodney W. Grimes }
1796ea022d16SRodney W. Grimes } else if (lseek(fileno(fout), restart_point, L_SET) < 0) {
1797ea022d16SRodney W. Grimes perror_reply(550, name);
1798ea022d16SRodney W. Grimes goto done;
1799ea022d16SRodney W. Grimes }
1800ea022d16SRodney W. Grimes }
18010e519c96SYaroslav Tykhiy din = dataconn(name, -1, "r");
1802ea022d16SRodney W. Grimes if (din == NULL)
1803ea022d16SRodney W. Grimes goto done;
1804ea022d16SRodney W. Grimes if (receive_data(din, fout) == 0) {
1805ea022d16SRodney W. Grimes if (unique)
1806ea022d16SRodney W. Grimes reply(226, "Transfer complete (unique file name:%s).",
1807ea022d16SRodney W. Grimes name);
1808ea022d16SRodney W. Grimes else
1809ea022d16SRodney W. Grimes reply(226, "Transfer complete.");
1810ea022d16SRodney W. Grimes }
1811ea022d16SRodney W. Grimes (void) fclose(din);
1812ea022d16SRodney W. Grimes data = -1;
1813ea022d16SRodney W. Grimes pdata = -1;
1814ea022d16SRodney W. Grimes done:
18157c20f337SYaroslav Tykhiy LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count);
1816ea022d16SRodney W. Grimes (*closefunc)(fout);
1817a117c345SYaroslav Tykhiy return;
1818a117c345SYaroslav Tykhiy err:
1819a117c345SYaroslav Tykhiy LOGCMD(*mode == 'a' ? "append" : "put" , name);
1820a117c345SYaroslav Tykhiy return;
1821ea022d16SRodney W. Grimes }
1822ea022d16SRodney W. Grimes
1823ea022d16SRodney W. Grimes static FILE *
getdatasock(char * mode)1824e4bc453cSWarner Losh getdatasock(char *mode)
1825ea022d16SRodney W. Grimes {
1826ea022d16SRodney W. Grimes int on = 1, s, t, tries;
1827ea022d16SRodney W. Grimes
1828ea022d16SRodney W. Grimes if (data >= 0)
1829ea022d16SRodney W. Grimes return (fdopen(data, mode));
18304dd8b5abSYoshinobu Inoue
18314dd8b5abSYoshinobu Inoue s = socket(data_dest.su_family, SOCK_STREAM, 0);
1832ea022d16SRodney W. Grimes if (s < 0)
1833ea022d16SRodney W. Grimes goto bad;
183457d4ef07SYaroslav Tykhiy if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
1835406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m");
1836ea022d16SRodney W. Grimes /* anchor socket to avoid multi-homing problems */
18374dd8b5abSYoshinobu Inoue data_source = ctrl_addr;
183863591ba5SYaroslav Tykhiy data_source.su_port = htons(dataport);
183952e7ee74SYaroslav Tykhiy (void) seteuid(0);
1840ea022d16SRodney W. Grimes for (tries = 1; ; tries++) {
18419ec7612aSYaroslav Tykhiy /*
18429ec7612aSYaroslav Tykhiy * We should loop here since it's possible that
18439ec7612aSYaroslav Tykhiy * another ftpd instance has passed this point and is
18449ec7612aSYaroslav Tykhiy * trying to open a data connection in active mode now.
18459ec7612aSYaroslav Tykhiy * Until the other connection is opened, we'll be getting
18469ec7612aSYaroslav Tykhiy * EADDRINUSE because no SOCK_STREAM sockets in the system
18479ec7612aSYaroslav Tykhiy * can share both local and remote addresses, localIP:20
18489ec7612aSYaroslav Tykhiy * and *:* in this case.
18499ec7612aSYaroslav Tykhiy */
1850ea022d16SRodney W. Grimes if (bind(s, (struct sockaddr *)&data_source,
18514dd8b5abSYoshinobu Inoue data_source.su_len) >= 0)
1852ea022d16SRodney W. Grimes break;
1853ea022d16SRodney W. Grimes if (errno != EADDRINUSE || tries > 10)
1854ea022d16SRodney W. Grimes goto bad;
1855ea022d16SRodney W. Grimes sleep(tries);
1856ea022d16SRodney W. Grimes }
185752e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid);
1858ea022d16SRodney W. Grimes #ifdef IP_TOS
18594dd8b5abSYoshinobu Inoue if (data_source.su_family == AF_INET)
18604dd8b5abSYoshinobu Inoue {
1861ea022d16SRodney W. Grimes on = IPTOS_THROUGHPUT;
186257d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0)
1863406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m");
18644dd8b5abSYoshinobu Inoue }
1865ea022d16SRodney W. Grimes #endif
18669fc5823aSGarrett Wollman #ifdef TCP_NOPUSH
18679fc5823aSGarrett Wollman /*
18689fc5823aSGarrett Wollman * Turn off push flag to keep sender TCP from sending short packets
186932072720SYaroslav Tykhiy * at the boundaries of each write().
18709fc5823aSGarrett Wollman */
18719fc5823aSGarrett Wollman on = 1;
187257d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0)
1873406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m");
18749fc5823aSGarrett Wollman #endif
1875ea022d16SRodney W. Grimes return (fdopen(s, mode));
1876ea022d16SRodney W. Grimes bad:
1877ea022d16SRodney W. Grimes /* Return the real value of errno (close may change it) */
1878ea022d16SRodney W. Grimes t = errno;
187952e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid);
1880ea022d16SRodney W. Grimes (void) close(s);
1881ea022d16SRodney W. Grimes errno = t;
1882ea022d16SRodney W. Grimes return (NULL);
1883ea022d16SRodney W. Grimes }
1884ea022d16SRodney W. Grimes
1885ea022d16SRodney W. Grimes static FILE *
dataconn(char * name,off_t size,char * mode)1886e4bc453cSWarner Losh dataconn(char *name, off_t size, char *mode)
1887ea022d16SRodney W. Grimes {
1888ea022d16SRodney W. Grimes char sizebuf[32];
1889ea022d16SRodney W. Grimes FILE *file;
1890e5094456SCrist J. Clark int retry = 0, tos, conerrno;
1891ea022d16SRodney W. Grimes
1892ea022d16SRodney W. Grimes file_size = size;
1893ea022d16SRodney W. Grimes byte_count = 0;
18940e519c96SYaroslav Tykhiy if (size != -1)
1895a57e1ef0SYaroslav Tykhiy (void) snprintf(sizebuf, sizeof(sizebuf),
1896a57e1ef0SYaroslav Tykhiy " (%jd bytes)", (intmax_t)size);
1897ea022d16SRodney W. Grimes else
1898e760ef2cSWarner Losh *sizebuf = '\0';
1899ea022d16SRodney W. Grimes if (pdata >= 0) {
19004dd8b5abSYoshinobu Inoue union sockunion from;
190178e3eed0SStefan Farfeleder socklen_t fromlen = ctrl_addr.su_len;
190278e3eed0SStefan Farfeleder int flags, s;
1903d6ed3c37SGuido van Rooij struct timeval timeout;
1904d6ed3c37SGuido van Rooij fd_set set;
1905ea022d16SRodney W. Grimes
1906d6ed3c37SGuido van Rooij FD_ZERO(&set);
1907d6ed3c37SGuido van Rooij FD_SET(pdata, &set);
1908d6ed3c37SGuido van Rooij
1909d6ed3c37SGuido van Rooij timeout.tv_usec = 0;
1910d6ed3c37SGuido van Rooij timeout.tv_sec = 120;
1911d6ed3c37SGuido van Rooij
19124cd48bacSYaroslav Tykhiy /*
19134cd48bacSYaroslav Tykhiy * Granted a socket is in the blocking I/O mode,
19144cd48bacSYaroslav Tykhiy * accept() will block after a successful select()
19154cd48bacSYaroslav Tykhiy * if the selected connection dies in between.
19164cd48bacSYaroslav Tykhiy * Therefore set the non-blocking I/O flag here.
19174cd48bacSYaroslav Tykhiy */
19184cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19194cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1)
19204cd48bacSYaroslav Tykhiy goto pdata_err;
1921aa5a9d3fSYaroslav Tykhiy if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 ||
19224cd48bacSYaroslav Tykhiy (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0)
19234cd48bacSYaroslav Tykhiy goto pdata_err;
1924ea022d16SRodney W. Grimes (void) close(pdata);
1925ea022d16SRodney W. Grimes pdata = s;
19264cd48bacSYaroslav Tykhiy /*
1927f6daca0dSYaroslav Tykhiy * Unset the inherited non-blocking I/O flag
1928f6daca0dSYaroslav Tykhiy * on the child socket so stdio can work on it.
19294cd48bacSYaroslav Tykhiy */
19304cd48bacSYaroslav Tykhiy if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 ||
19314cd48bacSYaroslav Tykhiy fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1)
19324cd48bacSYaroslav Tykhiy goto pdata_err;
1933ea022d16SRodney W. Grimes #ifdef IP_TOS
19344dd8b5abSYoshinobu Inoue if (from.su_family == AF_INET)
19354dd8b5abSYoshinobu Inoue {
1936a5a4544eSPaul Traina tos = IPTOS_THROUGHPUT;
193757d4ef07SYaroslav Tykhiy if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
1938406d1ae9SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m");
19394dd8b5abSYoshinobu Inoue }
1940ea022d16SRodney W. Grimes #endif
1941ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.",
1942ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1943ea022d16SRodney W. Grimes return (fdopen(pdata, mode));
19444cd48bacSYaroslav Tykhiy pdata_err:
19454cd48bacSYaroslav Tykhiy reply(425, "Can't open data connection.");
19464cd48bacSYaroslav Tykhiy (void) close(pdata);
19474cd48bacSYaroslav Tykhiy pdata = -1;
19484cd48bacSYaroslav Tykhiy return (NULL);
1949ea022d16SRodney W. Grimes }
1950ea022d16SRodney W. Grimes if (data >= 0) {
1951ea022d16SRodney W. Grimes reply(125, "Using existing data connection for '%s'%s.",
1952ea022d16SRodney W. Grimes name, sizebuf);
1953ea022d16SRodney W. Grimes usedefault = 1;
1954ea022d16SRodney W. Grimes return (fdopen(data, mode));
1955ea022d16SRodney W. Grimes }
1956ea022d16SRodney W. Grimes if (usedefault)
1957ea022d16SRodney W. Grimes data_dest = his_addr;
1958ea022d16SRodney W. Grimes usedefault = 1;
1959e5094456SCrist J. Clark do {
1960ea022d16SRodney W. Grimes file = getdatasock(mode);
1961ea022d16SRodney W. Grimes if (file == NULL) {
196204683b2cSYaroslav Tykhiy char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV];
196304683b2cSYaroslav Tykhiy
196404683b2cSYaroslav Tykhiy if (getnameinfo((struct sockaddr *)&data_source,
196504683b2cSYaroslav Tykhiy data_source.su_len,
196604683b2cSYaroslav Tykhiy hostbuf, sizeof(hostbuf) - 1,
196704683b2cSYaroslav Tykhiy portbuf, sizeof(portbuf) - 1,
196804683b2cSYaroslav Tykhiy NI_NUMERICHOST|NI_NUMERICSERV))
196904683b2cSYaroslav Tykhiy *hostbuf = *portbuf = 0;
197004683b2cSYaroslav Tykhiy hostbuf[sizeof(hostbuf) - 1] = 0;
197104683b2cSYaroslav Tykhiy portbuf[sizeof(portbuf) - 1] = 0;
19724dd8b5abSYoshinobu Inoue reply(425, "Can't create data socket (%s,%s): %s.",
19734dd8b5abSYoshinobu Inoue hostbuf, portbuf, strerror(errno));
1974ea022d16SRodney W. Grimes return (NULL);
1975ea022d16SRodney W. Grimes }
1976ea022d16SRodney W. Grimes data = fileno(file);
1977e5094456SCrist J. Clark conerrno = 0;
1978e5094456SCrist J. Clark if (connect(data, (struct sockaddr *)&data_dest,
1979e5094456SCrist J. Clark data_dest.su_len) == 0)
1980e5094456SCrist J. Clark break;
1981e5094456SCrist J. Clark conerrno = errno;
1982ea022d16SRodney W. Grimes (void) fclose(file);
1983ea022d16SRodney W. Grimes data = -1;
1984e5094456SCrist J. Clark if (conerrno == EADDRINUSE) {
1985e3765043SYaroslav Tykhiy sleep(swaitint);
1986e5094456SCrist J. Clark retry += swaitint;
1987e5094456SCrist J. Clark } else {
1988e5094456SCrist J. Clark break;
1989e5094456SCrist J. Clark }
1990e5094456SCrist J. Clark } while (retry <= swaitmax);
1991e5094456SCrist J. Clark if (conerrno != 0) {
199282c03024SYaroslav Tykhiy reply(425, "Can't build data connection: %s.",
199382c03024SYaroslav Tykhiy strerror(conerrno));
1994ea022d16SRodney W. Grimes return (NULL);
1995ea022d16SRodney W. Grimes }
1996ea022d16SRodney W. Grimes reply(150, "Opening %s mode data connection for '%s'%s.",
1997ea022d16SRodney W. Grimes type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf);
1998ea022d16SRodney W. Grimes return (file);
1999ea022d16SRodney W. Grimes }
2000ea022d16SRodney W. Grimes
2001ea022d16SRodney W. Grimes /*
20024cd51076SYaroslav Tykhiy * A helper macro to avoid code duplication
20034cd51076SYaroslav Tykhiy * in send_data() and receive_data().
20044cd51076SYaroslav Tykhiy *
20054cd51076SYaroslav Tykhiy * XXX We have to block SIGURG during putc() because BSD stdio
20064cd51076SYaroslav Tykhiy * is unable to restart interrupted write operations and hence
20074cd51076SYaroslav Tykhiy * the entire buffer contents will be lost as soon as a write()
20084cd51076SYaroslav Tykhiy * call indicates EINTR to stdio.
20094cd51076SYaroslav Tykhiy */
20104cd51076SYaroslav Tykhiy #define FTPD_PUTC(ch, file, label) \
20114cd51076SYaroslav Tykhiy do { \
20124cd51076SYaroslav Tykhiy int ret; \
20134cd51076SYaroslav Tykhiy \
20144cd51076SYaroslav Tykhiy do { \
20154cd51076SYaroslav Tykhiy START_UNSAFE; \
20164cd51076SYaroslav Tykhiy ret = putc((ch), (file)); \
20174cd51076SYaroslav Tykhiy END_UNSAFE; \
20184cd51076SYaroslav Tykhiy CHECKOOB(return (-1)) \
20194cd51076SYaroslav Tykhiy else if (ferror(file)) \
20204cd51076SYaroslav Tykhiy goto label; \
20214cd51076SYaroslav Tykhiy clearerr(file); \
20224cd51076SYaroslav Tykhiy } while (ret == EOF); \
20234cd51076SYaroslav Tykhiy } while (0)
20244cd51076SYaroslav Tykhiy
20254cd51076SYaroslav Tykhiy /*
2026ec489d64SPedro F. Giffuni * Transfer the contents of "instr" to "outstr" peer using the appropriate
20279fc5823aSGarrett Wollman * encapsulation of the data subject to Mode, Structure, and Type.
2028ea022d16SRodney W. Grimes *
2029ea022d16SRodney W. Grimes * NB: Form isn't handled.
2030ea022d16SRodney W. Grimes */
20314b82fc95SYaroslav Tykhiy static int
send_data(FILE * instr,FILE * outstr,size_t blksize,off_t filesize,int isreg)20326e4b0a55SYaroslav Tykhiy send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg)
2033ea022d16SRodney W. Grimes {
2034db1c2da3SYaroslav Tykhiy int c, cp, filefd, netfd;
2035f6f0c4b9SDan Moschuk char *buf;
2036ea022d16SRodney W. Grimes
20374cd51076SYaroslav Tykhiy STARTXFER;
20384cd51076SYaroslav Tykhiy
2039ea022d16SRodney W. Grimes switch (type) {
2040ea022d16SRodney W. Grimes
2041ea022d16SRodney W. Grimes case TYPE_A:
20424cd51076SYaroslav Tykhiy cp = EOF;
20434cd51076SYaroslav Tykhiy for (;;) {
20444cd51076SYaroslav Tykhiy c = getc(instr);
20454cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
20464cd51076SYaroslav Tykhiy else if (c == EOF && ferror(instr))
20474cd51076SYaroslav Tykhiy goto file_err;
20484cd51076SYaroslav Tykhiy if (c == EOF) {
20494cd51076SYaroslav Tykhiy if (ferror(instr)) { /* resume after OOB */
20504cd51076SYaroslav Tykhiy clearerr(instr);
20514cd51076SYaroslav Tykhiy continue;
2052ea022d16SRodney W. Grimes }
20534cd51076SYaroslav Tykhiy if (feof(instr)) /* EOF */
20544cd51076SYaroslav Tykhiy break;
20554cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: impossible condition"
20564cd51076SYaroslav Tykhiy " on file after getc()");
20574cd51076SYaroslav Tykhiy goto file_err;
20584cd51076SYaroslav Tykhiy }
20594cd51076SYaroslav Tykhiy if (c == '\n' && cp != '\r') {
20604cd51076SYaroslav Tykhiy FTPD_PUTC('\r', outstr, data_err);
20614cd51076SYaroslav Tykhiy byte_count++;
20624cd51076SYaroslav Tykhiy }
20634cd51076SYaroslav Tykhiy FTPD_PUTC(c, outstr, data_err);
20644cd51076SYaroslav Tykhiy byte_count++;
2065db1c2da3SYaroslav Tykhiy cp = c;
2066ea022d16SRodney W. Grimes }
20674cd51076SYaroslav Tykhiy #ifdef notyet /* BSD stdio isn't ready for that */
20684cd51076SYaroslav Tykhiy while (fflush(outstr) == EOF) {
20694cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
20704cd51076SYaroslav Tykhiy else
2071ea022d16SRodney W. Grimes goto data_err;
20724cd51076SYaroslav Tykhiy clearerr(outstr);
20734cd51076SYaroslav Tykhiy }
20744cd51076SYaroslav Tykhiy ENDXFER;
20754cd51076SYaroslav Tykhiy #else
20764cd51076SYaroslav Tykhiy ENDXFER;
20774cd51076SYaroslav Tykhiy if (fflush(outstr) == EOF)
20784cd51076SYaroslav Tykhiy goto data_err;
20794cd51076SYaroslav Tykhiy #endif
2080ea022d16SRodney W. Grimes reply(226, "Transfer complete.");
20814b82fc95SYaroslav Tykhiy return (0);
2082ea022d16SRodney W. Grimes
2083ea022d16SRodney W. Grimes case TYPE_I:
2084ea022d16SRodney W. Grimes case TYPE_L:
20859fc5823aSGarrett Wollman /*
20869fc5823aSGarrett Wollman * isreg is only set if we are not doing restart and we
20879fc5823aSGarrett Wollman * are sending a regular file
20889fc5823aSGarrett Wollman */
20899fc5823aSGarrett Wollman netfd = fileno(outstr);
20909fc5823aSGarrett Wollman filefd = fileno(instr);
20919fc5823aSGarrett Wollman
2092f6f0c4b9SDan Moschuk if (isreg) {
20932e22b914SYaroslav Tykhiy char *msg = "Transfer complete.";
20944cd51076SYaroslav Tykhiy off_t cnt, offset;
2095f6f0c4b9SDan Moschuk int err;
2096f6f0c4b9SDan Moschuk
20972f492fc8SYaroslav Tykhiy cnt = offset = 0;
2098f6f0c4b9SDan Moschuk
20992f492fc8SYaroslav Tykhiy while (filesize > 0) {
2100492f1d9cSMaxim Konovalov err = sendfile(filefd, netfd, offset, 0,
21017e295315SYaroslav Tykhiy NULL, &cnt, 0);
21023af48c42SMaxim Konovalov /*
21033af48c42SMaxim Konovalov * Calculate byte_count before OOB processing.
21043af48c42SMaxim Konovalov * It can be used in myoob() later.
21053af48c42SMaxim Konovalov */
21063af48c42SMaxim Konovalov byte_count += cnt;
2107f6f0c4b9SDan Moschuk offset += cnt;
2108492f1d9cSMaxim Konovalov filesize -= cnt;
21094cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
21104cd51076SYaroslav Tykhiy else if (err == -1) {
21114cd51076SYaroslav Tykhiy if (errno != EINTR &&
21124cd51076SYaroslav Tykhiy cnt == 0 && offset == 0)
2113f6f0c4b9SDan Moschuk goto oldway;
21149fc5823aSGarrett Wollman goto data_err;
2115f6f0c4b9SDan Moschuk }
21164cd51076SYaroslav Tykhiy if (err == -1) /* resume after OOB */
21174cd51076SYaroslav Tykhiy continue;
21182e22b914SYaroslav Tykhiy /*
21192e22b914SYaroslav Tykhiy * We hit the EOF prematurely.
21202e22b914SYaroslav Tykhiy * Perhaps the file was externally truncated.
21212e22b914SYaroslav Tykhiy */
21222e22b914SYaroslav Tykhiy if (cnt == 0) {
21232e22b914SYaroslav Tykhiy msg = "Transfer finished due to "
21242e22b914SYaroslav Tykhiy "premature end of file.";
21252e22b914SYaroslav Tykhiy break;
21262e22b914SYaroslav Tykhiy }
2127f6f0c4b9SDan Moschuk }
21284cd51076SYaroslav Tykhiy ENDXFER;
212962f390ecSEd Maste reply(226, "%s", msg);
21304b82fc95SYaroslav Tykhiy return (0);
21319fc5823aSGarrett Wollman }
21329fc5823aSGarrett Wollman
21339fc5823aSGarrett Wollman oldway:
2134e3765043SYaroslav Tykhiy if ((buf = malloc(blksize)) == NULL) {
21354cd51076SYaroslav Tykhiy ENDXFER;
213602c97492SYaroslav Tykhiy reply(451, "Ran out of memory.");
21374b82fc95SYaroslav Tykhiy return (-1);
2138ea022d16SRodney W. Grimes }
21399fc5823aSGarrett Wollman
21404cd51076SYaroslav Tykhiy for (;;) {
21414cd51076SYaroslav Tykhiy int cnt, len;
21424cd51076SYaroslav Tykhiy char *bp;
21434cd51076SYaroslav Tykhiy
21444cd51076SYaroslav Tykhiy cnt = read(filefd, buf, blksize);
21454cd51076SYaroslav Tykhiy CHECKOOB(free(buf); return (-1))
21464cd51076SYaroslav Tykhiy else if (cnt < 0) {
21478efc8b18SYaroslav Tykhiy free(buf);
2148ea022d16SRodney W. Grimes goto file_err;
21494cd51076SYaroslav Tykhiy }
21504cd51076SYaroslav Tykhiy if (cnt < 0) /* resume after OOB */
21514cd51076SYaroslav Tykhiy continue;
21524cd51076SYaroslav Tykhiy if (cnt == 0) /* EOF */
21534cd51076SYaroslav Tykhiy break;
21544cd51076SYaroslav Tykhiy for (len = cnt, bp = buf; len > 0;) {
21554cd51076SYaroslav Tykhiy cnt = write(netfd, bp, len);
21564cd51076SYaroslav Tykhiy CHECKOOB(free(buf); return (-1))
21574cd51076SYaroslav Tykhiy else if (cnt < 0) {
21584cd51076SYaroslav Tykhiy free(buf);
2159ea022d16SRodney W. Grimes goto data_err;
2160ea022d16SRodney W. Grimes }
21614cd51076SYaroslav Tykhiy if (cnt <= 0)
21624cd51076SYaroslav Tykhiy continue;
21634cd51076SYaroslav Tykhiy len -= cnt;
21644cd51076SYaroslav Tykhiy bp += cnt;
21654cd51076SYaroslav Tykhiy byte_count += cnt;
21664cd51076SYaroslav Tykhiy }
21674cd51076SYaroslav Tykhiy }
21684cd51076SYaroslav Tykhiy ENDXFER;
21694cd51076SYaroslav Tykhiy free(buf);
2170ea022d16SRodney W. Grimes reply(226, "Transfer complete.");
21714b82fc95SYaroslav Tykhiy return (0);
2172ea022d16SRodney W. Grimes default:
21734cd51076SYaroslav Tykhiy ENDXFER;
217402c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in send_data.", type);
21754b82fc95SYaroslav Tykhiy return (-1);
2176ea022d16SRodney W. Grimes }
2177ea022d16SRodney W. Grimes
2178ea022d16SRodney W. Grimes data_err:
21794cd51076SYaroslav Tykhiy ENDXFER;
2180ea022d16SRodney W. Grimes perror_reply(426, "Data connection");
21814b82fc95SYaroslav Tykhiy return (-1);
2182ea022d16SRodney W. Grimes
2183ea022d16SRodney W. Grimes file_err:
21844cd51076SYaroslav Tykhiy ENDXFER;
2185ea022d16SRodney W. Grimes perror_reply(551, "Error on input file");
21864b82fc95SYaroslav Tykhiy return (-1);
2187ea022d16SRodney W. Grimes }
2188ea022d16SRodney W. Grimes
2189ea022d16SRodney W. Grimes /*
2190ea022d16SRodney W. Grimes * Transfer data from peer to "outstr" using the appropriate encapulation of
2191ea022d16SRodney W. Grimes * the data subject to Mode, Structure, and Type.
2192ea022d16SRodney W. Grimes *
2193ea022d16SRodney W. Grimes * N.B.: Form isn't handled.
2194ea022d16SRodney W. Grimes */
2195ea022d16SRodney W. Grimes static int
receive_data(FILE * instr,FILE * outstr)2196e4bc453cSWarner Losh receive_data(FILE *instr, FILE *outstr)
2197ea022d16SRodney W. Grimes {
21984cd51076SYaroslav Tykhiy int c, cp;
21994cd51076SYaroslav Tykhiy int bare_lfs = 0;
2200ea022d16SRodney W. Grimes
22014cd51076SYaroslav Tykhiy STARTXFER;
220261f891a6SPaul Traina
2203ea022d16SRodney W. Grimes switch (type) {
2204ea022d16SRodney W. Grimes
2205ea022d16SRodney W. Grimes case TYPE_I:
2206ea022d16SRodney W. Grimes case TYPE_L:
22074cd51076SYaroslav Tykhiy for (;;) {
22084cd51076SYaroslav Tykhiy int cnt, len;
22094cd51076SYaroslav Tykhiy char *bp;
22104cd51076SYaroslav Tykhiy char buf[BUFSIZ];
22114cd51076SYaroslav Tykhiy
22124cd51076SYaroslav Tykhiy cnt = read(fileno(instr), buf, sizeof(buf));
22134cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
22144cd51076SYaroslav Tykhiy else if (cnt < 0)
22154cd51076SYaroslav Tykhiy goto data_err;
22164cd51076SYaroslav Tykhiy if (cnt < 0) /* resume after OOB */
22174cd51076SYaroslav Tykhiy continue;
22184cd51076SYaroslav Tykhiy if (cnt == 0) /* EOF */
22194cd51076SYaroslav Tykhiy break;
22204cd51076SYaroslav Tykhiy for (len = cnt, bp = buf; len > 0;) {
22214cd51076SYaroslav Tykhiy cnt = write(fileno(outstr), bp, len);
22224cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
22234cd51076SYaroslav Tykhiy else if (cnt < 0)
2224ea022d16SRodney W. Grimes goto file_err;
22254cd51076SYaroslav Tykhiy if (cnt <= 0)
22264cd51076SYaroslav Tykhiy continue;
22274cd51076SYaroslav Tykhiy len -= cnt;
22284cd51076SYaroslav Tykhiy bp += cnt;
2229ea022d16SRodney W. Grimes byte_count += cnt;
2230ea022d16SRodney W. Grimes }
22314cd51076SYaroslav Tykhiy }
22324cd51076SYaroslav Tykhiy ENDXFER;
2233ea022d16SRodney W. Grimes return (0);
2234ea022d16SRodney W. Grimes
2235ea022d16SRodney W. Grimes case TYPE_E:
22364cd51076SYaroslav Tykhiy ENDXFER;
2237ea022d16SRodney W. Grimes reply(553, "TYPE E not implemented.");
2238ea022d16SRodney W. Grimes return (-1);
2239ea022d16SRodney W. Grimes
2240ea022d16SRodney W. Grimes case TYPE_A:
22414cd51076SYaroslav Tykhiy cp = EOF;
22424cd51076SYaroslav Tykhiy for (;;) {
22434cd51076SYaroslav Tykhiy c = getc(instr);
22444cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
22454cd51076SYaroslav Tykhiy else if (c == EOF && ferror(instr))
22464cd51076SYaroslav Tykhiy goto data_err;
22474cd51076SYaroslav Tykhiy if (c == EOF && ferror(instr)) { /* resume after OOB */
22484cd51076SYaroslav Tykhiy clearerr(instr);
22494cd51076SYaroslav Tykhiy continue;
22504cd51076SYaroslav Tykhiy }
22514cd51076SYaroslav Tykhiy
22524cd51076SYaroslav Tykhiy if (cp == '\r') {
22534cd51076SYaroslav Tykhiy if (c != '\n')
22544cd51076SYaroslav Tykhiy FTPD_PUTC('\r', outstr, file_err);
22554cd51076SYaroslav Tykhiy } else
2256ea022d16SRodney W. Grimes if (c == '\n')
2257ea022d16SRodney W. Grimes bare_lfs++;
22584cd51076SYaroslav Tykhiy if (c == '\r') {
22594cd51076SYaroslav Tykhiy byte_count++;
22604cd51076SYaroslav Tykhiy cp = c;
22614cd51076SYaroslav Tykhiy continue;
22624cd51076SYaroslav Tykhiy }
22634cd51076SYaroslav Tykhiy
22644cd51076SYaroslav Tykhiy /* Check for EOF here in order not to lose last \r. */
22654cd51076SYaroslav Tykhiy if (c == EOF) {
22664cd51076SYaroslav Tykhiy if (feof(instr)) /* EOF */
22674cd51076SYaroslav Tykhiy break;
22684cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: impossible condition"
22694cd51076SYaroslav Tykhiy " on data stream after getc()");
2270ea022d16SRodney W. Grimes goto data_err;
2271ea022d16SRodney W. Grimes }
22724cd51076SYaroslav Tykhiy
22734cd51076SYaroslav Tykhiy byte_count++;
22744cd51076SYaroslav Tykhiy FTPD_PUTC(c, outstr, file_err);
22754cd51076SYaroslav Tykhiy cp = c;
2276ea022d16SRodney W. Grimes }
22774cd51076SYaroslav Tykhiy #ifdef notyet /* BSD stdio isn't ready for that */
22784cd51076SYaroslav Tykhiy while (fflush(outstr) == EOF) {
22794cd51076SYaroslav Tykhiy CHECKOOB(return (-1))
22804cd51076SYaroslav Tykhiy else
2281ea022d16SRodney W. Grimes goto file_err;
22824cd51076SYaroslav Tykhiy clearerr(outstr);
22834cd51076SYaroslav Tykhiy }
22844cd51076SYaroslav Tykhiy ENDXFER;
22854cd51076SYaroslav Tykhiy #else
22864cd51076SYaroslav Tykhiy ENDXFER;
22874cd51076SYaroslav Tykhiy if (fflush(outstr) == EOF)
22884cd51076SYaroslav Tykhiy goto file_err;
22894cd51076SYaroslav Tykhiy #endif
2290ea022d16SRodney W. Grimes if (bare_lfs) {
2291ea022d16SRodney W. Grimes lreply(226,
229202c97492SYaroslav Tykhiy "WARNING! %d bare linefeeds received in ASCII mode.",
2293ea022d16SRodney W. Grimes bare_lfs);
2294ea022d16SRodney W. Grimes (void)printf(" File may not have transferred correctly.\r\n");
2295ea022d16SRodney W. Grimes }
2296ea022d16SRodney W. Grimes return (0);
2297ea022d16SRodney W. Grimes default:
22984cd51076SYaroslav Tykhiy ENDXFER;
229902c97492SYaroslav Tykhiy reply(550, "Unimplemented TYPE %d in receive_data.", type);
2300ea022d16SRodney W. Grimes return (-1);
2301ea022d16SRodney W. Grimes }
2302ea022d16SRodney W. Grimes
2303ea022d16SRodney W. Grimes data_err:
23044cd51076SYaroslav Tykhiy ENDXFER;
230502c97492SYaroslav Tykhiy perror_reply(426, "Data connection");
2306ea022d16SRodney W. Grimes return (-1);
2307ea022d16SRodney W. Grimes
2308ea022d16SRodney W. Grimes file_err:
23094cd51076SYaroslav Tykhiy ENDXFER;
231002c97492SYaroslav Tykhiy perror_reply(452, "Error writing to file");
2311ea022d16SRodney W. Grimes return (-1);
2312ea022d16SRodney W. Grimes }
2313ea022d16SRodney W. Grimes
2314ea022d16SRodney W. Grimes void
statfilecmd(char * filename)2315e4bc453cSWarner Losh statfilecmd(char *filename)
2316ea022d16SRodney W. Grimes {
2317ea022d16SRodney W. Grimes FILE *fin;
2318f8a581a0SYaroslav Tykhiy int atstart;
231941c57b48SYaroslav Tykhiy int c, code;
2320ea022d16SRodney W. Grimes char line[LINE_MAX];
232141c57b48SYaroslav Tykhiy struct stat st;
2322ea022d16SRodney W. Grimes
232341c57b48SYaroslav Tykhiy code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213;
2324*e2097150SAllan Jude (void)snprintf(line, sizeof(line), _PATH_LS " -lA %s", filename);
2325ea022d16SRodney W. Grimes fin = ftpd_popen(line, "r");
2326763e8c96SEd Maste if (fin == NULL) {
2327763e8c96SEd Maste perror_reply(551, filename);
2328763e8c96SEd Maste return;
2329763e8c96SEd Maste }
23303b48b877SYaroslav Tykhiy lreply(code, "Status of %s:", filename);
2331f8a581a0SYaroslav Tykhiy atstart = 1;
2332ea022d16SRodney W. Grimes while ((c = getc(fin)) != EOF) {
2333ea022d16SRodney W. Grimes if (c == '\n') {
2334ea022d16SRodney W. Grimes if (ferror(stdout)){
233502c97492SYaroslav Tykhiy perror_reply(421, "Control connection");
2336ea022d16SRodney W. Grimes (void) ftpd_pclose(fin);
2337ea022d16SRodney W. Grimes dologout(1);
2338ea022d16SRodney W. Grimes /* NOTREACHED */
2339ea022d16SRodney W. Grimes }
2340ea022d16SRodney W. Grimes if (ferror(fin)) {
2341ea022d16SRodney W. Grimes perror_reply(551, filename);
2342ea022d16SRodney W. Grimes (void) ftpd_pclose(fin);
2343ea022d16SRodney W. Grimes return;
2344ea022d16SRodney W. Grimes }
2345ea022d16SRodney W. Grimes (void) putc('\r', stdout);
2346ea022d16SRodney W. Grimes }
2347f8a581a0SYaroslav Tykhiy /*
2348f8a581a0SYaroslav Tykhiy * RFC 959 says neutral text should be prepended before
2349f8a581a0SYaroslav Tykhiy * a leading 3-digit number followed by whitespace, but
2350f8a581a0SYaroslav Tykhiy * many ftp clients can be confused by any leading digits,
2351f8a581a0SYaroslav Tykhiy * as a matter of fact.
2352f8a581a0SYaroslav Tykhiy */
2353f8a581a0SYaroslav Tykhiy if (atstart && isdigit(c))
2354f8a581a0SYaroslav Tykhiy (void) putc(' ', stdout);
2355ea022d16SRodney W. Grimes (void) putc(c, stdout);
2356f8a581a0SYaroslav Tykhiy atstart = (c == '\n');
2357ea022d16SRodney W. Grimes }
2358ea022d16SRodney W. Grimes (void) ftpd_pclose(fin);
235902c97492SYaroslav Tykhiy reply(code, "End of status.");
2360ea022d16SRodney W. Grimes }
2361ea022d16SRodney W. Grimes
2362ea022d16SRodney W. Grimes void
statcmd(void)2363e4bc453cSWarner Losh statcmd(void)
2364ea022d16SRodney W. Grimes {
23654dd8b5abSYoshinobu Inoue union sockunion *su;
2366ea022d16SRodney W. Grimes u_char *a, *p;
2367b0f06defSHajimu UMEMOTO char hname[NI_MAXHOST];
23684dd8b5abSYoshinobu Inoue int ispassive;
2369ea022d16SRodney W. Grimes
2370c152df28SYaroslav Tykhiy if (hostinfo) {
2371a23f61bcSYaroslav Tykhiy lreply(211, "%s FTP server status:", hostname);
2372ea022d16SRodney W. Grimes printf(" %s\r\n", version);
2373c152df28SYaroslav Tykhiy } else
2374c152df28SYaroslav Tykhiy lreply(211, "FTP server status:");
2375ea022d16SRodney W. Grimes printf(" Connected to %s", remotehost);
23764dd8b5abSYoshinobu Inoue if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len,
2377b0f06defSHajimu UMEMOTO hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) {
237804683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0;
23794dd8b5abSYoshinobu Inoue if (strcmp(hname, remotehost) != 0)
23804dd8b5abSYoshinobu Inoue printf(" (%s)", hname);
23814dd8b5abSYoshinobu Inoue }
2382ea022d16SRodney W. Grimes printf("\r\n");
2383ea022d16SRodney W. Grimes if (logged_in) {
2384ea022d16SRodney W. Grimes if (guest)
2385ea022d16SRodney W. Grimes printf(" Logged in anonymously\r\n");
2386ea022d16SRodney W. Grimes else
2387ea022d16SRodney W. Grimes printf(" Logged in as %s\r\n", pw->pw_name);
2388ea022d16SRodney W. Grimes } else if (askpasswd)
2389ea022d16SRodney W. Grimes printf(" Waiting for password\r\n");
2390ea022d16SRodney W. Grimes else
2391ea022d16SRodney W. Grimes printf(" Waiting for user name\r\n");
2392ea022d16SRodney W. Grimes printf(" TYPE: %s", typenames[type]);
2393ea022d16SRodney W. Grimes if (type == TYPE_A || type == TYPE_E)
2394ea022d16SRodney W. Grimes printf(", FORM: %s", formnames[form]);
2395ea022d16SRodney W. Grimes if (type == TYPE_L)
239689fdc4e1SMike Barcroft #if CHAR_BIT == 8
239789fdc4e1SMike Barcroft printf(" %d", CHAR_BIT);
2398ea022d16SRodney W. Grimes #else
2399ea022d16SRodney W. Grimes printf(" %d", bytesize); /* need definition! */
2400ea022d16SRodney W. Grimes #endif
2401ea022d16SRodney W. Grimes printf("; STRUcture: %s; transfer MODE: %s\r\n",
2402ea022d16SRodney W. Grimes strunames[stru], modenames[mode]);
2403ea022d16SRodney W. Grimes if (data != -1)
2404ea022d16SRodney W. Grimes printf(" Data connection open\r\n");
2405ea022d16SRodney W. Grimes else if (pdata != -1) {
24064dd8b5abSYoshinobu Inoue ispassive = 1;
24074dd8b5abSYoshinobu Inoue su = &pasv_addr;
2408ea022d16SRodney W. Grimes goto printaddr;
2409ea022d16SRodney W. Grimes } else if (usedefault == 0) {
24104dd8b5abSYoshinobu Inoue ispassive = 0;
24114dd8b5abSYoshinobu Inoue su = &data_dest;
2412ea022d16SRodney W. Grimes printaddr:
2413ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
24144dd8b5abSYoshinobu Inoue if (epsvall) {
24154dd8b5abSYoshinobu Inoue printf(" EPSV only mode (EPSV ALL)\r\n");
24164dd8b5abSYoshinobu Inoue goto epsvonly;
24174dd8b5abSYoshinobu Inoue }
24184dd8b5abSYoshinobu Inoue
24194dd8b5abSYoshinobu Inoue /* PORT/PASV */
24204dd8b5abSYoshinobu Inoue if (su->su_family == AF_INET) {
24214dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr;
24224dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port;
24234dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,%d,%d,%d,%d)\r\n",
24244dd8b5abSYoshinobu Inoue ispassive ? "PASV" : "PORT",
24254dd8b5abSYoshinobu Inoue UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
24264dd8b5abSYoshinobu Inoue UC(p[0]), UC(p[1]));
24274dd8b5abSYoshinobu Inoue }
24284dd8b5abSYoshinobu Inoue
24294dd8b5abSYoshinobu Inoue /* LPRT/LPSV */
24304dd8b5abSYoshinobu Inoue {
24314dd8b5abSYoshinobu Inoue int alen, af, i;
24324dd8b5abSYoshinobu Inoue
24334dd8b5abSYoshinobu Inoue switch (su->su_family) {
24344dd8b5abSYoshinobu Inoue case AF_INET:
24354dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin.sin_addr;
24364dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin.sin_port;
24374dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin.sin_addr);
24384dd8b5abSYoshinobu Inoue af = 4;
24394dd8b5abSYoshinobu Inoue break;
24404dd8b5abSYoshinobu Inoue case AF_INET6:
24414dd8b5abSYoshinobu Inoue a = (u_char *) &su->su_sin6.sin6_addr;
24424dd8b5abSYoshinobu Inoue p = (u_char *) &su->su_sin6.sin6_port;
24434dd8b5abSYoshinobu Inoue alen = sizeof(su->su_sin6.sin6_addr);
24444dd8b5abSYoshinobu Inoue af = 6;
24454dd8b5abSYoshinobu Inoue break;
24464dd8b5abSYoshinobu Inoue default:
24474dd8b5abSYoshinobu Inoue af = 0;
24484dd8b5abSYoshinobu Inoue break;
24494dd8b5abSYoshinobu Inoue }
24504dd8b5abSYoshinobu Inoue if (af) {
24514dd8b5abSYoshinobu Inoue printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT",
24524dd8b5abSYoshinobu Inoue af, alen);
24534dd8b5abSYoshinobu Inoue for (i = 0; i < alen; i++)
24544dd8b5abSYoshinobu Inoue printf("%d,", UC(a[i]));
24554dd8b5abSYoshinobu Inoue printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1]));
24564dd8b5abSYoshinobu Inoue }
24574dd8b5abSYoshinobu Inoue }
24584dd8b5abSYoshinobu Inoue
24594dd8b5abSYoshinobu Inoue epsvonly:;
24604dd8b5abSYoshinobu Inoue /* EPRT/EPSV */
24614dd8b5abSYoshinobu Inoue {
24624dd8b5abSYoshinobu Inoue int af;
24634dd8b5abSYoshinobu Inoue
24644dd8b5abSYoshinobu Inoue switch (su->su_family) {
24654dd8b5abSYoshinobu Inoue case AF_INET:
24664dd8b5abSYoshinobu Inoue af = 1;
24674dd8b5abSYoshinobu Inoue break;
24684dd8b5abSYoshinobu Inoue case AF_INET6:
24694dd8b5abSYoshinobu Inoue af = 2;
24704dd8b5abSYoshinobu Inoue break;
24714dd8b5abSYoshinobu Inoue default:
24724dd8b5abSYoshinobu Inoue af = 0;
24734dd8b5abSYoshinobu Inoue break;
24744dd8b5abSYoshinobu Inoue }
24754dd8b5abSYoshinobu Inoue if (af) {
2476b0f06defSHajimu UMEMOTO union sockunion tmp;
2477b0f06defSHajimu UMEMOTO
2478b0f06defSHajimu UMEMOTO tmp = *su;
2479b0f06defSHajimu UMEMOTO if (tmp.su_family == AF_INET6)
2480b0f06defSHajimu UMEMOTO tmp.su_sin6.sin6_scope_id = 0;
2481b0f06defSHajimu UMEMOTO if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len,
24824dd8b5abSYoshinobu Inoue hname, sizeof(hname) - 1, NULL, 0,
24834dd8b5abSYoshinobu Inoue NI_NUMERICHOST)) {
248404683b2cSYaroslav Tykhiy hname[sizeof(hname) - 1] = 0;
24854dd8b5abSYoshinobu Inoue printf(" %s |%d|%s|%d|\r\n",
24864dd8b5abSYoshinobu Inoue ispassive ? "EPSV" : "EPRT",
2487b0f06defSHajimu UMEMOTO af, hname, htons(tmp.su_port));
24884dd8b5abSYoshinobu Inoue }
24894dd8b5abSYoshinobu Inoue }
24904dd8b5abSYoshinobu Inoue }
2491ea022d16SRodney W. Grimes #undef UC
2492ea022d16SRodney W. Grimes } else
2493ea022d16SRodney W. Grimes printf(" No data connection\r\n");
249402c97492SYaroslav Tykhiy reply(211, "End of status.");
2495ea022d16SRodney W. Grimes }
2496ea022d16SRodney W. Grimes
2497ea022d16SRodney W. Grimes void
fatalerror(char * s)2498e4bc453cSWarner Losh fatalerror(char *s)
2499ea022d16SRodney W. Grimes {
2500ea022d16SRodney W. Grimes
25014a3e5acdSYaroslav Tykhiy reply(451, "Error in server: %s", s);
2502ea022d16SRodney W. Grimes reply(221, "Closing connection due to server error.");
2503ea022d16SRodney W. Grimes dologout(0);
2504ea022d16SRodney W. Grimes /* NOTREACHED */
2505ea022d16SRodney W. Grimes }
2506ea022d16SRodney W. Grimes
2507ea022d16SRodney W. Grimes void
reply(int n,const char * fmt,...)2508ea022d16SRodney W. Grimes reply(int n, const char *fmt, ...)
2509ea022d16SRodney W. Grimes {
2510ea022d16SRodney W. Grimes va_list ap;
2511e4bc453cSWarner Losh
2512ea022d16SRodney W. Grimes (void)printf("%d ", n);
25139cbb335cSTim J. Robbins va_start(ap, fmt);
2514ea022d16SRodney W. Grimes (void)vprintf(fmt, ap);
25159cbb335cSTim J. Robbins va_end(ap);
2516ea022d16SRodney W. Grimes (void)printf("\r\n");
2517ea022d16SRodney W. Grimes (void)fflush(stdout);
2518618b0bbaSMark Murray if (ftpdebug) {
2519ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d ", n);
25209cbb335cSTim J. Robbins va_start(ap, fmt);
2521ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap);
25229cbb335cSTim J. Robbins va_end(ap);
2523ea022d16SRodney W. Grimes }
2524ea022d16SRodney W. Grimes }
2525ea022d16SRodney W. Grimes
2526ea022d16SRodney W. Grimes void
lreply(int n,const char * fmt,...)2527ea022d16SRodney W. Grimes lreply(int n, const char *fmt, ...)
2528ea022d16SRodney W. Grimes {
2529ea022d16SRodney W. Grimes va_list ap;
2530e4bc453cSWarner Losh
2531ea022d16SRodney W. Grimes (void)printf("%d- ", n);
25329cbb335cSTim J. Robbins va_start(ap, fmt);
2533ea022d16SRodney W. Grimes (void)vprintf(fmt, ap);
25349cbb335cSTim J. Robbins va_end(ap);
2535ea022d16SRodney W. Grimes (void)printf("\r\n");
2536ea022d16SRodney W. Grimes (void)fflush(stdout);
2537618b0bbaSMark Murray if (ftpdebug) {
2538ea022d16SRodney W. Grimes syslog(LOG_DEBUG, "<--- %d- ", n);
25399cbb335cSTim J. Robbins va_start(ap, fmt);
2540ea022d16SRodney W. Grimes vsyslog(LOG_DEBUG, fmt, ap);
25419cbb335cSTim J. Robbins va_end(ap);
2542ea022d16SRodney W. Grimes }
2543ea022d16SRodney W. Grimes }
2544ea022d16SRodney W. Grimes
2545ea022d16SRodney W. Grimes static void
ack(char * s)2546e4bc453cSWarner Losh ack(char *s)
2547ea022d16SRodney W. Grimes {
2548ea022d16SRodney W. Grimes
2549ea022d16SRodney W. Grimes reply(250, "%s command successful.", s);
2550ea022d16SRodney W. Grimes }
2551ea022d16SRodney W. Grimes
2552ea022d16SRodney W. Grimes void
nack(char * s)2553e4bc453cSWarner Losh nack(char *s)
2554ea022d16SRodney W. Grimes {
2555ea022d16SRodney W. Grimes
2556ea022d16SRodney W. Grimes reply(502, "%s command not implemented.", s);
2557ea022d16SRodney W. Grimes }
2558ea022d16SRodney W. Grimes
2559ea022d16SRodney W. Grimes /* ARGSUSED */
2560ea022d16SRodney W. Grimes void
yyerror(char * s)2561e4bc453cSWarner Losh yyerror(char *s)
2562ea022d16SRodney W. Grimes {
2563ea022d16SRodney W. Grimes char *cp;
2564ea022d16SRodney W. Grimes
25659aca17cbSMark Murray if ((cp = strchr(cbuf,'\n')))
2566ea022d16SRodney W. Grimes *cp = '\0';
256702c97492SYaroslav Tykhiy reply(500, "%s: command not understood.", cbuf);
2568ea022d16SRodney W. Grimes }
2569ea022d16SRodney W. Grimes
2570ea022d16SRodney W. Grimes void
delete(char * name)2571e4bc453cSWarner Losh delete(char *name)
2572ea022d16SRodney W. Grimes {
2573ea022d16SRodney W. Grimes struct stat st;
2574ea022d16SRodney W. Grimes
2575ea022d16SRodney W. Grimes LOGCMD("delete", name);
25761b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) {
2577ea022d16SRodney W. Grimes perror_reply(550, name);
2578ea022d16SRodney W. Grimes return;
2579ea022d16SRodney W. Grimes }
2580ac4f2391SYaroslav Tykhiy if (S_ISDIR(st.st_mode)) {
2581ea022d16SRodney W. Grimes if (rmdir(name) < 0) {
2582ea022d16SRodney W. Grimes perror_reply(550, name);
2583ea022d16SRodney W. Grimes return;
2584ea022d16SRodney W. Grimes }
2585ea022d16SRodney W. Grimes goto done;
2586ea022d16SRodney W. Grimes }
25873f8b9cfeSYaroslav Tykhiy if (guest && noguestmod) {
258802c97492SYaroslav Tykhiy reply(550, "Operation not permitted.");
25893f8b9cfeSYaroslav Tykhiy return;
25903f8b9cfeSYaroslav Tykhiy }
25913f8b9cfeSYaroslav Tykhiy if (unlink(name) < 0) {
2592ea022d16SRodney W. Grimes perror_reply(550, name);
2593ea022d16SRodney W. Grimes return;
2594ea022d16SRodney W. Grimes }
2595ea022d16SRodney W. Grimes done:
2596ea022d16SRodney W. Grimes ack("DELE");
2597ea022d16SRodney W. Grimes }
2598ea022d16SRodney W. Grimes
2599ea022d16SRodney W. Grimes void
cwd(char * path)2600e4bc453cSWarner Losh cwd(char *path)
2601ea022d16SRodney W. Grimes {
2602ea022d16SRodney W. Grimes
2603ea022d16SRodney W. Grimes if (chdir(path) < 0)
2604ea022d16SRodney W. Grimes perror_reply(550, path);
2605ea022d16SRodney W. Grimes else
2606ea022d16SRodney W. Grimes ack("CWD");
2607ea022d16SRodney W. Grimes }
2608ea022d16SRodney W. Grimes
2609ea022d16SRodney W. Grimes void
makedir(char * name)2610e4bc453cSWarner Losh makedir(char *name)
2611ea022d16SRodney W. Grimes {
26122b748987SYaroslav Tykhiy char *s;
2613ea022d16SRodney W. Grimes
2614ea022d16SRodney W. Grimes LOGCMD("mkdir", name);
2615d186bb12SMatthew N. Dodd if (guest && noguestmkd)
2616405e2987SYaroslav Tykhiy reply(550, "Operation not permitted.");
2617d186bb12SMatthew N. Dodd else if (mkdir(name, 0777) < 0)
2618ea022d16SRodney W. Grimes perror_reply(550, name);
26192b748987SYaroslav Tykhiy else {
26202b748987SYaroslav Tykhiy if ((s = doublequote(name)) == NULL)
26212b748987SYaroslav Tykhiy fatalerror("Ran out of memory.");
26222b748987SYaroslav Tykhiy reply(257, "\"%s\" directory created.", s);
26232b748987SYaroslav Tykhiy free(s);
26242b748987SYaroslav Tykhiy }
2625ea022d16SRodney W. Grimes }
2626ea022d16SRodney W. Grimes
2627ea022d16SRodney W. Grimes void
removedir(char * name)2628e4bc453cSWarner Losh removedir(char *name)
2629ea022d16SRodney W. Grimes {
2630ea022d16SRodney W. Grimes
2631ea022d16SRodney W. Grimes LOGCMD("rmdir", name);
2632ea022d16SRodney W. Grimes if (rmdir(name) < 0)
2633ea022d16SRodney W. Grimes perror_reply(550, name);
2634ea022d16SRodney W. Grimes else
2635ea022d16SRodney W. Grimes ack("RMD");
2636ea022d16SRodney W. Grimes }
2637ea022d16SRodney W. Grimes
2638ea022d16SRodney W. Grimes void
pwd(void)2639e4bc453cSWarner Losh pwd(void)
2640ea022d16SRodney W. Grimes {
2641e4648f05SYaroslav Tykhiy char *s, path[MAXPATHLEN + 1];
2642ea022d16SRodney W. Grimes
2643de9b6c03SYaroslav Tykhiy if (getcwd(path, sizeof(path)) == NULL)
264475933089SYaroslav Tykhiy perror_reply(550, "Get current directory");
2645e4648f05SYaroslav Tykhiy else {
2646e4648f05SYaroslav Tykhiy if ((s = doublequote(path)) == NULL)
2647e4648f05SYaroslav Tykhiy fatalerror("Ran out of memory.");
2648e4648f05SYaroslav Tykhiy reply(257, "\"%s\" is current directory.", s);
2649e4648f05SYaroslav Tykhiy free(s);
2650e4648f05SYaroslav Tykhiy }
2651ea022d16SRodney W. Grimes }
2652ea022d16SRodney W. Grimes
2653ea022d16SRodney W. Grimes char *
renamefrom(char * name)2654e4bc453cSWarner Losh renamefrom(char *name)
2655ea022d16SRodney W. Grimes {
2656ea022d16SRodney W. Grimes struct stat st;
2657ea022d16SRodney W. Grimes
2658b943b3c4SYaroslav Tykhiy if (guest && noguestmod) {
265902c97492SYaroslav Tykhiy reply(550, "Operation not permitted.");
2660b943b3c4SYaroslav Tykhiy return (NULL);
2661b943b3c4SYaroslav Tykhiy }
26621b0e12d7SYaroslav Tykhiy if (lstat(name, &st) < 0) {
2663ea022d16SRodney W. Grimes perror_reply(550, name);
2664385f9bf0SYaroslav Tykhiy return (NULL);
2665ea022d16SRodney W. Grimes }
266602c97492SYaroslav Tykhiy reply(350, "File exists, ready for destination name.");
2667ea022d16SRodney W. Grimes return (name);
2668ea022d16SRodney W. Grimes }
2669ea022d16SRodney W. Grimes
2670ea022d16SRodney W. Grimes void
renamecmd(char * from,char * to)2671e4bc453cSWarner Losh renamecmd(char *from, char *to)
2672ea022d16SRodney W. Grimes {
267361f891a6SPaul Traina struct stat st;
2674ea022d16SRodney W. Grimes
2675ea022d16SRodney W. Grimes LOGCMD2("rename", from, to);
267661f891a6SPaul Traina
267761f891a6SPaul Traina if (guest && (stat(to, &st) == 0)) {
267802c97492SYaroslav Tykhiy reply(550, "%s: permission denied.", to);
267961f891a6SPaul Traina return;
268061f891a6SPaul Traina }
268161f891a6SPaul Traina
2682ea022d16SRodney W. Grimes if (rename(from, to) < 0)
2683ea022d16SRodney W. Grimes perror_reply(550, "rename");
2684ea022d16SRodney W. Grimes else
2685ea022d16SRodney W. Grimes ack("RNTO");
2686ea022d16SRodney W. Grimes }
2687ea022d16SRodney W. Grimes
2688ea022d16SRodney W. Grimes static void
dolog(struct sockaddr * who)2689e4bc453cSWarner Losh dolog(struct sockaddr *who)
2690ea022d16SRodney W. Grimes {
26917cdd3cb7SYaroslav Tykhiy char who_name[NI_MAXHOST];
26924dd8b5abSYoshinobu Inoue
26934dd8b5abSYoshinobu Inoue realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len);
269404683b2cSYaroslav Tykhiy remotehost[sizeof(remotehost) - 1] = 0;
26957cdd3cb7SYaroslav Tykhiy if (getnameinfo(who, who->sa_len,
26967cdd3cb7SYaroslav Tykhiy who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST))
26977cdd3cb7SYaroslav Tykhiy *who_name = 0;
26987cdd3cb7SYaroslav Tykhiy who_name[sizeof(who_name) - 1] = 0;
2699ea022d16SRodney W. Grimes
2700ea022d16SRodney W. Grimes #ifdef SETPROCTITLE
2701ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2702ea4e54b9SDavid Nugent if (thishost != firsthost)
2703ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)",
2704ea4e54b9SDavid Nugent remotehost, hostname);
2705ea4e54b9SDavid Nugent else
2706ea4e54b9SDavid Nugent #endif
2707ea4e54b9SDavid Nugent snprintf(proctitle, sizeof(proctitle), "%s: connected",
2708ea4e54b9SDavid Nugent remotehost);
2709b63e1fe2SPeter Wemm setproctitle("%s", proctitle);
2710ea022d16SRodney W. Grimes #endif /* SETPROCTITLE */
2711ea022d16SRodney W. Grimes
2712ea4e54b9SDavid Nugent if (logging) {
2713ea4e54b9SDavid Nugent #ifdef VIRTUAL_HOSTING
2714ea4e54b9SDavid Nugent if (thishost != firsthost)
27157cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s) to %s",
27167cdd3cb7SYaroslav Tykhiy remotehost, who_name, hostname);
2717ea4e54b9SDavid Nugent else
2718ea4e54b9SDavid Nugent #endif
27197cdd3cb7SYaroslav Tykhiy syslog(LOG_INFO, "connection from %s (%s)",
27207cdd3cb7SYaroslav Tykhiy remotehost, who_name);
2721ea022d16SRodney W. Grimes }
2722ea4e54b9SDavid Nugent }
2723ea022d16SRodney W. Grimes
2724ea022d16SRodney W. Grimes /*
2725ea022d16SRodney W. Grimes * Record logout in wtmp file
2726ea022d16SRodney W. Grimes * and exit with supplied status.
2727ea022d16SRodney W. Grimes */
2728ea022d16SRodney W. Grimes void
dologout(int status)2729e4bc453cSWarner Losh dologout(int status)
2730ea022d16SRodney W. Grimes {
2731ea022d16SRodney W. Grimes
27329f37b1a2SEd Schouten if (logged_in && dowtmp) {
273352e7ee74SYaroslav Tykhiy (void) seteuid(0);
2734de8d85c9SEugene Grosbein #ifdef LOGIN_CAP
2735de8d85c9SEugene Grosbein setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN |
2736de8d85c9SEugene Grosbein LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH |
2737de8d85c9SEugene Grosbein LOGIN_SETENV));
2738de8d85c9SEugene Grosbein #endif
27399f37b1a2SEd Schouten ftpd_logwtmp(wtmpid, NULL, NULL);
2740ea022d16SRodney W. Grimes }
2741ea022d16SRodney W. Grimes /* beware of flushing buffers after a SIGPIPE */
2742ea022d16SRodney W. Grimes _exit(status);
2743ea022d16SRodney W. Grimes }
2744ea022d16SRodney W. Grimes
2745ea022d16SRodney W. Grimes static void
sigurg(int signo)2746e4bc453cSWarner Losh sigurg(int signo)
2747ea022d16SRodney W. Grimes {
27484b82fc95SYaroslav Tykhiy
27494b82fc95SYaroslav Tykhiy recvurg = 1;
27504b82fc95SYaroslav Tykhiy }
27514b82fc95SYaroslav Tykhiy
27524b82fc95SYaroslav Tykhiy static void
maskurg(int flag)27534cd51076SYaroslav Tykhiy maskurg(int flag)
27544cd51076SYaroslav Tykhiy {
27554cd51076SYaroslav Tykhiy int oerrno;
27564cd51076SYaroslav Tykhiy sigset_t sset;
27574cd51076SYaroslav Tykhiy
27584cd51076SYaroslav Tykhiy if (!transflag) {
27594cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: maskurg() while no transfer");
27604cd51076SYaroslav Tykhiy return;
27614cd51076SYaroslav Tykhiy }
27624cd51076SYaroslav Tykhiy oerrno = errno;
27634cd51076SYaroslav Tykhiy sigemptyset(&sset);
27644cd51076SYaroslav Tykhiy sigaddset(&sset, SIGURG);
27654cd51076SYaroslav Tykhiy sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL);
27664cd51076SYaroslav Tykhiy errno = oerrno;
27674cd51076SYaroslav Tykhiy }
27684cd51076SYaroslav Tykhiy
27694cd51076SYaroslav Tykhiy static void
flagxfer(int flag)27704cd51076SYaroslav Tykhiy flagxfer(int flag)
27714cd51076SYaroslav Tykhiy {
27724cd51076SYaroslav Tykhiy
27734cd51076SYaroslav Tykhiy if (flag) {
2774f9036ce6SYaroslav Tykhiy if (transflag)
2775f9036ce6SYaroslav Tykhiy syslog(LOG_ERR, "Internal: flagxfer(1): "
2776f9036ce6SYaroslav Tykhiy "transfer already under way");
27774cd51076SYaroslav Tykhiy transflag = 1;
277891ae7779SYaroslav Tykhiy maskurg(0);
277991ae7779SYaroslav Tykhiy recvurg = 0;
278091ae7779SYaroslav Tykhiy } else {
2781f9036ce6SYaroslav Tykhiy if (!transflag)
2782f9036ce6SYaroslav Tykhiy syslog(LOG_ERR, "Internal: flagxfer(0): "
2783f9036ce6SYaroslav Tykhiy "no active transfer");
278491ae7779SYaroslav Tykhiy maskurg(1);
27854cd51076SYaroslav Tykhiy transflag = 0;
27864cd51076SYaroslav Tykhiy }
278791ae7779SYaroslav Tykhiy }
27884cd51076SYaroslav Tykhiy
27894cd51076SYaroslav Tykhiy /*
27904cd51076SYaroslav Tykhiy * Returns 0 if OK to resume or -1 if abort requested.
27914cd51076SYaroslav Tykhiy */
27924cd51076SYaroslav Tykhiy static int
myoob(void)2793e4bc453cSWarner Losh myoob(void)
27944b82fc95SYaroslav Tykhiy {
2795ea022d16SRodney W. Grimes char *cp;
2796f0b40b1cSColin Percival int ret;
2797ea022d16SRodney W. Grimes
27984cd51076SYaroslav Tykhiy if (!transflag) {
27994cd51076SYaroslav Tykhiy syslog(LOG_ERR, "Internal: myoob() while no transfer");
28004cd51076SYaroslav Tykhiy return (0);
28014cd51076SYaroslav Tykhiy }
2802ea022d16SRodney W. Grimes cp = tmpline;
2803f03ef840SBaptiste Daroussin ret = get_line(cp, 7, stdin);
2804f0b40b1cSColin Percival if (ret == -1) {
2805ea022d16SRodney W. Grimes reply(221, "You could at least say goodbye.");
2806ea022d16SRodney W. Grimes dologout(0);
2807f0b40b1cSColin Percival } else if (ret == -2) {
2808f0b40b1cSColin Percival /* Ignore truncated command. */
2809f0b40b1cSColin Percival return (0);
2810ea022d16SRodney W. Grimes }
2811ea022d16SRodney W. Grimes upper(cp);
2812ea022d16SRodney W. Grimes if (strcmp(cp, "ABOR\r\n") == 0) {
2813ea022d16SRodney W. Grimes tmpline[0] = '\0';
2814ea022d16SRodney W. Grimes reply(426, "Transfer aborted. Data connection closed.");
281502c97492SYaroslav Tykhiy reply(226, "Abort successful.");
28164cd51076SYaroslav Tykhiy return (-1);
2817ea022d16SRodney W. Grimes }
2818ea022d16SRodney W. Grimes if (strcmp(cp, "STAT\r\n") == 0) {
28199db4bbf3SMichael Haro tmpline[0] = '\0';
28200e519c96SYaroslav Tykhiy if (file_size != -1)
282102c97492SYaroslav Tykhiy reply(213, "Status: %jd of %jd bytes transferred.",
2822a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count, (intmax_t)file_size);
2823ea022d16SRodney W. Grimes else
282402c97492SYaroslav Tykhiy reply(213, "Status: %jd bytes transferred.",
2825a57e1ef0SYaroslav Tykhiy (intmax_t)byte_count);
2826ea022d16SRodney W. Grimes }
28274cd51076SYaroslav Tykhiy return (0);
2828ea022d16SRodney W. Grimes }
2829ea022d16SRodney W. Grimes
2830ea022d16SRodney W. Grimes /*
2831ea022d16SRodney W. Grimes * Note: a response of 425 is not mentioned as a possible response to
2832ea022d16SRodney W. Grimes * the PASV command in RFC959. However, it has been blessed as
2833ea022d16SRodney W. Grimes * a legitimate response by Jon Postel in a telephone conversation
2834ea022d16SRodney W. Grimes * with Rick Adams on 25 Jan 89.
2835ea022d16SRodney W. Grimes */
2836ea022d16SRodney W. Grimes void
passive(void)2837e4bc453cSWarner Losh passive(void)
2838ea022d16SRodney W. Grimes {
283978e3eed0SStefan Farfeleder socklen_t len;
284078e3eed0SStefan Farfeleder int on;
2841ea022d16SRodney W. Grimes char *p, *a;
2842ea022d16SRodney W. Grimes
284361f891a6SPaul Traina if (pdata >= 0) /* close old port if one set */
284461f891a6SPaul Traina close(pdata);
284561f891a6SPaul Traina
28464dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
2847ea022d16SRodney W. Grimes if (pdata < 0) {
2848ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection");
2849ea022d16SRodney W. Grimes return;
2850ea022d16SRodney W. Grimes }
28518af7c9a3SYaroslav Tykhiy on = 1;
28528af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
28538af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
28544c450ad7SPaul Traina
285552e7ee74SYaroslav Tykhiy (void) seteuid(0);
285661f891a6SPaul Traina
2857dacc9752SPaul Traina #ifdef IP_PORTRANGE
28584dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family == AF_INET) {
28598af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH
2860dacc9752SPaul Traina : IP_PORTRANGE_DEFAULT;
2861dacc9752SPaul Traina
286240e9d39eSPeter Wemm if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
286357d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0)
28644c450ad7SPaul Traina goto pasv_error;
28654c450ad7SPaul Traina }
2866dacc9752SPaul Traina #endif
28672db39860SNick Sayer #ifdef IPV6_PORTRANGE
28682db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) {
28698af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
28702db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT;
28712db39860SNick Sayer
28722db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
287357d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0)
28742db39860SNick Sayer goto pasv_error;
28752db39860SNick Sayer }
28762db39860SNick Sayer #endif
287740e9d39eSPeter Wemm
2878ea022d16SRodney W. Grimes pasv_addr = ctrl_addr;
28794dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0;
28804dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0)
2881ea022d16SRodney W. Grimes goto pasv_error;
288261f891a6SPaul Traina
288352e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid);
28844c450ad7SPaul Traina
2885ea022d16SRodney W. Grimes len = sizeof(pasv_addr);
2886ea022d16SRodney W. Grimes if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
2887ea022d16SRodney W. Grimes goto pasv_error;
2888ea022d16SRodney W. Grimes if (listen(pdata, 1) < 0)
2889ea022d16SRodney W. Grimes goto pasv_error;
28904dd8b5abSYoshinobu Inoue if (pasv_addr.su_family == AF_INET)
28914dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr;
28924dd8b5abSYoshinobu Inoue else if (pasv_addr.su_family == AF_INET6 &&
28934dd8b5abSYoshinobu Inoue IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr))
28944dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
28954dd8b5abSYoshinobu Inoue else
28964dd8b5abSYoshinobu Inoue goto pasv_error;
28974dd8b5abSYoshinobu Inoue
28984dd8b5abSYoshinobu Inoue p = (char *) &pasv_addr.su_port;
2899ea022d16SRodney W. Grimes
2900ea022d16SRodney W. Grimes #define UC(b) (((int) b) & 0xff)
2901ea022d16SRodney W. Grimes
2902ea022d16SRodney W. Grimes reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]),
2903ea022d16SRodney W. Grimes UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
2904ea022d16SRodney W. Grimes return;
2905ea022d16SRodney W. Grimes
2906ea022d16SRodney W. Grimes pasv_error:
290752e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid);
2908ea022d16SRodney W. Grimes (void) close(pdata);
2909ea022d16SRodney W. Grimes pdata = -1;
2910ea022d16SRodney W. Grimes perror_reply(425, "Can't open passive connection");
2911ea022d16SRodney W. Grimes return;
2912ea022d16SRodney W. Grimes }
2913ea022d16SRodney W. Grimes
2914ea022d16SRodney W. Grimes /*
29154dd8b5abSYoshinobu Inoue * Long Passive defined in RFC 1639.
29164dd8b5abSYoshinobu Inoue * 228 Entering Long Passive Mode
29174dd8b5abSYoshinobu Inoue * (af, hal, h1, h2, h3,..., pal, p1, p2...)
29184dd8b5abSYoshinobu Inoue */
29194dd8b5abSYoshinobu Inoue
29204dd8b5abSYoshinobu Inoue void
long_passive(char * cmd,int pf)2921e4bc453cSWarner Losh long_passive(char *cmd, int pf)
29224dd8b5abSYoshinobu Inoue {
292378e3eed0SStefan Farfeleder socklen_t len;
292478e3eed0SStefan Farfeleder int on;
29254dd8b5abSYoshinobu Inoue char *p, *a;
29264dd8b5abSYoshinobu Inoue
29274dd8b5abSYoshinobu Inoue if (pdata >= 0) /* close old port if one set */
29284dd8b5abSYoshinobu Inoue close(pdata);
29294dd8b5abSYoshinobu Inoue
29304dd8b5abSYoshinobu Inoue if (pf != PF_UNSPEC) {
29314dd8b5abSYoshinobu Inoue if (ctrl_addr.su_family != pf) {
29324dd8b5abSYoshinobu Inoue switch (ctrl_addr.su_family) {
29334dd8b5abSYoshinobu Inoue case AF_INET:
29344dd8b5abSYoshinobu Inoue pf = 1;
29354dd8b5abSYoshinobu Inoue break;
29364dd8b5abSYoshinobu Inoue case AF_INET6:
29374dd8b5abSYoshinobu Inoue pf = 2;
29384dd8b5abSYoshinobu Inoue break;
29394dd8b5abSYoshinobu Inoue default:
29404dd8b5abSYoshinobu Inoue pf = 0;
29414dd8b5abSYoshinobu Inoue break;
29424dd8b5abSYoshinobu Inoue }
29434dd8b5abSYoshinobu Inoue /*
29444dd8b5abSYoshinobu Inoue * XXX
29454dd8b5abSYoshinobu Inoue * only EPRT/EPSV ready clients will understand this
29464dd8b5abSYoshinobu Inoue */
29474dd8b5abSYoshinobu Inoue if (strcmp(cmd, "EPSV") == 0 && pf) {
29484dd8b5abSYoshinobu Inoue reply(522, "Network protocol mismatch, "
29494dd8b5abSYoshinobu Inoue "use (%d)", pf);
29504dd8b5abSYoshinobu Inoue } else
295102c97492SYaroslav Tykhiy reply(501, "Network protocol mismatch."); /*XXX*/
29524dd8b5abSYoshinobu Inoue
29534dd8b5abSYoshinobu Inoue return;
29544dd8b5abSYoshinobu Inoue }
29554dd8b5abSYoshinobu Inoue }
29564dd8b5abSYoshinobu Inoue
29574dd8b5abSYoshinobu Inoue pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0);
29584dd8b5abSYoshinobu Inoue if (pdata < 0) {
29594dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection");
29604dd8b5abSYoshinobu Inoue return;
29614dd8b5abSYoshinobu Inoue }
29628af7c9a3SYaroslav Tykhiy on = 1;
29638af7c9a3SYaroslav Tykhiy if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
29648af7c9a3SYaroslav Tykhiy syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m");
29654dd8b5abSYoshinobu Inoue
296652e7ee74SYaroslav Tykhiy (void) seteuid(0);
29674dd8b5abSYoshinobu Inoue
29684dd8b5abSYoshinobu Inoue pasv_addr = ctrl_addr;
29694dd8b5abSYoshinobu Inoue pasv_addr.su_port = 0;
29704dd8b5abSYoshinobu Inoue len = pasv_addr.su_len;
29714dd8b5abSYoshinobu Inoue
29722db39860SNick Sayer #ifdef IP_PORTRANGE
29732db39860SNick Sayer if (ctrl_addr.su_family == AF_INET) {
29748af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IP_PORTRANGE_HIGH
29752db39860SNick Sayer : IP_PORTRANGE_DEFAULT;
29762db39860SNick Sayer
29772db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE,
297857d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0)
29792db39860SNick Sayer goto pasv_error;
29802db39860SNick Sayer }
29812db39860SNick Sayer #endif
29822db39860SNick Sayer #ifdef IPV6_PORTRANGE
29832db39860SNick Sayer if (ctrl_addr.su_family == AF_INET6) {
29848af7c9a3SYaroslav Tykhiy on = restricted_data_ports ? IPV6_PORTRANGE_HIGH
29852db39860SNick Sayer : IPV6_PORTRANGE_DEFAULT;
29862db39860SNick Sayer
29872db39860SNick Sayer if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE,
298857d4ef07SYaroslav Tykhiy &on, sizeof(on)) < 0)
29892db39860SNick Sayer goto pasv_error;
29902db39860SNick Sayer }
29912db39860SNick Sayer #endif
29922db39860SNick Sayer
29934dd8b5abSYoshinobu Inoue if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0)
29944dd8b5abSYoshinobu Inoue goto pasv_error;
29954dd8b5abSYoshinobu Inoue
299652e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid);
29974dd8b5abSYoshinobu Inoue
29984dd8b5abSYoshinobu Inoue if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0)
29994dd8b5abSYoshinobu Inoue goto pasv_error;
30004dd8b5abSYoshinobu Inoue if (listen(pdata, 1) < 0)
30014dd8b5abSYoshinobu Inoue goto pasv_error;
30024dd8b5abSYoshinobu Inoue
30034dd8b5abSYoshinobu Inoue #define UC(b) (((int) b) & 0xff)
30044dd8b5abSYoshinobu Inoue
30054dd8b5abSYoshinobu Inoue if (strcmp(cmd, "LPSV") == 0) {
30064dd8b5abSYoshinobu Inoue p = (char *)&pasv_addr.su_port;
30074dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) {
30084dd8b5abSYoshinobu Inoue case AF_INET:
30094dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin.sin_addr;
30104dd8b5abSYoshinobu Inoue v4_reply:
30114dd8b5abSYoshinobu Inoue reply(228,
30124dd8b5abSYoshinobu Inoue "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30134dd8b5abSYoshinobu Inoue 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30144dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1]));
30154dd8b5abSYoshinobu Inoue return;
30164dd8b5abSYoshinobu Inoue case AF_INET6:
30174dd8b5abSYoshinobu Inoue if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) {
30184dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12];
30194dd8b5abSYoshinobu Inoue goto v4_reply;
30204dd8b5abSYoshinobu Inoue }
30214dd8b5abSYoshinobu Inoue a = (char *) &pasv_addr.su_sin6.sin6_addr;
30224dd8b5abSYoshinobu Inoue reply(228,
30234dd8b5abSYoshinobu Inoue "Entering Long Passive Mode "
30244dd8b5abSYoshinobu Inoue "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
30254dd8b5abSYoshinobu Inoue 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
30264dd8b5abSYoshinobu Inoue UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]),
30274dd8b5abSYoshinobu Inoue UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]),
30284dd8b5abSYoshinobu Inoue UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]),
30294dd8b5abSYoshinobu Inoue 2, UC(p[0]), UC(p[1]));
30304dd8b5abSYoshinobu Inoue return;
30314dd8b5abSYoshinobu Inoue }
30324dd8b5abSYoshinobu Inoue } else if (strcmp(cmd, "EPSV") == 0) {
30334dd8b5abSYoshinobu Inoue switch (pasv_addr.su_family) {
30344dd8b5abSYoshinobu Inoue case AF_INET:
30354dd8b5abSYoshinobu Inoue case AF_INET6:
30364dd8b5abSYoshinobu Inoue reply(229, "Entering Extended Passive Mode (|||%d|)",
30374dd8b5abSYoshinobu Inoue ntohs(pasv_addr.su_port));
30384dd8b5abSYoshinobu Inoue return;
30394dd8b5abSYoshinobu Inoue }
30404dd8b5abSYoshinobu Inoue } else {
30414dd8b5abSYoshinobu Inoue /* more proper error code? */
30424dd8b5abSYoshinobu Inoue }
30434dd8b5abSYoshinobu Inoue
30444dd8b5abSYoshinobu Inoue pasv_error:
304552e7ee74SYaroslav Tykhiy (void) seteuid(pw->pw_uid);
30464dd8b5abSYoshinobu Inoue (void) close(pdata);
30474dd8b5abSYoshinobu Inoue pdata = -1;
30484dd8b5abSYoshinobu Inoue perror_reply(425, "Can't open passive connection");
30494dd8b5abSYoshinobu Inoue return;
30504dd8b5abSYoshinobu Inoue }
30514dd8b5abSYoshinobu Inoue
30524dd8b5abSYoshinobu Inoue /*
3053a117c345SYaroslav Tykhiy * Generate unique name for file with basename "local"
3054a117c345SYaroslav Tykhiy * and open the file in order to avoid possible races.
3055a117c345SYaroslav Tykhiy * Try "local" first, then "local.1", "local.2" etc, up to "local.99".
3056a117c345SYaroslav Tykhiy * Return descriptor to the file, set "name" to its name.
3057a117c345SYaroslav Tykhiy *
3058ea022d16SRodney W. Grimes * Generates failure reply on error.
3059ea022d16SRodney W. Grimes */
3060a117c345SYaroslav Tykhiy static int
guniquefd(char * local,char ** name)3061a117c345SYaroslav Tykhiy guniquefd(char *local, char **name)
3062ea022d16SRodney W. Grimes {
3063ea022d16SRodney W. Grimes static char new[MAXPATHLEN];
3064ea022d16SRodney W. Grimes struct stat st;
3065ea022d16SRodney W. Grimes char *cp;
3066a117c345SYaroslav Tykhiy int count;
3067a117c345SYaroslav Tykhiy int fd;
3068ea022d16SRodney W. Grimes
3069ea022d16SRodney W. Grimes cp = strrchr(local, '/');
3070ea022d16SRodney W. Grimes if (cp)
3071ea022d16SRodney W. Grimes *cp = '\0';
3072ea022d16SRodney W. Grimes if (stat(cp ? local : ".", &st) < 0) {
3073ea022d16SRodney W. Grimes perror_reply(553, cp ? local : ".");
3074a117c345SYaroslav Tykhiy return (-1);
3075ea022d16SRodney W. Grimes }
3076a117c345SYaroslav Tykhiy if (cp) {
3077a117c345SYaroslav Tykhiy /*
3078a117c345SYaroslav Tykhiy * Let not overwrite dirname with counter suffix.
3079a117c345SYaroslav Tykhiy * -4 is for /nn\0
3080a117c345SYaroslav Tykhiy * In this extreme case dot won't be put in front of suffix.
3081a117c345SYaroslav Tykhiy */
3082a117c345SYaroslav Tykhiy if (strlen(local) > sizeof(new) - 4) {
308302c97492SYaroslav Tykhiy reply(553, "Pathname too long.");
3084a117c345SYaroslav Tykhiy return (-1);
3085a117c345SYaroslav Tykhiy }
3086ea022d16SRodney W. Grimes *cp = '/';
3087a117c345SYaroslav Tykhiy }
3088e760ef2cSWarner Losh /* -4 is for the .nn<null> we put on the end below */
3089e760ef2cSWarner Losh (void) snprintf(new, sizeof(new) - 4, "%s", local);
3090ea022d16SRodney W. Grimes cp = new + strlen(new);
3091a117c345SYaroslav Tykhiy /*
3092a117c345SYaroslav Tykhiy * Don't generate dotfile unless requested explicitly.
3093a117c345SYaroslav Tykhiy * This covers the case when basename gets truncated off
3094a117c345SYaroslav Tykhiy * by buffer size.
3095a117c345SYaroslav Tykhiy */
3096a117c345SYaroslav Tykhiy if (cp > new && cp[-1] != '/')
3097ea022d16SRodney W. Grimes *cp++ = '.';
3098a117c345SYaroslav Tykhiy for (count = 0; count < 100; count++) {
3099a117c345SYaroslav Tykhiy /* At count 0 try unmodified name */
3100a117c345SYaroslav Tykhiy if (count)
3101ea022d16SRodney W. Grimes (void)sprintf(cp, "%d", count);
3102a117c345SYaroslav Tykhiy if ((fd = open(count ? new : local,
3103a117c345SYaroslav Tykhiy O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) {
3104a117c345SYaroslav Tykhiy *name = count ? new : local;
3105a117c345SYaroslav Tykhiy return (fd);
3106a117c345SYaroslav Tykhiy }
310788b70721SYaroslav Tykhiy if (errno != EEXIST) {
310850618d61SYaroslav Tykhiy perror_reply(553, count ? new : local);
310988b70721SYaroslav Tykhiy return (-1);
311088b70721SYaroslav Tykhiy }
3111ea022d16SRodney W. Grimes }
3112ea022d16SRodney W. Grimes reply(452, "Unique file name cannot be created.");
3113a117c345SYaroslav Tykhiy return (-1);
3114ea022d16SRodney W. Grimes }
3115ea022d16SRodney W. Grimes
3116ea022d16SRodney W. Grimes /*
3117ea022d16SRodney W. Grimes * Format and send reply containing system error number.
3118ea022d16SRodney W. Grimes */
3119ea022d16SRodney W. Grimes void
perror_reply(int code,char * string)3120e4bc453cSWarner Losh perror_reply(int code, char *string)
3121ea022d16SRodney W. Grimes {
3122ea022d16SRodney W. Grimes
3123ea022d16SRodney W. Grimes reply(code, "%s: %s.", string, strerror(errno));
3124ea022d16SRodney W. Grimes }
3125ea022d16SRodney W. Grimes
3126ea022d16SRodney W. Grimes static char *onefile[] = {
3127ea022d16SRodney W. Grimes "",
3128ea022d16SRodney W. Grimes 0
3129ea022d16SRodney W. Grimes };
3130ea022d16SRodney W. Grimes
3131ea022d16SRodney W. Grimes void
send_file_list(char * whichf)3132e4bc453cSWarner Losh send_file_list(char *whichf)
3133ea022d16SRodney W. Grimes {
3134ea022d16SRodney W. Grimes struct stat st;
3135ea022d16SRodney W. Grimes DIR *dirp = NULL;
3136ea022d16SRodney W. Grimes struct dirent *dir;
3137ea022d16SRodney W. Grimes FILE *dout = NULL;
3138ea022d16SRodney W. Grimes char **dirlist, *dirname;
3139ea022d16SRodney W. Grimes int simple = 0;
3140ea022d16SRodney W. Grimes int freeglob = 0;
3141ea022d16SRodney W. Grimes glob_t gl;
3142ea022d16SRodney W. Grimes
3143ea022d16SRodney W. Grimes if (strpbrk(whichf, "~{[*?") != NULL) {
314412da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
3145ea022d16SRodney W. Grimes
3146ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl));
31476d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS;
314875dc5f1aSMike Heffner flags |= GLOB_LIMIT;
3149ea022d16SRodney W. Grimes freeglob = 1;
3150ea022d16SRodney W. Grimes if (glob(whichf, flags, 0, &gl)) {
315102c97492SYaroslav Tykhiy reply(550, "No matching files found.");
3152ea022d16SRodney W. Grimes goto out;
3153ea022d16SRodney W. Grimes } else if (gl.gl_pathc == 0) {
3154ea022d16SRodney W. Grimes errno = ENOENT;
3155ea022d16SRodney W. Grimes perror_reply(550, whichf);
3156ea022d16SRodney W. Grimes goto out;
3157ea022d16SRodney W. Grimes }
3158ea022d16SRodney W. Grimes dirlist = gl.gl_pathv;
3159ea022d16SRodney W. Grimes } else {
3160ea022d16SRodney W. Grimes onefile[0] = whichf;
3161ea022d16SRodney W. Grimes dirlist = onefile;
3162ea022d16SRodney W. Grimes simple = 1;
3163ea022d16SRodney W. Grimes }
3164ea022d16SRodney W. Grimes
31659aca17cbSMark Murray while ((dirname = *dirlist++)) {
3166ea022d16SRodney W. Grimes if (stat(dirname, &st) < 0) {
3167ea022d16SRodney W. Grimes /*
3168ea022d16SRodney W. Grimes * If user typed "ls -l", etc, and the client
3169ea022d16SRodney W. Grimes * used NLST, do what the user meant.
3170ea022d16SRodney W. Grimes */
3171ea022d16SRodney W. Grimes if (dirname[0] == '-' && *dirlist == NULL &&
31724cd51076SYaroslav Tykhiy dout == NULL)
3173af85d782SDavid Nugent retrieve(_PATH_LS " %s", dirname);
31744cd51076SYaroslav Tykhiy else
3175ea022d16SRodney W. Grimes perror_reply(550, whichf);
3176ea022d16SRodney W. Grimes goto out;
3177ea022d16SRodney W. Grimes }
3178ea022d16SRodney W. Grimes
3179ea022d16SRodney W. Grimes if (S_ISREG(st.st_mode)) {
3180ea022d16SRodney W. Grimes if (dout == NULL) {
31810e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w");
3182ea022d16SRodney W. Grimes if (dout == NULL)
3183ea022d16SRodney W. Grimes goto out;
31844cd51076SYaroslav Tykhiy STARTXFER;
3185ea022d16SRodney W. Grimes }
31864cd51076SYaroslav Tykhiy START_UNSAFE;
3187ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", dirname,
3188ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : "");
31894cd51076SYaroslav Tykhiy END_UNSAFE;
31904cd51076SYaroslav Tykhiy if (ferror(dout))
31914cd51076SYaroslav Tykhiy goto data_err;
31924cd51076SYaroslav Tykhiy byte_count += strlen(dirname) +
31934cd51076SYaroslav Tykhiy (type == TYPE_A ? 2 : 1);
31944cd51076SYaroslav Tykhiy CHECKOOB(goto abrt);
3195ea022d16SRodney W. Grimes continue;
3196ea022d16SRodney W. Grimes } else if (!S_ISDIR(st.st_mode))
3197ea022d16SRodney W. Grimes continue;
3198ea022d16SRodney W. Grimes
3199ea022d16SRodney W. Grimes if ((dirp = opendir(dirname)) == NULL)
3200ea022d16SRodney W. Grimes continue;
3201ea022d16SRodney W. Grimes
3202ea022d16SRodney W. Grimes while ((dir = readdir(dirp)) != NULL) {
3203ea022d16SRodney W. Grimes char nbuf[MAXPATHLEN];
3204ea022d16SRodney W. Grimes
32054cd51076SYaroslav Tykhiy CHECKOOB(goto abrt);
32064b82fc95SYaroslav Tykhiy
3207ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_namlen == 1)
3208ea022d16SRodney W. Grimes continue;
3209ea022d16SRodney W. Grimes if (dir->d_name[0] == '.' && dir->d_name[1] == '.' &&
3210ea022d16SRodney W. Grimes dir->d_namlen == 2)
3211ea022d16SRodney W. Grimes continue;
3212ea022d16SRodney W. Grimes
3213e760ef2cSWarner Losh snprintf(nbuf, sizeof(nbuf),
3214e760ef2cSWarner Losh "%s/%s", dirname, dir->d_name);
3215ea022d16SRodney W. Grimes
3216ea022d16SRodney W. Grimes /*
3217ea022d16SRodney W. Grimes * We have to do a stat to insure it's
3218ea022d16SRodney W. Grimes * not a directory or special file.
3219ea022d16SRodney W. Grimes */
3220ea022d16SRodney W. Grimes if (simple || (stat(nbuf, &st) == 0 &&
3221ea022d16SRodney W. Grimes S_ISREG(st.st_mode))) {
3222ea022d16SRodney W. Grimes if (dout == NULL) {
32230e519c96SYaroslav Tykhiy dout = dataconn("file list", -1, "w");
3224ea022d16SRodney W. Grimes if (dout == NULL)
3225ea022d16SRodney W. Grimes goto out;
32264cd51076SYaroslav Tykhiy STARTXFER;
3227ea022d16SRodney W. Grimes }
32284cd51076SYaroslav Tykhiy START_UNSAFE;
3229ea022d16SRodney W. Grimes if (nbuf[0] == '.' && nbuf[1] == '/')
3230ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", &nbuf[2],
3231ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : "");
3232ea022d16SRodney W. Grimes else
3233ea022d16SRodney W. Grimes fprintf(dout, "%s%s\n", nbuf,
3234ea022d16SRodney W. Grimes type == TYPE_A ? "\r" : "");
32354cd51076SYaroslav Tykhiy END_UNSAFE;
32364cd51076SYaroslav Tykhiy if (ferror(dout))
32374cd51076SYaroslav Tykhiy goto data_err;
32384cd51076SYaroslav Tykhiy byte_count += strlen(nbuf) +
32394cd51076SYaroslav Tykhiy (type == TYPE_A ? 2 : 1);
32404cd51076SYaroslav Tykhiy CHECKOOB(goto abrt);
3241ea022d16SRodney W. Grimes }
3242ea022d16SRodney W. Grimes }
3243ea022d16SRodney W. Grimes (void) closedir(dirp);
32444cd51076SYaroslav Tykhiy dirp = NULL;
3245ea022d16SRodney W. Grimes }
3246ea022d16SRodney W. Grimes
3247ea022d16SRodney W. Grimes if (dout == NULL)
3248ea022d16SRodney W. Grimes reply(550, "No files found.");
32494cd51076SYaroslav Tykhiy else if (ferror(dout))
32504cd51076SYaroslav Tykhiy data_err: perror_reply(550, "Data connection");
3251ea022d16SRodney W. Grimes else
3252ea022d16SRodney W. Grimes reply(226, "Transfer complete.");
32534cd51076SYaroslav Tykhiy out:
32544cd51076SYaroslav Tykhiy if (dout) {
32554cd51076SYaroslav Tykhiy ENDXFER;
32564cd51076SYaroslav Tykhiy abrt:
3257ea022d16SRodney W. Grimes (void) fclose(dout);
3258ea022d16SRodney W. Grimes data = -1;
3259ea022d16SRodney W. Grimes pdata = -1;
32604cd51076SYaroslav Tykhiy }
32614cd51076SYaroslav Tykhiy if (dirp)
32624cd51076SYaroslav Tykhiy (void) closedir(dirp);
3263ea022d16SRodney W. Grimes if (freeglob) {
3264ea022d16SRodney W. Grimes freeglob = 0;
3265ea022d16SRodney W. Grimes globfree(&gl);
3266ea022d16SRodney W. Grimes }
3267ea022d16SRodney W. Grimes }
3268ea022d16SRodney W. Grimes
3269cf09a206SDavid Greenman void
reapchild(int signo)3270e4bc453cSWarner Losh reapchild(int signo)
3271cf09a206SDavid Greenman {
3272de9b6c03SYaroslav Tykhiy while (waitpid(-1, NULL, WNOHANG) > 0);
3273cf09a206SDavid Greenman }
3274cf09a206SDavid Greenman
327561f891a6SPaul Traina static void
appendf(char ** strp,char * fmt,...)32766b2dee6bSYaroslav Tykhiy appendf(char **strp, char *fmt, ...)
32776b2dee6bSYaroslav Tykhiy {
32786b2dee6bSYaroslav Tykhiy va_list ap;
32796b2dee6bSYaroslav Tykhiy char *ostr, *p;
32806b2dee6bSYaroslav Tykhiy
32816b2dee6bSYaroslav Tykhiy va_start(ap, fmt);
32826b2dee6bSYaroslav Tykhiy vasprintf(&p, fmt, ap);
32836b2dee6bSYaroslav Tykhiy va_end(ap);
32846b2dee6bSYaroslav Tykhiy if (p == NULL)
32856b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory.");
32866b2dee6bSYaroslav Tykhiy if (*strp == NULL)
32876b2dee6bSYaroslav Tykhiy *strp = p;
32886b2dee6bSYaroslav Tykhiy else {
32896b2dee6bSYaroslav Tykhiy ostr = *strp;
32906b2dee6bSYaroslav Tykhiy asprintf(strp, "%s%s", ostr, p);
32916b2dee6bSYaroslav Tykhiy if (*strp == NULL)
32926b2dee6bSYaroslav Tykhiy fatalerror("Ran out of memory.");
32936b2dee6bSYaroslav Tykhiy free(ostr);
32946b2dee6bSYaroslav Tykhiy }
32956b2dee6bSYaroslav Tykhiy }
32966b2dee6bSYaroslav Tykhiy
32976b2dee6bSYaroslav Tykhiy static void
logcmd(char * cmd,char * file1,char * file2,off_t cnt)32986b2dee6bSYaroslav Tykhiy logcmd(char *cmd, char *file1, char *file2, off_t cnt)
32996b2dee6bSYaroslav Tykhiy {
33006b2dee6bSYaroslav Tykhiy char *msg = NULL;
33016b2dee6bSYaroslav Tykhiy char wd[MAXPATHLEN + 1];
33026b2dee6bSYaroslav Tykhiy
33036b2dee6bSYaroslav Tykhiy if (logging <= 1)
33046b2dee6bSYaroslav Tykhiy return;
3305e897216fSYaroslav Tykhiy
33066b2dee6bSYaroslav Tykhiy if (getcwd(wd, sizeof(wd) - 1) == NULL)
33076b2dee6bSYaroslav Tykhiy strcpy(wd, strerror(errno));
33086b2dee6bSYaroslav Tykhiy
33096b2dee6bSYaroslav Tykhiy appendf(&msg, "%s", cmd);
33106b2dee6bSYaroslav Tykhiy if (file1)
33116b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file1);
33126b2dee6bSYaroslav Tykhiy if (file2)
33136b2dee6bSYaroslav Tykhiy appendf(&msg, " %s", file2);
33146b2dee6bSYaroslav Tykhiy if (cnt >= 0)
33156b2dee6bSYaroslav Tykhiy appendf(&msg, " = %jd bytes", (intmax_t)cnt);
3316e897216fSYaroslav Tykhiy appendf(&msg, " (wd: %s", wd);
3317215a9f9dSYaroslav Tykhiy if (guest || dochroot)
3318e897216fSYaroslav Tykhiy appendf(&msg, "; chrooted");
3319e897216fSYaroslav Tykhiy appendf(&msg, ")");
33206b2dee6bSYaroslav Tykhiy syslog(LOG_INFO, "%s", msg);
33216b2dee6bSYaroslav Tykhiy free(msg);
33226b2dee6bSYaroslav Tykhiy }
33236b2dee6bSYaroslav Tykhiy
33246b2dee6bSYaroslav Tykhiy static void
logxfer(char * name,off_t size,time_t start)3325e4bc453cSWarner Losh logxfer(char *name, off_t size, time_t start)
33263eb568f2SGuido van Rooij {
33278c1c21f2SYaroslav Tykhiy char buf[MAXPATHLEN + 1024];
33283eb568f2SGuido van Rooij char path[MAXPATHLEN + 1];
3329158a00b2SJohn Birrell time_t now;
33303eb568f2SGuido van Rooij
33318c1c21f2SYaroslav Tykhiy if (statfd >= 0) {
33323eb568f2SGuido van Rooij time(&now);
33338c1c21f2SYaroslav Tykhiy if (realpath(name, path) == NULL) {
33348c1c21f2SYaroslav Tykhiy syslog(LOG_NOTICE, "realpath failed on %s: %m", path);
33358c1c21f2SYaroslav Tykhiy return;
33368c1c21f2SYaroslav Tykhiy }
33378c1c21f2SYaroslav Tykhiy snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n",
33383eb568f2SGuido van Rooij ctime(&now)+4, ident, remotehost,
33398c1c21f2SYaroslav Tykhiy path, (intmax_t)size,
3340e4a71114SAndrey A. Chernov (long)(now - start + (now == start)));
33413eb568f2SGuido van Rooij write(statfd, buf, strlen(buf));
33423eb568f2SGuido van Rooij }
33433eb568f2SGuido van Rooij }
3344e4648f05SYaroslav Tykhiy
3345e4648f05SYaroslav Tykhiy static char *
doublequote(char * s)3346e4648f05SYaroslav Tykhiy doublequote(char *s)
3347e4648f05SYaroslav Tykhiy {
3348e4648f05SYaroslav Tykhiy int n;
3349e4648f05SYaroslav Tykhiy char *p, *s2;
3350e4648f05SYaroslav Tykhiy
3351e4648f05SYaroslav Tykhiy for (p = s, n = 0; *p; p++)
3352e4648f05SYaroslav Tykhiy if (*p == '"')
3353e4648f05SYaroslav Tykhiy n++;
3354e4648f05SYaroslav Tykhiy
3355e4648f05SYaroslav Tykhiy if ((s2 = malloc(p - s + n + 1)) == NULL)
3356e4648f05SYaroslav Tykhiy return (NULL);
3357e4648f05SYaroslav Tykhiy
3358e4648f05SYaroslav Tykhiy for (p = s2; *s; s++, p++) {
3359e4648f05SYaroslav Tykhiy if ((*p = *s) == '"')
3360e4648f05SYaroslav Tykhiy *(++p) = '"';
3361e4648f05SYaroslav Tykhiy }
3362e4648f05SYaroslav Tykhiy *p = '\0';
3363e4648f05SYaroslav Tykhiy
3364e4648f05SYaroslav Tykhiy return (s2);
3365e4648f05SYaroslav Tykhiy }
3366206fe568SHajimu UMEMOTO
3367206fe568SHajimu UMEMOTO /* setup server socket for specified address family */
3368206fe568SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
3369206fe568SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
3370206fe568SHajimu UMEMOTO static int *
socksetup(int af,char * bindname,const char * bindport)3371206fe568SHajimu UMEMOTO socksetup(int af, char *bindname, const char *bindport)
3372206fe568SHajimu UMEMOTO {
3373206fe568SHajimu UMEMOTO struct addrinfo hints, *res, *r;
3374206fe568SHajimu UMEMOTO int error, maxs, *s, *socks;
3375206fe568SHajimu UMEMOTO const int on = 1;
3376206fe568SHajimu UMEMOTO
3377206fe568SHajimu UMEMOTO memset(&hints, 0, sizeof(hints));
3378206fe568SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE;
3379206fe568SHajimu UMEMOTO hints.ai_family = af;
3380206fe568SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM;
3381206fe568SHajimu UMEMOTO error = getaddrinfo(bindname, bindport, &hints, &res);
3382206fe568SHajimu UMEMOTO if (error) {
3383206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", gai_strerror(error));
3384206fe568SHajimu UMEMOTO if (error == EAI_SYSTEM)
3385206fe568SHajimu UMEMOTO syslog(LOG_ERR, "%s", strerror(errno));
3386206fe568SHajimu UMEMOTO return NULL;
3387206fe568SHajimu UMEMOTO }
3388206fe568SHajimu UMEMOTO
3389206fe568SHajimu UMEMOTO /* Count max number of sockets we may open */
3390206fe568SHajimu UMEMOTO for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
3391206fe568SHajimu UMEMOTO ;
3392206fe568SHajimu UMEMOTO socks = malloc((maxs + 1) * sizeof(int));
3393206fe568SHajimu UMEMOTO if (!socks) {
3394206fe568SHajimu UMEMOTO freeaddrinfo(res);
3395206fe568SHajimu UMEMOTO syslog(LOG_ERR, "couldn't allocate memory for sockets");
3396206fe568SHajimu UMEMOTO return NULL;
3397206fe568SHajimu UMEMOTO }
3398206fe568SHajimu UMEMOTO
3399206fe568SHajimu UMEMOTO *socks = 0; /* num of sockets counter at start of array */
3400206fe568SHajimu UMEMOTO s = socks + 1;
3401206fe568SHajimu UMEMOTO for (r = res; r; r = r->ai_next) {
3402206fe568SHajimu UMEMOTO *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
3403206fe568SHajimu UMEMOTO if (*s < 0) {
3404206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control socket: %m");
3405206fe568SHajimu UMEMOTO continue;
3406206fe568SHajimu UMEMOTO }
3407206fe568SHajimu UMEMOTO if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR,
3408206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0)
3409206fe568SHajimu UMEMOTO syslog(LOG_WARNING,
3410206fe568SHajimu UMEMOTO "control setsockopt (SO_REUSEADDR): %m");
3411206fe568SHajimu UMEMOTO if (r->ai_family == AF_INET6) {
3412206fe568SHajimu UMEMOTO if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
3413206fe568SHajimu UMEMOTO &on, sizeof(on)) < 0)
3414206fe568SHajimu UMEMOTO syslog(LOG_WARNING,
3415206fe568SHajimu UMEMOTO "control setsockopt (IPV6_V6ONLY): %m");
3416206fe568SHajimu UMEMOTO }
3417206fe568SHajimu UMEMOTO if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
3418206fe568SHajimu UMEMOTO syslog(LOG_DEBUG, "control bind: %m");
3419206fe568SHajimu UMEMOTO close(*s);
3420206fe568SHajimu UMEMOTO continue;
3421206fe568SHajimu UMEMOTO }
3422206fe568SHajimu UMEMOTO (*socks)++;
3423206fe568SHajimu UMEMOTO s++;
3424206fe568SHajimu UMEMOTO }
3425206fe568SHajimu UMEMOTO
3426206fe568SHajimu UMEMOTO if (res)
3427206fe568SHajimu UMEMOTO freeaddrinfo(res);
3428206fe568SHajimu UMEMOTO
3429206fe568SHajimu UMEMOTO if (*socks == 0) {
3430206fe568SHajimu UMEMOTO syslog(LOG_ERR, "control socket: Couldn't bind to any socket");
3431206fe568SHajimu UMEMOTO free(socks);
3432206fe568SHajimu UMEMOTO return NULL;
3433206fe568SHajimu UMEMOTO }
3434206fe568SHajimu UMEMOTO return(socks);
3435206fe568SHajimu UMEMOTO }
3436