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"
42 * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal
43 * users will be automatically added to it. Furthermore, if a local
44 * user is added to this group, then that user will inherit any
45 * nonlocal supplementary gids from a nonlocal user of the same name.
47 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
50 * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users
51 * will be automatically added to it.
53 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
57 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
58 char *buffer, size_t buflen, int *errnop);
61 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
62 char *buffer, size_t buflen, int *errnop);
65 static service_user *__nss_group_nonlocal_database;
69 __nss_group_nonlocal_lookup(service_user **ni, const char *fct_name,
72 if (__nss_group_nonlocal_database == NULL
73 && __nss_database_lookup("group_nonlocal", NULL, NULL,
74 &__nss_group_nonlocal_database) < 0)
77 *ni = __nss_group_nonlocal_database;
79 *fctp = __nss_lookup_function(*ni, fct_name);
85 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
87 enum nss_status status;
90 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
91 const struct walk_nss w = {
92 .lookup = &__nss_group_lookup, .fct_name = "getgrgid_r",
93 .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
95 const __typeof__(&_nss_nonlocal_getgrgid_r) self = &_nss_nonlocal_getgrgid_r;
96 #define args (gid, &gbuf, buf, buflen, errnop)
100 if (status == NSS_STATUS_SUCCESS) {
101 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
103 status = NSS_STATUS_NOTFOUND;
104 } else if (status != NSS_STATUS_TRYAGAIN) {
105 status = NSS_STATUS_SUCCESS;
112 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
114 enum nss_status status = NSS_STATUS_SUCCESS;
115 int old_errno = errno;
120 gid = strtoul(grp->gr_name, &end, 10);
121 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
123 status = check_nonlocal_gid(user, gid, errnop);
126 if (status != NSS_STATUS_SUCCESS)
129 return check_nonlocal_gid(user, grp->gr_gid, errnop);
133 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
135 enum nss_status status;
136 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
137 const struct walk_nss w = {
138 .lookup = &__nss_group_lookup, .fct_name = "getgrnam_r",
139 .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen
141 const __typeof__(&_nss_nonlocal_getgrnam_r) self = &_nss_nonlocal_getgrnam_r;
142 #define args (name, grp, *buffer, buflen, errnop)
143 #include "walk_nss.h"
148 static service_user *grent_startp, *grent_nip;
149 static void *grent_fct_start;
151 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
155 static const char *grent_fct_name = "getgrent_r";
158 _nss_nonlocal_setgrent(int stayopen)
160 enum nss_status status;
161 const struct walk_nss w = {
162 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "setgrent",
165 const __typeof__(&_nss_nonlocal_setgrent) self = NULL;
166 #define args (stayopen)
167 #include "walk_nss.h"
169 if (status != NSS_STATUS_SUCCESS)
172 if (grent_fct_start == NULL)
173 __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
175 grent_nip = grent_startp;
176 grent_fct.ptr = grent_fct_start;
177 return NSS_STATUS_SUCCESS;
181 _nss_nonlocal_endgrent(void)
183 enum nss_status status;
184 const struct walk_nss w = {
185 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "endgrent",
188 const __typeof__(&_nss_nonlocal_endgrent) self = NULL;
193 #include "walk_nss.h"
199 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
202 enum nss_status status;
204 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
205 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
206 return NSS_STATUS_UNAVAIL;
208 if (grent_nip == NULL) {
209 status = _nss_nonlocal_setgrent(0);
210 if (status != NSS_STATUS_SUCCESS)
214 if (grent_fct.ptr == NULL)
215 status = NSS_STATUS_UNAVAIL;
219 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
220 while (status == NSS_STATUS_SUCCESS &&
221 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
223 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
226 if (status == NSS_STATUS_SUCCESS)
227 return NSS_STATUS_SUCCESS;
228 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
231 return NSS_STATUS_NOTFOUND;
236 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
237 char *buffer, size_t buflen, int *errnop)
239 enum nss_status status;
240 const struct walk_nss w = {
241 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrnam_r",
242 .status = &status, .errnop = errnop
244 const __typeof__(&_nss_nonlocal_getgrnam_r) self = NULL;
246 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
247 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
248 return NSS_STATUS_UNAVAIL;
250 #define args (name, grp, buffer, buflen, errnop)
251 #include "walk_nss.h"
253 if (status != NSS_STATUS_SUCCESS)
256 if (strcmp(name, grp->gr_name) != 0) {
257 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
258 return NSS_STATUS_NOTFOUND;
261 return check_nonlocal_group(name, grp, errnop);
265 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
266 char *buffer, size_t buflen, int *errnop)
268 enum nss_status status;
269 const struct walk_nss w = {
270 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrgid_r",
271 .status = &status, .errnop = errnop
273 const __typeof__(&_nss_nonlocal_getgrgid_r) self = NULL;
275 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
276 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
277 return NSS_STATUS_UNAVAIL;
279 #define args (gid, grp, buffer, buflen, errnop)
280 #include "walk_nss.h"
282 if (status != NSS_STATUS_SUCCESS)
285 if (gid != grp->gr_gid) {
286 syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
287 return NSS_STATUS_NOTFOUND;
290 return check_nonlocal_group(grp->gr_name, grp, errnop);
294 add_group(gid_t group, long int *start, long int *size, gid_t **groupsp,
295 long int limit, int *errnop, enum nss_status *status)
297 int i, old_errno = errno;
298 for (i = 0; i < *start; ++i)
299 if ((*groupsp)[i] == group)
301 if (*start + 1 > *size) {
303 long int newsize = 2 * *size;
305 if (*size >= limit) {
306 *status = NSS_STATUS_SUCCESS;
312 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
314 if (newgroups == NULL) {
316 *status = NSS_STATUS_TRYAGAIN;
319 *groupsp = newgroups;
322 (*groupsp)[(*start)++] = group;
327 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
328 long int *size, gid_t **groupsp, long int limit,
331 enum nss_status status;
332 const struct walk_nss w = {
333 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "initgroups_dyn",
334 .status = &status, .errnop = errnop
336 const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL;
338 struct group local_users_group, nonlocal_users_group;
339 bool is_nonlocal = true;
343 /* Check that the user is a nonlocal user, or a member of the
344 * MAGIC_NONLOCAL_GROUPNAME group, before adding any groups. */
345 status = check_nonlocal_user(user, errnop);
346 if (status == NSS_STATUS_TRYAGAIN) {
348 } else if (status != NSS_STATUS_SUCCESS) {
351 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
352 &local_users_group, &buffer, errnop);
353 if (status == NSS_STATUS_SUCCESS) {
355 if (!add_group(local_users_group.gr_gid, start, size, groupsp,
356 limit, errnop, &status))
358 } else if (status == NSS_STATUS_TRYAGAIN) {
362 "nss_nonlocal: Group %s does not exist locally!",
363 MAGIC_LOCAL_GROUPNAME);
367 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
368 &nonlocal_users_group, &buffer, errnop);
369 if (status == NSS_STATUS_SUCCESS) {
372 if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
373 limit, errnop, &status))
377 for (i = 0; i < *start; ++i) {
378 if ((*groupsp)[i] == nonlocal_users_group.gr_gid) {
384 } else if (status == NSS_STATUS_TRYAGAIN) {
388 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
389 MAGIC_NONLOCAL_GROUPNAME);
393 return NSS_STATUS_SUCCESS;
397 #define args (user, group, start, size, groupsp, limit, errnop)
398 #include "walk_nss.h"
400 if (status != NSS_STATUS_SUCCESS)
403 for (; in < *start; ++in) {
404 int nonlocal_errno = *errnop;
406 for (i = 0; i < out; ++i)
407 if ((*groupsp)[i] == (*groupsp)[in])
412 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
413 if (status == NSS_STATUS_SUCCESS) {
414 (*groupsp)[out++] = (*groupsp)[in];
415 } else if (status == NSS_STATUS_TRYAGAIN) {
417 *errnop = nonlocal_errno;
423 return NSS_STATUS_SUCCESS;