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 <limits.h>
41 #include <netdb.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
49 #include "proc.h"
50 #include "gotwebd.h"
51 #include "got_sockaddr.h"
53 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 static struct file {
55 TAILQ_ENTRY(file) entry;
56 FILE *stream;
57 char *name;
58 int lineno;
59 int errors;
60 } *file;
61 struct file *newfile(const char *, int);
62 static void closefile(struct file *);
63 int check_file_secrecy(int, const char *);
64 int yyparse(void);
65 int yylex(void);
66 int yyerror(const char *, ...)
67 __attribute__((__format__ (printf, 1, 2)))
68 __attribute__((__nonnull__ (1)));
69 int kw_cmp(const void *, const void *);
70 int lookup(char *);
71 int lgetc(int);
72 int lungetc(int);
73 int findeol(void);
75 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 struct sym {
77 TAILQ_ENTRY(sym) entry;
78 int used;
79 int persist;
80 char *nam;
81 char *val;
82 };
84 int symset(const char *, const char *, int);
85 char *symget(const char *);
87 static int errors;
89 static struct gotwebd *gotwebd;
90 static struct server *new_srv;
91 static struct server *conf_new_server(const char *);
92 int getservice(const char *);
93 int n;
95 int get_addrs(const char *, struct server *, in_port_t);
96 int addr_dup_check(struct addresslist *, struct address *,
97 const char *, const char *);
98 int add_addr(struct server *, struct address *);
99 struct address *host_v4(const char *);
100 struct address *host_v6(const char *);
101 int host_dns(const char *, struct server *,
102 int, in_port_t, const char *, int);
103 int host_if(const char *, struct server *,
104 int, in_port_t, const char *, int);
105 int host(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
124 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
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, "yes") == 0 ||
162 strcasecmp($1, "on") == 0)
163 $$ = 1;
164 else if (strcasecmp($1, "0") == 0 ||
165 strcasecmp($1, "off") == 0 ||
166 strcasecmp($1, "no") == 0)
167 $$ = 0;
168 else {
169 yyerror("invalid boolean value '%s'", $1);
170 free($1);
171 YYERROR;
173 free($1);
175 | ON { $$ = 1; }
176 | NUMBER { $$ = $1; }
179 fcgiport : PORT NUMBER {
180 if ($2 <= 0 || $2 > (int)USHRT_MAX) {
181 yyerror("invalid port: %lld", $2);
182 YYERROR;
184 $$ = $2;
186 | PORT STRING {
187 int val;
189 if ((val = getservice($2)) == -1) {
190 yyerror("invalid port: %s", $2);
191 free($2);
192 YYERROR;
194 free($2);
196 $$ = val;
200 main : PREFORK NUMBER {
201 gotwebd->prefork_gotwebd = $2;
203 | CHROOT STRING {
204 n = strlcpy(gotwebd->httpd_chroot, $2,
205 sizeof(gotwebd->httpd_chroot));
206 if (n >= sizeof(gotwebd->httpd_chroot)) {
207 yyerror("%s: httpd_chroot truncated", __func__);
208 free($2);
209 YYERROR;
211 free($2);
213 | UNIX_SOCKET boolean {
214 gotwebd->unix_socket = $2;
216 | UNIX_SOCKET_NAME STRING {
217 n = snprintf(gotwebd->unix_socket_name,
218 sizeof(gotwebd->unix_socket_name), "%s%s",
219 strlen(gotwebd->httpd_chroot) ?
220 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
221 if (n < 0 ||
222 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
223 yyerror("%s: unix_socket_name truncated",
224 __func__);
225 free($2);
226 YYERROR;
228 free($2);
232 server : SERVER STRING {
233 struct server *srv;
235 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
236 if (strcmp(srv->name, $2) == 0) {
237 yyerror("server name exists '%s'", $2);
238 free($2);
239 YYERROR;
243 new_srv = conf_new_server($2);
244 log_debug("adding server %s", $2);
245 free($2);
247 | 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);
261 } '{' optnl serveropts2 '}' {
265 serveropts1 : REPOS_PATH STRING {
266 n = strlcpy(new_srv->repos_path, $2,
267 sizeof(new_srv->repos_path));
268 if (n >= sizeof(new_srv->repos_path)) {
269 yyerror("%s: repos_path truncated", __func__);
270 free($2);
271 YYERROR;
273 free($2);
275 | SITE_NAME STRING {
276 n = strlcpy(new_srv->site_name, $2,
277 sizeof(new_srv->site_name));
278 if (n >= sizeof(new_srv->site_name)) {
279 yyerror("%s: site_name truncated", __func__);
280 free($2);
281 YYERROR;
283 free($2);
285 | SITE_OWNER STRING {
286 n = strlcpy(new_srv->site_owner, $2,
287 sizeof(new_srv->site_owner));
288 if (n >= sizeof(new_srv->site_owner)) {
289 yyerror("%s: site_owner truncated", __func__);
290 free($2);
291 YYERROR;
293 free($2);
295 | SITE_LINK STRING {
296 n = strlcpy(new_srv->site_link, $2,
297 sizeof(new_srv->site_link));
298 if (n >= sizeof(new_srv->site_link)) {
299 yyerror("%s: site_link truncated", __func__);
300 free($2);
301 YYERROR;
303 free($2);
305 | LOGO STRING {
306 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
307 if (n >= sizeof(new_srv->logo)) {
308 yyerror("%s: logo truncated", __func__);
309 free($2);
310 YYERROR;
312 free($2);
314 | LOGO_URL STRING {
315 n = strlcpy(new_srv->logo_url, $2,
316 sizeof(new_srv->logo_url));
317 if (n >= sizeof(new_srv->logo_url)) {
318 yyerror("%s: logo_url truncated", __func__);
319 free($2);
320 YYERROR;
322 free($2);
324 | CUSTOM_CSS STRING {
325 n = strlcpy(new_srv->custom_css, $2,
326 sizeof(new_srv->custom_css));
327 if (n >= sizeof(new_srv->custom_css)) {
328 yyerror("%s: custom_css truncated", __func__);
329 free($2);
330 YYERROR;
332 free($2);
334 | LISTEN ON STRING fcgiport {
335 if (get_addrs($3, new_srv, $4) == -1) {
336 yyerror("could not get addrs");
337 YYERROR;
339 new_srv->fcgi_socket = 1;
341 | MAX_REPOS NUMBER {
342 if ($2 > 0)
343 new_srv->max_repos = $2;
345 | SHOW_SITE_OWNER boolean {
346 new_srv->show_site_owner = $2;
348 | SHOW_REPO_OWNER boolean {
349 new_srv->show_repo_owner = $2;
351 | SHOW_REPO_AGE boolean {
352 new_srv->show_repo_age = $2;
354 | SHOW_REPO_DESCRIPTION boolean {
355 new_srv->show_repo_description = $2;
357 | SHOW_REPO_CLONEURL boolean {
358 new_srv->show_repo_cloneurl = $2;
360 | MAX_REPOS_DISPLAY NUMBER {
361 new_srv->max_repos_display = $2;
363 | MAX_COMMITS_DISPLAY NUMBER {
364 if ($2 > 0)
365 new_srv->max_commits_display = $2;
367 | UNIX_SOCKET boolean {
368 new_srv->unix_socket = $2;
370 | UNIX_SOCKET_NAME STRING {
371 n = snprintf(new_srv->unix_socket_name,
372 sizeof(new_srv->unix_socket_name), "%s%s",
373 strlen(gotwebd->httpd_chroot) ?
374 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
375 if (n < 0 ||
376 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
377 yyerror("%s: unix_socket_name truncated",
378 __func__);
379 free($2);
380 YYERROR;
382 free($2);
386 serveropts2 : serveropts2 serveropts1 nl
387 | serveropts1 optnl
390 nl : '\n' optnl
393 optnl : '\n' optnl /* zero or more newlines */
394 | /* empty */
397 %%
399 struct keywords {
400 const char *k_name;
401 int k_val;
402 };
404 int
405 yyerror(const char *fmt, ...)
407 va_list ap;
408 char *msg;
410 file->errors++;
411 va_start(ap, fmt);
412 if (vasprintf(&msg, fmt, ap) == -1)
413 fatalx("yyerror vasprintf");
414 va_end(ap);
415 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
416 free(msg);
417 return (0);
420 int
421 kw_cmp(const void *k, const void *e)
423 return (strcmp(k, ((const struct keywords *)e)->k_name));
426 int
427 lookup(char *s)
429 /* This has to be sorted always. */
430 static const struct keywords keywords[] = {
431 { "chroot", CHROOT },
432 { "custom_css", CUSTOM_CSS },
433 { "listen", LISTEN },
434 { "logo", LOGO },
435 { "logo_url" , LOGO_URL },
436 { "max_commits_display", MAX_COMMITS_DISPLAY },
437 { "max_repos", MAX_REPOS },
438 { "max_repos_display", MAX_REPOS_DISPLAY },
439 { "on", ON },
440 { "port", PORT },
441 { "prefork", PREFORK },
442 { "repos_path", REPOS_PATH },
443 { "server", SERVER },
444 { "show_repo_age", SHOW_REPO_AGE },
445 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
446 { "show_repo_description", SHOW_REPO_DESCRIPTION },
447 { "show_repo_owner", SHOW_REPO_OWNER },
448 { "show_site_owner", SHOW_SITE_OWNER },
449 { "site_link", SITE_LINK },
450 { "site_name", SITE_NAME },
451 { "site_owner", SITE_OWNER },
452 { "unix_socket", UNIX_SOCKET },
453 { "unix_socket_name", UNIX_SOCKET_NAME },
454 };
455 const struct keywords *p;
457 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
458 sizeof(keywords[0]), kw_cmp);
460 if (p)
461 return (p->k_val);
462 else
463 return (STRING);
466 #define MAXPUSHBACK 128
468 unsigned char *parsebuf;
469 int parseindex;
470 unsigned char pushback_buffer[MAXPUSHBACK];
471 int pushback_index = 0;
473 int
474 lgetc(int quotec)
476 int c, next;
478 if (parsebuf) {
479 /* Read character from the parsebuffer instead of input. */
480 if (parseindex >= 0) {
481 c = parsebuf[parseindex++];
482 if (c != '\0')
483 return (c);
484 parsebuf = NULL;
485 } else
486 parseindex++;
489 if (pushback_index)
490 return (pushback_buffer[--pushback_index]);
492 if (quotec) {
493 c = getc(file->stream);
494 if (c == EOF)
495 yyerror("reached end of file while parsing "
496 "quoted string");
497 return (c);
500 c = getc(file->stream);
501 while (c == '\\') {
502 next = getc(file->stream);
503 if (next != '\n') {
504 c = next;
505 break;
507 yylval.lineno = file->lineno;
508 file->lineno++;
509 c = getc(file->stream);
512 return (c);
515 int
516 lungetc(int c)
518 if (c == EOF)
519 return (EOF);
520 if (parsebuf) {
521 parseindex--;
522 if (parseindex >= 0)
523 return (c);
525 if (pushback_index < MAXPUSHBACK-1)
526 return (pushback_buffer[pushback_index++] = c);
527 else
528 return (EOF);
531 int
532 findeol(void)
534 int c;
536 parsebuf = NULL;
538 /* Skip to either EOF or the first real EOL. */
539 while (1) {
540 if (pushback_index)
541 c = pushback_buffer[--pushback_index];
542 else
543 c = lgetc(0);
544 if (c == '\n') {
545 file->lineno++;
546 break;
548 if (c == EOF)
549 break;
551 return (ERROR);
554 int
555 yylex(void)
557 unsigned char buf[8096];
558 unsigned char *p, *val;
559 int quotec, next, c;
560 int token;
562 top:
563 p = buf;
564 c = lgetc(0);
565 while (c == ' ' || c == '\t')
566 c = lgetc(0); /* nothing */
568 yylval.lineno = file->lineno;
569 if (c == '#') {
570 c = lgetc(0);
571 while (c != '\n' && c != EOF)
572 c = lgetc(0); /* nothing */
574 if (c == '$' && parsebuf == NULL) {
575 while (1) {
576 c = lgetc(0);
577 if (c == EOF)
578 return (0);
580 if (p + 1 >= buf + sizeof(buf) - 1) {
581 yyerror("string too long");
582 return (findeol());
584 if (isalnum(c) || c == '_') {
585 *p++ = c;
586 continue;
588 *p = '\0';
589 lungetc(c);
590 break;
592 val = symget(buf);
593 if (val == NULL) {
594 yyerror("macro '%s' not defined", buf);
595 return (findeol());
597 parsebuf = val;
598 parseindex = 0;
599 goto top;
602 switch (c) {
603 case '\'':
604 case '"':
605 quotec = c;
606 while (1) {
607 c = lgetc(quotec);
608 if (c == EOF)
609 return (0);
610 if (c == '\n') {
611 file->lineno++;
612 continue;
613 } else if (c == '\\') {
614 next = lgetc(quotec);
615 if (next == EOF)
616 return (0);
617 if (next == quotec || c == ' ' || c == '\t')
618 c = next;
619 else if (next == '\n') {
620 file->lineno++;
621 continue;
622 } else
623 lungetc(next);
624 } else if (c == quotec) {
625 *p = '\0';
626 break;
627 } else if (c == '\0') {
628 yyerror("syntax error");
629 return (findeol());
631 if (p + 1 >= buf + sizeof(buf) - 1) {
632 yyerror("string too long");
633 return (findeol());
635 *p++ = c;
637 yylval.v.string = strdup(buf);
638 if (yylval.v.string == NULL)
639 err(1, "yylex: strdup");
640 return (STRING);
643 #define allowed_to_end_number(x) \
644 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
646 if (c == '-' || isdigit(c)) {
647 do {
648 *p++ = c;
649 if ((unsigned)(p-buf) >= sizeof(buf)) {
650 yyerror("string too long");
651 return (findeol());
653 c = lgetc(0);
654 } while (c != EOF && isdigit(c));
655 lungetc(c);
656 if (p == buf + 1 && buf[0] == '-')
657 goto nodigits;
658 if (c == EOF || allowed_to_end_number(c)) {
659 const char *errstr = NULL;
661 *p = '\0';
662 yylval.v.number = strtonum(buf, LLONG_MIN,
663 LLONG_MAX, &errstr);
664 if (errstr) {
665 yyerror("\"%s\" invalid number: %s",
666 buf, errstr);
667 return (findeol());
669 return (NUMBER);
670 } else {
671 nodigits:
672 while (p > buf + 1)
673 lungetc(*--p);
674 c = *--p;
675 if (c == '-')
676 return (c);
680 #define allowed_in_string(x) \
681 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
682 x != '{' && x != '}' && \
683 x != '!' && x != '=' && x != '#' && \
684 x != ','))
686 if (isalnum(c) || c == ':' || 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 && (allowed_in_string(c)));
695 lungetc(c);
696 *p = '\0';
697 token = lookup(buf);
698 if (token == STRING) {
699 yylval.v.string = strdup(buf);
700 if (yylval.v.string == NULL)
701 err(1, "yylex: strdup");
703 return (token);
705 if (c == '\n') {
706 yylval.lineno = file->lineno;
707 file->lineno++;
709 if (c == EOF)
710 return (0);
711 return (c);
714 int
715 check_file_secrecy(int fd, const char *fname)
717 struct stat st;
719 if (fstat(fd, &st)) {
720 log_warn("cannot stat %s", fname);
721 return (-1);
723 if (st.st_uid != 0 && st.st_uid != getuid()) {
724 log_warnx("%s: owner not root or current user", fname);
725 return (-1);
727 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
728 log_warnx("%s: group writable or world read/writable", fname);
729 return (-1);
731 return (0);
734 struct file *
735 newfile(const char *name, int secret)
737 struct file *nfile;
739 nfile = calloc(1, sizeof(struct file));
740 if (nfile == NULL) {
741 log_warn("calloc");
742 return (NULL);
744 nfile->name = strdup(name);
745 if (nfile->name == NULL) {
746 log_warn("strdup");
747 free(nfile);
748 return (NULL);
750 nfile->stream = fopen(nfile->name, "r");
751 if (nfile->stream == NULL) {
752 /* no warning, we don't require a conf file */
753 free(nfile->name);
754 free(nfile);
755 return (NULL);
756 } else if (secret &&
757 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
758 fclose(nfile->stream);
759 free(nfile->name);
760 free(nfile);
761 return (NULL);
763 nfile->lineno = 1;
764 return (nfile);
767 static void
768 closefile(struct file *xfile)
770 fclose(xfile->stream);
771 free(xfile->name);
772 free(xfile);
775 static void
776 add_default_server(void)
778 new_srv = conf_new_server(D_SITENAME);
779 log_debug("%s: adding default server %s", __func__, D_SITENAME);
782 int
783 parse_config(const char *filename, struct gotwebd *env)
785 struct sym *sym, *next;
787 if (config_init(env) == -1)
788 fatalx("failed to initialize configuration");
790 gotwebd = env;
792 file = newfile(filename, 0);
793 if (file == NULL) {
794 add_default_server();
795 sockets_parse_sockets(env);
796 /* just return, as we don't require a conf file */
797 return (0);
800 yyparse();
801 errors = file->errors;
802 closefile(file);
804 /* Free macros and check which have not been used. */
805 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
806 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
807 fprintf(stderr, "warning: macro '%s' not used\n",
808 sym->nam);
809 if (!sym->persist) {
810 free(sym->nam);
811 free(sym->val);
812 TAILQ_REMOVE(&symhead, sym, entry);
813 free(sym);
817 if (errors)
818 return (-1);
820 /* just add default server if no config specified */
821 if (gotwebd->server_cnt == 0)
822 add_default_server();
824 /* setup our listening sockets */
825 sockets_parse_sockets(env);
827 return (0);
830 struct server *
831 conf_new_server(const char *name)
833 struct server *srv = NULL;
835 srv = calloc(1, sizeof(*srv));
836 if (srv == NULL)
837 fatalx("%s: calloc", __func__);
839 n = strlcpy(srv->name, name, sizeof(srv->name));
840 if (n >= sizeof(srv->name))
841 fatalx("%s: strlcpy", __func__);
842 n = snprintf(srv->unix_socket_name,
843 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
844 D_UNIX_SOCKET);
845 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
846 fatalx("%s: snprintf", __func__);
847 n = strlcpy(srv->repos_path, D_GOTPATH,
848 sizeof(srv->repos_path));
849 if (n >= sizeof(srv->repos_path))
850 fatalx("%s: strlcpy", __func__);
851 n = strlcpy(srv->site_name, D_SITENAME,
852 sizeof(srv->site_name));
853 if (n >= sizeof(srv->site_name))
854 fatalx("%s: strlcpy", __func__);
855 n = strlcpy(srv->site_owner, D_SITEOWNER,
856 sizeof(srv->site_owner));
857 if (n >= sizeof(srv->site_owner))
858 fatalx("%s: strlcpy", __func__);
859 n = strlcpy(srv->site_link, D_SITELINK,
860 sizeof(srv->site_link));
861 if (n >= sizeof(srv->site_link))
862 fatalx("%s: strlcpy", __func__);
863 n = strlcpy(srv->logo, D_GOTLOGO,
864 sizeof(srv->logo));
865 if (n >= sizeof(srv->logo))
866 fatalx("%s: strlcpy", __func__);
867 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
868 if (n >= sizeof(srv->logo_url))
869 fatalx("%s: strlcpy", __func__);
870 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
871 if (n >= sizeof(srv->custom_css))
872 fatalx("%s: strlcpy", __func__);
874 srv->show_site_owner = D_SHOWSOWNER;
875 srv->show_repo_owner = D_SHOWROWNER;
876 srv->show_repo_age = D_SHOWAGE;
877 srv->show_repo_description = D_SHOWDESC;
878 srv->show_repo_cloneurl = D_SHOWURL;
880 srv->max_repos_display = D_MAXREPODISP;
881 srv->max_commits_display = D_MAXCOMMITDISP;
882 srv->max_repos = D_MAXREPO;
884 srv->unix_socket = 1;
885 srv->fcgi_socket = 0;
887 TAILQ_INIT(&srv->al);
888 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
889 gotwebd->server_cnt++;
891 return srv;
892 };
894 int
895 symset(const char *nam, const char *val, int persist)
897 struct sym *sym;
899 TAILQ_FOREACH(sym, &symhead, entry) {
900 if (strcmp(nam, sym->nam) == 0)
901 break;
904 if (sym != NULL) {
905 if (sym->persist == 1)
906 return (0);
907 else {
908 free(sym->nam);
909 free(sym->val);
910 TAILQ_REMOVE(&symhead, sym, entry);
911 free(sym);
914 sym = calloc(1, sizeof(*sym));
915 if (sym == NULL)
916 return (-1);
918 sym->nam = strdup(nam);
919 if (sym->nam == NULL) {
920 free(sym);
921 return (-1);
923 sym->val = strdup(val);
924 if (sym->val == NULL) {
925 free(sym->nam);
926 free(sym);
927 return (-1);
929 sym->used = 0;
930 sym->persist = persist;
931 TAILQ_INSERT_TAIL(&symhead, sym, entry);
932 return (0);
935 int
936 cmdline_symset(char *s)
938 char *sym, *val;
939 int ret;
941 val = strrchr(s, '=');
942 if (val == NULL)
943 return (-1);
945 sym = strndup(s, val - s);
946 if (sym == NULL)
947 fatal("%s: strndup", __func__);
949 ret = symset(sym, val + 1, 1);
950 free(sym);
952 return (ret);
955 char *
956 symget(const char *nam)
958 struct sym *sym;
960 TAILQ_FOREACH(sym, &symhead, entry) {
961 if (strcmp(nam, sym->nam) == 0) {
962 sym->used = 1;
963 return (sym->val);
966 return (NULL);
969 int
970 getservice(const char *n)
972 struct servent *s;
973 const char *errstr;
974 long long llval;
976 llval = strtonum(n, 0, UINT16_MAX, &errstr);
977 if (errstr) {
978 s = getservbyname(n, "tcp");
979 if (s == NULL)
980 s = getservbyname(n, "udp");
981 if (s == NULL)
982 return (-1);
983 return ntohs(s->s_port);
986 return (unsigned short)llval;
989 struct address *
990 host_v4(const char *s)
992 struct in_addr ina;
993 struct sockaddr_in *sain;
994 struct address *h;
996 memset(&ina, 0, sizeof(ina));
997 if (inet_pton(AF_INET, s, &ina) != 1)
998 return (NULL);
1000 if ((h = calloc(1, sizeof(*h))) == NULL)
1001 fatal(__func__);
1002 sain = (struct sockaddr_in *)&h->ss;
1003 got_sockaddr_inet_init(sain, &ina);
1004 if (sain->sin_addr.s_addr == INADDR_ANY)
1005 h->prefixlen = 0; /* 0.0.0.0 address */
1006 else
1007 h->prefixlen = -1; /* host address */
1008 return (h);
1011 struct address *
1012 host_v6(const char *s)
1014 struct addrinfo hints, *res;
1015 struct sockaddr_in6 *sa_in6, *ra;
1016 struct address *h = NULL;
1018 memset(&hints, 0, sizeof(hints));
1019 hints.ai_family = AF_INET6;
1020 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1021 hints.ai_flags = AI_NUMERICHOST;
1022 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1023 if ((h = calloc(1, sizeof(*h))) == NULL)
1024 fatal(__func__);
1025 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1026 ra = (struct sockaddr_in6 *)res->ai_addr;
1027 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1028 ra->sin6_scope_id);
1029 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1030 sizeof(sa_in6->sin6_addr)) == 0)
1031 h->prefixlen = 0; /* any address */
1032 else
1033 h->prefixlen = -1; /* host address */
1034 freeaddrinfo(res);
1037 return (h);
1040 int
1041 host_dns(const char *s, struct server *new_srv, int max,
1042 in_port_t port, const char *ifname, int ipproto)
1044 struct addrinfo hints, *res0, *res;
1045 int error, cnt = 0;
1046 struct sockaddr_in *sain;
1047 struct sockaddr_in6 *sin6;
1048 struct address *h;
1050 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1051 return (cnt);
1053 memset(&hints, 0, sizeof(hints));
1054 hints.ai_family = PF_UNSPEC;
1055 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1056 hints.ai_flags = AI_ADDRCONFIG;
1057 error = getaddrinfo(s, NULL, &hints, &res0);
1058 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1059 return (0);
1060 if (error) {
1061 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1062 gai_strerror(error));
1063 return (-1);
1066 for (res = res0; res && cnt < max; res = res->ai_next) {
1067 if (res->ai_family != AF_INET &&
1068 res->ai_family != AF_INET6)
1069 continue;
1070 if ((h = calloc(1, sizeof(*h))) == NULL)
1071 fatal(__func__);
1073 if (port)
1074 h->port = port;
1075 if (ifname != NULL) {
1076 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1077 sizeof(h->ifname)) {
1078 log_warnx("%s: interface name truncated",
1079 __func__);
1080 freeaddrinfo(res0);
1081 free(h);
1082 return (-1);
1085 if (ipproto != -1)
1086 h->ipproto = ipproto;
1087 h->ss.ss_family = res->ai_family;
1088 h->prefixlen = -1; /* host address */
1090 if (res->ai_family == AF_INET) {
1091 struct sockaddr_in *ra;
1092 sain = (struct sockaddr_in *)&h->ss;
1093 ra = (struct sockaddr_in *)res->ai_addr;
1094 got_sockaddr_inet_init(sain, &ra->sin_addr);
1095 } else {
1096 struct sockaddr_in6 *ra;
1097 sin6 = (struct sockaddr_in6 *)&h->ss;
1098 ra = (struct sockaddr_in6 *)res->ai_addr;
1099 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1102 if (add_addr(new_srv, h))
1103 return -1;
1104 cnt++;
1106 if (cnt == max && res) {
1107 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1108 s, max);
1110 freeaddrinfo(res0);
1111 return (cnt);
1114 int
1115 host_if(const char *s, struct server *new_srv, int max,
1116 in_port_t port, const char *ifname, int ipproto)
1118 struct ifaddrs *ifap, *p;
1119 struct sockaddr_in *sain;
1120 struct sockaddr_in6 *sin6;
1121 struct address *h;
1122 int cnt = 0, af;
1124 if (getifaddrs(&ifap) == -1)
1125 fatal("getifaddrs");
1127 /* First search for IPv4 addresses */
1128 af = AF_INET;
1130 nextaf:
1131 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1132 if (p->ifa_addr == NULL ||
1133 p->ifa_addr->sa_family != af ||
1134 (strcmp(s, p->ifa_name) != 0 &&
1135 !is_if_in_group(p->ifa_name, s)))
1136 continue;
1137 if ((h = calloc(1, sizeof(*h))) == NULL)
1138 fatal("calloc");
1140 if (port)
1141 h->port = port;
1142 if (ifname != NULL) {
1143 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1144 sizeof(h->ifname)) {
1145 log_warnx("%s: interface name truncated",
1146 __func__);
1147 free(h);
1148 freeifaddrs(ifap);
1149 return (-1);
1152 if (ipproto != -1)
1153 h->ipproto = ipproto;
1154 h->ss.ss_family = af;
1155 h->prefixlen = -1; /* host address */
1157 if (af == AF_INET) {
1158 struct sockaddr_in *ra;
1159 sain = (struct sockaddr_in *)&h->ss;
1160 ra = (struct sockaddr_in *)p->ifa_addr;
1161 got_sockaddr_inet_init(sain, &ra->sin_addr);
1162 } else {
1163 struct sockaddr_in6 *ra;
1164 sin6 = (struct sockaddr_in6 *)&h->ss;
1165 ra = (struct sockaddr_in6 *)p->ifa_addr;
1166 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1167 ra->sin6_scope_id);
1170 if (add_addr(new_srv, h))
1171 return -1;
1172 cnt++;
1174 if (af == AF_INET) {
1175 /* Next search for IPv6 addresses */
1176 af = AF_INET6;
1177 goto nextaf;
1180 if (cnt > max) {
1181 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1182 s, max);
1184 freeifaddrs(ifap);
1185 return (cnt);
1188 int
1189 host(const char *s, struct server *new_srv, int max,
1190 in_port_t port, const char *ifname, int ipproto)
1192 struct address *h;
1194 h = host_v4(s);
1196 /* IPv6 address? */
1197 if (h == NULL)
1198 h = host_v6(s);
1200 if (h != NULL) {
1201 if (port)
1202 h->port = port;
1203 if (ifname != NULL) {
1204 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1205 sizeof(h->ifname)) {
1206 log_warnx("%s: interface name truncated",
1207 __func__);
1208 free(h);
1209 return (-1);
1212 if (ipproto != -1)
1213 h->ipproto = ipproto;
1215 if (add_addr(new_srv, h))
1216 return -1;
1217 return (1);
1220 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1223 int
1224 is_if_in_group(const char *ifname, const char *groupname)
1226 /* TA: Check this... */
1227 #ifdef HAVE_STRUCT_IFGROUPREQ
1228 unsigned int len;
1229 struct ifgroupreq ifgr;
1230 struct ifg_req *ifg;
1231 int s;
1232 int ret = 0;
1234 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1235 err(1, "socket");
1237 memset(&ifgr, 0, sizeof(ifgr));
1238 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1239 err(1, "IFNAMSIZ");
1240 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1241 if (errno == EINVAL || errno == ENOTTY)
1242 goto end;
1243 err(1, "SIOCGIFGROUP");
1246 len = ifgr.ifgr_len;
1247 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1248 sizeof(struct ifg_req));
1249 if (ifgr.ifgr_groups == NULL)
1250 err(1, "getifgroups");
1251 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1252 err(1, "SIOCGIFGROUP");
1254 ifg = ifgr.ifgr_groups;
1255 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1256 len -= sizeof(struct ifg_req);
1257 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1258 ret = 1;
1259 break;
1262 free(ifgr.ifgr_groups);
1264 end:
1265 close(s);
1266 return (ret);
1267 #else
1268 return (0);
1269 #endif
1272 int
1273 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1275 if (strcmp("", addr) == 0) {
1276 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1277 -1) <= 0) {
1278 yyerror("invalid listen ip: %s",
1279 "127.0.0.1");
1280 return (-1);
1282 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1283 yyerror("invalid listen ip: %s", "::1");
1284 return (-1);
1286 } else {
1287 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1288 -1) <= 0) {
1289 yyerror("invalid listen ip: %s", addr);
1290 return (-1);
1293 return (0);
1296 int
1297 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1298 const char *other_srv)
1300 struct address *a;
1301 void *ia;
1302 char buf[INET6_ADDRSTRLEN];
1303 const char *addrstr;
1305 TAILQ_FOREACH(a, al, entry) {
1306 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1307 a->port != h->port)
1308 continue;
1310 switch (h->ss.ss_family) {
1311 case AF_INET:
1312 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1313 break;
1314 case AF_INET6:
1315 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1316 break;
1317 default:
1318 yyerror("unknown address family: %d", h->ss.ss_family);
1319 return -1;
1321 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1322 if (addrstr) {
1323 if (other_srv) {
1324 yyerror("server %s: duplicate fcgi listen "
1325 "address %s:%d, already used by server %s",
1326 new_srv, addrstr, h->port, other_srv);
1327 } else {
1328 log_warnx("server: %s: duplicate fcgi listen "
1329 "address %s:%d", new_srv, addrstr, h->port);
1331 } else {
1332 if (other_srv) {
1333 yyerror("server: %s: duplicate fcgi listen "
1334 "address, already used by server %s",
1335 new_srv, other_srv);
1336 } else {
1337 log_warnx("server %s: duplicate fcgi listen "
1338 "address", new_srv);
1342 return -1;
1345 return 0;
1348 int
1349 add_addr(struct server *new_srv, struct address *h)
1351 struct server *srv;
1353 /* Address cannot be shared between different servers. */
1354 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1355 if (srv == new_srv)
1356 continue;
1357 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1358 return -1;
1361 /* Tolerate duplicate address lines within the scope of a server. */
1362 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1363 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1364 else
1365 free(h);
1367 return 0;