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