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_NONLOCAL_GROUPNAME "nss-nonlocal-users"
45 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
49 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
50 char *buffer, size_t buflen, int *errnop);
53 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
54 char *buffer, size_t buflen, int *errnop);
58 nss_group_nonlocal_database(void)
60 static service_user *nip = NULL;
62 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
69 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
71 static const char *fct_name = "getgrgid_r";
72 static service_user *startp = NULL;
73 static void *fct_start = NULL;
74 enum nss_status status;
77 enum nss_status (*l)(gid_t gid, struct group *grp,
78 char *buffer, size_t buflen, int *errnop);
82 int old_errno = errno;
84 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
85 char *buf = malloc(buflen);
89 return NSS_STATUS_TRYAGAIN;
92 if (fct_start == NULL &&
93 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
95 return NSS_STATUS_UNAVAIL;
101 if (fct.l == _nss_nonlocal_getgrgid_r)
102 status = NSS_STATUS_NOTFOUND;
104 status = DL_CALL_FCT(fct.l, (gid, &gbuf, buf, buflen, errnop));
105 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
108 buf = malloc(buflen);
112 return NSS_STATUS_TRYAGAIN;
116 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
118 if (status == NSS_STATUS_SUCCESS) {
119 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
120 status = NSS_STATUS_NOTFOUND;
121 } else if (status != NSS_STATUS_TRYAGAIN) {
122 status = NSS_STATUS_SUCCESS;
130 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
132 return check_nonlocal_gid(user, grp->gr_gid, errnop);
136 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
138 static const char *fct_name = "getgrnam_r";
139 static service_user *startp = NULL;
140 static void *fct_start = NULL;
141 enum nss_status status;
144 enum nss_status (*l)(const char *name, struct group *grp,
145 char *buffer, size_t buflen, int *errnop);
149 int old_errno = errno;
151 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
152 *buffer = malloc(buflen);
153 if (*buffer == NULL) {
156 return NSS_STATUS_TRYAGAIN;
159 if (fct_start == NULL &&
160 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
163 return NSS_STATUS_UNAVAIL;
169 if (fct.l == _nss_nonlocal_getgrnam_r)
170 status = NSS_STATUS_NOTFOUND;
172 status = DL_CALL_FCT(fct.l, (name, grp, *buffer, buflen, errnop));
173 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
176 *buffer = malloc(buflen);
177 if (*buffer == NULL) {
180 return NSS_STATUS_TRYAGAIN;
184 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
186 if (status != NSS_STATUS_SUCCESS) {
194 static service_user *grent_nip = NULL;
195 static void *grent_fct_start;
197 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
201 static const char *grent_fct_name = "getgrent_r";
204 _nss_nonlocal_setgrent(int stayopen)
206 static const char *fct_name = "setgrent";
207 static void *fct_start = NULL;
208 enum nss_status status;
211 enum nss_status (*l)(int stayopen);
215 nip = nss_group_nonlocal_database();
217 return NSS_STATUS_UNAVAIL;
218 if (fct_start == NULL)
219 fct_start = __nss_lookup_function(nip, fct_name);
223 status = NSS_STATUS_UNAVAIL;
225 status = DL_CALL_FCT(fct.l, (stayopen));
226 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
227 if (status != NSS_STATUS_SUCCESS)
231 if (grent_fct_start == NULL)
232 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
233 grent_fct.ptr = grent_fct_start;
234 return NSS_STATUS_SUCCESS;
238 _nss_nonlocal_endgrent(void)
240 static const char *fct_name = "endgrent";
241 static void *fct_start = NULL;
242 enum nss_status status;
245 enum nss_status (*l)(void);
251 nip = nss_group_nonlocal_database();
253 return NSS_STATUS_UNAVAIL;
254 if (fct_start == NULL)
255 fct_start = __nss_lookup_function(nip, fct_name);
259 status = NSS_STATUS_UNAVAIL;
261 status = DL_CALL_FCT(fct.l, ());
262 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
267 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
270 enum nss_status status;
272 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
273 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
274 return NSS_STATUS_UNAVAIL;
276 if (grent_nip == NULL) {
277 status = _nss_nonlocal_setgrent(0);
278 if (status != NSS_STATUS_SUCCESS)
282 if (grent_fct.ptr == NULL)
283 status = NSS_STATUS_UNAVAIL;
287 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
288 while (status == NSS_STATUS_SUCCESS &&
289 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
291 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
294 if (status == NSS_STATUS_SUCCESS)
295 return NSS_STATUS_SUCCESS;
296 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
299 return NSS_STATUS_NOTFOUND;
304 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
305 char *buffer, size_t buflen, int *errnop)
307 static const char *fct_name = "getgrnam_r";
308 static void *fct_start = NULL;
309 enum nss_status status;
312 enum nss_status (*l)(const char *name, struct group *grp,
313 char *buffer, size_t buflen, int *errnop);
317 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
318 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
319 return NSS_STATUS_UNAVAIL;
321 nip = nss_group_nonlocal_database();
323 return NSS_STATUS_UNAVAIL;
324 if (fct_start == NULL)
325 fct_start = __nss_lookup_function(nip, fct_name);
329 status = NSS_STATUS_UNAVAIL;
331 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
332 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
334 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
335 if (status != NSS_STATUS_SUCCESS)
338 if (strcmp(name, grp->gr_name) != 0) {
339 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
340 return NSS_STATUS_NOTFOUND;
343 return check_nonlocal_group(name, grp, errnop);
347 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
348 char *buffer, size_t buflen, int *errnop)
350 static const char *fct_name = "getgrgid_r";
351 static void *fct_start = NULL;
352 enum nss_status status;
355 enum nss_status (*l)(gid_t gid, struct group *grp,
356 char *buffer, size_t buflen, int *errnop);
360 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
361 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
362 return NSS_STATUS_UNAVAIL;
364 nip = nss_group_nonlocal_database();
366 return NSS_STATUS_UNAVAIL;
367 if (fct_start == NULL)
368 fct_start = __nss_lookup_function(nip, fct_name);
372 status = NSS_STATUS_UNAVAIL;
374 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
375 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
377 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
378 if (status != NSS_STATUS_SUCCESS)
381 return check_nonlocal_group(grp->gr_name, grp, errnop);
385 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
386 long int *size, gid_t **groupsp, long int limit,
389 static const char *fct_name = "initgroups_dyn";
390 static void *fct_start = NULL;
391 enum nss_status status;
394 enum nss_status (*l)(const char *user, gid_t group, long int *start,
395 long int *size, gid_t **groupsp, long int limit,
400 struct group local_users_group, nonlocal_users_group;
401 gid_t local_users_gid, gid;
407 /* Check that the user is a nonlocal user before adding any groups. */
408 status = check_nonlocal_user(user, errnop);
409 if (status == NSS_STATUS_TRYAGAIN)
411 else if (status != NSS_STATUS_SUCCESS)
416 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
417 &local_users_group, &buffer, errnop);
418 if (status == NSS_STATUS_SUCCESS) {
419 local_users_gid = local_users_group.gr_gid;
421 } else if (status == NSS_STATUS_TRYAGAIN) {
424 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
425 MAGIC_LOCAL_GROUPNAME);
426 local_users_gid = -1;
430 gid = local_users_gid;
432 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
433 &nonlocal_users_group, &buffer, errnop);
434 if (status == NSS_STATUS_SUCCESS) {
435 gid = nonlocal_users_group.gr_gid;
437 } else if (status == NSS_STATUS_TRYAGAIN) {
440 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
441 MAGIC_NONLOCAL_GROUPNAME);
448 for (i = 0; i < *start; ++i)
449 if ((*groupsp)[i] == gid)
452 if (*start + 1 > *size) {
454 long int newsize = 2 * *size;
457 return NSS_STATUS_SUCCESS;
461 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
462 if (newgroups == NULL) {
465 return NSS_STATUS_TRYAGAIN;
467 *groupsp = newgroups;
470 (*groupsp)[(*start)++] = gid;
475 return NSS_STATUS_SUCCESS;
479 nip = nss_group_nonlocal_database();
481 return NSS_STATUS_UNAVAIL;
482 if (fct_start == NULL)
483 fct_start = __nss_lookup_function(nip, fct_name);
488 status = NSS_STATUS_UNAVAIL;
490 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
491 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
493 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
494 if (status != NSS_STATUS_SUCCESS)
497 for (; in < *start; ++in) {
498 int nonlocal_errno = *errnop;
500 for (i = 0; i < out; ++i)
501 if ((*groupsp)[i] == (*groupsp)[in])
506 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
507 if (local_users_gid == (*groupsp)[in]) {
508 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
509 user, MAGIC_LOCAL_GROUPNAME);
513 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
514 if (status == NSS_STATUS_SUCCESS) {
515 (*groupsp)[out++] = (*groupsp)[in];
516 } else if (status == NSS_STATUS_TRYAGAIN) {
518 *errnop = nonlocal_errno;
524 return NSS_STATUS_SUCCESS;