s3-winreg: Fix _winreg_QueryValue crash bugs and implement windows behavior.
[Samba/kamenim.git] / source3 / torture / nsstest.c
blob8bb577e505429cdd164a31eaec6d850cd16f9f76
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 #ifdef malloc
24 #undef malloc
25 #endif
26 #ifdef realloc
27 #undef realloc
28 #endif
30 static const char *so_path = "/lib/libnss_winbind.so";
31 static const char *nss_name = "winbind";
32 static int nss_errno;
33 static NSS_STATUS last_error;
34 static int total_errors;
36 static void *find_fn(const char *name)
38 char *s;
39 static void *h;
40 void *res;
42 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
43 exit(1);
46 if (!h) {
47 h = dlopen(so_path, RTLD_LAZY);
49 if (!h) {
50 printf("Can't open shared library %s\n", so_path);
51 exit(1);
53 res = dlsym(h, s);
54 if (!res) {
55 printf("Can't find function %s\n", s);
56 total_errors++;
57 SAFE_FREE(s);
58 return NULL;
60 SAFE_FREE(s);
61 return res;
64 static void report_nss_error(const char *who, NSS_STATUS status)
66 last_error = status;
67 total_errors++;
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 *,
75 size_t , int *) =
76 (NSS_STATUS (*)(struct passwd *, char *,
77 size_t, int *))find_fn("getpwent_r");
78 static struct passwd pwd;
79 static char buf[1000];
80 NSS_STATUS status;
82 if (!_nss_getpwent_r)
83 return NULL;
85 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
86 if (status == NSS_STATUS_NOTFOUND) {
87 return NULL;
89 if (status != NSS_STATUS_SUCCESS) {
90 report_nss_error("getpwent", status);
91 return NULL;
93 return &pwd;
96 static struct passwd *nss_getpwnam(const char *name)
98 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
99 size_t , int *) =
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];
104 NSS_STATUS status;
106 if (!_nss_getpwnam_r)
107 return NULL;
109 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
110 if (status == NSS_STATUS_NOTFOUND) {
111 return NULL;
113 if (status != NSS_STATUS_SUCCESS) {
114 report_nss_error("getpwnam", status);
115 return NULL;
117 return &pwd;
120 static struct passwd *nss_getpwuid(uid_t uid)
122 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
123 size_t , int *) =
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];
128 NSS_STATUS status;
130 if (!_nss_getpwuid_r)
131 return NULL;
133 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
134 if (status == NSS_STATUS_NOTFOUND) {
135 return NULL;
137 if (status != NSS_STATUS_SUCCESS) {
138 report_nss_error("getpwuid", status);
139 return NULL;
141 return &pwd;
144 static void nss_setpwent(void)
146 NSS_STATUS (*_nss_setpwent)(void) =
147 (NSS_STATUS(*)(void))find_fn("setpwent");
148 NSS_STATUS status;
150 if (!_nss_setpwent)
151 return;
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");
163 NSS_STATUS status;
165 if (!_nss_endpwent)
166 return;
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 *,
178 size_t , int *) =
179 (NSS_STATUS (*)(struct group *, char *,
180 size_t, int *))find_fn("getgrent_r");
181 static struct group grp;
182 static char *buf;
183 static int buflen = 1024;
184 NSS_STATUS status;
186 if (!_nss_getgrent_r)
187 return NULL;
189 if (!buf)
190 buf = (char *)malloc(buflen);
192 again:
193 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
194 if (status == NSS_STATUS_TRYAGAIN) {
195 buflen *= 2;
196 buf = (char *)realloc(buf, buflen);
197 if (!buf) {
198 return NULL;
200 goto again;
202 if (status == NSS_STATUS_NOTFOUND) {
203 SAFE_FREE(buf);
204 return NULL;
206 if (status != NSS_STATUS_SUCCESS) {
207 report_nss_error("getgrent", status);
208 SAFE_FREE(buf);
209 return NULL;
211 return &grp;
214 static struct group *nss_getgrnam(const char *name)
216 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
217 size_t , int *) =
218 (NSS_STATUS (*)(const char *, struct group *, char *,
219 size_t, int *))find_fn("getgrnam_r");
220 static struct group grp;
221 static char *buf;
222 static int buflen = 1000;
223 NSS_STATUS status;
225 if (!_nss_getgrnam_r)
226 return NULL;
228 if (!buf)
229 buf = (char *)malloc(buflen);
230 again:
231 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
232 if (status == NSS_STATUS_TRYAGAIN) {
233 buflen *= 2;
234 buf = (char *)realloc(buf, buflen);
235 if (!buf) {
236 return NULL;
238 goto again;
240 if (status == NSS_STATUS_NOTFOUND) {
241 SAFE_FREE(buf);
242 return NULL;
244 if (status != NSS_STATUS_SUCCESS) {
245 report_nss_error("getgrnam", status);
246 SAFE_FREE(buf);
247 return NULL;
249 return &grp;
252 static struct group *nss_getgrgid(gid_t gid)
254 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
255 size_t , int *) =
256 (NSS_STATUS (*)(gid_t, struct group *, char *,
257 size_t, int *))find_fn("getgrgid_r");
258 static struct group grp;
259 static char *buf;
260 static int buflen = 1000;
261 NSS_STATUS status;
263 if (!_nss_getgrgid_r)
264 return NULL;
266 if (!buf)
267 buf = (char *)malloc(buflen);
269 again:
270 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
271 if (status == NSS_STATUS_TRYAGAIN) {
272 buflen *= 2;
273 buf = (char *)realloc(buf, buflen);
274 if (!buf) {
275 return NULL;
277 goto again;
279 if (status == NSS_STATUS_NOTFOUND) {
280 SAFE_FREE(buf);
281 return NULL;
283 if (status != NSS_STATUS_SUCCESS) {
284 report_nss_error("getgrgid", status);
285 SAFE_FREE(buf);
286 return NULL;
288 return &grp;
291 static void nss_setgrent(void)
293 NSS_STATUS (*_nss_setgrent)(void) =
294 (NSS_STATUS (*)(void))find_fn("setgrent");
295 NSS_STATUS status;
297 if (!_nss_setgrent)
298 return;
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");
310 NSS_STATUS status;
312 if (!_nss_endgrent)
313 return;
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");
328 NSS_STATUS status;
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);
337 return status;
340 static void print_passwd(struct passwd *pwd)
342 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
343 pwd->pw_name,
344 pwd->pw_passwd,
345 (unsigned long)pwd->pw_uid,
346 (unsigned long)pwd->pw_gid,
347 pwd->pw_gecos,
348 pwd->pw_dir,
349 pwd->pw_shell);
352 static void print_group(struct group *grp)
354 int i;
355 printf("%s:%s:%lu:",
356 grp->gr_name,
357 grp->gr_passwd,
358 (unsigned long)grp->gr_gid);
360 if (!grp->gr_mem[0]) {
361 printf("\n");
362 return;
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)
373 long int size = 16;
374 long int start = 1;
375 gid_t *groups = NULL;
376 int i;
377 NSS_STATUS status;
379 groups = (gid_t *)malloc(size);
380 groups[0] = gid;
382 status = nss_initgroups(name, gid, &groups, &start, &size);
383 if (status == NSS_STATUS_UNAVAIL) {
384 printf("No initgroups fn\n");
385 return;
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)
397 struct passwd *pwd;
399 nss_setpwent();
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);
405 if (!pwd) {
406 total_errors++;
407 printf("ERROR: can't getpwuid\n");
408 continue;
410 printf("getpwuid: "); print_passwd(pwd);
411 pwd = nss_getpwnam(pwd->pw_name);
412 if (!pwd) {
413 total_errors++;
414 printf("ERROR: can't getpwnam\n");
415 continue;
417 printf("getpwnam: "); print_passwd(pwd);
418 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
419 printf("\n");
421 nss_endpwent();
424 static void nss_test_groups(void)
426 struct group *grp;
428 nss_setgrent();
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);
434 if (!grp) {
435 total_errors++;
436 printf("ERROR: can't getgrnam\n");
437 continue;
439 printf("getgrnam: "); print_group(grp);
440 grp = nss_getgrgid(grp->gr_gid);
441 if (!grp) {
442 total_errors++;
443 printf("ERROR: can't getgrgid\n");
444 continue;
446 printf("getgrgid: "); print_group(grp);
447 printf("\n");
449 nss_endgrent();
452 static void nss_test_errors(void)
454 struct passwd *pwd;
455 struct group *grp;
457 pwd = getpwnam("nosuchname");
458 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
459 total_errors++;
460 printf("ERROR Non existant user gave error %d\n", last_error);
463 pwd = getpwuid(0xFFF0);
464 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
465 total_errors++;
466 printf("ERROR Non existant uid gave error %d\n", last_error);
469 grp = getgrnam("nosuchgroup");
470 if (grp || last_error != NSS_STATUS_NOTFOUND) {
471 total_errors++;
472 printf("ERROR Non existant group gave error %d\n", last_error);
475 grp = getgrgid(0xFFF0);
476 if (grp || last_error != NSS_STATUS_NOTFOUND) {
477 total_errors++;
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];
487 nss_test_users();
488 nss_test_groups();
489 nss_test_errors();
491 printf("total_errors=%d\n", total_errors);
493 return total_errors;