Skip to content

Commit 0cbddb6

Browse files
committed
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.
1 parent d001abc commit 0cbddb6

2 files changed

Lines changed: 48 additions & 18 deletions

File tree

plugins/sudoers/gram.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static struct defaults *new_default(char *, char *, short);
167167
static struct member *new_member(char *, short);
168168
static struct sudo_command *new_command(char *, char *);
169169
static struct command_digest *new_digest(unsigned int, char *);
170-
static void alias_error(const char *name, int errnum);
170+
static void alias_error(const char *name, short type, int errnum);
171171

172172
#line 167 "gram.c"
173173

@@ -2927,7 +2927,7 @@ yyparse (void)
29272927
{
29282928
if (!alias_add(&parsed_policy, (yyvsp[-3].string), HOSTALIAS,
29292929
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
2930-
alias_error((yyvsp[-3].string), errno);
2930+
alias_error((yyvsp[-3].string), HOSTALIAS, errno);
29312931
YYERROR;
29322932
}
29332933
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@@ -2960,7 +2960,7 @@ yyparse (void)
29602960
{
29612961
if (!alias_add(&parsed_policy, (yyvsp[-3].string), CMNDALIAS,
29622962
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
2963-
alias_error((yyvsp[-3].string), errno);
2963+
alias_error((yyvsp[-3].string), CMNDALIAS, errno);
29642964
YYERROR;
29652965
}
29662966
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@@ -2993,7 +2993,7 @@ yyparse (void)
29932993
{
29942994
if (!alias_add(&parsed_policy, (yyvsp[-3].string), RUNASALIAS,
29952995
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
2996-
alias_error((yyvsp[-3].string), errno);
2996+
alias_error((yyvsp[-3].string), RUNASALIAS, errno);
29972997
YYERROR;
29982998
}
29992999
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@@ -3016,7 +3016,7 @@ yyparse (void)
30163016
{
30173017
if (!alias_add(&parsed_policy, (yyvsp[-3].string), USERALIAS,
30183018
sudoers, alias_line, alias_column, (yyvsp[0].member))) {
3019-
alias_error((yyvsp[-3].string), errno);
3019+
alias_error((yyvsp[-3].string), USERALIAS, errno);
30203020
YYERROR;
30213021
}
30223022
parser_leak_remove(LEAK_PTR, (yyvsp[-3].string));
@@ -3475,12 +3475,27 @@ sudoerserror(const char *s)
34753475
}
34763476

34773477
static void
3478-
alias_error(const char *name, int errnum)
3478+
alias_error(const char *name, short type, int errnum)
34793479
{
3480-
if (errnum == EEXIST)
3481-
sudoerserrorf(U_("Alias \"%s\" already defined"), name);
3482-
else
3480+
if (errnum == EEXIST) {
3481+
struct alias *a = alias_get(&parsed_policy, name, type);
3482+
if (a != NULL) {
3483+
sudoerserrorf(
3484+
U_("duplicate %s \"%s\", previously defined at %s:%d:%d"),
3485+
alias_type_to_string(type), name, a->file, a->line, a->column);
3486+
alias_put(a);
3487+
} else {
3488+
if (errno == ELOOP) {
3489+
sudoerserrorf(U_("cycle in %s \"%s\""),
3490+
alias_type_to_string(type), name);
3491+
} else {
3492+
sudoerserrorf(U_("duplicate %s \"%s\""),
3493+
alias_type_to_string(type), name);
3494+
}
3495+
}
3496+
} else {
34833497
sudoerserror(N_("unable to allocate memory"));
3498+
}
34843499
}
34853500

34863501
static struct defaults *

plugins/sudoers/gram.y

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static struct defaults *new_default(char *, char *, short);
8484
static struct member *new_member(char *, short);
8585
static struct sudo_command *new_command(char *, char *);
8686
static struct command_digest *new_digest(unsigned int, char *);
87-
static void alias_error(const char *name, int errnum);
87+
static void alias_error(const char *name, short type, int errnum);
8888
%}
8989

9090
%union {
@@ -1012,7 +1012,7 @@ hostalias : ALIAS {
10121012
} '=' hostlist {
10131013
if (!alias_add(&parsed_policy, $1, HOSTALIAS,
10141014
sudoers, alias_line, alias_column, $4)) {
1015-
alias_error($1, errno);
1015+
alias_error($1, HOSTALIAS, errno);
10161016
YYERROR;
10171017
}
10181018
parser_leak_remove(LEAK_PTR, $1);
@@ -1039,7 +1039,7 @@ cmndalias : ALIAS {
10391039
} '=' cmndlist {
10401040
if (!alias_add(&parsed_policy, $1, CMNDALIAS,
10411041
sudoers, alias_line, alias_column, $4)) {
1042-
alias_error($1, errno);
1042+
alias_error($1, CMNDALIAS, errno);
10431043
YYERROR;
10441044
}
10451045
parser_leak_remove(LEAK_PTR, $1);
@@ -1066,7 +1066,7 @@ runasalias : ALIAS {
10661066
} '=' userlist {
10671067
if (!alias_add(&parsed_policy, $1, RUNASALIAS,
10681068
sudoers, alias_line, alias_column, $4)) {
1069-
alias_error($1, errno);
1069+
alias_error($1, RUNASALIAS, errno);
10701070
YYERROR;
10711071
}
10721072
parser_leak_remove(LEAK_PTR, $1);
@@ -1085,7 +1085,7 @@ useralias : ALIAS {
10851085
} '=' userlist {
10861086
if (!alias_add(&parsed_policy, $1, USERALIAS,
10871087
sudoers, alias_line, alias_column, $4)) {
1088-
alias_error($1, errno);
1088+
alias_error($1, USERALIAS, errno);
10891089
YYERROR;
10901090
}
10911091
parser_leak_remove(LEAK_PTR, $1);
@@ -1292,12 +1292,27 @@ sudoerserror(const char *s)
12921292
}
12931293

12941294
static void
1295-
alias_error(const char *name, int errnum)
1295+
alias_error(const char *name, short type, int errnum)
12961296
{
1297-
if (errnum == EEXIST)
1298-
sudoerserrorf(U_("Alias \"%s\" already defined"), name);
1299-
else
1297+
if (errnum == EEXIST) {
1298+
struct alias *a = alias_get(&parsed_policy, name, type);
1299+
if (a != NULL) {
1300+
sudoerserrorf(
1301+
U_("duplicate %s \"%s\", previously defined at %s:%d:%d"),
1302+
alias_type_to_string(type), name, a->file, a->line, a->column);
1303+
alias_put(a);
1304+
} else {
1305+
if (errno == ELOOP) {
1306+
sudoerserrorf(U_("cycle in %s \"%s\""),
1307+
alias_type_to_string(type), name);
1308+
} else {
1309+
sudoerserrorf(U_("duplicate %s \"%s\""),
1310+
alias_type_to_string(type), name);
1311+
}
1312+
}
1313+
} else {
13001314
sudoerserror(N_("unable to allocate memory"));
1315+
}
13011316
}
13021317

13031318
static struct defaults *

0 commit comments

Comments
 (0)