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 int 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 return check_nonlocal_gid(name, grp->gr_gid, errnop);
336 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
337 char *buffer, size_t buflen, int *errnop)
339 static const char *fct_name = "getgrgid_r";
340 static void *fct_start = NULL;
341 enum nss_status status;
344 enum nss_status (*l)(gid_t gid, struct group *grp,
345 char *buffer, size_t buflen, int *errnop);
349 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
350 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
351 return NSS_STATUS_UNAVAIL;
353 nip = nss_group_nonlocal_database();
355 return NSS_STATUS_UNAVAIL;
356 if (fct_start == NULL)
357 fct_start = __nss_lookup_function(nip, fct_name);
361 status = NSS_STATUS_UNAVAIL;
363 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
364 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
366 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
367 if (status != NSS_STATUS_SUCCESS)
370 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
374 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
375 long int *size, gid_t **groupsp, long int limit,
378 static const char *fct_name = "initgroups_dyn";
379 static void *fct_start = NULL;
380 enum nss_status status;
383 enum nss_status (*l)(const char *user, gid_t group, long int *start,
384 long int *size, gid_t **groupsp, long int limit,
389 struct group local_users_group, nonlocal_users_group;
390 gid_t local_users_gid, gid;
394 /* Check that the user is a nonlocal user before adding any groups. */
395 status = check_nonlocal_user(user, errnop);
396 if (status == NSS_STATUS_TRYAGAIN)
398 else if (status != NSS_STATUS_SUCCESS)
401 int old_errno = errno;
403 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
404 &local_users_group, &buffer, errnop);
405 if (status == NSS_STATUS_SUCCESS) {
406 local_users_gid = local_users_group.gr_gid;
408 } else if (status == NSS_STATUS_TRYAGAIN) {
411 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
412 MAGIC_LOCAL_GROUPNAME);
413 local_users_gid = -1;
417 gid = local_users_gid;
419 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
420 &nonlocal_users_group, &buffer, errnop);
421 if (status == NSS_STATUS_SUCCESS) {
422 gid = nonlocal_users_group.gr_gid;
424 } else if (status == NSS_STATUS_TRYAGAIN) {
427 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
428 MAGIC_NONLOCAL_GROUPNAME);
435 for (i = 0; i < *start; ++i)
436 if ((*groupsp)[i] == gid)
439 if (*start + 1 > *size) {
441 long int newsize = 2 * *size;
444 return NSS_STATUS_SUCCESS;
448 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
449 if (newgroups == NULL) {
452 return NSS_STATUS_TRYAGAIN;
454 *groupsp = newgroups;
457 (*groupsp)[(*start)++] = gid;
462 return NSS_STATUS_SUCCESS;
464 int in = *start, out = *start, i;
466 nip = nss_group_nonlocal_database();
468 return NSS_STATUS_UNAVAIL;
469 if (fct_start == NULL)
470 fct_start = __nss_lookup_function(nip, fct_name);
475 status = NSS_STATUS_UNAVAIL;
477 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
478 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
480 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
481 if (status != NSS_STATUS_SUCCESS)
484 for (; in < *start; ++in) {
485 int nonlocal_errno = *errnop;
487 for (i = 0; i < out; ++i)
488 if ((*groupsp)[i] == (*groupsp)[in])
493 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
494 if (local_users_gid == (*groupsp)[in]) {
495 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
496 user, MAGIC_LOCAL_GROUPNAME);
500 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
501 if (status == NSS_STATUS_SUCCESS) {
502 (*groupsp)[out++] = (*groupsp)[in];
503 } else if (status == NSS_STATUS_TRYAGAIN) {
505 *errnop = nonlocal_errno;
511 return NSS_STATUS_SUCCESS;