s4:dsdb/common: add samdb_domain_guid() helper function
[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 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 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                 buflen *= 2;
192                 buf = (char *)realloc(buf, buflen);
193                 if (!buf) {
194                         return NULL;
195                 }
196                 goto again;
197         }
198         if (status == NSS_STATUS_NOTFOUND) {
199                 SAFE_FREE(buf);
200                 return NULL;
201         }
202         if (status != NSS_STATUS_SUCCESS) {
203                 report_nss_error("getgrent", status);
204                 SAFE_FREE(buf);
205                 return NULL;
206         }
207         return &grp;
208 }
209
210 static struct group *nss_getgrnam(const char *name)
211 {
212         NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
213                                       size_t , int *) =
214                 (NSS_STATUS (*)(const char *, struct group *, char *,
215                                 size_t, int *))find_fn("getgrnam_r");
216         static struct group grp;
217         static char *buf;
218         static int buflen = 1000;
219         NSS_STATUS status;
220
221         if (!_nss_getgrnam_r)
222                 return NULL;
223
224         if (!buf)
225                 buf = (char *)malloc(buflen);
226 again:
227         status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
228         if (status == NSS_STATUS_TRYAGAIN) {
229                 buflen *= 2;
230                 buf = (char *)realloc(buf, buflen);
231                 if (!buf) {
232                         return NULL;
233                 }
234                 goto again;
235         }
236         if (status == NSS_STATUS_NOTFOUND) {
237                 SAFE_FREE(buf);
238                 return NULL;
239         }
240         if (status != NSS_STATUS_SUCCESS) {
241                 report_nss_error("getgrnam", status);
242                 SAFE_FREE(buf);
243                 return NULL;
244         }
245         return &grp;
246 }
247
248 static struct group *nss_getgrgid(gid_t gid)
249 {
250         NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
251                                       size_t , int *) =
252                 (NSS_STATUS (*)(gid_t, struct group *, char *,
253                                 size_t, int *))find_fn("getgrgid_r");
254         static struct group grp;
255         static char *buf;
256         static int buflen = 1000;
257         NSS_STATUS status;
258
259         if (!_nss_getgrgid_r)
260                 return NULL;
261
262         if (!buf)
263                 buf = (char *)malloc(buflen);
264
265 again:
266         status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
267         if (status == NSS_STATUS_TRYAGAIN) {
268                 buflen *= 2;
269                 buf = (char *)realloc(buf, buflen);
270                 if (!buf) {
271                         return NULL;
272                 }
273                 goto again;
274         }
275         if (status == NSS_STATUS_NOTFOUND) {
276                 SAFE_FREE(buf);
277                 return NULL;
278         }
279         if (status != NSS_STATUS_SUCCESS) {
280                 report_nss_error("getgrgid", status);
281                 SAFE_FREE(buf);
282                 return NULL;
283         }
284         return &grp;
285 }
286
287 static void nss_setgrent(void)
288 {
289         NSS_STATUS (*_nss_setgrent)(void) =
290                 (NSS_STATUS (*)(void))find_fn("setgrent");
291         NSS_STATUS status;
292
293         if (!_nss_setgrent)
294                 return;
295
296         status = _nss_setgrent();
297         if (status != NSS_STATUS_SUCCESS) {
298                 report_nss_error("setgrent", status);
299         }
300 }
301
302 static void nss_endgrent(void)
303 {
304         NSS_STATUS (*_nss_endgrent)(void) =
305                 (NSS_STATUS (*)(void))find_fn("endgrent");
306         NSS_STATUS status;
307
308         if (!_nss_endgrent)
309                 return;
310
311         status = _nss_endgrent();
312         if (status != NSS_STATUS_SUCCESS) {
313                 report_nss_error("endgrent", status);
314         }
315 }
316
317 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
318 {
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;
325
326         if (!_nss_initgroups)
327                 return NSS_STATUS_UNAVAIL;
328
329         status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
330         if (status != NSS_STATUS_SUCCESS) {
331                 report_nss_error("initgroups", status);
332         }
333         return status;
334 }
335
336 static void print_passwd(struct passwd *pwd)
337 {
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);
346 }
347
348 static void print_group(struct group *grp)
349 {
350         int i;
351         printf("%s:%s:%lu:",
352                grp->gr_name,
353                grp->gr_passwd,
354                (unsigned long)grp->gr_gid);
355
356         if (!grp->gr_mem[0]) {
357                 printf("\n");
358                 return;
359         }
360
361         for (i=0; grp->gr_mem[i+1]; i++) {
362                 printf("%s,", grp->gr_mem[i]);
363         }
364         printf("%s\n", grp->gr_mem[i]);
365 }
366
367 static void nss_test_initgroups(char *name, gid_t gid)
368 {
369         long int size = 16;
370         long int start = 1;
371         gid_t *groups = NULL;
372         int i;
373         NSS_STATUS status;
374
375         groups = (gid_t *)malloc(sizeof(gid_t) * size);
376         if (groups == NULL) {
377                 printf("Unable to allocate memory for groups\n");
378                 return;
379         }
380         groups[0] = gid;
381
382         status = nss_initgroups(name, gid, &groups, &start, &size);
383         if (status == NSS_STATUS_UNAVAIL) {
384                 printf("No initgroups fn\n");
385                 return;
386         }
387
388         for (i=0; i<start-1; i++) {
389                 printf("%lu, ", (unsigned long)groups[i]);
390         }
391         printf("%lu\n", (unsigned long)groups[i]);
392 }
393
394
395 static void nss_test_users(void)
396 {
397         struct passwd *pwd;
398
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;
409                 }
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;
416                 }
417                 printf("getpwnam:   "); print_passwd(pwd);
418                 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
419                 printf("\n");
420         }
421         nss_endpwent();
422 }
423
424 static void nss_test_groups(void)
425 {
426         struct group *grp;
427
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;
438                 }
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;
445                 }
446                 printf("getgrgid: "); print_group(grp);
447                 printf("\n");
448         }
449         nss_endgrent();
450 }
451
452 static void nss_test_errors(void)
453 {
454         struct passwd *pwd;
455         struct group *grp;
456
457         pwd = getpwnam("nosuchname");
458         if (pwd || last_error != NSS_STATUS_NOTFOUND) {
459                 total_errors++;
460                 printf("ERROR Non existent user gave error %d\n", last_error);
461         }
462
463         pwd = getpwuid(0xFFF0);
464         if (pwd || last_error != NSS_STATUS_NOTFOUND) {
465                 total_errors++;
466                 printf("ERROR Non existent uid gave error %d\n", last_error);
467         }
468
469         grp = getgrnam("nosuchgroup");
470         if (grp || last_error != NSS_STATUS_NOTFOUND) {
471                 total_errors++;
472                 printf("ERROR Non existent group gave error %d\n", last_error);
473         }
474
475         grp = getgrgid(0xFFF0);
476         if (grp || last_error != NSS_STATUS_NOTFOUND) {
477                 total_errors++;
478                 printf("ERROR Non existent gid gave error %d\n", last_error);
479         }
480 }
481
482  int main(int argc, char *argv[])
483 {
484         if (argc > 1) so_path = argv[1];
485         if (argc > 2) nss_name = argv[2];
486
487         nss_test_users();
488         nss_test_groups();
489         nss_test_errors();
490
491         printf("total_errors=%d\n", total_errors);
492
493         return total_errors;
494 }