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 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
26 static const char *so_path
= "/lib/libnss_winbind.so";
27 static const char *nss_name
= "winbind";
29 static NSS_STATUS last_error
;
30 static int total_errors
;
32 static void *find_fn(const char *name
)
38 if (asprintf(&s
, "_nss_%s_%s", nss_name
, name
) < 0) {
43 h
= dlopen(so_path
, RTLD_LAZY
);
46 printf("Can't open shared library %s\n", so_path
);
51 printf("Can't find function %s\n", s
);
60 static void report_nss_error(const char *who
, NSS_STATUS status
)
64 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
65 who
, status
, NSS_STATUS_SUCCESS
, nss_errno
);
68 static struct passwd
*nss_getpwent(void)
70 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*, char *,
72 (NSS_STATUS (*)(struct passwd
*, char *,
73 size_t, int *))find_fn("getpwent_r");
74 static struct passwd pwd
;
75 static char buf
[1000];
81 status
= _nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &nss_errno
);
82 if (status
== NSS_STATUS_NOTFOUND
) {
85 if (status
!= NSS_STATUS_SUCCESS
) {
86 report_nss_error("getpwent", status
);
92 static struct passwd
*nss_getpwnam(const char *name
)
94 NSS_STATUS (*_nss_getpwnam_r
)(const char *, struct passwd
*, char *,
96 (NSS_STATUS (*)(const char *, struct passwd
*, char *,
97 size_t, int *))find_fn("getpwnam_r");
98 static struct passwd pwd
;
99 static char buf
[1000];
102 if (!_nss_getpwnam_r
)
105 status
= _nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
106 if (status
== NSS_STATUS_NOTFOUND
) {
109 if (status
!= NSS_STATUS_SUCCESS
) {
110 report_nss_error("getpwnam", status
);
116 static struct passwd
*nss_getpwuid(uid_t uid
)
118 NSS_STATUS (*_nss_getpwuid_r
)(uid_t
, struct passwd
*, char *,
120 (NSS_STATUS (*)(uid_t
, struct passwd
*, char *,
121 size_t, int *))find_fn("getpwuid_r");
122 static struct passwd pwd
;
123 static char buf
[1000];
126 if (!_nss_getpwuid_r
)
129 status
= _nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
130 if (status
== NSS_STATUS_NOTFOUND
) {
133 if (status
!= NSS_STATUS_SUCCESS
) {
134 report_nss_error("getpwuid", status
);
140 static void samba_nss_setpwent(void)
142 NSS_STATUS (*_nss_setpwent
)(void) =
143 (NSS_STATUS(*)(void))find_fn("setpwent");
149 status
= _nss_setpwent();
150 if (status
!= NSS_STATUS_SUCCESS
) {
151 report_nss_error("setpwent", status
);
155 static void samba_nss_endpwent(void)
157 NSS_STATUS (*_nss_endpwent
)(void) =
158 (NSS_STATUS (*)(void))find_fn("endpwent");
164 status
= _nss_endpwent();
165 if (status
!= NSS_STATUS_SUCCESS
) {
166 report_nss_error("endpwent", status
);
171 static struct group
*nss_getgrent(void)
173 NSS_STATUS (*_nss_getgrent_r
)(struct group
*, char *,
175 (NSS_STATUS (*)(struct group
*, char *,
176 size_t, int *))find_fn("getgrent_r");
177 static struct group grp
;
179 static int buflen
= 1024;
182 if (!_nss_getgrent_r
)
186 buf
= (char *)malloc(buflen
);
189 status
= _nss_getgrent_r(&grp
, buf
, buflen
, &nss_errno
);
190 if (status
== NSS_STATUS_TRYAGAIN
) {
193 buf
= (char *)realloc(buf
, buflen
);
200 if (status
== NSS_STATUS_NOTFOUND
) {
204 if (status
!= NSS_STATUS_SUCCESS
) {
205 report_nss_error("getgrent", status
);
212 static struct group
*nss_getgrnam(const char *name
)
214 NSS_STATUS (*_nss_getgrnam_r
)(const char *, struct group
*, char *,
216 (NSS_STATUS (*)(const char *, struct group
*, char *,
217 size_t, int *))find_fn("getgrnam_r");
218 static struct group grp
;
220 static int buflen
= 1000;
223 if (!_nss_getgrnam_r
)
227 buf
= (char *)malloc(buflen
);
229 status
= _nss_getgrnam_r(name
, &grp
, buf
, buflen
, &nss_errno
);
230 if (status
== NSS_STATUS_TRYAGAIN
) {
233 buf
= (char *)realloc(buf
, buflen
);
240 if (status
== NSS_STATUS_NOTFOUND
) {
244 if (status
!= NSS_STATUS_SUCCESS
) {
245 report_nss_error("getgrnam", status
);
252 static struct group
*nss_getgrgid(gid_t gid
)
254 NSS_STATUS (*_nss_getgrgid_r
)(gid_t
, struct group
*, char *,
256 (NSS_STATUS (*)(gid_t
, struct group
*, char *,
257 size_t, int *))find_fn("getgrgid_r");
258 static struct group grp
;
260 static int buflen
= 1000;
263 if (!_nss_getgrgid_r
)
267 buf
= (char *)malloc(buflen
);
270 status
= _nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &nss_errno
);
271 if (status
== NSS_STATUS_TRYAGAIN
) {
274 buf
= (char *)realloc(buf
, buflen
);
281 if (status
== NSS_STATUS_NOTFOUND
) {
285 if (status
!= NSS_STATUS_SUCCESS
) {
286 report_nss_error("getgrgid", status
);
293 static void samba_nss_setgrent(void)
295 NSS_STATUS (*_nss_setgrent
)(void) =
296 (NSS_STATUS (*)(void))find_fn("setgrent");
302 status
= _nss_setgrent();
303 if (status
!= NSS_STATUS_SUCCESS
) {
304 report_nss_error("setgrent", status
);
308 static void samba_nss_endgrent(void)
310 NSS_STATUS (*_nss_endgrent
)(void) =
311 (NSS_STATUS (*)(void))find_fn("endgrent");
317 status
= _nss_endgrent();
318 if (status
!= NSS_STATUS_SUCCESS
) {
319 report_nss_error("endgrent", status
);
323 static int nss_initgroups(char *user
, gid_t group
, gid_t
**groups
, long int *start
, long int *size
)
325 NSS_STATUS (*_nss_initgroups
)(char *, gid_t
, long int *,
326 long int *, gid_t
**, long int , int *) =
327 (NSS_STATUS (*)(char *, gid_t
, long int *,
328 long int *, gid_t
**,
329 long int, int *))find_fn("initgroups_dyn");
332 if (!_nss_initgroups
)
333 return NSS_STATUS_UNAVAIL
;
335 status
= _nss_initgroups(user
, group
, start
, size
, groups
, 0, &nss_errno
);
336 if (status
!= NSS_STATUS_SUCCESS
) {
337 report_nss_error("initgroups", status
);
342 static void print_passwd(struct passwd
*pwd
)
344 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
347 (unsigned long)pwd
->pw_uid
,
348 (unsigned long)pwd
->pw_gid
,
354 static void print_group(struct group
*grp
)
360 (unsigned long)grp
->gr_gid
);
362 if (!grp
->gr_mem
[0]) {
367 for (i
=0; grp
->gr_mem
[i
+1]; i
++) {
368 printf("%s,", grp
->gr_mem
[i
]);
370 printf("%s\n", grp
->gr_mem
[i
]);
373 static void nss_test_initgroups(char *name
, gid_t gid
)
377 gid_t
*groups
= NULL
;
381 groups
= (gid_t
*)malloc(sizeof(gid_t
) * size
);
382 if (groups
== NULL
) {
383 printf("Unable to allocate memory for groups\n");
388 status
= nss_initgroups(name
, gid
, &groups
, &start
, &size
);
389 if (status
== NSS_STATUS_UNAVAIL
) {
390 printf("No initgroups fn\n");
394 for (i
=0; i
<start
-1; i
++) {
395 printf("%lu, ", (unsigned long)groups
[i
]);
397 printf("%lu\n", (unsigned long)groups
[i
]);
402 static void nss_test_users(void)
406 samba_nss_setpwent();
407 /* loop over all users */
408 while ((pwd
= nss_getpwent())) {
409 printf("Testing user %s\n", pwd
->pw_name
);
410 printf("getpwent: "); print_passwd(pwd
);
411 pwd
= nss_getpwuid(pwd
->pw_uid
);
414 printf("ERROR: can't getpwuid\n");
417 printf("getpwuid: "); print_passwd(pwd
);
418 pwd
= nss_getpwnam(pwd
->pw_name
);
421 printf("ERROR: can't getpwnam\n");
424 printf("getpwnam: "); print_passwd(pwd
);
425 printf("initgroups: "); nss_test_initgroups(pwd
->pw_name
, pwd
->pw_gid
);
428 samba_nss_endpwent();
431 static void nss_test_groups(void)
435 samba_nss_setgrent();
436 /* loop over all groups */
437 while ((grp
= nss_getgrent())) {
438 printf("Testing group %s\n", grp
->gr_name
);
439 printf("getgrent: "); print_group(grp
);
440 grp
= nss_getgrnam(grp
->gr_name
);
443 printf("ERROR: can't getgrnam\n");
446 printf("getgrnam: "); print_group(grp
);
447 grp
= nss_getgrgid(grp
->gr_gid
);
450 printf("ERROR: can't getgrgid\n");
453 printf("getgrgid: "); print_group(grp
);
456 samba_nss_endgrent();
459 static void nss_test_errors(void)
464 pwd
= getpwnam("nosuchname");
465 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
467 printf("ERROR Non existent user gave error %d\n", last_error
);
470 pwd
= getpwuid(0xFF00);
471 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
473 printf("ERROR Non existent uid gave error %d\n", last_error
);
476 grp
= getgrnam("nosuchgroup");
477 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
479 printf("ERROR Non existent group gave error %d\n", last_error
);
482 grp
= getgrgid(0xFFF0);
483 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
485 printf("ERROR Non existent gid gave error %d\n", last_error
);
489 int main(int argc
, char *argv
[])
491 if (argc
> 1) so_path
= argv
[1];
492 if (argc
> 2) nss_name
= argv
[2];
498 printf("total_errors=%d\n", total_errors
);