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/>.
23 static const char *so_path = "/lib/libnss_winbind.so";
24 static const char *nss_name = "winbind";
26 static NSS_STATUS last_error;
27 static int total_errors;
29 static void *find_fn(const char *name)
35 pstr_sprintf(s, "_nss_%s_%s", nss_name, name);
38 h = sys_dlopen(so_path, RTLD_LAZY);
41 printf("Can't open shared library %s\n", so_path);
44 res = sys_dlsym(h, s);
46 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 *,
65 (NSS_STATUS (*)(struct passwd *, char *,
66 size_t, int *))find_fn("getpwent_r");
67 static struct passwd pwd;
68 static char buf[1000];
74 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
75 if (status == NSS_STATUS_NOTFOUND) {
78 if (status != NSS_STATUS_SUCCESS) {
79 report_nss_error("getpwent", status);
85 static struct passwd *nss_getpwnam(const char *name)
87 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
89 (NSS_STATUS (*)(const char *, struct passwd *, char *,
90 size_t, int *))find_fn("getpwnam_r");
91 static struct passwd pwd;
92 static char buf[1000];
98 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
99 if (status == NSS_STATUS_NOTFOUND) {
102 if (status != NSS_STATUS_SUCCESS) {
103 report_nss_error("getpwnam", status);
109 static struct passwd *nss_getpwuid(uid_t uid)
111 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
113 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
114 size_t, int *))find_fn("getpwuid_r");
115 static struct passwd pwd;
116 static char buf[1000];
119 if (!_nss_getpwuid_r)
122 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
123 if (status == NSS_STATUS_NOTFOUND) {
126 if (status != NSS_STATUS_SUCCESS) {
127 report_nss_error("getpwuid", status);
133 static void nss_setpwent(void)
135 NSS_STATUS (*_nss_setpwent)(void) =
136 (NSS_STATUS(*)(void))find_fn("setpwent");
142 status = _nss_setpwent();
143 if (status != NSS_STATUS_SUCCESS) {
144 report_nss_error("setpwent", status);
148 static void nss_endpwent(void)
150 NSS_STATUS (*_nss_endpwent)(void) =
151 (NSS_STATUS (*)(void))find_fn("endpwent");
157 status = _nss_endpwent();
158 if (status != NSS_STATUS_SUCCESS) {
159 report_nss_error("endpwent", status);
164 static struct group *nss_getgrent(void)
166 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
168 (NSS_STATUS (*)(struct group *, char *,
169 size_t, int *))find_fn("getgrent_r");
170 static struct group grp;
172 static int buflen = 1024;
175 if (!_nss_getgrent_r)
179 buf = SMB_MALLOC_ARRAY(char, buflen);
182 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
183 if (status == NSS_STATUS_TRYAGAIN) {
185 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
191 if (status == NSS_STATUS_NOTFOUND) {
195 if (status != NSS_STATUS_SUCCESS) {
196 report_nss_error("getgrent", status);
204 static struct group *nss_getgrnam(const char *name)
206 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
208 (NSS_STATUS (*)(const char *, struct group *, char *,
209 size_t, int *))find_fn("getgrnam_r");
210 static struct group grp;
212 static int buflen = 1000;
215 if (!_nss_getgrnam_r)
219 buf = SMB_MALLOC_ARRAY(char, buflen);
221 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
222 if (status == NSS_STATUS_TRYAGAIN) {
224 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
230 if (status == NSS_STATUS_NOTFOUND) {
234 if (status != NSS_STATUS_SUCCESS) {
235 report_nss_error("getgrnam", status);
243 static struct group *nss_getgrgid(gid_t gid)
245 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
247 (NSS_STATUS (*)(gid_t, struct group *, char *,
248 size_t, int *))find_fn("getgrgid_r");
249 static struct group grp;
251 static int buflen = 1000;
254 if (!_nss_getgrgid_r)
258 buf = SMB_MALLOC_ARRAY(char, buflen);
261 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
262 if (status == NSS_STATUS_TRYAGAIN) {
264 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
270 if (status == NSS_STATUS_NOTFOUND) {
274 if (status != NSS_STATUS_SUCCESS) {
275 report_nss_error("getgrgid", status);
283 static void nss_setgrent(void)
285 NSS_STATUS (*_nss_setgrent)(void) =
286 (NSS_STATUS (*)(void))find_fn("setgrent");
292 status = _nss_setgrent();
293 if (status != NSS_STATUS_SUCCESS) {
294 report_nss_error("setgrent", status);
298 static void nss_endgrent(void)
300 NSS_STATUS (*_nss_endgrent)(void) =
301 (NSS_STATUS (*)(void))find_fn("endgrent");
307 status = _nss_endgrent();
308 if (status != NSS_STATUS_SUCCESS) {
309 report_nss_error("endgrent", status);
313 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
315 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
316 long int *, gid_t **, long int , int *) =
317 (NSS_STATUS (*)(char *, gid_t, long int *,
318 long int *, gid_t **,
319 long int, int *))find_fn("initgroups_dyn");
322 if (!_nss_initgroups)
323 return NSS_STATUS_UNAVAIL;
325 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
326 if (status != NSS_STATUS_SUCCESS) {
327 report_nss_error("initgroups", status);
332 static void print_passwd(struct passwd *pwd)
334 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
337 (unsigned long)pwd->pw_uid,
338 (unsigned long)pwd->pw_gid,
344 static void print_group(struct group *grp)
347 printf("%s:%s:%lu: ",
350 (unsigned long)grp->gr_gid);
352 if (!grp->gr_mem[0]) {
357 for (i=0; grp->gr_mem[i+1]; i++) {
358 printf("%s, ", grp->gr_mem[i]);
360 printf("%s\n", grp->gr_mem[i]);
363 static void nss_test_initgroups(char *name, gid_t gid)
367 gid_t *groups = NULL;
371 groups = SMB_MALLOC_ARRAY(gid_t, size);
374 status = nss_initgroups(name, gid, &groups, &start, &size);
375 if (status == NSS_STATUS_UNAVAIL) {
376 printf("No initgroups fn\n");
380 for (i=0; i<start-1; i++) {
381 printf("%lu, ", (unsigned long)groups[i]);
383 printf("%lu\n", (unsigned long)groups[i]);
387 static void nss_test_users(void)
392 /* loop over all users */
393 while ((pwd = nss_getpwent())) {
394 printf("Testing user %s\n", pwd->pw_name);
395 printf("getpwent: "); print_passwd(pwd);
396 pwd = nss_getpwuid(pwd->pw_uid);
399 printf("ERROR: can't getpwuid\n");
402 printf("getpwuid: "); print_passwd(pwd);
403 pwd = nss_getpwnam(pwd->pw_name);
406 printf("ERROR: can't getpwnam\n");
409 printf("getpwnam: "); print_passwd(pwd);
410 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
416 static void nss_test_groups(void)
421 /* loop over all groups */
422 while ((grp = nss_getgrent())) {
423 printf("Testing group %s\n", grp->gr_name);
424 printf("getgrent: "); print_group(grp);
425 grp = nss_getgrnam(grp->gr_name);
428 printf("ERROR: can't getgrnam\n");
431 printf("getgrnam: "); print_group(grp);
432 grp = nss_getgrgid(grp->gr_gid);
435 printf("ERROR: can't getgrgid\n");
438 printf("getgrgid: "); print_group(grp);
444 static void nss_test_errors(void)
449 pwd = getpwnam("nosuchname");
450 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
452 printf("ERROR Non existant user gave error %d\n", last_error);
455 pwd = getpwuid(0xFFF0);
456 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
458 printf("ERROR Non existant uid gave error %d\n", last_error);
461 grp = getgrnam("nosuchgroup");
462 if (grp || last_error != NSS_STATUS_NOTFOUND) {
464 printf("ERROR Non existant group gave error %d\n", last_error);
467 grp = getgrgid(0xFFF0);
468 if (grp || last_error != NSS_STATUS_NOTFOUND) {
470 printf("ERROR Non existant gid gave error %d\n", last_error);
474 int main(int argc, char *argv[])
476 if (argc > 1) so_path = argv[1];
477 if (argc > 2) nss_name = argv[2];
483 printf("total_errors=%d\n", total_errors);