2 * Copyright (C) Stefan Metzmacher 2007 <metze@samba.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 #define NSS_WRAPPER_NOT_REPLACE
37 #include "../replace/replace.h"
38 #include "system/passwd.h"
39 #include "system/filesys.h"
41 #else /* _SAMBA_BUILD_ */
43 #error nss_wrapper_only_supported_in_samba_yet
51 /* not all systems have _r functions... */
52 #ifndef HAVE_GETPWNAM_R
53 #define getpwnam_r(name, pwdst, buf, buflen, pwdstp) ENOSYS
55 #ifndef HAVE_GETPWUID_R
56 #define getpwuid_r(uid, pwdst, buf, buflen, pwdstp) ENOSYS
58 #ifndef HAVE_GETPWENT_R
59 #define getpwent_r(pwdst, buf, buflen, pwdstp) ENOSYS
61 #ifndef HAVE_GETGRNAM_R
62 #define getgrnam_r(name, grdst, buf, buflen, grdstp) ENOSYS
64 #ifndef HAVE_GETGRGID_R
65 #define getgrgid_r(gid, grdst, buf, buflen, grdstp) ENOSYS
67 #ifndef HAVE_GETGRENT_R
68 #define getgrent_r(grdst, buf, buflen, grdstp) ENOSYS
71 /* not all systems have getgrouplist */
72 #ifndef HAVE_GETGROUPLIST
73 #define getgrouplist(user, group, groups, ngroups) 0
76 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
82 #define real_getpwnam getpwnam
83 #define real_getpwnam_r getpwnam_r
84 #define real_getpwuid getpwuid
85 #define real_getpwuid_r getpwuid_r
87 #define real_setpwent setpwent
88 #define real_getpwent getpwent
89 #define real_getpwent_r getpwent_r
90 #define real_endpwent endpwent
93 #define real_getgrlst getgrlst
94 #define real_getgrlst_r getgrlst_r
95 #define real_initgroups_dyn initgroups_dyn
97 #define real_initgroups initgroups
98 #define real_getgrouplist getgrouplist
100 #define real_getgrnam getgrnam
101 #define real_getgrnam_r getgrnam_r
102 #define real_getgrgid getgrgid
103 #define real_getgrgid_r getgrgid_r
105 #define real_setgrent setgrent
106 #define real_getgrent getgrent
107 #define real_getgrent_r getgrent_r
108 #define real_endgrent endgrent
114 # define NWRAP_ERROR(args) DEBUG(0, args)
116 # define NWRAP_ERROR(args) printf args
119 #define NWRAP_ERROR(args)
124 # define NWRAP_DEBUG(args) DEBUG(0, args)
126 # define NWRAP_DEBUG(args) printf args
129 #define NWRAP_DEBUG(args)
134 # define NWRAP_VERBOSE(args) DEBUG(0, args)
136 # define NWRAP_VERBOSE(args) printf args
139 #define NWRAP_VERBOSE(args)
142 struct nwrap_backend {
144 struct nwrap_ops *ops;
148 struct passwd * (*nw_getpwnam)(struct nwrap_backend *b,
150 int (*nw_getpwnam_r)(struct nwrap_backend *b,
151 const char *name, struct passwd *pwdst,
152 char *buf, size_t buflen, struct passwd **pwdstp);
153 struct passwd * (*nw_getpwuid)(struct nwrap_backend *b,
155 int (*nw_getpwuid_r)(struct nwrap_backend *b,
156 uid_t uid, struct passwd *pwdst,
157 char *buf, size_t buflen, struct passwd **pwdstp);
158 void (*nw_setpwent)(struct nwrap_backend *b);
159 struct passwd * (*nw_getpwent)(struct nwrap_backend *b);
160 int (*nw_getpwent_r)(struct nwrap_backend *b,
161 struct passwd *pwdst, char *buf,
162 size_t buflen, struct passwd **pwdstp);
163 void (*nw_endpwent)(struct nwrap_backend *b);
164 int (*nw_initgroups)(struct nwrap_backend *b,
165 const char *user, gid_t group);
166 struct group * (*nw_getgrnam)(struct nwrap_backend *b,
168 int (*nw_getgrnam_r)(struct nwrap_backend *b,
169 const char *name, struct group *grdst,
170 char *buf, size_t buflen, struct group **grdstp);
171 struct group * (*nw_getgrgid)(struct nwrap_backend *b,
173 int (*nw_getgrgid_r)(struct nwrap_backend *b,
174 gid_t gid, struct group *grdst,
175 char *buf, size_t buflen, struct group **grdstp);
176 void (*nw_setgrent)(struct nwrap_backend *b);
177 struct group * (*nw_getgrent)(struct nwrap_backend *b);
178 int (*nw_getgrent_r)(struct nwrap_backend *b,
179 struct group *grdst, char *buf,
180 size_t buflen, struct group **grdstp);
181 void (*nw_endgrent)(struct nwrap_backend *b);
184 /* protoypes for files backend */
187 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
189 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
190 const char *name, struct passwd *pwdst,
191 char *buf, size_t buflen, struct passwd **pwdstp);
192 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
194 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
195 uid_t uid, struct passwd *pwdst,
196 char *buf, size_t buflen, struct passwd **pwdstp);
197 static void nwrap_files_setpwent(struct nwrap_backend *b);
198 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b);
199 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
200 struct passwd *pwdst, char *buf,
201 size_t buflen, struct passwd **pwdstp);
202 static void nwrap_files_endpwent(struct nwrap_backend *b);
203 static int nwrap_files_initgroups(struct nwrap_backend *b,
204 const char *user, gid_t group);
205 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
207 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
208 const char *name, struct group *grdst,
209 char *buf, size_t buflen, struct group **grdstp);
210 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
212 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
213 gid_t gid, struct group *grdst,
214 char *buf, size_t buflen, struct group **grdstp);
215 static void nwrap_files_setgrent(struct nwrap_backend *b);
216 static struct group *nwrap_files_getgrent(struct nwrap_backend *b);
217 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
218 struct group *grdst, char *buf,
219 size_t buflen, struct group **grdstp);
220 static void nwrap_files_endgrent(struct nwrap_backend *b);
222 struct nwrap_ops nwrap_files_ops = {
223 .nw_getpwnam = nwrap_files_getpwnam,
224 .nw_getpwnam_r = nwrap_files_getpwnam_r,
225 .nw_getpwuid = nwrap_files_getpwuid,
226 .nw_getpwuid_r = nwrap_files_getpwuid_r,
227 .nw_setpwent = nwrap_files_setpwent,
228 .nw_getpwent = nwrap_files_getpwent,
229 .nw_getpwent_r = nwrap_files_getpwent_r,
230 .nw_endpwent = nwrap_files_endpwent,
231 .nw_initgroups = nwrap_files_initgroups,
232 .nw_getgrnam = nwrap_files_getgrnam,
233 .nw_getgrnam_r = nwrap_files_getgrnam_r,
234 .nw_getgrgid = nwrap_files_getgrgid,
235 .nw_getgrgid_r = nwrap_files_getgrgid_r,
236 .nw_setgrent = nwrap_files_setgrent,
237 .nw_getgrent = nwrap_files_getgrent,
238 .nw_getgrent_r = nwrap_files_getgrent_r,
239 .nw_endgrent = nwrap_files_endgrent,
243 const char *nwrap_switch;
245 struct nwrap_backend *backends;
248 struct nwrap_main *nwrap_main_global;
249 struct nwrap_main __nwrap_main_global;
257 bool (*parse_line)(struct nwrap_cache *, char *line);
258 void (*unload)(struct nwrap_cache *);
262 struct nwrap_cache *cache;
269 struct nwrap_cache __nwrap_cache_pw;
270 struct nwrap_pw nwrap_pw_global;
272 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
273 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
276 struct nwrap_cache *cache;
283 struct nwrap_cache __nwrap_cache_gr;
284 struct nwrap_gr nwrap_gr_global;
286 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
287 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
289 static bool nwrap_module_init(const char *name,
290 struct nwrap_ops *ops,
292 struct nwrap_backend **backends)
294 *backends = (struct nwrap_backend *)realloc(*backends,
295 sizeof(struct nwrap_backend) * ((*num_backends) + 1));
297 NWRAP_ERROR(("%s: out of memory\n",
301 (*backends)[*num_backends].name = name;
302 (*backends)[*num_backends].ops = ops;
309 static void nwrap_backend_init(struct nwrap_main *r)
314 if (!nwrap_module_init("files", &nwrap_files_ops,
317 NWRAP_ERROR(("%s: failed to initialize 'files' backend\n",
323 static void nwrap_init(void)
325 static bool initialized;
327 if (initialized) return;
330 nwrap_main_global = &__nwrap_main_global;
332 nwrap_backend_init(nwrap_main_global);
334 nwrap_pw_global.cache = &__nwrap_cache_pw;
336 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
337 nwrap_pw_global.cache->fd = -1;
338 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
339 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
340 nwrap_pw_global.cache->unload = nwrap_pw_unload;
342 nwrap_gr_global.cache = &__nwrap_cache_gr;
344 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
345 nwrap_gr_global.cache->fd = -1;
346 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
347 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
348 nwrap_gr_global.cache->unload = nwrap_gr_unload;
351 static bool nwrap_enabled(void)
355 if (!nwrap_pw_global.cache->path) {
358 if (nwrap_pw_global.cache->path[0] == '\0') {
361 if (!nwrap_gr_global.cache->path) {
364 if (nwrap_gr_global.cache->path[0] == '\0') {
371 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
377 if (nwrap->st.st_size == 0) {
378 NWRAP_DEBUG(("%s: size == 0\n",
383 if (nwrap->st.st_size > INT32_MAX) {
384 NWRAP_ERROR(("%s: size[%u] larger than INT32_MAX\n",
385 __location__, (unsigned)nwrap->st.st_size));
389 ret = lseek(nwrap->fd, 0, SEEK_SET);
391 NWRAP_ERROR(("%s: lseek - %d\n",__location__,ret));
395 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
397 NWRAP_ERROR(("%s: malloc failed\n",__location__));
401 ret = read(nwrap->fd, buf, nwrap->st.st_size);
402 if (ret != nwrap->st.st_size) {
403 NWRAP_ERROR(("%s: read(%u) gave %d\n",
404 __location__, (unsigned)nwrap->st.st_size, ret));
408 buf[nwrap->st.st_size] = '\0';
411 while (nline && nline[0]) {
419 e = strchr(line, '\n');
430 NWRAP_VERBOSE(("%s:'%s'\n",__location__, line));
432 if (strlen(line) == 0) {
436 ok = nwrap->parse_line(nwrap, line);
451 static void nwrap_cache_unload(struct nwrap_cache *nwrap)
453 nwrap->unload(nwrap);
455 if (nwrap->buf) free(nwrap->buf);
460 static void nwrap_cache_reload(struct nwrap_cache *nwrap)
465 bool retried = false;
469 nwrap->fd = open(nwrap->path, O_RDONLY);
471 NWRAP_ERROR(("%s: unable to open '%s' readonly %d:%s\n",
473 nwrap->path, nwrap->fd,
477 NWRAP_VERBOSE(("%s: open '%s'\n", __location__, nwrap->path));
480 ret = fstat(nwrap->fd, &st);
482 NWRAP_ERROR(("%s: fstat(%s) - %d:%s\n",
485 ret, strerror(errno)));
489 if (retried == false && st.st_nlink == 0) {
490 /* maybe someone has replaced the file... */
491 NWRAP_DEBUG(("%s: st_nlink == 0, reopen %s\n",
492 __location__, nwrap->path));
494 memset(&nwrap->st, 0, sizeof(nwrap->st));
500 if (st.st_mtime == nwrap->st.st_mtime) {
501 NWRAP_VERBOSE(("%s: st_mtime[%u] hasn't changed, skip reload\n",
502 __location__, (unsigned)st.st_mtime));
505 NWRAP_DEBUG(("%s: st_mtime has changed [%u] => [%u], start reload\n",
506 __location__, (unsigned)st.st_mtime,
507 (unsigned)nwrap->st.st_mtime));
511 nwrap_cache_unload(nwrap);
513 ok = nwrap_parse_file(nwrap);
515 NWRAP_ERROR(("%s: failed to reload %s\n",
516 __location__, nwrap->path));
517 nwrap_cache_unload(nwrap);
519 NWRAP_DEBUG(("%s: reloaded %s\n",
520 __location__, nwrap->path));
524 * the caller has to call nwrap_unload() on failure
526 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
528 struct nwrap_pw *nwrap_pw;
535 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
537 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
538 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
540 NWRAP_ERROR(("%s:realloc(%u) failed\n",
541 __location__, list_size));
546 pw = &nwrap_pw->list[nwrap_pw->num];
553 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
554 __location__, line, c));
562 NWRAP_VERBOSE(("name[%s]\n", pw->pw_name));
567 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
568 __location__, line, c));
576 NWRAP_VERBOSE(("password[%s]\n", pw->pw_passwd));
581 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
582 __location__, line, c));
588 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
590 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
591 __location__, line, c, strerror(errno)));
595 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
596 __location__, line, c, strerror(errno)));
600 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
601 __location__, line, c, strerror(errno)));
606 NWRAP_VERBOSE(("uid[%u]\n", pw->pw_uid));
611 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
612 __location__, line, c));
618 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
620 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
621 __location__, line, c, strerror(errno)));
625 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
626 __location__, line, c, strerror(errno)));
630 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
631 __location__, line, c, strerror(errno)));
636 NWRAP_VERBOSE(("gid[%u]\n", pw->pw_gid));
641 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
642 __location__, line, c));
650 NWRAP_VERBOSE(("gecos[%s]\n", pw->pw_gecos));
655 NWRAP_ERROR(("%s:'%s'\n",__location__,c));
663 NWRAP_VERBOSE(("dir[%s]\n", pw->pw_dir));
667 NWRAP_VERBOSE(("shell[%s]\n", pw->pw_shell));
669 NWRAP_DEBUG(("add user[%s:%s:%u:%u:%s:%s:%s]\n",
670 pw->pw_name, pw->pw_passwd,
671 pw->pw_uid, pw->pw_gid,
672 pw->pw_gecos, pw->pw_dir, pw->pw_shell));
678 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
680 struct nwrap_pw *nwrap_pw;
681 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
683 if (nwrap_pw->list) free(nwrap_pw->list);
685 nwrap_pw->list = NULL;
690 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
691 char *buf, size_t buflen, struct passwd **dstp)
697 first = src->pw_name;
699 last = src->pw_shell;
700 while (*last) last++;
702 ofs = PTR_DIFF(last + 1, first);
708 memcpy(buf, first, ofs);
710 ofs = PTR_DIFF(src->pw_name, first);
711 dst->pw_name = buf + ofs;
712 ofs = PTR_DIFF(src->pw_passwd, first);
713 dst->pw_passwd = buf + ofs;
714 dst->pw_uid = src->pw_uid;
715 dst->pw_gid = src->pw_gid;
716 ofs = PTR_DIFF(src->pw_gecos, first);
717 dst->pw_gecos = buf + ofs;
718 ofs = PTR_DIFF(src->pw_dir, first);
719 dst->pw_dir = buf + ofs;
720 ofs = PTR_DIFF(src->pw_shell, first);
721 dst->pw_shell = buf + ofs;
731 * the caller has to call nwrap_unload() on failure
733 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
735 struct nwrap_gr *nwrap_gr;
743 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
745 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
746 gr = (struct group *)realloc(nwrap_gr->list, list_size);
748 NWRAP_ERROR(("%s:realloc failed\n",__location__));
753 gr = &nwrap_gr->list[nwrap_gr->num];
760 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
761 __location__, line, c));
769 NWRAP_VERBOSE(("name[%s]\n", gr->gr_name));
774 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
775 __location__, line, c));
783 NWRAP_VERBOSE(("password[%s]\n", gr->gr_passwd));
788 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
789 __location__, line, c));
795 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
797 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
798 __location__, line, c, strerror(errno)));
802 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
803 __location__, line, c, strerror(errno)));
807 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
808 __location__, line, c, strerror(errno)));
813 NWRAP_VERBOSE(("gid[%u]\n", gr->gr_gid));
816 gr->gr_mem = (char **)malloc(sizeof(char *));
818 NWRAP_ERROR(("%s:calloc failed\n",__location__));
821 gr->gr_mem[0] = NULL;
823 for(nummem=0; p; nummem++) {
833 if (strlen(c) == 0) {
837 m_size = sizeof(char *) * (nummem+2);
838 m = (char **)realloc(gr->gr_mem, m_size);
840 NWRAP_ERROR(("%s:realloc(%u) failed\n",
841 __location__, m_size));
845 gr->gr_mem[nummem] = c;
846 gr->gr_mem[nummem+1] = NULL;
848 NWRAP_VERBOSE(("member[%u]: '%s'\n", nummem, gr->gr_mem[nummem]));
851 NWRAP_DEBUG(("add group[%s:%s:%u:] with %u members\n",
852 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem));
858 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
861 struct nwrap_gr *nwrap_gr;
862 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
864 if (nwrap_gr->list) {
865 for (i=0; i < nwrap_gr->num; i++) {
866 if (nwrap_gr->list[i].gr_mem) {
867 free(nwrap_gr->list[i].gr_mem);
870 free(nwrap_gr->list);
873 nwrap_gr->list = NULL;
878 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
879 char *buf, size_t buflen, struct group **dstp)
889 first = src->gr_name;
898 last = src->gr_passwd;
900 while (*last) last++;
902 ofsb = PTR_DIFF(last + 1, first);
903 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
905 if ((ofsb + ofsm) > buflen) {
909 memcpy(buf, first, ofsb);
910 memcpy(buf + ofsb, src->gr_mem, ofsm);
912 ofs = PTR_DIFF(src->gr_name, first);
913 dst->gr_name = buf + ofs;
914 ofs = PTR_DIFF(src->gr_passwd, first);
915 dst->gr_passwd = buf + ofs;
916 dst->gr_gid = src->gr_gid;
918 dst->gr_mem = (char **)(buf + ofsb);
919 for (i=0; src->gr_mem[i]; i++) {
920 ofs = PTR_DIFF(src->gr_mem[i], first);
921 dst->gr_mem[i] = buf + ofs;
932 static struct passwd *nwrap_files_getpwnam(struct nwrap_backend *b,
937 nwrap_cache_reload(nwrap_pw_global.cache);
939 for (i=0; i<nwrap_pw_global.num; i++) {
940 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
941 NWRAP_DEBUG(("%s: user[%s] found\n",
942 __location__, name));
943 return &nwrap_pw_global.list[i];
945 NWRAP_VERBOSE(("%s: user[%s] does not match [%s]\n",
947 nwrap_pw_global.list[i].pw_name));
950 NWRAP_DEBUG(("%s: user[%s] not found\n", __location__, name));
956 static int nwrap_files_getpwnam_r(struct nwrap_backend *b,
957 const char *name, struct passwd *pwdst,
958 char *buf, size_t buflen, struct passwd **pwdstp)
962 pw = nwrap_files_getpwnam(b, name);
970 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
973 static struct passwd *nwrap_files_getpwuid(struct nwrap_backend *b,
978 nwrap_cache_reload(nwrap_pw_global.cache);
980 for (i=0; i<nwrap_pw_global.num; i++) {
981 if (nwrap_pw_global.list[i].pw_uid == uid) {
982 NWRAP_DEBUG(("%s: uid[%u] found\n",
984 return &nwrap_pw_global.list[i];
986 NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n",
988 nwrap_pw_global.list[i].pw_uid));
991 NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid));
997 static int nwrap_files_getpwuid_r(struct nwrap_backend *b,
998 uid_t uid, struct passwd *pwdst,
999 char *buf, size_t buflen, struct passwd **pwdstp)
1003 pw = nwrap_files_getpwuid(b, uid);
1011 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1014 /* user enum functions */
1015 static void nwrap_files_setpwent(struct nwrap_backend *b)
1017 nwrap_pw_global.idx = 0;
1020 static struct passwd *nwrap_files_getpwent(struct nwrap_backend *b)
1024 if (nwrap_pw_global.idx == 0) {
1025 nwrap_cache_reload(nwrap_pw_global.cache);
1028 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
1033 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
1035 NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n",
1036 __location__, pw->pw_name, pw->pw_uid));
1041 static int nwrap_files_getpwent_r(struct nwrap_backend *b,
1042 struct passwd *pwdst, char *buf,
1043 size_t buflen, struct passwd **pwdstp)
1047 pw = nwrap_files_getpwent(b);
1055 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1058 static void nwrap_files_endpwent(struct nwrap_backend *b)
1060 nwrap_pw_global.idx = 0;
1063 /* misc functions */
1064 static int nwrap_files_initgroups(struct nwrap_backend *b,
1065 const char *user, gid_t group)
1067 /* TODO: maybe we should also fake this... */
1071 /* group functions */
1072 static struct group *nwrap_files_getgrnam(struct nwrap_backend *b,
1077 nwrap_cache_reload(nwrap_gr_global.cache);
1079 for (i=0; i<nwrap_gr_global.num; i++) {
1080 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
1081 NWRAP_DEBUG(("%s: group[%s] found\n",
1082 __location__, name));
1083 return &nwrap_gr_global.list[i];
1085 NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n",
1087 nwrap_gr_global.list[i].gr_name));
1090 NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name));
1096 static int nwrap_files_getgrnam_r(struct nwrap_backend *b,
1097 const char *name, struct group *grdst,
1098 char *buf, size_t buflen, struct group **grdstp)
1102 gr = nwrap_files_getgrnam(b, name);
1110 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1113 static struct group *nwrap_files_getgrgid(struct nwrap_backend *b,
1118 nwrap_cache_reload(nwrap_gr_global.cache);
1120 for (i=0; i<nwrap_gr_global.num; i++) {
1121 if (nwrap_gr_global.list[i].gr_gid == gid) {
1122 NWRAP_DEBUG(("%s: gid[%u] found\n",
1123 __location__, gid));
1124 return &nwrap_gr_global.list[i];
1126 NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n",
1128 nwrap_gr_global.list[i].gr_gid));
1131 NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid));
1137 static int nwrap_files_getgrgid_r(struct nwrap_backend *b,
1138 gid_t gid, struct group *grdst,
1139 char *buf, size_t buflen, struct group **grdstp)
1143 gr = nwrap_files_getgrgid(b, gid);
1151 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1154 /* group enum functions */
1155 static void nwrap_files_setgrent(struct nwrap_backend *b)
1157 nwrap_gr_global.idx = 0;
1160 static struct group *nwrap_files_getgrent(struct nwrap_backend *b)
1164 if (nwrap_gr_global.idx == 0) {
1165 nwrap_cache_reload(nwrap_gr_global.cache);
1168 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
1173 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
1175 NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n",
1176 __location__, gr->gr_name, gr->gr_gid));
1181 static int nwrap_files_getgrent_r(struct nwrap_backend *b,
1182 struct group *grdst, char *buf,
1183 size_t buflen, struct group **grdstp)
1187 gr = nwrap_files_getgrent(b);
1195 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1198 static void nwrap_files_endgrent(struct nwrap_backend *b)
1200 nwrap_gr_global.idx = 0;
1207 _PUBLIC_ struct passwd *nwrap_getpwnam(const char *name)
1212 if (!nwrap_enabled()) {
1213 return real_getpwnam(name);
1216 for (i=0; i < nwrap_main_global->num_backends; i++) {
1217 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1218 pwd = b->ops->nw_getpwnam(b, name);
1227 _PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
1228 char *buf, size_t buflen, struct passwd **pwdstp)
1232 if (!nwrap_enabled()) {
1233 return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
1236 for (i=0; i < nwrap_main_global->num_backends; i++) {
1237 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1238 ret = b->ops->nw_getpwnam_r(b, name, pwdst, buf, buflen, pwdstp);
1239 if (ret == ENOENT) {
1248 _PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid)
1253 if (!nwrap_enabled()) {
1254 return real_getpwuid(uid);
1257 for (i=0; i < nwrap_main_global->num_backends; i++) {
1258 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1259 pwd = b->ops->nw_getpwuid(b, uid);
1268 _PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
1269 char *buf, size_t buflen, struct passwd **pwdstp)
1273 if (!nwrap_enabled()) {
1274 return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
1277 for (i=0; i < nwrap_main_global->num_backends; i++) {
1278 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1279 ret = b->ops->nw_getpwuid_r(b, uid, pwdst, buf, buflen, pwdstp);
1280 if (ret == ENOENT) {
1289 _PUBLIC_ void nwrap_setpwent(void)
1293 if (!nwrap_enabled()) {
1298 for (i=0; i < nwrap_main_global->num_backends; i++) {
1299 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1300 b->ops->nw_setpwent(b);
1304 _PUBLIC_ struct passwd *nwrap_getpwent(void)
1309 if (!nwrap_enabled()) {
1310 return real_getpwent();
1313 for (i=0; i < nwrap_main_global->num_backends; i++) {
1314 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1315 pwd = b->ops->nw_getpwent(b);
1324 _PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
1325 size_t buflen, struct passwd **pwdstp)
1329 if (!nwrap_enabled()) {
1330 #ifdef SOLARIS_GETPWENT_R
1332 pw = real_getpwent_r(pwdst, buf, buflen);
1344 return real_getpwent_r(pwdst, buf, buflen, pwdstp);
1348 for (i=0; i < nwrap_main_global->num_backends; i++) {
1349 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1350 ret = b->ops->nw_getpwent_r(b, pwdst, buf, buflen, pwdstp);
1351 if (ret == ENOENT) {
1360 _PUBLIC_ void nwrap_endpwent(void)
1364 if (!nwrap_enabled()) {
1369 for (i=0; i < nwrap_main_global->num_backends; i++) {
1370 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1371 b->ops->nw_endpwent(b);
1375 _PUBLIC_ int nwrap_initgroups(const char *user, gid_t group)
1379 if (!nwrap_enabled()) {
1380 return real_initgroups(user, group);
1383 for (i=0; i < nwrap_main_global->num_backends; i++) {
1384 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1385 return b->ops->nw_initgroups(b, user, group);
1392 _PUBLIC_ struct group *nwrap_getgrnam(const char *name)
1397 if (!nwrap_enabled()) {
1398 return real_getgrnam(name);
1401 for (i=0; i < nwrap_main_global->num_backends; i++) {
1402 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1403 grp = b->ops->nw_getgrnam(b, name);
1412 _PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst,
1413 char *buf, size_t buflen, struct group **grdstp)
1417 if (!nwrap_enabled()) {
1418 return real_getgrnam_r(name, grdst, buf, buflen, grdstp);
1421 for (i=0; i < nwrap_main_global->num_backends; i++) {
1422 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1423 ret = b->ops->nw_getgrnam_r(b, name, grdst, buf, buflen, grdstp);
1424 if (ret == ENOENT) {
1433 _PUBLIC_ struct group *nwrap_getgrgid(gid_t gid)
1438 if (!nwrap_enabled()) {
1439 return real_getgrgid(gid);
1442 for (i=0; i < nwrap_main_global->num_backends; i++) {
1443 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1444 grp = b->ops->nw_getgrgid(b, gid);
1453 _PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
1454 char *buf, size_t buflen, struct group **grdstp)
1458 if (!nwrap_enabled()) {
1459 return real_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1462 for (i=0; i < nwrap_main_global->num_backends; i++) {
1463 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1464 ret = b->ops->nw_getgrgid_r(b, gid, grdst, buf, buflen, grdstp);
1465 if (ret == ENOENT) {
1474 _PUBLIC_ void nwrap_setgrent(void)
1478 if (!nwrap_enabled()) {
1483 for (i=0; i < nwrap_main_global->num_backends; i++) {
1484 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1485 b->ops->nw_setgrent(b);
1489 _PUBLIC_ struct group *nwrap_getgrent(void)
1494 if (!nwrap_enabled()) {
1495 return real_getgrent();
1498 for (i=0; i < nwrap_main_global->num_backends; i++) {
1499 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1500 grp = b->ops->nw_getgrent(b);
1509 _PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf,
1510 size_t buflen, struct group **grdstp)
1514 if (!nwrap_enabled()) {
1515 #ifdef SOLARIS_GETGRENT_R
1517 gr = real_getgrent_r(grdst, buf, buflen);
1529 return real_getgrent_r(grdst, buf, buflen, grdstp);
1533 for (i=0; i < nwrap_main_global->num_backends; i++) {
1534 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1535 ret = b->ops->nw_getgrent_r(b, grdst, buf, buflen, grdstp);
1536 if (ret == ENOENT) {
1545 _PUBLIC_ void nwrap_endgrent(void)
1549 if (!nwrap_enabled()) {
1554 for (i=0; i < nwrap_main_global->num_backends; i++) {
1555 struct nwrap_backend *b = &nwrap_main_global->backends[i];
1556 b->ops->nw_endgrent(b);
1560 _PUBLIC_ int nwrap_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1565 const char *name_of_group = NULL;
1567 if (!nwrap_enabled()) {
1568 return real_getgrouplist(user, group, groups, ngroups);
1571 NWRAP_DEBUG(("%s: getgrouplist called for %s\n", __location__, user));
1573 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
1575 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1580 memcpy(groups_tmp, &group, sizeof(gid_t));
1582 grp = nwrap_getgrgid(group);
1584 name_of_group = grp->gr_name;
1588 while ((grp = nwrap_getgrent()) != NULL) {
1591 NWRAP_VERBOSE(("%s: inspecting %s for group membership\n",
1592 __location__, grp->gr_name));
1594 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
1596 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
1597 (strcmp(name_of_group, grp->gr_name) != 0)) {
1599 NWRAP_DEBUG(("%s: %s is member of %s\n",
1600 __location__, user, grp->gr_name));
1602 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
1604 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1609 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
1617 NWRAP_VERBOSE(("%s: %s is member of %d groups: %d\n",
1618 __location__, user, *ngroups));
1620 if (*ngroups < count) {
1627 memcpy(groups, groups_tmp, count * sizeof(gid_t));