2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 8a35f56c 2022-07-16 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 8a35f56c 2022-07-16 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
11 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
12 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
14 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
26 8a35f56c 2022-07-16 thomas #include <sys/ioctl.h>
27 8a35f56c 2022-07-16 thomas #include <sys/types.h>
28 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
29 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
30 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
32 8a35f56c 2022-07-16 thomas #include <net/if.h>
33 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
35 8a35f56c 2022-07-16 thomas #include <arpa/inet.h>
37 8a35f56c 2022-07-16 thomas #include <ctype.h>
38 8a35f56c 2022-07-16 thomas #include <err.h>
39 8a35f56c 2022-07-16 thomas #include <errno.h>
40 8a35f56c 2022-07-16 thomas #include <event.h>
41 8a35f56c 2022-07-16 thomas #include <ifaddrs.h>
42 8a35f56c 2022-07-16 thomas #include <limits.h>
43 8a35f56c 2022-07-16 thomas #include <netdb.h>
44 8a35f56c 2022-07-16 thomas #include <stdarg.h>
45 8a35f56c 2022-07-16 thomas #include <stdlib.h>
46 8a35f56c 2022-07-16 thomas #include <stdio.h>
47 8a35f56c 2022-07-16 thomas #include <string.h>
48 8a35f56c 2022-07-16 thomas #include <syslog.h>
49 8a35f56c 2022-07-16 thomas #include <unistd.h>
51 8a35f56c 2022-07-16 thomas #include "proc.h"
52 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
53 cdbe1d7d 2022-08-06 thomas #include "got_sockaddr.h"
55 8a35f56c 2022-07-16 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 8a35f56c 2022-07-16 thomas static struct file {
57 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(file) entry;
58 8a35f56c 2022-07-16 thomas FILE *stream;
59 8a35f56c 2022-07-16 thomas char *name;
60 8a35f56c 2022-07-16 thomas int lineno;
61 8a35f56c 2022-07-16 thomas int errors;
63 8a35f56c 2022-07-16 thomas struct file *newfile(const char *, int);
64 8a35f56c 2022-07-16 thomas static void closefile(struct file *);
65 8a35f56c 2022-07-16 thomas int check_file_secrecy(int, const char *);
66 8a35f56c 2022-07-16 thomas int yyparse(void);
67 8a35f56c 2022-07-16 thomas int yylex(void);
68 8a35f56c 2022-07-16 thomas int yyerror(const char *, ...)
69 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)))
70 8a35f56c 2022-07-16 thomas __attribute__((__nonnull__ (1)));
71 8a35f56c 2022-07-16 thomas int kw_cmp(const void *, const void *);
72 8a35f56c 2022-07-16 thomas int lookup(char *);
73 8a35f56c 2022-07-16 thomas int lgetc(int);
74 8a35f56c 2022-07-16 thomas int lungetc(int);
75 8a35f56c 2022-07-16 thomas int findeol(void);
77 8a35f56c 2022-07-16 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 8a35f56c 2022-07-16 thomas struct sym {
79 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(sym) entry;
81 8a35f56c 2022-07-16 thomas int persist;
82 8a35f56c 2022-07-16 thomas char *nam;
83 8a35f56c 2022-07-16 thomas char *val;
86 8a35f56c 2022-07-16 thomas int symset(const char *, const char *, int);
87 8a35f56c 2022-07-16 thomas char *symget(const char *);
89 8a35f56c 2022-07-16 thomas static int errors;
91 8a35f56c 2022-07-16 thomas static struct gotwebd *gotwebd;
92 8a35f56c 2022-07-16 thomas static struct server *new_srv;
93 8a35f56c 2022-07-16 thomas static struct server *conf_new_server(const char *);
94 8a35f56c 2022-07-16 thomas int getservice(const char *);
97 e4c7e0b0 2022-08-30 thomas int get_addrs(const char *, struct server *, in_port_t);
98 e4c7e0b0 2022-08-30 thomas int addr_dup_check(struct addresslist *, struct address *,
99 e4c7e0b0 2022-08-30 thomas const char *, const char *);
100 e4c7e0b0 2022-08-30 thomas int add_addr(struct server *, struct address *);
101 8a35f56c 2022-07-16 thomas struct address *host_v4(const char *);
102 8a35f56c 2022-07-16 thomas struct address *host_v6(const char *);
103 e4c7e0b0 2022-08-30 thomas int host_dns(const char *, struct server *,
104 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
105 e4c7e0b0 2022-08-30 thomas int host_if(const char *, struct server *,
106 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
107 e4c7e0b0 2022-08-30 thomas int host(const char *, struct server *,
108 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
109 8a35f56c 2022-07-16 thomas int is_if_in_group(const char *, const char *);
111 8a35f56c 2022-07-16 thomas typedef struct {
113 8a35f56c 2022-07-16 thomas long long number;
114 8a35f56c 2022-07-16 thomas char *string;
115 8a35f56c 2022-07-16 thomas in_port_t port;
117 8a35f56c 2022-07-16 thomas int lineno;
118 8a35f56c 2022-07-16 thomas } YYSTYPE;
122 c24a47af 2022-08-11 thomas %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
123 8a35f56c 2022-07-16 thomas %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
124 8a35f56c 2022-07-16 thomas %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
125 3991b2a5 2022-10-31 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
126 bea82c4f 2023-01-06 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
128 8a35f56c 2022-07-16 thomas %token <v.string> STRING
129 8a35f56c 2022-07-16 thomas %type <v.port> fcgiport
130 8a35f56c 2022-07-16 thomas %token <v.number> NUMBER
131 8a35f56c 2022-07-16 thomas %type <v.number> boolean
135 cfab1835 2022-10-04 thomas grammar : /* empty */
136 8a35f56c 2022-07-16 thomas | grammar '\n'
137 cfab1835 2022-10-04 thomas | grammar varset '\n'
138 8a35f56c 2022-07-16 thomas | grammar main '\n'
139 8a35f56c 2022-07-16 thomas | grammar server '\n'
140 cfab1835 2022-10-04 thomas | grammar error '\n' { file->errors++; }
143 cfab1835 2022-10-04 thomas varset : STRING '=' STRING {
144 cfab1835 2022-10-04 thomas char *s = $1;
145 cfab1835 2022-10-04 thomas while (*s++) {
146 cfab1835 2022-10-04 thomas if (isspace((unsigned char)*s)) {
147 cfab1835 2022-10-04 thomas yyerror("macro name cannot contain "
148 cfab1835 2022-10-04 thomas "whitespace");
149 cfab1835 2022-10-04 thomas free($1);
150 cfab1835 2022-10-04 thomas free($3);
154 cfab1835 2022-10-04 thomas if (symset($1, $3, 0) == -1)
155 cfab1835 2022-10-04 thomas fatal("cannot store variable");
156 cfab1835 2022-10-04 thomas free($1);
157 cfab1835 2022-10-04 thomas free($3);
161 8a35f56c 2022-07-16 thomas boolean : STRING {
162 8a35f56c 2022-07-16 thomas if (strcasecmp($1, "1") == 0 ||
163 8a35f56c 2022-07-16 thomas strcasecmp($1, "yes") == 0 ||
164 8a35f56c 2022-07-16 thomas strcasecmp($1, "on") == 0)
166 8a35f56c 2022-07-16 thomas else if (strcasecmp($1, "0") == 0 ||
167 8a35f56c 2022-07-16 thomas strcasecmp($1, "off") == 0 ||
168 8a35f56c 2022-07-16 thomas strcasecmp($1, "no") == 0)
171 8a35f56c 2022-07-16 thomas yyerror("invalid boolean value '%s'", $1);
172 8a35f56c 2022-07-16 thomas free($1);
175 8a35f56c 2022-07-16 thomas free($1);
177 8a35f56c 2022-07-16 thomas | ON { $$ = 1; }
178 8a35f56c 2022-07-16 thomas | NUMBER { $$ = $1; }
181 77fb808d 2022-08-29 thomas fcgiport : PORT NUMBER {
182 77fb808d 2022-08-29 thomas if ($2 <= 0 || $2 > (int)USHRT_MAX) {
183 77fb808d 2022-08-29 thomas yyerror("invalid port: %lld", $2);
188 77fb808d 2022-08-29 thomas | PORT STRING {
191 77fb808d 2022-08-29 thomas if ((val = getservice($2)) == -1) {
192 77fb808d 2022-08-29 thomas yyerror("invalid port: %s", $2);
193 77fb808d 2022-08-29 thomas free($2);
196 77fb808d 2022-08-29 thomas free($2);
198 8a35f56c 2022-07-16 thomas $$ = val;
202 8a35f56c 2022-07-16 thomas main : PREFORK NUMBER {
203 8a35f56c 2022-07-16 thomas gotwebd->prefork_gotwebd = $2;
205 8a35f56c 2022-07-16 thomas | CHROOT STRING {
206 8a35f56c 2022-07-16 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
207 8a35f56c 2022-07-16 thomas sizeof(gotwebd->httpd_chroot));
208 8a35f56c 2022-07-16 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
209 8a35f56c 2022-07-16 thomas yyerror("%s: httpd_chroot truncated", __func__);
210 8a35f56c 2022-07-16 thomas free($2);
213 8a35f56c 2022-07-16 thomas free($2);
215 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
216 8a35f56c 2022-07-16 thomas gotwebd->unix_socket = $2;
218 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
219 8a35f56c 2022-07-16 thomas n = snprintf(gotwebd->unix_socket_name,
220 8a35f56c 2022-07-16 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
221 8a35f56c 2022-07-16 thomas strlen(gotwebd->httpd_chroot) ?
222 8a35f56c 2022-07-16 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
223 717a78d4 2022-08-16 thomas if (n < 0 ||
224 717a78d4 2022-08-16 thomas (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
225 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
226 8a35f56c 2022-07-16 thomas __func__);
227 8a35f56c 2022-07-16 thomas free($2);
230 8a35f56c 2022-07-16 thomas free($2);
234 8a35f56c 2022-07-16 thomas server : SERVER STRING {
235 8a35f56c 2022-07-16 thomas struct server *srv;
237 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
238 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
239 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
240 8a35f56c 2022-07-16 thomas free($2);
245 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
246 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
247 8a35f56c 2022-07-16 thomas free($2);
249 8a35f56c 2022-07-16 thomas | SERVER STRING {
250 8a35f56c 2022-07-16 thomas struct server *srv;
252 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
253 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
254 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
255 8a35f56c 2022-07-16 thomas free($2);
260 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
261 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
262 8a35f56c 2022-07-16 thomas free($2);
263 8a35f56c 2022-07-16 thomas } '{' optnl serveropts2 '}' {
267 8a35f56c 2022-07-16 thomas serveropts1 : REPOS_PATH STRING {
268 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->repos_path, $2,
269 8a35f56c 2022-07-16 thomas sizeof(new_srv->repos_path));
270 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->repos_path)) {
271 8a35f56c 2022-07-16 thomas yyerror("%s: repos_path truncated", __func__);
272 8a35f56c 2022-07-16 thomas free($2);
275 8a35f56c 2022-07-16 thomas free($2);
277 8a35f56c 2022-07-16 thomas | SITE_NAME STRING {
278 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_name, $2,
279 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_name));
280 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_name)) {
281 8a35f56c 2022-07-16 thomas yyerror("%s: site_name truncated", __func__);
282 8a35f56c 2022-07-16 thomas free($2);
285 8a35f56c 2022-07-16 thomas free($2);
287 8a35f56c 2022-07-16 thomas | SITE_OWNER STRING {
288 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_owner, $2,
289 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_owner));
290 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_owner)) {
291 8a35f56c 2022-07-16 thomas yyerror("%s: site_owner truncated", __func__);
292 8a35f56c 2022-07-16 thomas free($2);
295 8a35f56c 2022-07-16 thomas free($2);
297 8a35f56c 2022-07-16 thomas | SITE_LINK STRING {
298 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_link, $2,
299 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_link));
300 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_link)) {
301 8a35f56c 2022-07-16 thomas yyerror("%s: site_link truncated", __func__);
302 8a35f56c 2022-07-16 thomas free($2);
305 8a35f56c 2022-07-16 thomas free($2);
307 8a35f56c 2022-07-16 thomas | LOGO STRING {
308 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
309 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo)) {
310 8a35f56c 2022-07-16 thomas yyerror("%s: logo truncated", __func__);
311 8a35f56c 2022-07-16 thomas free($2);
314 8a35f56c 2022-07-16 thomas free($2);
316 8a35f56c 2022-07-16 thomas | LOGO_URL STRING {
317 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo_url, $2,
318 8a35f56c 2022-07-16 thomas sizeof(new_srv->logo_url));
319 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo_url)) {
320 8a35f56c 2022-07-16 thomas yyerror("%s: logo_url truncated", __func__);
321 8a35f56c 2022-07-16 thomas free($2);
324 8a35f56c 2022-07-16 thomas free($2);
326 8a35f56c 2022-07-16 thomas | CUSTOM_CSS STRING {
327 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->custom_css, $2,
328 8a35f56c 2022-07-16 thomas sizeof(new_srv->custom_css));
329 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->custom_css)) {
330 8a35f56c 2022-07-16 thomas yyerror("%s: custom_css truncated", __func__);
331 8a35f56c 2022-07-16 thomas free($2);
334 8a35f56c 2022-07-16 thomas free($2);
336 77fb808d 2022-08-29 thomas | LISTEN ON STRING fcgiport {
337 e4c7e0b0 2022-08-30 thomas if (get_addrs($3, new_srv, $4) == -1) {
338 e4c7e0b0 2022-08-30 thomas yyerror("could not get addrs");
341 e4317279 2022-08-30 thomas new_srv->fcgi_socket = 1;
343 bea82c4f 2023-01-06 thomas | LISTEN ON SOCKET STRING {
344 bea82c4f 2023-01-06 thomas if (!strcasecmp($4, "off") ||
345 bea82c4f 2023-01-06 thomas !strcasecmp($4, "no")) {
346 bea82c4f 2023-01-06 thomas new_srv->unix_socket = 0;
347 bea82c4f 2023-01-06 thomas free($4);
348 bea82c4f 2023-01-06 thomas YYACCEPT;
351 bea82c4f 2023-01-06 thomas new_srv->unix_socket = 1;
353 bea82c4f 2023-01-06 thomas n = snprintf(new_srv->unix_socket_name,
354 bea82c4f 2023-01-06 thomas sizeof(new_srv->unix_socket_name), "%s%s",
355 bea82c4f 2023-01-06 thomas strlen(gotwebd->httpd_chroot) ?
356 bea82c4f 2023-01-06 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $4);
357 bea82c4f 2023-01-06 thomas if (n < 0 ||
358 bea82c4f 2023-01-06 thomas (size_t)n >= sizeof(new_srv->unix_socket_name)) {
359 bea82c4f 2023-01-06 thomas yyerror("%s: unix_socket_name truncated",
360 bea82c4f 2023-01-06 thomas __func__);
361 bea82c4f 2023-01-06 thomas free($4);
364 bea82c4f 2023-01-06 thomas free($4);
366 8a35f56c 2022-07-16 thomas | MAX_REPOS NUMBER {
367 8a35f56c 2022-07-16 thomas if ($2 > 0)
368 8a35f56c 2022-07-16 thomas new_srv->max_repos = $2;
370 8a35f56c 2022-07-16 thomas | SHOW_SITE_OWNER boolean {
371 8a35f56c 2022-07-16 thomas new_srv->show_site_owner = $2;
373 8a35f56c 2022-07-16 thomas | SHOW_REPO_OWNER boolean {
374 8a35f56c 2022-07-16 thomas new_srv->show_repo_owner = $2;
376 8a35f56c 2022-07-16 thomas | SHOW_REPO_AGE boolean {
377 8a35f56c 2022-07-16 thomas new_srv->show_repo_age = $2;
379 8a35f56c 2022-07-16 thomas | SHOW_REPO_DESCRIPTION boolean {
380 8a35f56c 2022-07-16 thomas new_srv->show_repo_description = $2;
382 8a35f56c 2022-07-16 thomas | SHOW_REPO_CLONEURL boolean {
383 8a35f56c 2022-07-16 thomas new_srv->show_repo_cloneurl = $2;
385 3991b2a5 2022-10-31 thomas | RESPECT_EXPORTOK boolean {
386 3991b2a5 2022-10-31 thomas new_srv->respect_exportok = $2;
388 8a35f56c 2022-07-16 thomas | MAX_REPOS_DISPLAY NUMBER {
389 8a35f56c 2022-07-16 thomas new_srv->max_repos_display = $2;
391 8a35f56c 2022-07-16 thomas | MAX_COMMITS_DISPLAY NUMBER {
392 8a35f56c 2022-07-16 thomas if ($2 > 0)
393 8a35f56c 2022-07-16 thomas new_srv->max_commits_display = $2;
397 8a35f56c 2022-07-16 thomas serveropts2 : serveropts2 serveropts1 nl
398 8a35f56c 2022-07-16 thomas | serveropts1 optnl
401 8a35f56c 2022-07-16 thomas nl : '\n' optnl
404 8a35f56c 2022-07-16 thomas optnl : '\n' optnl /* zero or more newlines */
405 8a35f56c 2022-07-16 thomas | /* empty */
410 8a35f56c 2022-07-16 thomas struct keywords {
411 8a35f56c 2022-07-16 thomas const char *k_name;
412 8a35f56c 2022-07-16 thomas int k_val;
416 8a35f56c 2022-07-16 thomas yyerror(const char *fmt, ...)
418 8a35f56c 2022-07-16 thomas va_list ap;
419 8a35f56c 2022-07-16 thomas char *msg;
421 8a35f56c 2022-07-16 thomas file->errors++;
422 8a35f56c 2022-07-16 thomas va_start(ap, fmt);
423 8a35f56c 2022-07-16 thomas if (vasprintf(&msg, fmt, ap) == -1)
424 8a35f56c 2022-07-16 thomas fatalx("yyerror vasprintf");
425 8a35f56c 2022-07-16 thomas va_end(ap);
426 8a35f56c 2022-07-16 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
427 8a35f56c 2022-07-16 thomas free(msg);
428 8a35f56c 2022-07-16 thomas return (0);
432 8a35f56c 2022-07-16 thomas kw_cmp(const void *k, const void *e)
434 8a35f56c 2022-07-16 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
438 8a35f56c 2022-07-16 thomas lookup(char *s)
440 8a35f56c 2022-07-16 thomas /* This has to be sorted always. */
441 8a35f56c 2022-07-16 thomas static const struct keywords keywords[] = {
442 8a35f56c 2022-07-16 thomas { "chroot", CHROOT },
443 8a35f56c 2022-07-16 thomas { "custom_css", CUSTOM_CSS },
444 c24a47af 2022-08-11 thomas { "listen", LISTEN },
445 8a35f56c 2022-07-16 thomas { "logo", LOGO },
446 f0b4ad15 2023-01-02 thomas { "logo_url", LOGO_URL },
447 8a35f56c 2022-07-16 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
448 8a35f56c 2022-07-16 thomas { "max_repos", MAX_REPOS },
449 8a35f56c 2022-07-16 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
450 c24a47af 2022-08-11 thomas { "on", ON },
451 8a35f56c 2022-07-16 thomas { "port", PORT },
452 8a35f56c 2022-07-16 thomas { "prefork", PREFORK },
453 8a35f56c 2022-07-16 thomas { "repos_path", REPOS_PATH },
454 3991b2a5 2022-10-31 thomas { "respect_exportok", RESPECT_EXPORTOK },
455 8a35f56c 2022-07-16 thomas { "server", SERVER },
456 8a35f56c 2022-07-16 thomas { "show_repo_age", SHOW_REPO_AGE },
457 8a35f56c 2022-07-16 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
458 8a35f56c 2022-07-16 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
459 8a35f56c 2022-07-16 thomas { "show_repo_owner", SHOW_REPO_OWNER },
460 8a35f56c 2022-07-16 thomas { "show_site_owner", SHOW_SITE_OWNER },
461 8a35f56c 2022-07-16 thomas { "site_link", SITE_LINK },
462 8a35f56c 2022-07-16 thomas { "site_name", SITE_NAME },
463 8a35f56c 2022-07-16 thomas { "site_owner", SITE_OWNER },
464 bea82c4f 2023-01-06 thomas { "socket", SOCKET },
465 8a35f56c 2022-07-16 thomas { "unix_socket", UNIX_SOCKET },
466 8a35f56c 2022-07-16 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
468 8a35f56c 2022-07-16 thomas const struct keywords *p;
470 8a35f56c 2022-07-16 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
471 8a35f56c 2022-07-16 thomas sizeof(keywords[0]), kw_cmp);
474 8a35f56c 2022-07-16 thomas return (p->k_val);
476 8a35f56c 2022-07-16 thomas return (STRING);
479 8a35f56c 2022-07-16 thomas #define MAXPUSHBACK 128
481 8a35f56c 2022-07-16 thomas unsigned char *parsebuf;
482 8a35f56c 2022-07-16 thomas int parseindex;
483 8a35f56c 2022-07-16 thomas unsigned char pushback_buffer[MAXPUSHBACK];
484 8a35f56c 2022-07-16 thomas int pushback_index = 0;
487 8a35f56c 2022-07-16 thomas lgetc(int quotec)
489 8a35f56c 2022-07-16 thomas int c, next;
491 8a35f56c 2022-07-16 thomas if (parsebuf) {
492 8a35f56c 2022-07-16 thomas /* Read character from the parsebuffer instead of input. */
493 8a35f56c 2022-07-16 thomas if (parseindex >= 0) {
494 8a35f56c 2022-07-16 thomas c = parsebuf[parseindex++];
495 8a35f56c 2022-07-16 thomas if (c != '\0')
496 8a35f56c 2022-07-16 thomas return (c);
497 8a35f56c 2022-07-16 thomas parsebuf = NULL;
499 8a35f56c 2022-07-16 thomas parseindex++;
502 8a35f56c 2022-07-16 thomas if (pushback_index)
503 8a35f56c 2022-07-16 thomas return (pushback_buffer[--pushback_index]);
505 8a35f56c 2022-07-16 thomas if (quotec) {
506 8a35f56c 2022-07-16 thomas c = getc(file->stream);
507 8a35f56c 2022-07-16 thomas if (c == EOF)
508 8a35f56c 2022-07-16 thomas yyerror("reached end of file while parsing "
509 8a35f56c 2022-07-16 thomas "quoted string");
510 8a35f56c 2022-07-16 thomas return (c);
513 8a35f56c 2022-07-16 thomas c = getc(file->stream);
514 8a35f56c 2022-07-16 thomas while (c == '\\') {
515 8a35f56c 2022-07-16 thomas next = getc(file->stream);
516 8a35f56c 2022-07-16 thomas if (next != '\n') {
517 8a35f56c 2022-07-16 thomas c = next;
520 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
521 8a35f56c 2022-07-16 thomas file->lineno++;
522 8a35f56c 2022-07-16 thomas c = getc(file->stream);
525 8a35f56c 2022-07-16 thomas return (c);
529 8a35f56c 2022-07-16 thomas lungetc(int c)
531 8a35f56c 2022-07-16 thomas if (c == EOF)
532 8a35f56c 2022-07-16 thomas return (EOF);
533 8a35f56c 2022-07-16 thomas if (parsebuf) {
534 8a35f56c 2022-07-16 thomas parseindex--;
535 8a35f56c 2022-07-16 thomas if (parseindex >= 0)
536 8a35f56c 2022-07-16 thomas return (c);
538 8a35f56c 2022-07-16 thomas if (pushback_index < MAXPUSHBACK-1)
539 8a35f56c 2022-07-16 thomas return (pushback_buffer[pushback_index++] = c);
541 8a35f56c 2022-07-16 thomas return (EOF);
545 8a35f56c 2022-07-16 thomas findeol(void)
549 8a35f56c 2022-07-16 thomas parsebuf = NULL;
551 8a35f56c 2022-07-16 thomas /* Skip to either EOF or the first real EOL. */
552 8a35f56c 2022-07-16 thomas while (1) {
553 8a35f56c 2022-07-16 thomas if (pushback_index)
554 8a35f56c 2022-07-16 thomas c = pushback_buffer[--pushback_index];
556 8a35f56c 2022-07-16 thomas c = lgetc(0);
557 8a35f56c 2022-07-16 thomas if (c == '\n') {
558 8a35f56c 2022-07-16 thomas file->lineno++;
561 8a35f56c 2022-07-16 thomas if (c == EOF)
564 8a35f56c 2022-07-16 thomas return (ERROR);
568 8a35f56c 2022-07-16 thomas yylex(void)
570 8a35f56c 2022-07-16 thomas unsigned char buf[8096];
571 8a35f56c 2022-07-16 thomas unsigned char *p, *val;
572 8a35f56c 2022-07-16 thomas int quotec, next, c;
573 8a35f56c 2022-07-16 thomas int token;
577 8a35f56c 2022-07-16 thomas c = lgetc(0);
578 8a35f56c 2022-07-16 thomas while (c == ' ' || c == '\t')
579 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
581 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
582 8a35f56c 2022-07-16 thomas if (c == '#') {
583 8a35f56c 2022-07-16 thomas c = lgetc(0);
584 8a35f56c 2022-07-16 thomas while (c != '\n' && c != EOF)
585 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
587 8a35f56c 2022-07-16 thomas if (c == '$' && parsebuf == NULL) {
588 8a35f56c 2022-07-16 thomas while (1) {
589 8a35f56c 2022-07-16 thomas c = lgetc(0);
590 8a35f56c 2022-07-16 thomas if (c == EOF)
591 8a35f56c 2022-07-16 thomas return (0);
593 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
594 8a35f56c 2022-07-16 thomas yyerror("string too long");
595 8a35f56c 2022-07-16 thomas return (findeol());
597 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == '_') {
598 8a35f56c 2022-07-16 thomas *p++ = c;
599 8a35f56c 2022-07-16 thomas continue;
601 8a35f56c 2022-07-16 thomas *p = '\0';
602 8a35f56c 2022-07-16 thomas lungetc(c);
605 8a35f56c 2022-07-16 thomas val = symget(buf);
606 8a35f56c 2022-07-16 thomas if (val == NULL) {
607 8a35f56c 2022-07-16 thomas yyerror("macro '%s' not defined", buf);
608 8a35f56c 2022-07-16 thomas return (findeol());
610 8a35f56c 2022-07-16 thomas parsebuf = val;
611 8a35f56c 2022-07-16 thomas parseindex = 0;
612 8a35f56c 2022-07-16 thomas goto top;
615 8a35f56c 2022-07-16 thomas switch (c) {
616 8a35f56c 2022-07-16 thomas case '\'':
617 8a35f56c 2022-07-16 thomas case '"':
618 8a35f56c 2022-07-16 thomas quotec = c;
619 8a35f56c 2022-07-16 thomas while (1) {
620 8a35f56c 2022-07-16 thomas c = lgetc(quotec);
621 8a35f56c 2022-07-16 thomas if (c == EOF)
622 8a35f56c 2022-07-16 thomas return (0);
623 8a35f56c 2022-07-16 thomas if (c == '\n') {
624 8a35f56c 2022-07-16 thomas file->lineno++;
625 8a35f56c 2022-07-16 thomas continue;
626 8a35f56c 2022-07-16 thomas } else if (c == '\\') {
627 8a35f56c 2022-07-16 thomas next = lgetc(quotec);
628 8a35f56c 2022-07-16 thomas if (next == EOF)
629 8a35f56c 2022-07-16 thomas return (0);
630 8a35f56c 2022-07-16 thomas if (next == quotec || c == ' ' || c == '\t')
631 8a35f56c 2022-07-16 thomas c = next;
632 8a35f56c 2022-07-16 thomas else if (next == '\n') {
633 8a35f56c 2022-07-16 thomas file->lineno++;
634 8a35f56c 2022-07-16 thomas continue;
636 8a35f56c 2022-07-16 thomas lungetc(next);
637 8a35f56c 2022-07-16 thomas } else if (c == quotec) {
638 8a35f56c 2022-07-16 thomas *p = '\0';
640 8a35f56c 2022-07-16 thomas } else if (c == '\0') {
641 8a35f56c 2022-07-16 thomas yyerror("syntax error");
642 8a35f56c 2022-07-16 thomas return (findeol());
644 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
645 8a35f56c 2022-07-16 thomas yyerror("string too long");
646 8a35f56c 2022-07-16 thomas return (findeol());
648 8a35f56c 2022-07-16 thomas *p++ = c;
650 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
651 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
652 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
653 8a35f56c 2022-07-16 thomas return (STRING);
656 8a35f56c 2022-07-16 thomas #define allowed_to_end_number(x) \
657 8a35f56c 2022-07-16 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
659 8a35f56c 2022-07-16 thomas if (c == '-' || isdigit(c)) {
661 8a35f56c 2022-07-16 thomas *p++ = c;
662 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
663 8a35f56c 2022-07-16 thomas yyerror("string too long");
664 8a35f56c 2022-07-16 thomas return (findeol());
666 8a35f56c 2022-07-16 thomas c = lgetc(0);
667 8a35f56c 2022-07-16 thomas } while (c != EOF && isdigit(c));
668 8a35f56c 2022-07-16 thomas lungetc(c);
669 8a35f56c 2022-07-16 thomas if (p == buf + 1 && buf[0] == '-')
670 8a35f56c 2022-07-16 thomas goto nodigits;
671 8a35f56c 2022-07-16 thomas if (c == EOF || allowed_to_end_number(c)) {
672 8a35f56c 2022-07-16 thomas const char *errstr = NULL;
674 8a35f56c 2022-07-16 thomas *p = '\0';
675 8a35f56c 2022-07-16 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
676 8a35f56c 2022-07-16 thomas LLONG_MAX, &errstr);
677 8a35f56c 2022-07-16 thomas if (errstr) {
678 8a35f56c 2022-07-16 thomas yyerror("\"%s\" invalid number: %s",
679 8a35f56c 2022-07-16 thomas buf, errstr);
680 8a35f56c 2022-07-16 thomas return (findeol());
682 8a35f56c 2022-07-16 thomas return (NUMBER);
684 8a35f56c 2022-07-16 thomas nodigits:
685 8a35f56c 2022-07-16 thomas while (p > buf + 1)
686 8a35f56c 2022-07-16 thomas lungetc(*--p);
687 8a35f56c 2022-07-16 thomas c = *--p;
688 8a35f56c 2022-07-16 thomas if (c == '-')
689 8a35f56c 2022-07-16 thomas return (c);
693 8a35f56c 2022-07-16 thomas #define allowed_in_string(x) \
694 8a35f56c 2022-07-16 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
695 8a35f56c 2022-07-16 thomas x != '{' && x != '}' && \
696 8a35f56c 2022-07-16 thomas x != '!' && x != '=' && x != '#' && \
697 8a35f56c 2022-07-16 thomas x != ','))
699 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == ':' || c == '_') {
701 8a35f56c 2022-07-16 thomas *p++ = c;
702 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
703 8a35f56c 2022-07-16 thomas yyerror("string too long");
704 8a35f56c 2022-07-16 thomas return (findeol());
706 8a35f56c 2022-07-16 thomas c = lgetc(0);
707 8a35f56c 2022-07-16 thomas } while (c != EOF && (allowed_in_string(c)));
708 8a35f56c 2022-07-16 thomas lungetc(c);
709 8a35f56c 2022-07-16 thomas *p = '\0';
710 8a35f56c 2022-07-16 thomas token = lookup(buf);
711 8a35f56c 2022-07-16 thomas if (token == STRING) {
712 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
713 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
714 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
716 8a35f56c 2022-07-16 thomas return (token);
718 8a35f56c 2022-07-16 thomas if (c == '\n') {
719 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
720 8a35f56c 2022-07-16 thomas file->lineno++;
722 8a35f56c 2022-07-16 thomas if (c == EOF)
723 8a35f56c 2022-07-16 thomas return (0);
724 8a35f56c 2022-07-16 thomas return (c);
728 8a35f56c 2022-07-16 thomas check_file_secrecy(int fd, const char *fname)
730 8a35f56c 2022-07-16 thomas struct stat st;
732 8a35f56c 2022-07-16 thomas if (fstat(fd, &st)) {
733 8a35f56c 2022-07-16 thomas log_warn("cannot stat %s", fname);
734 8a35f56c 2022-07-16 thomas return (-1);
736 8a35f56c 2022-07-16 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
737 8a35f56c 2022-07-16 thomas log_warnx("%s: owner not root or current user", fname);
738 8a35f56c 2022-07-16 thomas return (-1);
740 8a35f56c 2022-07-16 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
741 8a35f56c 2022-07-16 thomas log_warnx("%s: group writable or world read/writable", fname);
742 8a35f56c 2022-07-16 thomas return (-1);
744 8a35f56c 2022-07-16 thomas return (0);
747 8a35f56c 2022-07-16 thomas struct file *
748 8a35f56c 2022-07-16 thomas newfile(const char *name, int secret)
750 8a35f56c 2022-07-16 thomas struct file *nfile;
752 8a35f56c 2022-07-16 thomas nfile = calloc(1, sizeof(struct file));
753 8a35f56c 2022-07-16 thomas if (nfile == NULL) {
754 8a35f56c 2022-07-16 thomas log_warn("calloc");
755 8a35f56c 2022-07-16 thomas return (NULL);
757 8a35f56c 2022-07-16 thomas nfile->name = strdup(name);
758 8a35f56c 2022-07-16 thomas if (nfile->name == NULL) {
759 8a35f56c 2022-07-16 thomas log_warn("strdup");
760 8a35f56c 2022-07-16 thomas free(nfile);
761 8a35f56c 2022-07-16 thomas return (NULL);
763 8a35f56c 2022-07-16 thomas nfile->stream = fopen(nfile->name, "r");
764 8a35f56c 2022-07-16 thomas if (nfile->stream == NULL) {
765 8a35f56c 2022-07-16 thomas /* no warning, we don't require a conf file */
766 8a35f56c 2022-07-16 thomas free(nfile->name);
767 8a35f56c 2022-07-16 thomas free(nfile);
768 8a35f56c 2022-07-16 thomas return (NULL);
769 8a35f56c 2022-07-16 thomas } else if (secret &&
770 8a35f56c 2022-07-16 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
771 8a35f56c 2022-07-16 thomas fclose(nfile->stream);
772 8a35f56c 2022-07-16 thomas free(nfile->name);
773 8a35f56c 2022-07-16 thomas free(nfile);
774 8a35f56c 2022-07-16 thomas return (NULL);
776 8a35f56c 2022-07-16 thomas nfile->lineno = 1;
777 8a35f56c 2022-07-16 thomas return (nfile);
780 8a35f56c 2022-07-16 thomas static void
781 8a35f56c 2022-07-16 thomas closefile(struct file *xfile)
783 8a35f56c 2022-07-16 thomas fclose(xfile->stream);
784 8a35f56c 2022-07-16 thomas free(xfile->name);
785 8a35f56c 2022-07-16 thomas free(xfile);
788 9f849004 2022-08-06 thomas static void
789 9f849004 2022-08-06 thomas add_default_server(void)
791 9f849004 2022-08-06 thomas new_srv = conf_new_server(D_SITENAME);
792 9f849004 2022-08-06 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
796 8a35f56c 2022-07-16 thomas parse_config(const char *filename, struct gotwebd *env)
798 8a35f56c 2022-07-16 thomas struct sym *sym, *next;
800 8a35f56c 2022-07-16 thomas if (config_init(env) == -1)
801 8a35f56c 2022-07-16 thomas fatalx("failed to initialize configuration");
803 8a35f56c 2022-07-16 thomas gotwebd = env;
805 9f849004 2022-08-06 thomas file = newfile(filename, 0);
806 9f849004 2022-08-06 thomas if (file == NULL) {
807 9f849004 2022-08-06 thomas add_default_server();
808 9f849004 2022-08-06 thomas sockets_parse_sockets(env);
809 9f849004 2022-08-06 thomas /* just return, as we don't require a conf file */
810 9f849004 2022-08-06 thomas return (0);
813 8a35f56c 2022-07-16 thomas yyparse();
814 8a35f56c 2022-07-16 thomas errors = file->errors;
815 8a35f56c 2022-07-16 thomas closefile(file);
817 8a35f56c 2022-07-16 thomas /* Free macros and check which have not been used. */
818 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
819 8a35f56c 2022-07-16 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
820 8a35f56c 2022-07-16 thomas fprintf(stderr, "warning: macro '%s' not used\n",
821 8a35f56c 2022-07-16 thomas sym->nam);
822 8a35f56c 2022-07-16 thomas if (!sym->persist) {
823 8a35f56c 2022-07-16 thomas free(sym->nam);
824 8a35f56c 2022-07-16 thomas free(sym->val);
825 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
826 8a35f56c 2022-07-16 thomas free(sym);
830 8a35f56c 2022-07-16 thomas if (errors)
831 8a35f56c 2022-07-16 thomas return (-1);
833 8a35f56c 2022-07-16 thomas /* just add default server if no config specified */
834 9f849004 2022-08-06 thomas if (gotwebd->server_cnt == 0)
835 9f849004 2022-08-06 thomas add_default_server();
837 8a35f56c 2022-07-16 thomas /* setup our listening sockets */
838 8a35f56c 2022-07-16 thomas sockets_parse_sockets(env);
840 8a35f56c 2022-07-16 thomas return (0);
843 8a35f56c 2022-07-16 thomas struct server *
844 8a35f56c 2022-07-16 thomas conf_new_server(const char *name)
846 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
848 8a35f56c 2022-07-16 thomas srv = calloc(1, sizeof(*srv));
849 8a35f56c 2022-07-16 thomas if (srv == NULL)
850 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
852 8a35f56c 2022-07-16 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
853 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->name))
854 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
855 8a35f56c 2022-07-16 thomas n = snprintf(srv->unix_socket_name,
856 8a35f56c 2022-07-16 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
857 8a35f56c 2022-07-16 thomas D_UNIX_SOCKET);
858 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
859 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
860 8a35f56c 2022-07-16 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
861 8a35f56c 2022-07-16 thomas sizeof(srv->repos_path));
862 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->repos_path))
863 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
864 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_name, D_SITENAME,
865 8a35f56c 2022-07-16 thomas sizeof(srv->site_name));
866 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_name))
867 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
868 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
869 8a35f56c 2022-07-16 thomas sizeof(srv->site_owner));
870 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_owner))
871 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
872 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_link, D_SITELINK,
873 8a35f56c 2022-07-16 thomas sizeof(srv->site_link));
874 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_link))
875 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
876 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo, D_GOTLOGO,
877 8a35f56c 2022-07-16 thomas sizeof(srv->logo));
878 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo))
879 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
880 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
881 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo_url))
882 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
883 8a35f56c 2022-07-16 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
884 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->custom_css))
885 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
887 8a35f56c 2022-07-16 thomas srv->show_site_owner = D_SHOWSOWNER;
888 8a35f56c 2022-07-16 thomas srv->show_repo_owner = D_SHOWROWNER;
889 8a35f56c 2022-07-16 thomas srv->show_repo_age = D_SHOWAGE;
890 8a35f56c 2022-07-16 thomas srv->show_repo_description = D_SHOWDESC;
891 8a35f56c 2022-07-16 thomas srv->show_repo_cloneurl = D_SHOWURL;
892 3991b2a5 2022-10-31 thomas srv->respect_exportok = D_RESPECTEXPORTOK;
894 8a35f56c 2022-07-16 thomas srv->max_repos_display = D_MAXREPODISP;
895 8a35f56c 2022-07-16 thomas srv->max_commits_display = D_MAXCOMMITDISP;
896 8a35f56c 2022-07-16 thomas srv->max_repos = D_MAXREPO;
898 8a35f56c 2022-07-16 thomas srv->unix_socket = 1;
899 e4317279 2022-08-30 thomas srv->fcgi_socket = 0;
901 62f85214 2022-08-16 thomas TAILQ_INIT(&srv->al);
902 90d63d47 2022-08-16 thomas TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
903 8a35f56c 2022-07-16 thomas gotwebd->server_cnt++;
905 8a35f56c 2022-07-16 thomas return srv;
909 8a35f56c 2022-07-16 thomas symset(const char *nam, const char *val, int persist)
911 8a35f56c 2022-07-16 thomas struct sym *sym;
913 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
914 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0)
918 8a35f56c 2022-07-16 thomas if (sym != NULL) {
919 8a35f56c 2022-07-16 thomas if (sym->persist == 1)
920 8a35f56c 2022-07-16 thomas return (0);
922 8a35f56c 2022-07-16 thomas free(sym->nam);
923 8a35f56c 2022-07-16 thomas free(sym->val);
924 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
925 8a35f56c 2022-07-16 thomas free(sym);
928 8a35f56c 2022-07-16 thomas sym = calloc(1, sizeof(*sym));
929 8a35f56c 2022-07-16 thomas if (sym == NULL)
930 8a35f56c 2022-07-16 thomas return (-1);
932 8a35f56c 2022-07-16 thomas sym->nam = strdup(nam);
933 8a35f56c 2022-07-16 thomas if (sym->nam == NULL) {
934 8a35f56c 2022-07-16 thomas free(sym);
935 8a35f56c 2022-07-16 thomas return (-1);
937 8a35f56c 2022-07-16 thomas sym->val = strdup(val);
938 8a35f56c 2022-07-16 thomas if (sym->val == NULL) {
939 8a35f56c 2022-07-16 thomas free(sym->nam);
940 8a35f56c 2022-07-16 thomas free(sym);
941 8a35f56c 2022-07-16 thomas return (-1);
943 8a35f56c 2022-07-16 thomas sym->used = 0;
944 8a35f56c 2022-07-16 thomas sym->persist = persist;
945 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
946 8a35f56c 2022-07-16 thomas return (0);
950 8a35f56c 2022-07-16 thomas cmdline_symset(char *s)
952 8a35f56c 2022-07-16 thomas char *sym, *val;
955 8a35f56c 2022-07-16 thomas val = strrchr(s, '=');
956 8a35f56c 2022-07-16 thomas if (val == NULL)
957 8a35f56c 2022-07-16 thomas return (-1);
959 43be1edb 2022-09-05 thomas sym = strndup(s, val - s);
960 8a35f56c 2022-07-16 thomas if (sym == NULL)
961 43be1edb 2022-09-05 thomas fatal("%s: strndup", __func__);
963 8a35f56c 2022-07-16 thomas ret = symset(sym, val + 1, 1);
964 8a35f56c 2022-07-16 thomas free(sym);
966 8a35f56c 2022-07-16 thomas return (ret);
970 8a35f56c 2022-07-16 thomas symget(const char *nam)
972 8a35f56c 2022-07-16 thomas struct sym *sym;
974 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
975 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0) {
976 8a35f56c 2022-07-16 thomas sym->used = 1;
977 8a35f56c 2022-07-16 thomas return (sym->val);
980 8a35f56c 2022-07-16 thomas return (NULL);
984 8a35f56c 2022-07-16 thomas getservice(const char *n)
986 8a35f56c 2022-07-16 thomas struct servent *s;
987 8a35f56c 2022-07-16 thomas const char *errstr;
988 8a35f56c 2022-07-16 thomas long long llval;
990 8a35f56c 2022-07-16 thomas llval = strtonum(n, 0, UINT16_MAX, &errstr);
991 8a35f56c 2022-07-16 thomas if (errstr) {
992 8a35f56c 2022-07-16 thomas s = getservbyname(n, "tcp");
993 8a35f56c 2022-07-16 thomas if (s == NULL)
994 8a35f56c 2022-07-16 thomas s = getservbyname(n, "udp");
995 8a35f56c 2022-07-16 thomas if (s == NULL)
996 8a35f56c 2022-07-16 thomas return (-1);
997 c19738c9 2022-08-27 thomas return ntohs(s->s_port);
1000 c19738c9 2022-08-27 thomas return (unsigned short)llval;
1003 8a35f56c 2022-07-16 thomas struct address *
1004 8a35f56c 2022-07-16 thomas host_v4(const char *s)
1006 8a35f56c 2022-07-16 thomas struct in_addr ina;
1007 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1008 8a35f56c 2022-07-16 thomas struct address *h;
1010 8a35f56c 2022-07-16 thomas memset(&ina, 0, sizeof(ina));
1011 8a35f56c 2022-07-16 thomas if (inet_pton(AF_INET, s, &ina) != 1)
1012 8a35f56c 2022-07-16 thomas return (NULL);
1014 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1015 8a35f56c 2022-07-16 thomas fatal(__func__);
1016 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1017 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ina);
1018 8a35f56c 2022-07-16 thomas if (sain->sin_addr.s_addr == INADDR_ANY)
1019 8a35f56c 2022-07-16 thomas h->prefixlen = 0; /* 0.0.0.0 address */
1021 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1022 8a35f56c 2022-07-16 thomas return (h);
1025 8a35f56c 2022-07-16 thomas struct address *
1026 8a35f56c 2022-07-16 thomas host_v6(const char *s)
1028 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res;
1029 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *sa_in6, *ra;
1030 8a35f56c 2022-07-16 thomas struct address *h = NULL;
1032 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1033 8a35f56c 2022-07-16 thomas hints.ai_family = AF_INET6;
1034 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_DGRAM; /* dummy */
1035 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_NUMERICHOST;
1036 8a35f56c 2022-07-16 thomas if (getaddrinfo(s, "0", &hints, &res) == 0) {
1037 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1038 8a35f56c 2022-07-16 thomas fatal(__func__);
1039 8a35f56c 2022-07-16 thomas sa_in6 = (struct sockaddr_in6 *)&h->ss;
1040 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)res->ai_addr;
1041 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1042 cdbe1d7d 2022-08-06 thomas ra->sin6_scope_id);
1043 8a35f56c 2022-07-16 thomas if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1044 8a35f56c 2022-07-16 thomas sizeof(sa_in6->sin6_addr)) == 0)
1045 8a35f56c 2022-07-16 thomas h->prefixlen = 0; /* any address */
1047 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1048 8a35f56c 2022-07-16 thomas freeaddrinfo(res);
1051 8a35f56c 2022-07-16 thomas return (h);
1055 e4c7e0b0 2022-08-30 thomas host_dns(const char *s, struct server *new_srv, int max,
1056 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1058 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res0, *res;
1059 8a35f56c 2022-07-16 thomas int error, cnt = 0;
1060 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1061 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1062 8a35f56c 2022-07-16 thomas struct address *h;
1064 e4c7e0b0 2022-08-30 thomas if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1065 8a35f56c 2022-07-16 thomas return (cnt);
1067 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1068 8a35f56c 2022-07-16 thomas hints.ai_family = PF_UNSPEC;
1069 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1070 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_ADDRCONFIG;
1071 8a35f56c 2022-07-16 thomas error = getaddrinfo(s, NULL, &hints, &res0);
1072 8a35f56c 2022-07-16 thomas if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1073 8a35f56c 2022-07-16 thomas return (0);
1074 8a35f56c 2022-07-16 thomas if (error) {
1075 8a35f56c 2022-07-16 thomas log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1076 8a35f56c 2022-07-16 thomas gai_strerror(error));
1077 8a35f56c 2022-07-16 thomas return (-1);
1080 8a35f56c 2022-07-16 thomas for (res = res0; res && cnt < max; res = res->ai_next) {
1081 8a35f56c 2022-07-16 thomas if (res->ai_family != AF_INET &&
1082 8a35f56c 2022-07-16 thomas res->ai_family != AF_INET6)
1083 8a35f56c 2022-07-16 thomas continue;
1084 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1085 8a35f56c 2022-07-16 thomas fatal(__func__);
1087 8a35f56c 2022-07-16 thomas if (port)
1088 8a35f56c 2022-07-16 thomas h->port = port;
1089 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1090 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1091 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1092 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1093 8a35f56c 2022-07-16 thomas __func__);
1094 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1095 8a35f56c 2022-07-16 thomas free(h);
1096 8a35f56c 2022-07-16 thomas return (-1);
1099 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1100 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1101 8a35f56c 2022-07-16 thomas h->ss.ss_family = res->ai_family;
1102 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1104 8a35f56c 2022-07-16 thomas if (res->ai_family == AF_INET) {
1105 cdbe1d7d 2022-08-06 thomas struct sockaddr_in *ra;
1106 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1107 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in *)res->ai_addr;
1108 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ra->sin_addr);
1109 8a35f56c 2022-07-16 thomas } else {
1110 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *ra;
1111 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1112 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)res->ai_addr;
1113 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1116 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1117 e4c7e0b0 2022-08-30 thomas return -1;
1120 8a35f56c 2022-07-16 thomas if (cnt == max && res) {
1121 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1122 8a35f56c 2022-07-16 thomas s, max);
1124 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1125 8a35f56c 2022-07-16 thomas return (cnt);
1129 e4c7e0b0 2022-08-30 thomas host_if(const char *s, struct server *new_srv, int max,
1130 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1132 8a35f56c 2022-07-16 thomas struct ifaddrs *ifap, *p;
1133 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1134 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1135 8a35f56c 2022-07-16 thomas struct address *h;
1136 8a35f56c 2022-07-16 thomas int cnt = 0, af;
1138 8a35f56c 2022-07-16 thomas if (getifaddrs(&ifap) == -1)
1139 8a35f56c 2022-07-16 thomas fatal("getifaddrs");
1141 8a35f56c 2022-07-16 thomas /* First search for IPv4 addresses */
1142 8a35f56c 2022-07-16 thomas af = AF_INET;
1145 8a35f56c 2022-07-16 thomas for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1146 8a35f56c 2022-07-16 thomas if (p->ifa_addr == NULL ||
1147 8a35f56c 2022-07-16 thomas p->ifa_addr->sa_family != af ||
1148 8a35f56c 2022-07-16 thomas (strcmp(s, p->ifa_name) != 0 &&
1149 8a35f56c 2022-07-16 thomas !is_if_in_group(p->ifa_name, s)))
1150 8a35f56c 2022-07-16 thomas continue;
1151 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1152 8a35f56c 2022-07-16 thomas fatal("calloc");
1154 8a35f56c 2022-07-16 thomas if (port)
1155 8a35f56c 2022-07-16 thomas h->port = port;
1156 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1157 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1158 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1159 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1160 8a35f56c 2022-07-16 thomas __func__);
1161 8a35f56c 2022-07-16 thomas free(h);
1162 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1163 8a35f56c 2022-07-16 thomas return (-1);
1166 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1167 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1168 8a35f56c 2022-07-16 thomas h->ss.ss_family = af;
1169 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1171 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1172 cdbe1d7d 2022-08-06 thomas struct sockaddr_in *ra;
1173 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1174 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in *)p->ifa_addr;
1175 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet_init(sain, &ra->sin_addr);
1176 8a35f56c 2022-07-16 thomas } else {
1177 cdbe1d7d 2022-08-06 thomas struct sockaddr_in6 *ra;
1178 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1179 cdbe1d7d 2022-08-06 thomas ra = (struct sockaddr_in6 *)p->ifa_addr;
1180 cdbe1d7d 2022-08-06 thomas got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1181 cdbe1d7d 2022-08-06 thomas ra->sin6_scope_id);
1184 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1185 e4c7e0b0 2022-08-30 thomas return -1;
1188 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1189 8a35f56c 2022-07-16 thomas /* Next search for IPv6 addresses */
1190 8a35f56c 2022-07-16 thomas af = AF_INET6;
1191 8a35f56c 2022-07-16 thomas goto nextaf;
1194 8a35f56c 2022-07-16 thomas if (cnt > max) {
1195 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1196 8a35f56c 2022-07-16 thomas s, max);
1198 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1199 8a35f56c 2022-07-16 thomas return (cnt);
1203 e4c7e0b0 2022-08-30 thomas host(const char *s, struct server *new_srv, int max,
1204 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1206 8a35f56c 2022-07-16 thomas struct address *h;
1208 8a35f56c 2022-07-16 thomas h = host_v4(s);
1210 8a35f56c 2022-07-16 thomas /* IPv6 address? */
1211 8a35f56c 2022-07-16 thomas if (h == NULL)
1212 8a35f56c 2022-07-16 thomas h = host_v6(s);
1214 8a35f56c 2022-07-16 thomas if (h != NULL) {
1215 8a35f56c 2022-07-16 thomas if (port)
1216 8a35f56c 2022-07-16 thomas h->port = port;
1217 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1218 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1219 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1220 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1221 8a35f56c 2022-07-16 thomas __func__);
1222 8a35f56c 2022-07-16 thomas free(h);
1223 8a35f56c 2022-07-16 thomas return (-1);
1226 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1227 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1229 e4c7e0b0 2022-08-30 thomas if (add_addr(new_srv, h))
1230 e4c7e0b0 2022-08-30 thomas return -1;
1231 8a35f56c 2022-07-16 thomas return (1);
1234 e4c7e0b0 2022-08-30 thomas return (host_dns(s, new_srv, max, port, ifname, ipproto));
1238 8a35f56c 2022-07-16 thomas is_if_in_group(const char *ifname, const char *groupname)
1240 ff36aeea 2022-07-16 thomas /* TA: Check this... */
1241 ff36aeea 2022-07-16 thomas #ifdef HAVE_STRUCT_IFGROUPREQ
1242 8a35f56c 2022-07-16 thomas unsigned int len;
1243 8a35f56c 2022-07-16 thomas struct ifgroupreq ifgr;
1244 8a35f56c 2022-07-16 thomas struct ifg_req *ifg;
1246 8a35f56c 2022-07-16 thomas int ret = 0;
1248 8a35f56c 2022-07-16 thomas if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1249 8a35f56c 2022-07-16 thomas err(1, "socket");
1251 8a35f56c 2022-07-16 thomas memset(&ifgr, 0, sizeof(ifgr));
1252 8a35f56c 2022-07-16 thomas if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1253 8a35f56c 2022-07-16 thomas err(1, "IFNAMSIZ");
1254 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1255 8a35f56c 2022-07-16 thomas if (errno == EINVAL || errno == ENOTTY)
1256 8a35f56c 2022-07-16 thomas goto end;
1257 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1260 8a35f56c 2022-07-16 thomas len = ifgr.ifgr_len;
1261 8a35f56c 2022-07-16 thomas ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1262 8a35f56c 2022-07-16 thomas sizeof(struct ifg_req));
1263 8a35f56c 2022-07-16 thomas if (ifgr.ifgr_groups == NULL)
1264 8a35f56c 2022-07-16 thomas err(1, "getifgroups");
1265 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1266 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1268 8a35f56c 2022-07-16 thomas ifg = ifgr.ifgr_groups;
1269 8a35f56c 2022-07-16 thomas for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1270 8a35f56c 2022-07-16 thomas len -= sizeof(struct ifg_req);
1271 8a35f56c 2022-07-16 thomas if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1272 8a35f56c 2022-07-16 thomas ret = 1;
1276 8a35f56c 2022-07-16 thomas free(ifgr.ifgr_groups);
1279 8a35f56c 2022-07-16 thomas close(s);
1280 8a35f56c 2022-07-16 thomas return (ret);
1282 ff36aeea 2022-07-16 thomas return (0);
1287 e4c7e0b0 2022-08-30 thomas get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1289 8a35f56c 2022-07-16 thomas if (strcmp("", addr) == 0) {
1290 e4c7e0b0 2022-08-30 thomas if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1291 e4c7e0b0 2022-08-30 thomas -1) <= 0) {
1292 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s",
1293 a90e3117 2022-08-27 thomas "127.0.0.1");
1294 8a35f56c 2022-07-16 thomas return (-1);
1296 e4c7e0b0 2022-08-30 thomas if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1297 a90e3117 2022-08-27 thomas yyerror("invalid listen ip: %s", "::1");
1298 8a35f56c 2022-07-16 thomas return (-1);
1300 8a35f56c 2022-07-16 thomas } else {
1301 e4c7e0b0 2022-08-30 thomas if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1302 8a35f56c 2022-07-16 thomas -1) <= 0) {
1303 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s", addr);
1304 8a35f56c 2022-07-16 thomas return (-1);
1307 8a35f56c 2022-07-16 thomas return (0);
1311 e4c7e0b0 2022-08-30 thomas addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1312 e4c7e0b0 2022-08-30 thomas const char *other_srv)
1314 e4c7e0b0 2022-08-30 thomas struct address *a;
1315 e4c7e0b0 2022-08-30 thomas void *ia;
1316 e4c7e0b0 2022-08-30 thomas char buf[INET6_ADDRSTRLEN];
1317 e4c7e0b0 2022-08-30 thomas const char *addrstr;
1319 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(a, al, entry) {
1320 e4c7e0b0 2022-08-30 thomas if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1321 e4c7e0b0 2022-08-30 thomas a->port != h->port)
1322 e4c7e0b0 2022-08-30 thomas continue;
1324 e4c7e0b0 2022-08-30 thomas switch (h->ss.ss_family) {
1325 e4c7e0b0 2022-08-30 thomas case AF_INET:
1326 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1328 e4c7e0b0 2022-08-30 thomas case AF_INET6:
1329 e4c7e0b0 2022-08-30 thomas ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1331 e4c7e0b0 2022-08-30 thomas default:
1332 e4c7e0b0 2022-08-30 thomas yyerror("unknown address family: %d", h->ss.ss_family);
1333 e4c7e0b0 2022-08-30 thomas return -1;
1335 e4c7e0b0 2022-08-30 thomas addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1336 e4c7e0b0 2022-08-30 thomas if (addrstr) {
1337 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1338 e4c7e0b0 2022-08-30 thomas yyerror("server %s: duplicate fcgi listen "
1339 e4c7e0b0 2022-08-30 thomas "address %s:%d, already used by server %s",
1340 e4c7e0b0 2022-08-30 thomas new_srv, addrstr, h->port, other_srv);
1341 e4c7e0b0 2022-08-30 thomas } else {
1342 e4c7e0b0 2022-08-30 thomas log_warnx("server: %s: duplicate fcgi listen "
1343 e4c7e0b0 2022-08-30 thomas "address %s:%d", new_srv, addrstr, h->port);
1345 e4c7e0b0 2022-08-30 thomas } else {
1346 e4c7e0b0 2022-08-30 thomas if (other_srv) {
1347 e4c7e0b0 2022-08-30 thomas yyerror("server: %s: duplicate fcgi listen "
1348 e4c7e0b0 2022-08-30 thomas "address, already used by server %s",
1349 e4c7e0b0 2022-08-30 thomas new_srv, other_srv);
1350 e4c7e0b0 2022-08-30 thomas } else {
1351 e4c7e0b0 2022-08-30 thomas log_warnx("server %s: duplicate fcgi listen "
1352 e4c7e0b0 2022-08-30 thomas "address", new_srv);
1356 e4c7e0b0 2022-08-30 thomas return -1;
1359 e4c7e0b0 2022-08-30 thomas return 0;
1363 e4c7e0b0 2022-08-30 thomas add_addr(struct server *new_srv, struct address *h)
1365 e4c7e0b0 2022-08-30 thomas struct server *srv;
1367 e4c7e0b0 2022-08-30 thomas /* Address cannot be shared between different servers. */
1368 e4c7e0b0 2022-08-30 thomas TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1369 e4c7e0b0 2022-08-30 thomas if (srv == new_srv)
1370 e4c7e0b0 2022-08-30 thomas continue;
1371 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1372 e4c7e0b0 2022-08-30 thomas return -1;
1375 e4c7e0b0 2022-08-30 thomas /* Tolerate duplicate address lines within the scope of a server. */
1376 e4c7e0b0 2022-08-30 thomas if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1377 e4c7e0b0 2022-08-30 thomas TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1379 e4c7e0b0 2022-08-30 thomas free(h);
1381 e4c7e0b0 2022-08-30 thomas return 0;