Annotation of embedaddon/sudo/plugins/sudoers/group_plugin.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.3 ! misho       2:  * Copyright (c) 2010-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 <sys/time.h>
                     22: #include <stdio.h>
                     23: #ifdef STDC_HEADERS
                     24: # include <stdlib.h>
                     25: # include <stddef.h>
                     26: #else
                     27: # ifdef HAVE_STDLIB_H
                     28: #  include <stdlib.h>
                     29: # endif
                     30: #endif /* STDC_HEADERS */
                     31: #ifdef HAVE_STRING_H
                     32: # include <string.h>
                     33: #endif /* HAVE_STRING_H */
                     34: #ifdef HAVE_STRINGS_H
                     35: # include <strings.h>
                     36: #endif /* HAVE_STRINGS_H */
                     37: #ifdef HAVE_UNISTD_H
                     38: # include <unistd.h>
                     39: #endif /* HAVE_UNISTD_H */
                     40: #if TIME_WITH_SYS_TIME
                     41: # include <time.h>
                     42: #endif
                     43: #ifdef HAVE_DLOPEN
                     44: # include <dlfcn.h>
                     45: #else
                     46: # include "compat/dlfcn.h"
                     47: #endif
                     48: #include <ctype.h>
                     49: #include <errno.h>
                     50: #include <pwd.h>
                     51: 
                     52: #include "sudoers.h"
                     53: 
                     54: #ifndef RTLD_GLOBAL
                     55: # define RTLD_GLOBAL   0
                     56: #endif
                     57: 
1.1.1.2   misho      58: #if defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD)
                     59: 
1.1       misho      60: static void *group_handle;
                     61: static struct sudoers_group_plugin *group_plugin;
                     62: 
                     63: /*
                     64:  * Load the specified plugin and run its init function.
                     65:  * Returns -1 if unable to open the plugin, else it returns
                     66:  * the value from the plugin's init function.
                     67:  */
                     68: int
                     69: group_plugin_load(char *plugin_info)
                     70: {
                     71:     struct stat sb;
                     72:     char *args, path[PATH_MAX];
                     73:     char **argv = NULL;
                     74:     int len, rc = -1;
1.1.1.2   misho      75:     debug_decl(group_plugin_load, SUDO_DEBUG_UTIL)
1.1       misho      76: 
                     77:     /*
                     78:      * Fill in .so path and split out args (if any).
                     79:      */
                     80:     if ((args = strpbrk(plugin_info, " \t")) != NULL) {
                     81:        len = snprintf(path, sizeof(path), "%s%.*s",
                     82:            (*plugin_info != '/') ? _PATH_SUDO_PLUGIN_DIR : "",
                     83:            (int)(args - plugin_info), plugin_info);
                     84:        args++;
                     85:     } else {
                     86:        len = snprintf(path, sizeof(path), "%s%s",
                     87:            (*plugin_info != '/') ? _PATH_SUDO_PLUGIN_DIR : "", plugin_info);
                     88:     }
                     89:     if (len <= 0 || len >= sizeof(path)) {
1.1.1.3 ! misho      90:        errno = ENAMETOOLONG;
        !            91:        warning("%s%s",
        !            92:            (*plugin_info != '/') ? _PATH_SUDO_PLUGIN_DIR : "", plugin_info);
1.1       misho      93:        goto done;
                     94:     }
                     95: 
                     96:     /* Sanity check plugin path. */
                     97:     if (stat(path, &sb) != 0) {
                     98:        warning("%s", path);
                     99:        goto done;
                    100:     }
                    101:     if (sb.st_uid != ROOT_UID) {
                    102:        warningx(_("%s must be owned by uid %d"), path, ROOT_UID);
                    103:        goto done;
                    104:     }
                    105:     if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
                    106:        warningx(_("%s must only be writable by owner"), path);
                    107:        goto done;
                    108:     }
                    109: 
                    110:     /* Open plugin and map in symbol. */
                    111:     group_handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
                    112:     if (!group_handle) {
                    113:        warningx(_("unable to dlopen %s: %s"), path, dlerror());
                    114:        goto done;
                    115:     }
                    116:     group_plugin = dlsym(group_handle, "group_plugin");
                    117:     if (group_plugin == NULL) {
                    118:        warningx(_("unable to find symbol \"group_plugin\" in %s"), path);
                    119:        goto done;
                    120:     }
                    121: 
                    122:     if (GROUP_API_VERSION_GET_MAJOR(group_plugin->version) != GROUP_API_VERSION_MAJOR) {
                    123:        warningx(_("%s: incompatible group plugin major version %d, expected %d"),
                    124:            path, GROUP_API_VERSION_GET_MAJOR(group_plugin->version),
                    125:            GROUP_API_VERSION_MAJOR);
                    126:        goto done;
                    127:     }
                    128: 
                    129:     /*
                    130:      * Split args into a vector if specified.
                    131:      */
                    132:     if (args != NULL) {
1.1.1.2   misho     133:        int ac = 0;
                    134:        bool wasblank = true;
1.1       misho     135:        char *cp;
                    136: 
                    137:         for (cp = args; *cp != '\0'; cp++) {
                    138:             if (isblank((unsigned char)*cp)) {
1.1.1.2   misho     139:                 wasblank = true;
1.1       misho     140:             } else if (wasblank) {
1.1.1.2   misho     141:                 wasblank = false;
1.1       misho     142:                 ac++;
                    143:             }
                    144:         }
                    145:        if (ac != 0)    {
                    146:            argv = emalloc2(ac, sizeof(char *));
                    147:            ac = 0;
                    148:            for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
                    149:                argv[ac++] = cp;
                    150:        }
                    151:     }
                    152: 
                    153:     rc = (group_plugin->init)(GROUP_API_VERSION, sudo_printf, argv);
                    154: 
                    155: done:
                    156:     efree(argv);
                    157: 
1.1.1.2   misho     158:     if (rc != true) {
1.1       misho     159:        if (group_handle != NULL) {
                    160:            dlclose(group_handle);
                    161:            group_handle = NULL;
                    162:            group_plugin = NULL;
                    163:        }
                    164:     }
                    165: 
1.1.1.2   misho     166:     debug_return_bool(rc);
1.1       misho     167: }
                    168: 
                    169: void
                    170: group_plugin_unload(void)
                    171: {
1.1.1.2   misho     172:     debug_decl(group_plugin_unload, SUDO_DEBUG_UTIL)
                    173: 
1.1       misho     174:     if (group_plugin != NULL) {
                    175:        (group_plugin->cleanup)();
                    176:        group_plugin = NULL;
                    177:     }
                    178:     if (group_handle != NULL) {
                    179:        dlclose(group_handle);
                    180:        group_handle = NULL;
                    181:     }
1.1.1.2   misho     182:     debug_return;
1.1       misho     183: }
                    184: 
                    185: int
                    186: group_plugin_query(const char *user, const char *group,
                    187:     const struct passwd *pwd)
                    188: {
1.1.1.2   misho     189:     debug_decl(group_plugin_query, SUDO_DEBUG_UTIL)
                    190: 
1.1       misho     191:     if (group_plugin == NULL)
1.1.1.2   misho     192:        debug_return_bool(false);
                    193:     debug_return_bool((group_plugin->query)(user, group, pwd));
1.1       misho     194: }
                    195: 
                    196: #else /* !HAVE_DLOPEN && !HAVE_SHL_LOAD */
                    197: 
                    198: /*
                    199:  * No loadable shared object support.
                    200:  */
                    201: 
                    202: int
                    203: group_plugin_load(char *plugin_info)
                    204: {
1.1.1.2   misho     205:     debug_decl(group_plugin_load, SUDO_DEBUG_UTIL)
                    206:     debug_return_bool(false);
1.1       misho     207: }
                    208: 
                    209: void
                    210: group_plugin_unload(void)
                    211: {
1.1.1.2   misho     212:     debug_decl(group_plugin_unload, SUDO_DEBUG_UTIL)
                    213:     debug_return;
1.1       misho     214: }
                    215: 
                    216: int
                    217: group_plugin_query(const char *user, const char *group,
                    218:     const struct passwd *pwd)
                    219: {
1.1.1.2   misho     220:     debug_decl(group_plugin_query, SUDO_DEBUG_UTIL)
                    221:     debug_return_bool(false);
1.1       misho     222: }
                    223: 
                    224: #endif /* HAVE_DLOPEN || HAVE_SHL_LOAD */

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