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