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 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 | TREQUIRE TCLIENT TCA TSTRING {
194 if (loc->reqca != NULL)
195 yyerror("`require client ca' specified more than once");
196 if (*$4 != '/')
197 yyerror("path to certificate must be absolute: %s", $4);
198 if ((loc->reqca = load_ca($4)) == NULL)
199 yyerror("couldn't load ca cert: %s", $4);
200 free($4);
202 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
205 %%
207 void
208 yyerror(const char *msg, ...)
210 va_list ap;
212 goterror = 1;
214 va_start(ap, msg);
215 fprintf(stderr, "%s:%d: ", config_path, yylineno);
216 vfprintf(stderr, msg, ap);
217 fprintf(stderr, "\n");
218 va_end(ap);
221 int
222 parse_portno(const char *p)
224 const char *errstr;
225 int n;
227 n = strtonum(p, 0, UINT16_MAX, &errstr);
228 if (errstr != NULL)
229 yyerror("port number is %s: %s", errstr, p);
230 return n;
233 void
234 parse_conf(const char *path)
236 host = &hosts[0];
237 ihost = 0;
238 loc = &hosts[0].locations[0];
239 iloc = 0;
241 config_path = path;
242 if ((yyin = fopen(path, "r")) == NULL)
243 fatal("cannot open config: %s: %s", path, strerror(errno));
244 yyparse();
245 fclose(yyin);
247 if (goterror)
248 exit(1);
251 char *
252 ensure_absolute_path(char *path)
254 if (path == NULL || *path != '/')
255 yyerror("not an absolute path");
256 return path;
259 int
260 check_block_code(int n)
262 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
263 yyerror("invalid block code %d", n);
264 return n;
267 char *
268 check_block_fmt(char *fmt)
270 char *s;
272 for (s = fmt; *s; ++s) {
273 if (*s != '%')
274 continue;
275 switch (*++s) {
276 case '%':
277 case 'p':
278 case 'q':
279 case 'P':
280 case 'N':
281 break;
282 default:
283 yyerror("invalid format specifier %%%c", *s);
287 return fmt;
290 int
291 check_strip_no(int n)
293 if (n <= 0)
294 yyerror("invalid strip number %d", n);
295 return n;
298 int
299 check_prefork_num(int n)
301 if (n <= 0)
302 yyerror("invalid prefork number %d", n);
303 return n;
306 void
307 advance_loc(void)
309 if (++iloc == LOCLEN)
310 errx(1, "too much location rules defined");
311 loc++;