nss_wrapper: add missing return in nwrap_module_init().
[Samba/fernandojvsilva.git] / lib / nss_wrapper / nss_wrapper.c
blob0e02e05cc3f8645de3917136b13eaabc8389aa7b
1 /*
2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef _SAMBA_BUILD_
36 #define NSS_WRAPPER_NOT_REPLACE
37 #include "../replace/replace.h"
38 #include "system/passwd.h"
39 #include "system/filesys.h"
40 #include "../nsswitch/nsstest.h"
42 #else /* _SAMBA_BUILD_ */
44 #error nss_wrapper_only_supported_in_samba_yet
46 #endif
48 #ifndef _PUBLIC_
49 #define _PUBLIC_
50 #endif
52 /* not all systems have _r functions... */
53 #ifndef HAVE_GETPWNAM_R
54 #define getpwnam_r(name, pwdst, buf, buflen, pwdstp) ENOSYS
55 #endif
56 #ifndef HAVE_GETPWUID_R
57 #define getpwuid_r(uid, pwdst, buf, buflen, pwdstp) ENOSYS
58 #endif
59 #ifndef HAVE_GETPWENT_R
60 #define getpwent_r(pwdst, buf, buflen, pwdstp) ENOSYS
61 #endif
62 #ifndef HAVE_GETGRNAM_R
63 #define getgrnam_r(name, grdst, buf, buflen, grdstp) ENOSYS
64 #endif
65 #ifndef HAVE_GETGRGID_R
66 #define getgrgid_r(gid, grdst, buf, buflen, grdstp) ENOSYS
67 #endif
68 #ifndef HAVE_GETGRENT_R
69 #define getgrent_r(grdst, buf, buflen, grdstp) ENOSYS
70 #endif
72 /* not all systems have getgrouplist */
73 #ifndef HAVE_GETGROUPLIST
74 #define getgrouplist(user, group, groups, ngroups) 0
75 #endif
77 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
78 * for now */
79 #define REWRITE_CALLS
81 #ifdef REWRITE_CALLS
83 #define real_getpwnam getpwnam
84 #define real_getpwnam_r getpwnam_r
85 #define real_getpwuid getpwuid
86 #define real_getpwuid_r getpwuid_r
88 #define real_setpwent setpwent
89 #define real_getpwent getpwent
90 #define real_getpwent_r getpwent_r
91 #define real_endpwent endpwent
94 #define real_getgrlst getgrlst
95 #define real_getgrlst_r getgrlst_r
96 #define real_initgroups_dyn initgroups_dyn
98 #define real_initgroups initgroups
99 #define real_getgrouplist getgrouplist
101 #define real_getgrnam getgrnam
102 #define real_getgrnam_r getgrnam_r
103 #define real_getgrgid getgrgid
104 #define real_getgrgid_r getgrgid_r
106 #define real_setgrent setgrent
107 #define real_getgrent getgrent
108 #define real_getgrent_r getgrent_r
109 #define real_endgrent endgrent
111 #endif
113 #if 0
114 # ifdef DEBUG
115 # define NWRAP_ERROR(args) DEBUG(0, args)
116 # else
117 # define NWRAP_ERROR(args) printf args
118 # endif
119 #else
120 #define NWRAP_ERROR(args)
121 #endif
123 #if 0
124 # ifdef DEBUG
125 # define NWRAP_DEBUG(args) DEBUG(0, args)
126 # else
127 # define NWRAP_DEBUG(args) printf args
128 # endif
129 #else
130 #define NWRAP_DEBUG(args)
131 #endif
133 #if 0
134 # ifdef DEBUG
135 # define NWRAP_VERBOSE(args) DEBUG(0, args)
136 # else
137 # define NWRAP_VERBOSE(args) printf args
138 # endif
139 #else
140 #define NWRAP_VERBOSE(args)
141 #endif
143 struct nwrap_module_nss_fns {
144 NSS_STATUS (*_nss_getpwnam_r)(const char *name, struct passwd *result, char *buffer,
145 size_t buflen, int *errnop);
146 NSS_STATUS (*_nss_getpwuid_r)(uid_t uid, struct passwd *result, char *buffer,
147 size_t buflen, int *errnop);
148 NSS_STATUS (*_nss_setpwent)(void);
149 NSS_STATUS (*_nss_getpwent_r)(struct passwd *result, char *buffer,
150 size_t buflen, int *errnop);
151 NSS_STATUS (*_nss_endpwent)(void);
152 NSS_STATUS (*_nss_initgroups)(const char *user, gid_t group, long int *start,
153 long int *size, gid_t **groups, long int limit, int *errnop);
154 NSS_STATUS (*_nss_getgrnam_r)(const char *name, struct group *result, char *buffer,
155 size_t buflen, int *errnop);
156 NSS_STATUS (*_nss_getgrgid_r)(gid_t gid, struct group *result, char *buffer,
157 size_t buflen, int *errnop);
158 NSS_STATUS (*_nss_setgrent)(void);
159 NSS_STATUS (*_nss_getgrent_r)(struct group *result, char *buffer,
160 size_t buflen, int *errnop);
161 NSS_STATUS (*_nss_endgrent)(void);
164 struct nwrap_backend {
165 const char *name;
166 const char *so_path;
167 void *so_handle;
168 struct nwrap_ops *ops;
169 struct nwrap_module_nss_fns *fns;
172 struct nwrap_ops {
173 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
174 const char *name);
175 int (*nw_getpwnam_r)(struct nwrap_backend *b,
176 const char *name, struct passwd *pwdst,
177 char *buf, size_t buflen, struct passwd **pwdstp);
178 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
179 uid_t uid);
180 int (*nw_getpwuid_r)(struct nwrap_backend *b,
181 uid_t uid, struct passwd *pwdst,
182 char *buf, size_t buflen, struct passwd **pwdstp);
183 void (*nw_setpwent)(struct nwrap_backend *b);
184 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
185 int (*nw_getpwent_r)(struct nwrap_backend *b,
186 struct passwd *pwdst, char *buf,
187 size_t buflen, struct passwd **pwdstp);
188 void (*nw_endpwent)(struct nwrap_backend *b);
189 int (*nw_initgroups)(struct nwrap_backend *b,
190 const char *user, gid_t group);
191 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
192 const char *name);
193 int (*nw_getgrnam_r)(struct nwrap_backend *b,
194 const char *name, struct group *grdst,
195 char *buf, size_t buflen, struct group **grdstp);
196 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
197 gid_t gid);
198 int (*nw_getgrgid_r)(struct nwrap_backend *b,
199 gid_t gid, struct group *grdst,
200 char *buf, size_t buflen, struct group **grdstp);
201 void (*nw_setgrent)(struct nwrap_backend *b);
202 struct group * (*nw_getgrent)(struct nwrap_backend *b);
203 int (*nw_getgrent_r)(struct nwrap_backend *b,
204 struct group *grdst, char *buf,
205 size_t buflen, struct group **grdstp);
206 void (*nw_endgrent)(struct nwrap_backend *b);
209 /* protoypes for files backend */
212 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
213 const char *name);
214 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
215 const char *name, struct passwd *pwdst,
216 char *buf, size_t buflen, struct passwd **pwdstp);
217 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
218 uid_t uid);
219 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
220 uid_t uid, struct passwd *pwdst,
221 char *buf, size_t buflen, struct passwd **pwdstp);
222 static void nwrap_files_setpwent(struct nwrap_backend *b);
223 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
224 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
225 struct passwd *pwdst, char *buf,
226 size_t buflen, struct passwd **pwdstp);
227 static void nwrap_files_endpwent(struct nwrap_backend *b);
228 static int nwrap_files_initgroups(struct nwrap_backend *b,
229 const char *user, gid_t group);
230 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
231 const char *name);
232 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
233 const char *name, struct group *grdst,
234 char *buf, size_t buflen, struct group **grdstp);
235 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
236 gid_t gid);
237 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
238 gid_t gid, struct group *grdst,
239 char *buf, size_t buflen, struct group **grdstp);
240 static void nwrap_files_setgrent(struct nwrap_backend *b);
241 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
242 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
243 struct group *grdst, char *buf,
244 size_t buflen, struct group **grdstp);
245 static void nwrap_files_endgrent(struct nwrap_backend *b);
247 /* protoypes for module backend */
249 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b);
250 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
251 struct passwd *pwdst, char *buf,
252 size_t buflen, struct passwd **pwdstp);
253 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
254 const char *name);
255 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
256 const char *name, struct passwd *pwdst,
257 char *buf, size_t buflen, struct passwd **pwdstp);
258 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
259 uid_t uid);
260 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
261 uid_t uid, struct passwd *pwdst,
262 char *buf, size_t buflen, struct passwd **pwdstp);
263 static void nwrap_module_setpwent(struct nwrap_backend *b);
264 static void nwrap_module_endpwent(struct nwrap_backend *b);
265 static struct group *nwrap_module_getgrent(struct nwrap_backend *b);
266 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
267 struct group *grdst, char *buf,
268 size_t buflen, struct group **grdstp);
269 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
270 const char *name);
271 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
272 const char *name, struct group *grdst,
273 char *buf, size_t buflen, struct group **grdstp);
274 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
275 gid_t gid);
276 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
277 gid_t gid, struct group *grdst,
278 char *buf, size_t buflen, struct group **grdstp);
279 static void nwrap_module_setgrent(struct nwrap_backend *b);
280 static void nwrap_module_endgrent(struct nwrap_backend *b);
281 static int nwrap_module_initgroups(struct nwrap_backend *b,
282 const char *user, gid_t group);
284 struct nwrap_ops nwrap_files_ops = {
285 .nw_getpwnam = nwrap_files_getpwnam,
286 .nw_getpwnam_r = nwrap_files_getpwnam_r,
287 .nw_getpwuid = nwrap_files_getpwuid,
288 .nw_getpwuid_r = nwrap_files_getpwuid_r,
289 .nw_setpwent = nwrap_files_setpwent,
290 .nw_getpwent = nwrap_files_getpwent,
291 .nw_getpwent_r = nwrap_files_getpwent_r,
292 .nw_endpwent = nwrap_files_endpwent,
293 .nw_initgroups = nwrap_files_initgroups,
294 .nw_getgrnam = nwrap_files_getgrnam,
295 .nw_getgrnam_r = nwrap_files_getgrnam_r,
296 .nw_getgrgid = nwrap_files_getgrgid,
297 .nw_getgrgid_r = nwrap_files_getgrgid_r,
298 .nw_setgrent = nwrap_files_setgrent,
299 .nw_getgrent = nwrap_files_getgrent,
300 .nw_getgrent_r = nwrap_files_getgrent_r,
301 .nw_endgrent = nwrap_files_endgrent,
304 struct nwrap_ops nwrap_module_ops = {
305 .nw_getpwnam = nwrap_module_getpwnam,
306 .nw_getpwnam_r = nwrap_module_getpwnam_r,
307 .nw_getpwuid = nwrap_module_getpwuid,
308 .nw_getpwuid_r = nwrap_module_getpwuid_r,
309 .nw_setpwent = nwrap_module_setpwent,
310 .nw_getpwent = nwrap_module_getpwent,
311 .nw_getpwent_r = nwrap_module_getpwent_r,
312 .nw_endpwent = nwrap_module_endpwent,
313 .nw_initgroups = nwrap_module_initgroups,
314 .nw_getgrnam = nwrap_module_getgrnam,
315 .nw_getgrnam_r = nwrap_module_getgrnam_r,
316 .nw_getgrgid = nwrap_module_getgrgid,
317 .nw_getgrgid_r = nwrap_module_getgrgid_r,
318 .nw_setgrent = nwrap_module_setgrent,
319 .nw_getgrent = nwrap_module_getgrent,
320 .nw_getgrent_r = nwrap_module_getgrent_r,
321 .nw_endgrent = nwrap_module_endgrent,
324 struct nwrap_main {
325 const char *nwrap_switch;
326 int num_backends;
327 struct nwrap_backend *backends;
330 struct nwrap_main *nwrap_main_global;
331 struct nwrap_main __nwrap_main_global;
333 struct nwrap_cache {
334 const char *path;
335 int fd;
336 struct stat st;
337 uint8_t *buf;
338 void *private_data;
339 bool (*parse_line)(struct nwrap_cache *, char *line);
340 void (*unload)(struct nwrap_cache *);
343 struct nwrap_pw {
344 struct nwrap_cache *cache;
346 struct passwd *list;
347 int num;
348 int idx;
351 struct nwrap_cache __nwrap_cache_pw;
352 struct nwrap_pw nwrap_pw_global;
354 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
355 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
357 struct nwrap_gr {
358 struct nwrap_cache *cache;
360 struct group *list;
361 int num;
362 int idx;
365 struct nwrap_cache __nwrap_cache_gr;
366 struct nwrap_gr nwrap_gr_global;
368 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
369 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
371 static void *nwrap_load_module_fn(struct nwrap_backend *b,
372 const char *fn_name)
374 void *res;
375 char *s;
377 if (!b->so_handle) {
378 NWRAP_ERROR(("%s: no handle\n",
379 __location__));
380 return NULL;
383 if (asprintf(&s, "_nss_%s_%s", b->name, fn_name) == -1) {
384 NWRAP_ERROR(("%s: out of memory\n",
385 __location__));
386 return NULL;
389 res = dlsym(b->so_handle, s);
390 if (!res) {
391 NWRAP_ERROR(("%s: cannot find function %s in %s\n",
392 __location__, s, b->so_path));
394 free(s);
395 s = NULL;
396 return res;
399 static struct nwrap_module_nss_fns *nwrap_load_module_fns(struct nwrap_backend *b)
401 struct nwrap_module_nss_fns *fns;
403 if (!b->so_handle) {
404 return NULL;
407 fns = (struct nwrap_module_nss_fns *)malloc(sizeof(struct nwrap_module_nss_fns));
408 if (!fns) {
409 return NULL;
412 fns->_nss_getpwnam_r = (NSS_STATUS (*)(const char *, struct passwd *, char *, size_t, int *))
413 nwrap_load_module_fn(b, "getpwnam_r");
414 fns->_nss_getpwuid_r = (NSS_STATUS (*)(uid_t, struct passwd *, char *, size_t, int *))
415 nwrap_load_module_fn(b, "getpwuid_r");
416 fns->_nss_setpwent = (NSS_STATUS(*)(void))
417 nwrap_load_module_fn(b, "setpwent");
418 fns->_nss_getpwent_r = (NSS_STATUS (*)(struct passwd *, char *, size_t, int *))
419 nwrap_load_module_fn(b, "getpwent_r");
420 fns->_nss_endpwent = (NSS_STATUS(*)(void))
421 nwrap_load_module_fn(b, "endpwent");
422 fns->_nss_initgroups = (NSS_STATUS (*)(const char *, gid_t, long int *, long int *, gid_t **, long int, int *))
423 nwrap_load_module_fn(b, "initgroups_dyn");
424 fns->_nss_getgrnam_r = (NSS_STATUS (*)(const char *, struct group *, char *, size_t, int *))
425 nwrap_load_module_fn(b, "getgrnam_r");
426 fns->_nss_getgrgid_r = (NSS_STATUS (*)(gid_t, struct group *, char *, size_t, int *))
427 nwrap_load_module_fn(b, "getgrgid_r");
428 fns->_nss_setgrent = (NSS_STATUS(*)(void))
429 nwrap_load_module_fn(b, "setgrent");
430 fns->_nss_getgrent_r = (NSS_STATUS (*)(struct group *, char *, size_t, int *))
431 nwrap_load_module_fn(b, "getgrent_r");
432 fns->_nss_endgrent = (NSS_STATUS(*)(void))
433 nwrap_load_module_fn(b, "endgrent");
435 return fns;
438 static void *nwrap_load_module(const char *so_path)
440 void *h;
442 if (!so_path || !strlen(so_path)) {
443 return NULL;
446 h = dlopen(so_path, RTLD_LAZY);
447 if (!h) {
448 NWRAP_ERROR(("%s: cannot open shared library %s\n",
449 __location__, so_path));
450 return NULL;
453 return h;
456 static bool nwrap_module_init(const char *name,
457 struct nwrap_ops *ops,
458 const char *so_path,
459 int *num_backends,
460 struct nwrap_backend **backends)
462 *backends = (struct nwrap_backend *)realloc(*backends,
463 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
464 if (!*backends) {
465 NWRAP_ERROR(("%s: out of memory\n",
466 __location__));
467 return false;
470 (*backends)[*num_backends].name = name;
471 (*backends)[*num_backends].ops = ops;
472 (*backends)[*num_backends].so_path = so_path;
473 (*backends)[*num_backends].so_handle = nwrap_load_module(so_path);
474 (*backends)[*num_backends].fns = nwrap_load_module_fns(&((*backends)[*num_backends]));
476 (*num_backends)++;
478 return true;
481 static void nwrap_backend_init(struct nwrap_main *r)
483 r->num_backends = 0;
484 r->backends = NULL;
486 if (!nwrap_module_init("files", &nwrap_files_ops, NULL,
487 &r->num_backends,
488 &r->backends)) {
489 NWRAP_ERROR(("%s: failed to initialize 'files' backend\n",
490 __location__));
491 return;
495 static void nwrap_init(void)
497 static bool initialized;
499 if (initialized) return;
500 initialized = true;
502 nwrap_main_global = &__nwrap_main_global;
504 nwrap_backend_init(nwrap_main_global);
506 nwrap_pw_global.cache = &__nwrap_cache_pw;
508 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
509 nwrap_pw_global.cache->fd = -1;
510 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
511 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
512 nwrap_pw_global.cache->unload = nwrap_pw_unload;
514 nwrap_gr_global.cache = &__nwrap_cache_gr;
516 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
517 nwrap_gr_global.cache->fd = -1;
518 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
519 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
520 nwrap_gr_global.cache->unload = nwrap_gr_unload;
523 static bool nwrap_enabled(void)
525 nwrap_init();
527 if (!nwrap_pw_global.cache->path) {
528 return false;
530 if (nwrap_pw_global.cache->path[0] == '\0') {
531 return false;
533 if (!nwrap_gr_global.cache->path) {
534 return false;
536 if (nwrap_gr_global.cache->path[0] == '\0') {
537 return false;
540 return true;
543 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
545 int ret;
546 uint8_t *buf = NULL;
547 char *nline;
549 if (nwrap->st.st_size == 0) {
550 NWRAP_DEBUG(("%s: size == 0\n",
551 __location__));
552 goto done;
555 if (nwrap->st.st_size > INT32_MAX) {
556 NWRAP_ERROR(("%s: size[%u] larger than INT32_MAX\n",
557 __location__, (unsigned)nwrap->st.st_size));
558 goto failed;
561 ret = lseek(nwrap->fd, 0, SEEK_SET);
562 if (ret != 0) {
563 NWRAP_ERROR(("%s: lseek - %d\n",__location__,ret));
564 goto failed;
567 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
568 if (!buf) {
569 NWRAP_ERROR(("%s: malloc failed\n",__location__));
570 goto failed;
573 ret = read(nwrap->fd, buf, nwrap->st.st_size);
574 if (ret != nwrap->st.st_size) {
575 NWRAP_ERROR(("%s: read(%u) gave %d\n",
576 __location__, (unsigned)nwrap->st.st_size, ret));
577 goto failed;
580 buf[nwrap->st.st_size] = '\0';
582 nline = (char *)buf;
583 while (nline && nline[0]) {
584 char *line;
585 char *e;
586 bool ok;
588 line = nline;
589 nline = NULL;
591 e = strchr(line, '\n');
592 if (e) {
593 e[0] = '\0';
594 e++;
595 if (e[0] == '\r') {
596 e[0] = '\0';
597 e++;
599 nline = e;
602 NWRAP_VERBOSE(("%s:'%s'\n",__location__, line));
604 if (strlen(line) == 0) {
605 continue;
608 ok = nwrap->parse_line(nwrap, line);
609 if (!ok) {
610 goto failed;
614 done:
615 nwrap->buf = buf;
616 return true;
618 failed:
619 if (buf) free(buf);
620 return false;
623 static void nwrap_cache_unload(struct nwrap_cache *nwrap)
625 nwrap->unload(nwrap);
627 if (nwrap->buf) free(nwrap->buf);
629 nwrap->buf = NULL;
632 static void nwrap_cache_reload(struct nwrap_cache *nwrap)
634 struct stat st;
635 int ret;
636 bool ok;
637 bool retried = false;
639 reopen:
640 if (nwrap->fd < 0) {
641 nwrap->fd = open(nwrap->path, O_RDONLY);
642 if (nwrap->fd < 0) {
643 NWRAP_ERROR(("%s: unable to open '%s' readonly %d:%s\n",
644 __location__,
645 nwrap->path, nwrap->fd,
646 strerror(errno)));
647 return;
649 NWRAP_VERBOSE(("%s: open '%s'\n", __location__, nwrap->path));
652 ret = fstat(nwrap->fd, &st);
653 if (ret != 0) {
654 NWRAP_ERROR(("%s: fstat(%s) - %d:%s\n",
655 __location__,
656 nwrap->path,
657 ret, strerror(errno)));
658 return;
661 if (retried == false && st.st_nlink == 0) {
662 /* maybe someone has replaced the file... */
663 NWRAP_DEBUG(("%s: st_nlink == 0, reopen %s\n",
664 __location__, nwrap->path));
665 retried = true;
666 memset(&nwrap->st, 0, sizeof(nwrap->st));
667 close(nwrap->fd);
668 nwrap->fd = -1;
669 goto reopen;
672 if (st.st_mtime == nwrap->st.st_mtime) {
673 NWRAP_VERBOSE(("%s: st_mtime[%u] hasn't changed, skip reload\n",
674 __location__, (unsigned)st.st_mtime));
675 return;
677 NWRAP_DEBUG(("%s: st_mtime has changed [%u] => [%u], start reload\n",
678 __location__, (unsigned)st.st_mtime,
679 (unsigned)nwrap->st.st_mtime));
681 nwrap->st = st;
683 nwrap_cache_unload(nwrap);
685 ok = nwrap_parse_file(nwrap);
686 if (!ok) {
687 NWRAP_ERROR(("%s: failed to reload %s\n",
688 __location__, nwrap->path));
689 nwrap_cache_unload(nwrap);
691 NWRAP_DEBUG(("%s: reloaded %s\n",
692 __location__, nwrap->path));
696 * the caller has to call nwrap_unload() on failure
698 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
700 struct nwrap_pw *nwrap_pw;
701 char *c;
702 char *p;
703 char *e;
704 struct passwd *pw;
705 size_t list_size;
707 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
709 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
710 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
711 if (!pw) {
712 NWRAP_ERROR(("%s:realloc(%u) failed\n",
713 __location__, list_size));
714 return false;
716 nwrap_pw->list = pw;
718 pw = &nwrap_pw->list[nwrap_pw->num];
720 c = line;
722 /* name */
723 p = strchr(c, ':');
724 if (!p) {
725 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
726 __location__, line, c));
727 return false;
729 *p = '\0';
730 p++;
731 pw->pw_name = c;
732 c = p;
734 NWRAP_VERBOSE(("name[%s]\n", pw->pw_name));
736 /* password */
737 p = strchr(c, ':');
738 if (!p) {
739 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
740 __location__, line, c));
741 return false;
743 *p = '\0';
744 p++;
745 pw->pw_passwd = c;
746 c = p;
748 NWRAP_VERBOSE(("password[%s]\n", pw->pw_passwd));
750 /* uid */
751 p = strchr(c, ':');
752 if (!p) {
753 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
754 __location__, line, c));
755 return false;
757 *p = '\0';
758 p++;
759 e = NULL;
760 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
761 if (c == e) {
762 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
763 __location__, line, c, strerror(errno)));
764 return false;
766 if (e == NULL) {
767 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
768 __location__, line, c, strerror(errno)));
769 return false;
771 if (e[0] != '\0') {
772 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
773 __location__, line, c, strerror(errno)));
774 return false;
776 c = p;
778 NWRAP_VERBOSE(("uid[%u]\n", pw->pw_uid));
780 /* gid */
781 p = strchr(c, ':');
782 if (!p) {
783 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
784 __location__, line, c));
785 return false;
787 *p = '\0';
788 p++;
789 e = NULL;
790 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
791 if (c == e) {
792 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
793 __location__, line, c, strerror(errno)));
794 return false;
796 if (e == NULL) {
797 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
798 __location__, line, c, strerror(errno)));
799 return false;
801 if (e[0] != '\0') {
802 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
803 __location__, line, c, strerror(errno)));
804 return false;
806 c = p;
808 NWRAP_VERBOSE(("gid[%u]\n", pw->pw_gid));
810 /* gecos */
811 p = strchr(c, ':');
812 if (!p) {
813 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
814 __location__, line, c));
815 return false;
817 *p = '\0';
818 p++;
819 pw->pw_gecos = c;
820 c = p;
822 NWRAP_VERBOSE(("gecos[%s]\n", pw->pw_gecos));
824 /* dir */
825 p = strchr(c, ':');
826 if (!p) {
827 NWRAP_ERROR(("%s:'%s'\n",__location__,c));
828 return false;
830 *p = '\0';
831 p++;
832 pw->pw_dir = c;
833 c = p;
835 NWRAP_VERBOSE(("dir[%s]\n", pw->pw_dir));
837 /* shell */
838 pw->pw_shell = c;
839 NWRAP_VERBOSE(("shell[%s]\n", pw->pw_shell));
841 NWRAP_DEBUG(("add user[%s:%s:%u:%u:%s:%s:%s]\n",
842 pw->pw_name, pw->pw_passwd,
843 pw->pw_uid, pw->pw_gid,
844 pw->pw_gecos, pw->pw_dir, pw->pw_shell));
846 nwrap_pw->num++;
847 return true;
850 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
852 struct nwrap_pw *nwrap_pw;
853 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
855 if (nwrap_pw->list) free(nwrap_pw->list);
857 nwrap_pw->list = NULL;
858 nwrap_pw->num = 0;
859 nwrap_pw->idx = 0;
862 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
863 char *buf, size_t buflen, struct passwd **dstp)
865 char *first;
866 char *last;
867 off_t ofs;
869 first = src->pw_name;
871 last = src->pw_shell;
872 while (*last) last++;
874 ofs = PTR_DIFF(last + 1, first);
876 if (ofs > buflen) {
877 return ERANGE;
880 memcpy(buf, first, ofs);
882 ofs = PTR_DIFF(src->pw_name, first);
883 dst->pw_name = buf + ofs;
884 ofs = PTR_DIFF(src->pw_passwd, first);
885 dst->pw_passwd = buf + ofs;
886 dst->pw_uid = src->pw_uid;
887 dst->pw_gid = src->pw_gid;
888 ofs = PTR_DIFF(src->pw_gecos, first);
889 dst->pw_gecos = buf + ofs;
890 ofs = PTR_DIFF(src->pw_dir, first);
891 dst->pw_dir = buf + ofs;
892 ofs = PTR_DIFF(src->pw_shell, first);
893 dst->pw_shell = buf + ofs;
895 if (dstp) {
896 *dstp = dst;
899 return 0;
903 * the caller has to call nwrap_unload() on failure
905 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
907 struct nwrap_gr *nwrap_gr;
908 char *c;
909 char *p;
910 char *e;
911 struct group *gr;
912 size_t list_size;
913 unsigned nummem;
915 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
917 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
918 gr = (struct group *)realloc(nwrap_gr->list, list_size);
919 if (!gr) {
920 NWRAP_ERROR(("%s:realloc failed\n",__location__));
921 return false;
923 nwrap_gr->list = gr;
925 gr = &nwrap_gr->list[nwrap_gr->num];
927 c = line;
929 /* name */
930 p = strchr(c, ':');
931 if (!p) {
932 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
933 __location__, line, c));
934 return false;
936 *p = '\0';
937 p++;
938 gr->gr_name = c;
939 c = p;
941 NWRAP_VERBOSE(("name[%s]\n", gr->gr_name));
943 /* password */
944 p = strchr(c, ':');
945 if (!p) {
946 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
947 __location__, line, c));
948 return false;
950 *p = '\0';
951 p++;
952 gr->gr_passwd = c;
953 c = p;
955 NWRAP_VERBOSE(("password[%s]\n", gr->gr_passwd));
957 /* gid */
958 p = strchr(c, ':');
959 if (!p) {
960 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
961 __location__, line, c));
962 return false;
964 *p = '\0';
965 p++;
966 e = NULL;
967 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
968 if (c == e) {
969 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
970 __location__, line, c, strerror(errno)));
971 return false;
973 if (e == NULL) {
974 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
975 __location__, line, c, strerror(errno)));
976 return false;
978 if (e[0] != '\0') {
979 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
980 __location__, line, c, strerror(errno)));
981 return false;
983 c = p;
985 NWRAP_VERBOSE(("gid[%u]\n", gr->gr_gid));
987 /* members */
988 gr->gr_mem = (char **)malloc(sizeof(char *));
989 if (!gr->gr_mem) {
990 NWRAP_ERROR(("%s:calloc failed\n",__location__));
991 return false;
993 gr->gr_mem[0] = NULL;
995 for(nummem=0; p; nummem++) {
996 char **m;
997 size_t m_size;
998 c = p;
999 p = strchr(c, ',');
1000 if (p) {
1001 *p = '\0';
1002 p++;
1005 if (strlen(c) == 0) {
1006 break;
1009 m_size = sizeof(char *) * (nummem+2);
1010 m = (char **)realloc(gr->gr_mem, m_size);
1011 if (!m) {
1012 NWRAP_ERROR(("%s:realloc(%u) failed\n",
1013 __location__, m_size));
1014 return false;
1016 gr->gr_mem = m;
1017 gr->gr_mem[nummem] = c;
1018 gr->gr_mem[nummem+1] = NULL;
1020 NWRAP_VERBOSE(("member[%u]: '%s'\n", nummem, gr->gr_mem[nummem]));
1023 NWRAP_DEBUG(("add group[%s:%s:%u:] with %u members\n",
1024 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem));
1026 nwrap_gr->num++;
1027 return true;
1030 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
1032 int i;
1033 struct nwrap_gr *nwrap_gr;
1034 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
1036 if (nwrap_gr->list) {
1037 for (i=0; i < nwrap_gr->num; i++) {
1038 if (nwrap_gr->list[i].gr_mem) {
1039 free(nwrap_gr->list[i].gr_mem);
1042 free(nwrap_gr->list);
1045 nwrap_gr->list = NULL;
1046 nwrap_gr->num = 0;
1047 nwrap_gr->idx = 0;
1050 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
1051 char *buf, size_t buflen, struct group **dstp)
1053 char *first;
1054 char **lastm;
1055 char *last = NULL;
1056 off_t ofsb;
1057 off_t ofsm;
1058 off_t ofs;
1059 unsigned i;
1061 first = src->gr_name;
1063 lastm = src->gr_mem;
1064 while (*lastm) {
1065 last = *lastm;
1066 lastm++;
1069 if (last == NULL) {
1070 last = src->gr_passwd;
1072 while (*last) last++;
1074 ofsb = PTR_DIFF(last + 1, first);
1075 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
1077 if ((ofsb + ofsm) > buflen) {
1078 return ERANGE;
1081 memcpy(buf, first, ofsb);
1082 memcpy(buf + ofsb, src->gr_mem, ofsm);
1084 ofs = PTR_DIFF(src->gr_name, first);
1085 dst->gr_name = buf + ofs;
1086 ofs = PTR_DIFF(src->gr_passwd, first);
1087 dst->gr_passwd = buf + ofs;
1088 dst->gr_gid = src->gr_gid;
1090 dst->gr_mem = (char **)(buf + ofsb);
1091 for (i=0; src->gr_mem[i]; i++) {
1092 ofs = PTR_DIFF(src->gr_mem[i], first);
1093 dst->gr_mem[i] = buf + ofs;
1096 if (dstp) {
1097 *dstp = dst;
1100 return 0;
1103 /* user functions */
1104 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
1105 const char *name)
1107 int i;
1109 nwrap_cache_reload(nwrap_pw_global.cache);
1111 for (i=0; i<nwrap_pw_global.num; i++) {
1112 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
1113 NWRAP_DEBUG(("%s: user[%s] found\n",
1114 __location__, name));
1115 return &nwrap_pw_global.list[i];
1117 NWRAP_VERBOSE(("%s: user[%s] does not match [%s]\n",
1118 __location__, name,
1119 nwrap_pw_global.list[i].pw_name));
1122 NWRAP_DEBUG(("%s: user[%s] not found\n", __location__, name));
1124 errno = ENOENT;
1125 return NULL;
1128 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
1129 const char *name, struct passwd *pwdst,
1130 char *buf, size_t buflen, struct passwd **pwdstp)
1132 struct passwd *pw;
1134 pw = nwrap_files_getpwnam(b, name);
1135 if (!pw) {
1136 if (errno == 0) {
1137 return ENOENT;
1139 return errno;
1142 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1145 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
1146 uid_t uid)
1148 int i;
1150 nwrap_cache_reload(nwrap_pw_global.cache);
1152 for (i=0; i<nwrap_pw_global.num; i++) {
1153 if (nwrap_pw_global.list[i].pw_uid == uid) {
1154 NWRAP_DEBUG(("%s: uid[%u] found\n",
1155 __location__, uid));
1156 return &nwrap_pw_global.list[i];
1158 NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n",
1159 __location__, uid,
1160 nwrap_pw_global.list[i].pw_uid));
1163 NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid));
1165 errno = ENOENT;
1166 return NULL;
1169 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
1170 uid_t uid, struct passwd *pwdst,
1171 char *buf, size_t buflen, struct passwd **pwdstp)
1173 struct passwd *pw;
1175 pw = nwrap_files_getpwuid(b, uid);
1176 if (!pw) {
1177 if (errno == 0) {
1178 return ENOENT;
1180 return errno;
1183 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1186 /* user enum functions */
1187 static void nwrap_files_setpwent(struct nwrap_backend *b)
1189 nwrap_pw_global.idx = 0;
1192 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
1194 struct passwd *pw;
1196 if (nwrap_pw_global.idx == 0) {
1197 nwrap_cache_reload(nwrap_pw_global.cache);
1200 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
1201 errno = ENOENT;
1202 return NULL;
1205 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
1207 NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n",
1208 __location__, pw->pw_name, pw->pw_uid));
1210 return pw;
1213 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
1214 struct passwd *pwdst, char *buf,
1215 size_t buflen, struct passwd **pwdstp)
1217 struct passwd *pw;
1219 pw = nwrap_files_getpwent(b);
1220 if (!pw) {
1221 if (errno == 0) {
1222 return ENOENT;
1224 return errno;
1227 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1230 static void nwrap_files_endpwent(struct nwrap_backend *b)
1232 nwrap_pw_global.idx = 0;
1235 /* misc functions */
1236 static int nwrap_files_initgroups(struct nwrap_backend *b,
1237 const char *user, gid_t group)
1239 /* TODO: maybe we should also fake this... */
1240 return EPERM;
1243 /* group functions */
1244 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
1245 const char *name)
1247 int i;
1249 nwrap_cache_reload(nwrap_gr_global.cache);
1251 for (i=0; i<nwrap_gr_global.num; i++) {
1252 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
1253 NWRAP_DEBUG(("%s: group[%s] found\n",
1254 __location__, name));
1255 return &nwrap_gr_global.list[i];
1257 NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n",
1258 __location__, name,
1259 nwrap_gr_global.list[i].gr_name));
1262 NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name));
1264 errno = ENOENT;
1265 return NULL;
1268 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
1269 const char *name, struct group *grdst,
1270 char *buf, size_t buflen, struct group **grdstp)
1272 struct group *gr;
1274 gr = nwrap_files_getgrnam(b, name);
1275 if (!gr) {
1276 if (errno == 0) {
1277 return ENOENT;
1279 return errno;
1282 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1285 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
1286 gid_t gid)
1288 int i;
1290 nwrap_cache_reload(nwrap_gr_global.cache);
1292 for (i=0; i<nwrap_gr_global.num; i++) {
1293 if (nwrap_gr_global.list[i].gr_gid == gid) {
1294 NWRAP_DEBUG(("%s: gid[%u] found\n",
1295 __location__, gid));
1296 return &nwrap_gr_global.list[i];
1298 NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n",
1299 __location__, gid,
1300 nwrap_gr_global.list[i].gr_gid));
1303 NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid));
1305 errno = ENOENT;
1306 return NULL;
1309 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
1310 gid_t gid, struct group *grdst,
1311 char *buf, size_t buflen, struct group **grdstp)
1313 struct group *gr;
1315 gr = nwrap_files_getgrgid(b, gid);
1316 if (!gr) {
1317 if (errno == 0) {
1318 return ENOENT;
1320 return errno;
1323 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1326 /* group enum functions */
1327 static void nwrap_files_setgrent(struct nwrap_backend *b)
1329 nwrap_gr_global.idx = 0;
1332 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
1334 struct group *gr;
1336 if (nwrap_gr_global.idx == 0) {
1337 nwrap_cache_reload(nwrap_gr_global.cache);
1340 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
1341 errno = ENOENT;
1342 return NULL;
1345 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
1347 NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n",
1348 __location__, gr->gr_name, gr->gr_gid));
1350 return gr;
1353 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
1354 struct group *grdst, char *buf,
1355 size_t buflen, struct group **grdstp)
1357 struct group *gr;
1359 gr = nwrap_files_getgrent(b);
1360 if (!gr) {
1361 if (errno == 0) {
1362 return ENOENT;
1364 return errno;
1367 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1370 static void nwrap_files_endgrent(struct nwrap_backend *b)
1372 nwrap_gr_global.idx = 0;
1376 * module backend
1379 static struct passwd *nwrap_module_getpwnam(struct nwrap_backend *b,
1380 const char *name)
1382 return NULL;
1385 static int nwrap_module_getpwnam_r(struct nwrap_backend *b,
1386 const char *name, struct passwd *pwdst,
1387 char *buf, size_t buflen, struct passwd **pwdstp)
1389 return ENOENT;
1392 static struct passwd *nwrap_module_getpwuid(struct nwrap_backend *b,
1393 uid_t uid)
1395 return NULL;
1398 static int nwrap_module_getpwuid_r(struct nwrap_backend *b,
1399 uid_t uid, struct passwd *pwdst,
1400 char *buf, size_t buflen, struct passwd **pwdstp)
1402 return ENOENT;
1405 static void nwrap_module_setpwent(struct nwrap_backend *b)
1409 static struct passwd *nwrap_module_getpwent(struct nwrap_backend *b)
1411 return NULL;
1414 static int nwrap_module_getpwent_r(struct nwrap_backend *b,
1415 struct passwd *pwdst, char *buf,
1416 size_t buflen, struct passwd **pwdstp)
1418 return ENOENT;
1421 static void nwrap_module_endpwent(struct nwrap_backend *b)
1425 static int nwrap_module_initgroups(struct nwrap_backend *b,
1426 const char *user, gid_t group)
1428 return -1;
1431 static struct group *nwrap_module_getgrnam(struct nwrap_backend *b,
1432 const char *name)
1434 return NULL;
1437 static int nwrap_module_getgrnam_r(struct nwrap_backend *b,
1438 const char *name, struct group *grdst,
1439 char *buf, size_t buflen, struct group **grdstp)
1441 return ENOENT;
1444 static struct group *nwrap_module_getgrgid(struct nwrap_backend *b,
1445 gid_t gid)
1447 return NULL;
1450 static int nwrap_module_getgrgid_r(struct nwrap_backend *b,
1451 gid_t gid, struct group *grdst,
1452 char *buf, size_t buflen, struct group **grdstp)
1454 return ENOENT;
1457 static void nwrap_module_setgrent(struct nwrap_backend *b)
1461 static struct group *nwrap_module_getgrent(struct nwrap_backend *b)
1463 return NULL;
1466 static int nwrap_module_getgrent_r(struct nwrap_backend *b,
1467 struct group *grdst, char *buf,
1468 size_t buflen, struct group **grdstp)
1470 return 0;
1473 static void nwrap_module_endgrent(struct nwrap_backend *b)
1478 * PUBLIC interface
1481 _PUBLIC_ struct passwd *nwrap_getpwnam(const char *name)
1483 int i;
1484 struct passwd *pwd;
1486 if (!nwrap_enabled()) {
1487 return real_getpwnam(name);
1490 for (i=0; i < nwrap_main_global->num_backends; i++) {
1491 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1492 pwd = b->ops->nw_getpwnam(b, name);
1493 if (pwd) {
1494 return pwd;
1498 return NULL;
1501 _PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
1502 char *buf, size_t buflen, struct passwd **pwdstp)
1504 int i,ret;
1506 if (!nwrap_enabled()) {
1507 return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
1510 for (i=0; i < nwrap_main_global->num_backends; i++) {
1511 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1512 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
1513 if (ret == ENOENT) {
1514 continue;
1516 return ret;
1519 return ENOENT;
1522 _PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid)
1524 int i;
1525 struct passwd *pwd;
1527 if (!nwrap_enabled()) {
1528 return real_getpwuid(uid);
1531 for (i=0; i < nwrap_main_global->num_backends; i++) {
1532 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1533 pwd = b->ops->nw_getpwuid(b, uid);
1534 if (pwd) {
1535 return pwd;
1539 return NULL;
1542 _PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
1543 char *buf, size_t buflen, struct passwd **pwdstp)
1545 int i,ret;
1547 if (!nwrap_enabled()) {
1548 return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
1551 for (i=0; i < nwrap_main_global->num_backends; i++) {
1552 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1553 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
1554 if (ret == ENOENT) {
1555 continue;
1557 return ret;
1560 return ENOENT;
1563 _PUBLIC_ void nwrap_setpwent(void)
1565 int i;
1567 if (!nwrap_enabled()) {
1568 real_setpwent();
1569 return;
1572 for (i=0; i < nwrap_main_global->num_backends; i++) {
1573 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1574 b->ops->nw_setpwent(b);
1578 _PUBLIC_ struct passwd *nwrap_getpwent(void)
1580 int i;
1581 struct passwd *pwd;
1583 if (!nwrap_enabled()) {
1584 return real_getpwent();
1587 for (i=0; i < nwrap_main_global->num_backends; i++) {
1588 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1589 pwd = b->ops->nw_getpwent(b);
1590 if (pwd) {
1591 return pwd;
1595 return NULL;
1598 _PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
1599 size_t buflen, struct passwd **pwdstp)
1601 int i,ret;
1603 if (!nwrap_enabled()) {
1604 #ifdef SOLARIS_GETPWENT_R
1605 struct passwd *pw;
1606 pw = real_getpwent_r(pwdst, buf, buflen);
1607 if (!pw) {
1608 if (errno == 0) {
1609 return ENOENT;
1611 return errno;
1613 if (pwdstp) {
1614 *pwdstp = pw;
1616 return 0;
1617 #else
1618 return real_getpwent_r(pwdst, buf, buflen, pwdstp);
1619 #endif
1622 for (i=0; i < nwrap_main_global->num_backends; i++) {
1623 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1624 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
1625 if (ret == ENOENT) {
1626 continue;
1628 return ret;
1631 return ENOENT;
1634 _PUBLIC_ void nwrap_endpwent(void)
1636 int i;
1638 if (!nwrap_enabled()) {
1639 real_endpwent();
1640 return;
1643 for (i=0; i < nwrap_main_global->num_backends; i++) {
1644 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1645 b->ops->nw_endpwent(b);
1649 _PUBLIC_ int nwrap_initgroups(const char *user, gid_t group)
1651 int i;
1653 if (!nwrap_enabled()) {
1654 return real_initgroups(user, group);
1657 for (i=0; i < nwrap_main_global->num_backends; i++) {
1658 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1659 return b->ops->nw_initgroups(b, user, group);
1662 errno = ENOENT;
1663 return -1;
1666 _PUBLIC_ struct group *nwrap_getgrnam(const char *name)
1668 int i;
1669 struct group *grp;
1671 if (!nwrap_enabled()) {
1672 return real_getgrnam(name);
1675 for (i=0; i < nwrap_main_global->num_backends; i++) {
1676 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1677 grp = b->ops->nw_getgrnam(b, name);
1678 if (grp) {
1679 return grp;
1683 return NULL;
1686 _PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst,
1687 char *buf, size_t buflen, struct group **grdstp)
1689 int i,ret;
1691 if (!nwrap_enabled()) {
1692 return real_getgrnam_r(name, grdst, buf, buflen, grdstp);
1695 for (i=0; i < nwrap_main_global->num_backends; i++) {
1696 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1697 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
1698 if (ret == ENOENT) {
1699 continue;
1701 return ret;
1704 return ENOENT;
1707 _PUBLIC_ struct group *nwrap_getgrgid(gid_t gid)
1709 int i;
1710 struct group *grp;
1712 if (!nwrap_enabled()) {
1713 return real_getgrgid(gid);
1716 for (i=0; i < nwrap_main_global->num_backends; i++) {
1717 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1718 grp = b->ops->nw_getgrgid(b, gid);
1719 if (grp) {
1720 return grp;
1724 return NULL;
1727 _PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
1728 char *buf, size_t buflen, struct group **grdstp)
1730 int i,ret;
1732 if (!nwrap_enabled()) {
1733 return real_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1736 for (i=0; i < nwrap_main_global->num_backends; i++) {
1737 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1738 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
1739 if (ret == ENOENT) {
1740 continue;
1742 return ret;
1745 return ENOENT;
1748 _PUBLIC_ void nwrap_setgrent(void)
1750 int i;
1752 if (!nwrap_enabled()) {
1753 real_setgrent();
1754 return;
1757 for (i=0; i < nwrap_main_global->num_backends; i++) {
1758 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1759 b->ops->nw_setgrent(b);
1763 _PUBLIC_ struct group *nwrap_getgrent(void)
1765 int i;
1766 struct group *grp;
1768 if (!nwrap_enabled()) {
1769 return real_getgrent();
1772 for (i=0; i < nwrap_main_global->num_backends; i++) {
1773 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1774 grp = b->ops->nw_getgrent(b);
1775 if (grp) {
1776 return grp;
1780 return NULL;
1783 _PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf,
1784 size_t buflen, struct group **grdstp)
1786 int i,ret;
1788 if (!nwrap_enabled()) {
1789 #ifdef SOLARIS_GETGRENT_R
1790 struct group *gr;
1791 gr = real_getgrent_r(grdst, buf, buflen);
1792 if (!gr) {
1793 if (errno == 0) {
1794 return ENOENT;
1796 return errno;
1798 if (grdstp) {
1799 *grdstp = gr;
1801 return 0;
1802 #else
1803 return real_getgrent_r(grdst, buf, buflen, grdstp);
1804 #endif
1807 for (i=0; i < nwrap_main_global->num_backends; i++) {
1808 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1809 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
1810 if (ret == ENOENT) {
1811 continue;
1813 return ret;
1816 return ENOENT;
1819 _PUBLIC_ void nwrap_endgrent(void)
1821 int i;
1823 if (!nwrap_enabled()) {
1824 real_endgrent();
1825 return;
1828 for (i=0; i < nwrap_main_global->num_backends; i++) {
1829 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1830 b->ops->nw_endgrent(b);
1834 _PUBLIC_ int nwrap_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1836 struct group *grp;
1837 gid_t *groups_tmp;
1838 int count = 1;
1839 const char *name_of_group = NULL;
1841 if (!nwrap_enabled()) {
1842 return real_getgrouplist(user, group, groups, ngroups);
1845 NWRAP_DEBUG(("%s: getgrouplist called for %s\n", __location__, user));
1847 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
1848 if (!groups_tmp) {
1849 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1850 errno = ENOMEM;
1851 return -1;
1854 memcpy(groups_tmp, &group, sizeof(gid_t));
1856 grp = nwrap_getgrgid(group);
1857 if (grp) {
1858 name_of_group = grp->gr_name;
1861 nwrap_setgrent();
1862 while ((grp = nwrap_getgrent()) != NULL) {
1863 int i = 0;
1865 NWRAP_VERBOSE(("%s: inspecting %s for group membership\n",
1866 __location__, grp->gr_name));
1868 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
1870 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
1871 (strcmp(name_of_group, grp->gr_name) != 0)) {
1873 NWRAP_DEBUG(("%s: %s is member of %s\n",
1874 __location__, user, grp->gr_name));
1876 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
1877 if (!groups_tmp) {
1878 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1879 errno = ENOMEM;
1880 return -1;
1883 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
1884 count++;
1889 nwrap_endgrent();
1891 NWRAP_VERBOSE(("%s: %s is member of %d groups: %d\n",
1892 __location__, user, *ngroups));
1894 if (*ngroups < count) {
1895 *ngroups = count;
1896 free(groups_tmp);
1897 return -1;
1900 *ngroups = count;
1901 memcpy(groups, groups_tmp, count * sizeof(gid_t));
1902 free(groups_tmp);
1904 return count;