2 * Copyright (c) 2004-2005, 2007-2009 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
16 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 #include <sys/types.h>
22 #include <sys/param.h>
31 #endif /* STDC_HEADERS */
35 # ifdef HAVE_STRINGS_H
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_UNISTD_H */
51 /* Characters that must be quoted in sudoers */
52 #define SUDOERS_QUOTED ":\\,=#\""
54 /* sudoers nsswitch routines */
55 struct sudo_nss sudo_nss_file = {
63 sudo_file_display_cmnd,
64 sudo_file_display_defaults,
65 sudo_file_display_bound_defaults,
66 sudo_file_display_privs
73 extern char *errorfile;
74 extern int errorlineno, parse_error;
79 static void print_member __P((struct lbuf *, char *, int, int, int));
80 static int display_bound_defaults __P((int, struct lbuf *));
86 if (def_ignore_local_sudoers)
88 nss->handle = open_sudoers(_PATH_SUDOERS, FALSE, NULL);
89 return(nss->handle ? 0 : -1);
96 /* Free parser data structures and close sudoers file. */
98 if (nss->handle != NULL) {
107 * Parse the specified sudoers file.
111 struct sudo_nss *nss;
113 if (nss->handle == NULL)
116 init_parser(_PATH_SUDOERS, 0);
118 if (yyparse() != 0 || parse_error) {
119 log_error(NO_EXIT, "parse error in %s near line %d",
120 errorfile, errorlineno);
127 * Wrapper around update_defaults() for nsswitch code.
130 sudo_file_setdefs(nss)
131 struct sudo_nss *nss;
133 if (nss->handle == NULL)
136 if (!update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER))
142 * Look up the user in the parsed sudoers file and check to see if they are
143 * allowed to run the specified command on this host as the target user.
146 sudo_file_lookup(nss, validated, pwflag)
147 struct sudo_nss *nss;
151 int match, host_match, runas_match, cmnd_match;
153 struct cmndtag *tags = NULL;
154 struct privilege *priv;
157 if (nss->handle == NULL)
161 * Only check the actual command if pwflag is not set.
162 * It is set for the "validate", "list" and "kill" pseudo-commands.
163 * Always check the host and user.
167 enum def_tupple pwcheck;
169 pwcheck = (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
170 nopass = (pwcheck == all) ? TRUE : FALSE;
173 SET(validated, FLAG_NO_CHECK);
174 CLR(validated, FLAG_NO_USER);
175 CLR(validated, FLAG_NO_HOST);
177 tq_foreach_fwd(&userspecs, us) {
178 if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
180 tq_foreach_fwd(&us->privileges, priv) {
181 if (hostlist_matches(&priv->hostlist) != ALLOW)
183 tq_foreach_fwd(&priv->cmndlist, cs) {
184 /* Only check the command when listing another user. */
185 if (user_uid == 0 || list_pw == NULL ||
186 user_uid == list_pw->pw_uid ||
187 cmnd_matches(cs->cmnd) == ALLOW)
189 if ((pwcheck == any && cs->tags.nopasswd == TRUE) ||
190 (pwcheck == all && cs->tags.nopasswd != TRUE))
191 nopass = cs->tags.nopasswd;
195 if (match == ALLOW || user_uid == 0) {
196 /* User has an entry for this host. */
197 SET(validated, VALIDATE_OK);
198 } else if (match == DENY)
199 SET(validated, VALIDATE_NOT_OK);
200 if (pwcheck == always && def_authenticate)
201 SET(validated, FLAG_CHECK_USER);
202 else if (pwcheck == never || nopass == TRUE)
203 def_authenticate = FALSE;
207 /* Need to be runas user while stat'ing things. */
208 set_perms(PERM_RUNAS);
211 tq_foreach_rev(&userspecs, us) {
212 if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
214 CLR(validated, FLAG_NO_USER);
215 tq_foreach_rev(&us->privileges, priv) {
216 host_match = hostlist_matches(&priv->hostlist);
217 if (host_match == ALLOW)
218 CLR(validated, FLAG_NO_HOST);
221 tq_foreach_rev(&priv->cmndlist, cs) {
222 runas_match = runaslist_matches(&cs->runasuserlist,
223 &cs->runasgrouplist);
224 if (runas_match == ALLOW) {
225 cmnd_match = cmnd_matches(cs->cmnd);
226 if (cmnd_match != UNSPEC) {
230 /* Set role and type if not specified on command line. */
231 if (user_role == NULL)
232 user_role = cs->role ? estrdup(cs->role) : def_role;
233 if (user_type == NULL)
234 user_type = cs->type ? estrdup(cs->type) : def_type;
235 #endif /* HAVE_SELINUX */
243 if (match == ALLOW) {
244 SET(validated, VALIDATE_OK);
245 CLR(validated, VALIDATE_NOT_OK);
247 if (tags->nopasswd != UNSPEC)
248 def_authenticate = !tags->nopasswd;
249 if (tags->noexec != UNSPEC)
250 def_noexec = tags->noexec;
251 if (tags->setenv != UNSPEC)
252 def_setenv = tags->setenv;
254 } else if (match == DENY) {
255 SET(validated, VALIDATE_NOT_OK);
256 CLR(validated, VALIDATE_OK);
258 set_perms(PERM_ROOT);
262 #define TAG_CHANGED(t) \
263 (cs->tags.t != UNSPEC && cs->tags.t != IMPLIED && cs->tags.t != tags->t)
266 sudo_file_append_cmnd(cs, tags, lbuf)
268 struct cmndtag *tags;
275 lbuf_append(lbuf, "ROLE=", cs->role, " ", NULL);
277 lbuf_append(lbuf, "TYPE=", cs->type, " ", NULL);
278 #endif /* HAVE_SELINUX */
279 if (TAG_CHANGED(setenv)) {
280 lbuf_append(lbuf, cs->tags.setenv ? "SETENV: " :
282 tags->setenv = cs->tags.setenv;
284 if (TAG_CHANGED(noexec)) {
285 lbuf_append(lbuf, cs->tags.noexec ? "NOEXEC: " :
287 tags->noexec = cs->tags.noexec;
289 if (TAG_CHANGED(nopasswd)) {
290 lbuf_append(lbuf, cs->tags.nopasswd ? "NOPASSWD: " :
292 tags->nopasswd = cs->tags.nopasswd;
295 print_member(lbuf, m->name, m->type, m->negated,
300 sudo_file_display_priv_short(pw, us, lbuf)
307 struct privilege *priv;
311 tq_foreach_fwd(&us->privileges, priv) {
312 if (hostlist_matches(&priv->hostlist) != ALLOW)
314 tags.noexec = UNSPEC;
315 tags.setenv = UNSPEC;
316 tags.nopasswd = UNSPEC;
317 lbuf_append(lbuf, " ", NULL);
318 tq_foreach_fwd(&priv->cmndlist, cs) {
319 if (cs != tq_first(&priv->cmndlist))
320 lbuf_append(lbuf, ", ", NULL);
321 lbuf_append(lbuf, "(", NULL);
322 if (!tq_empty(&cs->runasuserlist)) {
323 tq_foreach_fwd(&cs->runasuserlist, m) {
324 if (m != tq_first(&cs->runasuserlist))
325 lbuf_append(lbuf, ", ", NULL);
326 print_member(lbuf, m->name, m->type, m->negated,
329 } else if (tq_empty(&cs->runasgrouplist)) {
330 lbuf_append(lbuf, def_runas_default, NULL);
332 lbuf_append(lbuf, pw->pw_name, NULL);
334 if (!tq_empty(&cs->runasgrouplist)) {
335 lbuf_append(lbuf, " : ", NULL);
336 tq_foreach_fwd(&cs->runasgrouplist, m) {
337 if (m != tq_first(&cs->runasgrouplist))
338 lbuf_append(lbuf, ", ", NULL);
339 print_member(lbuf, m->name, m->type, m->negated,
343 lbuf_append(lbuf, ") ", NULL);
344 sudo_file_append_cmnd(cs, &tags, lbuf);
347 lbuf_print(lbuf); /* forces a newline */
353 sudo_file_display_priv_long(pw, us, lbuf)
360 struct privilege *priv;
364 tq_foreach_fwd(&us->privileges, priv) {
365 if (hostlist_matches(&priv->hostlist) != ALLOW)
367 tags.noexec = UNSPEC;
368 tags.setenv = UNSPEC;
369 tags.nopasswd = UNSPEC;
370 lbuf_print(lbuf); /* force a newline */
371 lbuf_append(lbuf, "Sudoers entry:", NULL);
373 tq_foreach_fwd(&priv->cmndlist, cs) {
374 lbuf_append(lbuf, " RunAsUsers: ", NULL);
375 if (!tq_empty(&cs->runasuserlist)) {
376 tq_foreach_fwd(&cs->runasuserlist, m) {
377 if (m != tq_first(&cs->runasuserlist))
378 lbuf_append(lbuf, ", ", NULL);
379 print_member(lbuf, m->name, m->type, m->negated,
382 } else if (tq_empty(&cs->runasgrouplist)) {
383 lbuf_append(lbuf, def_runas_default, NULL);
385 lbuf_append(lbuf, pw->pw_name, NULL);
388 if (!tq_empty(&cs->runasgrouplist)) {
389 lbuf_append(lbuf, " RunAsGroups: ", NULL);
390 tq_foreach_fwd(&cs->runasgrouplist, m) {
391 if (m != tq_first(&cs->runasgrouplist))
392 lbuf_append(lbuf, ", ", NULL);
393 print_member(lbuf, m->name, m->type, m->negated,
398 lbuf_append(lbuf, " Commands: ", NULL);
400 lbuf_append(lbuf, "\t", NULL);
401 sudo_file_append_cmnd(cs, &tags, lbuf);
410 sudo_file_display_privs(nss, pw, lbuf)
411 struct sudo_nss *nss;
418 if (nss->handle == NULL)
421 tq_foreach_fwd(&userspecs, us) {
422 if (userlist_matches(pw, &us->users) != ALLOW)
426 nfound += sudo_file_display_priv_long(pw, us, lbuf);
428 nfound += sudo_file_display_priv_short(pw, us, lbuf);
434 * Display matching Defaults entries for the given user on this host.
437 sudo_file_display_defaults(nss, pw, lbuf)
438 struct sudo_nss *nss;
446 if (nss->handle == NULL)
454 tq_foreach_fwd(&defaults, d) {
457 if (hostlist_matches(&d->binding) != ALLOW)
461 if (userlist_matches(pw, &d->binding) != ALLOW)
468 lbuf_append(lbuf, prefix, NULL);
469 if (d->val != NULL) {
470 lbuf_append(lbuf, d->var, d->op == '+' ? "+=" :
471 d->op == '-' ? "-=" : "=", NULL);
472 if (strpbrk(d->val, " \t") != NULL) {
473 lbuf_append(lbuf, "\"", NULL);
474 lbuf_append_quoted(lbuf, "\"", d->val, NULL);
475 lbuf_append(lbuf, "\"", NULL);
477 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, d->val, NULL);
479 lbuf_append(lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
488 * Display Defaults entries that are per-runas or per-command
491 sudo_file_display_bound_defaults(nss, pw, lbuf)
492 struct sudo_nss *nss;
498 /* XXX - should only print ones that match what the user can do. */
499 nfound += display_bound_defaults(DEFAULTS_RUNAS, lbuf);
500 nfound += display_bound_defaults(DEFAULTS_CMND, lbuf);
506 * Display Defaults entries of the given type.
509 display_bound_defaults(dtype, lbuf)
514 struct member *m, *binding = NULL;
516 int atype, nfound = 0;
542 /* printf("Per-%s Defaults entries:\n", dname); */
543 tq_foreach_fwd(&defaults, d) {
544 if (d->type != dtype)
548 if (binding != tq_first(&d->binding)) {
549 binding = tq_first(&d->binding);
550 lbuf_append(lbuf, " Defaults", dsep, NULL);
551 for (m = binding; m != NULL; m = m->next) {
553 lbuf_append(lbuf, ",", NULL);
554 print_member(lbuf, m->name, m->type, m->negated, atype);
555 lbuf_append(lbuf, " ", NULL);
558 lbuf_append(lbuf, ", ", NULL);
559 if (d->val != NULL) {
560 lbuf_append(lbuf, d->var, d->op == '+' ? "+=" :
561 d->op == '-' ? "-=" : "=", d->val, NULL);
563 lbuf_append(lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
570 sudo_file_display_cmnd(nss, pw)
571 struct sudo_nss *nss;
575 struct member *match;
576 struct privilege *priv;
579 int host_match, runas_match, cmnd_match;
581 if (nss->handle == NULL)
585 tq_foreach_rev(&userspecs, us) {
586 if (userlist_matches(pw, &us->users) != ALLOW)
589 tq_foreach_rev(&us->privileges, priv) {
590 host_match = hostlist_matches(&priv->hostlist);
591 if (host_match != ALLOW)
593 tq_foreach_rev(&priv->cmndlist, cs) {
594 runas_match = runaslist_matches(&cs->runasuserlist,
595 &cs->runasgrouplist);
596 if (runas_match == ALLOW) {
597 cmnd_match = cmnd_matches(cs->cmnd);
598 if (cmnd_match != UNSPEC) {
599 match = host_match && runas_match ?
608 if (match != NULL && !match->negated) {
609 printf("%s%s%s\n", safe_cmnd, user_args ? " " : "",
610 user_args ? user_args : "");
617 * Print the contents of a struct member to stdout
620 _print_member(lbuf, name, type, negated, alias_type)
623 int type, negated, alias_type;
627 struct sudo_command *c;
631 lbuf_append(lbuf, negated ? "!ALL" : "ALL", NULL);
634 c = (struct sudo_command *) name;
636 lbuf_append(lbuf, "!", NULL);
637 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->cmnd, NULL);
639 lbuf_append(lbuf, " ", NULL);
640 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->args, NULL);
644 if ((a = alias_find(name, alias_type)) != NULL) {
645 tq_foreach_fwd(&a->members, m) {
646 if (m != tq_first(&a->members))
647 lbuf_append(lbuf, ", ", NULL);
648 _print_member(lbuf, m->name, m->type,
649 negated ? !m->negated : m->negated, alias_type);
655 lbuf_append(lbuf, negated ? "!" : "", name, NULL);
661 print_member(lbuf, name, type, negated, alias_type)
664 int type, negated, alias_type;
667 _print_member(lbuf, name, type, negated, alias_type);