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
);
376 status
= nss_initgroups(name
, gid
, &groups
, &start
, &size
);
377 if (status
== NSS_STATUS_UNAVAIL
) {
378 printf("No initgroups fn\n");
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)
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
);
401 printf("ERROR: can't getpwuid\n");
404 printf("getpwuid: "); print_passwd(pwd
);
405 pwd
= nss_getpwnam(pwd
->pw_name
);
408 printf("ERROR: can't getpwnam\n");
411 printf("getpwnam: "); print_passwd(pwd
);
412 printf("initgroups: "); nss_test_initgroups(pwd
->pw_name
, pwd
->pw_gid
);
418 static void nss_test_groups(void)
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
);
430 printf("ERROR: can't getgrnam\n");
433 printf("getgrnam: "); print_group(grp
);
434 grp
= nss_getgrgid(grp
->gr_gid
);
437 printf("ERROR: can't getgrgid\n");
440 printf("getgrgid: "); print_group(grp
);
446 static void nss_test_errors(void)
451 pwd
= getpwnam("nosuchname");
452 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
454 printf("ERROR Non existent user gave error %d\n", last_error
);
457 pwd
= getpwuid(0xFFF0);
458 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
460 printf("ERROR Non existent uid gave error %d\n", last_error
);
463 grp
= getgrnam("nosuchgroup");
464 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
466 printf("ERROR Non existent group gave error %d\n", last_error
);
469 grp
= getgrgid(0xFFF0);
470 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
472 printf("ERROR Non existent 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];
485 printf("total_errors=%d\n", total_errors
);