3 * group database for nss_nonlocal proxy
5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> and Tim
6 * Abbott <tabbott@mit.edu>
8 * This file is part of nss_nonlocal.
10 * nss_nonlocal is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1 of
13 * the License, or (at your option) any later version.
15 * nss_nonlocal is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with nss_nonlocal; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <sys/types.h>
38 #include "nsswitch-internal.h"
41 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
42 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
46 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
47 char *buffer, size_t buflen, int *errnop);
50 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
51 char *buffer, size_t buflen, int *errnop);
55 nss_group_nonlocal_database(void)
57 static service_user *nip = NULL;
59 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
66 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
68 static const char *fct_name = "getgrgid_r";
69 static service_user *startp = NULL;
70 static void *fct_start = NULL;
71 enum nss_status status;
74 enum nss_status (*l)(gid_t gid, struct group *grp,
75 char *buffer, size_t buflen, int *errnop);
79 int old_errno = errno;
81 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
82 char *buf = malloc(buflen);
86 return NSS_STATUS_TRYAGAIN;
89 if (fct_start == NULL &&
90 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
92 return NSS_STATUS_UNAVAIL;
98 if (fct.l == _nss_nonlocal_getgrgid_r)
99 status = NSS_STATUS_NOTFOUND;
101 status = DL_CALL_FCT(fct.l, (gid, &gbuf, buf, buflen, errnop));
102 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
105 buf = malloc(buflen);
109 return NSS_STATUS_TRYAGAIN;
113 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
115 if (status == NSS_STATUS_SUCCESS) {
116 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
117 status = NSS_STATUS_NOTFOUND;
118 } else if (status != NSS_STATUS_TRYAGAIN) {
119 status = NSS_STATUS_SUCCESS;
127 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
129 enum nss_status status = NSS_STATUS_SUCCESS;
130 int old_errno = errno;
135 gid = strtoul(grp->gr_name, &end, 10);
136 if (errno == 0 && *end == '\0' && (gid_t)gid == gid)
137 status = check_nonlocal_gid(user, gid, errnop);
139 if (status != NSS_STATUS_SUCCESS)
142 return check_nonlocal_gid(user, grp->gr_gid, errnop);
146 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
148 static const char *fct_name = "getgrnam_r";
149 static service_user *startp = NULL;
150 static void *fct_start = NULL;
151 enum nss_status status;
154 enum nss_status (*l)(const char *name, struct group *grp,
155 char *buffer, size_t buflen, int *errnop);
159 int old_errno = errno;
161 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
162 *buffer = malloc(buflen);
163 if (*buffer == NULL) {
166 return NSS_STATUS_TRYAGAIN;
169 if (fct_start == NULL &&
170 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
173 return NSS_STATUS_UNAVAIL;
179 if (fct.l == _nss_nonlocal_getgrnam_r)
180 status = NSS_STATUS_NOTFOUND;
182 status = DL_CALL_FCT(fct.l, (name, grp, *buffer, buflen, errnop));
183 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
186 *buffer = malloc(buflen);
187 if (*buffer == NULL) {
190 return NSS_STATUS_TRYAGAIN;
194 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
196 if (status != NSS_STATUS_SUCCESS) {
204 static service_user *grent_nip = NULL;
205 static void *grent_fct_start;
207 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
211 static const char *grent_fct_name = "getgrent_r";
214 _nss_nonlocal_setgrent(int stayopen)
216 static const char *fct_name = "setgrent";
217 static void *fct_start = NULL;
218 enum nss_status status;
221 enum nss_status (*l)(int stayopen);
225 nip = nss_group_nonlocal_database();
227 return NSS_STATUS_UNAVAIL;
228 if (fct_start == NULL)
229 fct_start = __nss_lookup_function(nip, fct_name);
233 status = NSS_STATUS_UNAVAIL;
235 status = DL_CALL_FCT(fct.l, (stayopen));
236 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
237 if (status != NSS_STATUS_SUCCESS)
241 if (grent_fct_start == NULL)
242 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
243 grent_fct.ptr = grent_fct_start;
244 return NSS_STATUS_SUCCESS;
248 _nss_nonlocal_endgrent(void)
250 static const char *fct_name = "endgrent";
251 static void *fct_start = NULL;
252 enum nss_status status;
255 enum nss_status (*l)(void);
261 nip = nss_group_nonlocal_database();
263 return NSS_STATUS_UNAVAIL;
264 if (fct_start == NULL)
265 fct_start = __nss_lookup_function(nip, fct_name);
269 status = NSS_STATUS_UNAVAIL;
271 status = DL_CALL_FCT(fct.l, ());
272 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
277 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
280 enum nss_status status;
282 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
283 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
284 return NSS_STATUS_UNAVAIL;
286 if (grent_nip == NULL) {
287 status = _nss_nonlocal_setgrent(0);
288 if (status != NSS_STATUS_SUCCESS)
292 if (grent_fct.ptr == NULL)
293 status = NSS_STATUS_UNAVAIL;
297 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
298 while (status == NSS_STATUS_SUCCESS &&
299 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
301 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
304 if (status == NSS_STATUS_SUCCESS)
305 return NSS_STATUS_SUCCESS;
306 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
309 return NSS_STATUS_NOTFOUND;
314 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
315 char *buffer, size_t buflen, int *errnop)
317 static const char *fct_name = "getgrnam_r";
318 static void *fct_start = NULL;
319 enum nss_status status;
322 enum nss_status (*l)(const char *name, struct group *grp,
323 char *buffer, size_t buflen, int *errnop);
327 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
328 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
329 return NSS_STATUS_UNAVAIL;
331 nip = nss_group_nonlocal_database();
333 return NSS_STATUS_UNAVAIL;
334 if (fct_start == NULL)
335 fct_start = __nss_lookup_function(nip, fct_name);
339 status = NSS_STATUS_UNAVAIL;
341 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
342 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
344 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
345 if (status != NSS_STATUS_SUCCESS)
348 if (strcmp(name, grp->gr_name) != 0) {
349 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
350 return NSS_STATUS_NOTFOUND;
353 return check_nonlocal_group(name, grp, errnop);
357 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
358 char *buffer, size_t buflen, int *errnop)
360 static const char *fct_name = "getgrgid_r";
361 static void *fct_start = NULL;
362 enum nss_status status;
365 enum nss_status (*l)(gid_t gid, struct group *grp,
366 char *buffer, size_t buflen, int *errnop);
370 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
371 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
372 return NSS_STATUS_UNAVAIL;
374 nip = nss_group_nonlocal_database();
376 return NSS_STATUS_UNAVAIL;
377 if (fct_start == NULL)
378 fct_start = __nss_lookup_function(nip, fct_name);
382 status = NSS_STATUS_UNAVAIL;
384 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
385 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
387 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
388 if (status != NSS_STATUS_SUCCESS)
391 if (gid != grp->gr_gid) {
392 syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
393 return NSS_STATUS_NOTFOUND;
396 return check_nonlocal_group(grp->gr_name, grp, errnop);
400 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
401 long int *size, gid_t **groupsp, long int limit,
404 static const char *fct_name = "initgroups_dyn";
405 static void *fct_start = NULL;
406 enum nss_status status;
409 enum nss_status (*l)(const char *user, gid_t group, long int *start,
410 long int *size, gid_t **groupsp, long int limit,
415 struct group local_users_group, nonlocal_users_group;
416 gid_t local_users_gid, gid;
422 /* Check that the user is a nonlocal user before adding any groups. */
423 status = check_nonlocal_user(user, errnop);
424 if (status == NSS_STATUS_TRYAGAIN)
426 else if (status != NSS_STATUS_SUCCESS)
431 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
432 &local_users_group, &buffer, errnop);
433 if (status == NSS_STATUS_SUCCESS) {
434 local_users_gid = local_users_group.gr_gid;
436 } else if (status == NSS_STATUS_TRYAGAIN) {
439 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
440 MAGIC_LOCAL_GROUPNAME);
441 local_users_gid = -1;
445 gid = local_users_gid;
447 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
448 &nonlocal_users_group, &buffer, errnop);
449 if (status == NSS_STATUS_SUCCESS) {
450 gid = nonlocal_users_group.gr_gid;
452 } else if (status == NSS_STATUS_TRYAGAIN) {
455 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
456 MAGIC_NONLOCAL_GROUPNAME);
463 for (i = 0; i < *start; ++i)
464 if ((*groupsp)[i] == gid)
467 if (*start + 1 > *size) {
469 long int newsize = 2 * *size;
472 return NSS_STATUS_SUCCESS;
476 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
477 if (newgroups == NULL) {
480 return NSS_STATUS_TRYAGAIN;
482 *groupsp = newgroups;
485 (*groupsp)[(*start)++] = gid;
490 return NSS_STATUS_SUCCESS;
494 nip = nss_group_nonlocal_database();
496 return NSS_STATUS_UNAVAIL;
497 if (fct_start == NULL)
498 fct_start = __nss_lookup_function(nip, fct_name);
503 status = NSS_STATUS_UNAVAIL;
505 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
506 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
508 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
509 if (status != NSS_STATUS_SUCCESS)
512 for (; in < *start; ++in) {
513 int nonlocal_errno = *errnop;
515 for (i = 0; i < out; ++i)
516 if ((*groupsp)[i] == (*groupsp)[in])
521 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
522 if (local_users_gid == (*groupsp)[in]) {
523 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
524 user, MAGIC_LOCAL_GROUPNAME);
528 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
529 if (status == NSS_STATUS_SUCCESS) {
530 (*groupsp)[out++] = (*groupsp)[in];
531 } else if (status == NSS_STATUS_TRYAGAIN) {
533 *errnop = nonlocal_errno;
539 return NSS_STATUS_SUCCESS;