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