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>
45 #include "got_error.h"
47 #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, 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 gotd *gotd;
91 static struct gotd_repo *new_repo;
92 static int conf_limit_user_connections(const char *, int);
93 static struct gotd_repo *conf_new_repo(const char *);
94 static void conf_new_access_rule(struct gotd_repo *,
95 enum gotd_access, int, char *);
96 static int conf_protect_ref_namespace(
97 struct got_pathlist_head *, char *);
98 static int conf_protect_tag_namespace(struct gotd_repo *,
100 static int conf_protect_branch_namespace(
101 struct gotd_repo *, char *);
102 static int conf_protect_branch(struct gotd_repo *,
104 static enum gotd_procid gotd_proc_id;
117 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
118 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
119 %token PROTECT NAMESPACE BRANCH TAG
121 %token <v.string> STRING
122 %token <v.number> NUMBER
130 | grammar repository '\n'
135 yyerror("invalid timeout: %lld", $1);
143 const char *type = "seconds";
148 yyerror("invalid number of seconds: %s", $1);
154 switch ($1[len - 1]) {
174 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
176 yyerror("number of %s is %s: %s", type,
187 main : LISTEN ON STRING {
188 if (!got_path_is_absolute($3))
189 yyerror("bad unix socket path \"%s\": "
190 "must be an absolute path", $3);
192 if (gotd_proc_id == PROC_LISTEN) {
193 if (strlcpy(gotd->unix_socket_path, $3,
194 sizeof(gotd->unix_socket_path)) >=
195 sizeof(gotd->unix_socket_path)) {
196 yyerror("%s: unix socket path too long",
205 if (strlcpy(gotd->user_name, $2,
206 sizeof(gotd->user_name)) >=
207 sizeof(gotd->user_name)) {
208 yyerror("%s: user name too long", __func__);
217 connection : CONNECTION '{' optnl conflags_l '}'
218 | CONNECTION conflags
220 conflags_l : conflags optnl conflags_l
224 conflags : REQUEST TIMEOUT timeout {
225 if ($3.tv_sec <= 0) {
226 yyerror("invalid timeout: %lld", $3.tv_sec);
229 memcpy(&gotd->request_timeout, &$3,
230 sizeof(gotd->request_timeout));
232 | LIMIT USER STRING NUMBER {
233 if (gotd_proc_id == PROC_LISTEN &&
234 conf_limit_user_connections($3, $4) == -1) {
242 protect : PROTECT '{' optnl protectflags_l '}'
243 | PROTECT protectflags
245 protectflags_l : protectflags optnl protectflags_l
249 protectflags : TAG NAMESPACE STRING {
250 if (gotd_proc_id == PROC_GOTD ||
251 gotd_proc_id == PROC_REPO_WRITE) {
252 if (conf_protect_tag_namespace(new_repo, $3)) {
258 | BRANCH NAMESPACE STRING {
259 if (gotd_proc_id == PROC_GOTD ||
260 gotd_proc_id == PROC_REPO_WRITE) {
261 if (conf_protect_branch_namespace(new_repo,
270 if (gotd_proc_id == PROC_GOTD ||
271 gotd_proc_id == PROC_REPO_WRITE) {
272 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);
326 if (gotd_proc_id == PROC_AUTH) {
327 conf_new_access_rule(new_repo,
328 GOTD_ACCESS_PERMITTED,
329 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
333 if (gotd_proc_id == PROC_AUTH) {
334 conf_new_access_rule(new_repo,
335 GOTD_ACCESS_DENIED, 0, $2);
341 repoopts2 : repoopts2 repoopts1 nl
348 optnl : '\n' optnl /* zero or more newlines */
360 yyerror(const char *fmt, ...)
367 if (vasprintf(&msg, fmt, ap) == -1)
368 fatalx("yyerror vasprintf");
370 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
376 kw_cmp(const void *k, const void *e)
378 return (strcmp(k, ((const struct keywords *)e)->k_name));
384 /* This has to be sorted always. */
385 static const struct keywords keywords[] = {
386 { "branch", BRANCH },
387 { "connection", CONNECTION },
390 { "listen", LISTEN },
391 { "namespace", NAMESPACE },
394 { "permit", PERMIT },
395 { "protect", PROTECT },
396 { "repository", REPOSITORY },
397 { "request", REQUEST },
401 { "timeout", TIMEOUT },
404 const struct keywords *p;
406 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
407 sizeof(keywords[0]), kw_cmp);
415 #define MAXPUSHBACK 128
417 unsigned char *parsebuf;
419 unsigned char pushback_buffer[MAXPUSHBACK];
420 int pushback_index = 0;
428 /* Read character from the parsebuffer instead of input. */
429 if (parseindex >= 0) {
430 c = parsebuf[parseindex++];
439 return (pushback_buffer[--pushback_index]);
442 c = getc(file->stream);
444 yyerror("reached end of file while parsing "
449 c = getc(file->stream);
451 next = getc(file->stream);
456 yylval.lineno = file->lineno;
458 c = getc(file->stream);
474 if (pushback_index < MAXPUSHBACK-1)
475 return (pushback_buffer[pushback_index++] = c);
487 /* Skip to either EOF or the first real EOL. */
490 c = pushback_buffer[--pushback_index];
506 unsigned char buf[8096];
507 unsigned char *p, *val;
514 while (c == ' ' || c == '\t')
515 c = lgetc(0); /* nothing */
517 yylval.lineno = file->lineno;
520 while (c != '\n' && c != EOF)
521 c = lgetc(0); /* nothing */
523 if (c == '$' && parsebuf == NULL) {
529 if (p + 1 >= buf + sizeof(buf) - 1) {
530 yyerror("string too long");
533 if (isalnum(c) || c == '_') {
543 yyerror("macro '%s' not defined", buf);
562 } else if (c == '\\') {
563 next = lgetc(quotec);
566 if (next == quotec || c == ' ' || c == '\t')
568 else if (next == '\n') {
573 } else if (c == quotec) {
576 } else if (c == '\0') {
577 yyerror("syntax error");
580 if (p + 1 >= buf + sizeof(buf) - 1) {
581 yyerror("string too long");
586 yylval.v.string = strdup(buf);
587 if (yylval.v.string == NULL)
588 err(1, "yylex: strdup");
592 #define allowed_to_end_number(x) \
593 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
595 if (c == '-' || isdigit(c)) {
598 if ((unsigned)(p-buf) >= sizeof(buf)) {
599 yyerror("string too long");
603 } while (c != EOF && isdigit(c));
605 if (p == buf + 1 && buf[0] == '-')
607 if (c == EOF || allowed_to_end_number(c)) {
608 const char *errstr = NULL;
611 yylval.v.number = strtonum(buf, LLONG_MIN,
614 yyerror("\"%s\" invalid number: %s",
629 #define allowed_in_string(x) \
630 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
631 x != '{' && x != '}' && \
632 x != '!' && x != '=' && x != '#' && \
635 if (isalnum(c) || c == ':' || c == '_') {
638 if ((unsigned)(p-buf) >= sizeof(buf)) {
639 yyerror("string too long");
643 } while (c != EOF && (allowed_in_string(c)));
647 if (token == STRING) {
648 yylval.v.string = strdup(buf);
649 if (yylval.v.string == NULL)
650 err(1, "yylex: strdup");
655 yylval.lineno = file->lineno;
664 check_file_secrecy(int fd, const char *fname)
668 if (fstat(fd, &st)) {
669 log_warn("cannot stat %s", fname);
672 if (st.st_uid != 0 && st.st_uid != getuid()) {
673 log_warnx("%s: owner not root or current user", fname);
676 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
677 log_warnx("%s: group writable or world read/writable", fname);
684 newfile(const char *name, int secret, int required)
688 nfile = calloc(1, sizeof(struct file));
693 nfile->name = strdup(name);
694 if (nfile->name == NULL) {
699 nfile->stream = fopen(nfile->name, "r");
700 if (nfile->stream == NULL) {
702 log_warn("open %s", nfile->name);
707 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
708 fclose(nfile->stream);
718 closefile(struct file *xfile)
720 fclose(xfile->stream);
726 parse_config(const char *filename, enum gotd_procid proc_id,
727 struct gotd *env, int require_config_file)
729 struct sym *sym, *next;
730 struct gotd_repo *repo;
732 memset(env, 0, sizeof(*env));
735 gotd_proc_id = proc_id;
736 TAILQ_INIT(&gotd->repos);
738 /* Apply default values. */
739 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
740 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
741 fprintf(stderr, "%s: unix socket path too long", __func__);
744 if (strlcpy(gotd->user_name, GOTD_USER,
745 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
746 fprintf(stderr, "%s: user name too long", __func__);
750 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
751 gotd->request_timeout.tv_usec = 0;
753 file = newfile(filename, 0, require_config_file);
755 return require_config_file ? -1 : 0;
758 errors = file->errors;
761 /* Free macros and check which have not been used. */
762 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
763 if ((gotd->verbosity > 1) && !sym->used)
764 fprintf(stderr, "warning: macro '%s' not used\n",
769 TAILQ_REMOVE(&symhead, sym, entry);
777 TAILQ_FOREACH(repo, &gotd->repos, entry) {
778 if (repo->path[0] == '\0') {
779 log_warnx("repository \"%s\": no path provided in "
780 "configuration file", repo->name);
785 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
786 log_warnx("no repository defined in configuration file");
794 uid_connection_limit_cmp(const void *pa, const void *pb)
796 const struct gotd_uid_connection_limit *a = pa, *b = pb;
800 else if (a->uid > b->uid);
807 conf_limit_user_connections(const char *user, int maximum)
810 struct gotd_uid_connection_limit *limit;
814 yyerror("max connections cannot be smaller 1");
817 if (maximum > GOTD_MAXCLIENTS) {
818 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
822 if (gotd_auth_parseuid(user, &uid) == -1) {
823 yyerror("%s: no such user", user);
827 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
828 gotd->nconnection_limits, uid);
830 limit->max_connections = maximum;
834 limit = gotd->connection_limits;
835 nlimits = gotd->nconnection_limits + 1;
836 limit = reallocarray(limit, nlimits, sizeof(*limit));
838 fatal("reallocarray");
840 limit[nlimits - 1].uid = uid;
841 limit[nlimits - 1].max_connections = maximum;
843 gotd->connection_limits = limit;
844 gotd->nconnection_limits = nlimits;
845 qsort(gotd->connection_limits, gotd->nconnection_limits,
846 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
851 static struct gotd_repo *
852 conf_new_repo(const char *name)
854 struct gotd_repo *repo;
856 if (name[0] == '\0') {
857 fatalx("syntax error: empty repository name found in %s",
861 if (strchr(name, '\n') != NULL)
862 fatalx("repository names must not contain linefeeds: %s", name);
864 repo = calloc(1, sizeof(*repo));
866 fatalx("%s: calloc", __func__);
868 STAILQ_INIT(&repo->rules);
869 TAILQ_INIT(&repo->protected_tag_namespaces);
870 TAILQ_INIT(&repo->protected_branch_namespaces);
871 TAILQ_INIT(&repo->protected_branches);
873 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
875 fatalx("%s: strlcpy", __func__);
877 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
884 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
885 int authorization, char *identifier)
887 struct gotd_access_rule *rule;
889 rule = calloc(1, sizeof(*rule));
893 rule->access = access;
894 rule->authorization = authorization;
895 rule->identifier = identifier;
897 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
901 refname_is_valid(char *refname)
903 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
904 yyerror("reference name must begin with \"refs/\": %s",
909 if (!got_ref_name_is_valid(refname)) {
910 yyerror("invalid reference name: %s", refname);
918 conf_protect_ref_namespace(struct got_pathlist_head *refs, char *namespace)
920 const struct got_error *error;
921 struct got_pathlist_entry *new;
924 got_path_strip_trailing_slashes(namespace);
925 if (!refname_is_valid(namespace))
927 if (asprintf(&s, "%s/", namespace) == -1) {
928 yyerror("asprintf: %s", strerror(errno));
932 error = got_pathlist_insert(&new, refs, s, NULL);
933 if (error || new == NULL) {
936 yyerror("got_pathlist_insert: %s", error->msg);
938 yyerror("duplicate protect namespace %s", namespace);
946 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
948 return conf_protect_ref_namespace(&repo->protected_tag_namespaces,
953 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
955 return conf_protect_ref_namespace(&repo->protected_branch_namespaces,
960 conf_protect_branch(struct gotd_repo *repo, char *branchname)
962 const struct got_error *error;
963 struct got_pathlist_entry *new;
966 if (strncmp(branchname, "refs/heads/", 11) != 0) {
967 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
968 yyerror("asprintf: %s", strerror(errno));
972 refname = strdup(branchname);
973 if (refname == NULL) {
974 yyerror("strdup: %s", strerror(errno));
979 if (!refname_is_valid(refname)) {
984 error = got_pathlist_insert(&new, &repo->protected_branches,
986 if (error || new == NULL) {
989 yyerror("got_pathlist_insert: %s", error->msg);
991 yyerror("duplicate protect branch %s", branchname);
999 symset(const char *nam, const char *val, int persist)
1003 TAILQ_FOREACH(sym, &symhead, entry) {
1004 if (strcmp(nam, sym->nam) == 0)
1009 if (sym->persist == 1)
1014 TAILQ_REMOVE(&symhead, sym, entry);
1018 sym = calloc(1, sizeof(*sym));
1022 sym->nam = strdup(nam);
1023 if (sym->nam == NULL) {
1027 sym->val = strdup(val);
1028 if (sym->val == NULL) {
1034 sym->persist = persist;
1035 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1040 symget(const char *nam)
1044 TAILQ_FOREACH(sym, &symhead, entry) {
1045 if (strcmp(nam, sym->nam) == 0) {
1054 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1056 struct gotd_repo *repo;
1059 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1060 namelen = strlen(repo->name);
1061 if (strncmp(repo->name, repo_name, namelen) != 0)
1063 if (repo_name[namelen] == '\0' ||
1064 strcmp(&repo_name[namelen], ".git") == 0)
1072 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1074 struct gotd_repo *repo;
1076 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1077 if (strcmp(repo->path, repo_path) == 0)