Blob


1 /*
2 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 *
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
23 %{
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/queue.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
33 #include <arpa/inet.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <event.h>
39 #include <ifaddrs.h>
40 #include <imsg.h>
41 #include <limits.h>
42 #include <netdb.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <unistd.h>
50 #include "got_reference.h"
52 #include "gotwebd.h"
54 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 static struct file {
56 TAILQ_ENTRY(file) entry;
57 FILE *stream;
58 char *name;
59 int lineno;
60 int errors;
61 } *file;
62 struct file *newfile(const char *, int);
63 static void closefile(struct file *);
64 int check_file_secrecy(int, const char *);
65 int yyparse(void);
66 int yylex(void);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
71 int lookup(char *);
72 int lgetc(int);
73 int lungetc(int);
74 int findeol(void);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 struct sym {
78 TAILQ_ENTRY(sym) entry;
79 int used;
80 int persist;
81 char *nam;
82 char *val;
83 };
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
88 static int errors;
90 static struct gotwebd *gotwebd;
91 static struct server *new_srv;
92 static struct server *conf_new_server(const char *);
93 int getservice(const char *);
94 int n;
96 int get_addrs(const char *, const char *, struct server *);
97 int addr_dup_check(struct addresslist *, struct address *,
98 const char *, const char *);
99 int add_addr(struct server *, struct address *);
101 typedef struct {
102 union {
103 long long number;
104 char *string;
105 } v;
106 int lineno;
107 } YYSTYPE;
109 %}
111 %token LISTEN WWW_PATH SITE_NAME SITE_OWNER SITE_LINK LOGO
112 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
113 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
114 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
115 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
116 %token SUMMARY_COMMITS_DISPLAY SUMMARY_TAGS_DISPLAY
118 %token <v.string> STRING
119 %token <v.number> NUMBER
120 %type <v.number> boolean
121 %type <v.string> listen_addr
123 %%
125 grammar : /* empty */
126 | grammar '\n'
127 | grammar varset '\n'
128 | grammar main '\n'
129 | grammar server '\n'
130 | grammar error '\n' { file->errors++; }
133 varset : STRING '=' STRING {
134 char *s = $1;
135 while (*s++) {
136 if (isspace((unsigned char)*s)) {
137 yyerror("macro name cannot contain "
138 "whitespace");
139 free($1);
140 free($3);
141 YYERROR;
144 if (symset($1, $3, 0) == -1)
145 fatal("cannot store variable");
146 free($1);
147 free($3);
151 boolean : STRING {
152 if (strcasecmp($1, "1") == 0 ||
153 strcasecmp($1, "on") == 0)
154 $$ = 1;
155 else if (strcasecmp($1, "0") == 0 ||
156 strcasecmp($1, "off") == 0)
157 $$ = 0;
158 else {
159 yyerror("invalid boolean value '%s'", $1);
160 free($1);
161 YYERROR;
163 free($1);
165 | ON { $$ = 1; }
166 | NUMBER {
167 if ($1 != 0 && $1 != 1) {
168 yyerror("invalid boolean value '%lld'", $1);
169 YYERROR;
171 $$ = $1;
175 listen_addr : '*' { $$ = NULL; }
176 | STRING
179 main : PREFORK NUMBER {
180 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
181 yyerror("prefork is %s: %lld",
182 $2 <= 0 ? "too small" : "too large", $2);
183 YYERROR;
185 gotwebd->prefork_gotwebd = $2;
187 | CHROOT STRING {
188 if (*$2 == '\0') {
189 yyerror("chroot path can't be an empty"
190 " string");
191 free($2);
192 YYERROR;
195 n = strlcpy(gotwebd->httpd_chroot, $2,
196 sizeof(gotwebd->httpd_chroot));
197 if (n >= sizeof(gotwebd->httpd_chroot)) {
198 yyerror("%s: httpd_chroot truncated", __func__);
199 free($2);
200 YYERROR;
202 free($2);
204 | UNIX_SOCKET boolean {
205 gotwebd->unix_socket = $2;
207 | UNIX_SOCKET_NAME STRING {
208 n = snprintf(gotwebd->unix_socket_name,
209 sizeof(gotwebd->unix_socket_name), "%s%s",
210 gotwebd->httpd_chroot, $2);
211 if (n < 0 ||
212 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
213 yyerror("%s: unix_socket_name truncated",
214 __func__);
215 free($2);
216 YYERROR;
218 free($2);
222 server : SERVER STRING {
223 struct server *srv;
225 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
226 if (strcmp(srv->name, $2) == 0) {
227 yyerror("server name exists '%s'", $2);
228 free($2);
229 YYERROR;
233 new_srv = conf_new_server($2);
234 log_debug("adding server %s", $2);
235 free($2);
237 | SERVER STRING {
238 struct server *srv;
240 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
241 if (strcmp(srv->name, $2) == 0) {
242 yyerror("server name exists '%s'", $2);
243 free($2);
244 YYERROR;
248 new_srv = conf_new_server($2);
249 log_debug("adding server %s", $2);
250 free($2);
251 } '{' optnl serveropts2 '}' {
255 serveropts1 : REPOS_PATH STRING {
256 n = strlcpy(new_srv->repos_path, $2,
257 sizeof(new_srv->repos_path));
258 if (n >= sizeof(new_srv->repos_path)) {
259 yyerror("%s: repos_path truncated", __func__);
260 free($2);
261 YYERROR;
263 free($2);
265 | SITE_NAME STRING {
266 n = strlcpy(new_srv->site_name, $2,
267 sizeof(new_srv->site_name));
268 if (n >= sizeof(new_srv->site_name)) {
269 yyerror("%s: site_name truncated", __func__);
270 free($2);
271 YYERROR;
273 free($2);
275 | SITE_OWNER STRING {
276 n = strlcpy(new_srv->site_owner, $2,
277 sizeof(new_srv->site_owner));
278 if (n >= sizeof(new_srv->site_owner)) {
279 yyerror("%s: site_owner truncated", __func__);
280 free($2);
281 YYERROR;
283 free($2);
285 | SITE_LINK STRING {
286 n = strlcpy(new_srv->site_link, $2,
287 sizeof(new_srv->site_link));
288 if (n >= sizeof(new_srv->site_link)) {
289 yyerror("%s: site_link truncated", __func__);
290 free($2);
291 YYERROR;
293 free($2);
295 | LOGO STRING {
296 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
297 if (n >= sizeof(new_srv->logo)) {
298 yyerror("%s: logo truncated", __func__);
299 free($2);
300 YYERROR;
302 free($2);
304 | LOGO_URL STRING {
305 n = strlcpy(new_srv->logo_url, $2,
306 sizeof(new_srv->logo_url));
307 if (n >= sizeof(new_srv->logo_url)) {
308 yyerror("%s: logo_url truncated", __func__);
309 free($2);
310 YYERROR;
312 free($2);
314 | CUSTOM_CSS STRING {
315 n = strlcpy(new_srv->custom_css, $2,
316 sizeof(new_srv->custom_css));
317 if (n >= sizeof(new_srv->custom_css)) {
318 yyerror("%s: custom_css truncated", __func__);
319 free($2);
320 YYERROR;
322 free($2);
324 | LISTEN ON listen_addr PORT STRING {
325 if (get_addrs($3, $5, new_srv) == -1) {
326 yyerror("could not get addrs");
327 YYERROR;
329 free($3);
330 free($5);
331 new_srv->fcgi_socket = 1;
333 | LISTEN ON listen_addr PORT NUMBER {
334 char portno[32];
335 int n;
337 n = snprintf(portno, sizeof(portno), "%lld",
338 (long long)$5);
339 if (n < 0 || (size_t)n >= sizeof(portno))
340 fatalx("port number too long: %lld",
341 (long long)$5);
343 if (get_addrs($3, portno, new_srv) == -1) {
344 yyerror("could not get addrs");
345 YYERROR;
347 free($3);
348 new_srv->fcgi_socket = 1;
350 | LISTEN ON SOCKET STRING {
351 if (strcasecmp($4, "off") == 0) {
352 new_srv->unix_socket = 0;
353 free($4);
354 YYACCEPT;
357 new_srv->unix_socket = 1;
359 n = snprintf(new_srv->unix_socket_name,
360 sizeof(new_srv->unix_socket_name), "%s%s",
361 gotwebd->httpd_chroot, $4);
362 if (n < 0 ||
363 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
364 yyerror("%s: unix_socket_name truncated",
365 __func__);
366 free($4);
367 YYERROR;
369 free($4);
371 | SHOW_SITE_OWNER boolean {
372 new_srv->show_site_owner = $2;
374 | SHOW_REPO_OWNER boolean {
375 new_srv->show_repo_owner = $2;
377 | SHOW_REPO_AGE boolean {
378 new_srv->show_repo_age = $2;
380 | SHOW_REPO_DESCRIPTION boolean {
381 new_srv->show_repo_description = $2;
383 | SHOW_REPO_CLONEURL boolean {
384 new_srv->show_repo_cloneurl = $2;
386 | RESPECT_EXPORTOK boolean {
387 new_srv->respect_exportok = $2;
389 | MAX_REPOS_DISPLAY NUMBER {
390 if ($2 < 0) {
391 yyerror("max_repos_display is too small: %lld",
392 $2);
393 YYERROR;
395 new_srv->max_repos_display = $2;
397 | MAX_COMMITS_DISPLAY NUMBER {
398 if ($2 <= 1) {
399 yyerror("max_commits_display is too small:"
400 " %lld", $2);
401 YYERROR;
403 new_srv->max_commits_display = $2;
405 | SUMMARY_COMMITS_DISPLAY NUMBER {
406 if ($2 < 1) {
407 yyerror("summary_commits_display is too small:"
408 " %lld", $2);
409 YYERROR;
411 new_srv->summary_commits_display = $2;
413 | SUMMARY_TAGS_DISPLAY NUMBER {
414 if ($2 < 1) {
415 yyerror("summary_tags_display is too small:"
416 " %lld", $2);
417 YYERROR;
419 new_srv->summary_tags_display = $2;
423 serveropts2 : serveropts2 serveropts1 nl
424 | serveropts1 optnl
427 nl : '\n' optnl
430 optnl : '\n' optnl /* zero or more newlines */
431 | /* empty */
434 %%
436 struct keywords {
437 const char *k_name;
438 int k_val;
439 };
441 int
442 yyerror(const char *fmt, ...)
444 va_list ap;
445 char *msg;
447 file->errors++;
448 va_start(ap, fmt);
449 if (vasprintf(&msg, fmt, ap) == -1)
450 fatalx("yyerror vasprintf");
451 va_end(ap);
452 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
453 free(msg);
454 return (0);
457 int
458 kw_cmp(const void *k, const void *e)
460 return (strcmp(k, ((const struct keywords *)e)->k_name));
463 int
464 lookup(char *s)
466 /* This has to be sorted always. */
467 static const struct keywords keywords[] = {
468 { "chroot", CHROOT },
469 { "custom_css", CUSTOM_CSS },
470 { "listen", LISTEN },
471 { "logo", LOGO },
472 { "logo_url", LOGO_URL },
473 { "max_commits_display", MAX_COMMITS_DISPLAY },
474 { "max_repos_display", MAX_REPOS_DISPLAY },
475 { "on", ON },
476 { "port", PORT },
477 { "prefork", PREFORK },
478 { "repos_path", REPOS_PATH },
479 { "respect_exportok", RESPECT_EXPORTOK },
480 { "server", SERVER },
481 { "show_repo_age", SHOW_REPO_AGE },
482 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
483 { "show_repo_description", SHOW_REPO_DESCRIPTION },
484 { "show_repo_owner", SHOW_REPO_OWNER },
485 { "show_site_owner", SHOW_SITE_OWNER },
486 { "site_link", SITE_LINK },
487 { "site_name", SITE_NAME },
488 { "site_owner", SITE_OWNER },
489 { "socket", SOCKET },
490 { "summary_commits_display", SUMMARY_COMMITS_DISPLAY },
491 { "summary_tags_display", SUMMARY_TAGS_DISPLAY },
492 { "unix_socket", UNIX_SOCKET },
493 { "unix_socket_name", UNIX_SOCKET_NAME },
494 };
495 const struct keywords *p;
497 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
498 sizeof(keywords[0]), kw_cmp);
500 if (p)
501 return (p->k_val);
502 else
503 return (STRING);
506 #define MAXPUSHBACK 128
508 unsigned char *parsebuf;
509 int parseindex;
510 unsigned char pushback_buffer[MAXPUSHBACK];
511 int pushback_index = 0;
513 int
514 lgetc(int quotec)
516 int c, next;
518 if (parsebuf) {
519 /* Read character from the parsebuffer instead of input. */
520 if (parseindex >= 0) {
521 c = parsebuf[parseindex++];
522 if (c != '\0')
523 return (c);
524 parsebuf = NULL;
525 } else
526 parseindex++;
529 if (pushback_index)
530 return (pushback_buffer[--pushback_index]);
532 if (quotec) {
533 c = getc(file->stream);
534 if (c == EOF)
535 yyerror("reached end of file while parsing "
536 "quoted string");
537 return (c);
540 c = getc(file->stream);
541 while (c == '\\') {
542 next = getc(file->stream);
543 if (next != '\n') {
544 c = next;
545 break;
547 yylval.lineno = file->lineno;
548 file->lineno++;
549 c = getc(file->stream);
552 return (c);
555 int
556 lungetc(int c)
558 if (c == EOF)
559 return (EOF);
560 if (parsebuf) {
561 parseindex--;
562 if (parseindex >= 0)
563 return (c);
565 if (pushback_index < MAXPUSHBACK-1)
566 return (pushback_buffer[pushback_index++] = c);
567 else
568 return (EOF);
571 int
572 findeol(void)
574 int c;
576 parsebuf = NULL;
578 /* Skip to either EOF or the first real EOL. */
579 while (1) {
580 if (pushback_index)
581 c = pushback_buffer[--pushback_index];
582 else
583 c = lgetc(0);
584 if (c == '\n') {
585 file->lineno++;
586 break;
588 if (c == EOF)
589 break;
591 return (ERROR);
594 int
595 yylex(void)
597 unsigned char buf[8096];
598 unsigned char *p, *val;
599 int quotec, next, c;
600 int token;
602 top:
603 p = buf;
604 c = lgetc(0);
605 while (c == ' ' || c == '\t')
606 c = lgetc(0); /* nothing */
608 yylval.lineno = file->lineno;
609 if (c == '#') {
610 c = lgetc(0);
611 while (c != '\n' && c != EOF)
612 c = lgetc(0); /* nothing */
614 if (c == '$' && parsebuf == NULL) {
615 while (1) {
616 c = lgetc(0);
617 if (c == EOF)
618 return (0);
620 if (p + 1 >= buf + sizeof(buf) - 1) {
621 yyerror("string too long");
622 return (findeol());
624 if (isalnum(c) || c == '_') {
625 *p++ = c;
626 continue;
628 *p = '\0';
629 lungetc(c);
630 break;
632 val = symget(buf);
633 if (val == NULL) {
634 yyerror("macro '%s' not defined", buf);
635 return (findeol());
637 parsebuf = val;
638 parseindex = 0;
639 goto top;
642 switch (c) {
643 case '\'':
644 case '"':
645 quotec = c;
646 while (1) {
647 c = lgetc(quotec);
648 if (c == EOF)
649 return (0);
650 if (c == '\n') {
651 file->lineno++;
652 continue;
653 } else if (c == '\\') {
654 next = lgetc(quotec);
655 if (next == EOF)
656 return (0);
657 if (next == quotec || c == ' ' || c == '\t')
658 c = next;
659 else if (next == '\n') {
660 file->lineno++;
661 continue;
662 } else
663 lungetc(next);
664 } else if (c == quotec) {
665 *p = '\0';
666 break;
667 } else if (c == '\0') {
668 yyerror("syntax error");
669 return (findeol());
671 if (p + 1 >= buf + sizeof(buf) - 1) {
672 yyerror("string too long");
673 return (findeol());
675 *p++ = c;
677 yylval.v.string = strdup(buf);
678 if (yylval.v.string == NULL)
679 err(1, "yylex: strdup");
680 return (STRING);
683 #define allowed_to_end_number(x) \
684 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
686 if (c == '-' || isdigit(c)) {
687 do {
688 *p++ = c;
689 if ((unsigned)(p-buf) >= sizeof(buf)) {
690 yyerror("string too long");
691 return (findeol());
693 c = lgetc(0);
694 } while (c != EOF && isdigit(c));
695 lungetc(c);
696 if (p == buf + 1 && buf[0] == '-')
697 goto nodigits;
698 if (c == EOF || allowed_to_end_number(c)) {
699 const char *errstr = NULL;
701 *p = '\0';
702 yylval.v.number = strtonum(buf, LLONG_MIN,
703 LLONG_MAX, &errstr);
704 if (errstr) {
705 yyerror("\"%s\" invalid number: %s",
706 buf, errstr);
707 return (findeol());
709 return (NUMBER);
710 } else {
711 nodigits:
712 while (p > buf + 1)
713 lungetc(*--p);
714 c = *--p;
715 if (c == '-')
716 return (c);
720 #define allowed_in_string(x) \
721 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
722 x != '{' && x != '}' && \
723 x != '!' && x != '=' && x != '#' && \
724 x != ','))
726 if (isalnum(c) || c == ':' || c == '_') {
727 do {
728 *p++ = c;
729 if ((unsigned)(p-buf) >= sizeof(buf)) {
730 yyerror("string too long");
731 return (findeol());
733 c = lgetc(0);
734 } while (c != EOF && (allowed_in_string(c)));
735 lungetc(c);
736 *p = '\0';
737 token = lookup(buf);
738 if (token == STRING) {
739 yylval.v.string = strdup(buf);
740 if (yylval.v.string == NULL)
741 err(1, "yylex: strdup");
743 return (token);
745 if (c == '\n') {
746 yylval.lineno = file->lineno;
747 file->lineno++;
749 if (c == EOF)
750 return (0);
751 return (c);
754 int
755 check_file_secrecy(int fd, const char *fname)
757 struct stat st;
759 if (fstat(fd, &st)) {
760 log_warn("cannot stat %s", fname);
761 return (-1);
763 if (st.st_uid != 0 && st.st_uid != getuid()) {
764 log_warnx("%s: owner not root or current user", fname);
765 return (-1);
767 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
768 log_warnx("%s: group writable or world read/writable", fname);
769 return (-1);
771 return (0);
774 struct file *
775 newfile(const char *name, int secret)
777 struct file *nfile;
779 nfile = calloc(1, sizeof(struct file));
780 if (nfile == NULL) {
781 log_warn("calloc");
782 return (NULL);
784 nfile->name = strdup(name);
785 if (nfile->name == NULL) {
786 log_warn("strdup");
787 free(nfile);
788 return (NULL);
790 nfile->stream = fopen(nfile->name, "r");
791 if (nfile->stream == NULL) {
792 /* no warning, we don't require a conf file */
793 free(nfile->name);
794 free(nfile);
795 return (NULL);
796 } else if (secret &&
797 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
798 fclose(nfile->stream);
799 free(nfile->name);
800 free(nfile);
801 return (NULL);
803 nfile->lineno = 1;
804 return (nfile);
807 static void
808 closefile(struct file *xfile)
810 fclose(xfile->stream);
811 free(xfile->name);
812 free(xfile);
815 static void
816 add_default_server(void)
818 new_srv = conf_new_server(D_SITENAME);
819 log_debug("%s: adding default server %s", __func__, D_SITENAME);
822 int
823 parse_config(const char *filename, struct gotwebd *env)
825 struct sym *sym, *next;
827 if (config_init(env) == -1)
828 fatalx("failed to initialize configuration");
830 gotwebd = env;
832 file = newfile(filename, 0);
833 if (file == NULL) {
834 add_default_server();
835 sockets_parse_sockets(env);
836 /* just return, as we don't require a conf file */
837 return (0);
840 yyparse();
841 errors = file->errors;
842 closefile(file);
844 /* Free macros and check which have not been used. */
845 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
846 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
847 fprintf(stderr, "warning: macro '%s' not used\n",
848 sym->nam);
849 if (!sym->persist) {
850 free(sym->nam);
851 free(sym->val);
852 TAILQ_REMOVE(&symhead, sym, entry);
853 free(sym);
857 if (errors)
858 return (-1);
860 /* just add default server if no config specified */
861 if (gotwebd->server_cnt == 0)
862 add_default_server();
864 /* setup our listening sockets */
865 sockets_parse_sockets(env);
867 return (0);
870 struct server *
871 conf_new_server(const char *name)
873 struct server *srv = NULL;
875 srv = calloc(1, sizeof(*srv));
876 if (srv == NULL)
877 fatalx("%s: calloc", __func__);
879 n = strlcpy(srv->name, name, sizeof(srv->name));
880 if (n >= sizeof(srv->name))
881 fatalx("%s: strlcpy", __func__);
882 n = snprintf(srv->unix_socket_name,
883 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
884 D_UNIX_SOCKET);
885 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
886 fatalx("%s: snprintf", __func__);
887 n = strlcpy(srv->repos_path, D_GOTPATH,
888 sizeof(srv->repos_path));
889 if (n >= sizeof(srv->repos_path))
890 fatalx("%s: strlcpy", __func__);
891 n = strlcpy(srv->site_name, D_SITENAME,
892 sizeof(srv->site_name));
893 if (n >= sizeof(srv->site_name))
894 fatalx("%s: strlcpy", __func__);
895 n = strlcpy(srv->site_owner, D_SITEOWNER,
896 sizeof(srv->site_owner));
897 if (n >= sizeof(srv->site_owner))
898 fatalx("%s: strlcpy", __func__);
899 n = strlcpy(srv->site_link, D_SITELINK,
900 sizeof(srv->site_link));
901 if (n >= sizeof(srv->site_link))
902 fatalx("%s: strlcpy", __func__);
903 n = strlcpy(srv->logo, D_GOTLOGO,
904 sizeof(srv->logo));
905 if (n >= sizeof(srv->logo))
906 fatalx("%s: strlcpy", __func__);
907 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
908 if (n >= sizeof(srv->logo_url))
909 fatalx("%s: strlcpy", __func__);
910 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
911 if (n >= sizeof(srv->custom_css))
912 fatalx("%s: strlcpy", __func__);
914 srv->show_site_owner = D_SHOWSOWNER;
915 srv->show_repo_owner = D_SHOWROWNER;
916 srv->show_repo_age = D_SHOWAGE;
917 srv->show_repo_description = D_SHOWDESC;
918 srv->show_repo_cloneurl = D_SHOWURL;
919 srv->respect_exportok = D_RESPECTEXPORTOK;
921 srv->max_repos_display = D_MAXREPODISP;
922 srv->max_commits_display = D_MAXCOMMITDISP;
923 srv->summary_commits_display = D_MAXSLCOMMDISP;
924 srv->summary_tags_display = D_MAXSLTAGDISP;
926 srv->unix_socket = 1;
927 srv->fcgi_socket = 0;
929 TAILQ_INIT(&srv->al);
930 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
931 gotwebd->server_cnt++;
933 return srv;
934 };
936 int
937 symset(const char *nam, const char *val, int persist)
939 struct sym *sym;
941 TAILQ_FOREACH(sym, &symhead, entry) {
942 if (strcmp(nam, sym->nam) == 0)
943 break;
946 if (sym != NULL) {
947 if (sym->persist == 1)
948 return (0);
949 else {
950 free(sym->nam);
951 free(sym->val);
952 TAILQ_REMOVE(&symhead, sym, entry);
953 free(sym);
956 sym = calloc(1, sizeof(*sym));
957 if (sym == NULL)
958 return (-1);
960 sym->nam = strdup(nam);
961 if (sym->nam == NULL) {
962 free(sym);
963 return (-1);
965 sym->val = strdup(val);
966 if (sym->val == NULL) {
967 free(sym->nam);
968 free(sym);
969 return (-1);
971 sym->used = 0;
972 sym->persist = persist;
973 TAILQ_INSERT_TAIL(&symhead, sym, entry);
974 return (0);
977 int
978 cmdline_symset(char *s)
980 char *sym, *val;
981 int ret;
983 val = strrchr(s, '=');
984 if (val == NULL)
985 return (-1);
987 sym = strndup(s, val - s);
988 if (sym == NULL)
989 fatal("%s: strndup", __func__);
991 ret = symset(sym, val + 1, 1);
992 free(sym);
994 return (ret);
997 char *
998 symget(const char *nam)
1000 struct sym *sym;
1002 TAILQ_FOREACH(sym, &symhead, entry) {
1003 if (strcmp(nam, sym->nam) == 0) {
1004 sym->used = 1;
1005 return (sym->val);
1008 return (NULL);
1011 int
1012 get_addrs(const char *hostname, const char *servname, struct server *new_srv)
1014 struct addrinfo hints, *res0, *res;
1015 int error;
1016 struct sockaddr_in *sin;
1017 struct sockaddr_in6 *sin6;
1018 struct address *h;
1020 memset(&hints, 0, sizeof(hints));
1021 hints.ai_family = AF_UNSPEC;
1022 hints.ai_socktype = SOCK_STREAM;
1023 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
1024 error = getaddrinfo(hostname, servname, &hints, &res0);
1025 if (error) {
1026 log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
1027 hostname, servname, gai_strerror(error));
1028 return (-1);
1031 for (res = res0; res; res = res->ai_next) {
1032 if ((h = calloc(1, sizeof(*h))) == NULL)
1033 fatal(__func__);
1035 if (hostname == NULL) {
1036 strlcpy(h->ifname, "*", sizeof(h->ifname));
1037 } else {
1038 if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1039 sizeof(h->ifname)) {
1040 log_warnx("%s: address truncated: %s",
1041 __func__, hostname);
1042 freeaddrinfo(res0);
1043 free(h);
1044 return (-1);
1048 h->ai_family = res->ai_family;
1049 h->ai_socktype = res->ai_socktype;
1050 h->ai_protocol = res->ai_protocol;
1051 memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
1052 h->slen = res->ai_addrlen;
1054 switch (res->ai_family) {
1055 case AF_INET:
1056 sin = (struct sockaddr_in *)res->ai_addr;
1057 h->port = ntohs(sin->sin_port);
1058 break;
1059 case AF_INET6:
1060 sin6 = (struct sockaddr_in6 *)res->ai_addr;
1061 h->port = ntohs(sin6->sin6_port);
1062 break;
1063 default:
1064 fatalx("unknown address family %d", res->ai_family);
1067 if (add_addr(new_srv, h))
1068 return -1;
1070 freeaddrinfo(res0);
1071 return (0);
1074 int
1075 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1076 const char *other_srv)
1078 struct address *a;
1079 void *ia;
1080 char buf[INET6_ADDRSTRLEN];
1081 const char *addrstr;
1083 TAILQ_FOREACH(a, al, entry) {
1084 if (a->ai_family != h->ai_family ||
1085 a->ai_socktype != h->ai_socktype ||
1086 a->ai_protocol != h->ai_protocol ||
1087 a->slen != h->slen ||
1088 memcmp(&a->ss, &h->ss, a->slen) != 0)
1089 continue;
1091 switch (h->ss.ss_family) {
1092 case AF_INET:
1093 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1094 break;
1095 case AF_INET6:
1096 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1097 break;
1098 default:
1099 yyerror("unknown address family: %d", h->ss.ss_family);
1100 return -1;
1102 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1103 if (addrstr) {
1104 if (other_srv) {
1105 yyerror("server %s: duplicate fcgi listen "
1106 "address %s:%d, already used by server %s",
1107 new_srv, addrstr, h->port, other_srv);
1108 } else {
1109 log_warnx("server: %s: duplicate fcgi listen "
1110 "address %s:%d", new_srv, addrstr, h->port);
1112 } else {
1113 if (other_srv) {
1114 yyerror("server: %s: duplicate fcgi listen "
1115 "address, already used by server %s",
1116 new_srv, other_srv);
1117 } else {
1118 log_warnx("server %s: duplicate fcgi listen "
1119 "address", new_srv);
1123 return -1;
1126 return 0;
1129 int
1130 add_addr(struct server *new_srv, struct address *h)
1132 struct server *srv;
1134 /* Address cannot be shared between different servers. */
1135 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1136 if (srv == new_srv)
1137 continue;
1138 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1139 return -1;
1142 /* Tolerate duplicate address lines within the scope of a server. */
1143 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1144 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1145 else
1146 free(h);
1148 return 0;