2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 22:35:10 +00:00

cvtsudoers: use init_parse_tree() to initialize a parse tree.

Also free the parse tree before exit.
This commit is contained in:
Todd C. Miller
2021-11-19 12:29:21 -07:00
parent 730ebabdba
commit aed51033e1
2 changed files with 16 additions and 16 deletions

View File

@@ -104,7 +104,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct sudoers_parse_tree_list parse_trees = TAILQ_HEAD_INITIALIZER(parse_trees); struct sudoers_parse_tree_list parse_trees = TAILQ_HEAD_INITIALIZER(parse_trees);
struct sudoers_parse_tree *parse_tree, merged_tree; struct sudoers_parse_tree merged_tree, *parse_tree = NULL;
struct cvtsudoers_config *conf = NULL; struct cvtsudoers_config *conf = NULL;
enum sudoers_formats output_format = format_ldif; enum sudoers_formats output_format = format_ldif;
enum sudoers_formats input_format = format_sudoers; enum sudoers_formats input_format = format_sudoers;
@@ -131,6 +131,9 @@ main(int argc, char *argv[])
bindtextdomain("sudoers", LOCALEDIR); bindtextdomain("sudoers", LOCALEDIR);
textdomain("sudoers"); textdomain("sudoers");
/* Initialize early, before any "goto done". */
init_parse_tree(&merged_tree, NULL, NULL);
/* Read debug and plugin sections of sudo.conf. */ /* Read debug and plugin sections of sudo.conf. */
if (sudo_conf_read(NULL, SUDO_CONF_DEBUG|SUDO_CONF_PLUGINS) == -1) if (sudo_conf_read(NULL, SUDO_CONF_DEBUG|SUDO_CONF_PLUGINS) == -1)
goto done; goto done;
@@ -373,13 +376,11 @@ main(int argc, char *argv[])
} }
} }
parse_tree = calloc(1, sizeof(*parse_tree)); parse_tree = malloc(sizeof(*parse_tree));
if (parse_tree == NULL) if (parse_tree == NULL)
sudo_fatalx("%s", U_("unable to allocate memory")); sudo_fatalx("%s", U_("unable to allocate memory"));
parse_tree->lhost = lhost; init_parse_tree(parse_tree, lhost, shost);
parse_tree->shost = shost; TAILQ_INSERT_TAIL(&parse_trees, parse_tree, entries);
TAILQ_INIT(&parse_tree->userspecs);
TAILQ_INIT(&parse_tree->defaults);
/* Setup defaults data structures. */ /* Setup defaults data structures. */
if (!init_defaults()) { if (!init_defaults()) {
@@ -400,7 +401,6 @@ main(int argc, char *argv[])
default: default:
sudo_fatalx("error: unhandled input %d", input_format); sudo_fatalx("error: unhandled input %d", input_format);
} }
TAILQ_INSERT_TAIL(&parse_trees, parse_tree, entries);
/* Apply filters. */ /* Apply filters. */
filter_userspecs(parse_tree, conf); filter_userspecs(parse_tree, conf);
@@ -416,14 +416,11 @@ main(int argc, char *argv[])
} while (argc > 0); } while (argc > 0);
parse_tree = TAILQ_FIRST(&parse_trees); parse_tree = TAILQ_FIRST(&parse_trees);
if (parse_tree == TAILQ_LAST(&parse_trees, sudoers_parse_tree_list)) { if (TAILQ_NEXT(parse_tree, entries)) {
/* No merging required. */ /* Multiple sudoers files, merge them all. */
goto output; parse_tree = merge_sudoers(&parse_trees, &merged_tree);
} }
parse_tree = merge_sudoers(&parse_trees, &merged_tree);
output:
switch (output_format) { switch (output_format) {
case format_csv: case format_csv:
exitcode = !convert_sudoers_csv(parse_tree, output_file, conf); exitcode = !convert_sudoers_csv(parse_tree, output_file, conf);
@@ -442,6 +439,12 @@ output:
} }
done: done:
free_parse_tree(&merged_tree);
while ((parse_tree = TAILQ_FIRST(&parse_trees)) != NULL) {
TAILQ_REMOVE(&parse_trees, parse_tree, entries);
free_parse_tree(parse_tree);
free(parse_tree);
}
cvtsudoers_conf_free(conf); cvtsudoers_conf_free(conf);
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode); sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);
return exitcode; return exitcode;

View File

@@ -838,9 +838,6 @@ merge_sudoers(struct sudoers_parse_tree_list *parse_trees,
{ {
debug_decl(merge_sudoers, SUDOERS_DEBUG_UTIL); debug_decl(merge_sudoers, SUDOERS_DEBUG_UTIL);
memset(merged_tree, 0, sizeof(*merged_tree));
TAILQ_INIT(&merged_tree->userspecs);
TAILQ_INIT(&merged_tree->defaults);
if ((merged_tree->aliases = alloc_aliases()) == NULL) if ((merged_tree->aliases = alloc_aliases()) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));