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 * 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>
38 #endif /* STDC_HEADERS */
42 # ifdef HAVE_STRINGS_H
45 #endif /* HAVE_STRING_H */
48 #endif /* HAVE_UNISTD_H */
49 #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
51 #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
58 __unused static const char rcsid[] = "$Sudo: parse.lex,v 1.132.2.10 2008/06/26 11:53:50 millert Exp $";
61 #undef yywrap /* guard against a yywrap macro */
63 extern YYSTYPE yylval;
64 extern int clearaliases;
66 static int sawspace = 0;
67 static int arg_len = 0;
68 static int arg_size = 0;
70 static int ipv6_valid __P((const char *s));
71 static void _fill __P((char *, int, int));
72 static void append __P((char *, int));
73 static void fill_cmnd __P((char *, int));
74 static void fill_args __P((char *, int, int));
75 extern void reset_aliases __P((void));
76 extern void yyerror __P((char *));
78 #define fill(a, b) _fill(a, b, 0)
80 /* realloc() to size + COMMANDARGINC to make room for command args */
81 #define COMMANDARGINC 64
84 #define LEXTRACE(msg) fputs(msg, stderr)
90 HEX16 [0-9A-Fa-f]{1,4}
91 OCTET (1?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5])
92 IPV4ADDR {OCTET}(\.{OCTET}){3}
93 IPV6ADDR ({HEX16}?:){2,7}{HEX16}?|({HEX16}?:){2,6}:{IPV4ADDR}
95 HOSTNAME [[:alnum:]_-]+
96 WORD ([^#>@!=:,\(\) \t\n\\]|\\[^\n])+
97 ENVAR ([^#!=, \t\n\\\"]|\\[^\n])([^#=, \t\n\\]|\\[^\n])*
100 /* XXX - convert GOTRUNAS to exclusive state (GOTDEFS cannot be) */
109 <GOTDEFS>[[:blank:]]+ BEGIN STARTDEFS;
111 <STARTDEFS>{DEFVAR} {
114 fill(yytext, yyleng);
141 LEXTRACE("BEGINSTR ");
142 yylval.string = NULL;
147 LEXTRACE("WORD(2) ");
148 fill(yytext, yyleng);
154 \\[[:blank:]]*\n[[:blank:]]* {
155 /* Line continuation char followed by newline. */
167 LEXTRACE("BACKSLASH ");
168 append(yytext, yyleng);
172 LEXTRACE("STRBODY ");
173 append(yytext, yyleng);
179 /* quoted fnmatch glob char, pass verbatim */
180 LEXTRACE("QUOTEDCHAR ");
181 fill_args(yytext, 2, sawspace);
186 /* quoted sudoers special char, strip backslash */
187 LEXTRACE("QUOTEDCHAR ");
188 fill_args(yytext + 1, 1, sawspace);
196 } /* end of command line args */
200 fill_args(yytext, yyleng, sawspace);
202 } /* a command line arg */
205 <INITIAL>^Defaults[:@>]? {
209 LEXTRACE("DEFAULTS_USER ");
210 return(DEFAULTS_USER);
212 LEXTRACE("DEFAULTS_RUNAS ");
213 return(DEFAULTS_RUNAS);
215 LEXTRACE("DEFAULTS_HOST ");
216 return(DEFAULTS_HOST);
218 LEXTRACE("DEFAULTS ");
223 <INITIAL>^(Host|Cmnd|User|Runas)_Alias {
224 fill(yytext, yyleng);
227 LEXTRACE("HOSTALIAS ");
230 LEXTRACE("CMNDALIAS ");
233 LEXTRACE("USERALIAS ");
236 LEXTRACE("RUNASALIAS ");
242 NOPASSWD[[:blank:]]*: {
243 /* cmnd does not require passwd for this user */
244 LEXTRACE("NOPASSWD ");
248 PASSWD[[:blank:]]*: {
249 /* cmnd requires passwd for this user */
254 NOEXEC[[:blank:]]*: {
264 SETENV[[:blank:]]*: {
269 NOSETENV[[:blank:]]*: {
270 LEXTRACE("NOSETENV ");
276 fill(yytext, yyleng);
277 LEXTRACE("NETGROUP ");
283 fill(yytext, yyleng);
288 {IPV4ADDR}(\/{IPV4ADDR})? {
289 fill(yytext, yyleng);
290 LEXTRACE("NTWKADDR ");
294 {IPV4ADDR}\/([12][0-9]*|3[0-2]*) {
295 fill(yytext, yyleng);
296 LEXTRACE("NTWKADDR ");
300 {IPV6ADDR}(\/{IPV6ADDR})? {
301 if (!ipv6_valid(yytext)) {
305 fill(yytext, yyleng);
306 LEXTRACE("NTWKADDR ");
310 {IPV6ADDR}\/([0-9]|[1-9][0-9]|1[01][0-9]|12[0-8]) {
311 if (!ipv6_valid(yytext)) {
315 fill(yytext, yyleng);
316 LEXTRACE("NTWKADDR ");
326 [[:upper:]][[:upper:][:digit:]_]* {
327 if (strcmp(yytext, "ALL") == 0) {
332 /* XXX - restrict type/role to initial state */
333 if (strcmp(yytext, "TYPE") == 0) {
337 if (strcmp(yytext, "ROLE") == 0) {
341 #endif /* HAVE_SELINUX */
342 fill(yytext, yyleng);
347 <GOTRUNAS>(#[0-9-]+|{WORD}) {
348 /* username/uid that user can run command as */
349 fill(yytext, yyleng);
350 LEXTRACE("WORD(3) ");
354 <GOTRUNAS>#[^0-9-].*\n {
367 LEXTRACE("COMMAND ");
368 fill_cmnd(yytext, yyleng);
371 \/(\\[\,:= \t#]|[^\,:=\\ \t\n#])+ {
372 /* directories can't have args... */
373 if (yytext[yyleng - 1] == '/') {
374 LEXTRACE("COMMAND ");
375 fill_cmnd(yytext, yyleng);
379 LEXTRACE("COMMAND ");
380 fill_cmnd(yytext, yyleng);
384 <INITIAL,GOTDEFS>{WORD} {
386 fill(yytext, yyleng);
387 LEXTRACE("WORD(4) ");
408 return('!'); /* return '!' */
416 } /* return newline */
418 <*>[[:blank:]]+ { /* throw away space/tabs */
419 sawspace = TRUE; /* but remember for fill_args */
422 <*>\\[[:blank:]]*\n {
423 sawspace = TRUE; /* remember for fill_args */
426 } /* throw away EOL after \ */
428 <INITIAL,STARTDEFS,INDEFS>#.*\n {
433 } /* return comments */
441 if (YY_START != INITIAL) {
451 _fill(src, len, olen)
458 dst = olen ? realloc(yylval.string, olen + len + 1) : malloc(len + 1);
460 yyerror("unable to allocate memory");
465 /* Copy the string and collapse any escaped characters. */
467 for (i = 0, j = 0; i < len; i++, j++) {
468 if (src[i] == '\\' && i != len - 1)
483 if (yylval.string != NULL)
484 olen = strlen(yylval.string);
486 _fill(src, len, olen);
494 arg_len = arg_size = 0;
496 yylval.command.cmnd = (char *) malloc(++len);
497 if (yylval.command.cmnd == NULL) {
498 yyerror("unable to allocate memory");
502 /* copy the string and NULL-terminate it (escapes handled by fnmatch) */
503 (void) strlcpy(yylval.command.cmnd, s, len);
505 yylval.command.args = NULL;
509 fill_args(s, len, addspace)
517 if (yylval.command.args == NULL) {
521 new_len = arg_len + len + addspace;
523 if (new_len >= arg_size) {
524 /* Allocate more space than we need for subsequent args */
525 while (new_len >= (arg_size += COMMANDARGINC))
528 p = yylval.command.args ?
529 (char *) realloc(yylval.command.args, arg_size) :
530 (char *) malloc(arg_size);
532 efree(yylval.command.args);
533 yyerror("unable to allocate memory");
536 yylval.command.args = p;
539 /* Efficiently append the arg (with a leading space if needed). */
540 p = yylval.command.args + arg_len;
543 if (strlcpy(p, s, arg_size - (p - yylval.command.args)) != len)
544 yyerror("fill_args: buffer overflow"); /* paranoia */
549 * Check to make sure an IPv6 address does not contain multiple instances
550 * of the string "::". Assumes strlen(s) >= 1.
551 * Returns TRUE if address is valid else FALSE.
559 for (; *s != '\0'; s++) {
560 if (s[0] == ':' && s[1] == ':') {
565 nmatch = 0; /* reset if we hit netmask */
568 return (nmatch <= 1);
575 /* Free space used by the aliases unless called by testsudoers. */