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, len, 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 goto get_local_group_done;
163 n = snprintf(buffer, buflen, "%s", gbuf.gr_name);
164 if (n < 0 || n >= buflen) {
166 status = NSS_STATUS_TRYAGAIN;
167 goto get_local_group_done;
169 grp->gr_name = buffer;
173 n = snprintf(buffer, buflen, "%s", gbuf.gr_passwd);
174 if (n < 0 || n >= buflen) {
176 status = NSS_STATUS_TRYAGAIN;
177 goto get_local_group_done;
179 grp->gr_passwd = buffer;
183 grp->gr_gid = gbuf.gr_gid;
185 if (buflen < sizeof(void *)) {
187 status = NSS_STATUS_TRYAGAIN;
188 goto get_local_group_done;
190 *(void **)buffer = NULL;
191 buffer += sizeof(void *);
192 buflen -= sizeof(void *);
194 get_local_group_done:
199 static service_user *grent_nip = NULL;
200 static void *grent_fct_start;
202 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
206 static const char *grent_fct_name = "getgrent_r";
209 _nss_nonlocal_setgrent(int stayopen)
211 static const char *fct_name = "setgrent";
212 static void *fct_start = NULL;
213 enum nss_status status;
216 enum nss_status (*l)(int stayopen);
220 nip = nss_group_nonlocal_database();
222 return NSS_STATUS_UNAVAIL;
223 if (fct_start == NULL)
224 fct_start = __nss_lookup_function(nip, fct_name);
228 status = NSS_STATUS_UNAVAIL;
230 status = DL_CALL_FCT(fct.l, (stayopen));
231 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
232 if (status != NSS_STATUS_SUCCESS)
236 if (grent_fct_start == NULL)
237 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
238 grent_fct.ptr = grent_fct_start;
239 return NSS_STATUS_SUCCESS;
243 _nss_nonlocal_endgrent(void)
245 static const char *fct_name = "endgrent";
246 static void *fct_start = NULL;
247 enum nss_status status;
250 enum nss_status (*l)(void);
256 nip = nss_group_nonlocal_database();
258 return NSS_STATUS_UNAVAIL;
259 if (fct_start == NULL)
260 fct_start = __nss_lookup_function(nip, fct_name);
264 status = NSS_STATUS_UNAVAIL;
266 status = DL_CALL_FCT(fct.l, ());
267 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
272 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
275 enum nss_status status;
277 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
278 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
279 return NSS_STATUS_UNAVAIL;
281 if (grent_nip == NULL) {
282 status = _nss_nonlocal_setgrent(0);
283 if (status != NSS_STATUS_SUCCESS)
287 if (grent_fct.ptr == NULL)
288 status = NSS_STATUS_UNAVAIL;
292 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
293 while (status == NSS_STATUS_SUCCESS &&
294 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
296 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
299 if (status == NSS_STATUS_SUCCESS)
300 return NSS_STATUS_SUCCESS;
301 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
304 return NSS_STATUS_NOTFOUND;
309 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
310 char *buffer, size_t buflen, int *errnop)
312 static const char *fct_name = "getgrnam_r";
313 static void *fct_start = NULL;
314 enum nss_status status;
317 enum nss_status (*l)(const char *name, struct group *grp,
318 char *buffer, size_t buflen, int *errnop);
322 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
323 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
324 return NSS_STATUS_UNAVAIL;
326 nip = nss_group_nonlocal_database();
328 return NSS_STATUS_UNAVAIL;
329 if (fct_start == NULL)
330 fct_start = __nss_lookup_function(nip, fct_name);
334 status = NSS_STATUS_UNAVAIL;
336 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
337 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
339 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
340 if (status != NSS_STATUS_SUCCESS)
343 return check_nonlocal_gid(name, grp->gr_gid, errnop);
347 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
348 char *buffer, size_t buflen, int *errnop)
350 static const char *fct_name = "getgrgid_r";
351 static void *fct_start = NULL;
352 enum nss_status status;
355 enum nss_status (*l)(gid_t gid, struct group *grp,
356 char *buffer, size_t buflen, int *errnop);
360 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
361 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
362 return NSS_STATUS_UNAVAIL;
364 nip = nss_group_nonlocal_database();
366 return NSS_STATUS_UNAVAIL;
367 if (fct_start == NULL)
368 fct_start = __nss_lookup_function(nip, fct_name);
372 status = NSS_STATUS_UNAVAIL;
374 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
375 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
377 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
378 if (status != NSS_STATUS_SUCCESS)
381 return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop);
385 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
386 long int *size, gid_t **groupsp, long int limit,
389 static const char *fct_name = "initgroups_dyn";
390 static void *fct_start = NULL;
391 enum nss_status status;
394 enum nss_status (*l)(const char *user, gid_t group, long int *start,
395 long int *size, gid_t **groupsp, long int limit,
400 struct group local_users_group, nonlocal_users_group;
401 gid_t local_users_gid, gid;
406 /* Check that the user is a nonlocal user before adding any groups. */
407 status = check_nonlocal_user(user, errnop);
408 if (status == NSS_STATUS_TRYAGAIN)
410 else if (status != NSS_STATUS_SUCCESS)
413 int old_errno = errno;
415 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
416 buffer = malloc(buflen);
417 if (buffer == NULL) {
420 return NSS_STATUS_TRYAGAIN;
422 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
423 &local_users_group, buffer, buflen, errnop);
424 if (status == NSS_STATUS_SUCCESS) {
425 local_users_gid = local_users_group.gr_gid;
426 } else if (status == NSS_STATUS_TRYAGAIN) {
430 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
431 MAGIC_LOCAL_GROUPNAME);
432 local_users_gid = -1;
437 gid = local_users_gid;
439 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
440 buffer = malloc(buflen);
441 if (buffer == NULL) {
444 return NSS_STATUS_TRYAGAIN;
446 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
447 &nonlocal_users_group, buffer, buflen, errnop);
448 if (status == NSS_STATUS_SUCCESS) {
449 gid = nonlocal_users_group.gr_gid;
450 } else if (status == NSS_STATUS_TRYAGAIN) {
454 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
455 MAGIC_NONLOCAL_GROUPNAME);
463 for (i = 0; i < *start; ++i)
464 if ((*groupsp)[i] == gid)
467 if (*start + 1 > *size) {
469 long int newsize = 2 * *size;
472 return NSS_STATUS_SUCCESS;
476 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
477 if (newgroups == NULL) {
480 return NSS_STATUS_TRYAGAIN;
482 *groupsp = newgroups;
485 (*groupsp)[(*start)++] = gid;
490 return NSS_STATUS_SUCCESS;
492 int in = *start, out = *start, i;
494 nip = nss_group_nonlocal_database();
496 return NSS_STATUS_UNAVAIL;
497 if (fct_start == NULL)
498 fct_start = __nss_lookup_function(nip, fct_name);
503 status = NSS_STATUS_UNAVAIL;
505 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
506 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
508 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
509 if (status != NSS_STATUS_SUCCESS)
512 for (; in < *start; ++in) {
513 int nonlocal_errno = *errnop;
515 for (i = 0; i < out; ++i)
516 if ((*groupsp)[i] == (*groupsp)[in])
521 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
522 if (local_users_gid == (*groupsp)[in]) {
523 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
524 user, MAGIC_LOCAL_GROUPNAME);
528 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
529 if (status == NSS_STATUS_SUCCESS) {
530 (*groupsp)[out++] = (*groupsp)[in];
531 } else if (status == NSS_STATUS_TRYAGAIN) {
533 *errnop = nonlocal_errno;
539 return NSS_STATUS_SUCCESS;