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;
139 | GOT_MAX_REPOS NUMBER {
140 if ($2 > 0)
141 gw_conf->got_max_repos = $2;
143 | GOT_SITE_NAME STRING {
144 gw_conf->got_site_name = strdup($2);
145 if (gw_conf->got_site_name == NULL) {
146 free($2);
147 yyerror("strdup");
148 YYERROR;
151 | GOT_SITE_OWNER STRING {
152 gw_conf->got_site_owner = strdup($2);
153 if (gw_conf->got_site_owner == NULL) {
154 free($2);
155 yyerror("strdup");
156 YYERROR;
159 | GOT_SITE_LINK STRING {
160 gw_conf->got_site_link = strdup($2);
161 if (gw_conf->got_site_link == NULL) {
162 free($2);
163 yyerror("strdup");
164 YYERROR;
167 | GOT_LOGO STRING {
168 gw_conf->got_logo = strdup($2);
169 if (gw_conf->got_logo== NULL) {
170 free($2);
171 yyerror("strdup");
172 YYERROR;
175 | GOT_LOGO_URL STRING {
176 gw_conf->got_logo_url = strdup($2);
177 if (gw_conf->got_logo_url== NULL) {
178 free($2);
179 yyerror("strdup");
180 YYERROR;
183 | GOT_SHOW_SITE_OWNER boolean {
184 gw_conf->got_show_site_owner = $2;
186 | GOT_SHOW_REPO_OWNER boolean {
187 gw_conf->got_show_repo_owner = $2;
189 | GOT_SHOW_REPO_AGE boolean {
190 gw_conf->got_show_repo_age = $2;
192 | GOT_SHOW_REPO_DESCRIPTION boolean {
193 gw_conf->got_show_repo_description = $2;
195 | GOT_SHOW_REPO_CLONEURL boolean {
196 gw_conf->got_show_repo_cloneurl = $2;
198 | GOT_MAX_REPOS_DISPLAY NUMBER {
199 if ($2 > 0)
200 gw_conf->got_max_repos_display = $2;
202 | GOT_MAX_COMMITS_DISPLAY NUMBER {
203 if ($2 > 0)
204 gw_conf->got_max_commits_display = $2;
207 %%
209 struct keywords {
210 const char *k_name;
211 int k_val;
212 };
214 int
215 yyerror(const char *fmt, ...)
217 va_list ap;
218 char *msg;
219 char *err = NULL;
221 va_start(ap, fmt);
222 if (vasprintf(&msg, fmt, ap) == -1) {
223 gerror = got_error_from_errno("vasprintf");
224 return 0;
226 va_end(ap);
227 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
228 gerror = got_error_from_errno("asprintf");
229 return(0);
231 gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
232 free(msg);
233 free(err);
234 return(0);
237 int
238 kw_cmp(const void *k, const void *e)
240 return (strcmp(k, ((const struct keywords *)e)->k_name));
243 int
244 lookup(char *s)
246 /* This has to be sorted always. */
247 static const struct keywords keywords[] = {
248 { "got_logo", GOT_LOGO },
249 { "got_logo_url", GOT_LOGO_URL },
250 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
251 { "got_max_repos", GOT_MAX_REPOS },
252 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
253 { "got_repos_path", GOT_REPOS_PATH },
254 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
255 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
256 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
257 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
258 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
259 { "got_site_link", GOT_SITE_LINK },
260 { "got_site_name", GOT_SITE_NAME },
261 { "got_site_owner", GOT_SITE_OWNER },
262 };
263 const struct keywords *p;
265 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
266 sizeof(keywords[0]), kw_cmp);
268 if (p)
269 return (p->k_val);
270 else
271 return (STRING);
274 #define START_EXPAND 1
275 #define DONE_EXPAND 2
277 static int expanding;
279 int
280 igetc(void)
282 int c;
284 while (1) {
285 if (file->ungetpos > 0)
286 c = file->ungetbuf[--file->ungetpos];
287 else
288 c = getc(file->stream);
290 if (c == START_EXPAND)
291 expanding = 1;
292 else if (c == DONE_EXPAND)
293 expanding = 0;
294 else
295 break;
297 return (c);
300 int
301 lgetc(int quotec)
303 int c, next;
305 if (quotec) {
306 if ((c = igetc()) == EOF) {
307 yyerror("reached end of file while parsing "
308 "quoted string");
309 if (file == topfile || popfile() == EOF)
310 return (EOF);
311 return (quotec);
313 return (c);
316 while ((c = igetc()) == '\\') {
317 next = igetc();
318 if (next != '\n') {
319 c = next;
320 break;
322 yylval.lineno = file->lineno;
323 file->lineno++;
326 if (c == EOF) {
327 /*
328 * Fake EOL when hit EOF for the first time. This gets line
329 * count right if last line in included file is syntactically
330 * invalid and has no newline.
331 */
332 if (file->eof_reached == 0) {
333 file->eof_reached = 1;
334 return ('\n');
336 while (c == EOF) {
337 if (file == topfile || popfile() == EOF)
338 return (EOF);
339 c = igetc();
342 return (c);
345 void
346 lungetc(int c)
348 if (c == EOF)
349 return;
351 if (file->ungetpos >= file->ungetsize) {
352 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
353 if (p == NULL)
354 err(1, "%s", __func__);
355 file->ungetbuf = p;
356 file->ungetsize *= 2;
358 file->ungetbuf[file->ungetpos++] = c;
361 int
362 findeol(void)
364 int c;
366 /* Skip to either EOF or the first real EOL. */
367 while (1) {
368 c = lgetc(0);
369 if (c == '\n') {
370 file->lineno++;
371 break;
373 if (c == EOF)
374 break;
376 return (ERROR);
379 int
380 yylex(void)
382 unsigned char buf[8096];
383 unsigned char *p, *val;
384 int quotec, next, c;
385 int token;
387 top:
388 p = buf;
389 while ((c = lgetc(0)) == ' ' || c == '\t')
390 ; /* nothing */
392 yylval.lineno = file->lineno;
393 if (c == '#')
394 while ((c = lgetc(0)) != '\n' && c != EOF)
395 ; /* nothing */
396 if (c == '$' && !expanding) {
397 while (1) {
398 if ((c = lgetc(0)) == EOF)
399 return (0);
401 if (p + 1 >= buf + sizeof(buf) - 1) {
402 yyerror("string too long");
403 return (findeol());
405 if (isalnum(c) || c == '_') {
406 *p++ = c;
407 continue;
409 *p = '\0';
410 lungetc(c);
411 break;
413 val = symget(buf);
414 if (val == NULL) {
415 yyerror("macro '%s' not defined", buf);
416 return (findeol());
418 p = val + strlen(val) - 1;
419 lungetc(DONE_EXPAND);
420 while (p >= val) {
421 lungetc(*p);
422 p--;
424 lungetc(START_EXPAND);
425 goto top;
428 switch (c) {
429 case '\'':
430 case '"':
431 quotec = c;
432 while (1) {
433 if ((c = lgetc(quotec)) == EOF)
434 return (0);
435 if (c == '\n') {
436 file->lineno++;
437 continue;
438 } else if (c == '\\') {
439 if ((next = lgetc(quotec)) == EOF)
440 return (0);
441 if (next == quotec || c == ' ' || c == '\t')
442 c = next;
443 else if (next == '\n') {
444 file->lineno++;
445 continue;
446 } else
447 lungetc(next);
448 } else if (c == quotec) {
449 *p = '\0';
450 break;
451 } else if (c == '\0') {
452 yyerror("syntax error");
453 return (findeol());
455 if (p + 1 >= buf + sizeof(buf) - 1) {
456 yyerror("string too long");
457 return (findeol());
459 *p++ = c;
461 yylval.v.string = strdup(buf);
462 if (yylval.v.string == NULL)
463 err(1, "%s", __func__);
464 return (STRING);
467 #define allowed_to_end_number(x) \
468 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
470 if (c == '-' || isdigit(c)) {
471 do {
472 *p++ = c;
473 if ((unsigned)(p-buf) >= sizeof(buf)) {
474 yyerror("string too long");
475 return (findeol());
477 } while ((c = lgetc(0)) != EOF && isdigit(c));
478 lungetc(c);
479 if (p == buf + 1 && buf[0] == '-')
480 goto nodigits;
481 if (c == EOF || allowed_to_end_number(c)) {
482 const char *errstr = NULL;
484 *p = '\0';
485 yylval.v.number = strtonum(buf, LLONG_MIN,
486 LLONG_MAX, &errstr);
487 if (errstr) {
488 yyerror("\"%s\" invalid number: %s",
489 buf, errstr);
490 return (findeol());
492 return (NUMBER);
493 } else {
494 nodigits:
495 while (p > buf + 1)
496 lungetc(*--p);
497 c = *--p;
498 if (c == '-')
499 return (c);
503 #define allowed_in_string(x) \
504 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
505 x != '{' && x != '}' && \
506 x != '!' && x != '=' && x != '#' && \
507 x != ','))
509 if (isalnum(c) || c == ':' || c == '_') {
510 do {
511 *p++ = c;
512 if ((unsigned)(p-buf) >= sizeof(buf)) {
513 yyerror("string too long");
514 return (findeol());
516 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
517 lungetc(c);
518 *p = '\0';
519 if ((token = lookup(buf)) == STRING)
520 if ((yylval.v.string = strdup(buf)) == NULL)
521 err(1, "%s", __func__);
522 return (token);
524 if (c == '\n') {
525 yylval.lineno = file->lineno;
526 file->lineno++;
528 if (c == EOF)
529 return (0);
530 return (c);
533 static const struct got_error*
534 pushfile(struct file **nfile, const char *name)
536 const struct got_error* error = NULL;
538 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
539 return got_error_from_errno2(__func__, "calloc");
540 if (((*nfile)->name = strdup(name)) == NULL) {
541 free(nfile);
542 return got_error_from_errno2(__func__, "strdup");
544 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
545 char *msg = NULL;
546 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
547 return got_error_from_errno("asprintf");
548 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
549 free((*nfile)->name);
550 free((*nfile));
551 free(msg);
552 return error;
554 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
555 (*nfile)->ungetsize = 16;
556 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
557 if ((*nfile)->ungetbuf == NULL) {
558 fclose((*nfile)->stream);
559 free((*nfile)->name);
560 free((*nfile));
561 return got_error_from_errno2(__func__, "malloc");
563 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
564 return error;
567 int
568 popfile(void)
570 struct file *prev = NULL;
572 TAILQ_REMOVE(&files, file, entry);
573 fclose(file->stream);
574 free(file->name);
575 free(file->ungetbuf);
576 free(file);
577 file = prev;
578 return (file ? 0 : EOF);
581 const struct got_error*
582 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
584 gw_conf = malloc(sizeof(struct gotweb_config));
585 if (gw_conf == NULL) {
586 gerror = got_error_from_errno("malloc");
587 goto done;
589 gw_conf->got_repos_path = strdup(D_GOTPATH);
590 if (gw_conf->got_repos_path == NULL) {
591 gerror = got_error_from_errno("strdup");
592 goto done;
594 gw_conf->got_site_name = strdup(D_SITENAME);
595 if (gw_conf->got_site_name == NULL) {
596 gerror = got_error_from_errno("strdup");
597 goto done;
599 gw_conf->got_site_owner = strdup(D_SITEOWNER);
600 if (gw_conf->got_site_owner == NULL) {
601 gerror = got_error_from_errno("strdup");
602 goto done;
604 gw_conf->got_site_link = strdup(D_SITELINK);
605 if (gw_conf->got_site_link == NULL) {
606 gerror = got_error_from_errno("strdup");
607 goto done;
609 gw_conf->got_logo = strdup(D_GOTLOGO);
610 if (gw_conf->got_logo == NULL) {
611 gerror = got_error_from_errno("strdup");
612 goto done;
614 gw_conf->got_logo_url = strdup(D_GOTURL);
615 if (gw_conf->got_logo_url == NULL) {
616 gerror = got_error_from_errno("strdup");
617 goto done;
619 gw_conf->got_show_site_owner = D_SHOWSOWNER;
620 gw_conf->got_show_repo_owner = D_SHOWROWNER;
621 gw_conf->got_show_repo_age = D_SHOWAGE;
622 gw_conf->got_show_repo_description = D_SHOWDESC;
623 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
624 gw_conf->got_max_repos = D_MAXREPO;
625 gw_conf->got_max_repos_display = D_MAXREPODISP;
626 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
628 /*
629 * We don't require that the gotweb config file exists
630 * So reset gerror if it doesn't exist and goto done.
631 */
632 gerror = pushfile(&file, filename);
633 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
634 gerror = NULL;
635 goto done;
636 } else if (gerror)
637 return gerror;
638 topfile = file;
640 yyparse();
641 popfile();
642 done:
643 *gconf = gw_conf;
644 return gerror;
647 int
648 symset(const char *nam, const char *val, int persist)
650 struct sym *sym;
652 TAILQ_FOREACH(sym, &symhead, entry) {
653 if (strcmp(nam, sym->nam) == 0)
654 break;
657 if (sym != NULL) {
658 if (sym->persist == 1)
659 return (0);
660 else {
661 free(sym->nam);
662 free(sym->val);
663 TAILQ_REMOVE(&symhead, sym, entry);
664 free(sym);
667 if ((sym = calloc(1, sizeof(*sym))) == NULL)
668 return (-1);
670 sym->nam = strdup(nam);
671 if (sym->nam == NULL) {
672 free(sym);
673 return (-1);
675 sym->val = strdup(val);
676 if (sym->val == NULL) {
677 free(sym->nam);
678 free(sym);
679 return (-1);
681 sym->used = 0;
682 sym->persist = persist;
683 TAILQ_INSERT_TAIL(&symhead, sym, entry);
684 return (0);
687 int
688 cmdline_symset(char *s)
690 char *sym, *val;
691 int ret;
692 size_t len;
694 if ((val = strrchr(s, '=')) == NULL)
695 return (-1);
697 len = strlen(s) - strlen(val) + 1;
698 if ((sym = malloc(len)) == NULL)
699 errx(1, "cmdline_symset: malloc");
701 strlcpy(sym, s, len);
703 ret = symset(sym, val + 1, 1);
704 free(sym);
706 return (ret);
709 char *
710 symget(const char *nam)
712 struct sym *sym;
714 TAILQ_FOREACH(sym, &symhead, entry) {
715 if (strcmp(nam, sym->nam) == 0) {
716 sym->used = 1;
717 return (sym->val);
720 return (NULL);