2 * Copyright (c) 2009-2011 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>
20 #include <sys/param.h>
30 #endif /* STDC_HEADERS */
33 #endif /* HAVE_STRING_H */
36 #endif /* HAVE_STRINGS_H */
39 #endif /* HAVE_UNISTD_H */
43 # include "compat/dlfcn.h"
48 #include "sudo_plugin.h"
49 #include "sudo_plugin_int.h"
50 #include "sudo_conf.h"
51 #include "sudo_debug.h"
54 # define RTLD_GLOBAL 0
58 * Load the plugin specified by "info".
61 sudo_load_plugin(struct plugin_container *policy_plugin,
62 struct plugin_container_list *io_plugins, struct plugin_info *info)
64 struct plugin_container *container;
65 struct generic_plugin *plugin;
70 debug_decl(sudo_load_plugin, SUDO_DEBUG_PLUGIN)
72 if (info->path[0] == '/') {
73 if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) {
74 warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG));
78 if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR,
79 info->path) >= sizeof(path)) {
80 warningx(_("%s%s: %s"), _PATH_SUDO_PLUGIN_DIR, info->path,
81 strerror(ENAMETOOLONG));
85 if (stat(path, &sb) != 0) {
89 if (sb.st_uid != ROOT_UID) {
90 warningx(_("%s must be owned by uid %d"), path, ROOT_UID);
93 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
94 warningx(_("%s must be only be writable by owner"), path);
98 /* Open plugin and map in symbol */
99 handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
101 warningx(_("unable to dlopen %s: %s"), path, dlerror());
104 plugin = dlsym(handle, info->symbol_name);
106 warningx(_("%s: unable to find symbol %s"), path,
111 if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
112 warningx(_("%s: unknown policy type %d"), path, plugin->type);
115 if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
116 warningx(_("%s: incompatible policy major version %d, expected %d"),
117 path, SUDO_API_VERSION_GET_MAJOR(plugin->version),
118 SUDO_API_VERSION_MAJOR);
121 if (plugin->type == SUDO_POLICY_PLUGIN) {
122 if (policy_plugin->handle) {
123 warningx(_("%s: only a single policy plugin may be loaded"),
127 policy_plugin->handle = handle;
128 policy_plugin->name = info->symbol_name;
129 policy_plugin->options = info->options;
130 policy_plugin->u.generic = plugin;
131 } else if (plugin->type == SUDO_IO_PLUGIN) {
132 container = ecalloc(1, sizeof(*container));
133 container->prev = container;
134 /* container->next = NULL; */
135 container->handle = handle;
136 container->name = info->symbol_name;
137 container->options = info->options;
138 container->u.generic = plugin;
139 tq_append(io_plugins, container);
144 debug_return_bool(rval);
148 * Load the plugins listed in sudo.conf.
151 sudo_load_plugins(struct plugin_container *policy_plugin,
152 struct plugin_container_list *io_plugins)
154 struct plugin_container *container;
155 struct plugin_info_list *plugins;
156 struct plugin_info *info;
158 debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN)
160 /* Walk the plugin list from sudo.conf, if any. */
161 plugins = sudo_conf_plugins();
162 tq_foreach_fwd(plugins, info) {
163 rval = sudo_load_plugin(policy_plugin, io_plugins, info);
169 * If no policy plugin, fall back to the default (sudoers).
170 * If there is also no I/O log plugin, sudoers for that too.
172 if (policy_plugin->handle == NULL) {
173 /* Default policy plugin */
174 info = ecalloc(1, sizeof(*info));
175 info->symbol_name = "sudoers_policy";
176 info->path = SUDOERS_PLUGIN;
177 /* info->options = NULL; */
179 /* info->next = NULL; */
180 rval = sudo_load_plugin(policy_plugin, io_plugins, info);
185 /* Default I/O plugin */
186 if (tq_empty(io_plugins)) {
187 info = ecalloc(1, sizeof(*info));
188 info->symbol_name = "sudoers_io";
189 info->path = SUDOERS_PLUGIN;
190 /* info->options = NULL; */
192 /* info->next = NULL; */
193 rval = sudo_load_plugin(policy_plugin, io_plugins, info);
199 if (policy_plugin->u.policy->check_policy == NULL) {
200 warningx(_("policy plugin %s does not include a check_policy method"),
201 policy_plugin->name);
206 /* Install hooks (XXX - later). */
207 if (policy_plugin->u.policy->version >= SUDO_API_MKVERSION(1, 2)) {
208 if (policy_plugin->u.policy->register_hooks != NULL)
209 policy_plugin->u.policy->register_hooks(SUDO_HOOK_VERSION, register_hook);
211 tq_foreach_fwd(io_plugins, container) {
212 if (container->u.io->version >= SUDO_API_MKVERSION(1, 2)) {
213 if (container->u.io->register_hooks != NULL)
214 container->u.io->register_hooks(SUDO_HOOK_VERSION, register_hook);
219 debug_return_bool(rval);