2 Unix SMB/Netbios implementation.
4 nss tester for winbindd
5 Copyright (C) Andrew Tridgell 2001
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.
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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static char *so_path
= "/lib/libnss_winbind.so";
27 static void *find_fn(const char *name
)
32 h
= dlopen(so_path
, RTLD_LAZY
);
35 printf("Can't open shared library %s\n", so_path
);
40 printf("Can't find function %s\n", name
);
46 static void report_nss_error(NSS_STATUS status
)
48 if (status
>= NSS_STATUS_SUCCESS
) return;
49 printf("NSS_STATUS=%d %d\n", status
, NSS_STATUS_SUCCESS
);
52 static struct passwd
*nss_getpwent(void)
54 NSS_STATUS (*_nss_getpwent_r
)(struct passwd
*, char *,
55 size_t , int *) = find_fn("_nss_winbind_getpwent_r");
56 static struct passwd pwd
;
57 static char buf
[1000];
60 status
= _nss_getpwent_r(&pwd
, buf
, sizeof(buf
), &nss_errno
);
61 if (status
== NSS_STATUS_NOTFOUND
) {
64 if (status
== NSS_STATUS_RETURN
) {
65 report_nss_error(status
);
71 static struct passwd
*nss_getpwnam(const char *name
)
73 NSS_STATUS (*_nss_getpwnam_r
)(const char *, struct passwd
*, char *,
74 size_t , int *) = find_fn("_nss_winbind_getpwnam_r");
75 static struct passwd pwd
;
76 static char buf
[1000];
79 status
= _nss_getpwnam_r(name
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
80 if (status
== NSS_STATUS_NOTFOUND
) {
83 if (status
== NSS_STATUS_RETURN
) {
84 report_nss_error(status
);
90 static struct passwd
*nss_getpwuid(uid_t uid
)
92 NSS_STATUS (*_nss_getpwuid_r
)(uid_t
, struct passwd
*, char *,
93 size_t , int *) = find_fn("_nss_winbind_getpwuid_r");
94 static struct passwd pwd
;
95 static char buf
[1000];
98 status
= _nss_getpwuid_r(uid
, &pwd
, buf
, sizeof(buf
), &nss_errno
);
99 if (status
== NSS_STATUS_NOTFOUND
) {
102 if (status
== NSS_STATUS_RETURN
) {
103 report_nss_error(status
);
109 static void nss_setpwent(void)
111 NSS_STATUS (*_nss_setpwent
)(void) = find_fn("_nss_winbind_setpwent");
113 report_nss_error(_nss_setpwent());
116 static void nss_endpwent(void)
118 NSS_STATUS (*_nss_endpwent
)(void) = find_fn("_nss_winbind_endpwent");
120 report_nss_error(_nss_endpwent());
124 static struct group
*nss_getgrent(void)
126 NSS_STATUS (*_nss_getgrent_r
)(struct group
*, char *,
127 size_t , int *) = find_fn("_nss_winbind_getgrent_r");
128 static struct group grp
;
129 static char buf
[1000];
132 status
= _nss_getgrent_r(&grp
, buf
, sizeof(buf
), &nss_errno
);
133 if (status
== NSS_STATUS_NOTFOUND
) {
136 if (status
== NSS_STATUS_RETURN
) {
137 report_nss_error(status
);
143 static struct group
*nss_getgrnam(const char *name
)
145 NSS_STATUS (*_nss_getgrnam_r
)(const char *, struct group
*, char *,
146 size_t , int *) = find_fn("_nss_winbind_getgrnam_r");
147 static struct group grp
;
148 static char buf
[1000];
151 status
= _nss_getgrnam_r(name
, &grp
, buf
, sizeof(buf
), &nss_errno
);
152 if (status
== NSS_STATUS_NOTFOUND
) {
155 if (status
== NSS_STATUS_RETURN
) {
156 report_nss_error(status
);
162 static struct group
*nss_getgrgid(gid_t gid
)
164 NSS_STATUS (*_nss_getgrgid_r
)(gid_t
, struct group
*, char *,
165 size_t , int *) = find_fn("_nss_winbind_getgrgid_r");
166 static struct group grp
;
167 static char buf
[1000];
170 status
= _nss_getgrgid_r(gid
, &grp
, buf
, sizeof(buf
), &nss_errno
);
171 if (status
== NSS_STATUS_NOTFOUND
) {
174 if (status
== NSS_STATUS_RETURN
) {
175 report_nss_error(status
);
181 static void nss_setgrent(void)
183 NSS_STATUS (*_nss_setgrent
)(void) = find_fn("_nss_winbind_setgrent");
185 report_nss_error(_nss_setgrent());
188 static void nss_endgrent(void)
190 NSS_STATUS (*_nss_endgrent
)(void) = find_fn("_nss_winbind_endgrent");
192 report_nss_error(_nss_endgrent());
195 static int nss_initgroups(char *user
, gid_t group
, gid_t
**groups
, long int *start
, long int *size
)
197 NSS_STATUS (*_nss_initgroups
)(char *, gid_t
, long int *,
198 long int *, gid_t
**, long int , int *) =
199 find_fn("_nss_winbind_initgroups_dyn");
202 status
= _nss_initgroups(user
, group
, start
, size
, groups
, 0, &nss_errno
);
203 report_nss_error(status
);
207 static void print_passwd(struct passwd
*pwd
)
209 printf("%s:%s:%d:%d:%s:%s:%s\n",
219 static void print_group(struct group
*grp
)
227 if (!grp
->gr_mem
[0]) {
232 for (i
=0; grp
->gr_mem
[i
+1]; i
++) {
233 printf("%s, ", grp
->gr_mem
[i
]);
235 printf("%s\n", grp
->gr_mem
[i
]);
238 static void nss_test_initgroups(char *name
, gid_t gid
)
242 gid_t
*groups
= NULL
;
245 groups
= (gid_t
*)malloc(size
* sizeof(gid_t
));
248 nss_initgroups(name
, gid
, &groups
, &start
, &size
);
249 for (i
=0; i
<start
-1; i
++) {
250 printf("%d, ", groups
[i
]);
252 printf("%d\n", groups
[i
]);
256 static void nss_test_users(void)
261 /* loop over all users */
262 while ((pwd
= nss_getpwent())) {
263 printf("Testing user %s\n", pwd
->pw_name
);
264 printf("getpwent: "); print_passwd(pwd
);
265 pwd
= nss_getpwnam(pwd
->pw_name
);
266 printf("getpwnam: "); print_passwd(pwd
);
267 pwd
= nss_getpwuid(pwd
->pw_uid
);
268 printf("getpwuid: "); print_passwd(pwd
);
269 printf("initgroups: "); nss_test_initgroups(pwd
->pw_name
, pwd
->pw_gid
);
275 static void nss_test_groups(void)
280 /* loop over all groups */
281 while ((grp
= nss_getgrent())) {
282 printf("Testing group %s\n", grp
->gr_name
);
283 printf("getgrent: "); print_group(grp
);
284 grp
= nss_getgrnam(grp
->gr_name
);
285 printf("getgrnam: "); print_group(grp
);
286 grp
= nss_getgrgid(grp
->gr_gid
);
287 printf("getgrgid: "); print_group(grp
);
294 int main(int argc
, char *argv
[])
296 if (argc
> 1) so_path
= argv
[1];