2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2001-2004
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
32 typedef enum nss_status NSS_STATUS;
34 static const char *so_path = "/lib/libnss_winbind.so";
35 static char *nss_name;
37 static NSS_STATUS last_error;
38 static int total_errors;
40 static void *find_fn(const char *name)
46 snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
49 h = dlopen(so_path, RTLD_LAZY);
52 printf("Can't open shared library %s : %s\n", so_path, dlerror());
57 printf("Can't find function %s : %s\n", s, dlerror());
63 static void report_nss_error(const char *who, NSS_STATUS status)
67 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
68 who, status, NSS_STATUS_SUCCESS, nss_errno);
71 static struct passwd *nss_getpwent(void)
73 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
74 size_t , int *) = find_fn("getpwent_r");
75 static struct passwd pwd;
76 static char buf[1000];
79 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
80 if (status == NSS_STATUS_NOTFOUND) {
83 if (status != NSS_STATUS_SUCCESS) {
84 report_nss_error("getpwent", status);
90 static struct passwd *nss_getpwnam(const char *name)
92 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
93 size_t , int *) = find_fn("getpwnam_r");
94 static struct passwd pwd;
95 static char buf[1000];
98 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
99 if (status == NSS_STATUS_NOTFOUND) {
102 if (status != NSS_STATUS_SUCCESS) {
103 report_nss_error("getpwnam", status);
109 static struct passwd *nss_getpwuid(uid_t uid)
111 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
112 size_t , int *) = find_fn("getpwuid_r");
113 static struct passwd pwd;
114 static char buf[1000];
117 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
118 if (status == NSS_STATUS_NOTFOUND) {
121 if (status != NSS_STATUS_SUCCESS) {
122 report_nss_error("getpwuid", status);
128 static void nss_setpwent(void)
130 NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
132 status = _nss_setpwent();
133 if (status != NSS_STATUS_SUCCESS) {
134 report_nss_error("setpwent", status);
138 static void nss_endpwent(void)
140 NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
142 status = _nss_endpwent();
143 if (status != NSS_STATUS_SUCCESS) {
144 report_nss_error("endpwent", status);
149 static struct group *nss_getgrent(void)
151 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
152 size_t , int *) = find_fn("getgrent_r");
153 static struct group grp;
155 static int buflen = 1024;
158 if (!buf) buf = malloc(buflen);
161 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
162 if (status == NSS_STATUS_TRYAGAIN) {
164 buf = realloc(buf, buflen);
167 if (status == NSS_STATUS_NOTFOUND) {
170 if (status != NSS_STATUS_SUCCESS) {
171 report_nss_error("getgrent", status);
177 static struct group *nss_getgrnam(const char *name)
179 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
180 size_t , int *) = find_fn("getgrnam_r");
181 static struct group grp;
183 static int buflen = 1000;
186 if (!buf) buf = malloc(buflen);
188 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
189 if (status == NSS_STATUS_TRYAGAIN) {
191 buf = realloc(buf, buflen);
194 if (status == NSS_STATUS_NOTFOUND) {
197 if (status != NSS_STATUS_SUCCESS) {
198 report_nss_error("getgrnam", status);
204 static struct group *nss_getgrgid(gid_t gid)
206 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
207 size_t , int *) = find_fn("getgrgid_r");
208 static struct group grp;
210 static int buflen = 1000;
213 if (!buf) buf = malloc(buflen);
215 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
216 if (status == NSS_STATUS_TRYAGAIN) {
218 buf = realloc(buf, buflen);
221 if (status == NSS_STATUS_NOTFOUND) {
224 if (status != NSS_STATUS_SUCCESS) {
225 report_nss_error("getgrgid", status);
231 static void nss_setgrent(void)
233 NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
235 status = _nss_setgrent();
236 if (status != NSS_STATUS_SUCCESS) {
237 report_nss_error("setgrent", status);
241 static void nss_endgrent(void)
243 NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
245 status = _nss_endgrent();
246 if (status != NSS_STATUS_SUCCESS) {
247 report_nss_error("endgrent", status);
251 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
253 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
254 long int *, gid_t **, long int , int *) =
255 find_fn("initgroups_dyn");
258 if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
260 status = _nss_initgroups(user, group, start, size, groups, 2, &nss_errno);
261 printf("nss_errno=%d *size=%d\n", nss_errno, *size);
262 if (status != NSS_STATUS_SUCCESS) {
263 report_nss_error("initgroups", status);
268 static void print_passwd(struct passwd *pwd)
270 printf("%s:%s:%d:%d:%s:%s:%s\n",
280 static void print_group(struct group *grp)
288 if (!grp->gr_mem[0]) {
293 for (i=0; grp->gr_mem[i+1]; i++) {
294 printf("%s, ", grp->gr_mem[i]);
296 printf("%s\n", grp->gr_mem[i]);
299 static void nss_test_initgroups(char *name, gid_t gid)
303 gid_t *groups = NULL;
307 groups = (gid_t *)malloc(size * sizeof(gid_t));
310 status = nss_initgroups(name, gid, &groups, &start, &size);
311 if (status == NSS_STATUS_UNAVAIL) {
312 printf("No initgroups fn\n");
316 for (i=0; i<start-1; i++) {
317 printf("%d, ", groups[i]);
319 printf("%d\n", groups[i]);
323 static void nss_test_users(void)
328 /* loop over all users */
329 while ((pwd = nss_getpwent())) {
330 printf("Testing user %s\n", pwd->pw_name);
331 printf("getpwent: "); print_passwd(pwd);
332 pwd = nss_getpwuid(pwd->pw_uid);
335 printf("ERROR: can't getpwuid\n");
338 printf("getpwuid: "); print_passwd(pwd);
339 pwd = nss_getpwnam(pwd->pw_name);
342 printf("ERROR: can't getpwnam\n");
345 printf("getpwnam: "); print_passwd(pwd);
346 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
352 static void nss_test_groups(void)
357 /* loop over all groups */
358 while ((grp = nss_getgrent())) {
359 printf("Testing group %s\n", grp->gr_name);
360 printf("getgrent: "); print_group(grp);
361 grp = nss_getgrnam(grp->gr_name);
364 printf("ERROR: can't getgrnam\n");
367 printf("getgrnam: "); print_group(grp);
368 grp = nss_getgrgid(grp->gr_gid);
371 printf("ERROR: can't getgrgid\n");
374 printf("getgrgid: "); print_group(grp);
380 static void nss_test_errors(void)
385 pwd = getpwnam("nosuchname");
386 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
388 printf("ERROR Non existant user gave error %d\n", last_error);
391 pwd = getpwuid(0xFFF0);
392 if (pwd || last_error != NSS_STATUS_NOTFOUND) {
394 printf("ERROR Non existant uid gave error %d\n", last_error);
397 grp = getgrnam("nosuchgroup");
398 if (grp || last_error != NSS_STATUS_NOTFOUND) {
400 printf("ERROR Non existant group gave error %d\n", last_error);
403 grp = getgrgid(0xFFF0);
404 if (grp || last_error != NSS_STATUS_NOTFOUND) {
406 printf("ERROR Non existant gid gave error %d\n", last_error);
410 int main(int argc, char *argv[])
414 if (argc > 1) so_path = argv[1];
416 p = strrchr(so_path, '_');
418 printf("Badly formed name for .so - must be libnss_FOO.so\n");
421 nss_name = strdup(p+1);
422 p = strchr(nss_name, '.');
425 printf("so_path=%s nss_name=%s\n\n", so_path, nss_name);
431 printf("total_errors=%d\n", total_errors);