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

Rename export_sudoers() to convert_sudoers_json() and move the

check for the same input and output file to the front-end.
This commit is contained in:
Todd C. Miller 2018-01-26 12:28:49 -07:00
parent 41bd9ae26f
commit 02d917e602
2 changed files with 33 additions and 30 deletions

View File

@ -49,7 +49,7 @@
# include "compat/getopt.h" # include "compat/getopt.h"
#endif /* HAVE_GETOPT_LONG */ #endif /* HAVE_GETOPT_LONG */
extern bool export_sudoers(const char *, const char *); extern bool convert_sudoers_json(const char *, const char *);
/* /*
* Globals * Globals
@ -148,6 +148,13 @@ main(int argc, char *argv[])
input_file = argv[0]; input_file = argv[0];
} }
if (strcmp(input_file, "-") != 0) {
if (strcmp(input_file, output_file) == 0) {
sudo_fatalx(U_("%s: input and output files must be different"),
input_file);
}
}
/* Mock up a fake sudo_user struct. */ /* Mock up a fake sudo_user struct. */
/* XXX - common with visudo */ /* XXX - common with visudo */
user_cmnd = user_base = ""; user_cmnd = user_base = "";
@ -166,7 +173,8 @@ main(int argc, char *argv[])
if (!init_defaults()) if (!init_defaults())
sudo_fatalx(U_("unable to initialize sudoers default values")); sudo_fatalx(U_("unable to initialize sudoers default values"));
exitcode = export_sudoers(input_file, output_file) ? EXIT_SUCCESS : EXIT_FAILURE; exitcode = convert_sudoers_json(input_file, output_file) ?
EXIT_SUCCESS : EXIT_FAILURE;
done: done:
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode); sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, exitcode);

View File

@ -984,33 +984,28 @@ print_userspecs_json(FILE *fp, int indent, bool need_comma)
* Export the parsed sudoers file in JSON format. * Export the parsed sudoers file in JSON format.
*/ */
bool bool
export_sudoers(const char *sudoers_path, const char *export_path) convert_sudoers_json(const char *input_file, const char *output_file)
{ {
bool ret = false, need_comma = false; bool ret = false, need_comma = false;
const int indent = 4; const int indent = 4;
FILE *export_fp = stdout; FILE *output_fp = stdout;
debug_decl(export_sudoers, SUDOERS_DEBUG_UTIL) debug_decl(convert_sudoers_json, SUDOERS_DEBUG_UTIL)
if (strcmp(sudoers_path, "-") == 0) { if (strcmp(input_file, "-") == 0) {
sudoersin = stdin; sudoersin = stdin;
sudoers_path = "stdin"; input_file = "stdin";
} else if ((sudoersin = fopen(sudoers_path, "r")) == NULL) } else if ((sudoersin = fopen(input_file, "r")) == NULL)
sudo_fatal(U_("unable to open %s"), sudoers_path); sudo_fatal(U_("unable to open %s"), input_file);
if (strcmp(export_path, "-") != 0) { if (strcmp(output_file, "-") != 0) {
/* XXX - move check to front-end */ if ((output_fp = fopen(output_file, "w")) == NULL)
if (strcmp(sudoers_path, export_path) == 0) { sudo_fatal(U_("unable to open %s"), output_file);
sudo_fatalx(U_("%s: input and output files must be different"),
sudoers_path);
}
if ((export_fp = fopen(export_path, "w")) == NULL)
sudo_fatal(U_("unable to open %s"), export_path);
} }
init_parser(sudoers_path, false); init_parser(input_file, false);
if (sudoersparse() && !parse_error) { if (sudoersparse() && !parse_error) {
sudo_warnx(U_("failed to parse %s file, unknown error"), sudoers_path); sudo_warnx(U_("failed to parse %s file, unknown error"), input_file);
parse_error = true; parse_error = true;
rcstr_delref(errorfile); rcstr_delref(errorfile);
if ((errorfile = rcstr_dup(sudoers_path)) == NULL) if ((errorfile = rcstr_dup(input_file)) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
} }
ret = !parse_error; ret = !parse_error;
@ -1025,27 +1020,27 @@ export_sudoers(const char *sudoers_path, const char *export_path)
} }
/* Open JSON output. */ /* Open JSON output. */
putc('{', export_fp); putc('{', output_fp);
/* Dump Defaults in JSON format. */ /* Dump Defaults in JSON format. */
need_comma = print_defaults_json(export_fp, indent, need_comma); need_comma = print_defaults_json(output_fp, indent, need_comma);
/* Dump Aliases in JSON format. */ /* Dump Aliases in JSON format. */
need_comma = print_aliases_json(export_fp, indent, need_comma); need_comma = print_aliases_json(output_fp, indent, need_comma);
/* Dump User_Specs in JSON format. */ /* Dump User_Specs in JSON format. */
print_userspecs_json(export_fp, indent, need_comma); print_userspecs_json(output_fp, indent, need_comma);
/* Close JSON output. */ /* Close JSON output. */
fputs("\n}\n", export_fp); fputs("\n}\n", output_fp);
done: done:
if (export_fp != NULL) { if (output_fp != NULL) {
(void)fflush(export_fp); (void)fflush(output_fp);
if (ferror(export_fp)) if (ferror(output_fp))
ret = false; ret = false;
if (export_fp != stdout) if (output_fp != stdout)
fclose(export_fp); fclose(output_fp);
} }
debug_return_bool(ret); debug_return_bool(ret);
} }