net_idmap: use wbcSet[U|G]idMapping() and wbcSet[U|G]idHwm() functions
[Samba.git] / source / torture / nsstest.c
blobca662262f8b303d08831e283e389737189209a44
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"
23 static const char *so_path = "/lib/libnss_winbind.so";
24 static const char *nss_name = "winbind";
25 static int nss_errno;
26 static NSS_STATUS last_error;
27 static int total_errors;
29 static void *find_fn(const char *name)
31 char *s;
32 static void *h;
33 void *res;
35 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
36 exit(1);
39 if (!h) {
40 h = sys_dlopen(so_path, RTLD_LAZY);
42 if (!h) {
43 printf("Can't open shared library %s\n", so_path);
44 exit(1);
46 res = sys_dlsym(h, s);
47 if (!res) {
48 printf("Can't find function %s\n", s);
49 total_errors++;
50 SAFE_FREE(s);
51 return NULL;
53 SAFE_FREE(s);
54 return res;
57 static void report_nss_error(const char *who, NSS_STATUS status)
59 last_error = status;
60 total_errors++;
61 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
62 who, status, NSS_STATUS_SUCCESS, nss_errno);
65 static struct passwd *nss_getpwent(void)
67 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
68 size_t , int *) =
69 (NSS_STATUS (*)(struct passwd *, char *,
70 size_t, int *))find_fn("getpwent_r");
71 static struct passwd pwd;
72 static char buf[1000];
73 NSS_STATUS status;
75 if (!_nss_getpwent_r)
76 return NULL;
78 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
79 if (status == NSS_STATUS_NOTFOUND) {
80 return NULL;
82 if (status != NSS_STATUS_SUCCESS) {
83 report_nss_error("getpwent", status);
84 return NULL;
86 return &pwd;
89 static struct passwd *nss_getpwnam(const char *name)
91 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
92 size_t , int *) =
93 (NSS_STATUS (*)(const char *, struct passwd *, char *,
94 size_t, int *))find_fn("getpwnam_r");
95 static struct passwd pwd;
96 static char buf[1000];
97 NSS_STATUS status;
99 if (!_nss_getpwnam_r)
100 return NULL;
102 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
103 if (status == NSS_STATUS_NOTFOUND) {
104 return NULL;
106 if (status != NSS_STATUS_SUCCESS) {
107 report_nss_error("getpwnam", status);
108 return NULL;
110 return &pwd;
113 static struct passwd *nss_getpwuid(uid_t uid)
115 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
116 size_t , int *) =
117 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
118 size_t, int *))find_fn("getpwuid_r");
119 static struct passwd pwd;
120 static char buf[1000];
121 NSS_STATUS status;
123 if (!_nss_getpwuid_r)
124 return NULL;
126 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
127 if (status == NSS_STATUS_NOTFOUND) {
128 return NULL;
130 if (status != NSS_STATUS_SUCCESS) {
131 report_nss_error("getpwuid", status);
132 return NULL;
134 return &pwd;
137 static void nss_setpwent(void)
139 NSS_STATUS (*_nss_setpwent)(void) =
140 (NSS_STATUS(*)(void))find_fn("setpwent");
141 NSS_STATUS status;
143 if (!_nss_setpwent)
144 return;
146 status = _nss_setpwent();
147 if (status != NSS_STATUS_SUCCESS) {
148 report_nss_error("setpwent", status);
152 static void nss_endpwent(void)
154 NSS_STATUS (*_nss_endpwent)(void) =
155 (NSS_STATUS (*)(void))find_fn("endpwent");
156 NSS_STATUS status;
158 if (!_nss_endpwent)
159 return;
161 status = _nss_endpwent();
162 if (status != NSS_STATUS_SUCCESS) {
163 report_nss_error("endpwent", status);
168 static struct group *nss_getgrent(void)
170 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
171 size_t , int *) =
172 (NSS_STATUS (*)(struct group *, char *,
173 size_t, int *))find_fn("getgrent_r");
174 static struct group grp;
175 static char *buf;
176 static int buflen = 1024;
177 NSS_STATUS status;
179 if (!_nss_getgrent_r)
180 return NULL;
182 if (!buf)
183 buf = SMB_MALLOC_ARRAY(char, buflen);
185 again:
186 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
187 if (status == NSS_STATUS_TRYAGAIN) {
188 buflen *= 2;
189 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
190 if (!buf) {
191 return NULL;
193 goto again;
195 if (status == NSS_STATUS_NOTFOUND) {
196 SAFE_FREE(buf);
197 return NULL;
199 if (status != NSS_STATUS_SUCCESS) {
200 report_nss_error("getgrent", status);
201 SAFE_FREE(buf);
202 return NULL;
204 SAFE_FREE(buf);
205 return &grp;
208 static struct group *nss_getgrnam(const char *name)
210 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
211 size_t , int *) =
212 (NSS_STATUS (*)(const char *, struct group *, char *,
213 size_t, int *))find_fn("getgrnam_r");
214 static struct group grp;
215 static char *buf;
216 static int buflen = 1000;
217 NSS_STATUS status;
219 if (!_nss_getgrnam_r)
220 return NULL;
222 if (!buf)
223 buf = SMB_MALLOC_ARRAY(char, buflen);
224 again:
225 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
226 if (status == NSS_STATUS_TRYAGAIN) {
227 buflen *= 2;
228 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
229 if (!buf) {
230 return NULL;
232 goto again;
234 if (status == NSS_STATUS_NOTFOUND) {
235 SAFE_FREE(buf);
236 return NULL;
238 if (status != NSS_STATUS_SUCCESS) {
239 report_nss_error("getgrnam", status);
240 SAFE_FREE(buf);
241 return NULL;
243 SAFE_FREE(buf);
244 return &grp;
247 static struct group *nss_getgrgid(gid_t gid)
249 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
250 size_t , int *) =
251 (NSS_STATUS (*)(gid_t, struct group *, char *,
252 size_t, int *))find_fn("getgrgid_r");
253 static struct group grp;
254 static char *buf;
255 static int buflen = 1000;
256 NSS_STATUS status;
258 if (!_nss_getgrgid_r)
259 return NULL;
261 if (!buf)
262 buf = SMB_MALLOC_ARRAY(char, buflen);
264 again:
265 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
266 if (status == NSS_STATUS_TRYAGAIN) {
267 buflen *= 2;
268 buf = SMB_REALLOC_ARRAY(buf, char, buflen);
269 if (!buf) {
270 return NULL;
272 goto again;
274 if (status == NSS_STATUS_NOTFOUND) {
275 SAFE_FREE(buf);
276 return NULL;
278 if (status != NSS_STATUS_SUCCESS) {
279 report_nss_error("getgrgid", status);
280 SAFE_FREE(buf);
281 return NULL;
283 SAFE_FREE(buf);
284 return &grp;
287 static void nss_setgrent(void)
289 NSS_STATUS (*_nss_setgrent)(void) =
290 (NSS_STATUS (*)(void))find_fn("setgrent");
291 NSS_STATUS status;
293 if (!_nss_setgrent)
294 return;
296 status = _nss_setgrent();
297 if (status != NSS_STATUS_SUCCESS) {
298 report_nss_error("setgrent", status);
302 static void nss_endgrent(void)
304 NSS_STATUS (*_nss_endgrent)(void) =
305 (NSS_STATUS (*)(void))find_fn("endgrent");
306 NSS_STATUS status;
308 if (!_nss_endgrent)
309 return;
311 status = _nss_endgrent();
312 if (status != NSS_STATUS_SUCCESS) {
313 report_nss_error("endgrent", status);
317 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
319 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
320 long int *, gid_t **, long int , int *) =
321 (NSS_STATUS (*)(char *, gid_t, long int *,
322 long int *, gid_t **,
323 long int, int *))find_fn("initgroups_dyn");
324 NSS_STATUS status;
326 if (!_nss_initgroups)
327 return NSS_STATUS_UNAVAIL;
329 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
330 if (status != NSS_STATUS_SUCCESS) {
331 report_nss_error("initgroups", status);
333 return status;
336 static void print_passwd(struct passwd *pwd)
338 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
339 pwd->pw_name,
340 pwd->pw_passwd,
341 (unsigned long)pwd->pw_uid,
342 (unsigned long)pwd->pw_gid,
343 pwd->pw_gecos,
344 pwd->pw_dir,
345 pwd->pw_shell);
348 static void print_group(struct group *grp)
350 int i;
351 printf("%s:%s:%lu: ",
352 grp->gr_name,
353 grp->gr_passwd,
354 (unsigned long)grp->gr_gid);
356 if (!grp->gr_mem[0]) {
357 printf("\n");
358 return;
361 for (i=0; grp->gr_mem[i+1]; i++) {
362 printf("%s, ", grp->gr_mem[i]);
364 printf("%s\n", grp->gr_mem[i]);
367 static void nss_test_initgroups(char *name, gid_t gid)
369 long int size = 16;
370 long int start = 1;
371 gid_t *groups = NULL;
372 int i;
373 NSS_STATUS status;
375 groups = SMB_MALLOC_ARRAY(gid_t, size);
376 groups[0] = gid;
378 status = nss_initgroups(name, gid, &groups, &start, &size);
379 if (status == NSS_STATUS_UNAVAIL) {
380 printf("No initgroups fn\n");
381 return;
384 for (i=0; i<start-1; i++) {
385 printf("%lu, ", (unsigned long)groups[i]);
387 printf("%lu\n", (unsigned long)groups[i]);
391 static void nss_test_users(void)
393 struct passwd *pwd;
395 nss_setpwent();
396 /* loop over all users */
397 while ((pwd = nss_getpwent())) {
398 printf("Testing user %s\n", pwd->pw_name);
399 printf("getpwent: "); print_passwd(pwd);
400 pwd = nss_getpwuid(pwd->pw_uid);
401 if (!pwd) {
402 total_errors++;
403 printf("ERROR: can't getpwuid\n");
404 continue;
406 printf("getpwuid: "); print_passwd(pwd);
407 pwd = nss_getpwnam(pwd->pw_name);
408 if (!pwd) {
409 total_errors++;
410 printf("ERROR: can't getpwnam\n");
411 continue;
413 printf("getpwnam: "); print_passwd(pwd);
414 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
415 printf("\n");
417 nss_endpwent();
420 static void nss_test_groups(void)
422 struct group *grp;
424 nss_setgrent();
425 /* loop over all groups */
426 while ((grp = nss_getgrent())) {
427 printf("Testing group %s\n", grp->gr_name);
428 printf("getgrent: "); print_group(grp);
429 grp = nss_getgrnam(grp->gr_name);
430 if (!grp) {
431 total_errors++;
432 printf("ERROR: can't getgrnam\n");
433 continue;
435 printf("getgrnam: "); print_group(grp);
436 grp = nss_getgrgid(grp->gr_gid);
437 if (!grp) {
438 total_errors++;
439 printf("ERROR: can't getgrgid\n");
440 continue;
442 printf("getgrgid: "); print_group(grp);
443 printf("\n");
445 nss_endgrent();
448 static void nss_test_errors(void)
450 struct passwd *pwd;
451 struct group *grp;
453 pwd = getpwnam("nosuchname");
454 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
455 total_errors++;
456 printf("ERROR Non existant user gave error %d\n", last_error);
459 pwd = getpwuid(0xFFF0);
460 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
461 total_errors++;
462 printf("ERROR Non existant uid gave error %d\n", last_error);
465 grp = getgrnam("nosuchgroup");
466 if (grp || last_error != NSS_STATUS_NOTFOUND) {
467 total_errors++;
468 printf("ERROR Non existant group gave error %d\n", last_error);
471 grp = getgrgid(0xFFF0);
472 if (grp || last_error != NSS_STATUS_NOTFOUND) {
473 total_errors++;
474 printf("ERROR Non existant gid gave error %d\n", last_error);
478 int main(int argc, char *argv[])
480 if (argc > 1) so_path = argv[1];
481 if (argc > 2) nss_name = argv[2];
483 nss_test_users();
484 nss_test_groups();
485 nss_test_errors();
487 printf("total_errors=%d\n", total_errors);
489 return total_errors;