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

alias_error: display the file and line info for the duplicate alias

Having the file and line of the previous alias definition should
make it easier to fix duplicate alias errors.
This commit is contained in:
Todd C. Miller 2024-09-13 11:56:09 -06:00
parent d001abc8ee
commit 0cbddb6939
2 changed files with 48 additions and 18 deletions

View File

@ -167,7 +167,7 @@ static struct defaults *new_default(char *, char *, short);
static struct member *new_member(char *, short);
static struct sudo_command *new_command(char *, char *);
static struct command_digest *new_digest(unsigned int, char *);
static void alias_error(const char *name, int errnum);
static void alias_error(const char *name, short type, int errnum);
#line 167 "gram.c"
@ -2927,7 +2927,7 @@ yyreduce:
{
if (!alias_add(&parsed_policy, (yyvsp[-3].string), HOSTALIAS,
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
alias_error((yyvsp[-3].string), errno);
alias_error((yyvsp[-3].string), HOSTALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@ -2960,7 +2960,7 @@ yyreduce:
{
if (!alias_add(&parsed_policy, (yyvsp[-3].string), CMNDALIAS,
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
alias_error((yyvsp[-3].string), errno);
alias_error((yyvsp[-3].string), CMNDALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@ -2993,7 +2993,7 @@ yyreduce:
{
if (!alias_add(&parsed_policy, (yyvsp[-3].string), RUNASALIAS,
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
alias_error((yyvsp[-3].string), errno);
alias_error((yyvsp[-3].string), RUNASALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@ -3016,7 +3016,7 @@ yyreduce:
{
if (!alias_add(&parsed_policy, (yyvsp[-3].string), USERALIAS,
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
alias_error((yyvsp[-3].string), errno);
alias_error((yyvsp[-3].string), USERALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@ -3475,13 +3475,28 @@ sudoerserror(const char *s)
}
static void
alias_error(const char *name, int errnum)
alias_error(const char *name, short type, int errnum)
{
if (errnum == EEXIST)
sudoerserrorf(U_("Alias \"%s\" already defined"), name);
else
if (errnum == EEXIST) {
struct alias *a = alias_get(&parsed_policy, name, type);
if (a != NULL) {
sudoerserrorf(
U_("duplicate %s \"%s\", previously defined at %s:%d:%d"),
alias_type_to_string(type), name, a->file, a->line, a->column);
alias_put(a);
} else {
if (errno == ELOOP) {
sudoerserrorf(U_("cycle in %s \"%s\""),
alias_type_to_string(type), name);
} else {
sudoerserrorf(U_("duplicate %s \"%s\""),
alias_type_to_string(type), name);
}
}
} else {
sudoerserror(N_("unable to allocate memory"));
}
}
static struct defaults *
new_default(char *var, char *val, short op)

View File

@ -84,7 +84,7 @@ static struct defaults *new_default(char *, char *, short);
static struct member *new_member(char *, short);
static struct sudo_command *new_command(char *, char *);
static struct command_digest *new_digest(unsigned int, char *);
static void alias_error(const char *name, int errnum);
static void alias_error(const char *name, short type, int errnum);
%}
%union {
@ -1012,7 +1012,7 @@ hostalias : ALIAS {
} '=' hostlist {
if (!alias_add(&parsed_policy, $1, HOSTALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
alias_error($1, HOSTALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, $1);
@ -1039,7 +1039,7 @@ cmndalias : ALIAS {
} '=' cmndlist {
if (!alias_add(&parsed_policy, $1, CMNDALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
alias_error($1, CMNDALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, $1);
@ -1066,7 +1066,7 @@ runasalias : ALIAS {
} '=' userlist {
if (!alias_add(&parsed_policy, $1, RUNASALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
alias_error($1, RUNASALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, $1);
@ -1085,7 +1085,7 @@ useralias : ALIAS {
} '=' userlist {
if (!alias_add(&parsed_policy, $1, USERALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
alias_error($1, USERALIAS, errno);
YYERROR;
}
parser_leak_remove(LEAK_PTR, $1);
@ -1292,13 +1292,28 @@ sudoerserror(const char *s)
}
static void
alias_error(const char *name, int errnum)
alias_error(const char *name, short type, int errnum)
{
if (errnum == EEXIST)
sudoerserrorf(U_("Alias \"%s\" already defined"), name);
else
if (errnum == EEXIST) {
struct alias *a = alias_get(&parsed_policy, name, type);
if (a != NULL) {
sudoerserrorf(
U_("duplicate %s \"%s\", previously defined at %s:%d:%d"),
alias_type_to_string(type), name, a->file, a->line, a->column);
alias_put(a);
} else {
if (errno == ELOOP) {
sudoerserrorf(U_("cycle in %s \"%s\""),
alias_type_to_string(type), name);
} else {
sudoerserrorf(U_("duplicate %s \"%s\""),
alias_type_to_string(type), name);
}
}
} else {
sudoerserror(N_("unable to allocate memory"));
}
}
static struct defaults *
new_default(char *var, char *val, short op)