Imported Upstream version 1.6.9p17
[debian/sudo] / parse.yacc
1 %{
2 /*
3  * Copyright (c) 1996, 1998-2004, 2007
4  *      Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
18  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  *
20  * Sponsored in part by the Defense Advanced Research Projects
21  * Agency (DARPA) and Air Force Research Laboratory, Air Force
22  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23  */
24
25 /*
26  * XXX - the whole opFOO naming thing is somewhat bogus.
27  *
28  * XXX - the way things are stored for printmatches is stupid,
29  *       they should be stored as elements in an array and then
30  *       list_matches() can format things the way it wants.
31  */
32
33 #include <config.h>
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <stdio.h>
38 #ifdef STDC_HEADERS
39 # include <stdlib.h>
40 # include <stddef.h>
41 #else
42 # ifdef HAVE_STDLIB_H
43 #  include <stdlib.h>
44 # endif
45 #endif /* STDC_HEADERS */
46 #ifdef HAVE_STRING_H
47 # include <string.h>
48 #else
49 # ifdef HAVE_STRINGS_H
50 #  include <strings.h>
51 # endif
52 #endif /* HAVE_STRING_H */
53 #ifdef HAVE_UNISTD_H
54 # include <unistd.h>
55 #endif /* HAVE_UNISTD_H */
56 #include <pwd.h>
57 #if defined(YYBISON) && defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
58 # include <alloca.h>
59 #endif /* YYBISON && HAVE_ALLOCA_H && !__GNUC__ */
60 #ifdef HAVE_LSEARCH
61 # include <search.h>
62 #endif /* HAVE_LSEARCH */
63 #include <limits.h>
64
65 #include "sudo.h"
66 #include "parse.h"
67
68 #ifndef HAVE_LSEARCH
69 #include "emul/search.h"
70 #endif /* HAVE_LSEARCH */
71
72 #ifndef lint
73 __unused static const char rcsid[] = "$Sudo: parse.yacc,v 1.204.2.13 2008/02/27 20:34:42 millert Exp $";
74 #endif /* lint */
75
76 /*
77  * We must define SIZE_MAX for yacc's skeleton.c.
78  * If there is no SIZE_MAX or SIZE_T_MAX we have to assume that size_t
79  * could be signed (as it is on SunOS 4.x).
80  */
81 #ifndef SIZE_MAX
82 # ifdef SIZE_T_MAX
83 #  define SIZE_MAX      SIZE_T_MAX
84 # else
85 #  define SIZE_MAX      INT_MAX
86 # endif /* SIZE_T_MAX */
87 #endif /* SIZE_MAX */
88
89 /*
90  * Globals
91  */
92 extern int sudolineno, parse_error;
93 int errorlineno = -1;
94 int clearaliases = TRUE;
95 int printmatches = FALSE;
96 int pedantic = FALSE;
97 int keepall = FALSE;
98 int quiet = FALSE;
99 int used_runas = FALSE;
100
101 /*
102  * Alias types
103  */
104 #define HOST_ALIAS               1
105 #define CMND_ALIAS               2
106 #define USER_ALIAS               3
107 #define RUNAS_ALIAS              4
108
109 #define SETMATCH(_var, _val)    do { \
110         if ((_var) == UNSPEC || (_val) != NOMATCH) \
111             (_var) = (_val); \
112 } while (0)
113
114 #define SETNMATCH(_var, _val)   do { \
115         if ((_val) != NOMATCH) \
116             (_var) = ! (_val); \
117         else if ((_var) == UNSPEC) \
118             (_var) = NOMATCH; \
119 } while (0)
120
121 #define SETENV_RESET \
122         if (setenv_ok == IMPLIED) setenv_ok = def_setenv ? TRUE : UNSPEC
123
124 /*
125  * The matching stack, initial space allocated in init_parser().
126  */
127 struct matchstack *match;
128 int top = 0, stacksize = 0;
129
130 #define push \
131     do { \
132         if (top >= stacksize) { \
133             while ((stacksize += STACKINCREMENT) < top); \
134             match = (struct matchstack *) erealloc3(match, stacksize, sizeof(struct matchstack)); \
135         } \
136         match[top].user   = UNSPEC; \
137         match[top].cmnd   = UNSPEC; \
138         match[top].host   = UNSPEC; \
139         match[top].runas  = UNSPEC; \
140         match[top].nopass = def_authenticate ? UNSPEC : TRUE; \
141         match[top].noexec = def_noexec ? TRUE : UNSPEC; \
142         match[top].setenv = def_setenv ? TRUE : UNSPEC; \
143         match[top].role = NULL; \
144         match[top].type = NULL; \
145         top++; \
146     } while (0)
147
148 #define pushcp \
149     do { \
150         if (top >= stacksize) { \
151             while ((stacksize += STACKINCREMENT) < top); \
152             match = (struct matchstack *) erealloc3(match, stacksize, sizeof(struct matchstack)); \
153         } \
154         match[top].user   = match[top-1].user; \
155         match[top].cmnd   = match[top-1].cmnd; \
156         match[top].host   = match[top-1].host; \
157         match[top].runas  = match[top-1].runas; \
158         match[top].nopass = match[top-1].nopass; \
159         match[top].noexec = match[top-1].noexec; \
160         match[top].setenv = match[top-1].setenv; \
161         match[top].role   = estrdup(match[top-1].role); \
162         match[top].type   = estrdup(match[top-1].type); \
163         top++; \
164     } while (0)
165
166 #define pop \
167     do { \
168         if (top == 0) \
169             yyerror("matching stack underflow"); \
170         else { \
171             efree(match[top-1].role); \
172             efree(match[top-1].type); \
173             top--; \
174         } \
175     } while (0)
176
177
178 /*
179  * For testing if foo_matches variable was set to TRUE or FALSE
180  */
181 #define MATCHED(_v)     ((_v) >= 0)
182
183 /*
184  * Shortcuts for append()
185  */
186 #define append_cmnd(s, p) append(s, &cm_list[cm_list_len].cmnd, \
187         &cm_list[cm_list_len].cmnd_len, &cm_list[cm_list_len].cmnd_size, p)
188
189 #define append_runas(s, p) append(s, &cm_list[cm_list_len].runas, \
190         &cm_list[cm_list_len].runas_len, &cm_list[cm_list_len].runas_size, p)
191
192 #define append_role(s, p) append(s, &cm_list[cm_list_len].role, \
193         &cm_list[cm_list_len].role_len, &cm_list[cm_list_len].role_size, p)
194
195 #define append_type(s, p) append(s, &cm_list[cm_list_len].type, \
196         &cm_list[cm_list_len].type_len, &cm_list[cm_list_len].type_size, p)
197
198 #define append_entries(s, p) append(s, &ga_list[ga_list_len-1].entries, \
199         &ga_list[ga_list_len-1].entries_len, \
200         &ga_list[ga_list_len-1].entries_size, p)
201
202 /*
203  * The stack for printmatches.  A list of allowed commands for the user.
204  */
205 static struct command_match *cm_list = NULL;
206 static size_t cm_list_len = 0, cm_list_size = 0;
207
208 /*
209  * List of Cmnd_Aliases and expansions for `sudo -l'
210  */
211 static int in_alias = FALSE;
212 static size_t ga_list_len = 0, ga_list_size = 0;
213 static struct generic_alias *ga_list = NULL;
214
215 /*
216  * Does this Defaults list pertain to this user?
217  */
218 static int defaults_matches = FALSE;
219
220 /*
221  * Local protoypes
222  */
223 static int  add_alias           __P((char *, int, int));
224 static void append              __P((char *, char **, size_t *, size_t *, char *));
225 static void expand_ga_list      __P((void));
226 static void expand_match_list   __P((void));
227 static aliasinfo *find_alias    __P((char *, int));
228 static void more_aliases        __P((void));
229        void init_parser         __P((void));
230        void yyerror             __P((char *));
231
232 void
233 yyerror(s)
234     char *s;
235 {
236     /* Save the line the first error occurred on. */
237     if (errorlineno == -1)
238         errorlineno = sudolineno ? sudolineno - 1 : 0;
239     if (s && !quiet) {
240 #ifndef TRACELEXER
241         (void) fprintf(stderr, ">>> sudoers file: %s, line %d <<<\n", s,
242             sudolineno ? sudolineno - 1 : 0);
243 #else
244         (void) fprintf(stderr, "<*> ");
245 #endif
246     }
247     parse_error = TRUE;
248 }
249 %}
250
251 %union {
252     char *string;
253     int BOOLEAN;
254     struct sudo_command command;
255     int tok;
256     struct selinux_info seinfo;
257 }
258
259 %start file                             /* special start symbol */
260 %token <command> COMMAND                /* absolute pathname w/ optional args */
261 %token <string>  ALIAS                  /* an UPPERCASE alias name */
262 %token <string>  DEFVAR                 /* a Defaults variable name */
263 %token <string>  NTWKADDR               /* w.x.y.z or ipv6 address */
264 %token <string>  NETGROUP               /* a netgroup (+NAME) */
265 %token <string>  USERGROUP              /* a usergroup (%NAME) */
266 %token <string>  WORD                   /* a word */
267 %token <tok>     DEFAULTS               /* Defaults entry */
268 %token <tok>     DEFAULTS_HOST          /* Host-specific defaults entry */
269 %token <tok>     DEFAULTS_USER          /* User-specific defaults entry */
270 %token <tok>     DEFAULTS_RUNAS         /* Runas-specific defaults entry */
271 %token <tok>     RUNAS                  /* ( runas_list ) */
272 %token <tok>     NOPASSWD               /* no passwd req for command */
273 %token <tok>     PASSWD                 /* passwd req for command (default) */
274 %token <tok>     NOEXEC                 /* preload dummy execve() for cmnd */
275 %token <tok>     EXEC                   /* don't preload dummy execve() */
276 %token <tok>     SETENV                 /* user may set environment for cmnd */
277 %token <tok>     NOSETENV               /* user may not set environment */
278 %token <tok>     ALL                    /* ALL keyword */
279 %token <tok>     COMMENT                /* comment and/or carriage return */
280 %token <tok>     HOSTALIAS              /* Host_Alias keyword */
281 %token <tok>     CMNDALIAS              /* Cmnd_Alias keyword */
282 %token <tok>     USERALIAS              /* User_Alias keyword */
283 %token <tok>     RUNASALIAS             /* Runas_Alias keyword */
284 %token <tok>     ':' '=' ',' '!' '+' '-' /* union member tokens */
285 %token <tok>     ERROR
286 %token <tok>     TYPE                   /* SELinux type */
287 %token <tok>     ROLE                   /* SELinux role */
288
289 /*
290  * NOTE: these are not true booleans as there are actually 4 possible values:
291  *        1) TRUE (positive match)
292  *        0) FALSE (negative match due to a '!' somewhere)
293  *       -1) NOMATCH (don't change the value of *_matches)
294  *       -2) UNSPEC (uninitialized value)
295  */
296 %type <BOOLEAN>  cmnd
297 %type <BOOLEAN>  host
298 %type <BOOLEAN>  runasuser
299 %type <BOOLEAN>  oprunasuser
300 %type <BOOLEAN>  runaslist
301 %type <BOOLEAN>  user
302 %type <seinfo>   selinux
303 %type <string>   rolespec
304 %type <string>   typespec
305
306 %%
307
308 file            :       entry
309                 |       file entry
310                 ;
311
312 entry           :       COMMENT
313                             { ; }
314                 |       error COMMENT
315                             { yyerrok; }
316                 |       { push; } userlist privileges {
317                             while (top && user_matches != TRUE)
318                                 pop;
319                         }
320                 |       USERALIAS useraliases
321                             { ; }
322                 |       HOSTALIAS hostaliases
323                             { ; }
324                 |       CMNDALIAS cmndaliases
325                             { ; }
326                 |       RUNASALIAS runasaliases
327                             { ; }
328                 |       defaults_line
329                             { ; }
330                 ;
331
332 defaults_line   :       defaults_type defaults_list
333                 ;
334
335 defaults_type   :       DEFAULTS {
336                             defaults_matches = TRUE;
337                         }
338                 |       DEFAULTS_USER { push; } userlist {
339                             defaults_matches = user_matches;
340                             pop;
341                         }
342                 |       DEFAULTS_RUNAS { push; } runaslist {
343                             defaults_matches = $3 == TRUE;
344                             pop;
345                         }
346                 |       DEFAULTS_HOST { push; } hostlist {
347                             defaults_matches = host_matches;
348                             pop;
349                         }
350                 ;
351
352 defaults_list   :       defaults_entry
353                 |       defaults_entry ',' defaults_list
354                 ;
355
356 defaults_entry  :       DEFVAR {
357                             if (defaults_matches == TRUE &&
358                                 !set_default($1, NULL, TRUE)) {
359                                 yyerror(NULL);
360                                 YYERROR;
361                             }
362                             efree($1);
363                         }
364                 |       '!' DEFVAR {
365                             if (defaults_matches == TRUE &&
366                                 !set_default($2, NULL, FALSE)) {
367                                 yyerror(NULL);
368                                 YYERROR;
369                             }
370                             efree($2);
371                         }
372                 |       DEFVAR '=' WORD {
373                             if (defaults_matches == TRUE &&
374                                 !set_default($1, $3, TRUE)) {
375                                 yyerror(NULL);
376                                 YYERROR;
377                             }
378                             efree($1);
379                             efree($3);
380                         }
381                 |       DEFVAR '+' WORD {
382                             if (defaults_matches == TRUE &&
383                                 !set_default($1, $3, '+')) {
384                                 yyerror(NULL);
385                                 YYERROR;
386                             }
387                             efree($1);
388                             efree($3);
389                         }
390                 |       DEFVAR '-' WORD {
391                             if (defaults_matches == TRUE &&
392                                 !set_default($1, $3, '-')) {
393                                 yyerror(NULL);
394                                 YYERROR;
395                             }
396                             efree($1);
397                             efree($3);
398                         }
399                 ;
400
401 privileges      :       privilege
402                 |       privileges ':' privilege
403                 ;
404
405 privilege       :       hostlist '=' cmndspeclist {
406                             /*
407                              * We already did a push if necessary in
408                              * cmndspec so just reset some values so
409                              * the next 'privilege' gets a clean slate.
410                              */
411                             host_matches = UNSPEC;
412                             runas_matches = UNSPEC;
413                             no_passwd = def_authenticate ? UNSPEC : TRUE;
414                             no_execve = def_noexec ? TRUE : UNSPEC;
415                             setenv_ok = def_setenv ? TRUE : UNSPEC;
416 #ifdef HAVE_SELINUX
417                             efree(match[top-1].role);
418                             match[top-1].role = NULL;
419                             efree(match[top-1].type);
420                             match[top-1].type = NULL;
421 #endif
422                         }
423                 ;
424
425 ophost          :       host {
426                             SETMATCH(host_matches, $1);
427                         }
428                 |       '!' host {
429                             SETNMATCH(host_matches, $2);
430                         }
431                 ;
432
433 host            :       ALL {
434                             $$ = TRUE;
435                         }
436                 |       NTWKADDR {
437                             if (addr_matches($1))
438                                 $$ = TRUE;
439                             else
440                                 $$ = NOMATCH;
441                             efree($1);
442                         }
443                 |       NETGROUP {
444                             if (netgr_matches($1, user_host, user_shost, NULL))
445                                 $$ = TRUE;
446                             else
447                                 $$ = NOMATCH;
448                             efree($1);
449                         }
450                 |       WORD {
451                             if (hostname_matches(user_shost, user_host, $1) == 0)
452                                 $$ = TRUE;
453                             else
454                                 $$ = NOMATCH;
455                             efree($1);
456                         }
457                 |       ALIAS {
458                             aliasinfo *aip = find_alias($1, HOST_ALIAS);
459
460                             /* could be an all-caps hostname */
461                             if (aip)
462                                 $$ = aip->val;
463                             else if (strcasecmp(user_shost, $1) == 0)
464                                 $$ = TRUE;
465                             else {
466                                 if (pedantic) {
467                                     (void) fprintf(stderr,
468                                         "%s: undeclared Host_Alias `%s' referenced near line %d\n",
469                                         (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
470                                     if (pedantic > 1) {
471                                         yyerror(NULL);
472                                         YYERROR;
473                                     }
474                                 }
475                                 $$ = NOMATCH;
476                             }
477                             efree($1);
478                         }
479                 ;
480
481 cmndspeclist    :       cmndspec
482                 |       cmndspeclist ',' cmndspec
483                 ;
484
485 cmndspec        :       { SETENV_RESET; } runasspec selinux cmndtag opcmnd {
486 #ifdef HAVE_SELINUX
487                             /* Replace inherited role/type as needed. */
488                             if ($3.role != NULL) {
489                                 efree(match[top-1].role);
490                                 match[top-1].role = $3.role;
491                             }
492                             if ($3.type != NULL) {
493                                 efree(match[top-1].type);
494                                 match[top-1].type = $3.type;
495                             }
496 #endif
497                             /*
498                              * Push the entry onto the stack if it is worth
499                              * saving and reset cmnd_matches for next cmnd.
500                              *
501                              * We need to save at least one entry on
502                              * the stack so sudoers_lookup() can tell that
503                              * the user was listed in sudoers.  Also, we
504                              * need to be able to tell whether or not a
505                              * user was listed for this specific host.
506                              *
507                              * If keepall is set and the user matches then
508                              * we need to keep entries around too...
509                              */
510                             if (MATCHED(user_matches) &&
511                                 MATCHED(host_matches) &&
512                                 MATCHED(cmnd_matches) &&
513                                 MATCHED(runas_matches))
514                                 pushcp;
515                             else if (MATCHED(user_matches) && (top == 1 ||
516                                 (top == 2 && MATCHED(host_matches) &&
517                                 !MATCHED(match[0].host))))
518                                 pushcp;
519                             else if (user_matches == TRUE && keepall)
520                                 pushcp;
521
522                             cmnd_matches = UNSPEC;
523                         }
524                 ;
525
526 opcmnd          :       cmnd {
527                             SETMATCH(cmnd_matches, $1);
528                         }
529                 |       '!' {
530                             if (printmatches == TRUE) {
531                                 if (in_alias == TRUE)
532                                     append_entries("!", ", ");
533                                 else if (host_matches == TRUE &&
534                                     user_matches == TRUE)
535                                     append_cmnd("!", NULL);
536                             }
537                         } cmnd {
538                             SETNMATCH(cmnd_matches, $3);
539                         }
540                 ;
541
542 rolespec        :       ROLE '=' WORD {
543 #ifdef HAVE_SELINUX
544                             if (printmatches == TRUE && host_matches == TRUE &&
545                                 user_matches == TRUE && runas_matches == TRUE)
546                                 append_role($3, NULL);
547                             $$ = $3;
548 #else
549                             free($3);
550                             $$ = NULL;
551 #endif /* HAVE_SELINUX */
552                         }
553                 ;
554
555 typespec        :       TYPE '=' WORD {
556 #ifdef HAVE_SELINUX
557                             if (printmatches == TRUE && host_matches == TRUE &&
558                                 user_matches == TRUE && runas_matches == TRUE)
559                                 append_type($3, NULL);
560                             $$ = $3;
561 #else
562                             free($3);
563                             $$ = NULL;
564 #endif /* HAVE_SELINUX */
565                         }
566                 ;
567
568 selinux         :       /* empty */ {
569 #ifdef HAVE_SELINUX
570                             if (printmatches == TRUE && host_matches == TRUE &&
571                                 user_matches == TRUE && runas_matches == TRUE) {
572                                 /* Inherit role. */
573                                 cm_list[cm_list_len].role =
574                                     estrdup(cm_list[cm_list_len-1].role);
575                                 cm_list[cm_list_len].role_len =
576                                     cm_list[cm_list_len-1].role_len;
577                                 cm_list[cm_list_len].role_size =
578                                     cm_list[cm_list_len-1].role_len + 1;
579                                 /* Inherit type. */
580                                 cm_list[cm_list_len].type =
581                                     estrdup(cm_list[cm_list_len-1].type);
582                                 cm_list[cm_list_len].type_len =
583                                     cm_list[cm_list_len-1].type_len;
584                                 cm_list[cm_list_len].type_size =
585                                     cm_list[cm_list_len-1].type_len + 1;
586                             }
587 #endif /* HAVE_SELINUX */
588                             $$.role = NULL;
589                             $$.type = NULL;
590                         }
591                 |       rolespec {
592 #ifdef HAVE_SELINUX
593                             if (printmatches == TRUE && host_matches == TRUE &&
594                                 user_matches == TRUE && runas_matches == TRUE) {
595                                 /* Inherit type. */
596                                 cm_list[cm_list_len].type =
597                                     estrdup(cm_list[cm_list_len-1].type);
598                                 cm_list[cm_list_len].type_len =
599                                     cm_list[cm_list_len-1].type_len;
600                                 cm_list[cm_list_len].type_size =
601                                     cm_list[cm_list_len-1].type_len + 1;
602                             }
603 #endif /* HAVE_SELINUX */
604                             $$.role = $1;
605                             $$.type = NULL;
606                         }
607                 |       typespec {
608 #ifdef HAVE_SELINUX
609                             if (printmatches == TRUE && host_matches == TRUE &&
610                                 user_matches == TRUE && runas_matches == TRUE) {
611                                 /* Inherit role. */
612                                 cm_list[cm_list_len].role =
613                                     estrdup(cm_list[cm_list_len-1].role);
614                                 cm_list[cm_list_len].role_len =
615                                     cm_list[cm_list_len-1].role_len;
616                                 cm_list[cm_list_len].role_size =
617                                     cm_list[cm_list_len-1].role_len + 1;
618                             }
619 #endif /* HAVE_SELINUX */
620                             $$.type = $1;
621                             $$.role = NULL;
622                         }
623                 |       rolespec typespec {
624                             $$.role = $1;
625                             $$.type = $2;
626                         }
627                 |       typespec rolespec {
628                             $$.type = $1;
629                             $$.role = $2;
630                         }
631                 ;
632
633 runasspec       :       /* empty */ {
634                             if (printmatches == TRUE && host_matches == TRUE &&
635                                 user_matches == TRUE) {
636                                 if (runas_matches == UNSPEC) {
637                                     cm_list[cm_list_len].runas_len = 0;
638                                 } else {
639                                     /* Inherit runas data. */
640                                     cm_list[cm_list_len].runas =
641                                         estrdup(cm_list[cm_list_len-1].runas);
642                                     cm_list[cm_list_len].runas_len =
643                                         cm_list[cm_list_len-1].runas_len;
644                                     cm_list[cm_list_len].runas_size =
645                                         cm_list[cm_list_len-1].runas_len + 1;
646                                 }
647                             }
648                             /*
649                              * If this is the first entry in a command list
650                              * then check against default runas user.
651                              */
652                             if (runas_matches == UNSPEC) {
653                                 runas_matches = userpw_matches(def_runas_default,
654                                     *user_runas, runas_pw) ? TRUE : NOMATCH;
655                             }
656                         }
657                 |       RUNAS runaslist {
658                             runas_matches = $2;
659                         }
660                 ;
661
662 runaslist       :       oprunasuser { ; }
663                 |       runaslist ',' oprunasuser {
664                             /* Later entries override earlier ones. */
665                             if ($3 != NOMATCH)
666                                 $$ = $3;
667                             else
668                                 $$ = $1;
669                         }
670                 ;
671
672 oprunasuser     :       runasuser { ; }
673                 |       '!' {
674                             if (printmatches == TRUE) {
675                                 if (in_alias == TRUE)
676                                     append_entries("!", ", ");
677                                 else if (host_matches == TRUE &&
678                                     user_matches == TRUE)
679                                     append_runas("!", ", ");
680                             }
681                         } runasuser {
682                             /* Set $$ to the negation of runasuser */
683                             $$ = ($3 == NOMATCH ? NOMATCH : ! $3);
684                         }
685                 ;
686
687 runasuser       :       WORD {
688                             if (printmatches == TRUE) {
689                                 if (in_alias == TRUE)
690                                     append_entries($1, ", ");
691                                 else if (host_matches == TRUE &&
692                                     user_matches == TRUE)
693                                     append_runas($1, ", ");
694                             }
695                             if (userpw_matches($1, *user_runas, runas_pw))
696                                 $$ = TRUE;
697                             else
698                                 $$ = NOMATCH;
699                             efree($1);
700                             used_runas = TRUE;
701                         }
702                 |       USERGROUP {
703                             if (printmatches == TRUE) {
704                                 if (in_alias == TRUE)
705                                     append_entries($1, ", ");
706                                 else if (host_matches == TRUE &&
707                                     user_matches == TRUE)
708                                     append_runas($1, ", ");
709                             }
710                             if (usergr_matches($1, *user_runas, runas_pw))
711                                 $$ = TRUE;
712                             else
713                                 $$ = NOMATCH;
714                             efree($1);
715                             used_runas = TRUE;
716                         }
717                 |       NETGROUP {
718                             if (printmatches == TRUE) {
719                                 if (in_alias == TRUE)
720                                     append_entries($1, ", ");
721                                 else if (host_matches == TRUE &&
722                                     user_matches == TRUE)
723                                     append_runas($1, ", ");
724                             }
725                             if (netgr_matches($1, NULL, NULL, *user_runas))
726                                 $$ = TRUE;
727                             else
728                                 $$ = NOMATCH;
729                             efree($1);
730                             used_runas = TRUE;
731                         }
732                 |       ALIAS {
733                             aliasinfo *aip = find_alias($1, RUNAS_ALIAS);
734
735                             if (printmatches == TRUE) {
736                                 if (in_alias == TRUE)
737                                     append_entries($1, ", ");
738                                 else if (host_matches == TRUE &&
739                                     user_matches == TRUE)
740                                     append_runas($1, ", ");
741                             }
742                             /* could be an all-caps username */
743                             if (aip)
744                                 $$ = aip->val;
745                             else if (strcmp($1, *user_runas) == 0)
746                                 $$ = TRUE;
747                             else {
748                                 if (pedantic) {
749                                     (void) fprintf(stderr,
750                                         "%s: undeclared Runas_Alias `%s' referenced near line %d\n",
751                                         (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
752                                     if (pedantic > 1) {
753                                         yyerror(NULL);
754                                         YYERROR;
755                                     }
756                                 }
757                                 $$ = NOMATCH;
758                             }
759                             efree($1);
760                             used_runas = TRUE;
761                         }
762                 |       ALL {
763                             if (printmatches == TRUE) {
764                                 if (in_alias == TRUE)
765                                     append_entries("ALL", ", ");
766                                 else if (host_matches == TRUE &&
767                                     user_matches == TRUE)
768                                     append_runas("ALL", ", ");
769                             }
770                             $$ = TRUE;
771                         }
772                 ;
773
774 cmndtag         :       /* empty */ {
775                             /* Inherit {NO,}{PASSWD,EXEC,SETENV} status. */
776                             if (printmatches == TRUE && host_matches == TRUE &&
777                                 user_matches == TRUE) {
778                                 if (no_passwd == TRUE)
779                                     cm_list[cm_list_len].nopasswd = TRUE;
780                                 else
781                                     cm_list[cm_list_len].nopasswd = FALSE;
782                                 if (no_execve == TRUE)
783                                     cm_list[cm_list_len].noexecve = TRUE;
784                                 else
785                                     cm_list[cm_list_len].noexecve = FALSE;
786                                 if (setenv_ok == TRUE)
787                                     cm_list[cm_list_len].setenv = TRUE;
788                                 else
789                                     cm_list[cm_list_len].setenv = FALSE;
790                             }
791                         }
792                 |       cmndtag NOPASSWD {
793                             no_passwd = TRUE;
794                             if (printmatches == TRUE && host_matches == TRUE &&
795                                 user_matches == TRUE)
796                                 cm_list[cm_list_len].nopasswd = TRUE;
797                         }
798                 |       cmndtag PASSWD {
799                             no_passwd = FALSE;
800                             if (printmatches == TRUE && host_matches == TRUE &&
801                                 user_matches == TRUE)
802                                 cm_list[cm_list_len].nopasswd = FALSE;
803                         }
804                 |       cmndtag NOEXEC {
805                             no_execve = TRUE;
806                             if (printmatches == TRUE && host_matches == TRUE &&
807                                 user_matches == TRUE)
808                                 cm_list[cm_list_len].noexecve = TRUE;
809                         }
810                 |       cmndtag EXEC {
811                             no_execve = FALSE;
812                             if (printmatches == TRUE && host_matches == TRUE &&
813                                 user_matches == TRUE)
814                                 cm_list[cm_list_len].noexecve = FALSE;
815                         }
816                 |       cmndtag SETENV {
817                             setenv_ok = TRUE;
818                             if (printmatches == TRUE && host_matches == TRUE &&
819                                 user_matches == TRUE)
820                                 cm_list[cm_list_len].setenv = TRUE;
821                         }
822                 |       cmndtag NOSETENV {
823                             setenv_ok = FALSE;
824                             if (printmatches == TRUE && host_matches == TRUE &&
825                                 user_matches == TRUE)
826                                 cm_list[cm_list_len].setenv = FALSE;
827                         }
828                 ;
829
830 cmnd            :       ALL {
831                             if (printmatches == TRUE) {
832                                 if (in_alias == TRUE)
833                                     append_entries("ALL", ", ");
834                                 else if (host_matches == TRUE &&
835                                     user_matches == TRUE) {
836                                     append_cmnd("ALL", NULL);
837                                     expand_match_list();
838                                 }
839                             }
840                             /* sudo "ALL" implies the SETENV tag */
841                             if (setenv_ok == UNSPEC)
842                                 setenv_ok = IMPLIED;
843
844                             efree(safe_cmnd);
845                             safe_cmnd = NULL;
846                             $$ = TRUE;
847                         }
848                 |       ALIAS {
849                             aliasinfo *aip;
850
851                             if (printmatches == TRUE) {
852                                 if (in_alias == TRUE)
853                                     append_entries($1, ", ");
854                                 else if (host_matches == TRUE &&
855                                     user_matches == TRUE) {
856                                     append_cmnd($1, NULL);
857                                     expand_match_list();
858                                 }
859                             }
860
861                             if ((aip = find_alias($1, CMND_ALIAS)))
862                                 $$ = aip->val;
863                             else {
864                                 if (pedantic) {
865                                     (void) fprintf(stderr,
866                                         "%s: undeclared Cmnd_Alias `%s' referenced near line %d\n",
867                                         (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
868                                     if (pedantic > 1) {
869                                         yyerror(NULL);
870                                         YYERROR;
871                                     }
872                                 }
873                                 $$ = NOMATCH;
874                             }
875                             efree($1);
876                         }
877                 |        COMMAND {
878                             if (printmatches == TRUE) {
879                                 if (in_alias == TRUE) {
880                                     append_entries($1.cmnd, ", ");
881                                     if ($1.args)
882                                         append_entries($1.args, " ");
883                                 }
884                                 if (host_matches == TRUE &&
885                                     user_matches == TRUE)  {
886                                     append_cmnd($1.cmnd, NULL);
887                                     if ($1.args)
888                                         append_cmnd($1.args, " ");
889                                     expand_match_list();
890                                 }
891                             }
892
893                             if (command_matches($1.cmnd, $1.args))
894                                 $$ = TRUE;
895                             else
896                                 $$ = NOMATCH;
897
898                             efree($1.cmnd);
899                             efree($1.args);
900                         }
901                 ;
902
903 hostaliases     :       hostalias
904                 |       hostaliases ':' hostalias
905                 ;
906
907 hostalias       :       ALIAS { push; } '=' hostlist {
908                             if ((MATCHED(host_matches) || pedantic) &&
909                                 !add_alias($1, HOST_ALIAS, host_matches)) {
910                                 yyerror(NULL);
911                                 YYERROR;
912                             }
913                             pop;
914                         }
915                 ;
916
917 hostlist        :       ophost
918                 |       hostlist ',' ophost
919                 ;
920
921 cmndaliases     :       cmndalias
922                 |       cmndaliases ':' cmndalias
923                 ;
924
925 cmndalias       :       ALIAS {
926                             push;
927                             if (printmatches == TRUE) {
928                                 in_alias = TRUE;
929                                 /* Allocate space for ga_list if necessary. */
930                                 expand_ga_list();
931                                 ga_list[ga_list_len-1].type = CMND_ALIAS;
932                                 ga_list[ga_list_len-1].alias = estrdup($1);
933                              }
934                         } '=' cmndlist {
935                             if ((MATCHED(cmnd_matches) || pedantic) &&
936                                 !add_alias($1, CMND_ALIAS, cmnd_matches)) {
937                                 yyerror(NULL);
938                                 YYERROR;
939                             }
940                             pop;
941                             efree($1);
942
943                             if (printmatches == TRUE)
944                                 in_alias = FALSE;
945                         }
946                 ;
947
948 cmndlist        :       opcmnd { ; }
949                 |       cmndlist ',' opcmnd
950                 ;
951
952 runasaliases    :       runasalias
953                 |       runasaliases ':' runasalias
954                 ;
955
956 runasalias      :       ALIAS {
957                             if (printmatches == TRUE) {
958                                 in_alias = TRUE;
959                                 /* Allocate space for ga_list if necessary. */
960                                 expand_ga_list();
961                                 ga_list[ga_list_len-1].type = RUNAS_ALIAS;
962                                 ga_list[ga_list_len-1].alias = estrdup($1);
963                             }
964                         } '=' runaslist {
965                             if (($4 != NOMATCH || pedantic) &&
966                                 !add_alias($1, RUNAS_ALIAS, $4)) {
967                                 yyerror(NULL);
968                                 YYERROR;
969                             }
970                             efree($1);
971
972                             if (printmatches == TRUE)
973                                 in_alias = FALSE;
974                         }
975                 ;
976
977 useraliases     :       useralias
978                 |       useraliases ':' useralias
979                 ;
980
981 useralias       :       ALIAS { push; } '=' userlist {
982                             if ((MATCHED(user_matches) || pedantic) &&
983                                 !add_alias($1, USER_ALIAS, user_matches)) {
984                                 yyerror(NULL);
985                                 YYERROR;
986                             }
987                             pop;
988                             efree($1);
989                         }
990                 ;
991
992 userlist        :       opuser
993                 |       userlist ',' opuser
994                 ;
995
996 opuser          :       user {
997                             SETMATCH(user_matches, $1);
998                         }
999                 |       '!' user {
1000                             SETNMATCH(user_matches, $2);
1001                         }
1002                 ;
1003
1004 user            :       WORD {
1005                             if (userpw_matches($1, user_name, sudo_user.pw))
1006                                 $$ = TRUE;
1007                             else
1008                                 $$ = NOMATCH;
1009                             efree($1);
1010                         }
1011                 |       USERGROUP {
1012                             if (usergr_matches($1, user_name, sudo_user.pw))
1013                                 $$ = TRUE;
1014                             else
1015                                 $$ = NOMATCH;
1016                             efree($1);
1017                         }
1018                 |       NETGROUP {
1019                             if (netgr_matches($1, NULL, NULL, user_name))
1020                                 $$ = TRUE;
1021                             else
1022                                 $$ = NOMATCH;
1023                             efree($1);
1024                         }
1025                 |       ALIAS {
1026                             aliasinfo *aip = find_alias($1, USER_ALIAS);
1027
1028                             /* could be an all-caps username */
1029                             if (aip)
1030                                 $$ = aip->val;
1031                             else if (strcmp($1, user_name) == 0)
1032                                 $$ = TRUE;
1033                             else {
1034                                 if (pedantic) {
1035                                     (void) fprintf(stderr,
1036                                         "%s: undeclared User_Alias `%s' referenced near line %d\n",
1037                                         (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
1038                                     if (pedantic > 1) {
1039                                         yyerror(NULL);
1040                                         YYERROR;
1041                                     }
1042                                 }
1043                                 $$ = NOMATCH;
1044                             }
1045                             efree($1);
1046                         }
1047                 |       ALL {
1048                             $$ = TRUE;
1049                         }
1050                 ;
1051
1052 %%
1053
1054 #define MOREALIASES (32)
1055 aliasinfo *aliases = NULL;
1056 size_t naliases = 0;
1057 size_t nslots = 0;
1058
1059
1060 /*
1061  * Compare two aliasinfo structures, strcmp() style.
1062  * Note that we do *not* compare their values.
1063  */
1064 static int
1065 aliascmp(a1, a2)
1066     const VOID *a1, *a2;
1067 {
1068     int r;
1069     aliasinfo *ai1, *ai2;
1070
1071     ai1 = (aliasinfo *) a1;
1072     ai2 = (aliasinfo *) a2;
1073     if ((r = strcmp(ai1->name, ai2->name)) == 0)
1074         r = ai1->type - ai2->type;
1075
1076     return(r);
1077 }
1078
1079 /*
1080  * Compare two generic_alias structures, strcmp() style.
1081  */
1082 static int
1083 genaliascmp(entry, key)
1084     const VOID *entry, *key;
1085 {
1086     int r;
1087     struct generic_alias *ga1, *ga2;
1088
1089     ga1 = (struct generic_alias *) key;
1090     ga2 = (struct generic_alias *) entry;
1091     if ((r = strcmp(ga1->alias, ga2->alias)) == 0)
1092         r = ga1->type - ga2->type;
1093
1094     return(r);
1095 }
1096
1097
1098 /*
1099  * Adds the named alias of the specified type to the aliases list.
1100  */
1101 static int
1102 add_alias(alias, type, val)
1103     char *alias;
1104     int type;
1105     int val;
1106 {
1107     aliasinfo ai, *aip;
1108     size_t onaliases;
1109     char s[512];
1110
1111     if (naliases >= nslots)
1112         more_aliases();
1113
1114     ai.type = type;
1115     ai.val = val;
1116     ai.name = estrdup(alias);
1117     onaliases = naliases;
1118
1119     aip = (aliasinfo *) lsearch((VOID *)&ai, (VOID *)aliases, &naliases,
1120                                 sizeof(ai), aliascmp);
1121     if (aip == NULL) {
1122         (void) snprintf(s, sizeof(s), "Aliases corrupted defining alias `%s'",
1123                         alias);
1124         yyerror(s);
1125         return(FALSE);
1126     }
1127     if (onaliases == naliases) {
1128         (void) snprintf(s, sizeof(s), "Alias `%s' already defined", alias);
1129         yyerror(s);
1130         return(FALSE);
1131     }
1132
1133     return(TRUE);
1134 }
1135
1136 /*
1137  * Searches for the named alias of the specified type.
1138  */
1139 static aliasinfo *
1140 find_alias(alias, type)
1141     char *alias;
1142     int type;
1143 {
1144     aliasinfo ai;
1145
1146     ai.name = alias;
1147     ai.type = type;
1148
1149     return((aliasinfo *) lfind((VOID *)&ai, (VOID *)aliases, &naliases,
1150                  sizeof(ai), aliascmp));
1151 }
1152
1153 /*
1154  * Allocates more space for the aliases list.
1155  */
1156 static void
1157 more_aliases()
1158 {
1159
1160     nslots += MOREALIASES;
1161     aliases = (aliasinfo *) erealloc3(aliases, nslots, sizeof(aliasinfo));
1162 }
1163
1164 /*
1165  * Lists the contents of the aliases list.
1166  */
1167 void
1168 dumpaliases()
1169 {
1170     size_t n;
1171
1172     for (n = 0; n < naliases; n++) {
1173         if (aliases[n].val == -1)
1174             continue;
1175
1176         switch (aliases[n].type) {
1177         case HOST_ALIAS:
1178             (void) puts("HOST_ALIAS");
1179             break;
1180
1181         case CMND_ALIAS:
1182             (void) puts("CMND_ALIAS");
1183             break;
1184
1185         case USER_ALIAS:
1186             (void) puts("USER_ALIAS");
1187             break;
1188
1189         case RUNAS_ALIAS:
1190             (void) puts("RUNAS_ALIAS");
1191             break;
1192         }
1193         (void) printf("\t%s: %d\n", aliases[n].name, aliases[n].val);
1194     }
1195 }
1196
1197 /*
1198  * Lists the contents of cm_list and ga_list for `sudo -l'.
1199  */
1200 void
1201 list_matches()
1202 {
1203     size_t count;
1204     char *p;
1205     struct generic_alias *ga, key;
1206
1207     (void) printf("User %s may run the following commands on this host:\n",
1208         user_name);
1209     for (count = 0; count < cm_list_len; count++) {
1210
1211         /* Print the runas list. */
1212         (void) fputs("    ", stdout);
1213         if (cm_list[count].runas) {
1214             (void) putchar('(');
1215             p = strtok(cm_list[count].runas, ", ");
1216             do {
1217                 if (p != cm_list[count].runas)
1218                     (void) fputs(", ", stdout);
1219
1220                 key.alias = p;
1221                 key.type = RUNAS_ALIAS;
1222                 if ((ga = (struct generic_alias *) lfind((VOID *) &key,
1223                     (VOID *) &ga_list[0], &ga_list_len, sizeof(key), genaliascmp)))
1224                     (void) fputs(ga->entries, stdout);
1225                 else
1226                     (void) fputs(p, stdout);
1227             } while ((p = strtok(NULL, ", ")));
1228             (void) fputs(") ", stdout);
1229         } else {
1230             (void) printf("(%s) ", def_runas_default);
1231         }
1232
1233 #ifdef HAVE_SELINUX
1234         /* SELinux role and type */
1235         if (cm_list[count].role != NULL)
1236             (void) printf("ROLE=%s ", cm_list[count].role);
1237         if (cm_list[count].type != NULL)
1238             (void) printf("TYPE=%s ", cm_list[count].type);
1239 #endif
1240
1241         /* Is execve(2) disabled? */
1242         if (cm_list[count].noexecve == TRUE && !def_noexec)
1243             (void) fputs("NOEXEC: ", stdout);
1244         else if (cm_list[count].noexecve == FALSE && def_noexec)
1245             (void) fputs("EXEC: ", stdout);
1246
1247         /* Is a password required? */
1248         if (cm_list[count].nopasswd == TRUE && def_authenticate)
1249             (void) fputs("NOPASSWD: ", stdout);
1250         else if (cm_list[count].nopasswd == FALSE && !def_authenticate)
1251             (void) fputs("PASSWD: ", stdout);
1252
1253         /* Is setenv enabled? */
1254         if (cm_list[count].setenv == TRUE && !def_setenv)
1255             (void) fputs("SETENV: ", stdout);
1256         else if (cm_list[count].setenv == FALSE && def_setenv)
1257             (void) fputs("NOSETENV: ", stdout);
1258
1259         /* Print the actual command or expanded Cmnd_Alias. */
1260         key.alias = cm_list[count].cmnd;
1261         key.type = CMND_ALIAS;
1262         if ((ga = (struct generic_alias *) lfind((VOID *) &key,
1263             (VOID *) &ga_list[0], &ga_list_len, sizeof(key), genaliascmp)))
1264             (void) puts(ga->entries);
1265         else
1266             (void) puts(cm_list[count].cmnd);
1267     }
1268
1269     /* Be nice and free up space now that we are done. */
1270     for (count = 0; count < ga_list_len; count++) {
1271         efree(ga_list[count].alias);
1272         efree(ga_list[count].entries);
1273     }
1274     efree(ga_list);
1275     ga_list = NULL;
1276
1277     for (count = 0; count < cm_list_len; count++) {
1278         efree(cm_list[count].runas);
1279         efree(cm_list[count].cmnd);
1280         efree(cm_list[count].role);
1281         efree(cm_list[count].type);
1282     }
1283     efree(cm_list);
1284     cm_list = NULL;
1285     cm_list_len = 0;
1286     cm_list_size = 0;
1287 }
1288
1289 /*
1290  * Appends a source string to the destination, optionally prefixing a separator.
1291  */
1292 static void
1293 append(src, dstp, dst_len, dst_size, separator)
1294     char *src, **dstp;
1295     size_t *dst_len, *dst_size;
1296     char *separator;
1297 {
1298     size_t src_len = strlen(src);
1299     char *dst = *dstp;
1300
1301     /*
1302      * Only add the separator if there is something to separate from.
1303      * If the last char is a '!', don't apply the separator (XXX).
1304      */
1305     if (separator && dst && dst[*dst_len - 1] != '!')
1306         src_len += strlen(separator);
1307     else
1308         separator = NULL;
1309
1310     /* Assumes dst will be NULL if not set. */
1311     if (dst == NULL) {
1312         dst = (char *) emalloc(BUFSIZ);
1313         *dst = '\0';
1314         *dst_size = BUFSIZ;
1315         *dst_len = 0;
1316         *dstp = dst;
1317     }
1318
1319     /* Allocate more space if necessary. */
1320     if (*dst_size <= *dst_len + src_len) {
1321         while (*dst_size <= *dst_len + src_len)
1322             *dst_size += BUFSIZ;
1323
1324         dst = (char *) erealloc(dst, *dst_size);
1325         *dstp = dst;
1326     }
1327
1328     /* Copy src -> dst adding a separator if appropriate and adjust len. */
1329     if (separator)
1330         (void) strlcat(dst, separator, *dst_size);
1331     (void) strlcat(dst, src, *dst_size);
1332     *dst_len += src_len;
1333 }
1334
1335 /*
1336  * Frees up space used by the aliases list and resets the associated counters.
1337  */
1338 void
1339 reset_aliases()
1340 {
1341     size_t n;
1342
1343     if (aliases) {
1344         for (n = 0; n < naliases; n++)
1345             efree(aliases[n].name);
1346         efree(aliases);
1347         aliases = NULL;
1348     }
1349     naliases = nslots = 0;
1350 }
1351
1352 /*
1353  * Increments ga_list_len, allocating more space as necessary.
1354  */
1355 static void
1356 expand_ga_list()
1357 {
1358
1359     if (++ga_list_len >= ga_list_size) {
1360         while ((ga_list_size += STACKINCREMENT) < ga_list_len)
1361             ;
1362         ga_list = (struct generic_alias *)
1363             erealloc3(ga_list, ga_list_size, sizeof(struct generic_alias));
1364     }
1365
1366     ga_list[ga_list_len - 1].entries = NULL;
1367 }
1368
1369 /*
1370  * Increments cm_list_len, allocating more space as necessary.
1371  */
1372 static void
1373 expand_match_list()
1374 {
1375
1376     if (++cm_list_len >= cm_list_size) {
1377         while ((cm_list_size += STACKINCREMENT) < cm_list_len)
1378             ;
1379         if (cm_list == NULL)
1380             cm_list_len = 0;            /* start at 0 since it is a subscript */
1381         cm_list = (struct command_match *)
1382             erealloc3(cm_list, cm_list_size, sizeof(struct command_match));
1383     }
1384
1385     cm_list[cm_list_len].runas = cm_list[cm_list_len].cmnd = NULL;
1386     cm_list[cm_list_len].type = cm_list[cm_list_len].role = NULL;
1387     cm_list[cm_list_len].nopasswd = FALSE;
1388     cm_list[cm_list_len].noexecve = FALSE;
1389     cm_list[cm_list_len].setenv = FALSE;
1390 }
1391
1392 /*
1393  * Frees up spaced used by a previous parser run and allocates new space
1394  * for various data structures.
1395  */
1396 void
1397 init_parser()
1398 {
1399
1400     /* Free up old data structures if we run the parser more than once. */
1401     if (match) {
1402         efree(match);
1403         match = NULL;
1404         top = 0;
1405         parse_error = FALSE;
1406         used_runas = FALSE;
1407         errorlineno = -1;
1408         sudolineno = 1;
1409     }
1410
1411     /* Allocate space for the matching stack. */
1412     stacksize = STACKINCREMENT;
1413     match = (struct matchstack *) emalloc2(stacksize, sizeof(struct matchstack));
1414
1415     /* Allocate space for the match list (for `sudo -l'). */
1416     if (printmatches == TRUE)
1417         expand_match_list();
1418 }