]>
Commit | Line | Data |
---|---|---|
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 | ||
f7293a54 | 44 | #define MAGIC_LOCAL_GR_BUFLEN (sysconf(_SC_GETGR_R_SIZE_MAX) + 7) |
48479fc7 TA |
45 | #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users" |
46 | #define MAGIC_LOCAL_GROUPNAME "nss-local-users" | |
f7293a54 | 47 | |
f6903667 AK |
48 | |
49 | static service_user * | |
50 | nss_group_nonlocal_database(void) | |
51 | { | |
52 | static service_user *nip = NULL; | |
53 | if (nip == NULL) | |
54 | __nss_database_lookup("group_nonlocal", NULL, "", &nip); | |
48479fc7 | 55 | |
f6903667 AK |
56 | return nip; |
57 | } | |
58 | ||
59 | ||
f6903667 AK |
60 | enum nss_status |
61 | check_nonlocal_gid(const char *user, gid_t gid, int *errnop) | |
62 | { | |
f7293a54 AK |
63 | enum nss_status status = NSS_STATUS_SUCCESS; |
64 | struct group gbuf; | |
65 | struct group *gbufp = &gbuf; | |
66 | int ret; | |
67 | int old_errno = errno; | |
68 | int buflen = MAGIC_LOCAL_GR_BUFLEN; | |
69 | char *buf = malloc(buflen); | |
70 | if (buf == NULL) { | |
f6903667 | 71 | *errnop = ENOMEM; |
f7293a54 | 72 | errno = old_errno; |
f6903667 AK |
73 | return NSS_STATUS_TRYAGAIN; |
74 | } | |
f7293a54 AK |
75 | ret = getgrgid_r(gid, gbufp, buf, buflen, &gbufp); |
76 | if (ret != 0) { | |
77 | *errnop = old_errno; | |
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); | |
f6903667 | 81 | status = NSS_STATUS_NOTFOUND; |
f6903667 | 82 | } |
f7293a54 AK |
83 | free(buf); |
84 | errno = old_errno; | |
f6903667 AK |
85 | return status; |
86 | } | |
87 | ||
48479fc7 TA |
88 | enum nss_status |
89 | get_local_group(const char *name, struct group *grp, char *buffer, size_t buflen, int *errnop) | |
90 | { | |
91 | enum nss_status status = NSS_STATUS_NOTFOUND; | |
92 | struct group gbuf; | |
93 | struct group *gbufp = &gbuf; | |
1e78305d | 94 | int ret, n; |
48479fc7 TA |
95 | int old_errno = errno; |
96 | int len = MAGIC_LOCAL_GR_BUFLEN; | |
97 | char *buf = malloc(len); | |
98 | if (buf == NULL) { | |
99 | *errnop = ENOMEM; | |
100 | errno = old_errno; | |
101 | return NSS_STATUS_TRYAGAIN; | |
102 | } | |
48479fc7 TA |
103 | ret = getgrnam_r(name, gbufp, buf, len, &gbufp); |
104 | if (ret != 0) { | |
105 | *errnop = old_errno; | |
106 | status = NSS_STATUS_TRYAGAIN; | |
107 | } else if (gbufp != NULL) { | |
108 | status = NSS_STATUS_SUCCESS; | |
1e78305d AK |
109 | |
110 | n = snprintf(buffer, buflen, "%s", gbufp->gr_name); | |
111 | if (n < 0 || n >= buflen) { | |
112 | *errnop = ERANGE; | |
113 | status = NSS_STATUS_TRYAGAIN; | |
114 | goto get_local_group_done; | |
115 | } | |
116 | grp->gr_name = buffer; | |
117 | buffer += n; | |
118 | buflen -= n; | |
119 | ||
120 | n = snprintf(buffer, buflen, "%s", gbufp->gr_passwd); | |
121 | if (n < 0 || n >= buflen) { | |
122 | *errnop = ERANGE; | |
123 | status = NSS_STATUS_TRYAGAIN; | |
124 | goto get_local_group_done; | |
125 | } | |
126 | grp->gr_passwd = buffer; | |
127 | buffer += n; | |
128 | buflen -= n; | |
129 | ||
48479fc7 | 130 | grp->gr_gid = gbufp->gr_gid; |
1e78305d | 131 | |
48479fc7 TA |
132 | if (buflen < sizeof(void *)) { |
133 | *errnop = ERANGE; | |
134 | status = NSS_STATUS_TRYAGAIN; | |
1e78305d | 135 | goto get_local_group_done; |
48479fc7 | 136 | } |
1e78305d AK |
137 | *(void **)buffer = NULL; |
138 | buffer += sizeof(void *); | |
139 | buflen -= sizeof(void *); | |
48479fc7 | 140 | } |
1e78305d | 141 | get_local_group_done: |
48479fc7 TA |
142 | free(buf); |
143 | errno = old_errno; | |
144 | return status; | |
145 | } | |
f6903667 AK |
146 | |
147 | static service_user *grent_nip = NULL; | |
148 | static void *grent_fct_start; | |
149 | static union { | |
150 | enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen, | |
151 | int *errnop); | |
152 | void *ptr; | |
153 | } grent_fct; | |
154 | static const char *grent_fct_name = "getgrent_r"; | |
155 | ||
156 | enum nss_status | |
157 | _nss_nonlocal_setgrent(int stayopen) | |
158 | { | |
159 | static const char *fct_name = "setgrent"; | |
160 | static void *fct_start = NULL; | |
161 | enum nss_status status; | |
162 | service_user *nip; | |
163 | union { | |
164 | enum nss_status (*l)(int stayopen); | |
165 | void *ptr; | |
166 | } fct; | |
167 | ||
168 | nip = nss_group_nonlocal_database(); | |
169 | if (nip == NULL) | |
170 | return NSS_STATUS_UNAVAIL; | |
171 | if (fct_start == NULL) | |
172 | fct_start = __nss_lookup_function(nip, fct_name); | |
173 | fct.ptr = fct_start; | |
174 | do { | |
175 | if (fct.ptr == NULL) | |
176 | status = NSS_STATUS_UNAVAIL; | |
177 | else | |
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) | |
181 | return status; | |
182 | ||
183 | grent_nip = nip; | |
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; | |
188 | } | |
189 | ||
190 | enum nss_status | |
191 | _nss_nonlocal_endgrent(void) | |
192 | { | |
193 | static const char *fct_name = "endgrent"; | |
194 | static void *fct_start = NULL; | |
195 | enum nss_status status; | |
196 | service_user *nip; | |
197 | union { | |
198 | enum nss_status (*l)(void); | |
199 | void *ptr; | |
200 | } fct; | |
201 | ||
202 | grent_nip = NULL; | |
203 | ||
204 | nip = nss_group_nonlocal_database(); | |
205 | if (nip == NULL) | |
206 | return NSS_STATUS_UNAVAIL; | |
207 | if (fct_start == NULL) | |
208 | fct_start = __nss_lookup_function(nip, fct_name); | |
209 | fct.ptr = fct_start; | |
210 | do { | |
211 | if (fct.ptr == NULL) | |
212 | status = NSS_STATUS_UNAVAIL; | |
213 | else | |
214 | status = DL_CALL_FCT(fct.l, ()); | |
215 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
216 | return status; | |
217 | } | |
218 | ||
219 | enum nss_status | |
220 | _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen, | |
221 | int *errnop) | |
222 | { | |
223 | enum nss_status status; | |
a360549e TA |
224 | |
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; | |
229 | ||
f6903667 AK |
230 | if (grent_nip == NULL) { |
231 | status = _nss_nonlocal_setgrent(0); | |
232 | if (status != NSS_STATUS_SUCCESS) | |
233 | return status; | |
234 | } | |
235 | do { | |
236 | if (grent_fct.ptr == NULL) | |
237 | status = NSS_STATUS_UNAVAIL; | |
238 | else { | |
239 | int nonlocal_errno; | |
240 | do | |
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); | |
244 | } | |
245 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
246 | return status; | |
247 | ||
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); | |
251 | ||
252 | grent_nip = NULL; | |
253 | return NSS_STATUS_NOTFOUND; | |
254 | } | |
255 | ||
256 | ||
257 | enum nss_status | |
258 | _nss_nonlocal_getgrnam_r(const char *name, struct group *grp, | |
259 | char *buffer, size_t buflen, int *errnop) | |
260 | { | |
261 | static const char *fct_name = "getgrnam_r"; | |
262 | static void *fct_start = NULL; | |
263 | enum nss_status status; | |
264 | service_user *nip; | |
265 | union { | |
266 | enum nss_status (*l)(const char *name, struct group *grp, | |
267 | char *buffer, size_t buflen, int *errnop); | |
268 | void *ptr; | |
269 | } fct; | |
270 | ||
a360549e TA |
271 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
272 | if (buflen == MAGIC_LOCAL_GR_BUFLEN || | |
273 | (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')) | |
48479fc7 TA |
274 | return NSS_STATUS_UNAVAIL; |
275 | ||
f6903667 AK |
276 | nip = nss_group_nonlocal_database(); |
277 | if (nip == NULL) | |
278 | return NSS_STATUS_UNAVAIL; | |
279 | if (fct_start == NULL) | |
280 | fct_start = __nss_lookup_function(nip, fct_name); | |
281 | fct.ptr = fct_start; | |
282 | do { | |
283 | if (fct.ptr == NULL) | |
284 | status = NSS_STATUS_UNAVAIL; | |
285 | else | |
286 | status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop)); | |
287 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
288 | break; | |
289 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
290 | if (status != NSS_STATUS_SUCCESS) | |
291 | return status; | |
292 | ||
293 | return check_nonlocal_gid(name, grp->gr_gid, errnop); | |
294 | } | |
295 | ||
296 | enum nss_status | |
297 | _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp, | |
298 | char *buffer, size_t buflen, int *errnop) | |
299 | { | |
300 | static const char *fct_name = "getgrgid_r"; | |
301 | static void *fct_start = NULL; | |
302 | enum nss_status status; | |
303 | service_user *nip; | |
304 | union { | |
305 | enum nss_status (*l)(gid_t gid, struct group *grp, | |
306 | char *buffer, size_t buflen, int *errnop); | |
307 | void *ptr; | |
308 | } fct; | |
309 | ||
a360549e TA |
310 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
311 | if (buflen == MAGIC_LOCAL_GR_BUFLEN || | |
312 | (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')) | |
f6903667 AK |
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, (gid, 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 | return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop); | |
333 | } | |
334 | ||
335 | enum nss_status | |
336 | _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start, | |
337 | long int *size, gid_t **groupsp, long int limit, | |
338 | int *errnop) | |
339 | { | |
340 | static const char *fct_name = "initgroups_dyn"; | |
341 | static void *fct_start = NULL; | |
342 | enum nss_status status; | |
343 | service_user *nip; | |
344 | union { | |
345 | enum nss_status (*l)(const char *user, gid_t group, long int *start, | |
346 | long int *size, gid_t **groupsp, long int limit, | |
347 | int *errnop); | |
348 | void *ptr; | |
349 | } fct; | |
1e78305d AK |
350 | |
351 | struct group local_users_group, nonlocal_users_group; | |
352 | gid_t local_users_gid, gid; | |
48479fc7 | 353 | int is_local = 0; |
1e78305d AK |
354 | int buflen; |
355 | char *buffer; | |
48479fc7 | 356 | |
1e78305d | 357 | /* Check that the user is a nonlocal user before adding any groups. */ |
48479fc7 | 358 | status = check_nonlocal_user(user, errnop); |
5879a696 | 359 | if (status == NSS_STATUS_TRYAGAIN) |
48479fc7 | 360 | return status; |
5879a696 AK |
361 | else if (status != NSS_STATUS_SUCCESS) |
362 | is_local = 1; | |
48479fc7 | 363 | |
1e78305d AK |
364 | int old_errno = errno; |
365 | ||
366 | buflen = sysconf(_SC_GETGR_R_SIZE_MAX); | |
367 | buffer = malloc(buflen); | |
48479fc7 TA |
368 | if (buffer == NULL) { |
369 | *errnop = ENOMEM; | |
1e78305d | 370 | errno = old_errno; |
48479fc7 TA |
371 | return NSS_STATUS_TRYAGAIN; |
372 | } | |
48479fc7 TA |
373 | status = get_local_group(MAGIC_LOCAL_GROUPNAME, |
374 | &local_users_group, buffer, buflen, errnop); | |
5879a696 AK |
375 | if (status == NSS_STATUS_SUCCESS) { |
376 | local_users_gid = local_users_group.gr_gid; | |
377 | } else if (status == NSS_STATUS_TRYAGAIN) { | |
dc94c928 | 378 | free(buffer); |
5879a696 AK |
379 | return status; |
380 | } else { | |
48479fc7 TA |
381 | syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!", |
382 | MAGIC_LOCAL_GROUPNAME); | |
1e78305d | 383 | local_users_gid = -1; |
5879a696 | 384 | } |
1e78305d AK |
385 | free(buffer); |
386 | ||
387 | if (is_local) { | |
388 | gid = local_users_gid; | |
389 | } else { | |
390 | buflen = sysconf(_SC_GETGR_R_SIZE_MAX); | |
391 | buffer = malloc(buflen); | |
392 | if (buffer == NULL) { | |
393 | *errnop = ENOMEM; | |
394 | errno = old_errno; | |
395 | return NSS_STATUS_TRYAGAIN; | |
48479fc7 | 396 | } |
1e78305d AK |
397 | status = get_local_group(MAGIC_NONLOCAL_GROUPNAME, |
398 | &nonlocal_users_group, buffer, buflen, errnop); | |
5879a696 AK |
399 | if (status == NSS_STATUS_SUCCESS) { |
400 | gid = nonlocal_users_group.gr_gid; | |
401 | } else if (status == NSS_STATUS_TRYAGAIN) { | |
dc94c928 | 402 | free(buffer); |
5879a696 AK |
403 | errno = old_errno; |
404 | return status; | |
405 | } else { | |
1e78305d AK |
406 | syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!", |
407 | MAGIC_NONLOCAL_GROUPNAME); | |
408 | gid = -1; | |
5879a696 | 409 | } |
1e78305d | 410 | free(buffer); |
48479fc7 | 411 | } |
1e78305d AK |
412 | |
413 | if (gid != -1) { | |
414 | int i; | |
415 | for (i = 0; i < *start; ++i) | |
416 | if ((*groupsp)[i] == gid) | |
417 | break; | |
418 | if (i >= *start) { | |
419 | if (*start + 1 > *size) { | |
420 | gid_t *newgroups; | |
421 | long int newsize = 2 * *size; | |
422 | if (limit > 0) { | |
423 | if (*size >= limit) | |
424 | return NSS_STATUS_SUCCESS; | |
425 | if (newsize > limit) | |
426 | newsize = limit; | |
427 | } | |
428 | newgroups = realloc(*groupsp, *size * sizeof((*groupsp)[0])); | |
429 | if (newgroups == NULL) { | |
430 | *errnop = ENOMEM; | |
431 | errno = old_errno; | |
432 | return NSS_STATUS_TRYAGAIN; | |
433 | } | |
434 | *groupsp = newgroups; | |
435 | *size = newsize; | |
436 | } | |
437 | (*groupsp)[(*start)++] = gid; | |
438 | } | |
48479fc7 | 439 | } |
f6903667 | 440 | |
1e78305d AK |
441 | errno = old_errno; |
442 | ||
443 | if (is_local) | |
444 | return NSS_STATUS_SUCCESS; | |
445 | ||
446 | int in = *start, out = *start, i; | |
447 | ||
f6903667 AK |
448 | nip = nss_group_nonlocal_database(); |
449 | if (nip == NULL) | |
450 | return NSS_STATUS_UNAVAIL; | |
451 | if (fct_start == NULL) | |
452 | fct_start = __nss_lookup_function(nip, fct_name); | |
453 | fct.ptr = fct_start; | |
454 | ||
455 | do { | |
456 | if (fct.ptr == NULL) | |
457 | status = NSS_STATUS_UNAVAIL; | |
458 | else | |
459 | status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop)); | |
460 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
461 | break; | |
462 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
463 | if (status != NSS_STATUS_SUCCESS) | |
464 | return status; | |
465 | ||
466 | for (; in < *start; ++in) { | |
467 | int nonlocal_errno = *errnop; | |
468 | ||
469 | for (i = 0; i < out; ++i) | |
470 | if ((*groupsp)[i] == (*groupsp)[in]) | |
471 | break; | |
472 | if (i < out) | |
473 | continue; | |
474 | ||
1e78305d AK |
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); | |
479 | continue; | |
480 | } | |
481 | ||
f6903667 AK |
482 | status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno); |
483 | if (status == NSS_STATUS_SUCCESS) { | |
1e78305d | 484 | (*groupsp)[out++] = (*groupsp)[in]; |
5879a696 | 485 | } else if (status == NSS_STATUS_TRYAGAIN) { |
f6903667 AK |
486 | *start = out; |
487 | *errnop = nonlocal_errno; | |
488 | return status; | |
489 | } | |
490 | } | |
491 | ||
492 | *start = out; | |
493 | return NSS_STATUS_SUCCESS; | |
494 | } |