Coverage Report

Created: 2025-11-05 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/pwutil.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 1996, 1998-2005, 2007-2018
5
 *  Todd C. Miller <Todd.Miller@sudo.ws>
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
 * Sponsored in part by the Defense Advanced Research Projects
20
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22
 */
23
24
#include <config.h>
25
26
#include <stdarg.h>
27
#include <stddef.h>
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#ifdef HAVE_STRINGS_H
32
# include <strings.h>   /* strcasecmp */
33
#endif
34
#ifdef HAVE_SETAUTHDB
35
# include <usersec.h>
36
#endif /* HAVE_SETAUTHDB */
37
#include <errno.h>
38
#include <pwd.h>
39
#include <grp.h>
40
41
#include <sudoers.h>
42
#include <redblack.h>
43
#include <pwutil.h>
44
45
/*
46
 * The passwd and group caches.
47
 */
48
static struct rbtree *pwcache_byuid, *pwcache_byname;
49
static struct rbtree *grcache_bygid, *grcache_byname;
50
static struct rbtree *gidlist_cache, *grlist_cache;
51
52
static int  cmp_pwuid(const void *, const void *);
53
static int  cmp_pwnam(const void *, const void *);
54
static int  cmp_grgid(const void *, const void *);
55
56
static int max_groups;
57
58
/*
59
 * Default functions for building cache items.
60
 */
61
static sudo_make_pwitem_t make_pwitem = sudo_make_pwitem;
62
static sudo_make_gritem_t make_gritem = sudo_make_gritem;
63
static sudo_make_gidlist_item_t make_gidlist_item = sudo_make_gidlist_item;
64
static sudo_make_grlist_item_t make_grlist_item = sudo_make_grlist_item;
65
static sudo_valid_shell_t valid_shell = sudo_valid_shell;
66
67
616
#define cmp_grnam cmp_pwnam
68
69
/*
70
 * AIX has the concept of authentication registries (files, NIS, LDAP, etc).
71
 * This allows you to have separate ID <-> name mappings based on which
72
 * authentication registries the user was looked up in.
73
 * We store the registry as part of the key and use it when matching.
74
 */
75
#ifdef HAVE_SETAUTHDB
76
# define getauthregistry(u, r)  aix_getauthregistry((u), (r))
77
#else
78
117k
# define getauthregistry(u, r)  ((r)[0] = '\0')
79
#endif
80
81
/*
82
 * Change the default pwutil backend functions.
83
 * The default functions query the password and group databases.
84
 */
85
void
86
sudo_pwutil_set_backend(sudo_make_pwitem_t pwitem, sudo_make_gritem_t gritem,
87
    sudo_make_gidlist_item_t gidlist_item, sudo_make_grlist_item_t grlist_item,
88
    sudo_valid_shell_t check_shell)
89
0
{
90
0
    debug_decl(sudo_pwutil_set_backend, SUDOERS_DEBUG_NSS);
91
92
0
    if (pwitem != NULL)
93
0
  make_pwitem = pwitem;
94
0
    if (gritem != NULL)
95
0
  make_gritem = gritem;
96
0
    if (gidlist_item != NULL)
97
0
  make_gidlist_item = gidlist_item;
98
0
    if (grlist_item != NULL)
99
0
  make_grlist_item = grlist_item;
100
0
    if (check_shell != NULL)
101
0
  valid_shell = check_shell;
102
103
0
    debug_return;
104
0
}
105
106
/* Get the max number of user groups if set, or 0 if not set. */
107
int
108
sudo_pwutil_get_max_groups(void)
109
25.4k
{
110
25.4k
    return max_groups;
111
25.4k
}
112
113
/* Set the max number of user groups (negative values ignored). */
114
void
115
sudo_pwutil_set_max_groups(int n)
116
236
{
117
236
    max_groups = n > 0 ? n : 0;
118
236
}
119
120
/*
121
 * Compare by user-ID.
122
 * v1 is the key to find or data to insert, v2 is in-tree data.
123
 */
124
static int
125
cmp_pwuid(const void *v1, const void *v2)
126
80
{
127
80
    const struct cache_item *ci1 = (const struct cache_item *) v1;
128
80
    const struct cache_item *ci2 = (const struct cache_item *) v2;
129
80
    if (ci1->k.uid == ci2->k.uid)
130
80
  return strcmp(ci1->registry, ci2->registry);
131
0
    if (ci1->k.uid < ci2->k.uid)
132
0
  return -1;
133
0
    return 1;
134
0
}
135
136
/*
137
 * Compare by user/group name.
138
 * v1 is the key to find or data to insert, v2 is in-tree data.
139
 */
140
static int
141
cmp_pwnam(const void *v1, const void *v2)
142
130k
{
143
130k
    const struct cache_item *ci1 = (const struct cache_item *) v1;
144
130k
    const struct cache_item *ci2 = (const struct cache_item *) v2;
145
130k
    int ret = strcmp(ci1->k.name, ci2->k.name);
146
130k
    if (ret == 0)
147
43.5k
  ret = strcmp(ci1->registry, ci2->registry);
148
130k
    return ret;
149
130k
}
150
151
/*
152
 * Compare by user name, taking into account the source type.
153
 * Need to differentiate between group-IDs received from the front-end
154
 * (via getgroups()) and groups IDs queried from the group database.
155
 * v1 is the key to find or data to insert, v2 is in-tree data.
156
 */
157
static int
158
cmp_gidlist(const void *v1, const void *v2)
159
32.2k
{
160
32.2k
    const struct cache_item *ci1 = (const struct cache_item *) v1;
161
32.2k
    const struct cache_item *ci2 = (const struct cache_item *) v2;
162
32.2k
    int ret = strcmp(ci1->k.name, ci2->k.name);
163
32.2k
    if (ret == 0) {
164
7.76k
  if (ci1->type == ENTRY_TYPE_ANY || ci1->type == ci2->type)
165
7.73k
      return strcmp(ci1->registry, ci2->registry);
166
37
  if (ci1->type < ci2->type)
167
37
      return -1;
168
0
  return 1;
169
37
    }
170
24.4k
    return ret;
171
32.2k
}
172
173
void
174
sudo_pw_addref(struct passwd *pw)
175
0
{
176
0
    debug_decl(sudo_pw_addref, SUDOERS_DEBUG_NSS);
177
0
    ptr_to_item(pw)->refcnt++;
178
0
    debug_return;
179
0
}
180
181
static void
182
sudo_pw_delref_item(void *v)
183
118k
{
184
118k
    struct cache_item *item = v;
185
118k
    debug_decl(sudo_pw_delref_item, SUDOERS_DEBUG_NSS);
186
187
118k
    if (--item->refcnt == 0)
188
38.0k
  free(item);
189
190
118k
    debug_return;
191
118k
}
192
193
void
194
sudo_pw_delref(struct passwd *pw)
195
80.7k
{
196
80.7k
    debug_decl(sudo_pw_delref, SUDOERS_DEBUG_NSS);
197
80.7k
    sudo_pw_delref_item(ptr_to_item(pw));
198
80.7k
    debug_return;
199
80.7k
}
200
201
/*
202
 * Get a password entry by uid and allocate space for it.
203
 */
204
struct passwd *
205
sudo_getpwuid(uid_t uid)
206
250
{
207
250
    struct cache_item key, *item;
208
250
    struct rbnode *node;
209
250
    debug_decl(sudo_getpwuid, SUDOERS_DEBUG_NSS);
210
211
250
    if (pwcache_byuid == NULL) {
212
250
  pwcache_byuid = rbcreate(cmp_pwuid);
213
250
  if (pwcache_byuid == NULL) {
214
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
215
0
      debug_return_ptr(NULL);
216
0
  }
217
250
    }
218
219
250
    key.k.uid = uid;
220
250
    getauthregistry(IDtouser(uid), key.registry);
221
250
    if ((node = rbfind(pwcache_byuid, &key)) != NULL) {
222
0
  item = node->data;
223
0
  goto done;
224
0
    }
225
    /*
226
     * Cache passwd db entry if it exists or a negative response if not.
227
     */
228
#ifdef HAVE_SETAUTHDB
229
    aix_setauthdb(IDtouser(uid), key.registry);
230
#endif
231
250
    item = make_pwitem(uid, NULL);
232
#ifdef HAVE_SETAUTHDB
233
    aix_restoreauthdb();
234
#endif
235
250
    if (item == NULL) {
236
80
  if (errno != ENOENT || (item = calloc(1, sizeof(*item))) == NULL) {
237
0
      sudo_warn(U_("unable to cache uid %u"), (unsigned int) uid);
238
      /* cppcheck-suppress memleak */
239
0
      debug_return_ptr(NULL);
240
0
  }
241
80
  item->refcnt = 1;
242
80
  item->k.uid = uid;
243
  /* item->d.pw = NULL; */
244
80
    }
245
250
    strlcpy(item->registry, key.registry, sizeof(item->registry));
246
250
    switch (rbinsert(pwcache_byuid, item, NULL)) {
247
0
    case 1:
248
  /* should not happen */
249
0
  sudo_warnx(U_("unable to cache uid %u, already exists"),
250
0
      (unsigned int) uid);
251
0
  item->refcnt = 0;
252
0
  break;
253
0
    case -1:
254
  /* can't cache item, just return it */
255
0
  sudo_warn(U_("unable to cache uid %u"), (unsigned int) uid);
256
0
  item->refcnt = 0;
257
0
  break;
258
250
    }
259
250
done:
260
250
    if (item->refcnt != 0) {
261
250
  sudo_debug_printf(SUDO_DEBUG_DEBUG,
262
250
      "%s: uid %u [%s] -> user %s [%s] (%s)", __func__,
263
250
      (unsigned int)uid, key.registry,
264
250
      item->d.pw ? item->d.pw->pw_name : "unknown",
265
250
      item->registry, node ? "cache hit" : "cached");
266
250
    }
267
250
    if (item->d.pw != NULL)
268
170
  item->refcnt++;
269
250
    debug_return_ptr(item->d.pw);
270
250
}
271
272
/*
273
 * Get a password entry by name and allocate space for it.
274
 */
275
struct passwd *
276
sudo_getpwnam(const char *name)
277
80.5k
{
278
80.5k
    struct cache_item key, *item;
279
80.5k
    struct rbnode *node;
280
80.5k
    debug_decl(sudo_getpwnam, SUDOERS_DEBUG_NSS);
281
282
80.5k
    if (pwcache_byname == NULL) {
283
20.1k
  pwcache_byname = rbcreate(cmp_pwnam);
284
20.1k
  if (pwcache_byname == NULL) {
285
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
286
0
      debug_return_ptr(NULL);
287
0
  }
288
20.1k
    }
289
290
80.5k
    key.k.name = (char *) name;
291
80.5k
    getauthregistry((char *) name, key.registry);
292
80.5k
    if ((node = rbfind(pwcache_byname, &key)) != NULL) {
293
43.3k
  item = node->data;
294
43.3k
  goto done;
295
43.3k
    }
296
    /*
297
     * Cache passwd db entry if it exists or a negative response if not.
298
     */
299
#ifdef HAVE_SETAUTHDB
300
    aix_setauthdb((char *) name, key.registry);
301
#endif
302
37.1k
    item = make_pwitem((uid_t)-1, name);
303
#ifdef HAVE_SETAUTHDB
304
    aix_restoreauthdb();
305
#endif
306
37.1k
    if (item == NULL) {
307
247
  const size_t len = strlen(name) + 1;
308
247
  if (errno != ENOENT || (item = calloc(1, sizeof(*item) + len)) == NULL) {
309
0
      sudo_warn(U_("unable to cache user %s"), name);
310
      /* cppcheck-suppress memleak */
311
0
      debug_return_ptr(NULL);
312
0
  }
313
247
  item->refcnt = 1;
314
247
  item->k.name = (char *) item + sizeof(*item);
315
247
  memcpy(item->k.name, name, len);
316
  /* item->d.pw = NULL; */
317
247
    }
318
37.1k
    strlcpy(item->registry, key.registry, sizeof(item->registry));
319
37.1k
    switch (rbinsert(pwcache_byname, item, NULL)) {
320
0
    case 1:
321
  /* should not happen */
322
0
  sudo_warnx(U_("unable to cache user %s, already exists"), name);
323
0
  item->refcnt = 0;
324
0
  break;
325
0
    case -1:
326
  /* can't cache item, just return it */
327
0
  sudo_warn(U_("unable to cache user %s"), name);
328
0
  item->refcnt = 0;
329
0
  break;
330
37.1k
    }
331
80.5k
done:
332
80.5k
    if (item->refcnt != 0) {
333
80.5k
  sudo_debug_printf(SUDO_DEBUG_DEBUG,
334
80.5k
      "%s: user %s [%s] -> uid %d [%s] (%s)", __func__, name,
335
80.5k
      key.registry, item->d.pw ? (int)item->d.pw->pw_uid : -1,
336
80.5k
      item->registry, node ? "cache hit" : "cached");
337
80.5k
    }
338
80.5k
    if (item->d.pw != NULL)
339
80.2k
  item->refcnt++;
340
80.5k
    debug_return_ptr(item->d.pw);
341
80.5k
}
342
343
/*
344
 * Take a user, uid, gid, home and shell and return a faked up passwd struct.
345
 * If home or shell are NULL default values will be used.
346
 */
347
struct passwd *
348
sudo_mkpwent(const char *user, uid_t uid, gid_t gid, const char *home,
349
    const char *shell)
350
304
{
351
304
    struct cache_item_pw *pwitem;
352
304
    struct cache_item *item;
353
304
    struct passwd *pw;
354
304
    size_t len, name_len, home_len, shell_len;
355
304
    unsigned int i;
356
304
    debug_decl(sudo_mkpwent, SUDOERS_DEBUG_NSS);
357
358
304
    if (pwcache_byuid == NULL)
359
224
  pwcache_byuid = rbcreate(cmp_pwuid);
360
304
    if (pwcache_byname == NULL)
361
0
  pwcache_byname = rbcreate(cmp_pwnam);
362
304
    if (pwcache_byuid == NULL || pwcache_byname == NULL) {
363
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
364
0
  debug_return_ptr(NULL);
365
0
    }
366
367
    /* Optional arguments. */
368
304
    if (home == NULL)
369
304
  home = "/";
370
304
    if (shell == NULL)
371
304
  shell = _PATH_BSHELL;
372
373
304
    sudo_debug_printf(SUDO_DEBUG_DEBUG,
374
304
  "%s: creating and caching passwd struct for %s:%u:%u:%s:%s", __func__,
375
304
  user, (unsigned int)uid, (unsigned int)gid, home, shell);
376
377
304
    name_len = strlen(user);
378
304
    home_len = strlen(home);
379
304
    shell_len = strlen(shell);
380
304
    len = sizeof(*pwitem) + name_len + 1 /* pw_name */ +
381
304
  sizeof("*") /* pw_passwd */ + sizeof("") /* pw_gecos */ +
382
304
  home_len + 1 /* pw_dir */ + shell_len + 1 /* pw_shell */;
383
384
912
    for (i = 0; i < 2; i++) {
385
608
  struct rbtree *pwcache;
386
608
  struct rbnode *node;
387
388
608
  pwitem = calloc(1, len);
389
608
  if (pwitem == NULL) {
390
0
      sudo_warn(U_("unable to cache user %s"), user);
391
0
      debug_return_ptr(NULL);
392
0
  }
393
608
  pw = &pwitem->pw;
394
608
  pw->pw_uid = uid;
395
608
  pw->pw_gid = gid;
396
608
  pw->pw_name = (char *)(pwitem + 1);
397
608
  memcpy(pw->pw_name, user, name_len + 1);
398
608
  pw->pw_passwd = pw->pw_name + name_len + 1;
399
608
  memcpy(pw->pw_passwd, "*", 2);
400
608
  pw->pw_gecos = pw->pw_passwd + 2;
401
608
  pw->pw_gecos[0] = '\0';
402
608
  pw->pw_dir = pw->pw_gecos + 1;
403
608
  memcpy(pw->pw_dir, home, home_len + 1);
404
608
  pw->pw_shell = pw->pw_dir + home_len + 1;
405
608
  memcpy(pw->pw_shell, shell, shell_len + 1);
406
407
608
  item = &pwitem->cache;
408
608
  item->refcnt = 1;
409
608
  item->d.pw = pw;
410
608
  if (i == 0) {
411
      /* Store by uid. */
412
304
      item->k.uid = pw->pw_uid;
413
304
      pwcache = pwcache_byuid;
414
304
  } else {
415
      /* Store by name. */
416
304
      item->k.name = pw->pw_name;
417
304
      pwcache = pwcache_byname;
418
304
  }
419
608
  getauthregistry(NULL, item->registry);
420
608
  switch (rbinsert(pwcache, item, &node)) {
421
304
  case 1:
422
      /* Already exists. */
423
304
      item = node->data;
424
304
      if (item->d.pw == NULL) {
425
    /* Negative cache entry, replace with ours. */
426
304
    sudo_pw_delref_item(item);
427
304
    item = node->data = &pwitem->cache;
428
304
      } else {
429
    /* Good entry, discard our fake one. */
430
0
    free(pwitem);
431
0
      }
432
304
      break;
433
0
  case -1:
434
      /* can't cache item, just return it */
435
0
      sudo_warn(U_("unable to cache user %s"), user);
436
0
      item->refcnt = 0;
437
0
      break;
438
608
  }
439
608
    }
440
304
    item->refcnt++;
441
304
    debug_return_ptr(item->d.pw);
442
304
}
443
444
/*
445
 * Take a uid in string form "#123" and return a faked up passwd struct.
446
 */
447
struct passwd *
448
sudo_fakepwnam(const char *user, gid_t gid)
449
80
{
450
80
    const char *errstr;
451
80
    uid_t uid;
452
80
    debug_decl(sudo_fakepwnam, SUDOERS_DEBUG_NSS);
453
454
80
    uid = (uid_t) sudo_strtoid(user + 1, &errstr);
455
80
    if (errstr != NULL) {
456
0
  sudo_debug_printf(SUDO_DEBUG_DIAG|SUDO_DEBUG_LINENO,
457
0
      "uid %s %s", user, errstr);
458
0
  debug_return_ptr(NULL);
459
0
    }
460
80
    debug_return_ptr(sudo_mkpwent(user, uid, gid, NULL, NULL));
461
80
}
462
463
void
464
sudo_freepwcache(void)
465
25.3k
{
466
25.3k
    debug_decl(sudo_freepwcache, SUDOERS_DEBUG_NSS);
467
468
25.3k
    if (pwcache_byuid != NULL) {
469
474
  rbdestroy(pwcache_byuid, sudo_pw_delref_item);
470
474
  pwcache_byuid = NULL;
471
474
    }
472
25.3k
    if (pwcache_byname != NULL) {
473
20.1k
  rbdestroy(pwcache_byname, sudo_pw_delref_item);
474
20.1k
  pwcache_byname = NULL;
475
20.1k
    }
476
477
25.3k
    debug_return;
478
25.3k
}
479
480
/*
481
 * Compare by group-ID.
482
 * v1 is the key to find or data to insert, v2 is in-tree data.
483
 */
484
static int
485
cmp_grgid(const void *v1, const void *v2)
486
451
{
487
451
    const struct cache_item *ci1 = (const struct cache_item *) v1;
488
451
    const struct cache_item *ci2 = (const struct cache_item *) v2;
489
451
    if (ci1->k.gid == ci2->k.gid)
490
451
  return strcmp(ci1->registry, ci2->registry);
491
0
    if (ci1->k.gid < ci2->k.gid)
492
0
  return -1;
493
0
    return 1;
494
0
}
495
496
void
497
sudo_gr_addref(struct group *gr)
498
0
{
499
0
    debug_decl(sudo_gr_addref, SUDOERS_DEBUG_NSS);
500
0
    ptr_to_item(gr)->refcnt++;
501
0
    debug_return;
502
0
}
503
504
static void
505
sudo_gr_delref_item(void *v)
506
2.21k
{
507
2.21k
    struct cache_item *item = v;
508
2.21k
    debug_decl(sudo_gr_delref_item, SUDOERS_DEBUG_NSS);
509
510
2.21k
    if (--item->refcnt == 0)
511
1.61k
  free(item);
512
513
2.21k
    debug_return;
514
2.21k
}
515
516
void
517
sudo_gr_delref(struct group *gr)
518
593
{
519
593
    debug_decl(sudo_gr_delref, SUDOERS_DEBUG_NSS);
520
593
    sudo_gr_delref_item(ptr_to_item(gr));
521
593
    debug_return;
522
593
}
523
524
/*
525
 * Get a group entry by gid and allocate space for it.
526
 */
527
struct group *
528
sudo_getgrgid(gid_t gid)
529
552
{
530
552
    struct cache_item key, *item;
531
552
    struct rbnode *node;
532
552
    debug_decl(sudo_getgrgid, SUDOERS_DEBUG_NSS);
533
534
552
    if (grcache_bygid == NULL) {
535
552
  grcache_bygid = rbcreate(cmp_grgid);
536
552
  if (grcache_bygid == NULL) {
537
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
538
0
      debug_return_ptr(NULL);
539
0
  }
540
552
    }
541
542
552
    key.k.gid = gid;
543
552
    getauthregistry(NULL, key.registry);
544
552
    if ((node = rbfind(grcache_bygid, &key)) != NULL) {
545
0
  item = node->data;
546
0
  goto done;
547
0
    }
548
    /*
549
     * Cache group db entry if it exists or a negative response if not.
550
     */
551
552
    item = make_gritem(gid, NULL);
552
552
    if (item == NULL) {
553
451
  if (errno != ENOENT || (item = calloc(1, sizeof(*item))) == NULL) {
554
0
      sudo_warn(U_("unable to cache gid %u"), (unsigned int) gid);
555
      /* cppcheck-suppress memleak */
556
0
      debug_return_ptr(NULL);
557
0
  }
558
451
  item->refcnt = 1;
559
451
  item->k.gid = gid;
560
  /* item->d.gr = NULL; */
561
451
    }
562
552
    strlcpy(item->registry, key.registry, sizeof(item->registry));
563
552
    switch (rbinsert(grcache_bygid, item, NULL)) {
564
0
    case 1:
565
  /* should not happen */
566
0
  sudo_warnx(U_("unable to cache gid %u, already exists"),
567
0
      (unsigned int) gid);
568
0
  item->refcnt = 0;
569
0
  break;
570
0
    case -1:
571
  /* can't cache item, just return it */
572
0
  sudo_warn(U_("unable to cache gid %u"), (unsigned int) gid);
573
0
  item->refcnt = 0;
574
0
  break;
575
552
    }
576
552
done:
577
552
    if (item->refcnt != 0) {
578
552
  sudo_debug_printf(SUDO_DEBUG_DEBUG,
579
552
      "%s: gid %u [%s] -> group %s [%s] (%s)", __func__,
580
552
      (unsigned int)gid, key.registry,
581
552
      item->d.gr ? item->d.gr->gr_name : "unknown",
582
552
      item->registry, node ? "cache hit" : "cached");
583
552
    }
584
552
    if (item->d.gr != NULL)
585
101
  item->refcnt++;
586
552
    debug_return_ptr(item->d.gr);
587
552
}
588
589
/*
590
 * Get a group entry by name and allocate space for it.
591
 */
592
struct group *
593
sudo_getgrnam(const char *name)
594
165
{
595
165
    struct cache_item key, *item;
596
165
    struct rbnode *node;
597
165
    debug_decl(sudo_getgrnam, SUDOERS_DEBUG_NSS);
598
599
165
    if (grcache_byname == NULL) {
600
165
  grcache_byname = rbcreate(cmp_grnam);
601
165
  if (grcache_byname == NULL) {
602
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
603
0
      debug_return_ptr(NULL);
604
0
  }
605
165
    }
606
607
165
    key.k.name = (char *) name;
608
165
    getauthregistry(NULL, key.registry);
609
165
    if ((node = rbfind(grcache_byname, &key)) != NULL) {
610
0
  item = node->data;
611
0
  goto done;
612
0
    }
613
    /*
614
     * Cache group db entry if it exists or a negative response if not.
615
     */
616
165
    item = make_gritem((gid_t)-1, name);
617
165
    if (item == NULL) {
618
124
  const size_t len = strlen(name) + 1;
619
124
  if (errno != ENOENT || (item = calloc(1, sizeof(*item) + len)) == NULL) {
620
0
      sudo_warn(U_("unable to cache group %s"), name);
621
      /* cppcheck-suppress memleak */
622
0
      debug_return_ptr(NULL);
623
0
  }
624
124
  item->refcnt = 1;
625
124
  item->k.name = (char *) item + sizeof(*item);
626
124
  memcpy(item->k.name, name, len);
627
  /* item->d.gr = NULL; */
628
124
    }
629
165
    strlcpy(item->registry, key.registry, sizeof(item->registry));
630
165
    switch (rbinsert(grcache_byname, item, NULL)) {
631
0
    case 1:
632
  /* should not happen */
633
0
  sudo_warnx(U_("unable to cache group %s, already exists"), name);
634
0
  item->refcnt = 0;
635
0
  break;
636
0
    case -1:
637
  /* can't cache item, just return it */
638
0
  sudo_warn(U_("unable to cache group %s"), name);
639
0
  item->refcnt = 0;
640
0
  break;
641
165
    }
642
165
done:
643
165
    if (item->refcnt != 0) {
644
165
  sudo_debug_printf(SUDO_DEBUG_DEBUG,
645
165
      "%s: group %s [%s] -> gid %d [%s] (%s)", __func__, name,
646
165
      key.registry, item->d.gr ? (int)item->d.gr->gr_gid : -1,
647
165
      item->registry, node ? "cache hit" : "cached");
648
165
    }
649
165
    if (item->d.gr != NULL)
650
41
  item->refcnt++;
651
165
    debug_return_ptr(item->d.gr);
652
165
}
653
654
/*
655
 * Take a group name, ID, members and return a faked up group struct.
656
 */
657
struct group *
658
sudo_mkgrent(const char *group, gid_t gid, ...)
659
451
{
660
451
    struct cache_item_gr *gritem;
661
451
    struct cache_item *item;
662
451
    struct group *gr;
663
451
    size_t nmem, nsize, total;
664
451
    char *cp, *mem;
665
451
    va_list ap;
666
451
    unsigned int i;
667
451
    debug_decl(sudo_mkgrent, SUDOERS_DEBUG_NSS);
668
669
451
    if (grcache_bygid == NULL)
670
0
  grcache_bygid = rbcreate(cmp_grgid);
671
451
    if (grcache_byname == NULL)
672
451
  grcache_byname = rbcreate(cmp_grnam);
673
451
    if (grcache_bygid == NULL || grcache_byname == NULL) {
674
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
675
0
  debug_return_ptr(NULL);
676
0
    }
677
678
    /* Allocate in one big chunk for easy freeing. */
679
451
    nsize = strlen(group) + 1;
680
451
    total = sizeof(*gritem) + nsize;
681
451
    va_start(ap, gid);
682
451
    for (nmem = 1; (mem = va_arg(ap, char *)) != NULL; nmem++) {
683
0
  total += strlen(mem) + 1;
684
0
    }
685
451
    va_end(ap);
686
451
    total += sizeof(char *) * nmem;
687
688
1.35k
    for (i = 0; i < 2; i++) {
689
902
  struct rbtree *grcache;
690
902
  struct rbnode *node;
691
692
  /*
693
   * Fill in group contents and make strings relative to space
694
   * at the end of the buffer.  Note that gr_mem must come
695
   * immediately after struct group to guarantee proper alignment.
696
   */
697
902
  gritem = calloc(1, total);
698
902
  if (gritem == NULL) {
699
0
      sudo_warn(U_("unable to cache group %s"), group);
700
0
      debug_return_ptr(NULL);
701
0
  }
702
902
  gr = &gritem->gr;
703
902
  gr->gr_gid = gid;
704
902
  gr->gr_passwd = (char *)"*";
705
902
  cp = (char *)(gritem + 1);
706
902
  gr->gr_mem = (char **)cp;
707
902
  cp += sizeof(char *) * nmem;
708
902
  va_start(ap, gid);
709
902
  for (nmem = 0; (mem = va_arg(ap, char *)) != NULL; nmem++) {
710
0
      size_t len = strlen(mem) + 1;
711
0
      memcpy(cp, mem, len);
712
0
      gr->gr_mem[nmem] = cp;
713
0
      cp += len;
714
0
  }
715
902
  va_end(ap);
716
902
  gr->gr_mem[nmem] = NULL;
717
902
  gr->gr_name = cp;
718
902
  memcpy(gr->gr_name, group, nsize);
719
720
902
  item = &gritem->cache;
721
902
  item->refcnt = 1;
722
902
  item->d.gr = gr;
723
902
  if (i == 0) {
724
      /* Store by gid if it doesn't already exist. */
725
451
      item->k.gid = gr->gr_gid;
726
451
      grcache = grcache_bygid;
727
451
  } else {
728
      /* Store by name, overwriting cached version. */
729
451
      gritem->cache.k.name = gr->gr_name;
730
451
      grcache = grcache_byname;
731
451
  }
732
902
  getauthregistry(NULL, item->registry);
733
902
  switch (rbinsert(grcache, item, &node)) {
734
451
  case 1:
735
      /* Already exists. */
736
451
      item = node->data;
737
451
      if (item->d.gr == NULL) {
738
    /* Negative cache entry, replace with ours. */
739
451
    sudo_gr_delref_item(item);
740
451
    item = node->data = &gritem->cache;
741
451
      } else {
742
    /* Good entry, discard our fake one. */
743
0
    free(gritem);
744
0
      }
745
451
      break;
746
0
  case -1:
747
      /* can't cache item, just return it */
748
0
      sudo_warn(U_("unable to cache group %s"), group);
749
0
      item->refcnt = 0;
750
0
      break;
751
902
  }
752
902
    }
753
451
    if (item->d.gr != NULL)
754
451
  item->refcnt++;
755
451
    debug_return_ptr(item->d.gr);
756
451
}
757
758
/*
759
 * Take a gid in string form "#123" and return a faked up group struct.
760
 */
761
struct group *
762
sudo_fakegrnam(const char *group)
763
451
{
764
451
    const char *errstr;
765
451
    gid_t gid;
766
451
    debug_decl(sudo_fakegrnam, SUDOERS_DEBUG_NSS);
767
768
451
    gid = (gid_t) sudo_strtoid(group + 1, &errstr);
769
451
    if (errstr != NULL) {
770
0
  sudo_debug_printf(SUDO_DEBUG_DIAG|SUDO_DEBUG_LINENO,
771
0
      "gid %s %s", group, errstr);
772
0
  debug_return_ptr(NULL);
773
0
    }
774
775
451
    debug_return_ptr(sudo_mkgrent(group, gid, (char *)NULL));
776
451
}
777
778
void
779
sudo_gidlist_addref(struct gid_list *gidlist)
780
0
{
781
0
    debug_decl(sudo_gidlist_addref, SUDOERS_DEBUG_NSS);
782
0
    ptr_to_item(gidlist)->refcnt++;
783
0
    debug_return;
784
0
}
785
786
static void
787
sudo_gidlist_delref_item(void *v)
788
59.8k
{
789
59.8k
    struct cache_item *item = v;
790
59.8k
    debug_decl(sudo_gidlist_delref_item, SUDOERS_DEBUG_NSS);
791
792
59.8k
    if (--item->refcnt == 0)
793
26.6k
  free(item);
794
795
59.8k
    debug_return;
796
59.8k
}
797
798
void
799
sudo_gidlist_delref(struct gid_list *gidlist)
800
33.2k
{
801
33.2k
    debug_decl(sudo_gidlist_delref, SUDOERS_DEBUG_NSS);
802
33.2k
    sudo_gidlist_delref_item(ptr_to_item(gidlist));
803
33.2k
    debug_return;
804
33.2k
}
805
806
void
807
sudo_grlist_addref(struct group_list *grlist)
808
0
{
809
0
    debug_decl(sudo_grlist_addref, SUDOERS_DEBUG_NSS);
810
0
    ptr_to_item(grlist)->refcnt++;
811
0
    debug_return;
812
0
}
813
814
static void
815
sudo_grlist_delref_item(void *v)
816
0
{
817
0
    struct cache_item *item = v;
818
0
    debug_decl(sudo_grlist_delref_item, SUDOERS_DEBUG_NSS);
819
820
0
    if (--item->refcnt == 0)
821
0
  free(item);
822
823
0
    debug_return;
824
0
}
825
826
void
827
sudo_grlist_delref(struct group_list *grlist)
828
0
{
829
0
    debug_decl(sudo_grlist_delref, SUDOERS_DEBUG_NSS);
830
0
    sudo_grlist_delref_item(ptr_to_item(grlist));
831
0
    debug_return;
832
0
}
833
834
void
835
sudo_freegrcache(void)
836
25.3k
{
837
25.3k
    debug_decl(sudo_freegrcache, SUDOERS_DEBUG_NSS);
838
839
25.3k
    if (grcache_bygid != NULL) {
840
552
  rbdestroy(grcache_bygid, sudo_gr_delref_item);
841
552
  grcache_bygid = NULL;
842
552
    }
843
25.3k
    if (grcache_byname != NULL) {
844
616
  rbdestroy(grcache_byname, sudo_gr_delref_item);
845
616
  grcache_byname = NULL;
846
616
    }
847
25.3k
    if (grlist_cache != NULL) {
848
0
  rbdestroy(grlist_cache, sudo_grlist_delref_item);
849
0
  grlist_cache = NULL;
850
0
    }
851
25.3k
    if (gidlist_cache != NULL) {
852
20.1k
  rbdestroy(gidlist_cache, sudo_gidlist_delref_item);
853
20.1k
  gidlist_cache = NULL;
854
20.1k
    }
855
856
25.3k
    debug_return;
857
25.3k
}
858
859
struct group_list *
860
sudo_get_grlist(const struct passwd *pw)
861
0
{
862
0
    struct cache_item key, *item;
863
0
    struct rbnode *node;
864
0
    debug_decl(sudo_get_grlist, SUDOERS_DEBUG_NSS);
865
866
0
    sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: looking up group names for %s",
867
0
  __func__, pw->pw_name);
868
869
0
    if (grlist_cache == NULL) {
870
0
  grlist_cache = rbcreate(cmp_pwnam);
871
0
  if (grlist_cache == NULL) {
872
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
873
0
      debug_return_ptr(NULL);
874
0
  }
875
0
    }
876
877
0
    key.k.name = pw->pw_name;
878
0
    getauthregistry(pw->pw_name, key.registry);
879
0
    if ((node = rbfind(grlist_cache, &key)) != NULL) {
880
0
  item = node->data;
881
0
  goto done;
882
0
    }
883
    /*
884
     * Cache group db entry if it exists or a negative response if not.
885
     */
886
0
    item = make_grlist_item(pw, NULL);
887
0
    if (item == NULL) {
888
  /* Out of memory? */
889
0
  debug_return_ptr(NULL);
890
0
    }
891
0
    strlcpy(item->registry, key.registry, sizeof(item->registry));
892
0
    switch (rbinsert(grlist_cache, item, NULL)) {
893
0
    case 1:
894
  /* should not happen */
895
0
  sudo_warnx(U_("unable to cache group list for %s, already exists"),
896
0
      pw->pw_name);
897
0
  item->refcnt = 0;
898
0
  break;
899
0
    case -1:
900
  /* can't cache item, just return it */
901
0
  sudo_warn(U_("unable to cache group list for %s"), pw->pw_name);
902
0
  item->refcnt = 0;
903
0
  break;
904
0
    }
905
0
    if (item->d.grlist != NULL) {
906
0
  int i;
907
0
  for (i = 0; i < item->d.grlist->ngroups; i++) {
908
0
      sudo_debug_printf(SUDO_DEBUG_DEBUG,
909
0
    "%s: user %s is a member of group %s", __func__,
910
0
    pw->pw_name, item->d.grlist->groups[i]);
911
0
  }
912
0
    }
913
0
done:
914
0
    if (item->d.grlist != NULL)
915
0
  item->refcnt++;
916
0
    debug_return_ptr(item->d.grlist);
917
0
}
918
919
static void
920
sudo_debug_group_list(const char *user, char * const *groups,
921
    unsigned int level)
922
1.16k
{
923
1.16k
    size_t i, len = 0;
924
1.16k
    debug_decl(sudo_debug_group_list, SUDOERS_DEBUG_NSS);
925
926
1.16k
    if (groups == NULL || !sudo_debug_needed(level))
927
1.16k
  debug_return;
928
929
0
    for (i = 0; groups[i] != NULL; i++) {
930
0
  len += strlen(groups[i]) + 1;
931
0
    }
932
0
    if (len != 0) {
933
0
  char *groupstr = malloc(len);
934
0
  if (groupstr != NULL) {
935
0
      char *cp = groupstr;
936
0
      for (i = 0; groups[i] != NULL; i++) {
937
0
    size_t n = (size_t)snprintf(cp, len, "%s%s", i ? "," : "",
938
0
        groups[i]);
939
0
    if (n >= len)
940
0
        break;
941
0
    cp += n;
942
0
    len -= n;
943
0
      }
944
0
      sudo_debug_printf(level, "%s: %s", user, groupstr);
945
0
      free(groupstr);
946
0
  }
947
0
    }
948
0
    debug_return;
949
0
}
950
951
int
952
sudo_set_grlist(struct passwd *pw, char * const *groups)
953
0
{
954
0
    struct cache_item key, *item;
955
0
    debug_decl(sudo_set_grlist, SUDOERS_DEBUG_NSS);
956
957
0
    sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: setting group names for %s",
958
0
  __func__, pw->pw_name);
959
960
0
    sudo_debug_group_list(pw->pw_name, groups, SUDO_DEBUG_DEBUG);
961
962
0
    if (grlist_cache == NULL) {
963
0
  grlist_cache = rbcreate(cmp_pwnam);
964
0
  if (grlist_cache == NULL) {
965
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
966
0
      debug_return_int(-1);
967
0
  }
968
0
    }
969
970
    /*
971
     * Cache group db entry if it doesn't already exist
972
     */
973
0
    key.k.name = pw->pw_name;
974
0
    getauthregistry(pw->pw_name, key.registry);
975
0
    if (rbfind(grlist_cache, &key) == NULL) {
976
0
  if ((item = make_grlist_item(pw, groups)) == NULL) {
977
0
      sudo_warnx(U_("unable to parse groups for %s"), pw->pw_name);
978
0
      debug_return_int(-1);
979
0
  }
980
0
  strlcpy(item->registry, key.registry, sizeof(item->registry));
981
0
  switch (rbinsert(grlist_cache, item, NULL)) {
982
0
  case 1:
983
0
      sudo_warnx(U_("unable to cache group list for %s, already exists"),
984
0
    pw->pw_name);
985
0
      sudo_grlist_delref_item(item);
986
0
      break;
987
0
  case -1:
988
0
      sudo_warn(U_("unable to cache group list for %s"), pw->pw_name);
989
0
      sudo_grlist_delref_item(item);
990
0
      debug_return_int(-1);
991
0
  }
992
0
    } else {
993
0
  sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
994
0
      "groups for user %s are already cached", pw->pw_name);
995
0
    }
996
997
0
    debug_return_int(0);
998
0
}
999
1000
struct gid_list *
1001
sudo_get_gidlist(const struct passwd *pw, unsigned int type)
1002
33.2k
{
1003
33.2k
    struct cache_item key, *item;
1004
33.2k
    struct rbnode *node;
1005
33.2k
    debug_decl(sudo_get_gidlist, SUDOERS_DEBUG_NSS);
1006
1007
33.2k
    sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: looking up group-IDs for %s",
1008
33.2k
  __func__, pw->pw_name);
1009
1010
33.2k
    if (gidlist_cache == NULL) {
1011
18.9k
  gidlist_cache = rbcreate(cmp_gidlist);
1012
18.9k
  if (gidlist_cache == NULL) {
1013
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1014
0
      debug_return_ptr(NULL);
1015
0
  }
1016
18.9k
    }
1017
1018
33.2k
    key.k.name = pw->pw_name;
1019
33.2k
    key.type = type;
1020
33.2k
    getauthregistry(pw->pw_name, key.registry);
1021
33.2k
    if ((node = rbfind(gidlist_cache, &key)) != NULL) {
1022
7.73k
  item = node->data;
1023
7.73k
  goto done;
1024
7.73k
    }
1025
    /*
1026
     * Cache group db entry if it exists or a negative response if not.
1027
     */
1028
25.4k
    item = make_gidlist_item(pw, -1, NULL, NULL, type);
1029
25.4k
    if (item == NULL) {
1030
  /* Out of memory? */
1031
0
  debug_return_ptr(NULL);
1032
0
    }
1033
25.4k
    strlcpy(item->registry, key.registry, sizeof(item->registry));
1034
25.4k
    switch (rbinsert(gidlist_cache, item, NULL)) {
1035
0
    case 1:
1036
  /* should not happen */
1037
0
  sudo_warnx(U_("unable to cache group list for %s, already exists"),
1038
0
      pw->pw_name);
1039
0
  item->refcnt = 0;
1040
0
  break;
1041
0
    case -1:
1042
  /* can't cache item, just return it */
1043
0
  sudo_warn(U_("unable to cache group list for %s"), pw->pw_name);
1044
0
  item->refcnt = 0;
1045
0
  break;
1046
25.4k
    }
1047
25.4k
    if (item->d.gidlist != NULL) {
1048
25.4k
  int i;
1049
50.9k
  for (i = 0; i < item->d.gidlist->ngids; i++) {
1050
25.4k
      sudo_debug_printf(SUDO_DEBUG_DEBUG,
1051
25.4k
    "%s: user %s has supplementary gid %u", __func__,
1052
25.4k
    pw->pw_name, (unsigned int)item->d.gidlist->gids[i]);
1053
25.4k
  }
1054
25.4k
    }
1055
33.2k
done:
1056
33.2k
    if (item->d.gidlist != NULL)
1057
33.2k
  item->refcnt++;
1058
33.2k
    debug_return_ptr(item->d.gidlist);
1059
33.2k
}
1060
1061
int
1062
sudo_set_gidlist(struct passwd *pw, int ngids, GETGROUPS_T *gids,
1063
    char * const *gidstrs, unsigned int type)
1064
1.16k
{
1065
1.16k
    struct cache_item key, *item;
1066
1.16k
    debug_decl(sudo_set_gidlist, SUDOERS_DEBUG_NSS);
1067
1068
1.16k
    sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: setting group-IDs for %s",
1069
1.16k
  __func__, pw->pw_name);
1070
1071
    /* XXX - ngids/gids too */
1072
1.16k
    sudo_debug_group_list(pw->pw_name, gidstrs, SUDO_DEBUG_DEBUG);
1073
1074
1.16k
    if (gidlist_cache == NULL) {
1075
1.16k
  gidlist_cache = rbcreate(cmp_gidlist);
1076
1.16k
  if (gidlist_cache == NULL) {
1077
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1078
0
      debug_return_int(-1);
1079
0
  }
1080
1.16k
    }
1081
1082
    /*
1083
     * Cache group db entry if it doesn't already exist
1084
     */
1085
1.16k
    key.k.name = pw->pw_name;
1086
1.16k
    key.type = type;
1087
1.16k
    getauthregistry(pw->pw_name, key.registry);
1088
1.16k
    if (rbfind(gidlist_cache, &key) == NULL) {
1089
1.16k
  if ((item = make_gidlist_item(pw, ngids, gids, gidstrs, type)) == NULL) {
1090
0
      sudo_warnx(U_("unable to parse gids for %s"), pw->pw_name);
1091
0
      debug_return_int(-1);
1092
0
  }
1093
1.16k
  strlcpy(item->registry, key.registry, sizeof(item->registry));
1094
1.16k
  switch (rbinsert(gidlist_cache, item, NULL)) {
1095
0
  case 1:
1096
0
      sudo_warnx(U_("unable to cache group list for %s, already exists"),
1097
0
    pw->pw_name);
1098
0
      sudo_gidlist_delref_item(item);
1099
0
      break;
1100
0
  case -1:
1101
0
      sudo_warn(U_("unable to cache group list for %s"), pw->pw_name);
1102
0
      sudo_gidlist_delref_item(item);
1103
0
      debug_return_int(-1);
1104
1.16k
  }
1105
1.16k
    } else {
1106
0
  sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
1107
0
      "gids for user %s are already cached", pw->pw_name);
1108
0
    }
1109
1110
1.16k
    debug_return_int(0);
1111
1.16k
}
1112
1113
bool
1114
user_in_group(const struct passwd *pw, const char *group)
1115
0
{
1116
0
    struct group_list *grlist = NULL;
1117
0
    struct gid_list *gidlist = NULL;
1118
0
    struct group *grp = NULL;
1119
0
    bool matched = false;
1120
0
    int i;
1121
0
    debug_decl(user_in_group, SUDOERS_DEBUG_NSS);
1122
1123
    /*
1124
     * If it could be a sudo-style group-ID check gids first.
1125
     */
1126
0
    if (group[0] == '#') {
1127
0
  const char *errstr;
1128
0
  gid_t gid = (gid_t) sudo_strtoid(group + 1, &errstr);
1129
0
  if (errstr != NULL) {
1130
0
      sudo_debug_printf(SUDO_DEBUG_DIAG|SUDO_DEBUG_LINENO,
1131
0
    "gid %s %s", group, errstr);
1132
0
  } else {
1133
0
      if (gid == pw->pw_gid) {
1134
0
    matched = true;
1135
0
    goto done;
1136
0
      }
1137
0
      if ((gidlist = sudo_get_gidlist(pw, ENTRY_TYPE_ANY)) != NULL) {
1138
0
    for (i = 0; i < gidlist->ngids; i++) {
1139
0
        if (gid == gidlist->gids[i]) {
1140
0
      matched = true;
1141
0
      goto done;
1142
0
        }
1143
0
    }
1144
0
      }
1145
0
  }
1146
0
    }
1147
1148
    /*
1149
     * Next match the group name.  By default, sudoers resolves all the user's
1150
     * group-IDs to names and matches by name.  If match_group_by_gid is
1151
     * set, each group is sudoers is resolved and matching is by group-ID.
1152
     */
1153
0
    if (def_match_group_by_gid) {
1154
0
  gid_t gid;
1155
1156
  /* Look up the ID of the group in sudoers. */
1157
0
  if ((grp = sudo_getgrnam(group)) == NULL)
1158
0
      goto done;
1159
0
  gid = grp->gr_gid;
1160
1161
  /* Check against user's primary (passwd file) group-ID. */
1162
0
  if (gid == pw->pw_gid) {
1163
0
      matched = true;
1164
0
      goto done;
1165
0
  }
1166
1167
  /* Check the supplementary group vector. */
1168
0
  if (gidlist == NULL) {
1169
0
      if ((gidlist = sudo_get_gidlist(pw, ENTRY_TYPE_ANY)) != NULL) {
1170
0
    for (i = 0; i < gidlist->ngids; i++) {
1171
0
        if (gid == gidlist->gids[i]) {
1172
0
      matched = true;
1173
0
      goto done;
1174
0
        }
1175
0
    }
1176
0
      }
1177
0
  }
1178
0
    } else if ((grlist = sudo_get_grlist(pw)) != NULL) {
1179
0
  int (*compare)(const char *, const char *);
1180
0
  if (def_case_insensitive_group)
1181
0
      compare = strcasecmp;
1182
0
  else
1183
0
      compare = strcmp;
1184
1185
  /* Check the user's group vector, which includes the primary group. */
1186
0
  for (i = 0; i < grlist->ngroups; i++) {
1187
0
      if (compare(group, grlist->groups[i]) == 0) {
1188
0
    matched = true;
1189
0
    goto done;
1190
0
      }
1191
0
  }
1192
0
    }
1193
1194
0
done:
1195
0
    if (grp != NULL)
1196
0
  sudo_gr_delref(grp);
1197
0
    if (grlist != NULL)
1198
0
  sudo_grlist_delref(grlist);
1199
0
    if (gidlist != NULL)
1200
0
  sudo_gidlist_delref(gidlist);
1201
1202
0
    sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: user %s %sin group %s",
1203
0
  __func__, pw->pw_name, matched ? "" : "NOT ", group);
1204
0
    debug_return_bool(matched);
1205
0
}
1206
1207
/*
1208
 * Returns true if the user's shell is considered to be valid.
1209
 */
1210
bool
1211
user_shell_valid(const struct passwd *pw)
1212
15.0k
{
1213
15.0k
    debug_decl(user_shell_valid, SUDOERS_DEBUG_NSS);
1214
1215
15.0k
    if (!def_runas_check_shell)
1216
15.0k
  debug_return_bool(true);
1217
1218
0
    debug_return_bool(valid_shell(pw->pw_shell));
1219
0
}