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 d68a7b14 2024-05-20 op int get_addrs(const char *, const char *);
98 d68a7b14 2024-05-20 op int get_unix_addr(const char *);
99 36079424 2024-05-16 op int addr_dup_check(struct addresslist *, struct address *);
100 d68a7b14 2024-05-20 op int add_addr(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 c16cda05 2024-05-20 op %token 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 d68a7b14 2024-05-20 op | LISTEN ON listen_addr PORT STRING {
206 d68a7b14 2024-05-20 op if (get_addrs($3, $5) == -1) {
207 d68a7b14 2024-05-20 op yyerror("could not get addrs");
213 d68a7b14 2024-05-20 op | LISTEN ON listen_addr PORT NUMBER {
214 d68a7b14 2024-05-20 op char portno[32];
217 d68a7b14 2024-05-20 op n = snprintf(portno, sizeof(portno), "%lld",
218 d68a7b14 2024-05-20 op (long long)$5);
219 d68a7b14 2024-05-20 op if (n < 0 || (size_t)n >= sizeof(portno))
220 d68a7b14 2024-05-20 op fatalx("port number too long: %lld",
221 d68a7b14 2024-05-20 op (long long)$5);
223 d68a7b14 2024-05-20 op if (get_addrs($3, portno) == -1) {
224 d68a7b14 2024-05-20 op yyerror("could not get addrs");
229 d68a7b14 2024-05-20 op | LISTEN ON SOCKET STRING {
230 d68a7b14 2024-05-20 op if (get_unix_addr($4) == -1) {
231 d68a7b14 2024-05-20 op yyerror("can't listen on %s", $4);
239 a596b957 2022-07-14 tracey server : SERVER STRING {
240 a596b957 2022-07-14 tracey struct server *srv;
242 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
243 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
244 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
245 a596b957 2022-07-14 tracey free($2);
250 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
251 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
252 a596b957 2022-07-14 tracey free($2);
254 a596b957 2022-07-14 tracey | SERVER STRING {
255 a596b957 2022-07-14 tracey struct server *srv;
257 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
258 a596b957 2022-07-14 tracey if (strcmp(srv->name, $2) == 0) {
259 a596b957 2022-07-14 tracey yyerror("server name exists '%s'", $2);
260 a596b957 2022-07-14 tracey free($2);
265 a596b957 2022-07-14 tracey new_srv = conf_new_server($2);
266 a596b957 2022-07-14 tracey log_debug("adding server %s", $2);
267 a596b957 2022-07-14 tracey free($2);
268 a596b957 2022-07-14 tracey } '{' optnl serveropts2 '}' {
272 a596b957 2022-07-14 tracey serveropts1 : REPOS_PATH STRING {
273 a596b957 2022-07-14 tracey n = strlcpy(new_srv->repos_path, $2,
274 a596b957 2022-07-14 tracey sizeof(new_srv->repos_path));
275 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->repos_path)) {
276 a596b957 2022-07-14 tracey yyerror("%s: repos_path truncated", __func__);
277 a596b957 2022-07-14 tracey free($2);
280 a596b957 2022-07-14 tracey free($2);
282 a596b957 2022-07-14 tracey | SITE_NAME STRING {
283 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_name, $2,
284 a596b957 2022-07-14 tracey sizeof(new_srv->site_name));
285 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_name)) {
286 a596b957 2022-07-14 tracey yyerror("%s: site_name truncated", __func__);
287 a596b957 2022-07-14 tracey free($2);
290 a596b957 2022-07-14 tracey free($2);
292 a596b957 2022-07-14 tracey | SITE_OWNER STRING {
293 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_owner, $2,
294 a596b957 2022-07-14 tracey sizeof(new_srv->site_owner));
295 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_owner)) {
296 a596b957 2022-07-14 tracey yyerror("%s: site_owner truncated", __func__);
297 a596b957 2022-07-14 tracey free($2);
300 a596b957 2022-07-14 tracey free($2);
302 a596b957 2022-07-14 tracey | SITE_LINK STRING {
303 a596b957 2022-07-14 tracey n = strlcpy(new_srv->site_link, $2,
304 a596b957 2022-07-14 tracey sizeof(new_srv->site_link));
305 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->site_link)) {
306 a596b957 2022-07-14 tracey yyerror("%s: site_link truncated", __func__);
307 a596b957 2022-07-14 tracey free($2);
310 a596b957 2022-07-14 tracey free($2);
312 a596b957 2022-07-14 tracey | LOGO STRING {
313 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
314 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo)) {
315 a596b957 2022-07-14 tracey yyerror("%s: logo truncated", __func__);
316 a596b957 2022-07-14 tracey free($2);
319 a596b957 2022-07-14 tracey free($2);
321 a596b957 2022-07-14 tracey | LOGO_URL STRING {
322 a596b957 2022-07-14 tracey n = strlcpy(new_srv->logo_url, $2,
323 a596b957 2022-07-14 tracey sizeof(new_srv->logo_url));
324 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->logo_url)) {
325 a596b957 2022-07-14 tracey yyerror("%s: logo_url truncated", __func__);
326 a596b957 2022-07-14 tracey free($2);
329 a596b957 2022-07-14 tracey free($2);
331 a596b957 2022-07-14 tracey | CUSTOM_CSS STRING {
332 a596b957 2022-07-14 tracey n = strlcpy(new_srv->custom_css, $2,
333 a596b957 2022-07-14 tracey sizeof(new_srv->custom_css));
334 a596b957 2022-07-14 tracey if (n >= sizeof(new_srv->custom_css)) {
335 a596b957 2022-07-14 tracey yyerror("%s: custom_css truncated", __func__);
336 a596b957 2022-07-14 tracey free($2);
339 a596b957 2022-07-14 tracey free($2);
341 a596b957 2022-07-14 tracey | SHOW_SITE_OWNER boolean {
342 a596b957 2022-07-14 tracey new_srv->show_site_owner = $2;
344 a596b957 2022-07-14 tracey | SHOW_REPO_OWNER boolean {
345 a596b957 2022-07-14 tracey new_srv->show_repo_owner = $2;
347 a596b957 2022-07-14 tracey | SHOW_REPO_AGE boolean {
348 a596b957 2022-07-14 tracey new_srv->show_repo_age = $2;
350 a596b957 2022-07-14 tracey | SHOW_REPO_DESCRIPTION boolean {
351 a596b957 2022-07-14 tracey new_srv->show_repo_description = $2;
353 a596b957 2022-07-14 tracey | SHOW_REPO_CLONEURL boolean {
354 a596b957 2022-07-14 tracey new_srv->show_repo_cloneurl = $2;
356 d5996b9e 2022-10-31 landry | RESPECT_EXPORTOK boolean {
357 d5996b9e 2022-10-31 landry new_srv->respect_exportok = $2;
359 a596b957 2022-07-14 tracey | MAX_REPOS_DISPLAY NUMBER {
360 20f27972 2023-06-19 op if ($2 < 0) {
361 1a0c81fb 2023-06-15 op yyerror("max_repos_display is too small: %lld",
365 1a0c81fb 2023-06-15 op new_srv->max_repos_display = $2;
367 a596b957 2022-07-14 tracey | MAX_COMMITS_DISPLAY NUMBER {
368 f4425f95 2023-06-14 op if ($2 <= 1) {
369 f4425f95 2023-06-14 op yyerror("max_commits_display is too small:"
370 f4425f95 2023-06-14 op " %lld", $2);
373 f4425f95 2023-06-14 op new_srv->max_commits_display = $2;
375 8762929a 2023-12-29 op | SUMMARY_COMMITS_DISPLAY NUMBER {
376 8762929a 2023-12-29 op if ($2 < 1) {
377 8762929a 2023-12-29 op yyerror("summary_commits_display is too small:"
378 8762929a 2023-12-29 op " %lld", $2);
381 8762929a 2023-12-29 op new_srv->summary_commits_display = $2;
383 8762929a 2023-12-29 op | SUMMARY_TAGS_DISPLAY NUMBER {
384 8762929a 2023-12-29 op if ($2 < 1) {
385 8762929a 2023-12-29 op yyerror("summary_tags_display is too small:"
386 8762929a 2023-12-29 op " %lld", $2);
389 8762929a 2023-12-29 op new_srv->summary_tags_display = $2;
393 a596b957 2022-07-14 tracey serveropts2 : serveropts2 serveropts1 nl
394 a596b957 2022-07-14 tracey | serveropts1 optnl
397 a596b957 2022-07-14 tracey nl : '\n' optnl
400 a596b957 2022-07-14 tracey optnl : '\n' optnl /* zero or more newlines */
401 a596b957 2022-07-14 tracey | /* empty */
406 a596b957 2022-07-14 tracey struct keywords {
407 a596b957 2022-07-14 tracey const char *k_name;
408 a596b957 2022-07-14 tracey int k_val;
412 a596b957 2022-07-14 tracey yyerror(const char *fmt, ...)
414 a596b957 2022-07-14 tracey va_list ap;
415 a596b957 2022-07-14 tracey char *msg;
417 a596b957 2022-07-14 tracey file->errors++;
418 a596b957 2022-07-14 tracey va_start(ap, fmt);
419 a596b957 2022-07-14 tracey if (vasprintf(&msg, fmt, ap) == -1)
420 a596b957 2022-07-14 tracey fatalx("yyerror vasprintf");
421 a596b957 2022-07-14 tracey va_end(ap);
422 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
423 a596b957 2022-07-14 tracey free(msg);
424 a596b957 2022-07-14 tracey return (0);
428 a596b957 2022-07-14 tracey kw_cmp(const void *k, const void *e)
430 a596b957 2022-07-14 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
434 a596b957 2022-07-14 tracey lookup(char *s)
436 a596b957 2022-07-14 tracey /* This has to be sorted always. */
437 a596b957 2022-07-14 tracey static const struct keywords keywords[] = {
438 a596b957 2022-07-14 tracey { "chroot", CHROOT },
439 a596b957 2022-07-14 tracey { "custom_css", CUSTOM_CSS },
440 d8473d93 2022-08-11 stsp { "listen", LISTEN },
441 a596b957 2022-07-14 tracey { "logo", LOGO },
442 8556b86b 2023-01-02 op { "logo_url", LOGO_URL },
443 a596b957 2022-07-14 tracey { "max_commits_display", MAX_COMMITS_DISPLAY },
444 a596b957 2022-07-14 tracey { "max_repos_display", MAX_REPOS_DISPLAY },
445 d8473d93 2022-08-11 stsp { "on", ON },
446 a596b957 2022-07-14 tracey { "port", PORT },
447 a596b957 2022-07-14 tracey { "prefork", PREFORK },
448 a596b957 2022-07-14 tracey { "repos_path", REPOS_PATH },
449 d5996b9e 2022-10-31 landry { "respect_exportok", RESPECT_EXPORTOK },
450 a596b957 2022-07-14 tracey { "server", SERVER },
451 a596b957 2022-07-14 tracey { "show_repo_age", SHOW_REPO_AGE },
452 a596b957 2022-07-14 tracey { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
453 a596b957 2022-07-14 tracey { "show_repo_description", SHOW_REPO_DESCRIPTION },
454 a596b957 2022-07-14 tracey { "show_repo_owner", SHOW_REPO_OWNER },
455 a596b957 2022-07-14 tracey { "show_site_owner", SHOW_SITE_OWNER },
456 a596b957 2022-07-14 tracey { "site_link", SITE_LINK },
457 a596b957 2022-07-14 tracey { "site_name", SITE_NAME },
458 a596b957 2022-07-14 tracey { "site_owner", SITE_OWNER },
459 3a1c1a1b 2023-01-04 op { "socket", SOCKET },
460 8762929a 2023-12-29 op { "summary_commits_display", SUMMARY_COMMITS_DISPLAY },
461 8762929a 2023-12-29 op { "summary_tags_display", SUMMARY_TAGS_DISPLAY },
463 a596b957 2022-07-14 tracey const struct keywords *p;
465 a596b957 2022-07-14 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
466 a596b957 2022-07-14 tracey sizeof(keywords[0]), kw_cmp);
469 a596b957 2022-07-14 tracey return (p->k_val);
471 a596b957 2022-07-14 tracey return (STRING);
474 a596b957 2022-07-14 tracey #define MAXPUSHBACK 128
476 a596b957 2022-07-14 tracey unsigned char *parsebuf;
477 a596b957 2022-07-14 tracey int parseindex;
478 a596b957 2022-07-14 tracey unsigned char pushback_buffer[MAXPUSHBACK];
479 a596b957 2022-07-14 tracey int pushback_index = 0;
482 a596b957 2022-07-14 tracey lgetc(int quotec)
484 a596b957 2022-07-14 tracey int c, next;
486 a596b957 2022-07-14 tracey if (parsebuf) {
487 a596b957 2022-07-14 tracey /* Read character from the parsebuffer instead of input. */
488 a596b957 2022-07-14 tracey if (parseindex >= 0) {
489 a596b957 2022-07-14 tracey c = parsebuf[parseindex++];
490 a596b957 2022-07-14 tracey if (c != '\0')
491 a596b957 2022-07-14 tracey return (c);
492 a596b957 2022-07-14 tracey parsebuf = NULL;
494 a596b957 2022-07-14 tracey parseindex++;
497 a596b957 2022-07-14 tracey if (pushback_index)
498 a596b957 2022-07-14 tracey return (pushback_buffer[--pushback_index]);
500 a596b957 2022-07-14 tracey if (quotec) {
501 a596b957 2022-07-14 tracey c = getc(file->stream);
502 a596b957 2022-07-14 tracey if (c == EOF)
503 a596b957 2022-07-14 tracey yyerror("reached end of file while parsing "
504 a596b957 2022-07-14 tracey "quoted string");
505 a596b957 2022-07-14 tracey return (c);
508 a596b957 2022-07-14 tracey c = getc(file->stream);
509 a596b957 2022-07-14 tracey while (c == '\\') {
510 a596b957 2022-07-14 tracey next = getc(file->stream);
511 a596b957 2022-07-14 tracey if (next != '\n') {
512 a596b957 2022-07-14 tracey c = next;
515 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
516 a596b957 2022-07-14 tracey file->lineno++;
517 a596b957 2022-07-14 tracey c = getc(file->stream);
520 a596b957 2022-07-14 tracey return (c);
524 a596b957 2022-07-14 tracey lungetc(int c)
526 a596b957 2022-07-14 tracey if (c == EOF)
527 a596b957 2022-07-14 tracey return (EOF);
528 a596b957 2022-07-14 tracey if (parsebuf) {
529 a596b957 2022-07-14 tracey parseindex--;
530 a596b957 2022-07-14 tracey if (parseindex >= 0)
531 a596b957 2022-07-14 tracey return (c);
533 a596b957 2022-07-14 tracey if (pushback_index < MAXPUSHBACK-1)
534 a596b957 2022-07-14 tracey return (pushback_buffer[pushback_index++] = c);
536 a596b957 2022-07-14 tracey return (EOF);
540 a596b957 2022-07-14 tracey findeol(void)
544 a596b957 2022-07-14 tracey parsebuf = NULL;
546 a596b957 2022-07-14 tracey /* Skip to either EOF or the first real EOL. */
547 a596b957 2022-07-14 tracey while (1) {
548 a596b957 2022-07-14 tracey if (pushback_index)
549 a596b957 2022-07-14 tracey c = pushback_buffer[--pushback_index];
551 a596b957 2022-07-14 tracey c = lgetc(0);
552 a596b957 2022-07-14 tracey if (c == '\n') {
553 a596b957 2022-07-14 tracey file->lineno++;
556 a596b957 2022-07-14 tracey if (c == EOF)
559 a596b957 2022-07-14 tracey return (ERROR);
563 a596b957 2022-07-14 tracey yylex(void)
565 a596b957 2022-07-14 tracey unsigned char buf[8096];
566 a596b957 2022-07-14 tracey unsigned char *p, *val;
567 a596b957 2022-07-14 tracey int quotec, next, c;
568 a596b957 2022-07-14 tracey int token;
572 a596b957 2022-07-14 tracey c = lgetc(0);
573 a596b957 2022-07-14 tracey while (c == ' ' || c == '\t')
574 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
576 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
577 a596b957 2022-07-14 tracey if (c == '#') {
578 a596b957 2022-07-14 tracey c = lgetc(0);
579 a596b957 2022-07-14 tracey while (c != '\n' && c != EOF)
580 a596b957 2022-07-14 tracey c = lgetc(0); /* nothing */
582 a596b957 2022-07-14 tracey if (c == '$' && parsebuf == NULL) {
583 a596b957 2022-07-14 tracey while (1) {
584 a596b957 2022-07-14 tracey c = lgetc(0);
585 a596b957 2022-07-14 tracey if (c == EOF)
586 a596b957 2022-07-14 tracey return (0);
588 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
589 a596b957 2022-07-14 tracey yyerror("string too long");
590 a596b957 2022-07-14 tracey return (findeol());
592 a596b957 2022-07-14 tracey if (isalnum(c) || c == '_') {
593 a596b957 2022-07-14 tracey *p++ = c;
594 a596b957 2022-07-14 tracey continue;
596 a596b957 2022-07-14 tracey *p = '\0';
597 a596b957 2022-07-14 tracey lungetc(c);
600 a596b957 2022-07-14 tracey val = symget(buf);
601 a596b957 2022-07-14 tracey if (val == NULL) {
602 a596b957 2022-07-14 tracey yyerror("macro '%s' not defined", buf);
603 a596b957 2022-07-14 tracey return (findeol());
605 a596b957 2022-07-14 tracey parsebuf = val;
606 a596b957 2022-07-14 tracey parseindex = 0;
607 a596b957 2022-07-14 tracey goto top;
610 a596b957 2022-07-14 tracey switch (c) {
611 a596b957 2022-07-14 tracey case '\'':
612 a596b957 2022-07-14 tracey case '"':
613 a596b957 2022-07-14 tracey quotec = c;
614 a596b957 2022-07-14 tracey while (1) {
615 a596b957 2022-07-14 tracey c = lgetc(quotec);
616 a596b957 2022-07-14 tracey if (c == EOF)
617 a596b957 2022-07-14 tracey return (0);
618 a596b957 2022-07-14 tracey if (c == '\n') {
619 a596b957 2022-07-14 tracey file->lineno++;
620 a596b957 2022-07-14 tracey continue;
621 a596b957 2022-07-14 tracey } else if (c == '\\') {
622 a596b957 2022-07-14 tracey next = lgetc(quotec);
623 a596b957 2022-07-14 tracey if (next == EOF)
624 a596b957 2022-07-14 tracey return (0);
625 a596b957 2022-07-14 tracey if (next == quotec || c == ' ' || c == '\t')
626 a596b957 2022-07-14 tracey c = next;
627 a596b957 2022-07-14 tracey else if (next == '\n') {
628 a596b957 2022-07-14 tracey file->lineno++;
629 a596b957 2022-07-14 tracey continue;
631 a596b957 2022-07-14 tracey lungetc(next);
632 a596b957 2022-07-14 tracey } else if (c == quotec) {
633 a596b957 2022-07-14 tracey *p = '\0';
635 a596b957 2022-07-14 tracey } else if (c == '\0') {
636 a596b957 2022-07-14 tracey yyerror("syntax error");
637 a596b957 2022-07-14 tracey return (findeol());
639 a596b957 2022-07-14 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
640 a596b957 2022-07-14 tracey yyerror("string too long");
641 a596b957 2022-07-14 tracey return (findeol());
643 a596b957 2022-07-14 tracey *p++ = c;
645 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
646 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
647 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
648 a596b957 2022-07-14 tracey return (STRING);
651 a596b957 2022-07-14 tracey #define allowed_to_end_number(x) \
652 a596b957 2022-07-14 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
654 a596b957 2022-07-14 tracey if (c == '-' || isdigit(c)) {
656 a596b957 2022-07-14 tracey *p++ = c;
657 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
658 a596b957 2022-07-14 tracey yyerror("string too long");
659 a596b957 2022-07-14 tracey return (findeol());
661 a596b957 2022-07-14 tracey c = lgetc(0);
662 a596b957 2022-07-14 tracey } while (c != EOF && isdigit(c));
663 a596b957 2022-07-14 tracey lungetc(c);
664 a596b957 2022-07-14 tracey if (p == buf + 1 && buf[0] == '-')
665 a596b957 2022-07-14 tracey goto nodigits;
666 a596b957 2022-07-14 tracey if (c == EOF || allowed_to_end_number(c)) {
667 a596b957 2022-07-14 tracey const char *errstr = NULL;
669 a596b957 2022-07-14 tracey *p = '\0';
670 a596b957 2022-07-14 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
671 a596b957 2022-07-14 tracey LLONG_MAX, &errstr);
672 a596b957 2022-07-14 tracey if (errstr) {
673 a596b957 2022-07-14 tracey yyerror("\"%s\" invalid number: %s",
674 a596b957 2022-07-14 tracey buf, errstr);
675 a596b957 2022-07-14 tracey return (findeol());
677 a596b957 2022-07-14 tracey return (NUMBER);
679 a596b957 2022-07-14 tracey nodigits:
680 a596b957 2022-07-14 tracey while (p > buf + 1)
681 a596b957 2022-07-14 tracey lungetc(*--p);
682 a596b957 2022-07-14 tracey c = *--p;
683 a596b957 2022-07-14 tracey if (c == '-')
684 a596b957 2022-07-14 tracey return (c);
688 a596b957 2022-07-14 tracey #define allowed_in_string(x) \
689 a596b957 2022-07-14 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
690 a596b957 2022-07-14 tracey x != '{' && x != '}' && \
691 a596b957 2022-07-14 tracey x != '!' && x != '=' && x != '#' && \
692 a596b957 2022-07-14 tracey x != ','))
694 a596b957 2022-07-14 tracey if (isalnum(c) || c == ':' || c == '_') {
696 a596b957 2022-07-14 tracey *p++ = c;
697 a596b957 2022-07-14 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
698 a596b957 2022-07-14 tracey yyerror("string too long");
699 a596b957 2022-07-14 tracey return (findeol());
701 a596b957 2022-07-14 tracey c = lgetc(0);
702 a596b957 2022-07-14 tracey } while (c != EOF && (allowed_in_string(c)));
703 a596b957 2022-07-14 tracey lungetc(c);
704 a596b957 2022-07-14 tracey *p = '\0';
705 a596b957 2022-07-14 tracey token = lookup(buf);
706 a596b957 2022-07-14 tracey if (token == STRING) {
707 a596b957 2022-07-14 tracey yylval.v.string = strdup(buf);
708 a596b957 2022-07-14 tracey if (yylval.v.string == NULL)
709 a596b957 2022-07-14 tracey err(1, "yylex: strdup");
711 a596b957 2022-07-14 tracey return (token);
713 a596b957 2022-07-14 tracey if (c == '\n') {
714 a596b957 2022-07-14 tracey yylval.lineno = file->lineno;
715 a596b957 2022-07-14 tracey file->lineno++;
717 a596b957 2022-07-14 tracey if (c == EOF)
718 a596b957 2022-07-14 tracey return (0);
719 a596b957 2022-07-14 tracey return (c);
723 a596b957 2022-07-14 tracey check_file_secrecy(int fd, const char *fname)
725 a596b957 2022-07-14 tracey struct stat st;
727 a596b957 2022-07-14 tracey if (fstat(fd, &st)) {
728 a596b957 2022-07-14 tracey log_warn("cannot stat %s", fname);
729 a596b957 2022-07-14 tracey return (-1);
731 a596b957 2022-07-14 tracey if (st.st_uid != 0 && st.st_uid != getuid()) {
732 a596b957 2022-07-14 tracey log_warnx("%s: owner not root or current user", fname);
733 a596b957 2022-07-14 tracey return (-1);
735 a596b957 2022-07-14 tracey if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
736 a596b957 2022-07-14 tracey log_warnx("%s: group writable or world read/writable", fname);
737 a596b957 2022-07-14 tracey return (-1);
739 a596b957 2022-07-14 tracey return (0);
742 a596b957 2022-07-14 tracey struct file *
743 a596b957 2022-07-14 tracey newfile(const char *name, int secret)
745 a596b957 2022-07-14 tracey struct file *nfile;
747 a596b957 2022-07-14 tracey nfile = calloc(1, sizeof(struct file));
748 a596b957 2022-07-14 tracey if (nfile == NULL) {
749 a596b957 2022-07-14 tracey log_warn("calloc");
750 a596b957 2022-07-14 tracey return (NULL);
752 a596b957 2022-07-14 tracey nfile->name = strdup(name);
753 a596b957 2022-07-14 tracey if (nfile->name == NULL) {
754 a596b957 2022-07-14 tracey log_warn("strdup");
755 a596b957 2022-07-14 tracey free(nfile);
756 a596b957 2022-07-14 tracey return (NULL);
758 a596b957 2022-07-14 tracey nfile->stream = fopen(nfile->name, "r");
759 a596b957 2022-07-14 tracey if (nfile->stream == NULL) {
760 a596b957 2022-07-14 tracey /* no warning, we don't require a conf file */
761 a596b957 2022-07-14 tracey free(nfile->name);
762 a596b957 2022-07-14 tracey free(nfile);
763 a596b957 2022-07-14 tracey return (NULL);
764 a596b957 2022-07-14 tracey } else if (secret &&
765 a596b957 2022-07-14 tracey check_file_secrecy(fileno(nfile->stream), nfile->name)) {
766 a596b957 2022-07-14 tracey fclose(nfile->stream);
767 a596b957 2022-07-14 tracey free(nfile->name);
768 a596b957 2022-07-14 tracey free(nfile);
769 a596b957 2022-07-14 tracey return (NULL);
771 a596b957 2022-07-14 tracey nfile->lineno = 1;
772 a596b957 2022-07-14 tracey return (nfile);
775 a596b957 2022-07-14 tracey static void
776 a596b957 2022-07-14 tracey closefile(struct file *xfile)
778 a596b957 2022-07-14 tracey fclose(xfile->stream);
779 a596b957 2022-07-14 tracey free(xfile->name);
780 a596b957 2022-07-14 tracey free(xfile);
783 a0037b73 2022-08-03 stsp static void
784 a0037b73 2022-08-03 stsp add_default_server(void)
786 a0037b73 2022-08-03 stsp new_srv = conf_new_server(D_SITENAME);
787 a0037b73 2022-08-03 stsp log_debug("%s: adding default server %s", __func__, D_SITENAME);
791 a596b957 2022-07-14 tracey parse_config(const char *filename, struct gotwebd *env)
793 a596b957 2022-07-14 tracey struct sym *sym, *next;
795 a596b957 2022-07-14 tracey if (config_init(env) == -1)
796 a596b957 2022-07-14 tracey fatalx("failed to initialize configuration");
798 a596b957 2022-07-14 tracey gotwebd = env;
800 a0037b73 2022-08-03 stsp file = newfile(filename, 0);
801 a0037b73 2022-08-03 stsp if (file == NULL) {
802 a0037b73 2022-08-03 stsp add_default_server();
803 a0037b73 2022-08-03 stsp sockets_parse_sockets(env);
804 a0037b73 2022-08-03 stsp /* just return, as we don't require a conf file */
805 a0037b73 2022-08-03 stsp return (0);
808 a596b957 2022-07-14 tracey yyparse();
809 a596b957 2022-07-14 tracey errors = file->errors;
810 a596b957 2022-07-14 tracey closefile(file);
812 a596b957 2022-07-14 tracey /* Free macros and check which have not been used. */
813 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
814 a596b957 2022-07-14 tracey if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
815 a596b957 2022-07-14 tracey fprintf(stderr, "warning: macro '%s' not used\n",
816 a596b957 2022-07-14 tracey sym->nam);
817 a596b957 2022-07-14 tracey if (!sym->persist) {
818 a596b957 2022-07-14 tracey free(sym->nam);
819 a596b957 2022-07-14 tracey free(sym->val);
820 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
821 a596b957 2022-07-14 tracey free(sym);
825 a596b957 2022-07-14 tracey /* just add default server if no config specified */
826 a0037b73 2022-08-03 stsp if (gotwebd->server_cnt == 0)
827 a0037b73 2022-08-03 stsp add_default_server();
829 d68a7b14 2024-05-20 op /* add the implicit listen on socket */
830 d68a7b14 2024-05-20 op if (TAILQ_EMPTY(&gotwebd->addresses)) {
831 d68a7b14 2024-05-20 op const char *path = D_HTTPD_CHROOT D_UNIX_SOCKET;
832 d68a7b14 2024-05-20 op if (get_unix_addr(path) == -1)
833 d68a7b14 2024-05-20 op yyerror("can't listen on %s", path);
839 a596b957 2022-07-14 tracey /* setup our listening sockets */
840 a596b957 2022-07-14 tracey sockets_parse_sockets(env);
842 a596b957 2022-07-14 tracey return (0);
845 a596b957 2022-07-14 tracey struct server *
846 a596b957 2022-07-14 tracey conf_new_server(const char *name)
848 a596b957 2022-07-14 tracey struct server *srv = NULL;
850 a596b957 2022-07-14 tracey srv = calloc(1, sizeof(*srv));
851 a596b957 2022-07-14 tracey if (srv == NULL)
852 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
854 a596b957 2022-07-14 tracey n = strlcpy(srv->name, name, sizeof(srv->name));
855 a596b957 2022-07-14 tracey if (n >= sizeof(srv->name))
856 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
857 a596b957 2022-07-14 tracey n = strlcpy(srv->repos_path, D_GOTPATH,
858 a596b957 2022-07-14 tracey sizeof(srv->repos_path));
859 a596b957 2022-07-14 tracey if (n >= sizeof(srv->repos_path))
860 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
861 a596b957 2022-07-14 tracey n = strlcpy(srv->site_name, D_SITENAME,
862 a596b957 2022-07-14 tracey sizeof(srv->site_name));
863 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_name))
864 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
865 a596b957 2022-07-14 tracey n = strlcpy(srv->site_owner, D_SITEOWNER,
866 a596b957 2022-07-14 tracey sizeof(srv->site_owner));
867 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_owner))
868 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
869 a596b957 2022-07-14 tracey n = strlcpy(srv->site_link, D_SITELINK,
870 a596b957 2022-07-14 tracey sizeof(srv->site_link));
871 a596b957 2022-07-14 tracey if (n >= sizeof(srv->site_link))
872 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
873 a596b957 2022-07-14 tracey n = strlcpy(srv->logo, D_GOTLOGO,
874 a596b957 2022-07-14 tracey sizeof(srv->logo));
875 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo))
876 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
877 a596b957 2022-07-14 tracey n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
878 a596b957 2022-07-14 tracey if (n >= sizeof(srv->logo_url))
879 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
880 a596b957 2022-07-14 tracey n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
881 a596b957 2022-07-14 tracey if (n >= sizeof(srv->custom_css))
882 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
884 a596b957 2022-07-14 tracey srv->show_site_owner = D_SHOWSOWNER;
885 a596b957 2022-07-14 tracey srv->show_repo_owner = D_SHOWROWNER;
886 a596b957 2022-07-14 tracey srv->show_repo_age = D_SHOWAGE;
887 a596b957 2022-07-14 tracey srv->show_repo_description = D_SHOWDESC;
888 a596b957 2022-07-14 tracey srv->show_repo_cloneurl = D_SHOWURL;
889 d5996b9e 2022-10-31 landry srv->respect_exportok = D_RESPECTEXPORTOK;
891 a596b957 2022-07-14 tracey srv->max_repos_display = D_MAXREPODISP;
892 a596b957 2022-07-14 tracey srv->max_commits_display = D_MAXCOMMITDISP;
893 8762929a 2023-12-29 op srv->summary_commits_display = D_MAXSLCOMMDISP;
894 8762929a 2023-12-29 op srv->summary_tags_display = D_MAXSLTAGDISP;
896 2ad48e9a 2022-08-16 stsp TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
897 a596b957 2022-07-14 tracey gotwebd->server_cnt++;
899 a596b957 2022-07-14 tracey return srv;
903 a596b957 2022-07-14 tracey symset(const char *nam, const char *val, int persist)
905 a596b957 2022-07-14 tracey struct sym *sym;
907 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
908 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0)
912 a596b957 2022-07-14 tracey if (sym != NULL) {
913 a596b957 2022-07-14 tracey if (sym->persist == 1)
914 a596b957 2022-07-14 tracey return (0);
916 a596b957 2022-07-14 tracey free(sym->nam);
917 a596b957 2022-07-14 tracey free(sym->val);
918 a596b957 2022-07-14 tracey TAILQ_REMOVE(&symhead, sym, entry);
919 a596b957 2022-07-14 tracey free(sym);
922 a596b957 2022-07-14 tracey sym = calloc(1, sizeof(*sym));
923 a596b957 2022-07-14 tracey if (sym == NULL)
924 a596b957 2022-07-14 tracey return (-1);
926 a596b957 2022-07-14 tracey sym->nam = strdup(nam);
927 a596b957 2022-07-14 tracey if (sym->nam == NULL) {
928 a596b957 2022-07-14 tracey free(sym);
929 a596b957 2022-07-14 tracey return (-1);
931 a596b957 2022-07-14 tracey sym->val = strdup(val);
932 a596b957 2022-07-14 tracey if (sym->val == NULL) {
933 a596b957 2022-07-14 tracey free(sym->nam);
934 a596b957 2022-07-14 tracey free(sym);
935 a596b957 2022-07-14 tracey return (-1);
937 a596b957 2022-07-14 tracey sym->used = 0;
938 a596b957 2022-07-14 tracey sym->persist = persist;
939 a596b957 2022-07-14 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
940 a596b957 2022-07-14 tracey return (0);
944 a596b957 2022-07-14 tracey cmdline_symset(char *s)
946 a596b957 2022-07-14 tracey char *sym, *val;
949 a596b957 2022-07-14 tracey val = strrchr(s, '=');
950 a596b957 2022-07-14 tracey if (val == NULL)
951 a596b957 2022-07-14 tracey return (-1);
953 4cdd299d 2022-09-05 op sym = strndup(s, val - s);
954 a596b957 2022-07-14 tracey if (sym == NULL)
955 4cdd299d 2022-09-05 op fatal("%s: strndup", __func__);
957 a596b957 2022-07-14 tracey ret = symset(sym, val + 1, 1);
958 a596b957 2022-07-14 tracey free(sym);
960 a596b957 2022-07-14 tracey return (ret);
964 a596b957 2022-07-14 tracey symget(const char *nam)
966 a596b957 2022-07-14 tracey struct sym *sym;
968 a596b957 2022-07-14 tracey TAILQ_FOREACH(sym, &symhead, entry) {
969 a596b957 2022-07-14 tracey if (strcmp(nam, sym->nam) == 0) {
970 a596b957 2022-07-14 tracey sym->used = 1;
971 a596b957 2022-07-14 tracey return (sym->val);
974 a596b957 2022-07-14 tracey return (NULL);
978 d68a7b14 2024-05-20 op get_addrs(const char *hostname, const char *servname)
980 a596b957 2022-07-14 tracey struct addrinfo hints, *res0, *res;
982 cdfd248a 2023-11-15 op struct sockaddr_in *sin;
983 cdfd248a 2023-11-15 op struct sockaddr_in6 *sin6;
984 a596b957 2022-07-14 tracey struct address *h;
986 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
987 fb307946 2023-05-29 op hints.ai_family = AF_UNSPEC;
988 89cfaaa7 2023-11-15 op hints.ai_socktype = SOCK_STREAM;
989 c5e111b9 2023-11-15 op hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
990 89cfaaa7 2023-11-15 op error = getaddrinfo(hostname, servname, &hints, &res0);
991 a596b957 2022-07-14 tracey if (error) {
992 89cfaaa7 2023-11-15 op log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
993 89cfaaa7 2023-11-15 op hostname, servname, gai_strerror(error));
994 a596b957 2022-07-14 tracey return (-1);
997 c5e111b9 2023-11-15 op for (res = res0; res; res = res->ai_next) {
998 a596b957 2022-07-14 tracey if ((h = calloc(1, sizeof(*h))) == NULL)
999 a596b957 2022-07-14 tracey fatal(__func__);
1001 89cfaaa7 2023-11-15 op if (hostname == NULL) {
1002 c5e111b9 2023-11-15 op strlcpy(h->ifname, "*", sizeof(h->ifname));
1004 89cfaaa7 2023-11-15 op if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1005 a596b957 2022-07-14 tracey sizeof(h->ifname)) {
1006 b8b20b3c 2023-11-15 op log_warnx("%s: address truncated: %s",
1007 b8b20b3c 2023-11-15 op __func__, hostname);
1008 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1009 a596b957 2022-07-14 tracey free(h);
1010 a596b957 2022-07-14 tracey return (-1);
1014 0c64c2f8 2023-11-15 op h->ai_family = res->ai_family;
1015 0c64c2f8 2023-11-15 op h->ai_socktype = res->ai_socktype;
1016 0c64c2f8 2023-11-15 op h->ai_protocol = res->ai_protocol;
1017 cdfd248a 2023-11-15 op memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
1018 cdfd248a 2023-11-15 op h->slen = res->ai_addrlen;
1020 89cfaaa7 2023-11-15 op switch (res->ai_family) {
1021 89cfaaa7 2023-11-15 op case AF_INET:
1022 cdfd248a 2023-11-15 op sin = (struct sockaddr_in *)res->ai_addr;
1023 cdfd248a 2023-11-15 op h->port = ntohs(sin->sin_port);
1025 89cfaaa7 2023-11-15 op case AF_INET6:
1026 cdfd248a 2023-11-15 op sin6 = (struct sockaddr_in6 *)res->ai_addr;
1027 cdfd248a 2023-11-15 op h->port = ntohs(sin6->sin6_port);
1030 89cfaaa7 2023-11-15 op fatalx("unknown address family %d", res->ai_family);
1033 d68a7b14 2024-05-20 op if (add_addr(h) == -1) {
1034 eb916daf 2024-05-16 op freeaddrinfo(res0);
1035 67d8de2a 2022-08-29 stsp return -1;
1038 a596b957 2022-07-14 tracey freeaddrinfo(res0);
1039 a596b957 2022-07-14 tracey return (0);
1043 d68a7b14 2024-05-20 op get_unix_addr(const char *path)
1045 36079424 2024-05-16 op struct address *h;
1046 36079424 2024-05-16 op struct sockaddr_un *sun;
1048 36079424 2024-05-16 op if ((h = calloc(1, sizeof(*h))) == NULL)
1049 36079424 2024-05-16 op fatal("%s: calloc", __func__);
1051 36079424 2024-05-16 op h->ai_family = AF_UNIX;
1052 36079424 2024-05-16 op h->ai_socktype = SOCK_STREAM;
1053 36079424 2024-05-16 op h->ai_protocol = PF_UNSPEC;
1054 36079424 2024-05-16 op h->slen = sizeof(*sun);
1056 36079424 2024-05-16 op sun = (struct sockaddr_un *)&h->ss;
1057 36079424 2024-05-16 op sun->sun_family = AF_UNIX;
1058 36079424 2024-05-16 op if (strlcpy(sun->sun_path, path, sizeof(sun->sun_path)) >=
1059 36079424 2024-05-16 op sizeof(sun->sun_path)) {
1060 36079424 2024-05-16 op log_warnx("socket path too long: %s", sun->sun_path);
1061 36079424 2024-05-16 op return (-1);
1064 d68a7b14 2024-05-20 op return add_addr(h);
1068 36079424 2024-05-16 op addr_dup_check(struct addresslist *al, struct address *h)
1070 67d8de2a 2022-08-29 stsp struct address *a;
1072 67d8de2a 2022-08-29 stsp TAILQ_FOREACH(a, al, entry) {
1073 0c64c2f8 2023-11-15 op if (a->ai_family != h->ai_family ||
1074 0c64c2f8 2023-11-15 op a->ai_socktype != h->ai_socktype ||
1075 0c64c2f8 2023-11-15 op a->ai_protocol != h->ai_protocol ||
1076 0c64c2f8 2023-11-15 op a->slen != h->slen ||
1077 cdfd248a 2023-11-15 op memcmp(&a->ss, &h->ss, a->slen) != 0)
1079 67d8de2a 2022-08-29 stsp return -1;
1086 d68a7b14 2024-05-20 op add_addr(struct address *h)
1088 36079424 2024-05-16 op if (addr_dup_check(&gotwebd->addresses, h) == 0) {
1089 d68a7b14 2024-05-20 op TAILQ_INSERT_TAIL(&gotwebd->addresses, h, entry);