Blame


1 13b2bc37 2022-10-23 stsp /*
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.
10 13b2bc37 2022-10-23 stsp *
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.
14 13b2bc37 2022-10-23 stsp *
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.
22 13b2bc37 2022-10-23 stsp */
23 13b2bc37 2022-10-23 stsp
24 13b2bc37 2022-10-23 stsp %{
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>
29 13b2bc37 2022-10-23 stsp
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>
45 13b2bc37 2022-10-23 stsp
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"
49 13b2bc37 2022-10-23 stsp
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"
54 13b2bc37 2022-10-23 stsp
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;
59 13b2bc37 2022-10-23 stsp char *name;
60 13b2bc37 2022-10-23 stsp int lineno;
61 13b2bc37 2022-10-23 stsp int errors;
62 13b2bc37 2022-10-23 stsp } *file;
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);
77 13b2bc37 2022-10-23 stsp
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;
81 13b2bc37 2022-10-23 stsp int used;
82 13b2bc37 2022-10-23 stsp int persist;
83 13b2bc37 2022-10-23 stsp char *nam;
84 13b2bc37 2022-10-23 stsp char *val;
85 13b2bc37 2022-10-23 stsp };
86 13b2bc37 2022-10-23 stsp
87 13b2bc37 2022-10-23 stsp int symset(const char *, const char *, int);
88 13b2bc37 2022-10-23 stsp char *symget(const char *);
89 13b2bc37 2022-10-23 stsp
90 13b2bc37 2022-10-23 stsp static int errors;
91 13b2bc37 2022-10-23 stsp
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 *,
101 9afa3de2 2023-04-04 stsp char *);
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 *,
105 9afa3de2 2023-04-04 stsp char *);
106 ba97b2d7 2024-03-20 stsp static int conf_notify_branch(struct gotd_repo *,
107 ba97b2d7 2024-03-20 stsp char *);
108 ba97b2d7 2024-03-20 stsp static int conf_notify_ref_namespace(struct gotd_repo *,
109 ba97b2d7 2024-03-20 stsp char *);
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 55286da2 2024-04-18 stsp char *, char *, char *, int);
114 13b2bc37 2022-10-23 stsp static enum gotd_procid gotd_proc_id;
115 13b2bc37 2022-10-23 stsp
116 13b2bc37 2022-10-23 stsp typedef struct {
117 13b2bc37 2022-10-23 stsp union {
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;
121 13b2bc37 2022-10-23 stsp } v;
122 13b2bc37 2022-10-23 stsp int lineno;
123 13b2bc37 2022-10-23 stsp } YYSTYPE;
124 13b2bc37 2022-10-23 stsp
125 13b2bc37 2022-10-23 stsp %}
126 13b2bc37 2022-10-23 stsp
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 55286da2 2024-04-18 stsp %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD INSECURE
131 13b2bc37 2022-10-23 stsp
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
135 13b2bc37 2022-10-23 stsp
136 13b2bc37 2022-10-23 stsp %%
137 13b2bc37 2022-10-23 stsp
138 13b2bc37 2022-10-23 stsp grammar :
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'
143 13b2bc37 2022-10-23 stsp ;
144 13b2bc37 2022-10-23 stsp
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");
151 b1c09054 2023-11-04 stsp free($1);
152 b1c09054 2023-11-04 stsp free($3);
153 b1c09054 2023-11-04 stsp YYERROR;
154 b1c09054 2023-11-04 stsp }
155 b1c09054 2023-11-04 stsp }
156 b1c09054 2023-11-04 stsp if (symset($1, $3, 0) == -1)
157 b1c09054 2023-11-04 stsp fatal("cannot store variable");
158 b1c09054 2023-11-04 stsp free($1);
159 b1c09054 2023-11-04 stsp free($3);
160 b1c09054 2023-11-04 stsp }
161 b1c09054 2023-11-04 stsp ;
162 b1c09054 2023-11-04 stsp
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);
166 40b85cca 2023-01-03 stsp YYERROR;
167 40b85cca 2023-01-03 stsp }
168 40b85cca 2023-01-03 stsp $$.tv_sec = $1;
169 40b85cca 2023-01-03 stsp $$.tv_usec = 0;
170 40b85cca 2023-01-03 stsp }
171 2be11cde 2023-01-03 op | STRING {
172 2be11cde 2023-01-03 op const char *errstr;
173 2be11cde 2023-01-03 op const char *type = "seconds";
174 2be11cde 2023-01-03 op size_t len;
175 2be11cde 2023-01-03 op int mul = 1;
176 2be11cde 2023-01-03 op
177 2be11cde 2023-01-03 op if (*$1 == '\0') {
178 2be11cde 2023-01-03 op yyerror("invalid number of seconds: %s", $1);
179 2be11cde 2023-01-03 op free($1);
180 2be11cde 2023-01-03 op YYERROR;
181 2be11cde 2023-01-03 op }
182 2be11cde 2023-01-03 op
183 2be11cde 2023-01-03 op len = strlen($1);
184 2be11cde 2023-01-03 op switch ($1[len - 1]) {
185 2be11cde 2023-01-03 op case 'S':
186 2be11cde 2023-01-03 op case 's':
187 2be11cde 2023-01-03 op $1[len - 1] = '\0';
188 2be11cde 2023-01-03 op break;
189 2be11cde 2023-01-03 op case 'M':
190 2be11cde 2023-01-03 op case 'm':
191 2be11cde 2023-01-03 op type = "minutes";
192 2be11cde 2023-01-03 op mul = 60;
193 2be11cde 2023-01-03 op $1[len - 1] = '\0';
194 2be11cde 2023-01-03 op break;
195 2be11cde 2023-01-03 op case 'H':
196 2be11cde 2023-01-03 op case 'h':
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';
200 2be11cde 2023-01-03 op break;
201 2be11cde 2023-01-03 op }
202 2be11cde 2023-01-03 op
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,
207 2be11cde 2023-01-03 op errstr, $1);
208 2be11cde 2023-01-03 op free($1);
209 2be11cde 2023-01-03 op YYERROR;
210 2be11cde 2023-01-03 op }
211 2be11cde 2023-01-03 op
212 2be11cde 2023-01-03 op $$.tv_sec *= mul;
213 2be11cde 2023-01-03 op free($1);
214 2be11cde 2023-01-03 op }
215 40b85cca 2023-01-03 stsp ;
216 40b85cca 2023-01-03 stsp
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);
221 abe844e2 2023-01-18 op
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",
227 13b2bc37 2022-10-23 stsp __func__);
228 83577462 2023-01-05 stsp free($3);
229 13b2bc37 2022-10-23 stsp YYERROR;
230 13b2bc37 2022-10-23 stsp }
231 13b2bc37 2022-10-23 stsp }
232 83577462 2023-01-05 stsp free($3);
233 13b2bc37 2022-10-23 stsp }
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__);
239 13b2bc37 2022-10-23 stsp free($2);
240 13b2bc37 2022-10-23 stsp YYERROR;
241 13b2bc37 2022-10-23 stsp }
242 13b2bc37 2022-10-23 stsp free($2);
243 13b2bc37 2022-10-23 stsp }
244 40b85cca 2023-01-03 stsp | connection
245 13b2bc37 2022-10-23 stsp ;
246 13b2bc37 2022-10-23 stsp
247 40b85cca 2023-01-03 stsp connection : CONNECTION '{' optnl conflags_l '}'
248 40b85cca 2023-01-03 stsp | CONNECTION conflags
249 40b85cca 2023-01-03 stsp
250 40b85cca 2023-01-03 stsp conflags_l : conflags optnl conflags_l
251 40b85cca 2023-01-03 stsp | conflags optnl
252 40b85cca 2023-01-03 stsp ;
253 40b85cca 2023-01-03 stsp
254 40b85cca 2023-01-03 stsp conflags : REQUEST TIMEOUT timeout {
255 46e48ac7 2023-01-03 stsp if ($3.tv_sec <= 0) {
256 c623f221 2024-04-09 op yyerror("invalid timeout: %lld",
257 c623f221 2024-04-09 op (long long)$3.tv_sec);
258 46e48ac7 2023-01-03 stsp YYERROR;
259 46e48ac7 2023-01-03 stsp }
260 40b85cca 2023-01-03 stsp memcpy(&gotd->request_timeout, &$3,
261 40b85cca 2023-01-03 stsp sizeof(gotd->request_timeout));
262 40b85cca 2023-01-03 stsp }
263 40b85cca 2023-01-03 stsp | LIMIT USER STRING NUMBER {
264 40b85cca 2023-01-03 stsp if (gotd_proc_id == PROC_LISTEN &&
265 40b85cca 2023-01-03 stsp conf_limit_user_connections($3, $4) == -1) {
266 40b85cca 2023-01-03 stsp free($3);
267 40b85cca 2023-01-03 stsp YYERROR;
268 40b85cca 2023-01-03 stsp }
269 40b85cca 2023-01-03 stsp free($3);
270 40b85cca 2023-01-03 stsp }
271 40b85cca 2023-01-03 stsp ;
272 40b85cca 2023-01-03 stsp
273 9afa3de2 2023-04-04 stsp protect : PROTECT '{' optnl protectflags_l '}'
274 9afa3de2 2023-04-04 stsp | PROTECT protectflags
275 9afa3de2 2023-04-04 stsp
276 9afa3de2 2023-04-04 stsp protectflags_l : protectflags optnl protectflags_l
277 9afa3de2 2023-04-04 stsp | protectflags optnl
278 9afa3de2 2023-04-04 stsp ;
279 9afa3de2 2023-04-04 stsp
280 9afa3de2 2023-04-04 stsp protectflags : TAG NAMESPACE STRING {
281 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
282 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
283 9afa3de2 2023-04-04 stsp if (conf_protect_tag_namespace(new_repo, $3)) {
284 9afa3de2 2023-04-04 stsp free($3);
285 9afa3de2 2023-04-04 stsp YYERROR;
286 9afa3de2 2023-04-04 stsp }
287 9afa3de2 2023-04-04 stsp }
288 584140c2 2023-04-05 op free($3);
289 9afa3de2 2023-04-04 stsp }
290 9afa3de2 2023-04-04 stsp | BRANCH NAMESPACE 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_namespace(new_repo,
294 9afa3de2 2023-04-04 stsp $3)) {
295 9afa3de2 2023-04-04 stsp free($3);
296 9afa3de2 2023-04-04 stsp YYERROR;
297 9afa3de2 2023-04-04 stsp }
298 9afa3de2 2023-04-04 stsp }
299 584140c2 2023-04-05 op free($3);
300 9afa3de2 2023-04-04 stsp }
301 9afa3de2 2023-04-04 stsp | BRANCH STRING {
302 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
303 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
304 9afa3de2 2023-04-04 stsp if (conf_protect_branch(new_repo, $2)) {
305 9afa3de2 2023-04-04 stsp free($2);
306 9afa3de2 2023-04-04 stsp YYERROR;
307 9afa3de2 2023-04-04 stsp }
308 9afa3de2 2023-04-04 stsp }
309 584140c2 2023-04-05 op free($2);
310 9afa3de2 2023-04-04 stsp }
311 9afa3de2 2023-04-04 stsp ;
312 9afa3de2 2023-04-04 stsp
313 ba97b2d7 2024-03-20 stsp notify : NOTIFY '{' optnl notifyflags_l '}'
314 ba97b2d7 2024-03-20 stsp | NOTIFY notifyflags
315 ba97b2d7 2024-03-20 stsp
316 ba97b2d7 2024-03-20 stsp notifyflags_l : notifyflags optnl notifyflags_l
317 ba97b2d7 2024-03-20 stsp | notifyflags optnl
318 ba97b2d7 2024-03-20 stsp ;
319 ba97b2d7 2024-03-20 stsp
320 ba97b2d7 2024-03-20 stsp notifyflags : BRANCH STRING {
321 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
322 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
323 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
324 ba97b2d7 2024-03-20 stsp if (conf_notify_branch(new_repo, $2)) {
325 ba97b2d7 2024-03-20 stsp free($2);
326 ba97b2d7 2024-03-20 stsp YYERROR;
327 ba97b2d7 2024-03-20 stsp }
328 ba97b2d7 2024-03-20 stsp }
329 7d68253b 2024-03-20 op free($2);
330 ba97b2d7 2024-03-20 stsp }
331 ba97b2d7 2024-03-20 stsp | REFERENCE NAMESPACE STRING {
332 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
333 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
334 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
335 ba97b2d7 2024-03-20 stsp if (conf_notify_ref_namespace(new_repo, $3)) {
336 ba97b2d7 2024-03-20 stsp free($3);
337 ba97b2d7 2024-03-20 stsp YYERROR;
338 ba97b2d7 2024-03-20 stsp }
339 ba97b2d7 2024-03-20 stsp }
340 7d68253b 2024-03-20 op free($3);
341 ba97b2d7 2024-03-20 stsp }
342 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING {
343 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
344 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
345 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
346 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
347 ba97b2d7 2024-03-20 stsp NULL, NULL, NULL)) {
348 ba97b2d7 2024-03-20 stsp free($3);
349 ba97b2d7 2024-03-20 stsp YYERROR;
350 ba97b2d7 2024-03-20 stsp }
351 ba97b2d7 2024-03-20 stsp }
352 7d68253b 2024-03-20 op free($3);
353 ba97b2d7 2024-03-20 stsp }
354 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING {
355 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
356 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
357 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
358 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
359 ba97b2d7 2024-03-20 stsp NULL, NULL, NULL)) {
360 ba97b2d7 2024-03-20 stsp free($3);
361 ba97b2d7 2024-03-20 stsp free($5);
362 ba97b2d7 2024-03-20 stsp YYERROR;
363 ba97b2d7 2024-03-20 stsp }
364 7d68253b 2024-03-20 op }
365 7d68253b 2024-03-20 op free($3);
366 7d68253b 2024-03-20 op free($5);
367 ba97b2d7 2024-03-20 stsp }
368 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING {
369 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
370 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
371 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
372 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
373 ba97b2d7 2024-03-20 stsp $6, NULL, NULL)) {
374 ba97b2d7 2024-03-20 stsp free($3);
375 ba97b2d7 2024-03-20 stsp free($6);
376 ba97b2d7 2024-03-20 stsp YYERROR;
377 ba97b2d7 2024-03-20 stsp }
378 7d68253b 2024-03-20 op }
379 7d68253b 2024-03-20 op free($3);
380 7d68253b 2024-03-20 op free($6);
381 ba97b2d7 2024-03-20 stsp }
382 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING {
383 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
384 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
385 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
386 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
387 ba97b2d7 2024-03-20 stsp $8, NULL, NULL)) {
388 ba97b2d7 2024-03-20 stsp free($3);
389 ba97b2d7 2024-03-20 stsp free($5);
390 ba97b2d7 2024-03-20 stsp free($8);
391 ba97b2d7 2024-03-20 stsp YYERROR;
392 ba97b2d7 2024-03-20 stsp }
393 ba97b2d7 2024-03-20 stsp }
394 7d68253b 2024-03-20 op free($3);
395 7d68253b 2024-03-20 op free($5);
396 7d68253b 2024-03-20 op free($8);
397 ba97b2d7 2024-03-20 stsp }
398 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING RELAY STRING {
399 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
400 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
401 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
402 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
403 ba97b2d7 2024-03-20 stsp NULL, $5, NULL)) {
404 ba97b2d7 2024-03-20 stsp free($3);
405 ba97b2d7 2024-03-20 stsp free($5);
406 ba97b2d7 2024-03-20 stsp YYERROR;
407 ba97b2d7 2024-03-20 stsp }
408 ba97b2d7 2024-03-20 stsp }
409 7d68253b 2024-03-20 op free($3);
410 7d68253b 2024-03-20 op free($5);
411 ba97b2d7 2024-03-20 stsp }
412 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING RELAY STRING {
413 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
414 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
415 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
416 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
417 ba97b2d7 2024-03-20 stsp NULL, $7, NULL)) {
418 ba97b2d7 2024-03-20 stsp free($3);
419 ba97b2d7 2024-03-20 stsp free($5);
420 ba97b2d7 2024-03-20 stsp free($7);
421 ba97b2d7 2024-03-20 stsp YYERROR;
422 ba97b2d7 2024-03-20 stsp }
423 ba97b2d7 2024-03-20 stsp }
424 7d68253b 2024-03-20 op free($3);
425 7d68253b 2024-03-20 op free($5);
426 7d68253b 2024-03-20 op free($7);
427 ba97b2d7 2024-03-20 stsp }
428 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING RELAY STRING {
429 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
430 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
431 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
432 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
433 ba97b2d7 2024-03-20 stsp $6, $8, NULL)) {
434 ba97b2d7 2024-03-20 stsp free($3);
435 ba97b2d7 2024-03-20 stsp free($6);
436 ba97b2d7 2024-03-20 stsp free($8);
437 ba97b2d7 2024-03-20 stsp YYERROR;
438 ba97b2d7 2024-03-20 stsp }
439 ba97b2d7 2024-03-20 stsp }
440 7d68253b 2024-03-20 op free($3);
441 7d68253b 2024-03-20 op free($6);
442 7d68253b 2024-03-20 op free($8);
443 ba97b2d7 2024-03-20 stsp }
444 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING {
445 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
446 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
447 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
448 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
449 ba97b2d7 2024-03-20 stsp $8, $10, NULL)) {
450 ba97b2d7 2024-03-20 stsp free($3);
451 ba97b2d7 2024-03-20 stsp free($5);
452 ba97b2d7 2024-03-20 stsp free($8);
453 ba97b2d7 2024-03-20 stsp free($10);
454 ba97b2d7 2024-03-20 stsp YYERROR;
455 ba97b2d7 2024-03-20 stsp }
456 ba97b2d7 2024-03-20 stsp }
457 7d68253b 2024-03-20 op free($3);
458 7d68253b 2024-03-20 op free($5);
459 7d68253b 2024-03-20 op free($8);
460 7d68253b 2024-03-20 op free($10);
461 ba97b2d7 2024-03-20 stsp }
462 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING RELAY STRING PORT STRING {
463 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
464 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
465 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
466 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
467 ba97b2d7 2024-03-20 stsp NULL, $5, $7)) {
468 ba97b2d7 2024-03-20 stsp free($3);
469 ba97b2d7 2024-03-20 stsp free($5);
470 ba97b2d7 2024-03-20 stsp free($7);
471 ba97b2d7 2024-03-20 stsp YYERROR;
472 ba97b2d7 2024-03-20 stsp }
473 ba97b2d7 2024-03-20 stsp }
474 7d68253b 2024-03-20 op free($3);
475 7d68253b 2024-03-20 op free($5);
476 7d68253b 2024-03-20 op free($7);
477 ba97b2d7 2024-03-20 stsp }
478 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING RELAY STRING PORT STRING {
479 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
480 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
481 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
482 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
483 ba97b2d7 2024-03-20 stsp NULL, $7, $9)) {
484 ba97b2d7 2024-03-20 stsp free($3);
485 ba97b2d7 2024-03-20 stsp free($5);
486 ba97b2d7 2024-03-20 stsp free($7);
487 ba97b2d7 2024-03-20 stsp free($9);
488 ba97b2d7 2024-03-20 stsp YYERROR;
489 ba97b2d7 2024-03-20 stsp }
490 ba97b2d7 2024-03-20 stsp }
491 7d68253b 2024-03-20 op free($3);
492 7d68253b 2024-03-20 op free($5);
493 7d68253b 2024-03-20 op free($7);
494 7d68253b 2024-03-20 op free($9);
495 ba97b2d7 2024-03-20 stsp }
496 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT STRING {
497 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
498 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
499 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
500 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
501 ba97b2d7 2024-03-20 stsp $6, $8, $10)) {
502 ba97b2d7 2024-03-20 stsp free($3);
503 ba97b2d7 2024-03-20 stsp free($6);
504 ba97b2d7 2024-03-20 stsp free($8);
505 ba97b2d7 2024-03-20 stsp free($10);
506 ba97b2d7 2024-03-20 stsp YYERROR;
507 ba97b2d7 2024-03-20 stsp }
508 7d68253b 2024-03-20 op }
509 7d68253b 2024-03-20 op free($3);
510 7d68253b 2024-03-20 op free($6);
511 7d68253b 2024-03-20 op free($8);
512 7d68253b 2024-03-20 op free($10);
513 ba97b2d7 2024-03-20 stsp }
514 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT STRING {
515 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
516 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
517 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
518 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
519 ba97b2d7 2024-03-20 stsp $8, $10, $12)) {
520 ba97b2d7 2024-03-20 stsp free($3);
521 ba97b2d7 2024-03-20 stsp free($5);
522 ba97b2d7 2024-03-20 stsp free($8);
523 ba97b2d7 2024-03-20 stsp free($10);
524 ba97b2d7 2024-03-20 stsp free($12);
525 ba97b2d7 2024-03-20 stsp YYERROR;
526 ba97b2d7 2024-03-20 stsp }
527 ba97b2d7 2024-03-20 stsp }
528 7d68253b 2024-03-20 op free($3);
529 7d68253b 2024-03-20 op free($5);
530 7d68253b 2024-03-20 op free($8);
531 7d68253b 2024-03-20 op free($10);
532 7d68253b 2024-03-20 op free($12);
533 ba97b2d7 2024-03-20 stsp }
534 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING RELAY STRING PORT NUMBER {
535 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
536 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
537 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
538 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
539 ba97b2d7 2024-03-20 stsp NULL, $5, port_sprintf($7))) {
540 ba97b2d7 2024-03-20 stsp free($3);
541 ba97b2d7 2024-03-20 stsp free($5);
542 ba97b2d7 2024-03-20 stsp YYERROR;
543 ba97b2d7 2024-03-20 stsp }
544 ba97b2d7 2024-03-20 stsp }
545 7d68253b 2024-03-20 op free($3);
546 7d68253b 2024-03-20 op free($5);
547 ba97b2d7 2024-03-20 stsp }
548 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING RELAY STRING PORT NUMBER {
549 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
550 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
551 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
552 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
553 ba97b2d7 2024-03-20 stsp NULL, $7, port_sprintf($9))) {
554 ba97b2d7 2024-03-20 stsp free($3);
555 ba97b2d7 2024-03-20 stsp free($5);
556 ba97b2d7 2024-03-20 stsp free($7);
557 ba97b2d7 2024-03-20 stsp YYERROR;
558 ba97b2d7 2024-03-20 stsp }
559 ba97b2d7 2024-03-20 stsp }
560 7d68253b 2024-03-20 op free($3);
561 7d68253b 2024-03-20 op free($5);
562 7d68253b 2024-03-20 op free($7);
563 ba97b2d7 2024-03-20 stsp }
564 ba97b2d7 2024-03-20 stsp | EMAIL TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
565 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
566 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
567 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
568 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, NULL, $3,
569 ba97b2d7 2024-03-20 stsp $6, $8, port_sprintf($10))) {
570 ba97b2d7 2024-03-20 stsp free($3);
571 ba97b2d7 2024-03-20 stsp free($6);
572 ba97b2d7 2024-03-20 stsp free($8);
573 ba97b2d7 2024-03-20 stsp YYERROR;
574 ba97b2d7 2024-03-20 stsp }
575 ba97b2d7 2024-03-20 stsp }
576 7d68253b 2024-03-20 op free($3);
577 7d68253b 2024-03-20 op free($6);
578 7d68253b 2024-03-20 op free($8);
579 ba97b2d7 2024-03-20 stsp }
580 ba97b2d7 2024-03-20 stsp | EMAIL FROM STRING TO STRING REPLY TO STRING RELAY STRING PORT NUMBER {
581 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
582 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
583 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
584 ba97b2d7 2024-03-20 stsp if (conf_notify_email(new_repo, $3, $5,
585 ba97b2d7 2024-03-20 stsp $8, $10, port_sprintf($12))) {
586 ba97b2d7 2024-03-20 stsp free($3);
587 ba97b2d7 2024-03-20 stsp free($5);
588 ba97b2d7 2024-03-20 stsp free($8);
589 ba97b2d7 2024-03-20 stsp free($10);
590 ba97b2d7 2024-03-20 stsp YYERROR;
591 ba97b2d7 2024-03-20 stsp }
592 ba97b2d7 2024-03-20 stsp }
593 7d68253b 2024-03-20 op free($3);
594 7d68253b 2024-03-20 op free($5);
595 7d68253b 2024-03-20 op free($8);
596 7d68253b 2024-03-20 op free($10);
597 ba97b2d7 2024-03-20 stsp }
598 ba97b2d7 2024-03-20 stsp | URL STRING {
599 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
600 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
601 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
602 ba97b2d7 2024-03-20 stsp if (conf_notify_http(new_repo, $2, NULL,
603 55286da2 2024-04-18 stsp NULL, 0)) {
604 ba97b2d7 2024-03-20 stsp free($2);
605 ba97b2d7 2024-03-20 stsp YYERROR;
606 ba97b2d7 2024-03-20 stsp }
607 ba97b2d7 2024-03-20 stsp }
608 7d68253b 2024-03-20 op free($2);
609 ba97b2d7 2024-03-20 stsp }
610 ba97b2d7 2024-03-20 stsp | URL STRING USER STRING PASSWORD STRING {
611 ba97b2d7 2024-03-20 stsp if (gotd_proc_id == PROC_GOTD ||
612 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
613 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
614 55286da2 2024-04-18 stsp if (conf_notify_http(new_repo, $2, $4, $6, 0)) {
615 55286da2 2024-04-18 stsp free($2);
616 55286da2 2024-04-18 stsp free($4);
617 55286da2 2024-04-18 stsp free($6);
618 55286da2 2024-04-18 stsp YYERROR;
619 55286da2 2024-04-18 stsp }
620 55286da2 2024-04-18 stsp }
621 55286da2 2024-04-18 stsp free($2);
622 55286da2 2024-04-18 stsp free($4);
623 55286da2 2024-04-18 stsp free($6);
624 55286da2 2024-04-18 stsp }
625 55286da2 2024-04-18 stsp | URL STRING USER STRING PASSWORD STRING INSECURE {
626 55286da2 2024-04-18 stsp if (gotd_proc_id == PROC_GOTD ||
627 55286da2 2024-04-18 stsp gotd_proc_id == PROC_SESSION_WRITE ||
628 55286da2 2024-04-18 stsp gotd_proc_id == PROC_NOTIFY) {
629 55286da2 2024-04-18 stsp if (conf_notify_http(new_repo, $2, $4, $6, 1)) {
630 ba97b2d7 2024-03-20 stsp free($2);
631 ba97b2d7 2024-03-20 stsp free($4);
632 ba97b2d7 2024-03-20 stsp free($6);
633 ba97b2d7 2024-03-20 stsp YYERROR;
634 ba97b2d7 2024-03-20 stsp }
635 ba97b2d7 2024-03-20 stsp }
636 7d68253b 2024-03-20 op free($2);
637 7d68253b 2024-03-20 op free($4);
638 7d68253b 2024-03-20 op free($6);
639 ba97b2d7 2024-03-20 stsp }
640 ba97b2d7 2024-03-20 stsp ;
641 ba97b2d7 2024-03-20 stsp
642 13b2bc37 2022-10-23 stsp repository : REPOSITORY STRING {
643 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
644 13b2bc37 2022-10-23 stsp
645 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
646 13b2bc37 2022-10-23 stsp if (strcmp(repo->name, $2) == 0) {
647 13b2bc37 2022-10-23 stsp yyerror("duplicate repository '%s'", $2);
648 13b2bc37 2022-10-23 stsp free($2);
649 13b2bc37 2022-10-23 stsp YYERROR;
650 13b2bc37 2022-10-23 stsp }
651 13b2bc37 2022-10-23 stsp }
652 13b2bc37 2022-10-23 stsp
653 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
654 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
655 4b3827cd 2023-07-08 stsp gotd_proc_id == PROC_REPO_WRITE ||
656 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
657 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_GITWRAPPER |
658 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
659 13b2bc37 2022-10-23 stsp new_repo = conf_new_repo($2);
660 13b2bc37 2022-10-23 stsp }
661 13b2bc37 2022-10-23 stsp free($2);
662 13b2bc37 2022-10-23 stsp } '{' optnl repoopts2 '}' {
663 13b2bc37 2022-10-23 stsp }
664 13b2bc37 2022-10-23 stsp ;
665 13b2bc37 2022-10-23 stsp
666 13b2bc37 2022-10-23 stsp repoopts1 : PATH STRING {
667 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
668 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
669 4b3827cd 2023-07-08 stsp gotd_proc_id == PROC_REPO_WRITE ||
670 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_SESSION_WRITE ||
671 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_GITWRAPPER ||
672 ba97b2d7 2024-03-20 stsp gotd_proc_id == PROC_NOTIFY) {
673 13b2bc37 2022-10-23 stsp if (!got_path_is_absolute($2)) {
674 13b2bc37 2022-10-23 stsp yyerror("%s: path %s is not absolute",
675 13b2bc37 2022-10-23 stsp __func__, $2);
676 13b2bc37 2022-10-23 stsp free($2);
677 13b2bc37 2022-10-23 stsp YYERROR;
678 13b2bc37 2022-10-23 stsp }
679 9b7f22a6 2023-01-08 stsp if (realpath($2, new_repo->path) == NULL) {
680 859316d0 2023-04-12 stsp /*
681 4b3827cd 2023-07-08 stsp * To give admins a chance to create
682 4b3827cd 2023-07-08 stsp * missing repositories at run-time
683 4b3827cd 2023-07-08 stsp * we only warn about ENOENT here.
684 4b3827cd 2023-07-08 stsp *
685 4b3827cd 2023-07-08 stsp * And ignore 'permission denied' when
686 4b3827cd 2023-07-08 stsp * running in gitwrapper. Users may be
687 4b3827cd 2023-07-08 stsp * able to access this repository via
688 4b3827cd 2023-07-08 stsp * gotd regardless.
689 859316d0 2023-04-12 stsp */
690 4b3827cd 2023-07-08 stsp if (errno == ENOENT) {
691 4b3827cd 2023-07-08 stsp yyerror("realpath %s: %s", $2,
692 4b3827cd 2023-07-08 stsp strerror(errno));
693 4b3827cd 2023-07-08 stsp } else if (errno != EACCES ||
694 4b3827cd 2023-07-08 stsp gotd_proc_id != PROC_GITWRAPPER) {
695 4b3827cd 2023-07-08 stsp yyerror("realpath %s: %s", $2,
696 4b3827cd 2023-07-08 stsp strerror(errno));
697 859316d0 2023-04-12 stsp free($2);
698 859316d0 2023-04-12 stsp YYERROR;
699 4b3827cd 2023-07-08 stsp }
700 4b3827cd 2023-07-08 stsp
701 4b3827cd 2023-07-08 stsp if (strlcpy(new_repo->path, $2,
702 859316d0 2023-04-12 stsp sizeof(new_repo->path)) >=
703 859316d0 2023-04-12 stsp sizeof(new_repo->path))
704 859316d0 2023-04-12 stsp yyerror("path too long");
705 13b2bc37 2022-10-23 stsp }
706 13b2bc37 2022-10-23 stsp }
707 13b2bc37 2022-10-23 stsp free($2);
708 0ccf3acb 2022-11-16 stsp }
709 0ccf3acb 2022-11-16 stsp | PERMIT RO STRING {
710 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
711 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
712 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
713 584140c2 2023-04-05 op } else
714 584140c2 2023-04-05 op free($3);
715 0ccf3acb 2022-11-16 stsp }
716 0ccf3acb 2022-11-16 stsp | PERMIT RW STRING {
717 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
718 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
719 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED,
720 0ccf3acb 2022-11-16 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
721 584140c2 2023-04-05 op } else
722 584140c2 2023-04-05 op free($3);
723 13b2bc37 2022-10-23 stsp }
724 0ccf3acb 2022-11-16 stsp | DENY STRING {
725 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
726 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
727 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_DENIED, 0, $2);
728 584140c2 2023-04-05 op } else
729 584140c2 2023-04-05 op free($2);
730 0ccf3acb 2022-11-16 stsp }
731 9afa3de2 2023-04-04 stsp | protect
732 ba97b2d7 2024-03-20 stsp | notify
733 13b2bc37 2022-10-23 stsp ;
734 13b2bc37 2022-10-23 stsp
735 13b2bc37 2022-10-23 stsp repoopts2 : repoopts2 repoopts1 nl
736 13b2bc37 2022-10-23 stsp | repoopts1 optnl
737 13b2bc37 2022-10-23 stsp ;
738 13b2bc37 2022-10-23 stsp
739 13b2bc37 2022-10-23 stsp nl : '\n' optnl
740 13b2bc37 2022-10-23 stsp ;
741 13b2bc37 2022-10-23 stsp
742 13b2bc37 2022-10-23 stsp optnl : '\n' optnl /* zero or more newlines */
743 13b2bc37 2022-10-23 stsp | /* empty */
744 13b2bc37 2022-10-23 stsp ;
745 13b2bc37 2022-10-23 stsp
746 13b2bc37 2022-10-23 stsp %%
747 13b2bc37 2022-10-23 stsp
748 13b2bc37 2022-10-23 stsp struct keywords {
749 13b2bc37 2022-10-23 stsp const char *k_name;
750 13b2bc37 2022-10-23 stsp int k_val;
751 13b2bc37 2022-10-23 stsp };
752 13b2bc37 2022-10-23 stsp
753 13b2bc37 2022-10-23 stsp int
754 13b2bc37 2022-10-23 stsp yyerror(const char *fmt, ...)
755 13b2bc37 2022-10-23 stsp {
756 13b2bc37 2022-10-23 stsp va_list ap;
757 13b2bc37 2022-10-23 stsp char *msg;
758 13b2bc37 2022-10-23 stsp
759 13b2bc37 2022-10-23 stsp file->errors++;
760 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
761 13b2bc37 2022-10-23 stsp if (vasprintf(&msg, fmt, ap) == -1)
762 13b2bc37 2022-10-23 stsp fatalx("yyerror vasprintf");
763 13b2bc37 2022-10-23 stsp va_end(ap);
764 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
765 13b2bc37 2022-10-23 stsp free(msg);
766 13b2bc37 2022-10-23 stsp return (0);
767 13b2bc37 2022-10-23 stsp }
768 13b2bc37 2022-10-23 stsp
769 13b2bc37 2022-10-23 stsp int
770 13b2bc37 2022-10-23 stsp kw_cmp(const void *k, const void *e)
771 13b2bc37 2022-10-23 stsp {
772 13b2bc37 2022-10-23 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
773 13b2bc37 2022-10-23 stsp }
774 13b2bc37 2022-10-23 stsp
775 13b2bc37 2022-10-23 stsp int
776 13b2bc37 2022-10-23 stsp lookup(char *s)
777 13b2bc37 2022-10-23 stsp {
778 13b2bc37 2022-10-23 stsp /* This has to be sorted always. */
779 13b2bc37 2022-10-23 stsp static const struct keywords keywords[] = {
780 9afa3de2 2023-04-04 stsp { "branch", BRANCH },
781 40b85cca 2023-01-03 stsp { "connection", CONNECTION },
782 0ccf3acb 2022-11-16 stsp { "deny", DENY },
783 ba97b2d7 2024-03-20 stsp { "email", EMAIL },
784 ba97b2d7 2024-03-20 stsp { "from", FROM },
785 55286da2 2024-04-18 stsp { "insecure", INSECURE },
786 40b85cca 2023-01-03 stsp { "limit", LIMIT },
787 83577462 2023-01-05 stsp { "listen", LISTEN },
788 9afa3de2 2023-04-04 stsp { "namespace", NAMESPACE },
789 ba97b2d7 2024-03-20 stsp { "notify", NOTIFY },
790 13b2bc37 2022-10-23 stsp { "on", ON },
791 ba97b2d7 2024-03-20 stsp { "password", PASSWORD },
792 13b2bc37 2022-10-23 stsp { "path", PATH },
793 0ccf3acb 2022-11-16 stsp { "permit", PERMIT },
794 ba97b2d7 2024-03-20 stsp { "port", PORT },
795 9afa3de2 2023-04-04 stsp { "protect", PROTECT },
796 ba97b2d7 2024-03-20 stsp { "reference", REFERENCE },
797 ba97b2d7 2024-03-20 stsp { "relay", RELAY },
798 ba97b2d7 2024-03-20 stsp { "reply", REPLY },
799 13b2bc37 2022-10-23 stsp { "repository", REPOSITORY },
800 40b85cca 2023-01-03 stsp { "request", REQUEST },
801 0ccf3acb 2022-11-16 stsp { "ro", RO },
802 0ccf3acb 2022-11-16 stsp { "rw", RW },
803 9afa3de2 2023-04-04 stsp { "tag", TAG },
804 40b85cca 2023-01-03 stsp { "timeout", TIMEOUT },
805 ba97b2d7 2024-03-20 stsp { "to", TO },
806 ba97b2d7 2024-03-20 stsp { "url", URL },
807 13b2bc37 2022-10-23 stsp { "user", USER },
808 13b2bc37 2022-10-23 stsp };
809 13b2bc37 2022-10-23 stsp const struct keywords *p;
810 13b2bc37 2022-10-23 stsp
811 13b2bc37 2022-10-23 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
812 13b2bc37 2022-10-23 stsp sizeof(keywords[0]), kw_cmp);
813 13b2bc37 2022-10-23 stsp
814 13b2bc37 2022-10-23 stsp if (p)
815 13b2bc37 2022-10-23 stsp return (p->k_val);
816 13b2bc37 2022-10-23 stsp else
817 13b2bc37 2022-10-23 stsp return (STRING);
818 13b2bc37 2022-10-23 stsp }
819 13b2bc37 2022-10-23 stsp
820 13b2bc37 2022-10-23 stsp #define MAXPUSHBACK 128
821 13b2bc37 2022-10-23 stsp
822 13b2bc37 2022-10-23 stsp unsigned char *parsebuf;
823 13b2bc37 2022-10-23 stsp int parseindex;
824 13b2bc37 2022-10-23 stsp unsigned char pushback_buffer[MAXPUSHBACK];
825 13b2bc37 2022-10-23 stsp int pushback_index = 0;
826 13b2bc37 2022-10-23 stsp
827 13b2bc37 2022-10-23 stsp int
828 13b2bc37 2022-10-23 stsp lgetc(int quotec)
829 13b2bc37 2022-10-23 stsp {
830 13b2bc37 2022-10-23 stsp int c, next;
831 13b2bc37 2022-10-23 stsp
832 13b2bc37 2022-10-23 stsp if (parsebuf) {
833 13b2bc37 2022-10-23 stsp /* Read character from the parsebuffer instead of input. */
834 13b2bc37 2022-10-23 stsp if (parseindex >= 0) {
835 13b2bc37 2022-10-23 stsp c = parsebuf[parseindex++];
836 13b2bc37 2022-10-23 stsp if (c != '\0')
837 13b2bc37 2022-10-23 stsp return (c);
838 13b2bc37 2022-10-23 stsp parsebuf = NULL;
839 13b2bc37 2022-10-23 stsp } else
840 13b2bc37 2022-10-23 stsp parseindex++;
841 13b2bc37 2022-10-23 stsp }
842 13b2bc37 2022-10-23 stsp
843 13b2bc37 2022-10-23 stsp if (pushback_index)
844 13b2bc37 2022-10-23 stsp return (pushback_buffer[--pushback_index]);
845 13b2bc37 2022-10-23 stsp
846 13b2bc37 2022-10-23 stsp if (quotec) {
847 13b2bc37 2022-10-23 stsp c = getc(file->stream);
848 13b2bc37 2022-10-23 stsp if (c == EOF)
849 13b2bc37 2022-10-23 stsp yyerror("reached end of file while parsing "
850 13b2bc37 2022-10-23 stsp "quoted string");
851 13b2bc37 2022-10-23 stsp return (c);
852 13b2bc37 2022-10-23 stsp }
853 13b2bc37 2022-10-23 stsp
854 13b2bc37 2022-10-23 stsp c = getc(file->stream);
855 13b2bc37 2022-10-23 stsp while (c == '\\') {
856 13b2bc37 2022-10-23 stsp next = getc(file->stream);
857 13b2bc37 2022-10-23 stsp if (next != '\n') {
858 13b2bc37 2022-10-23 stsp c = next;
859 13b2bc37 2022-10-23 stsp break;
860 13b2bc37 2022-10-23 stsp }
861 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
862 13b2bc37 2022-10-23 stsp file->lineno++;
863 13b2bc37 2022-10-23 stsp c = getc(file->stream);
864 13b2bc37 2022-10-23 stsp }
865 13b2bc37 2022-10-23 stsp
866 13b2bc37 2022-10-23 stsp return (c);
867 13b2bc37 2022-10-23 stsp }
868 13b2bc37 2022-10-23 stsp
869 13b2bc37 2022-10-23 stsp int
870 13b2bc37 2022-10-23 stsp lungetc(int c)
871 13b2bc37 2022-10-23 stsp {
872 13b2bc37 2022-10-23 stsp if (c == EOF)
873 13b2bc37 2022-10-23 stsp return (EOF);
874 13b2bc37 2022-10-23 stsp if (parsebuf) {
875 13b2bc37 2022-10-23 stsp parseindex--;
876 13b2bc37 2022-10-23 stsp if (parseindex >= 0)
877 13b2bc37 2022-10-23 stsp return (c);
878 13b2bc37 2022-10-23 stsp }
879 13b2bc37 2022-10-23 stsp if (pushback_index < MAXPUSHBACK-1)
880 13b2bc37 2022-10-23 stsp return (pushback_buffer[pushback_index++] = c);
881 13b2bc37 2022-10-23 stsp else
882 13b2bc37 2022-10-23 stsp return (EOF);
883 13b2bc37 2022-10-23 stsp }
884 13b2bc37 2022-10-23 stsp
885 13b2bc37 2022-10-23 stsp int
886 13b2bc37 2022-10-23 stsp findeol(void)
887 13b2bc37 2022-10-23 stsp {
888 13b2bc37 2022-10-23 stsp int c;
889 13b2bc37 2022-10-23 stsp
890 13b2bc37 2022-10-23 stsp parsebuf = NULL;
891 13b2bc37 2022-10-23 stsp
892 13b2bc37 2022-10-23 stsp /* Skip to either EOF or the first real EOL. */
893 13b2bc37 2022-10-23 stsp while (1) {
894 13b2bc37 2022-10-23 stsp if (pushback_index)
895 13b2bc37 2022-10-23 stsp c = pushback_buffer[--pushback_index];
896 13b2bc37 2022-10-23 stsp else
897 13b2bc37 2022-10-23 stsp c = lgetc(0);
898 13b2bc37 2022-10-23 stsp if (c == '\n') {
899 13b2bc37 2022-10-23 stsp file->lineno++;
900 13b2bc37 2022-10-23 stsp break;
901 13b2bc37 2022-10-23 stsp }
902 13b2bc37 2022-10-23 stsp if (c == EOF)
903 13b2bc37 2022-10-23 stsp break;
904 13b2bc37 2022-10-23 stsp }
905 13b2bc37 2022-10-23 stsp return (ERROR);
906 13b2bc37 2022-10-23 stsp }
907 13b2bc37 2022-10-23 stsp
908 13b2bc37 2022-10-23 stsp int
909 13b2bc37 2022-10-23 stsp yylex(void)
910 13b2bc37 2022-10-23 stsp {
911 13b2bc37 2022-10-23 stsp unsigned char buf[8096];
912 13b2bc37 2022-10-23 stsp unsigned char *p, *val;
913 13b2bc37 2022-10-23 stsp int quotec, next, c;
914 13b2bc37 2022-10-23 stsp int token;
915 13b2bc37 2022-10-23 stsp
916 13b2bc37 2022-10-23 stsp top:
917 13b2bc37 2022-10-23 stsp p = buf;
918 13b2bc37 2022-10-23 stsp c = lgetc(0);
919 13b2bc37 2022-10-23 stsp while (c == ' ' || c == '\t')
920 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
921 13b2bc37 2022-10-23 stsp
922 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
923 13b2bc37 2022-10-23 stsp if (c == '#') {
924 13b2bc37 2022-10-23 stsp c = lgetc(0);
925 13b2bc37 2022-10-23 stsp while (c != '\n' && c != EOF)
926 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
927 13b2bc37 2022-10-23 stsp }
928 13b2bc37 2022-10-23 stsp if (c == '$' && parsebuf == NULL) {
929 13b2bc37 2022-10-23 stsp while (1) {
930 13b2bc37 2022-10-23 stsp c = lgetc(0);
931 13b2bc37 2022-10-23 stsp if (c == EOF)
932 13b2bc37 2022-10-23 stsp return (0);
933 13b2bc37 2022-10-23 stsp
934 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
935 13b2bc37 2022-10-23 stsp yyerror("string too long");
936 13b2bc37 2022-10-23 stsp return (findeol());
937 13b2bc37 2022-10-23 stsp }
938 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == '_') {
939 13b2bc37 2022-10-23 stsp *p++ = c;
940 13b2bc37 2022-10-23 stsp continue;
941 13b2bc37 2022-10-23 stsp }
942 13b2bc37 2022-10-23 stsp *p = '\0';
943 13b2bc37 2022-10-23 stsp lungetc(c);
944 13b2bc37 2022-10-23 stsp break;
945 13b2bc37 2022-10-23 stsp }
946 13b2bc37 2022-10-23 stsp val = symget(buf);
947 13b2bc37 2022-10-23 stsp if (val == NULL) {
948 13b2bc37 2022-10-23 stsp yyerror("macro '%s' not defined", buf);
949 13b2bc37 2022-10-23 stsp return (findeol());
950 13b2bc37 2022-10-23 stsp }
951 13b2bc37 2022-10-23 stsp parsebuf = val;
952 13b2bc37 2022-10-23 stsp parseindex = 0;
953 13b2bc37 2022-10-23 stsp goto top;
954 13b2bc37 2022-10-23 stsp }
955 13b2bc37 2022-10-23 stsp
956 13b2bc37 2022-10-23 stsp switch (c) {
957 13b2bc37 2022-10-23 stsp case '\'':
958 13b2bc37 2022-10-23 stsp case '"':
959 13b2bc37 2022-10-23 stsp quotec = c;
960 13b2bc37 2022-10-23 stsp while (1) {
961 13b2bc37 2022-10-23 stsp c = lgetc(quotec);
962 13b2bc37 2022-10-23 stsp if (c == EOF)
963 13b2bc37 2022-10-23 stsp return (0);
964 13b2bc37 2022-10-23 stsp if (c == '\n') {
965 13b2bc37 2022-10-23 stsp file->lineno++;
966 13b2bc37 2022-10-23 stsp continue;
967 13b2bc37 2022-10-23 stsp } else if (c == '\\') {
968 13b2bc37 2022-10-23 stsp next = lgetc(quotec);
969 13b2bc37 2022-10-23 stsp if (next == EOF)
970 13b2bc37 2022-10-23 stsp return (0);
971 13b2bc37 2022-10-23 stsp if (next == quotec || c == ' ' || c == '\t')
972 13b2bc37 2022-10-23 stsp c = next;
973 13b2bc37 2022-10-23 stsp else if (next == '\n') {
974 13b2bc37 2022-10-23 stsp file->lineno++;
975 13b2bc37 2022-10-23 stsp continue;
976 13b2bc37 2022-10-23 stsp } else
977 13b2bc37 2022-10-23 stsp lungetc(next);
978 13b2bc37 2022-10-23 stsp } else if (c == quotec) {
979 13b2bc37 2022-10-23 stsp *p = '\0';
980 13b2bc37 2022-10-23 stsp break;
981 13b2bc37 2022-10-23 stsp } else if (c == '\0') {
982 13b2bc37 2022-10-23 stsp yyerror("syntax error");
983 13b2bc37 2022-10-23 stsp return (findeol());
984 13b2bc37 2022-10-23 stsp }
985 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
986 13b2bc37 2022-10-23 stsp yyerror("string too long");
987 13b2bc37 2022-10-23 stsp return (findeol());
988 13b2bc37 2022-10-23 stsp }
989 13b2bc37 2022-10-23 stsp *p++ = c;
990 13b2bc37 2022-10-23 stsp }
991 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
992 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
993 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
994 13b2bc37 2022-10-23 stsp return (STRING);
995 13b2bc37 2022-10-23 stsp }
996 13b2bc37 2022-10-23 stsp
997 13b2bc37 2022-10-23 stsp #define allowed_to_end_number(x) \
998 13b2bc37 2022-10-23 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
999 13b2bc37 2022-10-23 stsp
1000 13b2bc37 2022-10-23 stsp if (c == '-' || isdigit(c)) {
1001 13b2bc37 2022-10-23 stsp do {
1002 13b2bc37 2022-10-23 stsp *p++ = c;
1003 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
1004 13b2bc37 2022-10-23 stsp yyerror("string too long");
1005 13b2bc37 2022-10-23 stsp return (findeol());
1006 13b2bc37 2022-10-23 stsp }
1007 13b2bc37 2022-10-23 stsp c = lgetc(0);
1008 13b2bc37 2022-10-23 stsp } while (c != EOF && isdigit(c));
1009 13b2bc37 2022-10-23 stsp lungetc(c);
1010 13b2bc37 2022-10-23 stsp if (p == buf + 1 && buf[0] == '-')
1011 13b2bc37 2022-10-23 stsp goto nodigits;
1012 13b2bc37 2022-10-23 stsp if (c == EOF || allowed_to_end_number(c)) {
1013 13b2bc37 2022-10-23 stsp const char *errstr = NULL;
1014 13b2bc37 2022-10-23 stsp
1015 13b2bc37 2022-10-23 stsp *p = '\0';
1016 13b2bc37 2022-10-23 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
1017 13b2bc37 2022-10-23 stsp LLONG_MAX, &errstr);
1018 13b2bc37 2022-10-23 stsp if (errstr) {
1019 13b2bc37 2022-10-23 stsp yyerror("\"%s\" invalid number: %s",
1020 13b2bc37 2022-10-23 stsp buf, errstr);
1021 13b2bc37 2022-10-23 stsp return (findeol());
1022 13b2bc37 2022-10-23 stsp }
1023 13b2bc37 2022-10-23 stsp return (NUMBER);
1024 13b2bc37 2022-10-23 stsp } else {
1025 13b2bc37 2022-10-23 stsp nodigits:
1026 13b2bc37 2022-10-23 stsp while (p > buf + 1)
1027 13b2bc37 2022-10-23 stsp lungetc(*--p);
1028 13b2bc37 2022-10-23 stsp c = *--p;
1029 13b2bc37 2022-10-23 stsp if (c == '-')
1030 13b2bc37 2022-10-23 stsp return (c);
1031 13b2bc37 2022-10-23 stsp }
1032 13b2bc37 2022-10-23 stsp }
1033 13b2bc37 2022-10-23 stsp
1034 13b2bc37 2022-10-23 stsp #define allowed_in_string(x) \
1035 13b2bc37 2022-10-23 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1036 13b2bc37 2022-10-23 stsp x != '{' && x != '}' && \
1037 13b2bc37 2022-10-23 stsp x != '!' && x != '=' && x != '#' && \
1038 13b2bc37 2022-10-23 stsp x != ','))
1039 13b2bc37 2022-10-23 stsp
1040 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == ':' || c == '_') {
1041 13b2bc37 2022-10-23 stsp do {
1042 13b2bc37 2022-10-23 stsp *p++ = c;
1043 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
1044 13b2bc37 2022-10-23 stsp yyerror("string too long");
1045 13b2bc37 2022-10-23 stsp return (findeol());
1046 13b2bc37 2022-10-23 stsp }
1047 13b2bc37 2022-10-23 stsp c = lgetc(0);
1048 13b2bc37 2022-10-23 stsp } while (c != EOF && (allowed_in_string(c)));
1049 13b2bc37 2022-10-23 stsp lungetc(c);
1050 13b2bc37 2022-10-23 stsp *p = '\0';
1051 13b2bc37 2022-10-23 stsp token = lookup(buf);
1052 13b2bc37 2022-10-23 stsp if (token == STRING) {
1053 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
1054 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
1055 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
1056 13b2bc37 2022-10-23 stsp }
1057 13b2bc37 2022-10-23 stsp return (token);
1058 13b2bc37 2022-10-23 stsp }
1059 13b2bc37 2022-10-23 stsp if (c == '\n') {
1060 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
1061 13b2bc37 2022-10-23 stsp file->lineno++;
1062 13b2bc37 2022-10-23 stsp }
1063 13b2bc37 2022-10-23 stsp if (c == EOF)
1064 13b2bc37 2022-10-23 stsp return (0);
1065 13b2bc37 2022-10-23 stsp return (c);
1066 13b2bc37 2022-10-23 stsp }
1067 13b2bc37 2022-10-23 stsp
1068 13b2bc37 2022-10-23 stsp int
1069 13b2bc37 2022-10-23 stsp check_file_secrecy(int fd, const char *fname)
1070 13b2bc37 2022-10-23 stsp {
1071 13b2bc37 2022-10-23 stsp struct stat st;
1072 13b2bc37 2022-10-23 stsp
1073 13b2bc37 2022-10-23 stsp if (fstat(fd, &st)) {
1074 13b2bc37 2022-10-23 stsp log_warn("cannot stat %s", fname);
1075 13b2bc37 2022-10-23 stsp return (-1);
1076 13b2bc37 2022-10-23 stsp }
1077 13b2bc37 2022-10-23 stsp if (st.st_uid != 0 && st.st_uid != getuid()) {
1078 13b2bc37 2022-10-23 stsp log_warnx("%s: owner not root or current user", fname);
1079 13b2bc37 2022-10-23 stsp return (-1);
1080 13b2bc37 2022-10-23 stsp }
1081 13b2bc37 2022-10-23 stsp if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
1082 13b2bc37 2022-10-23 stsp log_warnx("%s: group writable or world read/writable", fname);
1083 13b2bc37 2022-10-23 stsp return (-1);
1084 13b2bc37 2022-10-23 stsp }
1085 13b2bc37 2022-10-23 stsp return (0);
1086 13b2bc37 2022-10-23 stsp }
1087 13b2bc37 2022-10-23 stsp
1088 13b2bc37 2022-10-23 stsp struct file *
1089 e9e0377f 2023-03-29 stsp newfile(const char *name, int secret, int required)
1090 13b2bc37 2022-10-23 stsp {
1091 13b2bc37 2022-10-23 stsp struct file *nfile;
1092 13b2bc37 2022-10-23 stsp
1093 13b2bc37 2022-10-23 stsp nfile = calloc(1, sizeof(struct file));
1094 13b2bc37 2022-10-23 stsp if (nfile == NULL) {
1095 13b2bc37 2022-10-23 stsp log_warn("calloc");
1096 13b2bc37 2022-10-23 stsp return (NULL);
1097 13b2bc37 2022-10-23 stsp }
1098 13b2bc37 2022-10-23 stsp nfile->name = strdup(name);
1099 13b2bc37 2022-10-23 stsp if (nfile->name == NULL) {
1100 13b2bc37 2022-10-23 stsp log_warn("strdup");
1101 13b2bc37 2022-10-23 stsp free(nfile);
1102 13b2bc37 2022-10-23 stsp return (NULL);
1103 13b2bc37 2022-10-23 stsp }
1104 13b2bc37 2022-10-23 stsp nfile->stream = fopen(nfile->name, "r");
1105 13b2bc37 2022-10-23 stsp if (nfile->stream == NULL) {
1106 e9e0377f 2023-03-29 stsp if (required)
1107 e9e0377f 2023-03-29 stsp log_warn("open %s", nfile->name);
1108 13b2bc37 2022-10-23 stsp free(nfile->name);
1109 13b2bc37 2022-10-23 stsp free(nfile);
1110 13b2bc37 2022-10-23 stsp return (NULL);
1111 13b2bc37 2022-10-23 stsp } else if (secret &&
1112 13b2bc37 2022-10-23 stsp check_file_secrecy(fileno(nfile->stream), nfile->name)) {
1113 13b2bc37 2022-10-23 stsp fclose(nfile->stream);
1114 13b2bc37 2022-10-23 stsp free(nfile->name);
1115 13b2bc37 2022-10-23 stsp free(nfile);
1116 13b2bc37 2022-10-23 stsp return (NULL);
1117 13b2bc37 2022-10-23 stsp }
1118 13b2bc37 2022-10-23 stsp nfile->lineno = 1;
1119 13b2bc37 2022-10-23 stsp return (nfile);
1120 13b2bc37 2022-10-23 stsp }
1121 13b2bc37 2022-10-23 stsp
1122 13b2bc37 2022-10-23 stsp static void
1123 13b2bc37 2022-10-23 stsp closefile(struct file *xfile)
1124 13b2bc37 2022-10-23 stsp {
1125 13b2bc37 2022-10-23 stsp fclose(xfile->stream);
1126 13b2bc37 2022-10-23 stsp free(xfile->name);
1127 13b2bc37 2022-10-23 stsp free(xfile);
1128 13b2bc37 2022-10-23 stsp }
1129 13b2bc37 2022-10-23 stsp
1130 13b2bc37 2022-10-23 stsp int
1131 13b2bc37 2022-10-23 stsp parse_config(const char *filename, enum gotd_procid proc_id,
1132 4b3827cd 2023-07-08 stsp struct gotd *env)
1133 13b2bc37 2022-10-23 stsp {
1134 13b2bc37 2022-10-23 stsp struct sym *sym, *next;
1135 3b706203 2023-01-02 stsp struct gotd_repo *repo;
1136 4b3827cd 2023-07-08 stsp int require_config_file = (proc_id != PROC_GITWRAPPER);
1137 13b2bc37 2022-10-23 stsp
1138 13b2bc37 2022-10-23 stsp memset(env, 0, sizeof(*env));
1139 13b2bc37 2022-10-23 stsp
1140 13b2bc37 2022-10-23 stsp gotd = env;
1141 13b2bc37 2022-10-23 stsp gotd_proc_id = proc_id;
1142 13b2bc37 2022-10-23 stsp TAILQ_INIT(&gotd->repos);
1143 13b2bc37 2022-10-23 stsp
1144 13b2bc37 2022-10-23 stsp /* Apply default values. */
1145 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
1146 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
1147 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: unix socket path too long", __func__);
1148 13b2bc37 2022-10-23 stsp return -1;
1149 13b2bc37 2022-10-23 stsp }
1150 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, GOTD_USER,
1151 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
1152 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: user name too long", __func__);
1153 13b2bc37 2022-10-23 stsp return -1;
1154 13b2bc37 2022-10-23 stsp }
1155 40b85cca 2023-01-03 stsp
1156 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
1157 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_usec = 0;
1158 13b2bc37 2022-10-23 stsp
1159 e9e0377f 2023-03-29 stsp file = newfile(filename, 0, require_config_file);
1160 3fa763e5 2023-03-01 stsp if (file == NULL)
1161 e9e0377f 2023-03-29 stsp return require_config_file ? -1 : 0;
1162 13b2bc37 2022-10-23 stsp
1163 13b2bc37 2022-10-23 stsp yyparse();
1164 13b2bc37 2022-10-23 stsp errors = file->errors;
1165 13b2bc37 2022-10-23 stsp closefile(file);
1166 13b2bc37 2022-10-23 stsp
1167 13b2bc37 2022-10-23 stsp /* Free macros and check which have not been used. */
1168 13b2bc37 2022-10-23 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1169 13b2bc37 2022-10-23 stsp if ((gotd->verbosity > 1) && !sym->used)
1170 13b2bc37 2022-10-23 stsp fprintf(stderr, "warning: macro '%s' not used\n",
1171 13b2bc37 2022-10-23 stsp sym->nam);
1172 13b2bc37 2022-10-23 stsp if (!sym->persist) {
1173 13b2bc37 2022-10-23 stsp free(sym->nam);
1174 13b2bc37 2022-10-23 stsp free(sym->val);
1175 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1176 13b2bc37 2022-10-23 stsp free(sym);
1177 13b2bc37 2022-10-23 stsp }
1178 13b2bc37 2022-10-23 stsp }
1179 13b2bc37 2022-10-23 stsp
1180 13b2bc37 2022-10-23 stsp if (errors)
1181 13b2bc37 2022-10-23 stsp return (-1);
1182 13b2bc37 2022-10-23 stsp
1183 3b706203 2023-01-02 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1184 3b706203 2023-01-02 stsp if (repo->path[0] == '\0') {
1185 2507ffb7 2023-01-02 stsp log_warnx("repository \"%s\": no path provided in "
1186 2507ffb7 2023-01-02 stsp "configuration file", repo->name);
1187 3b706203 2023-01-02 stsp return (-1);
1188 3b706203 2023-01-02 stsp }
1189 3b706203 2023-01-02 stsp }
1190 3b706203 2023-01-02 stsp
1191 809a54db 2023-01-18 op if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
1192 809a54db 2023-01-18 op log_warnx("no repository defined in configuration file");
1193 809a54db 2023-01-18 op return (-1);
1194 809a54db 2023-01-18 op }
1195 809a54db 2023-01-18 op
1196 13b2bc37 2022-10-23 stsp return (0);
1197 13b2bc37 2022-10-23 stsp }
1198 13b2bc37 2022-10-23 stsp
1199 40b85cca 2023-01-03 stsp static int
1200 40b85cca 2023-01-03 stsp uid_connection_limit_cmp(const void *pa, const void *pb)
1201 40b85cca 2023-01-03 stsp {
1202 40b85cca 2023-01-03 stsp const struct gotd_uid_connection_limit *a = pa, *b = pb;
1203 40b85cca 2023-01-03 stsp
1204 40b85cca 2023-01-03 stsp if (a->uid < b->uid)
1205 40b85cca 2023-01-03 stsp return -1;
1206 40b85cca 2023-01-03 stsp else if (a->uid > b->uid);
1207 40b85cca 2023-01-03 stsp return 1;
1208 40b85cca 2023-01-03 stsp
1209 40b85cca 2023-01-03 stsp return 0;
1210 40b85cca 2023-01-03 stsp }
1211 40b85cca 2023-01-03 stsp
1212 40b85cca 2023-01-03 stsp static int
1213 40b85cca 2023-01-03 stsp conf_limit_user_connections(const char *user, int maximum)
1214 40b85cca 2023-01-03 stsp {
1215 40b85cca 2023-01-03 stsp uid_t uid;
1216 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *limit;
1217 40b85cca 2023-01-03 stsp size_t nlimits;
1218 40b85cca 2023-01-03 stsp
1219 40b85cca 2023-01-03 stsp if (maximum < 1) {
1220 40b85cca 2023-01-03 stsp yyerror("max connections cannot be smaller 1");
1221 40b85cca 2023-01-03 stsp return -1;
1222 40b85cca 2023-01-03 stsp }
1223 40b85cca 2023-01-03 stsp if (maximum > GOTD_MAXCLIENTS) {
1224 40b85cca 2023-01-03 stsp yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
1225 40b85cca 2023-01-03 stsp return -1;
1226 40b85cca 2023-01-03 stsp }
1227 40b85cca 2023-01-03 stsp
1228 1963be61 2023-04-14 stsp if (gotd_parseuid(user, &uid) == -1) {
1229 40b85cca 2023-01-03 stsp yyerror("%s: no such user", user);
1230 40b85cca 2023-01-03 stsp return -1;
1231 40b85cca 2023-01-03 stsp }
1232 40b85cca 2023-01-03 stsp
1233 40b85cca 2023-01-03 stsp limit = gotd_find_uid_connection_limit(gotd->connection_limits,
1234 40b85cca 2023-01-03 stsp gotd->nconnection_limits, uid);
1235 40b85cca 2023-01-03 stsp if (limit) {
1236 40b85cca 2023-01-03 stsp limit->max_connections = maximum;
1237 40b85cca 2023-01-03 stsp return 0;
1238 40b85cca 2023-01-03 stsp }
1239 40b85cca 2023-01-03 stsp
1240 40b85cca 2023-01-03 stsp limit = gotd->connection_limits;
1241 40b85cca 2023-01-03 stsp nlimits = gotd->nconnection_limits + 1;
1242 40b85cca 2023-01-03 stsp limit = reallocarray(limit, nlimits, sizeof(*limit));
1243 40b85cca 2023-01-03 stsp if (limit == NULL)
1244 40b85cca 2023-01-03 stsp fatal("reallocarray");
1245 40b85cca 2023-01-03 stsp
1246 40b85cca 2023-01-03 stsp limit[nlimits - 1].uid = uid;
1247 40b85cca 2023-01-03 stsp limit[nlimits - 1].max_connections = maximum;
1248 40b85cca 2023-01-03 stsp
1249 40b85cca 2023-01-03 stsp gotd->connection_limits = limit;
1250 40b85cca 2023-01-03 stsp gotd->nconnection_limits = nlimits;
1251 40b85cca 2023-01-03 stsp qsort(gotd->connection_limits, gotd->nconnection_limits,
1252 40b85cca 2023-01-03 stsp sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
1253 40b85cca 2023-01-03 stsp
1254 40b85cca 2023-01-03 stsp return 0;
1255 40b85cca 2023-01-03 stsp }
1256 40b85cca 2023-01-03 stsp
1257 13b2bc37 2022-10-23 stsp static struct gotd_repo *
1258 13b2bc37 2022-10-23 stsp conf_new_repo(const char *name)
1259 13b2bc37 2022-10-23 stsp {
1260 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
1261 13b2bc37 2022-10-23 stsp
1262 7683f79a 2023-01-02 stsp if (name[0] == '\0') {
1263 7683f79a 2023-01-02 stsp fatalx("syntax error: empty repository name found in %s",
1264 7683f79a 2023-01-02 stsp file->name);
1265 7683f79a 2023-01-02 stsp }
1266 7683f79a 2023-01-02 stsp
1267 2507ffb7 2023-01-02 stsp if (strchr(name, '\n') != NULL)
1268 2507ffb7 2023-01-02 stsp fatalx("repository names must not contain linefeeds: %s", name);
1269 13b2bc37 2022-10-23 stsp
1270 13b2bc37 2022-10-23 stsp repo = calloc(1, sizeof(*repo));
1271 13b2bc37 2022-10-23 stsp if (repo == NULL)
1272 13b2bc37 2022-10-23 stsp fatalx("%s: calloc", __func__);
1273 13b2bc37 2022-10-23 stsp
1274 0ccf3acb 2022-11-16 stsp STAILQ_INIT(&repo->rules);
1275 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_tag_namespaces);
1276 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branch_namespaces);
1277 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branches);
1278 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&repo->protected_branches);
1279 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&repo->notification_refs);
1280 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&repo->notification_ref_namespaces);
1281 ba97b2d7 2024-03-20 stsp STAILQ_INIT(&repo->notification_targets);
1282 0ccf3acb 2022-11-16 stsp
1283 13b2bc37 2022-10-23 stsp if (strlcpy(repo->name, name, sizeof(repo->name)) >=
1284 13b2bc37 2022-10-23 stsp sizeof(repo->name))
1285 13b2bc37 2022-10-23 stsp fatalx("%s: strlcpy", __func__);
1286 13b2bc37 2022-10-23 stsp
1287 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1288 13b2bc37 2022-10-23 stsp gotd->nrepos++;
1289 13b2bc37 2022-10-23 stsp
1290 13b2bc37 2022-10-23 stsp return repo;
1291 13b2bc37 2022-10-23 stsp };
1292 0ccf3acb 2022-11-16 stsp
1293 0ccf3acb 2022-11-16 stsp static void
1294 0ccf3acb 2022-11-16 stsp conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
1295 0ccf3acb 2022-11-16 stsp int authorization, char *identifier)
1296 0ccf3acb 2022-11-16 stsp {
1297 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
1298 0ccf3acb 2022-11-16 stsp
1299 0ccf3acb 2022-11-16 stsp rule = calloc(1, sizeof(*rule));
1300 0ccf3acb 2022-11-16 stsp if (rule == NULL)
1301 0ccf3acb 2022-11-16 stsp fatal("calloc");
1302 13b2bc37 2022-10-23 stsp
1303 0ccf3acb 2022-11-16 stsp rule->access = access;
1304 0ccf3acb 2022-11-16 stsp rule->authorization = authorization;
1305 0ccf3acb 2022-11-16 stsp rule->identifier = identifier;
1306 0ccf3acb 2022-11-16 stsp
1307 0ccf3acb 2022-11-16 stsp STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1308 0ccf3acb 2022-11-16 stsp }
1309 0ccf3acb 2022-11-16 stsp
1310 9afa3de2 2023-04-04 stsp static int
1311 9afa3de2 2023-04-04 stsp refname_is_valid(char *refname)
1312 9afa3de2 2023-04-04 stsp {
1313 d571a176 2023-06-14 op if (strncmp(refname, "refs/", 5) != 0) {
1314 9afa3de2 2023-04-04 stsp yyerror("reference name must begin with \"refs/\": %s",
1315 9afa3de2 2023-04-04 stsp refname);
1316 9afa3de2 2023-04-04 stsp return 0;
1317 9afa3de2 2023-04-04 stsp }
1318 9afa3de2 2023-04-04 stsp
1319 9afa3de2 2023-04-04 stsp if (!got_ref_name_is_valid(refname)) {
1320 9afa3de2 2023-04-04 stsp yyerror("invalid reference name: %s", refname);
1321 9afa3de2 2023-04-04 stsp return 0;
1322 9afa3de2 2023-04-04 stsp }
1323 9afa3de2 2023-04-04 stsp
1324 9afa3de2 2023-04-04 stsp return 1;
1325 9afa3de2 2023-04-04 stsp }
1326 9afa3de2 2023-04-04 stsp
1327 9afa3de2 2023-04-04 stsp static int
1328 f850236e 2023-04-05 stsp conf_protect_ref_namespace(char **new, struct got_pathlist_head *refs,
1329 f850236e 2023-04-05 stsp char *namespace)
1330 9afa3de2 2023-04-04 stsp {
1331 9afa3de2 2023-04-04 stsp const struct got_error *error;
1332 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1333 9afa3de2 2023-04-04 stsp char *s;
1334 9afa3de2 2023-04-04 stsp
1335 f850236e 2023-04-05 stsp *new = NULL;
1336 f850236e 2023-04-05 stsp
1337 9afa3de2 2023-04-04 stsp got_path_strip_trailing_slashes(namespace);
1338 9afa3de2 2023-04-04 stsp if (!refname_is_valid(namespace))
1339 9afa3de2 2023-04-04 stsp return -1;
1340 9afa3de2 2023-04-04 stsp if (asprintf(&s, "%s/", namespace) == -1) {
1341 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
1342 9afa3de2 2023-04-04 stsp return -1;
1343 9afa3de2 2023-04-04 stsp }
1344 9afa3de2 2023-04-04 stsp
1345 f850236e 2023-04-05 stsp error = got_pathlist_insert(&pe, refs, s, NULL);
1346 f850236e 2023-04-05 stsp if (error || pe == NULL) {
1347 f0426190 2023-04-05 op free(s);
1348 f0426190 2023-04-05 op if (error)
1349 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1350 f0426190 2023-04-05 op else
1351 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1352 9afa3de2 2023-04-04 stsp return -1;
1353 9afa3de2 2023-04-04 stsp }
1354 9afa3de2 2023-04-04 stsp
1355 f850236e 2023-04-05 stsp *new = s;
1356 9afa3de2 2023-04-04 stsp return 0;
1357 9afa3de2 2023-04-04 stsp }
1358 9afa3de2 2023-04-04 stsp
1359 9afa3de2 2023-04-04 stsp static int
1360 9afa3de2 2023-04-04 stsp conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1361 9afa3de2 2023-04-04 stsp {
1362 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1363 f850236e 2023-04-05 stsp char *new;
1364 f850236e 2023-04-05 stsp
1365 f850236e 2023-04-05 stsp if (conf_protect_ref_namespace(&new, &repo->protected_tag_namespaces,
1366 f850236e 2023-04-05 stsp namespace) == -1)
1367 f850236e 2023-04-05 stsp return -1;
1368 f850236e 2023-04-05 stsp
1369 f850236e 2023-04-05 stsp TAILQ_FOREACH(pe, &repo->protected_branch_namespaces, entry) {
1370 f850236e 2023-04-05 stsp if (strcmp(pe->path, new) == 0) {
1371 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1372 f850236e 2023-04-05 stsp return -1;
1373 f850236e 2023-04-05 stsp }
1374 f850236e 2023-04-05 stsp }
1375 f850236e 2023-04-05 stsp
1376 f850236e 2023-04-05 stsp return 0;
1377 9afa3de2 2023-04-04 stsp }
1378 9afa3de2 2023-04-04 stsp
1379 9afa3de2 2023-04-04 stsp static int
1380 9afa3de2 2023-04-04 stsp conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1381 9afa3de2 2023-04-04 stsp {
1382 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1383 f850236e 2023-04-05 stsp char *new;
1384 f850236e 2023-04-05 stsp
1385 f850236e 2023-04-05 stsp if (conf_protect_ref_namespace(&new,
1386 f850236e 2023-04-05 stsp &repo->protected_branch_namespaces, namespace) == -1)
1387 f850236e 2023-04-05 stsp return -1;
1388 f850236e 2023-04-05 stsp
1389 f850236e 2023-04-05 stsp TAILQ_FOREACH(pe, &repo->protected_tag_namespaces, entry) {
1390 f850236e 2023-04-05 stsp if (strcmp(pe->path, new) == 0) {
1391 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1392 f850236e 2023-04-05 stsp return -1;
1393 f850236e 2023-04-05 stsp }
1394 f850236e 2023-04-05 stsp }
1395 f850236e 2023-04-05 stsp
1396 f850236e 2023-04-05 stsp return 0;
1397 9afa3de2 2023-04-04 stsp }
1398 9afa3de2 2023-04-04 stsp
1399 9afa3de2 2023-04-04 stsp static int
1400 9afa3de2 2023-04-04 stsp conf_protect_branch(struct gotd_repo *repo, char *branchname)
1401 9afa3de2 2023-04-04 stsp {
1402 9afa3de2 2023-04-04 stsp const struct got_error *error;
1403 f0426190 2023-04-05 op struct got_pathlist_entry *new;
1404 9afa3de2 2023-04-04 stsp char *refname;
1405 9afa3de2 2023-04-04 stsp
1406 9afa3de2 2023-04-04 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
1407 9afa3de2 2023-04-04 stsp if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1408 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
1409 9afa3de2 2023-04-04 stsp return -1;
1410 9afa3de2 2023-04-04 stsp }
1411 9afa3de2 2023-04-04 stsp } else {
1412 9afa3de2 2023-04-04 stsp refname = strdup(branchname);
1413 9afa3de2 2023-04-04 stsp if (refname == NULL) {
1414 9afa3de2 2023-04-04 stsp yyerror("strdup: %s", strerror(errno));
1415 9afa3de2 2023-04-04 stsp return -1;
1416 9afa3de2 2023-04-04 stsp }
1417 9afa3de2 2023-04-04 stsp }
1418 9afa3de2 2023-04-04 stsp
1419 9afa3de2 2023-04-04 stsp if (!refname_is_valid(refname)) {
1420 9afa3de2 2023-04-04 stsp free(refname);
1421 9afa3de2 2023-04-04 stsp return -1;
1422 9afa3de2 2023-04-04 stsp }
1423 9afa3de2 2023-04-04 stsp
1424 f0426190 2023-04-05 op error = got_pathlist_insert(&new, &repo->protected_branches,
1425 9afa3de2 2023-04-04 stsp refname, NULL);
1426 f0426190 2023-04-05 op if (error || new == NULL) {
1427 f0426190 2023-04-05 op free(refname);
1428 f0426190 2023-04-05 op if (error)
1429 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1430 f0426190 2023-04-05 op else
1431 f0426190 2023-04-05 op yyerror("duplicate protect branch %s", branchname);
1432 ba97b2d7 2024-03-20 stsp return -1;
1433 ba97b2d7 2024-03-20 stsp }
1434 ba97b2d7 2024-03-20 stsp
1435 ba97b2d7 2024-03-20 stsp return 0;
1436 ba97b2d7 2024-03-20 stsp }
1437 ba97b2d7 2024-03-20 stsp
1438 ba97b2d7 2024-03-20 stsp static int
1439 ba97b2d7 2024-03-20 stsp conf_notify_branch(struct gotd_repo *repo, char *branchname)
1440 ba97b2d7 2024-03-20 stsp {
1441 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1442 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1443 ba97b2d7 2024-03-20 stsp char *refname;
1444 ba97b2d7 2024-03-20 stsp
1445 ba97b2d7 2024-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
1446 ba97b2d7 2024-03-20 stsp if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
1447 ba97b2d7 2024-03-20 stsp yyerror("asprintf: %s", strerror(errno));
1448 ba97b2d7 2024-03-20 stsp return -1;
1449 ba97b2d7 2024-03-20 stsp }
1450 ba97b2d7 2024-03-20 stsp } else {
1451 ba97b2d7 2024-03-20 stsp refname = strdup(branchname);
1452 ba97b2d7 2024-03-20 stsp if (refname == NULL) {
1453 ba97b2d7 2024-03-20 stsp yyerror("strdup: %s", strerror(errno));
1454 ba97b2d7 2024-03-20 stsp return -1;
1455 ba97b2d7 2024-03-20 stsp }
1456 ba97b2d7 2024-03-20 stsp }
1457 ba97b2d7 2024-03-20 stsp
1458 ba97b2d7 2024-03-20 stsp if (!refname_is_valid(refname)) {
1459 ba97b2d7 2024-03-20 stsp free(refname);
1460 9afa3de2 2023-04-04 stsp return -1;
1461 9afa3de2 2023-04-04 stsp }
1462 9afa3de2 2023-04-04 stsp
1463 ba97b2d7 2024-03-20 stsp error = got_pathlist_insert(&pe, &repo->notification_refs,
1464 ba97b2d7 2024-03-20 stsp refname, NULL);
1465 ba97b2d7 2024-03-20 stsp if (error) {
1466 ba97b2d7 2024-03-20 stsp free(refname);
1467 ba97b2d7 2024-03-20 stsp yyerror("got_pathlist_insert: %s", error->msg);
1468 ba97b2d7 2024-03-20 stsp return -1;
1469 ba97b2d7 2024-03-20 stsp }
1470 ba97b2d7 2024-03-20 stsp if (pe == NULL)
1471 ba97b2d7 2024-03-20 stsp free(refname);
1472 ba97b2d7 2024-03-20 stsp
1473 9afa3de2 2023-04-04 stsp return 0;
1474 9afa3de2 2023-04-04 stsp }
1475 9afa3de2 2023-04-04 stsp
1476 ba97b2d7 2024-03-20 stsp static int
1477 ba97b2d7 2024-03-20 stsp conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1478 ba97b2d7 2024-03-20 stsp {
1479 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1480 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1481 ba97b2d7 2024-03-20 stsp char *s;
1482 ba97b2d7 2024-03-20 stsp
1483 ba97b2d7 2024-03-20 stsp got_path_strip_trailing_slashes(namespace);
1484 ba97b2d7 2024-03-20 stsp if (!refname_is_valid(namespace))
1485 ba97b2d7 2024-03-20 stsp return -1;
1486 ba97b2d7 2024-03-20 stsp
1487 ba97b2d7 2024-03-20 stsp if (asprintf(&s, "%s/", namespace) == -1) {
1488 ba97b2d7 2024-03-20 stsp yyerror("asprintf: %s", strerror(errno));
1489 ba97b2d7 2024-03-20 stsp return -1;
1490 ba97b2d7 2024-03-20 stsp }
1491 ba97b2d7 2024-03-20 stsp
1492 ba97b2d7 2024-03-20 stsp error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1493 ba97b2d7 2024-03-20 stsp s, NULL);
1494 ba97b2d7 2024-03-20 stsp if (error) {
1495 ba97b2d7 2024-03-20 stsp free(s);
1496 ba97b2d7 2024-03-20 stsp yyerror("got_pathlist_insert: %s", error->msg);
1497 ba97b2d7 2024-03-20 stsp return -1;
1498 ba97b2d7 2024-03-20 stsp }
1499 ba97b2d7 2024-03-20 stsp if (pe == NULL)
1500 ba97b2d7 2024-03-20 stsp free(s);
1501 ba97b2d7 2024-03-20 stsp
1502 ba97b2d7 2024-03-20 stsp return 0;
1503 ba97b2d7 2024-03-20 stsp }
1504 ba97b2d7 2024-03-20 stsp
1505 ba97b2d7 2024-03-20 stsp static int
1506 ba97b2d7 2024-03-20 stsp conf_notify_email(struct gotd_repo *repo, char *sender, char *recipient,
1507 ba97b2d7 2024-03-20 stsp char *responder, char *hostname, char *port)
1508 ba97b2d7 2024-03-20 stsp {
1509 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
1510 ba97b2d7 2024-03-20 stsp
1511 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1512 ba97b2d7 2024-03-20 stsp if (target->type != GOTD_NOTIFICATION_VIA_EMAIL)
1513 ba97b2d7 2024-03-20 stsp continue;
1514 ba97b2d7 2024-03-20 stsp if (strcmp(target->conf.email.recipient, recipient) == 0) {
1515 ba97b2d7 2024-03-20 stsp yyerror("duplicate email notification for '%s' in "
1516 ba97b2d7 2024-03-20 stsp "repository '%s'", recipient, repo->name);
1517 ba97b2d7 2024-03-20 stsp return -1;
1518 ba97b2d7 2024-03-20 stsp }
1519 ba97b2d7 2024-03-20 stsp }
1520 ba97b2d7 2024-03-20 stsp
1521 ba97b2d7 2024-03-20 stsp target = calloc(1, sizeof(*target));
1522 ba97b2d7 2024-03-20 stsp if (target == NULL)
1523 ba97b2d7 2024-03-20 stsp fatal("calloc");
1524 ba97b2d7 2024-03-20 stsp target->type = GOTD_NOTIFICATION_VIA_EMAIL;
1525 ba97b2d7 2024-03-20 stsp if (sender) {
1526 ba97b2d7 2024-03-20 stsp target->conf.email.sender = strdup(sender);
1527 ba97b2d7 2024-03-20 stsp if (target->conf.email.sender == NULL)
1528 ba97b2d7 2024-03-20 stsp fatal("strdup");
1529 ba97b2d7 2024-03-20 stsp }
1530 ba97b2d7 2024-03-20 stsp target->conf.email.recipient = strdup(recipient);
1531 ba97b2d7 2024-03-20 stsp if (target->conf.email.recipient == NULL)
1532 ba97b2d7 2024-03-20 stsp fatal("strdup");
1533 ba97b2d7 2024-03-20 stsp if (responder) {
1534 ba97b2d7 2024-03-20 stsp target->conf.email.responder = strdup(responder);
1535 ba97b2d7 2024-03-20 stsp if (target->conf.email.responder == NULL)
1536 ba97b2d7 2024-03-20 stsp fatal("strdup");
1537 ba97b2d7 2024-03-20 stsp }
1538 ba97b2d7 2024-03-20 stsp if (hostname) {
1539 ba97b2d7 2024-03-20 stsp target->conf.email.hostname = strdup(hostname);
1540 ba97b2d7 2024-03-20 stsp if (target->conf.email.hostname == NULL)
1541 ba97b2d7 2024-03-20 stsp fatal("strdup");
1542 ba97b2d7 2024-03-20 stsp }
1543 ba97b2d7 2024-03-20 stsp if (port) {
1544 ba97b2d7 2024-03-20 stsp target->conf.email.port = strdup(port);
1545 ba97b2d7 2024-03-20 stsp if (target->conf.email.port == NULL)
1546 ba97b2d7 2024-03-20 stsp fatal("strdup");
1547 ba97b2d7 2024-03-20 stsp }
1548 ba97b2d7 2024-03-20 stsp
1549 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1550 ba97b2d7 2024-03-20 stsp return 0;
1551 ba97b2d7 2024-03-20 stsp }
1552 ba97b2d7 2024-03-20 stsp
1553 ba97b2d7 2024-03-20 stsp static int
1554 55286da2 2024-04-18 stsp conf_notify_http(struct gotd_repo *repo, char *url, char *user, char *password,
1555 55286da2 2024-04-18 stsp int insecure)
1556 ba97b2d7 2024-03-20 stsp {
1557 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1558 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
1559 5565365c 2024-03-27 op char *proto, *hostname, *port, *path;
1560 5565365c 2024-03-27 op int tls = 0, ret = 0;
1561 ba97b2d7 2024-03-20 stsp
1562 5565365c 2024-03-27 op error = gotd_parse_url(&proto, &hostname, &port, &path, url);
1563 ba97b2d7 2024-03-20 stsp if (error) {
1564 ba97b2d7 2024-03-20 stsp yyerror("invalid HTTP notification URL '%s' in "
1565 ba97b2d7 2024-03-20 stsp "repository '%s': %s", url, repo->name, error->msg);
1566 ba97b2d7 2024-03-20 stsp return -1;
1567 ba97b2d7 2024-03-20 stsp }
1568 ba97b2d7 2024-03-20 stsp
1569 5565365c 2024-03-27 op tls = !strcmp(proto, "https");
1570 5565365c 2024-03-27 op
1571 ba97b2d7 2024-03-20 stsp if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1572 ba97b2d7 2024-03-20 stsp yyerror("invalid protocol '%s' in notification URL '%s' in "
1573 ba97b2d7 2024-03-20 stsp "repository '%s", proto, url, repo->name);
1574 5565365c 2024-03-27 op ret = -1;
1575 5565365c 2024-03-27 op goto done;
1576 cb557a49 2024-04-18 stsp }
1577 cb557a49 2024-04-18 stsp
1578 cb557a49 2024-04-18 stsp if (port == NULL) {
1579 cb557a49 2024-04-18 stsp if (strcmp(proto, "http") == 0)
1580 cb557a49 2024-04-18 stsp port = strdup("80");
1581 cb557a49 2024-04-18 stsp if (strcmp(proto, "https") == 0)
1582 cb557a49 2024-04-18 stsp port = strdup("443");
1583 cb557a49 2024-04-18 stsp if (port == NULL) {
1584 cb557a49 2024-04-18 stsp error = got_error_from_errno("strdup");
1585 cb557a49 2024-04-18 stsp ret = -1;
1586 cb557a49 2024-04-18 stsp goto done;
1587 cb557a49 2024-04-18 stsp }
1588 5565365c 2024-03-27 op }
1589 5565365c 2024-03-27 op
1590 5565365c 2024-03-27 op if ((user != NULL && password == NULL) ||
1591 5565365c 2024-03-27 op (user == NULL && password != NULL)) {
1592 5565365c 2024-03-27 op yyerror("missing username or password");
1593 ba97b2d7 2024-03-20 stsp ret = -1;
1594 ba97b2d7 2024-03-20 stsp goto done;
1595 ba97b2d7 2024-03-20 stsp }
1596 ba97b2d7 2024-03-20 stsp
1597 55286da2 2024-04-18 stsp if (!insecure && strcmp(proto, "http") == 0 &&
1598 55286da2 2024-04-18 stsp (user != NULL || password != NULL)) {
1599 55286da2 2024-04-18 stsp yyerror("%s: HTTP notifications with basic authentication "
1600 55286da2 2024-04-18 stsp "over plaintext HTTP will leak credentials; add the "
1601 55286da2 2024-04-18 stsp "'insecure' config keyword if this is intentional", url);
1602 55286da2 2024-04-18 stsp ret = -1;
1603 55286da2 2024-04-18 stsp goto done;
1604 ba97b2d7 2024-03-20 stsp }
1605 ba97b2d7 2024-03-20 stsp
1606 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1607 ba97b2d7 2024-03-20 stsp if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1608 ba97b2d7 2024-03-20 stsp continue;
1609 5565365c 2024-03-27 op if (target->conf.http.tls == tls &&
1610 5565365c 2024-03-27 op !strcmp(target->conf.http.hostname, hostname) &&
1611 5565365c 2024-03-27 op !strcmp(target->conf.http.port, port) &&
1612 5565365c 2024-03-27 op !strcmp(target->conf.http.path, path)) {
1613 ba97b2d7 2024-03-20 stsp yyerror("duplicate notification for URL '%s' in "
1614 ba97b2d7 2024-03-20 stsp "repository '%s'", url, repo->name);
1615 ba97b2d7 2024-03-20 stsp ret = -1;
1616 ba97b2d7 2024-03-20 stsp goto done;
1617 ba97b2d7 2024-03-20 stsp }
1618 ba97b2d7 2024-03-20 stsp }
1619 ba97b2d7 2024-03-20 stsp
1620 ba97b2d7 2024-03-20 stsp target = calloc(1, sizeof(*target));
1621 ba97b2d7 2024-03-20 stsp if (target == NULL)
1622 ba97b2d7 2024-03-20 stsp fatal("calloc");
1623 ba97b2d7 2024-03-20 stsp target->type = GOTD_NOTIFICATION_VIA_HTTP;
1624 5565365c 2024-03-27 op target->conf.http.tls = tls;
1625 5565365c 2024-03-27 op target->conf.http.hostname = hostname;
1626 5565365c 2024-03-27 op target->conf.http.port = port;
1627 5565365c 2024-03-27 op target->conf.http.path = path;
1628 5565365c 2024-03-27 op hostname = port = path = NULL;
1629 5565365c 2024-03-27 op
1630 ba97b2d7 2024-03-20 stsp if (user) {
1631 ba97b2d7 2024-03-20 stsp target->conf.http.user = strdup(user);
1632 ba97b2d7 2024-03-20 stsp if (target->conf.http.user == NULL)
1633 5565365c 2024-03-27 op fatal("strdup");
1634 ba97b2d7 2024-03-20 stsp target->conf.http.password = strdup(password);
1635 ba97b2d7 2024-03-20 stsp if (target->conf.http.password == NULL)
1636 5565365c 2024-03-27 op fatal("strdup");
1637 ba97b2d7 2024-03-20 stsp }
1638 ba97b2d7 2024-03-20 stsp
1639 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1640 ba97b2d7 2024-03-20 stsp done:
1641 ba97b2d7 2024-03-20 stsp free(proto);
1642 5565365c 2024-03-27 op free(hostname);
1643 ba97b2d7 2024-03-20 stsp free(port);
1644 5565365c 2024-03-27 op free(path);
1645 ba97b2d7 2024-03-20 stsp return ret;
1646 ba97b2d7 2024-03-20 stsp }
1647 ba97b2d7 2024-03-20 stsp
1648 13b2bc37 2022-10-23 stsp int
1649 13b2bc37 2022-10-23 stsp symset(const char *nam, const char *val, int persist)
1650 13b2bc37 2022-10-23 stsp {
1651 13b2bc37 2022-10-23 stsp struct sym *sym;
1652 13b2bc37 2022-10-23 stsp
1653 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1654 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0)
1655 13b2bc37 2022-10-23 stsp break;
1656 13b2bc37 2022-10-23 stsp }
1657 13b2bc37 2022-10-23 stsp
1658 13b2bc37 2022-10-23 stsp if (sym != NULL) {
1659 13b2bc37 2022-10-23 stsp if (sym->persist == 1)
1660 13b2bc37 2022-10-23 stsp return (0);
1661 13b2bc37 2022-10-23 stsp else {
1662 13b2bc37 2022-10-23 stsp free(sym->nam);
1663 13b2bc37 2022-10-23 stsp free(sym->val);
1664 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1665 13b2bc37 2022-10-23 stsp free(sym);
1666 13b2bc37 2022-10-23 stsp }
1667 13b2bc37 2022-10-23 stsp }
1668 13b2bc37 2022-10-23 stsp sym = calloc(1, sizeof(*sym));
1669 13b2bc37 2022-10-23 stsp if (sym == NULL)
1670 13b2bc37 2022-10-23 stsp return (-1);
1671 13b2bc37 2022-10-23 stsp
1672 13b2bc37 2022-10-23 stsp sym->nam = strdup(nam);
1673 13b2bc37 2022-10-23 stsp if (sym->nam == NULL) {
1674 13b2bc37 2022-10-23 stsp free(sym);
1675 13b2bc37 2022-10-23 stsp return (-1);
1676 13b2bc37 2022-10-23 stsp }
1677 13b2bc37 2022-10-23 stsp sym->val = strdup(val);
1678 13b2bc37 2022-10-23 stsp if (sym->val == NULL) {
1679 13b2bc37 2022-10-23 stsp free(sym->nam);
1680 13b2bc37 2022-10-23 stsp free(sym);
1681 13b2bc37 2022-10-23 stsp return (-1);
1682 13b2bc37 2022-10-23 stsp }
1683 13b2bc37 2022-10-23 stsp sym->used = 0;
1684 13b2bc37 2022-10-23 stsp sym->persist = persist;
1685 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
1686 13b2bc37 2022-10-23 stsp return (0);
1687 13b2bc37 2022-10-23 stsp }
1688 13b2bc37 2022-10-23 stsp
1689 13b2bc37 2022-10-23 stsp char *
1690 13b2bc37 2022-10-23 stsp symget(const char *nam)
1691 13b2bc37 2022-10-23 stsp {
1692 13b2bc37 2022-10-23 stsp struct sym *sym;
1693 13b2bc37 2022-10-23 stsp
1694 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1695 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0) {
1696 13b2bc37 2022-10-23 stsp sym->used = 1;
1697 13b2bc37 2022-10-23 stsp return (sym->val);
1698 13b2bc37 2022-10-23 stsp }
1699 13b2bc37 2022-10-23 stsp }
1700 13b2bc37 2022-10-23 stsp return (NULL);
1701 b09c1279 2023-03-28 stsp }
1702 b09c1279 2023-03-28 stsp
1703 b09c1279 2023-03-28 stsp struct gotd_repo *
1704 ba97b2d7 2024-03-20 stsp gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1705 b09c1279 2023-03-28 stsp {
1706 b09c1279 2023-03-28 stsp struct gotd_repo *repo;
1707 b09c1279 2023-03-28 stsp size_t namelen;
1708 b09c1279 2023-03-28 stsp
1709 ba97b2d7 2024-03-20 stsp TAILQ_FOREACH(repo, repos, entry) {
1710 b09c1279 2023-03-28 stsp namelen = strlen(repo->name);
1711 b09c1279 2023-03-28 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
1712 b09c1279 2023-03-28 stsp continue;
1713 b09c1279 2023-03-28 stsp if (repo_name[namelen] == '\0' ||
1714 b09c1279 2023-03-28 stsp strcmp(&repo_name[namelen], ".git") == 0)
1715 b09c1279 2023-03-28 stsp return repo;
1716 b09c1279 2023-03-28 stsp }
1717 b09c1279 2023-03-28 stsp
1718 b09c1279 2023-03-28 stsp return NULL;
1719 13b2bc37 2022-10-23 stsp }
1720 9afa3de2 2023-04-04 stsp
1721 9afa3de2 2023-04-04 stsp struct gotd_repo *
1722 9afa3de2 2023-04-04 stsp gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1723 9afa3de2 2023-04-04 stsp {
1724 9afa3de2 2023-04-04 stsp struct gotd_repo *repo;
1725 9afa3de2 2023-04-04 stsp
1726 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1727 9afa3de2 2023-04-04 stsp if (strcmp(repo->path, repo_path) == 0)
1728 9afa3de2 2023-04-04 stsp return repo;
1729 eeb616b7 2023-04-14 stsp }
1730 eeb616b7 2023-04-14 stsp
1731 eeb616b7 2023-04-14 stsp return NULL;
1732 eeb616b7 2023-04-14 stsp }
1733 eeb616b7 2023-04-14 stsp
1734 eeb616b7 2023-04-14 stsp struct gotd_uid_connection_limit *
1735 eeb616b7 2023-04-14 stsp gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1736 eeb616b7 2023-04-14 stsp size_t nlimits, uid_t uid)
1737 eeb616b7 2023-04-14 stsp {
1738 eeb616b7 2023-04-14 stsp /* This array is always sorted to allow for binary search. */
1739 eeb616b7 2023-04-14 stsp int i, left = 0, right = nlimits - 1;
1740 eeb616b7 2023-04-14 stsp
1741 eeb616b7 2023-04-14 stsp while (left <= right) {
1742 eeb616b7 2023-04-14 stsp i = ((left + right) / 2);
1743 eeb616b7 2023-04-14 stsp if (limits[i].uid == uid)
1744 eeb616b7 2023-04-14 stsp return &limits[i];
1745 eeb616b7 2023-04-14 stsp if (limits[i].uid > uid)
1746 eeb616b7 2023-04-14 stsp left = i + 1;
1747 eeb616b7 2023-04-14 stsp else
1748 eeb616b7 2023-04-14 stsp right = i - 1;
1749 9afa3de2 2023-04-04 stsp }
1750 9afa3de2 2023-04-04 stsp
1751 9afa3de2 2023-04-04 stsp return NULL;
1752 9afa3de2 2023-04-04 stsp }
1753 1963be61 2023-04-14 stsp
1754 1963be61 2023-04-14 stsp int
1755 1963be61 2023-04-14 stsp gotd_parseuid(const char *s, uid_t *uid)
1756 1963be61 2023-04-14 stsp {
1757 1963be61 2023-04-14 stsp struct passwd *pw;
1758 1963be61 2023-04-14 stsp const char *errstr;
1759 1963be61 2023-04-14 stsp
1760 1963be61 2023-04-14 stsp if ((pw = getpwnam(s)) != NULL) {
1761 1963be61 2023-04-14 stsp *uid = pw->pw_uid;
1762 1963be61 2023-04-14 stsp if (*uid == UID_MAX)
1763 1963be61 2023-04-14 stsp return -1;
1764 1963be61 2023-04-14 stsp return 0;
1765 1963be61 2023-04-14 stsp }
1766 1963be61 2023-04-14 stsp *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1767 1963be61 2023-04-14 stsp if (errstr)
1768 1963be61 2023-04-14 stsp return -1;
1769 1963be61 2023-04-14 stsp return 0;
1770 1963be61 2023-04-14 stsp }
1771 ba97b2d7 2024-03-20 stsp
1772 ba97b2d7 2024-03-20 stsp const struct got_error *
1773 ba97b2d7 2024-03-20 stsp gotd_parse_url(char **proto, char **host, char **port,
1774 ba97b2d7 2024-03-20 stsp char **request_path, const char *url)
1775 ba97b2d7 2024-03-20 stsp {
1776 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1777 ba97b2d7 2024-03-20 stsp char *s, *p, *q;
1778 ba97b2d7 2024-03-20 stsp
1779 ba97b2d7 2024-03-20 stsp *proto = *host = *port = *request_path = NULL;
1780 ba97b2d7 2024-03-20 stsp
1781 ba97b2d7 2024-03-20 stsp p = strstr(url, "://");
1782 ba97b2d7 2024-03-20 stsp if (!p)
1783 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PARSE_URI);
1784 ba97b2d7 2024-03-20 stsp
1785 ba97b2d7 2024-03-20 stsp *proto = strndup(url, p - url);
1786 ba97b2d7 2024-03-20 stsp if (*proto == NULL) {
1787 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1788 ba97b2d7 2024-03-20 stsp goto done;
1789 ba97b2d7 2024-03-20 stsp }
1790 ba97b2d7 2024-03-20 stsp s = p + 3;
1791 ba97b2d7 2024-03-20 stsp
1792 ba97b2d7 2024-03-20 stsp p = strstr(s, "/");
1793 f9e65370 2024-03-27 op if (p == NULL) {
1794 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1795 ba97b2d7 2024-03-20 stsp goto done;
1796 ba97b2d7 2024-03-20 stsp }
1797 ba97b2d7 2024-03-20 stsp
1798 ba97b2d7 2024-03-20 stsp q = memchr(s, ':', p - s);
1799 ba97b2d7 2024-03-20 stsp if (q) {
1800 ba97b2d7 2024-03-20 stsp *host = strndup(s, q - s);
1801 ba97b2d7 2024-03-20 stsp if (*host == NULL) {
1802 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1803 ba97b2d7 2024-03-20 stsp goto done;
1804 ba97b2d7 2024-03-20 stsp }
1805 ba97b2d7 2024-03-20 stsp if ((*host)[0] == '\0') {
1806 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1807 ba97b2d7 2024-03-20 stsp goto done;
1808 ba97b2d7 2024-03-20 stsp }
1809 ba97b2d7 2024-03-20 stsp *port = strndup(q + 1, p - (q + 1));
1810 ba97b2d7 2024-03-20 stsp if (*port == NULL) {
1811 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1812 ba97b2d7 2024-03-20 stsp goto done;
1813 ba97b2d7 2024-03-20 stsp }
1814 ba97b2d7 2024-03-20 stsp if ((*port)[0] == '\0') {
1815 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1816 ba97b2d7 2024-03-20 stsp goto done;
1817 ba97b2d7 2024-03-20 stsp }
1818 ba97b2d7 2024-03-20 stsp } else {
1819 ba97b2d7 2024-03-20 stsp *host = strndup(s, p - s);
1820 ba97b2d7 2024-03-20 stsp if (*host == NULL) {
1821 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1822 ba97b2d7 2024-03-20 stsp goto done;
1823 ba97b2d7 2024-03-20 stsp }
1824 ba97b2d7 2024-03-20 stsp if ((*host)[0] == '\0') {
1825 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1826 ba97b2d7 2024-03-20 stsp goto done;
1827 ba97b2d7 2024-03-20 stsp }
1828 ba97b2d7 2024-03-20 stsp }
1829 ba97b2d7 2024-03-20 stsp
1830 ba97b2d7 2024-03-20 stsp while (p[0] == '/' && p[1] == '/')
1831 ba97b2d7 2024-03-20 stsp p++;
1832 ba97b2d7 2024-03-20 stsp *request_path = strdup(p);
1833 ba97b2d7 2024-03-20 stsp if (*request_path == NULL) {
1834 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1835 ba97b2d7 2024-03-20 stsp goto done;
1836 ba97b2d7 2024-03-20 stsp }
1837 ba97b2d7 2024-03-20 stsp if ((*request_path)[0] == '\0') {
1838 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1839 ba97b2d7 2024-03-20 stsp goto done;
1840 ba97b2d7 2024-03-20 stsp }
1841 ba97b2d7 2024-03-20 stsp done:
1842 ba97b2d7 2024-03-20 stsp if (err) {
1843 ba97b2d7 2024-03-20 stsp free(*proto);
1844 ba97b2d7 2024-03-20 stsp *proto = NULL;
1845 ba97b2d7 2024-03-20 stsp free(*host);
1846 ba97b2d7 2024-03-20 stsp *host = NULL;
1847 ba97b2d7 2024-03-20 stsp free(*port);
1848 ba97b2d7 2024-03-20 stsp *port = NULL;
1849 ba97b2d7 2024-03-20 stsp free(*request_path);
1850 ba97b2d7 2024-03-20 stsp *request_path = NULL;
1851 ba97b2d7 2024-03-20 stsp }
1852 ba97b2d7 2024-03-20 stsp return err;
1853 ba97b2d7 2024-03-20 stsp }
1854 ba97b2d7 2024-03-20 stsp
1855 ba97b2d7 2024-03-20 stsp static char *
1856 ba97b2d7 2024-03-20 stsp port_sprintf(int p)
1857 ba97b2d7 2024-03-20 stsp {
1858 ba97b2d7 2024-03-20 stsp static char portno[32];
1859 ba97b2d7 2024-03-20 stsp int n;
1860 ba97b2d7 2024-03-20 stsp
1861 ba97b2d7 2024-03-20 stsp n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1862 ba97b2d7 2024-03-20 stsp if (n < 0 || (size_t)n >= sizeof(portno))
1863 ba97b2d7 2024-03-20 stsp fatalx("port number too long: %lld", (long long)p);
1864 ba97b2d7 2024-03-20 stsp
1865 ba97b2d7 2024-03-20 stsp return portno;
1866 ba97b2d7 2024-03-20 stsp }