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