]> andersk Git - nss_nonlocal.git/blame_incremental - nonlocal-group.c
Add supplementary nonlocal gids 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 <grp.h>
37#include <nss.h>
38#include "nsswitch-internal.h"
39#include "nonlocal.h"
40
41/*
42 * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal
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.
46 */
47#define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
48
49/*
50 * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users
51 * will be automatically added to it.
52 */
53#define MAGIC_LOCAL_GROUPNAME "nss-local-users"
54
55
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
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)
71{
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;
78
79 *fctp = __nss_lookup_function(*ni, fct_name);
80 return 0;
81}
82
83
84enum nss_status
85check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
86{
87 enum nss_status status;
88 struct group gbuf;
89 char *buf;
90 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
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
99
100 if (status == NSS_STATUS_SUCCESS) {
101 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
102 free(buf);
103 status = NSS_STATUS_NOTFOUND;
104 } else if (status != NSS_STATUS_TRYAGAIN) {
105 status = NSS_STATUS_SUCCESS;
106 }
107
108 return status;
109}
110
111enum nss_status
112check_nonlocal_group(const char *user, struct group *grp, int *errnop)
113{
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);
121 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
122 errno = old_errno;
123 status = check_nonlocal_gid(user, gid, errnop);
124 } else
125 errno = old_errno;
126 if (status != NSS_STATUS_SUCCESS)
127 return status;
128
129 return check_nonlocal_gid(user, grp->gr_gid, errnop);
130}
131
132enum nss_status
133get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
134{
135 enum nss_status status;
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
145 return status;
146}
147
148static service_user *grent_startp, *grent_nip;
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{
160 enum nss_status status;
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
169 if (status != NSS_STATUS_SUCCESS)
170 return status;
171
172 if (grent_fct_start == NULL)
173 __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
174 &grent_fct_start);
175 grent_nip = grent_startp;
176 grent_fct.ptr = grent_fct_start;
177 return NSS_STATUS_SUCCESS;
178}
179
180enum nss_status
181_nss_nonlocal_endgrent(void)
182{
183 enum nss_status status;
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;
189
190 grent_nip = NULL;
191
192#define args ()
193#include "walk_nss.h"
194#undef args
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;
203
204 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
205 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
206 return NSS_STATUS_UNAVAIL;
207
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_group("(unknown)", grp, &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
235enum nss_status
236_nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
237 char *buffer, size_t buflen, int *errnop)
238{
239 enum nss_status status;
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;
245
246 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
247 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
248 return NSS_STATUS_UNAVAIL;
249
250#define args (name, grp, buffer, buflen, errnop)
251#include "walk_nss.h"
252#undef args
253 if (status != NSS_STATUS_SUCCESS)
254 return status;
255
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
261 return check_nonlocal_group(name, grp, errnop);
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{
268 enum nss_status status;
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;
274
275 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
276 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
277 return NSS_STATUS_UNAVAIL;
278
279#define args (gid, grp, buffer, buflen, errnop)
280#include "walk_nss.h"
281#undef args
282 if (status != NSS_STATUS_SUCCESS)
283 return status;
284
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
290 return check_nonlocal_group(grp->gr_name, grp, errnop);
291}
292
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
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{
331 enum nss_status status;
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;
337
338 struct group local_users_group, nonlocal_users_group;
339 bool is_nonlocal = true;
340 char *buffer;
341 int in, out, i;
342
343 /* Check that the user is a nonlocal user, or a member of the
344 * MAGIC_NONLOCAL_GROUPNAME group, before adding any groups. */
345 status = check_nonlocal_user(user, errnop);
346 if (status == NSS_STATUS_TRYAGAIN) {
347 return status;
348 } else if (status != NSS_STATUS_SUCCESS) {
349 is_nonlocal = false;
350
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) {
359 return status;
360 } else {
361 syslog(LOG_WARNING,
362 "nss_nonlocal: Group %s does not exist locally!",
363 MAGIC_LOCAL_GROUPNAME);
364 }
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) {
372 if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
373 limit, errnop, &status))
374 return status;
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 }
383 }
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);
390 }
391
392 if (!is_nonlocal)
393 return NSS_STATUS_SUCCESS;
394
395 in = out = *start;
396
397#define args (user, group, start, size, groupsp, limit, errnop)
398#include "walk_nss.h"
399#undef args
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) {
414 (*groupsp)[out++] = (*groupsp)[in];
415 } else if (status == NSS_STATUS_TRYAGAIN) {
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.031577 seconds and 5 git commands to generate.