3 * Copyright (c) 1996, 1998-2004, 2007
4 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
26 * XXX - the whole opFOO naming thing is somewhat bogus.
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.
35 #include <sys/types.h>
36 #include <sys/param.h>
45 #endif /* STDC_HEADERS */
49 # ifdef HAVE_STRINGS_H
52 #endif /* HAVE_STRING_H */
55 #endif /* HAVE_UNISTD_H */
57 #if defined(YYBISON) && defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
59 #endif /* YYBISON && HAVE_ALLOCA_H && !__GNUC__ */
62 #endif /* HAVE_LSEARCH */
69 #include "emul/search.h"
70 #endif /* HAVE_LSEARCH */
73 __unused static const char rcsid[] = "$Sudo: parse.yacc,v 1.204.2.13 2008/02/27 20:34:42 millert Exp $";
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).
83 # define SIZE_MAX SIZE_T_MAX
85 # define SIZE_MAX INT_MAX
86 # endif /* SIZE_T_MAX */
92 extern int sudolineno, parse_error;
94 int clearaliases = TRUE;
95 int printmatches = FALSE;
99 int used_runas = FALSE;
107 #define RUNAS_ALIAS 4
109 #define SETMATCH(_var, _val) do { \
110 if ((_var) == UNSPEC || (_val) != NOMATCH) \
114 #define SETNMATCH(_var, _val) do { \
115 if ((_val) != NOMATCH) \
117 else if ((_var) == UNSPEC) \
121 #define SETENV_RESET \
122 if (setenv_ok == IMPLIED) setenv_ok = def_setenv ? TRUE : UNSPEC
125 * The matching stack, initial space allocated in init_parser().
127 struct matchstack *match;
128 int top = 0, stacksize = 0;
132 if (top >= stacksize) { \
133 while ((stacksize += STACKINCREMENT) < top); \
134 match = (struct matchstack *) erealloc3(match, stacksize, sizeof(struct matchstack)); \
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; \
150 if (top >= stacksize) { \
151 while ((stacksize += STACKINCREMENT) < top); \
152 match = (struct matchstack *) erealloc3(match, stacksize, sizeof(struct matchstack)); \
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); \
169 yyerror("matching stack underflow"); \
171 efree(match[top-1].role); \
172 efree(match[top-1].type); \
179 * For testing if foo_matches variable was set to TRUE or FALSE
181 #define MATCHED(_v) ((_v) >= 0)
184 * Shortcuts for append()
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)
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)
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)
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)
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)
203 * The stack for printmatches. A list of allowed commands for the user.
205 static struct command_match *cm_list = NULL;
206 static size_t cm_list_len = 0, cm_list_size = 0;
209 * List of Cmnd_Aliases and expansions for `sudo -l'
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;
216 * Does this Defaults list pertain to this user?
218 static int defaults_matches = FALSE;
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 *));
236 /* Save the line the first error occurred on. */
237 if (errorlineno == -1)
238 errorlineno = sudolineno ? sudolineno - 1 : 0;
241 (void) fprintf(stderr, ">>> sudoers file: %s, line %d <<<\n", s,
242 sudolineno ? sudolineno - 1 : 0);
244 (void) fprintf(stderr, "<*> ");
254 struct sudo_command command;
256 struct selinux_info seinfo;
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 */
286 %token <tok> TYPE /* SELinux type */
287 %token <tok> ROLE /* SELinux role */
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)
298 %type <BOOLEAN> runasuser
299 %type <BOOLEAN> oprunasuser
300 %type <BOOLEAN> runaslist
302 %type <seinfo> selinux
303 %type <string> rolespec
304 %type <string> typespec
316 | { push; } userlist privileges {
317 while (top && user_matches != TRUE)
320 | USERALIAS useraliases
322 | HOSTALIAS hostaliases
324 | CMNDALIAS cmndaliases
326 | RUNASALIAS runasaliases
332 defaults_line : defaults_type defaults_list
335 defaults_type : DEFAULTS {
336 defaults_matches = TRUE;
338 | DEFAULTS_USER { push; } userlist {
339 defaults_matches = user_matches;
342 | DEFAULTS_RUNAS { push; } runaslist {
343 defaults_matches = $3 == TRUE;
346 | DEFAULTS_HOST { push; } hostlist {
347 defaults_matches = host_matches;
352 defaults_list : defaults_entry
353 | defaults_entry ',' defaults_list
356 defaults_entry : DEFVAR {
357 if (defaults_matches == TRUE &&
358 !set_default($1, NULL, TRUE)) {
365 if (defaults_matches == TRUE &&
366 !set_default($2, NULL, FALSE)) {
373 if (defaults_matches == TRUE &&
374 !set_default($1, $3, TRUE)) {
382 if (defaults_matches == TRUE &&
383 !set_default($1, $3, '+')) {
391 if (defaults_matches == TRUE &&
392 !set_default($1, $3, '-')) {
401 privileges : privilege
402 | privileges ':' privilege
405 privilege : hostlist '=' cmndspeclist {
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.
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;
417 efree(match[top-1].role);
418 match[top-1].role = NULL;
419 efree(match[top-1].type);
420 match[top-1].type = NULL;
426 SETMATCH(host_matches, $1);
429 SETNMATCH(host_matches, $2);
437 if (addr_matches($1))
444 if (netgr_matches($1, user_host, user_shost, NULL))
451 if (hostname_matches(user_shost, user_host, $1) == 0)
458 aliasinfo *aip = find_alias($1, HOST_ALIAS);
460 /* could be an all-caps hostname */
463 else if (strcasecmp(user_shost, $1) == 0)
467 (void) fprintf(stderr,
468 "%s: undeclared Host_Alias `%s' referenced near line %d\n",
469 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
481 cmndspeclist : cmndspec
482 | cmndspeclist ',' cmndspec
485 cmndspec : { SETENV_RESET; } runasspec selinux cmndtag opcmnd {
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;
492 if ($3.type != NULL) {
493 efree(match[top-1].type);
494 match[top-1].type = $3.type;
498 * Push the entry onto the stack if it is worth
499 * saving and reset cmnd_matches for next cmnd.
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.
507 * If keepall is set and the user matches then
508 * we need to keep entries around too...
510 if (MATCHED(user_matches) &&
511 MATCHED(host_matches) &&
512 MATCHED(cmnd_matches) &&
513 MATCHED(runas_matches))
515 else if (MATCHED(user_matches) && (top == 1 ||
516 (top == 2 && MATCHED(host_matches) &&
517 !MATCHED(match[0].host))))
519 else if (user_matches == TRUE && keepall)
522 cmnd_matches = UNSPEC;
527 SETMATCH(cmnd_matches, $1);
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);
538 SETNMATCH(cmnd_matches, $3);
542 rolespec : ROLE '=' WORD {
544 if (printmatches == TRUE && host_matches == TRUE &&
545 user_matches == TRUE && runas_matches == TRUE)
546 append_role($3, NULL);
551 #endif /* HAVE_SELINUX */
555 typespec : TYPE '=' WORD {
557 if (printmatches == TRUE && host_matches == TRUE &&
558 user_matches == TRUE && runas_matches == TRUE)
559 append_type($3, NULL);
564 #endif /* HAVE_SELINUX */
568 selinux : /* empty */ {
570 if (printmatches == TRUE && host_matches == TRUE &&
571 user_matches == TRUE && runas_matches == TRUE) {
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;
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;
587 #endif /* HAVE_SELINUX */
593 if (printmatches == TRUE && host_matches == TRUE &&
594 user_matches == TRUE && runas_matches == TRUE) {
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;
603 #endif /* HAVE_SELINUX */
609 if (printmatches == TRUE && host_matches == TRUE &&
610 user_matches == TRUE && runas_matches == TRUE) {
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;
619 #endif /* HAVE_SELINUX */
623 | rolespec typespec {
627 | typespec rolespec {
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;
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;
649 * If this is the first entry in a command list
650 * then check against default runas user.
652 if (runas_matches == UNSPEC) {
653 runas_matches = userpw_matches(def_runas_default,
654 *user_runas, runas_pw) ? TRUE : NOMATCH;
662 runaslist : oprunasuser { ; }
663 | runaslist ',' oprunasuser {
664 /* Later entries override earlier ones. */
672 oprunasuser : runasuser { ; }
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("!", ", ");
682 /* Set $$ to the negation of runasuser */
683 $$ = ($3 == NOMATCH ? NOMATCH : ! $3);
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, ", ");
695 if (userpw_matches($1, *user_runas, runas_pw))
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, ", ");
710 if (usergr_matches($1, *user_runas, runas_pw))
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, ", ");
725 if (netgr_matches($1, NULL, NULL, *user_runas))
733 aliasinfo *aip = find_alias($1, RUNAS_ALIAS);
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, ", ");
742 /* could be an all-caps username */
745 else if (strcmp($1, *user_runas) == 0)
749 (void) fprintf(stderr,
750 "%s: undeclared Runas_Alias `%s' referenced near line %d\n",
751 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
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", ", ");
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;
781 cm_list[cm_list_len].nopasswd = FALSE;
782 if (no_execve == TRUE)
783 cm_list[cm_list_len].noexecve = TRUE;
785 cm_list[cm_list_len].noexecve = FALSE;
786 if (setenv_ok == TRUE)
787 cm_list[cm_list_len].setenv = TRUE;
789 cm_list[cm_list_len].setenv = FALSE;
794 if (printmatches == TRUE && host_matches == TRUE &&
795 user_matches == TRUE)
796 cm_list[cm_list_len].nopasswd = TRUE;
800 if (printmatches == TRUE && host_matches == TRUE &&
801 user_matches == TRUE)
802 cm_list[cm_list_len].nopasswd = FALSE;
806 if (printmatches == TRUE && host_matches == TRUE &&
807 user_matches == TRUE)
808 cm_list[cm_list_len].noexecve = TRUE;
812 if (printmatches == TRUE && host_matches == TRUE &&
813 user_matches == TRUE)
814 cm_list[cm_list_len].noexecve = FALSE;
818 if (printmatches == TRUE && host_matches == TRUE &&
819 user_matches == TRUE)
820 cm_list[cm_list_len].setenv = TRUE;
824 if (printmatches == TRUE && host_matches == TRUE &&
825 user_matches == TRUE)
826 cm_list[cm_list_len].setenv = FALSE;
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);
840 /* sudo "ALL" implies the SETENV tag */
841 if (setenv_ok == UNSPEC)
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);
861 if ((aip = find_alias($1, CMND_ALIAS)))
865 (void) fprintf(stderr,
866 "%s: undeclared Cmnd_Alias `%s' referenced near line %d\n",
867 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
878 if (printmatches == TRUE) {
879 if (in_alias == TRUE) {
880 append_entries($1.cmnd, ", ");
882 append_entries($1.args, " ");
884 if (host_matches == TRUE &&
885 user_matches == TRUE) {
886 append_cmnd($1.cmnd, NULL);
888 append_cmnd($1.args, " ");
893 if (command_matches($1.cmnd, $1.args))
903 hostaliases : hostalias
904 | hostaliases ':' hostalias
907 hostalias : ALIAS { push; } '=' hostlist {
908 if ((MATCHED(host_matches) || pedantic) &&
909 !add_alias($1, HOST_ALIAS, host_matches)) {
918 | hostlist ',' ophost
921 cmndaliases : cmndalias
922 | cmndaliases ':' cmndalias
927 if (printmatches == TRUE) {
929 /* Allocate space for ga_list if necessary. */
931 ga_list[ga_list_len-1].type = CMND_ALIAS;
932 ga_list[ga_list_len-1].alias = estrdup($1);
935 if ((MATCHED(cmnd_matches) || pedantic) &&
936 !add_alias($1, CMND_ALIAS, cmnd_matches)) {
943 if (printmatches == TRUE)
948 cmndlist : opcmnd { ; }
949 | cmndlist ',' opcmnd
952 runasaliases : runasalias
953 | runasaliases ':' runasalias
957 if (printmatches == TRUE) {
959 /* Allocate space for ga_list if necessary. */
961 ga_list[ga_list_len-1].type = RUNAS_ALIAS;
962 ga_list[ga_list_len-1].alias = estrdup($1);
965 if (($4 != NOMATCH || pedantic) &&
966 !add_alias($1, RUNAS_ALIAS, $4)) {
972 if (printmatches == TRUE)
977 useraliases : useralias
978 | useraliases ':' useralias
981 useralias : ALIAS { push; } '=' userlist {
982 if ((MATCHED(user_matches) || pedantic) &&
983 !add_alias($1, USER_ALIAS, user_matches)) {
993 | userlist ',' opuser
997 SETMATCH(user_matches, $1);
1000 SETNMATCH(user_matches, $2);
1005 if (userpw_matches($1, user_name, sudo_user.pw))
1012 if (usergr_matches($1, user_name, sudo_user.pw))
1019 if (netgr_matches($1, NULL, NULL, user_name))
1026 aliasinfo *aip = find_alias($1, USER_ALIAS);
1028 /* could be an all-caps username */
1031 else if (strcmp($1, user_name) == 0)
1035 (void) fprintf(stderr,
1036 "%s: undeclared User_Alias `%s' referenced near line %d\n",
1037 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
1054 #define MOREALIASES (32)
1055 aliasinfo *aliases = NULL;
1056 size_t naliases = 0;
1061 * Compare two aliasinfo structures, strcmp() style.
1062 * Note that we do *not* compare their values.
1066 const VOID *a1, *a2;
1069 aliasinfo *ai1, *ai2;
1071 ai1 = (aliasinfo *) a1;
1072 ai2 = (aliasinfo *) a2;
1073 if ((r = strcmp(ai1->name, ai2->name)) == 0)
1074 r = ai1->type - ai2->type;
1080 * Compare two generic_alias structures, strcmp() style.
1083 genaliascmp(entry, key)
1084 const VOID *entry, *key;
1087 struct generic_alias *ga1, *ga2;
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;
1099 * Adds the named alias of the specified type to the aliases list.
1102 add_alias(alias, type, val)
1111 if (naliases >= nslots)
1116 ai.name = estrdup(alias);
1117 onaliases = naliases;
1119 aip = (aliasinfo *) lsearch((VOID *)&ai, (VOID *)aliases, &naliases,
1120 sizeof(ai), aliascmp);
1122 (void) snprintf(s, sizeof(s), "Aliases corrupted defining alias `%s'",
1127 if (onaliases == naliases) {
1128 (void) snprintf(s, sizeof(s), "Alias `%s' already defined", alias);
1137 * Searches for the named alias of the specified type.
1140 find_alias(alias, type)
1149 return((aliasinfo *) lfind((VOID *)&ai, (VOID *)aliases, &naliases,
1150 sizeof(ai), aliascmp));
1154 * Allocates more space for the aliases list.
1160 nslots += MOREALIASES;
1161 aliases = (aliasinfo *) erealloc3(aliases, nslots, sizeof(aliasinfo));
1165 * Lists the contents of the aliases list.
1172 for (n = 0; n < naliases; n++) {
1173 if (aliases[n].val == -1)
1176 switch (aliases[n].type) {
1178 (void) puts("HOST_ALIAS");
1182 (void) puts("CMND_ALIAS");
1186 (void) puts("USER_ALIAS");
1190 (void) puts("RUNAS_ALIAS");
1193 (void) printf("\t%s: %d\n", aliases[n].name, aliases[n].val);
1198 * Lists the contents of cm_list and ga_list for `sudo -l'.
1205 struct generic_alias *ga, key;
1207 (void) printf("User %s may run the following commands on this host:\n",
1209 for (count = 0; count < cm_list_len; count++) {
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, ", ");
1217 if (p != cm_list[count].runas)
1218 (void) fputs(", ", stdout);
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);
1226 (void) fputs(p, stdout);
1227 } while ((p = strtok(NULL, ", ")));
1228 (void) fputs(") ", stdout);
1230 (void) printf("(%s) ", def_runas_default);
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);
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);
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);
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);
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);
1266 (void) puts(cm_list[count].cmnd);
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);
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);
1290 * Appends a source string to the destination, optionally prefixing a separator.
1293 append(src, dstp, dst_len, dst_size, separator)
1295 size_t *dst_len, *dst_size;
1298 size_t src_len = strlen(src);
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).
1305 if (separator && dst && dst[*dst_len - 1] != '!')
1306 src_len += strlen(separator);
1310 /* Assumes dst will be NULL if not set. */
1312 dst = (char *) emalloc(BUFSIZ);
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;
1324 dst = (char *) erealloc(dst, *dst_size);
1328 /* Copy src -> dst adding a separator if appropriate and adjust len. */
1330 (void) strlcat(dst, separator, *dst_size);
1331 (void) strlcat(dst, src, *dst_size);
1332 *dst_len += src_len;
1336 * Frees up space used by the aliases list and resets the associated counters.
1344 for (n = 0; n < naliases; n++)
1345 efree(aliases[n].name);
1349 naliases = nslots = 0;
1353 * Increments ga_list_len, allocating more space as necessary.
1359 if (++ga_list_len >= ga_list_size) {
1360 while ((ga_list_size += STACKINCREMENT) < ga_list_len)
1362 ga_list = (struct generic_alias *)
1363 erealloc3(ga_list, ga_list_size, sizeof(struct generic_alias));
1366 ga_list[ga_list_len - 1].entries = NULL;
1370 * Increments cm_list_len, allocating more space as necessary.
1376 if (++cm_list_len >= cm_list_size) {
1377 while ((cm_list_size += STACKINCREMENT) < cm_list_len)
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));
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;
1393 * Frees up spaced used by a previous parser run and allocates new space
1394 * for various data structures.
1400 /* Free up old data structures if we run the parser more than once. */
1405 parse_error = FALSE;
1411 /* Allocate space for the matching stack. */
1412 stacksize = STACKINCREMENT;
1413 match = (struct matchstack *) emalloc2(stacksize, sizeof(struct matchstack));
1415 /* Allocate space for the match list (for `sudo -l'). */
1416 if (printmatches == TRUE)
1417 expand_match_list();