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>
39 #include "nsswitch-internal.h"
43 * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal
44 * users will be automatically added to it. Furthermore, if a local
45 * user is added to this group, then that user will inherit any
46 * nonlocal gids from a nonlocal user of the same name, as
49 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
52 * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users
53 * will be automatically added to it.
55 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
59 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
60 char *buffer, size_t buflen, int *errnop);
63 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
64 char *buffer, size_t buflen, int *errnop);
67 static service_user *__nss_group_nonlocal_database;
71 __nss_group_nonlocal_lookup(service_user **ni, const char *fct_name,
74 if (__nss_group_nonlocal_database == NULL
75 && __nss_database_lookup("group_nonlocal", NULL, NULL,
76 &__nss_group_nonlocal_database) < 0)
79 *ni = __nss_group_nonlocal_database;
81 *fctp = __nss_lookup_function(*ni, fct_name);
87 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
89 enum nss_status status;
92 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
93 const struct walk_nss w = {
94 .lookup = &__nss_group_lookup, .fct_name = "getgrgid_r",
95 .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
97 const __typeof__(&_nss_nonlocal_getgrgid_r) self = &_nss_nonlocal_getgrgid_r;
98 #define args (gid, &gbuf, buf, buflen, errnop)
102 if (status == NSS_STATUS_SUCCESS) {
103 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
105 status = NSS_STATUS_NOTFOUND;
106 } else if (status != NSS_STATUS_TRYAGAIN) {
107 status = NSS_STATUS_SUCCESS;
114 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
116 enum nss_status status = NSS_STATUS_SUCCESS;
117 int old_errno = errno;
122 gid = strtoul(grp->gr_name, &end, 10);
123 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
125 status = check_nonlocal_gid(user, gid, errnop);
128 if (status != NSS_STATUS_SUCCESS)
131 return check_nonlocal_gid(user, grp->gr_gid, errnop);
135 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
137 enum nss_status status;
138 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
139 const struct walk_nss w = {
140 .lookup = &__nss_group_lookup, .fct_name = "getgrnam_r",
141 .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen
143 const __typeof__(&_nss_nonlocal_getgrnam_r) self = &_nss_nonlocal_getgrnam_r;
144 #define args (name, grp, *buffer, buflen, errnop)
145 #include "walk_nss.h"
150 static service_user *grent_startp, *grent_nip;
151 static void *grent_fct_start;
153 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
157 static const char *grent_fct_name = "getgrent_r";
160 _nss_nonlocal_setgrent(int stayopen)
162 enum nss_status status;
163 const struct walk_nss w = {
164 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "setgrent",
167 const __typeof__(&_nss_nonlocal_setgrent) self = NULL;
168 #define args (stayopen)
169 #include "walk_nss.h"
171 if (status != NSS_STATUS_SUCCESS)
174 if (grent_fct_start == NULL)
175 __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
177 grent_nip = grent_startp;
178 grent_fct.ptr = grent_fct_start;
179 return NSS_STATUS_SUCCESS;
183 _nss_nonlocal_endgrent(void)
185 enum nss_status status;
186 const struct walk_nss w = {
187 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "endgrent",
190 const __typeof__(&_nss_nonlocal_endgrent) self = NULL;
195 #include "walk_nss.h"
201 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
204 enum nss_status status;
206 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
207 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
208 return NSS_STATUS_UNAVAIL;
210 if (grent_nip == NULL) {
211 status = _nss_nonlocal_setgrent(0);
212 if (status != NSS_STATUS_SUCCESS)
216 if (grent_fct.ptr == NULL)
217 status = NSS_STATUS_UNAVAIL;
221 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
222 while (status == NSS_STATUS_SUCCESS &&
223 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
225 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
228 if (status == NSS_STATUS_SUCCESS)
229 return NSS_STATUS_SUCCESS;
230 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
233 return NSS_STATUS_NOTFOUND;
238 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
239 char *buffer, size_t buflen, int *errnop)
241 enum nss_status status;
242 const struct walk_nss w = {
243 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrnam_r",
244 .status = &status, .errnop = errnop
246 const __typeof__(&_nss_nonlocal_getgrnam_r) self = NULL;
248 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
249 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
250 return NSS_STATUS_UNAVAIL;
252 #define args (name, grp, buffer, buflen, errnop)
253 #include "walk_nss.h"
255 if (status != NSS_STATUS_SUCCESS)
258 if (strcmp(name, grp->gr_name) != 0) {
259 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
260 return NSS_STATUS_NOTFOUND;
263 return check_nonlocal_group(name, grp, errnop);
267 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
268 char *buffer, size_t buflen, int *errnop)
270 enum nss_status status;
271 const struct walk_nss w = {
272 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrgid_r",
273 .status = &status, .errnop = errnop
275 const __typeof__(&_nss_nonlocal_getgrgid_r) self = NULL;
277 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
278 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
279 return NSS_STATUS_UNAVAIL;
281 #define args (gid, grp, buffer, buflen, errnop)
282 #include "walk_nss.h"
284 if (status != NSS_STATUS_SUCCESS)
287 if (gid != grp->gr_gid) {
288 syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
289 return NSS_STATUS_NOTFOUND;
292 return check_nonlocal_group(grp->gr_name, grp, errnop);
296 add_group(gid_t group, long int *start, long int *size, gid_t **groupsp,
297 long int limit, int *errnop, enum nss_status *status)
299 int i, old_errno = errno;
300 for (i = 0; i < *start; ++i)
301 if ((*groupsp)[i] == group)
303 if (*start + 1 > *size) {
305 long int newsize = 2 * *size;
307 if (*size >= limit) {
308 *status = NSS_STATUS_SUCCESS;
314 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
316 if (newgroups == NULL) {
318 *status = NSS_STATUS_TRYAGAIN;
321 *groupsp = newgroups;
324 (*groupsp)[(*start)++] = group;
329 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
330 long int *size, gid_t **groupsp, long int limit,
333 enum nss_status status;
334 const struct walk_nss w = {
335 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "initgroups_dyn",
336 .status = &status, .errnop = errnop
338 const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL;
340 struct group local_users_group, nonlocal_users_group;
341 bool is_nonlocal = true;
345 /* Check that the user is a nonlocal user, or a member of the
346 * MAGIC_NONLOCAL_GROUPNAME group, before adding any groups. */
347 status = check_nonlocal_user(user, errnop);
348 if (status == NSS_STATUS_TRYAGAIN) {
350 } else if (status != NSS_STATUS_SUCCESS) {
353 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
354 &local_users_group, &buffer, errnop);
355 if (status == NSS_STATUS_SUCCESS) {
357 if (!add_group(local_users_group.gr_gid, start, size, groupsp,
358 limit, errnop, &status))
360 } else if (status == NSS_STATUS_TRYAGAIN) {
364 "nss_nonlocal: Group %s does not exist locally!",
365 MAGIC_LOCAL_GROUPNAME);
369 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
370 &nonlocal_users_group, &buffer, errnop);
371 if (status == NSS_STATUS_SUCCESS) {
374 if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
375 limit, errnop, &status))
379 for (i = 0; i < *start; ++i) {
380 if ((*groupsp)[i] == nonlocal_users_group.gr_gid) {
389 int nonlocal_errno = *errnop;
390 status = get_nonlocal_passwd(user, &pwbuf, &buf, errnop);
392 if (status == NSS_STATUS_SUCCESS) {
393 nonlocal_errno = *errnop;
394 status = check_nonlocal_gid(user, pwbuf.pw_gid,
399 if (status == NSS_STATUS_SUCCESS) {
400 if (!add_group(pwbuf.pw_gid, start, size, groupsp, limit,
403 } else if (status == NSS_STATUS_TRYAGAIN) {
404 *errnop = nonlocal_errno;
409 } else if (status == NSS_STATUS_TRYAGAIN) {
413 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
414 MAGIC_NONLOCAL_GROUPNAME);
418 return NSS_STATUS_SUCCESS;
422 #define args (user, group, start, size, groupsp, limit, errnop)
423 #include "walk_nss.h"
425 if (status != NSS_STATUS_SUCCESS)
428 for (; in < *start; ++in) {
429 int nonlocal_errno = *errnop;
431 for (i = 0; i < out; ++i)
432 if ((*groupsp)[i] == (*groupsp)[in])
437 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
438 if (status == NSS_STATUS_SUCCESS) {
439 (*groupsp)[out++] = (*groupsp)[in];
440 } else if (status == NSS_STATUS_TRYAGAIN) {
442 *errnop = nonlocal_errno;
448 return NSS_STATUS_SUCCESS;