Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions plugins/sudoers/cvtsudoers.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ static bool alias_prune(struct sudoers_parse_tree *parse_tree, struct cvtsudoers
sudo_noreturn static void help(void);
sudo_noreturn static void usage(void);

/* Does the next entry differ only in the command itself? */
bool
cmndspec_continues(const struct cmndspec *cs, const struct cmndspec *next)
{
bool ret = next != NULL &&
!RUNAS_CHANGED(cs, next) && !TAGS_CHANGED(cs->tags, next->tags)
&& cs->timeout == next->timeout && cs->notbefore == next->notbefore
&& cs->notafter == next->notafter
&& cs->privs == next->privs && cs->limitprivs == next->limitprivs
&& cs->role == next->role && cs->type == next->type
&& cs->apparmor_profile == next->apparmor_profile
&& cs->runchroot == next->runchroot && cs->runcwd == next->runcwd;
return ret;
}

int
main(int argc, char *argv[])
{
Expand Down
1 change: 1 addition & 0 deletions plugins/sudoers/cvtsudoers.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct cvtsudoers_filter {
};

/* cvtsudoers.c */
bool cmndspec_continues(const struct cmndspec *cs, const struct cmndspec *next);
extern struct cvtsudoers_filter *filters;
void log_warnx(const char * restrict fmt, ...) sudo_printflike(1, 2);

Expand Down
184 changes: 73 additions & 111 deletions plugins/sudoers/cvtsudoers_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,7 @@
#include <cvtsudoers.h>
#include <gram.h>

static bool print_member_list_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, struct member_list *members, bool negated, short alias_type, bool expand_aliases);

/*
* Print sudoOptions from a defaults_list.
*/
static bool
print_options_csv(FILE *fp, struct defaults_list *options, bool need_comma)
{
struct defaults *opt;
debug_decl(print_options_csv, SUDOERS_DEBUG_UTIL);

TAILQ_FOREACH(opt, options, entries) {
if (opt->val != NULL) {
/* There is no need to double quote values here. */
fprintf(fp, "%s%s%s%s", need_comma ? "," : "", opt->var,
opt->op == '+' ? "+=" : opt->op == '-' ? "-=" : "=", opt->val);
} else {
/* Boolean flag. */
fprintf(fp, "%s%s%s", need_comma ? "," : "",
opt->op == false ? "!" : "", opt->var);
}
need_comma = true;
}

debug_return_bool(!ferror(fp));
}
static bool print_member_list_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, struct member_list *members, bool negated, bool quoted, short alias_type, bool expand_aliases);

/*
* Map a Defaults type to string.
Expand Down Expand Up @@ -104,49 +79,62 @@ defaults_to_alias_type(int defaults_type)
* Print a string, performing quoting as needed.
* If a field includes a comma it must be double-quoted.
* Double quotes are replaced by a pair of double-quotes.
* XXX - rewrite this
*/
static bool
print_csv_string(FILE *fp, const char *str, bool quoted)
{
const char *src = str;
char *dst, *newstr;
size_t len, newsize;
bool quote_it = false;
bool ret = true;
bool quote_it = !quoted && strpbrk(str, "\",\r\n") != NULL;
debug_decl(print_csv_string, SUDOERS_DEBUG_UTIL);

len = strcspn(str, quoted ? "\"" : "\",");
if (str[len] == '\0') {
/* nothing to escape */
debug_return_bool(fputs(str, fp) != EOF);
if (quote_it)
putc('"', fp);
for (; *str != '\0'; str++) {
if (*str == '"')
putc('"', fp);
putc(*str, fp);
}
if (quote_it)
putc('"', fp);
debug_return_bool(!ferror(fp));
}

if (!quoted && strchr(str + len, ',') != NULL)
quote_it = true;
/* Append a name=value pair to the quoted options field. */
static void
print_cmnd_option(FILE *fp, const char *name, const char *val, bool *need_comma)
{
if (*need_comma)
putc(',', fp);
fputs(name, fp);
putc('=', fp);
print_csv_string(fp, val, true);
*need_comma = true;
}

/* String includes characters we need to escape. */
newsize = len + 2 + (strlen(len + str) * 2) + 1;
if ((newstr = malloc(newsize)) == NULL)
debug_return_bool(false);
dst = newstr;
/*
* Print sudoOptions from a defaults_list.
*/
static bool
print_options_csv(FILE *fp, struct defaults_list *options, bool need_comma)
{
struct defaults *opt;
debug_decl(print_options_csv, SUDOERS_DEBUG_UTIL);

if (quote_it)
*dst++ = '"';
while (*src != '\0') {
if (*src == '"')
*dst++ = '"';
*dst++ = *src++;
TAILQ_FOREACH(opt, options, entries) {
if (need_comma)
putc(',', fp);
if (opt->val == NULL && !opt->op)
putc('!', fp);
if (!print_csv_string(fp, opt->var, true))
debug_return_bool(false);
if (opt->val != NULL) {
fputs(opt->op == '+' ? "+=" : opt->op == '-' ? "-=" : "=", fp);
if (!print_csv_string(fp, opt->val, true))
debug_return_bool(false);
}
need_comma = true;
}
if (quote_it)
*dst++ = '"';
*dst = '\0';

if (fputs(newstr, fp) == EOF)
ret = false;
free(newstr);

debug_return_bool(ret);
debug_return_bool(!ferror(fp));
}

/*
Expand Down Expand Up @@ -234,7 +222,7 @@ print_member_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
case ALIAS:
if (expand_aliases) {
if ((a = alias_get(parse_tree, name, alias_type)) != NULL) {
if (!print_member_list_csv(fp, parse_tree, &a->members, negated,
if (!print_member_list_csv(fp, parse_tree, &a->members, negated, quoted,
alias_type, expand_aliases)) {
alias_put(a);
debug_return_bool(false);
Expand Down Expand Up @@ -267,27 +255,29 @@ print_member_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
*/
static bool
print_member_list_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
struct member_list *members, bool negated, short alias_type,
struct member_list *members, bool negated, bool quoted, short alias_type,
bool expand_aliases)
{
struct member *m, *next;
bool quote_list;
debug_decl(print_member_list_csv, SUDOERS_DEBUG_UTIL);

if (TAILQ_EMPTY(members))
debug_return_bool(true);

if (TAILQ_FIRST(members) != TAILQ_LAST(members, member_list))
quote_list = !quoted && TAILQ_FIRST(members) != TAILQ_LAST(members, member_list);
if (quote_list)
putc('"', fp);
TAILQ_FOREACH_SAFE(m, members, entries, next) {
if (!print_member_csv(fp, parse_tree, m->name, m->type,
negated ? !m->negated : m->negated, true, alias_type,
negated ? !m->negated : m->negated, quoted || quote_list, alias_type,
expand_aliases)) {
debug_return_bool(false);
}
if (next != NULL)
putc(',', fp);
}
if (TAILQ_FIRST(members) != TAILQ_LAST(members, member_list))
if (quote_list)
putc('"', fp);

debug_return_bool(!ferror(fp));
Expand All @@ -307,7 +297,7 @@ print_defaults_binding_csv(FILE *fp,
if (type != DEFAULTS) {
/* Print each member object in binding. */
alias_type = defaults_to_alias_type(type);
if (!print_member_list_csv(fp, parse_tree, &binding->members, false,
if (!print_member_list_csv(fp, parse_tree, &binding->members, false, false,
alias_type, expand_aliases)) {
debug_return_bool(false);
}
Expand Down Expand Up @@ -404,8 +394,8 @@ print_alias_csv(struct sudoers_parse_tree *parse_tree, struct alias *a, void *v)
}

fprintf(fp, "%s,%s,", title, a->name);
if (!print_member_list_csv(fp, parse_tree, &a->members, false, a->type,
false)) {
if (!print_member_list_csv(fp, parse_tree, &a->members, false, false,
a->type, false)) {
debug_return_int(-1);
}
putc('\n', fp);
Expand Down Expand Up @@ -454,15 +444,15 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
debug_decl(print_cmndspec_csv, SUDOERS_DEBUG_UTIL);

if (cs->runasuserlist != NULL) {
if (!print_member_list_csv(fp, parse_tree, cs->runasuserlist, false,
if (!print_member_list_csv(fp, parse_tree, cs->runasuserlist, false, false,
RUNASALIAS, expand_aliases)) {
debug_return_bool(false);
}
}
putc(',', fp);

if (cs->runasgrouplist != NULL) {
if (!print_member_list_csv(fp, parse_tree, cs->runasgrouplist, false,
if (!print_member_list_csv(fp, parse_tree, cs->runasgrouplist, false, false,
RUNASALIAS, expand_aliases)) {
debug_return_bool(false);
}
Expand Down Expand Up @@ -559,40 +549,20 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
if (!TAILQ_EMPTY(options))
need_comma = true;

/* Print runchroot and runcwd. */
if (cs->runchroot != NULL) {
fprintf(fp, "%srunchroot=%s", need_comma ? "," : "", cs->runchroot);
need_comma = true;
}
if (cs->runcwd != NULL) {
fprintf(fp, "%sruncwd=%s", need_comma ? "," : "", cs->runcwd);
need_comma = true;
}

/* Print SELinux role/type */
if (cs->runchroot != NULL)
print_cmnd_option(fp, "runchroot", cs->runchroot, &need_comma);
if (cs->runcwd != NULL)
print_cmnd_option(fp, "runcwd", cs->runcwd, &need_comma);
if (cs->role != NULL && cs->type != NULL) {
fprintf(fp, "%srole=%s,type=%s", need_comma ? "," : "",
cs->role, cs->type);
need_comma = true;
}

if (cs->apparmor_profile != NULL) {
fprintf(fp, "%sapparmor_profile=%s,", need_comma ? "," : "",
cs->apparmor_profile);
need_comma = true;
}

/* Print Solaris privs/limitprivs */
if (cs->privs != NULL || cs->limitprivs != NULL) {
if (cs->privs != NULL) {
fprintf(fp, "%sprivs=%s", need_comma ? "," : "", cs->privs);
need_comma = true;
}
if (cs->limitprivs != NULL) {
fprintf(fp, "%slimitprivs=%s", need_comma ? "," : "", cs->limitprivs);
need_comma = true;
}
}
print_cmnd_option(fp, "role", cs->role, &need_comma);
print_cmnd_option(fp, "type", cs->type, &need_comma);
}
if (cs->apparmor_profile != NULL)
print_cmnd_option(fp, "apparmor_profile", cs->apparmor_profile, &need_comma);
if (cs->privs != NULL)
print_cmnd_option(fp, "privs", cs->privs, &need_comma);
if (cs->limitprivs != NULL)
print_cmnd_option(fp, "limitprivs", cs->limitprivs, &need_comma);
#ifdef __clang_analyzer__
(void)&need_comma;
#endif
Expand All @@ -604,15 +574,7 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
* role/type, AppArmor profiles and Solaris priv settings.
*/
for (;;) {
/* Does the next entry differ only in the command itself? */
/* XXX - move into a function that returns bool */
/* XXX - TAG_SET does not account for implied SETENV */
last_one = next == NULL ||
RUNAS_CHANGED(cs, next) || TAGS_CHANGED(cs->tags, next->tags)
|| cs->privs != next->privs || cs->limitprivs != next->limitprivs
|| cs->role != next->role || cs->type != next->type
|| cs->apparmor_profile != next->apparmor_profile
|| cs->runchroot != next->runchroot || cs->runcwd != next->runcwd;
last_one = !cmndspec_continues(cs, next);

if (!quoted && !last_one) {
quoted = true;
Expand Down Expand Up @@ -654,13 +616,13 @@ print_userspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree,
TAILQ_FOREACH(priv, &us->privileges, entries) {
TAILQ_FOREACH_SAFE(cs, &priv->cmndlist, entries, next) {
fputs("rule,", fp);
if (!print_member_list_csv(fp, parse_tree, &us->users, false,
if (!print_member_list_csv(fp, parse_tree, &us->users, false, false,
USERALIAS, expand_aliases)) {
debug_return_bool(false);
}
putc(',', fp);

if (!print_member_list_csv(fp, parse_tree, &priv->hostlist, false,
if (!print_member_list_csv(fp, parse_tree, &priv->hostlist, false, false,
HOSTALIAS, expand_aliases)) {
debug_return_bool(false);
}
Expand Down
13 changes: 0 additions & 13 deletions plugins/sudoers/cvtsudoers_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,19 +660,6 @@ print_aliases_json(struct json_container *jsonc,
debug_return_bool(true);
}

/* Does the next entry differ only in the command itself? */
static bool
cmndspec_continues(struct cmndspec *cs, struct cmndspec *next)
{
bool ret = next != NULL &&
!RUNAS_CHANGED(cs, next) && !TAGS_CHANGED(cs->tags, next->tags)
&& cs->privs == next->privs && cs->limitprivs == next->limitprivs
&& cs->role == next->role && cs->type == next->type
&& cs->apparmor_profile == next->apparmor_profile
&& cs->runchroot == next->runchroot && cs->runcwd == next->runcwd;
return ret;
}

/*
* Print a Cmnd_Spec in JSON format at the correct indent level.
* A pointer to the next Cmnd_Spec is passed in to make it possible to
Expand Down
9 changes: 1 addition & 8 deletions plugins/sudoers/cvtsudoers_ldif.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,7 @@ print_cmndspec_ldif(FILE *fp, const struct sudoers_parse_tree *parse_tree,
* role/type and Solaris priv settings.
*/
for (;;) {
/* Does the next entry differ only in the command itself? */
/* XXX - move into a function that returns bool */
/* XXX - TAG_SET does not account for implied SETENV */
last_one = next == NULL ||
RUNAS_CHANGED(cs, next) || TAGS_CHANGED(cs->tags, next->tags)
|| cs->privs != next->privs || cs->limitprivs != next->limitprivs
|| cs->role != next->role || cs->type != next->type
|| cs->runchroot != next->runchroot || cs->runcwd != next->runcwd;
last_one = !cmndspec_continues(cs, next);

if (!print_member_ldif(fp, parse_tree, cs->cmnd->name, cs->cmnd->type,
cs->cmnd->negated, CMNDALIAS, "sudoCommand")) {
Expand Down
Loading
Loading