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'
118 | grammar revoked_signers '\n'
121 if (strcasecmp($1, "true") == 0 ||
122 strcasecmp($1, "yes") == 0)
124 else if (strcasecmp($1, "false") == 0 ||
125 strcasecmp($1, "no") == 0)
128 yyerror("invalid boolean value '%s'", $1);
135 numberstring : NUMBER {
137 if (asprintf(&s, "%lld", $1) == -1) {
138 yyerror("string: asprintf");
145 portplain : numberstring {
146 if (parseport($1, &$$) == -1) {
153 branch : /* empty */ { $$ = NULL; }
154 | xbranch { $$ = $1; }
155 | '{' optnl branch_list '}' { $$ = $3; }
158 $$ = calloc(1, sizeof(struct node_branch));
163 $$->branch_name = $1;
167 branch_list : xbranch optnl { $$ = $1; }
168 | branch_list comma xbranch optnl {
174 ref : /* empty */ { $$ = NULL; }
176 | '{' optnl ref_list '}' { $$ = $3; }
179 $$ = calloc(1, sizeof(struct node_ref));
188 ref_list : xref optnl { $$ = $1; }
189 | ref_list comma xref optnl {
195 remoteopts2 : remoteopts2 remoteopts1 nl
198 remoteopts1 : REPOSITORY STRING {
199 remote->repository = $2;
205 remote->protocol = $2;
207 | MIRROR_REFERENCES boolean {
208 remote->mirror_references = $2;
210 | FETCH_ALL_BRANCHES boolean {
211 remote->fetch_all_branches = $2;
220 remote->fetch_ref = $2;
223 static const struct got_error* error;
225 if (remote->fetch_config != NULL) {
226 yyerror("fetch block already exists");
229 error = new_fetch_config(&remote->fetch_config);
231 yyerror("%s", error->msg);
234 } '{' optnl fetchempty '}'
236 static const struct got_error* error;
238 if (remote->send_config != NULL) {
239 yyerror("send block already exists");
242 error = new_send_config(&remote->send_config);
244 yyerror("%s", error->msg);
247 } '{' optnl sendempty '}'
249 fetchempty : /* empty */
252 fetchopts2 : fetchopts2 fetchopts1 nl
255 fetchopts1 : REPOSITORY STRING {
256 remote->fetch_config->repository = $2;
259 remote->fetch_config->server = $2;
262 remote->fetch_config->protocol = $2;
265 remote->fetch_config->port = $2;
268 remote->fetch_config->branch = $2;
271 sendempty : /* empty */
274 sendopts2 : sendopts2 sendopts1 nl
277 sendopts1 : REPOSITORY STRING {
278 remote->send_config->repository = $2;
281 remote->send_config->server = $2;
284 remote->send_config->protocol = $2;
287 remote->send_config->port = $2;
290 remote->send_config->branch = $2;
293 remote : REMOTE STRING {
294 static const struct got_error* error;
296 error = new_remote(&remote);
299 yyerror("%s", error->msg);
303 } '{' optnl remoteopts2 '}' {
304 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
305 gotconfig.nremotes++;
308 author : AUTHOR STRING {
309 gotconfig.author = $2;
312 allowed_signers : ALLOWED_SIGNERS STRING {
313 gotconfig.allowed_signers_file = $2;
316 revoked_signers : REVOKED_SIGNERS STRING {
317 gotconfig.revoked_signers_file = $2;
336 yyerror(const char *fmt, ...)
343 if (vasprintf(&msg, fmt, ap) == -1) {
344 gerror = got_error_from_errno("vasprintf");
348 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
350 gerror = got_error_from_errno("asprintf");
353 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
358 kw_cmp(const void *k, const void *e)
360 return (strcmp(k, ((const struct keywords *)e)->k_name));
366 /* This has to be sorted always. */
367 static const struct keywords keywords[] = {
368 {"allowed_signers", ALLOWED_SIGNERS},
372 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
373 {"fetch_all_branches", FETCH_ALL_BRANCHES},
374 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
375 {"mirror_references", MIRROR_REFERENCES},
377 {"protocol", PROTOCOL},
378 {"reference", REFERENCE},
380 {"repository", REPOSITORY},
381 {"revoked_signers", REVOKED_SIGNERS},
385 const struct keywords *p;
387 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
388 sizeof(keywords[0]), kw_cmp);
396 #define START_EXPAND 1
397 #define DONE_EXPAND 2
399 static int expanding;
407 if (file->ungetpos > 0)
408 c = file->ungetbuf[--file->ungetpos];
410 c = getc(file->stream);
412 if (c == START_EXPAND)
414 else if (c == DONE_EXPAND)
430 yyerror("reached end of file while parsing "
443 yylval.lineno = file->lineno;
456 if (file->ungetpos >= file->ungetsize) {
457 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
459 err(1, "%s", __func__);
461 file->ungetsize *= 2;
463 file->ungetbuf[file->ungetpos++] = c;
471 /* Skip to either EOF or the first real EOL. */
490 if (atoul(n, &ulval) == 0) {
491 if (ulval == 0 || ulval > 65535) {
492 yyerror("illegal port value %lu", ulval);
497 s = getservbyname(n, "tcp");
499 s = getservbyname(n, "udp");
501 yyerror("unknown port %s", n);
509 parseport(char *port, long long *pn)
511 if ((*pn = getservice(port)) == -1) {
530 while (c == ' ' || c == '\t')
531 c = lgetc(0); /* nothing */
533 yylval.lineno = file->lineno;
536 while (c != '\n' && c != EOF)
537 c = lgetc(0); /* nothing */
539 if (c == '$' && !expanding) {
545 if (p + 1 >= buf + sizeof(buf) - 1) {
546 yyerror("string too long");
549 if (isalnum(c) || c == '_') {
559 yyerror("macro '%s' not defined", buf);
562 p = val + strlen(val) - 1;
563 lungetc(DONE_EXPAND);
565 lungetc((unsigned char)*p);
568 lungetc(START_EXPAND);
583 } else if (c == '\\') {
584 next = lgetc(quotec);
587 if (next == quotec || c == ' ' || c == '\t')
589 else if (next == '\n') {
594 } else if (c == quotec) {
597 } else if (c == '\0') {
598 yyerror("syntax error");
601 if (p + 1 >= buf + sizeof(buf) - 1) {
602 yyerror("string too long");
607 yylval.v.string = strdup(buf);
608 if (yylval.v.string == NULL)
609 err(1, "%s", __func__);
613 #define allowed_to_end_number(x) \
614 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
616 if (c == '-' || isdigit(c)) {
619 if ((size_t)(p-buf) >= sizeof(buf)) {
620 yyerror("string too long");
624 } while (c != EOF && isdigit(c));
626 if (p == buf + 1 && buf[0] == '-')
628 if (c == EOF || allowed_to_end_number(c)) {
629 const char *errstr = NULL;
632 yylval.v.number = strtonum(buf, LLONG_MIN,
635 yyerror("\"%s\" invalid number: %s",
643 lungetc((unsigned char)*--p);
644 c = (unsigned char)*--p;
650 #define allowed_in_string(x) \
651 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
652 x != '{' && x != '}' && \
653 x != '!' && x != '=' && x != '#' && \
656 if (isalnum(c) || c == ':' || c == '_') {
659 if ((size_t)(p-buf) >= sizeof(buf)) {
660 yyerror("string too long");
664 } while (c != EOF && (allowed_in_string(c)));
668 if (token == STRING) {
669 yylval.v.string = strdup(buf);
670 if (yylval.v.string == NULL)
671 err(1, "%s", __func__);
676 yylval.lineno = file->lineno;
684 static const struct got_error*
685 newfile(struct file **nfile, const char *filename, int *fd)
687 const struct got_error* error = NULL;
689 (*nfile) = calloc(1, sizeof(struct file));
690 if ((*nfile) == NULL)
691 return got_error_from_errno("calloc");
692 (*nfile)->stream = fdopen(*fd, "r");
693 if ((*nfile)->stream == NULL) {
694 error = got_error_from_errno("fdopen");
698 *fd = -1; /* Stream owns the file descriptor now. */
699 (*nfile)->name = filename;
700 (*nfile)->lineno = 1;
701 (*nfile)->ungetsize = 16;
702 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
703 if ((*nfile)->ungetbuf == NULL) {
704 error = got_error_from_errno("malloc");
705 fclose((*nfile)->stream);
712 static const struct got_error*
713 new_remote(struct gotconfig_remote_repo **remote)
715 const struct got_error *error = NULL;
717 *remote = calloc(1, sizeof(**remote));
719 error = got_error_from_errno("calloc");
723 static const struct got_error*
724 new_fetch_config(struct fetch_config **fetch_config)
726 const struct got_error *error = NULL;
728 *fetch_config = calloc(1, sizeof(**fetch_config));
729 if (*fetch_config == NULL)
730 error = got_error_from_errno("calloc");
734 static const struct got_error*
735 new_send_config(struct send_config **send_config)
737 const struct got_error *error = NULL;
739 *send_config = calloc(1, sizeof(**send_config));
740 if (*send_config == NULL)
741 error = got_error_from_errno("calloc");
746 closefile(struct file *file)
748 fclose(file->stream);
749 free(file->ungetbuf);
753 const struct got_error *
754 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
756 const struct got_error *err = NULL;
757 struct sym *sym, *next;
761 err = newfile(&file, filename, fd);
765 TAILQ_INIT(&gotconfig.remotes);
770 /* Free macros and check which have not been used. */
771 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
775 TAILQ_REMOVE(&symhead, sym, entry);
786 free_fetch_config(struct fetch_config *fetch_config)
788 free(remote->fetch_config->repository);
789 free(remote->fetch_config->server);
790 free(remote->fetch_config->protocol);
791 free(remote->fetch_config);
795 free_send_config(struct send_config *send_config)
797 free(remote->send_config->repository);
798 free(remote->send_config->server);
799 free(remote->send_config->protocol);
800 free(remote->send_config);
804 gotconfig_free(struct gotconfig *conf)
806 struct gotconfig_remote_repo *remote;
809 free(conf->allowed_signers_file);
810 free(conf->revoked_signers_file);
811 while (!TAILQ_EMPTY(&conf->remotes)) {
812 remote = TAILQ_FIRST(&conf->remotes);
813 TAILQ_REMOVE(&conf->remotes, remote, entry);
814 if (remote->fetch_config != NULL)
815 free_fetch_config(remote->fetch_config);
816 if (remote->send_config != NULL)
817 free_send_config(remote->send_config);
819 free(remote->repository);
820 free(remote->server);
821 free(remote->protocol);
827 symset(const char *nam, const char *val, int persist)
831 TAILQ_FOREACH(sym, &symhead, entry) {
832 if (strcmp(nam, sym->nam) == 0)
837 if (sym->persist == 1)
842 TAILQ_REMOVE(&symhead, sym, entry);
846 sym = calloc(1, sizeof(*sym));
850 sym->nam = strdup(nam);
851 if (sym->nam == NULL) {
855 sym->val = strdup(val);
856 if (sym->val == NULL) {
862 sym->persist = persist;
863 TAILQ_INSERT_TAIL(&symhead, sym, entry);
868 cmdline_symset(char *s)
874 val = strrchr(s, '=');
878 len = strlen(s) - strlen(val) + 1;
881 errx(1, "cmdline_symset: malloc");
883 strlcpy(sym, s, len);
885 ret = symset(sym, val + 1, 1);
892 symget(const char *nam)
896 TAILQ_FOREACH(sym, &symhead, entry) {
897 if (strcmp(nam, sym->nam) == 0) {
906 atoul(char *s, u_long *ulvalp)
912 ulval = strtoul(s, &ep, 0);
913 if (s[0] == '\0' || *ep != '\0')
915 if (errno == ERANGE && ulval == ULONG_MAX)