]>
Commit | Line | Data |
---|---|---|
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 | |
27 | #include <sys/types.h> | |
28 | #include <unistd.h> | |
29 | #include <stdlib.h> | |
30 | #include <stdint.h> | |
31 | #include <string.h> | |
32 | #include <dlfcn.h> | |
33 | #include <stdio.h> | |
34 | #include <syslog.h> | |
35 | #include <errno.h> | |
775f7dc3 | 36 | #include <pwd.h> |
f6903667 AK |
37 | #include <grp.h> |
38 | #include <nss.h> | |
39 | #include "nsswitch-internal.h" | |
40 | #include "nonlocal.h" | |
41 | ||
6ca16423 AK |
42 | /* |
43 | * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal | |
34cfeb28 AK |
44 | * users will be automatically added to it. Furthermore, if a local |
45 | * user is added to this group, then that user will inherit any | |
775f7dc3 AK |
46 | * nonlocal gids from a nonlocal user of the same name, as |
47 | * supplementary gids. | |
6ca16423 | 48 | */ |
48479fc7 | 49 | #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users" |
6ca16423 AK |
50 | |
51 | /* | |
52 | * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users | |
53 | * will be automatically added to it. | |
54 | */ | |
48479fc7 | 55 | #define MAGIC_LOCAL_GROUPNAME "nss-local-users" |
f7293a54 | 56 | |
f6903667 | 57 | |
c1812233 AK |
58 | enum nss_status |
59 | _nss_nonlocal_getgrnam_r(const char *name, struct group *grp, | |
60 | char *buffer, size_t buflen, int *errnop); | |
61 | ||
62 | enum nss_status | |
63 | _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp, | |
64 | char *buffer, size_t buflen, int *errnop); | |
65 | ||
66 | ||
cbb0e3ea AK |
67 | static service_user *__nss_group_nonlocal_database; |
68 | ||
69 | static int | |
70 | internal_function | |
71 | __nss_group_nonlocal_lookup(service_user **ni, const char *fct_name, | |
72 | void **fctp) | |
f6903667 | 73 | { |
cbb0e3ea AK |
74 | if (__nss_group_nonlocal_database == NULL |
75 | && __nss_database_lookup("group_nonlocal", NULL, NULL, | |
76 | &__nss_group_nonlocal_database) < 0) | |
77 | return -1; | |
78 | ||
79 | *ni = __nss_group_nonlocal_database; | |
48479fc7 | 80 | |
cbb0e3ea AK |
81 | *fctp = __nss_lookup_function(*ni, fct_name); |
82 | return 0; | |
f6903667 AK |
83 | } |
84 | ||
85 | ||
f6903667 AK |
86 | enum nss_status |
87 | check_nonlocal_gid(const char *user, gid_t gid, int *errnop) | |
88 | { | |
30fb3957 | 89 | enum nss_status status; |
f7293a54 | 90 | struct group gbuf; |
cbb0e3ea | 91 | char *buf; |
8870ee9c | 92 | size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX); |
cbb0e3ea AK |
93 | const struct walk_nss w = { |
94 | .lookup = &__nss_group_lookup, .fct_name = "getgrgid_r", | |
95 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen | |
96 | }; | |
97 | const __typeof__(&_nss_nonlocal_getgrgid_r) self = &_nss_nonlocal_getgrgid_r; | |
98 | #define args (gid, &gbuf, buf, buflen, errnop) | |
99 | #include "walk_nss.h" | |
100 | #undef args | |
30fb3957 AK |
101 | |
102 | if (status == NSS_STATUS_SUCCESS) { | |
21b6ddd9 | 103 | syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user); |
cbb0e3ea | 104 | free(buf); |
f6903667 | 105 | status = NSS_STATUS_NOTFOUND; |
30fb3957 AK |
106 | } else if (status != NSS_STATUS_TRYAGAIN) { |
107 | status = NSS_STATUS_SUCCESS; | |
f6903667 | 108 | } |
30fb3957 | 109 | |
f6903667 AK |
110 | return status; |
111 | } | |
112 | ||
cf338316 AK |
113 | enum nss_status |
114 | check_nonlocal_group(const char *user, struct group *grp, int *errnop) | |
115 | { | |
a07a7616 AK |
116 | enum nss_status status = NSS_STATUS_SUCCESS; |
117 | int old_errno = errno; | |
118 | char *end; | |
119 | unsigned long gid; | |
120 | ||
121 | errno = 0; | |
122 | gid = strtoul(grp->gr_name, &end, 10); | |
f4061d47 AK |
123 | if (errno == 0 && *end == '\0' && (gid_t)gid == gid) { |
124 | errno = old_errno; | |
a07a7616 | 125 | status = check_nonlocal_gid(user, gid, errnop); |
f4061d47 AK |
126 | } else |
127 | errno = old_errno; | |
a07a7616 AK |
128 | if (status != NSS_STATUS_SUCCESS) |
129 | return status; | |
130 | ||
cf338316 AK |
131 | return check_nonlocal_gid(user, grp->gr_gid, errnop); |
132 | } | |
133 | ||
48479fc7 | 134 | enum nss_status |
8aa9014e | 135 | get_local_group(const char *name, struct group *grp, char **buffer, int *errnop) |
48479fc7 | 136 | { |
30fb3957 | 137 | enum nss_status status; |
cbb0e3ea AK |
138 | size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX); |
139 | const struct walk_nss w = { | |
140 | .lookup = &__nss_group_lookup, .fct_name = "getgrnam_r", | |
141 | .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen | |
142 | }; | |
143 | const __typeof__(&_nss_nonlocal_getgrnam_r) self = &_nss_nonlocal_getgrnam_r; | |
144 | #define args (name, grp, *buffer, buflen, errnop) | |
145 | #include "walk_nss.h" | |
146 | #undef args | |
48479fc7 TA |
147 | return status; |
148 | } | |
f6903667 | 149 | |
cbb0e3ea | 150 | static service_user *grent_startp, *grent_nip; |
f6903667 AK |
151 | static void *grent_fct_start; |
152 | static union { | |
153 | enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen, | |
154 | int *errnop); | |
155 | void *ptr; | |
156 | } grent_fct; | |
157 | static const char *grent_fct_name = "getgrent_r"; | |
158 | ||
159 | enum nss_status | |
160 | _nss_nonlocal_setgrent(int stayopen) | |
161 | { | |
f6903667 | 162 | enum nss_status status; |
cbb0e3ea AK |
163 | const struct walk_nss w = { |
164 | .lookup = &__nss_group_nonlocal_lookup, .fct_name = "setgrent", | |
165 | .status = &status | |
166 | }; | |
167 | const __typeof__(&_nss_nonlocal_setgrent) self = NULL; | |
168 | #define args (stayopen) | |
169 | #include "walk_nss.h" | |
170 | #undef args | |
f6903667 AK |
171 | if (status != NSS_STATUS_SUCCESS) |
172 | return status; | |
173 | ||
f6903667 | 174 | if (grent_fct_start == NULL) |
cbb0e3ea AK |
175 | __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name, |
176 | &grent_fct_start); | |
177 | grent_nip = grent_startp; | |
f6903667 AK |
178 | grent_fct.ptr = grent_fct_start; |
179 | return NSS_STATUS_SUCCESS; | |
180 | } | |
181 | ||
182 | enum nss_status | |
183 | _nss_nonlocal_endgrent(void) | |
184 | { | |
f6903667 | 185 | enum nss_status status; |
cbb0e3ea AK |
186 | const struct walk_nss w = { |
187 | .lookup = &__nss_group_nonlocal_lookup, .fct_name = "endgrent", | |
188 | .status = &status | |
189 | }; | |
190 | const __typeof__(&_nss_nonlocal_endgrent) self = NULL; | |
f6903667 AK |
191 | |
192 | grent_nip = NULL; | |
193 | ||
cbb0e3ea AK |
194 | #define args () |
195 | #include "walk_nss.h" | |
196 | #undef args | |
f6903667 AK |
197 | return status; |
198 | } | |
199 | ||
200 | enum nss_status | |
201 | _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen, | |
202 | int *errnop) | |
203 | { | |
204 | enum nss_status status; | |
a360549e TA |
205 | |
206 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); | |
c1812233 | 207 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
a360549e TA |
208 | return NSS_STATUS_UNAVAIL; |
209 | ||
f6903667 AK |
210 | if (grent_nip == NULL) { |
211 | status = _nss_nonlocal_setgrent(0); | |
212 | if (status != NSS_STATUS_SUCCESS) | |
213 | return status; | |
214 | } | |
215 | do { | |
216 | if (grent_fct.ptr == NULL) | |
217 | status = NSS_STATUS_UNAVAIL; | |
218 | else { | |
219 | int nonlocal_errno; | |
220 | do | |
221 | status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop)); | |
222 | while (status == NSS_STATUS_SUCCESS && | |
cf338316 | 223 | check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS); |
f6903667 AK |
224 | } |
225 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
226 | return status; | |
227 | ||
228 | if (status == NSS_STATUS_SUCCESS) | |
229 | return NSS_STATUS_SUCCESS; | |
230 | } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0); | |
231 | ||
232 | grent_nip = NULL; | |
233 | return NSS_STATUS_NOTFOUND; | |
234 | } | |
235 | ||
236 | ||
237 | enum nss_status | |
238 | _nss_nonlocal_getgrnam_r(const char *name, struct group *grp, | |
239 | char *buffer, size_t buflen, int *errnop) | |
240 | { | |
f6903667 | 241 | enum nss_status status; |
cbb0e3ea AK |
242 | const struct walk_nss w = { |
243 | .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrnam_r", | |
244 | .status = &status, .errnop = errnop | |
245 | }; | |
246 | const __typeof__(&_nss_nonlocal_getgrnam_r) self = NULL; | |
f6903667 | 247 | |
a360549e | 248 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
c1812233 | 249 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
48479fc7 TA |
250 | return NSS_STATUS_UNAVAIL; |
251 | ||
cbb0e3ea AK |
252 | #define args (name, grp, buffer, buflen, errnop) |
253 | #include "walk_nss.h" | |
254 | #undef args | |
f6903667 AK |
255 | if (status != NSS_STATUS_SUCCESS) |
256 | return status; | |
257 | ||
22562df0 AK |
258 | if (strcmp(name, grp->gr_name) != 0) { |
259 | syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name); | |
260 | return NSS_STATUS_NOTFOUND; | |
261 | } | |
262 | ||
cf338316 | 263 | return check_nonlocal_group(name, grp, errnop); |
f6903667 AK |
264 | } |
265 | ||
266 | enum nss_status | |
267 | _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp, | |
268 | char *buffer, size_t buflen, int *errnop) | |
269 | { | |
f6903667 | 270 | enum nss_status status; |
cbb0e3ea AK |
271 | const struct walk_nss w = { |
272 | .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrgid_r", | |
273 | .status = &status, .errnop = errnop | |
274 | }; | |
275 | const __typeof__(&_nss_nonlocal_getgrgid_r) self = NULL; | |
f6903667 | 276 | |
a360549e | 277 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
c1812233 | 278 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
f6903667 AK |
279 | return NSS_STATUS_UNAVAIL; |
280 | ||
cbb0e3ea AK |
281 | #define args (gid, grp, buffer, buflen, errnop) |
282 | #include "walk_nss.h" | |
283 | #undef args | |
f6903667 AK |
284 | if (status != NSS_STATUS_SUCCESS) |
285 | return status; | |
286 | ||
8027fdc4 AK |
287 | if (gid != grp->gr_gid) { |
288 | syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid); | |
289 | return NSS_STATUS_NOTFOUND; | |
290 | } | |
291 | ||
cf338316 | 292 | return check_nonlocal_group(grp->gr_name, grp, errnop); |
f6903667 AK |
293 | } |
294 | ||
dfc6e292 AK |
295 | static bool |
296 | add_group(gid_t group, long int *start, long int *size, gid_t **groupsp, | |
297 | long int limit, int *errnop, enum nss_status *status) | |
298 | { | |
299 | int i, old_errno = errno; | |
300 | for (i = 0; i < *start; ++i) | |
301 | if ((*groupsp)[i] == group) | |
302 | return true; | |
303 | if (*start + 1 > *size) { | |
304 | gid_t *newgroups; | |
305 | long int newsize = 2 * *size; | |
306 | if (limit > 0) { | |
307 | if (*size >= limit) { | |
308 | *status = NSS_STATUS_SUCCESS; | |
309 | return false; | |
310 | } | |
311 | if (newsize > limit) | |
312 | newsize = limit; | |
313 | } | |
314 | newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0])); | |
315 | errno = old_errno; | |
316 | if (newgroups == NULL) { | |
317 | *errnop = ENOMEM; | |
318 | *status = NSS_STATUS_TRYAGAIN; | |
319 | return false; | |
320 | } | |
321 | *groupsp = newgroups; | |
322 | *size = newsize; | |
323 | } | |
324 | (*groupsp)[(*start)++] = group; | |
325 | return true; | |
326 | } | |
327 | ||
f6903667 AK |
328 | enum nss_status |
329 | _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start, | |
330 | long int *size, gid_t **groupsp, long int limit, | |
331 | int *errnop) | |
332 | { | |
f6903667 | 333 | enum nss_status status; |
cbb0e3ea AK |
334 | const struct walk_nss w = { |
335 | .lookup = &__nss_group_nonlocal_lookup, .fct_name = "initgroups_dyn", | |
336 | .status = &status, .errnop = errnop | |
337 | }; | |
338 | const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL; | |
1e78305d AK |
339 | |
340 | struct group local_users_group, nonlocal_users_group; | |
b3831d60 | 341 | bool is_nonlocal = true; |
1e78305d | 342 | char *buffer; |
d7bf1d11 | 343 | int in, out, i; |
48479fc7 | 344 | |
34cfeb28 AK |
345 | /* Check that the user is a nonlocal user, or a member of the |
346 | * MAGIC_NONLOCAL_GROUPNAME group, before adding any groups. */ | |
48479fc7 | 347 | status = check_nonlocal_user(user, errnop); |
3010a54b | 348 | if (status == NSS_STATUS_TRYAGAIN) { |
48479fc7 | 349 | return status; |
3010a54b | 350 | } else if (status != NSS_STATUS_SUCCESS) { |
b3831d60 | 351 | is_nonlocal = false; |
48479fc7 | 352 | |
3010a54b AK |
353 | status = get_local_group(MAGIC_LOCAL_GROUPNAME, |
354 | &local_users_group, &buffer, errnop); | |
355 | if (status == NSS_STATUS_SUCCESS) { | |
356 | free(buffer); | |
357 | if (!add_group(local_users_group.gr_gid, start, size, groupsp, | |
358 | limit, errnop, &status)) | |
359 | return status; | |
360 | } else if (status == NSS_STATUS_TRYAGAIN) { | |
dfc6e292 | 361 | return status; |
3010a54b AK |
362 | } else { |
363 | syslog(LOG_WARNING, | |
364 | "nss_nonlocal: Group %s does not exist locally!", | |
365 | MAGIC_LOCAL_GROUPNAME); | |
366 | } | |
a4e1e153 AK |
367 | } |
368 | ||
369 | status = get_local_group(MAGIC_NONLOCAL_GROUPNAME, | |
370 | &nonlocal_users_group, &buffer, errnop); | |
371 | if (status == NSS_STATUS_SUCCESS) { | |
372 | free(buffer); | |
373 | if (is_nonlocal) { | |
dfc6e292 AK |
374 | if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp, |
375 | limit, errnop, &status)) | |
376 | return status; | |
34cfeb28 AK |
377 | } else { |
378 | int i; | |
379 | for (i = 0; i < *start; ++i) { | |
380 | if ((*groupsp)[i] == nonlocal_users_group.gr_gid) { | |
381 | is_nonlocal = true; | |
382 | break; | |
383 | } | |
384 | } | |
775f7dc3 AK |
385 | |
386 | if (is_nonlocal) { | |
387 | struct passwd pwbuf; | |
388 | char *buf; | |
389 | int nonlocal_errno = *errnop; | |
390 | status = get_nonlocal_passwd(user, &pwbuf, &buf, errnop); | |
391 | ||
392 | if (status == NSS_STATUS_SUCCESS) { | |
393 | nonlocal_errno = *errnop; | |
394 | status = check_nonlocal_gid(user, pwbuf.pw_gid, | |
395 | &nonlocal_errno); | |
396 | free(buf); | |
397 | } | |
398 | ||
399 | if (status == NSS_STATUS_SUCCESS) { | |
400 | if (!add_group(pwbuf.pw_gid, start, size, groupsp, limit, | |
401 | errnop, &status)) | |
402 | return status; | |
403 | } else if (status == NSS_STATUS_TRYAGAIN) { | |
404 | *errnop = nonlocal_errno; | |
405 | return status; | |
406 | } | |
407 | } | |
1e78305d | 408 | } |
a4e1e153 AK |
409 | } else if (status == NSS_STATUS_TRYAGAIN) { |
410 | if (is_nonlocal) | |
411 | return status; | |
412 | } else { | |
413 | syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!", | |
414 | MAGIC_NONLOCAL_GROUPNAME); | |
48479fc7 | 415 | } |
f6903667 | 416 | |
b3831d60 | 417 | if (!is_nonlocal) |
1e78305d AK |
418 | return NSS_STATUS_SUCCESS; |
419 | ||
d7bf1d11 | 420 | in = out = *start; |
1e78305d | 421 | |
cbb0e3ea AK |
422 | #define args (user, group, start, size, groupsp, limit, errnop) |
423 | #include "walk_nss.h" | |
424 | #undef args | |
f6903667 AK |
425 | if (status != NSS_STATUS_SUCCESS) |
426 | return status; | |
427 | ||
428 | for (; in < *start; ++in) { | |
429 | int nonlocal_errno = *errnop; | |
430 | ||
431 | for (i = 0; i < out; ++i) | |
432 | if ((*groupsp)[i] == (*groupsp)[in]) | |
433 | break; | |
434 | if (i < out) | |
435 | continue; | |
436 | ||
437 | status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno); | |
438 | if (status == NSS_STATUS_SUCCESS) { | |
1e78305d | 439 | (*groupsp)[out++] = (*groupsp)[in]; |
5879a696 | 440 | } else if (status == NSS_STATUS_TRYAGAIN) { |
f6903667 AK |
441 | *start = out; |
442 | *errnop = nonlocal_errno; | |
443 | return status; | |
444 | } | |
445 | } | |
446 | ||
447 | *start = out; | |
448 | return NSS_STATUS_SUCCESS; | |
449 | } |