2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas * Copyright (c) 2016-2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
4 3efd8e31 2022-10-23 thomas * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 3efd8e31 2022-10-23 thomas * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 3efd8e31 2022-10-23 thomas * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 3efd8e31 2022-10-23 thomas * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
12 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
13 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
15 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
27 3efd8e31 2022-10-23 thomas #include <sys/time.h>
28 3efd8e31 2022-10-23 thomas #include <sys/types.h>
29 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
30 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
32 3efd8e31 2022-10-23 thomas #include <ctype.h>
33 3efd8e31 2022-10-23 thomas #include <err.h>
34 3efd8e31 2022-10-23 thomas #include <errno.h>
35 3efd8e31 2022-10-23 thomas #include <event.h>
36 3efd8e31 2022-10-23 thomas #include <imsg.h>
37 3efd8e31 2022-10-23 thomas #include <limits.h>
38 48488136 2023-04-22 thomas #include <pwd.h>
39 3efd8e31 2022-10-23 thomas #include <stdarg.h>
40 3efd8e31 2022-10-23 thomas #include <stdlib.h>
41 3efd8e31 2022-10-23 thomas #include <stdio.h>
42 3efd8e31 2022-10-23 thomas #include <string.h>
43 3efd8e31 2022-10-23 thomas #include <syslog.h>
44 3efd8e31 2022-10-23 thomas #include <unistd.h>
46 3efd8e31 2022-10-23 thomas #include "got_error.h"
47 3efd8e31 2022-10-23 thomas #include "got_path.h"
48 6d7eb4f7 2023-04-04 thomas #include "got_reference.h"
50 3efd8e31 2022-10-23 thomas #include "log.h"
51 3efd8e31 2022-10-23 thomas #include "gotd.h"
52 0781db0e 2023-01-06 thomas #include "auth.h"
53 0781db0e 2023-01-06 thomas #include "listen.h"
55 3efd8e31 2022-10-23 thomas TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
56 3efd8e31 2022-10-23 thomas static struct file {
57 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(file) entry;
58 3efd8e31 2022-10-23 thomas FILE *stream;
59 3efd8e31 2022-10-23 thomas char *name;
60 3efd8e31 2022-10-23 thomas int lineno;
61 3efd8e31 2022-10-23 thomas int errors;
63 7554713a 2023-04-01 thomas struct file *newfile(const char *, int, int);
64 3efd8e31 2022-10-23 thomas static void closefile(struct file *);
65 3efd8e31 2022-10-23 thomas int check_file_secrecy(int, const char *);
66 3efd8e31 2022-10-23 thomas int yyparse(void);
67 3efd8e31 2022-10-23 thomas int yylex(void);
68 3efd8e31 2022-10-23 thomas int yyerror(const char *, ...)
69 3efd8e31 2022-10-23 thomas __attribute__((__format__ (printf, 1, 2)))
70 3efd8e31 2022-10-23 thomas __attribute__((__nonnull__ (1)));
71 3efd8e31 2022-10-23 thomas int kw_cmp(const void *, const void *);
72 3efd8e31 2022-10-23 thomas int lookup(char *);
73 3efd8e31 2022-10-23 thomas int lgetc(int);
74 3efd8e31 2022-10-23 thomas int lungetc(int);
75 3efd8e31 2022-10-23 thomas int findeol(void);
77 3efd8e31 2022-10-23 thomas TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 3efd8e31 2022-10-23 thomas struct sym {
79 3efd8e31 2022-10-23 thomas TAILQ_ENTRY(sym) entry;
81 3efd8e31 2022-10-23 thomas int persist;
82 3efd8e31 2022-10-23 thomas char *nam;
83 3efd8e31 2022-10-23 thomas char *val;
86 3efd8e31 2022-10-23 thomas int symset(const char *, const char *, int);
87 3efd8e31 2022-10-23 thomas char *symget(const char *);
89 3efd8e31 2022-10-23 thomas static int errors;
91 3efd8e31 2022-10-23 thomas static struct gotd *gotd;
92 3efd8e31 2022-10-23 thomas static struct gotd_repo *new_repo;
93 67f822ee 2023-01-06 thomas static int conf_limit_user_connections(const char *, int);
94 3efd8e31 2022-10-23 thomas static struct gotd_repo *conf_new_repo(const char *);
95 c3841c67 2022-11-18 thomas static void conf_new_access_rule(struct gotd_repo *,
96 c3841c67 2022-11-18 thomas enum gotd_access, int, char *);
97 57b6056a 2023-04-05 thomas static int conf_protect_ref_namespace(char **,
98 6d7eb4f7 2023-04-04 thomas struct got_pathlist_head *, char *);
99 6d7eb4f7 2023-04-04 thomas static int conf_protect_tag_namespace(struct gotd_repo *,
101 6d7eb4f7 2023-04-04 thomas static int conf_protect_branch_namespace(
102 6d7eb4f7 2023-04-04 thomas struct gotd_repo *, char *);
103 6d7eb4f7 2023-04-04 thomas static int conf_protect_branch(struct gotd_repo *,
105 3efd8e31 2022-10-23 thomas static enum gotd_procid gotd_proc_id;
107 3efd8e31 2022-10-23 thomas typedef struct {
109 3efd8e31 2022-10-23 thomas long long number;
110 3efd8e31 2022-10-23 thomas char *string;
111 0781db0e 2023-01-06 thomas struct timeval tv;
113 3efd8e31 2022-10-23 thomas int lineno;
114 3efd8e31 2022-10-23 thomas } YYSTYPE;
118 f9a4feb6 2023-01-06 thomas %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
119 0781db0e 2023-01-06 thomas %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
120 6d7eb4f7 2023-04-04 thomas %token PROTECT NAMESPACE BRANCH TAG
122 3efd8e31 2022-10-23 thomas %token <v.string> STRING
123 3efd8e31 2022-10-23 thomas %token <v.number> NUMBER
124 0781db0e 2023-01-06 thomas %type <v.tv> timeout
128 3efd8e31 2022-10-23 thomas grammar :
129 3efd8e31 2022-10-23 thomas | grammar '\n'
130 07202819 2023-11-06 thomas | grammar varset '\n'
131 3efd8e31 2022-10-23 thomas | grammar main '\n'
132 3efd8e31 2022-10-23 thomas | grammar repository '\n'
135 07202819 2023-11-06 thomas varset : STRING '=' STRING {
136 07202819 2023-11-06 thomas char *s = $1;
137 07202819 2023-11-06 thomas while (*s++) {
138 07202819 2023-11-06 thomas if (isspace((unsigned char)*s)) {
139 07202819 2023-11-06 thomas yyerror("macro name cannot contain "
140 07202819 2023-11-06 thomas "whitespace");
141 07202819 2023-11-06 thomas free($1);
142 07202819 2023-11-06 thomas free($3);
146 07202819 2023-11-06 thomas if (symset($1, $3, 0) == -1)
147 07202819 2023-11-06 thomas fatal("cannot store variable");
148 07202819 2023-11-06 thomas free($1);
149 07202819 2023-11-06 thomas free($3);
153 0781db0e 2023-01-06 thomas timeout : NUMBER {
154 0781db0e 2023-01-06 thomas if ($1 < 0) {
155 0781db0e 2023-01-06 thomas yyerror("invalid timeout: %lld", $1);
158 0781db0e 2023-01-06 thomas $$.tv_sec = $1;
159 0781db0e 2023-01-06 thomas $$.tv_usec = 0;
161 77d755e8 2023-01-06 thomas | STRING {
162 77d755e8 2023-01-06 thomas const char *errstr;
163 77d755e8 2023-01-06 thomas const char *type = "seconds";
164 77d755e8 2023-01-06 thomas size_t len;
165 77d755e8 2023-01-06 thomas int mul = 1;
167 77d755e8 2023-01-06 thomas if (*$1 == '\0') {
168 77d755e8 2023-01-06 thomas yyerror("invalid number of seconds: %s", $1);
169 77d755e8 2023-01-06 thomas free($1);
173 77d755e8 2023-01-06 thomas len = strlen($1);
174 77d755e8 2023-01-06 thomas switch ($1[len - 1]) {
175 77d755e8 2023-01-06 thomas case 'S':
176 77d755e8 2023-01-06 thomas case 's':
177 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
179 77d755e8 2023-01-06 thomas case 'M':
180 77d755e8 2023-01-06 thomas case 'm':
181 77d755e8 2023-01-06 thomas type = "minutes";
182 77d755e8 2023-01-06 thomas mul = 60;
183 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
185 77d755e8 2023-01-06 thomas case 'H':
186 77d755e8 2023-01-06 thomas case 'h':
187 77d755e8 2023-01-06 thomas type = "hours";
188 77d755e8 2023-01-06 thomas mul = 60 * 60;
189 77d755e8 2023-01-06 thomas $1[len - 1] = '\0';
193 77d755e8 2023-01-06 thomas $$.tv_usec = 0;
194 85bccbf7 2023-01-06 thomas $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
195 77d755e8 2023-01-06 thomas if (errstr) {
196 77d755e8 2023-01-06 thomas yyerror("number of %s is %s: %s", type,
197 77d755e8 2023-01-06 thomas errstr, $1);
198 77d755e8 2023-01-06 thomas free($1);
202 77d755e8 2023-01-06 thomas $$.tv_sec *= mul;
203 77d755e8 2023-01-06 thomas free($1);
207 f9a4feb6 2023-01-06 thomas main : LISTEN ON STRING {
208 7348ded8 2023-01-19 thomas if (!got_path_is_absolute($3))
209 7348ded8 2023-01-19 thomas yyerror("bad unix socket path \"%s\": "
210 7348ded8 2023-01-19 thomas "must be an absolute path", $3);
212 2b3d32a1 2022-12-30 thomas if (gotd_proc_id == PROC_LISTEN) {
213 f9a4feb6 2023-01-06 thomas if (strlcpy(gotd->unix_socket_path, $3,
214 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >=
215 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) {
216 3efd8e31 2022-10-23 thomas yyerror("%s: unix socket path too long",
217 3efd8e31 2022-10-23 thomas __func__);
218 f9a4feb6 2023-01-06 thomas free($3);
222 f9a4feb6 2023-01-06 thomas free($3);
224 3efd8e31 2022-10-23 thomas | USER STRING {
225 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, $2,
226 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >=
227 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) {
228 3efd8e31 2022-10-23 thomas yyerror("%s: user name too long", __func__);
229 3efd8e31 2022-10-23 thomas free($2);
232 3efd8e31 2022-10-23 thomas free($2);
234 0781db0e 2023-01-06 thomas | connection
237 0781db0e 2023-01-06 thomas connection : CONNECTION '{' optnl conflags_l '}'
238 0781db0e 2023-01-06 thomas | CONNECTION conflags
240 0781db0e 2023-01-06 thomas conflags_l : conflags optnl conflags_l
241 0781db0e 2023-01-06 thomas | conflags optnl
244 0781db0e 2023-01-06 thomas conflags : REQUEST TIMEOUT timeout {
245 912db690 2023-01-06 thomas if ($3.tv_sec <= 0) {
246 912db690 2023-01-06 thomas yyerror("invalid timeout: %lld", $3.tv_sec);
249 0781db0e 2023-01-06 thomas memcpy(&gotd->request_timeout, &$3,
250 0781db0e 2023-01-06 thomas sizeof(gotd->request_timeout));
252 0781db0e 2023-01-06 thomas | LIMIT USER STRING NUMBER {
253 0781db0e 2023-01-06 thomas if (gotd_proc_id == PROC_LISTEN &&
254 0781db0e 2023-01-06 thomas conf_limit_user_connections($3, $4) == -1) {
255 0781db0e 2023-01-06 thomas free($3);
258 0781db0e 2023-01-06 thomas free($3);
262 6d7eb4f7 2023-04-04 thomas protect : PROTECT '{' optnl protectflags_l '}'
263 6d7eb4f7 2023-04-04 thomas | PROTECT protectflags
265 6d7eb4f7 2023-04-04 thomas protectflags_l : protectflags optnl protectflags_l
266 6d7eb4f7 2023-04-04 thomas | protectflags optnl
269 6d7eb4f7 2023-04-04 thomas protectflags : TAG NAMESPACE STRING {
270 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
271 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
272 6d7eb4f7 2023-04-04 thomas if (conf_protect_tag_namespace(new_repo, $3)) {
273 6d7eb4f7 2023-04-04 thomas free($3);
277 ffc797f3 2023-04-05 thomas free($3);
279 6d7eb4f7 2023-04-04 thomas | BRANCH NAMESPACE STRING {
280 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
281 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
282 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch_namespace(new_repo,
284 6d7eb4f7 2023-04-04 thomas free($3);
288 ffc797f3 2023-04-05 thomas free($3);
290 6d7eb4f7 2023-04-04 thomas | BRANCH STRING {
291 6d7eb4f7 2023-04-04 thomas if (gotd_proc_id == PROC_GOTD ||
292 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_REPO_WRITE) {
293 6d7eb4f7 2023-04-04 thomas if (conf_protect_branch(new_repo, $2)) {
294 6d7eb4f7 2023-04-04 thomas free($2);
298 ffc797f3 2023-04-05 thomas free($2);
302 3efd8e31 2022-10-23 thomas repository : REPOSITORY STRING {
303 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
305 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
306 3efd8e31 2022-10-23 thomas if (strcmp(repo->name, $2) == 0) {
307 3efd8e31 2022-10-23 thomas yyerror("duplicate repository '%s'", $2);
308 3efd8e31 2022-10-23 thomas free($2);
313 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
314 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
315 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_REPO_WRITE ||
316 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_GITWRAPPER) {
317 3efd8e31 2022-10-23 thomas new_repo = conf_new_repo($2);
319 3efd8e31 2022-10-23 thomas free($2);
320 3efd8e31 2022-10-23 thomas } '{' optnl repoopts2 '}' {
324 3efd8e31 2022-10-23 thomas repoopts1 : PATH STRING {
325 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_GOTD ||
326 6d7eb4f7 2023-04-04 thomas gotd_proc_id == PROC_AUTH ||
327 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_REPO_WRITE ||
328 f3807fe5 2023-07-10 thomas gotd_proc_id == PROC_GITWRAPPER) {
329 3efd8e31 2022-10-23 thomas if (!got_path_is_absolute($2)) {
330 3efd8e31 2022-10-23 thomas yyerror("%s: path %s is not absolute",
331 3efd8e31 2022-10-23 thomas __func__, $2);
332 3efd8e31 2022-10-23 thomas free($2);
335 fe6a8988 2023-01-08 thomas if (realpath($2, new_repo->path) == NULL) {
337 f3807fe5 2023-07-10 thomas * To give admins a chance to create
338 f3807fe5 2023-07-10 thomas * missing repositories at run-time
339 f3807fe5 2023-07-10 thomas * we only warn about ENOENT here.
341 f3807fe5 2023-07-10 thomas * And ignore 'permission denied' when
342 f3807fe5 2023-07-10 thomas * running in gitwrapper. Users may be
343 f3807fe5 2023-07-10 thomas * able to access this repository via
344 f3807fe5 2023-07-10 thomas * gotd regardless.
346 f3807fe5 2023-07-10 thomas if (errno == ENOENT) {
347 f3807fe5 2023-07-10 thomas yyerror("realpath %s: %s", $2,
348 f3807fe5 2023-07-10 thomas strerror(errno));
349 f3807fe5 2023-07-10 thomas } else if (errno != EACCES ||
350 f3807fe5 2023-07-10 thomas gotd_proc_id != PROC_GITWRAPPER) {
351 f3807fe5 2023-07-10 thomas yyerror("realpath %s: %s", $2,
352 f3807fe5 2023-07-10 thomas strerror(errno));
353 d95864cd 2023-04-14 thomas free($2);
357 f3807fe5 2023-07-10 thomas if (strlcpy(new_repo->path, $2,
358 d95864cd 2023-04-14 thomas sizeof(new_repo->path)) >=
359 d95864cd 2023-04-14 thomas sizeof(new_repo->path))
360 d95864cd 2023-04-14 thomas yyerror("path too long");
363 3efd8e31 2022-10-23 thomas free($2);
365 729a7e24 2022-11-17 thomas | PERMIT RO STRING {
366 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
367 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
368 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
370 ffc797f3 2023-04-05 thomas free($3);
372 729a7e24 2022-11-17 thomas | PERMIT RW STRING {
373 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
374 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
375 729a7e24 2022-11-17 thomas GOTD_ACCESS_PERMITTED,
376 729a7e24 2022-11-17 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
378 ffc797f3 2023-04-05 thomas free($3);
380 729a7e24 2022-11-17 thomas | DENY STRING {
381 c669c489 2022-12-30 thomas if (gotd_proc_id == PROC_AUTH) {
382 729a7e24 2022-11-17 thomas conf_new_access_rule(new_repo,
383 729a7e24 2022-11-17 thomas GOTD_ACCESS_DENIED, 0, $2);
385 ffc797f3 2023-04-05 thomas free($2);
387 6d7eb4f7 2023-04-04 thomas | protect
390 3efd8e31 2022-10-23 thomas repoopts2 : repoopts2 repoopts1 nl
391 3efd8e31 2022-10-23 thomas | repoopts1 optnl
394 3efd8e31 2022-10-23 thomas nl : '\n' optnl
397 3efd8e31 2022-10-23 thomas optnl : '\n' optnl /* zero or more newlines */
398 3efd8e31 2022-10-23 thomas | /* empty */
403 3efd8e31 2022-10-23 thomas struct keywords {
404 3efd8e31 2022-10-23 thomas const char *k_name;
405 3efd8e31 2022-10-23 thomas int k_val;
409 3efd8e31 2022-10-23 thomas yyerror(const char *fmt, ...)
411 3efd8e31 2022-10-23 thomas va_list ap;
412 3efd8e31 2022-10-23 thomas char *msg;
414 3efd8e31 2022-10-23 thomas file->errors++;
415 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
416 3efd8e31 2022-10-23 thomas if (vasprintf(&msg, fmt, ap) == -1)
417 3efd8e31 2022-10-23 thomas fatalx("yyerror vasprintf");
418 3efd8e31 2022-10-23 thomas va_end(ap);
419 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
420 3efd8e31 2022-10-23 thomas free(msg);
421 3efd8e31 2022-10-23 thomas return (0);
425 3efd8e31 2022-10-23 thomas kw_cmp(const void *k, const void *e)
427 3efd8e31 2022-10-23 thomas return (strcmp(k, ((const struct keywords *)e)->k_name));
431 3efd8e31 2022-10-23 thomas lookup(char *s)
433 3efd8e31 2022-10-23 thomas /* This has to be sorted always. */
434 3efd8e31 2022-10-23 thomas static const struct keywords keywords[] = {
435 6d7eb4f7 2023-04-04 thomas { "branch", BRANCH },
436 0781db0e 2023-01-06 thomas { "connection", CONNECTION },
437 729a7e24 2022-11-17 thomas { "deny", DENY },
438 0781db0e 2023-01-06 thomas { "limit", LIMIT },
439 f9a4feb6 2023-01-06 thomas { "listen", LISTEN },
440 6d7eb4f7 2023-04-04 thomas { "namespace", NAMESPACE },
441 3efd8e31 2022-10-23 thomas { "on", ON },
442 3efd8e31 2022-10-23 thomas { "path", PATH },
443 729a7e24 2022-11-17 thomas { "permit", PERMIT },
444 6d7eb4f7 2023-04-04 thomas { "protect", PROTECT },
445 3efd8e31 2022-10-23 thomas { "repository", REPOSITORY },
446 0781db0e 2023-01-06 thomas { "request", REQUEST },
447 729a7e24 2022-11-17 thomas { "ro", RO },
448 729a7e24 2022-11-17 thomas { "rw", RW },
449 6d7eb4f7 2023-04-04 thomas { "tag", TAG },
450 0781db0e 2023-01-06 thomas { "timeout", TIMEOUT },
451 3efd8e31 2022-10-23 thomas { "user", USER },
453 3efd8e31 2022-10-23 thomas const struct keywords *p;
455 3efd8e31 2022-10-23 thomas p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
456 3efd8e31 2022-10-23 thomas sizeof(keywords[0]), kw_cmp);
459 3efd8e31 2022-10-23 thomas return (p->k_val);
461 3efd8e31 2022-10-23 thomas return (STRING);
464 3efd8e31 2022-10-23 thomas #define MAXPUSHBACK 128
466 3efd8e31 2022-10-23 thomas unsigned char *parsebuf;
467 3efd8e31 2022-10-23 thomas int parseindex;
468 3efd8e31 2022-10-23 thomas unsigned char pushback_buffer[MAXPUSHBACK];
469 3efd8e31 2022-10-23 thomas int pushback_index = 0;
472 3efd8e31 2022-10-23 thomas lgetc(int quotec)
474 3efd8e31 2022-10-23 thomas int c, next;
476 3efd8e31 2022-10-23 thomas if (parsebuf) {
477 3efd8e31 2022-10-23 thomas /* Read character from the parsebuffer instead of input. */
478 3efd8e31 2022-10-23 thomas if (parseindex >= 0) {
479 3efd8e31 2022-10-23 thomas c = parsebuf[parseindex++];
480 3efd8e31 2022-10-23 thomas if (c != '\0')
481 3efd8e31 2022-10-23 thomas return (c);
482 3efd8e31 2022-10-23 thomas parsebuf = NULL;
484 3efd8e31 2022-10-23 thomas parseindex++;
487 3efd8e31 2022-10-23 thomas if (pushback_index)
488 3efd8e31 2022-10-23 thomas return (pushback_buffer[--pushback_index]);
490 3efd8e31 2022-10-23 thomas if (quotec) {
491 3efd8e31 2022-10-23 thomas c = getc(file->stream);
492 3efd8e31 2022-10-23 thomas if (c == EOF)
493 3efd8e31 2022-10-23 thomas yyerror("reached end of file while parsing "
494 3efd8e31 2022-10-23 thomas "quoted string");
495 3efd8e31 2022-10-23 thomas return (c);
498 3efd8e31 2022-10-23 thomas c = getc(file->stream);
499 3efd8e31 2022-10-23 thomas while (c == '\\') {
500 3efd8e31 2022-10-23 thomas next = getc(file->stream);
501 3efd8e31 2022-10-23 thomas if (next != '\n') {
502 3efd8e31 2022-10-23 thomas c = next;
505 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
506 3efd8e31 2022-10-23 thomas file->lineno++;
507 3efd8e31 2022-10-23 thomas c = getc(file->stream);
510 3efd8e31 2022-10-23 thomas return (c);
514 3efd8e31 2022-10-23 thomas lungetc(int c)
516 3efd8e31 2022-10-23 thomas if (c == EOF)
517 3efd8e31 2022-10-23 thomas return (EOF);
518 3efd8e31 2022-10-23 thomas if (parsebuf) {
519 3efd8e31 2022-10-23 thomas parseindex--;
520 3efd8e31 2022-10-23 thomas if (parseindex >= 0)
521 3efd8e31 2022-10-23 thomas return (c);
523 3efd8e31 2022-10-23 thomas if (pushback_index < MAXPUSHBACK-1)
524 3efd8e31 2022-10-23 thomas return (pushback_buffer[pushback_index++] = c);
526 3efd8e31 2022-10-23 thomas return (EOF);
530 3efd8e31 2022-10-23 thomas findeol(void)
534 3efd8e31 2022-10-23 thomas parsebuf = NULL;
536 3efd8e31 2022-10-23 thomas /* Skip to either EOF or the first real EOL. */
537 3efd8e31 2022-10-23 thomas while (1) {
538 3efd8e31 2022-10-23 thomas if (pushback_index)
539 3efd8e31 2022-10-23 thomas c = pushback_buffer[--pushback_index];
541 3efd8e31 2022-10-23 thomas c = lgetc(0);
542 3efd8e31 2022-10-23 thomas if (c == '\n') {
543 3efd8e31 2022-10-23 thomas file->lineno++;
546 3efd8e31 2022-10-23 thomas if (c == EOF)
549 3efd8e31 2022-10-23 thomas return (ERROR);
553 3efd8e31 2022-10-23 thomas yylex(void)
555 3efd8e31 2022-10-23 thomas unsigned char buf[8096];
556 3efd8e31 2022-10-23 thomas unsigned char *p, *val;
557 3efd8e31 2022-10-23 thomas int quotec, next, c;
558 3efd8e31 2022-10-23 thomas int token;
562 3efd8e31 2022-10-23 thomas c = lgetc(0);
563 3efd8e31 2022-10-23 thomas while (c == ' ' || c == '\t')
564 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
566 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
567 3efd8e31 2022-10-23 thomas if (c == '#') {
568 3efd8e31 2022-10-23 thomas c = lgetc(0);
569 3efd8e31 2022-10-23 thomas while (c != '\n' && c != EOF)
570 3efd8e31 2022-10-23 thomas c = lgetc(0); /* nothing */
572 3efd8e31 2022-10-23 thomas if (c == '$' && parsebuf == NULL) {
573 3efd8e31 2022-10-23 thomas while (1) {
574 3efd8e31 2022-10-23 thomas c = lgetc(0);
575 3efd8e31 2022-10-23 thomas if (c == EOF)
576 3efd8e31 2022-10-23 thomas return (0);
578 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
579 3efd8e31 2022-10-23 thomas yyerror("string too long");
580 3efd8e31 2022-10-23 thomas return (findeol());
582 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == '_') {
583 3efd8e31 2022-10-23 thomas *p++ = c;
584 3efd8e31 2022-10-23 thomas continue;
586 3efd8e31 2022-10-23 thomas *p = '\0';
587 3efd8e31 2022-10-23 thomas lungetc(c);
590 3efd8e31 2022-10-23 thomas val = symget(buf);
591 3efd8e31 2022-10-23 thomas if (val == NULL) {
592 3efd8e31 2022-10-23 thomas yyerror("macro '%s' not defined", buf);
593 3efd8e31 2022-10-23 thomas return (findeol());
595 3efd8e31 2022-10-23 thomas parsebuf = val;
596 3efd8e31 2022-10-23 thomas parseindex = 0;
597 3efd8e31 2022-10-23 thomas goto top;
600 3efd8e31 2022-10-23 thomas switch (c) {
601 3efd8e31 2022-10-23 thomas case '\'':
602 3efd8e31 2022-10-23 thomas case '"':
603 3efd8e31 2022-10-23 thomas quotec = c;
604 3efd8e31 2022-10-23 thomas while (1) {
605 3efd8e31 2022-10-23 thomas c = lgetc(quotec);
606 3efd8e31 2022-10-23 thomas if (c == EOF)
607 3efd8e31 2022-10-23 thomas return (0);
608 3efd8e31 2022-10-23 thomas if (c == '\n') {
609 3efd8e31 2022-10-23 thomas file->lineno++;
610 3efd8e31 2022-10-23 thomas continue;
611 3efd8e31 2022-10-23 thomas } else if (c == '\\') {
612 3efd8e31 2022-10-23 thomas next = lgetc(quotec);
613 3efd8e31 2022-10-23 thomas if (next == EOF)
614 3efd8e31 2022-10-23 thomas return (0);
615 3efd8e31 2022-10-23 thomas if (next == quotec || c == ' ' || c == '\t')
616 3efd8e31 2022-10-23 thomas c = next;
617 3efd8e31 2022-10-23 thomas else if (next == '\n') {
618 3efd8e31 2022-10-23 thomas file->lineno++;
619 3efd8e31 2022-10-23 thomas continue;
621 3efd8e31 2022-10-23 thomas lungetc(next);
622 3efd8e31 2022-10-23 thomas } else if (c == quotec) {
623 3efd8e31 2022-10-23 thomas *p = '\0';
625 3efd8e31 2022-10-23 thomas } else if (c == '\0') {
626 3efd8e31 2022-10-23 thomas yyerror("syntax error");
627 3efd8e31 2022-10-23 thomas return (findeol());
629 3efd8e31 2022-10-23 thomas if (p + 1 >= buf + sizeof(buf) - 1) {
630 3efd8e31 2022-10-23 thomas yyerror("string too long");
631 3efd8e31 2022-10-23 thomas return (findeol());
633 3efd8e31 2022-10-23 thomas *p++ = c;
635 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
636 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
637 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
638 3efd8e31 2022-10-23 thomas return (STRING);
641 3efd8e31 2022-10-23 thomas #define allowed_to_end_number(x) \
642 3efd8e31 2022-10-23 thomas (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
644 3efd8e31 2022-10-23 thomas if (c == '-' || isdigit(c)) {
646 3efd8e31 2022-10-23 thomas *p++ = c;
647 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
648 3efd8e31 2022-10-23 thomas yyerror("string too long");
649 3efd8e31 2022-10-23 thomas return (findeol());
651 3efd8e31 2022-10-23 thomas c = lgetc(0);
652 3efd8e31 2022-10-23 thomas } while (c != EOF && isdigit(c));
653 3efd8e31 2022-10-23 thomas lungetc(c);
654 3efd8e31 2022-10-23 thomas if (p == buf + 1 && buf[0] == '-')
655 3efd8e31 2022-10-23 thomas goto nodigits;
656 3efd8e31 2022-10-23 thomas if (c == EOF || allowed_to_end_number(c)) {
657 3efd8e31 2022-10-23 thomas const char *errstr = NULL;
659 3efd8e31 2022-10-23 thomas *p = '\0';
660 3efd8e31 2022-10-23 thomas yylval.v.number = strtonum(buf, LLONG_MIN,
661 3efd8e31 2022-10-23 thomas LLONG_MAX, &errstr);
662 3efd8e31 2022-10-23 thomas if (errstr) {
663 3efd8e31 2022-10-23 thomas yyerror("\"%s\" invalid number: %s",
664 3efd8e31 2022-10-23 thomas buf, errstr);
665 3efd8e31 2022-10-23 thomas return (findeol());
667 3efd8e31 2022-10-23 thomas return (NUMBER);
669 3efd8e31 2022-10-23 thomas nodigits:
670 3efd8e31 2022-10-23 thomas while (p > buf + 1)
671 3efd8e31 2022-10-23 thomas lungetc(*--p);
672 3efd8e31 2022-10-23 thomas c = *--p;
673 3efd8e31 2022-10-23 thomas if (c == '-')
674 3efd8e31 2022-10-23 thomas return (c);
678 3efd8e31 2022-10-23 thomas #define allowed_in_string(x) \
679 3efd8e31 2022-10-23 thomas (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
680 3efd8e31 2022-10-23 thomas x != '{' && x != '}' && \
681 3efd8e31 2022-10-23 thomas x != '!' && x != '=' && x != '#' && \
682 3efd8e31 2022-10-23 thomas x != ','))
684 3efd8e31 2022-10-23 thomas if (isalnum(c) || c == ':' || c == '_') {
686 3efd8e31 2022-10-23 thomas *p++ = c;
687 3efd8e31 2022-10-23 thomas if ((unsigned)(p-buf) >= sizeof(buf)) {
688 3efd8e31 2022-10-23 thomas yyerror("string too long");
689 3efd8e31 2022-10-23 thomas return (findeol());
691 3efd8e31 2022-10-23 thomas c = lgetc(0);
692 3efd8e31 2022-10-23 thomas } while (c != EOF && (allowed_in_string(c)));
693 3efd8e31 2022-10-23 thomas lungetc(c);
694 3efd8e31 2022-10-23 thomas *p = '\0';
695 3efd8e31 2022-10-23 thomas token = lookup(buf);
696 3efd8e31 2022-10-23 thomas if (token == STRING) {
697 3efd8e31 2022-10-23 thomas yylval.v.string = strdup(buf);
698 3efd8e31 2022-10-23 thomas if (yylval.v.string == NULL)
699 3efd8e31 2022-10-23 thomas err(1, "yylex: strdup");
701 3efd8e31 2022-10-23 thomas return (token);
703 3efd8e31 2022-10-23 thomas if (c == '\n') {
704 3efd8e31 2022-10-23 thomas yylval.lineno = file->lineno;
705 3efd8e31 2022-10-23 thomas file->lineno++;
707 3efd8e31 2022-10-23 thomas if (c == EOF)
708 3efd8e31 2022-10-23 thomas return (0);
709 3efd8e31 2022-10-23 thomas return (c);
713 3efd8e31 2022-10-23 thomas check_file_secrecy(int fd, const char *fname)
715 3efd8e31 2022-10-23 thomas struct stat st;
717 3efd8e31 2022-10-23 thomas if (fstat(fd, &st)) {
718 3efd8e31 2022-10-23 thomas log_warn("cannot stat %s", fname);
719 3efd8e31 2022-10-23 thomas return (-1);
721 3efd8e31 2022-10-23 thomas if (st.st_uid != 0 && st.st_uid != getuid()) {
722 3efd8e31 2022-10-23 thomas log_warnx("%s: owner not root or current user", fname);
723 3efd8e31 2022-10-23 thomas return (-1);
725 3efd8e31 2022-10-23 thomas if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
726 3efd8e31 2022-10-23 thomas log_warnx("%s: group writable or world read/writable", fname);
727 3efd8e31 2022-10-23 thomas return (-1);
729 3efd8e31 2022-10-23 thomas return (0);
732 3efd8e31 2022-10-23 thomas struct file *
733 7554713a 2023-04-01 thomas newfile(const char *name, int secret, int required)
735 3efd8e31 2022-10-23 thomas struct file *nfile;
737 3efd8e31 2022-10-23 thomas nfile = calloc(1, sizeof(struct file));
738 3efd8e31 2022-10-23 thomas if (nfile == NULL) {
739 3efd8e31 2022-10-23 thomas log_warn("calloc");
740 3efd8e31 2022-10-23 thomas return (NULL);
742 3efd8e31 2022-10-23 thomas nfile->name = strdup(name);
743 3efd8e31 2022-10-23 thomas if (nfile->name == NULL) {
744 3efd8e31 2022-10-23 thomas log_warn("strdup");
745 3efd8e31 2022-10-23 thomas free(nfile);
746 3efd8e31 2022-10-23 thomas return (NULL);
748 3efd8e31 2022-10-23 thomas nfile->stream = fopen(nfile->name, "r");
749 3efd8e31 2022-10-23 thomas if (nfile->stream == NULL) {
750 7554713a 2023-04-01 thomas if (required)
751 7554713a 2023-04-01 thomas log_warn("open %s", nfile->name);
752 3efd8e31 2022-10-23 thomas free(nfile->name);
753 3efd8e31 2022-10-23 thomas free(nfile);
754 3efd8e31 2022-10-23 thomas return (NULL);
755 3efd8e31 2022-10-23 thomas } else if (secret &&
756 3efd8e31 2022-10-23 thomas check_file_secrecy(fileno(nfile->stream), nfile->name)) {
757 3efd8e31 2022-10-23 thomas fclose(nfile->stream);
758 3efd8e31 2022-10-23 thomas free(nfile->name);
759 3efd8e31 2022-10-23 thomas free(nfile);
760 3efd8e31 2022-10-23 thomas return (NULL);
762 3efd8e31 2022-10-23 thomas nfile->lineno = 1;
763 3efd8e31 2022-10-23 thomas return (nfile);
766 3efd8e31 2022-10-23 thomas static void
767 3efd8e31 2022-10-23 thomas closefile(struct file *xfile)
769 3efd8e31 2022-10-23 thomas fclose(xfile->stream);
770 3efd8e31 2022-10-23 thomas free(xfile->name);
771 3efd8e31 2022-10-23 thomas free(xfile);
775 3efd8e31 2022-10-23 thomas parse_config(const char *filename, enum gotd_procid proc_id,
776 f3807fe5 2023-07-10 thomas struct gotd *env)
778 3efd8e31 2022-10-23 thomas struct sym *sym, *next;
779 88f1bb6d 2023-01-02 thomas struct gotd_repo *repo;
780 f3807fe5 2023-07-10 thomas int require_config_file = (proc_id != PROC_GITWRAPPER);
782 3efd8e31 2022-10-23 thomas memset(env, 0, sizeof(*env));
784 3efd8e31 2022-10-23 thomas gotd = env;
785 3efd8e31 2022-10-23 thomas gotd_proc_id = proc_id;
786 3efd8e31 2022-10-23 thomas TAILQ_INIT(&gotd->repos);
788 3efd8e31 2022-10-23 thomas /* Apply default values. */
789 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
790 3efd8e31 2022-10-23 thomas sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
791 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: unix socket path too long", __func__);
792 3efd8e31 2022-10-23 thomas return -1;
794 3efd8e31 2022-10-23 thomas if (strlcpy(gotd->user_name, GOTD_USER,
795 3efd8e31 2022-10-23 thomas sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
796 3efd8e31 2022-10-23 thomas fprintf(stderr, "%s: user name too long", __func__);
797 3efd8e31 2022-10-23 thomas return -1;
800 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
801 0781db0e 2023-01-06 thomas gotd->request_timeout.tv_usec = 0;
803 7554713a 2023-04-01 thomas file = newfile(filename, 0, require_config_file);
804 f3296add 2023-03-01 thomas if (file == NULL)
805 7554713a 2023-04-01 thomas return require_config_file ? -1 : 0;
807 3efd8e31 2022-10-23 thomas yyparse();
808 3efd8e31 2022-10-23 thomas errors = file->errors;
809 3efd8e31 2022-10-23 thomas closefile(file);
811 3efd8e31 2022-10-23 thomas /* Free macros and check which have not been used. */
812 3efd8e31 2022-10-23 thomas TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
813 3efd8e31 2022-10-23 thomas if ((gotd->verbosity > 1) && !sym->used)
814 3efd8e31 2022-10-23 thomas fprintf(stderr, "warning: macro '%s' not used\n",
815 3efd8e31 2022-10-23 thomas sym->nam);
816 3efd8e31 2022-10-23 thomas if (!sym->persist) {
817 3efd8e31 2022-10-23 thomas free(sym->nam);
818 3efd8e31 2022-10-23 thomas free(sym->val);
819 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
820 3efd8e31 2022-10-23 thomas free(sym);
824 3efd8e31 2022-10-23 thomas if (errors)
825 3efd8e31 2022-10-23 thomas return (-1);
827 88f1bb6d 2023-01-02 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
828 88f1bb6d 2023-01-02 thomas if (repo->path[0] == '\0') {
829 0cbf8de7 2023-01-02 thomas log_warnx("repository \"%s\": no path provided in "
830 0cbf8de7 2023-01-02 thomas "configuration file", repo->name);
831 88f1bb6d 2023-01-02 thomas return (-1);
835 4d24b1fd 2023-01-19 thomas if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
836 4d24b1fd 2023-01-19 thomas log_warnx("no repository defined in configuration file");
837 4d24b1fd 2023-01-19 thomas return (-1);
840 3efd8e31 2022-10-23 thomas return (0);
843 0781db0e 2023-01-06 thomas static int
844 0781db0e 2023-01-06 thomas uid_connection_limit_cmp(const void *pa, const void *pb)
846 0781db0e 2023-01-06 thomas const struct gotd_uid_connection_limit *a = pa, *b = pb;
848 0781db0e 2023-01-06 thomas if (a->uid < b->uid)
849 0781db0e 2023-01-06 thomas return -1;
850 0781db0e 2023-01-06 thomas else if (a->uid > b->uid);
851 0781db0e 2023-01-06 thomas return 1;
853 0781db0e 2023-01-06 thomas return 0;
856 0781db0e 2023-01-06 thomas static int
857 0781db0e 2023-01-06 thomas conf_limit_user_connections(const char *user, int maximum)
859 0781db0e 2023-01-06 thomas uid_t uid;
860 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
861 0781db0e 2023-01-06 thomas size_t nlimits;
863 0781db0e 2023-01-06 thomas if (maximum < 1) {
864 0781db0e 2023-01-06 thomas yyerror("max connections cannot be smaller 1");
865 0781db0e 2023-01-06 thomas return -1;
867 0781db0e 2023-01-06 thomas if (maximum > GOTD_MAXCLIENTS) {
868 0781db0e 2023-01-06 thomas yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
869 0781db0e 2023-01-06 thomas return -1;
872 48488136 2023-04-22 thomas if (gotd_parseuid(user, &uid) == -1) {
873 0781db0e 2023-01-06 thomas yyerror("%s: no such user", user);
874 0781db0e 2023-01-06 thomas return -1;
877 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(gotd->connection_limits,
878 0781db0e 2023-01-06 thomas gotd->nconnection_limits, uid);
879 0781db0e 2023-01-06 thomas if (limit) {
880 0781db0e 2023-01-06 thomas limit->max_connections = maximum;
881 0781db0e 2023-01-06 thomas return 0;
884 0781db0e 2023-01-06 thomas limit = gotd->connection_limits;
885 0781db0e 2023-01-06 thomas nlimits = gotd->nconnection_limits + 1;
886 0781db0e 2023-01-06 thomas limit = reallocarray(limit, nlimits, sizeof(*limit));
887 0781db0e 2023-01-06 thomas if (limit == NULL)
888 0781db0e 2023-01-06 thomas fatal("reallocarray");
890 0781db0e 2023-01-06 thomas limit[nlimits - 1].uid = uid;
891 0781db0e 2023-01-06 thomas limit[nlimits - 1].max_connections = maximum;
893 0781db0e 2023-01-06 thomas gotd->connection_limits = limit;
894 0781db0e 2023-01-06 thomas gotd->nconnection_limits = nlimits;
895 0781db0e 2023-01-06 thomas qsort(gotd->connection_limits, gotd->nconnection_limits,
896 0781db0e 2023-01-06 thomas sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
898 0781db0e 2023-01-06 thomas return 0;
901 3efd8e31 2022-10-23 thomas static struct gotd_repo *
902 3efd8e31 2022-10-23 thomas conf_new_repo(const char *name)
904 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
906 a42b418b 2023-01-02 thomas if (name[0] == '\0') {
907 a42b418b 2023-01-02 thomas fatalx("syntax error: empty repository name found in %s",
908 a42b418b 2023-01-02 thomas file->name);
911 0cbf8de7 2023-01-02 thomas if (strchr(name, '\n') != NULL)
912 0cbf8de7 2023-01-02 thomas fatalx("repository names must not contain linefeeds: %s", name);
914 3efd8e31 2022-10-23 thomas repo = calloc(1, sizeof(*repo));
915 3efd8e31 2022-10-23 thomas if (repo == NULL)
916 3efd8e31 2022-10-23 thomas fatalx("%s: calloc", __func__);
918 729a7e24 2022-11-17 thomas STAILQ_INIT(&repo->rules);
919 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_tag_namespaces);
920 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branch_namespaces);
921 6d7eb4f7 2023-04-04 thomas TAILQ_INIT(&repo->protected_branches);
923 3efd8e31 2022-10-23 thomas if (strlcpy(repo->name, name, sizeof(repo->name)) >=
924 3efd8e31 2022-10-23 thomas sizeof(repo->name))
925 3efd8e31 2022-10-23 thomas fatalx("%s: strlcpy", __func__);
927 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
928 3efd8e31 2022-10-23 thomas gotd->nrepos++;
930 3efd8e31 2022-10-23 thomas return repo;
933 729a7e24 2022-11-17 thomas static void
934 729a7e24 2022-11-17 thomas conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
935 729a7e24 2022-11-17 thomas int authorization, char *identifier)
937 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
939 729a7e24 2022-11-17 thomas rule = calloc(1, sizeof(*rule));
940 729a7e24 2022-11-17 thomas if (rule == NULL)
941 729a7e24 2022-11-17 thomas fatal("calloc");
943 729a7e24 2022-11-17 thomas rule->access = access;
944 729a7e24 2022-11-17 thomas rule->authorization = authorization;
945 729a7e24 2022-11-17 thomas rule->identifier = identifier;
947 729a7e24 2022-11-17 thomas STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
950 6d7eb4f7 2023-04-04 thomas static int
951 6d7eb4f7 2023-04-04 thomas refname_is_valid(char *refname)
953 ef44e136 2023-06-15 thomas if (strncmp(refname, "refs/", 5) != 0) {
954 6d7eb4f7 2023-04-04 thomas yyerror("reference name must begin with \"refs/\": %s",
955 6d7eb4f7 2023-04-04 thomas refname);
956 6d7eb4f7 2023-04-04 thomas return 0;
959 6d7eb4f7 2023-04-04 thomas if (!got_ref_name_is_valid(refname)) {
960 6d7eb4f7 2023-04-04 thomas yyerror("invalid reference name: %s", refname);
961 6d7eb4f7 2023-04-04 thomas return 0;
964 6d7eb4f7 2023-04-04 thomas return 1;
967 6d7eb4f7 2023-04-04 thomas static int
968 57b6056a 2023-04-05 thomas conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
969 57b6056a 2023-04-05 thomas char *namespace)
971 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
972 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
975 57b6056a 2023-04-05 thomas *new = NULL;
977 6d7eb4f7 2023-04-04 thomas got_path_strip_trailing_slashes(namespace);
978 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(namespace))
979 6d7eb4f7 2023-04-04 thomas return -1;
980 6d7eb4f7 2023-04-04 thomas if (asprintf(&s, "%s/", namespace) == -1) {
981 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
982 6d7eb4f7 2023-04-04 thomas return -1;
985 57b6056a 2023-04-05 thomas error = got_pathlist_insert(&pe, refs, s, NULL);
986 57b6056a 2023-04-05 thomas if (error || pe == NULL) {
988 a4435ef0 2023-04-05 thomas if (error)
989 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
991 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
992 6d7eb4f7 2023-04-04 thomas return -1;
995 57b6056a 2023-04-05 thomas *new = s;
996 6d7eb4f7 2023-04-04 thomas return 0;
999 6d7eb4f7 2023-04-04 thomas static int
1000 6d7eb4f7 2023-04-04 thomas conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1002 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1003 57b6056a 2023-04-05 thomas char *new;
1005 57b6056a 2023-04-05 thomas if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1006 57b6056a 2023-04-05 thomas namespace) == -1)
1007 57b6056a 2023-04-05 thomas return -1;
1009 57b6056a 2023-04-05 thomas TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1010 57b6056a 2023-04-05 thomas if (strcmp(pe->path, new) == 0) {
1011 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1012 57b6056a 2023-04-05 thomas return -1;
1016 57b6056a 2023-04-05 thomas return 0;
1019 6d7eb4f7 2023-04-04 thomas static int
1020 6d7eb4f7 2023-04-04 thomas conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1022 57b6056a 2023-04-05 thomas struct got_pathlist_entry *pe;
1023 57b6056a 2023-04-05 thomas char *new;
1025 57b6056a 2023-04-05 thomas if (conf_protect_ref_namespace(&new,
1026 57b6056a 2023-04-05 thomas &repo->protected_branch_namespaces, namespace) == -1)
1027 57b6056a 2023-04-05 thomas return -1;
1029 57b6056a 2023-04-05 thomas TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1030 57b6056a 2023-04-05 thomas if (strcmp(pe->path, new) == 0) {
1031 3f0853b8 2023-04-07 thomas yyerror("duplicate protected namespace %s", namespace);
1032 57b6056a 2023-04-05 thomas return -1;
1036 57b6056a 2023-04-05 thomas return 0;
1039 6d7eb4f7 2023-04-04 thomas static int
1040 6d7eb4f7 2023-04-04 thomas conf_protect_branch(struct gotd_repo *repo, char *branchname)
1042 6d7eb4f7 2023-04-04 thomas const struct got_error *error;
1043 a4435ef0 2023-04-05 thomas struct got_pathlist_entry *new;
1044 6d7eb4f7 2023-04-04 thomas char *refname;
1046 6d7eb4f7 2023-04-04 thomas if (strncmp(branchname, "refs/heads/", 11) != 0) {
1047 6d7eb4f7 2023-04-04 thomas if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1048 6d7eb4f7 2023-04-04 thomas yyerror("asprintf: %s", strerror(errno));
1049 6d7eb4f7 2023-04-04 thomas return -1;
1051 6d7eb4f7 2023-04-04 thomas } else {
1052 6d7eb4f7 2023-04-04 thomas refname = strdup(branchname);
1053 6d7eb4f7 2023-04-04 thomas if (refname == NULL) {
1054 6d7eb4f7 2023-04-04 thomas yyerror("strdup: %s", strerror(errno));
1055 6d7eb4f7 2023-04-04 thomas return -1;
1059 6d7eb4f7 2023-04-04 thomas if (!refname_is_valid(refname)) {
1060 6d7eb4f7 2023-04-04 thomas free(refname);
1061 6d7eb4f7 2023-04-04 thomas return -1;
1064 a4435ef0 2023-04-05 thomas error = got_pathlist_insert(&new, &repo->protected_branches,
1065 6d7eb4f7 2023-04-04 thomas refname, NULL);
1066 a4435ef0 2023-04-05 thomas if (error || new == NULL) {
1067 a4435ef0 2023-04-05 thomas free(refname);
1068 a4435ef0 2023-04-05 thomas if (error)
1069 a4435ef0 2023-04-05 thomas yyerror("got_pathlist_insert: %s", error->msg);
1071 a4435ef0 2023-04-05 thomas yyerror("duplicate protect branch %s", branchname);
1072 6d7eb4f7 2023-04-04 thomas return -1;
1075 6d7eb4f7 2023-04-04 thomas return 0;
1079 3efd8e31 2022-10-23 thomas symset(const char *nam, const char *val, int persist)
1081 3efd8e31 2022-10-23 thomas struct sym *sym;
1083 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1084 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0)
1088 3efd8e31 2022-10-23 thomas if (sym != NULL) {
1089 3efd8e31 2022-10-23 thomas if (sym->persist == 1)
1090 3efd8e31 2022-10-23 thomas return (0);
1092 3efd8e31 2022-10-23 thomas free(sym->nam);
1093 3efd8e31 2022-10-23 thomas free(sym->val);
1094 3efd8e31 2022-10-23 thomas TAILQ_REMOVE(&symhead, sym, entry);
1095 3efd8e31 2022-10-23 thomas free(sym);
1098 3efd8e31 2022-10-23 thomas sym = calloc(1, sizeof(*sym));
1099 3efd8e31 2022-10-23 thomas if (sym == NULL)
1100 3efd8e31 2022-10-23 thomas return (-1);
1102 3efd8e31 2022-10-23 thomas sym->nam = strdup(nam);
1103 3efd8e31 2022-10-23 thomas if (sym->nam == NULL) {
1104 3efd8e31 2022-10-23 thomas free(sym);
1105 3efd8e31 2022-10-23 thomas return (-1);
1107 3efd8e31 2022-10-23 thomas sym->val = strdup(val);
1108 3efd8e31 2022-10-23 thomas if (sym->val == NULL) {
1109 3efd8e31 2022-10-23 thomas free(sym->nam);
1110 3efd8e31 2022-10-23 thomas free(sym);
1111 3efd8e31 2022-10-23 thomas return (-1);
1113 3efd8e31 2022-10-23 thomas sym->used = 0;
1114 3efd8e31 2022-10-23 thomas sym->persist = persist;
1115 3efd8e31 2022-10-23 thomas TAILQ_INSERT_TAIL(&symhead, sym, entry);
1116 3efd8e31 2022-10-23 thomas return (0);
1120 3efd8e31 2022-10-23 thomas symget(const char *nam)
1122 3efd8e31 2022-10-23 thomas struct sym *sym;
1124 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(sym, &symhead, entry) {
1125 3efd8e31 2022-10-23 thomas if (strcmp(nam, sym->nam) == 0) {
1126 3efd8e31 2022-10-23 thomas sym->used = 1;
1127 3efd8e31 2022-10-23 thomas return (sym->val);
1130 3efd8e31 2022-10-23 thomas return (NULL);
1133 5dcb3a43 2023-04-01 thomas struct gotd_repo *
1134 5dcb3a43 2023-04-01 thomas gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1136 5dcb3a43 2023-04-01 thomas struct gotd_repo *repo;
1137 5dcb3a43 2023-04-01 thomas size_t namelen;
1139 5dcb3a43 2023-04-01 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1140 5dcb3a43 2023-04-01 thomas namelen = strlen(repo->name);
1141 5dcb3a43 2023-04-01 thomas if (strncmp(repo->name, repo_name, namelen) != 0)
1142 5dcb3a43 2023-04-01 thomas continue;
1143 5dcb3a43 2023-04-01 thomas if (repo_name[namelen] == '\0' ||
1144 5dcb3a43 2023-04-01 thomas strcmp(&repo_name[namelen], ".git") == 0)
1145 5dcb3a43 2023-04-01 thomas return repo;
1148 5dcb3a43 2023-04-01 thomas return NULL;
1151 6d7eb4f7 2023-04-04 thomas struct gotd_repo *
1152 6d7eb4f7 2023-04-04 thomas gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1154 6d7eb4f7 2023-04-04 thomas struct gotd_repo *repo;
1156 6d7eb4f7 2023-04-04 thomas TAILQ_FOREACH(repo, &gotd->repos, entry) {
1157 6d7eb4f7 2023-04-04 thomas if (strcmp(repo->path, repo_path) == 0)
1158 6d7eb4f7 2023-04-04 thomas return repo;
1161 65a36f17 2023-04-22 thomas return NULL;
1164 65a36f17 2023-04-22 thomas struct gotd_uid_connection_limit *
1165 65a36f17 2023-04-22 thomas gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1166 65a36f17 2023-04-22 thomas size_t nlimits, uid_t uid)
1168 65a36f17 2023-04-22 thomas /* This array is always sorted to allow for binary search. */
1169 65a36f17 2023-04-22 thomas int i, left = 0, right = nlimits - 1;
1171 65a36f17 2023-04-22 thomas while (left <= right) {
1172 65a36f17 2023-04-22 thomas i = ((left + right) / 2);
1173 65a36f17 2023-04-22 thomas if (limits[i].uid == uid)
1174 65a36f17 2023-04-22 thomas return &limits[i];
1175 65a36f17 2023-04-22 thomas if (limits[i].uid > uid)
1176 65a36f17 2023-04-22 thomas left = i + 1;
1178 65a36f17 2023-04-22 thomas right = i - 1;
1181 6d7eb4f7 2023-04-04 thomas return NULL;
1185 48488136 2023-04-22 thomas gotd_parseuid(const char *s, uid_t *uid)
1187 48488136 2023-04-22 thomas struct passwd *pw;
1188 48488136 2023-04-22 thomas const char *errstr;
1190 48488136 2023-04-22 thomas if ((pw = getpwnam(s)) != NULL) {
1191 48488136 2023-04-22 thomas *uid = pw->pw_uid;
1192 48488136 2023-04-22 thomas if (*uid == UID_MAX)
1193 48488136 2023-04-22 thomas return -1;
1194 48488136 2023-04-22 thomas return 0;
1196 48488136 2023-04-22 thomas *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1197 48488136 2023-04-22 thomas if (errstr)
1198 48488136 2023-04-22 thomas return -1;
1199 48488136 2023-04-22 thomas return 0;