[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.17

1.17    ! tedu        1: /* $OpenBSD: parse.y,v 1.16 2016/06/05 00:46:34 djm 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.1       tedu       51:
1.4       nicm       52: void yyerror(const char *, ...);
                     53: int yylex(void);
                     54: int yyparse(void);
                     55:
1.1       tedu       56: %}
                     57:
1.7       zhuk       58: %token TPERMIT TDENY TAS TCMD TARGS
1.17    ! tedu       59: %token TNOPASS TKEEPENV
1.1       tedu       60: %token TSTRING
                     61:
                     62: %%
                     63:
                     64: grammar:       /* empty */
                     65:                | grammar '\n'
                     66:                | grammar rule '\n'
1.10      zhuk       67:                | error '\n'
1.1       tedu       68:                ;
                     69:
                     70: rule:          action ident target cmd {
                     71:                        struct rule *r;
                     72:                        r = calloc(1, sizeof(*r));
1.2       nicm       73:                        if (!r)
                     74:                                errx(1, "can't allocate rule");
1.1       tedu       75:                        r->action = $1.action;
                     76:                        r->options = $1.options;
                     77:                        r->envlist = $1.envlist;
                     78:                        r->ident = $2.str;
                     79:                        r->target = $3.str;
1.7       zhuk       80:                        r->cmd = $4.cmd;
                     81:                        r->cmdargs = $4.cmdargs;
1.1       tedu       82:                        if (nrules == maxrules) {
                     83:                                if (maxrules == 0)
                     84:                                        maxrules = 63;
                     85:                                else
                     86:                                        maxrules *= 2;
1.6       benno      87:                                if (!(rules = reallocarray(rules, maxrules,
                     88:                                    sizeof(*rules))))
1.1       tedu       89:                                        errx(1, "can't allocate rules");
                     90:                        }
                     91:                        rules[nrules++] = r;
                     92:                } ;
                     93:
                     94: action:                TPERMIT options {
                     95:                        $$.action = PERMIT;
                     96:                        $$.options = $2.options;
                     97:                        $$.envlist = $2.envlist;
                     98:                } | TDENY {
                     99:                        $$.action = DENY;
                    100:                } ;
                    101:
                    102: options:       /* none */
                    103:                | options option {
                    104:                        $$.options = $1.options | $2.options;
                    105:                        $$.envlist = $1.envlist;
                    106:                        if ($2.envlist) {
1.10      zhuk      107:                                if ($$.envlist) {
                    108:                                        yyerror("can't have two keepenv sections");
                    109:                                        YYERROR;
                    110:                                } else
1.1       tedu      111:                                        $$.envlist = $2.envlist;
                    112:                        }
                    113:                } ;
1.16      djm       114:
1.1       tedu      115: option:                TNOPASS {
                    116:                        $$.options = NOPASS;
                    117:                } | TKEEPENV {
                    118:                        $$.options = KEEPENV;
                    119:                } | TKEEPENV '{' envlist '}' {
                    120:                        $$.options = KEEPENV;
                    121:                        $$.envlist = $3.envlist;
                    122:                } ;
                    123:
                    124: envlist:       /* empty */ {
                    125:                        if (!($$.envlist = calloc(1, sizeof(char *))))
                    126:                                errx(1, "can't allocate envlist");
                    127:                } | envlist TSTRING {
                    128:                        int nenv = arraylen($1.envlist);
1.6       benno     129:                        if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
                    130:                            sizeof(char *))))
1.1       tedu      131:                                errx(1, "can't allocate envlist");
                    132:                        $$.envlist[nenv] = $2.str;
                    133:                        $$.envlist[nenv + 1] = NULL;
1.17    ! tedu      134:                } | envlist TSTRING '=' TSTRING {
        !           135:                        int nenv = arraylen($1.envlist);
1.16      djm       136:                        char *cp = NULL;
                    137:
                    138:                        if (*$2.str == '\0' || strchr($2.str, '=') != NULL) {
                    139:                                yyerror("invalid setenv expression");
                    140:                                YYERROR;
                    141:                        }
1.17    ! tedu      142:                        if (!($$.envlist = reallocarray($1.envlist,
1.16      djm       143:                            nenv + 2, sizeof(char *))))
                    144:                                errx(1, "can't allocate envlist");
1.17    ! tedu      145:                        $$.envlist[nenv] = NULL;
1.16      djm       146:                        if (asprintf(&cp, "%s=%s", $2.str, $4.str) <= 0 ||
                    147:                            cp == NULL)
                    148:                                errx(1,"asprintf failed");
1.17    ! tedu      149:                        $$.envlist[nenv] = cp;
        !           150:                        $$.envlist[nenv + 1] = NULL;
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 },
                    214:        { "keepenv", TKEEPENV },
                    215: };
                    216:
                    217: int
                    218: yylex(void)
                    219: {
                    220:        char buf[1024], *ebuf, *p, *str;
1.10      zhuk      221:        int i, c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
1.1       tedu      222:
                    223:        p = buf;
                    224:        ebuf = buf + sizeof(buf);
1.9       zhuk      225:
1.5       benno     226: repeat:
1.9       zhuk      227:        /* skip whitespace first */
                    228:        for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
1.10      zhuk      229:                yylval.colno++;
1.9       zhuk      230:
                    231:        /* check for special one-character constructions */
1.1       tedu      232:        switch (c) {
1.9       zhuk      233:                case '\n':
1.10      zhuk      234:                        yylval.colno = 0;
                    235:                        yylval.lineno++;
1.9       zhuk      236:                        /* FALLTHROUGH */
                    237:                case '{':
                    238:                case '}':
1.16      djm       239:                case '=':
1.9       zhuk      240:                        return c;
                    241:                case '#':
                    242:                        /* skip comments; NUL is allowed; no continuation */
                    243:                        while ((c = getc(yyfp)) != '\n')
                    244:                                if (c == EOF)
1.14      tedu      245:                                        goto eof;
1.10      zhuk      246:                        yylval.colno = 0;
                    247:                        yylval.lineno++;
1.9       zhuk      248:                        return c;
                    249:                case EOF:
1.14      tedu      250:                        goto eof;
1.1       tedu      251:        }
1.9       zhuk      252:
                    253:        /* parsing next word */
1.10      zhuk      254:        for (;; c = getc(yyfp), yylval.colno++) {
1.3       zhuk      255:                switch (c) {
1.9       zhuk      256:                case '\0':
1.11      deraadt   257:                        yyerror("unallowed character NUL in column %d",
                    258:                            yylval.colno + 1);
1.9       zhuk      259:                        escape = 0;
                    260:                        continue;
                    261:                case '\\':
                    262:                        escape = !escape;
                    263:                        if (escape)
                    264:                                continue;
                    265:                        break;
1.3       zhuk      266:                case '\n':
1.9       zhuk      267:                        if (quotes)
1.10      zhuk      268:                                yyerror("unterminated quotes in column %d",
                    269:                                    qpos + 1);
1.9       zhuk      270:                        if (escape) {
                    271:                                nonkw = 1;
                    272:                                escape = 0;
1.12      mikeb     273:                                yylval.colno = 0;
                    274:                                yylval.lineno++;
1.9       zhuk      275:                                continue;
                    276:                        }
                    277:                        goto eow;
                    278:                case EOF:
                    279:                        if (escape)
1.10      zhuk      280:                                yyerror("unterminated escape in column %d",
                    281:                                    yylval.colno);
1.9       zhuk      282:                        if (quotes)
1.10      zhuk      283:                                yyerror("unterminated quotes in column %d",
                    284:                                    qpos + 1);
                    285:                        goto eow;
1.9       zhuk      286:                        /* FALLTHROUGH */
1.3       zhuk      287:                case '{':
                    288:                case '}':
                    289:                case '#':
                    290:                case ' ':
                    291:                case '\t':
1.16      djm       292:                case '=':
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: }