NetBSD/lib/libwrap/options.c

635 lines
16 KiB
C
Raw Normal View History

/* $NetBSD: options.c,v 1.16 2012/03/22 22:59:43 joerg Exp $ */
1996-11-26 19:48:08 +03:00
/*
* General skeleton for adding options to the access control language. The
* features offered by this module are documented in the hosts_options(5)
* manual page (source file: hosts_options.5, "nroff -man" format).
*
1996-11-26 19:48:08 +03:00
* Notes and warnings for those who want to add features:
*
1996-11-26 19:48:08 +03:00
* In case of errors, abort options processing and deny access. There are too
* many irreversible side effects to make error recovery feasible. For
* example, it makes no sense to continue after we have already changed the
* userid.
*
1996-11-26 19:48:08 +03:00
* In case of errors, do not terminate the process: the routines might be
* called from a long-running daemon that should run forever. Instead, call
* tcpd_jump() which does a non-local goto back into the hosts_access()
* routine.
*
1996-11-26 19:48:08 +03:00
* In case of severe errors, use clean_exit() instead of directly calling
* exit(), or the inetd may loop on an UDP request.
*
1996-11-26 19:48:08 +03:00
* In verification mode (for example, with the "tcpdmatch" command) the
* "dry_run" flag is set. In this mode, an option function should just "say"
* what it is going to do instead of really doing it.
*
1996-11-26 19:48:08 +03:00
* Some option functions do not return (for example, the twist option passes
* control to another program). In verification mode (dry_run flag is set)
* such options should clear the "dry_run" flag to inform the caller of this
* course of action.
*/
#include <sys/cdefs.h>
1996-11-26 19:48:08 +03:00
#ifndef lint
#if 0
1996-11-26 19:48:08 +03:00
static char sccsid[] = "@(#) options.c 1.17 96/02/11 17:01:31";
#else
__RCSID("$NetBSD: options.c,v 1.16 2012/03/22 22:59:43 joerg Exp $");
#endif
1996-11-26 19:48:08 +03:00
#endif
/* System libraries. */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
1996-11-26 19:48:08 +03:00
#include <pwd.h>
#include <grp.h>
#include <ctype.h>
#include <setjmp.h>
#include <string.h>
/* Local stuff. */
#include "tcpd.h"
/* Options runtime support. */
int dry_run = 0; /* flag set in verification mode */
extern jmp_buf tcpd_buf; /* tcpd_jump() support */
/* Options parser support. */
static char whitespace_eq[] = "= \t\r\n";
#define whitespace (whitespace_eq + 1)
static char *get_field /* chew :-delimited field off string */
2012-03-21 14:10:36 +04:00
(char *);
static char *chop_string /* strip leading and trailing blanks */
2012-03-21 14:10:36 +04:00
(char *);
struct syslog_names;
static int severity_map
2012-03-21 14:10:36 +04:00
(const struct syslog_names *, char *);
1996-11-26 19:48:08 +03:00
/* List of functions that implement the options. Add yours here. */
static void user_option /* execute "user name.group" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void group_option /* execute "group name" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void umask_option /* execute "umask mask" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void linger_option /* execute "linger time" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void keepalive_option /* execute "keepalive" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void spawn_option /* execute "spawn command" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void twist_option /* execute "twist command" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void rfc931_option /* execute "rfc931" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void setenv_option /* execute "setenv name value" */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void nice_option /* execute "nice" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void severity_option /* execute "severity value" */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
__dead static void allow_option /* execute "allow" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
__dead static void deny_option /* execute "deny" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
static void banners_option /* execute "banners path" option */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
1996-11-26 19:48:08 +03:00
/* Structure of the options table. */
2000-04-02 10:31:58 +04:00
struct option {
2012-03-21 14:10:36 +04:00
const char *name; /* keyword name, case is ignored */
void (*func) /* function that does the real work */
2012-03-21 14:10:36 +04:00
(char *, struct request_info *);
1996-11-26 19:48:08 +03:00
int flags; /* see below... */
};
#define NEED_ARG (1<<1) /* option requires argument */
#define USE_LAST (1<<2) /* option must be last */
#define OPT_ARG (1<<3) /* option has optional argument */
#define EXPAND_ARG (1<<4) /* do %x expansion on argument */
#define need_arg(o) ((o)->flags & NEED_ARG)
#define opt_arg(o) ((o)->flags & OPT_ARG)
#define permit_arg(o) ((o)->flags & (NEED_ARG | OPT_ARG))
#define use_last(o) ((o)->flags & USE_LAST)
#define expand_arg(o) ((o)->flags & EXPAND_ARG)
/* List of known keywords. Add yours here. */
2000-04-02 10:31:58 +04:00
static struct option option_table[] = {
{ "user", user_option, NEED_ARG },
{ "group", group_option, NEED_ARG },
{ "umask", umask_option, NEED_ARG },
{ "linger", linger_option, NEED_ARG },
{ "keepalive", keepalive_option, 0 },
{ "spawn", spawn_option, NEED_ARG | EXPAND_ARG },
{ "twist", twist_option, NEED_ARG | EXPAND_ARG | USE_LAST },
{ "rfc931", rfc931_option, OPT_ARG },
{ "setenv", setenv_option, NEED_ARG | EXPAND_ARG },
{ "nice", nice_option, OPT_ARG },
{ "severity", severity_option, NEED_ARG },
{ "allow", allow_option, USE_LAST },
{ "deny", deny_option, USE_LAST },
{ "banners", banners_option, NEED_ARG },
{ NULL, NULL, 0 }
1996-11-26 19:48:08 +03:00
};
/* process_options - process access control options */
2012-03-21 14:10:36 +04:00
void
process_options(char *options, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
char *key;
char *value;
char *curr_opt;
char *next_opt;
2000-04-02 10:31:58 +04:00
struct option *op;
1996-11-26 19:48:08 +03:00
char bf[BUFSIZ];
for (curr_opt = get_field(options); curr_opt; curr_opt = next_opt) {
next_opt = get_field((char *) 0);
/*
* Separate the option into name and value parts. For backwards
* compatibility we ignore exactly one '=' between name and value.
*/
curr_opt = chop_string(curr_opt);
if (*(value = curr_opt + strcspn(curr_opt, whitespace_eq))) {
if (*value != '=') {
*value++ = 0;
value += strspn(value, whitespace);
}
if (*value == '=') {
*value++ = 0;
value += strspn(value, whitespace);
}
}
if (*value == 0)
value = 0;
key = curr_opt;
/*
* Disallow missing option names (and empty option fields).
*/
if (*key == 0)
tcpd_jump("missing option name");
/*
* Lookup the option-specific info and do some common error checks.
* Delegate option-specific processing to the specific functions.
*/
for (op = option_table; op->name && STR_NE(op->name, key); op++)
/* VOID */ ;
if (op->name == 0)
tcpd_jump("bad option name: \"%s\"", key);
if (!value && need_arg(op))
tcpd_jump("option \"%s\" requires value", key);
if (value && !permit_arg(op))
tcpd_jump("option \"%s\" requires no value", key);
if (next_opt && use_last(op))
tcpd_jump("option \"%s\" must be at end", key);
if (value && expand_arg(op))
value = chop_string(percent_x(bf, sizeof(bf), value, request));
if (hosts_access_verbose)
syslog(LOG_DEBUG, "option: %s %s", key, value ? value : "");
(*(op->func)) (value, request);
}
}
/* allow_option - grant access */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
allow_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
longjmp(tcpd_buf, AC_PERMIT);
}
/* deny_option - deny access */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
deny_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
longjmp(tcpd_buf, AC_DENY);
}
/* banners_option - expand %<char>, terminate each line with CRLF */
2012-03-21 14:10:36 +04:00
static void
banners_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
char path[MAXPATHLEN];
1996-11-26 19:48:08 +03:00
char ibuf[BUFSIZ];
char obuf[2 * BUFSIZ];
struct stat st;
int ch;
FILE *fp;
(void)snprintf(path, sizeof path, "%s/%s", value, eval_daemon(request));
1996-11-26 19:48:08 +03:00
if ((fp = fopen(path, "r")) != 0) {
while ((ch = fgetc(fp)) == 0)
write(request->fd, "", 1);
ungetc(ch, fp);
while (fgets(ibuf, sizeof(ibuf) - 2, fp)) {
1996-11-26 19:48:08 +03:00
if (split_at(ibuf, '\n'))
strcat(ibuf, "\r\n"); /* XXX strcat is safe */
1996-11-26 19:48:08 +03:00
percent_x(obuf, sizeof(obuf), ibuf, request);
write(request->fd, obuf, strlen(obuf));
}
fclose(fp);
} else if (stat(value, &st) < 0) {
tcpd_warn("%s: %m", value);
}
}
/* group_option - switch group id */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
group_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
struct group grs, *grp;
char grbuf[1024];
1996-11-26 19:48:08 +03:00
(void)getgrnam_r(value, &grs, grbuf, sizeof(grbuf), &grp);
if (grp == NULL)
1996-11-26 19:48:08 +03:00
tcpd_jump("unknown group: \"%s\"", value);
if (dry_run == 0 && setgid(grp->gr_gid))
tcpd_jump("setgid(%s): %m", value);
}
/* user_option - switch user id */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
user_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
2005-04-10 12:05:40 +04:00
struct passwd *pwd, pws;
1996-11-26 19:48:08 +03:00
char *group;
2005-04-10 12:05:40 +04:00
char pwbuf[1024];
1996-11-26 19:48:08 +03:00
if ((group = split_at(value, '.')) != 0)
group_option(group, request);
(void)getpwnam_r(value, &pws, pwbuf, sizeof(pwbuf), &pwd);
if (pwd == NULL)
1996-11-26 19:48:08 +03:00
tcpd_jump("unknown user: \"%s\"", value);
if (dry_run == 0 && setuid(pwd->pw_uid))
tcpd_jump("setuid(%s): %m", value);
}
/* umask_option - set file creation mask */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
umask_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
unsigned mask;
char junk;
if (sscanf(value, "%o%c", &mask, &junk) != 1 || (mask & 0777) != mask)
tcpd_jump("bad umask value: \"%s\"", value);
(void) umask(mask);
}
/* spawn_option - spawn a shell command and wait */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
spawn_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
if (dry_run == 0)
shell_cmd(value);
}
/* linger_option - set the socket linger time (Marc Boucher <marc@cam.org>) */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
linger_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
struct linger linger;
char junk;
if (sscanf(value, "%d%c", &linger.l_linger, &junk) != 1
|| linger.l_linger < 0)
tcpd_jump("bad linger value: \"%s\"", value);
if (dry_run == 0) {
linger.l_onoff = (linger.l_linger != 0);
if (setsockopt(request->fd, SOL_SOCKET, SO_LINGER, (char *) &linger,
sizeof(linger)) < 0)
tcpd_warn("setsockopt SO_LINGER %d: %m", linger.l_linger);
}
}
/* keepalive_option - set the socket keepalive option */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
keepalive_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
static int on = 1;
if (dry_run == 0 && setsockopt(request->fd, SOL_SOCKET, SO_KEEPALIVE,
(char *) &on, sizeof(on)) < 0)
tcpd_warn("setsockopt SO_KEEPALIVE: %m");
}
/* nice_option - set nice value */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
nice_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
int niceval = 10;
char junk;
if (value != 0 && sscanf(value, "%d%c", &niceval, &junk) != 1)
tcpd_jump("bad nice value: \"%s\"", value);
if (dry_run == 0 && nice(niceval) < 0)
tcpd_warn("nice(%d): %m", niceval);
}
/* twist_option - replace process by shell command */
2012-03-21 14:10:36 +04:00
static void
twist_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
if (dry_run != 0) {
dry_run = 0;
} else {
if (resident > 0)
tcpd_jump("twist option in resident process");
syslog(deny_severity, "twist %s to %s", eval_client(request), value);
/* Before switching to the shell, set up stdin, stdout and stderr. */
#define maybe_dup2(from, to) ((from == to) ? to : (close(to), dup(from)))
if (maybe_dup2(request->fd, 0) != 0 ||
maybe_dup2(request->fd, 1) != 1 ||
maybe_dup2(request->fd, 2) != 2) {
tcpd_warn("twist_option: dup: %m");
1996-11-26 19:48:08 +03:00
} else {
if (request->fd > 2)
close(request->fd);
(void) execl("/bin/sh", "sh", "-c", value, (char *) 0);
tcpd_warn("twist_option: /bin/sh: %m");
1996-11-26 19:48:08 +03:00
}
/* Something went wrong: we MUST terminate the process. */
clean_exit(request);
}
}
/* rfc931_option - look up remote user name */
2012-03-21 14:10:36 +04:00
static void
rfc931_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
int timeout;
char junk;
if (value != 0) {
if (sscanf(value, "%d%c", &timeout, &junk) != 1 || timeout <= 0)
tcpd_jump("bad rfc931 timeout: \"%s\"", value);
rfc931_timeout = timeout;
}
(void) eval_user(request);
}
/* setenv_option - set environment variable */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
setenv_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
char *var_value;
if (*(var_value = value + strcspn(value, whitespace)))
*var_value++ = 0;
if (setenv(chop_string(value), chop_string(var_value), 1))
tcpd_jump("memory allocation failure");
}
/*
* The severity option goes last because it comes with a huge amount of ugly
* #ifdefs and tables.
*/
struct syslog_names {
2012-03-21 14:10:36 +04:00
const char *name;
int value;
1996-11-26 19:48:08 +03:00
};
2012-03-21 14:10:36 +04:00
static const struct syslog_names log_fac[] = {
1996-11-26 19:48:08 +03:00
#ifdef LOG_KERN
{ "kern", LOG_KERN },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_USER
{ "user", LOG_USER },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_MAIL
{ "mail", LOG_MAIL },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_DAEMON
{ "daemon", LOG_DAEMON },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_AUTH
{ "auth", LOG_AUTH },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LPR
{ "lpr", LOG_LPR },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_NEWS
{ "news", LOG_NEWS },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_UUCP
{ "uucp", LOG_UUCP },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_CRON
{ "cron", LOG_CRON },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_AUTHPRIV
{ "authpriv", LOG_AUTHPRIV },
#endif
#ifdef LOG_FTP
{ "ftp", LOG_FTP },
#endif
1996-11-26 19:48:08 +03:00
#ifdef LOG_LOCAL0
{ "local0", LOG_LOCAL0 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL1
{ "local1", LOG_LOCAL1 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL2
{ "local2", LOG_LOCAL2 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL3
{ "local3", LOG_LOCAL3 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL4
{ "local4", LOG_LOCAL4 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL5
{ "local5", LOG_LOCAL5 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL6
{ "local6", LOG_LOCAL6 },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_LOCAL7
{ "local7", LOG_LOCAL7 },
1996-11-26 19:48:08 +03:00
#endif
{ NULL, 0 }
1996-11-26 19:48:08 +03:00
};
2012-03-21 14:10:36 +04:00
static const struct syslog_names log_sev[] = {
1996-11-26 19:48:08 +03:00
#ifdef LOG_EMERG
{ "emerg", LOG_EMERG },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_ALERT
{ "alert", LOG_ALERT },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_CRIT
{ "crit", LOG_CRIT },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_ERR
{ "err", LOG_ERR },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_WARNING
{ "warning", LOG_WARNING },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_NOTICE
{ "notice", LOG_NOTICE },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_INFO
{ "info", LOG_INFO },
1996-11-26 19:48:08 +03:00
#endif
#ifdef LOG_DEBUG
{ "debug", LOG_DEBUG },
1996-11-26 19:48:08 +03:00
#endif
{ NULL, 0 }
1996-11-26 19:48:08 +03:00
};
/* severity_map - lookup facility or severity value */
2012-03-21 14:10:36 +04:00
static int
severity_map(const struct syslog_names *table, char *name)
1996-11-26 19:48:08 +03:00
{
2012-03-21 14:10:36 +04:00
const struct syslog_names *t;
1996-11-26 19:48:08 +03:00
for (t = table; t->name; t++)
if (STR_EQ(t->name, name))
return (t->value);
tcpd_jump("bad syslog facility or severity: \"%s\"", name);
/* NOTREACHED */
return -1;
1996-11-26 19:48:08 +03:00
}
/* severity_option - change logging severity for this event (Dave Mitchell) */
/* ARGSUSED */
2012-03-21 14:10:36 +04:00
static void
severity_option(char *value, struct request_info *request)
1996-11-26 19:48:08 +03:00
{
char *level = split_at(value, '.');
allow_severity = deny_severity = level ?
severity_map(log_fac, value) | severity_map(log_sev, level) :
severity_map(log_sev, value);
}
/* get_field - return pointer to next field in string */
2012-03-21 14:10:36 +04:00
static char *
get_field(char *string)
1996-11-26 19:48:08 +03:00
{
2012-03-21 14:10:36 +04:00
static char nul = '\0';
static char *last = &nul;
1996-11-26 19:48:08 +03:00
char *src;
char *dst;
char *ret;
int ch;
/*
* This function returns pointers to successive fields within a given
* string. ":" is the field separator; warn if the rule ends in one. It
* replaces a "\:" sequence by ":", without treating the result of
* substitution as field terminator. A null argument means resume search
* where the previous call terminated. This function destroys its
* argument.
*
1996-11-26 19:48:08 +03:00
* Work from explicit source or from memory. While processing \: we
* overwrite the input. This way we do not have to maintain buffers for
* copies of input fields.
*/
src = dst = ret = (string ? string : last);
if (src[0] == 0)
return (0);
while ((ch = *src) != '\0') {
1996-11-26 19:48:08 +03:00
if (ch == ':') {
if (*++src == 0)
tcpd_warn("rule ends in \":\"");
break;
}
if (ch == '\\' && src[1] == ':')
src++;
*dst++ = *src++;
}
last = src;
*dst = 0;
return (ret);
}
/* chop_string - strip leading and trailing blanks from string */
2012-03-21 14:10:36 +04:00
static char *
chop_string(register char *string)
1996-11-26 19:48:08 +03:00
{
char *start = NULL;
char *end = NULL;
1996-11-26 19:48:08 +03:00
char *cp;
for (cp = string; *cp; cp++) {
if (!isspace((unsigned char) *cp)) {
1996-11-26 19:48:08 +03:00
if (start == 0)
start = cp;
end = cp;
}
}
return (start ? (end[1] = 0, start) : cp);
}