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 "proc.h"
51 #include "gotwebd.h"
52 #include "got_sockaddr.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 *, struct server *, in_port_t);
97 int addr_dup_check(struct addresslist *, struct address *,
98 const char *, const char *);
99 int add_addr(struct server *, struct address *);
100 struct address *host_v4(const char *);
101 struct address *host_v6(const char *);
102 int host_dns(const char *, struct server *,
103 int, in_port_t, const char *, int);
104 int host_if(const char *, struct server *,
105 int, in_port_t, const char *, int);
106 int host(const char *, struct server *,
107 int, in_port_t, const char *, int);
108 int is_if_in_group(const char *, const char *);
110 typedef struct {
111 union {
112 long long number;
113 char *string;
114 in_port_t port;
115 } v;
116 int lineno;
117 } YYSTYPE;
119 %}
121 %token LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
122 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
123 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
124 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK FCGI_SOCKET
125 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
127 %token <v.string> STRING
128 %type <v.port> fcgiport
129 %token <v.number> NUMBER
130 %type <v.number> boolean
132 %%
134 grammar :
135 | grammar '\n'
136 | grammar main '\n'
137 | grammar server '\n'
140 boolean : STRING {
141 if (strcasecmp($1, "1") == 0 ||
142 strcasecmp($1, "yes") == 0 ||
143 strcasecmp($1, "on") == 0)
144 $$ = 1;
145 else if (strcasecmp($1, "0") == 0 ||
146 strcasecmp($1, "off") == 0 ||
147 strcasecmp($1, "no") == 0)
148 $$ = 0;
149 else {
150 yyerror("invalid boolean value '%s'", $1);
151 free($1);
152 YYERROR;
154 free($1);
156 | ON { $$ = 1; }
157 | NUMBER { $$ = $1; }
160 fcgiport : PORT NUMBER {
161 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
162 yyerror("invalid port: %lld", $2);
163 YYERROR;
165 $$ = $2;
167 | PORT STRING {
168 int val;
170 if ((val = getservice($2)) == -1) {
171 yyerror("invalid port: %s", $2);
172 free($2);
173 YYERROR;
175 free($2);
177 $$ = val;
181 main : PREFORK NUMBER {
182 gotwebd->prefork_gotwebd = $2;
184 | CHROOT STRING {
185 n = strlcpy(gotwebd->httpd_chroot, $2,
186 sizeof(gotwebd->httpd_chroot));
187 if (n >= sizeof(gotwebd->httpd_chroot)) {
188 yyerror("%s: httpd_chroot truncated", __func__);
189 free($2);
190 YYERROR;
192 free($2);
194 | FCGI_SOCKET boolean {
195 gotwebd->fcgi_socket = $2;
197 | UNIX_SOCKET boolean {
198 gotwebd->unix_socket = $2;
200 | UNIX_SOCKET_NAME STRING {
201 n = snprintf(gotwebd->unix_socket_name,
202 sizeof(gotwebd->unix_socket_name), "%s%s",
203 strlen(gotwebd->httpd_chroot) ?
204 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
205 if (n < 0 ||
206 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
207 yyerror("%s: unix_socket_name truncated",
208 __func__);
209 free($2);
210 YYERROR;
212 free($2);
216 server : SERVER STRING {
217 struct server *srv;
219 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
220 if (strcmp(srv->name, $2) == 0) {
221 yyerror("server name exists '%s'", $2);
222 free($2);
223 YYERROR;
227 new_srv = conf_new_server($2);
228 log_debug("adding server %s", $2);
229 free($2);
231 | SERVER STRING {
232 struct server *srv;
234 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
235 if (strcmp(srv->name, $2) == 0) {
236 yyerror("server name exists '%s'", $2);
237 free($2);
238 YYERROR;
242 new_srv = conf_new_server($2);
243 log_debug("adding server %s", $2);
244 free($2);
245 } '{' optnl serveropts2 '}' {
249 serveropts1 : REPOS_PATH STRING {
250 n = strlcpy(new_srv->repos_path, $2,
251 sizeof(new_srv->repos_path));
252 if (n >= sizeof(new_srv->repos_path)) {
253 yyerror("%s: repos_path truncated", __func__);
254 free($2);
255 YYERROR;
257 free($2);
259 | SITE_NAME STRING {
260 n = strlcpy(new_srv->site_name, $2,
261 sizeof(new_srv->site_name));
262 if (n >= sizeof(new_srv->site_name)) {
263 yyerror("%s: site_name truncated", __func__);
264 free($2);
265 YYERROR;
267 free($2);
269 | SITE_OWNER STRING {
270 n = strlcpy(new_srv->site_owner, $2,
271 sizeof(new_srv->site_owner));
272 if (n >= sizeof(new_srv->site_owner)) {
273 yyerror("%s: site_owner truncated", __func__);
274 free($2);
275 YYERROR;
277 free($2);
279 | SITE_LINK STRING {
280 n = strlcpy(new_srv->site_link, $2,
281 sizeof(new_srv->site_link));
282 if (n >= sizeof(new_srv->site_link)) {
283 yyerror("%s: site_link truncated", __func__);
284 free($2);
285 YYERROR;
287 free($2);
289 | LOGO STRING {
290 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
291 if (n >= sizeof(new_srv->logo)) {
292 yyerror("%s: logo truncated", __func__);
293 free($2);
294 YYERROR;
296 free($2);
298 | LOGO_URL STRING {
299 n = strlcpy(new_srv->logo_url, $2,
300 sizeof(new_srv->logo_url));
301 if (n >= sizeof(new_srv->logo_url)) {
302 yyerror("%s: logo_url truncated", __func__);
303 free($2);
304 YYERROR;
306 free($2);
308 | CUSTOM_CSS STRING {
309 n = strlcpy(new_srv->custom_css, $2,
310 sizeof(new_srv->custom_css));
311 if (n >= sizeof(new_srv->custom_css)) {
312 yyerror("%s: custom_css truncated", __func__);
313 free($2);
314 YYERROR;
316 free($2);
318 | LISTEN ON STRING fcgiport {
319 if (get_addrs($3, new_srv, $4) == -1) {
320 yyerror("could not get addrs");
321 YYERROR;
324 | MAX_REPOS NUMBER {
325 if ($2 > 0)
326 new_srv->max_repos = $2;
328 | SHOW_SITE_OWNER boolean {
329 new_srv->show_site_owner = $2;
331 | SHOW_REPO_OWNER boolean {
332 new_srv->show_repo_owner = $2;
334 | SHOW_REPO_AGE boolean {
335 new_srv->show_repo_age = $2;
337 | SHOW_REPO_DESCRIPTION boolean {
338 new_srv->show_repo_description = $2;
340 | SHOW_REPO_CLONEURL boolean {
341 new_srv->show_repo_cloneurl = $2;
343 | MAX_REPOS_DISPLAY NUMBER {
344 new_srv->max_repos_display = $2;
346 | MAX_COMMITS_DISPLAY NUMBER {
347 if ($2 > 0)
348 new_srv->max_commits_display = $2;
350 | FCGI_SOCKET boolean {
351 new_srv->fcgi_socket = $2;
353 | UNIX_SOCKET boolean {
354 new_srv->unix_socket = $2;
356 | UNIX_SOCKET_NAME STRING {
357 n = snprintf(new_srv->unix_socket_name,
358 sizeof(new_srv->unix_socket_name), "%s%s",
359 strlen(gotwebd->httpd_chroot) ?
360 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
361 if (n < 0 ||
362 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
363 yyerror("%s: unix_socket_name truncated",
364 __func__);
365 free($2);
366 YYERROR;
368 free($2);
372 serveropts2 : serveropts2 serveropts1 nl
373 | serveropts1 optnl
376 nl : '\n' optnl
379 optnl : '\n' optnl /* zero or more newlines */
380 | /* empty */
383 %%
385 struct keywords {
386 const char *k_name;
387 int k_val;
388 };
390 int
391 yyerror(const char *fmt, ...)
393 va_list ap;
394 char *msg;
396 file->errors++;
397 va_start(ap, fmt);
398 if (vasprintf(&msg, fmt, ap) == -1)
399 fatalx("yyerror vasprintf");
400 va_end(ap);
401 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
402 free(msg);
403 return (0);
406 int
407 kw_cmp(const void *k, const void *e)
409 return (strcmp(k, ((const struct keywords *)e)->k_name));
412 int
413 lookup(char *s)
415 /* This has to be sorted always. */
416 static const struct keywords keywords[] = {
417 { "chroot", CHROOT },
418 { "custom_css", CUSTOM_CSS },
419 { "fcgi_socket", FCGI_SOCKET },
420 { "listen", LISTEN },
421 { "logo", LOGO },
422 { "logo_url" , LOGO_URL },
423 { "max_commits_display", MAX_COMMITS_DISPLAY },
424 { "max_repos", MAX_REPOS },
425 { "max_repos_display", MAX_REPOS_DISPLAY },
426 { "on", ON },
427 { "port", PORT },
428 { "prefork", PREFORK },
429 { "repos_path", REPOS_PATH },
430 { "server", SERVER },
431 { "show_repo_age", SHOW_REPO_AGE },
432 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
433 { "show_repo_description", SHOW_REPO_DESCRIPTION },
434 { "show_repo_owner", SHOW_REPO_OWNER },
435 { "show_site_owner", SHOW_SITE_OWNER },
436 { "site_link", SITE_LINK },
437 { "site_name", SITE_NAME },
438 { "site_owner", SITE_OWNER },
439 { "unix_socket", UNIX_SOCKET },
440 { "unix_socket_name", UNIX_SOCKET_NAME },
441 };
442 const struct keywords *p;
444 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
445 sizeof(keywords[0]), kw_cmp);
447 if (p)
448 return (p->k_val);
449 else
450 return (STRING);
453 #define MAXPUSHBACK 128
455 unsigned char *parsebuf;
456 int parseindex;
457 unsigned char pushback_buffer[MAXPUSHBACK];
458 int pushback_index = 0;
460 int
461 lgetc(int quotec)
463 int c, next;
465 if (parsebuf) {
466 /* Read character from the parsebuffer instead of input. */
467 if (parseindex >= 0) {
468 c = parsebuf[parseindex++];
469 if (c != '\0')
470 return (c);
471 parsebuf = NULL;
472 } else
473 parseindex++;
476 if (pushback_index)
477 return (pushback_buffer[--pushback_index]);
479 if (quotec) {
480 c = getc(file->stream);
481 if (c == EOF)
482 yyerror("reached end of file while parsing "
483 "quoted string");
484 return (c);
487 c = getc(file->stream);
488 while (c == '\\') {
489 next = getc(file->stream);
490 if (next != '\n') {
491 c = next;
492 break;
494 yylval.lineno = file->lineno;
495 file->lineno++;
496 c = getc(file->stream);
499 return (c);
502 int
503 lungetc(int c)
505 if (c == EOF)
506 return (EOF);
507 if (parsebuf) {
508 parseindex--;
509 if (parseindex >= 0)
510 return (c);
512 if (pushback_index < MAXPUSHBACK-1)
513 return (pushback_buffer[pushback_index++] = c);
514 else
515 return (EOF);
518 int
519 findeol(void)
521 int c;
523 parsebuf = NULL;
525 /* Skip to either EOF or the first real EOL. */
526 while (1) {
527 if (pushback_index)
528 c = pushback_buffer[--pushback_index];
529 else
530 c = lgetc(0);
531 if (c == '\n') {
532 file->lineno++;
533 break;
535 if (c == EOF)
536 break;
538 return (ERROR);
541 int
542 yylex(void)
544 unsigned char buf[8096];
545 unsigned char *p, *val;
546 int quotec, next, c;
547 int token;
549 top:
550 p = buf;
551 c = lgetc(0);
552 while (c == ' ' || c == '\t')
553 c = lgetc(0); /* nothing */
555 yylval.lineno = file->lineno;
556 if (c == '#') {
557 c = lgetc(0);
558 while (c != '\n' && c != EOF)
559 c = lgetc(0); /* nothing */
561 if (c == '$' && parsebuf == NULL) {
562 while (1) {
563 c = lgetc(0);
564 if (c == EOF)
565 return (0);
567 if (p + 1 >= buf + sizeof(buf) - 1) {
568 yyerror("string too long");
569 return (findeol());
571 if (isalnum(c) || c == '_') {
572 *p++ = c;
573 continue;
575 *p = '\0';
576 lungetc(c);
577 break;
579 val = symget(buf);
580 if (val == NULL) {
581 yyerror("macro '%s' not defined", buf);
582 return (findeol());
584 parsebuf = val;
585 parseindex = 0;
586 goto top;
589 switch (c) {
590 case '\'':
591 case '"':
592 quotec = c;
593 while (1) {
594 c = lgetc(quotec);
595 if (c == EOF)
596 return (0);
597 if (c == '\n') {
598 file->lineno++;
599 continue;
600 } else if (c == '\\') {
601 next = lgetc(quotec);
602 if (next == EOF)
603 return (0);
604 if (next == quotec || c == ' ' || c == '\t')
605 c = next;
606 else if (next == '\n') {
607 file->lineno++;
608 continue;
609 } else
610 lungetc(next);
611 } else if (c == quotec) {
612 *p = '\0';
613 break;
614 } else if (c == '\0') {
615 yyerror("syntax error");
616 return (findeol());
618 if (p + 1 >= buf + sizeof(buf) - 1) {
619 yyerror("string too long");
620 return (findeol());
622 *p++ = c;
624 yylval.v.string = strdup(buf);
625 if (yylval.v.string == NULL)
626 err(1, "yylex: strdup");
627 return (STRING);
630 #define allowed_to_end_number(x) \
631 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
633 if (c == '-' || isdigit(c)) {
634 do {
635 *p++ = c;
636 if ((unsigned)(p-buf) >= sizeof(buf)) {
637 yyerror("string too long");
638 return (findeol());
640 c = lgetc(0);
641 } while (c != EOF && isdigit(c));
642 lungetc(c);
643 if (p == buf + 1 && buf[0] == '-')
644 goto nodigits;
645 if (c == EOF || allowed_to_end_number(c)) {
646 const char *errstr = NULL;
648 *p = '\0';
649 yylval.v.number = strtonum(buf, LLONG_MIN,
650 LLONG_MAX, &errstr);
651 if (errstr) {
652 yyerror("\"%s\" invalid number: %s",
653 buf, errstr);
654 return (findeol());
656 return (NUMBER);
657 } else {
658 nodigits:
659 while (p > buf + 1)
660 lungetc(*--p);
661 c = *--p;
662 if (c == '-')
663 return (c);
667 #define allowed_in_string(x) \
668 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
669 x != '{' && x != '}' && \
670 x != '!' && x != '=' && x != '#' && \
671 x != ','))
673 if (isalnum(c) || c == ':' || c == '_') {
674 do {
675 *p++ = c;
676 if ((unsigned)(p-buf) >= sizeof(buf)) {
677 yyerror("string too long");
678 return (findeol());
680 c = lgetc(0);
681 } while (c != EOF && (allowed_in_string(c)));
682 lungetc(c);
683 *p = '\0';
684 token = lookup(buf);
685 if (token == STRING) {
686 yylval.v.string = strdup(buf);
687 if (yylval.v.string == NULL)
688 err(1, "yylex: strdup");
690 return (token);
692 if (c == '\n') {
693 yylval.lineno = file->lineno;
694 file->lineno++;
696 if (c == EOF)
697 return (0);
698 return (c);
701 int
702 check_file_secrecy(int fd, const char *fname)
704 struct stat st;
706 if (fstat(fd, &st)) {
707 log_warn("cannot stat %s", fname);
708 return (-1);
710 if (st.st_uid != 0 && st.st_uid != getuid()) {
711 log_warnx("%s: owner not root or current user", fname);
712 return (-1);
714 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
715 log_warnx("%s: group writable or world read/writable", fname);
716 return (-1);
718 return (0);
721 struct file *
722 newfile(const char *name, int secret)
724 struct file *nfile;
726 nfile = calloc(1, sizeof(struct file));
727 if (nfile == NULL) {
728 log_warn("calloc");
729 return (NULL);
731 nfile->name = strdup(name);
732 if (nfile->name == NULL) {
733 log_warn("strdup");
734 free(nfile);
735 return (NULL);
737 nfile->stream = fopen(nfile->name, "r");
738 if (nfile->stream == NULL) {
739 /* no warning, we don't require a conf file */
740 free(nfile->name);
741 free(nfile);
742 return (NULL);
743 } else if (secret &&
744 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
745 fclose(nfile->stream);
746 free(nfile->name);
747 free(nfile);
748 return (NULL);
750 nfile->lineno = 1;
751 return (nfile);
754 static void
755 closefile(struct file *xfile)
757 fclose(xfile->stream);
758 free(xfile->name);
759 free(xfile);
762 static void
763 add_default_server(void)
765 new_srv = conf_new_server(D_SITENAME);
766 log_debug("%s: adding default server %s", __func__, D_SITENAME);
769 int
770 parse_config(const char *filename, struct gotwebd *env)
772 struct sym *sym, *next;
774 if (config_init(env) == -1)
775 fatalx("failed to initialize configuration");
777 gotwebd = env;
779 file = newfile(filename, 0);
780 if (file == NULL) {
781 add_default_server();
782 sockets_parse_sockets(env);
783 /* just return, as we don't require a conf file */
784 return (0);
787 yyparse();
788 errors = file->errors;
789 closefile(file);
791 /* Free macros and check which have not been used. */
792 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
793 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
794 fprintf(stderr, "warning: macro '%s' not used\n",
795 sym->nam);
796 if (!sym->persist) {
797 free(sym->nam);
798 free(sym->val);
799 TAILQ_REMOVE(&symhead, sym, entry);
800 free(sym);
804 if (errors)
805 return (-1);
807 /* just add default server if no config specified */
808 if (gotwebd->server_cnt == 0)
809 add_default_server();
811 /* setup our listening sockets */
812 sockets_parse_sockets(env);
814 return (0);
817 struct server *
818 conf_new_server(const char *name)
820 struct server *srv = NULL;
822 srv = calloc(1, sizeof(*srv));
823 if (srv == NULL)
824 fatalx("%s: calloc", __func__);
826 n = strlcpy(srv->name, name, sizeof(srv->name));
827 if (n >= sizeof(srv->name))
828 fatalx("%s: strlcpy", __func__);
829 n = snprintf(srv->unix_socket_name,
830 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
831 D_UNIX_SOCKET);
832 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
833 fatalx("%s: snprintf", __func__);
834 n = strlcpy(srv->repos_path, D_GOTPATH,
835 sizeof(srv->repos_path));
836 if (n >= sizeof(srv->repos_path))
837 fatalx("%s: strlcpy", __func__);
838 n = strlcpy(srv->site_name, D_SITENAME,
839 sizeof(srv->site_name));
840 if (n >= sizeof(srv->site_name))
841 fatalx("%s: strlcpy", __func__);
842 n = strlcpy(srv->site_owner, D_SITEOWNER,
843 sizeof(srv->site_owner));
844 if (n >= sizeof(srv->site_owner))
845 fatalx("%s: strlcpy", __func__);
846 n = strlcpy(srv->site_link, D_SITELINK,
847 sizeof(srv->site_link));
848 if (n >= sizeof(srv->site_link))
849 fatalx("%s: strlcpy", __func__);
850 n = strlcpy(srv->logo, D_GOTLOGO,
851 sizeof(srv->logo));
852 if (n >= sizeof(srv->logo))
853 fatalx("%s: strlcpy", __func__);
854 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
855 if (n >= sizeof(srv->logo_url))
856 fatalx("%s: strlcpy", __func__);
857 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
858 if (n >= sizeof(srv->custom_css))
859 fatalx("%s: strlcpy", __func__);
861 srv->show_site_owner = D_SHOWSOWNER;
862 srv->show_repo_owner = D_SHOWROWNER;
863 srv->show_repo_age = D_SHOWAGE;
864 srv->show_repo_description = D_SHOWDESC;
865 srv->show_repo_cloneurl = D_SHOWURL;
867 srv->max_repos_display = D_MAXREPODISP;
868 srv->max_commits_display = D_MAXCOMMITDISP;
869 srv->max_repos = D_MAXREPO;
871 srv->unix_socket = 1;
872 srv->fcgi_socket = gotwebd->fcgi_socket ? gotwebd->fcgi_socket : 0;
874 TAILQ_INIT(&srv->al);
875 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
876 gotwebd->server_cnt++;
878 return srv;
879 };
881 int
882 symset(const char *nam, const char *val, int persist)
884 struct sym *sym;
886 TAILQ_FOREACH(sym, &symhead, entry) {
887 if (strcmp(nam, sym->nam) == 0)
888 break;
891 if (sym != NULL) {
892 if (sym->persist == 1)
893 return (0);
894 else {
895 free(sym->nam);
896 free(sym->val);
897 TAILQ_REMOVE(&symhead, sym, entry);
898 free(sym);
901 sym = calloc(1, sizeof(*sym));
902 if (sym == NULL)
903 return (-1);
905 sym->nam = strdup(nam);
906 if (sym->nam == NULL) {
907 free(sym);
908 return (-1);
910 sym->val = strdup(val);
911 if (sym->val == NULL) {
912 free(sym->nam);
913 free(sym);
914 return (-1);
916 sym->used = 0;
917 sym->persist = persist;
918 TAILQ_INSERT_TAIL(&symhead, sym, entry);
919 return (0);
922 int
923 cmdline_symset(char *s)
925 char *sym, *val;
926 int ret;
927 size_t len;
929 val = strrchr(s, '=');
930 if (val == NULL)
931 return (-1);
933 len = strlen(s) - strlen(val) + 1;
934 sym = malloc(len);
935 if (sym == NULL)
936 fatal("%s: malloc", __func__);
938 memcpy(&sym, s, len);
940 ret = symset(sym, val + 1, 1);
941 free(sym);
943 return (ret);
946 char *
947 symget(const char *nam)
949 struct sym *sym;
951 TAILQ_FOREACH(sym, &symhead, entry) {
952 if (strcmp(nam, sym->nam) == 0) {
953 sym->used = 1;
954 return (sym->val);
957 return (NULL);
960 int
961 getservice(const char *n)
963 struct servent *s;
964 const char *errstr;
965 long long llval;
967 llval = strtonum(n, 0, UINT16_MAX, &errstr);
968 if (errstr) {
969 s = getservbyname(n, "tcp");
970 if (s == NULL)
971 s = getservbyname(n, "udp");
972 if (s == NULL)
973 return (-1);
974 return ntohs(s->s_port);
977 return (unsigned short)llval;
980 struct address *
981 host_v4(const char *s)
983 struct in_addr ina;
984 struct sockaddr_in *sain;
985 struct address *h;
987 memset(&ina, 0, sizeof(ina));
988 if (inet_pton(AF_INET, s, &ina) != 1)
989 return (NULL);
991 if ((h = calloc(1, sizeof(*h))) == NULL)
992 fatal(__func__);
993 sain = (struct sockaddr_in *)&h->ss;
994 got_sockaddr_inet_init(sain, &ina);
995 if (sain->sin_addr.s_addr == INADDR_ANY)
996 h->prefixlen = 0; /* 0.0.0.0 address */
997 else
998 h->prefixlen = -1; /* host address */
999 return (h);
1002 struct address *
1003 host_v6(const char *s)
1005 struct addrinfo hints, *res;
1006 struct sockaddr_in6 *sa_in6, *ra;
1007 struct address *h = NULL;
1009 memset(&hints, 0, sizeof(hints));
1010 hints.ai_family = AF_INET6;
1011 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1012 hints.ai_flags = AI_NUMERICHOST;
1013 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1014 if ((h = calloc(1, sizeof(*h))) == NULL)
1015 fatal(__func__);
1016 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1017 ra = (struct sockaddr_in6 *)res->ai_addr;
1018 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1019 ra->sin6_scope_id);
1020 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1021 sizeof(sa_in6->sin6_addr)) == 0)
1022 h->prefixlen = 0; /* any address */
1023 else
1024 h->prefixlen = -1; /* host address */
1025 freeaddrinfo(res);
1028 return (h);
1031 int
1032 host_dns(const char *s, struct server *new_srv, int max,
1033 in_port_t port, const char *ifname, int ipproto)
1035 struct addrinfo hints, *res0, *res;
1036 int error, cnt = 0;
1037 struct sockaddr_in *sain;
1038 struct sockaddr_in6 *sin6;
1039 struct address *h;
1041 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1042 return (cnt);
1044 memset(&hints, 0, sizeof(hints));
1045 hints.ai_family = PF_UNSPEC;
1046 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1047 hints.ai_flags = AI_ADDRCONFIG;
1048 error = getaddrinfo(s, NULL, &hints, &res0);
1049 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1050 return (0);
1051 if (error) {
1052 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1053 gai_strerror(error));
1054 return (-1);
1057 for (res = res0; res && cnt < max; res = res->ai_next) {
1058 if (res->ai_family != AF_INET &&
1059 res->ai_family != AF_INET6)
1060 continue;
1061 if ((h = calloc(1, sizeof(*h))) == NULL)
1062 fatal(__func__);
1064 if (port)
1065 h->port = port;
1066 if (ifname != NULL) {
1067 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1068 sizeof(h->ifname)) {
1069 log_warnx("%s: interface name truncated",
1070 __func__);
1071 freeaddrinfo(res0);
1072 free(h);
1073 return (-1);
1076 if (ipproto != -1)
1077 h->ipproto = ipproto;
1078 h->ss.ss_family = res->ai_family;
1079 h->prefixlen = -1; /* host address */
1081 if (res->ai_family == AF_INET) {
1082 struct sockaddr_in *ra;
1083 sain = (struct sockaddr_in *)&h->ss;
1084 ra = (struct sockaddr_in *)res->ai_addr;
1085 got_sockaddr_inet_init(sain, &ra->sin_addr);
1086 } else {
1087 struct sockaddr_in6 *ra;
1088 sin6 = (struct sockaddr_in6 *)&h->ss;
1089 ra = (struct sockaddr_in6 *)res->ai_addr;
1090 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1093 if (add_addr(new_srv, h))
1094 return -1;
1095 cnt++;
1097 if (cnt == max && res) {
1098 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1099 s, max);
1101 freeaddrinfo(res0);
1102 return (cnt);
1105 int
1106 host_if(const char *s, struct server *new_srv, int max,
1107 in_port_t port, const char *ifname, int ipproto)
1109 struct ifaddrs *ifap, *p;
1110 struct sockaddr_in *sain;
1111 struct sockaddr_in6 *sin6;
1112 struct address *h;
1113 int cnt = 0, af;
1115 if (getifaddrs(&ifap) == -1)
1116 fatal("getifaddrs");
1118 /* First search for IPv4 addresses */
1119 af = AF_INET;
1121 nextaf:
1122 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1123 if (p->ifa_addr == NULL ||
1124 p->ifa_addr->sa_family != af ||
1125 (strcmp(s, p->ifa_name) != 0 &&
1126 !is_if_in_group(p->ifa_name, s)))
1127 continue;
1128 if ((h = calloc(1, sizeof(*h))) == NULL)
1129 fatal("calloc");
1131 if (port)
1132 h->port = port;
1133 if (ifname != NULL) {
1134 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1135 sizeof(h->ifname)) {
1136 log_warnx("%s: interface name truncated",
1137 __func__);
1138 free(h);
1139 freeifaddrs(ifap);
1140 return (-1);
1143 if (ipproto != -1)
1144 h->ipproto = ipproto;
1145 h->ss.ss_family = af;
1146 h->prefixlen = -1; /* host address */
1148 if (af == AF_INET) {
1149 struct sockaddr_in *ra;
1150 sain = (struct sockaddr_in *)&h->ss;
1151 ra = (struct sockaddr_in *)p->ifa_addr;
1152 got_sockaddr_inet_init(sain, &ra->sin_addr);
1153 } else {
1154 struct sockaddr_in6 *ra;
1155 sin6 = (struct sockaddr_in6 *)&h->ss;
1156 ra = (struct sockaddr_in6 *)p->ifa_addr;
1157 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1158 ra->sin6_scope_id);
1161 if (add_addr(new_srv, h))
1162 return -1;
1163 cnt++;
1165 if (af == AF_INET) {
1166 /* Next search for IPv6 addresses */
1167 af = AF_INET6;
1168 goto nextaf;
1171 if (cnt > max) {
1172 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1173 s, max);
1175 freeifaddrs(ifap);
1176 return (cnt);
1179 int
1180 host(const char *s, struct server *new_srv, int max,
1181 in_port_t port, const char *ifname, int ipproto)
1183 struct address *h;
1185 h = host_v4(s);
1187 /* IPv6 address? */
1188 if (h == NULL)
1189 h = host_v6(s);
1191 if (h != NULL) {
1192 if (port)
1193 h->port = port;
1194 if (ifname != NULL) {
1195 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1196 sizeof(h->ifname)) {
1197 log_warnx("%s: interface name truncated",
1198 __func__);
1199 free(h);
1200 return (-1);
1203 if (ipproto != -1)
1204 h->ipproto = ipproto;
1206 if (add_addr(new_srv, h))
1207 return -1;
1208 return (1);
1211 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1214 int
1215 is_if_in_group(const char *ifname, const char *groupname)
1217 unsigned int len;
1218 struct ifgroupreq ifgr;
1219 struct ifg_req *ifg;
1220 int s;
1221 int ret = 0;
1223 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1224 err(1, "socket");
1226 memset(&ifgr, 0, sizeof(ifgr));
1227 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1228 err(1, "IFNAMSIZ");
1229 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1230 if (errno == EINVAL || errno == ENOTTY)
1231 goto end;
1232 err(1, "SIOCGIFGROUP");
1235 len = ifgr.ifgr_len;
1236 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1237 sizeof(struct ifg_req));
1238 if (ifgr.ifgr_groups == NULL)
1239 err(1, "getifgroups");
1240 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1241 err(1, "SIOCGIFGROUP");
1243 ifg = ifgr.ifgr_groups;
1244 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1245 len -= sizeof(struct ifg_req);
1246 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1247 ret = 1;
1248 break;
1251 free(ifgr.ifgr_groups);
1253 end:
1254 close(s);
1255 return (ret);
1258 int
1259 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1261 if (strcmp("", addr) == 0) {
1262 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1263 -1) <= 0) {
1264 yyerror("invalid listen ip: %s",
1265 "127.0.0.1");
1266 return (-1);
1268 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1269 yyerror("invalid listen ip: %s", "::1");
1270 return (-1);
1272 } else {
1273 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1274 -1) <= 0) {
1275 yyerror("invalid listen ip: %s", addr);
1276 return (-1);
1279 return (0);
1282 int
1283 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1284 const char *other_srv)
1286 struct address *a;
1287 void *ia;
1288 char buf[INET6_ADDRSTRLEN];
1289 const char *addrstr;
1291 TAILQ_FOREACH(a, al, entry) {
1292 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1293 a->port != h->port)
1294 continue;
1296 switch (h->ss.ss_family) {
1297 case AF_INET:
1298 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1299 break;
1300 case AF_INET6:
1301 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1302 break;
1303 default:
1304 yyerror("unknown address family: %d", h->ss.ss_family);
1305 return -1;
1307 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1308 if (addrstr) {
1309 if (other_srv) {
1310 yyerror("server %s: duplicate fcgi listen "
1311 "address %s:%d, already used by server %s",
1312 new_srv, addrstr, h->port, other_srv);
1313 } else {
1314 log_warnx("server: %s: duplicate fcgi listen "
1315 "address %s:%d", new_srv, addrstr, h->port);
1317 } else {
1318 if (other_srv) {
1319 yyerror("server: %s: duplicate fcgi listen "
1320 "address, already used by server %s",
1321 new_srv, other_srv);
1322 } else {
1323 log_warnx("server %s: duplicate fcgi listen "
1324 "address", new_srv);
1328 return -1;
1331 return 0;
1334 int
1335 add_addr(struct server *new_srv, struct address *h)
1337 struct server *srv;
1339 /* Address cannot be shared between different servers. */
1340 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1341 if (srv == new_srv)
1342 continue;
1343 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1344 return -1;
1347 /* Tolerate duplicate address lines within the scope of a server. */
1348 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1349 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1350 else
1351 free(h);
1353 return 0;