3 * group database for nss_nonlocal proxy
5 * Copyright © 2007 Anders Kaseorg <andersk@mit.edu>
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <sys/types.h>
40 #include "nsswitch-internal.h"
43 #define MAGIC_LOCAL_GR_BUFLEN (sysconf(_SC_GETGR_R_SIZE_MAX) + 7)
47 nss_group_nonlocal_database(void)
49 static service_user *nip = NULL;
51 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
58 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
60 enum nss_status status = NSS_STATUS_SUCCESS;
62 struct group *gbufp = &gbuf;
64 int old_errno = errno;
65 int buflen = MAGIC_LOCAL_GR_BUFLEN;
66 char *buf = malloc(buflen);
70 return NSS_STATUS_TRYAGAIN;
73 ret = getgrgid_r(gid, gbufp, buf, buflen, &gbufp);
76 status = NSS_STATUS_TRYAGAIN;
77 } else if (gbufp != NULL) {
78 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
79 status = NSS_STATUS_NOTFOUND;
87 static service_user *grent_nip = NULL;
88 static void *grent_fct_start;
90 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
94 static const char *grent_fct_name = "getgrent_r";
97 _nss_nonlocal_setgrent(int stayopen)
99 static const char *fct_name = "setgrent";
100 static void *fct_start = NULL;
101 enum nss_status status;
104 enum nss_status (*l)(int stayopen);
108 nip = nss_group_nonlocal_database();
110 return NSS_STATUS_UNAVAIL;
111 if (fct_start == NULL)
112 fct_start = __nss_lookup_function(nip, fct_name);
116 status = NSS_STATUS_UNAVAIL;
118 status = DL_CALL_FCT(fct.l, (stayopen));
119 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
120 if (status != NSS_STATUS_SUCCESS)
124 if (grent_fct_start == NULL)
125 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
126 grent_fct.ptr = grent_fct_start;
127 return NSS_STATUS_SUCCESS;
131 _nss_nonlocal_endgrent(void)
133 static const char *fct_name = "endgrent";
134 static void *fct_start = NULL;
135 enum nss_status status;
138 enum nss_status (*l)(void);
144 nip = nss_group_nonlocal_database();
146 return NSS_STATUS_UNAVAIL;
147 if (fct_start == NULL)
148 fct_start = __nss_lookup_function(nip, fct_name);
152 status = NSS_STATUS_UNAVAIL;
154 status = DL_CALL_FCT(fct.l, ());
155 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
160 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
163 enum nss_status status;
164 if (grent_nip == NULL) {
165 status = _nss_nonlocal_setgrent(0);
166 if (status != NSS_STATUS_SUCCESS)
170 if (grent_fct.ptr == NULL)
171 status = NSS_STATUS_UNAVAIL;
175 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
176 while (status == NSS_STATUS_SUCCESS &&
177 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
179 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
182 if (status == NSS_STATUS_SUCCESS)
183 return NSS_STATUS_SUCCESS;
184 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
187 return NSS_STATUS_NOTFOUND;
192 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
193 char *buffer, size_t buflen, int *errnop)
195 static const char *fct_name = "getgrnam_r";
196 static void *fct_start = NULL;
197 enum nss_status status;
200 enum nss_status (*l)(const char *name, struct group *grp,
201 char *buffer, size_t buflen, int *errnop);
205 nip = nss_group_nonlocal_database();
207 return NSS_STATUS_UNAVAIL;
208 if (fct_start == NULL)
209 fct_start = __nss_lookup_function(nip, fct_name);
213 status = NSS_STATUS_UNAVAIL;
215 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
216 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
218 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
219 if (status != NSS_STATUS_SUCCESS)
222 return check_nonlocal_gid(name, grp->gr_gid, errnop);
226 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
227 char *buffer, size_t buflen, int *errnop)
229 static const char *fct_name = "getgrgid_r";
230 static void *fct_start = NULL;
231 enum nss_status status;
234 enum nss_status (*l)(gid_t gid, struct group *grp,
235 char *buffer, size_t buflen, int *errnop);
239 if (buflen == MAGIC_LOCAL_GR_BUFLEN)
240 return NSS_STATUS_UNAVAIL;
242 nip = nss_group_nonlocal_database();
244 return NSS_STATUS_UNAVAIL;
245 if (fct_start == NULL)
246 fct_start = __nss_lookup_function(nip, fct_name);
250 status = NSS_STATUS_UNAVAIL;
252 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
253 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
255 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
256 if (status != NSS_STATUS_SUCCESS)
259 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
263 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
264 long int *size, gid_t **groupsp, long int limit,
267 static const char *fct_name = "initgroups_dyn";
268 static void *fct_start = NULL;
269 enum nss_status status;
272 enum nss_status (*l)(const char *user, gid_t group, long int *start,
273 long int *size, gid_t **groupsp, long int limit,
277 int in = *start, out = *start, i;
279 nip = nss_group_nonlocal_database();
281 return NSS_STATUS_UNAVAIL;
282 if (fct_start == NULL)
283 fct_start = __nss_lookup_function(nip, fct_name);
288 status = NSS_STATUS_UNAVAIL;
290 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
291 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
293 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
294 if (status != NSS_STATUS_SUCCESS)
297 for (; in < *start; ++in) {
298 int nonlocal_errno = *errnop;
300 for (i = 0; i < out; ++i)
301 if ((*groupsp)[i] == (*groupsp)[in])
306 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
307 if (status == NSS_STATUS_SUCCESS) {
308 (*groupsp)[out++] = (*groupsp)[in];
309 } else if (status != NSS_STATUS_NOTFOUND) {
311 *errnop = nonlocal_errno;
317 return NSS_STATUS_SUCCESS;