Debian Codegraph @

contrib/mod_wrap.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * ProFTPD: mod_wrap -- use Wietse Venema's TCP wrappers library for
3 * access control
4 * Copyright (c) 2000-2020 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 gives permission to link this program
21 * with OpenSSL, and distribute the resulting executable, without including
22 * the source code for OpenSSL in the source distribution.
23 *
24 * -- DO NOT MODIFY THE TWO LINES BELOW --
25 * $Libraries: -lwrap -lnsl$
26 */
27
28 #define MOD_WRAP_VERSION "mod_wrap/1.2.4"
29
30 #include "conf.h"
31 #include "privs.h"
32 #include "tcpd.h"
33
34 /* these need to be defined for the libwrap functions -- default settings
35 * are those from tcpd.h
36 */
37 int allow_severity = PR_LOG_INFO;
38 int deny_severity = PR_LOG_WARNING;
39
40 module wrap_module;
41
42 /* Necessary prototypes */
43 static int wrap_eval_expression(char **, array_header *);
44 static const char *wrap_get_user_table(cmd_rec *, const char *, char *);
45 static int wrap_is_usable_file(const char *);
46 static void wrap_log_request_allowed(int, struct request_info *);
47 static void wrap_log_request_denied(int, struct request_info *);
48 static config_rec *wrap_resolve_user(pool *, const char **);
49 static int wrap_sess_init(void);
50
51 static char *wrap_service_name = "proftpd";
52
53 /* Support routines
54 */
55
56 /* boolean "expression" matching, returns TRUE if the entire expression matches
57 */
58 static int wrap_eval_expression(char **config_expr,
59 array_header *session_expr) {
60 char *elem = NULL, **list = NULL;
61
62 /* sanity check */
63 if (!config_expr || !*config_expr || !session_expr) {
64 return FALSE;
65 }
66
67 list = (char **) session_expr->elts;
68
69 for (; *config_expr; config_expr++) {
70 register unsigned int i;
71 unsigned char found;
72
73 elem = *config_expr;
74 found = FALSE;
75
76 if (*elem == '!') {
77 found = !found;
78 elem++;
79 }
80
81 for (i = 0; i < session_expr->nelts; i++) {
82 if (list[i] &&
83 strcmp(list[i], elem) == 0) {
84 found = !found;
85 break;
86 }
87 }
88
89 if (!found) {
90 config_expr = NULL;
91 break;
92 }
93 }
94
95 if (config_expr) {
96 return TRUE;
97 }
98
99 return FALSE;
100 }
101
102 static const char *wrap_get_user_table(cmd_rec *cmd, const char *user,
103 char *path) {
104 int xerrno = 0;
105
106 char *real_path = NULL;
107 struct passwd *pw = NULL;
108
109 pw = pr_auth_getpwnam(cmd->pool, user);
110
111 /* Handle the case where the given user does not exist. */
112 if (pw == NULL) {
113 return NULL;
114 }
115
116 /* For the dir_realpath() function to work, some session members need to
117 * be set.
118 */
119 session.user = pstrdup(cmd->pool, pw->pw_name);
120 session.login_uid = pw->pw_uid;
121
122 PRIVS_USER
123 real_path = dir_realpath(cmd->pool, path);
124 xerrno = errno;
125 PRIVS_RELINQUISH
126
127 if (real_path) {
128 path = real_path;
129 }
130
131 errno = xerrno;
132 return path;
133 }
134
135 static int wrap_is_usable_file(const char *filename) {
136 struct stat st;
137 pr_fh_t *fh = NULL;
138
139 /* check the easy case first */
140 if (filename == NULL) {
141 return FALSE;
142 }
143
144 /* Make sure that the current process can _read_ the file. */
145 fh = pr_fsio_open(filename, O_RDONLY);
146 if (fh == NULL) {
147 int xerrno = errno;
148
149 pr_log_pri(PR_LOG_NOTICE, MOD_WRAP_VERSION ": failed to read \"%s\": %s",
150 filename, strerror(xerrno));
151
152 errno = xerrno;
153 return FALSE;
154 }
155
156 if (pr_fsio_fstat(fh, &st) < 0) {
157 int xerrno = errno;
158
159 pr_log_pri(PR_LOG_NOTICE, MOD_WRAP_VERSION ": failed to stat \"%s\": %s",
160 filename, strerror(xerrno));
161
162 pr_fsio_close(fh);
163 errno = xerrno;
164 return FALSE;
165 }
166
167 if (S_ISDIR(st.st_mode)) {
168 int xerrno = EISDIR;
169
170 pr_log_pri(PR_LOG_NOTICE, MOD_WRAP_VERSION ": unable to use \"%s\": %s",
171 filename, strerror(xerrno));
172
173 pr_fsio_close(fh);
174 errno = xerrno;
175 return FALSE;
176 }
177
178 pr_fsio_close(fh);
179 return TRUE;
180 }
181
182 static void wrap_log_request_allowed(int severity,
183 struct request_info *request) {
184 int priority;
185
186 /* Mask off the facility bits. */
187 priority = (severity & PR_LOG_PRIMASK);
188
189 pr_log_pri(priority, MOD_WRAP_VERSION ": allowed connection from %s",
190 eval_client(request));
191
192 /* done */
193 return;
194 }
195
196 static void wrap_log_request_denied(int severity,
197 struct request_info *request) {
198 int priority;
199
200 /* Mask off the facility bits. */
201 priority = (severity & PR_LOG_PRIMASK);
202
203 pr_log_pri(priority, MOD_WRAP_VERSION ": refused connection from %s",
204 eval_client(request));
205
206 /* done */
207 return;
208 }
209
210 static config_rec *wrap_resolve_user(pool *p, const char **user) {
211 config_rec *conf = NULL, *top_conf;
212 char *ourname = NULL, *anonname = NULL;
213 unsigned char is_alias = FALSE, force_anon = FALSE;
214
215 /* Precedence rules:
216 * 1. Search for UserAlias directive.
217 * 2. Search for Anonymous directive.
218 * 3. Normal user login
219 */
220
221 ourname = (char*) get_param_ptr(main_server->conf, "UserName", FALSE);
222
223 conf = find_config(main_server->conf, CONF_PARAM, "UserAlias", TRUE);
224
225 if (conf) do {
226 if (strcmp(conf->argv[0], "*") == 0 ||
227 strcmp(conf->argv[0], *user) == 0) {
228 is_alias = TRUE;
229 break;
230 }
231
232 } while ((conf = find_config_next(conf, conf->next, CONF_PARAM,
233 "UserAlias", TRUE)) != NULL);
234
235 /* if AuthAliasOnly is set, ignore this one and continue */
236 top_conf = conf;
237
238 while (conf && conf->parent &&
239 find_config(conf->parent->set, CONF_PARAM, "AuthAliasOnly", FALSE)) {
240
241 is_alias = FALSE;
242 find_config_set_top(top_conf);
243 conf = find_config_next(conf, conf->next, CONF_PARAM, "UserAlias", TRUE);
244
245 if (conf &&
246 (strcmp(conf->argv[0], "*") == 0 ||
247 strcmp(conf->argv[0], *user) == 0))
248 is_alias = TRUE;
249 }
250
251 if (conf != NULL) {
252 *user = conf->argv[1];
253
254 /* If the alias is applied inside an <Anonymous> context, we have found
255 * our anon block
256 */
257 if (conf->parent &&
258 conf->parent->config_type == CONF_ANON) {
259 conf = conf->parent;
260
261 } else {
262 conf = NULL;
263 }
264 }
265
266 /* Next, search for an anonymous entry */
267 if (conf == NULL) {
268 conf = find_config(main_server->conf, CONF_ANON, NULL, FALSE);
269
270 } else {
271 find_config_set_top(conf);
272 }
273
274 if (conf != NULL) do {
275 anonname = (char*) get_param_ptr(conf->subset, "UserName", FALSE);
276
277 if (!anonname)
278 anonname = ourname;
279
280 if (anonname &&
281 strcmp(anonname, *user) == 0) {
282 break;
283 }
284
285 } while ((conf = find_config_next(conf, conf->next, CONF_ANON, NULL,
286 FALSE)) != NULL);
287
288 if (!is_alias && !force_anon) {
289
290 if (find_config((conf ? conf->subset :
291 main_server->conf), CONF_PARAM, "AuthAliasOnly", FALSE)) {
292
293 if (conf != NULL &&
294 conf->config_type == CONF_ANON) {
295 conf = NULL;
296
297 } else {
298 *user = NULL;
299 }
300
301 if (*user != NULL &&
302 find_config(main_server->conf, CONF_PARAM, "AuthAliasOnly", FALSE)) {
303 *user = NULL;
304 }
305 }
306 }
307
308 return conf;
309 }
310
311 /* Configuration handlers
312 */
313
314 MODRET set_tcpaccessfiles(cmd_rec *cmd) {
315 config_rec *c = NULL;
316
317 /* assume use of the standard TCP wrappers installation locations */
318 char *allow_filename = "/etc/hosts.allow";
319 char *deny_filename = "/etc/hosts.deny";
320
321 CHECK_ARGS(cmd, 2);
322 CHECK_CONF(cmd, CONF_ROOT|CONF_ANON|CONF_VIRTUAL|CONF_GLOBAL);
323
324 /* use the user-given files, checking to make sure that they exist and
325 * are readable.
326 */
327 allow_filename = cmd->argv[1];
328 deny_filename = cmd->argv[2];
329
330 /* if the filenames begin with a '~', AND this is not immediately followed
331 * by a '/' (ie '~/'), expand it out for checking and storing for later
332 * lookups. If the filenames DO begin with '~/', do the expansion later,
333 * after authenication. In other words, do checking of static filenames
334 * now, and checking of dynamic (user-authentication-based) filenames
335 * later.
336 */
337 if (allow_filename[0] == '/') {
338
339 /* it's an absolute path, so the filename will be checked as is */
340 if (!wrap_is_usable_file(allow_filename))
341 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
342 cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL));
343
344 } else if (allow_filename[0] == '~' && allow_filename[1] != '/') {
345 char *allow_real_file = NULL;
346
347 allow_real_file = dir_realpath(cmd->pool, allow_filename);
348
349 if (allow_real_file == NULL || !wrap_is_usable_file(allow_real_file))
350 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
351 cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL));
352
353 allow_filename = allow_real_file;
354
355 } else if (allow_filename[0] != '~' && allow_filename[0] != '/') {
356
357 /* no relative paths allowed */
358 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
359 cmd->argv[0], ": '", allow_filename, "' must start with \"/\" or \"~\"",
360 NULL));
361
362 } else {
363
364 /* it's a determine-at-login-time filename -- check it later */
365 ;
366 }
367
368 if (deny_filename[0] == '/') {
369
370 /* it's an absolute path, so the filename will be checked as is */
371 if (!wrap_is_usable_file(deny_filename))
372 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
373 cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL));
374
375 } else if (deny_filename[0] == '~' && deny_filename[1] != '/') {
376 char *deny_real_file = NULL;
377
378 deny_real_file = dir_realpath(cmd->pool, deny_filename);
379
380 if (deny_real_file == NULL || !wrap_is_usable_file(deny_real_file))
381 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
382 cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL));
383
384 deny_filename = deny_real_file;
385
386 } else if (deny_filename[0] != '~' && deny_filename[0] != '/') {
387
388 /* no relative paths allowed */
389 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
390 cmd->argv[0], ": '", deny_filename, "' must start with \"/\" or \"~\"",
391 NULL));
392
393 } else {
394
395 /* it's a determine-at-login-time filename -- check it later */
396 ;
397 }
398
399 c = add_config_param_str(cmd->argv[0], 2, (void *) allow_filename,
400 (void *) deny_filename);
401 c->flags |= CF_MERGEDOWN;
402
403 /* done */
404 return PR_HANDLED(cmd);
405 }
406
407 MODRET set_tcpgroupaccessfiles(cmd_rec *cmd) {
408 unsigned int group_argc = 1;
409 char *expr, **group_argv = NULL;
410 array_header *group_acl = NULL;
411 config_rec *c = NULL;
412
413 /* assume use of the standard TCP wrappers installation locations */
414 char *allow_filename = NULL, *deny_filename = NULL;
415
416 CHECK_ARGS(cmd, 3);
417 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
418
419 /* use the user-given files, checking to make sure that they exist and
420 * are readable.
421 */
422 allow_filename = cmd->argv[2];
423 deny_filename = cmd->argv[3];
424
425 /* if the filenames begin with a '~', AND this is not immediately followed
426 * by a '/' (ie '~/'), expand it out for checking and storing for later
427 * lookups. If the filenames DO begin with '~/', do the expansion later,
428 * after authenication. In other words, do checking of static filenames
429 * now, and checking of dynamic (user-authentication-based) filenames
430 * later.
431 */
432 if (allow_filename[0] == '/') {
433
434 /* it's an absolute path, so the filename will be checked as is */
435 if (!wrap_is_usable_file(allow_filename))
436 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
437 cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL));
438
439 } else if (allow_filename[0] == '~' && allow_filename[1] != '/') {
440 char *allow_real_file = NULL;
441
442 allow_real_file = dir_realpath(cmd->pool, allow_filename);
443
444 if (allow_real_file == NULL || !wrap_is_usable_file(allow_real_file))
445 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
446 cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL));
447
448 allow_filename = allow_real_file;
449
450 } else if (allow_filename[0] != '~' && allow_filename[0] != '/') {
451
452 /* no relative paths allowed */
453 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
454 cmd->argv[0], ": '", allow_filename, "' must start with \"/\" or \"~\"",
455 NULL));
456
457 } else {
458
459 /* it's a determine-at-login-time filename -- check it later */
460 ;
461 }
462
463 if (deny_filename[0] == '/') {
464
465 /* it's an absolute path, so the filename will be checked as is */
466 if (!wrap_is_usable_file(deny_filename))
467 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
468 cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL));
469
470 } else if (deny_filename[0] == '~' && deny_filename[1] != '/') {
471 char *deny_real_file = NULL;
472
473 deny_real_file = dir_realpath(cmd->pool, deny_filename);
474
475 if (deny_real_file == NULL || !wrap_is_usable_file(deny_real_file))
476 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
477 cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL));
478
479 deny_filename = deny_real_file;
480
481 } else if (deny_filename[0] != '~' && deny_filename[0] != '/') {
482
483 /* no relative paths allowed */
484 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
485 cmd->argv[0], ": '", deny_filename, "' must start with \"/\" or \"~\"",
486 NULL));
487
488 } else {
489
490 /* it's a determine-at-login-time filename -- check it later */
491 ;
492 }
493
494 c = add_config_param(cmd->argv[0], 0);
495
496 expr = (char *) cmd->argv[0];
497 group_acl = pr_expr_create(cmd->tmp_pool, &group_argc, &expr);
498
499 /* build the desired config_rec manually */
500 c->argc = group_argc + 2;
501 c->argv = pcalloc(c->pool, (group_argc + 3) * sizeof(char *));
502 group_argv = (char **) c->argv;
503
504 /* the access files are the first two arguments */
505 *group_argv++ = pstrdup(c->pool, allow_filename);
506 *group_argv++ = pstrdup(c->pool, deny_filename);
507
508 /* and the group names follow */
509 if (group_argc && group_acl)
510 while (group_argc--) {
511 *group_argv++ = pstrdup(c->pool, *((char **) group_acl->elts));
512 group_acl->elts = ((char **) group_acl->elts) + 1;
513 }
514
515 /* don't forget to NULL-terminate */
516 *group_argv = NULL;
517
518 c->flags |= CF_MERGEDOWN;
519
520 /* done */
521 return PR_HANDLED(cmd);
522 }
523
524 MODRET set_tcpuseraccessfiles(cmd_rec *cmd) {
525 unsigned int user_argc = 1;
526 char *expr, **user_argv = NULL;
527 array_header *user_acl = NULL;
528 config_rec *c = NULL;
529
530 /* assume use of the standard TCP wrappers installation locations */
531 char *allow_filename = NULL, *deny_filename = NULL;
532
533 CHECK_ARGS(cmd, 3);
534 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
535
536 /* use the user-given files, checking to make sure that they exist and
537 * are readable.
538 */
539 allow_filename = cmd->argv[2];
540 deny_filename = cmd->argv[3];
541
542 /* if the filenames begin with a '~', AND this is not immediately followed
543 * by a '/' (ie '~/'), expand it out for checking and storing for later
544 * lookups. If the filenames DO begin with '~/', do the expansion later,
545 * after authenication. In other words, do checking of static filenames
546 * now, and checking of dynamic (user-authentication-based) filenames
547 * later.
548 */
549 if (allow_filename[0] == '/') {
550
551 /* it's an absolute path, so the filename will be checked as is */
552 if (!wrap_is_usable_file(allow_filename))
553 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
554 cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL));
555
556 } else if (allow_filename[0] == '~' && allow_filename[1] != '/') {
557 char *allow_real_file = NULL;
558
559 allow_real_file = dir_realpath(cmd->pool, allow_filename);
560
561 if (allow_real_file == NULL || !wrap_is_usable_file(allow_real_file))
562 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
563 cmd->argv[0], ": '", allow_filename, "' must be a usable file", NULL));
564
565 allow_filename = allow_real_file;
566
567 } else if (allow_filename[0] != '~' && allow_filename[0] != '/') {
568
569 /* no relative paths allowed */
570 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
571 cmd->argv[0], ": '", allow_filename, "' must start with \"/\" or \"~\"",
572 NULL));
573
574 } else {
575
576 /* it's a determine-at-login-time filename -- check it later */
577 ;
578 }
579
580 if (deny_filename[0] == '/') {
581
582 /* it's an absolute path, so the filename will be checked as is */
583 if (!wrap_is_usable_file(deny_filename))
584 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
585 cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL));
586
587 } else if (deny_filename[0] == '~' && deny_filename[1] != '/') {
588 char *deny_real_file = NULL;
589
590 deny_real_file = dir_realpath(cmd->pool, deny_filename);
591
592 if (deny_real_file == NULL || !wrap_is_usable_file(deny_real_file))
593 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
594 cmd->argv[0], ": '", deny_filename, "' must be a usable file", NULL));
595
596 deny_filename = deny_real_file;
597
598 } else if (deny_filename[0] != '~' && deny_filename[0] != '/') {
599
600 /* no relative paths allowed */
601 return PR_ERROR_MSG(cmd, NULL, pstrcat(cmd->tmp_pool,
602 cmd->argv[0], ": '", deny_filename, "' must start with \"/\" or \"~\"",
603 NULL));
604
605 } else {
606
607 /* it's a determine-at-login-time filename -- check it later */
608 ;
609 }
610
611 c = add_config_param_str(cmd->argv[0], 0);
612
613 expr = (char *) cmd->argv[0];
614 user_acl = pr_expr_create(cmd->tmp_pool, &user_argc, &expr);
615
616 /* build the desired config_rec manually */
617 c->argc = user_argc + 2;
618 c->argv = pcalloc(c->pool, (user_argc + 3) * sizeof(char *));
619 user_argv = (char **) c->argv;
620
621 /* the access files are the first two arguments */
622 *user_argv++ = pstrdup(c->pool, allow_filename);
623 *user_argv++ = pstrdup(c->pool, deny_filename);
624
625 /* and the user names follow */
626 if (user_argc && user_acl)
627 while (user_argc--) {
628 *user_argv++ = pstrdup(c->pool, *((char **) user_acl->elts));
629 user_acl->elts = ((char **) user_acl->elts) + 1;
630 }
631
632 /* don't forget to NULL-terminate */
633 *user_argv = NULL;
634
635 c->flags |= CF_MERGEDOWN;
636
637 /* done */
638 return PR_HANDLED(cmd);
639 }
640
641 /* This function was copied, almost verbatim, from the set_sysloglevel()
642 * function in modules/mod_core.c. I hereby cite the source for this code
643 * as MacGuyver <[email protected]>. =)
644 */
645 MODRET set_tcpaccesssysloglevels(cmd_rec *cmd) {
646 config_rec *c = NULL;
647 int allow_level = PR_LOG_DEBUG, deny_level = PR_LOG_DEBUG;
648
649 CHECK_ARGS(cmd, 2);
650 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_ANON|CONF_GLOBAL);
651
652 if (strcasecmp(cmd->argv[1], "emerg") == 0) {
653 allow_level = PR_LOG_EMERG;
654
655 } else if (strcasecmp(cmd->argv[1], "alert") == 0) {
656 allow_level = PR_LOG_ALERT;
657
658 } else if (strcasecmp(cmd->argv[1], "crit") == 0) {
659 allow_level = PR_LOG_CRIT;
660
661 } else if (strcasecmp(cmd->argv[1], "error") == 0) {
662 allow_level = PR_LOG_ERR;
663
664 } else if (strcasecmp(cmd->argv[1], "warn") == 0) {
665 allow_level = PR_LOG_WARNING;
666
667 } else if (strcasecmp(cmd->argv[1], "notice") == 0) {
668 allow_level = PR_LOG_NOTICE;
669
670 } else if (strcasecmp(cmd->argv[1], "info") == 0) {
671 allow_level = PR_LOG_INFO;
672
673 } else if (strcasecmp(cmd->argv[1], "debug") == 0) {
674 allow_level = PR_LOG_DEBUG;
675
676 } else {
677 CONF_ERROR(cmd, "TCPAccessSyslogLevels requires \"allow\" level keyword: "
678 "one of emerg/alert/crit/error/warn/notice/info/debug");
679 }
680
681 if (strcasecmp(cmd->argv[2], "emerg") == 0) {
682 deny_level = PR_LOG_EMERG;
683
684 } else if (strcasecmp(cmd->argv[2], "alert") == 0) {
685 deny_level = PR_LOG_ALERT;
686
687 } else if (strcasecmp(cmd->argv[2], "crit") == 0) {
688 deny_level = PR_LOG_CRIT;
689
690 } else if (strcasecmp(cmd->argv[2], "error") == 0) {
691 deny_level = PR_LOG_ERR;
692
693 } else if (strcasecmp(cmd->argv[2], "warn") == 0) {
694 deny_level = PR_LOG_WARNING;
695
696 } else if (strcasecmp(cmd->argv[2], "notice") == 0) {
697 deny_level = PR_LOG_NOTICE;
698
699 } else if (strcasecmp(cmd->argv[2], "info") == 0) {
700 deny_level = PR_LOG_INFO;
701
702 } else if (strcasecmp(cmd->argv[2], "debug") == 0 ) {
703 deny_level = PR_LOG_DEBUG;
704
705 } else {
706 CONF_ERROR(cmd, "TCPAccessSyslogLevels requires \"deny\" level keyword: "
707 "one of emerg/alert/crit/error/warn/notice/info/debug");
708 }
709
710 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
711 c->argv[0] = palloc(c->pool, sizeof(int));
712 *((int *) c->argv[0]) = allow_level;
713 c->argv[1] = palloc(c->pool, sizeof(int));
714 *((int *) c->argv[1]) = deny_level;
715
716 c->flags |= CF_MERGEDOWN;
717
718 return PR_HANDLED(cmd);
719 }
720
721 /* usage: TCPServiceName <name> */
722 MODRET set_tcpservicename(cmd_rec *cmd) {
723 CHECK_ARGS(cmd, 1);
724 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
725
726 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
727 return PR_HANDLED(cmd);
728 }
729
730 /* Command handlers
731 */
732
733 MODRET wrap_handle_request(cmd_rec *cmd) {
734
735 /* these variables are names expected to be set by the TCP wrapper code
736 */
737 struct request_info request;
738
739 const char *user = NULL;
740 config_rec *conf = NULL, *access_conf = NULL, *syslog_conf = NULL;
741 hosts_allow_table = NULL;
742 hosts_deny_table = NULL;
743
744 /* hide passwords */
745 session.hide_password = TRUE;
746
747 /* Sneaky...found in mod_auth.c's cmd_pass() function. Need to find the
748 * login UID in order to resolve the possibly-login-dependent filename.
749 */
750 user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
751
752 /* It's possible that a PASS command came before USER. This is a PRE_CMD
753 * handler, so it won't be protected from this case; we'll need to do
754 * it manually.
755 */
756 if (user == NULL) {
757 return PR_DECLINED(cmd);
758 }
759
760 /* Use mod_auth's _auth_resolve_user() [imported for use here] to get the
761 * right configuration set, since the user may be logging in anonymously,
762 * and the session struct hasn't yet been set for that yet (thus short-
763 * circuiting the easiest way to get the right context...the macros.
764 */
765 conf = wrap_resolve_user(cmd->pool, &user);
766
767 /* Search first for user-specific access files. Multiple TCPUserAccessFiles
768 * directives are allowed.
769 */
770 if ((access_conf = find_config(conf ? conf->subset : CURRENT_CONF, CONF_PARAM,
771 "TCPUserAccessFiles", FALSE)) != NULL) {
772 int matched = FALSE;
773 array_header *user_array = NULL;
774
775 while (access_conf) {
776
777 user_array = make_array(cmd->tmp_pool, 0, sizeof(char *));
778 *((char **) push_array(user_array)) = pstrdup(cmd->tmp_pool, user);
779
780 /* Check the user expression -- don't forget the offset, to skip
781 * the access file name strings in argv
782 */
783 if (wrap_eval_expression(((char **) access_conf->argv) + 2,
784 user_array)) {
785 pr_log_debug(DEBUG4, MOD_WRAP_VERSION
786 ": matched TCPUserAccessFiles expression");
787 matched = TRUE;
788 break;
789 }
790
791 access_conf = find_config_next(access_conf, access_conf->next,
792 CONF_PARAM, "TCPUserAccessFiles", FALSE);
793 }
794
795 if (!matched)
796 access_conf = NULL;
797 }
798
799 /* Next, search for group-specific access files. Multiple
800 * TCPGroupAccessFiles directives are allowed.
801 */
802 if (!access_conf && (access_conf = find_config(conf ? conf->subset :
803 CURRENT_CONF, CONF_PARAM, "TCPGroupAccessFiles", FALSE)) != NULL) {
804 unsigned char matched = FALSE;
805
806 /* NOTE: this gid_array is only necessary until Bug#1461 is fixed */
807 array_header *gid_array = make_array(cmd->pool, 0, sizeof(gid_t));
808
809 array_header *group_array = make_array(cmd->pool, 0, sizeof(char *));
810
811 while (access_conf) {
812 if (pr_auth_getgroups(cmd->pool, user, &gid_array, &group_array) < 1) {
813 pr_log_debug(DEBUG3, MOD_WRAP_VERSION
814 ": no supplemental groups found for user '%s'", user);
815
816 } else {
817
818 /* Check the group expression -- don't forget the offset, to skip
819 * the access file names strings in argv
820 */
821 if (wrap_eval_expression(((char **) access_conf->argv) + 2,
822 group_array)) {
823 pr_log_debug(DEBUG4, MOD_WRAP_VERSION
824 ": matched TCPGroupAccessFiles expression");
825 matched = TRUE;
826 break;
827 }
828 }
829
830 access_conf = find_config_next(access_conf, access_conf->next,
831 CONF_PARAM, "TCPGroupAccessFiles", FALSE);
832 }
833
834 if (!matched)
835 access_conf = NULL;
836 }
837
838 /* Finally for globally-applicable access files. Only one such directive
839 * is allowed.
840 */
841 if (!access_conf) {
842 access_conf = find_config(conf ? conf->subset : CURRENT_CONF,
843 CONF_PARAM, "TCPAccessFiles", FALSE);
844 }
845
846 if (access_conf) {
847 hosts_allow_table = (char *) access_conf->argv[0];
848 hosts_deny_table = (char *) access_conf->argv[1];
849 }
850
851 /* Now, check the retrieved filename, and see if it requires a login-time
852 * file.
853 */
854 if (hosts_allow_table != NULL && hosts_allow_table[0] == '~' &&
855 hosts_allow_table[1] == '/') {
856 const char *allow_real_table = NULL;
857
858 allow_real_table = wrap_get_user_table(cmd, user, hosts_allow_table);
859
860 if (!wrap_is_usable_file(allow_real_table)) {
861 pr_log_pri(PR_LOG_WARNING, MOD_WRAP_VERSION
862 ": configured TCPAllowFile %s is unusable", hosts_allow_table);
863 hosts_allow_table = NULL;
864
865 } else
866 hosts_allow_table = (char *) allow_real_table;
867 }
868
869 if (hosts_deny_table != NULL && hosts_deny_table[0] == '~' &&
870 hosts_deny_table[1] == '/') {
871 char *deny_real_table = NULL;
872
873 deny_real_table = dir_realpath(cmd->pool, hosts_deny_table);
874
875 if (!wrap_is_usable_file(deny_real_table)) {
876 pr_log_pri(PR_LOG_WARNING, MOD_WRAP_VERSION
877 ": configured TCPDenyFile %s is unusable", hosts_deny_table);
878 hosts_deny_table = NULL;
879
880 } else {
881 hosts_deny_table = deny_real_table;
882 }
883 }
884
885 /* Make sure that _both_ allow and deny TCPAccessFiles are present.
886 * If not, log the missing file, and by default allow request to succeed.
887 */
888 if (hosts_allow_table != NULL && hosts_deny_table != NULL) {
889
890 /* Most common case...nothing more necessary */
891
892 } else if (hosts_allow_table == NULL && hosts_deny_table != NULL) {
893
894 /* Log the missing file */
895 pr_log_pri(PR_LOG_INFO, MOD_WRAP_VERSION ": no usable allow access file -- "
896 "allowing connection");
897
898 return PR_DECLINED(cmd);
899
900 } else if (hosts_allow_table != NULL && hosts_deny_table == NULL) {
901
902 /* log the missing file */
903 pr_log_pri(PR_LOG_INFO, MOD_WRAP_VERSION ": no usable deny access file -- "
904 "allowing connection");
905
906 return PR_DECLINED(cmd);
907
908 } else {
909
910 /* Neither set -- assume the admin hasn't configured these directives
911 * at all.
912 */
913 return PR_DECLINED(cmd);
914 }
915
916 /* Log the names of the allow/deny files being used. */
917 pr_log_pri(PR_LOG_DEBUG, MOD_WRAP_VERSION ": using access files: %s, %s",
918 hosts_allow_table, hosts_deny_table);
919
920 /* retrieve the user-defined syslog priorities, if any. Fall back to the
921 * defaults as seen in tcpd.h if not defined.
922 */
923 syslog_conf = find_config(main_server->conf, CONF_PARAM,
924 "TCPAccessSyslogLevels", FALSE);
925
926 if (syslog_conf) {
927 allow_severity = *((int *) syslog_conf->argv[0]);
928 deny_severity = *((int *) syslog_conf->argv[1]);
929
930 } else {
931 allow_severity = PR_LOG_INFO;
932 deny_severity = PR_LOG_WARNING;
933 }
934
935 /* While it may look odd to OR together the syslog facility and level,
936 * that is the way that syslog(3) says to do it:
937 *
938 * "The priority argument is formed by ORing the facility and the level
939 * values..."
940 *
941 * Note that we do this OR here because the allow_severity/deny_severity
942 * values are ALSO used by the libwrap library; it is also why we need
943 * to mask off some bits later, when using proftpd's logging functions.
944 */
945 allow_severity = log_getfacility() | allow_severity;
946 deny_severity = log_getfacility() | deny_severity;
947
948 pr_log_debug(DEBUG4, MOD_WRAP_VERSION ": checking under service name '%s'",
949 wrap_service_name);
950 request_init(&request, RQ_DAEMON, wrap_service_name, RQ_FILE,
951 session.c->rfd, 0);
952
953 fromhost(&request);
954
•955 if (!hosts_access(&request)) {
956 char *denymsg = NULL;
957
958 /* log the denied connection */
959 wrap_log_request_denied(deny_severity, &request);
960
961 /* Broadcast this event to any interested listeners. */
962 pr_event_generate("mod_wrap.connection-denied", NULL);
963
964 /* check for AccessDenyMsg */
965 denymsg = (char *) get_param_ptr(TOPLEVEL_CONF, "AccessDenyMsg", FALSE);
966 if (denymsg != NULL) {
967 denymsg = (char *) sreplace(cmd->tmp_pool, denymsg, "%u", user, NULL);
968 }
969
970 if (denymsg != NULL) {
971 return PR_ERROR_MSG(cmd, R_530, denymsg);
972 }
973
974 return PR_ERROR_MSG(cmd, R_530, _("Access denied"));
975 }
976
977 /* If request is allowable, return DECLINED (for engine to act as if this
978 * handler was never called, else ERROR (for engine to abort processing and
979 * deny request.
980 */
981 wrap_log_request_allowed(allow_severity, &request);
982
983 return PR_DECLINED(cmd);
984 }
985
986 /* Event listeners
987 */
988
989 static void wrap_sess_reinit_ev(const void *event_data, void *user_data) {
990 int res;
991
992 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
993
994 pr_event_unregister(&wrap_module, "core.session-reinit", wrap_sess_reinit_ev);
995
996 /* Reset defaults */
997 wrap_service_name = "proftpd";
998
999 res = wrap_sess_init();
1000 if (res < 0) {
1001 pr_session_disconnect(&wrap_module, PR_SESS_DISCONNECT_SESSION_INIT_FAILED,
1002 NULL);
1003 }
1004 }
1005
1006 /* Initialization routines
1007 */
1008
1009 static int wrap_sess_init(void) {
1010 pr_event_register(&wrap_module, "core.session-reinit", wrap_sess_reinit_ev,
1011 NULL);
1012
1013 /* look up any configured TCPServiceName */
1014 wrap_service_name = get_param_ptr(main_server->conf, "TCPServiceName", FALSE);
1015 if (wrap_service_name == NULL) {
1016 wrap_service_name = "proftpd";
1017 }
1018
1019 return 0;
1020 }
1021
1022 /* Module API tables
1023 */
1024
1025 static conftable wrap_conftab[] = {
1026 { "TCPAccessFiles", set_tcpaccessfiles, NULL },
1027 { "TCPAccessSyslogLevels", set_tcpaccesssysloglevels, NULL },
1028 { "TCPGroupAccessFiles", set_tcpgroupaccessfiles, NULL },
1029 { "TCPServiceName", set_tcpservicename, NULL },
1030 { "TCPUserAccessFiles", set_tcpuseraccessfiles, NULL },
1031 { NULL }
1032 };
1033
1034 static cmdtable wrap_cmdtab[] = {
1035 { PRE_CMD, C_PASS, G_NONE, wrap_handle_request, FALSE, FALSE },
1036 { 0, NULL }
1037 };
1038
1039 module wrap_module = {
1040 NULL, NULL,
1041
1042 /* Module API version 2.0 */
1043 0x20,
1044
1045 /* Module name */
1046 "wrap",
1047
1048 /* Mmodule configuration handler table */
1049 wrap_conftab,
1050
1051 /* Module command handler table */
1052 wrap_cmdtab,
1053
1054 /* Module authentication handler table */
1055 NULL,
1056
1057 /* Module initialization */
1058 NULL,
1059
1060 /* Session initialization */
1061 wrap_sess_init,
1062
1063 /* Module version */
1064 MOD_WRAP_VERSION
1065 };