2 * Copyright (c) 2010-2011 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
17 #include <sys/types.h>
18 #include <sys/param.h>
32 #include "sudo_plugin.h"
35 * Simple driver to test sudoer group plugins.
36 * usage: plugin_test [-p "plugin.so plugin_args ..."] user:group ...
39 static void *group_handle;
40 static struct sudoers_group_plugin *group_plugin;
43 plugin_printf(int msg_type, const char *fmt, ...)
49 case SUDO_CONV_INFO_MSG:
52 case SUDO_CONV_ERROR_MSG:
61 vfprintf(fp, fmt, ap);
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.
73 group_plugin_load(char *plugin_info)
75 char *args, path[PATH_MAX], savedch;
80 * Fill in .so path and split out args (if any).
82 if ((args = strpbrk(plugin_info, " \t")) != NULL) {
86 strncpy(path, plugin_info, sizeof(path) - 1);
87 path[sizeof(path) - 1] = '\0';
91 /* Open plugin and map in symbol. */
92 group_handle = dlopen(path, RTLD_LAZY);
94 fprintf(stderr, "unable to dlopen %s: %s\n", path, dlerror());
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);
103 if (GROUP_API_VERSION_GET_MAJOR(group_plugin->version) != GROUP_API_VERSION_MAJOR) {
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);
112 * Split args into a vector if specified.
115 int ac = 0, wasblank = 1;
118 for (cp = args; *cp != '\0'; cp++) {
119 if (isblank((unsigned char)*cp)) {
121 } else if (wasblank) {
127 argv = malloc(ac * sizeof(char *));
129 fprintf(stderr, "unable to allocate memory\n");
133 for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
138 rc = (group_plugin->init)(GROUP_API_VERSION, plugin_printf, argv);
146 group_plugin_unload(void)
148 (group_plugin->cleanup)();
149 dlclose(group_handle);
154 group_plugin_query(const char *user, const char *group,
155 const struct passwd *pwd)
157 return group_plugin->query)(user, group, pwd;
164 "usage: plugin_test [-p \"plugin.so plugin_args ...\"] user:group ...\n");
169 main(int argc, char *argv[])
172 char *plugin = "sample_group.so";
176 while ((ch = getopt(argc, argv, "p:")) != -1) {
191 if (group_plugin_load(plugin) != 1) {
192 fprintf(stderr, "unable to load plugin: %s\n", plugin);
196 for (i = 0; argv[i] != NULL; i++) {
198 group = strchr(argv[i], ':');
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);
206 group_plugin_unload();