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, size_t buflen, 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);
144 int old_errno = errno;
146 int len = sysconf(_SC_GETGR_R_SIZE_MAX);
147 char *buf = malloc(len);
151 return NSS_STATUS_TRYAGAIN;
154 if (fct_start == NULL &&
155 __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, &gbuf, buf, len, errnop));
167 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
174 return NSS_STATUS_TRYAGAIN;
178 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
180 if (status != NSS_STATUS_SUCCESS)
181 goto get_local_group_done;
183 n = snprintf(buffer, buflen, "%s", gbuf.gr_name);
184 if (n < 0 || n >= buflen) {
186 status = NSS_STATUS_TRYAGAIN;
187 goto get_local_group_done;
189 grp->gr_name = buffer;
193 n = snprintf(buffer, buflen, "%s", gbuf.gr_passwd);
194 if (n < 0 || n >= buflen) {
196 status = NSS_STATUS_TRYAGAIN;
197 goto get_local_group_done;
199 grp->gr_passwd = buffer;
203 grp->gr_gid = gbuf.gr_gid;
205 if (buflen < sizeof(void *)) {
207 status = NSS_STATUS_TRYAGAIN;
208 goto get_local_group_done;
210 *(void **)buffer = NULL;
211 buffer += sizeof(void *);
212 buflen -= sizeof(void *);
214 get_local_group_done:
219 static service_user *grent_nip = NULL;
220 static void *grent_fct_start;
222 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
226 static const char *grent_fct_name = "getgrent_r";
229 _nss_nonlocal_setgrent(int stayopen)
231 static const char *fct_name = "setgrent";
232 static void *fct_start = NULL;
233 enum nss_status status;
236 enum nss_status (*l)(int stayopen);
240 nip = nss_group_nonlocal_database();
242 return NSS_STATUS_UNAVAIL;
243 if (fct_start == NULL)
244 fct_start = __nss_lookup_function(nip, fct_name);
248 status = NSS_STATUS_UNAVAIL;
250 status = DL_CALL_FCT(fct.l, (stayopen));
251 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
252 if (status != NSS_STATUS_SUCCESS)
256 if (grent_fct_start == NULL)
257 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
258 grent_fct.ptr = grent_fct_start;
259 return NSS_STATUS_SUCCESS;
263 _nss_nonlocal_endgrent(void)
265 static const char *fct_name = "endgrent";
266 static void *fct_start = NULL;
267 enum nss_status status;
270 enum nss_status (*l)(void);
276 nip = nss_group_nonlocal_database();
278 return NSS_STATUS_UNAVAIL;
279 if (fct_start == NULL)
280 fct_start = __nss_lookup_function(nip, fct_name);
284 status = NSS_STATUS_UNAVAIL;
286 status = DL_CALL_FCT(fct.l, ());
287 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
292 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
295 enum nss_status status;
297 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
298 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
299 return NSS_STATUS_UNAVAIL;
301 if (grent_nip == NULL) {
302 status = _nss_nonlocal_setgrent(0);
303 if (status != NSS_STATUS_SUCCESS)
307 if (grent_fct.ptr == NULL)
308 status = NSS_STATUS_UNAVAIL;
312 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
313 while (status == NSS_STATUS_SUCCESS &&
314 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
316 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
319 if (status == NSS_STATUS_SUCCESS)
320 return NSS_STATUS_SUCCESS;
321 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
324 return NSS_STATUS_NOTFOUND;
329 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
330 char *buffer, size_t buflen, int *errnop)
332 static const char *fct_name = "getgrnam_r";
333 static void *fct_start = NULL;
334 enum nss_status status;
337 enum nss_status (*l)(const char *name, struct group *grp,
338 char *buffer, size_t buflen, int *errnop);
342 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
343 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
344 return NSS_STATUS_UNAVAIL;
346 nip = nss_group_nonlocal_database();
348 return NSS_STATUS_UNAVAIL;
349 if (fct_start == NULL)
350 fct_start = __nss_lookup_function(nip, fct_name);
354 status = NSS_STATUS_UNAVAIL;
356 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
357 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
359 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
360 if (status != NSS_STATUS_SUCCESS)
363 return check_nonlocal_gid(name, grp->gr_gid, errnop);
367 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
368 char *buffer, size_t buflen, int *errnop)
370 static const char *fct_name = "getgrgid_r";
371 static void *fct_start = NULL;
372 enum nss_status status;
375 enum nss_status (*l)(gid_t gid, struct group *grp,
376 char *buffer, size_t buflen, int *errnop);
380 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
381 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
382 return NSS_STATUS_UNAVAIL;
384 nip = nss_group_nonlocal_database();
386 return NSS_STATUS_UNAVAIL;
387 if (fct_start == NULL)
388 fct_start = __nss_lookup_function(nip, fct_name);
392 status = NSS_STATUS_UNAVAIL;
394 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
395 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
397 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
398 if (status != NSS_STATUS_SUCCESS)
401 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
405 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
406 long int *size, gid_t **groupsp, long int limit,
409 static const char *fct_name = "initgroups_dyn";
410 static void *fct_start = NULL;
411 enum nss_status status;
414 enum nss_status (*l)(const char *user, gid_t group, long int *start,
415 long int *size, gid_t **groupsp, long int limit,
420 struct group local_users_group, nonlocal_users_group;
421 gid_t local_users_gid, gid;
426 /* Check that the user is a nonlocal user before adding any groups. */
427 status = check_nonlocal_user(user, errnop);
428 if (status == NSS_STATUS_TRYAGAIN)
430 else if (status != NSS_STATUS_SUCCESS)
433 int old_errno = errno;
435 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
436 buffer = malloc(buflen);
437 if (buffer == NULL) {
440 return NSS_STATUS_TRYAGAIN;
442 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
443 &local_users_group, buffer, buflen, errnop);
444 if (status == NSS_STATUS_SUCCESS) {
445 local_users_gid = local_users_group.gr_gid;
446 } else if (status == NSS_STATUS_TRYAGAIN) {
450 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
451 MAGIC_LOCAL_GROUPNAME);
452 local_users_gid = -1;
457 gid = local_users_gid;
459 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
460 buffer = malloc(buflen);
461 if (buffer == NULL) {
464 return NSS_STATUS_TRYAGAIN;
466 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
467 &nonlocal_users_group, buffer, buflen, errnop);
468 if (status == NSS_STATUS_SUCCESS) {
469 gid = nonlocal_users_group.gr_gid;
470 } else if (status == NSS_STATUS_TRYAGAIN) {
474 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
475 MAGIC_NONLOCAL_GROUPNAME);
483 for (i = 0; i < *start; ++i)
484 if ((*groupsp)[i] == gid)
487 if (*start + 1 > *size) {
489 long int newsize = 2 * *size;
492 return NSS_STATUS_SUCCESS;
496 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
497 if (newgroups == NULL) {
500 return NSS_STATUS_TRYAGAIN;
502 *groupsp = newgroups;
505 (*groupsp)[(*start)++] = gid;
510 return NSS_STATUS_SUCCESS;
512 int in = *start, out = *start, i;
514 nip = nss_group_nonlocal_database();
516 return NSS_STATUS_UNAVAIL;
517 if (fct_start == NULL)
518 fct_start = __nss_lookup_function(nip, fct_name);
523 status = NSS_STATUS_UNAVAIL;
525 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
526 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
528 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
529 if (status != NSS_STATUS_SUCCESS)
532 for (; in < *start; ++in) {
533 int nonlocal_errno = *errnop;
535 for (i = 0; i < out; ++i)
536 if ((*groupsp)[i] == (*groupsp)[in])
541 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
542 if (local_users_gid == (*groupsp)[in]) {
543 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
544 user, MAGIC_LOCAL_GROUPNAME);
548 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
549 if (status == NSS_STATUS_SUCCESS) {
550 (*groupsp)[out++] = (*groupsp)[in];
551 } else if (status == NSS_STATUS_TRYAGAIN) {
553 *errnop = nonlocal_errno;
559 return NSS_STATUS_SUCCESS;