2 Unix SMB/CIFS implementation.
3 nss tester for winbindd
4 Copyright (C) Andrew Tridgell 2001
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.
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.
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.
23 static const char *so_path = "/lib/libnss_winbind.so";
24 static const char *nss_name = "winbind";
26 static NSS_STATUS last_error;
27 static int total_errors;
29 static void *find_fn(const char *name)
35 snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
38 h = sys_dlopen(so_path, RTLD_LAZY);
41 printf("Can't open shared library %s\n", so_path);
44 res = sys_dlsym(h, s);
46 printf("Can't find function %s\n", s);
52 static void report_nss_error(const char *who, NSS_STATUS status)
56 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
57 who, status, NSS_STATUS_SUCCESS, nss_errno);
60 static struct passwd *nss_getpwent(void)
62 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
63 size_t , int *) = find_fn("getpwent_r");
64 static struct passwd pwd;
65 static char buf[1000];
68 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
69 if (status == NSS_STATUS_NOTFOUND) {
72 if (status != NSS_STATUS_SUCCESS) {
73 report_nss_error("getpwent", status);
79 static struct passwd *nss_getpwnam(const char *name)
81 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
82 size_t , int *) = find_fn("getpwnam_r");
83 static struct passwd pwd;
84 static char buf[1000];
87 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
88 if (status == NSS_STATUS_NOTFOUND) {
91 if (status != NSS_STATUS_SUCCESS) {
92 report_nss_error("getpwnam", status);
98 static struct passwd *nss_getpwuid(uid_t uid)
100 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
101 size_t , int *) = find_fn("getpwuid_r");
102 static struct passwd pwd;
103 static char buf[1000];
106 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
107 if (status == NSS_STATUS_NOTFOUND) {
110 if (status != NSS_STATUS_SUCCESS) {
111 report_nss_error("getpwuid", status);
117 static void nss_setpwent(void)
119 NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
121 status = _nss_setpwent();
122 if (status != NSS_STATUS_SUCCESS) {
123 report_nss_error("setpwent", status);
127 static void nss_endpwent(void)
129 NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
131 status = _nss_endpwent();
132 if (status != NSS_STATUS_SUCCESS) {
133 report_nss_error("endpwent", status);
138 static struct group *nss_getgrent(void)
140 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
141 size_t , int *) = find_fn("getgrent_r");
142 static struct group grp;
144 static int buflen = 1024;
147 if (!buf) buf = malloc(buflen);
150 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
151 if (status == NSS_STATUS_TRYAGAIN) {
153 buf = realloc(buf, buflen);
156 if (status == NSS_STATUS_NOTFOUND) {
159 if (status != NSS_STATUS_SUCCESS) {
160 report_nss_error("getgrent", status);
166 static struct group *nss_getgrnam(const char *name)
168 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
169 size_t , int *) = find_fn("getgrnam_r");
170 static struct group grp;
172 static int buflen = 1000;
175 if (!buf) buf = malloc(buflen);
177 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
178 if (status == NSS_STATUS_TRYAGAIN) {
180 buf = realloc(buf, buflen);
183 if (status == NSS_STATUS_NOTFOUND) {
186 if (status != NSS_STATUS_SUCCESS) {
187 report_nss_error("getgrnam", status);
193 static struct group *nss_getgrgid(gid_t gid)
195 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
196 size_t , int *) = find_fn("getgrgid_r");
197 static struct group grp;
199 static int buflen = 1000;
202 if (!buf) buf = malloc(buflen);
204 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
205 if (status == NSS_STATUS_TRYAGAIN) {
207 buf = realloc(buf, buflen);
210 if (status == NSS_STATUS_NOTFOUND) {
213 if (status != NSS_STATUS_SUCCESS) {
214 report_nss_error("getgrgid", status);
220 static void nss_setgrent(void)
222 NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
224 status = _nss_setgrent();
225 if (status != NSS_STATUS_SUCCESS) {
226 report_nss_error("setgrent", status);
230 static void nss_endgrent(void)
232 NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
234 status = _nss_endgrent();
235 if (status != NSS_STATUS_SUCCESS) {
236 report_nss_error("endgrent", status);
240 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
242 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
243 long int *, gid_t **, long int , int *) =
244 find_fn("initgroups_dyn");
247 if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
249 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
250 if (status != NSS_STATUS_SUCCESS) {
251 report_nss_error("initgroups", status);
256 static void print_passwd(struct passwd *pwd)
258 printf("%s:%s:%d:%d:%s:%s:%s\n",
268 static void print_group(struct group *grp)
276 if (!grp->gr_mem[0]) {
281 for (i=0; grp->gr_mem[i+1]; i++) {
282 printf("%s, ", grp->gr_mem[i]);
284 printf("%s\n", grp->gr_mem[i]);
287 static void nss_test_initgroups(char *name, gid_t gid)
291 gid_t *groups = NULL;
295 groups = (gid_t *)malloc(size * sizeof(gid_t));
298 status = nss_initgroups(name, gid, &groups, &start, &size);
299 if (status == NSS_STATUS_UNAVAIL) {
300 printf("No initgroups fn\n");
304 for (i=0; i<start-1; i++) {
305 printf("%d, ", groups[i]);
307 printf("%d\n", groups[i]);
311 static void nss_test_users(void)
316 /* loop over all users */
317 while ((pwd = nss_getpwent())) {
318 printf("Testing user %s\n", pwd->pw_name);
319 printf("getpwent: "); print_passwd(pwd);
320 pwd = nss_getpwuid(pwd->pw_uid);
323 printf("ERROR: can't getpwuid\n");
326 printf("getpwuid: "); print_passwd(pwd);
327 pwd = nss_getpwnam(pwd->pw_name);
330 printf("ERROR: can't getpwnam\n");
333 printf("getpwnam: "); print_passwd(pwd);
334 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
340 static void nss_test_groups(void)
345 /* loop over all groups */
346 while ((grp = nss_getgrent())) {
347 printf("Testing group %s\n", grp->gr_name);
348 printf("getgrent: "); print_group(grp);
349 grp = nss_getgrnam(grp->gr_name);
352 printf("ERROR: can't getgrnam\n");
355 printf("getgrnam: "); print_group(grp);
356 grp = nss_getgrgid(grp->gr_gid);
359 printf("ERROR: can't getgrgid\n");
362 printf("getgrgid: "); print_group(grp);
368 static void nss_test_errors(void)
373 pwd = getpwnam("nosuchname");
374 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
376 printf("ERROR Non existant user gave error %d\n", last_error);
379 pwd = getpwuid(0xFFF0);
380 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
382 printf("ERROR Non existant uid gave error %d\n", last_error);
385 grp = getgrnam("nosuchgroup");
386 if (grp || last_error != NSS_STATUS_NOTFOUND) {
388 printf("ERROR Non existant group gave error %d\n", last_error);
391 grp = getgrgid(0xFFF0);
392 if (grp || last_error != NSS_STATUS_NOTFOUND) {
394 printf("ERROR Non existant gid gave error %d\n", last_error);
398 int main(int argc, char *argv[])
400 if (argc > 1) so_path = argv[1];
401 if (argc > 2) nss_name = argv[2];
407 printf("total_errors=%d\n", total_errors);