update pam config to use common-session
[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         goto done;
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 done:
444     return nfound;
445 }
446
447 /*
448  * Display matching Defaults entries for the given user on this host.
449  */
450 int
451 sudo_file_display_defaults(nss, pw, lbuf)
452     struct sudo_nss *nss;
453     struct passwd *pw;
454     struct lbuf *lbuf;
455 {
456     struct defaults *d;
457     char *prefix;
458     int nfound = 0;
459
460     if (nss->handle == NULL)
461         goto done;
462
463     if (lbuf->len == 0 || isspace((unsigned char)lbuf->buf[lbuf->len - 1]))
464         prefix = "    ";
465     else
466         prefix = ", ";
467
468     tq_foreach_fwd(&defaults, d) {
469         switch (d->type) {
470             case DEFAULTS_HOST:
471                 if (hostlist_matches(&d->binding) != ALLOW)
472                     continue;
473                 break;
474             case DEFAULTS_USER:
475                 if (userlist_matches(pw, &d->binding) != ALLOW)
476                     continue;
477                 break;
478             case DEFAULTS_RUNAS:
479             case DEFAULTS_CMND:
480                 continue;
481         }
482         lbuf_append(lbuf, prefix, NULL);
483         if (d->val != NULL) {
484             lbuf_append(lbuf, d->var, d->op == '+' ? "+=" :
485                 d->op == '-' ? "-=" : "=", NULL);
486             if (strpbrk(d->val, " \t") != NULL) {
487                 lbuf_append(lbuf, "\"", NULL);
488                 lbuf_append_quoted(lbuf, "\"", d->val, NULL);
489                 lbuf_append(lbuf, "\"", NULL);
490             } else
491                 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, d->val, NULL);
492         } else
493             lbuf_append(lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
494         prefix = ", ";
495         nfound++;
496     }
497 done:
498     return nfound;
499 }
500
501 /*
502  * Display Defaults entries that are per-runas or per-command
503  */
504 int
505 sudo_file_display_bound_defaults(nss, pw, lbuf)
506     struct sudo_nss *nss;
507     struct passwd *pw;
508     struct lbuf *lbuf;
509 {
510     int nfound = 0;
511
512     /* XXX - should only print ones that match what the user can do. */
513     nfound += display_bound_defaults(DEFAULTS_RUNAS, lbuf);
514     nfound += display_bound_defaults(DEFAULTS_CMND, lbuf);
515
516     return nfound;
517 }
518
519 /*
520  * Display Defaults entries of the given type.
521  */
522 static int
523 display_bound_defaults(dtype, lbuf)
524     int dtype;
525     struct lbuf *lbuf;
526 {
527     struct defaults *d;
528     struct member *m, *binding = NULL;
529     char *dname, *dsep;
530     int atype, nfound = 0;
531
532     switch (dtype) {
533         case DEFAULTS_HOST:
534             atype = HOSTALIAS;
535             dname = "host";
536             dsep = "@";
537             break;
538         case DEFAULTS_USER:
539             atype = USERALIAS;
540             dname = "user";
541             dsep = ":";
542             break;
543         case DEFAULTS_RUNAS:
544             atype = RUNASALIAS;
545             dname = "runas";
546             dsep = ">";
547             break;
548         case DEFAULTS_CMND:
549             atype = CMNDALIAS;
550             dname = "cmnd";
551             dsep = "!";
552             break;
553         default:
554             return -1;
555     }
556     /* printf("Per-%s Defaults entries:\n", dname); */
557     tq_foreach_fwd(&defaults, d) {
558         if (d->type != dtype)
559             continue;
560
561         nfound++;
562         if (binding != tq_first(&d->binding)) {
563             binding = tq_first(&d->binding);
564             if (nfound != 1)
565                 lbuf_append(lbuf, "\n", NULL);
566             lbuf_append(lbuf, "    Defaults", dsep, NULL);
567             for (m = binding; m != NULL; m = m->next) {
568                 if (m != binding)
569                     lbuf_append(lbuf, ",", NULL);
570                 print_member(lbuf, m->name, m->type, m->negated, atype);
571                 lbuf_append(lbuf, " ", NULL);
572             }
573         } else
574             lbuf_append(lbuf, ", ", NULL);
575         if (d->val != NULL) {
576             lbuf_append(lbuf, d->var, d->op == '+' ? "+=" :
577                 d->op == '-' ? "-=" : "=", d->val, NULL);
578         } else
579             lbuf_append(lbuf, d->op == FALSE ? "!" : "", d->var, NULL);
580     }
581
582     return nfound;
583 }
584
585 int
586 sudo_file_display_cmnd(nss, pw)
587     struct sudo_nss *nss;
588     struct passwd *pw;
589 {
590     struct cmndspec *cs;
591     struct member *match;
592     struct privilege *priv;
593     struct userspec *us;
594     int rval = 1;
595     int host_match, runas_match, cmnd_match;
596
597     if (nss->handle == NULL)
598         goto done;
599
600     match = NULL;
601     tq_foreach_rev(&userspecs, us) {
602         if (userlist_matches(pw, &us->users) != ALLOW)
603             continue;
604
605         tq_foreach_rev(&us->privileges, priv) {
606             host_match = hostlist_matches(&priv->hostlist);
607             if (host_match != ALLOW)
608                 continue;
609             tq_foreach_rev(&priv->cmndlist, cs) {
610                 runas_match = runaslist_matches(&cs->runasuserlist,
611                     &cs->runasgrouplist);
612                 if (runas_match == ALLOW) {
613                     cmnd_match = cmnd_matches(cs->cmnd);
614                     if (cmnd_match != UNSPEC) {
615                         match = host_match && runas_match ? 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 done:
629     return rval;
630 }
631
632 /*
633  * Print the contents of a struct member to stdout
634  */
635 static void
636 _print_member(lbuf, name, type, negated, alias_type)
637     struct lbuf *lbuf;
638     char *name;
639     int type, negated, alias_type;
640 {
641     struct alias *a;
642     struct member *m;
643     struct sudo_command *c;
644
645     switch (type) {
646         case ALL:
647             lbuf_append(lbuf, negated ? "!ALL" : "ALL", NULL);
648             break;
649         case COMMAND:
650             c = (struct sudo_command *) name;
651             if (negated)
652                 lbuf_append(lbuf, "!", NULL);
653             lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->cmnd, NULL);
654             if (c->args) {
655                 lbuf_append(lbuf, " ", NULL);
656                 lbuf_append_quoted(lbuf, SUDOERS_QUOTED, c->args, NULL);
657             }
658             break;
659         case ALIAS:
660             if ((a = alias_find(name, alias_type)) != NULL) {
661                 tq_foreach_fwd(&a->members, m) {
662                     if (m != tq_first(&a->members))
663                         lbuf_append(lbuf, ", ", NULL);
664                     _print_member(lbuf, m->name, m->type,
665                         negated ? !m->negated : m->negated, alias_type);
666                 }
667                 break;
668             }
669             /* FALLTHROUGH */
670         default:
671             lbuf_append(lbuf, negated ? "!" : "", name, NULL);
672             break;
673     }
674 }
675
676 static void
677 print_member(lbuf, name, type, negated, alias_type)
678     struct lbuf *lbuf;
679     char *name;
680     int type, negated, alias_type;
681 {
682     alias_seqno++;
683     _print_member(lbuf, name, type, negated, alias_type);
684 }