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 RESPECT_EXPORTOK
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 | RESPECT_EXPORTOK boolean {
362 new_srv->respect_exportok = $2;
364 | MAX_REPOS_DISPLAY NUMBER {
365 new_srv->max_repos_display = $2;
367 | MAX_COMMITS_DISPLAY NUMBER {
368 if ($2 > 0)
369 new_srv->max_commits_display = $2;
371 | UNIX_SOCKET boolean {
372 new_srv->unix_socket = $2;
374 | UNIX_SOCKET_NAME STRING {
375 n = snprintf(new_srv->unix_socket_name,
376 sizeof(new_srv->unix_socket_name), "%s%s",
377 strlen(gotwebd->httpd_chroot) ?
378 gotwebd->httpd_chroot : D_HTTPD_CHROOT, $2);
379 if (n < 0 ||
380 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
381 yyerror("%s: unix_socket_name truncated",
382 __func__);
383 free($2);
384 YYERROR;
386 free($2);
390 serveropts2 : serveropts2 serveropts1 nl
391 | serveropts1 optnl
394 nl : '\n' optnl
397 optnl : '\n' optnl /* zero or more newlines */
398 | /* empty */
401 %%
403 struct keywords {
404 const char *k_name;
405 int k_val;
406 };
408 int
409 yyerror(const char *fmt, ...)
411 va_list ap;
412 char *msg;
414 file->errors++;
415 va_start(ap, fmt);
416 if (vasprintf(&msg, fmt, ap) == -1)
417 fatalx("yyerror vasprintf");
418 va_end(ap);
419 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
420 free(msg);
421 return (0);
424 int
425 kw_cmp(const void *k, const void *e)
427 return (strcmp(k, ((const struct keywords *)e)->k_name));
430 int
431 lookup(char *s)
433 /* This has to be sorted always. */
434 static const struct keywords keywords[] = {
435 { "chroot", CHROOT },
436 { "custom_css", CUSTOM_CSS },
437 { "listen", LISTEN },
438 { "logo", LOGO },
439 { "logo_url" , LOGO_URL },
440 { "max_commits_display", MAX_COMMITS_DISPLAY },
441 { "max_repos", MAX_REPOS },
442 { "max_repos_display", MAX_REPOS_DISPLAY },
443 { "on", ON },
444 { "port", PORT },
445 { "prefork", PREFORK },
446 { "repos_path", REPOS_PATH },
447 { "respect_exportok", RESPECT_EXPORTOK },
448 { "server", SERVER },
449 { "show_repo_age", SHOW_REPO_AGE },
450 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
451 { "show_repo_description", SHOW_REPO_DESCRIPTION },
452 { "show_repo_owner", SHOW_REPO_OWNER },
453 { "show_site_owner", SHOW_SITE_OWNER },
454 { "site_link", SITE_LINK },
455 { "site_name", SITE_NAME },
456 { "site_owner", SITE_OWNER },
457 { "unix_socket", UNIX_SOCKET },
458 { "unix_socket_name", UNIX_SOCKET_NAME },
459 };
460 const struct keywords *p;
462 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
463 sizeof(keywords[0]), kw_cmp);
465 if (p)
466 return (p->k_val);
467 else
468 return (STRING);
471 #define MAXPUSHBACK 128
473 unsigned char *parsebuf;
474 int parseindex;
475 unsigned char pushback_buffer[MAXPUSHBACK];
476 int pushback_index = 0;
478 int
479 lgetc(int quotec)
481 int c, next;
483 if (parsebuf) {
484 /* Read character from the parsebuffer instead of input. */
485 if (parseindex >= 0) {
486 c = parsebuf[parseindex++];
487 if (c != '\0')
488 return (c);
489 parsebuf = NULL;
490 } else
491 parseindex++;
494 if (pushback_index)
495 return (pushback_buffer[--pushback_index]);
497 if (quotec) {
498 c = getc(file->stream);
499 if (c == EOF)
500 yyerror("reached end of file while parsing "
501 "quoted string");
502 return (c);
505 c = getc(file->stream);
506 while (c == '\\') {
507 next = getc(file->stream);
508 if (next != '\n') {
509 c = next;
510 break;
512 yylval.lineno = file->lineno;
513 file->lineno++;
514 c = getc(file->stream);
517 return (c);
520 int
521 lungetc(int c)
523 if (c == EOF)
524 return (EOF);
525 if (parsebuf) {
526 parseindex--;
527 if (parseindex >= 0)
528 return (c);
530 if (pushback_index < MAXPUSHBACK-1)
531 return (pushback_buffer[pushback_index++] = c);
532 else
533 return (EOF);
536 int
537 findeol(void)
539 int c;
541 parsebuf = NULL;
543 /* Skip to either EOF or the first real EOL. */
544 while (1) {
545 if (pushback_index)
546 c = pushback_buffer[--pushback_index];
547 else
548 c = lgetc(0);
549 if (c == '\n') {
550 file->lineno++;
551 break;
553 if (c == EOF)
554 break;
556 return (ERROR);
559 int
560 yylex(void)
562 unsigned char buf[8096];
563 unsigned char *p, *val;
564 int quotec, next, c;
565 int token;
567 top:
568 p = buf;
569 c = lgetc(0);
570 while (c == ' ' || c == '\t')
571 c = lgetc(0); /* nothing */
573 yylval.lineno = file->lineno;
574 if (c == '#') {
575 c = lgetc(0);
576 while (c != '\n' && c != EOF)
577 c = lgetc(0); /* nothing */
579 if (c == '$' && parsebuf == NULL) {
580 while (1) {
581 c = lgetc(0);
582 if (c == EOF)
583 return (0);
585 if (p + 1 >= buf + sizeof(buf) - 1) {
586 yyerror("string too long");
587 return (findeol());
589 if (isalnum(c) || c == '_') {
590 *p++ = c;
591 continue;
593 *p = '\0';
594 lungetc(c);
595 break;
597 val = symget(buf);
598 if (val == NULL) {
599 yyerror("macro '%s' not defined", buf);
600 return (findeol());
602 parsebuf = val;
603 parseindex = 0;
604 goto top;
607 switch (c) {
608 case '\'':
609 case '"':
610 quotec = c;
611 while (1) {
612 c = lgetc(quotec);
613 if (c == EOF)
614 return (0);
615 if (c == '\n') {
616 file->lineno++;
617 continue;
618 } else if (c == '\\') {
619 next = lgetc(quotec);
620 if (next == EOF)
621 return (0);
622 if (next == quotec || c == ' ' || c == '\t')
623 c = next;
624 else if (next == '\n') {
625 file->lineno++;
626 continue;
627 } else
628 lungetc(next);
629 } else if (c == quotec) {
630 *p = '\0';
631 break;
632 } else if (c == '\0') {
633 yyerror("syntax error");
634 return (findeol());
636 if (p + 1 >= buf + sizeof(buf) - 1) {
637 yyerror("string too long");
638 return (findeol());
640 *p++ = c;
642 yylval.v.string = strdup(buf);
643 if (yylval.v.string == NULL)
644 err(1, "yylex: strdup");
645 return (STRING);
648 #define allowed_to_end_number(x) \
649 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
651 if (c == '-' || isdigit(c)) {
652 do {
653 *p++ = c;
654 if ((unsigned)(p-buf) >= sizeof(buf)) {
655 yyerror("string too long");
656 return (findeol());
658 c = lgetc(0);
659 } while (c != EOF && isdigit(c));
660 lungetc(c);
661 if (p == buf + 1 && buf[0] == '-')
662 goto nodigits;
663 if (c == EOF || allowed_to_end_number(c)) {
664 const char *errstr = NULL;
666 *p = '\0';
667 yylval.v.number = strtonum(buf, LLONG_MIN,
668 LLONG_MAX, &errstr);
669 if (errstr) {
670 yyerror("\"%s\" invalid number: %s",
671 buf, errstr);
672 return (findeol());
674 return (NUMBER);
675 } else {
676 nodigits:
677 while (p > buf + 1)
678 lungetc(*--p);
679 c = *--p;
680 if (c == '-')
681 return (c);
685 #define allowed_in_string(x) \
686 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
687 x != '{' && x != '}' && \
688 x != '!' && x != '=' && x != '#' && \
689 x != ','))
691 if (isalnum(c) || c == ':' || c == '_') {
692 do {
693 *p++ = c;
694 if ((unsigned)(p-buf) >= sizeof(buf)) {
695 yyerror("string too long");
696 return (findeol());
698 c = lgetc(0);
699 } while (c != EOF && (allowed_in_string(c)));
700 lungetc(c);
701 *p = '\0';
702 token = lookup(buf);
703 if (token == STRING) {
704 yylval.v.string = strdup(buf);
705 if (yylval.v.string == NULL)
706 err(1, "yylex: strdup");
708 return (token);
710 if (c == '\n') {
711 yylval.lineno = file->lineno;
712 file->lineno++;
714 if (c == EOF)
715 return (0);
716 return (c);
719 int
720 check_file_secrecy(int fd, const char *fname)
722 struct stat st;
724 if (fstat(fd, &st)) {
725 log_warn("cannot stat %s", fname);
726 return (-1);
728 if (st.st_uid != 0 && st.st_uid != getuid()) {
729 log_warnx("%s: owner not root or current user", fname);
730 return (-1);
732 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
733 log_warnx("%s: group writable or world read/writable", fname);
734 return (-1);
736 return (0);
739 struct file *
740 newfile(const char *name, int secret)
742 struct file *nfile;
744 nfile = calloc(1, sizeof(struct file));
745 if (nfile == NULL) {
746 log_warn("calloc");
747 return (NULL);
749 nfile->name = strdup(name);
750 if (nfile->name == NULL) {
751 log_warn("strdup");
752 free(nfile);
753 return (NULL);
755 nfile->stream = fopen(nfile->name, "r");
756 if (nfile->stream == NULL) {
757 /* no warning, we don't require a conf file */
758 free(nfile->name);
759 free(nfile);
760 return (NULL);
761 } else if (secret &&
762 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
763 fclose(nfile->stream);
764 free(nfile->name);
765 free(nfile);
766 return (NULL);
768 nfile->lineno = 1;
769 return (nfile);
772 static void
773 closefile(struct file *xfile)
775 fclose(xfile->stream);
776 free(xfile->name);
777 free(xfile);
780 static void
781 add_default_server(void)
783 new_srv = conf_new_server(D_SITENAME);
784 log_debug("%s: adding default server %s", __func__, D_SITENAME);
787 int
788 parse_config(const char *filename, struct gotwebd *env)
790 struct sym *sym, *next;
792 if (config_init(env) == -1)
793 fatalx("failed to initialize configuration");
795 gotwebd = env;
797 file = newfile(filename, 0);
798 if (file == NULL) {
799 add_default_server();
800 sockets_parse_sockets(env);
801 /* just return, as we don't require a conf file */
802 return (0);
805 yyparse();
806 errors = file->errors;
807 closefile(file);
809 /* Free macros and check which have not been used. */
810 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
811 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
812 fprintf(stderr, "warning: macro '%s' not used\n",
813 sym->nam);
814 if (!sym->persist) {
815 free(sym->nam);
816 free(sym->val);
817 TAILQ_REMOVE(&symhead, sym, entry);
818 free(sym);
822 if (errors)
823 return (-1);
825 /* just add default server if no config specified */
826 if (gotwebd->server_cnt == 0)
827 add_default_server();
829 /* setup our listening sockets */
830 sockets_parse_sockets(env);
832 return (0);
835 struct server *
836 conf_new_server(const char *name)
838 struct server *srv = NULL;
840 srv = calloc(1, sizeof(*srv));
841 if (srv == NULL)
842 fatalx("%s: calloc", __func__);
844 n = strlcpy(srv->name, name, sizeof(srv->name));
845 if (n >= sizeof(srv->name))
846 fatalx("%s: strlcpy", __func__);
847 n = snprintf(srv->unix_socket_name,
848 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
849 D_UNIX_SOCKET);
850 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
851 fatalx("%s: snprintf", __func__);
852 n = strlcpy(srv->repos_path, D_GOTPATH,
853 sizeof(srv->repos_path));
854 if (n >= sizeof(srv->repos_path))
855 fatalx("%s: strlcpy", __func__);
856 n = strlcpy(srv->site_name, D_SITENAME,
857 sizeof(srv->site_name));
858 if (n >= sizeof(srv->site_name))
859 fatalx("%s: strlcpy", __func__);
860 n = strlcpy(srv->site_owner, D_SITEOWNER,
861 sizeof(srv->site_owner));
862 if (n >= sizeof(srv->site_owner))
863 fatalx("%s: strlcpy", __func__);
864 n = strlcpy(srv->site_link, D_SITELINK,
865 sizeof(srv->site_link));
866 if (n >= sizeof(srv->site_link))
867 fatalx("%s: strlcpy", __func__);
868 n = strlcpy(srv->logo, D_GOTLOGO,
869 sizeof(srv->logo));
870 if (n >= sizeof(srv->logo))
871 fatalx("%s: strlcpy", __func__);
872 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
873 if (n >= sizeof(srv->logo_url))
874 fatalx("%s: strlcpy", __func__);
875 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
876 if (n >= sizeof(srv->custom_css))
877 fatalx("%s: strlcpy", __func__);
879 srv->show_site_owner = D_SHOWSOWNER;
880 srv->show_repo_owner = D_SHOWROWNER;
881 srv->show_repo_age = D_SHOWAGE;
882 srv->show_repo_description = D_SHOWDESC;
883 srv->show_repo_cloneurl = D_SHOWURL;
884 srv->respect_exportok = D_RESPECTEXPORTOK;
886 srv->max_repos_display = D_MAXREPODISP;
887 srv->max_commits_display = D_MAXCOMMITDISP;
888 srv->max_repos = D_MAXREPO;
890 srv->unix_socket = 1;
891 srv->fcgi_socket = 0;
893 TAILQ_INIT(&srv->al);
894 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
895 gotwebd->server_cnt++;
897 return srv;
898 };
900 int
901 symset(const char *nam, const char *val, int persist)
903 struct sym *sym;
905 TAILQ_FOREACH(sym, &symhead, entry) {
906 if (strcmp(nam, sym->nam) == 0)
907 break;
910 if (sym != NULL) {
911 if (sym->persist == 1)
912 return (0);
913 else {
914 free(sym->nam);
915 free(sym->val);
916 TAILQ_REMOVE(&symhead, sym, entry);
917 free(sym);
920 sym = calloc(1, sizeof(*sym));
921 if (sym == NULL)
922 return (-1);
924 sym->nam = strdup(nam);
925 if (sym->nam == NULL) {
926 free(sym);
927 return (-1);
929 sym->val = strdup(val);
930 if (sym->val == NULL) {
931 free(sym->nam);
932 free(sym);
933 return (-1);
935 sym->used = 0;
936 sym->persist = persist;
937 TAILQ_INSERT_TAIL(&symhead, sym, entry);
938 return (0);
941 int
942 cmdline_symset(char *s)
944 char *sym, *val;
945 int ret;
947 val = strrchr(s, '=');
948 if (val == NULL)
949 return (-1);
951 sym = strndup(s, val - s);
952 if (sym == NULL)
953 fatal("%s: strndup", __func__);
955 ret = symset(sym, val + 1, 1);
956 free(sym);
958 return (ret);
961 char *
962 symget(const char *nam)
964 struct sym *sym;
966 TAILQ_FOREACH(sym, &symhead, entry) {
967 if (strcmp(nam, sym->nam) == 0) {
968 sym->used = 1;
969 return (sym->val);
972 return (NULL);
975 int
976 getservice(const char *n)
978 struct servent *s;
979 const char *errstr;
980 long long llval;
982 llval = strtonum(n, 0, UINT16_MAX, &errstr);
983 if (errstr) {
984 s = getservbyname(n, "tcp");
985 if (s == NULL)
986 s = getservbyname(n, "udp");
987 if (s == NULL)
988 return (-1);
989 return ntohs(s->s_port);
992 return (unsigned short)llval;
995 struct address *
996 host_v4(const char *s)
998 struct in_addr ina;
999 struct sockaddr_in *sain;
1000 struct address *h;
1002 memset(&ina, 0, sizeof(ina));
1003 if (inet_pton(AF_INET, s, &ina) != 1)
1004 return (NULL);
1006 if ((h = calloc(1, sizeof(*h))) == NULL)
1007 fatal(__func__);
1008 sain = (struct sockaddr_in *)&h->ss;
1009 got_sockaddr_inet_init(sain, &ina);
1010 if (sain->sin_addr.s_addr == INADDR_ANY)
1011 h->prefixlen = 0; /* 0.0.0.0 address */
1012 else
1013 h->prefixlen = -1; /* host address */
1014 return (h);
1017 struct address *
1018 host_v6(const char *s)
1020 struct addrinfo hints, *res;
1021 struct sockaddr_in6 *sa_in6, *ra;
1022 struct address *h = NULL;
1024 memset(&hints, 0, sizeof(hints));
1025 hints.ai_family = AF_INET6;
1026 hints.ai_socktype = SOCK_DGRAM; /* dummy */
1027 hints.ai_flags = AI_NUMERICHOST;
1028 if (getaddrinfo(s, "0", &hints, &res) == 0) {
1029 if ((h = calloc(1, sizeof(*h))) == NULL)
1030 fatal(__func__);
1031 sa_in6 = (struct sockaddr_in6 *)&h->ss;
1032 ra = (struct sockaddr_in6 *)res->ai_addr;
1033 got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
1034 ra->sin6_scope_id);
1035 if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
1036 sizeof(sa_in6->sin6_addr)) == 0)
1037 h->prefixlen = 0; /* any address */
1038 else
1039 h->prefixlen = -1; /* host address */
1040 freeaddrinfo(res);
1043 return (h);
1046 int
1047 host_dns(const char *s, struct server *new_srv, int max,
1048 in_port_t port, const char *ifname, int ipproto)
1050 struct addrinfo hints, *res0, *res;
1051 int error, cnt = 0;
1052 struct sockaddr_in *sain;
1053 struct sockaddr_in6 *sin6;
1054 struct address *h;
1056 if ((cnt = host_if(s, new_srv, max, port, ifname, ipproto)) != 0)
1057 return (cnt);
1059 memset(&hints, 0, sizeof(hints));
1060 hints.ai_family = PF_UNSPEC;
1061 hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
1062 hints.ai_flags = AI_ADDRCONFIG;
1063 error = getaddrinfo(s, NULL, &hints, &res0);
1064 if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
1065 return (0);
1066 if (error) {
1067 log_warnx("%s: could not parse \"%s\": %s", __func__, s,
1068 gai_strerror(error));
1069 return (-1);
1072 for (res = res0; res && cnt < max; res = res->ai_next) {
1073 if (res->ai_family != AF_INET &&
1074 res->ai_family != AF_INET6)
1075 continue;
1076 if ((h = calloc(1, sizeof(*h))) == NULL)
1077 fatal(__func__);
1079 if (port)
1080 h->port = port;
1081 if (ifname != NULL) {
1082 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1083 sizeof(h->ifname)) {
1084 log_warnx("%s: interface name truncated",
1085 __func__);
1086 freeaddrinfo(res0);
1087 free(h);
1088 return (-1);
1091 if (ipproto != -1)
1092 h->ipproto = ipproto;
1093 h->ss.ss_family = res->ai_family;
1094 h->prefixlen = -1; /* host address */
1096 if (res->ai_family == AF_INET) {
1097 struct sockaddr_in *ra;
1098 sain = (struct sockaddr_in *)&h->ss;
1099 ra = (struct sockaddr_in *)res->ai_addr;
1100 got_sockaddr_inet_init(sain, &ra->sin_addr);
1101 } else {
1102 struct sockaddr_in6 *ra;
1103 sin6 = (struct sockaddr_in6 *)&h->ss;
1104 ra = (struct sockaddr_in6 *)res->ai_addr;
1105 got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
1108 if (add_addr(new_srv, h))
1109 return -1;
1110 cnt++;
1112 if (cnt == max && res) {
1113 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1114 s, max);
1116 freeaddrinfo(res0);
1117 return (cnt);
1120 int
1121 host_if(const char *s, struct server *new_srv, int max,
1122 in_port_t port, const char *ifname, int ipproto)
1124 struct ifaddrs *ifap, *p;
1125 struct sockaddr_in *sain;
1126 struct sockaddr_in6 *sin6;
1127 struct address *h;
1128 int cnt = 0, af;
1130 if (getifaddrs(&ifap) == -1)
1131 fatal("getifaddrs");
1133 /* First search for IPv4 addresses */
1134 af = AF_INET;
1136 nextaf:
1137 for (p = ifap; p != NULL && cnt < max; p = p->ifa_next) {
1138 if (p->ifa_addr == NULL ||
1139 p->ifa_addr->sa_family != af ||
1140 (strcmp(s, p->ifa_name) != 0 &&
1141 !is_if_in_group(p->ifa_name, s)))
1142 continue;
1143 if ((h = calloc(1, sizeof(*h))) == NULL)
1144 fatal("calloc");
1146 if (port)
1147 h->port = port;
1148 if (ifname != NULL) {
1149 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1150 sizeof(h->ifname)) {
1151 log_warnx("%s: interface name truncated",
1152 __func__);
1153 free(h);
1154 freeifaddrs(ifap);
1155 return (-1);
1158 if (ipproto != -1)
1159 h->ipproto = ipproto;
1160 h->ss.ss_family = af;
1161 h->prefixlen = -1; /* host address */
1163 if (af == AF_INET) {
1164 struct sockaddr_in *ra;
1165 sain = (struct sockaddr_in *)&h->ss;
1166 ra = (struct sockaddr_in *)p->ifa_addr;
1167 got_sockaddr_inet_init(sain, &ra->sin_addr);
1168 } else {
1169 struct sockaddr_in6 *ra;
1170 sin6 = (struct sockaddr_in6 *)&h->ss;
1171 ra = (struct sockaddr_in6 *)p->ifa_addr;
1172 got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
1173 ra->sin6_scope_id);
1176 if (add_addr(new_srv, h))
1177 return -1;
1178 cnt++;
1180 if (af == AF_INET) {
1181 /* Next search for IPv6 addresses */
1182 af = AF_INET6;
1183 goto nextaf;
1186 if (cnt > max) {
1187 log_warnx("%s: %s resolves to more than %d hosts", __func__,
1188 s, max);
1190 freeifaddrs(ifap);
1191 return (cnt);
1194 int
1195 host(const char *s, struct server *new_srv, int max,
1196 in_port_t port, const char *ifname, int ipproto)
1198 struct address *h;
1200 h = host_v4(s);
1202 /* IPv6 address? */
1203 if (h == NULL)
1204 h = host_v6(s);
1206 if (h != NULL) {
1207 if (port)
1208 h->port = port;
1209 if (ifname != NULL) {
1210 if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >=
1211 sizeof(h->ifname)) {
1212 log_warnx("%s: interface name truncated",
1213 __func__);
1214 free(h);
1215 return (-1);
1218 if (ipproto != -1)
1219 h->ipproto = ipproto;
1221 if (add_addr(new_srv, h))
1222 return -1;
1223 return (1);
1226 return (host_dns(s, new_srv, max, port, ifname, ipproto));
1229 int
1230 is_if_in_group(const char *ifname, const char *groupname)
1232 unsigned int len;
1233 struct ifgroupreq ifgr;
1234 struct ifg_req *ifg;
1235 int s;
1236 int ret = 0;
1238 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
1239 err(1, "socket");
1241 memset(&ifgr, 0, sizeof(ifgr));
1242 if (strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ) >= IFNAMSIZ)
1243 err(1, "IFNAMSIZ");
1244 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
1245 if (errno == EINVAL || errno == ENOTTY)
1246 goto end;
1247 err(1, "SIOCGIFGROUP");
1250 len = ifgr.ifgr_len;
1251 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
1252 sizeof(struct ifg_req));
1253 if (ifgr.ifgr_groups == NULL)
1254 err(1, "getifgroups");
1255 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
1256 err(1, "SIOCGIFGROUP");
1258 ifg = ifgr.ifgr_groups;
1259 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
1260 len -= sizeof(struct ifg_req);
1261 if (strcmp(ifg->ifgrq_group, groupname) == 0) {
1262 ret = 1;
1263 break;
1266 free(ifgr.ifgr_groups);
1268 end:
1269 close(s);
1270 return (ret);
1273 int
1274 get_addrs(const char *addr, struct server *new_srv, in_port_t port)
1276 if (strcmp("", addr) == 0) {
1277 if (host("127.0.0.1", new_srv, 1, port, "127.0.0.1",
1278 -1) <= 0) {
1279 yyerror("invalid listen ip: %s",
1280 "127.0.0.1");
1281 return (-1);
1283 if (host("::1", new_srv, 1, port, "::1", -1) <= 0) {
1284 yyerror("invalid listen ip: %s", "::1");
1285 return (-1);
1287 } else {
1288 if (host(addr, new_srv, GOTWEBD_MAXIFACE, port, addr,
1289 -1) <= 0) {
1290 yyerror("invalid listen ip: %s", addr);
1291 return (-1);
1294 return (0);
1297 int
1298 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1299 const char *other_srv)
1301 struct address *a;
1302 void *ia;
1303 char buf[INET6_ADDRSTRLEN];
1304 const char *addrstr;
1306 TAILQ_FOREACH(a, al, entry) {
1307 if (memcmp(&a->ss, &h->ss, sizeof(h->ss)) != 0 ||
1308 a->port != h->port)
1309 continue;
1311 switch (h->ss.ss_family) {
1312 case AF_INET:
1313 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1314 break;
1315 case AF_INET6:
1316 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1317 break;
1318 default:
1319 yyerror("unknown address family: %d", h->ss.ss_family);
1320 return -1;
1322 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1323 if (addrstr) {
1324 if (other_srv) {
1325 yyerror("server %s: duplicate fcgi listen "
1326 "address %s:%d, already used by server %s",
1327 new_srv, addrstr, h->port, other_srv);
1328 } else {
1329 log_warnx("server: %s: duplicate fcgi listen "
1330 "address %s:%d", new_srv, addrstr, h->port);
1332 } else {
1333 if (other_srv) {
1334 yyerror("server: %s: duplicate fcgi listen "
1335 "address, already used by server %s",
1336 new_srv, other_srv);
1337 } else {
1338 log_warnx("server %s: duplicate fcgi listen "
1339 "address", new_srv);
1343 return -1;
1346 return 0;
1349 int
1350 add_addr(struct server *new_srv, struct address *h)
1352 struct server *srv;
1354 /* Address cannot be shared between different servers. */
1355 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1356 if (srv == new_srv)
1357 continue;
1358 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1359 return -1;
1362 /* Tolerate duplicate address lines within the scope of a server. */
1363 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1364 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1365 else
1366 free(h);
1368 return 0;