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 <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/queue.h>
27 #include <sys/socket.h>
31 #include <netinet/in.h>
33 #include <arpa/inet.h>
50 #include "got_reference.h"
54 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 TAILQ_ENTRY(file) entry;
62 struct file *newfile(const char *, int);
63 static void closefile(struct file *);
64 int check_file_secrecy(int, const char *);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 TAILQ_ENTRY(sym) entry;
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
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 *);
96 int get_addrs(const char *, const char *, struct server *);
97 int addr_dup_check(struct addresslist *, struct address *,
98 const char *, const char *);
99 int add_addr(struct server *, struct address *);
111 %token LISTEN WWW_PATH SITE_NAME SITE_OWNER SITE_LINK LOGO
112 %token LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
113 %token MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
114 %token SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK RESPECT_EXPORTOK
115 %token UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS SOCKET
116 %token SUMMARY_COMMITS_DISPLAY SUMMARY_TAGS_DISPLAY
118 %token <v.string> STRING
119 %token <v.number> NUMBER
120 %type <v.number> boolean
121 %type <v.string> listen_addr
125 grammar : /* empty */
127 | grammar varset '\n'
129 | grammar server '\n'
130 | grammar error '\n' { file->errors++; }
133 varset : STRING '=' STRING {
136 if (isspace((unsigned char)*s)) {
137 yyerror("macro name cannot contain "
144 if (symset($1, $3, 0) == -1)
145 fatal("cannot store variable");
152 if (strcasecmp($1, "1") == 0 ||
153 strcasecmp($1, "on") == 0)
155 else if (strcasecmp($1, "0") == 0 ||
156 strcasecmp($1, "off") == 0)
159 yyerror("invalid boolean value '%s'", $1);
167 if ($1 != 0 && $1 != 1) {
168 yyerror("invalid boolean value '%lld'", $1);
175 listen_addr : '*' { $$ = NULL; }
179 main : PREFORK NUMBER {
180 if ($2 <= 0 || $2 > PROC_MAX_INSTANCES) {
181 yyerror("prefork is %s: %lld",
182 $2 <= 0 ? "too small" : "too large", $2);
185 gotwebd->prefork_gotwebd = $2;
189 yyerror("chroot path can't be an empty"
195 n = strlcpy(gotwebd->httpd_chroot, $2,
196 sizeof(gotwebd->httpd_chroot));
197 if (n >= sizeof(gotwebd->httpd_chroot)) {
198 yyerror("%s: httpd_chroot truncated", __func__);
204 | UNIX_SOCKET boolean {
205 gotwebd->unix_socket = $2;
207 | UNIX_SOCKET_NAME STRING {
208 n = snprintf(gotwebd->unix_socket_name,
209 sizeof(gotwebd->unix_socket_name), "%s%s",
210 gotwebd->httpd_chroot, $2);
212 (size_t)n >= sizeof(gotwebd->unix_socket_name)) {
213 yyerror("%s: unix_socket_name truncated",
222 server : SERVER STRING {
225 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
226 if (strcmp(srv->name, $2) == 0) {
227 yyerror("server name exists '%s'", $2);
233 new_srv = conf_new_server($2);
234 log_debug("adding server %s", $2);
240 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
241 if (strcmp(srv->name, $2) == 0) {
242 yyerror("server name exists '%s'", $2);
248 new_srv = conf_new_server($2);
249 log_debug("adding server %s", $2);
251 } '{' optnl serveropts2 '}' {
255 serveropts1 : REPOS_PATH STRING {
256 n = strlcpy(new_srv->repos_path, $2,
257 sizeof(new_srv->repos_path));
258 if (n >= sizeof(new_srv->repos_path)) {
259 yyerror("%s: repos_path truncated", __func__);
266 n = strlcpy(new_srv->site_name, $2,
267 sizeof(new_srv->site_name));
268 if (n >= sizeof(new_srv->site_name)) {
269 yyerror("%s: site_name truncated", __func__);
275 | SITE_OWNER STRING {
276 n = strlcpy(new_srv->site_owner, $2,
277 sizeof(new_srv->site_owner));
278 if (n >= sizeof(new_srv->site_owner)) {
279 yyerror("%s: site_owner truncated", __func__);
286 n = strlcpy(new_srv->site_link, $2,
287 sizeof(new_srv->site_link));
288 if (n >= sizeof(new_srv->site_link)) {
289 yyerror("%s: site_link truncated", __func__);
296 n = strlcpy(new_srv->logo, $2, sizeof(new_srv->logo));
297 if (n >= sizeof(new_srv->logo)) {
298 yyerror("%s: logo truncated", __func__);
305 n = strlcpy(new_srv->logo_url, $2,
306 sizeof(new_srv->logo_url));
307 if (n >= sizeof(new_srv->logo_url)) {
308 yyerror("%s: logo_url truncated", __func__);
314 | CUSTOM_CSS STRING {
315 n = strlcpy(new_srv->custom_css, $2,
316 sizeof(new_srv->custom_css));
317 if (n >= sizeof(new_srv->custom_css)) {
318 yyerror("%s: custom_css truncated", __func__);
324 | LISTEN ON listen_addr PORT STRING {
325 if (get_addrs($3, $5, new_srv) == -1) {
326 yyerror("could not get addrs");
331 new_srv->fcgi_socket = 1;
333 | LISTEN ON listen_addr PORT NUMBER {
337 n = snprintf(portno, sizeof(portno), "%lld",
339 if (n < 0 || (size_t)n >= sizeof(portno))
340 fatalx("port number too long: %lld",
343 if (get_addrs($3, portno, new_srv) == -1) {
344 yyerror("could not get addrs");
348 new_srv->fcgi_socket = 1;
350 | LISTEN ON SOCKET STRING {
351 if (strcasecmp($4, "off") == 0) {
352 new_srv->unix_socket = 0;
357 new_srv->unix_socket = 1;
359 n = snprintf(new_srv->unix_socket_name,
360 sizeof(new_srv->unix_socket_name), "%s%s",
361 gotwebd->httpd_chroot, $4);
363 (size_t)n >= sizeof(new_srv->unix_socket_name)) {
364 yyerror("%s: unix_socket_name truncated",
371 | SHOW_SITE_OWNER boolean {
372 new_srv->show_site_owner = $2;
374 | SHOW_REPO_OWNER boolean {
375 new_srv->show_repo_owner = $2;
377 | SHOW_REPO_AGE boolean {
378 new_srv->show_repo_age = $2;
380 | SHOW_REPO_DESCRIPTION boolean {
381 new_srv->show_repo_description = $2;
383 | SHOW_REPO_CLONEURL boolean {
384 new_srv->show_repo_cloneurl = $2;
386 | RESPECT_EXPORTOK boolean {
387 new_srv->respect_exportok = $2;
389 | MAX_REPOS_DISPLAY NUMBER {
391 yyerror("max_repos_display is too small: %lld",
395 new_srv->max_repos_display = $2;
397 | MAX_COMMITS_DISPLAY NUMBER {
399 yyerror("max_commits_display is too small:"
403 new_srv->max_commits_display = $2;
405 | SUMMARY_COMMITS_DISPLAY NUMBER {
407 yyerror("summary_commits_display is too small:"
411 new_srv->summary_commits_display = $2;
413 | SUMMARY_TAGS_DISPLAY NUMBER {
415 yyerror("summary_tags_display is too small:"
419 new_srv->summary_tags_display = $2;
423 serveropts2 : serveropts2 serveropts1 nl
430 optnl : '\n' optnl /* zero or more newlines */
442 yyerror(const char *fmt, ...)
449 if (vasprintf(&msg, fmt, ap) == -1)
450 fatalx("yyerror vasprintf");
452 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
458 kw_cmp(const void *k, const void *e)
460 return (strcmp(k, ((const struct keywords *)e)->k_name));
466 /* This has to be sorted always. */
467 static const struct keywords keywords[] = {
468 { "chroot", CHROOT },
469 { "custom_css", CUSTOM_CSS },
470 { "listen", LISTEN },
472 { "logo_url", LOGO_URL },
473 { "max_commits_display", MAX_COMMITS_DISPLAY },
474 { "max_repos_display", MAX_REPOS_DISPLAY },
477 { "prefork", PREFORK },
478 { "repos_path", REPOS_PATH },
479 { "respect_exportok", RESPECT_EXPORTOK },
480 { "server", SERVER },
481 { "show_repo_age", SHOW_REPO_AGE },
482 { "show_repo_cloneurl", SHOW_REPO_CLONEURL },
483 { "show_repo_description", SHOW_REPO_DESCRIPTION },
484 { "show_repo_owner", SHOW_REPO_OWNER },
485 { "show_site_owner", SHOW_SITE_OWNER },
486 { "site_link", SITE_LINK },
487 { "site_name", SITE_NAME },
488 { "site_owner", SITE_OWNER },
489 { "socket", SOCKET },
490 { "summary_commits_display", SUMMARY_COMMITS_DISPLAY },
491 { "summary_tags_display", SUMMARY_TAGS_DISPLAY },
492 { "unix_socket", UNIX_SOCKET },
493 { "unix_socket_name", UNIX_SOCKET_NAME },
495 const struct keywords *p;
497 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
498 sizeof(keywords[0]), kw_cmp);
506 #define MAXPUSHBACK 128
508 unsigned char *parsebuf;
510 unsigned char pushback_buffer[MAXPUSHBACK];
511 int pushback_index = 0;
519 /* Read character from the parsebuffer instead of input. */
520 if (parseindex >= 0) {
521 c = parsebuf[parseindex++];
530 return (pushback_buffer[--pushback_index]);
533 c = getc(file->stream);
535 yyerror("reached end of file while parsing "
540 c = getc(file->stream);
542 next = getc(file->stream);
547 yylval.lineno = file->lineno;
549 c = getc(file->stream);
565 if (pushback_index < MAXPUSHBACK-1)
566 return (pushback_buffer[pushback_index++] = c);
578 /* Skip to either EOF or the first real EOL. */
581 c = pushback_buffer[--pushback_index];
597 unsigned char buf[8096];
598 unsigned char *p, *val;
605 while (c == ' ' || c == '\t')
606 c = lgetc(0); /* nothing */
608 yylval.lineno = file->lineno;
611 while (c != '\n' && c != EOF)
612 c = lgetc(0); /* nothing */
614 if (c == '$' && parsebuf == NULL) {
620 if (p + 1 >= buf + sizeof(buf) - 1) {
621 yyerror("string too long");
624 if (isalnum(c) || c == '_') {
634 yyerror("macro '%s' not defined", buf);
653 } else if (c == '\\') {
654 next = lgetc(quotec);
657 if (next == quotec || c == ' ' || c == '\t')
659 else if (next == '\n') {
664 } else if (c == quotec) {
667 } else if (c == '\0') {
668 yyerror("syntax error");
671 if (p + 1 >= buf + sizeof(buf) - 1) {
672 yyerror("string too long");
677 yylval.v.string = strdup(buf);
678 if (yylval.v.string == NULL)
679 err(1, "yylex: strdup");
683 #define allowed_to_end_number(x) \
684 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
686 if (c == '-' || isdigit(c)) {
689 if ((unsigned)(p-buf) >= sizeof(buf)) {
690 yyerror("string too long");
694 } while (c != EOF && isdigit(c));
696 if (p == buf + 1 && buf[0] == '-')
698 if (c == EOF || allowed_to_end_number(c)) {
699 const char *errstr = NULL;
702 yylval.v.number = strtonum(buf, LLONG_MIN,
705 yyerror("\"%s\" invalid number: %s",
720 #define allowed_in_string(x) \
721 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
722 x != '{' && x != '}' && \
723 x != '!' && x != '=' && x != '#' && \
726 if (isalnum(c) || c == ':' || c == '_') {
729 if ((unsigned)(p-buf) >= sizeof(buf)) {
730 yyerror("string too long");
734 } while (c != EOF && (allowed_in_string(c)));
738 if (token == STRING) {
739 yylval.v.string = strdup(buf);
740 if (yylval.v.string == NULL)
741 err(1, "yylex: strdup");
746 yylval.lineno = file->lineno;
755 check_file_secrecy(int fd, const char *fname)
759 if (fstat(fd, &st)) {
760 log_warn("cannot stat %s", fname);
763 if (st.st_uid != 0 && st.st_uid != getuid()) {
764 log_warnx("%s: owner not root or current user", fname);
767 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
768 log_warnx("%s: group writable or world read/writable", fname);
775 newfile(const char *name, int secret)
779 nfile = calloc(1, sizeof(struct file));
784 nfile->name = strdup(name);
785 if (nfile->name == NULL) {
790 nfile->stream = fopen(nfile->name, "r");
791 if (nfile->stream == NULL) {
792 /* no warning, we don't require a conf file */
797 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
798 fclose(nfile->stream);
808 closefile(struct file *xfile)
810 fclose(xfile->stream);
816 add_default_server(void)
818 new_srv = conf_new_server(D_SITENAME);
819 log_debug("%s: adding default server %s", __func__, D_SITENAME);
823 parse_config(const char *filename, struct gotwebd *env)
825 struct sym *sym, *next;
827 if (config_init(env) == -1)
828 fatalx("failed to initialize configuration");
832 file = newfile(filename, 0);
834 add_default_server();
835 sockets_parse_sockets(env);
836 /* just return, as we don't require a conf file */
841 errors = file->errors;
844 /* Free macros and check which have not been used. */
845 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
846 if ((gotwebd->gotwebd_verbose > 1) && !sym->used)
847 fprintf(stderr, "warning: macro '%s' not used\n",
852 TAILQ_REMOVE(&symhead, sym, entry);
860 /* just add default server if no config specified */
861 if (gotwebd->server_cnt == 0)
862 add_default_server();
864 /* setup our listening sockets */
865 sockets_parse_sockets(env);
871 conf_new_server(const char *name)
873 struct server *srv = NULL;
875 srv = calloc(1, sizeof(*srv));
877 fatalx("%s: calloc", __func__);
879 n = strlcpy(srv->name, name, sizeof(srv->name));
880 if (n >= sizeof(srv->name))
881 fatalx("%s: strlcpy", __func__);
882 n = snprintf(srv->unix_socket_name,
883 sizeof(srv->unix_socket_name), "%s%s", D_HTTPD_CHROOT,
885 if (n < 0 || (size_t)n >= sizeof(srv->unix_socket_name))
886 fatalx("%s: snprintf", __func__);
887 n = strlcpy(srv->repos_path, D_GOTPATH,
888 sizeof(srv->repos_path));
889 if (n >= sizeof(srv->repos_path))
890 fatalx("%s: strlcpy", __func__);
891 n = strlcpy(srv->site_name, D_SITENAME,
892 sizeof(srv->site_name));
893 if (n >= sizeof(srv->site_name))
894 fatalx("%s: strlcpy", __func__);
895 n = strlcpy(srv->site_owner, D_SITEOWNER,
896 sizeof(srv->site_owner));
897 if (n >= sizeof(srv->site_owner))
898 fatalx("%s: strlcpy", __func__);
899 n = strlcpy(srv->site_link, D_SITELINK,
900 sizeof(srv->site_link));
901 if (n >= sizeof(srv->site_link))
902 fatalx("%s: strlcpy", __func__);
903 n = strlcpy(srv->logo, D_GOTLOGO,
905 if (n >= sizeof(srv->logo))
906 fatalx("%s: strlcpy", __func__);
907 n = strlcpy(srv->logo_url, D_GOTURL, sizeof(srv->logo_url));
908 if (n >= sizeof(srv->logo_url))
909 fatalx("%s: strlcpy", __func__);
910 n = strlcpy(srv->custom_css, D_GOTWEBCSS, sizeof(srv->custom_css));
911 if (n >= sizeof(srv->custom_css))
912 fatalx("%s: strlcpy", __func__);
914 srv->show_site_owner = D_SHOWSOWNER;
915 srv->show_repo_owner = D_SHOWROWNER;
916 srv->show_repo_age = D_SHOWAGE;
917 srv->show_repo_description = D_SHOWDESC;
918 srv->show_repo_cloneurl = D_SHOWURL;
919 srv->respect_exportok = D_RESPECTEXPORTOK;
921 srv->max_repos_display = D_MAXREPODISP;
922 srv->max_commits_display = D_MAXCOMMITDISP;
923 srv->summary_commits_display = D_MAXSLCOMMDISP;
924 srv->summary_tags_display = D_MAXSLTAGDISP;
926 srv->unix_socket = 1;
927 srv->fcgi_socket = 0;
929 TAILQ_INIT(&srv->al);
930 TAILQ_INSERT_TAIL(&gotwebd->servers, srv, entry);
931 gotwebd->server_cnt++;
937 symset(const char *nam, const char *val, int persist)
941 TAILQ_FOREACH(sym, &symhead, entry) {
942 if (strcmp(nam, sym->nam) == 0)
947 if (sym->persist == 1)
952 TAILQ_REMOVE(&symhead, sym, entry);
956 sym = calloc(1, sizeof(*sym));
960 sym->nam = strdup(nam);
961 if (sym->nam == NULL) {
965 sym->val = strdup(val);
966 if (sym->val == NULL) {
972 sym->persist = persist;
973 TAILQ_INSERT_TAIL(&symhead, sym, entry);
978 cmdline_symset(char *s)
983 val = strrchr(s, '=');
987 sym = strndup(s, val - s);
989 fatal("%s: strndup", __func__);
991 ret = symset(sym, val + 1, 1);
998 symget(const char *nam)
1002 TAILQ_FOREACH(sym, &symhead, entry) {
1003 if (strcmp(nam, sym->nam) == 0) {
1012 get_addrs(const char *hostname, const char *servname, struct server *new_srv)
1014 struct addrinfo hints, *res0, *res;
1016 struct sockaddr_in *sin;
1017 struct sockaddr_in6 *sin6;
1020 memset(&hints, 0, sizeof(hints));
1021 hints.ai_family = AF_UNSPEC;
1022 hints.ai_socktype = SOCK_STREAM;
1023 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
1024 error = getaddrinfo(hostname, servname, &hints, &res0);
1026 log_warnx("%s: could not parse \"%s:%s\": %s", __func__,
1027 hostname, servname, gai_strerror(error));
1031 for (res = res0; res; res = res->ai_next) {
1032 if ((h = calloc(1, sizeof(*h))) == NULL)
1035 if (hostname == NULL) {
1036 strlcpy(h->ifname, "*", sizeof(h->ifname));
1038 if (strlcpy(h->ifname, hostname, sizeof(h->ifname)) >=
1039 sizeof(h->ifname)) {
1040 log_warnx("%s: address truncated: %s",
1041 __func__, hostname);
1048 h->ai_family = res->ai_family;
1049 h->ai_socktype = res->ai_socktype;
1050 h->ai_protocol = res->ai_protocol;
1051 memcpy(&h->ss, res->ai_addr, res->ai_addrlen);
1052 h->slen = res->ai_addrlen;
1054 switch (res->ai_family) {
1056 sin = (struct sockaddr_in *)res->ai_addr;
1057 h->port = ntohs(sin->sin_port);
1060 sin6 = (struct sockaddr_in6 *)res->ai_addr;
1061 h->port = ntohs(sin6->sin6_port);
1064 fatalx("unknown address family %d", res->ai_family);
1067 if (add_addr(new_srv, h))
1075 addr_dup_check(struct addresslist *al, struct address *h, const char *new_srv,
1076 const char *other_srv)
1080 char buf[INET6_ADDRSTRLEN];
1081 const char *addrstr;
1083 TAILQ_FOREACH(a, al, entry) {
1084 if (a->ai_family != h->ai_family ||
1085 a->ai_socktype != h->ai_socktype ||
1086 a->ai_protocol != h->ai_protocol ||
1087 a->slen != h->slen ||
1088 memcmp(&a->ss, &h->ss, a->slen) != 0)
1091 switch (h->ss.ss_family) {
1093 ia = &((struct sockaddr_in *)(&h->ss))->sin_addr;
1096 ia = &((struct sockaddr_in6 *)(&h->ss))->sin6_addr;
1099 yyerror("unknown address family: %d", h->ss.ss_family);
1102 addrstr = inet_ntop(h->ss.ss_family, ia, buf, sizeof(buf));
1105 yyerror("server %s: duplicate fcgi listen "
1106 "address %s:%d, already used by server %s",
1107 new_srv, addrstr, h->port, other_srv);
1109 log_warnx("server: %s: duplicate fcgi listen "
1110 "address %s:%d", new_srv, addrstr, h->port);
1114 yyerror("server: %s: duplicate fcgi listen "
1115 "address, already used by server %s",
1116 new_srv, other_srv);
1118 log_warnx("server %s: duplicate fcgi listen "
1119 "address", new_srv);
1130 add_addr(struct server *new_srv, struct address *h)
1134 /* Address cannot be shared between different servers. */
1135 TAILQ_FOREACH(srv, &gotwebd->servers, entry) {
1138 if (addr_dup_check(&srv->al, h, new_srv->name, srv->name))
1142 /* Tolerate duplicate address lines within the scope of a server. */
1143 if (addr_dup_check(&new_srv->al, h, NULL, NULL) == 0)
1144 TAILQ_INSERT_TAIL(&new_srv->al, h, entry);