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 * (*getpwnam)(const char *name);
145 int (*getpwnam_r)(const char *name, struct passwd *pwdst,
146 char *buf, size_t buflen, struct passwd **pwdstp);
147 struct passwd * (*getpwuid)(uid_t uid);
148 int (*getpwuid_r)(uid_t uid, struct passwd *pwdst,
149 char *buf, size_t buflen, struct passwd **pwdstp);
150 void (*setpwent)(void);
151 struct passwd * (*getpwent)(void);
152 int (*getpwent_r)(struct passwd *pwdst, char *buf,
153 size_t buflen, struct passwd **pwdstp);
154 void (*endpwent)(void);
155 int (*initgroups)(const char *user, gid_t group);
156 struct group * (*getgrnam)(const char *name);
157 int (*getgrnam_r)(const char *name, struct group *grdst,
158 char *buf, size_t buflen, struct group **grdstp);
159 struct group * (*getgrgid)(gid_t gid);
160 int (*getgrgid_r)(gid_t gid, struct group *grdst,
161 char *buf, size_t buflen, struct group **grdstp);
162 void (*setgrent)(void);
163 struct group * (*getgrent)(void);
164 int (*getgrent_r)(struct group *grdst, char *buf,
165 size_t buflen, struct group **grdstp);
166 void (*endgrent)(void);
167 int (*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 .getpwnam = nwrap_files_getpwnam,
198 .getpwnam_r = nwrap_files_getpwnam_r,
199 .getpwuid = nwrap_files_getpwuid,
200 .getpwuid_r = nwrap_files_getpwuid_r,
201 .setpwent = nwrap_files_setpwent,
202 .getpwent = nwrap_files_getpwent,
203 .getpwent_r = nwrap_files_getpwent_r,
204 .endpwent = nwrap_files_endpwent,
205 .initgroups = nwrap_files_initgroups,
206 .getgrnam = nwrap_files_getgrnam,
207 .getgrnam_r = nwrap_files_getgrnam_r,
208 .getgrgid = nwrap_files_getgrgid,
209 .getgrgid_r = nwrap_files_getgrgid_r,
210 .setgrent = nwrap_files_setgrent,
211 .getgrent = nwrap_files_getgrent,
212 .getgrent_r = nwrap_files_getgrent_r,
213 .endgrent = nwrap_files_endgrent,
214 .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 _PUBLIC_ struct passwd *nwrap_getpwnam(const char *name)
892 if (!nwrap_enabled()) {
893 return real_getpwnam(name);
896 return nwrap_main_global->ops->getpwnam(name);
899 static int nwrap_files_getpwnam_r(const char *name, struct passwd *pwdst,
900 char *buf, size_t buflen, struct passwd **pwdstp)
904 pw = nwrap_getpwnam(name);
912 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
915 _PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst,
916 char *buf, size_t buflen, struct passwd **pwdstp)
918 if (!nwrap_enabled()) {
919 return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp);
922 return nwrap_main_global->ops->getpwnam_r(name, pwdst, buf, buflen, pwdstp);
925 static struct passwd *nwrap_files_getpwuid(uid_t uid)
929 nwrap_cache_reload(nwrap_pw_global.cache);
931 for (i=0; i<nwrap_pw_global.num; i++) {
932 if (nwrap_pw_global.list[i].pw_uid == uid) {
933 NWRAP_DEBUG(("%s: uid[%u] found\n",
935 return &nwrap_pw_global.list[i];
937 NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n",
939 nwrap_pw_global.list[i].pw_uid));
942 NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid));
948 _PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid)
950 if (!nwrap_enabled()) {
951 return real_getpwuid(uid);
954 return nwrap_main_global->ops->getpwuid(uid);
957 static int nwrap_files_getpwuid_r(uid_t uid, struct passwd *pwdst,
958 char *buf, size_t buflen, struct passwd **pwdstp)
962 pw = nwrap_getpwuid(uid);
970 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
973 _PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst,
974 char *buf, size_t buflen, struct passwd **pwdstp)
976 if (!nwrap_enabled()) {
977 return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
980 return nwrap_main_global->ops->getpwuid_r(uid, pwdst, buf, buflen, pwdstp);
983 /* user enum functions */
984 static void nwrap_files_setpwent(void)
986 nwrap_pw_global.idx = 0;
989 _PUBLIC_ void nwrap_setpwent(void)
991 if (!nwrap_enabled()) {
996 nwrap_main_global->ops->setpwent();
999 static struct passwd *nwrap_files_getpwent(void)
1003 if (nwrap_pw_global.idx == 0) {
1004 nwrap_cache_reload(nwrap_pw_global.cache);
1007 if (nwrap_pw_global.idx >= nwrap_pw_global.num) {
1012 pw = &nwrap_pw_global.list[nwrap_pw_global.idx++];
1014 NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n",
1015 __location__, pw->pw_name, pw->pw_uid));
1020 _PUBLIC_ struct passwd *nwrap_getpwent(void)
1022 if (!nwrap_enabled()) {
1023 return real_getpwent();
1026 return nwrap_main_global->ops->getpwent();
1029 static int nwrap_files_getpwent_r(struct passwd *pwdst, char *buf,
1030 size_t buflen, struct passwd **pwdstp)
1034 pw = nwrap_getpwent();
1042 return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);
1045 _PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf,
1046 size_t buflen, struct passwd **pwdstp)
1048 if (!nwrap_enabled()) {
1049 #ifdef SOLARIS_GETPWENT_R
1051 pw = real_getpwent_r(pwdst, buf, buflen);
1063 return real_getpwent_r(pwdst, buf, buflen, pwdstp);
1067 return nwrap_main_global->ops->getpwent_r(pwdst, buf, buflen, pwdstp);
1070 static void nwrap_files_endpwent(void)
1072 nwrap_pw_global.idx = 0;
1075 _PUBLIC_ void nwrap_endpwent(void)
1077 if (!nwrap_enabled()) {
1082 nwrap_main_global->ops->endpwent();
1085 /* misc functions */
1086 static int nwrap_files_initgroups(const char *user, gid_t group)
1088 /* TODO: maybe we should also fake this... */
1092 _PUBLIC_ int nwrap_initgroups(const char *user, gid_t group)
1094 if (!nwrap_enabled()) {
1095 return real_initgroups(user, group);
1098 return nwrap_main_global->ops->initgroups(user, group);
1101 /* group functions */
1102 static struct group *nwrap_files_getgrnam(const char *name)
1106 nwrap_cache_reload(nwrap_gr_global.cache);
1108 for (i=0; i<nwrap_gr_global.num; i++) {
1109 if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) {
1110 NWRAP_DEBUG(("%s: group[%s] found\n",
1111 __location__, name));
1112 return &nwrap_gr_global.list[i];
1114 NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n",
1116 nwrap_gr_global.list[i].gr_name));
1119 NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name));
1125 _PUBLIC_ struct group *nwrap_getgrnam(const char *name)
1127 if (!nwrap_enabled()) {
1128 return real_getgrnam(name);
1131 return nwrap_main_global->ops->getgrnam(name);
1134 static int nwrap_files_getgrnam_r(const char *name, struct group *grdst,
1135 char *buf, size_t buflen, struct group **grdstp)
1139 gr = nwrap_getgrnam(name);
1147 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1150 _PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst,
1151 char *buf, size_t buflen, struct group **grdstp)
1153 if (!nwrap_enabled()) {
1154 return real_getgrnam_r(name, grdst, buf, buflen, grdstp);
1157 return nwrap_main_global->ops->getgrnam_r(name, grdst, buf, buflen, grdstp);
1160 static struct group *nwrap_files_getgrgid(gid_t gid)
1164 nwrap_cache_reload(nwrap_gr_global.cache);
1166 for (i=0; i<nwrap_gr_global.num; i++) {
1167 if (nwrap_gr_global.list[i].gr_gid == gid) {
1168 NWRAP_DEBUG(("%s: gid[%u] found\n",
1169 __location__, gid));
1170 return &nwrap_gr_global.list[i];
1172 NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n",
1174 nwrap_gr_global.list[i].gr_gid));
1177 NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid));
1183 _PUBLIC_ struct group *nwrap_getgrgid(gid_t gid)
1185 if (!nwrap_enabled()) {
1186 return real_getgrgid(gid);
1189 return nwrap_main_global->ops->getgrgid(gid);
1192 static int nwrap_files_getgrgid_r(gid_t gid, struct group *grdst,
1193 char *buf, size_t buflen, struct group **grdstp)
1197 gr = nwrap_getgrgid(gid);
1205 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1210 _PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst,
1211 char *buf, size_t buflen, struct group **grdstp)
1213 if (!nwrap_enabled()) {
1214 return real_getgrgid_r(gid, grdst, buf, buflen, grdstp);
1217 return nwrap_main_global->ops->getgrgid_r(gid, grdst, buf, buflen, grdstp);
1220 /* group enum functions */
1221 static void nwrap_files_setgrent(void)
1223 nwrap_gr_global.idx = 0;
1226 _PUBLIC_ void nwrap_setgrent(void)
1228 if (!nwrap_enabled()) {
1233 nwrap_main_global->ops->setgrent();
1236 static struct group *nwrap_files_getgrent(void)
1240 if (nwrap_gr_global.idx == 0) {
1241 nwrap_cache_reload(nwrap_gr_global.cache);
1244 if (nwrap_gr_global.idx >= nwrap_gr_global.num) {
1249 gr = &nwrap_gr_global.list[nwrap_gr_global.idx++];
1251 NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n",
1252 __location__, gr->gr_name, gr->gr_gid));
1257 _PUBLIC_ struct group *nwrap_getgrent(void)
1259 if (!nwrap_enabled()) {
1260 return real_getgrent();
1263 return nwrap_main_global->ops->getgrent();
1266 static int nwrap_files_getgrent_r(struct group *grdst, char *buf,
1267 size_t buflen, struct group **grdstp)
1271 gr = nwrap_getgrent();
1279 return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);
1282 _PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf,
1283 size_t buflen, struct group **grdstp)
1285 if (!nwrap_enabled()) {
1286 #ifdef SOLARIS_GETGRENT_R
1288 gr = real_getgrent_r(grdst, buf, buflen);
1300 return real_getgrent_r(grdst, buf, buflen, grdstp);
1304 return nwrap_main_global->ops->getgrent_r(grdst, buf, buflen, grdstp);
1307 static void nwrap_files_endgrent(void)
1309 nwrap_gr_global.idx = 0;
1312 _PUBLIC_ void nwrap_endgrent(void)
1314 if (!nwrap_enabled()) {
1319 nwrap_main_global->ops->endgrent();
1322 static int nwrap_files_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1327 const char *name_of_group = NULL;
1329 NWRAP_DEBUG(("%s: getgrouplist called for %s\n", __location__, user));
1331 groups_tmp = (gid_t *)malloc(count * sizeof(gid_t));
1333 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1338 memcpy(groups_tmp, &group, sizeof(gid_t));
1340 grp = nwrap_getgrgid(group);
1342 name_of_group = grp->gr_name;
1345 nwrap_files_setgrent();
1346 while ((grp = nwrap_files_getgrent()) != NULL) {
1349 NWRAP_VERBOSE(("%s: inspecting %s for group membership\n",
1350 __location__, grp->gr_name));
1352 for (i=0; grp->gr_mem && grp->gr_mem[i] != NULL; i++) {
1354 if ((strcmp(user, grp->gr_mem[i]) == 0) &&
1355 (strcmp(name_of_group, grp->gr_name) != 0)) {
1357 NWRAP_DEBUG(("%s: %s is member of %s\n",
1358 __location__, user, grp->gr_name));
1360 groups_tmp = (gid_t *)realloc(groups_tmp, (count + 1) * sizeof(gid_t));
1362 NWRAP_ERROR(("%s:calloc failed\n",__location__));
1367 memcpy(&groups_tmp[count], &grp->gr_gid, sizeof(gid_t));
1372 nwrap_files_endgrent();
1374 NWRAP_VERBOSE(("%s: %s is member of %d groups: %d\n",
1375 __location__, user, *ngroups));
1377 if (*ngroups < count) {
1384 memcpy(groups, groups_tmp, count * sizeof(gid_t));
1390 _PUBLIC_ int nwrap_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
1392 if (!nwrap_enabled()) {
1393 return real_getgrouplist(user, group, groups, ngroups);
1396 return nwrap_main_global->ops->getgrouplist(user, group, groups, ngroups);