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/>.
30 static const char *so_path = "/lib/libnss_winbind.so";
31 static const char *nss_name = "winbind";
33 static NSS_STATUS last_error;
34 static int total_errors;
36 static void *find_fn(const char *name)
42 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
47 h = dlopen(so_path, RTLD_LAZY);
50 printf("Can't open shared library %s\n", so_path);
55 printf("Can't find function %s\n", s);
64 static void report_nss_error(const char *who, NSS_STATUS status)
68 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
69 who, status, NSS_STATUS_SUCCESS, nss_errno);
72 static struct passwd *nss_getpwent(void)
74 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
76 (NSS_STATUS (*)(struct passwd *, char *,
77 size_t, int *))find_fn("getpwent_r");
78 static struct passwd pwd;
79 static char buf[1000];
85 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
86 if (status == NSS_STATUS_NOTFOUND) {
89 if (status != NSS_STATUS_SUCCESS) {
90 report_nss_error("getpwent", status);
96 static struct passwd *nss_getpwnam(const char *name)
98 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
100 (NSS_STATUS (*)(const char *, struct passwd *, char *,
101 size_t, int *))find_fn("getpwnam_r");
102 static struct passwd pwd;
103 static char buf[1000];
106 if (!_nss_getpwnam_r)
109 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
110 if (status == NSS_STATUS_NOTFOUND) {
113 if (status != NSS_STATUS_SUCCESS) {
114 report_nss_error("getpwnam", status);
120 static struct passwd *nss_getpwuid(uid_t uid)
122 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
124 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
125 size_t, int *))find_fn("getpwuid_r");
126 static struct passwd pwd;
127 static char buf[1000];
130 if (!_nss_getpwuid_r)
133 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
134 if (status == NSS_STATUS_NOTFOUND) {
137 if (status != NSS_STATUS_SUCCESS) {
138 report_nss_error("getpwuid", status);
144 static void nss_setpwent(void)
146 NSS_STATUS (*_nss_setpwent)(void) =
147 (NSS_STATUS(*)(void))find_fn("setpwent");
153 status = _nss_setpwent();
154 if (status != NSS_STATUS_SUCCESS) {
155 report_nss_error("setpwent", status);
159 static void nss_endpwent(void)
161 NSS_STATUS (*_nss_endpwent)(void) =
162 (NSS_STATUS (*)(void))find_fn("endpwent");
168 status = _nss_endpwent();
169 if (status != NSS_STATUS_SUCCESS) {
170 report_nss_error("endpwent", status);
175 static struct group *nss_getgrent(void)
177 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
179 (NSS_STATUS (*)(struct group *, char *,
180 size_t, int *))find_fn("getgrent_r");
181 static struct group grp;
183 static int buflen = 1024;
186 if (!_nss_getgrent_r)
190 buf = (char *)malloc(buflen);
193 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
194 if (status == NSS_STATUS_TRYAGAIN) {
196 buf = (char *)realloc(buf, buflen);
202 if (status == NSS_STATUS_NOTFOUND) {
206 if (status != NSS_STATUS_SUCCESS) {
207 report_nss_error("getgrent", status);
214 static struct group *nss_getgrnam(const char *name)
216 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
218 (NSS_STATUS (*)(const char *, struct group *, char *,
219 size_t, int *))find_fn("getgrnam_r");
220 static struct group grp;
222 static int buflen = 1000;
225 if (!_nss_getgrnam_r)
229 buf = (char *)malloc(buflen);
231 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
232 if (status == NSS_STATUS_TRYAGAIN) {
234 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) {
273 buf = (char *)realloc(buf, buflen);
279 if (status == NSS_STATUS_NOTFOUND) {
283 if (status != NSS_STATUS_SUCCESS) {
284 report_nss_error("getgrgid", status);
291 static void nss_setgrent(void)
293 NSS_STATUS (*_nss_setgrent)(void) =
294 (NSS_STATUS (*)(void))find_fn("setgrent");
300 status = _nss_setgrent();
301 if (status != NSS_STATUS_SUCCESS) {
302 report_nss_error("setgrent", status);
306 static void nss_endgrent(void)
308 NSS_STATUS (*_nss_endgrent)(void) =
309 (NSS_STATUS (*)(void))find_fn("endgrent");
315 status = _nss_endgrent();
316 if (status != NSS_STATUS_SUCCESS) {
317 report_nss_error("endgrent", status);
321 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
323 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
324 long int *, gid_t **, long int , int *) =
325 (NSS_STATUS (*)(char *, gid_t, long int *,
326 long int *, gid_t **,
327 long int, int *))find_fn("initgroups_dyn");
330 if (!_nss_initgroups)
331 return NSS_STATUS_UNAVAIL;
333 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
334 if (status != NSS_STATUS_SUCCESS) {
335 report_nss_error("initgroups", status);
340 static void print_passwd(struct passwd *pwd)
342 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
345 (unsigned long)pwd->pw_uid,
346 (unsigned long)pwd->pw_gid,
352 static void print_group(struct group *grp)
358 (unsigned long)grp->gr_gid);
360 if (!grp->gr_mem[0]) {
365 for (i=0; grp->gr_mem[i+1]; i++) {
366 printf("%s,", grp->gr_mem[i]);
368 printf("%s\n", grp->gr_mem[i]);
371 static void nss_test_initgroups(char *name, gid_t gid)
375 gid_t *groups = NULL;
379 groups = (gid_t *)malloc(size);
382 status = nss_initgroups(name, gid, &groups, &start, &size);
383 if (status == NSS_STATUS_UNAVAIL) {
384 printf("No initgroups fn\n");
388 for (i=0; i<start-1; i++) {
389 printf("%lu, ", (unsigned long)groups[i]);
391 printf("%lu\n", (unsigned long)groups[i]);
395 static void nss_test_users(void)
400 /* loop over all users */
401 while ((pwd = nss_getpwent())) {
402 printf("Testing user %s\n", pwd->pw_name);
403 printf("getpwent: "); print_passwd(pwd);
404 pwd = nss_getpwuid(pwd->pw_uid);
407 printf("ERROR: can't getpwuid\n");
410 printf("getpwuid: "); print_passwd(pwd);
411 pwd = nss_getpwnam(pwd->pw_name);
414 printf("ERROR: can't getpwnam\n");
417 printf("getpwnam: "); print_passwd(pwd);
418 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
424 static void nss_test_groups(void)
429 /* loop over all groups */
430 while ((grp = nss_getgrent())) {
431 printf("Testing group %s\n", grp->gr_name);
432 printf("getgrent: "); print_group(grp);
433 grp = nss_getgrnam(grp->gr_name);
436 printf("ERROR: can't getgrnam\n");
439 printf("getgrnam: "); print_group(grp);
440 grp = nss_getgrgid(grp->gr_gid);
443 printf("ERROR: can't getgrgid\n");
446 printf("getgrgid: "); print_group(grp);
452 static void nss_test_errors(void)
457 pwd = getpwnam("nosuchname");
458 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
460 printf("ERROR Non existant user gave error %d\n", last_error);
463 pwd = getpwuid(0xFFF0);
464 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
466 printf("ERROR Non existant uid gave error %d\n", last_error);
469 grp = getgrnam("nosuchgroup");
470 if (grp || last_error != NSS_STATUS_NOTFOUND) {
472 printf("ERROR Non existant group gave error %d\n", last_error);
475 grp = getgrgid(0xFFF0);
476 if (grp || last_error != NSS_STATUS_NOTFOUND) {
478 printf("ERROR Non existant gid gave error %d\n", last_error);
482 int main(int argc, char *argv[])
484 if (argc > 1) so_path = argv[1];
485 if (argc > 2) nss_name = argv[2];
491 printf("total_errors=%d\n", total_errors);