Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/cvtsudoers.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2018-2023 Todd C. Miller <[email protected]>
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
19
/*
20
* Convert from the sudoers file format to LDIF or JSON format.
21
*/
22
23
#include <config.h>
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#ifdef HAVE_STRINGS_H
29
# include <strings.h>
30
#endif /* HAVE_STRINGS_H */
31
#include <ctype.h>
32
#include <errno.h>
33
#include <fcntl.h>
34
#include <pwd.h>
35
#include <unistd.h>
36
#ifdef HAVE_GETOPT_LONG
37
# include <getopt.h>
38
# else
39
# include <compat/getopt.h>
40
#endif /* HAVE_GETOPT_LONG */
41
42
#include <sudoers.h>
43
#include <sudoers_version.h>
44
#include <sudo_lbuf.h>
45
#include <redblack.h>
46
#include <cvtsudoers.h>
47
#include <testsudoers_pwutil.h>
48
#include <tsgetgrpw.h>
49
#include <gram.h>
50
51
/* Long-only options values. */
52
#define OPT_GROUP_FILE 256
53
#define OPT_PASSWD_FILE 257
54
55
/*
56
* Globals
57
*/
58
struct cvtsudoers_filter *filters;
59
static FILE *logfp;
60
static const char short_opts[] = "b:c:d:ef:hi:I:l:m:Mo:O:pP:s:V";
61
static struct option long_opts[] = {
62
{ "base", required_argument, NULL, 'b' },
63
{ "config", required_argument, NULL, 'c' },
64
{ "defaults", required_argument, NULL, 'd' },
65
{ "expand-aliases", no_argument, NULL, 'e' },
66
{ "output-format", required_argument, NULL, 'f' },
67
{ "help", no_argument, NULL, 'h' },
68
{ "input-format", required_argument, NULL, 'i' },
69
{ "increment", required_argument, NULL, 'I' },
70
{ "logfile", required_argument, NULL, 'l' },
71
{ "match", required_argument, NULL, 'm' },
72
{ "match-local", no_argument, NULL, 'M' },
73
{ "prune-matches", no_argument, NULL, 'p' },
74
{ "padding", required_argument, NULL, 'P' },
75
{ "order-start", required_argument, NULL, 'O' },
76
{ "output", required_argument, NULL, 'o' },
77
{ "suppress", required_argument, NULL, 's' },
78
{ "version", no_argument, NULL, 'V' },
79
{ "group-file", required_argument, NULL, OPT_GROUP_FILE },
80
{ "passwd-file", required_argument, NULL, OPT_PASSWD_FILE },
81
{ NULL, no_argument, NULL, 0 },
82
};
83
84
sudo_dso_public int main(int argc, char *argv[]);
85
static bool convert_sudoers_sudoers(struct sudoers_parse_tree *parse_tree, const char *output_file, struct cvtsudoers_config *conf);
86
static bool parse_sudoers(struct sudoers_context *ctx, const char *input_file, struct cvtsudoers_config *conf);
87
static bool parse_ldif(struct sudoers_parse_tree *parse_tree, const char *input_file, struct cvtsudoers_config *conf);
88
static bool cvtsudoers_parse_filter(char *expression);
89
static struct cvtsudoers_config *cvtsudoers_conf_read(const char *conf_file);
90
static void cvtsudoers_conf_free(struct cvtsudoers_config *conf);
91
static unsigned int cvtsudoers_parse_defaults(char *expression);
92
static unsigned int cvtsudoers_parse_suppression(char *expression);
93
static void filter_userspecs(struct sudoers_parse_tree *parse_tree, struct cvtsudoers_config *conf);
94
static void filter_defaults(struct sudoers_parse_tree *parse_tree, struct cvtsudoers_config *conf);
95
static void alias_remove_unused(struct sudoers_parse_tree *parse_tree);
96
static bool alias_prune(struct sudoers_parse_tree *parse_tree, struct cvtsudoers_config *conf);
97
sudo_noreturn static void help(void);
98
sudo_noreturn static void usage(void);
99
100
int
101
main(int argc, char *argv[])
102
{
103
struct sudoers_parse_tree_list parse_trees = TAILQ_HEAD_INITIALIZER(parse_trees);
104
struct sudoers_context ctx = SUDOERS_CONTEXT_INITIALIZER;
105
struct sudoers_parse_tree merged_tree, *parse_tree = NULL;
106
struct cvtsudoers_config *conf = NULL;
107
enum sudoers_formats output_format = format_ldif;
108
enum sudoers_formats input_format = format_sudoers;
109
const char *input_file = "-";
110
const char *output_file = "-";
111
const char *conf_file = NULL;
112
const char *grfile = NULL, *pwfile = NULL;
113
const char *cp, *errstr;
114
int ch, exitcode = EXIT_FAILURE;
115
bool match_local = false;
116
debug_decl(main, SUDOERS_DEBUG_MAIN);
117
118
#if defined(SUDO_DEVEL) && defined(__OpenBSD__)
119
{
120
extern char *malloc_options;
121
malloc_options = "S";
122
}
123
#endif
124
125
initprogname(argc > 0 ? argv[0] : "cvtsudoers");
126
if (!sudoers_initlocale(setlocale(LC_ALL, ""), def_sudoers_locale))
127
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
128
sudo_warn_set_locale_func(sudoers_warn_setlocale);
129
bindtextdomain("sudoers", LOCALEDIR);
130
textdomain("sudoers");
131
132
/* Initialize early, before any "goto done". */
133
init_parse_tree(&merged_tree, NULL, NULL, &ctx, NULL);
134
135
/* Read debug and plugin sections of sudo.conf. */
136
if (sudo_conf_read(NULL, SUDO_CONF_DEBUG|SUDO_CONF_PLUGINS) == -1)
137
goto done;
138
139
/* Initialize the debug subsystem. */
140
if (!sudoers_debug_register(getprogname(), sudo_conf_debug_files(getprogname())))
141
goto done;
142
143
/* Check for --config option first (no getopt warnings). */
144
opterr = 0;
145
while ((ch = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
146
switch (ch) {
147
case 'c':
148
conf_file = optarg;
149
break;
150
}
151
}
152
153
/* Read conf file. */
154
conf = cvtsudoers_conf_read(conf_file);
155
156
/*
157
* Reset getopt and handle the rest of the arguments.
158
*/
159
opterr = 1;
160
optind = 1;
161
#ifdef HAVE_OPTRESET
162
optreset = 1;
163
#endif
164
while ((ch = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
165
switch (ch) {
166
case 'b':
167
free(conf->sudoers_base);
168
conf->sudoers_base = strdup(optarg);
169
if (conf->sudoers_base == NULL) {
170
sudo_fatalx(U_("%s: %s"), __func__,
171
U_("unable to allocate memory"));
172
}
173
break;
174
case 'c':
175
/* handled above */
176
break;
177
case 'd':
178
conf->defstr = optarg;
179
break;
180
case 'e':
181
conf->expand_aliases = true;
182
break;
183
case 'f':
184
free(conf->output_format);
185
conf->output_format = strdup(optarg);
186
if (conf->output_format == NULL) {
187
sudo_fatalx(U_("%s: %s"), __func__,
188
U_("unable to allocate memory"));
189
}
190
break;
191
case 'h':
192
help();
193
/* NOTREACHED */
194
case 'i':
195
free(conf->input_format);
196
conf->input_format = strdup(optarg);
197
if (conf->input_format == NULL) {
198
sudo_fatalx(U_("%s: %s"), __func__,
199
U_("unable to allocate memory"));
200
}
201
break;
202
case 'I':
203
conf->order_increment =
204
(unsigned int)sudo_strtonum(optarg, 1, UINT_MAX, &errstr);
205
if (errstr != NULL) {
206
sudo_warnx(U_("order increment: %s: %s"), optarg, U_(errstr));
207
usage();
208
}
209
break;
210
case 'l':
211
conf->logfile = optarg;
212
break;
213
case 'm':
214
conf->filter = optarg;
215
break;
216
case 'M':
217
match_local = true;
218
break;
219
case 'o':
220
output_file = optarg;
221
break;
222
case 'O':
223
conf->sudo_order =
224
(unsigned int)sudo_strtonum(optarg, 0, UINT_MAX, &errstr);
225
if (errstr != NULL) {
226
sudo_warnx(U_("starting order: %s: %s"), optarg, U_(errstr));
227
usage();
228
}
229
break;
230
case 'p':
231
conf->prune_matches = true;
232
break;
233
case 'P':
234
conf->order_padding =
235
(unsigned int)sudo_strtonum(optarg, 1, UINT_MAX, &errstr);
236
if (errstr != NULL ) {
237
sudo_warnx(U_("order padding: %s: %s"), optarg, U_(errstr));
238
usage();
239
}
240
break;
241
case 's':
242
conf->supstr = optarg;
243
break;
244
case 'V':
245
(void) printf(_("%s version %s\n"), getprogname(),
246
PACKAGE_VERSION);
247
(void) printf(_("%s grammar version %d\n"), getprogname(),
248
SUDOERS_GRAMMAR_VERSION);
249
exitcode = EXIT_SUCCESS;
250
goto done;
251
case OPT_GROUP_FILE:
252
grfile = optarg;
253
break;
254
case OPT_PASSWD_FILE:
255
pwfile = optarg;
256
break;
257
default:
258
usage();
259
/* NOTREACHED */
260
}
261
}
262
argc -= optind;
263
argv += optind;
264
265
if (conf->logfile != NULL) {
266
logfp = fopen(conf->logfile, "w");
267
if (logfp == NULL)
268
sudo_fatalx(U_("unable to open log file %s"), conf->logfile);
269
}
270
271
if (conf->input_format != NULL) {
272
if (strcasecmp(conf->input_format, "ldif") == 0) {
273
input_format = format_ldif;
274
} else if (strcasecmp(conf->input_format, "sudoers") == 0) {
275
input_format = format_sudoers; // -V1048
276
} else {
277
sudo_warnx(U_("unsupported input format %s"), conf->input_format);
278
usage();
279
}
280
}
281
if (conf->output_format != NULL) {
282
if (strcasecmp(conf->output_format, "csv") == 0) {
283
output_format = format_csv;
284
conf->store_options = true;
285
} else if (strcasecmp(conf->output_format, "json") == 0) {
286
output_format = format_json;
287
conf->store_options = true;
288
} else if (strcasecmp(conf->output_format, "ldif") == 0) {
289
output_format = format_ldif; // -V1048
290
conf->store_options = true;
291
} else if (strcasecmp(conf->output_format, "sudoers") == 0) {
292
output_format = format_sudoers;
293
conf->store_options = false;
294
} else {
295
sudo_warnx(U_("unsupported output format %s"), conf->output_format);
296
usage();
297
}
298
}
299
if (conf->filter != NULL) {
300
/* We always expand aliases when filtering (may change in future). */
301
if (!cvtsudoers_parse_filter(conf->filter))
302
usage();
303
}
304
if (conf->defstr != NULL) {
305
conf->defaults = cvtsudoers_parse_defaults(conf->defstr);
306
if (conf->defaults == (unsigned int)-1)
307
usage();
308
}
309
if (conf->supstr != NULL) {
310
conf->suppress = cvtsudoers_parse_suppression(conf->supstr);
311
if (conf->suppress == (unsigned int)-1)
312
usage();
313
}
314
315
/* Apply padding to sudo_order if present. */
316
if (conf->sudo_order != 0 && conf->order_padding != 0) {
317
unsigned int multiplier = 1;
318
319
do {
320
multiplier *= 10;
321
} while (--conf->order_padding != 0);
322
conf->sudo_order *= multiplier;
323
conf->order_max = conf->sudo_order + (multiplier - 1);
324
conf->order_padding = multiplier;
325
}
326
327
/* If no base DN specified, check SUDOERS_BASE. */
328
if (conf->sudoers_base == NULL) {
329
conf->sudoers_base = getenv("SUDOERS_BASE");
330
if (conf->sudoers_base != NULL && *conf->sudoers_base != '\0') {
331
if ((conf->sudoers_base = strdup(conf->sudoers_base)) == NULL) {
332
sudo_fatalx(U_("%s: %s"), __func__,
333
U_("unable to allocate memory"));
334
}
335
}
336
}
337
338
/* Set pwutil backend to use the filter data. */
339
if (conf->filter != NULL && !match_local) {
340
sudo_pwutil_set_backend(cvtsudoers_make_pwitem, cvtsudoers_make_gritem,
341
cvtsudoers_make_gidlist_item, cvtsudoers_make_grlist_item, NULL);
342
} else {
343
if (grfile != NULL)
344
testsudoers_setgrfile(grfile);
345
if (pwfile != NULL)
346
testsudoers_setpwfile(pwfile);
347
sudo_pwutil_set_backend(
348
pwfile ? testsudoers_make_pwitem : NULL,
349
grfile ? testsudoers_make_gritem : NULL,
350
grfile ? testsudoers_make_gidlist_item : NULL,
351
grfile ? testsudoers_make_grlist_item : NULL,
352
NULL);
353
}
354
355
/* We may need the hostname to resolve %h escapes in include files. */
356
if (!sudoers_sethost(&ctx, NULL, NULL))
357
goto done;
358
359
do {
360
char *lhost = NULL, *shost = NULL;
361
362
/* Input file (defaults to stdin). */
363
if (argc > 0)
364
input_file = argv[0];
365
366
/* Check for optional hostname prefix on the input file. */
367
cp = strchr(input_file, ':');
368
if (cp != NULL) {
369
struct stat sb;
370
371
if (strcmp(cp, ":-") == 0 || stat(input_file, &sb) == -1) {
372
lhost = strndup(input_file, (size_t)(cp - input_file));
373
if (lhost == NULL)
374
sudo_fatalx("%s", U_("unable to allocate memory"));
375
input_file = cp + 1;
376
cp = strchr(lhost, '.');
377
if (cp == NULL) {
378
shost = lhost;
379
} else {
380
shost = strndup(lhost, (size_t)(cp - lhost));
381
}
382
}
383
}
384
385
if (strcmp(input_file, "-") != 0) {
386
if (strcmp(input_file, output_file) == 0) {
387
sudo_fatalx(U_("%s: input and output files must be different"),
388
input_file);
389
}
390
}
391
392
parse_tree = malloc(sizeof(*parse_tree));
393
if (parse_tree == NULL)
394
sudo_fatalx("%s", U_("unable to allocate memory"));
395
init_parse_tree(parse_tree, lhost, shost, &ctx, NULL);
396
TAILQ_INSERT_TAIL(&parse_trees, parse_tree, entries);
397
398
/* Setup defaults data structures. */
399
if (!init_defaults()) {
400
sudo_fatalx("%s",
401
U_("unable to initialize sudoers default values"));
402
}
403
404
switch (input_format) {
405
case format_ldif:
406
if (!parse_ldif(parse_tree, input_file, conf))
407
goto done;
408
break;
409
case format_sudoers:
410
if (!parse_sudoers(&ctx, input_file, conf))
411
goto done;
412
reparent_parse_tree(parse_tree);
413
break;
414
default:
415
sudo_fatalx("error: unhandled input %d", input_format);
416
}
417
418
/* Apply filters. */
419
filter_userspecs(parse_tree, conf);
420
filter_defaults(parse_tree, conf);
421
if (filters != NULL) {
422
alias_remove_unused(parse_tree);
423
if (conf->prune_matches && conf->expand_aliases)
424
alias_prune(parse_tree, conf);
425
}
426
427
argc--;
428
argv++;
429
} while (argc > 0);
430
431
parse_tree = TAILQ_FIRST(&parse_trees);
432
if (TAILQ_NEXT(parse_tree, entries)) {
433
/* Multiple sudoers files, merge them all. */
434
parse_tree = merge_sudoers(&parse_trees, &merged_tree);
435
}
436
437
switch (output_format) {
438
case format_csv:
439
exitcode = !convert_sudoers_csv(parse_tree, output_file, conf);
440
break;
441
case format_json:
442
exitcode = !convert_sudoers_json(parse_tree, output_file, conf);
443
break;
444
case format_ldif:
445
exitcode = !convert_sudoers_ldif(parse_tree, output_file, conf);
446
break;
447
case format_sudoers:
448
exitcode = !convert_sudoers_sudoers(parse_tree, output_file, conf);
449
break;
450
default:
451
sudo_fatalx("error: unhandled output format %d", output_format);
452
}
453
454
done:
455
sudoers_ctx_free(&ctx);
456
free_parse_tree(&merged_tree);
457
while ((parse_tree = TAILQ_FIRST(&parse_trees)) != NULL) {
458
TAILQ_REMOVE(&parse_trees, parse_tree, entries);
459
free_parse_tree(parse_tree);
460
free(parse_tree);
461
}
462
cvtsudoers_conf_free(conf);
463
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
464
return exitcode;
465
}
466
467
void
468
log_warnx(const char * restrict fmt, ...)
469
{
470
va_list ap;
471
472
va_start(ap, fmt);
473
if (logfp != NULL) {
474
vfprintf(logfp, fmt, ap);
475
fputc('\n', logfp);
476
} else {
477
sudo_vwarnx_nodebug(fmt, ap);
478
}
479
va_end(ap);
480
}
481
482
/*
483
* cvtsudoers configuration data.
484
*/
485
static struct cvtsudoers_config cvtsudoers_config = INITIAL_CONFIG;
486
static struct cvtsudoers_conf_table cvtsudoers_conf_vars[] = {
487
{ "order_start", CONF_UINT, &cvtsudoers_config.sudo_order },
488
{ "order_increment", CONF_UINT, &cvtsudoers_config.order_increment },
489
{ "order_padding", CONF_UINT, &cvtsudoers_config.order_padding },
490
{ "sudoers_base", CONF_STR, &cvtsudoers_config.sudoers_base },
491
{ "input_format", CONF_STR, &cvtsudoers_config.input_format },
492
{ "output_format", CONF_STR, &cvtsudoers_config.output_format },
493
{ "match", CONF_STR, &cvtsudoers_config.filter },
494
{ "match_local", CONF_BOOL, &cvtsudoers_config.match_local },
495
{ "logfile", CONF_STR, &cvtsudoers_config.logfile },
496
{ "defaults", CONF_STR, &cvtsudoers_config.defstr },
497
{ "suppress", CONF_STR, &cvtsudoers_config.supstr },
498
{ "group_file", CONF_STR, &cvtsudoers_config.group_file },
499
{ "passwd_file", CONF_STR, &cvtsudoers_config.passwd_file },
500
{ "expand_aliases", CONF_BOOL, &cvtsudoers_config.expand_aliases },
501
{ "prune_matches", CONF_BOOL, &cvtsudoers_config.prune_matches }
502
};
503
504
/*
505
* Look up keyword in config table.
506
* Returns true if found, else false.
507
*/
508
static bool
509
cvtsudoers_parse_keyword(const char *conf_file, const char *keyword,
510
const char *value, struct cvtsudoers_conf_table *table)
511
{
512
struct cvtsudoers_conf_table *cur;
513
const char *errstr;
514
debug_decl(sudo_ldap_parse_keyword, SUDOERS_DEBUG_UTIL);
515
516
/* Look up keyword in config tables */
517
for (cur = table; cur->conf_str != NULL; cur++) {
518
if (strcasecmp(keyword, cur->conf_str) == 0) {
519
switch (cur->type) {
520
case CONF_BOOL:
521
*(bool *)(cur->valp) = sudo_strtobool(value) == true;
522
break;
523
case CONF_UINT:
524
{
525
unsigned int uval =
526
(unsigned int)sudo_strtonum(value, 0, UINT_MAX, &errstr);
527
if (errstr != NULL) {
528
sudo_warnx(U_("%s: %s: %s: %s"),
529
conf_file, keyword, value, U_(errstr));
530
continue;
531
}
532
*(unsigned int *)(cur->valp) = uval;
533
}
534
break;
535
case CONF_STR:
536
{
537
char *cp = strdup(value);
538
if (cp == NULL) {
539
sudo_fatalx(U_("%s: %s"), __func__,
540
U_("unable to allocate memory"));
541
}
542
free(*(char **)(cur->valp));
543
*(char **)(cur->valp) = cp;
544
break;
545
}
546
}
547
debug_return_bool(true);
548
}
549
}
550
debug_return_bool(false);
551
}
552
553
static struct cvtsudoers_config *
554
cvtsudoers_conf_read(const char *path)
555
{
556
char conf_file[PATH_MAX], *line = NULL;
557
size_t linesize = 0;
558
FILE *fp = NULL;
559
int fd = -1;
560
debug_decl(cvtsudoers_conf_read, SUDOERS_DEBUG_UTIL);
561
562
if (path != NULL) {
563
/* Empty string means use the defaults. */
564
if (*path == '\0')
565
debug_return_ptr(&cvtsudoers_config);
566
if (strlcpy(conf_file, path, sizeof(conf_file)) >= sizeof(conf_file))
567
errno = ENAMETOOLONG;
568
else
569
fd = open(conf_file, O_RDONLY);
570
} else {
571
fd = sudo_open_conf_path(_PATH_CVTSUDOERS_CONF, conf_file,
572
sizeof(conf_file), NULL);
573
}
574
if (fd != -1)
575
fp = fdopen(fd, "r");
576
if (fp == NULL) {
577
if (path != NULL || errno != ENOENT)
578
sudo_warn("%s", conf_file);
579
debug_return_ptr(&cvtsudoers_config);
580
}
581
582
while (sudo_parseln(&line, &linesize, NULL, fp, 0) != -1) {
583
char *keyword, *value;
584
size_t len;
585
586
if (*line == '\0')
587
continue; /* skip empty line */
588
589
/* Parse keyword = value */
590
keyword = line;
591
if ((value = strchr(line, '=')) == NULL || value == line)
592
continue;
593
len = (size_t)(value - line);
594
595
/* Trim whitespace after keyword and NUL-terminate. */
596
while (len > 0 && isblank((unsigned char)line[len - 1]))
597
len--;
598
line[len] = '\0';
599
600
/* Trim whitespace before value. */
601
do {
602
value++;
603
} while (isblank((unsigned char)*value));
604
605
/* Look up keyword in config tables */
606
if (!cvtsudoers_parse_keyword(conf_file, keyword, value, cvtsudoers_conf_vars))
607
sudo_warnx(U_("%s: unknown key word %s"), conf_file, keyword);
608
}
609
free(line);
610
fclose(fp);
611
612
debug_return_ptr(&cvtsudoers_config);
613
}
614
615
static void
616
cvtsudoers_conf_free(struct cvtsudoers_config *conf)
617
{
618
debug_decl(cvtsudoers_conf_free, SUDOERS_DEBUG_UTIL);
619
620
if (conf != NULL) {
621
free(conf->sudoers_base);
622
free(conf->input_format);
623
free(conf->output_format);
624
conf->sudoers_base = NULL;
625
conf->input_format = NULL;
626
conf->output_format = NULL;
627
}
628
629
debug_return;
630
}
631
632
static unsigned int
633
cvtsudoers_parse_defaults(char *expression)
634
{
635
char *last, *cp = expression;
636
unsigned int flags = 0;
637
debug_decl(cvtsudoers_parse_defaults, SUDOERS_DEBUG_UTIL);
638
639
for ((cp = strtok_r(cp, ",", &last)); cp != NULL; (cp = strtok_r(NULL, ",", &last))) {
640
if (strcasecmp(cp, "all") == 0) {
641
SET(flags, CVT_DEFAULTS_ALL);
642
} else if (strcasecmp(cp, "global") == 0) {
643
SET(flags, CVT_DEFAULTS_GLOBAL);
644
} else if (strcasecmp(cp, "user") == 0) {
645
SET(flags, CVT_DEFAULTS_USER);
646
} else if (strcasecmp(cp, "runas") == 0) {
647
SET(flags, CVT_DEFAULTS_RUNAS);
648
} else if (strcasecmp(cp, "host") == 0) {
649
SET(flags, CVT_DEFAULTS_HOST);
650
} else if (strcasecmp(cp, "command") == 0) {
651
SET(flags, CVT_DEFAULTS_CMND);
652
} else {
653
sudo_warnx(U_("invalid defaults type: %s"), cp);
654
debug_return_uint((unsigned int)-1);
655
}
656
}
657
658
debug_return_uint(flags);
659
}
660
661
static unsigned int
662
cvtsudoers_parse_suppression(char *expression)
663
{
664
char *last, *cp = expression;
665
unsigned int flags = 0;
666
debug_decl(cvtsudoers_parse_suppression, SUDOERS_DEBUG_UTIL);
667
668
for ((cp = strtok_r(cp, ",", &last)); cp != NULL; (cp = strtok_r(NULL, ",", &last))) {
669
if (strcasecmp(cp, "defaults") == 0) {
670
SET(flags, SUPPRESS_DEFAULTS);
671
} else if (strcasecmp(cp, "aliases") == 0) {
672
SET(flags, SUPPRESS_ALIASES);
673
} else if (strcasecmp(cp, "privileges") == 0 || strcasecmp(cp, "privs") == 0) {
674
SET(flags, SUPPRESS_PRIVS);
675
} else {
676
sudo_warnx(U_("invalid suppression type: %s"), cp);
677
debug_return_uint((unsigned int)-1);
678
}
679
}
680
681
debug_return_uint(flags);
682
}
683
684
static bool
685
cvtsudoers_parse_filter(char *expression)
686
{
687
char *last, *cp = expression;
688
debug_decl(cvtsudoers_parse_filter, SUDOERS_DEBUG_UTIL);
689
690
if (filters == NULL) {
691
if ((filters = malloc(sizeof(*filters))) == NULL) {
692
sudo_fatalx(U_("%s: %s"), __func__,
693
U_("unable to allocate memory"));
694
}
695
STAILQ_INIT(&filters->users);
696
STAILQ_INIT(&filters->groups);
697
STAILQ_INIT(&filters->hosts);
698
STAILQ_INIT(&filters->cmnds);
699
}
700
701
for ((cp = strtok_r(cp, ",", &last)); cp != NULL; (cp = strtok_r(NULL, ",", &last))) {
702
/*
703
* Filter expression:
704
* user=foo,group=bar,host=baz
705
*/
706
char *keyword;
707
struct sudoers_string *s;
708
709
if ((s = malloc(sizeof(*s))) == NULL) {
710
sudo_fatalx(U_("%s: %s"), __func__,
711
U_("unable to allocate memory"));
712
}
713
714
/* Parse keyword = value */
715
keyword = cp;
716
if ((cp = strchr(cp, '=')) == NULL) {
717
sudo_warnx(U_("invalid filter: %s"), keyword);
718
free(s);
719
debug_return_bool(false);
720
}
721
*cp++ = '\0';
722
s->str = cp;
723
724
if (strcmp(keyword, "user") == 0) {
725
STAILQ_INSERT_TAIL(&filters->users, s, entries);
726
} else if (strcmp(keyword, "group") == 0) {
727
STAILQ_INSERT_TAIL(&filters->groups, s, entries);
728
} else if (strcmp(keyword, "host") == 0) {
729
STAILQ_INSERT_TAIL(&filters->hosts, s, entries);
730
} else if (strcmp(keyword, "cmnd") == 0 || strcmp(keyword, "cmd") == 0) {
731
STAILQ_INSERT_TAIL(&filters->cmnds, s, entries);
732
} else {
733
sudo_warnx(U_("invalid filter: %s"), keyword);
734
free(s);
735
debug_return_bool(false);
736
}
737
}
738
739
debug_return_bool(true);
740
}
741
742
static bool
743
parse_ldif(struct sudoers_parse_tree *parse_tree, const char *input_file,
744
struct cvtsudoers_config *conf)
745
{
746
FILE *fp = stdin;
747
bool ret = false;
748
debug_decl(parse_ldif, SUDOERS_DEBUG_UTIL);
749
750
/* Open LDIF file and parse it. */
751
if (strcmp(input_file, "-") != 0) {
752
if ((fp = fopen(input_file, "r")) == NULL)
753
sudo_warn(U_("unable to open %s"), input_file);
754
}
755
if (fp != NULL) {
756
ret = sudoers_parse_ldif(parse_tree, fp, conf->sudoers_base,
757
conf->store_options);
758
if (fp != stdin)
759
fclose(fp);
760
}
761
debug_return_bool(ret);
762
}
763
764
static bool
765
parse_sudoers(struct sudoers_context *ctx, const char *input_file,
766
struct cvtsudoers_config *conf)
767
{
768
debug_decl(parse_sudoers, SUDOERS_DEBUG_UTIL);
769
770
/* Open sudoers file and parse it. */
771
if (strcmp(input_file, "-") == 0) {
772
sudoersin = stdin;
773
input_file = "stdin";
774
} else if ((sudoersin = fopen(input_file, "r")) == NULL)
775
sudo_fatal(U_("unable to open %s"), input_file);
776
init_parser(ctx, input_file);
777
if (sudoersparse() && !parse_error) {
778
sudo_warnx(U_("failed to parse %s file, unknown error"), input_file);
779
parse_error = true;
780
}
781
debug_return_bool(!parse_error);
782
}
783
784
FILE *
785
open_sudoers(const char *file, char **outfile, bool doedit, bool *keepopen)
786
{
787
return fopen(file, "r");
788
}
789
790
static bool
791
userlist_matches_filter(struct sudoers_parse_tree *parse_tree,
792
struct member_list *users, struct cvtsudoers_config *conf)
793
{
794
struct sudoers_string *s;
795
struct member *m, *next;
796
bool ret = false;
797
debug_decl(userlist_matches_filter, SUDOERS_DEBUG_UTIL);
798
799
if (filters == NULL ||
800
(STAILQ_EMPTY(&filters->users) && STAILQ_EMPTY(&filters->groups)))
801
debug_return_bool(true);
802
803
TAILQ_FOREACH_REVERSE_SAFE(m, users, member_list, entries, next) {
804
bool matched = false;
805
806
if (STAILQ_EMPTY(&filters->users)) {
807
struct passwd pw;
808
809
/*
810
* Only groups in filter, make a fake user so userlist_matches()
811
* can do its thing.
812
*/
813
memset(&pw, 0, sizeof(pw));
814
pw.pw_name = (char *)"_nobody";
815
pw.pw_uid = (uid_t)-1;
816
pw.pw_gid = (gid_t)-1;
817
818
if (user_matches(parse_tree, &pw, m) == ALLOW)
819
matched = true;
820
} else {
821
STAILQ_FOREACH(s, &filters->users, entries) {
822
struct passwd *pw = NULL;
823
824
/* An upper case filter entry may be a User_Alias */
825
/* XXX - doesn't handle nested aliases */
826
if (m->type == ALIAS && !conf->expand_aliases) {
827
if (strcmp(m->name, s->str) == 0) {
828
matched = true;
829
break;
830
}
831
}
832
833
if (s->str[0] == '#') {
834
const char *errstr;
835
uid_t uid = sudo_strtoid(s->str + 1, &errstr);
836
if (errstr == NULL)
837
pw = sudo_getpwuid(uid);
838
}
839
if (pw == NULL)
840
pw = sudo_getpwnam(s->str);
841
if (pw == NULL)
842
continue;
843
844
if (user_matches(parse_tree, pw, m) == ALLOW)
845
matched = true;
846
sudo_pw_delref(pw);
847
848
/* Only need one user in the filter to match. */
849
if (matched)
850
break;
851
}
852
}
853
854
if (matched) {
855
ret = true;
856
} else if (conf->prune_matches) {
857
TAILQ_REMOVE(users, m, entries);
858
free_member(m);
859
}
860
}
861
862
debug_return_bool(ret);
863
}
864
865
static bool
866
hostlist_matches_filter(struct sudoers_parse_tree *parse_tree,
867
struct member_list *hostlist, struct cvtsudoers_config *conf)
868
{
869
struct sudoers_string *s;
870
struct member *m, *next;
871
char *lhost, *shost;
872
bool ret = false;
873
char **shosts;
874
size_t n = 0;
875
debug_decl(hostlist_matches_filter, SUDOERS_DEBUG_UTIL);
876
877
if (filters == NULL || STAILQ_EMPTY(&filters->hosts))
878
debug_return_bool(true);
879
880
/* Create an array of short host names. */
881
STAILQ_FOREACH(s, &filters->hosts, entries) {
882
n++;
883
}
884
shosts = reallocarray(NULL, n, sizeof(char *));
885
if (shosts == NULL)
886
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
887
n = 0;
888
STAILQ_FOREACH(s, &filters->hosts, entries) {
889
lhost = s->str;
890
if ((shost = strchr(lhost, '.')) != NULL) {
891
shost = strndup(lhost, (size_t)(shost - lhost));
892
if (shost == NULL) {
893
sudo_fatalx(U_("%s: %s"), __func__,
894
U_("unable to allocate memory"));
895
}
896
} else {
897
shost = lhost;
898
}
899
shosts[n++] = shost;
900
}
901
902
TAILQ_FOREACH_REVERSE_SAFE(m, hostlist, member_list, entries, next) {
903
bool matched = false;
904
n = 0;
905
STAILQ_FOREACH(s, &filters->hosts, entries) {
906
lhost = s->str;
907
shost = shosts[n++];
908
909
/* An upper case filter entry may be a Host_Alias */
910
/* XXX - doesn't handle nested aliases */
911
if (m->type == ALIAS && !conf->expand_aliases) {
912
if (strcmp(m->name, s->str) == 0) {
913
matched = true;
914
break;
915
}
916
}
917
918
/* Only need one host in the filter to match. */
919
/* XXX - can't use netgroup_tuple with NULL pw */
920
if (host_matches(parse_tree, NULL, lhost, shost, m) == ALLOW) {
921
matched = true;
922
break;
923
}
924
}
925
926
if (matched) {
927
ret = true;
928
} else if (conf->prune_matches) {
929
TAILQ_REMOVE(hostlist, m, entries);
930
free_member(m);
931
}
932
}
933
934
/* Free shosts array and its contents. */
935
n = 0;
936
STAILQ_FOREACH(s, &filters->hosts, entries) {
937
lhost = s->str;
938
shost = shosts[n++];
939
if (shost != lhost)
940
free(shost);
941
}
942
free(shosts);
943
944
debug_return_bool(ret);
945
}
946
947
static bool
948
cmnd_matches_filter(struct sudoers_parse_tree *parse_tree,
949
struct member *m, struct cvtsudoers_config *conf)
950
{
951
struct sudoers_context *ctx = parse_tree->ctx;
952
struct sudoers_string *s;
953
bool matched = false;
954
debug_decl(cmnd_matches_filter, SUDOERS_DEBUG_UTIL);
955
956
/* TODO: match on runasuserlist/runasgrouplist, notbefore/notafter etc */
957
STAILQ_FOREACH(s, &filters->cmnds, entries) {
958
/* An upper case filter entry may be a Cmnd_Alias */
959
/* XXX - doesn't handle nested aliases */
960
if (m->type == ALIAS && !conf->expand_aliases) {
961
if (strcmp(m->name, s->str) == 0) {
962
matched = true;
963
break;
964
}
965
}
966
967
/* Only need one command in the filter to match. */
968
ctx->user.cmnd = s->str;
969
ctx->user.cmnd_base = sudo_basename(ctx->user.cmnd);
970
if (cmnd_matches(parse_tree, m, NULL, NULL) == ALLOW) {
971
matched = true;
972
break;
973
}
974
}
975
ctx->user.cmnd_base = NULL;
976
ctx->user.cmnd = NULL;
977
978
debug_return_bool(matched);
979
}
980
981
static bool
982
cmndlist_matches_filter(struct sudoers_parse_tree *parse_tree,
983
struct member_list *cmndlist, struct cvtsudoers_config *conf)
984
{
985
struct member *m, *next;
986
bool ret = false;
987
debug_decl(cmndlist_matches_filter, SUDOERS_DEBUG_UTIL);
988
989
if (filters == NULL || STAILQ_EMPTY(&filters->cmnds))
990
debug_return_bool(true);
991
992
TAILQ_FOREACH_REVERSE_SAFE(m, cmndlist, member_list, entries, next) {
993
bool matched = cmnd_matches_filter(parse_tree, m, conf);
994
if (matched) {
995
ret = true;
996
} else if (conf->prune_matches) {
997
TAILQ_REMOVE(cmndlist, m, entries);
998
free_member(m);
999
}
1000
}
1001
1002
debug_return_bool(ret);
1003
}
1004
1005
static bool
1006
cmndspeclist_matches_filter(struct sudoers_parse_tree *parse_tree,
1007
struct cmndspec_list *cmndspecs, struct cvtsudoers_config *conf)
1008
{
1009
struct cmndspec *cs, *next;
1010
bool ret = false;
1011
debug_decl(cmndspeclist_matches_filter, SUDOERS_DEBUG_UTIL);
1012
1013
if (filters == NULL || STAILQ_EMPTY(&filters->cmnds))
1014
debug_return_bool(true);
1015
1016
TAILQ_FOREACH_REVERSE_SAFE(cs, cmndspecs, cmndspec_list, entries, next) {
1017
bool matched = cmnd_matches_filter(parse_tree, cs->cmnd, conf);
1018
if (matched) {
1019
ret = true;
1020
} else if (conf->prune_matches) {
1021
/* free_cmndspec() removes cs from the list itself. */
1022
free_cmndspec(cs, cmndspecs);
1023
}
1024
}
1025
1026
debug_return_bool(ret);
1027
}
1028
1029
/*
1030
* Display Defaults entries
1031
*/
1032
static bool
1033
print_defaults_sudoers(struct sudoers_parse_tree *parse_tree,
1034
struct sudo_lbuf *lbuf, bool expand_aliases)
1035
{
1036
struct defaults *def, *next;
1037
debug_decl(print_defaults_sudoers, SUDOERS_DEBUG_UTIL);
1038
1039
TAILQ_FOREACH_SAFE(def, &parse_tree->defaults, entries, next) {
1040
sudoers_format_default_line(lbuf, parse_tree, def, &next,
1041
expand_aliases);
1042
}
1043
1044
debug_return_bool(!sudo_lbuf_error(lbuf));
1045
}
1046
1047
static int
1048
print_alias_sudoers(struct sudoers_parse_tree *parse_tree, struct alias *a,
1049
void *v)
1050
{
1051
struct sudo_lbuf *lbuf = v;
1052
struct member *m;
1053
debug_decl(print_alias_sudoers, SUDOERS_DEBUG_UTIL);
1054
1055
sudo_lbuf_append(lbuf, "%s %s = ", alias_type_to_string(a->type),
1056
a->name);
1057
TAILQ_FOREACH(m, &a->members, entries) {
1058
if (m != TAILQ_FIRST(&a->members))
1059
sudo_lbuf_append(lbuf, ", ");
1060
sudoers_format_member(lbuf, parse_tree, m, NULL, UNSPEC);
1061
}
1062
sudo_lbuf_append(lbuf, "\n");
1063
1064
debug_return_int(sudo_lbuf_error(lbuf) ? -1 : 0);
1065
}
1066
1067
/*
1068
* Display aliases
1069
*/
1070
static bool
1071
print_aliases_sudoers(struct sudoers_parse_tree *parse_tree,
1072
struct sudo_lbuf *lbuf)
1073
{
1074
debug_decl(print_aliases_sudoers, SUDOERS_DEBUG_UTIL);
1075
1076
debug_return_bool(alias_apply(parse_tree, print_alias_sudoers, lbuf));
1077
}
1078
1079
static FILE *output_fp; /* global for convert_sudoers_output */
1080
1081
static int
1082
convert_sudoers_output(const char * restrict buf)
1083
{
1084
return fputs(buf, output_fp);
1085
}
1086
1087
/*
1088
* Apply filters to userspecs, removing non-matching entries.
1089
*/
1090
static void
1091
filter_userspecs(struct sudoers_parse_tree *parse_tree,
1092
struct cvtsudoers_config *conf)
1093
{
1094
struct userspec *us, *next_us;
1095
struct privilege *priv, *next_priv;
1096
debug_decl(filter_userspecs, SUDOERS_DEBUG_UTIL);
1097
1098
if (filters == NULL)
1099
debug_return;
1100
1101
/*
1102
* Does not currently prune out non-matching entries in the user or
1103
* host lists. It acts more like a grep than a true filter.
1104
* In the future, we may want to add a prune option.
1105
*/
1106
TAILQ_FOREACH_SAFE(us, &parse_tree->userspecs, entries, next_us) {
1107
if (!userlist_matches_filter(parse_tree, &us->users, conf)) {
1108
TAILQ_REMOVE(&parse_tree->userspecs, us, entries);
1109
free_userspec(us);
1110
continue;
1111
}
1112
TAILQ_FOREACH_SAFE(priv, &us->privileges, entries, next_priv) {
1113
if (!hostlist_matches_filter(parse_tree, &priv->hostlist, conf) ||
1114
!cmndspeclist_matches_filter(parse_tree, &priv->cmndlist, conf)) {
1115
TAILQ_REMOVE(&us->privileges, priv, entries);
1116
free_privilege(priv);
1117
}
1118
}
1119
if (TAILQ_EMPTY(&us->privileges)) {
1120
TAILQ_REMOVE(&parse_tree->userspecs, us, entries);
1121
free_userspec(us);
1122
continue;
1123
}
1124
}
1125
debug_return;
1126
}
1127
1128
/*
1129
* Check whether the alias described by "alias_name" is the same
1130
* as "name" or includes an alias called "name".
1131
* Returns true if matched, else false.
1132
*/
1133
static bool
1134
alias_matches(struct sudoers_parse_tree *parse_tree, const char *name,
1135
const char *alias_name, short alias_type)
1136
{
1137
struct alias *a;
1138
struct member *m;
1139
bool ret = false;
1140
debug_decl(alias_matches, SUDOERS_DEBUG_ALIAS);
1141
1142
if (strcmp(name, alias_name) == 0)
1143
debug_return_bool(true);
1144
1145
a = alias_get(parse_tree, alias_name, alias_type);
1146
if (a != NULL) {
1147
TAILQ_FOREACH(m, &a->members, entries) {
1148
if (m->type != ALIAS)
1149
continue;
1150
if (alias_matches(parse_tree, name, m->name, alias_type)) {
1151
ret = true;
1152
break;
1153
}
1154
}
1155
alias_put(a);
1156
}
1157
1158
debug_return_bool(ret);
1159
}
1160
1161
/*
1162
* Check whether userspecs uses the aliases in the specified member lists.
1163
* If used, they are removed (and freed) from the list.
1164
* This does *not* check Defaults for used aliases, only userspecs.
1165
*/
1166
static void
1167
alias_used_by_userspecs(struct sudoers_parse_tree *parse_tree,
1168
struct member_list *user_aliases, struct member_list *runas_aliases,
1169
struct member_list *host_aliases, struct member_list *cmnd_aliases)
1170
{
1171
struct privilege *priv, *priv_next;
1172
struct userspec *us, *us_next;
1173
struct cmndspec *cs, *cs_next;
1174
struct member *m, *m_next;
1175
struct member *am, *am_next;
1176
debug_decl(alias_used_by_userspecs, SUDOERS_DEBUG_ALIAS);
1177
1178
/* Iterate over the policy, checking for aliases. */
1179
TAILQ_FOREACH_SAFE(us, &parse_tree->userspecs, entries, us_next) {
1180
TAILQ_FOREACH_SAFE(m, &us->users, entries, m_next) {
1181
if (m->type == ALIAS) {
1182
/* If alias is used, remove from user_aliases and free. */
1183
TAILQ_FOREACH_SAFE(am, user_aliases, entries, am_next) {
1184
if (alias_matches(parse_tree, am->name, m->name, USERALIAS)) {
1185
TAILQ_REMOVE(user_aliases, am, entries);
1186
free_member(am);
1187
}
1188
}
1189
}
1190
}
1191
TAILQ_FOREACH_SAFE(priv, &us->privileges, entries, priv_next) {
1192
TAILQ_FOREACH(m, &priv->hostlist, entries) {
1193
if (m->type == ALIAS) {
1194
/* If alias is used, remove from host_aliases and free. */
1195
TAILQ_FOREACH_SAFE(am, host_aliases, entries, am_next) {
1196
if (alias_matches(parse_tree, am->name, m->name, HOSTALIAS)) {
1197
TAILQ_REMOVE(host_aliases, am, entries);
1198
free_member(am);
1199
}
1200
}
1201
}
1202
}
1203
TAILQ_FOREACH_SAFE(cs, &priv->cmndlist, entries, cs_next) {
1204
if (cs->runasuserlist != NULL) {
1205
TAILQ_FOREACH_SAFE(m, cs->runasuserlist, entries, m_next) {
1206
if (m->type == ALIAS) {
1207
/* If alias is used, remove from runas_aliases and free. */
1208
TAILQ_FOREACH_SAFE(am, runas_aliases, entries, am_next) {
1209
if (alias_matches(parse_tree, am->name, m->name, RUNASALIAS)) {
1210
TAILQ_REMOVE(runas_aliases, am, entries);
1211
free_member(am);
1212
}
1213
}
1214
}
1215
}
1216
}
1217
if (cs->runasgrouplist != NULL) {
1218
TAILQ_FOREACH_SAFE(m, cs->runasgrouplist, entries, m_next) {
1219
if (m->type == ALIAS) {
1220
/* If alias is used, remove from runas_aliases and free. */
1221
TAILQ_FOREACH_SAFE(am, runas_aliases, entries, am_next) {
1222
if (alias_matches(parse_tree, am->name, m->name, RUNASALIAS)) {
1223
TAILQ_REMOVE(runas_aliases, am, entries);
1224
free_member(am);
1225
}
1226
}
1227
}
1228
}
1229
}
1230
if ((m = cs->cmnd)->type == ALIAS) {
1231
/* If alias is used, remove from cmnd_aliases and free. */
1232
TAILQ_FOREACH_SAFE(am, cmnd_aliases, entries, am_next) {
1233
if (alias_matches(parse_tree, am->name, m->name, CMNDALIAS)) {
1234
TAILQ_REMOVE(cmnd_aliases, am, entries);
1235
free_member(am);
1236
}
1237
}
1238
}
1239
}
1240
}
1241
}
1242
1243
debug_return;
1244
}
1245
1246
/*
1247
* For each alias listed in members, remove and free the alias.
1248
* Frees the contents of members too.
1249
*/
1250
static void
1251
free_aliases_by_members(struct sudoers_parse_tree *parse_tree,
1252
struct member_list *members, short type)
1253
{
1254
struct member *m;
1255
struct alias *a;
1256
debug_decl(free_aliases_by_members, SUDOERS_DEBUG_ALIAS);
1257
1258
while ((m = TAILQ_FIRST(members)) != NULL) {
1259
TAILQ_REMOVE(members, m, entries);
1260
a = alias_remove(parse_tree, m->name, type);
1261
alias_free(a);
1262
free_member(m);
1263
}
1264
debug_return;
1265
}
1266
1267
/*
1268
* Apply filters to host/user-based Defaults, removing non-matching entries.
1269
*/
1270
static void
1271
filter_defaults(struct sudoers_parse_tree *parse_tree,
1272
struct cvtsudoers_config *conf)
1273
{
1274
struct member_list user_aliases = TAILQ_HEAD_INITIALIZER(user_aliases);
1275
struct member_list runas_aliases = TAILQ_HEAD_INITIALIZER(runas_aliases);
1276
struct member_list host_aliases = TAILQ_HEAD_INITIALIZER(host_aliases);
1277
struct member_list cmnd_aliases = TAILQ_HEAD_INITIALIZER(cmnd_aliases);
1278
struct defaults *def, *def_next;
1279
struct member *m, *m_next;
1280
short alias_type;
1281
debug_decl(filter_defaults, SUDOERS_DEBUG_DEFAULTS);
1282
1283
if (filters == NULL && conf->defaults == CVT_DEFAULTS_ALL)
1284
debug_return;
1285
1286
TAILQ_FOREACH_SAFE(def, &parse_tree->defaults, entries, def_next) {
1287
bool keep = true;
1288
1289
switch (def->type) {
1290
case DEFAULTS:
1291
if (!ISSET(conf->defaults, CVT_DEFAULTS_GLOBAL))
1292
keep = false;
1293
alias_type = UNSPEC;
1294
break;
1295
case DEFAULTS_USER:
1296
if (!ISSET(conf->defaults, CVT_DEFAULTS_USER) ||
1297
!userlist_matches_filter(parse_tree, &def->binding->members,
1298
conf)) {
1299
keep = false;
1300
}
1301
alias_type = USERALIAS;
1302
break;
1303
case DEFAULTS_RUNAS:
1304
if (!ISSET(conf->defaults, CVT_DEFAULTS_RUNAS))
1305
keep = false;
1306
alias_type = RUNASALIAS;
1307
break;
1308
case DEFAULTS_HOST:
1309
if (!ISSET(conf->defaults, CVT_DEFAULTS_HOST) ||
1310
!hostlist_matches_filter(parse_tree, &def->binding->members,
1311
conf)) {
1312
keep = false;
1313
}
1314
alias_type = HOSTALIAS;
1315
break;
1316
case DEFAULTS_CMND:
1317
if (!ISSET(conf->defaults, CVT_DEFAULTS_CMND) ||
1318
!cmndlist_matches_filter(parse_tree, &def->binding->members,
1319
conf)) {
1320
keep = false;
1321
}
1322
alias_type = CMNDALIAS;
1323
break;
1324
default:
1325
sudo_fatalx_nodebug("unexpected defaults type %d", def->type);
1326
break;
1327
}
1328
1329
if (!keep) {
1330
/*
1331
* Look for aliases used by the binding.
1332
* Consecutive Defaults can share the same binding.
1333
*/
1334
/* XXX - move to function */
1335
if (alias_type != UNSPEC &&
1336
(def_next == NULL || def->binding != def_next->binding)) {
1337
TAILQ_FOREACH_SAFE(m, &def->binding->members, entries, m_next) {
1338
if (m->type == ALIAS) {
1339
TAILQ_REMOVE(&def->binding->members, m, entries);
1340
switch (alias_type) {
1341
case USERALIAS:
1342
TAILQ_INSERT_TAIL(&user_aliases, m, entries);
1343
break;
1344
case RUNASALIAS:
1345
TAILQ_INSERT_TAIL(&runas_aliases, m, entries);
1346
break;
1347
case HOSTALIAS:
1348
TAILQ_INSERT_TAIL(&host_aliases, m, entries);
1349
break;
1350
case CMNDALIAS:
1351
TAILQ_INSERT_TAIL(&cmnd_aliases, m, entries);
1352
break;
1353
default:
1354
sudo_fatalx_nodebug("unexpected alias type %d",
1355
alias_type);
1356
break;
1357
}
1358
}
1359
}
1360
}
1361
TAILQ_REMOVE(&parse_tree->defaults, def, entries);
1362
free_default(def);
1363
}
1364
}
1365
1366
/* Determine unreferenced aliases and remove/free them. */
1367
alias_used_by_userspecs(parse_tree, &user_aliases, &runas_aliases,
1368
&host_aliases, &cmnd_aliases);
1369
free_aliases_by_members(parse_tree, &user_aliases, USERALIAS);
1370
free_aliases_by_members(parse_tree, &runas_aliases, RUNASALIAS);
1371
free_aliases_by_members(parse_tree, &host_aliases, HOSTALIAS);
1372
free_aliases_by_members(parse_tree, &cmnd_aliases, CMNDALIAS);
1373
1374
debug_return;
1375
}
1376
1377
/*
1378
* Remove unreferenced aliases.
1379
*/
1380
static void
1381
alias_remove_unused(struct sudoers_parse_tree *parse_tree)
1382
{
1383
struct rbtree *used_aliases;
1384
debug_decl(alias_remove_unused, SUDOERS_DEBUG_ALIAS);
1385
1386
used_aliases = alloc_aliases();
1387
if (used_aliases == NULL)
1388
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1389
1390
/* Move all referenced aliases to used_aliases. */
1391
if (!alias_find_used(parse_tree, used_aliases))
1392
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1393
1394
/* Only unreferenced aliases are left, swap and free the unused ones. */
1395
free_aliases(parse_tree->aliases);
1396
parse_tree->aliases = used_aliases;
1397
1398
debug_return;
1399
}
1400
1401
/*
1402
* Prune out non-matching entries from user and host aliases.
1403
*/
1404
static int
1405
alias_prune_helper(struct sudoers_parse_tree *parse_tree, struct alias *a,
1406
void *v)
1407
{
1408
struct cvtsudoers_config *conf = v;
1409
1410
/* XXX - misuse of these functions */
1411
switch (a->type) {
1412
case USERALIAS:
1413
userlist_matches_filter(parse_tree, &a->members, conf);
1414
break;
1415
case HOSTALIAS:
1416
hostlist_matches_filter(parse_tree, &a->members, conf);
1417
break;
1418
default:
1419
break;
1420
}
1421
1422
return 0;
1423
}
1424
1425
/*
1426
* Prune out non-matching entries from within aliases.
1427
*/
1428
static bool
1429
alias_prune(struct sudoers_parse_tree *parse_tree,
1430
struct cvtsudoers_config *conf)
1431
{
1432
debug_decl(alias_prune, SUDOERS_DEBUG_ALIAS);
1433
1434
debug_return_bool(alias_apply(parse_tree, alias_prune_helper, conf));
1435
}
1436
1437
/*
1438
* Convert back to sudoers.
1439
*/
1440
static bool
1441
convert_sudoers_sudoers(struct sudoers_parse_tree *parse_tree,
1442
const char *output_file, struct cvtsudoers_config *conf)
1443
{
1444
bool ret = true;
1445
struct sudo_lbuf lbuf;
1446
debug_decl(convert_sudoers_sudoers, SUDOERS_DEBUG_UTIL);
1447
1448
if (strcmp(output_file, "-") == 0) {
1449
output_fp = stdout;
1450
} else {
1451
if ((output_fp = fopen(output_file, "w")) == NULL)
1452
sudo_fatal(U_("unable to open %s"), output_file);
1453
}
1454
1455
/* Wrap lines at 80 columns with a 4 character indent. */
1456
sudo_lbuf_init(&lbuf, convert_sudoers_output, 4, "\\", 80);
1457
1458
/* Print Defaults */
1459
if (!ISSET(conf->suppress, SUPPRESS_DEFAULTS)) {
1460
if (!print_defaults_sudoers(parse_tree, &lbuf, conf->expand_aliases))
1461
goto done;
1462
if (lbuf.len > 0) {
1463
sudo_lbuf_print(&lbuf);
1464
sudo_lbuf_append(&lbuf, "\n");
1465
}
1466
}
1467
1468
/* Print Aliases */
1469
if (!conf->expand_aliases && !ISSET(conf->suppress, SUPPRESS_ALIASES)) {
1470
if (!print_aliases_sudoers(parse_tree, &lbuf))
1471
goto done;
1472
if (lbuf.len > 1) {
1473
sudo_lbuf_print(&lbuf);
1474
sudo_lbuf_append(&lbuf, "\n");
1475
}
1476
}
1477
1478
/* Print User_Specs, separated by blank lines. */
1479
if (!ISSET(conf->suppress, SUPPRESS_PRIVS)) {
1480
if (!sudoers_format_userspecs(&lbuf, parse_tree, "\n",
1481
conf->expand_aliases, true)) {
1482
goto done;
1483
}
1484
if (lbuf.len > 1) {
1485
sudo_lbuf_print(&lbuf);
1486
}
1487
}
1488
1489
done:
1490
if (sudo_lbuf_error(&lbuf)) {
1491
if (errno == ENOMEM)
1492
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1493
ret = false;
1494
}
1495
sudo_lbuf_destroy(&lbuf);
1496
1497
(void)fflush(output_fp);
1498
if (ferror(output_fp)) {
1499
sudo_warn(U_("unable to write to %s"), output_file);
1500
ret = false;
1501
}
1502
if (output_fp != stdout)
1503
fclose(output_fp);
1504
1505
debug_return_bool(ret);
1506
}
1507
1508
static void
1509
display_usage(FILE *fp)
1510
{
1511
(void) fprintf(fp, "usage: %s [-ehMpV] [-b dn] "
1512
"[-c conf_file ] [-d deftypes] [-f output_format] [-i input_format] "
1513
"[-I increment] [-m filter] [-o output_file] [-O start_point] "
1514
"[-P padding] [-s sections] [input_file]\n", getprogname());
1515
}
1516
1517
sudo_noreturn static void
1518
usage(void)
1519
{
1520
display_usage(stderr);
1521
exit(EXIT_FAILURE);
1522
}
1523
1524
sudo_noreturn static void
1525
help(void)
1526
{
1527
(void) printf(_("%s - convert between sudoers file formats\n\n"), getprogname());
1528
display_usage(stdout);
1529
(void) puts(_("\nOptions:\n"
1530
" -b, --base=dn the base DN for sudo LDAP queries\n"
1531
" -c, --config=conf_file the path to the configuration file\n"
1532
" -d, --defaults=deftypes only convert Defaults of the specified types\n"
1533
" -e, --expand-aliases expand aliases when converting\n"
1534
" -f, --output-format=format set output format: JSON, LDIF or sudoers\n"
1535
" -i, --input-format=format set input format: LDIF or sudoers\n"
1536
" -I, --increment=num amount to increase each sudoOrder by\n"
1537
" -h, --help display help message and exit\n"
1538
" -m, --match=filter only convert entries that match the filter\n"
1539
" -M, --match-local match filter uses passwd and group databases\n"
1540
" -o, --output=output_file write converted sudoers to output_file\n"
1541
" -O, --order-start=num starting point for first sudoOrder\n"
1542
" -p, --prune-matches prune non-matching users, groups and hosts\n"
1543
" -P, --padding=num base padding for sudoOrder increment\n"
1544
" -s, --suppress=sections suppress output of certain sections\n"
1545
" -V, --version display version information and exit"));
1546
exit(EXIT_SUCCESS);
1547
}
1548
1549