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