2 Unix SMB/CIFS implementation.
3 max initgroups nss tester
4 Copyright (C) Andrew Tridgell 2003
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.
28 #include <sys/types.h>
30 typedef enum nss_status NSS_STATUS;
32 static const char *so_path = "/lib/libnss_winbind.so";
33 static char *nss_name;
35 static NSS_STATUS last_error;
36 static int total_errors;
37 static int max_groups;
39 static void *find_fn(const char *name)
45 snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
48 h = dlopen(so_path, RTLD_LAZY);
51 printf("Can't open shared library %s : %s\n", so_path, dlerror());
56 printf("Can't find function %s : %s\n", s, dlerror());
62 static void report_nss_error(const char *who, NSS_STATUS status)
66 printf("ERROR %s: NSS_STATUS=%d %d (nss_errno=%d)\n",
67 who, status, NSS_STATUS_SUCCESS, nss_errno);
70 static struct passwd *nss_getpwent(void)
72 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
73 size_t , int *) = find_fn("getpwent_r");
74 static struct passwd pwd;
75 static char buf[1000];
78 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
79 if (status == NSS_STATUS_NOTFOUND) {
82 if (status != NSS_STATUS_SUCCESS) {
83 report_nss_error("getpwent", status);
89 static struct passwd *nss_getpwnam(const char *name)
91 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
92 size_t , int *) = find_fn("getpwnam_r");
93 static struct passwd pwd;
94 static char buf[1000];
97 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
98 if (status == NSS_STATUS_NOTFOUND) {
101 if (status != NSS_STATUS_SUCCESS) {
102 report_nss_error("getpwnam", status);
108 static struct passwd *nss_getpwuid(uid_t uid)
110 NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
111 size_t , int *) = find_fn("getpwuid_r");
112 static struct passwd pwd;
113 static char buf[1000];
116 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
117 if (status == NSS_STATUS_NOTFOUND) {
120 if (status != NSS_STATUS_SUCCESS) {
121 report_nss_error("getpwuid", status);
127 static void nss_setpwent(void)
129 NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
131 status = _nss_setpwent();
132 if (status != NSS_STATUS_SUCCESS) {
133 report_nss_error("setpwent", status);
137 static void nss_endpwent(void)
139 NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
141 status = _nss_endpwent();
142 if (status != NSS_STATUS_SUCCESS) {
143 report_nss_error("endpwent", status);
148 static struct group *nss_getgrent(void)
150 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
151 size_t , int *) = find_fn("getgrent_r");
152 static struct group grp;
154 static int buflen = 1024;
157 if (!buf) buf = malloc(buflen);
160 status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
161 if (status == NSS_STATUS_TRYAGAIN) {
163 buf = realloc(buf, buflen);
166 if (status == NSS_STATUS_NOTFOUND) {
169 if (status != NSS_STATUS_SUCCESS) {
170 report_nss_error("getgrent", status);
176 static struct group *nss_getgrnam(const char *name)
178 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
179 size_t , int *) = find_fn("getgrnam_r");
180 static struct group grp;
182 static int buflen = 1000;
185 if (!buf) buf = malloc(buflen);
187 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
188 if (status == NSS_STATUS_TRYAGAIN) {
190 buf = realloc(buf, buflen);
193 if (status == NSS_STATUS_NOTFOUND) {
196 if (status != NSS_STATUS_SUCCESS) {
197 report_nss_error("getgrnam", status);
203 static struct group *nss_getgrgid(gid_t gid)
205 NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
206 size_t , int *) = find_fn("getgrgid_r");
207 static struct group grp;
209 static int buflen = 1000;
212 if (!buf) buf = malloc(buflen);
214 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
215 if (status == NSS_STATUS_TRYAGAIN) {
217 buf = realloc(buf, buflen);
220 if (status == NSS_STATUS_NOTFOUND) {
223 if (status != NSS_STATUS_SUCCESS) {
224 report_nss_error("getgrgid", status);
230 static void nss_setgrent(void)
232 NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
234 status = _nss_setgrent();
235 if (status != NSS_STATUS_SUCCESS) {
236 report_nss_error("setgrent", status);
240 static void nss_endgrent(void)
242 NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
244 status = _nss_endgrent();
245 if (status != NSS_STATUS_SUCCESS) {
246 report_nss_error("endgrent", status);
250 static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *start, long int *size)
252 NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
253 long int *, gid_t **, long int , int *) =
254 find_fn("initgroups_dyn");
257 if (!_nss_initgroups) return NSS_STATUS_UNAVAIL;
259 status = _nss_initgroups(user, group, start, size, groups, 0, &nss_errno);
260 if (status != NSS_STATUS_SUCCESS) {
261 report_nss_error("initgroups", status);
266 static void print_passwd(struct passwd *pwd)
268 printf("%s:%s:%d:%d:%s:%s:%s\n",
278 static void nss_test_initgroups(char *name, gid_t gid)
280 long int size = 10240;
282 gid_t *groups = NULL;
286 groups = (gid_t *)malloc(size * sizeof(gid_t));
289 status = nss_initgroups(name, gid, &groups, &start, &size);
290 if (status == NSS_STATUS_UNAVAIL) {
291 printf("No initgroups fn\n");
295 for (i=0; i<start-1; i++) {
296 printf("%d, ", groups[i]);
298 printf("%d\n", groups[i]);
300 if (start > max_groups) {
306 static void nss_test_users(void)
311 /* loop over all users */
312 while ((pwd = nss_getpwent())) {
313 printf("Testing user %s\n", pwd->pw_name);
314 printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
320 int main(int argc, char *argv[])
324 if (argc > 1) so_path = argv[1];
326 p = strrchr(so_path, '_');
328 printf("Badly formed name for .so - must be libnss_FOO.so\n");
331 nss_name = strdup(p+1);
332 p = strchr(nss_name, '.');
335 printf("so_path=%s nss_name=%s\n\n", so_path, nss_name);
339 printf("total_errors=%d\n", total_errors);
340 printf("maximum number of groups: %d\n", max_groups);