Merge commit 'upstream/1.8.1p2'
[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
51 #ifndef RTLD_LOCAL
52 # define RTLD_LOCAL     0
53 #endif
54
55 const char *noexec_path = _PATH_SUDO_NOEXEC;
56
57 /*
58  * Read in /etc/sudo.conf
59  * Returns a list of plugins.
60  */
61 static struct plugin_info_list *
62 sudo_read_conf(const char *conf_file)
63 {
64     FILE *fp;
65     char *cp, *name, *path;
66     struct plugin_info *info;
67     static struct plugin_info_list pil; /* XXX */
68
69     if ((fp = fopen(conf_file, "r")) == NULL)
70         goto done;
71
72     while ((cp = sudo_parseln(fp)) != NULL) {
73         /* Skip blank or comment lines */
74         if (*cp == '\0')
75             continue;
76
77         /* Look for a line starting with "Path" */
78         if (strncasecmp(cp, "Path", 4) == 0) {
79             /* Parse line */
80             if ((name = strtok(cp + 4, " \t")) == NULL ||
81                 (path = strtok(NULL, " \t")) == NULL) {
82                 continue;
83             }
84             if (strcasecmp(name, "askpass") == 0)
85                 askpass_path = estrdup(path);
86             else if (strcasecmp(name, "noexec") == 0)
87                 noexec_path = estrdup(path);
88             continue;
89         }
90
91         /* Look for a line starting with "Plugin" */
92         if (strncasecmp(cp, "Plugin", 6) == 0) {
93             /* Parse line */
94             if ((name = strtok(cp + 6, " \t")) == NULL ||
95                 (path = strtok(NULL, " \t")) == NULL) {
96                 continue;
97             }
98             info = emalloc(sizeof(*info));
99             info->symbol_name = estrdup(name);
100             info->path = estrdup(path);
101             info->prev = info;
102             info->next = NULL;
103             tq_append(&pil, info);
104             continue;
105         }
106     }
107     fclose(fp);
108
109 done:
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;
115         info->prev = info;
116         info->next = NULL;
117         tq_append(&pil, info);
118
119         /* Default I/O plugin */
120         info = emalloc(sizeof(*info));
121         info->symbol_name = "sudoers_io";
122         info->path = SUDOERS_PLUGIN;
123         info->prev = info;
124         info->next = NULL;
125         tq_append(&pil, info);
126     }
127
128     return &pil;
129 }
130
131 /*
132  * Load the plugins listed in conf_file.
133  */
134 int
135 sudo_load_plugins(const char *conf_file,
136     struct plugin_container *policy_plugin,
137     struct plugin_container_list *io_plugins)
138 {
139     struct generic_plugin *plugin;
140     struct plugin_container *container;
141     struct plugin_info *info;
142     struct plugin_info_list *plugin_list;
143     struct stat sb;
144     void *handle;
145     char path[PATH_MAX];
146     int rval = FALSE;
147
148     /* Parse sudo.conf */
149     plugin_list = sudo_read_conf(conf_file);
150
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));
155                 goto done;
156             }
157         } else {
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));
162                 goto done;
163             }
164         }
165         if (stat(path, &sb) != 0) {
166             warning("%s", path);
167             goto done;
168         }
169         if (sb.st_uid != ROOT_UID) {
170             warningx("%s must be owned by uid %d", path, ROOT_UID);
171             goto done;
172         }
173         if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
174             warningx("%s must be only be writable by owner", path);
175             goto done;
176         }
177
178         /* Open plugin and map in symbol */
179         handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL);
180         if (!handle) {
181             warningx("unable to dlopen %s: %s", path, dlerror());
182             goto done;
183         }
184         plugin = dlsym(handle, info->symbol_name);
185         if (!plugin) {
186             warningx("unable to find symbol %s in %s", info->symbol_name, path);
187             goto done;
188         }
189
190         if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
191             warningx("%s: unknown policy type %d", path, plugin->type);
192             goto done;
193         }
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);
198             goto done;
199         }
200         if (plugin->type == SUDO_POLICY_PLUGIN) {
201             if (policy_plugin->handle) {
202                 warningx("only a single policy plugin may be loaded");
203                 goto done;
204             }
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);
216         }
217     }
218     if (policy_plugin->handle == NULL) {
219         warningx("%s: at least one policy plugin must be specified", conf_file);
220         goto done;
221     }
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);
225         goto done;
226     }
227
228     rval = TRUE;
229
230 done:
231     return rval;
232 }