r23733: Limit LDAP lookup in lookup_usergroups_member() to security groups.
[Samba.git] / source / torture / nsstest.c
blob121679ed8cc44b70620fb30651a89d8a6e49ada9
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 static const char *so_path = "/lib/libnss_winbind.so";
25 static const char *nss_name = "winbind";
26 static int nss_errno;
27 static NSS_STATUS last_error;
28 static int total_errors;
30 static void *find_fn(const char *name)
32 pstring s;
33 static void *h;
34 void *res;
36 pstr_sprintf(s, "_nss_%s_%s", nss_name, name);
38 if (!h) {
39 h = sys_dlopen(so_path, RTLD_LAZY);
41 if (!h) {
42 printf("Can't open shared library %s\n", so_path);
43 exit(1);
45 res = sys_dlsym(h, s);
46 if (!res) {
47 printf("Can't find function %s\n", s);
48 total_errors++;
49 return NULL;
51 return res;
54 static void report_nss_error(const char *who, NSS_STATUS status)
56 last_error = status;
57 total_errors++;
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 *) =
66 (NSS_STATUS (*)(struct passwd *, char *,
67 size_t, int *))find_fn("getpwent_r");
68 static struct passwd pwd;
69 static char buf[1000];
70 NSS_STATUS status;
72 if (!_nss_getpwent_r)
73 return NULL;
75 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
76 if (status == NSS_STATUS_NOTFOUND) {
77 return NULL;
79 if (status != NSS_STATUS_SUCCESS) {
80 report_nss_error("getpwent", status);
81 return NULL;
83 return &pwd;
86 static struct passwd *nss_getpwnam(const char *name)
88 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
89 size_t , int *) =
90 (NSS_STATUS (*)(const char *, struct passwd *, char *,
91 size_t, int *))find_fn("getpwnam_r");
92 static struct passwd pwd;
93 static char buf[1000];
94 NSS_STATUS status;
96 if (!_nss_getpwnam_r)
97 return NULL;
99 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
100 if (status == NSS_STATUS_NOTFOUND) {
101 return NULL;
103 if (status != NSS_STATUS_SUCCESS) {
104 report_nss_error("getpwnam", status);
105 return NULL;
107 return &pwd;
110 static struct passwd *nss_getpwuid(uid_t uid)
112 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
113 size_t , int *) =
114 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
115 size_t, int *))find_fn("getpwuid_r");
116 static struct passwd pwd;
117 static char buf[1000];
118 NSS_STATUS status;
120 if (!_nss_getpwuid_r)
121 return NULL;
123 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
124 if (status == NSS_STATUS_NOTFOUND) {
125 return NULL;
127 if (status != NSS_STATUS_SUCCESS) {
128 report_nss_error("getpwuid", status);
129 return NULL;
131 return &pwd;
134 static void nss_setpwent(void)
136 NSS_STATUS (*_nss_setpwent)(void) =
137 (NSS_STATUS(*)(void))find_fn("setpwent");
138 NSS_STATUS status;
140 if (!_nss_setpwent)
141 return;
143 status = _nss_setpwent();
144 if (status != NSS_STATUS_SUCCESS) {
145 report_nss_error("setpwent", status);
149 static void nss_endpwent(void)
151 NSS_STATUS (*_nss_endpwent)(void) =
152 (NSS_STATUS (*)(void))find_fn("endpwent");
153 NSS_STATUS status;
155 if (!_nss_endpwent)
156 return;
158 status = _nss_endpwent();
159 if (status != NSS_STATUS_SUCCESS) {
160 report_nss_error("endpwent", status);
165 static struct group *nss_getgrent(void)
167 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
168 size_t , int *) =
169 (NSS_STATUS (*)(struct group *, char *,
170 size_t, int *))find_fn("getgrent_r");
171 static struct group grp;
172 static char *buf;
173 static int buflen = 1024;
174 NSS_STATUS status;
176 if (!_nss_getgrent_r)
177 return NULL;
179 if (!buf)
180 buf = SMB_MALLOC_ARRAY(char, buflen);
182 again:
183 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
184 if (status == NSS_STATUS_TRYAGAIN) {
185 buflen *= 2;
186 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
187 if (!buf) {
188 return NULL;
190 goto again;
192 if (status == NSS_STATUS_NOTFOUND) {
193 SAFE_FREE(buf);
194 return NULL;
196 if (status != NSS_STATUS_SUCCESS) {
197 report_nss_error("getgrent", status);
198 SAFE_FREE(buf);
199 return NULL;
201 SAFE_FREE(buf);
202 return &grp;
205 static struct group *nss_getgrnam(const char *name)
207 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
208 size_t , int *) =
209 (NSS_STATUS (*)(const char *, struct group *, char *,
210 size_t, int *))find_fn("getgrnam_r");
211 static struct group grp;
212 static char *buf;
213 static int buflen = 1000;
214 NSS_STATUS status;
216 if (!_nss_getgrnam_r)
217 return NULL;
219 if (!buf)
220 buf = SMB_MALLOC_ARRAY(char, buflen);
221 again:
222 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
223 if (status == NSS_STATUS_TRYAGAIN) {
224 buflen *= 2;
225 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
226 if (!buf) {
227 return NULL;
229 goto again;
231 if (status == NSS_STATUS_NOTFOUND) {
232 SAFE_FREE(buf);
233 return NULL;
235 if (status != NSS_STATUS_SUCCESS) {
236 report_nss_error("getgrnam", status);
237 SAFE_FREE(buf);
238 return NULL;
240 SAFE_FREE(buf);
241 return &grp;
244 static struct group *nss_getgrgid(gid_t gid)
246 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
247 size_t , int *) =
248 (NSS_STATUS (*)(gid_t, struct group *, char *,
249 size_t, int *))find_fn("getgrgid_r");
250 static struct group grp;
251 static char *buf;
252 static int buflen = 1000;
253 NSS_STATUS status;
255 if (!_nss_getgrgid_r)
256 return NULL;
258 if (!buf)
259 buf = SMB_MALLOC_ARRAY(char, buflen);
261 again:
262 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
263 if (status == NSS_STATUS_TRYAGAIN) {
264 buflen *= 2;
265 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
266 if (!buf) {
267 return NULL;
269 goto again;
271 if (status == NSS_STATUS_NOTFOUND) {
272 SAFE_FREE(buf);
273 return NULL;
275 if (status != NSS_STATUS_SUCCESS) {
276 report_nss_error("getgrgid", status);
277 SAFE_FREE(buf);
278 return NULL;
280 SAFE_FREE(buf);
281 return &grp;
284 static void nss_setgrent(void)
286 NSS_STATUS (*_nss_setgrent)(void) =
287 (NSS_STATUS (*)(void))find_fn("setgrent");
288 NSS_STATUS status;
290 if (!_nss_setgrent)
291 return;
293 status = _nss_setgrent();
294 if (status != NSS_STATUS_SUCCESS) {
295 report_nss_error("setgrent", status);
299 static void nss_endgrent(void)
301 NSS_STATUS (*_nss_endgrent)(void) =
302 (NSS_STATUS (*)(void))find_fn("endgrent");
303 NSS_STATUS status;
305 if (!_nss_endgrent)
306 return;
308 status = _nss_endgrent();
309 if (status != NSS_STATUS_SUCCESS) {
310 report_nss_error("endgrent", status);
314 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
316 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
317 long int *, gid_t **, long int , int *) =
318 (NSS_STATUS (*)(char *, gid_t, long int *,
319 long int *, gid_t **,
320 long int, int *))find_fn("initgroups_dyn");
321 NSS_STATUS status;
323 if (!_nss_initgroups)
324 return NSS_STATUS_UNAVAIL;
326 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
327 if (status != NSS_STATUS_SUCCESS) {
328 report_nss_error("initgroups", status);
330 return status;
333 static void print_passwd(struct passwd *pwd)
335 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
336 pwd->pw_name,
337 pwd->pw_passwd,
338 (unsigned long)pwd->pw_uid,
339 (unsigned long)pwd->pw_gid,
340 pwd->pw_gecos,
341 pwd->pw_dir,
342 pwd->pw_shell);
345 static void print_group(struct group *grp)
347 int i;
348 printf("%s:%s:%lu: ",
349 grp->gr_name,
350 grp->gr_passwd,
351 (unsigned long)grp->gr_gid);
353 if (!grp->gr_mem[0]) {
354 printf("\n");
355 return;
358 for (i=0; grp->gr_mem[i+1]; i++) {
359 printf("%s, ", grp->gr_mem[i]);
361 printf("%s\n", grp->gr_mem[i]);
364 static void nss_test_initgroups(char *name, gid_t gid)
366 long int size = 16;
367 long int start = 1;
368 gid_t *groups = NULL;
369 int i;
370 NSS_STATUS status;
372 groups = SMB_MALLOC_ARRAY(gid_t, size);
373 groups[0] = gid;
375 status = nss_initgroups(name, gid, &groups, &start, &size);
376 if (status == NSS_STATUS_UNAVAIL) {
377 printf("No initgroups fn\n");
378 return;
381 for (i=0; i<start-1; i++) {
382 printf("%lu, ", (unsigned long)groups[i]);
384 printf("%lu\n", (unsigned long)groups[i]);
388 static void nss_test_users(void)
390 struct passwd *pwd;
392 nss_setpwent();
393 /* loop over all users */
394 while ((pwd = nss_getpwent())) {
395 printf("Testing user %s\n", pwd->pw_name);
396 printf("getpwent: "); print_passwd(pwd);
397 pwd = nss_getpwuid(pwd->pw_uid);
398 if (!pwd) {
399 total_errors++;
400 printf("ERROR: can't getpwuid\n");
401 continue;
403 printf("getpwuid: "); print_passwd(pwd);
404 pwd = nss_getpwnam(pwd->pw_name);
405 if (!pwd) {
406 total_errors++;
407 printf("ERROR: can't getpwnam\n");
408 continue;
410 printf("getpwnam: "); print_passwd(pwd);
411 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
412 printf("\n");
414 nss_endpwent();
417 static void nss_test_groups(void)
419 struct group *grp;
421 nss_setgrent();
422 /* loop over all groups */
423 while ((grp = nss_getgrent())) {
424 printf("Testing group %s\n", grp->gr_name);
425 printf("getgrent: "); print_group(grp);
426 grp = nss_getgrnam(grp->gr_name);
427 if (!grp) {
428 total_errors++;
429 printf("ERROR: can't getgrnam\n");
430 continue;
432 printf("getgrnam: "); print_group(grp);
433 grp = nss_getgrgid(grp->gr_gid);
434 if (!grp) {
435 total_errors++;
436 printf("ERROR: can't getgrgid\n");
437 continue;
439 printf("getgrgid: "); print_group(grp);
440 printf("\n");
442 nss_endgrent();
445 static void nss_test_errors(void)
447 struct passwd *pwd;
448 struct group *grp;
450 pwd = getpwnam("nosuchname");
451 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
452 total_errors++;
453 printf("ERROR Non existant user gave error %d\n", last_error);
456 pwd = getpwuid(0xFFF0);
457 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
458 total_errors++;
459 printf("ERROR Non existant uid gave error %d\n", last_error);
462 grp = getgrnam("nosuchgroup");
463 if (grp || last_error != NSS_STATUS_NOTFOUND) {
464 total_errors++;
465 printf("ERROR Non existant group gave error %d\n", last_error);
468 grp = getgrgid(0xFFF0);
469 if (grp || last_error != NSS_STATUS_NOTFOUND) {
470 total_errors++;
471 printf("ERROR Non existant gid gave error %d\n", last_error);
475 int main(int argc, char *argv[])
477 if (argc > 1) so_path = argv[1];
478 if (argc > 2) nss_name = argv[2];
480 nss_test_users();
481 nss_test_groups();
482 nss_test_errors();
484 printf("total_errors=%d\n", total_errors);
486 return total_errors;