regress: use seq instead of jot
[got-portable.git] / gotwebd / parse.y
blob93979865f4ad834239ef20b16f08a34de6220396
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.
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.
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.
24 #include "got_compat.h"
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/queue.h>
29 #include <sys/socket.h>
30 #include <sys/stat.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <event.h>
41 #include <ifaddrs.h>
42 #include <limits.h>
43 #include <netdb.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <unistd.h>
51 #include "got_reference.h"
53 #include "gotwebd.h"
55 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 static struct file {
57 TAILQ_ENTRY(file) entry;
58 FILE *stream;
59 char *name;
60 int lineno;
61 int errors;
62 } *file;
63 struct file *newfile(const char *, int);
64 static void closefile(struct file *);
65 int check_file_secrecy(int, const char *);
66 int yyparse(void);
67 int yylex(void);
68 int yyerror(const char *, ...)
69 __attribute__((__format__ (printf, 1, 2)))
70 __attribute__((__nonnull__ (1)));
71 int kw_cmp(const void *, const void *);
72 int lookup(char *);
73 int lgetc(int);
74 int lungetc(int);
75 int findeol(void);
77 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 struct sym {
79 TAILQ_ENTRY(sym) entry;
80 int used;
81 int persist;
82 char *nam;
83 char *val;
86 int symset(const char *, const char *, int);
87 char *symget(const char *);
89 static int errors;
91 static struct gotwebd *gotwebd;
92 static struct server *new_srv;
93 static struct server *conf_new_server(const char *);
94 int getservice(const char *);
95 int n;
97 int get_addrs(const char *, const char *, struct server *);
98 int addr_dup_check(struct addresslist *, struct address *,
99 const char *, const char *);
100 int add_addr(struct server *, struct address *);
102 typedef struct {
103 union {
104 long long number;
105 char *string;
106 } v;
107 int lineno;
108 } YYSTYPE;
112 %token LISTEN WWW_PATH SITE_NAME SITE_OWNER SITE_LINK LOGO
113 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
114 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
115 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
116 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
117 %token SUMMARY_COMMITS_DISPLAY SUMMARY_TAGS_DISPLAY
119 %token <v.string> STRING
120 %token <v.number> NUMBER
121 %type <v.number> boolean
122 %type <v.string> listen_addr
126 grammar : /* empty */
127 | grammar '\n'
128 | grammar varset '\n'
129 | grammar main '\n'
130 | grammar server '\n'
131 | grammar error '\n' { file->errors++; }
134 varset : STRING '=' STRING {
135 char *s = $1;
136 while (*s++) {
137 if (isspace((unsigned char)*s)) {
138 yyerror("macro name cannot contain "
139 "whitespace");
140 free($1);
141 free($3);
142 YYERROR;
145 if (symset($1, $3, 0) == -1)
146 fatal("cannot store variable");
147 free($1);
148 free($3);
152 boolean : STRING {
153 if (strcasecmp($1, "1") == 0 ||
154 strcasecmp($1, "on") == 0)
155 $$ = 1;
156 else if (strcasecmp($1, "0") == 0 ||
157 strcasecmp($1, "off") == 0)
158 $$ = 0;
159 else {
160 yyerror("invalid boolean value '%s'", $1);
161 free($1);
162 YYERROR;
164 free($1);
166 | ON { $$ = 1; }
167 | NUMBER {
168 if ($1 != 0 && $1 != 1) {
169 yyerror("invalid boolean value '%lld'", $1);
170 YYERROR;
172 $$ = $1;
176 listen_addr : '*' { $$ = NULL; }
177 | STRING
180 main : PREFORK NUMBER {
181 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
182 yyerror("prefork is %s: %lld",
183 $2 <= 0 ? "too small" : "too large", $2);
184 YYERROR;
186 gotwebd->prefork_gotwebd = $2;
188 | CHROOT STRING {
189 if (*$2 == '\0') {
190 yyerror("chroot path can't be an empty"
191 " string");
192 free($2);
193 YYERROR;
196 n = strlcpy(gotwebd->httpd_chroot, $2,
197 sizeof(gotwebd->httpd_chroot));
198 if (n >= sizeof(gotwebd->httpd_chroot)) {
199 yyerror("%s: httpd_chroot truncated", __func__);
200 free($2);
201 YYERROR;
203 free($2);
205 | UNIX_SOCKET boolean {
206 gotwebd->unix_socket = $2;
208 | UNIX_SOCKET_NAME STRING {
209 n = snprintf(gotwebd->unix_socket_name,
210 sizeof(gotwebd->unix_socket_name), "%s%s",
211 gotwebd->httpd_chroot, $2);
212 if (n < 0 ||
213 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
214 yyerror("%s: unix_socket_name truncated",
215 __func__);
216 free($2);
217 YYERROR;
219 free($2);
223 server : SERVER STRING {
224 struct server *srv;
226 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
227 if (strcmp(srv->name, $2) == 0) {
228 yyerror("server name exists '%s'", $2);
229 free($2);
230 YYERROR;
234 new_srv = conf_new_server($2);
235 log_debug("adding server %s", $2);
236 free($2);
238 | SERVER STRING {
239 struct server *srv;
241 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
242 if (strcmp(srv->name, $2) == 0) {
243 yyerror("server name exists '%s'", $2);
244 free($2);
245 YYERROR;
249 new_srv = conf_new_server($2);
250 log_debug("adding server %s", $2);
251 free($2);
252 } '{' optnl serveropts2 '}' {
256 serveropts1 : REPOS_PATH STRING {
257 n = strlcpy(new_srv->repos_path, $2,
258 sizeof(new_srv->repos_path));
259 if (n >= sizeof(new_srv->repos_path)) {
260 yyerror("%s: repos_path truncated", __func__);
261 free($2);
262 YYERROR;
264 free($2);
266 | SITE_NAME STRING {
267 n = strlcpy(new_srv->site_name, $2,
268 sizeof(new_srv->site_name));
269 if (n >= sizeof(new_srv->site_name)) {
270 yyerror("%s: site_name truncated", __func__);
271 free($2);
272 YYERROR;
274 free($2);
276 | SITE_OWNER STRING {
277 n = strlcpy(new_srv->site_owner, $2,
278 sizeof(new_srv->site_owner));
279 if (n >= sizeof(new_srv->site_owner)) {
280 yyerror("%s: site_owner truncated", __func__);
281 free($2);
282 YYERROR;
284 free($2);
286 | SITE_LINK STRING {
287 n = strlcpy(new_srv->site_link, $2,
288 sizeof(new_srv->site_link));
289 if (n >= sizeof(new_srv->site_link)) {
290 yyerror("%s: site_link truncated", __func__);
291 free($2);
292 YYERROR;
294 free($2);
296 | LOGO STRING {
297 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
298 if (n >= sizeof(new_srv->logo)) {
299 yyerror("%s: logo truncated", __func__);
300 free($2);
301 YYERROR;
303 free($2);
305 | LOGO_URL STRING {
306 n = strlcpy(new_srv->logo_url, $2,
307 sizeof(new_srv->logo_url));
308 if (n >= sizeof(new_srv->logo_url)) {
309 yyerror("%s: logo_url truncated", __func__);
310 free($2);
311 YYERROR;
313 free($2);
315 | CUSTOM_CSS STRING {
316 n = strlcpy(new_srv->custom_css, $2,
317 sizeof(new_srv->custom_css));
318 if (n >= sizeof(new_srv->custom_css)) {
319 yyerror("%s: custom_css truncated", __func__);
320 free($2);
321 YYERROR;
323 free($2);
325 | LISTEN ON listen_addr PORT STRING {
326 if (get_addrs($3, $5, new_srv) == -1) {
327 yyerror("could not get addrs");
328 YYERROR;
330 free($3);
331 free($5);
332 new_srv->fcgi_socket = 1;
334 | LISTEN ON listen_addr PORT NUMBER {
335 char portno[32];
336 int n;
338 n = snprintf(portno, sizeof(portno), "%lld",
339 (long long)$5);
340 if (n < 0 || (size_t)n >= sizeof(portno))
341 fatalx("port number too long: %lld",
342 (long long)$5);
344 if (get_addrs($3, portno, new_srv) == -1) {
345 yyerror("could not get addrs");
346 YYERROR;
348 free($3);
349 new_srv->fcgi_socket = 1;
351 | LISTEN ON SOCKET STRING {
352 if (strcasecmp($4, "off") == 0) {
353 new_srv->unix_socket = 0;
354 free($4);
355 YYACCEPT;
358 new_srv->unix_socket = 1;
360 n = snprintf(new_srv->unix_socket_name,
361 sizeof(new_srv->unix_socket_name), "%s%s",
362 gotwebd->httpd_chroot, $4);
363 if (n < 0 ||
364 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
365 yyerror("%s: unix_socket_name truncated",
366 __func__);
367 free($4);
368 YYERROR;
370 free($4);
372 | SHOW_SITE_OWNER boolean {
373 new_srv->show_site_owner = $2;
375 | SHOW_REPO_OWNER boolean {
376 new_srv->show_repo_owner = $2;
378 | SHOW_REPO_AGE boolean {
379 new_srv->show_repo_age = $2;
381 | SHOW_REPO_DESCRIPTION boolean {
382 new_srv->show_repo_description = $2;
384 | SHOW_REPO_CLONEURL boolean {
385 new_srv->show_repo_cloneurl = $2;
387 | RESPECT_EXPORTOK boolean {
388 new_srv->respect_exportok = $2;
390 | MAX_REPOS_DISPLAY NUMBER {
391 if ($2 < 0) {
392 yyerror("max_repos_display is too small: %lld",
393 $2);
394 YYERROR;
396 new_srv->max_repos_display = $2;
398 | MAX_COMMITS_DISPLAY NUMBER {
399 if ($2 <= 1) {
400 yyerror("max_commits_display is too small:"
401 " %lld", $2);
402 YYERROR;
404 new_srv->max_commits_display = $2;
406 | SUMMARY_COMMITS_DISPLAY NUMBER {
407 if ($2 < 1) {
408 yyerror("summary_commits_display is too small:"
409 " %lld", $2);
410 YYERROR;
412 new_srv->summary_commits_display = $2;
414 | SUMMARY_TAGS_DISPLAY NUMBER {
415 if ($2 < 1) {
416 yyerror("summary_tags_display is too small:"
417 " %lld", $2);
418 YYERROR;
420 new_srv->summary_tags_display = $2;
424 serveropts2 : serveropts2 serveropts1 nl
425 | serveropts1 optnl
428 nl : '\n' optnl
431 optnl : '\n' optnl /* zero or more newlines */
432 | /* empty */
437 struct keywords {
438 const char *k_name;
439 int k_val;
443 yyerror(const char *fmt, ...)
445 va_list ap;
446 char *msg;
448 file->errors++;
449 va_start(ap, fmt);
450 if (vasprintf(&msg, fmt, ap) == -1)
451 fatalx("yyerror vasprintf");
452 va_end(ap);
453 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
454 free(msg);
455 return (0);
459 kw_cmp(const void *k, const void *e)
461 return (strcmp(k, ((const struct keywords *)e)->k_name));
465 lookup(char *s)
467 /* This has to be sorted always. */
468 static const struct keywords keywords[] = {
469 { "chroot", CHROOT },
470 { "custom_css", CUSTOM_CSS },
471 { "listen", LISTEN },
472 { "logo", LOGO },
473 { "logo_url", LOGO_URL },
474 { "max_commits_display", MAX_COMMITS_DISPLAY },
475 { "max_repos_display", MAX_REPOS_DISPLAY },
476 { "on", ON },
477 { "port", PORT },
478 { "prefork", PREFORK },
479 { "repos_path", REPOS_PATH },
480 { "respect_exportok", RESPECT_EXPORTOK },
481 { "server", SERVER },
482 { "show_repo_age", SHOW_REPO_AGE },
483 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
484 { "show_repo_description", SHOW_REPO_DESCRIPTION },
485 { "show_repo_owner", SHOW_REPO_OWNER },
486 { "show_site_owner", SHOW_SITE_OWNER },
487 { "site_link", SITE_LINK },
488 { "site_name", SITE_NAME },
489 { "site_owner", SITE_OWNER },
490 { "socket", SOCKET },
491 { "summary_commits_display", SUMMARY_COMMITS_DISPLAY },
492 { "summary_tags_display", SUMMARY_TAGS_DISPLAY },
493 { "unix_socket", UNIX_SOCKET },
494 { "unix_socket_name", UNIX_SOCKET_NAME },
496 const struct keywords *p;
498 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
499 sizeof(keywords[0]), kw_cmp);
501 if (p)
502 return (p->k_val);
503 else
504 return (STRING);
507 #define MAXPUSHBACK 128
509 unsigned char *parsebuf;
510 int parseindex;
511 unsigned char pushback_buffer[MAXPUSHBACK];
512 int pushback_index = 0;
515 lgetc(int quotec)
517 int c, next;
519 if (parsebuf) {
520 /* Read character from the parsebuffer instead of input. */
521 if (parseindex >= 0) {
522 c = parsebuf[parseindex++];
523 if (c != '\0')
524 return (c);
525 parsebuf = NULL;
526 } else
527 parseindex++;
530 if (pushback_index)
531 return (pushback_buffer[--pushback_index]);
533 if (quotec) {
534 c = getc(file->stream);
535 if (c == EOF)
536 yyerror("reached end of file while parsing "
537 "quoted string");
538 return (c);
541 c = getc(file->stream);
542 while (c == '\\') {
543 next = getc(file->stream);
544 if (next != '\n') {
545 c = next;
546 break;
548 yylval.lineno = file->lineno;
549 file->lineno++;
550 c = getc(file->stream);
553 return (c);
557 lungetc(int c)
559 if (c == EOF)
560 return (EOF);
561 if (parsebuf) {
562 parseindex--;
563 if (parseindex >= 0)
564 return (c);
566 if (pushback_index < MAXPUSHBACK-1)
567 return (pushback_buffer[pushback_index++] = c);
568 else
569 return (EOF);
573 findeol(void)
575 int c;
577 parsebuf = NULL;
579 /* Skip to either EOF or the first real EOL. */
580 while (1) {
581 if (pushback_index)
582 c = pushback_buffer[--pushback_index];
583 else
584 c = lgetc(0);
585 if (c == '\n') {
586 file->lineno++;
587 break;
589 if (c == EOF)
590 break;
592 return (ERROR);
596 yylex(void)
598 unsigned char buf[8096];
599 unsigned char *p, *val;
600 int quotec, next, c;
601 int token;
603 top:
604 p = buf;
605 c = lgetc(0);
606 while (c == ' ' || c == '\t')
607 c = lgetc(0); /* nothing */
609 yylval.lineno = file->lineno;
610 if (c == '#') {
611 c = lgetc(0);
612 while (c != '\n' && c != EOF)
613 c = lgetc(0); /* nothing */
615 if (c == '$' && parsebuf == NULL) {
616 while (1) {
617 c = lgetc(0);
618 if (c == EOF)
619 return (0);
621 if (p + 1 >= buf + sizeof(buf) - 1) {
622 yyerror("string too long");
623 return (findeol());
625 if (isalnum(c) || c == '_') {
626 *p++ = c;
627 continue;
629 *p = '\0';
630 lungetc(c);
631 break;
633 val = symget(buf);
634 if (val == NULL) {
635 yyerror("macro '%s' not defined", buf);
636 return (findeol());
638 parsebuf = val;
639 parseindex = 0;
640 goto top;
643 switch (c) {
644 case '\'':
645 case '"':
646 quotec = c;
647 while (1) {
648 c = lgetc(quotec);
649 if (c == EOF)
650 return (0);
651 if (c == '\n') {
652 file->lineno++;
653 continue;
654 } else if (c == '\\') {
655 next = lgetc(quotec);
656 if (next == EOF)
657 return (0);
658 if (next == quotec || c == ' ' || c == '\t')
659 c = next;
660 else if (next == '\n') {
661 file->lineno++;
662 continue;
663 } else
664 lungetc(next);
665 } else if (c == quotec) {
666 *p = '\0';
667 break;
668 } else if (c == '\0') {
669 yyerror("syntax error");
670 return (findeol());
672 if (p + 1 >= buf + sizeof(buf) - 1) {
673 yyerror("string too long");
674 return (findeol());
676 *p++ = c;
678 yylval.v.string = strdup(buf);
679 if (yylval.v.string == NULL)
680 err(1, "yylex: strdup");
681 return (STRING);
684 #define allowed_to_end_number(x) \
685 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
687 if (c == '-' || isdigit(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 && isdigit(c));
696 lungetc(c);
697 if (p == buf + 1 && buf[0] == '-')
698 goto nodigits;
699 if (c == EOF || allowed_to_end_number(c)) {
700 const char *errstr = NULL;
702 *p = '\0';
703 yylval.v.number = strtonum(buf, LLONG_MIN,
704 LLONG_MAX, &errstr);
705 if (errstr) {
706 yyerror("\"%s\" invalid number: %s",
707 buf, errstr);
708 return (findeol());
710 return (NUMBER);
711 } else {
712 nodigits:
713 while (p > buf + 1)
714 lungetc(*--p);
715 c = *--p;
716 if (c == '-')
717 return (c);
721 #define allowed_in_string(x) \
722 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
723 x != '{' && x != '}' && \
724 x != '!' && x != '=' && x != '#' && \
725 x != ','))
727 if (isalnum(c) || c == ':' || c == '_') {
728 do {
729 *p++ = c;
730 if ((unsigned)(p-buf) >= sizeof(buf)) {
731 yyerror("string too long");
732 return (findeol());
734 c = lgetc(0);
735 } while (c != EOF && (allowed_in_string(c)));
736 lungetc(c);
737 *p = '\0';
738 token = lookup(buf);
739 if (token == STRING) {
740 yylval.v.string = strdup(buf);
741 if (yylval.v.string == NULL)
742 err(1, "yylex: strdup");
744 return (token);
746 if (c == '\n') {
747 yylval.lineno = file->lineno;
748 file->lineno++;
750 if (c == EOF)
751 return (0);
752 return (c);
756 check_file_secrecy(int fd, const char *fname)
758 struct stat st;
760 if (fstat(fd, &st)) {
761 log_warn("cannot stat %s", fname);
762 return (-1);
764 if (st.st_uid != 0 && st.st_uid != getuid()) {
765 log_warnx("%s: owner not root or current user", fname);
766 return (-1);
768 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
769 log_warnx("%s: group writable or world read/writable", fname);
770 return (-1);
772 return (0);
775 struct file *
776 newfile(const char *name, int secret)
778 struct file *nfile;
780 nfile = calloc(1, sizeof(struct file));
781 if (nfile == NULL) {
782 log_warn("calloc");
783 return (NULL);
785 nfile->name = strdup(name);
786 if (nfile->name == NULL) {
787 log_warn("strdup");
788 free(nfile);
789 return (NULL);
791 nfile->stream = fopen(nfile->name, "r");
792 if (nfile->stream == NULL) {
793 /* no warning, we don't require a conf file */
794 free(nfile->name);
795 free(nfile);
796 return (NULL);
797 } else if (secret &&
798 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
799 fclose(nfile->stream);
800 free(nfile->name);
801 free(nfile);
802 return (NULL);
804 nfile->lineno = 1;
805 return (nfile);
808 static void
809 closefile(struct file *xfile)
811 fclose(xfile->stream);
812 free(xfile->name);
813 free(xfile);
816 static void
817 add_default_server(void)
819 new_srv = conf_new_server(D_SITENAME);
820 log_debug("%s: adding default server %s", __func__, D_SITENAME);
824 parse_config(const char *filename, struct gotwebd *env)
826 struct sym *sym, *next;
828 if (config_init(env) == -1)
829 fatalx("failed to initialize configuration");
831 gotwebd = env;
833 file = newfile(filename, 0);
834 if (file == NULL) {
835 add_default_server();
836 sockets_parse_sockets(env);
837 /* just return, as we don't require a conf file */
838 return (0);
841 yyparse();
842 errors = file->errors;
843 closefile(file);
845 /* Free macros and check which have not been used. */
846 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
847 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
848 fprintf(stderr, "warning: macro '%s' not used\n",
849 sym->nam);
850 if (!sym->persist) {
851 free(sym->nam);
852 free(sym->val);
853 TAILQ_REMOVE(&symhead, sym, entry);
854 free(sym);
858 if (errors)
859 return (-1);
861 /* just add default server if no config specified */
862 if (gotwebd->server_cnt == 0)
863 add_default_server();
865 /* setup our listening sockets */
866 sockets_parse_sockets(env);
868 return (0);
871 struct server *
872 conf_new_server(const char *name)
874 struct server *srv = NULL;
876 srv = calloc(1, sizeof(*srv));
877 if (srv == NULL)
878 fatalx("%s: calloc", __func__);
880 n = strlcpy(srv->name, name, sizeof(srv->name));
881 if (n >= sizeof(srv->name))
882 fatalx("%s: strlcpy", __func__);
883 n = snprintf(srv->unix_socket_name,
884 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
885 D_UNIX_SOCKET);
886 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
887 fatalx("%s: snprintf", __func__);
888 n = strlcpy(srv->repos_path, D_GOTPATH,
889 sizeof(srv->repos_path));
890 if (n >= sizeof(srv->repos_path))
891 fatalx("%s: strlcpy", __func__);
892 n = strlcpy(srv->site_name, D_SITENAME,
893 sizeof(srv->site_name));
894 if (n >= sizeof(srv->site_name))
895 fatalx("%s: strlcpy", __func__);
896 n = strlcpy(srv->site_owner, D_SITEOWNER,
897 sizeof(srv->site_owner));
898 if (n >= sizeof(srv->site_owner))
899 fatalx("%s: strlcpy", __func__);
900 n = strlcpy(srv->site_link, D_SITELINK,
901 sizeof(srv->site_link));
902 if (n >= sizeof(srv->site_link))
903 fatalx("%s: strlcpy", __func__);
904 n = strlcpy(srv->logo, D_GOTLOGO,
905 sizeof(srv->logo));
906 if (n >= sizeof(srv->logo))
907 fatalx("%s: strlcpy", __func__);
908 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
909 if (n >= sizeof(srv->logo_url))
910 fatalx("%s: strlcpy", __func__);
911 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
912 if (n >= sizeof(srv->custom_css))
913 fatalx("%s: strlcpy", __func__);
915 srv->show_site_owner = D_SHOWSOWNER;
916 srv->show_repo_owner = D_SHOWROWNER;
917 srv->show_repo_age = D_SHOWAGE;
918 srv->show_repo_description = D_SHOWDESC;
919 srv->show_repo_cloneurl = D_SHOWURL;
920 srv->respect_exportok = D_RESPECTEXPORTOK;
922 srv->max_repos_display = D_MAXREPODISP;
923 srv->max_commits_display = D_MAXCOMMITDISP;
924 srv->summary_commits_display = D_MAXSLCOMMDISP;
925 srv->summary_tags_display = D_MAXSLTAGDISP;
927 srv->unix_socket = 1;
928 srv->fcgi_socket = 0;
930 TAILQ_INIT(&srv->al);
931 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
932 gotwebd->server_cnt++;
934 return srv;
938 symset(const char *nam, const char *val, int persist)
940 struct sym *sym;
942 TAILQ_FOREACH(sym, &symhead, entry) {
943 if (strcmp(nam, sym->nam) == 0)
944 break;
947 if (sym != NULL) {
948 if (sym->persist == 1)
949 return (0);
950 else {
951 free(sym->nam);
952 free(sym->val);
953 TAILQ_REMOVE(&symhead, sym, entry);
954 free(sym);
957 sym = calloc(1, sizeof(*sym));
958 if (sym == NULL)
959 return (-1);
961 sym->nam = strdup(nam);
962 if (sym->nam == NULL) {
963 free(sym);
964 return (-1);
966 sym->val = strdup(val);
967 if (sym->val == NULL) {
968 free(sym->nam);
969 free(sym);
970 return (-1);
972 sym->used = 0;
973 sym->persist = persist;
974 TAILQ_INSERT_TAIL(&symhead, sym, entry);
975 return (0);
979 cmdline_symset(char *s)
981 char *sym, *val;
982 int ret;
984 val = strrchr(s, '=');
985 if (val == NULL)
986 return (-1);
988 sym = strndup(s, val - s);
989 if (sym == NULL)
990 fatal("%s: strndup", __func__);
992 ret = symset(sym, val + 1, 1);
993 free(sym);
995 return (ret);
998 char *
999 symget(const char *nam)
1001 struct sym *sym;
1003 TAILQ_FOREACH(sym, &symhead, entry) {
1004 if (strcmp(nam, sym->nam) == 0) {
1005 sym->used = 1;
1006 return (sym->val);
1009 return (NULL);
1013 get_addrs(const char *hostname, const char *servname, struct server *new_srv)
1015 struct addrinfo hints, *res0, *res;
1016 int error;
1017 struct sockaddr_in *sin;
1018 struct sockaddr_in6 *sin6;
1019 struct address *h;
1021 memset(&hints, 0, sizeof(hints));
1022 hints.ai_family = AF_UNSPEC;
1023 hints.ai_socktype = SOCK_STREAM;
1024 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
1025 error = getaddrinfo(hostname, servname, &hints, &res0);
1026 if (error) {
1027 log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
1028 hostname, servname, gai_strerror(error));
1029 return (-1);
1032 for (res = res0; res; res = res->ai_next) {
1033 if ((h = calloc(1, sizeof(*h))) == NULL)
1034 fatal(__func__);
1036 if (hostname == NULL) {
1037 strlcpy(h->ifname, "*", sizeof(h->ifname));
1038 } else {
1039 if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1040 sizeof(h->ifname)) {
1041 log_warnx("%s: address truncated: %s",
1042 __func__, hostname);
1043 freeaddrinfo(res0);
1044 free(h);
1045 return (-1);
1049 h->ai_family = res->ai_family;
1050 h->ai_socktype = res->ai_socktype;
1051 h->ai_protocol = res->ai_protocol;
1052 memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
1053 h->slen = res->ai_addrlen;
1055 switch (res->ai_family) {
1056 case AF_INET:
1057 sin = (struct sockaddr_in *)res->ai_addr;
1058 h->port = ntohs(sin->sin_port);
1059 break;
1060 case AF_INET6:
1061 sin6 = (struct sockaddr_in6 *)res->ai_addr;
1062 h->port = ntohs(sin6->sin6_port);
1063 break;
1064 default:
1065 fatalx("unknown address family %d", res->ai_family);
1068 if (add_addr(new_srv, h))
1069 return -1;
1071 freeaddrinfo(res0);
1072 return (0);
1076 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1077 const char *other_srv)
1079 struct address *a;
1080 void *ia;
1081 char buf[INET6_ADDRSTRLEN];
1082 const char *addrstr;
1084 TAILQ_FOREACH(a, al, entry) {
1085 if (a->ai_family != h->ai_family ||
1086 a->ai_socktype != h->ai_socktype ||
1087 a->ai_protocol != h->ai_protocol ||
1088 a->slen != h->slen ||
1089 memcmp(&a->ss, &h->ss, a->slen) != 0)
1090 continue;
1092 switch (h->ss.ss_family) {
1093 case AF_INET:
1094 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1095 break;
1096 case AF_INET6:
1097 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1098 break;
1099 default:
1100 yyerror("unknown address family: %d", h->ss.ss_family);
1101 return -1;
1103 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1104 if (addrstr) {
1105 if (other_srv) {
1106 yyerror("server %s: duplicate fcgi listen "
1107 "address %s:%d, already used by server %s",
1108 new_srv, addrstr, h->port, other_srv);
1109 } else {
1110 log_warnx("server: %s: duplicate fcgi listen "
1111 "address %s:%d", new_srv, addrstr, h->port);
1113 } else {
1114 if (other_srv) {
1115 yyerror("server: %s: duplicate fcgi listen "
1116 "address, already used by server %s",
1117 new_srv, other_srv);
1118 } else {
1119 log_warnx("server %s: duplicate fcgi listen "
1120 "address", new_srv);
1124 return -1;
1127 return 0;
1131 add_addr(struct server *new_srv, struct address *h)
1133 struct server *srv;
1135 /* Address cannot be shared between different servers. */
1136 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1137 if (srv == new_srv)
1138 continue;
1139 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1140 return -1;
1143 /* Tolerate duplicate address lines within the scope of a server. */
1144 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1145 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);
1146 else
1147 free(h);
1149 return 0;