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