Preserve the column and error message when there is a syntax error.

This information is now included in the error mail sent to root.
This commit is contained in:
Todd C. Miller
2022-03-06 18:54:30 -07:00
parent dfda098ae7
commit 7d3f9293c6
8 changed files with 331 additions and 297 deletions

View File

@@ -1,7 +1,8 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2004-2005, 2007-2018 Todd C. Miller <Todd.Miller@sudo.ws>
* Copyright (c) 2004-2005, 2007-2018, 2021-2022
* 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
@@ -102,6 +103,8 @@ check_alias(struct sudoers_parse_tree *parse_tree,
if (strict && errorfile == NULL) {
errorfile = sudo_rcstr_addref(file);
errorlineno = line;
errorcolumn = column;
/* No need to set errorstring, visudo doesn't use it. */
}
alias_warned_add(warned, name);
}

View File

@@ -757,15 +757,7 @@ parse_sudoers(const char *input_file, struct cvtsudoers_config *conf)
if ((errorfile = sudo_rcstr_dup(input_file)) == NULL)
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
}
if (parse_error) {
if (errorlineno != -1)
sudo_warnx(U_("parse error in %s near line %d\n"),
errorfile, errorlineno);
else if (errorfile != NULL)
sudo_warnx(U_("parse error in %s\n"), errorfile);
debug_return_bool(false);
}
debug_return_bool(true);
debug_return_bool(!parse_error);
}
FILE *

View File

@@ -104,11 +104,12 @@ sudo_file_parse(struct sudo_nss *nss)
error = sudoersparse();
if (error || parse_error) {
if (errorlineno != -1) {
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR,
N_("parse error in %s near line %d"), errorfile, errorlineno);
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR, N_("%s:%d:%d: %s"),
errorfile, errorlineno, errorcolumn,
errorstring ? errorstring : N_("syntax error"));
} else {
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR,
N_("parse error in %s"), errorfile);
log_warningx(SLOG_SEND_MAIL|SLOG_NO_STDERR, N_("%s: %s"),
errorfile, errorstring ? errorstring : N_("syntax error"));
}
if (error || !sudoers_recovery) {
/* unrecoverable error */

File diff suppressed because it is too large Load Diff

View File

@@ -171,7 +171,7 @@ extern int sudoersdebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 86 "gram.y"
#line 88 "gram.y"
struct cmndspec *cmndspec;
struct defaults *defaults;

View File

@@ -52,7 +52,9 @@ bool sudoers_warnings = true;
bool sudoers_strict = false;
bool parse_error = false;
int errorlineno = -1;
int errorcolumn = -1;
char *errorfile = NULL;
char *errorstring = NULL;
static int alias_line, alias_column;
@@ -1168,13 +1170,26 @@ group : ALIAS {
void
sudoerserrorf(const char *fmt, ...)
{
va_list ap;
debug_decl(sudoerserrorf, SUDOERS_DEBUG_PARSER);
/* Save the line the first error occurred on. */
if (errorlineno == -1) {
errorlineno = this_lineno;
sudo_rcstr_delref(errorfile);
errorfile = sudo_rcstr_addref(sudoers);
errorlineno = this_lineno;
errorcolumn = sudolinebuf.toke_start + 1;
if (fmt != NULL) {
va_start(ap, fmt);
if (strcmp(fmt, "%s") == 0) {
/* Optimize common case, a single string. */
errorstring = strdup(_(va_arg(ap, char *)));
} else {
if (vasprintf(&errorstring, fmt, ap) == -1)
errorstring = NULL;
}
va_end(ap);
}
}
if (sudoers_warnings && fmt != NULL) {
LEXTRACE("<*> ");
@@ -1182,7 +1197,6 @@ sudoerserrorf(const char *fmt, ...)
if (trace_print == NULL || trace_print == sudoers_trace_print) {
char *s, *tofree = NULL;
int oldlocale;
va_list ap;
/* Warnings are displayed in the user's locale. */
sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
@@ -1754,6 +1768,9 @@ init_parser(const char *path, bool quiet, bool strict)
parse_error = false;
errorlineno = -1;
errorcolumn = -1;
free(errorstring);
errorstring = NULL;
sudo_rcstr_delref(errorfile);
errorfile = NULL;
sudoers_warnings = !quiet;

View File

@@ -312,6 +312,8 @@ int sudoersparse(void);
extern char *login_style;
extern char *errorfile;
extern int errorlineno;
extern int errorcolumn;
extern char *errorstring;
extern bool parse_error;
extern bool sudoers_warnings;
extern bool sudoers_recovery;

View File

@@ -564,7 +564,7 @@ done:
/*
* Check Defaults and Alias entries.
* Sets parse_error on error and errorfile/errorlineno if possible.
* Sets parse_error on error and error{file,lineno,column} if possible.
*/
static void
check_defaults_and_aliases(bool strict, bool quiet)
@@ -576,12 +576,14 @@ check_defaults_and_aliases(bool strict, bool quiet)
sudo_rcstr_delref(errorfile);
errorfile = NULL;
errorlineno = -1;
errorcolumn = -1;
/* XXX - should edit all files with errors */
TAILQ_FOREACH(d, &parsed_policy.defaults, entries) {
if (d->error) {
/* Defaults parse error, set errorfile/errorlineno. */
if (d->error && errorlineno == -1) {
/* Defaults parse error, set error{file,lineno,column}. */
errorfile = sudo_rcstr_addref(d->file);
errorlineno = d->line;
errorcolumn = d->column;
break;
}
}