2 * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
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.
19 #include <sys/types.h>
29 #endif /* STDC_HEADERS */
33 # include "compat/stdbool.h"
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRINGS_H */
43 #endif /* HAVE_UNISTD_H */
48 #define SUDO_ERROR_WRAP 0
54 #include "pathnames.h"
55 #include "sudo_plugin.h"
56 #include "sudo_conf.h"
57 #include "sudo_debug.h"
58 #include "secure_path.h"
60 #define DEFAULT_TEXT_DOMAIN "sudo"
64 # define ROOT_UID 65535
69 extern bool atobool(const char *str); /* atobool.c */
71 struct sudo_conf_table {
74 void (*setter)(const char *entry, const char *conf_file);
77 struct sudo_conf_paths {
79 unsigned int pnamelen;
83 static void set_debug(const char *entry, const char *conf_file);
84 static void set_path(const char *entry, const char *conf_file);
85 static void set_plugin(const char *entry, const char *conf_file);
86 static void set_variable(const char *entry, const char *conf_file);
87 static void set_var_disable_coredump(const char *entry, const char *conf_file);
88 static void set_var_group_source(const char *entry, const char *conf_file);
89 static void set_var_max_groups(const char *entry, const char *conf_file);
91 static unsigned int conf_lineno;
93 static struct sudo_conf_table sudo_conf_table[] = {
94 { "Debug", sizeof("Debug") - 1, set_debug },
95 { "Path", sizeof("Path") - 1, set_path },
96 { "Plugin", sizeof("Plugin") - 1, set_plugin },
97 { "Set", sizeof("Set") - 1, set_variable },
101 static struct sudo_conf_table sudo_conf_table_vars[] = {
102 { "disable_coredump", sizeof("disable_coredump") - 1, set_var_disable_coredump },
103 { "group_source", sizeof("group_source") - 1, set_var_group_source },
104 { "max_groups", sizeof("max_groups") - 1, set_var_max_groups },
108 static struct sudo_conf_data {
109 bool disable_coredump;
112 const char *debug_flags;
113 struct sudo_conf_paths paths[4];
114 struct plugin_info_list plugins;
117 GROUP_SOURCE_ADAPTIVE,
121 #define SUDO_CONF_ASKPASS_IDX 0
122 { "askpass", sizeof("askpass") - 1, _PATH_SUDO_ASKPASS },
123 #define SUDO_CONF_SESH_IDX 1
124 { "sesh", sizeof("sesh") - 1, _PATH_SUDO_SESH },
125 #ifdef _PATH_SUDO_NOEXEC
126 #define SUDO_CONF_NOEXEC_IDX 2
127 { "noexec", sizeof("noexec") - 1, _PATH_SUDO_NOEXEC },
134 * "Set variable_name value"
137 set_variable(const char *entry, const char *conf_file)
139 struct sudo_conf_table *var;
141 for (var = sudo_conf_table_vars; var->name != NULL; var++) {
142 if (strncmp(entry, var->name, var->namelen) == 0 &&
143 isblank((unsigned char)entry[var->namelen])) {
144 entry += var->namelen + 1;
145 while (isblank((unsigned char)*entry))
147 var->setter(entry, conf_file);
154 set_var_disable_coredump(const char *entry, const char *conf_file)
156 int val = atobool(entry);
159 sudo_conf_data.disable_coredump = val;
163 set_var_group_source(const char *entry, const char *conf_file)
165 if (strcasecmp(entry, "adaptive") == 0) {
166 sudo_conf_data.group_source = GROUP_SOURCE_ADAPTIVE;
167 } else if (strcasecmp(entry, "static") == 0) {
168 sudo_conf_data.group_source = GROUP_SOURCE_STATIC;
169 } else if (strcasecmp(entry, "dynamic") == 0) {
170 sudo_conf_data.group_source = GROUP_SOURCE_DYNAMIC;
172 warningx(_("unsupported group source `%s' in %s, line %d"), entry,
173 conf_file, conf_lineno);
178 set_var_max_groups(const char *entry, const char *conf_file)
183 lval = strtol(entry, &ep, 10);
184 if (*entry == '\0' || *ep != '\0' || lval < 0 || lval > INT_MAX ||
185 (errno == ERANGE && lval == LONG_MAX)) {
186 warningx(_("invalid max groups `%s' in %s, line %d"), entry,
187 conf_file, conf_lineno);
189 sudo_conf_data.max_groups = (int)lval;
194 * "Debug progname debug_file debug_flags"
197 set_debug(const char *entry, const char *conf_file)
199 size_t filelen, proglen;
200 const char *progname;
201 char *debug_file, *debug_flags;
203 /* Is this debug setting for me? */
204 progname = getprogname();
205 if (strcmp(progname, "sudoedit") == 0)
207 proglen = strlen(progname);
208 if (strncmp(entry, progname, proglen) != 0 ||
209 !isblank((unsigned char)entry[proglen]))
211 entry += proglen + 1;
212 while (isblank((unsigned char)*entry))
215 debug_flags = strpbrk(entry, " \t");
216 if (debug_flags == NULL)
218 filelen = (size_t)(debug_flags - entry);
219 while (isblank((unsigned char)*debug_flags))
222 /* Set debug file and parse the flags (init debug as soon as possible). */
223 debug_file = estrndup(entry, filelen);
224 debug_flags = estrdup(debug_flags);
225 sudo_debug_init(debug_file, debug_flags);
228 sudo_conf_data.debug_flags = debug_flags;
232 set_path(const char *entry, const char *conf_file)
234 const char *name, *path;
235 struct sudo_conf_paths *cur;
237 /* Parse Path line */
239 path = strpbrk(entry, " \t");
242 while (isblank((unsigned char)*path))
245 /* Match supported paths, ignore the rest. */
246 for (cur = sudo_conf_data.paths; cur->pname != NULL; cur++) {
247 if (strncasecmp(name, cur->pname, cur->pnamelen) == 0 &&
248 isblank((unsigned char)name[cur->pnamelen])) {
249 cur->pval = estrdup(path);
256 set_plugin(const char *entry, const char *conf_file)
258 struct plugin_info *info;
259 const char *name, *path, *cp, *ep;
260 char **options = NULL;
261 size_t namelen, pathlen;
264 /* Parse Plugin line */
266 path = strpbrk(entry, " \t");
269 namelen = (size_t)(path - name);
270 while (isblank((unsigned char)*path))
272 if ((cp = strpbrk(path, " \t")) != NULL) {
273 /* Convert any options to an array. */
274 pathlen = (size_t)(cp - path);
275 while (isblank((unsigned char)*cp))
277 /* Count number of options and allocate array. */
278 for (ep = cp, nopts = 1; (ep = strpbrk(ep, " \t")) != NULL; nopts++) {
279 while (isblank((unsigned char)*ep))
282 options = emalloc2(nopts + 1, sizeof(*options));
283 /* Fill in options array, there is at least one element. */
284 for (nopts = 0; (ep = strpbrk(cp, " \t")) != NULL; ) {
285 options[nopts++] = estrndup(cp, (size_t)(ep - cp));
286 while (isblank((unsigned char)*ep))
290 options[nopts++] = estrdup(cp);
291 options[nopts] = NULL;
293 /* No extra options. */
294 pathlen = strlen(path);
297 info = ecalloc(1, sizeof(*info));
298 info->symbol_name = estrndup(name, namelen);
299 info->path = estrndup(path, pathlen);
300 info->options = options;
302 /* info->next = NULL; */
303 info->lineno = conf_lineno;
304 tq_append(&sudo_conf_data.plugins, info);
308 sudo_conf_askpass_path(void)
310 return sudo_conf_data.paths[SUDO_CONF_ASKPASS_IDX].pval;
314 sudo_conf_sesh_path(void)
316 return sudo_conf_data.paths[SUDO_CONF_SESH_IDX].pval;
319 #ifdef _PATH_SUDO_NOEXEC
321 sudo_conf_noexec_path(void)
323 return sudo_conf_data.paths[SUDO_CONF_NOEXEC_IDX].pval;
328 sudo_conf_debug_flags(void)
330 return sudo_conf_data.debug_flags;
334 sudo_conf_group_source(void)
336 return sudo_conf_data.group_source;
340 sudo_conf_max_groups(void)
342 return sudo_conf_data.max_groups;
345 struct plugin_info_list *
346 sudo_conf_plugins(void)
348 return &sudo_conf_data.plugins;
352 sudo_conf_disable_coredump(void)
354 return sudo_conf_data.disable_coredump;
358 * Reads in /etc/sudo.conf and populates sudo_conf_data.
361 sudo_conf_read(const char *conf_file)
363 struct sudo_conf_table *cur;
366 char *cp, *line = NULL;
367 char *prev_locale = estrdup(setlocale(LC_ALL, NULL));
370 /* Parse sudo.conf in the "C" locale. */
371 if (prev_locale[0] != 'C' || prev_locale[1] != '\0')
372 setlocale(LC_ALL, "C");
374 if (conf_file == NULL) {
375 conf_file = _PATH_SUDO_CONF;
376 switch (sudo_secure_file(conf_file, ROOT_UID, -1, &sb)) {
377 case SUDO_PATH_SECURE:
379 case SUDO_PATH_MISSING:
380 /* Root should always be able to read sudo.conf. */
381 if (errno != ENOENT && geteuid() == ROOT_UID)
382 warning(_("unable to stat %s"), conf_file);
384 case SUDO_PATH_BAD_TYPE:
385 warningx(_("%s is not a regular file"), conf_file);
387 case SUDO_PATH_WRONG_OWNER:
388 warningx(_("%s is owned by uid %u, should be %u"),
389 conf_file, (unsigned int) sb.st_uid, ROOT_UID);
391 case SUDO_PATH_WORLD_WRITABLE:
392 warningx(_("%s is world writable"), conf_file);
394 case SUDO_PATH_GROUP_WRITABLE:
395 warningx(_("%s is group writable"), conf_file);
403 if ((fp = fopen(conf_file, "r")) == NULL) {
404 if (errno != ENOENT && geteuid() == ROOT_UID)
405 warning(_("unable to open %s"), conf_file);
410 while (sudo_parseln(&line, &linesize, &conf_lineno, fp) != -1) {
411 if (*(cp = line) == '\0')
412 continue; /* empty line or comment */
414 for (cur = sudo_conf_table; cur->name != NULL; cur++) {
415 if (strncasecmp(cp, cur->name, cur->namelen) == 0 &&
416 isblank((unsigned char)cp[cur->namelen])) {
418 while (isblank((unsigned char)*cp))
420 cur->setter(cp, conf_file);
428 /* Restore locale if needed. */
429 if (prev_locale[0] != 'C' || prev_locale[1] != '\0')
430 setlocale(LC_ALL, prev_locale);