-
Notifications
You must be signed in to change notification settings - Fork 890
/
Copy pathftpd.c
2858 lines (2582 loc) · 63.6 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
/* $OpenBSD: ftpd.c,v 1.234 2024/05/09 08:35:03 florian Exp $ */
/* $NetBSD: ftpd.c,v 1.15 1995/06/03 22:46:47 mycroft Exp $ */
/*
* Copyright (C) 1997 and 1998 WIDE Project.
* 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 project 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 PROJECT 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 PROJECT 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.
*/
/*
* 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/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <netinet/in.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 <bsd_auth.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <login_cap.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <vis.h>
#include <unistd.h>
#include <utmp.h>
#include <poll.h>
#include "pathnames.h"
#include "monitor.h"
#include "extern.h"
extern off_t restart_point;
extern char cbuf[];
union sockunion ctrl_addr;
union sockunion data_source;
union sockunion data_dest;
union sockunion his_addr;
union sockunion pasv_addr;
sigset_t allsigs;
int daemon_mode = 0;
int data;
int logged_in;
struct passwd *pw;
int debug = 0;
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 anon_ok = 1;
int anon_only = 0;
unsigned int minuid = 1000;
int multihome = 0;
int guest;
int stats;
int statfd = -1;
int portcheck = 1;
int dochroot;
int type;
int form;
int stru; /* avoid C keyword */
int mode;
int doutmp = 0; /* update utmp file */
int nowtmp = 0; /* do not update wtmp file */
int usedefault = 1; /* for data transfers */
int pdata = -1; /* for passive mode */
int family = AF_UNSPEC;
volatile sig_atomic_t transflag;
off_t file_size;
off_t byte_count;
mode_t defumask = S_IWGRP|S_IWOTH; /* default umask value */
int umaskchange = 1; /* allow user to change umask value. */
char tmpline[7];
char hostname[HOST_NAME_MAX+1];
char remotehost[HOST_NAME_MAX+1];
char dhostname[HOST_NAME_MAX+1];
char *guestpw;
char ttyline[20];
static struct utmp utmp; /* for utmp */
static login_cap_t *lc;
static auth_session_t *as;
static volatile sig_atomic_t recvurg;
int epsvall = 0;
/*
* 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;
char proctitle[BUFSIZ]; /* initial part of title */
#define LOGCMD(cmd, file) \
if (logging > 1) \
syslog(LOG_INFO,"%s %s%s", cmd, \
*(file) == '/' ? "" : curdir(), file);
#define LOGCMD2(cmd, file1, file2) \
if (logging > 1) \
syslog(LOG_INFO,"%s %s%s %s%s", cmd, \
*(file1) == '/' ? "" : curdir(), file1, \
*(file2) == '/' ? "" : curdir(), file2);
#define LOGBYTES(cmd, file, cnt) \
if (logging > 1) { \
if ((cnt) == -1) \
syslog(LOG_INFO,"%s %s%s", cmd, \
*(file) == '/' ? "" : curdir(), file); \
else \
syslog(LOG_INFO, "%s %s%s = %lld bytes", \
cmd, (*(file) == '/') ? "" : curdir(), file, \
(long long)(cnt)); \
}
static void ack(const char *);
static void sigurg(int);
static void myoob(void);
static int checkuser(char *, const char *);
static FILE *dataconn(const char *, off_t, char *);
static void dolog(struct sockaddr *);
static char *copy_dir(char *, struct passwd *);
static char *curdir(void);
static void end_login(void);
static FILE *getdatasock(char *);
static int guniquefd(const char *, char **);
static void lostconn(int);
static void sigquit(int);
static int receive_data(FILE *, FILE *);
static void replydirname(const char *, const char *);
static int send_data(FILE *, FILE *, off_t, off_t, int);
static struct passwd *
sgetpwnam(const char *, struct passwd *);
static void reapchild(int);
static void usage(void);
void logxfer(const char *, off_t, time_t);
void set_slave_signals(void);
static char *
curdir(void)
{
static char path[PATH_MAX+1]; /* path + '/' */
if (getcwd(path, sizeof(path)-1) == NULL)
return ("");
if (path[1] != '\0') /* special case for root dir. */
strlcat(path, "/", sizeof path);
/* For guest account, skip / since it's chrooted */
return (guest ? path+1 : path);
}
char *argstr = "AdDhnlm:MSt:T:u:PUvW46";
static void
usage(void)
{
syslog(LOG_ERR,
"usage: ftpd [-46ADdlMnPSUW] [-m minuid] [-T maxtimeout] "
"[-t timeout] [-u mask]");
exit(2);
}
int
main(int argc, char *argv[])
{
socklen_t addrlen;
int ch, on = 1, tos;
char line[LINE_MAX];
FILE *fp;
struct hostent *hp;
struct sigaction sa;
int error = 0;
const char *errstr;
tzset(); /* in case no timezone database in ~ftp */
sigfillset(&allsigs); /* used to block signals while root */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
while ((ch = getopt(argc, argv, argstr)) != -1) {
switch (ch) {
case 'A':
anon_only = 1;
break;
case 'd':
case 'v': /* deprecated */
debug = 1;
break;
case 'D':
daemon_mode = 1;
break;
case 'P':
portcheck = 0;
break;
case 'h': /* deprecated */
break;
case 'l':
logging++; /* > 1 == extra logging */
break;
case 'm':
minuid = strtonum(optarg, 0, UINT_MAX, &errstr);
if (errstr) {
syslog(LOG_ERR,
"%s is a bad value for -m, aborting",
optarg);
exit(2);
}
break;
case 'M':
multihome = 1;
break;
case 'n':
anon_ok = 0;
break;
case 'S':
stats = 1;
break;
case 't':
timeout = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr) {
syslog(LOG_ERR,
"%s is a bad value for -t, aborting",
optarg);
exit(2);
}
if (maxtimeout < timeout)
maxtimeout = timeout;
break;
case 'T':
maxtimeout = strtonum(optarg, 0, INT_MAX,
&errstr);
if (errstr) {
syslog(LOG_ERR,
"%s is a bad value for -T, aborting",
optarg);
exit(2);
}
if (timeout > maxtimeout)
timeout = maxtimeout;
break;
case 'u':
{
long val = 0;
char *p;
umaskchange = 0;
val = strtol(optarg, &p, 8);
if (*optarg == '\0' || *p != '\0' || val < 0 ||
(val & ~ACCESSPERMS)) {
syslog(LOG_ERR,
"%s is a bad value for -u, aborting",
optarg);
exit(2);
}
defumask = val;
break;
}
case 'U':
doutmp = 1;
break;
case 'W':
nowtmp = 1;
break;
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
default:
usage();
break;
}
}
if (nowtmp && doutmp) {
syslog(LOG_ERR, "options 'U' and 'W' are mutually exclusive");
exit(1);
}
(void) freopen(_PATH_DEVNULL, "w", stderr);
/*
* 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);
if (getpwnam(FTPD_PRIVSEP_USER) == NULL) {
syslog(LOG_ERR, "privilege separation user %s not found",
FTPD_PRIVSEP_USER);
exit(1);
}
endpwent();
if (daemon_mode) {
int *fds, fd;
struct pollfd *pfds;
struct addrinfo hints, *res, *res0;
nfds_t n, i;
/*
* Detach from parent.
*/
if (daemon(1, 1) == -1) {
syslog(LOG_ERR, "failed to become a daemon");
exit(1);
}
sa.sa_handler = reapchild;
(void) sigaction(SIGCHLD, &sa, NULL);
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
error = getaddrinfo(NULL, "ftp", &hints, &res0);
if (error) {
syslog(LOG_ERR, "%s", gai_strerror(error));
exit(1);
}
n = 0;
for (res = res0; res; res = res->ai_next)
n++;
fds = calloc(n, sizeof(int));
pfds = calloc(n, sizeof(struct pollfd));
if (!fds || !pfds) {
syslog(LOG_ERR, "%s", strerror(errno));
exit(1);
}
/*
* Open sockets, bind it to the FTP port, and start
* listening.
*/
n = 0;
for (res = res0; res; res = res->ai_next) {
fds[n] = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (fds[n] == -1)
continue;
if (setsockopt(fds[n], SOL_SOCKET, SO_KEEPALIVE,
&on, sizeof(on)) == -1) {
close(fds[n]);
fds[n] = -1;
continue;
}
if (setsockopt(fds[n], SOL_SOCKET, SO_REUSEADDR,
&on, sizeof(on)) == -1) {
close(fds[n]);
fds[n] = -1;
continue;
}
if (bind(fds[n], res->ai_addr, res->ai_addrlen) == -1) {
close(fds[n]);
fds[n] = -1;
continue;
}
if (listen(fds[n], 32) == -1) {
close(fds[n]);
fds[n] = -1;
continue;
}
pfds[n].fd = fds[n];
pfds[n].events = POLLIN;
n++;
}
freeaddrinfo(res0);
if (n == 0) {
syslog(LOG_ERR, "could not open control socket");
exit(1);
}
/*
* Loop forever accepting connection requests and forking off
* children to handle them.
*/
while (1) {
if (poll(pfds, n, INFTIM) == -1) {
if (errno == EINTR)
continue;
syslog(LOG_ERR, "poll: %m");
exit(1);
}
for (i = 0; i < n; i++)
if (pfds[i].revents & POLLIN) {
addrlen = sizeof(his_addr);
fd = accept(pfds[i].fd,
(struct sockaddr *)&his_addr,
&addrlen);
if (fd != -1) {
if (fork() == 0)
goto child;
close(fd);
}
}
}
child:
/* child */
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
for (i = 0; i < n; i++)
close(fds[i]);
} else {
addrlen = sizeof(his_addr);
if (getpeername(0, (struct sockaddr *)&his_addr,
&addrlen) == -1) {
/* syslog(LOG_ERR, "getpeername (%s): %m", argv[0]); */
exit(1);
}
}
/* set this here so klogin can use it... */
(void)snprintf(ttyline, sizeof(ttyline), "ftp%ld", (long)getpid());
set_slave_signals();
addrlen = sizeof(ctrl_addr);
if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) == -1) {
syslog(LOG_ERR, "getsockname: %m");
exit(1);
}
if (his_addr.su_family == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr)) {
syslog(LOG_WARNING,
"Connection from IPv4 mapped address is not supported.");
reply(530, "System not available.");
exit(1);
}
tos = IPTOS_LOWDELAY;
switch (his_addr.su_family) {
case AF_INET:
if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos,
sizeof(int)) == -1)
syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
break;
case AF_INET6:
if (setsockopt(0, IPPROTO_IPV6, IPV6_TCLASS, &tos,
sizeof(int)) == -1)
syslog(LOG_WARNING, "setsockopt (IPV6_TCLASS): %m");
break;
}
data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1);
/* Try to handle urgent data inline */
if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) == -1)
syslog(LOG_ERR, "setsockopt: %m");
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 ((fp = fopen(_PATH_NOLOGIN, "r")) != NULL) {
while (fgets(line, sizeof(line), fp) != NULL) {
line[strcspn(line, "\n")] = '\0';
lreply(530, "%s", line);
}
(void) fclose(fp);
reply(530, "System not available.");
exit(0);
}
if ((fp = fopen(_PATH_FTPWELCOME, "r")) != NULL) {
while (fgets(line, sizeof(line), fp) != NULL) {
line[strcspn(line, "\n")] = '\0';
lreply(220, "%s", line);
}
(void) fclose(fp);
/* reply(220,) must follow */
}
(void) gethostname(hostname, sizeof(hostname));
/* Make sure hostname is fully qualified. */
hp = gethostbyname(hostname);
if (hp != NULL)
strlcpy(hostname, hp->h_name, sizeof(hostname));
if (multihome) {
error = getnameinfo((struct sockaddr *)&ctrl_addr,
ctrl_addr.su_len, dhostname, sizeof(dhostname), NULL, 0, 0);
}
if (error != 0)
reply(220, "FTP server ready.");
else
reply(220, "%s FTP server ready.",
(multihome ? dhostname : hostname));
monitor_init();
for (;;)
(void) yyparse();
/* NOTREACHED */
}
/*
* Signal handlers.
*/
static void
lostconn(int signo)
{
struct syslog_data sdata = SYSLOG_DATA_INIT;
sdata.log_fac = LOG_FTP;
if (debug)
syslog_r(LOG_DEBUG, &sdata, "lost connection");
dologout(1);
}
static void
sigquit(int signo)
{
struct syslog_data sdata = SYSLOG_DATA_INIT;
sdata.log_fac = LOG_FTP;
syslog_r(LOG_DEBUG, &sdata, "got signal %s", sys_signame[signo]);
dologout(1);
}
/*
* 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).
*/
static struct passwd *
sgetpwnam(const char *name, struct passwd *pw)
{
static struct passwd *save;
struct passwd *old;
if (pw == NULL && (pw = getpwnam(name)) == NULL)
return (NULL);
old = save;
save = pw_dup(pw);
if (save == NULL) {
perror_reply(421, "Local resource failure: malloc");
dologout(1);
/* NOTREACHED */
}
if (old) {
explicit_bzero(old->pw_passwd, strlen(old->pw_passwd));
free(old);
}
return (save);
}
static int login_attempts; /* number of failed login attempts */
static int askpasswd; /* had user command, ask for passwd */
static char curname[LOGIN_NAME_MAX]; /* 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)
{
char *cp, *shell, *style, *host;
char *class = NULL;
if (logged_in) {
kill_slave("user already logged in");
end_login();
}
/* Close session from previous user if there was one. */
if (as) {
auth_close(as);
as = NULL;
}
if (lc) {
login_close(lc);
lc = NULL;
}
if ((style = strchr(name, ':')) != NULL)
*style++ = 0;
guest = 0;
askpasswd = 0;
host = multihome ? dhostname : hostname;
if (anon_ok &&
(strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0)) {
if (checkuser(_PATH_FTPUSERS, "ftp") ||
checkuser(_PATH_FTPUSERS, "anonymous"))
reply(530, "User %s access denied.", name);
else if ((pw = sgetpwnam("ftp", NULL)) != NULL) {
if ((lc = login_getclass(pw->pw_class)) == NULL ||
(as = auth_open()) == NULL ||
auth_setpwd(as, pw) != 0 ||
auth_setoption(as, "FTPD_HOST", host) < 0) {
if (as) {
auth_close(as);
as = NULL;
}
if (lc) {
login_close(lc);
lc = NULL;
}
reply(421, "Local resource failure");
return;
}
guest = 1;
askpasswd = 1;
reply(331,
"Guest login ok, send your email address as password.");
} else
reply(530, "User %s unknown.", name);
if (!askpasswd && logging)
syslog(LOG_NOTICE,
"ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost);
return;
}
shell = _PATH_BSHELL;
if ((pw = sgetpwnam(name, NULL))) {
class = pw->pw_class;
if (pw->pw_shell != NULL && *pw->pw_shell != '\0')
shell = pw->pw_shell;
while ((cp = getusershell()) != NULL)
if (strcmp(cp, shell) == 0)
break;
shell = cp;
endusershell();
}
/* Get login class; if invalid style treat like unknown user. */
lc = login_getclass(class);
if (lc && (style = login_getstyle(lc, style, "auth-ftp")) == NULL) {
login_close(lc);
lc = NULL;
pw = NULL;
}
/* Do pre-authentication setup. */
if (lc && ((as = auth_open()) == NULL ||
(pw != NULL && auth_setpwd(as, pw) != 0) ||
auth_setitem(as, AUTHV_STYLE, style) < 0 ||
auth_setitem(as, AUTHV_NAME, name) < 0 ||
auth_setitem(as, AUTHV_CLASS, class) < 0 ||
auth_setoption(as, "login", "yes") < 0 ||
auth_setoption(as, "notickets", "yes") < 0 ||
auth_setoption(as, "FTPD_HOST", host) < 0)) {
if (as) {
auth_close(as);
as = NULL;
}
login_close(lc);
lc = NULL;
reply(421, "Local resource failure");
return;
}
if (logging)
strlcpy(curname, name, sizeof(curname));
dochroot = (lc && login_getcapbool(lc, "ftp-chroot", 0)) ||
checkuser(_PATH_FTPCHROOT, name);
if (anon_only && !dochroot) {
reply(530, "User %s access denied.", name);
return;
}
if (pw) {
if (pw->pw_uid < minuid) {
reply(530, "User %s access denied.", name);
if (logging)
syslog(LOG_NOTICE,
"FTP LOGIN REFUSED FROM %s, %s (UID))",
remotehost, name);
return;
}
if ((!shell && !dochroot) || checkuser(_PATH_FTPUSERS, name)) {
reply(530, "User %s access denied.", name);
if (logging)
syslog(LOG_NOTICE,
"FTP LOGIN REFUSED FROM %s, %s",
remotehost, name);
pw = NULL;
return;
}
}
if (as != NULL && (cp = auth_challenge(as)) != NULL)
reply(331, "%s", cp);
else
reply(331, "Password required for %s.", name);
askpasswd = 1;
/*
* Delay before reading passwd after first failed
* attempt to slow down passwd-guessing programs.
*/
if (login_attempts)
sleep((unsigned) login_attempts);
}
/*
* Check if a user is in the file "fname"
*/
static int
checkuser(char *fname, const char *name)
{
FILE *fp;
int found = 0;
char *p, line[BUFSIZ];
if ((fp = fopen(fname, "r")) != NULL) {
while (fgets(line, sizeof(line), fp) != NULL)
if ((p = strchr(line, '\n')) != NULL) {
*p = '\0';
if (line[0] == '#')
continue;
if (strcmp(line, name) == 0) {
found = 1;
break;
}
}
(void) fclose(fp);
}
return (found);
}
/*
* Terminate login as previous user, if any, resetting state;
* used when USER command is given or login fails.
*/
static void
end_login(void)
{
sigprocmask (SIG_BLOCK, &allsigs, NULL);
if (logged_in) {
if (!nowtmp)
ftpdlogwtmp(ttyline, "", "");
if (doutmp)
ftpd_logout(utmp.ut_line);
}
reply(530, "Please reconnect to work as another user");
_exit(0);
}
enum auth_ret
pass(char *passwd)
{
int authok;
unsigned int flags;
FILE *fp;
static char homedir[PATH_MAX];
char *motd, *dir, rootdir[PATH_MAX];
size_t sz_pw_dir;
if (logged_in || askpasswd == 0) {
reply(503, "Login with USER first.");
return (AUTH_FAILED);
}
askpasswd = 0;
if (!guest) { /* "ftp" is only account allowed no password */
authok = 0;
if (pw == NULL || pw->pw_passwd[0] == '\0') {
useconds_t us;
/* Sleep between 1 and 3 seconds to emulate a crypt. */
us = arc4random_uniform(3000000);
usleep(us);
if (as != NULL) {
auth_close(as);
as = NULL;
}
} else {
authok = auth_userresponse(as, passwd, 0);
as = NULL;
}
if (authok == 0) {
reply(530, "Login incorrect.");
if (logging)
syslog(LOG_NOTICE,
"FTP LOGIN FAILED FROM %s, %s",
remotehost, curname);
pw = NULL;
if (login_attempts++ >= 5) {
syslog(LOG_NOTICE,
"repeated login failures from %s",
remotehost);
kill_slave("repeated login failures");
_exit(0);
}
return (AUTH_FAILED);
}
} else if (lc != NULL) {
/* Save anonymous' password. */
free(guestpw);
guestpw = strdup(passwd);
if (guestpw == NULL) {
kill_slave("out of mem");
fatal("Out of memory.");
}
authok = auth_approval(as, lc, pw->pw_name, "ftp");
auth_close(as);
as = NULL;
if (authok == 0) {
syslog(LOG_INFO|LOG_AUTH,
"FTP LOGIN FAILED (HOST) as %s: approval failure.",
pw->pw_name);
reply(530, "Approval failure.");
kill_slave("approval failure");
_exit(0);
}
} else {
syslog(LOG_INFO|LOG_AUTH,
"FTP LOGIN CLASS %s MISSING for %s: approval failure.",
pw->pw_class, pw->pw_name);
reply(530, "Permission denied.");
kill_slave("permission denied");
_exit(0);
}
if (monitor_post_auth() == 1) {
/* Post-auth monitor process */
logged_in = 1;
return (AUTH_MONITOR);
}
login_attempts = 0; /* this time successful */
/* set umask via setusercontext() unless -u flag was given. */
flags = LOGIN_SETGROUP|LOGIN_SETPRIORITY|LOGIN_SETRESOURCES;
if (umaskchange)
flags |= LOGIN_SETUMASK;
else
(void) umask(defumask);
if (setusercontext(lc, pw, 0, flags) != 0) {
perror_reply(421, "Local resource failure: setusercontext");
syslog(LOG_NOTICE, "setusercontext: %m");
dologout(1);
/* NOTREACHED */
}
/* open wtmp before chroot */
if (!nowtmp)
ftpdlogwtmp(ttyline, pw->pw_name, remotehost);
/* open utmp before chroot */
if (doutmp) {
memset(&utmp, 0, sizeof(utmp));
(void)time(&utmp.ut_time);
(void)strncpy(utmp.ut_name, pw->pw_name, sizeof(utmp.ut_name));
(void)strncpy(utmp.ut_host, remotehost, sizeof(utmp.ut_host));
(void)strncpy(utmp.ut_line, ttyline, sizeof(utmp.ut_line));
ftpd_login(&utmp);
}
/* open stats file before chroot */
if (guest && (stats == 1) && (statfd < 0))
if ((statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND)) == -1)
stats = 0;
logged_in = 1;
if ((dir = login_getcapstr(lc, "ftp-dir", NULL, NULL))) {
char *newdir;
newdir = copy_dir(dir, pw);
if (newdir == NULL) {
perror_reply(421, "Local resource failure: malloc");
dologout(1);
/* NOTREACHED */
}
pw->pw_dir = newdir;
pw = sgetpwnam(NULL, pw);
free(dir);
free(newdir);
}
/* make sure pw->pw_dir is big enough to hold "/" */
sz_pw_dir = strlen(pw->pw_dir) + 1;
if (sz_pw_dir < 2) {
pw->pw_dir = "/";
pw = sgetpwnam(NULL, pw);
sz_pw_dir = 2;
}
if (guest || dochroot) {
if (multihome && guest) {