3 * group database for nss_nonlocal proxy
5 * Copyright © 2007 Anders Kaseorg <andersk@mit.edu> and Tim Abbott
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy,
12 * modify, merge, publish, distribute, sublicense, and/or sell copies
13 * of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <sys/types.h>
41 #include "nsswitch-internal.h"
44 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
45 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
49 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
50 char *buffer, size_t buflen, int *errnop);
53 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
54 char *buffer, size_t buflen, int *errnop);
58 nss_group_nonlocal_database(void)
60 static service_user *nip = NULL;
62 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
69 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
71 static const char *fct_name = "getgrgid_r";
72 static service_user *startp = NULL;
73 static void *fct_start = NULL;
74 enum nss_status status;
77 enum nss_status (*l)(gid_t gid, struct group *grp,
78 char *buffer, size_t buflen, int *errnop);
82 int old_errno = errno;
84 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
85 char *buf = malloc(buflen);
89 return NSS_STATUS_TRYAGAIN;
92 if (fct_start == NULL &&
93 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
95 return NSS_STATUS_UNAVAIL;
101 if (fct.l == _nss_nonlocal_getgrgid_r)
102 status = NSS_STATUS_NOTFOUND;
104 status = DL_CALL_FCT(fct.l, (gid, &gbuf, buf, buflen, errnop));
105 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
108 buf = malloc(buflen);
112 return NSS_STATUS_TRYAGAIN;
116 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
118 if (status == NSS_STATUS_SUCCESS) {
119 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
120 status = NSS_STATUS_NOTFOUND;
121 } else if (status != NSS_STATUS_TRYAGAIN) {
122 status = NSS_STATUS_SUCCESS;
130 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
132 static const char *fct_name = "getgrnam_r";
133 static service_user *startp = NULL;
134 static void *fct_start = NULL;
135 enum nss_status status;
138 enum nss_status (*l)(const char *name, struct group *grp,
139 char *buffer, size_t buflen, int *errnop);
143 int old_errno = errno;
145 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
146 *buffer = malloc(buflen);
147 if (*buffer == NULL) {
150 return NSS_STATUS_TRYAGAIN;
153 if (fct_start == NULL &&
154 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
157 return NSS_STATUS_UNAVAIL;
163 if (fct.l == _nss_nonlocal_getgrnam_r)
164 status = NSS_STATUS_NOTFOUND;
166 status = DL_CALL_FCT(fct.l, (name, grp, *buffer, buflen, errnop));
167 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
170 *buffer = malloc(buflen);
171 if (*buffer == NULL) {
174 return NSS_STATUS_TRYAGAIN;
178 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
180 if (status != NSS_STATUS_SUCCESS) {
188 static service_user *grent_nip = NULL;
189 static void *grent_fct_start;
191 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
195 static const char *grent_fct_name = "getgrent_r";
198 _nss_nonlocal_setgrent(int stayopen)
200 static const char *fct_name = "setgrent";
201 static void *fct_start = NULL;
202 enum nss_status status;
205 enum nss_status (*l)(int stayopen);
209 nip = nss_group_nonlocal_database();
211 return NSS_STATUS_UNAVAIL;
212 if (fct_start == NULL)
213 fct_start = __nss_lookup_function(nip, fct_name);
217 status = NSS_STATUS_UNAVAIL;
219 status = DL_CALL_FCT(fct.l, (stayopen));
220 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
221 if (status != NSS_STATUS_SUCCESS)
225 if (grent_fct_start == NULL)
226 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
227 grent_fct.ptr = grent_fct_start;
228 return NSS_STATUS_SUCCESS;
232 _nss_nonlocal_endgrent(void)
234 static const char *fct_name = "endgrent";
235 static void *fct_start = NULL;
236 enum nss_status status;
239 enum nss_status (*l)(void);
245 nip = nss_group_nonlocal_database();
247 return NSS_STATUS_UNAVAIL;
248 if (fct_start == NULL)
249 fct_start = __nss_lookup_function(nip, fct_name);
253 status = NSS_STATUS_UNAVAIL;
255 status = DL_CALL_FCT(fct.l, ());
256 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
261 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
264 enum nss_status status;
266 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
267 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
268 return NSS_STATUS_UNAVAIL;
270 if (grent_nip == NULL) {
271 status = _nss_nonlocal_setgrent(0);
272 if (status != NSS_STATUS_SUCCESS)
276 if (grent_fct.ptr == NULL)
277 status = NSS_STATUS_UNAVAIL;
281 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
282 while (status == NSS_STATUS_SUCCESS &&
283 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
285 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
288 if (status == NSS_STATUS_SUCCESS)
289 return NSS_STATUS_SUCCESS;
290 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
293 return NSS_STATUS_NOTFOUND;
298 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
299 char *buffer, size_t buflen, int *errnop)
301 static const char *fct_name = "getgrnam_r";
302 static void *fct_start = NULL;
303 enum nss_status status;
306 enum nss_status (*l)(const char *name, struct group *grp,
307 char *buffer, size_t buflen, int *errnop);
311 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
312 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
313 return NSS_STATUS_UNAVAIL;
315 nip = nss_group_nonlocal_database();
317 return NSS_STATUS_UNAVAIL;
318 if (fct_start == NULL)
319 fct_start = __nss_lookup_function(nip, fct_name);
323 status = NSS_STATUS_UNAVAIL;
325 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
326 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
328 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
329 if (status != NSS_STATUS_SUCCESS)
332 if (strcmp(name, grp->gr_name) != 0) {
333 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
334 return NSS_STATUS_NOTFOUND;
337 return check_nonlocal_gid(name, grp->gr_gid, errnop);
341 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
342 char *buffer, size_t buflen, int *errnop)
344 static const char *fct_name = "getgrgid_r";
345 static void *fct_start = NULL;
346 enum nss_status status;
349 enum nss_status (*l)(gid_t gid, struct group *grp,
350 char *buffer, size_t buflen, int *errnop);
354 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
355 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
356 return NSS_STATUS_UNAVAIL;
358 nip = nss_group_nonlocal_database();
360 return NSS_STATUS_UNAVAIL;
361 if (fct_start == NULL)
362 fct_start = __nss_lookup_function(nip, fct_name);
366 status = NSS_STATUS_UNAVAIL;
368 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
369 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
371 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
372 if (status != NSS_STATUS_SUCCESS)
375 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
379 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
380 long int *size, gid_t **groupsp, long int limit,
383 static const char *fct_name = "initgroups_dyn";
384 static void *fct_start = NULL;
385 enum nss_status status;
388 enum nss_status (*l)(const char *user, gid_t group, long int *start,
389 long int *size, gid_t **groupsp, long int limit,
394 struct group local_users_group, nonlocal_users_group;
395 gid_t local_users_gid, gid;
401 /* Check that the user is a nonlocal user before adding any groups. */
402 status = check_nonlocal_user(user, errnop);
403 if (status == NSS_STATUS_TRYAGAIN)
405 else if (status != NSS_STATUS_SUCCESS)
410 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
411 &local_users_group, &buffer, errnop);
412 if (status == NSS_STATUS_SUCCESS) {
413 local_users_gid = local_users_group.gr_gid;
415 } else if (status == NSS_STATUS_TRYAGAIN) {
418 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
419 MAGIC_LOCAL_GROUPNAME);
420 local_users_gid = -1;
424 gid = local_users_gid;
426 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
427 &nonlocal_users_group, &buffer, errnop);
428 if (status == NSS_STATUS_SUCCESS) {
429 gid = nonlocal_users_group.gr_gid;
431 } else if (status == NSS_STATUS_TRYAGAIN) {
434 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
435 MAGIC_NONLOCAL_GROUPNAME);
442 for (i = 0; i < *start; ++i)
443 if ((*groupsp)[i] == gid)
446 if (*start + 1 > *size) {
448 long int newsize = 2 * *size;
451 return NSS_STATUS_SUCCESS;
455 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
456 if (newgroups == NULL) {
459 return NSS_STATUS_TRYAGAIN;
461 *groupsp = newgroups;
464 (*groupsp)[(*start)++] = gid;
469 return NSS_STATUS_SUCCESS;
473 nip = nss_group_nonlocal_database();
475 return NSS_STATUS_UNAVAIL;
476 if (fct_start == NULL)
477 fct_start = __nss_lookup_function(nip, fct_name);
482 status = NSS_STATUS_UNAVAIL;
484 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
485 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
487 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
488 if (status != NSS_STATUS_SUCCESS)
491 for (; in < *start; ++in) {
492 int nonlocal_errno = *errnop;
494 for (i = 0; i < out; ++i)
495 if ((*groupsp)[i] == (*groupsp)[in])
500 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
501 if (local_users_gid == (*groupsp)[in]) {
502 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
503 user, MAGIC_LOCAL_GROUPNAME);
507 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
508 if (status == NSS_STATUS_SUCCESS) {
509 (*groupsp)[out++] = (*groupsp)[in];
510 } else if (status == NSS_STATUS_TRYAGAIN) {
512 *errnop = nonlocal_errno;
518 return NSS_STATUS_SUCCESS;