s3:winbind add wcache_tdc_fetch_domainbysid
[Samba.git] / nsswitch / nsstest.c
blob8742b328c704e7cbf19d27c1b4c7e4674023d16f
1 /*
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/>.
21 #include "includes.h"
22 #include "nsswitch/nsstest.h"
24 #ifdef malloc
25 #undef malloc
26 #endif
27 #ifdef realloc
28 #undef realloc
29 #endif
31 static const char *so_path = "/lib/libnss_winbind.so";
32 static const char *nss_name = "winbind";
33 static int nss_errno;
34 static NSS_STATUS last_error;
35 static int total_errors;
37 static void *find_fn(const char *name)
39 char *s;
40 static void *h;
41 void *res;
43 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
44 exit(1);
47 if (!h) {
48 h = dlopen(so_path, RTLD_LAZY);
50 if (!h) {
51 printf("Can't open shared library %s\n", so_path);
52 exit(1);
54 res = dlsym(h, s);
55 if (!res) {
56 printf("Can't find function %s\n", s);
57 total_errors++;
58 SAFE_FREE(s);
59 return NULL;
61 SAFE_FREE(s);
62 return res;
65 static void report_nss_error(const char *who, NSS_STATUS status)
67 last_error = status;
68 total_errors++;
69 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
70 who, status, NSS_STATUS_SUCCESS, nss_errno);
73 static struct passwd *nss_getpwent(void)
75 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
76 size_t , int *) =
77 (NSS_STATUS (*)(struct passwd *, char *,
78 size_t, int *))find_fn("getpwent_r");
79 static struct passwd pwd;
80 static char buf[1000];
81 NSS_STATUS status;
83 if (!_nss_getpwent_r)
84 return NULL;
86 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
87 if (status == NSS_STATUS_NOTFOUND) {
88 return NULL;
90 if (status != NSS_STATUS_SUCCESS) {
91 report_nss_error("getpwent", status);
92 return NULL;
94 return &pwd;
97 static struct passwd *nss_getpwnam(const char *name)
99 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
100 size_t , int *) =
101 (NSS_STATUS (*)(const char *, struct passwd *, char *,
102 size_t, int *))find_fn("getpwnam_r");
103 static struct passwd pwd;
104 static char buf[1000];
105 NSS_STATUS status;
107 if (!_nss_getpwnam_r)
108 return NULL;
110 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
111 if (status == NSS_STATUS_NOTFOUND) {
112 return NULL;
114 if (status != NSS_STATUS_SUCCESS) {
115 report_nss_error("getpwnam", status);
116 return NULL;
118 return &pwd;
121 static struct passwd *nss_getpwuid(uid_t uid)
123 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
124 size_t , int *) =
125 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
126 size_t, int *))find_fn("getpwuid_r");
127 static struct passwd pwd;
128 static char buf[1000];
129 NSS_STATUS status;
131 if (!_nss_getpwuid_r)
132 return NULL;
134 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
135 if (status == NSS_STATUS_NOTFOUND) {
136 return NULL;
138 if (status != NSS_STATUS_SUCCESS) {
139 report_nss_error("getpwuid", status);
140 return NULL;
142 return &pwd;
145 static void nss_setpwent(void)
147 NSS_STATUS (*_nss_setpwent)(void) =
148 (NSS_STATUS(*)(void))find_fn("setpwent");
149 NSS_STATUS status;
151 if (!_nss_setpwent)
152 return;
154 status = _nss_setpwent();
155 if (status != NSS_STATUS_SUCCESS) {
156 report_nss_error("setpwent", status);
160 static void nss_endpwent(void)
162 NSS_STATUS (*_nss_endpwent)(void) =
163 (NSS_STATUS (*)(void))find_fn("endpwent");
164 NSS_STATUS status;
166 if (!_nss_endpwent)
167 return;
169 status = _nss_endpwent();
170 if (status != NSS_STATUS_SUCCESS) {
171 report_nss_error("endpwent", status);
176 static struct group *nss_getgrent(void)
178 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
179 size_t , int *) =
180 (NSS_STATUS (*)(struct group *, char *,
181 size_t, int *))find_fn("getgrent_r");
182 static struct group grp;
183 static char *buf;
184 static int buflen = 1024;
185 NSS_STATUS status;
187 if (!_nss_getgrent_r)
188 return NULL;
190 if (!buf)
191 buf = (char *)malloc(buflen);
193 again:
194 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
195 if (status == NSS_STATUS_TRYAGAIN) {
196 buflen *= 2;
197 buf = (char *)realloc(buf, buflen);
198 if (!buf) {
199 return NULL;
201 goto again;
203 if (status == NSS_STATUS_NOTFOUND) {
204 SAFE_FREE(buf);
205 return NULL;
207 if (status != NSS_STATUS_SUCCESS) {
208 report_nss_error("getgrent", status);
209 SAFE_FREE(buf);
210 return NULL;
212 return &grp;
215 static struct group *nss_getgrnam(const char *name)
217 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
218 size_t , int *) =
219 (NSS_STATUS (*)(const char *, struct group *, char *,
220 size_t, int *))find_fn("getgrnam_r");
221 static struct group grp;
222 static char *buf;
223 static int buflen = 1000;
224 NSS_STATUS status;
226 if (!_nss_getgrnam_r)
227 return NULL;
229 if (!buf)
230 buf = (char *)malloc(buflen);
231 again:
232 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
233 if (status == NSS_STATUS_TRYAGAIN) {
234 buflen *= 2;
235 buf = (char *)realloc(buf, buflen);
236 if (!buf) {
237 return NULL;
239 goto again;
241 if (status == NSS_STATUS_NOTFOUND) {
242 SAFE_FREE(buf);
243 return NULL;
245 if (status != NSS_STATUS_SUCCESS) {
246 report_nss_error("getgrnam", status);
247 SAFE_FREE(buf);
248 return NULL;
250 return &grp;
253 static struct group *nss_getgrgid(gid_t gid)
255 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
256 size_t , int *) =
257 (NSS_STATUS (*)(gid_t, struct group *, char *,
258 size_t, int *))find_fn("getgrgid_r");
259 static struct group grp;
260 static char *buf;
261 static int buflen = 1000;
262 NSS_STATUS status;
264 if (!_nss_getgrgid_r)
265 return NULL;
267 if (!buf)
268 buf = (char *)malloc(buflen);
270 again:
271 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
272 if (status == NSS_STATUS_TRYAGAIN) {
273 buflen *= 2;
274 buf = (char *)realloc(buf, buflen);
275 if (!buf) {
276 return NULL;
278 goto again;
280 if (status == NSS_STATUS_NOTFOUND) {
281 SAFE_FREE(buf);
282 return NULL;
284 if (status != NSS_STATUS_SUCCESS) {
285 report_nss_error("getgrgid", status);
286 SAFE_FREE(buf);
287 return NULL;
289 return &grp;
292 static void nss_setgrent(void)
294 NSS_STATUS (*_nss_setgrent)(void) =
295 (NSS_STATUS (*)(void))find_fn("setgrent");
296 NSS_STATUS status;
298 if (!_nss_setgrent)
299 return;
301 status = _nss_setgrent();
302 if (status != NSS_STATUS_SUCCESS) {
303 report_nss_error("setgrent", status);
307 static void nss_endgrent(void)
309 NSS_STATUS (*_nss_endgrent)(void) =
310 (NSS_STATUS (*)(void))find_fn("endgrent");
311 NSS_STATUS status;
313 if (!_nss_endgrent)
314 return;
316 status = _nss_endgrent();
317 if (status != NSS_STATUS_SUCCESS) {
318 report_nss_error("endgrent", status);
322 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
324 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
325 long int *, gid_t **, long int , int *) =
326 (NSS_STATUS (*)(char *, gid_t, long int *,
327 long int *, gid_t **,
328 long int, int *))find_fn("initgroups_dyn");
329 NSS_STATUS status;
331 if (!_nss_initgroups)
332 return NSS_STATUS_UNAVAIL;
334 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
335 if (status != NSS_STATUS_SUCCESS) {
336 report_nss_error("initgroups", status);
338 return status;
341 static void print_passwd(struct passwd *pwd)
343 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
344 pwd->pw_name,
345 pwd->pw_passwd,
346 (unsigned long)pwd->pw_uid,
347 (unsigned long)pwd->pw_gid,
348 pwd->pw_gecos,
349 pwd->pw_dir,
350 pwd->pw_shell);
353 static void print_group(struct group *grp)
355 int i;
356 printf("%s:%s:%lu:",
357 grp->gr_name,
358 grp->gr_passwd,
359 (unsigned long)grp->gr_gid);
361 if (!grp->gr_mem[0]) {
362 printf("\n");
363 return;
366 for (i=0; grp->gr_mem[i+1]; i++) {
367 printf("%s,", grp->gr_mem[i]);
369 printf("%s\n", grp->gr_mem[i]);
372 static void nss_test_initgroups(char *name, gid_t gid)
374 long int size = 16;
375 long int start = 1;
376 gid_t *groups = NULL;
377 int i;
378 NSS_STATUS status;
380 groups = (gid_t *)malloc(size);
381 groups[0] = gid;
383 status = nss_initgroups(name, gid, &groups, &start, &size);
384 if (status == NSS_STATUS_UNAVAIL) {
385 printf("No initgroups fn\n");
386 return;
389 for (i=0; i<start-1; i++) {
390 printf("%lu, ", (unsigned long)groups[i]);
392 printf("%lu\n", (unsigned long)groups[i]);
396 static void nss_test_users(void)
398 struct passwd *pwd;
400 nss_setpwent();
401 /* loop over all users */
402 while ((pwd = nss_getpwent())) {
403 printf("Testing user %s\n", pwd->pw_name);
404 printf("getpwent: "); print_passwd(pwd);
405 pwd = nss_getpwuid(pwd->pw_uid);
406 if (!pwd) {
407 total_errors++;
408 printf("ERROR: can't getpwuid\n");
409 continue;
411 printf("getpwuid: "); print_passwd(pwd);
412 pwd = nss_getpwnam(pwd->pw_name);
413 if (!pwd) {
414 total_errors++;
415 printf("ERROR: can't getpwnam\n");
416 continue;
418 printf("getpwnam: "); print_passwd(pwd);
419 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
420 printf("\n");
422 nss_endpwent();
425 static void nss_test_groups(void)
427 struct group *grp;
429 nss_setgrent();
430 /* loop over all groups */
431 while ((grp = nss_getgrent())) {
432 printf("Testing group %s\n", grp->gr_name);
433 printf("getgrent: "); print_group(grp);
434 grp = nss_getgrnam(grp->gr_name);
435 if (!grp) {
436 total_errors++;
437 printf("ERROR: can't getgrnam\n");
438 continue;
440 printf("getgrnam: "); print_group(grp);
441 grp = nss_getgrgid(grp->gr_gid);
442 if (!grp) {
443 total_errors++;
444 printf("ERROR: can't getgrgid\n");
445 continue;
447 printf("getgrgid: "); print_group(grp);
448 printf("\n");
450 nss_endgrent();
453 static void nss_test_errors(void)
455 struct passwd *pwd;
456 struct group *grp;
458 pwd = getpwnam("nosuchname");
459 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
460 total_errors++;
461 printf("ERROR Non existant user gave error %d\n", last_error);
464 pwd = getpwuid(0xFFF0);
465 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
466 total_errors++;
467 printf("ERROR Non existant uid gave error %d\n", last_error);
470 grp = getgrnam("nosuchgroup");
471 if (grp || last_error != NSS_STATUS_NOTFOUND) {
472 total_errors++;
473 printf("ERROR Non existant group gave error %d\n", last_error);
476 grp = getgrgid(0xFFF0);
477 if (grp || last_error != NSS_STATUS_NOTFOUND) {
478 total_errors++;
479 printf("ERROR Non existant gid gave error %d\n", last_error);
483 int main(int argc, char *argv[])
485 if (argc > 1) so_path = argv[1];
486 if (argc > 2) nss_name = argv[2];
488 nss_test_users();
489 nss_test_groups();
490 nss_test_errors();
492 printf("total_errors=%d\n", total_errors);
494 return total_errors;