Annotation of embedaddon/sudo/plugins/sample_group/plugin_test.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2010-2011 Todd C. Miller <Todd.Miller@courtesan.com>
        !             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 <sys/types.h>
        !            18: #include <sys/param.h>
        !            19: 
        !            20: #include <stdio.h>
        !            21: #include <stdlib.h>
        !            22: #include <stddef.h>
        !            23: #include <stdarg.h>
        !            24: #include <string.h>
        !            25: #include <unistd.h>
        !            26: #include <ctype.h>
        !            27: #include <dlfcn.h>
        !            28: #include <errno.h>
        !            29: #include <limits.h>
        !            30: #include <pwd.h>
        !            31: 
        !            32: #include "sudo_plugin.h"
        !            33: 
        !            34: /*
        !            35:  * Simple driver to test sudoer group plugins.
        !            36:  * usage: plugin_test [-p "plugin.so plugin_args ..."] user:group ...
        !            37:  */
        !            38: 
        !            39: static void *group_handle;
        !            40: static struct sudoers_group_plugin *group_plugin;
        !            41: 
        !            42: static int
        !            43: plugin_printf(int msg_type, const char *fmt, ...)
        !            44: {
        !            45:     va_list ap;
        !            46:     FILE *fp;
        !            47:            
        !            48:     switch (msg_type) {
        !            49:     case SUDO_CONV_INFO_MSG:
        !            50:        fp = stdout;
        !            51:        break;
        !            52:     case SUDO_CONV_ERROR_MSG:
        !            53:        fp = stderr;
        !            54:        break;
        !            55:     default:
        !            56:        errno = EINVAL;
        !            57:        return -1;
        !            58:     }
        !            59: 
        !            60:     va_start(ap, fmt);
        !            61:     vfprintf(fp, fmt, ap);
        !            62:     va_end(ap);
        !            63: 
        !            64:     return 0;
        !            65: }
        !            66: 
        !            67: /*
        !            68:  * Load the specified plugin and run its init function.
        !            69:  * Returns -1 if unable to open the plugin, else it returns
        !            70:  * the value from the plugin's init function.
        !            71:  */
        !            72: static int
        !            73: group_plugin_load(char *plugin_info)
        !            74: {
        !            75:     char *args, path[PATH_MAX], savedch;
        !            76:     char **argv = NULL;
        !            77:     int rc;
        !            78: 
        !            79:     /*
        !            80:      * Fill in .so path and split out args (if any).
        !            81:      */
        !            82:     if ((args = strpbrk(plugin_info, " \t")) != NULL) {
        !            83:        savedch = *args;
        !            84:        *args = '\0';
        !            85:     }
        !            86:     strncpy(path, plugin_info, sizeof(path) - 1);
        !            87:     path[sizeof(path) - 1] = '\0';
        !            88:     if (args != NULL)
        !            89:        *args++ = savedch;
        !            90: 
        !            91:     /* Open plugin and map in symbol. */
        !            92:     group_handle = dlopen(path, RTLD_LAZY);
        !            93:     if (!group_handle) {
        !            94:        fprintf(stderr, "unable to dlopen %s: %s\n", path, dlerror());
        !            95:        return -1;
        !            96:     }
        !            97:     group_plugin = dlsym(group_handle, "group_plugin");
        !            98:     if (group_plugin == NULL) {
        !            99:        fprintf(stderr, "unable to find symbol \"group_plugin\" in %s\n", path);
        !           100:        return -1;
        !           101:     }
        !           102: 
        !           103:     if (GROUP_API_VERSION_GET_MAJOR(group_plugin->version) != GROUP_API_VERSION_MAJOR) {
        !           104:        fprintf(stderr,
        !           105:            "%s: incompatible group plugin major version %d, expected %d\n",
        !           106:            path, GROUP_API_VERSION_GET_MAJOR(group_plugin->version),
        !           107:            GROUP_API_VERSION_MAJOR);
        !           108:        return -1;
        !           109:     }
        !           110: 
        !           111:     /*
        !           112:      * Split args into a vector if specified.
        !           113:      */
        !           114:     if (args != NULL) {
        !           115:        int ac = 0, wasblank = 1;
        !           116:        char *cp;
        !           117: 
        !           118:         for (cp = args; *cp != '\0'; cp++) {
        !           119:             if (isblank((unsigned char)*cp)) {
        !           120:                 wasblank = 1;
        !           121:             } else if (wasblank) {
        !           122:                 wasblank = 0;
        !           123:                 ac++;
        !           124:             }
        !           125:         }
        !           126:        if (ac != 0)    {
        !           127:            argv = malloc(ac * sizeof(char *));
        !           128:            if (argv == NULL) {
        !           129:                fprintf(stderr, "unable to allocate memory\n");
        !           130:                return -1;
        !           131:            }
        !           132:            ac = 0;
        !           133:            for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
        !           134:                argv[ac++] = cp;
        !           135:        }
        !           136:     }
        !           137: 
        !           138:     rc = (group_plugin->init)(GROUP_API_VERSION, plugin_printf, argv);
        !           139: 
        !           140:     free(argv);
        !           141: 
        !           142:     return rc;
        !           143: }
        !           144: 
        !           145: static void
        !           146: group_plugin_unload(void)
        !           147: {
        !           148:     (group_plugin->cleanup)();
        !           149:     dlclose(group_handle);
        !           150:     group_handle = NULL;
        !           151: }
        !           152: 
        !           153: static int
        !           154: group_plugin_query(const char *user, const char *group,
        !           155:     const struct passwd *pwd)
        !           156: {
        !           157:     return group_plugin->query)(user, group, pwd;
        !           158: }
        !           159: 
        !           160: static void
        !           161: usage(void)
        !           162: {
        !           163:     fprintf(stderr,
        !           164:        "usage: plugin_test [-p \"plugin.so plugin_args ...\"] user:group ...\n");
        !           165:     exit(1);
        !           166: }
        !           167: 
        !           168: int
        !           169: main(int argc, char *argv[])
        !           170: {
        !           171:     int ch, i, found;
        !           172:     char *plugin = "sample_group.so";
        !           173:     char *user, *group;
        !           174:     struct passwd *pwd;
        !           175: 
        !           176:     while ((ch = getopt(argc, argv, "p:")) != -1) {
        !           177:        switch (ch) {
        !           178:        case 'p':
        !           179:            plugin = optarg;
        !           180:            break;
        !           181:        default:
        !           182:            usage();
        !           183:        }
        !           184:     }
        !           185:     argc -= optind;
        !           186:     argv += optind;
        !           187: 
        !           188:     if (argc < 1)
        !           189:        usage();
        !           190: 
        !           191:     if (group_plugin_load(plugin) != 1) {
        !           192:        fprintf(stderr, "unable to load plugin: %s\n", plugin);
        !           193:        exit(1);
        !           194:     }
        !           195: 
        !           196:     for (i = 0; argv[i] != NULL; i++) {
        !           197:        user = argv[i];
        !           198:        group = strchr(argv[i], ':');
        !           199:        if (group == NULL)
        !           200:            continue;
        !           201:        *group++ = '\0';
        !           202:        pwd = getpwnam(user);
        !           203:        found = group_plugin_query(user, group, pwd);
        !           204:        printf("user %s %s in group %s\n", user, found ? "is" : "NOT ", group);
        !           205:     }
        !           206:     group_plugin_unload();
        !           207: 
        !           208:     exit(0);
        !           209: }
        !           210: 

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