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 *);
52 void add_param(char *, char *, int);
54 %}
56 /* for bison: */
57 /* %define parse.error verbose */
59 %union {
60 char *str;
61 int num;
62 }
64 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
65 %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
66 %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
67 %token TFASTCGI TSPAWN TPARAM
69 %token TERR
71 %token <str> TSTRING
72 %token <num> TNUM
73 %token <num> TBOOL
75 %%
77 conf : options vhosts ;
79 options : /* empty */
80 | options option
81 ;
83 option : TCHROOT TSTRING { conf.chroot = $2; }
84 | TIPV6 TBOOL { conf.ipv6 = $2; }
85 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
86 | TPORT TNUM { conf.port = $2; }
87 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
88 | TPROTOCOLS TSTRING {
89 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
90 yyerror("invalid protocols string \"%s\"", $2);
91 }
92 | TUSER TSTRING { conf.user = $2; }
93 ;
95 vhosts : /* empty */
96 | vhosts vhost
97 ;
99 vhost : TSERVER TSTRING {
100 host = new_vhost();
101 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
103 loc = new_location();
104 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
106 loc->match = xstrdup("*");
107 host->domain = $2;
109 if (strstr($2, "xn--") != NULL) {
110 warnx("%s:%d \"%s\" looks like punycode: "
111 "you should use the decoded hostname.",
112 config_path, yylineno, $2);
114 } '{' servopts locations '}' {
116 if (host->cert == NULL || host->key == NULL)
117 yyerror("invalid vhost definition: %s", $2);
119 | error '}' { yyerror("error in server directive"); }
122 servopts : /* empty */
123 | servopts servopt
126 servopt : TALIAS TSTRING {
127 struct alist *a;
129 a = xcalloc(1, sizeof(*a));
130 a->alias = $2;
131 if (TAILQ_EMPTY(&host->aliases))
132 TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
133 else
134 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
136 | TCERT TSTRING {
137 only_once(host->cert, "cert");
138 host->cert = ensure_absolute_path($2);
140 | TCGI TSTRING {
141 only_once(host->cgi, "cgi");
142 /* drop the starting '/', if any */
143 if (*$2 == '/')
144 memmove($2, $2+1, strlen($2));
145 host->cgi = $2;
147 | TENTRYPOINT TSTRING {
148 only_once(host->entrypoint, "entrypoint");
149 while (*$2 == '/')
150 memmove($2, $2+1, strlen($2));
151 host->entrypoint = $2;
153 | TENV TSTRING TSTRING {
154 add_param($2, $3, 1);
156 | TKEY TSTRING {
157 only_once(host->key, "key");
158 host->key = ensure_absolute_path($2);
160 | TPARAM TSTRING TSTRING {
161 add_param($2, $3, 0);
163 | locopt
166 locations : /* empty */
167 | locations location
170 location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
171 /* drop the starting '/' if any */
172 if (*$3 == '/')
173 memmove($3, $3+1, strlen($3));
174 loc->match = $3;
176 | error '}'
179 locopts : /* empty */
180 | locopts locopt
183 locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
184 | TBLOCK TRETURN TNUM TSTRING {
185 only_once(loc->block_fmt, "block");
186 loc->block_fmt = check_block_fmt($4);
187 loc->block_code = check_block_code($3);
189 | TBLOCK TRETURN TNUM {
190 only_once(loc->block_fmt, "block");
191 loc->block_fmt = xstrdup("temporary failure");
192 loc->block_code = check_block_code($3);
193 if ($3 >= 30 && $3 < 40)
194 yyerror("missing `meta' for block return %d", $3);
196 | TBLOCK {
197 only_once(loc->block_fmt, "block");
198 loc->block_fmt = xstrdup("temporary failure");
199 loc->block_code = 40;
201 | TDEFAULT TTYPE TSTRING {
202 only_once(loc->default_mime, "default type");
203 loc->default_mime = $3;
205 | TFASTCGI fastcgi
206 | TINDEX TSTRING {
207 only_once(loc->index, "index");
208 loc->index = $2;
210 | TLANG TSTRING {
211 only_once(loc->lang, "lang");
212 loc->lang = $2;
214 | TLOG TBOOL { loc->disable_log = !$2; }
215 | TREQUIRE TCLIENT TCA TSTRING {
216 only_once(loc->reqca, "require client ca");
217 ensure_absolute_path($4);
218 if ((loc->reqca = load_ca($4)) == NULL)
219 yyerror("couldn't load ca cert: %s", $4);
220 free($4);
222 | TROOT TSTRING {
223 only_once(loc->dir, "root");
224 loc->dir = ensure_absolute_path($2);
226 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
229 fastcgi : TSPAWN TSTRING {
230 only_oncei(loc->fcgi, "fastcgi");
231 loc->fcgi = fastcgi_conf(NULL, NULL, $2);
233 | TSTRING {
234 only_oncei(loc->fcgi, "fastcgi");
235 loc->fcgi = fastcgi_conf($1, NULL, NULL);
237 | TTCP TSTRING TNUM {
238 char *c;
239 if (asprintf(&c, "%d", $3) == -1)
240 err(1, "asprintf");
241 only_oncei(loc->fcgi, "fastcgi");
242 loc->fcgi = fastcgi_conf($2, c, NULL);
244 | TTCP TSTRING {
245 only_oncei(loc->fcgi, "fastcgi");
246 loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
248 | TTCP TSTRING TSTRING {
249 only_oncei(loc->fcgi, "fastcgi");
250 loc->fcgi = fastcgi_conf($2, $3, NULL);
254 %%
256 static struct vhost *
257 new_vhost(void)
259 return xcalloc(1, sizeof(struct vhost));
262 static struct location *
263 new_location(void)
265 struct location *l;
267 l = xcalloc(1, sizeof(*l));
268 l->dirfd = -1;
269 l->fcgi = -1;
270 return l;
273 void
274 yyerror(const char *msg, ...)
276 va_list ap;
278 goterror = 1;
280 va_start(ap, msg);
281 fprintf(stderr, "%s:%d: ", config_path, yylineno);
282 vfprintf(stderr, msg, ap);
283 fprintf(stderr, "\n");
284 va_end(ap);
287 int
288 parse_portno(const char *p)
290 const char *errstr;
291 int n;
293 n = strtonum(p, 0, UINT16_MAX, &errstr);
294 if (errstr != NULL)
295 yyerror("port number is %s: %s", errstr, p);
296 return n;
299 void
300 parse_conf(const char *path)
302 config_path = path;
303 if ((yyin = fopen(path, "r")) == NULL)
304 err(1, "cannot open config: %s", path);
305 yyparse();
306 fclose(yyin);
308 if (goterror)
309 exit(1);
311 if (TAILQ_FIRST(&hosts)->domain == NULL)
312 errx(1, "no vhost defined in %s", path);
315 char *
316 ensure_absolute_path(char *path)
318 if (path == NULL || *path != '/')
319 yyerror("not an absolute path: %s", path);
320 return path;
323 int
324 check_block_code(int n)
326 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
327 yyerror("invalid block code %d", n);
328 return n;
331 char *
332 check_block_fmt(char *fmt)
334 char *s;
336 for (s = fmt; *s; ++s) {
337 if (*s != '%')
338 continue;
339 switch (*++s) {
340 case '%':
341 case 'p':
342 case 'q':
343 case 'P':
344 case 'N':
345 break;
346 default:
347 yyerror("invalid format specifier %%%c", *s);
351 return fmt;
354 int
355 check_strip_no(int n)
357 if (n <= 0)
358 yyerror("invalid strip number %d", n);
359 return n;
362 int
363 check_prefork_num(int n)
365 if (n <= 0 || n >= PROC_MAX)
366 yyerror("invalid prefork number %d", n);
367 return n;
370 void
371 advance_loc(void)
373 loc = new_location();
374 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
377 void
378 only_once(const void *ptr, const char *name)
380 if (ptr != NULL)
381 yyerror("`%s' specified more than once", name);
384 void
385 only_oncei(int i, const char *name)
387 if (i != -1)
388 yyerror("`%s' specified more than once", name);
391 int
392 fastcgi_conf(char *path, char *port, char *prog)
394 struct fcgi *f;
395 int i;
397 for (i = 0; i < FCGI_MAX; ++i) {
398 f = &fcgi[i];
400 if (f->path == NULL) {
401 f->id = i;
402 f->path = path;
403 f->port = port;
404 f->prog = prog;
405 return i;
408 /* XXX: what to do with prog? */
409 if (!strcmp(f->path, path) &&
410 ((port == NULL && f->port == NULL) ||
411 !strcmp(f->port, port))) {
412 free(path);
413 free(port);
414 return i;
418 yyerror("too much `fastcgi' rules defined.");
419 return -1;
422 void
423 add_param(char *name, char *val, int env)
425 struct envlist *e;
426 struct envhead *h;
428 if (env)
429 h = &host->env;
430 else
431 h = &host->params;
433 e = xcalloc(1, sizeof(*e));
434 e->name = name;
435 e->value = val;
436 if (TAILQ_EMPTY(h))
437 TAILQ_INSERT_HEAD(h, e, envs);
438 else
439 TAILQ_INSERT_TAIL(h, e, envs);