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