Blob


1 /*
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.
10 *
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.
14 *
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.
22 */
24 %{
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <event.h>
34 #include <imsg.h>
35 #include <limits.h>
36 #include <sha1.h>
37 #include <sha2.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_path.h"
47 #include "got_reference.h"
49 #include "log.h"
50 #include "gotd.h"
51 #include "auth.h"
52 #include "listen.h"
54 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 static struct file {
56 TAILQ_ENTRY(file) entry;
57 FILE *stream;
58 char *name;
59 int lineno;
60 int errors;
61 } *file;
62 struct file *newfile(const char *, int, int);
63 static void closefile(struct file *);
64 int check_file_secrecy(int, const char *);
65 int yyparse(void);
66 int yylex(void);
67 int yyerror(const char *, ...)
68 __attribute__((__format__ (printf, 1, 2)))
69 __attribute__((__nonnull__ (1)));
70 int kw_cmp(const void *, const void *);
71 int lookup(char *);
72 int lgetc(int);
73 int lungetc(int);
74 int findeol(void);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 struct sym {
78 TAILQ_ENTRY(sym) entry;
79 int used;
80 int persist;
81 char *nam;
82 char *val;
83 };
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
88 static int errors;
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 *,
99 char *);
100 static int conf_protect_branch_namespace(
101 struct gotd_repo *, char *);
102 static int conf_protect_branch(struct gotd_repo *,
103 char *);
104 static enum gotd_procid gotd_proc_id;
106 typedef struct {
107 union {
108 long long number;
109 char *string;
110 struct timeval tv;
111 } v;
112 int lineno;
113 } YYSTYPE;
115 %}
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
123 %type <v.tv> timeout
125 %%
127 grammar :
128 | grammar '\n'
129 | grammar main '\n'
130 | grammar repository '\n'
133 timeout : NUMBER {
134 if ($1 < 0) {
135 yyerror("invalid timeout: %lld", $1);
136 YYERROR;
138 $$.tv_sec = $1;
139 $$.tv_usec = 0;
141 | STRING {
142 const char *errstr;
143 const char *type = "seconds";
144 size_t len;
145 int mul = 1;
147 if (*$1 == '\0') {
148 yyerror("invalid number of seconds: %s", $1);
149 free($1);
150 YYERROR;
153 len = strlen($1);
154 switch ($1[len - 1]) {
155 case 'S':
156 case 's':
157 $1[len - 1] = '\0';
158 break;
159 case 'M':
160 case 'm':
161 type = "minutes";
162 mul = 60;
163 $1[len - 1] = '\0';
164 break;
165 case 'H':
166 case 'h':
167 type = "hours";
168 mul = 60 * 60;
169 $1[len - 1] = '\0';
170 break;
173 $$.tv_usec = 0;
174 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
175 if (errstr) {
176 yyerror("number of %s is %s: %s", type,
177 errstr, $1);
178 free($1);
179 YYERROR;
182 $$.tv_sec *= mul;
183 free($1);
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",
197 __func__);
198 free($3);
199 YYERROR;
202 free($3);
204 | USER STRING {
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__);
209 free($2);
210 YYERROR;
212 free($2);
214 | connection
217 connection : CONNECTION '{' optnl conflags_l '}'
218 | CONNECTION conflags
220 conflags_l : conflags optnl conflags_l
221 | conflags optnl
224 conflags : REQUEST TIMEOUT timeout {
225 if ($3.tv_sec <= 0) {
226 yyerror("invalid timeout: %lld", $3.tv_sec);
227 YYERROR;
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) {
235 free($3);
236 YYERROR;
238 free($3);
242 protect : PROTECT '{' optnl protectflags_l '}'
243 | PROTECT protectflags
245 protectflags_l : protectflags optnl protectflags_l
246 | protectflags optnl
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)) {
253 free($3);
254 YYERROR;
257 free($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,
263 $3)) {
264 free($3);
265 YYERROR;
268 free($3);
270 | BRANCH STRING {
271 if (gotd_proc_id == PROC_GOTD ||
272 gotd_proc_id == PROC_REPO_WRITE) {
273 if (conf_protect_branch(new_repo, $2)) {
274 free($2);
275 YYERROR;
278 free($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);
288 free($2);
289 YYERROR;
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);
298 free($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",
309 __func__, $2);
310 free($2);
311 YYERROR;
313 if (realpath($2, new_repo->path) == NULL) {
314 yyerror("realpath %s: %s", $2,
315 strerror(errno));
316 /*
317 * Give admin a chance to create
318 * missing repositories at run-time.
319 */
320 if (errno != ENOENT) {
321 free($2);
322 YYERROR;
323 } else if (strlcpy(new_repo->path, $2,
324 sizeof(new_repo->path)) >=
325 sizeof(new_repo->path))
326 yyerror("path too long");
329 free($2);
331 | PERMIT RO STRING {
332 if (gotd_proc_id == PROC_AUTH) {
333 conf_new_access_rule(new_repo,
334 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
335 } else
336 free($3);
338 | PERMIT RW STRING {
339 if (gotd_proc_id == PROC_AUTH) {
340 conf_new_access_rule(new_repo,
341 GOTD_ACCESS_PERMITTED,
342 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
343 } else
344 free($3);
346 | DENY STRING {
347 if (gotd_proc_id == PROC_AUTH) {
348 conf_new_access_rule(new_repo,
349 GOTD_ACCESS_DENIED, 0, $2);
350 } else
351 free($2);
353 | protect
356 repoopts2 : repoopts2 repoopts1 nl
357 | repoopts1 optnl
360 nl : '\n' optnl
363 optnl : '\n' optnl /* zero or more newlines */
364 | /* empty */
367 %%
369 struct keywords {
370 const char *k_name;
371 int k_val;
372 };
374 int
375 yyerror(const char *fmt, ...)
377 va_list ap;
378 char *msg;
380 file->errors++;
381 va_start(ap, fmt);
382 if (vasprintf(&msg, fmt, ap) == -1)
383 fatalx("yyerror vasprintf");
384 va_end(ap);
385 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
386 free(msg);
387 return (0);
390 int
391 kw_cmp(const void *k, const void *e)
393 return (strcmp(k, ((const struct keywords *)e)->k_name));
396 int
397 lookup(char *s)
399 /* This has to be sorted always. */
400 static const struct keywords keywords[] = {
401 { "branch", BRANCH },
402 { "connection", CONNECTION },
403 { "deny", DENY },
404 { "limit", LIMIT },
405 { "listen", LISTEN },
406 { "namespace", NAMESPACE },
407 { "on", ON },
408 { "path", PATH },
409 { "permit", PERMIT },
410 { "protect", PROTECT },
411 { "repository", REPOSITORY },
412 { "request", REQUEST },
413 { "ro", RO },
414 { "rw", RW },
415 { "tag", TAG },
416 { "timeout", TIMEOUT },
417 { "user", USER },
418 };
419 const struct keywords *p;
421 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
422 sizeof(keywords[0]), kw_cmp);
424 if (p)
425 return (p->k_val);
426 else
427 return (STRING);
430 #define MAXPUSHBACK 128
432 unsigned char *parsebuf;
433 int parseindex;
434 unsigned char pushback_buffer[MAXPUSHBACK];
435 int pushback_index = 0;
437 int
438 lgetc(int quotec)
440 int c, next;
442 if (parsebuf) {
443 /* Read character from the parsebuffer instead of input. */
444 if (parseindex >= 0) {
445 c = parsebuf[parseindex++];
446 if (c != '\0')
447 return (c);
448 parsebuf = NULL;
449 } else
450 parseindex++;
453 if (pushback_index)
454 return (pushback_buffer[--pushback_index]);
456 if (quotec) {
457 c = getc(file->stream);
458 if (c == EOF)
459 yyerror("reached end of file while parsing "
460 "quoted string");
461 return (c);
464 c = getc(file->stream);
465 while (c == '\\') {
466 next = getc(file->stream);
467 if (next != '\n') {
468 c = next;
469 break;
471 yylval.lineno = file->lineno;
472 file->lineno++;
473 c = getc(file->stream);
476 return (c);
479 int
480 lungetc(int c)
482 if (c == EOF)
483 return (EOF);
484 if (parsebuf) {
485 parseindex--;
486 if (parseindex >= 0)
487 return (c);
489 if (pushback_index < MAXPUSHBACK-1)
490 return (pushback_buffer[pushback_index++] = c);
491 else
492 return (EOF);
495 int
496 findeol(void)
498 int c;
500 parsebuf = NULL;
502 /* Skip to either EOF or the first real EOL. */
503 while (1) {
504 if (pushback_index)
505 c = pushback_buffer[--pushback_index];
506 else
507 c = lgetc(0);
508 if (c == '\n') {
509 file->lineno++;
510 break;
512 if (c == EOF)
513 break;
515 return (ERROR);
518 int
519 yylex(void)
521 unsigned char buf[8096];
522 unsigned char *p, *val;
523 int quotec, next, c;
524 int token;
526 top:
527 p = buf;
528 c = lgetc(0);
529 while (c == ' ' || c == '\t')
530 c = lgetc(0); /* nothing */
532 yylval.lineno = file->lineno;
533 if (c == '#') {
534 c = lgetc(0);
535 while (c != '\n' && c != EOF)
536 c = lgetc(0); /* nothing */
538 if (c == '$' && parsebuf == NULL) {
539 while (1) {
540 c = lgetc(0);
541 if (c == EOF)
542 return (0);
544 if (p + 1 >= buf + sizeof(buf) - 1) {
545 yyerror("string too long");
546 return (findeol());
548 if (isalnum(c) || c == '_') {
549 *p++ = c;
550 continue;
552 *p = '\0';
553 lungetc(c);
554 break;
556 val = symget(buf);
557 if (val == NULL) {
558 yyerror("macro '%s' not defined", buf);
559 return (findeol());
561 parsebuf = val;
562 parseindex = 0;
563 goto top;
566 switch (c) {
567 case '\'':
568 case '"':
569 quotec = c;
570 while (1) {
571 c = lgetc(quotec);
572 if (c == EOF)
573 return (0);
574 if (c == '\n') {
575 file->lineno++;
576 continue;
577 } else if (c == '\\') {
578 next = lgetc(quotec);
579 if (next == EOF)
580 return (0);
581 if (next == quotec || c == ' ' || c == '\t')
582 c = next;
583 else if (next == '\n') {
584 file->lineno++;
585 continue;
586 } else
587 lungetc(next);
588 } else if (c == quotec) {
589 *p = '\0';
590 break;
591 } else if (c == '\0') {
592 yyerror("syntax error");
593 return (findeol());
595 if (p + 1 >= buf + sizeof(buf) - 1) {
596 yyerror("string too long");
597 return (findeol());
599 *p++ = c;
601 yylval.v.string = strdup(buf);
602 if (yylval.v.string == NULL)
603 err(1, "yylex: strdup");
604 return (STRING);
607 #define allowed_to_end_number(x) \
608 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
610 if (c == '-' || isdigit(c)) {
611 do {
612 *p++ = c;
613 if ((unsigned)(p-buf) >= sizeof(buf)) {
614 yyerror("string too long");
615 return (findeol());
617 c = lgetc(0);
618 } while (c != EOF && isdigit(c));
619 lungetc(c);
620 if (p == buf + 1 && buf[0] == '-')
621 goto nodigits;
622 if (c == EOF || allowed_to_end_number(c)) {
623 const char *errstr = NULL;
625 *p = '\0';
626 yylval.v.number = strtonum(buf, LLONG_MIN,
627 LLONG_MAX, &errstr);
628 if (errstr) {
629 yyerror("\"%s\" invalid number: %s",
630 buf, errstr);
631 return (findeol());
633 return (NUMBER);
634 } else {
635 nodigits:
636 while (p > buf + 1)
637 lungetc(*--p);
638 c = *--p;
639 if (c == '-')
640 return (c);
644 #define allowed_in_string(x) \
645 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
646 x != '{' && x != '}' && \
647 x != '!' && x != '=' && x != '#' && \
648 x != ','))
650 if (isalnum(c) || c == ':' || c == '_') {
651 do {
652 *p++ = c;
653 if ((unsigned)(p-buf) >= sizeof(buf)) {
654 yyerror("string too long");
655 return (findeol());
657 c = lgetc(0);
658 } while (c != EOF && (allowed_in_string(c)));
659 lungetc(c);
660 *p = '\0';
661 token = lookup(buf);
662 if (token == STRING) {
663 yylval.v.string = strdup(buf);
664 if (yylval.v.string == NULL)
665 err(1, "yylex: strdup");
667 return (token);
669 if (c == '\n') {
670 yylval.lineno = file->lineno;
671 file->lineno++;
673 if (c == EOF)
674 return (0);
675 return (c);
678 int
679 check_file_secrecy(int fd, const char *fname)
681 struct stat st;
683 if (fstat(fd, &st)) {
684 log_warn("cannot stat %s", fname);
685 return (-1);
687 if (st.st_uid != 0 && st.st_uid != getuid()) {
688 log_warnx("%s: owner not root or current user", fname);
689 return (-1);
691 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
692 log_warnx("%s: group writable or world read/writable", fname);
693 return (-1);
695 return (0);
698 struct file *
699 newfile(const char *name, int secret, int required)
701 struct file *nfile;
703 nfile = calloc(1, sizeof(struct file));
704 if (nfile == NULL) {
705 log_warn("calloc");
706 return (NULL);
708 nfile->name = strdup(name);
709 if (nfile->name == NULL) {
710 log_warn("strdup");
711 free(nfile);
712 return (NULL);
714 nfile->stream = fopen(nfile->name, "r");
715 if (nfile->stream == NULL) {
716 if (required)
717 log_warn("open %s", nfile->name);
718 free(nfile->name);
719 free(nfile);
720 return (NULL);
721 } else if (secret &&
722 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
723 fclose(nfile->stream);
724 free(nfile->name);
725 free(nfile);
726 return (NULL);
728 nfile->lineno = 1;
729 return (nfile);
732 static void
733 closefile(struct file *xfile)
735 fclose(xfile->stream);
736 free(xfile->name);
737 free(xfile);
740 int
741 parse_config(const char *filename, enum gotd_procid proc_id,
742 struct gotd *env, int require_config_file)
744 struct sym *sym, *next;
745 struct gotd_repo *repo;
747 memset(env, 0, sizeof(*env));
749 gotd = env;
750 gotd_proc_id = proc_id;
751 TAILQ_INIT(&gotd->repos);
753 /* Apply default values. */
754 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
755 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
756 fprintf(stderr, "%s: unix socket path too long", __func__);
757 return -1;
759 if (strlcpy(gotd->user_name, GOTD_USER,
760 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
761 fprintf(stderr, "%s: user name too long", __func__);
762 return -1;
765 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
766 gotd->request_timeout.tv_usec = 0;
768 file = newfile(filename, 0, require_config_file);
769 if (file == NULL)
770 return require_config_file ? -1 : 0;
772 yyparse();
773 errors = file->errors;
774 closefile(file);
776 /* Free macros and check which have not been used. */
777 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
778 if ((gotd->verbosity > 1) && !sym->used)
779 fprintf(stderr, "warning: macro '%s' not used\n",
780 sym->nam);
781 if (!sym->persist) {
782 free(sym->nam);
783 free(sym->val);
784 TAILQ_REMOVE(&symhead, sym, entry);
785 free(sym);
789 if (errors)
790 return (-1);
792 TAILQ_FOREACH(repo, &gotd->repos, entry) {
793 if (repo->path[0] == '\0') {
794 log_warnx("repository \"%s\": no path provided in "
795 "configuration file", repo->name);
796 return (-1);
800 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
801 log_warnx("no repository defined in configuration file");
802 return (-1);
805 return (0);
808 static int
809 uid_connection_limit_cmp(const void *pa, const void *pb)
811 const struct gotd_uid_connection_limit *a = pa, *b = pb;
813 if (a->uid < b->uid)
814 return -1;
815 else if (a->uid > b->uid);
816 return 1;
818 return 0;
821 static int
822 conf_limit_user_connections(const char *user, int maximum)
824 uid_t uid;
825 struct gotd_uid_connection_limit *limit;
826 size_t nlimits;
828 if (maximum < 1) {
829 yyerror("max connections cannot be smaller 1");
830 return -1;
832 if (maximum > GOTD_MAXCLIENTS) {
833 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
834 return -1;
837 if (gotd_auth_parseuid(user, &uid) == -1) {
838 yyerror("%s: no such user", user);
839 return -1;
842 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
843 gotd->nconnection_limits, uid);
844 if (limit) {
845 limit->max_connections = maximum;
846 return 0;
849 limit = gotd->connection_limits;
850 nlimits = gotd->nconnection_limits + 1;
851 limit = reallocarray(limit, nlimits, sizeof(*limit));
852 if (limit == NULL)
853 fatal("reallocarray");
855 limit[nlimits - 1].uid = uid;
856 limit[nlimits - 1].max_connections = maximum;
858 gotd->connection_limits = limit;
859 gotd->nconnection_limits = nlimits;
860 qsort(gotd->connection_limits, gotd->nconnection_limits,
861 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
863 return 0;
866 static struct gotd_repo *
867 conf_new_repo(const char *name)
869 struct gotd_repo *repo;
871 if (name[0] == '\0') {
872 fatalx("syntax error: empty repository name found in %s",
873 file->name);
876 if (strchr(name, '\n') != NULL)
877 fatalx("repository names must not contain linefeeds: %s", name);
879 repo = calloc(1, sizeof(*repo));
880 if (repo == NULL)
881 fatalx("%s: calloc", __func__);
883 STAILQ_INIT(&repo->rules);
884 TAILQ_INIT(&repo->protected_tag_namespaces);
885 TAILQ_INIT(&repo->protected_branch_namespaces);
886 TAILQ_INIT(&repo->protected_branches);
888 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
889 sizeof(repo->name))
890 fatalx("%s: strlcpy", __func__);
892 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
893 gotd->nrepos++;
895 return repo;
896 };
898 static void
899 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
900 int authorization, char *identifier)
902 struct gotd_access_rule *rule;
904 rule = calloc(1, sizeof(*rule));
905 if (rule == NULL)
906 fatal("calloc");
908 rule->access = access;
909 rule->authorization = authorization;
910 rule->identifier = identifier;
912 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
915 static int
916 refname_is_valid(char *refname)
918 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
919 yyerror("reference name must begin with \"refs/\": %s",
920 refname);
921 return 0;
924 if (!got_ref_name_is_valid(refname)) {
925 yyerror("invalid reference name: %s", refname);
926 return 0;
929 return 1;
932 static int
933 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
934 char *namespace)
936 const struct got_error *error;
937 struct got_pathlist_entry *pe;
938 char *s;
940 *new = NULL;
942 got_path_strip_trailing_slashes(namespace);
943 if (!refname_is_valid(namespace))
944 return -1;
945 if (asprintf(&s, "%s/", namespace) == -1) {
946 yyerror("asprintf: %s", strerror(errno));
947 return -1;
950 error = got_pathlist_insert(&pe, refs, s, NULL);
951 if (error || pe == NULL) {
952 free(s);
953 if (error)
954 yyerror("got_pathlist_insert: %s", error->msg);
955 else
956 yyerror("duplicate protected namespace %s", namespace);
957 return -1;
960 *new = s;
961 return 0;
964 static int
965 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
967 struct got_pathlist_entry *pe;
968 char *new;
970 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
971 namespace) == -1)
972 return -1;
974 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
975 if (strcmp(pe->path, new) == 0) {
976 yyerror("duplicate protected namespace %s", namespace);
977 return -1;
981 return 0;
984 static int
985 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
987 struct got_pathlist_entry *pe;
988 char *new;
990 if (conf_protect_ref_namespace(&new,
991 &repo->protected_branch_namespaces, namespace) == -1)
992 return -1;
994 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
995 if (strcmp(pe->path, new) == 0) {
996 yyerror("duplicate protected namespace %s", namespace);
997 return -1;
1001 return 0;
1004 static int
1005 conf_protect_branch(struct gotd_repo *repo, char *branchname)
1007 const struct got_error *error;
1008 struct got_pathlist_entry *new;
1009 char *refname;
1011 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1012 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1013 yyerror("asprintf: %s", strerror(errno));
1014 return -1;
1016 } else {
1017 refname = strdup(branchname);
1018 if (refname == NULL) {
1019 yyerror("strdup: %s", strerror(errno));
1020 return -1;
1024 if (!refname_is_valid(refname)) {
1025 free(refname);
1026 return -1;
1029 error = got_pathlist_insert(&new, &repo->protected_branches,
1030 refname, NULL);
1031 if (error || new == NULL) {
1032 free(refname);
1033 if (error)
1034 yyerror("got_pathlist_insert: %s", error->msg);
1035 else
1036 yyerror("duplicate protect branch %s", branchname);
1037 return -1;
1040 return 0;
1043 int
1044 symset(const char *nam, const char *val, int persist)
1046 struct sym *sym;
1048 TAILQ_FOREACH(sym, &symhead, entry) {
1049 if (strcmp(nam, sym->nam) == 0)
1050 break;
1053 if (sym != NULL) {
1054 if (sym->persist == 1)
1055 return (0);
1056 else {
1057 free(sym->nam);
1058 free(sym->val);
1059 TAILQ_REMOVE(&symhead, sym, entry);
1060 free(sym);
1063 sym = calloc(1, sizeof(*sym));
1064 if (sym == NULL)
1065 return (-1);
1067 sym->nam = strdup(nam);
1068 if (sym->nam == NULL) {
1069 free(sym);
1070 return (-1);
1072 sym->val = strdup(val);
1073 if (sym->val == NULL) {
1074 free(sym->nam);
1075 free(sym);
1076 return (-1);
1078 sym->used = 0;
1079 sym->persist = persist;
1080 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1081 return (0);
1084 char *
1085 symget(const char *nam)
1087 struct sym *sym;
1089 TAILQ_FOREACH(sym, &symhead, entry) {
1090 if (strcmp(nam, sym->nam) == 0) {
1091 sym->used = 1;
1092 return (sym->val);
1095 return (NULL);
1098 struct gotd_repo *
1099 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1101 struct gotd_repo *repo;
1102 size_t namelen;
1104 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1105 namelen = strlen(repo->name);
1106 if (strncmp(repo->name, repo_name, namelen) != 0)
1107 continue;
1108 if (repo_name[namelen] == '\0' ||
1109 strcmp(&repo_name[namelen], ".git") == 0)
1110 return repo;
1113 return NULL;
1116 struct gotd_repo *
1117 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1119 struct gotd_repo *repo;
1121 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1122 if (strcmp(repo->path, repo_path) == 0)
1123 return repo;
1126 return NULL;