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>
26 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "gotconfig.h"
51 static const struct got_error* newfile(struct file**, const char *, int *);
52 static void closefile(struct file *);
55 int yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
64 static int parseport(char *, long long *);
66 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 TAILQ_ENTRY(sym) entry;
75 int symset(const char *, const char *, int);
76 int cmdline_symset(char *);
77 char *symget(const char *);
79 static int atoul(char *, u_long *);
81 static const struct got_error* gerror;
82 static struct gotconfig_remote_repo *remote;
83 static struct gotconfig gotconfig;
84 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 static const struct got_error* new_fetch_config(struct fetch_config **);
86 static const struct got_error* new_send_config(struct send_config **);
92 struct node_branch *branch;
101 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
104 %token <v.string> STRING
105 %token <v.number> NUMBER
106 %type <v.number> boolean portplain
107 %type <v.string> numberstring
108 %type <v.branch> branch xbranch branch_list
109 %type <v.ref> ref xref ref_list
113 grammar : /* empty */
115 | grammar author '\n'
116 | grammar remote '\n'
117 | grammar allowed_signers '\n'
120 if (strcasecmp($1, "true") == 0 ||
121 strcasecmp($1, "yes") == 0)
123 else if (strcasecmp($1, "false") == 0 ||
124 strcasecmp($1, "no") == 0)
127 yyerror("invalid boolean value '%s'", $1);
134 numberstring : NUMBER {
136 if (asprintf(&s, "%lld", $1) == -1) {
137 yyerror("string: asprintf");
144 portplain : numberstring {
145 if (parseport($1, &$$) == -1) {
152 branch : /* empty */ { $$ = NULL; }
153 | xbranch { $$ = $1; }
154 | '{' optnl branch_list '}' { $$ = $3; }
157 $$ = calloc(1, sizeof(struct node_branch));
162 $$->branch_name = $1;
166 branch_list : xbranch optnl { $$ = $1; }
167 | branch_list comma xbranch optnl {
173 ref : /* empty */ { $$ = NULL; }
175 | '{' optnl ref_list '}' { $$ = $3; }
178 $$ = calloc(1, sizeof(struct node_ref));
187 ref_list : xref optnl { $$ = $1; }
188 | ref_list comma xref optnl {
194 remoteopts2 : remoteopts2 remoteopts1 nl
197 remoteopts1 : REPOSITORY STRING {
198 remote->repository = $2;
204 remote->protocol = $2;
206 | MIRROR_REFERENCES boolean {
207 remote->mirror_references = $2;
209 | FETCH_ALL_BRANCHES boolean {
210 remote->fetch_all_branches = $2;
219 remote->fetch_ref = $2;
222 static const struct got_error* error;
224 if (remote->fetch_config != NULL) {
225 yyerror("fetch block already exists");
228 error = new_fetch_config(&remote->fetch_config);
230 yyerror("%s", error->msg);
233 } '{' optnl fetchempty '}'
235 static const struct got_error* error;
237 if (remote->send_config != NULL) {
238 yyerror("send block already exists");
241 error = new_send_config(&remote->send_config);
243 yyerror("%s", error->msg);
246 } '{' optnl sendempty '}'
248 fetchempty : /* empty */
251 fetchopts2 : fetchopts2 fetchopts1 nl
254 fetchopts1 : REPOSITORY STRING {
255 remote->fetch_config->repository = $2;
258 remote->fetch_config->server = $2;
261 remote->fetch_config->protocol = $2;
264 remote->fetch_config->port = $2;
267 remote->fetch_config->branch = $2;
270 sendempty : /* empty */
273 sendopts2 : sendopts2 sendopts1 nl
276 sendopts1 : REPOSITORY STRING {
277 remote->send_config->repository = $2;
280 remote->send_config->server = $2;
283 remote->send_config->protocol = $2;
286 remote->send_config->port = $2;
289 remote->send_config->branch = $2;
292 remote : REMOTE STRING {
293 static const struct got_error* error;
295 error = new_remote(&remote);
298 yyerror("%s", error->msg);
302 } '{' optnl remoteopts2 '}' {
303 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
304 gotconfig.nremotes++;
307 author : AUTHOR STRING {
308 gotconfig.author = $2;
311 allowed_signers : ALLOWED_SIGNERS STRING {
312 gotconfig.allowed_signers_file = $2;
315 revoked_signers : REVOKED_SIGNERS STRING {
316 gotconfig.revoked_signers_file = $2;
335 yyerror(const char *fmt, ...)
342 if (vasprintf(&msg, fmt, ap) == -1) {
343 gerror = got_error_from_errno("vasprintf");
347 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
349 gerror = got_error_from_errno("asprintf");
352 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
357 kw_cmp(const void *k, const void *e)
359 return (strcmp(k, ((const struct keywords *)e)->k_name));
365 /* This has to be sorted always. */
366 static const struct keywords keywords[] = {
367 {"allowed_signers", ALLOWED_SIGNERS},
371 {"fetch-all-branches", FETCH_ALL_BRANCHES},
372 {"mirror-references", MIRROR_REFERENCES},
374 {"protocol", PROTOCOL},
375 {"reference", REFERENCE},
377 {"repository", REPOSITORY},
378 {"revoked_signers", REVOKED_SIGNERS},
382 const struct keywords *p;
384 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
385 sizeof(keywords[0]), kw_cmp);
393 #define START_EXPAND 1
394 #define DONE_EXPAND 2
396 static int expanding;
404 if (file->ungetpos > 0)
405 c = file->ungetbuf[--file->ungetpos];
407 c = getc(file->stream);
409 if (c == START_EXPAND)
411 else if (c == DONE_EXPAND)
427 yyerror("reached end of file while parsing "
440 yylval.lineno = file->lineno;
453 if (file->ungetpos >= file->ungetsize) {
454 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
456 err(1, "%s", __func__);
458 file->ungetsize *= 2;
460 file->ungetbuf[file->ungetpos++] = c;
468 /* Skip to either EOF or the first real EOL. */
487 if (atoul(n, &ulval) == 0) {
488 if (ulval == 0 || ulval > 65535) {
489 yyerror("illegal port value %lu", ulval);
494 s = getservbyname(n, "tcp");
496 s = getservbyname(n, "udp");
498 yyerror("unknown port %s", n);
506 parseport(char *port, long long *pn)
508 if ((*pn = getservice(port)) == -1) {
527 while (c == ' ' || c == '\t')
528 c = lgetc(0); /* nothing */
530 yylval.lineno = file->lineno;
533 while (c != '\n' && c != EOF)
534 c = lgetc(0); /* nothing */
536 if (c == '$' && !expanding) {
542 if (p + 1 >= buf + sizeof(buf) - 1) {
543 yyerror("string too long");
546 if (isalnum(c) || c == '_') {
556 yyerror("macro '%s' not defined", buf);
559 p = val + strlen(val) - 1;
560 lungetc(DONE_EXPAND);
562 lungetc((unsigned char)*p);
565 lungetc(START_EXPAND);
580 } else if (c == '\\') {
581 next = lgetc(quotec);
584 if (next == quotec || c == ' ' || c == '\t')
586 else if (next == '\n') {
591 } else if (c == quotec) {
594 } else if (c == '\0') {
595 yyerror("syntax error");
598 if (p + 1 >= buf + sizeof(buf) - 1) {
599 yyerror("string too long");
604 yylval.v.string = strdup(buf);
605 if (yylval.v.string == NULL)
606 err(1, "%s", __func__);
610 #define allowed_to_end_number(x) \
611 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
613 if (c == '-' || isdigit(c)) {
616 if ((size_t)(p-buf) >= sizeof(buf)) {
617 yyerror("string too long");
621 } while (c != EOF && isdigit(c));
623 if (p == buf + 1 && buf[0] == '-')
625 if (c == EOF || allowed_to_end_number(c)) {
626 const char *errstr = NULL;
629 yylval.v.number = strtonum(buf, LLONG_MIN,
632 yyerror("\"%s\" invalid number: %s",
640 lungetc((unsigned char)*--p);
641 c = (unsigned char)*--p;
647 #define allowed_in_string(x) \
648 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
649 x != '{' && x != '}' && \
650 x != '!' && x != '=' && x != '#' && \
653 if (isalnum(c) || c == ':' || c == '_') {
656 if ((size_t)(p-buf) >= sizeof(buf)) {
657 yyerror("string too long");
661 } while (c != EOF && (allowed_in_string(c)));
665 if (token == STRING) {
666 yylval.v.string = strdup(buf);
667 if (yylval.v.string == NULL)
668 err(1, "%s", __func__);
673 yylval.lineno = file->lineno;
681 static const struct got_error*
682 newfile(struct file **nfile, const char *filename, int *fd)
684 const struct got_error* error = NULL;
686 (*nfile) = calloc(1, sizeof(struct file));
687 if ((*nfile) == NULL)
688 return got_error_from_errno("calloc");
689 (*nfile)->stream = fdopen(*fd, "r");
690 if ((*nfile)->stream == NULL) {
691 error = got_error_from_errno("fdopen");
695 *fd = -1; /* Stream owns the file descriptor now. */
696 (*nfile)->name = filename;
697 (*nfile)->lineno = 1;
698 (*nfile)->ungetsize = 16;
699 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
700 if ((*nfile)->ungetbuf == NULL) {
701 error = got_error_from_errno("malloc");
702 fclose((*nfile)->stream);
709 static const struct got_error*
710 new_remote(struct gotconfig_remote_repo **remote)
712 const struct got_error *error = NULL;
714 *remote = calloc(1, sizeof(**remote));
716 error = got_error_from_errno("calloc");
720 static const struct got_error*
721 new_fetch_config(struct fetch_config **fetch_config)
723 const struct got_error *error = NULL;
725 *fetch_config = calloc(1, sizeof(**fetch_config));
726 if (*fetch_config == NULL)
727 error = got_error_from_errno("calloc");
731 static const struct got_error*
732 new_send_config(struct send_config **send_config)
734 const struct got_error *error = NULL;
736 *send_config = calloc(1, sizeof(**send_config));
737 if (*send_config == NULL)
738 error = got_error_from_errno("calloc");
743 closefile(struct file *file)
745 fclose(file->stream);
746 free(file->ungetbuf);
750 const struct got_error *
751 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
753 const struct got_error *err = NULL;
754 struct sym *sym, *next;
758 err = newfile(&file, filename, fd);
762 TAILQ_INIT(&gotconfig.remotes);
767 /* Free macros and check which have not been used. */
768 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
772 TAILQ_REMOVE(&symhead, sym, entry);
783 free_fetch_config(struct fetch_config *fetch_config)
785 free(remote->fetch_config->repository);
786 free(remote->fetch_config->server);
787 free(remote->fetch_config->protocol);
788 free(remote->fetch_config);
792 free_send_config(struct send_config *send_config)
794 free(remote->send_config->repository);
795 free(remote->send_config->server);
796 free(remote->send_config->protocol);
797 free(remote->send_config);
801 gotconfig_free(struct gotconfig *conf)
803 struct gotconfig_remote_repo *remote;
806 free(conf->allowed_signers_file);
807 free(conf->revoked_signers_file);
808 while (!TAILQ_EMPTY(&conf->remotes)) {
809 remote = TAILQ_FIRST(&conf->remotes);
810 TAILQ_REMOVE(&conf->remotes, remote, entry);
811 if (remote->fetch_config != NULL)
812 free_fetch_config(remote->fetch_config);
813 if (remote->send_config != NULL)
814 free_send_config(remote->send_config);
816 free(remote->repository);
817 free(remote->server);
818 free(remote->protocol);
824 symset(const char *nam, const char *val, int persist)
828 TAILQ_FOREACH(sym, &symhead, entry) {
829 if (strcmp(nam, sym->nam) == 0)
834 if (sym->persist == 1)
839 TAILQ_REMOVE(&symhead, sym, entry);
843 sym = calloc(1, sizeof(*sym));
847 sym->nam = strdup(nam);
848 if (sym->nam == NULL) {
852 sym->val = strdup(val);
853 if (sym->val == NULL) {
859 sym->persist = persist;
860 TAILQ_INSERT_TAIL(&symhead, sym, entry);
865 cmdline_symset(char *s)
871 val = strrchr(s, '=');
875 len = strlen(s) - strlen(val) + 1;
878 errx(1, "cmdline_symset: malloc");
880 strlcpy(sym, s, len);
882 ret = symset(sym, val + 1, 1);
889 symget(const char *nam)
893 TAILQ_FOREACH(sym, &symhead, entry) {
894 if (strcmp(nam, sym->nam) == 0) {
903 atoul(char *s, u_long *ulvalp)
909 ulval = strtoul(s, &ep, 0);
910 if (s[0] == '\0' || *ep != '\0')
912 if (errno == ERANGE && ulval == ULONG_MAX)