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