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

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