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.10 2008/01/16 23:20:53 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; \
148 if (top >= stacksize) { \
149 while ((stacksize += STACKINCREMENT) < top); \
150 match = (struct matchstack *) erealloc3(match, stacksize, sizeof(struct matchstack)); \
152 match[top].user = match[top-1].user; \
153 match[top].cmnd = match[top-1].cmnd; \
154 match[top].host = match[top-1].host; \
155 match[top].runas = match[top-1].runas; \
156 match[top].nopass = match[top-1].nopass; \
157 match[top].noexec = match[top-1].noexec; \
158 match[top].setenv = match[top-1].setenv; \
165 yyerror("matching stack underflow"); \
172 * For testing if foo_matches variable was set to TRUE or FALSE
174 #define MATCHED(_v) ((_v) >= 0)
177 * Shortcuts for append()
179 #define append_cmnd(s, p) append(s, &cm_list[cm_list_len].cmnd, \
180 &cm_list[cm_list_len].cmnd_len, &cm_list[cm_list_len].cmnd_size, p)
182 #define append_runas(s, p) append(s, &cm_list[cm_list_len].runas, \
183 &cm_list[cm_list_len].runas_len, &cm_list[cm_list_len].runas_size, p)
185 #define append_entries(s, p) append(s, &ga_list[ga_list_len-1].entries, \
186 &ga_list[ga_list_len-1].entries_len, \
187 &ga_list[ga_list_len-1].entries_size, p)
190 * The stack for printmatches. A list of allowed commands for the user.
192 static struct command_match *cm_list = NULL;
193 static size_t cm_list_len = 0, cm_list_size = 0;
196 * List of Cmnd_Aliases and expansions for `sudo -l'
198 static int in_alias = FALSE;
199 static size_t ga_list_len = 0, ga_list_size = 0;
200 static struct generic_alias *ga_list = NULL;
203 * Does this Defaults list pertain to this user?
205 static int defaults_matches = FALSE;
210 static int add_alias __P((char *, int, int));
211 static void append __P((char *, char **, size_t *, size_t *, char *));
212 static void expand_ga_list __P((void));
213 static void expand_match_list __P((void));
214 static aliasinfo *find_alias __P((char *, int));
215 static void more_aliases __P((void));
216 void init_parser __P((void));
217 void yyerror __P((char *));
223 /* Save the line the first error occurred on. */
224 if (errorlineno == -1)
225 errorlineno = sudolineno ? sudolineno - 1 : 0;
228 (void) fprintf(stderr, ">>> sudoers file: %s, line %d <<<\n", s,
229 sudolineno ? sudolineno - 1 : 0);
231 (void) fprintf(stderr, "<*> ");
241 struct sudo_command command;
245 %start file /* special start symbol */
246 %token <command> COMMAND /* absolute pathname w/ optional args */
247 %token <string> ALIAS /* an UPPERCASE alias name */
248 %token <string> DEFVAR /* a Defaults variable name */
249 %token <string> NTWKADDR /* w.x.y.z or ipv6 address */
250 %token <string> NETGROUP /* a netgroup (+NAME) */
251 %token <string> USERGROUP /* a usergroup (%NAME) */
252 %token <string> WORD /* a word */
253 %token <tok> DEFAULTS /* Defaults entry */
254 %token <tok> DEFAULTS_HOST /* Host-specific defaults entry */
255 %token <tok> DEFAULTS_USER /* User-specific defaults entry */
256 %token <tok> DEFAULTS_RUNAS /* Runas-specific defaults entry */
257 %token <tok> RUNAS /* ( runas_list ) */
258 %token <tok> NOPASSWD /* no passwd req for command */
259 %token <tok> PASSWD /* passwd req for command (default) */
260 %token <tok> NOEXEC /* preload dummy execve() for cmnd */
261 %token <tok> EXEC /* don't preload dummy execve() */
262 %token <tok> SETENV /* user may set environment for cmnd */
263 %token <tok> NOSETENV /* user may not set environment */
264 %token <tok> ALL /* ALL keyword */
265 %token <tok> COMMENT /* comment and/or carriage return */
266 %token <tok> HOSTALIAS /* Host_Alias keyword */
267 %token <tok> CMNDALIAS /* Cmnd_Alias keyword */
268 %token <tok> USERALIAS /* User_Alias keyword */
269 %token <tok> RUNASALIAS /* Runas_Alias keyword */
270 %token <tok> ':' '=' ',' '!' '+' '-' /* union member tokens */
274 * NOTE: these are not true booleans as there are actually 4 possible values:
275 * 1) TRUE (positive match)
276 * 0) FALSE (negative match due to a '!' somewhere)
277 * -1) NOMATCH (don't change the value of *_matches)
278 * -2) UNSPEC (uninitialized value)
282 %type <BOOLEAN> runasuser
283 %type <BOOLEAN> oprunasuser
284 %type <BOOLEAN> runaslist
297 | { push; } userlist privileges {
298 while (top && user_matches != TRUE)
301 | USERALIAS useraliases
303 | HOSTALIAS hostaliases
305 | CMNDALIAS cmndaliases
307 | RUNASALIAS runasaliases
313 defaults_line : defaults_type defaults_list
316 defaults_type : DEFAULTS {
317 defaults_matches = TRUE;
319 | DEFAULTS_USER { push; } userlist {
320 defaults_matches = user_matches;
323 | DEFAULTS_RUNAS { push; } runaslist {
324 defaults_matches = $3 == TRUE;
327 | DEFAULTS_HOST { push; } hostlist {
328 defaults_matches = host_matches;
333 defaults_list : defaults_entry
334 | defaults_entry ',' defaults_list
337 defaults_entry : DEFVAR {
338 if (defaults_matches == TRUE &&
339 !set_default($1, NULL, TRUE)) {
346 if (defaults_matches == TRUE &&
347 !set_default($2, NULL, FALSE)) {
354 if (defaults_matches == TRUE &&
355 !set_default($1, $3, TRUE)) {
363 if (defaults_matches == TRUE &&
364 !set_default($1, $3, '+')) {
372 if (defaults_matches == TRUE &&
373 !set_default($1, $3, '-')) {
382 privileges : privilege
383 | privileges ':' privilege
386 privilege : hostlist '=' cmndspeclist {
388 * We already did a push if necessary in
389 * cmndspec so just reset some values so
390 * the next 'privilege' gets a clean slate.
392 host_matches = UNSPEC;
393 runas_matches = UNSPEC;
394 no_passwd = def_authenticate ? UNSPEC : TRUE;
395 no_execve = def_noexec ? TRUE : UNSPEC;
396 setenv_ok = def_setenv ? TRUE : UNSPEC;
401 SETMATCH(host_matches, $1);
404 SETNMATCH(host_matches, $2);
412 if (addr_matches($1))
420 if (netgr_matches($1, user_host, user_shost, NULL))
428 if (hostname_matches(user_shost, user_host, $1) == 0)
435 aliasinfo *aip = find_alias($1, HOST_ALIAS);
438 /* could be an all-caps hostname */
441 else if (strcasecmp(user_shost, $1) == 0)
445 (void) fprintf(stderr,
446 "%s: undeclared Host_Alias `%s' referenced near line %d\n",
447 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
459 cmndspeclist : cmndspec
460 | cmndspeclist ',' cmndspec
463 cmndspec : { SETENV_RESET; } runasspec cmndtag opcmnd {
465 * Push the entry onto the stack if it is worth
466 * saving and reset cmnd_matches for next cmnd.
468 * We need to save at least one entry on
469 * the stack so sudoers_lookup() can tell that
470 * the user was listed in sudoers. Also, we
471 * need to be able to tell whether or not a
472 * user was listed for this specific host.
474 * If keepall is set and the user matches then
475 * we need to keep entries around too...
477 if (MATCHED(user_matches) &&
478 MATCHED(host_matches) &&
479 MATCHED(cmnd_matches) &&
480 MATCHED(runas_matches))
482 else if (MATCHED(user_matches) && (top == 1 ||
483 (top == 2 && MATCHED(host_matches) &&
484 !MATCHED(match[0].host))))
486 else if (user_matches == TRUE && keepall)
488 cmnd_matches = UNSPEC;
493 SETMATCH(cmnd_matches, $1);
496 if (printmatches == TRUE) {
497 if (in_alias == TRUE)
498 append_entries("!", ", ");
499 else if (host_matches == TRUE &&
500 user_matches == TRUE)
501 append_cmnd("!", NULL);
504 SETNMATCH(cmnd_matches, $3);
508 runasspec : /* empty */ {
509 if (printmatches == TRUE && host_matches == TRUE &&
510 user_matches == TRUE) {
511 if (runas_matches == UNSPEC) {
512 cm_list[cm_list_len].runas_len = 0;
514 /* Inherit runas data. */
515 cm_list[cm_list_len].runas =
516 estrdup(cm_list[cm_list_len-1].runas);
517 cm_list[cm_list_len].runas_len =
518 cm_list[cm_list_len-1].runas_len;
519 cm_list[cm_list_len].runas_size =
520 cm_list[cm_list_len-1].runas_size;
524 * If this is the first entry in a command list
525 * then check against default runas user.
527 if (runas_matches == UNSPEC) {
528 runas_matches = userpw_matches(def_runas_default,
529 *user_runas, runas_pw) ? TRUE : NOMATCH;
537 runaslist : oprunasuser { ; }
538 | runaslist ',' oprunasuser {
539 /* Later entries override earlier ones. */
547 oprunasuser : runasuser { ; }
549 if (printmatches == TRUE) {
550 if (in_alias == TRUE)
551 append_entries("!", ", ");
552 else if (host_matches == TRUE &&
553 user_matches == TRUE)
554 append_runas("!", ", ");
557 /* Set $$ to the negation of runasuser */
558 $$ = ($3 == NOMATCH ? NOMATCH : ! $3);
563 if (printmatches == TRUE) {
564 if (in_alias == TRUE)
565 append_entries($1, ", ");
566 else if (host_matches == TRUE &&
567 user_matches == TRUE)
568 append_runas($1, ", ");
570 if (userpw_matches($1, *user_runas, runas_pw))
578 if (printmatches == TRUE) {
579 if (in_alias == TRUE)
580 append_entries($1, ", ");
581 else if (host_matches == TRUE &&
582 user_matches == TRUE)
583 append_runas($1, ", ");
585 if (usergr_matches($1, *user_runas, runas_pw))
593 if (printmatches == TRUE) {
594 if (in_alias == TRUE)
595 append_entries($1, ", ");
596 else if (host_matches == TRUE &&
597 user_matches == TRUE)
598 append_runas($1, ", ");
600 if (netgr_matches($1, NULL, NULL, *user_runas))
608 aliasinfo *aip = find_alias($1, RUNAS_ALIAS);
610 if (printmatches == TRUE) {
611 if (in_alias == TRUE)
612 append_entries($1, ", ");
613 else if (host_matches == TRUE &&
614 user_matches == TRUE)
615 append_runas($1, ", ");
617 /* could be an all-caps username */
620 else if (strcmp($1, *user_runas) == 0)
624 (void) fprintf(stderr,
625 "%s: undeclared Runas_Alias `%s' referenced near line %d\n",
626 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
638 if (printmatches == TRUE) {
639 if (in_alias == TRUE)
640 append_entries("ALL", ", ");
641 else if (host_matches == TRUE &&
642 user_matches == TRUE)
643 append_runas("ALL", ", ");
649 cmndtag : /* empty */ {
650 /* Inherit {NO,}{PASSWD,EXEC,SETENV} status. */
651 if (printmatches == TRUE && host_matches == TRUE &&
652 user_matches == TRUE) {
653 if (no_passwd == TRUE)
654 cm_list[cm_list_len].nopasswd = TRUE;
656 cm_list[cm_list_len].nopasswd = FALSE;
657 if (no_execve == TRUE)
658 cm_list[cm_list_len].noexecve = TRUE;
660 cm_list[cm_list_len].noexecve = FALSE;
661 if (setenv_ok == TRUE)
662 cm_list[cm_list_len].setenv = TRUE;
664 cm_list[cm_list_len].setenv = FALSE;
669 if (printmatches == TRUE && host_matches == TRUE &&
670 user_matches == TRUE)
671 cm_list[cm_list_len].nopasswd = TRUE;
675 if (printmatches == TRUE && host_matches == TRUE &&
676 user_matches == TRUE)
677 cm_list[cm_list_len].nopasswd = FALSE;
681 if (printmatches == TRUE && host_matches == TRUE &&
682 user_matches == TRUE)
683 cm_list[cm_list_len].noexecve = TRUE;
687 if (printmatches == TRUE && host_matches == TRUE &&
688 user_matches == TRUE)
689 cm_list[cm_list_len].noexecve = FALSE;
693 if (printmatches == TRUE && host_matches == TRUE &&
694 user_matches == TRUE)
695 cm_list[cm_list_len].setenv = TRUE;
699 if (printmatches == TRUE && host_matches == TRUE &&
700 user_matches == TRUE)
701 cm_list[cm_list_len].setenv = FALSE;
706 if (printmatches == TRUE) {
707 if (in_alias == TRUE)
708 append_entries("ALL", ", ");
709 else if (host_matches == TRUE &&
710 user_matches == TRUE) {
711 append_cmnd("ALL", NULL);
715 /* sudo "ALL" implies the SETENV tag */
716 if (setenv_ok == UNSPEC)
726 if (printmatches == TRUE) {
727 if (in_alias == TRUE)
728 append_entries($1, ", ");
729 else if (host_matches == TRUE &&
730 user_matches == TRUE) {
731 append_cmnd($1, NULL);
736 if ((aip = find_alias($1, CMND_ALIAS)))
740 (void) fprintf(stderr,
741 "%s: undeclared Cmnd_Alias `%s' referenced near line %d\n",
742 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
753 if (printmatches == TRUE) {
754 if (in_alias == TRUE) {
755 append_entries($1.cmnd, ", ");
757 append_entries($1.args, " ");
759 if (host_matches == TRUE &&
760 user_matches == TRUE) {
761 append_cmnd($1.cmnd, NULL);
763 append_cmnd($1.args, " ");
768 if (command_matches($1.cmnd, $1.args))
778 hostaliases : hostalias
779 | hostaliases ':' hostalias
782 hostalias : ALIAS { push; } '=' hostlist {
783 if ((MATCHED(host_matches) || pedantic) &&
784 !add_alias($1, HOST_ALIAS, host_matches)) {
793 | hostlist ',' ophost
796 cmndaliases : cmndalias
797 | cmndaliases ':' cmndalias
802 if (printmatches == TRUE) {
804 /* Allocate space for ga_list if necessary. */
806 ga_list[ga_list_len-1].type = CMND_ALIAS;
807 ga_list[ga_list_len-1].alias = estrdup($1);
810 if ((MATCHED(cmnd_matches) || pedantic) &&
811 !add_alias($1, CMND_ALIAS, cmnd_matches)) {
818 if (printmatches == TRUE)
823 cmndlist : opcmnd { ; }
824 | cmndlist ',' opcmnd
827 runasaliases : runasalias
828 | runasaliases ':' runasalias
832 if (printmatches == TRUE) {
834 /* Allocate space for ga_list if necessary. */
836 ga_list[ga_list_len-1].type = RUNAS_ALIAS;
837 ga_list[ga_list_len-1].alias = estrdup($1);
840 if (($4 != NOMATCH || pedantic) &&
841 !add_alias($1, RUNAS_ALIAS, $4)) {
847 if (printmatches == TRUE)
852 useraliases : useralias
853 | useraliases ':' useralias
856 useralias : ALIAS { push; } '=' userlist {
857 if ((MATCHED(user_matches) || pedantic) &&
858 !add_alias($1, USER_ALIAS, user_matches)) {
868 | userlist ',' opuser
872 SETMATCH(user_matches, $1);
875 SETNMATCH(user_matches, $2);
880 if (userpw_matches($1, user_name, sudo_user.pw))
887 if (usergr_matches($1, user_name, sudo_user.pw))
894 if (netgr_matches($1, NULL, NULL, user_name))
901 aliasinfo *aip = find_alias($1, USER_ALIAS);
903 /* could be an all-caps username */
906 else if (strcmp($1, user_name) == 0)
910 (void) fprintf(stderr,
911 "%s: undeclared User_Alias `%s' referenced near line %d\n",
912 (pedantic == 1) ? "Warning" : "Error", $1, sudolineno);
929 #define MOREALIASES (32)
930 aliasinfo *aliases = NULL;
936 * Compare two aliasinfo structures, strcmp() style.
937 * Note that we do *not* compare their values.
944 aliasinfo *ai1, *ai2;
946 ai1 = (aliasinfo *) a1;
947 ai2 = (aliasinfo *) a2;
948 if ((r = strcmp(ai1->name, ai2->name)) == 0)
949 r = ai1->type - ai2->type;
955 * Compare two generic_alias structures, strcmp() style.
958 genaliascmp(entry, key)
959 const VOID *entry, *key;
962 struct generic_alias *ga1, *ga2;
964 ga1 = (struct generic_alias *) key;
965 ga2 = (struct generic_alias *) entry;
966 if ((r = strcmp(ga1->alias, ga2->alias)) == 0)
967 r = ga1->type - ga2->type;
974 * Adds the named alias of the specified type to the aliases list.
977 add_alias(alias, type, val)
986 if (naliases >= nslots)
991 ai.name = estrdup(alias);
992 onaliases = naliases;
994 aip = (aliasinfo *) lsearch((VOID *)&ai, (VOID *)aliases, &naliases,
995 sizeof(ai), aliascmp);
997 (void) snprintf(s, sizeof(s), "Aliases corrupted defining alias `%s'",
1002 if (onaliases == naliases) {
1003 (void) snprintf(s, sizeof(s), "Alias `%s' already defined", alias);
1012 * Searches for the named alias of the specified type.
1015 find_alias(alias, type)
1024 return((aliasinfo *) lfind((VOID *)&ai, (VOID *)aliases, &naliases,
1025 sizeof(ai), aliascmp));
1029 * Allocates more space for the aliases list.
1035 nslots += MOREALIASES;
1036 aliases = (aliasinfo *) erealloc3(aliases, nslots, sizeof(aliasinfo));
1040 * Lists the contents of the aliases list.
1047 for (n = 0; n < naliases; n++) {
1048 if (aliases[n].val == -1)
1051 switch (aliases[n].type) {
1053 (void) puts("HOST_ALIAS");
1057 (void) puts("CMND_ALIAS");
1061 (void) puts("USER_ALIAS");
1065 (void) puts("RUNAS_ALIAS");
1068 (void) printf("\t%s: %d\n", aliases[n].name, aliases[n].val);
1073 * Lists the contents of cm_list and ga_list for `sudo -l'.
1080 struct generic_alias *ga, key;
1082 (void) printf("User %s may run the following commands on this host:\n",
1084 for (count = 0; count < cm_list_len; count++) {
1086 /* Print the runas list. */
1087 (void) fputs(" ", stdout);
1088 if (cm_list[count].runas) {
1089 (void) putchar('(');
1090 p = strtok(cm_list[count].runas, ", ");
1092 if (p != cm_list[count].runas)
1093 (void) fputs(", ", stdout);
1096 key.type = RUNAS_ALIAS;
1097 if ((ga = (struct generic_alias *) lfind((VOID *) &key,
1098 (VOID *) &ga_list[0], &ga_list_len, sizeof(key), genaliascmp)))
1099 (void) fputs(ga->entries, stdout);
1101 (void) fputs(p, stdout);
1102 } while ((p = strtok(NULL, ", ")));
1103 (void) fputs(") ", stdout);
1105 (void) printf("(%s) ", def_runas_default);
1108 /* Is execve(2) disabled? */
1109 if (cm_list[count].noexecve == TRUE && !def_noexec)
1110 (void) fputs("NOEXEC: ", stdout);
1111 else if (cm_list[count].noexecve == FALSE && def_noexec)
1112 (void) fputs("EXEC: ", stdout);
1114 /* Is a password required? */
1115 if (cm_list[count].nopasswd == TRUE && def_authenticate)
1116 (void) fputs("NOPASSWD: ", stdout);
1117 else if (cm_list[count].nopasswd == FALSE && !def_authenticate)
1118 (void) fputs("PASSWD: ", stdout);
1120 /* Is setenv enabled? */
1121 if (cm_list[count].setenv == TRUE && !def_setenv)
1122 (void) fputs("SETENV: ", stdout);
1123 else if (cm_list[count].setenv == FALSE && def_setenv)
1124 (void) fputs("NOSETENV: ", stdout);
1126 /* Print the actual command or expanded Cmnd_Alias. */
1127 key.alias = cm_list[count].cmnd;
1128 key.type = CMND_ALIAS;
1129 if ((ga = (struct generic_alias *) lfind((VOID *) &key,
1130 (VOID *) &ga_list[0], &ga_list_len, sizeof(key), genaliascmp)))
1131 (void) puts(ga->entries);
1133 (void) puts(cm_list[count].cmnd);
1136 /* Be nice and free up space now that we are done. */
1137 for (count = 0; count < ga_list_len; count++) {
1138 efree(ga_list[count].alias);
1139 efree(ga_list[count].entries);
1144 for (count = 0; count < cm_list_len; count++) {
1145 efree(cm_list[count].runas);
1146 efree(cm_list[count].cmnd);
1155 * Appends a source string to the destination, optionally prefixing a separator.
1158 append(src, dstp, dst_len, dst_size, separator)
1160 size_t *dst_len, *dst_size;
1163 size_t src_len = strlen(src);
1167 * Only add the separator if there is something to separate from.
1168 * If the last char is a '!', don't apply the separator (XXX).
1170 if (separator && dst && dst[*dst_len - 1] != '!')
1171 src_len += strlen(separator);
1175 /* Assumes dst will be NULL if not set. */
1177 dst = (char *) emalloc(BUFSIZ);
1184 /* Allocate more space if necessary. */
1185 if (*dst_size <= *dst_len + src_len) {
1186 while (*dst_size <= *dst_len + src_len)
1187 *dst_size += BUFSIZ;
1189 dst = (char *) erealloc(dst, *dst_size);
1193 /* Copy src -> dst adding a separator if appropriate and adjust len. */
1195 (void) strlcat(dst, separator, *dst_size);
1196 (void) strlcat(dst, src, *dst_size);
1197 *dst_len += src_len;
1201 * Frees up space used by the aliases list and resets the associated counters.
1209 for (n = 0; n < naliases; n++)
1210 efree(aliases[n].name);
1214 naliases = nslots = 0;
1218 * Increments ga_list_len, allocating more space as necessary.
1224 if (++ga_list_len >= ga_list_size) {
1225 while ((ga_list_size += STACKINCREMENT) < ga_list_len)
1227 ga_list = (struct generic_alias *)
1228 erealloc3(ga_list, ga_list_size, sizeof(struct generic_alias));
1231 ga_list[ga_list_len - 1].entries = NULL;
1235 * Increments cm_list_len, allocating more space as necessary.
1241 if (++cm_list_len >= cm_list_size) {
1242 while ((cm_list_size += STACKINCREMENT) < cm_list_len)
1244 if (cm_list == NULL)
1245 cm_list_len = 0; /* start at 0 since it is a subscript */
1246 cm_list = (struct command_match *)
1247 erealloc3(cm_list, cm_list_size, sizeof(struct command_match));
1250 cm_list[cm_list_len].runas = cm_list[cm_list_len].cmnd = NULL;
1251 cm_list[cm_list_len].nopasswd = FALSE;
1252 cm_list[cm_list_len].noexecve = FALSE;
1253 cm_list[cm_list_len].setenv = FALSE;
1257 * Frees up spaced used by a previous parser run and allocates new space
1258 * for various data structures.
1264 /* Free up old data structures if we run the parser more than once. */
1269 parse_error = FALSE;
1275 /* Allocate space for the matching stack. */
1276 stacksize = STACKINCREMENT;
1277 match = (struct matchstack *) emalloc2(stacksize, sizeof(struct matchstack));
1279 /* Allocate space for the match list (for `sudo -l'). */
1280 if (printmatches == TRUE)
1281 expand_match_list();