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 <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "gmid.h"
27 /*
28 * #define YYDEBUG 1
29 * int yydebug = 1;
30 */
32 struct vhost *host;
33 size_t ihost;
34 struct location *loc;
35 size_t iloc;
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*);
44 int check_block_code(int);
45 char *check_block_fmt(char*);
46 int check_strip_no(int);
48 %}
50 /* for bison: */
51 /* %define parse.error verbose */
53 %union {
54 char *str;
55 int num;
56 }
58 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
59 %token TCHROOT TUSER TSERVER
60 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
61 %token TSTRIP TBLOCK TRETURN
62 %token TERR
64 %token <str> TSTRING
65 %token <num> TNUM
66 %token <num> TBOOL
68 %%
70 conf : options vhosts ;
72 options : /* empty */
73 | options option
74 ;
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);
81 }
82 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
83 | TCHROOT TSTRING { conf.chroot = $2; }
84 | TUSER TSTRING { conf.user = $2; }
85 ;
87 vhosts : /* empty */
88 | vhosts vhost
89 ;
91 vhost : TSERVER TSTRING '{' servopts locations '}' {
92 host->locations[0].match = xstrdup("*");
93 host->domain = $2;
95 if (strstr($2, "xn--") != NULL) {
96 warnx("%s:%d \"%s\" looks like punycode: "
97 "you should use the decoded hostname.",
98 config_path, yylineno, $2);
99 }
101 if (host->cert == NULL || host->key == NULL ||
102 host->dir == NULL)
103 errx(1, "invalid vhost definition: %s", $2);
105 if (++ihost == HOSTSLEN)
106 errx(1, "too much vhosts defined");
108 host++;
109 loc = &host->locations[0];
110 iloc = 0;
112 | error '}' { yyerror("error in server directive"); }
115 servopts : /* empty */
116 | servopts servopt
119 servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
120 | TKEY TSTRING { host->key = ensure_absolute_path($2); }
121 | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
122 | TCGI TSTRING {
123 /* drop the starting '/', if any */
124 if (*$2 == '/')
125 memmove($2, $2+1, strlen($2));
126 host->cgi = $2;
128 | locopt
131 locations : /* empty */
132 | locations location
135 location : TLOCATION TSTRING '{' locopts '}' {
136 loc->match = $2;
137 if (++iloc == LOCLEN)
138 errx(1, "too much location rules defined");
139 loc++;
141 | error '}'
144 locopts : /* empty */
145 | locopts locopt
148 locopt : TDEFAULT TTYPE TSTRING {
149 if (loc->default_mime != NULL)
150 yyerror("`default type' specified more than once");
151 loc->default_mime = $3;
153 | TLANG TSTRING {
154 if (loc->lang != NULL)
155 yyerror("`lang' specified more than once");
156 loc->lang = $2;
158 | TINDEX TSTRING {
159 if (loc->index != NULL)
160 yyerror("`index' specified more than once");
161 loc->index = $2;
163 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
164 | TBLOCK TRETURN TNUM TSTRING {
165 if (loc->block_fmt != NULL)
166 yyerror("`block' rule specified more than once");
167 loc->block_fmt = check_block_fmt($4);
168 loc->block_code = check_block_code($3);
170 | TBLOCK TRETURN TNUM {
171 if (loc->block_fmt != NULL)
172 yyerror("`block' rule specified more than once");
173 loc->block_fmt = xstrdup("temporary failure");
174 loc->block_code = check_block_code($3);
175 if ($3 >= 30 && $3 < 40)
176 yyerror("missing `meta' for block return %d", $3);
178 | TBLOCK {
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 = 40;
184 | TSTRIP TNUM { loc->strip = check_strip_no($2); }
187 %%
189 void
190 yyerror(const char *msg, ...)
192 va_list ap;
194 goterror = 1;
196 va_start(ap, msg);
197 fprintf(stderr, "%s:%d: ", config_path, yylineno);
198 vfprintf(stderr, msg, ap);
199 va_end(ap);
202 int
203 parse_portno(const char *p)
205 const char *errstr;
206 int n;
208 n = strtonum(p, 0, UINT16_MAX, &errstr);
209 if (errstr != NULL)
210 errx(1, "port number is %s: %s", errstr, p);
211 return n;
214 void
215 parse_conf(const char *path)
217 host = &hosts[0];
218 ihost = 0;
219 loc = &hosts[0].locations[0];
220 iloc = 0;
222 config_path = path;
223 if ((yyin = fopen(path, "r")) == NULL)
224 fatal("cannot open config %s", path);
225 yyparse();
226 fclose(yyin);
228 if (goterror)
229 exit(1);
232 char *
233 ensure_absolute_path(char *path)
235 if (path == NULL || *path != '/')
236 yyerror("not an absolute path");
237 return path;
240 int
241 check_block_code(int n)
243 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
244 yyerror("invalid block code %d", n);
245 return n;
248 char *
249 check_block_fmt(char *fmt)
251 char *s;
253 for (s = fmt; *s; ++s) {
254 if (*s != '%')
255 continue;
256 switch (*++s) {
257 case '%':
258 case 'p':
259 case 'q':
260 case 'P':
261 case 'N':
262 break;
263 default:
264 yyerror("invalid format specifier %%%c", *s);
268 return fmt;
271 int
272 check_strip_no(int n)
274 if (n <= 0)
275 yyerror("invalid strip number %d", n);
276 return n;