update control to reflect move of primary repo to collab-maint
[debian/sudo] / src / load_plugins.c
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"
50 #include "sudo_conf.h"
51 #include "sudo_debug.h"
52
53 #ifndef RTLD_GLOBAL
54 # define RTLD_GLOBAL    0
55 #endif
56
57 /*
58  * Load the plugins listed in sudo.conf.
59  */
60 bool
61 sudo_load_plugins(struct plugin_container *policy_plugin,
62     struct plugin_container_list *io_plugins)
63 {
64     struct plugin_info_list *plugins;
65     struct generic_plugin *plugin;
66     struct plugin_container *container;
67     struct plugin_info *info;
68     struct stat sb;
69     void *handle;
70     char path[PATH_MAX];
71     bool rval = false;
72     debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN)
73
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));
80                 goto done;
81             }
82         } else {
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));
87                 goto done;
88             }
89         }
90         if (stat(path, &sb) != 0) {
91             warning("%s", path);
92             goto done;
93         }
94         if (sb.st_uid != ROOT_UID) {
95             warningx(_("%s must be owned by uid %d"), path, ROOT_UID);
96             goto done;
97         }
98         if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
99             warningx(_("%s must be only be writable by owner"), path);
100             goto done;
101         }
102
103         /* Open plugin and map in symbol */
104         handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
105         if (!handle) {
106             warningx(_("unable to dlopen %s: %s"), path, dlerror());
107             goto done;
108         }
109         plugin = dlsym(handle, info->symbol_name);
110         if (!plugin) {
111             warningx(_("%s: unable to find symbol %s"), path,
112                 info->symbol_name);
113             goto done;
114         }
115
116         if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
117             warningx(_("%s: unknown policy type %d"), path, plugin->type);
118             goto done;
119         }
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);
124             goto done;
125         }
126         if (plugin->type == SUDO_POLICY_PLUGIN) {
127             if (policy_plugin->handle) {
128                 warningx(_("%s: only a single policy plugin may be loaded"),
129                     _PATH_SUDO_CONF);
130                 goto done;
131             }
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);
145         }
146     }
147     if (policy_plugin->handle == NULL) {
148         warningx(_("%s: at least one policy plugin must be specified"),
149             _PATH_SUDO_CONF);
150         goto done;
151     }
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);
155         goto done;
156     }
157
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);
165         }
166     }
167
168     rval = true;
169
170 done:
171     debug_return_bool(rval);
172 }