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);
48 %}
50 /* for bison: */
51 /* %define parse.error verbose */
53 %union {
54 char *str;
55 int num;
56 }
58 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
59 %token TCHROOT TUSER TSERVER TPREFORK
60 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
61 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA
62 %token TERR
64 %token <str> TSTRING
65 %token <num> TNUM
66 %token <num> TBOOL
68 %%
70 conf : options vhosts ;
72 options : /* empty */
73 | options option
74 ;
76 option : TCHROOT TSTRING { conf.chroot = $2; }
77 | TIPV6 TBOOL { conf.ipv6 = $2; }
78 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
79 | TPORT TNUM { conf.port = $2; }
80 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
81 | TPROTOCOLS TSTRING {
82 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
83 yyerror("invalid protocols string \"%s\"", $2);
84 }
85 | TUSER TSTRING { conf.user = $2; }
86 ;
88 vhosts : /* empty */
89 | vhosts vhost
90 ;
92 vhost : TSERVER TSTRING '{' servopts locations '}' {
93 host->locations[0].match = xstrdup("*");
94 host->domain = $2;
96 if (strstr($2, "xn--") != NULL) {
97 warnx("%s:%d \"%s\" looks like punycode: "
98 "you should use the decoded hostname.",
99 config_path, yylineno, $2);
102 if (host->cert == NULL || host->key == NULL ||
103 host->dir == NULL)
104 yyerror("invalid vhost definition: %s", $2);
106 if (++ihost == HOSTSLEN)
107 errx(1, "too much vhosts defined");
109 host++;
110 loc = &host->locations[0];
111 iloc = 0;
113 | error '}' { yyerror("error in server directive"); }
116 servopts : /* empty */
117 | servopts servopt
120 servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
121 | TCGI TSTRING {
122 /* drop the starting '/', if any */
123 if (*$2 == '/')
124 memmove($2, $2+1, strlen($2));
125 host->cgi = $2;
127 | TENTRYPOINT TSTRING {
128 if (host->entrypoint != NULL)
129 yyerror("`entrypoint' specified more than once");
130 while (*$2 == '/')
131 memmove($2, $2+1, strlen($2));
132 host->entrypoint = $2;
134 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
135 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
136 | locopt
139 locations : /* empty */
140 | locations location
143 location : TLOCATION TSTRING '{' locopts '}' {
144 loc->match = $2;
145 if (++iloc == LOCLEN)
146 errx(1, "too much location rules defined");
147 loc++;
149 | error '}'
152 locopts : /* empty */
153 | locopts locopt
156 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
157 | TBLOCK TRETURN TNUM TSTRING {
158 if (loc->block_fmt != NULL)
159 yyerror("`block' rule specified more than once");
160 loc->block_fmt = check_block_fmt($4);
161 loc->block_code = check_block_code($3);
163 | TBLOCK TRETURN TNUM {
164 if (loc->block_fmt != NULL)
165 yyerror("`block' rule specified more than once");
166 loc->block_fmt = xstrdup("temporary failure");
167 loc->block_code = check_block_code($3);
168 if ($3 >= 30 && $3 < 40)
169 yyerror("missing `meta' for block return %d", $3);
171 | TBLOCK {
172 if (loc->block_fmt != NULL)
173 yyerror("`block' rule specified more than once");
174 loc->block_fmt = xstrdup("temporary failure");
175 loc->block_code = 40;
177 | TDEFAULT TTYPE TSTRING {
178 if (loc->default_mime != NULL)
179 yyerror("`default type' specified more than once");
180 loc->default_mime = $3;
182 | TINDEX TSTRING {
183 if (loc->index != NULL)
184 yyerror("`index' specified more than once");
185 loc->index = $2;
187 | TLANG TSTRING {
188 if (loc->lang != NULL)
189 yyerror("`lang' specified more than once");
190 loc->lang = $2;
192 | TREQUIRE TCLIENT TCA TSTRING {
193 if (loc->reqca != NULL)
194 yyerror("`require client ca' specified more than once");
195 if (*$4 != '/')
196 yyerror("path to certificate must be absolute: %s", $4);
197 if ((loc->reqca = load_ca($4)) == NULL)
198 yyerror("couldn't load ca cert: %s", $4);
199 free($4);
201 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
204 %%
206 void
207 yyerror(const char *msg, ...)
209 va_list ap;
211 goterror = 1;
213 va_start(ap, msg);
214 fprintf(stderr, "%s:%d: ", config_path, yylineno);
215 vfprintf(stderr, msg, ap);
216 fprintf(stderr, "\n");
217 va_end(ap);
220 int
221 parse_portno(const char *p)
223 const char *errstr;
224 int n;
226 n = strtonum(p, 0, UINT16_MAX, &errstr);
227 if (errstr != NULL)
228 yylineno("port number is %s: %s", errstr, p);
229 return n;
232 void
233 parse_conf(const char *path)
235 host = &hosts[0];
236 ihost = 0;
237 loc = &hosts[0].locations[0];
238 iloc = 0;
240 config_path = path;
241 if ((yyin = fopen(path, "r")) == NULL)
242 fatal("cannot open config: %s: %s", path, strerror(errno));
243 yyparse();
244 fclose(yyin);
246 if (goterror)
247 exit(1);
250 char *
251 ensure_absolute_path(char *path)
253 if (path == NULL || *path != '/')
254 yyerror("not an absolute path");
255 return path;
258 int
259 check_block_code(int n)
261 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
262 yyerror("invalid block code %d", n);
263 return n;
266 char *
267 check_block_fmt(char *fmt)
269 char *s;
271 for (s = fmt; *s; ++s) {
272 if (*s != '%')
273 continue;
274 switch (*++s) {
275 case '%':
276 case 'p':
277 case 'q':
278 case 'P':
279 case 'N':
280 break;
281 default:
282 yyerror("invalid format specifier %%%c", *s);
286 return fmt;
289 int
290 check_strip_no(int n)
292 if (n <= 0)
293 yyerror("invalid strip number %d", n);
294 return n;
297 int
298 check_prefork_num(int n)
300 if (n <= 0)
301 yyerror("invalid prefork number %d", n);
302 return n;