1 /* $NetBSD: options.c,v 1.13 2005/04/19 03:38:19 lukem Exp $ */
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).
8 * Notes and warnings for those who want to add features:
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
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()
20 * In case of severe errors, use clean_exit() instead of directly calling
21 * exit(), or the inetd may loop on an UDP request.
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.
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
33 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#) options.c 1.17 96/02/11 17:01:31";
38 __RCSID("$NetBSD: options.c,v 1.13 2005/04/19 03:38:19 lukem Exp $");
42 /* System libraries. */
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
64 /* Options runtime support. */
66 int dry_run
= 0; /* flag set in verification mode */
67 extern jmp_buf tcpd_buf
; /* tcpd_jump() support */
69 /* Options parser support. */
71 static char whitespace_eq
[] = "= \t\r\n";
72 #define whitespace (whitespace_eq + 1)
74 static char *get_field
/* chew :-delimited field off string */
76 static char *chop_string
/* strip leading and trailing blanks */
79 static int severity_map
80 __P((struct syslog_names
*, char *));
82 /* List of functions that implement the options. Add yours here. */
84 static void user_option
/* execute "user name.group" option */
85 __P((char *, struct request_info
*));
86 static void group_option
/* execute "group name" option */
87 __P((char *, struct request_info
*));
88 static void umask_option
/* execute "umask mask" option */
89 __P((char *, struct request_info
*));
90 static void linger_option
/* execute "linger time" option */
91 __P((char *, struct request_info
*));
92 static void keepalive_option
/* execute "keepalive" option */
93 __P((char *, struct request_info
*));
94 static void spawn_option
/* execute "spawn command" option */
95 __P((char *, struct request_info
*));
96 static void twist_option
/* execute "twist command" option */
97 __P((char *, struct request_info
*));
98 static void rfc931_option
/* execute "rfc931" option */
99 __P((char *, struct request_info
*));
100 static void setenv_option
/* execute "setenv name value" */
101 __P((char *, struct request_info
*));
102 static void nice_option
/* execute "nice" option */
103 __P((char *, struct request_info
*));
104 static void severity_option
/* execute "severity value" */
105 __P((char *, struct request_info
*));
106 static void allow_option
/* execute "allow" option */
107 __P((char *, struct request_info
*));
108 static void deny_option
/* execute "deny" option */
109 __P((char *, struct request_info
*));
110 static void banners_option
/* execute "banners path" option */
111 __P((char *, struct request_info
*));
113 /* Structure of the options table. */
116 char *name
; /* keyword name, case is ignored */
117 void (*func
) /* function that does the real work */
118 __P((char *, struct request_info
*));
119 int flags
; /* see below... */
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 */
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)
133 /* List of known keywords. Add yours here. */
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
},
153 /* process_options - process access control options */
155 void process_options(options
, request
)
157 struct request_info
*request
;
166 for (curr_opt
= get_field(options
); curr_opt
; curr_opt
= next_opt
) {
167 next_opt
= get_field((char *) 0);
170 * Separate the option into name and value parts. For backwards
171 * compatibility we ignore exactly one '=' between name and value.
173 curr_opt
= chop_string(curr_opt
);
174 if (*(value
= curr_opt
+ strcspn(curr_opt
, whitespace_eq
))) {
177 value
+= strspn(value
, whitespace
);
181 value
+= strspn(value
, whitespace
);
189 * Disallow missing option names (and empty option fields).
192 tcpd_jump("missing option name");
195 * Lookup the option-specific info and do some common error checks.
196 * Delegate option-specific processing to the specific functions.
199 for (op
= option_table
; op
->name
&& STR_NE(op
->name
, key
); op
++)
202 tcpd_jump("bad option name: \"%s\"", key
);
203 if (!value
&& need_arg(op
))
204 tcpd_jump("option \"%s\" requires value", key
);
205 if (value
&& !permit_arg(op
))
206 tcpd_jump("option \"%s\" requires no value", key
);
207 if (next_opt
&& use_last(op
))
208 tcpd_jump("option \"%s\" must be at end", key
);
209 if (value
&& expand_arg(op
))
210 value
= chop_string(percent_x(bf
, sizeof(bf
), value
, request
));
211 if (hosts_access_verbose
)
212 syslog(LOG_DEBUG
, "option: %s %s", key
, value
? value
: "");
213 (*(op
->func
)) (value
, request
);
217 /* allow_option - grant access */
221 static void allow_option(value
, request
)
223 struct request_info
*request
;
225 longjmp(tcpd_buf
, AC_PERMIT
);
228 /* deny_option - deny access */
232 static void deny_option(value
, request
)
234 struct request_info
*request
;
236 longjmp(tcpd_buf
, AC_DENY
);
239 /* banners_option - expand %<char>, terminate each line with CRLF */
241 static void banners_option(value
, request
)
243 struct request_info
*request
;
245 char path
[MAXPATHLEN
];
247 char obuf
[2 * BUFSIZ
];
252 (void)snprintf(path
, sizeof path
, "%s/%s", value
, eval_daemon(request
));
253 if ((fp
= fopen(path
, "r")) != 0) {
254 while ((ch
= fgetc(fp
)) == 0)
255 write(request
->fd
, "", 1);
257 while (fgets(ibuf
, sizeof(ibuf
) - 2, fp
)) {
258 if (split_at(ibuf
, '\n'))
259 strcat(ibuf
, "\r\n"); /* XXX strcat is safe */
260 percent_x(obuf
, sizeof(obuf
), ibuf
, request
);
261 write(request
->fd
, obuf
, strlen(obuf
));
264 } else if (stat(value
, &st
) < 0) {
265 tcpd_warn("%s: %m", value
);
269 /* group_option - switch group id */
273 static void group_option(value
, request
)
275 struct request_info
*request
;
277 struct group grs
, *grp
;
280 (void)getgrnam_r(value
, &grs
, grbuf
, sizeof(grbuf
), &grp
);
282 tcpd_jump("unknown group: \"%s\"", value
);
284 if (dry_run
== 0 && setgid(grp
->gr_gid
))
285 tcpd_jump("setgid(%s): %m", value
);
288 /* user_option - switch user id */
292 static void user_option(value
, request
)
294 struct request_info
*request
;
296 struct passwd
*pwd
, pws
;
300 if ((group
= split_at(value
, '.')) != 0)
301 group_option(group
, request
);
302 (void)getpwnam_r(value
, &pws
, pwbuf
, sizeof(pwbuf
), &pwd
);
304 tcpd_jump("unknown user: \"%s\"", value
);
306 if (dry_run
== 0 && setuid(pwd
->pw_uid
))
307 tcpd_jump("setuid(%s): %m", value
);
310 /* umask_option - set file creation mask */
314 static void umask_option(value
, request
)
316 struct request_info
*request
;
321 if (sscanf(value
, "%o%c", &mask
, &junk
) != 1 || (mask
& 0777) != mask
)
322 tcpd_jump("bad umask value: \"%s\"", value
);
326 /* spawn_option - spawn a shell command and wait */
330 static void spawn_option(value
, request
)
332 struct request_info
*request
;
338 /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
342 static void linger_option(value
, request
)
344 struct request_info
*request
;
346 struct linger linger
;
349 if (sscanf(value
, "%d%c", &linger
.l_linger
, &junk
) != 1
350 || linger
.l_linger
< 0)
351 tcpd_jump("bad linger value: \"%s\"", value
);
353 linger
.l_onoff
= (linger
.l_linger
!= 0);
354 if (setsockopt(request
->fd
, SOL_SOCKET
, SO_LINGER
, (char *) &linger
,
356 tcpd_warn("setsockopt SO_LINGER %d: %m", linger
.l_linger
);
360 /* keepalive_option - set the socket keepalive option */
364 static void keepalive_option(value
, request
)
366 struct request_info
*request
;
370 if (dry_run
== 0 && setsockopt(request
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
371 (char *) &on
, sizeof(on
)) < 0)
372 tcpd_warn("setsockopt SO_KEEPALIVE: %m");
375 /* nice_option - set nice value */
379 static void nice_option(value
, request
)
381 struct request_info
*request
;
386 if (value
!= 0 && sscanf(value
, "%d%c", &niceval
, &junk
) != 1)
387 tcpd_jump("bad nice value: \"%s\"", value
);
388 if (dry_run
== 0 && nice(niceval
) < 0)
389 tcpd_warn("nice(%d): %m", niceval
);
392 /* twist_option - replace process by shell command */
394 static void twist_option(value
, request
)
396 struct request_info
*request
;
402 tcpd_jump("twist option in resident process");
404 syslog(deny_severity
, "twist %s to %s", eval_client(request
), value
);
406 /* Before switching to the shell, set up stdin, stdout and stderr. */
408 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
410 if (maybe_dup2(request
->fd
, 0) != 0 ||
411 maybe_dup2(request
->fd
, 1) != 1 ||
412 maybe_dup2(request
->fd
, 2) != 2) {
413 tcpd_warn("twist_option: dup: %m");
417 (void) execl("/bin/sh", "sh", "-c", value
, (char *) 0);
418 tcpd_warn("twist_option: /bin/sh: %m");
421 /* Something went wrong: we MUST terminate the process. */
426 /* rfc931_option - look up remote user name */
428 static void rfc931_option(value
, request
)
430 struct request_info
*request
;
436 if (sscanf(value
, "%d%c", &timeout
, &junk
) != 1 || timeout
<= 0)
437 tcpd_jump("bad rfc931 timeout: \"%s\"", value
);
438 rfc931_timeout
= timeout
;
440 (void) eval_user(request
);
443 /* setenv_option - set environment variable */
447 static void setenv_option(value
, request
)
449 struct request_info
*request
;
453 if (*(var_value
= value
+ strcspn(value
, whitespace
)))
455 if (setenv(chop_string(value
), chop_string(var_value
), 1))
456 tcpd_jump("memory allocation failure");
460 * The severity option goes last because it comes with a huge amount of ugly
461 * #ifdefs and tables.
464 struct syslog_names
{
469 static struct syslog_names log_fac
[] = {
471 { "kern", LOG_KERN
},
474 { "user", LOG_USER
},
477 { "mail", LOG_MAIL
},
480 { "daemon", LOG_DAEMON
},
483 { "auth", LOG_AUTH
},
489 { "news", LOG_NEWS
},
492 { "uucp", LOG_UUCP
},
495 { "cron", LOG_CRON
},
498 { "authpriv", LOG_AUTHPRIV
},
504 { "local0", LOG_LOCAL0
},
507 { "local1", LOG_LOCAL1
},
510 { "local2", LOG_LOCAL2
},
513 { "local3", LOG_LOCAL3
},
516 { "local4", LOG_LOCAL4
},
519 { "local5", LOG_LOCAL5
},
522 { "local6", LOG_LOCAL6
},
525 { "local7", LOG_LOCAL7
},
530 static struct syslog_names log_sev
[] = {
532 { "emerg", LOG_EMERG
},
535 { "alert", LOG_ALERT
},
538 { "crit", LOG_CRIT
},
544 { "warning", LOG_WARNING
},
547 { "notice", LOG_NOTICE
},
550 { "info", LOG_INFO
},
553 { "debug", LOG_DEBUG
},
558 /* severity_map - lookup facility or severity value */
560 static int severity_map(table
, name
)
561 struct syslog_names
*table
;
564 struct syslog_names
*t
;
566 for (t
= table
; t
->name
; t
++)
567 if (STR_EQ(t
->name
, name
))
569 tcpd_jump("bad syslog facility or severity: \"%s\"", name
);
574 /* severity_option - change logging severity for this event (Dave Mitchell) */
578 static void severity_option(value
, request
)
580 struct request_info
*request
;
582 char *level
= split_at(value
, '.');
584 allow_severity
= deny_severity
= level
?
585 severity_map(log_fac
, value
) | severity_map(log_sev
, level
) :
586 severity_map(log_sev
, value
);
589 /* get_field - return pointer to next field in string */
591 static char *get_field(string
)
594 static char *last
= "";
601 * This function returns pointers to successive fields within a given
602 * string. ":" is the field separator; warn if the rule ends in one. It
603 * replaces a "\:" sequence by ":", without treating the result of
604 * substitution as field terminator. A null argument means resume search
605 * where the previous call terminated. This function destroys its
608 * Work from explicit source or from memory. While processing \: we
609 * overwrite the input. This way we do not have to maintain buffers for
610 * copies of input fields.
613 src
= dst
= ret
= (string
? string
: last
);
617 while ((ch
= *src
) != '\0') {
620 tcpd_warn("rule ends in \":\"");
623 if (ch
== '\\' && src
[1] == ':')
632 /* chop_string - strip leading and trailing blanks from string */
634 static char *chop_string(string
)
635 register char *string
;
641 for (cp
= string
; *cp
; cp
++) {
642 if (!isspace((unsigned char) *cp
)) {
648 return (start
? (end
[1] = 0, start
) : cp
);