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;
32 size_t ihost;
33 struct location *loc;
34 size_t iloc;
36 int goterror = 0;
37 const char *config_path;
39 void yyerror(const char*);
40 int parse_portno(const char*);
41 void parse_conf(const char*);
42 char *ensure_absolute_path(char*);
44 %}
46 /* for bison: */
47 /* %define parse.error verbose */
49 %union {
50 char *str;
51 int num;
52 }
54 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
55 %token TCHROOT TUSER TSERVER
56 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
57 %token TERR
59 %token <str> TSTRING
60 %token <num> TNUM
61 %token <num> TBOOL
63 %%
65 conf : options vhosts ;
67 options : /* empty */
68 | options option
69 ;
71 option : TIPV6 TBOOL { conf.ipv6 = $2; }
72 | TPORT TNUM { conf.port = $2; }
73 | TPROTOCOLS TSTRING {
74 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
75 errx(1, "invalid protocols string \"%s\"", $2);
76 }
77 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
78 | TCHROOT TSTRING { conf.chroot = $2; }
79 | TUSER TSTRING { conf.user = $2; }
80 ;
82 vhosts : /* empty */
83 | vhosts vhost
84 ;
86 vhost : TSERVER TSTRING '{' servopts locations '}' {
87 host->locations[0].match = xstrdup("*");
88 host->domain = $2;
90 if (strstr($2, "xn--") != NULL) {
91 warnx("%s:%d \"%s\" looks like punycode: "
92 "you should use the decoded hostname.",
93 config_path, yylineno, $2);
94 }
96 if (host->cert == NULL || host->key == NULL ||
97 host->dir == NULL)
98 errx(1, "invalid vhost definition: %s", $2);
100 if (++ihost == HOSTSLEN)
101 errx(1, "too much vhosts defined");
103 host++;
104 loc = &host->locations[0];
105 iloc = 0;
107 | error '}' { yyerror("error in server directive"); }
110 servopts : /* empty */
111 | servopts servopt
114 servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
115 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
116 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
117 | TCGI TSTRING {
118 /* drop the starting '/', if any */
119 if (*$2 == '/')
120 memmove($2, $2+1, strlen($2));
121 host->cgi = $2;
123 | locopt
126 locations : /* empty */
127 | locations location
130 location : TLOCATION TSTRING '{' locopts '}' {
131 loc->match = $2;
132 if (++iloc == LOCLEN)
133 errx(1, "too much location rules defined");
134 loc++;
136 | error '}'
139 locopts : /* empty */
140 | locopts locopt
143 locopt : TDEFAULT TTYPE TSTRING {
144 if (loc->default_mime != NULL)
145 yyerror("`default type' specified more than once");
146 loc->default_mime = $3;
148 | TLANG TSTRING {
149 if (loc->lang != NULL)
150 yyerror("`lang' specified more than once");
151 loc->lang = $2;
153 | TINDEX TSTRING {
154 if (loc->index != NULL)
155 yyerror("`index' specified more than once");
156 loc->index = $2;
158 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
161 %%
163 void
164 yyerror(const char *msg)
166 goterror = 1;
167 fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
170 int
171 parse_portno(const char *p)
173 const char *errstr;
174 int n;
176 n = strtonum(p, 0, UINT16_MAX, &errstr);
177 if (errstr != NULL)
178 errx(1, "port number is %s: %s", errstr, p);
179 return n;
182 void
183 parse_conf(const char *path)
185 host = &hosts[0];
186 ihost = 0;
187 loc = &hosts[0].locations[0];
188 iloc = 0;
190 config_path = path;
191 if ((yyin = fopen(path, "r")) == NULL)
192 fatal("cannot open config %s", path);
193 yyparse();
194 fclose(yyin);
196 if (goterror)
197 exit(1);
200 char *
201 ensure_absolute_path(char *path)
203 if (path == NULL || *path != '/')
204 yyerror("not an absolute path");
205 return path;