Change alias_add() to return bool and set errno on failure.

This fixes a localization problem where the error message could
have been reported in the wrong locale.
This commit is contained in:
Todd C. Miller
2020-12-02 13:12:09 -07:00
parent e22817e3ab
commit d98374753c
5 changed files with 332 additions and 339 deletions

View File

@@ -110,30 +110,23 @@ alias_put(struct alias *a)
/*
* Add an alias to the aliases redblack tree.
* Note that "file" must be a reference-counted string.
* Returns NULL on success and an error string on failure.
* Returns true on success and false on failure, setting errno.
*/
const char *
bool
alias_add(struct sudoers_parse_tree *parse_tree, char *name, int type,
char *file, int line, int column, struct member *members)
{
static char errbuf[512];
struct alias *a;
debug_decl(alias_add, SUDOERS_DEBUG_ALIAS);
if (parse_tree->aliases == NULL) {
if ((parse_tree->aliases = alloc_aliases()) == NULL) {
/* XXX - return error code instead */
strlcpy(errbuf, N_("unable to allocate memory"), sizeof(errbuf));
debug_return_str(errbuf);
}
if ((parse_tree->aliases = alloc_aliases()) == NULL)
debug_return_bool(false);
}
a = calloc(1, sizeof(*a));
if (a == NULL) {
/* XXX - return error code instead */
strlcpy(errbuf, N_("unable to allocate memory"), sizeof(errbuf));
debug_return_str(errbuf);
}
if (a == NULL)
debug_return_bool(false);
a->name = name;
a->type = type;
/* a->used = false; */
@@ -143,18 +136,14 @@ alias_add(struct sudoers_parse_tree *parse_tree, char *name, int type,
HLTQ_TO_TAILQ(&a->members, members, entries);
switch (rbinsert(parse_tree->aliases, a, NULL)) {
case 1:
/* XXX - return error code instead, this is not translatable. */
(void)snprintf(errbuf, sizeof(errbuf),
N_("Alias \"%s\" already defined"), name);
alias_free(a);
debug_return_str(errbuf);
errno = EEXIST;
debug_return_bool(false);
case -1:
/* XXX - return error code instead */
(void)strlcpy(errbuf, N_("unable to allocate memory"), sizeof(errbuf));
alias_free(a);
debug_return_str(errbuf);
debug_return_bool(false);
}
debug_return_str(NULL);
debug_return_bool(true);
}
/*

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
/* A Bison parser, made by GNU Bison 3.7.3. */
/* A Bison parser, made by GNU Bison 3.7.4. */
/* Bison interface for Yacc-like parsers in C
@@ -167,7 +167,7 @@ extern int sudoersdebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 79 "gram.y"
#line 80 "gram.y"
struct cmndspec *cmndspec;
struct defaults *defaults;

View File

@@ -74,6 +74,7 @@ static struct defaults *new_default(char *, char *, short);
static struct member *new_member(char *, int);
static struct sudo_command *new_command(char *, char *);
static struct command_digest *new_digest(int, char *);
static void alias_error(const char *name, int errnum);
%}
%union {
@@ -866,11 +867,9 @@ hostalias : ALIAS {
alias_line = this_lineno;
alias_column = sudolinebuf.toke_start + 1;
} '=' hostlist {
const char *s;
s = alias_add(&parsed_policy, $1, HOSTALIAS,
sudoers, alias_line, alias_column, $4);
if (s != NULL) {
sudoerserror(s);
if (!alias_add(&parsed_policy, $1, HOSTALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
YYERROR;
}
}
@@ -892,11 +891,9 @@ cmndalias : ALIAS {
alias_line = this_lineno;
alias_column = sudolinebuf.toke_start + 1;
} '=' cmndlist {
const char *s;
s = alias_add(&parsed_policy, $1, CMNDALIAS,
sudoers, alias_line, alias_column, $4);
if (s != NULL) {
sudoerserror(s);
if (!alias_add(&parsed_policy, $1, CMNDALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
YYERROR;
}
}
@@ -918,11 +915,9 @@ runasalias : ALIAS {
alias_line = this_lineno;
alias_column = sudolinebuf.toke_start + 1;
} '=' userlist {
const char *s;
s = alias_add(&parsed_policy, $1, RUNASALIAS,
sudoers, alias_line, alias_column, $4);
if (s != NULL) {
sudoerserror(s);
if (!alias_add(&parsed_policy, $1, RUNASALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
YYERROR;
}
}
@@ -937,11 +932,9 @@ useralias : ALIAS {
alias_line = this_lineno;
alias_column = sudolinebuf.toke_start + 1;
} '=' userlist {
const char *s;
s = alias_add(&parsed_policy, $1, USERALIAS,
sudoers, alias_line, alias_column, $4);
if (s != NULL) {
sudoerserror(s);
if (!alias_add(&parsed_policy, $1, USERALIAS,
sudoers, alias_line, alias_column, $4)) {
alias_error($1, errno);
YYERROR;
}
}
@@ -1115,6 +1108,15 @@ sudoerserror(const char *s)
sudoerserrorf("%s", s);
}
static void
alias_error(const char *name, int errnum)
{
if (errnum == EEXIST)
sudoerserrorf(U_("Alias \"%s\" already defined"), name);
else
sudoerserror(N_("unable to allocate memory"));
}
static struct defaults *
new_default(char *var, char *val, short op)
{

View File

@@ -298,7 +298,7 @@ struct cmnd_info {
struct rbtree *alloc_aliases(void);
void free_aliases(struct rbtree *aliases);
bool no_aliases(struct sudoers_parse_tree *parse_tree);
const char *alias_add(struct sudoers_parse_tree *parse_tree, char *name, int type, char *file, int line, int column, struct member *members);
bool alias_add(struct sudoers_parse_tree *parse_tree, char *name, int type, char *file, int line, int column, struct member *members);
const char *alias_type_to_string(int alias_type);
struct alias *alias_get(struct sudoers_parse_tree *parse_tree, const char *name, int type);
struct alias *alias_remove(struct sudoers_parse_tree *parse_tree, char *name, int type);