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
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 : /* empty */
135 | grammar '\n'
136 | grammar varset '\n'
137 | grammar main '\n'
138 | grammar server '\n'
139 | grammar error '\n' { file->errors++; }
142 varset : STRING '=' STRING {
143 char *s = $1;
144 while (*s++) {
145 if (isspace((unsigned char)*s)) {
146 yyerror("macro name cannot contain "
147 "whitespace");
148 free($1);
149 free($3);
150 YYERROR;
153 if (symset($1, $3, 0) == -1)
154 fatal("cannot store variable");
155 free($1);
156 free($3);
160 boolean : STRING {
161 if (strcasecmp($1, "1") == 0 ||
162 strcasecmp($1, "yes") == 0 ||
163 strcasecmp($1, "on") == 0)
164 $$ = 1;
165 else if (strcasecmp($1, "0") == 0 ||
166 strcasecmp($1, "off") == 0 ||
167 strcasecmp($1, "no") == 0)
168 $$ = 0;
169 else {
170 yyerror("invalid boolean value '%s'", $1);
171 free($1);
172 YYERROR;
174 free($1);
176 | ON { $$ = 1; }
177 | NUMBER { $$ = $1; }
180 fcgiport : PORT NUMBER {
181 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
182 yyerror("invalid port: %lld", $2);
183 YYERROR;
185 $$ = $2;
187 | PORT STRING {
188 int val;
190 if ((val = getservice($2)) == -1) {
191 yyerror("invalid port: %s", $2);
192 free($2);
193 YYERROR;
195 free($2);
197 $$ = val;
201 main : PREFORK NUMBER {
202 gotwebd->prefork_gotwebd = $2;
204 | CHROOT STRING {
205 n = strlcpy(gotwebd->httpd_chroot, $2,
206 sizeof(gotwebd->httpd_chroot));
207 if (n >= sizeof(gotwebd->httpd_chroot)) {
208 yyerror("%s: httpd_chroot truncated", __func__);
209 free($2);
210 YYERROR;
212 free($2);
214 | UNIX_SOCKET boolean {
215 gotwebd->unix_socket = $2;
217 | UNIX_SOCKET_NAME STRING {
218 n = snprintf(gotwebd->unix_socket_name,
219 sizeof(gotwebd->unix_socket_name), "%s%s",
220 strlen(gotwebd->httpd_chroot) ?
221 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
222 if (n < 0 ||
223 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
224 yyerror("%s: unix_socket_name truncated",
225 __func__);
226 free($2);
227 YYERROR;
229 free($2);
233 server : SERVER STRING {
234 struct server *srv;
236 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
237 if (strcmp(srv->name, $2) == 0) {
238 yyerror("server name exists '%s'", $2);
239 free($2);
240 YYERROR;
244 new_srv = conf_new_server($2);
245 log_debug("adding server %s", $2);
246 free($2);
248 | SERVER STRING {
249 struct server *srv;
251 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
252 if (strcmp(srv->name, $2) == 0) {
253 yyerror("server name exists '%s'", $2);
254 free($2);
255 YYERROR;
259 new_srv = conf_new_server($2);
260 log_debug("adding server %s", $2);
261 free($2);
262 } '{' optnl serveropts2 '}' {
266 serveropts1 : REPOS_PATH STRING {
267 n = strlcpy(new_srv->repos_path, $2,
268 sizeof(new_srv->repos_path));
269 if (n >= sizeof(new_srv->repos_path)) {
270 yyerror("%s: repos_path truncated", __func__);
271 free($2);
272 YYERROR;
274 free($2);
276 | SITE_NAME STRING {
277 n = strlcpy(new_srv->site_name, $2,
278 sizeof(new_srv->site_name));
279 if (n >= sizeof(new_srv->site_name)) {
280 yyerror("%s: site_name truncated", __func__);
281 free($2);
282 YYERROR;
284 free($2);
286 | SITE_OWNER STRING {
287 n = strlcpy(new_srv->site_owner, $2,
288 sizeof(new_srv->site_owner));
289 if (n >= sizeof(new_srv->site_owner)) {
290 yyerror("%s: site_owner truncated", __func__);
291 free($2);
292 YYERROR;
294 free($2);
296 | SITE_LINK STRING {
297 n = strlcpy(new_srv->site_link, $2,
298 sizeof(new_srv->site_link));
299 if (n >= sizeof(new_srv->site_link)) {
300 yyerror("%s: site_link truncated", __func__);
301 free($2);
302 YYERROR;
304 free($2);
306 | LOGO STRING {
307 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
308 if (n >= sizeof(new_srv->logo)) {
309 yyerror("%s: logo truncated", __func__);
310 free($2);
311 YYERROR;
313 free($2);
315 | LOGO_URL STRING {
316 n = strlcpy(new_srv->logo_url, $2,
317 sizeof(new_srv->logo_url));
318 if (n >= sizeof(new_srv->logo_url)) {
319 yyerror("%s: logo_url truncated", __func__);
320 free($2);
321 YYERROR;
323 free($2);
325 | CUSTOM_CSS STRING {
326 n = strlcpy(new_srv->custom_css, $2,
327 sizeof(new_srv->custom_css));
328 if (n >= sizeof(new_srv->custom_css)) {
329 yyerror("%s: custom_css truncated", __func__);
330 free($2);
331 YYERROR;
333 free($2);
335 | LISTEN ON STRING fcgiport {
336 if (get_addrs($3, new_srv, $4) == -1) {
337 yyerror("could not get addrs");
338 YYERROR;
340 new_srv->fcgi_socket = 1;
342 | MAX_REPOS NUMBER {
343 if ($2 > 0)
344 new_srv->max_repos = $2;
346 | SHOW_SITE_OWNER boolean {
347 new_srv->show_site_owner = $2;
349 | SHOW_REPO_OWNER boolean {
350 new_srv->show_repo_owner = $2;
352 | SHOW_REPO_AGE boolean {
353 new_srv->show_repo_age = $2;
355 | SHOW_REPO_DESCRIPTION boolean {
356 new_srv->show_repo_description = $2;
358 | SHOW_REPO_CLONEURL boolean {
359 new_srv->show_repo_cloneurl = $2;
361 | MAX_REPOS_DISPLAY NUMBER {
362 new_srv->max_repos_display = $2;
364 | MAX_COMMITS_DISPLAY NUMBER {
365 if ($2 > 0)
366 new_srv->max_commits_display = $2;
368 | UNIX_SOCKET boolean {
369 new_srv->unix_socket = $2;
371 | UNIX_SOCKET_NAME STRING {
372 n = snprintf(new_srv->unix_socket_name,
373 sizeof(new_srv->unix_socket_name), "%s%s",
374 strlen(gotwebd->httpd_chroot) ?
375 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
376 if (n < 0 ||
377 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
378 yyerror("%s: unix_socket_name truncated",
379 __func__);
380 free($2);
381 YYERROR;
383 free($2);
387 serveropts2 : serveropts2 serveropts1 nl
388 | serveropts1 optnl
391 nl : '\n' optnl
394 optnl : '\n' optnl /* zero or more newlines */
395 | /* empty */
398 %%
400 struct keywords {
401 const char *k_name;
402 int k_val;
403 };
405 int
406 yyerror(const char *fmt, ...)
408 va_list ap;
409 char *msg;
411 file->errors++;
412 va_start(ap, fmt);
413 if (vasprintf(&msg, fmt, ap) == -1)
414 fatalx("yyerror vasprintf");
415 va_end(ap);
416 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
417 free(msg);
418 return (0);
421 int
422 kw_cmp(const void *k, const void *e)
424 return (strcmp(k, ((const struct keywords *)e)->k_name));
427 int
428 lookup(char *s)
430 /* This has to be sorted always. */
431 static const struct keywords keywords[] = {
432 { "chroot", CHROOT },
433 { "custom_css", CUSTOM_CSS },
434 { "listen", LISTEN },
435 { "logo", LOGO },
436 { "logo_url" , LOGO_URL },
437 { "max_commits_display", MAX_COMMITS_DISPLAY },
438 { "max_repos", MAX_REPOS },
439 { "max_repos_display", MAX_REPOS_DISPLAY },
440 { "on", ON },
441 { "port", PORT },
442 { "prefork", PREFORK },
443 { "repos_path", REPOS_PATH },
444 { "server", SERVER },
445 { "show_repo_age", SHOW_REPO_AGE },
446 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
447 { "show_repo_description", SHOW_REPO_DESCRIPTION },
448 { "show_repo_owner", SHOW_REPO_OWNER },
449 { "show_site_owner", SHOW_SITE_OWNER },
450 { "site_link", SITE_LINK },
451 { "site_name", SITE_NAME },
452 { "site_owner", SITE_OWNER },
453 { "unix_socket", UNIX_SOCKET },
454 { "unix_socket_name", UNIX_SOCKET_NAME },
455 };
456 const struct keywords *p;
458 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
459 sizeof(keywords[0]), kw_cmp);
461 if (p)
462 return (p->k_val);
463 else
464 return (STRING);
467 #define MAXPUSHBACK 128
469 unsigned char *parsebuf;
470 int parseindex;
471 unsigned char pushback_buffer[MAXPUSHBACK];
472 int pushback_index = 0;
474 int
475 lgetc(int quotec)
477 int c, next;
479 if (parsebuf) {
480 /* Read character from the parsebuffer instead of input. */
481 if (parseindex >= 0) {
482 c = parsebuf[parseindex++];
483 if (c != '\0')
484 return (c);
485 parsebuf = NULL;
486 } else
487 parseindex++;
490 if (pushback_index)
491 return (pushback_buffer[--pushback_index]);
493 if (quotec) {
494 c = getc(file->stream);
495 if (c == EOF)
496 yyerror("reached end of file while parsing "
497 "quoted string");
498 return (c);
501 c = getc(file->stream);
502 while (c == '\\') {
503 next = getc(file->stream);
504 if (next != '\n') {
505 c = next;
506 break;
508 yylval.lineno = file->lineno;
509 file->lineno++;
510 c = getc(file->stream);
513 return (c);
516 int
517 lungetc(int c)
519 if (c == EOF)
520 return (EOF);
521 if (parsebuf) {
522 parseindex--;
523 if (parseindex >= 0)
524 return (c);
526 if (pushback_index < MAXPUSHBACK-1)
527 return (pushback_buffer[pushback_index++] = c);
528 else
529 return (EOF);
532 int
533 findeol(void)
535 int c;
537 parsebuf = NULL;
539 /* Skip to either EOF or the first real EOL. */
540 while (1) {
541 if (pushback_index)
542 c = pushback_buffer[--pushback_index];
543 else
544 c = lgetc(0);
545 if (c == '\n') {
546 file->lineno++;
547 break;
549 if (c == EOF)
550 break;
552 return (ERROR);
555 int
556 yylex(void)
558 unsigned char buf[8096];
559 unsigned char *p, *val;
560 int quotec, next, c;
561 int token;
563 top:
564 p = buf;
565 c = lgetc(0);
566 while (c == ' ' || c == '\t')
567 c = lgetc(0); /* nothing */
569 yylval.lineno = file->lineno;
570 if (c == '#') {
571 c = lgetc(0);
572 while (c != '\n' && c != EOF)
573 c = lgetc(0); /* nothing */
575 if (c == '$' && parsebuf == NULL) {
576 while (1) {
577 c = lgetc(0);
578 if (c == EOF)
579 return (0);
581 if (p + 1 >= buf + sizeof(buf) - 1) {
582 yyerror("string too long");
583 return (findeol());
585 if (isalnum(c) || c == '_') {
586 *p++ = c;
587 continue;
589 *p = '\0';
590 lungetc(c);
591 break;
593 val = symget(buf);
594 if (val == NULL) {
595 yyerror("macro '%s' not defined", buf);
596 return (findeol());
598 parsebuf = val;
599 parseindex = 0;
600 goto top;
603 switch (c) {
604 case '\'':
605 case '"':
606 quotec = c;
607 while (1) {
608 c = lgetc(quotec);
609 if (c == EOF)
610 return (0);
611 if (c == '\n') {
612 file->lineno++;
613 continue;
614 } else if (c == '\\') {
615 next = lgetc(quotec);
616 if (next == EOF)
617 return (0);
618 if (next == quotec || c == ' ' || c == '\t')
619 c = next;
620 else if (next == '\n') {
621 file->lineno++;
622 continue;
623 } else
624 lungetc(next);
625 } else if (c == quotec) {
626 *p = '\0';
627 break;
628 } else if (c == '\0') {
629 yyerror("syntax error");
630 return (findeol());
632 if (p + 1 >= buf + sizeof(buf) - 1) {
633 yyerror("string too long");
634 return (findeol());
636 *p++ = c;
638 yylval.v.string = strdup(buf);
639 if (yylval.v.string == NULL)
640 err(1, "yylex: strdup");
641 return (STRING);
644 #define allowed_to_end_number(x) \
645 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
647 if (c == '-' || isdigit(c)) {
648 do {
649 *p++ = c;
650 if ((unsigned)(p-buf) >= sizeof(buf)) {
651 yyerror("string too long");
652 return (findeol());
654 c = lgetc(0);
655 } while (c != EOF && isdigit(c));
656 lungetc(c);
657 if (p == buf + 1 && buf[0] == '-')
658 goto nodigits;
659 if (c == EOF || allowed_to_end_number(c)) {
660 const char *errstr = NULL;
662 *p = '\0';
663 yylval.v.number = strtonum(buf, LLONG_MIN,
664 LLONG_MAX, &errstr);
665 if (errstr) {
666 yyerror("\"%s\" invalid number: %s",
667 buf, errstr);
668 return (findeol());
670 return (NUMBER);
671 } else {
672 nodigits:
673 while (p > buf + 1)
674 lungetc(*--p);
675 c = *--p;
676 if (c == '-')
677 return (c);
681 #define allowed_in_string(x) \
682 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
683 x != '{' && x != '}' && \
684 x != '!' && x != '=' && x != '#' && \
685 x != ','))
687 if (isalnum(c) || c == ':' || c == '_') {
688 do {
689 *p++ = c;
690 if ((unsigned)(p-buf) >= sizeof(buf)) {
691 yyerror("string too long");
692 return (findeol());
694 c = lgetc(0);
695 } while (c != EOF && (allowed_in_string(c)));
696 lungetc(c);
697 *p = '\0';
698 token = lookup(buf);
699 if (token == STRING) {
700 yylval.v.string = strdup(buf);
701 if (yylval.v.string == NULL)
702 err(1, "yylex: strdup");
704 return (token);
706 if (c == '\n') {
707 yylval.lineno = file->lineno;
708 file->lineno++;
710 if (c == EOF)
711 return (0);
712 return (c);
715 int
716 check_file_secrecy(int fd, const char *fname)
718 struct stat st;
720 if (fstat(fd, &st)) {
721 log_warn("cannot stat %s", fname);
722 return (-1);
724 if (st.st_uid != 0 && st.st_uid != getuid()) {
725 log_warnx("%s: owner not root or current user", fname);
726 return (-1);
728 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
729 log_warnx("%s: group writable or world read/writable", fname);
730 return (-1);
732 return (0);
735 struct file *
736 newfile(const char *name, int secret)
738 struct file *nfile;
740 nfile = calloc(1, sizeof(struct file));
741 if (nfile == NULL) {
742 log_warn("calloc");
743 return (NULL);
745 nfile->name = strdup(name);
746 if (nfile->name == NULL) {
747 log_warn("strdup");
748 free(nfile);
749 return (NULL);
751 nfile->stream = fopen(nfile->name, "r");
752 if (nfile->stream == NULL) {
753 /* no warning, we don't require a conf file */
754 free(nfile->name);
755 free(nfile);
756 return (NULL);
757 } else if (secret &&
758 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
759 fclose(nfile->stream);
760 free(nfile->name);
761 free(nfile);
762 return (NULL);
764 nfile->lineno = 1;
765 return (nfile);
768 static void
769 closefile(struct file *xfile)
771 fclose(xfile->stream);
772 free(xfile->name);
773 free(xfile);
776 static void
777 add_default_server(void)
779 new_srv = conf_new_server(D_SITENAME);
780 log_debug("%s: adding default server %s", __func__, D_SITENAME);
783 int
784 parse_config(const char *filename, struct gotwebd *env)
786 struct sym *sym, *next;
788 if (config_init(env) == -1)
789 fatalx("failed to initialize configuration");
791 gotwebd = env;
793 file = newfile(filename, 0);
794 if (file == NULL) {
795 add_default_server();
796 sockets_parse_sockets(env);
797 /* just return, as we don't require a conf file */
798 return (0);
801 yyparse();
802 errors = file->errors;
803 closefile(file);
805 /* Free macros and check which have not been used. */
806 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
807 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
808 fprintf(stderr, "warning: macro '%s' not used\n",
809 sym->nam);
810 if (!sym->persist) {
811 free(sym->nam);
812 free(sym->val);
813 TAILQ_REMOVE(&symhead, sym, entry);
814 free(sym);
818 if (errors)
819 return (-1);
821 /* just add default server if no config specified */
822 if (gotwebd->server_cnt == 0)
823 add_default_server();
825 /* setup our listening sockets */
826 sockets_parse_sockets(env);
828 return (0);
831 struct server *
832 conf_new_server(const char *name)
834 struct server *srv = NULL;
836 srv = calloc(1, sizeof(*srv));
837 if (srv == NULL)
838 fatalx("%s: calloc", __func__);
840 n = strlcpy(srv->name, name, sizeof(srv->name));
841 if (n >= sizeof(srv->name))
842 fatalx("%s: strlcpy", __func__);
843 n = snprintf(srv->unix_socket_name,
844 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
845 D_UNIX_SOCKET);
846 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
847 fatalx("%s: snprintf", __func__);
848 n = strlcpy(srv->repos_path, D_GOTPATH,
849 sizeof(srv->repos_path));
850 if (n >= sizeof(srv->repos_path))
851 fatalx("%s: strlcpy", __func__);
852 n = strlcpy(srv->site_name, D_SITENAME,
853 sizeof(srv->site_name));
854 if (n >= sizeof(srv->site_name))
855 fatalx("%s: strlcpy", __func__);
856 n = strlcpy(srv->site_owner, D_SITEOWNER,
857 sizeof(srv->site_owner));
858 if (n >= sizeof(srv->site_owner))
859 fatalx("%s: strlcpy", __func__);
860 n = strlcpy(srv->site_link, D_SITELINK,
861 sizeof(srv->site_link));
862 if (n >= sizeof(srv->site_link))
863 fatalx("%s: strlcpy", __func__);
864 n = strlcpy(srv->logo, D_GOTLOGO,
865 sizeof(srv->logo));
866 if (n >= sizeof(srv->logo))
867 fatalx("%s: strlcpy", __func__);
868 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
869 if (n >= sizeof(srv->logo_url))
870 fatalx("%s: strlcpy", __func__);
871 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
872 if (n >= sizeof(srv->custom_css))
873 fatalx("%s: strlcpy", __func__);
875 srv->show_site_owner = D_SHOWSOWNER;
876 srv->show_repo_owner = D_SHOWROWNER;
877 srv->show_repo_age = D_SHOWAGE;
878 srv->show_repo_description = D_SHOWDESC;
879 srv->show_repo_cloneurl = D_SHOWURL;
881 srv->max_repos_display = D_MAXREPODISP;
882 srv->max_commits_display = D_MAXCOMMITDISP;
883 srv->max_repos = D_MAXREPO;
885 srv->unix_socket = 1;
886 srv->fcgi_socket = 0;
888 TAILQ_INIT(&srv->al);
889 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
890 gotwebd->server_cnt++;
892 return srv;
893 };
895 int
896 symset(const char *nam, const char *val, int persist)
898 struct sym *sym;
900 TAILQ_FOREACH(sym, &symhead, entry) {
901 if (strcmp(nam, sym->nam) == 0)
902 break;
905 if (sym != NULL) {
906 if (sym->persist == 1)
907 return (0);
908 else {
909 free(sym->nam);
910 free(sym->val);
911 TAILQ_REMOVE(&symhead, sym, entry);
912 free(sym);
915 sym = calloc(1, sizeof(*sym));
916 if (sym == NULL)
917 return (-1);
919 sym->nam = strdup(nam);
920 if (sym->nam == NULL) {
921 free(sym);
922 return (-1);
924 sym->val = strdup(val);
925 if (sym->val == NULL) {
926 free(sym->nam);
927 free(sym);
928 return (-1);
930 sym->used = 0;
931 sym->persist = persist;
932 TAILQ_INSERT_TAIL(&symhead, sym, entry);
933 return (0);
936 int
937 cmdline_symset(char *s)
939 char *sym, *val;
940 int ret;
942 val = strrchr(s, '=');
943 if (val == NULL)
944 return (-1);
946 sym = strndup(s, val - s);
947 if (sym == NULL)
948 fatal("%s: strndup", __func__);
950 ret = symset(sym, val + 1, 1);
951 free(sym);
953 return (ret);
956 char *
957 symget(const char *nam)
959 struct sym *sym;
961 TAILQ_FOREACH(sym, &symhead, entry) {
962 if (strcmp(nam, sym->nam) == 0) {
963 sym->used = 1;
964 return (sym->val);
967 return (NULL);
970 int
971 getservice(const char *n)
973 struct servent *s;
974 const char *errstr;
975 long long llval;
977 llval = strtonum(n, 0, UINT16_MAX, &errstr);
978 if (errstr) {
979 s = getservbyname(n, "tcp");
980 if (s == NULL)
981 s = getservbyname(n, "udp");
982 if (s == NULL)
983 return (-1);
984 return ntohs(s->s_port);
987 return (unsigned short)llval;
990 struct address *
991 host_v4(const char *s)
993 struct in_addr ina;
994 struct sockaddr_in *sain;
995 struct address *h;
997 memset(&ina, 0, sizeof(ina));
998 if (inet_pton(AF_INET, s, &ina) != 1)
999 return (NULL);
1001 if ((h = calloc(1, sizeof(*h))) == NULL)
1002 fatal(__func__);
1003 sain = (struct sockaddr_in *)&h->ss;
1004 got_sockaddr_inet_init(sain, &ina);
1005 if (sain->sin_addr.s_addr == INADDR_ANY)
1006 h->prefixlen = 0; /* 0.0.0.0 address */
1007 else
1008 h->prefixlen = -1; /* host address */
1009 return (h);
1012 struct address *
1013 host_v6(const char *s)
1015 struct addrinfo hints, *res;
1016 struct sockaddr_in6 *sa_in6, *ra;
1017 struct address *h = NULL;
1019 memset(&hints, 0, sizeof(hints));
1020 hints.ai_family = AF_INET6;
1021 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1022 hints.ai_flags = AI_NUMERICHOST;
1023 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1024 if ((h = calloc(1, sizeof(*h))) == NULL)
1025 fatal(__func__);
1026 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1027 ra = (struct sockaddr_in6 *)res->ai_addr;
1028 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1029 ra->sin6_scope_id);
1030 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1031 sizeof(sa_in6->sin6_addr)) == 0)
1032 h->prefixlen = 0; /* any address */
1033 else
1034 h->prefixlen = -1; /* host address */
1035 freeaddrinfo(res);
1038 return (h);
1041 int
1042 host_dns(const char *s, struct server *new_srv, int max,
1043 in_port_t port, const char *ifname, int ipproto)
1045 struct addrinfo hints, *res0, *res;
1046 int error, cnt = 0;
1047 struct sockaddr_in *sain;
1048 struct sockaddr_in6 *sin6;
1049 struct address *h;
1051 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1052 return (cnt);
1054 memset(&hints, 0, sizeof(hints));
1055 hints.ai_family = PF_UNSPEC;
1056 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1057 hints.ai_flags = AI_ADDRCONFIG;
1058 error = getaddrinfo(s, NULL, &hints, &res0);
1059 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1060 return (0);
1061 if (error) {
1062 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1063 gai_strerror(error));
1064 return (-1);
1067 for (res = res0; res && cnt < max; res = res->ai_next) {
1068 if (res->ai_family != AF_INET &&
1069 res->ai_family != AF_INET6)
1070 continue;
1071 if ((h = calloc(1, sizeof(*h))) == NULL)
1072 fatal(__func__);
1074 if (port)
1075 h->port = port;
1076 if (ifname != NULL) {
1077 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1078 sizeof(h->ifname)) {
1079 log_warnx("%s: interface name truncated",
1080 __func__);
1081 freeaddrinfo(res0);
1082 free(h);
1083 return (-1);
1086 if (ipproto != -1)
1087 h->ipproto = ipproto;
1088 h->ss.ss_family = res->ai_family;
1089 h->prefixlen = -1; /* host address */
1091 if (res->ai_family == AF_INET) {
1092 struct sockaddr_in *ra;
1093 sain = (struct sockaddr_in *)&h->ss;
1094 ra = (struct sockaddr_in *)res->ai_addr;
1095 got_sockaddr_inet_init(sain, &ra->sin_addr);
1096 } else {
1097 struct sockaddr_in6 *ra;
1098 sin6 = (struct sockaddr_in6 *)&h->ss;
1099 ra = (struct sockaddr_in6 *)res->ai_addr;
1100 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1103 if (add_addr(new_srv, h))
1104 return -1;
1105 cnt++;
1107 if (cnt == max && res) {
1108 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1109 s, max);
1111 freeaddrinfo(res0);
1112 return (cnt);
1115 int
1116 host_if(const char *s, struct server *new_srv, int max,
1117 in_port_t port, const char *ifname, int ipproto)
1119 struct ifaddrs *ifap, *p;
1120 struct sockaddr_in *sain;
1121 struct sockaddr_in6 *sin6;
1122 struct address *h;
1123 int cnt = 0, af;
1125 if (getifaddrs(&ifap) == -1)
1126 fatal("getifaddrs");
1128 /* First search for IPv4 addresses */
1129 af = AF_INET;
1131 nextaf:
1132 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1133 if (p->ifa_addr == NULL ||
1134 p->ifa_addr->sa_family != af ||
1135 (strcmp(s, p->ifa_name) != 0 &&
1136 !is_if_in_group(p->ifa_name, s)))
1137 continue;
1138 if ((h = calloc(1, sizeof(*h))) == NULL)
1139 fatal("calloc");
1141 if (port)
1142 h->port = port;
1143 if (ifname != NULL) {
1144 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1145 sizeof(h->ifname)) {
1146 log_warnx("%s: interface name truncated",
1147 __func__);
1148 free(h);
1149 freeifaddrs(ifap);
1150 return (-1);
1153 if (ipproto != -1)
1154 h->ipproto = ipproto;
1155 h->ss.ss_family = af;
1156 h->prefixlen = -1; /* host address */
1158 if (af == AF_INET) {
1159 struct sockaddr_in *ra;
1160 sain = (struct sockaddr_in *)&h->ss;
1161 ra = (struct sockaddr_in *)p->ifa_addr;
1162 got_sockaddr_inet_init(sain, &ra->sin_addr);
1163 } else {
1164 struct sockaddr_in6 *ra;
1165 sin6 = (struct sockaddr_in6 *)&h->ss;
1166 ra = (struct sockaddr_in6 *)p->ifa_addr;
1167 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1168 ra->sin6_scope_id);
1171 if (add_addr(new_srv, h))
1172 return -1;
1173 cnt++;
1175 if (af == AF_INET) {
1176 /* Next search for IPv6 addresses */
1177 af = AF_INET6;
1178 goto nextaf;
1181 if (cnt > max) {
1182 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1183 s, max);
1185 freeifaddrs(ifap);
1186 return (cnt);
1189 int
1190 host(const char *s, struct server *new_srv, int max,
1191 in_port_t port, const char *ifname, int ipproto)
1193 struct address *h;
1195 h = host_v4(s);
1197 /* IPv6 address? */
1198 if (h == NULL)
1199 h = host_v6(s);
1201 if (h != NULL) {
1202 if (port)
1203 h->port = port;
1204 if (ifname != NULL) {
1205 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1206 sizeof(h->ifname)) {
1207 log_warnx("%s: interface name truncated",
1208 __func__);
1209 free(h);
1210 return (-1);
1213 if (ipproto != -1)
1214 h->ipproto = ipproto;
1216 if (add_addr(new_srv, h))
1217 return -1;
1218 return (1);
1221 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1224 int
1225 is_if_in_group(const char *ifname, const char *groupname)
1227 unsigned int len;
1228 struct ifgroupreq ifgr;
1229 struct ifg_req *ifg;
1230 int s;
1231 int ret = 0;
1233 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1234 err(1, "socket");
1236 memset(&ifgr, 0, sizeof(ifgr));
1237 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1238 err(1, "IFNAMSIZ");
1239 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1240 if (errno == EINVAL || errno == ENOTTY)
1241 goto end;
1242 err(1, "SIOCGIFGROUP");
1245 len = ifgr.ifgr_len;
1246 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1247 sizeof(struct ifg_req));
1248 if (ifgr.ifgr_groups == NULL)
1249 err(1, "getifgroups");
1250 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1251 err(1, "SIOCGIFGROUP");
1253 ifg = ifgr.ifgr_groups;
1254 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1255 len -= sizeof(struct ifg_req);
1256 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1257 ret = 1;
1258 break;
1261 free(ifgr.ifgr_groups);
1263 end:
1264 close(s);
1265 return (ret);
1268 int
1269 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1271 if (strcmp("", addr) == 0) {
1272 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1273 -1) <= 0) {
1274 yyerror("invalid listen ip: %s",
1275 "127.0.0.1");
1276 return (-1);
1278 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1279 yyerror("invalid listen ip: %s", "::1");
1280 return (-1);
1282 } else {
1283 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1284 -1) <= 0) {
1285 yyerror("invalid listen ip: %s", addr);
1286 return (-1);
1289 return (0);
1292 int
1293 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1294 const char *other_srv)
1296 struct address *a;
1297 void *ia;
1298 char buf[INET6_ADDRSTRLEN];
1299 const char *addrstr;
1301 TAILQ_FOREACH(a, al, entry) {
1302 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1303 a->port != h->port)
1304 continue;
1306 switch (h->ss.ss_family) {
1307 case AF_INET:
1308 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1309 break;
1310 case AF_INET6:
1311 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1312 break;
1313 default:
1314 yyerror("unknown address family: %d", h->ss.ss_family);
1315 return -1;
1317 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1318 if (addrstr) {
1319 if (other_srv) {
1320 yyerror("server %s: duplicate fcgi listen "
1321 "address %s:%d, already used by server %s",
1322 new_srv, addrstr, h->port, other_srv);
1323 } else {
1324 log_warnx("server: %s: duplicate fcgi listen "
1325 "address %s:%d", new_srv, addrstr, h->port);
1327 } else {
1328 if (other_srv) {
1329 yyerror("server: %s: duplicate fcgi listen "
1330 "address, already used by server %s",
1331 new_srv, other_srv);
1332 } else {
1333 log_warnx("server %s: duplicate fcgi listen "
1334 "address", new_srv);
1338 return -1;
1341 return 0;
1344 int
1345 add_addr(struct server *new_srv, struct address *h)
1347 struct server *srv;
1349 /* Address cannot be shared between different servers. */
1350 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1351 if (srv == new_srv)
1352 continue;
1353 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1354 return -1;
1357 /* Tolerate duplicate address lines within the scope of a server. */
1358 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1359 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1360 else
1361 free(h);
1363 return 0;