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

Annotation of src/usr.bin/doas/parse.y, Revision 1.20

1.20    ! tedu        1: /* $OpenBSD: parse.y,v 1.19 2016/06/27 15:41:17 tedu Exp $ */
1.1       tedu        2: /*
                      3:  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
                     18: %{
                     19: #include <sys/types.h>
                     20: #include <ctype.h>
                     21: #include <unistd.h>
                     22: #include <stdint.h>
                     23: #include <stdarg.h>
                     24: #include <stdio.h>
                     25: #include <string.h>
                     26: #include <err.h>
                     27:
                     28: #include "doas.h"
                     29:
                     30: typedef struct {
                     31:        union {
                     32:                struct {
                     33:                        int action;
                     34:                        int options;
1.7       zhuk       35:                        const char *cmd;
                     36:                        const char **cmdargs;
1.1       tedu       37:                        const char **envlist;
                     38:                };
                     39:                const char *str;
                     40:        };
1.10      zhuk       41:        int lineno;
                     42:        int colno;
1.1       tedu       43: } yystype;
                     44: #define YYSTYPE yystype
                     45:
                     46: FILE *yyfp;
                     47:
                     48: struct rule **rules;
                     49: int nrules, maxrules;
1.10      zhuk       50: int parse_errors = 0;
1.19      tedu       51: int obsolete_warned = 0;
1.1       tedu       52:
1.4       nicm       53: void yyerror(const char *, ...);
                     54: int yylex(void);
                     55: int yyparse(void);
                     56:
1.1       tedu       57: %}
                     58:
1.7       zhuk       59: %token TPERMIT TDENY TAS TCMD TARGS
1.20    ! tedu       60: %token TNOPASS TPERSIST TKEEPENV TSETENV
1.1       tedu       61: %token TSTRING
                     62:
                     63: %%
                     64:
                     65: grammar:       /* empty */
                     66:                | grammar '\n'
                     67:                | grammar rule '\n'
1.10      zhuk       68:                | error '\n'
1.1       tedu       69:                ;
                     70:
                     71: rule:          action ident target cmd {
                     72:                        struct rule *r;
                     73:                        r = calloc(1, sizeof(*r));
1.2       nicm       74:                        if (!r)
                     75:                                errx(1, "can't allocate rule");
1.1       tedu       76:                        r->action = $1.action;
                     77:                        r->options = $1.options;
                     78:                        r->envlist = $1.envlist;
                     79:                        r->ident = $2.str;
                     80:                        r->target = $3.str;
1.7       zhuk       81:                        r->cmd = $4.cmd;
                     82:                        r->cmdargs = $4.cmdargs;
1.1       tedu       83:                        if (nrules == maxrules) {
                     84:                                if (maxrules == 0)
                     85:                                        maxrules = 63;
                     86:                                else
                     87:                                        maxrules *= 2;
1.6       benno      88:                                if (!(rules = reallocarray(rules, maxrules,
                     89:                                    sizeof(*rules))))
1.1       tedu       90:                                        errx(1, "can't allocate rules");
                     91:                        }
                     92:                        rules[nrules++] = r;
                     93:                } ;
                     94:
                     95: action:                TPERMIT options {
                     96:                        $$.action = PERMIT;
                     97:                        $$.options = $2.options;
                     98:                        $$.envlist = $2.envlist;
                     99:                } | TDENY {
                    100:                        $$.action = DENY;
1.19      tedu      101:                        $$.options = 0;
                    102:                        $$.envlist = NULL;
1.1       tedu      103:                } ;
                    104:
1.19      tedu      105: options:       /* none */ {
                    106:                        $$.options = 0;
                    107:                        $$.envlist = NULL;
                    108:                } | options option {
1.1       tedu      109:                        $$.options = $1.options | $2.options;
                    110:                        $$.envlist = $1.envlist;
                    111:                        if ($2.envlist) {
1.10      zhuk      112:                                if ($$.envlist) {
1.19      tedu      113:                                        yyerror("can't have two setenv sections");
1.10      zhuk      114:                                        YYERROR;
                    115:                                } else
1.1       tedu      116:                                        $$.envlist = $2.envlist;
                    117:                        }
                    118:                } ;
                    119: option:                TNOPASS {
                    120:                        $$.options = NOPASS;
1.19      tedu      121:                        $$.envlist = NULL;
1.20    ! tedu      122:                } | TPERSIST {
        !           123:                        $$.options = PERSIST;
        !           124:                        $$.envlist = NULL;
1.1       tedu      125:                } | TKEEPENV {
                    126:                        $$.options = KEEPENV;
1.19      tedu      127:                        $$.envlist = NULL;
1.1       tedu      128:                } | TKEEPENV '{' envlist '}' {
1.19      tedu      129:                        $$.options = 0;
                    130:                        if (!obsolete_warned) {
                    131:                                warnx("keepenv with list is obsolete");
                    132:                                obsolete_warned = 1;
                    133:                        }
                    134:                        $$.envlist = $3.envlist;
                    135:                } | TSETENV '{' envlist '}' {
                    136:                        $$.options = 0;
1.1       tedu      137:                        $$.envlist = $3.envlist;
                    138:                } ;
                    139:
                    140: envlist:       /* empty */ {
                    141:                        if (!($$.envlist = calloc(1, sizeof(char *))))
                    142:                                errx(1, "can't allocate envlist");
                    143:                } | envlist TSTRING {
                    144:                        int nenv = arraylen($1.envlist);
1.6       benno     145:                        if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
                    146:                            sizeof(char *))))
1.1       tedu      147:                                errx(1, "can't allocate envlist");
                    148:                        $$.envlist[nenv] = $2.str;
                    149:                        $$.envlist[nenv + 1] = NULL;
1.18      tedu      150:                }
1.16      djm       151:
                    152:
1.1       tedu      153: ident:         TSTRING {
                    154:                        $$.str = $1.str;
                    155:                } ;
                    156:
                    157: target:                /* optional */ {
                    158:                        $$.str = NULL;
                    159:                } | TAS TSTRING {
                    160:                        $$.str = $2.str;
                    161:                } ;
                    162:
                    163: cmd:           /* optional */ {
1.7       zhuk      164:                        $$.cmd = NULL;
                    165:                        $$.cmdargs = NULL;
                    166:                } | TCMD TSTRING args {
                    167:                        $$.cmd = $2.str;
                    168:                        $$.cmdargs = $3.cmdargs;
                    169:                } ;
                    170:
                    171: args:          /* empty */ {
                    172:                        $$.cmdargs = NULL;
                    173:                } | TARGS argslist {
                    174:                        $$.cmdargs = $2.cmdargs;
                    175:                } ;
                    176:
                    177: argslist:      /* empty */ {
                    178:                        if (!($$.cmdargs = calloc(1, sizeof(char *))))
                    179:                                errx(1, "can't allocate args");
                    180:                } | argslist TSTRING {
                    181:                        int nargs = arraylen($1.cmdargs);
1.11      deraadt   182:                        if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
                    183:                            sizeof(char *))))
1.7       zhuk      184:                                errx(1, "can't allocate args");
                    185:                        $$.cmdargs[nargs] = $2.str;
                    186:                        $$.cmdargs[nargs + 1] = NULL;
1.1       tedu      187:                } ;
                    188:
                    189: %%
                    190:
                    191: void
                    192: yyerror(const char *fmt, ...)
                    193: {
                    194:        va_list va;
                    195:
1.15      gsoares   196:        fprintf(stderr, "doas: ");
1.1       tedu      197:        va_start(va, fmt);
1.10      zhuk      198:        vfprintf(stderr, fmt, va);
                    199:        va_end(va);
                    200:        fprintf(stderr, " at line %d\n", yylval.lineno + 1);
                    201:        parse_errors++;
1.1       tedu      202: }
                    203:
                    204: struct keyword {
                    205:        const char *word;
                    206:        int token;
                    207: } keywords[] = {
                    208:        { "deny", TDENY },
                    209:        { "permit", TPERMIT },
                    210:        { "as", TAS },
                    211:        { "cmd", TCMD },
1.7       zhuk      212:        { "args", TARGS },
1.1       tedu      213:        { "nopass", TNOPASS },
1.20    ! tedu      214:        { "persist", TPERSIST },
1.1       tedu      215:        { "keepenv", TKEEPENV },
1.19      tedu      216:        { "setenv", TSETENV },
1.1       tedu      217: };
                    218:
                    219: int
                    220: yylex(void)
                    221: {
                    222:        char buf[1024], *ebuf, *p, *str;
1.10      zhuk      223:        int i, c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
1.1       tedu      224:
                    225:        p = buf;
                    226:        ebuf = buf + sizeof(buf);
1.9       zhuk      227:
1.5       benno     228: repeat:
1.9       zhuk      229:        /* skip whitespace first */
                    230:        for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
1.10      zhuk      231:                yylval.colno++;
1.9       zhuk      232:
                    233:        /* check for special one-character constructions */
1.1       tedu      234:        switch (c) {
1.9       zhuk      235:                case '\n':
1.10      zhuk      236:                        yylval.colno = 0;
                    237:                        yylval.lineno++;
1.9       zhuk      238:                        /* FALLTHROUGH */
                    239:                case '{':
                    240:                case '}':
                    241:                        return c;
                    242:                case '#':
                    243:                        /* skip comments; NUL is allowed; no continuation */
                    244:                        while ((c = getc(yyfp)) != '\n')
                    245:                                if (c == EOF)
1.14      tedu      246:                                        goto eof;
1.10      zhuk      247:                        yylval.colno = 0;
                    248:                        yylval.lineno++;
1.9       zhuk      249:                        return c;
                    250:                case EOF:
1.14      tedu      251:                        goto eof;
1.1       tedu      252:        }
1.9       zhuk      253:
                    254:        /* parsing next word */
1.10      zhuk      255:        for (;; c = getc(yyfp), yylval.colno++) {
1.3       zhuk      256:                switch (c) {
1.9       zhuk      257:                case '\0':
1.11      deraadt   258:                        yyerror("unallowed character NUL in column %d",
                    259:                            yylval.colno + 1);
1.9       zhuk      260:                        escape = 0;
                    261:                        continue;
                    262:                case '\\':
                    263:                        escape = !escape;
                    264:                        if (escape)
                    265:                                continue;
                    266:                        break;
1.3       zhuk      267:                case '\n':
1.9       zhuk      268:                        if (quotes)
1.10      zhuk      269:                                yyerror("unterminated quotes in column %d",
                    270:                                    qpos + 1);
1.9       zhuk      271:                        if (escape) {
                    272:                                nonkw = 1;
                    273:                                escape = 0;
1.12      mikeb     274:                                yylval.colno = 0;
                    275:                                yylval.lineno++;
1.9       zhuk      276:                                continue;
                    277:                        }
                    278:                        goto eow;
                    279:                case EOF:
                    280:                        if (escape)
1.10      zhuk      281:                                yyerror("unterminated escape in column %d",
                    282:                                    yylval.colno);
1.9       zhuk      283:                        if (quotes)
1.10      zhuk      284:                                yyerror("unterminated quotes in column %d",
                    285:                                    qpos + 1);
                    286:                        goto eow;
1.9       zhuk      287:                        /* FALLTHROUGH */
1.3       zhuk      288:                case '{':
                    289:                case '}':
                    290:                case '#':
                    291:                case ' ':
                    292:                case '\t':
1.9       zhuk      293:                        if (!escape && !quotes)
                    294:                                goto eow;
                    295:                        break;
                    296:                case '"':
                    297:                        if (!escape) {
                    298:                                quotes = !quotes;
                    299:                                if (quotes) {
                    300:                                        nonkw = 1;
1.10      zhuk      301:                                        qpos = yylval.colno;
1.9       zhuk      302:                                }
                    303:                                continue;
                    304:                        }
1.3       zhuk      305:                }
1.1       tedu      306:                *p++ = c;
1.13      tedu      307:                if (p == ebuf) {
1.10      zhuk      308:                        yyerror("too long line");
1.13      tedu      309:                        p = buf;
                    310:                }
1.9       zhuk      311:                escape = 0;
1.1       tedu      312:        }
1.9       zhuk      313:
1.3       zhuk      314: eow:
1.1       tedu      315:        *p = 0;
                    316:        if (c != EOF)
                    317:                ungetc(c, yyfp);
1.9       zhuk      318:        if (p == buf) {
                    319:                /*
1.11      deraadt   320:                 * There could be a number of reasons for empty buffer,
                    321:                 * and we handle all of them here, to avoid cluttering
                    322:                 * the main loop.
1.9       zhuk      323:                 */
                    324:                if (c == EOF)
1.14      tedu      325:                        goto eof;
1.10      zhuk      326:                else if (qpos == -1)    /* accept, e.g., empty args: cmd foo args "" */
1.9       zhuk      327:                        goto repeat;
                    328:        }
                    329:        if (!nonkw) {
                    330:                for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
                    331:                        if (strcmp(buf, keywords[i].word) == 0)
                    332:                                return keywords[i].token;
                    333:                }
1.1       tedu      334:        }
                    335:        if ((str = strdup(buf)) == NULL)
                    336:                err(1, "strdup");
                    337:        yylval.str = str;
                    338:        return TSTRING;
1.14      tedu      339:
                    340: eof:
                    341:        if (ferror(yyfp))
                    342:                yyerror("input error reading config");
                    343:        return 0;
1.1       tedu      344: }