Blob


1 /*
2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 *
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
23 %{
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
29 #include <netinet/in.h>
31 #include <arpa/inet.h>
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <event.h>
37 #include <ifaddrs.h>
38 #include <imsg.h>
39 #include <limits.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <unistd.h>
46 #include "got_error.h"
47 #include "gotweb.h"
49 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
50 static struct file {
51 TAILQ_ENTRY(file) entry;
52 FILE *stream;
53 char *name;
54 size_t ungetpos;
55 size_t ungetsize;
56 u_char *ungetbuf;
57 int eof_reached;
58 int lineno;
59 } *file, *topfile;
60 static const struct got_error* pushfile(struct file**, const char *);
61 int popfile(void);
62 int yyparse(void);
63 int yylex(void);
64 int yyerror(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
68 int lookup(char *);
69 int igetc(void);
70 int lgetc(int);
71 void lungetc(int);
72 int findeol(void);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 struct sym {
76 TAILQ_ENTRY(sym) entry;
77 int used;
78 int persist;
79 char *nam;
80 char *val;
81 };
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 const struct got_error* gerror = NULL;
87 struct gotweb_config *gw_conf;
89 typedef struct {
90 union {
91 int64_t number;
92 char *string;
93 } v;
94 int lineno;
95 } YYSTYPE;
97 %}
99 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
100 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
101 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
102 %token GOT_MAX_COMMITS_DISPLAY ON ERROR GOT_SHOW_SITE_OWNER
103 %token GOT_SHOW_REPO_CLONEURL
104 %token <v.string> STRING
105 %token <v.number> NUMBER
106 %type <v.number> boolean
107 %%
109 grammar : /* empty */
110 | grammar '\n'
111 | grammar main '\n'
114 boolean : STRING {
115 if (strcasecmp($1, "true") == 0 ||
116 strcasecmp($1, "yes") == 0)
117 $$ = 1;
118 else if (strcasecmp($1, "false") == 0 ||
119 strcasecmp($1, "off") == 0 ||
120 strcasecmp($1, "no") == 0)
121 $$ = 0;
122 else {
123 yyerror("invalid boolean value '%s'", $1);
124 free($1);
125 YYERROR;
127 free($1);
129 | ON { $$ = 1; }
131 main : GOT_REPOS_PATH STRING {
132 gw_conf->got_repos_path = strdup($2);
133 if (gw_conf->got_repos_path== NULL) {
134 free($2);
135 yyerror("strdup");
136 YYERROR;
138 free($2);
140 | GOT_MAX_REPOS NUMBER {
141 if ($2 > 0)
142 gw_conf->got_max_repos = $2;
144 | GOT_SITE_NAME STRING {
145 gw_conf->got_site_name = strdup($2);
146 if (gw_conf->got_site_name == NULL) {
147 free($2);
148 yyerror("strdup");
149 YYERROR;
151 free($2);
153 | GOT_SITE_OWNER STRING {
154 gw_conf->got_site_owner = strdup($2);
155 if (gw_conf->got_site_owner == NULL) {
156 free($2);
157 yyerror("strdup");
158 YYERROR;
160 free($2);
162 | GOT_SITE_LINK STRING {
163 gw_conf->got_site_link = strdup($2);
164 if (gw_conf->got_site_link == NULL) {
165 free($2);
166 yyerror("strdup");
167 YYERROR;
169 free($2);
171 | GOT_LOGO STRING {
172 gw_conf->got_logo = strdup($2);
173 if (gw_conf->got_logo== NULL) {
174 free($2);
175 yyerror("strdup");
176 YYERROR;
178 free($2);
180 | GOT_LOGO_URL STRING {
181 gw_conf->got_logo_url = strdup($2);
182 if (gw_conf->got_logo_url== NULL) {
183 free($2);
184 yyerror("strdup");
185 YYERROR;
187 free($2);
189 | GOT_SHOW_SITE_OWNER boolean {
190 gw_conf->got_show_site_owner = $2;
192 | GOT_SHOW_REPO_OWNER boolean {
193 gw_conf->got_show_repo_owner = $2;
195 | GOT_SHOW_REPO_AGE boolean {
196 gw_conf->got_show_repo_age = $2;
198 | GOT_SHOW_REPO_DESCRIPTION boolean {
199 gw_conf->got_show_repo_description = $2;
201 | GOT_SHOW_REPO_CLONEURL boolean {
202 gw_conf->got_show_repo_cloneurl = $2;
204 | GOT_MAX_REPOS_DISPLAY NUMBER {
205 if ($2 > 0)
206 gw_conf->got_max_repos_display = $2;
208 | GOT_MAX_COMMITS_DISPLAY NUMBER {
209 if ($2 > 0)
210 gw_conf->got_max_commits_display = $2;
213 %%
215 struct keywords {
216 const char *k_name;
217 int k_val;
218 };
220 int
221 yyerror(const char *fmt, ...)
223 va_list ap;
224 char *msg;
225 char *err = NULL;
227 va_start(ap, fmt);
228 if (vasprintf(&msg, fmt, ap) == -1) {
229 gerror = got_error_from_errno("vasprintf");
230 return 0;
232 va_end(ap);
233 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
234 gerror = got_error_from_errno("asprintf");
235 return(0);
237 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
238 free(msg);
239 free(err);
240 return(0);
243 int
244 kw_cmp(const void *k, const void *e)
246 return (strcmp(k, ((const struct keywords *)e)->k_name));
249 int
250 lookup(char *s)
252 /* This has to be sorted always. */
253 static const struct keywords keywords[] = {
254 { "got_logo", GOT_LOGO },
255 { "got_logo_url", GOT_LOGO_URL },
256 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
257 { "got_max_repos", GOT_MAX_REPOS },
258 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
259 { "got_repos_path", GOT_REPOS_PATH },
260 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
261 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
262 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
263 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
264 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
265 { "got_site_link", GOT_SITE_LINK },
266 { "got_site_name", GOT_SITE_NAME },
267 { "got_site_owner", GOT_SITE_OWNER },
268 };
269 const struct keywords *p;
271 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
272 sizeof(keywords[0]), kw_cmp);
274 if (p)
275 return (p->k_val);
276 else
277 return (STRING);
280 #define START_EXPAND 1
281 #define DONE_EXPAND 2
283 static int expanding;
285 int
286 igetc(void)
288 int c;
290 while (1) {
291 if (file->ungetpos > 0)
292 c = file->ungetbuf[--file->ungetpos];
293 else
294 c = getc(file->stream);
296 if (c == START_EXPAND)
297 expanding = 1;
298 else if (c == DONE_EXPAND)
299 expanding = 0;
300 else
301 break;
303 return (c);
306 int
307 lgetc(int quotec)
309 int c, next;
311 if (quotec) {
312 if ((c = igetc()) == EOF) {
313 yyerror("reached end of file while parsing "
314 "quoted string");
315 if (file == topfile || popfile() == EOF)
316 return (EOF);
317 return (quotec);
319 return (c);
322 while ((c = igetc()) == '\\') {
323 next = igetc();
324 if (next != '\n') {
325 c = next;
326 break;
328 yylval.lineno = file->lineno;
329 file->lineno++;
332 if (c == EOF) {
333 /*
334 * Fake EOL when hit EOF for the first time. This gets line
335 * count right if last line in included file is syntactically
336 * invalid and has no newline.
337 */
338 if (file->eof_reached == 0) {
339 file->eof_reached = 1;
340 return ('\n');
342 while (c == EOF) {
343 if (file == topfile || popfile() == EOF)
344 return (EOF);
345 c = igetc();
348 return (c);
351 void
352 lungetc(int c)
354 if (c == EOF)
355 return;
357 if (file->ungetpos >= file->ungetsize) {
358 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
359 if (p == NULL)
360 err(1, "%s", __func__);
361 file->ungetbuf = p;
362 file->ungetsize *= 2;
364 file->ungetbuf[file->ungetpos++] = c;
367 int
368 findeol(void)
370 int c;
372 /* Skip to either EOF or the first real EOL. */
373 while (1) {
374 c = lgetc(0);
375 if (c == '\n') {
376 file->lineno++;
377 break;
379 if (c == EOF)
380 break;
382 return (ERROR);
385 int
386 yylex(void)
388 unsigned char buf[8096];
389 unsigned char *p, *val;
390 int quotec, next, c;
391 int token;
393 top:
394 p = buf;
395 while ((c = lgetc(0)) == ' ' || c == '\t')
396 ; /* nothing */
398 yylval.lineno = file->lineno;
399 if (c == '#')
400 while ((c = lgetc(0)) != '\n' && c != EOF)
401 ; /* nothing */
402 if (c == '$' && !expanding) {
403 while (1) {
404 if ((c = lgetc(0)) == EOF)
405 return (0);
407 if (p + 1 >= buf + sizeof(buf) - 1) {
408 yyerror("string too long");
409 return (findeol());
411 if (isalnum(c) || c == '_') {
412 *p++ = c;
413 continue;
415 *p = '\0';
416 lungetc(c);
417 break;
419 val = symget(buf);
420 if (val == NULL) {
421 yyerror("macro '%s' not defined", buf);
422 return (findeol());
424 p = val + strlen(val) - 1;
425 lungetc(DONE_EXPAND);
426 while (p >= val) {
427 lungetc(*p);
428 p--;
430 lungetc(START_EXPAND);
431 goto top;
434 switch (c) {
435 case '\'':
436 case '"':
437 quotec = c;
438 while (1) {
439 if ((c = lgetc(quotec)) == EOF)
440 return (0);
441 if (c == '\n') {
442 file->lineno++;
443 continue;
444 } else if (c == '\\') {
445 if ((next = lgetc(quotec)) == EOF)
446 return (0);
447 if (next == quotec || c == ' ' || c == '\t')
448 c = next;
449 else if (next == '\n') {
450 file->lineno++;
451 continue;
452 } else
453 lungetc(next);
454 } else if (c == quotec) {
455 *p = '\0';
456 break;
457 } else if (c == '\0') {
458 yyerror("syntax error");
459 return (findeol());
461 if (p + 1 >= buf + sizeof(buf) - 1) {
462 yyerror("string too long");
463 return (findeol());
465 *p++ = c;
467 yylval.v.string = strdup(buf);
468 if (yylval.v.string == NULL)
469 err(1, "%s", __func__);
470 return (STRING);
473 #define allowed_to_end_number(x) \
474 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
476 if (c == '-' || isdigit(c)) {
477 do {
478 *p++ = c;
479 if ((unsigned)(p-buf) >= sizeof(buf)) {
480 yyerror("string too long");
481 return (findeol());
483 } while ((c = lgetc(0)) != EOF && isdigit(c));
484 lungetc(c);
485 if (p == buf + 1 && buf[0] == '-')
486 goto nodigits;
487 if (c == EOF || allowed_to_end_number(c)) {
488 const char *errstr = NULL;
490 *p = '\0';
491 yylval.v.number = strtonum(buf, LLONG_MIN,
492 LLONG_MAX, &errstr);
493 if (errstr) {
494 yyerror("\"%s\" invalid number: %s",
495 buf, errstr);
496 return (findeol());
498 return (NUMBER);
499 } else {
500 nodigits:
501 while (p > buf + 1)
502 lungetc(*--p);
503 c = *--p;
504 if (c == '-')
505 return (c);
509 #define allowed_in_string(x) \
510 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
511 x != '{' && x != '}' && \
512 x != '!' && x != '=' && x != '#' && \
513 x != ','))
515 if (isalnum(c) || c == ':' || c == '_') {
516 do {
517 *p++ = c;
518 if ((unsigned)(p-buf) >= sizeof(buf)) {
519 yyerror("string too long");
520 return (findeol());
522 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
523 lungetc(c);
524 *p = '\0';
525 if ((token = lookup(buf)) == STRING)
526 if ((yylval.v.string = strdup(buf)) == NULL)
527 err(1, "%s", __func__);
528 return (token);
530 if (c == '\n') {
531 yylval.lineno = file->lineno;
532 file->lineno++;
534 if (c == EOF)
535 return (0);
536 return (c);
539 static const struct got_error*
540 pushfile(struct file **nfile, const char *name)
542 const struct got_error* error = NULL;
544 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
545 return got_error_from_errno2(__func__, "calloc");
546 if (((*nfile)->name = strdup(name)) == NULL) {
547 free(nfile);
548 return got_error_from_errno2(__func__, "strdup");
550 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
551 char *msg = NULL;
552 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
553 return got_error_from_errno("asprintf");
554 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
555 free((*nfile)->name);
556 free((*nfile));
557 free(msg);
558 return error;
560 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
561 (*nfile)->ungetsize = 16;
562 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
563 if ((*nfile)->ungetbuf == NULL) {
564 fclose((*nfile)->stream);
565 free((*nfile)->name);
566 free((*nfile));
567 return got_error_from_errno2(__func__, "malloc");
569 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
570 return error;
573 int
574 popfile(void)
576 struct file *prev = NULL;
578 TAILQ_REMOVE(&files, file, entry);
579 fclose(file->stream);
580 free(file->name);
581 free(file->ungetbuf);
582 free(file);
583 file = prev;
584 return (file ? 0 : EOF);
587 const struct got_error*
588 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
590 gw_conf = malloc(sizeof(struct gotweb_config));
591 if (gw_conf == NULL) {
592 gerror = got_error_from_errno("malloc");
593 goto done;
595 gw_conf->got_repos_path = strdup(D_GOTPATH);
596 if (gw_conf->got_repos_path == NULL) {
597 gerror = got_error_from_errno("strdup");
598 goto done;
600 gw_conf->got_site_name = strdup(D_SITENAME);
601 if (gw_conf->got_site_name == NULL) {
602 gerror = got_error_from_errno("strdup");
603 goto done;
605 gw_conf->got_site_owner = strdup(D_SITEOWNER);
606 if (gw_conf->got_site_owner == NULL) {
607 gerror = got_error_from_errno("strdup");
608 goto done;
610 gw_conf->got_site_link = strdup(D_SITELINK);
611 if (gw_conf->got_site_link == NULL) {
612 gerror = got_error_from_errno("strdup");
613 goto done;
615 gw_conf->got_logo = strdup(D_GOTLOGO);
616 if (gw_conf->got_logo == NULL) {
617 gerror = got_error_from_errno("strdup");
618 goto done;
620 gw_conf->got_logo_url = strdup(D_GOTURL);
621 if (gw_conf->got_logo_url == NULL) {
622 gerror = got_error_from_errno("strdup");
623 goto done;
625 gw_conf->got_show_site_owner = D_SHOWSOWNER;
626 gw_conf->got_show_repo_owner = D_SHOWROWNER;
627 gw_conf->got_show_repo_age = D_SHOWAGE;
628 gw_conf->got_show_repo_description = D_SHOWDESC;
629 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
630 gw_conf->got_max_repos = D_MAXREPO;
631 gw_conf->got_max_repos_display = D_MAXREPODISP;
632 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
634 /*
635 * We don't require that the gotweb config file exists
636 * So reset gerror if it doesn't exist and goto done.
637 */
638 gerror = pushfile(&file, filename);
639 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
640 gerror = NULL;
641 goto done;
642 } else if (gerror)
643 return gerror;
644 topfile = file;
646 yyparse();
647 popfile();
648 done:
649 *gconf = gw_conf;
650 return gerror;
653 int
654 symset(const char *nam, const char *val, int persist)
656 struct sym *sym;
658 TAILQ_FOREACH(sym, &symhead, entry) {
659 if (strcmp(nam, sym->nam) == 0)
660 break;
663 if (sym != NULL) {
664 if (sym->persist == 1)
665 return (0);
666 else {
667 free(sym->nam);
668 free(sym->val);
669 TAILQ_REMOVE(&symhead, sym, entry);
670 free(sym);
673 if ((sym = calloc(1, sizeof(*sym))) == NULL)
674 return (-1);
676 sym->nam = strdup(nam);
677 if (sym->nam == NULL) {
678 free(sym);
679 return (-1);
681 sym->val = strdup(val);
682 if (sym->val == NULL) {
683 free(sym->nam);
684 free(sym);
685 return (-1);
687 sym->used = 0;
688 sym->persist = persist;
689 TAILQ_INSERT_TAIL(&symhead, sym, entry);
690 return (0);
693 int
694 cmdline_symset(char *s)
696 char *sym, *val;
697 int ret;
698 size_t len;
700 if ((val = strrchr(s, '=')) == NULL)
701 return (-1);
703 len = strlen(s) - strlen(val) + 1;
704 if ((sym = malloc(len)) == NULL)
705 errx(1, "cmdline_symset: malloc");
707 strlcpy(sym, s, len);
709 ret = symset(sym, val + 1, 1);
710 free(sym);
712 return (ret);
715 char *
716 symget(const char *nam)
718 struct sym *sym;
720 TAILQ_FOREACH(sym, &symhead, entry) {
721 if (strcmp(nam, sym->nam) == 0) {
722 sym->used = 1;
723 return (sym->val);
726 return (NULL);