--- embedaddon/sudo/plugins/sudoers/parse.c 2012/05/29 12:26:49 1.1.1.2 +++ embedaddon/sudo/plugins/sudoers/parse.c 2013/07/22 10:46:12 1.1.1.4 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2005, 2007-2012 Todd C. Miller + * Copyright (c) 2004-2005, 2007-2013 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,7 +19,6 @@ #include #include -#include #include #ifdef STDC_HEADERS # include @@ -48,7 +47,7 @@ #include /* Characters that must be quoted in sudoers */ -#define SUDOERS_QUOTED ":\\,=#\"" +#define SUDOERS_QUOTED ":\\,=#\"" /* sudoers nsswitch routines */ struct sudo_nss sudo_nss_file = { @@ -68,7 +67,7 @@ struct sudo_nss sudo_nss_file = { /* * Parser externs. */ -extern FILE *yyin; +extern FILE *sudoersin; extern char *errorfile; extern int errorlineno; extern bool parse_error; @@ -76,8 +75,10 @@ extern bool parse_error; /* * Local prototypes. */ -static void print_member(struct lbuf *, char *, int, int, int); -static int display_bound_defaults(int, struct lbuf *); +static int display_bound_defaults(int dtype, struct lbuf *lbuf); +static void print_member(struct lbuf *lbuf, struct member *m, int alias_type); +static void print_member2(struct lbuf *lbuf, struct member *m, + const char *separator, int alias_type); int sudo_file_open(struct sudo_nss *nss) @@ -96,11 +97,11 @@ sudo_file_close(struct sudo_nss *nss) debug_decl(sudo_file_close, SUDO_DEBUG_NSS) /* Free parser data structures and close sudoers file. */ - init_parser(NULL, 0); + init_parser(NULL, false); if (nss->handle != NULL) { fclose(nss->handle); nss->handle = NULL; - yyin = NULL; + sudoersin = NULL; } debug_return_int(0); } @@ -116,14 +117,14 @@ sudo_file_parse(struct sudo_nss *nss) if (nss->handle == NULL) debug_return_int(-1); - init_parser(sudoers_file, 0); - yyin = nss->handle; - if (yyparse() != 0 || parse_error) { + init_parser(sudoers_file, false); + sudoersin = nss->handle; + if (sudoersparse() != 0 || parse_error) { if (errorlineno != -1) { - log_error(0, _("parse error in %s near line %d"), + log_warning(0, N_("parse error in %s near line %d"), errorfile, errorlineno); } else { - log_error(0, _("parse error in %s"), errorfile); + log_warning(0, N_("parse error in %s"), errorfile); } debug_return_int(-1); } @@ -158,6 +159,7 @@ sudo_file_lookup(struct sudo_nss *nss, int validated, struct cmndtag *tags = NULL; struct privilege *priv; struct userspec *us; + struct member *matching_user; debug_decl(sudo_file_lookup, SUDO_DEBUG_NSS) if (nss->handle == NULL) @@ -225,8 +227,9 @@ sudo_file_lookup(struct sudo_nss *nss, int validated, else continue; tq_foreach_rev(&priv->cmndlist, cs) { + matching_user = NULL; runas_match = runaslist_matches(&cs->runasuserlist, - &cs->runasgrouplist); + &cs->runasgrouplist, &matching_user, NULL); if (runas_match == ALLOW) { cmnd_match = cmnd_matches(cs->cmnd); if (cmnd_match != UNSPEC) { @@ -239,6 +242,23 @@ sudo_file_lookup(struct sudo_nss *nss, int validated, if (user_type == NULL) user_type = cs->type ? estrdup(cs->type) : def_type; #endif /* HAVE_SELINUX */ +#ifdef HAVE_PRIV_SET + /* Set Solaris privilege sets */ + if (runas_privs == NULL) + runas_privs = cs->privs ? estrdup(cs->privs) : def_privs; + if (runas_limitprivs == NULL) + runas_limitprivs = cs->limitprivs ? estrdup(cs->limitprivs) : def_limitprivs; +#endif /* HAVE_PRIV_SET */ + /* + * If user is running command as himself, + * set runas_pw = sudo_user.pw. + * XXX - hack, want more general solution + */ + if (matching_user && matching_user->type == MYSELF) { + sudo_pw_delref(runas_pw); + sudo_pw_addref(sudo_user.pw); + runas_pw = sudo_user.pw; + } goto matched2; } } @@ -271,16 +291,24 @@ sudo_file_lookup(struct sudo_nss *nss, int validated, debug_return_int(validated); } +#define TAG_SET(tt) \ + ((tt) != UNSPEC && (tt) != IMPLIED) + #define TAG_CHANGED(t) \ - (cs->tags.t != UNSPEC && cs->tags.t != IMPLIED && cs->tags.t != tags->t) + (TAG_SET(cs->tags.t) && cs->tags.t != tags->t) static void sudo_file_append_cmnd(struct cmndspec *cs, struct cmndtag *tags, struct lbuf *lbuf) { - struct member *m; debug_decl(sudo_file_append_cmnd, SUDO_DEBUG_NSS) +#ifdef HAVE_PRIV_SET + if (cs->privs) + lbuf_append(lbuf, "PRIVS=\"%s\" ", cs->privs); + if (cs->limitprivs) + lbuf_append(lbuf, "LIMITPRIVS=\"%s\" ", cs->limitprivs); +#endif /* HAVE_PRIV_SET */ #ifdef HAVE_SELINUX if (cs->role) lbuf_append(lbuf, "ROLE=%s ", cs->role); @@ -307,59 +335,73 @@ sudo_file_append_cmnd(struct cmndspec *cs, struct cmnd lbuf_append(lbuf, cs->tags.log_output ? "LOG_OUTPUT: " : "NOLOG_OUTPUT: "); tags->log_output = cs->tags.log_output; } - m = cs->cmnd; - print_member(lbuf, m->name, m->type, m->negated, - CMNDALIAS); + print_member(lbuf, cs->cmnd, CMNDALIAS); debug_return; } +#define RUNAS_CHANGED(cs1, cs2) \ + (cs1 == NULL || cs2 == NULL || \ + cs1->runasuserlist.first != cs2->runasuserlist.first || \ + cs1->runasuserlist.last != cs2->runasuserlist.last || \ + cs1->runasgrouplist.first != cs2->runasgrouplist.first || \ + cs1->runasgrouplist.last != cs2->runasgrouplist.last) + static int sudo_file_display_priv_short(struct passwd *pw, struct userspec *us, struct lbuf *lbuf) { - struct cmndspec *cs; + struct cmndspec *cs, *prev_cs; struct member *m; struct privilege *priv; struct cmndtag tags; int nfound = 0; debug_decl(sudo_file_display_priv_short, SUDO_DEBUG_NSS) + /* gcc -Wuninitialized false positive */ + tags.noexec = UNSPEC; + tags.setenv = UNSPEC; + tags.nopasswd = UNSPEC; + tags.log_input = UNSPEC; + tags.log_output = UNSPEC; tq_foreach_fwd(&us->privileges, priv) { if (hostlist_matches(&priv->hostlist) != ALLOW) continue; - tags.noexec = UNSPEC; - tags.setenv = UNSPEC; - tags.nopasswd = UNSPEC; - tags.log_input = UNSPEC; - tags.log_output = UNSPEC; - lbuf_append(lbuf, " "); + prev_cs = NULL; tq_foreach_fwd(&priv->cmndlist, cs) { - if (cs != tq_first(&priv->cmndlist)) - lbuf_append(lbuf, ", "); - lbuf_append(lbuf, "("); - if (!tq_empty(&cs->runasuserlist)) { - tq_foreach_fwd(&cs->runasuserlist, m) { - if (m != tq_first(&cs->runasuserlist)) - lbuf_append(lbuf, ", "); - print_member(lbuf, m->name, m->type, m->negated, - RUNASALIAS); + if (RUNAS_CHANGED(cs, prev_cs)) { + if (cs != tq_first(&priv->cmndlist)) + lbuf_append(lbuf, "\n"); + lbuf_append(lbuf, " ("); + if (!tq_empty(&cs->runasuserlist)) { + tq_foreach_fwd(&cs->runasuserlist, m) { + if (m != tq_first(&cs->runasuserlist)) + lbuf_append(lbuf, ", "); + print_member(lbuf, m, RUNASALIAS); + } + } else if (tq_empty(&cs->runasgrouplist)) { + lbuf_append(lbuf, "%s", def_runas_default); + } else { + lbuf_append(lbuf, "%s", pw->pw_name); } - } else if (tq_empty(&cs->runasgrouplist)) { - lbuf_append(lbuf, "%s", def_runas_default); - } else { - lbuf_append(lbuf, "%s", pw->pw_name); - } - if (!tq_empty(&cs->runasgrouplist)) { - lbuf_append(lbuf, " : "); - tq_foreach_fwd(&cs->runasgrouplist, m) { - if (m != tq_first(&cs->runasgrouplist)) - lbuf_append(lbuf, ", "); - print_member(lbuf, m->name, m->type, m->negated, - RUNASALIAS); + if (!tq_empty(&cs->runasgrouplist)) { + lbuf_append(lbuf, " : "); + tq_foreach_fwd(&cs->runasgrouplist, m) { + if (m != tq_first(&cs->runasgrouplist)) + lbuf_append(lbuf, ", "); + print_member(lbuf, m, RUNASALIAS); + } } + lbuf_append(lbuf, ") "); + tags.noexec = UNSPEC; + tags.setenv = UNSPEC; + tags.nopasswd = UNSPEC; + tags.log_input = UNSPEC; + tags.log_output = UNSPEC; + } else if (cs != tq_first(&priv->cmndlist)) { + lbuf_append(lbuf, ", "); } - lbuf_append(lbuf, ") "); sudo_file_append_cmnd(cs, &tags, lbuf); + prev_cs = cs; nfound++; } lbuf_append(lbuf, "\n"); @@ -367,54 +409,115 @@ sudo_file_display_priv_short(struct passwd *pw, struct debug_return_int(nfound); } +#define TAGS_CHANGED(ot, nt) \ + ((TAG_SET((nt).setenv) && (nt).setenv != (ot).setenv) || \ + (TAG_SET((nt).noexec) && (nt).noexec != (ot).noexec) || \ + (TAG_SET((nt).nopasswd) && (nt).nopasswd != (ot).nopasswd) || \ + (TAG_SET((nt).log_input) && (nt).log_input != (ot).log_input) || \ + (TAG_SET((nt).log_output) && (nt).log_output != (ot).log_output)) + +/* + * Compare the current cmndspec with the previous one to determine + * whether we need to start a new long entry for "sudo -ll". + * Returns true if we should start a new long entry, else false. + */ +static bool +new_long_entry(struct cmndspec *cs, struct cmndspec *prev_cs) +{ + if (prev_cs == NULL) + return true; + if (RUNAS_CHANGED(cs, prev_cs) || TAGS_CHANGED(cs->tags, prev_cs->tags)) + return true; +#ifdef HAVE_PRIV_SET + if (cs->privs && (!prev_cs->privs || strcmp(cs->privs, prev_cs->privs) != 0)) + return true; + if (cs->limitprivs && (!prev_cs->limitprivs || strcmp(cs->limitprivs, prev_cs->limitprivs) != 0)) + return true; +#endif /* HAVE_PRIV_SET */ +#ifdef HAVE_SELINUX + if (cs->role && (!prev_cs->role || strcmp(cs->role, prev_cs->role) != 0)) + return true; + if (cs->type && (!prev_cs->type || strcmp(cs->type, prev_cs->type) != 0)) + return true; +#endif /* HAVE_SELINUX */ + return false; +} + static int sudo_file_display_priv_long(struct passwd *pw, struct userspec *us, struct lbuf *lbuf) { - struct cmndspec *cs; + struct cmndspec *cs, *prev_cs; struct member *m; struct privilege *priv; - struct cmndtag tags; - int nfound = 0; + int nfound = 0, olen; debug_decl(sudo_file_display_priv_long, SUDO_DEBUG_NSS) tq_foreach_fwd(&us->privileges, priv) { if (hostlist_matches(&priv->hostlist) != ALLOW) continue; - tags.noexec = UNSPEC; - tags.setenv = UNSPEC; - tags.nopasswd = UNSPEC; - tags.log_input = UNSPEC; - tags.log_output = UNSPEC; - lbuf_append(lbuf, _("\nSudoers entry:\n")); + prev_cs = NULL; tq_foreach_fwd(&priv->cmndlist, cs) { - lbuf_append(lbuf, _(" RunAsUsers: ")); - if (!tq_empty(&cs->runasuserlist)) { - tq_foreach_fwd(&cs->runasuserlist, m) { - if (m != tq_first(&cs->runasuserlist)) - lbuf_append(lbuf, ", "); - print_member(lbuf, m->name, m->type, m->negated, - RUNASALIAS); + if (new_long_entry(cs, prev_cs)) { + lbuf_append(lbuf, _("\nSudoers entry:\n")); + lbuf_append(lbuf, _(" RunAsUsers: ")); + if (!tq_empty(&cs->runasuserlist)) { + tq_foreach_fwd(&cs->runasuserlist, m) { + if (m != tq_first(&cs->runasuserlist)) + lbuf_append(lbuf, ", "); + print_member(lbuf, m, RUNASALIAS); + } + } else if (tq_empty(&cs->runasgrouplist)) { + lbuf_append(lbuf, "%s", def_runas_default); + } else { + lbuf_append(lbuf, "%s", pw->pw_name); } - } else if (tq_empty(&cs->runasgrouplist)) { - lbuf_append(lbuf, "%s", def_runas_default); - } else { - lbuf_append(lbuf, "%s", pw->pw_name); - } - lbuf_append(lbuf, "\n"); - if (!tq_empty(&cs->runasgrouplist)) { - lbuf_append(lbuf, _(" RunAsGroups: ")); - tq_foreach_fwd(&cs->runasgrouplist, m) { - if (m != tq_first(&cs->runasgrouplist)) - lbuf_append(lbuf, ", "); - print_member(lbuf, m->name, m->type, m->negated, - RUNASALIAS); - } lbuf_append(lbuf, "\n"); + if (!tq_empty(&cs->runasgrouplist)) { + lbuf_append(lbuf, _(" RunAsGroups: ")); + tq_foreach_fwd(&cs->runasgrouplist, m) { + if (m != tq_first(&cs->runasgrouplist)) + lbuf_append(lbuf, ", "); + print_member(lbuf, m, RUNASALIAS); + } + lbuf_append(lbuf, "\n"); + } + olen = lbuf->len; + lbuf_append(lbuf, _(" Options: ")); + if (TAG_SET(cs->tags.setenv)) + lbuf_append(lbuf, "%ssetenv, ", cs->tags.setenv ? "" : "!"); + if (TAG_SET(cs->tags.noexec)) + lbuf_append(lbuf, "%snoexec, ", cs->tags.noexec ? "" : "!"); + if (TAG_SET(cs->tags.nopasswd)) + lbuf_append(lbuf, "%sauthenticate, ", cs->tags.nopasswd ? "!" : ""); + if (TAG_SET(cs->tags.log_input)) + lbuf_append(lbuf, "%slog_input, ", cs->tags.log_input ? "" : "!"); + if (TAG_SET(cs->tags.log_output)) + lbuf_append(lbuf, "%slog_output, ", cs->tags.log_output ? "" : "!"); + if (lbuf->buf[lbuf->len - 2] == ',') { + lbuf->len -= 2; /* remove trailing ", " */ + lbuf_append(lbuf, "\n"); + } else { + lbuf->len = olen; /* no options */ + } +#ifdef HAVE_PRIV_SET + if (cs->privs) + lbuf_append(lbuf, " Privs: %s\n", cs->privs); + if (cs->limitprivs) + lbuf_append(lbuf, " Limitprivs: %s\n", cs->limitprivs); +#endif /* HAVE_PRIV_SET */ +#ifdef HAVE_SELINUX + if (cs->role) + lbuf_append(lbuf, " Role: %s\n", cs->role); + if (cs->type) + lbuf_append(lbuf, " Type: %s\n", cs->type); +#endif /* HAVE_SELINUX */ + lbuf_append(lbuf, _(" Commands:\n")); } - lbuf_append(lbuf, _(" Commands:\n\t")); - sudo_file_append_cmnd(cs, &tags, lbuf); + lbuf_append(lbuf, "\t"); + print_member2(lbuf, cs->cmnd, "\n\t", CMNDALIAS); lbuf_append(lbuf, "\n"); + prev_cs = cs; nfound++; } } @@ -560,7 +663,7 @@ display_bound_defaults(int dtype, struct lbuf *lbuf) for (m = binding; m != NULL; m = m->next) { if (m != binding) lbuf_append(lbuf, ","); - print_member(lbuf, m->name, m->type, m->negated, atype); + print_member(lbuf, m, atype); lbuf_append(lbuf, " "); } } else @@ -600,7 +703,7 @@ sudo_file_display_cmnd(struct sudo_nss *nss, struct pa continue; tq_foreach_rev(&priv->cmndlist, cs) { runas_match = runaslist_matches(&cs->runasuserlist, - &cs->runasgrouplist); + &cs->runasgrouplist, NULL, NULL); if (runas_match == ALLOW) { cmnd_match = cmnd_matches(cs->cmnd); if (cmnd_match != UNSPEC) { @@ -626,7 +729,7 @@ done: */ static void _print_member(struct lbuf *lbuf, char *name, int type, int negated, - int alias_type) + const char *separator, int alias_type) { struct alias *a; struct member *m; @@ -637,6 +740,9 @@ _print_member(struct lbuf *lbuf, char *name, int type, case ALL: lbuf_append(lbuf, "%sALL", negated ? "!" : ""); break; + case MYSELF: + lbuf_append(lbuf, "%s%s", negated ? "!" : "", user_name); + break; case COMMAND: c = (struct sudo_command *) name; if (negated) @@ -648,13 +754,15 @@ _print_member(struct lbuf *lbuf, char *name, int type, } break; case ALIAS: - if ((a = alias_find(name, alias_type)) != NULL) { + if ((a = alias_get(name, alias_type)) != NULL) { tq_foreach_fwd(&a->members, m) { if (m != tq_first(&a->members)) - lbuf_append(lbuf, ", "); + lbuf_append(lbuf, "%s", separator); _print_member(lbuf, m->name, m->type, - negated ? !m->negated : m->negated, alias_type); + negated ? !m->negated : m->negated, separator, + alias_type); } + alias_put(a); break; } /* FALLTHROUGH */ @@ -666,9 +774,14 @@ _print_member(struct lbuf *lbuf, char *name, int type, } static void -print_member(struct lbuf *lbuf, char *name, int type, int negated, +print_member(struct lbuf *lbuf, struct member *m, int alias_type) +{ + _print_member(lbuf, m->name, m->type, m->negated, ", ", alias_type); +} + +static void +print_member2(struct lbuf *lbuf, struct member *m, const char *separator, int alias_type) { - alias_seqno++; - _print_member(lbuf, name, type, negated, alias_type); + _print_member(lbuf, m->name, m->type, m->negated, separator, alias_type); }