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 <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;
38 const char *config_path;
40 void yyerror(const char*, ...);
41 int parse_portno(const char*);
42 void parse_conf(const char*);
43 char *ensure_absolute_path(char*);
44 int check_block_code(int);
45 char *check_block_fmt(char*);
46 int check_strip_no(int);
47 int check_prefork_num(int);
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
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 : TIPV6 TBOOL { conf.ipv6 = $2; }
78 | TPORT TNUM { conf.port = $2; }
79 | TPROTOCOLS TSTRING {
80 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
81 errx(1, "invalid protocols string \"%s\"", $2);
82 }
83 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
84 | TCHROOT TSTRING { conf.chroot = $2; }
85 | TUSER TSTRING { conf.user = $2; }
86 | TPREFORK TNUM { conf.prefork = check_prefork_num($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 errx(1, "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 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
123 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
124 | TCGI TSTRING {
125 /* drop the starting '/', if any */
126 if (*$2 == '/')
127 memmove($2, $2+1, strlen($2));
128 host->cgi = $2;
130 | TENTRYPOINT TSTRING {
131 if (host->entrypoint != NULL)
132 yyerror("`entrypoint' specified more than once");
133 while (*$2 == '/')
134 memmove($2, $2+1, strlen($2));
135 host->entrypoint = $2;
137 | locopt
140 locations : /* empty */
141 | locations location
144 location : TLOCATION TSTRING '{' locopts '}' {
145 loc->match = $2;
146 if (++iloc == LOCLEN)
147 errx(1, "too much location rules defined");
148 loc++;
150 | error '}'
153 locopts : /* empty */
154 | locopts locopt
157 locopt : TDEFAULT TTYPE TSTRING {
158 if (loc->default_mime != NULL)
159 yyerror("`default type' specified more than once");
160 loc->default_mime = $3;
162 | TLANG TSTRING {
163 if (loc->lang != NULL)
164 yyerror("`lang' specified more than once");
165 loc->lang = $2;
167 | TINDEX TSTRING {
168 if (loc->index != NULL)
169 yyerror("`index' specified more than once");
170 loc->index = $2;
172 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
173 | TBLOCK TRETURN TNUM TSTRING {
174 if (loc->block_fmt != NULL)
175 yyerror("`block' rule specified more than once");
176 loc->block_fmt = check_block_fmt($4);
177 loc->block_code = check_block_code($3);
179 | TBLOCK TRETURN TNUM {
180 if (loc->block_fmt != NULL)
181 yyerror("`block' rule specified more than once");
182 loc->block_fmt = xstrdup("temporary failure");
183 loc->block_code = check_block_code($3);
184 if ($3 >= 30 && $3 < 40)
185 yyerror("missing `meta' for block return %d", $3);
187 | TBLOCK {
188 if (loc->block_fmt != NULL)
189 yyerror("`block' rule specified more than once");
190 loc->block_fmt = xstrdup("temporary failure");
191 loc->block_code = 40;
193 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
196 %%
198 void
199 yyerror(const char *msg, ...)
201 va_list ap;
203 goterror = 1;
205 va_start(ap, msg);
206 fprintf(stderr, "%s:%d: ", config_path, yylineno);
207 vfprintf(stderr, msg, ap);
208 fprintf(stderr, "\n");
209 va_end(ap);
212 int
213 parse_portno(const char *p)
215 const char *errstr;
216 int n;
218 n = strtonum(p, 0, UINT16_MAX, &errstr);
219 if (errstr != NULL)
220 errx(1, "port number is %s: %s", errstr, p);
221 return n;
224 void
225 parse_conf(const char *path)
227 host = &hosts[0];
228 ihost = 0;
229 loc = &hosts[0].locations[0];
230 iloc = 0;
232 config_path = path;
233 if ((yyin = fopen(path, "r")) == NULL)
234 fatal("cannot open config %s", path);
235 yyparse();
236 fclose(yyin);
238 if (goterror)
239 exit(1);
242 char *
243 ensure_absolute_path(char *path)
245 if (path == NULL || *path != '/')
246 yyerror("not an absolute path");
247 return path;
250 int
251 check_block_code(int n)
253 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
254 yyerror("invalid block code %d", n);
255 return n;
258 char *
259 check_block_fmt(char *fmt)
261 char *s;
263 for (s = fmt; *s; ++s) {
264 if (*s != '%')
265 continue;
266 switch (*++s) {
267 case '%':
268 case 'p':
269 case 'q':
270 case 'P':
271 case 'N':
272 break;
273 default:
274 yyerror("invalid format specifier %%%c", *s);
278 return fmt;
281 int
282 check_strip_no(int n)
284 if (n <= 0)
285 yyerror("invalid strip number %d", n);
286 return n;
289 int
290 check_prefork_num(int n)
292 if (n <= 0)
293 yyerror("invalid prefork number %d", n);
294 return n;