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