3 * Copyright (c) 1996, 1998-2005, 2007-2010
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 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
19 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 * Sponsored in part by the Defense Advanced Research Projects
22 * Agency (DARPA) and Air Force Research Laboratory, Air Force
23 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
28 #include <sys/types.h>
29 #include <sys/param.h>
39 #endif /* STDC_HEADERS */
42 #endif /* HAVE_STRING_H */
45 #endif /* HAVE_STRINGS_H */
48 #endif /* HAVE_UNISTD_H */
49 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
51 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
54 # define NAMLEN(dirent) strlen((dirent)->d_name)
56 # define dirent direct
57 # define NAMLEN(dirent) (dirent)->d_namlen
58 # ifdef HAVE_SYS_NDIR_H
59 # include <sys/ndir.h>
61 # ifdef HAVE_SYS_DIR_H
73 extern YYSTYPE yylval;
74 extern int parse_error;
77 static int sawspace = 0;
78 static int arg_len = 0;
79 static int arg_size = 0;
81 static int append __P((char *, int));
82 static int _fill __P((char *, int, int));
83 static int fill_cmnd __P((char *, int));
84 static int fill_args __P((char *, int, int));
85 static int _push_include __P((char *, int));
86 static int pop_include __P((void));
87 static int ipv6_valid __P((const char *s));
88 static char *parse_include __P((char *));
89 extern void yyerror __P((const char *));
91 #define fill(a, b) _fill(a, b, 0)
93 #define push_include(_p) (_push_include((_p), FALSE))
94 #define push_includedir(_p) (_push_include((_p), TRUE))
96 /* realloc() to size + COMMANDARGINC to make room for command args */
97 #define COMMANDARGINC 64
100 #define LEXTRACE(msg) fputs(msg, stderr)
102 #define LEXTRACE(msg)
106 HEX16 [0-9A-Fa-f]{1,4}
107 OCTET (1?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5])
108 IPV4ADDR {OCTET}(\.{OCTET}){3}
109 IPV6ADDR ({HEX16}?:){2,7}{HEX16}?|({HEX16}?:){2,6}:{IPV4ADDR}
111 HOSTNAME [[:alnum:]_-]+
112 WORD ([^#>!=:,\(\) \t\n\\]|\\[^\n])+
114 PATH \/(\\[\,:= \t#]|[^\,:=\\ \t\n#])+
115 ENVAR ([^#!=, \t\n\\\"]|\\[^\n])([^#=, \t\n\\\"]|\\[^\n])*
128 <GOTDEFS>[[:blank:]]+ BEGIN STARTDEFS;
130 <STARTDEFS>{DEFVAR} {
133 if (!fill(yytext, yyleng))
161 LEXTRACE("BEGINSTR ");
162 yylval.string = NULL;
167 LEXTRACE("WORD(2) ");
168 if (!fill(yytext, yyleng))
175 \\[[:blank:]]*\n[[:blank:]]* {
176 /* Line continuation char followed by newline. */
188 LEXTRACE("BACKSLASH ");
189 if (!append(yytext, yyleng))
194 LEXTRACE("STRBODY ");
195 if (!append(yytext, yyleng))
202 /* quoted fnmatch glob char, pass verbatim */
203 LEXTRACE("QUOTEDCHAR ");
204 if (!fill_args(yytext, 2, sawspace))
210 /* quoted sudoers special char, strip backslash */
211 LEXTRACE("QUOTEDCHAR ");
212 if (!fill_args(yytext + 1, 1, sawspace))
221 } /* end of command line args */
225 if (!fill_args(yytext, yyleng, sawspace))
228 } /* a command line arg */
231 <INITIAL>^#include[[:blank:]]+\/.*\n {
234 if ((path = parse_include(yytext)) == NULL)
237 LEXTRACE("INCLUDE\n");
239 /* Push current buffer and switch to include file */
240 if (!push_include(path))
244 <INITIAL>^#includedir[[:blank:]]+\/.*\n {
247 if ((path = parse_include(yytext)) == NULL)
250 LEXTRACE("INCLUDEDIR\n");
253 * Push current buffer and switch to include file.
254 * We simply ignore empty directories.
256 if (!push_includedir(path) && parse_error)
260 <INITIAL>^[[:blank:]]*Defaults([:@>\!]\!?{WORD})? {
262 for (n = 0; isblank((unsigned char)yytext[n]); n++)
266 switch (yytext[n++]) {
269 LEXTRACE("DEFAULTS_USER ");
270 return(DEFAULTS_USER);
273 LEXTRACE("DEFAULTS_RUNAS ");
274 return(DEFAULTS_RUNAS);
277 LEXTRACE("DEFAULTS_HOST ");
278 return(DEFAULTS_HOST);
281 LEXTRACE("DEFAULTS_CMND ");
282 return(DEFAULTS_CMND);
284 LEXTRACE("DEFAULTS ");
289 <INITIAL>^[[:blank:]]*(Host|Cmnd|User|Runas)_Alias {
291 for (n = 0; isblank((unsigned char)yytext[n]); n++)
295 LEXTRACE("HOSTALIAS ");
298 LEXTRACE("CMNDALIAS ");
301 LEXTRACE("USERALIAS ");
304 LEXTRACE("RUNASALIAS ");
309 NOPASSWD[[:blank:]]*: {
310 /* cmnd does not require passwd for this user */
311 LEXTRACE("NOPASSWD ");
315 PASSWD[[:blank:]]*: {
316 /* cmnd requires passwd for this user */
321 NOEXEC[[:blank:]]*: {
331 SETENV[[:blank:]]*: {
336 NOSETENV[[:blank:]]*: {
337 LEXTRACE("NOSETENV ");
343 if (!fill(yytext, yyleng))
345 LEXTRACE("NETGROUP ");
351 if (!fill(yytext, yyleng))
353 LEXTRACE("USERGROUP ");
357 {IPV4ADDR}(\/{IPV4ADDR})? {
358 if (!fill(yytext, yyleng))
360 LEXTRACE("NTWKADDR ");
364 {IPV4ADDR}\/([12][0-9]*|3[0-2]*) {
365 if (!fill(yytext, yyleng))
367 LEXTRACE("NTWKADDR ");
371 {IPV6ADDR}(\/{IPV6ADDR})? {
372 if (!ipv6_valid(yytext)) {
376 if (!fill(yytext, yyleng))
378 LEXTRACE("NTWKADDR ");
382 {IPV6ADDR}\/([0-9]|[1-9][0-9]|1[01][0-9]|12[0-8]) {
383 if (!ipv6_valid(yytext)) {
387 if (!fill(yytext, yyleng))
389 LEXTRACE("NTWKADDR ");
393 [[:upper:]][[:upper:][:digit:]_]* {
394 if (strcmp(yytext, "ALL") == 0) {
399 /* XXX - restrict type/role to initial state */
400 if (strcmp(yytext, "TYPE") == 0) {
404 if (strcmp(yytext, "ROLE") == 0) {
408 #endif /* HAVE_SELINUX */
409 if (!fill(yytext, yyleng))
415 <GOTDEFS>({PATH}|sudoedit) {
416 /* no command args allowed for Defaults!/path */
417 if (!fill_cmnd(yytext, yyleng))
419 LEXTRACE("COMMAND ");
425 LEXTRACE("COMMAND ");
426 if (!fill_cmnd(yytext, yyleng))
431 /* directories can't have args... */
432 if (yytext[yyleng - 1] == '/') {
433 LEXTRACE("COMMAND ");
434 if (!fill_cmnd(yytext, yyleng))
439 LEXTRACE("COMMAND ");
440 if (!fill_cmnd(yytext, yyleng))
445 <INITIAL,GOTDEFS>\"[^"\n]+\" {
446 /* a quoted user/group name */
447 if (!fill(yytext + 1, yyleng - 2))
451 LEXTRACE("USERGROUP ");
454 LEXTRACE("NETGROUP ");
457 LEXTRACE("WORD(4) ");
462 <INITIAL,GOTDEFS>({ID}|{WORD}) {
464 if (!fill(yytext, yyleng))
466 LEXTRACE("WORD(5) ");
497 return('!'); /* return '!' */
505 } /* return newline */
507 <*>[[:blank:]]+ { /* throw away space/tabs */
508 sawspace = TRUE; /* but remember for fill_args */
511 <*>\\[[:blank:]]*\n {
512 sawspace = TRUE; /* remember for fill_args */
515 } /* throw away EOL after \ */
517 <INITIAL,STARTDEFS,INDEFS>#(-[^\n0-9].*|[^\n0-9-].*)?\n {
522 } /* comment, not uid/gid */
530 if (YY_START != INITIAL) {
547 s += 2; /* skip \\x */
548 for (i = 0; i < 2; i++) {
583 return((unsigned char)result);
587 _fill(src, len, olen)
593 dst = olen ? realloc(yylval.string, olen + len + 1) : malloc(len + 1);
595 yyerror("unable to allocate memory");
600 /* Copy the string and collapse any escaped characters. */
603 if (*src == '\\' && len) {
604 if (src[1] == 'x' && len >= 3 &&
605 isxdigit((unsigned char) src[2]) &&
606 isxdigit((unsigned char) src[3])) {
607 *dst++ = hexchar(src);
630 if (yylval.string != NULL)
631 olen = strlen(yylval.string);
633 return(_fill(src, len, olen));
637 ((c) == ',' || (c) == ':' || (c) == '=' || (c) == ' ' || (c) == '\t' || (c) == '#')
647 arg_len = arg_size = 0;
649 dst = yylval.command.cmnd = (char *) malloc(len + 1);
650 if (yylval.command.cmnd == NULL) {
651 yyerror("unable to allocate memory");
655 /* Copy the string and collapse any escaped sudo-specific characters. */
656 for (i = 0; i < len; i++) {
657 if (src[i] == '\\' && i != len - 1 && SPECIAL(src[i + 1]))
664 yylval.command.args = NULL;
669 fill_args(s, len, addspace)
677 if (yylval.command.args == NULL) {
681 new_len = arg_len + len + addspace;
683 if (new_len >= arg_size) {
684 /* Allocate more space than we need for subsequent args */
685 while (new_len >= (arg_size += COMMANDARGINC))
688 p = yylval.command.args ?
689 (char *) realloc(yylval.command.args, arg_size) :
690 (char *) malloc(arg_size);
692 efree(yylval.command.args);
693 yyerror("unable to allocate memory");
696 yylval.command.args = p;
699 /* Efficiently append the arg (with a leading space if needed). */
700 p = yylval.command.args + arg_len;
703 if (strlcpy(p, s, arg_size - (p - yylval.command.args)) != len) {
704 yyerror("fill_args: buffer overflow"); /* paranoia */
713 struct path_list *next;
716 struct include_stack {
719 struct path_list *more; /* more files in case of includedir */
729 const struct path_list * const *p1 = v1;
730 const struct path_list * const *p2 = v2;
732 return(strcmp((*p1)->path, (*p2)->path));
736 switch_dir(stack, dirpath)
737 struct include_stack *stack;
745 struct path_list *pl, *first = NULL;
746 struct path_list **sorted = NULL;
748 if (!(dir = opendir(dirpath))) {
752 while ((dent = readdir(dir))) {
753 /* Ignore files that end in '~' or have a '.' in them. */
754 if (dent->d_name[0] == '\0' || dent->d_name[NAMLEN(dent) - 1] == '~'
755 || strchr(dent->d_name, '.') != NULL) {
758 if (asprintf(&path, "%s/%s", dirpath, dent->d_name) == -1) {
762 if (stat(path, &sb) != 0 || !S_ISREG(sb.st_mode)) {
766 pl = malloc(sizeof(*pl));
779 /* Sort the list as an array. */
780 sorted = malloc(sizeof(*sorted) * count);
784 for (i = 0; i < count; i++) {
788 qsort(sorted, count, sizeof(*sorted), pl_compare);
790 /* Apply sorting to the list. */
792 sorted[count - 1]->next = NULL;
793 for (i = 1; i < count; i++)
794 sorted[i - 1]->next = sorted[i];
797 /* Pull out the first element for parsing, leave the rest for later. */
810 while (first != NULL) {
822 #define MAX_SUDOERS_DEPTH 128
823 #define SUDOERS_STACK_INCREMENT 16
825 static size_t istacksize, idepth;
826 static struct include_stack *istack;
832 struct path_list *pl;
836 while ((pl = istack[idepth].more) != NULL) {
837 istack[idepth].more = pl->next;
841 efree(istack[idepth].path);
842 if (idepth && !istack[idepth].keepopen)
843 fclose(istack[idepth].bs->yy_input_file);
844 yy_delete_buffer(istack[idepth].bs);
848 istacksize = idepth = 0;
853 _push_include(path, isdir)
857 struct path_list *pl;
860 /* push current state onto stack */
861 if (idepth >= istacksize) {
862 if (idepth > MAX_SUDOERS_DEPTH) {
863 yyerror("too many levels of includes");
866 istacksize += SUDOERS_STACK_INCREMENT;
867 istack = (struct include_stack *) realloc(istack,
868 sizeof(*istack) * istacksize);
869 if (istack == NULL) {
870 yyerror("unable to allocate memory");
875 if (!(path = switch_dir(&istack[idepth], path))) {
876 /* switch_dir() called yyerror() for us */
879 while ((fp = open_sudoers(path, FALSE, &keepopen)) == NULL) {
880 /* Unable to open path in includedir, go to next one, if any. */
882 if ((pl = istack[idepth].more) == NULL)
885 istack[idepth].more = pl->next;
889 if ((fp = open_sudoers(path, TRUE, &keepopen)) == NULL) {
893 istack[idepth].more = NULL;
895 /* Push the old (current) file and open the new one. */
896 istack[idepth].path = sudoers; /* push old path */
897 istack[idepth].bs = YY_CURRENT_BUFFER;
898 istack[idepth].lineno = sudolineno;
899 istack[idepth].keepopen = keepopen;
903 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
911 struct path_list *pl;
918 fclose(YY_CURRENT_BUFFER->yy_input_file);
919 yy_delete_buffer(YY_CURRENT_BUFFER);
920 /* If we are in an include dir, move to the next file. */
921 while ((pl = istack[idepth - 1].more) != NULL) {
922 fp = open_sudoers(pl->path, FALSE, &keepopen);
924 istack[idepth - 1].more = pl->next;
928 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
932 /* Unable to open path in include dir, go to next one. */
933 istack[idepth - 1].more = pl->next;
937 /* If no path list, just pop the last dir on the stack. */
940 yy_switch_to_buffer(istack[idepth].bs);
942 sudoers = istack[idepth].path;
943 sudolineno = istack[idepth].lineno;
944 keepopen = istack[idepth].keepopen;
953 char *cp, *ep, *path;
954 int len = 0, subst = 0;
955 size_t shost_len = 0;
957 /* Pull out path from #include line. */
958 cp = base + sizeof("#include");
960 cp += 3; /* includedir */
961 while (isblank((unsigned char) *cp))
964 while (*ep != '\0' && !isspace((unsigned char) *ep)) {
965 if (ep[0] == '%' && ep[1] == 'h') {
966 shost_len = strlen(user_shost);
967 len += shost_len - 2;
973 /* Make a copy of path and return it. */
974 len += (int)(ep - cp);
975 if ((path = malloc(len + 1)) == NULL)
976 yyerror("unable to allocate memory");
978 /* substitute for %h */
981 if (cp[0] == '%' && cp[1] == 'h') {
982 memcpy(pp, user_shost, shost_len);
991 memcpy(path, cp, len);
995 /* Push any excess characters (e.g. comment, newline) back to the lexer */
997 yyless((int)(ep - base));
1003 * Check to make sure an IPv6 address does not contain multiple instances
1004 * of the string "::". Assumes strlen(s) >= 1.
1005 * Returns TRUE if address is valid else FALSE.
1013 for (; *s != '\0'; s++) {
1014 if (s[0] == ':' && s[1] == ':') {
1019 nmatch = 0; /* reset if we hit netmask */
1022 return (nmatch <= 1);