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