version 1.1.1.1, 2012/02/21 16:23:02
|
version 1.1.1.4, 2013/07/22 10:46:13
|
Line 1
|
Line 1
|
/* |
/* |
* Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com> | * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
Line 17
|
Line 17
|
#include <config.h> |
#include <config.h> |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/param.h> |
|
#include <sys/stat.h> |
#include <sys/stat.h> |
#include <stdio.h> |
#include <stdio.h> |
#ifdef STDC_HEADERS |
#ifdef STDC_HEADERS |
Line 47
|
Line 46
|
#include "sudo.h" |
#include "sudo.h" |
#include "sudo_plugin.h" |
#include "sudo_plugin.h" |
#include "sudo_plugin_int.h" |
#include "sudo_plugin_int.h" |
|
#include "sudo_conf.h" |
|
#include "sudo_debug.h" |
|
|
#ifndef RTLD_GLOBAL |
#ifndef RTLD_GLOBAL |
# define RTLD_GLOBAL 0 |
# define RTLD_GLOBAL 0 |
#endif |
#endif |
|
|
#ifdef _PATH_SUDO_NOEXEC | #ifndef SUDOERS_PLUGIN |
const char *noexec_path = _PATH_SUDO_NOEXEC; | # define SUDOERS_PLUGIN "sudoers.la" |
#endif |
#endif |
|
|
/* | #ifdef _PATH_SUDO_PLUGIN_DIR |
* Read in /etc/sudo.conf | static int |
* Returns a list of plugins. | sudo_stat_plugin(struct plugin_info *info, char *fullpath, |
*/ | size_t pathsize, struct stat *sb) |
static struct plugin_info_list * | |
sudo_read_conf(const char *conf_file) | |
{ |
{ |
FILE *fp; | int status = -1; |
char *cp, *name, *path; | debug_decl(sudo_stat_plugin, SUDO_DEBUG_PLUGIN) |
struct plugin_info *info; | |
static struct plugin_info_list pil; /* XXX */ | |
|
|
if ((fp = fopen(conf_file, "r")) == NULL) | if (info->path[0] == '/') { |
goto done; | if (strlcpy(fullpath, info->path, pathsize) >= pathsize) { |
| warningx(_("error in %s, line %d while loading plugin `%s'"), |
while ((cp = sudo_parseln(fp)) != NULL) { | _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
/* Skip blank or comment lines */ | warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG)); |
if (*cp == '\0') | goto done; |
continue; | } |
| status = stat(fullpath, sb); |
/* Look for a line starting with "Path" */ | } else { |
if (strncasecmp(cp, "Path", 4) == 0) { | if (snprintf(fullpath, pathsize, "%s%s", _PATH_SUDO_PLUGIN_DIR, |
/* Parse line */ | info->path) >= pathsize) { |
if ((name = strtok(cp + 4, " \t")) == NULL || | warningx(_("error in %s, line %d while loading plugin `%s'"), |
(path = strtok(NULL, " \t")) == NULL) { | _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
continue; | warningx(_("%s%s: %s"), _PATH_SUDO_PLUGIN_DIR, info->path, |
| strerror(ENAMETOOLONG)); |
| goto done; |
| } |
| /* Try parent dir for compatibility with old plugindir default. */ |
| if ((status = stat(fullpath, sb)) != 0) { |
| char *cp = strrchr(fullpath, '/'); |
| if (cp > fullpath + 4 && cp[-5] == '/' && cp[-4] == 's' && |
| cp[-3] == 'u' && cp[-2] == 'd' && cp[-1] == 'o') { |
| int serrno = errno; |
| strlcpy(cp - 4, info->path, pathsize - (cp - 4 - fullpath)); |
| if ((status = stat(fullpath, sb)) != 0) |
| errno = serrno; |
} |
} |
if (strcasecmp(name, "askpass") == 0) |
|
askpass_path = estrdup(path); |
|
#ifdef _PATH_SUDO_NOEXEC |
|
else if (strcasecmp(name, "noexec") == 0) |
|
noexec_path = estrdup(path); |
|
#endif |
|
continue; |
|
} |
} |
|
# ifdef __hpux |
|
/* Try .sl instead of .so on HP-UX for backwards compatibility. */ |
|
if (status != 0) { |
|
size_t len = strlen(info->path); |
|
if (len >= 3 && info->path[len - 3] == '.' && |
|
info->path[len - 2] == 's' && info->path[len - 1] == 'o') { |
|
const char *sopath = info->path; |
|
char *slpath = estrdup(info->path); |
|
int serrno = errno; |
|
|
/* Look for a line starting with "Plugin" */ | slpath[len - 1] = 'l'; |
if (strncasecmp(cp, "Plugin", 6) == 0) { | info->path = slpath; |
/* Parse line */ | status = sudo_stat_plugin(info, fullpath, pathsize, sb); |
if ((name = strtok(cp + 6, " \t")) == NULL || | if (status == 0) { |
(path = strtok(NULL, " \t")) == NULL) { | efree((void *)sopath); |
continue; | } else { |
| efree(slpath); |
| info->path = sopath; |
| errno = serrno; |
| } |
} |
} |
info = emalloc(sizeof(*info)); |
|
info->symbol_name = estrdup(name); |
|
info->path = estrdup(path); |
|
info->prev = info; |
|
info->next = NULL; |
|
tq_append(&pil, info); |
|
continue; |
|
} |
} |
|
# endif /* __hpux */ |
} |
} |
fclose(fp); |
|
|
|
done: |
done: |
if (tq_empty(&pil)) { | debug_return_int(status); |
/* Default policy plugin */ | } |
info = emalloc(sizeof(*info)); | |
info->symbol_name = "sudoers_policy"; | |
info->path = SUDOERS_PLUGIN; | |
info->prev = info; | |
info->next = NULL; | |
tq_append(&pil, info); | |
|
|
/* Default I/O plugin */ | static bool |
info = emalloc(sizeof(*info)); | sudo_check_plugin(struct plugin_info *info, char *fullpath, size_t pathsize) |
info->symbol_name = "sudoers_io"; | { |
info->path = SUDOERS_PLUGIN; | struct stat sb; |
info->prev = info; | int rval = false; |
info->next = NULL; | debug_decl(sudo_check_plugin, SUDO_DEBUG_PLUGIN) |
tq_append(&pil, info); | |
| if (sudo_stat_plugin(info, fullpath, pathsize, &sb) != 0) { |
| warningx(_("error in %s, line %d while loading plugin `%s'"), |
| _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
| warning("%s%s", _PATH_SUDO_PLUGIN_DIR, info->path); |
| goto done; |
} |
} |
|
if (sb.st_uid != ROOT_UID) { |
|
warningx(_("error in %s, line %d while loading plugin `%s'"), |
|
_PATH_SUDO_CONF, info->lineno, info->symbol_name); |
|
warningx(_("%s must be owned by uid %d"), fullpath, ROOT_UID); |
|
goto done; |
|
} |
|
if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) { |
|
warningx(_("error in %s, line %d while loading plugin `%s'"), |
|
_PATH_SUDO_CONF, info->lineno, info->symbol_name); |
|
warningx(_("%s must be only be writable by owner"), fullpath); |
|
goto done; |
|
} |
|
rval = true; |
|
|
return &pil; | done: |
| debug_return_bool(rval); |
} |
} |
|
#else |
|
static bool |
|
sudo_check_plugin(struct plugin_info *info, char *fullpath, size_t pathsize) |
|
{ |
|
debug_decl(sudo_check_plugin, SUDO_DEBUG_PLUGIN) |
|
(void)strlcpy(fullpath, info->path, pathsize); |
|
debug_return_bool(true); |
|
} |
|
#endif /* _PATH_SUDO_PLUGIN_DIR */ |
|
|
/* |
/* |
* Load the plugins listed in conf_file. | * Load the plugin specified by "info". |
*/ |
*/ |
int | static bool |
sudo_load_plugins(const char *conf_file, | sudo_load_plugin(struct plugin_container *policy_plugin, |
struct plugin_container *policy_plugin, | struct plugin_container_list *io_plugins, struct plugin_info *info) |
struct plugin_container_list *io_plugins) | |
{ |
{ |
struct generic_plugin *plugin; |
|
struct plugin_container *container; |
struct plugin_container *container; |
struct plugin_info *info; | struct generic_plugin *plugin; |
struct plugin_info_list *plugin_list; | |
struct stat sb; | |
void *handle; | |
char path[PATH_MAX]; |
char path[PATH_MAX]; |
int rval = FALSE; | bool rval = false; |
| void *handle; |
| debug_decl(sudo_load_plugin, SUDO_DEBUG_PLUGIN) |
|
|
/* Parse sudo.conf */ | /* Sanity check plugin and fill in path */ |
plugin_list = sudo_read_conf(conf_file); | if (!sudo_check_plugin(info, path, sizeof(path))) |
| goto done; |
|
|
tq_foreach_fwd(plugin_list, info) { | /* Open plugin and map in symbol */ |
if (info->path[0] == '/') { | handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL); |
if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) { | if (!handle) { |
warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG)); | warningx(_("error in %s, line %d while loading plugin `%s'"), |
goto done; | _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
} | warningx(_("unable to dlopen %s: %s"), path, dlerror()); |
} else { | goto done; |
if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR, | } |
info->path) >= sizeof(path)) { | plugin = dlsym(handle, info->symbol_name); |
warningx(_("%s%s: %s"), _PATH_SUDO_PLUGIN_DIR, info->path, | if (!plugin) { |
strerror(ENAMETOOLONG)); | warningx(_("error in %s, line %d while loading plugin `%s'"), |
goto done; | _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
} | warningx(_("unable to find symbol `%s' in %s"), info->symbol_name, path); |
} | goto done; |
if (stat(path, &sb) != 0) { | } |
warning("%s", path); | |
goto done; | |
} | |
if (sb.st_uid != ROOT_UID) { | |
warningx(_("%s must be owned by uid %d"), path, ROOT_UID); | |
goto done; | |
} | |
if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) { | |
warningx(_("%s must be only be writable by owner"), path); | |
goto done; | |
} | |
|
|
/* Open plugin and map in symbol */ | if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) { |
handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL); | warningx(_("error in %s, line %d while loading plugin `%s'"), |
if (!handle) { | _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
warningx(_("unable to dlopen %s: %s"), path, dlerror()); | warningx(_("unknown policy type %d found in %s"), plugin->type, path); |
goto done; | goto done; |
} | } |
plugin = dlsym(handle, info->symbol_name); | if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) { |
if (!plugin) { | warningx(_("error in %s, line %d while loading plugin `%s'"), |
warningx(_("%s: unable to find symbol %s"), path, | _PATH_SUDO_CONF, info->lineno, info->symbol_name); |
info->symbol_name); | warningx(_("incompatible plugin major version %d (expected %d) found in %s"), |
goto done; | SUDO_API_VERSION_GET_MAJOR(plugin->version), |
} | SUDO_API_VERSION_MAJOR, path); |
| goto done; |
if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) { | } |
warningx(_("%s: unknown policy type %d"), path, plugin->type); | if (plugin->type == SUDO_POLICY_PLUGIN) { |
goto done; | if (policy_plugin->handle) { |
} | /* Ignore duplicate entries. */ |
if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) { | if (strcmp(policy_plugin->name, info->symbol_name) != 0) { |
warningx(_("%s: incompatible policy major version %d, expected %d"), | warningx(_("ignoring policy plugin `%s' in %s, line %d"), |
path, SUDO_API_VERSION_GET_MAJOR(plugin->version), | info->symbol_name, _PATH_SUDO_CONF, info->lineno); |
SUDO_API_VERSION_MAJOR); | warningx(_("only a single policy plugin may be specified")); |
goto done; | |
} | |
if (plugin->type == SUDO_POLICY_PLUGIN) { | |
if (policy_plugin->handle) { | |
warningx(_("%s: only a single policy plugin may be loaded"), | |
conf_file); | |
goto done; |
goto done; |
} |
} |
|
warningx(_("ignoring duplicate policy plugin `%s' in %s, line %d"), |
|
info->symbol_name, _PATH_SUDO_CONF, info->lineno); |
|
dlclose(handle); |
|
handle = NULL; |
|
} |
|
if (handle != NULL) { |
policy_plugin->handle = handle; |
policy_plugin->handle = handle; |
policy_plugin->name = info->symbol_name; |
policy_plugin->name = info->symbol_name; |
|
policy_plugin->options = info->options; |
policy_plugin->u.generic = plugin; |
policy_plugin->u.generic = plugin; |
} else if (plugin->type == SUDO_IO_PLUGIN) { | } |
container = emalloc(sizeof(*container)); | } else if (plugin->type == SUDO_IO_PLUGIN) { |
| /* Check for duplicate entries. */ |
| tq_foreach_fwd(io_plugins, container) { |
| if (strcmp(container->name, info->symbol_name) == 0) { |
| warningx(_("ignoring duplicate I/O plugin `%s' in %s, line %d"), |
| info->symbol_name, _PATH_SUDO_CONF, info->lineno); |
| dlclose(handle); |
| handle = NULL; |
| break; |
| } |
| } |
| if (handle != NULL) { |
| container = ecalloc(1, sizeof(*container)); |
container->prev = container; |
container->prev = container; |
container->next = NULL; | /* container->next = NULL; */ |
container->handle = handle; |
container->handle = handle; |
container->name = info->symbol_name; |
container->name = info->symbol_name; |
|
container->options = info->options; |
container->u.generic = plugin; |
container->u.generic = plugin; |
tq_append(io_plugins, container); |
tq_append(io_plugins, container); |
} |
} |
} |
} |
|
|
|
rval = true; |
|
done: |
|
debug_return_bool(rval); |
|
} |
|
|
|
/* |
|
* Load the plugins listed in sudo.conf. |
|
*/ |
|
bool |
|
sudo_load_plugins(struct plugin_container *policy_plugin, |
|
struct plugin_container_list *io_plugins) |
|
{ |
|
struct plugin_container *container; |
|
struct plugin_info_list *plugins; |
|
struct plugin_info *info; |
|
bool rval = false; |
|
debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN) |
|
|
|
/* Walk the plugin list from sudo.conf, if any. */ |
|
plugins = sudo_conf_plugins(); |
|
tq_foreach_fwd(plugins, info) { |
|
rval = sudo_load_plugin(policy_plugin, io_plugins, info); |
|
if (!rval) |
|
goto done; |
|
} |
|
|
|
/* |
|
* If no policy plugin, fall back to the default (sudoers). |
|
* If there is also no I/O log plugin, sudoers for that too. |
|
*/ |
if (policy_plugin->handle == NULL) { |
if (policy_plugin->handle == NULL) { |
warningx(_("%s: at least one policy plugin must be specified"), | /* Default policy plugin */ |
conf_file); | info = ecalloc(1, sizeof(*info)); |
goto done; | info->symbol_name = "sudoers_policy"; |
| info->path = SUDOERS_PLUGIN; |
| /* info->options = NULL; */ |
| info->prev = info; |
| /* info->next = NULL; */ |
| rval = sudo_load_plugin(policy_plugin, io_plugins, info); |
| efree(info); |
| if (!rval) |
| goto done; |
| |
| /* Default I/O plugin */ |
| if (tq_empty(io_plugins)) { |
| info = ecalloc(1, sizeof(*info)); |
| info->symbol_name = "sudoers_io"; |
| info->path = SUDOERS_PLUGIN; |
| /* info->options = NULL; */ |
| info->prev = info; |
| /* info->next = NULL; */ |
| rval = sudo_load_plugin(policy_plugin, io_plugins, info); |
| efree(info); |
| if (!rval) |
| goto done; |
| } |
} |
} |
if (policy_plugin->u.policy->check_policy == NULL) { |
if (policy_plugin->u.policy->check_policy == NULL) { |
warningx(_("policy plugin %s does not include a check_policy method"), |
warningx(_("policy plugin %s does not include a check_policy method"), |
policy_plugin->name); |
policy_plugin->name); |
|
rval = false; |
goto done; |
goto done; |
} |
} |
|
|
rval = TRUE; | /* Install hooks (XXX - later). */ |
| if (policy_plugin->u.policy->version >= SUDO_API_MKVERSION(1, 2)) { |
| if (policy_plugin->u.policy->register_hooks != NULL) |
| policy_plugin->u.policy->register_hooks(SUDO_HOOK_VERSION, register_hook); |
| } |
| tq_foreach_fwd(io_plugins, container) { |
| if (container->u.io->version >= SUDO_API_MKVERSION(1, 2)) { |
| if (container->u.io->register_hooks != NULL) |
| container->u.io->register_hooks(SUDO_HOOK_VERSION, register_hook); |
| } |
| } |
|
|
done: |
done: |
return rval; | debug_return_bool(rval); |
} |
} |