- check for correct error codes
[gd/samba/.git] / source3 / torture / nsstest.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    nss tester for winbindd
5    Copyright (C) Andrew Tridgell 2001
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 char *so_path = "/lib/libnss_winbind.so";
25 static char *nss_name = "winbind";
26 static int nss_errno;
27
28 static void *find_fn(const char *name)
29 {
30         char s[1024];
31         static void *h;
32         void *res;
33
34         snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
35
36         if (!h) {
37                 h = dlopen(so_path, RTLD_LAZY);
38         }
39         if (!h) {
40                 printf("Can't open shared library %s\n", so_path);
41                 exit(1);
42         }
43         res = dlsym(h, s);
44         if (!res) {
45                 printf("Can't find function %s\n", s);
46                 return NULL;
47         }
48         return res;
49 }
50
51 static void report_nss_error(const char *who, NSS_STATUS status)
52 {
53         printf("ERROR %s: NSS_STATUS=%d  %d\n", who, status, NSS_STATUS_SUCCESS);
54 }
55
56 static struct passwd *nss_getpwent(void)
57 {
58         NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *, 
59                                       size_t , int *) = find_fn("getpwent_r");
60         static struct passwd pwd;
61         static char buf[1000];
62         NSS_STATUS status;
63
64         status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
65         if (status == NSS_STATUS_NOTFOUND) {
66                 return NULL;
67         }
68         if (status != NSS_STATUS_SUCCESS) {
69                 report_nss_error("getpwent", status);
70                 return NULL;
71         }
72         return &pwd;
73 }
74
75 static struct passwd *nss_getpwnam(const char *name)
76 {
77         NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *, 
78                                       size_t , int *) = find_fn("getpwnam_r");
79         static struct passwd pwd;
80         static char buf[1000];
81         NSS_STATUS status;
82         
83         status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
84         if (status == NSS_STATUS_NOTFOUND) {
85                 return NULL;
86         }
87         if (status != NSS_STATUS_SUCCESS) {
88                 report_nss_error("getpwnam", status);
89                 return NULL;
90         }
91         return &pwd;
92 }
93
94 static struct passwd *nss_getpwuid(uid_t uid)
95 {
96         NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *, 
97                                       size_t , int *) = find_fn("getpwuid_r");
98         static struct passwd pwd;
99         static char buf[1000];
100         NSS_STATUS status;
101         
102         status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
103         if (status == NSS_STATUS_NOTFOUND) {
104                 return NULL;
105         }
106         if (status != NSS_STATUS_SUCCESS) {
107                 report_nss_error("getpwuid", status);
108                 return NULL;
109         }
110         return &pwd;
111 }
112
113 static void nss_setpwent(void)
114 {
115         NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
116         NSS_STATUS status;
117         status = _nss_setpwent();
118         if (status != NSS_STATUS_SUCCESS) {
119                 report_nss_error("setpwent", status);
120         }
121 }
122
123 static void nss_endpwent(void)
124 {
125         NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
126         NSS_STATUS status;
127         status = _nss_endpwent();
128         if (status != NSS_STATUS_SUCCESS) {
129                 report_nss_error("endpwent", status);
130         }
131 }
132
133
134 static struct group *nss_getgrent(void)
135 {
136         NSS_STATUS (*_nss_getgrent_r)(struct group *, char *, 
137                                       size_t , int *) = find_fn("getgrent_r");
138         static struct group grp;
139         static char buf[1000];
140         NSS_STATUS status;
141         
142         status = _nss_getgrent_r(&grp, buf, sizeof(buf), &nss_errno);
143         if (status == NSS_STATUS_NOTFOUND) {
144                 return NULL;
145         }
146         if (status != NSS_STATUS_SUCCESS) {
147                 report_nss_error("getgrent", status);
148                 return NULL;
149         }
150         return &grp;
151 }
152
153 static struct group *nss_getgrnam(const char *name)
154 {
155         NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *, 
156                                       size_t , int *) = find_fn("getgrnam_r");
157         static struct group grp;
158         static char buf[1000];
159         NSS_STATUS status;
160         
161         status = _nss_getgrnam_r(name, &grp, buf, sizeof(buf), &nss_errno);
162         if (status == NSS_STATUS_NOTFOUND) {
163                 return NULL;
164         }
165         if (status != NSS_STATUS_SUCCESS) {
166                 report_nss_error("getgrnam", status);
167                 return NULL;
168         }
169         return &grp;
170 }
171
172 static struct group *nss_getgrgid(gid_t gid)
173 {
174         NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *, 
175                                       size_t , int *) = find_fn("getgrgid_r");
176         static struct group grp;
177         static char buf[1000];
178         NSS_STATUS status;
179         
180         status = _nss_getgrgid_r(gid, &grp, buf, sizeof(buf), &nss_errno);
181         if (status == NSS_STATUS_NOTFOUND) {
182                 return NULL;
183         }
184         if (status != NSS_STATUS_SUCCESS) {
185                 report_nss_error("getgrgid", status);
186                 return NULL;
187         }
188         return &grp;
189 }
190
191 static void nss_setgrent(void)
192 {
193         NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
194         NSS_STATUS status;
195         status = _nss_setgrent();
196         if (status != NSS_STATUS_SUCCESS) {
197                 report_nss_error("setgrent", status);
198         }
199 }
200
201 static void nss_endgrent(void)
202 {
203         NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
204         NSS_STATUS status;
205         status = _nss_endgrent();
206         if (status != NSS_STATUS_SUCCESS) {
207                 report_nss_error("endgrent", status);
208         }
209 }
210
211 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
212 {
213         NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
214                                       long int *, gid_t **, long int , int *) = 
215                 find_fn("initgroups_dyn");
216         NSS_STATUS status;
217
218         if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
219
220         status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
221         if (status != NSS_STATUS_SUCCESS) {
222                 report_nss_error("initgroups", status);
223         }
224         return status;
225 }
226
227 static void print_passwd(struct passwd *pwd)
228 {
229         printf("%s:%s:%d:%d:%s:%s:%s\n", 
230                pwd->pw_name,
231                pwd->pw_passwd,
232                pwd->pw_uid,
233                pwd->pw_gid,
234                pwd->pw_gecos,
235                pwd->pw_dir,
236                pwd->pw_shell);
237 }
238
239 static void print_group(struct group *grp)
240 {
241         int i;
242         printf("%s:%s:%d: ", 
243                grp->gr_name,
244                grp->gr_passwd,
245                grp->gr_gid);
246         
247         if (!grp->gr_mem[0]) {
248                 printf("\n");
249                 return;
250         }
251         
252         for (i=0; grp->gr_mem[i+1]; i++) {
253                 printf("%s, ", grp->gr_mem[i]);
254         }
255         printf("%s\n", grp->gr_mem[i]);
256 }
257
258 static void nss_test_initgroups(char *name, gid_t gid)
259 {
260         long int size = 16;
261         long int start = 1;
262         gid_t *groups = NULL;
263         int i;
264         NSS_STATUS status;
265
266         groups = (gid_t *)malloc(size * sizeof(gid_t));
267         groups[0] = gid;
268
269         status = nss_initgroups(name, gid, &groups, &start, &size);
270         if (status == NSS_STATUS_UNAVAIL) {
271                 printf("No initgroups fn\n");
272                 return;
273         }
274
275         for (i=0; i<start-1; i++) {
276                 printf("%d, ", groups[i]);
277         }
278         printf("%d\n", groups[i]);
279 }
280
281
282 static void nss_test_users(void)
283 {
284         struct passwd *pwd;
285
286         nss_setpwent();
287         /* loop over all users */
288         while ((pwd = nss_getpwent())) {
289                 printf("Testing user %s\n", pwd->pw_name);
290                 printf("getpwent:   "); print_passwd(pwd);
291                 pwd = nss_getpwuid(pwd->pw_uid);
292                 printf("getpwuid:   "); print_passwd(pwd);
293                 pwd = nss_getpwnam(pwd->pw_name);
294                 printf("getpwnam:   "); print_passwd(pwd);
295                 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
296                 printf("\n");
297         }
298         nss_endpwent();
299 }
300
301 static void nss_test_groups(void)
302 {
303         struct group *grp;
304
305         nss_setgrent();
306         /* loop over all groups */
307         while ((grp = nss_getgrent())) {
308                 printf("Testing group %s\n", grp->gr_name);
309                 printf("getgrent: "); print_group(grp);
310                 grp = nss_getgrnam(grp->gr_name);
311                 printf("getgrnam: "); print_group(grp);
312                 grp = nss_getgrgid(grp->gr_gid);
313                 printf("getgrgid: "); print_group(grp);
314                 printf("\n");
315         }
316         nss_endgrent();
317 }
318
319
320  int main(int argc, char *argv[])
321 {       
322         if (argc > 1) so_path = argv[1];
323         if (argc > 2) nss_name = argv[2];
324
325         nss_test_users();
326         nss_test_groups();
327
328         return 0;
329 }