Debian Codegraph @

options.c

raw ◀ ▶
Source reconstructed at patch state 0 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 /*
2 * General skeleton for adding options to the access control language. The
3 * features offered by this module are documented in the hosts_options(5)
4 * manual page (source file: hosts_options.5, "nroff -man" format).
5 *
6 * Notes and warnings for those who want to add features:
7 *
8 * In case of errors, abort options processing and deny access. There are too
9 * many irreversible side effects to make error recovery feasible. For
10 * example, it makes no sense to continue after we have already changed the
11 * userid.
12 *
13 * In case of errors, do not terminate the process: the routines might be
14 * called from a long-running daemon that should run forever. Instead, call
15 * tcpd_jump() which does a non-local goto back into the hosts_access()
16 * routine.
17 *
18 * In case of severe errors, use clean_exit() instead of directly calling
19 * exit(), or the inetd may loop on an UDP request.
20 *
21 * In verification mode (for example, with the "tcpdmatch" command) the
22 * "dry_run" flag is set. In this mode, an option function should just "say"
23 * what it is going to do instead of really doing it.
24 *
25 * Some option functions do not return (for example, the twist option passes
26 * control to another program). In verification mode (dry_run flag is set)
27 * such options should clear the "dry_run" flag to inform the caller of this
28 * course of action.
29 */
30
31 #ifndef lint
32 static char sccsid[] = "@(#) options.c 1.17 96/02/11 17:01:31";
33 #endif
34
35 /* System libraries. */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <netinet/in.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <syslog.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <ctype.h>
50 #include <setjmp.h>
51 #include <string.h>
52 #include <sys/wait.h>
53
54 #ifndef MAXPATHNAMELEN
55 #define MAXPATHNAMELEN BUFSIZ
56 #endif
57
58 /* Local stuff. */
59
60 #include "tcpd.h"
61
62 /* Options runtime support. */
63
64 int dry_run = 0; /* flag set in verification mode */
65 extern jmp_buf tcpd_buf; /* tcpd_jump() support */
66
67 /* Options parser support. */
68
69 static char whitespace_eq[] = "= \t\r\n";
70 #define whitespace (whitespace_eq + 1)
71
72 static char *get_field(char *); /* chew :-delimited field off string */
73 static char *chop_string(register char *); /* strip leading and trailing blanks */
74
75 /* List of functions that implement the options. Add yours here. */
76
77 static void user_option(char *, struct request_info *); /* execute "user name.group" option */
78 static void group_option(char *, struct request_info *); /* execute "group name" option */
79 static void umask_option(char *, struct request_info *); /* execute "umask mask" option */
80 static void linger_option(char *, struct request_info *); /* execute "linger time" option */
81 static void keepalive_option(char *, struct request_info *); /* execute "keepalive" option */
82 static void aclexec_option(char *, struct request_info *); /* execute "aclexec command" option */
83 static void spawn_option(char *, struct request_info *); /* execute "spawn command" option */
84 static void twist_option(char *, struct request_info *); /* execute "twist command" option */
85 static void rfc931_option(char *, struct request_info *); /* execute "rfc931" option */
86 static void setenv_option(char *, struct request_info *); /* execute "setenv name value" */
87 static void nice_option(char *, struct request_info *); /* execute "nice" option */
88 static void severity_option(char *, struct request_info *); /* execute "severity value" */
89 static void allow_option(char *, struct request_info *); /* execute "allow" option */
90 static void deny_option(char *, struct request_info *); /* execute "deny" option */
91 static void banners_option(char *, struct request_info *); /* execute "banners path" option */
92
93 /* Structure of the options table. */
94
95 struct option {
96 char *name; /* keyword name, case is ignored */
97 void (*func) (char *, struct request_info *); /* function that does the real work */
98 int flags; /* see below... */
99 };
100
101 #define NEED_ARG (1<<1) /* option requires argument */
102 #define USE_LAST (1<<2) /* option must be last */
103 #define OPT_ARG (1<<3) /* option has optional argument */
104 #define EXPAND_ARG (1<<4) /* do %x expansion on argument */
105
106 #define need_arg(o) ((o)->flags & NEED_ARG)
107 #define opt_arg(o) ((o)->flags & OPT_ARG)
108 #define permit_arg(o) ((o)->flags & (NEED_ARG | OPT_ARG))
109 #define use_last(o) ((o)->flags & USE_LAST)
110 #define expand_arg(o) ((o)->flags & EXPAND_ARG)
111
112 /* List of known keywords. Add yours here. */
113
114 static struct option option_table[] = {
115 "user", user_option, NEED_ARG,
116 "group", group_option, NEED_ARG,
117 "umask", umask_option, NEED_ARG,
118 "linger", linger_option, NEED_ARG,
119 "keepalive", keepalive_option, 0,
120 #ifdef ACLEXEC
121 "aclexec", aclexec_option, NEED_ARG | EXPAND_ARG,
122 #endif
123 "spawn", spawn_option, NEED_ARG | EXPAND_ARG,
124 "twist", twist_option, NEED_ARG | EXPAND_ARG | USE_LAST,
125 "rfc931", rfc931_option, OPT_ARG,
126 "setenv", setenv_option, NEED_ARG | EXPAND_ARG,
127 "nice", nice_option, OPT_ARG,
128 "severity", severity_option, NEED_ARG,
129 "allow", allow_option, USE_LAST,
130 "deny", deny_option, USE_LAST,
131 "banners", banners_option, NEED_ARG,
132 0,
133 };
134
135 /* process_options - process access control options */
136
137 void process_options(char *options, struct request_info *request)
138 {
139 char *key;
140 char *value;
141 char *curr_opt;
142 char *next_opt;
143 struct option *op;
144 char bf[BUFSIZ];
145
146 for (curr_opt = get_field(options); curr_opt; curr_opt = next_opt) {
147 next_opt = get_field((char *) 0);
148
149 /*
150 * Separate the option into name and value parts. For backwards
151 * compatibility we ignore exactly one '=' between name and value.
152 */
153 curr_opt = chop_string(curr_opt);
154 if (*(value = curr_opt + strcspn(curr_opt, whitespace_eq))) {
155 if (*value != '=') {
156 *value++ = 0;
157 value += strspn(value, whitespace);
158 }
159 if (*value == '=') {
160 *value++ = 0;
161 value += strspn(value, whitespace);
162 }
163 }
164 if (*value == 0)
165 value = 0;
166 key = curr_opt;
167
168 /*
169 * Disallow missing option names (and empty option fields).
170 */
171 if (*key == 0)
172 tcpd_jump("missing option name");
173
174 /*
175 * Lookup the option-specific info and do some common error checks.
176 * Delegate option-specific processing to the specific functions.
177 */
178
179 for (op = option_table; op->name && STR_NE(op->name, key); op++)
180 /* VOID */ ;
181 if (op->name == 0)
182 tcpd_jump("bad option name: \"%s\"", key);
183 if (!value && need_arg(op))
184 tcpd_jump("option \"%s\" requires value", key);
185 if (value && !permit_arg(op))
186 tcpd_jump("option \"%s\" requires no value", key);
187 if (next_opt && use_last(op))
188 tcpd_jump("option \"%s\" must be at end", key);
189 if (value && expand_arg(op))
190 value = chop_string(percent_x(bf, sizeof(bf), value, request));
191 if (hosts_access_verbose)
192 syslog(LOG_DEBUG, "option: %s %s", key, value ? value : "");
193 (*(op->func)) (value, request);
194 }
195 }
196
197 /* allow_option - grant access */
198
199 /* ARGSUSED */
200
201 static void allow_option(char *value, struct request_info *request)
202 {
203 longjmp(tcpd_buf, AC_PERMIT);
204 }
205
206 /* deny_option - deny access */
207
208 /* ARGSUSED */
209
210 static void deny_option(char *value, struct request_info *request)
211 {
212 longjmp(tcpd_buf, AC_DENY);
213 }
214
215 /* banners_option - expand %<char>, terminate each line with CRLF */
216
217 static void banners_option(char *value, struct request_info *request)
218 {
219 char path[MAXPATHNAMELEN];
220 char ibuf[BUFSIZ];
221 char obuf[2 * BUFSIZ];
222 struct stat st;
223 int ch;
224 FILE *fp;
225
226 sprintf(path, "%s/%s", value, eval_daemon(request));
227 if ((fp = fopen(path, "r")) != 0) {
228 while ((ch = fgetc(fp)) == 0)
229 write(request->fd, "", 1);
230 ungetc(ch, fp);
231 while (fgets(ibuf, sizeof(ibuf) - 1, fp)) {
232 if (split_at(ibuf, '\n'))
233 strcat(ibuf, "\r\n");
234 percent_x(obuf, sizeof(obuf), ibuf, request);
235 write(request->fd, obuf, strlen(obuf));
236 }
237 fclose(fp);
238 } else if (stat(value, &st) < 0) {
239 tcpd_warn("%s: %m", value);
240 }
241 }
242
243 /* group_option - switch group id */
244
245 /* ARGSUSED */
246
247 static void group_option(char *value, struct request_info *request)
248 {
249 struct group *grp;
250
251 if ((grp = getgrnam(value)) == 0)
252 tcpd_jump("unknown group: \"%s\"", value);
253 endgrent();
254
255 if (dry_run != 0) {
256 if (setgid(grp->gr_gid))
257 tcpd_jump("setgid(%s): %m", value);
258 if (setgroups(0, NULL))
259 tcpd_jump("setgroups(%s): %m", value);
260 }
261 }
262
263 /* user_option - switch user id */
264
265 /* ARGSUSED */
266
267 static void user_option(char *value, struct request_info *request)
268 {
269 struct passwd *pwd;
270 char *group;
271 int defaultgroup = 0;
272
273 if ((group = split_at(value, '.')) != 0)
274 group_option(group, request);
275 else
276 defaultgroup = 1;
277 if ((pwd = getpwnam(value)) == 0)
278 tcpd_jump("unknown user: \"%s\"", value);
279 endpwent();
280
281 if (dry_run != 0) {
282 if (setuid(pwd->pw_uid))
283 tcpd_jump("setuid(%s): %m", value);
284 if (defaultgroup) {
285 if (setgid(pwd->pw_gid))
286 tcpd_jump("setgid(%s): %m", value);
287 if (initgroups(value, pwd->pw_gid))
288 tcpd_jump("initgroups(%s): %m", value);
289 }
290 }
291 }
292
293 /* umask_option - set file creation mask */
294
295 /* ARGSUSED */
296
297 static void umask_option(char *value, struct request_info *request)
298 {
299 unsigned mask;
300 char junk;
301
302 if (sscanf(value, "%o%c", &mask, &junk) != 1 || (mask & 0777) != mask)
303 tcpd_jump("bad umask value: \"%s\"", value);
304 (void) umask(mask);
305 }
306
307 /* spawn_option - spawn a shell command and wait */
308
309 /* ARGSUSED */
310
311 static void spawn_option(char *value, struct request_info *request)
312 {
313 if (dry_run == 0)
314 shell_cmd(value);
315 }
316
317 #ifdef ACLEXEC
318 /* aclexec_option - spawn a shell command and check status */
319
320 /* ARGSUSED */
321
322 static void aclexec_option(char *value, struct request_info *request)
323 {
324 int status, child_pid, wait_pid;
325 extern int aclexec_matched;
326
327 if (dry_run != 0)
328 return;
329
330 child_pid = fork();
331
332 /* Something went wrong: we MUST terminate the process. */
333 if (child_pid < 0) {
334 tcpd_warn("aclexec_option: /bin/sh: %m");
335 clean_exit(request);
336 }
337
338 if (child_pid == 0) {
339 execl("/bin/sh", "sh", "-c", value, (char *) 0);
340
341 /* Something went wrong. We MUST terminate the child process. */
342 tcpd_warn("execl /bin/sh: %m");
343 _exit(0);
344 }
345
346 while ((wait_pid = wait(&status)) != -1 && wait_pid != child_pid)
347 /* void */ ;
348
349 aclexec_matched = 1;
350
351 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
352 aclexec_matched = 0;
353 }
354
355 if (WIFSIGNALED(status))
356 tcpd_warn("process %d exited with signal %d", child_pid,
357 WTERMSIG(status));
358
359 return;
360 }
361 #endif
362
363 /* linger_option - set the socket linger time (Marc Boucher <[email protected]>) */
364
365 /* ARGSUSED */
366
367 static void linger_option(char *value, struct request_info *request)
368 {
369 struct linger linger;
370 char junk;
371
372 if (sscanf(value, "%d%c", &linger.l_linger, &junk) != 1
373 || linger.l_linger < 0)
374 tcpd_jump("bad linger value: \"%s\"", value);
375 if (dry_run == 0) {
376 linger.l_onoff = (linger.l_linger != 0);
377 if (setsockopt(request->fd, SOL_SOCKET, SO_LINGER, (char *) &linger,
378 sizeof(linger)) < 0)
379 tcpd_warn("setsockopt SO_LINGER %d: %m", linger.l_linger);
380 }
381 }
382
383 /* keepalive_option - set the socket keepalive option */
384
385 /* ARGSUSED */
386
387 static void keepalive_option(char *value, struct request_info *request)
388 {
389 static int on = 1;
390
391 if (dry_run == 0 && setsockopt(request->fd, SOL_SOCKET, SO_KEEPALIVE,
392 (char *) &on, sizeof(on)) < 0)
393 tcpd_warn("setsockopt SO_KEEPALIVE: %m");
394 }
395
396 /* nice_option - set nice value */
397
398 /* ARGSUSED */
399
400 static void nice_option(char *value, struct request_info *request)
401 {
402 int niceval = 10;
403 char junk;
404
405 if (value != 0 && sscanf(value, "%d%c", &niceval, &junk) != 1)
406 tcpd_jump("bad nice value: \"%s\"", value);
407 if (dry_run == 0 && nice(niceval) < 0)
408 tcpd_warn("nice(%d): %m", niceval);
409 }
410
411 /* twist_option - replace process by shell command */
412
413 static void twist_option(char *value, struct request_info *request)
414 {
415 char *error;
416
417 if (dry_run != 0) {
418 dry_run = 0;
419 } else {
420 if (resident > 0)
421 tcpd_jump("twist option in resident process");
422
423 syslog(deny_severity, "twist %s to %s", eval_client(request), value);
424
425 /* Before switching to the shell, set up stdin, stdout and stderr. */
426
427 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
428
429 if (maybe_dup2(request->fd, 0) != 0 ||
430 maybe_dup2(request->fd, 1) != 1 ||
431 maybe_dup2(request->fd, 2) != 2) {
432 error = "twist_option: dup: %m";
433 } else {
434 if (request->fd > 2)
435 close(request->fd);
436 (void) execl("/bin/sh", "sh", "-c", value, (char *) 0);
437 error = "twist_option: /bin/sh: %m";
438 }
439
440 /* Something went wrong: we MUST terminate the process. */
441
442 tcpd_warn(error);
443 clean_exit(request);
444 }
445 }
446
447 /* rfc931_option - look up remote user name */
448
449 static void rfc931_option(char *value, struct request_info *request)
450 {
451 int timeout;
452 char junk;
453
454 if (value != 0) {
455 if (sscanf(value, "%d%c", &timeout, &junk) != 1 || timeout <= 0)
456 tcpd_jump("bad rfc931 timeout: \"%s\"", value);
457 rfc931_timeout = timeout;
458 }
459 (void) eval_user(request);
460 }
461
462 /* setenv_option - set environment variable */
463
464 /* ARGSUSED */
465
466 static void setenv_option(char *value, struct request_info *request)
467 {
468 char *var_value;
469
470 if (*(var_value = value + strcspn(value, whitespace)))
471 *var_value++ = 0;
472 if (setenv(chop_string(value), chop_string(var_value), 1))
473 tcpd_jump("memory allocation failure");
474 }
475
476 /*
477 * The severity option goes last because it comes with a huge amount of ugly
478 * #ifdefs and tables.
479 */
480
481 struct syslog_names {
482 char *name;
483 int value;
484 };
485
486 static struct syslog_names log_fac[] = {
487 #ifdef LOG_KERN
488 "kern", LOG_KERN,
489 #endif
490 #ifdef LOG_USER
491 "user", LOG_USER,
492 #endif
493 #ifdef LOG_MAIL
494 "mail", LOG_MAIL,
495 #endif
496 #ifdef LOG_DAEMON
497 "daemon", LOG_DAEMON,
498 #endif
499 #ifdef LOG_AUTH
500 "auth", LOG_AUTH,
501 #endif
502 #ifdef LOG_LPR
503 "lpr", LOG_LPR,
504 #endif
505 #ifdef LOG_NEWS
506 "news", LOG_NEWS,
507 #endif
508 #ifdef LOG_UUCP
509 "uucp", LOG_UUCP,
510 #endif
511 #ifdef LOG_CRON
512 "cron", LOG_CRON,
513 #endif
514 #ifdef LOG_LOCAL0
515 "local0", LOG_LOCAL0,
516 #endif
517 #ifdef LOG_LOCAL1
518 "local1", LOG_LOCAL1,
519 #endif
520 #ifdef LOG_LOCAL2
521 "local2", LOG_LOCAL2,
522 #endif
523 #ifdef LOG_LOCAL3
524 "local3", LOG_LOCAL3,
525 #endif
526 #ifdef LOG_LOCAL4
527 "local4", LOG_LOCAL4,
528 #endif
529 #ifdef LOG_LOCAL5
530 "local5", LOG_LOCAL5,
531 #endif
532 #ifdef LOG_LOCAL6
533 "local6", LOG_LOCAL6,
534 #endif
535 #ifdef LOG_LOCAL7
536 "local7", LOG_LOCAL7,
537 #endif
538 0,
539 };
540
541 static struct syslog_names log_sev[] = {
542 #ifdef LOG_EMERG
543 "emerg", LOG_EMERG,
544 #endif
545 #ifdef LOG_ALERT
546 "alert", LOG_ALERT,
547 #endif
548 #ifdef LOG_CRIT
549 "crit", LOG_CRIT,
550 #endif
551 #ifdef LOG_ERR
552 "err", LOG_ERR,
553 #endif
554 #ifdef LOG_WARNING
555 "warning", LOG_WARNING,
556 #endif
557 #ifdef LOG_NOTICE
558 "notice", LOG_NOTICE,
559 #endif
560 #ifdef LOG_INFO
561 "info", LOG_INFO,
562 #endif
563 #ifdef LOG_DEBUG
564 "debug", LOG_DEBUG,
565 #endif
566 0,
567 };
568
569 /* severity_map - lookup facility or severity value */
570
571 static int severity_map(struct syslog_names *table, char *name)
572 {
573 struct syslog_names *t;
574
575 for (t = table; t->name; t++)
576 if (STR_EQ(t->name, name))
577 return (t->value);
578 tcpd_jump("bad syslog facility or severity: \"%s\"", name);
579 /* NOTREACHED */
580 }
581
582 /* severity_option - change logging severity for this event (Dave Mitchell) */
583
584 /* ARGSUSED */
585
586 static void severity_option(char *value, struct request_info *request)
587 {
588 char *level = split_at(value, '.');
589
590 allow_severity = deny_severity = level ?
591 severity_map(log_fac, value) | severity_map(log_sev, level) :
592 severity_map(log_sev, value);
593 }
594
595 /* get_field - return pointer to next field in string */
596
597 static char *get_field(char *string)
598 {
599 static char *last = "";
600 char *src;
601 char *dst;
602 char *ret;
603 int ch;
604
605 /*
606 * This function returns pointers to successive fields within a given
607 * string. ":" is the field separator; warn if the rule ends in one. It
608 * replaces a "\:" sequence by ":", without treating the result of
609 * substitution as field terminator. A null argument means resume search
610 * where the previous call terminated. This function destroys its
611 * argument.
612 *
613 * Work from explicit source or from memory. While processing \: we
614 * overwrite the input. This way we do not have to maintain buffers for
615 * copies of input fields.
616 */
617
618 src = dst = ret = (string ? string : last);
619 if (src[0] == 0)
620 return (0);
621
622 while (ch = *src) {
623 if (ch == ':') {
624 if (*++src == 0)
625 tcpd_warn("rule ends in \":\"");
626 break;
627 }
628 if (ch == '\\' && src[1] == ':')
629 src++;
630 *dst++ = *src++;
631 }
632 last = src;
633 *dst = 0;
634 return (ret);
635 }
636
637 /* chop_string - strip leading and trailing blanks from string */
638
639 static char *chop_string(register char *string)
640 {
641 char *start = 0;
642 char *end;
643 char *cp;
644
645 for (cp = string; *cp; cp++) {
646 if (!isspace(*cp)) {
647 if (start == 0)
648 start = cp;
649 end = cp;
650 }
651 }
652 return (start ? (end[1] = 0, start) : cp);
653 }