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 "got_compat.h"
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/queue.h>
29 #include <sys/socket.h>
30 #include <sys/stat.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <event.h>
41 #include <ifaddrs.h>
42 #include <limits.h>
43 #include <netdb.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <unistd.h>
51 #include "got_sockaddr.h"
52 #include "got_reference.h"
54 #include "proc.h"
55 #include "gotwebd.h"
57 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
58 static struct file {
59 TAILQ_ENTRY(file) entry;
60 FILE *stream;
61 char *name;
62 int lineno;
63 int errors;
64 } *file;
65 struct file *newfile(const char *, int);
66 static void closefile(struct file *);
67 int check_file_secrecy(int, const char *);
68 int yyparse(void);
69 int yylex(void);
70 int yyerror(const char *, ...)
71 __attribute__((__format__ (printf, 1, 2)))
72 __attribute__((__nonnull__ (1)));
73 int kw_cmp(const void *, const void *);
74 int lookup(char *);
75 int lgetc(int);
76 int lungetc(int);
77 int findeol(void);
79 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 struct sym {
81 TAILQ_ENTRY(sym) entry;
82 int used;
83 int persist;
84 char *nam;
85 char *val;
86 };
88 int symset(const char *, const char *, int);
89 char *symget(const char *);
91 static int errors;
93 static struct gotwebd *gotwebd;
94 static struct server *new_srv;
95 static struct server *conf_new_server(const char *);
96 int getservice(const char *);
97 int n;
99 int get_addrs(const char *, struct server *, in_port_t);
100 int addr_dup_check(struct addresslist *, struct address *,
101 const char *, const char *);
102 int add_addr(struct server *, struct address *);
103 int host(const char *, struct server *,
104 int, in_port_t, const char *, int);
105 int host_if(const char *, struct server *,
106 int, in_port_t, const char *, int);
107 int is_if_in_group(const char *, const char *);
109 typedef struct {
110 union {
111 long long number;
112 char *string;
113 in_port_t port;
114 } v;
115 int lineno;
116 } YYSTYPE;
118 %}
120 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
121 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
122 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
123 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
124 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
126 %token <v.string> STRING
127 %type <v.port> fcgiport
128 %token <v.number> NUMBER
129 %type <v.number> boolean
131 %%
133 grammar : /* empty */
134 | grammar '\n'
135 | grammar varset '\n'
136 | grammar main '\n'
137 | grammar server '\n'
138 | grammar error '\n' { file->errors++; }
141 varset : STRING '=' STRING {
142 char *s = $1;
143 while (*s++) {
144 if (isspace((unsigned char)*s)) {
145 yyerror("macro name cannot contain "
146 "whitespace");
147 free($1);
148 free($3);
149 YYERROR;
152 if (symset($1, $3, 0) == -1)
153 fatal("cannot store variable");
154 free($1);
155 free($3);
159 boolean : STRING {
160 if (strcasecmp($1, "1") == 0 ||
161 strcasecmp($1, "on") == 0)
162 $$ = 1;
163 else if (strcasecmp($1, "0") == 0 ||
164 strcasecmp($1, "off") == 0)
165 $$ = 0;
166 else {
167 yyerror("invalid boolean value '%s'", $1);
168 free($1);
169 YYERROR;
171 free($1);
173 | ON { $$ = 1; }
174 | NUMBER {
175 if ($1 != 0 && $1 != 1) {
176 yyerror("invalid boolean value '%lld'", $1);
177 YYERROR;
179 $$ = $1;
183 fcgiport : PORT NUMBER {
184 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
185 yyerror("invalid port: %lld", $2);
186 YYERROR;
188 $$ = $2;
190 | PORT STRING {
191 int val;
193 if ((val = getservice($2)) == -1) {
194 yyerror("invalid port: %s", $2);
195 free($2);
196 YYERROR;
198 free($2);
200 $$ = val;
204 main : PREFORK NUMBER {
205 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
206 yyerror("prefork is %s: %lld",
207 $2 <= 0 ? "too small" : "too large", $2);
208 YYERROR;
210 gotwebd->prefork_gotwebd = $2;
212 | CHROOT STRING {
213 if (*$2 == '\0') {
214 yyerror("chroot path can't be an empty"
215 " string");
216 free($2);
217 YYERROR;
220 n = strlcpy(gotwebd->httpd_chroot, $2,
221 sizeof(gotwebd->httpd_chroot));
222 if (n >= sizeof(gotwebd->httpd_chroot)) {
223 yyerror("%s: httpd_chroot truncated", __func__);
224 free($2);
225 YYERROR;
227 free($2);
229 | UNIX_SOCKET boolean {
230 gotwebd->unix_socket = $2;
232 | UNIX_SOCKET_NAME STRING {
233 n = snprintf(gotwebd->unix_socket_name,
234 sizeof(gotwebd->unix_socket_name), "%s%s",
235 gotwebd->httpd_chroot, $2);
236 if (n < 0 ||
237 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
238 yyerror("%s: unix_socket_name truncated",
239 __func__);
240 free($2);
241 YYERROR;
243 free($2);
247 server : SERVER STRING {
248 struct server *srv;
250 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
251 if (strcmp(srv->name, $2) == 0) {
252 yyerror("server name exists '%s'", $2);
253 free($2);
254 YYERROR;
258 new_srv = conf_new_server($2);
259 log_debug("adding server %s", $2);
260 free($2);
262 | SERVER STRING {
263 struct server *srv;
265 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
266 if (strcmp(srv->name, $2) == 0) {
267 yyerror("server name exists '%s'", $2);
268 free($2);
269 YYERROR;
273 new_srv = conf_new_server($2);
274 log_debug("adding server %s", $2);
275 free($2);
276 } '{' optnl serveropts2 '}' {
280 serveropts1 : REPOS_PATH STRING {
281 n = strlcpy(new_srv->repos_path, $2,
282 sizeof(new_srv->repos_path));
283 if (n >= sizeof(new_srv->repos_path)) {
284 yyerror("%s: repos_path truncated", __func__);
285 free($2);
286 YYERROR;
288 free($2);
290 | SITE_NAME STRING {
291 n = strlcpy(new_srv->site_name, $2,
292 sizeof(new_srv->site_name));
293 if (n >= sizeof(new_srv->site_name)) {
294 yyerror("%s: site_name truncated", __func__);
295 free($2);
296 YYERROR;
298 free($2);
300 | SITE_OWNER STRING {
301 n = strlcpy(new_srv->site_owner, $2,
302 sizeof(new_srv->site_owner));
303 if (n >= sizeof(new_srv->site_owner)) {
304 yyerror("%s: site_owner truncated", __func__);
305 free($2);
306 YYERROR;
308 free($2);
310 | SITE_LINK STRING {
311 n = strlcpy(new_srv->site_link, $2,
312 sizeof(new_srv->site_link));
313 if (n >= sizeof(new_srv->site_link)) {
314 yyerror("%s: site_link truncated", __func__);
315 free($2);
316 YYERROR;
318 free($2);
320 | LOGO STRING {
321 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
322 if (n >= sizeof(new_srv->logo)) {
323 yyerror("%s: logo truncated", __func__);
324 free($2);
325 YYERROR;
327 free($2);
329 | LOGO_URL STRING {
330 n = strlcpy(new_srv->logo_url, $2,
331 sizeof(new_srv->logo_url));
332 if (n >= sizeof(new_srv->logo_url)) {
333 yyerror("%s: logo_url truncated", __func__);
334 free($2);
335 YYERROR;
337 free($2);
339 | CUSTOM_CSS STRING {
340 n = strlcpy(new_srv->custom_css, $2,
341 sizeof(new_srv->custom_css));
342 if (n >= sizeof(new_srv->custom_css)) {
343 yyerror("%s: custom_css truncated", __func__);
344 free($2);
345 YYERROR;
347 free($2);
349 | LISTEN ON STRING fcgiport {
350 if (get_addrs($3, new_srv, $4) == -1) {
351 yyerror("could not get addrs");
352 YYERROR;
354 new_srv->fcgi_socket = 1;
356 | LISTEN ON SOCKET STRING {
357 if (strcasecmp($4, "off") == 0) {
358 new_srv->unix_socket = 0;
359 free($4);
360 YYACCEPT;
363 new_srv->unix_socket = 1;
365 n = snprintf(new_srv->unix_socket_name,
366 sizeof(new_srv->unix_socket_name), "%s%s",
367 gotwebd->httpd_chroot, $4);
368 if (n < 0 ||
369 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
370 yyerror("%s: unix_socket_name truncated",
371 __func__);
372 free($4);
373 YYERROR;
375 free($4);
377 | MAX_REPOS NUMBER {
378 if ($2 <= 0) {
379 yyerror("max_repos is too small: %lld", $2);
380 YYERROR;
382 new_srv->max_repos = $2;
384 | SHOW_SITE_OWNER boolean {
385 new_srv->show_site_owner = $2;
387 | SHOW_REPO_OWNER boolean {
388 new_srv->show_repo_owner = $2;
390 | SHOW_REPO_AGE boolean {
391 new_srv->show_repo_age = $2;
393 | SHOW_REPO_DESCRIPTION boolean {
394 new_srv->show_repo_description = $2;
396 | SHOW_REPO_CLONEURL boolean {
397 new_srv->show_repo_cloneurl = $2;
399 | RESPECT_EXPORTOK boolean {
400 new_srv->respect_exportok = $2;
402 | MAX_REPOS_DISPLAY NUMBER {
403 if ($2 < 0) {
404 yyerror("max_repos_display is too small: %lld",
405 $2);
406 YYERROR;
408 new_srv->max_repos_display = $2;
410 | MAX_COMMITS_DISPLAY NUMBER {
411 if ($2 <= 1) {
412 yyerror("max_commits_display is too small:"
413 " %lld", $2);
414 YYERROR;
416 new_srv->max_commits_display = $2;
420 serveropts2 : serveropts2 serveropts1 nl
421 | serveropts1 optnl
424 nl : '\n' optnl
427 optnl : '\n' optnl /* zero or more newlines */
428 | /* empty */
431 %%
433 struct keywords {
434 const char *k_name;
435 int k_val;
436 };
438 int
439 yyerror(const char *fmt, ...)
441 va_list ap;
442 char *msg;
444 file->errors++;
445 va_start(ap, fmt);
446 if (vasprintf(&msg, fmt, ap) == -1)
447 fatalx("yyerror vasprintf");
448 va_end(ap);
449 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
450 free(msg);
451 return (0);
454 int
455 kw_cmp(const void *k, const void *e)
457 return (strcmp(k, ((const struct keywords *)e)->k_name));
460 int
461 lookup(char *s)
463 /* This has to be sorted always. */
464 static const struct keywords keywords[] = {
465 { "chroot", CHROOT },
466 { "custom_css", CUSTOM_CSS },
467 { "listen", LISTEN },
468 { "logo", LOGO },
469 { "logo_url", LOGO_URL },
470 { "max_commits_display", MAX_COMMITS_DISPLAY },
471 { "max_repos", MAX_REPOS },
472 { "max_repos_display", MAX_REPOS_DISPLAY },
473 { "on", ON },
474 { "port", PORT },
475 { "prefork", PREFORK },
476 { "repos_path", REPOS_PATH },
477 { "respect_exportok", RESPECT_EXPORTOK },
478 { "server", SERVER },
479 { "show_repo_age", SHOW_REPO_AGE },
480 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
481 { "show_repo_description", SHOW_REPO_DESCRIPTION },
482 { "show_repo_owner", SHOW_REPO_OWNER },
483 { "show_site_owner", SHOW_SITE_OWNER },
484 { "site_link", SITE_LINK },
485 { "site_name", SITE_NAME },
486 { "site_owner", SITE_OWNER },
487 { "socket", SOCKET },
488 { "unix_socket", UNIX_SOCKET },
489 { "unix_socket_name", UNIX_SOCKET_NAME },
490 };
491 const struct keywords *p;
493 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
494 sizeof(keywords[0]), kw_cmp);
496 if (p)
497 return (p->k_val);
498 else
499 return (STRING);
502 #define MAXPUSHBACK 128
504 unsigned char *parsebuf;
505 int parseindex;
506 unsigned char pushback_buffer[MAXPUSHBACK];
507 int pushback_index = 0;
509 int
510 lgetc(int quotec)
512 int c, next;
514 if (parsebuf) {
515 /* Read character from the parsebuffer instead of input. */
516 if (parseindex >= 0) {
517 c = parsebuf[parseindex++];
518 if (c != '\0')
519 return (c);
520 parsebuf = NULL;
521 } else
522 parseindex++;
525 if (pushback_index)
526 return (pushback_buffer[--pushback_index]);
528 if (quotec) {
529 c = getc(file->stream);
530 if (c == EOF)
531 yyerror("reached end of file while parsing "
532 "quoted string");
533 return (c);
536 c = getc(file->stream);
537 while (c == '\\') {
538 next = getc(file->stream);
539 if (next != '\n') {
540 c = next;
541 break;
543 yylval.lineno = file->lineno;
544 file->lineno++;
545 c = getc(file->stream);
548 return (c);
551 int
552 lungetc(int c)
554 if (c == EOF)
555 return (EOF);
556 if (parsebuf) {
557 parseindex--;
558 if (parseindex >= 0)
559 return (c);
561 if (pushback_index < MAXPUSHBACK-1)
562 return (pushback_buffer[pushback_index++] = c);
563 else
564 return (EOF);
567 int
568 findeol(void)
570 int c;
572 parsebuf = NULL;
574 /* Skip to either EOF or the first real EOL. */
575 while (1) {
576 if (pushback_index)
577 c = pushback_buffer[--pushback_index];
578 else
579 c = lgetc(0);
580 if (c == '\n') {
581 file->lineno++;
582 break;
584 if (c == EOF)
585 break;
587 return (ERROR);
590 int
591 yylex(void)
593 unsigned char buf[8096];
594 unsigned char *p, *val;
595 int quotec, next, c;
596 int token;
598 top:
599 p = buf;
600 c = lgetc(0);
601 while (c == ' ' || c == '\t')
602 c = lgetc(0); /* nothing */
604 yylval.lineno = file->lineno;
605 if (c == '#') {
606 c = lgetc(0);
607 while (c != '\n' && c != EOF)
608 c = lgetc(0); /* nothing */
610 if (c == '$' && parsebuf == NULL) {
611 while (1) {
612 c = lgetc(0);
613 if (c == EOF)
614 return (0);
616 if (p + 1 >= buf + sizeof(buf) - 1) {
617 yyerror("string too long");
618 return (findeol());
620 if (isalnum(c) || c == '_') {
621 *p++ = c;
622 continue;
624 *p = '\0';
625 lungetc(c);
626 break;
628 val = symget(buf);
629 if (val == NULL) {
630 yyerror("macro '%s' not defined", buf);
631 return (findeol());
633 parsebuf = val;
634 parseindex = 0;
635 goto top;
638 switch (c) {
639 case '\'':
640 case '"':
641 quotec = c;
642 while (1) {
643 c = lgetc(quotec);
644 if (c == EOF)
645 return (0);
646 if (c == '\n') {
647 file->lineno++;
648 continue;
649 } else if (c == '\\') {
650 next = lgetc(quotec);
651 if (next == EOF)
652 return (0);
653 if (next == quotec || c == ' ' || c == '\t')
654 c = next;
655 else if (next == '\n') {
656 file->lineno++;
657 continue;
658 } else
659 lungetc(next);
660 } else if (c == quotec) {
661 *p = '\0';
662 break;
663 } else if (c == '\0') {
664 yyerror("syntax error");
665 return (findeol());
667 if (p + 1 >= buf + sizeof(buf) - 1) {
668 yyerror("string too long");
669 return (findeol());
671 *p++ = c;
673 yylval.v.string = strdup(buf);
674 if (yylval.v.string == NULL)
675 err(1, "yylex: strdup");
676 return (STRING);
679 #define allowed_to_end_number(x) \
680 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
682 if (c == '-' || isdigit(c)) {
683 do {
684 *p++ = c;
685 if ((unsigned)(p-buf) >= sizeof(buf)) {
686 yyerror("string too long");
687 return (findeol());
689 c = lgetc(0);
690 } while (c != EOF && isdigit(c));
691 lungetc(c);
692 if (p == buf + 1 && buf[0] == '-')
693 goto nodigits;
694 if (c == EOF || allowed_to_end_number(c)) {
695 const char *errstr = NULL;
697 *p = '\0';
698 yylval.v.number = strtonum(buf, LLONG_MIN,
699 LLONG_MAX, &errstr);
700 if (errstr) {
701 yyerror("\"%s\" invalid number: %s",
702 buf, errstr);
703 return (findeol());
705 return (NUMBER);
706 } else {
707 nodigits:
708 while (p > buf + 1)
709 lungetc(*--p);
710 c = *--p;
711 if (c == '-')
712 return (c);
716 #define allowed_in_string(x) \
717 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
718 x != '{' && x != '}' && \
719 x != '!' && x != '=' && x != '#' && \
720 x != ','))
722 if (isalnum(c) || c == ':' || c == '_') {
723 do {
724 *p++ = c;
725 if ((unsigned)(p-buf) >= sizeof(buf)) {
726 yyerror("string too long");
727 return (findeol());
729 c = lgetc(0);
730 } while (c != EOF && (allowed_in_string(c)));
731 lungetc(c);
732 *p = '\0';
733 token = lookup(buf);
734 if (token == STRING) {
735 yylval.v.string = strdup(buf);
736 if (yylval.v.string == NULL)
737 err(1, "yylex: strdup");
739 return (token);
741 if (c == '\n') {
742 yylval.lineno = file->lineno;
743 file->lineno++;
745 if (c == EOF)
746 return (0);
747 return (c);
750 int
751 check_file_secrecy(int fd, const char *fname)
753 struct stat st;
755 if (fstat(fd, &st)) {
756 log_warn("cannot stat %s", fname);
757 return (-1);
759 if (st.st_uid != 0 && st.st_uid != getuid()) {
760 log_warnx("%s: owner not root or current user", fname);
761 return (-1);
763 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
764 log_warnx("%s: group writable or world read/writable", fname);
765 return (-1);
767 return (0);
770 struct file *
771 newfile(const char *name, int secret)
773 struct file *nfile;
775 nfile = calloc(1, sizeof(struct file));
776 if (nfile == NULL) {
777 log_warn("calloc");
778 return (NULL);
780 nfile->name = strdup(name);
781 if (nfile->name == NULL) {
782 log_warn("strdup");
783 free(nfile);
784 return (NULL);
786 nfile->stream = fopen(nfile->name, "r");
787 if (nfile->stream == NULL) {
788 /* no warning, we don't require a conf file */
789 free(nfile->name);
790 free(nfile);
791 return (NULL);
792 } else if (secret &&
793 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
794 fclose(nfile->stream);
795 free(nfile->name);
796 free(nfile);
797 return (NULL);
799 nfile->lineno = 1;
800 return (nfile);
803 static void
804 closefile(struct file *xfile)
806 fclose(xfile->stream);
807 free(xfile->name);
808 free(xfile);
811 static void
812 add_default_server(void)
814 new_srv = conf_new_server(D_SITENAME);
815 log_debug("%s: adding default server %s", __func__, D_SITENAME);
818 int
819 parse_config(const char *filename, struct gotwebd *env)
821 struct sym *sym, *next;
823 if (config_init(env) == -1)
824 fatalx("failed to initialize configuration");
826 gotwebd = env;
828 file = newfile(filename, 0);
829 if (file == NULL) {
830 add_default_server();
831 sockets_parse_sockets(env);
832 /* just return, as we don't require a conf file */
833 return (0);
836 yyparse();
837 errors = file->errors;
838 closefile(file);
840 /* Free macros and check which have not been used. */
841 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
842 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
843 fprintf(stderr, "warning: macro '%s' not used\n",
844 sym->nam);
845 if (!sym->persist) {
846 free(sym->nam);
847 free(sym->val);
848 TAILQ_REMOVE(&symhead, sym, entry);
849 free(sym);
853 if (errors)
854 return (-1);
856 /* just add default server if no config specified */
857 if (gotwebd->server_cnt == 0)
858 add_default_server();
860 /* setup our listening sockets */
861 sockets_parse_sockets(env);
863 return (0);
866 struct server *
867 conf_new_server(const char *name)
869 struct server *srv = NULL;
871 srv = calloc(1, sizeof(*srv));
872 if (srv == NULL)
873 fatalx("%s: calloc", __func__);
875 n = strlcpy(srv->name, name, sizeof(srv->name));
876 if (n >= sizeof(srv->name))
877 fatalx("%s: strlcpy", __func__);
878 n = snprintf(srv->unix_socket_name,
879 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
880 D_UNIX_SOCKET);
881 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
882 fatalx("%s: snprintf", __func__);
883 n = strlcpy(srv->repos_path, D_GOTPATH,
884 sizeof(srv->repos_path));
885 if (n >= sizeof(srv->repos_path))
886 fatalx("%s: strlcpy", __func__);
887 n = strlcpy(srv->site_name, D_SITENAME,
888 sizeof(srv->site_name));
889 if (n >= sizeof(srv->site_name))
890 fatalx("%s: strlcpy", __func__);
891 n = strlcpy(srv->site_owner, D_SITEOWNER,
892 sizeof(srv->site_owner));
893 if (n >= sizeof(srv->site_owner))
894 fatalx("%s: strlcpy", __func__);
895 n = strlcpy(srv->site_link, D_SITELINK,
896 sizeof(srv->site_link));
897 if (n >= sizeof(srv->site_link))
898 fatalx("%s: strlcpy", __func__);
899 n = strlcpy(srv->logo, D_GOTLOGO,
900 sizeof(srv->logo));
901 if (n >= sizeof(srv->logo))
902 fatalx("%s: strlcpy", __func__);
903 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
904 if (n >= sizeof(srv->logo_url))
905 fatalx("%s: strlcpy", __func__);
906 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
907 if (n >= sizeof(srv->custom_css))
908 fatalx("%s: strlcpy", __func__);
910 srv->show_site_owner = D_SHOWSOWNER;
911 srv->show_repo_owner = D_SHOWROWNER;
912 srv->show_repo_age = D_SHOWAGE;
913 srv->show_repo_description = D_SHOWDESC;
914 srv->show_repo_cloneurl = D_SHOWURL;
915 srv->respect_exportok = D_RESPECTEXPORTOK;
917 srv->max_repos_display = D_MAXREPODISP;
918 srv->max_commits_display = D_MAXCOMMITDISP;
919 srv->max_repos = D_MAXREPO;
921 srv->unix_socket = 1;
922 srv->fcgi_socket = 0;
924 TAILQ_INIT(&srv->al);
925 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
926 gotwebd->server_cnt++;
928 return srv;
929 };
931 int
932 symset(const char *nam, const char *val, int persist)
934 struct sym *sym;
936 TAILQ_FOREACH(sym, &symhead, entry) {
937 if (strcmp(nam, sym->nam) == 0)
938 break;
941 if (sym != NULL) {
942 if (sym->persist == 1)
943 return (0);
944 else {
945 free(sym->nam);
946 free(sym->val);
947 TAILQ_REMOVE(&symhead, sym, entry);
948 free(sym);
951 sym = calloc(1, sizeof(*sym));
952 if (sym == NULL)
953 return (-1);
955 sym->nam = strdup(nam);
956 if (sym->nam == NULL) {
957 free(sym);
958 return (-1);
960 sym->val = strdup(val);
961 if (sym->val == NULL) {
962 free(sym->nam);
963 free(sym);
964 return (-1);
966 sym->used = 0;
967 sym->persist = persist;
968 TAILQ_INSERT_TAIL(&symhead, sym, entry);
969 return (0);
972 int
973 cmdline_symset(char *s)
975 char *sym, *val;
976 int ret;
978 val = strrchr(s, '=');
979 if (val == NULL)
980 return (-1);
982 sym = strndup(s, val - s);
983 if (sym == NULL)
984 fatal("%s: strndup", __func__);
986 ret = symset(sym, val + 1, 1);
987 free(sym);
989 return (ret);
992 char *
993 symget(const char *nam)
995 struct sym *sym;
997 TAILQ_FOREACH(sym, &symhead, entry) {
998 if (strcmp(nam, sym->nam) == 0) {
999 sym->used = 1;
1000 return (sym->val);
1003 return (NULL);
1006 int
1007 getservice(const char *n)
1009 struct servent *s;
1010 const char *errstr;
1011 long long llval;
1013 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1014 if (errstr) {
1015 s = getservbyname(n, "tcp");
1016 if (s == NULL)
1017 s = getservbyname(n, "udp");
1018 if (s == NULL)
1019 return (-1);
1020 return ntohs(s->s_port);
1023 return (unsigned short)llval;
1026 int
1027 host(const char *s, struct server *new_srv, int max,
1028 in_port_t port, const char *ifname, int ipproto)
1030 struct addrinfo hints, *res0, *res;
1031 int error, cnt = 0;
1032 struct sockaddr_in *sain;
1033 struct sockaddr_in6 *sin6;
1034 struct address *h;
1036 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1037 return (cnt);
1039 memset(&hints, 0, sizeof(hints));
1040 hints.ai_family = AF_UNSPEC;
1041 hints.ai_socktype = SOCK_STREAM; /* DUMMY */
1042 hints.ai_flags = AI_ADDRCONFIG;
1043 error = getaddrinfo(s, NULL, &hints, &res0);
1044 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1045 return (0);
1046 if (error) {
1047 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1048 gai_strerror(error));
1049 return (-1);
1052 for (res = res0; res && cnt < max; res = res->ai_next) {
1053 if (res->ai_family != AF_INET &&
1054 res->ai_family != AF_INET6)
1055 continue;
1056 if ((h = calloc(1, sizeof(*h))) == NULL)
1057 fatal(__func__);
1059 if (port)
1060 h->port = port;
1061 if (ifname != NULL) {
1062 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1063 sizeof(h->ifname)) {
1064 log_warnx("%s: interface name truncated",
1065 __func__);
1066 freeaddrinfo(res0);
1067 free(h);
1068 return (-1);
1071 if (ipproto != -1)
1072 h->ipproto = ipproto;
1073 h->ss.ss_family = res->ai_family;
1075 if (res->ai_family == AF_INET) {
1076 struct sockaddr_in *ra;
1077 sain = (struct sockaddr_in *)&h->ss;
1078 ra = (struct sockaddr_in *)res->ai_addr;
1079 got_sockaddr_inet_init(sain, &ra->sin_addr);
1080 } else {
1081 struct sockaddr_in6 *ra;
1082 sin6 = (struct sockaddr_in6 *)&h->ss;
1083 ra = (struct sockaddr_in6 *)res->ai_addr;
1084 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1087 if (add_addr(new_srv, h))
1088 return -1;
1089 cnt++;
1091 if (cnt == max && res) {
1092 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1093 s, max);
1095 freeaddrinfo(res0);
1096 return (cnt);
1099 int
1100 host_if(const char *s, struct server *new_srv, int max,
1101 in_port_t port, const char *ifname, int ipproto)
1103 struct ifaddrs *ifap, *p;
1104 struct sockaddr_in *sain;
1105 struct sockaddr_in6 *sin6;
1106 struct address *h;
1107 int cnt = 0, af;
1109 if (getifaddrs(&ifap) == -1)
1110 fatal("getifaddrs");
1112 /* First search for IPv4 addresses */
1113 af = AF_INET;
1115 nextaf:
1116 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1117 if (p->ifa_addr == NULL ||
1118 p->ifa_addr->sa_family != af ||
1119 (strcmp(s, p->ifa_name) != 0 &&
1120 !is_if_in_group(p->ifa_name, s)))
1121 continue;
1122 if ((h = calloc(1, sizeof(*h))) == NULL)
1123 fatal("calloc");
1125 if (port)
1126 h->port = port;
1127 if (ifname != NULL) {
1128 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1129 sizeof(h->ifname)) {
1130 log_warnx("%s: interface name truncated",
1131 __func__);
1132 free(h);
1133 freeifaddrs(ifap);
1134 return (-1);
1137 if (ipproto != -1)
1138 h->ipproto = ipproto;
1139 h->ss.ss_family = af;
1141 if (af == AF_INET) {
1142 struct sockaddr_in *ra;
1143 sain = (struct sockaddr_in *)&h->ss;
1144 ra = (struct sockaddr_in *)p->ifa_addr;
1145 got_sockaddr_inet_init(sain, &ra->sin_addr);
1146 } else {
1147 struct sockaddr_in6 *ra;
1148 sin6 = (struct sockaddr_in6 *)&h->ss;
1149 ra = (struct sockaddr_in6 *)p->ifa_addr;
1150 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1151 ra->sin6_scope_id);
1154 if (add_addr(new_srv, h))
1155 return -1;
1156 cnt++;
1158 if (af == AF_INET) {
1159 /* Next search for IPv6 addresses */
1160 af = AF_INET6;
1161 goto nextaf;
1164 if (cnt > max) {
1165 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1166 s, max);
1168 freeifaddrs(ifap);
1169 return (cnt);
1172 int
1173 is_if_in_group(const char *ifname, const char *groupname)
1175 /* TA: Check this... */
1176 #ifdef HAVE_STRUCT_IFGROUPREQ
1177 unsigned int len;
1178 struct ifgroupreq ifgr;
1179 struct ifg_req *ifg;
1180 int s;
1181 int ret = 0;
1183 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1184 err(1, "socket");
1186 memset(&ifgr, 0, sizeof(ifgr));
1187 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1188 err(1, "IFNAMSIZ");
1189 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1190 if (errno == EINVAL || errno == ENOTTY)
1191 goto end;
1192 err(1, "SIOCGIFGROUP");
1195 len = ifgr.ifgr_len;
1196 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1197 sizeof(struct ifg_req));
1198 if (ifgr.ifgr_groups == NULL)
1199 err(1, "getifgroups");
1200 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1201 err(1, "SIOCGIFGROUP");
1203 ifg = ifgr.ifgr_groups;
1204 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1205 len -= sizeof(struct ifg_req);
1206 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1207 ret = 1;
1208 break;
1211 free(ifgr.ifgr_groups);
1213 end:
1214 close(s);
1215 return (ret);
1216 #else
1217 return (0);
1218 #endif
1221 int
1222 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1224 if (strcmp("", addr) == 0) {
1225 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1226 -1) <= 0) {
1227 yyerror("invalid listen ip: %s",
1228 "127.0.0.1");
1229 return (-1);
1231 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1232 yyerror("invalid listen ip: %s", "::1");
1233 return (-1);
1235 } else {
1236 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1237 -1) <= 0) {
1238 yyerror("invalid listen ip: %s", addr);
1239 return (-1);
1242 return (0);
1245 int
1246 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1247 const char *other_srv)
1249 struct address *a;
1250 void *ia;
1251 char buf[INET6_ADDRSTRLEN];
1252 const char *addrstr;
1254 TAILQ_FOREACH(a, al, entry) {
1255 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1256 a->port != h->port)
1257 continue;
1259 switch (h->ss.ss_family) {
1260 case AF_INET:
1261 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1262 break;
1263 case AF_INET6:
1264 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1265 break;
1266 default:
1267 yyerror("unknown address family: %d", h->ss.ss_family);
1268 return -1;
1270 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1271 if (addrstr) {
1272 if (other_srv) {
1273 yyerror("server %s: duplicate fcgi listen "
1274 "address %s:%d, already used by server %s",
1275 new_srv, addrstr, h->port, other_srv);
1276 } else {
1277 log_warnx("server: %s: duplicate fcgi listen "
1278 "address %s:%d", new_srv, addrstr, h->port);
1280 } else {
1281 if (other_srv) {
1282 yyerror("server: %s: duplicate fcgi listen "
1283 "address, already used by server %s",
1284 new_srv, other_srv);
1285 } else {
1286 log_warnx("server %s: duplicate fcgi listen "
1287 "address", new_srv);
1291 return -1;
1294 return 0;
1297 int
1298 add_addr(struct server *new_srv, struct address *h)
1300 struct server *srv;
1302 /* Address cannot be shared between different servers. */
1303 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1304 if (srv == new_srv)
1305 continue;
1306 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1307 return -1;
1310 /* Tolerate duplicate address lines within the scope of a server. */
1311 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1312 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1313 else
1314 free(h);
1316 return 0;