[BACK]Return to parse.lex CVS log [TXT][DIR] Up to [local] / src / usr.bin / sudo

Annotation of src/usr.bin/sudo/parse.lex, Revision 1.2

1.1       millert     1: %{
                      2: /*
                      3:  * Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
                      4:  * All rights reserved.
                      5:  *
                      6:  * This code is derived from software contributed by Chris Jepeway
                      7:  * <jepeway@cs.utk.edu>
                      8:  *
                      9:  * This code is derived from software contributed by Chris Jepeway
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  *
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  *
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
                     20:  *
                     21:  * 3. The name of the author may not be used to endorse or promote products
                     22:  *    derived from this software without specific prior written permission.
                     23:  *
                     24:  * 4. Products derived from this software may not be called "Sudo" nor
                     25:  *    may "Sudo" appear in their names without specific prior written
                     26:  *    permission from the author.
                     27:  *
                     28:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     29:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     30:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     31:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     32:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     33:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     34:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     35:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     36:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     37:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     38:  */
                     39:
                     40: #include "config.h"
                     41:
                     42: #ifdef STDC_HEADERS
                     43: #include <stdlib.h>
                     44: #endif /* STDC_HEADERS */
                     45: #ifdef HAVE_UNISTD_H
                     46: #include <unistd.h>
                     47: #endif /* HAVE_UNISTD_H */
                     48: #ifdef HAVE_STRING_H
                     49: #include <string.h>
                     50: #endif /* HAVE_STRING_H */
                     51: #ifdef HAVE_STRINGS_H
                     52: #include <strings.h>
                     53: #endif /* HAVE_STRINGS_H */
                     54: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
                     55: #include <malloc.h>
                     56: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
                     57: #include <ctype.h>
                     58: #include <sys/types.h>
                     59: #include <sys/param.h>
                     60: #include "sudo.h"
                     61: #include "parse.h"
                     62: #include "sudo.tab.h"
                     63:
                     64: #ifndef lint
1.2     ! millert    65: static const char rcsid[] = "$Sudo: parse.lex,v 1.110 1999/12/06 00:05:53 millert Exp $";
1.1       millert    66: #endif /* lint */
                     67:
                     68: #undef yywrap          /* guard against a yywrap macro */
                     69:
                     70: extern YYSTYPE yylval;
                     71: extern int clearaliases;
                     72: int sudolineno = 1;
                     73: static int sawspace = 0;
                     74: static int arg_len = 0;
                     75: static int arg_size = 0;
                     76:
                     77: static void fill               __P((char *, int));
                     78: static void fill_cmnd          __P((char *, int));
                     79: static void fill_args          __P((char *, int, int));
                     80: extern void reset_aliases      __P((void));
                     81: extern void yyerror            __P((char *));
                     82:
                     83: /* realloc() to size + COMMANDARGINC to make room for command args */
                     84: #define COMMANDARGINC  64
                     85:
                     86: #ifdef TRACELEXER
                     87: #define LEXTRACE(msg)  fputs(msg, stderr)
                     88: #else
                     89: #define LEXTRACE(msg)
                     90: #endif
                     91: %}
                     92:
                     93: OCTET                  (1?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5])
                     94: DOTTEDQUAD             {OCTET}(\.{OCTET}){3}
                     95: HOSTNAME               [[:alnum:]_-]+
                     96: WORD                   ([^@!=:,\(\) \t\n\\]|\\[^\n])+
                     97:
                     98: %s     GOTCMND
                     99: %s     GOTRUNAS
                    100: %s     GOTDEFS
                    101:
                    102: %%
                    103: [ \t]+                 {                       /* throw away space/tabs */
                    104:                            sawspace = TRUE;    /* but remember for fill_args */
                    105:                        }
                    106:
                    107: \\[ \t]*\n             {
                    108:                            sawspace = TRUE;    /* remember for fill_args */
                    109:                            ++sudolineno;
                    110:                            LEXTRACE("\n\t");
                    111:                        }                       /* throw away EOL after \ */
                    112:
                    113: <GOTCMND>\\[:\,=\\ \t] {
                    114:                            LEXTRACE("QUOTEDCHAR ");
                    115:                            fill_args(yytext + 1, 1, sawspace);
                    116:                            sawspace = FALSE;
                    117:                        }
                    118:
                    119: <GOTDEFS>\"([^\"]|\\\")+\"     {
                    120:                            LEXTRACE("WORD(1) ");
                    121:                            fill(yytext + 1, yyleng - 2);
                    122:                            return(WORD);
                    123:                        }
                    124:
                    125: <GOTDEFS>(#.*)?\n      {
                    126:                            BEGIN INITIAL;
                    127:                            ++sudolineno;
                    128:                            LEXTRACE("\n");
                    129:                            return(COMMENT);
                    130:                        }
                    131:
                    132: <GOTCMND>[:\,=\n]      {
                    133:                            BEGIN INITIAL;
                    134:                            unput(*yytext);
                    135:                            return(COMMAND);
                    136:                        }                       /* end of command line args */
                    137:
                    138: \n                     {
                    139:                            ++sudolineno;
                    140:                            LEXTRACE("\n");
1.2     ! millert   141:                            BEGIN INITIAL;
1.1       millert   142:                            return(COMMENT);
                    143:                        }                       /* return newline */
                    144:
                    145: <INITIAL>#.*\n         {
                    146:                            ++sudolineno;
                    147:                            LEXTRACE("\n");
                    148:                            return(COMMENT);
                    149:                        }                       /* return comments */
                    150:
                    151: <GOTCMND>[^\\:, \t\n]+ {
                    152:                            LEXTRACE("ARG ");
                    153:                            fill_args(yytext, yyleng, sawspace);
                    154:                            sawspace = FALSE;
                    155:                          }                     /* a command line arg */
                    156:
                    157: ,                      {
                    158:                            LEXTRACE(", ");
                    159:                            return(',');
                    160:                        }                       /* return ',' */
                    161:
                    162: !+                     {
                    163:                            if (yyleng % 2 == 1)
                    164:                                return('!');    /* return '!' */
                    165:                        }
                    166:
                    167: =                      {
                    168:                            LEXTRACE("= ");
                    169:                            return('=');
                    170:                        }                       /* return '=' */
                    171:
                    172: :                      {
                    173:                            LEXTRACE(": ");
                    174:                            return(':');
                    175:                        }                       /* return ':' */
                    176:
                    177: NOPASSWD[[:blank:]]*:  {
                    178:                                /* cmnd does not require passwd for this user */
                    179:                                LEXTRACE("NOPASSWD ");
                    180:                                return(NOPASSWD);
                    181:                        }
                    182:
                    183: PASSWD[[:blank:]]*:    {
                    184:                                /* cmnd requires passwd for this user */
                    185:                                LEXTRACE("PASSWD ");
                    186:                                return(PASSWD);
                    187:                        }
                    188:
                    189: \+{WORD}               {
                    190:                            /* netgroup */
                    191:                            fill(yytext, yyleng);
                    192:                            LEXTRACE("NETGROUP ");
                    193:                            return(NETGROUP);
                    194:                        }
                    195:
                    196: \%{WORD}               {
                    197:                            /* UN*X group */
                    198:                            fill(yytext, yyleng);
                    199:                            LEXTRACE("GROUP ");
                    200:                            return(USERGROUP);
                    201:                        }
                    202:
                    203: {DOTTEDQUAD}(\/{DOTTEDQUAD})? {
                    204:                            fill(yytext, yyleng);
                    205:                            LEXTRACE("NTWKADDR ");
                    206:                            return(NTWKADDR);
                    207:                        }
                    208:
                    209: {DOTTEDQUAD}\/([12][0-9]*|3[0-2]*) {
                    210:                            fill(yytext, yyleng);
                    211:                            LEXTRACE("NTWKADDR ");
                    212:                            return(NTWKADDR);
                    213:                        }
                    214:
                    215: [[:alpha:]][[:alnum:]_-]*(\.{HOSTNAME})+ {
                    216:                            fill(yytext, yyleng);
                    217:                            LEXTRACE("FQHOST ");
                    218:                            return(FQHOST);
                    219:                        }
                    220:
                    221: <INITIAL>\(            {
                    222:                                BEGIN GOTRUNAS;
                    223:                                LEXTRACE("RUNAS ");
                    224:                                return (RUNAS);
                    225:                        }
                    226:
                    227: <GOTRUNAS>[[:upper:]][[:upper:][:digit:]_]* {
                    228:                            /* Runas_Alias user can run command as or ALL */
                    229:                            if (strcmp(yytext, "ALL") == 0) {
                    230:                                LEXTRACE("ALL ");
                    231:                                return(ALL);
                    232:                            } else {
                    233:                                fill(yytext, yyleng);
                    234:                                LEXTRACE("ALIAS ");
                    235:                                return(ALIAS);
                    236:                            }
                    237:                        }
                    238:
                    239: <GOTRUNAS>#?{WORD}     {
                    240:                            /* username/uid that user can run command as */
                    241:                            fill(yytext, yyleng);
                    242:                            LEXTRACE("WORD(2) ");
                    243:                            return(WORD);
                    244:                        }
                    245:
                    246: <GOTRUNAS>\)           {
                    247:                            BEGIN INITIAL;
                    248:                        }
                    249:
                    250: [[:upper:]][[:upper:][:digit:]_]*      {
                    251:                            if (strcmp(yytext, "ALL") == 0) {
                    252:                                LEXTRACE("ALL ");
                    253:                                return(ALL);
                    254:                            } else {
                    255:                                fill(yytext, yyleng);
                    256:                                LEXTRACE("ALIAS ");
                    257:                                return(ALIAS);
                    258:                            }
                    259:                        }
                    260:
1.2     ! millert   261: <GOTDEFS>{WORD}                {
1.1       millert   262:                            LEXTRACE("WORD(3) ");
                    263:                            fill(yytext, yyleng);
                    264:                            return(WORD);
                    265:                        }
                    266:
                    267: <INITIAL>^Defaults[:@]?        {
                    268:                            BEGIN GOTDEFS;
                    269:                            if (yyleng == 9) {
                    270:                                switch (yytext[8]) {
                    271:                                    case ':' :
                    272:                                        LEXTRACE("DEFAULTS_USER ");
                    273:                                        return(DEFAULTS_USER);
                    274:                                    case '@' :
                    275:                                        LEXTRACE("DEFAULTS_HOST ");
                    276:                                        return(DEFAULTS_HOST);
                    277:                                }
                    278:                            } else {
                    279:                                LEXTRACE("DEFAULTS ");
                    280:                                return(DEFAULTS);
                    281:                            }
                    282:                        }
                    283:
                    284: <INITIAL>^(Host|Cmnd|User|Runas)_Alias {
                    285:                            fill(yytext, yyleng);
                    286:                            if (*yytext == 'H') {
                    287:                                LEXTRACE("HOSTALIAS ");
                    288:                                return(HOSTALIAS);
                    289:                            }
                    290:                            if (*yytext == 'C') {
                    291:                                LEXTRACE("CMNDALIAS ");
                    292:                                return(CMNDALIAS);
                    293:                            }
                    294:                            if (*yytext == 'U') {
                    295:                                LEXTRACE("USERALIAS ");
                    296:                                return(USERALIAS);
                    297:                            }
                    298:                            if (*yytext == 'R') {
                    299:                                LEXTRACE("RUNASALIAS ");
1.2     ! millert   300:                                BEGIN GOTRUNAS;
1.1       millert   301:                                return(RUNASALIAS);
                    302:                            }
                    303:                        }
                    304:
                    305: \/[^\,:=\\ \t\n#]+     {
                    306:                            /* directories can't have args... */
                    307:                            if (yytext[yyleng - 1] == '/') {
                    308:                                LEXTRACE("COMMAND ");
                    309:                                fill_cmnd(yytext, yyleng);
                    310:                                return(COMMAND);
                    311:                            } else {
                    312:                                BEGIN GOTCMND;
                    313:                                LEXTRACE("COMMAND ");
                    314:                                fill_cmnd(yytext, yyleng);
                    315:                            }
                    316:                        }                       /* a pathname */
                    317:
                    318: <INITIAL>{WORD}                {
                    319:                            /* a word */
                    320:                            fill(yytext, yyleng);
                    321:                            LEXTRACE("WORD(4) ");
                    322:                            return(WORD);
                    323:                        }
                    324:
                    325: .                      {
                    326:                            LEXTRACE("ERROR ");
                    327:                            return(ERROR);
                    328:                        }       /* parse error */
                    329:
                    330: %%
                    331: static void
                    332: fill(s, len)
                    333:     char *s;
                    334:     int len;
                    335: {
                    336:     int i, j;
                    337:
                    338:     yylval.string = (char *) malloc(len + 1);
                    339:     if (yylval.string == NULL)
                    340:        yyerror("unable to allocate memory");
                    341:
                    342:     /* Copy the string and collapse any escaped characters. */
                    343:     for (i = 0, j = 0; i < len; i++, j++) {
                    344:        if (s[i] == '\\' && i != len - 1)
                    345:            yylval.string[j] = s[++i];
                    346:        else
                    347:            yylval.string[j] = s[i];
                    348:     }
                    349:     yylval.string[j] = '\0';
                    350: }
                    351:
                    352: static void
                    353: fill_cmnd(s, len)
                    354:     char *s;
                    355:     int len;
                    356: {
                    357:     arg_len = arg_size = 0;
                    358:
                    359:     yylval.command.cmnd = (char *) malloc(len + 1);
                    360:     if (yylval.command.cmnd == NULL)
                    361:        yyerror("unable to allocate memory");
                    362:
                    363:     /* copy the string and NULL-terminate it */
                    364:     (void) strncpy(yylval.command.cmnd, s, len);
                    365:     yylval.command.cmnd[len] = '\0';
                    366:
                    367:     yylval.command.args = NULL;
                    368: }
                    369:
                    370: static void
                    371: fill_args(s, len, addspace)
                    372:     char *s;
                    373:     int len;
                    374:     int addspace;
                    375: {
                    376:     int new_len;
                    377:     char *p;
                    378:
                    379:     /*
                    380:      * If first arg, malloc() some room, else if we don't
                    381:      * have enough space realloc() some more.
                    382:      */
                    383:     if (yylval.command.args == NULL) {
                    384:        addspace = 0;
                    385:        new_len = len;
                    386:
                    387:        while (new_len >= (arg_size += COMMANDARGINC))
                    388:            ;
                    389:
                    390:        yylval.command.args = (char *) malloc(arg_size);
                    391:        if (yylval.command.args == NULL)
                    392:            yyerror("unable to allocate memory");
                    393:     } else {
                    394:        new_len = arg_len + len + addspace;
                    395:
                    396:        if (new_len >= arg_size) {
                    397:            /* Allocate more space than we need for subsequent args */
                    398:            while (new_len >= (arg_size += COMMANDARGINC))
                    399:                ;
                    400:
                    401:            if ((p = (char *) realloc(yylval.command.args, arg_size)) == NULL) {
                    402:                free(yylval.command.args);
                    403:                yyerror("unable to allocate memory");
                    404:            } else
                    405:                yylval.command.args = p;
                    406:        }
                    407:     }
                    408:
                    409:     /* Efficiently append the arg (with a leading space if needed). */
                    410:     p = yylval.command.args + arg_len;
                    411:     if (addspace)
                    412:        *p++ = ' ';
                    413:     (void) strcpy(p, s);
                    414:     arg_len = new_len;
                    415: }
                    416:
                    417: int
                    418: yywrap()
                    419: {
                    420:
                    421:     /* Free space used by the aliases unless called by testsudoers. */
                    422:     if (clearaliases)
                    423:        reset_aliases();
                    424:
                    425:     return(TRUE);
                    426: }