Imported Upstream version 1.7.6p1
[debian/sudo] / defaults.c
index 667f44a4a5f6530cfea2d30c7aa08ad168ef423a..c1f0afa76d66bc753467d8913ebddbb52154f321 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 1999-2001, 2003-2004 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1999-2005, 2007-2008, 2010
+ *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -18,7 +19,7 @@
  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <sys/types.h>
 #include <sys/param.h>
 #endif /* STDC_HEADERS */
 #ifdef HAVE_STRING_H
 # include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-#  include <strings.h>
-# endif
 #endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
 # ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif /* HAVE_UNISTD_H */
 #include <pwd.h>
-#ifdef HAVE_ERR_H
-# include <err.h>
-#else
-# include "emul/err.h"
-#endif /* HAVE_ERR_H */
 #include <ctype.h>
 
 #include "sudo.h"
-
-#ifndef lint
-static const char rcsid[] = "$Sudo: defaults.c,v 1.48 2004/06/06 23:58:10 millert Exp $";
-#endif /* lint */
+#include "parse.h"
+#include <gram.h>
 
 /*
  * For converting between syslog numbers and strings.
@@ -95,8 +88,6 @@ static struct strmap priorities[] = {
        { NULL,         -1 }
 };
 
-extern int sudolineno;
-
 /*
  * Local prototypes.
  */
@@ -108,6 +99,7 @@ static int store_syslogfac __P((char *, struct sudo_defs_types *, int));
 static int store_syslogpri __P((char *, struct sudo_defs_types *, int));
 static int store_tuple __P((char *, struct sudo_defs_types *, int));
 static int store_uint __P((char *, struct sudo_defs_types *, int));
+static int store_float __P((char *, struct sudo_defs_types *, int));
 static void list_op __P((char *, size_t, struct sudo_defs_types *, enum list_ops));
 static const char *logfac2str __P((int));
 static const char *logpri2str __P((int));
@@ -157,6 +149,10 @@ dump_defaults()
                    (void) printf(cur->desc, cur->sd_un.ival);
                    putchar('\n');
                    break;
+               case T_FLOAT:
+                   (void) printf(cur->desc, cur->sd_un.fval);
+                   putchar('\n');
+                   break;
                case T_MODE:
                    (void) printf(cur->desc, cur->sd_un.mode);
                    putchar('\n');
@@ -232,132 +228,131 @@ set_default(var, val, op)
            break;
     }
     if (!cur->name) {
-       warnx("unknown defaults entry `%s' referenced near line %d",
-           var, sudolineno);
-       return(FALSE);
+       warningx("unknown defaults entry `%s'", var);
+       return FALSE;
     }
 
     switch (cur->type & T_MASK) {
        case T_LOGFAC:
            if (!store_syslogfac(val, cur, op)) {
                if (val)
-                   warnx("value `%s' is invalid for option `%s'", val, var);
+                   warningx("value `%s' is invalid for option `%s'", val, var);
                else
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-               return(FALSE);
+                   warningx("no value specified for `%s'", var);
+               return FALSE;
            }
            break;
        case T_LOGPRI:
            if (!store_syslogpri(val, cur, op)) {
                if (val)
-                   warnx("value `%s' is invalid for option `%s'", val, var);
+                   warningx("value `%s' is invalid for option `%s'", val, var);
                else
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-               return(FALSE);
+                   warningx("no value specified for `%s'", var);
+               return FALSE;
            }
            break;
        case T_STR:
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-                   return(FALSE);
+                   warningx("no value specified for `%s'", var);
+                   return FALSE;
                }
            }
            if (ISSET(cur->type, T_PATH) && val && *val != '/') {
-               warnx("values for `%s' must start with a '/'", var);
-               return(FALSE);
+               warningx("values for `%s' must start with a '/'", var);
+               return FALSE;
            }
            if (!store_str(val, cur, op)) {
-               warnx("value `%s' is invalid for option `%s'", val, var);
-               return(FALSE);
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
            }
            break;
        case T_INT:
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-                   return(FALSE);
+                   warningx("no value specified for `%s'", var);
+                   return FALSE;
                }
            }
            if (!store_int(val, cur, op)) {
-               warnx("value `%s' is invalid for option `%s'", val, var);
-               return(FALSE);
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
            }
            break;
        case T_UINT:
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-                   return(FALSE);
+                   warningx("no value specified for `%s'", var);
+                   return FALSE;
                }
            }
            if (!store_uint(val, cur, op)) {
-               warnx("value `%s' is invalid for option `%s'", val, var);
-               return(FALSE);
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
+           }
+           break;
+       case T_FLOAT:
+           if (!val) {
+               /* Check for bogus boolean usage or lack of a value. */
+               if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
+                   warningx("no value specified for `%s'", var);
+                   return FALSE;
+               }
+           }
+           if (!store_float(val, cur, op)) {
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
            }
            break;
        case T_MODE:
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-                   return(FALSE);
+                   warningx("no value specified for `%s'", var);
+                   return FALSE;
                }
            }
            if (!store_mode(val, cur, op)) {
-               warnx("value `%s' is invalid for option `%s'", val, var);
-               return(FALSE);
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
            }
            break;
        case T_FLAG:
            if (val) {
-               warnx("option `%s' does not take a value on line %d",
-                   var, sudolineno);
-               return(FALSE);
+               warningx("option `%s' does not take a value", var);
+               return FALSE;
            }
            cur->sd_un.flag = op;
-
-           /* Special action for I_FQDN.  Move to own switch if we get more */
-           if (num == I_FQDN && op)
-               set_fqdn();
            break;
        case T_LIST:
            if (!val) {
                /* Check for bogus boolean usage or lack of a value. */
                if (!ISSET(cur->type, T_BOOL) || op != FALSE) {
-                   warnx("no value specified for `%s' on line %d",
-                       var, sudolineno);
-                   return(FALSE);
+                   warningx("no value specified for `%s'", var);
+                   return FALSE;
                }
            }
            if (!store_list(val, cur, op)) {
-               warnx("value `%s' is invalid for option `%s'", val, var);
-               return(FALSE);
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
            }
            break;
        case T_TUPLE:
            if (!val && !ISSET(cur->type, T_BOOL)) {
-               warnx("no value specified for `%s' on line %d",
-                   var, sudolineno);
-               return(FALSE);
+               warningx("no value specified for `%s'", var);
+               return FALSE;
            }
            if (!store_tuple(val, cur, op)) {
-               warnx("value `%s' is invalid for option `%s'", val, var);
-               return(FALSE);
+               warningx("value `%s' is invalid for option `%s'", val, var);
+               return FALSE;
            }
            break;
     }
 
-    return(TRUE);
+    return TRUE;
 }
 
 /*
@@ -370,20 +365,20 @@ init_defaults()
     static int firsttime = 1;
     struct sudo_defs_types *def;
 
-    /* Free any strings that were set. */
+    /* Clear any old settings. */
     if (!firsttime) {
-       for (def = sudo_defs_table; def->name; def++)
+       for (def = sudo_defs_table; def->name; def++) {
            switch (def->type & T_MASK) {
                case T_STR:
-                   if (def->sd_un.str) {
-                       free(def->sd_un.str);
-                       def->sd_un.str = NULL;
-                   }
+                   efree(def->sd_un.str);
+                   def->sd_un.str = NULL;
                    break;
                case T_LIST:
                    list_op(NULL, 0, def, freeall);
                    break;
            }
+           zero_bytes(&def->sd_un, sizeof(def->sd_un));
+       }
     }
 
     /* First initialize the flags. */
@@ -405,7 +400,7 @@ init_defaults()
 #ifdef SEND_MAIL_WHEN_NOT_OK
     def_mail_no_perms = TRUE;
 #endif
-#ifdef USE_TTY_TICKETS
+#ifndef NO_TTY_TICKETS
     def_tty_tickets = TRUE;
 #endif
 #ifndef NO_LECTURE
@@ -438,7 +433,17 @@ init_defaults()
 #ifdef ENV_EDITOR
     def_env_editor = TRUE;
 #endif
+#ifdef UMASK_OVERRIDE
+    def_umask_override = TRUE;
+#endif
+#ifdef _PATH_SUDO_ASKPASS
+    def_askpass = estrdup(_PATH_SUDO_ASKPASS);
+#endif
+    def_iolog_dir = estrdup(_PATH_SUDO_IO_LOGDIR);
+    def_sudoers_locale = estrdup("C");
+    def_env_reset = ENV_RESET;
     def_set_logname = TRUE;
+    def_closefrom = STDERR_FILENO + 1;
 
     /* Syslog options need special care since they both strings and ints */
 #if (LOGGING & SLOG_SYSLOG)
@@ -463,6 +468,9 @@ init_defaults()
     def_timestamp_timeout = TIMEOUT;
     def_passwd_timeout = PASSWORD_TIMEOUT;
     def_passwd_tries = TRIES_FOR_PASSWORD;
+#ifdef HAVE_ZLIB_H
+    def_compress_io = TRUE;
+#endif
 
     /* Now do the strings */
     def_mailto = estrdup(MAILTO);
@@ -480,6 +488,9 @@ init_defaults()
 #endif
 #ifdef EXEMPTGROUP
     def_exempt_group = estrdup(EXEMPTGROUP);
+#endif
+#ifdef SECURE_PATH
+    def_secure_path = estrdup(SECURE_PATH);
 #endif
     def_editor = estrdup(EDITOR);
 #ifdef _PATH_SUDO_NOEXEC
@@ -489,17 +500,56 @@ init_defaults()
     /* Finally do the lists (currently just environment tables). */
     init_envtables();
 
-    /*
-     * The following depend on the above values.
-     * We use a pointer to the string so that if its
-     * value changes we get the change.
-     */
-    if (user_runas == NULL)
-       user_runas = &def_runas_default;
-
     firsttime = 0;
 }
 
+/*
+ * Update the defaults based on what was set by sudoers.
+ * Pass in an OR'd list of which default types to update.
+ */
+int
+update_defaults(what)
+    int what;
+{
+    struct defaults *def;
+    int rc = TRUE;
+
+    tq_foreach_fwd(&defaults, def) {
+       switch (def->type) {
+           case DEFAULTS:
+               if (ISSET(what, SETDEF_GENERIC) &&
+                   !set_default(def->var, def->val, def->op))
+                   rc = FALSE;
+               break;
+           case DEFAULTS_USER:
+               if (ISSET(what, SETDEF_USER) &&
+                   userlist_matches(sudo_user.pw, &def->binding) == ALLOW &&
+                   !set_default(def->var, def->val, def->op))
+                   rc = FALSE;
+               break;
+           case DEFAULTS_RUNAS:
+               if (ISSET(what, SETDEF_RUNAS) &&
+                   runaslist_matches(&def->binding, NULL) == ALLOW &&
+                   !set_default(def->var, def->val, def->op))
+                   rc = FALSE;
+               break;
+           case DEFAULTS_HOST:
+               if (ISSET(what, SETDEF_HOST) &&
+                   hostlist_matches(&def->binding) == ALLOW &&
+                   !set_default(def->var, def->val, def->op))
+                   rc = FALSE;
+               break;
+           case DEFAULTS_CMND:
+               if (ISSET(what, SETDEF_CMND) &&
+                   cmndlist_matches(&def->binding) == ALLOW &&
+                   !set_default(def->var, def->val, def->op))
+                   rc = FALSE;
+               break;
+       }
+    }
+    return rc;
+}
+
 static int
 store_int(val, def, op)
     char *val;
@@ -514,13 +564,13 @@ store_int(val, def, op)
     } else {
        l = strtol(val, &endp, 10);
        if (*endp != '\0')
-           return(FALSE);
+           return FALSE;
        /* XXX - should check against INT_MAX */
-       def->sd_un.ival = (unsigned int)l;
+       def->sd_un.ival = (int)l;
     }
     if (def->callback)
-       return(def->callback(val));
-    return(TRUE);
+       return def->callback(val);
+    return TRUE;
 }
 
 static int
@@ -537,13 +587,36 @@ store_uint(val, def, op)
     } else {
        l = strtol(val, &endp, 10);
        if (*endp != '\0' || l < 0)
-           return(FALSE);
+           return FALSE;
        /* XXX - should check against INT_MAX */
        def->sd_un.ival = (unsigned int)l;
     }
     if (def->callback)
-       return(def->callback(val));
-    return(TRUE);
+       return def->callback(val);
+    return TRUE;
+}
+
+static int
+store_float(val, def, op)
+    char *val;
+    struct sudo_defs_types *def;
+    int op;
+{
+    char *endp;
+    double d;
+
+    if (op == FALSE) {
+       def->sd_un.fval = 0.0;
+    } else {
+       d = strtod(val, &endp);
+       if (*endp != '\0')
+           return FALSE;
+       /* XXX - should check against HUGE_VAL */
+       def->sd_un.fval = d;
+    }
+    if (def->callback)
+       return def->callback(val);
+    return TRUE;
 }
 
 static int
@@ -571,11 +644,11 @@ store_tuple(val, def, op)
            }
        }
        if (v->sval == NULL)
-           return(FALSE);
+           return FALSE;
     }
     if (def->callback)
-       return(def->callback(val));
-    return(TRUE);
+       return def->callback(val);
+    return TRUE;
 }
 
 static int
@@ -585,15 +658,14 @@ store_str(val, def, op)
     int op;
 {
 
-    if (def->sd_un.str)
-       free(def->sd_un.str);
+    efree(def->sd_un.str);
     if (op == FALSE)
        def->sd_un.str = NULL;
     else
        def->sd_un.str = estrdup(val);
     if (def->callback)
-       return(def->callback(val));
-    return(TRUE);
+       return def->callback(val);
+    return TRUE;
 }
 
 static int
@@ -613,18 +685,18 @@ store_list(str, def, op)
        end = str;
        do {
            /* Remove leading blanks, if nothing but blanks we are done. */
-           for (start = end; isblank(*start); start++)
+           for (start = end; isblank((unsigned char)*start); start++)
                ;
            if (*start == '\0')
                break;
 
            /* Find end position and perform operation. */
-           for (end = start; *end && !isblank(*end); end++)
+           for (end = start; *end && !isblank((unsigned char)*end); end++)
                ;
            list_op(start, end - start, def, op == '-' ? delete : add);
        } while (*end++ != '\0');
     }
-    return(TRUE);
+    return TRUE;
 }
 
 static int
@@ -637,21 +709,21 @@ store_syslogfac(val, def, op)
 
     if (op == FALSE) {
        def->sd_un.ival = FALSE;
-       return(TRUE);
+       return TRUE;
     }
 #ifdef LOG_NFACILITIES
     if (!val)
-       return(FALSE);
+       return FALSE;
     for (fac = facilities; fac->name && strcmp(val, fac->name); fac++)
        ;
     if (fac->name == NULL)
-       return(FALSE);                          /* not found */
+       return FALSE;                           /* not found */
 
     def->sd_un.ival = fac->num;
 #else
     def->sd_un.ival = -1;
 #endif /* LOG_NFACILITIES */
-    return(TRUE);
+    return TRUE;
 }
 
 static const char *
@@ -663,9 +735,9 @@ logfac2str(n)
 
     for (fac = facilities; fac->name && fac->num != n; fac++)
        ;
-    return (fac->name);
+    return fac->name;
 #else
-    return ("default");
+    return "default";
 #endif /* LOG_NFACILITIES */
 }
 
@@ -678,15 +750,15 @@ store_syslogpri(val, def, op)
     struct strmap *pri;
 
     if (op == FALSE || !val)
-       return(FALSE);
+       return FALSE;
 
     for (pri = priorities; pri->name && strcmp(val, pri->name); pri++)
        ;
     if (pri->name == NULL)
-       return(FALSE);                          /* not found */
+       return FALSE;                           /* not found */
 
     def->sd_un.ival = pri->num;
-    return(TRUE);
+    return TRUE;
 }
 
 static const char *
@@ -697,7 +769,7 @@ logpri2str(n)
 
     for (pri = priorities; pri->name && pri->num != n; pri++)
        ;
-    return (pri->name);
+    return pri->name;
 }
 
 static int
@@ -714,12 +786,12 @@ store_mode(val, def, op)
     } else {
        l = strtol(val, &endp, 8);
        if (*endp != '\0' || l < 0 || l > 0777)
-           return(FALSE);
+           return FALSE;
        def->sd_un.mode = (mode_t)l;
     }
     if (def->callback)
-       return(def->callback(val));
-    return(TRUE);
+       return def->callback(val);
+    return TRUE;
 }
 
 static void
@@ -735,8 +807,8 @@ list_op(val, len, def, op)
        for (cur = def->sd_un.list; cur; ) {
            tmp = cur;
            cur = tmp->next;
-           free(tmp->value);
-           free(tmp);
+           efree(tmp->value);
+           efree(tmp);
        }
        def->sd_un.list = NULL;
        return;
@@ -753,8 +825,8 @@ list_op(val, len, def, op)
                prev->next = cur->next;
            else
                def->sd_un.list = cur->next;
-           free(cur->value);
-           free(cur);
+           efree(cur->value);
+           efree(cur);
            break;
        }
     }