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(
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;
256 | BRANCH NAMESPACE STRING {
257 if (gotd_proc_id == PROC_GOTD ||
258 gotd_proc_id == PROC_REPO_WRITE) {
259 if (conf_protect_branch_namespace(new_repo,
260 $3)) {
261 free($3);
262 YYERROR;
264 free($3);
267 | BRANCH STRING {
268 if (gotd_proc_id == PROC_GOTD ||
269 gotd_proc_id == PROC_REPO_WRITE) {
270 if (conf_protect_branch(new_repo, $2)) {
271 free($2);
272 YYERROR;
278 repository : REPOSITORY STRING {
279 struct gotd_repo *repo;
281 TAILQ_FOREACH(repo, &gotd->repos, entry) {
282 if (strcmp(repo->name, $2) == 0) {
283 yyerror("duplicate repository '%s'", $2);
284 free($2);
285 YYERROR;
289 if (gotd_proc_id == PROC_GOTD ||
290 gotd_proc_id == PROC_AUTH ||
291 gotd_proc_id == PROC_REPO_WRITE) {
292 new_repo = conf_new_repo($2);
294 free($2);
295 } '{' optnl repoopts2 '}' {
299 repoopts1 : PATH STRING {
300 if (gotd_proc_id == PROC_GOTD ||
301 gotd_proc_id == PROC_AUTH ||
302 gotd_proc_id == PROC_REPO_WRITE) {
303 if (!got_path_is_absolute($2)) {
304 yyerror("%s: path %s is not absolute",
305 __func__, $2);
306 free($2);
307 YYERROR;
309 if (realpath($2, new_repo->path) == NULL) {
310 yyerror("realpath %s: %s", $2, strerror(errno));
311 free($2);
312 YYERROR;
315 free($2);
317 | PERMIT RO STRING {
318 if (gotd_proc_id == PROC_AUTH) {
319 conf_new_access_rule(new_repo,
320 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
323 | PERMIT RW STRING {
324 if (gotd_proc_id == PROC_AUTH) {
325 conf_new_access_rule(new_repo,
326 GOTD_ACCESS_PERMITTED,
327 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
330 | DENY STRING {
331 if (gotd_proc_id == PROC_AUTH) {
332 conf_new_access_rule(new_repo,
333 GOTD_ACCESS_DENIED, 0, $2);
336 | protect
339 repoopts2 : repoopts2 repoopts1 nl
340 | repoopts1 optnl
343 nl : '\n' optnl
346 optnl : '\n' optnl /* zero or more newlines */
347 | /* empty */
350 %%
352 struct keywords {
353 const char *k_name;
354 int k_val;
355 };
357 int
358 yyerror(const char *fmt, ...)
360 va_list ap;
361 char *msg;
363 file->errors++;
364 va_start(ap, fmt);
365 if (vasprintf(&msg, fmt, ap) == -1)
366 fatalx("yyerror vasprintf");
367 va_end(ap);
368 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
369 free(msg);
370 return (0);
373 int
374 kw_cmp(const void *k, const void *e)
376 return (strcmp(k, ((const struct keywords *)e)->k_name));
379 int
380 lookup(char *s)
382 /* This has to be sorted always. */
383 static const struct keywords keywords[] = {
384 { "branch", BRANCH },
385 { "connection", CONNECTION },
386 { "deny", DENY },
387 { "limit", LIMIT },
388 { "listen", LISTEN },
389 { "namespace", NAMESPACE },
390 { "on", ON },
391 { "path", PATH },
392 { "permit", PERMIT },
393 { "protect", PROTECT },
394 { "repository", REPOSITORY },
395 { "request", REQUEST },
396 { "ro", RO },
397 { "rw", RW },
398 { "tag", TAG },
399 { "timeout", TIMEOUT },
400 { "user", USER },
401 };
402 const struct keywords *p;
404 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
405 sizeof(keywords[0]), kw_cmp);
407 if (p)
408 return (p->k_val);
409 else
410 return (STRING);
413 #define MAXPUSHBACK 128
415 unsigned char *parsebuf;
416 int parseindex;
417 unsigned char pushback_buffer[MAXPUSHBACK];
418 int pushback_index = 0;
420 int
421 lgetc(int quotec)
423 int c, next;
425 if (parsebuf) {
426 /* Read character from the parsebuffer instead of input. */
427 if (parseindex >= 0) {
428 c = parsebuf[parseindex++];
429 if (c != '\0')
430 return (c);
431 parsebuf = NULL;
432 } else
433 parseindex++;
436 if (pushback_index)
437 return (pushback_buffer[--pushback_index]);
439 if (quotec) {
440 c = getc(file->stream);
441 if (c == EOF)
442 yyerror("reached end of file while parsing "
443 "quoted string");
444 return (c);
447 c = getc(file->stream);
448 while (c == '\\') {
449 next = getc(file->stream);
450 if (next != '\n') {
451 c = next;
452 break;
454 yylval.lineno = file->lineno;
455 file->lineno++;
456 c = getc(file->stream);
459 return (c);
462 int
463 lungetc(int c)
465 if (c == EOF)
466 return (EOF);
467 if (parsebuf) {
468 parseindex--;
469 if (parseindex >= 0)
470 return (c);
472 if (pushback_index < MAXPUSHBACK-1)
473 return (pushback_buffer[pushback_index++] = c);
474 else
475 return (EOF);
478 int
479 findeol(void)
481 int c;
483 parsebuf = NULL;
485 /* Skip to either EOF or the first real EOL. */
486 while (1) {
487 if (pushback_index)
488 c = pushback_buffer[--pushback_index];
489 else
490 c = lgetc(0);
491 if (c == '\n') {
492 file->lineno++;
493 break;
495 if (c == EOF)
496 break;
498 return (ERROR);
501 int
502 yylex(void)
504 unsigned char buf[8096];
505 unsigned char *p, *val;
506 int quotec, next, c;
507 int token;
509 top:
510 p = buf;
511 c = lgetc(0);
512 while (c == ' ' || c == '\t')
513 c = lgetc(0); /* nothing */
515 yylval.lineno = file->lineno;
516 if (c == '#') {
517 c = lgetc(0);
518 while (c != '\n' && c != EOF)
519 c = lgetc(0); /* nothing */
521 if (c == '$' && parsebuf == NULL) {
522 while (1) {
523 c = lgetc(0);
524 if (c == EOF)
525 return (0);
527 if (p + 1 >= buf + sizeof(buf) - 1) {
528 yyerror("string too long");
529 return (findeol());
531 if (isalnum(c) || c == '_') {
532 *p++ = c;
533 continue;
535 *p = '\0';
536 lungetc(c);
537 break;
539 val = symget(buf);
540 if (val == NULL) {
541 yyerror("macro '%s' not defined", buf);
542 return (findeol());
544 parsebuf = val;
545 parseindex = 0;
546 goto top;
549 switch (c) {
550 case '\'':
551 case '"':
552 quotec = c;
553 while (1) {
554 c = lgetc(quotec);
555 if (c == EOF)
556 return (0);
557 if (c == '\n') {
558 file->lineno++;
559 continue;
560 } else if (c == '\\') {
561 next = lgetc(quotec);
562 if (next == EOF)
563 return (0);
564 if (next == quotec || c == ' ' || c == '\t')
565 c = next;
566 else if (next == '\n') {
567 file->lineno++;
568 continue;
569 } else
570 lungetc(next);
571 } else if (c == quotec) {
572 *p = '\0';
573 break;
574 } else if (c == '\0') {
575 yyerror("syntax error");
576 return (findeol());
578 if (p + 1 >= buf + sizeof(buf) - 1) {
579 yyerror("string too long");
580 return (findeol());
582 *p++ = c;
584 yylval.v.string = strdup(buf);
585 if (yylval.v.string == NULL)
586 err(1, "yylex: strdup");
587 return (STRING);
590 #define allowed_to_end_number(x) \
591 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
593 if (c == '-' || isdigit(c)) {
594 do {
595 *p++ = c;
596 if ((unsigned)(p-buf) >= sizeof(buf)) {
597 yyerror("string too long");
598 return (findeol());
600 c = lgetc(0);
601 } while (c != EOF && isdigit(c));
602 lungetc(c);
603 if (p == buf + 1 && buf[0] == '-')
604 goto nodigits;
605 if (c == EOF || allowed_to_end_number(c)) {
606 const char *errstr = NULL;
608 *p = '\0';
609 yylval.v.number = strtonum(buf, LLONG_MIN,
610 LLONG_MAX, &errstr);
611 if (errstr) {
612 yyerror("\"%s\" invalid number: %s",
613 buf, errstr);
614 return (findeol());
616 return (NUMBER);
617 } else {
618 nodigits:
619 while (p > buf + 1)
620 lungetc(*--p);
621 c = *--p;
622 if (c == '-')
623 return (c);
627 #define allowed_in_string(x) \
628 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
629 x != '{' && x != '}' && \
630 x != '!' && x != '=' && x != '#' && \
631 x != ','))
633 if (isalnum(c) || c == ':' || c == '_') {
634 do {
635 *p++ = c;
636 if ((unsigned)(p-buf) >= sizeof(buf)) {
637 yyerror("string too long");
638 return (findeol());
640 c = lgetc(0);
641 } while (c != EOF && (allowed_in_string(c)));
642 lungetc(c);
643 *p = '\0';
644 token = lookup(buf);
645 if (token == STRING) {
646 yylval.v.string = strdup(buf);
647 if (yylval.v.string == NULL)
648 err(1, "yylex: strdup");
650 return (token);
652 if (c == '\n') {
653 yylval.lineno = file->lineno;
654 file->lineno++;
656 if (c == EOF)
657 return (0);
658 return (c);
661 int
662 check_file_secrecy(int fd, const char *fname)
664 struct stat st;
666 if (fstat(fd, &st)) {
667 log_warn("cannot stat %s", fname);
668 return (-1);
670 if (st.st_uid != 0 && st.st_uid != getuid()) {
671 log_warnx("%s: owner not root or current user", fname);
672 return (-1);
674 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
675 log_warnx("%s: group writable or world read/writable", fname);
676 return (-1);
678 return (0);
681 struct file *
682 newfile(const char *name, int secret, int required)
684 struct file *nfile;
686 nfile = calloc(1, sizeof(struct file));
687 if (nfile == NULL) {
688 log_warn("calloc");
689 return (NULL);
691 nfile->name = strdup(name);
692 if (nfile->name == NULL) {
693 log_warn("strdup");
694 free(nfile);
695 return (NULL);
697 nfile->stream = fopen(nfile->name, "r");
698 if (nfile->stream == NULL) {
699 if (required)
700 log_warn("open %s", nfile->name);
701 free(nfile->name);
702 free(nfile);
703 return (NULL);
704 } else if (secret &&
705 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
706 fclose(nfile->stream);
707 free(nfile->name);
708 free(nfile);
709 return (NULL);
711 nfile->lineno = 1;
712 return (nfile);
715 static void
716 closefile(struct file *xfile)
718 fclose(xfile->stream);
719 free(xfile->name);
720 free(xfile);
723 int
724 parse_config(const char *filename, enum gotd_procid proc_id,
725 struct gotd *env, int require_config_file)
727 struct sym *sym, *next;
728 struct gotd_repo *repo;
730 memset(env, 0, sizeof(*env));
732 gotd = env;
733 gotd_proc_id = proc_id;
734 TAILQ_INIT(&gotd->repos);
736 /* Apply default values. */
737 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
738 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
739 fprintf(stderr, "%s: unix socket path too long", __func__);
740 return -1;
742 if (strlcpy(gotd->user_name, GOTD_USER,
743 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
744 fprintf(stderr, "%s: user name too long", __func__);
745 return -1;
748 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
749 gotd->request_timeout.tv_usec = 0;
751 file = newfile(filename, 0, require_config_file);
752 if (file == NULL)
753 return require_config_file ? -1 : 0;
755 yyparse();
756 errors = file->errors;
757 closefile(file);
759 /* Free macros and check which have not been used. */
760 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
761 if ((gotd->verbosity > 1) && !sym->used)
762 fprintf(stderr, "warning: macro '%s' not used\n",
763 sym->nam);
764 if (!sym->persist) {
765 free(sym->nam);
766 free(sym->val);
767 TAILQ_REMOVE(&symhead, sym, entry);
768 free(sym);
772 if (errors)
773 return (-1);
775 TAILQ_FOREACH(repo, &gotd->repos, entry) {
776 if (repo->path[0] == '\0') {
777 log_warnx("repository \"%s\": no path provided in "
778 "configuration file", repo->name);
779 return (-1);
783 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
784 log_warnx("no repository defined in configuration file");
785 return (-1);
788 return (0);
791 static int
792 uid_connection_limit_cmp(const void *pa, const void *pb)
794 const struct gotd_uid_connection_limit *a = pa, *b = pb;
796 if (a->uid < b->uid)
797 return -1;
798 else if (a->uid > b->uid);
799 return 1;
801 return 0;
804 static int
805 conf_limit_user_connections(const char *user, int maximum)
807 uid_t uid;
808 struct gotd_uid_connection_limit *limit;
809 size_t nlimits;
811 if (maximum < 1) {
812 yyerror("max connections cannot be smaller 1");
813 return -1;
815 if (maximum > GOTD_MAXCLIENTS) {
816 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
817 return -1;
820 if (gotd_auth_parseuid(user, &uid) == -1) {
821 yyerror("%s: no such user", user);
822 return -1;
825 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
826 gotd->nconnection_limits, uid);
827 if (limit) {
828 limit->max_connections = maximum;
829 return 0;
832 limit = gotd->connection_limits;
833 nlimits = gotd->nconnection_limits + 1;
834 limit = reallocarray(limit, nlimits, sizeof(*limit));
835 if (limit == NULL)
836 fatal("reallocarray");
838 limit[nlimits - 1].uid = uid;
839 limit[nlimits - 1].max_connections = maximum;
841 gotd->connection_limits = limit;
842 gotd->nconnection_limits = nlimits;
843 qsort(gotd->connection_limits, gotd->nconnection_limits,
844 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
846 return 0;
849 static struct gotd_repo *
850 conf_new_repo(const char *name)
852 struct gotd_repo *repo;
854 if (name[0] == '\0') {
855 fatalx("syntax error: empty repository name found in %s",
856 file->name);
859 if (strchr(name, '\n') != NULL)
860 fatalx("repository names must not contain linefeeds: %s", name);
862 repo = calloc(1, sizeof(*repo));
863 if (repo == NULL)
864 fatalx("%s: calloc", __func__);
866 STAILQ_INIT(&repo->rules);
867 TAILQ_INIT(&repo->protected_tag_namespaces);
868 TAILQ_INIT(&repo->protected_branch_namespaces);
869 TAILQ_INIT(&repo->protected_branches);
871 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
872 sizeof(repo->name))
873 fatalx("%s: strlcpy", __func__);
875 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
876 gotd->nrepos++;
878 return repo;
879 };
881 static void
882 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
883 int authorization, char *identifier)
885 struct gotd_access_rule *rule;
887 rule = calloc(1, sizeof(*rule));
888 if (rule == NULL)
889 fatal("calloc");
891 rule->access = access;
892 rule->authorization = authorization;
893 rule->identifier = identifier;
895 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
898 static int
899 refname_is_valid(char *refname)
901 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
902 yyerror("reference name must begin with \"refs/\": %s",
903 refname);
904 return 0;
907 if (!got_ref_name_is_valid(refname)) {
908 yyerror("invalid reference name: %s", refname);
909 return 0;
912 return 1;
915 static int
916 conf_protect_ref_namespace(struct got_pathlist_head *refs, char *namespace)
918 const struct got_error *error;
919 struct got_pathlist_entry *new;
920 char *s;
922 got_path_strip_trailing_slashes(namespace);
923 if (!refname_is_valid(namespace))
924 return -1;
925 if (asprintf(&s, "%s/", namespace) == -1) {
926 yyerror("asprintf: %s", strerror(errno));
927 return -1;
930 error = got_pathlist_insert(&new, refs, s, NULL);
931 if (error || new == NULL) {
932 free(s);
933 if (error)
934 yyerror("got_pathlist_insert: %s", error->msg);
935 else
936 yyerror("duplicate protect namespace %s", namespace);
937 return -1;
940 return 0;
943 static int
944 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
946 return conf_protect_ref_namespace(&repo->protected_tag_namespaces,
947 namespace);
950 static int
951 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
953 return conf_protect_ref_namespace(&repo->protected_branch_namespaces,
954 namespace);
957 static int
958 conf_protect_branch(struct gotd_repo *repo, char *branchname)
960 const struct got_error *error;
961 struct got_pathlist_entry *new;
962 char *refname;
964 if (strncmp(branchname, "refs/heads/", 11) != 0) {
965 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
966 yyerror("asprintf: %s", strerror(errno));
967 return -1;
969 } else {
970 refname = strdup(branchname);
971 if (refname == NULL) {
972 yyerror("strdup: %s", strerror(errno));
973 return -1;
977 if (!refname_is_valid(refname)) {
978 free(refname);
979 return -1;
982 error = got_pathlist_insert(&new, &repo->protected_branches,
983 refname, NULL);
984 if (error || new == NULL) {
985 free(refname);
986 if (error)
987 yyerror("got_pathlist_insert: %s", error->msg);
988 else
989 yyerror("duplicate protect branch %s", branchname);
990 return -1;
993 return 0;
996 int
997 symset(const char *nam, const char *val, int persist)
999 struct sym *sym;
1001 TAILQ_FOREACH(sym, &symhead, entry) {
1002 if (strcmp(nam, sym->nam) == 0)
1003 break;
1006 if (sym != NULL) {
1007 if (sym->persist == 1)
1008 return (0);
1009 else {
1010 free(sym->nam);
1011 free(sym->val);
1012 TAILQ_REMOVE(&symhead, sym, entry);
1013 free(sym);
1016 sym = calloc(1, sizeof(*sym));
1017 if (sym == NULL)
1018 return (-1);
1020 sym->nam = strdup(nam);
1021 if (sym->nam == NULL) {
1022 free(sym);
1023 return (-1);
1025 sym->val = strdup(val);
1026 if (sym->val == NULL) {
1027 free(sym->nam);
1028 free(sym);
1029 return (-1);
1031 sym->used = 0;
1032 sym->persist = persist;
1033 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1034 return (0);
1037 char *
1038 symget(const char *nam)
1040 struct sym *sym;
1042 TAILQ_FOREACH(sym, &symhead, entry) {
1043 if (strcmp(nam, sym->nam) == 0) {
1044 sym->used = 1;
1045 return (sym->val);
1048 return (NULL);
1051 struct gotd_repo *
1052 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1054 struct gotd_repo *repo;
1055 size_t namelen;
1057 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1058 namelen = strlen(repo->name);
1059 if (strncmp(repo->name, repo_name, namelen) != 0)
1060 continue;
1061 if (repo_name[namelen] == '\0' ||
1062 strcmp(&repo_name[namelen], ".git") == 0)
1063 return repo;
1066 return NULL;
1069 struct gotd_repo *
1070 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1072 struct gotd_repo *repo;
1074 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1075 if (strcmp(repo->path, repo_path) == 0)
1076 return repo;
1079 return NULL;