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
39 static char sccsid
[] = "@(#) options.c 1.17 96/02/11 17:01:31";
42 /* System libraries. */
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
60 #ifndef MAXPATHNAMELEN
61 #define MAXPATHNAMELEN BUFSIZ
68 /* Options runtime support. */
70 int dry_run
= 0; /* flag set in verification mode */
71 extern jmp_buf tcpd_buf
; /* tcpd_jump() support */
73 /* Options parser support. */
75 static char whitespace_eq
[] = "= \t\r\n";
76 #define whitespace (whitespace_eq + 1)
78 static char *get_field(); /* chew :-delimited field off string */
79 static char *chop_string(); /* strip leading and trailing blanks */
81 /* List of functions that implement the options. Add yours here. */
83 static void user_option(); /* execute "user name.group" option */
84 static void group_option(); /* execute "group name" option */
85 static void umask_option(); /* execute "umask mask" option */
86 static void linger_option(); /* execute "linger time" option */
87 static void keepalive_option(); /* execute "keepalive" option */
88 static void spawn_option(); /* execute "spawn command" option */
89 static void twist_option(); /* execute "twist command" option */
90 static void rfc931_option(); /* execute "rfc931" option */
91 static void setenv_option(); /* execute "setenv name value" */
92 static void nice_option(); /* execute "nice" option */
93 static void severity_option(); /* execute "severity value" */
94 static void allow_option(); /* execute "allow" option */
95 static void deny_option(); /* execute "deny" option */
96 static void banners_option(); /* execute "banners path" option */
98 /* Structure of the options table. */
101 char *name
; /* keyword name, case is ignored */
102 void (*func
) (); /* function that does the real work */
103 int flags
; /* see below... */
106 #define NEED_ARG (1<<1) /* option requires argument */
107 #define USE_LAST (1<<2) /* option must be last */
108 #define OPT_ARG (1<<3) /* option has optional argument */
109 #define EXPAND_ARG (1<<4) /* do %x expansion on argument */
111 #define need_arg(o) ((o)->flags & NEED_ARG)
112 #define opt_arg(o) ((o)->flags & OPT_ARG)
113 #define permit_arg(o) ((o)->flags & (NEED_ARG | OPT_ARG))
114 #define use_last(o) ((o)->flags & USE_LAST)
115 #define expand_arg(o) ((o)->flags & EXPAND_ARG)
117 /* List of known keywords. Add yours here. */
119 static struct option option_table
[] = {
120 "user", user_option
, NEED_ARG
,
121 "group", group_option
, NEED_ARG
,
122 "umask", umask_option
, NEED_ARG
,
123 "linger", linger_option
, NEED_ARG
,
124 "keepalive", keepalive_option
, 0,
125 "spawn", spawn_option
, NEED_ARG
| EXPAND_ARG
,
126 "twist", twist_option
, NEED_ARG
| EXPAND_ARG
| USE_LAST
,
127 "rfc931", rfc931_option
, OPT_ARG
,
128 "setenv", setenv_option
, NEED_ARG
| EXPAND_ARG
,
129 "nice", nice_option
, OPT_ARG
,
130 "severity", severity_option
, NEED_ARG
,
131 "allow", allow_option
, USE_LAST
,
132 "deny", deny_option
, USE_LAST
,
133 "banners", banners_option
, NEED_ARG
,
137 /* process_options - process access control options */
139 void process_options(options
, request
)
141 struct request_info
*request
;
150 for (curr_opt
= get_field(options
); curr_opt
; curr_opt
= next_opt
) {
151 next_opt
= get_field((char *) 0);
154 * Separate the option into name and value parts. For backwards
155 * compatibility we ignore exactly one '=' between name and value.
157 curr_opt
= chop_string(curr_opt
);
158 if (*(value
= curr_opt
+ strcspn(curr_opt
, whitespace_eq
))) {
161 value
+= strspn(value
, whitespace
);
165 value
+= strspn(value
, whitespace
);
173 * Disallow missing option names (and empty option fields).
176 tcpd_jump("missing option name");
179 * Lookup the option-specific info and do some common error checks.
180 * Delegate option-specific processing to the specific functions.
183 for (op
= option_table
; op
->name
&& STR_NE(op
->name
, key
); op
++)
186 tcpd_jump("bad option name: \"%s\"", key
);
187 if (!value
&& need_arg(op
))
188 tcpd_jump("option \"%s\" requires value", key
);
189 if (value
&& !permit_arg(op
))
190 tcpd_jump("option \"%s\" requires no value", key
);
191 if (next_opt
&& use_last(op
))
192 tcpd_jump("option \"%s\" must be at end", key
);
193 if (value
&& expand_arg(op
))
194 value
= chop_string(percent_x(bf
, sizeof(bf
), value
, request
));
195 if (hosts_access_verbose
)
196 syslog(LOG_DEBUG
, "option: %s %s", key
, value
? value
: "");
197 (*(op
->func
)) (value
, request
);
201 /* allow_option - grant access */
205 static void allow_option(value
, request
)
207 struct request_info
*request
;
209 longjmp(tcpd_buf
, AC_PERMIT
);
212 /* deny_option - deny access */
216 static void deny_option(value
, request
)
218 struct request_info
*request
;
220 longjmp(tcpd_buf
, AC_DENY
);
223 /* banners_option - expand %<char>, terminate each line with CRLF */
225 static void banners_option(value
, request
)
227 struct request_info
*request
;
229 char path
[MAXPATHNAMELEN
];
231 char obuf
[2 * BUFSIZ
];
236 sprintf(path
, "%s/%s", value
, eval_daemon(request
));
237 if ((fp
= fopen(path
, "r")) != 0) {
238 while ((ch
= fgetc(fp
)) == 0)
239 write(request
->fd
, "", 1);
241 while (fgets(ibuf
, sizeof(ibuf
) - 1, fp
)) {
242 if (split_at(ibuf
, '\n'))
243 strcat(ibuf
, "\r\n");
244 percent_x(obuf
, sizeof(obuf
), ibuf
, request
);
245 write(request
->fd
, obuf
, strlen(obuf
));
248 } else if (stat(value
, &st
) < 0) {
249 tcpd_warn("%s: %m", value
);
253 /* group_option - switch group id */
257 static void group_option(value
, request
)
259 struct request_info
*request
;
262 struct group
*getgrnam();
264 if ((grp
= getgrnam(value
)) == 0)
265 tcpd_jump("unknown group: \"%s\"", value
);
268 if (dry_run
== 0 && setgid(grp
->gr_gid
))
269 tcpd_jump("setgid(%s): %m", value
);
272 /* user_option - switch user id */
276 static void user_option(value
, request
)
278 struct request_info
*request
;
281 struct passwd
*getpwnam();
284 if ((group
= split_at(value
, '.')) != 0)
285 group_option(group
, request
);
286 if ((pwd
= getpwnam(value
)) == 0)
287 tcpd_jump("unknown user: \"%s\"", value
);
290 if (dry_run
== 0 && setuid(pwd
->pw_uid
))
291 tcpd_jump("setuid(%s): %m", value
);
294 /* umask_option - set file creation mask */
298 static void umask_option(value
, request
)
300 struct request_info
*request
;
305 if (sscanf(value
, "%o%c", &mask
, &junk
) != 1 || (mask
& 0777) != mask
)
306 tcpd_jump("bad umask value: \"%s\"", value
);
310 /* spawn_option - spawn a shell command and wait */
314 static void spawn_option(value
, request
)
316 struct request_info
*request
;
322 /* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
326 static void linger_option(value
, request
)
328 struct request_info
*request
;
330 struct linger linger
;
333 if (sscanf(value
, "%d%c", &linger
.l_linger
, &junk
) != 1
334 || linger
.l_linger
< 0)
335 tcpd_jump("bad linger value: \"%s\"", value
);
337 linger
.l_onoff
= (linger
.l_linger
!= 0);
338 if (setsockopt(request
->fd
, SOL_SOCKET
, SO_LINGER
, (char *) &linger
,
340 tcpd_warn("setsockopt SO_LINGER %d: %m", linger
.l_linger
);
344 /* keepalive_option - set the socket keepalive option */
348 static void keepalive_option(value
, request
)
350 struct request_info
*request
;
354 if (dry_run
== 0 && setsockopt(request
->fd
, SOL_SOCKET
, SO_KEEPALIVE
,
355 (char *) &on
, sizeof(on
)) < 0)
356 tcpd_warn("setsockopt SO_KEEPALIVE: %m");
359 /* nice_option - set nice value */
363 static void nice_option(value
, request
)
365 struct request_info
*request
;
370 if (value
!= 0 && sscanf(value
, "%d%c", &niceval
, &junk
) != 1)
371 tcpd_jump("bad nice value: \"%s\"", value
);
372 if (dry_run
== 0 && nice(niceval
) < 0)
373 tcpd_warn("nice(%d): %m", niceval
);
376 /* twist_option - replace process by shell command */
378 static void twist_option(value
, request
)
380 struct request_info
*request
;
388 tcpd_jump("twist option in resident process");
390 syslog(deny_severity
, "twist %s to %s", eval_client(request
), value
);
392 /* Before switching to the shell, set up stdin, stdout and stderr. */
394 #define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
396 if (maybe_dup2(request
->fd
, 0) != 0 ||
397 maybe_dup2(request
->fd
, 1) != 1 ||
398 maybe_dup2(request
->fd
, 2) != 2) {
399 error
= "twist_option: dup: %m";
403 (void) execl("/bin/sh", "sh", "-c", value
, (char *) 0);
404 error
= "twist_option: /bin/sh: %m";
407 /* Something went wrong: we MUST terminate the process. */
414 /* rfc931_option - look up remote user name */
416 static void rfc931_option(value
, request
)
418 struct request_info
*request
;
424 if (sscanf(value
, "%d%c", &timeout
, &junk
) != 1 || timeout
<= 0)
425 tcpd_jump("bad rfc931 timeout: \"%s\"", value
);
426 rfc931_timeout
= timeout
;
428 (void) eval_user(request
);
431 /* setenv_option - set environment variable */
435 static void setenv_option(value
, request
)
437 struct request_info
*request
;
439 extern int setenv(const char *, const char *, int);
442 if (*(var_value
= value
+ strcspn(value
, whitespace
)))
444 if (setenv(chop_string(value
), chop_string(var_value
), 1))
445 tcpd_jump("memory allocation failure");
449 * The severity option goes last because it comes with a huge amount of ugly
450 * #ifdefs and tables.
453 struct syslog_names
{
458 static struct syslog_names log_fac
[] = {
469 "daemon", LOG_DAEMON
,
487 "local0", LOG_LOCAL0
,
490 "local1", LOG_LOCAL1
,
493 "local2", LOG_LOCAL2
,
496 "local3", LOG_LOCAL3
,
499 "local4", LOG_LOCAL4
,
502 "local5", LOG_LOCAL5
,
505 "local6", LOG_LOCAL6
,
508 "local7", LOG_LOCAL7
,
513 static struct syslog_names log_sev
[] = {
527 "warning", LOG_WARNING
,
530 "notice", LOG_NOTICE
,
541 /* severity_map - lookup facility or severity value */
543 static int severity_map(table
, name
)
544 struct syslog_names
*table
;
547 struct syslog_names
*t
;
549 for (t
= table
; t
->name
; t
++)
550 if (STR_EQ(t
->name
, name
))
552 tcpd_jump("bad syslog facility or severity: \"%s\"", name
);
556 /* severity_option - change logging severity for this event (Dave Mitchell) */
560 static void severity_option(value
, request
)
562 struct request_info
*request
;
564 char *level
= split_at(value
, '.');
566 allow_severity
= deny_severity
= level
?
567 severity_map(log_fac
, value
) | severity_map(log_sev
, level
) :
568 severity_map(log_sev
, value
);
571 /* get_field - return pointer to next field in string */
573 static char *get_field(string
)
576 static char *last
= "";
583 * This function returns pointers to successive fields within a given
584 * string. ":" is the field separator; warn if the rule ends in one. It
585 * replaces a "\:" sequence by ":", without treating the result of
586 * substitution as field terminator. A null argument means resume search
587 * where the previous call terminated. This function destroys its
590 * Work from explicit source or from memory. While processing \: we
591 * overwrite the input. This way we do not have to maintain buffers for
592 * copies of input fields.
595 src
= dst
= ret
= (string
? string
: last
);
602 tcpd_warn("rule ends in \":\"");
605 if (ch
== '\\' && src
[1] == ':')
614 /* chop_string - strip leading and trailing blanks from string */
616 static char *chop_string(string
)
617 register char *string
;
623 for (cp
= string
; *cp
; cp
++) {
630 return (start
? (end
[1] = 0, start
) : cp
);