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 SIGNER_ID FETCH_ALL_BRANCHES
108 %token REFERENCE 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'
124 | grammar signer_id '\n'
126 boolean : STRING {
127 if (strcasecmp($1, "true") == 0 ||
128 strcasecmp($1, "yes") == 0)
129 $$ = 1;
130 else if (strcasecmp($1, "false") == 0 ||
131 strcasecmp($1, "no") == 0)
132 $$ = 0;
133 else {
134 yyerror("invalid boolean value '%s'", $1);
135 free($1);
136 YYERROR;
138 free($1);
141 numberstring : NUMBER {
142 char *s;
143 if (asprintf(&s, "%lld", $1) == -1) {
144 yyerror("string: asprintf");
145 YYERROR;
147 $$ = s;
149 | STRING
151 portplain : numberstring {
152 if (parseport($1, &$$) == -1) {
153 free($1);
154 YYERROR;
156 free($1);
159 branch : /* empty */ { $$ = NULL; }
160 | xbranch { $$ = $1; }
161 | '{' optnl branch_list '}' { $$ = $3; }
163 xbranch : STRING {
164 $$ = calloc(1, sizeof(struct node_branch));
165 if ($$ == NULL) {
166 yyerror("calloc");
167 YYERROR;
169 $$->branch_name = $1;
170 $$->tail = $$;
173 branch_list : xbranch optnl { $$ = $1; }
174 | branch_list comma xbranch optnl {
175 $1->tail->next = $3;
176 $1->tail = $3;
177 $$ = $1;
180 ref : /* empty */ { $$ = NULL; }
181 | xref { $$ = $1; }
182 | '{' optnl ref_list '}' { $$ = $3; }
184 xref : STRING {
185 $$ = calloc(1, sizeof(struct node_ref));
186 if ($$ == NULL) {
187 yyerror("calloc");
188 YYERROR;
190 $$->ref_name = $1;
191 $$->tail = $$;
194 ref_list : xref optnl { $$ = $1; }
195 | ref_list comma xref optnl {
196 $1->tail->next = $3;
197 $1->tail = $3;
198 $$ = $1;
201 remoteopts2 : remoteopts2 remoteopts1 nl
202 | remoteopts1 optnl
204 remoteopts1 : REPOSITORY STRING {
205 remote->repository = $2;
207 | SERVER STRING {
208 remote->server = $2;
210 | PROTOCOL STRING {
211 remote->protocol = $2;
213 | MIRROR_REFERENCES boolean {
214 remote->mirror_references = $2;
216 | FETCH_ALL_BRANCHES boolean {
217 remote->fetch_all_branches = $2;
219 | PORT portplain {
220 remote->port = $2;
222 | BRANCH branch {
223 remote->branch = $2;
225 | REFERENCE ref {
226 remote->fetch_ref = $2;
228 | FETCH {
229 static const struct got_error* error;
231 if (remote->fetch_config != NULL) {
232 yyerror("fetch block already exists");
233 YYERROR;
235 error = new_fetch_config(&remote->fetch_config);
236 if (error) {
237 yyerror("%s", error->msg);
238 YYERROR;
240 } '{' optnl fetchempty '}'
241 | SEND {
242 static const struct got_error* error;
244 if (remote->send_config != NULL) {
245 yyerror("send block already exists");
246 YYERROR;
248 error = new_send_config(&remote->send_config);
249 if (error) {
250 yyerror("%s", error->msg);
251 YYERROR;
253 } '{' optnl sendempty '}'
255 fetchempty : /* empty */
256 | fetchopts2
258 fetchopts2 : fetchopts2 fetchopts1 nl
259 | fetchopts1 optnl
261 fetchopts1 : REPOSITORY STRING {
262 remote->fetch_config->repository = $2;
264 | SERVER STRING {
265 remote->fetch_config->server = $2;
267 | PROTOCOL STRING {
268 remote->fetch_config->protocol = $2;
270 | PORT portplain {
271 remote->fetch_config->port = $2;
273 | BRANCH branch {
274 remote->fetch_config->branch = $2;
277 sendempty : /* empty */
278 | sendopts2
280 sendopts2 : sendopts2 sendopts1 nl
281 | sendopts1 optnl
283 sendopts1 : REPOSITORY STRING {
284 remote->send_config->repository = $2;
286 | SERVER STRING {
287 remote->send_config->server = $2;
289 | PROTOCOL STRING {
290 remote->send_config->protocol = $2;
292 | PORT portplain {
293 remote->send_config->port = $2;
295 | BRANCH branch {
296 remote->send_config->branch = $2;
299 remote : REMOTE STRING {
300 static const struct got_error* error;
302 error = new_remote(&remote);
303 if (error) {
304 free($2);
305 yyerror("%s", error->msg);
306 YYERROR;
308 remote->name = $2;
309 } '{' optnl remoteopts2 '}' {
310 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
311 gotconfig.nremotes++;
314 author : AUTHOR STRING {
315 gotconfig.author = $2;
318 allowed_signers : ALLOWED_SIGNERS STRING {
319 gotconfig.allowed_signers_file = $2;
322 revoked_signers : REVOKED_SIGNERS STRING {
323 gotconfig.revoked_signers_file = $2;
326 signer_id : SIGNER_ID STRING {
327 gotconfig.signer_id = $2;
330 optnl : '\n' optnl
331 | /* empty */
333 nl : '\n' optnl
335 comma : ','
336 | /* empty */
338 %%
340 struct keywords {
341 const char *k_name;
342 int k_val;
343 };
345 int
346 yyerror(const char *fmt, ...)
348 va_list ap;
349 char *msg;
350 char *err = NULL;
352 va_start(ap, fmt);
353 if (vasprintf(&msg, fmt, ap) == -1) {
354 gerror = got_error_from_errno("vasprintf");
355 return 0;
357 va_end(ap);
358 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
359 msg) == -1) {
360 gerror = got_error_from_errno("asprintf");
361 return(0);
363 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
364 free(msg);
365 return(0);
367 int
368 kw_cmp(const void *k, const void *e)
370 return (strcmp(k, ((const struct keywords *)e)->k_name));
373 int
374 lookup(char *s)
376 /* This has to be sorted always. */
377 static const struct keywords keywords[] = {
378 {"allowed_signers", ALLOWED_SIGNERS},
379 {"author", AUTHOR},
380 {"branch", BRANCH},
381 {"fetch", FETCH},
382 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
383 {"fetch_all_branches", FETCH_ALL_BRANCHES},
384 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
385 {"mirror_references", MIRROR_REFERENCES},
386 {"port", PORT},
387 {"protocol", PROTOCOL},
388 {"reference", REFERENCE},
389 {"remote", REMOTE},
390 {"repository", REPOSITORY},
391 {"revoked_signers", REVOKED_SIGNERS},
392 {"send", SEND},
393 {"server", SERVER},
394 {"signer_id", SIGNER_ID},
395 };
396 const struct keywords *p;
398 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
399 sizeof(keywords[0]), kw_cmp);
401 if (p)
402 return (p->k_val);
403 else
404 return (STRING);
407 #define START_EXPAND 1
408 #define DONE_EXPAND 2
410 static int expanding;
412 int
413 igetc(void)
415 int c;
417 while (1) {
418 if (file->ungetpos > 0)
419 c = file->ungetbuf[--file->ungetpos];
420 else
421 c = getc(file->stream);
423 if (c == START_EXPAND)
424 expanding = 1;
425 else if (c == DONE_EXPAND)
426 expanding = 0;
427 else
428 break;
430 return (c);
433 int
434 lgetc(int quotec)
436 int c, next;
438 if (quotec) {
439 c = igetc();
440 if (c == EOF) {
441 yyerror("reached end of file while parsing "
442 "quoted string");
444 return (c);
447 c = igetc();
448 while (c == '\\') {
449 next = igetc();
450 if (next != '\n') {
451 c = next;
452 break;
454 yylval.lineno = file->lineno;
455 file->lineno++;
458 return (c);
461 void
462 lungetc(int c)
464 if (c == EOF)
465 return;
467 if (file->ungetpos >= file->ungetsize) {
468 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
469 if (p == NULL)
470 err(1, "%s", __func__);
471 file->ungetbuf = p;
472 file->ungetsize *= 2;
474 file->ungetbuf[file->ungetpos++] = c;
477 int
478 findeol(void)
480 int c;
482 /* Skip to either EOF or the first real EOL. */
483 while (1) {
484 c = lgetc(0);
485 if (c == '\n') {
486 file->lineno++;
487 break;
489 if (c == EOF)
490 break;
492 return (ERROR);
495 static long long
496 getservice(char *n)
498 struct servent *s;
499 u_long ulval;
501 if (atoul(n, &ulval) == 0) {
502 if (ulval == 0 || ulval > 65535) {
503 yyerror("illegal port value %lu", ulval);
504 return (-1);
506 return ulval;
507 } else {
508 s = getservbyname(n, "tcp");
509 if (s == NULL)
510 s = getservbyname(n, "udp");
511 if (s == NULL) {
512 yyerror("unknown port %s", n);
513 return (-1);
515 return (s->s_port);
519 static int
520 parseport(char *port, long long *pn)
522 if ((*pn = getservice(port)) == -1) {
523 *pn = 0LL;
524 return (-1);
526 return (0);
530 int
531 yylex(void)
533 char buf[8096];
534 char *p, *val;
535 int quotec, next, c;
536 int token;
538 top:
539 p = buf;
540 c = lgetc(0);
541 while (c == ' ' || c == '\t')
542 c = lgetc(0); /* nothing */
544 yylval.lineno = file->lineno;
545 if (c == '#') {
546 c = lgetc(0);
547 while (c != '\n' && c != EOF)
548 c = lgetc(0); /* nothing */
550 if (c == '$' && !expanding) {
551 while (1) {
552 c = lgetc(0);
553 if (c == EOF)
554 return (0);
556 if (p + 1 >= buf + sizeof(buf) - 1) {
557 yyerror("string too long");
558 return (findeol());
560 if (isalnum(c) || c == '_') {
561 *p++ = c;
562 continue;
564 *p = '\0';
565 lungetc(c);
566 break;
568 val = symget(buf);
569 if (val == NULL) {
570 yyerror("macro '%s' not defined", buf);
571 return (findeol());
573 p = val + strlen(val) - 1;
574 lungetc(DONE_EXPAND);
575 while (p >= val) {
576 lungetc((unsigned char)*p);
577 p--;
579 lungetc(START_EXPAND);
580 goto top;
583 switch (c) {
584 case '\'':
585 case '"':
586 quotec = c;
587 while (1) {
588 c = lgetc(quotec);
589 if (c == EOF)
590 return (0);
591 if (c == '\n') {
592 file->lineno++;
593 continue;
594 } else if (c == '\\') {
595 next = lgetc(quotec);
596 if (next == EOF)
597 return (0);
598 if (next == quotec || c == ' ' || c == '\t')
599 c = next;
600 else if (next == '\n') {
601 file->lineno++;
602 continue;
603 } else
604 lungetc(next);
605 } else if (c == quotec) {
606 *p = '\0';
607 break;
608 } else if (c == '\0') {
609 yyerror("syntax error");
610 return (findeol());
612 if (p + 1 >= buf + sizeof(buf) - 1) {
613 yyerror("string too long");
614 return (findeol());
616 *p++ = c;
618 yylval.v.string = strdup(buf);
619 if (yylval.v.string == NULL)
620 err(1, "%s", __func__);
621 return (STRING);
624 #define allowed_to_end_number(x) \
625 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
627 if (c == '-' || isdigit(c)) {
628 do {
629 *p++ = c;
630 if ((size_t)(p-buf) >= sizeof(buf)) {
631 yyerror("string too long");
632 return (findeol());
634 c = lgetc(0);
635 } while (c != EOF && isdigit(c));
636 lungetc(c);
637 if (p == buf + 1 && buf[0] == '-')
638 goto nodigits;
639 if (c == EOF || allowed_to_end_number(c)) {
640 const char *errstr = NULL;
642 *p = '\0';
643 yylval.v.number = strtonum(buf, LLONG_MIN,
644 LLONG_MAX, &errstr);
645 if (errstr) {
646 yyerror("\"%s\" invalid number: %s",
647 buf, errstr);
648 return (findeol());
650 return (NUMBER);
651 } else {
652 nodigits:
653 while (p > buf + 1)
654 lungetc((unsigned char)*--p);
655 c = (unsigned char)*--p;
656 if (c == '-')
657 return (c);
661 #define allowed_in_string(x) \
662 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
663 x != '{' && x != '}' && \
664 x != '!' && x != '=' && x != '#' && \
665 x != ','))
667 if (isalnum(c) || c == ':' || c == '_') {
668 do {
669 *p++ = c;
670 if ((size_t)(p-buf) >= sizeof(buf)) {
671 yyerror("string too long");
672 return (findeol());
674 c = lgetc(0);
675 } while (c != EOF && (allowed_in_string(c)));
676 lungetc(c);
677 *p = '\0';
678 token = lookup(buf);
679 if (token == STRING) {
680 yylval.v.string = strdup(buf);
681 if (yylval.v.string == NULL)
682 err(1, "%s", __func__);
684 return (token);
686 if (c == '\n') {
687 yylval.lineno = file->lineno;
688 file->lineno++;
690 if (c == EOF)
691 return (0);
692 return (c);
695 static const struct got_error*
696 newfile(struct file **nfile, const char *filename, int *fd)
698 const struct got_error* error = NULL;
700 (*nfile) = calloc(1, sizeof(struct file));
701 if ((*nfile) == NULL)
702 return got_error_from_errno("calloc");
703 (*nfile)->stream = fdopen(*fd, "r");
704 if ((*nfile)->stream == NULL) {
705 error = got_error_from_errno("fdopen");
706 free((*nfile));
707 return error;
709 *fd = -1; /* Stream owns the file descriptor now. */
710 (*nfile)->name = filename;
711 (*nfile)->lineno = 1;
712 (*nfile)->ungetsize = 16;
713 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
714 if ((*nfile)->ungetbuf == NULL) {
715 error = got_error_from_errno("malloc");
716 fclose((*nfile)->stream);
717 free((*nfile));
718 return error;
720 return NULL;
723 static const struct got_error*
724 new_remote(struct gotconfig_remote_repo **remote)
726 const struct got_error *error = NULL;
728 *remote = calloc(1, sizeof(**remote));
729 if (*remote == NULL)
730 error = got_error_from_errno("calloc");
731 return error;
734 static const struct got_error*
735 new_fetch_config(struct fetch_config **fetch_config)
737 const struct got_error *error = NULL;
739 *fetch_config = calloc(1, sizeof(**fetch_config));
740 if (*fetch_config == NULL)
741 error = got_error_from_errno("calloc");
742 return error;
745 static const struct got_error*
746 new_send_config(struct send_config **send_config)
748 const struct got_error *error = NULL;
750 *send_config = calloc(1, sizeof(**send_config));
751 if (*send_config == NULL)
752 error = got_error_from_errno("calloc");
753 return error;
756 static void
757 closefile(struct file *file)
759 fclose(file->stream);
760 free(file->ungetbuf);
761 free(file);
764 const struct got_error *
765 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
767 const struct got_error *err = NULL;
768 struct sym *sym, *next;
770 *conf = NULL;
772 err = newfile(&file, filename, fd);
773 if (err)
774 return err;
776 TAILQ_INIT(&gotconfig.remotes);
778 yyparse();
779 closefile(file);
781 /* Free macros and check which have not been used. */
782 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
783 if (!sym->persist) {
784 free(sym->nam);
785 free(sym->val);
786 TAILQ_REMOVE(&symhead, sym, entry);
787 free(sym);
791 if (gerror == NULL)
792 *conf = &gotconfig;
793 return gerror;
796 static void
797 free_fetch_config(struct fetch_config *fetch_config)
799 free(remote->fetch_config->repository);
800 free(remote->fetch_config->server);
801 free(remote->fetch_config->protocol);
802 free(remote->fetch_config);
805 static void
806 free_send_config(struct send_config *send_config)
808 free(remote->send_config->repository);
809 free(remote->send_config->server);
810 free(remote->send_config->protocol);
811 free(remote->send_config);
814 void
815 gotconfig_free(struct gotconfig *conf)
817 struct gotconfig_remote_repo *remote;
819 free(conf->author);
820 free(conf->allowed_signers_file);
821 free(conf->revoked_signers_file);
822 free(conf->signer_id);
823 while (!TAILQ_EMPTY(&conf->remotes)) {
824 remote = TAILQ_FIRST(&conf->remotes);
825 TAILQ_REMOVE(&conf->remotes, remote, entry);
826 if (remote->fetch_config != NULL)
827 free_fetch_config(remote->fetch_config);
828 if (remote->send_config != NULL)
829 free_send_config(remote->send_config);
830 free(remote->name);
831 free(remote->repository);
832 free(remote->server);
833 free(remote->protocol);
834 free(remote);
838 int
839 symset(const char *nam, const char *val, int persist)
841 struct sym *sym;
843 TAILQ_FOREACH(sym, &symhead, entry) {
844 if (strcmp(nam, sym->nam) == 0)
845 break;
848 if (sym != NULL) {
849 if (sym->persist == 1)
850 return (0);
851 else {
852 free(sym->nam);
853 free(sym->val);
854 TAILQ_REMOVE(&symhead, sym, entry);
855 free(sym);
858 sym = calloc(1, sizeof(*sym));
859 if (sym == NULL)
860 return (-1);
862 sym->nam = strdup(nam);
863 if (sym->nam == NULL) {
864 free(sym);
865 return (-1);
867 sym->val = strdup(val);
868 if (sym->val == NULL) {
869 free(sym->nam);
870 free(sym);
871 return (-1);
873 sym->used = 0;
874 sym->persist = persist;
875 TAILQ_INSERT_TAIL(&symhead, sym, entry);
876 return (0);
879 int
880 cmdline_symset(char *s)
882 char *sym, *val;
883 int ret;
884 size_t len;
886 val = strrchr(s, '=');
887 if (val == NULL)
888 return (-1);
890 len = strlen(s) - strlen(val) + 1;
891 sym = malloc(len);
892 if (sym == NULL)
893 errx(1, "cmdline_symset: malloc");
895 strlcpy(sym, s, len);
897 ret = symset(sym, val + 1, 1);
898 free(sym);
900 return (ret);
903 char *
904 symget(const char *nam)
906 struct sym *sym;
908 TAILQ_FOREACH(sym, &symhead, entry) {
909 if (strcmp(nam, sym->nam) == 0) {
910 sym->used = 1;
911 return (sym->val);
914 return (NULL);
917 static int
918 atoul(char *s, u_long *ulvalp)
920 u_long ulval;
921 char *ep;
923 errno = 0;
924 ulval = strtoul(s, &ep, 0);
925 if (s[0] == '\0' || *ep != '\0')
926 return (-1);
927 if (errno == ERANGE && ulval == ULONG_MAX)
928 return (-1);
929 *ulvalp = ulval;
930 return (0);