Coverage Report

Created: 2025-08-26 06:57

/src/sudo/plugins/sudoers/defaults.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 1999-2005, 2007-2023
5
 *  Todd C. Miller <Todd.Miller@sudo.ws>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 *
19
 * Sponsored in part by the Defense Advanced Research Projects
20
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22
 */
23
24
/*
25
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
26
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
27
 */
28
29
#include <config.h>
30
31
#include <sys/stat.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <unistd.h>
36
#include <ctype.h>
37
#include <errno.h>
38
#include <limits.h>
39
#include <syslog.h>
40
41
#include <sudoers.h>
42
#include <sudo_eventlog.h>
43
#include <sudo_iolog.h>
44
#include <gram.h>
45
46
static struct early_default early_defaults[] = {
47
    { I_IGNORE_UNKNOWN_DEFAULTS },
48
#ifdef FQDN
49
    { I_FQDN, true },
50
#else
51
    { I_FQDN },
52
#endif
53
    { I_MATCH_GROUP_BY_GID },
54
    { I_GROUP_PLUGIN },
55
    { I_RUNAS_DEFAULT },
56
    { I_SUDOERS_LOCALE },
57
    { -1 }
58
};
59
60
/*
61
 * Local prototypes.
62
 */
63
static bool store_int(const char *str, struct sudo_defs_types *def);
64
static bool store_list(const char *str, struct sudo_defs_types *def, int op);
65
static bool store_mode(const char *str, struct sudo_defs_types *def);
66
static int  store_str(const char *str, struct sudo_defs_types *def);
67
static bool store_syslogfac(const char *str, struct sudo_defs_types *def);
68
static bool store_syslogpri(const char *str, struct sudo_defs_types *def);
69
static bool store_timeout(const char *str, struct sudo_defs_types *def);
70
static bool store_tuple(const char *str, struct sudo_defs_types *def, int op);
71
static bool store_uint(const char *str, struct sudo_defs_types *def);
72
static bool store_timespec(const char *str, struct sudo_defs_types *def);
73
static bool store_rlimit(const char *str, struct sudo_defs_types *def);
74
static bool store_plugin(const char *str, struct sudo_defs_types *def, int op);
75
static bool list_op(const char *str, size_t, struct list_members *list, enum list_ops op);
76
static bool valid_path(const struct sudoers_context *ctx, const struct sudo_defs_types *def, const char *val, const char *file, int line, int column, bool quiet);
77
78
/*
79
 * Table describing compile-time and run-time options.
80
 */
81
#include <def_data.c>
82
83
/*
84
 * Print version and configure info.
85
 */
86
void
87
dump_defaults(void)
88
2.15k
{
89
2.15k
    const struct sudo_defs_types *cur;
90
2.15k
    const struct list_member *item;
91
2.15k
    const struct def_values *def;
92
2.15k
    const char *desc;
93
2.15k
    debug_decl(dump_defaults, SUDOERS_DEBUG_DEFAULTS);
94
95
353k
    for (cur = sudo_defs_table; cur->name; cur++) {
96
351k
  if (cur->desc) {
97
351k
      desc = _(cur->desc);
98
351k
      switch (cur->type & T_MASK) {
99
185k
    case T_FLAG:
100
185k
        if (cur->sd_un.flag)
101
97.0k
      sudo_printf(SUDO_CONV_INFO_MSG, "%s\n", desc);
102
185k
        break;
103
88.3k
    case T_STR:
104
112k
    case T_RLIMIT:
105
112k
        if (cur->sd_un.str) {
106
58.2k
      sudo_printf(SUDO_CONV_INFO_MSG, desc, cur->sd_un.str);
107
58.2k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
108
58.2k
        }
109
112k
        break;
110
2.15k
    case T_LOGFAC:
111
2.15k
        if (cur->sd_un.ival) {
112
2.15k
      sudo_printf(SUDO_CONV_INFO_MSG, desc,
113
2.15k
          sudo_logfac2str(cur->sd_un.ival));
114
2.15k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
115
2.15k
        }
116
2.15k
        break;
117
4.31k
    case T_LOGPRI:
118
4.31k
        if (cur->sd_un.ival) {
119
4.31k
      sudo_printf(SUDO_CONV_INFO_MSG, desc,
120
4.31k
          sudo_logpri2str(cur->sd_un.ival));
121
4.31k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
122
4.31k
        }
123
4.31k
        break;
124
2.15k
    case T_INT:
125
2.15k
        sudo_printf(SUDO_CONV_INFO_MSG, desc, cur->sd_un.ival);
126
2.15k
        sudo_printf(SUDO_CONV_INFO_MSG, "\n");
127
2.15k
        break;
128
6.46k
    case T_UINT:
129
6.46k
        sudo_printf(SUDO_CONV_INFO_MSG, desc, cur->sd_un.uival);
130
6.46k
        sudo_printf(SUDO_CONV_INFO_MSG, "\n");
131
6.46k
        break;
132
4.31k
    case T_TIMESPEC: {
133
        /* display timespec in minutes and 10ths of a minute */
134
4.31k
        const int min = cur->sd_un.tspec.tv_sec / 60;
135
4.31k
        int decimin =
136
4.31k
      (((cur->sd_un.tspec.tv_sec % 60) * 10) + 30) / 60;
137
4.31k
        decimin += cur->sd_un.tspec.tv_nsec / 100000000;
138
4.31k
        sudo_printf(SUDO_CONV_INFO_MSG, desc, min, decimin);
139
4.31k
        sudo_printf(SUDO_CONV_INFO_MSG, "\n");
140
4.31k
        break;
141
88.3k
    }
142
4.31k
    case T_MODE:
143
4.31k
        sudo_printf(SUDO_CONV_INFO_MSG, desc, cur->sd_un.mode);
144
4.31k
        sudo_printf(SUDO_CONV_INFO_MSG, "\n");
145
4.31k
        break;
146
10.7k
    case T_LIST:
147
10.7k
        if (!SLIST_EMPTY(&cur->sd_un.list)) {
148
10.7k
      sudo_printf(SUDO_CONV_INFO_MSG, "%s\n", desc);
149
122k
      SLIST_FOREACH(item, &cur->sd_un.list, entries) {
150
122k
          sudo_printf(SUDO_CONV_INFO_MSG,
151
122k
        "\t%s\n", item->value);
152
122k
      }
153
10.7k
        }
154
10.7k
        break;
155
4.31k
    case T_TIMEOUT:
156
4.31k
        if (cur->sd_un.ival) {
157
2.15k
      sudo_printf(SUDO_CONV_INFO_MSG, desc,
158
2.15k
          cur->sd_un.ival);
159
2.15k
      sudo_printf(SUDO_CONV_INFO_MSG, "\n");
160
2.15k
        }
161
4.31k
        break;
162
15.0k
    case T_TUPLE:
163
25.8k
        for (def = cur->values; def->sval; def++) {
164
25.8k
      if (cur->sd_un.tuple == def->nval) {
165
15.0k
          sudo_printf(SUDO_CONV_INFO_MSG, desc, def->sval);
166
15.0k
          break;
167
15.0k
      }
168
25.8k
        }
169
15.0k
        sudo_printf(SUDO_CONV_INFO_MSG, "\n");
170
15.0k
        break;
171
351k
      }
172
351k
  }
173
351k
    }
174
2.15k
    debug_return;
175
2.15k
}
176
177
static bool
178
defaults_warnx(const struct sudoers_context *ctx, const char *file, int line,
179
    int column, bool quiet, const char * restrict fmt, ...)
180
58.9k
{
181
58.9k
    va_list ap;
182
58.9k
    bool ret;
183
58.9k
    debug_decl(defaults_warnx, SUDOERS_DEBUG_DEFAULTS);
184
185
58.9k
    va_start(ap, fmt);
186
58.9k
    ret = parser_vwarnx(ctx, file, line, column, true, quiet, fmt, ap);
187
58.9k
    va_end(ap);
188
189
58.9k
    debug_return_bool(ret);
190
58.9k
}
191
192
/*
193
 * Find the index of the specified Defaults name in sudo_defs_table[]
194
 * On success, returns the matching index or -1 on failure.
195
 */
196
static int
197
find_default(const struct sudoers_context *ctx, const char *name,
198
    const char *file, int line, int column, bool quiet)
199
1.33M
{
200
1.33M
    int i;
201
1.33M
    debug_decl(find_default, SUDOERS_DEBUG_DEFAULTS);
202
203
104M
    for (i = 0; sudo_defs_table[i].name != NULL; i++) {
204
103M
  if (strcmp(name, sudo_defs_table[i].name) == 0)
205
1.26M
      debug_return_int(i);
206
103M
    }
207
73.0k
    if (!def_ignore_unknown_defaults) {
208
57.3k
  defaults_warnx(ctx, file, line, column, quiet,
209
57.3k
      N_("unknown defaults entry \"%s\""), name);
210
57.3k
    }
211
73.0k
    debug_return_int(-1);
212
73.0k
}
213
214
/*
215
 * Parse a defaults entry, storing the parsed entry in sd_un.
216
 * Returns true on success or false on failure.
217
 */
218
static bool
219
parse_default_entry(const struct sudoers_context *ctx,
220
    struct sudo_defs_types *def, const char *val, int op,
221
    const char *file, int line, int column, bool quiet)
222
1.26M
{
223
1.26M
    int rc;
224
1.26M
    debug_decl(parse_default_entry, SUDOERS_DEBUG_DEFAULTS);
225
226
1.26M
    if (file == NULL)
227
21.6k
  file = "front-end";
228
229
1.26M
    sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %s:%d:%d: %s=%s op=%d",
230
1.26M
  __func__, file, line, column, def->name, val ? val : "", op);
231
232
    /*
233
     * If no value specified, the boolean flag must be set for non-flags.
234
     * Only flags and tuples support boolean "true".
235
     */
236
1.26M
    if (val == NULL) {
237
581k
  switch (def->type & T_MASK) {
238
490
  case T_LOGFAC:
239
490
      if (op == true) {
240
    /* Use default syslog facility if none specified. */
241
240
    val = LOGFAC;
242
240
      }
243
490
      break;
244
496k
  case T_FLAG:
245
496k
      break;
246
0
  case T_TUPLE:
247
0
      if (ISSET(def->type, T_BOOL))
248
0
    break;
249
0
      FALLTHROUGH;
250
83.8k
  default:
251
83.8k
      if (!ISSET(def->type, T_BOOL) || op != false) {
252
10
    defaults_warnx(ctx, file, line, column, quiet,
253
10
        N_("no value specified for \"%s\""), def->name);
254
10
    debug_return_bool(false);
255
10
      }
256
581k
  }
257
581k
    }
258
259
    /* Only lists support append/remove. */
260
1.26M
    if ((op == '+' || op == '-') && (def->type & T_MASK) != T_LIST) {
261
0
  defaults_warnx(ctx, file, line, column, quiet,
262
0
      N_("invalid operator \"%c=\" for \"%s\""), op, def->name);
263
0
  debug_return_bool(false);
264
0
    }
265
266
1.26M
    switch (def->type & T_MASK) {
267
28.6k
  case T_LOGFAC:
268
28.6k
      rc = store_syslogfac(val, def);
269
28.6k
      break;
270
55.9k
  case T_LOGPRI:
271
55.9k
      rc = store_syslogpri(val, def);
272
55.9k
      break;
273
518k
  case T_STR:
274
518k
      if (val != NULL && ISSET(def->type, T_PATH|T_CHPATH)) {
275
210k
    if (!valid_path(ctx, def, val, file, line, column, quiet)) {
276
0
        rc = -1;
277
0
        break;
278
0
    }
279
210k
      }
280
518k
      rc = store_str(val, def);
281
518k
      break;
282
0
  case T_INT:
283
0
      rc = store_int(val, def);
284
0
      break;
285
55.9k
  case T_UINT:
286
55.9k
      rc = store_uint(val, def);
287
55.9k
      break;
288
55.9k
  case T_MODE:
289
55.9k
      rc = store_mode(val, def);
290
55.9k
      break;
291
496k
  case T_FLAG:
292
496k
      if (val != NULL) {
293
0
    defaults_warnx(ctx, file, line, column, quiet,
294
0
        N_("option \"%s\" does not take a value"), def->name);
295
0
    rc = -1;
296
0
    break;
297
0
      }
298
496k
      def->sd_un.flag = (bool)op;
299
496k
      rc = true;
300
496k
      break;
301
23.6k
  case T_LIST:
302
23.6k
      rc = store_list(val, def, op);
303
23.6k
      break;
304
23.6k
  case T_TIMEOUT:
305
23.6k
      rc = store_timeout(val, def);
306
23.6k
      break;
307
564
  case T_TUPLE:
308
564
      rc = store_tuple(val, def, op);
309
564
      break;
310
40
  case T_TIMESPEC:
311
40
      rc = store_timespec(val, def);
312
40
      break;
313
0
  case T_PLUGIN:
314
0
      rc = store_plugin(val, def, op);
315
0
      break;
316
2.02k
  case T_RLIMIT:
317
2.02k
      rc = store_rlimit(val, def);
318
2.02k
      break;
319
0
  default:
320
0
      defaults_warnx(ctx, file, line, column, quiet,
321
0
    N_("invalid Defaults type 0x%x for option \"%s\""),
322
0
    def->type, def->name);
323
0
      rc = -1;
324
0
      break;
325
1.26M
    }
326
1.26M
    if (rc == false) {
327
1.62k
  defaults_warnx(ctx, file, line, column, quiet,
328
1.62k
      N_("value \"%s\" is invalid for option \"%s\""), val, def->name);
329
1.62k
    }
330
331
1.26M
    debug_return_bool(rc == true);
332
1.26M
}
333
334
static struct early_default *
335
is_early_default(const char *name)
336
124k
{
337
124k
    struct early_default *early;
338
124k
    debug_decl(is_early_default, SUDOERS_DEBUG_DEFAULTS);
339
340
870k
    for (early = early_defaults; early->idx != -1; early++) {
341
746k
  if (strcmp(name, sudo_defs_table[early->idx].name) == 0)
342
232
      debug_return_ptr(early);
343
746k
    }
344
124k
    debug_return_ptr(NULL);
345
124k
}
346
347
static bool
348
run_callback(struct sudoers_context *ctx, const char *file, int line,
349
    int column, struct sudo_defs_types *def, int op)
350
1.25M
{
351
1.25M
    debug_decl(run_callback, SUDOERS_DEBUG_DEFAULTS);
352
353
1.25M
    if (def->callback == NULL)
354
560k
  debug_return_bool(true);
355
698k
    debug_return_bool(def->callback(ctx, file, line, column, &def->sd_un, op));
356
698k
}
357
358
/*
359
 * Sets/clears an entry in the defaults structure.
360
 * Runs the callback if present on success.
361
 */
362
bool
363
set_default(struct sudoers_context *ctx, const char *var, const char *val,
364
    int op, const char *file, int line, int column, bool quiet)
365
1.31M
{
366
1.31M
    int idx;
367
1.31M
    debug_decl(set_default, SUDOERS_DEBUG_DEFAULTS);
368
369
1.31M
    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
370
1.31M
  "%s: setting Defaults %s -> %s", __func__, var, val ? val : "false");
371
372
1.31M
    idx = find_default(ctx, var, file, line, column, quiet);
373
1.31M
    if (idx != -1) {
374
  /* Set parsed value in sudo_defs_table and run callback (if any). */
375
1.26M
  struct sudo_defs_types *def = &sudo_defs_table[idx];
376
1.26M
  if (parse_default_entry(ctx, def, val, op, file, line, column, quiet))
377
1.25M
      debug_return_bool(run_callback(ctx, file, line, column, def, op));
378
1.26M
    }
379
59.5k
    debug_return_bool(false);
380
59.5k
}
381
382
/*
383
 * Like set_default() but stores the matching default value
384
 * and does not run callbacks.
385
 */
386
static bool
387
set_early_default(const struct sudoers_context *ctx, const char *var,
388
    const char *val, int op, const char *file, int line, int column,
389
    bool quiet, struct early_default *early)
390
116
{
391
116
    int idx;
392
116
    debug_decl(set_early_default, SUDOERS_DEBUG_DEFAULTS);
393
394
116
    idx = find_default(ctx, var, file, line, column, quiet);
395
116
    if (idx != -1) {
396
  /* Set parsed value in sudo_defs_table but defer callback (if any). */
397
116
  struct sudo_defs_types *def = &sudo_defs_table[idx];
398
116
  if (parse_default_entry(ctx, def, val, op, file, line, column, quiet)) {
399
116
      if (early->file != NULL)
400
112
    sudo_rcstr_delref(early->file);
401
116
      early->file = sudo_rcstr_addref(file);
402
116
      early->line = line;
403
116
      early->column = column;
404
116
      early->run_callback = true;
405
116
      debug_return_bool(true);
406
116
  }
407
116
    }
408
0
    debug_return_bool(false);
409
0
}
410
411
/*
412
 * Run callbacks for early defaults.
413
 */
414
static bool
415
run_early_defaults(struct sudoers_context *ctx)
416
57.2k
{
417
57.2k
    struct early_default *early;
418
57.2k
    bool ret = true;
419
57.2k
    debug_decl(run_early_defaults, SUDOERS_DEBUG_DEFAULTS);
420
421
400k
    for (early = early_defaults; early->idx != -1; early++) {
422
343k
  if (early->run_callback) {
423
24
      if (!run_callback(ctx, early->file, early->line, early->column,
424
24
        &sudo_defs_table[early->idx], true))
425
0
    ret = false;
426
24
      early->run_callback = false;
427
24
  }
428
343k
    }
429
57.2k
    debug_return_bool(ret);
430
57.2k
}
431
432
static void
433
free_defs_val(int type, union sudo_defs_val *sd_un)
434
6.27M
{
435
6.27M
    switch (type & T_MASK) {
436
1.57M
  case T_STR:
437
2.00M
  case T_RLIMIT:
438
2.00M
      free(sd_un->str);
439
2.00M
      break;
440
192k
  case T_LIST:
441
192k
      (void)list_op(NULL, 0, &sd_un->list, freeall);
442
192k
      break;
443
6.27M
    }
444
6.27M
    memset(sd_un, 0, sizeof(*sd_un));
445
6.27M
}
446
447
static bool
448
init_passprompt_regex(void)
449
38.5k
{
450
38.5k
    struct list_member *lm;
451
38.5k
    debug_decl(init_passprompt_regex, SUDOERS_DEBUG_DEFAULTS);
452
453
    /* Add initial defaults setting. */
454
38.5k
    lm = calloc(1, sizeof(struct list_member));
455
38.5k
    if (lm == NULL || (lm->value = strdup(PASSPROMPT_REGEX)) == NULL) {
456
0
  free(lm);
457
0
  sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
458
0
  debug_return_bool(false);
459
0
    }
460
38.5k
    SLIST_INSERT_HEAD(&def_passprompt_regex, lm, entries);
461
462
38.5k
    debug_return_bool(true);
463
38.5k
}
464
465
/*
466
 * Set default options to compiled-in values.
467
 * Any of these may be overridden at runtime by a "Defaults" file.
468
 */
469
bool
470
init_defaults(void)
471
38.5k
{
472
38.5k
    static bool firsttime = true;
473
38.5k
    struct sudo_defs_types *def;
474
38.5k
    debug_decl(init_defaults, SUDOERS_DEBUG_DEFAULTS);
475
476
    /* Clear any old settings. */
477
38.5k
    if (!firsttime) {
478
6.31M
  for (def = sudo_defs_table; def->name != NULL; def++)
479
6.27M
      free_defs_val(def->type, &def->sd_un);
480
38.5k
    }
481
482
    /* First initialize the flags. */
483
#ifdef LONG_OTP_PROMPT
484
    def_long_otp_prompt = true;
485
#endif
486
38.5k
#ifndef ALLOW_DOT_PATH
487
38.5k
    def_ignore_dot = true;
488
38.5k
#endif
489
#ifdef ALWAYS_SEND_MAIL
490
    def_mail_always = true;
491
#endif
492
38.5k
#ifdef SEND_MAIL_WHEN_NO_USER
493
38.5k
    def_mail_no_user = true;
494
38.5k
#endif
495
#ifdef SEND_MAIL_WHEN_NO_HOST
496
    def_mail_no_host = true;
497
#endif
498
#ifdef SEND_MAIL_WHEN_NOT_OK
499
    def_mail_no_perms = true;
500
#endif
501
38.5k
#ifndef NO_LECTURE
502
38.5k
    def_lecture = once;
503
38.5k
#endif
504
38.5k
#ifndef NO_AUTHENTICATION
505
38.5k
    def_authenticate = true;
506
38.5k
#endif
507
38.5k
#ifndef NO_ROOT_SUDO
508
38.5k
    def_root_sudo = true;
509
38.5k
#endif
510
#ifdef HOST_IN_LOG
511
    def_log_host = true;
512
#endif
513
#ifdef SHELL_IF_NO_ARGS
514
    def_shell_noargs = true;
515
#endif
516
#ifdef SHELL_SETS_HOME
517
    def_set_home = true;
518
#endif
519
38.5k
#ifndef DONT_LEAK_PATH_INFO
520
38.5k
    def_path_info = true;
521
38.5k
#endif
522
#ifdef USE_INSULTS
523
    def_insults = true;
524
#endif
525
#ifdef FQDN
526
    def_fqdn = true;
527
#endif
528
38.5k
#ifdef ENV_EDITOR
529
38.5k
    def_env_editor = true;
530
38.5k
#endif
531
#ifdef UMASK_OVERRIDE
532
    def_umask_override = true;
533
#endif
534
38.5k
#ifdef SUDOERS_NAME_MATCH
535
38.5k
    def_fast_glob = true;
536
38.5k
    def_fdexec = never;
537
#else
538
    def_fdexec = digest_only;
539
#endif
540
38.5k
    def_timestamp_type = TIMESTAMP_TYPE;
541
38.5k
    if ((def_iolog_file = strdup(IOLOG_FILE)) == NULL)
542
0
  goto oom;
543
38.5k
    if ((def_iolog_dir = strdup(_PATH_SUDO_IO_LOGDIR)) == NULL)
544
0
  goto oom;
545
38.5k
    if ((def_sudoers_locale = strdup("C")) == NULL)
546
0
  goto oom;
547
38.5k
    def_env_reset = ENV_RESET;
548
38.5k
    def_set_logname = true;
549
38.5k
    def_closefrom = STDERR_FILENO + 1;
550
38.5k
    def_pam_ruser = true;
551
#ifdef __sun__
552
    def_pam_rhost = true;
553
#endif
554
38.5k
    if ((def_pam_service = strdup("sudo")) == NULL)
555
0
  goto oom;
556
#ifdef HAVE_PAM_LOGIN
557
    if ((def_pam_login_service = strdup("sudo-i")) == NULL)
558
  goto oom;
559
#else
560
38.5k
    if ((def_pam_login_service = strdup("sudo")) == NULL)
561
0
  goto oom;
562
38.5k
#endif
563
#ifdef NO_PAM_SESSION
564
    def_pam_session = false;
565
#else
566
38.5k
    def_pam_session = true;
567
38.5k
#endif
568
#ifdef HAVE_SELINUX
569
    def_selinux = true;
570
#endif
571
#ifdef _PATH_SUDO_ADMIN_FLAG
572
    if ((def_admin_flag = strdup(_PATH_SUDO_ADMIN_FLAG)) == NULL)
573
  goto oom;
574
#endif
575
38.5k
    if ((def_rlimit_core = strdup("0,0")) == NULL)
576
0
  goto oom;
577
38.5k
    def_intercept_type = dso;
578
38.5k
    def_intercept_verify = true;
579
38.5k
    def_use_netgroups = true;
580
38.5k
    def_netgroup_tuple = false;
581
38.5k
    def_sudoedit_checkdir = true;
582
38.5k
    def_iolog_mode = S_IRUSR|S_IWUSR;
583
38.5k
    def_log_allowed = true;
584
38.5k
    def_log_denied = true;
585
38.5k
    def_log_format = sudo;
586
38.5k
    def_runas_allow_unknown_id = false;
587
38.5k
    def_noninteractive_auth = false;
588
38.5k
    def_use_pty = true;
589
590
    /* Syslog options need special care since they both strings and ints */
591
38.5k
#if (LOGGING & SLOG_SYSLOG)
592
38.5k
    (void) store_syslogfac(LOGFAC, &sudo_defs_table[I_SYSLOG]);
593
38.5k
    (void) store_syslogpri(PRI_SUCCESS, &sudo_defs_table[I_SYSLOG_GOODPRI]);
594
38.5k
    (void) store_syslogpri(PRI_FAILURE, &sudo_defs_table[I_SYSLOG_BADPRI]);
595
38.5k
#endif
596
597
    /* Password flags also have a string and integer component. */
598
38.5k
    (void) store_tuple("any", &sudo_defs_table[I_LISTPW], 0);
599
38.5k
    (void) store_tuple("all", &sudo_defs_table[I_VERIFYPW], 0);
600
601
    /* Then initialize the int-like things. */
602
38.5k
#ifdef SUDO_UMASK
603
38.5k
    def_umask = SUDO_UMASK;
604
#else
605
    def_umask = ACCESSPERMS;
606
#endif
607
38.5k
    def_loglinelen = MAXLOGFILELEN;
608
38.5k
    def_timestamp_timeout.tv_sec = TIMEOUT * 60;
609
38.5k
    def_passwd_timeout.tv_sec = PASSWORD_TIMEOUT * 60;
610
38.5k
    def_passwd_tries = TRIES_FOR_PASSWORD;
611
#ifdef HAVE_ZLIB_H
612
    def_compress_io = true;
613
#endif
614
38.5k
    def_ignore_audit_errors = true;
615
38.5k
    def_ignore_iolog_errors = false;
616
38.5k
    def_ignore_logfile_errors = true;
617
38.5k
    def_log_passwords = true;
618
38.5k
#ifdef SUDOERS_LOG_CLIENT
619
38.5k
    def_log_server_timeout = 30;
620
38.5k
    def_log_server_verify = true;
621
38.5k
    def_log_server_keepalive = true;
622
38.5k
#endif
623
624
    /* Now do the strings */
625
38.5k
    if ((def_mailto = strdup(MAILTO)) == NULL)
626
0
  goto oom;
627
38.5k
    if ((def_mailsub = strdup(N_(MAILSUBJECT))) == NULL)
628
0
  goto oom;
629
38.5k
    if ((def_badpass_message = strdup(_(INCORRECT_PASSWORD))) == NULL)
630
0
  goto oom;
631
38.5k
#ifdef _PATH_SUDO_LECTURE_DIR
632
38.5k
    if ((def_lecture_status_dir = strdup(_PATH_SUDO_LECTURE_DIR)) == NULL)
633
0
  goto oom;
634
38.5k
#endif
635
38.5k
#ifdef _PATH_SUDO_TIMEDIR
636
38.5k
    if ((def_timestampdir = strdup(_PATH_SUDO_TIMEDIR)) == NULL)
637
0
  goto oom;
638
38.5k
#endif
639
38.5k
    if ((def_passprompt = strdup(_(PASSPROMPT))) == NULL)
640
0
  goto oom;
641
38.5k
    if ((def_runas_default = strdup(RUNAS_DEFAULT)) == NULL)
642
0
  goto oom;
643
#ifdef _PATH_SUDO_SENDMAIL
644
    if ((def_mailerpath = strdup(_PATH_SUDO_SENDMAIL)) == NULL)
645
  goto oom;
646
#endif
647
38.5k
    if ((def_mailerflags = strdup("-t")) == NULL)
648
0
  goto oom;
649
#if (LOGGING & SLOG_FILE)
650
    if ((def_logfile = strdup(_PATH_SUDO_LOGFILE)) == NULL)
651
  goto oom;
652
#endif
653
#ifdef EXEMPTGROUP
654
    if ((def_exempt_group = strdup(EXEMPTGROUP)) == NULL)
655
  goto oom;
656
#endif
657
#ifdef SECURE_PATH
658
    if ((def_secure_path = strdup(SECURE_PATH)) == NULL)
659
  goto oom;
660
#endif
661
38.5k
    if ((def_editor = strdup(EDITOR)) == NULL)
662
0
  goto oom;
663
38.5k
    def_set_utmp = true;
664
38.5k
    def_pam_acct_mgmt = true;
665
38.5k
    def_pam_setcred = true;
666
38.5k
    def_pam_silent = true;
667
38.5k
    def_syslog_maxlen = MAXSYSLOGLEN;
668
38.5k
    def_case_insensitive_user = true;
669
38.5k
    def_case_insensitive_group = true;
670
671
    /* Reset the locale. */
672
38.5k
    if (!firsttime) {
673
38.5k
  if (!sudoers_initlocale(NULL, def_sudoers_locale))
674
0
      goto oom;
675
38.5k
    }
676
677
    /* Finally do the lists (currently just environment tables). */
678
38.5k
    if (!init_envtables())
679
0
  goto oom;
680
681
    /* Init eventlog config. */
682
38.5k
    init_eventlog_config();
683
684
    /* Initial iolog password prompt regex. */
685
38.5k
    if (!init_passprompt_regex())
686
0
  debug_return_bool(false);
687
688
38.5k
    firsttime = false;
689
690
38.5k
    debug_return_bool(true);
691
0
oom:
692
0
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
693
0
    debug_return_bool(false);
694
0
}
695
696
/*
697
 * Check whether a defaults entry matches the specified type.
698
 * Returns true if it matches, else false.
699
 */
700
static bool
701
default_type_matches(const struct defaults *d, int what)
702
83.9k
{
703
83.9k
    debug_decl(default_type_matches, SUDOERS_DEBUG_DEFAULTS);
704
705
83.9k
    switch (d->type) {
706
81.6k
    case DEFAULTS:
707
81.6k
  if (ISSET(what, SETDEF_GENERIC))
708
81.6k
      debug_return_bool(true);
709
0
  break;
710
676
    case DEFAULTS_USER:
711
676
  if (ISSET(what, SETDEF_USER))
712
676
      debug_return_bool(true);
713
0
  break;
714
560
    case DEFAULTS_RUNAS:
715
560
  if (ISSET(what, SETDEF_RUNAS))
716
560
      debug_return_bool(true);
717
0
  break;
718
0
    case DEFAULTS_HOST:
719
0
  if (ISSET(what, SETDEF_HOST))
720
0
      debug_return_bool(true);
721
0
  break;
722
1.05k
    case DEFAULTS_CMND:
723
1.05k
  if (ISSET(what, SETDEF_CMND))
724
1.05k
      debug_return_bool(true);
725
0
  break;
726
83.9k
    }
727
0
    debug_return_bool(false);
728
0
}
729
730
/*
731
 * Check whether a defaults entry's binding matches.
732
 * Returns true if it matches, else false.
733
 */
734
static bool
735
default_binding_matches(const struct sudoers_context *ctx,
736
    struct sudoers_parse_tree *parse_tree, const struct defaults *d, int what)
737
83.9k
{
738
83.9k
    debug_decl(default_binding_matches, SUDOERS_DEBUG_DEFAULTS);
739
740
83.9k
    switch (d->type) {
741
81.6k
    case DEFAULTS:
742
81.6k
  debug_return_bool(true);
743
676
    case DEFAULTS_USER:
744
676
  if (userlist_matches(parse_tree, ctx->user.pw, &d->binding->members) == ALLOW)
745
169
      debug_return_bool(true);
746
507
  break;
747
560
    case DEFAULTS_RUNAS:
748
560
  if (runaslist_matches(parse_tree, &d->binding->members, NULL, NULL, NULL) == ALLOW)
749
0
      debug_return_bool(true);
750
560
  break;
751
560
    case DEFAULTS_HOST:
752
0
  if (hostlist_matches(parse_tree, ctx->user.pw, &d->binding->members) == ALLOW)
753
0
      debug_return_bool(true);
754
0
  break;
755
1.05k
    case DEFAULTS_CMND:
756
1.05k
  if (cmndlist_matches(parse_tree, &d->binding->members, NULL, NULL) == ALLOW)
757
896
      debug_return_bool(true);
758
160
  break;
759
83.9k
    }
760
1.22k
    debug_return_bool(false);
761
1.22k
}
762
763
/*
764
 * Update the global defaults based on the given defaults list.
765
 * Pass in an OR'd list of which default types to update.
766
 */
767
bool
768
update_defaults(struct sudoers_context *ctx,
769
    struct sudoers_parse_tree *parse_tree,
770
    const struct defaults_list *defs, int what, bool quiet)
771
85.2k
{
772
85.2k
    const struct defaults *d;
773
85.2k
    bool global_defaults = false;
774
85.2k
    bool ret = true;
775
85.2k
    debug_decl(update_defaults, SUDOERS_DEBUG_DEFAULTS);
776
777
85.2k
    sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
778
85.2k
  "what: 0x%02x", what);
779
780
    /* If no defaults list specified, use the global one in the parse tree. */
781
85.2k
    if (defs == NULL) {
782
57.2k
  defs = &parse_tree->defaults;
783
57.2k
  global_defaults = true;
784
57.2k
    }
785
786
    /*
787
     * If using the global defaults list, apply Defaults values marked as early.
788
     */
789
85.2k
    if (global_defaults) {
790
62.2k
  TAILQ_FOREACH(d, defs, entries) {
791
62.2k
      struct early_default *early = is_early_default(d->var);
792
62.2k
      if (early == NULL)
793
62.1k
    continue;
794
795
      /* Defaults type and binding must match. */
796
116
      if (!default_type_matches(d, what) ||
797
116
    !default_binding_matches(ctx, parse_tree, d, what))
798
0
    continue;
799
800
      /* Copy the value to sudo_defs_table and mark as early. */
801
116
      if (!set_early_default(ctx, d->var, d->val, d->op, d->file, d->line,
802
116
    d->column, quiet, early))
803
0
    ret = false;
804
116
  }
805
806
  /* Run callbacks for early defaults (if any) */
807
57.2k
  if (!run_early_defaults(ctx))
808
0
      ret = false;
809
57.2k
    }
810
811
    /*
812
     * Set the rest of the defaults and run their callbacks, if any.
813
     */
814
85.2k
    TAILQ_FOREACH(d, defs, entries) {
815
83.9k
  if (global_defaults) {
816
      /* Skip Defaults marked as early, we already did them. */
817
62.2k
      if (is_early_default(d->var))
818
116
    continue;
819
62.2k
  }
820
821
  /* Defaults type and binding must match. */
822
83.7k
  if (!default_type_matches(d, what) ||
823
83.7k
      !default_binding_matches(ctx, parse_tree, d, what))
824
1.22k
      continue;
825
826
  /* Copy the value to sudo_defs_table and run callback (if any) */
827
82.5k
  if (!set_default(ctx, d->var, d->val, d->op, d->file, d->line, d->column, quiet))
828
59.5k
      ret = false;
829
82.5k
    }
830
831
85.2k
    debug_return_bool(ret);
832
85.2k
}
833
834
/*
835
 * Check all defaults entries without actually setting them.
836
 */
837
bool
838
check_defaults(const struct sudoers_parse_tree *parse_tree, bool quiet)
839
7
{
840
7
    const struct defaults *d;
841
7
    bool ret = true;
842
7
    int idx;
843
7
    debug_decl(check_defaults, SUDOERS_DEBUG_DEFAULTS);
844
845
15.5k
    TAILQ_FOREACH(d, &parse_tree->defaults, entries) {
846
15.5k
  idx = find_default(parse_tree->ctx, d->var, d->file, d->line,
847
15.5k
      d->column, quiet);
848
15.5k
  if (idx != -1) {
849
696
      struct sudo_defs_types def = sudo_defs_table[idx];
850
696
      memset(&def.sd_un, 0, sizeof(def.sd_un));
851
696
      if (parse_default_entry(parse_tree->ctx, &def, d->val, d->op,
852
696
        d->file, d->line, d->column, quiet)) {
853
369
    free_defs_val(def.type, &def.sd_un);
854
369
    continue;
855
369
      }
856
696
  }
857
  /* There was an error in the entry. */
858
15.1k
  ret = false;
859
15.1k
    }
860
7
    debug_return_bool(ret);
861
7
}
862
863
static bool
864
store_int(const char *str, struct sudo_defs_types *def)
865
0
{
866
0
    const char *errstr;
867
0
    int i;
868
0
    debug_decl(store_int, SUDOERS_DEBUG_DEFAULTS);
869
870
0
    if (str == NULL) {
871
0
  def->sd_un.ival = 0;
872
0
    } else {
873
0
  i = (int)sudo_strtonum(str, INT_MIN, INT_MAX, &errstr);
874
0
  if (errstr != NULL) {
875
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
876
0
    "%s: %s", str, errstr);
877
0
      debug_return_bool(false);
878
0
  }
879
0
  def->sd_un.ival = i;
880
0
    }
881
0
    debug_return_bool(true);
882
0
}
883
884
static bool
885
store_uint(const char *str, struct sudo_defs_types *def)
886
55.9k
{
887
55.9k
    const char *errstr;
888
55.9k
    unsigned int u;
889
55.9k
    debug_decl(store_uint, SUDOERS_DEBUG_DEFAULTS);
890
891
55.9k
    if (str == NULL) {
892
0
  def->sd_un.uival = 0;
893
55.9k
    } else {
894
55.9k
  u = (unsigned int)sudo_strtonum(str, 0, UINT_MAX, &errstr);
895
55.9k
  if (errstr != NULL) {
896
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
897
0
    "%s: %s", str, errstr);
898
0
      debug_return_bool(false);
899
0
  }
900
55.9k
  def->sd_un.uival = u;
901
55.9k
    }
902
55.9k
    debug_return_bool(true);
903
55.9k
}
904
905
/* Check resource limit syntax, does not save as rlim_t. */
906
static bool
907
check_rlimit(const char *str, bool soft)
908
2.58k
{
909
2.58k
    const size_t inflen = sizeof("infinity") - 1;
910
2.58k
    debug_decl(check_rlimit, SUDOERS_DEBUG_DEFAULTS);
911
912
2.58k
    if (isdigit((unsigned char)*str)) {
913
0
  unsigned long long ullval;
914
0
  char *ep;
915
916
0
  errno = 0;
917
0
#ifdef HAVE_STRTOULL
918
0
  ullval = strtoull(str, &ep, 10);
919
0
  if (str == ep || (errno == ERANGE && ullval == ULLONG_MAX))
920
0
      debug_return_bool(false);
921
#else
922
  ullval = strtoul(str, &ep, 10);
923
  if (str == ep || (errno == ERANGE && ullval == ULONG_MAX))
924
      debug_return_bool(false);
925
#endif
926
0
  if (*ep == '\0' || (soft && *ep == ','))
927
0
      debug_return_bool(true);
928
0
  debug_return_bool(false);
929
0
    }
930
2.58k
    if (strncmp(str, "infinity", inflen) == 0) {
931
1.13k
  if (str[inflen] == '\0' || (soft && str[inflen] == ','))
932
1.13k
      debug_return_bool(true);
933
1.13k
    }
934
1.45k
    debug_return_bool(false);
935
1.45k
}
936
937
static bool
938
store_rlimit(const char *str, struct sudo_defs_types *def)
939
2.02k
{
940
2.02k
    debug_decl(store_rlimit, SUDOERS_DEBUG_DEFAULTS);
941
942
    /* The special values "user" and "default" are not compound. */
943
2.02k
    if (str != NULL && strcmp(str, "user") != 0 && strcmp(str, "default") != 0) {
944
2.02k
  const char *hard, *soft = str;
945
  /*
946
   * Expect a limit in the form "soft,hard" or "limit" (both soft+hard).
947
   */
948
2.02k
  hard = strchr(str, ',');
949
2.02k
  if (hard != NULL)
950
0
      hard++;
951
2.02k
  else
952
2.02k
      hard = soft;
953
954
2.02k
  if (!check_rlimit(soft, true))
955
1.45k
      debug_return_bool(false);
956
565
  if (!check_rlimit(hard, false))
957
0
      debug_return_bool(false);
958
565
    }
959
960
    /* Store as string, front-end will parse it as a limit. */
961
565
    debug_return_bool(store_str(str, def));
962
565
}
963
964
static bool
965
store_timespec(const char *str, struct sudo_defs_types *def)
966
40
{
967
40
    struct timespec ts;
968
40
    char sign = '+';
969
40
    long i;
970
40
    debug_decl(store_timespec, SUDOERS_DEBUG_DEFAULTS);
971
972
40
    sudo_timespecclear(&ts);
973
40
    if (str != NULL) {
974
  /* Convert from minutes to seconds. */
975
40
  if (*str == '+' || *str == '-')
976
0
      sign = *str++;
977
760
  while (*str != '\0' && *str != '.') {
978
720
    if (!isdigit((unsigned char)*str))
979
0
        debug_return_bool(false);  /* invalid number */
980
981
    /* Verify (ts.tv_sec * 10) + (digit * 60) <= TIME_T_MAX. */
982
720
    i = (*str++ - '0') * 60L;
983
720
    if (ts.tv_sec > (TIME_T_MAX - i) / 10)
984
0
        debug_return_bool(false);  /* overflow */
985
720
    ts.tv_sec *= 10;
986
720
    ts.tv_sec += i;
987
720
  }
988
40
  if (*str++ == '.') {
989
40
      long long nsec = 0;
990
991
      /* Convert optional fractional component to seconds and nanosecs. */
992
80
      for (i = 100000000; i > 0; i /= 10) {
993
80
    if (*str == '\0')
994
40
        break;
995
40
    if (!isdigit((unsigned char)*str))
996
0
        debug_return_bool(false);  /* invalid number */
997
40
    nsec += i * (*str++ - '0') * 60LL;
998
40
      }
999
320
      while (nsec >= 1000000000) {
1000
320
    if (ts.tv_sec == TIME_T_MAX)
1001
40
        debug_return_bool(false); /* overflow */
1002
280
    ts.tv_sec++;
1003
280
    nsec -= 1000000000;
1004
280
      }
1005
0
      ts.tv_nsec = (long)nsec;
1006
0
  }
1007
40
    }
1008
0
    if (sign == '-') {
1009
0
  def->sd_un.tspec.tv_sec = -ts.tv_sec;
1010
0
  def->sd_un.tspec.tv_nsec = -ts.tv_nsec;
1011
0
    } else {
1012
0
  def->sd_un.tspec.tv_sec = ts.tv_sec;
1013
0
  def->sd_un.tspec.tv_nsec = ts.tv_nsec;
1014
0
    }
1015
0
    debug_return_bool(true);
1016
0
}
1017
1018
static bool
1019
store_tuple(const char *str, struct sudo_defs_types *def, int op)
1020
77.5k
{
1021
77.5k
    const struct def_values *v;
1022
77.5k
    debug_decl(store_tuple, SUDOERS_DEBUG_DEFAULTS);
1023
1024
    /*
1025
     * Look up tuple value by name to find enum def_tuple value.
1026
     * A tuple must have at least two possible values.
1027
     */
1028
77.5k
    if (str == NULL) {
1029
  /*
1030
   * Boolean context: true maps to values[1], false maps to values[0].
1031
   */
1032
0
  if (op == true) {
1033
0
      v = &def->values[1];
1034
0
      def->sd_un.ival = v->nval;
1035
0
  } else if (op == false) {
1036
0
      v = &def->values[0];
1037
0
      def->sd_un.ival = v->nval;
1038
0
  } else {
1039
0
      debug_return_bool(false);
1040
0
  }
1041
77.5k
    } else {
1042
155k
  for (v = def->values; v->sval != NULL; v++) {
1043
155k
      if (strcmp(v->sval, str) == 0) {
1044
77.4k
    def->sd_un.tuple = v->nval;
1045
77.4k
    break;
1046
77.4k
      }
1047
155k
  }
1048
77.5k
  if (v->sval == NULL)
1049
130
      debug_return_bool(false);
1050
77.5k
    }
1051
77.4k
    debug_return_bool(true);
1052
77.4k
}
1053
1054
static int
1055
store_str(const char *str, struct sudo_defs_types *def)
1056
518k
{
1057
518k
    debug_decl(store_str, SUDOERS_DEBUG_DEFAULTS);
1058
1059
518k
    free(def->sd_un.str);
1060
518k
    if (str == NULL) {
1061
83.8k
  def->sd_un.str = NULL;
1062
434k
    } else {
1063
434k
  if ((def->sd_un.str = strdup(str)) == NULL) {
1064
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1065
0
      debug_return_int(-1);
1066
0
  }
1067
434k
    }
1068
518k
    debug_return_int(true);
1069
518k
}
1070
1071
static bool
1072
store_list(const char *str, struct sudo_defs_types *def, int op)
1073
23.6k
{
1074
23.6k
    debug_decl(store_list, SUDOERS_DEBUG_DEFAULTS);
1075
1076
    /* Remove all old members. */
1077
23.6k
    if (op == false || op == true)
1078
23.6k
  (void)list_op(NULL, 0, &def->sd_un.list, freeall);
1079
1080
    /* Split str into multiple space-separated words and act on each one. */
1081
23.6k
    if (str != NULL) {
1082
23.6k
  const char *cp, *ep;
1083
23.6k
  const char *end = str + strlen(str);
1084
23.6k
  const enum list_ops lop = op == '-' ? delete : add;
1085
1086
23.6k
  if (ISSET(def->type, T_SPACE)) {
1087
0
      if (!list_op(str, strlen(str), &def->sd_un.list, lop))
1088
0
    debug_return_bool(false);
1089
23.6k
  } else {
1090
47.3k
      for (cp = sudo_strsplit(str, end, " \t", &ep); cp != NULL;
1091
23.6k
    cp = sudo_strsplit(NULL, end, " \t", &ep)) {
1092
23.6k
    if (!list_op(cp, (size_t)(ep - cp), &def->sd_un.list, lop))
1093
0
        debug_return_bool(false);
1094
23.6k
      }
1095
23.6k
  }
1096
23.6k
    }
1097
23.6k
    debug_return_bool(true);
1098
23.6k
}
1099
1100
static bool
1101
store_plugin(const char *str, struct sudo_defs_types *def, int op)
1102
0
{
1103
0
    const enum list_ops lop = op == '-' ? delete : add;
1104
0
    debug_decl(store_plugin, SUDOERS_DEBUG_DEFAULTS);
1105
1106
    /* Remove all old members. */
1107
0
    if (op == false || op == true)
1108
0
  (void)list_op(NULL, 0, &def->sd_un.list, freeall);
1109
1110
0
    if (str != NULL) {
1111
0
  if (!list_op(str, strlen(str), &def->sd_un.list, lop))
1112
0
      debug_return_bool(false);
1113
0
    }
1114
1115
0
    debug_return_bool(true);
1116
0
}
1117
1118
static bool
1119
store_syslogfac(const char *str, struct sudo_defs_types *def)
1120
67.1k
{
1121
67.1k
    debug_decl(store_syslogfac, SUDOERS_DEBUG_DEFAULTS);
1122
1123
67.1k
    if (str == NULL) {
1124
250
  def->sd_un.ival = false;
1125
250
  debug_return_bool(true);
1126
250
    }
1127
66.9k
    debug_return_bool(sudo_str2logfac(str, &def->sd_un.ival));
1128
66.9k
}
1129
1130
static bool
1131
store_syslogpri(const char *str, struct sudo_defs_types *def)
1132
132k
{
1133
132k
    debug_decl(store_syslogpri, SUDOERS_DEBUG_DEFAULTS);
1134
1135
132k
    if (str == NULL) {
1136
0
  def->sd_un.ival = -1;
1137
0
  debug_return_bool(true);
1138
0
    }
1139
132k
    debug_return_bool(sudo_str2logpri(str, &def->sd_un.ival));
1140
132k
}
1141
1142
static bool
1143
store_mode(const char *str, struct sudo_defs_types *def)
1144
55.9k
{
1145
55.9k
    mode_t mode;
1146
55.9k
    const char *errstr;
1147
55.9k
    debug_decl(store_mode, SUDOERS_DEBUG_DEFAULTS);
1148
1149
55.9k
    if (str == NULL) {
1150
0
  def->sd_un.mode = ACCESSPERMS;
1151
55.9k
    } else {
1152
55.9k
  mode = sudo_strtomode(str, &errstr);
1153
55.9k
  if (errstr != NULL) {
1154
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
1155
0
    "%s is %s", str, errstr);
1156
0
      debug_return_bool(false);
1157
0
  }
1158
55.9k
  def->sd_un.mode = mode;
1159
55.9k
    }
1160
55.9k
    debug_return_bool(true);
1161
55.9k
}
1162
1163
static bool
1164
store_timeout(const char *str, struct sudo_defs_types *def)
1165
23.6k
{
1166
23.6k
    debug_decl(store_mode, SUDOERS_DEBUG_DEFAULTS);
1167
1168
23.6k
    if (str == NULL) {
1169
0
  def->sd_un.ival = 0;
1170
23.6k
    } else {
1171
23.6k
  int seconds = parse_timeout(str);
1172
23.6k
  if (seconds == -1) {
1173
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
1174
0
    "%s", str);
1175
0
      debug_return_bool(false);
1176
0
  }
1177
23.6k
  def->sd_un.ival = seconds;
1178
23.6k
    }
1179
23.6k
    debug_return_bool(true);
1180
23.6k
}
1181
1182
static bool
1183
valid_path(const struct sudoers_context *ctx, const struct sudo_defs_types *def,
1184
    const char *val, const char *file, int line, int column, bool quiet)
1185
210k
{
1186
210k
    bool ret = true;
1187
210k
    debug_decl(valid_path, SUDOERS_DEBUG_DEFAULTS);
1188
1189
210k
    if (strlen(val) >= PATH_MAX) {
1190
0
  defaults_warnx(ctx, file, line, column, quiet,
1191
0
      N_("path name for \"%s\" too long"), def->name);
1192
0
  ret = false;
1193
0
    }
1194
210k
    if (ISSET(def->type, T_CHPATH)) {
1195
55.9k
  if (val[0] != '/' && val[0] != '~' && (val[0] != '*' || val[1] != '\0')) {
1196
0
      defaults_warnx(ctx, file, line, column, quiet,
1197
0
    N_("values for \"%s\" must start with a '/', '~', or '*'"),
1198
0
    def->name);
1199
0
      ret = false;
1200
0
  }
1201
154k
    } else {
1202
154k
  if (val[0] != '/') {
1203
0
      defaults_warnx(ctx, file, line, column, quiet,
1204
0
    N_("values for \"%s\" must start with a '/'"), def->name);
1205
0
      ret = false;
1206
0
  }
1207
1208
154k
    }
1209
210k
    debug_return_bool(ret);
1210
210k
}
1211
1212
static bool
1213
list_op(const char *str, size_t len, struct list_members *list,
1214
    enum list_ops op)
1215
239k
{
1216
239k
    struct list_member *cur, *prev = NULL;
1217
239k
    debug_decl(list_op, SUDOERS_DEBUG_DEFAULTS);
1218
1219
239k
    if (op == freeall) {
1220
2.13M
  while ((cur = SLIST_FIRST(list)) != NULL) {
1221
1.91M
      SLIST_REMOVE_HEAD(list, entries);
1222
1.91M
      free(cur->value);
1223
1.91M
      free(cur);
1224
1.91M
  }
1225
216k
  debug_return_bool(true);
1226
216k
    }
1227
1228
23.6k
    SLIST_FOREACH(cur, list, entries) {
1229
0
  if ((strncmp(cur->value, str, len) == 0 && cur->value[len] == '\0')) {
1230
1231
0
      if (op == add)
1232
0
    debug_return_bool(true); /* already exists */
1233
1234
      /* Delete node */
1235
0
      if (prev == NULL)
1236
0
    SLIST_REMOVE_HEAD(list, entries);
1237
0
      else
1238
0
    SLIST_REMOVE_AFTER(prev, entries);
1239
0
      free(cur->value);
1240
0
      free(cur);
1241
0
      break;
1242
0
  }
1243
0
  prev = cur;
1244
0
    }
1245
1246
    /* Add new node to the head of the list. */
1247
23.6k
    if (op == add) {
1248
23.6k
  cur = calloc(1, sizeof(struct list_member));
1249
23.6k
  if (cur == NULL || (cur->value = strndup(str, len)) == NULL) {
1250
0
      sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1251
0
      free(cur);
1252
0
      debug_return_bool(false);
1253
0
  }
1254
23.6k
  SLIST_INSERT_HEAD(list, cur, entries);
1255
23.6k
    }
1256
23.6k
    debug_return_bool(true);
1257
23.6k
}
1258
1259
bool
1260
append_default(const char *var, const char *val, int op,
1261
    char *source, struct defaults_list *defs)
1262
154k
{
1263
154k
    struct defaults *def;
1264
154k
    debug_decl(append_default, SUDOERS_DEBUG_DEFAULTS);
1265
1266
154k
    if ((def = calloc(1, sizeof(*def))) == NULL)
1267
0
  goto oom;
1268
1269
154k
    def->type = DEFAULTS;
1270
154k
    def->op = op;
1271
154k
    if ((def->var = strdup(var)) == NULL) {
1272
0
  goto oom;
1273
0
    }
1274
154k
    if (val != NULL) {
1275
1.42k
  if ((def->val = strdup(val)) == NULL)
1276
0
      goto oom;
1277
1.42k
    }
1278
154k
    def->file = source;
1279
154k
    sudo_rcstr_addref(source);
1280
154k
    TAILQ_INSERT_TAIL(defs, def, entries);
1281
154k
    debug_return_bool(true);
1282
1283
0
oom:
1284
0
    if (def != NULL) {
1285
0
  free(def->var);
1286
0
  free(def->val);
1287
0
  free(def);
1288
0
    }
1289
0
    debug_return_bool(false);
1290
0
}
1291
1292
bool
1293
cb_passprompt_regex(struct sudoers_context *ctx, const char *file,
1294
    int line, int column, const union sudo_defs_val *sd_un, int op)
1295
0
{
1296
0
    struct list_member *lm;
1297
0
    const char *errstr;
1298
0
    debug_decl(cb_passprompt_regex, SUDOERS_DEBUG_DEFAULTS);
1299
1300
    /* If adding one or more regexps, make sure they are valid. */
1301
0
    if (op == '+' || op == true) {
1302
0
  SLIST_FOREACH(lm, &sd_un->list, entries) {
1303
0
      if (!sudo_regex_compile(NULL, lm->value, &errstr)) {
1304
0
    defaults_warnx(ctx, file, line, column, false,
1305
0
        U_("invalid regular expression \"%s\": %s"),
1306
0
        lm->value, U_(errstr));
1307
0
    debug_return_bool(false);
1308
0
      }
1309
0
  }
1310
0
    }
1311
1312
0
    debug_return_bool(true);
1313
0
}