Fix for servers that don't put a path separator at the end of the service.
[Samba.git] / nsswitch / nsstest.c
blobd84e028513b09009ae57c4fa9b2c1453944b9900
1 /*
2 Unix SMB/CIFS implementation.
3 nss tester for winbindd
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Tim Potter 2003
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.
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.
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/>.
21 #include "replace.h"
22 #include "nsswitch/nsstest.h"
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;
30 static void *find_fn(const char *name)
32 char *s;
33 static void *h;
34 void *res;
36 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
37 exit(1);
40 if (!h) {
41 h = dlopen(so_path, RTLD_LAZY);
43 if (!h) {
44 printf("Can't open shared library %s\n", so_path);
45 exit(1);
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;
54 free(s);
55 return res;
58 static void report_nss_error(const char *who, NSS_STATUS status)
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);
66 static struct passwd *nss_getpwent(void)
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;
76 if (!_nss_getpwent_r)
77 return NULL;
79 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
80 if (status == NSS_STATUS_NOTFOUND) {
81 return NULL;
83 if (status != NSS_STATUS_SUCCESS) {
84 report_nss_error("getpwent", status);
85 return NULL;
87 return &pwd;
90 static struct passwd *nss_getpwnam(const char *name)
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;
100 if (!_nss_getpwnam_r)
101 return NULL;
103 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
104 if (status == NSS_STATUS_NOTFOUND) {
105 return NULL;
107 if (status != NSS_STATUS_SUCCESS) {
108 report_nss_error("getpwnam", status);
109 return NULL;
111 return &pwd;
114 static struct passwd *nss_getpwuid(uid_t uid)
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;
124 if (!_nss_getpwuid_r)
125 return NULL;
127 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
128 if (status == NSS_STATUS_NOTFOUND) {
129 return NULL;
131 if (status != NSS_STATUS_SUCCESS) {
132 report_nss_error("getpwuid", status);
133 return NULL;
135 return &pwd;
138 static void nss_setpwent(void)
140 NSS_STATUS (*_nss_setpwent)(void) =
141 (NSS_STATUS(*)(void))find_fn("setpwent");
142 NSS_STATUS status;
144 if (!_nss_setpwent)
145 return;
147 status = _nss_setpwent();
148 if (status != NSS_STATUS_SUCCESS) {
149 report_nss_error("setpwent", status);
153 static void nss_endpwent(void)
155 NSS_STATUS (*_nss_endpwent)(void) =
156 (NSS_STATUS (*)(void))find_fn("endpwent");
157 NSS_STATUS status;
159 if (!_nss_endpwent)
160 return;
162 status = _nss_endpwent();
163 if (status != NSS_STATUS_SUCCESS) {
164 report_nss_error("endpwent", status);
169 static struct group *nss_getgrent(void)
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;
180 if (!_nss_getgrent_r)
181 return NULL;
183 if (!buf)
184 buf = (char *)malloc(buflen);
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;
194 goto again;
196 if (status == NSS_STATUS_NOTFOUND) {
197 free(buf);
198 return NULL;
200 if (status != NSS_STATUS_SUCCESS) {
201 report_nss_error("getgrent", status);
202 free(buf);
203 return NULL;
205 return &grp;
208 static struct group *nss_getgrnam(const char *name)
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;
219 if (!_nss_getgrnam_r)
220 return NULL;
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;
232 goto again;
234 if (status == NSS_STATUS_NOTFOUND) {
235 free(buf);
236 return NULL;
238 if (status != NSS_STATUS_SUCCESS) {
239 report_nss_error("getgrnam", status);
240 free(buf);
241 return NULL;
243 return &grp;
246 static struct group *nss_getgrgid(gid_t gid)
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;
257 if (!_nss_getgrgid_r)
258 return NULL;
260 if (!buf)
261 buf = (char *)malloc(buflen);
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;
271 goto again;
273 if (status == NSS_STATUS_NOTFOUND) {
274 free(buf);
275 return NULL;
277 if (status != NSS_STATUS_SUCCESS) {
278 report_nss_error("getgrgid", status);
279 free(buf);
280 return NULL;
282 return &grp;
285 static void nss_setgrent(void)
287 NSS_STATUS (*_nss_setgrent)(void) =
288 (NSS_STATUS (*)(void))find_fn("setgrent");
289 NSS_STATUS status;
291 if (!_nss_setgrent)
292 return;
294 status = _nss_setgrent();
295 if (status != NSS_STATUS_SUCCESS) {
296 report_nss_error("setgrent", status);
300 static void nss_endgrent(void)
302 NSS_STATUS (*_nss_endgrent)(void) =
303 (NSS_STATUS (*)(void))find_fn("endgrent");
304 NSS_STATUS status;
306 if (!_nss_endgrent)
307 return;
309 status = _nss_endgrent();
310 if (status != NSS_STATUS_SUCCESS) {
311 report_nss_error("endgrent", status);
315 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
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;
324 if (!_nss_initgroups)
325 return NSS_STATUS_UNAVAIL;
327 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
328 if (status != NSS_STATUS_SUCCESS) {
329 report_nss_error("initgroups", status);
331 return status;
334 static void print_passwd(struct passwd *pwd)
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);
346 static void print_group(struct group *grp)
348 int i;
349 printf("%s:%s:%lu:",
350 grp->gr_name,
351 grp->gr_passwd,
352 (unsigned long)grp->gr_gid);
354 if (!grp->gr_mem[0]) {
355 printf("\n");
356 return;
359 for (i=0; grp->gr_mem[i+1]; i++) {
360 printf("%s,", grp->gr_mem[i]);
362 printf("%s\n", grp->gr_mem[i]);
365 static void nss_test_initgroups(char *name, gid_t gid)
367 long int size = 16;
368 long int start = 1;
369 gid_t *groups = NULL;
370 int i;
371 NSS_STATUS status;
373 groups = (gid_t *)malloc(size);
374 groups[0] = gid;
376 status = nss_initgroups(name, gid, &groups, &start, &size);
377 if (status == NSS_STATUS_UNAVAIL) {
378 printf("No initgroups fn\n");
379 return;
382 for (i=0; i<start-1; i++) {
383 printf("%lu, ", (unsigned long)groups[i]);
385 printf("%lu\n", (unsigned long)groups[i]);
389 static void nss_test_users(void)
391 struct passwd *pwd;
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;
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;
411 printf("getpwnam: "); print_passwd(pwd);
412 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
413 printf("\n");
415 nss_endpwent();
418 static void nss_test_groups(void)
420 struct group *grp;
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;
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;
440 printf("getgrgid: "); print_group(grp);
441 printf("\n");
443 nss_endgrent();
446 static void nss_test_errors(void)
448 struct passwd *pwd;
449 struct group *grp;
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);
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);
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);
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);
476 int main(int argc, char *argv[])
478 if (argc > 1) so_path = argv[1];
479 if (argc > 2) nss_name = argv[2];
481 nss_test_users();
482 nss_test_groups();
483 nss_test_errors();
485 printf("total_errors=%d\n", total_errors);
487 return total_errors;