Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/getgrouplist.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2010, 2011, 2013-2021
5
* Todd C. Miller <[email protected]>
6
*
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
10
*
11
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
*/
19
20
#include <config.h>
21
22
#include <stdlib.h>
23
#include <string.h>
24
#include <grp.h>
25
#include <limits.h>
26
#include <unistd.h>
27
#ifdef HAVE_NSS_SEARCH
28
# include <errno.h>
29
# include <limits.h>
30
# include <nsswitch.h>
31
# ifdef HAVE_NSS_DBDEFS_H
32
# include <nss_dbdefs.h>
33
# else
34
# include <compat/nss_dbdefs.h>
35
# endif
36
#endif
37
38
#include <sudo_compat.h>
39
#include <sudo_debug.h>
40
#include <sudo_util.h>
41
42
#ifndef HAVE_GETGROUPLIST
43
int
44
sudo_getgrouplist(const char *name, GETGROUPS_T basegid, GETGROUPS_T *groups,
45
int *ngroupsp)
46
{
47
return sudo_getgrouplist2(name, basegid, &groups, ngroupsp);
48
}
49
#endif /* HAVE_GETGROUPLIST */
50
51
#if defined(HAVE_GETGROUPLIST)
52
53
#if defined(HAVE_GETGROUPLIST_2) && !HAVE_DECL_GETGROUPLIST_2
54
int getgrouplist_2(const char *name, GETGROUPS_T basegid, GETGROUPS_T **groups);
55
#endif /* HAVE_GETGROUPLIST_2 && !HAVE_DECL_GETGROUPLIST_2 */
56
57
/*
58
* Extended getgrouplist(3) using getgrouplist(3) and getgrouplist_2(3)
59
*/
60
int
61
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
62
GETGROUPS_T **groupsp, int *ngroupsp)
63
{
64
#ifdef __APPLE__
65
int *groups = (int *)*groupsp;
66
#else
67
GETGROUPS_T *groups = *groupsp;
68
#endif
69
int ngroups;
70
#ifndef HAVE_GETGROUPLIST_2
71
long grpsize;
72
int tries;
73
#endif
74
debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
75
76
/* For static group vector, just use getgrouplist(3). */
77
if (groups != NULL)
78
debug_return_int(getgrouplist(name, basegid, groups, ngroupsp));
79
80
#ifdef HAVE_GETGROUPLIST_2
81
if ((ngroups = getgrouplist_2(name, basegid, groupsp)) == -1)
82
debug_return_int(-1);
83
*ngroupsp = ngroups;
84
debug_return_int(0);
85
#else
86
grpsize = sysconf(_SC_NGROUPS_MAX);
87
if (grpsize < 0 || grpsize > INT_MAX)
88
grpsize = NGROUPS_MAX;
89
grpsize++; /* include space for the primary gid */
90
/*
91
* It is possible to belong to more groups in the group database
92
* than NGROUPS_MAX.
93
*/
94
for (tries = 0; tries < 10; tries++) {
95
free(groups);
96
groups = reallocarray(NULL, (size_t)grpsize, sizeof(*groups));
97
if (groups == NULL)
98
debug_return_int(-1);
99
ngroups = (int)grpsize;
100
if (getgrouplist(name, basegid, groups, &ngroups) != -1) {
101
*groupsp = groups;
102
*ngroupsp = ngroups;
103
debug_return_int(0);
104
}
105
if (ngroups == grpsize) {
106
/* Failed for some reason other than ngroups too small. */
107
break;
108
}
109
/* getgrouplist(3) set ngroups to the required length, use it. */
110
grpsize = ngroups;
111
}
112
free(groups);
113
debug_return_int(-1);
114
#endif /* HAVE_GETGROUPLIST_2 */
115
}
116
117
#elif defined(HAVE_GETGRSET)
118
119
/*
120
* Extended getgrouplist(3) using AIX getgrset(3)
121
*/
122
int
123
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
124
GETGROUPS_T **groupsp, int *ngroupsp)
125
{
126
GETGROUPS_T *groups = *groupsp;
127
char *cp, *last, *grset = NULL;
128
const char *errstr;
129
int ngroups = 1;
130
int grpsize = *ngroupsp;
131
int ret = -1;
132
gid_t gid;
133
debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
134
135
#ifdef HAVE_SETAUTHDB
136
aix_setauthdb((char *) name, NULL);
137
#endif
138
if ((grset = getgrset(name)) == NULL)
139
goto done;
140
141
if (groups == NULL) {
142
/* Dynamically-sized group vector, count groups and alloc. */
143
grpsize = 1; /* reserve one for basegid */
144
if (*grset != '\0') {
145
grpsize++; /* at least one supplementary group */
146
for (cp = grset; *cp != '\0'; cp++) {
147
if (*cp == ',')
148
grpsize++;
149
}
150
}
151
groups = reallocarray(NULL, grpsize, sizeof(*groups));
152
if (groups == NULL)
153
debug_return_int(-1);
154
} else {
155
/* Static group vector. */
156
if (grpsize < 1)
157
debug_return_int(-1);
158
}
159
160
/* We support BSD semantics where the first element is the base gid */
161
groups[0] = basegid;
162
163
for (cp = strtok_r(grset, ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {
164
gid = sudo_strtoid(cp, &errstr);
165
if (errstr == NULL && gid != basegid) {
166
if (ngroups == grpsize)
167
goto done;
168
groups[ngroups++] = gid;
169
}
170
}
171
ret = 0;
172
173
done:
174
free(grset);
175
#ifdef HAVE_SETAUTHDB
176
aix_restoreauthdb();
177
#endif
178
*groupsp = groups;
179
*ngroupsp = ngroups;
180
181
debug_return_int(ret);
182
}
183
184
#elif defined(HAVE_NSS_SEARCH)
185
186
#ifndef ALIGNBYTES
187
# define ALIGNBYTES (sizeof(long) - 1L)
188
#endif
189
#ifndef ALIGN
190
# define ALIGN(p) (((unsigned long)(p) + ALIGNBYTES) & ~ALIGNBYTES)
191
#endif
192
193
#if defined(HAVE__NSS_INITF_GROUP) || defined(HAVE___NSS_INITF_GROUP)
194
extern void _nss_initf_group(nss_db_params_t *params);
195
#else
196
static void
197
_nss_initf_group(nss_db_params_t *params)
198
{
199
params->name = NSS_DBNAM_GROUP;
200
params->default_config = NSS_DEFCONF_GROUP;
201
}
202
#endif
203
204
/*
205
* Convert a groups file string (instr) to a struct group (ent) using
206
* buf for storage.
207
*/
208
static int
209
str2grp(const char *instr, int inlen, void *ent, char *buf, int buflen)
210
{
211
struct group *grp = ent;
212
char *cp, *fieldsep = buf;
213
char **gr_mem, **gr_end;
214
const char *errstr;
215
int yp = 0;
216
id_t id;
217
debug_decl(str2grp, SUDO_DEBUG_UTIL);
218
219
/* Must at least have space to copy instr -> buf. */
220
if (inlen >= buflen)
221
debug_return_int(NSS_STR_PARSE_ERANGE);
222
223
/* Paranoia: buf and instr should be distinct. */
224
if (buf != instr) {
225
memmove(buf, instr, inlen);
226
buf[inlen] = '\0';
227
}
228
229
if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
230
debug_return_int(NSS_STR_PARSE_PARSE);
231
*fieldsep++ = '\0';
232
grp->gr_name = cp;
233
234
/* Check for YP inclusion/exclusion entries. */
235
if (*cp == '+' || *cp == '-') {
236
/* Only the name is required for YP inclusion/exclusion entries. */
237
grp->gr_passwd = (char *)"";
238
grp->gr_gid = 0;
239
grp->gr_mem = NULL;
240
yp = 1;
241
}
242
243
if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
244
debug_return_int(yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE);
245
*fieldsep++ = '\0';
246
grp->gr_passwd = cp;
247
248
if ((fieldsep = strchr(cp = fieldsep, ':')) == NULL)
249
debug_return_int(yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE);
250
*fieldsep++ = '\0';
251
id = sudo_strtoid(cp, &errstr);
252
if (errstr != NULL) {
253
/*
254
* A range error is always a fatal error, but ignore garbage
255
* at the end of YP entries since it has no meaning.
256
*/
257
if (errno == ERANGE)
258
debug_return_int(NSS_STR_PARSE_ERANGE);
259
debug_return_int(yp ? NSS_STR_PARSE_SUCCESS : NSS_STR_PARSE_PARSE);
260
}
261
#ifdef GID_NOBODY
262
/* Negative gids get mapped to nobody on Solaris. */
263
if (*cp == '-' && id != 0)
264
grp->gr_gid = GID_NOBODY;
265
else
266
#endif
267
grp->gr_gid = (gid_t)id;
268
269
/* Store group members, taking care to use proper alignment. */
270
grp->gr_mem = NULL;
271
if (*fieldsep != '\0') {
272
grp->gr_mem = gr_mem = (char **)ALIGN(buf + inlen + 1);
273
gr_end = (char **)((unsigned long)(buf + buflen) & ~ALIGNBYTES) - 1;
274
for (;;) {
275
if (gr_mem >= gr_end)
276
debug_return_int(NSS_STR_PARSE_ERANGE); /* out of space! */
277
*gr_mem++ = cp;
278
if (fieldsep == NULL)
279
break;
280
if ((fieldsep = strchr(cp = fieldsep, ',')) != NULL)
281
*fieldsep++ = '\0';
282
}
283
*gr_mem = NULL;
284
}
285
debug_return_int(NSS_STR_PARSE_SUCCESS);
286
}
287
288
static nss_status_t
289
process_cstr(const char *instr, int inlen, struct nss_groupsbymem *gbm,
290
int dynamic)
291
{
292
const char *user = gbm->username;
293
nss_status_t ret = NSS_NOTFOUND;
294
nss_XbyY_buf_t *buf;
295
struct group *grp;
296
char **gr_mem;
297
int error, i;
298
debug_decl(process_cstr, SUDO_DEBUG_UTIL);
299
300
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: parsing %.*s", __func__,
301
inlen, instr);
302
303
/* Hack to let us check whether the query was handled by nscd or us. */
304
if (gbm->force_slow_way != 0)
305
gbm->force_slow_way = 2;
306
307
buf = _nss_XbyY_buf_alloc(sizeof(struct group), NSS_BUFLEN_GROUP);
308
if (buf == NULL)
309
debug_return_int(NSS_UNAVAIL);
310
311
/* Parse groups file string -> struct group. */
312
grp = buf->result;
313
error = (*gbm->str2ent)(instr, inlen, grp, buf->buffer, buf->buflen);
314
if (error != NSS_STR_PARSE_SUCCESS || grp->gr_mem == NULL)
315
goto done;
316
317
for (gr_mem = grp->gr_mem; *gr_mem != NULL; gr_mem++) {
318
if (strcmp(*gr_mem, user) == 0) {
319
const int numgids = MIN(gbm->numgids, gbm->maxgids);
320
321
/* Append to gid_array unless gr_gid is a dupe. */
322
for (i = 0; i < numgids; i++) {
323
if (gbm->gid_array[i] == grp->gr_gid)
324
goto done; /* already present */
325
}
326
if (i == gbm->maxgids && dynamic) {
327
GETGROUPS_T *tmp = reallocarray(gbm->gid_array, gbm->maxgids,
328
2 * sizeof(GETGROUPS_T));
329
if (tmp == NULL) {
330
/* Out of memory, just return what we have. */
331
dynamic = 0;
332
} else {
333
gbm->gid_array = tmp;
334
gbm->maxgids <<= 1;
335
}
336
}
337
/* Store gid if there is space. */
338
if (i < gbm->maxgids)
339
gbm->gid_array[i] = grp->gr_gid;
340
/* Always increment numgids so we can detect when out of space. */
341
gbm->numgids++;
342
goto done;
343
}
344
}
345
done:
346
_nss_XbyY_buf_free(buf);
347
debug_return_int(ret);
348
}
349
350
static nss_status_t
351
process_cstr_static(const char *instr, int inlen, struct nss_groupsbymem *gbm)
352
{
353
return process_cstr(instr, inlen, gbm, 0);
354
}
355
356
static nss_status_t
357
process_cstr_dynamic(const char *instr, int inlen, struct nss_groupsbymem *gbm)
358
{
359
return process_cstr(instr, inlen, gbm, 1);
360
}
361
362
/*
363
* Extended getgrouplist(3) using nss_search(3)
364
*/
365
int
366
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
367
GETGROUPS_T **groupsp, int *ngroupsp)
368
{
369
struct nss_groupsbymem gbm;
370
static DEFINE_NSS_DB_ROOT(db_root);
371
debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
372
373
memset(&gbm, 0, sizeof(gbm));
374
gbm.username = name;
375
gbm.gid_array = *groupsp;
376
gbm.maxgids = *ngroupsp;
377
gbm.numgids = 1; /* for basegid */
378
gbm.force_slow_way = 1;
379
gbm.str2ent = str2grp;
380
381
if (gbm.gid_array == NULL) {
382
/* Dynamically-sized group vector. */
383
gbm.maxgids = (int)sysconf(_SC_NGROUPS_MAX);
384
if (gbm.maxgids < 0)
385
gbm.maxgids = NGROUPS_MAX;
386
gbm.gid_array = reallocarray(NULL, gbm.maxgids, 4 * sizeof(GETGROUPS_T));
387
if (gbm.gid_array == NULL)
388
debug_return_int(-1);
389
gbm.maxgids <<= 2;
390
gbm.process_cstr = process_cstr_dynamic;
391
} else {
392
/* Static group vector. */
393
if (gbm.maxgids <= 0)
394
debug_return_int(-1);
395
gbm.process_cstr = process_cstr_static;
396
}
397
398
/* We support BSD semantics where the first element is the base gid */
399
gbm.gid_array[0] = basegid;
400
401
/*
402
* Can't use nss_search return value since it may return NSS_UNAVAIL
403
* when no nsswitch.conf entry (e.g. compat mode).
404
*/
405
for (;;) {
406
GETGROUPS_T *tmp;
407
408
(void)nss_search(&db_root, _nss_initf_group, NSS_DBOP_GROUP_BYMEMBER,
409
&gbm);
410
411
/*
412
* If this was a statically-sized group vector or nscd was not used
413
* we are done.
414
*/
415
if (gbm.process_cstr != process_cstr_dynamic || gbm.force_slow_way == 2)
416
break;
417
418
/*
419
* If gid_array is full and the query was handled by nscd, there
420
* may be more data, so double gid_array and try again.
421
*/
422
if (gbm.numgids != gbm.maxgids)
423
break;
424
425
tmp = reallocarray(gbm.gid_array, gbm.maxgids, 2 * sizeof(GETGROUPS_T));
426
if (tmp == NULL) {
427
free(gbm.gid_array);
428
debug_return_int(-1);
429
}
430
gbm.gid_array = tmp;
431
gbm.maxgids <<= 1;
432
}
433
434
/* Note: we can only detect a too-small group list if nscd is not used. */
435
*groupsp = gbm.gid_array;
436
if (gbm.numgids <= gbm.maxgids) {
437
*ngroupsp = gbm.numgids;
438
debug_return_int(0);
439
}
440
*ngroupsp = gbm.maxgids;
441
debug_return_int(-1);
442
}
443
444
#else /* !HAVE_GETGROUPLIST && !HAVE_GETGRSET && !HAVE__GETGROUPSBYMEMBER */
445
446
/*
447
* Extended getgrouplist(3) using getgrent(3)
448
*/
449
int
450
sudo_getgrouplist2_v1(const char *name, GETGROUPS_T basegid,
451
GETGROUPS_T **groupsp, int *ngroupsp)
452
{
453
GETGROUPS_T *groups = *groupsp;
454
int grpsize = *ngroupsp;
455
int i, ngroups = 1;
456
int ret = -1;
457
struct group *grp;
458
debug_decl(sudo_getgrouplist2, SUDO_DEBUG_UTIL);
459
460
if (groups == NULL) {
461
/* Dynamically-sized group vector. */
462
grpsize = (int)sysconf(_SC_NGROUPS_MAX);
463
if (grpsize < 0)
464
grpsize = NGROUPS_MAX;
465
groups = reallocarray(NULL, grpsize, 4 * sizeof(*groups));
466
if (groups == NULL)
467
debug_return_int(-1);
468
grpsize <<= 2;
469
} else {
470
/* Static group vector. */
471
if (grpsize < 1)
472
debug_return_int(-1);
473
}
474
475
/* We support BSD semantics where the first element is the base gid */
476
groups[0] = basegid;
477
478
setgrent();
479
while ((grp = getgrent()) != NULL) {
480
if (grp->gr_gid == basegid || grp->gr_mem == NULL)
481
continue;
482
483
for (i = 0; grp->gr_mem[i] != NULL; i++) {
484
if (strcmp(name, grp->gr_mem[i]) == 0)
485
break;
486
}
487
if (grp->gr_mem[i] == NULL)
488
continue; /* user not found */
489
490
/* Only add if it is not the same as an existing gid */
491
for (i = 0; i < ngroups; i++) {
492
if (grp->gr_gid == groups[i])
493
break;
494
}
495
if (i == ngroups) {
496
if (ngroups == grpsize) {
497
GETGROUPS_T *tmp;
498
499
if (*groupsp != NULL) {
500
/* Static group vector. */
501
goto done;
502
}
503
tmp = reallocarray(groups, grpsize, 2 * sizeof(*groups));
504
if (tmp == NULL) {
505
free(groups);
506
groups = NULL;
507
ngroups = 0;
508
goto done;
509
}
510
groups = tmp;
511
grpsize <<= 1;
512
}
513
groups[ngroups++] = grp->gr_gid;
514
}
515
}
516
ret = 0;
517
518
done:
519
endgrent();
520
*groupsp = groups;
521
*ngroupsp = ngroups;
522
523
debug_return_int(ret);
524
}
525
#endif /* !HAVE_GETGROUPLIST && !HAVE_GETGRSET && !HAVE__GETGROUPSBYMEMBER */
526
527