add some comments
[tridge/junkcode.git] / nsstest.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    nss tester
5
6    Copyright (C) Andrew Tridgell 2001-2004
7    
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public
10    License as published by the Free Software Foundation; either
11    version 2 of the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with this library; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <nss.h>
26 #include <dlfcn.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 typedef enum nss_status NSS_STATUS;
33
34 static const char *so_path = "/lib/libnss_winbind.so";
35 static char *nss_name;
36 static int nss_errno;
37 static NSS_STATUS last_error;
38 static int total_errors;
39
40 static void *find_fn(const char *name)
41 {
42         char s[1024];
43         static void *h;
44         void *res;
45
46         snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
47
48         if (!h) {
49                 h = dlopen(so_path, RTLD_LAZY);
50         }
51         if (!h) {
52                 printf("Can't open shared library %s : %s\n", so_path, dlerror());
53                 exit(1);
54         }
55         res = dlsym(h, s);
56         if (!res) {
57                 printf("Can't find function %s : %s\n", s, dlerror());
58                 return NULL;
59         }
60         return res;
61 }
62
63 static void report_nss_error(const char *who, NSS_STATUS status)
64 {
65         last_error = status;
66         total_errors++;
67         printf("ERROR %s: NSS_STATUS=%d  %d (nss_errno=%d)\n", 
68                who, status, NSS_STATUS_SUCCESS, nss_errno);
69 }
70
71 static struct passwd *nss_getpwent(void)
72 {
73         NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *, 
74                                       size_t , int *) = find_fn("getpwent_r");
75         static struct passwd pwd;
76         static char buf[1000];
77         NSS_STATUS status;
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 *) = find_fn("getpwnam_r");
94         static struct passwd pwd;
95         static char buf[1000];
96         NSS_STATUS status;
97         
98         status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
99         if (status == NSS_STATUS_NOTFOUND) {
100                 return NULL;
101         }
102         if (status != NSS_STATUS_SUCCESS) {
103                 report_nss_error("getpwnam", status);
104                 return NULL;
105         }
106         return &pwd;
107 }
108
109 static struct passwd *nss_getpwuid(uid_t uid)
110 {
111         NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *, 
112                                       size_t , int *) = find_fn("getpwuid_r");
113         static struct passwd pwd;
114         static char buf[1000];
115         NSS_STATUS status;
116         
117         status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
118         if (status == NSS_STATUS_NOTFOUND) {
119                 return NULL;
120         }
121         if (status != NSS_STATUS_SUCCESS) {
122                 report_nss_error("getpwuid", status);
123                 return NULL;
124         }
125         return &pwd;
126 }
127
128 static void nss_setpwent(void)
129 {
130         NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
131         NSS_STATUS status;
132         status = _nss_setpwent();
133         if (status != NSS_STATUS_SUCCESS) {
134                 report_nss_error("setpwent", status);
135         }
136 }
137
138 static void nss_endpwent(void)
139 {
140         NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
141         NSS_STATUS status;
142         status = _nss_endpwent();
143         if (status != NSS_STATUS_SUCCESS) {
144                 report_nss_error("endpwent", status);
145         }
146 }
147
148
149 static struct group *nss_getgrent(void)
150 {
151         NSS_STATUS (*_nss_getgrent_r)(struct group *, char *, 
152                                       size_t , int *) = find_fn("getgrent_r");
153         static struct group grp;
154         static char *buf;
155         static int buflen = 1024;
156         NSS_STATUS status;
157
158         if (!buf) buf = malloc(buflen);
159
160 again:  
161         status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
162         if (status == NSS_STATUS_TRYAGAIN) {
163                 buflen *= 2;
164                 buf = realloc(buf, buflen);
165                 goto again;
166         }
167         if (status == NSS_STATUS_NOTFOUND) {
168                 return NULL;
169         }
170         if (status != NSS_STATUS_SUCCESS) {
171                 report_nss_error("getgrent", status);
172                 return NULL;
173         }
174         return &grp;
175 }
176
177 static struct group *nss_getgrnam(const char *name)
178 {
179         NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *, 
180                                       size_t , int *) = find_fn("getgrnam_r");
181         static struct group grp;
182         static char *buf;
183         static int buflen = 1000;
184         NSS_STATUS status;
185
186         if (!buf) buf = malloc(buflen);
187 again:  
188         status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
189         if (status == NSS_STATUS_TRYAGAIN) {
190                 buflen *= 2;
191                 buf = realloc(buf, buflen);
192                 goto again;
193         }
194         if (status == NSS_STATUS_NOTFOUND) {
195                 return NULL;
196         }
197         if (status != NSS_STATUS_SUCCESS) {
198                 report_nss_error("getgrnam", status);
199                 return NULL;
200         }
201         return &grp;
202 }
203
204 static struct group *nss_getgrgid(gid_t gid)
205 {
206         NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *, 
207                                       size_t , int *) = find_fn("getgrgid_r");
208         static struct group grp;
209         static char *buf;
210         static int buflen = 1000;
211         NSS_STATUS status;
212         
213         if (!buf) buf = malloc(buflen);
214 again:  
215         status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
216         if (status == NSS_STATUS_TRYAGAIN) {
217                 buflen *= 2;
218                 buf = realloc(buf, buflen);
219                 goto again;
220         }
221         if (status == NSS_STATUS_NOTFOUND) {
222                 return NULL;
223         }
224         if (status != NSS_STATUS_SUCCESS) {
225                 report_nss_error("getgrgid", status);
226                 return NULL;
227         }
228         return &grp;
229 }
230
231 static void nss_setgrent(void)
232 {
233         NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
234         NSS_STATUS status;
235         status = _nss_setgrent();
236         if (status != NSS_STATUS_SUCCESS) {
237                 report_nss_error("setgrent", status);
238         }
239 }
240
241 static void nss_endgrent(void)
242 {
243         NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
244         NSS_STATUS status;
245         status = _nss_endgrent();
246         if (status != NSS_STATUS_SUCCESS) {
247                 report_nss_error("endgrent", status);
248         }
249 }
250
251 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
252 {
253         NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
254                                       long int *, gid_t **, long int , int *) = 
255                 find_fn("initgroups_dyn");
256         NSS_STATUS status;
257
258         if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
259
260         status = _nss_initgroups(user, group, start, size, groups, 2, &nss_errno);
261         printf("nss_errno=%d *size=%d\n", nss_errno, *size);
262         if (status != NSS_STATUS_SUCCESS) {
263                 report_nss_error("initgroups", status);
264         }
265         return status;
266 }
267
268 static void print_passwd(struct passwd *pwd)
269 {
270         printf("%s:%s:%d:%d:%s:%s:%s\n", 
271                pwd->pw_name,
272                pwd->pw_passwd,
273                pwd->pw_uid,
274                pwd->pw_gid,
275                pwd->pw_gecos,
276                pwd->pw_dir,
277                pwd->pw_shell);
278 }
279
280 static void print_group(struct group *grp)
281 {
282         int i;
283         printf("%s:%s:%d: ", 
284                grp->gr_name,
285                grp->gr_passwd,
286                grp->gr_gid);
287         
288         if (!grp->gr_mem[0]) {
289                 printf("\n");
290                 return;
291         }
292         
293         for (i=0; grp->gr_mem[i+1]; i++) {
294                 printf("%s, ", grp->gr_mem[i]);
295         }
296         printf("%s\n", grp->gr_mem[i]);
297 }
298
299 static void nss_test_initgroups(char *name, gid_t gid)
300 {
301         long int size = 16;
302         long int start = 1;
303         gid_t *groups = NULL;
304         int i;
305         NSS_STATUS status;
306
307         groups = (gid_t *)malloc(size * sizeof(gid_t));
308         groups[0] = gid;
309
310         status = nss_initgroups(name, gid, &groups, &start, &size);
311         if (status == NSS_STATUS_UNAVAIL) {
312                 printf("No initgroups fn\n");
313                 return;
314         }
315
316         for (i=0; i<start-1; i++) {
317                 printf("%d, ", groups[i]);
318         }
319         printf("%d\n", groups[i]);
320 }
321
322
323 static void nss_test_users(void)
324 {
325         struct passwd *pwd;
326
327         nss_setpwent();
328         /* loop over all users */
329         while ((pwd = nss_getpwent())) {
330                 printf("Testing user %s\n", pwd->pw_name);
331                 printf("getpwent:   "); print_passwd(pwd);
332                 pwd = nss_getpwuid(pwd->pw_uid);
333                 if (!pwd) {
334                         total_errors++;
335                         printf("ERROR: can't getpwuid\n");
336                         continue;
337                 }
338                 printf("getpwuid:   "); print_passwd(pwd);
339                 pwd = nss_getpwnam(pwd->pw_name);
340                 if (!pwd) {
341                         total_errors++;
342                         printf("ERROR: can't getpwnam\n");
343                         continue;
344                 }
345                 printf("getpwnam:   "); print_passwd(pwd);
346                 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
347                 printf("\n");
348         }
349         nss_endpwent();
350 }
351
352 static void nss_test_groups(void)
353 {
354         struct group *grp;
355
356         nss_setgrent();
357         /* loop over all groups */
358         while ((grp = nss_getgrent())) {
359                 printf("Testing group %s\n", grp->gr_name);
360                 printf("getgrent: "); print_group(grp);
361                 grp = nss_getgrnam(grp->gr_name);
362                 if (!grp) {
363                         total_errors++;
364                         printf("ERROR: can't getgrnam\n");
365                         continue;
366                 }
367                 printf("getgrnam: "); print_group(grp);
368                 grp = nss_getgrgid(grp->gr_gid);
369                 if (!grp) {
370                         total_errors++;
371                         printf("ERROR: can't getgrgid\n");
372                         continue;
373                 }
374                 printf("getgrgid: "); print_group(grp);
375                 printf("\n");
376         }
377         nss_endgrent();
378 }
379
380 static void nss_test_errors(void)
381 {
382         struct passwd *pwd;
383         struct group *grp;
384
385         pwd = getpwnam("nosuchname");
386         if (pwd || last_error != NSS_STATUS_NOTFOUND) {
387                 total_errors++;
388                 printf("ERROR Non existant user gave error %d\n", last_error);
389         }
390
391         pwd = getpwuid(0xFFF0);
392         if (pwd || last_error != NSS_STATUS_NOTFOUND) {
393                 total_errors++;
394                 printf("ERROR Non existant uid gave error %d\n", last_error);
395         }
396
397         grp = getgrnam("nosuchgroup");
398         if (grp || last_error != NSS_STATUS_NOTFOUND) {
399                 total_errors++;
400                 printf("ERROR Non existant group gave error %d\n", last_error);
401         }
402
403         grp = getgrgid(0xFFF0);
404         if (grp || last_error != NSS_STATUS_NOTFOUND) {
405                 total_errors++;
406                 printf("ERROR Non existant gid gave error %d\n", last_error);
407         }
408 }
409
410  int main(int argc, char *argv[])
411 {       
412         char *p;
413
414         if (argc > 1) so_path = argv[1];
415
416         p = strrchr(so_path, '_');
417         if (!p) {
418                 printf("Badly formed name for .so - must be libnss_FOO.so\n");
419                 exit(1);
420         }
421         nss_name = strdup(p+1);
422         p = strchr(nss_name, '.');
423         if (p) *p = 0;
424
425         printf("so_path=%s nss_name=%s\n\n", so_path, nss_name);
426
427         nss_test_users();
428         nss_test_groups();
429         nss_test_errors();
430
431         printf("total_errors=%d\n", total_errors);
432
433         return total_errors;
434 }