3aeaa177a13681c3ce38c4af3864c522b86fc261
[kai/samba.git] / source3 / torture / nsstest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    nss tester for winbindd
4    Copyright (C) Andrew Tridgell 2001
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 static char *so_path = "/lib/libnss_winbind.so";
24 static char *nss_name = "winbind";
25 static int nss_errno;
26 static NSS_STATUS last_error;
27 static int total_errors;
28
29 static void *find_fn(const char *name)
30 {
31         char s[1024];
32         static void *h;
33         void *res;
34
35         snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
36
37         if (!h) {
38                 h = dlopen(so_path, RTLD_LAZY);
39         }
40         if (!h) {
41                 printf("Can't open shared library %s\n", so_path);
42                 exit(1);
43         }
44         res = dlsym(h, s);
45         if (!res) {
46                 printf("Can't find function %s\n", s);
47                 return NULL;
48         }
49         return res;
50 }
51
52 static void report_nss_error(const char *who, NSS_STATUS status)
53 {
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);
58 }
59
60 static struct passwd *nss_getpwent(void)
61 {
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;
67
68         status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
69         if (status == NSS_STATUS_NOTFOUND) {
70                 return NULL;
71         }
72         if (status != NSS_STATUS_SUCCESS) {
73                 report_nss_error("getpwent", status);
74                 return NULL;
75         }
76         return &pwd;
77 }
78
79 static struct passwd *nss_getpwnam(const char *name)
80 {
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;
86         
87         status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
88         if (status == NSS_STATUS_NOTFOUND) {
89                 return NULL;
90         }
91         if (status != NSS_STATUS_SUCCESS) {
92                 report_nss_error("getpwnam", status);
93                 return NULL;
94         }
95         return &pwd;
96 }
97
98 static struct passwd *nss_getpwuid(uid_t uid)
99 {
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;
105         
106         status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
107         if (status == NSS_STATUS_NOTFOUND) {
108                 return NULL;
109         }
110         if (status != NSS_STATUS_SUCCESS) {
111                 report_nss_error("getpwuid", status);
112                 return NULL;
113         }
114         return &pwd;
115 }
116
117 static void nss_setpwent(void)
118 {
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);
124         }
125 }
126
127 static void nss_endpwent(void)
128 {
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);
134         }
135 }
136
137
138 static struct group *nss_getgrent(void)
139 {
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;
146
147         if (!buf) buf = malloc(buflen);
148
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;
155         }
156         if (status == NSS_STATUS_NOTFOUND) {
157                 return NULL;
158         }
159         if (status != NSS_STATUS_SUCCESS) {
160                 report_nss_error("getgrent", status);
161                 return NULL;
162         }
163         return &grp;
164 }
165
166 static struct group *nss_getgrnam(const char *name)
167 {
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;
174
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;
182         }
183         if (status == NSS_STATUS_NOTFOUND) {
184                 return NULL;
185         }
186         if (status != NSS_STATUS_SUCCESS) {
187                 report_nss_error("getgrnam", status);
188                 return NULL;
189         }
190         return &grp;
191 }
192
193 static struct group *nss_getgrgid(gid_t gid)
194 {
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;
201         
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;
209         }
210         if (status == NSS_STATUS_NOTFOUND) {
211                 return NULL;
212         }
213         if (status != NSS_STATUS_SUCCESS) {
214                 report_nss_error("getgrgid", status);
215                 return NULL;
216         }
217         return &grp;
218 }
219
220 static void nss_setgrent(void)
221 {
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);
227         }
228 }
229
230 static void nss_endgrent(void)
231 {
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);
237         }
238 }
239
240 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
241 {
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;
246
247         if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
248
249         status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
250         if (status != NSS_STATUS_SUCCESS) {
251                 report_nss_error("initgroups", status);
252         }
253         return status;
254 }
255
256 static void print_passwd(struct passwd *pwd)
257 {
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);
266 }
267
268 static void print_group(struct group *grp)
269 {
270         int i;
271         printf("%s:%s:%d: ", 
272                grp->gr_name,
273                grp->gr_passwd,
274                grp->gr_gid);
275         
276         if (!grp->gr_mem[0]) {
277                 printf("\n");
278                 return;
279         }
280         
281         for (i=0; grp->gr_mem[i+1]; i++) {
282                 printf("%s, ", grp->gr_mem[i]);
283         }
284         printf("%s\n", grp->gr_mem[i]);
285 }
286
287 static void nss_test_initgroups(char *name, gid_t gid)
288 {
289         long int size = 16;
290         long int start = 1;
291         gid_t *groups = NULL;
292         int i;
293         NSS_STATUS status;
294
295         groups = (gid_t *)malloc(size * sizeof(gid_t));
296         groups[0] = gid;
297
298         status = nss_initgroups(name, gid, &groups, &start, &size);
299         if (status == NSS_STATUS_UNAVAIL) {
300                 printf("No initgroups fn\n");
301                 return;
302         }
303
304         for (i=0; i<start-1; i++) {
305                 printf("%d, ", groups[i]);
306         }
307         printf("%d\n", groups[i]);
308 }
309
310
311 static void nss_test_users(void)
312 {
313         struct passwd *pwd;
314
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;
325                 }
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;
332                 }
333                 printf("getpwnam:   "); print_passwd(pwd);
334                 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
335                 printf("\n");
336         }
337         nss_endpwent();
338 }
339
340 static void nss_test_groups(void)
341 {
342         struct group *grp;
343
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;
354                 }
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;
361                 }
362                 printf("getgrgid: "); print_group(grp);
363                 printf("\n");
364         }
365         nss_endgrent();
366 }
367
368 static void nss_test_errors(void)
369 {
370         struct passwd *pwd;
371         struct group *grp;
372
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);
377         }
378
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);
383         }
384
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);
389         }
390
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);
395         }
396 }
397
398  int main(int argc, char *argv[])
399 {       
400         if (argc > 1) so_path = argv[1];
401         if (argc > 2) nss_name = argv[2];
402
403         nss_test_users();
404         nss_test_groups();
405         nss_test_errors();
406
407         printf("total_errors=%d\n", total_errors);
408
409         return total_errors;
410 }