]> andersk Git - nss_nonlocal.git/blame_incremental - nonlocal-group.c
Add the primary nonlocal gid to local users in MAGIC_NONLOCAL_GROUPNAME
[nss_nonlocal.git] / nonlocal-group.c
... / ...
CommitLineData
1/*
2 * nonlocal-group.c
3 * group database for nss_nonlocal proxy
4 *
5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> and Tim
6 * Abbott <tabbott@mit.edu>
7 *
8 * This file is part of nss_nonlocal.
9 *
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.
14 *
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
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>
36#include <pwd.h>
37#include <grp.h>
38#include <nss.h>
39#include "nsswitch-internal.h"
40#include "nonlocal.h"
41
42/*
43 * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal
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
46 * nonlocal gids from a nonlocal user of the same name, as
47 * supplementary gids.
48 */
49#define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
50
51/*
52 * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users
53 * will be automatically added to it.
54 */
55#define MAGIC_LOCAL_GROUPNAME "nss-local-users"
56
57
58enum nss_status
59_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
60 char *buffer, size_t buflen, int *errnop);
61
62enum nss_status
63_nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
64 char *buffer, size_t buflen, int *errnop);
65
66
67static service_user *__nss_group_nonlocal_database;
68
69static int
70internal_function
71__nss_group_nonlocal_lookup(service_user **ni, const char *fct_name,
72 void **fctp)
73{
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;
80
81 *fctp = __nss_lookup_function(*ni, fct_name);
82 return 0;
83}
84
85
86enum nss_status
87check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
88{
89 enum nss_status status;
90 struct group gbuf;
91 char *buf;
92 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
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
101
102 if (status == NSS_STATUS_SUCCESS) {
103 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
104 free(buf);
105 status = NSS_STATUS_NOTFOUND;
106 } else if (status != NSS_STATUS_TRYAGAIN) {
107 status = NSS_STATUS_SUCCESS;
108 }
109
110 return status;
111}
112
113enum nss_status
114check_nonlocal_group(const char *user, struct group *grp, int *errnop)
115{
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);
123 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
124 errno = old_errno;
125 status = check_nonlocal_gid(user, gid, errnop);
126 } else
127 errno = old_errno;
128 if (status != NSS_STATUS_SUCCESS)
129 return status;
130
131 return check_nonlocal_gid(user, grp->gr_gid, errnop);
132}
133
134enum nss_status
135get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
136{
137 enum nss_status status;
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
147 return status;
148}
149
150static service_user *grent_startp, *grent_nip;
151static void *grent_fct_start;
152static union {
153 enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
154 int *errnop);
155 void *ptr;
156} grent_fct;
157static const char *grent_fct_name = "getgrent_r";
158
159enum nss_status
160_nss_nonlocal_setgrent(int stayopen)
161{
162 enum nss_status status;
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
171 if (status != NSS_STATUS_SUCCESS)
172 return status;
173
174 if (grent_fct_start == NULL)
175 __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
176 &grent_fct_start);
177 grent_nip = grent_startp;
178 grent_fct.ptr = grent_fct_start;
179 return NSS_STATUS_SUCCESS;
180}
181
182enum nss_status
183_nss_nonlocal_endgrent(void)
184{
185 enum nss_status status;
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;
191
192 grent_nip = NULL;
193
194#define args ()
195#include "walk_nss.h"
196#undef args
197 return status;
198}
199
200enum nss_status
201_nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
202 int *errnop)
203{
204 enum nss_status status;
205
206 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
207 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
208 return NSS_STATUS_UNAVAIL;
209
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 &&
223 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
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
237enum nss_status
238_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
239 char *buffer, size_t buflen, int *errnop)
240{
241 enum nss_status status;
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;
247
248 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
249 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
250 return NSS_STATUS_UNAVAIL;
251
252#define args (name, grp, buffer, buflen, errnop)
253#include "walk_nss.h"
254#undef args
255 if (status != NSS_STATUS_SUCCESS)
256 return status;
257
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
263 return check_nonlocal_group(name, grp, errnop);
264}
265
266enum nss_status
267_nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
268 char *buffer, size_t buflen, int *errnop)
269{
270 enum nss_status status;
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;
276
277 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
278 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
279 return NSS_STATUS_UNAVAIL;
280
281#define args (gid, grp, buffer, buflen, errnop)
282#include "walk_nss.h"
283#undef args
284 if (status != NSS_STATUS_SUCCESS)
285 return status;
286
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
292 return check_nonlocal_group(grp->gr_name, grp, errnop);
293}
294
295static bool
296add_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
328enum 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{
333 enum nss_status status;
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;
339
340 struct group local_users_group, nonlocal_users_group;
341 bool is_nonlocal = true;
342 char *buffer;
343 int in, out, i;
344
345 /* Check that the user is a nonlocal user, or a member of the
346 * MAGIC_NONLOCAL_GROUPNAME group, before adding any groups. */
347 status = check_nonlocal_user(user, errnop);
348 if (status == NSS_STATUS_TRYAGAIN) {
349 return status;
350 } else if (status != NSS_STATUS_SUCCESS) {
351 is_nonlocal = false;
352
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) {
361 return status;
362 } else {
363 syslog(LOG_WARNING,
364 "nss_nonlocal: Group %s does not exist locally!",
365 MAGIC_LOCAL_GROUPNAME);
366 }
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) {
374 if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
375 limit, errnop, &status))
376 return status;
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 }
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 }
408 }
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);
415 }
416
417 if (!is_nonlocal)
418 return NSS_STATUS_SUCCESS;
419
420 in = out = *start;
421
422#define args (user, group, start, size, groupsp, limit, errnop)
423#include "walk_nss.h"
424#undef args
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) {
439 (*groupsp)[out++] = (*groupsp)[in];
440 } else if (status == NSS_STATUS_TRYAGAIN) {
441 *start = out;
442 *errnop = nonlocal_errno;
443 return status;
444 }
445 }
446
447 *start = out;
448 return NSS_STATUS_SUCCESS;
449}
This page took 0.032812 seconds and 5 git commands to generate.