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_GETGRUID_R
65 #define getgrgid_r(uid, grdst, buf, buflen, grdstp) ENOSYS
67 #ifndef HAVE_GETGRENT_R
68 #define getgrent_r(grdst, buf, buflen, grdstp) ENOSYS
71 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
77 #define real_getpwnam getpwnam
78 #define real_getpwnam_r getpwnam_r
79 #define real_getpwuid getpwuid
80 #define real_getpwuid_r getpwuid_r
82 #define real_setpwent setpwent
83 #define real_getpwent getpwent
84 #define real_getpwent_r getpwent_r
85 #define real_endpwent endpwent
88 #define real_getgrlst getgrlst
89 #define real_getgrlst_r getgrlst_r
90 #define real_initgroups_dyn initgroups_dyn
92 #define real_initgroups initgroups
93 #define real_getgrouplist getgrouplist
95 #define real_getgrnam getgrnam
96 #define real_getgrnam_r getgrnam_r
97 #define real_getgrgid getgrgid
98 #define real_getgrgid_r getgrgid_r
100 #define real_setgrent setgrent
101 #define real_getgrent getgrent
102 #define real_getgrent_r getgrent_r
103 #define real_endgrent endgrent
109 # define NWRAP_ERROR(args) DEBUG(0, args)
111 # define NWRAP_ERROR(args) printf args
114 #define NWRAP_ERROR(args)
119 # define NWRAP_DEBUG(args) DEBUG(0, args)
121 # define NWRAP_DEBUG(args) printf args
124 #define NWRAP_DEBUG(args)
129 # define NWRAP_VERBOSE(args) DEBUG(0, args)
131 # define NWRAP_VERBOSE(args) printf args
134 #define NWRAP_VERBOSE(args)
143 bool (*parse_line)(struct nwrap_cache *, char *line);
144 void (*unload)(struct nwrap_cache *);
148 struct nwrap_cache *cache;
155 struct nwrap_cache __nwrap_cache_pw;
156 struct nwrap_pw nwrap_pw_global;
158 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
159 static void nwrap_pw_unload(struct nwrap_cache *nwrap);
162 struct nwrap_cache *cache;
169 struct nwrap_cache __nwrap_cache_gr;
170 struct nwrap_gr nwrap_gr_global;
172 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
173 static void nwrap_gr_unload(struct nwrap_cache *nwrap);
175 static void nwrap_init(void)
177 static bool initialized;
179 if (initialized) return;
182 nwrap_pw_global.cache = &__nwrap_cache_pw;
184 nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
185 nwrap_pw_global.cache->fd = -1;
186 nwrap_pw_global.cache->private_data = &nwrap_pw_global;
187 nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
188 nwrap_pw_global.cache->unload = nwrap_pw_unload;
190 nwrap_gr_global.cache = &__nwrap_cache_gr;
192 nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
193 nwrap_gr_global.cache->fd = -1;
194 nwrap_gr_global.cache->private_data = &nwrap_gr_global;
195 nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
196 nwrap_gr_global.cache->unload = nwrap_gr_unload;
199 static bool nwrap_enabled(void)
203 if (!nwrap_pw_global.cache->path) {
206 if (nwrap_pw_global.cache->path[0] == '\0') {
209 if (!nwrap_gr_global.cache->path) {
212 if (nwrap_gr_global.cache->path[0] == '\0') {
219 static bool nwrap_parse_file(struct nwrap_cache *nwrap)
225 if (nwrap->st.st_size == 0) {
226 NWRAP_DEBUG(("%s: size == 0\n",
231 if (nwrap->st.st_size > INT32_MAX) {
232 NWRAP_ERROR(("%s: size[%u] larger than INT32_MAX\n",
233 __location__, (unsigned)nwrap->st.st_size));
237 ret = lseek(nwrap->fd, 0, SEEK_SET);
239 NWRAP_ERROR(("%s: lseek - %d\n",__location__,ret));
243 buf = (uint8_t *)malloc(nwrap->st.st_size + 1);
245 NWRAP_ERROR(("%s: malloc failed\n",__location__));
249 ret = read(nwrap->fd, buf, nwrap->st.st_size);
250 if (ret != nwrap->st.st_size) {
251 NWRAP_ERROR(("%s: read(%u) gave %d\n",
252 __location__, (unsigned)nwrap->st.st_size, ret));
256 buf[nwrap->st.st_size] = '\0';
259 while (nline && nline[0]) {
267 e = strchr(line, '\n');
278 NWRAP_VERBOSE(("%s:'%s'\n",__location__, line));
280 if (strlen(line) == 0) {
284 ok = nwrap->parse_line(nwrap, line);
299 static void nwrap_cache_unload(struct nwrap_cache *nwrap)
301 nwrap->unload(nwrap);
303 if (nwrap->buf) free(nwrap->buf);
308 static void nwrap_cache_reload(struct nwrap_cache *nwrap)
313 bool retried = false;
317 nwrap->fd = open(nwrap->path, O_RDONLY);
319 NWRAP_ERROR(("%s: unable to open '%s' readonly %d:%s\n",
321 nwrap->path, nwrap->fd,
325 NWRAP_VERBOSE(("%s: open '%s'\n", __location__, nwrap->path));
328 ret = fstat(nwrap->fd, &st);
330 NWRAP_ERROR(("%s: fstat(%s) - %d:%s\n",
333 ret, strerror(errno)));
337 if (retried == false && st.st_nlink == 0) {
338 /* maybe someone has replaced the file... */
339 NWRAP_DEBUG(("%s: st_nlink == 0, reopen %s\n",
340 __location__, nwrap->path));
342 memset(&nwrap->st, 0, sizeof(nwrap->st));
348 if (st.st_mtime == nwrap->st.st_mtime) {
349 NWRAP_VERBOSE(("%s: st_mtime[%u] hasn't changed, skip reload\n",
350 __location__, (unsigned)st.st_mtime));
353 NWRAP_DEBUG(("%s: st_mtime has changed [%u] => [%u], start reload\n",
354 __location__, (unsigned)st.st_mtime,
355 (unsigned)nwrap->st.st_mtime));
359 nwrap_cache_unload(nwrap);
361 ok = nwrap_parse_file(nwrap);
363 NWRAP_ERROR(("%s: failed to reload %s\n",
364 __location__, nwrap->path));
365 nwrap_cache_unload(nwrap);
367 NWRAP_DEBUG(("%s: reloaded %s\n",
368 __location__, nwrap->path));
372 * the caller has to call nwrap_unload() on failure
374 static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
376 struct nwrap_pw *nwrap_pw;
383 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
385 list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
386 pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
388 NWRAP_ERROR(("%s:realloc(%u) failed\n",
389 __location__, list_size));
394 pw = &nwrap_pw->list[nwrap_pw->num];
401 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
402 __location__, line, c));
410 NWRAP_VERBOSE(("name[%s]\n", pw->pw_name));
415 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
416 __location__, line, c));
424 NWRAP_VERBOSE(("password[%s]\n", pw->pw_passwd));
429 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
430 __location__, line, c));
436 pw->pw_uid = (uid_t)strtoul(c, &e, 10);
438 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
439 __location__, line, c, strerror(errno)));
443 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
444 __location__, line, c, strerror(errno)));
448 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
449 __location__, line, c, strerror(errno)));
454 NWRAP_VERBOSE(("uid[%u]\n", pw->pw_uid));
459 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
460 __location__, line, c));
466 pw->pw_gid = (gid_t)strtoul(c, &e, 10);
468 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
469 __location__, line, c, strerror(errno)));
473 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
474 __location__, line, c, strerror(errno)));
478 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
479 __location__, line, c, strerror(errno)));
484 NWRAP_VERBOSE(("gid[%u]\n", pw->pw_gid));
489 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
490 __location__, line, c));
498 NWRAP_VERBOSE(("gecos[%s]\n", pw->pw_gecos));
503 NWRAP_ERROR(("%s:'%s'\n",__location__,c));
511 NWRAP_VERBOSE(("dir[%s]\n", pw->pw_dir));
515 NWRAP_VERBOSE(("shell[%s]\n", pw->pw_shell));
517 NWRAP_DEBUG(("add user[%s:%s:%u:%u:%s:%s:%s]\n",
518 pw->pw_name, pw->pw_passwd,
519 pw->pw_uid, pw->pw_gid,
520 pw->pw_gecos, pw->pw_dir, pw->pw_shell));
526 static void nwrap_pw_unload(struct nwrap_cache *nwrap)
528 struct nwrap_pw *nwrap_pw;
529 nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
531 if (nwrap_pw->list) free(nwrap_pw->list);
533 nwrap_pw->list = NULL;
538 static int nwrap_pw_copy_r(const struct passwd *src, struct passwd *dst,
539 char *buf, size_t buflen, struct passwd **dstp)
545 first = src->pw_name;
547 last = src->pw_shell;
548 while (*last) last++;
550 ofs = PTR_DIFF(last + 1, first);
556 memcpy(buf, first, ofs);
558 ofs = PTR_DIFF(src->pw_name, first);
559 dst->pw_name = buf + ofs;
560 ofs = PTR_DIFF(src->pw_passwd, first);
561 dst->pw_passwd = buf + ofs;
562 dst->pw_uid = src->pw_uid;
563 dst->pw_gid = src->pw_gid;
564 ofs = PTR_DIFF(src->pw_gecos, first);
565 dst->pw_gecos = buf + ofs;
566 ofs = PTR_DIFF(src->pw_dir, first);
567 dst->pw_dir = buf + ofs;
568 ofs = PTR_DIFF(src->pw_shell, first);
569 dst->pw_shell = buf + ofs;
579 * the caller has to call nwrap_unload() on failure
581 static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line)
583 struct nwrap_gr *nwrap_gr;
591 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
593 list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1);
594 gr = (struct group *)realloc(nwrap_gr->list, list_size);
596 NWRAP_ERROR(("%s:realloc failed\n",__location__));
601 gr = &nwrap_gr->list[nwrap_gr->num];
608 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
609 __location__, line, c));
617 NWRAP_VERBOSE(("name[%s]\n", gr->gr_name));
622 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
623 __location__, line, c));
631 NWRAP_VERBOSE(("password[%s]\n", gr->gr_passwd));
636 NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n",
637 __location__, line, c));
643 gr->gr_gid = (gid_t)strtoul(c, &e, 10);
645 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
646 __location__, line, c, strerror(errno)));
650 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
651 __location__, line, c, strerror(errno)));
655 NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n",
656 __location__, line, c, strerror(errno)));
661 NWRAP_VERBOSE(("gid[%u]\n", gr->gr_gid));
664 gr->gr_mem = (char **)malloc(sizeof(char *));
666 NWRAP_ERROR(("%s:calloc failed\n",__location__));
669 gr->gr_mem[0] = NULL;
671 for(nummem=0; p; nummem++) {
681 if (strlen(c) == 0) {
685 m_size = sizeof(char *) * (nummem+2);
686 m = (char **)realloc(gr->gr_mem, m_size);
688 NWRAP_ERROR(("%s:realloc(%u) failed\n",
689 __location__, m_size));
693 gr->gr_mem[nummem] = c;
694 gr->gr_mem[nummem+1] = NULL;
696 NWRAP_VERBOSE(("member[%u]: '%s'\n", nummem, gr->gr_mem[nummem]));
699 NWRAP_DEBUG(("add group[%s:%s:%u:] with %u members\n",
700 gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem));
706 static void nwrap_gr_unload(struct nwrap_cache *nwrap)
709 struct nwrap_gr *nwrap_gr;
710 nwrap_gr = (struct nwrap_gr *)nwrap->private_data;
712 if (nwrap_gr->list) {
713 for (i=0; i < nwrap_gr->num; i++) {
714 if (nwrap_gr->list[i].gr_mem) {
715 free(nwrap_gr->list[i].gr_mem);
718 free(nwrap_gr->list);
721 nwrap_gr->list = NULL;
726 static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
727 char *buf, size_t buflen, struct group **dstp)
737 first = src->gr_name;
740 while (*lastm) lastm++;
743 while (*last) last++;
745 ofsb = PTR_DIFF(last + 1, first);
746 ofsm = PTR_DIFF(lastm + 1, src->gr_mem);
748 if ((ofsb + ofsm) > buflen) {
752 memcpy(buf, first, ofsb);
753 memcpy(buf + ofsb, src->gr_mem, ofsm);
755 ofs = PTR_DIFF(src->gr_name, first);
756 dst->gr_name = buf + ofs;
757 ofs = PTR_DIFF(src->gr_passwd, first);
758 dst->gr_passwd = buf + ofs;
759 dst->gr_gid = src->gr_gid;
761 dst->gr_mem = (char **)(buf + ofsb);
762 for (i=0; src->gr_mem[i]; i++) {
763 ofs = PTR_DIFF(src->gr_mem[i], first);
764 dst->gr_mem[i] = buf + ofs;
776 static struct passwd *nwrap_files_getpwnam(const char *name)
780 nwrap_cache_reload(nwrap_pw_global.cache);
782 for (i=0; i<nwrap_pw_global.num; i++) {
783 if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) {
784 NWRAP_DEBUG(("%s: user[%s] found\n",
785 __location__, name));
786 return &nwrap_pw_global.list[i];
788 NWRAP_VERBOSE(("%s: user[%s] does not match [%s]\n",
790 nwrap_pw_global.list[i].pw_name));
793 NWRAP_DEBUG(("%s: user[%s] not found\n", __location__, name));
799 _PUBLIC_ struct passwd *nwrap_getpwnam(const char *name)
801 if (!nwrap_enabled()) {
802 return real_getpwnam(name);
805 return nwrap_files_getpwnam(name);
808 static int nwrap_files_getpwnam_r(const char *name, struct passwd *pwdst,
809 char *buf, size_t buflen, struct passwd **pwdstp)
813 pw = nwrap_getpwnam(name);
821 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
824 _PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
825 char *buf, size_t buflen, struct passwd **pwdstp)
827 if (!nwrap_enabled()) {
828 return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
831 return nwrap_files_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
834 static struct passwd *nwrap_files_getpwuid(uid_t uid)
838 nwrap_cache_reload(nwrap_pw_global.cache);
840 for (i=0; i<nwrap_pw_global.num; i++) {
841 if (nwrap_pw_global.list[i].pw_uid == uid) {
842 NWRAP_DEBUG(("%s: uid[%u] found\n",
844 return &nwrap_pw_global.list[i];
846 NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n",
848 nwrap_pw_global.list[i].pw_uid));
851 NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid));
857 _PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid)
859 if (!nwrap_enabled()) {
860 return real_getpwuid(uid);
863 return nwrap_files_getpwuid(uid);
866 static int nwrap_files_getpwuid_r(uid_t uid, struct passwd *pwdst,
867 char *buf, size_t buflen, struct passwd **pwdstp)
871 pw = nwrap_getpwuid(uid);
879 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
882 _PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
883 char *buf, size_t buflen, struct passwd **pwdstp)
885 if (!nwrap_enabled()) {
886 return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
889 return nwrap_files_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
892 /* user enum functions */
893 static void nwrap_files_setpwent(void)
895 nwrap_pw_global.idx = 0;
898 _PUBLIC_ void nwrap_setpwent(void)
900 if (!nwrap_enabled()) {
904 nwrap_files_setpwent();
907 static struct passwd *nwrap_files_getpwent(void)
911 if (nwrap_pw_global.idx == 0) {
912 nwrap_cache_reload(nwrap_pw_global.cache);
915 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
920 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
922 NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n",
923 __location__, pw->pw_name, pw->pw_uid));
928 _PUBLIC_ struct passwd *nwrap_getpwent(void)
930 if (!nwrap_enabled()) {
931 return real_getpwent();
934 return nwrap_files_getpwent();
937 static int nwrap_files_getpwent_r(struct passwd *pwdst, char *buf,
938 size_t buflen, struct passwd **pwdstp)
942 pw = nwrap_getpwent();
950 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
953 _PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
954 size_t buflen, struct passwd **pwdstp)
956 if (!nwrap_enabled()) {
957 #ifdef SOLARIS_GETPWENT_R
959 pw = real_getpwent_r(pwdst, buf, buflen);
971 return real_getpwent_r(pwdst, buf, buflen, pwdstp);
975 return nwrap_files_getpwent_r(pwdst, buf, buflen, pwdstp);
978 static void nwrap_files_endpwent(void)
980 nwrap_pw_global.idx = 0;
983 _PUBLIC_ void nwrap_endpwent(void)
985 if (!nwrap_enabled()) {
989 nwrap_files_endpwent();
993 static int nwrap_files_initgroups(const char *user, gid_t group)
995 /* TODO: maybe we should also fake this... */
999 _PUBLIC_ int nwrap_initgroups(const char *user, gid_t group)
1001 if (!nwrap_enabled()) {
1002 return real_initgroups(user, group);
1005 return nwrap_files_initgroups(user, group);
1008 /* group functions */
1009 static struct group *nwrap_files_getgrnam(const char *name)
1013 nwrap_cache_reload(nwrap_gr_global.cache);
1015 for (i=0; i<nwrap_gr_global.num; i++) {
1016 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
1017 NWRAP_DEBUG(("%s: group[%s] found\n",
1018 __location__, name));
1019 return &nwrap_gr_global.list[i];
1021 NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n",
1023 nwrap_gr_global.list[i].gr_name));
1026 NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name));
1032 _PUBLIC_ struct group *nwrap_getgrnam(const char *name)
1034 if (!nwrap_enabled()) {
1035 return real_getgrnam(name);
1038 return nwrap_files_getgrnam(name);
1041 static int nwrap_files_getgrnam_r(const char *name, struct group *grdst,
1042 char *buf, size_t buflen, struct group **grdstp)
1046 gr = nwrap_getgrnam(name);
1054 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1057 _PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst,
1058 char *buf, size_t buflen, struct group **grdstp)
1060 if (!nwrap_enabled()) {
1061 return real_getgrnam_r(name, grdst, buf, buflen, grdstp);
1064 return nwrap_files_getgrnam_r(name, grdst, buf, buflen, grdstp);
1067 static struct group *nwrap_files_getgrgid(gid_t gid)
1071 nwrap_cache_reload(nwrap_gr_global.cache);
1073 for (i=0; i<nwrap_gr_global.num; i++) {
1074 if (nwrap_gr_global.list[i].gr_gid == gid) {
1075 NWRAP_DEBUG(("%s: gid[%u] found\n",
1076 __location__, gid));
1077 return &nwrap_gr_global.list[i];
1079 NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n",
1081 nwrap_gr_global.list[i].gr_gid));
1084 NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid));
1090 _PUBLIC_ struct group *nwrap_getgrgid(gid_t gid)
1092 if (!nwrap_enabled()) {
1093 return real_getgrgid(gid);
1096 return nwrap_files_getgrgid(gid);
1099 static int nwrap_files_getgrgid_r(gid_t gid, struct group *grdst,
1100 char *buf, size_t buflen, struct group **grdstp)
1104 gr = nwrap_getgrgid(gid);
1112 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1117 _PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
1118 char *buf, size_t buflen, struct group **grdstp)
1120 if (!nwrap_enabled()) {
1121 return real_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1124 return nwrap_files_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1127 /* group enum functions */
1128 static void nwrap_files_setgrent(void)
1130 nwrap_gr_global.idx = 0;
1133 _PUBLIC_ void nwrap_setgrent(void)
1135 if (!nwrap_enabled()) {
1139 nwrap_files_setgrent();
1142 static struct group *nwrap_files_getgrent(void)
1146 if (nwrap_gr_global.idx == 0) {
1147 nwrap_cache_reload(nwrap_gr_global.cache);
1150 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
1155 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
1157 NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n",
1158 __location__, gr->gr_name, gr->gr_gid));
1163 _PUBLIC_ struct group *nwrap_getgrent(void)
1165 if (!nwrap_enabled()) {
1166 return real_getgrent();
1169 return nwrap_files_getgrent();
1172 static int nwrap_files_getgrent_r(struct group *grdst, char *buf,
1173 size_t buflen, struct group **grdstp)
1177 gr = nwrap_getgrent();
1185 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1188 _PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf,
1189 size_t buflen, struct group **grdstp)
1191 if (!nwrap_enabled()) {
1192 #ifdef SOLARIS_GETGRENT_R
1194 gr = real_getgrent_r(grdst, buf, buflen);
1206 return real_getgrent_r(grdst, buf, buflen, grdstp);
1210 return nwrap_files_getgrent_r(grdst, buf, buflen, grdstp);
1213 static void nwrap_files_endgrent(void)
1215 nwrap_gr_global.idx = 0;
1218 _PUBLIC_ void nwrap_endgrent(void)
1220 if (!nwrap_enabled()) {
1224 nwrap_files_endgrent();
1227 static int nwrap_files_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1232 const char *name_of_group = NULL;
1234 NWRAP_DEBUG(("%s: getgrouplist called for %s\n", __location__, user));
1236 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
1238 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1243 memcpy(groups_tmp, &group, sizeof(gid_t));
1245 grp = nwrap_getgrgid(group);
1247 name_of_group = grp->gr_name;
1250 nwrap_files_setgrent();
1251 while ((grp = nwrap_files_getgrent()) != NULL) {
1254 NWRAP_VERBOSE(("%s: inspecting %s for group membership\n",
1255 __location__, grp->gr_name));
1257 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
1259 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
1260 (strcmp(name_of_group, grp->gr_name) != 0)) {
1262 NWRAP_DEBUG(("%s: %s is member of %s\n",
1263 __location__, user, grp->gr_name));
1265 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
1267 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1272 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
1277 nwrap_files_endgrent();
1279 NWRAP_VERBOSE(("%s: %s is member of %d groups: %d\n",
1280 __location__, user, *ngroups));
1282 if (*ngroups < count) {
1289 memcpy(groups, groups_tmp, count * sizeof(gid_t));
1295 _PUBLIC_ int nwrap_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1297 if (!nwrap_enabled()) {
1298 return real_getgrouplist(user, group, groups, ngroups);
1301 return nwrap_files_getgrouplist(user, group, groups, ngroups);