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"
31 static const char *so_path
= "/lib/libnss_winbind.so";
32 static const char *nss_name
= "winbind";
34 static NSS_STATUS last_error
;
35 static int total_errors
;
37 static void *find_fn(const char *name
)
43 if (asprintf(&s
, "_nss_%s_%s", nss_name
, name
) < 0) {
48 h
= dlopen(so_path
, RTLD_LAZY
);
51 printf("Can't open shared library %s\n", so_path
);
56 printf("Can't find function %s\n", s
);
65 static void report_nss_error(const char *who
, NSS_STATUS status
)
69 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
70 who
, status
, NSS_STATUS_SUCCESS
, nss_errno
);
73 static struct passwd
*nss_getpwent(void)
75 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*, char *,
77 (NSS_STATUS (*)(struct passwd
*, char *,
78 size_t, int *))find_fn("getpwent_r");
79 static struct passwd pwd
;
80 static char buf
[1000];
86 status
= _nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &nss_errno
);
87 if (status
== NSS_STATUS_NOTFOUND
) {
90 if (status
!= NSS_STATUS_SUCCESS
) {
91 report_nss_error("getpwent", status
);
97 static struct passwd
*nss_getpwnam(const char *name
)
99 NSS_STATUS (*_nss_getpwnam_r
)(const char *, struct passwd
*, char *,
101 (NSS_STATUS (*)(const char *, struct passwd
*, char *,
102 size_t, int *))find_fn("getpwnam_r");
103 static struct passwd pwd
;
104 static char buf
[1000];
107 if (!_nss_getpwnam_r
)
110 status
= _nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
111 if (status
== NSS_STATUS_NOTFOUND
) {
114 if (status
!= NSS_STATUS_SUCCESS
) {
115 report_nss_error("getpwnam", status
);
121 static struct passwd
*nss_getpwuid(uid_t uid
)
123 NSS_STATUS (*_nss_getpwuid_r
)(uid_t
, struct passwd
*, char *,
125 (NSS_STATUS (*)(uid_t
, struct passwd
*, char *,
126 size_t, int *))find_fn("getpwuid_r");
127 static struct passwd pwd
;
128 static char buf
[1000];
131 if (!_nss_getpwuid_r
)
134 status
= _nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
135 if (status
== NSS_STATUS_NOTFOUND
) {
138 if (status
!= NSS_STATUS_SUCCESS
) {
139 report_nss_error("getpwuid", status
);
145 static void nss_setpwent(void)
147 NSS_STATUS (*_nss_setpwent
)(void) =
148 (NSS_STATUS(*)(void))find_fn("setpwent");
154 status
= _nss_setpwent();
155 if (status
!= NSS_STATUS_SUCCESS
) {
156 report_nss_error("setpwent", status
);
160 static void nss_endpwent(void)
162 NSS_STATUS (*_nss_endpwent
)(void) =
163 (NSS_STATUS (*)(void))find_fn("endpwent");
169 status
= _nss_endpwent();
170 if (status
!= NSS_STATUS_SUCCESS
) {
171 report_nss_error("endpwent", status
);
176 static struct group
*nss_getgrent(void)
178 NSS_STATUS (*_nss_getgrent_r
)(struct group
*, char *,
180 (NSS_STATUS (*)(struct group
*, char *,
181 size_t, int *))find_fn("getgrent_r");
182 static struct group grp
;
184 static int buflen
= 1024;
187 if (!_nss_getgrent_r
)
191 buf
= (char *)malloc(buflen
);
194 status
= _nss_getgrent_r(&grp
, buf
, buflen
, &nss_errno
);
195 if (status
== NSS_STATUS_TRYAGAIN
) {
197 buf
= (char *)realloc(buf
, buflen
);
203 if (status
== NSS_STATUS_NOTFOUND
) {
207 if (status
!= NSS_STATUS_SUCCESS
) {
208 report_nss_error("getgrent", status
);
215 static struct group
*nss_getgrnam(const char *name
)
217 NSS_STATUS (*_nss_getgrnam_r
)(const char *, struct group
*, char *,
219 (NSS_STATUS (*)(const char *, struct group
*, char *,
220 size_t, int *))find_fn("getgrnam_r");
221 static struct group grp
;
223 static int buflen
= 1000;
226 if (!_nss_getgrnam_r
)
230 buf
= (char *)malloc(buflen
);
232 status
= _nss_getgrnam_r(name
, &grp
, buf
, buflen
, &nss_errno
);
233 if (status
== NSS_STATUS_TRYAGAIN
) {
235 buf
= (char *)realloc(buf
, buflen
);
241 if (status
== NSS_STATUS_NOTFOUND
) {
245 if (status
!= NSS_STATUS_SUCCESS
) {
246 report_nss_error("getgrnam", status
);
253 static struct group
*nss_getgrgid(gid_t gid
)
255 NSS_STATUS (*_nss_getgrgid_r
)(gid_t
, struct group
*, char *,
257 (NSS_STATUS (*)(gid_t
, struct group
*, char *,
258 size_t, int *))find_fn("getgrgid_r");
259 static struct group grp
;
261 static int buflen
= 1000;
264 if (!_nss_getgrgid_r
)
268 buf
= (char *)malloc(buflen
);
271 status
= _nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &nss_errno
);
272 if (status
== NSS_STATUS_TRYAGAIN
) {
274 buf
= (char *)realloc(buf
, buflen
);
280 if (status
== NSS_STATUS_NOTFOUND
) {
284 if (status
!= NSS_STATUS_SUCCESS
) {
285 report_nss_error("getgrgid", status
);
292 static void nss_setgrent(void)
294 NSS_STATUS (*_nss_setgrent
)(void) =
295 (NSS_STATUS (*)(void))find_fn("setgrent");
301 status
= _nss_setgrent();
302 if (status
!= NSS_STATUS_SUCCESS
) {
303 report_nss_error("setgrent", status
);
307 static void nss_endgrent(void)
309 NSS_STATUS (*_nss_endgrent
)(void) =
310 (NSS_STATUS (*)(void))find_fn("endgrent");
316 status
= _nss_endgrent();
317 if (status
!= NSS_STATUS_SUCCESS
) {
318 report_nss_error("endgrent", status
);
322 static int nss_initgroups(char *user
, gid_t group
, gid_t
**groups
, long int *start
, long int *size
)
324 NSS_STATUS (*_nss_initgroups
)(char *, gid_t
, long int *,
325 long int *, gid_t
**, long int , int *) =
326 (NSS_STATUS (*)(char *, gid_t
, long int *,
327 long int *, gid_t
**,
328 long int, int *))find_fn("initgroups_dyn");
331 if (!_nss_initgroups
)
332 return NSS_STATUS_UNAVAIL
;
334 status
= _nss_initgroups(user
, group
, start
, size
, groups
, 0, &nss_errno
);
335 if (status
!= NSS_STATUS_SUCCESS
) {
336 report_nss_error("initgroups", status
);
341 static void print_passwd(struct passwd
*pwd
)
343 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
346 (unsigned long)pwd
->pw_uid
,
347 (unsigned long)pwd
->pw_gid
,
353 static void print_group(struct group
*grp
)
359 (unsigned long)grp
->gr_gid
);
361 if (!grp
->gr_mem
[0]) {
366 for (i
=0; grp
->gr_mem
[i
+1]; i
++) {
367 printf("%s,", grp
->gr_mem
[i
]);
369 printf("%s\n", grp
->gr_mem
[i
]);
372 static void nss_test_initgroups(char *name
, gid_t gid
)
376 gid_t
*groups
= NULL
;
380 groups
= (gid_t
*)malloc(size
);
383 status
= nss_initgroups(name
, gid
, &groups
, &start
, &size
);
384 if (status
== NSS_STATUS_UNAVAIL
) {
385 printf("No initgroups fn\n");
389 for (i
=0; i
<start
-1; i
++) {
390 printf("%lu, ", (unsigned long)groups
[i
]);
392 printf("%lu\n", (unsigned long)groups
[i
]);
396 static void nss_test_users(void)
401 /* loop over all users */
402 while ((pwd
= nss_getpwent())) {
403 printf("Testing user %s\n", pwd
->pw_name
);
404 printf("getpwent: "); print_passwd(pwd
);
405 pwd
= nss_getpwuid(pwd
->pw_uid
);
408 printf("ERROR: can't getpwuid\n");
411 printf("getpwuid: "); print_passwd(pwd
);
412 pwd
= nss_getpwnam(pwd
->pw_name
);
415 printf("ERROR: can't getpwnam\n");
418 printf("getpwnam: "); print_passwd(pwd
);
419 printf("initgroups: "); nss_test_initgroups(pwd
->pw_name
, pwd
->pw_gid
);
425 static void nss_test_groups(void)
430 /* loop over all groups */
431 while ((grp
= nss_getgrent())) {
432 printf("Testing group %s\n", grp
->gr_name
);
433 printf("getgrent: "); print_group(grp
);
434 grp
= nss_getgrnam(grp
->gr_name
);
437 printf("ERROR: can't getgrnam\n");
440 printf("getgrnam: "); print_group(grp
);
441 grp
= nss_getgrgid(grp
->gr_gid
);
444 printf("ERROR: can't getgrgid\n");
447 printf("getgrgid: "); print_group(grp
);
453 static void nss_test_errors(void)
458 pwd
= getpwnam("nosuchname");
459 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
461 printf("ERROR Non existant user gave error %d\n", last_error
);
464 pwd
= getpwuid(0xFFF0);
465 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
467 printf("ERROR Non existant uid gave error %d\n", last_error
);
470 grp
= getgrnam("nosuchgroup");
471 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
473 printf("ERROR Non existant group gave error %d\n", last_error
);
476 grp
= getgrgid(0xFFF0);
477 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
479 printf("ERROR Non existant gid gave error %d\n", last_error
);
483 int main(int argc
, char *argv
[])
485 if (argc
> 1) so_path
= argv
[1];
486 if (argc
> 2) nss_name
= argv
[2];
492 printf("total_errors=%d\n", total_errors
);