CVE-2020-25719 heimdal:kdc: Move fetching krbtgt entry to before enctype selection
[Samba.git] / nsswitch / nsstest.c
blobe2ee9fbf3af47bd496727f487389d8d3588d8bcc
1 /*
2 Unix SMB/CIFS implementation.
3 nss tester for winbindd
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Tim Potter 2003
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.
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.
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/>.
21 #include "replace.h"
22 #include "nsswitch/nsstest.h"
24 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
26 static const char *so_path = "/lib/libnss_winbind.so";
27 static const char *nss_name = "winbind";
28 static int nss_errno;
29 static NSS_STATUS last_error;
30 static int total_errors;
32 static void *find_fn(const char *name)
34 char *s;
35 static void *h;
36 void *res;
38 if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
39 exit(1);
42 if (!h) {
43 h = dlopen(so_path, RTLD_LAZY);
45 if (!h) {
46 printf("Can't open shared library %s\n", so_path);
47 exit(1);
49 res = dlsym(h, s);
50 if (!res) {
51 printf("Can't find function %s\n", s);
52 total_errors++;
53 SAFE_FREE(s);
54 return NULL;
56 SAFE_FREE(s);
57 return res;
60 static void report_nss_error(const char *who, NSS_STATUS status)
62 last_error = status;
63 total_errors++;
64 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
65 who, status, NSS_STATUS_SUCCESS, nss_errno);
68 static struct passwd *nss_getpwent(void)
70 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
71 size_t , int *) =
72 (NSS_STATUS (*)(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;
78 if (!_nss_getpwent_r)
79 return NULL;
81 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
82 if (status == NSS_STATUS_NOTFOUND) {
83 return NULL;
85 if (status != NSS_STATUS_SUCCESS) {
86 report_nss_error("getpwent", status);
87 return NULL;
89 return &pwd;
92 static struct passwd *nss_getpwnam(const char *name)
94 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
95 size_t , int *) =
96 (NSS_STATUS (*)(const char *, struct passwd *, char *,
97 size_t, int *))find_fn("getpwnam_r");
98 static struct passwd pwd;
99 static char buf[1000];
100 NSS_STATUS status;
102 if (!_nss_getpwnam_r)
103 return NULL;
105 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
106 if (status == NSS_STATUS_NOTFOUND) {
107 return NULL;
109 if (status != NSS_STATUS_SUCCESS) {
110 report_nss_error("getpwnam", status);
111 return NULL;
113 return &pwd;
116 static struct passwd *nss_getpwuid(uid_t uid)
118 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
119 size_t , int *) =
120 (NSS_STATUS (*)(uid_t, struct passwd *, char *,
121 size_t, int *))find_fn("getpwuid_r");
122 static struct passwd pwd;
123 static char buf[1000];
124 NSS_STATUS status;
126 if (!_nss_getpwuid_r)
127 return NULL;
129 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
130 if (status == NSS_STATUS_NOTFOUND) {
131 return NULL;
133 if (status != NSS_STATUS_SUCCESS) {
134 report_nss_error("getpwuid", status);
135 return NULL;
137 return &pwd;
140 static void samba_nss_setpwent(void)
142 NSS_STATUS (*_nss_setpwent)(void) =
143 (NSS_STATUS(*)(void))find_fn("setpwent");
144 NSS_STATUS status;
146 if (!_nss_setpwent)
147 return;
149 status = _nss_setpwent();
150 if (status != NSS_STATUS_SUCCESS) {
151 report_nss_error("setpwent", status);
155 static void samba_nss_endpwent(void)
157 NSS_STATUS (*_nss_endpwent)(void) =
158 (NSS_STATUS (*)(void))find_fn("endpwent");
159 NSS_STATUS status;
161 if (!_nss_endpwent)
162 return;
164 status = _nss_endpwent();
165 if (status != NSS_STATUS_SUCCESS) {
166 report_nss_error("endpwent", status);
171 static struct group *nss_getgrent(void)
173 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
174 size_t , int *) =
175 (NSS_STATUS (*)(struct group *, char *,
176 size_t, int *))find_fn("getgrent_r");
177 static struct group grp;
178 static char *buf;
179 static int buflen = 1024;
180 NSS_STATUS status;
182 if (!_nss_getgrent_r)
183 return NULL;
185 if (!buf)
186 buf = (char *)malloc(buflen);
188 again:
189 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
190 if (status == NSS_STATUS_TRYAGAIN) {
191 char *oldbuf = buf;
192 buflen *= 2;
193 buf = (char *)realloc(buf, buflen);
194 if (!buf) {
195 SAFE_FREE(oldbuf);
196 return NULL;
198 goto again;
200 if (status == NSS_STATUS_NOTFOUND) {
201 SAFE_FREE(buf);
202 return NULL;
204 if (status != NSS_STATUS_SUCCESS) {
205 report_nss_error("getgrent", status);
206 SAFE_FREE(buf);
207 return NULL;
209 return &grp;
212 static struct group *nss_getgrnam(const char *name)
214 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
215 size_t , int *) =
216 (NSS_STATUS (*)(const char *, struct group *, char *,
217 size_t, int *))find_fn("getgrnam_r");
218 static struct group grp;
219 static char *buf;
220 static int buflen = 1000;
221 NSS_STATUS status;
223 if (!_nss_getgrnam_r)
224 return NULL;
226 if (!buf)
227 buf = (char *)malloc(buflen);
228 again:
229 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
230 if (status == NSS_STATUS_TRYAGAIN) {
231 char *oldbuf = buf;
232 buflen *= 2;
233 buf = (char *)realloc(buf, buflen);
234 if (!buf) {
235 SAFE_FREE(oldbuf);
236 return NULL;
238 goto again;
240 if (status == NSS_STATUS_NOTFOUND) {
241 SAFE_FREE(buf);
242 return NULL;
244 if (status != NSS_STATUS_SUCCESS) {
245 report_nss_error("getgrnam", status);
246 SAFE_FREE(buf);
247 return NULL;
249 return &grp;
252 static struct group *nss_getgrgid(gid_t gid)
254 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
255 size_t , int *) =
256 (NSS_STATUS (*)(gid_t, struct group *, char *,
257 size_t, int *))find_fn("getgrgid_r");
258 static struct group grp;
259 static char *buf;
260 static int buflen = 1000;
261 NSS_STATUS status;
263 if (!_nss_getgrgid_r)
264 return NULL;
266 if (!buf)
267 buf = (char *)malloc(buflen);
269 again:
270 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
271 if (status == NSS_STATUS_TRYAGAIN) {
272 char *oldbuf = buf;
273 buflen *= 2;
274 buf = (char *)realloc(buf, buflen);
275 if (!buf) {
276 SAFE_FREE(oldbuf);
277 return NULL;
279 goto again;
281 if (status == NSS_STATUS_NOTFOUND) {
282 SAFE_FREE(buf);
283 return NULL;
285 if (status != NSS_STATUS_SUCCESS) {
286 report_nss_error("getgrgid", status);
287 SAFE_FREE(buf);
288 return NULL;
290 return &grp;
293 static void samba_nss_setgrent(void)
295 NSS_STATUS (*_nss_setgrent)(void) =
296 (NSS_STATUS (*)(void))find_fn("setgrent");
297 NSS_STATUS status;
299 if (!_nss_setgrent)
300 return;
302 status = _nss_setgrent();
303 if (status != NSS_STATUS_SUCCESS) {
304 report_nss_error("setgrent", status);
308 static void samba_nss_endgrent(void)
310 NSS_STATUS (*_nss_endgrent)(void) =
311 (NSS_STATUS (*)(void))find_fn("endgrent");
312 NSS_STATUS status;
314 if (!_nss_endgrent)
315 return;
317 status = _nss_endgrent();
318 if (status != NSS_STATUS_SUCCESS) {
319 report_nss_error("endgrent", status);
323 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
325 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
326 long int *, gid_t **, long int , int *) =
327 (NSS_STATUS (*)(char *, gid_t, long int *,
328 long int *, gid_t **,
329 long int, int *))find_fn("initgroups_dyn");
330 NSS_STATUS status;
332 if (!_nss_initgroups)
333 return NSS_STATUS_UNAVAIL;
335 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
336 if (status != NSS_STATUS_SUCCESS) {
337 report_nss_error("initgroups", status);
339 return status;
342 static void print_passwd(struct passwd *pwd)
344 printf("%s:%s:%lu:%lu:%s:%s:%s\n",
345 pwd->pw_name,
346 pwd->pw_passwd,
347 (unsigned long)pwd->pw_uid,
348 (unsigned long)pwd->pw_gid,
349 pwd->pw_gecos,
350 pwd->pw_dir,
351 pwd->pw_shell);
354 static void print_group(struct group *grp)
356 int i;
357 printf("%s:%s:%lu:",
358 grp->gr_name,
359 grp->gr_passwd,
360 (unsigned long)grp->gr_gid);
362 if (!grp->gr_mem[0]) {
363 printf("\n");
364 return;
367 for (i=0; grp->gr_mem[i+1]; i++) {
368 printf("%s,", grp->gr_mem[i]);
370 printf("%s\n", grp->gr_mem[i]);
373 static void nss_test_initgroups(char *name, gid_t gid)
375 long int size = 16;
376 long int start = 1;
377 gid_t *groups = NULL;
378 int i;
379 NSS_STATUS status;
381 groups = (gid_t *)malloc(sizeof(gid_t) * size);
382 if (groups == NULL) {
383 printf("Unable to allocate memory for groups\n");
384 return;
386 groups[0] = gid;
388 status = nss_initgroups(name, gid, &groups, &start, &size);
389 if (status == NSS_STATUS_UNAVAIL) {
390 printf("No initgroups fn\n");
391 return;
394 for (i=0; i<start-1; i++) {
395 printf("%lu, ", (unsigned long)groups[i]);
397 printf("%lu\n", (unsigned long)groups[i]);
401 static void nss_test_users(void)
403 struct passwd *pwd;
405 samba_nss_setpwent();
406 /* loop over all users */
407 while ((pwd = nss_getpwent())) {
408 printf("Testing user %s\n", pwd->pw_name);
409 printf("getpwent: "); print_passwd(pwd);
410 pwd = nss_getpwuid(pwd->pw_uid);
411 if (!pwd) {
412 total_errors++;
413 printf("ERROR: can't getpwuid\n");
414 continue;
416 printf("getpwuid: "); print_passwd(pwd);
417 pwd = nss_getpwnam(pwd->pw_name);
418 if (!pwd) {
419 total_errors++;
420 printf("ERROR: can't getpwnam\n");
421 continue;
423 printf("getpwnam: "); print_passwd(pwd);
424 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
425 printf("\n");
427 samba_nss_endpwent();
430 static void nss_test_groups(void)
432 struct group *grp;
434 samba_nss_setgrent();
435 /* loop over all groups */
436 while ((grp = nss_getgrent())) {
437 printf("Testing group %s\n", grp->gr_name);
438 printf("getgrent: "); print_group(grp);
439 grp = nss_getgrnam(grp->gr_name);
440 if (!grp) {
441 total_errors++;
442 printf("ERROR: can't getgrnam\n");
443 continue;
445 printf("getgrnam: "); print_group(grp);
446 grp = nss_getgrgid(grp->gr_gid);
447 if (!grp) {
448 total_errors++;
449 printf("ERROR: can't getgrgid\n");
450 continue;
452 printf("getgrgid: "); print_group(grp);
453 printf("\n");
455 samba_nss_endgrent();
458 static void nss_test_errors(void)
460 struct passwd *pwd;
461 struct group *grp;
463 pwd = getpwnam("nosuchname");
464 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
465 total_errors++;
466 printf("ERROR Non existent user gave error %d\n", last_error);
469 pwd = getpwuid(0xFFF0);
470 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
471 total_errors++;
472 printf("ERROR Non existent uid gave error %d\n", last_error);
475 grp = getgrnam("nosuchgroup");
476 if (grp || last_error != NSS_STATUS_NOTFOUND) {
477 total_errors++;
478 printf("ERROR Non existent group gave error %d\n", last_error);
481 grp = getgrgid(0xFFF0);
482 if (grp || last_error != NSS_STATUS_NOTFOUND) {
483 total_errors++;
484 printf("ERROR Non existent gid gave error %d\n", last_error);
488 int main(int argc, char *argv[])
490 if (argc > 1) so_path = argv[1];
491 if (argc > 2) nss_name = argv[2];
493 nss_test_users();
494 nss_test_groups();
495 nss_test_errors();
497 printf("total_errors=%d\n", total_errors);
499 return total_errors;