Blame


1 56324ebe 2022-07-14 thomas /*
2 56324ebe 2022-07-14 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 56324ebe 2022-07-14 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 56324ebe 2022-07-14 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 56324ebe 2022-07-14 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 56324ebe 2022-07-14 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 56324ebe 2022-07-14 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 56324ebe 2022-07-14 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 56324ebe 2022-07-14 thomas *
10 56324ebe 2022-07-14 thomas * Permission to use, copy, modify, and distribute this software for any
11 56324ebe 2022-07-14 thomas * purpose with or without fee is hereby granted, provided that the above
12 56324ebe 2022-07-14 thomas * copyright notice and this permission notice appear in all copies.
13 56324ebe 2022-07-14 thomas *
14 56324ebe 2022-07-14 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 56324ebe 2022-07-14 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 56324ebe 2022-07-14 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 56324ebe 2022-07-14 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 56324ebe 2022-07-14 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 56324ebe 2022-07-14 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 56324ebe 2022-07-14 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 56324ebe 2022-07-14 thomas */
22 56324ebe 2022-07-14 thomas
23 56324ebe 2022-07-14 thomas %{
24 56324ebe 2022-07-14 thomas #include <sys/ioctl.h>
25 56324ebe 2022-07-14 thomas #include <sys/types.h>
26 56324ebe 2022-07-14 thomas #include <sys/socket.h>
27 56324ebe 2022-07-14 thomas #include <sys/stat.h>
28 56324ebe 2022-07-14 thomas
29 56324ebe 2022-07-14 thomas #include <net/if.h>
30 56324ebe 2022-07-14 thomas #include <netinet/in.h>
31 56324ebe 2022-07-14 thomas
32 56324ebe 2022-07-14 thomas #include <arpa/inet.h>
33 56324ebe 2022-07-14 thomas
34 56324ebe 2022-07-14 thomas #include <ctype.h>
35 56324ebe 2022-07-14 thomas #include <err.h>
36 56324ebe 2022-07-14 thomas #include <errno.h>
37 56324ebe 2022-07-14 thomas #include <event.h>
38 56324ebe 2022-07-14 thomas #include <ifaddrs.h>
39 56324ebe 2022-07-14 thomas #include <limits.h>
40 56324ebe 2022-07-14 thomas #include <netdb.h>
41 56324ebe 2022-07-14 thomas #include <stdarg.h>
42 56324ebe 2022-07-14 thomas #include <stdlib.h>
43 56324ebe 2022-07-14 thomas #include <stdio.h>
44 56324ebe 2022-07-14 thomas #include <string.h>
45 56324ebe 2022-07-14 thomas #include <syslog.h>
46 56324ebe 2022-07-14 thomas #include <tls.h>
47 56324ebe 2022-07-14 thomas #include <unistd.h>
48 56324ebe 2022-07-14 thomas
49 56324ebe 2022-07-14 thomas #include "proc.h"
50 56324ebe 2022-07-14 thomas #include "gotwebd.h"
51 d7cad54e 2022-07-14 thomas #include "got_compat.h"
52 56324ebe 2022-07-14 thomas
53 56324ebe 2022-07-14 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 56324ebe 2022-07-14 thomas static struct file {
55 56324ebe 2022-07-14 thomas TAILQ_ENTRY(file) entry;
56 56324ebe 2022-07-14 thomas FILE *stream;
57 56324ebe 2022-07-14 thomas char *name;
58 56324ebe 2022-07-14 thomas int lineno;
59 56324ebe 2022-07-14 thomas int errors;
60 56324ebe 2022-07-14 thomas } *file;
61 56324ebe 2022-07-14 thomas struct file *newfile(const char *, int);
62 56324ebe 2022-07-14 thomas static void closefile(struct file *);
63 56324ebe 2022-07-14 thomas int check_file_secrecy(int, const char *);
64 56324ebe 2022-07-14 thomas int yyparse(void);
65 56324ebe 2022-07-14 thomas int yylex(void);
66 56324ebe 2022-07-14 thomas int yyerror(const char *, ...)
67 56324ebe 2022-07-14 thomas __attribute__((__format__ (printf, 1, 2)))
68 56324ebe 2022-07-14 thomas __attribute__((__nonnull__ (1)));
69 56324ebe 2022-07-14 thomas int kw_cmp(const void *, const void *);
70 56324ebe 2022-07-14 thomas int lookup(char *);
71 56324ebe 2022-07-14 thomas int lgetc(int);
72 56324ebe 2022-07-14 thomas int lungetc(int);
73 56324ebe 2022-07-14 thomas int findeol(void);
74 56324ebe 2022-07-14 thomas
75 56324ebe 2022-07-14 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 56324ebe 2022-07-14 thomas struct sym {
77 56324ebe 2022-07-14 thomas TAILQ_ENTRY(sym) entry;
78 56324ebe 2022-07-14 thomas int used;
79 56324ebe 2022-07-14 thomas int persist;
80 56324ebe 2022-07-14 thomas char *nam;
81 56324ebe 2022-07-14 thomas char *val;
82 56324ebe 2022-07-14 thomas };
83 56324ebe 2022-07-14 thomas
84 56324ebe 2022-07-14 thomas int symset(const char *, const char *, int);
85 56324ebe 2022-07-14 thomas char *symget(const char *);
86 56324ebe 2022-07-14 thomas
87 56324ebe 2022-07-14 thomas static int errors;
88 56324ebe 2022-07-14 thomas
89 56324ebe 2022-07-14 thomas static struct gotwebd *gotwebd;
90 56324ebe 2022-07-14 thomas static struct server *new_srv;
91 56324ebe 2022-07-14 thomas static struct server *conf_new_server(const char *);
92 56324ebe 2022-07-14 thomas int getservice(const char *);
93 56324ebe 2022-07-14 thomas int n;
94 56324ebe 2022-07-14 thomas
95 56324ebe 2022-07-14 thomas int get_addrs(const char *, struct addresslist *, in_port_t);
96 56324ebe 2022-07-14 thomas struct address *host_v4(const char *);
97 56324ebe 2022-07-14 thomas struct address *host_v6(const char *);
98 56324ebe 2022-07-14 thomas int host_dns(const char *, struct addresslist *,
99 56324ebe 2022-07-14 thomas int, in_port_t, const char *, int);
100 56324ebe 2022-07-14 thomas int host_if(const char *, struct addresslist *,
101 56324ebe 2022-07-14 thomas int, in_port_t, const char *, int);
102 56324ebe 2022-07-14 thomas int host(const char *, struct addresslist *,
103 56324ebe 2022-07-14 thomas int, in_port_t, const char *, int);
104 56324ebe 2022-07-14 thomas int is_if_in_group(const char *, const char *);
105 56324ebe 2022-07-14 thomas
106 56324ebe 2022-07-14 thomas typedef struct {
107 56324ebe 2022-07-14 thomas union {
108 56324ebe 2022-07-14 thomas long long number;
109 56324ebe 2022-07-14 thomas char *string;
110 56324ebe 2022-07-14 thomas in_port_t port;
111 56324ebe 2022-07-14 thomas } v;
112 56324ebe 2022-07-14 thomas int lineno;
113 56324ebe 2022-07-14 thomas } YYSTYPE;
114 56324ebe 2022-07-14 thomas
115 56324ebe 2022-07-14 thomas %}
116 56324ebe 2022-07-14 thomas
117 56324ebe 2022-07-14 thomas %token BIND INTERFACE WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
118 56324ebe 2022-07-14 thomas %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
119 56324ebe 2022-07-14 thomas %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
120 56324ebe 2022-07-14 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK FCGI_SOCKET
121 56324ebe 2022-07-14 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
122 56324ebe 2022-07-14 thomas
123 56324ebe 2022-07-14 thomas %token <v.string> STRING
124 56324ebe 2022-07-14 thomas %type <v.port> fcgiport
125 56324ebe 2022-07-14 thomas %token <v.number> NUMBER
126 56324ebe 2022-07-14 thomas %type <v.number> boolean
127 56324ebe 2022-07-14 thomas
128 56324ebe 2022-07-14 thomas %%
129 56324ebe 2022-07-14 thomas
130 56324ebe 2022-07-14 thomas grammar :
131 56324ebe 2022-07-14 thomas | grammar '\n'
132 56324ebe 2022-07-14 thomas | grammar main '\n'
133 56324ebe 2022-07-14 thomas | grammar server '\n'
134 56324ebe 2022-07-14 thomas ;
135 56324ebe 2022-07-14 thomas
136 56324ebe 2022-07-14 thomas boolean : STRING {
137 56324ebe 2022-07-14 thomas if (strcasecmp($1, "1") == 0 ||
138 56324ebe 2022-07-14 thomas strcasecmp($1, "yes") == 0 ||
139 56324ebe 2022-07-14 thomas strcasecmp($1, "on") == 0)
140 56324ebe 2022-07-14 thomas $$ = 1;
141 56324ebe 2022-07-14 thomas else if (strcasecmp($1, "0") == 0 ||
142 56324ebe 2022-07-14 thomas strcasecmp($1, "off") == 0 ||
143 56324ebe 2022-07-14 thomas strcasecmp($1, "no") == 0)
144 56324ebe 2022-07-14 thomas $$ = 0;
145 56324ebe 2022-07-14 thomas else {
146 56324ebe 2022-07-14 thomas yyerror("invalid boolean value '%s'", $1);
147 56324ebe 2022-07-14 thomas free($1);
148 56324ebe 2022-07-14 thomas YYERROR;
149 56324ebe 2022-07-14 thomas }
150 56324ebe 2022-07-14 thomas free($1);
151 56324ebe 2022-07-14 thomas }
152 56324ebe 2022-07-14 thomas | ON { $$ = 1; }
153 56324ebe 2022-07-14 thomas | NUMBER { $$ = $1; }
154 56324ebe 2022-07-14 thomas ;
155 56324ebe 2022-07-14 thomas
156 56324ebe 2022-07-14 thomas fcgiport : NUMBER {
157 56324ebe 2022-07-14 thomas if ($1 <= 0 || $1 > (int)USHRT_MAX) {
158 56324ebe 2022-07-14 thomas yyerror("invalid port: %lld", $1);
159 56324ebe 2022-07-14 thomas YYERROR;
160 56324ebe 2022-07-14 thomas }
161 56324ebe 2022-07-14 thomas $$ = htons($1);
162 56324ebe 2022-07-14 thomas }
163 56324ebe 2022-07-14 thomas | STRING {
164 56324ebe 2022-07-14 thomas int val;
165 56324ebe 2022-07-14 thomas
166 56324ebe 2022-07-14 thomas if ((val = getservice($1)) == -1) {
167 56324ebe 2022-07-14 thomas yyerror("invalid port: %s", $1);
168 56324ebe 2022-07-14 thomas free($1);
169 56324ebe 2022-07-14 thomas YYERROR;
170 56324ebe 2022-07-14 thomas }
171 56324ebe 2022-07-14 thomas free($1);
172 56324ebe 2022-07-14 thomas
173 56324ebe 2022-07-14 thomas $$ = val;
174 56324ebe 2022-07-14 thomas }
175 56324ebe 2022-07-14 thomas ;
176 56324ebe 2022-07-14 thomas
177 56324ebe 2022-07-14 thomas main : PREFORK NUMBER {
178 56324ebe 2022-07-14 thomas gotwebd->prefork_gotwebd = $2;
179 56324ebe 2022-07-14 thomas }
180 56324ebe 2022-07-14 thomas | CHROOT STRING {
181 56324ebe 2022-07-14 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
182 56324ebe 2022-07-14 thomas sizeof(gotwebd->httpd_chroot));
183 56324ebe 2022-07-14 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
184 56324ebe 2022-07-14 thomas yyerror("%s: httpd_chroot truncated", __func__);
185 56324ebe 2022-07-14 thomas free($2);
186 56324ebe 2022-07-14 thomas YYERROR;
187 56324ebe 2022-07-14 thomas }
188 56324ebe 2022-07-14 thomas free($2);
189 56324ebe 2022-07-14 thomas }
190 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
191 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket = $2;
192 56324ebe 2022-07-14 thomas }
193 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
194 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket = $2;
195 56324ebe 2022-07-14 thomas } '{' optnl socketopts4 '}'
196 56324ebe 2022-07-14 thomas | UNIX_SOCKET boolean {
197 56324ebe 2022-07-14 thomas gotwebd->unix_socket = $2;
198 56324ebe 2022-07-14 thomas }
199 56324ebe 2022-07-14 thomas | UNIX_SOCKET_NAME STRING {
200 56324ebe 2022-07-14 thomas n = snprintf(gotwebd->unix_socket_name,
201 56324ebe 2022-07-14 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
202 56324ebe 2022-07-14 thomas strlen(gotwebd->httpd_chroot) ?
203 56324ebe 2022-07-14 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
204 56324ebe 2022-07-14 thomas if (n < 0) {
205 56324ebe 2022-07-14 thomas yyerror("%s: unix_socket_name truncated",
206 56324ebe 2022-07-14 thomas __func__);
207 56324ebe 2022-07-14 thomas free($2);
208 56324ebe 2022-07-14 thomas YYERROR;
209 56324ebe 2022-07-14 thomas }
210 56324ebe 2022-07-14 thomas free($2);
211 56324ebe 2022-07-14 thomas }
212 56324ebe 2022-07-14 thomas ;
213 56324ebe 2022-07-14 thomas
214 56324ebe 2022-07-14 thomas server : SERVER STRING {
215 56324ebe 2022-07-14 thomas struct server *srv;
216 56324ebe 2022-07-14 thomas
217 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
218 56324ebe 2022-07-14 thomas if (strcmp(srv->name, $2) == 0) {
219 56324ebe 2022-07-14 thomas yyerror("server name exists '%s'", $2);
220 56324ebe 2022-07-14 thomas free($2);
221 56324ebe 2022-07-14 thomas YYERROR;
222 56324ebe 2022-07-14 thomas }
223 56324ebe 2022-07-14 thomas }
224 56324ebe 2022-07-14 thomas
225 56324ebe 2022-07-14 thomas new_srv = conf_new_server($2);
226 56324ebe 2022-07-14 thomas if (new_srv->fcgi_socket)
227 56324ebe 2022-07-14 thomas if (get_addrs(new_srv->fcgi_socket_bind,
228 56324ebe 2022-07-14 thomas new_srv->al,
229 56324ebe 2022-07-14 thomas new_srv->fcgi_socket_port) == -1) {
230 56324ebe 2022-07-14 thomas yyerror("could not get tcp iface "
231 56324ebe 2022-07-14 thomas "addrs");
232 56324ebe 2022-07-14 thomas YYERROR;
233 56324ebe 2022-07-14 thomas }
234 56324ebe 2022-07-14 thomas log_debug("adding server %s", $2);
235 56324ebe 2022-07-14 thomas free($2);
236 56324ebe 2022-07-14 thomas }
237 56324ebe 2022-07-14 thomas | SERVER STRING {
238 56324ebe 2022-07-14 thomas struct server *srv;
239 56324ebe 2022-07-14 thomas
240 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
241 56324ebe 2022-07-14 thomas if (strcmp(srv->name, $2) == 0) {
242 56324ebe 2022-07-14 thomas yyerror("server name exists '%s'", $2);
243 56324ebe 2022-07-14 thomas free($2);
244 56324ebe 2022-07-14 thomas YYERROR;
245 56324ebe 2022-07-14 thomas }
246 56324ebe 2022-07-14 thomas }
247 56324ebe 2022-07-14 thomas
248 56324ebe 2022-07-14 thomas new_srv = conf_new_server($2);
249 56324ebe 2022-07-14 thomas log_debug("adding server %s", $2);
250 56324ebe 2022-07-14 thomas free($2);
251 56324ebe 2022-07-14 thomas } '{' optnl serveropts2 '}' {
252 56324ebe 2022-07-14 thomas if (get_addrs(new_srv->fcgi_socket_bind,
253 56324ebe 2022-07-14 thomas new_srv->al, new_srv->fcgi_socket_port) == -1) {
254 56324ebe 2022-07-14 thomas yyerror("could not get tcp iface addrs");
255 56324ebe 2022-07-14 thomas YYERROR;
256 56324ebe 2022-07-14 thomas }
257 56324ebe 2022-07-14 thomas }
258 56324ebe 2022-07-14 thomas ;
259 56324ebe 2022-07-14 thomas
260 56324ebe 2022-07-14 thomas serveropts1 : REPOS_PATH STRING {
261 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->repos_path, $2,
262 56324ebe 2022-07-14 thomas sizeof(new_srv->repos_path));
263 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->repos_path)) {
264 56324ebe 2022-07-14 thomas yyerror("%s: repos_path truncated", __func__);
265 56324ebe 2022-07-14 thomas free($2);
266 56324ebe 2022-07-14 thomas YYERROR;
267 56324ebe 2022-07-14 thomas }
268 56324ebe 2022-07-14 thomas free($2);
269 56324ebe 2022-07-14 thomas }
270 56324ebe 2022-07-14 thomas | SITE_NAME STRING {
271 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->site_name, $2,
272 56324ebe 2022-07-14 thomas sizeof(new_srv->site_name));
273 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->site_name)) {
274 56324ebe 2022-07-14 thomas yyerror("%s: site_name truncated", __func__);
275 56324ebe 2022-07-14 thomas free($2);
276 56324ebe 2022-07-14 thomas YYERROR;
277 56324ebe 2022-07-14 thomas }
278 56324ebe 2022-07-14 thomas free($2);
279 56324ebe 2022-07-14 thomas }
280 56324ebe 2022-07-14 thomas | SITE_OWNER STRING {
281 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->site_owner, $2,
282 56324ebe 2022-07-14 thomas sizeof(new_srv->site_owner));
283 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->site_owner)) {
284 56324ebe 2022-07-14 thomas yyerror("%s: site_owner truncated", __func__);
285 56324ebe 2022-07-14 thomas free($2);
286 56324ebe 2022-07-14 thomas YYERROR;
287 56324ebe 2022-07-14 thomas }
288 56324ebe 2022-07-14 thomas free($2);
289 56324ebe 2022-07-14 thomas }
290 56324ebe 2022-07-14 thomas | SITE_LINK STRING {
291 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->site_link, $2,
292 56324ebe 2022-07-14 thomas sizeof(new_srv->site_link));
293 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->site_link)) {
294 56324ebe 2022-07-14 thomas yyerror("%s: site_link truncated", __func__);
295 56324ebe 2022-07-14 thomas free($2);
296 56324ebe 2022-07-14 thomas YYERROR;
297 56324ebe 2022-07-14 thomas }
298 56324ebe 2022-07-14 thomas free($2);
299 56324ebe 2022-07-14 thomas }
300 56324ebe 2022-07-14 thomas | LOGO STRING {
301 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
302 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->logo)) {
303 56324ebe 2022-07-14 thomas yyerror("%s: logo truncated", __func__);
304 56324ebe 2022-07-14 thomas free($2);
305 56324ebe 2022-07-14 thomas YYERROR;
306 56324ebe 2022-07-14 thomas }
307 56324ebe 2022-07-14 thomas free($2);
308 56324ebe 2022-07-14 thomas }
309 56324ebe 2022-07-14 thomas | LOGO_URL STRING {
310 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->logo_url, $2,
311 56324ebe 2022-07-14 thomas sizeof(new_srv->logo_url));
312 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->logo_url)) {
313 56324ebe 2022-07-14 thomas yyerror("%s: logo_url truncated", __func__);
314 56324ebe 2022-07-14 thomas free($2);
315 56324ebe 2022-07-14 thomas YYERROR;
316 56324ebe 2022-07-14 thomas }
317 56324ebe 2022-07-14 thomas free($2);
318 56324ebe 2022-07-14 thomas }
319 56324ebe 2022-07-14 thomas | CUSTOM_CSS STRING {
320 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->custom_css, $2,
321 56324ebe 2022-07-14 thomas sizeof(new_srv->custom_css));
322 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->custom_css)) {
323 56324ebe 2022-07-14 thomas yyerror("%s: custom_css truncated", __func__);
324 56324ebe 2022-07-14 thomas free($2);
325 56324ebe 2022-07-14 thomas YYERROR;
326 56324ebe 2022-07-14 thomas }
327 56324ebe 2022-07-14 thomas free($2);
328 56324ebe 2022-07-14 thomas }
329 56324ebe 2022-07-14 thomas | MAX_REPOS NUMBER {
330 56324ebe 2022-07-14 thomas if ($2 > 0)
331 56324ebe 2022-07-14 thomas new_srv->max_repos = $2;
332 56324ebe 2022-07-14 thomas }
333 56324ebe 2022-07-14 thomas | SHOW_SITE_OWNER boolean {
334 56324ebe 2022-07-14 thomas new_srv->show_site_owner = $2;
335 56324ebe 2022-07-14 thomas }
336 56324ebe 2022-07-14 thomas | SHOW_REPO_OWNER boolean {
337 56324ebe 2022-07-14 thomas new_srv->show_repo_owner = $2;
338 56324ebe 2022-07-14 thomas }
339 56324ebe 2022-07-14 thomas | SHOW_REPO_AGE boolean {
340 56324ebe 2022-07-14 thomas new_srv->show_repo_age = $2;
341 56324ebe 2022-07-14 thomas }
342 56324ebe 2022-07-14 thomas | SHOW_REPO_DESCRIPTION boolean {
343 56324ebe 2022-07-14 thomas new_srv->show_repo_description = $2;
344 56324ebe 2022-07-14 thomas }
345 56324ebe 2022-07-14 thomas | SHOW_REPO_CLONEURL boolean {
346 56324ebe 2022-07-14 thomas new_srv->show_repo_cloneurl = $2;
347 56324ebe 2022-07-14 thomas }
348 56324ebe 2022-07-14 thomas | MAX_REPOS_DISPLAY NUMBER {
349 56324ebe 2022-07-14 thomas new_srv->max_repos_display = $2;
350 56324ebe 2022-07-14 thomas }
351 56324ebe 2022-07-14 thomas | MAX_COMMITS_DISPLAY NUMBER {
352 56324ebe 2022-07-14 thomas if ($2 > 0)
353 56324ebe 2022-07-14 thomas new_srv->max_commits_display = $2;
354 56324ebe 2022-07-14 thomas }
355 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
356 56324ebe 2022-07-14 thomas new_srv->fcgi_socket = $2;
357 56324ebe 2022-07-14 thomas }
358 56324ebe 2022-07-14 thomas | FCGI_SOCKET boolean {
359 56324ebe 2022-07-14 thomas new_srv->fcgi_socket = $2;
360 56324ebe 2022-07-14 thomas } '{' optnl socketopts2 '}'
361 56324ebe 2022-07-14 thomas | UNIX_SOCKET boolean {
362 56324ebe 2022-07-14 thomas new_srv->unix_socket = $2;
363 56324ebe 2022-07-14 thomas }
364 56324ebe 2022-07-14 thomas | UNIX_SOCKET_NAME STRING {
365 56324ebe 2022-07-14 thomas n = snprintf(new_srv->unix_socket_name,
366 56324ebe 2022-07-14 thomas sizeof(new_srv->unix_socket_name), "%s%s",
367 56324ebe 2022-07-14 thomas strlen(gotwebd->httpd_chroot) ?
368 56324ebe 2022-07-14 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
369 56324ebe 2022-07-14 thomas if (n < 0) {
370 56324ebe 2022-07-14 thomas yyerror("%s: unix_socket_name truncated",
371 56324ebe 2022-07-14 thomas __func__);
372 56324ebe 2022-07-14 thomas free($2);
373 56324ebe 2022-07-14 thomas YYERROR;
374 56324ebe 2022-07-14 thomas }
375 56324ebe 2022-07-14 thomas free($2);
376 56324ebe 2022-07-14 thomas }
377 56324ebe 2022-07-14 thomas ;
378 56324ebe 2022-07-14 thomas
379 56324ebe 2022-07-14 thomas serveropts2 : serveropts2 serveropts1 nl
380 56324ebe 2022-07-14 thomas | serveropts1 optnl
381 56324ebe 2022-07-14 thomas ;
382 56324ebe 2022-07-14 thomas
383 56324ebe 2022-07-14 thomas socketopts1 : BIND INTERFACE STRING {
384 56324ebe 2022-07-14 thomas n = strlcpy(new_srv->fcgi_socket_bind, $3,
385 56324ebe 2022-07-14 thomas sizeof(new_srv->fcgi_socket_bind));
386 56324ebe 2022-07-14 thomas if (n >= sizeof(new_srv->fcgi_socket_bind)) {
387 56324ebe 2022-07-14 thomas yyerror("%s: fcgi_socket_bind truncated",
388 56324ebe 2022-07-14 thomas __func__);
389 56324ebe 2022-07-14 thomas free($3);
390 56324ebe 2022-07-14 thomas YYERROR;
391 56324ebe 2022-07-14 thomas }
392 56324ebe 2022-07-14 thomas free($3);
393 56324ebe 2022-07-14 thomas }
394 56324ebe 2022-07-14 thomas | PORT fcgiport {
395 56324ebe 2022-07-14 thomas struct server *srv;
396 56324ebe 2022-07-14 thomas
397 56324ebe 2022-07-14 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
398 56324ebe 2022-07-14 thomas if (srv->fcgi_socket_port == $2) {
399 56324ebe 2022-07-14 thomas yyerror("port already assigned");
400 56324ebe 2022-07-14 thomas YYERROR;
401 56324ebe 2022-07-14 thomas }
402 56324ebe 2022-07-14 thomas }
403 56324ebe 2022-07-14 thomas new_srv->fcgi_socket_port = $2;
404 56324ebe 2022-07-14 thomas }
405 56324ebe 2022-07-14 thomas ;
406 56324ebe 2022-07-14 thomas
407 56324ebe 2022-07-14 thomas socketopts2 : socketopts2 socketopts1 nl
408 56324ebe 2022-07-14 thomas | socketopts1 optnl
409 56324ebe 2022-07-14 thomas ;
410 56324ebe 2022-07-14 thomas
411 56324ebe 2022-07-14 thomas socketopts3 : BIND INTERFACE STRING {
412 56324ebe 2022-07-14 thomas n = strlcpy(gotwebd->fcgi_socket_bind, $3,
413 56324ebe 2022-07-14 thomas sizeof(gotwebd->fcgi_socket_bind));
414 56324ebe 2022-07-14 thomas if (n >= sizeof(gotwebd->fcgi_socket_bind)) {
415 56324ebe 2022-07-14 thomas yyerror("%s: fcgi_socket_bind truncated",
416 56324ebe 2022-07-14 thomas __func__);
417 56324ebe 2022-07-14 thomas free($3);
418 56324ebe 2022-07-14 thomas YYERROR;
419 56324ebe 2022-07-14 thomas }
420 56324ebe 2022-07-14 thomas free($3);
421 56324ebe 2022-07-14 thomas }
422 56324ebe 2022-07-14 thomas | PORT fcgiport {
423 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket_port = $2;
424 56324ebe 2022-07-14 thomas }
425 56324ebe 2022-07-14 thomas ;
426 56324ebe 2022-07-14 thomas
427 56324ebe 2022-07-14 thomas socketopts4 : socketopts4 socketopts3 nl
428 56324ebe 2022-07-14 thomas | socketopts3 optnl
429 56324ebe 2022-07-14 thomas ;
430 56324ebe 2022-07-14 thomas
431 56324ebe 2022-07-14 thomas nl : '\n' optnl
432 56324ebe 2022-07-14 thomas ;
433 56324ebe 2022-07-14 thomas
434 56324ebe 2022-07-14 thomas optnl : '\n' optnl /* zero or more newlines */
435 56324ebe 2022-07-14 thomas | /* empty */
436 56324ebe 2022-07-14 thomas ;
437 56324ebe 2022-07-14 thomas
438 56324ebe 2022-07-14 thomas %%
439 56324ebe 2022-07-14 thomas
440 56324ebe 2022-07-14 thomas struct keywords {
441 56324ebe 2022-07-14 thomas const char *k_name;
442 56324ebe 2022-07-14 thomas int k_val;
443 56324ebe 2022-07-14 thomas };
444 56324ebe 2022-07-14 thomas
445 56324ebe 2022-07-14 thomas int
446 56324ebe 2022-07-14 thomas yyerror(const char *fmt, ...)
447 56324ebe 2022-07-14 thomas {
448 56324ebe 2022-07-14 thomas va_list ap;
449 56324ebe 2022-07-14 thomas char *msg;
450 56324ebe 2022-07-14 thomas
451 56324ebe 2022-07-14 thomas file->errors++;
452 56324ebe 2022-07-14 thomas va_start(ap, fmt);
453 56324ebe 2022-07-14 thomas if (vasprintf(&msg, fmt, ap) == -1)
454 56324ebe 2022-07-14 thomas fatalx("yyerror vasprintf");
455 56324ebe 2022-07-14 thomas va_end(ap);
456 56324ebe 2022-07-14 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
457 56324ebe 2022-07-14 thomas free(msg);
458 56324ebe 2022-07-14 thomas return (0);
459 56324ebe 2022-07-14 thomas }
460 56324ebe 2022-07-14 thomas
461 56324ebe 2022-07-14 thomas int
462 56324ebe 2022-07-14 thomas kw_cmp(const void *k, const void *e)
463 56324ebe 2022-07-14 thomas {
464 56324ebe 2022-07-14 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
465 56324ebe 2022-07-14 thomas }
466 56324ebe 2022-07-14 thomas
467 56324ebe 2022-07-14 thomas int
468 56324ebe 2022-07-14 thomas lookup(char *s)
469 56324ebe 2022-07-14 thomas {
470 56324ebe 2022-07-14 thomas /* This has to be sorted always. */
471 56324ebe 2022-07-14 thomas static const struct keywords keywords[] = {
472 56324ebe 2022-07-14 thomas { "bind", BIND },
473 56324ebe 2022-07-14 thomas { "chroot", CHROOT },
474 56324ebe 2022-07-14 thomas { "custom_css", CUSTOM_CSS },
475 56324ebe 2022-07-14 thomas { "fcgi_socket", FCGI_SOCKET },
476 56324ebe 2022-07-14 thomas { "interface", INTERFACE },
477 56324ebe 2022-07-14 thomas { "logo", LOGO },
478 56324ebe 2022-07-14 thomas { "logo_url" , LOGO_URL },
479 56324ebe 2022-07-14 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
480 56324ebe 2022-07-14 thomas { "max_repos", MAX_REPOS },
481 56324ebe 2022-07-14 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
482 56324ebe 2022-07-14 thomas { "port", PORT },
483 56324ebe 2022-07-14 thomas { "prefork", PREFORK },
484 56324ebe 2022-07-14 thomas { "repos_path", REPOS_PATH },
485 56324ebe 2022-07-14 thomas { "server", SERVER },
486 56324ebe 2022-07-14 thomas { "show_repo_age", SHOW_REPO_AGE },
487 56324ebe 2022-07-14 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
488 56324ebe 2022-07-14 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
489 56324ebe 2022-07-14 thomas { "show_repo_owner", SHOW_REPO_OWNER },
490 56324ebe 2022-07-14 thomas { "show_site_owner", SHOW_SITE_OWNER },
491 56324ebe 2022-07-14 thomas { "site_link", SITE_LINK },
492 56324ebe 2022-07-14 thomas { "site_name", SITE_NAME },
493 56324ebe 2022-07-14 thomas { "site_owner", SITE_OWNER },
494 56324ebe 2022-07-14 thomas { "unix_socket", UNIX_SOCKET },
495 56324ebe 2022-07-14 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
496 56324ebe 2022-07-14 thomas };
497 56324ebe 2022-07-14 thomas const struct keywords *p;
498 56324ebe 2022-07-14 thomas
499 56324ebe 2022-07-14 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
500 56324ebe 2022-07-14 thomas sizeof(keywords[0]), kw_cmp);
501 56324ebe 2022-07-14 thomas
502 56324ebe 2022-07-14 thomas if (p)
503 56324ebe 2022-07-14 thomas return (p->k_val);
504 56324ebe 2022-07-14 thomas else
505 56324ebe 2022-07-14 thomas return (STRING);
506 56324ebe 2022-07-14 thomas }
507 56324ebe 2022-07-14 thomas
508 56324ebe 2022-07-14 thomas #define MAXPUSHBACK 128
509 56324ebe 2022-07-14 thomas
510 56324ebe 2022-07-14 thomas unsigned char *parsebuf;
511 56324ebe 2022-07-14 thomas int parseindex;
512 56324ebe 2022-07-14 thomas unsigned char pushback_buffer[MAXPUSHBACK];
513 56324ebe 2022-07-14 thomas int pushback_index = 0;
514 56324ebe 2022-07-14 thomas
515 56324ebe 2022-07-14 thomas int
516 56324ebe 2022-07-14 thomas lgetc(int quotec)
517 56324ebe 2022-07-14 thomas {
518 56324ebe 2022-07-14 thomas int c, next;
519 56324ebe 2022-07-14 thomas
520 56324ebe 2022-07-14 thomas if (parsebuf) {
521 56324ebe 2022-07-14 thomas /* Read character from the parsebuffer instead of input. */
522 56324ebe 2022-07-14 thomas if (parseindex >= 0) {
523 56324ebe 2022-07-14 thomas c = parsebuf[parseindex++];
524 56324ebe 2022-07-14 thomas if (c != '\0')
525 56324ebe 2022-07-14 thomas return (c);
526 56324ebe 2022-07-14 thomas parsebuf = NULL;
527 56324ebe 2022-07-14 thomas } else
528 56324ebe 2022-07-14 thomas parseindex++;
529 56324ebe 2022-07-14 thomas }
530 56324ebe 2022-07-14 thomas
531 56324ebe 2022-07-14 thomas if (pushback_index)
532 56324ebe 2022-07-14 thomas return (pushback_buffer[--pushback_index]);
533 56324ebe 2022-07-14 thomas
534 56324ebe 2022-07-14 thomas if (quotec) {
535 56324ebe 2022-07-14 thomas c = getc(file->stream);
536 56324ebe 2022-07-14 thomas if (c == EOF)
537 56324ebe 2022-07-14 thomas yyerror("reached end of file while parsing "
538 56324ebe 2022-07-14 thomas "quoted string");
539 56324ebe 2022-07-14 thomas return (c);
540 56324ebe 2022-07-14 thomas }
541 56324ebe 2022-07-14 thomas
542 56324ebe 2022-07-14 thomas c = getc(file->stream);
543 56324ebe 2022-07-14 thomas while (c == '\\') {
544 56324ebe 2022-07-14 thomas next = getc(file->stream);
545 56324ebe 2022-07-14 thomas if (next != '\n') {
546 56324ebe 2022-07-14 thomas c = next;
547 56324ebe 2022-07-14 thomas break;
548 56324ebe 2022-07-14 thomas }
549 56324ebe 2022-07-14 thomas yylval.lineno = file->lineno;
550 56324ebe 2022-07-14 thomas file->lineno++;
551 56324ebe 2022-07-14 thomas c = getc(file->stream);
552 56324ebe 2022-07-14 thomas }
553 56324ebe 2022-07-14 thomas
554 56324ebe 2022-07-14 thomas return (c);
555 56324ebe 2022-07-14 thomas }
556 56324ebe 2022-07-14 thomas
557 56324ebe 2022-07-14 thomas int
558 56324ebe 2022-07-14 thomas lungetc(int c)
559 56324ebe 2022-07-14 thomas {
560 56324ebe 2022-07-14 thomas if (c == EOF)
561 56324ebe 2022-07-14 thomas return (EOF);
562 56324ebe 2022-07-14 thomas if (parsebuf) {
563 56324ebe 2022-07-14 thomas parseindex--;
564 56324ebe 2022-07-14 thomas if (parseindex >= 0)
565 56324ebe 2022-07-14 thomas return (c);
566 56324ebe 2022-07-14 thomas }
567 56324ebe 2022-07-14 thomas if (pushback_index < MAXPUSHBACK-1)
568 56324ebe 2022-07-14 thomas return (pushback_buffer[pushback_index++] = c);
569 56324ebe 2022-07-14 thomas else
570 56324ebe 2022-07-14 thomas return (EOF);
571 56324ebe 2022-07-14 thomas }
572 56324ebe 2022-07-14 thomas
573 56324ebe 2022-07-14 thomas int
574 56324ebe 2022-07-14 thomas findeol(void)
575 56324ebe 2022-07-14 thomas {
576 56324ebe 2022-07-14 thomas int c;
577 56324ebe 2022-07-14 thomas
578 56324ebe 2022-07-14 thomas parsebuf = NULL;
579 56324ebe 2022-07-14 thomas
580 56324ebe 2022-07-14 thomas /* Skip to either EOF or the first real EOL. */
581 56324ebe 2022-07-14 thomas while (1) {
582 56324ebe 2022-07-14 thomas if (pushback_index)
583 56324ebe 2022-07-14 thomas c = pushback_buffer[--pushback_index];
584 56324ebe 2022-07-14 thomas else
585 56324ebe 2022-07-14 thomas c = lgetc(0);
586 56324ebe 2022-07-14 thomas if (c == '\n') {
587 56324ebe 2022-07-14 thomas file->lineno++;
588 56324ebe 2022-07-14 thomas break;
589 56324ebe 2022-07-14 thomas }
590 56324ebe 2022-07-14 thomas if (c == EOF)
591 56324ebe 2022-07-14 thomas break;
592 56324ebe 2022-07-14 thomas }
593 56324ebe 2022-07-14 thomas return (ERROR);
594 56324ebe 2022-07-14 thomas }
595 56324ebe 2022-07-14 thomas
596 56324ebe 2022-07-14 thomas int
597 56324ebe 2022-07-14 thomas yylex(void)
598 56324ebe 2022-07-14 thomas {
599 56324ebe 2022-07-14 thomas unsigned char buf[8096];
600 56324ebe 2022-07-14 thomas unsigned char *p, *val;
601 56324ebe 2022-07-14 thomas int quotec, next, c;
602 56324ebe 2022-07-14 thomas int token;
603 56324ebe 2022-07-14 thomas
604 56324ebe 2022-07-14 thomas top:
605 56324ebe 2022-07-14 thomas p = buf;
606 56324ebe 2022-07-14 thomas c = lgetc(0);
607 56324ebe 2022-07-14 thomas while (c == ' ' || c == '\t')
608 56324ebe 2022-07-14 thomas c = lgetc(0); /* nothing */
609 56324ebe 2022-07-14 thomas
610 56324ebe 2022-07-14 thomas yylval.lineno = file->lineno;
611 56324ebe 2022-07-14 thomas if (c == '#') {
612 56324ebe 2022-07-14 thomas c = lgetc(0);
613 56324ebe 2022-07-14 thomas while (c != '\n' && c != EOF)
614 56324ebe 2022-07-14 thomas c = lgetc(0); /* nothing */
615 56324ebe 2022-07-14 thomas }
616 56324ebe 2022-07-14 thomas if (c == '$' && parsebuf == NULL) {
617 56324ebe 2022-07-14 thomas while (1) {
618 56324ebe 2022-07-14 thomas c = lgetc(0);
619 56324ebe 2022-07-14 thomas if (c == EOF)
620 56324ebe 2022-07-14 thomas return (0);
621 56324ebe 2022-07-14 thomas
622 56324ebe 2022-07-14 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
623 56324ebe 2022-07-14 thomas yyerror("string too long");
624 56324ebe 2022-07-14 thomas return (findeol());
625 56324ebe 2022-07-14 thomas }
626 56324ebe 2022-07-14 thomas if (isalnum(c) || c == '_') {
627 56324ebe 2022-07-14 thomas *p++ = c;
628 56324ebe 2022-07-14 thomas continue;
629 56324ebe 2022-07-14 thomas }
630 56324ebe 2022-07-14 thomas *p = '\0';
631 56324ebe 2022-07-14 thomas lungetc(c);
632 56324ebe 2022-07-14 thomas break;
633 56324ebe 2022-07-14 thomas }
634 56324ebe 2022-07-14 thomas val = symget(buf);
635 56324ebe 2022-07-14 thomas if (val == NULL) {
636 56324ebe 2022-07-14 thomas yyerror("macro '%s' not defined", buf);
637 56324ebe 2022-07-14 thomas return (findeol());
638 56324ebe 2022-07-14 thomas }
639 56324ebe 2022-07-14 thomas parsebuf = val;
640 56324ebe 2022-07-14 thomas parseindex = 0;
641 56324ebe 2022-07-14 thomas goto top;
642 56324ebe 2022-07-14 thomas }
643 56324ebe 2022-07-14 thomas
644 56324ebe 2022-07-14 thomas switch (c) {
645 56324ebe 2022-07-14 thomas case '\'':
646 56324ebe 2022-07-14 thomas case '"':
647 56324ebe 2022-07-14 thomas quotec = c;
648 56324ebe 2022-07-14 thomas while (1) {
649 56324ebe 2022-07-14 thomas c = lgetc(quotec);
650 56324ebe 2022-07-14 thomas if (c == EOF)
651 56324ebe 2022-07-14 thomas return (0);
652 56324ebe 2022-07-14 thomas if (c == '\n') {
653 56324ebe 2022-07-14 thomas file->lineno++;
654 56324ebe 2022-07-14 thomas continue;
655 56324ebe 2022-07-14 thomas } else if (c == '\\') {
656 56324ebe 2022-07-14 thomas next = lgetc(quotec);
657 56324ebe 2022-07-14 thomas if (next == EOF)
658 56324ebe 2022-07-14 thomas return (0);
659 56324ebe 2022-07-14 thomas if (next == quotec || c == ' ' || c == '\t')
660 56324ebe 2022-07-14 thomas c = next;
661 56324ebe 2022-07-14 thomas else if (next == '\n') {
662 56324ebe 2022-07-14 thomas file->lineno++;
663 56324ebe 2022-07-14 thomas continue;
664 56324ebe 2022-07-14 thomas } else
665 56324ebe 2022-07-14 thomas lungetc(next);
666 56324ebe 2022-07-14 thomas } else if (c == quotec) {
667 56324ebe 2022-07-14 thomas *p = '\0';
668 56324ebe 2022-07-14 thomas break;
669 56324ebe 2022-07-14 thomas } else if (c == '\0') {
670 56324ebe 2022-07-14 thomas yyerror("syntax error");
671 56324ebe 2022-07-14 thomas return (findeol());
672 56324ebe 2022-07-14 thomas }
673 56324ebe 2022-07-14 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
674 56324ebe 2022-07-14 thomas yyerror("string too long");
675 56324ebe 2022-07-14 thomas return (findeol());
676 56324ebe 2022-07-14 thomas }
677 56324ebe 2022-07-14 thomas *p++ = c;
678 56324ebe 2022-07-14 thomas }
679 56324ebe 2022-07-14 thomas yylval.v.string = strdup(buf);
680 56324ebe 2022-07-14 thomas if (yylval.v.string == NULL)
681 56324ebe 2022-07-14 thomas err(1, "yylex: strdup");
682 56324ebe 2022-07-14 thomas return (STRING);
683 56324ebe 2022-07-14 thomas }
684 56324ebe 2022-07-14 thomas
685 56324ebe 2022-07-14 thomas #define allowed_to_end_number(x) \
686 56324ebe 2022-07-14 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
687 56324ebe 2022-07-14 thomas
688 56324ebe 2022-07-14 thomas if (c == '-' || isdigit(c)) {
689 56324ebe 2022-07-14 thomas do {
690 56324ebe 2022-07-14 thomas *p++ = c;
691 56324ebe 2022-07-14 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
692 56324ebe 2022-07-14 thomas yyerror("string too long");
693 56324ebe 2022-07-14 thomas return (findeol());
694 56324ebe 2022-07-14 thomas }
695 56324ebe 2022-07-14 thomas c = lgetc(0);
696 56324ebe 2022-07-14 thomas } while (c != EOF && isdigit(c));
697 56324ebe 2022-07-14 thomas lungetc(c);
698 56324ebe 2022-07-14 thomas if (p == buf + 1 && buf[0] == '-')
699 56324ebe 2022-07-14 thomas goto nodigits;
700 56324ebe 2022-07-14 thomas if (c == EOF || allowed_to_end_number(c)) {
701 56324ebe 2022-07-14 thomas const char *errstr = NULL;
702 56324ebe 2022-07-14 thomas
703 56324ebe 2022-07-14 thomas *p = '\0';
704 56324ebe 2022-07-14 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
705 56324ebe 2022-07-14 thomas LLONG_MAX, &errstr);
706 56324ebe 2022-07-14 thomas if (errstr) {
707 56324ebe 2022-07-14 thomas yyerror("\"%s\" invalid number: %s",
708 56324ebe 2022-07-14 thomas buf, errstr);
709 56324ebe 2022-07-14 thomas return (findeol());
710 56324ebe 2022-07-14 thomas }
711 56324ebe 2022-07-14 thomas return (NUMBER);
712 56324ebe 2022-07-14 thomas } else {
713 56324ebe 2022-07-14 thomas nodigits:
714 56324ebe 2022-07-14 thomas while (p > buf + 1)
715 56324ebe 2022-07-14 thomas lungetc(*--p);
716 56324ebe 2022-07-14 thomas c = *--p;
717 56324ebe 2022-07-14 thomas if (c == '-')
718 56324ebe 2022-07-14 thomas return (c);
719 56324ebe 2022-07-14 thomas }
720 56324ebe 2022-07-14 thomas }
721 56324ebe 2022-07-14 thomas
722 56324ebe 2022-07-14 thomas #define allowed_in_string(x) \
723 56324ebe 2022-07-14 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
724 56324ebe 2022-07-14 thomas x != '{' && x != '}' && \
725 56324ebe 2022-07-14 thomas x != '!' && x != '=' && x != '#' && \
726 56324ebe 2022-07-14 thomas x != ','))
727 56324ebe 2022-07-14 thomas
728 56324ebe 2022-07-14 thomas if (isalnum(c) || c == ':' || c == '_') {
729 56324ebe 2022-07-14 thomas do {
730 56324ebe 2022-07-14 thomas *p++ = c;
731 56324ebe 2022-07-14 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
732 56324ebe 2022-07-14 thomas yyerror("string too long");
733 56324ebe 2022-07-14 thomas return (findeol());
734 56324ebe 2022-07-14 thomas }
735 56324ebe 2022-07-14 thomas c = lgetc(0);
736 56324ebe 2022-07-14 thomas } while (c != EOF && (allowed_in_string(c)));
737 56324ebe 2022-07-14 thomas lungetc(c);
738 56324ebe 2022-07-14 thomas *p = '\0';
739 56324ebe 2022-07-14 thomas token = lookup(buf);
740 56324ebe 2022-07-14 thomas if (token == STRING) {
741 56324ebe 2022-07-14 thomas yylval.v.string = strdup(buf);
742 56324ebe 2022-07-14 thomas if (yylval.v.string == NULL)
743 56324ebe 2022-07-14 thomas err(1, "yylex: strdup");
744 56324ebe 2022-07-14 thomas }
745 56324ebe 2022-07-14 thomas return (token);
746 56324ebe 2022-07-14 thomas }
747 56324ebe 2022-07-14 thomas if (c == '\n') {
748 56324ebe 2022-07-14 thomas yylval.lineno = file->lineno;
749 56324ebe 2022-07-14 thomas file->lineno++;
750 56324ebe 2022-07-14 thomas }
751 56324ebe 2022-07-14 thomas if (c == EOF)
752 56324ebe 2022-07-14 thomas return (0);
753 56324ebe 2022-07-14 thomas return (c);
754 56324ebe 2022-07-14 thomas }
755 56324ebe 2022-07-14 thomas
756 56324ebe 2022-07-14 thomas int
757 56324ebe 2022-07-14 thomas check_file_secrecy(int fd, const char *fname)
758 56324ebe 2022-07-14 thomas {
759 56324ebe 2022-07-14 thomas struct stat st;
760 56324ebe 2022-07-14 thomas
761 56324ebe 2022-07-14 thomas if (fstat(fd, &st)) {
762 56324ebe 2022-07-14 thomas log_warn("cannot stat %s", fname);
763 56324ebe 2022-07-14 thomas return (-1);
764 56324ebe 2022-07-14 thomas }
765 56324ebe 2022-07-14 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
766 56324ebe 2022-07-14 thomas log_warnx("%s: owner not root or current user", fname);
767 56324ebe 2022-07-14 thomas return (-1);
768 56324ebe 2022-07-14 thomas }
769 56324ebe 2022-07-14 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
770 56324ebe 2022-07-14 thomas log_warnx("%s: group writable or world read/writable", fname);
771 56324ebe 2022-07-14 thomas return (-1);
772 56324ebe 2022-07-14 thomas }
773 56324ebe 2022-07-14 thomas return (0);
774 56324ebe 2022-07-14 thomas }
775 56324ebe 2022-07-14 thomas
776 56324ebe 2022-07-14 thomas struct file *
777 56324ebe 2022-07-14 thomas newfile(const char *name, int secret)
778 56324ebe 2022-07-14 thomas {
779 56324ebe 2022-07-14 thomas struct file *nfile;
780 56324ebe 2022-07-14 thomas
781 56324ebe 2022-07-14 thomas nfile = calloc(1, sizeof(struct file));
782 56324ebe 2022-07-14 thomas if (nfile == NULL) {
783 56324ebe 2022-07-14 thomas log_warn("calloc");
784 56324ebe 2022-07-14 thomas return (NULL);
785 56324ebe 2022-07-14 thomas }
786 56324ebe 2022-07-14 thomas nfile->name = strdup(name);
787 56324ebe 2022-07-14 thomas if (nfile->name == NULL) {
788 56324ebe 2022-07-14 thomas log_warn("strdup");
789 56324ebe 2022-07-14 thomas free(nfile);
790 56324ebe 2022-07-14 thomas return (NULL);
791 56324ebe 2022-07-14 thomas }
792 56324ebe 2022-07-14 thomas nfile->stream = fopen(nfile->name, "r");
793 56324ebe 2022-07-14 thomas if (nfile->stream == NULL) {
794 56324ebe 2022-07-14 thomas /* no warning, we don't require a conf file */
795 56324ebe 2022-07-14 thomas free(nfile->name);
796 56324ebe 2022-07-14 thomas free(nfile);
797 56324ebe 2022-07-14 thomas return (NULL);
798 56324ebe 2022-07-14 thomas } else if (secret &&
799 56324ebe 2022-07-14 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
800 56324ebe 2022-07-14 thomas fclose(nfile->stream);
801 56324ebe 2022-07-14 thomas free(nfile->name);
802 56324ebe 2022-07-14 thomas free(nfile);
803 56324ebe 2022-07-14 thomas return (NULL);
804 56324ebe 2022-07-14 thomas }
805 56324ebe 2022-07-14 thomas nfile->lineno = 1;
806 56324ebe 2022-07-14 thomas return (nfile);
807 56324ebe 2022-07-14 thomas }
808 56324ebe 2022-07-14 thomas
809 56324ebe 2022-07-14 thomas static void
810 56324ebe 2022-07-14 thomas closefile(struct file *xfile)
811 56324ebe 2022-07-14 thomas {
812 56324ebe 2022-07-14 thomas fclose(xfile->stream);
813 56324ebe 2022-07-14 thomas free(xfile->name);
814 56324ebe 2022-07-14 thomas free(xfile);
815 56324ebe 2022-07-14 thomas }
816 56324ebe 2022-07-14 thomas
817 56324ebe 2022-07-14 thomas int
818 56324ebe 2022-07-14 thomas parse_config(const char *filename, struct gotwebd *env)
819 56324ebe 2022-07-14 thomas {
820 56324ebe 2022-07-14 thomas struct sym *sym, *next;
821 56324ebe 2022-07-14 thomas
822 56324ebe 2022-07-14 thomas file = newfile(filename, 0);
823 56324ebe 2022-07-14 thomas if (file == NULL)
824 56324ebe 2022-07-14 thomas /* just return, as we don't require a conf file */
825 56324ebe 2022-07-14 thomas return (0);
826 56324ebe 2022-07-14 thomas
827 56324ebe 2022-07-14 thomas if (config_init(env) == -1)
828 56324ebe 2022-07-14 thomas fatalx("failed to initialize configuration");
829 56324ebe 2022-07-14 thomas
830 56324ebe 2022-07-14 thomas gotwebd = env;
831 56324ebe 2022-07-14 thomas
832 56324ebe 2022-07-14 thomas yyparse();
833 56324ebe 2022-07-14 thomas errors = file->errors;
834 56324ebe 2022-07-14 thomas closefile(file);
835 56324ebe 2022-07-14 thomas
836 56324ebe 2022-07-14 thomas /* Free macros and check which have not been used. */
837 56324ebe 2022-07-14 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
838 56324ebe 2022-07-14 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
839 56324ebe 2022-07-14 thomas fprintf(stderr, "warning: macro '%s' not used\n",
840 56324ebe 2022-07-14 thomas sym->nam);
841 56324ebe 2022-07-14 thomas if (!sym->persist) {
842 56324ebe 2022-07-14 thomas free(sym->nam);
843 56324ebe 2022-07-14 thomas free(sym->val);
844 56324ebe 2022-07-14 thomas TAILQ_REMOVE(&symhead, sym, entry);
845 56324ebe 2022-07-14 thomas free(sym);
846 56324ebe 2022-07-14 thomas }
847 56324ebe 2022-07-14 thomas }
848 56324ebe 2022-07-14 thomas
849 56324ebe 2022-07-14 thomas if (errors)
850 56324ebe 2022-07-14 thomas return (-1);
851 56324ebe 2022-07-14 thomas
852 56324ebe 2022-07-14 thomas /* just add default server if no config specified */
853 56324ebe 2022-07-14 thomas if (gotwebd->server_cnt == 0) {
854 56324ebe 2022-07-14 thomas new_srv = conf_new_server(D_SITENAME);
855 56324ebe 2022-07-14 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
856 56324ebe 2022-07-14 thomas }
857 56324ebe 2022-07-14 thomas
858 56324ebe 2022-07-14 thomas /* setup our listening sockets */
859 56324ebe 2022-07-14 thomas sockets_parse_sockets(env);
860 56324ebe 2022-07-14 thomas
861 56324ebe 2022-07-14 thomas return (0);
862 56324ebe 2022-07-14 thomas }
863 56324ebe 2022-07-14 thomas
864 56324ebe 2022-07-14 thomas struct server *
865 56324ebe 2022-07-14 thomas conf_new_server(const char *name)
866 56324ebe 2022-07-14 thomas {
867 56324ebe 2022-07-14 thomas struct server *srv = NULL;
868 56324ebe 2022-07-14 thomas int val;
869 56324ebe 2022-07-14 thomas
870 56324ebe 2022-07-14 thomas srv = calloc(1, sizeof(*srv));
871 56324ebe 2022-07-14 thomas if (srv == NULL)
872 56324ebe 2022-07-14 thomas fatalx("%s: calloc", __func__);
873 56324ebe 2022-07-14 thomas
874 56324ebe 2022-07-14 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
875 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->name))
876 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
877 56324ebe 2022-07-14 thomas n = snprintf(srv->unix_socket_name,
878 56324ebe 2022-07-14 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
879 56324ebe 2022-07-14 thomas D_UNIX_SOCKET);
880 56324ebe 2022-07-14 thomas if (n < 0)
881 56324ebe 2022-07-14 thomas fatalx("%s: snprintf", __func__);
882 56324ebe 2022-07-14 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
883 56324ebe 2022-07-14 thomas sizeof(srv->repos_path));
884 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->repos_path))
885 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
886 56324ebe 2022-07-14 thomas n = strlcpy(srv->site_name, D_SITENAME,
887 56324ebe 2022-07-14 thomas sizeof(srv->site_name));
888 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->site_name))
889 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
890 56324ebe 2022-07-14 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
891 56324ebe 2022-07-14 thomas sizeof(srv->site_owner));
892 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->site_owner))
893 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
894 56324ebe 2022-07-14 thomas n = strlcpy(srv->site_link, D_SITELINK,
895 56324ebe 2022-07-14 thomas sizeof(srv->site_link));
896 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->site_link))
897 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
898 56324ebe 2022-07-14 thomas n = strlcpy(srv->logo, D_GOTLOGO,
899 56324ebe 2022-07-14 thomas sizeof(srv->logo));
900 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->logo))
901 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
902 56324ebe 2022-07-14 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
903 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->logo_url))
904 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
905 56324ebe 2022-07-14 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
906 56324ebe 2022-07-14 thomas if (n >= sizeof(srv->custom_css))
907 56324ebe 2022-07-14 thomas fatalx("%s: strlcpy", __func__);
908 56324ebe 2022-07-14 thomas
909 56324ebe 2022-07-14 thomas val = getservice(D_FCGI_PORT);
910 56324ebe 2022-07-14 thomas srv->fcgi_socket_port = gotwebd->fcgi_socket_port ?
911 56324ebe 2022-07-14 thomas gotwebd->fcgi_socket_port: htons(val);
912 56324ebe 2022-07-14 thomas
913 56324ebe 2022-07-14 thomas srv->show_site_owner = D_SHOWSOWNER;
914 56324ebe 2022-07-14 thomas srv->show_repo_owner = D_SHOWROWNER;
915 56324ebe 2022-07-14 thomas srv->show_repo_age = D_SHOWAGE;
916 56324ebe 2022-07-14 thomas srv->show_repo_description = D_SHOWDESC;
917 56324ebe 2022-07-14 thomas srv->show_repo_cloneurl = D_SHOWURL;
918 56324ebe 2022-07-14 thomas
919 56324ebe 2022-07-14 thomas srv->max_repos_display = D_MAXREPODISP;
920 56324ebe 2022-07-14 thomas srv->max_commits_display = D_MAXCOMMITDISP;
921 56324ebe 2022-07-14 thomas srv->max_repos = D_MAXREPO;
922 56324ebe 2022-07-14 thomas
923 56324ebe 2022-07-14 thomas srv->unix_socket = 1;
924 56324ebe 2022-07-14 thomas srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
925 56324ebe 2022-07-14 thomas
926 56324ebe 2022-07-14 thomas if ((srv->al = calloc(1, sizeof(*srv->al))) == NULL)
927 56324ebe 2022-07-14 thomas fatalx("%s: calloc", __func__);
928 56324ebe 2022-07-14 thomas
929 56324ebe 2022-07-14 thomas TAILQ_INIT(srv->al);
930 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(gotwebd->servers, srv, entry);
931 56324ebe 2022-07-14 thomas gotwebd->server_cnt++;
932 56324ebe 2022-07-14 thomas
933 56324ebe 2022-07-14 thomas return srv;
934 56324ebe 2022-07-14 thomas };
935 56324ebe 2022-07-14 thomas
936 56324ebe 2022-07-14 thomas int
937 56324ebe 2022-07-14 thomas symset(const char *nam, const char *val, int persist)
938 56324ebe 2022-07-14 thomas {
939 56324ebe 2022-07-14 thomas struct sym *sym;
940 56324ebe 2022-07-14 thomas
941 56324ebe 2022-07-14 thomas TAILQ_FOREACH(sym, &symhead, entry) {
942 56324ebe 2022-07-14 thomas if (strcmp(nam, sym->nam) == 0)
943 56324ebe 2022-07-14 thomas break;
944 56324ebe 2022-07-14 thomas }
945 56324ebe 2022-07-14 thomas
946 56324ebe 2022-07-14 thomas if (sym != NULL) {
947 56324ebe 2022-07-14 thomas if (sym->persist == 1)
948 56324ebe 2022-07-14 thomas return (0);
949 56324ebe 2022-07-14 thomas else {
950 56324ebe 2022-07-14 thomas free(sym->nam);
951 56324ebe 2022-07-14 thomas free(sym->val);
952 56324ebe 2022-07-14 thomas TAILQ_REMOVE(&symhead, sym, entry);
953 56324ebe 2022-07-14 thomas free(sym);
954 56324ebe 2022-07-14 thomas }
955 56324ebe 2022-07-14 thomas }
956 56324ebe 2022-07-14 thomas sym = calloc(1, sizeof(*sym));
957 56324ebe 2022-07-14 thomas if (sym == NULL)
958 56324ebe 2022-07-14 thomas return (-1);
959 56324ebe 2022-07-14 thomas
960 56324ebe 2022-07-14 thomas sym->nam = strdup(nam);
961 56324ebe 2022-07-14 thomas if (sym->nam == NULL) {
962 56324ebe 2022-07-14 thomas free(sym);
963 56324ebe 2022-07-14 thomas return (-1);
964 56324ebe 2022-07-14 thomas }
965 56324ebe 2022-07-14 thomas sym->val = strdup(val);
966 56324ebe 2022-07-14 thomas if (sym->val == NULL) {
967 56324ebe 2022-07-14 thomas free(sym->nam);
968 56324ebe 2022-07-14 thomas free(sym);
969 56324ebe 2022-07-14 thomas return (-1);
970 56324ebe 2022-07-14 thomas }
971 56324ebe 2022-07-14 thomas sym->used = 0;
972 56324ebe 2022-07-14 thomas sym->persist = persist;
973 56324ebe 2022-07-14 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
974 56324ebe 2022-07-14 thomas return (0);
975 56324ebe 2022-07-14 thomas }
976 56324ebe 2022-07-14 thomas
977 56324ebe 2022-07-14 thomas int
978 56324ebe 2022-07-14 thomas cmdline_symset(char *s)
979 56324ebe 2022-07-14 thomas {
980 56324ebe 2022-07-14 thomas char *sym, *val;
981 56324ebe 2022-07-14 thomas int ret;
982 56324ebe 2022-07-14 thomas size_t len;
983 56324ebe 2022-07-14 thomas
984 56324ebe 2022-07-14 thomas val = strrchr(s, '=');
985 56324ebe 2022-07-14 thomas if (val == NULL)
986 56324ebe 2022-07-14 thomas return (-1);
987 56324ebe 2022-07-14 thomas
988 56324ebe 2022-07-14 thomas len = strlen(s) - strlen(val) + 1;
989 56324ebe 2022-07-14 thomas sym = malloc(len);
990 56324ebe 2022-07-14 thomas if (sym == NULL)
991 56324ebe 2022-07-14 thomas fatal("%s: malloc", __func__);
992 56324ebe 2022-07-14 thomas
993 56324ebe 2022-07-14 thomas memcpy(&sym, s, len);
994 56324ebe 2022-07-14 thomas
995 56324ebe 2022-07-14 thomas ret = symset(sym, val + 1, 1);
996 56324ebe 2022-07-14 thomas free(sym);
997 56324ebe 2022-07-14 thomas
998 56324ebe 2022-07-14 thomas return (ret);
999 56324ebe 2022-07-14 thomas }
1000 56324ebe 2022-07-14 thomas
1001 56324ebe 2022-07-14 thomas char *
1002 56324ebe 2022-07-14 thomas symget(const char *nam)
1003 56324ebe 2022-07-14 thomas {
1004 56324ebe 2022-07-14 thomas struct sym *sym;
1005 56324ebe 2022-07-14 thomas
1006 56324ebe 2022-07-14 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1007 56324ebe 2022-07-14 thomas if (strcmp(nam, sym->nam) == 0) {
1008 56324ebe 2022-07-14 thomas sym->used = 1;
1009 56324ebe 2022-07-14 thomas return (sym->val);
1010 56324ebe 2022-07-14 thomas }
1011 56324ebe 2022-07-14 thomas }
1012 56324ebe 2022-07-14 thomas return (NULL);
1013 56324ebe 2022-07-14 thomas }
1014 56324ebe 2022-07-14 thomas
1015 56324ebe 2022-07-14 thomas int
1016 56324ebe 2022-07-14 thomas getservice(const char *n)
1017 56324ebe 2022-07-14 thomas {
1018 56324ebe 2022-07-14 thomas struct servent *s;
1019 56324ebe 2022-07-14 thomas const char *errstr;
1020 56324ebe 2022-07-14 thomas long long llval;
1021 56324ebe 2022-07-14 thomas
1022 56324ebe 2022-07-14 thomas llval = strtonum(n, 0, UINT16_MAX, &errstr);
1023 56324ebe 2022-07-14 thomas if (errstr) {
1024 56324ebe 2022-07-14 thomas s = getservbyname(n, "tcp");
1025 56324ebe 2022-07-14 thomas if (s == NULL)
1026 56324ebe 2022-07-14 thomas s = getservbyname(n, "udp");
1027 56324ebe 2022-07-14 thomas if (s == NULL)
1028 56324ebe 2022-07-14 thomas return (-1);
1029 56324ebe 2022-07-14 thomas return (s->s_port);
1030 56324ebe 2022-07-14 thomas }
1031 56324ebe 2022-07-14 thomas
1032 56324ebe 2022-07-14 thomas return (htons((unsigned short)llval));
1033 56324ebe 2022-07-14 thomas }
1034 56324ebe 2022-07-14 thomas
1035 56324ebe 2022-07-14 thomas struct address *
1036 56324ebe 2022-07-14 thomas host_v4(const char *s)
1037 56324ebe 2022-07-14 thomas {
1038 56324ebe 2022-07-14 thomas struct in_addr ina;
1039 56324ebe 2022-07-14 thomas struct sockaddr_in *sain;
1040 56324ebe 2022-07-14 thomas struct address *h;
1041 56324ebe 2022-07-14 thomas
1042 56324ebe 2022-07-14 thomas memset(&ina, 0, sizeof(ina));
1043 56324ebe 2022-07-14 thomas if (inet_pton(AF_INET, s, &ina) != 1)
1044 56324ebe 2022-07-14 thomas return (NULL);
1045 56324ebe 2022-07-14 thomas
1046 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1047 56324ebe 2022-07-14 thomas fatal(__func__);
1048 56324ebe 2022-07-14 thomas sain = (struct sockaddr_in *)&h->ss;
1049 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1050 d7cad54e 2022-07-14 thomas #ifndef __linux__
1051 56324ebe 2022-07-14 thomas sain->sin_len = sizeof(struct sockaddr_in);
1052 d7cad54e 2022-07-14 thomas #endif
1053 56324ebe 2022-07-14 thomas sain->sin_family = AF_INET;
1054 56324ebe 2022-07-14 thomas sain->sin_addr.s_addr = ina.s_addr;
1055 56324ebe 2022-07-14 thomas if (sain->sin_addr.s_addr == INADDR_ANY)
1056 56324ebe 2022-07-14 thomas h->prefixlen = 0; /* 0.0.0.0 address */
1057 56324ebe 2022-07-14 thomas else
1058 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1059 56324ebe 2022-07-14 thomas return (h);
1060 56324ebe 2022-07-14 thomas }
1061 56324ebe 2022-07-14 thomas
1062 56324ebe 2022-07-14 thomas struct address *
1063 56324ebe 2022-07-14 thomas host_v6(const char *s)
1064 56324ebe 2022-07-14 thomas {
1065 56324ebe 2022-07-14 thomas struct addrinfo hints, *res;
1066 56324ebe 2022-07-14 thomas struct sockaddr_in6 *sa_in6;
1067 56324ebe 2022-07-14 thomas struct address *h = NULL;
1068 56324ebe 2022-07-14 thomas
1069 56324ebe 2022-07-14 thomas memset(&hints, 0, sizeof(hints));
1070 56324ebe 2022-07-14 thomas hints.ai_family = AF_INET6;
1071 56324ebe 2022-07-14 thomas hints.ai_socktype = SOCK_DGRAM; /* dummy */
1072 56324ebe 2022-07-14 thomas hints.ai_flags = AI_NUMERICHOST;
1073 56324ebe 2022-07-14 thomas if (getaddrinfo(s, "0", &hints, &res) == 0) {
1074 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1075 56324ebe 2022-07-14 thomas fatal(__func__);
1076 56324ebe 2022-07-14 thomas sa_in6 = (struct sockaddr_in6 *)&h->ss;
1077 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1078 d7cad54e 2022-07-14 thomas #ifndef __linux__
1079 56324ebe 2022-07-14 thomas sa_in6->sin6_len = sizeof(struct sockaddr_in6);
1080 d7cad54e 2022-07-14 thomas #endif
1081 56324ebe 2022-07-14 thomas sa_in6->sin6_family = AF_INET6;
1082 56324ebe 2022-07-14 thomas memcpy(&sa_in6->sin6_addr,
1083 56324ebe 2022-07-14 thomas &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1084 56324ebe 2022-07-14 thomas sizeof(sa_in6->sin6_addr));
1085 56324ebe 2022-07-14 thomas sa_in6->sin6_scope_id =
1086 56324ebe 2022-07-14 thomas ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
1087 56324ebe 2022-07-14 thomas if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1088 56324ebe 2022-07-14 thomas sizeof(sa_in6->sin6_addr)) == 0)
1089 56324ebe 2022-07-14 thomas h->prefixlen = 0; /* any address */
1090 56324ebe 2022-07-14 thomas else
1091 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1092 56324ebe 2022-07-14 thomas freeaddrinfo(res);
1093 56324ebe 2022-07-14 thomas }
1094 56324ebe 2022-07-14 thomas
1095 56324ebe 2022-07-14 thomas return (h);
1096 56324ebe 2022-07-14 thomas }
1097 56324ebe 2022-07-14 thomas
1098 56324ebe 2022-07-14 thomas int
1099 56324ebe 2022-07-14 thomas host_dns(const char *s, struct addresslist *al, int max,
1100 56324ebe 2022-07-14 thomas in_port_t port, const char *ifname, int ipproto)
1101 56324ebe 2022-07-14 thomas {
1102 56324ebe 2022-07-14 thomas struct addrinfo hints, *res0, *res;
1103 56324ebe 2022-07-14 thomas int error, cnt = 0;
1104 56324ebe 2022-07-14 thomas struct sockaddr_in *sain;
1105 56324ebe 2022-07-14 thomas struct sockaddr_in6 *sin6;
1106 56324ebe 2022-07-14 thomas struct address *h;
1107 56324ebe 2022-07-14 thomas
1108 56324ebe 2022-07-14 thomas if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0)
1109 56324ebe 2022-07-14 thomas return (cnt);
1110 56324ebe 2022-07-14 thomas
1111 56324ebe 2022-07-14 thomas memset(&hints, 0, sizeof(hints));
1112 56324ebe 2022-07-14 thomas hints.ai_family = PF_UNSPEC;
1113 56324ebe 2022-07-14 thomas hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1114 56324ebe 2022-07-14 thomas hints.ai_flags = AI_ADDRCONFIG;
1115 56324ebe 2022-07-14 thomas error = getaddrinfo(s, NULL, &hints, &res0);
1116 56324ebe 2022-07-14 thomas if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1117 56324ebe 2022-07-14 thomas return (0);
1118 56324ebe 2022-07-14 thomas if (error) {
1119 56324ebe 2022-07-14 thomas log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1120 56324ebe 2022-07-14 thomas gai_strerror(error));
1121 56324ebe 2022-07-14 thomas return (-1);
1122 56324ebe 2022-07-14 thomas }
1123 56324ebe 2022-07-14 thomas
1124 56324ebe 2022-07-14 thomas for (res = res0; res && cnt < max; res = res->ai_next) {
1125 56324ebe 2022-07-14 thomas if (res->ai_family != AF_INET &&
1126 56324ebe 2022-07-14 thomas res->ai_family != AF_INET6)
1127 56324ebe 2022-07-14 thomas continue;
1128 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1129 56324ebe 2022-07-14 thomas fatal(__func__);
1130 56324ebe 2022-07-14 thomas
1131 56324ebe 2022-07-14 thomas if (port)
1132 56324ebe 2022-07-14 thomas h->port = port;
1133 56324ebe 2022-07-14 thomas if (ifname != NULL) {
1134 56324ebe 2022-07-14 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1135 56324ebe 2022-07-14 thomas sizeof(h->ifname)) {
1136 56324ebe 2022-07-14 thomas log_warnx("%s: interface name truncated",
1137 56324ebe 2022-07-14 thomas __func__);
1138 56324ebe 2022-07-14 thomas freeaddrinfo(res0);
1139 56324ebe 2022-07-14 thomas free(h);
1140 56324ebe 2022-07-14 thomas return (-1);
1141 56324ebe 2022-07-14 thomas }
1142 56324ebe 2022-07-14 thomas }
1143 56324ebe 2022-07-14 thomas if (ipproto != -1)
1144 56324ebe 2022-07-14 thomas h->ipproto = ipproto;
1145 56324ebe 2022-07-14 thomas h->ss.ss_family = res->ai_family;
1146 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1147 56324ebe 2022-07-14 thomas
1148 56324ebe 2022-07-14 thomas if (res->ai_family == AF_INET) {
1149 56324ebe 2022-07-14 thomas sain = (struct sockaddr_in *)&h->ss;
1150 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1151 d7cad54e 2022-07-14 thomas #ifndef __linux__
1152 56324ebe 2022-07-14 thomas sain->sin_len = sizeof(struct sockaddr_in);
1153 d7cad54e 2022-07-14 thomas #endif
1154 56324ebe 2022-07-14 thomas sain->sin_addr.s_addr = ((struct sockaddr_in *)
1155 56324ebe 2022-07-14 thomas res->ai_addr)->sin_addr.s_addr;
1156 56324ebe 2022-07-14 thomas } else {
1157 56324ebe 2022-07-14 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1158 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1159 d7cad54e 2022-07-14 thomas #ifndef __linux__
1160 56324ebe 2022-07-14 thomas sin6->sin6_len = sizeof(struct sockaddr_in6);
1161 d7cad54e 2022-07-14 thomas #endif
1162 56324ebe 2022-07-14 thomas memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
1163 56324ebe 2022-07-14 thomas res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
1164 56324ebe 2022-07-14 thomas }
1165 56324ebe 2022-07-14 thomas
1166 56324ebe 2022-07-14 thomas TAILQ_INSERT_HEAD(al, h, entry);
1167 56324ebe 2022-07-14 thomas cnt++;
1168 56324ebe 2022-07-14 thomas }
1169 56324ebe 2022-07-14 thomas if (cnt == max && res) {
1170 56324ebe 2022-07-14 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1171 56324ebe 2022-07-14 thomas s, max);
1172 56324ebe 2022-07-14 thomas }
1173 56324ebe 2022-07-14 thomas freeaddrinfo(res0);
1174 56324ebe 2022-07-14 thomas return (cnt);
1175 56324ebe 2022-07-14 thomas }
1176 56324ebe 2022-07-14 thomas
1177 56324ebe 2022-07-14 thomas int
1178 56324ebe 2022-07-14 thomas host_if(const char *s, struct addresslist *al, int max,
1179 56324ebe 2022-07-14 thomas in_port_t port, const char *ifname, int ipproto)
1180 56324ebe 2022-07-14 thomas {
1181 56324ebe 2022-07-14 thomas struct ifaddrs *ifap, *p;
1182 56324ebe 2022-07-14 thomas struct sockaddr_in *sain;
1183 56324ebe 2022-07-14 thomas struct sockaddr_in6 *sin6;
1184 56324ebe 2022-07-14 thomas struct address *h;
1185 56324ebe 2022-07-14 thomas int cnt = 0, af;
1186 56324ebe 2022-07-14 thomas
1187 56324ebe 2022-07-14 thomas if (getifaddrs(&ifap) == -1)
1188 56324ebe 2022-07-14 thomas fatal("getifaddrs");
1189 56324ebe 2022-07-14 thomas
1190 56324ebe 2022-07-14 thomas /* First search for IPv4 addresses */
1191 56324ebe 2022-07-14 thomas af = AF_INET;
1192 56324ebe 2022-07-14 thomas
1193 56324ebe 2022-07-14 thomas nextaf:
1194 56324ebe 2022-07-14 thomas for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1195 56324ebe 2022-07-14 thomas if (p->ifa_addr == NULL ||
1196 56324ebe 2022-07-14 thomas p->ifa_addr->sa_family != af ||
1197 56324ebe 2022-07-14 thomas (strcmp(s, p->ifa_name) != 0 &&
1198 56324ebe 2022-07-14 thomas !is_if_in_group(p->ifa_name, s)))
1199 56324ebe 2022-07-14 thomas continue;
1200 56324ebe 2022-07-14 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1201 56324ebe 2022-07-14 thomas fatal("calloc");
1202 56324ebe 2022-07-14 thomas
1203 56324ebe 2022-07-14 thomas if (port)
1204 56324ebe 2022-07-14 thomas h->port = port;
1205 56324ebe 2022-07-14 thomas if (ifname != NULL) {
1206 56324ebe 2022-07-14 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1207 56324ebe 2022-07-14 thomas sizeof(h->ifname)) {
1208 56324ebe 2022-07-14 thomas log_warnx("%s: interface name truncated",
1209 56324ebe 2022-07-14 thomas __func__);
1210 56324ebe 2022-07-14 thomas free(h);
1211 56324ebe 2022-07-14 thomas freeifaddrs(ifap);
1212 56324ebe 2022-07-14 thomas return (-1);
1213 56324ebe 2022-07-14 thomas }
1214 56324ebe 2022-07-14 thomas }
1215 56324ebe 2022-07-14 thomas if (ipproto != -1)
1216 56324ebe 2022-07-14 thomas h->ipproto = ipproto;
1217 56324ebe 2022-07-14 thomas h->ss.ss_family = af;
1218 56324ebe 2022-07-14 thomas h->prefixlen = -1; /* host address */
1219 56324ebe 2022-07-14 thomas
1220 56324ebe 2022-07-14 thomas if (af == AF_INET) {
1221 56324ebe 2022-07-14 thomas sain = (struct sockaddr_in *)&h->ss;
1222 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1223 d7cad54e 2022-07-14 thomas #ifndef __linux__
1224 56324ebe 2022-07-14 thomas sain->sin_len = sizeof(struct sockaddr_in);
1225 d7cad54e 2022-07-14 thomas #endif
1226 56324ebe 2022-07-14 thomas sain->sin_addr.s_addr = ((struct sockaddr_in *)
1227 56324ebe 2022-07-14 thomas p->ifa_addr)->sin_addr.s_addr;
1228 56324ebe 2022-07-14 thomas } else {
1229 56324ebe 2022-07-14 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1230 d7cad54e 2022-07-14 thomas /* TA: Iffy... */
1231 d7cad54e 2022-07-14 thomas #ifndef __linux__
1232 56324ebe 2022-07-14 thomas sin6->sin6_len = sizeof(struct sockaddr_in6);
1233 d7cad54e 2022-07-14 thomas #endif
1234 56324ebe 2022-07-14 thomas memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
1235 56324ebe 2022-07-14 thomas p->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
1236 56324ebe 2022-07-14 thomas sin6->sin6_scope_id = ((struct sockaddr_in6 *)
1237 56324ebe 2022-07-14 thomas p->ifa_addr)->sin6_scope_id;
1238 56324ebe 2022-07-14 thomas }
1239 56324ebe 2022-07-14 thomas
1240 56324ebe 2022-07-14 thomas TAILQ_INSERT_HEAD(al, h, entry);
1241 56324ebe 2022-07-14 thomas cnt++;
1242 56324ebe 2022-07-14 thomas }
1243 56324ebe 2022-07-14 thomas if (af == AF_INET) {
1244 56324ebe 2022-07-14 thomas /* Next search for IPv6 addresses */
1245 56324ebe 2022-07-14 thomas af = AF_INET6;
1246 56324ebe 2022-07-14 thomas goto nextaf;
1247 56324ebe 2022-07-14 thomas }
1248 56324ebe 2022-07-14 thomas
1249 56324ebe 2022-07-14 thomas if (cnt > max) {
1250 56324ebe 2022-07-14 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1251 56324ebe 2022-07-14 thomas s, max);
1252 56324ebe 2022-07-14 thomas }
1253 56324ebe 2022-07-14 thomas freeifaddrs(ifap);
1254 56324ebe 2022-07-14 thomas return (cnt);
1255 56324ebe 2022-07-14 thomas }
1256 56324ebe 2022-07-14 thomas
1257 56324ebe 2022-07-14 thomas int
1258 56324ebe 2022-07-14 thomas host(const char *s, struct addresslist *al, int max,
1259 56324ebe 2022-07-14 thomas in_port_t port, const char *ifname, int ipproto)
1260 56324ebe 2022-07-14 thomas {
1261 56324ebe 2022-07-14 thomas struct address *h;
1262 56324ebe 2022-07-14 thomas
1263 56324ebe 2022-07-14 thomas h = host_v4(s);
1264 56324ebe 2022-07-14 thomas
1265 56324ebe 2022-07-14 thomas /* IPv6 address? */
1266 56324ebe 2022-07-14 thomas if (h == NULL)
1267 56324ebe 2022-07-14 thomas h = host_v6(s);
1268 56324ebe 2022-07-14 thomas
1269 56324ebe 2022-07-14 thomas if (h != NULL) {
1270 56324ebe 2022-07-14 thomas if (port)
1271 56324ebe 2022-07-14 thomas h->port = port;
1272 56324ebe 2022-07-14 thomas if (ifname != NULL) {
1273 56324ebe 2022-07-14 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1274 56324ebe 2022-07-14 thomas sizeof(h->ifname)) {
1275 56324ebe 2022-07-14 thomas log_warnx("%s: interface name truncated",
1276 56324ebe 2022-07-14 thomas __func__);
1277 56324ebe 2022-07-14 thomas free(h);
1278 56324ebe 2022-07-14 thomas return (-1);
1279 56324ebe 2022-07-14 thomas }
1280 56324ebe 2022-07-14 thomas }
1281 56324ebe 2022-07-14 thomas if (ipproto != -1)
1282 56324ebe 2022-07-14 thomas h->ipproto = ipproto;
1283 56324ebe 2022-07-14 thomas
1284 56324ebe 2022-07-14 thomas TAILQ_INSERT_HEAD(al, h, entry);
1285 56324ebe 2022-07-14 thomas return (1);
1286 56324ebe 2022-07-14 thomas }
1287 56324ebe 2022-07-14 thomas
1288 56324ebe 2022-07-14 thomas return (host_dns(s, al, max, port, ifname, ipproto));
1289 56324ebe 2022-07-14 thomas }
1290 56324ebe 2022-07-14 thomas
1291 56324ebe 2022-07-14 thomas int
1292 56324ebe 2022-07-14 thomas is_if_in_group(const char *ifname, const char *groupname)
1293 56324ebe 2022-07-14 thomas {
1294 d7cad54e 2022-07-14 thomas /* TA: Check this... */
1295 d7cad54e 2022-07-14 thomas #ifdef HAVE_STRUCT_IFGROUPREQ
1296 56324ebe 2022-07-14 thomas unsigned int len;
1297 56324ebe 2022-07-14 thomas struct ifgroupreq ifgr;
1298 56324ebe 2022-07-14 thomas struct ifg_req *ifg;
1299 56324ebe 2022-07-14 thomas int s;
1300 56324ebe 2022-07-14 thomas int ret = 0;
1301 56324ebe 2022-07-14 thomas
1302 56324ebe 2022-07-14 thomas if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1303 56324ebe 2022-07-14 thomas err(1, "socket");
1304 56324ebe 2022-07-14 thomas
1305 56324ebe 2022-07-14 thomas memset(&ifgr, 0, sizeof(ifgr));
1306 56324ebe 2022-07-14 thomas if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1307 56324ebe 2022-07-14 thomas err(1, "IFNAMSIZ");
1308 56324ebe 2022-07-14 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1309 56324ebe 2022-07-14 thomas if (errno == EINVAL || errno == ENOTTY)
1310 56324ebe 2022-07-14 thomas goto end;
1311 56324ebe 2022-07-14 thomas err(1, "SIOCGIFGROUP");
1312 56324ebe 2022-07-14 thomas }
1313 56324ebe 2022-07-14 thomas
1314 56324ebe 2022-07-14 thomas len = ifgr.ifgr_len;
1315 56324ebe 2022-07-14 thomas ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1316 56324ebe 2022-07-14 thomas sizeof(struct ifg_req));
1317 56324ebe 2022-07-14 thomas if (ifgr.ifgr_groups == NULL)
1318 56324ebe 2022-07-14 thomas err(1, "getifgroups");
1319 56324ebe 2022-07-14 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1320 56324ebe 2022-07-14 thomas err(1, "SIOCGIFGROUP");
1321 56324ebe 2022-07-14 thomas
1322 56324ebe 2022-07-14 thomas ifg = ifgr.ifgr_groups;
1323 56324ebe 2022-07-14 thomas for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1324 56324ebe 2022-07-14 thomas len -= sizeof(struct ifg_req);
1325 56324ebe 2022-07-14 thomas if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1326 56324ebe 2022-07-14 thomas ret = 1;
1327 56324ebe 2022-07-14 thomas break;
1328 56324ebe 2022-07-14 thomas }
1329 56324ebe 2022-07-14 thomas }
1330 56324ebe 2022-07-14 thomas free(ifgr.ifgr_groups);
1331 56324ebe 2022-07-14 thomas
1332 56324ebe 2022-07-14 thomas end:
1333 56324ebe 2022-07-14 thomas close(s);
1334 56324ebe 2022-07-14 thomas return (ret);
1335 d7cad54e 2022-07-14 thomas #else
1336 d7cad54e 2022-07-14 thomas return (0);
1337 d7cad54e 2022-07-14 thomas #endif
1338 56324ebe 2022-07-14 thomas }
1339 56324ebe 2022-07-14 thomas
1340 56324ebe 2022-07-14 thomas int
1341 56324ebe 2022-07-14 thomas get_addrs(const char *addr, struct addresslist *al, in_port_t port)
1342 56324ebe 2022-07-14 thomas {
1343 56324ebe 2022-07-14 thomas if (strcmp("", addr) == 0) {
1344 56324ebe 2022-07-14 thomas if (host("0.0.0.0", al, 1, port, "0.0.0.0", -1) <= 0) {
1345 56324ebe 2022-07-14 thomas yyerror("invalid listen ip: %s",
1346 56324ebe 2022-07-14 thomas "0.0.0.0");
1347 56324ebe 2022-07-14 thomas return (-1);
1348 56324ebe 2022-07-14 thomas }
1349 56324ebe 2022-07-14 thomas if (host("::", al, 1, port, "::", -1) <= 0) {
1350 56324ebe 2022-07-14 thomas yyerror("invalid listen ip: %s", "::");
1351 56324ebe 2022-07-14 thomas return (-1);
1352 56324ebe 2022-07-14 thomas }
1353 56324ebe 2022-07-14 thomas } else {
1354 56324ebe 2022-07-14 thomas if (host(addr, al, GOTWEBD_MAXIFACE, port, addr,
1355 56324ebe 2022-07-14 thomas -1) <= 0) {
1356 56324ebe 2022-07-14 thomas yyerror("invalid listen ip: %s", addr);
1357 56324ebe 2022-07-14 thomas return (-1);
1358 56324ebe 2022-07-14 thomas }
1359 56324ebe 2022-07-14 thomas }
1360 56324ebe 2022-07-14 thomas return (0);
1361 56324ebe 2022-07-14 thomas }