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 plugins listed in sudo.conf.
61 sudo_load_plugins(struct plugin_container *policy_plugin,
62 struct plugin_container_list *io_plugins)
64 struct plugin_info_list *plugins;
65 struct generic_plugin *plugin;
66 struct plugin_container *container;
67 struct plugin_info *info;
72 debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN)
74 /* Walk plugin list. */
75 plugins = sudo_conf_plugins();
76 tq_foreach_fwd(plugins, info) {
77 if (info->path[0] == '/') {
78 if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) {
79 warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG));
83 if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR,
84 info->path) >= sizeof(path)) {
85 warningx(_("%s%s: %s"), _PATH_SUDO_PLUGIN_DIR, info->path,
86 strerror(ENAMETOOLONG));
90 if (stat(path, &sb) != 0) {
94 if (sb.st_uid != ROOT_UID) {
95 warningx(_("%s must be owned by uid %d"), path, ROOT_UID);
98 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
99 warningx(_("%s must be only be writable by owner"), path);
103 /* Open plugin and map in symbol */
104 handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
106 warningx(_("unable to dlopen %s: %s"), path, dlerror());
109 plugin = dlsym(handle, info->symbol_name);
111 warningx(_("%s: unable to find symbol %s"), path,
116 if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
117 warningx(_("%s: unknown policy type %d"), path, plugin->type);
120 if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
121 warningx(_("%s: incompatible policy major version %d, expected %d"),
122 path, SUDO_API_VERSION_GET_MAJOR(plugin->version),
123 SUDO_API_VERSION_MAJOR);
126 if (plugin->type == SUDO_POLICY_PLUGIN) {
127 if (policy_plugin->handle) {
128 warningx(_("%s: only a single policy plugin may be loaded"),
132 policy_plugin->handle = handle;
133 policy_plugin->name = info->symbol_name;
134 policy_plugin->options = info->options;
135 policy_plugin->u.generic = plugin;
136 } else if (plugin->type == SUDO_IO_PLUGIN) {
137 container = ecalloc(1, sizeof(*container));
138 container->prev = container;
139 /* container->next = NULL; */
140 container->handle = handle;
141 container->name = info->symbol_name;
142 container->options = info->options;
143 container->u.generic = plugin;
144 tq_append(io_plugins, container);
147 if (policy_plugin->handle == NULL) {
148 warningx(_("%s: at least one policy plugin must be specified"),
152 if (policy_plugin->u.policy->check_policy == NULL) {
153 warningx(_("policy plugin %s does not include a check_policy method"),
154 policy_plugin->name);
158 /* Install hooks (XXX - later). */
159 if (policy_plugin->u.policy->version >= SUDO_API_MKVERSION(1, 2)) {
160 if (policy_plugin->u.policy->register_hooks != NULL)
161 policy_plugin->u.policy->register_hooks(SUDO_HOOK_VERSION, register_hook);
162 tq_foreach_fwd(io_plugins, container) {
163 if (container->u.io->register_hooks != NULL)
164 container->u.io->register_hooks(SUDO_HOOK_VERSION, register_hook);
171 debug_return_bool(rval);