2 Unix SMB/CIFS implementation.
3 nss tester for winbindd
4 Copyright (C) Andrew Tridgell 2001
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 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 snprintf(s
,sizeof(s
), "_nss_%s_%s", nss_name
, name
);
39 h
= dlopen(so_path
, RTLD_LAZY
);
42 printf("Can't open shared library %s\n", so_path
);
47 printf("Can't find function %s\n", s
);
53 static void report_nss_error(const char *who
, NSS_STATUS status
)
57 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
58 who
, status
, NSS_STATUS_SUCCESS
, nss_errno
);
61 static struct passwd
*nss_getpwent(void)
63 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*, char *,
64 size_t , int *) = find_fn("getpwent_r");
65 static struct passwd pwd
;
66 static char buf
[1000];
69 status
= _nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &nss_errno
);
70 if (status
== NSS_STATUS_NOTFOUND
) {
73 if (status
!= NSS_STATUS_SUCCESS
) {
74 report_nss_error("getpwent", status
);
80 static struct passwd
*nss_getpwnam(const char *name
)
82 NSS_STATUS (*_nss_getpwnam_r
)(const char *, struct passwd
*, char *,
83 size_t , int *) = find_fn("getpwnam_r");
84 static struct passwd pwd
;
85 static char buf
[1000];
88 status
= _nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
89 if (status
== NSS_STATUS_NOTFOUND
) {
92 if (status
!= NSS_STATUS_SUCCESS
) {
93 report_nss_error("getpwnam", status
);
99 static struct passwd
*nss_getpwuid(uid_t uid
)
101 NSS_STATUS (*_nss_getpwuid_r
)(uid_t
, struct passwd
*, char *,
102 size_t , int *) = find_fn("getpwuid_r");
103 static struct passwd pwd
;
104 static char buf
[1000];
107 status
= _nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
108 if (status
== NSS_STATUS_NOTFOUND
) {
111 if (status
!= NSS_STATUS_SUCCESS
) {
112 report_nss_error("getpwuid", status
);
118 static void nss_setpwent(void)
120 NSS_STATUS (*_nss_setpwent
)(void) = find_fn("setpwent");
122 status
= _nss_setpwent();
123 if (status
!= NSS_STATUS_SUCCESS
) {
124 report_nss_error("setpwent", status
);
128 static void nss_endpwent(void)
130 NSS_STATUS (*_nss_endpwent
)(void) = find_fn("endpwent");
132 status
= _nss_endpwent();
133 if (status
!= NSS_STATUS_SUCCESS
) {
134 report_nss_error("endpwent", status
);
139 static struct group
*nss_getgrent(void)
141 NSS_STATUS (*_nss_getgrent_r
)(struct group
*, char *,
142 size_t , int *) = find_fn("getgrent_r");
143 static struct group grp
;
145 static int buflen
= 1024;
148 if (!buf
) buf
= malloc_array_p(char, buflen
);
151 status
= _nss_getgrent_r(&grp
, buf
, buflen
, &nss_errno
);
152 if (status
== NSS_STATUS_TRYAGAIN
) {
154 buf
= realloc_p(buf
, char, buflen
);
157 if (status
== NSS_STATUS_NOTFOUND
) {
160 if (status
!= NSS_STATUS_SUCCESS
) {
161 report_nss_error("getgrent", status
);
167 static struct group
*nss_getgrnam(const char *name
)
169 NSS_STATUS (*_nss_getgrnam_r
)(const char *, struct group
*, char *,
170 size_t , int *) = find_fn("getgrnam_r");
171 static struct group grp
;
173 static int buflen
= 1000;
176 if (!buf
) buf
= malloc_array_p(char, buflen
);
178 status
= _nss_getgrnam_r(name
, &grp
, buf
, buflen
, &nss_errno
);
179 if (status
== NSS_STATUS_TRYAGAIN
) {
181 buf
= realloc_p(buf
, char, buflen
);
184 if (status
== NSS_STATUS_NOTFOUND
) {
187 if (status
!= NSS_STATUS_SUCCESS
) {
188 report_nss_error("getgrnam", status
);
194 static struct group
*nss_getgrgid(gid_t gid
)
196 NSS_STATUS (*_nss_getgrgid_r
)(gid_t
, struct group
*, char *,
197 size_t , int *) = find_fn("getgrgid_r");
198 static struct group grp
;
200 static int buflen
= 1000;
203 if (!buf
) buf
= malloc_array_p(char, buflen
);
205 status
= _nss_getgrgid_r(gid
, &grp
, buf
, buflen
, &nss_errno
);
206 if (status
== NSS_STATUS_TRYAGAIN
) {
208 buf
= realloc_p(buf
, char, buflen
);
211 if (status
== NSS_STATUS_NOTFOUND
) {
214 if (status
!= NSS_STATUS_SUCCESS
) {
215 report_nss_error("getgrgid", status
);
221 static void nss_setgrent(void)
223 NSS_STATUS (*_nss_setgrent
)(void) = find_fn("setgrent");
225 status
= _nss_setgrent();
226 if (status
!= NSS_STATUS_SUCCESS
) {
227 report_nss_error("setgrent", status
);
231 static void nss_endgrent(void)
233 NSS_STATUS (*_nss_endgrent
)(void) = find_fn("endgrent");
235 status
= _nss_endgrent();
236 if (status
!= NSS_STATUS_SUCCESS
) {
237 report_nss_error("endgrent", status
);
241 static int nss_initgroups(char *user
, gid_t group
, gid_t
**groups
, long int *start
, long int *size
)
243 NSS_STATUS (*_nss_initgroups
)(char *, gid_t
, long int *,
244 long int *, gid_t
**, long int , int *) =
245 find_fn("initgroups_dyn");
248 if (!_nss_initgroups
) return NSS_STATUS_UNAVAIL
;
250 status
= _nss_initgroups(user
, group
, start
, size
, groups
, 0, &nss_errno
);
251 if (status
!= NSS_STATUS_SUCCESS
) {
252 report_nss_error("initgroups", status
);
257 static void print_passwd(struct passwd
*pwd
)
259 printf("%s:%s:%d:%d:%s:%s:%s\n",
269 static void print_group(struct group
*grp
)
277 if (!grp
->gr_mem
[0]) {
282 for (i
=0; grp
->gr_mem
[i
+1]; i
++) {
283 printf("%s, ", grp
->gr_mem
[i
]);
285 printf("%s\n", grp
->gr_mem
[i
]);
288 static void nss_test_initgroups(char *name
, gid_t gid
)
292 gid_t
*groups
= NULL
;
296 groups
= (gid_t
*)malloc_array_p(gid_t
, size
);
299 status
= nss_initgroups(name
, gid
, &groups
, &start
, &size
);
300 if (status
== NSS_STATUS_UNAVAIL
) {
301 printf("No initgroups fn\n");
305 for (i
=0; i
<start
-1; i
++) {
306 printf("%d, ", groups
[i
]);
308 printf("%d\n", groups
[i
]);
312 static void nss_test_users(void)
317 /* loop over all users */
318 while ((pwd
= nss_getpwent())) {
319 printf("Testing user %s\n", pwd
->pw_name
);
320 printf("getpwent: "); print_passwd(pwd
);
321 pwd
= nss_getpwuid(pwd
->pw_uid
);
324 printf("ERROR: can't getpwuid\n");
327 printf("getpwuid: "); print_passwd(pwd
);
328 pwd
= nss_getpwnam(pwd
->pw_name
);
331 printf("ERROR: can't getpwnam\n");
334 printf("getpwnam: "); print_passwd(pwd
);
335 printf("initgroups: "); nss_test_initgroups(pwd
->pw_name
, pwd
->pw_gid
);
341 static void nss_test_groups(void)
346 /* loop over all groups */
347 while ((grp
= nss_getgrent())) {
348 printf("Testing group %s\n", grp
->gr_name
);
349 printf("getgrent: "); print_group(grp
);
350 grp
= nss_getgrnam(grp
->gr_name
);
353 printf("ERROR: can't getgrnam\n");
356 printf("getgrnam: "); print_group(grp
);
357 grp
= nss_getgrgid(grp
->gr_gid
);
360 printf("ERROR: can't getgrgid\n");
363 printf("getgrgid: "); print_group(grp
);
369 static void nss_test_errors(void)
374 pwd
= getpwnam("nosuchname");
375 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
377 printf("ERROR Non existant user gave error %d\n", last_error
);
380 pwd
= getpwuid(0xFFF0);
381 if (pwd
|| last_error
!= NSS_STATUS_NOTFOUND
) {
383 printf("ERROR Non existant uid gave error %d\n", last_error
);
386 grp
= getgrnam("nosuchgroup");
387 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
389 printf("ERROR Non existant group gave error %d\n", last_error
);
392 grp
= getgrgid(0xFFF0);
393 if (grp
|| last_error
!= NSS_STATUS_NOTFOUND
) {
395 printf("ERROR Non existant gid gave error %d\n", last_error
);
399 int main(int argc
, char *argv
[])
401 if (argc
> 1) so_path
= argv
[1];
402 if (argc
> 2) nss_name
= argv
[2];
408 printf("total_errors=%d\n", total_errors
);