2 * Copyright (c) 2012-2016 Todd C. Miller <Todd.Miller@sudo.ws>
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.
18 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
19 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
24 #include <sys/types.h>
29 #endif /* HAVE_STRING_H */
32 #endif /* HAVE_STRINGS_H */
37 #include "sudo_plugin.h"
38 #include "sudo_plugin_int.h"
40 /* Singly linked hook list. */
41 struct sudo_hook_entry {
42 SLIST_ENTRY(sudo_hook_entry) entries;
44 sudo_hook_fn_t generic_fn;
45 sudo_hook_fn_setenv_t setenv_fn;
46 sudo_hook_fn_unsetenv_t unsetenv_fn;
47 sudo_hook_fn_getenv_t getenv_fn;
48 sudo_hook_fn_putenv_t putenv_fn;
52 SLIST_HEAD(sudo_hook_list, sudo_hook_entry);
54 /* Each hook type gets own hook list. */
55 static struct sudo_hook_list sudo_hook_setenv_list =
56 SLIST_HEAD_INITIALIZER(sudo_hook_setenv_list);
57 static struct sudo_hook_list sudo_hook_unsetenv_list =
58 SLIST_HEAD_INITIALIZER(sudo_hook_unsetenv_list);
59 static struct sudo_hook_list sudo_hook_getenv_list =
60 SLIST_HEAD_INITIALIZER(sudo_hook_getenv_list);
61 static struct sudo_hook_list sudo_hook_putenv_list =
62 SLIST_HEAD_INITIALIZER(sudo_hook_putenv_list);
64 /* NOTE: must not anything that might call setenv() */
66 process_hooks_setenv(const char *name, const char *value, int overwrite)
68 struct sudo_hook_entry *hook;
69 int rc = SUDO_HOOK_RET_NEXT;
71 /* First process the hooks. */
72 SLIST_FOREACH(hook, &sudo_hook_setenv_list, entries) {
73 rc = hook->u.setenv_fn(name, value, overwrite, hook->closure);
74 if (rc == SUDO_HOOK_RET_STOP || rc == SUDO_HOOK_RET_ERROR)
80 /* NOTE: must not anything that might call putenv() */
82 process_hooks_putenv(char *string)
84 struct sudo_hook_entry *hook;
85 int rc = SUDO_HOOK_RET_NEXT;
87 /* First process the hooks. */
88 SLIST_FOREACH(hook, &sudo_hook_putenv_list, entries) {
89 rc = hook->u.putenv_fn(string, hook->closure);
90 if (rc == SUDO_HOOK_RET_STOP || rc == SUDO_HOOK_RET_ERROR)
96 /* NOTE: must not anything that might call getenv() */
98 process_hooks_getenv(const char *name, char **value)
100 struct sudo_hook_entry *hook;
102 int rc = SUDO_HOOK_RET_NEXT;
104 /* First process the hooks. */
105 SLIST_FOREACH(hook, &sudo_hook_getenv_list, entries) {
106 rc = hook->u.getenv_fn(name, &val, hook->closure);
107 if (rc == SUDO_HOOK_RET_STOP || rc == SUDO_HOOK_RET_ERROR)
115 /* NOTE: must not anything that might call unsetenv() */
117 process_hooks_unsetenv(const char *name)
119 struct sudo_hook_entry *hook;
120 int rc = SUDO_HOOK_RET_NEXT;
122 /* First process the hooks. */
123 SLIST_FOREACH(hook, &sudo_hook_unsetenv_list, entries) {
124 rc = hook->u.unsetenv_fn(name, hook->closure);
125 if (rc == SUDO_HOOK_RET_STOP || rc == SUDO_HOOK_RET_ERROR)
131 /* Hook registration internals. */
133 register_hook_internal(struct sudo_hook_list *head,
134 int (*hook_fn)(), void *closure)
136 struct sudo_hook_entry *hook;
137 debug_decl(register_hook_internal, SUDO_DEBUG_HOOKS)
139 if ((hook = calloc(1, sizeof(*hook))) == NULL) {
140 sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
141 "unable to allocate memory");
142 debug_return_int(-1);
144 hook->u.generic_fn = hook_fn;
145 hook->closure = closure;
146 SLIST_INSERT_HEAD(head, hook, entries);
151 /* Register the specified hook. */
153 register_hook(struct sudo_hook *hook)
156 debug_decl(register_hook, SUDO_DEBUG_HOOKS)
158 if (SUDO_API_VERSION_GET_MAJOR(hook->hook_version) != SUDO_HOOK_VERSION_MAJOR) {
159 /* Major versions must match. */
163 switch (hook->hook_type) {
164 case SUDO_HOOK_GETENV:
165 ret = register_hook_internal(&sudo_hook_getenv_list,
166 hook->hook_fn, hook->closure);
168 case SUDO_HOOK_PUTENV:
169 ret = register_hook_internal(&sudo_hook_putenv_list,
170 hook->hook_fn, hook->closure);
172 case SUDO_HOOK_SETENV:
173 ret = register_hook_internal(&sudo_hook_setenv_list,
174 hook->hook_fn, hook->closure);
176 case SUDO_HOOK_UNSETENV:
177 ret = register_hook_internal(&sudo_hook_unsetenv_list,
178 hook->hook_fn, hook->closure);
181 /* XXX - use define for unknown value */
188 debug_return_int(ret);
191 /* Hook deregistration internals. */
193 deregister_hook_internal(struct sudo_hook_list *head,
194 int (*hook_fn)(), void *closure)
196 struct sudo_hook_entry *hook, *prev = NULL;
197 debug_decl(deregister_hook_internal, SUDO_DEBUG_HOOKS)
199 SLIST_FOREACH(hook, head, entries) {
200 if (hook->u.generic_fn == hook_fn && hook->closure == closure) {
201 /* Remove from list and free. */
203 SLIST_REMOVE_HEAD(head, entries);
205 SLIST_REMOVE_AFTER(prev, entries);
215 /* Deregister the specified hook. */
217 deregister_hook(struct sudo_hook *hook)
220 debug_decl(deregister_hook, SUDO_DEBUG_HOOKS)
222 if (SUDO_API_VERSION_GET_MAJOR(hook->hook_version) != SUDO_HOOK_VERSION_MAJOR) {
223 /* Major versions must match. */
226 switch (hook->hook_type) {
227 case SUDO_HOOK_GETENV:
228 deregister_hook_internal(&sudo_hook_getenv_list, hook->hook_fn,
231 case SUDO_HOOK_PUTENV:
232 deregister_hook_internal(&sudo_hook_putenv_list, hook->hook_fn,
235 case SUDO_HOOK_SETENV:
236 deregister_hook_internal(&sudo_hook_setenv_list, hook->hook_fn,
239 case SUDO_HOOK_UNSETENV:
240 deregister_hook_internal(&sudo_hook_unsetenv_list, hook->hook_fn,
244 /* XXX - use define for unknown value */
250 debug_return_int(ret);