2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
9 * General skeleton for adding options to the access control language. The
10 * features offered by this module are documented in the hosts_options(5)
11 * manual page (source file: hosts_options.5, "nroff -man" format).
13 * Notes and warnings for those who want to add features:
15 * In case of errors, abort options processing and deny access. There are too
16 * many irreversible side effects to make error recovery feasible. For
17 * example, it makes no sense to continue after we have already changed the
20 * In case of errors, do not terminate the process: the routines might be
21 * called from a long-running daemon that should run forever. Instead, call
22 * tcpd_jump() which does a non-local goto back into the hosts_access()
25 * In case of severe errors, use clean_exit() instead of directly calling
26 * exit(), or the inetd may loop on an UDP request.
28 * In verification mode (for example, with the "tcpdmatch" command) the
29 * "dry_run" flag is set. In this mode, an option function should just "say"
30 * what it is going to do instead of really doing it.
32 * Some option functions do not return (for example, the twist option passes
33 * control to another program). In verification mode (dry_run flag is set)
34 * such options should clear the "dry_run" flag to inform the caller of this
38 static char sccsid
[] = "@(#) options.c 1.17 96/02/11 17:01:31";
40 /* System libraries. */
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
58 #ifndef MAXPATHNAMELEN
59 #define MAXPATHNAMELEN BUFSIZ
66 /* Options runtime support. */
68 int dry_run
= 0; /* flag set in verification mode */
69 extern jmp_buf tcpd_buf
; /* tcpd_jump() support */
71 /* Options parser support. */
73 static char whitespace_eq
[] = "= \t\r\n";
74 #define whitespace (whitespace_eq + 1)
76 static char *get_field(); /* chew :-delimited field off string */
77 static char *chop_string(); /* strip leading and trailing blanks */
79 /* List of functions that implement the options. Add yours here. */
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 */
96 /* Structure of the options table. */
99 char *name
; /* keyword name, case is ignored */
100 void (*func
) (); /* function that does the real work */
101 int flags
; /* see below... */
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 */
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)
115 /* List of known keywords. Add yours here. */
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
,
135 /* process_options - process access control options */
137 void process_options(options
, request
)
139 struct request_info
*request
;
148 for (curr_opt
= get_field(options
); curr_opt
; curr_opt
= next_opt
) {
149 next_opt
= get_field((char *) 0);
152 * Separate the option into name and value parts. For backwards
153 * compatibility we ignore exactly one '=' between name and value.
155 curr_opt
= chop_string(curr_opt
);
156 if (*(value
= curr_opt
+ strcspn(curr_opt
, whitespace_eq
))) {
159 value
+= strspn(value
, whitespace
);
163 value
+= strspn(value
, whitespace
);
171 * Disallow missing option names (and empty option fields).
174 tcpd_jump("missing option name");
177 * Lookup the option-specific info and do some common error checks.
178 * Delegate option-specific processing to the specific functions.
181 for (op
= option_table
; op
->name
&& STR_NE(op
->name
, key
); op
++)
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
);
199 /* allow_option - grant access */
203 static void allow_option(value
, request
)
205 struct request_info
*request
;
207 longjmp(tcpd_buf
, AC_PERMIT
);
210 /* deny_option - deny access */
214 static void deny_option(value
, request
)
216 struct request_info
*request
;
218 longjmp(tcpd_buf
, AC_DENY
);
221 /* banners_option - expand %<char>, terminate each line with CRLF */
223 static void banners_option(value
, request
)
225 struct request_info
*request
;
227 char path
[MAXPATHNAMELEN
];
229 char obuf
[2 * BUFSIZ
];
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);
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
));
246 } else if (stat(value
, &st
) < 0) {
247 tcpd_warn("%s: %m", value
);
251 /* group_option - switch group id */
255 static void group_option(value
, request
)
257 struct request_info
*request
;
260 struct group
*getgrnam();
262 if ((grp
= getgrnam(value
)) == 0)
263 tcpd_jump("unknown group: \"%s\"", value
);
266 if (dry_run
== 0 && setgid(grp
->gr_gid
))
267 tcpd_jump("setgid(%s): %m", value
);
270 /* user_option - switch user id */
274 static void user_option(value
, request
)
276 struct request_info
*request
;
279 struct passwd
*getpwnam();
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
);
288 if (dry_run
== 0 && setuid(pwd
->pw_uid
))
289 tcpd_jump("setuid(%s): %m", value
);
292 /* umask_option - set file creation mask */
296 static void umask_option(value
, request
)
298 struct request_info
*request
;
303 if (sscanf(value
, "%o%c", &mask
, &junk
) != 1 || (mask
& 0777) != mask
)
304 tcpd_jump("bad umask value: \"%s\"", value
);
308 /* spawn_option - spawn a shell command and wait */
312 static void spawn_option(value
, request
)
314 struct request_info
*request
;
320 /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
324 static void linger_option(value
, request
)
326 struct request_info
*request
;
328 struct linger linger
;
331 if (sscanf(value
, "%d%c", &linger
.l_linger
, &junk
) != 1
332 || linger
.l_linger
< 0)
333 tcpd_jump("bad linger value: \"%s\"", value
);
335 linger
.l_onoff
= (linger
.l_linger
!= 0);
336 if (setsockopt(request
->fd
, SOL_SOCKET
, SO_LINGER
, (char *) &linger
,
338 tcpd_warn("setsockopt SO_LINGER %d: %m", linger
.l_linger
);
342 /* keepalive_option - set the socket keepalive option */
346 static void keepalive_option(value
, request
)
348 struct request_info
*request
;
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");
357 /* nice_option - set nice value */
361 static void nice_option(value
, request
)
363 struct request_info
*request
;
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
);
374 /* twist_option - replace process by shell command */
376 static void twist_option(value
, request
)
378 struct request_info
*request
;
386 tcpd_jump("twist option in resident process");
388 syslog(deny_severity
, "twist %s to %s", eval_client(request
), value
);
390 /* Before switching to the shell, set up stdin, stdout and stderr. */
392 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
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";
401 (void) execl("/bin/sh", "sh", "-c", value
, (char *) 0);
402 error
= "twist_option: /bin/sh: %m";
405 /* Something went wrong: we MUST terminate the process. */
412 /* rfc931_option - look up remote user name */
414 static void rfc931_option(value
, request
)
416 struct request_info
*request
;
422 if (sscanf(value
, "%d%c", &timeout
, &junk
) != 1 || timeout
<= 0)
423 tcpd_jump("bad rfc931 timeout: \"%s\"", value
);
424 rfc931_timeout
= timeout
;
426 (void) eval_user(request
);
429 /* setenv_option - set environment variable */
433 static void setenv_option(value
, request
)
435 struct request_info
*request
;
437 extern int setenv(const char *, const char *, int);
440 if (*(var_value
= value
+ strcspn(value
, whitespace
)))
442 if (setenv(chop_string(value
), chop_string(var_value
), 1))
443 tcpd_jump("memory allocation failure");
447 * The severity option goes last because it comes with a huge amount of ugly
448 * #ifdefs and tables.
451 struct syslog_names
{
456 static struct syslog_names log_fac
[] = {
467 "daemon", LOG_DAEMON
,
485 "local0", LOG_LOCAL0
,
488 "local1", LOG_LOCAL1
,
491 "local2", LOG_LOCAL2
,
494 "local3", LOG_LOCAL3
,
497 "local4", LOG_LOCAL4
,
500 "local5", LOG_LOCAL5
,
503 "local6", LOG_LOCAL6
,
506 "local7", LOG_LOCAL7
,
511 static struct syslog_names log_sev
[] = {
525 "warning", LOG_WARNING
,
528 "notice", LOG_NOTICE
,
539 /* severity_map - lookup facility or severity value */
541 static int severity_map(table
, name
)
542 struct syslog_names
*table
;
545 struct syslog_names
*t
;
547 for (t
= table
; t
->name
; t
++)
548 if (STR_EQ(t
->name
, name
))
550 tcpd_jump("bad syslog facility or severity: \"%s\"", name
);
554 /* severity_option - change logging severity for this event (Dave Mitchell) */
558 static void severity_option(value
, request
)
560 struct request_info
*request
;
562 char *level
= split_at(value
, '.');
564 allow_severity
= deny_severity
= level
?
565 severity_map(log_fac
, value
) | severity_map(log_sev
, level
) :
566 severity_map(log_sev
, value
);
569 /* get_field - return pointer to next field in string */
571 static char *get_field(string
)
574 static char *last
= "";
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
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.
593 src
= dst
= ret
= (string
? string
: last
);
600 tcpd_warn("rule ends in \":\"");
603 if (ch
== '\\' && src
[1] == ':')
612 /* chop_string - strip leading and trailing blanks from string */
614 static char *chop_string(string
)
615 register char *string
;
621 for (cp
= string
; *cp
; cp
++) {
628 return (start
? (end
[1] = 0, start
) : cp
);