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 struct location *loc;
35 int goterror = 0;
37 static struct vhost *new_vhost(void);
38 static struct location *new_location(void);
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);
48 void advance_loc(void);
49 void only_once(const void*, const char*);
50 void only_oncei(int, const char*);
51 int fastcgi_conf(char *, char *, char *);
53 %}
55 /* for bison: */
56 /* %define parse.error verbose */
58 %union {
59 char *str;
60 int num;
61 }
63 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
64 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
65 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
66 %token TFASTCGI TSPAWN
68 %token TERR
70 %token <str> TSTRING
71 %token <num> TNUM
72 %token <num> TBOOL
74 %%
76 conf : options vhosts ;
78 options : /* empty */
79 | options option
80 ;
82 option : TCHROOT TSTRING { conf.chroot = $2; }
83 | TIPV6 TBOOL { conf.ipv6 = $2; }
84 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
85 | TPORT TNUM { conf.port = $2; }
86 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
87 | TPROTOCOLS TSTRING {
88 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
89 yyerror("invalid protocols string \"%s\"", $2);
90 }
91 | TUSER TSTRING { conf.user = $2; }
92 ;
94 vhosts : /* empty */
95 | vhosts vhost
96 ;
98 vhost : TSERVER TSTRING {
99 host = new_vhost();
100 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
102 loc = new_location();
103 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
105 loc->match = xstrdup("*");
106 host->domain = $2;
108 if (strstr($2, "xn--") != NULL) {
109 warnx("%s:%d \"%s\" looks like punycode: "
110 "you should use the decoded hostname.",
111 config_path, yylineno, $2);
113 } '{' servopts locations '}' {
115 if (host->cert == NULL || host->key == NULL)
116 yyerror("invalid vhost definition: %s", $2);
118 | error '}' { yyerror("error in server directive"); }
121 servopts : /* empty */
122 | servopts servopt
125 servopt : TALIAS TSTRING {
126 struct alist *a;
128 a = xcalloc(1, sizeof(*a));
129 a->alias = $2;
130 if (TAILQ_EMPTY(&host->aliases))
131 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
132 else
133 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
135 | TCERT TSTRING {
136 only_once(host->cert, "cert");
137 host->cert = ensure_absolute_path($2);
139 | TCGI TSTRING {
140 only_once(host->cgi, "cgi");
141 /* drop the starting '/', if any */
142 if (*$2 == '/')
143 memmove($2, $2+1, strlen($2));
144 host->cgi = $2;
146 | TENTRYPOINT TSTRING {
147 only_once(host->entrypoint, "entrypoint");
148 while (*$2 == '/')
149 memmove($2, $2+1, strlen($2));
150 host->entrypoint = $2;
152 | TENV TSTRING TSTRING {
153 struct envlist *e;
155 e = xcalloc(1, sizeof(*e));
156 e->name = $2;
157 e->value = $3;
158 if (TAILQ_EMPTY(&host->env))
159 TAILQ_INSERT_HEAD(&host->env, e, envs);
160 else
161 TAILQ_INSERT_TAIL(&host->env, e, envs);
163 | TKEY TSTRING {
164 only_once(host->key, "key");
165 host->key = ensure_absolute_path($2);
167 | locopt
170 locations : /* empty */
171 | locations location
174 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
175 /* drop the starting '/' if any */
176 if (*$3 == '/')
177 memmove($3, $3+1, strlen($3));
178 loc->match = $3;
180 | error '}'
183 locopts : /* empty */
184 | locopts locopt
187 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
188 | TBLOCK TRETURN TNUM TSTRING {
189 only_once(loc->block_fmt, "block");
190 loc->block_fmt = check_block_fmt($4);
191 loc->block_code = check_block_code($3);
193 | TBLOCK TRETURN TNUM {
194 only_once(loc->block_fmt, "block");
195 loc->block_fmt = xstrdup("temporary failure");
196 loc->block_code = check_block_code($3);
197 if ($3 >= 30 && $3 < 40)
198 yyerror("missing `meta' for block return %d", $3);
200 | TBLOCK {
201 only_once(loc->block_fmt, "block");
202 loc->block_fmt = xstrdup("temporary failure");
203 loc->block_code = 40;
205 | TDEFAULT TTYPE TSTRING {
206 only_once(loc->default_mime, "default type");
207 loc->default_mime = $3;
209 | TFASTCGI TSPAWN TSTRING {
210 only_oncei(loc->fcgi, "fastcgi");
211 loc->fcgi = fastcgi_conf(NULL, NULL, $3);
213 | TFASTCGI TSTRING {
214 only_oncei(loc->fcgi, "fastcgi");
215 loc->fcgi = fastcgi_conf($2, NULL, NULL);
217 | TFASTCGI TTCP TSTRING TNUM {
218 char *c;
219 if (asprintf(&c, "%d", $4) == -1)
220 err(1, "asprintf");
221 only_oncei(loc->fcgi, "fastcgi");
222 loc->fcgi = fastcgi_conf($3, c, NULL);
224 | TFASTCGI TTCP TSTRING {
225 only_oncei(loc->fcgi, "fastcgi");
226 loc->fcgi = fastcgi_conf($3, xstrdup("9000"), NULL);
228 | TFASTCGI TTCP TSTRING TSTRING {
229 only_oncei(loc->fcgi, "fastcgi");
230 loc->fcgi = fastcgi_conf($3, $4, NULL);
232 | TINDEX TSTRING {
233 only_once(loc->index, "index");
234 loc->index = $2;
236 | TLANG TSTRING {
237 only_once(loc->lang, "lang");
238 loc->lang = $2;
240 | TLOG TBOOL { loc->disable_log = !$2; }
241 | TREQUIRE TCLIENT TCA TSTRING {
242 only_once(loc->reqca, "require client ca");
243 ensure_absolute_path($4);
244 if ((loc->reqca = load_ca($4)) == NULL)
245 yyerror("couldn't load ca cert: %s", $4);
246 free($4);
248 | TROOT TSTRING {
249 only_once(loc->dir, "root");
250 loc->dir = ensure_absolute_path($2);
252 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
255 %%
257 static struct vhost *
258 new_vhost(void)
260 return xcalloc(1, sizeof(struct vhost));
263 static struct location *
264 new_location(void)
266 struct location *l;
268 l = xcalloc(1, sizeof(*l));
269 l->dirfd = -1;
270 l->fcgi = -1;
271 return l;
274 void
275 yyerror(const char *msg, ...)
277 va_list ap;
279 goterror = 1;
281 va_start(ap, msg);
282 fprintf(stderr, "%s:%d: ", config_path, yylineno);
283 vfprintf(stderr, msg, ap);
284 fprintf(stderr, "\n");
285 va_end(ap);
288 int
289 parse_portno(const char *p)
291 const char *errstr;
292 int n;
294 n = strtonum(p, 0, UINT16_MAX, &errstr);
295 if (errstr != NULL)
296 yyerror("port number is %s: %s", errstr, p);
297 return n;
300 void
301 parse_conf(const char *path)
303 config_path = path;
304 if ((yyin = fopen(path, "r")) == NULL)
305 err(1, "cannot open config: %s", path);
306 yyparse();
307 fclose(yyin);
309 if (goterror)
310 exit(1);
312 if (TAILQ_FIRST(&hosts)->domain == NULL)
313 errx(1, "no vhost defined in %s", path);
316 char *
317 ensure_absolute_path(char *path)
319 if (path == NULL || *path != '/')
320 yyerror("not an absolute path: %s", path);
321 return path;
324 int
325 check_block_code(int n)
327 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
328 yyerror("invalid block code %d", n);
329 return n;
332 char *
333 check_block_fmt(char *fmt)
335 char *s;
337 for (s = fmt; *s; ++s) {
338 if (*s != '%')
339 continue;
340 switch (*++s) {
341 case '%':
342 case 'p':
343 case 'q':
344 case 'P':
345 case 'N':
346 break;
347 default:
348 yyerror("invalid format specifier %%%c", *s);
352 return fmt;
355 int
356 check_strip_no(int n)
358 if (n <= 0)
359 yyerror("invalid strip number %d", n);
360 return n;
363 int
364 check_prefork_num(int n)
366 if (n <= 0 || n >= PROC_MAX)
367 yyerror("invalid prefork number %d", n);
368 return n;
371 void
372 advance_loc(void)
374 loc = new_location();
375 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
378 void
379 only_once(const void *ptr, const char *name)
381 if (ptr != NULL)
382 yyerror("`%s' specified more than once", name);
385 void
386 only_oncei(int i, const char *name)
388 if (i != -1)
389 yyerror("`%s' specified more than once", name);
392 int
393 fastcgi_conf(char *path, char *port, char *prog)
395 struct fcgi *f;
396 int i;
398 for (i = 0; i < FCGI_MAX; ++i) {
399 f = &fcgi[i];
401 if (f->path == NULL) {
402 f->id = i;
403 f->path = path;
404 f->port = port;
405 f->prog = prog;
406 return i;
409 /* XXX: what to do with prog? */
410 if (!strcmp(f->path, path) &&
411 ((port == NULL && f->port == NULL) ||
412 !strcmp(f->port, port))) {
413 free(path);
414 free(port);
415 return i;
419 yyerror("too much `fastcgi' rules defined.");
420 return -1;