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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "nsswitch/nsstest.h"
25 static const char *so_path = "/lib/libnss_winbind.so";
26 static const char *nss_name = "winbind";
28 static NSS_STATUS last_error;
29 static int total_errors;
31 static void *find_fn(const char *name)
37 snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
40 h = dlopen(so_path, RTLD_LAZY);
43 printf("Can't open shared library %s\n", so_path);
48 printf("Can't find function %s\n", s);
54 static void report_nss_error(const char *who, NSS_STATUS status)
58 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
59 who, status, NSS_STATUS_SUCCESS, nss_errno);
62 static struct passwd *nss_getpwent(void)
64 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
65 size_t , int *) = find_fn("getpwent_r");
66 static struct passwd pwd;
67 static char buf[1000];
70 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
71 if (status == NSS_STATUS_NOTFOUND) {
74 if (status != NSS_STATUS_SUCCESS) {
75 report_nss_error("getpwent", status);
81 static struct passwd *nss_getpwnam(const char *name)
83 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
84 size_t , int *) = find_fn("getpwnam_r");
85 static struct passwd pwd;
86 static char buf[1000];
89 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
90 if (status == NSS_STATUS_NOTFOUND) {
93 if (status != NSS_STATUS_SUCCESS) {
94 report_nss_error("getpwnam", status);
100 static struct passwd *nss_getpwuid(uid_t uid)
102 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
103 size_t , int *) = find_fn("getpwuid_r");
104 static struct passwd pwd;
105 static char buf[1000];
108 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
109 if (status == NSS_STATUS_NOTFOUND) {
112 if (status != NSS_STATUS_SUCCESS) {
113 report_nss_error("getpwuid", status);
119 static void nss_setpwent(void)
121 NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
123 status = _nss_setpwent();
124 if (status != NSS_STATUS_SUCCESS) {
125 report_nss_error("setpwent", status);
129 static void nss_endpwent(void)
131 NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
133 status = _nss_endpwent();
134 if (status != NSS_STATUS_SUCCESS) {
135 report_nss_error("endpwent", status);
140 static struct group *nss_getgrent(void)
142 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
143 size_t , int *) = find_fn("getgrent_r");
144 static struct group grp;
146 static int buflen = 1024;
149 if (!buf) buf = malloc(buflen);
152 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
153 if (status == NSS_STATUS_TRYAGAIN) {
155 buf = realloc(buf, buflen);
158 if (status == NSS_STATUS_NOTFOUND) {
161 if (status != NSS_STATUS_SUCCESS) {
162 report_nss_error("getgrent", status);
168 static struct group *nss_getgrnam(const char *name)
170 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
171 size_t , int *) = find_fn("getgrnam_r");
172 static struct group grp;
174 static int buflen = 1000;
177 if (!buf) buf = malloc(buflen);
179 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
180 if (status == NSS_STATUS_TRYAGAIN) {
182 buf = realloc(buf, buflen);
185 if (status == NSS_STATUS_NOTFOUND) {
188 if (status != NSS_STATUS_SUCCESS) {
189 report_nss_error("getgrnam", status);
195 static struct group *nss_getgrgid(gid_t gid)
197 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
198 size_t , int *) = find_fn("getgrgid_r");
199 static struct group grp;
201 static int buflen = 1000;
204 if (!buf) buf = malloc(buflen);
206 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
207 if (status == NSS_STATUS_TRYAGAIN) {
209 buf = realloc(buf, buflen);
212 if (status == NSS_STATUS_NOTFOUND) {
215 if (status != NSS_STATUS_SUCCESS) {
216 report_nss_error("getgrgid", status);
222 static void nss_setgrent(void)
224 NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
226 status = _nss_setgrent();
227 if (status != NSS_STATUS_SUCCESS) {
228 report_nss_error("setgrent", status);
232 static void nss_endgrent(void)
234 NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
236 status = _nss_endgrent();
237 if (status != NSS_STATUS_SUCCESS) {
238 report_nss_error("endgrent", status);
242 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
244 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
245 long int *, gid_t **, long int , int *) =
246 find_fn("initgroups_dyn");
249 if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
251 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
252 if (status != NSS_STATUS_SUCCESS) {
253 report_nss_error("initgroups", status);
258 static void print_passwd(struct passwd *pwd)
260 printf("%s:%s:%d:%d:%s:%s:%s\n",
270 static void print_group(struct group *grp)
278 if (!grp->gr_mem[0]) {
283 for (i=0; grp->gr_mem[i+1]; i++) {
284 printf("%s, ", grp->gr_mem[i]);
286 printf("%s\n", grp->gr_mem[i]);
289 static void nss_test_initgroups(char *name, gid_t gid)
293 gid_t *groups = NULL;
297 groups = (gid_t *)malloc_array_p(gid_t, size);
300 status = nss_initgroups(name, gid, &groups, &start, &size);
301 if (status == NSS_STATUS_UNAVAIL) {
302 printf("No initgroups fn\n");
306 for (i=0; i<start-1; i++) {
307 printf("%d, ", groups[i]);
309 printf("%d\n", groups[i]);
313 static void nss_test_users(void)
318 /* loop over all users */
319 while ((pwd = nss_getpwent())) {
320 printf("Testing user %s\n", pwd->pw_name);
321 printf("getpwent: "); print_passwd(pwd);
322 pwd = nss_getpwuid(pwd->pw_uid);
325 printf("ERROR: can't getpwuid\n");
328 printf("getpwuid: "); print_passwd(pwd);
329 pwd = nss_getpwnam(pwd->pw_name);
332 printf("ERROR: can't getpwnam\n");
335 printf("getpwnam: "); print_passwd(pwd);
336 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
342 static void nss_test_groups(void)
347 /* loop over all groups */
348 while ((grp = nss_getgrent())) {
349 printf("Testing group %s\n", grp->gr_name);
350 printf("getgrent: "); print_group(grp);
351 grp = nss_getgrnam(grp->gr_name);
354 printf("ERROR: can't getgrnam\n");
357 printf("getgrnam: "); print_group(grp);
358 grp = nss_getgrgid(grp->gr_gid);
361 printf("ERROR: can't getgrgid\n");
364 printf("getgrgid: "); print_group(grp);
370 static void nss_test_errors(void)
375 pwd = getpwnam("nosuchname");
376 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
378 printf("ERROR Non existant user gave error %d\n", last_error);
381 pwd = getpwuid(0xFFF0);
382 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
384 printf("ERROR Non existant uid gave error %d\n", last_error);
387 grp = getgrnam("nosuchgroup");
388 if (grp || last_error != NSS_STATUS_NOTFOUND) {
390 printf("ERROR Non existant group gave error %d\n", last_error);
393 grp = getgrgid(0xFFF0);
394 if (grp || last_error != NSS_STATUS_NOTFOUND) {
396 printf("ERROR Non existant gid gave error %d\n", last_error);
400 int main(int argc, char *argv[])
402 if (argc > 1) so_path = argv[1];
403 if (argc > 2) nss_name = argv[2];
409 printf("total_errors=%d\n", total_errors);