Blob


1 /*
2 * Copyright (c) 2020 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>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
30 #include <netinet/in.h>
32 #include <arpa/inet.h>
34 #include <netdb.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <event.h>
40 #include <ifaddrs.h>
41 #include <imsg.h>
42 #include <limits.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
49 #include "got_error.h"
50 #include "gotconfig.h"
52 static struct file {
53 FILE *stream;
54 const char *name;
55 size_t ungetpos;
56 size_t ungetsize;
57 u_char *ungetbuf;
58 int eof_reached;
59 int lineno;
60 } *file;
61 static const struct got_error* newfile(struct file**, const char *, int *);
62 static void closefile(struct file *);
63 int yyparse(void);
64 int yylex(void);
65 int yyerror(const char *, ...)
66 __attribute__((__format__ (printf, 1, 2)))
67 __attribute__((__nonnull__ (1)));
68 int kw_cmp(const void *, const void *);
69 int lookup(char *);
70 int igetc(void);
71 int lgetc(int);
72 void lungetc(int);
73 int findeol(void);
74 static int parseport(char *, long long *);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 struct sym {
78 TAILQ_ENTRY(sym) entry;
79 int used;
80 int persist;
81 char *nam;
82 char *val;
83 };
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
88 static int atoul(char *, u_long *);
90 static const struct got_error* gerror;
91 static struct gotconfig_remote_repo *remote;
92 static struct gotconfig gotconfig;
93 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
95 typedef struct {
96 union {
97 int64_t number;
98 char *string;
99 } v;
100 int lineno;
101 } YYSTYPE;
103 %}
105 %token ERROR
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
107 %token <v.string> STRING
108 %token <v.number> NUMBER
109 %type <v.number> boolean portplain
110 %type <v.string> numberstring
112 %%
114 grammar : /* empty */
115 | grammar '\n'
116 | grammar author '\n'
117 | grammar remote '\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 remoteopts2 : remoteopts2 remoteopts1 nl
153 | remoteopts1 optnl
155 remoteopts1 : REPOSITORY STRING {
156 remote->repository = strdup($2);
157 if (remote->repository == NULL) {
158 free($2);
159 yyerror("strdup");
160 YYERROR;
162 free($2);
164 | SERVER STRING {
165 remote->server = strdup($2);
166 if (remote->server == NULL) {
167 free($2);
168 yyerror("strdup");
169 YYERROR;
171 free($2);
173 | PROTOCOL STRING {
174 remote->protocol = strdup($2);
175 if (remote->protocol == NULL) {
176 free($2);
177 yyerror("strdup");
178 YYERROR;
180 free($2);
182 | MIRROR_REFERENCES boolean {
183 remote->mirror_references = $2;
185 | PORT portplain {
186 remote->port = $2;
189 remote : REMOTE STRING {
190 static const struct got_error* error;
192 error = new_remote(&remote);
193 if (error) {
194 free($2);
195 yyerror("%s", error->msg);
196 YYERROR;
198 remote->name = strdup($2);
199 if (remote->name == NULL) {
200 free($2);
201 yyerror("strdup");
202 YYERROR;
204 free($2);
205 } '{' optnl remoteopts2 '}' {
206 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
207 gotconfig.nremotes++;
210 author : AUTHOR STRING {
211 gotconfig.author = strdup($2);
212 if (gotconfig.author == NULL) {
213 free($2);
214 yyerror("strdup");
215 YYERROR;
217 free($2);
220 optnl : '\n' optnl
221 | /* empty */
223 nl : '\n' optnl
225 %%
227 struct keywords {
228 const char *k_name;
229 int k_val;
230 };
232 int
233 yyerror(const char *fmt, ...)
235 va_list ap;
236 char *msg;
237 char *err = NULL;
239 va_start(ap, fmt);
240 if (vasprintf(&msg, fmt, ap) == -1) {
241 gerror = got_error_from_errno("vasprintf");
242 return 0;
244 va_end(ap);
245 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
246 msg) == -1) {
247 gerror = got_error_from_errno("asprintf");
248 return(0);
250 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
251 free(msg);
252 free(err);
253 return(0);
255 int
256 kw_cmp(const void *k, const void *e)
258 return (strcmp(k, ((const struct keywords *)e)->k_name));
261 int
262 lookup(char *s)
264 /* This has to be sorted always. */
265 static const struct keywords keywords[] = {
266 {"author", AUTHOR},
267 {"mirror-references", MIRROR_REFERENCES},
268 {"port", PORT},
269 {"protocol", PROTOCOL},
270 {"remote", REMOTE},
271 {"repository", REPOSITORY},
272 {"server", SERVER},
273 };
274 const struct keywords *p;
276 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
277 sizeof(keywords[0]), kw_cmp);
279 if (p)
280 return (p->k_val);
281 else
282 return (STRING);
285 #define START_EXPAND 1
286 #define DONE_EXPAND 2
288 static int expanding;
290 int
291 igetc(void)
293 int c;
295 while (1) {
296 if (file->ungetpos > 0)
297 c = file->ungetbuf[--file->ungetpos];
298 else
299 c = getc(file->stream);
301 if (c == START_EXPAND)
302 expanding = 1;
303 else if (c == DONE_EXPAND)
304 expanding = 0;
305 else
306 break;
308 return (c);
311 int
312 lgetc(int quotec)
314 int c, next;
316 if (quotec) {
317 c = igetc();
318 if (c == EOF) {
319 yyerror("reached end of file while parsing "
320 "quoted string");
322 return (c);
325 c = igetc();
326 while (c == '\\') {
327 next = igetc();
328 if (next != '\n') {
329 c = next;
330 break;
332 yylval.lineno = file->lineno;
333 file->lineno++;
336 return (c);
339 void
340 lungetc(int c)
342 if (c == EOF)
343 return;
345 if (file->ungetpos >= file->ungetsize) {
346 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
347 if (p == NULL)
348 err(1, "%s", __func__);
349 file->ungetbuf = p;
350 file->ungetsize *= 2;
352 file->ungetbuf[file->ungetpos++] = c;
355 int
356 findeol(void)
358 int c;
360 /* Skip to either EOF or the first real EOL. */
361 while (1) {
362 c = lgetc(0);
363 if (c == '\n') {
364 file->lineno++;
365 break;
367 if (c == EOF)
368 break;
370 return (ERROR);
373 static long long
374 getservice(char *n)
376 struct servent *s;
377 u_long ulval;
379 if (atoul(n, &ulval) == 0) {
380 if (ulval > 65535) {
381 yyerror("illegal port value %lu", ulval);
382 return (-1);
384 return ulval;
385 } else {
386 s = getservbyname(n, "tcp");
387 if (s == NULL)
388 s = getservbyname(n, "udp");
389 if (s == NULL) {
390 yyerror("unknown port %s", n);
391 return (-1);
393 return (s->s_port);
397 static int
398 parseport(char *port, long long *pn)
400 if ((*pn = getservice(port)) == -1) {
401 *pn = 0LL;
402 return (-1);
404 return (0);
408 int
409 yylex(void)
411 unsigned char buf[8096];
412 unsigned char *p, *val;
413 int quotec, next, c;
414 int token;
416 top:
417 p = buf;
418 c = lgetc(0);
419 while (c == ' ' || c == '\t')
420 c = lgetc(0); /* nothing */
422 yylval.lineno = file->lineno;
423 if (c == '#') {
424 c = lgetc(0);
425 while (c != '\n' && c != EOF)
426 c = lgetc(0); /* nothing */
428 if (c == '$' && !expanding) {
429 while (1) {
430 c = lgetc(0);
431 if (c == EOF)
432 return (0);
434 if (p + 1 >= buf + sizeof(buf) - 1) {
435 yyerror("string too long");
436 return (findeol());
438 if (isalnum(c) || c == '_') {
439 *p++ = c;
440 continue;
442 *p = '\0';
443 lungetc(c);
444 break;
446 val = symget(buf);
447 if (val == NULL) {
448 yyerror("macro '%s' not defined", buf);
449 return (findeol());
451 p = val + strlen(val) - 1;
452 lungetc(DONE_EXPAND);
453 while (p >= val) {
454 lungetc(*p);
455 p--;
457 lungetc(START_EXPAND);
458 goto top;
461 switch (c) {
462 case '\'':
463 case '"':
464 quotec = c;
465 while (1) {
466 c = lgetc(quotec);
467 if (c == EOF)
468 return (0);
469 if (c == '\n') {
470 file->lineno++;
471 continue;
472 } else if (c == '\\') {
473 next = lgetc(quotec);
474 if (next == EOF)
475 return (0);
476 if (next == quotec || c == ' ' || c == '\t')
477 c = next;
478 else if (next == '\n') {
479 file->lineno++;
480 continue;
481 } else
482 lungetc(next);
483 } else if (c == quotec) {
484 *p = '\0';
485 break;
486 } else if (c == '\0') {
487 yyerror("syntax error");
488 return (findeol());
490 if (p + 1 >= buf + sizeof(buf) - 1) {
491 yyerror("string too long");
492 return (findeol());
494 *p++ = c;
496 yylval.v.string = strdup(buf);
497 if (yylval.v.string == NULL)
498 err(1, "%s", __func__);
499 return (STRING);
502 #define allowed_to_end_number(x) \
503 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
505 if (c == '-' || isdigit(c)) {
506 do {
507 *p++ = c;
508 if ((unsigned)(p-buf) >= sizeof(buf)) {
509 yyerror("string too long");
510 return (findeol());
512 c = lgetc(0);
513 } while (c != EOF && isdigit(c));
514 lungetc(c);
515 if (p == buf + 1 && buf[0] == '-')
516 goto nodigits;
517 if (c == EOF || allowed_to_end_number(c)) {
518 const char *errstr = NULL;
520 *p = '\0';
521 yylval.v.number = strtonum(buf, LLONG_MIN,
522 LLONG_MAX, &errstr);
523 if (errstr) {
524 yyerror("\"%s\" invalid number: %s",
525 buf, errstr);
526 return (findeol());
528 return (NUMBER);
529 } else {
530 nodigits:
531 while (p > buf + 1)
532 lungetc(*--p);
533 c = *--p;
534 if (c == '-')
535 return (c);
539 #define allowed_in_string(x) \
540 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
541 x != '{' && x != '}' && \
542 x != '!' && x != '=' && x != '#' && \
543 x != ','))
545 if (isalnum(c) || c == ':' || c == '_') {
546 do {
547 *p++ = c;
548 if ((unsigned)(p-buf) >= sizeof(buf)) {
549 yyerror("string too long");
550 return (findeol());
552 c = lgetc(0);
553 } while (c != EOF && (allowed_in_string(c)));
554 lungetc(c);
555 *p = '\0';
556 token = lookup(buf);
557 if (token == STRING) {
558 yylval.v.string = strdup(buf);
559 if (yylval.v.string == NULL)
560 err(1, "%s", __func__);
562 return (token);
564 if (c == '\n') {
565 yylval.lineno = file->lineno;
566 file->lineno++;
568 if (c == EOF)
569 return (0);
570 return (c);
573 static const struct got_error*
574 newfile(struct file **nfile, const char *filename, int *fd)
576 const struct got_error* error = NULL;
578 (*nfile) = calloc(1, sizeof(struct file));
579 if ((*nfile) == NULL)
580 return got_error_from_errno("calloc");
581 (*nfile)->stream = fdopen(*fd, "r");
582 if ((*nfile)->stream == NULL) {
583 error = got_error_from_errno("fdopen");
584 free((*nfile));
585 return error;
587 *fd = -1; /* Stream owns the file descriptor now. */
588 (*nfile)->name = filename;
589 (*nfile)->lineno = 1;
590 (*nfile)->ungetsize = 16;
591 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
592 if ((*nfile)->ungetbuf == NULL) {
593 error = got_error_from_errno("malloc");
594 fclose((*nfile)->stream);
595 free((*nfile));
596 return error;
598 return NULL;
601 static const struct got_error*
602 new_remote(struct gotconfig_remote_repo **remote)
604 const struct got_error *error = NULL;
606 *remote = calloc(1, sizeof(**remote));
607 if (*remote == NULL)
608 error = got_error_from_errno("calloc");
609 return error;
612 static void
613 closefile(struct file *file)
615 fclose(file->stream);
616 free(file->ungetbuf);
617 free(file);
620 const struct got_error *
621 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
623 const struct got_error *err = NULL;
624 struct sym *sym, *next;
626 *conf = NULL;
628 err = newfile(&file, filename, fd);
629 if (err)
630 return err;
632 TAILQ_INIT(&gotconfig.remotes);
634 yyparse();
635 closefile(file);
637 /* Free macros and check which have not been used. */
638 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
639 if (!sym->persist) {
640 free(sym->nam);
641 free(sym->val);
642 TAILQ_REMOVE(&symhead, sym, entry);
643 free(sym);
647 if (gerror == NULL)
648 *conf = &gotconfig;
649 return gerror;
652 void
653 gotconfig_free(struct gotconfig *conf)
655 struct gotconfig_remote_repo *remote;
657 free(conf->author);
658 while (!TAILQ_EMPTY(&conf->remotes)) {
659 remote = TAILQ_FIRST(&conf->remotes);
660 TAILQ_REMOVE(&conf->remotes, remote, entry);
661 free(remote->name);
662 free(remote->repository);
663 free(remote->server);
664 free(remote->protocol);
665 free(remote);
669 int
670 symset(const char *nam, const char *val, int persist)
672 struct sym *sym;
674 TAILQ_FOREACH(sym, &symhead, entry) {
675 if (strcmp(nam, sym->nam) == 0)
676 break;
679 if (sym != NULL) {
680 if (sym->persist == 1)
681 return (0);
682 else {
683 free(sym->nam);
684 free(sym->val);
685 TAILQ_REMOVE(&symhead, sym, entry);
686 free(sym);
689 sym = calloc(1, sizeof(*sym));
690 if (sym == NULL)
691 return (-1);
693 sym->nam = strdup(nam);
694 if (sym->nam == NULL) {
695 free(sym);
696 return (-1);
698 sym->val = strdup(val);
699 if (sym->val == NULL) {
700 free(sym->nam);
701 free(sym);
702 return (-1);
704 sym->used = 0;
705 sym->persist = persist;
706 TAILQ_INSERT_TAIL(&symhead, sym, entry);
707 return (0);
710 int
711 cmdline_symset(char *s)
713 char *sym, *val;
714 int ret;
715 size_t len;
717 val = strrchr(s, '=');
718 if (val == NULL)
719 return (-1);
721 len = strlen(s) - strlen(val) + 1;
722 sym = malloc(len);
723 if (sym == NULL)
724 errx(1, "cmdline_symset: malloc");
726 strlcpy(sym, s, len);
728 ret = symset(sym, val + 1, 1);
729 free(sym);
731 return (ret);
734 char *
735 symget(const char *nam)
737 struct sym *sym;
739 TAILQ_FOREACH(sym, &symhead, entry) {
740 if (strcmp(nam, sym->nam) == 0) {
741 sym->used = 1;
742 return (sym->val);
745 return (NULL);
748 static int
749 atoul(char *s, u_long *ulvalp)
751 u_long ulval;
752 char *ep;
754 errno = 0;
755 ulval = strtoul(s, &ep, 0);
756 if (s[0] == '\0' || *ep != '\0')
757 return (-1);
758 if (errno == ERANGE && ulval == ULONG_MAX)
759 return (-1);
760 *ulvalp = ulval;
761 return (0);