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 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
132 enum nss_status status = NSS_STATUS_SUCCESS;
133 int old_errno = errno;
138 gid = strtoul(grp->gr_name, &end, 10);
139 if (errno == 0 && *end == '\0' && (gid_t)gid == gid)
140 status = check_nonlocal_gid(user, gid, errnop);
142 if (status != NSS_STATUS_SUCCESS)
145 return check_nonlocal_gid(user, grp->gr_gid, errnop);
149 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
151 static const char *fct_name = "getgrnam_r";
152 static service_user *startp = NULL;
153 static void *fct_start = NULL;
154 enum nss_status status;
157 enum nss_status (*l)(const char *name, struct group *grp,
158 char *buffer, size_t buflen, int *errnop);
162 int old_errno = errno;
164 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
165 *buffer = malloc(buflen);
166 if (*buffer == NULL) {
169 return NSS_STATUS_TRYAGAIN;
172 if (fct_start == NULL &&
173 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
176 return NSS_STATUS_UNAVAIL;
182 if (fct.l == _nss_nonlocal_getgrnam_r)
183 status = NSS_STATUS_NOTFOUND;
185 status = DL_CALL_FCT(fct.l, (name, grp, *buffer, buflen, errnop));
186 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
189 *buffer = malloc(buflen);
190 if (*buffer == NULL) {
193 return NSS_STATUS_TRYAGAIN;
197 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
199 if (status != NSS_STATUS_SUCCESS) {
207 static service_user *grent_nip = NULL;
208 static void *grent_fct_start;
210 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
214 static const char *grent_fct_name = "getgrent_r";
217 _nss_nonlocal_setgrent(int stayopen)
219 static const char *fct_name = "setgrent";
220 static void *fct_start = NULL;
221 enum nss_status status;
224 enum nss_status (*l)(int stayopen);
228 nip = nss_group_nonlocal_database();
230 return NSS_STATUS_UNAVAIL;
231 if (fct_start == NULL)
232 fct_start = __nss_lookup_function(nip, fct_name);
236 status = NSS_STATUS_UNAVAIL;
238 status = DL_CALL_FCT(fct.l, (stayopen));
239 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
240 if (status != NSS_STATUS_SUCCESS)
244 if (grent_fct_start == NULL)
245 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
246 grent_fct.ptr = grent_fct_start;
247 return NSS_STATUS_SUCCESS;
251 _nss_nonlocal_endgrent(void)
253 static const char *fct_name = "endgrent";
254 static void *fct_start = NULL;
255 enum nss_status status;
258 enum nss_status (*l)(void);
264 nip = nss_group_nonlocal_database();
266 return NSS_STATUS_UNAVAIL;
267 if (fct_start == NULL)
268 fct_start = __nss_lookup_function(nip, fct_name);
272 status = NSS_STATUS_UNAVAIL;
274 status = DL_CALL_FCT(fct.l, ());
275 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
280 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
283 enum nss_status status;
285 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
286 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
287 return NSS_STATUS_UNAVAIL;
289 if (grent_nip == NULL) {
290 status = _nss_nonlocal_setgrent(0);
291 if (status != NSS_STATUS_SUCCESS)
295 if (grent_fct.ptr == NULL)
296 status = NSS_STATUS_UNAVAIL;
300 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
301 while (status == NSS_STATUS_SUCCESS &&
302 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
304 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
307 if (status == NSS_STATUS_SUCCESS)
308 return NSS_STATUS_SUCCESS;
309 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
312 return NSS_STATUS_NOTFOUND;
317 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
318 char *buffer, size_t buflen, int *errnop)
320 static const char *fct_name = "getgrnam_r";
321 static void *fct_start = NULL;
322 enum nss_status status;
325 enum nss_status (*l)(const char *name, struct group *grp,
326 char *buffer, size_t buflen, int *errnop);
330 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
331 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
332 return NSS_STATUS_UNAVAIL;
334 nip = nss_group_nonlocal_database();
336 return NSS_STATUS_UNAVAIL;
337 if (fct_start == NULL)
338 fct_start = __nss_lookup_function(nip, fct_name);
342 status = NSS_STATUS_UNAVAIL;
344 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
345 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
347 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
348 if (status != NSS_STATUS_SUCCESS)
351 if (strcmp(name, grp->gr_name) != 0) {
352 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
353 return NSS_STATUS_NOTFOUND;
356 return check_nonlocal_group(name, grp, errnop);
360 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
361 char *buffer, size_t buflen, int *errnop)
363 static const char *fct_name = "getgrgid_r";
364 static void *fct_start = NULL;
365 enum nss_status status;
368 enum nss_status (*l)(gid_t gid, struct group *grp,
369 char *buffer, size_t buflen, int *errnop);
373 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
374 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
375 return NSS_STATUS_UNAVAIL;
377 nip = nss_group_nonlocal_database();
379 return NSS_STATUS_UNAVAIL;
380 if (fct_start == NULL)
381 fct_start = __nss_lookup_function(nip, fct_name);
385 status = NSS_STATUS_UNAVAIL;
387 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
388 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
390 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
391 if (status != NSS_STATUS_SUCCESS)
394 return check_nonlocal_group(grp->gr_name, grp, errnop);
398 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
399 long int *size, gid_t **groupsp, long int limit,
402 static const char *fct_name = "initgroups_dyn";
403 static void *fct_start = NULL;
404 enum nss_status status;
407 enum nss_status (*l)(const char *user, gid_t group, long int *start,
408 long int *size, gid_t **groupsp, long int limit,
413 struct group local_users_group, nonlocal_users_group;
414 gid_t local_users_gid, gid;
420 /* Check that the user is a nonlocal user before adding any groups. */
421 status = check_nonlocal_user(user, errnop);
422 if (status == NSS_STATUS_TRYAGAIN)
424 else if (status != NSS_STATUS_SUCCESS)
429 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
430 &local_users_group, &buffer, errnop);
431 if (status == NSS_STATUS_SUCCESS) {
432 local_users_gid = local_users_group.gr_gid;
434 } else if (status == NSS_STATUS_TRYAGAIN) {
437 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
438 MAGIC_LOCAL_GROUPNAME);
439 local_users_gid = -1;
443 gid = local_users_gid;
445 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
446 &nonlocal_users_group, &buffer, errnop);
447 if (status == NSS_STATUS_SUCCESS) {
448 gid = nonlocal_users_group.gr_gid;
450 } else if (status == NSS_STATUS_TRYAGAIN) {
453 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
454 MAGIC_NONLOCAL_GROUPNAME);
461 for (i = 0; i < *start; ++i)
462 if ((*groupsp)[i] == gid)
465 if (*start + 1 > *size) {
467 long int newsize = 2 * *size;
470 return NSS_STATUS_SUCCESS;
474 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
475 if (newgroups == NULL) {
478 return NSS_STATUS_TRYAGAIN;
480 *groupsp = newgroups;
483 (*groupsp)[(*start)++] = gid;
488 return NSS_STATUS_SUCCESS;
492 nip = nss_group_nonlocal_database();
494 return NSS_STATUS_UNAVAIL;
495 if (fct_start == NULL)
496 fct_start = __nss_lookup_function(nip, fct_name);
501 status = NSS_STATUS_UNAVAIL;
503 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
504 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
506 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
507 if (status != NSS_STATUS_SUCCESS)
510 for (; in < *start; ++in) {
511 int nonlocal_errno = *errnop;
513 for (i = 0; i < out; ++i)
514 if ((*groupsp)[i] == (*groupsp)[in])
519 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
520 if (local_users_gid == (*groupsp)[in]) {
521 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
522 user, MAGIC_LOCAL_GROUPNAME);
526 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
527 if (status == NSS_STATUS_SUCCESS) {
528 (*groupsp)[out++] = (*groupsp)[in];
529 } else if (status == NSS_STATUS_TRYAGAIN) {
531 *errnop = nonlocal_errno;
537 return NSS_STATUS_SUCCESS;