Blob


1 /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
2 %{
4 /*
5 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
20 #include <err.h>
21 #include <stdio.h>
23 #include "gmid.h"
25 /*
26 * #define YYDEBUG 1
27 * int yydebug = 1;
28 */
30 struct vhost *host = &hosts[0];
31 size_t ihost = 0;
33 struct location *loc = &hosts[0].locations[0];
34 size_t iloc = 0;
36 int goterror = 0;
37 const char *config_path;
39 void yyerror(const char*);
40 int parse_portno(const char*);
41 void parse_conf(const char*);
43 %}
45 /* for bison: */
46 /* %define parse.error verbose */
48 %union {
49 char *str;
50 int num;
51 }
53 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
54 %token TCHROOT TUSER TSERVER
55 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
56 %token TERR
58 %token <str> TSTRING
59 %token <num> TNUM
60 %token <num> TBOOL
62 %%
64 conf : options vhosts ;
66 options : /* empty */
67 | options option
68 ;
70 option : TIPV6 TBOOL { conf.ipv6 = $2; }
71 | TPORT TNUM { conf.port = $2; }
72 | TPROTOCOLS TSTRING {
73 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
74 errx(1, "invalid protocols string \"%s\"", $2);
75 }
76 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
77 | TCHROOT TSTRING { conf.chroot = $2; }
78 | TUSER TSTRING { conf.user = $2; }
79 ;
81 vhosts : /* empty */
82 | vhosts vhost
83 ;
85 vhost : TSERVER TSTRING '{' servopts locations '}' {
86 host->locations[0].match = (char*)"*";
87 host->domain = $2;
89 if (strstr($2, "xn--") != NULL) {
90 warnx("%s:%d \"%s\" looks like punycode: "
91 "you should use the decoded hostname.",
92 config_path, yylineno, $2);
93 }
95 if (host->cert == NULL || host->key == NULL ||
96 host->dir == NULL)
97 errx(1, "invalid vhost definition: %s", $2);
99 if (++ihost == HOSTSLEN)
100 errx(1, "too much vhosts defined");
102 host++;
103 loc = &host->locations[0];
104 iloc = 0;
106 | error '}' { yyerror("error in server directive"); }
109 servopts : /* empty */
110 | servopts servopt
113 servopt : TCERT TSTRING { host->cert = $2; }
114 | TKEY TSTRING { host->key = $2; }
115 | TROOT TSTRING { host->dir = $2; }
116 | TCGI TSTRING {
117 host->cgi = $2;
118 /* drop the starting '/', if any */
119 if (*host->cgi == '/')
120 host->cgi++;
122 | locopt
125 locations : /* empty */
126 | locations location
129 location : TLOCATION TSTRING '{' locopts '}' {
130 loc->match = $2;
131 if (++iloc == LOCLEN)
132 errx(1, "too much location rules defined");
133 loc++;
135 | error '}'
138 locopts : /* empty */
139 | locopts locopt
142 locopt : TDEFAULT TTYPE TSTRING {
143 if (loc->default_mime != NULL)
144 yyerror("`default type' specified more than once");
145 loc->default_mime = $3;
147 | TLANG TSTRING {
148 if (loc->lang != NULL)
149 yyerror("`lang' specified more than once");
150 loc->lang = $2;
152 | TINDEX TSTRING {
153 if (loc->index != NULL)
154 yyerror("`index' specified more than once");
155 loc->index = $2;
157 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
160 %%
162 void
163 yyerror(const char *msg)
165 goterror = 1;
166 fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
169 int
170 parse_portno(const char *p)
172 const char *errstr;
173 int n;
175 n = strtonum(p, 0, UINT16_MAX, &errstr);
176 if (errstr != NULL)
177 errx(1, "port number is %s: %s", errstr, p);
178 return n;
181 void
182 parse_conf(const char *path)
184 config_path = path;
185 if ((yyin = fopen(path, "r")) == NULL)
186 fatal("cannot open config %s", path);
187 yyparse();
188 fclose(yyin);
190 if (goterror)
191 exit(1);