2 * Copyright (c) 2020, 2021 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
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.
25 #include <sys/types.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 TAILQ_ENTRY(sym) entry;
76 int symset(const char *, const char *, int);
77 int cmdline_symset(char *);
78 char *symget(const char *);
80 static int atoul(char *, u_long *);
82 static const struct got_error* gerror;
83 static struct gotconfig_remote_repo *remote;
84 static struct gotconfig gotconfig;
85 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
86 static const struct got_error* new_fetch_config(struct fetch_config **);
87 static const struct got_error* new_send_config(struct send_config **);
93 struct node_branch *branch;
99 #if defined(__APPLE__) && !defined(YYSTYPE)
100 #warning "Setting YYSTYPE - is GNU Bison installed?"
101 #define YYSTYPE YYSTYPE
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
107 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
109 %token <v.string> STRING
110 %token <v.number> NUMBER
111 %type <v.number> boolean portplain
112 %type <v.string> numberstring
113 %type <v.branch> branch xbranch branch_list
114 %type <v.ref> ref xref ref_list
118 grammar : /* empty */
120 | grammar author '\n'
121 | grammar remote '\n'
122 | grammar allowed_signers '\n'
125 if (strcasecmp($1, "true") == 0 ||
126 strcasecmp($1, "yes") == 0)
128 else if (strcasecmp($1, "false") == 0 ||
129 strcasecmp($1, "no") == 0)
132 yyerror("invalid boolean value '%s'", $1);
139 numberstring : NUMBER {
141 if (asprintf(&s, "%lld", $1) == -1) {
142 yyerror("string: asprintf");
149 portplain : numberstring {
150 if (parseport($1, &$$) == -1) {
157 branch : /* empty */ { $$ = NULL; }
158 | xbranch { $$ = $1; }
159 | '{' optnl branch_list '}' { $$ = $3; }
162 $$ = calloc(1, sizeof(struct node_branch));
167 $$->branch_name = $1;
171 branch_list : xbranch optnl { $$ = $1; }
172 | branch_list comma xbranch optnl {
178 ref : /* empty */ { $$ = NULL; }
180 | '{' optnl ref_list '}' { $$ = $3; }
183 $$ = calloc(1, sizeof(struct node_ref));
192 ref_list : xref optnl { $$ = $1; }
193 | ref_list comma xref optnl {
199 remoteopts2 : remoteopts2 remoteopts1 nl
202 remoteopts1 : REPOSITORY STRING {
203 remote->repository = $2;
209 remote->protocol = $2;
211 | MIRROR_REFERENCES boolean {
212 remote->mirror_references = $2;
214 | FETCH_ALL_BRANCHES boolean {
215 remote->fetch_all_branches = $2;
224 remote->fetch_ref = $2;
227 static const struct got_error* error;
229 if (remote->fetch_config != NULL) {
230 yyerror("fetch block already exists");
233 error = new_fetch_config(&remote->fetch_config);
235 yyerror("%s", error->msg);
238 } '{' optnl fetchempty '}'
240 static const struct got_error* error;
242 if (remote->send_config != NULL) {
243 yyerror("send block already exists");
246 error = new_send_config(&remote->send_config);
248 yyerror("%s", error->msg);
251 } '{' optnl sendempty '}'
253 fetchempty : /* empty */
256 fetchopts2 : fetchopts2 fetchopts1 nl
259 fetchopts1 : REPOSITORY STRING {
260 remote->fetch_config->repository = $2;
263 remote->fetch_config->server = $2;
266 remote->fetch_config->protocol = $2;
269 remote->fetch_config->port = $2;
272 remote->fetch_config->branch = $2;
275 sendempty : /* empty */
278 sendopts2 : sendopts2 sendopts1 nl
281 sendopts1 : REPOSITORY STRING {
282 remote->send_config->repository = $2;
285 remote->send_config->server = $2;
288 remote->send_config->protocol = $2;
291 remote->send_config->port = $2;
294 remote->send_config->branch = $2;
297 remote : REMOTE STRING {
298 static const struct got_error* error;
300 error = new_remote(&remote);
303 yyerror("%s", error->msg);
307 } '{' optnl remoteopts2 '}' {
308 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
309 gotconfig.nremotes++;
312 author : AUTHOR STRING {
313 gotconfig.author = $2;
316 allowed_signers : ALLOWED_SIGNERS STRING {
317 gotconfig.allowed_signers_file = $2;
320 revoked_signers : REVOKED_SIGNERS STRING {
321 gotconfig.revoked_signers_file = $2;
340 yyerror(const char *fmt, ...)
347 if (vasprintf(&msg, fmt, ap) == -1) {
348 gerror = got_error_from_errno("vasprintf");
352 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
354 gerror = got_error_from_errno("asprintf");
357 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
362 kw_cmp(const void *k, const void *e)
364 return (strcmp(k, ((const struct keywords *)e)->k_name));
370 /* This has to be sorted always. */
371 static const struct keywords keywords[] = {
372 {"allowed_signers", ALLOWED_SIGNERS},
376 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
377 {"fetch_all_branches", FETCH_ALL_BRANCHES},
378 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
379 {"mirror_references", MIRROR_REFERENCES},
381 {"protocol", PROTOCOL},
382 {"reference", REFERENCE},
384 {"repository", REPOSITORY},
385 {"revoked_signers", REVOKED_SIGNERS},
389 const struct keywords *p;
391 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
392 sizeof(keywords[0]), kw_cmp);
400 #define START_EXPAND 1
401 #define DONE_EXPAND 2
403 static int expanding;
411 if (file->ungetpos > 0)
412 c = file->ungetbuf[--file->ungetpos];
414 c = getc(file->stream);
416 if (c == START_EXPAND)
418 else if (c == DONE_EXPAND)
434 yyerror("reached end of file while parsing "
447 yylval.lineno = file->lineno;
460 if (file->ungetpos >= file->ungetsize) {
461 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
463 err(1, "%s", __func__);
465 file->ungetsize *= 2;
467 file->ungetbuf[file->ungetpos++] = c;
475 /* Skip to either EOF or the first real EOL. */
494 if (atoul(n, &ulval) == 0) {
495 if (ulval == 0 || ulval > 65535) {
496 yyerror("illegal port value %lu", ulval);
501 s = getservbyname(n, "tcp");
503 s = getservbyname(n, "udp");
505 yyerror("unknown port %s", n);
513 parseport(char *port, long long *pn)
515 if ((*pn = getservice(port)) == -1) {
534 while (c == ' ' || c == '\t')
535 c = lgetc(0); /* nothing */
537 yylval.lineno = file->lineno;
540 while (c != '\n' && c != EOF)
541 c = lgetc(0); /* nothing */
543 if (c == '$' && !expanding) {
549 if (p + 1 >= buf + sizeof(buf) - 1) {
550 yyerror("string too long");
553 if (isalnum(c) || c == '_') {
563 yyerror("macro '%s' not defined", buf);
566 p = val + strlen(val) - 1;
567 lungetc(DONE_EXPAND);
569 lungetc((unsigned char)*p);
572 lungetc(START_EXPAND);
587 } else if (c == '\\') {
588 next = lgetc(quotec);
591 if (next == quotec || c == ' ' || c == '\t')
593 else if (next == '\n') {
598 } else if (c == quotec) {
601 } else if (c == '\0') {
602 yyerror("syntax error");
605 if (p + 1 >= buf + sizeof(buf) - 1) {
606 yyerror("string too long");
611 yylval.v.string = strdup(buf);
612 if (yylval.v.string == NULL)
613 err(1, "%s", __func__);
617 #define allowed_to_end_number(x) \
618 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
620 if (c == '-' || isdigit(c)) {
623 if ((size_t)(p-buf) >= sizeof(buf)) {
624 yyerror("string too long");
628 } while (c != EOF && isdigit(c));
630 if (p == buf + 1 && buf[0] == '-')
632 if (c == EOF || allowed_to_end_number(c)) {
633 const char *errstr = NULL;
636 yylval.v.number = strtonum(buf, LLONG_MIN,
639 yyerror("\"%s\" invalid number: %s",
647 lungetc((unsigned char)*--p);
648 c = (unsigned char)*--p;
654 #define allowed_in_string(x) \
655 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
656 x != '{' && x != '}' && \
657 x != '!' && x != '=' && x != '#' && \
660 if (isalnum(c) || c == ':' || c == '_') {
663 if ((size_t)(p-buf) >= sizeof(buf)) {
664 yyerror("string too long");
668 } while (c != EOF && (allowed_in_string(c)));
672 if (token == STRING) {
673 yylval.v.string = strdup(buf);
674 if (yylval.v.string == NULL)
675 err(1, "%s", __func__);
680 yylval.lineno = file->lineno;
688 static const struct got_error*
689 newfile(struct file **nfile, const char *filename, int *fd)
691 const struct got_error* error = NULL;
693 (*nfile) = calloc(1, sizeof(struct file));
694 if ((*nfile) == NULL)
695 return got_error_from_errno("calloc");
696 (*nfile)->stream = fdopen(*fd, "r");
697 if ((*nfile)->stream == NULL) {
698 error = got_error_from_errno("fdopen");
702 *fd = -1; /* Stream owns the file descriptor now. */
703 (*nfile)->name = filename;
704 (*nfile)->lineno = 1;
705 (*nfile)->ungetsize = 16;
706 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
707 if ((*nfile)->ungetbuf == NULL) {
708 error = got_error_from_errno("malloc");
709 fclose((*nfile)->stream);
716 static const struct got_error*
717 new_remote(struct gotconfig_remote_repo **remote)
719 const struct got_error *error = NULL;
721 *remote = calloc(1, sizeof(**remote));
723 error = got_error_from_errno("calloc");
727 static const struct got_error*
728 new_fetch_config(struct fetch_config **fetch_config)
730 const struct got_error *error = NULL;
732 *fetch_config = calloc(1, sizeof(**fetch_config));
733 if (*fetch_config == NULL)
734 error = got_error_from_errno("calloc");
738 static const struct got_error*
739 new_send_config(struct send_config **send_config)
741 const struct got_error *error = NULL;
743 *send_config = calloc(1, sizeof(**send_config));
744 if (*send_config == NULL)
745 error = got_error_from_errno("calloc");
750 closefile(struct file *file)
752 fclose(file->stream);
753 free(file->ungetbuf);
757 const struct got_error *
758 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
760 const struct got_error *err = NULL;
761 struct sym *sym, *next;
765 err = newfile(&file, filename, fd);
769 TAILQ_INIT(&gotconfig.remotes);
774 /* Free macros and check which have not been used. */
775 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
779 TAILQ_REMOVE(&symhead, sym, entry);
790 free_fetch_config(struct fetch_config *fetch_config)
792 free(remote->fetch_config->repository);
793 free(remote->fetch_config->server);
794 free(remote->fetch_config->protocol);
795 free(remote->fetch_config);
799 free_send_config(struct send_config *send_config)
801 free(remote->send_config->repository);
802 free(remote->send_config->server);
803 free(remote->send_config->protocol);
804 free(remote->send_config);
808 gotconfig_free(struct gotconfig *conf)
810 struct gotconfig_remote_repo *remote;
813 free(conf->allowed_signers_file);
814 free(conf->revoked_signers_file);
815 while (!TAILQ_EMPTY(&conf->remotes)) {
816 remote = TAILQ_FIRST(&conf->remotes);
817 TAILQ_REMOVE(&conf->remotes, remote, entry);
818 if (remote->fetch_config != NULL)
819 free_fetch_config(remote->fetch_config);
820 if (remote->send_config != NULL)
821 free_send_config(remote->send_config);
823 free(remote->repository);
824 free(remote->server);
825 free(remote->protocol);
831 symset(const char *nam, const char *val, int persist)
835 TAILQ_FOREACH(sym, &symhead, entry) {
836 if (strcmp(nam, sym->nam) == 0)
841 if (sym->persist == 1)
846 TAILQ_REMOVE(&symhead, sym, entry);
850 sym = calloc(1, sizeof(*sym));
854 sym->nam = strdup(nam);
855 if (sym->nam == NULL) {
859 sym->val = strdup(val);
860 if (sym->val == NULL) {
866 sym->persist = persist;
867 TAILQ_INSERT_TAIL(&symhead, sym, entry);
872 cmdline_symset(char *s)
878 val = strrchr(s, '=');
882 len = strlen(s) - strlen(val) + 1;
885 errx(1, "cmdline_symset: malloc");
887 strlcpy(sym, s, len);
889 ret = symset(sym, val + 1, 1);
896 symget(const char *nam)
900 TAILQ_FOREACH(sym, &symhead, entry) {
901 if (strcmp(nam, sym->nam) == 0) {
910 atoul(char *s, u_long *ulvalp)
916 ulval = strtoul(s, &ep, 0);
917 if (s[0] == '\0' || *ep != '\0')
919 if (errno == ERANGE && ulval == ULONG_MAX)