Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
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/time.h>
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <event.h>
34 #include <imsg.h>
35 #include <limits.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
43 #include "got_error.h"
44 #include "got_path.h"
46 #include "log.h"
47 #include "gotd.h"
48 #include "auth.h"
49 #include "listen.h"
51 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
52 static struct file {
53 TAILQ_ENTRY(file) entry;
54 FILE *stream;
55 char *name;
56 int lineno;
57 int errors;
58 } *file;
59 struct file *newfile(const char *, int);
60 static void closefile(struct file *);
61 int check_file_secrecy(int, const char *);
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 lgetc(int);
70 int lungetc(int);
71 int findeol(void);
73 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
74 struct sym {
75 TAILQ_ENTRY(sym) entry;
76 int used;
77 int persist;
78 char *nam;
79 char *val;
80 };
82 int symset(const char *, const char *, int);
83 char *symget(const char *);
85 static int errors;
87 static struct gotd *gotd;
88 static struct gotd_repo *new_repo;
89 static int conf_limit_user_connections(const char *, int);
90 static struct gotd_repo *conf_new_repo(const char *);
91 static void conf_new_access_rule(struct gotd_repo *,
92 enum gotd_access, int, char *);
93 static enum gotd_procid gotd_proc_id;
95 typedef struct {
96 union {
97 long long number;
98 char *string;
99 struct timeval tv;
100 } v;
101 int lineno;
102 } YYSTYPE;
104 %}
106 %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
107 %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
109 %token <v.string> STRING
110 %token <v.number> NUMBER
111 %type <v.tv> timeout
113 %%
115 grammar :
116 | grammar '\n'
117 | grammar main '\n'
118 | grammar repository '\n'
121 timeout : NUMBER {
122 if ($1 < 0) {
123 yyerror("invalid timeout: %lld", $1);
124 YYERROR;
126 $$.tv_sec = $1;
127 $$.tv_usec = 0;
129 | STRING {
130 const char *errstr;
131 const char *type = "seconds";
132 size_t len;
133 int mul = 1;
135 if (*$1 == '\0') {
136 yyerror("invalid number of seconds: %s", $1);
137 free($1);
138 YYERROR;
141 len = strlen($1);
142 switch ($1[len - 1]) {
143 case 'S':
144 case 's':
145 $1[len - 1] = '\0';
146 break;
147 case 'M':
148 case 'm':
149 type = "minutes";
150 mul = 60;
151 $1[len - 1] = '\0';
152 break;
153 case 'H':
154 case 'h':
155 type = "hours";
156 mul = 60 * 60;
157 $1[len - 1] = '\0';
158 break;
161 $$.tv_usec = 0;
162 $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
163 if (errstr) {
164 yyerror("number of %s is %s: %s", type,
165 errstr, $1);
166 free($1);
167 YYERROR;
170 $$.tv_sec *= mul;
171 free($1);
175 main : LISTEN ON STRING {
176 if (!got_path_is_absolute($3))
177 yyerror("bad unix socket path \"%s\": "
178 "must be an absolute path", $3);
180 if (gotd_proc_id == PROC_LISTEN) {
181 if (strlcpy(gotd->unix_socket_path, $3,
182 sizeof(gotd->unix_socket_path)) >=
183 sizeof(gotd->unix_socket_path)) {
184 yyerror("%s: unix socket path too long",
185 __func__);
186 free($3);
187 YYERROR;
190 free($3);
192 | USER STRING {
193 if (strlcpy(gotd->user_name, $2,
194 sizeof(gotd->user_name)) >=
195 sizeof(gotd->user_name)) {
196 yyerror("%s: user name too long", __func__);
197 free($2);
198 YYERROR;
200 free($2);
202 | connection
205 connection : CONNECTION '{' optnl conflags_l '}'
206 | CONNECTION conflags
208 conflags_l : conflags optnl conflags_l
209 | conflags optnl
212 conflags : REQUEST TIMEOUT timeout {
213 if ($3.tv_sec <= 0) {
214 yyerror("invalid timeout: %lld", $3.tv_sec);
215 YYERROR;
217 memcpy(&gotd->request_timeout, &$3,
218 sizeof(gotd->request_timeout));
220 | LIMIT USER STRING NUMBER {
221 if (gotd_proc_id == PROC_LISTEN &&
222 conf_limit_user_connections($3, $4) == -1) {
223 free($3);
224 YYERROR;
226 free($3);
230 repository : REPOSITORY STRING {
231 struct gotd_repo *repo;
233 TAILQ_FOREACH(repo, &gotd->repos, entry) {
234 if (strcmp(repo->name, $2) == 0) {
235 yyerror("duplicate repository '%s'", $2);
236 free($2);
237 YYERROR;
241 if (gotd_proc_id == PROC_GOTD ||
242 gotd_proc_id == PROC_AUTH) {
243 new_repo = conf_new_repo($2);
245 free($2);
246 } '{' optnl repoopts2 '}' {
250 repoopts1 : PATH STRING {
251 if (gotd_proc_id == PROC_GOTD ||
252 gotd_proc_id == PROC_AUTH) {
253 if (!got_path_is_absolute($2)) {
254 yyerror("%s: path %s is not absolute",
255 __func__, $2);
256 free($2);
257 YYERROR;
259 if (realpath($2, new_repo->path) == NULL) {
260 yyerror("realpath %s: %s", $2, strerror(errno));
261 free($2);
262 YYERROR;
265 free($2);
267 | PERMIT RO STRING {
268 if (gotd_proc_id == PROC_AUTH) {
269 conf_new_access_rule(new_repo,
270 GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
273 | PERMIT RW STRING {
274 if (gotd_proc_id == PROC_AUTH) {
275 conf_new_access_rule(new_repo,
276 GOTD_ACCESS_PERMITTED,
277 GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
280 | DENY STRING {
281 if (gotd_proc_id == PROC_AUTH) {
282 conf_new_access_rule(new_repo,
283 GOTD_ACCESS_DENIED, 0, $2);
288 repoopts2 : repoopts2 repoopts1 nl
289 | repoopts1 optnl
292 nl : '\n' optnl
295 optnl : '\n' optnl /* zero or more newlines */
296 | /* empty */
299 %%
301 struct keywords {
302 const char *k_name;
303 int k_val;
304 };
306 int
307 yyerror(const char *fmt, ...)
309 va_list ap;
310 char *msg;
312 file->errors++;
313 va_start(ap, fmt);
314 if (vasprintf(&msg, fmt, ap) == -1)
315 fatalx("yyerror vasprintf");
316 va_end(ap);
317 logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
318 free(msg);
319 return (0);
322 int
323 kw_cmp(const void *k, const void *e)
325 return (strcmp(k, ((const struct keywords *)e)->k_name));
328 int
329 lookup(char *s)
331 /* This has to be sorted always. */
332 static const struct keywords keywords[] = {
333 { "connection", CONNECTION },
334 { "deny", DENY },
335 { "limit", LIMIT },
336 { "listen", LISTEN },
337 { "on", ON },
338 { "path", PATH },
339 { "permit", PERMIT },
340 { "repository", REPOSITORY },
341 { "request", REQUEST },
342 { "ro", RO },
343 { "rw", RW },
344 { "timeout", TIMEOUT },
345 { "user", USER },
346 };
347 const struct keywords *p;
349 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
350 sizeof(keywords[0]), kw_cmp);
352 if (p)
353 return (p->k_val);
354 else
355 return (STRING);
358 #define MAXPUSHBACK 128
360 unsigned char *parsebuf;
361 int parseindex;
362 unsigned char pushback_buffer[MAXPUSHBACK];
363 int pushback_index = 0;
365 int
366 lgetc(int quotec)
368 int c, next;
370 if (parsebuf) {
371 /* Read character from the parsebuffer instead of input. */
372 if (parseindex >= 0) {
373 c = parsebuf[parseindex++];
374 if (c != '\0')
375 return (c);
376 parsebuf = NULL;
377 } else
378 parseindex++;
381 if (pushback_index)
382 return (pushback_buffer[--pushback_index]);
384 if (quotec) {
385 c = getc(file->stream);
386 if (c == EOF)
387 yyerror("reached end of file while parsing "
388 "quoted string");
389 return (c);
392 c = getc(file->stream);
393 while (c == '\\') {
394 next = getc(file->stream);
395 if (next != '\n') {
396 c = next;
397 break;
399 yylval.lineno = file->lineno;
400 file->lineno++;
401 c = getc(file->stream);
404 return (c);
407 int
408 lungetc(int c)
410 if (c == EOF)
411 return (EOF);
412 if (parsebuf) {
413 parseindex--;
414 if (parseindex >= 0)
415 return (c);
417 if (pushback_index < MAXPUSHBACK-1)
418 return (pushback_buffer[pushback_index++] = c);
419 else
420 return (EOF);
423 int
424 findeol(void)
426 int c;
428 parsebuf = NULL;
430 /* Skip to either EOF or the first real EOL. */
431 while (1) {
432 if (pushback_index)
433 c = pushback_buffer[--pushback_index];
434 else
435 c = lgetc(0);
436 if (c == '\n') {
437 file->lineno++;
438 break;
440 if (c == EOF)
441 break;
443 return (ERROR);
446 int
447 yylex(void)
449 unsigned char buf[8096];
450 unsigned char *p, *val;
451 int quotec, next, c;
452 int token;
454 top:
455 p = buf;
456 c = lgetc(0);
457 while (c == ' ' || c == '\t')
458 c = lgetc(0); /* nothing */
460 yylval.lineno = file->lineno;
461 if (c == '#') {
462 c = lgetc(0);
463 while (c != '\n' && c != EOF)
464 c = lgetc(0); /* nothing */
466 if (c == '$' && parsebuf == NULL) {
467 while (1) {
468 c = lgetc(0);
469 if (c == EOF)
470 return (0);
472 if (p + 1 >= buf + sizeof(buf) - 1) {
473 yyerror("string too long");
474 return (findeol());
476 if (isalnum(c) || c == '_') {
477 *p++ = c;
478 continue;
480 *p = '\0';
481 lungetc(c);
482 break;
484 val = symget(buf);
485 if (val == NULL) {
486 yyerror("macro '%s' not defined", buf);
487 return (findeol());
489 parsebuf = val;
490 parseindex = 0;
491 goto top;
494 switch (c) {
495 case '\'':
496 case '"':
497 quotec = c;
498 while (1) {
499 c = lgetc(quotec);
500 if (c == EOF)
501 return (0);
502 if (c == '\n') {
503 file->lineno++;
504 continue;
505 } else if (c == '\\') {
506 next = lgetc(quotec);
507 if (next == EOF)
508 return (0);
509 if (next == quotec || c == ' ' || c == '\t')
510 c = next;
511 else if (next == '\n') {
512 file->lineno++;
513 continue;
514 } else
515 lungetc(next);
516 } else if (c == quotec) {
517 *p = '\0';
518 break;
519 } else if (c == '\0') {
520 yyerror("syntax error");
521 return (findeol());
523 if (p + 1 >= buf + sizeof(buf) - 1) {
524 yyerror("string too long");
525 return (findeol());
527 *p++ = c;
529 yylval.v.string = strdup(buf);
530 if (yylval.v.string == NULL)
531 err(1, "yylex: strdup");
532 return (STRING);
535 #define allowed_to_end_number(x) \
536 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
538 if (c == '-' || isdigit(c)) {
539 do {
540 *p++ = c;
541 if ((unsigned)(p-buf) >= sizeof(buf)) {
542 yyerror("string too long");
543 return (findeol());
545 c = lgetc(0);
546 } while (c != EOF && isdigit(c));
547 lungetc(c);
548 if (p == buf + 1 && buf[0] == '-')
549 goto nodigits;
550 if (c == EOF || allowed_to_end_number(c)) {
551 const char *errstr = NULL;
553 *p = '\0';
554 yylval.v.number = strtonum(buf, LLONG_MIN,
555 LLONG_MAX, &errstr);
556 if (errstr) {
557 yyerror("\"%s\" invalid number: %s",
558 buf, errstr);
559 return (findeol());
561 return (NUMBER);
562 } else {
563 nodigits:
564 while (p > buf + 1)
565 lungetc(*--p);
566 c = *--p;
567 if (c == '-')
568 return (c);
572 #define allowed_in_string(x) \
573 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
574 x != '{' && x != '}' && \
575 x != '!' && x != '=' && x != '#' && \
576 x != ','))
578 if (isalnum(c) || c == ':' || c == '_') {
579 do {
580 *p++ = c;
581 if ((unsigned)(p-buf) >= sizeof(buf)) {
582 yyerror("string too long");
583 return (findeol());
585 c = lgetc(0);
586 } while (c != EOF && (allowed_in_string(c)));
587 lungetc(c);
588 *p = '\0';
589 token = lookup(buf);
590 if (token == STRING) {
591 yylval.v.string = strdup(buf);
592 if (yylval.v.string == NULL)
593 err(1, "yylex: strdup");
595 return (token);
597 if (c == '\n') {
598 yylval.lineno = file->lineno;
599 file->lineno++;
601 if (c == EOF)
602 return (0);
603 return (c);
606 int
607 check_file_secrecy(int fd, const char *fname)
609 struct stat st;
611 if (fstat(fd, &st)) {
612 log_warn("cannot stat %s", fname);
613 return (-1);
615 if (st.st_uid != 0 && st.st_uid != getuid()) {
616 log_warnx("%s: owner not root or current user", fname);
617 return (-1);
619 if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
620 log_warnx("%s: group writable or world read/writable", fname);
621 return (-1);
623 return (0);
626 struct file *
627 newfile(const char *name, int secret)
629 struct file *nfile;
631 nfile = calloc(1, sizeof(struct file));
632 if (nfile == NULL) {
633 log_warn("calloc");
634 return (NULL);
636 nfile->name = strdup(name);
637 if (nfile->name == NULL) {
638 log_warn("strdup");
639 free(nfile);
640 return (NULL);
642 nfile->stream = fopen(nfile->name, "r");
643 if (nfile->stream == NULL) {
644 log_warn("open %s", nfile->name);
645 free(nfile->name);
646 free(nfile);
647 return (NULL);
648 } else if (secret &&
649 check_file_secrecy(fileno(nfile->stream), nfile->name)) {
650 fclose(nfile->stream);
651 free(nfile->name);
652 free(nfile);
653 return (NULL);
655 nfile->lineno = 1;
656 return (nfile);
659 static void
660 closefile(struct file *xfile)
662 fclose(xfile->stream);
663 free(xfile->name);
664 free(xfile);
667 int
668 parse_config(const char *filename, enum gotd_procid proc_id,
669 struct gotd *env)
671 struct sym *sym, *next;
672 struct gotd_repo *repo;
674 memset(env, 0, sizeof(*env));
676 gotd = env;
677 gotd_proc_id = proc_id;
678 TAILQ_INIT(&gotd->repos);
680 /* Apply default values. */
681 if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
682 sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
683 fprintf(stderr, "%s: unix socket path too long", __func__);
684 return -1;
686 if (strlcpy(gotd->user_name, GOTD_USER,
687 sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
688 fprintf(stderr, "%s: user name too long", __func__);
689 return -1;
692 gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
693 gotd->request_timeout.tv_usec = 0;
695 file = newfile(filename, 0);
696 if (file == NULL)
697 return -1;
699 yyparse();
700 errors = file->errors;
701 closefile(file);
703 /* Free macros and check which have not been used. */
704 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
705 if ((gotd->verbosity > 1) && !sym->used)
706 fprintf(stderr, "warning: macro '%s' not used\n",
707 sym->nam);
708 if (!sym->persist) {
709 free(sym->nam);
710 free(sym->val);
711 TAILQ_REMOVE(&symhead, sym, entry);
712 free(sym);
716 if (errors)
717 return (-1);
719 TAILQ_FOREACH(repo, &gotd->repos, entry) {
720 if (repo->path[0] == '\0') {
721 log_warnx("repository \"%s\": no path provided in "
722 "configuration file", repo->name);
723 return (-1);
727 if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
728 log_warnx("no repository defined in configuration file");
729 return (-1);
732 return (0);
735 static int
736 uid_connection_limit_cmp(const void *pa, const void *pb)
738 const struct gotd_uid_connection_limit *a = pa, *b = pb;
740 if (a->uid < b->uid)
741 return -1;
742 else if (a->uid > b->uid);
743 return 1;
745 return 0;
748 static int
749 conf_limit_user_connections(const char *user, int maximum)
751 uid_t uid;
752 struct gotd_uid_connection_limit *limit;
753 size_t nlimits;
755 if (maximum < 1) {
756 yyerror("max connections cannot be smaller 1");
757 return -1;
759 if (maximum > GOTD_MAXCLIENTS) {
760 yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
761 return -1;
764 if (gotd_auth_parseuid(user, &uid) == -1) {
765 yyerror("%s: no such user", user);
766 return -1;
769 limit = gotd_find_uid_connection_limit(gotd->connection_limits,
770 gotd->nconnection_limits, uid);
771 if (limit) {
772 limit->max_connections = maximum;
773 return 0;
776 limit = gotd->connection_limits;
777 nlimits = gotd->nconnection_limits + 1;
778 limit = reallocarray(limit, nlimits, sizeof(*limit));
779 if (limit == NULL)
780 fatal("reallocarray");
782 limit[nlimits - 1].uid = uid;
783 limit[nlimits - 1].max_connections = maximum;
785 gotd->connection_limits = limit;
786 gotd->nconnection_limits = nlimits;
787 qsort(gotd->connection_limits, gotd->nconnection_limits,
788 sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
790 return 0;
793 static struct gotd_repo *
794 conf_new_repo(const char *name)
796 struct gotd_repo *repo;
798 if (name[0] == '\0') {
799 fatalx("syntax error: empty repository name found in %s",
800 file->name);
803 if (strchr(name, '\n') != NULL)
804 fatalx("repository names must not contain linefeeds: %s", name);
806 repo = calloc(1, sizeof(*repo));
807 if (repo == NULL)
808 fatalx("%s: calloc", __func__);
810 STAILQ_INIT(&repo->rules);
812 if (strlcpy(repo->name, name, sizeof(repo->name)) >=
813 sizeof(repo->name))
814 fatalx("%s: strlcpy", __func__);
816 TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
817 gotd->nrepos++;
819 return repo;
820 };
822 static void
823 conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
824 int authorization, char *identifier)
826 struct gotd_access_rule *rule;
828 rule = calloc(1, sizeof(*rule));
829 if (rule == NULL)
830 fatal("calloc");
832 rule->access = access;
833 rule->authorization = authorization;
834 rule->identifier = identifier;
836 STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
839 int
840 symset(const char *nam, const char *val, int persist)
842 struct sym *sym;
844 TAILQ_FOREACH(sym, &symhead, entry) {
845 if (strcmp(nam, sym->nam) == 0)
846 break;
849 if (sym != NULL) {
850 if (sym->persist == 1)
851 return (0);
852 else {
853 free(sym->nam);
854 free(sym->val);
855 TAILQ_REMOVE(&symhead, sym, entry);
856 free(sym);
859 sym = calloc(1, sizeof(*sym));
860 if (sym == NULL)
861 return (-1);
863 sym->nam = strdup(nam);
864 if (sym->nam == NULL) {
865 free(sym);
866 return (-1);
868 sym->val = strdup(val);
869 if (sym->val == NULL) {
870 free(sym->nam);
871 free(sym);
872 return (-1);
874 sym->used = 0;
875 sym->persist = persist;
876 TAILQ_INSERT_TAIL(&symhead, sym, entry);
877 return (0);
880 char *
881 symget(const char *nam)
883 struct sym *sym;
885 TAILQ_FOREACH(sym, &symhead, entry) {
886 if (strcmp(nam, sym->nam) == 0) {
887 sym->used = 1;
888 return (sym->val);
891 return (NULL);