]> andersk Git - nss_nonlocal.git/blame - nonlocal-group.c
Switch from __nss_next API to __nss_next2 API
[nss_nonlocal.git] / nonlocal-group.c
CommitLineData
f6903667
AK
1/*
2 * nonlocal-group.c
3 * group database for nss_nonlocal proxy
4 *
96a1ee0f
AK
5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> and Tim
6 * Abbott <tabbott@mit.edu>
f6903667 7 *
96a1ee0f 8 * This file is part of nss_nonlocal.
f6903667 9 *
96a1ee0f
AK
10 * nss_nonlocal is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1 of
13 * the License, or (at your option) any later version.
f6903667 14 *
96a1ee0f
AK
15 * nss_nonlocal is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with nss_nonlocal; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * 02110-1301 USA
f6903667
AK
24 */
25
26#define _GNU_SOURCE
dc397f8f 27
f6903667 28#include <sys/types.h>
f6903667 29#include <dlfcn.h>
f6903667
AK
30#include <errno.h>
31#include <grp.h>
32#include <nss.h>
dc397f8f
AK
33#include <pwd.h>
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdlib.h>
37#include <string.h>
38#include <syslog.h>
39#include <unistd.h>
40
f6903667
AK
41#include "nsswitch-internal.h"
42#include "nonlocal.h"
43
6ca16423
AK
44/*
45 * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal
34cfeb28
AK
46 * users will be automatically added to it. Furthermore, if a local
47 * user is added to this group, then that user will inherit any
775f7dc3
AK
48 * nonlocal gids from a nonlocal user of the same name, as
49 * supplementary gids.
6ca16423 50 */
48479fc7 51#define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
6ca16423
AK
52
53/*
54 * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users
55 * will be automatically added to it.
56 */
48479fc7 57#define MAGIC_LOCAL_GROUPNAME "nss-local-users"
f7293a54 58
25468b96
AK
59/*
60 * If the MAGIC_NONLOCAL_USERNAME local user is added to a local
61 * group, then the local group will inherit the nonlocal membership of
62 * a group of the same gid.
63 */
64#define MAGIC_NONLOCAL_USERNAME "nss-nonlocal-users"
65
f6903667 66
c1812233
AK
67enum nss_status
68_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
69 char *buffer, size_t buflen, int *errnop);
70
71enum nss_status
72_nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
73 char *buffer, size_t buflen, int *errnop);
74
75
cbb0e3ea
AK
76static service_user *__nss_group_nonlocal_database;
77
78static int
79internal_function
d52c3f35
AK
80__nss_group_nonlocal_lookup2(service_user **ni, const char *fct_name,
81 const char *fct2_name, void **fctp)
f6903667 82{
cbb0e3ea
AK
83 if (__nss_group_nonlocal_database == NULL
84 && __nss_database_lookup("group_nonlocal", NULL, NULL,
85 &__nss_group_nonlocal_database) < 0)
86 return -1;
87
88 *ni = __nss_group_nonlocal_database;
48479fc7 89
cbb0e3ea 90 *fctp = __nss_lookup_function(*ni, fct_name);
d52c3f35
AK
91 if (*fctp == NULL && fct2_name != NULL)
92 *fctp = __nss_lookup_function(*ni, fct2_name);
cbb0e3ea 93 return 0;
f6903667
AK
94}
95
96
f6903667 97enum nss_status
25468b96 98check_nonlocal_gid(const char *user, const char *group, gid_t gid, int *errnop)
f6903667 99{
30fb3957 100 enum nss_status status;
f7293a54 101 struct group gbuf;
cbb0e3ea 102 char *buf;
8870ee9c 103 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
cbb0e3ea 104 const struct walk_nss w = {
d52c3f35 105 .lookup2 = &__nss_group_lookup2, .fct_name = "getgrgid_r",
cbb0e3ea
AK
106 .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
107 };
108 const __typeof__(&_nss_nonlocal_getgrgid_r) self = &_nss_nonlocal_getgrgid_r;
109#define args (gid, &gbuf, buf, buflen, errnop)
110#include "walk_nss.h"
111#undef args
30fb3957 112
25468b96
AK
113 if (status == NSS_STATUS_TRYAGAIN)
114 return status;
115 else if (status != NSS_STATUS_SUCCESS)
116 return NSS_STATUS_SUCCESS;
117
118 if (group == NULL || strcmp(gbuf.gr_name, group) == 0) {
119 char *const *mem;
120 for (mem = gbuf.gr_mem; *mem != NULL; mem++)
121 if (strcmp(*mem, MAGIC_NONLOCAL_USERNAME) == 0) {
122 status = check_nonlocal_user(*mem, errnop);
123 if (status == NSS_STATUS_TRYAGAIN) {
124 free(buf);
125 return status;
126 } else if (status == NSS_STATUS_NOTFOUND) {
127 free(buf);
128 return NSS_STATUS_SUCCESS;
129 }
130 break;
131 }
f6903667 132 }
30fb3957 133
25468b96
AK
134 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
135 free(buf);
136 return NSS_STATUS_NOTFOUND;
f6903667
AK
137}
138
cf338316
AK
139enum nss_status
140check_nonlocal_group(const char *user, struct group *grp, int *errnop)
141{
a07a7616
AK
142 enum nss_status status = NSS_STATUS_SUCCESS;
143 int old_errno = errno;
144 char *end;
145 unsigned long gid;
146
147 errno = 0;
148 gid = strtoul(grp->gr_name, &end, 10);
f4061d47
AK
149 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
150 errno = old_errno;
25468b96 151 status = check_nonlocal_gid(user, grp->gr_name, gid, errnop);
f4061d47
AK
152 } else
153 errno = old_errno;
a07a7616
AK
154 if (status != NSS_STATUS_SUCCESS)
155 return status;
156
25468b96 157 return check_nonlocal_gid(user, grp->gr_name, grp->gr_gid, errnop);
cf338316
AK
158}
159
48479fc7 160enum nss_status
8aa9014e 161get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
48479fc7 162{
30fb3957 163 enum nss_status status;
cbb0e3ea
AK
164 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
165 const struct walk_nss w = {
d52c3f35 166 .lookup2 = &__nss_group_lookup2, .fct_name = "getgrnam_r",
cbb0e3ea
AK
167 .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen
168 };
169 const __typeof__(&_nss_nonlocal_getgrnam_r) self = &_nss_nonlocal_getgrnam_r;
170#define args (name, grp, *buffer, buflen, errnop)
171#include "walk_nss.h"
172#undef args
48479fc7
TA
173 return status;
174}
f6903667 175
48939704 176static bool grent_initialized = false;
cbb0e3ea 177static service_user *grent_startp, *grent_nip;
f6903667
AK
178static void *grent_fct_start;
179static union {
180 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
181 int *errnop);
182 void *ptr;
183} grent_fct;
184static const char *grent_fct_name = "getgrent_r";
185
186enum nss_status
187_nss_nonlocal_setgrent(int stayopen)
188{
f6903667 189 enum nss_status status;
cbb0e3ea 190 const struct walk_nss w = {
d52c3f35 191 .lookup2 = &__nss_group_nonlocal_lookup2, .fct_name = "setgrent",
cbb0e3ea
AK
192 .status = &status
193 };
194 const __typeof__(&_nss_nonlocal_setgrent) self = NULL;
195#define args (stayopen)
196#include "walk_nss.h"
197#undef args
f6903667
AK
198 if (status != NSS_STATUS_SUCCESS)
199 return status;
200
48939704 201 if (!grent_initialized) {
d52c3f35
AK
202 __nss_group_nonlocal_lookup2(&grent_startp, grent_fct_name, NULL,
203 &grent_fct_start);
48939704
AK
204 __sync_synchronize();
205 grent_initialized = true;
206 }
cbb0e3ea 207 grent_nip = grent_startp;
f6903667
AK
208 grent_fct.ptr = grent_fct_start;
209 return NSS_STATUS_SUCCESS;
210}
211
212enum nss_status
213_nss_nonlocal_endgrent(void)
214{
f6903667 215 enum nss_status status;
cbb0e3ea 216 const struct walk_nss w = {
d52c3f35 217 .lookup2 = &__nss_group_nonlocal_lookup2, .fct_name = "endgrent",
23ca0ded 218 .status = &status, .all_values = 1,
cbb0e3ea
AK
219 };
220 const __typeof__(&_nss_nonlocal_endgrent) self = NULL;
f6903667
AK
221
222 grent_nip = NULL;
223
cbb0e3ea
AK
224#define args ()
225#include "walk_nss.h"
226#undef args
f6903667
AK
227 return status;
228}
229
230enum nss_status
231_nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
232 int *errnop)
233{
234 enum nss_status status;
a360549e
TA
235
236 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 237 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
a360549e
TA
238 return NSS_STATUS_UNAVAIL;
239
f6903667
AK
240 if (grent_nip == NULL) {
241 status = _nss_nonlocal_setgrent(0);
242 if (status != NSS_STATUS_SUCCESS)
243 return status;
244 }
245 do {
246 if (grent_fct.ptr == NULL)
247 status = NSS_STATUS_UNAVAIL;
248 else {
249 int nonlocal_errno;
250 do
251 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
252 while (status == NSS_STATUS_SUCCESS &&
cf338316 253 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
f6903667
AK
254 }
255 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
256 return status;
257
258 if (status == NSS_STATUS_SUCCESS)
259 return NSS_STATUS_SUCCESS;
d52c3f35
AK
260 } while (__nss_next2(&grent_nip, grent_fct_name, NULL, &grent_fct.ptr,
261 status, 0) == 0);
f6903667
AK
262
263 grent_nip = NULL;
264 return NSS_STATUS_NOTFOUND;
265}
266
267
268enum nss_status
269_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
270 char *buffer, size_t buflen, int *errnop)
271{
f6903667 272 enum nss_status status;
cbb0e3ea 273 const struct walk_nss w = {
d52c3f35 274 .lookup2 = &__nss_group_nonlocal_lookup2, .fct_name = "getgrnam_r",
cbb0e3ea
AK
275 .status = &status, .errnop = errnop
276 };
277 const __typeof__(&_nss_nonlocal_getgrnam_r) self = NULL;
f6903667 278
a360549e 279 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 280 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
48479fc7
TA
281 return NSS_STATUS_UNAVAIL;
282
cbb0e3ea
AK
283#define args (name, grp, buffer, buflen, errnop)
284#include "walk_nss.h"
285#undef args
f6903667
AK
286 if (status != NSS_STATUS_SUCCESS)
287 return status;
288
22562df0
AK
289 if (strcmp(name, grp->gr_name) != 0) {
290 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
291 return NSS_STATUS_NOTFOUND;
292 }
293
cf338316 294 return check_nonlocal_group(name, grp, errnop);
f6903667
AK
295}
296
297enum nss_status
298_nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
299 char *buffer, size_t buflen, int *errnop)
300{
f6903667 301 enum nss_status status;
cbb0e3ea 302 const struct walk_nss w = {
d52c3f35 303 .lookup2 = &__nss_group_nonlocal_lookup2, .fct_name = "getgrgid_r",
cbb0e3ea
AK
304 .status = &status, .errnop = errnop
305 };
306 const __typeof__(&_nss_nonlocal_getgrgid_r) self = NULL;
f6903667 307
a360549e 308 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 309 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
f6903667
AK
310 return NSS_STATUS_UNAVAIL;
311
cbb0e3ea
AK
312#define args (gid, grp, buffer, buflen, errnop)
313#include "walk_nss.h"
314#undef args
f6903667
AK
315 if (status != NSS_STATUS_SUCCESS)
316 return status;
317
8027fdc4
AK
318 if (gid != grp->gr_gid) {
319 syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
320 return NSS_STATUS_NOTFOUND;
321 }
322
cf338316 323 return check_nonlocal_group(grp->gr_name, grp, errnop);
f6903667
AK
324}
325
dfc6e292
AK
326static bool
327add_group(gid_t group, long int *start, long int *size, gid_t **groupsp,
328 long int limit, int *errnop, enum nss_status *status)
329{
330 int i, old_errno = errno;
331 for (i = 0; i < *start; ++i)
332 if ((*groupsp)[i] == group)
333 return true;
334 if (*start + 1 > *size) {
335 gid_t *newgroups;
336 long int newsize = 2 * *size;
337 if (limit > 0) {
338 if (*size >= limit) {
339 *status = NSS_STATUS_SUCCESS;
340 return false;
341 }
342 if (newsize > limit)
343 newsize = limit;
344 }
345 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
346 errno = old_errno;
347 if (newgroups == NULL) {
348 *errnop = ENOMEM;
349 *status = NSS_STATUS_TRYAGAIN;
350 return false;
351 }
352 *groupsp = newgroups;
353 *size = newsize;
354 }
355 (*groupsp)[(*start)++] = group;
356 return true;
357}
358
f6903667
AK
359enum nss_status
360_nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
361 long int *size, gid_t **groupsp, long int limit,
362 int *errnop)
363{
f6903667 364 enum nss_status status;
cbb0e3ea 365 const struct walk_nss w = {
d52c3f35 366 .lookup2 = &__nss_group_nonlocal_lookup2, .fct_name = "initgroups_dyn",
23ca0ded 367 .status = &status, .all_values = 1, .errnop = errnop
cbb0e3ea
AK
368 };
369 const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL;
1e78305d
AK
370
371 struct group local_users_group, nonlocal_users_group;
b3831d60 372 bool is_nonlocal = true;
1e78305d 373 char *buffer;
d7bf1d11 374 int in, out, i;
48479fc7 375
34cfeb28
AK
376 /* Check that the user is a nonlocal user, or a member of the
377 * MAGIC_NONLOCAL_GROUPNAME group, before adding any groups. */
48479fc7 378 status = check_nonlocal_user(user, errnop);
3010a54b 379 if (status == NSS_STATUS_TRYAGAIN) {
48479fc7 380 return status;
3010a54b 381 } else if (status != NSS_STATUS_SUCCESS) {
b3831d60 382 is_nonlocal = false;
48479fc7 383
3010a54b
AK
384 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
385 &local_users_group, &buffer, errnop);
386 if (status == NSS_STATUS_SUCCESS) {
387 free(buffer);
388 if (!add_group(local_users_group.gr_gid, start, size, groupsp,
389 limit, errnop, &status))
390 return status;
391 } else if (status == NSS_STATUS_TRYAGAIN) {
dfc6e292 392 return status;
3010a54b
AK
393 } else {
394 syslog(LOG_WARNING,
395 "nss_nonlocal: Group %s does not exist locally!",
396 MAGIC_LOCAL_GROUPNAME);
397 }
a4e1e153
AK
398 }
399
400 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
401 &nonlocal_users_group, &buffer, errnop);
402 if (status == NSS_STATUS_SUCCESS) {
403 free(buffer);
404 if (is_nonlocal) {
dfc6e292
AK
405 if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
406 limit, errnop, &status))
407 return status;
34cfeb28
AK
408 } else {
409 int i;
410 for (i = 0; i < *start; ++i) {
411 if ((*groupsp)[i] == nonlocal_users_group.gr_gid) {
412 is_nonlocal = true;
413 break;
414 }
415 }
775f7dc3
AK
416
417 if (is_nonlocal) {
418 struct passwd pwbuf;
419 char *buf;
420 int nonlocal_errno = *errnop;
421 status = get_nonlocal_passwd(user, &pwbuf, &buf, errnop);
422
423 if (status == NSS_STATUS_SUCCESS) {
424 nonlocal_errno = *errnop;
25468b96 425 status = check_nonlocal_gid(user, NULL, pwbuf.pw_gid,
775f7dc3
AK
426 &nonlocal_errno);
427 free(buf);
428 }
429
430 if (status == NSS_STATUS_SUCCESS) {
431 if (!add_group(pwbuf.pw_gid, start, size, groupsp, limit,
432 errnop, &status))
433 return status;
434 } else if (status == NSS_STATUS_TRYAGAIN) {
435 *errnop = nonlocal_errno;
436 return status;
437 }
438 }
1e78305d 439 }
a4e1e153
AK
440 } else if (status == NSS_STATUS_TRYAGAIN) {
441 if (is_nonlocal)
442 return status;
443 } else {
444 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
445 MAGIC_NONLOCAL_GROUPNAME);
48479fc7 446 }
f6903667 447
b3831d60 448 if (!is_nonlocal)
1e78305d
AK
449 return NSS_STATUS_SUCCESS;
450
d7bf1d11 451 in = out = *start;
1e78305d 452
cbb0e3ea
AK
453#define args (user, group, start, size, groupsp, limit, errnop)
454#include "walk_nss.h"
455#undef args
18c6e9c7
AK
456 if (status == NSS_STATUS_NOTFOUND || status == NSS_STATUS_UNAVAIL)
457 return NSS_STATUS_SUCCESS;
458 else if (status != NSS_STATUS_SUCCESS)
f6903667
AK
459 return status;
460
461 for (; in < *start; ++in) {
462 int nonlocal_errno = *errnop;
463
464 for (i = 0; i < out; ++i)
465 if ((*groupsp)[i] == (*groupsp)[in])
466 break;
467 if (i < out)
468 continue;
469
25468b96
AK
470 status = check_nonlocal_gid(user, NULL, (*groupsp)[in],
471 &nonlocal_errno);
f6903667 472 if (status == NSS_STATUS_SUCCESS) {
1e78305d 473 (*groupsp)[out++] = (*groupsp)[in];
5879a696 474 } else if (status == NSS_STATUS_TRYAGAIN) {
f6903667
AK
475 *start = out;
476 *errnop = nonlocal_errno;
477 return status;
478 }
479 }
480
481 *start = out;
482 return NSS_STATUS_SUCCESS;
483}
This page took 0.58979 seconds and 5 git commands to generate.