2010-02-20 09:41:49 -05:00
|
|
|
/*
|
2011-02-16 12:15:46 -05:00
|
|
|
* Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
|
2010-02-20 09:41:49 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif /* HAVE_STRING_H */
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2010-02-20 09:41:49 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* HAVE_UNISTD_H */
|
2010-10-11 17:39:51 -04:00
|
|
|
#ifdef HAVE_DLOPEN
|
2010-09-26 17:41:35 -04:00
|
|
|
# include <dlfcn.h>
|
|
|
|
#else
|
|
|
|
# include "compat/dlfcn.h"
|
|
|
|
#endif
|
2011-10-22 14:00:52 -04:00
|
|
|
#include <ctype.h>
|
2010-02-20 09:41:49 -05:00
|
|
|
#include <errno.h>
|
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
#define SUDO_ERROR_WRAP 0
|
|
|
|
|
2010-02-20 09:41:49 -05:00
|
|
|
#include "sudo.h"
|
|
|
|
#include "sudo_plugin.h"
|
|
|
|
#include "sudo_plugin_int.h"
|
2011-10-22 14:00:52 -04:00
|
|
|
#include "sudo_debug.h"
|
2010-02-20 09:41:49 -05:00
|
|
|
|
2011-06-06 10:53:58 -04:00
|
|
|
#ifndef RTLD_GLOBAL
|
|
|
|
# define RTLD_GLOBAL 0
|
2010-09-26 17:41:35 -04:00
|
|
|
#endif
|
|
|
|
|
2011-07-05 12:20:10 -04:00
|
|
|
#ifdef _PATH_SUDO_NOEXEC
|
2011-03-10 16:12:33 -05:00
|
|
|
const char *noexec_path = _PATH_SUDO_NOEXEC;
|
2011-07-05 12:20:10 -04:00
|
|
|
#endif
|
2011-03-10 16:12:33 -05:00
|
|
|
|
2011-10-22 14:00:52 -04:00
|
|
|
/* XXX - for parse_args() */
|
|
|
|
const char *debug_flags;
|
|
|
|
|
|
|
|
struct sudo_conf_table {
|
|
|
|
const char *name;
|
|
|
|
unsigned int namelen;
|
|
|
|
int (*setter)(const char *entry, void *data);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sudo_conf_paths {
|
|
|
|
const char *pname;
|
|
|
|
unsigned int pnamelen;
|
|
|
|
const char **pval;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int set_debug(const char *entry, void *data);
|
|
|
|
static int set_path(const char *entry, void *data);
|
|
|
|
static int set_plugin(const char *entry, void *data);
|
|
|
|
|
|
|
|
static struct plugin_info_list plugin_info_list;
|
|
|
|
|
|
|
|
static struct sudo_conf_table sudo_conf_table[] = {
|
|
|
|
{ "Debug", sizeof("Debug") - 1, set_debug },
|
|
|
|
{ "Path", sizeof("Path") - 1, set_path },
|
|
|
|
{ "Plugin", sizeof("Plugin") - 1, set_plugin },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct sudo_conf_paths sudo_conf_paths[] = {
|
|
|
|
{ "askpass", sizeof("askpass"), &askpass_path },
|
|
|
|
#ifdef _PATH_SUDO_NOEXEC
|
|
|
|
{ "noexec", sizeof("noexec"), &noexec_path },
|
|
|
|
#endif
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-11-08 08:24:45 -05:00
|
|
|
/*
|
|
|
|
* "Debug progname debug_file debug_flags"
|
|
|
|
*/
|
2011-10-22 14:00:52 -04:00
|
|
|
static int
|
|
|
|
set_debug(const char *entry, void *data)
|
|
|
|
{
|
2011-11-08 08:24:45 -05:00
|
|
|
size_t filelen, proglen;
|
|
|
|
const char *progname;
|
2011-11-30 14:56:41 -05:00
|
|
|
char *debug_file;
|
2011-11-08 08:24:45 -05:00
|
|
|
|
|
|
|
/* Is this debug setting for me? */
|
|
|
|
progname = getprogname();
|
|
|
|
if (strcmp(progname, "sudoedit") == 0)
|
|
|
|
progname = "sudo";
|
|
|
|
proglen = strlen(progname);
|
|
|
|
if (strncmp(entry, progname, proglen) != 0 ||
|
|
|
|
!isblank((unsigned char)entry[proglen]))
|
|
|
|
return FALSE;
|
|
|
|
entry += proglen + 1;
|
|
|
|
while (isblank((unsigned char)*entry))
|
|
|
|
entry++;
|
2011-10-22 14:00:52 -04:00
|
|
|
|
|
|
|
debug_flags = strpbrk(entry, " \t");
|
|
|
|
if (debug_flags == NULL)
|
|
|
|
return FALSE;
|
|
|
|
filelen = (size_t)(debug_flags - entry);
|
|
|
|
while (isblank((unsigned char)*debug_flags))
|
|
|
|
debug_flags++;
|
|
|
|
|
|
|
|
/* Set debug file and parse the flags. */
|
|
|
|
debug_file = estrndup(entry, filelen);
|
|
|
|
debug_flags = estrdup(debug_flags);
|
|
|
|
sudo_debug_init(debug_file, debug_flags);
|
2011-11-30 14:56:41 -05:00
|
|
|
efree(debug_file);
|
2011-10-22 14:00:52 -04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_path(const char *entry, void *data)
|
|
|
|
{
|
|
|
|
const char *name, *path;
|
|
|
|
struct sudo_conf_paths *cur;
|
|
|
|
|
|
|
|
/* Parse Path line */
|
|
|
|
name = entry;
|
|
|
|
path = strpbrk(entry, " \t");
|
|
|
|
if (path == NULL)
|
|
|
|
return FALSE;
|
|
|
|
while (isblank((unsigned char)*path))
|
|
|
|
path++;
|
|
|
|
|
|
|
|
/* Match supported paths, ignore the rest. */
|
|
|
|
for (cur = sudo_conf_paths; cur->pname != NULL; cur++) {
|
|
|
|
if (strncasecmp(name, cur->pname, cur->pnamelen) == 0 &&
|
|
|
|
isblank((unsigned char)name[cur->pnamelen])) {
|
|
|
|
*(cur->pval) = estrdup(path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_plugin(const char *entry, void *data)
|
|
|
|
{
|
|
|
|
struct plugin_info_list *pil = data;
|
|
|
|
struct plugin_info *info;
|
|
|
|
const char *name, *path;
|
|
|
|
size_t namelen;
|
|
|
|
|
|
|
|
/* Parse Plugin line */
|
|
|
|
name = entry;
|
|
|
|
path = strpbrk(entry, " \t");
|
|
|
|
if (path == NULL)
|
|
|
|
return FALSE;
|
|
|
|
namelen = (size_t)(path - name);
|
|
|
|
while (isblank((unsigned char)*path))
|
|
|
|
path++;
|
|
|
|
|
|
|
|
info = emalloc(sizeof(*info));
|
|
|
|
info->symbol_name = estrndup(name, namelen);
|
|
|
|
info->path = estrdup(path);
|
|
|
|
info->prev = info;
|
|
|
|
info->next = NULL;
|
|
|
|
tq_append(pil, info);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-02-20 09:41:49 -05:00
|
|
|
/*
|
2011-10-22 14:00:52 -04:00
|
|
|
* Reads in /etc/sudo.conf
|
2010-02-20 09:41:49 -05:00
|
|
|
* Returns a list of plugins.
|
|
|
|
*/
|
2011-10-22 14:00:52 -04:00
|
|
|
void
|
|
|
|
sudo_read_conf(void)
|
2010-02-20 09:41:49 -05:00
|
|
|
{
|
2011-10-22 14:00:52 -04:00
|
|
|
struct sudo_conf_table *cur;
|
2010-02-20 09:41:49 -05:00
|
|
|
struct plugin_info *info;
|
2011-10-22 14:00:52 -04:00
|
|
|
FILE *fp;
|
|
|
|
char *cp;
|
2010-02-20 09:41:49 -05:00
|
|
|
|
2011-10-22 14:00:52 -04:00
|
|
|
if ((fp = fopen(_PATH_SUDO_CONF, "r")) == NULL)
|
2010-02-20 09:41:49 -05:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
while ((cp = sudo_parseln(fp)) != NULL) {
|
|
|
|
/* Skip blank or comment lines */
|
|
|
|
if (*cp == '\0')
|
|
|
|
continue;
|
|
|
|
|
2011-10-22 14:00:52 -04:00
|
|
|
for (cur = sudo_conf_table; cur->name != NULL; cur++) {
|
|
|
|
if (strncasecmp(cp, cur->name, cur->namelen) == 0 &&
|
|
|
|
isblank((unsigned char)cp[cur->namelen])) {
|
|
|
|
cp += cur->namelen;
|
|
|
|
while (isblank((unsigned char)*cp))
|
|
|
|
cp++;
|
|
|
|
if (cur->setter(cp, &plugin_info_list))
|
|
|
|
break;
|
2010-06-09 17:40:44 -04:00
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
done:
|
2011-10-22 14:00:52 -04:00
|
|
|
if (tq_empty(&plugin_info_list)) {
|
2011-02-16 12:15:46 -05:00
|
|
|
/* Default policy plugin */
|
|
|
|
info = emalloc(sizeof(*info));
|
|
|
|
info->symbol_name = "sudoers_policy";
|
|
|
|
info->path = SUDOERS_PLUGIN;
|
|
|
|
info->prev = info;
|
|
|
|
info->next = NULL;
|
2011-10-22 14:00:52 -04:00
|
|
|
tq_append(&plugin_info_list, info);
|
2011-02-16 12:15:46 -05:00
|
|
|
|
|
|
|
/* Default I/O plugin */
|
|
|
|
info = emalloc(sizeof(*info));
|
|
|
|
info->symbol_name = "sudoers_io";
|
|
|
|
info->path = SUDOERS_PLUGIN;
|
|
|
|
info->prev = info;
|
|
|
|
info->next = NULL;
|
2011-10-22 14:00:52 -04:00
|
|
|
tq_append(&plugin_info_list, info);
|
2011-02-16 12:15:46 -05:00
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-10-22 14:00:52 -04:00
|
|
|
* Load the plugins listed in sudo.conf.
|
2010-02-20 09:41:49 -05:00
|
|
|
*/
|
2011-04-06 17:51:36 -04:00
|
|
|
int
|
2011-10-22 14:00:52 -04:00
|
|
|
sudo_load_plugins(struct plugin_container *policy_plugin,
|
2010-02-20 09:41:49 -05:00
|
|
|
struct plugin_container_list *io_plugins)
|
|
|
|
{
|
|
|
|
struct generic_plugin *plugin;
|
|
|
|
struct plugin_container *container;
|
|
|
|
struct plugin_info *info;
|
|
|
|
struct stat sb;
|
|
|
|
void *handle;
|
|
|
|
char path[PATH_MAX];
|
2011-04-06 17:51:36 -04:00
|
|
|
int rval = FALSE;
|
2010-02-20 09:41:49 -05:00
|
|
|
|
2011-10-22 14:00:52 -04:00
|
|
|
/* Walk plugin list. */
|
|
|
|
tq_foreach_fwd(&plugin_info_list, info) {
|
2010-02-20 09:41:49 -05:00
|
|
|
if (info->path[0] == '/') {
|
2011-04-06 17:51:36 -04:00
|
|
|
if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG));
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
} else {
|
2010-07-02 10:53:47 -04:00
|
|
|
if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR,
|
2010-02-20 09:41:49 -05:00
|
|
|
info->path) >= sizeof(path)) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s%s: %s"), _PATH_SUDO_PLUGIN_DIR, info->path,
|
2010-02-20 09:41:49 -05:00
|
|
|
strerror(ENAMETOOLONG));
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
2010-02-20 09:41:49 -05:00
|
|
|
}
|
|
|
|
}
|
2011-04-06 17:51:36 -04:00
|
|
|
if (stat(path, &sb) != 0) {
|
|
|
|
warning("%s", path);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (sb.st_uid != ROOT_UID) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s must be owned by uid %d"), path, ROOT_UID);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s must be only be writable by owner"), path);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
|
|
|
|
/* Open plugin and map in symbol */
|
2011-06-06 10:53:58 -04:00
|
|
|
handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
|
2011-04-06 17:51:36 -04:00
|
|
|
if (!handle) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("unable to dlopen %s: %s"), path, dlerror());
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
plugin = dlsym(handle, info->symbol_name);
|
2011-04-06 17:51:36 -04:00
|
|
|
if (!plugin) {
|
2011-05-18 13:04:24 -04:00
|
|
|
warningx(_("%s: unable to find symbol %s"), path,
|
|
|
|
info->symbol_name);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
|
|
|
|
if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s: unknown policy type %d"), path, plugin->type);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
2010-02-20 09:41:49 -05:00
|
|
|
}
|
|
|
|
if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s: incompatible policy major version %d, expected %d"),
|
2010-02-20 09:41:49 -05:00
|
|
|
path, SUDO_API_VERSION_GET_MAJOR(plugin->version),
|
|
|
|
SUDO_API_VERSION_MAJOR);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
2010-02-20 09:41:49 -05:00
|
|
|
}
|
|
|
|
if (plugin->type == SUDO_POLICY_PLUGIN) {
|
2011-04-06 17:51:36 -04:00
|
|
|
if (policy_plugin->handle) {
|
2011-05-18 13:04:24 -04:00
|
|
|
warningx(_("%s: only a single policy plugin may be loaded"),
|
2011-10-22 14:00:52 -04:00
|
|
|
_PATH_SUDO_CONF);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
2010-02-20 09:41:49 -05:00
|
|
|
policy_plugin->handle = handle;
|
|
|
|
policy_plugin->name = info->symbol_name;
|
|
|
|
policy_plugin->u.generic = plugin;
|
|
|
|
} else if (plugin->type == SUDO_IO_PLUGIN) {
|
|
|
|
container = emalloc(sizeof(*container));
|
|
|
|
container->prev = container;
|
|
|
|
container->next = NULL;
|
|
|
|
container->handle = handle;
|
|
|
|
container->name = info->symbol_name;
|
|
|
|
container->u.generic = plugin;
|
|
|
|
tq_append(io_plugins, container);
|
|
|
|
}
|
|
|
|
}
|
2011-04-06 17:51:36 -04:00
|
|
|
if (policy_plugin->handle == NULL) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("%s: at least one policy plugin must be specified"),
|
2011-10-22 14:00:52 -04:00
|
|
|
_PATH_SUDO_CONF);
|
2011-04-06 17:51:36 -04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (policy_plugin->u.policy->check_policy == NULL) {
|
2011-05-06 17:47:51 -04:00
|
|
|
warningx(_("policy plugin %s does not include a check_policy method"),
|
2011-04-06 17:51:36 -04:00
|
|
|
policy_plugin->name);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rval = TRUE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return rval;
|
2010-02-20 09:41:49 -05:00
|
|
|
}
|