3 * group database for nss_nonlocal proxy
5 * Copyright © 2007 Anders Kaseorg <andersk@mit.edu> and Tim Abbott
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy,
12 * modify, merge, publish, distribute, sublicense, and/or sell copies
13 * of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <sys/types.h>
41 #include "nsswitch-internal.h"
44 #define MAGIC_LOCAL_GR_BUFLEN (sysconf(_SC_GETGR_R_SIZE_MAX) + 7)
45 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
46 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
50 nss_group_nonlocal_database(void)
52 static service_user *nip = NULL;
54 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
61 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
63 static const char *fct_name = "getgrgid_r";
64 static service_user *startp = NULL;
65 static void *fct_start = NULL;
66 enum nss_status status;
69 enum nss_status (*l)(gid_t gid, struct group *grp,
70 char *buffer, size_t buflen, int *errnop);
74 int old_errno = errno;
75 int buflen = MAGIC_LOCAL_GR_BUFLEN;
76 char *buf = malloc(buflen);
80 return NSS_STATUS_TRYAGAIN;
83 if (fct_start == NULL &&
84 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
86 return NSS_STATUS_UNAVAIL;
91 status = DL_CALL_FCT(fct.l, (gid, &gbuf, buf, buflen, errnop));
92 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
94 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
96 if (status == NSS_STATUS_SUCCESS) {
97 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
98 status = NSS_STATUS_NOTFOUND;
99 } else if (status != NSS_STATUS_TRYAGAIN) {
100 status = NSS_STATUS_SUCCESS;
108 get_local_group(const char *name, struct group *grp, char *buffer, size_t buflen, int *errnop)
110 static const char *fct_name = "getgrnam_r";
111 static service_user *startp = NULL;
112 static void *fct_start = NULL;
113 enum nss_status status;
116 enum nss_status (*l)(const char *name, struct group *grp,
117 char *buffer, size_t buflen, int *errnop);
122 int old_errno = errno;
123 int len = MAGIC_LOCAL_GR_BUFLEN;
124 char *buf = malloc(len);
128 return NSS_STATUS_TRYAGAIN;
131 if (fct_start == NULL &&
132 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
134 return NSS_STATUS_UNAVAIL;
139 status = DL_CALL_FCT(fct.l, (name, &gbuf, buf, buflen, errnop));
140 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
142 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
144 if (status == NSS_STATUS_SUCCESS) {
145 n = snprintf(buffer, buflen, "%s", gbuf.gr_name);
146 if (n < 0 || n >= buflen) {
148 status = NSS_STATUS_TRYAGAIN;
149 goto get_local_group_done;
151 grp->gr_name = buffer;
155 n = snprintf(buffer, buflen, "%s", gbuf.gr_passwd);
156 if (n < 0 || n >= buflen) {
158 status = NSS_STATUS_TRYAGAIN;
159 goto get_local_group_done;
161 grp->gr_passwd = buffer;
165 grp->gr_gid = gbuf.gr_gid;
167 if (buflen < sizeof(void *)) {
169 status = NSS_STATUS_TRYAGAIN;
170 goto get_local_group_done;
172 *(void **)buffer = NULL;
173 buffer += sizeof(void *);
174 buflen -= sizeof(void *);
177 get_local_group_done:
182 static service_user *grent_nip = NULL;
183 static void *grent_fct_start;
185 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
189 static const char *grent_fct_name = "getgrent_r";
192 _nss_nonlocal_setgrent(int stayopen)
194 static const char *fct_name = "setgrent";
195 static void *fct_start = NULL;
196 enum nss_status status;
199 enum nss_status (*l)(int stayopen);
203 nip = nss_group_nonlocal_database();
205 return NSS_STATUS_UNAVAIL;
206 if (fct_start == NULL)
207 fct_start = __nss_lookup_function(nip, fct_name);
211 status = NSS_STATUS_UNAVAIL;
213 status = DL_CALL_FCT(fct.l, (stayopen));
214 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
215 if (status != NSS_STATUS_SUCCESS)
219 if (grent_fct_start == NULL)
220 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
221 grent_fct.ptr = grent_fct_start;
222 return NSS_STATUS_SUCCESS;
226 _nss_nonlocal_endgrent(void)
228 static const char *fct_name = "endgrent";
229 static void *fct_start = NULL;
230 enum nss_status status;
233 enum nss_status (*l)(void);
239 nip = nss_group_nonlocal_database();
241 return NSS_STATUS_UNAVAIL;
242 if (fct_start == NULL)
243 fct_start = __nss_lookup_function(nip, fct_name);
247 status = NSS_STATUS_UNAVAIL;
249 status = DL_CALL_FCT(fct.l, ());
250 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
255 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
258 enum nss_status status;
260 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
261 if (buflen == MAGIC_LOCAL_GR_BUFLEN ||
262 (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0'))
263 return NSS_STATUS_UNAVAIL;
265 if (grent_nip == NULL) {
266 status = _nss_nonlocal_setgrent(0);
267 if (status != NSS_STATUS_SUCCESS)
271 if (grent_fct.ptr == NULL)
272 status = NSS_STATUS_UNAVAIL;
276 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
277 while (status == NSS_STATUS_SUCCESS &&
278 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
280 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
283 if (status == NSS_STATUS_SUCCESS)
284 return NSS_STATUS_SUCCESS;
285 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
288 return NSS_STATUS_NOTFOUND;
293 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
294 char *buffer, size_t buflen, int *errnop)
296 static const char *fct_name = "getgrnam_r";
297 static void *fct_start = NULL;
298 enum nss_status status;
301 enum nss_status (*l)(const char *name, struct group *grp,
302 char *buffer, size_t buflen, int *errnop);
306 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
307 if (buflen == MAGIC_LOCAL_GR_BUFLEN ||
308 (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0'))
309 return NSS_STATUS_UNAVAIL;
311 nip = nss_group_nonlocal_database();
313 return NSS_STATUS_UNAVAIL;
314 if (fct_start == NULL)
315 fct_start = __nss_lookup_function(nip, fct_name);
319 status = NSS_STATUS_UNAVAIL;
321 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
322 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
324 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
325 if (status != NSS_STATUS_SUCCESS)
328 return check_nonlocal_gid(name, grp->gr_gid, errnop);
332 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
333 char *buffer, size_t buflen, int *errnop)
335 static const char *fct_name = "getgrgid_r";
336 static void *fct_start = NULL;
337 enum nss_status status;
340 enum nss_status (*l)(gid_t gid, struct group *grp,
341 char *buffer, size_t buflen, int *errnop);
345 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
346 if (buflen == MAGIC_LOCAL_GR_BUFLEN ||
347 (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0'))
348 return NSS_STATUS_UNAVAIL;
350 nip = nss_group_nonlocal_database();
352 return NSS_STATUS_UNAVAIL;
353 if (fct_start == NULL)
354 fct_start = __nss_lookup_function(nip, fct_name);
358 status = NSS_STATUS_UNAVAIL;
360 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
361 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
363 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
364 if (status != NSS_STATUS_SUCCESS)
367 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
371 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
372 long int *size, gid_t **groupsp, long int limit,
375 static const char *fct_name = "initgroups_dyn";
376 static void *fct_start = NULL;
377 enum nss_status status;
380 enum nss_status (*l)(const char *user, gid_t group, long int *start,
381 long int *size, gid_t **groupsp, long int limit,
386 struct group local_users_group, nonlocal_users_group;
387 gid_t local_users_gid, gid;
392 /* Check that the user is a nonlocal user before adding any groups. */
393 status = check_nonlocal_user(user, errnop);
394 if (status == NSS_STATUS_TRYAGAIN)
396 else if (status != NSS_STATUS_SUCCESS)
399 int old_errno = errno;
401 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
402 buffer = malloc(buflen);
403 if (buffer == NULL) {
406 return NSS_STATUS_TRYAGAIN;
408 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
409 &local_users_group, buffer, buflen, errnop);
410 if (status == NSS_STATUS_SUCCESS) {
411 local_users_gid = local_users_group.gr_gid;
412 } else if (status == NSS_STATUS_TRYAGAIN) {
416 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
417 MAGIC_LOCAL_GROUPNAME);
418 local_users_gid = -1;
423 gid = local_users_gid;
425 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
426 buffer = malloc(buflen);
427 if (buffer == NULL) {
430 return NSS_STATUS_TRYAGAIN;
432 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
433 &nonlocal_users_group, buffer, buflen, errnop);
434 if (status == NSS_STATUS_SUCCESS) {
435 gid = nonlocal_users_group.gr_gid;
436 } else if (status == NSS_STATUS_TRYAGAIN) {
440 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
441 MAGIC_NONLOCAL_GROUPNAME);
449 for (i = 0; i < *start; ++i)
450 if ((*groupsp)[i] == gid)
453 if (*start + 1 > *size) {
455 long int newsize = 2 * *size;
458 return NSS_STATUS_SUCCESS;
462 newgroups = realloc(*groupsp, *size * sizeof((*groupsp)[0]));
463 if (newgroups == NULL) {
466 return NSS_STATUS_TRYAGAIN;
468 *groupsp = newgroups;
471 (*groupsp)[(*start)++] = gid;
476 return NSS_STATUS_SUCCESS;
478 int in = *start, out = *start, i;
480 nip = nss_group_nonlocal_database();
482 return NSS_STATUS_UNAVAIL;
483 if (fct_start == NULL)
484 fct_start = __nss_lookup_function(nip, fct_name);
489 status = NSS_STATUS_UNAVAIL;
491 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
492 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
494 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
495 if (status != NSS_STATUS_SUCCESS)
498 for (; in < *start; ++in) {
499 int nonlocal_errno = *errnop;
501 for (i = 0; i < out; ++i)
502 if ((*groupsp)[i] == (*groupsp)[in])
507 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
508 if (local_users_gid == (*groupsp)[in]) {
509 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
510 user, MAGIC_LOCAL_GROUPNAME);
514 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
515 if (status == NSS_STATUS_SUCCESS) {
516 (*groupsp)[out++] = (*groupsp)[in];
517 } else if (status == NSS_STATUS_TRYAGAIN) {
519 *errnop = nonlocal_errno;
525 return NSS_STATUS_SUCCESS;