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