1 /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
5 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
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);
51 /* %define parse.error verbose */
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
70 conf : options vhosts ;
76 option : TIPV6 TBOOL { conf.ipv6 = $2; }
77 | TPORT TNUM { conf.port = $2; }
78 | TPROTOCOLS TSTRING {
79 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
80 errx(1, "invalid protocols string \"%s\"", $2);
82 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
83 | TCHROOT TSTRING { conf.chroot = $2; }
84 | TUSER TSTRING { conf.user = $2; }
85 | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
92 vhost : TSERVER TSTRING '{' servopts locations '}' {
93 host->locations[0].match = xstrdup("*");
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 ||
104 errx(1, "invalid vhost definition: %s", $2);
106 if (++ihost == HOSTSLEN)
107 errx(1, "too much vhosts defined");
110 loc = &host->locations[0];
113 | error '}' { yyerror("error in server directive"); }
116 servopts : /* empty */
120 servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
121 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
122 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
124 /* drop the starting '/', if any */
126 memmove($2, $2+1, strlen($2));
129 | TENTRYPOINT TSTRING {
130 if (host->entrypoint != NULL)
131 yyerror("`entrypoint' specified more than once");
133 memmove($2, $2+1, strlen($2));
134 host->entrypoint = $2;
139 locations : /* empty */
143 location : TLOCATION TSTRING '{' locopts '}' {
145 if (++iloc == LOCLEN)
146 errx(1, "too much location rules defined");
152 locopts : /* empty */
156 locopt : TDEFAULT TTYPE TSTRING {
157 if (loc->default_mime != NULL)
158 yyerror("`default type' specified more than once");
159 loc->default_mime = $3;
162 if (loc->lang != NULL)
163 yyerror("`lang' specified more than once");
167 if (loc->index != NULL)
168 yyerror("`index' specified more than once");
171 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
172 | TBLOCK TRETURN TNUM TSTRING {
173 if (loc->block_fmt != NULL)
174 yyerror("`block' rule specified more than once");
175 loc->block_fmt = check_block_fmt($4);
176 loc->block_code = check_block_code($3);
178 | TBLOCK TRETURN TNUM {
179 if (loc->block_fmt != NULL)
180 yyerror("`block' rule specified more than once");
181 loc->block_fmt = xstrdup("temporary failure");
182 loc->block_code = check_block_code($3);
183 if ($3 >= 30 && $3 < 40)
184 yyerror("missing `meta' for block return %d", $3);
187 if (loc->block_fmt != NULL)
188 yyerror("`block' rule specified more than once");
189 loc->block_fmt = xstrdup("temporary failure");
190 loc->block_code = 40;
192 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
198 yyerror(const char *msg, ...)
205 fprintf(stderr, "%s:%d: ", config_path, yylineno);
206 vfprintf(stderr, msg, ap);
207 fprintf(stderr, "\n");
212 parse_portno(const char *p)
217 n = strtonum(p, 0, UINT16_MAX, &errstr);
219 errx(1, "port number is %s: %s", errstr, p);
224 parse_conf(const char *path)
228 loc = &hosts[0].locations[0];
232 if ((yyin = fopen(path, "r")) == NULL)
233 fatal("cannot open config %s", path);
242 ensure_absolute_path(char *path)
244 if (path == NULL || *path != '/')
245 yyerror("not an absolute path");
250 check_block_code(int n)
252 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
253 yyerror("invalid block code %d", n);
258 check_block_fmt(char *fmt)
262 for (s = fmt; *s; ++s) {
273 yyerror("invalid format specifier %%%c", *s);
281 check_strip_no(int n)
284 yyerror("invalid strip number %d", n);
289 check_prefork_num(int n)
292 yyerror("invalid prefork number %d", n);