2 a596b957 2022-07-14 tracey * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 a596b957 2022-07-14 tracey * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 a596b957 2022-07-14 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 a596b957 2022-07-14 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
11 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
12 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
14 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 a596b957 2022-07-14 tracey #include <sys/ioctl.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
26 a596b957 2022-07-14 tracey #include <sys/queue.h>
27 a596b957 2022-07-14 tracey #include <sys/socket.h>
28 a596b957 2022-07-14 tracey #include <sys/stat.h>
30 a596b957 2022-07-14 tracey #include <net/if.h>
31 a596b957 2022-07-14 tracey #include <netinet/in.h>
33 a596b957 2022-07-14 tracey #include <arpa/inet.h>
35 a596b957 2022-07-14 tracey #include <ctype.h>
36 a596b957 2022-07-14 tracey #include <err.h>
37 a596b957 2022-07-14 tracey #include <errno.h>
38 a596b957 2022-07-14 tracey #include <event.h>
39 a596b957 2022-07-14 tracey #include <ifaddrs.h>
40 a596b957 2022-07-14 tracey #include <imsg.h>
41 a596b957 2022-07-14 tracey #include <limits.h>
42 a596b957 2022-07-14 tracey #include <netdb.h>
43 a596b957 2022-07-14 tracey #include <stdarg.h>
44 a596b957 2022-07-14 tracey #include <stdlib.h>
45 a596b957 2022-07-14 tracey #include <stdio.h>
46 a596b957 2022-07-14 tracey #include <string.h>
47 a596b957 2022-07-14 tracey #include <syslog.h>
48 a596b957 2022-07-14 tracey #include <unistd.h>
50 df2d3cd2 2023-03-11 op #include "got_sockaddr.h"
51 df2d3cd2 2023-03-11 op #include "got_reference.h"
53 a596b957 2022-07-14 tracey #include "proc.h"
54 a596b957 2022-07-14 tracey #include "gotwebd.h"
56 a596b957 2022-07-14 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
57 a596b957 2022-07-14 tracey static struct file {
58 a596b957 2022-07-14 tracey TAILQ_ENTRY(file) entry;
59 a596b957 2022-07-14 tracey FILE *stream;
60 a596b957 2022-07-14 tracey char *name;
61 a596b957 2022-07-14 tracey int lineno;
62 a596b957 2022-07-14 tracey int errors;
64 a596b957 2022-07-14 tracey struct file *newfile(const char *, int);
65 a596b957 2022-07-14 tracey static void closefile(struct file *);
66 a596b957 2022-07-14 tracey int check_file_secrecy(int, const char *);
67 a596b957 2022-07-14 tracey int yyparse(void);
68 a596b957 2022-07-14 tracey int yylex(void);
69 a596b957 2022-07-14 tracey int yyerror(const char *, ...)
70 a596b957 2022-07-14 tracey __attribute__((__format__ (printf, 1, 2)))
71 a596b957 2022-07-14 tracey __attribute__((__nonnull__ (1)));
72 a596b957 2022-07-14 tracey int kw_cmp(const void *, const void *);
73 a596b957 2022-07-14 tracey int lookup(char *);
74 a596b957 2022-07-14 tracey int lgetc(int);
75 a596b957 2022-07-14 tracey int lungetc(int);
76 a596b957 2022-07-14 tracey int findeol(void);
78 a596b957 2022-07-14 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 a596b957 2022-07-14 tracey struct sym {
80 a596b957 2022-07-14 tracey TAILQ_ENTRY(sym) entry;
82 a596b957 2022-07-14 tracey int persist;
83 a596b957 2022-07-14 tracey char *nam;
84 a596b957 2022-07-14 tracey char *val;
87 a596b957 2022-07-14 tracey int symset(const char *, const char *, int);
88 a596b957 2022-07-14 tracey char *symget(const char *);
90 a596b957 2022-07-14 tracey static int errors;
92 a596b957 2022-07-14 tracey static struct gotwebd *gotwebd;
93 a596b957 2022-07-14 tracey static struct server *new_srv;
94 a596b957 2022-07-14 tracey static struct server *conf_new_server(const char *);
95 a596b957 2022-07-14 tracey int getservice(const char *);
98 67d8de2a 2022-08-29 stsp int get_addrs(const char *, struct server *, in_port_t);
99 67d8de2a 2022-08-29 stsp int addr_dup_check(struct addresslist *, struct address *,
100 67d8de2a 2022-08-29 stsp const char *, const char *);
101 67d8de2a 2022-08-29 stsp int add_addr(struct server *, struct address *);
102 abf3e3f4 2023-05-29 op int host(const char *, struct server *,
103 a3dc2f5d 2023-11-03 op int, in_port_t, const char *);
105 a596b957 2022-07-14 tracey typedef struct {
107 a596b957 2022-07-14 tracey long long number;
108 a596b957 2022-07-14 tracey char *string;
109 a596b957 2022-07-14 tracey in_port_t port;
111 a596b957 2022-07-14 tracey int lineno;
112 a596b957 2022-07-14 tracey } YYSTYPE;
116 d8473d93 2022-08-11 stsp %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
117 a596b957 2022-07-14 tracey %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
118 a596b957 2022-07-14 tracey %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
119 d5996b9e 2022-10-31 landry %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
120 3a1c1a1b 2023-01-04 op %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
122 a596b957 2022-07-14 tracey %token <v.string> STRING
123 a596b957 2022-07-14 tracey %type <v.port> fcgiport
124 a596b957 2022-07-14 tracey %token <v.number> NUMBER
125 a596b957 2022-07-14 tracey %type <v.number> boolean
129 47b307cd 2022-10-02 op grammar : /* empty */
130 a596b957 2022-07-14 tracey | grammar '\n'
131 47b307cd 2022-10-02 op | grammar varset '\n'
132 a596b957 2022-07-14 tracey | grammar main '\n'
133 a596b957 2022-07-14 tracey | grammar server '\n'
134 47b307cd 2022-10-02 op | grammar error '\n' { file->errors++; }
137 47b307cd 2022-10-02 op varset : STRING '=' STRING {
138 47b307cd 2022-10-02 op char *s = $1;
139 47b307cd 2022-10-02 op while (*s++) {
140 47b307cd 2022-10-02 op if (isspace((unsigned char)*s)) {
141 47b307cd 2022-10-02 op yyerror("macro name cannot contain "
142 47b307cd 2022-10-02 op "whitespace");
148 47b307cd 2022-10-02 op if (symset($1, $3, 0) == -1)
149 47b307cd 2022-10-02 op fatal("cannot store variable");
155 a596b957 2022-07-14 tracey boolean : STRING {
156 a596b957 2022-07-14 tracey if (strcasecmp($1, "1") == 0 ||
157 a596b957 2022-07-14 tracey strcasecmp($1, "on") == 0)
159 a596b957 2022-07-14 tracey else if (strcasecmp($1, "0") == 0 ||
160 031687ba 2023-06-15 op strcasecmp($1, "off") == 0)
163 a596b957 2022-07-14 tracey yyerror("invalid boolean value '%s'", $1);
164 a596b957 2022-07-14 tracey free($1);
167 a596b957 2022-07-14 tracey free($1);
169 a596b957 2022-07-14 tracey | ON { $$ = 1; }
171 1a0c81fb 2023-06-15 op if ($1 != 0 && $1 != 1) {
172 1a0c81fb 2023-06-15 op yyerror("invalid boolean value '%lld'", $1);
179 17132eaa 2022-08-29 stsp fcgiport : PORT NUMBER {
180 17132eaa 2022-08-29 stsp if ($2 <= 0 || $2 > (int)USHRT_MAX) {
181 17132eaa 2022-08-29 stsp yyerror("invalid port: %lld", $2);
186 17132eaa 2022-08-29 stsp | PORT STRING {
189 17132eaa 2022-08-29 stsp if ((val = getservice($2)) == -1) {
190 17132eaa 2022-08-29 stsp yyerror("invalid port: %s", $2);
196 a596b957 2022-07-14 tracey $$ = val;
200 a596b957 2022-07-14 tracey main : PREFORK NUMBER {
201 1a0c81fb 2023-06-15 op if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
202 1a0c81fb 2023-06-15 op yyerror("prefork is %s: %lld",
203 1a0c81fb 2023-06-15 op $2 <= 0 ? "too small" : "too large", $2);
206 a596b957 2022-07-14 tracey gotwebd->prefork_gotwebd = $2;
208 a596b957 2022-07-14 tracey | CHROOT STRING {
209 a678036d 2023-06-15 op if (*$2 == '\0') {
210 a678036d 2023-06-15 op yyerror("chroot path can't be an empty"
216 a596b957 2022-07-14 tracey n = strlcpy(gotwebd->httpd_chroot, $2,
217 a596b957 2022-07-14 tracey sizeof(gotwebd->httpd_chroot));
218 a596b957 2022-07-14 tracey if (n >= sizeof(gotwebd->httpd_chroot)) {
219 a596b957 2022-07-14 tracey yyerror("%s: httpd_chroot truncated", __func__);
220 a596b957 2022-07-14 tracey free($2);
223 a596b957 2022-07-14 tracey free($2);
225 a596b957 2022-07-14 tracey | UNIX_SOCKET boolean {
226 a596b957 2022-07-14 tracey gotwebd->unix_socket = $2;
228 a596b957 2022-07-14 tracey | UNIX_SOCKET_NAME STRING {
229 a596b957 2022-07-14 tracey n = snprintf(gotwebd->unix_socket_name,
230 a596b957 2022-07-14 tracey sizeof(gotwebd->unix_socket_name), "%s%s",
231 f4a5cef1 2023-06-15 op gotwebd->httpd_chroot, $2);
233 438d0cc3 2022-08-16 op (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
234 a596b957 2022-07-14 tracey yyerror("%s: unix_socket_name truncated",
235 a596b957 2022-07-14 tracey __func__);
236 a596b957 2022-07-14 tracey free($2);
239 a596b957 2022-07-14 tracey free($2);
243 a596b957 2022-07-14 tracey server : SERVER STRING {
244 a596b957 2022-07-14 tracey struct server *srv;
246 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
247 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
248 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
249 a596b957 2022-07-14 tracey free($2);
254 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
255 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
256 a596b957 2022-07-14 tracey free($2);
258 a596b957 2022-07-14 tracey | SERVER STRING {
259 a596b957 2022-07-14 tracey struct server *srv;
261 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
262 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
263 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
264 a596b957 2022-07-14 tracey free($2);
269 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
270 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
271 a596b957 2022-07-14 tracey free($2);
272 a596b957 2022-07-14 tracey } '{' optnl serveropts2 '}' {
276 a596b957 2022-07-14 tracey serveropts1 : REPOS_PATH STRING {
277 a596b957 2022-07-14 tracey n = strlcpy(new_srv->repos_path, $2,
278 a596b957 2022-07-14 tracey sizeof(new_srv->repos_path));
279 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->repos_path)) {
280 a596b957 2022-07-14 tracey yyerror("%s: repos_path truncated", __func__);
281 a596b957 2022-07-14 tracey free($2);
284 a596b957 2022-07-14 tracey free($2);
286 a596b957 2022-07-14 tracey | SITE_NAME STRING {
287 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_name, $2,
288 a596b957 2022-07-14 tracey sizeof(new_srv->site_name));
289 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_name)) {
290 a596b957 2022-07-14 tracey yyerror("%s: site_name truncated", __func__);
291 a596b957 2022-07-14 tracey free($2);
294 a596b957 2022-07-14 tracey free($2);
296 a596b957 2022-07-14 tracey | SITE_OWNER STRING {
297 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_owner, $2,
298 a596b957 2022-07-14 tracey sizeof(new_srv->site_owner));
299 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_owner)) {
300 a596b957 2022-07-14 tracey yyerror("%s: site_owner truncated", __func__);
301 a596b957 2022-07-14 tracey free($2);
304 a596b957 2022-07-14 tracey free($2);
306 a596b957 2022-07-14 tracey | SITE_LINK STRING {
307 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_link, $2,
308 a596b957 2022-07-14 tracey sizeof(new_srv->site_link));
309 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_link)) {
310 a596b957 2022-07-14 tracey yyerror("%s: site_link truncated", __func__);
311 a596b957 2022-07-14 tracey free($2);
314 a596b957 2022-07-14 tracey free($2);
316 a596b957 2022-07-14 tracey | LOGO STRING {
317 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
318 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo)) {
319 a596b957 2022-07-14 tracey yyerror("%s: logo truncated", __func__);
320 a596b957 2022-07-14 tracey free($2);
323 a596b957 2022-07-14 tracey free($2);
325 a596b957 2022-07-14 tracey | LOGO_URL STRING {
326 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo_url, $2,
327 a596b957 2022-07-14 tracey sizeof(new_srv->logo_url));
328 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo_url)) {
329 a596b957 2022-07-14 tracey yyerror("%s: logo_url truncated", __func__);
330 a596b957 2022-07-14 tracey free($2);
333 a596b957 2022-07-14 tracey free($2);
335 a596b957 2022-07-14 tracey | CUSTOM_CSS STRING {
336 a596b957 2022-07-14 tracey n = strlcpy(new_srv->custom_css, $2,
337 a596b957 2022-07-14 tracey sizeof(new_srv->custom_css));
338 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->custom_css)) {
339 a596b957 2022-07-14 tracey yyerror("%s: custom_css truncated", __func__);
340 a596b957 2022-07-14 tracey free($2);
343 a596b957 2022-07-14 tracey free($2);
345 17132eaa 2022-08-29 stsp | LISTEN ON STRING fcgiport {
346 67d8de2a 2022-08-29 stsp if (get_addrs($3, new_srv, $4) == -1) {
347 67d8de2a 2022-08-29 stsp yyerror("could not get addrs");
350 6c8aa58f 2022-08-30 stsp new_srv->fcgi_socket = 1;
352 3a1c1a1b 2023-01-04 op | LISTEN ON SOCKET STRING {
353 031687ba 2023-06-15 op if (strcasecmp($4, "off") == 0) {
354 3a1c1a1b 2023-01-04 op new_srv->unix_socket = 0;
359 3a1c1a1b 2023-01-04 op new_srv->unix_socket = 1;
361 3a1c1a1b 2023-01-04 op n = snprintf(new_srv->unix_socket_name,
362 3a1c1a1b 2023-01-04 op sizeof(new_srv->unix_socket_name), "%s%s",
363 f4a5cef1 2023-06-15 op gotwebd->httpd_chroot, $4);
365 3a1c1a1b 2023-01-04 op (size_t)n >= sizeof(new_srv->unix_socket_name)) {
366 3a1c1a1b 2023-01-04 op yyerror("%s: unix_socket_name truncated",
373 a596b957 2022-07-14 tracey | MAX_REPOS NUMBER {
374 1a0c81fb 2023-06-15 op if ($2 <= 0) {
375 1a0c81fb 2023-06-15 op yyerror("max_repos is too small: %lld", $2);
378 1a0c81fb 2023-06-15 op new_srv->max_repos = $2;
380 a596b957 2022-07-14 tracey | SHOW_SITE_OWNER boolean {
381 a596b957 2022-07-14 tracey new_srv->show_site_owner = $2;
383 a596b957 2022-07-14 tracey | SHOW_REPO_OWNER boolean {
384 a596b957 2022-07-14 tracey new_srv->show_repo_owner = $2;
386 a596b957 2022-07-14 tracey | SHOW_REPO_AGE boolean {
387 a596b957 2022-07-14 tracey new_srv->show_repo_age = $2;
389 a596b957 2022-07-14 tracey | SHOW_REPO_DESCRIPTION boolean {
390 a596b957 2022-07-14 tracey new_srv->show_repo_description = $2;
392 a596b957 2022-07-14 tracey | SHOW_REPO_CLONEURL boolean {
393 a596b957 2022-07-14 tracey new_srv->show_repo_cloneurl = $2;
395 d5996b9e 2022-10-31 landry | RESPECT_EXPORTOK boolean {
396 d5996b9e 2022-10-31 landry new_srv->respect_exportok = $2;
398 a596b957 2022-07-14 tracey | MAX_REPOS_DISPLAY NUMBER {
399 20f27972 2023-06-19 op if ($2 < 0) {
400 1a0c81fb 2023-06-15 op yyerror("max_repos_display is too small: %lld",
404 1a0c81fb 2023-06-15 op new_srv->max_repos_display = $2;
406 a596b957 2022-07-14 tracey | MAX_COMMITS_DISPLAY NUMBER {
407 f4425f95 2023-06-14 op if ($2 <= 1) {
408 f4425f95 2023-06-14 op yyerror("max_commits_display is too small:"
409 f4425f95 2023-06-14 op " %lld", $2);
412 f4425f95 2023-06-14 op new_srv->max_commits_display = $2;
416 a596b957 2022-07-14 tracey serveropts2 : serveropts2 serveropts1 nl
417 a596b957 2022-07-14 tracey | serveropts1 optnl
420 a596b957 2022-07-14 tracey nl : '\n' optnl
423 a596b957 2022-07-14 tracey optnl : '\n' optnl /* zero or more newlines */
424 a596b957 2022-07-14 tracey | /* empty */
429 a596b957 2022-07-14 tracey struct keywords {
430 a596b957 2022-07-14 tracey const char *k_name;
431 a596b957 2022-07-14 tracey int k_val;
435 a596b957 2022-07-14 tracey yyerror(const char *fmt, ...)
437 a596b957 2022-07-14 tracey va_list ap;
438 a596b957 2022-07-14 tracey char *msg;
440 a596b957 2022-07-14 tracey file->errors++;
441 a596b957 2022-07-14 tracey va_start(ap, fmt);
442 a596b957 2022-07-14 tracey if (vasprintf(&msg, fmt, ap) == -1)
443 a596b957 2022-07-14 tracey fatalx("yyerror vasprintf");
444 a596b957 2022-07-14 tracey va_end(ap);
445 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
446 a596b957 2022-07-14 tracey free(msg);
447 a596b957 2022-07-14 tracey return (0);
451 a596b957 2022-07-14 tracey kw_cmp(const void *k, const void *e)
453 a596b957 2022-07-14 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
457 a596b957 2022-07-14 tracey lookup(char *s)
459 a596b957 2022-07-14 tracey /* This has to be sorted always. */
460 a596b957 2022-07-14 tracey static const struct keywords keywords[] = {
461 a596b957 2022-07-14 tracey { "chroot", CHROOT },
462 a596b957 2022-07-14 tracey { "custom_css", CUSTOM_CSS },
463 d8473d93 2022-08-11 stsp { "listen", LISTEN },
464 a596b957 2022-07-14 tracey { "logo", LOGO },
465 8556b86b 2023-01-02 op { "logo_url", LOGO_URL },
466 a596b957 2022-07-14 tracey { "max_commits_display", MAX_COMMITS_DISPLAY },
467 a596b957 2022-07-14 tracey { "max_repos", MAX_REPOS },
468 a596b957 2022-07-14 tracey { "max_repos_display", MAX_REPOS_DISPLAY },
469 d8473d93 2022-08-11 stsp { "on", ON },
470 a596b957 2022-07-14 tracey { "port", PORT },
471 a596b957 2022-07-14 tracey { "prefork", PREFORK },
472 a596b957 2022-07-14 tracey { "repos_path", REPOS_PATH },
473 d5996b9e 2022-10-31 landry { "respect_exportok", RESPECT_EXPORTOK },
474 a596b957 2022-07-14 tracey { "server", SERVER },
475 a596b957 2022-07-14 tracey { "show_repo_age", SHOW_REPO_AGE },
476 a596b957 2022-07-14 tracey { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
477 a596b957 2022-07-14 tracey { "show_repo_description", SHOW_REPO_DESCRIPTION },
478 a596b957 2022-07-14 tracey { "show_repo_owner", SHOW_REPO_OWNER },
479 a596b957 2022-07-14 tracey { "show_site_owner", SHOW_SITE_OWNER },
480 a596b957 2022-07-14 tracey { "site_link", SITE_LINK },
481 a596b957 2022-07-14 tracey { "site_name", SITE_NAME },
482 a596b957 2022-07-14 tracey { "site_owner", SITE_OWNER },
483 3a1c1a1b 2023-01-04 op { "socket", SOCKET },
484 a596b957 2022-07-14 tracey { "unix_socket", UNIX_SOCKET },
485 a596b957 2022-07-14 tracey { "unix_socket_name", UNIX_SOCKET_NAME },
487 a596b957 2022-07-14 tracey const struct keywords *p;
489 a596b957 2022-07-14 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
490 a596b957 2022-07-14 tracey sizeof(keywords[0]), kw_cmp);
493 a596b957 2022-07-14 tracey return (p->k_val);
495 a596b957 2022-07-14 tracey return (STRING);
498 a596b957 2022-07-14 tracey #define MAXPUSHBACK 128
500 a596b957 2022-07-14 tracey unsigned char *parsebuf;
501 a596b957 2022-07-14 tracey int parseindex;
502 a596b957 2022-07-14 tracey unsigned char pushback_buffer[MAXPUSHBACK];
503 a596b957 2022-07-14 tracey int pushback_index = 0;
506 a596b957 2022-07-14 tracey lgetc(int quotec)
508 a596b957 2022-07-14 tracey int c, next;
510 a596b957 2022-07-14 tracey if (parsebuf) {
511 a596b957 2022-07-14 tracey /* Read character from the parsebuffer instead of input. */
512 a596b957 2022-07-14 tracey if (parseindex >= 0) {
513 a596b957 2022-07-14 tracey c = parsebuf[parseindex++];
514 a596b957 2022-07-14 tracey if (c != '\0')
515 a596b957 2022-07-14 tracey return (c);
516 a596b957 2022-07-14 tracey parsebuf = NULL;
518 a596b957 2022-07-14 tracey parseindex++;
521 a596b957 2022-07-14 tracey if (pushback_index)
522 a596b957 2022-07-14 tracey return (pushback_buffer[--pushback_index]);
524 a596b957 2022-07-14 tracey if (quotec) {
525 a596b957 2022-07-14 tracey c = getc(file->stream);
526 a596b957 2022-07-14 tracey if (c == EOF)
527 a596b957 2022-07-14 tracey yyerror("reached end of file while parsing "
528 a596b957 2022-07-14 tracey "quoted string");
529 a596b957 2022-07-14 tracey return (c);
532 a596b957 2022-07-14 tracey c = getc(file->stream);
533 a596b957 2022-07-14 tracey while (c == '\\') {
534 a596b957 2022-07-14 tracey next = getc(file->stream);
535 a596b957 2022-07-14 tracey if (next != '\n') {
536 a596b957 2022-07-14 tracey c = next;
539 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
540 a596b957 2022-07-14 tracey file->lineno++;
541 a596b957 2022-07-14 tracey c = getc(file->stream);
544 a596b957 2022-07-14 tracey return (c);
548 a596b957 2022-07-14 tracey lungetc(int c)
550 a596b957 2022-07-14 tracey if (c == EOF)
551 a596b957 2022-07-14 tracey return (EOF);
552 a596b957 2022-07-14 tracey if (parsebuf) {
553 a596b957 2022-07-14 tracey parseindex--;
554 a596b957 2022-07-14 tracey if (parseindex >= 0)
555 a596b957 2022-07-14 tracey return (c);
557 a596b957 2022-07-14 tracey if (pushback_index < MAXPUSHBACK-1)
558 a596b957 2022-07-14 tracey return (pushback_buffer[pushback_index++] = c);
560 a596b957 2022-07-14 tracey return (EOF);
564 a596b957 2022-07-14 tracey findeol(void)
568 a596b957 2022-07-14 tracey parsebuf = NULL;
570 a596b957 2022-07-14 tracey /* Skip to either EOF or the first real EOL. */
571 a596b957 2022-07-14 tracey while (1) {
572 a596b957 2022-07-14 tracey if (pushback_index)
573 a596b957 2022-07-14 tracey c = pushback_buffer[--pushback_index];
575 a596b957 2022-07-14 tracey c = lgetc(0);
576 a596b957 2022-07-14 tracey if (c == '\n') {
577 a596b957 2022-07-14 tracey file->lineno++;
580 a596b957 2022-07-14 tracey if (c == EOF)
583 a596b957 2022-07-14 tracey return (ERROR);
587 a596b957 2022-07-14 tracey yylex(void)
589 a596b957 2022-07-14 tracey unsigned char buf[8096];
590 a596b957 2022-07-14 tracey unsigned char *p, *val;
591 a596b957 2022-07-14 tracey int quotec, next, c;
592 a596b957 2022-07-14 tracey int token;
596 a596b957 2022-07-14 tracey c = lgetc(0);
597 a596b957 2022-07-14 tracey while (c == ' ' || c == '\t')
598 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
600 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
601 a596b957 2022-07-14 tracey if (c == '#') {
602 a596b957 2022-07-14 tracey c = lgetc(0);
603 a596b957 2022-07-14 tracey while (c != '\n' && c != EOF)
604 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
606 a596b957 2022-07-14 tracey if (c == '$' && parsebuf == NULL) {
607 a596b957 2022-07-14 tracey while (1) {
608 a596b957 2022-07-14 tracey c = lgetc(0);
609 a596b957 2022-07-14 tracey if (c == EOF)
610 a596b957 2022-07-14 tracey return (0);
612 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
613 a596b957 2022-07-14 tracey yyerror("string too long");
614 a596b957 2022-07-14 tracey return (findeol());
616 a596b957 2022-07-14 tracey if (isalnum(c) || c == '_') {
617 a596b957 2022-07-14 tracey *p++ = c;
618 a596b957 2022-07-14 tracey continue;
620 a596b957 2022-07-14 tracey *p = '\0';
621 a596b957 2022-07-14 tracey lungetc(c);
624 a596b957 2022-07-14 tracey val = symget(buf);
625 a596b957 2022-07-14 tracey if (val == NULL) {
626 a596b957 2022-07-14 tracey yyerror("macro '%s' not defined", buf);
627 a596b957 2022-07-14 tracey return (findeol());
629 a596b957 2022-07-14 tracey parsebuf = val;
630 a596b957 2022-07-14 tracey parseindex = 0;
631 a596b957 2022-07-14 tracey goto top;
634 a596b957 2022-07-14 tracey switch (c) {
635 a596b957 2022-07-14 tracey case '\'':
636 a596b957 2022-07-14 tracey case '"':
637 a596b957 2022-07-14 tracey quotec = c;
638 a596b957 2022-07-14 tracey while (1) {
639 a596b957 2022-07-14 tracey c = lgetc(quotec);
640 a596b957 2022-07-14 tracey if (c == EOF)
641 a596b957 2022-07-14 tracey return (0);
642 a596b957 2022-07-14 tracey if (c == '\n') {
643 a596b957 2022-07-14 tracey file->lineno++;
644 a596b957 2022-07-14 tracey continue;
645 a596b957 2022-07-14 tracey } else if (c == '\\') {
646 a596b957 2022-07-14 tracey next = lgetc(quotec);
647 a596b957 2022-07-14 tracey if (next == EOF)
648 a596b957 2022-07-14 tracey return (0);
649 a596b957 2022-07-14 tracey if (next == quotec || c == ' ' || c == '\t')
650 a596b957 2022-07-14 tracey c = next;
651 a596b957 2022-07-14 tracey else if (next == '\n') {
652 a596b957 2022-07-14 tracey file->lineno++;
653 a596b957 2022-07-14 tracey continue;
655 a596b957 2022-07-14 tracey lungetc(next);
656 a596b957 2022-07-14 tracey } else if (c == quotec) {
657 a596b957 2022-07-14 tracey *p = '\0';
659 a596b957 2022-07-14 tracey } else if (c == '\0') {
660 a596b957 2022-07-14 tracey yyerror("syntax error");
661 a596b957 2022-07-14 tracey return (findeol());
663 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
664 a596b957 2022-07-14 tracey yyerror("string too long");
665 a596b957 2022-07-14 tracey return (findeol());
667 a596b957 2022-07-14 tracey *p++ = c;
669 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
670 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
671 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
672 a596b957 2022-07-14 tracey return (STRING);
675 a596b957 2022-07-14 tracey #define allowed_to_end_number(x) \
676 a596b957 2022-07-14 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
678 a596b957 2022-07-14 tracey if (c == '-' || isdigit(c)) {
680 a596b957 2022-07-14 tracey *p++ = c;
681 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
682 a596b957 2022-07-14 tracey yyerror("string too long");
683 a596b957 2022-07-14 tracey return (findeol());
685 a596b957 2022-07-14 tracey c = lgetc(0);
686 a596b957 2022-07-14 tracey } while (c != EOF && isdigit(c));
687 a596b957 2022-07-14 tracey lungetc(c);
688 a596b957 2022-07-14 tracey if (p == buf + 1 && buf[0] == '-')
689 a596b957 2022-07-14 tracey goto nodigits;
690 a596b957 2022-07-14 tracey if (c == EOF || allowed_to_end_number(c)) {
691 a596b957 2022-07-14 tracey const char *errstr = NULL;
693 a596b957 2022-07-14 tracey *p = '\0';
694 a596b957 2022-07-14 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
695 a596b957 2022-07-14 tracey LLONG_MAX, &errstr);
696 a596b957 2022-07-14 tracey if (errstr) {
697 a596b957 2022-07-14 tracey yyerror("\"%s\" invalid number: %s",
698 a596b957 2022-07-14 tracey buf, errstr);
699 a596b957 2022-07-14 tracey return (findeol());
701 a596b957 2022-07-14 tracey return (NUMBER);
703 a596b957 2022-07-14 tracey nodigits:
704 a596b957 2022-07-14 tracey while (p > buf + 1)
705 a596b957 2022-07-14 tracey lungetc(*--p);
706 a596b957 2022-07-14 tracey c = *--p;
707 a596b957 2022-07-14 tracey if (c == '-')
708 a596b957 2022-07-14 tracey return (c);
712 a596b957 2022-07-14 tracey #define allowed_in_string(x) \
713 a596b957 2022-07-14 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
714 a596b957 2022-07-14 tracey x != '{' && x != '}' && \
715 a596b957 2022-07-14 tracey x != '!' && x != '=' && x != '#' && \
716 a596b957 2022-07-14 tracey x != ','))
718 a596b957 2022-07-14 tracey if (isalnum(c) || c == ':' || c == '_') {
720 a596b957 2022-07-14 tracey *p++ = c;
721 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
722 a596b957 2022-07-14 tracey yyerror("string too long");
723 a596b957 2022-07-14 tracey return (findeol());
725 a596b957 2022-07-14 tracey c = lgetc(0);
726 a596b957 2022-07-14 tracey } while (c != EOF && (allowed_in_string(c)));
727 a596b957 2022-07-14 tracey lungetc(c);
728 a596b957 2022-07-14 tracey *p = '\0';
729 a596b957 2022-07-14 tracey token = lookup(buf);
730 a596b957 2022-07-14 tracey if (token == STRING) {
731 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
732 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
733 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
735 a596b957 2022-07-14 tracey return (token);
737 a596b957 2022-07-14 tracey if (c == '\n') {
738 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
739 a596b957 2022-07-14 tracey file->lineno++;
741 a596b957 2022-07-14 tracey if (c == EOF)
742 a596b957 2022-07-14 tracey return (0);
743 a596b957 2022-07-14 tracey return (c);
747 a596b957 2022-07-14 tracey check_file_secrecy(int fd, const char *fname)
749 a596b957 2022-07-14 tracey struct stat st;
751 a596b957 2022-07-14 tracey if (fstat(fd, &st)) {
752 a596b957 2022-07-14 tracey log_warn("cannot stat %s", fname);
753 a596b957 2022-07-14 tracey return (-1);
755 a596b957 2022-07-14 tracey if (st.st_uid != 0 && st.st_uid != getuid()) {
756 a596b957 2022-07-14 tracey log_warnx("%s: owner not root or current user", fname);
757 a596b957 2022-07-14 tracey return (-1);
759 a596b957 2022-07-14 tracey if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
760 a596b957 2022-07-14 tracey log_warnx("%s: group writable or world read/writable", fname);
761 a596b957 2022-07-14 tracey return (-1);
763 a596b957 2022-07-14 tracey return (0);
766 a596b957 2022-07-14 tracey struct file *
767 a596b957 2022-07-14 tracey newfile(const char *name, int secret)
769 a596b957 2022-07-14 tracey struct file *nfile;
771 a596b957 2022-07-14 tracey nfile = calloc(1, sizeof(struct file));
772 a596b957 2022-07-14 tracey if (nfile == NULL) {
773 a596b957 2022-07-14 tracey log_warn("calloc");
774 a596b957 2022-07-14 tracey return (NULL);
776 a596b957 2022-07-14 tracey nfile->name = strdup(name);
777 a596b957 2022-07-14 tracey if (nfile->name == NULL) {
778 a596b957 2022-07-14 tracey log_warn("strdup");
779 a596b957 2022-07-14 tracey free(nfile);
780 a596b957 2022-07-14 tracey return (NULL);
782 a596b957 2022-07-14 tracey nfile->stream = fopen(nfile->name, "r");
783 a596b957 2022-07-14 tracey if (nfile->stream == NULL) {
784 a596b957 2022-07-14 tracey /* no warning, we don't require a conf file */
785 a596b957 2022-07-14 tracey free(nfile->name);
786 a596b957 2022-07-14 tracey free(nfile);
787 a596b957 2022-07-14 tracey return (NULL);
788 a596b957 2022-07-14 tracey } else if (secret &&
789 a596b957 2022-07-14 tracey check_file_secrecy(fileno(nfile->stream), nfile->name)) {
790 a596b957 2022-07-14 tracey fclose(nfile->stream);
791 a596b957 2022-07-14 tracey free(nfile->name);
792 a596b957 2022-07-14 tracey free(nfile);
793 a596b957 2022-07-14 tracey return (NULL);
795 a596b957 2022-07-14 tracey nfile->lineno = 1;
796 a596b957 2022-07-14 tracey return (nfile);
799 a596b957 2022-07-14 tracey static void
800 a596b957 2022-07-14 tracey closefile(struct file *xfile)
802 a596b957 2022-07-14 tracey fclose(xfile->stream);
803 a596b957 2022-07-14 tracey free(xfile->name);
804 a596b957 2022-07-14 tracey free(xfile);
807 a0037b73 2022-08-03 stsp static void
808 a0037b73 2022-08-03 stsp add_default_server(void)
810 a0037b73 2022-08-03 stsp new_srv = conf_new_server(D_SITENAME);
811 a0037b73 2022-08-03 stsp log_debug("%s: adding default server %s", __func__, D_SITENAME);
815 a596b957 2022-07-14 tracey parse_config(const char *filename, struct gotwebd *env)
817 a596b957 2022-07-14 tracey struct sym *sym, *next;
819 a596b957 2022-07-14 tracey if (config_init(env) == -1)
820 a596b957 2022-07-14 tracey fatalx("failed to initialize configuration");
822 a596b957 2022-07-14 tracey gotwebd = env;
824 a0037b73 2022-08-03 stsp file = newfile(filename, 0);
825 a0037b73 2022-08-03 stsp if (file == NULL) {
826 a0037b73 2022-08-03 stsp add_default_server();
827 a0037b73 2022-08-03 stsp sockets_parse_sockets(env);
828 a0037b73 2022-08-03 stsp /* just return, as we don't require a conf file */
829 a0037b73 2022-08-03 stsp return (0);
832 a596b957 2022-07-14 tracey yyparse();
833 a596b957 2022-07-14 tracey errors = file->errors;
834 a596b957 2022-07-14 tracey closefile(file);
836 a596b957 2022-07-14 tracey /* Free macros and check which have not been used. */
837 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
838 a596b957 2022-07-14 tracey if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
839 a596b957 2022-07-14 tracey fprintf(stderr, "warning: macro '%s' not used\n",
840 a596b957 2022-07-14 tracey sym->nam);
841 a596b957 2022-07-14 tracey if (!sym->persist) {
842 a596b957 2022-07-14 tracey free(sym->nam);
843 a596b957 2022-07-14 tracey free(sym->val);
844 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
845 a596b957 2022-07-14 tracey free(sym);
849 a596b957 2022-07-14 tracey if (errors)
850 a596b957 2022-07-14 tracey return (-1);
852 a596b957 2022-07-14 tracey /* just add default server if no config specified */
853 a0037b73 2022-08-03 stsp if (gotwebd->server_cnt == 0)
854 a0037b73 2022-08-03 stsp add_default_server();
856 a596b957 2022-07-14 tracey /* setup our listening sockets */
857 a596b957 2022-07-14 tracey sockets_parse_sockets(env);
859 a596b957 2022-07-14 tracey return (0);
862 a596b957 2022-07-14 tracey struct server *
863 a596b957 2022-07-14 tracey conf_new_server(const char *name)
865 a596b957 2022-07-14 tracey struct server *srv = NULL;
867 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
868 a596b957 2022-07-14 tracey if (srv == NULL)
869 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
871 a596b957 2022-07-14 tracey n = strlcpy(srv->name, name, sizeof(srv->name));
872 a596b957 2022-07-14 tracey if (n >= sizeof(srv->name))
873 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
874 a596b957 2022-07-14 tracey n = snprintf(srv->unix_socket_name,
875 a596b957 2022-07-14 tracey sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
876 a596b957 2022-07-14 tracey D_UNIX_SOCKET);
877 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
878 a596b957 2022-07-14 tracey fatalx("%s: snprintf", __func__);
879 a596b957 2022-07-14 tracey n = strlcpy(srv->repos_path, D_GOTPATH,
880 a596b957 2022-07-14 tracey sizeof(srv->repos_path));
881 a596b957 2022-07-14 tracey if (n >= sizeof(srv->repos_path))
882 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
883 a596b957 2022-07-14 tracey n = strlcpy(srv->site_name, D_SITENAME,
884 a596b957 2022-07-14 tracey sizeof(srv->site_name));
885 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_name))
886 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
887 a596b957 2022-07-14 tracey n = strlcpy(srv->site_owner, D_SITEOWNER,
888 a596b957 2022-07-14 tracey sizeof(srv->site_owner));
889 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_owner))
890 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
891 a596b957 2022-07-14 tracey n = strlcpy(srv->site_link, D_SITELINK,
892 a596b957 2022-07-14 tracey sizeof(srv->site_link));
893 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_link))
894 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
895 a596b957 2022-07-14 tracey n = strlcpy(srv->logo, D_GOTLOGO,
896 a596b957 2022-07-14 tracey sizeof(srv->logo));
897 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo))
898 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
899 a596b957 2022-07-14 tracey n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
900 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo_url))
901 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
902 a596b957 2022-07-14 tracey n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
903 a596b957 2022-07-14 tracey if (n >= sizeof(srv->custom_css))
904 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
906 a596b957 2022-07-14 tracey srv->show_site_owner = D_SHOWSOWNER;
907 a596b957 2022-07-14 tracey srv->show_repo_owner = D_SHOWROWNER;
908 a596b957 2022-07-14 tracey srv->show_repo_age = D_SHOWAGE;
909 a596b957 2022-07-14 tracey srv->show_repo_description = D_SHOWDESC;
910 a596b957 2022-07-14 tracey srv->show_repo_cloneurl = D_SHOWURL;
911 d5996b9e 2022-10-31 landry srv->respect_exportok = D_RESPECTEXPORTOK;
913 a596b957 2022-07-14 tracey srv->max_repos_display = D_MAXREPODISP;
914 a596b957 2022-07-14 tracey srv->max_commits_display = D_MAXCOMMITDISP;
915 a596b957 2022-07-14 tracey srv->max_repos = D_MAXREPO;
917 a596b957 2022-07-14 tracey srv->unix_socket = 1;
918 6c8aa58f 2022-08-30 stsp srv->fcgi_socket = 0;
920 e087e1f6 2022-08-16 stsp TAILQ_INIT(&srv->al);
921 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
922 a596b957 2022-07-14 tracey gotwebd->server_cnt++;
924 a596b957 2022-07-14 tracey return srv;
928 a596b957 2022-07-14 tracey symset(const char *nam, const char *val, int persist)
930 a596b957 2022-07-14 tracey struct sym *sym;
932 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
933 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0)
937 a596b957 2022-07-14 tracey if (sym != NULL) {
938 a596b957 2022-07-14 tracey if (sym->persist == 1)
939 a596b957 2022-07-14 tracey return (0);
941 a596b957 2022-07-14 tracey free(sym->nam);
942 a596b957 2022-07-14 tracey free(sym->val);
943 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
944 a596b957 2022-07-14 tracey free(sym);
947 a596b957 2022-07-14 tracey sym = calloc(1, sizeof(*sym));
948 a596b957 2022-07-14 tracey if (sym == NULL)
949 a596b957 2022-07-14 tracey return (-1);
951 a596b957 2022-07-14 tracey sym->nam = strdup(nam);
952 a596b957 2022-07-14 tracey if (sym->nam == NULL) {
953 a596b957 2022-07-14 tracey free(sym);
954 a596b957 2022-07-14 tracey return (-1);
956 a596b957 2022-07-14 tracey sym->val = strdup(val);
957 a596b957 2022-07-14 tracey if (sym->val == NULL) {
958 a596b957 2022-07-14 tracey free(sym->nam);
959 a596b957 2022-07-14 tracey free(sym);
960 a596b957 2022-07-14 tracey return (-1);
962 a596b957 2022-07-14 tracey sym->used = 0;
963 a596b957 2022-07-14 tracey sym->persist = persist;
964 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
965 a596b957 2022-07-14 tracey return (0);
969 a596b957 2022-07-14 tracey cmdline_symset(char *s)
971 a596b957 2022-07-14 tracey char *sym, *val;
974 a596b957 2022-07-14 tracey val = strrchr(s, '=');
975 a596b957 2022-07-14 tracey if (val == NULL)
976 a596b957 2022-07-14 tracey return (-1);
978 4cdd299d 2022-09-05 op sym = strndup(s, val - s);
979 a596b957 2022-07-14 tracey if (sym == NULL)
980 4cdd299d 2022-09-05 op fatal("%s: strndup", __func__);
982 a596b957 2022-07-14 tracey ret = symset(sym, val + 1, 1);
983 a596b957 2022-07-14 tracey free(sym);
985 a596b957 2022-07-14 tracey return (ret);
989 a596b957 2022-07-14 tracey symget(const char *nam)
991 a596b957 2022-07-14 tracey struct sym *sym;
993 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
994 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0) {
995 a596b957 2022-07-14 tracey sym->used = 1;
996 a596b957 2022-07-14 tracey return (sym->val);
999 a596b957 2022-07-14 tracey return (NULL);
1003 a596b957 2022-07-14 tracey getservice(const char *n)
1005 a596b957 2022-07-14 tracey struct servent *s;
1006 a596b957 2022-07-14 tracey const char *errstr;
1007 a596b957 2022-07-14 tracey long long llval;
1009 a596b957 2022-07-14 tracey llval = strtonum(n, 0, UINT16_MAX, &errstr);
1010 a596b957 2022-07-14 tracey if (errstr) {
1011 a596b957 2022-07-14 tracey s = getservbyname(n, "tcp");
1012 a596b957 2022-07-14 tracey if (s == NULL)
1013 a596b957 2022-07-14 tracey s = getservbyname(n, "udp");
1014 a596b957 2022-07-14 tracey if (s == NULL)
1015 a596b957 2022-07-14 tracey return (-1);
1016 859aa9f4 2022-08-19 stsp return ntohs(s->s_port);
1019 859aa9f4 2022-08-19 stsp return (unsigned short)llval;
1023 abf3e3f4 2023-05-29 op host(const char *s, struct server *new_srv, int max,
1024 a3dc2f5d 2023-11-03 op in_port_t port, const char *ifname)
1026 a596b957 2022-07-14 tracey struct addrinfo hints, *res0, *res;
1027 a596b957 2022-07-14 tracey int error, cnt = 0;
1028 a596b957 2022-07-14 tracey struct sockaddr_in *sain;
1029 a596b957 2022-07-14 tracey struct sockaddr_in6 *sin6;
1030 a596b957 2022-07-14 tracey struct address *h;
1032 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1033 fb307946 2023-05-29 op hints.ai_family = AF_UNSPEC;
1034 fb307946 2023-05-29 op hints.ai_socktype = SOCK_STREAM; /* DUMMY */
1035 a596b957 2022-07-14 tracey hints.ai_flags = AI_ADDRCONFIG;
1036 a596b957 2022-07-14 tracey error = getaddrinfo(s, NULL, &hints, &res0);
1037 a596b957 2022-07-14 tracey if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1038 a596b957 2022-07-14 tracey return (0);
1039 a596b957 2022-07-14 tracey if (error) {
1040 a596b957 2022-07-14 tracey log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1041 a596b957 2022-07-14 tracey gai_strerror(error));
1042 a596b957 2022-07-14 tracey return (-1);
1045 a596b957 2022-07-14 tracey for (res = res0; res && cnt < max; res = res->ai_next) {
1046 a596b957 2022-07-14 tracey if (res->ai_family != AF_INET &&
1047 a596b957 2022-07-14 tracey res->ai_family != AF_INET6)
1048 a596b957 2022-07-14 tracey continue;
1049 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1050 a596b957 2022-07-14 tracey fatal(__func__);
1052 a596b957 2022-07-14 tracey if (port)
1053 a596b957 2022-07-14 tracey h->port = port;
1054 a596b957 2022-07-14 tracey if (ifname != NULL) {
1055 a596b957 2022-07-14 tracey if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1056 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1057 a596b957 2022-07-14 tracey log_warnx("%s: interface name truncated",
1058 a596b957 2022-07-14 tracey __func__);
1059 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1060 a596b957 2022-07-14 tracey free(h);
1061 a596b957 2022-07-14 tracey return (-1);
1064 a596b957 2022-07-14 tracey h->ss.ss_family = res->ai_family;
1066 a596b957 2022-07-14 tracey if (res->ai_family == AF_INET) {
1067 86b4b772 2022-07-29 stsp struct sockaddr_in *ra;
1068 a596b957 2022-07-14 tracey sain = (struct sockaddr_in *)&h->ss;
1069 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in *)res->ai_addr;
1070 86b4b772 2022-07-29 stsp got_sockaddr_inet_init(sain, &ra->sin_addr);
1071 a596b957 2022-07-14 tracey } else {
1072 86b4b772 2022-07-29 stsp struct sockaddr_in6 *ra;
1073 a596b957 2022-07-14 tracey sin6 = (struct sockaddr_in6 *)&h->ss;
1074 86b4b772 2022-07-29 stsp ra = (struct sockaddr_in6 *)res->ai_addr;
1075 86b4b772 2022-07-29 stsp got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1078 67d8de2a 2022-08-29 stsp if (add_addr(new_srv, h))
1079 67d8de2a 2022-08-29 stsp return -1;
1082 a596b957 2022-07-14 tracey if (cnt == max && res) {
1083 a596b957 2022-07-14 tracey log_warnx("%s: %s resolves to more than %d hosts", __func__,
1084 a596b957 2022-07-14 tracey s, max);
1086 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1087 a596b957 2022-07-14 tracey return (cnt);
1091 67d8de2a 2022-08-29 stsp get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1093 a596b957 2022-07-14 tracey if (strcmp("", addr) == 0) {
1094 a3dc2f5d 2023-11-03 op if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1") <= 0) {
1095 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s",
1096 2fb25d87 2022-08-19 stsp "127.0.0.1");
1097 a596b957 2022-07-14 tracey return (-1);
1099 a3dc2f5d 2023-11-03 op if (host("::1", new_srv, 1, port, "::1") <= 0) {
1100 2fb25d87 2022-08-19 stsp yyerror("invalid listen ip: %s", "::1");
1101 a596b957 2022-07-14 tracey return (-1);
1103 a596b957 2022-07-14 tracey } else {
1104 a3dc2f5d 2023-11-03 op if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr) <= 0) {
1105 a596b957 2022-07-14 tracey yyerror("invalid listen ip: %s", addr);
1106 a596b957 2022-07-14 tracey return (-1);
1109 a596b957 2022-07-14 tracey return (0);
1113 67d8de2a 2022-08-29 stsp addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1114 67d8de2a 2022-08-29 stsp const char *other_srv)
1116 67d8de2a 2022-08-29 stsp struct address *a;
1118 67d8de2a 2022-08-29 stsp char buf[INET6_ADDRSTRLEN];
1119 67d8de2a 2022-08-29 stsp const char *addrstr;
1121 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(a, al, entry) {
1122 67d8de2a 2022-08-29 stsp if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1123 67d8de2a 2022-08-29 stsp a->port != h->port)
1126 67d8de2a 2022-08-29 stsp switch (h->ss.ss_family) {
1127 67d8de2a 2022-08-29 stsp case AF_INET:
1128 67d8de2a 2022-08-29 stsp ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1130 67d8de2a 2022-08-29 stsp case AF_INET6:
1131 67d8de2a 2022-08-29 stsp ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1134 67d8de2a 2022-08-29 stsp yyerror("unknown address family: %d", h->ss.ss_family);
1135 67d8de2a 2022-08-29 stsp return -1;
1137 67d8de2a 2022-08-29 stsp addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1138 67d8de2a 2022-08-29 stsp if (addrstr) {
1139 67d8de2a 2022-08-29 stsp if (other_srv) {
1140 67d8de2a 2022-08-29 stsp yyerror("server %s: duplicate fcgi listen "
1141 67d8de2a 2022-08-29 stsp "address %s:%d, already used by server %s",
1142 67d8de2a 2022-08-29 stsp new_srv, addrstr, h->port, other_srv);
1144 67d8de2a 2022-08-29 stsp log_warnx("server: %s: duplicate fcgi listen "
1145 67d8de2a 2022-08-29 stsp "address %s:%d", new_srv, addrstr, h->port);
1148 67d8de2a 2022-08-29 stsp if (other_srv) {
1149 67d8de2a 2022-08-29 stsp yyerror("server: %s: duplicate fcgi listen "
1150 67d8de2a 2022-08-29 stsp "address, already used by server %s",
1151 67d8de2a 2022-08-29 stsp new_srv, other_srv);
1153 67d8de2a 2022-08-29 stsp log_warnx("server %s: duplicate fcgi listen "
1154 67d8de2a 2022-08-29 stsp "address", new_srv);
1158 67d8de2a 2022-08-29 stsp return -1;
1165 67d8de2a 2022-08-29 stsp add_addr(struct server *new_srv, struct address *h)
1167 67d8de2a 2022-08-29 stsp struct server *srv;
1169 67d8de2a 2022-08-29 stsp /* Address cannot be shared between different servers. */
1170 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1171 67d8de2a 2022-08-29 stsp if (srv == new_srv)
1173 67d8de2a 2022-08-29 stsp if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1174 67d8de2a 2022-08-29 stsp return -1;
1177 67d8de2a 2022-08-29 stsp /* Tolerate duplicate address lines within the scope of a server. */
1178 67d8de2a 2022-08-29 stsp if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1179 67d8de2a 2022-08-29 stsp TAILQ_INSERT_TAIL(&new_srv->al, h, entry);