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(
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;
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,
262 $3)) {
263 free($3);
264 YYERROR;
266 free($3);
269 | BRANCH STRING {
270 if (gotd_proc_id == PROC_GOTD ||
271 gotd_proc_id == PROC_REPO_WRITE) {
272 if (conf_protect_branch(new_repo, $2)) {
273 free($2);
274 YYERROR;
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);
286 free($2);
287 YYERROR;
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);
296 free($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",
307 __func__, $2);
308 free($2);
309 YYERROR;
311 if (realpath($2, new_repo->path) == NULL) {
312 yyerror("realpath %s: %s", $2, strerror(errno));
313 free($2);
314 YYERROR;
317 free($2);
319 | PERMIT RO STRING {
320 if (gotd_proc_id == PROC_AUTH) {
321 conf_new_access_rule(new_repo,
322 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
325 | PERMIT RW STRING {
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);
332 | DENY STRING {
333 if (gotd_proc_id == PROC_AUTH) {
334 conf_new_access_rule(new_repo,
335 GOTD_ACCESS_DENIED, 0, $2);
338 | protect
341 repoopts2 : repoopts2 repoopts1 nl
342 | repoopts1 optnl
345 nl : '\n' optnl
348 optnl : '\n' optnl /* zero or more newlines */
349 | /* empty */
352 %%
354 struct keywords {
355 const char *k_name;
356 int k_val;
357 };
359 int
360 yyerror(const char *fmt, ...)
362 va_list ap;
363 char *msg;
365 file->errors++;
366 va_start(ap, fmt);
367 if (vasprintf(&msg, fmt, ap) == -1)
368 fatalx("yyerror vasprintf");
369 va_end(ap);
370 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
371 free(msg);
372 return (0);
375 int
376 kw_cmp(const void *k, const void *e)
378 return (strcmp(k, ((const struct keywords *)e)->k_name));
381 int
382 lookup(char *s)
384 /* This has to be sorted always. */
385 static const struct keywords keywords[] = {
386 { "branch", BRANCH },
387 { "connection", CONNECTION },
388 { "deny", DENY },
389 { "limit", LIMIT },
390 { "listen", LISTEN },
391 { "namespace", NAMESPACE },
392 { "on", ON },
393 { "path", PATH },
394 { "permit", PERMIT },
395 { "protect", PROTECT },
396 { "repository", REPOSITORY },
397 { "request", REQUEST },
398 { "ro", RO },
399 { "rw", RW },
400 { "tag", TAG },
401 { "timeout", TIMEOUT },
402 { "user", USER },
403 };
404 const struct keywords *p;
406 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
407 sizeof(keywords[0]), kw_cmp);
409 if (p)
410 return (p->k_val);
411 else
412 return (STRING);
415 #define MAXPUSHBACK 128
417 unsigned char *parsebuf;
418 int parseindex;
419 unsigned char pushback_buffer[MAXPUSHBACK];
420 int pushback_index = 0;
422 int
423 lgetc(int quotec)
425 int c, next;
427 if (parsebuf) {
428 /* Read character from the parsebuffer instead of input. */
429 if (parseindex >= 0) {
430 c = parsebuf[parseindex++];
431 if (c != '\0')
432 return (c);
433 parsebuf = NULL;
434 } else
435 parseindex++;
438 if (pushback_index)
439 return (pushback_buffer[--pushback_index]);
441 if (quotec) {
442 c = getc(file->stream);
443 if (c == EOF)
444 yyerror("reached end of file while parsing "
445 "quoted string");
446 return (c);
449 c = getc(file->stream);
450 while (c == '\\') {
451 next = getc(file->stream);
452 if (next != '\n') {
453 c = next;
454 break;
456 yylval.lineno = file->lineno;
457 file->lineno++;
458 c = getc(file->stream);
461 return (c);
464 int
465 lungetc(int c)
467 if (c == EOF)
468 return (EOF);
469 if (parsebuf) {
470 parseindex--;
471 if (parseindex >= 0)
472 return (c);
474 if (pushback_index < MAXPUSHBACK-1)
475 return (pushback_buffer[pushback_index++] = c);
476 else
477 return (EOF);
480 int
481 findeol(void)
483 int c;
485 parsebuf = NULL;
487 /* Skip to either EOF or the first real EOL. */
488 while (1) {
489 if (pushback_index)
490 c = pushback_buffer[--pushback_index];
491 else
492 c = lgetc(0);
493 if (c == '\n') {
494 file->lineno++;
495 break;
497 if (c == EOF)
498 break;
500 return (ERROR);
503 int
504 yylex(void)
506 unsigned char buf[8096];
507 unsigned char *p, *val;
508 int quotec, next, c;
509 int token;
511 top:
512 p = buf;
513 c = lgetc(0);
514 while (c == ' ' || c == '\t')
515 c = lgetc(0); /* nothing */
517 yylval.lineno = file->lineno;
518 if (c == '#') {
519 c = lgetc(0);
520 while (c != '\n' && c != EOF)
521 c = lgetc(0); /* nothing */
523 if (c == '$' && parsebuf == NULL) {
524 while (1) {
525 c = lgetc(0);
526 if (c == EOF)
527 return (0);
529 if (p + 1 >= buf + sizeof(buf) - 1) {
530 yyerror("string too long");
531 return (findeol());
533 if (isalnum(c) || c == '_') {
534 *p++ = c;
535 continue;
537 *p = '\0';
538 lungetc(c);
539 break;
541 val = symget(buf);
542 if (val == NULL) {
543 yyerror("macro '%s' not defined", buf);
544 return (findeol());
546 parsebuf = val;
547 parseindex = 0;
548 goto top;
551 switch (c) {
552 case '\'':
553 case '"':
554 quotec = c;
555 while (1) {
556 c = lgetc(quotec);
557 if (c == EOF)
558 return (0);
559 if (c == '\n') {
560 file->lineno++;
561 continue;
562 } else if (c == '\\') {
563 next = lgetc(quotec);
564 if (next == EOF)
565 return (0);
566 if (next == quotec || c == ' ' || c == '\t')
567 c = next;
568 else if (next == '\n') {
569 file->lineno++;
570 continue;
571 } else
572 lungetc(next);
573 } else if (c == quotec) {
574 *p = '\0';
575 break;
576 } else if (c == '\0') {
577 yyerror("syntax error");
578 return (findeol());
580 if (p + 1 >= buf + sizeof(buf) - 1) {
581 yyerror("string too long");
582 return (findeol());
584 *p++ = c;
586 yylval.v.string = strdup(buf);
587 if (yylval.v.string == NULL)
588 err(1, "yylex: strdup");
589 return (STRING);
592 #define allowed_to_end_number(x) \
593 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
595 if (c == '-' || isdigit(c)) {
596 do {
597 *p++ = c;
598 if ((unsigned)(p-buf) >= sizeof(buf)) {
599 yyerror("string too long");
600 return (findeol());
602 c = lgetc(0);
603 } while (c != EOF && isdigit(c));
604 lungetc(c);
605 if (p == buf + 1 && buf[0] == '-')
606 goto nodigits;
607 if (c == EOF || allowed_to_end_number(c)) {
608 const char *errstr = NULL;
610 *p = '\0';
611 yylval.v.number = strtonum(buf, LLONG_MIN,
612 LLONG_MAX, &errstr);
613 if (errstr) {
614 yyerror("\"%s\" invalid number: %s",
615 buf, errstr);
616 return (findeol());
618 return (NUMBER);
619 } else {
620 nodigits:
621 while (p > buf + 1)
622 lungetc(*--p);
623 c = *--p;
624 if (c == '-')
625 return (c);
629 #define allowed_in_string(x) \
630 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
631 x != '{' && x != '}' && \
632 x != '!' && x != '=' && x != '#' && \
633 x != ','))
635 if (isalnum(c) || c == ':' || c == '_') {
636 do {
637 *p++ = c;
638 if ((unsigned)(p-buf) >= sizeof(buf)) {
639 yyerror("string too long");
640 return (findeol());
642 c = lgetc(0);
643 } while (c != EOF && (allowed_in_string(c)));
644 lungetc(c);
645 *p = '\0';
646 token = lookup(buf);
647 if (token == STRING) {
648 yylval.v.string = strdup(buf);
649 if (yylval.v.string == NULL)
650 err(1, "yylex: strdup");
652 return (token);
654 if (c == '\n') {
655 yylval.lineno = file->lineno;
656 file->lineno++;
658 if (c == EOF)
659 return (0);
660 return (c);
663 int
664 check_file_secrecy(int fd, const char *fname)
666 struct stat st;
668 if (fstat(fd, &st)) {
669 log_warn("cannot stat %s", fname);
670 return (-1);
672 if (st.st_uid != 0 && st.st_uid != getuid()) {
673 log_warnx("%s: owner not root or current user", fname);
674 return (-1);
676 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
677 log_warnx("%s: group writable or world read/writable", fname);
678 return (-1);
680 return (0);
683 struct file *
684 newfile(const char *name, int secret, int required)
686 struct file *nfile;
688 nfile = calloc(1, sizeof(struct file));
689 if (nfile == NULL) {
690 log_warn("calloc");
691 return (NULL);
693 nfile->name = strdup(name);
694 if (nfile->name == NULL) {
695 log_warn("strdup");
696 free(nfile);
697 return (NULL);
699 nfile->stream = fopen(nfile->name, "r");
700 if (nfile->stream == NULL) {
701 if (required)
702 log_warn("open %s", nfile->name);
703 free(nfile->name);
704 free(nfile);
705 return (NULL);
706 } else if (secret &&
707 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
708 fclose(nfile->stream);
709 free(nfile->name);
710 free(nfile);
711 return (NULL);
713 nfile->lineno = 1;
714 return (nfile);
717 static void
718 closefile(struct file *xfile)
720 fclose(xfile->stream);
721 free(xfile->name);
722 free(xfile);
725 int
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));
734 gotd = 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__);
742 return -1;
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__);
747 return -1;
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);
754 if (file == NULL)
755 return require_config_file ? -1 : 0;
757 yyparse();
758 errors = file->errors;
759 closefile(file);
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",
765 sym->nam);
766 if (!sym->persist) {
767 free(sym->nam);
768 free(sym->val);
769 TAILQ_REMOVE(&symhead, sym, entry);
770 free(sym);
774 if (errors)
775 return (-1);
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);
781 return (-1);
785 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
786 log_warnx("no repository defined in configuration file");
787 return (-1);
790 return (0);
793 static int
794 uid_connection_limit_cmp(const void *pa, const void *pb)
796 const struct gotd_uid_connection_limit *a = pa, *b = pb;
798 if (a->uid < b->uid)
799 return -1;
800 else if (a->uid > b->uid);
801 return 1;
803 return 0;
806 static int
807 conf_limit_user_connections(const char *user, int maximum)
809 uid_t uid;
810 struct gotd_uid_connection_limit *limit;
811 size_t nlimits;
813 if (maximum < 1) {
814 yyerror("max connections cannot be smaller 1");
815 return -1;
817 if (maximum > GOTD_MAXCLIENTS) {
818 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
819 return -1;
822 if (gotd_auth_parseuid(user, &uid) == -1) {
823 yyerror("%s: no such user", user);
824 return -1;
827 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
828 gotd->nconnection_limits, uid);
829 if (limit) {
830 limit->max_connections = maximum;
831 return 0;
834 limit = gotd->connection_limits;
835 nlimits = gotd->nconnection_limits + 1;
836 limit = reallocarray(limit, nlimits, sizeof(*limit));
837 if (limit == NULL)
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);
848 return 0;
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",
858 file->name);
861 if (strchr(name, '\n') != NULL)
862 fatalx("repository names must not contain linefeeds: %s", name);
864 repo = calloc(1, sizeof(*repo));
865 if (repo == NULL)
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)) >=
874 sizeof(repo->name))
875 fatalx("%s: strlcpy", __func__);
877 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
878 gotd->nrepos++;
880 return repo;
881 };
883 static void
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));
890 if (rule == NULL)
891 fatal("calloc");
893 rule->access = access;
894 rule->authorization = authorization;
895 rule->identifier = identifier;
897 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
900 static int
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",
905 refname);
906 return 0;
909 if (!got_ref_name_is_valid(refname)) {
910 yyerror("invalid reference name: %s", refname);
911 return 0;
914 return 1;
917 static int
918 conf_protect_ref_namespace(struct got_pathlist_head *refs, char *namespace)
920 const struct got_error *error;
921 struct got_pathlist_entry *new;
922 char *s;
924 got_path_strip_trailing_slashes(namespace);
925 if (!refname_is_valid(namespace))
926 return -1;
927 if (asprintf(&s, "%s/", namespace) == -1) {
928 yyerror("asprintf: %s", strerror(errno));
929 return -1;
932 error = got_pathlist_insert(&new, refs, s, NULL);
933 if (error || new == NULL) {
934 free(s);
935 if (error)
936 yyerror("got_pathlist_insert: %s", error->msg);
937 else
938 yyerror("duplicate protect namespace %s", namespace);
939 return -1;
942 return 0;
945 static int
946 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
948 return conf_protect_ref_namespace(&repo->protected_tag_namespaces,
949 namespace);
952 static int
953 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
955 return conf_protect_ref_namespace(&repo->protected_branch_namespaces,
956 namespace);
959 static int
960 conf_protect_branch(struct gotd_repo *repo, char *branchname)
962 const struct got_error *error;
963 struct got_pathlist_entry *new;
964 char *refname;
966 if (strncmp(branchname, "refs/heads/", 11) != 0) {
967 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
968 yyerror("asprintf: %s", strerror(errno));
969 return -1;
971 } else {
972 refname = strdup(branchname);
973 if (refname == NULL) {
974 yyerror("strdup: %s", strerror(errno));
975 return -1;
979 if (!refname_is_valid(refname)) {
980 free(refname);
981 return -1;
984 error = got_pathlist_insert(&new, &repo->protected_branches,
985 refname, NULL);
986 if (error || new == NULL) {
987 free(refname);
988 if (error)
989 yyerror("got_pathlist_insert: %s", error->msg);
990 else
991 yyerror("duplicate protect branch %s", branchname);
992 return -1;
995 return 0;
998 int
999 symset(const char *nam, const char *val, int persist)
1001 struct sym *sym;
1003 TAILQ_FOREACH(sym, &symhead, entry) {
1004 if (strcmp(nam, sym->nam) == 0)
1005 break;
1008 if (sym != NULL) {
1009 if (sym->persist == 1)
1010 return (0);
1011 else {
1012 free(sym->nam);
1013 free(sym->val);
1014 TAILQ_REMOVE(&symhead, sym, entry);
1015 free(sym);
1018 sym = calloc(1, sizeof(*sym));
1019 if (sym == NULL)
1020 return (-1);
1022 sym->nam = strdup(nam);
1023 if (sym->nam == NULL) {
1024 free(sym);
1025 return (-1);
1027 sym->val = strdup(val);
1028 if (sym->val == NULL) {
1029 free(sym->nam);
1030 free(sym);
1031 return (-1);
1033 sym->used = 0;
1034 sym->persist = persist;
1035 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1036 return (0);
1039 char *
1040 symget(const char *nam)
1042 struct sym *sym;
1044 TAILQ_FOREACH(sym, &symhead, entry) {
1045 if (strcmp(nam, sym->nam) == 0) {
1046 sym->used = 1;
1047 return (sym->val);
1050 return (NULL);
1053 struct gotd_repo *
1054 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1056 struct gotd_repo *repo;
1057 size_t namelen;
1059 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1060 namelen = strlen(repo->name);
1061 if (strncmp(repo->name, repo_name, namelen) != 0)
1062 continue;
1063 if (repo_name[namelen] == '\0' ||
1064 strcmp(&repo_name[namelen], ".git") == 0)
1065 return repo;
1068 return NULL;
1071 struct gotd_repo *
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)
1078 return repo;
1081 return NULL;