Blob


1 /*
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.
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/types.h>
27 #include <netdb.h>
29 #include <ctype.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
43 static struct file {
44 FILE *stream;
45 const char *name;
46 size_t ungetpos;
47 size_t ungetsize;
48 u_char *ungetbuf;
49 int eof_reached;
50 int lineno;
51 } *file;
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
54 int yyparse(void);
55 int yylex(void);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
60 int lookup(char *);
61 int igetc(void);
62 int lgetc(int);
63 void lungetc(int);
64 int findeol(void);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 struct sym {
69 TAILQ_ENTRY(sym) entry;
70 int used;
71 int persist;
72 char *nam;
73 char *val;
74 };
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 **);
89 typedef struct {
90 union {
91 long long number;
92 char *string;
93 struct node_branch *branch;
94 struct node_ref *ref;
95 } v;
96 int lineno;
97 } YYSTYPE;
99 #if defined(__APPLE__) && !defined(YYSTYPE)
100 #warning "Setting YYSTYPE - is GNU Bison installed?"
101 #define YYSTYPE YYSTYPE
102 #endif
103 %}
105 %token ERROR
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
107 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
108 %token FETCH SEND
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
116 %%
118 grammar : /* empty */
119 | grammar '\n'
120 | grammar author '\n'
121 | grammar remote '\n'
122 | grammar allowed_signers '\n'
123 | grammar revoked_signers '\n'
125 boolean : STRING {
126 if (strcasecmp($1, "true") == 0 ||
127 strcasecmp($1, "yes") == 0)
128 $$ = 1;
129 else if (strcasecmp($1, "false") == 0 ||
130 strcasecmp($1, "no") == 0)
131 $$ = 0;
132 else {
133 yyerror("invalid boolean value '%s'", $1);
134 free($1);
135 YYERROR;
137 free($1);
140 numberstring : NUMBER {
141 char *s;
142 if (asprintf(&s, "%lld", $1) == -1) {
143 yyerror("string: asprintf");
144 YYERROR;
146 $$ = s;
148 | STRING
150 portplain : numberstring {
151 if (parseport($1, &$$) == -1) {
152 free($1);
153 YYERROR;
155 free($1);
158 branch : /* empty */ { $$ = NULL; }
159 | xbranch { $$ = $1; }
160 | '{' optnl branch_list '}' { $$ = $3; }
162 xbranch : STRING {
163 $$ = calloc(1, sizeof(struct node_branch));
164 if ($$ == NULL) {
165 yyerror("calloc");
166 YYERROR;
168 $$->branch_name = $1;
169 $$->tail = $$;
172 branch_list : xbranch optnl { $$ = $1; }
173 | branch_list comma xbranch optnl {
174 $1->tail->next = $3;
175 $1->tail = $3;
176 $$ = $1;
179 ref : /* empty */ { $$ = NULL; }
180 | xref { $$ = $1; }
181 | '{' optnl ref_list '}' { $$ = $3; }
183 xref : STRING {
184 $$ = calloc(1, sizeof(struct node_ref));
185 if ($$ == NULL) {
186 yyerror("calloc");
187 YYERROR;
189 $$->ref_name = $1;
190 $$->tail = $$;
193 ref_list : xref optnl { $$ = $1; }
194 | ref_list comma xref optnl {
195 $1->tail->next = $3;
196 $1->tail = $3;
197 $$ = $1;
200 remoteopts2 : remoteopts2 remoteopts1 nl
201 | remoteopts1 optnl
203 remoteopts1 : REPOSITORY STRING {
204 remote->repository = $2;
206 | SERVER STRING {
207 remote->server = $2;
209 | PROTOCOL STRING {
210 remote->protocol = $2;
212 | MIRROR_REFERENCES boolean {
213 remote->mirror_references = $2;
215 | FETCH_ALL_BRANCHES boolean {
216 remote->fetch_all_branches = $2;
218 | PORT portplain {
219 remote->port = $2;
221 | BRANCH branch {
222 remote->branch = $2;
224 | REFERENCE ref {
225 remote->fetch_ref = $2;
227 | FETCH {
228 static const struct got_error* error;
230 if (remote->fetch_config != NULL) {
231 yyerror("fetch block already exists");
232 YYERROR;
234 error = new_fetch_config(&remote->fetch_config);
235 if (error) {
236 yyerror("%s", error->msg);
237 YYERROR;
239 } '{' optnl fetchempty '}'
240 | SEND {
241 static const struct got_error* error;
243 if (remote->send_config != NULL) {
244 yyerror("send block already exists");
245 YYERROR;
247 error = new_send_config(&remote->send_config);
248 if (error) {
249 yyerror("%s", error->msg);
250 YYERROR;
252 } '{' optnl sendempty '}'
254 fetchempty : /* empty */
255 | fetchopts2
257 fetchopts2 : fetchopts2 fetchopts1 nl
258 | fetchopts1 optnl
260 fetchopts1 : REPOSITORY STRING {
261 remote->fetch_config->repository = $2;
263 | SERVER STRING {
264 remote->fetch_config->server = $2;
266 | PROTOCOL STRING {
267 remote->fetch_config->protocol = $2;
269 | PORT portplain {
270 remote->fetch_config->port = $2;
272 | BRANCH branch {
273 remote->fetch_config->branch = $2;
276 sendempty : /* empty */
277 | sendopts2
279 sendopts2 : sendopts2 sendopts1 nl
280 | sendopts1 optnl
282 sendopts1 : REPOSITORY STRING {
283 remote->send_config->repository = $2;
285 | SERVER STRING {
286 remote->send_config->server = $2;
288 | PROTOCOL STRING {
289 remote->send_config->protocol = $2;
291 | PORT portplain {
292 remote->send_config->port = $2;
294 | BRANCH branch {
295 remote->send_config->branch = $2;
298 remote : REMOTE STRING {
299 static const struct got_error* error;
301 error = new_remote(&remote);
302 if (error) {
303 free($2);
304 yyerror("%s", error->msg);
305 YYERROR;
307 remote->name = $2;
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;
325 optnl : '\n' optnl
326 | /* empty */
328 nl : '\n' optnl
330 comma : ','
331 | /* empty */
333 %%
335 struct keywords {
336 const char *k_name;
337 int k_val;
338 };
340 int
341 yyerror(const char *fmt, ...)
343 va_list ap;
344 char *msg;
345 char *err = NULL;
347 va_start(ap, fmt);
348 if (vasprintf(&msg, fmt, ap) == -1) {
349 gerror = got_error_from_errno("vasprintf");
350 return 0;
352 va_end(ap);
353 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
354 msg) == -1) {
355 gerror = got_error_from_errno("asprintf");
356 return(0);
358 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
359 free(msg);
360 return(0);
362 int
363 kw_cmp(const void *k, const void *e)
365 return (strcmp(k, ((const struct keywords *)e)->k_name));
368 int
369 lookup(char *s)
371 /* This has to be sorted always. */
372 static const struct keywords keywords[] = {
373 {"allowed_signers", ALLOWED_SIGNERS},
374 {"author", AUTHOR},
375 {"branch", BRANCH},
376 {"fetch", FETCH},
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},
381 {"port", PORT},
382 {"protocol", PROTOCOL},
383 {"reference", REFERENCE},
384 {"remote", REMOTE},
385 {"repository", REPOSITORY},
386 {"revoked_signers", REVOKED_SIGNERS},
387 {"send", SEND},
388 {"server", SERVER},
389 };
390 const struct keywords *p;
392 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
393 sizeof(keywords[0]), kw_cmp);
395 if (p)
396 return (p->k_val);
397 else
398 return (STRING);
401 #define START_EXPAND 1
402 #define DONE_EXPAND 2
404 static int expanding;
406 int
407 igetc(void)
409 int c;
411 while (1) {
412 if (file->ungetpos > 0)
413 c = file->ungetbuf[--file->ungetpos];
414 else
415 c = getc(file->stream);
417 if (c == START_EXPAND)
418 expanding = 1;
419 else if (c == DONE_EXPAND)
420 expanding = 0;
421 else
422 break;
424 return (c);
427 int
428 lgetc(int quotec)
430 int c, next;
432 if (quotec) {
433 c = igetc();
434 if (c == EOF) {
435 yyerror("reached end of file while parsing "
436 "quoted string");
438 return (c);
441 c = igetc();
442 while (c == '\\') {
443 next = igetc();
444 if (next != '\n') {
445 c = next;
446 break;
448 yylval.lineno = file->lineno;
449 file->lineno++;
452 return (c);
455 void
456 lungetc(int c)
458 if (c == EOF)
459 return;
461 if (file->ungetpos >= file->ungetsize) {
462 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
463 if (p == NULL)
464 err(1, "%s", __func__);
465 file->ungetbuf = p;
466 file->ungetsize *= 2;
468 file->ungetbuf[file->ungetpos++] = c;
471 int
472 findeol(void)
474 int c;
476 /* Skip to either EOF or the first real EOL. */
477 while (1) {
478 c = lgetc(0);
479 if (c == '\n') {
480 file->lineno++;
481 break;
483 if (c == EOF)
484 break;
486 return (ERROR);
489 static long long
490 getservice(char *n)
492 struct servent *s;
493 u_long ulval;
495 if (atoul(n, &ulval) == 0) {
496 if (ulval == 0 || ulval > 65535) {
497 yyerror("illegal port value %lu", ulval);
498 return (-1);
500 return ulval;
501 } else {
502 s = getservbyname(n, "tcp");
503 if (s == NULL)
504 s = getservbyname(n, "udp");
505 if (s == NULL) {
506 yyerror("unknown port %s", n);
507 return (-1);
509 return (s->s_port);
513 static int
514 parseport(char *port, long long *pn)
516 if ((*pn = getservice(port)) == -1) {
517 *pn = 0LL;
518 return (-1);
520 return (0);
524 int
525 yylex(void)
527 char buf[8096];
528 char *p, *val;
529 int quotec, next, c;
530 int token;
532 top:
533 p = buf;
534 c = lgetc(0);
535 while (c == ' ' || c == '\t')
536 c = lgetc(0); /* nothing */
538 yylval.lineno = file->lineno;
539 if (c == '#') {
540 c = lgetc(0);
541 while (c != '\n' && c != EOF)
542 c = lgetc(0); /* nothing */
544 if (c == '$' && !expanding) {
545 while (1) {
546 c = lgetc(0);
547 if (c == EOF)
548 return (0);
550 if (p + 1 >= buf + sizeof(buf) - 1) {
551 yyerror("string too long");
552 return (findeol());
554 if (isalnum(c) || c == '_') {
555 *p++ = c;
556 continue;
558 *p = '\0';
559 lungetc(c);
560 break;
562 val = symget(buf);
563 if (val == NULL) {
564 yyerror("macro '%s' not defined", buf);
565 return (findeol());
567 p = val + strlen(val) - 1;
568 lungetc(DONE_EXPAND);
569 while (p >= val) {
570 lungetc((unsigned char)*p);
571 p--;
573 lungetc(START_EXPAND);
574 goto top;
577 switch (c) {
578 case '\'':
579 case '"':
580 quotec = c;
581 while (1) {
582 c = lgetc(quotec);
583 if (c == EOF)
584 return (0);
585 if (c == '\n') {
586 file->lineno++;
587 continue;
588 } else if (c == '\\') {
589 next = lgetc(quotec);
590 if (next == EOF)
591 return (0);
592 if (next == quotec || c == ' ' || c == '\t')
593 c = next;
594 else if (next == '\n') {
595 file->lineno++;
596 continue;
597 } else
598 lungetc(next);
599 } else if (c == quotec) {
600 *p = '\0';
601 break;
602 } else if (c == '\0') {
603 yyerror("syntax error");
604 return (findeol());
606 if (p + 1 >= buf + sizeof(buf) - 1) {
607 yyerror("string too long");
608 return (findeol());
610 *p++ = c;
612 yylval.v.string = strdup(buf);
613 if (yylval.v.string == NULL)
614 err(1, "%s", __func__);
615 return (STRING);
618 #define allowed_to_end_number(x) \
619 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
621 if (c == '-' || isdigit(c)) {
622 do {
623 *p++ = c;
624 if ((size_t)(p-buf) >= sizeof(buf)) {
625 yyerror("string too long");
626 return (findeol());
628 c = lgetc(0);
629 } while (c != EOF && isdigit(c));
630 lungetc(c);
631 if (p == buf + 1 && buf[0] == '-')
632 goto nodigits;
633 if (c == EOF || allowed_to_end_number(c)) {
634 const char *errstr = NULL;
636 *p = '\0';
637 yylval.v.number = strtonum(buf, LLONG_MIN,
638 LLONG_MAX, &errstr);
639 if (errstr) {
640 yyerror("\"%s\" invalid number: %s",
641 buf, errstr);
642 return (findeol());
644 return (NUMBER);
645 } else {
646 nodigits:
647 while (p > buf + 1)
648 lungetc((unsigned char)*--p);
649 c = (unsigned char)*--p;
650 if (c == '-')
651 return (c);
655 #define allowed_in_string(x) \
656 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
657 x != '{' && x != '}' && \
658 x != '!' && x != '=' && x != '#' && \
659 x != ','))
661 if (isalnum(c) || c == ':' || c == '_') {
662 do {
663 *p++ = c;
664 if ((size_t)(p-buf) >= sizeof(buf)) {
665 yyerror("string too long");
666 return (findeol());
668 c = lgetc(0);
669 } while (c != EOF && (allowed_in_string(c)));
670 lungetc(c);
671 *p = '\0';
672 token = lookup(buf);
673 if (token == STRING) {
674 yylval.v.string = strdup(buf);
675 if (yylval.v.string == NULL)
676 err(1, "%s", __func__);
678 return (token);
680 if (c == '\n') {
681 yylval.lineno = file->lineno;
682 file->lineno++;
684 if (c == EOF)
685 return (0);
686 return (c);
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");
700 free((*nfile));
701 return error;
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);
711 free((*nfile));
712 return error;
714 return NULL;
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));
723 if (*remote == NULL)
724 error = got_error_from_errno("calloc");
725 return error;
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");
736 return error;
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");
747 return error;
750 static void
751 closefile(struct file *file)
753 fclose(file->stream);
754 free(file->ungetbuf);
755 free(file);
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;
764 *conf = NULL;
766 err = newfile(&file, filename, fd);
767 if (err)
768 return err;
770 TAILQ_INIT(&gotconfig.remotes);
772 yyparse();
773 closefile(file);
775 /* Free macros and check which have not been used. */
776 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
777 if (!sym->persist) {
778 free(sym->nam);
779 free(sym->val);
780 TAILQ_REMOVE(&symhead, sym, entry);
781 free(sym);
785 if (gerror == NULL)
786 *conf = &gotconfig;
787 return gerror;
790 static void
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);
799 static void
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);
808 void
809 gotconfig_free(struct gotconfig *conf)
811 struct gotconfig_remote_repo *remote;
813 free(conf->author);
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);
823 free(remote->name);
824 free(remote->repository);
825 free(remote->server);
826 free(remote->protocol);
827 free(remote);
831 int
832 symset(const char *nam, const char *val, int persist)
834 struct sym *sym;
836 TAILQ_FOREACH(sym, &symhead, entry) {
837 if (strcmp(nam, sym->nam) == 0)
838 break;
841 if (sym != NULL) {
842 if (sym->persist == 1)
843 return (0);
844 else {
845 free(sym->nam);
846 free(sym->val);
847 TAILQ_REMOVE(&symhead, sym, entry);
848 free(sym);
851 sym = calloc(1, sizeof(*sym));
852 if (sym == NULL)
853 return (-1);
855 sym->nam = strdup(nam);
856 if (sym->nam == NULL) {
857 free(sym);
858 return (-1);
860 sym->val = strdup(val);
861 if (sym->val == NULL) {
862 free(sym->nam);
863 free(sym);
864 return (-1);
866 sym->used = 0;
867 sym->persist = persist;
868 TAILQ_INSERT_TAIL(&symhead, sym, entry);
869 return (0);
872 int
873 cmdline_symset(char *s)
875 char *sym, *val;
876 int ret;
877 size_t len;
879 val = strrchr(s, '=');
880 if (val == NULL)
881 return (-1);
883 len = strlen(s) - strlen(val) + 1;
884 sym = malloc(len);
885 if (sym == NULL)
886 errx(1, "cmdline_symset: malloc");
888 strlcpy(sym, s, len);
890 ret = symset(sym, val + 1, 1);
891 free(sym);
893 return (ret);
896 char *
897 symget(const char *nam)
899 struct sym *sym;
901 TAILQ_FOREACH(sym, &symhead, entry) {
902 if (strcmp(nam, sym->nam) == 0) {
903 sym->used = 1;
904 return (sym->val);
907 return (NULL);
910 static int
911 atoul(char *s, u_long *ulvalp)
913 u_long ulval;
914 char *ep;
916 errno = 0;
917 ulval = strtoul(s, &ep, 0);
918 if (s[0] == '\0' || *ep != '\0')
919 return (-1);
920 if (errno == ERANGE && ulval == ULONG_MAX)
921 return (-1);
922 *ulvalp = ulval;
923 return (0);