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;
100 if (fct.l == _nss_nonlocal_getgrgid_r)
101 status = NSS_STATUS_NOTFOUND;
103 status = DL_CALL_FCT(fct.l, (gid, &gbuf, buf, buflen, errnop));
104 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
106 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
108 if (status == NSS_STATUS_SUCCESS) {
109 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
110 status = NSS_STATUS_NOTFOUND;
111 } else if (status != NSS_STATUS_TRYAGAIN) {
112 status = NSS_STATUS_SUCCESS;
120 get_local_group(const char *name, struct group *grp, char *buffer, size_t buflen, int *errnop)
122 static const char *fct_name = "getgrnam_r";
123 static service_user *startp = NULL;
124 static void *fct_start = NULL;
125 enum nss_status status;
128 enum nss_status (*l)(const char *name, struct group *grp,
129 char *buffer, size_t buflen, int *errnop);
134 int old_errno = errno;
136 int len = sysconf(_SC_GETGR_R_SIZE_MAX);
137 char *buf = malloc(len);
141 return NSS_STATUS_TRYAGAIN;
144 if (fct_start == NULL &&
145 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
147 return NSS_STATUS_UNAVAIL;
152 if (fct.l == _nss_nonlocal_getgrnam_r)
153 status = NSS_STATUS_NOTFOUND;
155 status = DL_CALL_FCT(fct.l, (name, &gbuf, buf, buflen, errnop));
156 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
158 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
160 if (status == NSS_STATUS_SUCCESS) {
161 n = snprintf(buffer, buflen, "%s", gbuf.gr_name);
162 if (n < 0 || n >= buflen) {
164 status = NSS_STATUS_TRYAGAIN;
165 goto get_local_group_done;
167 grp->gr_name = buffer;
171 n = snprintf(buffer, buflen, "%s", gbuf.gr_passwd);
172 if (n < 0 || n >= buflen) {
174 status = NSS_STATUS_TRYAGAIN;
175 goto get_local_group_done;
177 grp->gr_passwd = buffer;
181 grp->gr_gid = gbuf.gr_gid;
183 if (buflen < sizeof(void *)) {
185 status = NSS_STATUS_TRYAGAIN;
186 goto get_local_group_done;
188 *(void **)buffer = NULL;
189 buffer += sizeof(void *);
190 buflen -= sizeof(void *);
193 get_local_group_done:
198 static service_user *grent_nip = NULL;
199 static void *grent_fct_start;
201 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
205 static const char *grent_fct_name = "getgrent_r";
208 _nss_nonlocal_setgrent(int stayopen)
210 static const char *fct_name = "setgrent";
211 static void *fct_start = NULL;
212 enum nss_status status;
215 enum nss_status (*l)(int stayopen);
219 nip = nss_group_nonlocal_database();
221 return NSS_STATUS_UNAVAIL;
222 if (fct_start == NULL)
223 fct_start = __nss_lookup_function(nip, fct_name);
227 status = NSS_STATUS_UNAVAIL;
229 status = DL_CALL_FCT(fct.l, (stayopen));
230 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
231 if (status != NSS_STATUS_SUCCESS)
235 if (grent_fct_start == NULL)
236 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
237 grent_fct.ptr = grent_fct_start;
238 return NSS_STATUS_SUCCESS;
242 _nss_nonlocal_endgrent(void)
244 static const char *fct_name = "endgrent";
245 static void *fct_start = NULL;
246 enum nss_status status;
249 enum nss_status (*l)(void);
255 nip = nss_group_nonlocal_database();
257 return NSS_STATUS_UNAVAIL;
258 if (fct_start == NULL)
259 fct_start = __nss_lookup_function(nip, fct_name);
263 status = NSS_STATUS_UNAVAIL;
265 status = DL_CALL_FCT(fct.l, ());
266 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
271 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
274 enum nss_status status;
276 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
277 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
278 return NSS_STATUS_UNAVAIL;
280 if (grent_nip == NULL) {
281 status = _nss_nonlocal_setgrent(0);
282 if (status != NSS_STATUS_SUCCESS)
286 if (grent_fct.ptr == NULL)
287 status = NSS_STATUS_UNAVAIL;
291 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
292 while (status == NSS_STATUS_SUCCESS &&
293 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
295 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
298 if (status == NSS_STATUS_SUCCESS)
299 return NSS_STATUS_SUCCESS;
300 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
303 return NSS_STATUS_NOTFOUND;
308 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
309 char *buffer, size_t buflen, int *errnop)
311 static const char *fct_name = "getgrnam_r";
312 static void *fct_start = NULL;
313 enum nss_status status;
316 enum nss_status (*l)(const char *name, struct group *grp,
317 char *buffer, size_t buflen, int *errnop);
321 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
322 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
323 return NSS_STATUS_UNAVAIL;
325 nip = nss_group_nonlocal_database();
327 return NSS_STATUS_UNAVAIL;
328 if (fct_start == NULL)
329 fct_start = __nss_lookup_function(nip, fct_name);
333 status = NSS_STATUS_UNAVAIL;
335 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
336 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
338 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
339 if (status != NSS_STATUS_SUCCESS)
342 return check_nonlocal_gid(name, grp->gr_gid, errnop);
346 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
347 char *buffer, size_t buflen, int *errnop)
349 static const char *fct_name = "getgrgid_r";
350 static void *fct_start = NULL;
351 enum nss_status status;
354 enum nss_status (*l)(gid_t gid, struct group *grp,
355 char *buffer, size_t buflen, int *errnop);
359 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
360 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
361 return NSS_STATUS_UNAVAIL;
363 nip = nss_group_nonlocal_database();
365 return NSS_STATUS_UNAVAIL;
366 if (fct_start == NULL)
367 fct_start = __nss_lookup_function(nip, fct_name);
371 status = NSS_STATUS_UNAVAIL;
373 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
374 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
376 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
377 if (status != NSS_STATUS_SUCCESS)
380 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
384 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
385 long int *size, gid_t **groupsp, long int limit,
388 static const char *fct_name = "initgroups_dyn";
389 static void *fct_start = NULL;
390 enum nss_status status;
393 enum nss_status (*l)(const char *user, gid_t group, long int *start,
394 long int *size, gid_t **groupsp, long int limit,
399 struct group local_users_group, nonlocal_users_group;
400 gid_t local_users_gid, gid;
405 /* Check that the user is a nonlocal user before adding any groups. */
406 status = check_nonlocal_user(user, errnop);
407 if (status == NSS_STATUS_TRYAGAIN)
409 else if (status != NSS_STATUS_SUCCESS)
412 int old_errno = errno;
414 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
415 buffer = malloc(buflen);
416 if (buffer == NULL) {
419 return NSS_STATUS_TRYAGAIN;
421 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
422 &local_users_group, buffer, buflen, errnop);
423 if (status == NSS_STATUS_SUCCESS) {
424 local_users_gid = local_users_group.gr_gid;
425 } else if (status == NSS_STATUS_TRYAGAIN) {
429 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
430 MAGIC_LOCAL_GROUPNAME);
431 local_users_gid = -1;
436 gid = local_users_gid;
438 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
439 buffer = malloc(buflen);
440 if (buffer == NULL) {
443 return NSS_STATUS_TRYAGAIN;
445 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
446 &nonlocal_users_group, buffer, buflen, errnop);
447 if (status == NSS_STATUS_SUCCESS) {
448 gid = nonlocal_users_group.gr_gid;
449 } else if (status == NSS_STATUS_TRYAGAIN) {
453 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
454 MAGIC_NONLOCAL_GROUPNAME);
462 for (i = 0; i < *start; ++i)
463 if ((*groupsp)[i] == gid)
466 if (*start + 1 > *size) {
468 long int newsize = 2 * *size;
471 return NSS_STATUS_SUCCESS;
475 newgroups = realloc(*groupsp, *size * sizeof((*groupsp)[0]));
476 if (newgroups == NULL) {
479 return NSS_STATUS_TRYAGAIN;
481 *groupsp = newgroups;
484 (*groupsp)[(*start)++] = gid;
489 return NSS_STATUS_SUCCESS;
491 int in = *start, out = *start, i;
493 nip = nss_group_nonlocal_database();
495 return NSS_STATUS_UNAVAIL;
496 if (fct_start == NULL)
497 fct_start = __nss_lookup_function(nip, fct_name);
502 status = NSS_STATUS_UNAVAIL;
504 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
505 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
507 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
508 if (status != NSS_STATUS_SUCCESS)
511 for (; in < *start; ++in) {
512 int nonlocal_errno = *errnop;
514 for (i = 0; i < out; ++i)
515 if ((*groupsp)[i] == (*groupsp)[in])
520 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
521 if (local_users_gid == (*groupsp)[in]) {
522 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
523 user, MAGIC_LOCAL_GROUPNAME);
527 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
528 if (status == NSS_STATUS_SUCCESS) {
529 (*groupsp)[out++] = (*groupsp)[in];
530 } else if (status == NSS_STATUS_TRYAGAIN) {
532 *errnop = nonlocal_errno;
538 return NSS_STATUS_SUCCESS;