python:tests: Store keys as bytes rather than as lists of ints
[samba.git] / nsswitch / nsstest.c
1 /*
2    Unix SMB/CIFS implementation.
3    nss tester for winbindd
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Tim Potter 2003
6
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.
11
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.
16
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/>.
19 */
20
21 #include "replace.h"
22 #include "nsswitch/nsstest.h"
23
24 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
25
26 static const char *so_path = "/lib/libnss_winbind.so";
27 static const char *nss_name = "winbind";
28 static int nss_errno;
29 static NSS_STATUS last_error;
30 static int total_errors;
31
32 static void *find_fn(const char *name)
33 {
34         char *s;
35         static void *h;
36         void *res;
37
38         if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
39                 exit(1);
40         }
41
42         if (!h) {
43                 h = dlopen(so_path, RTLD_LAZY);
44         }
45         if (!h) {
46                 printf("Can't open shared library %s\n", so_path);
47                 exit(1);
48         }
49         res = dlsym(h, s);
50         if (!res) {
51                 printf("Can't find function %s\n", s);
52                 total_errors++;
53                 SAFE_FREE(s);
54                 return NULL;
55         }
56         SAFE_FREE(s);
57         return res;
58 }
59
60 static void report_nss_error(const char *who, NSS_STATUS status)
61 {
62         last_error = status;
63         total_errors++;
64         printf("ERROR %s: NSS_STATUS=%d  %d (nss_errno=%d)\n",
65                who, status, NSS_STATUS_SUCCESS, nss_errno);
66 }
67
68 static struct passwd *nss_getpwent(void)
69 {
70         NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
71                                       size_t , int *) =
72                 (NSS_STATUS (*)(struct passwd *, char *,
73                                 size_t, int *))find_fn("getpwent_r");
74         static struct passwd pwd;
75         static char buf[1000];
76         NSS_STATUS status;
77
78         if (!_nss_getpwent_r)
79                 return NULL;
80
81         status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
82         if (status == NSS_STATUS_NOTFOUND) {
83                 return NULL;
84         }
85         if (status != NSS_STATUS_SUCCESS) {
86                 report_nss_error("getpwent", status);
87                 return NULL;
88         }
89         return &pwd;
90 }
91
92 static struct passwd *nss_getpwnam(const char *name)
93 {
94         NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
95                                       size_t , int *) =
96                 (NSS_STATUS (*)(const char *, struct passwd *, char *,
97                                 size_t, int *))find_fn("getpwnam_r");
98         static struct passwd pwd;
99         static char buf[1000];
100         NSS_STATUS status;
101
102         if (!_nss_getpwnam_r)
103                 return NULL;
104
105         status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
106         if (status == NSS_STATUS_NOTFOUND) {
107                 return NULL;
108         }
109         if (status != NSS_STATUS_SUCCESS) {
110                 report_nss_error("getpwnam", status);
111                 return NULL;
112         }
113         return &pwd;
114 }
115
116 static struct passwd *nss_getpwuid(uid_t uid)
117 {
118         NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
119                                       size_t , int *) =
120                 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
121                                 size_t, int *))find_fn("getpwuid_r");
122         static struct passwd pwd;
123         static char buf[1000];
124         NSS_STATUS status;
125
126         if (!_nss_getpwuid_r)
127                 return NULL;
128
129         status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
130         if (status == NSS_STATUS_NOTFOUND) {
131                 return NULL;
132         }
133         if (status != NSS_STATUS_SUCCESS) {
134                 report_nss_error("getpwuid", status);
135                 return NULL;
136         }
137         return &pwd;
138 }
139
140 static void samba_nss_setpwent(void)
141 {
142         NSS_STATUS (*_nss_setpwent)(void) =
143                 (NSS_STATUS(*)(void))find_fn("setpwent");
144         NSS_STATUS status;
145
146         if (!_nss_setpwent)
147                 return;
148
149         status = _nss_setpwent();
150         if (status != NSS_STATUS_SUCCESS) {
151                 report_nss_error("setpwent", status);
152         }
153 }
154
155 static void samba_nss_endpwent(void)
156 {
157         NSS_STATUS (*_nss_endpwent)(void) =
158                 (NSS_STATUS (*)(void))find_fn("endpwent");
159         NSS_STATUS status;
160
161         if (!_nss_endpwent)
162                 return;
163
164         status = _nss_endpwent();
165         if (status != NSS_STATUS_SUCCESS) {
166                 report_nss_error("endpwent", status);
167         }
168 }
169
170
171 static struct group *nss_getgrent(void)
172 {
173         NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
174                                       size_t , int *) =
175                 (NSS_STATUS (*)(struct group *, char *,
176                                 size_t, int *))find_fn("getgrent_r");
177         static struct group grp;
178         static char *buf;
179         static int buflen = 1024;
180         NSS_STATUS status;
181
182         if (!_nss_getgrent_r)
183                 return NULL;
184
185         if (!buf)
186                 buf = (char *)malloc(buflen);
187
188 again:
189         status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
190         if (status == NSS_STATUS_TRYAGAIN) {
191                 char *oldbuf = buf;
192                 buflen *= 2;
193                 buf = (char *)realloc(buf, buflen);
194                 if (!buf) {
195                         SAFE_FREE(oldbuf);
196                         return NULL;
197                 }
198                 goto again;
199         }
200         if (status == NSS_STATUS_NOTFOUND) {
201                 SAFE_FREE(buf);
202                 return NULL;
203         }
204         if (status != NSS_STATUS_SUCCESS) {
205                 report_nss_error("getgrent", status);
206                 SAFE_FREE(buf);
207                 return NULL;
208         }
209         return &grp;
210 }
211
212 static struct group *nss_getgrnam(const char *name)
213 {
214         NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
215                                       size_t , int *) =
216                 (NSS_STATUS (*)(const char *, struct group *, char *,
217                                 size_t, int *))find_fn("getgrnam_r");
218         static struct group grp;
219         static char *buf;
220         static int buflen = 1000;
221         NSS_STATUS status;
222
223         if (!_nss_getgrnam_r)
224                 return NULL;
225
226         if (!buf)
227                 buf = (char *)malloc(buflen);
228 again:
229         status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
230         if (status == NSS_STATUS_TRYAGAIN) {
231                 char *oldbuf = buf;
232                 buflen *= 2;
233                 buf = (char *)realloc(buf, buflen);
234                 if (!buf) {
235                         SAFE_FREE(oldbuf);
236                         return NULL;
237                 }
238                 goto again;
239         }
240         if (status == NSS_STATUS_NOTFOUND) {
241                 SAFE_FREE(buf);
242                 return NULL;
243         }
244         if (status != NSS_STATUS_SUCCESS) {
245                 report_nss_error("getgrnam", status);
246                 SAFE_FREE(buf);
247                 return NULL;
248         }
249         return &grp;
250 }
251
252 static struct group *nss_getgrgid(gid_t gid)
253 {
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;
262
263         if (!_nss_getgrgid_r)
264                 return NULL;
265
266         if (!buf)
267                 buf = (char *)malloc(buflen);
268
269 again:
270         status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
271         if (status == NSS_STATUS_TRYAGAIN) {
272                 char *oldbuf = buf;
273                 buflen *= 2;
274                 buf = (char *)realloc(buf, buflen);
275                 if (!buf) {
276                         SAFE_FREE(oldbuf);
277                         return NULL;
278                 }
279                 goto again;
280         }
281         if (status == NSS_STATUS_NOTFOUND) {
282                 SAFE_FREE(buf);
283                 return NULL;
284         }
285         if (status != NSS_STATUS_SUCCESS) {
286                 report_nss_error("getgrgid", status);
287                 SAFE_FREE(buf);
288                 return NULL;
289         }
290         return &grp;
291 }
292
293 static void samba_nss_setgrent(void)
294 {
295         NSS_STATUS (*_nss_setgrent)(void) =
296                 (NSS_STATUS (*)(void))find_fn("setgrent");
297         NSS_STATUS status;
298
299         if (!_nss_setgrent)
300                 return;
301
302         status = _nss_setgrent();
303         if (status != NSS_STATUS_SUCCESS) {
304                 report_nss_error("setgrent", status);
305         }
306 }
307
308 static void samba_nss_endgrent(void)
309 {
310         NSS_STATUS (*_nss_endgrent)(void) =
311                 (NSS_STATUS (*)(void))find_fn("endgrent");
312         NSS_STATUS status;
313
314         if (!_nss_endgrent)
315                 return;
316
317         status = _nss_endgrent();
318         if (status != NSS_STATUS_SUCCESS) {
319                 report_nss_error("endgrent", status);
320         }
321 }
322
323 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
324 {
325         NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
326                                       long int *, gid_t **, long int , int *) =
327                 (NSS_STATUS (*)(char *, gid_t, long int *,
328                                 long int *, gid_t **,
329                                 long int, int *))find_fn("initgroups_dyn");
330         NSS_STATUS status;
331
332         if (!_nss_initgroups)
333                 return NSS_STATUS_UNAVAIL;
334
335         status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
336         if (status != NSS_STATUS_SUCCESS) {
337                 report_nss_error("initgroups", status);
338         }
339         return status;
340 }
341
342 static void print_passwd(struct passwd *pwd)
343 {
344         printf("%s:%s:%lu:%lu:%s:%s:%s\n",
345                pwd->pw_name,
346                pwd->pw_passwd,
347                (unsigned long)pwd->pw_uid,
348                (unsigned long)pwd->pw_gid,
349                pwd->pw_gecos,
350                pwd->pw_dir,
351                pwd->pw_shell);
352 }
353
354 static void print_group(struct group *grp)
355 {
356         int i;
357         printf("%s:%s:%lu:",
358                grp->gr_name,
359                grp->gr_passwd,
360                (unsigned long)grp->gr_gid);
361
362         if (!grp->gr_mem[0]) {
363                 printf("\n");
364                 return;
365         }
366
367         for (i=0; grp->gr_mem[i+1]; i++) {
368                 printf("%s,", grp->gr_mem[i]);
369         }
370         printf("%s\n", grp->gr_mem[i]);
371 }
372
373 static void nss_test_initgroups(char *name, gid_t gid)
374 {
375         long int size = 16;
376         long int start = 1;
377         gid_t *groups = NULL;
378         int i;
379         NSS_STATUS status;
380
381         groups = (gid_t *)malloc(sizeof(gid_t) * size);
382         if (groups == NULL) {
383                 printf("Unable to allocate memory for groups\n");
384                 return;
385         }
386         groups[0] = gid;
387
388         status = nss_initgroups(name, gid, &groups, &start, &size);
389         if (status == NSS_STATUS_UNAVAIL) {
390                 printf("No initgroups fn\n");
391                 return;
392         }
393
394         for (i=0; i<start-1; i++) {
395                 printf("%lu, ", (unsigned long)groups[i]);
396         }
397         printf("%lu\n", (unsigned long)groups[i]);
398 }
399
400
401 static void nss_test_users(void)
402 {
403         struct passwd *pwd;
404
405         samba_nss_setpwent();
406         /* loop over all users */
407         while ((pwd = nss_getpwent())) {
408                 printf("Testing user %s\n", pwd->pw_name);
409                 printf("getpwent:   "); print_passwd(pwd);
410                 pwd = nss_getpwuid(pwd->pw_uid);
411                 if (!pwd) {
412                         total_errors++;
413                         printf("ERROR: can't getpwuid\n");
414                         continue;
415                 }
416                 printf("getpwuid:   "); print_passwd(pwd);
417                 pwd = nss_getpwnam(pwd->pw_name);
418                 if (!pwd) {
419                         total_errors++;
420                         printf("ERROR: can't getpwnam\n");
421                         continue;
422                 }
423                 printf("getpwnam:   "); print_passwd(pwd);
424                 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
425                 printf("\n");
426         }
427         samba_nss_endpwent();
428 }
429
430 static void nss_test_groups(void)
431 {
432         struct group *grp;
433
434         samba_nss_setgrent();
435         /* loop over all groups */
436         while ((grp = nss_getgrent())) {
437                 printf("Testing group %s\n", grp->gr_name);
438                 printf("getgrent: "); print_group(grp);
439                 grp = nss_getgrnam(grp->gr_name);
440                 if (!grp) {
441                         total_errors++;
442                         printf("ERROR: can't getgrnam\n");
443                         continue;
444                 }
445                 printf("getgrnam: "); print_group(grp);
446                 grp = nss_getgrgid(grp->gr_gid);
447                 if (!grp) {
448                         total_errors++;
449                         printf("ERROR: can't getgrgid\n");
450                         continue;
451                 }
452                 printf("getgrgid: "); print_group(grp);
453                 printf("\n");
454         }
455         samba_nss_endgrent();
456 }
457
458 static void nss_test_errors(void)
459 {
460         struct passwd *pwd;
461         struct group *grp;
462
463         pwd = getpwnam("nosuchname");
464         if (pwd || last_error != NSS_STATUS_NOTFOUND) {
465                 total_errors++;
466                 printf("ERROR Non existent user gave error %d\n", last_error);
467         }
468
469         pwd = getpwuid(0xFF00);
470         if (pwd || last_error != NSS_STATUS_NOTFOUND) {
471                 total_errors++;
472                 printf("ERROR Non existent uid gave error %d\n", last_error);
473         }
474
475         grp = getgrnam("nosuchgroup");
476         if (grp || last_error != NSS_STATUS_NOTFOUND) {
477                 total_errors++;
478                 printf("ERROR Non existent group gave error %d\n", last_error);
479         }
480
481         grp = getgrgid(0xFFF0);
482         if (grp || last_error != NSS_STATUS_NOTFOUND) {
483                 total_errors++;
484                 printf("ERROR Non existent gid gave error %d\n", last_error);
485         }
486 }
487
488  int main(int argc, char *argv[])
489 {
490         if (argc > 1) so_path = argv[1];
491         if (argc > 2) nss_name = argv[2];
492
493         nss_test_users();
494         nss_test_groups();
495         nss_test_errors();
496
497         printf("total_errors=%d\n", total_errors);
498
499         return total_errors;
500 }