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_WWW_PATH STRING {
141 gw_conf->got_www_path = strdup($2);
142 if (gw_conf->got_www_path == NULL) {
143 free($2);
144 yyerror("strdup");
145 YYERROR;
147 free($2);
149 | GOT_MAX_REPOS NUMBER {
150 if ($2 > 0)
151 gw_conf->got_max_repos = $2;
153 | GOT_SITE_NAME STRING {
154 gw_conf->got_site_name = strdup($2);
155 if (gw_conf->got_site_name == NULL) {
156 free($2);
157 yyerror("strdup");
158 YYERROR;
160 free($2);
162 | GOT_SITE_OWNER STRING {
163 gw_conf->got_site_owner = strdup($2);
164 if (gw_conf->got_site_owner == NULL) {
165 free($2);
166 yyerror("strdup");
167 YYERROR;
169 free($2);
171 | GOT_SITE_LINK STRING {
172 gw_conf->got_site_link = strdup($2);
173 if (gw_conf->got_site_link == NULL) {
174 free($2);
175 yyerror("strdup");
176 YYERROR;
178 free($2);
180 | GOT_LOGO STRING {
181 gw_conf->got_logo = strdup($2);
182 if (gw_conf->got_logo== NULL) {
183 free($2);
184 yyerror("strdup");
185 YYERROR;
187 free($2);
189 | GOT_LOGO_URL STRING {
190 gw_conf->got_logo_url = strdup($2);
191 if (gw_conf->got_logo_url== NULL) {
192 free($2);
193 yyerror("strdup");
194 YYERROR;
196 free($2);
198 | GOT_SHOW_SITE_OWNER boolean {
199 gw_conf->got_show_site_owner = $2;
201 | GOT_SHOW_REPO_OWNER boolean {
202 gw_conf->got_show_repo_owner = $2;
204 | GOT_SHOW_REPO_AGE boolean {
205 gw_conf->got_show_repo_age = $2;
207 | GOT_SHOW_REPO_DESCRIPTION boolean {
208 gw_conf->got_show_repo_description = $2;
210 | GOT_SHOW_REPO_CLONEURL boolean {
211 gw_conf->got_show_repo_cloneurl = $2;
213 | GOT_MAX_REPOS_DISPLAY NUMBER {
214 if ($2 > 0)
215 gw_conf->got_max_repos_display = $2;
217 | GOT_MAX_COMMITS_DISPLAY NUMBER {
218 if ($2 > 0)
219 gw_conf->got_max_commits_display = $2;
222 %%
224 struct keywords {
225 const char *k_name;
226 int k_val;
227 };
229 int
230 yyerror(const char *fmt, ...)
232 va_list ap;
233 char *msg;
234 char *err = NULL;
236 va_start(ap, fmt);
237 if (vasprintf(&msg, fmt, ap) == -1) {
238 gerror = got_error_from_errno("vasprintf");
239 return 0;
241 va_end(ap);
242 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
243 gerror = got_error_from_errno("asprintf");
244 return(0);
246 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
247 free(msg);
248 free(err);
249 return(0);
252 int
253 kw_cmp(const void *k, const void *e)
255 return (strcmp(k, ((const struct keywords *)e)->k_name));
258 int
259 lookup(char *s)
261 /* This has to be sorted always. */
262 static const struct keywords keywords[] = {
263 { "got_logo", GOT_LOGO },
264 { "got_logo_url", GOT_LOGO_URL },
265 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
266 { "got_max_repos", GOT_MAX_REPOS },
267 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
268 { "got_repos_path", GOT_REPOS_PATH },
269 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
270 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
271 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
272 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
273 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
274 { "got_site_link", GOT_SITE_LINK },
275 { "got_site_name", GOT_SITE_NAME },
276 { "got_site_owner", GOT_SITE_OWNER },
277 { "got_www_path", GOT_WWW_PATH },
278 };
279 const struct keywords *p;
281 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
282 sizeof(keywords[0]), kw_cmp);
284 if (p)
285 return (p->k_val);
286 else
287 return (STRING);
290 #define START_EXPAND 1
291 #define DONE_EXPAND 2
293 static int expanding;
295 int
296 igetc(void)
298 int c;
300 while (1) {
301 if (file->ungetpos > 0)
302 c = file->ungetbuf[--file->ungetpos];
303 else
304 c = getc(file->stream);
306 if (c == START_EXPAND)
307 expanding = 1;
308 else if (c == DONE_EXPAND)
309 expanding = 0;
310 else
311 break;
313 return (c);
316 int
317 lgetc(int quotec)
319 int c, next;
321 if (quotec) {
322 if ((c = igetc()) == EOF) {
323 yyerror("reached end of file while parsing "
324 "quoted string");
325 if (file == topfile || popfile() == EOF)
326 return (EOF);
327 return (quotec);
329 return (c);
332 while ((c = igetc()) == '\\') {
333 next = igetc();
334 if (next != '\n') {
335 c = next;
336 break;
338 yylval.lineno = file->lineno;
339 file->lineno++;
342 if (c == EOF) {
343 /*
344 * Fake EOL when hit EOF for the first time. This gets line
345 * count right if last line in included file is syntactically
346 * invalid and has no newline.
347 */
348 if (file->eof_reached == 0) {
349 file->eof_reached = 1;
350 return ('\n');
352 while (c == EOF) {
353 if (file == topfile || popfile() == EOF)
354 return (EOF);
355 c = igetc();
358 return (c);
361 void
362 lungetc(int c)
364 if (c == EOF)
365 return;
367 if (file->ungetpos >= file->ungetsize) {
368 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
369 if (p == NULL)
370 err(1, "%s", __func__);
371 file->ungetbuf = p;
372 file->ungetsize *= 2;
374 file->ungetbuf[file->ungetpos++] = c;
377 int
378 findeol(void)
380 int c;
382 /* Skip to either EOF or the first real EOL. */
383 while (1) {
384 c = lgetc(0);
385 if (c == '\n') {
386 file->lineno++;
387 break;
389 if (c == EOF)
390 break;
392 return (ERROR);
395 int
396 yylex(void)
398 unsigned char buf[8096];
399 unsigned char *p, *val;
400 int quotec, next, c;
401 int token;
403 top:
404 p = buf;
405 while ((c = lgetc(0)) == ' ' || c == '\t')
406 ; /* nothing */
408 yylval.lineno = file->lineno;
409 if (c == '#')
410 while ((c = lgetc(0)) != '\n' && c != EOF)
411 ; /* nothing */
412 if (c == '$' && !expanding) {
413 while (1) {
414 if ((c = lgetc(0)) == EOF)
415 return (0);
417 if (p + 1 >= buf + sizeof(buf) - 1) {
418 yyerror("string too long");
419 return (findeol());
421 if (isalnum(c) || c == '_') {
422 *p++ = c;
423 continue;
425 *p = '\0';
426 lungetc(c);
427 break;
429 val = symget(buf);
430 if (val == NULL) {
431 yyerror("macro '%s' not defined", buf);
432 return (findeol());
434 p = val + strlen(val) - 1;
435 lungetc(DONE_EXPAND);
436 while (p >= val) {
437 lungetc(*p);
438 p--;
440 lungetc(START_EXPAND);
441 goto top;
444 switch (c) {
445 case '\'':
446 case '"':
447 quotec = c;
448 while (1) {
449 if ((c = lgetc(quotec)) == EOF)
450 return (0);
451 if (c == '\n') {
452 file->lineno++;
453 continue;
454 } else if (c == '\\') {
455 if ((next = lgetc(quotec)) == EOF)
456 return (0);
457 if (next == quotec || c == ' ' || c == '\t')
458 c = next;
459 else if (next == '\n') {
460 file->lineno++;
461 continue;
462 } else
463 lungetc(next);
464 } else if (c == quotec) {
465 *p = '\0';
466 break;
467 } else if (c == '\0') {
468 yyerror("syntax error");
469 return (findeol());
471 if (p + 1 >= buf + sizeof(buf) - 1) {
472 yyerror("string too long");
473 return (findeol());
475 *p++ = c;
477 yylval.v.string = strdup(buf);
478 if (yylval.v.string == NULL)
479 err(1, "%s", __func__);
480 return (STRING);
483 #define allowed_to_end_number(x) \
484 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
486 if (c == '-' || isdigit(c)) {
487 do {
488 *p++ = c;
489 if ((unsigned)(p-buf) >= sizeof(buf)) {
490 yyerror("string too long");
491 return (findeol());
493 } while ((c = lgetc(0)) != EOF && isdigit(c));
494 lungetc(c);
495 if (p == buf + 1 && buf[0] == '-')
496 goto nodigits;
497 if (c == EOF || allowed_to_end_number(c)) {
498 const char *errstr = NULL;
500 *p = '\0';
501 yylval.v.number = strtonum(buf, LLONG_MIN,
502 LLONG_MAX, &errstr);
503 if (errstr) {
504 yyerror("\"%s\" invalid number: %s",
505 buf, errstr);
506 return (findeol());
508 return (NUMBER);
509 } else {
510 nodigits:
511 while (p > buf + 1)
512 lungetc(*--p);
513 c = *--p;
514 if (c == '-')
515 return (c);
519 #define allowed_in_string(x) \
520 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
521 x != '{' && x != '}' && \
522 x != '!' && x != '=' && x != '#' && \
523 x != ','))
525 if (isalnum(c) || c == ':' || c == '_') {
526 do {
527 *p++ = c;
528 if ((unsigned)(p-buf) >= sizeof(buf)) {
529 yyerror("string too long");
530 return (findeol());
532 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
533 lungetc(c);
534 *p = '\0';
535 if ((token = lookup(buf)) == STRING)
536 if ((yylval.v.string = strdup(buf)) == NULL)
537 err(1, "%s", __func__);
538 return (token);
540 if (c == '\n') {
541 yylval.lineno = file->lineno;
542 file->lineno++;
544 if (c == EOF)
545 return (0);
546 return (c);
549 static const struct got_error*
550 pushfile(struct file **nfile, const char *name)
552 const struct got_error* error = NULL;
554 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
555 return got_error_from_errno2(__func__, "calloc");
556 if (((*nfile)->name = strdup(name)) == NULL) {
557 free(nfile);
558 return got_error_from_errno2(__func__, "strdup");
560 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
561 char *msg = NULL;
562 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
563 return got_error_from_errno("asprintf");
564 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
565 free((*nfile)->name);
566 free((*nfile));
567 free(msg);
568 return error;
570 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
571 (*nfile)->ungetsize = 16;
572 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
573 if ((*nfile)->ungetbuf == NULL) {
574 fclose((*nfile)->stream);
575 free((*nfile)->name);
576 free((*nfile));
577 return got_error_from_errno2(__func__, "malloc");
579 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
580 return error;
583 int
584 popfile(void)
586 struct file *prev = NULL;
588 TAILQ_REMOVE(&files, file, entry);
589 fclose(file->stream);
590 free(file->name);
591 free(file->ungetbuf);
592 free(file);
593 file = prev;
594 return (file ? 0 : EOF);
597 const struct got_error*
598 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
600 gw_conf = malloc(sizeof(struct gotweb_config));
601 if (gw_conf == NULL) {
602 gerror = got_error_from_errno("malloc");
603 goto done;
605 gw_conf->got_repos_path = strdup(D_GOTPATH);
606 if (gw_conf->got_repos_path == NULL) {
607 gerror = got_error_from_errno("strdup");
608 goto done;
610 gw_conf->got_www_path = strdup(D_GOTWWW);
611 if (gw_conf->got_www_path == NULL) {
612 gerror = got_error_from_errno("strdup");
613 goto done;
615 gw_conf->got_site_name = strdup(D_SITENAME);
616 if (gw_conf->got_site_name == NULL) {
617 gerror = got_error_from_errno("strdup");
618 goto done;
620 gw_conf->got_site_owner = strdup(D_SITEOWNER);
621 if (gw_conf->got_site_owner == NULL) {
622 gerror = got_error_from_errno("strdup");
623 goto done;
625 gw_conf->got_site_link = strdup(D_SITELINK);
626 if (gw_conf->got_site_link == NULL) {
627 gerror = got_error_from_errno("strdup");
628 goto done;
630 gw_conf->got_logo = strdup(D_GOTLOGO);
631 if (gw_conf->got_logo == NULL) {
632 gerror = got_error_from_errno("strdup");
633 goto done;
635 gw_conf->got_logo_url = strdup(D_GOTURL);
636 if (gw_conf->got_logo_url == NULL) {
637 gerror = got_error_from_errno("strdup");
638 goto done;
640 gw_conf->got_show_site_owner = D_SHOWSOWNER;
641 gw_conf->got_show_repo_owner = D_SHOWROWNER;
642 gw_conf->got_show_repo_age = D_SHOWAGE;
643 gw_conf->got_show_repo_description = D_SHOWDESC;
644 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
645 gw_conf->got_max_repos = D_MAXREPO;
646 gw_conf->got_max_repos_display = D_MAXREPODISP;
647 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
649 /*
650 * We don't require that the gotweb config file exists
651 * So reset gerror if it doesn't exist and goto done.
652 */
653 gerror = pushfile(&file, filename);
654 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
655 gerror = NULL;
656 goto done;
657 } else if (gerror)
658 return gerror;
659 topfile = file;
661 yyparse();
662 popfile();
663 done:
664 *gconf = gw_conf;
665 return gerror;
668 int
669 symset(const char *nam, const char *val, int persist)
671 struct sym *sym;
673 TAILQ_FOREACH(sym, &symhead, entry) {
674 if (strcmp(nam, sym->nam) == 0)
675 break;
678 if (sym != NULL) {
679 if (sym->persist == 1)
680 return (0);
681 else {
682 free(sym->nam);
683 free(sym->val);
684 TAILQ_REMOVE(&symhead, sym, entry);
685 free(sym);
688 if ((sym = calloc(1, sizeof(*sym))) == NULL)
689 return (-1);
691 sym->nam = strdup(nam);
692 if (sym->nam == NULL) {
693 free(sym);
694 return (-1);
696 sym->val = strdup(val);
697 if (sym->val == NULL) {
698 free(sym->nam);
699 free(sym);
700 return (-1);
702 sym->used = 0;
703 sym->persist = persist;
704 TAILQ_INSERT_TAIL(&symhead, sym, entry);
705 return (0);
708 int
709 cmdline_symset(char *s)
711 char *sym, *val;
712 int ret;
713 size_t len;
715 if ((val = strrchr(s, '=')) == NULL)
716 return (-1);
718 len = strlen(s) - strlen(val) + 1;
719 if ((sym = malloc(len)) == NULL)
720 errx(1, "cmdline_symset: malloc");
722 strlcpy(sym, s, len);
724 ret = symset(sym, val + 1, 1);
725 free(sym);
727 return (ret);
730 char *
731 symget(const char *nam)
733 struct sym *sym;
735 TAILQ_FOREACH(sym, &symhead, entry) {
736 if (strcmp(nam, sym->nam) == 0) {
737 sym->used = 1;
738 return (sym->val);
741 return (NULL);