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>
26 #include <sys/queue.h>
28 #include <netdb.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "got_error.h"
40 #include "gotconfig.h"
42 static struct file {
43 FILE *stream;
44 const char *name;
45 size_t ungetpos;
46 size_t ungetsize;
47 u_char *ungetbuf;
48 int eof_reached;
49 int lineno;
50 } *file;
51 static const struct got_error* newfile(struct file**, const char *, int *);
52 static void closefile(struct file *);
53 int yyparse(void);
54 int yylex(void);
55 int yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
64 static int parseport(char *, long long *);
66 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
67 struct sym {
68 TAILQ_ENTRY(sym) entry;
69 int used;
70 int persist;
71 char *nam;
72 char *val;
73 };
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 **);
88 typedef struct {
89 union {
90 long long number;
91 char *string;
92 struct node_branch *branch;
93 struct node_ref *ref;
94 } v;
95 int lineno;
96 } YYSTYPE;
98 %}
100 %token ERROR
101 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS FETCH_ALL_BRANCHES REFERENCE
103 %token FETCH SEND
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
111 %%
113 grammar : /* empty */
114 | grammar '\n'
115 | grammar author '\n'
116 | grammar remote '\n'
117 | grammar allowed_signers '\n'
119 boolean : STRING {
120 if (strcasecmp($1, "true") == 0 ||
121 strcasecmp($1, "yes") == 0)
122 $$ = 1;
123 else if (strcasecmp($1, "false") == 0 ||
124 strcasecmp($1, "no") == 0)
125 $$ = 0;
126 else {
127 yyerror("invalid boolean value '%s'", $1);
128 free($1);
129 YYERROR;
131 free($1);
134 numberstring : NUMBER {
135 char *s;
136 if (asprintf(&s, "%lld", $1) == -1) {
137 yyerror("string: asprintf");
138 YYERROR;
140 $$ = s;
142 | STRING
144 portplain : numberstring {
145 if (parseport($1, &$$) == -1) {
146 free($1);
147 YYERROR;
149 free($1);
152 branch : /* empty */ { $$ = NULL; }
153 | xbranch { $$ = $1; }
154 | '{' optnl branch_list '}' { $$ = $3; }
156 xbranch : STRING {
157 $$ = calloc(1, sizeof(struct node_branch));
158 if ($$ == NULL) {
159 yyerror("calloc");
160 YYERROR;
162 $$->branch_name = $1;
163 $$->tail = $$;
166 branch_list : xbranch optnl { $$ = $1; }
167 | branch_list comma xbranch optnl {
168 $1->tail->next = $3;
169 $1->tail = $3;
170 $$ = $1;
173 ref : /* empty */ { $$ = NULL; }
174 | xref { $$ = $1; }
175 | '{' optnl ref_list '}' { $$ = $3; }
177 xref : STRING {
178 $$ = calloc(1, sizeof(struct node_ref));
179 if ($$ == NULL) {
180 yyerror("calloc");
181 YYERROR;
183 $$->ref_name = $1;
184 $$->tail = $$;
187 ref_list : xref optnl { $$ = $1; }
188 | ref_list comma xref optnl {
189 $1->tail->next = $3;
190 $1->tail = $3;
191 $$ = $1;
194 remoteopts2 : remoteopts2 remoteopts1 nl
195 | remoteopts1 optnl
197 remoteopts1 : REPOSITORY STRING {
198 remote->repository = $2;
200 | SERVER STRING {
201 remote->server = $2;
203 | PROTOCOL STRING {
204 remote->protocol = $2;
206 | MIRROR_REFERENCES boolean {
207 remote->mirror_references = $2;
209 | FETCH_ALL_BRANCHES boolean {
210 remote->fetch_all_branches = $2;
212 | PORT portplain {
213 remote->port = $2;
215 | BRANCH branch {
216 remote->branch = $2;
218 | REFERENCE ref {
219 remote->fetch_ref = $2;
221 | FETCH {
222 static const struct got_error* error;
224 if (remote->fetch_config != NULL) {
225 yyerror("fetch block already exists");
226 YYERROR;
228 error = new_fetch_config(&remote->fetch_config);
229 if (error) {
230 yyerror("%s", error->msg);
231 YYERROR;
233 } '{' optnl fetchempty '}'
234 | SEND {
235 static const struct got_error* error;
237 if (remote->send_config != NULL) {
238 yyerror("send block already exists");
239 YYERROR;
241 error = new_send_config(&remote->send_config);
242 if (error) {
243 yyerror("%s", error->msg);
244 YYERROR;
246 } '{' optnl sendempty '}'
248 fetchempty : /* empty */
249 | fetchopts2
251 fetchopts2 : fetchopts2 fetchopts1 nl
252 | fetchopts1 optnl
254 fetchopts1 : REPOSITORY STRING {
255 remote->fetch_config->repository = $2;
257 | SERVER STRING {
258 remote->fetch_config->server = $2;
260 | PROTOCOL STRING {
261 remote->fetch_config->protocol = $2;
263 | PORT portplain {
264 remote->fetch_config->port = $2;
266 | BRANCH branch {
267 remote->fetch_config->branch = $2;
270 sendempty : /* empty */
271 | sendopts2
273 sendopts2 : sendopts2 sendopts1 nl
274 | sendopts1 optnl
276 sendopts1 : REPOSITORY STRING {
277 remote->send_config->repository = $2;
279 | SERVER STRING {
280 remote->send_config->server = $2;
282 | PROTOCOL STRING {
283 remote->send_config->protocol = $2;
285 | PORT portplain {
286 remote->send_config->port = $2;
288 | BRANCH branch {
289 remote->send_config->branch = $2;
292 remote : REMOTE STRING {
293 static const struct got_error* error;
295 error = new_remote(&remote);
296 if (error) {
297 free($2);
298 yyerror("%s", error->msg);
299 YYERROR;
301 remote->name = $2;
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;
319 optnl : '\n' optnl
320 | /* empty */
322 nl : '\n' optnl
324 comma : ','
325 | /* empty */
327 %%
329 struct keywords {
330 const char *k_name;
331 int k_val;
332 };
334 int
335 yyerror(const char *fmt, ...)
337 va_list ap;
338 char *msg;
339 char *err = NULL;
341 va_start(ap, fmt);
342 if (vasprintf(&msg, fmt, ap) == -1) {
343 gerror = got_error_from_errno("vasprintf");
344 return 0;
346 va_end(ap);
347 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
348 msg) == -1) {
349 gerror = got_error_from_errno("asprintf");
350 return(0);
352 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
353 free(msg);
354 return(0);
356 int
357 kw_cmp(const void *k, const void *e)
359 return (strcmp(k, ((const struct keywords *)e)->k_name));
362 int
363 lookup(char *s)
365 /* This has to be sorted always. */
366 static const struct keywords keywords[] = {
367 {"allowed_signers", ALLOWED_SIGNERS},
368 {"author", AUTHOR},
369 {"branch", BRANCH},
370 {"fetch", FETCH},
371 {"fetch-all-branches", FETCH_ALL_BRANCHES},
372 {"mirror-references", MIRROR_REFERENCES},
373 {"port", PORT},
374 {"protocol", PROTOCOL},
375 {"reference", REFERENCE},
376 {"remote", REMOTE},
377 {"repository", REPOSITORY},
378 {"revoked_signers", REVOKED_SIGNERS},
379 {"send", SEND},
380 {"server", SERVER},
381 };
382 const struct keywords *p;
384 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
385 sizeof(keywords[0]), kw_cmp);
387 if (p)
388 return (p->k_val);
389 else
390 return (STRING);
393 #define START_EXPAND 1
394 #define DONE_EXPAND 2
396 static int expanding;
398 int
399 igetc(void)
401 int c;
403 while (1) {
404 if (file->ungetpos > 0)
405 c = file->ungetbuf[--file->ungetpos];
406 else
407 c = getc(file->stream);
409 if (c == START_EXPAND)
410 expanding = 1;
411 else if (c == DONE_EXPAND)
412 expanding = 0;
413 else
414 break;
416 return (c);
419 int
420 lgetc(int quotec)
422 int c, next;
424 if (quotec) {
425 c = igetc();
426 if (c == EOF) {
427 yyerror("reached end of file while parsing "
428 "quoted string");
430 return (c);
433 c = igetc();
434 while (c == '\\') {
435 next = igetc();
436 if (next != '\n') {
437 c = next;
438 break;
440 yylval.lineno = file->lineno;
441 file->lineno++;
444 return (c);
447 void
448 lungetc(int c)
450 if (c == EOF)
451 return;
453 if (file->ungetpos >= file->ungetsize) {
454 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
455 if (p == NULL)
456 err(1, "%s", __func__);
457 file->ungetbuf = p;
458 file->ungetsize *= 2;
460 file->ungetbuf[file->ungetpos++] = c;
463 int
464 findeol(void)
466 int c;
468 /* Skip to either EOF or the first real EOL. */
469 while (1) {
470 c = lgetc(0);
471 if (c == '\n') {
472 file->lineno++;
473 break;
475 if (c == EOF)
476 break;
478 return (ERROR);
481 static long long
482 getservice(char *n)
484 struct servent *s;
485 u_long ulval;
487 if (atoul(n, &ulval) == 0) {
488 if (ulval == 0 || ulval > 65535) {
489 yyerror("illegal port value %lu", ulval);
490 return (-1);
492 return ulval;
493 } else {
494 s = getservbyname(n, "tcp");
495 if (s == NULL)
496 s = getservbyname(n, "udp");
497 if (s == NULL) {
498 yyerror("unknown port %s", n);
499 return (-1);
501 return (s->s_port);
505 static int
506 parseport(char *port, long long *pn)
508 if ((*pn = getservice(port)) == -1) {
509 *pn = 0LL;
510 return (-1);
512 return (0);
516 int
517 yylex(void)
519 char buf[8096];
520 char *p, *val;
521 int quotec, next, c;
522 int token;
524 top:
525 p = buf;
526 c = lgetc(0);
527 while (c == ' ' || c == '\t')
528 c = lgetc(0); /* nothing */
530 yylval.lineno = file->lineno;
531 if (c == '#') {
532 c = lgetc(0);
533 while (c != '\n' && c != EOF)
534 c = lgetc(0); /* nothing */
536 if (c == '$' && !expanding) {
537 while (1) {
538 c = lgetc(0);
539 if (c == EOF)
540 return (0);
542 if (p + 1 >= buf + sizeof(buf) - 1) {
543 yyerror("string too long");
544 return (findeol());
546 if (isalnum(c) || c == '_') {
547 *p++ = c;
548 continue;
550 *p = '\0';
551 lungetc(c);
552 break;
554 val = symget(buf);
555 if (val == NULL) {
556 yyerror("macro '%s' not defined", buf);
557 return (findeol());
559 p = val + strlen(val) - 1;
560 lungetc(DONE_EXPAND);
561 while (p >= val) {
562 lungetc((unsigned char)*p);
563 p--;
565 lungetc(START_EXPAND);
566 goto top;
569 switch (c) {
570 case '\'':
571 case '"':
572 quotec = c;
573 while (1) {
574 c = lgetc(quotec);
575 if (c == EOF)
576 return (0);
577 if (c == '\n') {
578 file->lineno++;
579 continue;
580 } else if (c == '\\') {
581 next = lgetc(quotec);
582 if (next == EOF)
583 return (0);
584 if (next == quotec || c == ' ' || c == '\t')
585 c = next;
586 else if (next == '\n') {
587 file->lineno++;
588 continue;
589 } else
590 lungetc(next);
591 } else if (c == quotec) {
592 *p = '\0';
593 break;
594 } else if (c == '\0') {
595 yyerror("syntax error");
596 return (findeol());
598 if (p + 1 >= buf + sizeof(buf) - 1) {
599 yyerror("string too long");
600 return (findeol());
602 *p++ = c;
604 yylval.v.string = strdup(buf);
605 if (yylval.v.string == NULL)
606 err(1, "%s", __func__);
607 return (STRING);
610 #define allowed_to_end_number(x) \
611 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
613 if (c == '-' || isdigit(c)) {
614 do {
615 *p++ = c;
616 if ((size_t)(p-buf) >= sizeof(buf)) {
617 yyerror("string too long");
618 return (findeol());
620 c = lgetc(0);
621 } while (c != EOF && isdigit(c));
622 lungetc(c);
623 if (p == buf + 1 && buf[0] == '-')
624 goto nodigits;
625 if (c == EOF || allowed_to_end_number(c)) {
626 const char *errstr = NULL;
628 *p = '\0';
629 yylval.v.number = strtonum(buf, LLONG_MIN,
630 LLONG_MAX, &errstr);
631 if (errstr) {
632 yyerror("\"%s\" invalid number: %s",
633 buf, errstr);
634 return (findeol());
636 return (NUMBER);
637 } else {
638 nodigits:
639 while (p > buf + 1)
640 lungetc((unsigned char)*--p);
641 c = (unsigned char)*--p;
642 if (c == '-')
643 return (c);
647 #define allowed_in_string(x) \
648 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
649 x != '{' && x != '}' && \
650 x != '!' && x != '=' && x != '#' && \
651 x != ','))
653 if (isalnum(c) || c == ':' || c == '_') {
654 do {
655 *p++ = c;
656 if ((size_t)(p-buf) >= sizeof(buf)) {
657 yyerror("string too long");
658 return (findeol());
660 c = lgetc(0);
661 } while (c != EOF && (allowed_in_string(c)));
662 lungetc(c);
663 *p = '\0';
664 token = lookup(buf);
665 if (token == STRING) {
666 yylval.v.string = strdup(buf);
667 if (yylval.v.string == NULL)
668 err(1, "%s", __func__);
670 return (token);
672 if (c == '\n') {
673 yylval.lineno = file->lineno;
674 file->lineno++;
676 if (c == EOF)
677 return (0);
678 return (c);
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");
692 free((*nfile));
693 return error;
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);
703 free((*nfile));
704 return error;
706 return NULL;
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));
715 if (*remote == NULL)
716 error = got_error_from_errno("calloc");
717 return error;
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");
728 return error;
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");
739 return error;
742 static void
743 closefile(struct file *file)
745 fclose(file->stream);
746 free(file->ungetbuf);
747 free(file);
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;
756 *conf = NULL;
758 err = newfile(&file, filename, fd);
759 if (err)
760 return err;
762 TAILQ_INIT(&gotconfig.remotes);
764 yyparse();
765 closefile(file);
767 /* Free macros and check which have not been used. */
768 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
769 if (!sym->persist) {
770 free(sym->nam);
771 free(sym->val);
772 TAILQ_REMOVE(&symhead, sym, entry);
773 free(sym);
777 if (gerror == NULL)
778 *conf = &gotconfig;
779 return gerror;
782 static void
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);
791 static void
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);
800 void
801 gotconfig_free(struct gotconfig *conf)
803 struct gotconfig_remote_repo *remote;
805 free(conf->author);
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);
815 free(remote->name);
816 free(remote->repository);
817 free(remote->server);
818 free(remote->protocol);
819 free(remote);
823 int
824 symset(const char *nam, const char *val, int persist)
826 struct sym *sym;
828 TAILQ_FOREACH(sym, &symhead, entry) {
829 if (strcmp(nam, sym->nam) == 0)
830 break;
833 if (sym != NULL) {
834 if (sym->persist == 1)
835 return (0);
836 else {
837 free(sym->nam);
838 free(sym->val);
839 TAILQ_REMOVE(&symhead, sym, entry);
840 free(sym);
843 sym = calloc(1, sizeof(*sym));
844 if (sym == NULL)
845 return (-1);
847 sym->nam = strdup(nam);
848 if (sym->nam == NULL) {
849 free(sym);
850 return (-1);
852 sym->val = strdup(val);
853 if (sym->val == NULL) {
854 free(sym->nam);
855 free(sym);
856 return (-1);
858 sym->used = 0;
859 sym->persist = persist;
860 TAILQ_INSERT_TAIL(&symhead, sym, entry);
861 return (0);
864 int
865 cmdline_symset(char *s)
867 char *sym, *val;
868 int ret;
869 size_t len;
871 val = strrchr(s, '=');
872 if (val == NULL)
873 return (-1);
875 len = strlen(s) - strlen(val) + 1;
876 sym = malloc(len);
877 if (sym == NULL)
878 errx(1, "cmdline_symset: malloc");
880 strlcpy(sym, s, len);
882 ret = symset(sym, val + 1, 1);
883 free(sym);
885 return (ret);
888 char *
889 symget(const char *nam)
891 struct sym *sym;
893 TAILQ_FOREACH(sym, &symhead, entry) {
894 if (strcmp(nam, sym->nam) == 0) {
895 sym->used = 1;
896 return (sym->val);
899 return (NULL);
902 static int
903 atoul(char *s, u_long *ulvalp)
905 u_long ulval;
906 char *ep;
908 errno = 0;
909 ulval = strtoul(s, &ep, 0);
910 if (s[0] == '\0' || *ep != '\0')
911 return (-1);
912 if (errno == ERANGE && ulval == ULONG_MAX)
913 return (-1);
914 *ulvalp = ulval;
915 return (0);