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'
123 | grammar revoked_signers '\n'
126 if (strcasecmp($1, "true") == 0 ||
127 strcasecmp($1, "yes") == 0)
129 else if (strcasecmp($1, "false") == 0 ||
130 strcasecmp($1, "no") == 0)
133 yyerror("invalid boolean value '%s'", $1);
140 numberstring : NUMBER {
142 if (asprintf(&s, "%lld", $1) == -1) {
143 yyerror("string: asprintf");
150 portplain : numberstring {
151 if (parseport($1, &$$) == -1) {
158 branch : /* empty */ { $$ = NULL; }
159 | xbranch { $$ = $1; }
160 | '{' optnl branch_list '}' { $$ = $3; }
163 $$ = calloc(1, sizeof(struct node_branch));
168 $$->branch_name = $1;
172 branch_list : xbranch optnl { $$ = $1; }
173 | branch_list comma xbranch optnl {
179 ref : /* empty */ { $$ = NULL; }
181 | '{' optnl ref_list '}' { $$ = $3; }
184 $$ = calloc(1, sizeof(struct node_ref));
193 ref_list : xref optnl { $$ = $1; }
194 | ref_list comma xref optnl {
200 remoteopts2 : remoteopts2 remoteopts1 nl
203 remoteopts1 : REPOSITORY STRING {
204 remote->repository = $2;
210 remote->protocol = $2;
212 | MIRROR_REFERENCES boolean {
213 remote->mirror_references = $2;
215 | FETCH_ALL_BRANCHES boolean {
216 remote->fetch_all_branches = $2;
225 remote->fetch_ref = $2;
228 static const struct got_error* error;
230 if (remote->fetch_config != NULL) {
231 yyerror("fetch block already exists");
234 error = new_fetch_config(&remote->fetch_config);
236 yyerror("%s", error->msg);
239 } '{' optnl fetchempty '}'
241 static const struct got_error* error;
243 if (remote->send_config != NULL) {
244 yyerror("send block already exists");
247 error = new_send_config(&remote->send_config);
249 yyerror("%s", error->msg);
252 } '{' optnl sendempty '}'
254 fetchempty : /* empty */
257 fetchopts2 : fetchopts2 fetchopts1 nl
260 fetchopts1 : REPOSITORY STRING {
261 remote->fetch_config->repository = $2;
264 remote->fetch_config->server = $2;
267 remote->fetch_config->protocol = $2;
270 remote->fetch_config->port = $2;
273 remote->fetch_config->branch = $2;
276 sendempty : /* empty */
279 sendopts2 : sendopts2 sendopts1 nl
282 sendopts1 : REPOSITORY STRING {
283 remote->send_config->repository = $2;
286 remote->send_config->server = $2;
289 remote->send_config->protocol = $2;
292 remote->send_config->port = $2;
295 remote->send_config->branch = $2;
298 remote : REMOTE STRING {
299 static const struct got_error* error;
301 error = new_remote(&remote);
304 yyerror("%s", error->msg);
308 } '{' optnl remoteopts2 '}' {
309 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
310 gotconfig.nremotes++;
313 author : AUTHOR STRING {
314 gotconfig.author = $2;
317 allowed_signers : ALLOWED_SIGNERS STRING {
318 gotconfig.allowed_signers_file = $2;
321 revoked_signers : REVOKED_SIGNERS STRING {
322 gotconfig.revoked_signers_file = $2;
341 yyerror(const char *fmt, ...)
348 if (vasprintf(&msg, fmt, ap) == -1) {
349 gerror = got_error_from_errno("vasprintf");
353 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
355 gerror = got_error_from_errno("asprintf");
358 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
363 kw_cmp(const void *k, const void *e)
365 return (strcmp(k, ((const struct keywords *)e)->k_name));
371 /* This has to be sorted always. */
372 static const struct keywords keywords[] = {
373 {"allowed_signers", ALLOWED_SIGNERS},
377 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
378 {"fetch_all_branches", FETCH_ALL_BRANCHES},
379 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
380 {"mirror_references", MIRROR_REFERENCES},
382 {"protocol", PROTOCOL},
383 {"reference", REFERENCE},
385 {"repository", REPOSITORY},
386 {"revoked_signers", REVOKED_SIGNERS},
390 const struct keywords *p;
392 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
393 sizeof(keywords[0]), kw_cmp);
401 #define START_EXPAND 1
402 #define DONE_EXPAND 2
404 static int expanding;
412 if (file->ungetpos > 0)
413 c = file->ungetbuf[--file->ungetpos];
415 c = getc(file->stream);
417 if (c == START_EXPAND)
419 else if (c == DONE_EXPAND)
435 yyerror("reached end of file while parsing "
448 yylval.lineno = file->lineno;
461 if (file->ungetpos >= file->ungetsize) {
462 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
464 err(1, "%s", __func__);
466 file->ungetsize *= 2;
468 file->ungetbuf[file->ungetpos++] = c;
476 /* Skip to either EOF or the first real EOL. */
495 if (atoul(n, &ulval) == 0) {
496 if (ulval == 0 || ulval > 65535) {
497 yyerror("illegal port value %lu", ulval);
502 s = getservbyname(n, "tcp");
504 s = getservbyname(n, "udp");
506 yyerror("unknown port %s", n);
514 parseport(char *port, long long *pn)
516 if ((*pn = getservice(port)) == -1) {
535 while (c == ' ' || c == '\t')
536 c = lgetc(0); /* nothing */
538 yylval.lineno = file->lineno;
541 while (c != '\n' && c != EOF)
542 c = lgetc(0); /* nothing */
544 if (c == '$' && !expanding) {
550 if (p + 1 >= buf + sizeof(buf) - 1) {
551 yyerror("string too long");
554 if (isalnum(c) || c == '_') {
564 yyerror("macro '%s' not defined", buf);
567 p = val + strlen(val) - 1;
568 lungetc(DONE_EXPAND);
570 lungetc((unsigned char)*p);
573 lungetc(START_EXPAND);
588 } else if (c == '\\') {
589 next = lgetc(quotec);
592 if (next == quotec || c == ' ' || c == '\t')
594 else if (next == '\n') {
599 } else if (c == quotec) {
602 } else if (c == '\0') {
603 yyerror("syntax error");
606 if (p + 1 >= buf + sizeof(buf) - 1) {
607 yyerror("string too long");
612 yylval.v.string = strdup(buf);
613 if (yylval.v.string == NULL)
614 err(1, "%s", __func__);
618 #define allowed_to_end_number(x) \
619 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
621 if (c == '-' || isdigit(c)) {
624 if ((size_t)(p-buf) >= sizeof(buf)) {
625 yyerror("string too long");
629 } while (c != EOF && isdigit(c));
631 if (p == buf + 1 && buf[0] == '-')
633 if (c == EOF || allowed_to_end_number(c)) {
634 const char *errstr = NULL;
637 yylval.v.number = strtonum(buf, LLONG_MIN,
640 yyerror("\"%s\" invalid number: %s",
648 lungetc((unsigned char)*--p);
649 c = (unsigned char)*--p;
655 #define allowed_in_string(x) \
656 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
657 x != '{' && x != '}' && \
658 x != '!' && x != '=' && x != '#' && \
661 if (isalnum(c) || c == ':' || c == '_') {
664 if ((size_t)(p-buf) >= sizeof(buf)) {
665 yyerror("string too long");
669 } while (c != EOF && (allowed_in_string(c)));
673 if (token == STRING) {
674 yylval.v.string = strdup(buf);
675 if (yylval.v.string == NULL)
676 err(1, "%s", __func__);
681 yylval.lineno = file->lineno;
689 static const struct got_error*
690 newfile(struct file **nfile, const char *filename, int *fd)
692 const struct got_error* error = NULL;
694 (*nfile) = calloc(1, sizeof(struct file));
695 if ((*nfile) == NULL)
696 return got_error_from_errno("calloc");
697 (*nfile)->stream = fdopen(*fd, "r");
698 if ((*nfile)->stream == NULL) {
699 error = got_error_from_errno("fdopen");
703 *fd = -1; /* Stream owns the file descriptor now. */
704 (*nfile)->name = filename;
705 (*nfile)->lineno = 1;
706 (*nfile)->ungetsize = 16;
707 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
708 if ((*nfile)->ungetbuf == NULL) {
709 error = got_error_from_errno("malloc");
710 fclose((*nfile)->stream);
717 static const struct got_error*
718 new_remote(struct gotconfig_remote_repo **remote)
720 const struct got_error *error = NULL;
722 *remote = calloc(1, sizeof(**remote));
724 error = got_error_from_errno("calloc");
728 static const struct got_error*
729 new_fetch_config(struct fetch_config **fetch_config)
731 const struct got_error *error = NULL;
733 *fetch_config = calloc(1, sizeof(**fetch_config));
734 if (*fetch_config == NULL)
735 error = got_error_from_errno("calloc");
739 static const struct got_error*
740 new_send_config(struct send_config **send_config)
742 const struct got_error *error = NULL;
744 *send_config = calloc(1, sizeof(**send_config));
745 if (*send_config == NULL)
746 error = got_error_from_errno("calloc");
751 closefile(struct file *file)
753 fclose(file->stream);
754 free(file->ungetbuf);
758 const struct got_error *
759 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
761 const struct got_error *err = NULL;
762 struct sym *sym, *next;
766 err = newfile(&file, filename, fd);
770 TAILQ_INIT(&gotconfig.remotes);
775 /* Free macros and check which have not been used. */
776 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
780 TAILQ_REMOVE(&symhead, sym, entry);
791 free_fetch_config(struct fetch_config *fetch_config)
793 free(remote->fetch_config->repository);
794 free(remote->fetch_config->server);
795 free(remote->fetch_config->protocol);
796 free(remote->fetch_config);
800 free_send_config(struct send_config *send_config)
802 free(remote->send_config->repository);
803 free(remote->send_config->server);
804 free(remote->send_config->protocol);
805 free(remote->send_config);
809 gotconfig_free(struct gotconfig *conf)
811 struct gotconfig_remote_repo *remote;
814 free(conf->allowed_signers_file);
815 free(conf->revoked_signers_file);
816 while (!TAILQ_EMPTY(&conf->remotes)) {
817 remote = TAILQ_FIRST(&conf->remotes);
818 TAILQ_REMOVE(&conf->remotes, remote, entry);
819 if (remote->fetch_config != NULL)
820 free_fetch_config(remote->fetch_config);
821 if (remote->send_config != NULL)
822 free_send_config(remote->send_config);
824 free(remote->repository);
825 free(remote->server);
826 free(remote->protocol);
832 symset(const char *nam, const char *val, int persist)
836 TAILQ_FOREACH(sym, &symhead, entry) {
837 if (strcmp(nam, sym->nam) == 0)
842 if (sym->persist == 1)
847 TAILQ_REMOVE(&symhead, sym, entry);
851 sym = calloc(1, sizeof(*sym));
855 sym->nam = strdup(nam);
856 if (sym->nam == NULL) {
860 sym->val = strdup(val);
861 if (sym->val == NULL) {
867 sym->persist = persist;
868 TAILQ_INSERT_TAIL(&symhead, sym, entry);
873 cmdline_symset(char *s)
879 val = strrchr(s, '=');
883 len = strlen(s) - strlen(val) + 1;
886 errx(1, "cmdline_symset: malloc");
888 strlcpy(sym, s, len);
890 ret = symset(sym, val + 1, 1);
897 symget(const char *nam)
901 TAILQ_FOREACH(sym, &symhead, entry) {
902 if (strcmp(nam, sym->nam) == 0) {
911 atoul(char *s, u_long *ulvalp)
917 ulval = strtoul(s, &ep, 0);
918 if (s[0] == '\0' || *ep != '\0')
920 if (errno == ERANGE && ulval == ULONG_MAX)