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).
6 * Notes and warnings for those who want to add features:
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
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()
18 * In case of severe errors, use clean_exit() instead of directly calling
19 * exit(), or the inetd may loop on an UDP request.
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.
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
32 static char sccsid
[] = "@(#) options.c 1.17 96/02/11 17:01:31";
35 /* System libraries. */
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
52 #ifndef MAXPATHNAMELEN
53 #define MAXPATHNAMELEN BUFSIZ
60 /* Options runtime support. */
62 int dry_run
= 0; /* flag set in verification mode */
63 extern jmp_buf tcpd_buf
; /* tcpd_jump() support */
65 /* Options parser support. */
67 static char whitespace_eq
[] = "= \t\r\n";
68 #define whitespace (whitespace_eq + 1)
70 static char *get_field(); /* chew :-delimited field off string */
71 static char *chop_string(); /* strip leading and trailing blanks */
73 /* List of functions that implement the options. Add yours here. */
75 static void user_option(); /* execute "user name.group" option */
76 static void group_option(); /* execute "group name" option */
77 static void umask_option(); /* execute "umask mask" option */
78 static void linger_option(); /* execute "linger time" option */
79 static void keepalive_option(); /* execute "keepalive" option */
80 static void spawn_option(); /* execute "spawn command" option */
81 static void twist_option(); /* execute "twist command" option */
82 static void rfc931_option(); /* execute "rfc931" option */
83 static void setenv_option(); /* execute "setenv name value" */
84 static void nice_option(); /* execute "nice" option */
85 static void severity_option(); /* execute "severity value" */
86 static void allow_option(); /* execute "allow" option */
87 static void deny_option(); /* execute "deny" option */
88 static void banners_option(); /* execute "banners path" option */
90 /* Structure of the options table. */
93 char *name
; /* keyword name, case is ignored */
94 void (*func
) (); /* function that does the real work */
95 int flags
; /* see below... */
98 #define NEED_ARG (1<<1) /* option requires argument */
99 #define USE_LAST (1<<2) /* option must be last */
100 #define OPT_ARG (1<<3) /* option has optional argument */
101 #define EXPAND_ARG (1<<4) /* do %x expansion on argument */
103 #define need_arg(o) ((o)->flags & NEED_ARG)
104 #define opt_arg(o) ((o)->flags & OPT_ARG)
105 #define permit_arg(o) ((o)->flags & (NEED_ARG | OPT_ARG))
106 #define use_last(o) ((o)->flags & USE_LAST)
107 #define expand_arg(o) ((o)->flags & EXPAND_ARG)
109 /* List of known keywords. Add yours here. */
111 static struct option option_table
[] = {
112 "user", user_option
, NEED_ARG
,
113 "group", group_option
, NEED_ARG
,
114 "umask", umask_option
, NEED_ARG
,
115 "linger", linger_option
, NEED_ARG
,
116 "keepalive", keepalive_option
, 0,
117 "spawn", spawn_option
, NEED_ARG
| EXPAND_ARG
,
118 "twist", twist_option
, NEED_ARG
| EXPAND_ARG
| USE_LAST
,
119 "rfc931", rfc931_option
, OPT_ARG
,
120 "setenv", setenv_option
, NEED_ARG
| EXPAND_ARG
,
121 "nice", nice_option
, OPT_ARG
,
122 "severity", severity_option
, NEED_ARG
,
123 "allow", allow_option
, USE_LAST
,
124 "deny", deny_option
, USE_LAST
,
125 "banners", banners_option
, NEED_ARG
,
129 /* process_options - process access control options */
131 void process_options(options
, request
)
133 struct request_info
*request
;
142 for (curr_opt
= get_field(options
); curr_opt
; curr_opt
= next_opt
) {
143 next_opt
= get_field((char *) 0);
146 * Separate the option into name and value parts. For backwards
147 * compatibility we ignore exactly one '=' between name and value.
149 curr_opt
= chop_string(curr_opt
);
150 if (*(value
= curr_opt
+ strcspn(curr_opt
, whitespace_eq
))) {
153 value
+= strspn(value
, whitespace
);
157 value
+= strspn(value
, whitespace
);
165 * Disallow missing option names (and empty option fields).
168 tcpd_jump("missing option name");
171 * Lookup the option-specific info and do some common error checks.
172 * Delegate option-specific processing to the specific functions.
175 for (op
= option_table
; op
->name
&& STR_NE(op
->name
, key
); op
++)
178 tcpd_jump("bad option name: \"%s\"", key
);
179 if (!value
&& need_arg(op
))
180 tcpd_jump("option \"%s\" requires value", key
);
181 if (value
&& !permit_arg(op
))
182 tcpd_jump("option \"%s\" requires no value", key
);
183 if (next_opt
&& use_last(op
))
184 tcpd_jump("option \"%s\" must be at end", key
);
185 if (value
&& expand_arg(op
))
186 value
= chop_string(percent_x(bf
, sizeof(bf
), value
, request
));
187 if (hosts_access_verbose
)
188 syslog(LOG_DEBUG
, "option: %s %s", key
, value
? value
: "");
189 (*(op
->func
)) (value
, request
);
193 /* allow_option - grant access */
197 static void allow_option(value
, request
)
199 struct request_info
*request
;
201 longjmp(tcpd_buf
, AC_PERMIT
);
204 /* deny_option - deny access */
208 static void deny_option(value
, request
)
210 struct request_info
*request
;
212 longjmp(tcpd_buf
, AC_DENY
);
215 /* banners_option - expand %<char>, terminate each line with CRLF */
217 static void banners_option(value
, request
)
219 struct request_info
*request
;
221 char path
[MAXPATHNAMELEN
];
223 char obuf
[2 * BUFSIZ
];
228 sprintf(path
, "%s/%s", value
, eval_daemon(request
));
229 if ((fp
= fopen(path
, "r")) != 0) {
230 while ((ch
= fgetc(fp
)) == 0)
231 write(request
->fd
, "", 1);
233 while (fgets(ibuf
, sizeof(ibuf
) - 1, fp
)) {
234 if (split_at(ibuf
, '\n'))
235 strcat(ibuf
, "\r\n");
236 percent_x(obuf
, sizeof(obuf
), ibuf
, request
);
237 write(request
->fd
, obuf
, strlen(obuf
));
240 } else if (stat(value
, &st
) < 0) {
241 tcpd_warn("%s: %m", value
);
245 /* group_option - switch group id */
249 static void group_option(value
, request
)
251 struct request_info
*request
;
254 struct group
*getgrnam();
256 if ((grp
= getgrnam(value
)) == 0)
257 tcpd_jump("unknown group: \"%s\"", value
);
260 if (dry_run
== 0 && setgid(grp
->gr_gid
))
261 tcpd_jump("setgid(%s): %m", value
);
264 /* user_option - switch user id */
268 static void user_option(value
, request
)
270 struct request_info
*request
;
273 struct passwd
*getpwnam();
276 if ((group
= split_at(value
, '.')) != 0)
277 group_option(group
, request
);
278 if ((pwd
= getpwnam(value
)) == 0)
279 tcpd_jump("unknown user: \"%s\"", value
);
282 if (dry_run
== 0 && setuid(pwd
->pw_uid
))
283 tcpd_jump("setuid(%s): %m", value
);
286 /* umask_option - set file creation mask */
290 static void umask_option(value
, request
)
292 struct request_info
*request
;
297 if (sscanf(value
, "%o%c", &mask
, &junk
) != 1 || (mask
& 0777) != mask
)
298 tcpd_jump("bad umask value: \"%s\"", value
);
302 /* spawn_option - spawn a shell command and wait */
306 static void spawn_option(value
, request
)
308 struct request_info
*request
;
314 /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
318 static void linger_option(value
, request
)
320 struct request_info
*request
;
322 struct linger linger
;
325 if (sscanf(value
, "%d%c", &linger
.l_linger
, &junk
) != 1
326 || linger
.l_linger
< 0)
327 tcpd_jump("bad linger value: \"%s\"", value
);
329 linger
.l_onoff
= (linger
.l_linger
!= 0);
330 if (setsockopt(request
->fd
, SOL_SOCKET
, SO_LINGER
, (char *) &linger
,
332 tcpd_warn("setsockopt SO_LINGER %d: %m", linger
.l_linger
);
336 /* keepalive_option - set the socket keepalive option */
340 static void keepalive_option(value
, request
)
342 struct request_info
*request
;
346 if (dry_run
== 0 && setsockopt(request
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
347 (char *) &on
, sizeof(on
)) < 0)
348 tcpd_warn("setsockopt SO_KEEPALIVE: %m");
351 /* nice_option - set nice value */
355 static void nice_option(value
, request
)
357 struct request_info
*request
;
362 if (value
!= 0 && sscanf(value
, "%d%c", &niceval
, &junk
) != 1)
363 tcpd_jump("bad nice value: \"%s\"", value
);
364 if (dry_run
== 0 && nice(niceval
) < 0)
365 tcpd_warn("nice(%d): %m", niceval
);
368 /* twist_option - replace process by shell command */
370 static void twist_option(value
, request
)
372 struct request_info
*request
;
380 tcpd_jump("twist option in resident process");
382 syslog(deny_severity
, "twist %s to %s", eval_client(request
), value
);
384 /* Before switching to the shell, set up stdin, stdout and stderr. */
386 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
388 if (maybe_dup2(request
->fd
, 0) != 0 ||
389 maybe_dup2(request
->fd
, 1) != 1 ||
390 maybe_dup2(request
->fd
, 2) != 2) {
391 error
= "twist_option: dup: %m";
395 (void) execl("/bin/sh", "sh", "-c", value
, (char *) 0);
396 error
= "twist_option: /bin/sh: %m";
399 /* Something went wrong: we MUST terminate the process. */
406 /* rfc931_option - look up remote user name */
408 static void rfc931_option(value
, request
)
410 struct request_info
*request
;
416 if (sscanf(value
, "%d%c", &timeout
, &junk
) != 1 || timeout
<= 0)
417 tcpd_jump("bad rfc931 timeout: \"%s\"", value
);
418 rfc931_timeout
= timeout
;
420 (void) eval_user(request
);
423 /* setenv_option - set environment variable */
427 static void setenv_option(value
, request
)
429 struct request_info
*request
;
433 if (*(var_value
= value
+ strcspn(value
, whitespace
)))
435 if (setenv(chop_string(value
), chop_string(var_value
), 1))
436 tcpd_jump("memory allocation failure");
439 /* severity_map - lookup facility or severity value */
441 static int severity_map(table
, name
)
447 for (t
= table
; t
->c_name
; t
++)
448 if (STR_EQ(t
->c_name
, name
))
450 tcpd_jump("bad syslog facility or severity: \"%s\"", name
);
454 /* severity_option - change logging severity for this event (Dave Mitchell) */
458 static void severity_option(value
, request
)
460 struct request_info
*request
;
462 char *level
= split_at(value
, '.');
464 allow_severity
= deny_severity
= level
?
465 severity_map(facilitynames
, value
) | severity_map(prioritynames
, level
)
466 : severity_map(prioritynames
, value
);
469 /* get_field - return pointer to next field in string */
471 static char *get_field(string
)
474 static char *last
= "";
481 * This function returns pointers to successive fields within a given
482 * string. ":" is the field separator; warn if the rule ends in one. It
483 * replaces a "\:" sequence by ":", without treating the result of
484 * substitution as field terminator. A null argument means resume search
485 * where the previous call terminated. This function destroys its
488 * Work from explicit source or from memory. While processing \: we
489 * overwrite the input. This way we do not have to maintain buffers for
490 * copies of input fields.
493 src
= dst
= ret
= (string
? string
: last
);
500 tcpd_warn("rule ends in \":\"");
503 if (ch
== '\\' && src
[1] == ':')
512 /* chop_string - strip leading and trailing blanks from string */
514 static char *chop_string(string
)
515 register char *string
;
521 for (cp
= string
; *cp
; cp
++) {
528 return (start
? (end
[1] = 0, start
) : cp
);