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 <stdarg.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
43 #include "got_error.h"
44 #include "got_path.h"
45 #include "got_reference.h"
47 #include "log.h"
48 #include "gotd.h"
49 #include "auth.h"
50 #include "listen.h"
52 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
53 static struct file {
54 TAILQ_ENTRY(file) entry;
55 FILE *stream;
56 char *name;
57 int lineno;
58 int errors;
59 } *file;
60 struct file *newfile(const char *, int, int);
61 static void closefile(struct file *);
62 int check_file_secrecy(int, const char *);
63 int yyparse(void);
64 int yylex(void);
65 int yyerror(const char *, ...)
66 __attribute__((__format__ (printf, 1, 2)))
67 __attribute__((__nonnull__ (1)));
68 int kw_cmp(const void *, const void *);
69 int lookup(char *);
70 int lgetc(int);
71 int lungetc(int);
72 int findeol(void);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 struct sym {
76 TAILQ_ENTRY(sym) entry;
77 int used;
78 int persist;
79 char *nam;
80 char *val;
81 };
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 static int errors;
88 static struct gotd *gotd;
89 static struct gotd_repo *new_repo;
90 static int conf_limit_user_connections(const char *, int);
91 static struct gotd_repo *conf_new_repo(const char *);
92 static void conf_new_access_rule(struct gotd_repo *,
93 enum gotd_access, int, char *);
94 static int conf_protect_ref_namespace(char **,
95 struct got_pathlist_head *, char *);
96 static int conf_protect_tag_namespace(struct gotd_repo *,
97 char *);
98 static int conf_protect_branch_namespace(
99 struct gotd_repo *, char *);
100 static int conf_protect_branch(struct gotd_repo *,
101 char *);
102 static enum gotd_procid gotd_proc_id;
104 typedef struct {
105 union {
106 long long number;
107 char *string;
108 struct timeval tv;
109 } v;
110 int lineno;
111 } YYSTYPE;
113 %}
115 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
116 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
117 %token PROTECT NAMESPACE BRANCH TAG
119 %token <v.string> STRING
120 %token <v.number> NUMBER
121 %type <v.tv> timeout
123 %%
125 grammar :
126 | grammar '\n'
127 | grammar main '\n'
128 | grammar repository '\n'
131 timeout : NUMBER {
132 if ($1 < 0) {
133 yyerror("invalid timeout: %lld", $1);
134 YYERROR;
136 $$.tv_sec = $1;
137 $$.tv_usec = 0;
139 | STRING {
140 const char *errstr;
141 const char *type = "seconds";
142 size_t len;
143 int mul = 1;
145 if (*$1 == '\0') {
146 yyerror("invalid number of seconds: %s", $1);
147 free($1);
148 YYERROR;
151 len = strlen($1);
152 switch ($1[len - 1]) {
153 case 'S':
154 case 's':
155 $1[len - 1] = '\0';
156 break;
157 case 'M':
158 case 'm':
159 type = "minutes";
160 mul = 60;
161 $1[len - 1] = '\0';
162 break;
163 case 'H':
164 case 'h':
165 type = "hours";
166 mul = 60 * 60;
167 $1[len - 1] = '\0';
168 break;
171 $$.tv_usec = 0;
172 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
173 if (errstr) {
174 yyerror("number of %s is %s: %s", type,
175 errstr, $1);
176 free($1);
177 YYERROR;
180 $$.tv_sec *= mul;
181 free($1);
185 main : LISTEN ON STRING {
186 if (!got_path_is_absolute($3))
187 yyerror("bad unix socket path \"%s\": "
188 "must be an absolute path", $3);
190 if (gotd_proc_id == PROC_LISTEN) {
191 if (strlcpy(gotd->unix_socket_path, $3,
192 sizeof(gotd->unix_socket_path)) >=
193 sizeof(gotd->unix_socket_path)) {
194 yyerror("%s: unix socket path too long",
195 __func__);
196 free($3);
197 YYERROR;
200 free($3);
202 | USER STRING {
203 if (strlcpy(gotd->user_name, $2,
204 sizeof(gotd->user_name)) >=
205 sizeof(gotd->user_name)) {
206 yyerror("%s: user name too long", __func__);
207 free($2);
208 YYERROR;
210 free($2);
212 | connection
215 connection : CONNECTION '{' optnl conflags_l '}'
216 | CONNECTION conflags
218 conflags_l : conflags optnl conflags_l
219 | conflags optnl
222 conflags : REQUEST TIMEOUT timeout {
223 if ($3.tv_sec <= 0) {
224 yyerror("invalid timeout: %lld", $3.tv_sec);
225 YYERROR;
227 memcpy(&gotd->request_timeout, &$3,
228 sizeof(gotd->request_timeout));
230 | LIMIT USER STRING NUMBER {
231 if (gotd_proc_id == PROC_LISTEN &&
232 conf_limit_user_connections($3, $4) == -1) {
233 free($3);
234 YYERROR;
236 free($3);
240 protect : PROTECT '{' optnl protectflags_l '}'
241 | PROTECT protectflags
243 protectflags_l : protectflags optnl protectflags_l
244 | protectflags optnl
247 protectflags : TAG NAMESPACE STRING {
248 if (gotd_proc_id == PROC_GOTD ||
249 gotd_proc_id == PROC_REPO_WRITE) {
250 if (conf_protect_tag_namespace(new_repo, $3)) {
251 free($3);
252 YYERROR;
255 free($3);
257 | BRANCH NAMESPACE STRING {
258 if (gotd_proc_id == PROC_GOTD ||
259 gotd_proc_id == PROC_REPO_WRITE) {
260 if (conf_protect_branch_namespace(new_repo,
261 $3)) {
262 free($3);
263 YYERROR;
266 free($3);
268 | BRANCH STRING {
269 if (gotd_proc_id == PROC_GOTD ||
270 gotd_proc_id == PROC_REPO_WRITE) {
271 if (conf_protect_branch(new_repo, $2)) {
272 free($2);
273 YYERROR;
276 free($2);
280 repository : REPOSITORY STRING {
281 struct gotd_repo *repo;
283 TAILQ_FOREACH(repo, &gotd->repos, entry) {
284 if (strcmp(repo->name, $2) == 0) {
285 yyerror("duplicate repository '%s'", $2);
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);
323 } else
324 free($3);
326 | PERMIT RW STRING {
327 if (gotd_proc_id == PROC_AUTH) {
328 conf_new_access_rule(new_repo,
329 GOTD_ACCESS_PERMITTED,
330 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
331 } else
332 free($3);
334 | DENY STRING {
335 if (gotd_proc_id == PROC_AUTH) {
336 conf_new_access_rule(new_repo,
337 GOTD_ACCESS_DENIED, 0, $2);
338 } else
339 free($2);
341 | protect
344 repoopts2 : repoopts2 repoopts1 nl
345 | repoopts1 optnl
348 nl : '\n' optnl
351 optnl : '\n' optnl /* zero or more newlines */
352 | /* empty */
355 %%
357 struct keywords {
358 const char *k_name;
359 int k_val;
360 };
362 int
363 yyerror(const char *fmt, ...)
365 va_list ap;
366 char *msg;
368 file->errors++;
369 va_start(ap, fmt);
370 if (vasprintf(&msg, fmt, ap) == -1)
371 fatalx("yyerror vasprintf");
372 va_end(ap);
373 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
374 free(msg);
375 return (0);
378 int
379 kw_cmp(const void *k, const void *e)
381 return (strcmp(k, ((const struct keywords *)e)->k_name));
384 int
385 lookup(char *s)
387 /* This has to be sorted always. */
388 static const struct keywords keywords[] = {
389 { "branch", BRANCH },
390 { "connection", CONNECTION },
391 { "deny", DENY },
392 { "limit", LIMIT },
393 { "listen", LISTEN },
394 { "namespace", NAMESPACE },
395 { "on", ON },
396 { "path", PATH },
397 { "permit", PERMIT },
398 { "protect", PROTECT },
399 { "repository", REPOSITORY },
400 { "request", REQUEST },
401 { "ro", RO },
402 { "rw", RW },
403 { "tag", TAG },
404 { "timeout", TIMEOUT },
405 { "user", USER },
406 };
407 const struct keywords *p;
409 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
410 sizeof(keywords[0]), kw_cmp);
412 if (p)
413 return (p->k_val);
414 else
415 return (STRING);
418 #define MAXPUSHBACK 128
420 unsigned char *parsebuf;
421 int parseindex;
422 unsigned char pushback_buffer[MAXPUSHBACK];
423 int pushback_index = 0;
425 int
426 lgetc(int quotec)
428 int c, next;
430 if (parsebuf) {
431 /* Read character from the parsebuffer instead of input. */
432 if (parseindex >= 0) {
433 c = parsebuf[parseindex++];
434 if (c != '\0')
435 return (c);
436 parsebuf = NULL;
437 } else
438 parseindex++;
441 if (pushback_index)
442 return (pushback_buffer[--pushback_index]);
444 if (quotec) {
445 c = getc(file->stream);
446 if (c == EOF)
447 yyerror("reached end of file while parsing "
448 "quoted string");
449 return (c);
452 c = getc(file->stream);
453 while (c == '\\') {
454 next = getc(file->stream);
455 if (next != '\n') {
456 c = next;
457 break;
459 yylval.lineno = file->lineno;
460 file->lineno++;
461 c = getc(file->stream);
464 return (c);
467 int
468 lungetc(int c)
470 if (c == EOF)
471 return (EOF);
472 if (parsebuf) {
473 parseindex--;
474 if (parseindex >= 0)
475 return (c);
477 if (pushback_index < MAXPUSHBACK-1)
478 return (pushback_buffer[pushback_index++] = c);
479 else
480 return (EOF);
483 int
484 findeol(void)
486 int c;
488 parsebuf = NULL;
490 /* Skip to either EOF or the first real EOL. */
491 while (1) {
492 if (pushback_index)
493 c = pushback_buffer[--pushback_index];
494 else
495 c = lgetc(0);
496 if (c == '\n') {
497 file->lineno++;
498 break;
500 if (c == EOF)
501 break;
503 return (ERROR);
506 int
507 yylex(void)
509 unsigned char buf[8096];
510 unsigned char *p, *val;
511 int quotec, next, c;
512 int token;
514 top:
515 p = buf;
516 c = lgetc(0);
517 while (c == ' ' || c == '\t')
518 c = lgetc(0); /* nothing */
520 yylval.lineno = file->lineno;
521 if (c == '#') {
522 c = lgetc(0);
523 while (c != '\n' && c != EOF)
524 c = lgetc(0); /* nothing */
526 if (c == '$' && parsebuf == NULL) {
527 while (1) {
528 c = lgetc(0);
529 if (c == EOF)
530 return (0);
532 if (p + 1 >= buf + sizeof(buf) - 1) {
533 yyerror("string too long");
534 return (findeol());
536 if (isalnum(c) || c == '_') {
537 *p++ = c;
538 continue;
540 *p = '\0';
541 lungetc(c);
542 break;
544 val = symget(buf);
545 if (val == NULL) {
546 yyerror("macro '%s' not defined", buf);
547 return (findeol());
549 parsebuf = val;
550 parseindex = 0;
551 goto top;
554 switch (c) {
555 case '\'':
556 case '"':
557 quotec = c;
558 while (1) {
559 c = lgetc(quotec);
560 if (c == EOF)
561 return (0);
562 if (c == '\n') {
563 file->lineno++;
564 continue;
565 } else if (c == '\\') {
566 next = lgetc(quotec);
567 if (next == EOF)
568 return (0);
569 if (next == quotec || c == ' ' || c == '\t')
570 c = next;
571 else if (next == '\n') {
572 file->lineno++;
573 continue;
574 } else
575 lungetc(next);
576 } else if (c == quotec) {
577 *p = '\0';
578 break;
579 } else if (c == '\0') {
580 yyerror("syntax error");
581 return (findeol());
583 if (p + 1 >= buf + sizeof(buf) - 1) {
584 yyerror("string too long");
585 return (findeol());
587 *p++ = c;
589 yylval.v.string = strdup(buf);
590 if (yylval.v.string == NULL)
591 err(1, "yylex: strdup");
592 return (STRING);
595 #define allowed_to_end_number(x) \
596 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
598 if (c == '-' || isdigit(c)) {
599 do {
600 *p++ = c;
601 if ((unsigned)(p-buf) >= sizeof(buf)) {
602 yyerror("string too long");
603 return (findeol());
605 c = lgetc(0);
606 } while (c != EOF && isdigit(c));
607 lungetc(c);
608 if (p == buf + 1 && buf[0] == '-')
609 goto nodigits;
610 if (c == EOF || allowed_to_end_number(c)) {
611 const char *errstr = NULL;
613 *p = '\0';
614 yylval.v.number = strtonum(buf, LLONG_MIN,
615 LLONG_MAX, &errstr);
616 if (errstr) {
617 yyerror("\"%s\" invalid number: %s",
618 buf, errstr);
619 return (findeol());
621 return (NUMBER);
622 } else {
623 nodigits:
624 while (p > buf + 1)
625 lungetc(*--p);
626 c = *--p;
627 if (c == '-')
628 return (c);
632 #define allowed_in_string(x) \
633 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
634 x != '{' && x != '}' && \
635 x != '!' && x != '=' && x != '#' && \
636 x != ','))
638 if (isalnum(c) || c == ':' || c == '_') {
639 do {
640 *p++ = c;
641 if ((unsigned)(p-buf) >= sizeof(buf)) {
642 yyerror("string too long");
643 return (findeol());
645 c = lgetc(0);
646 } while (c != EOF && (allowed_in_string(c)));
647 lungetc(c);
648 *p = '\0';
649 token = lookup(buf);
650 if (token == STRING) {
651 yylval.v.string = strdup(buf);
652 if (yylval.v.string == NULL)
653 err(1, "yylex: strdup");
655 return (token);
657 if (c == '\n') {
658 yylval.lineno = file->lineno;
659 file->lineno++;
661 if (c == EOF)
662 return (0);
663 return (c);
666 int
667 check_file_secrecy(int fd, const char *fname)
669 struct stat st;
671 if (fstat(fd, &st)) {
672 log_warn("cannot stat %s", fname);
673 return (-1);
675 if (st.st_uid != 0 && st.st_uid != getuid()) {
676 log_warnx("%s: owner not root or current user", fname);
677 return (-1);
679 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
680 log_warnx("%s: group writable or world read/writable", fname);
681 return (-1);
683 return (0);
686 struct file *
687 newfile(const char *name, int secret, int required)
689 struct file *nfile;
691 nfile = calloc(1, sizeof(struct file));
692 if (nfile == NULL) {
693 log_warn("calloc");
694 return (NULL);
696 nfile->name = strdup(name);
697 if (nfile->name == NULL) {
698 log_warn("strdup");
699 free(nfile);
700 return (NULL);
702 nfile->stream = fopen(nfile->name, "r");
703 if (nfile->stream == NULL) {
704 if (required)
705 log_warn("open %s", nfile->name);
706 free(nfile->name);
707 free(nfile);
708 return (NULL);
709 } else if (secret &&
710 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
711 fclose(nfile->stream);
712 free(nfile->name);
713 free(nfile);
714 return (NULL);
716 nfile->lineno = 1;
717 return (nfile);
720 static void
721 closefile(struct file *xfile)
723 fclose(xfile->stream);
724 free(xfile->name);
725 free(xfile);
728 int
729 parse_config(const char *filename, enum gotd_procid proc_id,
730 struct gotd *env, int require_config_file)
732 struct sym *sym, *next;
733 struct gotd_repo *repo;
735 memset(env, 0, sizeof(*env));
737 gotd = env;
738 gotd_proc_id = proc_id;
739 TAILQ_INIT(&gotd->repos);
741 /* Apply default values. */
742 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
743 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
744 fprintf(stderr, "%s: unix socket path too long", __func__);
745 return -1;
747 if (strlcpy(gotd->user_name, GOTD_USER,
748 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
749 fprintf(stderr, "%s: user name too long", __func__);
750 return -1;
753 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
754 gotd->request_timeout.tv_usec = 0;
756 file = newfile(filename, 0, require_config_file);
757 if (file == NULL)
758 return require_config_file ? -1 : 0;
760 yyparse();
761 errors = file->errors;
762 closefile(file);
764 /* Free macros and check which have not been used. */
765 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
766 if ((gotd->verbosity > 1) && !sym->used)
767 fprintf(stderr, "warning: macro '%s' not used\n",
768 sym->nam);
769 if (!sym->persist) {
770 free(sym->nam);
771 free(sym->val);
772 TAILQ_REMOVE(&symhead, sym, entry);
773 free(sym);
777 if (errors)
778 return (-1);
780 TAILQ_FOREACH(repo, &gotd->repos, entry) {
781 if (repo->path[0] == '\0') {
782 log_warnx("repository \"%s\": no path provided in "
783 "configuration file", repo->name);
784 return (-1);
788 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
789 log_warnx("no repository defined in configuration file");
790 return (-1);
793 return (0);
796 static int
797 uid_connection_limit_cmp(const void *pa, const void *pb)
799 const struct gotd_uid_connection_limit *a = pa, *b = pb;
801 if (a->uid < b->uid)
802 return -1;
803 else if (a->uid > b->uid);
804 return 1;
806 return 0;
809 static int
810 conf_limit_user_connections(const char *user, int maximum)
812 uid_t uid;
813 struct gotd_uid_connection_limit *limit;
814 size_t nlimits;
816 if (maximum < 1) {
817 yyerror("max connections cannot be smaller 1");
818 return -1;
820 if (maximum > GOTD_MAXCLIENTS) {
821 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
822 return -1;
825 if (gotd_auth_parseuid(user, &uid) == -1) {
826 yyerror("%s: no such user", user);
827 return -1;
830 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
831 gotd->nconnection_limits, uid);
832 if (limit) {
833 limit->max_connections = maximum;
834 return 0;
837 limit = gotd->connection_limits;
838 nlimits = gotd->nconnection_limits + 1;
839 limit = reallocarray(limit, nlimits, sizeof(*limit));
840 if (limit == NULL)
841 fatal("reallocarray");
843 limit[nlimits - 1].uid = uid;
844 limit[nlimits - 1].max_connections = maximum;
846 gotd->connection_limits = limit;
847 gotd->nconnection_limits = nlimits;
848 qsort(gotd->connection_limits, gotd->nconnection_limits,
849 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
851 return 0;
854 static struct gotd_repo *
855 conf_new_repo(const char *name)
857 struct gotd_repo *repo;
859 if (name[0] == '\0') {
860 fatalx("syntax error: empty repository name found in %s",
861 file->name);
864 if (strchr(name, '\n') != NULL)
865 fatalx("repository names must not contain linefeeds: %s", name);
867 repo = calloc(1, sizeof(*repo));
868 if (repo == NULL)
869 fatalx("%s: calloc", __func__);
871 STAILQ_INIT(&repo->rules);
872 TAILQ_INIT(&repo->protected_tag_namespaces);
873 TAILQ_INIT(&repo->protected_branch_namespaces);
874 TAILQ_INIT(&repo->protected_branches);
876 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
877 sizeof(repo->name))
878 fatalx("%s: strlcpy", __func__);
880 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
881 gotd->nrepos++;
883 return repo;
884 };
886 static void
887 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
888 int authorization, char *identifier)
890 struct gotd_access_rule *rule;
892 rule = calloc(1, sizeof(*rule));
893 if (rule == NULL)
894 fatal("calloc");
896 rule->access = access;
897 rule->authorization = authorization;
898 rule->identifier = identifier;
900 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
903 static int
904 refname_is_valid(char *refname)
906 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
907 yyerror("reference name must begin with \"refs/\": %s",
908 refname);
909 return 0;
912 if (!got_ref_name_is_valid(refname)) {
913 yyerror("invalid reference name: %s", refname);
914 return 0;
917 return 1;
920 static int
921 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
922 char *namespace)
924 const struct got_error *error;
925 struct got_pathlist_entry *pe;
926 char *s;
928 *new = NULL;
930 got_path_strip_trailing_slashes(namespace);
931 if (!refname_is_valid(namespace))
932 return -1;
933 if (asprintf(&s, "%s/", namespace) == -1) {
934 yyerror("asprintf: %s", strerror(errno));
935 return -1;
938 error = got_pathlist_insert(&pe, refs, s, NULL);
939 if (error || pe == NULL) {
940 free(s);
941 if (error)
942 yyerror("got_pathlist_insert: %s", error->msg);
943 else
944 yyerror("duplicate protected namespace %s", namespace);
945 return -1;
948 *new = s;
949 return 0;
952 static int
953 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
955 struct got_pathlist_entry *pe;
956 char *new;
958 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
959 namespace) == -1)
960 return -1;
962 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
963 if (strcmp(pe->path, new) == 0) {
964 yyerror("duplicate protected namespace %s", namespace);
965 return -1;
969 return 0;
972 static int
973 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
975 struct got_pathlist_entry *pe;
976 char *new;
978 if (conf_protect_ref_namespace(&new,
979 &repo->protected_branch_namespaces, namespace) == -1)
980 return -1;
982 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
983 if (strcmp(pe->path, new) == 0) {
984 yyerror("duplicate protected namespace %s", namespace);
985 return -1;
989 return 0;
992 static int
993 conf_protect_branch(struct gotd_repo *repo, char *branchname)
995 const struct got_error *error;
996 struct got_pathlist_entry *new;
997 char *refname;
999 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1000 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1001 yyerror("asprintf: %s", strerror(errno));
1002 return -1;
1004 } else {
1005 refname = strdup(branchname);
1006 if (refname == NULL) {
1007 yyerror("strdup: %s", strerror(errno));
1008 return -1;
1012 if (!refname_is_valid(refname)) {
1013 free(refname);
1014 return -1;
1017 error = got_pathlist_insert(&new, &repo->protected_branches,
1018 refname, NULL);
1019 if (error || new == NULL) {
1020 free(refname);
1021 if (error)
1022 yyerror("got_pathlist_insert: %s", error->msg);
1023 else
1024 yyerror("duplicate protect branch %s", branchname);
1025 return -1;
1028 return 0;
1031 int
1032 symset(const char *nam, const char *val, int persist)
1034 struct sym *sym;
1036 TAILQ_FOREACH(sym, &symhead, entry) {
1037 if (strcmp(nam, sym->nam) == 0)
1038 break;
1041 if (sym != NULL) {
1042 if (sym->persist == 1)
1043 return (0);
1044 else {
1045 free(sym->nam);
1046 free(sym->val);
1047 TAILQ_REMOVE(&symhead, sym, entry);
1048 free(sym);
1051 sym = calloc(1, sizeof(*sym));
1052 if (sym == NULL)
1053 return (-1);
1055 sym->nam = strdup(nam);
1056 if (sym->nam == NULL) {
1057 free(sym);
1058 return (-1);
1060 sym->val = strdup(val);
1061 if (sym->val == NULL) {
1062 free(sym->nam);
1063 free(sym);
1064 return (-1);
1066 sym->used = 0;
1067 sym->persist = persist;
1068 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1069 return (0);
1072 char *
1073 symget(const char *nam)
1075 struct sym *sym;
1077 TAILQ_FOREACH(sym, &symhead, entry) {
1078 if (strcmp(nam, sym->nam) == 0) {
1079 sym->used = 1;
1080 return (sym->val);
1083 return (NULL);
1086 struct gotd_repo *
1087 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1089 struct gotd_repo *repo;
1090 size_t namelen;
1092 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1093 namelen = strlen(repo->name);
1094 if (strncmp(repo->name, repo_name, namelen) != 0)
1095 continue;
1096 if (repo_name[namelen] == '\0' ||
1097 strcmp(&repo_name[namelen], ".git") == 0)
1098 return repo;
1101 return NULL;
1104 struct gotd_repo *
1105 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1107 struct gotd_repo *repo;
1109 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1110 if (strcmp(repo->path, repo_path) == 0)
1111 return repo;
1114 return NULL;