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";
25 static char *nss_name = "winbind";
28 static void *find_fn(const char *name)
34 snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
37 h = dlopen(so_path, RTLD_LAZY);
40 printf("Can't open shared library %s\n", so_path);
45 printf("Can't find function %s\n", s);
51 static void report_nss_error(const char *who, NSS_STATUS status)
53 printf("ERROR %s: NSS_STATUS=%d %d\n", who, status, NSS_STATUS_SUCCESS);
56 static struct passwd *nss_getpwent(void)
58 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
59 size_t , int *) = find_fn("getpwent_r");
60 static struct passwd pwd;
61 static char buf[1000];
64 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
65 if (status == NSS_STATUS_NOTFOUND) {
68 if (status != NSS_STATUS_SUCCESS) {
69 report_nss_error("getpwent", status);
75 static struct passwd *nss_getpwnam(const char *name)
77 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
78 size_t , int *) = find_fn("getpwnam_r");
79 static struct passwd pwd;
80 static char buf[1000];
83 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
84 if (status == NSS_STATUS_NOTFOUND) {
87 if (status != NSS_STATUS_SUCCESS) {
88 report_nss_error("getpwnam", status);
94 static struct passwd *nss_getpwuid(uid_t uid)
96 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
97 size_t , int *) = find_fn("getpwuid_r");
98 static struct passwd pwd;
99 static char buf[1000];
102 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
103 if (status == NSS_STATUS_NOTFOUND) {
106 if (status != NSS_STATUS_SUCCESS) {
107 report_nss_error("getpwuid", status);
113 static void nss_setpwent(void)
115 NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
117 status = _nss_setpwent();
118 if (status != NSS_STATUS_SUCCESS) {
119 report_nss_error("setpwent", status);
123 static void nss_endpwent(void)
125 NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
127 status = _nss_endpwent();
128 if (status != NSS_STATUS_SUCCESS) {
129 report_nss_error("endpwent", status);
134 static struct group *nss_getgrent(void)
136 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
137 size_t , int *) = find_fn("getgrent_r");
138 static struct group grp;
139 static char buf[1000];
142 status = _nss_getgrent_r(&grp, buf, sizeof(buf), &nss_errno);
143 if (status == NSS_STATUS_NOTFOUND) {
146 if (status != NSS_STATUS_SUCCESS) {
147 report_nss_error("getgrent", status);
153 static struct group *nss_getgrnam(const char *name)
155 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
156 size_t , int *) = find_fn("getgrnam_r");
157 static struct group grp;
158 static char buf[1000];
161 status = _nss_getgrnam_r(name, &grp, buf, sizeof(buf), &nss_errno);
162 if (status == NSS_STATUS_NOTFOUND) {
165 if (status != NSS_STATUS_SUCCESS) {
166 report_nss_error("getgrnam", status);
172 static struct group *nss_getgrgid(gid_t gid)
174 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
175 size_t , int *) = find_fn("getgrgid_r");
176 static struct group grp;
177 static char buf[1000];
180 status = _nss_getgrgid_r(gid, &grp, buf, sizeof(buf), &nss_errno);
181 if (status == NSS_STATUS_NOTFOUND) {
184 if (status != NSS_STATUS_SUCCESS) {
185 report_nss_error("getgrgid", status);
191 static void nss_setgrent(void)
193 NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
195 status = _nss_setgrent();
196 if (status != NSS_STATUS_SUCCESS) {
197 report_nss_error("setgrent", status);
201 static void nss_endgrent(void)
203 NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
205 status = _nss_endgrent();
206 if (status != NSS_STATUS_SUCCESS) {
207 report_nss_error("endgrent", status);
211 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
213 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
214 long int *, gid_t **, long int , int *) =
215 find_fn("initgroups_dyn");
218 if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
220 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
221 if (status != NSS_STATUS_SUCCESS) {
222 report_nss_error("initgroups", status);
227 static void print_passwd(struct passwd *pwd)
229 printf("%s:%s:%d:%d:%s:%s:%s\n",
239 static void print_group(struct group *grp)
247 if (!grp->gr_mem[0]) {
252 for (i=0; grp->gr_mem[i+1]; i++) {
253 printf("%s, ", grp->gr_mem[i]);
255 printf("%s\n", grp->gr_mem[i]);
258 static void nss_test_initgroups(char *name, gid_t gid)
262 gid_t *groups = NULL;
266 groups = (gid_t *)malloc(size * sizeof(gid_t));
269 status = nss_initgroups(name, gid, &groups, &start, &size);
270 if (status == NSS_STATUS_UNAVAIL) {
271 printf("No initgroups fn\n");
275 for (i=0; i<start-1; i++) {
276 printf("%d, ", groups[i]);
278 printf("%d\n", groups[i]);
282 static void nss_test_users(void)
287 /* loop over all users */
288 while ((pwd = nss_getpwent())) {
289 printf("Testing user %s\n", pwd->pw_name);
290 printf("getpwent: "); print_passwd(pwd);
291 pwd = nss_getpwuid(pwd->pw_uid);
292 printf("getpwuid: "); print_passwd(pwd);
293 pwd = nss_getpwnam(pwd->pw_name);
294 printf("getpwnam: "); print_passwd(pwd);
295 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
301 static void nss_test_groups(void)
306 /* loop over all groups */
307 while ((grp = nss_getgrent())) {
308 printf("Testing group %s\n", grp->gr_name);
309 printf("getgrent: "); print_group(grp);
310 grp = nss_getgrnam(grp->gr_name);
311 printf("getgrnam: "); print_group(grp);
312 grp = nss_getgrgid(grp->gr_gid);
313 printf("getgrgid: "); print_group(grp);
320 int main(int argc, char *argv[])
322 if (argc > 1) so_path = argv[1];
323 if (argc > 2) nss_name = argv[2];