]> andersk Git - nss_nonlocal.git/blame - nonlocal-group.c
Add check_nonlocal_passwd and check_nonlocal_group helper functions.
[nss_nonlocal.git] / nonlocal-group.c
CommitLineData
f6903667
AK
1/*
2 * nonlocal-group.c
3 * group database for nss_nonlocal proxy
4 *
48479fc7
TA
5 * Copyright © 2007 Anders Kaseorg <andersk@mit.edu> and Tim Abbott
6 * <tabbott@mit.edu>
f6903667
AK
7 *
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:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
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
26 * SOFTWARE.
27 */
28
29#define _GNU_SOURCE
30#include <sys/types.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include <stdint.h>
34#include <string.h>
35#include <dlfcn.h>
36#include <stdio.h>
37#include <syslog.h>
38#include <errno.h>
39#include <grp.h>
40#include <nss.h>
41#include "nsswitch-internal.h"
42#include "nonlocal.h"
43
48479fc7
TA
44#define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
45#define MAGIC_LOCAL_GROUPNAME "nss-local-users"
f7293a54 46
f6903667 47
c1812233
AK
48enum nss_status
49_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
50 char *buffer, size_t buflen, int *errnop);
51
52enum nss_status
53_nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
54 char *buffer, size_t buflen, int *errnop);
55
56
f6903667
AK
57static service_user *
58nss_group_nonlocal_database(void)
59{
60 static service_user *nip = NULL;
61 if (nip == NULL)
62 __nss_database_lookup("group_nonlocal", NULL, "", &nip);
48479fc7 63
f6903667
AK
64 return nip;
65}
66
67
f6903667
AK
68enum nss_status
69check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
70{
30fb3957
AK
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;
75 service_user *nip;
76 union {
77 enum nss_status (*l)(gid_t gid, struct group *grp,
78 char *buffer, size_t buflen, int *errnop);
79 void *ptr;
80 } fct;
f7293a54 81 struct group gbuf;
f7293a54 82 int old_errno = errno;
c1812233 83
8870ee9c 84 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
f7293a54
AK
85 char *buf = malloc(buflen);
86 if (buf == NULL) {
f6903667 87 *errnop = ENOMEM;
f7293a54 88 errno = old_errno;
f6903667
AK
89 return NSS_STATUS_TRYAGAIN;
90 }
30fb3957
AK
91
92 if (fct_start == NULL &&
93 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
94 free(buf);
95 return NSS_STATUS_UNAVAIL;
96 }
97 nip = startp;
98 fct.ptr = fct_start;
99 do {
d905b369 100 morebuf:
c1812233
AK
101 if (fct.l == _nss_nonlocal_getgrgid_r)
102 status = NSS_STATUS_NOTFOUND;
103 else
104 status = DL_CALL_FCT(fct.l, (gid, &gbuf, buf, buflen, errnop));
d905b369
AK
105 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
106 free(buf);
107 buflen *= 2;
108 buf = malloc(buflen);
109 if (buf == NULL) {
110 *errnop = ENOMEM;
111 errno = old_errno;
112 return NSS_STATUS_TRYAGAIN;
113 }
114 goto morebuf;
115 }
30fb3957
AK
116 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
117
118 if (status == NSS_STATUS_SUCCESS) {
f7293a54 119 syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
f6903667 120 status = NSS_STATUS_NOTFOUND;
30fb3957
AK
121 } else if (status != NSS_STATUS_TRYAGAIN) {
122 status = NSS_STATUS_SUCCESS;
f6903667 123 }
30fb3957 124
f7293a54 125 free(buf);
f6903667
AK
126 return status;
127}
128
cf338316
AK
129enum nss_status
130check_nonlocal_group(const char *user, struct group *grp, int *errnop)
131{
132 return check_nonlocal_gid(user, grp->gr_gid, errnop);
133}
134
48479fc7 135enum nss_status
8aa9014e 136get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
48479fc7 137{
30fb3957
AK
138 static const char *fct_name = "getgrnam_r";
139 static service_user *startp = NULL;
140 static void *fct_start = NULL;
141 enum nss_status status;
142 service_user *nip;
143 union {
144 enum nss_status (*l)(const char *name, struct group *grp,
145 char *buffer, size_t buflen, int *errnop);
146 void *ptr;
147 } fct;
8aa9014e 148 size_t buflen;
48479fc7 149 int old_errno = errno;
c1812233 150
8aa9014e
AK
151 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
152 *buffer = malloc(buflen);
153 if (*buffer == NULL) {
48479fc7
TA
154 *errnop = ENOMEM;
155 errno = old_errno;
156 return NSS_STATUS_TRYAGAIN;
157 }
1e78305d 158
30fb3957
AK
159 if (fct_start == NULL &&
160 __nss_group_lookup(&startp, fct_name, &fct_start) != 0) {
8aa9014e
AK
161 free(*buffer);
162 *buffer = NULL;
30fb3957
AK
163 return NSS_STATUS_UNAVAIL;
164 }
165 nip = startp;
166 fct.ptr = fct_start;
167 do {
d905b369 168 morebuf:
c1812233
AK
169 if (fct.l == _nss_nonlocal_getgrnam_r)
170 status = NSS_STATUS_NOTFOUND;
171 else
8aa9014e 172 status = DL_CALL_FCT(fct.l, (name, grp, *buffer, buflen, errnop));
d905b369 173 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) {
8aa9014e
AK
174 free(*buffer);
175 buflen *= 2;
176 *buffer = malloc(buflen);
177 if (*buffer == NULL) {
d905b369
AK
178 *errnop = ENOMEM;
179 errno = old_errno;
180 return NSS_STATUS_TRYAGAIN;
181 }
182 goto morebuf;
183 }
30fb3957
AK
184 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
185
8aa9014e
AK
186 if (status != NSS_STATUS_SUCCESS) {
187 free(*buffer);
188 *buffer = NULL;
48479fc7 189 }
30fb3957 190
48479fc7
TA
191 return status;
192}
f6903667
AK
193
194static service_user *grent_nip = NULL;
195static void *grent_fct_start;
196static union {
197 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
198 int *errnop);
199 void *ptr;
200} grent_fct;
201static const char *grent_fct_name = "getgrent_r";
202
203enum nss_status
204_nss_nonlocal_setgrent(int stayopen)
205{
206 static const char *fct_name = "setgrent";
207 static void *fct_start = NULL;
208 enum nss_status status;
209 service_user *nip;
210 union {
211 enum nss_status (*l)(int stayopen);
212 void *ptr;
213 } fct;
214
215 nip = nss_group_nonlocal_database();
216 if (nip == NULL)
217 return NSS_STATUS_UNAVAIL;
218 if (fct_start == NULL)
219 fct_start = __nss_lookup_function(nip, fct_name);
220 fct.ptr = fct_start;
221 do {
222 if (fct.ptr == NULL)
223 status = NSS_STATUS_UNAVAIL;
224 else
225 status = DL_CALL_FCT(fct.l, (stayopen));
226 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
227 if (status != NSS_STATUS_SUCCESS)
228 return status;
229
230 grent_nip = nip;
231 if (grent_fct_start == NULL)
232 grent_fct_start = __nss_lookup_function(nip, grent_fct_name);
233 grent_fct.ptr = grent_fct_start;
234 return NSS_STATUS_SUCCESS;
235}
236
237enum nss_status
238_nss_nonlocal_endgrent(void)
239{
240 static const char *fct_name = "endgrent";
241 static void *fct_start = NULL;
242 enum nss_status status;
243 service_user *nip;
244 union {
245 enum nss_status (*l)(void);
246 void *ptr;
247 } fct;
248
249 grent_nip = NULL;
250
251 nip = nss_group_nonlocal_database();
252 if (nip == NULL)
253 return NSS_STATUS_UNAVAIL;
254 if (fct_start == NULL)
255 fct_start = __nss_lookup_function(nip, fct_name);
256 fct.ptr = fct_start;
257 do {
258 if (fct.ptr == NULL)
259 status = NSS_STATUS_UNAVAIL;
260 else
261 status = DL_CALL_FCT(fct.l, ());
262 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
263 return status;
264}
265
266enum nss_status
267_nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
268 int *errnop)
269{
270 enum nss_status status;
a360549e
TA
271
272 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 273 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
a360549e
TA
274 return NSS_STATUS_UNAVAIL;
275
f6903667
AK
276 if (grent_nip == NULL) {
277 status = _nss_nonlocal_setgrent(0);
278 if (status != NSS_STATUS_SUCCESS)
279 return status;
280 }
281 do {
282 if (grent_fct.ptr == NULL)
283 status = NSS_STATUS_UNAVAIL;
284 else {
285 int nonlocal_errno;
286 do
287 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
288 while (status == NSS_STATUS_SUCCESS &&
cf338316 289 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
f6903667
AK
290 }
291 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
292 return status;
293
294 if (status == NSS_STATUS_SUCCESS)
295 return NSS_STATUS_SUCCESS;
296 } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
297
298 grent_nip = NULL;
299 return NSS_STATUS_NOTFOUND;
300}
301
302
303enum nss_status
304_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
305 char *buffer, size_t buflen, int *errnop)
306{
307 static const char *fct_name = "getgrnam_r";
308 static void *fct_start = NULL;
309 enum nss_status status;
310 service_user *nip;
311 union {
312 enum nss_status (*l)(const char *name, struct group *grp,
313 char *buffer, size_t buflen, int *errnop);
314 void *ptr;
315 } fct;
316
a360549e 317 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 318 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
48479fc7
TA
319 return NSS_STATUS_UNAVAIL;
320
f6903667
AK
321 nip = nss_group_nonlocal_database();
322 if (nip == NULL)
323 return NSS_STATUS_UNAVAIL;
324 if (fct_start == NULL)
325 fct_start = __nss_lookup_function(nip, fct_name);
326 fct.ptr = fct_start;
327 do {
328 if (fct.ptr == NULL)
329 status = NSS_STATUS_UNAVAIL;
330 else
331 status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop));
332 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
333 break;
334 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
335 if (status != NSS_STATUS_SUCCESS)
336 return status;
337
22562df0
AK
338 if (strcmp(name, grp->gr_name) != 0) {
339 syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
340 return NSS_STATUS_NOTFOUND;
341 }
342
cf338316 343 return check_nonlocal_group(name, grp, errnop);
f6903667
AK
344}
345
346enum nss_status
347_nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
348 char *buffer, size_t buflen, int *errnop)
349{
350 static const char *fct_name = "getgrgid_r";
351 static void *fct_start = NULL;
352 enum nss_status status;
353 service_user *nip;
354 union {
355 enum nss_status (*l)(gid_t gid, struct group *grp,
356 char *buffer, size_t buflen, int *errnop);
357 void *ptr;
358 } fct;
359
a360549e 360 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 361 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
f6903667
AK
362 return NSS_STATUS_UNAVAIL;
363
364 nip = nss_group_nonlocal_database();
365 if (nip == NULL)
366 return NSS_STATUS_UNAVAIL;
367 if (fct_start == NULL)
368 fct_start = __nss_lookup_function(nip, fct_name);
369 fct.ptr = fct_start;
370 do {
371 if (fct.ptr == NULL)
372 status = NSS_STATUS_UNAVAIL;
373 else
374 status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop));
375 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
376 break;
377 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
378 if (status != NSS_STATUS_SUCCESS)
379 return status;
380
cf338316 381 return check_nonlocal_group(grp->gr_name, grp, errnop);
f6903667
AK
382}
383
384enum nss_status
385_nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
386 long int *size, gid_t **groupsp, long int limit,
387 int *errnop)
388{
389 static const char *fct_name = "initgroups_dyn";
390 static void *fct_start = NULL;
391 enum nss_status status;
392 service_user *nip;
393 union {
394 enum nss_status (*l)(const char *user, gid_t group, long int *start,
395 long int *size, gid_t **groupsp, long int limit,
396 int *errnop);
397 void *ptr;
398 } fct;
1e78305d
AK
399
400 struct group local_users_group, nonlocal_users_group;
401 gid_t local_users_gid, gid;
48479fc7 402 int is_local = 0;
1e78305d 403 char *buffer;
d7bf1d11
AK
404 int old_errno;
405 int in, out, i;
48479fc7 406
1e78305d 407 /* Check that the user is a nonlocal user before adding any groups. */
48479fc7 408 status = check_nonlocal_user(user, errnop);
5879a696 409 if (status == NSS_STATUS_TRYAGAIN)
48479fc7 410 return status;
5879a696
AK
411 else if (status != NSS_STATUS_SUCCESS)
412 is_local = 1;
48479fc7 413
d7bf1d11 414 old_errno = errno;
1e78305d 415
48479fc7 416 status = get_local_group(MAGIC_LOCAL_GROUPNAME,
8aa9014e 417 &local_users_group, &buffer, errnop);
5879a696
AK
418 if (status == NSS_STATUS_SUCCESS) {
419 local_users_gid = local_users_group.gr_gid;
dc94c928 420 free(buffer);
8aa9014e 421 } else if (status == NSS_STATUS_TRYAGAIN) {
5879a696
AK
422 return status;
423 } else {
48479fc7
TA
424 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
425 MAGIC_LOCAL_GROUPNAME);
1e78305d 426 local_users_gid = -1;
5879a696 427 }
1e78305d
AK
428
429 if (is_local) {
430 gid = local_users_gid;
431 } else {
1e78305d 432 status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
8aa9014e 433 &nonlocal_users_group, &buffer, errnop);
5879a696
AK
434 if (status == NSS_STATUS_SUCCESS) {
435 gid = nonlocal_users_group.gr_gid;
dc94c928 436 free(buffer);
8aa9014e 437 } else if (status == NSS_STATUS_TRYAGAIN) {
5879a696
AK
438 return status;
439 } else {
1e78305d
AK
440 syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
441 MAGIC_NONLOCAL_GROUPNAME);
442 gid = -1;
5879a696 443 }
48479fc7 444 }
1e78305d
AK
445
446 if (gid != -1) {
447 int i;
448 for (i = 0; i < *start; ++i)
449 if ((*groupsp)[i] == gid)
450 break;
451 if (i >= *start) {
452 if (*start + 1 > *size) {
453 gid_t *newgroups;
454 long int newsize = 2 * *size;
455 if (limit > 0) {
456 if (*size >= limit)
457 return NSS_STATUS_SUCCESS;
458 if (newsize > limit)
459 newsize = limit;
460 }
ad0399bd 461 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
1e78305d
AK
462 if (newgroups == NULL) {
463 *errnop = ENOMEM;
464 errno = old_errno;
465 return NSS_STATUS_TRYAGAIN;
466 }
467 *groupsp = newgroups;
468 *size = newsize;
469 }
470 (*groupsp)[(*start)++] = gid;
471 }
48479fc7 472 }
f6903667 473
1e78305d
AK
474 if (is_local)
475 return NSS_STATUS_SUCCESS;
476
d7bf1d11 477 in = out = *start;
1e78305d 478
f6903667
AK
479 nip = nss_group_nonlocal_database();
480 if (nip == NULL)
481 return NSS_STATUS_UNAVAIL;
482 if (fct_start == NULL)
483 fct_start = __nss_lookup_function(nip, fct_name);
484 fct.ptr = fct_start;
485
486 do {
487 if (fct.ptr == NULL)
488 status = NSS_STATUS_UNAVAIL;
489 else
490 status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop));
491 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
492 break;
493 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
494 if (status != NSS_STATUS_SUCCESS)
495 return status;
496
497 for (; in < *start; ++in) {
498 int nonlocal_errno = *errnop;
499
500 for (i = 0; i < out; ++i)
501 if ((*groupsp)[i] == (*groupsp)[in])
502 break;
503 if (i < out)
504 continue;
505
1e78305d
AK
506 /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
507 if (local_users_gid == (*groupsp)[in]) {
508 syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
509 user, MAGIC_LOCAL_GROUPNAME);
510 continue;
511 }
512
f6903667
AK
513 status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
514 if (status == NSS_STATUS_SUCCESS) {
1e78305d 515 (*groupsp)[out++] = (*groupsp)[in];
5879a696 516 } else if (status == NSS_STATUS_TRYAGAIN) {
f6903667
AK
517 *start = out;
518 *errnop = nonlocal_errno;
519 return status;
520 }
521 }
522
523 *start = out;
524 return NSS_STATUS_SUCCESS;
525}
This page took 0.746 seconds and 5 git commands to generate.