Coverage Report

Created: 2026-08-31 06:41

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.23k
# 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
25.7k
{
101
25.7k
#ifdef __linux__
102
25.7k
    struct rlimit rl;
103
25.7k
    debug_decl(unlimit_nproc, SUDOERS_DEBUG_UTIL);
104
105
25.7k
    if (getrlimit(RLIMIT_NPROC, &nproclimit) != 0)
106
0
      sudo_warn("getrlimit(RLIMIT_NPROC)");
107
25.7k
    rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
108
25.7k
    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
25.7k
    debug_return;
114
25.7k
#endif /* __linux__ */
115
25.7k
}
116
117
/*
118
 * Restore saved value of RLIMIT_NPROC.
119
 */
120
static void
121
restore_nproc(void)
122
25.7k
{
123
25.7k
#ifdef __linux__
124
25.7k
    debug_decl(restore_nproc, SUDOERS_DEBUG_UTIL);
125
126
25.7k
    if (setrlimit(RLIMIT_NPROC, &nproclimit) != 0)
127
0
  sudo_warn("setrlimit(RLIMIT_NPROC)");
128
129
25.7k
    debug_return;
130
25.7k
#endif /* __linux__ */
131
25.7k
}
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.51k
{
141
5.51k
    struct sudo_nss *nss, *nss_next;
142
5.51k
    sudoers_logger_t logger = sudoers_error_hook;
143
5.51k
    debug_decl(sudoers_reinit_defaults, SUDOERS_DEBUG_PLUGIN);
144
145
5.51k
    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.51k
    if (!update_defaults(ctx, NULL, &initial_defaults,
152
5.51k
      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.51k
    sudoers_error_hook = NULL;
157
158
5.51k
    TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
159
  /* Missing/invalid defaults is not a fatal error. */
160
5.51k
  if (nss->getdefs(ctx, nss) != -1) {
161
5.51k
      (void)update_defaults(ctx, nss->parse_tree, NULL,
162
5.51k
    SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, true);
163
5.51k
  }
164
5.51k
    }
165
166
    /* Restore error logging. */
167
5.51k
    sudoers_error_hook = logger;
168
169
    /* No need to check the admin flag file multiple times. */
170
5.51k
    if (ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
171
0
  free(def_admin_flag);
172
0
  def_admin_flag = NULL;
173
0
    }
174
175
5.51k
    debug_return_bool(true);
176
5.51k
}
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
23.8k
{
187
23.8k
    struct sudo_nss *nss, *nss_next;
188
23.8k
    int oldlocale, sources = 0;
189
23.8k
    static int ret;
190
23.8k
    debug_decl(sudoers_init, SUDOERS_DEBUG_PLUGIN);
191
192
    /* Only attempt to initialize once. */
193
23.8k
    if (sudoers_initialized)
194
0
  debug_return_int(ret);
195
23.8k
    sudoers_initialized = true;
196
23.8k
    ret = -1;
197
198
23.8k
    bindtextdomain("sudoers", LOCALEDIR);
199
200
    /* Hook up logging function for parse errors. */
201
23.8k
    sudoers_error_hook = logger;
202
203
    /* Register fatal/fatalx callback. */
204
23.8k
    sudo_fatal_callback_register(sudoers_cleanup);
205
206
    /* Initialize environment functions (including replacements). */
207
23.8k
    if (!env_init(envp))
208
0
  debug_return_int(-1);
209
210
    /* Setup defaults data structures. */
211
23.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
23.8k
    sudoers_ctx.mode = sudoers_policy_deserialize_info(&sudoers_ctx, info,
218
23.8k
  &initial_defaults);
219
23.8k
    if (ISSET(sudoers_ctx.mode, MODE_ERROR))
220
4.82k
  debug_return_int(-1);
221
222
18.9k
    if (!init_vars(&sudoers_ctx, envp))
223
313
  debug_return_int(-1);
224
225
    /* Parse nsswitch.conf for sudoers order. */
226
18.6k
    snl = sudo_read_nss();
227
228
    /* LDAP or NSS may modify the euid so we need to be root for the open. */
229
18.6k
    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
18.6k
    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
234
18.6k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
235
236
    /* Update defaults set by front-end. */
237
18.6k
    if (!update_defaults(&sudoers_ctx, NULL, &initial_defaults,
238
18.6k
      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
18.6k
    TAILQ_FOREACH_SAFE(nss, snl, entries, nss_next) {
244
18.6k
  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
18.6k
  sources++;
249
250
  /* Missing/invalid defaults is not a fatal error. */
251
18.6k
  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
18.6k
  } else {
255
18.6k
      (void)update_defaults(&sudoers_ctx, nss->parse_tree, NULL,
256
18.6k
    SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER|SETDEF_RUNAS, false);
257
18.6k
  }
258
18.6k
    }
259
18.6k
    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
18.6k
    if (set_loginclass(&sudoers_ctx))
268
18.6k
  ret = true;
269
270
18.6k
cleanup:
271
18.6k
    mail_parse_errors(&sudoers_ctx);
272
273
18.6k
    if (!restore_perms())
274
0
  ret = -1;
275
276
    /* Restore user's locale. */
277
18.6k
    sudo_warn_set_locale_func(NULL);
278
18.6k
    sudoers_setlocale(oldlocale, NULL);
279
280
18.6k
    debug_return_int(ret);
281
18.6k
}
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.50k
{
291
2.50k
    char dir[PATH_MAX], file[PATH_MAX];
292
2.50k
    char *iolog_path = NULL;
293
2.50k
    int oldlocale;
294
2.50k
    bool ok;
295
2.50k
    debug_decl(format_iolog_path, SUDOERS_DEBUG_PLUGIN);
296
297
    /* Use sudoers locale for strftime() */
298
2.50k
    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
299
2.50k
    ok = expand_iolog_path(def_iolog_dir, dir, sizeof(dir),
300
2.50k
  &sudoers_iolog_path_escapes[1], ctx);
301
2.50k
    if (ok) {
302
2.50k
  ctx->iolog_dir = dir;
303
2.50k
  ok = expand_iolog_path(def_iolog_file, file, sizeof(file),
304
2.50k
      &sudoers_iolog_path_escapes[0], ctx);
305
2.50k
  ctx->iolog_dir = NULL;
306
2.50k
    }
307
2.50k
    sudoers_setlocale(oldlocale, NULL);
308
2.50k
    if (!ok)
309
0
  goto done;
310
311
2.50k
    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.50k
    ctx->iolog_path = iolog_path + sizeof("iolog_path=") - 1;
318
2.50k
    ctx->iolog_file = ctx->iolog_path + 1 + strlen(dir);
319
320
2.50k
done:
321
2.50k
    debug_return_str(iolog_path);
322
2.50k
}
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
25.6k
{
350
25.6k
    struct sudoers_match_info match_info = { NULL };
351
25.6k
    int oldlocale, ret = -1;
352
25.6k
    unsigned int validated;
353
25.6k
    time_t now;
354
25.6k
    debug_decl(sudoers_check_common, SUDOERS_DEBUG_PLUGIN);
355
356
    /* The user may only specify a host for "sudo -l". */
357
25.6k
    if (!ISSET(ctx->mode, MODE_LIST|MODE_CHECK)) {
358
16.0k
  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.0k
    }
367
368
    /* If given the -P option, set the "preserve_groups" flag. */
369
25.6k
    if (ISSET(ctx->mode, MODE_PRESERVE_GROUPS))
370
74
  def_preserve_groups = true;
371
372
    /* Find command in path and apply per-command Defaults. */
373
25.6k
    cmnd_status = set_cmnd(ctx);
374
25.6k
    if (cmnd_status == NOT_FOUND_ERROR)
375
0
  goto done;
376
377
    /* Is root even allowed to run sudo? */
378
25.6k
    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
25.6k
    if (ctx->user.closefrom >= 0 && ctx->user.closefrom != def_closefrom) {
388
1.11k
  if (!def_closefrom_override) {
389
1.11k
      log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
390
1.11k
    N_("user not allowed to override closefrom limit"));
391
1.11k
      sudo_warnx("%s", U_("you are not permitted to use the -C option"));
392
1.11k
      goto bad;
393
1.11k
  }
394
0
  def_closefrom = ctx->user.closefrom;
395
0
    }
396
397
    /*
398
     * Check sudoers sources, using the locale specified in sudoers.
399
     */
400
24.5k
    time(&now);
401
24.5k
    sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
402
24.5k
    validated = sudoers_lookup(snl, ctx, now, cb_lookup, &match_info,
403
24.5k
  &cmnd_status, pwflag);
404
24.5k
    sudoers_setlocale(oldlocale, NULL);
405
24.5k
    if (ISSET(validated, VALIDATE_ERROR)) {
406
  /* The lookup function should have printed an error. */
407
0
  goto done;
408
0
    }
409
410
24.5k
    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
24.5k
    if (ctx->runas.cmnd == NULL) {
426
24.5k
  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
24.5k
    }
431
432
    /* Defer uid/gid checks until after defaults have been updated. */
433
24.5k
    if (unknown_runas_uid && !def_runas_allow_unknown_id) {
434
96
  log_warningx(ctx, SLOG_AUDIT, N_("unknown user %s"),
435
96
      ctx->runas.pw->pw_name);
436
96
  goto done;
437
96
    }
438
24.4k
    if (ctx->runas.gr != NULL) {
439
298
  if (unknown_runas_gid && !def_runas_allow_unknown_id) {
440
37
      log_warningx(ctx, SLOG_AUDIT, N_("unknown group %s"),
441
37
    ctx->runas.gr->gr_name);
442
37
      goto done;
443
37
  }
444
298
    }
445
446
    /* If no command line args and "shell_noargs" is not set, error out. */
447
24.4k
    if (ISSET(ctx->mode, MODE_IMPLIED_SHELL) && !def_shell_noargs) {
448
  /* Not an audit event. */
449
42
  ret = -2; /* usage error */
450
42
  goto done;
451
42
    }
452
453
    /* Bail if a tty is required and we don't have one. */
454
24.3k
    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
24.3k
    if (ISSET(ctx->mode, MODE_RUN|MODE_CHECK)) {
462
14.2k
  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
14.2k
    }
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
24.3k
    if (ISSET(ctx->mode, MODE_EDIT) ||
475
23.3k
  (ISSET(ctx->mode, MODE_PRESERVE_ENV) && def_setenv))
476
1.00k
  def_env_reset = false;
477
478
    /* Build a new environment that avoids any nasty bits. */
479
24.3k
    if (!rebuild_env(ctx))
480
0
  goto bad;
481
482
    /* Require a password if sudoers says so.  */
483
24.3k
    switch (check_user(ctx, validated, ctx->mode)) {
484
24.3k
    case AUTH_SUCCESS:
485
  /* user authenticated successfully. */
486
24.3k
  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
24.3k
    }
499
500
    /* Check whether ctx->runas.chroot is permitted (if specified). */
501
24.3k
    switch (check_user_runchroot(ctx->runas.chroot)) {
502
24.3k
    case true:
503
24.3k
  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
24.3k
    }
514
515
    /* Check whether ctx->runas.cwd is permitted (if specified). */
516
24.3k
    switch (check_user_runcwd(ctx->runas.cwd)) {
517
24.3k
    case true:
518
24.3k
  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
24.3k
    }
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
24.3k
    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
24.3k
    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
24.3k
    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
24.3k
    if (create_admin_success_flag(ctx) == -1)
570
0
  goto done;
571
572
    /* Finally tell the user if the command did not exist. */
573
24.3k
    if (cmnd_status == NOT_FOUND_DOT) {
574
3.31k
  audit_failure(ctx, ctx->runas.argv, N_("command in current directory"));
575
3.31k
  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.31k
  goto bad;
577
21.0k
    } else if (cmnd_status == NOT_FOUND) {
578
1.71k
  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.71k
  } else {
583
1.71k
      audit_failure(ctx, ctx->runas.argv, N_("%s: command not found"),
584
1.71k
    ctx->user.cmnd);
585
1.71k
      sudo_warnx(U_("%s: command not found"), ctx->user.cmnd);
586
1.71k
      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.71k
  }
596
1.71k
  goto bad;
597
1.71k
    }
598
599
    /* If user specified a timeout make sure sudoers allows it. */
600
19.3k
    if (!def_user_command_timeouts && ctx->user.timeout > 0) {
601
696
  log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
602
696
      N_("user not allowed to set a command timeout"));
603
696
  sudo_warnx("%s",
604
696
      U_("sorry, you are not allowed set a command timeout"));
605
696
  goto bad;
606
696
    }
607
608
    /* If user specified env vars make sure sudoers allows it. */
609
18.6k
    if (ISSET(ctx->mode, MODE_RUN) && !def_setenv) {
610
6.16k
  if (ISSET(ctx->mode, MODE_PRESERVE_ENV)) {
611
32
      log_warningx(ctx, SLOG_NO_STDERR|SLOG_AUDIT,
612
32
    N_("user not allowed to preserve the environment"));
613
32
      sudo_warnx("%s",
614
32
    U_("sorry, you are not allowed to preserve the environment"));
615
32
      goto bad;
616
6.13k
  } else {
617
6.13k
      if (!validate_env_vars(ctx, ctx->user.env_add))
618
1.66k
    goto bad;
619
6.13k
  }
620
6.16k
    }
621
622
16.9k
    ret = true;
623
16.9k
    goto done;
624
625
8.53k
bad:
626
8.53k
    ret = false;
627
25.6k
done:
628
25.6k
    debug_return_int(ret);
629
25.6k
}
630
631
static bool need_reinit;
632
633
/*
634
 * Check whether the user is allowed to run the specified command.
635
 * Returns true if allowed, false if denied, -1 on error and
636
 * -2 for usage error.
637
 */
638
int
639
sudoers_check_cmnd(int argc, char * const argv[], char *env_add[],
640
    void *closure)
641
12.9k
{
642
12.9k
    char *iolog_path = NULL;
643
12.9k
    mode_t cmnd_umask = ACCESSPERMS;
644
12.9k
    int ret = -1;
645
12.9k
    debug_decl(sudoers_check_cmnd, SUDOERS_DEBUG_PLUGIN);
646
647
12.9k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
648
649
12.9k
    if (argc == 0) {
650
0
  sudo_warnx("%s", U_("no command specified"));
651
0
  debug_return_int(-1);
652
0
    }
653
654
12.9k
    if (need_reinit) {
655
  /* Was previous command intercepted? */
656
5.51k
  if (ISSET(sudoers_ctx.mode, MODE_RUN) && def_intercept)
657
0
      SET(sudoers_ctx.mode, MODE_POLICY_INTERCEPTED);
658
659
  /* Only certain mode flags are legal for intercepted commands. */
660
5.51k
  if (ISSET(sudoers_ctx.mode, MODE_POLICY_INTERCEPTED))
661
0
      sudoers_ctx.mode &= MODE_INTERCEPT_MASK;
662
663
  /* Re-initialize defaults if we are called multiple times. */
664
5.51k
  if (!sudoers_reinit_defaults(&sudoers_ctx))
665
0
      debug_return_int(-1);
666
5.51k
    }
667
12.9k
    need_reinit = true;
668
669
12.9k
    unlimit_nproc();
670
671
12.9k
    if (!set_perms(&sudoers_ctx, PERM_INITIAL))
672
0
  goto bad;
673
674
    /* Environment variables specified on the command line. */
675
12.9k
    if (env_add != NULL && env_add[0] != NULL)
676
5.39k
  sudoers_ctx.user.env_add = env_add;
677
678
    /*
679
     * Make a local copy of argc/argv, with special handling for the
680
     * '-i' option.  We also allocate an extra slot for bash's --login.
681
     */
682
12.9k
    if (sudoers_ctx.runas.argv != NULL && sudoers_ctx.runas.argv != sudoers_ctx.runas.argv_saved) {
683
3.06k
  sudoers_gc_remove(GC_PTR, sudoers_ctx.runas.argv);
684
3.06k
  free(sudoers_ctx.runas.argv);
685
3.06k
    }
686
12.9k
    sudoers_ctx.runas.argv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
687
12.9k
    if (sudoers_ctx.runas.argv == NULL) {
688
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
689
0
  goto error;
690
0
    }
691
12.9k
    sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
692
12.9k
    memcpy(sudoers_ctx.runas.argv, argv, (size_t)argc * sizeof(char *));
693
12.9k
    sudoers_ctx.runas.argc = argc;
694
12.9k
    sudoers_ctx.runas.argv[sudoers_ctx.runas.argc] = NULL;
695
12.9k
    if (ISSET(sudoers_ctx.mode, MODE_LOGIN_SHELL) && sudoers_ctx.runas.pw != NULL) {
696
322
  sudoers_ctx.runas.argv[0] = strdup(sudoers_ctx.runas.pw->pw_shell);
697
322
  if (sudoers_ctx.runas.argv[0] == NULL) {
698
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
699
0
      goto error;
700
0
  }
701
322
  sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv[0]);
702
322
    }
703
12.9k
    if (ISSET(sudoers_ctx.mode, MODE_EDIT)) {
704
684
  int i;
705
706
  /*
707
   * Fully-qualify sudoedit file names if necessary.
708
   * We don't currently support -D or CWD for sudoedit
709
   * so this uses the user's current working directory.
710
   */
711
411k
  for (i = 1; i < sudoers_ctx.runas.argc; i++) {
712
411k
      if (*sudoers_ctx.runas.argv[i] != '/') {
713
411k
    char *cp;
714
715
411k
    if (sudoers_ctx.user.cwd == NULL) {
716
125
        sudo_warnx("%s",
717
125
      U_("unable to get current working directory"));
718
125
        goto error;
719
125
    }
720
411k
    if (asprintf(&cp, "%s/%s", sudoers_ctx.user.cwd,
721
411k
      sudoers_ctx.runas.argv[i]) == -1) {
722
0
        sudo_warnx(U_("%s: %s"), __func__,
723
0
      U_("unable to allocate memory"));
724
0
        goto error;
725
0
    }
726
411k
    sudoers_gc_add(GC_PTR, cp);
727
411k
    sudoers_ctx.runas.argv[i] = cp;
728
411k
      }
729
      /* Remove consecutive '/' and "/./" path elements. */
730
411k
      rationalize_path(sudoers_ctx.runas.argv[i]);
731
411k
  }
732
684
    }
733
734
12.8k
    ret = sudoers_check_common(&sudoers_ctx, 0);
735
12.8k
    if (ret != true)
736
7.60k
  goto done;
737
738
5.23k
    if (!remote_iologs) {
739
2.50k
  if (iolog_enabled && def_iolog_file && def_iolog_dir) {
740
2.50k
      if ((iolog_path = format_iolog_path(&sudoers_ctx)) == NULL) {
741
0
    if (!def_ignore_iolog_errors)
742
0
        goto error;
743
    /* Unable to expand I/O log path, disable I/O logging. */
744
0
    def_log_input = false;
745
0
    def_log_output = false;
746
0
    def_log_stdin = false;
747
0
    def_log_stdout = false;
748
0
    def_log_stderr = false;
749
0
    def_log_ttyin = false;
750
0
    def_log_ttyout = false;
751
0
      }
752
2.50k
  }
753
2.50k
    }
754
755
    /*
756
     * Set umask based on sudoers.
757
     * If user's umask is more restrictive, OR in those bits too
758
     * unless umask_override is set.
759
     */
760
5.23k
    if (def_umask != ACCESSPERMS) {
761
5.23k
  cmnd_umask = def_umask;
762
5.23k
  if (!def_umask_override)
763
5.23k
      cmnd_umask |= sudoers_ctx.user.umask;
764
5.23k
    }
765
766
5.23k
    if (ISSET(sudoers_ctx.mode, MODE_LOGIN_SHELL)) {
767
148
  char *p;
768
769
  /* Convert /bin/sh -> -sh so shell knows it is a login shell */
770
148
  if ((p = strrchr(sudoers_ctx.runas.argv[0], '/')) == NULL)
771
0
      p = sudoers_ctx.runas.argv[0];
772
148
  *p = '-';
773
148
  sudoers_ctx.runas.argv[0] = p;
774
775
  /*
776
   * Newer versions of bash require the --login option to be used
777
   * in conjunction with the -c option even if the shell name starts
778
   * with a '-'.  Unfortunately, bash 1.x uses -login, not --login
779
   * so this will cause an error for that.
780
   */
781
148
  if (sudoers_ctx.runas.argc > 1 && strcmp(sudoers_ctx.runas.argv[0], "-bash") == 0 &&
782
92
      strcmp(sudoers_ctx.runas.argv[1], "-c") == 0) {
783
      /* We allocated extra space for the --login above. */
784
0
      memmove(&sudoers_ctx.runas.argv[2], &sudoers_ctx.runas.argv[1],
785
0
    (size_t)sudoers_ctx.runas.argc * sizeof(char *));
786
0
      sudoers_ctx.runas.argv[1] = (char *)"--login";
787
0
      sudoers_ctx.runas.argc++;
788
0
  }
789
790
148
#ifdef _PATH_ENVIRONMENT
791
  /* Insert system-wide environment variables. */
792
148
  if (!read_env_file(&sudoers_ctx, _PATH_ENVIRONMENT, true, false))
793
0
      sudo_warn("%s", _PATH_ENVIRONMENT);
794
148
#endif
795
#ifdef HAVE_LOGIN_CAP_H
796
  /* Set environment based on login class. */
797
  if (sudoers_ctx.runas.class) {
798
      login_cap_t *lc = login_getclass(sudoers_ctx.runas.class);
799
      if (lc != NULL) {
800
    setusercontext(lc, sudoers_ctx.runas.pw,
801
        sudoers_ctx.runas.pw->pw_uid, LOGIN_SETPATH|LOGIN_SETENV);
802
    login_close(lc);
803
      }
804
  }
805
#endif /* HAVE_LOGIN_CAP_H */
806
148
    }
807
808
    /* Insert system-wide environment variables. */
809
5.23k
    if (def_restricted_env_file) {
810
5.23k
  if (!read_env_file(&sudoers_ctx, def_restricted_env_file, false, true))
811
0
      sudo_warn("%s", def_restricted_env_file);
812
5.23k
    }
813
5.23k
    if (def_env_file) {
814
5.23k
  if (!read_env_file(&sudoers_ctx, def_env_file, false, false))
815
0
      sudo_warn("%s", def_env_file);
816
5.23k
    }
817
818
    /* Insert user-specified environment variables. */
819
5.23k
    if (!insert_env_vars(sudoers_ctx.user.env_add)) {
820
130
  sudo_warnx("%s",
821
130
      U_("error setting user-specified environment variables"));
822
130
  goto error;
823
130
    }
824
825
    /* Note: must call audit before uid change. */
826
5.10k
    if (ISSET(sudoers_ctx.mode, MODE_EDIT)) {
827
726
  const char *env_editor = NULL;
828
726
  char **edit_argv;
829
726
  int edit_argc;
830
831
726
  sudoers_ctx.sudoedit_nfiles = sudoers_ctx.runas.argc - 1;
832
726
  free(sudoers_ctx.runas.cmnd);
833
726
  sudoers_ctx.runas.cmnd = find_editor(sudoers_ctx.sudoedit_nfiles,
834
726
      sudoers_ctx.runas.argv + 1, &edit_argc, &edit_argv, NULL, &env_editor);
835
726
  if (sudoers_ctx.runas.cmnd == NULL) {
836
208
      switch (errno) {
837
208
      case ENOENT:
838
208
    audit_failure(&sudoers_ctx, sudoers_ctx.runas.argv,
839
208
        N_("%s: command not found"),
840
208
        env_editor ? env_editor : def_editor);
841
208
    sudo_warnx(U_("%s: command not found"),
842
208
        env_editor ? env_editor : def_editor);
843
208
    goto error;
844
0
      case EINVAL:
845
0
    if (def_env_editor && env_editor != NULL) {
846
        /* User tried to do something funny with the editor. */
847
0
        log_warningx(&sudoers_ctx,
848
0
      SLOG_NO_STDERR|SLOG_AUDIT|SLOG_SEND_MAIL,
849
0
      "invalid user-specified editor: %s", env_editor);
850
0
        goto error;
851
0
    }
852
0
    FALLTHROUGH;
853
0
      default:
854
0
    goto error;
855
208
      }
856
208
  }
857
  /* find_editor() already g/c'd edit_argv[] */
858
518
  if (sudoers_ctx.runas.argv != sudoers_ctx.runas.argv_saved) {
859
518
      sudoers_gc_remove(GC_PTR, sudoers_ctx.runas.argv);
860
518
      free(sudoers_ctx.runas.argv);
861
518
  }
862
518
  sudoers_ctx.runas.argv = edit_argv;
863
518
  sudoers_ctx.runas.argc = edit_argc;
864
865
  /* We want to run the editor with the unmodified environment. */
866
518
  env_swap_old();
867
518
    }
868
869
    /* Save the initial command and argv so we have it for exit logging. */
870
4.89k
    if (sudoers_ctx.runas.cmnd_saved == NULL) {
871
2.50k
  sudoers_ctx.runas.cmnd_saved = strdup(sudoers_ctx.runas.cmnd);
872
2.50k
  if (sudoers_ctx.runas.cmnd_saved == NULL) {
873
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
874
0
      goto error;
875
0
  }
876
2.50k
  sudoers_ctx.runas.argv_saved = sudoers_ctx.runas.argv;
877
2.50k
    }
878
879
4.89k
    ret = true;
880
4.89k
    goto done;
881
882
0
bad:
883
0
    ret = false;
884
0
    goto done;
885
886
463
error:
887
463
    ret = -1;
888
889
12.9k
done:
890
12.9k
    mail_parse_errors(&sudoers_ctx);
891
892
12.9k
    if (def_group_plugin)
893
0
  group_plugin_unload();
894
12.9k
    reset_parser();
895
896
12.9k
    if (ret == -1) {
897
  /* Free locally-allocated strings. */
898
540
  free(iolog_path);
899
12.4k
    } else {
900
  /* Store settings to pass back to front-end. */
901
12.4k
  if (!sudoers_policy_store_result(&sudoers_ctx, ret,
902
12.4k
      sudoers_ctx.runas.argv, env_get(), cmnd_umask, iolog_path, closure))
903
0
      ret = -1;
904
12.4k
    }
905
906
    /* Zero out stashed copy of environment, it is owned by the front-end. */
907
12.9k
    (void)env_init(NULL);
908
909
12.9k
    if (!rewind_perms())
910
0
  ret = -1;
911
912
12.9k
    restore_nproc();
913
914
12.9k
    sudo_warn_set_locale_func(NULL);
915
916
12.9k
    debug_return_int(ret);
917
12.9k
}
918
919
/*
920
 * Validate the user and update their timestamp file entry.
921
 * Returns true if allowed, false if denied, -1 on error and
922
 * -2 for usage error.
923
 */
924
int
925
sudoers_validate_user(void)
926
3.20k
{
927
3.20k
    int ret = -1;
928
3.20k
    debug_decl(sudoers_validate_user, SUDOERS_DEBUG_PLUGIN);
929
930
3.20k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
931
932
3.20k
    unlimit_nproc();
933
934
3.20k
    if (!set_perms(&sudoers_ctx, PERM_INITIAL))
935
0
  goto done;
936
937
3.20k
    sudoers_ctx.runas.argv = reallocarray(NULL, 2, sizeof(char *));
938
3.20k
    if (sudoers_ctx.runas.argv == NULL) {
939
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
940
0
  goto done;
941
0
    }
942
3.20k
    sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
943
3.20k
    sudoers_ctx.runas.argv[0] = (char *)"validate";
944
3.20k
    sudoers_ctx.runas.argv[1] = NULL;
945
3.20k
    sudoers_ctx.runas.argc = 2;
946
947
3.20k
    ret = sudoers_check_common(&sudoers_ctx, I_VERIFYPW);
948
949
3.20k
done:
950
3.20k
    mail_parse_errors(&sudoers_ctx);
951
952
3.20k
    if (def_group_plugin)
953
0
  group_plugin_unload();
954
3.20k
    reset_parser();
955
3.20k
    env_init(NULL);
956
957
3.20k
    if (!rewind_perms())
958
0
  ret = -1;
959
960
3.20k
    restore_nproc();
961
962
3.20k
    sudo_warn_set_locale_func(NULL);
963
964
3.20k
    debug_return_int(ret);
965
3.20k
}
966
967
/*
968
 * List a user's privileges or check whether a specific command may be run.
969
 * Returns true if allowed, false if denied, -1 on error and
970
 * -2 for usage error.
971
 */
972
int
973
sudoers_list(int argc, char * const argv[], const char *list_user, int verbose)
974
9.60k
{
975
9.60k
    struct passwd *pw;
976
9.60k
    int ret = -1;
977
9.60k
    debug_decl(sudoers_list, SUDOERS_DEBUG_PLUGIN);
978
979
9.60k
    sudo_warn_set_locale_func(sudoers_warn_setlocale);
980
981
9.60k
    unlimit_nproc();
982
983
9.60k
    if (!set_perms(&sudoers_ctx, PERM_INITIAL))
984
0
  goto done;
985
986
9.60k
    if (list_user) {
987
3.20k
  if (sudoers_ctx.runas.list_pw != NULL)
988
1.60k
      sudo_pw_delref(sudoers_ctx.runas.list_pw);
989
3.20k
  sudoers_ctx.runas.list_pw = sudo_getpwnam(list_user);
990
3.20k
  if (sudoers_ctx.runas.list_pw == NULL) {
991
0
      sudo_warnx(U_("unknown user %s"), list_user);
992
0
      goto done;
993
0
  }
994
3.20k
    }
995
996
9.60k
    sudoers_ctx.runas.argv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
997
9.60k
    if (sudoers_ctx.runas.argv == NULL) {
998
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
999
0
  goto done;
1000
0
    }
1001
9.60k
    sudoers_gc_add(GC_PTR, sudoers_ctx.runas.argv);
1002
9.60k
    sudoers_ctx.runas.argv[0] = (char *)"list";
1003
9.60k
    if (argc != 0)
1004
3.20k
  memcpy(sudoers_ctx.runas.argv + 1, argv, (size_t)argc * sizeof(char *));
1005
9.60k
    sudoers_ctx.runas.argc = argc + 1;
1006
9.60k
    sudoers_ctx.runas.argv[sudoers_ctx.runas.argc] = NULL;
1007
1008
9.60k
    ret = sudoers_check_common(&sudoers_ctx, I_LISTPW);
1009
9.60k
    if (ret != true)
1010
822
  goto done;
1011
1012
8.78k
    pw = sudoers_ctx.runas.list_pw ? sudoers_ctx.runas.list_pw : sudoers_ctx.user.pw;
1013
8.78k
    if (ISSET(sudoers_ctx.mode, MODE_CHECK))
1014
2.92k
  ret = display_cmnd(&sudoers_ctx, snl, pw, verbose);
1015
5.85k
    else
1016
5.85k
  ret = display_privs(&sudoers_ctx, snl, pw, verbose);
1017
1018
9.60k
done:
1019
9.60k
    mail_parse_errors(&sudoers_ctx);
1020
1021
9.60k
    if (def_group_plugin)
1022
0
  group_plugin_unload();
1023
9.60k
    reset_parser();
1024
9.60k
    env_init(NULL);
1025
1026
9.60k
    if (!rewind_perms())
1027
0
  ret = -1;
1028
1029
9.60k
    restore_nproc();
1030
1031
9.60k
    sudo_warn_set_locale_func(NULL);
1032
1033
9.60k
    debug_return_int(ret);
1034
9.60k
}
1035
1036
/*
1037
 * Initialize timezone and fill in ctx->user.
1038
 */
1039
static bool
1040
init_vars(struct sudoers_context *ctx, char * const envp[])
1041
18.9k
{
1042
18.9k
    char * const * ep;
1043
18.9k
    bool unknown_user = false;
1044
18.9k
    debug_decl(init_vars, SUDOERS_DEBUG_PLUGIN);
1045
1046
18.9k
    if (!sudoers_initlocale(setlocale(LC_ALL, NULL), def_sudoers_locale)) {
1047
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1048
0
  debug_return_bool(false);
1049
0
    }
1050
1051
18.9k
#define MATCHES(s, v) \
1052
227k
    (strncmp((s), (v), sizeof(v) - 1) == 0 && (s)[sizeof(v) - 1] != '\0')
1053
1054
18.9k
    ctx->user.envp = envp;
1055
683k
    for (ep = ctx->user.envp; *ep; ep++) {
1056
664k
  switch (**ep) {
1057
18.9k
      case 'K':
1058
18.9k
    if (MATCHES(*ep, "KRB5CCNAME="))
1059
18.9k
        ctx->user.ccname = *ep + sizeof("KRB5CCNAME=") - 1;
1060
18.9k
    break;
1061
37.9k
      case 'P':
1062
37.9k
    if (MATCHES(*ep, "PATH="))
1063
18.9k
        ctx->user.path = *ep + sizeof("PATH=") - 1;
1064
37.9k
    break;
1065
94.9k
      case 'S':
1066
94.9k
    if (MATCHES(*ep, "SUDO_PROMPT=")) {
1067
        /* Don't override "sudo -p prompt" */
1068
18.9k
        if (ctx->user.prompt == NULL)
1069
18.1k
      ctx->user.prompt = *ep + sizeof("SUDO_PROMPT=") - 1;
1070
18.9k
        break;
1071
18.9k
    }
1072
75.9k
    if (MATCHES(*ep, "SUDO_USER="))
1073
0
        prev_user = *ep + sizeof("SUDO_USER=") - 1;
1074
75.9k
    break;
1075
664k
      }
1076
664k
    }
1077
18.9k
#undef MATCHES
1078
1079
18.9k
    if (ctx->user.pw == NULL) {
1080
  /* Fake a struct passwd for the call to log_warningx(). */
1081
199
  ctx->user.pw = sudo_mkpwent(ctx->user.name, ctx->user.uid,
1082
199
      ctx->user.gid, NULL, NULL);
1083
199
  unknown_user = true;
1084
199
    }
1085
18.9k
    if (ctx->user.gid_list == NULL)
1086
18.9k
  ctx->user.gid_list = sudo_get_gidlist(ctx->user.pw, ENTRY_TYPE_ANY);
1087
1088
    /* Store initialize permissions so we can restore them later. */
1089
18.9k
    if (!set_perms(ctx, PERM_INITIAL))
1090
0
  debug_return_bool(false);
1091
1092
    /* Set parse callbacks */
1093
18.9k
    set_callbacks();
1094
1095
    /* It is now safe to use log_warningx() and set_perms() */
1096
18.9k
    if (unknown_user) {
1097
199
  log_warningx(ctx, SLOG_SEND_MAIL, N_("unknown user %s"), ctx->user.name);
1098
199
  debug_return_bool(false);
1099
199
    }
1100
1101
    /*
1102
     * Set runas passwd/group entries based on command line or sudoers.
1103
     * Note that if runas_group was specified without runas_user we
1104
     * run the command as the invoking user.
1105
     */
1106
18.7k
    if (ctx->runas.group != NULL) {
1107
636
  if (!set_runasgr(ctx, ctx->runas.group, false))
1108
83
      debug_return_bool(false);
1109
553
  if (!set_runaspw(ctx, ctx->runas.user ?
1110
553
    ctx->runas.user : ctx->user.name, false))
1111
3
      debug_return_bool(false);
1112
18.1k
    } else {
1113
18.1k
  if (!set_runaspw(ctx, ctx->runas.user ?
1114
18.1k
    ctx->runas.user : def_runas_default, false))
1115
28
      debug_return_bool(false);
1116
18.1k
    }
1117
1118
18.6k
    debug_return_bool(true);
1119
18.6k
}
1120
1121
/*
1122
 * Fill in ctx->user.cmnd and ctx->user.cmnd_stat variables.
1123
 * Does not fill in ctx->user.cmnd_base.
1124
 */
1125
int
1126
set_cmnd_path(struct sudoers_context *ctx, const char *runchroot)
1127
20.9k
{
1128
20.9k
    const char *cmnd_in;
1129
20.9k
    char *cmnd_out = NULL;
1130
20.9k
    char *path = ctx->user.path;
1131
20.9k
    int ret;
1132
20.9k
    debug_decl(set_cmnd_path, SUDOERS_DEBUG_PLUGIN);
1133
1134
20.9k
    cmnd_in = ISSET(ctx->mode, MODE_CHECK) ?
1135
17.7k
  ctx->runas.argv[1] : ctx->runas.argv[0];
1136
1137
20.9k
    free(ctx->user.cmnd_list);
1138
20.9k
    ctx->user.cmnd_list = NULL;
1139
20.9k
    free(ctx->user.cmnd);
1140
20.9k
    ctx->user.cmnd = NULL;
1141
20.9k
    canon_path_free(ctx->user.cmnd_dir);
1142
20.9k
    ctx->user.cmnd_dir = NULL;
1143
20.9k
    if (def_secure_path && !user_is_exempt(ctx))
1144
0
  path = def_secure_path;
1145
1146
20.9k
    ret = resolve_cmnd(ctx, cmnd_in, &cmnd_out, path, runchroot);
1147
20.9k
    if (ret == FOUND) {
1148
13.8k
  char *slash = strrchr(cmnd_out, '/');
1149
13.8k
  if (slash != NULL) {
1150
13.8k
      *slash = '\0';
1151
13.8k
      ctx->user.cmnd_dir = canon_path(cmnd_out);
1152
13.8k
      if (ctx->user.cmnd_dir == NULL && errno == ENOMEM)
1153
0
    goto error;
1154
13.8k
      *slash = '/';
1155
13.8k
  }
1156
13.8k
    }
1157
1158
20.9k
    if (ISSET(ctx->mode, MODE_CHECK))
1159
3.20k
  ctx->user.cmnd_list = cmnd_out;
1160
17.7k
    else
1161
17.7k
  ctx->user.cmnd = cmnd_out;
1162
1163
20.9k
    debug_return_int(ret);
1164
0
error:
1165
0
    free(cmnd_out);
1166
0
    debug_return_int(NOT_FOUND_ERROR);
1167
0
}
1168
1169
/*
1170
 * Fill in ctx->user.cmnd, ctx->user.cmnd_stat and cmnd_status variables.
1171
 * Does not fill in ctx->user.cmnd_base.
1172
 */
1173
void
1174
set_cmnd_status(struct sudoers_context *ctx, const char *runchroot)
1175
5.49k
{
1176
5.49k
    cmnd_status = set_cmnd_path(ctx, runchroot);
1177
5.49k
}
1178
1179
/*
1180
 * Fill in ctx->user.cmnd, ctx->user.cmnd_args, ctx->user.cmnd_base and
1181
 * ctx->user.cmnd_stat variables and apply any command-specific defaults entries.
1182
 */
1183
static int
1184
set_cmnd(struct sudoers_context *ctx)
1185
25.6k
{
1186
25.6k
    struct sudo_nss *nss;
1187
25.6k
    int ret = FOUND;
1188
25.6k
    debug_decl(set_cmnd, SUDOERS_DEBUG_PLUGIN);
1189
1190
    /* Allocate ctx->user.cmnd_stat for find_path() and match functions. */
1191
25.6k
    free(ctx->user.cmnd_stat);
1192
25.6k
    ctx->user.cmnd_stat = calloc(1, sizeof(struct stat));
1193
25.6k
    if (ctx->user.cmnd_stat == NULL) {
1194
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1195
0
  debug_return_int(NOT_FOUND_ERROR);
1196
0
    }
1197
1198
    /* Re-initialize for when we are called multiple times. */
1199
25.6k
    free(ctx->runas.cmnd);
1200
25.6k
    ctx->runas.cmnd = NULL;
1201
1202
25.6k
    if (ISSET(ctx->mode, MODE_RUN|MODE_EDIT|MODE_CHECK)) {
1203
16.0k
  if (!ISSET(ctx->mode, MODE_EDIT)) {
1204
15.4k
      const char *runchroot = ctx->runas.chroot;
1205
15.4k
      if (runchroot == NULL && def_runchroot != NULL &&
1206
15.4k
        strcmp(def_runchroot, "*") != 0)
1207
15.4k
    runchroot = def_runchroot;
1208
1209
15.4k
      ret = set_cmnd_path(ctx, runchroot);
1210
15.4k
      if (ret == NOT_FOUND_ERROR) {
1211
0
    if (errno == ENAMETOOLONG) {
1212
0
        audit_failure(ctx, ctx->runas.argv, N_("command too long"));
1213
0
    }
1214
0
    log_warning(ctx, 0, "%s", ctx->runas.argv[0]);
1215
0
    debug_return_int(ret);
1216
0
      }
1217
15.4k
  }
1218
1219
  /* set ctx->user.cmnd_args */
1220
16.0k
  free(ctx->user.cmnd_args);
1221
16.0k
  ctx->user.cmnd_args = NULL;
1222
16.0k
  if (ISSET(ctx->mode, MODE_CHECK)) {
1223
3.20k
      if (ctx->runas.argc > 2) {
1224
    /* Skip the command being listed in ctx->runas.argv[1]. */
1225
308
    ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 2, ' ', NULL);
1226
308
    if (ctx->user.cmnd_args == NULL)
1227
0
        debug_return_int(NOT_FOUND_ERROR);
1228
308
      }
1229
12.8k
  } else if (ctx->runas.argc > 1) {
1230
1.67k
      if (ISSET(ctx->mode, MODE_SHELL|MODE_LOGIN_SHELL) &&
1231
586
        ISSET(ctx->mode, MODE_RUN)) {
1232
    /*
1233
     * When running a command via a shell, the sudo front-end
1234
     * escapes potential meta chars.  We unescape non-spaces
1235
     * for sudoers matching and logging purposes.
1236
     * TODO: move escaping to the policy plugin instead
1237
     */
1238
586
    ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 1, ' ',
1239
586
        strlcpy_unescape);
1240
1.08k
      } else {
1241
1.08k
    ctx->user.cmnd_args = strvec_join(ctx->runas.argv + 1, ' ',
1242
1.08k
        NULL);
1243
1.08k
      }
1244
1.67k
      if (ctx->user.cmnd_args == NULL)
1245
0
    debug_return_int(NOT_FOUND_ERROR);
1246
1.67k
  }
1247
16.0k
    }
1248
25.6k
    if (ctx->user.cmnd == NULL) {
1249
13.6k
  ctx->user.cmnd = strdup(ctx->runas.argv[0]);
1250
13.6k
  if (ctx->user.cmnd == NULL)
1251
0
      debug_return_int(NOT_FOUND_ERROR);
1252
13.6k
    }
1253
25.6k
    ctx->user.cmnd_base = sudo_basename(ctx->user.cmnd);
1254
1255
    /* Convert "sudo sudoedit" -> "sudoedit" */
1256
25.6k
    if (ISSET(ctx->mode, MODE_RUN) && strcmp(ctx->user.cmnd_base, "sudoedit") == 0) {
1257
456
  char *new_cmnd;
1258
1259
456
  CLR(ctx->mode, MODE_RUN);
1260
456
  SET(ctx->mode, MODE_EDIT);
1261
456
  sudo_warnx("%s", U_("sudoedit doesn't need to be run via sudo"));
1262
456
  if ((new_cmnd = strdup("sudoedit")) == NULL) {
1263
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1264
0
      debug_return_int(NOT_FOUND_ERROR);
1265
0
  }
1266
456
  free(ctx->user.cmnd);
1267
456
  ctx->user.cmnd_base = ctx->user.cmnd = new_cmnd;
1268
456
    }
1269
1270
25.6k
    TAILQ_FOREACH(nss, snl, entries) {
1271
  /* Missing/invalid defaults is not a fatal error. */
1272
25.6k
  (void)update_defaults(ctx, nss->parse_tree, NULL, SETDEF_CMND, false);
1273
25.6k
    }
1274
1275
25.6k
    debug_return_int(ret);
1276
25.6k
}
1277
1278
static int
1279
open_file(const char *path, int flags)
1280
0
{
1281
0
    int fd;
1282
0
    debug_decl(open_file, SUDOERS_DEBUG_PLUGIN);
1283
1284
0
    if (!set_perms(NULL, PERM_SUDOERS))
1285
0
  debug_return_int(-1);
1286
1287
0
    fd = open(path, flags);
1288
0
    if (fd == -1 && errno == EACCES && geteuid() != ROOT_UID) {
1289
  /*
1290
   * If we tried to open sudoers as non-root but got EACCES,
1291
   * try again as root.
1292
   */
1293
0
  int serrno = errno;
1294
0
  if (restore_perms() && set_perms(NULL, PERM_ROOT))
1295
0
      fd = open(path, flags);
1296
0
  errno = serrno;
1297
0
    }
1298
0
    if (!restore_perms()) {
1299
  /* unable to change back to root */
1300
0
  if (fd != -1) {
1301
0
      close(fd);
1302
0
      fd = -1;
1303
0
  }
1304
0
    }
1305
1306
0
    debug_return_int(fd);
1307
0
}
1308
1309
/*
1310
 * Open sudoers file and check mode/owner/type.
1311
 * Returns a handle to the sudoers file or NULL on error.
1312
 */
1313
FILE *
1314
open_sudoers(const char *path, char **outfile, bool doedit, bool *keepopen)
1315
0
{
1316
0
    char fnamebuf[PATH_MAX];
1317
0
    const char *fname = fnamebuf;
1318
0
    FILE *fp = NULL;
1319
0
    struct stat sb;
1320
0
    int error, fd;
1321
0
    debug_decl(open_sudoers, SUDOERS_DEBUG_PLUGIN);
1322
1323
0
    if (outfile == NULL) {
1324
  /* Single file, do not treat as a path. */
1325
0
  fname = path;
1326
0
  fd = open_file(fname, O_RDONLY|O_NONBLOCK);
1327
0
        if (fd != -1)
1328
0
            (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
1329
0
    } else {
1330
  /* Could be a colon-separated path of file names. */
1331
0
  fd = sudo_open_conf_path(path, fnamebuf, sizeof(fnamebuf), open_file);
1332
0
    }
1333
0
    if (sudoers_ctx.parser_conf.ignore_perms) {
1334
  /* Skip sudoers security checks when ignore_perms is set. */
1335
0
  if (fd == -1 || fstat(fd, &sb) == -1)
1336
0
      error = SUDO_PATH_MISSING;
1337
0
  else
1338
0
      error = SUDO_PATH_SECURE;
1339
0
    } else {
1340
0
  error = sudo_secure_fd(fd, S_IFREG, sudoers_file_uid(),
1341
0
      sudoers_file_gid(), &sb);
1342
0
    }
1343
0
    switch (error) {
1344
0
    case SUDO_PATH_SECURE:
1345
  /*
1346
   * Make sure we can read the file so we can present the
1347
   * user with a reasonable error message (unlike the lexer).
1348
   */
1349
0
  if ((fp = fdopen(fd, "r")) == NULL) {
1350
0
      log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
1351
0
    N_("unable to open %s"), fname);
1352
0
  } else {
1353
0
      fd = -1;
1354
0
      if (sb.st_size != 0 && fgetc(fp) == EOF) {
1355
0
    log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
1356
0
        N_("unable to read %s"), fname);
1357
0
    fclose(fp);
1358
0
    fp = NULL;
1359
0
      } else {
1360
    /* Rewind fp and set close on exec flag. */
1361
0
    rewind(fp);
1362
0
    (void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
1363
0
    if (outfile != NULL) {
1364
0
                    *outfile = sudo_rcstr_dup(fname);
1365
0
        if (*outfile == NULL) {
1366
0
      sudo_warnx(U_("%s: %s"), __func__,
1367
0
          U_("unable to allocate memory"));
1368
0
      fclose(fp);
1369
0
      fp = NULL;
1370
0
        }
1371
0
    }
1372
0
      }
1373
0
  }
1374
0
  break;
1375
0
    case SUDO_PATH_MISSING:
1376
0
  log_warning(&sudoers_ctx, SLOG_PARSE_ERROR,
1377
0
      N_("unable to open %s"), path);
1378
0
  break;
1379
0
    case SUDO_PATH_BAD_TYPE:
1380
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1381
0
      N_("%s is not a regular file"), fname);
1382
0
  break;
1383
0
    case SUDO_PATH_WRONG_OWNER:
1384
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1385
0
      N_("%s is owned by uid %u, should be %u"), fname,
1386
0
      (unsigned int)sb.st_uid, (unsigned int)sudoers_file_uid());
1387
0
  break;
1388
0
    case SUDO_PATH_WORLD_WRITABLE:
1389
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1390
0
      N_("%s is world writable"), fname);
1391
0
  break;
1392
0
    case SUDO_PATH_GROUP_WRITABLE:
1393
0
  log_warningx(&sudoers_ctx, SLOG_PARSE_ERROR,
1394
0
      N_("%s is owned by gid %u, should be %u"), fname,
1395
0
      (unsigned int)sb.st_gid, (unsigned int)sudoers_file_gid());
1396
0
  break;
1397
0
    default:
1398
0
  sudo_warnx("%s: internal error, unexpected error %d", __func__, error);
1399
0
  break;
1400
0
    }
1401
1402
0
    if (fp == NULL && fd != -1)
1403
0
  close(fd);
1404
1405
0
    debug_return_ptr(fp);
1406
0
}
1407
1408
#ifdef HAVE_LOGIN_CAP_H
1409
static bool
1410
set_loginclass(struct sudoers_context *ctx)
1411
{
1412
    const struct passwd *pw = ctx->runas.pw ? ctx->runas.pw : ctx->user.pw;
1413
    const unsigned int errflags = SLOG_RAW_MSG;
1414
    login_cap_t *lc;
1415
    bool ret = true;
1416
    debug_decl(set_loginclass, SUDOERS_DEBUG_PLUGIN);
1417
1418
    if (!def_use_loginclass)
1419
  goto done;
1420
1421
    if (ctx->runas.class && strcmp(ctx->runas.class, "-") != 0) {
1422
  if (ctx->user.uid != 0 && pw->pw_uid != 0) {
1423
      sudo_warnx(U_("only root can use \"-c %s\""), ctx->runas.class);
1424
      ret = false;
1425
      goto done;
1426
  }
1427
    } else {
1428
  ctx->runas.class = pw->pw_class;
1429
  if (!ctx->runas.class || !*ctx->runas.class)
1430
      ctx->runas.class = (char *)
1431
    ((pw->pw_uid == 0) ? LOGIN_DEFROOTCLASS : LOGIN_DEFCLASS);
1432
    }
1433
1434
    /* Make sure specified login class is valid. */
1435
    lc = login_getclass(ctx->runas.class);
1436
    if (!lc || !lc->lc_class || strcmp(lc->lc_class, ctx->runas.class) != 0) {
1437
  /*
1438
   * Don't make it an error if the user didn't specify the login
1439
   * class themselves.  We do this because if login.conf gets
1440
   * corrupted we want the admin to be able to use sudo to fix it.
1441
   */
1442
  log_warningx(ctx, errflags, N_("unknown login class %s"),
1443
      ctx->runas.class);
1444
  def_use_loginclass = false;
1445
  if (ctx->runas.class)
1446
      ret = false;
1447
    }
1448
    login_close(lc);
1449
done:
1450
    debug_return_bool(ret);
1451
}
1452
#else
1453
static bool
1454
set_loginclass(struct sudoers_context *ctx)
1455
18.6k
{
1456
18.6k
    return true;
1457
18.6k
}
1458
#endif /* HAVE_LOGIN_CAP_H */
1459
1460
/*
1461
 * Get passwd entry for the user we are going to run commands as
1462
 * and store it in ctx->runas.pw.  By default, commands run as "root".
1463
 */
1464
static bool
1465
set_runaspw(struct sudoers_context *ctx, const char *user, bool quiet)
1466
41.6k
{
1467
41.6k
    struct passwd *pw = NULL;
1468
41.6k
    debug_decl(set_runaspw, SUDOERS_DEBUG_PLUGIN);
1469
1470
41.6k
    unknown_runas_uid = false;
1471
41.6k
    if (*user == '#') {
1472
413
  const char *errstr;
1473
413
  uid_t uid = sudo_strtoid(user + 1, &errstr);
1474
413
  if (errstr == NULL) {
1475
410
      if ((pw = sudo_getpwuid(uid)) == NULL) {
1476
130
    unknown_runas_uid = true;
1477
130
    pw = sudo_fakepwnam(user, ctx->user.gid);
1478
130
      }
1479
410
  }
1480
413
    }
1481
41.6k
    if (pw == NULL) {
1482
41.2k
  if ((pw = sudo_getpwnam(user)) == NULL) {
1483
31
      if (!quiet)
1484
31
    log_warningx(ctx, SLOG_AUDIT, N_("unknown user %s"), user);
1485
31
      debug_return_bool(false);
1486
31
  }
1487
41.2k
    }
1488
41.6k
    if (ctx->runas.pw != NULL)
1489
22.9k
  sudo_pw_delref(ctx->runas.pw);
1490
41.6k
    ctx->runas.pw = pw;
1491
41.6k
    debug_return_bool(true);
1492
41.6k
}
1493
1494
/*
1495
 * Get group entry for the group we are going to run commands as
1496
 * and store it in ctx->runas.gr.
1497
 */
1498
static bool
1499
set_runasgr(struct sudoers_context *ctx, const char *group, bool quiet)
1500
636
{
1501
636
    struct group *gr = NULL;
1502
636
    debug_decl(set_runasgr, SUDOERS_DEBUG_PLUGIN);
1503
1504
636
    unknown_runas_gid = false;
1505
636
    if (*group == '#') {
1506
523
  const char *errstr;
1507
523
  gid_t gid = sudo_strtoid(group + 1, &errstr);
1508
523
  if (errstr == NULL) {
1509
522
      if ((gr = sudo_getgrgid(gid)) == NULL) {
1510
351
    unknown_runas_gid = true;
1511
351
    gr = sudo_fakegrnam(group);
1512
351
      }
1513
522
  }
1514
523
    }
1515
636
    if (gr == NULL) {
1516
114
  if ((gr = sudo_getgrnam(group)) == NULL) {
1517
83
      if (!quiet)
1518
83
    log_warningx(ctx, SLOG_AUDIT, N_("unknown group %s"), group);
1519
83
      debug_return_bool(false);
1520
83
  }
1521
114
    }
1522
553
    if (ctx->runas.gr != NULL)
1523
0
  sudo_gr_delref(ctx->runas.gr);
1524
553
    ctx->runas.gr = gr;
1525
553
    debug_return_bool(true);
1526
553
}
1527
1528
/*
1529
 * Callback for runas_default sudoers setting.
1530
 */
1531
bool
1532
cb_runas_default(struct sudoers_context *ctx, const char *file, int line,
1533
    int column, const union sudo_defs_val *sd_un, int op)
1534
24.1k
{
1535
24.1k
    debug_decl(cb_runas_default, SUDOERS_DEBUG_PLUGIN);
1536
1537
    /* Only reset runaspw if user didn't specify one. */
1538
24.1k
    if (ctx->runas.user == NULL && ctx->runas.group == NULL)
1539
22.9k
  debug_return_bool(set_runaspw(ctx, sd_un->str, true));
1540
1.26k
    debug_return_bool(true);
1541
1.26k
}
1542
1543
/*
1544
 * Cleanup hook for sudo_fatal()/sudo_fatalx()
1545
 * Also called at policy close time.
1546
 */
1547
void
1548
sudoers_cleanup(void)
1549
23.8k
{
1550
23.8k
    struct sudo_nss *nss;
1551
23.8k
    struct defaults *def;
1552
23.8k
    debug_decl(sudoers_cleanup, SUDOERS_DEBUG_PLUGIN);
1553
1554
23.8k
    if (snl != NULL) {
1555
18.6k
  TAILQ_FOREACH(nss, snl, entries) {
1556
18.6k
      nss->close(&sudoers_ctx, nss);
1557
18.6k
  }
1558
18.6k
  snl = NULL;
1559
18.6k
  reset_parser();
1560
18.6k
    }
1561
52.5k
    while ((def = TAILQ_FIRST(&initial_defaults)) != NULL) {
1562
28.7k
  TAILQ_REMOVE(&initial_defaults, def, entries);
1563
28.7k
  free(def->var);
1564
28.7k
  free(def->val);
1565
28.7k
  free(def);
1566
28.7k
    }
1567
23.8k
    sudoers_initialized = false;
1568
23.8k
    need_reinit = false;
1569
23.8k
    if (def_group_plugin)
1570
0
  group_plugin_unload();
1571
23.8k
    sudoers_ctx_free(&sudoers_ctx);
1572
23.8k
    sudo_freepwcache();
1573
23.8k
    sudo_freegrcache();
1574
23.8k
    canon_path_free_cache();
1575
1576
    /* We must free the cached environment before running g/c. */
1577
23.8k
    env_free();
1578
1579
    /* Run garbage collector. */
1580
23.8k
    sudoers_gc_run();
1581
1582
    /* Clear globals */
1583
23.8k
    prev_user = NULL;
1584
1585
23.8k
    debug_return;
1586
23.8k
}
1587
1588
bool
1589
sudoers_set_mode(unsigned int flags, unsigned int mask)
1590
31.7k
{
1591
31.7k
    SET(sudoers_ctx.mode, flags);
1592
31.7k
    return ((sudoers_ctx.mode & mask) == sudoers_ctx.mode);
1593
31.7k
}
1594
1595
const struct sudoers_context *
1596
sudoers_get_context(void)
1597
63.0k
{
1598
63.0k
    return &sudoers_ctx;
1599
63.0k
}
1600
1601
bool
1602
sudoers_set_log_format(enum def_tuple tuple)
1603
0
{
1604
0
    enum eventlog_format format;
1605
0
    debug_decl(cb_log_format, SUDOERS_DEBUG_PLUGIN);
1606
1607
    /* FFR - make "json" an alias for EVLOG_JSON_COMPACT instead. */
1608
0
    switch (tuple) {
1609
0
    case json_compact:
1610
0
        format = EVLOG_JSON_COMPACT;
1611
0
        break;
1612
0
    case json:
1613
0
    case json_pretty:
1614
0
        format = EVLOG_JSON_PRETTY;
1615
0
        break;
1616
0
    case sudo:
1617
0
        format = EVLOG_SUDO;
1618
0
        break;
1619
0
    default:
1620
0
  debug_return_bool(false);
1621
0
    }
1622
0
    eventlog_set_format(format);
1623
1624
    debug_return_bool(true);
1625
0
}