2 a596b957 2022-07-14 tracey * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 a596b957 2022-07-14 tracey * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 a596b957 2022-07-14 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 a596b957 2022-07-14 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
11 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
12 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
14 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 a596b957 2022-07-14 tracey #include <sys/ioctl.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
26 a596b957 2022-07-14 tracey #include <sys/queue.h>
27 a596b957 2022-07-14 tracey #include <sys/socket.h>
28 a596b957 2022-07-14 tracey #include <sys/stat.h>
30 a596b957 2022-07-14 tracey #include <net/if.h>
31 a596b957 2022-07-14 tracey #include <netinet/in.h>
33 a596b957 2022-07-14 tracey #include <arpa/inet.h>
35 a596b957 2022-07-14 tracey #include <ctype.h>
36 a596b957 2022-07-14 tracey #include <err.h>
37 a596b957 2022-07-14 tracey #include <errno.h>
38 a596b957 2022-07-14 tracey #include <event.h>
39 a596b957 2022-07-14 tracey #include <ifaddrs.h>
40 a596b957 2022-07-14 tracey #include <imsg.h>
41 a596b957 2022-07-14 tracey #include <limits.h>
42 a596b957 2022-07-14 tracey #include <netdb.h>
43 a596b957 2022-07-14 tracey #include <stdarg.h>
44 a596b957 2022-07-14 tracey #include <stdlib.h>
45 a596b957 2022-07-14 tracey #include <stdio.h>
46 a596b957 2022-07-14 tracey #include <string.h>
47 a596b957 2022-07-14 tracey #include <syslog.h>
48 a596b957 2022-07-14 tracey #include <unistd.h>
50 a596b957 2022-07-14 tracey #include "proc.h"
51 a596b957 2022-07-14 tracey #include "gotwebd.h"
52 86b4b772 2022-07-29 stsp #include "got_sockaddr.h"
54 a596b957 2022-07-14 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 a596b957 2022-07-14 tracey static struct file {
56 a596b957 2022-07-14 tracey TAILQ_ENTRY(file) entry;
57 a596b957 2022-07-14 tracey FILE *stream;
58 a596b957 2022-07-14 tracey char *name;
59 a596b957 2022-07-14 tracey int lineno;
60 a596b957 2022-07-14 tracey int errors;
62 a596b957 2022-07-14 tracey struct file *newfile(const char *, int);
63 a596b957 2022-07-14 tracey static void closefile(struct file *);
64 a596b957 2022-07-14 tracey int check_file_secrecy(int, const char *);
65 a596b957 2022-07-14 tracey int yyparse(void);
66 a596b957 2022-07-14 tracey int yylex(void);
67 a596b957 2022-07-14 tracey int yyerror(const char *, ...)
68 a596b957 2022-07-14 tracey __attribute__((__format__ (printf, 1, 2)))
69 a596b957 2022-07-14 tracey __attribute__((__nonnull__ (1)));
70 a596b957 2022-07-14 tracey int kw_cmp(const void *, const void *);
71 a596b957 2022-07-14 tracey int lookup(char *);
72 a596b957 2022-07-14 tracey int lgetc(int);
73 a596b957 2022-07-14 tracey int lungetc(int);
74 a596b957 2022-07-14 tracey int findeol(void);
76 a596b957 2022-07-14 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 a596b957 2022-07-14 tracey struct sym {
78 a596b957 2022-07-14 tracey TAILQ_ENTRY(sym) entry;
80 a596b957 2022-07-14 tracey int persist;
81 a596b957 2022-07-14 tracey char *nam;
82 a596b957 2022-07-14 tracey char *val;
85 a596b957 2022-07-14 tracey int symset(const char *, const char *, int);
86 a596b957 2022-07-14 tracey char *symget(const char *);
88 a596b957 2022-07-14 tracey static int errors;
90 a596b957 2022-07-14 tracey static struct gotwebd *gotwebd;
91 a596b957 2022-07-14 tracey static struct server *new_srv;
92 a596b957 2022-07-14 tracey static struct server *conf_new_server(const char *);
93 a596b957 2022-07-14 tracey int getservice(const char *);
96 a596b957 2022-07-14 tracey int get_addrs(const char *, struct addresslist *, in_port_t);
97 a596b957 2022-07-14 tracey struct address *host_v4(const char *);
98 a596b957 2022-07-14 tracey struct address *host_v6(const char *);
99 a596b957 2022-07-14 tracey int host_dns(const char *, struct addresslist *,
100 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
101 a596b957 2022-07-14 tracey int host_if(const char *, struct addresslist *,
102 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
103 a596b957 2022-07-14 tracey int host(const char *, struct addresslist *,
104 a596b957 2022-07-14 tracey int, in_port_t, const char *, int);
105 a596b957 2022-07-14 tracey int is_if_in_group(const char *, const char *);
107 a596b957 2022-07-14 tracey typedef struct {
109 a596b957 2022-07-14 tracey long long number;
110 a596b957 2022-07-14 tracey char *string;
111 a596b957 2022-07-14 tracey in_port_t port;
113 a596b957 2022-07-14 tracey int lineno;
114 a596b957 2022-07-14 tracey } YYSTYPE;
118 d8473d93 2022-08-11 stsp %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
119 a596b957 2022-07-14 tracey %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
120 a596b957 2022-07-14 tracey %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
121 a596b957 2022-07-14 tracey %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK FCGI_SOCKET
122 a596b957 2022-07-14 tracey %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
124 a596b957 2022-07-14 tracey %token <v.string> STRING
125 a596b957 2022-07-14 tracey %type <v.port> fcgiport
126 a596b957 2022-07-14 tracey %token <v.number> NUMBER
127 a596b957 2022-07-14 tracey %type <v.number> boolean
131 a596b957 2022-07-14 tracey grammar :
132 a596b957 2022-07-14 tracey | grammar '\n'
133 a596b957 2022-07-14 tracey | grammar main '\n'
134 a596b957 2022-07-14 tracey | grammar server '\n'
137 a596b957 2022-07-14 tracey boolean : STRING {
138 a596b957 2022-07-14 tracey if (strcasecmp($1, "1") == 0 ||
139 a596b957 2022-07-14 tracey strcasecmp($1, "yes") == 0 ||
140 a596b957 2022-07-14 tracey strcasecmp($1, "on") == 0)
142 a596b957 2022-07-14 tracey else if (strcasecmp($1, "0") == 0 ||
143 a596b957 2022-07-14 tracey strcasecmp($1, "off") == 0 ||
144 a596b957 2022-07-14 tracey strcasecmp($1, "no") == 0)
147 a596b957 2022-07-14 tracey yyerror("invalid boolean value '%s'", $1);
148 a596b957 2022-07-14 tracey free($1);
151 a596b957 2022-07-14 tracey free($1);
153 a596b957 2022-07-14 tracey | ON { $$ = 1; }
154 a596b957 2022-07-14 tracey | NUMBER { $$ = $1; }
157 a596b957 2022-07-14 tracey fcgiport : NUMBER {
158 a596b957 2022-07-14 tracey if ($1 <= 0 || $1 > (int)USHRT_MAX) {
159 a596b957 2022-07-14 tracey yyerror("invalid port: %lld", $1);
162 a596b957 2022-07-14 tracey $$ = htons($1);
164 a596b957 2022-07-14 tracey | STRING {
167 a596b957 2022-07-14 tracey if ((val = getservice($1)) == -1) {
168 a596b957 2022-07-14 tracey yyerror("invalid port: %s", $1);
169 a596b957 2022-07-14 tracey free($1);
172 a596b957 2022-07-14 tracey free($1);
174 a596b957 2022-07-14 tracey $$ = val;
178 a596b957 2022-07-14 tracey main : PREFORK NUMBER {
179 a596b957 2022-07-14 tracey gotwebd->prefork_gotwebd = $2;
181 a596b957 2022-07-14 tracey | CHROOT STRING {
182 a596b957 2022-07-14 tracey n = strlcpy(gotwebd->httpd_chroot, $2,
183 a596b957 2022-07-14 tracey sizeof(gotwebd->httpd_chroot));
184 a596b957 2022-07-14 tracey if (n >= sizeof(gotwebd->httpd_chroot)) {
185 a596b957 2022-07-14 tracey yyerror("%s: httpd_chroot truncated", __func__);
186 a596b957 2022-07-14 tracey free($2);
189 a596b957 2022-07-14 tracey free($2);
191 a596b957 2022-07-14 tracey | FCGI_SOCKET boolean {
192 a596b957 2022-07-14 tracey gotwebd->fcgi_socket = $2;
194 a596b957 2022-07-14 tracey | FCGI_SOCKET boolean {
195 a596b957 2022-07-14 tracey gotwebd->fcgi_socket = $2;
196 a596b957 2022-07-14 tracey } '{' optnl socketopts4 '}'
197 a596b957 2022-07-14 tracey | UNIX_SOCKET boolean {
198 a596b957 2022-07-14 tracey gotwebd->unix_socket = $2;
200 a596b957 2022-07-14 tracey | UNIX_SOCKET_NAME STRING {
201 a596b957 2022-07-14 tracey n = snprintf(gotwebd->unix_socket_name,
202 a596b957 2022-07-14 tracey sizeof(gotwebd->unix_socket_name), "%s%s",
203 a596b957 2022-07-14 tracey strlen(gotwebd->httpd_chroot) ?
204 a596b957 2022-07-14 tracey gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
206 438d0cc3 2022-08-16 op (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
207 a596b957 2022-07-14 tracey yyerror("%s: unix_socket_name truncated",
208 a596b957 2022-07-14 tracey __func__);
209 a596b957 2022-07-14 tracey free($2);
212 a596b957 2022-07-14 tracey free($2);
216 a596b957 2022-07-14 tracey server : SERVER STRING {
217 a596b957 2022-07-14 tracey struct server *srv;
219 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
220 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
221 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
222 a596b957 2022-07-14 tracey free($2);
227 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
228 a596b957 2022-07-14 tracey if (new_srv->fcgi_socket)
229 a596b957 2022-07-14 tracey if (get_addrs(new_srv->fcgi_socket_bind,
230 e087e1f6 2022-08-16 stsp &new_srv->al,
231 a596b957 2022-07-14 tracey new_srv->fcgi_socket_port) == -1) {
232 a596b957 2022-07-14 tracey yyerror("could not get tcp iface "
233 a596b957 2022-07-14 tracey "addrs");
236 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
237 a596b957 2022-07-14 tracey free($2);
239 a596b957 2022-07-14 tracey | SERVER STRING {
240 a596b957 2022-07-14 tracey struct server *srv;
242 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
243 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
244 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
245 a596b957 2022-07-14 tracey free($2);
250 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
251 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
252 a596b957 2022-07-14 tracey free($2);
253 a596b957 2022-07-14 tracey } '{' optnl serveropts2 '}' {
254 a596b957 2022-07-14 tracey if (get_addrs(new_srv->fcgi_socket_bind,
255 e087e1f6 2022-08-16 stsp &new_srv->al, new_srv->fcgi_socket_port) == -1) {
256 a596b957 2022-07-14 tracey yyerror("could not get tcp iface addrs");
262 a596b957 2022-07-14 tracey serveropts1 : REPOS_PATH STRING {
263 a596b957 2022-07-14 tracey n = strlcpy(new_srv->repos_path, $2,
264 a596b957 2022-07-14 tracey sizeof(new_srv->repos_path));
265 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->repos_path)) {
266 a596b957 2022-07-14 tracey yyerror("%s: repos_path truncated", __func__);
267 a596b957 2022-07-14 tracey free($2);
270 a596b957 2022-07-14 tracey free($2);
272 a596b957 2022-07-14 tracey | SITE_NAME STRING {
273 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_name, $2,
274 a596b957 2022-07-14 tracey sizeof(new_srv->site_name));
275 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_name)) {
276 a596b957 2022-07-14 tracey yyerror("%s: site_name truncated", __func__);
277 a596b957 2022-07-14 tracey free($2);
280 a596b957 2022-07-14 tracey free($2);
282 a596b957 2022-07-14 tracey | SITE_OWNER STRING {
283 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_owner, $2,
284 a596b957 2022-07-14 tracey sizeof(new_srv->site_owner));
285 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_owner)) {
286 a596b957 2022-07-14 tracey yyerror("%s: site_owner truncated", __func__);
287 a596b957 2022-07-14 tracey free($2);
290 a596b957 2022-07-14 tracey free($2);
292 a596b957 2022-07-14 tracey | SITE_LINK STRING {
293 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_link, $2,
294 a596b957 2022-07-14 tracey sizeof(new_srv->site_link));
295 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_link)) {
296 a596b957 2022-07-14 tracey yyerror("%s: site_link truncated", __func__);
297 a596b957 2022-07-14 tracey free($2);
300 a596b957 2022-07-14 tracey free($2);
302 a596b957 2022-07-14 tracey | LOGO STRING {
303 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
304 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo)) {
305 a596b957 2022-07-14 tracey yyerror("%s: logo truncated", __func__);
306 a596b957 2022-07-14 tracey free($2);
309 a596b957 2022-07-14 tracey free($2);
311 a596b957 2022-07-14 tracey | LOGO_URL STRING {
312 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo_url, $2,
313 a596b957 2022-07-14 tracey sizeof(new_srv->logo_url));
314 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo_url)) {
315 a596b957 2022-07-14 tracey yyerror("%s: logo_url truncated", __func__);
316 a596b957 2022-07-14 tracey free($2);
319 a596b957 2022-07-14 tracey free($2);
321 a596b957 2022-07-14 tracey | CUSTOM_CSS STRING {
322 a596b957 2022-07-14 tracey n = strlcpy(new_srv->custom_css, $2,
323 a596b957 2022-07-14 tracey sizeof(new_srv->custom_css));
324 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->custom_css)) {
325 a596b957 2022-07-14 tracey yyerror("%s: custom_css truncated", __func__);
326 a596b957 2022-07-14 tracey free($2);
329 a596b957 2022-07-14 tracey free($2);
331 a596b957 2022-07-14 tracey | MAX_REPOS NUMBER {
332 a596b957 2022-07-14 tracey if ($2 > 0)
333 a596b957 2022-07-14 tracey new_srv->max_repos = $2;
335 a596b957 2022-07-14 tracey | SHOW_SITE_OWNER boolean {
336 a596b957 2022-07-14 tracey new_srv->show_site_owner = $2;
338 a596b957 2022-07-14 tracey | SHOW_REPO_OWNER boolean {
339 a596b957 2022-07-14 tracey new_srv->show_repo_owner = $2;
341 a596b957 2022-07-14 tracey | SHOW_REPO_AGE boolean {
342 a596b957 2022-07-14 tracey new_srv->show_repo_age = $2;
344 a596b957 2022-07-14 tracey | SHOW_REPO_DESCRIPTION boolean {
345 a596b957 2022-07-14 tracey new_srv->show_repo_description = $2;
347 a596b957 2022-07-14 tracey | SHOW_REPO_CLONEURL boolean {
348 a596b957 2022-07-14 tracey new_srv->show_repo_cloneurl = $2;
350 a596b957 2022-07-14 tracey | MAX_REPOS_DISPLAY NUMBER {
351 a596b957 2022-07-14 tracey new_srv->max_repos_display = $2;
353 a596b957 2022-07-14 tracey | MAX_COMMITS_DISPLAY NUMBER {
354 a596b957 2022-07-14 tracey if ($2 > 0)
355 a596b957 2022-07-14 tracey new_srv->max_commits_display = $2;
357 a596b957 2022-07-14 tracey | FCGI_SOCKET boolean {
358 a596b957 2022-07-14 tracey new_srv->fcgi_socket = $2;
360 a596b957 2022-07-14 tracey | FCGI_SOCKET boolean {
361 a596b957 2022-07-14 tracey new_srv->fcgi_socket = $2;
362 a596b957 2022-07-14 tracey } '{' optnl socketopts2 '}'
363 a596b957 2022-07-14 tracey | UNIX_SOCKET boolean {
364 a596b957 2022-07-14 tracey new_srv->unix_socket = $2;
366 a596b957 2022-07-14 tracey | UNIX_SOCKET_NAME STRING {
367 a596b957 2022-07-14 tracey n = snprintf(new_srv->unix_socket_name,
368 a596b957 2022-07-14 tracey sizeof(new_srv->unix_socket_name), "%s%s",
369 a596b957 2022-07-14 tracey strlen(gotwebd->httpd_chroot) ?
370 a596b957 2022-07-14 tracey gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
372 438d0cc3 2022-08-16 op (size_t)n >= sizeof(new_srv->unix_socket_name)) {
373 a596b957 2022-07-14 tracey yyerror("%s: unix_socket_name truncated",
374 a596b957 2022-07-14 tracey __func__);
375 a596b957 2022-07-14 tracey free($2);
378 a596b957 2022-07-14 tracey free($2);
382 a596b957 2022-07-14 tracey serveropts2 : serveropts2 serveropts1 nl
383 a596b957 2022-07-14 tracey | serveropts1 optnl
386 d8473d93 2022-08-11 stsp socketopts1 : LISTEN ON STRING {
387 a596b957 2022-07-14 tracey n = strlcpy(new_srv->fcgi_socket_bind, $3,
388 a596b957 2022-07-14 tracey sizeof(new_srv->fcgi_socket_bind));
389 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->fcgi_socket_bind)) {
390 a596b957 2022-07-14 tracey yyerror("%s: fcgi_socket_bind truncated",
391 a596b957 2022-07-14 tracey __func__);
392 a596b957 2022-07-14 tracey free($3);
395 a596b957 2022-07-14 tracey free($3);
397 a596b957 2022-07-14 tracey | PORT fcgiport {
398 a596b957 2022-07-14 tracey struct server *srv;
400 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
401 a596b957 2022-07-14 tracey if (srv->fcgi_socket_port == $2) {
402 a596b957 2022-07-14 tracey yyerror("port already assigned");
406 a596b957 2022-07-14 tracey new_srv->fcgi_socket_port = $2;
410 a596b957 2022-07-14 tracey socketopts2 : socketopts2 socketopts1 nl
411 a596b957 2022-07-14 tracey | socketopts1 optnl
414 d8473d93 2022-08-11 stsp socketopts3 : LISTEN ON STRING {
415 a596b957 2022-07-14 tracey n = strlcpy(gotwebd->fcgi_socket_bind, $3,
416 a596b957 2022-07-14 tracey sizeof(gotwebd->fcgi_socket_bind));
417 a596b957 2022-07-14 tracey if (n >= sizeof(gotwebd->fcgi_socket_bind)) {
418 a596b957 2022-07-14 tracey yyerror("%s: fcgi_socket_bind truncated",
419 a596b957 2022-07-14 tracey __func__);
420 a596b957 2022-07-14 tracey free($3);
423 a596b957 2022-07-14 tracey free($3);
425 a596b957 2022-07-14 tracey | PORT fcgiport {
426 a596b957 2022-07-14 tracey gotwebd->fcgi_socket_port = $2;
430 a596b957 2022-07-14 tracey socketopts4 : socketopts4 socketopts3 nl
431 a596b957 2022-07-14 tracey | socketopts3 optnl
434 a596b957 2022-07-14 tracey nl : '\n' optnl
437 a596b957 2022-07-14 tracey optnl : '\n' optnl /* zero or more newlines */
438 a596b957 2022-07-14 tracey | /* empty */
443 a596b957 2022-07-14 tracey struct keywords {
444 a596b957 2022-07-14 tracey const char *k_name;
445 a596b957 2022-07-14 tracey int k_val;
449 a596b957 2022-07-14 tracey yyerror(const char *fmt, ...)
451 a596b957 2022-07-14 tracey va_list ap;
452 a596b957 2022-07-14 tracey char *msg;
454 a596b957 2022-07-14 tracey file->errors++;
455 a596b957 2022-07-14 tracey va_start(ap, fmt);
456 a596b957 2022-07-14 tracey if (vasprintf(&msg, fmt, ap) == -1)
457 a596b957 2022-07-14 tracey fatalx("yyerror vasprintf");
458 a596b957 2022-07-14 tracey va_end(ap);
459 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
460 a596b957 2022-07-14 tracey free(msg);
461 a596b957 2022-07-14 tracey return (0);
465 a596b957 2022-07-14 tracey kw_cmp(const void *k, const void *e)
467 a596b957 2022-07-14 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
471 a596b957 2022-07-14 tracey lookup(char *s)
473 a596b957 2022-07-14 tracey /* This has to be sorted always. */
474 a596b957 2022-07-14 tracey static const struct keywords keywords[] = {
475 a596b957 2022-07-14 tracey { "chroot", CHROOT },
476 a596b957 2022-07-14 tracey { "custom_css", CUSTOM_CSS },
477 a596b957 2022-07-14 tracey { "fcgi_socket", FCGI_SOCKET },
478 d8473d93 2022-08-11 stsp { "listen", LISTEN },
479 a596b957 2022-07-14 tracey { "logo", LOGO },
480 a596b957 2022-07-14 tracey { "logo_url" , LOGO_URL },
481 a596b957 2022-07-14 tracey { "max_commits_display", MAX_COMMITS_DISPLAY },
482 a596b957 2022-07-14 tracey { "max_repos", MAX_REPOS },
483 a596b957 2022-07-14 tracey { "max_repos_display", MAX_REPOS_DISPLAY },
484 d8473d93 2022-08-11 stsp { "on", ON },
485 a596b957 2022-07-14 tracey { "port", PORT },
486 a596b957 2022-07-14 tracey { "prefork", PREFORK },
487 a596b957 2022-07-14 tracey { "repos_path", REPOS_PATH },
488 a596b957 2022-07-14 tracey { "server", SERVER },
489 a596b957 2022-07-14 tracey { "show_repo_age", SHOW_REPO_AGE },
490 a596b957 2022-07-14 tracey { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
491 a596b957 2022-07-14 tracey { "show_repo_description", SHOW_REPO_DESCRIPTION },
492 a596b957 2022-07-14 tracey { "show_repo_owner", SHOW_REPO_OWNER },
493 a596b957 2022-07-14 tracey { "show_site_owner", SHOW_SITE_OWNER },
494 a596b957 2022-07-14 tracey { "site_link", SITE_LINK },
495 a596b957 2022-07-14 tracey { "site_name", SITE_NAME },
496 a596b957 2022-07-14 tracey { "site_owner", SITE_OWNER },
497 a596b957 2022-07-14 tracey { "unix_socket", UNIX_SOCKET },
498 a596b957 2022-07-14 tracey { "unix_socket_name", UNIX_SOCKET_NAME },
500 a596b957 2022-07-14 tracey const struct keywords *p;
502 a596b957 2022-07-14 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
503 a596b957 2022-07-14 tracey sizeof(keywords[0]), kw_cmp);
506 a596b957 2022-07-14 tracey return (p->k_val);
508 a596b957 2022-07-14 tracey return (STRING);
511 a596b957 2022-07-14 tracey #define MAXPUSHBACK 128
513 a596b957 2022-07-14 tracey unsigned char *parsebuf;
514 a596b957 2022-07-14 tracey int parseindex;
515 a596b957 2022-07-14 tracey unsigned char pushback_buffer[MAXPUSHBACK];
516 a596b957 2022-07-14 tracey int pushback_index = 0;
519 a596b957 2022-07-14 tracey lgetc(int quotec)
521 a596b957 2022-07-14 tracey int c, next;
523 a596b957 2022-07-14 tracey if (parsebuf) {
524 a596b957 2022-07-14 tracey /* Read character from the parsebuffer instead of input. */
525 a596b957 2022-07-14 tracey if (parseindex >= 0) {
526 a596b957 2022-07-14 tracey c = parsebuf[parseindex++];
527 a596b957 2022-07-14 tracey if (c != '\0')
528 a596b957 2022-07-14 tracey return (c);
529 a596b957 2022-07-14 tracey parsebuf = NULL;
531 a596b957 2022-07-14 tracey parseindex++;
534 a596b957 2022-07-14 tracey if (pushback_index)
535 a596b957 2022-07-14 tracey return (pushback_buffer[--pushback_index]);
537 a596b957 2022-07-14 tracey if (quotec) {
538 a596b957 2022-07-14 tracey c = getc(file->stream);
539 a596b957 2022-07-14 tracey if (c == EOF)
540 a596b957 2022-07-14 tracey yyerror("reached end of file while parsing "
541 a596b957 2022-07-14 tracey "quoted string");
542 a596b957 2022-07-14 tracey return (c);
545 a596b957 2022-07-14 tracey c = getc(file->stream);
546 a596b957 2022-07-14 tracey while (c == '\\') {
547 a596b957 2022-07-14 tracey next = getc(file->stream);
548 a596b957 2022-07-14 tracey if (next != '\n') {
549 a596b957 2022-07-14 tracey c = next;
552 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
553 a596b957 2022-07-14 tracey file->lineno++;
554 a596b957 2022-07-14 tracey c = getc(file->stream);
557 a596b957 2022-07-14 tracey return (c);
561 a596b957 2022-07-14 tracey lungetc(int c)
563 a596b957 2022-07-14 tracey if (c == EOF)
564 a596b957 2022-07-14 tracey return (EOF);
565 a596b957 2022-07-14 tracey if (parsebuf) {
566 a596b957 2022-07-14 tracey parseindex--;
567 a596b957 2022-07-14 tracey if (parseindex >= 0)
568 a596b957 2022-07-14 tracey return (c);
570 a596b957 2022-07-14 tracey if (pushback_index < MAXPUSHBACK-1)
571 a596b957 2022-07-14 tracey return (pushback_buffer[pushback_index++] = c);
573 a596b957 2022-07-14 tracey return (EOF);
577 a596b957 2022-07-14 tracey findeol(void)
581 a596b957 2022-07-14 tracey parsebuf = NULL;
583 a596b957 2022-07-14 tracey /* Skip to either EOF or the first real EOL. */
584 a596b957 2022-07-14 tracey while (1) {
585 a596b957 2022-07-14 tracey if (pushback_index)
586 a596b957 2022-07-14 tracey c = pushback_buffer[--pushback_index];
588 a596b957 2022-07-14 tracey c = lgetc(0);
589 a596b957 2022-07-14 tracey if (c == '\n') {
590 a596b957 2022-07-14 tracey file->lineno++;
593 a596b957 2022-07-14 tracey if (c == EOF)
596 a596b957 2022-07-14 tracey return (ERROR);
600 a596b957 2022-07-14 tracey yylex(void)
602 a596b957 2022-07-14 tracey unsigned char buf[8096];
603 a596b957 2022-07-14 tracey unsigned char *p, *val;
604 a596b957 2022-07-14 tracey int quotec, next, c;
605 a596b957 2022-07-14 tracey int token;
609 a596b957 2022-07-14 tracey c = lgetc(0);
610 a596b957 2022-07-14 tracey while (c == ' ' || c == '\t')
611 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
613 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
614 a596b957 2022-07-14 tracey if (c == '#') {
615 a596b957 2022-07-14 tracey c = lgetc(0);
616 a596b957 2022-07-14 tracey while (c != '\n' && c != EOF)
617 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
619 a596b957 2022-07-14 tracey if (c == '$' && parsebuf == NULL) {
620 a596b957 2022-07-14 tracey while (1) {
621 a596b957 2022-07-14 tracey c = lgetc(0);
622 a596b957 2022-07-14 tracey if (c == EOF)
623 a596b957 2022-07-14 tracey return (0);
625 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
626 a596b957 2022-07-14 tracey yyerror("string too long");
627 a596b957 2022-07-14 tracey return (findeol());
629 a596b957 2022-07-14 tracey if (isalnum(c) || c == '_') {
630 a596b957 2022-07-14 tracey *p++ = c;
631 a596b957 2022-07-14 tracey continue;
633 a596b957 2022-07-14 tracey *p = '\0';
634 a596b957 2022-07-14 tracey lungetc(c);
637 a596b957 2022-07-14 tracey val = symget(buf);
638 a596b957 2022-07-14 tracey if (val == NULL) {
639 a596b957 2022-07-14 tracey yyerror("macro '%s' not defined", buf);
640 a596b957 2022-07-14 tracey return (findeol());
642 a596b957 2022-07-14 tracey parsebuf = val;
643 a596b957 2022-07-14 tracey parseindex = 0;
644 a596b957 2022-07-14 tracey goto top;
647 a596b957 2022-07-14 tracey switch (c) {
648 a596b957 2022-07-14 tracey case '\'':
649 a596b957 2022-07-14 tracey case '"':
650 a596b957 2022-07-14 tracey quotec = c;
651 a596b957 2022-07-14 tracey while (1) {
652 a596b957 2022-07-14 tracey c = lgetc(quotec);
653 a596b957 2022-07-14 tracey if (c == EOF)
654 a596b957 2022-07-14 tracey return (0);
655 a596b957 2022-07-14 tracey if (c == '\n') {
656 a596b957 2022-07-14 tracey file->lineno++;
657 a596b957 2022-07-14 tracey continue;
658 a596b957 2022-07-14 tracey } else if (c == '\\') {
659 a596b957 2022-07-14 tracey next = lgetc(quotec);
660 a596b957 2022-07-14 tracey if (next == EOF)
661 a596b957 2022-07-14 tracey return (0);
662 a596b957 2022-07-14 tracey if (next == quotec || c == ' ' || c == '\t')
663 a596b957 2022-07-14 tracey c = next;
664 a596b957 2022-07-14 tracey else if (next == '\n') {
665 a596b957 2022-07-14 tracey file->lineno++;
666 a596b957 2022-07-14 tracey continue;
668 a596b957 2022-07-14 tracey lungetc(next);
669 a596b957 2022-07-14 tracey } else if (c == quotec) {
670 a596b957 2022-07-14 tracey *p = '\0';
672 a596b957 2022-07-14 tracey } else if (c == '\0') {
673 a596b957 2022-07-14 tracey yyerror("syntax error");
674 a596b957 2022-07-14 tracey return (findeol());
676 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
677 a596b957 2022-07-14 tracey yyerror("string too long");
678 a596b957 2022-07-14 tracey return (findeol());
680 a596b957 2022-07-14 tracey *p++ = c;
682 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
683 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
684 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
685 a596b957 2022-07-14 tracey return (STRING);
688 a596b957 2022-07-14 tracey #define allowed_to_end_number(x) \
689 a596b957 2022-07-14 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
691 a596b957 2022-07-14 tracey if (c == '-' || isdigit(c)) {
693 a596b957 2022-07-14 tracey *p++ = c;
694 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
695 a596b957 2022-07-14 tracey yyerror("string too long");
696 a596b957 2022-07-14 tracey return (findeol());
698 a596b957 2022-07-14 tracey c = lgetc(0);
699 a596b957 2022-07-14 tracey } while (c != EOF && isdigit(c));
700 a596b957 2022-07-14 tracey lungetc(c);
701 a596b957 2022-07-14 tracey if (p == buf + 1 && buf[0] == '-')
702 a596b957 2022-07-14 tracey goto nodigits;
703 a596b957 2022-07-14 tracey if (c == EOF || allowed_to_end_number(c)) {
704 a596b957 2022-07-14 tracey const char *errstr = NULL;
706 a596b957 2022-07-14 tracey *p = '\0';
707 a596b957 2022-07-14 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
708 a596b957 2022-07-14 tracey LLONG_MAX, &errstr);
709 a596b957 2022-07-14 tracey if (errstr) {
710 a596b957 2022-07-14 tracey yyerror("\"%s\" invalid number: %s",
711 a596b957 2022-07-14 tracey buf, errstr);
712 a596b957 2022-07-14 tracey return (findeol());
714 a596b957 2022-07-14 tracey return (NUMBER);
716 a596b957 2022-07-14 tracey nodigits:
717 a596b957 2022-07-14 tracey while (p > buf + 1)
718 a596b957 2022-07-14 tracey lungetc(*--p);
719 a596b957 2022-07-14 tracey c = *--p;
720 a596b957 2022-07-14 tracey if (c == '-')
721 a596b957 2022-07-14 tracey return (c);
725 a596b957 2022-07-14 tracey #define allowed_in_string(x) \
726 a596b957 2022-07-14 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
727 a596b957 2022-07-14 tracey x != '{' && x != '}' && \
728 a596b957 2022-07-14 tracey x != '!' && x != '=' && x != '#' && \
729 a596b957 2022-07-14 tracey x != ','))
731 a596b957 2022-07-14 tracey if (isalnum(c) || c == ':' || c == '_') {
733 a596b957 2022-07-14 tracey *p++ = c;
734 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
735 a596b957 2022-07-14 tracey yyerror("string too long");
736 a596b957 2022-07-14 tracey return (findeol());
738 a596b957 2022-07-14 tracey c = lgetc(0);
739 a596b957 2022-07-14 tracey } while (c != EOF && (allowed_in_string(c)));
740 a596b957 2022-07-14 tracey lungetc(c);
741 a596b957 2022-07-14 tracey *p = '\0';
742 a596b957 2022-07-14 tracey token = lookup(buf);
743 a596b957 2022-07-14 tracey if (token == STRING) {
744 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
745 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
746 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
748 a596b957 2022-07-14 tracey return (token);
750 a596b957 2022-07-14 tracey if (c == '\n') {
751 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
752 a596b957 2022-07-14 tracey file->lineno++;
754 a596b957 2022-07-14 tracey if (c == EOF)
755 a596b957 2022-07-14 tracey return (0);
756 a596b957 2022-07-14 tracey return (c);
760 a596b957 2022-07-14 tracey check_file_secrecy(int fd, const char *fname)
762 a596b957 2022-07-14 tracey struct stat st;
764 a596b957 2022-07-14 tracey if (fstat(fd, &st)) {
765 a596b957 2022-07-14 tracey log_warn("cannot stat %s", fname);
766 a596b957 2022-07-14 tracey return (-1);
768 a596b957 2022-07-14 tracey if (st.st_uid != 0 && st.st_uid != getuid()) {
769 a596b957 2022-07-14 tracey log_warnx("%s: owner not root or current user", fname);
770 a596b957 2022-07-14 tracey return (-1);
772 a596b957 2022-07-14 tracey if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
773 a596b957 2022-07-14 tracey log_warnx("%s: group writable or world read/writable", fname);
774 a596b957 2022-07-14 tracey return (-1);
776 a596b957 2022-07-14 tracey return (0);
779 a596b957 2022-07-14 tracey struct file *
780 a596b957 2022-07-14 tracey newfile(const char *name, int secret)
782 a596b957 2022-07-14 tracey struct file *nfile;
784 a596b957 2022-07-14 tracey nfile = calloc(1, sizeof(struct file));
785 a596b957 2022-07-14 tracey if (nfile == NULL) {
786 a596b957 2022-07-14 tracey log_warn("calloc");
787 a596b957 2022-07-14 tracey return (NULL);
789 a596b957 2022-07-14 tracey nfile->name = strdup(name);
790 a596b957 2022-07-14 tracey if (nfile->name == NULL) {
791 a596b957 2022-07-14 tracey log_warn("strdup");
792 a596b957 2022-07-14 tracey free(nfile);
793 a596b957 2022-07-14 tracey return (NULL);
795 a596b957 2022-07-14 tracey nfile->stream = fopen(nfile->name, "r");
796 a596b957 2022-07-14 tracey if (nfile->stream == NULL) {
797 a596b957 2022-07-14 tracey /* no warning, we don't require a conf file */
798 a596b957 2022-07-14 tracey free(nfile->name);
799 a596b957 2022-07-14 tracey free(nfile);
800 a596b957 2022-07-14 tracey return (NULL);
801 a596b957 2022-07-14 tracey } else if (secret &&
802 a596b957 2022-07-14 tracey check_file_secrecy(fileno(nfile->stream), nfile->name)) {
803 a596b957 2022-07-14 tracey fclose(nfile->stream);
804 a596b957 2022-07-14 tracey free(nfile->name);
805 a596b957 2022-07-14 tracey free(nfile);
806 a596b957 2022-07-14 tracey return (NULL);
808 a596b957 2022-07-14 tracey nfile->lineno = 1;
809 a596b957 2022-07-14 tracey return (nfile);
812 a596b957 2022-07-14 tracey static void
813 a596b957 2022-07-14 tracey closefile(struct file *xfile)
815 a596b957 2022-07-14 tracey fclose(xfile->stream);
816 a596b957 2022-07-14 tracey free(xfile->name);
817 a596b957 2022-07-14 tracey free(xfile);
820 a0037b73 2022-08-03 stsp static void
821 a0037b73 2022-08-03 stsp add_default_server(void)
823 a0037b73 2022-08-03 stsp new_srv = conf_new_server(D_SITENAME);
824 a0037b73 2022-08-03 stsp log_debug("%s: adding default server %s", __func__, D_SITENAME);
828 a596b957 2022-07-14 tracey parse_config(const char *filename, struct gotwebd *env)
830 a596b957 2022-07-14 tracey struct sym *sym, *next;
832 a596b957 2022-07-14 tracey if (config_init(env) == -1)
833 a596b957 2022-07-14 tracey fatalx("failed to initialize configuration");
835 a596b957 2022-07-14 tracey gotwebd = env;
837 a0037b73 2022-08-03 stsp file = newfile(filename, 0);
838 a0037b73 2022-08-03 stsp if (file == NULL) {
839 a0037b73 2022-08-03 stsp add_default_server();
840 a0037b73 2022-08-03 stsp sockets_parse_sockets(env);
841 a0037b73 2022-08-03 stsp /* just return, as we don't require a conf file */
842 a0037b73 2022-08-03 stsp return (0);
845 a596b957 2022-07-14 tracey yyparse();
846 a596b957 2022-07-14 tracey errors = file->errors;
847 a596b957 2022-07-14 tracey closefile(file);
849 a596b957 2022-07-14 tracey /* Free macros and check which have not been used. */
850 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
851 a596b957 2022-07-14 tracey if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
852 a596b957 2022-07-14 tracey fprintf(stderr, "warning: macro '%s' not used\n",
853 a596b957 2022-07-14 tracey sym->nam);
854 a596b957 2022-07-14 tracey if (!sym->persist) {
855 a596b957 2022-07-14 tracey free(sym->nam);
856 a596b957 2022-07-14 tracey free(sym->val);
857 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
858 a596b957 2022-07-14 tracey free(sym);
862 a596b957 2022-07-14 tracey if (errors)
863 a596b957 2022-07-14 tracey return (-1);
865 a596b957 2022-07-14 tracey /* just add default server if no config specified */
866 a0037b73 2022-08-03 stsp if (gotwebd->server_cnt == 0)
867 a0037b73 2022-08-03 stsp add_default_server();
869 a596b957 2022-07-14 tracey /* setup our listening sockets */
870 a596b957 2022-07-14 tracey sockets_parse_sockets(env);
872 a596b957 2022-07-14 tracey return (0);
875 a596b957 2022-07-14 tracey struct server *
876 a596b957 2022-07-14 tracey conf_new_server(const char *name)
878 a596b957 2022-07-14 tracey struct server *srv = NULL;
881 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
882 a596b957 2022-07-14 tracey if (srv == NULL)
883 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
885 a596b957 2022-07-14 tracey n = strlcpy(srv->name, name, sizeof(srv->name));
886 a596b957 2022-07-14 tracey if (n >= sizeof(srv->name))
887 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
888 a596b957 2022-07-14 tracey n = snprintf(srv->unix_socket_name,
889 a596b957 2022-07-14 tracey sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
890 a596b957 2022-07-14 tracey D_UNIX_SOCKET);
891 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
892 a596b957 2022-07-14 tracey fatalx("%s: snprintf", __func__);
893 a596b957 2022-07-14 tracey n = strlcpy(srv->repos_path, D_GOTPATH,
894 a596b957 2022-07-14 tracey sizeof(srv->repos_path));
895 a596b957 2022-07-14 tracey if (n >= sizeof(srv->repos_path))
896 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
897 a596b957 2022-07-14 tracey n = strlcpy(srv->site_name, D_SITENAME,
898 a596b957 2022-07-14 tracey sizeof(srv->site_name));
899 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_name))
900 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
901 a596b957 2022-07-14 tracey n = strlcpy(srv->site_owner, D_SITEOWNER,
902 a596b957 2022-07-14 tracey sizeof(srv->site_owner));
903 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_owner))
904 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
905 a596b957 2022-07-14 tracey n = strlcpy(srv->site_link, D_SITELINK,
906 a596b957 2022-07-14 tracey sizeof(srv->site_link));
907 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_link))
908 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
909 a596b957 2022-07-14 tracey n = strlcpy(srv->logo, D_GOTLOGO,
910 a596b957 2022-07-14 tracey sizeof(srv->logo));
911 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo))
912 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
913 a596b957 2022-07-14 tracey n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
914 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo_url))
915 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
916 a596b957 2022-07-14 tracey n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
917 a596b957 2022-07-14 tracey if (n >= sizeof(srv->custom_css))
918 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
920 a596b957 2022-07-14 tracey val = getservice(D_FCGI_PORT);
921 a596b957 2022-07-14 tracey srv->fcgi_socket_port = gotwebd->fcgi_socket_port ?
922 a596b957 2022-07-14 tracey gotwebd->fcgi_socket_port: htons(val);
924 a596b957 2022-07-14 tracey srv->show_site_owner = D_SHOWSOWNER;
925 a596b957 2022-07-14 tracey srv->show_repo_owner = D_SHOWROWNER;
926 a596b957 2022-07-14 tracey srv->show_repo_age = D_SHOWAGE;
927 a596b957 2022-07-14 tracey srv->show_repo_description = D_SHOWDESC;
928 a596b957 2022-07-14 tracey srv->show_repo_cloneurl = D_SHOWURL;
930 a596b957 2022-07-14 tracey srv->max_repos_display = D_MAXREPODISP;
931 a596b957 2022-07-14 tracey srv->max_commits_display = D_MAXCOMMITDISP;
932 a596b957 2022-07-14 tracey srv->max_repos = D_MAXREPO;
934 a596b957 2022-07-14 tracey srv->unix_socket = 1;
935 a596b957 2022-07-14 tracey srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
937 e087e1f6 2022-08-16 stsp TAILQ_INIT(&srv->al);
938 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
939 a596b957 2022-07-14 tracey gotwebd->server_cnt++;
941 a596b957 2022-07-14 tracey return srv;
945 a596b957 2022-07-14 tracey symset(const char *nam, const char *val, int persist)
947 a596b957 2022-07-14 tracey struct sym *sym;
949 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
950 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0)
954 a596b957 2022-07-14 tracey if (sym != NULL) {
955 a596b957 2022-07-14 tracey if (sym->persist == 1)
956 a596b957 2022-07-14 tracey return (0);
958 a596b957 2022-07-14 tracey free(sym->nam);
959 a596b957 2022-07-14 tracey free(sym->val);
960 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
961 a596b957 2022-07-14 tracey free(sym);
964 a596b957 2022-07-14 tracey sym = calloc(1, sizeof(*sym));
965 a596b957 2022-07-14 tracey if (sym == NULL)
966 a596b957 2022-07-14 tracey return (-1);
968 a596b957 2022-07-14 tracey sym->nam = strdup(nam);
969 a596b957 2022-07-14 tracey if (sym->nam == NULL) {
970 a596b957 2022-07-14 tracey free(sym);
971 a596b957 2022-07-14 tracey return (-1);
973 a596b957 2022-07-14 tracey sym->val = strdup(val);
974 a596b957 2022-07-14 tracey if (sym->val == NULL) {
975 a596b957 2022-07-14 tracey free(sym->nam);
976 a596b957 2022-07-14 tracey free(sym);
977 a596b957 2022-07-14 tracey return (-1);
979 a596b957 2022-07-14 tracey sym->used = 0;
980 a596b957 2022-07-14 tracey sym->persist = persist;
981 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
982 a596b957 2022-07-14 tracey return (0);
986 a596b957 2022-07-14 tracey cmdline_symset(char *s)
988 a596b957 2022-07-14 tracey char *sym, *val;
990 a596b957 2022-07-14 tracey size_t len;
992 a596b957 2022-07-14 tracey val = strrchr(s, '=');
993 a596b957 2022-07-14 tracey if (val == NULL)
994 a596b957 2022-07-14 tracey return (-1);
996 a596b957 2022-07-14 tracey len = strlen(s) - strlen(val) + 1;
997 a596b957 2022-07-14 tracey sym = malloc(len);
998 a596b957 2022-07-14 tracey if (sym == NULL)
999 a596b957 2022-07-14 tracey fatal("%s: malloc", __func__);
1001 a596b957 2022-07-14 tracey memcpy(&sym, s, len);
1003 a596b957 2022-07-14 tracey ret = symset(sym, val + 1, 1);
1004 a596b957 2022-07-14 tracey free(sym);
1006 a596b957 2022-07-14 tracey return (ret);
1010 a596b957 2022-07-14 tracey symget(const char *nam)
1012 a596b957 2022-07-14 tracey struct sym *sym;
1014 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
1015 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0) {
1016 a596b957 2022-07-14 tracey sym->used = 1;
1017 a596b957 2022-07-14 tracey return (sym->val);
1020 a596b957 2022-07-14 tracey return (NULL);
1024 a596b957 2022-07-14 tracey getservice(const char *n)
1026 a596b957 2022-07-14 tracey struct servent *s;
1027 a596b957 2022-07-14 tracey const char *errstr;
1028 a596b957 2022-07-14 tracey long long llval;
1030 a596b957 2022-07-14 tracey llval = strtonum(n, 0, UINT16_MAX, &errstr);
1031 a596b957 2022-07-14 tracey if (errstr) {
1032 a596b957 2022-07-14 tracey s = getservbyname(n, "tcp");
1033 a596b957 2022-07-14 tracey if (s == NULL)
1034 a596b957 2022-07-14 tracey s = getservbyname(n, "udp");
1035 a596b957 2022-07-14 tracey if (s == NULL)
1036 a596b957 2022-07-14 tracey return (-1);
1037 a596b957 2022-07-14 tracey return (s->s_port);
1040 a596b957 2022-07-14 tracey return (htons((unsigned short)llval));
1043 a596b957 2022-07-14 tracey struct address *
1044 a596b957 2022-07-14 tracey host_v4(const char *s)
1046 a596b957 2022-07-14 tracey struct in_addr ina;
1047 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1048 a596b957 2022-07-14 tracey struct address *h;
1050 a596b957 2022-07-14 tracey memset(&ina, 0, sizeof(ina));
1051 a596b957 2022-07-14 tracey if (inet_pton(AF_INET, s, &ina) != 1)
1052 a596b957 2022-07-14 tracey return (NULL);
1054 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1055 a596b957 2022-07-14 tracey fatal(__func__);
1056 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1057 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ina);
1058 a596b957 2022-07-14 tracey if (sain->sin_addr.s_addr == INADDR_ANY)
1059 a596b957 2022-07-14 tracey h->prefixlen = 0; /* 0.0.0.0 address */
1061 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1062 a596b957 2022-07-14 tracey return (h);
1065 a596b957 2022-07-14 tracey struct address *
1066 a596b957 2022-07-14 tracey host_v6(const char *s)
1068 a596b957 2022-07-14 tracey struct addrinfo hints, *res;
1069 86b4b772 2022-07-29 stsp struct sockaddr_in6 *sa_in6, *ra;
1070 a596b957 2022-07-14 tracey struct address *h = NULL;
1072 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1073 a596b957 2022-07-14 tracey hints.ai_family = AF_INET6;
1074 a596b957 2022-07-14 tracey hints.ai_socktype = SOCK_DGRAM; /* dummy */
1075 a596b957 2022-07-14 tracey hints.ai_flags = AI_NUMERICHOST;
1076 a596b957 2022-07-14 tracey if (getaddrinfo(s, "0", &hints, &res) == 0) {
1077 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1078 a596b957 2022-07-14 tracey fatal(__func__);
1079 a596b957 2022-07-14 tracey sa_in6 = (struct sockaddr_in6 *)&h->ss;
1080 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)res->ai_addr;
1081 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1082 86b4b772 2022-07-29 stsp ra->sin6_scope_id);
1083 a596b957 2022-07-14 tracey if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1084 a596b957 2022-07-14 tracey sizeof(sa_in6->sin6_addr)) == 0)
1085 a596b957 2022-07-14 tracey h->prefixlen = 0; /* any address */
1087 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1088 a596b957 2022-07-14 tracey freeaddrinfo(res);
1091 a596b957 2022-07-14 tracey return (h);
1095 a596b957 2022-07-14 tracey host_dns(const char *s, struct addresslist *al, int max,
1096 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1098 a596b957 2022-07-14 tracey struct addrinfo hints, *res0, *res;
1099 a596b957 2022-07-14 tracey int error, cnt = 0;
1100 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1101 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1102 a596b957 2022-07-14 tracey struct address *h;
1104 a596b957 2022-07-14 tracey if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0)
1105 a596b957 2022-07-14 tracey return (cnt);
1107 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1108 a596b957 2022-07-14 tracey hints.ai_family = PF_UNSPEC;
1109 a596b957 2022-07-14 tracey hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1110 a596b957 2022-07-14 tracey hints.ai_flags = AI_ADDRCONFIG;
1111 a596b957 2022-07-14 tracey error = getaddrinfo(s, NULL, &hints, &res0);
1112 a596b957 2022-07-14 tracey if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1113 a596b957 2022-07-14 tracey return (0);
1114 a596b957 2022-07-14 tracey if (error) {
1115 a596b957 2022-07-14 tracey log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1116 a596b957 2022-07-14 tracey gai_strerror(error));
1117 a596b957 2022-07-14 tracey return (-1);
1120 a596b957 2022-07-14 tracey for (res = res0; res && cnt < max; res = res->ai_next) {
1121 a596b957 2022-07-14 tracey if (res->ai_family != AF_INET &&
1122 a596b957 2022-07-14 tracey res->ai_family != AF_INET6)
1123 a596b957 2022-07-14 tracey continue;
1124 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1125 a596b957 2022-07-14 tracey fatal(__func__);
1127 a596b957 2022-07-14 tracey if (port)
1128 a596b957 2022-07-14 tracey h->port = port;
1129 a596b957 2022-07-14 tracey if (ifname != NULL) {
1130 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1131 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1132 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1133 a596b957 2022-07-14 tracey __func__);
1134 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1135 a596b957 2022-07-14 tracey free(h);
1136 a596b957 2022-07-14 tracey return (-1);
1139 a596b957 2022-07-14 tracey if (ipproto != -1)
1140 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1141 a596b957 2022-07-14 tracey h->ss.ss_family = res->ai_family;
1142 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1144 a596b957 2022-07-14 tracey if (res->ai_family == AF_INET) {
1145 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1146 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1147 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)res->ai_addr;
1148 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1149 a596b957 2022-07-14 tracey } else {
1150 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1151 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1152 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)res->ai_addr;
1153 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1156 a596b957 2022-07-14 tracey TAILQ_INSERT_HEAD(al, h, entry);
1159 a596b957 2022-07-14 tracey if (cnt == max && res) {
1160 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1161 a596b957 2022-07-14 tracey s, max);
1163 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1164 a596b957 2022-07-14 tracey return (cnt);
1168 a596b957 2022-07-14 tracey host_if(const char *s, struct addresslist *al, int max,
1169 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1171 a596b957 2022-07-14 tracey struct ifaddrs *ifap, *p;
1172 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1173 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1174 a596b957 2022-07-14 tracey struct address *h;
1175 a596b957 2022-07-14 tracey int cnt = 0, af;
1177 a596b957 2022-07-14 tracey if (getifaddrs(&ifap) == -1)
1178 a596b957 2022-07-14 tracey fatal("getifaddrs");
1180 a596b957 2022-07-14 tracey /* First search for IPv4 addresses */
1181 a596b957 2022-07-14 tracey af = AF_INET;
1184 a596b957 2022-07-14 tracey for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1185 a596b957 2022-07-14 tracey if (p->ifa_addr == NULL ||
1186 a596b957 2022-07-14 tracey p->ifa_addr->sa_family != af ||
1187 a596b957 2022-07-14 tracey (strcmp(s, p->ifa_name) != 0 &&
1188 a596b957 2022-07-14 tracey !is_if_in_group(p->ifa_name, s)))
1189 a596b957 2022-07-14 tracey continue;
1190 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1191 a596b957 2022-07-14 tracey fatal("calloc");
1193 a596b957 2022-07-14 tracey if (port)
1194 a596b957 2022-07-14 tracey h->port = port;
1195 a596b957 2022-07-14 tracey if (ifname != NULL) {
1196 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1197 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1198 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1199 a596b957 2022-07-14 tracey __func__);
1200 a596b957 2022-07-14 tracey free(h);
1201 a596b957 2022-07-14 tracey freeifaddrs(ifap);
1202 a596b957 2022-07-14 tracey return (-1);
1205 a596b957 2022-07-14 tracey if (ipproto != -1)
1206 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1207 a596b957 2022-07-14 tracey h->ss.ss_family = af;
1208 a596b957 2022-07-14 tracey h->prefixlen = -1; /* host address */
1210 a596b957 2022-07-14 tracey if (af == AF_INET) {
1211 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1212 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1213 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)p->ifa_addr;
1214 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1215 a596b957 2022-07-14 tracey } else {
1216 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1217 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1218 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)p->ifa_addr;
1219 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1220 86b4b772 2022-07-29 stsp ra->sin6_scope_id);
1223 a596b957 2022-07-14 tracey TAILQ_INSERT_HEAD(al, h, entry);
1226 a596b957 2022-07-14 tracey if (af == AF_INET) {
1227 a596b957 2022-07-14 tracey /* Next search for IPv6 addresses */
1228 a596b957 2022-07-14 tracey af = AF_INET6;
1229 a596b957 2022-07-14 tracey goto nextaf;
1232 a596b957 2022-07-14 tracey if (cnt > max) {
1233 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1234 a596b957 2022-07-14 tracey s, max);
1236 a596b957 2022-07-14 tracey freeifaddrs(ifap);
1237 a596b957 2022-07-14 tracey return (cnt);
1241 a596b957 2022-07-14 tracey host(const char *s, struct addresslist *al, int max,
1242 a596b957 2022-07-14 tracey in_port_t port, const char *ifname, int ipproto)
1244 a596b957 2022-07-14 tracey struct address *h;
1246 a596b957 2022-07-14 tracey h = host_v4(s);
1248 a596b957 2022-07-14 tracey /* IPv6 address? */
1249 a596b957 2022-07-14 tracey if (h == NULL)
1250 a596b957 2022-07-14 tracey h = host_v6(s);
1252 a596b957 2022-07-14 tracey if (h != NULL) {
1253 a596b957 2022-07-14 tracey if (port)
1254 a596b957 2022-07-14 tracey h->port = port;
1255 a596b957 2022-07-14 tracey if (ifname != NULL) {
1256 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1257 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1258 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1259 a596b957 2022-07-14 tracey __func__);
1260 a596b957 2022-07-14 tracey free(h);
1261 a596b957 2022-07-14 tracey return (-1);
1264 a596b957 2022-07-14 tracey if (ipproto != -1)
1265 a596b957 2022-07-14 tracey h->ipproto = ipproto;
1267 a596b957 2022-07-14 tracey TAILQ_INSERT_HEAD(al, h, entry);
1268 a596b957 2022-07-14 tracey return (1);
1271 a596b957 2022-07-14 tracey return (host_dns(s, al, max, port, ifname, ipproto));
1275 a596b957 2022-07-14 tracey is_if_in_group(const char *ifname, const char *groupname)
1277 a596b957 2022-07-14 tracey unsigned int len;
1278 a596b957 2022-07-14 tracey struct ifgroupreq ifgr;
1279 a596b957 2022-07-14 tracey struct ifg_req *ifg;
1281 a596b957 2022-07-14 tracey int ret = 0;
1283 a596b957 2022-07-14 tracey if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1284 a596b957 2022-07-14 tracey err(1, "socket");
1286 a596b957 2022-07-14 tracey memset(&ifgr, 0, sizeof(ifgr));
1287 a596b957 2022-07-14 tracey if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1288 a596b957 2022-07-14 tracey err(1, "IFNAMSIZ");
1289 a596b957 2022-07-14 tracey if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1290 a596b957 2022-07-14 tracey if (errno == EINVAL || errno == ENOTTY)
1291 a596b957 2022-07-14 tracey goto end;
1292 a596b957 2022-07-14 tracey err(1, "SIOCGIFGROUP");
1295 a596b957 2022-07-14 tracey len = ifgr.ifgr_len;
1296 a596b957 2022-07-14 tracey ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1297 a596b957 2022-07-14 tracey sizeof(struct ifg_req));
1298 a596b957 2022-07-14 tracey if (ifgr.ifgr_groups == NULL)
1299 a596b957 2022-07-14 tracey err(1, "getifgroups");
1300 a596b957 2022-07-14 tracey if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1301 a596b957 2022-07-14 tracey err(1, "SIOCGIFGROUP");
1303 a596b957 2022-07-14 tracey ifg = ifgr.ifgr_groups;
1304 a596b957 2022-07-14 tracey for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1305 a596b957 2022-07-14 tracey len -= sizeof(struct ifg_req);
1306 a596b957 2022-07-14 tracey if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1307 a596b957 2022-07-14 tracey ret = 1;
1311 a596b957 2022-07-14 tracey free(ifgr.ifgr_groups);
1314 a596b957 2022-07-14 tracey close(s);
1315 a596b957 2022-07-14 tracey return (ret);
1319 a596b957 2022-07-14 tracey get_addrs(const char *addr, struct addresslist *al, in_port_t port)
1321 a596b957 2022-07-14 tracey if (strcmp("", addr) == 0) {
1322 a596b957 2022-07-14 tracey if (host("0.0.0.0", al, 1, port, "0.0.0.0", -1) <= 0) {
1323 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s",
1324 a596b957 2022-07-14 tracey "0.0.0.0");
1325 a596b957 2022-07-14 tracey return (-1);
1327 a596b957 2022-07-14 tracey if (host("::", al, 1, port, "::", -1) <= 0) {
1328 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s", "::");
1329 a596b957 2022-07-14 tracey return (-1);
1331 a596b957 2022-07-14 tracey } else {
1332 a596b957 2022-07-14 tracey if (host(addr, al, GOTWEBD_MAXIFACE, port, addr,
1333 a596b957 2022-07-14 tracey -1) <= 0) {
1334 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s", addr);
1335 a596b957 2022-07-14 tracey return (-1);
1338 a596b957 2022-07-14 tracey return (0);