Blame


1 15902770 2021-01-15 op /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
2 15902770 2021-01-15 op %{
3 15902770 2021-01-15 op
4 15902770 2021-01-15 op /*
5 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 15902770 2021-01-15 op *
7 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
8 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
9 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
10 15902770 2021-01-15 op *
11 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 15902770 2021-01-15 op */
19 15902770 2021-01-15 op
20 15902770 2021-01-15 op #include <err.h>
21 15902770 2021-01-15 op #include <stdio.h>
22 32693ee6 2021-01-28 op #include <string.h>
23 15902770 2021-01-15 op
24 15902770 2021-01-15 op #include "gmid.h"
25 15902770 2021-01-15 op
26 15902770 2021-01-15 op /*
27 15902770 2021-01-15 op * #define YYDEBUG 1
28 15902770 2021-01-15 op * int yydebug = 1;
29 15902770 2021-01-15 op */
30 15902770 2021-01-15 op
31 ca21e100 2021-02-04 op struct vhost *host;
32 ca21e100 2021-02-04 op size_t ihost;
33 ca21e100 2021-02-04 op struct location *loc;
34 ca21e100 2021-02-04 op size_t iloc;
35 15902770 2021-01-15 op
36 13ed2fb6 2021-01-27 op int goterror = 0;
37 13ed2fb6 2021-01-27 op const char *config_path;
38 15902770 2021-01-15 op
39 13ed2fb6 2021-01-27 op void yyerror(const char*);
40 13ed2fb6 2021-01-27 op int parse_portno(const char*);
41 13ed2fb6 2021-01-27 op void parse_conf(const char*);
42 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
43 13ed2fb6 2021-01-27 op
44 15902770 2021-01-15 op %}
45 15902770 2021-01-15 op
46 15902770 2021-01-15 op /* for bison: */
47 15902770 2021-01-15 op /* %define parse.error verbose */
48 15902770 2021-01-15 op
49 15902770 2021-01-15 op %union {
50 15902770 2021-01-15 op char *str;
51 15902770 2021-01-15 op int num;
52 15902770 2021-01-15 op }
53 15902770 2021-01-15 op
54 46af8c6c 2021-01-27 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
55 ae08ec7d 2021-01-25 op %token TCHROOT TUSER TSERVER
56 252908e6 2021-01-24 op %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
57 15902770 2021-01-15 op %token TERR
58 15902770 2021-01-15 op
59 15902770 2021-01-15 op %token <str> TSTRING
60 15902770 2021-01-15 op %token <num> TNUM
61 15902770 2021-01-15 op %token <num> TBOOL
62 15902770 2021-01-15 op
63 15902770 2021-01-15 op %%
64 15902770 2021-01-15 op
65 15902770 2021-01-15 op conf : options vhosts ;
66 15902770 2021-01-15 op
67 15902770 2021-01-15 op options : /* empty */
68 15902770 2021-01-15 op | options option
69 15902770 2021-01-15 op ;
70 15902770 2021-01-15 op
71 46af8c6c 2021-01-27 op option : TIPV6 TBOOL { conf.ipv6 = $2; }
72 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
73 5bc3c98e 2021-01-15 op | TPROTOCOLS TSTRING {
74 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
75 5bc3c98e 2021-01-15 op errx(1, "invalid protocols string \"%s\"", $2);
76 5bc3c98e 2021-01-15 op }
77 b2a6b613 2021-01-21 op | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
78 ae08ec7d 2021-01-25 op | TCHROOT TSTRING { conf.chroot = $2; }
79 ae08ec7d 2021-01-25 op | TUSER TSTRING { conf.user = $2; }
80 15902770 2021-01-15 op ;
81 15902770 2021-01-15 op
82 15902770 2021-01-15 op vhosts : /* empty */
83 15902770 2021-01-15 op | vhosts vhost
84 15902770 2021-01-15 op ;
85 15902770 2021-01-15 op
86 c8b74339 2021-01-24 op vhost : TSERVER TSTRING '{' servopts locations '}' {
87 ca21e100 2021-02-04 op host->locations[0].match = xstrdup("*");
88 15902770 2021-01-15 op host->domain = $2;
89 15902770 2021-01-15 op
90 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
91 cbeee4ca 2021-01-28 op warnx("%s:%d \"%s\" looks like punycode: "
92 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
93 415ac7a2 2021-01-28 op config_path, yylineno, $2);
94 cbeee4ca 2021-01-28 op }
95 cbeee4ca 2021-01-28 op
96 15902770 2021-01-15 op if (host->cert == NULL || host->key == NULL ||
97 15902770 2021-01-15 op host->dir == NULL)
98 15902770 2021-01-15 op errx(1, "invalid vhost definition: %s", $2);
99 c8b74339 2021-01-24 op
100 15902770 2021-01-15 op if (++ihost == HOSTSLEN)
101 15902770 2021-01-15 op errx(1, "too much vhosts defined");
102 c8b74339 2021-01-24 op
103 c8b74339 2021-01-24 op host++;
104 c8b74339 2021-01-24 op loc = &host->locations[0];
105 c8b74339 2021-01-24 op iloc = 0;
106 15902770 2021-01-15 op }
107 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
108 15902770 2021-01-15 op ;
109 15902770 2021-01-15 op
110 15902770 2021-01-15 op servopts : /* empty */
111 15902770 2021-01-15 op | servopts servopt
112 15902770 2021-01-15 op ;
113 15902770 2021-01-15 op
114 e17642a7 2021-02-01 op servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); }
115 e17642a7 2021-02-01 op | TKEY TSTRING { host->key = ensure_absolute_path($2); }
116 e17642a7 2021-02-01 op | TROOT TSTRING { host->dir = ensure_absolute_path($2); }
117 15902770 2021-01-15 op | TCGI TSTRING {
118 15902770 2021-01-15 op /* drop the starting '/', if any */
119 709f4c94 2021-02-04 op if (*$2 == '/')
120 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
121 709f4c94 2021-02-04 op host->cgi = $2;
122 05c23a54 2021-01-19 op }
123 c8b74339 2021-01-24 op | locopt
124 c8b74339 2021-01-24 op ;
125 c8b74339 2021-01-24 op
126 c8b74339 2021-01-24 op locations : /* empty */
127 c8b74339 2021-01-24 op | locations location
128 c8b74339 2021-01-24 op ;
129 c8b74339 2021-01-24 op
130 c8b74339 2021-01-24 op location : TLOCATION TSTRING '{' locopts '}' {
131 c8b74339 2021-01-24 op loc->match = $2;
132 c8b74339 2021-01-24 op if (++iloc == LOCLEN)
133 c8b74339 2021-01-24 op errx(1, "too much location rules defined");
134 c8b74339 2021-01-24 op loc++;
135 6119e13e 2021-01-19 op }
136 c8b74339 2021-01-24 op | error '}'
137 c8b74339 2021-01-24 op ;
138 c8b74339 2021-01-24 op
139 c8b74339 2021-01-24 op locopts : /* empty */
140 c8b74339 2021-01-24 op | locopts locopt
141 c8b74339 2021-01-24 op ;
142 c8b74339 2021-01-24 op
143 c8b74339 2021-01-24 op locopt : TDEFAULT TTYPE TSTRING {
144 fe5967cd 2021-01-27 op if (loc->default_mime != NULL)
145 fe5967cd 2021-01-27 op yyerror("`default type' specified more than once");
146 c8b74339 2021-01-24 op loc->default_mime = $3;
147 c8b74339 2021-01-24 op }
148 05c23a54 2021-01-19 op | TLANG TSTRING {
149 fe5967cd 2021-01-27 op if (loc->lang != NULL)
150 fe5967cd 2021-01-27 op yyerror("`lang' specified more than once");
151 c8b74339 2021-01-24 op loc->lang = $2;
152 05c23a54 2021-01-19 op }
153 e7a2a99b 2021-01-24 op | TINDEX TSTRING {
154 fe5967cd 2021-01-27 op if (loc->index != NULL)
155 fe5967cd 2021-01-27 op yyerror("`index' specified more than once");
156 c8b74339 2021-01-24 op loc->index = $2;
157 e7a2a99b 2021-01-24 op }
158 252908e6 2021-01-24 op | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
159 15902770 2021-01-15 op ;
160 13ed2fb6 2021-01-27 op
161 13ed2fb6 2021-01-27 op %%
162 13ed2fb6 2021-01-27 op
163 13ed2fb6 2021-01-27 op void
164 13ed2fb6 2021-01-27 op yyerror(const char *msg)
165 13ed2fb6 2021-01-27 op {
166 13ed2fb6 2021-01-27 op goterror = 1;
167 13ed2fb6 2021-01-27 op fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
168 13ed2fb6 2021-01-27 op }
169 13ed2fb6 2021-01-27 op
170 13ed2fb6 2021-01-27 op int
171 13ed2fb6 2021-01-27 op parse_portno(const char *p)
172 13ed2fb6 2021-01-27 op {
173 13ed2fb6 2021-01-27 op const char *errstr;
174 13ed2fb6 2021-01-27 op int n;
175 13ed2fb6 2021-01-27 op
176 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
177 13ed2fb6 2021-01-27 op if (errstr != NULL)
178 13ed2fb6 2021-01-27 op errx(1, "port number is %s: %s", errstr, p);
179 13ed2fb6 2021-01-27 op return n;
180 13ed2fb6 2021-01-27 op }
181 13ed2fb6 2021-01-27 op
182 13ed2fb6 2021-01-27 op void
183 13ed2fb6 2021-01-27 op parse_conf(const char *path)
184 13ed2fb6 2021-01-27 op {
185 ca21e100 2021-02-04 op host = &hosts[0];
186 ca21e100 2021-02-04 op ihost = 0;
187 ca21e100 2021-02-04 op loc = &hosts[0].locations[0];
188 ca21e100 2021-02-04 op iloc = 0;
189 ca21e100 2021-02-04 op
190 13ed2fb6 2021-01-27 op config_path = path;
191 13ed2fb6 2021-01-27 op if ((yyin = fopen(path, "r")) == NULL)
192 13ed2fb6 2021-01-27 op fatal("cannot open config %s", path);
193 13ed2fb6 2021-01-27 op yyparse();
194 13ed2fb6 2021-01-27 op fclose(yyin);
195 13ed2fb6 2021-01-27 op
196 13ed2fb6 2021-01-27 op if (goterror)
197 13ed2fb6 2021-01-27 op exit(1);
198 13ed2fb6 2021-01-27 op }
199 e17642a7 2021-02-01 op
200 e17642a7 2021-02-01 op char *
201 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
202 e17642a7 2021-02-01 op {
203 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
204 e17642a7 2021-02-01 op yyerror("not an absolute path");
205 e17642a7 2021-02-01 op return path;
206 e17642a7 2021-02-01 op }