Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/group_file/plugin_test.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2010-2013 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
#include <ctype.h>
21
#include <dlfcn.h>
22
#include <errno.h>
23
#include <limits.h>
24
#include <stdarg.h>
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <unistd.h>
29
30
#include <sudo_plugin.h>
31
32
sudo_dso_public int main(int argc, char *argv[]);
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 * restrict 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
if (strlcpy(path, plugin_info, sizeof(path)) >= sizeof(path)) {
87
fprintf(stderr, "path too long: %s\n", plugin_info);
88
return -1;
89
}
90
if (args != NULL)
91
*args++ = savedch;
92
93
/* Open plugin and map in symbol. */
94
group_handle = dlopen(path, RTLD_LAZY);
95
if (!group_handle) {
96
fprintf(stderr, "unable to dlopen %s: %s\n", path, dlerror());
97
return -1;
98
}
99
group_plugin = dlsym(group_handle, "group_plugin");
100
if (group_plugin == NULL) {
101
fprintf(stderr, "unable to find symbol \"group_plugin\" in %s\n", path);
102
return -1;
103
}
104
105
if (SUDO_API_VERSION_GET_MAJOR(group_plugin->version) != GROUP_API_VERSION_MAJOR) {
106
fprintf(stderr,
107
"%s: incompatible group plugin major version %u, expected %u\n",
108
path, SUDO_API_VERSION_GET_MAJOR(group_plugin->version),
109
GROUP_API_VERSION_MAJOR);
110
return -1;
111
}
112
113
/*
114
* Split args into a vector if specified.
115
*/
116
if (args != NULL) {
117
int ac = 0, wasblank = 1;
118
char *cp, *last;
119
120
for (cp = args; *cp != '\0'; cp++) {
121
if (isblank((unsigned char)*cp)) {
122
wasblank = 1;
123
} else if (wasblank) {
124
wasblank = 0;
125
ac++;
126
}
127
}
128
if (ac != 0) {
129
argv = malloc((ac + 1) * sizeof(char *));
130
if (argv == NULL) {
131
perror(NULL);
132
return -1;
133
}
134
ac = 0;
135
cp = strtok_r(args, " \t", &last);
136
while (cp != NULL) {
137
argv[ac++] = cp;
138
cp = strtok_r(NULL, " \t", &last);
139
}
140
argv[ac] = NULL;
141
}
142
}
143
144
rc = (group_plugin->init)(GROUP_API_VERSION, plugin_printf, argv);
145
146
free(argv);
147
148
return rc;
149
}
150
151
static void
152
group_plugin_unload(void)
153
{
154
(group_plugin->cleanup)();
155
dlclose(group_handle);
156
group_handle = NULL;
157
}
158
159
static int
160
group_plugin_query(const char *user, const char *group,
161
const struct passwd *pwd)
162
{
163
return (group_plugin->query)(user, group, pwd);
164
}
165
166
static void
167
usage(void)
168
{
169
fputs("usage: plugin_test [-p \"plugin.so plugin_args ...\"] user:group ...\n",
170
stderr);
171
exit(EXIT_FAILURE);
172
}
173
174
int
175
main(int argc, char *argv[])
176
{
177
int ch, found;
178
size_t i;
179
char *plugin = "group_file.so";
180
char *user, *group;
181
struct passwd *pwd;
182
183
while ((ch = getopt(argc, argv, "p:")) != -1) {
184
switch (ch) {
185
case 'p':
186
plugin = optarg;
187
break;
188
default:
189
usage();
190
}
191
}
192
argc -= optind;
193
argv += optind;
194
195
if (argc < 1)
196
usage();
197
198
if (group_plugin_load(plugin) != 1) {
199
fprintf(stderr, "unable to load plugin: %s\n", plugin);
200
return EXIT_FAILURE;
201
}
202
203
for (i = 0; argv[i] != NULL; i++) {
204
user = argv[i];
205
group = strchr(argv[i], ':');
206
if (group == NULL)
207
continue;
208
*group++ = '\0';
209
pwd = getpwnam(user);
210
found = group_plugin_query(user, group, pwd);
211
printf("user %s %s in group %s\n", user, found ? "is" : "NOT ", group);
212
}
213
group_plugin_unload();
214
215
return EXIT_SUCCESS;
216
}
217
218
219