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/>.
22 #include "nsswitch/nsstest.h"
24 static const char *so_path = "/lib/libnss_winbind.so";
25 static const char *nss_name = "winbind";
27 static NSS_STATUS last_error;
28 static int total_errors;
30 static void *find_fn(const char *name)
36 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
41 h = dlopen(so_path, RTLD_LAZY);
44 printf("Can't open shared library %s\n", so_path);
49 printf("Can't find function %s\n", s);
58 static void report_nss_error(const char *who, NSS_STATUS status)
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 *,
70 (NSS_STATUS (*)(struct passwd *, char *,
71 size_t, int *))find_fn("getpwent_r");
72 static struct passwd pwd;
73 static char buf[1000];
79 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
80 if (status == NSS_STATUS_NOTFOUND) {
83 if (status != NSS_STATUS_SUCCESS) {
84 report_nss_error("getpwent", status);
90 static struct passwd *nss_getpwnam(const char *name)
92 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
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];
100 if (!_nss_getpwnam_r)
103 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
104 if (status == NSS_STATUS_NOTFOUND) {
107 if (status != NSS_STATUS_SUCCESS) {
108 report_nss_error("getpwnam", status);
114 static struct passwd *nss_getpwuid(uid_t uid)
116 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
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];
124 if (!_nss_getpwuid_r)
127 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
128 if (status == NSS_STATUS_NOTFOUND) {
131 if (status != NSS_STATUS_SUCCESS) {
132 report_nss_error("getpwuid", status);
138 static void nss_setpwent(void)
140 NSS_STATUS (*_nss_setpwent)(void) =
141 (NSS_STATUS(*)(void))find_fn("setpwent");
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");
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 *,
173 (NSS_STATUS (*)(struct group *, char *,
174 size_t, int *))find_fn("getgrent_r");
175 static struct group grp;
177 static int buflen = 1024;
180 if (!_nss_getgrent_r)
184 buf = (char *)malloc(buflen);
187 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
188 if (status == NSS_STATUS_TRYAGAIN) {
190 buf = (char *)realloc(buf, buflen);
196 if (status == NSS_STATUS_NOTFOUND) {
200 if (status != NSS_STATUS_SUCCESS) {
201 report_nss_error("getgrent", status);
208 static struct group *nss_getgrnam(const char *name)
210 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
212 (NSS_STATUS (*)(const char *, struct group *, char *,
213 size_t, int *))find_fn("getgrnam_r");
214 static struct group grp;
216 static int buflen = 1000;
219 if (!_nss_getgrnam_r)
223 buf = (char *)malloc(buflen);
225 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
226 if (status == NSS_STATUS_TRYAGAIN) {
228 buf = (char *)realloc(buf, buflen);
234 if (status == NSS_STATUS_NOTFOUND) {
238 if (status != NSS_STATUS_SUCCESS) {
239 report_nss_error("getgrnam", status);
246 static struct group *nss_getgrgid(gid_t gid)
248 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
250 (NSS_STATUS (*)(gid_t, struct group *, char *,
251 size_t, int *))find_fn("getgrgid_r");
252 static struct group grp;
254 static int buflen = 1000;
257 if (!_nss_getgrgid_r)
261 buf = (char *)malloc(buflen);
264 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
265 if (status == NSS_STATUS_TRYAGAIN) {
267 buf = (char *)realloc(buf, buflen);
273 if (status == NSS_STATUS_NOTFOUND) {
277 if (status != NSS_STATUS_SUCCESS) {
278 report_nss_error("getgrgid", status);
285 static void nss_setgrent(void)
287 NSS_STATUS (*_nss_setgrent)(void) =
288 (NSS_STATUS (*)(void))find_fn("setgrent");
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");
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");
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);
334 static void print_passwd(struct passwd *pwd)
336 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
339 (unsigned long)pwd->pw_uid,
340 (unsigned long)pwd->pw_gid,
346 static void print_group(struct group *grp)
352 (unsigned long)grp->gr_gid);
354 if (!grp->gr_mem[0]) {
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)
369 gid_t *groups = NULL;
373 groups = (gid_t *)malloc(sizeof(gid_t) * size);
374 if (groups == NULL) {
375 printf("Unable to allocate memory for groups\n");
380 status = nss_initgroups(name, gid, &groups, &start, &size);
381 if (status == NSS_STATUS_UNAVAIL) {
382 printf("No initgroups fn\n");
386 for (i=0; i<start-1; i++) {
387 printf("%lu, ", (unsigned long)groups[i]);
389 printf("%lu\n", (unsigned long)groups[i]);
393 static void nss_test_users(void)
398 /* loop over all users */
399 while ((pwd = nss_getpwent())) {
400 printf("Testing user %s\n", pwd->pw_name);
401 printf("getpwent: "); print_passwd(pwd);
402 pwd = nss_getpwuid(pwd->pw_uid);
405 printf("ERROR: can't getpwuid\n");
408 printf("getpwuid: "); print_passwd(pwd);
409 pwd = nss_getpwnam(pwd->pw_name);
412 printf("ERROR: can't getpwnam\n");
415 printf("getpwnam: "); print_passwd(pwd);
416 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
422 static void nss_test_groups(void)
427 /* loop over all groups */
428 while ((grp = nss_getgrent())) {
429 printf("Testing group %s\n", grp->gr_name);
430 printf("getgrent: "); print_group(grp);
431 grp = nss_getgrnam(grp->gr_name);
434 printf("ERROR: can't getgrnam\n");
437 printf("getgrnam: "); print_group(grp);
438 grp = nss_getgrgid(grp->gr_gid);
441 printf("ERROR: can't getgrgid\n");
444 printf("getgrgid: "); print_group(grp);
450 static void nss_test_errors(void)
455 pwd = getpwnam("nosuchname");
456 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
458 printf("ERROR Non existent user gave error %d\n", last_error);
461 pwd = getpwuid(0xFFF0);
462 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
464 printf("ERROR Non existent uid gave error %d\n", last_error);
467 grp = getgrnam("nosuchgroup");
468 if (grp || last_error != NSS_STATUS_NOTFOUND) {
470 printf("ERROR Non existent group gave error %d\n", last_error);
473 grp = getgrgid(0xFFF0);
474 if (grp || last_error != NSS_STATUS_NOTFOUND) {
476 printf("ERROR Non existent gid gave error %d\n", last_error);
480 int main(int argc, char *argv[])
482 if (argc > 1) so_path = argv[1];
483 if (argc > 2) nss_name = argv[2];
489 printf("total_errors=%d\n", total_errors);