Annotation of embedaddon/sudo/common/sudo_conf.c, revision 1.1.1.4

1.1       misho       1: /*
1.1.1.3   misho       2:  * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1       misho       3:  *
                      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.
                      7:  *
                      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.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: #include <sys/stat.h>
                     21: #include <stdio.h>
                     22: #ifdef STDC_HEADERS
                     23: # include <stdlib.h>
                     24: # include <stddef.h>
                     25: #else
                     26: # ifdef HAVE_STDLIB_H
                     27: #  include <stdlib.h>
                     28: # endif
                     29: #endif /* STDC_HEADERS */
                     30: #ifdef HAVE_STDBOOL_H
                     31: # include <stdbool.h>
                     32: #else
                     33: # include "compat/stdbool.h"
                     34: #endif
                     35: #ifdef HAVE_STRING_H
                     36: # include <string.h>
                     37: #endif /* HAVE_STRING_H */
                     38: #ifdef HAVE_STRINGS_H
                     39: # include <strings.h>
                     40: #endif /* HAVE_STRINGS_H */
                     41: #ifdef HAVE_UNISTD_H
                     42: # include <unistd.h>
                     43: #endif /* HAVE_UNISTD_H */
                     44: #include <ctype.h>
                     45: #include <errno.h>
1.1.1.3   misho      46: #include <limits.h>
1.1       misho      47: 
                     48: #define SUDO_ERROR_WRAP        0
                     49: 
                     50: #include "missing.h"
                     51: #include "alloc.h"
1.1.1.4 ! misho      52: #include "fatal.h"
1.1       misho      53: #include "fileops.h"
                     54: #include "pathnames.h"
                     55: #include "sudo_plugin.h"
                     56: #include "sudo_conf.h"
                     57: #include "sudo_debug.h"
                     58: #include "secure_path.h"
1.1.1.3   misho      59: 
                     60: #define DEFAULT_TEXT_DOMAIN    "sudo"
1.1       misho      61: #include "gettext.h"
                     62: 
                     63: #ifdef __TANDEM
                     64: # define ROOT_UID      65535
                     65: #else
                     66: # define ROOT_UID      0
                     67: #endif
                     68: 
                     69: extern bool atobool(const char *str); /* atobool.c */
                     70: 
                     71: struct sudo_conf_table {
                     72:     const char *name;
                     73:     unsigned int namelen;
1.1.1.3   misho      74:     void (*setter)(const char *entry, const char *conf_file);
1.1       misho      75: };
                     76: 
                     77: struct sudo_conf_paths {
                     78:     const char *pname;
                     79:     unsigned int pnamelen;
                     80:     const char *pval;
                     81: };
                     82: 
1.1.1.3   misho      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);
                     90: 
                     91: static unsigned int conf_lineno;
1.1       misho      92: 
                     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 },
                     98:     { NULL }
                     99: };
                    100: 
1.1.1.3   misho     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 },
                    105:     { NULL }
                    106: };
                    107: 
1.1       misho     108: static struct sudo_conf_data {
                    109:     bool disable_coredump;
1.1.1.3   misho     110:     int group_source;
                    111:     int max_groups;
1.1       misho     112:     const char *debug_flags;
1.1.1.4 ! misho     113:     struct sudo_conf_paths paths[5];
1.1       misho     114:     struct plugin_info_list plugins;
                    115: } sudo_conf_data = {
                    116:     true,
1.1.1.3   misho     117:     GROUP_SOURCE_ADAPTIVE,
                    118:     -1,
1.1       misho     119:     NULL,
                    120:     {
                    121: #define SUDO_CONF_ASKPASS_IDX  0
                    122:        { "askpass", sizeof("askpass") - 1, _PATH_SUDO_ASKPASS },
1.1.1.3   misho     123: #define SUDO_CONF_SESH_IDX     1
                    124:        { "sesh", sizeof("sesh") - 1, _PATH_SUDO_SESH },
1.1       misho     125: #ifdef _PATH_SUDO_NOEXEC
1.1.1.3   misho     126: #define SUDO_CONF_NOEXEC_IDX   2
1.1       misho     127:        { "noexec", sizeof("noexec") - 1, _PATH_SUDO_NOEXEC },
                    128: #endif
1.1.1.4 ! misho     129: #ifdef _PATH_SUDO_PLUGIN_DIR
        !           130: #define SUDO_CONF_PLUGIN_IDX   3
        !           131:        { "plugin", sizeof("plugin") - 1, _PATH_SUDO_PLUGIN_DIR },
        !           132: #endif
1.1       misho     133:        { NULL }
                    134:     }
                    135: };
                    136: 
                    137: /*
                    138:  * "Set variable_name value"
                    139:  */
1.1.1.3   misho     140: static void
                    141: set_variable(const char *entry, const char *conf_file)
1.1       misho     142: {
1.1.1.3   misho     143:     struct sudo_conf_table *var;
                    144: 
                    145:     for (var = sudo_conf_table_vars; var->name != NULL; var++) {
                    146:        if (strncmp(entry, var->name, var->namelen) == 0 &&
                    147:            isblank((unsigned char)entry[var->namelen])) {
                    148:            entry += var->namelen + 1;
                    149:            while (isblank((unsigned char)*entry))
                    150:                entry++;
                    151:            var->setter(entry, conf_file);
                    152:            break;
                    153:        }
                    154:     }
                    155: }
                    156: 
                    157: static void
                    158: set_var_disable_coredump(const char *entry, const char *conf_file)
                    159: {
                    160:     int val = atobool(entry);
                    161: 
                    162:     if (val != -1)
                    163:        sudo_conf_data.disable_coredump = val;
                    164: }
                    165: 
                    166: static void
                    167: set_var_group_source(const char *entry, const char *conf_file)
                    168: {
                    169:     if (strcasecmp(entry, "adaptive") == 0) {
                    170:        sudo_conf_data.group_source = GROUP_SOURCE_ADAPTIVE;
                    171:     } else if (strcasecmp(entry, "static") == 0) {
                    172:        sudo_conf_data.group_source = GROUP_SOURCE_STATIC;
                    173:     } else if (strcasecmp(entry, "dynamic") == 0) {
                    174:        sudo_conf_data.group_source = GROUP_SOURCE_DYNAMIC;
                    175:     } else {
                    176:        warningx(_("unsupported group source `%s' in %s, line %d"), entry,
                    177:            conf_file, conf_lineno);
                    178:     }
                    179: }
                    180: 
                    181: static void
                    182: set_var_max_groups(const char *entry, const char *conf_file)
                    183: {
                    184:     long lval;
                    185:     char *ep;
                    186: 
                    187:     lval = strtol(entry, &ep, 10);
1.1.1.4 ! misho     188:     if (*entry == '\0' || *ep != '\0' || lval <= 0 || lval > INT_MAX ||
1.1.1.3   misho     189:        (errno == ERANGE && lval == LONG_MAX)) {
                    190:        warningx(_("invalid max groups `%s' in %s, line %d"), entry,
                    191:                    conf_file, conf_lineno);
                    192:     } else {
                    193:        sudo_conf_data.max_groups = (int)lval;
1.1       misho     194:     }
                    195: }
                    196: 
                    197: /*
                    198:  * "Debug progname debug_file debug_flags"
                    199:  */
1.1.1.3   misho     200: static void
                    201: set_debug(const char *entry, const char *conf_file)
1.1       misho     202: {
                    203:     size_t filelen, proglen;
                    204:     const char *progname;
                    205:     char *debug_file, *debug_flags;
                    206: 
                    207:     /* Is this debug setting for me? */
                    208:     progname = getprogname();
                    209:     if (strcmp(progname, "sudoedit") == 0)
                    210:        progname = "sudo";
                    211:     proglen = strlen(progname);
                    212:     if (strncmp(entry, progname, proglen) != 0 ||
                    213:        !isblank((unsigned char)entry[proglen]))
1.1.1.3   misho     214:        return;
1.1       misho     215:     entry += proglen + 1;
                    216:     while (isblank((unsigned char)*entry))
                    217:        entry++;
                    218: 
                    219:     debug_flags = strpbrk(entry, " \t");
                    220:     if (debug_flags == NULL)
1.1.1.3   misho     221:        return;
1.1       misho     222:     filelen = (size_t)(debug_flags - entry);
                    223:     while (isblank((unsigned char)*debug_flags))
                    224:        debug_flags++;
                    225: 
                    226:     /* Set debug file and parse the flags (init debug as soon as possible). */
                    227:     debug_file = estrndup(entry, filelen);
                    228:     debug_flags = estrdup(debug_flags);
                    229:     sudo_debug_init(debug_file, debug_flags);
                    230:     efree(debug_file);
                    231: 
                    232:     sudo_conf_data.debug_flags = debug_flags;
                    233: }
                    234: 
1.1.1.3   misho     235: static void
                    236: set_path(const char *entry, const char *conf_file)
1.1       misho     237: {
                    238:     const char *name, *path;
                    239:     struct sudo_conf_paths *cur;
                    240: 
                    241:     /* Parse Path line */
                    242:     name = entry;
                    243:     path = strpbrk(entry, " \t");
                    244:     if (path == NULL)
1.1.1.3   misho     245:        return;
1.1       misho     246:     while (isblank((unsigned char)*path))
                    247:        path++;
                    248: 
                    249:     /* Match supported paths, ignore the rest. */
                    250:     for (cur = sudo_conf_data.paths; cur->pname != NULL; cur++) {
                    251:        if (strncasecmp(name, cur->pname, cur->pnamelen) == 0 &&
                    252:            isblank((unsigned char)name[cur->pnamelen])) {
                    253:            cur->pval = estrdup(path);
                    254:            break;
                    255:        }
                    256:     }
                    257: }
                    258: 
1.1.1.3   misho     259: static void
                    260: set_plugin(const char *entry, const char *conf_file)
1.1       misho     261: {
                    262:     struct plugin_info *info;
                    263:     const char *name, *path, *cp, *ep;
                    264:     char **options = NULL;
                    265:     size_t namelen, pathlen;
                    266:     unsigned int nopts;
                    267: 
                    268:     /* Parse Plugin line */
                    269:     name = entry;
                    270:     path = strpbrk(entry, " \t");
                    271:     if (path == NULL)
1.1.1.3   misho     272:        return;
1.1       misho     273:     namelen = (size_t)(path - name);
                    274:     while (isblank((unsigned char)*path))
                    275:        path++;
                    276:     if ((cp = strpbrk(path, " \t")) != NULL) {
                    277:        /* Convert any options to an array. */
                    278:        pathlen = (size_t)(cp - path);
                    279:        while (isblank((unsigned char)*cp))
                    280:            cp++;
                    281:        /* Count number of options and allocate array. */
                    282:        for (ep = cp, nopts = 1; (ep = strpbrk(ep, " \t")) != NULL; nopts++) {
                    283:            while (isblank((unsigned char)*ep))
                    284:                ep++;
                    285:        }
                    286:        options = emalloc2(nopts + 1, sizeof(*options));
                    287:        /* Fill in options array, there is at least one element. */
                    288:        for (nopts = 0; (ep = strpbrk(cp, " \t")) != NULL; ) {
                    289:            options[nopts++] = estrndup(cp, (size_t)(ep - cp));
                    290:            while (isblank((unsigned char)*ep))
                    291:                ep++;
                    292:            cp = ep;
                    293:        }
                    294:        options[nopts++] = estrdup(cp);
                    295:        options[nopts] = NULL;
                    296:     } else {
                    297:        /* No extra options. */
                    298:        pathlen = strlen(path);
                    299:     }
                    300: 
                    301:     info = ecalloc(1, sizeof(*info));
                    302:     info->symbol_name = estrndup(name, namelen);
                    303:     info->path = estrndup(path, pathlen);
                    304:     info->options = options;
                    305:     info->prev = info;
                    306:     /* info->next = NULL; */
1.1.1.3   misho     307:     info->lineno = conf_lineno;
1.1       misho     308:     tq_append(&sudo_conf_data.plugins, info);
                    309: }
                    310: 
                    311: const char *
                    312: sudo_conf_askpass_path(void)
                    313: {
                    314:     return sudo_conf_data.paths[SUDO_CONF_ASKPASS_IDX].pval;
                    315: }
                    316: 
1.1.1.3   misho     317: const char *
                    318: sudo_conf_sesh_path(void)
                    319: {
                    320:     return sudo_conf_data.paths[SUDO_CONF_SESH_IDX].pval;
                    321: }
                    322: 
1.1       misho     323: #ifdef _PATH_SUDO_NOEXEC
                    324: const char *
                    325: sudo_conf_noexec_path(void)
                    326: {
                    327:     return sudo_conf_data.paths[SUDO_CONF_NOEXEC_IDX].pval;
                    328: }
                    329: #endif
                    330: 
1.1.1.4 ! misho     331: #ifdef _PATH_SUDO_PLUGIN_DIR
        !           332: const char *
        !           333: sudo_conf_plugin_dir_path(void)
        !           334: {
        !           335:     return sudo_conf_data.paths[SUDO_CONF_PLUGIN_IDX].pval;
        !           336: }
        !           337: #endif
        !           338: 
1.1       misho     339: const char *
                    340: sudo_conf_debug_flags(void)
                    341: {
                    342:     return sudo_conf_data.debug_flags;
                    343: }
                    344: 
1.1.1.3   misho     345: int
                    346: sudo_conf_group_source(void)
                    347: {
                    348:     return sudo_conf_data.group_source;
                    349: }
                    350: 
                    351: int
                    352: sudo_conf_max_groups(void)
                    353: {
                    354:     return sudo_conf_data.max_groups;
                    355: }
                    356: 
1.1       misho     357: struct plugin_info_list *
                    358: sudo_conf_plugins(void)
                    359: {
                    360:     return &sudo_conf_data.plugins;
                    361: }
                    362: 
                    363: bool
                    364: sudo_conf_disable_coredump(void)
                    365: {
                    366:     return sudo_conf_data.disable_coredump;
                    367: }
                    368: 
                    369: /*
                    370:  * Reads in /etc/sudo.conf and populates sudo_conf_data.
                    371:  */
                    372: void
1.1.1.3   misho     373: sudo_conf_read(const char *conf_file)
1.1       misho     374: {
                    375:     struct sudo_conf_table *cur;
                    376:     struct stat sb;
                    377:     FILE *fp;
1.1.1.3   misho     378:     char *cp, *line = NULL;
                    379:     char *prev_locale = estrdup(setlocale(LC_ALL, NULL));
                    380:     size_t linesize = 0;
                    381: 
                    382:     /* Parse sudo.conf in the "C" locale. */
                    383:     if (prev_locale[0] != 'C' || prev_locale[1] != '\0')
                    384:         setlocale(LC_ALL, "C");
                    385: 
                    386:     if (conf_file == NULL) {
                    387:        conf_file = _PATH_SUDO_CONF;
                    388:        switch (sudo_secure_file(conf_file, ROOT_UID, -1, &sb)) {
                    389:            case SUDO_PATH_SECURE:
                    390:                break;
                    391:            case SUDO_PATH_MISSING:
                    392:                /* Root should always be able to read sudo.conf. */
                    393:                if (errno != ENOENT && geteuid() == ROOT_UID)
                    394:                    warning(_("unable to stat %s"), conf_file);
                    395:                goto done;
                    396:            case SUDO_PATH_BAD_TYPE:
                    397:                warningx(_("%s is not a regular file"), conf_file);
                    398:                goto done;
                    399:            case SUDO_PATH_WRONG_OWNER:
                    400:                warningx(_("%s is owned by uid %u, should be %u"),
                    401:                    conf_file, (unsigned int) sb.st_uid, ROOT_UID);
                    402:                goto done;
                    403:            case SUDO_PATH_WORLD_WRITABLE:
                    404:                warningx(_("%s is world writable"), conf_file);
                    405:                goto done;
                    406:            case SUDO_PATH_GROUP_WRITABLE:
                    407:                warningx(_("%s is group writable"), conf_file);
                    408:                goto done;
                    409:            default:
                    410:                /* NOTREACHED */
                    411:                goto done;
                    412:        }
1.1       misho     413:     }
                    414: 
1.1.1.3   misho     415:     if ((fp = fopen(conf_file, "r")) == NULL) {
1.1       misho     416:        if (errno != ENOENT && geteuid() == ROOT_UID)
1.1.1.3   misho     417:            warning(_("unable to open %s"), conf_file);
1.1       misho     418:        goto done;
                    419:     }
                    420: 
1.1.1.3   misho     421:     conf_lineno = 0;
                    422:     while (sudo_parseln(&line, &linesize, &conf_lineno, fp) != -1) {
                    423:        if (*(cp = line) == '\0')
                    424:            continue;           /* empty line or comment */
1.1       misho     425: 
                    426:        for (cur = sudo_conf_table; cur->name != NULL; cur++) {
                    427:            if (strncasecmp(cp, cur->name, cur->namelen) == 0 &&
                    428:                isblank((unsigned char)cp[cur->namelen])) {
                    429:                cp += cur->namelen;
                    430:                while (isblank((unsigned char)*cp))
                    431:                    cp++;
1.1.1.3   misho     432:                cur->setter(cp, conf_file);
                    433:                break;
1.1       misho     434:            }
                    435:        }
                    436:     }
                    437:     fclose(fp);
1.1.1.3   misho     438:     free(line);
1.1       misho     439: done:
1.1.1.3   misho     440:     /* Restore locale if needed. */
                    441:     if (prev_locale[0] != 'C' || prev_locale[1] != '\0')
                    442:         setlocale(LC_ALL, prev_locale);
                    443:     efree(prev_locale);
1.1       misho     444: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>