Debian Codegraph @

contrib/mod_ifsession.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_ifsession -- a module supporting conditional
3 * per-user/group/class configuration contexts.
4 * Copyright (c) 2002-2023 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_ifsession, contrib software for proftpd 1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30
31 #define MOD_IFSESSION_VERSION "mod_ifsession/1.3.1"
32
33 /* Make sure the version of proftpd is as necessary. */
34 #if PROFTPD_VERSION_NUMBER < 0x0001030402
35 # error "ProFTPD 1.3.4rc2 or later required"
36 #endif
37
38 #define IFSESS_CLASS_NUMBER 100
39 #define IFSESS_CLASS_TEXT "<IfClass>"
40 #define IFSESS_GROUP_NUMBER 101
41 #define IFSESS_GROUP_TEXT "<IfGroup>"
42 #define IFSESS_USER_NUMBER 102
43 #define IFSESS_USER_TEXT "<IfUser>"
44 #define IFSESS_AUTHN_NUMBER 103
45 #define IFSESS_AUTHN_TEXT "<IfAuthenticated>"
46
47 /* mod_ifsession options */
48 #define IFSESS_OPT_PER_UNAUTHED_USER 0x001
49
50 module ifsession_module;
51
52 static int ifsess_ctx = -1;
53 static int ifsess_merged = FALSE;
54 static unsigned long ifsess_opts = 0UL;
55
56 /* For storing the home directory of user, symlinks resolved. */
57 static const char *ifsess_home_dir = NULL;
58
59 /* For supporting DisplayLogin files in <IfUser>/<IfGroup> sections. */
60 static pr_fh_t *displaylogin_fh = NULL;
61
62 static int ifsess_sess_init(void);
63
64 static const char *trace_channel = "ifsession";
65
66 /* Necessary prototypes */
67 static void ifsess_resolve_dirs(config_rec *);
68 static void ifsess_resolve_server_dirs(server_rec *);
69
70 /* Support routines
71 */
72
73 static void ifsess_remove_param(xaset_t *set, int config_type,
74 const char *name) {
75 config_rec *c = NULL;
76 int lookup_type = -1;
77
78 if (config_type == CONF_DIR) {
79 pr_trace_msg(trace_channel, 9, "removing <Directory %s> config", name);
80 lookup_type = CONF_DIR;
81
82 } else {
83 pr_trace_msg(trace_channel, 9, "removing '%s' config", name);
84 }
85
86 c = find_config(set, lookup_type, name, TRUE);
87 while (c != NULL) {
88 xaset_t *fset;
89 xasetmember_t *member;
90
91 pr_signals_handle();
92
93 fset = c->set;
94 member = (xasetmember_t *) c;
95 xaset_remove(fset, member);
96
97 c = find_config(set, lookup_type, name, TRUE);
98 }
99 }
100
101 static void ifsess_dup_param(pool *dst_pool, xaset_t **dst, config_rec *c,
102 config_rec *parent) {
103 config_rec *dup_c = NULL;
104
105 if (c->config_type == CONF_DIR) {
106 pr_trace_msg(trace_channel, 9, "adding <Directory %s> config", c->name);
107
108 } else if (c->config_type == CONF_LIMIT) {
109 pr_trace_msg(trace_channel, 9, "adding <Limit> config");
110
111 } else {
112 pr_trace_msg(trace_channel, 9, "adding '%s' config", c->name);
113 }
114
115 if (!*dst) {
116 *dst = xaset_create(dst_pool, NULL);
117 }
118
119 dup_c = pr_config_add_set(dst, c->name, PR_CONFIG_FL_INSERT_HEAD);
120 dup_c->config_type = c->config_type;
121 dup_c->flags = c->flags;
122 dup_c->parent = parent;
123 dup_c->argc = c->argc;
124
125 if (c->argc) {
126 void **dst_argv = NULL, **src_argv = NULL;
127 int dst_argc;
128
129 dup_c->argv = pcalloc(dup_c->pool, (c->argc + 1) * sizeof(void *));
130
131 src_argv = c->argv;
132 dst_argv = dup_c->argv;
133 dst_argc = dup_c->argc;
134
135 while (dst_argc--) {
136 *dst_argv++ = *src_argv++;
137 }
138
139 if (dst_argv) {
140 *dst_argv++ = NULL;
141 }
142 }
143
144 if (c->subset) {
145 for (c = (config_rec *) c->subset->xas_list; c; c = c->next) {
146
147 /* If this directive does not allow multiple instances, make sure
148 * it is removed from the destination set first. The "source"
149 * directive then effectively replaces any directive there.
150 *
151 * Note that we only want to do this IF the config is NOT part of
152 * of a <Limit> section.
153 */
154 if (c->parent->config_type != CONF_LIMIT &&
155 c->config_type == CONF_PARAM &&
156 !(c->flags & CF_MERGEDOWN_MULTI) &&
157 !(c->flags & CF_MULTI)) {
158 pr_trace_msg(trace_channel, 15, "removing '%s' config because "
159 "c->flags does not contain MULTI or MERGEDOWN_MULTI", c->name);
160 ifsess_remove_param(dup_c->subset, c->config_type, c->name);
161 }
162
163 ifsess_dup_param(dst_pool, &dup_c->subset, c, dup_c);
164 }
165 }
166 }
167
168 static void ifsess_dup_set(pool *dst_pool, xaset_t *dst, xaset_t *src) {
169 config_rec *c, *next;
170
171 for (c = (config_rec *) src->xas_list; c; c = next) {
172 next = c->next;
173
174 /* Skip the context lists. */
175 if (c->config_type == IFSESS_CLASS_NUMBER ||
176 c->config_type == IFSESS_GROUP_NUMBER ||
177 c->config_type == IFSESS_USER_NUMBER ||
178 c->config_type == IFSESS_AUTHN_NUMBER) {
179 continue;
180 }
181
182 /* If this directive does not allow multiple instances, make sure
183 * it is removed from the destination set first. The "source"
184 * directive then effectively replaces any directive there.
185 *
186 * Note that we only want to do this IF the config is NOT part of
187 * of a <Limit> section.
188 */
189 if (c->parent->config_type != CONF_LIMIT &&
190 c->config_type == CONF_PARAM &&
191 !(c->flags & CF_MERGEDOWN_MULTI) &&
192 !(c->flags & CF_MULTI)) {
193 pr_trace_msg(trace_channel, 15, "removing '%s' config because "
194 "c->flags does not contain MULTI or MERGEDOWN_MULTI", c->name);
195 ifsess_remove_param(dst, c->config_type, c->name);
196 }
197
198 if (c->config_type == CONF_DIR) {
199 pr_trace_msg(trace_channel, 15, "removing old <Directory %s> config "
200 "because new <Directory %s> takes precedence", c->name, c->name);
201 ifsess_remove_param(dst, c->config_type, c->name);
202 }
203
204 ifsess_dup_param(dst_pool, &dst, c, NULL);
205 }
206 }
207
208 /* Similar to dir_interpolate(), except that we are cognizant of being
209 * chrooted, and so try to Do The Right Thing(tm).
210 */
211 static char *ifsess_dir_interpolate(pool *p, const char *path) {
212 char *ret = (char *) path;
213
214 if (ret == NULL) {
215 errno = EINVAL;
216 return NULL;
217 }
218
219 if (*ret == '~') {
220 const char *user;
221 char *interp_dir = NULL, *ptr;
222
223 user = pstrdup(p, ret+1);
224 ptr = strchr(user, '/');
225
226 if (ptr != NULL) {
227 *ptr++ = '\0';
228 }
229
230 if (user[0] == '\0') {
231 user = session.user;
232
233 if (ifsess_home_dir != NULL) {
234 /* We're chrooted; we already know the interpolated path. */
235 interp_dir = (char *) ifsess_home_dir;
236
237 } else if (session.user_homedir != NULL) {
238 /* We are not chrooted, but we do know the user's home directory. */
239 interp_dir = (char *) session.user_homedir;
240 }
241 }
242
243 if (interp_dir == NULL) {
244 struct passwd *pw;
245 struct stat st;
246 size_t interp_dirlen;
247
248 pw = pr_auth_getpwnam(p, user);
249 if (pw == NULL) {
250 errno = ENOENT;
251 return NULL;
252 }
253
254 if (pw->pw_dir == NULL) {
255 errno = EPERM;
256 return NULL;
257 }
258
259 interp_dir = pstrdup(p, pw->pw_dir);
260 interp_dirlen = strlen(interp_dir);
261
262 /* If the given directory is a symlink, follow it. Note that for
263 * proper handling of such paths, we need to ensure that the path does
264 * not end in a slash.
265 */
266 if (interp_dir[interp_dirlen] == '/') {
267 interp_dir[interp_dirlen--] = '\0';
268 }
269
270 if (pr_fsio_lstat(interp_dir, &st) == 0) {
271 if (S_ISLNK(st.st_mode)) {
272 char link_path[PR_TUNABLE_PATH_MAX+1];
273
274 memset(link_path, '\0', sizeof(link_path));
275 if (pr_fs_resolve_path(interp_dir, link_path, sizeof(link_path)-1,
276 FSIO_DIR_CHDIR) < 0) {
277 return NULL;
278 }
279
280 interp_dir = pstrdup(p, link_path);
281 }
282 }
283 }
284
285 ret = pdircat(p, interp_dir, ptr, NULL);
286 }
287
288 return ret;
289 }
290
291 /* Similar to resolve_deferred_dirs(), except that we need to recurse
292 * and resolve ALL <Directory> paths.
293 */
294 static void ifsess_resolve_dir(config_rec *c) {
295 char *interp_dir = NULL, *real_dir = NULL, *orig_name = NULL;
296
297 if (pr_trace_get_level(trace_channel) >= 11) {
298 orig_name = pstrdup(c->pool, c->name);
299 }
300
301 /* Check for any expandable variables. */
302 c->name = (char *) path_subst_uservar(c->pool, (const char **) &c->name);
303
304 /* Handle any '~' interpolation. */
305 interp_dir = ifsess_dir_interpolate(c->pool, c->name);
306 if (interp_dir == NULL) {
307 /* This can happen when the '~' is just that, and does not refer
308 * to any known user.
309 */
310 interp_dir = c->name;
311 }
312
313 real_dir = dir_best_path(c->pool, interp_dir);
314 if (real_dir) {
315 c->name = real_dir;
316
317 } else {
318 real_dir = dir_canonical_path(c->pool, interp_dir);
319 if (real_dir) {
320 c->name = real_dir;
321 }
322 }
323
324 pr_trace_msg(trace_channel, 11,
325 "resolved <Directory %s> to <Directory %s>", orig_name, c->name);
326 }
327
328 void ifsess_resolve_dirs(config_rec *c) {
329 ifsess_resolve_dir(c);
330
331 if (c->subset != NULL) {
332 config_rec *subc;
333
334 for (subc = (config_rec *) c->subset->xas_list; subc; subc = subc->next) {
335 if (subc->config_type == CONF_DIR) {
336 ifsess_resolve_dirs(subc);
337 }
338 }
339 }
340 }
341
342 void ifsess_resolve_server_dirs(server_rec *s) {
343 config_rec *c;
344
345 if (s == NULL ||
346 s->conf == NULL) {
347 return;
348 }
349
350 for (c = (config_rec *) s->conf->xas_list; c; c = c->next) {
351 if (c->config_type == CONF_DIR) {
352 ifsess_resolve_dirs(c);
353 }
354 }
355 }
356
357 static int ifsess_sess_merge_authn(pool *p) {
358 register unsigned int i = 0;
359 config_rec *c = NULL;
360 pool *tmp_pool;
361 array_header *authn_remove_list;
362
363 tmp_pool = make_sub_pool(p);
364 pr_pool_tag(tmp_pool, "<IfAuthenticated> merge pool");
365
366 authn_remove_list = make_array(tmp_pool, 1, sizeof(config_rec *));
367
368 c = find_config(main_server->conf, -1, IFSESS_AUTHN_TEXT, FALSE);
369 while (c != NULL) {
370 config_rec *list = NULL;
371
372 pr_signals_handle();
373
374 list = find_config(c->subset, IFSESS_AUTHN_NUMBER, NULL, FALSE);
375 if (list != NULL) {
376 pr_log_debug(DEBUG2, MOD_IFSESSION_VERSION
377 ": merging <IfAuthenticated> directives in");
378 ifsess_dup_set(session.pool, main_server->conf, c->subset);
379
380 /* Add this config_rec pointer to the list of pointers to be
381 * removed later.
382 */
383 *((config_rec **) push_array(authn_remove_list)) = c;
384 ifsess_resolve_server_dirs(main_server);
385 resolve_deferred_dirs(main_server);
386
387 /* We need to call fixup_dirs() twice: once for any added <Directory>
388 * sections that use absolute paths, and again for any added <Directory>
389 * sections that use deferred-resolution paths (e.g. "~").
390 */
391 fixup_dirs(main_server, CF_SILENT);
392 fixup_dirs(main_server, CF_DEFER|CF_SILENT);
393
394 ifsess_merged = TRUE;
395 }
396
397 c = find_config_next(c, c->next, -1, IFSESS_AUTHN_TEXT, FALSE);
398 }
399
400 /* Now, remove any <IfAuthenticated> config_recs that have been merged in. */
401 for (i = 0; i < authn_remove_list->nelts; i++) {
402 c = ((config_rec **) authn_remove_list->elts)[i];
403 xaset_remove(main_server->conf, (xasetmember_t *) c);
404 }
405
406 destroy_pool(tmp_pool);
407 return 0;
408 }
409
410 static int ifsess_sess_merge_class(pool *p) {
411 register unsigned int i = 0;
412 config_rec *c = NULL;
413 pool *tmp_pool;
414 array_header *class_remove_list;
415
416 tmp_pool = make_sub_pool(p);
417 pr_pool_tag(tmp_pool, "<IfClass> merge pool");
418
419 class_remove_list = make_array(tmp_pool, 1, sizeof(config_rec *));
420
421 c = find_config(main_server->conf, -1, IFSESS_CLASS_TEXT, FALSE);
422 while (c != NULL) {
423 config_rec *list = NULL;
424
425 pr_signals_handle();
426
427 list = find_config(c->subset, IFSESS_CLASS_NUMBER, NULL, FALSE);
428 if (list != NULL) {
429 unsigned char mergein = FALSE;
430
431 #if defined(PR_USE_REGEX)
432 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_REGEX) {
433 pr_regex_t *pre = list->argv[2];
434
435 if (session.conn_class != NULL) {
436 pr_log_debug(DEBUG8, MOD_IFSESSION_VERSION
437 ": evaluating regexp pattern '%s' against subject '%s'",
438 pr_regexp_get_pattern(pre), session.conn_class->cls_name);
439
440 if (pr_regexp_exec(pre, session.conn_class->cls_name, 0, NULL, 0, 0,
441 0) == 0) {
442 mergein = TRUE;
443 }
444 }
445
446 } else
447 #endif /* regex support */
448
449 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_OR &&
450 pr_expr_eval_class_or((char **) &list->argv[2]) == TRUE) {
451 mergein = TRUE;
452
453 } else if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_AND &&
454 pr_expr_eval_class_and((char **) &list->argv[2]) == TRUE) {
455 mergein = TRUE;
456 }
457
458 if (mergein == TRUE) {
459 pr_log_debug(DEBUG2, MOD_IFSESSION_VERSION
460 ": merging <IfClass %s> directives in", (char *) list->argv[0]);
461 ifsess_dup_set(session.pool, main_server->conf, c->subset);
462
463 /* Add this config_rec pointer to the list of pointers to be
464 * removed later.
465 */
466 *((config_rec **) push_array(class_remove_list)) = c;
467
468 /* Do NOT call fixup_dirs() here; we need to wait until after
469 * authentication to do so (in which case, mod_auth will handle the
470 * call to fixup_dirs() for us).
471 */
472
473 ifsess_merged = TRUE;
474
475 } else {
476 pr_log_debug(DEBUG9, MOD_IFSESSION_VERSION
477 ": <IfClass %s> not matched, skipping", (char *) list->argv[0]);
478 }
479 }
480
481 c = find_config_next(c, c->next, -1, IFSESS_CLASS_TEXT, FALSE);
482 }
483
484 /* Now, remove any <IfClass> config_recs that have been merged in. */
485 for (i = 0; i < class_remove_list->nelts; i++) {
486 c = ((config_rec **) class_remove_list->elts)[i];
487 xaset_remove(main_server->conf, (xasetmember_t *) c);
488 }
489
490 destroy_pool(tmp_pool);
491 return 0;
492 }
493
494 static int ifsess_sess_merge_group(pool *p) {
495 register unsigned int i = 0;
496 config_rec *c = NULL;
497 pool *tmp_pool;
498 array_header *group_remove_list;
499
500 tmp_pool = make_sub_pool(p);
501 pr_pool_tag(tmp_pool, "<IfGroup> merge pool");
502
503 group_remove_list = make_array(tmp_pool, 1, sizeof(config_rec *));
504
505 c = find_config(main_server->conf, -1, IFSESS_GROUP_TEXT, FALSE);
506 while (c != NULL) {
507 config_rec *list = NULL;
508
509 pr_signals_handle();
510
511 list = find_config(c->subset, IFSESS_GROUP_NUMBER, NULL, FALSE);
512 if (list != NULL) {
513 unsigned char mergein = FALSE;
514
515 #if defined(PR_USE_REGEX)
516 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_REGEX) {
517 pr_regex_t *pre = list->argv[2];
518
519 if (session.group != NULL) {
520 pr_log_debug(DEBUG8, MOD_IFSESSION_VERSION
521 ": evaluating regexp pattern '%s' against subject '%s'",
522 pr_regexp_get_pattern(pre), session.group);
523
524 if (pr_regexp_exec(pre, session.group, 0, NULL, 0, 0, 0) == 0) {
525 mergein = TRUE;
526 }
527 }
528
529 if (mergein == FALSE &&
530 session.groups != NULL) {
531 register int j = 0;
532
533 for (j = session.groups->nelts-1; j >= 0; j--) {
534 char *suppl_group;
535
536 suppl_group = *(((char **) session.groups->elts) + j);
537
538 pr_log_debug(DEBUG8, MOD_IFSESSION_VERSION
539 ": evaluating regexp pattern '%s' against subject '%s'",
540 pr_regexp_get_pattern(pre), suppl_group);
541
542 if (pr_regexp_exec(pre, suppl_group, 0, NULL, 0, 0, 0) == 0) {
543 mergein = TRUE;
544 break;
545 }
546 }
547 }
548
549 } else
550 #endif /* regex support */
551
552 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_OR &&
553 pr_expr_eval_group_or((char **) &list->argv[2]) == TRUE) {
554 mergein = TRUE;
555
556 } else if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_AND &&
557 pr_expr_eval_group_and((char **) &list->argv[2]) == TRUE) {
558 mergein = TRUE;
559 }
560
561 if (mergein == TRUE) {
562 pr_log_debug(DEBUG2, MOD_IFSESSION_VERSION
563 ": merging <IfGroup %s> directives in", (char *) list->argv[0]);
564 ifsess_dup_set(session.pool, main_server->conf, c->subset);
565
566 /* Add this config_rec pointer to the list of pointers to be
567 * removed later.
568 */
569 *((config_rec **) push_array(group_remove_list)) = c;
570
571 ifsess_resolve_server_dirs(main_server);
572 resolve_deferred_dirs(main_server);
573
574 /* We need to call fixup_dirs() twice: once for any added <Directory>
575 * sections that use absolute paths, and again for any added <Directory>
576 * sections that use deferred-resolution paths (e.g. "~").
577 */
578 fixup_dirs(main_server, CF_SILENT);
579 fixup_dirs(main_server, CF_DEFER|CF_SILENT);
580
581 ifsess_merged = TRUE;
582
583 } else {
584 pr_log_debug(DEBUG9, MOD_IFSESSION_VERSION
585 ": <IfGroup %s> not matched, skipping", (char *) list->argv[0]);
586 }
587 }
588
589 /* Note: it would be more efficient, memory-wise, to destroy the
590 * memory pool of the removed config_rec. However, the dup'd data
591 * from that config_rec may point to memory within the pool being
592 * freed; and once freed, that memory becomes fair game, and thus may
593 * (and probably will) be overwritten. This means that, for now,
594 * keep the removed config_rec's memory around, rather than calling
595 * destroy_pool(c->pool) if removed_c is TRUE.
596 */
597
598 c = find_config_next(c, c->next, -1, IFSESS_GROUP_TEXT, FALSE);
599 }
600
601 /* Now, remove any <IfGroup> config_recs that have been merged in. */
602 for (i = 0; i < group_remove_list->nelts; i++) {
603 c = ((config_rec **) group_remove_list->elts)[i];
604 xaset_remove(main_server->conf, (xasetmember_t *) c);
605 }
606
607 destroy_pool(tmp_pool);
608 return 0;
609 }
610
611 static int ifsess_sess_merge_user(pool *p) {
612 register unsigned int i = 0;
613 config_rec *c = NULL;
614 pool *tmp_pool;
615 array_header *user_remove_list;
616
617 tmp_pool = make_sub_pool(p);
618 pr_pool_tag(tmp_pool, "<IfUser> merge pool");
619
620 user_remove_list = make_array(tmp_pool, 1, sizeof(config_rec *));
621
622 c = find_config(main_server->conf, -1, IFSESS_USER_TEXT, FALSE);
623 while (c != NULL) {
624 config_rec *list = NULL;
625
626 pr_signals_handle();
627
628 list = find_config(c->subset, IFSESS_USER_NUMBER, NULL, FALSE);
629 if (list != NULL) {
630 unsigned char mergein = FALSE;
631
632 #if defined(PR_USE_REGEX)
633 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_REGEX) {
634 pr_regex_t *pre = list->argv[2];
635
636 pr_log_debug(DEBUG8, MOD_IFSESSION_VERSION
637 ": evaluating regexp pattern '%s' against subject '%s'",
638 pr_regexp_get_pattern(pre), session.user);
639
640 if (pr_regexp_exec(pre, session.user, 0, NULL, 0, 0, 0) == 0) {
641 mergein = TRUE;
642 }
643
644 } else
645 #endif /* regex support */
646
647 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_OR &&
648 pr_expr_eval_user_or((char **) &list->argv[2]) == TRUE) {
649 mergein = TRUE;
650
651 } else if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_AND &&
652 pr_expr_eval_user_and((char **) &list->argv[2]) == TRUE) {
653 mergein = TRUE;
654 }
655
656 if (mergein == TRUE) {
657 pr_log_debug(DEBUG2, MOD_IFSESSION_VERSION
658 ": merging <IfUser %s> directives in", (char *) list->argv[0]);
659 ifsess_dup_set(session.pool, main_server->conf, c->subset);
660
661 /* Add this config_rec pointer to the list of pointers to be
662 * removed later.
663 */
664 *((config_rec **) push_array(user_remove_list)) = c;
665
666 ifsess_resolve_server_dirs(main_server);
667 resolve_deferred_dirs(main_server);
668
669 /* We need to call fixup_dirs() twice: once for any added <Directory>
670 * sections that use absolute paths, and again for any added <Directory>
671 * sections that use deferred-resolution paths (e.g. "~").
672 */
673 fixup_dirs(main_server, CF_SILENT);
674 fixup_dirs(main_server, CF_DEFER|CF_SILENT);
675
676 ifsess_merged = TRUE;
677
678 } else {
679 pr_log_debug(DEBUG9, MOD_IFSESSION_VERSION
680 ": <IfUser %s> not matched, skipping", (char *) list->argv[0]);
681 }
682 }
683
684 c = find_config_next(c, c->next, -1, IFSESS_USER_TEXT, FALSE);
685 }
686
687 /* Now, remove any <IfUser> config_recs that have been merged in. */
688 for (i = 0; i < user_remove_list->nelts; i++) {
689 c = ((config_rec **) user_remove_list->elts)[i];
690 xaset_remove(main_server->conf, (xasetmember_t *) c);
691 }
692
693 destroy_pool(tmp_pool);
694 return 0;
695 }
696
697 static void ifsess_sess_process_displayfiles(void) {
698 config_rec *c;
699 char *displaylogin = NULL;
700 xaset_t *config_set = NULL;
701
702 /* Look for a DisplayLogin file which has an absolute path. If we find one,
703 * open a filehandle, such that that file can be displayed even if the
704 * session is chrooted. DisplayLogin files with relative paths will be
705 * handled after chroot, preserving the old behavior.
706 */
707
708 c = find_config(main_server->conf, -1, IFSESS_GROUP_TEXT, FALSE);
709 while (c != NULL) {
710 config_rec *list = NULL;
711
712 pr_signals_handle();
713
714 list = find_config(c->subset, IFSESS_GROUP_NUMBER, NULL, FALSE);
715 if (list != NULL) {
716 #if defined(PR_USE_REGEX)
717 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_REGEX) {
718 pr_regex_t *pre = list->argv[2];
719
720 if (session.group != NULL) {
721 if (pr_regexp_exec(pre, session.group, 0, NULL, 0, 0, 0) == 0) {
722 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
723 if (displaylogin != NULL) {
724 if (*displaylogin == '/') {
725 config_set = c->subset;
726 }
727 }
728 }
729 }
730
731 if (displaylogin == NULL &&
732 session.groups != NULL) {
733 register int j = 0;
734
735 for (j = session.groups->nelts-1; j >= 0; j--) {
736 char *suppl_group;
737
738 suppl_group = *(((char **) session.groups->elts) + j);
739
740 if (pr_regexp_exec(pre, suppl_group, 0, NULL, 0, 0, 0) == 0) {
741 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
742 if (displaylogin != NULL) {
743 if (*displaylogin == '/') {
744 config_set = c->subset;
745 }
746 }
747
748 break;
749 }
750 }
751 }
752
753 } else
754 #endif /* regex support */
755
756 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_OR &&
757 pr_expr_eval_group_or((char **) &list->argv[2]) == TRUE) {
758 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
759 if (displaylogin != NULL) {
760 if (*displaylogin == '/') {
761 config_set = c->subset;
762 }
763 }
764
765 } else if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_AND &&
766 pr_expr_eval_group_and((char **) &list->argv[2]) == TRUE) {
767
768 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
769 if (displaylogin != NULL) {
770 if (*displaylogin == '/') {
771 config_set = c->subset;
772 }
773 }
774 }
775 }
776
777 c = find_config_next(c, c->next, -1, IFSESS_GROUP_TEXT, FALSE);
778 }
779
780 c = find_config(main_server->conf, -1, IFSESS_USER_TEXT, FALSE);
781 while (c != NULL) {
782 config_rec *list = NULL;
783
784 pr_signals_handle();
785
786 list = find_config(c->subset, IFSESS_USER_NUMBER, NULL, FALSE);
787 if (list != NULL) {
788 #if defined(PR_USE_REGEX)
789 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_REGEX) {
790 pr_regex_t *pre = list->argv[2];
791
792 if (pr_regexp_exec(pre, session.user, 0, NULL, 0, 0, 0) == 0) {
793 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
794 if (displaylogin != NULL) {
795 if (*displaylogin == '/') {
796 config_set = c->subset;
797 }
798 }
799 }
800
801 } else
802 #endif /* regex support */
803
804 if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_OR &&
805 pr_expr_eval_user_or((char **) &list->argv[2]) == TRUE) {
806 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
807 if (displaylogin != NULL) {
808 if (*displaylogin == '/') {
809 config_set = c->subset;
810 }
811 }
812
813 } else if (*((unsigned char *) list->argv[1]) == PR_EXPR_EVAL_AND &&
814 pr_expr_eval_user_and((char **) &list->argv[2]) == TRUE) {
815 displaylogin = get_param_ptr(c->subset, "DisplayLogin", FALSE);
816 if (displaylogin != NULL) {
817 if (*displaylogin == '/') {
818 config_set = c->subset;
819 }
820 }
821 }
822 }
823
824 c = find_config_next(c, c->next, -1, IFSESS_USER_TEXT, FALSE);
825 }
826
827 if (displaylogin != NULL &&
828 config_set != NULL) {
829 displaylogin_fh = pr_fsio_open(displaylogin, O_RDONLY);
830 if (displaylogin_fh == NULL) {
831 pr_log_debug(DEBUG6, MOD_IFSESSION_VERSION
832 ": unable to open DisplayLogin file '%s': %s", displaylogin,
833 strerror(errno));
834
835 } else {
836 struct stat st;
837
838 if (pr_fsio_fstat(displaylogin_fh, &st) < 0) {
839 pr_log_debug(DEBUG6, MOD_IFSESSION_VERSION
840 ": unable to stat DisplayLogin file '%s': %s", displaylogin,
841 strerror(errno));
842 pr_fsio_close(displaylogin_fh);
843 displaylogin_fh = NULL;
844
845 } else {
846 if (S_ISDIR(st.st_mode)) {
847 errno = EISDIR;
848 pr_log_debug(DEBUG6, MOD_IFSESSION_VERSION
849 ": unable to use DisplayLogin file '%s': %s", displaylogin,
850 strerror(errno));
851 pr_fsio_close(displaylogin_fh);
852 displaylogin_fh = NULL;
853
854 } else {
855 /* Remove the directive from the set, since we'll be handling it. */
856 remove_config(config_set, "DisplayLogin", FALSE);
857 }
858 }
859 }
860 }
861 }
862
863 /* Configuration handlers
864 */
865
866 /* usage: IfSessionOptions opt1 ... */
867 MODRET set_ifsessopts(cmd_rec *cmd) {
868 config_rec *c = NULL;
869 register unsigned int i = 0;
870 unsigned long opts = 0UL;
871
872 if (cmd->argc-1 == 0) {
873 CONF_ERROR(cmd, "wrong number of parameters");
874 }
875
876 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
877
878 c = add_config_param(cmd->argv[0], 1, NULL);
879
880 for (i = 1; i < cmd->argc; i++) {
881 if (strcmp(cmd->argv[i], "PerUnauthenticatedUser") == 0) {
882 opts |= IFSESS_OPT_PER_UNAUTHED_USER;
883
884 } else {
885 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown IfSessionOption '",
886 cmd->argv[i], "'", NULL));
887 }
888 }
889
890 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
891 *((unsigned long *) c->argv[0]) = opts;
892
893 return PR_HANDLED(cmd);
894 }
895
896 MODRET start_ifctxt(cmd_rec *cmd) {
897 config_rec *c = NULL;
898 int config_type = 0, eval_type = 0;
899 unsigned int argc = 0;
900 char *name = NULL;
901 void **argv = NULL;
902 array_header *acl = NULL;
903
904 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
905
906 c = pr_parser_config_ctxt_open(cmd->argv[0]);
907
908 /* "Inherit" the parent's context type. */
909 c->config_type = (cmd->config && cmd->config->config_type != CONF_PARAM ?
910 cmd->config->config_type : cmd->server->config_type ?
911 cmd->server->config_type : CONF_ROOT);
912
913 if (strcmp(cmd->argv[0], IFSESS_CLASS_TEXT) == 0) {
914 name = "_IfClassList";
915 ifsess_ctx = config_type = IFSESS_CLASS_NUMBER;
916 eval_type = PR_EXPR_EVAL_OR;
917
918 if (cmd->argc-1 < 1) {
919 CONF_ERROR(cmd, "wrong number of parameters");
920 }
921
922 } else if (strcmp(cmd->argv[0], IFSESS_GROUP_TEXT) == 0) {
923 name = "_IfGroupList";
924 ifsess_ctx = config_type = IFSESS_GROUP_NUMBER;
925 eval_type = PR_EXPR_EVAL_AND;
926
927 if (cmd->argc-1 < 1) {
928 CONF_ERROR(cmd, "wrong number of parameters");
929 }
930
931 } else if (strcmp(cmd->argv[0], IFSESS_USER_TEXT) == 0) {
932 name = "_IfUserList";
933 ifsess_ctx = config_type = IFSESS_USER_NUMBER;
934 eval_type = PR_EXPR_EVAL_OR;
935
936 if (cmd->argc-1 < 1) {
937 CONF_ERROR(cmd, "wrong number of parameters");
938 }
939
940 } else if (strcmp(cmd->argv[0], IFSESS_AUTHN_TEXT) == 0) {
941 name = "_IfAuthenticatedList";
942 ifsess_ctx = config_type = IFSESS_AUTHN_NUMBER;
943 eval_type = PR_EXPR_EVAL_OR;
944
945 /* <IfAuthenticated> sections don't take any parameters. */
946 if (cmd->argc > 1) {
947 CONF_ERROR(cmd, "wrong number of parameters");
948 }
949 }
950
951 /* Is this a normal expression, an explicit AND, an explicit OR, or a
952 * regular expression?
953 */
954 if (cmd->argc-1 > 1) {
955 if (strcmp(cmd->argv[1], "AND") == 0) {
956 eval_type = PR_EXPR_EVAL_AND;
957 argc = cmd->argc-2;
958 argv = cmd->argv+1;
959
960 } else if (strcmp(cmd->argv[1], "OR") == 0) {
961 eval_type = PR_EXPR_EVAL_OR;
962 argc = cmd->argc-2;
963 argv = cmd->argv+1;
964
965 } else if (strcmp(cmd->argv[1], "regex") == 0) {
966 #if defined(PR_USE_REGEX)
967 pr_regex_t *pre = NULL;
968 int res = 0;
969
970 if (cmd->argc != 3)
971 CONF_ERROR(cmd, "wrong number of parameters");
972
973 pre = pr_regexp_alloc(&ifsession_module);
974
975 res = pr_regexp_compile(pre, cmd->argv[2], REG_EXTENDED|REG_NOSUB);
976 if (res != 0) {
977 char errstr[200] = {'\0'};
978
979 pr_regexp_error(res, pre, errstr, sizeof(errstr));
980 pr_regexp_free(NULL, pre);
981
982 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": '", cmd->argv[2], "' failed "
983 "regex compilation: ", errstr, NULL));
984 }
985
986 eval_type = PR_EXPR_EVAL_REGEX;
987
988 c = add_config_param(name, 3, NULL, NULL, NULL);
989 c->config_type = config_type;
990 c->argv[0] = pstrdup(c->pool, cmd->arg);
991 c->argv[1] = pcalloc(c->pool, sizeof(unsigned char));
992 *((unsigned char *) c->argv[1]) = eval_type;
993 c->argv[2] = (void *) pre;
994
995 return PR_HANDLED(cmd);
996
997 #else
998 CONF_ERROR(cmd, "The 'regex' parameter cannot be used on this system, "
999 "as you do not have POSIX compliant regex support");
1000 #endif /* regex support */
1001
1002 } else {
1003 argc = cmd->argc-1;
1004 argv = cmd->argv;
1005 }
1006
1007 } else {
1008 argc = cmd->argc-1;
1009 argv = cmd->argv;
1010 }
1011
1012 acl = pr_expr_create(cmd->tmp_pool, &argc, (char **) argv);
1013 if (acl == NULL) {
1014 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error creating regex expression: ",
1015 strerror(errno), NULL));
1016 }
1017
1018 c = add_config_param(name, 0);
1019
1020 c->config_type = config_type;
1021 c->argc = acl->nelts + 2;
1022 c->argv = pcalloc(c->pool, (c->argc + 2) * sizeof(void *));
1023 c->argv[0] = pstrdup(c->pool, cmd->arg);
1024 c->argv[1] = pcalloc(c->pool, sizeof(unsigned char));
1025 *((unsigned char *) c->argv[1]) = eval_type;
1026
1027 argv = c->argv + 2;
1028
1029 if (acl) {
1030 while (acl->nelts--) {
1031 *argv++ = pstrdup(c->pool, *((char **) acl->elts));
1032 acl->elts = ((char **) acl->elts) + 1;
1033 }
1034 }
1035
1036 *argv = NULL;
1037 return PR_HANDLED(cmd);
1038 }
1039
1040 MODRET end_ifctxt(cmd_rec *cmd) {
1041 pr_parser_config_ctxt_close(NULL);
1042
1043 switch (ifsess_ctx) {
1044 case IFSESS_CLASS_NUMBER:
1045 if (strcasecmp("</IfClass>", cmd->argv[0]) == 0) {
1046 ifsess_ctx = -1;
1047 }
1048 break;
1049
1050 case IFSESS_GROUP_NUMBER:
1051 if (strcasecmp("</IfGroup>", cmd->argv[0]) == 0) {
1052 ifsess_ctx = -1;
1053 }
1054 break;
1055
1056 case IFSESS_USER_NUMBER:
1057 if (strcasecmp("</IfUser>", cmd->argv[0]) == 0) {
1058 ifsess_ctx = -1;
1059 }
1060 break;
1061
1062 case IFSESS_AUTHN_NUMBER:
1063 if (strcasecmp("</IfAuthenticated>", cmd->argv[0]) == 0) {
1064 ifsess_ctx = -1;
1065 }
1066 break;
1067 }
1068
1069 return PR_HANDLED(cmd);
1070 }
1071
1072 /* Command handlers
1073 */
1074
1075 MODRET ifsess_post_pass(cmd_rec *cmd) {
1076 /* Unfortunately, I can't assign my own context types for these custom
1077 * contexts, otherwise the existing directives would not be allowed in
1078 * them. Good to know for the future, though, when developing modules that
1079 * want to have their own complete contexts (e.g. mod_time-3.0).
1080 *
1081 * However, I _can_ add a directive config_rec to these contexts that has
1082 * its own custom config_type. And by using -1 as the context type when
1083 * searching via find_config(), it will match any context as long as the
1084 * name also matches. Note: using a type of -1 and a name of NULL will
1085 * result in a scan of the whole in-memory db. Hmm...
1086 */
1087
1088 (void) ifsess_sess_merge_authn(session.pool);
1089 (void) ifsess_sess_merge_group(session.pool);
1090 (void) ifsess_sess_merge_user(session.pool);
1091
1092 if (ifsess_merged == TRUE) {
1093 int found = 0;
1094
1095 /* Try to honor any <Limit LOGIN> sections that may have been merged in. */
1096 if (!login_check_limits(TOPLEVEL_CONF, FALSE, TRUE, &found)) {
1097 pr_log_debug(DEBUG3, MOD_IFSESSION_VERSION
1098 ": %s %s: Limit access denies login",
1099 session.anon_config ? "ANON" : C_USER, session.user);
1100
1101 pr_log_auth(PR_LOG_NOTICE, "%s %s: Limit access denies login.",
1102 session.anon_config ? "ANON" : C_USER, session.user);
1103 pr_session_disconnect(&ifsession_module, PR_SESS_DISCONNECT_CONFIG_ACL,
1104 "Denied by <Limit LOGIN>");
1105 }
1106
1107 /* Try to honor any DisplayLogin directives that may have been merged
1108 * in (Bug#3882).
1109 */
1110 if (displaylogin_fh != NULL) {
1111 if (pr_display_fh(displaylogin_fh, NULL, R_230, 0) < 0) {
1112 pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
1113 displaylogin_fh->fh_path, strerror(errno));
1114 }
1115
1116 pr_fsio_close(displaylogin_fh);
1117 displaylogin_fh = NULL;
1118 }
1119
1120 /* Make sure directory config pointers are set correctly (Bug #4315). */
1121 dir_check_full(cmd->tmp_pool, cmd, G_NONE, session.cwd, NULL);
1122 }
1123
1124 return PR_DECLINED(cmd);
1125 }
1126
1127 MODRET ifsess_post_user(cmd_rec *cmd) {
1128 const char *user, *group = NULL, *sess_user, *sess_group;
1129 array_header *gids = NULL, *groups = NULL, *sess_groups = NULL;
1130 struct passwd *pw = NULL;
1131 struct group *gr = NULL;
1132
1133 /* Only handle the case where the PerUnauthenticatedUser option is in
1134 * effect.
1135 */
1136 if (!(ifsess_opts & IFSESS_OPT_PER_UNAUTHED_USER)) {
1137 return PR_DECLINED(cmd);
1138 }
1139
1140 user = cmd->arg;
1141 if (user == NULL) {
1142 return PR_DECLINED(cmd);
1143 }
1144
1145 pw = pr_auth_getpwnam(cmd->tmp_pool, user);
1146 if (pw == NULL) {
1147 pr_trace_msg(trace_channel, 9,
1148 "unable to lookup user '%s' (%s), skipping pre-USER handling",
1149 user, strerror(errno));
1150 return PR_DECLINED(cmd);
1151 }
1152
1153 gr = pr_auth_getgrgid(cmd->tmp_pool, pw->pw_gid);
1154 if (gr != NULL) {
1155 group = gr->gr_name;
1156 }
1157
1158 (void) pr_auth_getgroups(cmd->tmp_pool, user, &gids, &groups);
1159
1160 /* Temporarily set session.user, session.group, session.groups, for the
1161 * sake of the pr_eval_*() function calls.
1162 */
1163 sess_user = session.user;
1164 sess_group = session.group;
1165 sess_groups = session.groups;
1166
1167 session.user = user;
1168 session.group = group;
1169 session.groups = groups;
1170
1171 (void) ifsess_sess_merge_group(session.pool);
1172 (void) ifsess_sess_merge_user(session.pool);
1173
1174 session.user = sess_user;
1175 session.group = sess_group;
1176 session.groups = sess_groups;
1177
1178 /* Stash a session note indicated that we merged in per-user/group settings
1179 * for an unauthenticated client, for use by e.g. mod_tls; see Issue #1640.
1180 */
1181 (void) pr_table_add_dup(session.notes,
1182 "mod_ifsession.per-unauthenticated-user", "true", 0);
1183 return PR_DECLINED(cmd);
1184 }
1185
1186 /* Event handlers
1187 */
1188
1189 static void ifsess_chroot_ev(const void *event_data, void *user_data) {
1190 ifsess_home_dir = (const char *) event_data;
1191
1192 ifsess_sess_process_displayfiles();
1193 }
1194
1195 #if defined(PR_SHARED_MODULE)
1196 static void ifsess_mod_unload_ev(const void *event_data, void *user_data) {
1197 if (strcmp("mod_ifsession.c", (const char *) event_data) != 0) {
1198 return;
1199 }
1200
1201 pr_event_unregister(&ifsession_module, NULL, NULL);
1202 }
1203 #endif /* PR_SHARED_MODULE */
1204
1205 static void ifsess_postparse_ev(const void *event_data, void *user_data) {
1206 /* Make sure that all mod_ifsession sections have been properly closed. */
1207
1208 if (ifsess_ctx == -1) {
1209 /* All sections properly closed; nothing to do. */
1210 return;
1211 }
1212
1213 switch (ifsess_ctx) {
1214 case IFSESS_CLASS_NUMBER:
1215 pr_log_pri(PR_LOG_WARNING,
1216 "error: unclosed <IfClass> context in config file");
1217 break;
1218
1219 case IFSESS_GROUP_NUMBER:
1220 pr_log_pri(PR_LOG_WARNING,
1221 "error: unclosed <IfGroup> context in config file");
1222 break;
1223
1224 case IFSESS_USER_NUMBER:
1225 pr_log_pri(PR_LOG_WARNING,
1226 "error: unclosed <IfUser> context in config file");
1227 break;
1228 }
1229
1230 pr_session_disconnect(&ifsession_module, PR_SESS_DISCONNECT_BAD_CONFIG, NULL);
1231 return;
1232 }
1233
1234 /* Initialization routines
1235 */
1236
1237 static int ifsess_init(void) {
1238 #if defined(PR_SHARED_MODULE)
1239 pr_event_register(&ifsession_module, "core.module-unload",
1240 ifsess_mod_unload_ev, NULL);
1241 #endif /* PR_SHARED_MODULE */
1242
1243 pr_event_register(&ifsession_module, "core.postparse",
1244 ifsess_postparse_ev, NULL);
1245
1246 return 0;
1247 }
1248
1249 static int ifsess_sess_init(void) {
1250 config_rec *c;
1251
1252 pr_event_register(&ifsession_module, "core.chroot",
1253 ifsess_chroot_ev, NULL);
1254
1255 c = find_config(main_server->conf, CONF_PARAM, "IfSessionOptions", FALSE);
1256 while (c != NULL) {
1257 unsigned long opts = 0UL;
1258
1259 pr_signals_handle();
1260
1261 opts = *((unsigned long *) c->argv[0]);
1262 ifsess_opts |= opts;
1263
1264 c = find_config_next(c, c->next, CONF_PARAM, "IfSessionOptions", FALSE);
1265 }
1266
1267 if (ifsess_sess_merge_class(session.pool) < 0) {
1268 return -1;
1269 }
1270
1271 return 0;
1272 }
1273
1274 /* Module API tables
1275 */
1276
1277 static conftable ifsess_conftab[] = {
1278 { IFSESS_AUTHN_TEXT, start_ifctxt, NULL },
1279 { "</IfAuthenticated>", end_ifctxt, NULL },
1280 { IFSESS_CLASS_TEXT, start_ifctxt, NULL },
1281 { "</IfClass>", end_ifctxt, NULL },
1282 { IFSESS_GROUP_TEXT, start_ifctxt, NULL },
1283 { "</IfGroup>", end_ifctxt, NULL },
1284 { "IfSessionOptions", set_ifsessopts, NULL },
1285 { IFSESS_USER_TEXT, start_ifctxt, NULL },
1286 { "</IfUser>", end_ifctxt, NULL },
1287 { NULL }
1288 };
1289
1290 static cmdtable ifsess_cmdtab[] = {
1291 { POST_CMD, C_PASS, G_NONE, ifsess_post_pass, FALSE, FALSE },
1292 { POST_CMD, C_USER, G_NONE, ifsess_post_user, FALSE, FALSE },
1293 { 0, NULL }
1294 };
1295
1296 module ifsession_module = {
1297 NULL, NULL,
1298
1299 /* Module API version 2.0 */
1300 0x20,
1301
1302 /* Module name */
1303 "ifsession",
1304
1305 /* Module configuration handler table */
1306 ifsess_conftab,
1307
1308 /* Module command handler table */
1309 ifsess_cmdtab,
1310
1311 /* Module authentication handler table */
1312 NULL,
1313
1314 /* Module initialization function */
1315 ifsess_init,
1316
1317 /* Session initialization function */
1318 ifsess_sess_init,
1319
1320 /* Module version */
1321 MOD_IFSESSION_VERSION
1322 };