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);
76 ba97b2d7 2024-03-20 stsp static char *port_sprintf(int);
78 13b2bc37 2022-10-23 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
79 13b2bc37 2022-10-23 stsp struct sym {
80 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(sym) entry;
82 13b2bc37 2022-10-23 stsp int persist;
87 13b2bc37 2022-10-23 stsp int symset(const char *, const char *, int);
88 13b2bc37 2022-10-23 stsp char *symget(const char *);
90 13b2bc37 2022-10-23 stsp static int errors;
92 13b2bc37 2022-10-23 stsp static struct gotd *gotd;
93 13b2bc37 2022-10-23 stsp static struct gotd_repo *new_repo;
94 fc89c900 2023-01-03 op static int conf_limit_user_connections(const char *, int);
95 13b2bc37 2022-10-23 stsp static struct gotd_repo *conf_new_repo(const char *);
96 533abaaa 2022-11-18 op static void conf_new_access_rule(struct gotd_repo *,
97 533abaaa 2022-11-18 op enum gotd_access, int, char *);
98 f850236e 2023-04-05 stsp static int conf_protect_ref_namespace(char **,
99 9afa3de2 2023-04-04 stsp struct got_pathlist_head *, char *);
100 9afa3de2 2023-04-04 stsp static int conf_protect_tag_namespace(struct gotd_repo *,
102 9afa3de2 2023-04-04 stsp static int conf_protect_branch_namespace(
103 9afa3de2 2023-04-04 stsp struct gotd_repo *, char *);
104 9afa3de2 2023-04-04 stsp static int conf_protect_branch(struct gotd_repo *,
106 ba97b2d7 2024-03-20 stsp static int conf_notify_branch(struct gotd_repo *,
108 ba97b2d7 2024-03-20 stsp static int conf_notify_ref_namespace(struct gotd_repo *,
110 ba97b2d7 2024-03-20 stsp static int conf_notify_email(struct gotd_repo *,
111 ba97b2d7 2024-03-20 stsp char *, char *, char *, char *, char *);
112 ba97b2d7 2024-03-20 stsp static int conf_notify_http(struct gotd_repo *,
113 ba97b2d7 2024-03-20 stsp char *, char *, char *);
114 13b2bc37 2022-10-23 stsp static enum gotd_procid gotd_proc_id;
116 13b2bc37 2022-10-23 stsp typedef struct {
118 13b2bc37 2022-10-23 stsp long long number;
119 13b2bc37 2022-10-23 stsp char *string;
120 40b85cca 2023-01-03 stsp struct timeval tv;
122 13b2bc37 2022-10-23 stsp int lineno;
127 83577462 2023-01-05 stsp %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
128 40b85cca 2023-01-03 stsp %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
129 ba97b2d7 2024-03-20 stsp %token PROTECT NAMESPACE BRANCH TAG REFERENCE RELAY PORT
130 ba97b2d7 2024-03-20 stsp %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD
132 13b2bc37 2022-10-23 stsp %token <v.string> STRING
133 13b2bc37 2022-10-23 stsp %token <v.number> NUMBER
134 40b85cca 2023-01-03 stsp %type <v.tv> timeout
139 13b2bc37 2022-10-23 stsp | grammar '\n'
140 b1c09054 2023-11-04 stsp | grammar varset '\n'
141 13b2bc37 2022-10-23 stsp | grammar main '\n'
142 13b2bc37 2022-10-23 stsp | grammar repository '\n'
145 b1c09054 2023-11-04 stsp varset : STRING '=' STRING {
146 b1c09054 2023-11-04 stsp char *s = $1;
147 b1c09054 2023-11-04 stsp while (*s++) {
148 b1c09054 2023-11-04 stsp if (isspace((unsigned char)*s)) {
149 b1c09054 2023-11-04 stsp yyerror("macro name cannot contain "
150 b1c09054 2023-11-04 stsp "whitespace");
156 b1c09054 2023-11-04 stsp if (symset($1, $3, 0) == -1)
157 b1c09054 2023-11-04 stsp fatal("cannot store variable");
163 40b85cca 2023-01-03 stsp timeout : NUMBER {
164 40b85cca 2023-01-03 stsp if ($1 < 0) {
165 40b85cca 2023-01-03 stsp yyerror("invalid timeout: %lld", $1);
168 40b85cca 2023-01-03 stsp $$.tv_sec = $1;
169 40b85cca 2023-01-03 stsp $$.tv_usec = 0;
172 2be11cde 2023-01-03 op const char *errstr;
173 2be11cde 2023-01-03 op const char *type = "seconds";
177 2be11cde 2023-01-03 op if (*$1 == '\0') {
178 2be11cde 2023-01-03 op yyerror("invalid number of seconds: %s", $1);
183 2be11cde 2023-01-03 op len = strlen($1);
184 2be11cde 2023-01-03 op switch ($1[len - 1]) {
187 2be11cde 2023-01-03 op $1[len - 1] = '\0';
191 2be11cde 2023-01-03 op type = "minutes";
193 2be11cde 2023-01-03 op $1[len - 1] = '\0';
197 2be11cde 2023-01-03 op type = "hours";
198 2be11cde 2023-01-03 op mul = 60 * 60;
199 2be11cde 2023-01-03 op $1[len - 1] = '\0';
203 2be11cde 2023-01-03 op $$.tv_usec = 0;
204 71cd355c 2023-01-03 op $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
205 2be11cde 2023-01-03 op if (errstr) {
206 2be11cde 2023-01-03 op yyerror("number of %s is %s: %s", type,
212 2be11cde 2023-01-03 op $$.tv_sec *= mul;
217 83577462 2023-01-05 stsp main : LISTEN ON STRING {
218 abe844e2 2023-01-18 op if (!got_path_is_absolute($3))
219 abe844e2 2023-01-18 op yyerror("bad unix socket path \"%s\": "
220 abe844e2 2023-01-18 op "must be an absolute path", $3);
222 d93ecf7d 2022-12-14 stsp if (gotd_proc_id == PROC_LISTEN) {
223 83577462 2023-01-05 stsp if (strlcpy(gotd->unix_socket_path, $3,
224 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >=
225 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) {
226 13b2bc37 2022-10-23 stsp yyerror("%s: unix socket path too long",
234 13b2bc37 2022-10-23 stsp | USER STRING {
235 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, $2,
236 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >=
237 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) {
238 13b2bc37 2022-10-23 stsp yyerror("%s: user name too long", __func__);
244 40b85cca 2023-01-03 stsp | connection
247 40b85cca 2023-01-03 stsp connection : CONNECTION '{' optnl conflags_l '}'
248 40b85cca 2023-01-03 stsp | CONNECTION conflags
250 40b85cca 2023-01-03 stsp conflags_l : conflags optnl conflags_l
251 40b85cca 2023-01-03 stsp | conflags optnl
254 40b85cca 2023-01-03 stsp conflags : REQUEST TIMEOUT timeout {
255 46e48ac7 2023-01-03 stsp if ($3.tv_sec <= 0) {
256 46e48ac7 2023-01-03 stsp yyerror("invalid timeout: %lld", $3.tv_sec);
259 40b85cca 2023-01-03 stsp memcpy(&gotd->request_timeout, &$3,
260 40b85cca 2023-01-03 stsp sizeof(gotd->request_timeout));
262 40b85cca 2023-01-03 stsp | LIMIT USER STRING NUMBER {
263 40b85cca 2023-01-03 stsp if (gotd_proc_id == PROC_LISTEN &&
264 40b85cca 2023-01-03 stsp conf_limit_user_connections($3, $4) == -1) {
272 9afa3de2 2023-04-04 stsp protect : PROTECT '{' optnl protectflags_l '}'
273 9afa3de2 2023-04-04 stsp | PROTECT protectflags
275 9afa3de2 2023-04-04 stsp protectflags_l : protectflags optnl protectflags_l
276 9afa3de2 2023-04-04 stsp | protectflags optnl
279 9afa3de2 2023-04-04 stsp protectflags : TAG 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_tag_namespace(new_repo, $3)) {
289 9afa3de2 2023-04-04 stsp | BRANCH NAMESPACE STRING {
290 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
291 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
292 9afa3de2 2023-04-04 stsp if (conf_protect_branch_namespace(new_repo,
300 9afa3de2 2023-04-04 stsp | BRANCH STRING {
301 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
302 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
303 9afa3de2 2023-04-04 stsp if (conf_protect_branch(new_repo, $2)) {
312 ba97b2d7 2024-03-20 stsp notify : NOTIFY '{' optnl notifyflags_l '}'
313 ba97b2d7 2024-03-20 stsp | NOTIFY notifyflags
315 ba97b2d7 2024-03-20 stsp notifyflags_l : notifyflags optnl notifyflags_l
316 ba97b2d7 2024-03-20 stsp | notifyflags optnl
319 ba97b2d7 2024-03-20 stsp notifyflags : BRANCH STRING {
320 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
321 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
322 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
323 ba97b2d7 2024-03-20 stsp if (conf_notify_branch(new_repo, $2)) {
330 ba97b2d7 2024-03-20 stsp | REFERENCE NAMESPACE STRING {
331 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
332 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
333 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
334 ba97b2d7 2024-03-20 stsp if (conf_notify_ref_namespace(new_repo, $3)) {
341 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING {
342 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
343 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
344 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
345 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
346 ba97b2d7 2024-03-20 stsp NULL, NULL, NULL)) {
353 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING {
354 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
355 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
356 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
357 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
358 ba97b2d7 2024-03-20 stsp NULL, NULL, NULL)) {
367 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING {
368 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
369 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
370 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
371 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
372 ba97b2d7 2024-03-20 stsp $6, NULL, NULL)) {
381 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING {
382 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
383 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
384 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
385 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
386 ba97b2d7 2024-03-20 stsp $8, NULL, NULL)) {
397 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING RELAY STRING {
398 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
399 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
400 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
401 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
402 ba97b2d7 2024-03-20 stsp NULL, $5, NULL)) {
411 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING RELAY STRING {
412 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
413 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
414 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
415 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
416 ba97b2d7 2024-03-20 stsp NULL, $7, NULL)) {
427 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING RELAY STRING {
428 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
429 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
430 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
431 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
432 ba97b2d7 2024-03-20 stsp $6, $8, NULL)) {
443 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
444 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
445 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
446 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
447 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
448 ba97b2d7 2024-03-20 stsp $8, $10, NULL)) {
461 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING RELAY STRING PORT STRING {
462 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
463 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
464 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
465 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
466 ba97b2d7 2024-03-20 stsp NULL, $5, $7)) {
477 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
478 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
479 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
480 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
481 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
482 ba97b2d7 2024-03-20 stsp NULL, $7, $9)) {
495 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
496 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
497 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
498 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
499 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
500 ba97b2d7 2024-03-20 stsp $6, $8, $10)) {
513 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
514 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
515 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
516 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
517 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
518 ba97b2d7 2024-03-20 stsp $8, $10, $12)) {
533 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING RELAY STRING PORT NUMBER {
534 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
535 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
536 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
537 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
538 ba97b2d7 2024-03-20 stsp NULL, $5, port_sprintf($7))) {
547 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
548 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
549 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
550 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
551 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
552 ba97b2d7 2024-03-20 stsp NULL, $7, port_sprintf($9))) {
563 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
564 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
565 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
566 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
567 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
568 ba97b2d7 2024-03-20 stsp $6, $8, port_sprintf($10))) {
579 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
580 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
581 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
582 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
583 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
584 ba97b2d7 2024-03-20 stsp $8, $10, port_sprintf($12))) {
597 ba97b2d7 2024-03-20 stsp | URL STRING {
598 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
599 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
600 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
601 ba97b2d7 2024-03-20 stsp if (conf_notify_http(new_repo, $2, NULL,
609 ba97b2d7 2024-03-20 stsp | URL STRING USER STRING PASSWORD STRING {
610 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
611 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
612 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
613 ba97b2d7 2024-03-20 stsp if (conf_notify_http(new_repo, $2, $4, $6)) {
626 13b2bc37 2022-10-23 stsp repository : REPOSITORY STRING {
627 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
629 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
630 13b2bc37 2022-10-23 stsp if (strcmp(repo->name, $2) == 0) {
631 13b2bc37 2022-10-23 stsp yyerror("duplicate repository '%s'", $2);
637 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
638 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
639 4b3827cd 2023-07-08 stsp gotd_proc_id == PROC_REPO_WRITE ||
640 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
641 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_GITWRAPPER |
642 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
643 13b2bc37 2022-10-23 stsp new_repo = conf_new_repo($2);
646 13b2bc37 2022-10-23 stsp } '{' optnl repoopts2 '}' {
650 13b2bc37 2022-10-23 stsp repoopts1 : PATH STRING {
651 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
652 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
653 4b3827cd 2023-07-08 stsp gotd_proc_id == PROC_REPO_WRITE ||
654 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
655 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_GITWRAPPER ||
656 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
657 13b2bc37 2022-10-23 stsp if (!got_path_is_absolute($2)) {
658 13b2bc37 2022-10-23 stsp yyerror("%s: path %s is not absolute",
659 13b2bc37 2022-10-23 stsp __func__, $2);
663 9b7f22a6 2023-01-08 stsp if (realpath($2, new_repo->path) == NULL) {
665 4b3827cd 2023-07-08 stsp * To give admins a chance to create
666 4b3827cd 2023-07-08 stsp * missing repositories at run-time
667 4b3827cd 2023-07-08 stsp * we only warn about ENOENT here.
669 4b3827cd 2023-07-08 stsp * And ignore 'permission denied' when
670 4b3827cd 2023-07-08 stsp * running in gitwrapper. Users may be
671 4b3827cd 2023-07-08 stsp * able to access this repository via
672 4b3827cd 2023-07-08 stsp * gotd regardless.
674 4b3827cd 2023-07-08 stsp if (errno == ENOENT) {
675 4b3827cd 2023-07-08 stsp yyerror("realpath %s: %s", $2,
676 4b3827cd 2023-07-08 stsp strerror(errno));
677 4b3827cd 2023-07-08 stsp } else if (errno != EACCES ||
678 4b3827cd 2023-07-08 stsp gotd_proc_id != PROC_GITWRAPPER) {
679 4b3827cd 2023-07-08 stsp yyerror("realpath %s: %s", $2,
680 4b3827cd 2023-07-08 stsp strerror(errno));
685 4b3827cd 2023-07-08 stsp if (strlcpy(new_repo->path, $2,
686 859316d0 2023-04-12 stsp sizeof(new_repo->path)) >=
687 859316d0 2023-04-12 stsp sizeof(new_repo->path))
688 859316d0 2023-04-12 stsp yyerror("path too long");
693 0ccf3acb 2022-11-16 stsp | PERMIT RO STRING {
694 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
695 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
696 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
700 0ccf3acb 2022-11-16 stsp | PERMIT RW STRING {
701 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
702 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
703 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED,
704 0ccf3acb 2022-11-16 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
708 0ccf3acb 2022-11-16 stsp | DENY STRING {
709 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
710 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
711 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_DENIED, 0, $2);
719 13b2bc37 2022-10-23 stsp repoopts2 : repoopts2 repoopts1 nl
720 13b2bc37 2022-10-23 stsp | repoopts1 optnl
723 13b2bc37 2022-10-23 stsp nl : '\n' optnl
726 13b2bc37 2022-10-23 stsp optnl : '\n' optnl /* zero or more newlines */
727 13b2bc37 2022-10-23 stsp | /* empty */
732 13b2bc37 2022-10-23 stsp struct keywords {
733 13b2bc37 2022-10-23 stsp const char *k_name;
738 13b2bc37 2022-10-23 stsp yyerror(const char *fmt, ...)
740 13b2bc37 2022-10-23 stsp va_list ap;
743 13b2bc37 2022-10-23 stsp file->errors++;
744 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
745 13b2bc37 2022-10-23 stsp if (vasprintf(&msg, fmt, ap) == -1)
746 13b2bc37 2022-10-23 stsp fatalx("yyerror vasprintf");
747 13b2bc37 2022-10-23 stsp va_end(ap);
748 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
750 13b2bc37 2022-10-23 stsp return (0);
754 13b2bc37 2022-10-23 stsp kw_cmp(const void *k, const void *e)
756 13b2bc37 2022-10-23 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
760 13b2bc37 2022-10-23 stsp lookup(char *s)
762 13b2bc37 2022-10-23 stsp /* This has to be sorted always. */
763 13b2bc37 2022-10-23 stsp static const struct keywords keywords[] = {
764 9afa3de2 2023-04-04 stsp { "branch", BRANCH },
765 40b85cca 2023-01-03 stsp { "connection", CONNECTION },
766 0ccf3acb 2022-11-16 stsp { "deny", DENY },
767 ba97b2d7 2024-03-20 stsp { "email", EMAIL },
768 ba97b2d7 2024-03-20 stsp { "from", FROM },
769 40b85cca 2023-01-03 stsp { "limit", LIMIT },
770 83577462 2023-01-05 stsp { "listen", LISTEN },
771 9afa3de2 2023-04-04 stsp { "namespace", NAMESPACE },
772 ba97b2d7 2024-03-20 stsp { "notify", NOTIFY },
773 13b2bc37 2022-10-23 stsp { "on", ON },
774 ba97b2d7 2024-03-20 stsp { "password", PASSWORD },
775 13b2bc37 2022-10-23 stsp { "path", PATH },
776 0ccf3acb 2022-11-16 stsp { "permit", PERMIT },
777 ba97b2d7 2024-03-20 stsp { "port", PORT },
778 9afa3de2 2023-04-04 stsp { "protect", PROTECT },
779 ba97b2d7 2024-03-20 stsp { "reference", REFERENCE },
780 ba97b2d7 2024-03-20 stsp { "relay", RELAY },
781 ba97b2d7 2024-03-20 stsp { "reply", REPLY },
782 13b2bc37 2022-10-23 stsp { "repository", REPOSITORY },
783 40b85cca 2023-01-03 stsp { "request", REQUEST },
784 0ccf3acb 2022-11-16 stsp { "ro", RO },
785 0ccf3acb 2022-11-16 stsp { "rw", RW },
786 9afa3de2 2023-04-04 stsp { "tag", TAG },
787 40b85cca 2023-01-03 stsp { "timeout", TIMEOUT },
788 ba97b2d7 2024-03-20 stsp { "to", TO },
789 ba97b2d7 2024-03-20 stsp { "url", URL },
790 13b2bc37 2022-10-23 stsp { "user", USER },
792 13b2bc37 2022-10-23 stsp const struct keywords *p;
794 13b2bc37 2022-10-23 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
795 13b2bc37 2022-10-23 stsp sizeof(keywords[0]), kw_cmp);
798 13b2bc37 2022-10-23 stsp return (p->k_val);
800 13b2bc37 2022-10-23 stsp return (STRING);
803 13b2bc37 2022-10-23 stsp #define MAXPUSHBACK 128
805 13b2bc37 2022-10-23 stsp unsigned char *parsebuf;
806 13b2bc37 2022-10-23 stsp int parseindex;
807 13b2bc37 2022-10-23 stsp unsigned char pushback_buffer[MAXPUSHBACK];
808 13b2bc37 2022-10-23 stsp int pushback_index = 0;
811 13b2bc37 2022-10-23 stsp lgetc(int quotec)
813 13b2bc37 2022-10-23 stsp int c, next;
815 13b2bc37 2022-10-23 stsp if (parsebuf) {
816 13b2bc37 2022-10-23 stsp /* Read character from the parsebuffer instead of input. */
817 13b2bc37 2022-10-23 stsp if (parseindex >= 0) {
818 13b2bc37 2022-10-23 stsp c = parsebuf[parseindex++];
819 13b2bc37 2022-10-23 stsp if (c != '\0')
820 13b2bc37 2022-10-23 stsp return (c);
821 13b2bc37 2022-10-23 stsp parsebuf = NULL;
823 13b2bc37 2022-10-23 stsp parseindex++;
826 13b2bc37 2022-10-23 stsp if (pushback_index)
827 13b2bc37 2022-10-23 stsp return (pushback_buffer[--pushback_index]);
829 13b2bc37 2022-10-23 stsp if (quotec) {
830 13b2bc37 2022-10-23 stsp c = getc(file->stream);
831 13b2bc37 2022-10-23 stsp if (c == EOF)
832 13b2bc37 2022-10-23 stsp yyerror("reached end of file while parsing "
833 13b2bc37 2022-10-23 stsp "quoted string");
834 13b2bc37 2022-10-23 stsp return (c);
837 13b2bc37 2022-10-23 stsp c = getc(file->stream);
838 13b2bc37 2022-10-23 stsp while (c == '\\') {
839 13b2bc37 2022-10-23 stsp next = getc(file->stream);
840 13b2bc37 2022-10-23 stsp if (next != '\n') {
844 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
845 13b2bc37 2022-10-23 stsp file->lineno++;
846 13b2bc37 2022-10-23 stsp c = getc(file->stream);
849 13b2bc37 2022-10-23 stsp return (c);
853 13b2bc37 2022-10-23 stsp lungetc(int c)
855 13b2bc37 2022-10-23 stsp if (c == EOF)
856 13b2bc37 2022-10-23 stsp return (EOF);
857 13b2bc37 2022-10-23 stsp if (parsebuf) {
858 13b2bc37 2022-10-23 stsp parseindex--;
859 13b2bc37 2022-10-23 stsp if (parseindex >= 0)
860 13b2bc37 2022-10-23 stsp return (c);
862 13b2bc37 2022-10-23 stsp if (pushback_index < MAXPUSHBACK-1)
863 13b2bc37 2022-10-23 stsp return (pushback_buffer[pushback_index++] = c);
865 13b2bc37 2022-10-23 stsp return (EOF);
869 13b2bc37 2022-10-23 stsp findeol(void)
873 13b2bc37 2022-10-23 stsp parsebuf = NULL;
875 13b2bc37 2022-10-23 stsp /* Skip to either EOF or the first real EOL. */
876 13b2bc37 2022-10-23 stsp while (1) {
877 13b2bc37 2022-10-23 stsp if (pushback_index)
878 13b2bc37 2022-10-23 stsp c = pushback_buffer[--pushback_index];
880 13b2bc37 2022-10-23 stsp c = lgetc(0);
881 13b2bc37 2022-10-23 stsp if (c == '\n') {
882 13b2bc37 2022-10-23 stsp file->lineno++;
885 13b2bc37 2022-10-23 stsp if (c == EOF)
888 13b2bc37 2022-10-23 stsp return (ERROR);
892 13b2bc37 2022-10-23 stsp yylex(void)
894 13b2bc37 2022-10-23 stsp unsigned char buf[8096];
895 13b2bc37 2022-10-23 stsp unsigned char *p, *val;
896 13b2bc37 2022-10-23 stsp int quotec, next, c;
901 13b2bc37 2022-10-23 stsp c = lgetc(0);
902 13b2bc37 2022-10-23 stsp while (c == ' ' || c == '\t')
903 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
905 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
906 13b2bc37 2022-10-23 stsp if (c == '#') {
907 13b2bc37 2022-10-23 stsp c = lgetc(0);
908 13b2bc37 2022-10-23 stsp while (c != '\n' && c != EOF)
909 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
911 13b2bc37 2022-10-23 stsp if (c == '$' && parsebuf == NULL) {
912 13b2bc37 2022-10-23 stsp while (1) {
913 13b2bc37 2022-10-23 stsp c = lgetc(0);
914 13b2bc37 2022-10-23 stsp if (c == EOF)
915 13b2bc37 2022-10-23 stsp return (0);
917 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
918 13b2bc37 2022-10-23 stsp yyerror("string too long");
919 13b2bc37 2022-10-23 stsp return (findeol());
921 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == '_') {
926 13b2bc37 2022-10-23 stsp lungetc(c);
929 13b2bc37 2022-10-23 stsp val = symget(buf);
930 13b2bc37 2022-10-23 stsp if (val == NULL) {
931 13b2bc37 2022-10-23 stsp yyerror("macro '%s' not defined", buf);
932 13b2bc37 2022-10-23 stsp return (findeol());
934 13b2bc37 2022-10-23 stsp parsebuf = val;
935 13b2bc37 2022-10-23 stsp parseindex = 0;
939 13b2bc37 2022-10-23 stsp switch (c) {
942 13b2bc37 2022-10-23 stsp quotec = c;
943 13b2bc37 2022-10-23 stsp while (1) {
944 13b2bc37 2022-10-23 stsp c = lgetc(quotec);
945 13b2bc37 2022-10-23 stsp if (c == EOF)
946 13b2bc37 2022-10-23 stsp return (0);
947 13b2bc37 2022-10-23 stsp if (c == '\n') {
948 13b2bc37 2022-10-23 stsp file->lineno++;
950 13b2bc37 2022-10-23 stsp } else if (c == '\\') {
951 13b2bc37 2022-10-23 stsp next = lgetc(quotec);
952 13b2bc37 2022-10-23 stsp if (next == EOF)
953 13b2bc37 2022-10-23 stsp return (0);
954 13b2bc37 2022-10-23 stsp if (next == quotec || c == ' ' || c == '\t')
956 13b2bc37 2022-10-23 stsp else if (next == '\n') {
957 13b2bc37 2022-10-23 stsp file->lineno++;
960 13b2bc37 2022-10-23 stsp lungetc(next);
961 13b2bc37 2022-10-23 stsp } else if (c == quotec) {
964 13b2bc37 2022-10-23 stsp } else if (c == '\0') {
965 13b2bc37 2022-10-23 stsp yyerror("syntax error");
966 13b2bc37 2022-10-23 stsp return (findeol());
968 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
969 13b2bc37 2022-10-23 stsp yyerror("string too long");
970 13b2bc37 2022-10-23 stsp return (findeol());
974 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
975 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
976 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
977 13b2bc37 2022-10-23 stsp return (STRING);
980 13b2bc37 2022-10-23 stsp #define allowed_to_end_number(x) \
981 13b2bc37 2022-10-23 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
983 13b2bc37 2022-10-23 stsp if (c == '-' || isdigit(c)) {
986 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
987 13b2bc37 2022-10-23 stsp yyerror("string too long");
988 13b2bc37 2022-10-23 stsp return (findeol());
990 13b2bc37 2022-10-23 stsp c = lgetc(0);
991 13b2bc37 2022-10-23 stsp } while (c != EOF && isdigit(c));
992 13b2bc37 2022-10-23 stsp lungetc(c);
993 13b2bc37 2022-10-23 stsp if (p == buf + 1 && buf[0] == '-')
994 13b2bc37 2022-10-23 stsp goto nodigits;
995 13b2bc37 2022-10-23 stsp if (c == EOF || allowed_to_end_number(c)) {
996 13b2bc37 2022-10-23 stsp const char *errstr = NULL;
999 13b2bc37 2022-10-23 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
1000 13b2bc37 2022-10-23 stsp LLONG_MAX, &errstr);
1001 13b2bc37 2022-10-23 stsp if (errstr) {
1002 13b2bc37 2022-10-23 stsp yyerror("\"%s\" invalid number: %s",
1003 13b2bc37 2022-10-23 stsp buf, errstr);
1004 13b2bc37 2022-10-23 stsp return (findeol());
1006 13b2bc37 2022-10-23 stsp return (NUMBER);
1009 13b2bc37 2022-10-23 stsp while (p > buf + 1)
1010 13b2bc37 2022-10-23 stsp lungetc(*--p);
1012 13b2bc37 2022-10-23 stsp if (c == '-')
1013 13b2bc37 2022-10-23 stsp return (c);
1017 13b2bc37 2022-10-23 stsp #define allowed_in_string(x) \
1018 13b2bc37 2022-10-23 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1019 13b2bc37 2022-10-23 stsp x != '{' && x != '}' && \
1020 13b2bc37 2022-10-23 stsp x != '!' && x != '=' && x != '#' && \
1021 13b2bc37 2022-10-23 stsp x != ','))
1023 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == ':' || c == '_') {
1026 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
1027 13b2bc37 2022-10-23 stsp yyerror("string too long");
1028 13b2bc37 2022-10-23 stsp return (findeol());
1030 13b2bc37 2022-10-23 stsp c = lgetc(0);
1031 13b2bc37 2022-10-23 stsp } while (c != EOF && (allowed_in_string(c)));
1032 13b2bc37 2022-10-23 stsp lungetc(c);
1033 13b2bc37 2022-10-23 stsp *p = '\0';
1034 13b2bc37 2022-10-23 stsp token = lookup(buf);
1035 13b2bc37 2022-10-23 stsp if (token == STRING) {
1036 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
1037 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
1038 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
1040 13b2bc37 2022-10-23 stsp return (token);
1042 13b2bc37 2022-10-23 stsp if (c == '\n') {
1043 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
1044 13b2bc37 2022-10-23 stsp file->lineno++;
1046 13b2bc37 2022-10-23 stsp if (c == EOF)
1047 13b2bc37 2022-10-23 stsp return (0);
1048 13b2bc37 2022-10-23 stsp return (c);
1052 13b2bc37 2022-10-23 stsp check_file_secrecy(int fd, const char *fname)
1054 13b2bc37 2022-10-23 stsp struct stat st;
1056 13b2bc37 2022-10-23 stsp if (fstat(fd, &st)) {
1057 13b2bc37 2022-10-23 stsp log_warn("cannot stat %s", fname);
1058 13b2bc37 2022-10-23 stsp return (-1);
1060 13b2bc37 2022-10-23 stsp if (st.st_uid != 0 && st.st_uid != getuid()) {
1061 13b2bc37 2022-10-23 stsp log_warnx("%s: owner not root or current user", fname);
1062 13b2bc37 2022-10-23 stsp return (-1);
1064 13b2bc37 2022-10-23 stsp if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1065 13b2bc37 2022-10-23 stsp log_warnx("%s: group writable or world read/writable", fname);
1066 13b2bc37 2022-10-23 stsp return (-1);
1068 13b2bc37 2022-10-23 stsp return (0);
1071 13b2bc37 2022-10-23 stsp struct file *
1072 e9e0377f 2023-03-29 stsp newfile(const char *name, int secret, int required)
1074 13b2bc37 2022-10-23 stsp struct file *nfile;
1076 13b2bc37 2022-10-23 stsp nfile = calloc(1, sizeof(struct file));
1077 13b2bc37 2022-10-23 stsp if (nfile == NULL) {
1078 13b2bc37 2022-10-23 stsp log_warn("calloc");
1079 13b2bc37 2022-10-23 stsp return (NULL);
1081 13b2bc37 2022-10-23 stsp nfile->name = strdup(name);
1082 13b2bc37 2022-10-23 stsp if (nfile->name == NULL) {
1083 13b2bc37 2022-10-23 stsp log_warn("strdup");
1084 13b2bc37 2022-10-23 stsp free(nfile);
1085 13b2bc37 2022-10-23 stsp return (NULL);
1087 13b2bc37 2022-10-23 stsp nfile->stream = fopen(nfile->name, "r");
1088 13b2bc37 2022-10-23 stsp if (nfile->stream == NULL) {
1089 e9e0377f 2023-03-29 stsp if (required)
1090 e9e0377f 2023-03-29 stsp log_warn("open %s", nfile->name);
1091 13b2bc37 2022-10-23 stsp free(nfile->name);
1092 13b2bc37 2022-10-23 stsp free(nfile);
1093 13b2bc37 2022-10-23 stsp return (NULL);
1094 13b2bc37 2022-10-23 stsp } else if (secret &&
1095 13b2bc37 2022-10-23 stsp check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1096 13b2bc37 2022-10-23 stsp fclose(nfile->stream);
1097 13b2bc37 2022-10-23 stsp free(nfile->name);
1098 13b2bc37 2022-10-23 stsp free(nfile);
1099 13b2bc37 2022-10-23 stsp return (NULL);
1101 13b2bc37 2022-10-23 stsp nfile->lineno = 1;
1102 13b2bc37 2022-10-23 stsp return (nfile);
1105 13b2bc37 2022-10-23 stsp static void
1106 13b2bc37 2022-10-23 stsp closefile(struct file *xfile)
1108 13b2bc37 2022-10-23 stsp fclose(xfile->stream);
1109 13b2bc37 2022-10-23 stsp free(xfile->name);
1110 13b2bc37 2022-10-23 stsp free(xfile);
1114 13b2bc37 2022-10-23 stsp parse_config(const char *filename, enum gotd_procid proc_id,
1115 4b3827cd 2023-07-08 stsp struct gotd *env)
1117 13b2bc37 2022-10-23 stsp struct sym *sym, *next;
1118 3b706203 2023-01-02 stsp struct gotd_repo *repo;
1119 4b3827cd 2023-07-08 stsp int require_config_file = (proc_id != PROC_GITWRAPPER);
1121 13b2bc37 2022-10-23 stsp memset(env, 0, sizeof(*env));
1123 13b2bc37 2022-10-23 stsp gotd = env;
1124 13b2bc37 2022-10-23 stsp gotd_proc_id = proc_id;
1125 13b2bc37 2022-10-23 stsp TAILQ_INIT(&gotd->repos);
1127 13b2bc37 2022-10-23 stsp /* Apply default values. */
1128 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1129 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1130 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: unix socket path too long", __func__);
1131 13b2bc37 2022-10-23 stsp return -1;
1133 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, GOTD_USER,
1134 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1135 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: user name too long", __func__);
1136 13b2bc37 2022-10-23 stsp return -1;
1139 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1140 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_usec = 0;
1142 e9e0377f 2023-03-29 stsp file = newfile(filename, 0, require_config_file);
1143 3fa763e5 2023-03-01 stsp if (file == NULL)
1144 e9e0377f 2023-03-29 stsp return require_config_file ? -1 : 0;
1146 13b2bc37 2022-10-23 stsp yyparse();
1147 13b2bc37 2022-10-23 stsp errors = file->errors;
1148 13b2bc37 2022-10-23 stsp closefile(file);
1150 13b2bc37 2022-10-23 stsp /* Free macros and check which have not been used. */
1151 13b2bc37 2022-10-23 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1152 13b2bc37 2022-10-23 stsp if ((gotd->verbosity > 1) && !sym->used)
1153 13b2bc37 2022-10-23 stsp fprintf(stderr, "warning: macro '%s' not used\n",
1154 13b2bc37 2022-10-23 stsp sym->nam);
1155 13b2bc37 2022-10-23 stsp if (!sym->persist) {
1156 13b2bc37 2022-10-23 stsp free(sym->nam);
1157 13b2bc37 2022-10-23 stsp free(sym->val);
1158 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1159 13b2bc37 2022-10-23 stsp free(sym);
1163 13b2bc37 2022-10-23 stsp if (errors)
1164 13b2bc37 2022-10-23 stsp return (-1);
1166 3b706203 2023-01-02 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1167 3b706203 2023-01-02 stsp if (repo->path[0] == '\0') {
1168 2507ffb7 2023-01-02 stsp log_warnx("repository \"%s\": no path provided in "
1169 2507ffb7 2023-01-02 stsp "configuration file", repo->name);
1170 3b706203 2023-01-02 stsp return (-1);
1174 809a54db 2023-01-18 op if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1175 809a54db 2023-01-18 op log_warnx("no repository defined in configuration file");
1176 809a54db 2023-01-18 op return (-1);
1179 13b2bc37 2022-10-23 stsp return (0);
1182 40b85cca 2023-01-03 stsp static int
1183 40b85cca 2023-01-03 stsp uid_connection_limit_cmp(const void *pa, const void *pb)
1185 40b85cca 2023-01-03 stsp const struct gotd_uid_connection_limit *a = pa, *b = pb;
1187 40b85cca 2023-01-03 stsp if (a->uid < b->uid)
1188 40b85cca 2023-01-03 stsp return -1;
1189 40b85cca 2023-01-03 stsp else if (a->uid > b->uid);
1195 40b85cca 2023-01-03 stsp static int
1196 40b85cca 2023-01-03 stsp conf_limit_user_connections(const char *user, int maximum)
1198 40b85cca 2023-01-03 stsp uid_t uid;
1199 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *limit;
1200 40b85cca 2023-01-03 stsp size_t nlimits;
1202 40b85cca 2023-01-03 stsp if (maximum < 1) {
1203 40b85cca 2023-01-03 stsp yyerror("max connections cannot be smaller 1");
1204 40b85cca 2023-01-03 stsp return -1;
1206 40b85cca 2023-01-03 stsp if (maximum > GOTD_MAXCLIENTS) {
1207 40b85cca 2023-01-03 stsp yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1208 40b85cca 2023-01-03 stsp return -1;
1211 1963be61 2023-04-14 stsp if (gotd_parseuid(user, &uid) == -1) {
1212 40b85cca 2023-01-03 stsp yyerror("%s: no such user", user);
1213 40b85cca 2023-01-03 stsp return -1;
1216 40b85cca 2023-01-03 stsp limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1217 40b85cca 2023-01-03 stsp gotd->nconnection_limits, uid);
1218 40b85cca 2023-01-03 stsp if (limit) {
1219 40b85cca 2023-01-03 stsp limit->max_connections = maximum;
1223 40b85cca 2023-01-03 stsp limit = gotd->connection_limits;
1224 40b85cca 2023-01-03 stsp nlimits = gotd->nconnection_limits + 1;
1225 40b85cca 2023-01-03 stsp limit = reallocarray(limit, nlimits, sizeof(*limit));
1226 40b85cca 2023-01-03 stsp if (limit == NULL)
1227 40b85cca 2023-01-03 stsp fatal("reallocarray");
1229 40b85cca 2023-01-03 stsp limit[nlimits - 1].uid = uid;
1230 40b85cca 2023-01-03 stsp limit[nlimits - 1].max_connections = maximum;
1232 40b85cca 2023-01-03 stsp gotd->connection_limits = limit;
1233 40b85cca 2023-01-03 stsp gotd->nconnection_limits = nlimits;
1234 40b85cca 2023-01-03 stsp qsort(gotd->connection_limits, gotd->nconnection_limits,
1235 40b85cca 2023-01-03 stsp sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1240 13b2bc37 2022-10-23 stsp static struct gotd_repo *
1241 13b2bc37 2022-10-23 stsp conf_new_repo(const char *name)
1243 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
1245 7683f79a 2023-01-02 stsp if (name[0] == '\0') {
1246 7683f79a 2023-01-02 stsp fatalx("syntax error: empty repository name found in %s",
1247 7683f79a 2023-01-02 stsp file->name);
1250 2507ffb7 2023-01-02 stsp if (strchr(name, '\n') != NULL)
1251 2507ffb7 2023-01-02 stsp fatalx("repository names must not contain linefeeds: %s", name);
1253 13b2bc37 2022-10-23 stsp repo = calloc(1, sizeof(*repo));
1254 13b2bc37 2022-10-23 stsp if (repo == NULL)
1255 13b2bc37 2022-10-23 stsp fatalx("%s: calloc", __func__);
1257 0ccf3acb 2022-11-16 stsp STAILQ_INIT(&repo->rules);
1258 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_tag_namespaces);
1259 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branch_namespaces);
1260 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branches);
1261 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&repo->protected_branches);
1262 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&repo->notification_refs);
1263 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&repo->notification_ref_namespaces);
1264 ba97b2d7 2024-03-20 stsp STAILQ_INIT(&repo->notification_targets);
1266 13b2bc37 2022-10-23 stsp if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1267 13b2bc37 2022-10-23 stsp sizeof(repo->name))
1268 13b2bc37 2022-10-23 stsp fatalx("%s: strlcpy", __func__);
1270 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1271 13b2bc37 2022-10-23 stsp gotd->nrepos++;
1273 13b2bc37 2022-10-23 stsp return repo;
1276 0ccf3acb 2022-11-16 stsp static void
1277 0ccf3acb 2022-11-16 stsp conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1278 0ccf3acb 2022-11-16 stsp int authorization, char *identifier)
1280 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
1282 0ccf3acb 2022-11-16 stsp rule = calloc(1, sizeof(*rule));
1283 0ccf3acb 2022-11-16 stsp if (rule == NULL)
1284 0ccf3acb 2022-11-16 stsp fatal("calloc");
1286 0ccf3acb 2022-11-16 stsp rule->access = access;
1287 0ccf3acb 2022-11-16 stsp rule->authorization = authorization;
1288 0ccf3acb 2022-11-16 stsp rule->identifier = identifier;
1290 0ccf3acb 2022-11-16 stsp STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1293 9afa3de2 2023-04-04 stsp static int
1294 9afa3de2 2023-04-04 stsp refname_is_valid(char *refname)
1296 d571a176 2023-06-14 op if (strncmp(refname, "refs/", 5) != 0) {
1297 9afa3de2 2023-04-04 stsp yyerror("reference name must begin with \"refs/\": %s",
1302 9afa3de2 2023-04-04 stsp if (!got_ref_name_is_valid(refname)) {
1303 9afa3de2 2023-04-04 stsp yyerror("invalid reference name: %s", refname);
1310 9afa3de2 2023-04-04 stsp static int
1311 f850236e 2023-04-05 stsp conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1312 f850236e 2023-04-05 stsp char *namespace)
1314 9afa3de2 2023-04-04 stsp const struct got_error *error;
1315 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1318 f850236e 2023-04-05 stsp *new = NULL;
1320 9afa3de2 2023-04-04 stsp got_path_strip_trailing_slashes(namespace);
1321 9afa3de2 2023-04-04 stsp if (!refname_is_valid(namespace))
1322 9afa3de2 2023-04-04 stsp return -1;
1323 9afa3de2 2023-04-04 stsp if (asprintf(&s, "%s/", namespace) == -1) {
1324 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
1325 9afa3de2 2023-04-04 stsp return -1;
1328 f850236e 2023-04-05 stsp error = got_pathlist_insert(&pe, refs, s, NULL);
1329 f850236e 2023-04-05 stsp if (error || pe == NULL) {
1332 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1334 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1335 9afa3de2 2023-04-04 stsp return -1;
1342 9afa3de2 2023-04-04 stsp static int
1343 9afa3de2 2023-04-04 stsp conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1345 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1346 f850236e 2023-04-05 stsp char *new;
1348 f850236e 2023-04-05 stsp if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1349 f850236e 2023-04-05 stsp namespace) == -1)
1350 f850236e 2023-04-05 stsp return -1;
1352 f850236e 2023-04-05 stsp TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1353 f850236e 2023-04-05 stsp if (strcmp(pe->path, new) == 0) {
1354 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1355 f850236e 2023-04-05 stsp return -1;
1362 9afa3de2 2023-04-04 stsp static int
1363 9afa3de2 2023-04-04 stsp conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1365 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1366 f850236e 2023-04-05 stsp char *new;
1368 f850236e 2023-04-05 stsp if (conf_protect_ref_namespace(&new,
1369 f850236e 2023-04-05 stsp &repo->protected_branch_namespaces, namespace) == -1)
1370 f850236e 2023-04-05 stsp return -1;
1372 f850236e 2023-04-05 stsp TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1373 f850236e 2023-04-05 stsp if (strcmp(pe->path, new) == 0) {
1374 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1375 f850236e 2023-04-05 stsp return -1;
1382 9afa3de2 2023-04-04 stsp static int
1383 9afa3de2 2023-04-04 stsp conf_protect_branch(struct gotd_repo *repo, char *branchname)
1385 9afa3de2 2023-04-04 stsp const struct got_error *error;
1386 f0426190 2023-04-05 op struct got_pathlist_entry *new;
1387 9afa3de2 2023-04-04 stsp char *refname;
1389 9afa3de2 2023-04-04 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
1390 9afa3de2 2023-04-04 stsp if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1391 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
1392 9afa3de2 2023-04-04 stsp return -1;
1395 9afa3de2 2023-04-04 stsp refname = strdup(branchname);
1396 9afa3de2 2023-04-04 stsp if (refname == NULL) {
1397 9afa3de2 2023-04-04 stsp yyerror("strdup: %s", strerror(errno));
1398 9afa3de2 2023-04-04 stsp return -1;
1402 9afa3de2 2023-04-04 stsp if (!refname_is_valid(refname)) {
1403 9afa3de2 2023-04-04 stsp free(refname);
1404 9afa3de2 2023-04-04 stsp return -1;
1407 f0426190 2023-04-05 op error = got_pathlist_insert(&new, &repo->protected_branches,
1408 9afa3de2 2023-04-04 stsp refname, NULL);
1409 f0426190 2023-04-05 op if (error || new == NULL) {
1410 f0426190 2023-04-05 op free(refname);
1412 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1414 f0426190 2023-04-05 op yyerror("duplicate protect branch %s", branchname);
1415 ba97b2d7 2024-03-20 stsp return -1;
1421 ba97b2d7 2024-03-20 stsp static int
1422 ba97b2d7 2024-03-20 stsp conf_notify_branch(struct gotd_repo *repo, char *branchname)
1424 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1425 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1426 ba97b2d7 2024-03-20 stsp char *refname;
1428 ba97b2d7 2024-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
1429 ba97b2d7 2024-03-20 stsp if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1430 ba97b2d7 2024-03-20 stsp yyerror("asprintf: %s", strerror(errno));
1431 ba97b2d7 2024-03-20 stsp return -1;
1434 ba97b2d7 2024-03-20 stsp refname = strdup(branchname);
1435 ba97b2d7 2024-03-20 stsp if (refname == NULL) {
1436 ba97b2d7 2024-03-20 stsp yyerror("strdup: %s", strerror(errno));
1437 ba97b2d7 2024-03-20 stsp return -1;
1441 ba97b2d7 2024-03-20 stsp if (!refname_is_valid(refname)) {
1442 ba97b2d7 2024-03-20 stsp free(refname);
1443 9afa3de2 2023-04-04 stsp return -1;
1446 ba97b2d7 2024-03-20 stsp error = got_pathlist_insert(&pe, &repo->notification_refs,
1447 ba97b2d7 2024-03-20 stsp refname, NULL);
1448 ba97b2d7 2024-03-20 stsp if (error) {
1449 ba97b2d7 2024-03-20 stsp free(refname);
1450 ba97b2d7 2024-03-20 stsp yyerror("got_pathlist_insert: %s", error->msg);
1451 ba97b2d7 2024-03-20 stsp return -1;
1453 ba97b2d7 2024-03-20 stsp if (pe == NULL)
1454 ba97b2d7 2024-03-20 stsp free(refname);
1459 ba97b2d7 2024-03-20 stsp static int
1460 ba97b2d7 2024-03-20 stsp conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1462 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1463 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1466 ba97b2d7 2024-03-20 stsp got_path_strip_trailing_slashes(namespace);
1467 ba97b2d7 2024-03-20 stsp if (!refname_is_valid(namespace))
1468 ba97b2d7 2024-03-20 stsp return -1;
1470 ba97b2d7 2024-03-20 stsp if (asprintf(&s, "%s/", namespace) == -1) {
1471 ba97b2d7 2024-03-20 stsp yyerror("asprintf: %s", strerror(errno));
1472 ba97b2d7 2024-03-20 stsp return -1;
1475 ba97b2d7 2024-03-20 stsp error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1477 ba97b2d7 2024-03-20 stsp if (error) {
1479 ba97b2d7 2024-03-20 stsp yyerror("got_pathlist_insert: %s", error->msg);
1480 ba97b2d7 2024-03-20 stsp return -1;
1482 ba97b2d7 2024-03-20 stsp if (pe == NULL)
1488 ba97b2d7 2024-03-20 stsp static int
1489 ba97b2d7 2024-03-20 stsp conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1490 ba97b2d7 2024-03-20 stsp char *responder, char *hostname, char *port)
1492 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
1494 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1495 ba97b2d7 2024-03-20 stsp if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1497 ba97b2d7 2024-03-20 stsp if (strcmp(target->conf.email.recipient, recipient) == 0) {
1498 ba97b2d7 2024-03-20 stsp yyerror("duplicate email notification for '%s' in "
1499 ba97b2d7 2024-03-20 stsp "repository '%s'", recipient, repo->name);
1500 ba97b2d7 2024-03-20 stsp return -1;
1504 ba97b2d7 2024-03-20 stsp target = calloc(1, sizeof(*target));
1505 ba97b2d7 2024-03-20 stsp if (target == NULL)
1506 ba97b2d7 2024-03-20 stsp fatal("calloc");
1507 ba97b2d7 2024-03-20 stsp target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1508 ba97b2d7 2024-03-20 stsp if (sender) {
1509 ba97b2d7 2024-03-20 stsp target->conf.email.sender = strdup(sender);
1510 ba97b2d7 2024-03-20 stsp if (target->conf.email.sender == NULL)
1511 ba97b2d7 2024-03-20 stsp fatal("strdup");
1513 ba97b2d7 2024-03-20 stsp target->conf.email.recipient = strdup(recipient);
1514 ba97b2d7 2024-03-20 stsp if (target->conf.email.recipient == NULL)
1515 ba97b2d7 2024-03-20 stsp fatal("strdup");
1516 ba97b2d7 2024-03-20 stsp if (responder) {
1517 ba97b2d7 2024-03-20 stsp target->conf.email.responder = strdup(responder);
1518 ba97b2d7 2024-03-20 stsp if (target->conf.email.responder == NULL)
1519 ba97b2d7 2024-03-20 stsp fatal("strdup");
1521 ba97b2d7 2024-03-20 stsp if (hostname) {
1522 ba97b2d7 2024-03-20 stsp target->conf.email.hostname = strdup(hostname);
1523 ba97b2d7 2024-03-20 stsp if (target->conf.email.hostname == NULL)
1524 ba97b2d7 2024-03-20 stsp fatal("strdup");
1526 ba97b2d7 2024-03-20 stsp if (port) {
1527 ba97b2d7 2024-03-20 stsp target->conf.email.port = strdup(port);
1528 ba97b2d7 2024-03-20 stsp if (target->conf.email.port == NULL)
1529 ba97b2d7 2024-03-20 stsp fatal("strdup");
1532 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1536 ba97b2d7 2024-03-20 stsp static int
1537 ba97b2d7 2024-03-20 stsp conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password)
1539 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1540 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
1541 ba97b2d7 2024-03-20 stsp char *proto, *host, *port, *request_path;
1542 ba97b2d7 2024-03-20 stsp int ret = 0;
1544 ba97b2d7 2024-03-20 stsp error = gotd_parse_url(&proto, &host, &port, &request_path, url);
1545 ba97b2d7 2024-03-20 stsp if (error) {
1546 ba97b2d7 2024-03-20 stsp yyerror("invalid HTTP notification URL '%s' in "
1547 ba97b2d7 2024-03-20 stsp "repository '%s': %s", url, repo->name, error->msg);
1548 ba97b2d7 2024-03-20 stsp return -1;
1551 ba97b2d7 2024-03-20 stsp if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1552 ba97b2d7 2024-03-20 stsp yyerror("invalid protocol '%s' in notification URL '%s' in "
1553 ba97b2d7 2024-03-20 stsp "repository '%s", proto, url, repo->name);
1555 ba97b2d7 2024-03-20 stsp goto done;
1558 ba97b2d7 2024-03-20 stsp if (strcmp(proto, "http") == 0 && (user != NULL || password != NULL)) {
1559 ba97b2d7 2024-03-20 stsp log_warnx("%s: WARNING: Using basic authentication over "
1560 ba97b2d7 2024-03-20 stsp "plaintext http:// will leak credentials; https:// is "
1561 ba97b2d7 2024-03-20 stsp "recommended for URL '%s'", getprogname(), url);
1564 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1565 ba97b2d7 2024-03-20 stsp if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1567 ba97b2d7 2024-03-20 stsp if (strcmp(target->conf.http.url, url) == 0) {
1568 ba97b2d7 2024-03-20 stsp yyerror("duplicate notification for URL '%s' in "
1569 ba97b2d7 2024-03-20 stsp "repository '%s'", url, repo->name);
1571 ba97b2d7 2024-03-20 stsp goto done;
1575 ba97b2d7 2024-03-20 stsp target = calloc(1, sizeof(*target));
1576 ba97b2d7 2024-03-20 stsp if (target == NULL)
1577 ba97b2d7 2024-03-20 stsp fatal("calloc");
1578 ba97b2d7 2024-03-20 stsp target->type = GOTD_NOTIFICATION_VIA_HTTP;
1579 ba97b2d7 2024-03-20 stsp target->conf.http.url = strdup(url);
1580 ba97b2d7 2024-03-20 stsp if (target->conf.http.url == NULL)
1581 ba97b2d7 2024-03-20 stsp fatal("calloc");
1582 ba97b2d7 2024-03-20 stsp if (user) {
1583 ba97b2d7 2024-03-20 stsp target->conf.http.user = strdup(user);
1584 ba97b2d7 2024-03-20 stsp if (target->conf.http.user == NULL)
1585 ba97b2d7 2024-03-20 stsp fatal("calloc");
1587 ba97b2d7 2024-03-20 stsp if (password) {
1588 ba97b2d7 2024-03-20 stsp target->conf.http.password = strdup(password);
1589 ba97b2d7 2024-03-20 stsp if (target->conf.http.password == NULL)
1590 ba97b2d7 2024-03-20 stsp fatal("calloc");
1593 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1595 ba97b2d7 2024-03-20 stsp free(proto);
1596 ba97b2d7 2024-03-20 stsp free(host);
1597 ba97b2d7 2024-03-20 stsp free(port);
1598 ba97b2d7 2024-03-20 stsp free(request_path);
1599 ba97b2d7 2024-03-20 stsp return ret;
1603 13b2bc37 2022-10-23 stsp symset(const char *nam, const char *val, int persist)
1605 13b2bc37 2022-10-23 stsp struct sym *sym;
1607 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1608 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0)
1612 13b2bc37 2022-10-23 stsp if (sym != NULL) {
1613 13b2bc37 2022-10-23 stsp if (sym->persist == 1)
1614 13b2bc37 2022-10-23 stsp return (0);
1616 13b2bc37 2022-10-23 stsp free(sym->nam);
1617 13b2bc37 2022-10-23 stsp free(sym->val);
1618 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1619 13b2bc37 2022-10-23 stsp free(sym);
1622 13b2bc37 2022-10-23 stsp sym = calloc(1, sizeof(*sym));
1623 13b2bc37 2022-10-23 stsp if (sym == NULL)
1624 13b2bc37 2022-10-23 stsp return (-1);
1626 13b2bc37 2022-10-23 stsp sym->nam = strdup(nam);
1627 13b2bc37 2022-10-23 stsp if (sym->nam == NULL) {
1628 13b2bc37 2022-10-23 stsp free(sym);
1629 13b2bc37 2022-10-23 stsp return (-1);
1631 13b2bc37 2022-10-23 stsp sym->val = strdup(val);
1632 13b2bc37 2022-10-23 stsp if (sym->val == NULL) {
1633 13b2bc37 2022-10-23 stsp free(sym->nam);
1634 13b2bc37 2022-10-23 stsp free(sym);
1635 13b2bc37 2022-10-23 stsp return (-1);
1637 13b2bc37 2022-10-23 stsp sym->used = 0;
1638 13b2bc37 2022-10-23 stsp sym->persist = persist;
1639 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
1640 13b2bc37 2022-10-23 stsp return (0);
1644 13b2bc37 2022-10-23 stsp symget(const char *nam)
1646 13b2bc37 2022-10-23 stsp struct sym *sym;
1648 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1649 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0) {
1650 13b2bc37 2022-10-23 stsp sym->used = 1;
1651 13b2bc37 2022-10-23 stsp return (sym->val);
1654 13b2bc37 2022-10-23 stsp return (NULL);
1657 b09c1279 2023-03-28 stsp struct gotd_repo *
1658 ba97b2d7 2024-03-20 stsp gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1660 b09c1279 2023-03-28 stsp struct gotd_repo *repo;
1661 b09c1279 2023-03-28 stsp size_t namelen;
1663 ba97b2d7 2024-03-20 stsp TAILQ_FOREACH(repo, repos, entry) {
1664 b09c1279 2023-03-28 stsp namelen = strlen(repo->name);
1665 b09c1279 2023-03-28 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
1667 b09c1279 2023-03-28 stsp if (repo_name[namelen] == '\0' ||
1668 b09c1279 2023-03-28 stsp strcmp(&repo_name[namelen], ".git") == 0)
1669 b09c1279 2023-03-28 stsp return repo;
1672 b09c1279 2023-03-28 stsp return NULL;
1675 9afa3de2 2023-04-04 stsp struct gotd_repo *
1676 9afa3de2 2023-04-04 stsp gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1678 9afa3de2 2023-04-04 stsp struct gotd_repo *repo;
1680 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1681 9afa3de2 2023-04-04 stsp if (strcmp(repo->path, repo_path) == 0)
1682 9afa3de2 2023-04-04 stsp return repo;
1685 eeb616b7 2023-04-14 stsp return NULL;
1688 eeb616b7 2023-04-14 stsp struct gotd_uid_connection_limit *
1689 eeb616b7 2023-04-14 stsp gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1690 eeb616b7 2023-04-14 stsp size_t nlimits, uid_t uid)
1692 eeb616b7 2023-04-14 stsp /* This array is always sorted to allow for binary search. */
1693 eeb616b7 2023-04-14 stsp int i, left = 0, right = nlimits - 1;
1695 eeb616b7 2023-04-14 stsp while (left <= right) {
1696 eeb616b7 2023-04-14 stsp i = ((left + right) / 2);
1697 eeb616b7 2023-04-14 stsp if (limits[i].uid == uid)
1698 eeb616b7 2023-04-14 stsp return &limits[i];
1699 eeb616b7 2023-04-14 stsp if (limits[i].uid > uid)
1700 eeb616b7 2023-04-14 stsp left = i + 1;
1702 eeb616b7 2023-04-14 stsp right = i - 1;
1705 9afa3de2 2023-04-04 stsp return NULL;
1709 1963be61 2023-04-14 stsp gotd_parseuid(const char *s, uid_t *uid)
1711 1963be61 2023-04-14 stsp struct passwd *pw;
1712 1963be61 2023-04-14 stsp const char *errstr;
1714 1963be61 2023-04-14 stsp if ((pw = getpwnam(s)) != NULL) {
1715 1963be61 2023-04-14 stsp *uid = pw->pw_uid;
1716 1963be61 2023-04-14 stsp if (*uid == UID_MAX)
1717 1963be61 2023-04-14 stsp return -1;
1720 1963be61 2023-04-14 stsp *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1721 1963be61 2023-04-14 stsp if (errstr)
1722 1963be61 2023-04-14 stsp return -1;
1726 ba97b2d7 2024-03-20 stsp const struct got_error *
1727 ba97b2d7 2024-03-20 stsp gotd_parse_url(char **proto, char **host, char **port,
1728 ba97b2d7 2024-03-20 stsp char **request_path, const char *url)
1730 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1731 ba97b2d7 2024-03-20 stsp char *s, *p, *q;
1733 ba97b2d7 2024-03-20 stsp *proto = *host = *port = *request_path = NULL;
1735 ba97b2d7 2024-03-20 stsp p = strstr(url, "://");
1737 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PARSE_URI);
1739 ba97b2d7 2024-03-20 stsp *proto = strndup(url, p - url);
1740 ba97b2d7 2024-03-20 stsp if (*proto == NULL) {
1741 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1742 ba97b2d7 2024-03-20 stsp goto done;
1744 ba97b2d7 2024-03-20 stsp s = p + 3;
1746 ba97b2d7 2024-03-20 stsp p = strstr(s, "/");
1747 ba97b2d7 2024-03-20 stsp if (p == NULL || strlen(p) == 1) {
1748 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1749 ba97b2d7 2024-03-20 stsp goto done;
1752 ba97b2d7 2024-03-20 stsp q = memchr(s, ':', p - s);
1754 ba97b2d7 2024-03-20 stsp *host = strndup(s, q - s);
1755 ba97b2d7 2024-03-20 stsp if (*host == NULL) {
1756 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1757 ba97b2d7 2024-03-20 stsp goto done;
1759 ba97b2d7 2024-03-20 stsp if ((*host)[0] == '\0') {
1760 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1761 ba97b2d7 2024-03-20 stsp goto done;
1763 ba97b2d7 2024-03-20 stsp *port = strndup(q + 1, p - (q + 1));
1764 ba97b2d7 2024-03-20 stsp if (*port == NULL) {
1765 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1766 ba97b2d7 2024-03-20 stsp goto done;
1768 ba97b2d7 2024-03-20 stsp if ((*port)[0] == '\0') {
1769 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1770 ba97b2d7 2024-03-20 stsp goto done;
1773 ba97b2d7 2024-03-20 stsp *host = strndup(s, p - s);
1774 ba97b2d7 2024-03-20 stsp if (*host == NULL) {
1775 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1776 ba97b2d7 2024-03-20 stsp goto done;
1778 ba97b2d7 2024-03-20 stsp if ((*host)[0] == '\0') {
1779 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1780 ba97b2d7 2024-03-20 stsp goto done;
1784 ba97b2d7 2024-03-20 stsp while (p[0] == '/' && p[1] == '/')
1786 ba97b2d7 2024-03-20 stsp *request_path = strdup(p);
1787 ba97b2d7 2024-03-20 stsp if (*request_path == NULL) {
1788 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1789 ba97b2d7 2024-03-20 stsp goto done;
1791 ba97b2d7 2024-03-20 stsp got_path_strip_trailing_slashes(*request_path);
1792 ba97b2d7 2024-03-20 stsp if ((*request_path)[0] == '\0') {
1793 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1794 ba97b2d7 2024-03-20 stsp goto done;
1797 ba97b2d7 2024-03-20 stsp if (err) {
1798 ba97b2d7 2024-03-20 stsp free(*proto);
1799 ba97b2d7 2024-03-20 stsp *proto = NULL;
1800 ba97b2d7 2024-03-20 stsp free(*host);
1801 ba97b2d7 2024-03-20 stsp *host = NULL;
1802 ba97b2d7 2024-03-20 stsp free(*port);
1803 ba97b2d7 2024-03-20 stsp *port = NULL;
1804 ba97b2d7 2024-03-20 stsp free(*request_path);
1805 ba97b2d7 2024-03-20 stsp *request_path = NULL;
1807 ba97b2d7 2024-03-20 stsp return err;
1810 ba97b2d7 2024-03-20 stsp static char *
1811 ba97b2d7 2024-03-20 stsp port_sprintf(int p)
1813 ba97b2d7 2024-03-20 stsp static char portno[32];
1816 ba97b2d7 2024-03-20 stsp n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1817 ba97b2d7 2024-03-20 stsp if (n < 0 || (size_t)n >= sizeof(portno))
1818 ba97b2d7 2024-03-20 stsp fatalx("port number too long: %lld", (long long)p);
1820 ba97b2d7 2024-03-20 stsp return portno;