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) {
138 status = check_nonlocal_gid(user, gid, errnop);
141 if (status != NSS_STATUS_SUCCESS)
144 return check_nonlocal_gid(user, grp->gr_gid, errnop);
148 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
150 static const char *fct_name = "getgrnam_r";
151 static service_user *startp = NULL;
152 static void *fct_start = NULL;
153 enum nss_status status;
156 enum nss_status (*l)(const char *name, struct group *grp,
157 char *buffer, size_t buflen, int *errnop);
161 int old_errno = errno;
163 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
164 *buffer = malloc(buflen);
166 if (*buffer == NULL) {
168 return NSS_STATUS_TRYAGAIN;
171 if (fct_start == NULL &&
172 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
175 return NSS_STATUS_UNAVAIL;
181 if (fct.l == _nss_nonlocal_getgrnam_r)
182 status = NSS_STATUS_NOTFOUND;
184 status = DL_CALL_FCT(fct.l, (name, grp, *buffer, buflen, errnop));
185 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
188 *buffer = malloc(buflen);
190 if (*buffer == NULL) {
192 return NSS_STATUS_TRYAGAIN;
196 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
198 if (status != NSS_STATUS_SUCCESS) {
206 static service_user *grent_nip = NULL;
207 static void *grent_fct_start;
209 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
213 static const char *grent_fct_name = "getgrent_r";
216 _nss_nonlocal_setgrent(int stayopen)
218 static const char *fct_name = "setgrent";
219 static void *fct_start = NULL;
220 enum nss_status status;
223 enum nss_status (*l)(int stayopen);
227 nip = nss_group_nonlocal_database();
229 return NSS_STATUS_UNAVAIL;
230 if (fct_start == NULL)
231 fct_start = __nss_lookup_function(nip, fct_name);
235 status = NSS_STATUS_UNAVAIL;
237 status = DL_CALL_FCT(fct.l, (stayopen));
238 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
239 if (status != NSS_STATUS_SUCCESS)
243 if (grent_fct_start == NULL)
244 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
245 grent_fct.ptr = grent_fct_start;
246 return NSS_STATUS_SUCCESS;
250 _nss_nonlocal_endgrent(void)
252 static const char *fct_name = "endgrent";
253 static void *fct_start = NULL;
254 enum nss_status status;
257 enum nss_status (*l)(void);
263 nip = nss_group_nonlocal_database();
265 return NSS_STATUS_UNAVAIL;
266 if (fct_start == NULL)
267 fct_start = __nss_lookup_function(nip, fct_name);
271 status = NSS_STATUS_UNAVAIL;
273 status = DL_CALL_FCT(fct.l, ());
274 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
279 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
282 enum nss_status status;
284 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
285 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
286 return NSS_STATUS_UNAVAIL;
288 if (grent_nip == NULL) {
289 status = _nss_nonlocal_setgrent(0);
290 if (status != NSS_STATUS_SUCCESS)
294 if (grent_fct.ptr == NULL)
295 status = NSS_STATUS_UNAVAIL;
299 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
300 while (status == NSS_STATUS_SUCCESS &&
301 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
303 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
306 if (status == NSS_STATUS_SUCCESS)
307 return NSS_STATUS_SUCCESS;
308 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
311 return NSS_STATUS_NOTFOUND;
316 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
317 char *buffer, size_t buflen, int *errnop)
319 static const char *fct_name = "getgrnam_r";
320 static void *fct_start = NULL;
321 enum nss_status status;
324 enum nss_status (*l)(const char *name, struct group *grp,
325 char *buffer, size_t buflen, int *errnop);
329 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
330 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
331 return NSS_STATUS_UNAVAIL;
333 nip = nss_group_nonlocal_database();
335 return NSS_STATUS_UNAVAIL;
336 if (fct_start == NULL)
337 fct_start = __nss_lookup_function(nip, fct_name);
341 status = NSS_STATUS_UNAVAIL;
343 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
344 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
346 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
347 if (status != NSS_STATUS_SUCCESS)
350 if (strcmp(name, grp->gr_name) != 0) {
351 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
352 return NSS_STATUS_NOTFOUND;
355 return check_nonlocal_group(name, grp, errnop);
359 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
360 char *buffer, size_t buflen, int *errnop)
362 static const char *fct_name = "getgrgid_r";
363 static void *fct_start = NULL;
364 enum nss_status status;
367 enum nss_status (*l)(gid_t gid, struct group *grp,
368 char *buffer, size_t buflen, int *errnop);
372 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
373 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
374 return NSS_STATUS_UNAVAIL;
376 nip = nss_group_nonlocal_database();
378 return NSS_STATUS_UNAVAIL;
379 if (fct_start == NULL)
380 fct_start = __nss_lookup_function(nip, fct_name);
384 status = NSS_STATUS_UNAVAIL;
386 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
387 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
389 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
390 if (status != NSS_STATUS_SUCCESS)
393 if (gid != grp->gr_gid) {
394 syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
395 return NSS_STATUS_NOTFOUND;
398 return check_nonlocal_group(grp->gr_name, grp, errnop);
402 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
403 long int *size, gid_t **groupsp, long int limit,
406 static const char *fct_name = "initgroups_dyn";
407 static void *fct_start = NULL;
408 enum nss_status status;
411 enum nss_status (*l)(const char *user, gid_t group, long int *start,
412 long int *size, gid_t **groupsp, long int limit,
417 struct group local_users_group, nonlocal_users_group;
418 gid_t local_users_gid, gid;
424 /* Check that the user is a nonlocal user before adding any groups. */
425 status = check_nonlocal_user(user, errnop);
426 if (status == NSS_STATUS_TRYAGAIN)
428 else if (status != NSS_STATUS_SUCCESS)
433 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
434 &local_users_group, &buffer, errnop);
435 if (status == NSS_STATUS_SUCCESS) {
436 local_users_gid = local_users_group.gr_gid;
438 } else if (status == NSS_STATUS_TRYAGAIN) {
441 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
442 MAGIC_LOCAL_GROUPNAME);
443 local_users_gid = -1;
447 gid = local_users_gid;
449 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
450 &nonlocal_users_group, &buffer, errnop);
451 if (status == NSS_STATUS_SUCCESS) {
452 gid = nonlocal_users_group.gr_gid;
454 } else if (status == NSS_STATUS_TRYAGAIN) {
457 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
458 MAGIC_NONLOCAL_GROUPNAME);
465 for (i = 0; i < *start; ++i)
466 if ((*groupsp)[i] == gid)
469 if (*start + 1 > *size) {
471 long int newsize = 2 * *size;
474 return NSS_STATUS_SUCCESS;
478 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
480 if (newgroups == NULL) {
482 return NSS_STATUS_TRYAGAIN;
484 *groupsp = newgroups;
487 (*groupsp)[(*start)++] = gid;
492 return NSS_STATUS_SUCCESS;
496 nip = nss_group_nonlocal_database();
498 return NSS_STATUS_UNAVAIL;
499 if (fct_start == NULL)
500 fct_start = __nss_lookup_function(nip, fct_name);
505 status = NSS_STATUS_UNAVAIL;
507 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
508 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
510 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
511 if (status != NSS_STATUS_SUCCESS)
514 for (; in < *start; ++in) {
515 int nonlocal_errno = *errnop;
517 for (i = 0; i < out; ++i)
518 if ((*groupsp)[i] == (*groupsp)[in])
523 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
524 if (local_users_gid == (*groupsp)[in]) {
525 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
526 user, MAGIC_LOCAL_GROUPNAME);
530 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
531 if (status == NSS_STATUS_SUCCESS) {
532 (*groupsp)[out++] = (*groupsp)[in];
533 } else if (status == NSS_STATUS_TRYAGAIN) {
535 *errnop = nonlocal_errno;
541 return NSS_STATUS_SUCCESS;