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 <errno.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "gmid.h"
27 /*
28 * #define YYDEBUG 1
29 * int yydebug = 1;
30 */
32 struct vhost *host;
33 size_t ihost;
34 struct location *loc;
35 size_t iloc;
37 int goterror = 0;
39 void yyerror(const char*, ...);
40 int parse_portno(const char*);
41 void parse_conf(const char*);
42 char *ensure_absolute_path(char*);
43 int check_block_code(int);
44 char *check_block_fmt(char*);
45 int check_strip_no(int);
46 int check_prefork_num(int);
47 void advance_loc(void);
49 %}
51 /* for bison: */
52 /* %define parse.error verbose */
54 %union {
55 char *str;
56 int num;
57 }
59 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
60 %token TCHROOT TUSER TSERVER TPREFORK
61 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TLOG TINDEX TAUTO
62 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA
63 %token TERR
65 %token <str> TSTRING
66 %token <num> TNUM
67 %token <num> TBOOL
69 %%
71 conf : options vhosts ;
73 options : /* empty */
74 | options option
75 ;
77 option : TCHROOT TSTRING { conf.chroot = $2; }
78 | TIPV6 TBOOL { conf.ipv6 = $2; }
79 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
80 | TPORT TNUM { conf.port = $2; }
81 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
82 | TPROTOCOLS TSTRING {
83 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
84 yyerror("invalid protocols string \"%s\"", $2);
85 }
86 | TUSER TSTRING { conf.user = $2; }
87 ;
89 vhosts : /* empty */
90 | vhosts vhost
91 ;
93 vhost : TSERVER TSTRING '{' servopts locations '}' {
94 host->locations[0].match = xstrdup("*");
95 host->domain = $2;
97 if (strstr($2, "xn--") != NULL) {
98 warnx("%s:%d \"%s\" looks like punycode: "
99 "you should use the decoded hostname.",
100 config_path, yylineno, $2);
103 if (host->cert == NULL || host->key == NULL ||
104 host->dir == NULL)
105 yyerror("invalid vhost definition: %s", $2);
107 if (++ihost == HOSTSLEN)
108 errx(1, "too much vhosts defined");
110 host++;
111 loc = &host->locations[0];
112 iloc = 0;
114 | error '}' { yyerror("error in server directive"); }
117 servopts : /* empty */
118 | servopts servopt
121 servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
122 | TCGI TSTRING {
123 /* drop the starting '/', if any */
124 if (*$2 == '/')
125 memmove($2, $2+1, strlen($2));
126 host->cgi = $2;
128 | TENTRYPOINT TSTRING {
129 if (host->entrypoint != NULL)
130 yyerror("`entrypoint' specified more than once");
131 while (*$2 == '/')
132 memmove($2, $2+1, strlen($2));
133 host->entrypoint = $2;
135 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
136 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
137 | locopt
140 locations : /* empty */
141 | locations location
144 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
145 /* drop the starting '/' if any */
146 if (*$3 == '/')
147 memmove($3, $3+1, strlen($3));
148 loc->match = $3;
150 | error '}'
153 locopts : /* empty */
154 | locopts locopt
157 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
158 | TBLOCK TRETURN TNUM TSTRING {
159 if (loc->block_fmt != NULL)
160 yyerror("`block' rule specified more than once");
161 loc->block_fmt = check_block_fmt($4);
162 loc->block_code = check_block_code($3);
164 | TBLOCK TRETURN TNUM {
165 if (loc->block_fmt != NULL)
166 yyerror("`block' rule specified more than once");
167 loc->block_fmt = xstrdup("temporary failure");
168 loc->block_code = check_block_code($3);
169 if ($3 >= 30 && $3 < 40)
170 yyerror("missing `meta' for block return %d", $3);
172 | TBLOCK {
173 if (loc->block_fmt != NULL)
174 yyerror("`block' rule specified more than once");
175 loc->block_fmt = xstrdup("temporary failure");
176 loc->block_code = 40;
178 | TDEFAULT TTYPE TSTRING {
179 if (loc->default_mime != NULL)
180 yyerror("`default type' specified more than once");
181 loc->default_mime = $3;
183 | TINDEX TSTRING {
184 if (loc->index != NULL)
185 yyerror("`index' specified more than once");
186 loc->index = $2;
188 | TLANG TSTRING {
189 if (loc->lang != NULL)
190 yyerror("`lang' specified more than once");
191 loc->lang = $2;
193 | TLOG TBOOL { loc->disable_log = !$2; }
194 | TREQUIRE TCLIENT TCA TSTRING {
195 if (loc->reqca != NULL)
196 yyerror("`require client ca' specified more than once");
197 if (*$4 != '/')
198 yyerror("path to certificate must be absolute: %s", $4);
199 if ((loc->reqca = load_ca($4)) == NULL)
200 yyerror("couldn't load ca cert: %s", $4);
201 free($4);
203 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
206 %%
208 void
209 yyerror(const char *msg, ...)
211 va_list ap;
213 goterror = 1;
215 va_start(ap, msg);
216 fprintf(stderr, "%s:%d: ", config_path, yylineno);
217 vfprintf(stderr, msg, ap);
218 fprintf(stderr, "\n");
219 va_end(ap);
222 int
223 parse_portno(const char *p)
225 const char *errstr;
226 int n;
228 n = strtonum(p, 0, UINT16_MAX, &errstr);
229 if (errstr != NULL)
230 yyerror("port number is %s: %s", errstr, p);
231 return n;
234 void
235 parse_conf(const char *path)
237 host = &hosts[0];
238 ihost = 0;
239 loc = &hosts[0].locations[0];
240 iloc = 0;
242 config_path = path;
243 if ((yyin = fopen(path, "r")) == NULL)
244 fatal("cannot open config: %s: %s", path, strerror(errno));
245 yyparse();
246 fclose(yyin);
248 if (goterror)
249 exit(1);
252 char *
253 ensure_absolute_path(char *path)
255 if (path == NULL || *path != '/')
256 yyerror("not an absolute path");
257 return path;
260 int
261 check_block_code(int n)
263 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
264 yyerror("invalid block code %d", n);
265 return n;
268 char *
269 check_block_fmt(char *fmt)
271 char *s;
273 for (s = fmt; *s; ++s) {
274 if (*s != '%')
275 continue;
276 switch (*++s) {
277 case '%':
278 case 'p':
279 case 'q':
280 case 'P':
281 case 'N':
282 break;
283 default:
284 yyerror("invalid format specifier %%%c", *s);
288 return fmt;
291 int
292 check_strip_no(int n)
294 if (n <= 0)
295 yyerror("invalid strip number %d", n);
296 return n;
299 int
300 check_prefork_num(int n)
302 if (n <= 0)
303 yyerror("invalid prefork number %d", n);
304 return n;
307 void
308 advance_loc(void)
310 if (++iloc == LOCLEN)
311 errx(1, "too much location rules defined");
312 loc++;