2 * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
29 #endif /* STDC_HEADERS */
32 #endif /* HAVE_STRING_H */
35 #endif /* HAVE_STRINGS_H */
38 #endif /* HAVE_UNISTD_H */
42 # include "compat/dlfcn.h"
47 #include "sudo_plugin.h"
48 #include "sudo_plugin_int.h"
49 #include "sudo_conf.h"
50 #include "sudo_debug.h"
53 # define RTLD_GLOBAL 0
56 #ifndef SUDOERS_PLUGIN
57 # define SUDOERS_PLUGIN "sudoers.la"
60 #ifdef _PATH_SUDO_PLUGIN_DIR
62 sudo_stat_plugin(struct plugin_info *info, char *fullpath,
63 size_t pathsize, struct stat *sb)
66 debug_decl(sudo_stat_plugin, SUDO_DEBUG_PLUGIN)
68 if (info->path[0] == '/') {
69 if (strlcpy(fullpath, info->path, pathsize) >= pathsize) {
70 warningx(_("error in %s, line %d while loading plugin `%s'"),
71 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
72 warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG));
75 status = stat(fullpath, sb);
77 if (snprintf(fullpath, pathsize, "%s%s", _PATH_SUDO_PLUGIN_DIR,
78 info->path) >= pathsize) {
79 warningx(_("error in %s, line %d while loading plugin `%s'"),
80 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
81 warningx(_("%s%s: %s"), _PATH_SUDO_PLUGIN_DIR, info->path,
82 strerror(ENAMETOOLONG));
85 /* Try parent dir for compatibility with old plugindir default. */
86 if ((status = stat(fullpath, sb)) != 0) {
87 char *cp = strrchr(fullpath, '/');
88 if (cp > fullpath + 4 && cp[-5] == '/' && cp[-4] == 's' &&
89 cp[-3] == 'u' && cp[-2] == 'd' && cp[-1] == 'o') {
91 strlcpy(cp - 4, info->path, pathsize - (cp - 4 - fullpath));
92 if ((status = stat(fullpath, sb)) != 0)
97 /* Try .sl instead of .so on HP-UX for backwards compatibility. */
99 size_t len = strlen(info->path);
100 if (len >= 3 && info->path[len - 3] == '.' &&
101 info->path[len - 2] == 's' && info->path[len - 1] == 'o') {
102 const char *sopath = info->path;
103 char *slpath = estrdup(info->path);
106 slpath[len - 1] = 'l';
108 status = sudo_stat_plugin(info, fullpath, pathsize, sb);
110 efree((void *)sopath);
121 debug_return_int(status);
125 sudo_check_plugin(struct plugin_info *info, char *fullpath, size_t pathsize)
129 debug_decl(sudo_check_plugin, SUDO_DEBUG_PLUGIN)
131 if (sudo_stat_plugin(info, fullpath, pathsize, &sb) != 0) {
132 warningx(_("error in %s, line %d while loading plugin `%s'"),
133 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
134 warning("%s%s", _PATH_SUDO_PLUGIN_DIR, info->path);
137 if (sb.st_uid != ROOT_UID) {
138 warningx(_("error in %s, line %d while loading plugin `%s'"),
139 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
140 warningx(_("%s must be owned by uid %d"), fullpath, ROOT_UID);
143 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
144 warningx(_("error in %s, line %d while loading plugin `%s'"),
145 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
146 warningx(_("%s must be only be writable by owner"), fullpath);
152 debug_return_bool(rval);
156 sudo_check_plugin(struct plugin_info *info, char *fullpath, size_t pathsize)
158 debug_decl(sudo_check_plugin, SUDO_DEBUG_PLUGIN)
159 (void)strlcpy(fullpath, info->path, pathsize);
160 debug_return_bool(true);
162 #endif /* _PATH_SUDO_PLUGIN_DIR */
165 * Load the plugin specified by "info".
168 sudo_load_plugin(struct plugin_container *policy_plugin,
169 struct plugin_container_list *io_plugins, struct plugin_info *info)
171 struct plugin_container *container;
172 struct generic_plugin *plugin;
176 debug_decl(sudo_load_plugin, SUDO_DEBUG_PLUGIN)
178 /* Sanity check plugin and fill in path */
179 if (!sudo_check_plugin(info, path, sizeof(path)))
182 /* Open plugin and map in symbol */
183 handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
185 warningx(_("error in %s, line %d while loading plugin `%s'"),
186 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
187 warningx(_("unable to dlopen %s: %s"), path, dlerror());
190 plugin = dlsym(handle, info->symbol_name);
192 warningx(_("error in %s, line %d while loading plugin `%s'"),
193 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
194 warningx(_("unable to find symbol `%s' in %s"), info->symbol_name, path);
198 if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
199 warningx(_("error in %s, line %d while loading plugin `%s'"),
200 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
201 warningx(_("unknown policy type %d found in %s"), plugin->type, path);
204 if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
205 warningx(_("error in %s, line %d while loading plugin `%s'"),
206 _PATH_SUDO_CONF, info->lineno, info->symbol_name);
207 warningx(_("incompatible plugin major version %d (expected %d) found in %s"),
208 SUDO_API_VERSION_GET_MAJOR(plugin->version),
209 SUDO_API_VERSION_MAJOR, path);
212 if (plugin->type == SUDO_POLICY_PLUGIN) {
213 if (policy_plugin->handle) {
214 /* Ignore duplicate entries. */
215 if (strcmp(policy_plugin->name, info->symbol_name) != 0) {
216 warningx(_("ignoring policy plugin `%s' in %s, line %d"),
217 info->symbol_name, _PATH_SUDO_CONF, info->lineno);
218 warningx(_("only a single policy plugin may be specified"));
221 warningx(_("ignoring duplicate policy plugin `%s' in %s, line %d"),
222 info->symbol_name, _PATH_SUDO_CONF, info->lineno);
226 if (handle != NULL) {
227 policy_plugin->handle = handle;
228 policy_plugin->name = info->symbol_name;
229 policy_plugin->options = info->options;
230 policy_plugin->u.generic = plugin;
232 } else if (plugin->type == SUDO_IO_PLUGIN) {
233 /* Check for duplicate entries. */
234 tq_foreach_fwd(io_plugins, container) {
235 if (strcmp(container->name, info->symbol_name) == 0) {
236 warningx(_("ignoring duplicate I/O plugin `%s' in %s, line %d"),
237 info->symbol_name, _PATH_SUDO_CONF, info->lineno);
243 if (handle != NULL) {
244 container = ecalloc(1, sizeof(*container));
245 container->prev = container;
246 /* container->next = NULL; */
247 container->handle = handle;
248 container->name = info->symbol_name;
249 container->options = info->options;
250 container->u.generic = plugin;
251 tq_append(io_plugins, container);
257 debug_return_bool(rval);
261 * Load the plugins listed in sudo.conf.
264 sudo_load_plugins(struct plugin_container *policy_plugin,
265 struct plugin_container_list *io_plugins)
267 struct plugin_container *container;
268 struct plugin_info_list *plugins;
269 struct plugin_info *info;
271 debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN)
273 /* Walk the plugin list from sudo.conf, if any. */
274 plugins = sudo_conf_plugins();
275 tq_foreach_fwd(plugins, info) {
276 rval = sudo_load_plugin(policy_plugin, io_plugins, info);
282 * If no policy plugin, fall back to the default (sudoers).
283 * If there is also no I/O log plugin, sudoers for that too.
285 if (policy_plugin->handle == NULL) {
286 /* Default policy plugin */
287 info = ecalloc(1, sizeof(*info));
288 info->symbol_name = "sudoers_policy";
289 info->path = SUDOERS_PLUGIN;
290 /* info->options = NULL; */
292 /* info->next = NULL; */
293 rval = sudo_load_plugin(policy_plugin, io_plugins, info);
298 /* Default I/O plugin */
299 if (tq_empty(io_plugins)) {
300 info = ecalloc(1, sizeof(*info));
301 info->symbol_name = "sudoers_io";
302 info->path = SUDOERS_PLUGIN;
303 /* info->options = NULL; */
305 /* info->next = NULL; */
306 rval = sudo_load_plugin(policy_plugin, io_plugins, info);
312 if (policy_plugin->u.policy->check_policy == NULL) {
313 warningx(_("policy plugin %s does not include a check_policy method"),
314 policy_plugin->name);
319 /* Install hooks (XXX - later). */
320 if (policy_plugin->u.policy->version >= SUDO_API_MKVERSION(1, 2)) {
321 if (policy_plugin->u.policy->register_hooks != NULL)
322 policy_plugin->u.policy->register_hooks(SUDO_HOOK_VERSION, register_hook);
324 tq_foreach_fwd(io_plugins, container) {
325 if (container->u.io->version >= SUDO_API_MKVERSION(1, 2)) {
326 if (container->u.io->register_hooks != NULL)
327 container->u.io->register_hooks(SUDO_HOOK_VERSION, register_hook);
332 debug_return_bool(rval);