/src/sudo/lib/util/sudo_conf.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * SPDX-License-Identifier: ISC  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2009-2023 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  |  | #include <config.h>  | 
20  |  |  | 
21  |  | #include <sys/stat.h>  | 
22  |  | #include <stdio.h>  | 
23  |  | #include <stdlib.h>  | 
24  |  | #ifdef HAVE_STDBOOL_H  | 
25  |  | # include <stdbool.h>  | 
26  |  | #else  | 
27  |  | # include <compat/stdbool.h>  | 
28  |  | #endif  | 
29  |  | #include <string.h>  | 
30  |  | #ifdef HAVE_STRINGS_H  | 
31  |  | # include <strings.h>  | 
32  |  | #endif /* HAVE_STRINGS_H */  | 
33  |  | #include <unistd.h>  | 
34  |  | #include <ctype.h>  | 
35  |  | #include <errno.h>  | 
36  |  | #include <fcntl.h>  | 
37  |  | #include <limits.h>  | 
38  |  | #ifdef HAVE_DLOPEN  | 
39  |  | # include <dlfcn.h>  | 
40  |  | #endif  | 
41  |  |  | 
42  |  | #define SUDO_ERROR_WRAP 0  | 
43  |  |  | 
44  |  | #include <sudo_compat.h>  | 
45  |  | #include <sudo_conf.h>  | 
46  |  | #include <sudo_debug.h>  | 
47  |  | #include <sudo_fatal.h>  | 
48  |  | #include <sudo_gettext.h>  | 
49  |  | #include <sudo_plugin.h>  | 
50  |  | #include <sudo_util.h>  | 
51  |  | #include <pathnames.h>  | 
52  |  |  | 
53  |  | #ifndef _PATH_SUDO_INTERCEPT  | 
54  | 611  | # define _PATH_SUDO_INTERCEPT NULL  | 
55  |  | #endif  | 
56  |  | #ifndef _PATH_SUDO_NOEXEC  | 
57  | 611  | # define _PATH_SUDO_NOEXEC NULL  | 
58  |  | #endif  | 
59  |  |  | 
60  |  | struct sudo_conf_table { | 
61  |  |     const char *name;  | 
62  |  |     unsigned int namelen;  | 
63  |  |     int (*parser)(const char *entry, const char *conf_file, unsigned int lineno);  | 
64  |  | };  | 
65  |  |  | 
66  |  | struct sudo_conf_path_table { | 
67  |  |     const char *pname;  | 
68  |  |     unsigned int pnamelen;  | 
69  |  |     bool dynamic;  | 
70  |  |     const char *pval;  | 
71  |  | };  | 
72  |  |  | 
73  |  | struct sudo_conf_settings { | 
74  |  |     bool updated;  | 
75  |  |     bool disable_coredump;  | 
76  |  |     bool probe_interfaces;  | 
77  |  |     int group_source;  | 
78  |  |     int max_groups;  | 
79  |  | };  | 
80  |  |  | 
81  |  | static int parse_debug(const char *entry, const char *conf_file, unsigned int lineno);  | 
82  |  | static int parse_path(const char *entry, const char *conf_file, unsigned int lineno);  | 
83  |  | static int parse_plugin(const char *entry, const char *conf_file, unsigned int lineno);  | 
84  |  | static int parse_variable(const char *entry, const char *conf_file, unsigned int lineno);  | 
85  |  |  | 
86  |  | static struct sudo_conf_table sudo_conf_table[] = { | 
87  |  |     { "Debug", sizeof("Debug") - 1, parse_debug }, | 
88  |  |     { "Path", sizeof("Path") - 1, parse_path }, | 
89  |  |     { "Plugin", sizeof("Plugin") - 1, parse_plugin }, | 
90  |  |     { "Set", sizeof("Set") - 1, parse_variable }, | 
91  |  |     { NULL } | 
92  |  | };  | 
93  |  |  | 
94  |  | static int set_var_disable_coredump(const char *entry, const char *conf_file, unsigned int);  | 
95  |  | static int set_var_group_source(const char *entry, const char *conf_file, unsigned int);  | 
96  |  | static int set_var_max_groups(const char *entry, const char *conf_file, unsigned int);  | 
97  |  | static int set_var_probe_interfaces(const char *entry, const char *conf_file, unsigned int);  | 
98  |  |  | 
99  |  | static struct sudo_conf_table sudo_conf_var_table[] = { | 
100  |  |     { "disable_coredump", sizeof("disable_coredump") - 1, set_var_disable_coredump }, | 
101  |  |     { "group_source", sizeof("group_source") - 1, set_var_group_source }, | 
102  |  |     { "max_groups", sizeof("max_groups") - 1, set_var_max_groups }, | 
103  |  |     { "probe_interfaces", sizeof("probe_interfaces") - 1, set_var_probe_interfaces }, | 
104  |  |     { NULL } | 
105  |  | };  | 
106  |  |  | 
107  |  | /* Indexes into path_table[] below (order is important). */  | 
108  | 1.94k  | #define SUDO_CONF_PATH_ASKPASS    0  | 
109  | 1.94k  | #define SUDO_CONF_PATH_SESH   1  | 
110  | 1.94k  | #define SUDO_CONF_PATH_INTERCEPT  2  | 
111  | 1.94k  | #define SUDO_CONF_PATH_NOEXEC   3  | 
112  | 1.94k  | #define SUDO_CONF_PATH_PLUGIN_DIR 4  | 
113  | 0  | #define SUDO_CONF_PATH_DEVSEARCH  5  | 
114  |  |  | 
115  | 611  | #define SUDO_CONF_PATH_INITIALIZER  {       \ | 
116  | 611  |     { "askpass", sizeof("askpass") - 1, false, _PATH_SUDO_ASKPASS }, \ | 
117  | 611  |     { "sesh", sizeof("sesh") - 1, false, _PATH_SUDO_SESH },   \ | 
118  | 611  |     { "intercept", sizeof("intercept") - 1, false, _PATH_SUDO_INTERCEPT }, \ | 
119  | 611  |     { "noexec", sizeof("noexec") - 1, false, _PATH_SUDO_NOEXEC }, \ | 
120  | 611  |     { "plugin_dir", sizeof("plugin_dir") - 1, false, _PATH_SUDO_PLUGIN_DIR }, \ | 
121  | 611  |     { "devsearch", sizeof("devsearch") - 1, false, _PATH_SUDO_DEVSEARCH }, \ | 
122  | 611  |     { NULL } \ | 
123  | 611  | }  | 
124  |  |  | 
125  |  | /*  | 
126  |  |  * getgroups(2) on macOS is flakey with respect to non-local groups.   | 
127  |  |  * Even with _DARWIN_UNLIMITED_GETGROUPS set we may not get all groups./  | 
128  |  |  * See bug #946 for details.  | 
129  |  |  */  | 
130  |  | #ifdef __APPLE__  | 
131  |  | # define GROUP_SOURCE_DEFAULT GROUP_SOURCE_DYNAMIC  | 
132  |  | #else  | 
133  | 611  | # define GROUP_SOURCE_DEFAULT GROUP_SOURCE_ADAPTIVE  | 
134  |  | #endif  | 
135  |  |  | 
136  | 611  | #define SUDO_CONF_SETTINGS_INITIALIZER  {       \ | 
137  | 611  |     false,      /* updated */       \  | 
138  | 611  |     true,     /* disable_coredump */      \  | 
139  | 611  |     true,     /* probe_interfaces */      \  | 
140  | 611  |     GROUP_SOURCE_DEFAULT, /* group_source */      \  | 
141  | 611  |     -1        /* max_groups */      \  | 
142  | 611  | }  | 
143  |  |  | 
144  |  | static struct sudo_conf_data { | 
145  |  |     struct sudo_conf_settings settings;  | 
146  |  |     struct sudo_conf_debug_list debugging;  | 
147  |  |     struct plugin_info_list plugins;  | 
148  |  |     struct sudo_conf_path_table path_table[7];  | 
149  |  | } sudo_conf_data = { | 
150  |  |     SUDO_CONF_SETTINGS_INITIALIZER,  | 
151  |  |     TAILQ_HEAD_INITIALIZER(sudo_conf_data.debugging),  | 
152  |  |     TAILQ_HEAD_INITIALIZER(sudo_conf_data.plugins),  | 
153  |  |     SUDO_CONF_PATH_INITIALIZER  | 
154  |  | };  | 
155  |  |  | 
156  |  | /*  | 
157  |  |  * "Set variable_name value"  | 
158  |  |  */  | 
159  |  | static int  | 
160  |  | parse_variable(const char *entry, const char *conf_file, unsigned int lineno)  | 
161  | 7.84k  | { | 
162  | 7.84k  |     struct sudo_conf_table *var;  | 
163  | 7.84k  |     int ret;  | 
164  | 7.84k  |     debug_decl(parse_variable, SUDO_DEBUG_UTIL);  | 
165  |  |  | 
166  | 22.1k  |     for (var = sudo_conf_var_table; var->name != NULL; var++) { | 
167  | 21.4k  |   if (strncmp(entry, var->name, var->namelen) == 0 &&  | 
168  | 21.4k  |       isblank((unsigned char)entry[var->namelen])) { | 
169  | 7.20k  |       entry += var->namelen + 1;  | 
170  | 7.20k  |       while (isblank((unsigned char)*entry))  | 
171  | 333  |     entry++;  | 
172  | 7.20k  |       ret = var->parser(entry, conf_file, lineno);  | 
173  | 7.20k  |       sudo_debug_printf(ret ? SUDO_DEBUG_INFO : SUDO_DEBUG_ERROR,  | 
174  | 7.20k  |     "%s: %s:%u: Set %s %s", __func__, conf_file,  | 
175  | 7.20k  |     lineno, var->name, entry);  | 
176  | 7.20k  |       debug_return_int(ret);  | 
177  | 7.20k  |   }  | 
178  | 21.4k  |     }  | 
179  | 7.84k  |     sudo_debug_printf(SUDO_DEBUG_WARN, "%s: %s:%u: unknown setting %s",  | 
180  | 641  |   __func__, conf_file, lineno, entry);  | 
181  | 641  |     debug_return_int(false);  | 
182  | 641  | }  | 
183  |  |  | 
184  |  | /*  | 
185  |  |  * "Path name /path/to/file"  | 
186  |  |  * If path is missing it will be set to the NULL pointer.  | 
187  |  |  */  | 
188  |  | static int  | 
189  |  | parse_path(const char *entry, const char *conf_file, unsigned int lineno)  | 
190  | 3.12k  | { | 
191  | 3.12k  |     const char *entry_end = entry + strlen(entry);  | 
192  | 3.12k  |     const char *ep, *name, *path;  | 
193  | 3.12k  |     struct sudo_conf_path_table *cur;  | 
194  | 3.12k  |     size_t namelen;  | 
195  | 3.12k  |     debug_decl(parse_path, SUDO_DEBUG_UTIL);  | 
196  |  |  | 
197  |  |     /* Parse name. */  | 
198  | 3.12k  |     name = sudo_strsplit(entry, entry_end, " \t", &ep);  | 
199  | 3.12k  |     if (name == NULL)  | 
200  | 1.91k  |   goto bad;  | 
201  | 1.20k  |     namelen = (size_t)(ep - name);  | 
202  |  |  | 
203  |  |     /* Parse path (if present). */  | 
204  | 1.20k  |     path = sudo_strsplit(NULL, entry_end, " \t", &ep);  | 
205  |  |  | 
206  |  |     /* Match supported paths, ignoring unknown paths. */  | 
207  | 5.42k  |     for (cur = sudo_conf_data.path_table; cur->pname != NULL; cur++) { | 
208  | 4.82k  |   if (namelen == cur->pnamelen &&  | 
209  | 1.03k  |       strncasecmp(name, cur->pname, cur->pnamelen) == 0) { | 
210  | 607  |       char *pval = NULL;  | 
211  | 607  |       if (path != NULL) { | 
212  | 199  |     if ((pval = strdup(path)) == NULL) { | 
213  | 0  |         sudo_warnx(U_("%s: %s"), __func__, | 
214  | 0  |       U_("unable to allocate memory")); | 
215  | 0  |         debug_return_int(-1);  | 
216  | 0  |     }  | 
217  | 199  |       }  | 
218  | 607  |       if (cur->dynamic)  | 
219  | 567  |     free((char *)cur->pval);  | 
220  | 607  |       cur->pval = pval;  | 
221  | 607  |       cur->dynamic = true;  | 
222  | 607  |       sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %s:%u: Path %s %s",  | 
223  | 607  |     __func__, conf_file, lineno, cur->pname,  | 
224  | 607  |     pval ? pval : "(none)");  | 
225  | 607  |       debug_return_int(true);  | 
226  | 607  |   }  | 
227  | 4.82k  |     }  | 
228  | 1.20k  |     sudo_debug_printf(SUDO_DEBUG_WARN, "%s: %s:%u: unknown path %s",  | 
229  | 599  |   __func__, conf_file, lineno, entry);  | 
230  | 599  |     debug_return_int(false);  | 
231  | 1.91k  | bad:  | 
232  | 1.91k  |     sudo_warnx(U_("invalid Path value \"%s\" in %s, line %u"), | 
233  | 1.91k  |   entry, conf_file, lineno);  | 
234  | 1.91k  |     debug_return_int(false);  | 
235  | 1.91k  | }  | 
236  |  |  | 
237  |  | /*  | 
238  |  |  * "Debug program /path/to/log flags,..."  | 
239  |  |  */  | 
240  |  | static int  | 
241  |  | parse_debug(const char *entry, const char *conf_file, unsigned int lineno)  | 
242  | 2.54k  | { | 
243  | 2.54k  |     struct sudo_conf_debug *debug_spec;  | 
244  | 2.54k  |     struct sudo_debug_file *debug_file = NULL;  | 
245  | 2.54k  |     const char *ep, *path, *progname, *flags;  | 
246  | 2.54k  |     const char *entry_end = entry + strlen(entry);  | 
247  | 2.54k  |     size_t pathlen, prognamelen;  | 
248  | 2.54k  |     debug_decl(parse_debug, SUDO_DEBUG_UTIL);  | 
249  |  |  | 
250  |  |     /* Parse progname. */  | 
251  | 2.54k  |     progname = sudo_strsplit(entry, entry_end, " \t", &ep);  | 
252  | 2.54k  |     if (progname == NULL)  | 
253  | 194  |   debug_return_int(false); /* not enough fields */  | 
254  | 2.35k  |     prognamelen = (size_t)(ep - progname);  | 
255  |  |  | 
256  |  |     /* Parse path. */  | 
257  | 2.35k  |     path = sudo_strsplit(NULL, entry_end, " \t", &ep);  | 
258  | 2.35k  |     if (path == NULL)  | 
259  | 194  |   debug_return_int(false); /* not enough fields */  | 
260  | 2.16k  |     pathlen = (size_t)(ep - path);  | 
261  |  |  | 
262  |  |     /* Remainder is flags (freeform). */  | 
263  | 2.16k  |     flags = sudo_strsplit(NULL, entry_end, " \t", &ep);  | 
264  | 2.16k  |     if (flags == NULL)  | 
265  | 196  |   debug_return_int(false); /* not enough fields */  | 
266  |  |  | 
267  |  |     /* If progname already exists, use it, else alloc a new one. */  | 
268  | 36.0k  |     TAILQ_FOREACH(debug_spec, &sudo_conf_data.debugging, entries) { | 
269  | 36.0k  |   if (strncmp(debug_spec->progname, progname, prognamelen) == 0 &&  | 
270  | 1.13k  |       debug_spec->progname[prognamelen] == '\0')  | 
271  | 696  |       break;  | 
272  | 36.0k  |     }  | 
273  | 1.96k  |     if (debug_spec == NULL) { | 
274  | 1.26k  |   debug_spec = malloc(sizeof(*debug_spec));  | 
275  | 1.26k  |   if (debug_spec == NULL)  | 
276  | 0  |       goto oom;  | 
277  | 1.26k  |   debug_spec->progname = strndup(progname, prognamelen);  | 
278  | 1.26k  |   if (debug_spec->progname == NULL) { | 
279  | 0  |       free(debug_spec);  | 
280  | 0  |       debug_spec = NULL;  | 
281  | 0  |       goto oom;  | 
282  | 0  |   }  | 
283  | 1.26k  |   TAILQ_INIT(&debug_spec->debug_files);  | 
284  | 1.26k  |   TAILQ_INSERT_TAIL(&sudo_conf_data.debugging, debug_spec, entries);  | 
285  | 1.26k  |     }  | 
286  | 1.96k  |     debug_file = calloc(1, sizeof(*debug_file));  | 
287  | 1.96k  |     if (debug_file == NULL)  | 
288  | 0  |   goto oom;  | 
289  | 1.96k  |     debug_file->debug_file = strndup(path, pathlen);  | 
290  | 1.96k  |     if (debug_file->debug_file == NULL)  | 
291  | 0  |   goto oom;  | 
292  | 1.96k  |     debug_file->debug_flags = strdup(flags);  | 
293  | 1.96k  |     if (debug_file->debug_flags == NULL)  | 
294  | 0  |   goto oom;  | 
295  | 1.96k  |     TAILQ_INSERT_TAIL(&debug_spec->debug_files, debug_file, entries);  | 
296  |  |  | 
297  | 1.96k  |     debug_return_int(true);  | 
298  | 0  | oom:  | 
299  | 0  |     sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
300  | 0  |     if (debug_file != NULL) { | 
301  | 0  |   free(debug_file->debug_file);  | 
302  | 0  |   free(debug_file->debug_flags);  | 
303  | 0  |   free(debug_file);  | 
304  | 0  |     }  | 
305  | 0  |     debug_return_int(-1);  | 
306  | 0  | }  | 
307  |  |  | 
308  |  | /*  | 
309  |  |  * "Plugin symbol /path/to/log args..."  | 
310  |  |  */  | 
311  |  | static int  | 
312  |  | parse_plugin(const char *entry, const char *conf_file, unsigned int lineno)  | 
313  | 1.44k  | { | 
314  | 1.44k  |     struct plugin_info *info = NULL;  | 
315  | 1.44k  |     const char *ep, *path, *symbol;  | 
316  | 1.44k  |     const char *entry_end = entry + strlen(entry);  | 
317  | 1.44k  |     char **options = NULL;  | 
318  | 1.44k  |     size_t pathlen, symlen;  | 
319  | 1.44k  |     unsigned int nopts = 0;  | 
320  | 1.44k  |     debug_decl(parse_plugin, SUDO_DEBUG_UTIL);  | 
321  |  |  | 
322  |  |     /* Parse symbol. */  | 
323  | 1.44k  |     symbol = sudo_strsplit(entry, entry_end, " \t", &ep);  | 
324  | 1.44k  |     if (symbol == NULL)  | 
325  | 237  |   debug_return_int(false); /* not enough fields */  | 
326  | 1.21k  |     symlen = (size_t)(ep - symbol);  | 
327  |  |  | 
328  |  |     /* Parse path. */  | 
329  | 1.21k  |     path = sudo_strsplit(NULL, entry_end, " \t", &ep);  | 
330  | 1.21k  |     if (path == NULL)  | 
331  | 195  |   debug_return_int(false); /* not enough fields */  | 
332  | 1.01k  |     pathlen = (size_t)(ep - path);  | 
333  |  |  | 
334  |  |     /* Split options into an array if present. */  | 
335  | 1.01k  |     while (isblank((unsigned char)*ep))  | 
336  | 712  |   ep++;  | 
337  | 1.01k  |     if (*ep != '\0') { | 
338  |  |   /* Count number of options and allocate array. */  | 
339  | 499  |   const char *cp, *opt = ep;  | 
340  |  |  | 
341  |  |   /* Count and allocate options array. */  | 
342  | 499  |   for (nopts = 0, cp = sudo_strsplit(opt, entry_end, " \t", &ep);  | 
343  | 1.85k  |       cp != NULL; cp = sudo_strsplit(NULL, entry_end, " \t", &ep)) { | 
344  | 1.35k  |       nopts++;  | 
345  | 1.35k  |   }  | 
346  | 499  |   options = reallocarray(NULL, nopts + 1, sizeof(*options));  | 
347  | 499  |   if (options == NULL)  | 
348  | 0  |       goto oom;  | 
349  |  |  | 
350  |  |   /* Fill in options array. */  | 
351  | 499  |   for (nopts = 0, cp = sudo_strsplit(opt, entry_end, " \t", &ep);  | 
352  | 1.85k  |       cp != NULL; cp = sudo_strsplit(NULL, entry_end, " \t", &ep)) { | 
353  | 1.35k  |       options[nopts] = strndup(cp, (size_t)(ep - cp));  | 
354  | 1.35k  |       if (options[nopts] == NULL)  | 
355  | 0  |     goto oom;  | 
356  | 1.35k  |       nopts++;  | 
357  | 1.35k  |   }  | 
358  | 499  |   options[nopts] = NULL;  | 
359  | 499  |     }  | 
360  |  |  | 
361  | 1.01k  |     info = calloc(1, sizeof(*info));  | 
362  | 1.01k  |     if (info == NULL)  | 
363  | 0  |       goto oom;  | 
364  | 1.01k  |     info->symbol_name = strndup(symbol, symlen);  | 
365  | 1.01k  |     if (info->symbol_name == NULL)  | 
366  | 0  |       goto oom;  | 
367  | 1.01k  |     info->path = strndup(path, pathlen);  | 
368  | 1.01k  |     if (info->path == NULL)  | 
369  | 0  |       goto oom;  | 
370  | 1.01k  |     info->options = options;  | 
371  | 1.01k  |     info->lineno = lineno;  | 
372  | 1.01k  |     TAILQ_INSERT_TAIL(&sudo_conf_data.plugins, info, entries);  | 
373  |  |  | 
374  | 1.01k  |     debug_return_int(true);  | 
375  | 0  | oom:  | 
376  | 0  |     sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
377  | 0  |     if (options != NULL) { | 
378  | 0  |   while (nopts)  | 
379  | 0  |       free(options[--nopts]);  | 
380  | 0  |   free(options);  | 
381  | 0  |     }  | 
382  | 0  |     if (info != NULL) { | 
383  | 0  |   free(info->symbol_name);  | 
384  | 0  |   free(info->path);  | 
385  | 0  |   free(info);  | 
386  | 0  |     }  | 
387  | 0  |     debug_return_int(-1);  | 
388  | 0  | }  | 
389  |  |  | 
390  |  | static int  | 
391  |  | set_var_disable_coredump(const char *strval, const char *conf_file,  | 
392  |  |     unsigned int lineno)  | 
393  | 2.02k  | { | 
394  | 2.02k  |     int val = sudo_strtobool(strval);  | 
395  | 2.02k  |     debug_decl(set_var_disable_coredump, SUDO_DEBUG_UTIL);  | 
396  |  |  | 
397  | 2.02k  |     if (val == -1) { | 
398  | 1.61k  |   sudo_warnx(U_("invalid value for %s \"%s\" in %s, line %u"), | 
399  | 1.61k  |       "disable_coredump", strval, conf_file, lineno);  | 
400  | 1.61k  |   debug_return_int(false);  | 
401  | 1.61k  |     }  | 
402  | 408  |     sudo_conf_data.settings.disable_coredump = val;  | 
403  | 408  |     debug_return_int(true);  | 
404  | 408  | }  | 
405  |  |  | 
406  |  | static int  | 
407  |  | set_var_group_source(const char *strval, const char *conf_file,  | 
408  |  |     unsigned int lineno)  | 
409  | 1.03k  | { | 
410  | 1.03k  |     debug_decl(set_var_group_source, SUDO_DEBUG_UTIL);  | 
411  |  |  | 
412  | 1.03k  |     if (strcasecmp(strval, "adaptive") == 0) { | 
413  | 244  |   sudo_conf_data.settings.group_source = GROUP_SOURCE_ADAPTIVE;  | 
414  | 794  |     } else if (strcasecmp(strval, "static") == 0) { | 
415  | 198  |   sudo_conf_data.settings.group_source = GROUP_SOURCE_STATIC;  | 
416  | 596  |     } else if (strcasecmp(strval, "dynamic") == 0) { | 
417  | 194  |   sudo_conf_data.settings.group_source = GROUP_SOURCE_DYNAMIC;  | 
418  | 402  |     } else { | 
419  | 402  |   sudo_warnx(U_("unsupported group source \"%s\" in %s, line %u"), strval, | 
420  | 402  |       conf_file, lineno);  | 
421  | 402  |   debug_return_int(false);  | 
422  | 402  |     }  | 
423  | 636  |     debug_return_int(true);  | 
424  | 636  | }  | 
425  |  |  | 
426  |  | static int  | 
427  |  | set_var_max_groups(const char *strval, const char *conf_file,  | 
428  |  |     unsigned int lineno)  | 
429  | 1.77k  | { | 
430  | 1.77k  |     int max_groups;  | 
431  | 1.77k  |     debug_decl(set_var_max_groups, SUDO_DEBUG_UTIL);  | 
432  |  |  | 
433  | 1.77k  |     max_groups = (int)sudo_strtonum(strval, 1, 1024, NULL);  | 
434  | 1.77k  |     if (max_groups <= 0) { | 
435  | 1.23k  |   sudo_warnx(U_("invalid max groups \"%s\" in %s, line %u"), strval, | 
436  | 1.23k  |       conf_file, lineno);  | 
437  | 1.23k  |   debug_return_int(false);  | 
438  | 1.23k  |     }  | 
439  | 539  |     sudo_conf_data.settings.max_groups = max_groups;  | 
440  | 539  |     debug_return_int(true);  | 
441  | 539  | }  | 
442  |  |  | 
443  |  | static int  | 
444  |  | set_var_probe_interfaces(const char *strval, const char *conf_file,  | 
445  |  |     unsigned int lineno)  | 
446  | 2.37k  | { | 
447  | 2.37k  |     int val = sudo_strtobool(strval);  | 
448  | 2.37k  |     debug_decl(set_var_probe_interfaces, SUDO_DEBUG_UTIL);  | 
449  |  |  | 
450  | 2.37k  |     if (val == -1) { | 
451  | 1.08k  |   sudo_warnx(U_("invalid value for %s \"%s\" in %s, line %u"), | 
452  | 1.08k  |       "probe_interfaces", strval, conf_file, lineno);  | 
453  | 1.08k  |   debug_return_int(false);  | 
454  | 1.08k  |     }  | 
455  | 1.28k  |     sudo_conf_data.settings.probe_interfaces = val;  | 
456  | 1.28k  |     debug_return_int(true);  | 
457  | 1.28k  | }  | 
458  |  |  | 
459  |  | const char *  | 
460  |  | sudo_conf_askpass_path_v1(void)  | 
461  | 1.94k  | { | 
462  | 1.94k  |     return sudo_conf_data.path_table[SUDO_CONF_PATH_ASKPASS].pval;  | 
463  | 1.94k  | }  | 
464  |  |  | 
465  |  | const char *  | 
466  |  | sudo_conf_sesh_path_v1(void)  | 
467  | 1.94k  | { | 
468  | 1.94k  |     return sudo_conf_data.path_table[SUDO_CONF_PATH_SESH].pval;  | 
469  | 1.94k  | }  | 
470  |  |  | 
471  |  | const char *  | 
472  |  | sudo_conf_intercept_path_v1(void)  | 
473  | 1.94k  | { | 
474  | 1.94k  |     return sudo_conf_data.path_table[SUDO_CONF_PATH_INTERCEPT].pval;  | 
475  | 1.94k  | }  | 
476  |  |  | 
477  |  | const char *  | 
478  |  | sudo_conf_noexec_path_v1(void)  | 
479  | 1.94k  | { | 
480  | 1.94k  |     return sudo_conf_data.path_table[SUDO_CONF_PATH_NOEXEC].pval;  | 
481  | 1.94k  | }  | 
482  |  |  | 
483  |  | const char *  | 
484  |  | sudo_conf_plugin_dir_path_v1(void)  | 
485  | 1.94k  | { | 
486  | 1.94k  |     return sudo_conf_data.path_table[SUDO_CONF_PATH_PLUGIN_DIR].pval;  | 
487  | 1.94k  | }  | 
488  |  |  | 
489  |  | const char *  | 
490  |  | sudo_conf_devsearch_path_v1(void)  | 
491  | 0  | { | 
492  | 0  |     return sudo_conf_data.path_table[SUDO_CONF_PATH_DEVSEARCH].pval;  | 
493  | 0  | }  | 
494  |  |  | 
495  |  | int  | 
496  |  | sudo_conf_group_source_v1(void)  | 
497  | 1.95k  | { | 
498  | 1.95k  |     return sudo_conf_data.settings.group_source;  | 
499  | 1.95k  | }  | 
500  |  |  | 
501  |  | int  | 
502  |  | sudo_conf_max_groups_v1(void)  | 
503  | 1.94k  | { | 
504  | 1.94k  |     return sudo_conf_data.settings.max_groups;  | 
505  | 1.94k  | }  | 
506  |  |  | 
507  |  | struct plugin_info_list *  | 
508  |  | sudo_conf_plugins_v1(void)  | 
509  | 3.88k  | { | 
510  | 3.88k  |     return &sudo_conf_data.plugins;  | 
511  | 3.88k  | }  | 
512  |  |  | 
513  |  | struct sudo_conf_debug_list *  | 
514  |  | sudo_conf_debugging_v1(void)  | 
515  | 3.88k  | { | 
516  | 3.88k  |     return &sudo_conf_data.debugging;  | 
517  | 3.88k  | }  | 
518  |  |  | 
519  |  | /* Return the debug files list for a program, or NULL if none. */  | 
520  |  | struct sudo_conf_debug_file_list *  | 
521  |  | sudo_conf_debug_files_v1(const char *progname)  | 
522  | 1.94k  | { | 
523  | 1.94k  |     struct sudo_conf_debug *debug_spec;  | 
524  | 1.94k  |     const char *progbase;  | 
525  | 1.94k  |     debug_decl(sudo_conf_debug_files, SUDO_DEBUG_UTIL);  | 
526  |  |  | 
527  |  |     /* Determine basename if program is fully qualified (like for plugins). */  | 
528  | 1.94k  |     progbase = progname[0] == '/' ? sudo_basename(progname) : progname;  | 
529  |  |  | 
530  |  |     /* Convert sudoedit -> sudo. */  | 
531  | 1.94k  |     if (strcmp(progbase, "sudoedit") == 0)  | 
532  | 0  |   progbase = "sudo";  | 
533  |  |  | 
534  | 1.94k  |     TAILQ_FOREACH(debug_spec, &sudo_conf_data.debugging, entries) { | 
535  | 1.26k  |   const char *prog = progbase;  | 
536  |  |  | 
537  | 1.26k  |   if (debug_spec->progname[0] == '/') { | 
538  |  |       /* Match fully-qualified name, if possible. */  | 
539  | 214  |       prog = progname;  | 
540  | 214  |   }  | 
541  | 1.26k  |   if (strcmp(debug_spec->progname, prog) == 0)  | 
542  | 20  |       debug_return_ptr(&debug_spec->debug_files);  | 
543  |  |  | 
544  |  | #ifdef RTLD_MEMBER  | 
545  |  |   /* Handle names like sudoers.a(sudoers.so) for AIX. */  | 
546  |  |   const char *cp = strchr(prog, '('); | 
547  |  |   const char *ep = strchr(prog, ')');  | 
548  |  |   if (cp != NULL && ep != NULL) { | 
549  |  |       /* Match on the program name without the member. */  | 
550  |  |       size_t len = (size_t)(cp - prog);  | 
551  |  |       if (strncmp(debug_spec->progname, prog, len) == 0 &&  | 
552  |  |         debug_spec->progname[len] == '\0') { | 
553  |  |     debug_return_ptr(&debug_spec->debug_files);  | 
554  |  |       }  | 
555  |  |  | 
556  |  |       /* Match on the member itself. */  | 
557  |  |       cp++;  | 
558  |  |       len = (size_t)(ep - cp);  | 
559  |  |       if (strncmp(debug_spec->progname, cp, len) == 0 &&  | 
560  |  |         debug_spec->progname[len] == '\0') { | 
561  |  |     debug_return_ptr(&debug_spec->debug_files);  | 
562  |  |       }  | 
563  |  |   }  | 
564  |  | #endif  | 
565  | 1.26k  |     }  | 
566  | 1.92k  |     debug_return_ptr(NULL);  | 
567  | 1.92k  | }  | 
568  |  |  | 
569  |  | bool  | 
570  |  | sudo_conf_developer_mode_v1(void)  | 
571  | 0  | { | 
572  |  |     /* Developer mode was removed in sudo 1.9.13. */  | 
573  | 0  |     return false;  | 
574  | 0  | }  | 
575  |  |  | 
576  |  | bool  | 
577  |  | sudo_conf_disable_coredump_v1(void)  | 
578  | 1.94k  | { | 
579  | 1.94k  |     return sudo_conf_data.settings.disable_coredump;  | 
580  | 1.94k  | }  | 
581  |  |  | 
582  |  | bool  | 
583  |  | sudo_conf_probe_interfaces_v1(void)  | 
584  | 1.94k  | { | 
585  | 1.94k  |     return sudo_conf_data.settings.probe_interfaces;  | 
586  | 1.94k  | }  | 
587  |  |  | 
588  |  | /*  | 
589  |  |  * Free dynamically allocated parts of sudo_conf_data and  | 
590  |  |  * reset to initial values.  | 
591  |  |  */  | 
592  |  | static void  | 
593  |  | sudo_conf_init(int conf_types)  | 
594  | 611  | { | 
595  | 611  |     struct sudo_conf_debug *debug_spec;  | 
596  | 611  |     struct sudo_debug_file *debug_file;  | 
597  | 611  |     struct plugin_info *plugin_info;  | 
598  | 611  |     size_t i;  | 
599  | 611  |     debug_decl(sudo_conf_init, SUDO_DEBUG_UTIL);  | 
600  |  |  | 
601  |  |     /* Free and reset paths. */  | 
602  | 611  |     if (ISSET(conf_types, SUDO_CONF_PATHS)) { | 
603  | 611  |   const struct sudo_conf_path_table path_table[] = SUDO_CONF_PATH_INITIALIZER;  | 
604  | 611  |   sudo_conf_clear_paths();  | 
605  | 611  |   memcpy(sudo_conf_data.path_table, path_table,  | 
606  | 611  |       sizeof(sudo_conf_data.path_table));  | 
607  | 611  |     }  | 
608  |  |  | 
609  |  |     /* Free and reset debug settings. */  | 
610  | 611  |     if (ISSET(conf_types, SUDO_CONF_DEBUG)) { | 
611  | 1.87k  |   while ((debug_spec = TAILQ_FIRST(&sudo_conf_data.debugging))) { | 
612  | 1.26k  |       TAILQ_REMOVE(&sudo_conf_data.debugging, debug_spec, entries);  | 
613  | 1.26k  |       free(debug_spec->progname);  | 
614  | 3.23k  |       while ((debug_file = TAILQ_FIRST(&debug_spec->debug_files))) { | 
615  | 1.96k  |     TAILQ_REMOVE(&debug_spec->debug_files, debug_file, entries);  | 
616  | 1.96k  |     free(debug_file->debug_file);  | 
617  | 1.96k  |     free(debug_file->debug_flags);  | 
618  | 1.96k  |     free(debug_file);  | 
619  | 1.96k  |       }  | 
620  | 1.26k  |       free(debug_spec);  | 
621  | 1.26k  |   }  | 
622  | 611  |     }  | 
623  |  |  | 
624  |  |     /* Free and reset plugins. */  | 
625  | 611  |     if (ISSET(conf_types, SUDO_CONF_PLUGINS)) { | 
626  | 1.62k  |   while ((plugin_info = TAILQ_FIRST(&sudo_conf_data.plugins))) { | 
627  | 1.01k  |       TAILQ_REMOVE(&sudo_conf_data.plugins, plugin_info, entries);  | 
628  | 1.01k  |       free(plugin_info->symbol_name);  | 
629  | 1.01k  |       free(plugin_info->path);  | 
630  | 1.01k  |       if (plugin_info->options != NULL) { | 
631  | 1.85k  |     for (i = 0; plugin_info->options[i] != NULL; i++)  | 
632  | 1.35k  |         free(plugin_info->options[i]);  | 
633  | 499  |     free(plugin_info->options);  | 
634  | 499  |       }  | 
635  | 1.01k  |       free(plugin_info);  | 
636  | 1.01k  |   }  | 
637  | 611  |     }  | 
638  |  |  | 
639  |  |     /* Set initial values. */  | 
640  | 611  |     if (ISSET(conf_types, SUDO_CONF_SETTINGS)) { | 
641  | 611  |   const struct sudo_conf_settings settings = SUDO_CONF_SETTINGS_INITIALIZER;  | 
642  | 611  |   sudo_conf_data.settings = settings;  | 
643  | 611  |     }  | 
644  |  |  | 
645  | 611  |     debug_return;  | 
646  | 611  | }  | 
647  |  |  | 
648  |  | /*  | 
649  |  |  * Read in /etc/sudo.conf and populates sudo_conf_data.  | 
650  |  |  */  | 
651  |  | int  | 
652  |  | sudo_conf_read_v1(const char *path, int conf_types)  | 
653  | 1.94k  | { | 
654  | 1.94k  |     FILE *fp = NULL;  | 
655  | 1.94k  |     int fd = -1, ret = false;  | 
656  | 1.94k  |     char *prev_locale, *line = NULL;  | 
657  | 1.94k  |     unsigned int conf_lineno = 0;  | 
658  | 1.94k  |     char conf_file[PATH_MAX];  | 
659  | 1.94k  |     size_t linesize = 0;  | 
660  | 1.94k  |     debug_decl(sudo_conf_read, SUDO_DEBUG_UTIL);  | 
661  |  |  | 
662  | 1.94k  |     if ((prev_locale = setlocale(LC_ALL, NULL)) == NULL) { | 
663  | 0  |   sudo_warn("setlocale(LC_ALL, NULL)"); | 
664  | 0  |   debug_return_int(-1);  | 
665  | 0  |     }  | 
666  | 1.94k  |     if ((prev_locale = strdup(prev_locale)) == NULL) { | 
667  | 0  |   sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); | 
668  | 0  |   debug_return_int(-1);  | 
669  | 0  |     }  | 
670  |  |  | 
671  |  |     /* Parse sudo.conf in the "C" locale. */  | 
672  | 1.94k  |     if (prev_locale[0] != 'C' || prev_locale[1] != '\0')  | 
673  | 0  |         setlocale(LC_ALL, "C");  | 
674  |  |  | 
675  | 1.94k  |     if (path != NULL) { | 
676  |  |   /* Caller specified a single file, which must exist. */  | 
677  | 1.94k  |   if (strlcpy(conf_file, path, sizeof(conf_file)) >= sizeof(conf_file)) { | 
678  | 0  |       errno = ENAMETOOLONG;  | 
679  | 0  |       sudo_warn("%s", path); | 
680  | 0  |       goto done;  | 
681  | 0  |   }  | 
682  | 1.94k  |   fd = open(conf_file, O_RDONLY);  | 
683  | 1.94k  |   if (fd == -1) { | 
684  | 0  |       sudo_warn(U_("unable to open %s"), conf_file); | 
685  | 0  |       goto done;  | 
686  | 0  |   }  | 
687  | 1.94k  |     } else { | 
688  |  | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION  | 
689  |  |   struct stat sb;  | 
690  |  |   int error;  | 
691  |  |  | 
692  |  |   /* _PATH_SUDO_CONF is a colon-separated list of path. */  | 
693  |  |   fd = sudo_open_conf_path(_PATH_SUDO_CONF, conf_file,  | 
694  |  |       sizeof(conf_file), NULL);  | 
695  |  |   error = sudo_secure_fd(fd, S_IFREG, ROOT_UID, (gid_t)-1, &sb);  | 
696  |  |   switch (error) { | 
697  |  |   case SUDO_PATH_SECURE:  | 
698  |  |       /* OK! */  | 
699  |  |       break;  | 
700  |  |   case SUDO_PATH_MISSING:  | 
701  |  |       /* Root should always be able to read sudo.conf. */  | 
702  |  |       if (errno != ENOENT && geteuid() == ROOT_UID)  | 
703  |  |     sudo_warn(U_("unable to open %s"), conf_file); | 
704  |  |       goto done;  | 
705  |  |   case SUDO_PATH_BAD_TYPE:  | 
706  |  |       sudo_warnx(U_("%s is not a regular file"), conf_file); | 
707  |  |       goto done;  | 
708  |  |   case SUDO_PATH_WRONG_OWNER:  | 
709  |  |       sudo_warnx(U_("%s is owned by uid %u, should be %u"), | 
710  |  |     conf_file, (unsigned int) sb.st_uid, ROOT_UID);  | 
711  |  |       goto done;  | 
712  |  |   case SUDO_PATH_WORLD_WRITABLE:  | 
713  |  |       sudo_warnx(U_("%s is world writable"), conf_file); | 
714  |  |       goto done;  | 
715  |  |   case SUDO_PATH_GROUP_WRITABLE:  | 
716  |  |       sudo_warnx(U_("%s is group writable"), conf_file); | 
717  |  |       goto done;  | 
718  |  |   default:  | 
719  |  |       sudo_warnx("%s: internal error, unexpected error %d", | 
720  |  |     __func__, error);  | 
721  |  |       goto done;  | 
722  |  |   }  | 
723  |  | #else  | 
724  |  |   /* No default sudo.conf when fuzzing. */  | 
725  | 0  |   goto done;  | 
726  | 0  | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */  | 
727  | 0  |     }  | 
728  |  |  | 
729  | 1.94k  |     if ((fp = fdopen(fd, "r")) == NULL) { | 
730  | 0  |   sudo_warn(U_("unable to open %s"), conf_file); | 
731  | 0  |   goto done;  | 
732  | 0  |     }  | 
733  |  |  | 
734  |  |     /* Reset to initial values if necessary. */  | 
735  | 1.94k  |     if (sudo_conf_data.settings.updated)  | 
736  | 611  |   sudo_conf_init(conf_types);  | 
737  |  |  | 
738  | 18.9k  |     while (sudo_parseln(&line, &linesize, &conf_lineno, fp, 0) != -1) { | 
739  | 17.0k  |   struct sudo_conf_table *cur;  | 
740  | 17.0k  |   size_t i;  | 
741  | 17.0k  |   char *cp;  | 
742  |  |  | 
743  | 17.0k  |   if (*(cp = line) == '\0')  | 
744  | 952  |       continue;    /* empty line or comment */  | 
745  |  |  | 
746  | 50.2k  |   for (i = 0, cur = sudo_conf_table; cur->name != NULL; i++, cur++) { | 
747  | 49.0k  |       if (strncasecmp(cp, cur->name, cur->namelen) == 0 &&  | 
748  | 49.0k  |     isblank((unsigned char)cp[cur->namelen])) { | 
749  | 14.9k  |     if (ISSET(conf_types, (1 << i))) { | 
750  | 14.9k  |         cp += cur->namelen;  | 
751  | 14.9k  |         while (isblank((unsigned char)*cp))  | 
752  | 15.2k  |       cp++;  | 
753  | 14.9k  |         ret = cur->parser(cp, conf_file, conf_lineno);  | 
754  | 14.9k  |         switch (ret) { | 
755  | 6.45k  |         case true:  | 
756  | 6.45k  |       sudo_conf_data.settings.updated = true;  | 
757  | 6.45k  |       break;  | 
758  | 8.50k  |         case false:  | 
759  | 8.50k  |       break;  | 
760  | 0  |         default:  | 
761  | 0  |       goto done;  | 
762  | 14.9k  |         }  | 
763  | 14.9k  |     }  | 
764  | 14.9k  |     break;  | 
765  | 14.9k  |       }  | 
766  | 49.0k  |   }  | 
767  | 16.1k  |   if (cur->name == NULL) { | 
768  | 1.13k  |       sudo_debug_printf(SUDO_DEBUG_WARN,  | 
769  | 1.13k  |     "%s: %s:%u: unsupported entry: %s", __func__, conf_file,  | 
770  | 1.13k  |     conf_lineno, line);  | 
771  | 1.13k  |   }  | 
772  | 16.1k  |     }  | 
773  | 1.94k  |     ret = true;  | 
774  |  |  | 
775  | 1.94k  | done:  | 
776  | 1.94k  |     if (fp != NULL)  | 
777  | 1.94k  |   fclose(fp);  | 
778  | 0  |     else if (fd != -1)  | 
779  | 0  |   close(fd);  | 
780  | 1.94k  |     free(line);  | 
781  |  |  | 
782  |  |     /* Restore locale if needed. */  | 
783  | 1.94k  |     if (prev_locale[0] != 'C' || prev_locale[1] != '\0')  | 
784  | 0  |         setlocale(LC_ALL, prev_locale);  | 
785  | 1.94k  |     free(prev_locale);  | 
786  | 1.94k  |     debug_return_int(ret);  | 
787  | 1.94k  | }  | 
788  |  |  | 
789  |  | /*  | 
790  |  |  * Used by the sudo_conf regress test to clear compile-time path settings.  | 
791  |  |  */  | 
792  |  | void  | 
793  |  | sudo_conf_clear_paths_v1(void)  | 
794  | 2.55k  | { | 
795  | 2.55k  |     struct sudo_conf_path_table *cur;  | 
796  | 2.55k  |     debug_decl(sudo_conf_clear_paths, SUDO_DEBUG_UTIL);  | 
797  |  |  | 
798  | 17.8k  |     for (cur = sudo_conf_data.path_table; cur->pname != NULL; cur++) { | 
799  | 15.3k  |   if (cur->dynamic)  | 
800  | 40  |       free((char *)cur->pval);  | 
801  | 15.3k  |   cur->pval = NULL;  | 
802  |  |   cur->dynamic = false;  | 
803  | 15.3k  |     }  | 
804  | 2.55k  | }  |