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