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)
144 struct passwd * (*nw_getpwnam)(const char *name);
145 int (*nw_getpwnam_r)(const char *name, struct passwd *pwdst,
146 char *buf, size_t buflen, struct passwd **pwdstp);
147 struct passwd * (*nw_getpwuid)(uid_t uid);
148 int (*nw_getpwuid_r)(uid_t uid, struct passwd *pwdst,
149 char *buf, size_t buflen, struct passwd **pwdstp);
150 void (*nw_setpwent)(void);
151 struct passwd * (*nw_getpwent)(void);
152 int (*nw_getpwent_r)(struct passwd *pwdst, char *buf,
153 size_t buflen, struct passwd **pwdstp);
154 void (*nw_endpwent)(void);
155 int (*nw_initgroups)(const char *user, gid_t group);
156 struct group * (*nw_getgrnam)(const char *name);
157 int (*nw_getgrnam_r)(const char *name, struct group *grdst,
158 char *buf, size_t buflen, struct group **grdstp);
159 struct group * (*nw_getgrgid)(gid_t gid);
160 int (*nw_getgrgid_r)(gid_t gid, struct group *grdst,
161 char *buf, size_t buflen, struct group **grdstp);
162 void (*nw_setgrent)(void);
163 struct group * (*nw_getgrent)(void);
164 int (*nw_getgrent_r)(struct group *grdst, char *buf,
165 size_t buflen, struct group **grdstp);
166 void (*nw_endgrent)(void);
167 int (*nw_getgrouplist)(const char *user, gid_t group, gid_t *groups, int *ngroups);
170 static struct passwd *nwrap_files_getpwnam(const char *name);
171 static int nwrap_files_getpwnam_r(const char *name, struct passwd *pwdst,
172 char *buf, size_t buflen, struct passwd **pwdstp);
173 static struct passwd *nwrap_files_getpwuid(uid_t uid);
174 static int nwrap_files_getpwuid_r(uid_t uid, struct passwd *pwdst,
175 char *buf, size_t buflen, struct passwd **pwdstp);
176 static void nwrap_files_setpwent(void);
177 static struct passwd *nwrap_files_getpwent(void);
178 static int nwrap_files_getpwent_r(struct passwd *pwdst, char *buf,
179 size_t buflen, struct passwd **pwdstp);
180 static void nwrap_files_endpwent(void);
181 static int nwrap_files_initgroups(const char *user, gid_t group);
182 static struct group *nwrap_files_getgrnam(const char *name);
183 static int nwrap_files_getgrnam_r(const char *name, struct group *grdst,
184 char *buf, size_t buflen, struct group **grdstp);
185 static struct group *nwrap_files_getgrgid(gid_t gid);
186 static int nwrap_files_getgrgid_r(gid_t gid, struct group *grdst,
187 char *buf, size_t buflen, struct group **grdstp);
188 static void nwrap_files_setgrent(void);
189 static struct group *nwrap_files_getgrent(void);
190 static int nwrap_files_getgrent_r(struct group *grdst, char *buf,
191 size_t buflen, struct group **grdstp);
192 static void nwrap_files_endgrent(void);
193 static int nwrap_files_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups);
195 struct nwrap_ops nwrap_files_ops = {
197 .nw_getpwnam = nwrap_files_getpwnam,
198 .nw_getpwnam_r = nwrap_files_getpwnam_r,
199 .nw_getpwuid = nwrap_files_getpwuid,
200 .nw_getpwuid_r = nwrap_files_getpwuid_r,
201 .nw_setpwent = nwrap_files_setpwent,
202 .nw_getpwent = nwrap_files_getpwent,
203 .nw_getpwent_r = nwrap_files_getpwent_r,
204 .nw_endpwent = nwrap_files_endpwent,
205 .nw_initgroups = nwrap_files_initgroups,
206 .nw_getgrnam = nwrap_files_getgrnam,
207 .nw_getgrnam_r = nwrap_files_getgrnam_r,
208 .nw_getgrgid = nwrap_files_getgrgid,
209 .nw_getgrgid_r = nwrap_files_getgrgid_r,
210 .nw_setgrent = nwrap_files_setgrent,
211 .nw_getgrent = nwrap_files_getgrent,
212 .nw_getgrent_r = nwrap_files_getgrent_r,
213 .nw_endgrent = nwrap_files_endgrent,
214 .nw_getgrouplist= nwrap_files_getgrouplist
218 struct nwrap_ops *ops;
221 struct nwrap_main *nwrap_main_global;
222 struct nwrap_main __nwrap_main_global;
230 bool (*parse_line)(struct nwrap_cache *, char *line);
231 void (*unload)(struct nwrap_cache *);
235 struct nwrap_cache *cache;
242 struct nwrap_cache __nwrap_cache_pw;
243 struct nwrap_pw nwrap_pw_global;
245 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
246 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
249 struct nwrap_cache *cache;
256 struct nwrap_cache __nwrap_cache_gr;
257 struct nwrap_gr nwrap_gr_global;
259 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
260 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
262 static void nwrap_init(void)
264 static bool initialized;
266 if (initialized) return;
269 nwrap_main_global = &__nwrap_main_global;
271 nwrap_main_global->ops = &nwrap_files_ops;
273 nwrap_pw_global.cache = &__nwrap_cache_pw;
275 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
276 nwrap_pw_global.cache->fd = -1;
277 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
278 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
279 nwrap_pw_global.cache->unload = nwrap_pw_unload;
281 nwrap_gr_global.cache = &__nwrap_cache_gr;
283 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
284 nwrap_gr_global.cache->fd = -1;
285 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
286 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
287 nwrap_gr_global.cache->unload = nwrap_gr_unload;
290 static bool nwrap_enabled(void)
294 if (!nwrap_pw_global.cache->path) {
297 if (nwrap_pw_global.cache->path[0] == '\0') {
300 if (!nwrap_gr_global.cache->path) {
303 if (nwrap_gr_global.cache->path[0] == '\0') {
310 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
316 if (nwrap->st.st_size == 0) {
317 NWRAP_DEBUG(("%s: size == 0\n",
322 if (nwrap->st.st_size > INT32_MAX) {
323 NWRAP_ERROR(("%s: size[%u] larger than INT32_MAX\n",
324 __location__, (unsigned)nwrap->st.st_size));
328 ret = lseek(nwrap->fd, 0, SEEK_SET);
330 NWRAP_ERROR(("%s: lseek - %d\n",__location__,ret));
334 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
336 NWRAP_ERROR(("%s: malloc failed\n",__location__));
340 ret = read(nwrap->fd, buf, nwrap->st.st_size);
341 if (ret != nwrap->st.st_size) {
342 NWRAP_ERROR(("%s: read(%u) gave %d\n",
343 __location__, (unsigned)nwrap->st.st_size, ret));
347 buf[nwrap->st.st_size] = '\0';
350 while (nline && nline[0]) {
358 e = strchr(line, '\n');
369 NWRAP_VERBOSE(("%s:'%s'\n",__location__, line));
371 if (strlen(line) == 0) {
375 ok = nwrap->parse_line(nwrap, line);
390 static void nwrap_cache_unload(struct nwrap_cache *nwrap)
392 nwrap->unload(nwrap);
394 if (nwrap->buf) free(nwrap->buf);
399 static void nwrap_cache_reload(struct nwrap_cache *nwrap)
404 bool retried = false;
408 nwrap->fd = open(nwrap->path, O_RDONLY);
410 NWRAP_ERROR(("%s: unable to open '%s' readonly %d:%s\n",
412 nwrap->path, nwrap->fd,
416 NWRAP_VERBOSE(("%s: open '%s'\n", __location__, nwrap->path));
419 ret = fstat(nwrap->fd, &st);
421 NWRAP_ERROR(("%s: fstat(%s) - %d:%s\n",
424 ret, strerror(errno)));
428 if (retried == false && st.st_nlink == 0) {
429 /* maybe someone has replaced the file... */
430 NWRAP_DEBUG(("%s: st_nlink == 0, reopen %s\n",
431 __location__, nwrap->path));
433 memset(&nwrap->st, 0, sizeof(nwrap->st));
439 if (st.st_mtime == nwrap->st.st_mtime) {
440 NWRAP_VERBOSE(("%s: st_mtime[%u] hasn't changed, skip reload\n",
441 __location__, (unsigned)st.st_mtime));
444 NWRAP_DEBUG(("%s: st_mtime has changed [%u] => [%u], start reload\n",
445 __location__, (unsigned)st.st_mtime,
446 (unsigned)nwrap->st.st_mtime));
450 nwrap_cache_unload(nwrap);
452 ok = nwrap_parse_file(nwrap);
454 NWRAP_ERROR(("%s: failed to reload %s\n",
455 __location__, nwrap->path));
456 nwrap_cache_unload(nwrap);
458 NWRAP_DEBUG(("%s: reloaded %s\n",
459 __location__, nwrap->path));
463 * the caller has to call nwrap_unload() on failure
465 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
467 struct nwrap_pw *nwrap_pw;
474 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
476 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
477 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
479 NWRAP_ERROR(("%s:realloc(%u) failed\n",
480 __location__, list_size));
485 pw = &nwrap_pw->list[nwrap_pw->num];
492 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
493 __location__, line, c));
501 NWRAP_VERBOSE(("name[%s]\n", pw->pw_name));
506 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
507 __location__, line, c));
515 NWRAP_VERBOSE(("password[%s]\n", pw->pw_passwd));
520 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
521 __location__, line, c));
527 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
529 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
530 __location__, line, c, strerror(errno)));
534 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
535 __location__, line, c, strerror(errno)));
539 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
540 __location__, line, c, strerror(errno)));
545 NWRAP_VERBOSE(("uid[%u]\n", pw->pw_uid));
550 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
551 __location__, line, c));
557 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
559 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
560 __location__, line, c, strerror(errno)));
564 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
565 __location__, line, c, strerror(errno)));
569 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
570 __location__, line, c, strerror(errno)));
575 NWRAP_VERBOSE(("gid[%u]\n", pw->pw_gid));
580 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
581 __location__, line, c));
589 NWRAP_VERBOSE(("gecos[%s]\n", pw->pw_gecos));
594 NWRAP_ERROR(("%s:'%s'\n",__location__,c));
602 NWRAP_VERBOSE(("dir[%s]\n", pw->pw_dir));
606 NWRAP_VERBOSE(("shell[%s]\n", pw->pw_shell));
608 NWRAP_DEBUG(("add user[%s:%s:%u:%u:%s:%s:%s]\n",
609 pw->pw_name, pw->pw_passwd,
610 pw->pw_uid, pw->pw_gid,
611 pw->pw_gecos, pw->pw_dir, pw->pw_shell));
617 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
619 struct nwrap_pw *nwrap_pw;
620 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
622 if (nwrap_pw->list) free(nwrap_pw->list);
624 nwrap_pw->list = NULL;
629 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
630 char *buf, size_t buflen, struct passwd **dstp)
636 first = src->pw_name;
638 last = src->pw_shell;
639 while (*last) last++;
641 ofs = PTR_DIFF(last + 1, first);
647 memcpy(buf, first, ofs);
649 ofs = PTR_DIFF(src->pw_name, first);
650 dst->pw_name = buf + ofs;
651 ofs = PTR_DIFF(src->pw_passwd, first);
652 dst->pw_passwd = buf + ofs;
653 dst->pw_uid = src->pw_uid;
654 dst->pw_gid = src->pw_gid;
655 ofs = PTR_DIFF(src->pw_gecos, first);
656 dst->pw_gecos = buf + ofs;
657 ofs = PTR_DIFF(src->pw_dir, first);
658 dst->pw_dir = buf + ofs;
659 ofs = PTR_DIFF(src->pw_shell, first);
660 dst->pw_shell = buf + ofs;
670 * the caller has to call nwrap_unload() on failure
672 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
674 struct nwrap_gr *nwrap_gr;
682 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
684 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
685 gr = (struct group *)realloc(nwrap_gr->list, list_size);
687 NWRAP_ERROR(("%s:realloc failed\n",__location__));
692 gr = &nwrap_gr->list[nwrap_gr->num];
699 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
700 __location__, line, c));
708 NWRAP_VERBOSE(("name[%s]\n", gr->gr_name));
713 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
714 __location__, line, c));
722 NWRAP_VERBOSE(("password[%s]\n", gr->gr_passwd));
727 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
728 __location__, line, c));
734 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
736 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
737 __location__, line, c, strerror(errno)));
741 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
742 __location__, line, c, strerror(errno)));
746 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
747 __location__, line, c, strerror(errno)));
752 NWRAP_VERBOSE(("gid[%u]\n", gr->gr_gid));
755 gr->gr_mem = (char **)malloc(sizeof(char *));
757 NWRAP_ERROR(("%s:calloc failed\n",__location__));
760 gr->gr_mem[0] = NULL;
762 for(nummem=0; p; nummem++) {
772 if (strlen(c) == 0) {
776 m_size = sizeof(char *) * (nummem+2);
777 m = (char **)realloc(gr->gr_mem, m_size);
779 NWRAP_ERROR(("%s:realloc(%u) failed\n",
780 __location__, m_size));
784 gr->gr_mem[nummem] = c;
785 gr->gr_mem[nummem+1] = NULL;
787 NWRAP_VERBOSE(("member[%u]: '%s'\n", nummem, gr->gr_mem[nummem]));
790 NWRAP_DEBUG(("add group[%s:%s:%u:] with %u members\n",
791 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem));
797 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
800 struct nwrap_gr *nwrap_gr;
801 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
803 if (nwrap_gr->list) {
804 for (i=0; i < nwrap_gr->num; i++) {
805 if (nwrap_gr->list[i].gr_mem) {
806 free(nwrap_gr->list[i].gr_mem);
809 free(nwrap_gr->list);
812 nwrap_gr->list = NULL;
817 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
818 char *buf, size_t buflen, struct group **dstp)
828 first = src->gr_name;
831 while (*lastm) lastm++;
834 while (*last) last++;
836 ofsb = PTR_DIFF(last + 1, first);
837 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
839 if ((ofsb + ofsm) > buflen) {
843 memcpy(buf, first, ofsb);
844 memcpy(buf + ofsb, src->gr_mem, ofsm);
846 ofs = PTR_DIFF(src->gr_name, first);
847 dst->gr_name = buf + ofs;
848 ofs = PTR_DIFF(src->gr_passwd, first);
849 dst->gr_passwd = buf + ofs;
850 dst->gr_gid = src->gr_gid;
852 dst->gr_mem = (char **)(buf + ofsb);
853 for (i=0; src->gr_mem[i]; i++) {
854 ofs = PTR_DIFF(src->gr_mem[i], first);
855 dst->gr_mem[i] = buf + ofs;
867 static struct passwd *nwrap_files_getpwnam(const char *name)
871 nwrap_cache_reload(nwrap_pw_global.cache);
873 for (i=0; i<nwrap_pw_global.num; i++) {
874 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
875 NWRAP_DEBUG(("%s: user[%s] found\n",
876 __location__, name));
877 return &nwrap_pw_global.list[i];
879 NWRAP_VERBOSE(("%s: user[%s] does not match [%s]\n",
881 nwrap_pw_global.list[i].pw_name));
884 NWRAP_DEBUG(("%s: user[%s] not found\n", __location__, name));
890 static int nwrap_files_getpwnam_r(const char *name, struct passwd *pwdst,
891 char *buf, size_t buflen, struct passwd **pwdstp)
895 pw = nwrap_getpwnam(name);
903 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
906 static struct passwd *nwrap_files_getpwuid(uid_t uid)
910 nwrap_cache_reload(nwrap_pw_global.cache);
912 for (i=0; i<nwrap_pw_global.num; i++) {
913 if (nwrap_pw_global.list[i].pw_uid == uid) {
914 NWRAP_DEBUG(("%s: uid[%u] found\n",
916 return &nwrap_pw_global.list[i];
918 NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n",
920 nwrap_pw_global.list[i].pw_uid));
923 NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid));
929 static int nwrap_files_getpwuid_r(uid_t uid, struct passwd *pwdst,
930 char *buf, size_t buflen, struct passwd **pwdstp)
934 pw = nwrap_getpwuid(uid);
942 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
945 /* user enum functions */
946 static void nwrap_files_setpwent(void)
948 nwrap_pw_global.idx = 0;
951 static struct passwd *nwrap_files_getpwent(void)
955 if (nwrap_pw_global.idx == 0) {
956 nwrap_cache_reload(nwrap_pw_global.cache);
959 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
964 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
966 NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n",
967 __location__, pw->pw_name, pw->pw_uid));
972 static int nwrap_files_getpwent_r(struct passwd *pwdst, char *buf,
973 size_t buflen, struct passwd **pwdstp)
977 pw = nwrap_getpwent();
985 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
988 static void nwrap_files_endpwent(void)
990 nwrap_pw_global.idx = 0;
994 static int nwrap_files_initgroups(const char *user, gid_t group)
996 /* TODO: maybe we should also fake this... */
1000 /* group functions */
1001 static struct group *nwrap_files_getgrnam(const char *name)
1005 nwrap_cache_reload(nwrap_gr_global.cache);
1007 for (i=0; i<nwrap_gr_global.num; i++) {
1008 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
1009 NWRAP_DEBUG(("%s: group[%s] found\n",
1010 __location__, name));
1011 return &nwrap_gr_global.list[i];
1013 NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n",
1015 nwrap_gr_global.list[i].gr_name));
1018 NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name));
1024 static int nwrap_files_getgrnam_r(const char *name, struct group *grdst,
1025 char *buf, size_t buflen, struct group **grdstp)
1029 gr = nwrap_getgrnam(name);
1037 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1040 static struct group *nwrap_files_getgrgid(gid_t gid)
1044 nwrap_cache_reload(nwrap_gr_global.cache);
1046 for (i=0; i<nwrap_gr_global.num; i++) {
1047 if (nwrap_gr_global.list[i].gr_gid == gid) {
1048 NWRAP_DEBUG(("%s: gid[%u] found\n",
1049 __location__, gid));
1050 return &nwrap_gr_global.list[i];
1052 NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n",
1054 nwrap_gr_global.list[i].gr_gid));
1057 NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid));
1063 static int nwrap_files_getgrgid_r(gid_t gid, struct group *grdst,
1064 char *buf, size_t buflen, struct group **grdstp)
1068 gr = nwrap_getgrgid(gid);
1076 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1081 /* group enum functions */
1082 static void nwrap_files_setgrent(void)
1084 nwrap_gr_global.idx = 0;
1087 static struct group *nwrap_files_getgrent(void)
1091 if (nwrap_gr_global.idx == 0) {
1092 nwrap_cache_reload(nwrap_gr_global.cache);
1095 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
1100 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
1102 NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n",
1103 __location__, gr->gr_name, gr->gr_gid));
1108 static int nwrap_files_getgrent_r(struct group *grdst, char *buf,
1109 size_t buflen, struct group **grdstp)
1113 gr = nwrap_getgrent();
1121 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1124 static void nwrap_files_endgrent(void)
1126 nwrap_gr_global.idx = 0;
1129 static int nwrap_files_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1134 const char *name_of_group = NULL;
1136 NWRAP_DEBUG(("%s: getgrouplist called for %s\n", __location__, user));
1138 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
1140 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1145 memcpy(groups_tmp, &group, sizeof(gid_t));
1147 grp = nwrap_getgrgid(group);
1149 name_of_group = grp->gr_name;
1152 nwrap_files_setgrent();
1153 while ((grp = nwrap_files_getgrent()) != NULL) {
1156 NWRAP_VERBOSE(("%s: inspecting %s for group membership\n",
1157 __location__, grp->gr_name));
1159 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
1161 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
1162 (strcmp(name_of_group, grp->gr_name) != 0)) {
1164 NWRAP_DEBUG(("%s: %s is member of %s\n",
1165 __location__, user, grp->gr_name));
1167 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
1169 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1174 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
1179 nwrap_files_endgrent();
1181 NWRAP_VERBOSE(("%s: %s is member of %d groups: %d\n",
1182 __location__, user, *ngroups));
1184 if (*ngroups < count) {
1191 memcpy(groups, groups_tmp, count * sizeof(gid_t));
1201 _PUBLIC_ struct passwd *nwrap_getpwnam(const char *name)
1203 if (!nwrap_enabled()) {
1204 return real_getpwnam(name);
1207 return nwrap_main_global->ops->nw_getpwnam(name);
1210 _PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
1211 char *buf, size_t buflen, struct passwd **pwdstp)
1213 if (!nwrap_enabled()) {
1214 return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
1217 return nwrap_main_global->ops->nw_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
1220 _PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid)
1222 if (!nwrap_enabled()) {
1223 return real_getpwuid(uid);
1226 return nwrap_main_global->ops->nw_getpwuid(uid);
1229 _PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
1230 char *buf, size_t buflen, struct passwd **pwdstp)
1232 if (!nwrap_enabled()) {
1233 return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
1236 return nwrap_main_global->ops->nw_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
1239 _PUBLIC_ void nwrap_setpwent(void)
1241 if (!nwrap_enabled()) {
1246 nwrap_main_global->ops->nw_setpwent();
1249 _PUBLIC_ struct passwd *nwrap_getpwent(void)
1251 if (!nwrap_enabled()) {
1252 return real_getpwent();
1255 return nwrap_main_global->ops->nw_getpwent();
1258 _PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
1259 size_t buflen, struct passwd **pwdstp)
1261 if (!nwrap_enabled()) {
1262 #ifdef SOLARIS_GETPWENT_R
1264 pw = real_getpwent_r(pwdst, buf, buflen);
1276 return real_getpwent_r(pwdst, buf, buflen, pwdstp);
1280 return nwrap_main_global->ops->nw_getpwent_r(pwdst, buf, buflen, pwdstp);
1283 _PUBLIC_ void nwrap_endpwent(void)
1285 if (!nwrap_enabled()) {
1290 nwrap_main_global->ops->nw_endpwent();
1293 _PUBLIC_ int nwrap_initgroups(const char *user, gid_t group)
1295 if (!nwrap_enabled()) {
1296 return real_initgroups(user, group);
1299 return nwrap_main_global->ops->nw_initgroups(user, group);
1302 _PUBLIC_ struct group *nwrap_getgrnam(const char *name)
1304 if (!nwrap_enabled()) {
1305 return real_getgrnam(name);
1308 return nwrap_main_global->ops->nw_getgrnam(name);
1311 _PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst,
1312 char *buf, size_t buflen, struct group **grdstp)
1314 if (!nwrap_enabled()) {
1315 return real_getgrnam_r(name, grdst, buf, buflen, grdstp);
1318 return nwrap_main_global->ops->nw_getgrnam_r(name, grdst, buf, buflen, grdstp);
1321 _PUBLIC_ struct group *nwrap_getgrgid(gid_t gid)
1323 if (!nwrap_enabled()) {
1324 return real_getgrgid(gid);
1327 return nwrap_main_global->ops->nw_getgrgid(gid);
1330 _PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
1331 char *buf, size_t buflen, struct group **grdstp)
1333 if (!nwrap_enabled()) {
1334 return real_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1337 return nwrap_main_global->ops->nw_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1340 _PUBLIC_ void nwrap_setgrent(void)
1342 if (!nwrap_enabled()) {
1347 nwrap_main_global->ops->nw_setgrent();
1350 _PUBLIC_ struct group *nwrap_getgrent(void)
1352 if (!nwrap_enabled()) {
1353 return real_getgrent();
1356 return nwrap_main_global->ops->nw_getgrent();
1359 _PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf,
1360 size_t buflen, struct group **grdstp)
1362 if (!nwrap_enabled()) {
1363 #ifdef SOLARIS_GETGRENT_R
1365 gr = real_getgrent_r(grdst, buf, buflen);
1377 return real_getgrent_r(grdst, buf, buflen, grdstp);
1381 return nwrap_main_global->ops->nw_getgrent_r(grdst, buf, buflen, grdstp);
1384 _PUBLIC_ void nwrap_endgrent(void)
1386 if (!nwrap_enabled()) {
1391 nwrap_main_global->ops->nw_endgrent();
1394 _PUBLIC_ int nwrap_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1396 if (!nwrap_enabled()) {
1397 return real_getgrouplist(user, group, groups, ngroups);
1400 return nwrap_main_global->ops->nw_getgrouplist(user, group, groups, ngroups);