/
mod_auth.c
4272 lines (3349 loc) · 118 KB
/
mod_auth.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
/*
* ProFTPD - FTP server daemon
* Copyright (c) 1997, 1998 Public Flood Software
* Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
* Copyright (c) 2001-2017 The ProFTPD Project team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
* and other respective copyright holders give permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
/* Authentication module for ProFTPD */
#include "conf.h"
#include "privs.h"
#ifdef HAVE_USERSEC_H
# include <usersec.h>
#endif
#ifdef HAVE_SYS_AUDIT_H
# include <sys/audit.h>
#endif
extern pid_t mpid;
module auth_module;
#ifdef PR_USE_LASTLOG
static unsigned char lastlog = FALSE;
#endif /* PR_USE_LASTLOG */
static unsigned char mkhome = FALSE;
static unsigned char authenticated_without_pass = FALSE;
static int TimeoutLogin = PR_TUNABLE_TIMEOUTLOGIN;
static int logged_in = FALSE;
static int auth_anon_allow_robots = FALSE;
static int auth_anon_allow_robots_enabled = FALSE;
static int auth_client_connected = FALSE;
static unsigned int auth_tries = 0;
static char *auth_pass_resp_code = R_230;
static pr_fh_t *displaylogin_fh = NULL;
static int TimeoutSession = 0;
static int saw_first_user_cmd = FALSE;
static const char *timing_channel = "timing";
static int auth_count_scoreboard(cmd_rec *, const char *);
static int auth_scan_scoreboard(void);
static int auth_sess_init(void);
/* auth_cmd_chk_cb() is hooked into the main server's auth_hook function,
* so that we can deny all commands until authentication is complete.
*
* Note: Once this function returns true (i.e. client has authenticated),
* it will ALWAYS return true. At least until REIN is implemented. Thus
* we have a flag for such a situation, to save on redundant lookups for
* the "authenticated" record.
*/
static int auth_have_authenticated = FALSE;
static int auth_cmd_chk_cb(cmd_rec *cmd) {
if (auth_have_authenticated == FALSE) {
unsigned char *authd;
authd = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
if (authd == NULL ||
*authd == FALSE) {
pr_response_send(R_530, _("Please login with USER and PASS"));
return FALSE;
}
auth_have_authenticated = TRUE;
}
return TRUE;
}
static int auth_login_timeout_cb(CALLBACK_FRAME) {
pr_response_send_async(R_421,
_("Login timeout (%d %s): closing control connection"), TimeoutLogin,
TimeoutLogin != 1 ? "seconds" : "second");
/* It's possible that any listeners of this event might terminate the
* session process themselves (e.g. mod_ban). So write out that the
* TimeoutLogin has been exceeded to the log here, in addition to the
* scheduled session exit message.
*/
pr_log_pri(PR_LOG_INFO, "%s", "Login timeout exceeded, disconnected");
pr_event_generate("core.timeout-login", NULL);
pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_TIMEOUT,
"TimeoutLogin");
/* Do not restart the timer (should never be reached). */
return 0;
}
static int auth_session_timeout_cb(CALLBACK_FRAME) {
pr_event_generate("core.timeout-session", NULL);
pr_response_send_async(R_421,
_("Session Timeout (%d seconds): closing control connection"),
TimeoutSession);
pr_log_pri(PR_LOG_INFO, "%s", "FTP session timed out, disconnected");
pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_TIMEOUT,
"TimeoutSession");
/* no need to restart the timer -- session's over */
return 0;
}
/* Event listeners
*/
static void auth_exit_ev(const void *event_data, void *user_data) {
pr_auth_cache_clear();
/* Close the scoreboard descriptor that we opened. */
(void) pr_close_scoreboard(FALSE);
}
static void auth_sess_reinit_ev(const void *event_data, void *user_data) {
int res;
/* A HOST command changed the main_server pointer, reinitialize ourselves. */
pr_event_unregister(&auth_module, "core.exit", auth_exit_ev);
pr_event_unregister(&auth_module, "core.session-reinit", auth_sess_reinit_ev);
pr_timer_remove(PR_TIMER_LOGIN, &auth_module);
/* Reset the CreateHome setting. */
mkhome = FALSE;
/* Reset any MaxPasswordSize setting. */
(void) pr_auth_set_max_password_len(session.pool, 0);
#if defined(PR_USE_LASTLOG)
lastlog = FALSE;
#endif /* PR_USE_LASTLOG */
mkhome = FALSE;
res = auth_sess_init();
if (res < 0) {
pr_session_disconnect(&auth_module,
PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
}
}
/* Initialization functions
*/
static int auth_init(void) {
/* Add the commands handled by this module to the HELP list. */
pr_help_add(C_USER, _("<sp> username"), TRUE);
pr_help_add(C_PASS, _("<sp> password"), TRUE);
pr_help_add(C_ACCT, _("is not implemented"), FALSE);
pr_help_add(C_REIN, _("is not implemented"), FALSE);
/* By default, enable auth checking */
set_auth_check(auth_cmd_chk_cb);
return 0;
}
static int auth_sess_init(void) {
config_rec *c = NULL;
unsigned char *tmp = NULL;
pr_event_register(&auth_module, "core.session-reinit", auth_sess_reinit_ev,
NULL);
/* Check for any MaxPasswordSize. */
c = find_config(main_server->conf, CONF_PARAM, "MaxPasswordSize", FALSE);
if (c != NULL) {
size_t len;
len = *((size_t *) c->argv[0]);
(void) pr_auth_set_max_password_len(session.pool, len);
}
/* Check for a server-specific TimeoutLogin */
c = find_config(main_server->conf, CONF_PARAM, "TimeoutLogin", FALSE);
if (c != NULL) {
TimeoutLogin = *((int *) c->argv[0]);
}
/* Start the login timer */
if (TimeoutLogin) {
pr_timer_remove(PR_TIMER_LOGIN, &auth_module);
pr_timer_add(TimeoutLogin, PR_TIMER_LOGIN, &auth_module,
auth_login_timeout_cb, "TimeoutLogin");
}
if (auth_client_connected == FALSE) {
int res = 0;
PRIVS_ROOT
res = pr_open_scoreboard(O_RDWR);
PRIVS_RELINQUISH
if (res < 0) {
switch (res) {
case PR_SCORE_ERR_BAD_MAGIC:
pr_log_debug(DEBUG0, "error opening scoreboard: bad/corrupted file");
break;
case PR_SCORE_ERR_OLDER_VERSION:
pr_log_debug(DEBUG0,
"error opening scoreboard: bad version (too old)");
break;
case PR_SCORE_ERR_NEWER_VERSION:
pr_log_debug(DEBUG0,
"error opening scoreboard: bad version (too new)");
break;
default:
pr_log_debug(DEBUG0, "error opening scoreboard: %s", strerror(errno));
break;
}
}
}
pr_event_register(&auth_module, "core.exit", auth_exit_ev, NULL);
if (auth_client_connected == FALSE) {
/* Create an entry in the scoreboard for this session, if we don't already
* have one.
*/
if (pr_scoreboard_entry_get(PR_SCORE_CLIENT_ADDR) == NULL) {
if (pr_scoreboard_entry_add() < 0) {
pr_log_pri(PR_LOG_NOTICE, "notice: unable to add scoreboard entry: %s",
strerror(errno));
}
pr_scoreboard_entry_update(session.pid,
PR_SCORE_USER, "(none)",
PR_SCORE_SERVER_PORT, main_server->ServerPort,
PR_SCORE_SERVER_ADDR, session.c->local_addr, session.c->local_port,
PR_SCORE_SERVER_LABEL, main_server->ServerName,
PR_SCORE_CLIENT_ADDR, session.c->remote_addr,
PR_SCORE_CLIENT_NAME, session.c->remote_name,
PR_SCORE_CLASS, session.conn_class ? session.conn_class->cls_name : "",
PR_SCORE_PROTOCOL, "ftp",
PR_SCORE_BEGIN_SESSION, time(NULL),
NULL);
}
} else {
/* We're probably handling a HOST comand, and the server changed; just
* update the SERVER_LABEL field.
*/
pr_scoreboard_entry_update(session.pid,
PR_SCORE_SERVER_LABEL, main_server->ServerName,
NULL);
}
/* Should we create the home for a user, if they don't have one? */
tmp = get_param_ptr(main_server->conf, "CreateHome", FALSE);
if (tmp != NULL &&
*tmp == TRUE) {
mkhome = TRUE;
} else {
mkhome = FALSE;
}
#ifdef PR_USE_LASTLOG
/* Use the lastlog file, if supported and requested. */
tmp = get_param_ptr(main_server->conf, "UseLastlog", FALSE);
if (tmp &&
*tmp == TRUE) {
lastlog = TRUE;
} else {
lastlog = FALSE;
}
#endif /* PR_USE_LASTLOG */
/* Scan the scoreboard now, in order to tally up certain values for
* substituting in any of the Display* file variables. This function
* also performs the MaxConnectionsPerHost enforcement.
*/
auth_scan_scoreboard();
auth_client_connected = TRUE;
return 0;
}
static int do_auth(pool *p, xaset_t *conf, const char *u, char *pw) {
char *cpw = NULL;
config_rec *c;
if (conf != NULL) {
c = find_config(conf, CONF_PARAM, "UserPassword", FALSE);
while (c != NULL) {
pr_signals_handle();
if (strcmp(c->argv[0], u) == 0) {
cpw = (char *) c->argv[1];
break;
}
c = find_config_next(c, c->next, CONF_PARAM, "UserPassword", FALSE);
}
}
if (cpw != NULL) {
if (pr_auth_getpwnam(p, u) == NULL) {
int xerrno = errno;
if (xerrno == ENOENT) {
pr_log_pri(PR_LOG_NOTICE, "no such user '%s'", u);
}
errno = xerrno;
return PR_AUTH_NOPWD;
}
return pr_auth_check(p, cpw, u, pw);
}
return pr_auth_authenticate(p, u, pw);
}
/* Command handlers
*/
static void login_failed(pool *p, const char *user) {
#ifdef HAVE_LOGINFAILED
const char *host, *sess_ttyname;
int res, xerrno;
host = pr_netaddr_get_dnsstr(session.c->remote_addr);
sess_ttyname = pr_session_get_ttyname(p);
PRIVS_ROOT
res = loginfailed((char *) user, (char *) host, (char *) sess_ttyname,
AUDIT_FAIL);
xerrno = errno;
PRIVS_RELINQUISH
if (res < 0) {
pr_trace_msg("auth", 3, "AIX loginfailed() error for user '%s', "
"host '%s', tty '%s', reason %d: %s", user, host, sess_ttyname,
AUDIT_FAIL, strerror(errno));
}
#endif /* HAVE_LOGINFAILED */
}
MODRET auth_err_pass(cmd_rec *cmd) {
const char *user;
user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
if (user != NULL) {
login_failed(cmd->tmp_pool, user);
}
/* Remove the stashed original USER name here in a LOG_CMD_ERR handler, so
* that other modules, who may want to lookup the original USER parameter on
* a failed login in an earlier command handler phase, have a chance to do
* so. This removal of the USER parameter on failure was happening directly
* in the CMD handler previously, thus preventing POST_CMD_ERR handlers from
* using USER.
*/
pr_table_remove(session.notes, "mod_auth.orig-user", NULL);
return PR_HANDLED(cmd);
}
MODRET auth_log_pass(cmd_rec *cmd) {
/* Only log, to the syslog, that the login has succeeded here, where we
* know that the login has definitely succeeded.
*/
pr_log_auth(PR_LOG_INFO, "%s %s: Login successful.",
(session.anon_config != NULL) ? "ANON" : C_USER, session.user);
if (cmd->arg != NULL) {
size_t passwd_len;
/* And scrub the memory holding the password sent by the client, for
* safety/security.
*/
passwd_len = strlen(cmd->arg);
pr_memscrub(cmd->arg, passwd_len);
}
return PR_DECLINED(cmd);
}
static void login_succeeded(pool *p, const char *user) {
#ifdef HAVE_LOGINSUCCESS
const char *host, *sess_ttyname;
char *msg = NULL;
int res, xerrno;
host = pr_netaddr_get_dnsstr(session.c->remote_addr);
sess_ttyname = pr_session_get_ttyname(p);
PRIVS_ROOT
res = loginsuccess((char *) user, (char *) host, (char *) sess_ttyname, &msg);
xerrno = errno;
PRIVS_RELINQUISH
if (res == 0) {
if (msg != NULL) {
pr_trace_msg("auth", 14, "AIX loginsuccess() report: %s", msg);
}
} else {
pr_trace_msg("auth", 3, "AIX loginsuccess() error for user '%s', "
"host '%s', tty '%s': %s", user, host, sess_ttyname, strerror(errno));
}
if (msg != NULL) {
free(msg);
}
#endif /* HAVE_LOGINSUCCESS */
}
MODRET auth_post_pass(cmd_rec *cmd) {
config_rec *c = NULL;
const char *grantmsg = NULL, *user;
unsigned int ctxt_precedence = 0;
unsigned char have_user_timeout, have_group_timeout, have_class_timeout,
have_all_timeout, *root_revoke = NULL, *authenticated;
struct stat st;
/* Was there a precending USER command? Was the client successfully
* authenticated?
*/
authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
/* Clear the list of auth-only modules. */
pr_auth_clear_auth_only_modules();
if (authenticated != NULL &&
*authenticated == TRUE) {
/* At this point, we can look up the Protocols config if the client
* has been authenticated, which may have been tweaked via mod_ifsession's
* user/group/class-specific sections.
*/
c = find_config(main_server->conf, CONF_PARAM, "Protocols", FALSE);
if (c != NULL) {
register unsigned int i;
array_header *protocols;
char **elts;
const char *protocol;
protocols = c->argv[0];
elts = protocols->elts;
protocol = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
/* We only want to check for 'ftp' in the configured Protocols list
* if a) a RFC2228 mechanism (e.g. SSL or GSS) is not in use, and
* b) an SSH protocol is not in use.
*/
if (session.rfc2228_mech == NULL &&
strncmp(protocol, "SSH2", 5) != 0) {
int allow_ftp = FALSE;
for (i = 0; i < protocols->nelts; i++) {
char *proto;
proto = elts[i];
if (proto != NULL) {
if (strncasecmp(proto, "ftp", 4) == 0) {
allow_ftp = TRUE;
break;
}
}
}
if (!allow_ftp) {
pr_log_debug(DEBUG0, "%s", "ftp protocol denied by Protocols config");
pr_response_send(R_530, "%s", _("Login incorrect."));
pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
"Denied by Protocols setting");
}
}
}
}
user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
/* Count up various quantities in the scoreboard, checking them against
* the Max* limits to see if the session should be barred from going
* any further.
*/
auth_count_scoreboard(cmd, session.user);
/* Check for dynamic configuration. This check needs to be after the
* setting of any possible anon_config, as that context may be allowed
* or denied .ftpaccess-parsing separately from the containing server.
*/
if (pr_fsio_stat(session.cwd, &st) != -1)
build_dyn_config(cmd->tmp_pool, session.cwd, &st, TRUE);
have_user_timeout = have_group_timeout = have_class_timeout =
have_all_timeout = FALSE;
c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TimeoutSession", FALSE);
while (c != NULL) {
pr_signals_handle();
if (c->argc == 3) {
if (strncmp(c->argv[1], "user", 5) == 0) {
if (pr_expr_eval_user_or((char **) &c->argv[2]) == TRUE) {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence. */
ctxt_precedence = *((unsigned int *) c->argv[1]);
TimeoutSession = *((int *) c->argv[0]);
have_group_timeout = have_class_timeout = have_all_timeout = FALSE;
have_user_timeout = TRUE;
}
}
} else if (strncmp(c->argv[1], "group", 6) == 0) {
if (pr_expr_eval_group_and((char **) &c->argv[2]) == TRUE) {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence. */
ctxt_precedence = *((unsigned int *) c->argv[1]);
TimeoutSession = *((int *) c->argv[0]);
have_user_timeout = have_class_timeout = have_all_timeout = FALSE;
have_group_timeout = TRUE;
}
}
} else if (strncmp(c->argv[1], "class", 6) == 0) {
if (session.conn_class != NULL &&
strcmp(session.conn_class->cls_name, c->argv[2]) == 0) {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence. */
ctxt_precedence = *((unsigned int *) c->argv[1]);
TimeoutSession = *((int *) c->argv[0]);
have_user_timeout = have_group_timeout = have_all_timeout = FALSE;
have_class_timeout = TRUE;
}
}
}
} else {
if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
/* Set the context precedence. */
ctxt_precedence = *((unsigned int *) c->argv[1]);
TimeoutSession = *((int *) c->argv[0]);
have_user_timeout = have_group_timeout = have_class_timeout = FALSE;
have_all_timeout = TRUE;
}
}
c = find_config_next(c, c->next, CONF_PARAM, "TimeoutSession", FALSE);
}
/* If configured, start a session timer. The timer ID value for
* session timers will not be #defined, as I think that is a bad approach.
* A better mechanism would be to use the random timer ID generation, and
* store the returned ID in order to later remove the timer.
*/
if (have_user_timeout || have_group_timeout ||
have_class_timeout || have_all_timeout) {
pr_log_debug(DEBUG4, "setting TimeoutSession of %d seconds for current %s",
TimeoutSession,
have_user_timeout ? "user" : have_group_timeout ? "group" :
have_class_timeout ? "class" : "all");
pr_timer_add(TimeoutSession, PR_TIMER_SESSION, &auth_module,
auth_session_timeout_cb, "TimeoutSession");
}
/* Handle a DisplayLogin file. */
if (displaylogin_fh) {
if (!(session.sf_flags & SF_ANON)) {
if (pr_display_fh(displaylogin_fh, NULL, auth_pass_resp_code, 0) < 0) {
pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
displaylogin_fh->fh_path, strerror(errno));
}
pr_fsio_close(displaylogin_fh);
displaylogin_fh = NULL;
} else {
/* We're an <Anonymous> login, but there was a previous DisplayLogin
* configured which was picked up earlier. Close that filehandle,
* and look for a new one.
*/
char *displaylogin;
pr_fsio_close(displaylogin_fh);
displaylogin_fh = NULL;
displaylogin = get_param_ptr(TOPLEVEL_CONF, "DisplayLogin", FALSE);
if (displaylogin) {
if (pr_display_file(displaylogin, NULL, auth_pass_resp_code, 0) < 0) {
pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
displaylogin, strerror(errno));
}
}
}
} else {
char *displaylogin = get_param_ptr(TOPLEVEL_CONF, "DisplayLogin", FALSE);
if (displaylogin) {
if (pr_display_file(displaylogin, NULL, auth_pass_resp_code, 0) < 0) {
pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
displaylogin, strerror(errno));
}
}
}
grantmsg = get_param_ptr(TOPLEVEL_CONF, "AccessGrantMsg", FALSE);
if (grantmsg == NULL) {
/* Append the final greeting lines. */
if (session.sf_flags & SF_ANON) {
pr_response_add(auth_pass_resp_code, "%s",
_("Anonymous access granted, restrictions apply"));
} else {
pr_response_add(auth_pass_resp_code, _("User %s logged in"), user);
}
} else {
/* Handle any AccessGrantMsg directive. */
grantmsg = sreplace(cmd->tmp_pool, grantmsg, "%u", user, NULL);
pr_response_add(auth_pass_resp_code, "%s", grantmsg);
}
login_succeeded(cmd->tmp_pool, user);
/* A RootRevoke value of 0 indicates 'false', 1 indicates 'true', and
* 2 indicates 'NonCompliantActiveTransfer'. We will drop root privs for any
* RootRevoke value greater than 0.
*/
root_revoke = get_param_ptr(TOPLEVEL_CONF, "RootRevoke", FALSE);
if (root_revoke != NULL &&
*root_revoke > 0) {
pr_signals_block();
PRIVS_ROOT
PRIVS_REVOKE
pr_signals_unblock();
/* Disable future attempts at UID/GID manipulation. */
session.disable_id_switching = TRUE;
if (*root_revoke == 1) {
/* If the server's listening port is less than 1024, block PORT
* commands (effectively allowing only passive connections, which is
* not necessarily a Bad Thing). Only log this here -- the blocking
* will need to occur in mod_core's handling of the PORT/EPRT commands.
*/
if (session.c->local_port < 1024) {
pr_log_debug(DEBUG0,
"RootRevoke in effect, active data transfers may not succeed");
}
}
pr_log_debug(DEBUG0, "RootRevoke in effect, dropped root privs");
}
c = find_config(TOPLEVEL_CONF, CONF_PARAM, "AnonAllowRobots", FALSE);
if (c != NULL) {
auth_anon_allow_robots = *((int *) c->argv[0]);
}
return PR_DECLINED(cmd);
}
/* Handle group based authentication, only checked if pw based fails. */
static config_rec *auth_group(pool *p, const char *user, char **group,
char **ournamep, char **anonnamep, char *pass) {
config_rec *c;
char *ourname = NULL, *anonname = NULL;
char **grmem;
struct group *grp;
ourname = get_param_ptr(main_server->conf, "UserName", FALSE);
if (ournamep != NULL &&
ourname != NULL) {
*ournamep = ourname;
}
c = find_config(main_server->conf, CONF_PARAM, "GroupPassword", TRUE);
if (c) do {
grp = pr_auth_getgrnam(p, c->argv[0]);
if (grp == NULL) {
continue;
}
for (grmem = grp->gr_mem; *grmem; grmem++) {
if (strcmp(*grmem, user) == 0) {
if (pr_auth_check(p, c->argv[1], user, pass) == 0) {
break;
}
}
}
if (*grmem) {
if (group != NULL) {
*group = c->argv[0];
}
if (c->parent != NULL) {
c = c->parent;
}
if (c->config_type == CONF_ANON) {
anonname = get_param_ptr(c->subset, "UserName", FALSE);
}
if (anonnamep != NULL) {
*anonnamep = anonname;
}
if (anonnamep != NULL &&
!anonname &&
ourname != NULL) {
*anonnamep = ourname;
}
break;
}
} while((c = find_config_next(c, c->next, CONF_PARAM, "GroupPassword",
TRUE)) != NULL);
return c;
}
/* Determine any applicable chdirs. */
static const char *get_default_chdir(pool *p, xaset_t *conf) {
config_rec *c;
const char *dir = NULL;
c = find_config(conf, CONF_PARAM, "DefaultChdir", FALSE);
while (c != NULL) {
int res;
pr_signals_handle();
/* Check the groups acl */
if (c->argc < 2) {
dir = c->argv[0];
break;
}
res = pr_expr_eval_group_and(((char **) c->argv)+1);
if (res) {
dir = c->argv[0];
break;
}
c = find_config_next(c, c->next, CONF_PARAM, "DefaultChdir", FALSE);
}
/* If the directory is relative, concatenate w/ session.cwd. */
if (dir != NULL &&
*dir != '/' &&
*dir != '~') {
dir = pdircat(p, session.cwd, dir, NULL);
}
/* Check for any expandable variables. */
if (dir != NULL) {
dir = path_subst_uservar(p, &dir);
}
return dir;
}
/* Determine if the user (non-anon) needs a default root dir other than /. */
static int get_default_root(pool *p, int allow_symlinks, const char **root) {
config_rec *c = NULL;
const char *dir = NULL;
int res;
c = find_config(main_server->conf, CONF_PARAM, "DefaultRoot", FALSE);
while (c != NULL) {
pr_signals_handle();
/* Check the groups acl */
if (c->argc < 2) {
dir = c->argv[0];
break;
}
res = pr_expr_eval_group_and(((char **) c->argv)+1);
if (res) {
dir = c->argv[0];
break;
}
c = find_config_next(c, c->next, CONF_PARAM, "DefaultRoot", FALSE);
}
if (dir != NULL) {
const char *new_dir;
/* Check for any expandable variables. */
new_dir = path_subst_uservar(p, &dir);
if (new_dir != NULL) {
dir = new_dir;
}
if (strncmp(dir, "/", 2) == 0) {
dir = NULL;
} else {
char *realdir;
int xerrno = 0;
if (allow_symlinks == FALSE) {
char *path, target_path[PR_TUNABLE_PATH_MAX + 1];
struct stat st;
size_t pathlen;
/* First, deal with any possible interpolation. dir_realpath() will
* do this for us, but dir_realpath() ALSO automatically follows
* symlinks, which is what we do NOT want to do here.
*/
path = pstrdup(p, dir);
if (*path != '/') {
if (*path == '~') {
if (pr_fs_interpolate(dir, target_path,
sizeof(target_path)-1) < 0) {
return -1;
}
path = target_path;
}
}
/* Note: lstat(2) is sensitive to the presence of a trailing slash on
* the path, particularly in the case of a symlink to a directory.
* Thus to get the correct test, we need to remove any trailing slash
* that might be present. Subtle.
*/
pathlen = strlen(path);
if (pathlen > 1 &&
path[pathlen-1] == '/') {
path[pathlen-1] = '\0';
}
pr_fs_clear_cache2(path);
res = pr_fsio_lstat(path, &st);
if (res < 0) {
xerrno = errno;
pr_log_pri(PR_LOG_WARNING, "error: unable to check %s: %s", path,
strerror(xerrno));
errno = xerrno;
return -1;
}
if (S_ISLNK(st.st_mode)) {
pr_log_pri(PR_LOG_WARNING,
"error: DefaultRoot %s is a symlink (denied by AllowChrootSymlinks "
"config)", path);
errno = EPERM;
return -1;
}
}
/* We need to be the final user here so that if the user has their home
* directory with a mode the user proftpd is running (i.e. the User
* directive) as can not traverse down, we can still have the default
* root.
*/
pr_fs_clear_cache2(dir);
PRIVS_USER
realdir = dir_realpath(p, dir);
xerrno = errno;
PRIVS_RELINQUISH
if (realdir) {
dir = realdir;
} else {
/* Try to provide a more informative message. */
char interp_dir[PR_TUNABLE_PATH_MAX + 1];
memset(interp_dir, '\0', sizeof(interp_dir));
(void) pr_fs_interpolate(dir, interp_dir, sizeof(interp_dir)-1);
pr_log_pri(PR_LOG_NOTICE,
"notice: unable to use DefaultRoot '%s' [resolved to '%s']: %s",
dir, interp_dir, strerror(xerrno));
errno = xerrno;
}
}
}
*root = dir;
return 0;
}
static struct passwd *passwd_dup(pool *p, struct passwd *pw) {
struct passwd *npw;
npw = pcalloc(p, sizeof(struct passwd));
npw->pw_name = pstrdup(p, pw->pw_name);
npw->pw_passwd = pstrdup(p, pw->pw_passwd);
npw->pw_uid = pw->pw_uid;
npw->pw_gid = pw->pw_gid;
npw->pw_gecos = pstrdup(p, pw->pw_gecos);
npw->pw_dir = pstrdup(p, pw->pw_dir);
npw->pw_shell = pstrdup(p, pw->pw_shell);
return npw;
}
static void ensure_open_passwd(pool *p) {
/* Make sure pass/group is open. */
pr_auth_setpwent(p);
pr_auth_setgrent(p);
/* On some unices the following is necessary to ensure the files
* are open (BSDI 3.1)
*/
pr_auth_getpwent(p);
pr_auth_getgrent(p);
/* Per Debian bug report:
* https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=717235
* we might want to do another set{pw,gr}ent(), to play better with
* some NSS modules.
*/
pr_auth_setpwent(p);
pr_auth_setgrent(p);
}
/* Next function (the biggie) handles all authentication, setting
* up chroot() jail, etc.
*/
static int setup_env(pool *p, cmd_rec *cmd, const char *user, char *pass) {
struct passwd *pw;
config_rec *c, *tmpc;
const char *defchdir = NULL, *defroot = NULL, *origuser, *sess_ttyname;
char *ourname = NULL, *anonname = NULL, *anongroup = NULL, *ugroup = NULL;
char *xferlog = NULL;
int aclp, i, res = 0, allow_chroot_symlinks = TRUE, showsymlinks;
unsigned char *wtmp_log = NULL, *anon_require_passwd = NULL;
/********************* Authenticate the user here *********************/
session.hide_password = TRUE;
origuser = user;
c = pr_auth_get_anon_config(p, &user, &ourname, &anonname);
if (c != NULL) {
session.anon_config = c;
}
if (user == NULL) {
pr_log_auth(PR_LOG_NOTICE, "USER %s: user is not a UserAlias from %s [%s] "
"to %s:%i", origuser, session.c->remote_name,
pr_netaddr_get_ipstr(session.c->remote_addr),
pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port);