1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | nss tester for winbindd
|
---|
4 | Copyright (C) Andrew Tridgell 2001
|
---|
5 | Copyright (C) Tim Potter 2003
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "replace.h"
|
---|
22 | #include "nsswitch/nsstest.h"
|
---|
23 |
|
---|
24 | static const char *so_path = "/lib/libnss_winbind.so";
|
---|
25 | static const char *nss_name = "winbind";
|
---|
26 | static int nss_errno;
|
---|
27 | static NSS_STATUS last_error;
|
---|
28 | static int total_errors;
|
---|
29 |
|
---|
30 | static void *find_fn(const char *name)
|
---|
31 | {
|
---|
32 | char *s;
|
---|
33 | static void *h;
|
---|
34 | void *res;
|
---|
35 |
|
---|
36 | if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
|
---|
37 | exit(1);
|
---|
38 | }
|
---|
39 |
|
---|
40 | if (!h) {
|
---|
41 | h = dlopen(so_path, RTLD_LAZY);
|
---|
42 | }
|
---|
43 | if (!h) {
|
---|
44 | printf("Can't open shared library %s\n", so_path);
|
---|
45 | exit(1);
|
---|
46 | }
|
---|
47 | res = dlsym(h, s);
|
---|
48 | if (!res) {
|
---|
49 | printf("Can't find function %s\n", s);
|
---|
50 | total_errors++;
|
---|
51 | free(s);
|
---|
52 | return NULL;
|
---|
53 | }
|
---|
54 | free(s);
|
---|
55 | return res;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void report_nss_error(const char *who, NSS_STATUS status)
|
---|
59 | {
|
---|
60 | last_error = status;
|
---|
61 | total_errors++;
|
---|
62 | printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
|
---|
63 | who, status, NSS_STATUS_SUCCESS, nss_errno);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static struct passwd *nss_getpwent(void)
|
---|
67 | {
|
---|
68 | NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
|
---|
69 | size_t , int *) =
|
---|
70 | (NSS_STATUS (*)(struct passwd *, char *,
|
---|
71 | size_t, int *))find_fn("getpwent_r");
|
---|
72 | static struct passwd pwd;
|
---|
73 | static char buf[1000];
|
---|
74 | NSS_STATUS status;
|
---|
75 |
|
---|
76 | if (!_nss_getpwent_r)
|
---|
77 | return NULL;
|
---|
78 |
|
---|
79 | status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
|
---|
80 | if (status == NSS_STATUS_NOTFOUND) {
|
---|
81 | return NULL;
|
---|
82 | }
|
---|
83 | if (status != NSS_STATUS_SUCCESS) {
|
---|
84 | report_nss_error("getpwent", status);
|
---|
85 | return NULL;
|
---|
86 | }
|
---|
87 | return &pwd;
|
---|
88 | }
|
---|
89 |
|
---|
90 | static struct passwd *nss_getpwnam(const char *name)
|
---|
91 | {
|
---|
92 | NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
|
---|
93 | size_t , int *) =
|
---|
94 | (NSS_STATUS (*)(const char *, struct passwd *, char *,
|
---|
95 | size_t, int *))find_fn("getpwnam_r");
|
---|
96 | static struct passwd pwd;
|
---|
97 | static char buf[1000];
|
---|
98 | NSS_STATUS status;
|
---|
99 |
|
---|
100 | if (!_nss_getpwnam_r)
|
---|
101 | return NULL;
|
---|
102 |
|
---|
103 | status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
|
---|
104 | if (status == NSS_STATUS_NOTFOUND) {
|
---|
105 | return NULL;
|
---|
106 | }
|
---|
107 | if (status != NSS_STATUS_SUCCESS) {
|
---|
108 | report_nss_error("getpwnam", status);
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 | return &pwd;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static struct passwd *nss_getpwuid(uid_t uid)
|
---|
115 | {
|
---|
116 | NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
|
---|
117 | size_t , int *) =
|
---|
118 | (NSS_STATUS (*)(uid_t, struct passwd *, char *,
|
---|
119 | size_t, int *))find_fn("getpwuid_r");
|
---|
120 | static struct passwd pwd;
|
---|
121 | static char buf[1000];
|
---|
122 | NSS_STATUS status;
|
---|
123 |
|
---|
124 | if (!_nss_getpwuid_r)
|
---|
125 | return NULL;
|
---|
126 |
|
---|
127 | status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
|
---|
128 | if (status == NSS_STATUS_NOTFOUND) {
|
---|
129 | return NULL;
|
---|
130 | }
|
---|
131 | if (status != NSS_STATUS_SUCCESS) {
|
---|
132 | report_nss_error("getpwuid", status);
|
---|
133 | return NULL;
|
---|
134 | }
|
---|
135 | return &pwd;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void nss_setpwent(void)
|
---|
139 | {
|
---|
140 | NSS_STATUS (*_nss_setpwent)(void) =
|
---|
141 | (NSS_STATUS(*)(void))find_fn("setpwent");
|
---|
142 | NSS_STATUS status;
|
---|
143 |
|
---|
144 | if (!_nss_setpwent)
|
---|
145 | return;
|
---|
146 |
|
---|
147 | status = _nss_setpwent();
|
---|
148 | if (status != NSS_STATUS_SUCCESS) {
|
---|
149 | report_nss_error("setpwent", status);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | static void nss_endpwent(void)
|
---|
154 | {
|
---|
155 | NSS_STATUS (*_nss_endpwent)(void) =
|
---|
156 | (NSS_STATUS (*)(void))find_fn("endpwent");
|
---|
157 | NSS_STATUS status;
|
---|
158 |
|
---|
159 | if (!_nss_endpwent)
|
---|
160 | return;
|
---|
161 |
|
---|
162 | status = _nss_endpwent();
|
---|
163 | if (status != NSS_STATUS_SUCCESS) {
|
---|
164 | report_nss_error("endpwent", status);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | static struct group *nss_getgrent(void)
|
---|
170 | {
|
---|
171 | NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
|
---|
172 | size_t , int *) =
|
---|
173 | (NSS_STATUS (*)(struct group *, char *,
|
---|
174 | size_t, int *))find_fn("getgrent_r");
|
---|
175 | static struct group grp;
|
---|
176 | static char *buf;
|
---|
177 | static int buflen = 1024;
|
---|
178 | NSS_STATUS status;
|
---|
179 |
|
---|
180 | if (!_nss_getgrent_r)
|
---|
181 | return NULL;
|
---|
182 |
|
---|
183 | if (!buf)
|
---|
184 | buf = (char *)malloc(buflen);
|
---|
185 |
|
---|
186 | again:
|
---|
187 | status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
|
---|
188 | if (status == NSS_STATUS_TRYAGAIN) {
|
---|
189 | buflen *= 2;
|
---|
190 | buf = (char *)realloc(buf, buflen);
|
---|
191 | if (!buf) {
|
---|
192 | return NULL;
|
---|
193 | }
|
---|
194 | goto again;
|
---|
195 | }
|
---|
196 | if (status == NSS_STATUS_NOTFOUND) {
|
---|
197 | free(buf);
|
---|
198 | return NULL;
|
---|
199 | }
|
---|
200 | if (status != NSS_STATUS_SUCCESS) {
|
---|
201 | report_nss_error("getgrent", status);
|
---|
202 | free(buf);
|
---|
203 | return NULL;
|
---|
204 | }
|
---|
205 | return &grp;
|
---|
206 | }
|
---|
207 |
|
---|
208 | static struct group *nss_getgrnam(const char *name)
|
---|
209 | {
|
---|
210 | NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
|
---|
211 | size_t , int *) =
|
---|
212 | (NSS_STATUS (*)(const char *, struct group *, char *,
|
---|
213 | size_t, int *))find_fn("getgrnam_r");
|
---|
214 | static struct group grp;
|
---|
215 | static char *buf;
|
---|
216 | static int buflen = 1000;
|
---|
217 | NSS_STATUS status;
|
---|
218 |
|
---|
219 | if (!_nss_getgrnam_r)
|
---|
220 | return NULL;
|
---|
221 |
|
---|
222 | if (!buf)
|
---|
223 | buf = (char *)malloc(buflen);
|
---|
224 | again:
|
---|
225 | status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
|
---|
226 | if (status == NSS_STATUS_TRYAGAIN) {
|
---|
227 | buflen *= 2;
|
---|
228 | buf = (char *)realloc(buf, buflen);
|
---|
229 | if (!buf) {
|
---|
230 | return NULL;
|
---|
231 | }
|
---|
232 | goto again;
|
---|
233 | }
|
---|
234 | if (status == NSS_STATUS_NOTFOUND) {
|
---|
235 | free(buf);
|
---|
236 | return NULL;
|
---|
237 | }
|
---|
238 | if (status != NSS_STATUS_SUCCESS) {
|
---|
239 | report_nss_error("getgrnam", status);
|
---|
240 | free(buf);
|
---|
241 | return NULL;
|
---|
242 | }
|
---|
243 | return &grp;
|
---|
244 | }
|
---|
245 |
|
---|
246 | static struct group *nss_getgrgid(gid_t gid)
|
---|
247 | {
|
---|
248 | NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
|
---|
249 | size_t , int *) =
|
---|
250 | (NSS_STATUS (*)(gid_t, struct group *, char *,
|
---|
251 | size_t, int *))find_fn("getgrgid_r");
|
---|
252 | static struct group grp;
|
---|
253 | static char *buf;
|
---|
254 | static int buflen = 1000;
|
---|
255 | NSS_STATUS status;
|
---|
256 |
|
---|
257 | if (!_nss_getgrgid_r)
|
---|
258 | return NULL;
|
---|
259 |
|
---|
260 | if (!buf)
|
---|
261 | buf = (char *)malloc(buflen);
|
---|
262 |
|
---|
263 | again:
|
---|
264 | status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
|
---|
265 | if (status == NSS_STATUS_TRYAGAIN) {
|
---|
266 | buflen *= 2;
|
---|
267 | buf = (char *)realloc(buf, buflen);
|
---|
268 | if (!buf) {
|
---|
269 | return NULL;
|
---|
270 | }
|
---|
271 | goto again;
|
---|
272 | }
|
---|
273 | if (status == NSS_STATUS_NOTFOUND) {
|
---|
274 | free(buf);
|
---|
275 | return NULL;
|
---|
276 | }
|
---|
277 | if (status != NSS_STATUS_SUCCESS) {
|
---|
278 | report_nss_error("getgrgid", status);
|
---|
279 | free(buf);
|
---|
280 | return NULL;
|
---|
281 | }
|
---|
282 | return &grp;
|
---|
283 | }
|
---|
284 |
|
---|
285 | static void nss_setgrent(void)
|
---|
286 | {
|
---|
287 | NSS_STATUS (*_nss_setgrent)(void) =
|
---|
288 | (NSS_STATUS (*)(void))find_fn("setgrent");
|
---|
289 | NSS_STATUS status;
|
---|
290 |
|
---|
291 | if (!_nss_setgrent)
|
---|
292 | return;
|
---|
293 |
|
---|
294 | status = _nss_setgrent();
|
---|
295 | if (status != NSS_STATUS_SUCCESS) {
|
---|
296 | report_nss_error("setgrent", status);
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | static void nss_endgrent(void)
|
---|
301 | {
|
---|
302 | NSS_STATUS (*_nss_endgrent)(void) =
|
---|
303 | (NSS_STATUS (*)(void))find_fn("endgrent");
|
---|
304 | NSS_STATUS status;
|
---|
305 |
|
---|
306 | if (!_nss_endgrent)
|
---|
307 | return;
|
---|
308 |
|
---|
309 | status = _nss_endgrent();
|
---|
310 | if (status != NSS_STATUS_SUCCESS) {
|
---|
311 | report_nss_error("endgrent", status);
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
|
---|
316 | {
|
---|
317 | NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
|
---|
318 | long int *, gid_t **, long int , int *) =
|
---|
319 | (NSS_STATUS (*)(char *, gid_t, long int *,
|
---|
320 | long int *, gid_t **,
|
---|
321 | long int, int *))find_fn("initgroups_dyn");
|
---|
322 | NSS_STATUS status;
|
---|
323 |
|
---|
324 | if (!_nss_initgroups)
|
---|
325 | return NSS_STATUS_UNAVAIL;
|
---|
326 |
|
---|
327 | status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
|
---|
328 | if (status != NSS_STATUS_SUCCESS) {
|
---|
329 | report_nss_error("initgroups", status);
|
---|
330 | }
|
---|
331 | return status;
|
---|
332 | }
|
---|
333 |
|
---|
334 | static void print_passwd(struct passwd *pwd)
|
---|
335 | {
|
---|
336 | printf("%s:%s:%lu:%lu:%s:%s:%s\n",
|
---|
337 | pwd->pw_name,
|
---|
338 | pwd->pw_passwd,
|
---|
339 | (unsigned long)pwd->pw_uid,
|
---|
340 | (unsigned long)pwd->pw_gid,
|
---|
341 | pwd->pw_gecos,
|
---|
342 | pwd->pw_dir,
|
---|
343 | pwd->pw_shell);
|
---|
344 | }
|
---|
345 |
|
---|
346 | static void print_group(struct group *grp)
|
---|
347 | {
|
---|
348 | int i;
|
---|
349 | printf("%s:%s:%lu:",
|
---|
350 | grp->gr_name,
|
---|
351 | grp->gr_passwd,
|
---|
352 | (unsigned long)grp->gr_gid);
|
---|
353 |
|
---|
354 | if (!grp->gr_mem[0]) {
|
---|
355 | printf("\n");
|
---|
356 | return;
|
---|
357 | }
|
---|
358 |
|
---|
359 | for (i=0; grp->gr_mem[i+1]; i++) {
|
---|
360 | printf("%s,", grp->gr_mem[i]);
|
---|
361 | }
|
---|
362 | printf("%s\n", grp->gr_mem[i]);
|
---|
363 | }
|
---|
364 |
|
---|
365 | static void nss_test_initgroups(char *name, gid_t gid)
|
---|
366 | {
|
---|
367 | long int size = 16;
|
---|
368 | long int start = 1;
|
---|
369 | gid_t *groups = NULL;
|
---|
370 | int i;
|
---|
371 | NSS_STATUS status;
|
---|
372 |
|
---|
373 | groups = (gid_t *)malloc(size);
|
---|
374 | groups[0] = gid;
|
---|
375 |
|
---|
376 | status = nss_initgroups(name, gid, &groups, &start, &size);
|
---|
377 | if (status == NSS_STATUS_UNAVAIL) {
|
---|
378 | printf("No initgroups fn\n");
|
---|
379 | return;
|
---|
380 | }
|
---|
381 |
|
---|
382 | for (i=0; i<start-1; i++) {
|
---|
383 | printf("%lu, ", (unsigned long)groups[i]);
|
---|
384 | }
|
---|
385 | printf("%lu\n", (unsigned long)groups[i]);
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | static void nss_test_users(void)
|
---|
390 | {
|
---|
391 | struct passwd *pwd;
|
---|
392 |
|
---|
393 | nss_setpwent();
|
---|
394 | /* loop over all users */
|
---|
395 | while ((pwd = nss_getpwent())) {
|
---|
396 | printf("Testing user %s\n", pwd->pw_name);
|
---|
397 | printf("getpwent: "); print_passwd(pwd);
|
---|
398 | pwd = nss_getpwuid(pwd->pw_uid);
|
---|
399 | if (!pwd) {
|
---|
400 | total_errors++;
|
---|
401 | printf("ERROR: can't getpwuid\n");
|
---|
402 | continue;
|
---|
403 | }
|
---|
404 | printf("getpwuid: "); print_passwd(pwd);
|
---|
405 | pwd = nss_getpwnam(pwd->pw_name);
|
---|
406 | if (!pwd) {
|
---|
407 | total_errors++;
|
---|
408 | printf("ERROR: can't getpwnam\n");
|
---|
409 | continue;
|
---|
410 | }
|
---|
411 | printf("getpwnam: "); print_passwd(pwd);
|
---|
412 | printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
|
---|
413 | printf("\n");
|
---|
414 | }
|
---|
415 | nss_endpwent();
|
---|
416 | }
|
---|
417 |
|
---|
418 | static void nss_test_groups(void)
|
---|
419 | {
|
---|
420 | struct group *grp;
|
---|
421 |
|
---|
422 | nss_setgrent();
|
---|
423 | /* loop over all groups */
|
---|
424 | while ((grp = nss_getgrent())) {
|
---|
425 | printf("Testing group %s\n", grp->gr_name);
|
---|
426 | printf("getgrent: "); print_group(grp);
|
---|
427 | grp = nss_getgrnam(grp->gr_name);
|
---|
428 | if (!grp) {
|
---|
429 | total_errors++;
|
---|
430 | printf("ERROR: can't getgrnam\n");
|
---|
431 | continue;
|
---|
432 | }
|
---|
433 | printf("getgrnam: "); print_group(grp);
|
---|
434 | grp = nss_getgrgid(grp->gr_gid);
|
---|
435 | if (!grp) {
|
---|
436 | total_errors++;
|
---|
437 | printf("ERROR: can't getgrgid\n");
|
---|
438 | continue;
|
---|
439 | }
|
---|
440 | printf("getgrgid: "); print_group(grp);
|
---|
441 | printf("\n");
|
---|
442 | }
|
---|
443 | nss_endgrent();
|
---|
444 | }
|
---|
445 |
|
---|
446 | static void nss_test_errors(void)
|
---|
447 | {
|
---|
448 | struct passwd *pwd;
|
---|
449 | struct group *grp;
|
---|
450 |
|
---|
451 | pwd = getpwnam("nosuchname");
|
---|
452 | if (pwd || last_error != NSS_STATUS_NOTFOUND) {
|
---|
453 | total_errors++;
|
---|
454 | printf("ERROR Non existant user gave error %d\n", last_error);
|
---|
455 | }
|
---|
456 |
|
---|
457 | pwd = getpwuid(0xFFF0);
|
---|
458 | if (pwd || last_error != NSS_STATUS_NOTFOUND) {
|
---|
459 | total_errors++;
|
---|
460 | printf("ERROR Non existant uid gave error %d\n", last_error);
|
---|
461 | }
|
---|
462 |
|
---|
463 | grp = getgrnam("nosuchgroup");
|
---|
464 | if (grp || last_error != NSS_STATUS_NOTFOUND) {
|
---|
465 | total_errors++;
|
---|
466 | printf("ERROR Non existant group gave error %d\n", last_error);
|
---|
467 | }
|
---|
468 |
|
---|
469 | grp = getgrgid(0xFFF0);
|
---|
470 | if (grp || last_error != NSS_STATUS_NOTFOUND) {
|
---|
471 | total_errors++;
|
---|
472 | printf("ERROR Non existant gid gave error %d\n", last_error);
|
---|
473 | }
|
---|
474 | }
|
---|
475 |
|
---|
476 | int main(int argc, char *argv[])
|
---|
477 | {
|
---|
478 | if (argc > 1) so_path = argv[1];
|
---|
479 | if (argc > 2) nss_name = argv[2];
|
---|
480 |
|
---|
481 | nss_test_users();
|
---|
482 | nss_test_groups();
|
---|
483 | nss_test_errors();
|
---|
484 |
|
---|
485 | printf("total_errors=%d\n", total_errors);
|
---|
486 |
|
---|
487 | return total_errors;
|
---|
488 | }
|
---|