Coverage Report

Created: 2025-08-29 07:10

/src/sudo/plugins/sudoers/match.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 1996, 1998-2005, 2007-2023, 2025
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
/*
25
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
26
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
27
 */
28
29
#include <config.h>
30
31
#include <sys/stat.h>
32
#ifdef HAVE_SYS_SYSTEMINFO_H
33
# include <sys/systeminfo.h>
34
#endif
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#ifdef HAVE_STRINGS_H
39
# include <strings.h>
40
#endif /* HAVE_STRINGS_H */
41
#include <unistd.h>
42
#ifdef HAVE_NETGROUP_H
43
# include <netgroup.h>
44
#else
45
# include <netdb.h>
46
#endif /* HAVE_NETGROUP_H */
47
#include <dirent.h>
48
#include <fcntl.h>
49
#include <pwd.h>
50
#include <grp.h>
51
#include <errno.h>
52
#ifdef HAVE_FNMATCH
53
# include <fnmatch.h>
54
#else
55
# include <compat/fnmatch.h>
56
#endif /* HAVE_FNMATCH */
57
58
#include <sudoers.h>
59
#include <gram.h>
60
61
/*
62
 * Check whether user described by pw matches member.
63
 * Returns ALLOW, DENY or UNSPEC.
64
 */
65
int
66
user_matches(const struct sudoers_parse_tree *parse_tree,
67
    const struct passwd *pw, const struct member *m)
68
16.9k
{
69
16.9k
    const struct sudoers_context *ctx = parse_tree->ctx;
70
16.9k
    const char *lhost = parse_tree->lhost ? parse_tree->lhost : ctx->runas.host;
71
16.9k
    const char *shost = parse_tree->shost ? parse_tree->shost : ctx->runas.shost;
72
16.9k
    int matched = UNSPEC;
73
16.9k
    struct alias *a;
74
16.9k
    debug_decl(user_matches, SUDOERS_DEBUG_MATCH);
75
76
16.9k
    switch (m->type) {
77
108
  case ALL:
78
108
      matched = m->negated ? DENY : ALLOW;
79
108
      break;
80
0
  case NETGROUP:
81
0
      if (netgr_matches(parse_tree->nss, m->name,
82
0
    def_netgroup_tuple ? lhost : NULL,
83
0
    def_netgroup_tuple ? shost : NULL, pw->pw_name) == ALLOW)
84
0
    matched = m->negated ? DENY : ALLOW;
85
0
      break;
86
15.3k
  case USERGROUP:
87
15.3k
      if (usergr_matches(m->name, pw->pw_name, pw) == ALLOW)
88
4.64k
    matched = m->negated ? DENY : ALLOW;
89
15.3k
      break;
90
41
  case ALIAS:
91
41
      if ((a = alias_get(parse_tree, m->name, USERALIAS)) != NULL) {
92
    /* XXX */
93
0
    const int rc = userlist_matches(parse_tree, pw, &a->members);
94
0
    if (SPECIFIED(rc)) {
95
0
        if (m->negated) {
96
0
      matched = rc == ALLOW ? DENY : ALLOW;
97
0
        } else {
98
0
      matched = rc;
99
0
        }
100
0
    }
101
0
    alias_put(a);
102
0
    break;
103
0
      }
104
41
      FALLTHROUGH;
105
1.49k
  case WORD:
106
1.49k
      if (userpw_matches(m->name, pw->pw_name, pw) == ALLOW)
107
296
    matched = m->negated ? DENY : ALLOW;
108
1.49k
      break;
109
16.9k
    }
110
16.9k
    debug_return_int(matched);
111
16.9k
}
112
113
/*
114
 * Check for user described by pw in a list of members.
115
 * Returns ALLOW, DENY or UNSPEC.
116
 */
117
int
118
userlist_matches(const struct sudoers_parse_tree *parse_tree,
119
    const struct passwd *pw, const struct member_list *list)
120
15.3k
{
121
15.3k
    struct member *m;
122
15.3k
    int matched = UNSPEC;
123
15.3k
    debug_decl(userlist_matches, SUDOERS_DEBUG_MATCH);
124
125
16.9k
    TAILQ_FOREACH_REVERSE(m, list, member_list, entries) {
126
16.9k
  matched = user_matches(parse_tree, pw, m);
127
16.9k
  if (SPECIFIED(matched))
128
5.04k
      break;
129
16.9k
    }
130
15.3k
    debug_return_int(matched);
131
15.3k
}
132
133
struct gid_list *
134
runas_getgroups(const struct sudoers_context *ctx)
135
0
{
136
0
    const struct passwd *pw;
137
0
    debug_decl(runas_getgroups, SUDOERS_DEBUG_MATCH);
138
139
0
    if (def_preserve_groups) {
140
0
  sudo_gidlist_addref(ctx->user.gid_list);
141
0
  debug_return_ptr(ctx->user.gid_list);
142
0
    }
143
144
    /* Only use results from a group db query, not the front end. */
145
0
    pw = ctx->runas.pw ? ctx->runas.pw : ctx->user.pw;
146
0
    debug_return_ptr(sudo_get_gidlist(pw, ENTRY_TYPE_QUERIED));
147
0
}
148
149
/*
150
 * Check whether the requested runas user matches user_list, the
151
 * user portion of a sudoers runaslist.  If user_list is NULL, a
152
 * list containing runas_default is used.
153
 * Returns ALLOW, DENY or UNSPEC.
154
 */
155
static int
156
runas_userlist_matches(const struct sudoers_parse_tree *parse_tree,
157
    const struct member_list *user_list)
158
404
{
159
404
    const struct sudoers_context *ctx = parse_tree->ctx;
160
404
    const char *lhost = parse_tree->lhost ? parse_tree->lhost : ctx->runas.host;
161
404
    const char *shost = parse_tree->shost ? parse_tree->shost : ctx->runas.shost;
162
404
    int user_matched = UNSPEC;
163
404
    struct member *m;
164
404
    struct alias *a;
165
404
    debug_decl(runas_userlist_matches, SUDOERS_DEBUG_MATCH);
166
167
782
    TAILQ_FOREACH_REVERSE(m, user_list, member_list, entries) {
168
782
  switch (m->type) {
169
52
      case ALL:
170
52
    user_matched = m->negated ? DENY : ALLOW;
171
52
    break;
172
0
      case NETGROUP:
173
0
    if (netgr_matches(parse_tree->nss, m->name,
174
0
        def_netgroup_tuple ? lhost : NULL,
175
0
        def_netgroup_tuple ? shost : NULL,
176
0
        ctx->runas.pw->pw_name) == ALLOW)
177
0
        user_matched = m->negated ? DENY : ALLOW;
178
0
    break;
179
0
      case USERGROUP:
180
0
    if (usergr_matches(m->name, ctx->runas.pw->pw_name, ctx->runas.pw) == ALLOW)
181
0
        user_matched = m->negated ? DENY : ALLOW;
182
0
    break;
183
276
      case ALIAS:
184
276
    a = alias_get(parse_tree, m->name, RUNASALIAS);
185
276
    if (a != NULL) {
186
0
        const int rc = runas_userlist_matches(parse_tree,
187
0
      &a->members);
188
0
        if (SPECIFIED(rc)) {
189
0
      if (m->negated) {
190
0
          user_matched = rc == ALLOW ? DENY : ALLOW;
191
0
      } else {
192
0
          user_matched = rc;
193
0
      }
194
0
        }
195
0
        alias_put(a);
196
0
        break;
197
0
    }
198
276
    FALLTHROUGH;
199
730
      case WORD:
200
730
    if (userpw_matches(m->name, ctx->runas.pw->pw_name, ctx->runas.pw) == ALLOW)
201
38
        user_matched = m->negated ? DENY : ALLOW;
202
730
    break;
203
0
      case MYSELF:
204
    /*
205
     * Only match a rule with an empty runas user if a group
206
     * was specified on the command line without a user _or_
207
     * the user specified their own name on the command line.
208
     */
209
0
    if ((!ISSET(ctx->settings.flags, RUNAS_USER_SPECIFIED) &&
210
0
      ISSET(ctx->settings.flags, RUNAS_GROUP_SPECIFIED)) ||
211
0
      strcmp(ctx->user.name, ctx->runas.pw->pw_name) == 0)
212
0
        user_matched = m->negated ? DENY : ALLOW;
213
0
    break;
214
782
  }
215
782
    }
216
404
    debug_return_int(user_matched);
217
404
}
218
219
/*
220
 * Check whether the requested runas group matches group_list, the
221
 * group portion of a sudoers runaslist, or the runas user's groups.
222
 * Returns ALLOW, DENY or UNSPEC.
223
 */
224
static int
225
runas_grouplist_matches(const struct sudoers_parse_tree *parse_tree,
226
    const struct member_list *group_list, int user_matched)
227
117
{
228
117
    const struct sudoers_context *ctx = parse_tree->ctx;
229
117
    int group_matched = UNSPEC;
230
117
    struct member *m;
231
117
    struct alias *a;
232
117
    debug_decl(runas_grouplist_matches, SUDOERS_DEBUG_MATCH);
233
234
117
    if (group_list != NULL) {
235
0
  TAILQ_FOREACH_REVERSE(m, group_list, member_list, entries) {
236
0
      switch (m->type) {
237
0
    case ALL:
238
0
        group_matched = m->negated ? DENY : ALLOW;
239
0
        break;
240
0
    case ALIAS:
241
0
        a = alias_get(parse_tree, m->name, RUNASALIAS);
242
0
        if (a != NULL) {
243
0
      const int rc = runas_grouplist_matches(parse_tree,
244
0
          &a->members, user_matched);
245
0
      if (SPECIFIED(rc)) {
246
0
          if (m->negated) {
247
0
        group_matched = rc == ALLOW ? DENY : ALLOW;
248
0
          } else {
249
0
        group_matched = rc;
250
0
          }
251
0
      }
252
0
      alias_put(a);
253
0
      break;
254
0
        }
255
0
        FALLTHROUGH;
256
0
    case WORD:
257
0
        if (group_matches(m->name, ctx->runas.gr) == ALLOW)
258
0
      group_matched = m->negated ? DENY : ALLOW;
259
0
        break;
260
0
      }
261
0
  }
262
0
    }
263
117
    if (!SPECIFIED(group_matched) && user_matched == ALLOW) {
264
44
  struct gid_list *runas_groups;
265
  /*
266
   * The runas group was not explicitly allowed by sudoers.
267
   * If the runas user matched, check its group list too.
268
   */
269
44
  if (ctx->runas.pw->pw_gid == ctx->runas.gr->gr_gid) {
270
44
      group_matched = ALLOW; /* runas group matches passwd db */
271
44
  } else if ((runas_groups = runas_getgroups(ctx)) != NULL) {
272
0
      int i;
273
274
0
      for (i = 0; i < runas_groups->ngids; i++) {
275
0
    if (runas_groups->gids[i] == ctx->runas.gr->gr_gid) {
276
0
        group_matched = ALLOW; /* matched aux group vector */
277
0
        break;
278
0
    }
279
0
      }
280
0
      sudo_gidlist_delref(runas_groups);
281
0
  }
282
44
    }
283
284
117
    debug_return_int(group_matched);
285
117
}
286
287
/*
288
 * Check whether the sudoers runaslist, composed of user_list and
289
 * group_list, matches the runas user/group requested by the user.
290
 * Either (or both) user_list and group_list may be NULL.
291
 * If user_list is NULL, a list containing runas_default is used.
292
 * Returns ALLOW, DENY or UNSPEC.
293
 */
294
int
295
runaslist_matches(const struct sudoers_parse_tree *parse_tree,
296
    const struct member_list *user_list, const struct member_list *group_list)
297
404
{
298
404
    const struct sudoers_context *ctx = parse_tree->ctx;
299
404
    struct member_list _user_list = TAILQ_HEAD_INITIALIZER(_user_list);
300
404
    int user_matched, group_matched = UNSPEC;
301
404
    struct member m_user;
302
404
    debug_decl(runaslist_matches, SUDOERS_DEBUG_MATCH);
303
304
    /* If no runas user listed in sudoers, use the default value.  */
305
404
    if (user_list == NULL) {
306
65
  m_user.name = def_runas_default;
307
65
  m_user.type = WORD;
308
65
  m_user.negated = false;
309
65
  TAILQ_INSERT_HEAD(&_user_list, &m_user, entries);
310
65
  user_list = &_user_list;
311
65
    }
312
313
404
    user_matched = runas_userlist_matches(parse_tree, user_list);
314
404
    if (ISSET(ctx->settings.flags, RUNAS_GROUP_SPECIFIED)) {
315
117
  group_matched = runas_grouplist_matches(parse_tree, group_list,
316
117
      user_matched);
317
  /*
318
   * Allow "sudo -g group" or "sudo -u myname -g group"
319
   * if the runas group matches.
320
   */
321
117
  if (group_matched == ALLOW && user_matched == UNSPEC) {
322
0
      if (strcmp(ctx->user.name, ctx->runas.pw->pw_name) == 0)
323
0
    user_matched = ALLOW;
324
0
  }
325
117
    }
326
327
404
    if (user_matched == DENY || group_matched == DENY)
328
0
  debug_return_int(DENY);
329
404
    if (user_matched == group_matched || ctx->runas.gr == NULL)
330
404
  debug_return_int(user_matched);
331
0
    debug_return_int(UNSPEC);
332
0
}
333
334
/*
335
 * Check for lhost and shost in a list of members.
336
 * Returns ALLOW, DENY or UNSPEC.
337
 */
338
static int
339
hostlist_matches_int(const struct sudoers_parse_tree *parse_tree,
340
    const struct passwd *pw, const char *lhost, const char *shost,
341
    const struct member_list *list)
342
4.78k
{
343
4.78k
    struct member *m;
344
4.78k
    int matched = UNSPEC;
345
4.78k
    debug_decl(hostlist_matches, SUDOERS_DEBUG_MATCH);
346
347
4.78k
    TAILQ_FOREACH_REVERSE(m, list, member_list, entries) {
348
4.78k
  matched = host_matches(parse_tree, pw, lhost, shost, m);
349
4.78k
  if (SPECIFIED(matched))
350
582
      break;
351
4.78k
    }
352
4.78k
    debug_return_int(matched);
353
4.78k
}
354
355
/*
356
 * Check for ctx->runas.host and ctx->runas.shost in a list of members.
357
 * Returns ALLOW, DENY or UNSPEC.
358
 */
359
int
360
hostlist_matches(const struct sudoers_parse_tree *parse_tree,
361
    const struct passwd *pw, const struct member_list *list)
362
4.78k
{
363
4.78k
    const struct sudoers_context *ctx = parse_tree->ctx;
364
4.78k
    const char *lhost = parse_tree->lhost ? parse_tree->lhost : ctx->runas.host;
365
4.78k
    const char *shost = parse_tree->shost ? parse_tree->shost : ctx->runas.shost;
366
367
4.78k
    return hostlist_matches_int(parse_tree, pw, lhost, shost, list);
368
4.78k
}
369
370
/*
371
 * Check whether host or shost matches member.
372
 * Returns ALLOW, DENY or UNSPEC.
373
 */
374
int
375
host_matches(const struct sudoers_parse_tree *parse_tree,
376
    const struct passwd *pw, const char *lhost, const char *shost,
377
    const struct member *m)
378
4.78k
{
379
4.78k
    struct alias *a;
380
4.78k
    int ret = UNSPEC;
381
4.78k
    debug_decl(host_matches, SUDOERS_DEBUG_MATCH);
382
383
4.78k
    switch (m->type) {
384
209
  case ALL:
385
209
      ret = m->negated ? DENY : ALLOW;
386
209
      break;
387
0
  case NETGROUP:
388
0
      if (netgr_matches(parse_tree->nss, m->name, lhost, shost,
389
0
    def_netgroup_tuple ? pw->pw_name : NULL) == ALLOW)
390
0
    ret = m->negated ? DENY : ALLOW;
391
0
      break;
392
0
  case NTWKADDR:
393
0
      if (addr_matches(m->name) == ALLOW)
394
0
    ret = m->negated ? DENY : ALLOW;
395
0
      break;
396
108
  case ALIAS:
397
108
      a = alias_get(parse_tree, m->name, HOSTALIAS);
398
108
      if (a != NULL) {
399
    /* XXX */
400
0
    const int rc = hostlist_matches_int(parse_tree, pw, lhost,
401
0
        shost, &a->members);
402
0
    if (SPECIFIED(rc)) {
403
0
        if (m->negated) {
404
0
      ret = rc == ALLOW ? DENY : ALLOW;
405
0
        } else {
406
0
      ret = rc;
407
0
        }
408
0
    }
409
0
    alias_put(a);
410
0
    break;
411
0
      }
412
108
      FALLTHROUGH;
413
4.57k
  case WORD:
414
4.57k
      if (hostname_matches(shost, lhost, m->name) == ALLOW)
415
373
    ret = m->negated ? DENY : ALLOW;
416
4.57k
      break;
417
4.78k
    }
418
4.78k
    sudo_debug_printf(SUDO_DEBUG_DEBUG,
419
4.78k
  "host %s (%s) matches sudoers host %s%s: %s", lhost, shost,
420
4.78k
  m->negated ? "!" : "", m->name ? m->name : "ALL",
421
4.78k
  ret == ALLOW ? "ALLOW" : "DENY");
422
4.78k
    debug_return_int(ret);
423
4.78k
}
424
425
/*
426
 * Check for cmnd and args in a list of members.
427
 * Returns ALLOW, DENY or UNSPEC.
428
 */
429
int
430
cmndlist_matches(const struct sudoers_parse_tree *parse_tree,
431
    const struct member_list *list, const char *runchroot,
432
    struct cmnd_info *info)
433
312
{
434
312
    struct member *m;
435
312
    int matched;
436
312
    debug_decl(cmndlist_matches, SUDOERS_DEBUG_MATCH);
437
438
352
    TAILQ_FOREACH_REVERSE(m, list, member_list, entries) {
439
352
  matched = cmnd_matches(parse_tree, m, runchroot, info);
440
352
  if (SPECIFIED(matched))
441
296
      debug_return_int(matched);
442
352
    }
443
16
    debug_return_int(UNSPEC);
444
16
}
445
446
/*
447
 * Check cmnd and args.
448
 * Returns ALLOW, DENY or UNSPEC.
449
 */
450
int
451
cmnd_matches(const struct sudoers_parse_tree *parse_tree,
452
    const struct member *m, const char *runchroot, struct cmnd_info *info)
453
442
{
454
442
    struct alias *a;
455
442
    struct sudo_command *c;
456
442
    int rc, matched = UNSPEC;
457
442
    debug_decl(cmnd_matches, SUDOERS_DEBUG_MATCH);
458
459
442
    switch (m->type) {
460
284
  case ALL:
461
398
  case COMMAND:
462
398
      c = (struct sudo_command *)m->name;
463
398
      if (command_matches(parse_tree->ctx, c->cmnd, c->args, runchroot,
464
398
        info, &c->digests) == ALLOW)
465
298
    matched = m->negated ? DENY : ALLOW;
466
398
      break;
467
44
  case ALIAS:
468
44
      a = alias_get(parse_tree, m->name, CMNDALIAS);
469
44
      if (a != NULL) {
470
0
    rc = cmndlist_matches(parse_tree, &a->members, runchroot, info);
471
0
    if (SPECIFIED(rc)) {
472
0
        if (m->negated) {
473
0
      matched = rc == ALLOW ? DENY : ALLOW;
474
0
        } else {
475
0
      matched = rc;
476
0
        }
477
0
    }
478
0
    alias_put(a);
479
0
      }
480
44
      break;
481
442
    }
482
442
    debug_return_int(matched);
483
442
}
484
485
/*
486
 * Like cmnd_matches() but only matches against the ALL command.
487
 * Returns ALLOW, DENY or UNSPEC.
488
 */
489
int
490
cmnd_matches_all(const struct sudoers_parse_tree *parse_tree,
491
    const struct member *m, const char *runchroot, struct cmnd_info *info)
492
0
{
493
0
    const bool negated = m->negated;
494
0
    struct sudo_command *c;
495
0
    int matched = UNSPEC;
496
0
    struct alias *a;
497
0
    debug_decl(cmnd_matches_all, SUDOERS_DEBUG_MATCH);
498
499
0
    switch (m->type) {
500
0
  case ALL:
501
0
      c = (struct sudo_command *)m->name;
502
0
      if (command_matches(parse_tree->ctx, c->cmnd, c->args, runchroot,
503
0
        info, &c->digests) == ALLOW)
504
0
    matched = negated ? DENY : ALLOW;
505
0
      break;
506
0
  case ALIAS:
507
0
      a = alias_get(parse_tree, m->name, CMNDALIAS);
508
0
      if (a != NULL) {
509
0
    TAILQ_FOREACH_REVERSE(m, &a->members, member_list, entries) {
510
0
        matched = cmnd_matches_all(parse_tree, m, runchroot, info);
511
0
        if (SPECIFIED(matched)) {
512
0
      if (negated)
513
0
          matched = matched == ALLOW ? DENY : ALLOW;
514
0
      break;
515
0
        }
516
0
    }
517
0
    alias_put(a);
518
0
      }
519
0
      break;
520
0
    }
521
0
    debug_return_int(matched);
522
0
}
523
524
/*
525
 * Returns ALLOW if the hostname matches the pattern, else DENY
526
 */
527
int
528
hostname_matches(const char *shost, const char *lhost, const char *pattern)
529
4.57k
{
530
4.57k
    const char *host;
531
4.57k
    int ret;
532
4.57k
    debug_decl(hostname_matches, SUDOERS_DEBUG_MATCH);
533
534
4.57k
    host = strchr(pattern, '.') != NULL ? lhost : shost;
535
4.57k
    ret = DENY;
536
4.57k
    if (has_meta(pattern)) {
537
385
  if (fnmatch(pattern, host, FNM_CASEFOLD) == 0)
538
373
      ret = ALLOW;
539
4.18k
    } else {
540
4.18k
  if (strcasecmp(host, pattern) == 0)
541
0
      ret = ALLOW;
542
4.18k
    }
543
4.57k
    debug_return_int(ret);
544
4.57k
}
545
546
/*
547
 * Returns ALLOW if the user/uid from sudoers matches the specified user/uid,
548
 * else returns DENY.
549
 */
550
int
551
userpw_matches(const char *sudoers_user, const char *user, const struct passwd *pw)
552
2.22k
{
553
2.22k
    const char *errstr;
554
2.22k
    int ret = DENY;
555
2.22k
    uid_t uid;
556
2.22k
    debug_decl(userpw_matches, SUDOERS_DEBUG_MATCH);
557
558
2.22k
    if (pw != NULL && *sudoers_user == '#') {
559
1.18k
  uid = (uid_t) sudo_strtoid(sudoers_user + 1, &errstr);
560
1.18k
  if (errstr == NULL && uid == pw->pw_uid) {
561
290
      ret = ALLOW;
562
290
      goto done;
563
290
  }
564
1.18k
    }
565
1.93k
    if (def_case_insensitive_user) {
566
1.93k
  if (strcasecmp(sudoers_user, user) == 0)
567
44
      ret = ALLOW;
568
1.93k
    } else {
569
0
  if (strcmp(sudoers_user, user) == 0)
570
0
      ret = ALLOW;
571
0
    }
572
2.22k
done:
573
2.22k
    sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
574
2.22k
  "user %s matches sudoers user %s: %s",
575
2.22k
  user, sudoers_user, ret == ALLOW ? "ALLOW" : "DENY");
576
2.22k
    debug_return_int(ret);
577
2.22k
}
578
579
/*
580
 * Returns ALLOW if the group/gid from sudoers matches the specified group/gid,
581
 * else returns DENY.
582
 */
583
int
584
group_matches(const char *sudoers_group, const struct group *gr)
585
0
{
586
0
    const char *errstr;
587
0
    int ret = DENY;
588
0
    gid_t gid;
589
0
    debug_decl(group_matches, SUDOERS_DEBUG_MATCH);
590
591
0
    if (*sudoers_group == '#') {
592
0
  gid = (gid_t) sudo_strtoid(sudoers_group + 1, &errstr);
593
0
  if (errstr == NULL && gid == gr->gr_gid) {
594
0
      ret = ALLOW;
595
0
      goto done;
596
0
  }
597
0
    }
598
0
    if (def_case_insensitive_group) {
599
0
  if (strcasecmp(sudoers_group, gr->gr_name) == 0)
600
0
      ret = ALLOW;
601
0
    } else {
602
0
  if (strcmp(sudoers_group, gr->gr_name) == 0)
603
0
      ret = ALLOW;
604
0
    }
605
0
done:
606
0
    sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
607
0
  "group %s matches sudoers group %s: %s",
608
0
  gr->gr_name, sudoers_group, ret == ALLOW ? "ALLOW" : "DENY");
609
0
    debug_return_int(ret);
610
0
}
611
612
/*
613
 * Returns true if the given user belongs to the named group,
614
 * else returns false.
615
 */
616
int
617
usergr_matches(const char *group, const char *user, const struct passwd *pw)
618
15.3k
{
619
15.3k
    struct passwd *pw0 = NULL;
620
15.3k
    int ret = DENY;
621
15.3k
    debug_decl(usergr_matches, SUDOERS_DEBUG_MATCH);
622
623
    /* Make sure we have a valid usergroup, sudo style */
624
15.3k
    if (*group++ != '%') {
625
0
  sudo_debug_printf(SUDO_DEBUG_DIAG, "user group %s has no leading '%%'",
626
0
      group);
627
0
  goto done;
628
0
    }
629
630
    /* Query group plugin for %:name groups. */
631
15.3k
    if (*group == ':' && def_group_plugin) {
632
0
  if (group_plugin_query(user, group + 1, pw) == true)
633
0
      ret = ALLOW;
634
0
  goto done;
635
0
    }
636
637
    /* Look up user's primary gid in the passwd file. */
638
15.3k
    if (pw == NULL) {
639
0
  if ((pw0 = sudo_getpwnam(user)) == NULL) {
640
0
      sudo_debug_printf(SUDO_DEBUG_DIAG, "unable to find %s in passwd db",
641
0
    user);
642
0
      goto done;
643
0
  }
644
0
  pw = pw0;
645
0
    }
646
647
15.3k
    if (user_in_group(pw, group)) {
648
4.64k
  ret = ALLOW;
649
4.64k
  goto done;
650
4.64k
    }
651
652
    /* Query the group plugin for Unix groups too? */
653
10.7k
    if (def_group_plugin && def_always_query_group_plugin) {
654
0
  if (group_plugin_query(user, group, pw) == true) {
655
0
      ret = ALLOW;
656
0
      goto done;
657
0
  }
658
0
    }
659
660
15.3k
done:
661
15.3k
    if (pw0 != NULL)
662
0
  sudo_pw_delref(pw0);
663
664
15.3k
    sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
665
15.3k
  "user %s matches group %s: %s", user, group,
666
15.3k
  ret == ALLOW ? "ALLOW" : "DENY");
667
15.3k
    debug_return_int(ret);
668
15.3k
}
669
670
#if defined(HAVE_GETDOMAINNAME) || defined(SI_SRPC_DOMAIN)
671
/*
672
 * Check the domain for invalid characters.
673
 * Linux getdomainname(2) returns (none) if no domain is set.
674
 */
675
static bool
676
valid_domain(const char *domain)
677
0
{
678
0
    const char *cp;
679
0
    debug_decl(valid_domain, SUDOERS_DEBUG_MATCH);
680
681
0
    for (cp = domain; *cp != '\0'; cp++) {
682
  /* Check for illegal characters, Linux may use "(none)". */
683
0
  if (*cp == '(' || *cp == ')' || *cp == ',' || *cp == ' ')
684
0
      break;
685
0
    }
686
0
    if (cp == domain || *cp != '\0')
687
0
  debug_return_bool(false);
688
0
    debug_return_bool(true);
689
0
}
690
691
/*
692
 * Get NIS-style domain name and copy from static storage or NULL if none.
693
 */
694
const char *
695
sudo_getdomainname(void)
696
0
{
697
0
    static char *domain;
698
0
    static bool initialized;
699
0
    debug_decl(sudo_getdomainname, SUDOERS_DEBUG_MATCH);
700
701
0
    if (!initialized) {
702
0
  const size_t host_name_max = sudo_host_name_max();
703
0
  int rc;
704
705
0
  domain = malloc(host_name_max + 1);
706
0
  if (domain != NULL) {
707
0
      domain[0] = '\0';
708
# ifdef SI_SRPC_DOMAIN
709
      rc = sysinfo(SI_SRPC_DOMAIN, domain, host_name_max + 1);
710
# else
711
0
      rc = getdomainname(domain, host_name_max + 1);
712
0
# endif
713
0
      if (rc == -1 || !valid_domain(domain)) {
714
    /* Error or invalid domain name. */
715
0
    free(domain);
716
0
    domain = NULL;
717
0
      }
718
0
  } else {
719
      /* XXX - want to pass error back to caller */
720
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
721
0
    "unable to allocate memory");
722
0
  }
723
0
  initialized = true;
724
0
    }
725
0
    debug_return_str(domain);
726
0
}
727
#else
728
const char *
729
sudo_getdomainname(void)
730
{
731
    debug_decl(sudo_getdomainname, SUDOERS_DEBUG_MATCH);
732
    debug_return_ptr(NULL);
733
}
734
#endif /* HAVE_GETDOMAINNAME || SI_SRPC_DOMAIN */
735
736
/*
737
 * Returns ALLOW if "host" and "user" belong to the netgroup "netgr",
738
 * else return DENY.  Either of "lhost", "shost" or "user" may be NULL
739
 * in which case that argument is not checked...
740
 */
741
int
742
netgr_matches(const struct sudo_nss *nss, const char *netgr,
743
    const char *lhost, const char *shost, const char *user)
744
0
{
745
0
    const char *domain;
746
0
    int ret = DENY;
747
0
    debug_decl(netgr_matches, SUDOERS_DEBUG_MATCH);
748
749
0
    if (!def_use_netgroups) {
750
0
  sudo_debug_printf(SUDO_DEBUG_INFO, "netgroups are disabled");
751
0
  debug_return_int(DENY);
752
0
    }
753
754
    /* make sure we have a valid netgroup, sudo style */
755
0
    if (*netgr++ != '+') {
756
0
  sudo_debug_printf(SUDO_DEBUG_DIAG, "netgroup %s has no leading '+'",
757
0
      netgr);
758
0
  debug_return_int(DENY);
759
0
    }
760
761
    /* get the domain name (if any) */
762
0
    domain = sudo_getdomainname();
763
764
    /* Use nss-specific innetgr() function if available. */
765
0
    if (nss != NULL && nss->innetgr != NULL) {
766
0
  switch (nss->innetgr(nss, netgr, lhost, user, domain)) {
767
0
  case 0:
768
0
      if (lhost != shost) {
769
0
    if (nss->innetgr(nss, netgr, shost, user, domain) == 1)
770
0
        ret = ALLOW;
771
0
      }
772
0
      goto done;
773
0
  case 1:
774
0
      ret = ALLOW;
775
0
      goto done;
776
0
  default:
777
      /* Not supported, use system innetgr(3). */
778
0
      break;
779
0
  }
780
0
    }
781
782
0
#ifdef HAVE_INNETGR
783
    /* Use system innetgr() function. */
784
0
    if (innetgr(netgr, lhost, user, domain) == 1) {
785
0
  ret = ALLOW;
786
0
    } else if (lhost != shost) {
787
0
  if (innetgr(netgr, shost, user, domain) == 1)
788
0
      ret = ALLOW;
789
0
    }
790
#else
791
    sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
792
  "%s: no system netgroup support", __func__);
793
#endif /* HAVE_INNETGR */
794
795
0
done:
796
0
    sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
797
0
  "netgroup %s matches (%s|%s, %s, %s): %s", netgr, lhost ? lhost : "",
798
0
  shost ? shost : "", user ? user : "", domain ? domain : "",
799
0
  ret == ALLOW ? "ALLOW" : "DENY");
800
801
0
    debug_return_int(ret);
802
0
}