]>
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 | errno = 0; |
76 | ret = getgrgid_r(gid, gbufp, buf, buflen, &gbufp); | |
77 | if (ret != 0) { | |
78 | *errnop = old_errno; | |
79 | status = NSS_STATUS_TRYAGAIN; | |
80 | } else if (gbufp != NULL) { | |
81 | syslog(LOG_WARNING, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user); | |
f6903667 | 82 | status = NSS_STATUS_NOTFOUND; |
f6903667 | 83 | } |
f7293a54 AK |
84 | free(buf); |
85 | errno = old_errno; | |
f6903667 AK |
86 | return status; |
87 | } | |
88 | ||
48479fc7 TA |
89 | enum nss_status |
90 | get_local_group(const char *name, struct group *grp, char *buffer, size_t buflen, int *errnop) | |
91 | { | |
92 | enum nss_status status = NSS_STATUS_NOTFOUND; | |
93 | struct group gbuf; | |
94 | struct group *gbufp = &gbuf; | |
95 | int ret; | |
96 | int old_errno = errno; | |
97 | int len = MAGIC_LOCAL_GR_BUFLEN; | |
98 | char *buf = malloc(len); | |
99 | if (buf == NULL) { | |
100 | *errnop = ENOMEM; | |
101 | errno = old_errno; | |
102 | return NSS_STATUS_TRYAGAIN; | |
103 | } | |
104 | errno = 0; | |
105 | ret = getgrnam_r(name, gbufp, buf, len, &gbufp); | |
106 | if (ret != 0) { | |
107 | *errnop = old_errno; | |
108 | status = NSS_STATUS_TRYAGAIN; | |
109 | } else if (gbufp != NULL) { | |
110 | status = NSS_STATUS_SUCCESS; | |
111 | grp->gr_name = strncpy(buffer, gbufp->gr_name, buflen); | |
112 | buffer = buffer + strlen(grp->gr_name); | |
113 | buflen = buflen - strlen(grp->gr_name); | |
114 | grp->gr_passwd = strncpy(buffer, gbufp->gr_passwd, buflen); | |
115 | buffer = buffer + strlen(grp->gr_passwd); | |
116 | buflen = buflen - strlen(grp->gr_passwd); | |
117 | grp->gr_gid = gbufp->gr_gid; | |
118 | if (buflen < sizeof(void *)) { | |
119 | *errnop = ERANGE; | |
120 | status = NSS_STATUS_TRYAGAIN; | |
121 | } | |
122 | else { | |
123 | grp->gr_mem = memset(buffer, 0, sizeof(void *)); | |
124 | } | |
125 | } | |
126 | free(buf); | |
127 | errno = old_errno; | |
128 | return status; | |
129 | } | |
f6903667 AK |
130 | |
131 | static service_user *grent_nip = NULL; | |
132 | static void *grent_fct_start; | |
133 | static union { | |
134 | enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen, | |
135 | int *errnop); | |
136 | void *ptr; | |
137 | } grent_fct; | |
138 | static const char *grent_fct_name = "getgrent_r"; | |
139 | ||
140 | enum nss_status | |
141 | _nss_nonlocal_setgrent(int stayopen) | |
142 | { | |
143 | static const char *fct_name = "setgrent"; | |
144 | static void *fct_start = NULL; | |
145 | enum nss_status status; | |
146 | service_user *nip; | |
147 | union { | |
148 | enum nss_status (*l)(int stayopen); | |
149 | void *ptr; | |
150 | } fct; | |
151 | ||
152 | nip = nss_group_nonlocal_database(); | |
153 | if (nip == NULL) | |
154 | return NSS_STATUS_UNAVAIL; | |
155 | if (fct_start == NULL) | |
156 | fct_start = __nss_lookup_function(nip, fct_name); | |
157 | fct.ptr = fct_start; | |
158 | do { | |
159 | if (fct.ptr == NULL) | |
160 | status = NSS_STATUS_UNAVAIL; | |
161 | else | |
162 | status = DL_CALL_FCT(fct.l, (stayopen)); | |
163 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
164 | if (status != NSS_STATUS_SUCCESS) | |
165 | return status; | |
166 | ||
167 | grent_nip = nip; | |
168 | if (grent_fct_start == NULL) | |
169 | grent_fct_start = __nss_lookup_function(nip, grent_fct_name); | |
170 | grent_fct.ptr = grent_fct_start; | |
171 | return NSS_STATUS_SUCCESS; | |
172 | } | |
173 | ||
174 | enum nss_status | |
175 | _nss_nonlocal_endgrent(void) | |
176 | { | |
177 | static const char *fct_name = "endgrent"; | |
178 | static void *fct_start = NULL; | |
179 | enum nss_status status; | |
180 | service_user *nip; | |
181 | union { | |
182 | enum nss_status (*l)(void); | |
183 | void *ptr; | |
184 | } fct; | |
185 | ||
186 | grent_nip = NULL; | |
187 | ||
188 | nip = nss_group_nonlocal_database(); | |
189 | if (nip == NULL) | |
190 | return NSS_STATUS_UNAVAIL; | |
191 | if (fct_start == NULL) | |
192 | fct_start = __nss_lookup_function(nip, fct_name); | |
193 | fct.ptr = fct_start; | |
194 | do { | |
195 | if (fct.ptr == NULL) | |
196 | status = NSS_STATUS_UNAVAIL; | |
197 | else | |
198 | status = DL_CALL_FCT(fct.l, ()); | |
199 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
200 | return status; | |
201 | } | |
202 | ||
203 | enum nss_status | |
204 | _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen, | |
205 | int *errnop) | |
206 | { | |
207 | enum nss_status status; | |
208 | if (grent_nip == NULL) { | |
209 | status = _nss_nonlocal_setgrent(0); | |
210 | if (status != NSS_STATUS_SUCCESS) | |
211 | return status; | |
212 | } | |
213 | do { | |
214 | if (grent_fct.ptr == NULL) | |
215 | status = NSS_STATUS_UNAVAIL; | |
216 | else { | |
217 | int nonlocal_errno; | |
218 | do | |
219 | status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop)); | |
220 | while (status == NSS_STATUS_SUCCESS && | |
221 | check_nonlocal_gid("(unknown)", grp->gr_gid, &nonlocal_errno) != NSS_STATUS_SUCCESS); | |
222 | } | |
223 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
224 | return status; | |
225 | ||
226 | if (status == NSS_STATUS_SUCCESS) | |
227 | return NSS_STATUS_SUCCESS; | |
228 | } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0); | |
229 | ||
230 | grent_nip = NULL; | |
231 | return NSS_STATUS_NOTFOUND; | |
232 | } | |
233 | ||
234 | ||
235 | enum nss_status | |
236 | _nss_nonlocal_getgrnam_r(const char *name, struct group *grp, | |
237 | char *buffer, size_t buflen, int *errnop) | |
238 | { | |
239 | static const char *fct_name = "getgrnam_r"; | |
240 | static void *fct_start = NULL; | |
241 | enum nss_status status; | |
242 | service_user *nip; | |
243 | union { | |
244 | enum nss_status (*l)(const char *name, struct group *grp, | |
245 | char *buffer, size_t buflen, int *errnop); | |
246 | void *ptr; | |
247 | } fct; | |
248 | ||
48479fc7 TA |
249 | if (buflen == MAGIC_LOCAL_GR_BUFLEN) |
250 | return NSS_STATUS_UNAVAIL; | |
251 | ||
f6903667 AK |
252 | nip = nss_group_nonlocal_database(); |
253 | if (nip == NULL) | |
254 | return NSS_STATUS_UNAVAIL; | |
255 | if (fct_start == NULL) | |
256 | fct_start = __nss_lookup_function(nip, fct_name); | |
257 | fct.ptr = fct_start; | |
258 | do { | |
259 | if (fct.ptr == NULL) | |
260 | status = NSS_STATUS_UNAVAIL; | |
261 | else | |
262 | status = DL_CALL_FCT(fct.l, (name, grp, buffer, buflen, errnop)); | |
263 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
264 | break; | |
265 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
266 | if (status != NSS_STATUS_SUCCESS) | |
267 | return status; | |
268 | ||
269 | return check_nonlocal_gid(name, grp->gr_gid, errnop); | |
270 | } | |
271 | ||
272 | enum nss_status | |
273 | _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp, | |
274 | char *buffer, size_t buflen, int *errnop) | |
275 | { | |
276 | static const char *fct_name = "getgrgid_r"; | |
277 | static void *fct_start = NULL; | |
278 | enum nss_status status; | |
279 | service_user *nip; | |
280 | union { | |
281 | enum nss_status (*l)(gid_t gid, struct group *grp, | |
282 | char *buffer, size_t buflen, int *errnop); | |
283 | void *ptr; | |
284 | } fct; | |
285 | ||
f7293a54 | 286 | if (buflen == MAGIC_LOCAL_GR_BUFLEN) |
f6903667 AK |
287 | return NSS_STATUS_UNAVAIL; |
288 | ||
289 | nip = nss_group_nonlocal_database(); | |
290 | if (nip == NULL) | |
291 | return NSS_STATUS_UNAVAIL; | |
292 | if (fct_start == NULL) | |
293 | fct_start = __nss_lookup_function(nip, fct_name); | |
294 | fct.ptr = fct_start; | |
295 | do { | |
296 | if (fct.ptr == NULL) | |
297 | status = NSS_STATUS_UNAVAIL; | |
298 | else | |
299 | status = DL_CALL_FCT(fct.l, (gid, grp, buffer, buflen, errnop)); | |
300 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
301 | break; | |
302 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
303 | if (status != NSS_STATUS_SUCCESS) | |
304 | return status; | |
305 | ||
306 | return check_nonlocal_gid(grp->gr_name, grp->gr_gid, errnop); | |
307 | } | |
308 | ||
309 | enum nss_status | |
310 | _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start, | |
311 | long int *size, gid_t **groupsp, long int limit, | |
312 | int *errnop) | |
313 | { | |
314 | static const char *fct_name = "initgroups_dyn"; | |
315 | static void *fct_start = NULL; | |
316 | enum nss_status status; | |
317 | service_user *nip; | |
318 | union { | |
319 | enum nss_status (*l)(const char *user, gid_t group, long int *start, | |
320 | long int *size, gid_t **groupsp, long int limit, | |
321 | int *errnop); | |
322 | void *ptr; | |
323 | } fct; | |
324 | int in = *start, out = *start, i; | |
48479fc7 TA |
325 | gid_t local_users_gid; |
326 | int is_local = 0; | |
327 | ||
328 | // Check that the user is a nonlocal user before adding any groups | |
329 | status = check_nonlocal_user(user, errnop); | |
330 | if (status == NSS_STATUS_NOTFOUND) { | |
331 | is_local = 1; | |
332 | } | |
333 | else if (status != NSS_STATUS_SUCCESS) { | |
334 | return status; | |
335 | } | |
336 | ||
337 | int buflen = sysconf(_SC_GETGR_R_SIZE_MAX); | |
338 | char *buffer = malloc(buflen); | |
339 | if (buffer == NULL) { | |
340 | *errnop = ENOMEM; | |
341 | return NSS_STATUS_TRYAGAIN; | |
342 | } | |
343 | struct group local_users_group; | |
344 | status = get_local_group(MAGIC_LOCAL_GROUPNAME, | |
345 | &local_users_group, buffer, buflen, errnop); | |
346 | free(buffer); | |
347 | if (status == NSS_STATUS_NOTFOUND) { | |
348 | syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!", | |
349 | MAGIC_LOCAL_GROUPNAME); | |
350 | local_users_gid = 0; | |
351 | } | |
352 | else if (status == NSS_STATUS_SUCCESS) { | |
353 | if (is_local == 1) { | |
354 | // add the (local) user to the magic local users group, and finish | |
355 | (*groupsp)[out++] = local_users_group.gr_gid; | |
356 | syslog(LOG_DEBUG, "nss_nonlocal: Added %s to special group %s", | |
357 | user, MAGIC_LOCAL_GROUPNAME); | |
358 | *start = out; | |
359 | return NSS_STATUS_SUCCESS; | |
360 | } | |
361 | else { | |
362 | local_users_gid = local_users_group.gr_gid; | |
363 | } | |
364 | } | |
365 | else { | |
366 | return status; | |
367 | } | |
f6903667 AK |
368 | |
369 | nip = nss_group_nonlocal_database(); | |
370 | if (nip == NULL) | |
371 | return NSS_STATUS_UNAVAIL; | |
372 | if (fct_start == NULL) | |
373 | fct_start = __nss_lookup_function(nip, fct_name); | |
374 | fct.ptr = fct_start; | |
375 | ||
376 | do { | |
377 | if (fct.ptr == NULL) | |
378 | status = NSS_STATUS_UNAVAIL; | |
379 | else | |
380 | status = DL_CALL_FCT(fct.l, (user, group, start, size, groupsp, limit, errnop)); | |
381 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
382 | break; | |
383 | } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0); | |
384 | if (status != NSS_STATUS_SUCCESS) | |
385 | return status; | |
386 | ||
387 | for (; in < *start; ++in) { | |
388 | int nonlocal_errno = *errnop; | |
389 | ||
390 | for (i = 0; i < out; ++i) | |
391 | if ((*groupsp)[i] == (*groupsp)[in]) | |
392 | break; | |
393 | if (i < out) | |
394 | continue; | |
395 | ||
396 | status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno); | |
397 | if (status == NSS_STATUS_SUCCESS) { | |
48479fc7 TA |
398 | // Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. |
399 | if ((*groupsp)[in] == local_users_gid) { | |
400 | syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s", | |
401 | user, MAGIC_LOCAL_GROUPNAME); | |
402 | } | |
403 | else { | |
404 | (*groupsp)[out++] = (*groupsp)[in]; | |
405 | } | |
f6903667 AK |
406 | } else if (status != NSS_STATUS_NOTFOUND) { |
407 | *start = out; | |
408 | *errnop = nonlocal_errno; | |
409 | return status; | |
410 | } | |
411 | } | |
412 | ||
48479fc7 TA |
413 | // add the (nonlocal) user to the magic nonlocal users group |
414 | buflen = sysconf(_SC_GETGR_R_SIZE_MAX); | |
415 | buffer = malloc(buflen); | |
416 | if (buffer == NULL) { | |
417 | *errnop = ENOMEM; | |
418 | return NSS_STATUS_TRYAGAIN; | |
419 | } | |
420 | struct group nonlocal_users_group; | |
421 | status = get_local_group(MAGIC_NONLOCAL_GROUPNAME, | |
422 | &nonlocal_users_group, buffer, buflen, errnop); | |
423 | if (status == NSS_STATUS_NOTFOUND) { | |
424 | syslog(LOG_WARNING, "nss_nonlocal: (local) group %s does not exist!", | |
425 | MAGIC_NONLOCAL_GROUPNAME); | |
426 | } | |
427 | else if (status == NSS_STATUS_SUCCESS) { | |
428 | (*groupsp)[out++] = nonlocal_users_group.gr_gid; | |
429 | syslog(LOG_DEBUG, "nss_nonlocal: Added %s to special group %s", | |
430 | user, MAGIC_NONLOCAL_GROUPNAME); | |
431 | } | |
432 | free(buffer); | |
433 | ||
f6903667 AK |
434 | *start = out; |
435 | return NSS_STATUS_SUCCESS; | |
436 | } |