2 * Copyright (c) 2010-2013 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>
31 #include "sudo_plugin.h"
33 __dso_public int main(int argc, char *argv[]);
36 * Simple driver to test sudoer group plugins.
37 * usage: plugin_test [-p "plugin.so plugin_args ..."] user:group ...
40 static void *group_handle;
41 static struct sudoers_group_plugin *group_plugin;
44 plugin_printf(int msg_type, const char *fmt, ...)
50 case SUDO_CONV_INFO_MSG:
53 case SUDO_CONV_ERROR_MSG:
62 vfprintf(fp, fmt, ap);
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.
74 group_plugin_load(char *plugin_info)
76 char *args, path[PATH_MAX], savedch;
81 * Fill in .so path and split out args (if any).
83 if ((args = strpbrk(plugin_info, " \t")) != NULL) {
87 strncpy(path, plugin_info, sizeof(path) - 1);
88 path[sizeof(path) - 1] = '\0';
92 /* Open plugin and map in symbol. */
93 group_handle = dlopen(path, RTLD_LAZY);
95 fprintf(stderr, "unable to dlopen %s: %s\n", path, dlerror());
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);
104 if (GROUP_API_VERSION_GET_MAJOR(group_plugin->version) != GROUP_API_VERSION_MAJOR) {
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);
113 * Split args into a vector if specified.
116 int ac = 0, wasblank = 1;
119 for (cp = args; *cp != '\0'; cp++) {
120 if (isblank((unsigned char)*cp)) {
122 } else if (wasblank) {
128 argv = malloc(ac * sizeof(char *));
134 for ((cp = strtok(args, " \t")); cp; (cp = strtok(NULL, " \t")))
139 rc = (group_plugin->init)(GROUP_API_VERSION, plugin_printf, argv);
147 group_plugin_unload(void)
149 (group_plugin->cleanup)();
150 dlclose(group_handle);
155 group_plugin_query(const char *user, const char *group,
156 const struct passwd *pwd)
158 return group_plugin->query)(user, group, pwd;
165 "usage: plugin_test [-p \"plugin.so plugin_args ...\"] user:group ...\n");
170 main(int argc, char *argv[])
173 char *plugin = "group_file.so";
177 while ((ch = getopt(argc, argv, "p:")) != -1) {
192 if (group_plugin_load(plugin) != 1) {
193 fprintf(stderr, "unable to load plugin: %s\n", plugin);
197 for (i = 0; argv[i] != NULL; i++) {
199 group = strchr(argv[i], ':');
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);
207 group_plugin_unload();