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