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.
25 #include "got_compat.h"
28 #include <sys/types.h>
29 #include <sys/queue.h>
46 #include "got_error.h"
48 #include "got_reference.h"
55 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
57 TAILQ_ENTRY(file) entry;
63 struct file *newfile(const char *, int, int);
64 static void closefile(struct file *);
65 int check_file_secrecy(int, const char *);
68 int yyerror(const char *, ...)
69 __attribute__((__format__ (printf, 1, 2)))
70 __attribute__((__nonnull__ (1)));
71 int kw_cmp(const void *, const void *);
76 static char *port_sprintf(int);
78 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
80 TAILQ_ENTRY(sym) entry;
87 int symset(const char *, const char *, int);
88 char *symget(const char *);
92 static struct gotd *gotd;
93 static struct gotd_repo *new_repo;
94 static int conf_limit_user_connections(const char *, int);
95 static struct gotd_repo *conf_new_repo(const char *);
96 static void conf_new_access_rule(struct gotd_repo *,
97 enum gotd_access, int, char *);
98 static int conf_protect_ref_namespace(char **,
99 struct got_pathlist_head *, char *);
100 static int conf_protect_tag_namespace(struct gotd_repo *,
102 static int conf_protect_branch_namespace(
103 struct gotd_repo *, char *);
104 static int conf_protect_branch(struct gotd_repo *,
106 static int conf_notify_branch(struct gotd_repo *,
108 static int conf_notify_ref_namespace(struct gotd_repo *,
110 static int conf_notify_email(struct gotd_repo *,
111 char *, char *, char *, char *, char *);
112 static int conf_notify_http(struct gotd_repo *,
113 char *, char *, char *);
114 static enum gotd_procid gotd_proc_id;
127 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
128 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
129 %token PROTECT NAMESPACE BRANCH TAG REFERENCE RELAY PORT
130 %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD
132 %token <v.string> STRING
133 %token <v.number> NUMBER
140 | grammar varset '\n'
142 | grammar repository '\n'
145 varset : STRING '=' STRING {
148 if (isspace((unsigned char)*s)) {
149 yyerror("macro name cannot contain "
156 if (symset($1, $3, 0) == -1)
157 fatal("cannot store variable");
165 yyerror("invalid timeout: %lld", $1);
173 const char *type = "seconds";
178 yyerror("invalid number of seconds: %s", $1);
184 switch ($1[len - 1]) {
204 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
206 yyerror("number of %s is %s: %s", type,
217 main : LISTEN ON STRING {
218 if (!got_path_is_absolute($3))
219 yyerror("bad unix socket path \"%s\": "
220 "must be an absolute path", $3);
222 if (gotd_proc_id == PROC_LISTEN) {
223 if (strlcpy(gotd->unix_socket_path, $3,
224 sizeof(gotd->unix_socket_path)) >=
225 sizeof(gotd->unix_socket_path)) {
226 yyerror("%s: unix socket path too long",
235 if (strlcpy(gotd->user_name, $2,
236 sizeof(gotd->user_name)) >=
237 sizeof(gotd->user_name)) {
238 yyerror("%s: user name too long", __func__);
247 connection : CONNECTION '{' optnl conflags_l '}'
248 | CONNECTION conflags
250 conflags_l : conflags optnl conflags_l
254 conflags : REQUEST TIMEOUT timeout {
255 if ($3.tv_sec <= 0) {
256 yyerror("invalid timeout: %lld",
257 (long long)$3.tv_sec);
260 memcpy(&gotd->request_timeout, &$3,
261 sizeof(gotd->request_timeout));
263 | LIMIT USER STRING NUMBER {
264 if (gotd_proc_id == PROC_LISTEN &&
265 conf_limit_user_connections($3, $4) == -1) {
273 protect : PROTECT '{' optnl protectflags_l '}'
274 | PROTECT protectflags
276 protectflags_l : protectflags optnl protectflags_l
280 protectflags : TAG NAMESPACE STRING {
281 if (gotd_proc_id == PROC_GOTD ||
282 gotd_proc_id == PROC_REPO_WRITE) {
283 if (conf_protect_tag_namespace(new_repo, $3)) {
290 | BRANCH NAMESPACE STRING {
291 if (gotd_proc_id == PROC_GOTD ||
292 gotd_proc_id == PROC_REPO_WRITE) {
293 if (conf_protect_branch_namespace(new_repo,
302 if (gotd_proc_id == PROC_GOTD ||
303 gotd_proc_id == PROC_REPO_WRITE) {
304 if (conf_protect_branch(new_repo, $2)) {
313 notify : NOTIFY '{' optnl notifyflags_l '}'
316 notifyflags_l : notifyflags optnl notifyflags_l
320 notifyflags : BRANCH STRING {
321 if (gotd_proc_id == PROC_GOTD ||
322 gotd_proc_id == PROC_SESSION_WRITE ||
323 gotd_proc_id == PROC_NOTIFY) {
324 if (conf_notify_branch(new_repo, $2)) {
331 | REFERENCE NAMESPACE STRING {
332 if (gotd_proc_id == PROC_GOTD ||
333 gotd_proc_id == PROC_SESSION_WRITE ||
334 gotd_proc_id == PROC_NOTIFY) {
335 if (conf_notify_ref_namespace(new_repo, $3)) {
343 if (gotd_proc_id == PROC_GOTD ||
344 gotd_proc_id == PROC_SESSION_WRITE ||
345 gotd_proc_id == PROC_NOTIFY) {
346 if (conf_notify_email(new_repo, NULL, $3,
354 | EMAIL FROM STRING TO STRING {
355 if (gotd_proc_id == PROC_GOTD ||
356 gotd_proc_id == PROC_SESSION_WRITE ||
357 gotd_proc_id == PROC_NOTIFY) {
358 if (conf_notify_email(new_repo, $3, $5,
368 | EMAIL TO STRING REPLY TO STRING {
369 if (gotd_proc_id == PROC_GOTD ||
370 gotd_proc_id == PROC_SESSION_WRITE ||
371 gotd_proc_id == PROC_NOTIFY) {
372 if (conf_notify_email(new_repo, NULL, $3,
382 | EMAIL FROM STRING TO STRING REPLY TO STRING {
383 if (gotd_proc_id == PROC_GOTD ||
384 gotd_proc_id == PROC_SESSION_WRITE ||
385 gotd_proc_id == PROC_NOTIFY) {
386 if (conf_notify_email(new_repo, $3, $5,
398 | EMAIL TO STRING RELAY STRING {
399 if (gotd_proc_id == PROC_GOTD ||
400 gotd_proc_id == PROC_SESSION_WRITE ||
401 gotd_proc_id == PROC_NOTIFY) {
402 if (conf_notify_email(new_repo, NULL, $3,
412 | EMAIL FROM STRING TO STRING RELAY STRING {
413 if (gotd_proc_id == PROC_GOTD ||
414 gotd_proc_id == PROC_SESSION_WRITE ||
415 gotd_proc_id == PROC_NOTIFY) {
416 if (conf_notify_email(new_repo, $3, $5,
428 | EMAIL TO STRING REPLY TO STRING RELAY STRING {
429 if (gotd_proc_id == PROC_GOTD ||
430 gotd_proc_id == PROC_SESSION_WRITE ||
431 gotd_proc_id == PROC_NOTIFY) {
432 if (conf_notify_email(new_repo, NULL, $3,
444 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
445 if (gotd_proc_id == PROC_GOTD ||
446 gotd_proc_id == PROC_SESSION_WRITE ||
447 gotd_proc_id == PROC_NOTIFY) {
448 if (conf_notify_email(new_repo, $3, $5,
462 | EMAIL TO STRING RELAY STRING PORT STRING {
463 if (gotd_proc_id == PROC_GOTD ||
464 gotd_proc_id == PROC_SESSION_WRITE ||
465 gotd_proc_id == PROC_NOTIFY) {
466 if (conf_notify_email(new_repo, NULL, $3,
478 | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
479 if (gotd_proc_id == PROC_GOTD ||
480 gotd_proc_id == PROC_SESSION_WRITE ||
481 gotd_proc_id == PROC_NOTIFY) {
482 if (conf_notify_email(new_repo, $3, $5,
496 | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
497 if (gotd_proc_id == PROC_GOTD ||
498 gotd_proc_id == PROC_SESSION_WRITE ||
499 gotd_proc_id == PROC_NOTIFY) {
500 if (conf_notify_email(new_repo, NULL, $3,
514 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
515 if (gotd_proc_id == PROC_GOTD ||
516 gotd_proc_id == PROC_SESSION_WRITE ||
517 gotd_proc_id == PROC_NOTIFY) {
518 if (conf_notify_email(new_repo, $3, $5,
534 | EMAIL TO STRING RELAY STRING PORT NUMBER {
535 if (gotd_proc_id == PROC_GOTD ||
536 gotd_proc_id == PROC_SESSION_WRITE ||
537 gotd_proc_id == PROC_NOTIFY) {
538 if (conf_notify_email(new_repo, NULL, $3,
539 NULL, $5, port_sprintf($7))) {
548 | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
549 if (gotd_proc_id == PROC_GOTD ||
550 gotd_proc_id == PROC_SESSION_WRITE ||
551 gotd_proc_id == PROC_NOTIFY) {
552 if (conf_notify_email(new_repo, $3, $5,
553 NULL, $7, port_sprintf($9))) {
564 | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
565 if (gotd_proc_id == PROC_GOTD ||
566 gotd_proc_id == PROC_SESSION_WRITE ||
567 gotd_proc_id == PROC_NOTIFY) {
568 if (conf_notify_email(new_repo, NULL, $3,
569 $6, $8, port_sprintf($10))) {
580 | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
581 if (gotd_proc_id == PROC_GOTD ||
582 gotd_proc_id == PROC_SESSION_WRITE ||
583 gotd_proc_id == PROC_NOTIFY) {
584 if (conf_notify_email(new_repo, $3, $5,
585 $8, $10, port_sprintf($12))) {
599 if (gotd_proc_id == PROC_GOTD ||
600 gotd_proc_id == PROC_SESSION_WRITE ||
601 gotd_proc_id == PROC_NOTIFY) {
602 if (conf_notify_http(new_repo, $2, NULL,
610 | URL STRING USER STRING PASSWORD STRING {
611 if (gotd_proc_id == PROC_GOTD ||
612 gotd_proc_id == PROC_SESSION_WRITE ||
613 gotd_proc_id == PROC_NOTIFY) {
614 if (conf_notify_http(new_repo, $2, $4, $6)) {
627 repository : REPOSITORY STRING {
628 struct gotd_repo *repo;
630 TAILQ_FOREACH(repo, &gotd->repos, entry) {
631 if (strcmp(repo->name, $2) == 0) {
632 yyerror("duplicate repository '%s'", $2);
638 if (gotd_proc_id == PROC_GOTD ||
639 gotd_proc_id == PROC_AUTH ||
640 gotd_proc_id == PROC_REPO_WRITE ||
641 gotd_proc_id == PROC_SESSION_WRITE ||
642 gotd_proc_id == PROC_GITWRAPPER |
643 gotd_proc_id == PROC_NOTIFY) {
644 new_repo = conf_new_repo($2);
647 } '{' optnl repoopts2 '}' {
651 repoopts1 : PATH STRING {
652 if (gotd_proc_id == PROC_GOTD ||
653 gotd_proc_id == PROC_AUTH ||
654 gotd_proc_id == PROC_REPO_WRITE ||
655 gotd_proc_id == PROC_SESSION_WRITE ||
656 gotd_proc_id == PROC_GITWRAPPER ||
657 gotd_proc_id == PROC_NOTIFY) {
658 if (!got_path_is_absolute($2)) {
659 yyerror("%s: path %s is not absolute",
664 if (realpath($2, new_repo->path) == NULL) {
666 * To give admins a chance to create
667 * missing repositories at run-time
668 * we only warn about ENOENT here.
670 * And ignore 'permission denied' when
671 * running in gitwrapper. Users may be
672 * able to access this repository via
675 if (errno == ENOENT) {
676 yyerror("realpath %s: %s", $2,
678 } else if (errno != EACCES ||
679 gotd_proc_id != PROC_GITWRAPPER) {
680 yyerror("realpath %s: %s", $2,
686 if (strlcpy(new_repo->path, $2,
687 sizeof(new_repo->path)) >=
688 sizeof(new_repo->path))
689 yyerror("path too long");
695 if (gotd_proc_id == PROC_AUTH) {
696 conf_new_access_rule(new_repo,
697 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
702 if (gotd_proc_id == PROC_AUTH) {
703 conf_new_access_rule(new_repo,
704 GOTD_ACCESS_PERMITTED,
705 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
710 if (gotd_proc_id == PROC_AUTH) {
711 conf_new_access_rule(new_repo,
712 GOTD_ACCESS_DENIED, 0, $2);
720 repoopts2 : repoopts2 repoopts1 nl
727 optnl : '\n' optnl /* zero or more newlines */
739 yyerror(const char *fmt, ...)
746 if (vasprintf(&msg, fmt, ap) == -1)
747 fatalx("yyerror vasprintf");
749 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
755 kw_cmp(const void *k, const void *e)
757 return (strcmp(k, ((const struct keywords *)e)->k_name));
763 /* This has to be sorted always. */
764 static const struct keywords keywords[] = {
765 { "branch", BRANCH },
766 { "connection", CONNECTION },
771 { "listen", LISTEN },
772 { "namespace", NAMESPACE },
773 { "notify", NOTIFY },
775 { "password", PASSWORD },
777 { "permit", PERMIT },
779 { "protect", PROTECT },
780 { "reference", REFERENCE },
783 { "repository", REPOSITORY },
784 { "request", REQUEST },
788 { "timeout", TIMEOUT },
793 const struct keywords *p;
795 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
796 sizeof(keywords[0]), kw_cmp);
804 #define MAXPUSHBACK 128
806 unsigned char *parsebuf;
808 unsigned char pushback_buffer[MAXPUSHBACK];
809 int pushback_index = 0;
817 /* Read character from the parsebuffer instead of input. */
818 if (parseindex >= 0) {
819 c = parsebuf[parseindex++];
828 return (pushback_buffer[--pushback_index]);
831 c = getc(file->stream);
833 yyerror("reached end of file while parsing "
838 c = getc(file->stream);
840 next = getc(file->stream);
845 yylval.lineno = file->lineno;
847 c = getc(file->stream);
863 if (pushback_index < MAXPUSHBACK-1)
864 return (pushback_buffer[pushback_index++] = c);
876 /* Skip to either EOF or the first real EOL. */
879 c = pushback_buffer[--pushback_index];
895 unsigned char buf[8096];
896 unsigned char *p, *val;
903 while (c == ' ' || c == '\t')
904 c = lgetc(0); /* nothing */
906 yylval.lineno = file->lineno;
909 while (c != '\n' && c != EOF)
910 c = lgetc(0); /* nothing */
912 if (c == '$' && parsebuf == NULL) {
918 if (p + 1 >= buf + sizeof(buf) - 1) {
919 yyerror("string too long");
922 if (isalnum(c) || c == '_') {
932 yyerror("macro '%s' not defined", buf);
951 } else if (c == '\\') {
952 next = lgetc(quotec);
955 if (next == quotec || c == ' ' || c == '\t')
957 else if (next == '\n') {
962 } else if (c == quotec) {
965 } else if (c == '\0') {
966 yyerror("syntax error");
969 if (p + 1 >= buf + sizeof(buf) - 1) {
970 yyerror("string too long");
975 yylval.v.string = strdup(buf);
976 if (yylval.v.string == NULL)
977 err(1, "yylex: strdup");
981 #define allowed_to_end_number(x) \
982 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
984 if (c == '-' || isdigit(c)) {
987 if ((unsigned)(p-buf) >= sizeof(buf)) {
988 yyerror("string too long");
992 } while (c != EOF && isdigit(c));
994 if (p == buf + 1 && buf[0] == '-')
996 if (c == EOF || allowed_to_end_number(c)) {
997 const char *errstr = NULL;
1000 yylval.v.number = strtonum(buf, LLONG_MIN,
1001 LLONG_MAX, &errstr);
1003 yyerror("\"%s\" invalid number: %s",
1018 #define allowed_in_string(x) \
1019 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1020 x != '{' && x != '}' && \
1021 x != '!' && x != '=' && x != '#' && \
1024 if (isalnum(c) || c == ':' || c == '_') {
1027 if ((unsigned)(p-buf) >= sizeof(buf)) {
1028 yyerror("string too long");
1032 } while (c != EOF && (allowed_in_string(c)));
1035 token = lookup(buf);
1036 if (token == STRING) {
1037 yylval.v.string = strdup(buf);
1038 if (yylval.v.string == NULL)
1039 err(1, "yylex: strdup");
1044 yylval.lineno = file->lineno;
1053 check_file_secrecy(int fd, const char *fname)
1057 if (fstat(fd, &st)) {
1058 log_warn("cannot stat %s", fname);
1061 if (st.st_uid != 0 && st.st_uid != getuid()) {
1062 log_warnx("%s: owner not root or current user", fname);
1065 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1066 log_warnx("%s: group writable or world read/writable", fname);
1073 newfile(const char *name, int secret, int required)
1077 nfile = calloc(1, sizeof(struct file));
1078 if (nfile == NULL) {
1082 nfile->name = strdup(name);
1083 if (nfile->name == NULL) {
1088 nfile->stream = fopen(nfile->name, "r");
1089 if (nfile->stream == NULL) {
1091 log_warn("open %s", nfile->name);
1095 } else if (secret &&
1096 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1097 fclose(nfile->stream);
1107 closefile(struct file *xfile)
1109 fclose(xfile->stream);
1115 parse_config(const char *filename, enum gotd_procid proc_id,
1118 struct sym *sym, *next;
1119 struct gotd_repo *repo;
1120 int require_config_file = (proc_id != PROC_GITWRAPPER);
1122 memset(env, 0, sizeof(*env));
1125 gotd_proc_id = proc_id;
1126 TAILQ_INIT(&gotd->repos);
1128 /* Apply default values. */
1129 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1130 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1131 fprintf(stderr, "%s: unix socket path too long", __func__);
1134 if (strlcpy(gotd->user_name, GOTD_USER,
1135 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1136 fprintf(stderr, "%s: user name too long", __func__);
1140 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1141 gotd->request_timeout.tv_usec = 0;
1143 file = newfile(filename, 0, require_config_file);
1145 return require_config_file ? -1 : 0;
1148 errors = file->errors;
1151 /* Free macros and check which have not been used. */
1152 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1153 if ((gotd->verbosity > 1) && !sym->used)
1154 fprintf(stderr, "warning: macro '%s' not used\n",
1156 if (!sym->persist) {
1159 TAILQ_REMOVE(&symhead, sym, entry);
1167 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1168 if (repo->path[0] == '\0') {
1169 log_warnx("repository \"%s\": no path provided in "
1170 "configuration file", repo->name);
1175 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1176 log_warnx("no repository defined in configuration file");
1184 uid_connection_limit_cmp(const void *pa, const void *pb)
1186 const struct gotd_uid_connection_limit *a = pa, *b = pb;
1188 if (a->uid < b->uid)
1190 else if (a->uid > b->uid);
1197 conf_limit_user_connections(const char *user, int maximum)
1200 struct gotd_uid_connection_limit *limit;
1204 yyerror("max connections cannot be smaller 1");
1207 if (maximum > GOTD_MAXCLIENTS) {
1208 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1212 if (gotd_parseuid(user, &uid) == -1) {
1213 yyerror("%s: no such user", user);
1217 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1218 gotd->nconnection_limits, uid);
1220 limit->max_connections = maximum;
1224 limit = gotd->connection_limits;
1225 nlimits = gotd->nconnection_limits + 1;
1226 limit = reallocarray(limit, nlimits, sizeof(*limit));
1228 fatal("reallocarray");
1230 limit[nlimits - 1].uid = uid;
1231 limit[nlimits - 1].max_connections = maximum;
1233 gotd->connection_limits = limit;
1234 gotd->nconnection_limits = nlimits;
1235 qsort(gotd->connection_limits, gotd->nconnection_limits,
1236 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1241 static struct gotd_repo *
1242 conf_new_repo(const char *name)
1244 struct gotd_repo *repo;
1246 if (name[0] == '\0') {
1247 fatalx("syntax error: empty repository name found in %s",
1251 if (strchr(name, '\n') != NULL)
1252 fatalx("repository names must not contain linefeeds: %s", name);
1254 repo = calloc(1, sizeof(*repo));
1256 fatalx("%s: calloc", __func__);
1258 STAILQ_INIT(&repo->rules);
1259 TAILQ_INIT(&repo->protected_tag_namespaces);
1260 TAILQ_INIT(&repo->protected_branch_namespaces);
1261 TAILQ_INIT(&repo->protected_branches);
1262 TAILQ_INIT(&repo->protected_branches);
1263 TAILQ_INIT(&repo->notification_refs);
1264 TAILQ_INIT(&repo->notification_ref_namespaces);
1265 STAILQ_INIT(&repo->notification_targets);
1267 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1269 fatalx("%s: strlcpy", __func__);
1271 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1278 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1279 int authorization, char *identifier)
1281 struct gotd_access_rule *rule;
1283 rule = calloc(1, sizeof(*rule));
1287 rule->access = access;
1288 rule->authorization = authorization;
1289 rule->identifier = identifier;
1291 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1295 refname_is_valid(char *refname)
1297 if (strncmp(refname, "refs/", 5) != 0) {
1298 yyerror("reference name must begin with \"refs/\": %s",
1303 if (!got_ref_name_is_valid(refname)) {
1304 yyerror("invalid reference name: %s", refname);
1312 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1315 const struct got_error *error;
1316 struct got_pathlist_entry *pe;
1321 got_path_strip_trailing_slashes(namespace);
1322 if (!refname_is_valid(namespace))
1324 if (asprintf(&s, "%s/", namespace) == -1) {
1325 yyerror("asprintf: %s", strerror(errno));
1329 error = got_pathlist_insert(&pe, refs, s, NULL);
1330 if (error || pe == NULL) {
1333 yyerror("got_pathlist_insert: %s", error->msg);
1335 yyerror("duplicate protected namespace %s", namespace);
1344 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1346 struct got_pathlist_entry *pe;
1349 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1353 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1354 if (strcmp(pe->path, new) == 0) {
1355 yyerror("duplicate protected namespace %s", namespace);
1364 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1366 struct got_pathlist_entry *pe;
1369 if (conf_protect_ref_namespace(&new,
1370 &repo->protected_branch_namespaces, namespace) == -1)
1373 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1374 if (strcmp(pe->path, new) == 0) {
1375 yyerror("duplicate protected namespace %s", namespace);
1384 conf_protect_branch(struct gotd_repo *repo, char *branchname)
1386 const struct got_error *error;
1387 struct got_pathlist_entry *new;
1390 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1391 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1392 yyerror("asprintf: %s", strerror(errno));
1396 refname = strdup(branchname);
1397 if (refname == NULL) {
1398 yyerror("strdup: %s", strerror(errno));
1403 if (!refname_is_valid(refname)) {
1408 error = got_pathlist_insert(&new, &repo->protected_branches,
1410 if (error || new == NULL) {
1413 yyerror("got_pathlist_insert: %s", error->msg);
1415 yyerror("duplicate protect branch %s", branchname);
1423 conf_notify_branch(struct gotd_repo *repo, char *branchname)
1425 const struct got_error *error;
1426 struct got_pathlist_entry *pe;
1429 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1430 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1431 yyerror("asprintf: %s", strerror(errno));
1435 refname = strdup(branchname);
1436 if (refname == NULL) {
1437 yyerror("strdup: %s", strerror(errno));
1442 if (!refname_is_valid(refname)) {
1447 error = got_pathlist_insert(&pe, &repo->notification_refs,
1451 yyerror("got_pathlist_insert: %s", error->msg);
1461 conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1463 const struct got_error *error;
1464 struct got_pathlist_entry *pe;
1467 got_path_strip_trailing_slashes(namespace);
1468 if (!refname_is_valid(namespace))
1471 if (asprintf(&s, "%s/", namespace) == -1) {
1472 yyerror("asprintf: %s", strerror(errno));
1476 error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1480 yyerror("got_pathlist_insert: %s", error->msg);
1490 conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1491 char *responder, char *hostname, char *port)
1493 struct gotd_notification_target *target;
1495 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1496 if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1498 if (strcmp(target->conf.email.recipient, recipient) == 0) {
1499 yyerror("duplicate email notification for '%s' in "
1500 "repository '%s'", recipient, repo->name);
1505 target = calloc(1, sizeof(*target));
1508 target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1510 target->conf.email.sender = strdup(sender);
1511 if (target->conf.email.sender == NULL)
1514 target->conf.email.recipient = strdup(recipient);
1515 if (target->conf.email.recipient == NULL)
1518 target->conf.email.responder = strdup(responder);
1519 if (target->conf.email.responder == NULL)
1523 target->conf.email.hostname = strdup(hostname);
1524 if (target->conf.email.hostname == NULL)
1528 target->conf.email.port = strdup(port);
1529 if (target->conf.email.port == NULL)
1533 STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1538 conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password)
1540 const struct got_error *error;
1541 struct gotd_notification_target *target;
1542 char *proto, *hostname, *port, *path;
1543 int tls = 0, ret = 0;
1545 error = gotd_parse_url(&proto, &hostname, &port, &path, url);
1547 yyerror("invalid HTTP notification URL '%s' in "
1548 "repository '%s': %s", url, repo->name, error->msg);
1552 tls = !strcmp(proto, "https");
1554 if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1555 yyerror("invalid protocol '%s' in notification URL '%s' in "
1556 "repository '%s", proto, url, repo->name);
1561 if ((user != NULL && password == NULL) ||
1562 (user == NULL && password != NULL)) {
1563 yyerror("missing username or password");
1568 if (strcmp(proto, "http") == 0 && (user != NULL || password != NULL)) {
1569 log_warnx("%s: WARNING: Using basic authentication over "
1570 "plaintext http:// will leak credentials; https:// is "
1571 "recommended for URL '%s'", getprogname(), url);
1574 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1575 if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1577 if (target->conf.http.tls == tls &&
1578 !strcmp(target->conf.http.hostname, hostname) &&
1579 !strcmp(target->conf.http.port, port) &&
1580 !strcmp(target->conf.http.path, path)) {
1581 yyerror("duplicate notification for URL '%s' in "
1582 "repository '%s'", url, repo->name);
1588 target = calloc(1, sizeof(*target));
1591 target->type = GOTD_NOTIFICATION_VIA_HTTP;
1592 target->conf.http.tls = tls;
1593 target->conf.http.hostname = hostname;
1594 target->conf.http.port = port;
1595 target->conf.http.path = path;
1596 hostname = port = path = NULL;
1599 target->conf.http.user = strdup(user);
1600 if (target->conf.http.user == NULL)
1602 target->conf.http.password = strdup(password);
1603 if (target->conf.http.password == NULL)
1607 STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1617 symset(const char *nam, const char *val, int persist)
1621 TAILQ_FOREACH(sym, &symhead, entry) {
1622 if (strcmp(nam, sym->nam) == 0)
1627 if (sym->persist == 1)
1632 TAILQ_REMOVE(&symhead, sym, entry);
1636 sym = calloc(1, sizeof(*sym));
1640 sym->nam = strdup(nam);
1641 if (sym->nam == NULL) {
1645 sym->val = strdup(val);
1646 if (sym->val == NULL) {
1652 sym->persist = persist;
1653 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1658 symget(const char *nam)
1662 TAILQ_FOREACH(sym, &symhead, entry) {
1663 if (strcmp(nam, sym->nam) == 0) {
1672 gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1674 struct gotd_repo *repo;
1677 TAILQ_FOREACH(repo, repos, entry) {
1678 namelen = strlen(repo->name);
1679 if (strncmp(repo->name, repo_name, namelen) != 0)
1681 if (repo_name[namelen] == '\0' ||
1682 strcmp(&repo_name[namelen], ".git") == 0)
1690 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1692 struct gotd_repo *repo;
1694 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1695 if (strcmp(repo->path, repo_path) == 0)
1702 struct gotd_uid_connection_limit *
1703 gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1704 size_t nlimits, uid_t uid)
1706 /* This array is always sorted to allow for binary search. */
1707 int i, left = 0, right = nlimits - 1;
1709 while (left <= right) {
1710 i = ((left + right) / 2);
1711 if (limits[i].uid == uid)
1713 if (limits[i].uid > uid)
1723 gotd_parseuid(const char *s, uid_t *uid)
1728 if ((pw = getpwnam(s)) != NULL) {
1730 if (*uid == UID_MAX)
1734 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1740 const struct got_error *
1741 gotd_parse_url(char **proto, char **host, char **port,
1742 char **request_path, const char *url)
1744 const struct got_error *err = NULL;
1747 *proto = *host = *port = *request_path = NULL;
1749 p = strstr(url, "://");
1751 return got_error(GOT_ERR_PARSE_URI);
1753 *proto = strndup(url, p - url);
1754 if (*proto == NULL) {
1755 err = got_error_from_errno("strndup");
1762 err = got_error(GOT_ERR_PARSE_URI);
1766 q = memchr(s, ':', p - s);
1768 *host = strndup(s, q - s);
1769 if (*host == NULL) {
1770 err = got_error_from_errno("strndup");
1773 if ((*host)[0] == '\0') {
1774 err = got_error(GOT_ERR_PARSE_URI);
1777 *port = strndup(q + 1, p - (q + 1));
1778 if (*port == NULL) {
1779 err = got_error_from_errno("strndup");
1782 if ((*port)[0] == '\0') {
1783 err = got_error(GOT_ERR_PARSE_URI);
1787 *host = strndup(s, p - s);
1788 if (*host == NULL) {
1789 err = got_error_from_errno("strndup");
1792 if ((*host)[0] == '\0') {
1793 err = got_error(GOT_ERR_PARSE_URI);
1798 while (p[0] == '/' && p[1] == '/')
1800 *request_path = strdup(p);
1801 if (*request_path == NULL) {
1802 err = got_error_from_errno("strdup");
1805 if ((*request_path)[0] == '\0') {
1806 err = got_error(GOT_ERR_PARSE_URI);
1817 free(*request_path);
1818 *request_path = NULL;
1826 static char portno[32];
1829 n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1830 if (n < 0 || (size_t)n >= sizeof(portno))
1831 fatalx("port number too long: %lld", (long long)p);