Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/src/load_plugins.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2009-2018 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <sys/stat.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <errno.h>
26
27
#include <sudo.h>
28
#include <sudo_plugin.h>
29
#include <sudo_plugin_int.h>
30
#include <sudo_dso.h>
31
32
#ifdef ENABLE_SUDO_PLUGIN_API
33
static bool
34
sudo_qualify_plugin(struct plugin_info *info, char *fullpath, size_t pathsize)
35
{
36
const char *plugin_dir = sudo_conf_plugin_dir_path();
37
int len;
38
debug_decl(sudo_qualify_plugin, SUDO_DEBUG_PLUGIN);
39
40
if (info->path[0] == '/') {
41
if (strlcpy(fullpath, info->path, pathsize) >= pathsize) {
42
errno = ENAMETOOLONG;
43
goto bad;
44
}
45
} else {
46
#ifdef STATIC_SUDOERS_PLUGIN
47
/* Check static symbols. */
48
if (strcmp(info->path, _PATH_SUDOERS_PLUGIN) == 0) {
49
if (strlcpy(fullpath, info->path, pathsize) >= pathsize) {
50
errno = ENAMETOOLONG;
51
goto bad;
52
}
53
/* Plugin is static, do not fully-qualify. */
54
debug_return_bool(true);
55
}
56
#endif /* STATIC_SUDOERS_PLUGIN */
57
58
if (plugin_dir == NULL) {
59
errno = ENOENT;
60
goto bad;
61
}
62
len = snprintf(fullpath, pathsize, "%s%s", plugin_dir, info->path);
63
if (len < 0 || (size_t)len >= pathsize) {
64
errno = ENAMETOOLONG;
65
goto bad;
66
}
67
}
68
debug_return_bool(true);
69
bad:
70
sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
71
_PATH_SUDO_CONF, info->lineno, info->symbol_name);
72
if (info->path[0] != '/' && plugin_dir != NULL)
73
sudo_warn("%s%s", plugin_dir, info->path);
74
else
75
sudo_warn("%s", info->path);
76
debug_return_bool(false);
77
}
78
#else
79
static bool
80
sudo_qualify_plugin(struct plugin_info *info, char *fullpath, size_t pathsize)
81
{
82
debug_decl(sudo_qualify_plugin, SUDO_DEBUG_PLUGIN);
83
(void)strlcpy(fullpath, info->path, pathsize);
84
debug_return_bool(true);
85
}
86
#endif /* ENABLE_SUDO_PLUGIN_API */
87
88
static bool
89
fill_container(struct plugin_container *container, void *handle,
90
const char *path, struct generic_plugin *plugin, struct plugin_info *info)
91
{
92
debug_decl(fill_container, SUDO_DEBUG_PLUGIN);
93
94
if ((container->path = strdup(path)) == NULL) {
95
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
96
debug_return_bool(false);
97
}
98
container->handle = handle;
99
container->name = info->symbol_name;
100
container->options = info->options;
101
container->debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
102
container->u.generic = plugin;
103
container->debug_files = sudo_conf_debug_files(path);
104
105
/* Zero out info strings that the container now owns. */
106
info->symbol_name = NULL;
107
info->options = NULL;
108
109
debug_return_bool(true);
110
}
111
112
static struct plugin_container *
113
new_container(void *handle, const char *path, struct generic_plugin *plugin,
114
struct plugin_info *info)
115
{
116
struct plugin_container *container;
117
debug_decl(new_container, SUDO_DEBUG_PLUGIN);
118
119
if ((container = calloc(1, sizeof(*container))) == NULL) {
120
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
121
goto bad;
122
}
123
if (!fill_container(container, handle, path, plugin, info))
124
goto bad;
125
126
debug_return_ptr(container);
127
bad:
128
free(container);
129
debug_return_ptr(NULL);
130
}
131
132
static bool
133
plugin_exists(struct plugin_container_list *plugins, const char *symbol_name)
134
{
135
struct plugin_container *container;
136
debug_decl(plugin_exists, SUDO_DEBUG_PLUGIN);
137
138
TAILQ_FOREACH(container, plugins, entries) {
139
if (strcmp(container->name, symbol_name) == 0)
140
debug_return_bool(true);
141
}
142
debug_return_bool(false);
143
}
144
145
typedef struct generic_plugin * (plugin_clone_func)(void);
146
147
static struct generic_plugin *
148
sudo_plugin_try_to_clone(void *so_handle, const char *symbol_name)
149
{
150
debug_decl(sudo_plugin_try_to_clone, SUDO_DEBUG_PLUGIN);
151
struct generic_plugin *plugin = NULL;
152
plugin_clone_func *clone_func;
153
char *clone_func_name = NULL;
154
155
if (asprintf(&clone_func_name, "%s_clone", symbol_name) < 0) {
156
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
157
goto cleanup;
158
}
159
160
clone_func = (plugin_clone_func *)sudo_dso_findsym(so_handle,
161
clone_func_name);
162
if (clone_func) {
163
plugin = (*clone_func)();
164
}
165
166
cleanup:
167
free(clone_func_name);
168
debug_return_ptr(plugin);
169
}
170
171
static bool
172
sudo_insert_plugin(struct plugin_container_list *plugin_list, void *handle,
173
const char *path, struct generic_plugin *plugin, struct plugin_info *info)
174
{
175
struct plugin_container *container;
176
debug_decl(sudo_insert_plugin, SUDO_DEBUG_PLUGIN);
177
178
if (plugin_exists(plugin_list, info->symbol_name)) {
179
plugin = sudo_plugin_try_to_clone(handle, info->symbol_name);
180
if (plugin == NULL) {
181
sudo_warnx(U_("ignoring duplicate plugin \"%s\" in %s, line %d"),
182
info->symbol_name, _PATH_SUDO_CONF, info->lineno);
183
sudo_dso_unload(handle);
184
goto done;
185
}
186
}
187
188
if ((container = new_container(handle, path, plugin, info)) == NULL)
189
debug_return_bool(false);
190
TAILQ_INSERT_TAIL(plugin_list, container, entries);
191
192
done:
193
debug_return_bool(true);
194
}
195
196
/*
197
* Load the plugin specified by "info".
198
*/
199
static bool
200
sudo_load_plugin(struct plugin_info *info, bool quiet)
201
{
202
struct generic_plugin *plugin;
203
char path[PATH_MAX];
204
void *handle = NULL;
205
bool ret = false;
206
debug_decl(sudo_load_plugin, SUDO_DEBUG_PLUGIN);
207
208
/* Fill in path from info and plugin dir. */
209
if (!sudo_qualify_plugin(info, path, sizeof(path)))
210
goto done;
211
212
/* Open plugin and map in symbol */
213
handle = sudo_dso_load(path, SUDO_DSO_LAZY|SUDO_DSO_GLOBAL);
214
if (!handle) {
215
if (!quiet) {
216
const char *errstr = sudo_dso_strerror();
217
sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
218
_PATH_SUDO_CONF, info->lineno, info->symbol_name);
219
sudo_warnx(U_("unable to load %s: %s"), path,
220
errstr ? errstr : "unknown error");
221
}
222
goto done;
223
}
224
plugin = sudo_dso_findsym(handle, info->symbol_name);
225
if (!plugin) {
226
if (!quiet) {
227
sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
228
_PATH_SUDO_CONF, info->lineno, info->symbol_name);
229
sudo_warnx(U_("unable to find symbol \"%s\" in %s"),
230
info->symbol_name, path);
231
}
232
goto done;
233
}
234
235
if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
236
if (!quiet) {
237
sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
238
_PATH_SUDO_CONF, info->lineno, info->symbol_name);
239
sudo_warnx(U_("incompatible plugin major version %d (expected %d) found in %s"),
240
SUDO_API_VERSION_GET_MAJOR(plugin->version),
241
SUDO_API_VERSION_MAJOR, path);
242
}
243
goto done;
244
}
245
246
switch (plugin->type) {
247
case SUDO_POLICY_PLUGIN:
248
if (policy_plugin.handle != NULL) {
249
/* Ignore duplicate entries. */
250
if (strcmp(policy_plugin.name, info->symbol_name) == 0) {
251
if (!quiet) {
252
sudo_warnx(U_("ignoring duplicate plugin \"%s\" in %s, line %d"),
253
info->symbol_name, _PATH_SUDO_CONF, info->lineno);
254
}
255
} else {
256
if (!quiet) {
257
sudo_warnx(U_("ignoring policy plugin \"%s\" in %s, line %d"),
258
info->symbol_name, _PATH_SUDO_CONF, info->lineno);
259
sudo_warnx("%s",
260
U_("only a single policy plugin may be specified"));
261
}
262
goto done;
263
}
264
ret = true;
265
goto done;
266
}
267
if (!fill_container(&policy_plugin, handle, path, plugin, info))
268
goto done;
269
break;
270
case SUDO_IO_PLUGIN:
271
if (!sudo_insert_plugin(&io_plugins, handle, path, plugin, info))
272
goto done;
273
break;
274
case SUDO_AUDIT_PLUGIN:
275
if (!sudo_insert_plugin(&audit_plugins, handle, path, plugin, info))
276
goto done;
277
break;
278
case SUDO_APPROVAL_PLUGIN:
279
if (!sudo_insert_plugin(&approval_plugins, handle, path, plugin, info))
280
goto done;
281
break;
282
default:
283
if (!quiet) {
284
sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
285
_PATH_SUDO_CONF, info->lineno, info->symbol_name);
286
sudo_warnx(U_("unknown plugin type %d found in %s"), plugin->type, path);
287
}
288
goto done;
289
}
290
291
/* Handle is either in use or has been closed. */
292
handle = NULL;
293
294
ret = true;
295
296
done:
297
if (handle != NULL)
298
sudo_dso_unload(handle);
299
debug_return_bool(ret);
300
}
301
302
static void
303
free_plugin_info(struct plugin_info *info)
304
{
305
free(info->path);
306
free(info->symbol_name);
307
if (info->options != NULL) {
308
int i = 0;
309
while (info->options[i] != NULL)
310
free(info->options[i++]);
311
free(info->options);
312
}
313
free(info);
314
}
315
316
static void
317
sudo_register_hooks(void)
318
{
319
struct plugin_container *container;
320
debug_decl(sudo_register_hooks, SUDO_DEBUG_PLUGIN);
321
322
if (policy_plugin.u.policy->version >= SUDO_API_MKVERSION(1, 2)) {
323
if (policy_plugin.u.policy->register_hooks != NULL) {
324
sudo_debug_set_active_instance(policy_plugin.debug_instance);
325
policy_plugin.u.policy->register_hooks(SUDO_HOOK_VERSION,
326
register_hook);
327
sudo_debug_set_active_instance(sudo_debug_instance);
328
}
329
}
330
331
TAILQ_FOREACH(container, &io_plugins, entries) {
332
if (container->u.io->version >= SUDO_API_MKVERSION(1, 2)) {
333
if (container->u.io->register_hooks != NULL) {
334
sudo_debug_set_active_instance(container->debug_instance);
335
container->u.io->register_hooks(SUDO_HOOK_VERSION,
336
register_hook);
337
sudo_debug_set_active_instance(sudo_debug_instance);
338
}
339
}
340
}
341
342
TAILQ_FOREACH(container, &audit_plugins, entries) {
343
if (container->u.audit->register_hooks != NULL) {
344
sudo_debug_set_active_instance(container->debug_instance);
345
container->u.audit->register_hooks(SUDO_HOOK_VERSION,
346
register_hook);
347
sudo_debug_set_active_instance(sudo_debug_instance);
348
}
349
}
350
351
debug_return;
352
}
353
354
static void
355
sudo_init_event_alloc(void)
356
{
357
struct plugin_container *container;
358
debug_decl(sudo_init_event_alloc, SUDO_DEBUG_PLUGIN);
359
360
if (policy_plugin.u.policy->version >= SUDO_API_MKVERSION(1, 15))
361
policy_plugin.u.policy->event_alloc = sudo_plugin_event_alloc;
362
363
TAILQ_FOREACH(container, &io_plugins, entries) {
364
if (container->u.io->version >= SUDO_API_MKVERSION(1, 15))
365
container->u.io->event_alloc = sudo_plugin_event_alloc;
366
}
367
TAILQ_FOREACH(container, &audit_plugins, entries) {
368
if (container->u.audit->version >= SUDO_API_MKVERSION(1, 17))
369
container->u.audit->event_alloc = sudo_plugin_event_alloc;
370
}
371
372
debug_return;
373
}
374
375
/*
376
* Load the specified symbol from the sudoers plugin.
377
* Used to provide a default plugin when none are specified in sudo.conf.
378
*/
379
static bool
380
sudo_load_sudoers_plugin(const char *symbol_name, bool optional)
381
{
382
struct plugin_info *info;
383
bool ret = false;
384
debug_decl(sudo_load_sudoers_plugin, SUDO_DEBUG_PLUGIN);
385
386
/* Default policy plugin */
387
info = calloc(1, sizeof(*info));
388
if (info == NULL) {
389
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
390
goto done;
391
}
392
info->symbol_name = strdup(symbol_name);
393
info->path = strdup(_PATH_SUDOERS_PLUGIN);
394
if (info->symbol_name == NULL || info->path == NULL) {
395
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
396
free_plugin_info(info);
397
goto done;
398
}
399
/* info->options = NULL; */
400
ret = sudo_load_plugin(info, optional);
401
free_plugin_info(info);
402
403
done:
404
debug_return_bool(ret);
405
}
406
407
/*
408
* Load the plugins listed in sudo.conf.
409
*/
410
bool
411
sudo_load_plugins(void)
412
{
413
struct plugin_info_list *plugins;
414
struct plugin_info *info, *next;
415
bool ret = false;
416
debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN);
417
418
/* Walk the plugin list from sudo.conf, if any and free it. */
419
plugins = sudo_conf_plugins();
420
TAILQ_FOREACH_SAFE(info, plugins, entries, next) {
421
ret = sudo_load_plugin(info, false);
422
if (!ret)
423
goto done;
424
free_plugin_info(info);
425
}
426
TAILQ_INIT(plugins);
427
428
/*
429
* If no policy plugin, fall back to the default (sudoers).
430
* If there is also no I/O log plugin, use sudoers for that too.
431
*/
432
if (policy_plugin.handle == NULL) {
433
/* Default policy plugin */
434
ret = sudo_load_sudoers_plugin("sudoers_policy", false);
435
if (!ret)
436
goto done;
437
438
/* Default audit plugin, optional (sudoers < 1.9.1 lack this) */
439
(void)sudo_load_sudoers_plugin("sudoers_audit", true);
440
441
/* Default I/O plugin */
442
if (TAILQ_EMPTY(&io_plugins)) {
443
ret = sudo_load_sudoers_plugin("sudoers_io", false);
444
if (!ret)
445
goto done;
446
}
447
} else if (strcmp(policy_plugin.name, "sudoers_policy") == 0) {
448
/*
449
* If policy plugin is sudoers_policy but there is no sudoers_audit
450
* loaded, load it too, if possible.
451
*/
452
if (!plugin_exists(&audit_plugins, "sudoers_audit")) {
453
if (sudo_load_sudoers_plugin("sudoers_audit", true)) {
454
/*
455
* Move the plugin options from sudoers_policy to sudoers_audit
456
* since the audit module is now what actually opens sudoers.
457
*/
458
if (policy_plugin.options != NULL) {
459
TAILQ_LAST(&audit_plugins, plugin_container_list)->options =
460
policy_plugin.options;
461
policy_plugin.options = NULL;
462
}
463
}
464
}
465
}
466
467
/* TODO: check all plugins for open function too */
468
if (policy_plugin.u.policy->check_policy == NULL) {
469
sudo_warnx(U_("policy plugin %s does not include a check_policy method"),
470
policy_plugin.name);
471
ret = false;
472
goto done;
473
}
474
475
/* Set event_alloc() in plugins. */
476
sudo_init_event_alloc();
477
478
/* Install hooks (XXX - later, after open). */
479
sudo_register_hooks();
480
481
done:
482
debug_return_bool(ret);
483
}
484
485