1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2010-2020 Todd C. Miller <Todd.Miller@sudo.ws>
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 /*
20 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22 */
23
24 #include <config.h>
25
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <ctype.h>
32 #include <errno.h>
33
34 #include "sudoers.h"
35 #include "sudo_dso.h"
36
37 #if defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD)
38
39 static void *group_handle;
40 static struct sudoers_group_plugin *group_plugin;
41 const char *path_plugin_dir = _PATH_SUDO_PLUGIN_DIR;
42
43 /*
44 * Load the specified plugin and run its init function.
45 * Returns -1 if unable to open the plugin, else it returns
46 * the value from the plugin's init function.
47 */
48 int
group_plugin_load(char * plugin_info)49 group_plugin_load(char *plugin_info)
50 {
51 struct stat sb;
52 char *args, path[PATH_MAX];
53 char **argv = NULL;
54 int len, rc = -1;
55 debug_decl(group_plugin_load, SUDOERS_DEBUG_UTIL);
56
57 /*
58 * Fill in .so path and split out args (if any).
59 */
60 if ((args = strpbrk(plugin_info, " \t")) != NULL) {
61 len = snprintf(path, sizeof(path), "%s%.*s",
62 (*plugin_info != '/') ? path_plugin_dir : "",
63 (int)(args - plugin_info), plugin_info);
64 args++;
65 } else {
66 len = snprintf(path, sizeof(path), "%s%s",
67 (*plugin_info != '/') ? path_plugin_dir : "", plugin_info);
68 }
69 if (len < 0 || len >= ssizeof(path)) {
70 errno = ENAMETOOLONG;
71 sudo_warn("%s%s",
72 (*plugin_info != '/') ? path_plugin_dir : "", plugin_info);
73 goto done;
74 }
75
76 /* Check owner and mode of plugin path. */
77 if (stat(path, &sb) != 0) {
78 sudo_warn("%s", path);
79 goto done;
80 }
81 if (!sudo_conf_developer_mode()) {
82 if (sb.st_uid != ROOT_UID) {
83 sudo_warnx(U_("%s must be owned by uid %d"), path, ROOT_UID);
84 goto done;
85 }
86 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
87 sudo_warnx(U_("%s must only be writable by owner"), path);
88 goto done;
89 }
90 }
91
92 /* Open plugin and map in symbol. */
93 group_handle = sudo_dso_load(path, SUDO_DSO_LAZY|SUDO_DSO_GLOBAL);
94 if (!group_handle) {
95 const char *errstr = sudo_dso_strerror();
96 sudo_warnx(U_("unable to load %s: %s"), path,
97 errstr ? errstr : "unknown error");
98 goto done;
99 }
100 group_plugin = sudo_dso_findsym(group_handle, "group_plugin");
101 if (group_plugin == NULL) {
102 sudo_warnx(U_("unable to find symbol \"group_plugin\" in %s"), path);
103 goto done;
104 }
105
106 if (SUDO_API_VERSION_GET_MAJOR(group_plugin->version) != GROUP_API_VERSION_MAJOR) {
107 sudo_warnx(U_("%s: incompatible group plugin major version %d, expected %d"),
108 path, SUDO_API_VERSION_GET_MAJOR(group_plugin->version),
109 GROUP_API_VERSION_MAJOR);
110 goto done;
111 }
112
113 /*
114 * Split args into a vector if specified.
115 */
116 if (args != NULL) {
117 int ac = 0;
118 bool wasblank = true;
119 char *cp, *last;
120
121 for (cp = args; *cp != '\0'; cp++) {
122 if (isblank((unsigned char)*cp)) {
123 wasblank = true;
124 } else if (wasblank) {
125 wasblank = false;
126 ac++;
127 }
128 }
129 if (ac != 0) {
130 argv = reallocarray(NULL, ac + 1, sizeof(char *));
131 if (argv == NULL) {
132 sudo_warnx(U_("%s: %s"), __func__,
133 U_("unable to allocate memory"));
134 goto done;
135 }
136 ac = 0;
137 cp = strtok_r(args, " \t", &last);
138 while (cp != NULL) {
139 argv[ac++] = cp;
140 cp = strtok_r(NULL, " \t", &last);
141 }
142 argv[ac] = NULL;
143 }
144 }
145
146 rc = (group_plugin->init)(GROUP_API_VERSION, sudo_printf, argv);
147
148 done:
149 free(argv);
150
151 if (rc != true) {
152 if (group_handle != NULL) {
153 sudo_dso_unload(group_handle);
154 group_handle = NULL;
155 group_plugin = NULL;
156 }
157 }
158
159 debug_return_int(rc);
160 }
161
162 void
group_plugin_unload(void)163 group_plugin_unload(void)
164 {
165 debug_decl(group_plugin_unload, SUDOERS_DEBUG_UTIL);
166
167 if (group_plugin != NULL) {
168 (group_plugin->cleanup)();
169 group_plugin = NULL;
170 }
171 if (group_handle != NULL) {
172 sudo_dso_unload(group_handle);
173 group_handle = NULL;
174 }
175 debug_return;
176 }
177
178 int
group_plugin_query(const char * user,const char * group,const struct passwd * pwd)179 group_plugin_query(const char *user, const char *group,
180 const struct passwd *pwd)
181 {
182 debug_decl(group_plugin_query, SUDOERS_DEBUG_UTIL);
183
184 if (group_plugin == NULL)
185 debug_return_int(false);
186 debug_return_int((group_plugin->query)(user, group, pwd));
187 }
188
189 #else /* !HAVE_DLOPEN && !HAVE_SHL_LOAD */
190
191 /*
192 * No loadable shared object support.
193 */
194
195 int
group_plugin_load(char * plugin_info)196 group_plugin_load(char *plugin_info)
197 {
198 debug_decl(group_plugin_load, SUDOERS_DEBUG_UTIL);
199 debug_return_int(false);
200 }
201
202 void
group_plugin_unload(void)203 group_plugin_unload(void)
204 {
205 debug_decl(group_plugin_unload, SUDOERS_DEBUG_UTIL);
206 debug_return;
207 }
208
209 int
group_plugin_query(const char * user,const char * group,const struct passwd * pwd)210 group_plugin_query(const char *user, const char *group,
211 const struct passwd *pwd)
212 {
213 debug_decl(group_plugin_query, SUDOERS_DEBUG_UTIL);
214 debug_return_int(false);
215 }
216
217 #endif /* HAVE_DLOPEN || HAVE_SHL_LOAD */
218
219 /*
220 * Group plugin sudoers callback.
221 */
222 bool
cb_group_plugin(const union sudo_defs_val * sd_un)223 cb_group_plugin(const union sudo_defs_val *sd_un)
224 {
225 bool rc = true;
226 debug_decl(cb_group_plugin, SUDOERS_DEBUG_PLUGIN);
227
228 /* Unload any existing group plugin before loading a new one. */
229 group_plugin_unload();
230 if (sd_un->str != NULL)
231 rc = group_plugin_load(sd_un->str);
232 debug_return_bool(rc);
233 }
234