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>
29 36079424 2024-05-16 op #include <sys/un.h>
31 a596b957 2022-07-14 tracey #include <net/if.h>
32 a596b957 2022-07-14 tracey #include <netinet/in.h>
34 a596b957 2022-07-14 tracey #include <arpa/inet.h>
36 a596b957 2022-07-14 tracey #include <ctype.h>
37 a596b957 2022-07-14 tracey #include <err.h>
38 a596b957 2022-07-14 tracey #include <errno.h>
39 a596b957 2022-07-14 tracey #include <event.h>
40 a596b957 2022-07-14 tracey #include <ifaddrs.h>
41 a596b957 2022-07-14 tracey #include <imsg.h>
42 a596b957 2022-07-14 tracey #include <limits.h>
43 a596b957 2022-07-14 tracey #include <netdb.h>
44 a596b957 2022-07-14 tracey #include <stdarg.h>
45 a596b957 2022-07-14 tracey #include <stdlib.h>
46 a596b957 2022-07-14 tracey #include <stdio.h>
47 a596b957 2022-07-14 tracey #include <string.h>
48 a596b957 2022-07-14 tracey #include <syslog.h>
49 a596b957 2022-07-14 tracey #include <unistd.h>
51 df2d3cd2 2023-03-11 op #include "got_reference.h"
53 a596b957 2022-07-14 tracey #include "gotwebd.h"
55 a596b957 2022-07-14 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 a596b957 2022-07-14 tracey static struct file {
57 a596b957 2022-07-14 tracey TAILQ_ENTRY(file) entry;
58 a596b957 2022-07-14 tracey FILE *stream;
59 a596b957 2022-07-14 tracey char *name;
60 a596b957 2022-07-14 tracey int lineno;
61 a596b957 2022-07-14 tracey int errors;
63 a596b957 2022-07-14 tracey struct file *newfile(const char *, int);
64 a596b957 2022-07-14 tracey static void closefile(struct file *);
65 a596b957 2022-07-14 tracey int check_file_secrecy(int, const char *);
66 a596b957 2022-07-14 tracey int yyparse(void);
67 a596b957 2022-07-14 tracey int yylex(void);
68 a596b957 2022-07-14 tracey int yyerror(const char *, ...)
69 a596b957 2022-07-14 tracey __attribute__((__format__ (printf, 1, 2)))
70 a596b957 2022-07-14 tracey __attribute__((__nonnull__ (1)));
71 a596b957 2022-07-14 tracey int kw_cmp(const void *, const void *);
72 a596b957 2022-07-14 tracey int lookup(char *);
73 a596b957 2022-07-14 tracey int lgetc(int);
74 a596b957 2022-07-14 tracey int lungetc(int);
75 a596b957 2022-07-14 tracey int findeol(void);
77 a596b957 2022-07-14 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 a596b957 2022-07-14 tracey struct sym {
79 a596b957 2022-07-14 tracey TAILQ_ENTRY(sym) entry;
81 a596b957 2022-07-14 tracey int persist;
82 a596b957 2022-07-14 tracey char *nam;
83 a596b957 2022-07-14 tracey char *val;
86 a596b957 2022-07-14 tracey int symset(const char *, const char *, int);
87 a596b957 2022-07-14 tracey char *symget(const char *);
89 a596b957 2022-07-14 tracey static int errors;
91 a596b957 2022-07-14 tracey static struct gotwebd *gotwebd;
92 a596b957 2022-07-14 tracey static struct server *new_srv;
93 a596b957 2022-07-14 tracey static struct server *conf_new_server(const char *);
94 a596b957 2022-07-14 tracey int getservice(const char *);
97 89cfaaa7 2023-11-15 op int get_addrs(const char *, const char *, struct server *);
98 36079424 2024-05-16 op int get_unix_addr(const char *, struct server *);
99 36079424 2024-05-16 op int addr_dup_check(struct addresslist *, struct address *);
100 67d8de2a 2022-08-29 stsp int add_addr(struct server *, struct address *);
102 a596b957 2022-07-14 tracey typedef struct {
104 a596b957 2022-07-14 tracey long long number;
105 a596b957 2022-07-14 tracey char *string;
107 a596b957 2022-07-14 tracey int lineno;
108 a596b957 2022-07-14 tracey } YYSTYPE;
112 e114f3d1 2023-12-29 op %token LISTEN WWW_PATH SITE_NAME SITE_OWNER SITE_LINK LOGO
113 a596b957 2022-07-14 tracey %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
114 a596b957 2022-07-14 tracey %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
115 d5996b9e 2022-10-31 landry %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
116 36079424 2024-05-16 op %token UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
117 8762929a 2023-12-29 op %token SUMMARY_COMMITS_DISPLAY SUMMARY_TAGS_DISPLAY
119 a596b957 2022-07-14 tracey %token <v.string> STRING
120 a596b957 2022-07-14 tracey %token <v.number> NUMBER
121 a596b957 2022-07-14 tracey %type <v.number> boolean
122 c5e111b9 2023-11-15 op %type <v.string> listen_addr
126 47b307cd 2022-10-02 op grammar : /* empty */
127 a596b957 2022-07-14 tracey | grammar '\n'
128 47b307cd 2022-10-02 op | grammar varset '\n'
129 a596b957 2022-07-14 tracey | grammar main '\n'
130 a596b957 2022-07-14 tracey | grammar server '\n'
131 47b307cd 2022-10-02 op | grammar error '\n' { file->errors++; }
134 47b307cd 2022-10-02 op varset : STRING '=' STRING {
135 47b307cd 2022-10-02 op char *s = $1;
136 47b307cd 2022-10-02 op while (*s++) {
137 47b307cd 2022-10-02 op if (isspace((unsigned char)*s)) {
138 47b307cd 2022-10-02 op yyerror("macro name cannot contain "
139 47b307cd 2022-10-02 op "whitespace");
145 47b307cd 2022-10-02 op if (symset($1, $3, 0) == -1)
146 47b307cd 2022-10-02 op fatal("cannot store variable");
152 a596b957 2022-07-14 tracey boolean : STRING {
153 a596b957 2022-07-14 tracey if (strcasecmp($1, "1") == 0 ||
154 a596b957 2022-07-14 tracey strcasecmp($1, "on") == 0)
156 a596b957 2022-07-14 tracey else if (strcasecmp($1, "0") == 0 ||
157 031687ba 2023-06-15 op strcasecmp($1, "off") == 0)
160 a596b957 2022-07-14 tracey yyerror("invalid boolean value '%s'", $1);
161 a596b957 2022-07-14 tracey free($1);
164 a596b957 2022-07-14 tracey free($1);
166 a596b957 2022-07-14 tracey | ON { $$ = 1; }
168 1a0c81fb 2023-06-15 op if ($1 != 0 && $1 != 1) {
169 1a0c81fb 2023-06-15 op yyerror("invalid boolean value '%lld'", $1);
176 c5e111b9 2023-11-15 op listen_addr : '*' { $$ = NULL; }
180 a596b957 2022-07-14 tracey main : PREFORK NUMBER {
181 1a0c81fb 2023-06-15 op if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
182 1a0c81fb 2023-06-15 op yyerror("prefork is %s: %lld",
183 1a0c81fb 2023-06-15 op $2 <= 0 ? "too small" : "too large", $2);
186 a596b957 2022-07-14 tracey gotwebd->prefork_gotwebd = $2;
188 a596b957 2022-07-14 tracey | CHROOT STRING {
189 a678036d 2023-06-15 op if (*$2 == '\0') {
190 a678036d 2023-06-15 op yyerror("chroot path can't be an empty"
196 a596b957 2022-07-14 tracey n = strlcpy(gotwebd->httpd_chroot, $2,
197 a596b957 2022-07-14 tracey sizeof(gotwebd->httpd_chroot));
198 a596b957 2022-07-14 tracey if (n >= sizeof(gotwebd->httpd_chroot)) {
199 a596b957 2022-07-14 tracey yyerror("%s: httpd_chroot truncated", __func__);
200 a596b957 2022-07-14 tracey free($2);
203 a596b957 2022-07-14 tracey free($2);
205 a596b957 2022-07-14 tracey | UNIX_SOCKET_NAME STRING {
206 a596b957 2022-07-14 tracey n = snprintf(gotwebd->unix_socket_name,
207 a596b957 2022-07-14 tracey sizeof(gotwebd->unix_socket_name), "%s%s",
208 f4a5cef1 2023-06-15 op gotwebd->httpd_chroot, $2);
210 438d0cc3 2022-08-16 op (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
211 a596b957 2022-07-14 tracey yyerror("%s: unix_socket_name truncated",
212 a596b957 2022-07-14 tracey __func__);
213 a596b957 2022-07-14 tracey free($2);
216 a596b957 2022-07-14 tracey free($2);
220 a596b957 2022-07-14 tracey server : SERVER STRING {
221 a596b957 2022-07-14 tracey struct server *srv;
223 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
224 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
225 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
226 a596b957 2022-07-14 tracey free($2);
231 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
232 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
233 a596b957 2022-07-14 tracey free($2);
235 a596b957 2022-07-14 tracey | SERVER STRING {
236 a596b957 2022-07-14 tracey struct server *srv;
238 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
239 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
240 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
241 a596b957 2022-07-14 tracey free($2);
246 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
247 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
248 a596b957 2022-07-14 tracey free($2);
249 a596b957 2022-07-14 tracey } '{' optnl serveropts2 '}' {
253 a596b957 2022-07-14 tracey serveropts1 : REPOS_PATH STRING {
254 a596b957 2022-07-14 tracey n = strlcpy(new_srv->repos_path, $2,
255 a596b957 2022-07-14 tracey sizeof(new_srv->repos_path));
256 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->repos_path)) {
257 a596b957 2022-07-14 tracey yyerror("%s: repos_path truncated", __func__);
258 a596b957 2022-07-14 tracey free($2);
261 a596b957 2022-07-14 tracey free($2);
263 a596b957 2022-07-14 tracey | SITE_NAME STRING {
264 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_name, $2,
265 a596b957 2022-07-14 tracey sizeof(new_srv->site_name));
266 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_name)) {
267 a596b957 2022-07-14 tracey yyerror("%s: site_name truncated", __func__);
268 a596b957 2022-07-14 tracey free($2);
271 a596b957 2022-07-14 tracey free($2);
273 a596b957 2022-07-14 tracey | SITE_OWNER STRING {
274 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_owner, $2,
275 a596b957 2022-07-14 tracey sizeof(new_srv->site_owner));
276 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_owner)) {
277 a596b957 2022-07-14 tracey yyerror("%s: site_owner truncated", __func__);
278 a596b957 2022-07-14 tracey free($2);
281 a596b957 2022-07-14 tracey free($2);
283 a596b957 2022-07-14 tracey | SITE_LINK STRING {
284 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_link, $2,
285 a596b957 2022-07-14 tracey sizeof(new_srv->site_link));
286 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_link)) {
287 a596b957 2022-07-14 tracey yyerror("%s: site_link truncated", __func__);
288 a596b957 2022-07-14 tracey free($2);
291 a596b957 2022-07-14 tracey free($2);
293 a596b957 2022-07-14 tracey | LOGO STRING {
294 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
295 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo)) {
296 a596b957 2022-07-14 tracey yyerror("%s: logo truncated", __func__);
297 a596b957 2022-07-14 tracey free($2);
300 a596b957 2022-07-14 tracey free($2);
302 a596b957 2022-07-14 tracey | LOGO_URL STRING {
303 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo_url, $2,
304 a596b957 2022-07-14 tracey sizeof(new_srv->logo_url));
305 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo_url)) {
306 a596b957 2022-07-14 tracey yyerror("%s: logo_url truncated", __func__);
307 a596b957 2022-07-14 tracey free($2);
310 a596b957 2022-07-14 tracey free($2);
312 a596b957 2022-07-14 tracey | CUSTOM_CSS STRING {
313 a596b957 2022-07-14 tracey n = strlcpy(new_srv->custom_css, $2,
314 a596b957 2022-07-14 tracey sizeof(new_srv->custom_css));
315 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->custom_css)) {
316 a596b957 2022-07-14 tracey yyerror("%s: custom_css truncated", __func__);
317 a596b957 2022-07-14 tracey free($2);
320 a596b957 2022-07-14 tracey free($2);
322 89cfaaa7 2023-11-15 op | LISTEN ON listen_addr PORT STRING {
323 89cfaaa7 2023-11-15 op if (get_addrs($3, $5, new_srv) == -1) {
324 67d8de2a 2022-08-29 stsp yyerror("could not get addrs");
330 89cfaaa7 2023-11-15 op | LISTEN ON listen_addr PORT NUMBER {
331 89cfaaa7 2023-11-15 op char portno[32];
334 89cfaaa7 2023-11-15 op n = snprintf(portno, sizeof(portno), "%lld",
335 89cfaaa7 2023-11-15 op (long long)$5);
336 89cfaaa7 2023-11-15 op if (n < 0 || (size_t)n >= sizeof(portno))
337 89cfaaa7 2023-11-15 op fatalx("port number too long: %lld",
338 89cfaaa7 2023-11-15 op (long long)$5);
340 89cfaaa7 2023-11-15 op if (get_addrs($3, portno, new_srv) == -1) {
341 89cfaaa7 2023-11-15 op yyerror("could not get addrs");
346 3a1c1a1b 2023-01-04 op | LISTEN ON SOCKET STRING {
347 36079424 2024-05-16 op if (get_unix_addr($4, new_srv) == -1) {
348 36079424 2024-05-16 op yyerror("can't listen on %s", $4);
354 a596b957 2022-07-14 tracey | SHOW_SITE_OWNER boolean {
355 a596b957 2022-07-14 tracey new_srv->show_site_owner = $2;
357 a596b957 2022-07-14 tracey | SHOW_REPO_OWNER boolean {
358 a596b957 2022-07-14 tracey new_srv->show_repo_owner = $2;
360 a596b957 2022-07-14 tracey | SHOW_REPO_AGE boolean {
361 a596b957 2022-07-14 tracey new_srv->show_repo_age = $2;
363 a596b957 2022-07-14 tracey | SHOW_REPO_DESCRIPTION boolean {
364 a596b957 2022-07-14 tracey new_srv->show_repo_description = $2;
366 a596b957 2022-07-14 tracey | SHOW_REPO_CLONEURL boolean {
367 a596b957 2022-07-14 tracey new_srv->show_repo_cloneurl = $2;
369 d5996b9e 2022-10-31 landry | RESPECT_EXPORTOK boolean {
370 d5996b9e 2022-10-31 landry new_srv->respect_exportok = $2;
372 a596b957 2022-07-14 tracey | MAX_REPOS_DISPLAY NUMBER {
373 20f27972 2023-06-19 op if ($2 < 0) {
374 1a0c81fb 2023-06-15 op yyerror("max_repos_display is too small: %lld",
378 1a0c81fb 2023-06-15 op new_srv->max_repos_display = $2;
380 a596b957 2022-07-14 tracey | MAX_COMMITS_DISPLAY NUMBER {
381 f4425f95 2023-06-14 op if ($2 <= 1) {
382 f4425f95 2023-06-14 op yyerror("max_commits_display is too small:"
383 f4425f95 2023-06-14 op " %lld", $2);
386 f4425f95 2023-06-14 op new_srv->max_commits_display = $2;
388 8762929a 2023-12-29 op | SUMMARY_COMMITS_DISPLAY NUMBER {
389 8762929a 2023-12-29 op if ($2 < 1) {
390 8762929a 2023-12-29 op yyerror("summary_commits_display is too small:"
391 8762929a 2023-12-29 op " %lld", $2);
394 8762929a 2023-12-29 op new_srv->summary_commits_display = $2;
396 8762929a 2023-12-29 op | SUMMARY_TAGS_DISPLAY NUMBER {
397 8762929a 2023-12-29 op if ($2 < 1) {
398 8762929a 2023-12-29 op yyerror("summary_tags_display is too small:"
399 8762929a 2023-12-29 op " %lld", $2);
402 8762929a 2023-12-29 op new_srv->summary_tags_display = $2;
406 a596b957 2022-07-14 tracey serveropts2 : serveropts2 serveropts1 nl
407 a596b957 2022-07-14 tracey | serveropts1 optnl
410 a596b957 2022-07-14 tracey nl : '\n' optnl
413 a596b957 2022-07-14 tracey optnl : '\n' optnl /* zero or more newlines */
414 a596b957 2022-07-14 tracey | /* empty */
419 a596b957 2022-07-14 tracey struct keywords {
420 a596b957 2022-07-14 tracey const char *k_name;
421 a596b957 2022-07-14 tracey int k_val;
425 a596b957 2022-07-14 tracey yyerror(const char *fmt, ...)
427 a596b957 2022-07-14 tracey va_list ap;
428 a596b957 2022-07-14 tracey char *msg;
430 a596b957 2022-07-14 tracey file->errors++;
431 a596b957 2022-07-14 tracey va_start(ap, fmt);
432 a596b957 2022-07-14 tracey if (vasprintf(&msg, fmt, ap) == -1)
433 a596b957 2022-07-14 tracey fatalx("yyerror vasprintf");
434 a596b957 2022-07-14 tracey va_end(ap);
435 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
436 a596b957 2022-07-14 tracey free(msg);
437 a596b957 2022-07-14 tracey return (0);
441 a596b957 2022-07-14 tracey kw_cmp(const void *k, const void *e)
443 a596b957 2022-07-14 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
447 a596b957 2022-07-14 tracey lookup(char *s)
449 a596b957 2022-07-14 tracey /* This has to be sorted always. */
450 a596b957 2022-07-14 tracey static const struct keywords keywords[] = {
451 a596b957 2022-07-14 tracey { "chroot", CHROOT },
452 a596b957 2022-07-14 tracey { "custom_css", CUSTOM_CSS },
453 d8473d93 2022-08-11 stsp { "listen", LISTEN },
454 a596b957 2022-07-14 tracey { "logo", LOGO },
455 8556b86b 2023-01-02 op { "logo_url", LOGO_URL },
456 a596b957 2022-07-14 tracey { "max_commits_display", MAX_COMMITS_DISPLAY },
457 a596b957 2022-07-14 tracey { "max_repos_display", MAX_REPOS_DISPLAY },
458 d8473d93 2022-08-11 stsp { "on", ON },
459 a596b957 2022-07-14 tracey { "port", PORT },
460 a596b957 2022-07-14 tracey { "prefork", PREFORK },
461 a596b957 2022-07-14 tracey { "repos_path", REPOS_PATH },
462 d5996b9e 2022-10-31 landry { "respect_exportok", RESPECT_EXPORTOK },
463 a596b957 2022-07-14 tracey { "server", SERVER },
464 a596b957 2022-07-14 tracey { "show_repo_age", SHOW_REPO_AGE },
465 a596b957 2022-07-14 tracey { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
466 a596b957 2022-07-14 tracey { "show_repo_description", SHOW_REPO_DESCRIPTION },
467 a596b957 2022-07-14 tracey { "show_repo_owner", SHOW_REPO_OWNER },
468 a596b957 2022-07-14 tracey { "show_site_owner", SHOW_SITE_OWNER },
469 a596b957 2022-07-14 tracey { "site_link", SITE_LINK },
470 a596b957 2022-07-14 tracey { "site_name", SITE_NAME },
471 a596b957 2022-07-14 tracey { "site_owner", SITE_OWNER },
472 3a1c1a1b 2023-01-04 op { "socket", SOCKET },
473 8762929a 2023-12-29 op { "summary_commits_display", SUMMARY_COMMITS_DISPLAY },
474 8762929a 2023-12-29 op { "summary_tags_display", SUMMARY_TAGS_DISPLAY },
475 a596b957 2022-07-14 tracey { "unix_socket_name", UNIX_SOCKET_NAME },
477 a596b957 2022-07-14 tracey const struct keywords *p;
479 a596b957 2022-07-14 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
480 a596b957 2022-07-14 tracey sizeof(keywords[0]), kw_cmp);
483 a596b957 2022-07-14 tracey return (p->k_val);
485 a596b957 2022-07-14 tracey return (STRING);
488 a596b957 2022-07-14 tracey #define MAXPUSHBACK 128
490 a596b957 2022-07-14 tracey unsigned char *parsebuf;
491 a596b957 2022-07-14 tracey int parseindex;
492 a596b957 2022-07-14 tracey unsigned char pushback_buffer[MAXPUSHBACK];
493 a596b957 2022-07-14 tracey int pushback_index = 0;
496 a596b957 2022-07-14 tracey lgetc(int quotec)
498 a596b957 2022-07-14 tracey int c, next;
500 a596b957 2022-07-14 tracey if (parsebuf) {
501 a596b957 2022-07-14 tracey /* Read character from the parsebuffer instead of input. */
502 a596b957 2022-07-14 tracey if (parseindex >= 0) {
503 a596b957 2022-07-14 tracey c = parsebuf[parseindex++];
504 a596b957 2022-07-14 tracey if (c != '\0')
505 a596b957 2022-07-14 tracey return (c);
506 a596b957 2022-07-14 tracey parsebuf = NULL;
508 a596b957 2022-07-14 tracey parseindex++;
511 a596b957 2022-07-14 tracey if (pushback_index)
512 a596b957 2022-07-14 tracey return (pushback_buffer[--pushback_index]);
514 a596b957 2022-07-14 tracey if (quotec) {
515 a596b957 2022-07-14 tracey c = getc(file->stream);
516 a596b957 2022-07-14 tracey if (c == EOF)
517 a596b957 2022-07-14 tracey yyerror("reached end of file while parsing "
518 a596b957 2022-07-14 tracey "quoted string");
519 a596b957 2022-07-14 tracey return (c);
522 a596b957 2022-07-14 tracey c = getc(file->stream);
523 a596b957 2022-07-14 tracey while (c == '\\') {
524 a596b957 2022-07-14 tracey next = getc(file->stream);
525 a596b957 2022-07-14 tracey if (next != '\n') {
526 a596b957 2022-07-14 tracey c = next;
529 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
530 a596b957 2022-07-14 tracey file->lineno++;
531 a596b957 2022-07-14 tracey c = getc(file->stream);
534 a596b957 2022-07-14 tracey return (c);
538 a596b957 2022-07-14 tracey lungetc(int c)
540 a596b957 2022-07-14 tracey if (c == EOF)
541 a596b957 2022-07-14 tracey return (EOF);
542 a596b957 2022-07-14 tracey if (parsebuf) {
543 a596b957 2022-07-14 tracey parseindex--;
544 a596b957 2022-07-14 tracey if (parseindex >= 0)
545 a596b957 2022-07-14 tracey return (c);
547 a596b957 2022-07-14 tracey if (pushback_index < MAXPUSHBACK-1)
548 a596b957 2022-07-14 tracey return (pushback_buffer[pushback_index++] = c);
550 a596b957 2022-07-14 tracey return (EOF);
554 a596b957 2022-07-14 tracey findeol(void)
558 a596b957 2022-07-14 tracey parsebuf = NULL;
560 a596b957 2022-07-14 tracey /* Skip to either EOF or the first real EOL. */
561 a596b957 2022-07-14 tracey while (1) {
562 a596b957 2022-07-14 tracey if (pushback_index)
563 a596b957 2022-07-14 tracey c = pushback_buffer[--pushback_index];
565 a596b957 2022-07-14 tracey c = lgetc(0);
566 a596b957 2022-07-14 tracey if (c == '\n') {
567 a596b957 2022-07-14 tracey file->lineno++;
570 a596b957 2022-07-14 tracey if (c == EOF)
573 a596b957 2022-07-14 tracey return (ERROR);
577 a596b957 2022-07-14 tracey yylex(void)
579 a596b957 2022-07-14 tracey unsigned char buf[8096];
580 a596b957 2022-07-14 tracey unsigned char *p, *val;
581 a596b957 2022-07-14 tracey int quotec, next, c;
582 a596b957 2022-07-14 tracey int token;
586 a596b957 2022-07-14 tracey c = lgetc(0);
587 a596b957 2022-07-14 tracey while (c == ' ' || c == '\t')
588 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
590 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
591 a596b957 2022-07-14 tracey if (c == '#') {
592 a596b957 2022-07-14 tracey c = lgetc(0);
593 a596b957 2022-07-14 tracey while (c != '\n' && c != EOF)
594 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
596 a596b957 2022-07-14 tracey if (c == '$' && parsebuf == NULL) {
597 a596b957 2022-07-14 tracey while (1) {
598 a596b957 2022-07-14 tracey c = lgetc(0);
599 a596b957 2022-07-14 tracey if (c == EOF)
600 a596b957 2022-07-14 tracey return (0);
602 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
603 a596b957 2022-07-14 tracey yyerror("string too long");
604 a596b957 2022-07-14 tracey return (findeol());
606 a596b957 2022-07-14 tracey if (isalnum(c) || c == '_') {
607 a596b957 2022-07-14 tracey *p++ = c;
608 a596b957 2022-07-14 tracey continue;
610 a596b957 2022-07-14 tracey *p = '\0';
611 a596b957 2022-07-14 tracey lungetc(c);
614 a596b957 2022-07-14 tracey val = symget(buf);
615 a596b957 2022-07-14 tracey if (val == NULL) {
616 a596b957 2022-07-14 tracey yyerror("macro '%s' not defined", buf);
617 a596b957 2022-07-14 tracey return (findeol());
619 a596b957 2022-07-14 tracey parsebuf = val;
620 a596b957 2022-07-14 tracey parseindex = 0;
621 a596b957 2022-07-14 tracey goto top;
624 a596b957 2022-07-14 tracey switch (c) {
625 a596b957 2022-07-14 tracey case '\'':
626 a596b957 2022-07-14 tracey case '"':
627 a596b957 2022-07-14 tracey quotec = c;
628 a596b957 2022-07-14 tracey while (1) {
629 a596b957 2022-07-14 tracey c = lgetc(quotec);
630 a596b957 2022-07-14 tracey if (c == EOF)
631 a596b957 2022-07-14 tracey return (0);
632 a596b957 2022-07-14 tracey if (c == '\n') {
633 a596b957 2022-07-14 tracey file->lineno++;
634 a596b957 2022-07-14 tracey continue;
635 a596b957 2022-07-14 tracey } else if (c == '\\') {
636 a596b957 2022-07-14 tracey next = lgetc(quotec);
637 a596b957 2022-07-14 tracey if (next == EOF)
638 a596b957 2022-07-14 tracey return (0);
639 a596b957 2022-07-14 tracey if (next == quotec || c == ' ' || c == '\t')
640 a596b957 2022-07-14 tracey c = next;
641 a596b957 2022-07-14 tracey else if (next == '\n') {
642 a596b957 2022-07-14 tracey file->lineno++;
643 a596b957 2022-07-14 tracey continue;
645 a596b957 2022-07-14 tracey lungetc(next);
646 a596b957 2022-07-14 tracey } else if (c == quotec) {
647 a596b957 2022-07-14 tracey *p = '\0';
649 a596b957 2022-07-14 tracey } else if (c == '\0') {
650 a596b957 2022-07-14 tracey yyerror("syntax error");
651 a596b957 2022-07-14 tracey return (findeol());
653 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
654 a596b957 2022-07-14 tracey yyerror("string too long");
655 a596b957 2022-07-14 tracey return (findeol());
657 a596b957 2022-07-14 tracey *p++ = c;
659 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
660 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
661 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
662 a596b957 2022-07-14 tracey return (STRING);
665 a596b957 2022-07-14 tracey #define allowed_to_end_number(x) \
666 a596b957 2022-07-14 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
668 a596b957 2022-07-14 tracey if (c == '-' || isdigit(c)) {
670 a596b957 2022-07-14 tracey *p++ = c;
671 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
672 a596b957 2022-07-14 tracey yyerror("string too long");
673 a596b957 2022-07-14 tracey return (findeol());
675 a596b957 2022-07-14 tracey c = lgetc(0);
676 a596b957 2022-07-14 tracey } while (c != EOF && isdigit(c));
677 a596b957 2022-07-14 tracey lungetc(c);
678 a596b957 2022-07-14 tracey if (p == buf + 1 && buf[0] == '-')
679 a596b957 2022-07-14 tracey goto nodigits;
680 a596b957 2022-07-14 tracey if (c == EOF || allowed_to_end_number(c)) {
681 a596b957 2022-07-14 tracey const char *errstr = NULL;
683 a596b957 2022-07-14 tracey *p = '\0';
684 a596b957 2022-07-14 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
685 a596b957 2022-07-14 tracey LLONG_MAX, &errstr);
686 a596b957 2022-07-14 tracey if (errstr) {
687 a596b957 2022-07-14 tracey yyerror("\"%s\" invalid number: %s",
688 a596b957 2022-07-14 tracey buf, errstr);
689 a596b957 2022-07-14 tracey return (findeol());
691 a596b957 2022-07-14 tracey return (NUMBER);
693 a596b957 2022-07-14 tracey nodigits:
694 a596b957 2022-07-14 tracey while (p > buf + 1)
695 a596b957 2022-07-14 tracey lungetc(*--p);
696 a596b957 2022-07-14 tracey c = *--p;
697 a596b957 2022-07-14 tracey if (c == '-')
698 a596b957 2022-07-14 tracey return (c);
702 a596b957 2022-07-14 tracey #define allowed_in_string(x) \
703 a596b957 2022-07-14 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
704 a596b957 2022-07-14 tracey x != '{' && x != '}' && \
705 a596b957 2022-07-14 tracey x != '!' && x != '=' && x != '#' && \
706 a596b957 2022-07-14 tracey x != ','))
708 a596b957 2022-07-14 tracey if (isalnum(c) || c == ':' || c == '_') {
710 a596b957 2022-07-14 tracey *p++ = c;
711 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
712 a596b957 2022-07-14 tracey yyerror("string too long");
713 a596b957 2022-07-14 tracey return (findeol());
715 a596b957 2022-07-14 tracey c = lgetc(0);
716 a596b957 2022-07-14 tracey } while (c != EOF && (allowed_in_string(c)));
717 a596b957 2022-07-14 tracey lungetc(c);
718 a596b957 2022-07-14 tracey *p = '\0';
719 a596b957 2022-07-14 tracey token = lookup(buf);
720 a596b957 2022-07-14 tracey if (token == STRING) {
721 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
722 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
723 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
725 a596b957 2022-07-14 tracey return (token);
727 a596b957 2022-07-14 tracey if (c == '\n') {
728 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
729 a596b957 2022-07-14 tracey file->lineno++;
731 a596b957 2022-07-14 tracey if (c == EOF)
732 a596b957 2022-07-14 tracey return (0);
733 a596b957 2022-07-14 tracey return (c);
737 a596b957 2022-07-14 tracey check_file_secrecy(int fd, const char *fname)
739 a596b957 2022-07-14 tracey struct stat st;
741 a596b957 2022-07-14 tracey if (fstat(fd, &st)) {
742 a596b957 2022-07-14 tracey log_warn("cannot stat %s", fname);
743 a596b957 2022-07-14 tracey return (-1);
745 a596b957 2022-07-14 tracey if (st.st_uid != 0 && st.st_uid != getuid()) {
746 a596b957 2022-07-14 tracey log_warnx("%s: owner not root or current user", fname);
747 a596b957 2022-07-14 tracey return (-1);
749 a596b957 2022-07-14 tracey if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
750 a596b957 2022-07-14 tracey log_warnx("%s: group writable or world read/writable", fname);
751 a596b957 2022-07-14 tracey return (-1);
753 a596b957 2022-07-14 tracey return (0);
756 a596b957 2022-07-14 tracey struct file *
757 a596b957 2022-07-14 tracey newfile(const char *name, int secret)
759 a596b957 2022-07-14 tracey struct file *nfile;
761 a596b957 2022-07-14 tracey nfile = calloc(1, sizeof(struct file));
762 a596b957 2022-07-14 tracey if (nfile == NULL) {
763 a596b957 2022-07-14 tracey log_warn("calloc");
764 a596b957 2022-07-14 tracey return (NULL);
766 a596b957 2022-07-14 tracey nfile->name = strdup(name);
767 a596b957 2022-07-14 tracey if (nfile->name == NULL) {
768 a596b957 2022-07-14 tracey log_warn("strdup");
769 a596b957 2022-07-14 tracey free(nfile);
770 a596b957 2022-07-14 tracey return (NULL);
772 a596b957 2022-07-14 tracey nfile->stream = fopen(nfile->name, "r");
773 a596b957 2022-07-14 tracey if (nfile->stream == NULL) {
774 a596b957 2022-07-14 tracey /* no warning, we don't require a conf file */
775 a596b957 2022-07-14 tracey free(nfile->name);
776 a596b957 2022-07-14 tracey free(nfile);
777 a596b957 2022-07-14 tracey return (NULL);
778 a596b957 2022-07-14 tracey } else if (secret &&
779 a596b957 2022-07-14 tracey check_file_secrecy(fileno(nfile->stream), nfile->name)) {
780 a596b957 2022-07-14 tracey fclose(nfile->stream);
781 a596b957 2022-07-14 tracey free(nfile->name);
782 a596b957 2022-07-14 tracey free(nfile);
783 a596b957 2022-07-14 tracey return (NULL);
785 a596b957 2022-07-14 tracey nfile->lineno = 1;
786 a596b957 2022-07-14 tracey return (nfile);
789 a596b957 2022-07-14 tracey static void
790 a596b957 2022-07-14 tracey closefile(struct file *xfile)
792 a596b957 2022-07-14 tracey fclose(xfile->stream);
793 a596b957 2022-07-14 tracey free(xfile->name);
794 a596b957 2022-07-14 tracey free(xfile);
797 a0037b73 2022-08-03 stsp static void
798 a0037b73 2022-08-03 stsp add_default_server(void)
800 a0037b73 2022-08-03 stsp new_srv = conf_new_server(D_SITENAME);
801 a0037b73 2022-08-03 stsp log_debug("%s: adding default server %s", __func__, D_SITENAME);
805 a596b957 2022-07-14 tracey parse_config(const char *filename, struct gotwebd *env)
807 a596b957 2022-07-14 tracey struct sym *sym, *next;
808 36079424 2024-05-16 op struct server *srv;
811 a596b957 2022-07-14 tracey if (config_init(env) == -1)
812 a596b957 2022-07-14 tracey fatalx("failed to initialize configuration");
814 a596b957 2022-07-14 tracey gotwebd = env;
816 36079424 2024-05-16 op n = snprintf(env->unix_socket_name, sizeof(env->unix_socket_name),
817 36079424 2024-05-16 op "%s%s", D_HTTPD_CHROOT, D_UNIX_SOCKET);
818 36079424 2024-05-16 op if (n < 0 || (size_t)n >= sizeof(env->unix_socket_name))
819 36079424 2024-05-16 op fatalx("%s: snprintf", __func__);
821 a0037b73 2022-08-03 stsp file = newfile(filename, 0);
822 a0037b73 2022-08-03 stsp if (file == NULL) {
823 a0037b73 2022-08-03 stsp add_default_server();
824 a0037b73 2022-08-03 stsp sockets_parse_sockets(env);
825 a0037b73 2022-08-03 stsp /* just return, as we don't require a conf file */
826 a0037b73 2022-08-03 stsp return (0);
829 a596b957 2022-07-14 tracey yyparse();
830 a596b957 2022-07-14 tracey errors = file->errors;
831 a596b957 2022-07-14 tracey closefile(file);
833 a596b957 2022-07-14 tracey /* Free macros and check which have not been used. */
834 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
835 a596b957 2022-07-14 tracey if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
836 a596b957 2022-07-14 tracey fprintf(stderr, "warning: macro '%s' not used\n",
837 a596b957 2022-07-14 tracey sym->nam);
838 a596b957 2022-07-14 tracey if (!sym->persist) {
839 a596b957 2022-07-14 tracey free(sym->nam);
840 a596b957 2022-07-14 tracey free(sym->val);
841 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
842 a596b957 2022-07-14 tracey free(sym);
846 a596b957 2022-07-14 tracey /* just add default server if no config specified */
847 a0037b73 2022-08-03 stsp if (gotwebd->server_cnt == 0)
848 a0037b73 2022-08-03 stsp add_default_server();
850 36079424 2024-05-16 op /* add the implicit listen on socket where missing */
851 36079424 2024-05-16 op TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
852 36079424 2024-05-16 op if (!TAILQ_EMPTY(&srv->al))
854 36079424 2024-05-16 op if (get_unix_addr(env->unix_socket_name, srv) == -1)
855 36079424 2024-05-16 op yyerror("can't listen on %s", env->unix_socket_name);
861 a596b957 2022-07-14 tracey /* setup our listening sockets */
862 a596b957 2022-07-14 tracey sockets_parse_sockets(env);
864 a596b957 2022-07-14 tracey return (0);
867 a596b957 2022-07-14 tracey struct server *
868 a596b957 2022-07-14 tracey conf_new_server(const char *name)
870 a596b957 2022-07-14 tracey struct server *srv = NULL;
872 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
873 a596b957 2022-07-14 tracey if (srv == NULL)
874 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
876 a596b957 2022-07-14 tracey n = strlcpy(srv->name, name, sizeof(srv->name));
877 a596b957 2022-07-14 tracey if (n >= sizeof(srv->name))
878 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __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 8762929a 2023-12-29 op srv->summary_commits_display = D_MAXSLCOMMDISP;
916 8762929a 2023-12-29 op srv->summary_tags_display = D_MAXSLTAGDISP;
918 e087e1f6 2022-08-16 stsp TAILQ_INIT(&srv->al);
919 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
920 a596b957 2022-07-14 tracey gotwebd->server_cnt++;
922 a596b957 2022-07-14 tracey return srv;
926 a596b957 2022-07-14 tracey symset(const char *nam, const char *val, int persist)
928 a596b957 2022-07-14 tracey struct sym *sym;
930 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
931 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0)
935 a596b957 2022-07-14 tracey if (sym != NULL) {
936 a596b957 2022-07-14 tracey if (sym->persist == 1)
937 a596b957 2022-07-14 tracey return (0);
939 a596b957 2022-07-14 tracey free(sym->nam);
940 a596b957 2022-07-14 tracey free(sym->val);
941 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
942 a596b957 2022-07-14 tracey free(sym);
945 a596b957 2022-07-14 tracey sym = calloc(1, sizeof(*sym));
946 a596b957 2022-07-14 tracey if (sym == NULL)
947 a596b957 2022-07-14 tracey return (-1);
949 a596b957 2022-07-14 tracey sym->nam = strdup(nam);
950 a596b957 2022-07-14 tracey if (sym->nam == NULL) {
951 a596b957 2022-07-14 tracey free(sym);
952 a596b957 2022-07-14 tracey return (-1);
954 a596b957 2022-07-14 tracey sym->val = strdup(val);
955 a596b957 2022-07-14 tracey if (sym->val == NULL) {
956 a596b957 2022-07-14 tracey free(sym->nam);
957 a596b957 2022-07-14 tracey free(sym);
958 a596b957 2022-07-14 tracey return (-1);
960 a596b957 2022-07-14 tracey sym->used = 0;
961 a596b957 2022-07-14 tracey sym->persist = persist;
962 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
963 a596b957 2022-07-14 tracey return (0);
967 a596b957 2022-07-14 tracey cmdline_symset(char *s)
969 a596b957 2022-07-14 tracey char *sym, *val;
972 a596b957 2022-07-14 tracey val = strrchr(s, '=');
973 a596b957 2022-07-14 tracey if (val == NULL)
974 a596b957 2022-07-14 tracey return (-1);
976 4cdd299d 2022-09-05 op sym = strndup(s, val - s);
977 a596b957 2022-07-14 tracey if (sym == NULL)
978 4cdd299d 2022-09-05 op fatal("%s: strndup", __func__);
980 a596b957 2022-07-14 tracey ret = symset(sym, val + 1, 1);
981 a596b957 2022-07-14 tracey free(sym);
983 a596b957 2022-07-14 tracey return (ret);
987 a596b957 2022-07-14 tracey symget(const char *nam)
989 a596b957 2022-07-14 tracey struct sym *sym;
991 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
992 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0) {
993 a596b957 2022-07-14 tracey sym->used = 1;
994 a596b957 2022-07-14 tracey return (sym->val);
997 a596b957 2022-07-14 tracey return (NULL);
1001 89cfaaa7 2023-11-15 op get_addrs(const char *hostname, const char *servname, struct server *new_srv)
1003 a596b957 2022-07-14 tracey struct addrinfo hints, *res0, *res;
1005 cdfd248a 2023-11-15 op struct sockaddr_in *sin;
1006 cdfd248a 2023-11-15 op struct sockaddr_in6 *sin6;
1007 a596b957 2022-07-14 tracey struct address *h;
1009 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
1010 fb307946 2023-05-29 op hints.ai_family = AF_UNSPEC;
1011 89cfaaa7 2023-11-15 op hints.ai_socktype = SOCK_STREAM;
1012 c5e111b9 2023-11-15 op hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
1013 89cfaaa7 2023-11-15 op error = getaddrinfo(hostname, servname, &hints, &res0);
1014 a596b957 2022-07-14 tracey if (error) {
1015 89cfaaa7 2023-11-15 op log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
1016 89cfaaa7 2023-11-15 op hostname, servname, gai_strerror(error));
1017 a596b957 2022-07-14 tracey return (-1);
1020 c5e111b9 2023-11-15 op for (res = res0; res; res = res->ai_next) {
1021 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
1022 a596b957 2022-07-14 tracey fatal(__func__);
1024 89cfaaa7 2023-11-15 op if (hostname == NULL) {
1025 c5e111b9 2023-11-15 op strlcpy(h->ifname, "*", sizeof(h->ifname));
1027 89cfaaa7 2023-11-15 op if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1028 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1029 b8b20b3c 2023-11-15 op log_warnx("%s: address truncated: %s",
1030 b8b20b3c 2023-11-15 op __func__, hostname);
1031 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1032 a596b957 2022-07-14 tracey free(h);
1033 a596b957 2022-07-14 tracey return (-1);
1037 0c64c2f8 2023-11-15 op h->ai_family = res->ai_family;
1038 0c64c2f8 2023-11-15 op h->ai_socktype = res->ai_socktype;
1039 0c64c2f8 2023-11-15 op h->ai_protocol = res->ai_protocol;
1040 cdfd248a 2023-11-15 op memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
1041 cdfd248a 2023-11-15 op h->slen = res->ai_addrlen;
1043 89cfaaa7 2023-11-15 op switch (res->ai_family) {
1044 89cfaaa7 2023-11-15 op case AF_INET:
1045 cdfd248a 2023-11-15 op sin = (struct sockaddr_in *)res->ai_addr;
1046 cdfd248a 2023-11-15 op h->port = ntohs(sin->sin_port);
1048 89cfaaa7 2023-11-15 op case AF_INET6:
1049 cdfd248a 2023-11-15 op sin6 = (struct sockaddr_in6 *)res->ai_addr;
1050 cdfd248a 2023-11-15 op h->port = ntohs(sin6->sin6_port);
1053 89cfaaa7 2023-11-15 op fatalx("unknown address family %d", res->ai_family);
1056 eb916daf 2024-05-16 op if (add_addr(new_srv, h) == -1) {
1057 eb916daf 2024-05-16 op freeaddrinfo(res0);
1058 67d8de2a 2022-08-29 stsp return -1;
1061 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1062 a596b957 2022-07-14 tracey return (0);
1066 36079424 2024-05-16 op get_unix_addr(const char *path, struct server *new_srv)
1068 36079424 2024-05-16 op struct address *h;
1069 36079424 2024-05-16 op struct sockaddr_un *sun;
1071 36079424 2024-05-16 op if ((h = calloc(1, sizeof(*h))) == NULL)
1072 36079424 2024-05-16 op fatal("%s: calloc", __func__);
1074 36079424 2024-05-16 op h->ai_family = AF_UNIX;
1075 36079424 2024-05-16 op h->ai_socktype = SOCK_STREAM;
1076 36079424 2024-05-16 op h->ai_protocol = PF_UNSPEC;
1077 36079424 2024-05-16 op h->slen = sizeof(*sun);
1079 36079424 2024-05-16 op sun = (struct sockaddr_un *)&h->ss;
1080 36079424 2024-05-16 op sun->sun_family = AF_UNIX;
1081 36079424 2024-05-16 op if (strlcpy(sun->sun_path, path, sizeof(sun->sun_path)) >=
1082 36079424 2024-05-16 op sizeof(sun->sun_path)) {
1083 36079424 2024-05-16 op log_warnx("socket path too long: %s", sun->sun_path);
1084 36079424 2024-05-16 op return (-1);
1087 36079424 2024-05-16 op return add_addr(new_srv, h);
1091 36079424 2024-05-16 op addr_dup_check(struct addresslist *al, struct address *h)
1093 67d8de2a 2022-08-29 stsp struct address *a;
1095 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(a, al, entry) {
1096 0c64c2f8 2023-11-15 op if (a->ai_family != h->ai_family ||
1097 0c64c2f8 2023-11-15 op a->ai_socktype != h->ai_socktype ||
1098 0c64c2f8 2023-11-15 op a->ai_protocol != h->ai_protocol ||
1099 0c64c2f8 2023-11-15 op a->slen != h->slen ||
1100 cdfd248a 2023-11-15 op memcmp(&a->ss, &h->ss, a->slen) != 0)
1102 67d8de2a 2022-08-29 stsp return -1;
1109 67d8de2a 2022-08-29 stsp add_addr(struct server *new_srv, struct address *h)
1111 67d8de2a 2022-08-29 stsp struct server *srv;
1112 36079424 2024-05-16 op struct address *dup;
1114 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1115 36079424 2024-05-16 op if (addr_dup_check(&srv->al, h) == -1) {
1121 36079424 2024-05-16 op if (addr_dup_check(&gotwebd->addresses, h) == 0) {
1122 36079424 2024-05-16 op if ((dup = calloc(1, sizeof(*dup))) == NULL)
1123 36079424 2024-05-16 op fatal("%s: calloc", __func__);
1124 36079424 2024-05-16 op memcpy(dup, h, sizeof(*dup));
1125 36079424 2024-05-16 op TAILQ_INSERT_TAIL(&gotwebd->addresses, dup, entry);
1128 36079424 2024-05-16 op TAILQ_INSERT_TAIL(&new_srv->al, h, entry);