2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #include <sys/types.h>
27 #include <sys/queue.h>
43 #include "got_error.h"
45 #include "got_reference.h"
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
54 TAILQ_ENTRY(file) entry;
60 struct file *newfile(const char *, int, int);
61 static void closefile(struct file *);
62 int check_file_secrecy(int, const char *);
65 int yyerror(const char *, ...)
66 __attribute__((__format__ (printf, 1, 2)))
67 __attribute__((__nonnull__ (1)));
68 int kw_cmp(const void *, const void *);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 TAILQ_ENTRY(sym) entry;
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
88 static struct gotd *gotd;
89 static struct gotd_repo *new_repo;
90 static int conf_limit_user_connections(const char *, int);
91 static struct gotd_repo *conf_new_repo(const char *);
92 static void conf_new_access_rule(struct gotd_repo *,
93 enum gotd_access, int, char *);
94 static int conf_protect_ref_namespace(char **,
95 struct got_pathlist_head *, char *);
96 static int conf_protect_tag_namespace(struct gotd_repo *,
98 static int conf_protect_branch_namespace(
99 struct gotd_repo *, char *);
100 static int conf_protect_branch(struct gotd_repo *,
102 static enum gotd_procid gotd_proc_id;
115 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
116 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
117 %token PROTECT NAMESPACE BRANCH TAG
119 %token <v.string> STRING
120 %token <v.number> NUMBER
128 | grammar repository '\n'
133 yyerror("invalid timeout: %lld", $1);
141 const char *type = "seconds";
146 yyerror("invalid number of seconds: %s", $1);
152 switch ($1[len - 1]) {
172 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
174 yyerror("number of %s is %s: %s", type,
185 main : LISTEN ON STRING {
186 if (!got_path_is_absolute($3))
187 yyerror("bad unix socket path \"%s\": "
188 "must be an absolute path", $3);
190 if (gotd_proc_id == PROC_LISTEN) {
191 if (strlcpy(gotd->unix_socket_path, $3,
192 sizeof(gotd->unix_socket_path)) >=
193 sizeof(gotd->unix_socket_path)) {
194 yyerror("%s: unix socket path too long",
203 if (strlcpy(gotd->user_name, $2,
204 sizeof(gotd->user_name)) >=
205 sizeof(gotd->user_name)) {
206 yyerror("%s: user name too long", __func__);
215 connection : CONNECTION '{' optnl conflags_l '}'
216 | CONNECTION conflags
218 conflags_l : conflags optnl conflags_l
222 conflags : REQUEST TIMEOUT timeout {
223 if ($3.tv_sec <= 0) {
224 yyerror("invalid timeout: %lld", $3.tv_sec);
227 memcpy(&gotd->request_timeout, &$3,
228 sizeof(gotd->request_timeout));
230 | LIMIT USER STRING NUMBER {
231 if (gotd_proc_id == PROC_LISTEN &&
232 conf_limit_user_connections($3, $4) == -1) {
240 protect : PROTECT '{' optnl protectflags_l '}'
241 | PROTECT protectflags
243 protectflags_l : protectflags optnl protectflags_l
247 protectflags : TAG NAMESPACE STRING {
248 if (gotd_proc_id == PROC_GOTD ||
249 gotd_proc_id == PROC_REPO_WRITE) {
250 if (conf_protect_tag_namespace(new_repo, $3)) {
257 | BRANCH NAMESPACE STRING {
258 if (gotd_proc_id == PROC_GOTD ||
259 gotd_proc_id == PROC_REPO_WRITE) {
260 if (conf_protect_branch_namespace(new_repo,
269 if (gotd_proc_id == PROC_GOTD ||
270 gotd_proc_id == PROC_REPO_WRITE) {
271 if (conf_protect_branch(new_repo, $2)) {
280 repository : REPOSITORY STRING {
281 struct gotd_repo *repo;
283 TAILQ_FOREACH(repo, &gotd->repos, entry) {
284 if (strcmp(repo->name, $2) == 0) {
285 yyerror("duplicate repository '%s'", $2);
291 if (gotd_proc_id == PROC_GOTD ||
292 gotd_proc_id == PROC_AUTH ||
293 gotd_proc_id == PROC_REPO_WRITE) {
294 new_repo = conf_new_repo($2);
297 } '{' optnl repoopts2 '}' {
301 repoopts1 : PATH STRING {
302 if (gotd_proc_id == PROC_GOTD ||
303 gotd_proc_id == PROC_AUTH ||
304 gotd_proc_id == PROC_REPO_WRITE) {
305 if (!got_path_is_absolute($2)) {
306 yyerror("%s: path %s is not absolute",
311 if (realpath($2, new_repo->path) == NULL) {
312 yyerror("realpath %s: %s", $2, strerror(errno));
320 if (gotd_proc_id == PROC_AUTH) {
321 conf_new_access_rule(new_repo,
322 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
327 if (gotd_proc_id == PROC_AUTH) {
328 conf_new_access_rule(new_repo,
329 GOTD_ACCESS_PERMITTED,
330 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
335 if (gotd_proc_id == PROC_AUTH) {
336 conf_new_access_rule(new_repo,
337 GOTD_ACCESS_DENIED, 0, $2);
344 repoopts2 : repoopts2 repoopts1 nl
351 optnl : '\n' optnl /* zero or more newlines */
363 yyerror(const char *fmt, ...)
370 if (vasprintf(&msg, fmt, ap) == -1)
371 fatalx("yyerror vasprintf");
373 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
379 kw_cmp(const void *k, const void *e)
381 return (strcmp(k, ((const struct keywords *)e)->k_name));
387 /* This has to be sorted always. */
388 static const struct keywords keywords[] = {
389 { "branch", BRANCH },
390 { "connection", CONNECTION },
393 { "listen", LISTEN },
394 { "namespace", NAMESPACE },
397 { "permit", PERMIT },
398 { "protect", PROTECT },
399 { "repository", REPOSITORY },
400 { "request", REQUEST },
404 { "timeout", TIMEOUT },
407 const struct keywords *p;
409 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
410 sizeof(keywords[0]), kw_cmp);
418 #define MAXPUSHBACK 128
420 unsigned char *parsebuf;
422 unsigned char pushback_buffer[MAXPUSHBACK];
423 int pushback_index = 0;
431 /* Read character from the parsebuffer instead of input. */
432 if (parseindex >= 0) {
433 c = parsebuf[parseindex++];
442 return (pushback_buffer[--pushback_index]);
445 c = getc(file->stream);
447 yyerror("reached end of file while parsing "
452 c = getc(file->stream);
454 next = getc(file->stream);
459 yylval.lineno = file->lineno;
461 c = getc(file->stream);
477 if (pushback_index < MAXPUSHBACK-1)
478 return (pushback_buffer[pushback_index++] = c);
490 /* Skip to either EOF or the first real EOL. */
493 c = pushback_buffer[--pushback_index];
509 unsigned char buf[8096];
510 unsigned char *p, *val;
517 while (c == ' ' || c == '\t')
518 c = lgetc(0); /* nothing */
520 yylval.lineno = file->lineno;
523 while (c != '\n' && c != EOF)
524 c = lgetc(0); /* nothing */
526 if (c == '$' && parsebuf == NULL) {
532 if (p + 1 >= buf + sizeof(buf) - 1) {
533 yyerror("string too long");
536 if (isalnum(c) || c == '_') {
546 yyerror("macro '%s' not defined", buf);
565 } else if (c == '\\') {
566 next = lgetc(quotec);
569 if (next == quotec || c == ' ' || c == '\t')
571 else if (next == '\n') {
576 } else if (c == quotec) {
579 } else if (c == '\0') {
580 yyerror("syntax error");
583 if (p + 1 >= buf + sizeof(buf) - 1) {
584 yyerror("string too long");
589 yylval.v.string = strdup(buf);
590 if (yylval.v.string == NULL)
591 err(1, "yylex: strdup");
595 #define allowed_to_end_number(x) \
596 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
598 if (c == '-' || isdigit(c)) {
601 if ((unsigned)(p-buf) >= sizeof(buf)) {
602 yyerror("string too long");
606 } while (c != EOF && isdigit(c));
608 if (p == buf + 1 && buf[0] == '-')
610 if (c == EOF || allowed_to_end_number(c)) {
611 const char *errstr = NULL;
614 yylval.v.number = strtonum(buf, LLONG_MIN,
617 yyerror("\"%s\" invalid number: %s",
632 #define allowed_in_string(x) \
633 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
634 x != '{' && x != '}' && \
635 x != '!' && x != '=' && x != '#' && \
638 if (isalnum(c) || c == ':' || c == '_') {
641 if ((unsigned)(p-buf) >= sizeof(buf)) {
642 yyerror("string too long");
646 } while (c != EOF && (allowed_in_string(c)));
650 if (token == STRING) {
651 yylval.v.string = strdup(buf);
652 if (yylval.v.string == NULL)
653 err(1, "yylex: strdup");
658 yylval.lineno = file->lineno;
667 check_file_secrecy(int fd, const char *fname)
671 if (fstat(fd, &st)) {
672 log_warn("cannot stat %s", fname);
675 if (st.st_uid != 0 && st.st_uid != getuid()) {
676 log_warnx("%s: owner not root or current user", fname);
679 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
680 log_warnx("%s: group writable or world read/writable", fname);
687 newfile(const char *name, int secret, int required)
691 nfile = calloc(1, sizeof(struct file));
696 nfile->name = strdup(name);
697 if (nfile->name == NULL) {
702 nfile->stream = fopen(nfile->name, "r");
703 if (nfile->stream == NULL) {
705 log_warn("open %s", nfile->name);
710 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
711 fclose(nfile->stream);
721 closefile(struct file *xfile)
723 fclose(xfile->stream);
729 parse_config(const char *filename, enum gotd_procid proc_id,
730 struct gotd *env, int require_config_file)
732 struct sym *sym, *next;
733 struct gotd_repo *repo;
735 memset(env, 0, sizeof(*env));
738 gotd_proc_id = proc_id;
739 TAILQ_INIT(&gotd->repos);
741 /* Apply default values. */
742 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
743 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
744 fprintf(stderr, "%s: unix socket path too long", __func__);
747 if (strlcpy(gotd->user_name, GOTD_USER,
748 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
749 fprintf(stderr, "%s: user name too long", __func__);
753 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
754 gotd->request_timeout.tv_usec = 0;
756 file = newfile(filename, 0, require_config_file);
758 return require_config_file ? -1 : 0;
761 errors = file->errors;
764 /* Free macros and check which have not been used. */
765 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
766 if ((gotd->verbosity > 1) && !sym->used)
767 fprintf(stderr, "warning: macro '%s' not used\n",
772 TAILQ_REMOVE(&symhead, sym, entry);
780 TAILQ_FOREACH(repo, &gotd->repos, entry) {
781 if (repo->path[0] == '\0') {
782 log_warnx("repository \"%s\": no path provided in "
783 "configuration file", repo->name);
788 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
789 log_warnx("no repository defined in configuration file");
797 uid_connection_limit_cmp(const void *pa, const void *pb)
799 const struct gotd_uid_connection_limit *a = pa, *b = pb;
803 else if (a->uid > b->uid);
810 conf_limit_user_connections(const char *user, int maximum)
813 struct gotd_uid_connection_limit *limit;
817 yyerror("max connections cannot be smaller 1");
820 if (maximum > GOTD_MAXCLIENTS) {
821 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
825 if (gotd_auth_parseuid(user, &uid) == -1) {
826 yyerror("%s: no such user", user);
830 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
831 gotd->nconnection_limits, uid);
833 limit->max_connections = maximum;
837 limit = gotd->connection_limits;
838 nlimits = gotd->nconnection_limits + 1;
839 limit = reallocarray(limit, nlimits, sizeof(*limit));
841 fatal("reallocarray");
843 limit[nlimits - 1].uid = uid;
844 limit[nlimits - 1].max_connections = maximum;
846 gotd->connection_limits = limit;
847 gotd->nconnection_limits = nlimits;
848 qsort(gotd->connection_limits, gotd->nconnection_limits,
849 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
854 static struct gotd_repo *
855 conf_new_repo(const char *name)
857 struct gotd_repo *repo;
859 if (name[0] == '\0') {
860 fatalx("syntax error: empty repository name found in %s",
864 if (strchr(name, '\n') != NULL)
865 fatalx("repository names must not contain linefeeds: %s", name);
867 repo = calloc(1, sizeof(*repo));
869 fatalx("%s: calloc", __func__);
871 STAILQ_INIT(&repo->rules);
872 TAILQ_INIT(&repo->protected_tag_namespaces);
873 TAILQ_INIT(&repo->protected_branch_namespaces);
874 TAILQ_INIT(&repo->protected_branches);
876 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
878 fatalx("%s: strlcpy", __func__);
880 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
887 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
888 int authorization, char *identifier)
890 struct gotd_access_rule *rule;
892 rule = calloc(1, sizeof(*rule));
896 rule->access = access;
897 rule->authorization = authorization;
898 rule->identifier = identifier;
900 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
904 refname_is_valid(char *refname)
906 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
907 yyerror("reference name must begin with \"refs/\": %s",
912 if (!got_ref_name_is_valid(refname)) {
913 yyerror("invalid reference name: %s", refname);
921 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
924 const struct got_error *error;
925 struct got_pathlist_entry *pe;
930 got_path_strip_trailing_slashes(namespace);
931 if (!refname_is_valid(namespace))
933 if (asprintf(&s, "%s/", namespace) == -1) {
934 yyerror("asprintf: %s", strerror(errno));
938 error = got_pathlist_insert(&pe, refs, s, NULL);
939 if (error || pe == NULL) {
942 yyerror("got_pathlist_insert: %s", error->msg);
944 yyerror("duplicate protected namespace %s", namespace);
953 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
955 struct got_pathlist_entry *pe;
958 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
962 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
963 if (strcmp(pe->path, new) == 0) {
964 yyerror("duplicate protected namespace %s", namespace);
973 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
975 struct got_pathlist_entry *pe;
978 if (conf_protect_ref_namespace(&new,
979 &repo->protected_branch_namespaces, namespace) == -1)
982 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
983 if (strcmp(pe->path, new) == 0) {
984 yyerror("duplicate protected namespace %s", namespace);
993 conf_protect_branch(struct gotd_repo *repo, char *branchname)
995 const struct got_error *error;
996 struct got_pathlist_entry *new;
999 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1000 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1001 yyerror("asprintf: %s", strerror(errno));
1005 refname = strdup(branchname);
1006 if (refname == NULL) {
1007 yyerror("strdup: %s", strerror(errno));
1012 if (!refname_is_valid(refname)) {
1017 error = got_pathlist_insert(&new, &repo->protected_branches,
1019 if (error || new == NULL) {
1022 yyerror("got_pathlist_insert: %s", error->msg);
1024 yyerror("duplicate protect branch %s", branchname);
1032 symset(const char *nam, const char *val, int persist)
1036 TAILQ_FOREACH(sym, &symhead, entry) {
1037 if (strcmp(nam, sym->nam) == 0)
1042 if (sym->persist == 1)
1047 TAILQ_REMOVE(&symhead, sym, entry);
1051 sym = calloc(1, sizeof(*sym));
1055 sym->nam = strdup(nam);
1056 if (sym->nam == NULL) {
1060 sym->val = strdup(val);
1061 if (sym->val == NULL) {
1067 sym->persist = persist;
1068 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1073 symget(const char *nam)
1077 TAILQ_FOREACH(sym, &symhead, entry) {
1078 if (strcmp(nam, sym->nam) == 0) {
1087 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1089 struct gotd_repo *repo;
1092 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1093 namelen = strlen(repo->name);
1094 if (strncmp(repo->name, repo_name, namelen) != 0)
1096 if (repo_name[namelen] == '\0' ||
1097 strcmp(&repo_name[namelen], ".git") == 0)
1105 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1107 struct gotd_repo *repo;
1109 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1110 if (strcmp(repo->path, repo_path) == 0)