-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftpd.c
3436 lines (3130 loc) · 77.2 KB
/
ftpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* FTP server.
*/
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#define FTP_NAMES
#include <arpa/ftp.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <libutil.h>
#ifdef LOGIN_CAP
#include <login_cap.h>
#endif
#ifdef USE_PAM
#include <security/pam_appl.h>
#endif
#include "blacklist_client.h"
#include "pathnames.h"
#include "extern.h"
#include <stdarg.h>
static char version[] = "Version 6.00LS";
#undef main
union sockunion ctrl_addr;
union sockunion data_source;
union sockunion data_dest;
union sockunion his_addr;
union sockunion pasv_addr;
int daemon_mode;
int data;
int dataport;
int hostinfo = 1; /* print host-specific info in messages */
int logged_in;
struct passwd *pw;
char *homedir;
int ftpdebug;
int timeout = 900; /* timeout after 15 minutes of inactivity */
int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */
int logging;
int restricted_data_ports = 1;
int paranoid = 1; /* be extra careful about security */
int anon_only = 0; /* Only anonymous ftp allowed */
int assumeutf8 = 0; /* Assume that server file names are in UTF-8 */
int guest;
int dochroot;
char *chrootdir;
int dowtmp = 1;
int stats;
int statfd = -1;
int type;
int form;
int stru; /* avoid C keyword */
int mode;
int usedefault = 1; /* for data transfers */
int pdata = -1; /* for passive mode */
int readonly = 0; /* Server is in readonly mode. */
int noepsv = 0; /* EPSV command is disabled. */
int noretr = 0; /* RETR command is disabled. */
int noguestretr = 0; /* RETR command is disabled for anon users. */
int noguestmkd = 0; /* MKD command is disabled for anon users. */
int noguestmod = 1; /* anon users may not modify existing files. */
int use_blacklist = 0;
off_t file_size;
off_t byte_count;
#if !defined(CMASK) || CMASK == 0
#undef CMASK
#define CMASK 027
#endif
int defumask = CMASK; /* default umask value */
char tmpline[7];
char *hostname;
int epsvall = 0;
#ifdef VIRTUAL_HOSTING
char *ftpuser;
static struct ftphost {
struct ftphost *next;
struct addrinfo *hostinfo;
char *hostname;
char *anonuser;
char *statfile;
char *welcome;
char *loginmsg;
} *thishost, *firsthost;
#endif
char remotehost[NI_MAXHOST];
char *ident = NULL;
static char wtmpid[20];
#ifdef USE_PAM
static int auth_pam(struct passwd**, const char*);
pam_handle_t *pamh = NULL;
#endif
char *pid_file = NULL; /* means default location to pidfile(3) */
/*
* Limit number of pathnames that glob can return.
* A limit of 0 indicates the number of pathnames is unlimited.
*/
#define MAXGLOBARGS 16384
#
/*
* Timeout intervals for retrying connections
* to hosts that don't accept PORT cmds. This
* is a kludge, but given the problems with TCP...
*/
#define SWAITMAX 90 /* wait at most 90 seconds */
#define SWAITINT 5 /* interval between retries */
int swaitmax = SWAITMAX;
int swaitint = SWAITINT;
#ifdef SETPROCTITLE
char proctitle[LINE_MAX]; /* initial part of title */
#endif /* SETPROCTITLE */
#define LOGCMD(cmd, file) logcmd((cmd), (file), NULL, -1)
#define LOGCMD2(cmd, file1, file2) logcmd((cmd), (file1), (file2), -1)
#define LOGBYTES(cmd, file, cnt) logcmd((cmd), (file), NULL, (cnt))
static volatile sig_atomic_t recvurg;
static int transflag; /* NB: for debugging only */
#define STARTXFER flagxfer(1)
#define ENDXFER flagxfer(0)
#define START_UNSAFE maskurg(1)
#define END_UNSAFE maskurg(0)
/* It's OK to put an `else' clause after this macro. */
#define CHECKOOB(action) \
if (recvurg) { \
recvurg = 0; \
if (myoob()) { \
ENDXFER; \
action; \
} \
}
#ifdef VIRTUAL_HOSTING
static void inithosts(int);
static void selecthost(union sockunion *);
#endif
static void ack(char *);
static void sigurg(int);
static void maskurg(int);
static void flagxfer(int);
static int myoob(void);
static int checkuser(char *, char *, int, char **, int *);
static FILE *dataconn(char *, off_t, char *);
static void dolog(struct sockaddr *);
static void end_login(void);
static FILE *getdatasock(char *);
static int guniquefd(char *, char **);
static void lostconn(int);
static void sigquit(int);
static int receive_data(FILE *, FILE *);
static int send_data(FILE *, FILE *, size_t, off_t, off_t, int);
static struct passwd *
sgetpwnam(char *);
static char *sgetsave(char *);
static void reapchild(int);
static void appendf(char **, char *, ...) __printflike(2, 3);
static void logcmd(char *, char *, char *, off_t);
static void logxfer(char *, off_t, time_t);
static char *doublequote(char *);
static int *socksetup(int, char *, const char *);
int
main(int argc, char *argv[], char **envp)
{
socklen_t addrlen;
int ch, on = 1, tos, s = STDIN_FILENO;
char *cp, line[LINE_MAX];
FILE *fd;
char *bindname = NULL;
const char *bindport = "ftp";
int family = AF_UNSPEC;
struct sigaction sa;
tzset(); /* in case no timezone database in ~ftp */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
/*
* Prevent diagnostic messages from appearing on stderr.
* We run as a daemon or from inetd; in both cases, there's
* more reason in logging to syslog.
*/
(void) freopen(_PATH_DEVNULL, "w", stderr);
opterr = 0;
/*
* LOG_NDELAY sets up the logging connection immediately,
* necessary for anonymous ftp's that chroot and can't do it later.
*/
openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
while ((ch = getopt(argc, argv,
"468a:ABdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) {
switch (ch) {
case '4':
family = (family == AF_INET6) ? AF_UNSPEC : AF_INET;
break;
case '6':
family = (family == AF_INET) ? AF_UNSPEC : AF_INET6;
break;
case '8':
assumeutf8 = 1;
break;
case 'a':
bindname = optarg;
break;
case 'A':
anon_only = 1;
break;
case 'B':
#ifdef USE_BLACKLIST
use_blacklist = 1;
#else
syslog(LOG_WARNING, "not compiled with USE_BLACKLIST support");
#endif
break;
case 'd':
ftpdebug++;
break;
case 'D':
daemon_mode++;
break;
case 'E':
noepsv = 1;
break;
case 'h':
hostinfo = 0;
break;
case 'l':
logging++; /* > 1 == extra logging */
break;
case 'm':
noguestmod = 0;
break;
case 'M':
noguestmkd = 1;
break;
case 'o':
noretr = 1;
break;
case 'O':
noguestretr = 1;
break;
case 'p':
pid_file = optarg;
break;
case 'P':
bindport = optarg;
break;
case 'r':
readonly = 1;
break;
case 'R':
paranoid = 0;
break;
case 'S':
stats++;
break;
case 't':
timeout = atoi(optarg);
if (maxtimeout < timeout)
maxtimeout = timeout;
break;
case 'T':
maxtimeout = atoi(optarg);
if (timeout > maxtimeout)
timeout = maxtimeout;
break;
case 'u':
{
long val = 0;
val = strtol(optarg, &optarg, 8);
if (*optarg != '\0' || val < 0)
syslog(LOG_WARNING, "bad value for -u");
else
defumask = val;
break;
}
case 'U':
restricted_data_ports = 0;
break;
case 'v':
ftpdebug++;
break;
case 'W':
dowtmp = 0;
break;
default:
syslog(LOG_WARNING, "unknown flag -%c ignored", optopt);
break;
}
}
/* handle filesize limit gracefully */
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGXFSZ, &sa, NULL);
if (daemon_mode) {
int *ctl_sock, fd, maxfd = -1, nfds, i;
fd_set defreadfds, readfds;
pid_t pid;
struct pidfh *pfh;
if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) {
if (errno == EEXIST) {
syslog(LOG_ERR, "%s already running, pid %d",
getprogname(), (int)pid);
exit(1);
}
syslog(LOG_WARNING, "pidfile_open: %m");
}
/*
* Detach from parent.
*/
if (daemon(1, 1) < 0) {
syslog(LOG_ERR, "failed to become a daemon");
exit(1);
}
if (pfh != NULL && pidfile_write(pfh) == -1)
syslog(LOG_WARNING, "pidfile_write: %m");
sa.sa_handler = reapchild;
(void)sigaction(SIGCHLD, &sa, NULL);
#ifdef VIRTUAL_HOSTING
inithosts(family);
#endif
/*
* Open a socket, bind it to the FTP port, and start
* listening.
*/
ctl_sock = socksetup(family, bindname, bindport);
if (ctl_sock == NULL)
exit(1);
FD_ZERO(&defreadfds);
for (i = 1; i <= *ctl_sock; i++) {
FD_SET(ctl_sock[i], &defreadfds);
if (listen(ctl_sock[i], 32) < 0) {
syslog(LOG_ERR, "control listen: %m");
exit(1);
}
if (maxfd < ctl_sock[i])
maxfd = ctl_sock[i];
}
/*
* Loop forever accepting connection requests and forking off
* children to handle them.
*/
while (1) {
FD_COPY(&defreadfds, &readfds);
nfds = select(maxfd + 1, &readfds, NULL, NULL, 0);
if (nfds <= 0) {
if (nfds < 0 && errno != EINTR)
syslog(LOG_WARNING, "select: %m");
continue;
}
pid = -1;
for (i = 1; i <= *ctl_sock; i++)
if (FD_ISSET(ctl_sock[i], &readfds)) {
addrlen = sizeof(his_addr);
fd = accept(ctl_sock[i],
(struct sockaddr *)&his_addr,
&addrlen);
if (fd == -1) {
syslog(LOG_WARNING,
"accept: %m");
continue;
}
switch (pid = fork()) {
case 0:
/* child */
(void) dup2(fd, s);
(void) dup2(fd, STDOUT_FILENO);
(void) close(fd);
for (i = 1; i <= *ctl_sock; i++)
close(ctl_sock[i]);
if (pfh != NULL)
pidfile_close(pfh);
goto gotchild;
case -1:
syslog(LOG_WARNING, "fork: %m");
/* FALLTHROUGH */
default:
close(fd);
}
}
}
} else {
addrlen = sizeof(his_addr);
if (getpeername(s, (struct sockaddr *)&his_addr, &addrlen) < 0) {
syslog(LOG_ERR, "getpeername (%s): %m",argv[0]);
exit(1);
}
#ifdef VIRTUAL_HOSTING
if (his_addr.su_family == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))
family = AF_INET;
else
family = his_addr.su_family;
inithosts(family);
#endif
}
gotchild:
sa.sa_handler = SIG_DFL;
(void)sigaction(SIGCHLD, &sa, NULL);
sa.sa_handler = sigurg;
sa.sa_flags = 0; /* don't restart syscalls for SIGURG */
(void)sigaction(SIGURG, &sa, NULL);
sigfillset(&sa.sa_mask); /* block all signals in handler */
sa.sa_flags = SA_RESTART;
sa.sa_handler = sigquit;
(void)sigaction(SIGHUP, &sa, NULL);
(void)sigaction(SIGINT, &sa, NULL);
(void)sigaction(SIGQUIT, &sa, NULL);
(void)sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = lostconn;
(void)sigaction(SIGPIPE, &sa, NULL);
addrlen = sizeof(ctrl_addr);
if (getsockname(s, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) {
syslog(LOG_ERR, "getsockname (%s): %m",argv[0]);
exit(1);
}
dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */
#ifdef VIRTUAL_HOSTING
/* select our identity from virtual host table */
selecthost(&ctrl_addr);
#endif
#ifdef IP_TOS
if (ctrl_addr.su_family == AF_INET)
{
tos = IPTOS_LOWDELAY;
if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0)
syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m");
}
#endif
/*
* Disable Nagle on the control channel so that we don't have to wait
* for peer's ACK before issuing our next reply.
*/
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m");
data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
(void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid());
/* Try to handle urgent data inline */
#ifdef SO_OOBINLINE
if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0)
syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m");
#endif
#ifdef F_SETOWN
if (fcntl(s, F_SETOWN, getpid()) == -1)
syslog(LOG_ERR, "fcntl F_SETOWN: %m");
#endif
dolog((struct sockaddr *)&his_addr);
/*
* Set up default state
*/
data = -1;
type = TYPE_A;
form = FORM_N;
stru = STRU_F;
mode = MODE_S;
tmpline[0] = '\0';
/* If logins are disabled, print out the message. */
if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) {
while (fgets(line, sizeof(line), fd) != NULL) {
if ((cp = strchr(line, '\n')) != NULL)
*cp = '\0';
lreply(530, "%s", line);
}
(void) fflush(stdout);
(void) fclose(fd);
reply(530, "System not available.");
exit(0);
}
#ifdef VIRTUAL_HOSTING
fd = fopen(thishost->welcome, "r");
#else
fd = fopen(_PATH_FTPWELCOME, "r");
#endif
if (fd != NULL) {
while (fgets(line, sizeof(line), fd) != NULL) {
if ((cp = strchr(line, '\n')) != NULL)
*cp = '\0';
lreply(220, "%s", line);
}
(void) fflush(stdout);
(void) fclose(fd);
/* reply(220,) must follow */
}
#ifndef VIRTUAL_HOSTING
if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
fatalerror("Ran out of memory.");
if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
hostname[0] = '\0';
hostname[MAXHOSTNAMELEN - 1] = '\0';
#endif
if (hostinfo)
reply(220, "%s FTP server (%s) ready.", hostname, version);
else
reply(220, "FTP server ready.");
BLACKLIST_INIT();
for (;;)
(void) yyparse();
/* NOTREACHED */
}
static void
lostconn(int signo)
{
if (ftpdebug)
syslog(LOG_DEBUG, "lost connection");
dologout(1);
}
static void
sigquit(int signo)
{
syslog(LOG_ERR, "got signal %d", signo);
dologout(1);
}
#ifdef VIRTUAL_HOSTING
/*
* read in virtual host tables (if they exist)
*/
static void
inithosts(int family)
{
int insert;
size_t len;
FILE *fp;
char *cp, *mp, *line;
char *hostname;
char *vhost, *anonuser, *statfile, *welcome, *loginmsg;
struct ftphost *hrp, *lhrp;
struct addrinfo hints, *res, *ai;
/*
* Fill in the default host information
*/
if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL)
fatalerror("Ran out of memory.");
if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0)
hostname[0] = '\0';
hostname[MAXHOSTNAMELEN - 1] = '\0';
if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
fatalerror("Ran out of memory.");
hrp->hostname = hostname;
hrp->hostinfo = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0)
hrp->hostinfo = res;
hrp->statfile = _PATH_FTPDSTATFILE;
hrp->welcome = _PATH_FTPWELCOME;
hrp->loginmsg = _PATH_FTPLOGINMESG;
hrp->anonuser = "ftp";
hrp->next = NULL;
thishost = firsthost = lhrp = hrp;
if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) {
int addrsize, gothost;
void *addr;
struct hostent *hp;
while ((line = fgetln(fp, &len)) != NULL) {
int i, hp_error;
/* skip comments */
if (line[0] == '#')
continue;
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
mp = NULL;
} else {
if ((mp = malloc(len + 1)) == NULL)
fatalerror("Ran out of memory.");
memcpy(mp, line, len);
mp[len] = '\0';
line = mp;
}
cp = strtok(line, " \t");
/* skip empty lines */
if (cp == NULL)
goto nextline;
vhost = cp;
/* set defaults */
anonuser = "ftp";
statfile = _PATH_FTPDSTATFILE;
welcome = _PATH_FTPWELCOME;
loginmsg = _PATH_FTPLOGINMESG;
/*
* Preparse the line so we can use its info
* for all the addresses associated with
* the virtual host name.
* Field 0, the virtual host name, is special:
* it's already parsed off and will be strdup'ed
* later, after we know its canonical form.
*/
for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++)
if (*cp != '-' && (cp = strdup(cp)))
switch (i) {
case 1: /* anon user permissions */
anonuser = cp;
break;
case 2: /* statistics file */
statfile = cp;
break;
case 3: /* welcome message */
welcome = cp;
break;
case 4: /* login message */
loginmsg = cp;
break;
default: /* programming error */
abort();
/* NOTREACHED */
}
hints.ai_flags = AI_PASSIVE;
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(vhost, NULL, &hints, &res) != 0)
goto nextline;
for (ai = res; ai != NULL && ai->ai_addr != NULL;
ai = ai->ai_next) {
gothost = 0;
for (hrp = firsthost; hrp != NULL; hrp = hrp->next) {
struct addrinfo *hi;
for (hi = hrp->hostinfo; hi != NULL;
hi = hi->ai_next)
if (hi->ai_addrlen == ai->ai_addrlen &&
memcmp(hi->ai_addr,
ai->ai_addr,
ai->ai_addr->sa_len) == 0) {
gothost++;
break;
}
if (gothost)
break;
}
if (hrp == NULL) {
if ((hrp = malloc(sizeof(struct ftphost))) == NULL)
goto nextline;
hrp->hostname = NULL;
insert = 1;
} else {
if (hrp->hostinfo && hrp->hostinfo != res)
freeaddrinfo(hrp->hostinfo);
insert = 0; /* host already in the chain */
}
hrp->hostinfo = res;
/*
* determine hostname to use.
* force defined name if there is a valid alias
* otherwise fallback to primary hostname
*/
/* XXX: getaddrinfo() can't do alias check */
switch(hrp->hostinfo->ai_family) {
case AF_INET:
addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr;
addrsize = sizeof(struct in_addr);
break;
case AF_INET6:
addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr;
addrsize = sizeof(struct in6_addr);
break;
default:
/* should not reach here */
freeaddrinfo(hrp->hostinfo);
if (insert)
free(hrp); /*not in chain, can free*/
else
hrp->hostinfo = NULL; /*mark as blank*/
goto nextline;
/* NOTREACHED */
}
if ((hp = getipnodebyaddr(addr, addrsize,
hrp->hostinfo->ai_family,
&hp_error)) != NULL) {
if (strcmp(vhost, hp->h_name) != 0) {
if (hp->h_aliases == NULL)
vhost = hp->h_name;
else {
i = 0;
while (hp->h_aliases[i] &&
strcmp(vhost, hp->h_aliases[i]) != 0)
++i;
if (hp->h_aliases[i] == NULL)
vhost = hp->h_name;
}
}
}
if (hrp->hostname &&
strcmp(hrp->hostname, vhost) != 0) {
free(hrp->hostname);
hrp->hostname = NULL;
}
if (hrp->hostname == NULL &&
(hrp->hostname = strdup(vhost)) == NULL) {
freeaddrinfo(hrp->hostinfo);
hrp->hostinfo = NULL; /* mark as blank */
if (hp)
freehostent(hp);
goto nextline;
}
hrp->anonuser = anonuser;
hrp->statfile = statfile;
hrp->welcome = welcome;
hrp->loginmsg = loginmsg;
if (insert) {
hrp->next = NULL;
lhrp->next = hrp;
lhrp = hrp;
}
if (hp)
freehostent(hp);
}
nextline:
if (mp)
free(mp);
}
(void) fclose(fp);
}
}
static void
selecthost(union sockunion *su)
{
struct ftphost *hrp;
u_int16_t port;
#ifdef INET6
struct in6_addr *mapped_in6 = NULL;
#endif
struct addrinfo *hi;
#ifdef INET6
/*
* XXX IPv4 mapped IPv6 addr consideraton,
* specified in rfc2373.
*/
if (su->su_family == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr))
mapped_in6 = &su->su_sin6.sin6_addr;
#endif
hrp = thishost = firsthost; /* default */
port = su->su_port;
su->su_port = 0;
while (hrp != NULL) {
for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) {
if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) {
thishost = hrp;
goto found;
}
#ifdef INET6
/* XXX IPv4 mapped IPv6 addr consideraton */
if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL &&
(memcmp(&mapped_in6->s6_addr[12],
&((struct sockaddr_in *)hi->ai_addr)->sin_addr,
sizeof(struct in_addr)) == 0)) {
thishost = hrp;
goto found;
}
#endif
}
hrp = hrp->next;
}
found:
su->su_port = port;
/* setup static variables as appropriate */
hostname = thishost->hostname;
ftpuser = thishost->anonuser;
}
#endif
/*
* Helper function for sgetpwnam().
*/
static char *
sgetsave(char *s)
{
char *new = malloc(strlen(s) + 1);
if (new == NULL) {
reply(421, "Ran out of memory.");
dologout(1);
/* NOTREACHED */
}
(void) strcpy(new, s);
return (new);
}
/*
* Save the result of a getpwnam. Used for USER command, since
* the data returned must not be clobbered by any other command
* (e.g., globbing).
* NB: The data returned by sgetpwnam() will remain valid until
* the next call to this function. Its difference from getpwnam()
* is that sgetpwnam() is known to be called from ftpd code only.
*/
static struct passwd *
sgetpwnam(char *name)
{
static struct passwd save;
struct passwd *p;
if ((p = getpwnam(name)) == NULL)
return (p);
if (save.pw_name) {
free(save.pw_name);
free(save.pw_passwd);
free(save.pw_class);
free(save.pw_gecos);
free(save.pw_dir);
free(save.pw_shell);
}
save = *p;
save.pw_name = sgetsave(p->pw_name);
save.pw_passwd = sgetsave(p->pw_passwd);
save.pw_class = sgetsave(p->pw_class);
save.pw_gecos = sgetsave(p->pw_gecos);
save.pw_dir = sgetsave(p->pw_dir);
save.pw_shell = sgetsave(p->pw_shell);
return (&save);
}
static int login_attempts; /* number of failed login attempts */
static int askpasswd; /* had user command, ask for passwd */
static char curname[MAXLOGNAME]; /* current USER name */
/*
* USER command.
* Sets global passwd pointer pw if named account exists and is acceptable;
* sets askpasswd if a PASS command is expected. If logged in previously,
* need to reset state. If name is "ftp" or "anonymous", the name is not in
* _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return.
* If account doesn't exist, ask for passwd anyway. Otherwise, check user
* requesting login privileges. Disallow anyone who does not have a standard
* shell as returned by getusershell(). Disallow anyone mentioned in the file
* _PATH_FTPUSERS to allow people such as root and uucp to be avoided.
*/
void
user(char *name)
{
int ecode;
char *cp, *shell;
if (logged_in) {
if (guest) {
reply(530, "Can't change user from guest login.");
return;
} else if (dochroot) {
reply(530, "Can't change user from chroot user.");
return;
}
end_login();
}
guest = 0;
#ifdef VIRTUAL_HOSTING
pw = sgetpwnam(thishost->anonuser);
#else
pw = sgetpwnam("ftp");
#endif
if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) {
if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) ||
(ecode != 0 && ecode != ENOENT))