update changelog
[debian/sudo] / parse.c
1 /*
2  * Copyright (c) 2004-2005, 2007-2010 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
17  */
18
19 #include <config.h>
20
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <stdio.h>
24 #ifdef STDC_HEADERS
25 # include <stdlib.h>
26 # include <stddef.h>
27 #else
28 # ifdef HAVE_STDLIB_H
29 #  include <stdlib.h>
30 # endif
31 #endif /* STDC_HEADERS */
32 #ifdef HAVE_STRING_H
33 # include <string.h>
34 #endif /* HAVE_STRING_H */
35 #ifdef HAVE_STRINGS_H
36 # include <strings.h>
37 #endif /* HAVE_STRINGS_H */
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif /* HAVE_UNISTD_H */
41 #include <ctype.h>
42 #include <pwd.h>
43 #include <grp.h>
44
45 #include "sudo.h"
46 #include "parse.h"
47 #include "lbuf.h"
48 #include <gram.h>
49
50 /* Characters that must be quoted in sudoers */
51 #define SUDOERS_QUOTED  ":\\,=#\""
52
53 /* sudoers nsswitch routines */
54 struct sudo_nss sudo_nss_file = {
55     &sudo_nss_file,
56     NULL,
57     sudo_file_open,
58     sudo_file_close,
59     sudo_file_parse,
60     sudo_file_setdefs,
61     sudo_file_lookup,
62     sudo_file_display_cmnd,
63     sudo_file_display_defaults,
64     sudo_file_display_bound_defaults,
65     sudo_file_display_privs
66 };
67
68 /*
69  * Parser externs.
70  */
71 extern FILE *yyin;
72 extern char *errorfile;
73 extern int errorlineno, parse_error;
74
75 /*
76  * Local prototypes.
77  */
78 static void print_member        __P((struct lbuf *, char *, int, int, int));
79 static int display_bound_defaults __P((int, struct lbuf *));
80
81 int
82 sudo_file_open(nss)
83     struct sudo_nss *nss;
84 {
85     if (def_ignore_local_sudoers)
86         return(-1);
87     nss->handle = open_sudoers(_PATH_SUDOERS, FALSE, NULL);
88     return(nss->handle ? 0 : -1);
89 }
90
91 int
92 sudo_file_close(nss)
93     struct sudo_nss *nss;
94 {
95     /* Free parser data structures and close sudoers file. */
96     init_parser(NULL, 0);
97     if (nss->handle != NULL) {
98         fclose(nss->handle);
99         nss->handle = NULL;
100         yyin = NULL;
101     }
102     return(0);
103 }
104
105 /*
106  * Parse the specified sudoers file.
107  */
108 int
109 sudo_file_parse(nss)
110     struct sudo_nss *nss;
111 {
112     if (nss->handle == NULL)
113         return(-1);
114
115     init_parser(_PATH_SUDOERS, 0);
116     yyin = nss->handle;
117     if (yyparse() != 0 || parse_error) {
118         log_error(NO_EXIT, "parse error in %s near line %d",
119             errorfile, errorlineno);
120         return(-1);
121     }
122     return(0);
123 }
124
125 /*
126  * Wrapper around update_defaults() for nsswitch code.
127  */
128 int
129 sudo_file_setdefs(nss)
130     struct sudo_nss *nss;
131 {
132     if (nss->handle == NULL)
133         return(-1);
134
135     if (!update_defaults(SETDEF_GENERIC|SETDEF_HOST|SETDEF_USER))
136         return(-1);
137     return(0);
138 }
139
140 /*
141  * Look up the user in the parsed sudoers file and check to see if they are
142  * allowed to run the specified command on this host as the target user.
143  */
144 int
145 sudo_file_lookup(nss, validated, pwflag)
146     struct sudo_nss *nss;
147     int validated;
148     int pwflag;
149 {
150     int match, host_match, runas_match, cmnd_match;
151     struct cmndspec *cs;
152     struct cmndtag *tags = NULL;
153     struct privilege *priv;
154     struct userspec *us;
155
156     if (nss->handle == NULL)
157         return(validated);
158
159     /*
160      * Only check the actual command if pwflag is not set.
161      * It is set for the "validate", "list" and "kill" pseudo-commands.
162      * Always check the host and user.
163      */
164     if (pwflag) {
165         int nopass;
166         enum def_tupple pwcheck;
167
168         pwcheck = (pwflag == -1) ? never : sudo_defs_table[pwflag].sd_un.tuple;
169         nopass = (pwcheck == all) ? TRUE : FALSE;
170
171         if (list_pw == NULL)
172             SET(validated, FLAG_NO_CHECK);
173         CLR(validated, FLAG_NO_USER);
174         CLR(validated, FLAG_NO_HOST);
175         match = DENY;
176         tq_foreach_fwd(&userspecs, us) {
177             if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
178                 continue;
179             tq_foreach_fwd(&us->privileges, priv) {
180                 if (hostlist_matches(&priv->hostlist) != ALLOW)
181                     continue;
182                 tq_foreach_fwd(&priv->cmndlist, cs) {
183                     /* Only check the command when listing another user. */
184                     if (user_uid == 0 || list_pw == NULL ||
185                         user_uid == list_pw->pw_uid ||
186                         cmnd_matches(cs->cmnd) == ALLOW)
187                             match = ALLOW;
188                     if ((pwcheck == any && cs->tags.nopasswd == TRUE) ||
189                         (pwcheck == all && cs->tags.nopasswd != TRUE))
190                         nopass = cs->tags.nopasswd;
191                 }
192             }
193         }
194         if (match == ALLOW || user_uid == 0) {
195             /* User has an entry for this host. */
196             SET(validated, VALIDATE_OK);
197         } else if (match == DENY)
198             SET(validated, VALIDATE_NOT_OK);
199         if (pwcheck == always && def_authenticate)
200             SET(validated, FLAG_CHECK_USER);
201         else if (pwcheck == never || nopass == TRUE)
202             def_authenticate = FALSE;
203         return(validated);
204     }
205
206     /* Need to be runas user while stat'ing things. */
207     set_perms(PERM_RUNAS);
208
209     match = UNSPEC;
210     tq_foreach_rev(&userspecs, us) {
211         if (userlist_matches(sudo_user.pw, &us->users) != ALLOW)
212             continue;
213         CLR(validated, FLAG_NO_USER);
214         tq_foreach_rev(&us->privileges, priv) {
215             host_match = hostlist_matches(&priv->hostlist);
216             if (host_match == ALLOW)
217                 CLR(validated, FLAG_NO_HOST);
218             else
219                 continue;
220             tq_foreach_rev(&priv->cmndlist, cs) {
221                 runas_match = runaslist_matches(&cs->runasuserlist,
222                     &cs->runasgrouplist);
223                 if (runas_match == ALLOW) {
224                     cmnd_match = cmnd_matches(cs->cmnd);
225                     if (cmnd_match != UNSPEC) {
226                         match = cmnd_match;
227                         tags = &cs->tags;
228 #ifdef HAVE_SELINUX
229                         /* Set role and type if not specified on command line. */
230                         if (user_role == NULL)
231                             user_role = cs->role ? estrdup(cs->role) : def_role;
232                         if (user_type == NULL)
233                             user_type = cs->type ? estrdup(cs->type) : def_type;
234 #endif /* HAVE_SELINUX */
235                         goto matched2;
236                     }
237                 }
238             }
239         }
240     }
241     matched2:
242     if (match == ALLOW) {
243         SET(validated, VALIDATE_OK);
244         CLR(validated, VALIDATE_NOT_OK);
245         if (tags != NULL) {
246             if (tags->nopasswd != UNSPEC)
247                 def_authenticate = !tags->nopasswd;
248             if (tags->noexec != UNSPEC)
249                 def_noexec = tags->noexec;
250             if (tags->setenv != UNSPEC)
251                 def_setenv = tags->setenv;
252             if (tags->log_input != UNSPEC)
253                 def_log_input = tags->log_input;
254             if (tags->log_output != UNSPEC)
255                 def_log_output = tags->log_output;
256         }
257     } else if (match == DENY) {
258         SET(validated, VALIDATE_NOT_OK);
259         CLR(validated, VALIDATE_OK);
260     }
261     set_perms(PERM_ROOT);
262     return(validated);
263 }
264
265 #define TAG_CHANGED(t) \
266         (cs->tags.t != UNSPEC && cs->tags.t != IMPLIED && cs->tags.t != tags->t)
267
268 static void
269 sudo_file_append_cmnd(cs, tags, lbuf)
270     struct cmndspec *cs;
271     struct cmndtag *tags;
272     struct lbuf *lbuf;
273 {
274     struct member *m;
275
276 #ifdef HAVE_SELINUX
277     if (cs->role)
278         lbuf_append(lbuf, "ROLE=", cs->role, " ", NULL);
279     if (cs->type)
280         lbuf_append(lbuf, "TYPE=", cs->type, " ", NULL);
281 #endif /* HAVE_SELINUX */
282     if (TAG_CHANGED(setenv)) {
283         lbuf_append(lbuf, cs->tags.setenv ? "SETENV: " :
284             "NOSETENV: ", NULL);
285         tags->setenv = cs->tags.setenv;
286     }
287     if (TAG_CHANGED(noexec)) {
288         lbuf_append(lbuf, cs->tags.noexec ? "NOEXEC: " :
289             "EXEC: ", NULL);
290         tags->noexec = cs->tags.noexec;
291     }
292     if (TAG_CHANGED(nopasswd)) {
293         lbuf_append(lbuf, cs->tags.nopasswd ? "NOPASSWD: " :
294             "PASSWD: ", NULL);
295         tags->nopasswd = cs->tags.nopasswd;
296     }
297     if (TAG_CHANGED(log_input)) {
298         lbuf_append(lbuf, cs->tags.log_input ? "LOG_INPUT: " :
299             "NOLOG_INPUT: ", NULL);
300         tags->log_input = cs->tags.log_input;
301     }
302     if (TAG_CHANGED(log_output)) {
303         lbuf_append(lbuf, cs->tags.log_output ? "LOG_OUTPUT: " :
304             "NOLOG_OUTPUT: ", NULL);
305         tags->log_output = cs->tags.log_output;
306     }
307     m = cs->cmnd;
308     print_member(lbuf, m->name, m->type, m->negated,
309         CMNDALIAS);
310 }
311
312 static int
313 sudo_file_display_priv_short(pw, us, lbuf)
314     struct passwd *pw;
315     struct userspec *us;
316     struct lbuf *lbuf;
317 {
318     struct cmndspec *cs;
319     struct member *m;
320     struct privilege *priv;
321     struct cmndtag tags;
322     int nfound = 0;
323
324     tq_foreach_fwd(&us->privileges, priv) {
325         if (hostlist_matches(&priv->hostlist) != ALLOW)
326             continue;
327         tags.noexec = UNSPEC;
328         tags.setenv = UNSPEC;
329         tags.nopasswd = UNSPEC;
330         tags.log_input = UNSPEC;
331         tags.log_output = UNSPEC;
332         lbuf_append(lbuf, "    ", NULL);
333         tq_foreach_fwd(&priv->cmndlist, cs) {
334             if (cs != tq_first(&priv->cmndlist))
335                 lbuf_append(lbuf, ", ", NULL);
336             lbuf_append(lbuf, "(", NULL);
337             if (!tq_empty(&cs->runasuserlist)) {
338                 tq_foreach_fwd(&cs->runasuserlist, m) {
339                     if (m != tq_first(&cs->runasuserlist))
340                         lbuf_append(lbuf, ", ", NULL);
341                     print_member(lbuf, m->name, m->type, m->negated,
342                         RUNASALIAS);
343                 }
344             } else if (tq_empty(&cs->runasgrouplist)) {
345                 lbuf_append(lbuf, def_runas_default, NULL);
346             } else {
347                 lbuf_append(lbuf, pw->pw_name, NULL);
348             }
349             if (!tq_empty(&cs->runasgrouplist)) {
350                 lbuf_append(lbuf, " : ", NULL);
351                 tq_foreach_fwd(&cs->runasgrouplist, m) {
352                     if (m != tq_first(&cs->runasgrouplist))
353                         lbuf_append(lbuf, ", ", NULL);
354                     print_member(lbuf, m->name, m->type, m->negated,
355                         RUNASALIAS);
356                 }
357             }
358             lbuf_append(lbuf, ") ", NULL);
359             sudo_file_append_cmnd(cs, &tags, lbuf);
360             nfound++;
361         }
362         lbuf_append(lbuf, "\n", NULL);
363     }
364     return(nfound);
365 }
366
367 static int
368 sudo_file_display_priv_long(pw, us, lbuf)
369     struct passwd *pw;
370     struct userspec *us;
371     struct lbuf *lbuf;
372 {
373     struct cmndspec *cs;
374     struct member *m;
375     struct privilege *priv;
376     struct cmndtag tags;
377     int nfound = 0;
378
379     tq_foreach_fwd(&us->privileges, priv) {
380         if (hostlist_matches(&priv->hostlist) != ALLOW)
381             continue;
382         tags.noexec = UNSPEC;
383         tags.setenv = UNSPEC;
384         tags.nopasswd = UNSPEC;
385         tags.log_input = UNSPEC;
386         tags.log_output = UNSPEC;
387         lbuf_append(lbuf, "\nSudoers entry:\n", NULL);
388         tq_foreach_fwd(&priv->cmndlist, cs) {
389             lbuf_append(lbuf, "    RunAsUsers: ", NULL);
390             if (!tq_empty(&cs->runasuserlist)) {
391                 tq_foreach_fwd(&cs->runasuserlist, m) {
392                     if (m != tq_first(&cs->runasuserlist))
393                         lbuf_append(lbuf, ", ", NULL);
394                     print_member(lbuf, m->name, m->type, m->negated,
395                         RUNASALIAS);
396                 }
397             } else if (tq_empty(&cs->runasgrouplist)) {
398                 lbuf_append(lbuf, def_runas_default, NULL);
399             } else {
400                 lbuf_append(lbuf, pw->pw_name, NULL);
401             }
402             lbuf_append(lbuf, "\n", NULL);
403             if (!tq_empty(&cs->runasgrouplist)) {
404                 lbuf_append(lbuf, "    RunAsGroups: ", NULL);
405                 tq_foreach_fwd(&cs->runasgrouplist, m) {
406                     if (m != tq_first(&cs->runasgrouplist))
407                         lbuf_append(lbuf, ", ", NULL);
408                     print_member(lbuf, m->name, m->type, m->negated,
409                         RUNASALIAS);
410                 }
411                 lbuf_append(lbuf, "\n", NULL);
412             }
413             lbuf_append(lbuf, "    Commands:\n\t", NULL);
414             sudo_file_append_cmnd(cs, &tags, lbuf);
415             lbuf_append(lbuf, "\n", NULL);
416             nfound++;
417         }
418     }
419     return(nfound);
420 }
421
422 int
423 sudo_file_display_privs(nss, pw, lbuf)
424     struct sudo_nss *nss;
425     struct passwd *pw;
426     struct lbuf *lbuf;
427 {
428     struct userspec *us;
429     int nfound = 0;
430
431     if (nss->handle == NULL)
432         return(-1);
433
434     tq_foreach_fwd(&userspecs, us) {
435         if (userlist_matches(pw, &us->users) != ALLOW)
436             continue;
437
438         if (long_list)
439             nfound += sudo_file_display_priv_long(pw, us, lbuf);
440         else
441             nfound += sudo_file_display_priv_short(pw, us, lbuf);
442     }
443     return(nfound);
444 }
445
446 /*
447  * Display matching Defaults entries for the given user on this host.
448  */
449 int
450 sudo_file_display_defaults(nss, pw, lbuf)
451     struct sudo_nss *nss;
452     struct passwd *pw;
453     struct lbuf *lbuf;
454 {
455     struct defaults *d;
456     char *prefix;
457     int nfound = 0;
458
459     if (nss->handle == NULL)
460         return(-1);
461
462     if (lbuf->len == 0 || isspace((unsigned char)lbuf->buf[lbuf->len - 1]))
463         prefix = "    ";
464     else
465         prefix = ", ";
466
467     tq_foreach_fwd(&defaults, d) {
468         switch (d->type) {
469             case DEFAULTS_HOST:
470                 if (hostlist_matches(&d->binding) != ALLOW)
471                     continue;
472                 break;
473             case DEFAULTS_USER:
474                 if (userlist_matches(pw, &d->binding) != ALLOW)
475                     continue;
476                 break;
477             case DEFAULTS_RUNAS:
478             case DEFAULTS_CMND:
479                 continue;
480         }
481         lbuf_append(lbuf, prefix, NULL);
482         if (d->val != NULL) {
483             lbuf_append(lbuf, d->var, d->op == '+' ? "+=" :
484                 d->op == '-' ? "-=" : "=", NULL);
485             if (strpbrk(d->val, " \t") != NULL) {
486                 lbuf_append(lbuf, "\"", NULL);
487                 lbuf_append_quoted(lbuf, "\"", d->val, NULL);
488                 lbuf_append(lbuf, "\"", NULL);
489             } else
490                 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, d->val, NULL);
491         } else
492             lbuf_append(lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
493         prefix = ", ";
494         nfound++;
495     }
496
497     return(nfound);
498 }
499
500 /*
501  * Display Defaults entries that are per-runas or per-command
502  */
503 int
504 sudo_file_display_bound_defaults(nss, pw, lbuf)
505     struct sudo_nss *nss;
506     struct passwd *pw;
507     struct lbuf *lbuf;
508 {
509     int nfound = 0;
510
511     /* XXX - should only print ones that match what the user can do. */
512     nfound += display_bound_defaults(DEFAULTS_RUNAS, lbuf);
513     nfound += display_bound_defaults(DEFAULTS_CMND, lbuf);
514
515     return(nfound);
516 }
517
518 /*
519  * Display Defaults entries of the given type.
520  */
521 static int
522 display_bound_defaults(dtype, lbuf)
523     int dtype;
524     struct lbuf *lbuf;
525 {
526     struct defaults *d;
527     struct member *m, *binding = NULL;
528     char *dname, *dsep;
529     int atype, nfound = 0;
530
531     switch (dtype) {
532         case DEFAULTS_HOST:
533             atype = HOSTALIAS;
534             dname = "host";
535             dsep = "@";
536             break;
537         case DEFAULTS_USER:
538             atype = USERALIAS;
539             dname = "user";
540             dsep = ":";
541             break;
542         case DEFAULTS_RUNAS:
543             atype = RUNASALIAS;
544             dname = "runas";
545             dsep = ">";
546             break;
547         case DEFAULTS_CMND:
548             atype = CMNDALIAS;
549             dname = "cmnd";
550             dsep = "!";
551             break;
552         default:
553             return(-1);
554     }
555     /* printf("Per-%s Defaults entries:\n", dname); */
556     tq_foreach_fwd(&defaults, d) {
557         if (d->type != dtype)
558             continue;
559
560         nfound++;
561         if (binding != tq_first(&d->binding)) {
562             binding = tq_first(&d->binding);
563             if (nfound != 1)
564                 lbuf_append(lbuf, "\n", NULL);
565             lbuf_append(lbuf, "    Defaults", dsep, NULL);
566             for (m = binding; m != NULL; m = m->next) {
567                 if (m != binding)
568                     lbuf_append(lbuf, ",", NULL);
569                 print_member(lbuf, m->name, m->type, m->negated, atype);
570                 lbuf_append(lbuf, " ", NULL);
571             }
572         } else
573             lbuf_append(lbuf, ", ", NULL);
574         if (d->val != NULL) {
575             lbuf_append(lbuf, d->var, d->op == '+' ? "+=" :
576                 d->op == '-' ? "-=" : "=", d->val, NULL);
577         } else
578             lbuf_append(lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
579     }
580
581     return(nfound);
582 }
583
584 int
585 sudo_file_display_cmnd(nss, pw)
586     struct sudo_nss *nss;
587     struct passwd *pw;
588 {
589     struct cmndspec *cs;
590     struct member *match;
591     struct privilege *priv;
592     struct userspec *us;
593     int rval = 1;
594     int host_match, runas_match, cmnd_match;
595
596     if (nss->handle == NULL)
597         return(rval);
598
599     match = NULL;
600     tq_foreach_rev(&userspecs, us) {
601         if (userlist_matches(pw, &us->users) != ALLOW)
602             continue;
603
604         tq_foreach_rev(&us->privileges, priv) {
605             host_match = hostlist_matches(&priv->hostlist);
606             if (host_match != ALLOW)
607                 continue;
608             tq_foreach_rev(&priv->cmndlist, cs) {
609                 runas_match = runaslist_matches(&cs->runasuserlist,
610                     &cs->runasgrouplist);
611                 if (runas_match == ALLOW) {
612                     cmnd_match = cmnd_matches(cs->cmnd);
613                     if (cmnd_match != UNSPEC) {
614                         match = host_match && runas_match ?
615                             cs->cmnd : NULL;
616                         goto matched;
617                     }
618                 }
619             }
620         }
621     }
622     matched:
623     if (match != NULL && !match->negated) {
624         printf("%s%s%s\n", safe_cmnd, user_args ? " " : "",
625             user_args ? user_args : "");
626         rval = 0;
627     }
628     return(rval);
629 }
630
631 /*
632  * Print the contents of a struct member to stdout
633  */
634 static void
635 _print_member(lbuf, name, type, negated, alias_type)
636     struct lbuf *lbuf;
637     char *name;
638     int type, negated, alias_type;
639 {
640     struct alias *a;
641     struct member *m;
642     struct sudo_command *c;
643
644     switch (type) {
645         case ALL:
646             lbuf_append(lbuf, negated ? "!ALL" : "ALL", NULL);
647             break;
648         case COMMAND:
649             c = (struct sudo_command *) name;
650             if (negated)
651                 lbuf_append(lbuf, "!", NULL);
652             lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->cmnd, NULL);
653             if (c->args) {
654                 lbuf_append(lbuf, " ", NULL);
655                 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->args, NULL);
656             }
657             break;
658         case ALIAS:
659             if ((a = alias_find(name, alias_type)) != NULL) {
660                 tq_foreach_fwd(&a->members, m) {
661                     if (m != tq_first(&a->members))
662                         lbuf_append(lbuf, ", ", NULL);
663                     _print_member(lbuf, m->name, m->type,
664                         negated ? !m->negated : m->negated, alias_type);
665                 }
666                 break;
667             }
668             /* FALLTHROUGH */
669         default:
670             lbuf_append(lbuf, negated ? "!" : "", name, NULL);
671             break;
672     }
673 }
674
675 static void
676 print_member(lbuf, name, type, negated, alias_type)
677     struct lbuf *lbuf;
678     char *name;
679     int type, negated, alias_type;
680 {
681     alias_seqno++;
682     _print_member(lbuf, name, type, negated, alias_type);
683 }