2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
3 * Copyright (C) Guenther Deschner 2009 <gd@samba.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 /* defining this gives us the posix getpwnam_r() calls on solaris
38 Thanks to heimdal for this */
39 #define _POSIX_PTHREAD_SEMANTICS
41 #define NSS_WRAPPER_NOT_REPLACE
42 #include "../replace/replace.h"
43 #include "system/passwd.h"
44 #include "system/filesys.h"
45 #include "../nsswitch/nsstest.h"
47 #else /* _SAMBA_BUILD_ */
49 #error nss_wrapper_only_supported_in_samba_yet
57 /* not all systems have _r functions... */
58 #ifndef HAVE_GETPWNAM_R
59 #define getpwnam_r(name, pwdst, buf, buflen, pwdstp) ENOSYS
61 #ifndef HAVE_GETPWUID_R
62 #define getpwuid_r(uid, pwdst, buf, buflen, pwdstp) ENOSYS
64 #ifndef HAVE_GETPWENT_R
65 #define getpwent_r(pwdst, buf, buflen, pwdstp) ENOSYS
67 #ifndef HAVE_GETGRNAM_R
68 #define getgrnam_r(name, grdst, buf, buflen, grdstp) ENOSYS
70 #ifndef HAVE_GETGRGID_R
71 #define getgrgid_r(gid, grdst, buf, buflen, grdstp) ENOSYS
73 #ifndef HAVE_GETGRENT_R
74 #define getgrent_r(grdst, buf, buflen, grdstp) ENOSYS
77 /* not all systems have getgrouplist */
78 #ifndef HAVE_GETGROUPLIST
79 #define getgrouplist(user, group, groups, ngroups) 0
82 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
88 #define real_getpwnam getpwnam
89 #define real_getpwnam_r getpwnam_r
90 #define real_getpwuid getpwuid
91 #define real_getpwuid_r getpwuid_r
93 #define real_setpwent setpwent
94 #define real_getpwent getpwent
95 #define real_getpwent_r getpwent_r
96 #define real_endpwent endpwent
99 #define real_getgrlst getgrlst
100 #define real_getgrlst_r getgrlst_r
101 #define real_initgroups_dyn initgroups_dyn
103 #define real_initgroups initgroups
104 #define real_getgrouplist getgrouplist
106 #define real_getgrnam getgrnam
107 #define real_getgrnam_r getgrnam_r
108 #define real_getgrgid getgrgid
109 #define real_getgrgid_r getgrgid_r
111 #define real_setgrent setgrent
112 #define real_getgrent getgrent
113 #define real_getgrent_r getgrent_r
114 #define real_endgrent endgrent
120 # define NWRAP_ERROR(args) DEBUG(0, args)
122 # define NWRAP_ERROR(args) printf args
125 #define NWRAP_ERROR(args)
130 # define NWRAP_DEBUG(args) DEBUG(0, args)
132 # define NWRAP_DEBUG(args) printf args
135 #define NWRAP_DEBUG(args)
140 # define NWRAP_VERBOSE(args) DEBUG(0, args)
142 # define NWRAP_VERBOSE(args) printf args
145 #define NWRAP_VERBOSE(args)
148 struct nwrap_module_nss_fns {
149 NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
150 size_t buflen, int *errnop);
151 NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
152 size_t buflen, int *errnop);
153 NSS_STATUS (*_nss_setpwent)(void);
154 NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
155 size_t buflen, int *errnop);
156 NSS_STATUS (*_nss_endpwent)(void);
157 NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
158 long int *size, gid_t **groups, long int limit, int *errnop);
159 NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
160 size_t buflen, int *errnop);
161 NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
162 size_t buflen, int *errnop);
163 NSS_STATUS (*_nss_setgrent)(void);
164 NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
165 size_t buflen, int *errnop);
166 NSS_STATUS (*_nss_endgrent)(void);
169 struct nwrap_backend {
173 struct nwrap_ops *ops;
174 struct nwrap_module_nss_fns *fns;
178 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
180 int (*nw_getpwnam_r)(struct nwrap_backend *b,
181 const char *name, struct passwd *pwdst,
182 char *buf, size_t buflen, struct passwd **pwdstp);
183 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
185 int (*nw_getpwuid_r)(struct nwrap_backend *b,
186 uid_t uid, struct passwd *pwdst,
187 char *buf, size_t buflen, struct passwd **pwdstp);
188 void (*nw_setpwent)(struct nwrap_backend *b);
189 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
190 int (*nw_getpwent_r)(struct nwrap_backend *b,
191 struct passwd *pwdst, char *buf,
192 size_t buflen, struct passwd **pwdstp);
193 void (*nw_endpwent)(struct nwrap_backend *b);
194 int (*nw_initgroups)(struct nwrap_backend *b,
195 const char *user, gid_t group);
196 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
198 int (*nw_getgrnam_r)(struct nwrap_backend *b,
199 const char *name, struct group *grdst,
200 char *buf, size_t buflen, struct group **grdstp);
201 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
203 int (*nw_getgrgid_r)(struct nwrap_backend *b,
204 gid_t gid, struct group *grdst,
205 char *buf, size_t buflen, struct group **grdstp);
206 void (*nw_setgrent)(struct nwrap_backend *b);
207 struct group * (*nw_getgrent)(struct nwrap_backend *b);
208 int (*nw_getgrent_r)(struct nwrap_backend *b,
209 struct group *grdst, char *buf,
210 size_t buflen, struct group **grdstp);
211 void (*nw_endgrent)(struct nwrap_backend *b);
214 /* prototypes for files backend */
217 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
219 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
220 const char *name, struct passwd *pwdst,
221 char *buf, size_t buflen, struct passwd **pwdstp);
222 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
224 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
225 uid_t uid, struct passwd *pwdst,
226 char *buf, size_t buflen, struct passwd **pwdstp);
227 static void nwrap_files_setpwent(struct nwrap_backend *b);
228 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
229 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
230 struct passwd *pwdst, char *buf,
231 size_t buflen, struct passwd **pwdstp);
232 static void nwrap_files_endpwent(struct nwrap_backend *b);
233 static int nwrap_files_initgroups(struct nwrap_backend *b,
234 const char *user, gid_t group);
235 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
237 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
238 const char *name, struct group *grdst,
239 char *buf, size_t buflen, struct group **grdstp);
240 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
242 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
243 gid_t gid, struct group *grdst,
244 char *buf, size_t buflen, struct group **grdstp);
245 static void nwrap_files_setgrent(struct nwrap_backend *b);
246 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
247 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
248 struct group *grdst, char *buf,
249 size_t buflen, struct group **grdstp);
250 static void nwrap_files_endgrent(struct nwrap_backend *b);
252 /* prototypes for module backend */
254 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
255 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
256 struct passwd *pwdst, char *buf,
257 size_t buflen, struct passwd **pwdstp);
258 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
260 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
261 const char *name, struct passwd *pwdst,
262 char *buf, size_t buflen, struct passwd **pwdstp);
263 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
265 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
266 uid_t uid, struct passwd *pwdst,
267 char *buf, size_t buflen, struct passwd **pwdstp);
268 static void nwrap_module_setpwent(struct nwrap_backend *b);
269 static void nwrap_module_endpwent(struct nwrap_backend *b);
270 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
271 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
272 struct group *grdst, char *buf,
273 size_t buflen, struct group **grdstp);
274 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
276 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
277 const char *name, struct group *grdst,
278 char *buf, size_t buflen, struct group **grdstp);
279 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
281 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
282 gid_t gid, struct group *grdst,
283 char *buf, size_t buflen, struct group **grdstp);
284 static void nwrap_module_setgrent(struct nwrap_backend *b);
285 static void nwrap_module_endgrent(struct nwrap_backend *b);
286 static int nwrap_module_initgroups(struct nwrap_backend *b,
287 const char *user, gid_t group);
289 struct nwrap_ops nwrap_files_ops = {
290 .nw_getpwnam = nwrap_files_getpwnam,
291 .nw_getpwnam_r = nwrap_files_getpwnam_r,
292 .nw_getpwuid = nwrap_files_getpwuid,
293 .nw_getpwuid_r = nwrap_files_getpwuid_r,
294 .nw_setpwent = nwrap_files_setpwent,
295 .nw_getpwent = nwrap_files_getpwent,
296 .nw_getpwent_r = nwrap_files_getpwent_r,
297 .nw_endpwent = nwrap_files_endpwent,
298 .nw_initgroups = nwrap_files_initgroups,
299 .nw_getgrnam = nwrap_files_getgrnam,
300 .nw_getgrnam_r = nwrap_files_getgrnam_r,
301 .nw_getgrgid = nwrap_files_getgrgid,
302 .nw_getgrgid_r = nwrap_files_getgrgid_r,
303 .nw_setgrent = nwrap_files_setgrent,
304 .nw_getgrent = nwrap_files_getgrent,
305 .nw_getgrent_r = nwrap_files_getgrent_r,
306 .nw_endgrent = nwrap_files_endgrent,
309 struct nwrap_ops nwrap_module_ops = {
310 .nw_getpwnam = nwrap_module_getpwnam,
311 .nw_getpwnam_r = nwrap_module_getpwnam_r,
312 .nw_getpwuid = nwrap_module_getpwuid,
313 .nw_getpwuid_r = nwrap_module_getpwuid_r,
314 .nw_setpwent = nwrap_module_setpwent,
315 .nw_getpwent = nwrap_module_getpwent,
316 .nw_getpwent_r = nwrap_module_getpwent_r,
317 .nw_endpwent = nwrap_module_endpwent,
318 .nw_initgroups = nwrap_module_initgroups,
319 .nw_getgrnam = nwrap_module_getgrnam,
320 .nw_getgrnam_r = nwrap_module_getgrnam_r,
321 .nw_getgrgid = nwrap_module_getgrgid,
322 .nw_getgrgid_r = nwrap_module_getgrgid_r,
323 .nw_setgrent = nwrap_module_setgrent,
324 .nw_getgrent = nwrap_module_getgrent,
325 .nw_getgrent_r = nwrap_module_getgrent_r,
326 .nw_endgrent = nwrap_module_endgrent,
330 const char *nwrap_switch;
332 struct nwrap_backend *backends;
335 struct nwrap_main *nwrap_main_global;
336 struct nwrap_main __nwrap_main_global;
344 bool (*parse_line)(struct nwrap_cache *, char *line);
345 void (*unload)(struct nwrap_cache *);
349 struct nwrap_cache *cache;
356 struct nwrap_cache __nwrap_cache_pw;
357 struct nwrap_pw nwrap_pw_global;
359 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
360 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
363 struct nwrap_cache *cache;
370 struct nwrap_cache __nwrap_cache_gr;
371 struct nwrap_gr nwrap_gr_global;
373 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
374 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
376 static void *nwrap_load_module_fn(struct nwrap_backend *b,
383 NWRAP_ERROR(("%s: no handle\n",
388 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
389 NWRAP_ERROR(("%s: out of memory\n",
394 res = dlsym(b->so_handle, s);
396 NWRAP_ERROR(("%s: cannot find function %s in %s\n",
397 __location__, s, b->so_path));
404 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
406 struct nwrap_module_nss_fns *fns;
412 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
417 fns->_nss_getpwnam_r = (NSS_STATUS (*)(const char *, struct passwd *, char *, size_t, int *))
418 nwrap_load_module_fn(b, "getpwnam_r");
419 fns->_nss_getpwuid_r = (NSS_STATUS (*)(uid_t, struct passwd *, char *, size_t, int *))
420 nwrap_load_module_fn(b, "getpwuid_r");
421 fns->_nss_setpwent = (NSS_STATUS(*)(void))
422 nwrap_load_module_fn(b, "setpwent");
423 fns->_nss_getpwent_r = (NSS_STATUS (*)(struct passwd *, char *, size_t, int *))
424 nwrap_load_module_fn(b, "getpwent_r");
425 fns->_nss_endpwent = (NSS_STATUS(*)(void))
426 nwrap_load_module_fn(b, "endpwent");
427 fns->_nss_initgroups = (NSS_STATUS (*)(const char *, gid_t, long int *, long int *, gid_t **, long int, int *))
428 nwrap_load_module_fn(b, "initgroups_dyn");
429 fns->_nss_getgrnam_r = (NSS_STATUS (*)(const char *, struct group *, char *, size_t, int *))
430 nwrap_load_module_fn(b, "getgrnam_r");
431 fns->_nss_getgrgid_r = (NSS_STATUS (*)(gid_t, struct group *, char *, size_t, int *))
432 nwrap_load_module_fn(b, "getgrgid_r");
433 fns->_nss_setgrent = (NSS_STATUS(*)(void))
434 nwrap_load_module_fn(b, "setgrent");
435 fns->_nss_getgrent_r = (NSS_STATUS (*)(struct group *, char *, size_t, int *))
436 nwrap_load_module_fn(b, "getgrent_r");
437 fns->_nss_endgrent = (NSS_STATUS(*)(void))
438 nwrap_load_module_fn(b, "endgrent");
443 static void *nwrap_load_module(const char *so_path)
447 if (!so_path || !strlen(so_path)) {
451 h = dlopen(so_path, RTLD_LAZY);
453 NWRAP_ERROR(("%s: cannot open shared library %s\n",
454 __location__, so_path));
461 static bool nwrap_module_init(const char *name,
462 struct nwrap_ops *ops,
465 struct nwrap_backend **backends)
467 *backends = (struct nwrap_backend *)realloc(*backends,
468 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
470 NWRAP_ERROR(("%s: out of memory\n",
475 (*backends)[*num_backends].name = name;
476 (*backends)[*num_backends].ops = ops;
477 (*backends)[*num_backends].so_path = so_path;
478 (*backends)[*num_backends].so_handle = nwrap_load_module(so_path);
479 (*backends)[*num_backends].fns = nwrap_load_module_fns(&((*backends)[*num_backends]));
486 static void nwrap_backend_init(struct nwrap_main *r)
488 const char *winbind_so_path = getenv("NSS_WRAPPER_WINBIND_SO_PATH");
493 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
496 NWRAP_ERROR(("%s: failed to initialize 'files' backend\n",
501 if (winbind_so_path && strlen(winbind_so_path)) {
502 if (!nwrap_module_init("winbind", &nwrap_module_ops, winbind_so_path,
505 NWRAP_ERROR(("%s: failed to initialize 'winbind' backend\n",
512 static void nwrap_init(void)
514 static bool initialized;
516 if (initialized) return;
519 nwrap_main_global = &__nwrap_main_global;
521 nwrap_backend_init(nwrap_main_global);
523 nwrap_pw_global.cache = &__nwrap_cache_pw;
525 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
526 nwrap_pw_global.cache->fd = -1;
527 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
528 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
529 nwrap_pw_global.cache->unload = nwrap_pw_unload;
531 nwrap_gr_global.cache = &__nwrap_cache_gr;
533 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
534 nwrap_gr_global.cache->fd = -1;
535 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
536 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
537 nwrap_gr_global.cache->unload = nwrap_gr_unload;
540 static bool nwrap_enabled(void)
544 if (!nwrap_pw_global.cache->path) {
547 if (nwrap_pw_global.cache->path[0] == '\0') {
550 if (!nwrap_gr_global.cache->path) {
553 if (nwrap_gr_global.cache->path[0] == '\0') {
560 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
566 if (nwrap->st.st_size == 0) {
567 NWRAP_DEBUG(("%s: size == 0\n",
572 if (nwrap->st.st_size > INT32_MAX) {
573 NWRAP_ERROR(("%s: size[%u] larger than INT32_MAX\n",
574 __location__, (unsigned)nwrap->st.st_size));
578 ret = lseek(nwrap->fd, 0, SEEK_SET);
580 NWRAP_ERROR(("%s: lseek - %d\n",__location__,ret));
584 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
586 NWRAP_ERROR(("%s: malloc failed\n",__location__));
590 ret = read(nwrap->fd, buf, nwrap->st.st_size);
591 if (ret != nwrap->st.st_size) {
592 NWRAP_ERROR(("%s: read(%u) gave %d\n",
593 __location__, (unsigned)nwrap->st.st_size, ret));
597 buf[nwrap->st.st_size] = '\0';
600 while (nline && nline[0]) {
608 e = strchr(line, '\n');
619 NWRAP_VERBOSE(("%s:'%s'\n",__location__, line));
621 if (strlen(line) == 0) {
625 ok = nwrap->parse_line(nwrap, line);
640 static void nwrap_files_cache_unload(struct nwrap_cache *nwrap)
642 nwrap->unload(nwrap);
644 if (nwrap->buf) free(nwrap->buf);
649 static void nwrap_files_cache_reload(struct nwrap_cache *nwrap)
654 bool retried = false;
658 nwrap->fd = open(nwrap->path, O_RDONLY);
660 NWRAP_ERROR(("%s: unable to open '%s' readonly %d:%s\n",
662 nwrap->path, nwrap->fd,
666 NWRAP_VERBOSE(("%s: open '%s'\n", __location__, nwrap->path));
669 ret = fstat(nwrap->fd, &st);
671 NWRAP_ERROR(("%s: fstat(%s) - %d:%s\n",
674 ret, strerror(errno)));
678 if (retried == false && st.st_nlink == 0) {
679 /* maybe someone has replaced the file... */
680 NWRAP_DEBUG(("%s: st_nlink == 0, reopen %s\n",
681 __location__, nwrap->path));
683 memset(&nwrap->st, 0, sizeof(nwrap->st));
689 if (st.st_mtime == nwrap->st.st_mtime) {
690 NWRAP_VERBOSE(("%s: st_mtime[%u] hasn't changed, skip reload\n",
691 __location__, (unsigned)st.st_mtime));
694 NWRAP_DEBUG(("%s: st_mtime has changed [%u] => [%u], start reload\n",
695 __location__, (unsigned)st.st_mtime,
696 (unsigned)nwrap->st.st_mtime));
700 nwrap_files_cache_unload(nwrap);
702 ok = nwrap_parse_file(nwrap);
704 NWRAP_ERROR(("%s: failed to reload %s\n",
705 __location__, nwrap->path));
706 nwrap_files_cache_unload(nwrap);
708 NWRAP_DEBUG(("%s: reloaded %s\n",
709 __location__, nwrap->path));
713 * the caller has to call nwrap_unload() on failure
715 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
717 struct nwrap_pw *nwrap_pw;
724 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
726 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
727 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
729 NWRAP_ERROR(("%s:realloc(%u) failed\n",
730 __location__, list_size));
735 pw = &nwrap_pw->list[nwrap_pw->num];
742 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
743 __location__, line, c));
751 NWRAP_VERBOSE(("name[%s]\n", pw->pw_name));
756 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
757 __location__, line, c));
765 NWRAP_VERBOSE(("password[%s]\n", pw->pw_passwd));
770 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
771 __location__, line, c));
777 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
779 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
780 __location__, line, c, strerror(errno)));
784 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
785 __location__, line, c, strerror(errno)));
789 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
790 __location__, line, c, strerror(errno)));
795 NWRAP_VERBOSE(("uid[%u]\n", pw->pw_uid));
800 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
801 __location__, line, c));
807 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
809 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
810 __location__, line, c, strerror(errno)));
814 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
815 __location__, line, c, strerror(errno)));
819 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
820 __location__, line, c, strerror(errno)));
825 NWRAP_VERBOSE(("gid[%u]\n", pw->pw_gid));
830 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
831 __location__, line, c));
839 NWRAP_VERBOSE(("gecos[%s]\n", pw->pw_gecos));
844 NWRAP_ERROR(("%s:'%s'\n",__location__,c));
852 NWRAP_VERBOSE(("dir[%s]\n", pw->pw_dir));
856 NWRAP_VERBOSE(("shell[%s]\n", pw->pw_shell));
858 NWRAP_DEBUG(("add user[%s:%s:%u:%u:%s:%s:%s]\n",
859 pw->pw_name, pw->pw_passwd,
860 pw->pw_uid, pw->pw_gid,
861 pw->pw_gecos, pw->pw_dir, pw->pw_shell));
867 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
869 struct nwrap_pw *nwrap_pw;
870 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
872 if (nwrap_pw->list) free(nwrap_pw->list);
874 nwrap_pw->list = NULL;
879 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
880 char *buf, size_t buflen, struct passwd **dstp)
886 first = src->pw_name;
888 last = src->pw_shell;
889 while (*last) last++;
891 ofs = PTR_DIFF(last + 1, first);
897 memcpy(buf, first, ofs);
899 ofs = PTR_DIFF(src->pw_name, first);
900 dst->pw_name = buf + ofs;
901 ofs = PTR_DIFF(src->pw_passwd, first);
902 dst->pw_passwd = buf + ofs;
903 dst->pw_uid = src->pw_uid;
904 dst->pw_gid = src->pw_gid;
905 ofs = PTR_DIFF(src->pw_gecos, first);
906 dst->pw_gecos = buf + ofs;
907 ofs = PTR_DIFF(src->pw_dir, first);
908 dst->pw_dir = buf + ofs;
909 ofs = PTR_DIFF(src->pw_shell, first);
910 dst->pw_shell = buf + ofs;
920 * the caller has to call nwrap_unload() on failure
922 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
924 struct nwrap_gr *nwrap_gr;
932 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
934 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
935 gr = (struct group *)realloc(nwrap_gr->list, list_size);
937 NWRAP_ERROR(("%s:realloc failed\n",__location__));
942 gr = &nwrap_gr->list[nwrap_gr->num];
949 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
950 __location__, line, c));
958 NWRAP_VERBOSE(("name[%s]\n", gr->gr_name));
963 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
964 __location__, line, c));
972 NWRAP_VERBOSE(("password[%s]\n", gr->gr_passwd));
977 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
978 __location__, line, c));
984 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
986 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
987 __location__, line, c, strerror(errno)));
991 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
992 __location__, line, c, strerror(errno)));
996 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
997 __location__, line, c, strerror(errno)));
1002 NWRAP_VERBOSE(("gid[%u]\n", gr->gr_gid));
1005 gr->gr_mem = (char **)malloc(sizeof(char *));
1007 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1010 gr->gr_mem[0] = NULL;
1012 for(nummem=0; p; nummem++) {
1022 if (strlen(c) == 0) {
1026 m_size = sizeof(char *) * (nummem+2);
1027 m = (char **)realloc(gr->gr_mem, m_size);
1029 NWRAP_ERROR(("%s:realloc(%u) failed\n",
1030 __location__, m_size));
1034 gr->gr_mem[nummem] = c;
1035 gr->gr_mem[nummem+1] = NULL;
1037 NWRAP_VERBOSE(("member[%u]: '%s'\n", nummem, gr->gr_mem[nummem]));
1040 NWRAP_DEBUG(("add group[%s:%s:%u:] with %u members\n",
1041 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem));
1047 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
1050 struct nwrap_gr *nwrap_gr;
1051 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
1053 if (nwrap_gr->list) {
1054 for (i=0; i < nwrap_gr->num; i++) {
1055 if (nwrap_gr->list[i].gr_mem) {
1056 free(nwrap_gr->list[i].gr_mem);
1059 free(nwrap_gr->list);
1062 nwrap_gr->list = NULL;
1067 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
1068 char *buf, size_t buflen, struct group **dstp)
1078 first = src->gr_name;
1080 lastm = src->gr_mem;
1087 last = src->gr_passwd;
1089 while (*last) last++;
1091 ofsb = PTR_DIFF(last + 1, first);
1092 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
1094 if ((ofsb + ofsm) > buflen) {
1098 memcpy(buf, first, ofsb);
1099 memcpy(buf + ofsb, src->gr_mem, ofsm);
1101 ofs = PTR_DIFF(src->gr_name, first);
1102 dst->gr_name = buf + ofs;
1103 ofs = PTR_DIFF(src->gr_passwd, first);
1104 dst->gr_passwd = buf + ofs;
1105 dst->gr_gid = src->gr_gid;
1107 dst->gr_mem = (char **)(buf + ofsb);
1108 for (i=0; src->gr_mem[i]; i++) {
1109 ofs = PTR_DIFF(src->gr_mem[i], first);
1110 dst->gr_mem[i] = buf + ofs;
1120 /* user functions */
1121 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
1126 nwrap_files_cache_reload(nwrap_pw_global.cache);
1128 for (i=0; i<nwrap_pw_global.num; i++) {
1129 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
1130 NWRAP_DEBUG(("%s: user[%s] found\n",
1131 __location__, name));
1132 return &nwrap_pw_global.list[i];
1134 NWRAP_VERBOSE(("%s: user[%s] does not match [%s]\n",
1136 nwrap_pw_global.list[i].pw_name));
1139 NWRAP_DEBUG(("%s: user[%s] not found\n", __location__, name));
1145 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
1146 const char *name, struct passwd *pwdst,
1147 char *buf, size_t buflen, struct passwd **pwdstp)
1151 pw = nwrap_files_getpwnam(b, name);
1159 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1162 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
1167 nwrap_files_cache_reload(nwrap_pw_global.cache);
1169 for (i=0; i<nwrap_pw_global.num; i++) {
1170 if (nwrap_pw_global.list[i].pw_uid == uid) {
1171 NWRAP_DEBUG(("%s: uid[%u] found\n",
1172 __location__, uid));
1173 return &nwrap_pw_global.list[i];
1175 NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n",
1177 nwrap_pw_global.list[i].pw_uid));
1180 NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid));
1186 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
1187 uid_t uid, struct passwd *pwdst,
1188 char *buf, size_t buflen, struct passwd **pwdstp)
1192 pw = nwrap_files_getpwuid(b, uid);
1200 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1203 /* user enum functions */
1204 static void nwrap_files_setpwent(struct nwrap_backend *b)
1206 nwrap_pw_global.idx = 0;
1209 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
1213 if (nwrap_pw_global.idx == 0) {
1214 nwrap_files_cache_reload(nwrap_pw_global.cache);
1217 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
1222 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
1224 NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n",
1225 __location__, pw->pw_name, pw->pw_uid));
1230 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
1231 struct passwd *pwdst, char *buf,
1232 size_t buflen, struct passwd **pwdstp)
1236 pw = nwrap_files_getpwent(b);
1244 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1247 static void nwrap_files_endpwent(struct nwrap_backend *b)
1249 nwrap_pw_global.idx = 0;
1252 /* misc functions */
1253 static int nwrap_files_initgroups(struct nwrap_backend *b,
1254 const char *user, gid_t group)
1256 /* TODO: maybe we should also fake this... */
1260 /* group functions */
1261 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
1266 nwrap_files_cache_reload(nwrap_gr_global.cache);
1268 for (i=0; i<nwrap_gr_global.num; i++) {
1269 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
1270 NWRAP_DEBUG(("%s: group[%s] found\n",
1271 __location__, name));
1272 return &nwrap_gr_global.list[i];
1274 NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n",
1276 nwrap_gr_global.list[i].gr_name));
1279 NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name));
1285 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
1286 const char *name, struct group *grdst,
1287 char *buf, size_t buflen, struct group **grdstp)
1291 gr = nwrap_files_getgrnam(b, name);
1299 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1302 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
1307 nwrap_files_cache_reload(nwrap_gr_global.cache);
1309 for (i=0; i<nwrap_gr_global.num; i++) {
1310 if (nwrap_gr_global.list[i].gr_gid == gid) {
1311 NWRAP_DEBUG(("%s: gid[%u] found\n",
1312 __location__, gid));
1313 return &nwrap_gr_global.list[i];
1315 NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n",
1317 nwrap_gr_global.list[i].gr_gid));
1320 NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid));
1326 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
1327 gid_t gid, struct group *grdst,
1328 char *buf, size_t buflen, struct group **grdstp)
1332 gr = nwrap_files_getgrgid(b, gid);
1340 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1343 /* group enum functions */
1344 static void nwrap_files_setgrent(struct nwrap_backend *b)
1346 nwrap_gr_global.idx = 0;
1349 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
1353 if (nwrap_gr_global.idx == 0) {
1354 nwrap_files_cache_reload(nwrap_gr_global.cache);
1357 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
1362 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
1364 NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n",
1365 __location__, gr->gr_name, gr->gr_gid));
1370 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
1371 struct group *grdst, char *buf,
1372 size_t buflen, struct group **grdstp)
1376 gr = nwrap_files_getgrent(b);
1384 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1387 static void nwrap_files_endgrent(struct nwrap_backend *b)
1389 nwrap_gr_global.idx = 0;
1397 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
1400 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
1403 static struct passwd pwd;
1404 static char buf[1000];
1407 if (!b->fns->_nss_getpwnam_r) {
1411 status = b->fns->_nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &errno);
1412 if (status == NSS_STATUS_NOTFOUND) {
1415 if (status != NSS_STATUS_SUCCESS) {
1421 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
1422 const char *name, struct passwd *pwdst,
1423 char *buf, size_t buflen, struct passwd **pwdstp)
1427 if (!b->fns->_nss_getpwnam_r) {
1428 return NSS_STATUS_NOTFOUND;
1431 ret = b->fns->_nss_getpwnam_r(name, pwdst, buf, buflen, &errno);
1433 case NSS_STATUS_SUCCESS:
1435 case NSS_STATUS_NOTFOUND:
1440 case NSS_STATUS_TRYAGAIN:
1453 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
1456 static struct passwd pwd;
1457 static char buf[1000];
1460 if (!b->fns->_nss_getpwuid_r) {
1464 status = b->fns->_nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &errno);
1465 if (status == NSS_STATUS_NOTFOUND) {
1468 if (status != NSS_STATUS_SUCCESS) {
1474 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
1475 uid_t uid, struct passwd *pwdst,
1476 char *buf, size_t buflen, struct passwd **pwdstp)
1480 if (!b->fns->_nss_getpwuid_r) {
1484 ret = b->fns->_nss_getpwuid_r(uid, pwdst, buf, buflen, &errno);
1486 case NSS_STATUS_SUCCESS:
1488 case NSS_STATUS_NOTFOUND:
1493 case NSS_STATUS_TRYAGAIN:
1506 static void nwrap_module_setpwent(struct nwrap_backend *b)
1508 if (!b->fns->_nss_setpwent) {
1512 b->fns->_nss_setpwent();
1515 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
1517 static struct passwd pwd;
1518 static char buf[1000];
1521 if (!b->fns->_nss_getpwent_r) {
1525 status = b->fns->_nss_getpwent_r(&pwd, buf, sizeof(buf), &errno);
1526 if (status == NSS_STATUS_NOTFOUND) {
1529 if (status != NSS_STATUS_SUCCESS) {
1535 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
1536 struct passwd *pwdst, char *buf,
1537 size_t buflen, struct passwd **pwdstp)
1541 if (!b->fns->_nss_getpwent_r) {
1545 ret = b->fns->_nss_getpwent_r(pwdst, buf, buflen, &errno);
1547 case NSS_STATUS_SUCCESS:
1549 case NSS_STATUS_NOTFOUND:
1554 case NSS_STATUS_TRYAGAIN:
1567 static void nwrap_module_endpwent(struct nwrap_backend *b)
1569 if (!b->fns->_nss_endpwent) {
1573 b->fns->_nss_endpwent();
1576 static int nwrap_module_initgroups(struct nwrap_backend *b,
1577 const char *user, gid_t group)
1583 if (!b->fns->_nss_initgroups) {
1584 return NSS_STATUS_UNAVAIL;
1587 return b->fns->_nss_initgroups(user, group, &start, &size, &groups, 0, &errno);
1590 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
1593 static struct group grp;
1595 static int buflen = 1000;
1598 if (!b->fns->_nss_getgrnam_r) {
1603 buf = (char *)malloc(buflen);
1606 status = b->fns->_nss_getgrnam_r(name, &grp, buf, buflen, &errno);
1607 if (status == NSS_STATUS_TRYAGAIN) {
1609 buf = (char *)realloc(buf, buflen);
1615 if (status == NSS_STATUS_NOTFOUND) {
1619 if (status != NSS_STATUS_SUCCESS) {
1626 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
1627 const char *name, struct group *grdst,
1628 char *buf, size_t buflen, struct group **grdstp)
1632 if (!b->fns->_nss_getgrnam_r) {
1636 ret = b->fns->_nss_getgrnam_r(name, grdst, buf, buflen, &errno);
1638 case NSS_STATUS_SUCCESS:
1640 case NSS_STATUS_NOTFOUND:
1645 case NSS_STATUS_TRYAGAIN:
1658 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
1661 static struct group grp;
1663 static int buflen = 1000;
1666 if (!b->fns->_nss_getgrgid_r) {
1671 buf = (char *)malloc(buflen);
1675 status = b->fns->_nss_getgrgid_r(gid, &grp, buf, buflen, &errno);
1676 if (status == NSS_STATUS_TRYAGAIN) {
1678 buf = (char *)realloc(buf, buflen);
1684 if (status == NSS_STATUS_NOTFOUND) {
1688 if (status != NSS_STATUS_SUCCESS) {
1695 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
1696 gid_t gid, struct group *grdst,
1697 char *buf, size_t buflen, struct group **grdstp)
1701 if (!b->fns->_nss_getgrgid_r) {
1705 ret = b->fns->_nss_getgrgid_r(gid, grdst, buf, buflen, &errno);
1707 case NSS_STATUS_SUCCESS:
1709 case NSS_STATUS_NOTFOUND:
1714 case NSS_STATUS_TRYAGAIN:
1727 static void nwrap_module_setgrent(struct nwrap_backend *b)
1729 if (!b->fns->_nss_setgrent) {
1733 b->fns->_nss_setgrent();
1736 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
1738 static struct group grp;
1740 static int buflen = 1024;
1743 if (!b->fns->_nss_getgrent_r) {
1748 buf = (char *)malloc(buflen);
1752 status = b->fns->_nss_getgrent_r(&grp, buf, buflen, &errno);
1753 if (status == NSS_STATUS_TRYAGAIN) {
1755 buf = (char *)realloc(buf, buflen);
1761 if (status == NSS_STATUS_NOTFOUND) {
1765 if (status != NSS_STATUS_SUCCESS) {
1772 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
1773 struct group *grdst, char *buf,
1774 size_t buflen, struct group **grdstp)
1778 if (!b->fns->_nss_getgrent_r) {
1782 ret = b->fns->_nss_getgrent_r(grdst, buf, buflen, &errno);
1784 case NSS_STATUS_SUCCESS:
1786 case NSS_STATUS_NOTFOUND:
1791 case NSS_STATUS_TRYAGAIN:
1804 static void nwrap_module_endgrent(struct nwrap_backend *b)
1806 if (!b->fns->_nss_endgrent) {
1810 b->fns->_nss_endgrent();
1817 _PUBLIC_ struct passwd *nwrap_getpwnam(const char *name)
1822 if (!nwrap_enabled()) {
1823 return real_getpwnam(name);
1826 for (i=0; i < nwrap_main_global->num_backends; i++) {
1827 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1828 pwd = b->ops->nw_getpwnam(b, name);
1837 _PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
1838 char *buf, size_t buflen, struct passwd **pwdstp)
1842 if (!nwrap_enabled()) {
1843 return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
1846 for (i=0; i < nwrap_main_global->num_backends; i++) {
1847 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1848 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
1849 if (ret == ENOENT) {
1858 _PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid)
1863 if (!nwrap_enabled()) {
1864 return real_getpwuid(uid);
1867 for (i=0; i < nwrap_main_global->num_backends; i++) {
1868 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1869 pwd = b->ops->nw_getpwuid(b, uid);
1878 _PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
1879 char *buf, size_t buflen, struct passwd **pwdstp)
1883 if (!nwrap_enabled()) {
1884 return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
1887 for (i=0; i < nwrap_main_global->num_backends; i++) {
1888 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1889 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
1890 if (ret == ENOENT) {
1899 _PUBLIC_ void nwrap_setpwent(void)
1903 if (!nwrap_enabled()) {
1908 for (i=0; i < nwrap_main_global->num_backends; i++) {
1909 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1910 b->ops->nw_setpwent(b);
1914 _PUBLIC_ struct passwd *nwrap_getpwent(void)
1919 if (!nwrap_enabled()) {
1920 return real_getpwent();
1923 for (i=0; i < nwrap_main_global->num_backends; i++) {
1924 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1925 pwd = b->ops->nw_getpwent(b);
1934 _PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
1935 size_t buflen, struct passwd **pwdstp)
1939 if (!nwrap_enabled()) {
1940 #ifdef SOLARIS_GETPWENT_R
1942 pw = real_getpwent_r(pwdst, buf, buflen);
1954 return real_getpwent_r(pwdst, buf, buflen, pwdstp);
1958 for (i=0; i < nwrap_main_global->num_backends; i++) {
1959 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1960 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
1961 if (ret == ENOENT) {
1970 _PUBLIC_ void nwrap_endpwent(void)
1974 if (!nwrap_enabled()) {
1979 for (i=0; i < nwrap_main_global->num_backends; i++) {
1980 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1981 b->ops->nw_endpwent(b);
1985 _PUBLIC_ int nwrap_initgroups(const char *user, gid_t group)
1989 if (!nwrap_enabled()) {
1990 return real_initgroups(user, group);
1993 for (i=0; i < nwrap_main_global->num_backends; i++) {
1994 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1995 return b->ops->nw_initgroups(b, user, group);
2002 _PUBLIC_ struct group *nwrap_getgrnam(const char *name)
2007 if (!nwrap_enabled()) {
2008 return real_getgrnam(name);
2011 for (i=0; i < nwrap_main_global->num_backends; i++) {
2012 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2013 grp = b->ops->nw_getgrnam(b, name);
2022 _PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst,
2023 char *buf, size_t buflen, struct group **grdstp)
2027 if (!nwrap_enabled()) {
2028 return real_getgrnam_r(name, grdst, buf, buflen, grdstp);
2031 for (i=0; i < nwrap_main_global->num_backends; i++) {
2032 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2033 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
2034 if (ret == ENOENT) {
2043 _PUBLIC_ struct group *nwrap_getgrgid(gid_t gid)
2048 if (!nwrap_enabled()) {
2049 return real_getgrgid(gid);
2052 for (i=0; i < nwrap_main_global->num_backends; i++) {
2053 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2054 grp = b->ops->nw_getgrgid(b, gid);
2063 _PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
2064 char *buf, size_t buflen, struct group **grdstp)
2068 if (!nwrap_enabled()) {
2069 return real_getgrgid_r(gid, grdst, buf, buflen, grdstp);
2072 for (i=0; i < nwrap_main_global->num_backends; i++) {
2073 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2074 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
2075 if (ret == ENOENT) {
2084 _PUBLIC_ void nwrap_setgrent(void)
2088 if (!nwrap_enabled()) {
2093 for (i=0; i < nwrap_main_global->num_backends; i++) {
2094 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2095 b->ops->nw_setgrent(b);
2099 _PUBLIC_ struct group *nwrap_getgrent(void)
2104 if (!nwrap_enabled()) {
2105 return real_getgrent();
2108 for (i=0; i < nwrap_main_global->num_backends; i++) {
2109 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2110 grp = b->ops->nw_getgrent(b);
2119 _PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf,
2120 size_t buflen, struct group **grdstp)
2124 if (!nwrap_enabled()) {
2125 #ifdef SOLARIS_GETGRENT_R
2127 gr = real_getgrent_r(grdst, buf, buflen);
2139 return real_getgrent_r(grdst, buf, buflen, grdstp);
2143 for (i=0; i < nwrap_main_global->num_backends; i++) {
2144 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2145 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
2146 if (ret == ENOENT) {
2155 _PUBLIC_ void nwrap_endgrent(void)
2159 if (!nwrap_enabled()) {
2164 for (i=0; i < nwrap_main_global->num_backends; i++) {
2165 struct nwrap_backend *b = &nwrap_main_global->backends[i];
2166 b->ops->nw_endgrent(b);
2170 _PUBLIC_ int nwrap_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
2175 const char *name_of_group = NULL;
2177 if (!nwrap_enabled()) {
2178 return real_getgrouplist(user, group, groups, ngroups);
2181 NWRAP_DEBUG(("%s: getgrouplist called for %s\n", __location__, user));
2183 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
2185 NWRAP_ERROR(("%s:calloc failed\n",__location__));
2190 memcpy(groups_tmp, &group, sizeof(gid_t));
2192 grp = nwrap_getgrgid(group);
2194 name_of_group = grp->gr_name;
2198 while ((grp = nwrap_getgrent()) != NULL) {
2201 NWRAP_VERBOSE(("%s: inspecting %s for group membership\n",
2202 __location__, grp->gr_name));
2204 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
2206 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
2207 (strcmp(name_of_group, grp->gr_name) != 0)) {
2209 NWRAP_DEBUG(("%s: %s is member of %s\n",
2210 __location__, user, grp->gr_name));
2212 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
2214 NWRAP_ERROR(("%s:calloc failed\n",__location__));
2219 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
2227 NWRAP_VERBOSE(("%s: %s is member of %d groups: %d\n",
2228 __location__, user, *ngroups));
2230 if (*ngroups < count) {
2237 memcpy(groups, groups_tmp, count * sizeof(gid_t));