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_LOCAL_GR_BUFLEN (sysconf(_SC_GETGR_R_SIZE_MAX) + 7)
45 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
46 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
50 nss_group_nonlocal_database(void)
52 static service_user *nip = NULL;
54 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
61 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
63 enum nss_status status = NSS_STATUS_SUCCESS;
65 struct group *gbufp = &gbuf;
67 int old_errno = errno;
68 int buflen = MAGIC_LOCAL_GR_BUFLEN;
69 char *buf = malloc(buflen);
73 return NSS_STATUS_TRYAGAIN;
75 ret = getgrgid_r(gid, gbufp, buf, buflen, &gbufp);
78 status = NSS_STATUS_TRYAGAIN;
79 } else if (gbufp != NULL) {
80 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
81 status = NSS_STATUS_NOTFOUND;
89 get_local_group(const char *name, struct group *grp, char *buffer, size_t buflen, int *errnop)
91 enum nss_status status = NSS_STATUS_NOTFOUND;
93 struct group *gbufp = &gbuf;
95 int old_errno = errno;
96 int len = MAGIC_LOCAL_GR_BUFLEN;
97 char *buf = malloc(len);
101 return NSS_STATUS_TRYAGAIN;
103 ret = getgrnam_r(name, gbufp, buf, len, &gbufp);
106 status = NSS_STATUS_TRYAGAIN;
107 } else if (gbufp != NULL) {
108 status = NSS_STATUS_SUCCESS;
110 n = snprintf(buffer, buflen, "%s", gbufp->gr_name);
111 if (n < 0 || n >= buflen) {
113 status = NSS_STATUS_TRYAGAIN;
114 goto get_local_group_done;
116 grp->gr_name = buffer;
120 n = snprintf(buffer, buflen, "%s", gbufp->gr_passwd);
121 if (n < 0 || n >= buflen) {
123 status = NSS_STATUS_TRYAGAIN;
124 goto get_local_group_done;
126 grp->gr_passwd = buffer;
130 grp->gr_gid = gbufp->gr_gid;
132 if (buflen < sizeof(void *)) {
134 status = NSS_STATUS_TRYAGAIN;
135 goto get_local_group_done;
137 *(void **)buffer = NULL;
138 buffer += sizeof(void *);
139 buflen -= sizeof(void *);
141 get_local_group_done:
147 static service_user *grent_nip = NULL;
148 static void *grent_fct_start;
150 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
154 static const char *grent_fct_name = "getgrent_r";
157 _nss_nonlocal_setgrent(int stayopen)
159 static const char *fct_name = "setgrent";
160 static void *fct_start = NULL;
161 enum nss_status status;
164 enum nss_status (*l)(int stayopen);
168 nip = nss_group_nonlocal_database();
170 return NSS_STATUS_UNAVAIL;
171 if (fct_start == NULL)
172 fct_start = __nss_lookup_function(nip, fct_name);
176 status = NSS_STATUS_UNAVAIL;
178 status = DL_CALL_FCT(fct.l, (stayopen));
179 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
180 if (status != NSS_STATUS_SUCCESS)
184 if (grent_fct_start == NULL)
185 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
186 grent_fct.ptr = grent_fct_start;
187 return NSS_STATUS_SUCCESS;
191 _nss_nonlocal_endgrent(void)
193 static const char *fct_name = "endgrent";
194 static void *fct_start = NULL;
195 enum nss_status status;
198 enum nss_status (*l)(void);
204 nip = nss_group_nonlocal_database();
206 return NSS_STATUS_UNAVAIL;
207 if (fct_start == NULL)
208 fct_start = __nss_lookup_function(nip, fct_name);
212 status = NSS_STATUS_UNAVAIL;
214 status = DL_CALL_FCT(fct.l, ());
215 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
220 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
223 enum nss_status status;
225 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
226 if (buflen == MAGIC_LOCAL_GR_BUFLEN ||
227 (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0'))
228 return NSS_STATUS_UNAVAIL;
230 if (grent_nip == NULL) {
231 status = _nss_nonlocal_setgrent(0);
232 if (status != NSS_STATUS_SUCCESS)
236 if (grent_fct.ptr == NULL)
237 status = NSS_STATUS_UNAVAIL;
241 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
242 while (status == NSS_STATUS_SUCCESS &&
243 check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
245 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
248 if (status == NSS_STATUS_SUCCESS)
249 return NSS_STATUS_SUCCESS;
250 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
253 return NSS_STATUS_NOTFOUND;
258 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
259 char *buffer, size_t buflen, int *errnop)
261 static const char *fct_name = "getgrnam_r";
262 static void *fct_start = NULL;
263 enum nss_status status;
266 enum nss_status (*l)(const char *name, struct group *grp,
267 char *buffer, size_t buflen, int *errnop);
271 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
272 if (buflen == MAGIC_LOCAL_GR_BUFLEN ||
273 (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0'))
274 return NSS_STATUS_UNAVAIL;
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, (name, grp, buffer, buflen, errnop));
287 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
289 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
290 if (status != NSS_STATUS_SUCCESS)
293 return check_nonlocal_gid(name, grp->gr_gid, errnop);
297 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
298 char *buffer, size_t buflen, int *errnop)
300 static const char *fct_name = "getgrgid_r";
301 static void *fct_start = NULL;
302 enum nss_status status;
305 enum nss_status (*l)(gid_t gid, struct group *grp,
306 char *buffer, size_t buflen, int *errnop);
310 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
311 if (buflen == MAGIC_LOCAL_GR_BUFLEN ||
312 (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, (gid, 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(grp->gr_name, grp->gr_gid, errnop);
336 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
337 long int *size, gid_t **groupsp, long int limit,
340 static const char *fct_name = "initgroups_dyn";
341 static void *fct_start = NULL;
342 enum nss_status status;
345 enum nss_status (*l)(const char *user, gid_t group, long int *start,
346 long int *size, gid_t **groupsp, long int limit,
351 struct group local_users_group, nonlocal_users_group;
352 gid_t local_users_gid, gid;
357 /* Check that the user is a nonlocal user before adding any groups. */
358 status = check_nonlocal_user(user, errnop);
359 if (status == NSS_STATUS_TRYAGAIN)
361 else if (status != NSS_STATUS_SUCCESS)
364 int old_errno = errno;
366 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
367 buffer = malloc(buflen);
368 if (buffer == NULL) {
371 return NSS_STATUS_TRYAGAIN;
373 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
374 &local_users_group, buffer, buflen, errnop);
375 if (status == NSS_STATUS_SUCCESS) {
376 local_users_gid = local_users_group.gr_gid;
377 } else if (status == NSS_STATUS_TRYAGAIN) {
381 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
382 MAGIC_LOCAL_GROUPNAME);
383 local_users_gid = -1;
388 gid = local_users_gid;
390 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
391 buffer = malloc(buflen);
392 if (buffer == NULL) {
395 return NSS_STATUS_TRYAGAIN;
397 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
398 &nonlocal_users_group, buffer, buflen, errnop);
399 if (status == NSS_STATUS_SUCCESS) {
400 gid = nonlocal_users_group.gr_gid;
401 } else if (status == NSS_STATUS_TRYAGAIN) {
406 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
407 MAGIC_NONLOCAL_GROUPNAME);
415 for (i = 0; i < *start; ++i)
416 if ((*groupsp)[i] == gid)
419 if (*start + 1 > *size) {
421 long int newsize = 2 * *size;
424 return NSS_STATUS_SUCCESS;
428 newgroups = realloc(*groupsp, *size * sizeof((*groupsp)[0]));
429 if (newgroups == NULL) {
432 return NSS_STATUS_TRYAGAIN;
434 *groupsp = newgroups;
437 (*groupsp)[(*start)++] = gid;
444 return NSS_STATUS_SUCCESS;
446 int in = *start, out = *start, i;
448 nip = nss_group_nonlocal_database();
450 return NSS_STATUS_UNAVAIL;
451 if (fct_start == NULL)
452 fct_start = __nss_lookup_function(nip, fct_name);
457 status = NSS_STATUS_UNAVAIL;
459 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
460 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
462 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
463 if (status != NSS_STATUS_SUCCESS)
466 for (; in < *start; ++in) {
467 int nonlocal_errno = *errnop;
469 for (i = 0; i < out; ++i)
470 if ((*groupsp)[i] == (*groupsp)[in])
475 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
476 if (local_users_gid == (*groupsp)[in]) {
477 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
478 user, MAGIC_LOCAL_GROUPNAME);
482 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
483 if (status == NSS_STATUS_SUCCESS) {
484 (*groupsp)[out++] = (*groupsp)[in];
485 } else if (status == NSS_STATUS_TRYAGAIN) {
487 *errnop = nonlocal_errno;
493 return NSS_STATUS_SUCCESS;