summaryrefslogtreecommitdiffstats
path: root/lib/util/sudo_conf.c
blob: 3880423625093a2f33c8eceb135f79cdb7dcc0a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2009-2017 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>

#define SUDO_ERROR_WRAP	0

#include "sudo_compat.h"
#include "sudo_conf.h"
#include "sudo_debug.h"
#include "sudo_fatal.h"
#include "sudo_gettext.h"
#include "sudo_plugin.h"
#include "sudo_util.h"
#include "pathnames.h"

struct sudo_conf_table {
    const char *name;
    unsigned int namelen;
    int (*parser)(const char *entry, const char *conf_file, unsigned int lineno);
};

struct sudo_conf_path_table {
    const char *pname;
    unsigned int pnamelen;
    bool dynamic;
    char *pval;
};

struct sudo_conf_settings {
    bool updated;
    bool developer_mode;
    bool disable_coredump;
    bool probe_interfaces;
    int group_source;
    int max_groups;
};

static int parse_debug(const char *entry, const char *conf_file, unsigned int lineno);
static int parse_path(const char *entry, const char *conf_file, unsigned int lineno);
static int parse_plugin(const char *entry, const char *conf_file, unsigned int lineno);
static int parse_variable(const char *entry, const char *conf_file, unsigned int lineno);

static struct sudo_conf_table sudo_conf_table[] = {
    { "Debug", sizeof("Debug") - 1, parse_debug },
    { "Path", sizeof("Path") - 1, parse_path },
    { "Plugin", sizeof("Plugin") - 1, parse_plugin },
    { "Set", sizeof("Set") - 1, parse_variable },
    { NULL }
};

static int set_var_developer_mode(const char *entry, const char *conf_file, unsigned int);
static int set_var_disable_coredump(const char *entry, const char *conf_file, unsigned int);
static int set_var_group_source(const char *entry, const char *conf_file, unsigned int);
static int set_var_max_groups(const char *entry, const char *conf_file, unsigned int);
static int set_var_probe_interfaces(const char *entry, const char *conf_file, unsigned int);

static struct sudo_conf_table sudo_conf_var_table[] = {
    { "developer_mode", sizeof("developer_mode") - 1, set_var_developer_mode },
    { "disable_coredump", sizeof("disable_coredump") - 1, set_var_disable_coredump },
    { "group_source", sizeof("group_source") - 1, set_var_group_source },
    { "max_groups", sizeof("max_groups") - 1, set_var_max_groups },
    { "probe_interfaces", sizeof("probe_interfaces") - 1, set_var_probe_interfaces },
    { NULL }
};

/* Indexes into path_table[] below (order is important). */
#define SUDO_CONF_PATH_ASKPASS		0
#define SUDO_CONF_PATH_SESH		1
#define SUDO_CONF_PATH_NOEXEC		2
#define SUDO_CONF_PATH_PLUGIN_DIR	3
#define SUDO_CONF_PATH_DEVSEARCH	4

#define SUDO_CONF_PATH_INITIALIZER	{				\
    { "askpass", sizeof("askpass") - 1, false, _PATH_SUDO_ASKPASS },	\
    { "sesh", sizeof("sesh") - 1, false, _PATH_SUDO_SESH },		\
    { "noexec", sizeof("noexec") - 1, false, _PATH_SUDO_NOEXEC },	\
    { "plugin_dir", sizeof("plugin_dir") - 1, false, _PATH_SUDO_PLUGIN_DIR }, \
    { "devsearch", sizeof("devsearch") - 1, false, _PATH_SUDO_DEVSEARCH }, \
    { NULL } \
}

#define SUDO_CONF_SETTINGS_INITIALIZER	{				\
    false,			/* updated */				\
    false,			/* developer_mode */			\
    true,			/* disable_coredump */			\
    true,			/* probe_interfaces */			\
    GROUP_SOURCE_ADAPTIVE,	/* group_source */			\
    -1				/* max_groups */			\
}

static struct sudo_conf_data {
    struct sudo_conf_settings settings;
    struct sudo_conf_debug_list debugging;
    struct plugin_info_list plugins;
    struct sudo_conf_path_table path_table[6];
} sudo_conf_data = {
    SUDO_CONF_SETTINGS_INITIALIZER,
    TAILQ_HEAD_INITIALIZER(sudo_conf_data.debugging),
    TAILQ_HEAD_INITIALIZER(sudo_conf_data.plugins),
    SUDO_CONF_PATH_INITIALIZER
};

/*
 * "Set variable_name value"
 */
static int
parse_variable(const char *entry, const char *conf_file, unsigned int lineno)
{
    struct sudo_conf_table *var;
    int ret;
    debug_decl(parse_variable, SUDO_DEBUG_UTIL);

    for (var = sudo_conf_var_table; var->name != NULL; var++) {
	if (strncmp(entry, var->name, var->namelen) == 0 &&
	    isblank((unsigned char)entry[var->namelen])) {
	    entry += var->namelen + 1;
	    while (isblank((unsigned char)*entry))
		entry++;
	    ret = var->parser(entry, conf_file, lineno);
	    sudo_debug_printf(ret ? SUDO_DEBUG_INFO : SUDO_DEBUG_ERROR,
		"%s: %s:%u: Set %s %s", __func__, conf_file,
		lineno, var->name, entry);
	    debug_return_int(ret);
	}
    }
    sudo_debug_printf(SUDO_DEBUG_WARN, "%s: %s:%u: unknown setting %s",
	__func__, conf_file, lineno, entry);
    debug_return_int(false);
}

/*
 * "Path name /path/to/file"
 * If path is missing it will be set to the NULL pointer.
 */
static int
parse_path(const char *entry, const char *conf_file, unsigned int lineno)
{
    const char *entry_end = entry + strlen(entry);
    const char *ep, *name, *path;
    struct sudo_conf_path_table *cur;
    size_t namelen;
    debug_decl(parse_path, SUDO_DEBUG_UTIL);

    /* Parse name. */
    name = sudo_strsplit(entry, entry_end, " \t", &ep);
    if (name == NULL)
	goto bad;
    namelen = (size_t)(ep - name);

    /* Parse path (if present). */
    path = sudo_strsplit(NULL, entry_end, " \t", &ep);

    /* Match supported paths, ignoring unknown paths. */
    for (cur = sudo_conf_data.path_table; cur->pname != NULL; cur++) {
	if (namelen == cur->pnamelen &&
	    strncasecmp(name, cur->pname, cur->pnamelen) == 0) {
	    char *pval = NULL;
	    if (path != NULL) {
		if ((pval = strdup(path)) == NULL) {
		    sudo_warnx(U_("%s: %s"), __func__,
			U_("unable to allocate memory"));
		    debug_return_int(-1);
		}
	    }
	    if (cur->dynamic)
		free(cur->pval);
	    cur->pval = pval;
	    cur->dynamic = true;
	    sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %s:%u: Path %s %s",
		__func__, conf_file, lineno, cur->pname,
		pval ? pval : "(none)");
	    debug_return_int(true);
	}
    }
    sudo_debug_printf(SUDO_DEBUG_WARN, "%s: %s:%u: unknown path %s",
	__func__, conf_file, lineno, entry);
    debug_return_int(false);
bad:
    sudo_warnx(U_("invalid Path value \"%s\" in %s, line %u"),
	entry, conf_file, lineno);
    debug_return_int(false);
}

/*
 * "Debug program /path/to/log flags,..."
 */
static int
parse_debug(const char *entry, const char *conf_file, unsigned int lineno)
{
    struct sudo_conf_debug *debug_spec;
    struct sudo_debug_file *debug_file = NULL;
    const char *ep, *path, *progname, *flags;
    const char *entry_end = entry + strlen(entry);
    size_t pathlen, prognamelen;
    debug_decl(parse_debug, SUDO_DEBUG_UTIL);

    /* Parse progname. */
    progname = sudo_strsplit(entry, entry_end, " \t", &ep);
    if (progname == NULL)
	debug_return_int(false);	/* not enough fields */
    prognamelen = (size_t)(ep - progname);

    /* Parse path. */
    path = sudo_strsplit(NULL, entry_end, " \t", &ep);
    if (path == NULL)
	debug_return_int(false);	/* not enough fields */
    pathlen = (size_t)(ep - path);

    /* Remainder is flags (freeform). */
    flags = sudo_strsplit(NULL, entry_end, " \t", &ep);
    if (flags == NULL)
	debug_return_int(false);	/* not enough fields */

    /* If progname already exists, use it, else alloc a new one. */
    TAILQ_FOREACH(debug_spec, &sudo_conf_data.debugging, entries) {
	if (strncmp(debug_spec->progname, progname, prognamelen) == 0 &&
	    debug_spec->progname[prognamelen] == '\0')
	    break;
    }
    if (debug_spec == NULL) {
	debug_spec = malloc(sizeof(*debug_spec));
	if (debug_spec == NULL)
	    goto oom;
	debug_spec->progname = strndup(progname, prognamelen);
	if (debug_spec->progname == NULL) {
	    free(debug_spec);
	    debug_spec = NULL;
	    goto oom;
	}
	TAILQ_INIT(&debug_spec->debug_files);
	TAILQ_INSERT_TAIL(&sudo_conf_data.debugging, debug_spec, entries);
    }
    debug_file = calloc(1, sizeof(*debug_file));
    if (debug_file == NULL)
	goto oom;
    debug_file->debug_file = strndup(path, pathlen);
    if (debug_file->debug_file == NULL)
	goto oom;
    debug_file->debug_flags = strdup(flags);
    if (debug_file->debug_flags == NULL)
	goto oom;
    TAILQ_INSERT_TAIL(&debug_spec->debug_files, debug_file, entries);

    debug_return_int(true);
oom:
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
    if (debug_file != NULL) {
	free(debug_file->debug_file);
	free(debug_file->debug_flags);
	free(debug_file);
    }
    debug_return_int(-1);
}

/*
 * "Plugin symbol /path/to/log args..."
 */
static int
parse_plugin(const char *entry, const char *conf_file, unsigned int lineno)
{
    struct plugin_info *info = NULL;
    const char *ep, *path, *symbol;
    const char *entry_end = entry + strlen(entry);
    char **options = NULL;
    size_t pathlen, symlen;
    unsigned int nopts = 0;
    debug_decl(parse_plugin, SUDO_DEBUG_UTIL);

    /* Parse symbol. */
    symbol = sudo_strsplit(entry, entry_end, " \t", &ep);
    if (symbol == NULL)
	debug_return_int(false);	/* not enough fields */
    symlen = (size_t)(ep - symbol);

    /* Parse path. */
    path = sudo_strsplit(NULL, entry_end, " \t", &ep);
    if (path == NULL)
	debug_return_int(false);	/* not enough fields */
    pathlen = (size_t)(ep - path);

    /* Split options into an array if present. */
    while (isblank((unsigned char)*ep))
	ep++;
    if (*ep != '\0') {
	/* Count number of options and allocate array. */
	const char *cp, *opt = ep;

	/* Count and allocate options array. */
	for (nopts = 0, cp = sudo_strsplit(opt, entry_end, " \t", &ep);
	    cp != NULL; cp = sudo_strsplit(NULL, entry_end, " \t", &ep)) {
	    nopts++;
	}
	options = reallocarray(NULL, nopts + 1, sizeof(*options));
	if (options == NULL)
	    goto oom;

	/* Fill in options array. */
	for (nopts = 0, cp = sudo_strsplit(opt, entry_end, " \t", &ep);
	    cp != NULL; cp = sudo_strsplit(NULL, entry_end, " \t", &ep)) {
	    options[nopts] = strndup(cp, (size_t)(ep - cp));
	    if (options[nopts] == NULL)
		goto oom;
	    nopts++;
	}
	options[nopts] = NULL;
    }

    info = calloc(sizeof(*info), 1);
    if (info == NULL)
	    goto oom;
    info->symbol_name = strndup(symbol, symlen);
    if (info->symbol_name == NULL)
	    goto oom;
    info->path = strndup(path, pathlen);
    if (info->path == NULL)
	    goto oom;
    info->options = options;
    info->lineno = lineno;
    TAILQ_INSERT_TAIL(&sudo_conf_data.plugins, info, entries);

    debug_return_int(true);
oom:
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
    if (options != NULL) {
	while (nopts--)
	    free(options[nopts]);
	free(options);
    }
    if (info != NULL) {
	free(info->symbol_name);
	free(info->path);
	free(info);
    }
    debug_return_int(-1);
}

static int
set_var_developer_mode(const char *strval, const char *conf_file,
    unsigned int lineno)
{
    int val = sudo_strtobool(strval);
    debug_decl(set_var_developer_mode, SUDO_DEBUG_UTIL);

    if (val == -1) {
        sudo_warnx(U_("invalid value for %s \"%s\" in %s, line %u"),
            "developer_mode", strval, conf_file, lineno);
        debug_return_bool(false);
    }
    sudo_conf_data.settings.developer_mode = val;
    debug_return_bool(true);
}

static int
set_var_disable_coredump(const char *strval, const char *conf_file,
    unsigned int lineno)
{
    int val = sudo_strtobool(strval);
    debug_decl(set_var_disable_coredump, SUDO_DEBUG_UTIL);

    if (val == -1) {
	sudo_warnx(U_("invalid value for %s \"%s\" in %s, line %u"),
	    "disable_coredump", strval, conf_file, lineno);
	debug_return_bool(false);
    }
    sudo_conf_data.settings.disable_coredump = val;
    debug_return_bool(true);
}

static int
set_var_group_source(const char *strval, const char *conf_file,
    unsigned int lineno)
{
    debug_decl(set_var_group_source, SUDO_DEBUG_UTIL);

    if (strcasecmp(strval, "adaptive") == 0) {
	sudo_conf_data.settings.group_source = GROUP_SOURCE_ADAPTIVE;
    } else if (strcasecmp(strval, "static") == 0) {
	sudo_conf_data.settings.group_source = GROUP_SOURCE_STATIC;
    } else if (strcasecmp(strval, "dynamic") == 0) {
	sudo_conf_data.settings.group_source = GROUP_SOURCE_DYNAMIC;
    } else {
	sudo_warnx(U_("unsupported group source \"%s\" in %s, line %u"), strval,
	    conf_file, lineno);
	debug_return_bool(false);
    }
    debug_return_bool(true);
}

static int
set_var_max_groups(const char *strval, const char *conf_file,
    unsigned int lineno)
{
    int max_groups;
    debug_decl(set_var_max_groups, SUDO_DEBUG_UTIL);

    max_groups = sudo_strtonum(strval, 1, INT_MAX, NULL);
    if (max_groups <= 0) {
	sudo_warnx(U_("invalid max groups \"%s\" in %s, line %u"), strval,
	    conf_file, lineno);
	debug_return_bool(false);
    }
    sudo_conf_data.settings.max_groups = max_groups;
    debug_return_bool(true);
}

static int
set_var_probe_interfaces(const char *strval, const char *conf_file,
    unsigned int lineno)
{
    int val = sudo_strtobool(strval);
    debug_decl(set_var_probe_interfaces, SUDO_DEBUG_UTIL);

    if (val == -1) {
	sudo_warnx(U_("invalid value for %s \"%s\" in %s, line %u"),
	    "probe_interfaces", strval, conf_file, lineno);
	debug_return_bool(false);
    }
    sudo_conf_data.settings.probe_interfaces = val;
    debug_return_bool(true);
}

const char *
sudo_conf_askpass_path_v1(void)
{
    return sudo_conf_data.path_table[SUDO_CONF_PATH_ASKPASS].pval;
}

const char *
sudo_conf_sesh_path_v1(void)
{
    return sudo_conf_data.path_table[SUDO_CONF_PATH_SESH].pval;
}

const char *
sudo_conf_noexec_path_v1(void)
{
    return sudo_conf_data.path_table[SUDO_CONF_PATH_NOEXEC].pval;
}

const char *
sudo_conf_plugin_dir_path_v1(void)
{
    return sudo_conf_data.path_table[SUDO_CONF_PATH_PLUGIN_DIR].pval;
}

const char *
sudo_conf_devsearch_path_v1(void)
{
    return sudo_conf_data.path_table[SUDO_CONF_PATH_DEVSEARCH].pval;
}

int
sudo_conf_group_source_v1(void)
{
    return sudo_conf_data.settings.group_source;
}

int
sudo_conf_max_groups_v1(void)
{
    return sudo_conf_data.settings.max_groups;
}

struct plugin_info_list *
sudo_conf_plugins_v1(void)
{
    return &sudo_conf_data.plugins;
}

struct sudo_conf_debug_list *
sudo_conf_debugging_v1(void)
{
    return &sudo_conf_data.debugging;
}

/* Return the debug files list for a program, or NULL if none. */
struct sudo_conf_debug_file_list *
sudo_conf_debug_files_v1(const char *progname)
{
    struct sudo_conf_debug *debug_spec;
    size_t prognamelen, progbaselen;
    const char *progbase = progname;
    debug_decl(sudo_conf_debug_files, SUDO_DEBUG_UTIL);

    /* Determine basename if program is fully qualified (like for plugins). */
    prognamelen = progbaselen = strlen(progname);
    if (*progname == '/') {
	progbase = strrchr(progname, '/');
	progbaselen = strlen(++progbase);
    }
    /* Convert sudoedit -> sudo. */
    if (progbaselen > 4 && strcmp(progbase + 4, "edit") == 0) {
	progbaselen -= 4;
    }
    TAILQ_FOREACH(debug_spec, &sudo_conf_data.debugging, entries) {
	const char *prog = progbase;
	size_t len = progbaselen;

	if (debug_spec->progname[0] == '/') {
	    /* Match fully-qualified name, if possible. */
	    prog = progname;
	    len = prognamelen;
	}
	if (strncmp(debug_spec->progname, prog, len) == 0 &&
	    debug_spec->progname[len] == '\0') {
	    debug_return_ptr(&debug_spec->debug_files);
	}
    }
    debug_return_ptr(NULL);
}

bool
sudo_conf_developer_mode_v1(void)
{
    return sudo_conf_data.settings.developer_mode;
}

bool
sudo_conf_disable_coredump_v1(void)
{
    return sudo_conf_data.settings.disable_coredump;
}

bool
sudo_conf_probe_interfaces_v1(void)
{
    return sudo_conf_data.settings.probe_interfaces;
}

/*
 * Free dynamically allocated parts of sudo_conf_data and
 * reset to initial values.
 */
static void
sudo_conf_init(int conf_types)
{
    struct sudo_conf_debug *debug_spec;
    struct sudo_debug_file *debug_file;
    struct plugin_info *plugin_info;
    int i;
    debug_decl(sudo_conf_init, SUDO_DEBUG_UTIL);

    /* Free and reset paths. */
    if (ISSET(conf_types, SUDO_CONF_PATHS)) {
	const struct sudo_conf_path_table path_table[] = SUDO_CONF_PATH_INITIALIZER;
	sudo_conf_clear_paths();
	memcpy(sudo_conf_data.path_table, path_table,
	    sizeof(sudo_conf_data.path_table));
    }

    /* Free and reset debug settings. */
    if (ISSET(conf_types, SUDO_CONF_DEBUG)) {
	while ((debug_spec = TAILQ_FIRST(&sudo_conf_data.debugging))) {
	    TAILQ_REMOVE(&sudo_conf_data.debugging, debug_spec, entries);
	    free(debug_spec->progname);
	    while ((debug_file = TAILQ_FIRST(&debug_spec->debug_files))) {
		TAILQ_REMOVE(&debug_spec->debug_files, debug_file, entries);
		free(debug_file->debug_file);
		free(debug_file->debug_flags);
		free(debug_file);
	    }
	    free(debug_spec);
	}
    }

    /* Free and reset plugins. */
    if (ISSET(conf_types, SUDO_CONF_PLUGINS)) {
	while ((plugin_info = TAILQ_FIRST(&sudo_conf_data.plugins))) {
	    TAILQ_REMOVE(&sudo_conf_data.plugins, plugin_info, entries);
	    free(plugin_info->symbol_name);
	    free(plugin_info->path);
	    if (plugin_info->options != NULL) {
		for (i = 0; plugin_info->options[i] != NULL; i++)
		    free(plugin_info->options[i]);
		free(plugin_info->options);
	    }
	    free(plugin_info);
	}
    }

    /* Set initial values. */
    if (ISSET(conf_types, SUDO_CONF_SETTINGS)) {
	const struct sudo_conf_settings settings = SUDO_CONF_SETTINGS_INITIALIZER;
	sudo_conf_data.settings = settings;
    }

    debug_return;
}

/*
 * Read in /etc/sudo.conf and populates sudo_conf_data.
 */
int
sudo_conf_read_v1(const char *conf_file, int conf_types)
{
    struct stat sb;
    FILE *fp = NULL;
    int ret = false;
    char *prev_locale, *line = NULL;
    unsigned int conf_lineno = 0;
    size_t linesize = 0;
    debug_decl(sudo_conf_read, SUDO_DEBUG_UTIL);

    if ((prev_locale = setlocale(LC_ALL, NULL)) == NULL) {
	sudo_warn("setlocale(LC_ALL, NULL)");
	debug_return_int(-1);
    }
    if ((prev_locale = strdup(prev_locale)) == NULL) {
	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	debug_return_int(-1);
    }

    /* Parse sudo.conf in the "C" locale. */
    if (prev_locale[0] != 'C' || prev_locale[1] != '\0')
        setlocale(LC_ALL, "C");

    if (conf_file == NULL) {
	conf_file = _PATH_SUDO_CONF;
	switch (sudo_secure_file(conf_file, ROOT_UID, -1, &sb)) {
	    case SUDO_PATH_SECURE:
		break;
	    case SUDO_PATH_MISSING:
		/* Root should always be able to read sudo.conf. */
		if (errno != ENOENT && geteuid() == ROOT_UID)
		    sudo_warn(U_("unable to stat %s"), conf_file);
		goto done;
	    case SUDO_PATH_BAD_TYPE:
		sudo_warnx(U_("%s is not a regular file"), conf_file);
		goto done;
	    case SUDO_PATH_WRONG_OWNER:
		sudo_warnx(U_("%s is owned by uid %u, should be %u"),
		    conf_file, (unsigned int) sb.st_uid, ROOT_UID);
		goto done;
	    case SUDO_PATH_WORLD_WRITABLE:
		sudo_warnx(U_("%s is world writable"), conf_file);
		goto done;
	    case SUDO_PATH_GROUP_WRITABLE:
		sudo_warnx(U_("%s is group writable"), conf_file);
		goto done;
	    default:
		/* NOTREACHED */
		goto done;
	}
    }

    if ((fp = fopen(conf_file, "r")) == NULL) {
	if (errno != ENOENT && geteuid() == ROOT_UID)
	    sudo_warn(U_("unable to open %s"), conf_file);
	goto done;
    }

    /* Reset to initial values if necessary. */
    if (sudo_conf_data.settings.updated)
	sudo_conf_init(conf_types);

    while (sudo_parseln(&line, &linesize, &conf_lineno, fp, 0) != -1) {
	struct sudo_conf_table *cur;
	unsigned int i;
	char *cp;

	if (*(cp = line) == '\0')
	    continue;		/* empty line or comment */

	for (i = 0, cur = sudo_conf_table; cur->name != NULL; i++, cur++) {
	    if (strncasecmp(cp, cur->name, cur->namelen) == 0 &&
		isblank((unsigned char)cp[cur->namelen])) {
		if (ISSET(conf_types, (1 << i))) {
		    cp += cur->namelen;
		    while (isblank((unsigned char)*cp))
			cp++;
		    ret = cur->parser(cp, conf_file, conf_lineno);
		    switch (ret) {
		    case true:
			sudo_conf_data.settings.updated = true;
			break;
		    case false:
			break;
		    default:
			goto done;
		    }
		}
		break;
	    }
	}
	if (cur->name == NULL) {
	    sudo_debug_printf(SUDO_DEBUG_WARN,
		"%s: %s:%u: unsupported entry: %s", __func__, conf_file,
		conf_lineno, line);
	}
    }
    ret = true;

done:
    if (fp != NULL)
	fclose(fp);
    free(line);

    /* Restore locale if needed. */
    if (prev_locale[0] != 'C' || prev_locale[1] != '\0')
        setlocale(LC_ALL, prev_locale);
    free(prev_locale);
    debug_return_int(ret);
}

/*
 * Used by the sudo_conf regress test to clear compile-time path settings.
 */
void
sudo_conf_clear_paths_v1(void)
{
    struct sudo_conf_path_table *cur;
    debug_decl(sudo_conf_clear_paths, SUDO_DEBUG_UTIL);

    for (cur = sudo_conf_data.path_table; cur->pname != NULL; cur++) {
	if (cur->dynamic)
	    free(cur->pval);
	cur->pval = NULL;
	cur->dynamic = false;
    }
}