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(char **,
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)) {
259 | BRANCH NAMESPACE STRING {
260 if (gotd_proc_id == PROC_GOTD ||
261 gotd_proc_id == PROC_REPO_WRITE) {
262 if (conf_protect_branch_namespace(new_repo,
271 if (gotd_proc_id == PROC_GOTD ||
272 gotd_proc_id == PROC_REPO_WRITE) {
273 if (conf_protect_branch(new_repo, $2)) {
282 repository : REPOSITORY STRING {
283 struct gotd_repo *repo;
285 TAILQ_FOREACH(repo, &gotd->repos, entry) {
286 if (strcmp(repo->name, $2) == 0) {
287 yyerror("duplicate repository '%s'", $2);
293 if (gotd_proc_id == PROC_GOTD ||
294 gotd_proc_id == PROC_AUTH ||
295 gotd_proc_id == PROC_REPO_WRITE) {
296 new_repo = conf_new_repo($2);
299 } '{' optnl repoopts2 '}' {
303 repoopts1 : PATH STRING {
304 if (gotd_proc_id == PROC_GOTD ||
305 gotd_proc_id == PROC_AUTH ||
306 gotd_proc_id == PROC_REPO_WRITE) {
307 if (!got_path_is_absolute($2)) {
308 yyerror("%s: path %s is not absolute",
313 if (realpath($2, new_repo->path) == NULL) {
314 yyerror("realpath %s: %s", $2, strerror(errno));
322 if (gotd_proc_id == PROC_AUTH) {
323 conf_new_access_rule(new_repo,
324 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
329 if (gotd_proc_id == PROC_AUTH) {
330 conf_new_access_rule(new_repo,
331 GOTD_ACCESS_PERMITTED,
332 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
337 if (gotd_proc_id == PROC_AUTH) {
338 conf_new_access_rule(new_repo,
339 GOTD_ACCESS_DENIED, 0, $2);
346 repoopts2 : repoopts2 repoopts1 nl
353 optnl : '\n' optnl /* zero or more newlines */
365 yyerror(const char *fmt, ...)
372 if (vasprintf(&msg, fmt, ap) == -1)
373 fatalx("yyerror vasprintf");
375 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
381 kw_cmp(const void *k, const void *e)
383 return (strcmp(k, ((const struct keywords *)e)->k_name));
389 /* This has to be sorted always. */
390 static const struct keywords keywords[] = {
391 { "branch", BRANCH },
392 { "connection", CONNECTION },
395 { "listen", LISTEN },
396 { "namespace", NAMESPACE },
399 { "permit", PERMIT },
400 { "protect", PROTECT },
401 { "repository", REPOSITORY },
402 { "request", REQUEST },
406 { "timeout", TIMEOUT },
409 const struct keywords *p;
411 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
412 sizeof(keywords[0]), kw_cmp);
420 #define MAXPUSHBACK 128
422 unsigned char *parsebuf;
424 unsigned char pushback_buffer[MAXPUSHBACK];
425 int pushback_index = 0;
433 /* Read character from the parsebuffer instead of input. */
434 if (parseindex >= 0) {
435 c = parsebuf[parseindex++];
444 return (pushback_buffer[--pushback_index]);
447 c = getc(file->stream);
449 yyerror("reached end of file while parsing "
454 c = getc(file->stream);
456 next = getc(file->stream);
461 yylval.lineno = file->lineno;
463 c = getc(file->stream);
479 if (pushback_index < MAXPUSHBACK-1)
480 return (pushback_buffer[pushback_index++] = c);
492 /* Skip to either EOF or the first real EOL. */
495 c = pushback_buffer[--pushback_index];
511 unsigned char buf[8096];
512 unsigned char *p, *val;
519 while (c == ' ' || c == '\t')
520 c = lgetc(0); /* nothing */
522 yylval.lineno = file->lineno;
525 while (c != '\n' && c != EOF)
526 c = lgetc(0); /* nothing */
528 if (c == '$' && parsebuf == NULL) {
534 if (p + 1 >= buf + sizeof(buf) - 1) {
535 yyerror("string too long");
538 if (isalnum(c) || c == '_') {
548 yyerror("macro '%s' not defined", buf);
567 } else if (c == '\\') {
568 next = lgetc(quotec);
571 if (next == quotec || c == ' ' || c == '\t')
573 else if (next == '\n') {
578 } else if (c == quotec) {
581 } else if (c == '\0') {
582 yyerror("syntax error");
585 if (p + 1 >= buf + sizeof(buf) - 1) {
586 yyerror("string too long");
591 yylval.v.string = strdup(buf);
592 if (yylval.v.string == NULL)
593 err(1, "yylex: strdup");
597 #define allowed_to_end_number(x) \
598 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
600 if (c == '-' || isdigit(c)) {
603 if ((unsigned)(p-buf) >= sizeof(buf)) {
604 yyerror("string too long");
608 } while (c != EOF && isdigit(c));
610 if (p == buf + 1 && buf[0] == '-')
612 if (c == EOF || allowed_to_end_number(c)) {
613 const char *errstr = NULL;
616 yylval.v.number = strtonum(buf, LLONG_MIN,
619 yyerror("\"%s\" invalid number: %s",
634 #define allowed_in_string(x) \
635 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
636 x != '{' && x != '}' && \
637 x != '!' && x != '=' && x != '#' && \
640 if (isalnum(c) || c == ':' || c == '_') {
643 if ((unsigned)(p-buf) >= sizeof(buf)) {
644 yyerror("string too long");
648 } while (c != EOF && (allowed_in_string(c)));
652 if (token == STRING) {
653 yylval.v.string = strdup(buf);
654 if (yylval.v.string == NULL)
655 err(1, "yylex: strdup");
660 yylval.lineno = file->lineno;
669 check_file_secrecy(int fd, const char *fname)
673 if (fstat(fd, &st)) {
674 log_warn("cannot stat %s", fname);
677 if (st.st_uid != 0 && st.st_uid != getuid()) {
678 log_warnx("%s: owner not root or current user", fname);
681 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
682 log_warnx("%s: group writable or world read/writable", fname);
689 newfile(const char *name, int secret, int required)
693 nfile = calloc(1, sizeof(struct file));
698 nfile->name = strdup(name);
699 if (nfile->name == NULL) {
704 nfile->stream = fopen(nfile->name, "r");
705 if (nfile->stream == NULL) {
707 log_warn("open %s", nfile->name);
712 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
713 fclose(nfile->stream);
723 closefile(struct file *xfile)
725 fclose(xfile->stream);
731 parse_config(const char *filename, enum gotd_procid proc_id,
732 struct gotd *env, int require_config_file)
734 struct sym *sym, *next;
735 struct gotd_repo *repo;
737 memset(env, 0, sizeof(*env));
740 gotd_proc_id = proc_id;
741 TAILQ_INIT(&gotd->repos);
743 /* Apply default values. */
744 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
745 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
746 fprintf(stderr, "%s: unix socket path too long", __func__);
749 if (strlcpy(gotd->user_name, GOTD_USER,
750 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
751 fprintf(stderr, "%s: user name too long", __func__);
755 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
756 gotd->request_timeout.tv_usec = 0;
758 file = newfile(filename, 0, require_config_file);
760 return require_config_file ? -1 : 0;
763 errors = file->errors;
766 /* Free macros and check which have not been used. */
767 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
768 if ((gotd->verbosity > 1) && !sym->used)
769 fprintf(stderr, "warning: macro '%s' not used\n",
774 TAILQ_REMOVE(&symhead, sym, entry);
782 TAILQ_FOREACH(repo, &gotd->repos, entry) {
783 if (repo->path[0] == '\0') {
784 log_warnx("repository \"%s\": no path provided in "
785 "configuration file", repo->name);
790 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
791 log_warnx("no repository defined in configuration file");
799 uid_connection_limit_cmp(const void *pa, const void *pb)
801 const struct gotd_uid_connection_limit *a = pa, *b = pb;
805 else if (a->uid > b->uid);
812 conf_limit_user_connections(const char *user, int maximum)
815 struct gotd_uid_connection_limit *limit;
819 yyerror("max connections cannot be smaller 1");
822 if (maximum > GOTD_MAXCLIENTS) {
823 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
827 if (gotd_auth_parseuid(user, &uid) == -1) {
828 yyerror("%s: no such user", user);
832 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
833 gotd->nconnection_limits, uid);
835 limit->max_connections = maximum;
839 limit = gotd->connection_limits;
840 nlimits = gotd->nconnection_limits + 1;
841 limit = reallocarray(limit, nlimits, sizeof(*limit));
843 fatal("reallocarray");
845 limit[nlimits - 1].uid = uid;
846 limit[nlimits - 1].max_connections = maximum;
848 gotd->connection_limits = limit;
849 gotd->nconnection_limits = nlimits;
850 qsort(gotd->connection_limits, gotd->nconnection_limits,
851 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
856 static struct gotd_repo *
857 conf_new_repo(const char *name)
859 struct gotd_repo *repo;
861 if (name[0] == '\0') {
862 fatalx("syntax error: empty repository name found in %s",
866 if (strchr(name, '\n') != NULL)
867 fatalx("repository names must not contain linefeeds: %s", name);
869 repo = calloc(1, sizeof(*repo));
871 fatalx("%s: calloc", __func__);
873 STAILQ_INIT(&repo->rules);
874 TAILQ_INIT(&repo->protected_tag_namespaces);
875 TAILQ_INIT(&repo->protected_branch_namespaces);
876 TAILQ_INIT(&repo->protected_branches);
878 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
880 fatalx("%s: strlcpy", __func__);
882 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
889 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
890 int authorization, char *identifier)
892 struct gotd_access_rule *rule;
894 rule = calloc(1, sizeof(*rule));
898 rule->access = access;
899 rule->authorization = authorization;
900 rule->identifier = identifier;
902 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
906 refname_is_valid(char *refname)
908 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
909 yyerror("reference name must begin with \"refs/\": %s",
914 if (!got_ref_name_is_valid(refname)) {
915 yyerror("invalid reference name: %s", refname);
923 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
926 const struct got_error *error;
927 struct got_pathlist_entry *pe;
932 got_path_strip_trailing_slashes(namespace);
933 if (!refname_is_valid(namespace))
935 if (asprintf(&s, "%s/", namespace) == -1) {
936 yyerror("asprintf: %s", strerror(errno));
940 error = got_pathlist_insert(&pe, refs, s, NULL);
941 if (error || pe == NULL) {
944 yyerror("got_pathlist_insert: %s", error->msg);
946 yyerror("duplicate protected namespace %s", namespace);
955 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
957 struct got_pathlist_entry *pe;
960 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
964 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
965 if (strcmp(pe->path, new) == 0) {
966 yyerror("duplicate protected namespace %s", namespace);
975 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
977 struct got_pathlist_entry *pe;
980 if (conf_protect_ref_namespace(&new,
981 &repo->protected_branch_namespaces, namespace) == -1)
984 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
985 if (strcmp(pe->path, new) == 0) {
986 yyerror("duplicate protected namespace %s", namespace);
995 conf_protect_branch(struct gotd_repo *repo, char *branchname)
997 const struct got_error *error;
998 struct got_pathlist_entry *new;
1001 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1002 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1003 yyerror("asprintf: %s", strerror(errno));
1007 refname = strdup(branchname);
1008 if (refname == NULL) {
1009 yyerror("strdup: %s", strerror(errno));
1014 if (!refname_is_valid(refname)) {
1019 error = got_pathlist_insert(&new, &repo->protected_branches,
1021 if (error || new == NULL) {
1024 yyerror("got_pathlist_insert: %s", error->msg);
1026 yyerror("duplicate protect branch %s", branchname);
1034 symset(const char *nam, const char *val, int persist)
1038 TAILQ_FOREACH(sym, &symhead, entry) {
1039 if (strcmp(nam, sym->nam) == 0)
1044 if (sym->persist == 1)
1049 TAILQ_REMOVE(&symhead, sym, entry);
1053 sym = calloc(1, sizeof(*sym));
1057 sym->nam = strdup(nam);
1058 if (sym->nam == NULL) {
1062 sym->val = strdup(val);
1063 if (sym->val == NULL) {
1069 sym->persist = persist;
1070 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1075 symget(const char *nam)
1079 TAILQ_FOREACH(sym, &symhead, entry) {
1080 if (strcmp(nam, sym->nam) == 0) {
1089 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1091 struct gotd_repo *repo;
1094 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1095 namelen = strlen(repo->name);
1096 if (strncmp(repo->name, repo_name, namelen) != 0)
1098 if (repo_name[namelen] == '\0' ||
1099 strcmp(&repo_name[namelen], ".git") == 0)
1107 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1109 struct gotd_repo *repo;
1111 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1112 if (strcmp(repo->path, repo_path) == 0)