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.
31 struct vhost *host = &hosts[0];
34 struct location *loc = &hosts[0].locations[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*);
48 /* %define parse.error verbose */
55 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
56 %token TCHROOT TUSER TSERVER
57 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
66 conf : options vhosts ;
72 option : TIPV6 TBOOL { conf.ipv6 = $2; }
73 | TPORT TNUM { conf.port = $2; }
74 | TPROTOCOLS TSTRING {
75 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
76 errx(1, "invalid protocols string \"%s\"", $2);
78 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
79 | TCHROOT TSTRING { conf.chroot = $2; }
80 | TUSER TSTRING { conf.user = $2; }
87 vhost : TSERVER TSTRING '{' servopts locations '}' {
88 host->locations[0].match = (char*)"*";
91 if (strstr($2, "xn--") != NULL) {
92 warnx("%s:%d \"%s\" looks like punycode: "
93 "you should use the decoded hostname.",
94 config_path, yylineno, $2);
97 if (host->cert == NULL || host->key == NULL ||
99 errx(1, "invalid vhost definition: %s", $2);
101 if (++ihost == HOSTSLEN)
102 errx(1, "too much vhosts defined");
105 loc = &host->locations[0];
108 | error '}' { yyerror("error in server directive"); }
111 servopts : /* empty */
115 servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
116 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
117 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
120 /* drop the starting '/', if any */
121 if (*host->cgi == '/')
127 locations : /* empty */
131 location : TLOCATION TSTRING '{' locopts '}' {
133 if (++iloc == LOCLEN)
134 errx(1, "too much location rules defined");
140 locopts : /* empty */
144 locopt : TDEFAULT TTYPE TSTRING {
145 if (loc->default_mime != NULL)
146 yyerror("`default type' specified more than once");
147 loc->default_mime = $3;
150 if (loc->lang != NULL)
151 yyerror("`lang' specified more than once");
155 if (loc->index != NULL)
156 yyerror("`index' specified more than once");
159 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
165 yyerror(const char *msg)
168 fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
172 parse_portno(const char *p)
177 n = strtonum(p, 0, UINT16_MAX, &errstr);
179 errx(1, "port number is %s: %s", errstr, p);
184 parse_conf(const char *path)
187 if ((yyin = fopen(path, "r")) == NULL)
188 fatal("cannot open config %s", path);
197 ensure_absolute_path(char *path)
199 if (path == NULL || *path != '/')
200 yyerror("not an absolute path");