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

1.19    ! tedu        1: /* $OpenBSD: parse.y,v 1.18 2016/06/07 16:49:23 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.19    ! tedu       60: %token TNOPASS 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.1       tedu      122:                } | TKEEPENV {
                    123:                        $$.options = KEEPENV;
1.19    ! tedu      124:                        $$.envlist = NULL;
1.1       tedu      125:                } | TKEEPENV '{' envlist '}' {
1.19    ! tedu      126:                        $$.options = 0;
        !           127:                        if (!obsolete_warned) {
        !           128:                                warnx("keepenv with list is obsolete");
        !           129:                                obsolete_warned = 1;
        !           130:                        }
        !           131:                        $$.envlist = $3.envlist;
        !           132:                } | TSETENV '{' envlist '}' {
        !           133:                        $$.options = 0;
1.1       tedu      134:                        $$.envlist = $3.envlist;
                    135:                } ;
                    136:
                    137: envlist:       /* empty */ {
                    138:                        if (!($$.envlist = calloc(1, sizeof(char *))))
                    139:                                errx(1, "can't allocate envlist");
                    140:                } | envlist TSTRING {
                    141:                        int nenv = arraylen($1.envlist);
1.6       benno     142:                        if (!($$.envlist = reallocarray($1.envlist, nenv + 2,
                    143:                            sizeof(char *))))
1.1       tedu      144:                                errx(1, "can't allocate envlist");
                    145:                        $$.envlist[nenv] = $2.str;
                    146:                        $$.envlist[nenv + 1] = NULL;
1.18      tedu      147:                }
1.16      djm       148:
                    149:
1.1       tedu      150: ident:         TSTRING {
                    151:                        $$.str = $1.str;
                    152:                } ;
                    153:
                    154: target:                /* optional */ {
                    155:                        $$.str = NULL;
                    156:                } | TAS TSTRING {
                    157:                        $$.str = $2.str;
                    158:                } ;
                    159:
                    160: cmd:           /* optional */ {
1.7       zhuk      161:                        $$.cmd = NULL;
                    162:                        $$.cmdargs = NULL;
                    163:                } | TCMD TSTRING args {
                    164:                        $$.cmd = $2.str;
                    165:                        $$.cmdargs = $3.cmdargs;
                    166:                } ;
                    167:
                    168: args:          /* empty */ {
                    169:                        $$.cmdargs = NULL;
                    170:                } | TARGS argslist {
                    171:                        $$.cmdargs = $2.cmdargs;
                    172:                } ;
                    173:
                    174: argslist:      /* empty */ {
                    175:                        if (!($$.cmdargs = calloc(1, sizeof(char *))))
                    176:                                errx(1, "can't allocate args");
                    177:                } | argslist TSTRING {
                    178:                        int nargs = arraylen($1.cmdargs);
1.11      deraadt   179:                        if (!($$.cmdargs = reallocarray($1.cmdargs, nargs + 2,
                    180:                            sizeof(char *))))
1.7       zhuk      181:                                errx(1, "can't allocate args");
                    182:                        $$.cmdargs[nargs] = $2.str;
                    183:                        $$.cmdargs[nargs + 1] = NULL;
1.1       tedu      184:                } ;
                    185:
                    186: %%
                    187:
                    188: void
                    189: yyerror(const char *fmt, ...)
                    190: {
                    191:        va_list va;
                    192:
1.15      gsoares   193:        fprintf(stderr, "doas: ");
1.1       tedu      194:        va_start(va, fmt);
1.10      zhuk      195:        vfprintf(stderr, fmt, va);
                    196:        va_end(va);
                    197:        fprintf(stderr, " at line %d\n", yylval.lineno + 1);
                    198:        parse_errors++;
1.1       tedu      199: }
                    200:
                    201: struct keyword {
                    202:        const char *word;
                    203:        int token;
                    204: } keywords[] = {
                    205:        { "deny", TDENY },
                    206:        { "permit", TPERMIT },
                    207:        { "as", TAS },
                    208:        { "cmd", TCMD },
1.7       zhuk      209:        { "args", TARGS },
1.1       tedu      210:        { "nopass", TNOPASS },
                    211:        { "keepenv", TKEEPENV },
1.19    ! tedu      212:        { "setenv", TSETENV },
1.1       tedu      213: };
                    214:
                    215: int
                    216: yylex(void)
                    217: {
                    218:        char buf[1024], *ebuf, *p, *str;
1.10      zhuk      219:        int i, c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
1.1       tedu      220:
                    221:        p = buf;
                    222:        ebuf = buf + sizeof(buf);
1.9       zhuk      223:
1.5       benno     224: repeat:
1.9       zhuk      225:        /* skip whitespace first */
                    226:        for (c = getc(yyfp); c == ' ' || c == '\t'; c = getc(yyfp))
1.10      zhuk      227:                yylval.colno++;
1.9       zhuk      228:
                    229:        /* check for special one-character constructions */
1.1       tedu      230:        switch (c) {
1.9       zhuk      231:                case '\n':
1.10      zhuk      232:                        yylval.colno = 0;
                    233:                        yylval.lineno++;
1.9       zhuk      234:                        /* FALLTHROUGH */
                    235:                case '{':
                    236:                case '}':
                    237:                        return c;
                    238:                case '#':
                    239:                        /* skip comments; NUL is allowed; no continuation */
                    240:                        while ((c = getc(yyfp)) != '\n')
                    241:                                if (c == EOF)
1.14      tedu      242:                                        goto eof;
1.10      zhuk      243:                        yylval.colno = 0;
                    244:                        yylval.lineno++;
1.9       zhuk      245:                        return c;
                    246:                case EOF:
1.14      tedu      247:                        goto eof;
1.1       tedu      248:        }
1.9       zhuk      249:
                    250:        /* parsing next word */
1.10      zhuk      251:        for (;; c = getc(yyfp), yylval.colno++) {
1.3       zhuk      252:                switch (c) {
1.9       zhuk      253:                case '\0':
1.11      deraadt   254:                        yyerror("unallowed character NUL in column %d",
                    255:                            yylval.colno + 1);
1.9       zhuk      256:                        escape = 0;
                    257:                        continue;
                    258:                case '\\':
                    259:                        escape = !escape;
                    260:                        if (escape)
                    261:                                continue;
                    262:                        break;
1.3       zhuk      263:                case '\n':
1.9       zhuk      264:                        if (quotes)
1.10      zhuk      265:                                yyerror("unterminated quotes in column %d",
                    266:                                    qpos + 1);
1.9       zhuk      267:                        if (escape) {
                    268:                                nonkw = 1;
                    269:                                escape = 0;
1.12      mikeb     270:                                yylval.colno = 0;
                    271:                                yylval.lineno++;
1.9       zhuk      272:                                continue;
                    273:                        }
                    274:                        goto eow;
                    275:                case EOF:
                    276:                        if (escape)
1.10      zhuk      277:                                yyerror("unterminated escape in column %d",
                    278:                                    yylval.colno);
1.9       zhuk      279:                        if (quotes)
1.10      zhuk      280:                                yyerror("unterminated quotes in column %d",
                    281:                                    qpos + 1);
                    282:                        goto eow;
1.9       zhuk      283:                        /* FALLTHROUGH */
1.3       zhuk      284:                case '{':
                    285:                case '}':
                    286:                case '#':
                    287:                case ' ':
                    288:                case '\t':
1.9       zhuk      289:                        if (!escape && !quotes)
                    290:                                goto eow;
                    291:                        break;
                    292:                case '"':
                    293:                        if (!escape) {
                    294:                                quotes = !quotes;
                    295:                                if (quotes) {
                    296:                                        nonkw = 1;
1.10      zhuk      297:                                        qpos = yylval.colno;
1.9       zhuk      298:                                }
                    299:                                continue;
                    300:                        }
1.3       zhuk      301:                }
1.1       tedu      302:                *p++ = c;
1.13      tedu      303:                if (p == ebuf) {
1.10      zhuk      304:                        yyerror("too long line");
1.13      tedu      305:                        p = buf;
                    306:                }
1.9       zhuk      307:                escape = 0;
1.1       tedu      308:        }
1.9       zhuk      309:
1.3       zhuk      310: eow:
1.1       tedu      311:        *p = 0;
                    312:        if (c != EOF)
                    313:                ungetc(c, yyfp);
1.9       zhuk      314:        if (p == buf) {
                    315:                /*
1.11      deraadt   316:                 * There could be a number of reasons for empty buffer,
                    317:                 * and we handle all of them here, to avoid cluttering
                    318:                 * the main loop.
1.9       zhuk      319:                 */
                    320:                if (c == EOF)
1.14      tedu      321:                        goto eof;
1.10      zhuk      322:                else if (qpos == -1)    /* accept, e.g., empty args: cmd foo args "" */
1.9       zhuk      323:                        goto repeat;
                    324:        }
                    325:        if (!nonkw) {
                    326:                for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
                    327:                        if (strcmp(buf, keywords[i].word) == 0)
                    328:                                return keywords[i].token;
                    329:                }
1.1       tedu      330:        }
                    331:        if ((str = strdup(buf)) == NULL)
                    332:                err(1, "strdup");
                    333:        yylval.str = str;
                    334:        return TSTRING;
1.14      tedu      335:
                    336: eof:
                    337:        if (ferror(yyfp))
                    338:                yyerror("input error reading config");
                    339:        return 0;
1.1       tedu      340: }