Coverage Report

Created: 2026-10-10 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/sudoers.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 1993-1996, 1998-2023 Todd C. Miller <Todd.Miller@sudo.ws>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 *
18
 * Sponsored in part by the Defense Advanced Research Projects
19
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21
 */
22
23
#ifdef __TANDEM
24
# include <floss.h>
25
#endif
26
27
#include <config.h>
28
29
#include <sys/types.h>
30
#include <sys/resource.h>
31
#include <sys/stat.h>
32
#include <sys/socket.h>
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <unistd.h>
37
#include <pwd.h>
38
#include <errno.h>
39
#include <fcntl.h>
40
#include <grp.h>
41
#include <netdb.h>
42
#ifdef HAVE_LOGIN_CAP_H
43
# include <login_cap.h>
44
# ifndef LOGIN_DEFROOTCLASS
45
#  define LOGIN_DEFROOTCLASS  "daemon"
46
# endif
47
# ifndef LOGIN_SETENV
48
#  define LOGIN_SETENV  0
49
# endif
50
#endif
51
#ifdef HAVE_SELINUX
52
# include <selinux/selinux.h>
53
#endif
54
#include <ctype.h>
55
#ifndef HAVE_GETADDRINFO
56
# include <compat/getaddrinfo.h>
57
#endif
58
59
#include <sudoers.h>
60
#include <timestamp.h>
61
#include <sudo_iolog.h>
62
63
/*
64
 * Prototypes
65
 */
66
static int set_cmnd(struct sudoers_context *ctx);
67
static bool init_vars(struct sudoers_context *ctx, char * const *);
68
static bool set_loginclass(struct sudoers_context *);
69
static bool set_runaspw(struct sudoers_context *ctx, const char *, bool);
70
static bool set_runasgr(struct sudoers_context *ctx, const char *, bool);
71
72
/*
73
 * Globals
74
 */
75
static char *prev_user;
76
static struct sudoers_context sudoers_ctx = SUDOERS_CONTEXT_INITIALIZER;
77
static struct sudo_nss_list *snl;
78
static bool unknown_runas_uid;
79
static bool unknown_runas_gid;
80
static int cmnd_status = NOT_FOUND_ERROR;
81
static struct defaults_list initial_defaults = TAILQ_HEAD_INITIALIZER(initial_defaults);
82
83
#ifdef __linux__
84
static struct rlimit nproclimit;
85
#endif
86
87
#ifdef SUDOERS_LOG_CLIENT
88
5.71k
# define remote_iologs  (!SLIST_EMPTY(&def_log_servers))
89
#else
90
# define remote_iologs  0
91
#endif
92
93
/*
94
 * Unlimit the number of processes since Linux's setuid() will
95
 * apply resource limits when changing uid and return EAGAIN if
96
 * nproc would be exceeded by the uid switch.
97
 */
98
static void
99
unlimit_nproc(void)
100
26.9k
{
101
26.9k
#ifdef __linux__
102
26.9k
    struct rlimit rl;
103
26.9k
    debug_decl(unlimit_nproc, SUDOERS_DEBUG_UTIL);
104
105
26.9k
    if (getrlimit(RLIMIT_NPROC, &nproclimit) != 0)
106
0
      sudo_warn("getrlimit(RLIMIT_NPROC)");
107
26.9k
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
108
26.9k
    if (setrlimit(RLIMIT_NPROC, &rl) != 0) {
109
0
  rl.rlim_cur = rl.rlim_max = nproclimit.rlim_max;
110
0
  if (setrlimit(RLIMIT_NPROC, &rl) != 0)
111
0
      sudo_warn("setrlimit(RLIMIT_NPROC)");
112
0
    }
113
26.9k
    debug_return;
114
26.9k
#endif /* __linux__ */
115
26.9k
}
116
117
/*
118
 * Restore saved value of RLIMIT_NPROC.
119
 */
120
static void
121
restore_nproc(void)
122
26.9k
{
123
26.9k
#ifdef __linux__
124
26.9k
    debug_decl(restore_nproc, SUDOERS_DEBUG_UTIL);
125
126
26.9k
    if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0)
127
0
  sudo_warn("setrlimit(RLIMIT_NPROC)");
128
129
26.9k
    debug_return;
130
26.9k
#endif /* __linux__ */
131
26.9k
}
132
133
/*
134
 * Re-initialize Defaults settings.
135
 * We do not warn, log or send mail for errors when reinitializing,
136
 * this would have already been done the first time through.
137
 */
138
static bool
139
sudoers_reinit_defaults(struct sudoers_context *ctx)
140
5.85k
{
141
5.85k
    struct sudo_nss *nss, *nss_next;
142
5.85k
    sudoers_logger_t logger = sudoers_error_hook;
143
5.85k
    debug_decl(sudoers_reinit_defaults, SUDOERS_DEBUG_PLUGIN);
144
145
5.85k
    if (!init_defaults()) {
146
0
  sudo_warnx("%s", U_("unable to initialize sudoers default values"));
147
0
  debug_return_bool(false);
148
0
    }
149
150
    /* It should not be possible for the initial defaults to fail to apply. */
151
5.85k
    if (!update_defaults(ctx, NULL, &initial_defaults,
152
5.85k
      SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false))
153
0
  debug_return_bool(false);
154
155
    /* Disable error logging while re-processing defaults. */
156
5.85k
    sudoers_error_hook = NULL;
157
158
5.85k
    TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
159
  /* Missing/invalid defaults is not a fatal error. */
160
5.85k
  if (nss->getdefs(ctx, nss) != -1) {
161
5.85k
      (void)update_defaults(ctx, nss->parse_tree, NULL,
162
5.85k
    SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, true);
163
5.85k
  }
164
5.85k
    }
165
166
    /* Restore error logging. */
167
5.85k
    sudoers_error_hook = logger;
168
169
    /* No need to check the admin flag file multiple times. */
170
5.85k
    if (ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
171
0
  free(def_admin_flag);
172
0
  def_admin_flag = NULL;
173
0
    }
174
175
5.85k
    debug_return_bool(true);
176
5.85k
}
177
178
static bool sudoers_initialized;
179
180
/*
181
 * Initialize sudoers data structures and parse sudoers sources.
182
 * Returns 1 on success and -1 on error.
183
 */
184
int
185
sudoers_init(void *info, sudoers_logger_t logger, char * const envp[])
186
24.8k
{
187
24.8k
    struct sudo_nss *nss, *nss_next;
188
24.8k
    int oldlocale, sources = 0;
189
24.8k
    static int ret;
190
24.8k
    debug_decl(sudoers_init, SUDOERS_DEBUG_PLUGIN);
191
192
    /* Only attempt to initialize once. */
193
24.8k
    if (sudoers_initialized)
194
0
  debug_return_int(ret);
195
24.8k
    sudoers_initialized = true;
196
24.8k
    ret = -1;
197
198
24.8k
    bindtextdomain("sudoers", LOCALEDIR);
199
200
    /* Hook up logging function for parse errors. */
201
24.8k
    sudoers_error_hook = logger;
202
203
    /* Register fatal/fatalx callback. */
204
24.8k
    sudo_fatal_callback_register(sudoers_cleanup);
205
206
    /* Initialize environment functions (including replacements). */
207
24.8k
    if (!env_init(envp))
208
0
  debug_return_int(-1);
209
210
    /* Setup defaults data structures. */
211
24.8k
    if (!init_defaults()) {
212
0
  sudo_warnx("%s", U_("unable to initialize sudoers default values"));
213
0
  debug_return_int(-1);
214
0
    }
215
216
    /* Parse info from front-end. */
217
24.8k
    sudoers_ctx.mode = sudoers_policy_deserialize_info(&sudoers_ctx, info,
218
24.8k
  &initial_defaults);
219
24.8k
    if (ISSET(sudoers_ctx.mode, MODE_ERROR))
220
4.77k
  debug_return_int(-1);
221
222
20.0k
    if (!init_vars(&sudoers_ctx, envp))
223
326
  debug_return_int(-1);
224
225
    /* Parse nsswitch.conf for sudoers order. */
226
19.7k
    snl = sudo_read_nss();
227
228
    /* LDAP or NSS may modify the euid so we need to be root for the open. */
229
19.7k
    if (!set_perms(NULL, PERM_ROOT))
230
0
  debug_return_int(-1);
231
232
    /* Use the C locale unless another is specified in sudoers. */
233
19.7k
    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
234
19.7k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
235
236
    /* Update defaults set by front-end. */
237
19.7k
    if (!update_defaults(&sudoers_ctx, NULL, &initial_defaults,
238
19.7k
      SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false)) {
239
0
  goto cleanup;
240
0
    }
241
242
    /* Open and parse sudoers, set global defaults.  */
243
19.7k
    TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
244
19.7k
  if (nss->open(&sudoers_ctx, nss) == -1 || (nss->parse_tree = nss->parse(&sudoers_ctx, nss)) == NULL) {
245
0
      TAILQ_REMOVE(snl, nss, entries);
246
0
      continue;
247
0
  }
248
19.7k
  sources++;
249
250
  /* Missing/invalid defaults is not a fatal error. */
251
19.7k
  if (nss->getdefs(&sudoers_ctx, nss) == -1) {
252
0
      log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR|SLOG_NO_STDERR,
253
0
    N_("unable to get defaults from %s"), nss->source);
254
19.7k
  } else {
255
19.7k
      (void)update_defaults(&sudoers_ctx, nss->parse_tree, NULL,
256
19.7k
    SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false);
257
19.7k
  }
258
19.7k
    }
259
19.7k
    if (sources == 0) {
260
  /* Display an extra warning if there are multiple sudoers sources. */
261
0
  if (TAILQ_FIRST(snl) != TAILQ_LAST(snl, sudo_nss_list))
262
0
      sudo_warnx("%s", U_("no valid sudoers sources found, quitting"));
263
0
  goto cleanup;
264
0
    }
265
266
    /* Set login class if applicable (after sudoers is parsed). */
267
19.7k
    if (set_loginclass(&sudoers_ctx))
268
19.7k
  ret = true;
269
270
19.7k
cleanup:
271
19.7k
    mail_parse_errors(&sudoers_ctx);
272
273
19.7k
    if (!restore_perms())
274
0
  ret = -1;
275
276
    /* Restore user's locale. */
277
19.7k
    sudo_warn_set_locale_func(NULL);
278
19.7k
    sudoers_setlocale(oldlocale, NULL);
279
280
19.7k
    debug_return_int(ret);
281
19.7k
}
282
283
/*
284
 * Expand I/O log dir and file into a full path.
285
 * Returns the full I/O log path prefixed with "iolog_path=".
286
 * Sets ctx->iolog_file and ctx->iolog_path as a side effect.
287
 */
288
static char *
289
format_iolog_path(struct sudoers_context *ctx)
290
2.69k
{
291
2.69k
    char dir[PATH_MAX], file[PATH_MAX];
292
2.69k
    char *iolog_path = NULL;
293
2.69k
    int oldlocale;
294
2.69k
    bool ok;
295
2.69k
    debug_decl(format_iolog_path, SUDOERS_DEBUG_PLUGIN);
296
297
    /* Use sudoers locale for strftime() */
298
2.69k
    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
299
2.69k
    ok = expand_iolog_path(def_iolog_dir, dir, sizeof(dir),
300
2.69k
  &sudoers_iolog_path_escapes[1], ctx);
301
2.69k
    if (ok) {
302
2.69k
  ctx->iolog_dir = dir;
303
2.69k
  ok = expand_iolog_path(def_iolog_file, file, sizeof(file),
304
2.69k
      &sudoers_iolog_path_escapes[0], ctx);
305
2.69k
  ctx->iolog_dir = NULL;
306
2.69k
    }
307
2.69k
    sudoers_setlocale(oldlocale, NULL);
308
2.69k
    if (!ok)
309
0
  goto done;
310
311
2.69k
    if (asprintf(&iolog_path, "iolog_path=%s/%s", dir, file) == -1) {
312
0
  iolog_path = NULL;
313
0
  goto done;
314
0
    }
315
316
    /* Stash pointer to the I/O log for the event log. */
317
2.69k
    ctx->iolog_path = iolog_path + sizeof("iolog_path=") - 1;
318
2.69k
    ctx->iolog_file = ctx->iolog_path + 1 + strlen(dir);
319
320
2.69k
done:
321
2.69k
    debug_return_str(iolog_path);
322
2.69k
}
323
324
static void
325
cb_lookup(const struct sudoers_parse_tree *parse_tree,
326
    const struct userspec *us, int user_match, const struct privilege *priv,
327
    int host_match, const struct cmndspec *cs, int date_match, int runas_match,
328
    int cmnd_match, void *closure)
329
0
{
330
0
    struct sudoers_match_info *info = closure;
331
332
0
    if (cmnd_match != UNSPEC) {
333
0
  info->us = us;
334
0
  info->priv = priv;
335
0
  info->cs = cs;
336
0
    }
337
0
}
338
339
/*
340
 * Find the command, perform a sudoers lookup, ask for a password as
341
 * needed, and perform post-lookup checks.  Logs success/failure.
342
 * This is used by the check, list and validate plugin methods.
343
 *
344
 * Returns true if allowed, false if denied, -1 on error and
345
 * -2 for usage error.
346
 */
347
static int
348
sudoers_check_common(struct sudoers_context *ctx, int pwflag)
349
26.4k
{
350
26.4k
    struct sudoers_match_info match_info = { NULL };
351
26.4k
    int oldlocale, ret = -1;
352
26.4k
    unsigned int validated;
353
26.4k
    time_t now;
354
26.4k
    debug_decl(sudoers_check_common, SUDOERS_DEBUG_PLUGIN);
355
356
    /* The user may only specify a host for "sudo -l". */
357
26.4k
    if (!ISSET(ctx->mode, MODE_LIST|MODE_CHECK)) {
358
16.5k
  if (strcmp(ctx->runas.host, ctx->user.host) != 0) {
359
0
      log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
360
0
    N_("user not allowed to set remote host for command"));
361
0
      sudo_warnx("%s",
362
0
    U_("a remote host may only be specified when listing privileges."));
363
0
      ret = false;
364
0
      goto done;
365
0
  }
366
16.5k
    }
367
368
    /* If given the -P option, set the "preserve_groups" flag. */
369
26.4k
    if (ISSET(ctx->mode, MODE_PRESERVE_GROUPS))
370
67
  def_preserve_groups = true;
371
372
    /* Find command in path and apply per-command Defaults. */
373
26.4k
    cmnd_status = set_cmnd(ctx);
374
26.4k
    if (cmnd_status == NOT_FOUND_ERROR)
375
0
  goto done;
376
377
    /* Is root even allowed to run sudo? */
378
26.4k
    if (ctx->user.uid == 0 && !def_root_sudo) {
379
  /* Not an audit event (should it be?). */
380
0
  sudo_warnx("%s",
381
0
      U_("sudoers specifies that root is not allowed to sudo"));
382
0
  ret = false;
383
0
  goto done;
384
0
    }
385
386
    /* Check for -C overriding def_closefrom. */
387
26.4k
    if (ctx->user.closefrom >= 0 && ctx->user.closefrom != def_closefrom) {
388
1.15k
  if (!def_closefrom_override) {
389
1.15k
      log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
390
1.15k
    N_("user not allowed to override closefrom limit"));
391
1.15k
      sudo_warnx("%s", U_("you are not permitted to use the -C option"));
392
1.15k
      goto bad;
393
1.15k
  }
394
0
  def_closefrom = ctx->user.closefrom;
395
0
    }
396
397
    /*
398
     * Check sudoers sources, using the locale specified in sudoers.
399
     */
400
25.2k
    time(&now);
401
25.2k
    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
402
25.2k
    validated = sudoers_lookup(snl, ctx, now, cb_lookup, &match_info,
403
25.2k
  &cmnd_status, pwflag);
404
25.2k
    sudoers_setlocale(oldlocale, NULL);
405
25.2k
    if (ISSET(validated, VALIDATE_ERROR)) {
406
  /* The lookup function should have printed an error. */
407
0
  goto done;
408
0
    }
409
410
25.2k
    if (match_info.us != NULL && match_info.us->file != NULL) {
411
0
  free(ctx->source);
412
0
  if (match_info.us->line != 0) {
413
0
      if (asprintf(&ctx->source, "%s:%d:%d", match_info.us->file,
414
0
        match_info.us->line, match_info.us->column) == -1)
415
0
    ctx->source = NULL;
416
0
  } else {
417
0
      ctx->source = strdup(match_info.us->file);
418
0
  }
419
0
  if (ctx->source == NULL) {
420
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
421
0
      goto done;
422
0
  }
423
0
    }
424
425
25.2k
    if (ctx->runas.cmnd == NULL) {
426
25.2k
  if ((ctx->runas.cmnd = strdup(ctx->user.cmnd)) == NULL) {
427
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
428
0
      goto done;
429
0
  }
430
25.2k
    }
431
432
    /* Defer uid/gid checks until after defaults have been updated. */
433
25.2k
    if (unknown_runas_uid && !def_runas_allow_unknown_id) {
434
99
  log_warningx(ctx, SLOG_AUDIT, N_("unknown user %s"),
435
99
      ctx->runas.pw->pw_name);
436
99
  goto done;
437
99
    }
438
25.1k
    if (ctx->runas.gr != NULL) {
439
358
  if (unknown_runas_gid && !def_runas_allow_unknown_id) {
440
38
      log_warningx(ctx, SLOG_AUDIT, N_("unknown group %s"),
441
38
    ctx->runas.gr->gr_name);
442
38
      goto done;
443
38
  }
444
358
    }
445
446
    /* If no command line args and "shell_noargs" is not set, error out. */
447
25.1k
    if (ISSET(ctx->mode, MODE_IMPLIED_SHELL) && !def_shell_noargs) {
448
  /* Not an audit event. */
449
35
  ret = -2; /* usage error */
450
35
  goto done;
451
35
    }
452
453
    /* Bail if a tty is required and we don't have one. */
454
25.1k
    if (def_requiretty && !sudoers_tty_present(ctx)) {
455
0
  log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT, N_("no tty"));
456
0
  sudo_warnx("%s", U_("sorry, you must have a tty to run sudo"));
457
0
  goto bad;
458
0
    }
459
460
    /* Check runas user's shell if running (or checking) a command. */
461
25.1k
    if (ISSET(ctx->mode, MODE_RUN|MODE_CHECK)) {
462
13.5k
  if (!user_shell_valid(ctx->runas.pw)) {
463
0
      log_warningx(ctx, SLOG_RAW_MSG|SLOG_AUDIT,
464
0
    N_("invalid shell for user %s: %s"),
465
0
    ctx->runas.pw->pw_name, ctx->runas.pw->pw_shell);
466
0
      goto bad;
467
0
  }
468
13.5k
    }
469
470
    /*
471
     * We don't reset the environment for sudoedit or if the user
472
     * specified the -E command line flag and they have setenv privs.
473
     */
474
25.1k
    if (ISSET(ctx->mode, MODE_EDIT) ||
475
22.9k
  (ISSET(ctx->mode, MODE_PRESERVE_ENV) && def_setenv))
476
2.16k
  def_env_reset = false;
477
478
    /* Build a new environment that avoids any nasty bits. */
479
25.1k
    if (!rebuild_env(ctx))
480
0
  goto bad;
481
482
    /* Require a password if sudoers says so.  */
483
25.1k
    switch (check_user(ctx, validated, ctx->mode)) {
484
25.1k
    case AUTH_SUCCESS:
485
  /* user authenticated successfully. */
486
25.1k
  break;
487
0
    case AUTH_FAILURE:
488
  /* Note: log_denial() calls audit for us. */
489
0
  if (!ISSET(validated, VALIDATE_SUCCESS)) {
490
      /* Only display a denial message if no password was read. */
491
0
      if (!log_denial(ctx, validated, def_passwd_tries <= 0))
492
0
    goto done;
493
0
  }
494
0
  goto bad;
495
0
    default:
496
  /* some other error, ret is -1. */
497
0
  goto done;
498
25.1k
    }
499
500
    /* Check whether ctx->runas.chroot is permitted (if specified). */
501
25.1k
    switch (check_user_runchroot(ctx->runas.chroot)) {
502
25.1k
    case true:
503
25.1k
  break;
504
0
    case false:
505
0
  log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
506
0
      N_("user not allowed to change root directory to %s"),
507
0
      ctx->runas.chroot);
508
0
  sudo_warnx(U_("you are not permitted to use the -R option with %s"),
509
0
      ctx->user.cmnd);
510
0
  goto bad;
511
0
    default:
512
0
  goto done;
513
25.1k
    }
514
515
    /* Check whether ctx->runas.cwd is permitted (if specified). */
516
25.1k
    switch (check_user_runcwd(ctx->runas.cwd)) {
517
25.1k
    case true:
518
25.1k
  break;
519
0
    case false:
520
0
  log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
521
0
      N_("user not allowed to change directory to %s"), ctx->runas.cwd);
522
0
  sudo_warnx(U_("you are not permitted to use the -D option with %s"),
523
0
      ctx->user.cmnd);
524
0
  goto bad;
525
0
    default:
526
0
  goto done;
527
25.1k
    }
528
529
    /* If run as root with SUDO_USER set, set ctx->user.pw to that user. */
530
    /* XXX - causes confusion when root is not listed in sudoers */
531
25.1k
    if (ISSET(ctx->mode, MODE_RUN|MODE_EDIT) && prev_user != NULL) {
532
0
  if (ctx->user.uid == 0 && strcmp(prev_user, "root") != 0) {
533
0
      struct passwd *pw;
534
535
0
      if ((pw = sudo_getpwnam(prev_user)) != NULL) {
536
0
        if (ctx->user.pw != NULL)
537
0
      sudo_pw_delref(ctx->user.pw);
538
0
        ctx->user.pw = pw;
539
0
      }
540
0
  }
541
0
    }
542
543
    /* If the user was not allowed to run the command we are done. */
544
25.1k
    if (!ISSET(validated, VALIDATE_SUCCESS)) {
545
  /* Note: log_failure() calls audit for us. */
546
0
  if (!log_failure(ctx, validated, cmnd_status))
547
0
      goto done;
548
0
  goto bad;
549
0
    }
550
551
    /*
552
     * Check if the user is trying to run a setid binary in intercept mode.
553
     * For the DSO intercept_type, we reject attempts to run setid binaries
554
     * by default since the dynamic loader will clear LD_PRELOAD, defeating
555
     * intercept.
556
     */
557
25.1k
    if (def_intercept || ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
558
0
  if (!def_intercept_allow_setid && ctx->user.cmnd_stat != NULL) {
559
0
      if (ISSET(ctx->user.cmnd_stat->st_mode, S_ISUID|S_ISGID)) {
560
0
    CLR(validated, VALIDATE_SUCCESS);
561
0
    if (!log_denial(ctx, validated|FLAG_INTERCEPT_SETID, true))
562
0
        goto done;
563
0
    goto bad;
564
0
      }
565
0
  }
566
0
    }
567
568
    /* Create Ubuntu-style dot file to indicate sudo was successful. */
569
25.1k
    if (create_admin_success_flag(ctx) == -1)
570
0
  goto done;
571
572
    /* Finally tell the user if the command did not exist. */
573
25.1k
    if (cmnd_status == NOT_FOUND_DOT) {
574
3.24k
  audit_failure(ctx, ctx->runas.argv, N_("command in current directory"));
575
3.24k
  sudo_warnx(U_("ignoring \"%s\" found in '.'\nUse \"sudo ./%s\" if this is the \"%s\" you wish to run."), ctx->user.cmnd, ctx->user.cmnd, ctx->user.cmnd);
576
3.24k
  goto bad;
577
21.8k
    } else if (cmnd_status == NOT_FOUND) {
578
1.75k
  if (ISSET(ctx->mode, MODE_CHECK)) {
579
0
      audit_failure(ctx, ctx->runas.argv, N_("%s: command not found"),
580
0
    ctx->runas.argv[1]);
581
0
      sudo_warnx(U_("%s: command not found"), ctx->runas.argv[1]);
582
1.75k
  } else {
583
1.75k
      audit_failure(ctx, ctx->runas.argv, N_("%s: command not found"),
584
1.75k
    ctx->user.cmnd);
585
1.75k
      sudo_warnx(U_("%s: command not found"), ctx->user.cmnd);
586
1.75k
      if (strncmp(ctx->user.cmnd, "cd", 2) == 0 && (ctx->user.cmnd[2] == '\0' ||
587
0
        isblank((unsigned char)ctx->user.cmnd[2]))) {
588
0
    sudo_warnx("%s",
589
0
        U_("\"cd\" is a shell built-in command, it cannot be run directly."));
590
0
    sudo_warnx("%s",
591
0
        U_("the -s option may be used to run a privileged shell."));
592
0
    sudo_warnx("%s",
593
0
        U_("the -D option may be used to run a command in a specific directory."));
594
0
      }
595
1.75k
  }
596
1.75k
  goto bad;
597
1.75k
    }
598
599
    /* If user specified a timeout make sure sudoers allows it. */
600
20.1k
    if (!def_user_command_timeouts && ctx->user.timeout > 0) {
601
701
  log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
602
701
      N_("user not allowed to set a command timeout"));
603
701
  sudo_warnx("%s",
604
701
      U_("sorry, you are not allowed set a command timeout"));
605
701
  goto bad;
606
701
    }
607
608
    /* If user specified env vars make sure sudoers allows it. */
609
19.4k
    if (ISSET(ctx->mode, MODE_RUN) && !def_setenv) {
610
5.72k
  if (ISSET(ctx->mode, MODE_PRESERVE_ENV)) {
611
24
      log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
612
24
    N_("user not allowed to preserve the environment"));
613
24
      sudo_warnx("%s",
614
24
    U_("sorry, you are not allowed to preserve the environment"));
615
24
      goto bad;
616
5.70k
  } else {
617
5.70k
      if (!validate_env_vars(ctx, ctx->user.env_add))
618
1.59k
    goto bad;
619
5.70k
  }
620
5.72k
    }
621
622
17.7k
    ret = true;
623
17.7k
    goto done;
624
625
8.47k
bad:
626
8.47k
    ret = false;
627
26.4k
done:
628
26.4k
    debug_return_int(ret);
629
26.4k
}
630
631
/*
632
 * Canonicalize sudoedit file names if necessary before policy match.
633
 * This helps avoid issues with wildcard and regex matching.
634
 * We don't currently support -D or CWD for sudoedit so this uses
635
 * the user's current working directory.
636
 */
637
static bool
638
canonicalize_edit_args(int argc, char *argv[])
639
1.62k
{
640
1.62k
    int i;
641
1.62k
    debug_decl(canonicalize_edit_args, SUDOERS_DEBUG_PLUGIN);
642
643
917k
    for (i = 1; i < argc; i++) {
644
916k
  char *file = argv[i];
645
646
  /*
647
   * Canonicalize file names containing "..".
648
   * We don't canonicalize unconditionally since it could
649
   * affect sudoers path matching.
650
   */
651
916k
  if (sudo_contains_dot_dot(file)) {
652
2.32k
      char *cp, dir[PATH_MAX], resbuf[PATH_MAX];
653
2.32k
      const char *base, *resolved;
654
655
      /*
656
       * Only canonicalize the parent dir, we may be creating a new file.
657
       */
658
2.32k
      base = sudo_basename(file);
659
2.32k
      if (base == file)
660
0
    continue;
661
2.32k
      if (strlcpy(dir, file, sizeof(dir)) >= sizeof(dir)) {
662
39
    errno = ENAMETOOLONG;
663
39
    sudo_warn("%s", file);
664
39
    goto bad;
665
39
      }
666
2.28k
      dir[base - file - 1] = '\0';
667
668
      /* Need to be runas user for realpath(). */
669
2.28k
      if (!set_perms(&sudoers_ctx, PERM_RUNAS))
670
0
    goto bad;
671
672
      /* Older realpath() doesn't support passing a NULL buffer. */
673
2.28k
      resolved = realpath(dir, resbuf);
674
675
2.28k
      if (!restore_perms())
676
0
    goto bad;
677
678
2.28k
      if (resolved != NULL) {
679
0
    if (asprintf(&cp, "%s/%s", resolved, base) == -1)
680
0
        goto oom;
681
0
    file = cp;
682
0
      }
683
913k
  } else {
684
      /* Fully-qualify file to be edited if needed. */
685
913k
      if (file[0] != '/') {
686
913k
    char *cp;
687
688
913k
    if (sudoers_ctx.user.cwd == NULL) {
689
480
        sudo_warnx("%s",
690
480
      U_("unable to get current working directory"));
691
480
        goto bad;
692
480
    }
693
913k
    if (asprintf(&cp, "%s/%s", sudoers_ctx.user.cwd, argv[i]) == -1)
694
0
        goto oom;
695
913k
    file = cp;
696
913k
      }
697
913k
  }
698
915k
  if (file != argv[i]) {
699
913k
      sudoers_gc_add(GC_PTR, file);
700
913k
      argv[i] = file;
701
913k
  }
702
703
  /* Remove consecutive '/' and "/./" and "/../" path elements. */
704
915k
  rationalize_path(file, true);
705
915k
    }
706
1.10k
    debug_return_bool(true);
707
708
0
oom:
709
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
710
519
bad:
711
519
    debug_return_bool(false);
712
519
}
713
714
static bool need_reinit;
715
716
/*
717
 * Check whether the user is allowed to run the specified command.
718
 * Returns true if allowed, false if denied, -1 on error and
719
 * -2 for usage error.
720
 */
721
int
722
sudoers_check_cmnd(int argc, char * const argv[], char *env_add[],
723
    void *closure)
724
13.7k
{
725
13.7k
    char *iolog_path = NULL;
726
13.7k
    mode_t cmnd_umask = ACCESSPERMS;
727
13.7k
    int ret = -1;
728
13.7k
    debug_decl(sudoers_check_cmnd, SUDOERS_DEBUG_PLUGIN);
729
730
13.7k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
731
732
13.7k
    if (argc == 0) {
733
0
  sudo_warnx("%s", U_("no command specified"));
734
0
  debug_return_int(-1);
735
0
    }
736
737
13.7k
    if (need_reinit) {
738
  /* Was previous command intercepted? */
739
5.85k
  if (ISSET(sudoers_ctx.mode, MODE_RUN) && def_intercept)
740
0
      SET(sudoers_ctx.mode, MODE_POLICY_INTERCEPTED);
741
742
  /* Only certain mode flags are legal for intercepted commands. */
743
5.85k
  if (ISSET(sudoers_ctx.mode, MODE_POLICY_INTERCEPTED))
744
0
      sudoers_ctx.mode &= MODE_INTERCEPT_MASK;
745
746
  /* Re-initialize defaults if we are called multiple times. */
747
5.85k
  if (!sudoers_reinit_defaults(&sudoers_ctx))
748
0
      debug_return_int(-1);
749
5.85k
    }
750
13.7k
    need_reinit = true;
751
752
13.7k
    unlimit_nproc();
753
754
13.7k
    if (!set_perms(&sudoers_ctx, PERM_INITIAL))
755
0
  goto bad;
756
757
    /* Environment variables specified on the command line. */
758
13.7k
    if (env_add != NULL && env_add[0] != NULL)
759
5.24k
  sudoers_ctx.user.env_add = env_add;
760
761
    /*
762
     * Make a local copy of argc/argv, with special handling for the
763
     * '-i' option.  We also allocate an extra slot for bash's --login.
764
     */
765
13.7k
    if (sudoers_ctx.runas.argv != NULL && sudoers_ctx.runas.argv != sudoers_ctx.runas.argv_saved) {
766
3.19k
  sudoers_gc_remove(GC_PTR, sudoers_ctx.runas.argv);
767
3.19k
  free(sudoers_ctx.runas.argv);
768
3.19k
    }
769
13.7k
    sudoers_ctx.runas.argv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
770
13.7k
    if (sudoers_ctx.runas.argv == NULL)
771
0
  goto oom;
772
13.7k
    sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
773
13.7k
    memcpy(sudoers_ctx.runas.argv, argv, (size_t)argc * sizeof(char *));
774
13.7k
    sudoers_ctx.runas.argc = argc;
775
13.7k
    sudoers_ctx.runas.argv[sudoers_ctx.runas.argc] = NULL;
776
13.7k
    if (ISSET(sudoers_ctx.mode, MODE_LOGIN_SHELL) && sudoers_ctx.runas.pw != NULL) {
777
350
  sudoers_ctx.runas.argv[0] = strdup(sudoers_ctx.runas.pw->pw_shell);
778
350
  if (sudoers_ctx.runas.argv[0] == NULL)
779
0
      goto oom;
780
350
  sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv[0]);
781
350
    }
782
13.7k
    if (ISSET(sudoers_ctx.mode, MODE_EDIT)) {
783
1.62k
  if (!canonicalize_edit_args(sudoers_ctx.runas.argc,
784
1.62k
    sudoers_ctx.runas.argv)) {
785
519
      goto error;
786
519
  }
787
1.62k
    }
788
789
13.2k
    ret = sudoers_check_common(&sudoers_ctx, 0);
790
13.2k
    if (ret != true)
791
7.52k
  goto done;
792
793
5.71k
    if (!remote_iologs) {
794
2.69k
  if (iolog_enabled && def_iolog_file && def_iolog_dir) {
795
2.69k
      if ((iolog_path = format_iolog_path(&sudoers_ctx)) == NULL) {
796
0
    if (!def_ignore_iolog_errors)
797
0
        goto error;
798
    /* Unable to expand I/O log path, disable I/O logging. */
799
0
    def_log_input = false;
800
0
    def_log_output = false;
801
0
    def_log_stdin = false;
802
0
    def_log_stdout = false;
803
0
    def_log_stderr = false;
804
0
    def_log_ttyin = false;
805
0
    def_log_ttyout = false;
806
0
      }
807
2.69k
  }
808
2.69k
    }
809
810
    /*
811
     * Set umask based on sudoers.
812
     * If user's umask is more restrictive, OR in those bits too
813
     * unless umask_override is set.
814
     */
815
5.71k
    if (def_umask != ACCESSPERMS) {
816
5.71k
  cmnd_umask = def_umask;
817
5.71k
  if (!def_umask_override)
818
5.71k
      cmnd_umask |= sudoers_ctx.user.umask;
819
5.71k
    }
820
821
5.71k
    if (ISSET(sudoers_ctx.mode, MODE_LOGIN_SHELL)) {
822
176
  char *p;
823
824
  /* Convert /bin/sh -> -sh so shell knows it is a login shell */
825
176
  if ((p = strrchr(sudoers_ctx.runas.argv[0], '/')) == NULL)
826
0
      p = sudoers_ctx.runas.argv[0];
827
176
  *p = '-';
828
176
  sudoers_ctx.runas.argv[0] = p;
829
830
  /*
831
   * Newer versions of bash require the --login option to be used
832
   * in conjunction with the -c option even if the shell name starts
833
   * with a '-'.  Unfortunately, bash 1.x uses -login, not --login
834
   * so this will cause an error for that.
835
   */
836
176
  if (sudoers_ctx.runas.argc > 1 && strcmp(sudoers_ctx.runas.argv[0], "-bash") == 0 &&
837
120
      strcmp(sudoers_ctx.runas.argv[1], "-c") == 0) {
838
      /* We allocated extra space for the --login above. */
839
0
      memmove(&sudoers_ctx.runas.argv[2], &sudoers_ctx.runas.argv[1],
840
0
    (size_t)sudoers_ctx.runas.argc * sizeof(char *));
841
0
      sudoers_ctx.runas.argv[1] = (char *)"--login";
842
0
      sudoers_ctx.runas.argc++;
843
0
  }
844
845
176
#ifdef _PATH_ENVIRONMENT
846
  /* Insert system-wide environment variables. */
847
176
  if (!read_env_file(&sudoers_ctx, _PATH_ENVIRONMENT, true, false))
848
0
      sudo_warn("%s", _PATH_ENVIRONMENT);
849
176
#endif
850
#ifdef HAVE_LOGIN_CAP_H
851
  /* Set environment based on login class. */
852
  if (sudoers_ctx.runas.class) {
853
      login_cap_t *lc = login_getclass(sudoers_ctx.runas.class);
854
      if (lc != NULL) {
855
    setusercontext(lc, sudoers_ctx.runas.pw,
856
        sudoers_ctx.runas.pw->pw_uid, LOGIN_SETPATH|LOGIN_SETENV);
857
    login_close(lc);
858
      }
859
  }
860
#endif /* HAVE_LOGIN_CAP_H */
861
176
    }
862
863
    /* Insert system-wide environment variables. */
864
5.71k
    if (def_restricted_env_file) {
865
5.71k
  if (!read_env_file(&sudoers_ctx, def_restricted_env_file, false, true))
866
0
      sudo_warn("%s", def_restricted_env_file);
867
5.71k
    }
868
5.71k
    if (def_env_file) {
869
5.71k
  if (!read_env_file(&sudoers_ctx, def_env_file, false, false))
870
0
      sudo_warn("%s", def_env_file);
871
5.71k
    }
872
873
    /* Insert user-specified environment variables. */
874
5.71k
    if (!insert_env_vars(sudoers_ctx.user.env_add)) {
875
150
  sudo_warnx("%s",
876
150
      U_("error setting user-specified environment variables"));
877
150
  goto error;
878
150
    }
879
880
    /* Note: must call audit before uid change. */
881
5.56k
    if (ISSET(sudoers_ctx.mode, MODE_EDIT)) {
882
1.54k
  const char *env_editor = NULL;
883
1.54k
  char **edit_argv;
884
1.54k
  int edit_argc;
885
886
1.54k
  sudoers_ctx.sudoedit_nfiles = sudoers_ctx.runas.argc - 1;
887
1.54k
  free(sudoers_ctx.runas.cmnd);
888
1.54k
  sudoers_ctx.runas.cmnd = find_editor(sudoers_ctx.sudoedit_nfiles,
889
1.54k
      sudoers_ctx.runas.argv + 1, &edit_argc, &edit_argv, NULL, &env_editor);
890
1.54k
  if (sudoers_ctx.runas.cmnd == NULL) {
891
368
      switch (errno) {
892
368
      case ENOENT:
893
368
    audit_failure(&sudoers_ctx, sudoers_ctx.runas.argv,
894
368
        N_("%s: command not found"),
895
368
        env_editor ? env_editor : def_editor);
896
368
    sudo_warnx(U_("%s: command not found"),
897
368
        env_editor ? env_editor : def_editor);
898
368
    goto error;
899
0
      case EINVAL:
900
0
    if (def_env_editor && env_editor != NULL) {
901
        /* User tried to do something funny with the editor. */
902
0
        log_warningx(&sudoers_ctx,
903
0
      SLOG_NO_STDERR|SLOG_AUDIT|SLOG_SEND_MAIL,
904
0
      "invalid user-specified editor: %s", env_editor);
905
0
        goto error;
906
0
    }
907
0
    FALLTHROUGH;
908
0
      default:
909
0
    goto error;
910
368
      }
911
368
  }
912
  /* find_editor() already g/c'd edit_argv[] */
913
1.17k
  if (sudoers_ctx.runas.argv != sudoers_ctx.runas.argv_saved) {
914
1.17k
      sudoers_gc_remove(GC_PTR, sudoers_ctx.runas.argv);
915
1.17k
      free(sudoers_ctx.runas.argv);
916
1.17k
  }
917
1.17k
  sudoers_ctx.runas.argv = edit_argv;
918
1.17k
  sudoers_ctx.runas.argc = edit_argc;
919
920
  /* We want to run the editor with the unmodified environment. */
921
1.17k
  env_swap_old();
922
1.17k
    }
923
924
    /* Save the initial command and argv so we have it for exit logging. */
925
5.19k
    if (sudoers_ctx.runas.cmnd_saved == NULL) {
926
2.69k
  sudoers_ctx.runas.cmnd_saved = strdup(sudoers_ctx.runas.cmnd);
927
2.69k
  if (sudoers_ctx.runas.cmnd_saved == NULL)
928
0
      goto oom;
929
2.69k
  sudoers_ctx.runas.argv_saved = sudoers_ctx.runas.argv;
930
2.69k
    }
931
932
5.19k
    ret = true;
933
5.19k
    goto done;
934
935
0
bad:
936
0
    ret = false;
937
0
    goto done;
938
939
0
oom:
940
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
941
942
1.03k
error:
943
1.03k
    ret = -1;
944
945
13.7k
done:
946
13.7k
    mail_parse_errors(&sudoers_ctx);
947
948
13.7k
    if (def_group_plugin)
949
0
  group_plugin_unload();
950
13.7k
    reset_parser();
951
952
13.7k
    if (ret == -1) {
953
  /* Free locally-allocated strings. */
954
1.11k
  free(iolog_path);
955
12.6k
    } else {
956
  /* Store settings to pass back to front-end. */
957
12.6k
  if (!sudoers_policy_store_result(&sudoers_ctx, ret,
958
12.6k
      sudoers_ctx.runas.argv, env_get(), cmnd_umask, iolog_path, closure))
959
0
      ret = -1;
960
12.6k
    }
961
962
    /* Zero out stashed copy of environment, it is owned by the front-end. */
963
13.7k
    (void)env_init(NULL);
964
965
13.7k
    if (!rewind_perms())
966
0
  ret = -1;
967
968
13.7k
    restore_nproc();
969
970
13.7k
    sudo_warn_set_locale_func(NULL);
971
972
13.7k
    debug_return_int(ret);
973
13.7k
}
974
975
/*
976
 * Validate the user and update their timestamp file entry.
977
 * Returns true if allowed, false if denied, -1 on error and
978
 * -2 for usage error.
979
 */
980
int
981
sudoers_validate_user(void)
982
3.30k
{
983
3.30k
    int ret = -1;
984
3.30k
    debug_decl(sudoers_validate_user, SUDOERS_DEBUG_PLUGIN);
985
986
3.30k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
987
988
3.30k
    unlimit_nproc();
989
990
3.30k
    if (!set_perms(&sudoers_ctx, PERM_INITIAL))
991
0
  goto done;
992
993
3.30k
    sudoers_ctx.runas.argv = reallocarray(NULL, 2, sizeof(char *));
994
3.30k
    if (sudoers_ctx.runas.argv == NULL) {
995
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
996
0
  goto done;
997
0
    }
998
3.30k
    sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
999
3.30k
    sudoers_ctx.runas.argv[0] = (char *)"validate";
1000
3.30k
    sudoers_ctx.runas.argv[1] = NULL;
1001
3.30k
    sudoers_ctx.runas.argc = 2;
1002
1003
3.30k
    ret = sudoers_check_common(&sudoers_ctx, I_VERIFYPW);
1004
1005
3.30k
done:
1006
3.30k
    mail_parse_errors(&sudoers_ctx);
1007
1008
3.30k
    if (def_group_plugin)
1009
0
  group_plugin_unload();
1010
3.30k
    reset_parser();
1011
3.30k
    env_init(NULL);
1012
1013
3.30k
    if (!rewind_perms())
1014
0
  ret = -1;
1015
1016
3.30k
    restore_nproc();
1017
1018
3.30k
    sudo_warn_set_locale_func(NULL);
1019
1020
3.30k
    debug_return_int(ret);
1021
3.30k
}
1022
1023
/*
1024
 * List a user's privileges or check whether a specific command may be run.
1025
 * Returns true if allowed, false if denied, -1 on error and
1026
 * -2 for usage error.
1027
 */
1028
int
1029
sudoers_list(int argc, char * const argv[], const char *list_user, int verbose)
1030
9.90k
{
1031
9.90k
    struct passwd *pw;
1032
9.90k
    int ret = -1;
1033
9.90k
    debug_decl(sudoers_list, SUDOERS_DEBUG_PLUGIN);
1034
1035
9.90k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
1036
1037
9.90k
    unlimit_nproc();
1038
1039
9.90k
    if (!set_perms(&sudoers_ctx, PERM_INITIAL))
1040
0
  goto done;
1041
1042
9.90k
    if (list_user) {
1043
3.30k
  if (sudoers_ctx.runas.list_pw != NULL)
1044
1.65k
      sudo_pw_delref(sudoers_ctx.runas.list_pw);
1045
3.30k
  sudoers_ctx.runas.list_pw = sudo_getpwnam(list_user);
1046
3.30k
  if (sudoers_ctx.runas.list_pw == NULL) {
1047
0
      sudo_warnx(U_("unknown user %s"), list_user);
1048
0
      goto done;
1049
0
  }
1050
3.30k
    }
1051
1052
9.90k
    sudoers_ctx.runas.argv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
1053
9.90k
    if (sudoers_ctx.runas.argv == NULL) {
1054
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1055
0
  goto done;
1056
0
    }
1057
9.90k
    sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
1058
9.90k
    sudoers_ctx.runas.argv[0] = (char *)"list";
1059
9.90k
    if (argc != 0)
1060
3.30k
  memcpy(sudoers_ctx.runas.argv + 1, argv, (size_t)argc * sizeof(char *));
1061
9.90k
    sudoers_ctx.runas.argc = argc + 1;
1062
9.90k
    sudoers_ctx.runas.argv[sudoers_ctx.runas.argc] = NULL;
1063
1064
9.90k
    ret = sudoers_check_common(&sudoers_ctx, I_LISTPW);
1065
9.90k
    if (ret != true)
1066
846
  goto done;
1067
1068
9.05k
    pw = sudoers_ctx.runas.list_pw ? sudoers_ctx.runas.list_pw : sudoers_ctx.user.pw;
1069
9.05k
    if (ISSET(sudoers_ctx.mode, MODE_CHECK))
1070
3.01k
  ret = display_cmnd(&sudoers_ctx, snl, pw, verbose);
1071
6.03k
    else
1072
6.03k
  ret = display_privs(&sudoers_ctx, snl, pw, verbose);
1073
1074
9.90k
done:
1075
9.90k
    mail_parse_errors(&sudoers_ctx);
1076
1077
9.90k
    if (def_group_plugin)
1078
0
  group_plugin_unload();
1079
9.90k
    reset_parser();
1080
9.90k
    env_init(NULL);
1081
1082
9.90k
    if (!rewind_perms())
1083
0
  ret = -1;
1084
1085
9.90k
    restore_nproc();
1086
1087
9.90k
    sudo_warn_set_locale_func(NULL);
1088
1089
9.90k
    debug_return_int(ret);
1090
9.90k
}
1091
1092
/*
1093
 * Initialize timezone and fill in ctx->user.
1094
 */
1095
static bool
1096
init_vars(struct sudoers_context *ctx, char * const envp[])
1097
20.0k
{
1098
20.0k
    char * const * ep;
1099
20.0k
    bool unknown_user = false;
1100
20.0k
    debug_decl(init_vars, SUDOERS_DEBUG_PLUGIN);
1101
1102
20.0k
    if (!sudoers_initlocale(setlocale(LC_ALL, NULL), def_sudoers_locale)) {
1103
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1104
0
  debug_return_bool(false);
1105
0
    }
1106
1107
20.0k
#define MATCHES(s, v) \
1108
241k
    (strncmp((s), (v), sizeof(v) - 1) == 0 && (s)[sizeof(v) - 1] != '\0')
1109
1110
20.0k
    ctx->user.envp = envp;
1111
723k
    for (ep = ctx->user.envp; *ep; ep++) {
1112
703k
  switch (**ep) {
1113
20.0k
      case 'K':
1114
20.0k
    if (MATCHES(*ep, "KRB5CCNAME="))
1115
20.0k
        ctx->user.ccname = *ep + sizeof("KRB5CCNAME=") - 1;
1116
20.0k
    break;
1117
40.1k
      case 'P':
1118
40.1k
    if (MATCHES(*ep, "PATH="))
1119
20.0k
        ctx->user.path = *ep + sizeof("PATH=") - 1;
1120
40.1k
    break;
1121
100k
      case 'S':
1122
100k
    if (MATCHES(*ep, "SUDO_PROMPT=")) {
1123
        /* Don't override "sudo -p prompt" */
1124
20.0k
        if (ctx->user.prompt == NULL)
1125
19.2k
      ctx->user.prompt = *ep + sizeof("SUDO_PROMPT=") - 1;
1126
20.0k
        break;
1127
20.0k
    }
1128
80.3k
    if (MATCHES(*ep, "SUDO_USER="))
1129
0
        prev_user = *ep + sizeof("SUDO_USER=") - 1;
1130
80.3k
    break;
1131
703k
      }
1132
703k
    }
1133
20.0k
#undef MATCHES
1134
1135
20.0k
    if (ctx->user.pw == NULL) {
1136
  /* Fake a struct passwd for the call to log_warningx(). */
1137
208
  ctx->user.pw = sudo_mkpwent(ctx->user.name, ctx->user.uid,
1138
208
      ctx->user.gid, NULL, NULL);
1139
208
  unknown_user = true;
1140
208
    }
1141
20.0k
    if (ctx->user.gid_list == NULL)
1142
20.0k
  ctx->user.gid_list = sudo_get_gidlist(ctx->user.pw, ENTRY_TYPE_ANY);
1143
1144
    /* Store initialize permissions so we can restore them later. */
1145
20.0k
    if (!set_perms(ctx, PERM_INITIAL))
1146
0
  debug_return_bool(false);
1147
1148
    /* Set parse callbacks */
1149
20.0k
    set_callbacks();
1150
1151
    /* It is now safe to use log_warningx() and set_perms() */
1152
20.0k
    if (unknown_user) {
1153
208
  log_warningx(ctx, SLOG_SEND_MAIL, N_("unknown user %s"), ctx->user.name);
1154
208
  debug_return_bool(false);
1155
208
    }
1156
1157
    /*
1158
     * Set runas passwd/group entries based on command line or sudoers.
1159
     * Note that if runas_group was specified without runas_user we
1160
     * run the command as the invoking user.
1161
     */
1162
19.8k
    if (ctx->runas.group != NULL) {
1163
717
  if (!set_runasgr(ctx, ctx->runas.group, false))
1164
84
      debug_return_bool(false);
1165
633
  if (!set_runaspw(ctx, ctx->runas.user ?
1166
633
    ctx->runas.user : ctx->user.name, false))
1167
3
      debug_return_bool(false);
1168
19.1k
    } else {
1169
19.1k
  if (!set_runaspw(ctx, ctx->runas.user ?
1170
19.1k
    ctx->runas.user : def_runas_default, false))
1171
31
      debug_return_bool(false);
1172
19.1k
    }
1173
1174
19.7k
    debug_return_bool(true);
1175
19.7k
}
1176
1177
/*
1178
 * Fill in ctx->user.cmnd and ctx->user.cmnd_stat variables.
1179
 * Does not fill in ctx->user.cmnd_base.
1180
 */
1181
int
1182
set_cmnd_path(struct sudoers_context *ctx, const char *runchroot)
1183
21.1k
{
1184
21.1k
    const char *cmnd_in;
1185
21.1k
    char *cmnd_out = NULL;
1186
21.1k
    char *path = ctx->user.path;
1187
21.1k
    int ret;
1188
21.1k
    debug_decl(set_cmnd_path, SUDOERS_DEBUG_PLUGIN);
1189
1190
21.1k
    cmnd_in = ISSET(ctx->mode, MODE_CHECK) ?
1191
17.8k
  ctx->runas.argv[1] : ctx->runas.argv[0];
1192
1193
21.1k
    free(ctx->user.cmnd_list);
1194
21.1k
    ctx->user.cmnd_list = NULL;
1195
21.1k
    free(ctx->user.cmnd);
1196
21.1k
    ctx->user.cmnd = NULL;
1197
21.1k
    canon_path_free(ctx->user.cmnd_dir);
1198
21.1k
    ctx->user.cmnd_dir = NULL;
1199
21.1k
    if (def_secure_path && !user_is_exempt(ctx))
1200
0
  path = def_secure_path;
1201
1202
21.1k
    ret = resolve_cmnd(ctx, cmnd_in, &cmnd_out, path, runchroot);
1203
21.1k
    if (ret == FOUND) {
1204
13.9k
  char *slash = strrchr(cmnd_out, '/');
1205
13.9k
  if (slash != NULL) {
1206
13.9k
      *slash = '\0';
1207
13.9k
      ctx->user.cmnd_dir = canon_path(cmnd_out);
1208
13.9k
      if (ctx->user.cmnd_dir == NULL && errno == ENOMEM)
1209
0
    goto error;
1210
13.9k
      *slash = '/';
1211
13.9k
  }
1212
13.9k
    }
1213
1214
21.1k
    if (ISSET(ctx->mode, MODE_CHECK))
1215
3.30k
  ctx->user.cmnd_list = cmnd_out;
1216
17.8k
    else
1217
17.8k
  ctx->user.cmnd = cmnd_out;
1218
1219
21.1k
    debug_return_int(ret);
1220
0
error:
1221
0
    free(cmnd_out);
1222
0
    debug_return_int(NOT_FOUND_ERROR);
1223
0
}
1224
1225
/*
1226
 * Fill in ctx->user.cmnd, ctx->user.cmnd_stat and cmnd_status variables.
1227
 * Does not fill in ctx->user.cmnd_base.
1228
 */
1229
void
1230
set_cmnd_status(struct sudoers_context *ctx, const char *runchroot)
1231
5.75k
{
1232
5.75k
    cmnd_status = set_cmnd_path(ctx, runchroot);
1233
5.75k
}
1234
1235
/*
1236
 * Fill in ctx->user.cmnd, ctx->user.cmnd_args, ctx->user.cmnd_base and
1237
 * ctx->user.cmnd_stat variables and apply any command-specific defaults entries.
1238
 */
1239
static int
1240
set_cmnd(struct sudoers_context *ctx)
1241
26.4k
{
1242
26.4k
    struct sudo_nss *nss;
1243
26.4k
    int ret = FOUND;
1244
26.4k
    debug_decl(set_cmnd, SUDOERS_DEBUG_PLUGIN);
1245
1246
    /* Allocate ctx->user.cmnd_stat for find_path() and match functions. */
1247
26.4k
    free(ctx->user.cmnd_stat);
1248
26.4k
    ctx->user.cmnd_stat = calloc(1, sizeof(struct stat));
1249
26.4k
    if (ctx->user.cmnd_stat == NULL) {
1250
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1251
0
  debug_return_int(NOT_FOUND_ERROR);
1252
0
    }
1253
1254
    /* Re-initialize for when we are called multiple times. */
1255
26.4k
    free(ctx->runas.cmnd);
1256
26.4k
    ctx->runas.cmnd = NULL;
1257
1258
26.4k
    if (ISSET(ctx->mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
1259
16.5k
  if (!ISSET(ctx->mode, MODE_EDIT)) {
1260
15.4k
      const char *runchroot = ctx->runas.chroot;
1261
15.4k
      if (runchroot == NULL && def_runchroot != NULL &&
1262
15.3k
        strcmp(def_runchroot, "*") != 0)
1263
15.3k
    runchroot = def_runchroot;
1264
1265
15.4k
      ret = set_cmnd_path(ctx, runchroot);
1266
15.4k
      if (ret == NOT_FOUND_ERROR) {
1267
0
    if (errno == ENAMETOOLONG) {
1268
0
        audit_failure(ctx, ctx->runas.argv, N_("command too long"));
1269
0
    }
1270
0
    log_warning(ctx, 0, "%s", ctx->runas.argv[0]);
1271
0
    debug_return_int(ret);
1272
0
      }
1273
15.4k
  }
1274
1275
  /* set ctx->user.cmnd_args */
1276
16.5k
  free(ctx->user.cmnd_args);
1277
16.5k
  ctx->user.cmnd_args = NULL;
1278
16.5k
  if (ISSET(ctx->mode, MODE_CHECK)) {
1279
3.30k
      if (ctx->runas.argc > 2) {
1280
    /* Skip the command being listed in ctx->runas.argv[1]. */
1281
602
    ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 2, ' ', NULL);
1282
602
    if (ctx->user.cmnd_args == NULL)
1283
0
        debug_return_int(NOT_FOUND_ERROR);
1284
602
      }
1285
13.2k
  } else if (ctx->runas.argc > 1) {
1286
2.82k
      if (ISSET(ctx->mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
1287
610
        ISSET(ctx->mode, MODE_RUN)) {
1288
    /*
1289
     * When running a command via a shell, the sudo front-end
1290
     * escapes potential meta chars.  We unescape non-spaces
1291
     * for sudoers matching and logging purposes.
1292
     * TODO: move escaping to the policy plugin instead
1293
     */
1294
610
    ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 1, ' ',
1295
610
        strlcpy_unescape);
1296
2.21k
      } else {
1297
2.21k
    ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 1, ' ',
1298
2.21k
        NULL);
1299
2.21k
      }
1300
2.82k
      if (ctx->user.cmnd_args == NULL)
1301
0
    debug_return_int(NOT_FOUND_ERROR);
1302
2.82k
  }
1303
16.5k
    }
1304
26.4k
    if (ctx->user.cmnd == NULL) {
1305
14.1k
  ctx->user.cmnd = strdup(ctx->runas.argv[0]);
1306
14.1k
  if (ctx->user.cmnd == NULL)
1307
0
      debug_return_int(NOT_FOUND_ERROR);
1308
14.1k
    }
1309
26.4k
    ctx->user.cmnd_base = sudo_basename(ctx->user.cmnd);
1310
1311
    /* Convert "sudo sudoedit" -> "sudoedit" */
1312
26.4k
    if (ISSET(ctx->mode, MODE_RUN) && strcmp(ctx->user.cmnd_base, "sudoedit") == 0) {
1313
1.09k
  char *new_cmnd;
1314
1315
1.09k
  CLR(ctx->mode, MODE_RUN);
1316
1.09k
  SET(ctx->mode, MODE_EDIT);
1317
1.09k
  sudo_warnx("%s", U_("sudoedit doesn't need to be run via sudo"));
1318
1.09k
  if ((new_cmnd = strdup("sudoedit")) == NULL) {
1319
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1320
0
      debug_return_int(NOT_FOUND_ERROR);
1321
0
  }
1322
1.09k
  free(ctx->user.cmnd);
1323
1.09k
  ctx->user.cmnd_base = ctx->user.cmnd = new_cmnd;
1324
1.09k
    }
1325
1326
26.4k
    TAILQ_FOREACH(nss, snl, entries) {
1327
  /* Missing/invalid defaults is not a fatal error. */
1328
26.4k
  (void)update_defaults(ctx, nss->parse_tree, NULL, SETDEF_CMND, false);
1329
26.4k
    }
1330
1331
26.4k
    debug_return_int(ret);
1332
26.4k
}
1333
1334
static int
1335
open_file(const char *path, int flags)
1336
0
{
1337
0
    int fd;
1338
0
    debug_decl(open_file, SUDOERS_DEBUG_PLUGIN);
1339
1340
0
    if (!set_perms(NULL, PERM_SUDOERS))
1341
0
  debug_return_int(-1);
1342
1343
0
    fd = open(path, flags);
1344
0
    if (fd == -1 && errno == EACCES && geteuid() != ROOT_UID) {
1345
  /*
1346
   * If we tried to open sudoers as non-root but got EACCES,
1347
   * try again as root.
1348
   */
1349
0
  int serrno = errno;
1350
0
  if (restore_perms() && set_perms(NULL, PERM_ROOT))
1351
0
      fd = open(path, flags);
1352
0
  errno = serrno;
1353
0
    }
1354
0
    if (!restore_perms()) {
1355
  /* unable to change back to root */
1356
0
  if (fd != -1) {
1357
0
      close(fd);
1358
0
      fd = -1;
1359
0
  }
1360
0
    }
1361
1362
0
    debug_return_int(fd);
1363
0
}
1364
1365
/*
1366
 * Open sudoers file and check mode/owner/type.
1367
 * Returns a handle to the sudoers file or NULL on error.
1368
 */
1369
FILE *
1370
open_sudoers(const char *path, char **outfile, bool doedit, bool *keepopen)
1371
0
{
1372
0
    char fnamebuf[PATH_MAX];
1373
0
    const char *fname = fnamebuf;
1374
0
    FILE *fp = NULL;
1375
0
    struct stat sb;
1376
0
    int error, fd;
1377
0
    debug_decl(open_sudoers, SUDOERS_DEBUG_PLUGIN);
1378
1379
0
    if (outfile == NULL) {
1380
  /* Single file, do not treat as a path. */
1381
0
  fname = path;
1382
0
  fd = open_file(fname, O_RDONLY|O_NONBLOCK);
1383
0
        if (fd != -1)
1384
0
            (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
1385
0
    } else {
1386
  /* Could be a colon-separated path of file names. */
1387
0
  fd = sudo_open_conf_path(path, fnamebuf, sizeof(fnamebuf), open_file);
1388
0
    }
1389
0
    if (sudoers_ctx.parser_conf.ignore_perms) {
1390
  /* Skip sudoers security checks when ignore_perms is set. */
1391
0
  if (fd == -1 || fstat(fd, &sb) == -1)
1392
0
      error = SUDO_PATH_MISSING;
1393
0
  else
1394
0
      error = SUDO_PATH_SECURE;
1395
0
    } else {
1396
0
  error = sudo_secure_fd(fd, S_IFREG, sudoers_file_uid(),
1397
0
      sudoers_file_gid(), &sb);
1398
0
    }
1399
0
    switch (error) {
1400
0
    case SUDO_PATH_SECURE:
1401
  /*
1402
   * Make sure we can read the file so we can present the
1403
   * user with a reasonable error message (unlike the lexer).
1404
   */
1405
0
  if ((fp = fdopen(fd, "r")) == NULL) {
1406
0
      log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
1407
0
    N_("unable to open %s"), fname);
1408
0
  } else {
1409
0
      fd = -1;
1410
0
      if (sb.st_size != 0 && fgetc(fp) == EOF) {
1411
0
    log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
1412
0
        N_("unable to read %s"), fname);
1413
0
    fclose(fp);
1414
0
    fp = NULL;
1415
0
      } else {
1416
    /* Rewind fp and set close on exec flag. */
1417
0
    rewind(fp);
1418
0
    (void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
1419
0
    if (outfile != NULL) {
1420
0
                    *outfile = sudo_rcstr_dup(fname);
1421
0
        if (*outfile == NULL) {
1422
0
      sudo_warnx(U_("%s: %s"), __func__,
1423
0
          U_("unable to allocate memory"));
1424
0
      fclose(fp);
1425
0
      fp = NULL;
1426
0
        }
1427
0
    }
1428
0
      }
1429
0
  }
1430
0
  break;
1431
0
    case SUDO_PATH_MISSING:
1432
0
  log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
1433
0
      N_("unable to open %s"), path);
1434
0
  break;
1435
0
    case SUDO_PATH_BAD_TYPE:
1436
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1437
0
      N_("%s is not a regular file"), fname);
1438
0
  break;
1439
0
    case SUDO_PATH_WRONG_OWNER:
1440
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1441
0
      N_("%s is owned by uid %u, should be %u"), fname,
1442
0
      (unsigned int)sb.st_uid, (unsigned int)sudoers_file_uid());
1443
0
  break;
1444
0
    case SUDO_PATH_WORLD_WRITABLE:
1445
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1446
0
      N_("%s is world writable"), fname);
1447
0
  break;
1448
0
    case SUDO_PATH_GROUP_WRITABLE:
1449
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1450
0
      N_("%s is owned by gid %u, should be %u"), fname,
1451
0
      (unsigned int)sb.st_gid, (unsigned int)sudoers_file_gid());
1452
0
  break;
1453
0
    default:
1454
0
  sudo_warnx("%s: internal error, unexpected error %d", __func__, error);
1455
0
  break;
1456
0
    }
1457
1458
0
    if (fp == NULL && fd != -1)
1459
0
  close(fd);
1460
1461
0
    debug_return_ptr(fp);
1462
0
}
1463
1464
#ifdef HAVE_LOGIN_CAP_H
1465
static bool
1466
set_loginclass(struct sudoers_context *ctx)
1467
{
1468
    const struct passwd *pw = ctx->runas.pw ? ctx->runas.pw : ctx->user.pw;
1469
    const unsigned int errflags = SLOG_RAW_MSG;
1470
    login_cap_t *lc;
1471
    bool ret = true;
1472
    debug_decl(set_loginclass, SUDOERS_DEBUG_PLUGIN);
1473
1474
    if (!def_use_loginclass)
1475
  goto done;
1476
1477
    if (ctx->runas.class && strcmp(ctx->runas.class, "-") != 0) {
1478
  if (ctx->user.uid != 0 && pw->pw_uid != 0) {
1479
      sudo_warnx(U_("only root can use \"-c %s\""), ctx->runas.class);
1480
      ret = false;
1481
      goto done;
1482
  }
1483
    } else {
1484
  ctx->runas.class = pw->pw_class;
1485
  if (!ctx->runas.class || !*ctx->runas.class)
1486
      ctx->runas.class = (char *)
1487
    ((pw->pw_uid == 0) ? LOGIN_DEFROOTCLASS : LOGIN_DEFCLASS);
1488
    }
1489
1490
    /* Make sure specified login class is valid. */
1491
    lc = login_getclass(ctx->runas.class);
1492
    if (!lc || !lc->lc_class || strcmp(lc->lc_class, ctx->runas.class) != 0) {
1493
  /*
1494
   * Don't make it an error if the user didn't specify the login
1495
   * class themselves.  We do this because if login.conf gets
1496
   * corrupted we want the admin to be able to use sudo to fix it.
1497
   */
1498
  log_warningx(ctx, errflags, N_("unknown login class %s"),
1499
      ctx->runas.class);
1500
  def_use_loginclass = false;
1501
  if (ctx->runas.class)
1502
      ret = false;
1503
    }
1504
    login_close(lc);
1505
done:
1506
    debug_return_bool(ret);
1507
}
1508
#else
1509
static bool
1510
set_loginclass(struct sudoers_context *ctx)
1511
19.7k
{
1512
19.7k
    return true;
1513
19.7k
}
1514
#endif /* HAVE_LOGIN_CAP_H */
1515
1516
/*
1517
 * Get passwd entry for the user we are going to run commands as
1518
 * and store it in ctx->runas.pw.  By default, commands run as "root".
1519
 */
1520
static bool
1521
set_runaspw(struct sudoers_context *ctx, const char *user, bool quiet)
1522
43.7k
{
1523
43.7k
    struct passwd *pw = NULL;
1524
43.7k
    debug_decl(set_runaspw, SUDOERS_DEBUG_PLUGIN);
1525
1526
43.7k
    unknown_runas_uid = false;
1527
43.7k
    if (*user == '#') {
1528
463
  const char *errstr;
1529
463
  uid_t uid = sudo_strtoid(user + 1, &errstr);
1530
463
  if (errstr == NULL) {
1531
460
      if ((pw = sudo_getpwuid(uid)) == NULL) {
1532
190
    unknown_runas_uid = true;
1533
190
    pw = sudo_fakepwnam(user, ctx->user.gid);
1534
190
      }
1535
460
  }
1536
463
    }
1537
43.7k
    if (pw == NULL) {
1538
43.3k
  if ((pw = sudo_getpwnam(user)) == NULL) {
1539
34
      if (!quiet)
1540
34
    log_warningx(ctx, SLOG_AUDIT, N_("unknown user %s"), user);
1541
34
      debug_return_bool(false);
1542
34
  }
1543
43.3k
    }
1544
43.7k
    if (ctx->runas.pw != NULL)
1545
24.0k
  sudo_pw_delref(ctx->runas.pw);
1546
43.7k
    ctx->runas.pw = pw;
1547
43.7k
    debug_return_bool(true);
1548
43.7k
}
1549
1550
/*
1551
 * Get group entry for the group we are going to run commands as
1552
 * and store it in ctx->runas.gr.
1553
 */
1554
static bool
1555
set_runasgr(struct sudoers_context *ctx, const char *group, bool quiet)
1556
717
{
1557
717
    struct group *gr = NULL;
1558
717
    debug_decl(set_runasgr, SUDOERS_DEBUG_PLUGIN);
1559
1560
717
    unknown_runas_gid = false;
1561
717
    if (*group == '#') {
1562
554
  const char *errstr;
1563
554
  gid_t gid = sudo_strtoid(group + 1, &errstr);
1564
554
  if (errstr == NULL) {
1565
552
      if ((gr = sudo_getgrgid(gid)) == NULL) {
1566
311
    unknown_runas_gid = true;
1567
311
    gr = sudo_fakegrnam(group);
1568
311
      }
1569
552
  }
1570
554
    }
1571
717
    if (gr == NULL) {
1572
165
  if ((gr = sudo_getgrnam(group)) == NULL) {
1573
84
      if (!quiet)
1574
84
    log_warningx(ctx, SLOG_AUDIT, N_("unknown group %s"), group);
1575
84
      debug_return_bool(false);
1576
84
  }
1577
165
    }
1578
633
    if (ctx->runas.gr != NULL)
1579
0
  sudo_gr_delref(ctx->runas.gr);
1580
633
    ctx->runas.gr = gr;
1581
633
    debug_return_bool(true);
1582
633
}
1583
1584
/*
1585
 * Callback for runas_default sudoers setting.
1586
 */
1587
bool
1588
cb_runas_default(struct sudoers_context *ctx, const char *file, int line,
1589
    int column, const union sudo_defs_val *sd_un, int op)
1590
25.6k
{
1591
25.6k
    debug_decl(cb_runas_default, SUDOERS_DEBUG_PLUGIN);
1592
1593
    /* Only reset runaspw if user didn't specify one. */
1594
25.6k
    if (ctx->runas.user == NULL && ctx->runas.group == NULL)
1595
24.0k
  debug_return_bool(set_runaspw(ctx, sd_un->str, true));
1596
1.61k
    debug_return_bool(true);
1597
1.61k
}
1598
1599
/*
1600
 * Cleanup hook for sudo_fatal()/sudo_fatalx()
1601
 * Also called at policy close time.
1602
 */
1603
void
1604
sudoers_cleanup(void)
1605
24.8k
{
1606
24.8k
    struct sudo_nss *nss;
1607
24.8k
    struct defaults *def;
1608
24.8k
    debug_decl(sudoers_cleanup, SUDOERS_DEBUG_PLUGIN);
1609
1610
24.8k
    if (snl != NULL) {
1611
19.7k
  TAILQ_FOREACH(nss, snl, entries) {
1612
19.7k
      nss->close(&sudoers_ctx, nss);
1613
19.7k
  }
1614
19.7k
  snl = NULL;
1615
19.7k
  reset_parser();
1616
19.7k
    }
1617
66.2k
    while ((def = TAILQ_FIRST(&initial_defaults)) != NULL) {
1618
41.3k
  TAILQ_REMOVE(&initial_defaults, def, entries);
1619
41.3k
  free(def->var);
1620
41.3k
  free(def->val);
1621
41.3k
  free(def);
1622
41.3k
    }
1623
24.8k
    sudoers_initialized = false;
1624
24.8k
    need_reinit = false;
1625
24.8k
    if (def_group_plugin)
1626
0
  group_plugin_unload();
1627
24.8k
    sudoers_ctx_free(&sudoers_ctx);
1628
24.8k
    sudo_freepwcache();
1629
24.8k
    sudo_freegrcache();
1630
24.8k
    canon_path_free_cache();
1631
1632
    /* We must free the cached environment before running g/c. */
1633
24.8k
    env_free();
1634
1635
    /* Run garbage collector. */
1636
24.8k
    sudoers_gc_run();
1637
1638
    /* Clear globals */
1639
24.8k
    prev_user = NULL;
1640
1641
24.8k
    debug_return;
1642
24.8k
}
1643
1644
bool
1645
sudoers_set_mode(unsigned int flags, unsigned int mask)
1646
33.5k
{
1647
33.5k
    SET(sudoers_ctx.mode, flags);
1648
33.5k
    return ((sudoers_ctx.mode & mask) == sudoers_ctx.mode);
1649
33.5k
}
1650
1651
const struct sudoers_context *
1652
sudoers_get_context(void)
1653
66.3k
{
1654
66.3k
    return &sudoers_ctx;
1655
66.3k
}
1656
1657
bool
1658
sudoers_set_log_format(enum def_tuple tuple)
1659
0
{
1660
0
    enum eventlog_format format;
1661
0
    debug_decl(cb_log_format, SUDOERS_DEBUG_PLUGIN);
1662
1663
    /* FFR - make "json" an alias for EVLOG_JSON_COMPACT instead. */
1664
0
    switch (tuple) {
1665
0
    case json_compact:
1666
0
        format = EVLOG_JSON_COMPACT;
1667
0
        break;
1668
0
    case json:
1669
0
    case json_pretty:
1670
0
        format = EVLOG_JSON_PRETTY;
1671
0
        break;
1672
0
    case sudo:
1673
0
        format = EVLOG_SUDO;
1674
0
        break;
1675
0
    default:
1676
0
  debug_return_bool(false);
1677
0
    }
1678
0
    eventlog_set_format(format);
1679
1680
    debug_return_bool(true);
1681
0
}