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, strerror(errno));
315 free($2);
316 YYERROR;
319 free($2);
321 | PERMIT RO STRING {
322 if (gotd_proc_id == PROC_AUTH) {
323 conf_new_access_rule(new_repo,
324 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
325 } else
326 free($3);
328 | PERMIT RW STRING {
329 if (gotd_proc_id == PROC_AUTH) {
330 conf_new_access_rule(new_repo,
331 GOTD_ACCESS_PERMITTED,
332 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
333 } else
334 free($3);
336 | DENY STRING {
337 if (gotd_proc_id == PROC_AUTH) {
338 conf_new_access_rule(new_repo,
339 GOTD_ACCESS_DENIED, 0, $2);
340 } else
341 free($2);
343 | protect
346 repoopts2 : repoopts2 repoopts1 nl
347 | repoopts1 optnl
350 nl : '\n' optnl
353 optnl : '\n' optnl /* zero or more newlines */
354 | /* empty */
357 %%
359 struct keywords {
360 const char *k_name;
361 int k_val;
362 };
364 int
365 yyerror(const char *fmt, ...)
367 va_list ap;
368 char *msg;
370 file->errors++;
371 va_start(ap, fmt);
372 if (vasprintf(&msg, fmt, ap) == -1)
373 fatalx("yyerror vasprintf");
374 va_end(ap);
375 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
376 free(msg);
377 return (0);
380 int
381 kw_cmp(const void *k, const void *e)
383 return (strcmp(k, ((const struct keywords *)e)->k_name));
386 int
387 lookup(char *s)
389 /* This has to be sorted always. */
390 static const struct keywords keywords[] = {
391 { "branch", BRANCH },
392 { "connection", CONNECTION },
393 { "deny", DENY },
394 { "limit", LIMIT },
395 { "listen", LISTEN },
396 { "namespace", NAMESPACE },
397 { "on", ON },
398 { "path", PATH },
399 { "permit", PERMIT },
400 { "protect", PROTECT },
401 { "repository", REPOSITORY },
402 { "request", REQUEST },
403 { "ro", RO },
404 { "rw", RW },
405 { "tag", TAG },
406 { "timeout", TIMEOUT },
407 { "user", USER },
408 };
409 const struct keywords *p;
411 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
412 sizeof(keywords[0]), kw_cmp);
414 if (p)
415 return (p->k_val);
416 else
417 return (STRING);
420 #define MAXPUSHBACK 128
422 unsigned char *parsebuf;
423 int parseindex;
424 unsigned char pushback_buffer[MAXPUSHBACK];
425 int pushback_index = 0;
427 int
428 lgetc(int quotec)
430 int c, next;
432 if (parsebuf) {
433 /* Read character from the parsebuffer instead of input. */
434 if (parseindex >= 0) {
435 c = parsebuf[parseindex++];
436 if (c != '\0')
437 return (c);
438 parsebuf = NULL;
439 } else
440 parseindex++;
443 if (pushback_index)
444 return (pushback_buffer[--pushback_index]);
446 if (quotec) {
447 c = getc(file->stream);
448 if (c == EOF)
449 yyerror("reached end of file while parsing "
450 "quoted string");
451 return (c);
454 c = getc(file->stream);
455 while (c == '\\') {
456 next = getc(file->stream);
457 if (next != '\n') {
458 c = next;
459 break;
461 yylval.lineno = file->lineno;
462 file->lineno++;
463 c = getc(file->stream);
466 return (c);
469 int
470 lungetc(int c)
472 if (c == EOF)
473 return (EOF);
474 if (parsebuf) {
475 parseindex--;
476 if (parseindex >= 0)
477 return (c);
479 if (pushback_index < MAXPUSHBACK-1)
480 return (pushback_buffer[pushback_index++] = c);
481 else
482 return (EOF);
485 int
486 findeol(void)
488 int c;
490 parsebuf = NULL;
492 /* Skip to either EOF or the first real EOL. */
493 while (1) {
494 if (pushback_index)
495 c = pushback_buffer[--pushback_index];
496 else
497 c = lgetc(0);
498 if (c == '\n') {
499 file->lineno++;
500 break;
502 if (c == EOF)
503 break;
505 return (ERROR);
508 int
509 yylex(void)
511 unsigned char buf[8096];
512 unsigned char *p, *val;
513 int quotec, next, c;
514 int token;
516 top:
517 p = buf;
518 c = lgetc(0);
519 while (c == ' ' || c == '\t')
520 c = lgetc(0); /* nothing */
522 yylval.lineno = file->lineno;
523 if (c == '#') {
524 c = lgetc(0);
525 while (c != '\n' && c != EOF)
526 c = lgetc(0); /* nothing */
528 if (c == '$' && parsebuf == NULL) {
529 while (1) {
530 c = lgetc(0);
531 if (c == EOF)
532 return (0);
534 if (p + 1 >= buf + sizeof(buf) - 1) {
535 yyerror("string too long");
536 return (findeol());
538 if (isalnum(c) || c == '_') {
539 *p++ = c;
540 continue;
542 *p = '\0';
543 lungetc(c);
544 break;
546 val = symget(buf);
547 if (val == NULL) {
548 yyerror("macro '%s' not defined", buf);
549 return (findeol());
551 parsebuf = val;
552 parseindex = 0;
553 goto top;
556 switch (c) {
557 case '\'':
558 case '"':
559 quotec = c;
560 while (1) {
561 c = lgetc(quotec);
562 if (c == EOF)
563 return (0);
564 if (c == '\n') {
565 file->lineno++;
566 continue;
567 } else if (c == '\\') {
568 next = lgetc(quotec);
569 if (next == EOF)
570 return (0);
571 if (next == quotec || c == ' ' || c == '\t')
572 c = next;
573 else if (next == '\n') {
574 file->lineno++;
575 continue;
576 } else
577 lungetc(next);
578 } else if (c == quotec) {
579 *p = '\0';
580 break;
581 } else if (c == '\0') {
582 yyerror("syntax error");
583 return (findeol());
585 if (p + 1 >= buf + sizeof(buf) - 1) {
586 yyerror("string too long");
587 return (findeol());
589 *p++ = c;
591 yylval.v.string = strdup(buf);
592 if (yylval.v.string == NULL)
593 err(1, "yylex: strdup");
594 return (STRING);
597 #define allowed_to_end_number(x) \
598 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
600 if (c == '-' || isdigit(c)) {
601 do {
602 *p++ = c;
603 if ((unsigned)(p-buf) >= sizeof(buf)) {
604 yyerror("string too long");
605 return (findeol());
607 c = lgetc(0);
608 } while (c != EOF && isdigit(c));
609 lungetc(c);
610 if (p == buf + 1 && buf[0] == '-')
611 goto nodigits;
612 if (c == EOF || allowed_to_end_number(c)) {
613 const char *errstr = NULL;
615 *p = '\0';
616 yylval.v.number = strtonum(buf, LLONG_MIN,
617 LLONG_MAX, &errstr);
618 if (errstr) {
619 yyerror("\"%s\" invalid number: %s",
620 buf, errstr);
621 return (findeol());
623 return (NUMBER);
624 } else {
625 nodigits:
626 while (p > buf + 1)
627 lungetc(*--p);
628 c = *--p;
629 if (c == '-')
630 return (c);
634 #define allowed_in_string(x) \
635 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
636 x != '{' && x != '}' && \
637 x != '!' && x != '=' && x != '#' && \
638 x != ','))
640 if (isalnum(c) || c == ':' || c == '_') {
641 do {
642 *p++ = c;
643 if ((unsigned)(p-buf) >= sizeof(buf)) {
644 yyerror("string too long");
645 return (findeol());
647 c = lgetc(0);
648 } while (c != EOF && (allowed_in_string(c)));
649 lungetc(c);
650 *p = '\0';
651 token = lookup(buf);
652 if (token == STRING) {
653 yylval.v.string = strdup(buf);
654 if (yylval.v.string == NULL)
655 err(1, "yylex: strdup");
657 return (token);
659 if (c == '\n') {
660 yylval.lineno = file->lineno;
661 file->lineno++;
663 if (c == EOF)
664 return (0);
665 return (c);
668 int
669 check_file_secrecy(int fd, const char *fname)
671 struct stat st;
673 if (fstat(fd, &st)) {
674 log_warn("cannot stat %s", fname);
675 return (-1);
677 if (st.st_uid != 0 && st.st_uid != getuid()) {
678 log_warnx("%s: owner not root or current user", fname);
679 return (-1);
681 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
682 log_warnx("%s: group writable or world read/writable", fname);
683 return (-1);
685 return (0);
688 struct file *
689 newfile(const char *name, int secret, int required)
691 struct file *nfile;
693 nfile = calloc(1, sizeof(struct file));
694 if (nfile == NULL) {
695 log_warn("calloc");
696 return (NULL);
698 nfile->name = strdup(name);
699 if (nfile->name == NULL) {
700 log_warn("strdup");
701 free(nfile);
702 return (NULL);
704 nfile->stream = fopen(nfile->name, "r");
705 if (nfile->stream == NULL) {
706 if (required)
707 log_warn("open %s", nfile->name);
708 free(nfile->name);
709 free(nfile);
710 return (NULL);
711 } else if (secret &&
712 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
713 fclose(nfile->stream);
714 free(nfile->name);
715 free(nfile);
716 return (NULL);
718 nfile->lineno = 1;
719 return (nfile);
722 static void
723 closefile(struct file *xfile)
725 fclose(xfile->stream);
726 free(xfile->name);
727 free(xfile);
730 int
731 parse_config(const char *filename, enum gotd_procid proc_id,
732 struct gotd *env, int require_config_file)
734 struct sym *sym, *next;
735 struct gotd_repo *repo;
737 memset(env, 0, sizeof(*env));
739 gotd = env;
740 gotd_proc_id = proc_id;
741 TAILQ_INIT(&gotd->repos);
743 /* Apply default values. */
744 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
745 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
746 fprintf(stderr, "%s: unix socket path too long", __func__);
747 return -1;
749 if (strlcpy(gotd->user_name, GOTD_USER,
750 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
751 fprintf(stderr, "%s: user name too long", __func__);
752 return -1;
755 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
756 gotd->request_timeout.tv_usec = 0;
758 file = newfile(filename, 0, require_config_file);
759 if (file == NULL)
760 return require_config_file ? -1 : 0;
762 yyparse();
763 errors = file->errors;
764 closefile(file);
766 /* Free macros and check which have not been used. */
767 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
768 if ((gotd->verbosity > 1) && !sym->used)
769 fprintf(stderr, "warning: macro '%s' not used\n",
770 sym->nam);
771 if (!sym->persist) {
772 free(sym->nam);
773 free(sym->val);
774 TAILQ_REMOVE(&symhead, sym, entry);
775 free(sym);
779 if (errors)
780 return (-1);
782 TAILQ_FOREACH(repo, &gotd->repos, entry) {
783 if (repo->path[0] == '\0') {
784 log_warnx("repository \"%s\": no path provided in "
785 "configuration file", repo->name);
786 return (-1);
790 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
791 log_warnx("no repository defined in configuration file");
792 return (-1);
795 return (0);
798 static int
799 uid_connection_limit_cmp(const void *pa, const void *pb)
801 const struct gotd_uid_connection_limit *a = pa, *b = pb;
803 if (a->uid < b->uid)
804 return -1;
805 else if (a->uid > b->uid);
806 return 1;
808 return 0;
811 static int
812 conf_limit_user_connections(const char *user, int maximum)
814 uid_t uid;
815 struct gotd_uid_connection_limit *limit;
816 size_t nlimits;
818 if (maximum < 1) {
819 yyerror("max connections cannot be smaller 1");
820 return -1;
822 if (maximum > GOTD_MAXCLIENTS) {
823 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
824 return -1;
827 if (gotd_auth_parseuid(user, &uid) == -1) {
828 yyerror("%s: no such user", user);
829 return -1;
832 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
833 gotd->nconnection_limits, uid);
834 if (limit) {
835 limit->max_connections = maximum;
836 return 0;
839 limit = gotd->connection_limits;
840 nlimits = gotd->nconnection_limits + 1;
841 limit = reallocarray(limit, nlimits, sizeof(*limit));
842 if (limit == NULL)
843 fatal("reallocarray");
845 limit[nlimits - 1].uid = uid;
846 limit[nlimits - 1].max_connections = maximum;
848 gotd->connection_limits = limit;
849 gotd->nconnection_limits = nlimits;
850 qsort(gotd->connection_limits, gotd->nconnection_limits,
851 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
853 return 0;
856 static struct gotd_repo *
857 conf_new_repo(const char *name)
859 struct gotd_repo *repo;
861 if (name[0] == '\0') {
862 fatalx("syntax error: empty repository name found in %s",
863 file->name);
866 if (strchr(name, '\n') != NULL)
867 fatalx("repository names must not contain linefeeds: %s", name);
869 repo = calloc(1, sizeof(*repo));
870 if (repo == NULL)
871 fatalx("%s: calloc", __func__);
873 STAILQ_INIT(&repo->rules);
874 TAILQ_INIT(&repo->protected_tag_namespaces);
875 TAILQ_INIT(&repo->protected_branch_namespaces);
876 TAILQ_INIT(&repo->protected_branches);
878 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
879 sizeof(repo->name))
880 fatalx("%s: strlcpy", __func__);
882 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
883 gotd->nrepos++;
885 return repo;
886 };
888 static void
889 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
890 int authorization, char *identifier)
892 struct gotd_access_rule *rule;
894 rule = calloc(1, sizeof(*rule));
895 if (rule == NULL)
896 fatal("calloc");
898 rule->access = access;
899 rule->authorization = authorization;
900 rule->identifier = identifier;
902 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
905 static int
906 refname_is_valid(char *refname)
908 if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
909 yyerror("reference name must begin with \"refs/\": %s",
910 refname);
911 return 0;
914 if (!got_ref_name_is_valid(refname)) {
915 yyerror("invalid reference name: %s", refname);
916 return 0;
919 return 1;
922 static int
923 conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
924 char *namespace)
926 const struct got_error *error;
927 struct got_pathlist_entry *pe;
928 char *s;
930 *new = NULL;
932 got_path_strip_trailing_slashes(namespace);
933 if (!refname_is_valid(namespace))
934 return -1;
935 if (asprintf(&s, "%s/", namespace) == -1) {
936 yyerror("asprintf: %s", strerror(errno));
937 return -1;
940 error = got_pathlist_insert(&pe, refs, s, NULL);
941 if (error || pe == NULL) {
942 free(s);
943 if (error)
944 yyerror("got_pathlist_insert: %s", error->msg);
945 else
946 yyerror("duplicate protected namespace %s", namespace);
947 return -1;
950 *new = s;
951 return 0;
954 static int
955 conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
957 struct got_pathlist_entry *pe;
958 char *new;
960 if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
961 namespace) == -1)
962 return -1;
964 TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
965 if (strcmp(pe->path, new) == 0) {
966 yyerror("duplicate protected namespace %s", namespace);
967 return -1;
971 return 0;
974 static int
975 conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
977 struct got_pathlist_entry *pe;
978 char *new;
980 if (conf_protect_ref_namespace(&new,
981 &repo->protected_branch_namespaces, namespace) == -1)
982 return -1;
984 TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
985 if (strcmp(pe->path, new) == 0) {
986 yyerror("duplicate protected namespace %s", namespace);
987 return -1;
991 return 0;
994 static int
995 conf_protect_branch(struct gotd_repo *repo, char *branchname)
997 const struct got_error *error;
998 struct got_pathlist_entry *new;
999 char *refname;
1001 if (strncmp(branchname, "refs/heads/", 11) != 0) {
1002 if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1003 yyerror("asprintf: %s", strerror(errno));
1004 return -1;
1006 } else {
1007 refname = strdup(branchname);
1008 if (refname == NULL) {
1009 yyerror("strdup: %s", strerror(errno));
1010 return -1;
1014 if (!refname_is_valid(refname)) {
1015 free(refname);
1016 return -1;
1019 error = got_pathlist_insert(&new, &repo->protected_branches,
1020 refname, NULL);
1021 if (error || new == NULL) {
1022 free(refname);
1023 if (error)
1024 yyerror("got_pathlist_insert: %s", error->msg);
1025 else
1026 yyerror("duplicate protect branch %s", branchname);
1027 return -1;
1030 return 0;
1033 int
1034 symset(const char *nam, const char *val, int persist)
1036 struct sym *sym;
1038 TAILQ_FOREACH(sym, &symhead, entry) {
1039 if (strcmp(nam, sym->nam) == 0)
1040 break;
1043 if (sym != NULL) {
1044 if (sym->persist == 1)
1045 return (0);
1046 else {
1047 free(sym->nam);
1048 free(sym->val);
1049 TAILQ_REMOVE(&symhead, sym, entry);
1050 free(sym);
1053 sym = calloc(1, sizeof(*sym));
1054 if (sym == NULL)
1055 return (-1);
1057 sym->nam = strdup(nam);
1058 if (sym->nam == NULL) {
1059 free(sym);
1060 return (-1);
1062 sym->val = strdup(val);
1063 if (sym->val == NULL) {
1064 free(sym->nam);
1065 free(sym);
1066 return (-1);
1068 sym->used = 0;
1069 sym->persist = persist;
1070 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1071 return (0);
1074 char *
1075 symget(const char *nam)
1077 struct sym *sym;
1079 TAILQ_FOREACH(sym, &symhead, entry) {
1080 if (strcmp(nam, sym->nam) == 0) {
1081 sym->used = 1;
1082 return (sym->val);
1085 return (NULL);
1088 struct gotd_repo *
1089 gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1091 struct gotd_repo *repo;
1092 size_t namelen;
1094 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1095 namelen = strlen(repo->name);
1096 if (strncmp(repo->name, repo_name, namelen) != 0)
1097 continue;
1098 if (repo_name[namelen] == '\0' ||
1099 strcmp(&repo_name[namelen], ".git") == 0)
1100 return repo;
1103 return NULL;
1106 struct gotd_repo *
1107 gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1109 struct gotd_repo *repo;
1111 TAILQ_FOREACH(repo, &gotd->repos, entry) {
1112 if (strcmp(repo->path, repo_path) == 0)
1113 return repo;
1116 return NULL;