r21439: fix compiler warnings
[Samba/ekacnet.git] / source4 / torture / nsstest.c
blob376edada56a1134b3f7d284ff29a999b8118a930
1 /*
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.
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[1024];
32 static void *h;
33 void *res;
35 snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
37 if (!h) {
38 h = sys_dlopen(so_path, RTLD_LAZY);
40 if (!h) {
41 printf("Can't open shared library %s\n", so_path);
42 exit(1);
44 res = sys_dlsym(h, s);
45 if (!res) {
46 printf("Can't find function %s\n", s);
47 return NULL;
49 return res;
52 static void report_nss_error(const char *who, NSS_STATUS status)
54 last_error = status;
55 total_errors++;
56 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
57 who, status, NSS_STATUS_SUCCESS, nss_errno);
60 static struct passwd *nss_getpwent(void)
62 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
63 size_t , int *) = find_fn("getpwent_r");
64 static struct passwd pwd;
65 static char buf[1000];
66 NSS_STATUS status;
68 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
69 if (status == NSS_STATUS_NOTFOUND) {
70 return NULL;
72 if (status != NSS_STATUS_SUCCESS) {
73 report_nss_error("getpwent", status);
74 return NULL;
76 return &pwd;
79 static struct passwd *nss_getpwnam(const char *name)
81 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
82 size_t , int *) = find_fn("getpwnam_r");
83 static struct passwd pwd;
84 static char buf[1000];
85 NSS_STATUS status;
87 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
88 if (status == NSS_STATUS_NOTFOUND) {
89 return NULL;
91 if (status != NSS_STATUS_SUCCESS) {
92 report_nss_error("getpwnam", status);
93 return NULL;
95 return &pwd;
98 static struct passwd *nss_getpwuid(uid_t uid)
100 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
101 size_t , int *) = find_fn("getpwuid_r");
102 static struct passwd pwd;
103 static char buf[1000];
104 NSS_STATUS status;
106 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
107 if (status == NSS_STATUS_NOTFOUND) {
108 return NULL;
110 if (status != NSS_STATUS_SUCCESS) {
111 report_nss_error("getpwuid", status);
112 return NULL;
114 return &pwd;
117 static void nss_setpwent(void)
119 NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
120 NSS_STATUS status;
121 status = _nss_setpwent();
122 if (status != NSS_STATUS_SUCCESS) {
123 report_nss_error("setpwent", status);
127 static void nss_endpwent(void)
129 NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
130 NSS_STATUS status;
131 status = _nss_endpwent();
132 if (status != NSS_STATUS_SUCCESS) {
133 report_nss_error("endpwent", status);
138 static struct group *nss_getgrent(void)
140 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
141 size_t , int *) = find_fn("getgrent_r");
142 static struct group grp;
143 static char *buf;
144 static int buflen = 1024;
145 NSS_STATUS status;
147 if (!buf) buf = malloc(buflen);
149 again:
150 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
151 if (status == NSS_STATUS_TRYAGAIN) {
152 buflen *= 2;
153 buf = realloc(buf, buflen);
154 goto again;
156 if (status == NSS_STATUS_NOTFOUND) {
157 return NULL;
159 if (status != NSS_STATUS_SUCCESS) {
160 report_nss_error("getgrent", status);
161 return NULL;
163 return &grp;
166 static struct group *nss_getgrnam(const char *name)
168 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
169 size_t , int *) = find_fn("getgrnam_r");
170 static struct group grp;
171 static char *buf;
172 static int buflen = 1000;
173 NSS_STATUS status;
175 if (!buf) buf = malloc(buflen);
176 again:
177 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
178 if (status == NSS_STATUS_TRYAGAIN) {
179 buflen *= 2;
180 buf = realloc(buf, buflen);
181 goto again;
183 if (status == NSS_STATUS_NOTFOUND) {
184 return NULL;
186 if (status != NSS_STATUS_SUCCESS) {
187 report_nss_error("getgrnam", status);
188 return NULL;
190 return &grp;
193 static struct group *nss_getgrgid(gid_t gid)
195 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
196 size_t , int *) = find_fn("getgrgid_r");
197 static struct group grp;
198 static char *buf;
199 static int buflen = 1000;
200 NSS_STATUS status;
202 if (!buf) buf = malloc(buflen);
203 again:
204 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
205 if (status == NSS_STATUS_TRYAGAIN) {
206 buflen *= 2;
207 buf = realloc(buf, buflen);
208 goto again;
210 if (status == NSS_STATUS_NOTFOUND) {
211 return NULL;
213 if (status != NSS_STATUS_SUCCESS) {
214 report_nss_error("getgrgid", status);
215 return NULL;
217 return &grp;
220 static void nss_setgrent(void)
222 NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
223 NSS_STATUS status;
224 status = _nss_setgrent();
225 if (status != NSS_STATUS_SUCCESS) {
226 report_nss_error("setgrent", status);
230 static void nss_endgrent(void)
232 NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
233 NSS_STATUS status;
234 status = _nss_endgrent();
235 if (status != NSS_STATUS_SUCCESS) {
236 report_nss_error("endgrent", status);
240 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
242 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
243 long int *, gid_t **, long int , int *) =
244 find_fn("initgroups_dyn");
245 NSS_STATUS status;
247 if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
249 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
250 if (status != NSS_STATUS_SUCCESS) {
251 report_nss_error("initgroups", status);
253 return status;
256 static void print_passwd(struct passwd *pwd)
258 printf("%s:%s:%d:%d:%s:%s:%s\n",
259 pwd->pw_name,
260 pwd->pw_passwd,
261 pwd->pw_uid,
262 pwd->pw_gid,
263 pwd->pw_gecos,
264 pwd->pw_dir,
265 pwd->pw_shell);
268 static void print_group(struct group *grp)
270 int i;
271 printf("%s:%s:%d: ",
272 grp->gr_name,
273 grp->gr_passwd,
274 grp->gr_gid);
276 if (!grp->gr_mem[0]) {
277 printf("\n");
278 return;
281 for (i=0; grp->gr_mem[i+1]; i++) {
282 printf("%s, ", grp->gr_mem[i]);
284 printf("%s\n", grp->gr_mem[i]);
287 static void nss_test_initgroups(char *name, gid_t gid)
289 long int size = 16;
290 long int start = 1;
291 gid_t *groups = NULL;
292 int i;
293 NSS_STATUS status;
295 groups = (gid_t *)malloc_array_p(gid_t, size);
296 groups[0] = gid;
298 status = nss_initgroups(name, gid, &groups, &start, &size);
299 if (status == NSS_STATUS_UNAVAIL) {
300 printf("No initgroups fn\n");
301 return;
304 for (i=0; i<start-1; i++) {
305 printf("%d, ", groups[i]);
307 printf("%d\n", groups[i]);
311 static void nss_test_users(void)
313 struct passwd *pwd;
315 nss_setpwent();
316 /* loop over all users */
317 while ((pwd = nss_getpwent())) {
318 printf("Testing user %s\n", pwd->pw_name);
319 printf("getpwent: "); print_passwd(pwd);
320 pwd = nss_getpwuid(pwd->pw_uid);
321 if (!pwd) {
322 total_errors++;
323 printf("ERROR: can't getpwuid\n");
324 continue;
326 printf("getpwuid: "); print_passwd(pwd);
327 pwd = nss_getpwnam(pwd->pw_name);
328 if (!pwd) {
329 total_errors++;
330 printf("ERROR: can't getpwnam\n");
331 continue;
333 printf("getpwnam: "); print_passwd(pwd);
334 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
335 printf("\n");
337 nss_endpwent();
340 static void nss_test_groups(void)
342 struct group *grp;
344 nss_setgrent();
345 /* loop over all groups */
346 while ((grp = nss_getgrent())) {
347 printf("Testing group %s\n", grp->gr_name);
348 printf("getgrent: "); print_group(grp);
349 grp = nss_getgrnam(grp->gr_name);
350 if (!grp) {
351 total_errors++;
352 printf("ERROR: can't getgrnam\n");
353 continue;
355 printf("getgrnam: "); print_group(grp);
356 grp = nss_getgrgid(grp->gr_gid);
357 if (!grp) {
358 total_errors++;
359 printf("ERROR: can't getgrgid\n");
360 continue;
362 printf("getgrgid: "); print_group(grp);
363 printf("\n");
365 nss_endgrent();
368 static void nss_test_errors(void)
370 struct passwd *pwd;
371 struct group *grp;
373 pwd = getpwnam("nosuchname");
374 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
375 total_errors++;
376 printf("ERROR Non existant user gave error %d\n", last_error);
379 pwd = getpwuid(0xFFF0);
380 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
381 total_errors++;
382 printf("ERROR Non existant uid gave error %d\n", last_error);
385 grp = getgrnam("nosuchgroup");
386 if (grp || last_error != NSS_STATUS_NOTFOUND) {
387 total_errors++;
388 printf("ERROR Non existant group gave error %d\n", last_error);
391 grp = getgrgid(0xFFF0);
392 if (grp || last_error != NSS_STATUS_NOTFOUND) {
393 total_errors++;
394 printf("ERROR Non existant gid gave error %d\n", last_error);
398 int main(int argc, char *argv[])
400 if (argc > 1) so_path = argv[1];
401 if (argc > 2) nss_name = argv[2];
403 nss_test_users();
404 nss_test_groups();
405 nss_test_errors();
407 printf("total_errors=%d\n", total_errors);
409 return total_errors;