3 * Copyright (c) 1996, 1998-2005, 2007-2009
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 */
43 # ifdef HAVE_STRINGS_H
46 #endif /* HAVE_STRING_H */
49 #endif /* HAVE_UNISTD_H */
50 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
52 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
55 # define NAMLEN(dirent) strlen((dirent)->d_name)
57 # define dirent direct
58 # define NAMLEN(dirent) (dirent)->d_namlen
59 # ifdef HAVE_SYS_NDIR_H
60 # include <sys/ndir.h>
62 # ifdef HAVE_SYS_DIR_H
74 extern YYSTYPE yylval;
75 extern int parse_error;
78 static int sawspace = 0;
79 static int arg_len = 0;
80 static int arg_size = 0;
82 static int append __P((char *, int));
83 static int _fill __P((char *, int, int));
84 static int fill_cmnd __P((char *, int));
85 static int fill_args __P((char *, int, int));
86 static int _push_include __P((char *, int));
87 static int pop_include __P((void));
88 static int ipv6_valid __P((const char *s));
89 static char *parse_include __P((char *));
90 extern void yyerror __P((const char *));
92 #define fill(a, b) _fill(a, b, 0)
94 #define push_include(_p) (_push_include((_p), FALSE))
95 #define push_includedir(_p) (_push_include((_p), TRUE))
97 /* realloc() to size + COMMANDARGINC to make room for command args */
98 #define COMMANDARGINC 64
101 #define LEXTRACE(msg) fputs(msg, stderr)
103 #define LEXTRACE(msg)
107 HEX16 [0-9A-Fa-f]{1,4}
108 OCTET (1?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5])
109 IPV4ADDR {OCTET}(\.{OCTET}){3}
110 IPV6ADDR ({HEX16}?:){2,7}{HEX16}?|({HEX16}?:){2,6}:{IPV4ADDR}
112 HOSTNAME [[:alnum:]_-]+
113 WORD ([^#>!=:,\(\) \t\n\\]|\\[^\n])+
115 PATH \/(\\[\,:= \t#]|[^\,:=\\ \t\n#])+
116 ENVAR ([^#!=, \t\n\\\"]|\\[^\n])([^#=, \t\n\\\"]|\\[^\n])*
129 <GOTDEFS>[[:blank:]]+ BEGIN STARTDEFS;
131 <STARTDEFS>{DEFVAR} {
134 if (!fill(yytext, yyleng))
162 LEXTRACE("BEGINSTR ");
163 yylval.string = NULL;
168 LEXTRACE("WORD(2) ");
169 if (!fill(yytext, yyleng))
176 \\[[:blank:]]*\n[[:blank:]]* {
177 /* Line continuation char followed by newline. */
189 LEXTRACE("BACKSLASH ");
190 if (!append(yytext, yyleng))
195 LEXTRACE("STRBODY ");
196 if (!append(yytext, yyleng))
203 /* quoted fnmatch glob char, pass verbatim */
204 LEXTRACE("QUOTEDCHAR ");
205 if (!fill_args(yytext, 2, sawspace))
211 /* quoted sudoers special char, strip backslash */
212 LEXTRACE("QUOTEDCHAR ");
213 if (!fill_args(yytext + 1, 1, sawspace))
222 } /* end of command line args */
226 if (!fill_args(yytext, yyleng, sawspace))
229 } /* a command line arg */
232 <INITIAL>^#include[[:blank:]]+\/.*\n {
235 if ((path = parse_include(yytext)) == NULL)
238 LEXTRACE("INCLUDE\n");
240 /* Push current buffer and switch to include file */
241 if (!push_include(path))
245 <INITIAL>^#includedir[[:blank:]]+\/.*\n {
248 if ((path = parse_include(yytext)) == NULL)
251 LEXTRACE("INCLUDEDIR\n");
254 * Push current buffer and switch to include file.
255 * We simply ignore empty directories.
257 if (!push_includedir(path) && parse_error)
261 <INITIAL>^[[:blank:]]*Defaults([:@>\!]{WORD})? {
263 for (n = 0; isblank((unsigned char)yytext[n]); n++)
267 switch (yytext[n++]) {
270 LEXTRACE("DEFAULTS_USER ");
271 return(DEFAULTS_USER);
274 LEXTRACE("DEFAULTS_RUNAS ");
275 return(DEFAULTS_RUNAS);
278 LEXTRACE("DEFAULTS_HOST ");
279 return(DEFAULTS_HOST);
282 LEXTRACE("DEFAULTS_CMND ");
283 return(DEFAULTS_CMND);
285 LEXTRACE("DEFAULTS ");
290 <INITIAL>^[[:blank:]]*(Host|Cmnd|User|Runas)_Alias {
292 for (n = 0; isblank((unsigned char)yytext[n]); n++)
296 LEXTRACE("HOSTALIAS ");
299 LEXTRACE("CMNDALIAS ");
302 LEXTRACE("USERALIAS ");
305 LEXTRACE("RUNASALIAS ");
310 NOPASSWD[[:blank:]]*: {
311 /* cmnd does not require passwd for this user */
312 LEXTRACE("NOPASSWD ");
316 PASSWD[[:blank:]]*: {
317 /* cmnd requires passwd for this user */
322 NOEXEC[[:blank:]]*: {
332 SETENV[[:blank:]]*: {
337 NOSETENV[[:blank:]]*: {
338 LEXTRACE("NOSETENV ");
344 if (!fill(yytext, yyleng))
346 LEXTRACE("NETGROUP ");
352 if (!fill(yytext, yyleng))
354 LEXTRACE("USERGROUP ");
358 {IPV4ADDR}(\/{IPV4ADDR})? {
359 if (!fill(yytext, yyleng))
361 LEXTRACE("NTWKADDR ");
365 {IPV4ADDR}\/([12][0-9]*|3[0-2]*) {
366 if (!fill(yytext, yyleng))
368 LEXTRACE("NTWKADDR ");
372 {IPV6ADDR}(\/{IPV6ADDR})? {
373 if (!ipv6_valid(yytext)) {
377 if (!fill(yytext, yyleng))
379 LEXTRACE("NTWKADDR ");
383 {IPV6ADDR}\/([0-9]|[1-9][0-9]|1[01][0-9]|12[0-8]) {
384 if (!ipv6_valid(yytext)) {
388 if (!fill(yytext, yyleng))
390 LEXTRACE("NTWKADDR ");
394 [[:upper:]][[:upper:][:digit:]_]* {
395 if (strcmp(yytext, "ALL") == 0) {
400 /* XXX - restrict type/role to initial state */
401 if (strcmp(yytext, "TYPE") == 0) {
405 if (strcmp(yytext, "ROLE") == 0) {
409 #endif /* HAVE_SELINUX */
410 if (!fill(yytext, yyleng))
416 <GOTDEFS>({PATH}|sudoedit) {
417 /* no command args allowed for Defaults!/path */
418 if (!fill_cmnd(yytext, yyleng))
420 LEXTRACE("COMMAND ");
426 LEXTRACE("COMMAND ");
427 if (!fill_cmnd(yytext, yyleng))
432 /* directories can't have args... */
433 if (yytext[yyleng - 1] == '/') {
434 LEXTRACE("COMMAND ");
435 if (!fill_cmnd(yytext, yyleng))
440 LEXTRACE("COMMAND ");
441 if (!fill_cmnd(yytext, yyleng))
446 <INITIAL,GOTDEFS>\"[^"\n]+\" {
447 /* a quoted user/group name */
448 if (!fill(yytext + 1, yyleng - 2))
452 LEXTRACE("USERGROUP ");
455 LEXTRACE("NETGROUP ");
458 LEXTRACE("WORD(4) ");
463 <INITIAL,GOTDEFS>({ID}|{WORD}) {
465 if (!fill(yytext, yyleng))
467 LEXTRACE("WORD(5) ");
498 return('!'); /* return '!' */
506 } /* return newline */
508 <*>[[:blank:]]+ { /* throw away space/tabs */
509 sawspace = TRUE; /* but remember for fill_args */
512 <*>\\[[:blank:]]*\n {
513 sawspace = TRUE; /* remember for fill_args */
516 } /* throw away EOL after \ */
518 <INITIAL,STARTDEFS,INDEFS>#(-[^\n0-9].*|[^\n0-9-].*)?\n {
523 } /* comment, not uid/gid */
531 if (YY_START != INITIAL) {
548 s += 2; /* skip \\x */
549 for (i = 0; i < 2; i++) {
584 return((unsigned char)result);
588 _fill(src, len, olen)
594 dst = olen ? realloc(yylval.string, olen + len + 1) : malloc(len + 1);
596 yyerror("unable to allocate memory");
601 /* Copy the string and collapse any escaped characters. */
604 if (*src == '\\' && len) {
605 if (src[1] == 'x' && len >= 3 &&
606 isxdigit((unsigned char) src[2]) &&
607 isxdigit((unsigned char) src[3])) {
608 *dst++ = hexchar(src);
631 if (yylval.string != NULL)
632 olen = strlen(yylval.string);
634 return(_fill(src, len, olen));
638 ((c) == ',' || (c) == ':' || (c) == '=' || (c) == ' ' || (c) == '\t' || (c) == '#')
648 arg_len = arg_size = 0;
650 dst = yylval.command.cmnd = (char *) malloc(len + 1);
651 if (yylval.command.cmnd == NULL) {
652 yyerror("unable to allocate memory");
656 /* Copy the string and collapse any escaped sudo-specific characters. */
657 for (i = 0; i < len; i++) {
658 if (src[i] == '\\' && i != len - 1 && SPECIAL(src[i + 1]))
665 yylval.command.args = NULL;
670 fill_args(s, len, addspace)
678 if (yylval.command.args == NULL) {
682 new_len = arg_len + len + addspace;
684 if (new_len >= arg_size) {
685 /* Allocate more space than we need for subsequent args */
686 while (new_len >= (arg_size += COMMANDARGINC))
689 p = yylval.command.args ?
690 (char *) realloc(yylval.command.args, arg_size) :
691 (char *) malloc(arg_size);
693 efree(yylval.command.args);
694 yyerror("unable to allocate memory");
697 yylval.command.args = p;
700 /* Efficiently append the arg (with a leading space if needed). */
701 p = yylval.command.args + arg_len;
704 if (strlcpy(p, s, arg_size - (p - yylval.command.args)) != len) {
705 yyerror("fill_args: buffer overflow"); /* paranoia */
714 struct path_list *next;
717 struct include_stack {
720 struct path_list *more; /* more files in case of includedir */
730 const struct path_list * const *p1 = v1;
731 const struct path_list * const *p2 = v2;
733 return(strcmp((*p1)->path, (*p2)->path));
737 switch_dir(stack, dirpath)
738 struct include_stack *stack;
746 struct path_list *pl, *first = NULL;
747 struct path_list **sorted = NULL;
749 if (!(dir = opendir(dirpath))) {
753 while ((dent = readdir(dir))) {
754 /* Ignore files that end in '~' or have a '.' in them. */
755 if (dent->d_name[0] == '\0' || dent->d_name[NAMLEN(dent) - 1] == '~'
756 || strchr(dent->d_name, '.') != NULL) {
759 if (asprintf(&path, "%s/%s", dirpath, dent->d_name) == -1) {
763 if (stat(path, &sb) != 0 || !S_ISREG(sb.st_mode)) {
767 pl = malloc(sizeof(*pl));
780 /* Sort the list as an array. */
781 sorted = malloc(sizeof(*sorted) * count);
785 for (i = 0; i < count; i++) {
789 qsort(sorted, count, sizeof(*sorted), pl_compare);
791 /* Apply sorting to the list. */
793 sorted[count - 1]->next = NULL;
794 for (i = 1; i < count; i++)
795 sorted[i - 1]->next = sorted[i];
798 /* Pull out the first element for parsing, leave the rest for later. */
811 while (first != NULL) {
823 #define MAX_SUDOERS_DEPTH 128
824 #define SUDOERS_STACK_INCREMENT 16
826 static size_t istacksize, idepth;
827 static struct include_stack *istack;
833 struct path_list *pl;
837 while ((pl = istack[idepth].more) != NULL) {
838 istack[idepth].more = pl->next;
842 efree(istack[idepth].path);
843 if (idepth && !istack[idepth].keepopen)
844 fclose(istack[idepth].bs->yy_input_file);
845 yy_delete_buffer(istack[idepth].bs);
849 istacksize = idepth = 0;
854 _push_include(path, isdir)
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 if ((fp = open_sudoers(path, FALSE, &keepopen)) == NULL) {
881 return(FALSE); /* XXX - just to go next one */
884 if ((fp = open_sudoers(path, TRUE, &keepopen)) == NULL) {
888 istack[idepth].more = NULL;
890 /* Push the old (current) file and open the new one. */
891 istack[idepth].path = sudoers; /* push old path */
892 istack[idepth].bs = YY_CURRENT_BUFFER;
893 istack[idepth].lineno = sudolineno;
894 istack[idepth].keepopen = keepopen;
898 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
906 struct path_list *pl;
913 fclose(YY_CURRENT_BUFFER->yy_input_file);
914 yy_delete_buffer(YY_CURRENT_BUFFER);
916 if ((pl = istack[idepth - 1].more) != NULL) {
917 /* Move to next file in the dir. */
918 istack[idepth - 1].more = pl->next;
919 if ((fp = open_sudoers(pl->path, FALSE, &keepopen)) == NULL) {
921 return(FALSE); /* XXX - just to go next one */
926 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
930 yy_switch_to_buffer(istack[idepth].bs);
932 sudoers = istack[idepth].path;
933 sudolineno = istack[idepth].lineno;
942 char *cp, *ep, *path;
943 int len = 0, subst = 0;
944 size_t shost_len = 0;
946 /* Pull out path from #include line. */
947 cp = base + sizeof("#include");
949 cp += 3; /* includedir */
950 while (isblank((unsigned char) *cp))
953 while (*ep != '\0' && !isspace((unsigned char) *ep)) {
954 if (ep[0] == '%' && ep[1] == 'h') {
955 shost_len = strlen(user_shost);
956 len += shost_len - 2;
962 /* Make a copy of path and return it. */
963 len += (int)(ep - cp);
964 if ((path = malloc(len + 1)) == NULL)
965 yyerror("unable to allocate memory");
967 /* substitute for %h */
970 if (cp[0] == '%' && cp[1] == 'h') {
971 memcpy(pp, user_shost, shost_len);
980 memcpy(path, cp, len);
984 /* Push any excess characters (e.g. comment, newline) back to the lexer */
986 yyless((int)(ep - base));
992 * Check to make sure an IPv6 address does not contain multiple instances
993 * of the string "::". Assumes strlen(s) >= 1.
994 * Returns TRUE if address is valid else FALSE.
1002 for (; *s != '\0'; s++) {
1003 if (s[0] == ':' && s[1] == ':') {
1008 nmatch = 0; /* reset if we hit netmask */
1011 return (nmatch <= 1);