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"
55 const char *noexec_path = _PATH_SUDO_NOEXEC;
58 * Read in /etc/sudo.conf
59 * Returns a list of plugins.
61 static struct plugin_info_list *
62 sudo_read_conf(const char *conf_file)
65 char *cp, *name, *path;
66 struct plugin_info *info;
67 static struct plugin_info_list pil; /* XXX */
69 if ((fp = fopen(conf_file, "r")) == NULL)
72 while ((cp = sudo_parseln(fp)) != NULL) {
73 /* Skip blank or comment lines */
77 /* Look for a line starting with "Path" */
78 if (strncasecmp(cp, "Path", 4) == 0) {
80 if ((name = strtok(cp + 4, " \t")) == NULL ||
81 (path = strtok(NULL, " \t")) == NULL) {
84 if (strcasecmp(name, "askpass") == 0)
85 askpass_path = estrdup(path);
86 else if (strcasecmp(name, "noexec") == 0)
87 noexec_path = estrdup(path);
91 /* Look for a line starting with "Plugin" */
92 if (strncasecmp(cp, "Plugin", 6) == 0) {
94 if ((name = strtok(cp + 6, " \t")) == NULL ||
95 (path = strtok(NULL, " \t")) == NULL) {
98 info = emalloc(sizeof(*info));
99 info->symbol_name = estrdup(name);
100 info->path = estrdup(path);
103 tq_append(&pil, info);
110 if (tq_empty(&pil)) {
111 /* Default policy plugin */
112 info = emalloc(sizeof(*info));
113 info->symbol_name = "sudoers_policy";
114 info->path = SUDOERS_PLUGIN;
117 tq_append(&pil, info);
119 /* Default I/O plugin */
120 info = emalloc(sizeof(*info));
121 info->symbol_name = "sudoers_io";
122 info->path = SUDOERS_PLUGIN;
125 tq_append(&pil, info);
132 * Load the plugins listed in conf_file.
135 sudo_load_plugins(const char *conf_file,
136 struct plugin_container *policy_plugin,
137 struct plugin_container_list *io_plugins)
139 struct generic_plugin *plugin;
140 struct plugin_container *container;
141 struct plugin_info *info;
142 struct plugin_info_list *plugin_list;
148 /* Parse sudo.conf */
149 plugin_list = sudo_read_conf(conf_file);
151 tq_foreach_fwd(plugin_list, info) {
152 if (info->path[0] == '/') {
153 if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) {
154 warningx("%s: %s", info->path, strerror(ENAMETOOLONG));
158 if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR,
159 info->path) >= sizeof(path)) {
160 warningx("%s%s: %s", _PATH_SUDO_PLUGIN_DIR, info->path,
161 strerror(ENAMETOOLONG));
165 if (stat(path, &sb) != 0) {
169 if (sb.st_uid != ROOT_UID) {
170 warningx("%s must be owned by uid %d", path, ROOT_UID);
173 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
174 warningx("%s must be only be writable by owner", path);
178 /* Open plugin and map in symbol */
179 handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL);
181 warningx("unable to dlopen %s: %s", path, dlerror());
184 plugin = dlsym(handle, info->symbol_name);
186 warningx("unable to find symbol %s in %s", info->symbol_name, path);
190 if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
191 warningx("%s: unknown policy type %d", path, plugin->type);
194 if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
195 warningx("%s: incompatible policy major version %d, expected %d",
196 path, SUDO_API_VERSION_GET_MAJOR(plugin->version),
197 SUDO_API_VERSION_MAJOR);
200 if (plugin->type == SUDO_POLICY_PLUGIN) {
201 if (policy_plugin->handle) {
202 warningx("only a single policy plugin may be loaded");
205 policy_plugin->handle = handle;
206 policy_plugin->name = info->symbol_name;
207 policy_plugin->u.generic = plugin;
208 } else if (plugin->type == SUDO_IO_PLUGIN) {
209 container = emalloc(sizeof(*container));
210 container->prev = container;
211 container->next = NULL;
212 container->handle = handle;
213 container->name = info->symbol_name;
214 container->u.generic = plugin;
215 tq_append(io_plugins, container);
218 if (policy_plugin->handle == NULL) {
219 warningx("%s: at least one policy plugin must be specified", conf_file);
222 if (policy_plugin->u.policy->check_policy == NULL) {
223 warningx("policy plugin %s does not include a check_policy method",
224 policy_plugin->name);