add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / utils / nsstest.c
blob76108876dffac38a9e90a4736713c5ef47cb0447
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
22 #include "includes.h"
24 static char *so_path = "/lib/libnss_winbind.so";
25 static int nss_errno;
27 static void *find_fn(const char *name)
29 static void *h;
30 void *res;
31 if (!h) {
32 h = dlopen(so_path, RTLD_LAZY);
34 if (!h) {
35 printf("Can't open shared library %s\n", so_path);
36 exit(1);
38 res = dlsym(h, name);
39 if (!res) {
40 printf("Can't find function %s\n", name);
41 exit(1);
43 return res;
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];
58 NSS_STATUS status;
60 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
61 if (status == NSS_STATUS_NOTFOUND) {
62 return NULL;
64 if (status == NSS_STATUS_RETURN) {
65 report_nss_error(status);
66 return NULL;
68 return &pwd;
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];
77 NSS_STATUS status;
79 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
80 if (status == NSS_STATUS_NOTFOUND) {
81 return NULL;
83 if (status == NSS_STATUS_RETURN) {
84 report_nss_error(status);
85 return NULL;
87 return &pwd;
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];
96 NSS_STATUS status;
98 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
99 if (status == NSS_STATUS_NOTFOUND) {
100 return NULL;
102 if (status == NSS_STATUS_RETURN) {
103 report_nss_error(status);
104 return NULL;
106 return &pwd;
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];
130 NSS_STATUS status;
132 status = _nss_getgrent_r(&grp, buf, sizeof(buf), &nss_errno);
133 if (status == NSS_STATUS_NOTFOUND) {
134 return NULL;
136 if (status == NSS_STATUS_RETURN) {
137 report_nss_error(status);
138 return NULL;
140 return &grp;
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];
149 NSS_STATUS status;
151 status = _nss_getgrnam_r(name, &grp, buf, sizeof(buf), &nss_errno);
152 if (status == NSS_STATUS_NOTFOUND) {
153 return NULL;
155 if (status == NSS_STATUS_RETURN) {
156 report_nss_error(status);
157 return NULL;
159 return &grp;
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];
168 NSS_STATUS status;
170 status = _nss_getgrgid_r(gid, &grp, buf, sizeof(buf), &nss_errno);
171 if (status == NSS_STATUS_NOTFOUND) {
172 return NULL;
174 if (status == NSS_STATUS_RETURN) {
175 report_nss_error(status);
176 return NULL;
178 return &grp;
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");
200 NSS_STATUS status;
202 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
203 report_nss_error(status);
204 return status;
207 static void print_passwd(struct passwd *pwd)
209 printf("%s:%s:%d:%d:%s:%s:%s\n",
210 pwd->pw_name,
211 pwd->pw_passwd,
212 pwd->pw_uid,
213 pwd->pw_gid,
214 pwd->pw_gecos,
215 pwd->pw_dir,
216 pwd->pw_shell);
219 static void print_group(struct group *grp)
221 int i;
222 printf("%s:%s:%d: ",
223 grp->gr_name,
224 grp->gr_passwd,
225 grp->gr_gid);
227 if (!grp->gr_mem[0]) {
228 printf("\n");
229 return;
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)
240 long int size = 16;
241 long int start = 1;
242 gid_t *groups = NULL;
243 int i;
245 groups = (gid_t *)malloc(size * sizeof(gid_t));
246 groups[0] = gid;
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)
258 struct passwd *pwd;
260 nss_setpwent();
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);
270 printf("\n");
272 nss_endpwent();
275 static void nss_test_groups(void)
277 struct group *grp;
279 nss_setgrent();
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);
288 printf("\n");
290 nss_endgrent();
294 int main(int argc, char *argv[])
296 if (argc > 1) so_path = argv[1];
298 nss_test_users();
299 nss_test_groups();
301 return 0;