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 8a35f56c 2022-07-16 thomas #include <sys/ioctl.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
26 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
27 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
29 8a35f56c 2022-07-16 thomas #include <net/if.h>
30 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
32 8a35f56c 2022-07-16 thomas #include <arpa/inet.h>
34 8a35f56c 2022-07-16 thomas #include <ctype.h>
35 8a35f56c 2022-07-16 thomas #include <err.h>
36 8a35f56c 2022-07-16 thomas #include <errno.h>
37 8a35f56c 2022-07-16 thomas #include <event.h>
38 8a35f56c 2022-07-16 thomas #include <ifaddrs.h>
39 8a35f56c 2022-07-16 thomas #include <limits.h>
40 8a35f56c 2022-07-16 thomas #include <netdb.h>
41 8a35f56c 2022-07-16 thomas #include <stdarg.h>
42 8a35f56c 2022-07-16 thomas #include <stdlib.h>
43 8a35f56c 2022-07-16 thomas #include <stdio.h>
44 8a35f56c 2022-07-16 thomas #include <string.h>
45 8a35f56c 2022-07-16 thomas #include <syslog.h>
46 8a35f56c 2022-07-16 thomas #include <unistd.h>
48 8a35f56c 2022-07-16 thomas #include "proc.h"
49 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
50 ff36aeea 2022-07-16 thomas #include "got_compat.h"
52 8a35f56c 2022-07-16 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 8a35f56c 2022-07-16 thomas static struct file {
54 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(file) entry;
55 8a35f56c 2022-07-16 thomas FILE *stream;
56 8a35f56c 2022-07-16 thomas char *name;
57 8a35f56c 2022-07-16 thomas int lineno;
58 8a35f56c 2022-07-16 thomas int errors;
60 8a35f56c 2022-07-16 thomas struct file *newfile(const char *, int);
61 8a35f56c 2022-07-16 thomas static void closefile(struct file *);
62 8a35f56c 2022-07-16 thomas int check_file_secrecy(int, const char *);
63 8a35f56c 2022-07-16 thomas int yyparse(void);
64 8a35f56c 2022-07-16 thomas int yylex(void);
65 8a35f56c 2022-07-16 thomas int yyerror(const char *, ...)
66 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)))
67 8a35f56c 2022-07-16 thomas __attribute__((__nonnull__ (1)));
68 8a35f56c 2022-07-16 thomas int kw_cmp(const void *, const void *);
69 8a35f56c 2022-07-16 thomas int lookup(char *);
70 8a35f56c 2022-07-16 thomas int lgetc(int);
71 8a35f56c 2022-07-16 thomas int lungetc(int);
72 8a35f56c 2022-07-16 thomas int findeol(void);
74 8a35f56c 2022-07-16 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 8a35f56c 2022-07-16 thomas struct sym {
76 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(sym) entry;
78 8a35f56c 2022-07-16 thomas int persist;
79 8a35f56c 2022-07-16 thomas char *nam;
80 8a35f56c 2022-07-16 thomas char *val;
83 8a35f56c 2022-07-16 thomas int symset(const char *, const char *, int);
84 8a35f56c 2022-07-16 thomas char *symget(const char *);
86 8a35f56c 2022-07-16 thomas static int errors;
88 8a35f56c 2022-07-16 thomas static struct gotwebd *gotwebd;
89 8a35f56c 2022-07-16 thomas static struct server *new_srv;
90 8a35f56c 2022-07-16 thomas static struct server *conf_new_server(const char *);
91 8a35f56c 2022-07-16 thomas int getservice(const char *);
94 8a35f56c 2022-07-16 thomas int get_addrs(const char *, struct addresslist *, in_port_t);
95 8a35f56c 2022-07-16 thomas struct address *host_v4(const char *);
96 8a35f56c 2022-07-16 thomas struct address *host_v6(const char *);
97 8a35f56c 2022-07-16 thomas int host_dns(const char *, struct addresslist *,
98 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
99 8a35f56c 2022-07-16 thomas int host_if(const char *, struct addresslist *,
100 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
101 8a35f56c 2022-07-16 thomas int host(const char *, struct addresslist *,
102 8a35f56c 2022-07-16 thomas int, in_port_t, const char *, int);
103 8a35f56c 2022-07-16 thomas int is_if_in_group(const char *, const char *);
105 8a35f56c 2022-07-16 thomas typedef struct {
107 8a35f56c 2022-07-16 thomas long long number;
108 8a35f56c 2022-07-16 thomas char *string;
109 8a35f56c 2022-07-16 thomas in_port_t port;
111 8a35f56c 2022-07-16 thomas int lineno;
112 8a35f56c 2022-07-16 thomas } YYSTYPE;
116 8a35f56c 2022-07-16 thomas %token BIND INTERFACE WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
117 8a35f56c 2022-07-16 thomas %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
118 8a35f56c 2022-07-16 thomas %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
119 8a35f56c 2022-07-16 thomas %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK FCGI_SOCKET
120 8a35f56c 2022-07-16 thomas %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
122 8a35f56c 2022-07-16 thomas %token <v.string> STRING
123 8a35f56c 2022-07-16 thomas %type <v.port> fcgiport
124 8a35f56c 2022-07-16 thomas %token <v.number> NUMBER
125 8a35f56c 2022-07-16 thomas %type <v.number> boolean
129 8a35f56c 2022-07-16 thomas grammar :
130 8a35f56c 2022-07-16 thomas | grammar '\n'
131 8a35f56c 2022-07-16 thomas | grammar main '\n'
132 8a35f56c 2022-07-16 thomas | grammar server '\n'
135 8a35f56c 2022-07-16 thomas boolean : STRING {
136 8a35f56c 2022-07-16 thomas if (strcasecmp($1, "1") == 0 ||
137 8a35f56c 2022-07-16 thomas strcasecmp($1, "yes") == 0 ||
138 8a35f56c 2022-07-16 thomas strcasecmp($1, "on") == 0)
140 8a35f56c 2022-07-16 thomas else if (strcasecmp($1, "0") == 0 ||
141 8a35f56c 2022-07-16 thomas strcasecmp($1, "off") == 0 ||
142 8a35f56c 2022-07-16 thomas strcasecmp($1, "no") == 0)
145 8a35f56c 2022-07-16 thomas yyerror("invalid boolean value '%s'", $1);
146 8a35f56c 2022-07-16 thomas free($1);
149 8a35f56c 2022-07-16 thomas free($1);
151 8a35f56c 2022-07-16 thomas | ON { $$ = 1; }
152 8a35f56c 2022-07-16 thomas | NUMBER { $$ = $1; }
155 8a35f56c 2022-07-16 thomas fcgiport : NUMBER {
156 8a35f56c 2022-07-16 thomas if ($1 <= 0 || $1 > (int)USHRT_MAX) {
157 8a35f56c 2022-07-16 thomas yyerror("invalid port: %lld", $1);
160 8a35f56c 2022-07-16 thomas $$ = htons($1);
162 8a35f56c 2022-07-16 thomas | STRING {
165 8a35f56c 2022-07-16 thomas if ((val = getservice($1)) == -1) {
166 8a35f56c 2022-07-16 thomas yyerror("invalid port: %s", $1);
167 8a35f56c 2022-07-16 thomas free($1);
170 8a35f56c 2022-07-16 thomas free($1);
172 8a35f56c 2022-07-16 thomas $$ = val;
176 8a35f56c 2022-07-16 thomas main : PREFORK NUMBER {
177 8a35f56c 2022-07-16 thomas gotwebd->prefork_gotwebd = $2;
179 8a35f56c 2022-07-16 thomas | CHROOT STRING {
180 8a35f56c 2022-07-16 thomas n = strlcpy(gotwebd->httpd_chroot, $2,
181 8a35f56c 2022-07-16 thomas sizeof(gotwebd->httpd_chroot));
182 8a35f56c 2022-07-16 thomas if (n >= sizeof(gotwebd->httpd_chroot)) {
183 8a35f56c 2022-07-16 thomas yyerror("%s: httpd_chroot truncated", __func__);
184 8a35f56c 2022-07-16 thomas free($2);
187 8a35f56c 2022-07-16 thomas free($2);
189 8a35f56c 2022-07-16 thomas | FCGI_SOCKET boolean {
190 8a35f56c 2022-07-16 thomas gotwebd->fcgi_socket = $2;
192 8a35f56c 2022-07-16 thomas | FCGI_SOCKET boolean {
193 8a35f56c 2022-07-16 thomas gotwebd->fcgi_socket = $2;
194 8a35f56c 2022-07-16 thomas } '{' optnl socketopts4 '}'
195 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
196 8a35f56c 2022-07-16 thomas gotwebd->unix_socket = $2;
198 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
199 8a35f56c 2022-07-16 thomas n = snprintf(gotwebd->unix_socket_name,
200 8a35f56c 2022-07-16 thomas sizeof(gotwebd->unix_socket_name), "%s%s",
201 8a35f56c 2022-07-16 thomas strlen(gotwebd->httpd_chroot) ?
202 8a35f56c 2022-07-16 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
203 8a35f56c 2022-07-16 thomas if (n < 0) {
204 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
205 8a35f56c 2022-07-16 thomas __func__);
206 8a35f56c 2022-07-16 thomas free($2);
209 8a35f56c 2022-07-16 thomas free($2);
213 8a35f56c 2022-07-16 thomas server : SERVER STRING {
214 8a35f56c 2022-07-16 thomas struct server *srv;
216 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
217 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
218 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
219 8a35f56c 2022-07-16 thomas free($2);
224 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
225 8a35f56c 2022-07-16 thomas if (new_srv->fcgi_socket)
226 8a35f56c 2022-07-16 thomas if (get_addrs(new_srv->fcgi_socket_bind,
227 8a35f56c 2022-07-16 thomas new_srv->al,
228 8a35f56c 2022-07-16 thomas new_srv->fcgi_socket_port) == -1) {
229 8a35f56c 2022-07-16 thomas yyerror("could not get tcp iface "
230 8a35f56c 2022-07-16 thomas "addrs");
233 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
234 8a35f56c 2022-07-16 thomas free($2);
236 8a35f56c 2022-07-16 thomas | SERVER STRING {
237 8a35f56c 2022-07-16 thomas struct server *srv;
239 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
240 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, $2) == 0) {
241 8a35f56c 2022-07-16 thomas yyerror("server name exists '%s'", $2);
242 8a35f56c 2022-07-16 thomas free($2);
247 8a35f56c 2022-07-16 thomas new_srv = conf_new_server($2);
248 8a35f56c 2022-07-16 thomas log_debug("adding server %s", $2);
249 8a35f56c 2022-07-16 thomas free($2);
250 8a35f56c 2022-07-16 thomas } '{' optnl serveropts2 '}' {
251 8a35f56c 2022-07-16 thomas if (get_addrs(new_srv->fcgi_socket_bind,
252 8a35f56c 2022-07-16 thomas new_srv->al, new_srv->fcgi_socket_port) == -1) {
253 8a35f56c 2022-07-16 thomas yyerror("could not get tcp iface addrs");
259 8a35f56c 2022-07-16 thomas serveropts1 : REPOS_PATH STRING {
260 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->repos_path, $2,
261 8a35f56c 2022-07-16 thomas sizeof(new_srv->repos_path));
262 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->repos_path)) {
263 8a35f56c 2022-07-16 thomas yyerror("%s: repos_path truncated", __func__);
264 8a35f56c 2022-07-16 thomas free($2);
267 8a35f56c 2022-07-16 thomas free($2);
269 8a35f56c 2022-07-16 thomas | SITE_NAME STRING {
270 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_name, $2,
271 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_name));
272 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_name)) {
273 8a35f56c 2022-07-16 thomas yyerror("%s: site_name truncated", __func__);
274 8a35f56c 2022-07-16 thomas free($2);
277 8a35f56c 2022-07-16 thomas free($2);
279 8a35f56c 2022-07-16 thomas | SITE_OWNER STRING {
280 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_owner, $2,
281 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_owner));
282 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_owner)) {
283 8a35f56c 2022-07-16 thomas yyerror("%s: site_owner truncated", __func__);
284 8a35f56c 2022-07-16 thomas free($2);
287 8a35f56c 2022-07-16 thomas free($2);
289 8a35f56c 2022-07-16 thomas | SITE_LINK STRING {
290 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->site_link, $2,
291 8a35f56c 2022-07-16 thomas sizeof(new_srv->site_link));
292 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->site_link)) {
293 8a35f56c 2022-07-16 thomas yyerror("%s: site_link truncated", __func__);
294 8a35f56c 2022-07-16 thomas free($2);
297 8a35f56c 2022-07-16 thomas free($2);
299 8a35f56c 2022-07-16 thomas | LOGO STRING {
300 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
301 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo)) {
302 8a35f56c 2022-07-16 thomas yyerror("%s: logo truncated", __func__);
303 8a35f56c 2022-07-16 thomas free($2);
306 8a35f56c 2022-07-16 thomas free($2);
308 8a35f56c 2022-07-16 thomas | LOGO_URL STRING {
309 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->logo_url, $2,
310 8a35f56c 2022-07-16 thomas sizeof(new_srv->logo_url));
311 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->logo_url)) {
312 8a35f56c 2022-07-16 thomas yyerror("%s: logo_url truncated", __func__);
313 8a35f56c 2022-07-16 thomas free($2);
316 8a35f56c 2022-07-16 thomas free($2);
318 8a35f56c 2022-07-16 thomas | CUSTOM_CSS STRING {
319 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->custom_css, $2,
320 8a35f56c 2022-07-16 thomas sizeof(new_srv->custom_css));
321 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->custom_css)) {
322 8a35f56c 2022-07-16 thomas yyerror("%s: custom_css truncated", __func__);
323 8a35f56c 2022-07-16 thomas free($2);
326 8a35f56c 2022-07-16 thomas free($2);
328 8a35f56c 2022-07-16 thomas | MAX_REPOS NUMBER {
329 8a35f56c 2022-07-16 thomas if ($2 > 0)
330 8a35f56c 2022-07-16 thomas new_srv->max_repos = $2;
332 8a35f56c 2022-07-16 thomas | SHOW_SITE_OWNER boolean {
333 8a35f56c 2022-07-16 thomas new_srv->show_site_owner = $2;
335 8a35f56c 2022-07-16 thomas | SHOW_REPO_OWNER boolean {
336 8a35f56c 2022-07-16 thomas new_srv->show_repo_owner = $2;
338 8a35f56c 2022-07-16 thomas | SHOW_REPO_AGE boolean {
339 8a35f56c 2022-07-16 thomas new_srv->show_repo_age = $2;
341 8a35f56c 2022-07-16 thomas | SHOW_REPO_DESCRIPTION boolean {
342 8a35f56c 2022-07-16 thomas new_srv->show_repo_description = $2;
344 8a35f56c 2022-07-16 thomas | SHOW_REPO_CLONEURL boolean {
345 8a35f56c 2022-07-16 thomas new_srv->show_repo_cloneurl = $2;
347 8a35f56c 2022-07-16 thomas | MAX_REPOS_DISPLAY NUMBER {
348 8a35f56c 2022-07-16 thomas new_srv->max_repos_display = $2;
350 8a35f56c 2022-07-16 thomas | MAX_COMMITS_DISPLAY NUMBER {
351 8a35f56c 2022-07-16 thomas if ($2 > 0)
352 8a35f56c 2022-07-16 thomas new_srv->max_commits_display = $2;
354 8a35f56c 2022-07-16 thomas | FCGI_SOCKET boolean {
355 8a35f56c 2022-07-16 thomas new_srv->fcgi_socket = $2;
357 8a35f56c 2022-07-16 thomas | FCGI_SOCKET boolean {
358 8a35f56c 2022-07-16 thomas new_srv->fcgi_socket = $2;
359 8a35f56c 2022-07-16 thomas } '{' optnl socketopts2 '}'
360 8a35f56c 2022-07-16 thomas | UNIX_SOCKET boolean {
361 8a35f56c 2022-07-16 thomas new_srv->unix_socket = $2;
363 8a35f56c 2022-07-16 thomas | UNIX_SOCKET_NAME STRING {
364 8a35f56c 2022-07-16 thomas n = snprintf(new_srv->unix_socket_name,
365 8a35f56c 2022-07-16 thomas sizeof(new_srv->unix_socket_name), "%s%s",
366 8a35f56c 2022-07-16 thomas strlen(gotwebd->httpd_chroot) ?
367 8a35f56c 2022-07-16 thomas gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
368 8a35f56c 2022-07-16 thomas if (n < 0) {
369 8a35f56c 2022-07-16 thomas yyerror("%s: unix_socket_name truncated",
370 8a35f56c 2022-07-16 thomas __func__);
371 8a35f56c 2022-07-16 thomas free($2);
374 8a35f56c 2022-07-16 thomas free($2);
378 8a35f56c 2022-07-16 thomas serveropts2 : serveropts2 serveropts1 nl
379 8a35f56c 2022-07-16 thomas | serveropts1 optnl
382 8a35f56c 2022-07-16 thomas socketopts1 : BIND INTERFACE STRING {
383 8a35f56c 2022-07-16 thomas n = strlcpy(new_srv->fcgi_socket_bind, $3,
384 8a35f56c 2022-07-16 thomas sizeof(new_srv->fcgi_socket_bind));
385 8a35f56c 2022-07-16 thomas if (n >= sizeof(new_srv->fcgi_socket_bind)) {
386 8a35f56c 2022-07-16 thomas yyerror("%s: fcgi_socket_bind truncated",
387 8a35f56c 2022-07-16 thomas __func__);
388 8a35f56c 2022-07-16 thomas free($3);
391 8a35f56c 2022-07-16 thomas free($3);
393 8a35f56c 2022-07-16 thomas | PORT fcgiport {
394 8a35f56c 2022-07-16 thomas struct server *srv;
396 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(srv, gotwebd->servers, entry) {
397 8a35f56c 2022-07-16 thomas if (srv->fcgi_socket_port == $2) {
398 8a35f56c 2022-07-16 thomas yyerror("port already assigned");
402 8a35f56c 2022-07-16 thomas new_srv->fcgi_socket_port = $2;
406 8a35f56c 2022-07-16 thomas socketopts2 : socketopts2 socketopts1 nl
407 8a35f56c 2022-07-16 thomas | socketopts1 optnl
410 8a35f56c 2022-07-16 thomas socketopts3 : BIND INTERFACE STRING {
411 8a35f56c 2022-07-16 thomas n = strlcpy(gotwebd->fcgi_socket_bind, $3,
412 8a35f56c 2022-07-16 thomas sizeof(gotwebd->fcgi_socket_bind));
413 8a35f56c 2022-07-16 thomas if (n >= sizeof(gotwebd->fcgi_socket_bind)) {
414 8a35f56c 2022-07-16 thomas yyerror("%s: fcgi_socket_bind truncated",
415 8a35f56c 2022-07-16 thomas __func__);
416 8a35f56c 2022-07-16 thomas free($3);
419 8a35f56c 2022-07-16 thomas free($3);
421 8a35f56c 2022-07-16 thomas | PORT fcgiport {
422 8a35f56c 2022-07-16 thomas gotwebd->fcgi_socket_port = $2;
426 8a35f56c 2022-07-16 thomas socketopts4 : socketopts4 socketopts3 nl
427 8a35f56c 2022-07-16 thomas | socketopts3 optnl
430 8a35f56c 2022-07-16 thomas nl : '\n' optnl
433 8a35f56c 2022-07-16 thomas optnl : '\n' optnl /* zero or more newlines */
434 8a35f56c 2022-07-16 thomas | /* empty */
439 8a35f56c 2022-07-16 thomas struct keywords {
440 8a35f56c 2022-07-16 thomas const char *k_name;
441 8a35f56c 2022-07-16 thomas int k_val;
445 8a35f56c 2022-07-16 thomas yyerror(const char *fmt, ...)
447 8a35f56c 2022-07-16 thomas va_list ap;
448 8a35f56c 2022-07-16 thomas char *msg;
450 8a35f56c 2022-07-16 thomas file->errors++;
451 8a35f56c 2022-07-16 thomas va_start(ap, fmt);
452 8a35f56c 2022-07-16 thomas if (vasprintf(&msg, fmt, ap) == -1)
453 8a35f56c 2022-07-16 thomas fatalx("yyerror vasprintf");
454 8a35f56c 2022-07-16 thomas va_end(ap);
455 8a35f56c 2022-07-16 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
456 8a35f56c 2022-07-16 thomas free(msg);
457 8a35f56c 2022-07-16 thomas return (0);
461 8a35f56c 2022-07-16 thomas kw_cmp(const void *k, const void *e)
463 8a35f56c 2022-07-16 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
467 8a35f56c 2022-07-16 thomas lookup(char *s)
469 8a35f56c 2022-07-16 thomas /* This has to be sorted always. */
470 8a35f56c 2022-07-16 thomas static const struct keywords keywords[] = {
471 8a35f56c 2022-07-16 thomas { "bind", BIND },
472 8a35f56c 2022-07-16 thomas { "chroot", CHROOT },
473 8a35f56c 2022-07-16 thomas { "custom_css", CUSTOM_CSS },
474 8a35f56c 2022-07-16 thomas { "fcgi_socket", FCGI_SOCKET },
475 8a35f56c 2022-07-16 thomas { "interface", INTERFACE },
476 8a35f56c 2022-07-16 thomas { "logo", LOGO },
477 8a35f56c 2022-07-16 thomas { "logo_url" , LOGO_URL },
478 8a35f56c 2022-07-16 thomas { "max_commits_display", MAX_COMMITS_DISPLAY },
479 8a35f56c 2022-07-16 thomas { "max_repos", MAX_REPOS },
480 8a35f56c 2022-07-16 thomas { "max_repos_display", MAX_REPOS_DISPLAY },
481 8a35f56c 2022-07-16 thomas { "port", PORT },
482 8a35f56c 2022-07-16 thomas { "prefork", PREFORK },
483 8a35f56c 2022-07-16 thomas { "repos_path", REPOS_PATH },
484 8a35f56c 2022-07-16 thomas { "server", SERVER },
485 8a35f56c 2022-07-16 thomas { "show_repo_age", SHOW_REPO_AGE },
486 8a35f56c 2022-07-16 thomas { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
487 8a35f56c 2022-07-16 thomas { "show_repo_description", SHOW_REPO_DESCRIPTION },
488 8a35f56c 2022-07-16 thomas { "show_repo_owner", SHOW_REPO_OWNER },
489 8a35f56c 2022-07-16 thomas { "show_site_owner", SHOW_SITE_OWNER },
490 8a35f56c 2022-07-16 thomas { "site_link", SITE_LINK },
491 8a35f56c 2022-07-16 thomas { "site_name", SITE_NAME },
492 8a35f56c 2022-07-16 thomas { "site_owner", SITE_OWNER },
493 8a35f56c 2022-07-16 thomas { "unix_socket", UNIX_SOCKET },
494 8a35f56c 2022-07-16 thomas { "unix_socket_name", UNIX_SOCKET_NAME },
496 8a35f56c 2022-07-16 thomas const struct keywords *p;
498 8a35f56c 2022-07-16 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
499 8a35f56c 2022-07-16 thomas sizeof(keywords[0]), kw_cmp);
502 8a35f56c 2022-07-16 thomas return (p->k_val);
504 8a35f56c 2022-07-16 thomas return (STRING);
507 8a35f56c 2022-07-16 thomas #define MAXPUSHBACK 128
509 8a35f56c 2022-07-16 thomas unsigned char *parsebuf;
510 8a35f56c 2022-07-16 thomas int parseindex;
511 8a35f56c 2022-07-16 thomas unsigned char pushback_buffer[MAXPUSHBACK];
512 8a35f56c 2022-07-16 thomas int pushback_index = 0;
515 8a35f56c 2022-07-16 thomas lgetc(int quotec)
517 8a35f56c 2022-07-16 thomas int c, next;
519 8a35f56c 2022-07-16 thomas if (parsebuf) {
520 8a35f56c 2022-07-16 thomas /* Read character from the parsebuffer instead of input. */
521 8a35f56c 2022-07-16 thomas if (parseindex >= 0) {
522 8a35f56c 2022-07-16 thomas c = parsebuf[parseindex++];
523 8a35f56c 2022-07-16 thomas if (c != '\0')
524 8a35f56c 2022-07-16 thomas return (c);
525 8a35f56c 2022-07-16 thomas parsebuf = NULL;
527 8a35f56c 2022-07-16 thomas parseindex++;
530 8a35f56c 2022-07-16 thomas if (pushback_index)
531 8a35f56c 2022-07-16 thomas return (pushback_buffer[--pushback_index]);
533 8a35f56c 2022-07-16 thomas if (quotec) {
534 8a35f56c 2022-07-16 thomas c = getc(file->stream);
535 8a35f56c 2022-07-16 thomas if (c == EOF)
536 8a35f56c 2022-07-16 thomas yyerror("reached end of file while parsing "
537 8a35f56c 2022-07-16 thomas "quoted string");
538 8a35f56c 2022-07-16 thomas return (c);
541 8a35f56c 2022-07-16 thomas c = getc(file->stream);
542 8a35f56c 2022-07-16 thomas while (c == '\\') {
543 8a35f56c 2022-07-16 thomas next = getc(file->stream);
544 8a35f56c 2022-07-16 thomas if (next != '\n') {
545 8a35f56c 2022-07-16 thomas c = next;
548 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
549 8a35f56c 2022-07-16 thomas file->lineno++;
550 8a35f56c 2022-07-16 thomas c = getc(file->stream);
553 8a35f56c 2022-07-16 thomas return (c);
557 8a35f56c 2022-07-16 thomas lungetc(int c)
559 8a35f56c 2022-07-16 thomas if (c == EOF)
560 8a35f56c 2022-07-16 thomas return (EOF);
561 8a35f56c 2022-07-16 thomas if (parsebuf) {
562 8a35f56c 2022-07-16 thomas parseindex--;
563 8a35f56c 2022-07-16 thomas if (parseindex >= 0)
564 8a35f56c 2022-07-16 thomas return (c);
566 8a35f56c 2022-07-16 thomas if (pushback_index < MAXPUSHBACK-1)
567 8a35f56c 2022-07-16 thomas return (pushback_buffer[pushback_index++] = c);
569 8a35f56c 2022-07-16 thomas return (EOF);
573 8a35f56c 2022-07-16 thomas findeol(void)
577 8a35f56c 2022-07-16 thomas parsebuf = NULL;
579 8a35f56c 2022-07-16 thomas /* Skip to either EOF or the first real EOL. */
580 8a35f56c 2022-07-16 thomas while (1) {
581 8a35f56c 2022-07-16 thomas if (pushback_index)
582 8a35f56c 2022-07-16 thomas c = pushback_buffer[--pushback_index];
584 8a35f56c 2022-07-16 thomas c = lgetc(0);
585 8a35f56c 2022-07-16 thomas if (c == '\n') {
586 8a35f56c 2022-07-16 thomas file->lineno++;
589 8a35f56c 2022-07-16 thomas if (c == EOF)
592 8a35f56c 2022-07-16 thomas return (ERROR);
596 8a35f56c 2022-07-16 thomas yylex(void)
598 8a35f56c 2022-07-16 thomas unsigned char buf[8096];
599 8a35f56c 2022-07-16 thomas unsigned char *p, *val;
600 8a35f56c 2022-07-16 thomas int quotec, next, c;
601 8a35f56c 2022-07-16 thomas int token;
605 8a35f56c 2022-07-16 thomas c = lgetc(0);
606 8a35f56c 2022-07-16 thomas while (c == ' ' || c == '\t')
607 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
609 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
610 8a35f56c 2022-07-16 thomas if (c == '#') {
611 8a35f56c 2022-07-16 thomas c = lgetc(0);
612 8a35f56c 2022-07-16 thomas while (c != '\n' && c != EOF)
613 8a35f56c 2022-07-16 thomas c = lgetc(0); /* nothing */
615 8a35f56c 2022-07-16 thomas if (c == '$' && parsebuf == NULL) {
616 8a35f56c 2022-07-16 thomas while (1) {
617 8a35f56c 2022-07-16 thomas c = lgetc(0);
618 8a35f56c 2022-07-16 thomas if (c == EOF)
619 8a35f56c 2022-07-16 thomas return (0);
621 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
622 8a35f56c 2022-07-16 thomas yyerror("string too long");
623 8a35f56c 2022-07-16 thomas return (findeol());
625 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == '_') {
626 8a35f56c 2022-07-16 thomas *p++ = c;
627 8a35f56c 2022-07-16 thomas continue;
629 8a35f56c 2022-07-16 thomas *p = '\0';
630 8a35f56c 2022-07-16 thomas lungetc(c);
633 8a35f56c 2022-07-16 thomas val = symget(buf);
634 8a35f56c 2022-07-16 thomas if (val == NULL) {
635 8a35f56c 2022-07-16 thomas yyerror("macro '%s' not defined", buf);
636 8a35f56c 2022-07-16 thomas return (findeol());
638 8a35f56c 2022-07-16 thomas parsebuf = val;
639 8a35f56c 2022-07-16 thomas parseindex = 0;
640 8a35f56c 2022-07-16 thomas goto top;
643 8a35f56c 2022-07-16 thomas switch (c) {
644 8a35f56c 2022-07-16 thomas case '\'':
645 8a35f56c 2022-07-16 thomas case '"':
646 8a35f56c 2022-07-16 thomas quotec = c;
647 8a35f56c 2022-07-16 thomas while (1) {
648 8a35f56c 2022-07-16 thomas c = lgetc(quotec);
649 8a35f56c 2022-07-16 thomas if (c == EOF)
650 8a35f56c 2022-07-16 thomas return (0);
651 8a35f56c 2022-07-16 thomas if (c == '\n') {
652 8a35f56c 2022-07-16 thomas file->lineno++;
653 8a35f56c 2022-07-16 thomas continue;
654 8a35f56c 2022-07-16 thomas } else if (c == '\\') {
655 8a35f56c 2022-07-16 thomas next = lgetc(quotec);
656 8a35f56c 2022-07-16 thomas if (next == EOF)
657 8a35f56c 2022-07-16 thomas return (0);
658 8a35f56c 2022-07-16 thomas if (next == quotec || c == ' ' || c == '\t')
659 8a35f56c 2022-07-16 thomas c = next;
660 8a35f56c 2022-07-16 thomas else if (next == '\n') {
661 8a35f56c 2022-07-16 thomas file->lineno++;
662 8a35f56c 2022-07-16 thomas continue;
664 8a35f56c 2022-07-16 thomas lungetc(next);
665 8a35f56c 2022-07-16 thomas } else if (c == quotec) {
666 8a35f56c 2022-07-16 thomas *p = '\0';
668 8a35f56c 2022-07-16 thomas } else if (c == '\0') {
669 8a35f56c 2022-07-16 thomas yyerror("syntax error");
670 8a35f56c 2022-07-16 thomas return (findeol());
672 8a35f56c 2022-07-16 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
673 8a35f56c 2022-07-16 thomas yyerror("string too long");
674 8a35f56c 2022-07-16 thomas return (findeol());
676 8a35f56c 2022-07-16 thomas *p++ = c;
678 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
679 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
680 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
681 8a35f56c 2022-07-16 thomas return (STRING);
684 8a35f56c 2022-07-16 thomas #define allowed_to_end_number(x) \
685 8a35f56c 2022-07-16 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
687 8a35f56c 2022-07-16 thomas if (c == '-' || isdigit(c)) {
689 8a35f56c 2022-07-16 thomas *p++ = c;
690 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
691 8a35f56c 2022-07-16 thomas yyerror("string too long");
692 8a35f56c 2022-07-16 thomas return (findeol());
694 8a35f56c 2022-07-16 thomas c = lgetc(0);
695 8a35f56c 2022-07-16 thomas } while (c != EOF && isdigit(c));
696 8a35f56c 2022-07-16 thomas lungetc(c);
697 8a35f56c 2022-07-16 thomas if (p == buf + 1 && buf[0] == '-')
698 8a35f56c 2022-07-16 thomas goto nodigits;
699 8a35f56c 2022-07-16 thomas if (c == EOF || allowed_to_end_number(c)) {
700 8a35f56c 2022-07-16 thomas const char *errstr = NULL;
702 8a35f56c 2022-07-16 thomas *p = '\0';
703 8a35f56c 2022-07-16 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
704 8a35f56c 2022-07-16 thomas LLONG_MAX, &errstr);
705 8a35f56c 2022-07-16 thomas if (errstr) {
706 8a35f56c 2022-07-16 thomas yyerror("\"%s\" invalid number: %s",
707 8a35f56c 2022-07-16 thomas buf, errstr);
708 8a35f56c 2022-07-16 thomas return (findeol());
710 8a35f56c 2022-07-16 thomas return (NUMBER);
712 8a35f56c 2022-07-16 thomas nodigits:
713 8a35f56c 2022-07-16 thomas while (p > buf + 1)
714 8a35f56c 2022-07-16 thomas lungetc(*--p);
715 8a35f56c 2022-07-16 thomas c = *--p;
716 8a35f56c 2022-07-16 thomas if (c == '-')
717 8a35f56c 2022-07-16 thomas return (c);
721 8a35f56c 2022-07-16 thomas #define allowed_in_string(x) \
722 8a35f56c 2022-07-16 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
723 8a35f56c 2022-07-16 thomas x != '{' && x != '}' && \
724 8a35f56c 2022-07-16 thomas x != '!' && x != '=' && x != '#' && \
725 8a35f56c 2022-07-16 thomas x != ','))
727 8a35f56c 2022-07-16 thomas if (isalnum(c) || c == ':' || c == '_') {
729 8a35f56c 2022-07-16 thomas *p++ = c;
730 8a35f56c 2022-07-16 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
731 8a35f56c 2022-07-16 thomas yyerror("string too long");
732 8a35f56c 2022-07-16 thomas return (findeol());
734 8a35f56c 2022-07-16 thomas c = lgetc(0);
735 8a35f56c 2022-07-16 thomas } while (c != EOF && (allowed_in_string(c)));
736 8a35f56c 2022-07-16 thomas lungetc(c);
737 8a35f56c 2022-07-16 thomas *p = '\0';
738 8a35f56c 2022-07-16 thomas token = lookup(buf);
739 8a35f56c 2022-07-16 thomas if (token == STRING) {
740 8a35f56c 2022-07-16 thomas yylval.v.string = strdup(buf);
741 8a35f56c 2022-07-16 thomas if (yylval.v.string == NULL)
742 8a35f56c 2022-07-16 thomas err(1, "yylex: strdup");
744 8a35f56c 2022-07-16 thomas return (token);
746 8a35f56c 2022-07-16 thomas if (c == '\n') {
747 8a35f56c 2022-07-16 thomas yylval.lineno = file->lineno;
748 8a35f56c 2022-07-16 thomas file->lineno++;
750 8a35f56c 2022-07-16 thomas if (c == EOF)
751 8a35f56c 2022-07-16 thomas return (0);
752 8a35f56c 2022-07-16 thomas return (c);
756 8a35f56c 2022-07-16 thomas check_file_secrecy(int fd, const char *fname)
758 8a35f56c 2022-07-16 thomas struct stat st;
760 8a35f56c 2022-07-16 thomas if (fstat(fd, &st)) {
761 8a35f56c 2022-07-16 thomas log_warn("cannot stat %s", fname);
762 8a35f56c 2022-07-16 thomas return (-1);
764 8a35f56c 2022-07-16 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
765 8a35f56c 2022-07-16 thomas log_warnx("%s: owner not root or current user", fname);
766 8a35f56c 2022-07-16 thomas return (-1);
768 8a35f56c 2022-07-16 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
769 8a35f56c 2022-07-16 thomas log_warnx("%s: group writable or world read/writable", fname);
770 8a35f56c 2022-07-16 thomas return (-1);
772 8a35f56c 2022-07-16 thomas return (0);
775 8a35f56c 2022-07-16 thomas struct file *
776 8a35f56c 2022-07-16 thomas newfile(const char *name, int secret)
778 8a35f56c 2022-07-16 thomas struct file *nfile;
780 8a35f56c 2022-07-16 thomas nfile = calloc(1, sizeof(struct file));
781 8a35f56c 2022-07-16 thomas if (nfile == NULL) {
782 8a35f56c 2022-07-16 thomas log_warn("calloc");
783 8a35f56c 2022-07-16 thomas return (NULL);
785 8a35f56c 2022-07-16 thomas nfile->name = strdup(name);
786 8a35f56c 2022-07-16 thomas if (nfile->name == NULL) {
787 8a35f56c 2022-07-16 thomas log_warn("strdup");
788 8a35f56c 2022-07-16 thomas free(nfile);
789 8a35f56c 2022-07-16 thomas return (NULL);
791 8a35f56c 2022-07-16 thomas nfile->stream = fopen(nfile->name, "r");
792 8a35f56c 2022-07-16 thomas if (nfile->stream == NULL) {
793 8a35f56c 2022-07-16 thomas /* no warning, we don't require a conf file */
794 8a35f56c 2022-07-16 thomas free(nfile->name);
795 8a35f56c 2022-07-16 thomas free(nfile);
796 8a35f56c 2022-07-16 thomas return (NULL);
797 8a35f56c 2022-07-16 thomas } else if (secret &&
798 8a35f56c 2022-07-16 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
799 8a35f56c 2022-07-16 thomas fclose(nfile->stream);
800 8a35f56c 2022-07-16 thomas free(nfile->name);
801 8a35f56c 2022-07-16 thomas free(nfile);
802 8a35f56c 2022-07-16 thomas return (NULL);
804 8a35f56c 2022-07-16 thomas nfile->lineno = 1;
805 8a35f56c 2022-07-16 thomas return (nfile);
808 8a35f56c 2022-07-16 thomas static void
809 8a35f56c 2022-07-16 thomas closefile(struct file *xfile)
811 8a35f56c 2022-07-16 thomas fclose(xfile->stream);
812 8a35f56c 2022-07-16 thomas free(xfile->name);
813 8a35f56c 2022-07-16 thomas free(xfile);
817 8a35f56c 2022-07-16 thomas parse_config(const char *filename, struct gotwebd *env)
819 8a35f56c 2022-07-16 thomas struct sym *sym, *next;
821 8a35f56c 2022-07-16 thomas file = newfile(filename, 0);
822 8a35f56c 2022-07-16 thomas if (file == NULL)
823 8a35f56c 2022-07-16 thomas /* just return, as we don't require a conf file */
824 8a35f56c 2022-07-16 thomas return (0);
826 8a35f56c 2022-07-16 thomas if (config_init(env) == -1)
827 8a35f56c 2022-07-16 thomas fatalx("failed to initialize configuration");
829 8a35f56c 2022-07-16 thomas gotwebd = env;
831 8a35f56c 2022-07-16 thomas yyparse();
832 8a35f56c 2022-07-16 thomas errors = file->errors;
833 8a35f56c 2022-07-16 thomas closefile(file);
835 8a35f56c 2022-07-16 thomas /* Free macros and check which have not been used. */
836 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
837 8a35f56c 2022-07-16 thomas if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
838 8a35f56c 2022-07-16 thomas fprintf(stderr, "warning: macro '%s' not used\n",
839 8a35f56c 2022-07-16 thomas sym->nam);
840 8a35f56c 2022-07-16 thomas if (!sym->persist) {
841 8a35f56c 2022-07-16 thomas free(sym->nam);
842 8a35f56c 2022-07-16 thomas free(sym->val);
843 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
844 8a35f56c 2022-07-16 thomas free(sym);
848 8a35f56c 2022-07-16 thomas if (errors)
849 8a35f56c 2022-07-16 thomas return (-1);
851 8a35f56c 2022-07-16 thomas /* just add default server if no config specified */
852 8a35f56c 2022-07-16 thomas if (gotwebd->server_cnt == 0) {
853 8a35f56c 2022-07-16 thomas new_srv = conf_new_server(D_SITENAME);
854 8a35f56c 2022-07-16 thomas log_debug("%s: adding default server %s", __func__, D_SITENAME);
857 8a35f56c 2022-07-16 thomas /* setup our listening sockets */
858 8a35f56c 2022-07-16 thomas sockets_parse_sockets(env);
860 8a35f56c 2022-07-16 thomas return (0);
863 8a35f56c 2022-07-16 thomas struct server *
864 8a35f56c 2022-07-16 thomas conf_new_server(const char *name)
866 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
869 8a35f56c 2022-07-16 thomas srv = calloc(1, sizeof(*srv));
870 8a35f56c 2022-07-16 thomas if (srv == NULL)
871 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
873 8a35f56c 2022-07-16 thomas n = strlcpy(srv->name, name, sizeof(srv->name));
874 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->name))
875 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
876 8a35f56c 2022-07-16 thomas n = snprintf(srv->unix_socket_name,
877 8a35f56c 2022-07-16 thomas sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
878 8a35f56c 2022-07-16 thomas D_UNIX_SOCKET);
879 8a35f56c 2022-07-16 thomas if (n < 0)
880 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
881 8a35f56c 2022-07-16 thomas n = strlcpy(srv->repos_path, D_GOTPATH,
882 8a35f56c 2022-07-16 thomas sizeof(srv->repos_path));
883 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->repos_path))
884 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
885 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_name, D_SITENAME,
886 8a35f56c 2022-07-16 thomas sizeof(srv->site_name));
887 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_name))
888 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
889 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_owner, D_SITEOWNER,
890 8a35f56c 2022-07-16 thomas sizeof(srv->site_owner));
891 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_owner))
892 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
893 8a35f56c 2022-07-16 thomas n = strlcpy(srv->site_link, D_SITELINK,
894 8a35f56c 2022-07-16 thomas sizeof(srv->site_link));
895 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->site_link))
896 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
897 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo, D_GOTLOGO,
898 8a35f56c 2022-07-16 thomas sizeof(srv->logo));
899 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo))
900 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
901 8a35f56c 2022-07-16 thomas n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
902 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->logo_url))
903 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
904 8a35f56c 2022-07-16 thomas n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
905 8a35f56c 2022-07-16 thomas if (n >= sizeof(srv->custom_css))
906 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
908 8a35f56c 2022-07-16 thomas val = getservice(D_FCGI_PORT);
909 8a35f56c 2022-07-16 thomas srv->fcgi_socket_port = gotwebd->fcgi_socket_port ?
910 8a35f56c 2022-07-16 thomas gotwebd->fcgi_socket_port: htons(val);
912 8a35f56c 2022-07-16 thomas srv->show_site_owner = D_SHOWSOWNER;
913 8a35f56c 2022-07-16 thomas srv->show_repo_owner = D_SHOWROWNER;
914 8a35f56c 2022-07-16 thomas srv->show_repo_age = D_SHOWAGE;
915 8a35f56c 2022-07-16 thomas srv->show_repo_description = D_SHOWDESC;
916 8a35f56c 2022-07-16 thomas srv->show_repo_cloneurl = D_SHOWURL;
918 8a35f56c 2022-07-16 thomas srv->max_repos_display = D_MAXREPODISP;
919 8a35f56c 2022-07-16 thomas srv->max_commits_display = D_MAXCOMMITDISP;
920 8a35f56c 2022-07-16 thomas srv->max_repos = D_MAXREPO;
922 8a35f56c 2022-07-16 thomas srv->unix_socket = 1;
923 8a35f56c 2022-07-16 thomas srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
925 8a35f56c 2022-07-16 thomas if ((srv->al = calloc(1, sizeof(*srv->al))) == NULL)
926 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
928 8a35f56c 2022-07-16 thomas TAILQ_INIT(srv->al);
929 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(gotwebd->servers, srv, entry);
930 8a35f56c 2022-07-16 thomas gotwebd->server_cnt++;
932 8a35f56c 2022-07-16 thomas return srv;
936 8a35f56c 2022-07-16 thomas symset(const char *nam, const char *val, int persist)
938 8a35f56c 2022-07-16 thomas struct sym *sym;
940 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
941 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0)
945 8a35f56c 2022-07-16 thomas if (sym != NULL) {
946 8a35f56c 2022-07-16 thomas if (sym->persist == 1)
947 8a35f56c 2022-07-16 thomas return (0);
949 8a35f56c 2022-07-16 thomas free(sym->nam);
950 8a35f56c 2022-07-16 thomas free(sym->val);
951 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&symhead, sym, entry);
952 8a35f56c 2022-07-16 thomas free(sym);
955 8a35f56c 2022-07-16 thomas sym = calloc(1, sizeof(*sym));
956 8a35f56c 2022-07-16 thomas if (sym == NULL)
957 8a35f56c 2022-07-16 thomas return (-1);
959 8a35f56c 2022-07-16 thomas sym->nam = strdup(nam);
960 8a35f56c 2022-07-16 thomas if (sym->nam == NULL) {
961 8a35f56c 2022-07-16 thomas free(sym);
962 8a35f56c 2022-07-16 thomas return (-1);
964 8a35f56c 2022-07-16 thomas sym->val = strdup(val);
965 8a35f56c 2022-07-16 thomas if (sym->val == NULL) {
966 8a35f56c 2022-07-16 thomas free(sym->nam);
967 8a35f56c 2022-07-16 thomas free(sym);
968 8a35f56c 2022-07-16 thomas return (-1);
970 8a35f56c 2022-07-16 thomas sym->used = 0;
971 8a35f56c 2022-07-16 thomas sym->persist = persist;
972 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
973 8a35f56c 2022-07-16 thomas return (0);
977 8a35f56c 2022-07-16 thomas cmdline_symset(char *s)
979 8a35f56c 2022-07-16 thomas char *sym, *val;
981 8a35f56c 2022-07-16 thomas size_t len;
983 8a35f56c 2022-07-16 thomas val = strrchr(s, '=');
984 8a35f56c 2022-07-16 thomas if (val == NULL)
985 8a35f56c 2022-07-16 thomas return (-1);
987 8a35f56c 2022-07-16 thomas len = strlen(s) - strlen(val) + 1;
988 8a35f56c 2022-07-16 thomas sym = malloc(len);
989 8a35f56c 2022-07-16 thomas if (sym == NULL)
990 8a35f56c 2022-07-16 thomas fatal("%s: malloc", __func__);
992 8a35f56c 2022-07-16 thomas memcpy(&sym, s, len);
994 8a35f56c 2022-07-16 thomas ret = symset(sym, val + 1, 1);
995 8a35f56c 2022-07-16 thomas free(sym);
997 8a35f56c 2022-07-16 thomas return (ret);
1001 8a35f56c 2022-07-16 thomas symget(const char *nam)
1003 8a35f56c 2022-07-16 thomas struct sym *sym;
1005 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1006 8a35f56c 2022-07-16 thomas if (strcmp(nam, sym->nam) == 0) {
1007 8a35f56c 2022-07-16 thomas sym->used = 1;
1008 8a35f56c 2022-07-16 thomas return (sym->val);
1011 8a35f56c 2022-07-16 thomas return (NULL);
1015 8a35f56c 2022-07-16 thomas getservice(const char *n)
1017 8a35f56c 2022-07-16 thomas struct servent *s;
1018 8a35f56c 2022-07-16 thomas const char *errstr;
1019 8a35f56c 2022-07-16 thomas long long llval;
1021 8a35f56c 2022-07-16 thomas llval = strtonum(n, 0, UINT16_MAX, &errstr);
1022 8a35f56c 2022-07-16 thomas if (errstr) {
1023 8a35f56c 2022-07-16 thomas s = getservbyname(n, "tcp");
1024 8a35f56c 2022-07-16 thomas if (s == NULL)
1025 8a35f56c 2022-07-16 thomas s = getservbyname(n, "udp");
1026 8a35f56c 2022-07-16 thomas if (s == NULL)
1027 8a35f56c 2022-07-16 thomas return (-1);
1028 8a35f56c 2022-07-16 thomas return (s->s_port);
1031 8a35f56c 2022-07-16 thomas return (htons((unsigned short)llval));
1034 8a35f56c 2022-07-16 thomas struct address *
1035 8a35f56c 2022-07-16 thomas host_v4(const char *s)
1037 8a35f56c 2022-07-16 thomas struct in_addr ina;
1038 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1039 8a35f56c 2022-07-16 thomas struct address *h;
1041 8a35f56c 2022-07-16 thomas memset(&ina, 0, sizeof(ina));
1042 8a35f56c 2022-07-16 thomas if (inet_pton(AF_INET, s, &ina) != 1)
1043 8a35f56c 2022-07-16 thomas return (NULL);
1045 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1046 8a35f56c 2022-07-16 thomas fatal(__func__);
1047 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1048 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
1049 ff36aeea 2022-07-16 thomas #ifndef __linux__
1050 8a35f56c 2022-07-16 thomas sain->sin_len = sizeof(struct sockaddr_in);
1052 8a35f56c 2022-07-16 thomas sain->sin_family = AF_INET;
1053 8a35f56c 2022-07-16 thomas sain->sin_addr.s_addr = ina.s_addr;
1054 8a35f56c 2022-07-16 thomas if (sain->sin_addr.s_addr == INADDR_ANY)
1055 8a35f56c 2022-07-16 thomas h->prefixlen = 0; /* 0.0.0.0 address */
1057 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1058 8a35f56c 2022-07-16 thomas return (h);
1061 8a35f56c 2022-07-16 thomas struct address *
1062 8a35f56c 2022-07-16 thomas host_v6(const char *s)
1064 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res;
1065 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sa_in6;
1066 8a35f56c 2022-07-16 thomas struct address *h = NULL;
1068 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1069 8a35f56c 2022-07-16 thomas hints.ai_family = AF_INET6;
1070 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_DGRAM; /* dummy */
1071 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_NUMERICHOST;
1072 8a35f56c 2022-07-16 thomas if (getaddrinfo(s, "0", &hints, &res) == 0) {
1073 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1074 8a35f56c 2022-07-16 thomas fatal(__func__);
1075 8a35f56c 2022-07-16 thomas sa_in6 = (struct sockaddr_in6 *)&h->ss;
1076 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
1077 ff36aeea 2022-07-16 thomas #ifndef __linux__
1078 8a35f56c 2022-07-16 thomas sa_in6->sin6_len = sizeof(struct sockaddr_in6);
1080 8a35f56c 2022-07-16 thomas sa_in6->sin6_family = AF_INET6;
1081 8a35f56c 2022-07-16 thomas memcpy(&sa_in6->sin6_addr,
1082 8a35f56c 2022-07-16 thomas &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
1083 8a35f56c 2022-07-16 thomas sizeof(sa_in6->sin6_addr));
1084 8a35f56c 2022-07-16 thomas sa_in6->sin6_scope_id =
1085 8a35f56c 2022-07-16 thomas ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
1086 8a35f56c 2022-07-16 thomas if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1087 8a35f56c 2022-07-16 thomas sizeof(sa_in6->sin6_addr)) == 0)
1088 8a35f56c 2022-07-16 thomas h->prefixlen = 0; /* any address */
1090 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1091 8a35f56c 2022-07-16 thomas freeaddrinfo(res);
1094 8a35f56c 2022-07-16 thomas return (h);
1098 8a35f56c 2022-07-16 thomas host_dns(const char *s, struct addresslist *al, int max,
1099 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1101 8a35f56c 2022-07-16 thomas struct addrinfo hints, *res0, *res;
1102 8a35f56c 2022-07-16 thomas int error, cnt = 0;
1103 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1104 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1105 8a35f56c 2022-07-16 thomas struct address *h;
1107 8a35f56c 2022-07-16 thomas if ((cnt = host_if(s, al, max, port, ifname, ipproto)) != 0)
1108 8a35f56c 2022-07-16 thomas return (cnt);
1110 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
1111 8a35f56c 2022-07-16 thomas hints.ai_family = PF_UNSPEC;
1112 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1113 8a35f56c 2022-07-16 thomas hints.ai_flags = AI_ADDRCONFIG;
1114 8a35f56c 2022-07-16 thomas error = getaddrinfo(s, NULL, &hints, &res0);
1115 8a35f56c 2022-07-16 thomas if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1116 8a35f56c 2022-07-16 thomas return (0);
1117 8a35f56c 2022-07-16 thomas if (error) {
1118 8a35f56c 2022-07-16 thomas log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1119 8a35f56c 2022-07-16 thomas gai_strerror(error));
1120 8a35f56c 2022-07-16 thomas return (-1);
1123 8a35f56c 2022-07-16 thomas for (res = res0; res && cnt < max; res = res->ai_next) {
1124 8a35f56c 2022-07-16 thomas if (res->ai_family != AF_INET &&
1125 8a35f56c 2022-07-16 thomas res->ai_family != AF_INET6)
1126 8a35f56c 2022-07-16 thomas continue;
1127 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1128 8a35f56c 2022-07-16 thomas fatal(__func__);
1130 8a35f56c 2022-07-16 thomas if (port)
1131 8a35f56c 2022-07-16 thomas h->port = port;
1132 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1133 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1134 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1135 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1136 8a35f56c 2022-07-16 thomas __func__);
1137 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1138 8a35f56c 2022-07-16 thomas free(h);
1139 8a35f56c 2022-07-16 thomas return (-1);
1142 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1143 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1144 8a35f56c 2022-07-16 thomas h->ss.ss_family = res->ai_family;
1145 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1147 8a35f56c 2022-07-16 thomas if (res->ai_family == AF_INET) {
1148 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1149 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
1150 ff36aeea 2022-07-16 thomas #ifndef __linux__
1151 8a35f56c 2022-07-16 thomas sain->sin_len = sizeof(struct sockaddr_in);
1153 8a35f56c 2022-07-16 thomas sain->sin_addr.s_addr = ((struct sockaddr_in *)
1154 8a35f56c 2022-07-16 thomas res->ai_addr)->sin_addr.s_addr;
1155 8a35f56c 2022-07-16 thomas } else {
1156 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1157 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
1158 ff36aeea 2022-07-16 thomas #ifndef __linux__
1159 8a35f56c 2022-07-16 thomas sin6->sin6_len = sizeof(struct sockaddr_in6);
1161 8a35f56c 2022-07-16 thomas memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
1162 8a35f56c 2022-07-16 thomas res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
1165 8a35f56c 2022-07-16 thomas TAILQ_INSERT_HEAD(al, h, entry);
1168 8a35f56c 2022-07-16 thomas if (cnt == max && res) {
1169 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1170 8a35f56c 2022-07-16 thomas s, max);
1172 8a35f56c 2022-07-16 thomas freeaddrinfo(res0);
1173 8a35f56c 2022-07-16 thomas return (cnt);
1177 8a35f56c 2022-07-16 thomas host_if(const char *s, struct addresslist *al, int max,
1178 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1180 8a35f56c 2022-07-16 thomas struct ifaddrs *ifap, *p;
1181 8a35f56c 2022-07-16 thomas struct sockaddr_in *sain;
1182 8a35f56c 2022-07-16 thomas struct sockaddr_in6 *sin6;
1183 8a35f56c 2022-07-16 thomas struct address *h;
1184 8a35f56c 2022-07-16 thomas int cnt = 0, af;
1186 8a35f56c 2022-07-16 thomas if (getifaddrs(&ifap) == -1)
1187 8a35f56c 2022-07-16 thomas fatal("getifaddrs");
1189 8a35f56c 2022-07-16 thomas /* First search for IPv4 addresses */
1190 8a35f56c 2022-07-16 thomas af = AF_INET;
1193 8a35f56c 2022-07-16 thomas for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1194 8a35f56c 2022-07-16 thomas if (p->ifa_addr == NULL ||
1195 8a35f56c 2022-07-16 thomas p->ifa_addr->sa_family != af ||
1196 8a35f56c 2022-07-16 thomas (strcmp(s, p->ifa_name) != 0 &&
1197 8a35f56c 2022-07-16 thomas !is_if_in_group(p->ifa_name, s)))
1198 8a35f56c 2022-07-16 thomas continue;
1199 8a35f56c 2022-07-16 thomas if ((h = calloc(1, sizeof(*h))) == NULL)
1200 8a35f56c 2022-07-16 thomas fatal("calloc");
1202 8a35f56c 2022-07-16 thomas if (port)
1203 8a35f56c 2022-07-16 thomas h->port = port;
1204 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1205 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1206 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1207 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1208 8a35f56c 2022-07-16 thomas __func__);
1209 8a35f56c 2022-07-16 thomas free(h);
1210 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1211 8a35f56c 2022-07-16 thomas return (-1);
1214 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1215 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1216 8a35f56c 2022-07-16 thomas h->ss.ss_family = af;
1217 8a35f56c 2022-07-16 thomas h->prefixlen = -1; /* host address */
1219 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1220 8a35f56c 2022-07-16 thomas sain = (struct sockaddr_in *)&h->ss;
1221 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
1222 ff36aeea 2022-07-16 thomas #ifndef __linux__
1223 8a35f56c 2022-07-16 thomas sain->sin_len = sizeof(struct sockaddr_in);
1225 8a35f56c 2022-07-16 thomas sain->sin_addr.s_addr = ((struct sockaddr_in *)
1226 8a35f56c 2022-07-16 thomas p->ifa_addr)->sin_addr.s_addr;
1227 8a35f56c 2022-07-16 thomas } else {
1228 8a35f56c 2022-07-16 thomas sin6 = (struct sockaddr_in6 *)&h->ss;
1229 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
1230 ff36aeea 2022-07-16 thomas #ifndef __linux__
1231 8a35f56c 2022-07-16 thomas sin6->sin6_len = sizeof(struct sockaddr_in6);
1233 8a35f56c 2022-07-16 thomas memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
1234 8a35f56c 2022-07-16 thomas p->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
1235 8a35f56c 2022-07-16 thomas sin6->sin6_scope_id = ((struct sockaddr_in6 *)
1236 8a35f56c 2022-07-16 thomas p->ifa_addr)->sin6_scope_id;
1239 8a35f56c 2022-07-16 thomas TAILQ_INSERT_HEAD(al, h, entry);
1242 8a35f56c 2022-07-16 thomas if (af == AF_INET) {
1243 8a35f56c 2022-07-16 thomas /* Next search for IPv6 addresses */
1244 8a35f56c 2022-07-16 thomas af = AF_INET6;
1245 8a35f56c 2022-07-16 thomas goto nextaf;
1248 8a35f56c 2022-07-16 thomas if (cnt > max) {
1249 8a35f56c 2022-07-16 thomas log_warnx("%s: %s resolves to more than %d hosts", __func__,
1250 8a35f56c 2022-07-16 thomas s, max);
1252 8a35f56c 2022-07-16 thomas freeifaddrs(ifap);
1253 8a35f56c 2022-07-16 thomas return (cnt);
1257 8a35f56c 2022-07-16 thomas host(const char *s, struct addresslist *al, int max,
1258 8a35f56c 2022-07-16 thomas in_port_t port, const char *ifname, int ipproto)
1260 8a35f56c 2022-07-16 thomas struct address *h;
1262 8a35f56c 2022-07-16 thomas h = host_v4(s);
1264 8a35f56c 2022-07-16 thomas /* IPv6 address? */
1265 8a35f56c 2022-07-16 thomas if (h == NULL)
1266 8a35f56c 2022-07-16 thomas h = host_v6(s);
1268 8a35f56c 2022-07-16 thomas if (h != NULL) {
1269 8a35f56c 2022-07-16 thomas if (port)
1270 8a35f56c 2022-07-16 thomas h->port = port;
1271 8a35f56c 2022-07-16 thomas if (ifname != NULL) {
1272 8a35f56c 2022-07-16 thomas if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1273 8a35f56c 2022-07-16 thomas sizeof(h->ifname)) {
1274 8a35f56c 2022-07-16 thomas log_warnx("%s: interface name truncated",
1275 8a35f56c 2022-07-16 thomas __func__);
1276 8a35f56c 2022-07-16 thomas free(h);
1277 8a35f56c 2022-07-16 thomas return (-1);
1280 8a35f56c 2022-07-16 thomas if (ipproto != -1)
1281 8a35f56c 2022-07-16 thomas h->ipproto = ipproto;
1283 8a35f56c 2022-07-16 thomas TAILQ_INSERT_HEAD(al, h, entry);
1284 8a35f56c 2022-07-16 thomas return (1);
1287 8a35f56c 2022-07-16 thomas return (host_dns(s, al, max, port, ifname, ipproto));
1291 8a35f56c 2022-07-16 thomas is_if_in_group(const char *ifname, const char *groupname)
1293 ff36aeea 2022-07-16 thomas /* TA: Check this... */
1294 ff36aeea 2022-07-16 thomas #ifdef HAVE_STRUCT_IFGROUPREQ
1295 8a35f56c 2022-07-16 thomas unsigned int len;
1296 8a35f56c 2022-07-16 thomas struct ifgroupreq ifgr;
1297 8a35f56c 2022-07-16 thomas struct ifg_req *ifg;
1299 8a35f56c 2022-07-16 thomas int ret = 0;
1301 8a35f56c 2022-07-16 thomas if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1302 8a35f56c 2022-07-16 thomas err(1, "socket");
1304 8a35f56c 2022-07-16 thomas memset(&ifgr, 0, sizeof(ifgr));
1305 8a35f56c 2022-07-16 thomas if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1306 8a35f56c 2022-07-16 thomas err(1, "IFNAMSIZ");
1307 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1308 8a35f56c 2022-07-16 thomas if (errno == EINVAL || errno == ENOTTY)
1309 8a35f56c 2022-07-16 thomas goto end;
1310 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1313 8a35f56c 2022-07-16 thomas len = ifgr.ifgr_len;
1314 8a35f56c 2022-07-16 thomas ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1315 8a35f56c 2022-07-16 thomas sizeof(struct ifg_req));
1316 8a35f56c 2022-07-16 thomas if (ifgr.ifgr_groups == NULL)
1317 8a35f56c 2022-07-16 thomas err(1, "getifgroups");
1318 8a35f56c 2022-07-16 thomas if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1319 8a35f56c 2022-07-16 thomas err(1, "SIOCGIFGROUP");
1321 8a35f56c 2022-07-16 thomas ifg = ifgr.ifgr_groups;
1322 8a35f56c 2022-07-16 thomas for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1323 8a35f56c 2022-07-16 thomas len -= sizeof(struct ifg_req);
1324 8a35f56c 2022-07-16 thomas if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1325 8a35f56c 2022-07-16 thomas ret = 1;
1329 8a35f56c 2022-07-16 thomas free(ifgr.ifgr_groups);
1332 8a35f56c 2022-07-16 thomas close(s);
1333 8a35f56c 2022-07-16 thomas return (ret);
1335 ff36aeea 2022-07-16 thomas return (0);
1340 8a35f56c 2022-07-16 thomas get_addrs(const char *addr, struct addresslist *al, in_port_t port)
1342 8a35f56c 2022-07-16 thomas if (strcmp("", addr) == 0) {
1343 8a35f56c 2022-07-16 thomas if (host("0.0.0.0", al, 1, port, "0.0.0.0", -1) <= 0) {
1344 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s",
1345 8a35f56c 2022-07-16 thomas "0.0.0.0");
1346 8a35f56c 2022-07-16 thomas return (-1);
1348 8a35f56c 2022-07-16 thomas if (host("::", al, 1, port, "::", -1) <= 0) {
1349 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s", "::");
1350 8a35f56c 2022-07-16 thomas return (-1);
1352 8a35f56c 2022-07-16 thomas } else {
1353 8a35f56c 2022-07-16 thomas if (host(addr, al, GOTWEBD_MAXIFACE, port, addr,
1354 8a35f56c 2022-07-16 thomas -1) <= 0) {
1355 8a35f56c 2022-07-16 thomas yyerror("invalid listen ip: %s", addr);
1356 8a35f56c 2022-07-16 thomas return (-1);
1359 8a35f56c 2022-07-16 thomas return (0);