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 <err.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "gmid.h"
26 /*
27 * #define YYDEBUG 1
28 * int yydebug = 1;
29 */
31 struct vhost *host = &hosts[0];
32 size_t ihost = 0;
34 struct location *loc = &hosts[0].locations[0];
35 size_t iloc = 0;
37 int goterror = 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*);
45 %}
47 /* for bison: */
48 /* %define parse.error verbose */
50 %union {
51 char *str;
52 int num;
53 }
55 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
56 %token TCHROOT TUSER TSERVER
57 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
58 %token TERR
60 %token <str> TSTRING
61 %token <num> TNUM
62 %token <num> TBOOL
64 %%
66 conf : options vhosts ;
68 options : /* empty */
69 | options option
70 ;
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);
77 }
78 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
79 | TCHROOT TSTRING { conf.chroot = $2; }
80 | TUSER TSTRING { conf.user = $2; }
81 ;
83 vhosts : /* empty */
84 | vhosts vhost
85 ;
87 vhost : TSERVER TSTRING '{' servopts locations '}' {
88 host->locations[0].match = (char*)"*";
89 host->domain = $2;
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);
95 }
97 if (host->cert == NULL || host->key == NULL ||
98 host->dir == NULL)
99 errx(1, "invalid vhost definition: %s", $2);
101 if (++ihost == HOSTSLEN)
102 errx(1, "too much vhosts defined");
104 host++;
105 loc = &host->locations[0];
106 iloc = 0;
108 | error '}' { yyerror("error in server directive"); }
111 servopts : /* empty */
112 | servopts servopt
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); }
118 | TCGI TSTRING {
119 host->cgi = $2;
120 /* drop the starting '/', if any */
121 if (*host->cgi == '/')
122 host->cgi++;
124 | locopt
127 locations : /* empty */
128 | locations location
131 location : TLOCATION TSTRING '{' locopts '}' {
132 loc->match = $2;
133 if (++iloc == LOCLEN)
134 errx(1, "too much location rules defined");
135 loc++;
137 | error '}'
140 locopts : /* empty */
141 | locopts locopt
144 locopt : TDEFAULT TTYPE TSTRING {
145 if (loc->default_mime != NULL)
146 yyerror("`default type' specified more than once");
147 loc->default_mime = $3;
149 | TLANG TSTRING {
150 if (loc->lang != NULL)
151 yyerror("`lang' specified more than once");
152 loc->lang = $2;
154 | TINDEX TSTRING {
155 if (loc->index != NULL)
156 yyerror("`index' specified more than once");
157 loc->index = $2;
159 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
162 %%
164 void
165 yyerror(const char *msg)
167 goterror = 1;
168 fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
171 int
172 parse_portno(const char *p)
174 const char *errstr;
175 int n;
177 n = strtonum(p, 0, UINT16_MAX, &errstr);
178 if (errstr != NULL)
179 errx(1, "port number is %s: %s", errstr, p);
180 return n;
183 void
184 parse_conf(const char *path)
186 config_path = path;
187 if ((yyin = fopen(path, "r")) == NULL)
188 fatal("cannot open config %s", path);
189 yyparse();
190 fclose(yyin);
192 if (goterror)
193 exit(1);
196 char *
197 ensure_absolute_path(char *path)
199 if (path == NULL || *path != '/')
200 yyerror("not an absolute path");
201 return path;