]> andersk Git - nss_nonlocal.git/blame - nonlocal-group.c
Document nss-nonlocal-users and nss-local-users
[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
43 * users will be automatically added to it.
44 */
48479fc7 45#define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
6ca16423
AK
46
47/*
48 * If the MAGIC_LOCAL_GROUPNAME local group exists, then local users
49 * will be automatically added to it.
50 */
48479fc7 51#define MAGIC_LOCAL_GROUPNAME "nss-local-users"
f7293a54 52
f6903667 53
c1812233
AK
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
cbb0e3ea
AK
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)
f6903667 69{
cbb0e3ea
AK
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;
48479fc7 76
cbb0e3ea
AK
77 *fctp = __nss_lookup_function(*ni, fct_name);
78 return 0;
f6903667
AK
79}
80
81
f6903667
AK
82enum nss_status
83check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
84{
30fb3957 85 enum nss_status status;
f7293a54 86 struct group gbuf;
cbb0e3ea 87 char *buf;
8870ee9c 88 size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
cbb0e3ea
AK
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
30fb3957
AK
97
98 if (status == NSS_STATUS_SUCCESS) {
21b6ddd9 99 syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
cbb0e3ea 100 free(buf);
f6903667 101 status = NSS_STATUS_NOTFOUND;
30fb3957
AK
102 } else if (status != NSS_STATUS_TRYAGAIN) {
103 status = NSS_STATUS_SUCCESS;
f6903667 104 }
30fb3957 105
f6903667
AK
106 return status;
107}
108
cf338316
AK
109enum nss_status
110check_nonlocal_group(const char *user, struct group *grp, int *errnop)
111{
a07a7616
AK
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);
f4061d47
AK
119 if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
120 errno = old_errno;
a07a7616 121 status = check_nonlocal_gid(user, gid, errnop);
f4061d47
AK
122 } else
123 errno = old_errno;
a07a7616
AK
124 if (status != NSS_STATUS_SUCCESS)
125 return status;
126
cf338316
AK
127 return check_nonlocal_gid(user, grp->gr_gid, errnop);
128}
129
48479fc7 130enum nss_status
8aa9014e 131get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
48479fc7 132{
30fb3957 133 enum nss_status status;
cbb0e3ea
AK
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
48479fc7
TA
143 return status;
144}
f6903667 145
cbb0e3ea 146static service_user *grent_startp, *grent_nip;
f6903667
AK
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{
f6903667 158 enum nss_status status;
cbb0e3ea
AK
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
f6903667
AK
167 if (status != NSS_STATUS_SUCCESS)
168 return status;
169
f6903667 170 if (grent_fct_start == NULL)
cbb0e3ea
AK
171 __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
172 &grent_fct_start);
173 grent_nip = grent_startp;
f6903667
AK
174 grent_fct.ptr = grent_fct_start;
175 return NSS_STATUS_SUCCESS;
176}
177
178enum nss_status
179_nss_nonlocal_endgrent(void)
180{
f6903667 181 enum nss_status status;
cbb0e3ea
AK
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;
f6903667
AK
187
188 grent_nip = NULL;
189
cbb0e3ea
AK
190#define args ()
191#include "walk_nss.h"
192#undef args
f6903667
AK
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;
a360549e
TA
201
202 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 203 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
a360549e
TA
204 return NSS_STATUS_UNAVAIL;
205
f6903667
AK
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 &&
cf338316 219 check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
f6903667
AK
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{
f6903667 237 enum nss_status status;
cbb0e3ea
AK
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;
f6903667 243
a360549e 244 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 245 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
48479fc7
TA
246 return NSS_STATUS_UNAVAIL;
247
cbb0e3ea
AK
248#define args (name, grp, buffer, buflen, errnop)
249#include "walk_nss.h"
250#undef args
f6903667
AK
251 if (status != NSS_STATUS_SUCCESS)
252 return status;
253
22562df0
AK
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
cf338316 259 return check_nonlocal_group(name, grp, errnop);
f6903667
AK
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{
f6903667 266 enum nss_status status;
cbb0e3ea
AK
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;
f6903667 272
a360549e 273 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
c1812233 274 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
f6903667
AK
275 return NSS_STATUS_UNAVAIL;
276
cbb0e3ea
AK
277#define args (gid, grp, buffer, buflen, errnop)
278#include "walk_nss.h"
279#undef args
f6903667
AK
280 if (status != NSS_STATUS_SUCCESS)
281 return status;
282
8027fdc4
AK
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
cf338316 288 return check_nonlocal_group(grp->gr_name, grp, errnop);
f6903667
AK
289}
290
dfc6e292
AK
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
f6903667
AK
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{
f6903667 329 enum nss_status status;
cbb0e3ea
AK
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;
1e78305d
AK
335
336 struct group local_users_group, nonlocal_users_group;
b3831d60 337 bool is_nonlocal = true;
1e78305d 338 char *buffer;
d7bf1d11 339 int in, out, i;
48479fc7 340
1e78305d 341 /* Check that the user is a nonlocal user before adding any groups. */
48479fc7 342 status = check_nonlocal_user(user, errnop);
3010a54b 343 if (status == NSS_STATUS_TRYAGAIN) {
48479fc7 344 return status;
3010a54b 345 } else if (status != NSS_STATUS_SUCCESS) {
b3831d60 346 is_nonlocal = false;
48479fc7 347
3010a54b
AK
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) {
dfc6e292 356 return status;
3010a54b
AK
357 } else {
358 syslog(LOG_WARNING,
359 "nss_nonlocal: Group %s does not exist locally!",
360 MAGIC_LOCAL_GROUPNAME);
361 }
a4e1e153
AK
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) {
dfc6e292
AK
369 if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
370 limit, errnop, &status))
371 return status;
1e78305d 372 }
a4e1e153
AK
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);
48479fc7 379 }
f6903667 380
b3831d60 381 if (!is_nonlocal)
1e78305d
AK
382 return NSS_STATUS_SUCCESS;
383
d7bf1d11 384 in = out = *start;
1e78305d 385
cbb0e3ea
AK
386#define args (user, group, start, size, groupsp, limit, errnop)
387#include "walk_nss.h"
388#undef args
f6903667
AK
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) {
1e78305d 403 (*groupsp)[out++] = (*groupsp)[in];
5879a696 404 } else if (status == NSS_STATUS_TRYAGAIN) {
f6903667
AK
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.24718 seconds and 5 git commands to generate.