Annotation of embedaddon/sudo/src/load_plugins.c, revision 1.1.1.3
1.1 misho 1: /*
2: * Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
3: *
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.
7: *
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.
15: */
16:
17: #include <config.h>
18:
19: #include <sys/types.h>
20: #include <sys/param.h>
21: #include <sys/stat.h>
22: #include <stdio.h>
23: #ifdef STDC_HEADERS
24: # include <stdlib.h>
25: # include <stddef.h>
26: #else
27: # ifdef HAVE_STDLIB_H
28: # include <stdlib.h>
29: # endif
30: #endif /* STDC_HEADERS */
31: #ifdef HAVE_STRING_H
32: # include <string.h>
33: #endif /* HAVE_STRING_H */
34: #ifdef HAVE_STRINGS_H
35: # include <strings.h>
36: #endif /* HAVE_STRINGS_H */
37: #ifdef HAVE_UNISTD_H
38: # include <unistd.h>
39: #endif /* HAVE_UNISTD_H */
40: #ifdef HAVE_DLOPEN
41: # include <dlfcn.h>
42: #else
43: # include "compat/dlfcn.h"
44: #endif
45: #include <errno.h>
46:
47: #include "sudo.h"
48: #include "sudo_plugin.h"
49: #include "sudo_plugin_int.h"
1.1.1.2 misho 50: #include "sudo_conf.h"
51: #include "sudo_debug.h"
1.1 misho 52:
53: #ifndef RTLD_GLOBAL
54: # define RTLD_GLOBAL 0
55: #endif
56:
57: /*
1.1.1.3 ! misho 58: * Load the plugin specified by "info".
1.1 misho 59: */
1.1.1.3 ! misho 60: static bool
! 61: sudo_load_plugin(struct plugin_container *policy_plugin,
! 62: struct plugin_container_list *io_plugins, struct plugin_info *info)
1.1 misho 63: {
64: struct plugin_container *container;
1.1.1.3 ! misho 65: struct generic_plugin *plugin;
1.1 misho 66: struct stat sb;
67: void *handle;
68: char path[PATH_MAX];
1.1.1.2 misho 69: bool rval = false;
1.1.1.3 ! misho 70: debug_decl(sudo_load_plugin, SUDO_DEBUG_PLUGIN)
1.1 misho 71:
1.1.1.3 ! misho 72: if (info->path[0] == '/') {
! 73: if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) {
! 74: warningx(_("%s: %s"), info->path, strerror(ENAMETOOLONG));
1.1 misho 75: goto done;
76: }
1.1.1.3 ! misho 77: } else {
! 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));
1.1 misho 82: goto done;
83: }
1.1.1.3 ! misho 84: }
! 85: if (stat(path, &sb) != 0) {
! 86: warning("%s", path);
! 87: goto done;
! 88: }
! 89: if (sb.st_uid != ROOT_UID) {
! 90: warningx(_("%s must be owned by uid %d"), path, ROOT_UID);
! 91: goto done;
! 92: }
! 93: if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
! 94: warningx(_("%s must be only be writable by owner"), path);
! 95: goto done;
! 96: }
1.1 misho 97:
1.1.1.3 ! misho 98: /* Open plugin and map in symbol */
! 99: handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
! 100: if (!handle) {
! 101: warningx(_("unable to dlopen %s: %s"), path, dlerror());
! 102: goto done;
! 103: }
! 104: plugin = dlsym(handle, info->symbol_name);
! 105: if (!plugin) {
! 106: warningx(_("%s: unable to find symbol %s"), path,
! 107: info->symbol_name);
! 108: goto done;
! 109: }
1.1 misho 110:
1.1.1.3 ! misho 111: if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
! 112: warningx(_("%s: unknown policy type %d"), path, plugin->type);
! 113: goto done;
! 114: }
! 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);
! 119: goto done;
! 120: }
! 121: if (plugin->type == SUDO_POLICY_PLUGIN) {
! 122: if (policy_plugin->handle) {
! 123: warningx(_("%s: only a single policy plugin may be loaded"),
! 124: _PATH_SUDO_CONF);
! 125: goto done;
! 126: }
! 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);
! 140: }
! 141:
! 142: rval = true;
! 143: done:
! 144: debug_return_bool(rval);
! 145: }
! 146:
! 147: /*
! 148: * Load the plugins listed in sudo.conf.
! 149: */
! 150: bool
! 151: sudo_load_plugins(struct plugin_container *policy_plugin,
! 152: struct plugin_container_list *io_plugins)
! 153: {
! 154: struct plugin_container *container;
! 155: struct plugin_info_list *plugins;
! 156: struct plugin_info *info;
! 157: bool rval = false;
! 158: debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN)
! 159:
! 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);
! 164: if (!rval)
1.1 misho 165: goto done;
166: }
1.1.1.3 ! misho 167:
! 168: /*
! 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.
! 171: */
1.1 misho 172: if (policy_plugin->handle == NULL) {
1.1.1.3 ! misho 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; */
! 178: info->prev = info;
! 179: /* info->next = NULL; */
! 180: rval = sudo_load_plugin(policy_plugin, io_plugins, info);
! 181: efree(info);
! 182: if (!rval)
! 183: goto done;
! 184:
! 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; */
! 191: info->prev = info;
! 192: /* info->next = NULL; */
! 193: rval = sudo_load_plugin(policy_plugin, io_plugins, info);
! 194: efree(info);
! 195: if (!rval)
! 196: goto done;
! 197: }
1.1 misho 198: }
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);
1.1.1.3 ! misho 202: rval = false;
1.1 misho 203: goto done;
204: }
205:
1.1.1.2 misho 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);
1.1.1.3 ! misho 210: }
! 211: tq_foreach_fwd(io_plugins, container) {
! 212: if (container->u.io->version >= SUDO_API_MKVERSION(1, 2)) {
1.1.1.2 misho 213: if (container->u.io->register_hooks != NULL)
214: container->u.io->register_hooks(SUDO_HOOK_VERSION, register_hook);
215: }
216: }
217:
1.1 misho 218: done:
1.1.1.2 misho 219: debug_return_bool(rval);
1.1 misho 220: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>