1 15902770 2021-01-15 op /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
5 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
7 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
8 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
9 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
11 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 002a84a1 2021-02-10 op #include <errno.h>
21 6abda252 2021-02-06 op #include <stdarg.h>
22 15902770 2021-01-15 op #include <stdio.h>
23 32693ee6 2021-01-28 op #include <string.h>
25 15902770 2021-01-15 op #include "gmid.h"
28 15902770 2021-01-15 op * #define YYDEBUG 1
29 15902770 2021-01-15 op * int yydebug = 1;
32 ca21e100 2021-02-04 op struct vhost *host;
33 ca21e100 2021-02-04 op struct location *loc;
35 13ed2fb6 2021-01-27 op int goterror = 0;
37 b8e64ccd 2021-03-31 op static struct vhost *new_vhost(void);
38 b8e64ccd 2021-03-31 op static struct location *new_location(void);
40 6abda252 2021-02-06 op void yyerror(const char*, ...);
41 13ed2fb6 2021-01-27 op int parse_portno(const char*);
42 13ed2fb6 2021-01-27 op void parse_conf(const char*);
43 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
44 6abda252 2021-02-06 op int check_block_code(int);
45 6abda252 2021-02-06 op char *check_block_fmt(char*);
46 6abda252 2021-02-06 op int check_strip_no(int);
47 a709ddf5 2021-02-07 op int check_prefork_num(int);
48 49b73ba1 2021-02-10 op void advance_loc(void);
52 15902770 2021-01-15 op /* for bison: */
53 15902770 2021-01-15 op /* %define parse.error verbose */
60 d06d6f4b 2021-04-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
61 d06d6f4b 2021-04-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
62 cc8c2901 2021-04-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS
66 15902770 2021-01-15 op %token <str> TSTRING
67 15902770 2021-01-15 op %token <num> TNUM
68 15902770 2021-01-15 op %token <num> TBOOL
72 15902770 2021-01-15 op conf : options vhosts ;
74 15902770 2021-01-15 op options : /* empty */
75 15902770 2021-01-15 op | options option
78 eb59f87e 2021-02-09 op option : TCHROOT TSTRING { conf.chroot = $2; }
79 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
80 eb59f87e 2021-02-09 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
81 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
82 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
83 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
84 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
85 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
87 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
90 15902770 2021-01-15 op vhosts : /* empty */
91 15902770 2021-01-15 op | vhosts vhost
94 b8e64ccd 2021-03-31 op vhost : TSERVER TSTRING {
95 b8e64ccd 2021-03-31 op host = new_vhost();
96 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
98 b8e64ccd 2021-03-31 op loc = new_location();
99 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
101 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
102 15902770 2021-01-15 op host->domain = $2;
104 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
105 cbeee4ca 2021-01-28 op warnx("%s:%d \"%s\" looks like punycode: "
106 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
107 415ac7a2 2021-01-28 op config_path, yylineno, $2);
109 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
111 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
112 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
114 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
117 15902770 2021-01-15 op servopts : /* empty */
118 15902770 2021-01-15 op | servopts servopt
121 cc8c2901 2021-04-29 op servopt : TALIAS TSTRING {
122 cc8c2901 2021-04-29 op struct alist *a;
124 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
125 cc8c2901 2021-04-29 op a->alias = $2;
126 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
127 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
129 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
131 cc8c2901 2021-04-29 op | TCERT TSTRING { host->cert = ensure_absolute_path($2); }
132 15902770 2021-01-15 op | TCGI TSTRING {
133 15902770 2021-01-15 op /* drop the starting '/', if any */
134 709f4c94 2021-02-04 op if (*$2 == '/')
135 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
136 709f4c94 2021-02-04 op host->cgi = $2;
138 e3ddf390 2021-02-06 op | TENTRYPOINT TSTRING {
139 e3ddf390 2021-02-06 op if (host->entrypoint != NULL)
140 e3ddf390 2021-02-06 op yyerror("`entrypoint' specified more than once");
141 e3ddf390 2021-02-06 op while (*$2 == '/')
142 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
143 e3ddf390 2021-02-06 op host->entrypoint = $2;
145 9cc630aa 2021-04-28 op | TENV TSTRING TSTRING {
146 9cc630aa 2021-04-28 op struct envlist *e;
148 9cc630aa 2021-04-28 op e = xcalloc(1, sizeof(*e));
149 9cc630aa 2021-04-28 op e->name = $2;
150 9cc630aa 2021-04-28 op e->value = $3;
151 9cc630aa 2021-04-28 op if (TAILQ_EMPTY(&host->env))
152 9cc630aa 2021-04-28 op TAILQ_INSERT_HEAD(&host->env, e, envs);
154 9cc630aa 2021-04-28 op TAILQ_INSERT_TAIL(&host->env, e, envs);
156 eb59f87e 2021-02-09 op | TKEY TSTRING { host->key = ensure_absolute_path($2); }
160 c8b74339 2021-01-24 op locations : /* empty */
161 c8b74339 2021-01-24 op | locations location
164 49b73ba1 2021-02-10 op location : TLOCATION { advance_loc(); } TSTRING '{' locopts '}' {
165 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
166 49b73ba1 2021-02-10 op if (*$3 == '/')
167 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
168 49b73ba1 2021-02-10 op loc->match = $3;
173 c8b74339 2021-01-24 op locopts : /* empty */
174 c8b74339 2021-01-24 op | locopts locopt
177 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
178 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM TSTRING {
179 6abda252 2021-02-06 op if (loc->block_fmt != NULL)
180 6abda252 2021-02-06 op yyerror("`block' rule specified more than once");
181 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
182 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
184 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
185 6abda252 2021-02-06 op if (loc->block_fmt != NULL)
186 6abda252 2021-02-06 op yyerror("`block' rule specified more than once");
187 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
188 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
189 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
190 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
193 6abda252 2021-02-06 op if (loc->block_fmt != NULL)
194 6abda252 2021-02-06 op yyerror("`block' rule specified more than once");
195 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
196 6abda252 2021-02-06 op loc->block_code = 40;
198 eb59f87e 2021-02-09 op | TDEFAULT TTYPE TSTRING {
199 eb59f87e 2021-02-09 op if (loc->default_mime != NULL)
200 eb59f87e 2021-02-09 op yyerror("`default type' specified more than once");
201 eb59f87e 2021-02-09 op loc->default_mime = $3;
203 eb59f87e 2021-02-09 op | TINDEX TSTRING {
204 eb59f87e 2021-02-09 op if (loc->index != NULL)
205 eb59f87e 2021-02-09 op yyerror("`index' specified more than once");
206 eb59f87e 2021-02-09 op loc->index = $2;
208 eb59f87e 2021-02-09 op | TLANG TSTRING {
209 eb59f87e 2021-02-09 op if (loc->lang != NULL)
210 eb59f87e 2021-02-09 op yyerror("`lang' specified more than once");
211 eb59f87e 2021-02-09 op loc->lang = $2;
213 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
214 02be96c6 2021-02-09 op | TREQUIRE TCLIENT TCA TSTRING {
215 02be96c6 2021-02-09 op if (loc->reqca != NULL)
216 02be96c6 2021-02-09 op yyerror("`require client ca' specified more than once");
218 adbe6a64 2021-04-30 op ensure_absolute_path($4);
219 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
220 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
223 fdea6aa0 2021-04-30 op | TROOT TSTRING {
224 fdea6aa0 2021-04-30 op if (loc->dir != NULL)
225 fdea6aa0 2021-04-30 op yyerror("`root' specified more than once");
227 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
229 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
234 b8e64ccd 2021-03-31 op static struct vhost *
235 b8e64ccd 2021-03-31 op new_vhost(void)
237 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
240 b8e64ccd 2021-03-31 op static struct location *
241 b8e64ccd 2021-03-31 op new_location(void)
243 fdea6aa0 2021-04-30 op struct location *l;
245 fdea6aa0 2021-04-30 op l = xcalloc(1, sizeof(*l));
246 fdea6aa0 2021-04-30 op l->dirfd = -1;
251 6abda252 2021-02-06 op yyerror(const char *msg, ...)
255 13ed2fb6 2021-01-27 op goterror = 1;
257 6abda252 2021-02-06 op va_start(ap, msg);
258 6abda252 2021-02-06 op fprintf(stderr, "%s:%d: ", config_path, yylineno);
259 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
260 a1373913 2021-02-07 op fprintf(stderr, "\n");
265 13ed2fb6 2021-01-27 op parse_portno(const char *p)
267 13ed2fb6 2021-01-27 op const char *errstr;
270 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
271 13ed2fb6 2021-01-27 op if (errstr != NULL)
272 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
277 13ed2fb6 2021-01-27 op parse_conf(const char *path)
279 13ed2fb6 2021-01-27 op config_path = path;
280 13ed2fb6 2021-01-27 op if ((yyin = fopen(path, "r")) == NULL)
281 48b69cb2 2021-04-28 op err(1, "cannot open config: %s", path);
283 13ed2fb6 2021-01-27 op fclose(yyin);
285 13ed2fb6 2021-01-27 op if (goterror)
288 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
289 48b69cb2 2021-04-28 op errx(1, "no vhost defined in %s", path);
293 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
295 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
296 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
301 6abda252 2021-02-06 op check_block_code(int n)
303 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
304 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
309 6abda252 2021-02-06 op check_block_fmt(char *fmt)
313 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
314 6abda252 2021-02-06 op if (*s != '%')
316 6abda252 2021-02-06 op switch (*++s) {
324 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
332 6abda252 2021-02-06 op check_strip_no(int n)
335 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
340 a709ddf5 2021-02-07 op check_prefork_num(int n)
342 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
343 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
348 49b73ba1 2021-02-10 op advance_loc(void)
350 b8e64ccd 2021-03-31 op loc = new_location();
351 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);