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);
54 static service_user *__nss_group_nonlocal_database;
58 __nss_group_nonlocal_lookup(service_user **ni, const char *fct_name,
61 if (__nss_group_nonlocal_database == NULL
62 && __nss_database_lookup("group_nonlocal", NULL, NULL,
63 &__nss_group_nonlocal_database) < 0)
66 *ni = __nss_group_nonlocal_database;
68 *fctp = __nss_lookup_function(*ni, fct_name);
74 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
76 enum nss_status status;
79 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
80 const struct walk_nss w = {
81 .lookup = &__nss_group_lookup, .fct_name = "getgrgid_r",
82 .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
84 const __typeof__(&_nss_nonlocal_getgrgid_r) self = &_nss_nonlocal_getgrgid_r;
85 #define args (gid, &gbuf, buf, buflen, errnop)
89 if (status == NSS_STATUS_SUCCESS) {
90 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
92 status = NSS_STATUS_NOTFOUND;
93 } else if (status != NSS_STATUS_TRYAGAIN) {
94 status = NSS_STATUS_SUCCESS;
101 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
103 enum nss_status status = NSS_STATUS_SUCCESS;
104 int old_errno = errno;
109 gid = strtoul(grp->gr_name, &end, 10);
110 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
112 status = check_nonlocal_gid(user, gid, errnop);
115 if (status != NSS_STATUS_SUCCESS)
118 return check_nonlocal_gid(user, grp->gr_gid, errnop);
122 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
124 enum nss_status status;
125 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
126 const struct walk_nss w = {
127 .lookup = &__nss_group_lookup, .fct_name = "getgrnam_r",
128 .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen
130 const __typeof__(&_nss_nonlocal_getgrnam_r) self = &_nss_nonlocal_getgrnam_r;
131 #define args (name, grp, *buffer, buflen, errnop)
132 #include "walk_nss.h"
137 static service_user *grent_startp, *grent_nip;
138 static void *grent_fct_start;
140 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
144 static const char *grent_fct_name = "getgrent_r";
147 _nss_nonlocal_setgrent(int stayopen)
149 enum nss_status status;
150 const struct walk_nss w = {
151 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "setgrent",
154 const __typeof__(&_nss_nonlocal_setgrent) self = NULL;
155 #define args (stayopen)
156 #include "walk_nss.h"
158 if (status != NSS_STATUS_SUCCESS)
161 if (grent_fct_start == NULL)
162 __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
164 grent_nip = grent_startp;
165 grent_fct.ptr = grent_fct_start;
166 return NSS_STATUS_SUCCESS;
170 _nss_nonlocal_endgrent(void)
172 enum nss_status status;
173 const struct walk_nss w = {
174 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "endgrent",
177 const __typeof__(&_nss_nonlocal_endgrent) self = NULL;
182 #include "walk_nss.h"
188 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
191 enum nss_status status;
193 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
194 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
195 return NSS_STATUS_UNAVAIL;
197 if (grent_nip == NULL) {
198 status = _nss_nonlocal_setgrent(0);
199 if (status != NSS_STATUS_SUCCESS)
203 if (grent_fct.ptr == NULL)
204 status = NSS_STATUS_UNAVAIL;
208 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
209 while (status == NSS_STATUS_SUCCESS &&
210 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
212 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
215 if (status == NSS_STATUS_SUCCESS)
216 return NSS_STATUS_SUCCESS;
217 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
220 return NSS_STATUS_NOTFOUND;
225 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
226 char *buffer, size_t buflen, int *errnop)
228 enum nss_status status;
229 const struct walk_nss w = {
230 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrnam_r",
231 .status = &status, .errnop = errnop
233 const __typeof__(&_nss_nonlocal_getgrnam_r) self = NULL;
235 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
236 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
237 return NSS_STATUS_UNAVAIL;
239 #define args (name, grp, buffer, buflen, errnop)
240 #include "walk_nss.h"
242 if (status != NSS_STATUS_SUCCESS)
245 if (strcmp(name, grp->gr_name) != 0) {
246 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
247 return NSS_STATUS_NOTFOUND;
250 return check_nonlocal_group(name, grp, errnop);
254 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
255 char *buffer, size_t buflen, int *errnop)
257 enum nss_status status;
258 const struct walk_nss w = {
259 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrgid_r",
260 .status = &status, .errnop = errnop
262 const __typeof__(&_nss_nonlocal_getgrgid_r) self = NULL;
264 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
265 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
266 return NSS_STATUS_UNAVAIL;
268 #define args (gid, grp, buffer, buflen, errnop)
269 #include "walk_nss.h"
271 if (status != NSS_STATUS_SUCCESS)
274 if (gid != grp->gr_gid) {
275 syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
276 return NSS_STATUS_NOTFOUND;
279 return check_nonlocal_group(grp->gr_name, grp, errnop);
283 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
284 long int *size, gid_t **groupsp, long int limit,
287 enum nss_status status;
288 const struct walk_nss w = {
289 .lookup = &__nss_group_nonlocal_lookup, .fct_name = "initgroups_dyn",
290 .status = &status, .errnop = errnop
292 const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL;
294 struct group local_users_group, nonlocal_users_group;
295 gid_t local_users_gid, gid;
301 /* Check that the user is a nonlocal user before adding any groups. */
302 status = check_nonlocal_user(user, errnop);
303 if (status == NSS_STATUS_TRYAGAIN)
305 else if (status != NSS_STATUS_SUCCESS)
310 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
311 &local_users_group, &buffer, errnop);
312 if (status == NSS_STATUS_SUCCESS) {
313 local_users_gid = local_users_group.gr_gid;
315 } else if (status == NSS_STATUS_TRYAGAIN) {
318 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
319 MAGIC_LOCAL_GROUPNAME);
320 local_users_gid = -1;
324 gid = local_users_gid;
326 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
327 &nonlocal_users_group, &buffer, errnop);
328 if (status == NSS_STATUS_SUCCESS) {
329 gid = nonlocal_users_group.gr_gid;
331 } else if (status == NSS_STATUS_TRYAGAIN) {
334 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
335 MAGIC_NONLOCAL_GROUPNAME);
342 for (i = 0; i < *start; ++i)
343 if ((*groupsp)[i] == gid)
346 if (*start + 1 > *size) {
348 long int newsize = 2 * *size;
351 return NSS_STATUS_SUCCESS;
355 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
357 if (newgroups == NULL) {
359 return NSS_STATUS_TRYAGAIN;
361 *groupsp = newgroups;
364 (*groupsp)[(*start)++] = gid;
369 return NSS_STATUS_SUCCESS;
373 #define args (user, group, start, size, groupsp, limit, errnop)
374 #include "walk_nss.h"
376 if (status != NSS_STATUS_SUCCESS)
379 for (; in < *start; ++in) {
380 int nonlocal_errno = *errnop;
382 for (i = 0; i < out; ++i)
383 if ((*groupsp)[i] == (*groupsp)[in])
388 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
389 if (local_users_gid == (*groupsp)[in]) {
390 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
391 user, MAGIC_LOCAL_GROUPNAME);
395 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
396 if (status == NSS_STATUS_SUCCESS) {
397 (*groupsp)[out++] = (*groupsp)[in];
398 } else if (status == NSS_STATUS_TRYAGAIN) {
400 *errnop = nonlocal_errno;
406 return NSS_STATUS_SUCCESS;