2 * Copyright (c) 1999-2005, 2007-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/types.h>
25 #include <sys/param.h>
34 #endif /* STDC_HEADERS */
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRINGS_H */
43 #endif /* HAVE_UNISTD_H */
52 * For converting between syslog numbers and strings.
59 #ifdef LOG_NFACILITIES
60 static struct strmap facilities[] = {
62 { "authpriv", LOG_AUTHPRIV },
65 { "daemon", LOG_DAEMON },
67 { "local0", LOG_LOCAL0 },
68 { "local1", LOG_LOCAL1 },
69 { "local2", LOG_LOCAL2 },
70 { "local3", LOG_LOCAL3 },
71 { "local4", LOG_LOCAL4 },
72 { "local5", LOG_LOCAL5 },
73 { "local6", LOG_LOCAL6 },
74 { "local7", LOG_LOCAL7 },
77 #endif /* LOG_NFACILITIES */
79 static struct strmap priorities[] = {
80 { "alert", LOG_ALERT },
82 { "debug", LOG_DEBUG },
83 { "emerg", LOG_EMERG },
86 { "notice", LOG_NOTICE },
87 { "warning", LOG_WARNING },
94 static int store_int(char *, struct sudo_defs_types *, int);
95 static int store_list(char *, struct sudo_defs_types *, int);
96 static int store_mode(char *, struct sudo_defs_types *, int);
97 static int store_str(char *, struct sudo_defs_types *, int);
98 static int store_syslogfac(char *, struct sudo_defs_types *, int);
99 static int store_syslogpri(char *, struct sudo_defs_types *, int);
100 static int store_tuple(char *, struct sudo_defs_types *, int);
101 static int store_uint(char *, struct sudo_defs_types *, int);
102 static int store_float(char *, struct sudo_defs_types *, int);
103 static void list_op(char *, size_t, struct sudo_defs_types *, enum list_ops);
104 static const char *logfac2str(int);
105 static const char *logpri2str(int);
108 * Table describing compile-time and run-time options.
110 #include <def_data.c>
113 * Print version and configure info.
118 struct sudo_defs_types *cur;
119 struct list_member *item;
120 struct def_values *def;
122 for (cur = sudo_defs_table; cur->name; cur++) {
124 switch (cur->type & T_MASK) {
127 sudo_printf(SUDO_CONV_INFO_MSG, "%s\n", cur->desc);
130 if (cur->sd_un.str) {
131 sudo_printf(SUDO_CONV_INFO_MSG,
132 cur->desc, cur->sd_un.str);
133 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
137 if (cur->sd_un.ival) {
138 sudo_printf(SUDO_CONV_INFO_MSG,
139 cur->desc, logfac2str(cur->sd_un.ival));
140 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
144 if (cur->sd_un.ival) {
145 sudo_printf(SUDO_CONV_INFO_MSG,
146 cur->desc, logpri2str(cur->sd_un.ival));
147 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
152 sudo_printf(SUDO_CONV_INFO_MSG, cur->desc, cur->sd_un.ival);
153 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
156 sudo_printf(SUDO_CONV_INFO_MSG, cur->desc, cur->sd_un.fval);
157 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
160 sudo_printf(SUDO_CONV_INFO_MSG, cur->desc, cur->sd_un.mode);
161 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
164 if (cur->sd_un.list) {
165 sudo_printf(SUDO_CONV_INFO_MSG, "%s\n", cur->desc);
166 for (item = cur->sd_un.list; item; item = item->next) {
167 sudo_printf(SUDO_CONV_INFO_MSG,
168 "\t%s\n", item->value);
173 for (def = cur->values; def->sval; def++) {
174 if (cur->sd_un.ival == def->ival) {
175 sudo_printf(SUDO_CONV_INFO_MSG,
176 cur->desc, def->sval);
180 sudo_printf(SUDO_CONV_INFO_MSG, "\n");
188 * List each option along with its description.
193 struct sudo_defs_types *cur;
196 sudo_printf(SUDO_CONV_INFO_MSG,
197 "Available options in a sudoers ``Defaults'' line:\n\n");
198 for (cur = sudo_defs_table; cur->name; cur++) {
199 if (cur->name && cur->desc) {
200 switch (cur->type & T_MASK) {
202 sudo_printf(SUDO_CONV_INFO_MSG,
203 "%s: %s\n", cur->name, cur->desc);
206 p = strrchr(cur->desc, ':');
208 sudo_printf(SUDO_CONV_INFO_MSG, "%s: %.*s\n",
209 cur->name, (int) (p - cur->desc), cur->desc);
211 sudo_printf(SUDO_CONV_INFO_MSG,
212 "%s: %s\n", cur->name, cur->desc);
221 * Sets/clears an entry in the defaults structure
222 * If a variable that takes a value is used in a boolean
223 * context with op == 0, disable that variable.
224 * Eg. you may want to turn off logging to a file for some hosts.
225 * This is only meaningful for variables that are *optional*.
228 set_default(char *var, char *val, int op)
230 struct sudo_defs_types *cur;
233 for (cur = sudo_defs_table, num = 0; cur->name; cur++, num++) {
234 if (strcmp(var, cur->name) == 0)
238 warningx("unknown defaults entry `%s'", var);
242 switch (cur->type & T_MASK) {
244 if (!store_syslogfac(val, cur, op)) {
246 warningx("value `%s' is invalid for option `%s'", val, var);
248 warningx("no value specified for `%s'", var);
253 if (!store_syslogpri(val, cur, op)) {
255 warningx("value `%s' is invalid for option `%s'", val, var);
257 warningx("no value specified for `%s'", var);
263 /* Check for bogus boolean usage or lack of a value. */
264 if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
265 warningx("no value specified for `%s'", var);
269 if (ISSET(cur->type, T_PATH) && val && *val != '/') {
270 warningx("values for `%s' must start with a '/'", var);
273 if (!store_str(val, cur, op)) {
274 warningx("value `%s' is invalid for option `%s'", val, var);
280 /* Check for bogus boolean usage or lack of a value. */
281 if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
282 warningx("no value specified for `%s'", var);
286 if (!store_int(val, cur, op)) {
287 warningx("value `%s' is invalid for option `%s'", val, var);
293 /* Check for bogus boolean usage or lack of a value. */
294 if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
295 warningx("no value specified for `%s'", var);
299 if (!store_uint(val, cur, op)) {
300 warningx("value `%s' is invalid for option `%s'", val, var);
306 /* Check for bogus boolean usage or lack of a value. */
307 if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
308 warningx("no value specified for `%s'", var);
312 if (!store_float(val, cur, op)) {
313 warningx("value `%s' is invalid for option `%s'", val, var);
319 /* Check for bogus boolean usage or lack of a value. */
320 if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
321 warningx("no value specified for `%s'", var);
325 if (!store_mode(val, cur, op)) {
326 warningx("value `%s' is invalid for option `%s'", val, var);
332 warningx("option `%s' does not take a value", var);
335 cur->sd_un.flag = op;
339 /* Check for bogus boolean usage or lack of a value. */
340 if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
341 warningx("no value specified for `%s'", var);
345 if (!store_list(val, cur, op)) {
346 warningx("value `%s' is invalid for option `%s'", val, var);
351 if (!val && !ISSET(cur->type, T_BOOL)) {
352 warningx("no value specified for `%s'", var);
355 if (!store_tuple(val, cur, op)) {
356 warningx("value `%s' is invalid for option `%s'", val, var);
366 * Set default options to compiled-in values.
367 * Any of these may be overridden at runtime by a "Defaults" file.
372 static int firsttime = 1;
373 struct sudo_defs_types *def;
375 /* Clear any old settings. */
377 for (def = sudo_defs_table; def->name; def++) {
378 switch (def->type & T_MASK) {
380 efree(def->sd_un.str);
381 def->sd_un.str = NULL;
384 list_op(NULL, 0, def, freeall);
387 zero_bytes(&def->sd_un, sizeof(def->sd_un));
391 /* First initialize the flags. */
392 #ifdef LONG_OTP_PROMPT
393 def_long_otp_prompt = TRUE;
395 #ifdef IGNORE_DOT_PATH
396 def_ignore_dot = TRUE;
398 #ifdef ALWAYS_SEND_MAIL
399 def_mail_always = TRUE;
401 #ifdef SEND_MAIL_WHEN_NO_USER
402 def_mail_no_user = TRUE;
404 #ifdef SEND_MAIL_WHEN_NO_HOST
405 def_mail_no_host = TRUE;
407 #ifdef SEND_MAIL_WHEN_NOT_OK
408 def_mail_no_perms = TRUE;
410 #ifndef NO_TTY_TICKETS
411 def_tty_tickets = TRUE;
416 #ifndef NO_AUTHENTICATION
417 def_authenticate = TRUE;
420 def_root_sudo = TRUE;
425 #ifdef SHELL_IF_NO_ARGS
426 def_shell_noargs = TRUE;
428 #ifdef SHELL_SETS_HOME
431 #ifndef DONT_LEAK_PATH_INFO
432 def_path_info = TRUE;
441 def_env_editor = TRUE;
443 #ifdef UMASK_OVERRIDE
444 def_umask_override = TRUE;
446 def_iolog_file = estrdup("%{seq}");
447 def_iolog_dir = estrdup(_PATH_SUDO_IO_LOGDIR);
448 def_sudoers_locale = estrdup("C");
449 def_env_reset = ENV_RESET;
450 def_set_logname = TRUE;
451 def_closefrom = STDERR_FILENO + 1;
453 /* Syslog options need special care since they both strings and ints */
454 #if (LOGGING & SLOG_SYSLOG)
455 (void) store_syslogfac(LOGFAC, &sudo_defs_table[I_SYSLOG], TRUE);
456 (void) store_syslogpri(PRI_SUCCESS, &sudo_defs_table[I_SYSLOG_GOODPRI],
458 (void) store_syslogpri(PRI_FAILURE, &sudo_defs_table[I_SYSLOG_BADPRI],
462 /* Password flags also have a string and integer component. */
463 (void) store_tuple("any", &sudo_defs_table[I_LISTPW], TRUE);
464 (void) store_tuple("all", &sudo_defs_table[I_VERIFYPW], TRUE);
466 /* Then initialize the int-like things. */
468 def_umask = SUDO_UMASK;
472 def_loglinelen = MAXLOGFILELEN;
473 def_timestamp_timeout = TIMEOUT;
474 def_passwd_timeout = PASSWORD_TIMEOUT;
475 def_passwd_tries = TRIES_FOR_PASSWORD;
477 def_compress_io = TRUE;
480 /* Now do the strings */
481 def_mailto = estrdup(MAILTO);
482 def_mailsub = estrdup(MAILSUBJECT);
483 def_badpass_message = estrdup(INCORRECT_PASSWORD);
484 def_timestampdir = estrdup(_PATH_SUDO_TIMEDIR);
485 def_passprompt = estrdup(PASSPROMPT);
486 def_runas_default = estrdup(RUNAS_DEFAULT);
487 #ifdef _PATH_SUDO_SENDMAIL
488 def_mailerpath = estrdup(_PATH_SUDO_SENDMAIL);
489 def_mailerflags = estrdup("-t");
491 #if (LOGGING & SLOG_FILE)
492 def_logfile = estrdup(_PATH_SUDO_LOGFILE);
495 def_exempt_group = estrdup(EXEMPTGROUP);
498 def_secure_path = estrdup(SECURE_PATH);
500 def_editor = estrdup(EDITOR);
503 /* Finally do the lists (currently just environment tables). */
510 * Update the defaults based on what was set by sudoers.
511 * Pass in an OR'd list of which default types to update.
514 update_defaults(int what)
516 struct defaults *def;
519 tq_foreach_fwd(&defaults, def) {
522 if (ISSET(what, SETDEF_GENERIC) &&
523 !set_default(def->var, def->val, def->op))
527 if (ISSET(what, SETDEF_USER) &&
528 userlist_matches(sudo_user.pw, &def->binding) == ALLOW &&
529 !set_default(def->var, def->val, def->op))
533 if (ISSET(what, SETDEF_RUNAS) &&
534 runaslist_matches(&def->binding, NULL) == ALLOW &&
535 !set_default(def->var, def->val, def->op))
539 if (ISSET(what, SETDEF_HOST) &&
540 hostlist_matches(&def->binding) == ALLOW &&
541 !set_default(def->var, def->val, def->op))
545 if (ISSET(what, SETDEF_CMND) &&
546 cmndlist_matches(&def->binding) == ALLOW &&
547 !set_default(def->var, def->val, def->op))
556 store_int(char *val, struct sudo_defs_types *def, int op)
564 l = strtol(val, &endp, 10);
567 /* XXX - should check against INT_MAX */
568 def->sd_un.ival = (int)l;
571 return def->callback(val);
576 store_uint(char *val, struct sudo_defs_types *def, int op)
584 l = strtol(val, &endp, 10);
585 if (*endp != '\0' || l < 0)
587 /* XXX - should check against INT_MAX */
588 def->sd_un.ival = (unsigned int)l;
591 return def->callback(val);
596 store_float(char *val, struct sudo_defs_types *def, int op)
602 def->sd_un.fval = 0.0;
604 d = strtod(val, &endp);
607 /* XXX - should check against HUGE_VAL */
611 return def->callback(val);
616 store_tuple(char *val, struct sudo_defs_types *def, int op)
618 struct def_values *v;
621 * Since enums are really just ints we store the value as an ival.
622 * In the future, there may be multiple enums for different tuple
623 * types we want to avoid and special knowledge of the tuple type.
624 * This does assume that the first entry in the tuple enum will
625 * be the equivalent to a boolean "false".
628 def->sd_un.ival = (op == FALSE) ? 0 : 1;
630 for (v = def->values; v->sval != NULL; v++) {
631 if (strcmp(v->sval, val) == 0) {
632 def->sd_un.ival = v->ival;
640 return def->callback(val);
645 store_str(char *val, struct sudo_defs_types *def, int op)
648 efree(def->sd_un.str);
650 def->sd_un.str = NULL;
652 def->sd_un.str = estrdup(val);
654 return def->callback(val);
659 store_list(char *str, struct sudo_defs_types *def, int op)
663 /* Remove all old members. */
664 if (op == FALSE || op == TRUE)
665 list_op(NULL, 0, def, freeall);
667 /* Split str into multiple space-separated words and act on each one. */
671 /* Remove leading blanks, if nothing but blanks we are done. */
672 for (start = end; isblank((unsigned char)*start); start++)
677 /* Find end position and perform operation. */
678 for (end = start; *end && !isblank((unsigned char)*end); end++)
680 list_op(start, end - start, def, op == '-' ? delete : add);
681 } while (*end++ != '\0');
687 store_syslogfac(char *val, struct sudo_defs_types *def, int op)
692 def->sd_un.ival = FALSE;
695 #ifdef LOG_NFACILITIES
698 for (fac = facilities; fac->name && strcmp(val, fac->name); fac++)
700 if (fac->name == NULL)
701 return FALSE; /* not found */
703 def->sd_un.ival = fac->num;
705 def->sd_un.ival = -1;
706 #endif /* LOG_NFACILITIES */
713 #ifdef LOG_NFACILITIES
716 for (fac = facilities; fac->name && fac->num != n; fac++)
721 #endif /* LOG_NFACILITIES */
725 store_syslogpri(char *val, struct sudo_defs_types *def, int op)
729 if (op == FALSE || !val)
732 for (pri = priorities; pri->name && strcmp(val, pri->name); pri++)
734 if (pri->name == NULL)
735 return FALSE; /* not found */
737 def->sd_un.ival = pri->num;
746 for (pri = priorities; pri->name && pri->num != n; pri++)
752 store_mode(char *val, struct sudo_defs_types *def, int op)
758 def->sd_un.mode = (mode_t)0777;
760 l = strtol(val, &endp, 8);
761 if (*endp != '\0' || l < 0 || l > 0777)
763 def->sd_un.mode = (mode_t)l;
766 return def->callback(val);
771 list_op(char *val, size_t len, struct sudo_defs_types *def, enum list_ops op)
773 struct list_member *cur, *prev, *tmp;
776 for (cur = def->sd_un.list; cur; ) {
782 def->sd_un.list = NULL;
786 for (cur = def->sd_un.list, prev = NULL; cur; prev = cur, cur = cur->next) {
787 if ((strncmp(cur->value, val, len) == 0 && cur->value[len] == '\0')) {
790 return; /* already exists */
794 prev->next = cur->next;
796 def->sd_un.list = cur->next;
803 /* Add new node to the head of the list. */
805 cur = emalloc(sizeof(struct list_member));
806 cur->value = emalloc(len + 1);
807 (void) memcpy(cur->value, val, len);
808 cur->value[len] = '\0';
809 cur->next = def->sd_un.list;
810 def->sd_un.list = cur;