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 ba97b2d7 2024-03-20 stsp char *, char *, char *);
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 ba97b2d7 2024-03-20 stsp %token NOTIFY EMAIL FROM REPLY TO URL PASSWORD
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 46e48ac7 2023-01-03 stsp yyerror("invalid timeout: %lld", $3.tv_sec);
257 46e48ac7 2023-01-03 stsp YYERROR;
258 46e48ac7 2023-01-03 stsp }
259 40b85cca 2023-01-03 stsp memcpy(&gotd->request_timeout, &$3,
260 40b85cca 2023-01-03 stsp sizeof(gotd->request_timeout));
261 40b85cca 2023-01-03 stsp }
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) {
265 40b85cca 2023-01-03 stsp free($3);
266 40b85cca 2023-01-03 stsp YYERROR;
267 40b85cca 2023-01-03 stsp }
268 40b85cca 2023-01-03 stsp free($3);
269 40b85cca 2023-01-03 stsp }
270 40b85cca 2023-01-03 stsp ;
271 40b85cca 2023-01-03 stsp
272 9afa3de2 2023-04-04 stsp protect : PROTECT '{' optnl protectflags_l '}'
273 9afa3de2 2023-04-04 stsp | PROTECT protectflags
274 9afa3de2 2023-04-04 stsp
275 9afa3de2 2023-04-04 stsp protectflags_l : protectflags optnl protectflags_l
276 9afa3de2 2023-04-04 stsp | protectflags optnl
277 9afa3de2 2023-04-04 stsp ;
278 9afa3de2 2023-04-04 stsp
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)) {
283 9afa3de2 2023-04-04 stsp free($3);
284 9afa3de2 2023-04-04 stsp YYERROR;
285 9afa3de2 2023-04-04 stsp }
286 9afa3de2 2023-04-04 stsp }
287 584140c2 2023-04-05 op free($3);
288 9afa3de2 2023-04-04 stsp }
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,
293 9afa3de2 2023-04-04 stsp $3)) {
294 9afa3de2 2023-04-04 stsp free($3);
295 9afa3de2 2023-04-04 stsp YYERROR;
296 9afa3de2 2023-04-04 stsp }
297 9afa3de2 2023-04-04 stsp }
298 584140c2 2023-04-05 op free($3);
299 9afa3de2 2023-04-04 stsp }
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)) {
304 9afa3de2 2023-04-04 stsp free($2);
305 9afa3de2 2023-04-04 stsp YYERROR;
306 9afa3de2 2023-04-04 stsp }
307 9afa3de2 2023-04-04 stsp }
308 584140c2 2023-04-05 op free($2);
309 9afa3de2 2023-04-04 stsp }
310 9afa3de2 2023-04-04 stsp ;
311 9afa3de2 2023-04-04 stsp
312 ba97b2d7 2024-03-20 stsp notify : NOTIFY '{' optnl notifyflags_l '}'
313 ba97b2d7 2024-03-20 stsp | NOTIFY notifyflags
314 ba97b2d7 2024-03-20 stsp
315 ba97b2d7 2024-03-20 stsp notifyflags_l : notifyflags optnl notifyflags_l
316 ba97b2d7 2024-03-20 stsp | notifyflags optnl
317 ba97b2d7 2024-03-20 stsp ;
318 ba97b2d7 2024-03-20 stsp
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)) {
324 ba97b2d7 2024-03-20 stsp free($2);
325 ba97b2d7 2024-03-20 stsp YYERROR;
326 ba97b2d7 2024-03-20 stsp }
327 ba97b2d7 2024-03-20 stsp }
328 7d68253b 2024-03-20 op free($2);
329 ba97b2d7 2024-03-20 stsp }
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)) {
335 ba97b2d7 2024-03-20 stsp free($3);
336 ba97b2d7 2024-03-20 stsp YYERROR;
337 ba97b2d7 2024-03-20 stsp }
338 ba97b2d7 2024-03-20 stsp }
339 7d68253b 2024-03-20 op free($3);
340 ba97b2d7 2024-03-20 stsp }
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)) {
347 ba97b2d7 2024-03-20 stsp free($3);
348 ba97b2d7 2024-03-20 stsp YYERROR;
349 ba97b2d7 2024-03-20 stsp }
350 ba97b2d7 2024-03-20 stsp }
351 7d68253b 2024-03-20 op free($3);
352 ba97b2d7 2024-03-20 stsp }
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)) {
359 ba97b2d7 2024-03-20 stsp free($3);
360 ba97b2d7 2024-03-20 stsp free($5);
361 ba97b2d7 2024-03-20 stsp YYERROR;
362 ba97b2d7 2024-03-20 stsp }
363 7d68253b 2024-03-20 op }
364 7d68253b 2024-03-20 op free($3);
365 7d68253b 2024-03-20 op free($5);
366 ba97b2d7 2024-03-20 stsp }
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)) {
373 ba97b2d7 2024-03-20 stsp free($3);
374 ba97b2d7 2024-03-20 stsp free($6);
375 ba97b2d7 2024-03-20 stsp YYERROR;
376 ba97b2d7 2024-03-20 stsp }
377 7d68253b 2024-03-20 op }
378 7d68253b 2024-03-20 op free($3);
379 7d68253b 2024-03-20 op free($6);
380 ba97b2d7 2024-03-20 stsp }
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)) {
387 ba97b2d7 2024-03-20 stsp free($3);
388 ba97b2d7 2024-03-20 stsp free($5);
389 ba97b2d7 2024-03-20 stsp free($8);
390 ba97b2d7 2024-03-20 stsp YYERROR;
391 ba97b2d7 2024-03-20 stsp }
392 ba97b2d7 2024-03-20 stsp }
393 7d68253b 2024-03-20 op free($3);
394 7d68253b 2024-03-20 op free($5);
395 7d68253b 2024-03-20 op free($8);
396 ba97b2d7 2024-03-20 stsp }
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)) {
403 ba97b2d7 2024-03-20 stsp free($3);
404 ba97b2d7 2024-03-20 stsp free($5);
405 ba97b2d7 2024-03-20 stsp YYERROR;
406 ba97b2d7 2024-03-20 stsp }
407 ba97b2d7 2024-03-20 stsp }
408 7d68253b 2024-03-20 op free($3);
409 7d68253b 2024-03-20 op free($5);
410 ba97b2d7 2024-03-20 stsp }
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)) {
417 ba97b2d7 2024-03-20 stsp free($3);
418 ba97b2d7 2024-03-20 stsp free($5);
419 ba97b2d7 2024-03-20 stsp free($7);
420 ba97b2d7 2024-03-20 stsp YYERROR;
421 ba97b2d7 2024-03-20 stsp }
422 ba97b2d7 2024-03-20 stsp }
423 7d68253b 2024-03-20 op free($3);
424 7d68253b 2024-03-20 op free($5);
425 7d68253b 2024-03-20 op free($7);
426 ba97b2d7 2024-03-20 stsp }
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)) {
433 ba97b2d7 2024-03-20 stsp free($3);
434 ba97b2d7 2024-03-20 stsp free($6);
435 ba97b2d7 2024-03-20 stsp free($8);
436 ba97b2d7 2024-03-20 stsp YYERROR;
437 ba97b2d7 2024-03-20 stsp }
438 ba97b2d7 2024-03-20 stsp }
439 7d68253b 2024-03-20 op free($3);
440 7d68253b 2024-03-20 op free($6);
441 7d68253b 2024-03-20 op free($8);
442 ba97b2d7 2024-03-20 stsp }
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)) {
449 ba97b2d7 2024-03-20 stsp free($3);
450 ba97b2d7 2024-03-20 stsp free($5);
451 ba97b2d7 2024-03-20 stsp free($8);
452 ba97b2d7 2024-03-20 stsp free($10);
453 ba97b2d7 2024-03-20 stsp YYERROR;
454 ba97b2d7 2024-03-20 stsp }
455 ba97b2d7 2024-03-20 stsp }
456 7d68253b 2024-03-20 op free($3);
457 7d68253b 2024-03-20 op free($5);
458 7d68253b 2024-03-20 op free($8);
459 7d68253b 2024-03-20 op free($10);
460 ba97b2d7 2024-03-20 stsp }
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)) {
467 ba97b2d7 2024-03-20 stsp free($3);
468 ba97b2d7 2024-03-20 stsp free($5);
469 ba97b2d7 2024-03-20 stsp free($7);
470 ba97b2d7 2024-03-20 stsp YYERROR;
471 ba97b2d7 2024-03-20 stsp }
472 ba97b2d7 2024-03-20 stsp }
473 7d68253b 2024-03-20 op free($3);
474 7d68253b 2024-03-20 op free($5);
475 7d68253b 2024-03-20 op free($7);
476 ba97b2d7 2024-03-20 stsp }
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)) {
483 ba97b2d7 2024-03-20 stsp free($3);
484 ba97b2d7 2024-03-20 stsp free($5);
485 ba97b2d7 2024-03-20 stsp free($7);
486 ba97b2d7 2024-03-20 stsp free($9);
487 ba97b2d7 2024-03-20 stsp YYERROR;
488 ba97b2d7 2024-03-20 stsp }
489 ba97b2d7 2024-03-20 stsp }
490 7d68253b 2024-03-20 op free($3);
491 7d68253b 2024-03-20 op free($5);
492 7d68253b 2024-03-20 op free($7);
493 7d68253b 2024-03-20 op free($9);
494 ba97b2d7 2024-03-20 stsp }
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)) {
501 ba97b2d7 2024-03-20 stsp free($3);
502 ba97b2d7 2024-03-20 stsp free($6);
503 ba97b2d7 2024-03-20 stsp free($8);
504 ba97b2d7 2024-03-20 stsp free($10);
505 ba97b2d7 2024-03-20 stsp YYERROR;
506 ba97b2d7 2024-03-20 stsp }
507 7d68253b 2024-03-20 op }
508 7d68253b 2024-03-20 op free($3);
509 7d68253b 2024-03-20 op free($6);
510 7d68253b 2024-03-20 op free($8);
511 7d68253b 2024-03-20 op free($10);
512 ba97b2d7 2024-03-20 stsp }
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)) {
519 ba97b2d7 2024-03-20 stsp free($3);
520 ba97b2d7 2024-03-20 stsp free($5);
521 ba97b2d7 2024-03-20 stsp free($8);
522 ba97b2d7 2024-03-20 stsp free($10);
523 ba97b2d7 2024-03-20 stsp free($12);
524 ba97b2d7 2024-03-20 stsp YYERROR;
525 ba97b2d7 2024-03-20 stsp }
526 ba97b2d7 2024-03-20 stsp }
527 7d68253b 2024-03-20 op free($3);
528 7d68253b 2024-03-20 op free($5);
529 7d68253b 2024-03-20 op free($8);
530 7d68253b 2024-03-20 op free($10);
531 7d68253b 2024-03-20 op free($12);
532 ba97b2d7 2024-03-20 stsp }
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))) {
539 ba97b2d7 2024-03-20 stsp free($3);
540 ba97b2d7 2024-03-20 stsp free($5);
541 ba97b2d7 2024-03-20 stsp YYERROR;
542 ba97b2d7 2024-03-20 stsp }
543 ba97b2d7 2024-03-20 stsp }
544 7d68253b 2024-03-20 op free($3);
545 7d68253b 2024-03-20 op free($5);
546 ba97b2d7 2024-03-20 stsp }
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))) {
553 ba97b2d7 2024-03-20 stsp free($3);
554 ba97b2d7 2024-03-20 stsp free($5);
555 ba97b2d7 2024-03-20 stsp free($7);
556 ba97b2d7 2024-03-20 stsp YYERROR;
557 ba97b2d7 2024-03-20 stsp }
558 ba97b2d7 2024-03-20 stsp }
559 7d68253b 2024-03-20 op free($3);
560 7d68253b 2024-03-20 op free($5);
561 7d68253b 2024-03-20 op free($7);
562 ba97b2d7 2024-03-20 stsp }
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))) {
569 ba97b2d7 2024-03-20 stsp free($3);
570 ba97b2d7 2024-03-20 stsp free($6);
571 ba97b2d7 2024-03-20 stsp free($8);
572 ba97b2d7 2024-03-20 stsp YYERROR;
573 ba97b2d7 2024-03-20 stsp }
574 ba97b2d7 2024-03-20 stsp }
575 7d68253b 2024-03-20 op free($3);
576 7d68253b 2024-03-20 op free($6);
577 7d68253b 2024-03-20 op free($8);
578 ba97b2d7 2024-03-20 stsp }
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))) {
585 ba97b2d7 2024-03-20 stsp free($3);
586 ba97b2d7 2024-03-20 stsp free($5);
587 ba97b2d7 2024-03-20 stsp free($8);
588 ba97b2d7 2024-03-20 stsp free($10);
589 ba97b2d7 2024-03-20 stsp YYERROR;
590 ba97b2d7 2024-03-20 stsp }
591 ba97b2d7 2024-03-20 stsp }
592 7d68253b 2024-03-20 op free($3);
593 7d68253b 2024-03-20 op free($5);
594 7d68253b 2024-03-20 op free($8);
595 7d68253b 2024-03-20 op free($10);
596 ba97b2d7 2024-03-20 stsp }
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,
602 ba97b2d7 2024-03-20 stsp NULL)) {
603 ba97b2d7 2024-03-20 stsp free($2);
604 ba97b2d7 2024-03-20 stsp YYERROR;
605 ba97b2d7 2024-03-20 stsp }
606 ba97b2d7 2024-03-20 stsp }
607 7d68253b 2024-03-20 op free($2);
608 ba97b2d7 2024-03-20 stsp }
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)) {
614 ba97b2d7 2024-03-20 stsp free($2);
615 ba97b2d7 2024-03-20 stsp free($4);
616 ba97b2d7 2024-03-20 stsp free($6);
617 ba97b2d7 2024-03-20 stsp YYERROR;
618 ba97b2d7 2024-03-20 stsp }
619 ba97b2d7 2024-03-20 stsp }
620 7d68253b 2024-03-20 op free($2);
621 7d68253b 2024-03-20 op free($4);
622 7d68253b 2024-03-20 op free($6);
623 ba97b2d7 2024-03-20 stsp }
624 ba97b2d7 2024-03-20 stsp ;
625 ba97b2d7 2024-03-20 stsp
626 13b2bc37 2022-10-23 stsp repository : REPOSITORY STRING {
627 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
628 13b2bc37 2022-10-23 stsp
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);
632 13b2bc37 2022-10-23 stsp free($2);
633 13b2bc37 2022-10-23 stsp YYERROR;
634 13b2bc37 2022-10-23 stsp }
635 13b2bc37 2022-10-23 stsp }
636 13b2bc37 2022-10-23 stsp
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);
644 13b2bc37 2022-10-23 stsp }
645 13b2bc37 2022-10-23 stsp free($2);
646 13b2bc37 2022-10-23 stsp } '{' optnl repoopts2 '}' {
647 13b2bc37 2022-10-23 stsp }
648 13b2bc37 2022-10-23 stsp ;
649 13b2bc37 2022-10-23 stsp
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);
660 13b2bc37 2022-10-23 stsp free($2);
661 13b2bc37 2022-10-23 stsp YYERROR;
662 13b2bc37 2022-10-23 stsp }
663 9b7f22a6 2023-01-08 stsp if (realpath($2, new_repo->path) == NULL) {
664 859316d0 2023-04-12 stsp /*
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.
668 4b3827cd 2023-07-08 stsp *
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.
673 859316d0 2023-04-12 stsp */
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));
681 859316d0 2023-04-12 stsp free($2);
682 859316d0 2023-04-12 stsp YYERROR;
683 4b3827cd 2023-07-08 stsp }
684 4b3827cd 2023-07-08 stsp
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");
689 13b2bc37 2022-10-23 stsp }
690 13b2bc37 2022-10-23 stsp }
691 13b2bc37 2022-10-23 stsp free($2);
692 0ccf3acb 2022-11-16 stsp }
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);
697 584140c2 2023-04-05 op } else
698 584140c2 2023-04-05 op free($3);
699 0ccf3acb 2022-11-16 stsp }
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);
705 584140c2 2023-04-05 op } else
706 584140c2 2023-04-05 op free($3);
707 13b2bc37 2022-10-23 stsp }
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);
712 584140c2 2023-04-05 op } else
713 584140c2 2023-04-05 op free($2);
714 0ccf3acb 2022-11-16 stsp }
715 9afa3de2 2023-04-04 stsp | protect
716 ba97b2d7 2024-03-20 stsp | notify
717 13b2bc37 2022-10-23 stsp ;
718 13b2bc37 2022-10-23 stsp
719 13b2bc37 2022-10-23 stsp repoopts2 : repoopts2 repoopts1 nl
720 13b2bc37 2022-10-23 stsp | repoopts1 optnl
721 13b2bc37 2022-10-23 stsp ;
722 13b2bc37 2022-10-23 stsp
723 13b2bc37 2022-10-23 stsp nl : '\n' optnl
724 13b2bc37 2022-10-23 stsp ;
725 13b2bc37 2022-10-23 stsp
726 13b2bc37 2022-10-23 stsp optnl : '\n' optnl /* zero or more newlines */
727 13b2bc37 2022-10-23 stsp | /* empty */
728 13b2bc37 2022-10-23 stsp ;
729 13b2bc37 2022-10-23 stsp
730 13b2bc37 2022-10-23 stsp %%
731 13b2bc37 2022-10-23 stsp
732 13b2bc37 2022-10-23 stsp struct keywords {
733 13b2bc37 2022-10-23 stsp const char *k_name;
734 13b2bc37 2022-10-23 stsp int k_val;
735 13b2bc37 2022-10-23 stsp };
736 13b2bc37 2022-10-23 stsp
737 13b2bc37 2022-10-23 stsp int
738 13b2bc37 2022-10-23 stsp yyerror(const char *fmt, ...)
739 13b2bc37 2022-10-23 stsp {
740 13b2bc37 2022-10-23 stsp va_list ap;
741 13b2bc37 2022-10-23 stsp char *msg;
742 13b2bc37 2022-10-23 stsp
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);
749 13b2bc37 2022-10-23 stsp free(msg);
750 13b2bc37 2022-10-23 stsp return (0);
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 kw_cmp(const void *k, const void *e)
755 13b2bc37 2022-10-23 stsp {
756 13b2bc37 2022-10-23 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
757 13b2bc37 2022-10-23 stsp }
758 13b2bc37 2022-10-23 stsp
759 13b2bc37 2022-10-23 stsp int
760 13b2bc37 2022-10-23 stsp lookup(char *s)
761 13b2bc37 2022-10-23 stsp {
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 },
791 13b2bc37 2022-10-23 stsp };
792 13b2bc37 2022-10-23 stsp const struct keywords *p;
793 13b2bc37 2022-10-23 stsp
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);
796 13b2bc37 2022-10-23 stsp
797 13b2bc37 2022-10-23 stsp if (p)
798 13b2bc37 2022-10-23 stsp return (p->k_val);
799 13b2bc37 2022-10-23 stsp else
800 13b2bc37 2022-10-23 stsp return (STRING);
801 13b2bc37 2022-10-23 stsp }
802 13b2bc37 2022-10-23 stsp
803 13b2bc37 2022-10-23 stsp #define MAXPUSHBACK 128
804 13b2bc37 2022-10-23 stsp
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;
809 13b2bc37 2022-10-23 stsp
810 13b2bc37 2022-10-23 stsp int
811 13b2bc37 2022-10-23 stsp lgetc(int quotec)
812 13b2bc37 2022-10-23 stsp {
813 13b2bc37 2022-10-23 stsp int c, next;
814 13b2bc37 2022-10-23 stsp
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;
822 13b2bc37 2022-10-23 stsp } else
823 13b2bc37 2022-10-23 stsp parseindex++;
824 13b2bc37 2022-10-23 stsp }
825 13b2bc37 2022-10-23 stsp
826 13b2bc37 2022-10-23 stsp if (pushback_index)
827 13b2bc37 2022-10-23 stsp return (pushback_buffer[--pushback_index]);
828 13b2bc37 2022-10-23 stsp
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);
835 13b2bc37 2022-10-23 stsp }
836 13b2bc37 2022-10-23 stsp
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') {
841 13b2bc37 2022-10-23 stsp c = next;
842 13b2bc37 2022-10-23 stsp break;
843 13b2bc37 2022-10-23 stsp }
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);
847 13b2bc37 2022-10-23 stsp }
848 13b2bc37 2022-10-23 stsp
849 13b2bc37 2022-10-23 stsp return (c);
850 13b2bc37 2022-10-23 stsp }
851 13b2bc37 2022-10-23 stsp
852 13b2bc37 2022-10-23 stsp int
853 13b2bc37 2022-10-23 stsp lungetc(int c)
854 13b2bc37 2022-10-23 stsp {
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);
861 13b2bc37 2022-10-23 stsp }
862 13b2bc37 2022-10-23 stsp if (pushback_index < MAXPUSHBACK-1)
863 13b2bc37 2022-10-23 stsp return (pushback_buffer[pushback_index++] = c);
864 13b2bc37 2022-10-23 stsp else
865 13b2bc37 2022-10-23 stsp return (EOF);
866 13b2bc37 2022-10-23 stsp }
867 13b2bc37 2022-10-23 stsp
868 13b2bc37 2022-10-23 stsp int
869 13b2bc37 2022-10-23 stsp findeol(void)
870 13b2bc37 2022-10-23 stsp {
871 13b2bc37 2022-10-23 stsp int c;
872 13b2bc37 2022-10-23 stsp
873 13b2bc37 2022-10-23 stsp parsebuf = NULL;
874 13b2bc37 2022-10-23 stsp
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];
879 13b2bc37 2022-10-23 stsp else
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++;
883 13b2bc37 2022-10-23 stsp break;
884 13b2bc37 2022-10-23 stsp }
885 13b2bc37 2022-10-23 stsp if (c == EOF)
886 13b2bc37 2022-10-23 stsp break;
887 13b2bc37 2022-10-23 stsp }
888 13b2bc37 2022-10-23 stsp return (ERROR);
889 13b2bc37 2022-10-23 stsp }
890 13b2bc37 2022-10-23 stsp
891 13b2bc37 2022-10-23 stsp int
892 13b2bc37 2022-10-23 stsp yylex(void)
893 13b2bc37 2022-10-23 stsp {
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;
897 13b2bc37 2022-10-23 stsp int token;
898 13b2bc37 2022-10-23 stsp
899 13b2bc37 2022-10-23 stsp top:
900 13b2bc37 2022-10-23 stsp p = buf;
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 */
904 13b2bc37 2022-10-23 stsp
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 */
910 13b2bc37 2022-10-23 stsp }
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);
916 13b2bc37 2022-10-23 stsp
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());
920 13b2bc37 2022-10-23 stsp }
921 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == '_') {
922 13b2bc37 2022-10-23 stsp *p++ = c;
923 13b2bc37 2022-10-23 stsp continue;
924 13b2bc37 2022-10-23 stsp }
925 13b2bc37 2022-10-23 stsp *p = '\0';
926 13b2bc37 2022-10-23 stsp lungetc(c);
927 13b2bc37 2022-10-23 stsp break;
928 13b2bc37 2022-10-23 stsp }
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());
933 13b2bc37 2022-10-23 stsp }
934 13b2bc37 2022-10-23 stsp parsebuf = val;
935 13b2bc37 2022-10-23 stsp parseindex = 0;
936 13b2bc37 2022-10-23 stsp goto top;
937 13b2bc37 2022-10-23 stsp }
938 13b2bc37 2022-10-23 stsp
939 13b2bc37 2022-10-23 stsp switch (c) {
940 13b2bc37 2022-10-23 stsp case '\'':
941 13b2bc37 2022-10-23 stsp case '"':
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++;
949 13b2bc37 2022-10-23 stsp continue;
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')
955 13b2bc37 2022-10-23 stsp c = next;
956 13b2bc37 2022-10-23 stsp else if (next == '\n') {
957 13b2bc37 2022-10-23 stsp file->lineno++;
958 13b2bc37 2022-10-23 stsp continue;
959 13b2bc37 2022-10-23 stsp } else
960 13b2bc37 2022-10-23 stsp lungetc(next);
961 13b2bc37 2022-10-23 stsp } else if (c == quotec) {
962 13b2bc37 2022-10-23 stsp *p = '\0';
963 13b2bc37 2022-10-23 stsp break;
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());
967 13b2bc37 2022-10-23 stsp }
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());
971 13b2bc37 2022-10-23 stsp }
972 13b2bc37 2022-10-23 stsp *p++ = c;
973 13b2bc37 2022-10-23 stsp }
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);
978 13b2bc37 2022-10-23 stsp }
979 13b2bc37 2022-10-23 stsp
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 == '=')
982 13b2bc37 2022-10-23 stsp
983 13b2bc37 2022-10-23 stsp if (c == '-' || isdigit(c)) {
984 13b2bc37 2022-10-23 stsp do {
985 13b2bc37 2022-10-23 stsp *p++ = 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());
989 13b2bc37 2022-10-23 stsp }
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;
997 13b2bc37 2022-10-23 stsp
998 13b2bc37 2022-10-23 stsp *p = '\0';
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());
1005 13b2bc37 2022-10-23 stsp }
1006 13b2bc37 2022-10-23 stsp return (NUMBER);
1007 13b2bc37 2022-10-23 stsp } else {
1008 13b2bc37 2022-10-23 stsp nodigits:
1009 13b2bc37 2022-10-23 stsp while (p > buf + 1)
1010 13b2bc37 2022-10-23 stsp lungetc(*--p);
1011 13b2bc37 2022-10-23 stsp c = *--p;
1012 13b2bc37 2022-10-23 stsp if (c == '-')
1013 13b2bc37 2022-10-23 stsp return (c);
1014 13b2bc37 2022-10-23 stsp }
1015 13b2bc37 2022-10-23 stsp }
1016 13b2bc37 2022-10-23 stsp
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 != ','))
1022 13b2bc37 2022-10-23 stsp
1023 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == ':' || c == '_') {
1024 13b2bc37 2022-10-23 stsp do {
1025 13b2bc37 2022-10-23 stsp *p++ = 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());
1029 13b2bc37 2022-10-23 stsp }
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");
1039 13b2bc37 2022-10-23 stsp }
1040 13b2bc37 2022-10-23 stsp return (token);
1041 13b2bc37 2022-10-23 stsp }
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++;
1045 13b2bc37 2022-10-23 stsp }
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);
1049 13b2bc37 2022-10-23 stsp }
1050 13b2bc37 2022-10-23 stsp
1051 13b2bc37 2022-10-23 stsp int
1052 13b2bc37 2022-10-23 stsp check_file_secrecy(int fd, const char *fname)
1053 13b2bc37 2022-10-23 stsp {
1054 13b2bc37 2022-10-23 stsp struct stat st;
1055 13b2bc37 2022-10-23 stsp
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);
1059 13b2bc37 2022-10-23 stsp }
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);
1063 13b2bc37 2022-10-23 stsp }
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);
1067 13b2bc37 2022-10-23 stsp }
1068 13b2bc37 2022-10-23 stsp return (0);
1069 13b2bc37 2022-10-23 stsp }
1070 13b2bc37 2022-10-23 stsp
1071 13b2bc37 2022-10-23 stsp struct file *
1072 e9e0377f 2023-03-29 stsp newfile(const char *name, int secret, int required)
1073 13b2bc37 2022-10-23 stsp {
1074 13b2bc37 2022-10-23 stsp struct file *nfile;
1075 13b2bc37 2022-10-23 stsp
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);
1080 13b2bc37 2022-10-23 stsp }
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);
1086 13b2bc37 2022-10-23 stsp }
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);
1100 13b2bc37 2022-10-23 stsp }
1101 13b2bc37 2022-10-23 stsp nfile->lineno = 1;
1102 13b2bc37 2022-10-23 stsp return (nfile);
1103 13b2bc37 2022-10-23 stsp }
1104 13b2bc37 2022-10-23 stsp
1105 13b2bc37 2022-10-23 stsp static void
1106 13b2bc37 2022-10-23 stsp closefile(struct file *xfile)
1107 13b2bc37 2022-10-23 stsp {
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);
1111 13b2bc37 2022-10-23 stsp }
1112 13b2bc37 2022-10-23 stsp
1113 13b2bc37 2022-10-23 stsp int
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)
1116 13b2bc37 2022-10-23 stsp {
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);
1120 13b2bc37 2022-10-23 stsp
1121 13b2bc37 2022-10-23 stsp memset(env, 0, sizeof(*env));
1122 13b2bc37 2022-10-23 stsp
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);
1126 13b2bc37 2022-10-23 stsp
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;
1132 13b2bc37 2022-10-23 stsp }
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;
1137 13b2bc37 2022-10-23 stsp }
1138 40b85cca 2023-01-03 stsp
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;
1141 13b2bc37 2022-10-23 stsp
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;
1145 13b2bc37 2022-10-23 stsp
1146 13b2bc37 2022-10-23 stsp yyparse();
1147 13b2bc37 2022-10-23 stsp errors = file->errors;
1148 13b2bc37 2022-10-23 stsp closefile(file);
1149 13b2bc37 2022-10-23 stsp
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);
1160 13b2bc37 2022-10-23 stsp }
1161 13b2bc37 2022-10-23 stsp }
1162 13b2bc37 2022-10-23 stsp
1163 13b2bc37 2022-10-23 stsp if (errors)
1164 13b2bc37 2022-10-23 stsp return (-1);
1165 13b2bc37 2022-10-23 stsp
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);
1171 3b706203 2023-01-02 stsp }
1172 3b706203 2023-01-02 stsp }
1173 3b706203 2023-01-02 stsp
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);
1177 809a54db 2023-01-18 op }
1178 809a54db 2023-01-18 op
1179 13b2bc37 2022-10-23 stsp return (0);
1180 13b2bc37 2022-10-23 stsp }
1181 13b2bc37 2022-10-23 stsp
1182 40b85cca 2023-01-03 stsp static int
1183 40b85cca 2023-01-03 stsp uid_connection_limit_cmp(const void *pa, const void *pb)
1184 40b85cca 2023-01-03 stsp {
1185 40b85cca 2023-01-03 stsp const struct gotd_uid_connection_limit *a = pa, *b = pb;
1186 40b85cca 2023-01-03 stsp
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);
1190 40b85cca 2023-01-03 stsp return 1;
1191 40b85cca 2023-01-03 stsp
1192 40b85cca 2023-01-03 stsp return 0;
1193 40b85cca 2023-01-03 stsp }
1194 40b85cca 2023-01-03 stsp
1195 40b85cca 2023-01-03 stsp static int
1196 40b85cca 2023-01-03 stsp conf_limit_user_connections(const char *user, int maximum)
1197 40b85cca 2023-01-03 stsp {
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;
1201 40b85cca 2023-01-03 stsp
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;
1205 40b85cca 2023-01-03 stsp }
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;
1209 40b85cca 2023-01-03 stsp }
1210 40b85cca 2023-01-03 stsp
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;
1214 40b85cca 2023-01-03 stsp }
1215 40b85cca 2023-01-03 stsp
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;
1220 40b85cca 2023-01-03 stsp return 0;
1221 40b85cca 2023-01-03 stsp }
1222 40b85cca 2023-01-03 stsp
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");
1228 40b85cca 2023-01-03 stsp
1229 40b85cca 2023-01-03 stsp limit[nlimits - 1].uid = uid;
1230 40b85cca 2023-01-03 stsp limit[nlimits - 1].max_connections = maximum;
1231 40b85cca 2023-01-03 stsp
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);
1236 40b85cca 2023-01-03 stsp
1237 40b85cca 2023-01-03 stsp return 0;
1238 40b85cca 2023-01-03 stsp }
1239 40b85cca 2023-01-03 stsp
1240 13b2bc37 2022-10-23 stsp static struct gotd_repo *
1241 13b2bc37 2022-10-23 stsp conf_new_repo(const char *name)
1242 13b2bc37 2022-10-23 stsp {
1243 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
1244 13b2bc37 2022-10-23 stsp
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);
1248 7683f79a 2023-01-02 stsp }
1249 7683f79a 2023-01-02 stsp
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);
1252 13b2bc37 2022-10-23 stsp
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__);
1256 13b2bc37 2022-10-23 stsp
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);
1265 0ccf3acb 2022-11-16 stsp
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__);
1269 13b2bc37 2022-10-23 stsp
1270 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
1271 13b2bc37 2022-10-23 stsp gotd->nrepos++;
1272 13b2bc37 2022-10-23 stsp
1273 13b2bc37 2022-10-23 stsp return repo;
1274 13b2bc37 2022-10-23 stsp };
1275 0ccf3acb 2022-11-16 stsp
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)
1279 0ccf3acb 2022-11-16 stsp {
1280 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
1281 0ccf3acb 2022-11-16 stsp
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");
1285 13b2bc37 2022-10-23 stsp
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;
1289 0ccf3acb 2022-11-16 stsp
1290 0ccf3acb 2022-11-16 stsp STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
1291 0ccf3acb 2022-11-16 stsp }
1292 0ccf3acb 2022-11-16 stsp
1293 9afa3de2 2023-04-04 stsp static int
1294 9afa3de2 2023-04-04 stsp refname_is_valid(char *refname)
1295 9afa3de2 2023-04-04 stsp {
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",
1298 9afa3de2 2023-04-04 stsp refname);
1299 9afa3de2 2023-04-04 stsp return 0;
1300 9afa3de2 2023-04-04 stsp }
1301 9afa3de2 2023-04-04 stsp
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);
1304 9afa3de2 2023-04-04 stsp return 0;
1305 9afa3de2 2023-04-04 stsp }
1306 9afa3de2 2023-04-04 stsp
1307 9afa3de2 2023-04-04 stsp return 1;
1308 9afa3de2 2023-04-04 stsp }
1309 9afa3de2 2023-04-04 stsp
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)
1313 9afa3de2 2023-04-04 stsp {
1314 9afa3de2 2023-04-04 stsp const struct got_error *error;
1315 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1316 9afa3de2 2023-04-04 stsp char *s;
1317 9afa3de2 2023-04-04 stsp
1318 f850236e 2023-04-05 stsp *new = NULL;
1319 f850236e 2023-04-05 stsp
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;
1326 9afa3de2 2023-04-04 stsp }
1327 9afa3de2 2023-04-04 stsp
1328 f850236e 2023-04-05 stsp error = got_pathlist_insert(&pe, refs, s, NULL);
1329 f850236e 2023-04-05 stsp if (error || pe == NULL) {
1330 f0426190 2023-04-05 op free(s);
1331 f0426190 2023-04-05 op if (error)
1332 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1333 f0426190 2023-04-05 op else
1334 6be067ce 2023-04-06 stsp yyerror("duplicate protected namespace %s", namespace);
1335 9afa3de2 2023-04-04 stsp return -1;
1336 9afa3de2 2023-04-04 stsp }
1337 9afa3de2 2023-04-04 stsp
1338 f850236e 2023-04-05 stsp *new = s;
1339 9afa3de2 2023-04-04 stsp return 0;
1340 9afa3de2 2023-04-04 stsp }
1341 9afa3de2 2023-04-04 stsp
1342 9afa3de2 2023-04-04 stsp static int
1343 9afa3de2 2023-04-04 stsp conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
1344 9afa3de2 2023-04-04 stsp {
1345 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1346 f850236e 2023-04-05 stsp char *new;
1347 f850236e 2023-04-05 stsp
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;
1351 f850236e 2023-04-05 stsp
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;
1356 f850236e 2023-04-05 stsp }
1357 f850236e 2023-04-05 stsp }
1358 f850236e 2023-04-05 stsp
1359 f850236e 2023-04-05 stsp return 0;
1360 9afa3de2 2023-04-04 stsp }
1361 9afa3de2 2023-04-04 stsp
1362 9afa3de2 2023-04-04 stsp static int
1363 9afa3de2 2023-04-04 stsp conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
1364 9afa3de2 2023-04-04 stsp {
1365 f850236e 2023-04-05 stsp struct got_pathlist_entry *pe;
1366 f850236e 2023-04-05 stsp char *new;
1367 f850236e 2023-04-05 stsp
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;
1371 f850236e 2023-04-05 stsp
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;
1376 f850236e 2023-04-05 stsp }
1377 f850236e 2023-04-05 stsp }
1378 f850236e 2023-04-05 stsp
1379 f850236e 2023-04-05 stsp return 0;
1380 9afa3de2 2023-04-04 stsp }
1381 9afa3de2 2023-04-04 stsp
1382 9afa3de2 2023-04-04 stsp static int
1383 9afa3de2 2023-04-04 stsp conf_protect_branch(struct gotd_repo *repo, char *branchname)
1384 9afa3de2 2023-04-04 stsp {
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;
1388 9afa3de2 2023-04-04 stsp
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;
1393 9afa3de2 2023-04-04 stsp }
1394 9afa3de2 2023-04-04 stsp } else {
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;
1399 9afa3de2 2023-04-04 stsp }
1400 9afa3de2 2023-04-04 stsp }
1401 9afa3de2 2023-04-04 stsp
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;
1405 9afa3de2 2023-04-04 stsp }
1406 9afa3de2 2023-04-04 stsp
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);
1411 f0426190 2023-04-05 op if (error)
1412 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
1413 f0426190 2023-04-05 op else
1414 f0426190 2023-04-05 op yyerror("duplicate protect branch %s", branchname);
1415 ba97b2d7 2024-03-20 stsp return -1;
1416 ba97b2d7 2024-03-20 stsp }
1417 ba97b2d7 2024-03-20 stsp
1418 ba97b2d7 2024-03-20 stsp return 0;
1419 ba97b2d7 2024-03-20 stsp }
1420 ba97b2d7 2024-03-20 stsp
1421 ba97b2d7 2024-03-20 stsp static int
1422 ba97b2d7 2024-03-20 stsp conf_notify_branch(struct gotd_repo *repo, char *branchname)
1423 ba97b2d7 2024-03-20 stsp {
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;
1427 ba97b2d7 2024-03-20 stsp
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;
1432 ba97b2d7 2024-03-20 stsp }
1433 ba97b2d7 2024-03-20 stsp } else {
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;
1438 ba97b2d7 2024-03-20 stsp }
1439 ba97b2d7 2024-03-20 stsp }
1440 ba97b2d7 2024-03-20 stsp
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;
1444 9afa3de2 2023-04-04 stsp }
1445 9afa3de2 2023-04-04 stsp
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;
1452 ba97b2d7 2024-03-20 stsp }
1453 ba97b2d7 2024-03-20 stsp if (pe == NULL)
1454 ba97b2d7 2024-03-20 stsp free(refname);
1455 ba97b2d7 2024-03-20 stsp
1456 9afa3de2 2023-04-04 stsp return 0;
1457 9afa3de2 2023-04-04 stsp }
1458 9afa3de2 2023-04-04 stsp
1459 ba97b2d7 2024-03-20 stsp static int
1460 ba97b2d7 2024-03-20 stsp conf_notify_ref_namespace(struct gotd_repo *repo, char *namespace)
1461 ba97b2d7 2024-03-20 stsp {
1462 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1463 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1464 ba97b2d7 2024-03-20 stsp char *s;
1465 ba97b2d7 2024-03-20 stsp
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;
1469 ba97b2d7 2024-03-20 stsp
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;
1473 ba97b2d7 2024-03-20 stsp }
1474 ba97b2d7 2024-03-20 stsp
1475 ba97b2d7 2024-03-20 stsp error = got_pathlist_insert(&pe, &repo->notification_ref_namespaces,
1476 ba97b2d7 2024-03-20 stsp s, NULL);
1477 ba97b2d7 2024-03-20 stsp if (error) {
1478 ba97b2d7 2024-03-20 stsp free(s);
1479 ba97b2d7 2024-03-20 stsp yyerror("got_pathlist_insert: %s", error->msg);
1480 ba97b2d7 2024-03-20 stsp return -1;
1481 ba97b2d7 2024-03-20 stsp }
1482 ba97b2d7 2024-03-20 stsp if (pe == NULL)
1483 ba97b2d7 2024-03-20 stsp free(s);
1484 ba97b2d7 2024-03-20 stsp
1485 ba97b2d7 2024-03-20 stsp return 0;
1486 ba97b2d7 2024-03-20 stsp }
1487 ba97b2d7 2024-03-20 stsp
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)
1491 ba97b2d7 2024-03-20 stsp {
1492 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
1493 ba97b2d7 2024-03-20 stsp
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)
1496 ba97b2d7 2024-03-20 stsp continue;
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;
1501 ba97b2d7 2024-03-20 stsp }
1502 ba97b2d7 2024-03-20 stsp }
1503 ba97b2d7 2024-03-20 stsp
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");
1512 ba97b2d7 2024-03-20 stsp }
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");
1520 ba97b2d7 2024-03-20 stsp }
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");
1525 ba97b2d7 2024-03-20 stsp }
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");
1530 ba97b2d7 2024-03-20 stsp }
1531 ba97b2d7 2024-03-20 stsp
1532 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1533 ba97b2d7 2024-03-20 stsp return 0;
1534 ba97b2d7 2024-03-20 stsp }
1535 ba97b2d7 2024-03-20 stsp
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)
1538 ba97b2d7 2024-03-20 stsp {
1539 ba97b2d7 2024-03-20 stsp const struct got_error *error;
1540 ba97b2d7 2024-03-20 stsp struct gotd_notification_target *target;
1541 5565365c 2024-03-27 op char *proto, *hostname, *port, *path;
1542 5565365c 2024-03-27 op int tls = 0, ret = 0;
1543 ba97b2d7 2024-03-20 stsp
1544 5565365c 2024-03-27 op error = gotd_parse_url(&proto, &hostname, &port, &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;
1549 ba97b2d7 2024-03-20 stsp }
1550 ba97b2d7 2024-03-20 stsp
1551 5565365c 2024-03-27 op tls = !strcmp(proto, "https");
1552 5565365c 2024-03-27 op
1553 ba97b2d7 2024-03-20 stsp if (strcmp(proto, "http") != 0 && strcmp(proto, "https") != 0) {
1554 ba97b2d7 2024-03-20 stsp yyerror("invalid protocol '%s' in notification URL '%s' in "
1555 ba97b2d7 2024-03-20 stsp "repository '%s", proto, url, repo->name);
1556 5565365c 2024-03-27 op ret = -1;
1557 5565365c 2024-03-27 op goto done;
1558 5565365c 2024-03-27 op }
1559 5565365c 2024-03-27 op
1560 5565365c 2024-03-27 op if ((user != NULL && password == NULL) ||
1561 5565365c 2024-03-27 op (user == NULL && password != NULL)) {
1562 5565365c 2024-03-27 op yyerror("missing username or password");
1563 ba97b2d7 2024-03-20 stsp ret = -1;
1564 ba97b2d7 2024-03-20 stsp goto done;
1565 ba97b2d7 2024-03-20 stsp }
1566 ba97b2d7 2024-03-20 stsp
1567 ba97b2d7 2024-03-20 stsp if (strcmp(proto, "http") == 0 && (user != NULL || password != NULL)) {
1568 ba97b2d7 2024-03-20 stsp log_warnx("%s: WARNING: Using basic authentication over "
1569 ba97b2d7 2024-03-20 stsp "plaintext http:// will leak credentials; https:// is "
1570 ba97b2d7 2024-03-20 stsp "recommended for URL '%s'", getprogname(), url);
1571 ba97b2d7 2024-03-20 stsp }
1572 ba97b2d7 2024-03-20 stsp
1573 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(target, &repo->notification_targets, entry) {
1574 ba97b2d7 2024-03-20 stsp if (target->type != GOTD_NOTIFICATION_VIA_HTTP)
1575 ba97b2d7 2024-03-20 stsp continue;
1576 5565365c 2024-03-27 op if (target->conf.http.tls == tls &&
1577 5565365c 2024-03-27 op !strcmp(target->conf.http.hostname, hostname) &&
1578 5565365c 2024-03-27 op !strcmp(target->conf.http.port, port) &&
1579 5565365c 2024-03-27 op !strcmp(target->conf.http.path, path)) {
1580 ba97b2d7 2024-03-20 stsp yyerror("duplicate notification for URL '%s' in "
1581 ba97b2d7 2024-03-20 stsp "repository '%s'", url, repo->name);
1582 ba97b2d7 2024-03-20 stsp ret = -1;
1583 ba97b2d7 2024-03-20 stsp goto done;
1584 ba97b2d7 2024-03-20 stsp }
1585 ba97b2d7 2024-03-20 stsp }
1586 ba97b2d7 2024-03-20 stsp
1587 ba97b2d7 2024-03-20 stsp target = calloc(1, sizeof(*target));
1588 ba97b2d7 2024-03-20 stsp if (target == NULL)
1589 ba97b2d7 2024-03-20 stsp fatal("calloc");
1590 ba97b2d7 2024-03-20 stsp target->type = GOTD_NOTIFICATION_VIA_HTTP;
1591 5565365c 2024-03-27 op target->conf.http.tls = tls;
1592 5565365c 2024-03-27 op target->conf.http.hostname = hostname;
1593 5565365c 2024-03-27 op target->conf.http.port = port;
1594 5565365c 2024-03-27 op target->conf.http.path = path;
1595 5565365c 2024-03-27 op hostname = port = path = NULL;
1596 5565365c 2024-03-27 op
1597 ba97b2d7 2024-03-20 stsp if (user) {
1598 ba97b2d7 2024-03-20 stsp target->conf.http.user = strdup(user);
1599 ba97b2d7 2024-03-20 stsp if (target->conf.http.user == NULL)
1600 5565365c 2024-03-27 op fatal("strdup");
1601 ba97b2d7 2024-03-20 stsp target->conf.http.password = strdup(password);
1602 ba97b2d7 2024-03-20 stsp if (target->conf.http.password == NULL)
1603 5565365c 2024-03-27 op fatal("strdup");
1604 ba97b2d7 2024-03-20 stsp }
1605 ba97b2d7 2024-03-20 stsp
1606 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_TAIL(&repo->notification_targets, target, entry);
1607 ba97b2d7 2024-03-20 stsp done:
1608 ba97b2d7 2024-03-20 stsp free(proto);
1609 5565365c 2024-03-27 op free(hostname);
1610 ba97b2d7 2024-03-20 stsp free(port);
1611 5565365c 2024-03-27 op free(path);
1612 ba97b2d7 2024-03-20 stsp return ret;
1613 ba97b2d7 2024-03-20 stsp }
1614 ba97b2d7 2024-03-20 stsp
1615 13b2bc37 2022-10-23 stsp int
1616 13b2bc37 2022-10-23 stsp symset(const char *nam, const char *val, int persist)
1617 13b2bc37 2022-10-23 stsp {
1618 13b2bc37 2022-10-23 stsp struct sym *sym;
1619 13b2bc37 2022-10-23 stsp
1620 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1621 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0)
1622 13b2bc37 2022-10-23 stsp break;
1623 13b2bc37 2022-10-23 stsp }
1624 13b2bc37 2022-10-23 stsp
1625 13b2bc37 2022-10-23 stsp if (sym != NULL) {
1626 13b2bc37 2022-10-23 stsp if (sym->persist == 1)
1627 13b2bc37 2022-10-23 stsp return (0);
1628 13b2bc37 2022-10-23 stsp else {
1629 13b2bc37 2022-10-23 stsp free(sym->nam);
1630 13b2bc37 2022-10-23 stsp free(sym->val);
1631 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1632 13b2bc37 2022-10-23 stsp free(sym);
1633 13b2bc37 2022-10-23 stsp }
1634 13b2bc37 2022-10-23 stsp }
1635 13b2bc37 2022-10-23 stsp sym = calloc(1, sizeof(*sym));
1636 13b2bc37 2022-10-23 stsp if (sym == NULL)
1637 13b2bc37 2022-10-23 stsp return (-1);
1638 13b2bc37 2022-10-23 stsp
1639 13b2bc37 2022-10-23 stsp sym->nam = strdup(nam);
1640 13b2bc37 2022-10-23 stsp if (sym->nam == NULL) {
1641 13b2bc37 2022-10-23 stsp free(sym);
1642 13b2bc37 2022-10-23 stsp return (-1);
1643 13b2bc37 2022-10-23 stsp }
1644 13b2bc37 2022-10-23 stsp sym->val = strdup(val);
1645 13b2bc37 2022-10-23 stsp if (sym->val == NULL) {
1646 13b2bc37 2022-10-23 stsp free(sym->nam);
1647 13b2bc37 2022-10-23 stsp free(sym);
1648 13b2bc37 2022-10-23 stsp return (-1);
1649 13b2bc37 2022-10-23 stsp }
1650 13b2bc37 2022-10-23 stsp sym->used = 0;
1651 13b2bc37 2022-10-23 stsp sym->persist = persist;
1652 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
1653 13b2bc37 2022-10-23 stsp return (0);
1654 13b2bc37 2022-10-23 stsp }
1655 13b2bc37 2022-10-23 stsp
1656 13b2bc37 2022-10-23 stsp char *
1657 13b2bc37 2022-10-23 stsp symget(const char *nam)
1658 13b2bc37 2022-10-23 stsp {
1659 13b2bc37 2022-10-23 stsp struct sym *sym;
1660 13b2bc37 2022-10-23 stsp
1661 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1662 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0) {
1663 13b2bc37 2022-10-23 stsp sym->used = 1;
1664 13b2bc37 2022-10-23 stsp return (sym->val);
1665 13b2bc37 2022-10-23 stsp }
1666 13b2bc37 2022-10-23 stsp }
1667 13b2bc37 2022-10-23 stsp return (NULL);
1668 b09c1279 2023-03-28 stsp }
1669 b09c1279 2023-03-28 stsp
1670 b09c1279 2023-03-28 stsp struct gotd_repo *
1671 ba97b2d7 2024-03-20 stsp gotd_find_repo_by_name(const char *repo_name, struct gotd_repolist *repos)
1672 b09c1279 2023-03-28 stsp {
1673 b09c1279 2023-03-28 stsp struct gotd_repo *repo;
1674 b09c1279 2023-03-28 stsp size_t namelen;
1675 b09c1279 2023-03-28 stsp
1676 ba97b2d7 2024-03-20 stsp TAILQ_FOREACH(repo, repos, entry) {
1677 b09c1279 2023-03-28 stsp namelen = strlen(repo->name);
1678 b09c1279 2023-03-28 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
1679 b09c1279 2023-03-28 stsp continue;
1680 b09c1279 2023-03-28 stsp if (repo_name[namelen] == '\0' ||
1681 b09c1279 2023-03-28 stsp strcmp(&repo_name[namelen], ".git") == 0)
1682 b09c1279 2023-03-28 stsp return repo;
1683 b09c1279 2023-03-28 stsp }
1684 b09c1279 2023-03-28 stsp
1685 b09c1279 2023-03-28 stsp return NULL;
1686 13b2bc37 2022-10-23 stsp }
1687 9afa3de2 2023-04-04 stsp
1688 9afa3de2 2023-04-04 stsp struct gotd_repo *
1689 9afa3de2 2023-04-04 stsp gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1690 9afa3de2 2023-04-04 stsp {
1691 9afa3de2 2023-04-04 stsp struct gotd_repo *repo;
1692 9afa3de2 2023-04-04 stsp
1693 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1694 9afa3de2 2023-04-04 stsp if (strcmp(repo->path, repo_path) == 0)
1695 9afa3de2 2023-04-04 stsp return repo;
1696 eeb616b7 2023-04-14 stsp }
1697 eeb616b7 2023-04-14 stsp
1698 eeb616b7 2023-04-14 stsp return NULL;
1699 eeb616b7 2023-04-14 stsp }
1700 eeb616b7 2023-04-14 stsp
1701 eeb616b7 2023-04-14 stsp struct gotd_uid_connection_limit *
1702 eeb616b7 2023-04-14 stsp gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
1703 eeb616b7 2023-04-14 stsp size_t nlimits, uid_t uid)
1704 eeb616b7 2023-04-14 stsp {
1705 eeb616b7 2023-04-14 stsp /* This array is always sorted to allow for binary search. */
1706 eeb616b7 2023-04-14 stsp int i, left = 0, right = nlimits - 1;
1707 eeb616b7 2023-04-14 stsp
1708 eeb616b7 2023-04-14 stsp while (left <= right) {
1709 eeb616b7 2023-04-14 stsp i = ((left + right) / 2);
1710 eeb616b7 2023-04-14 stsp if (limits[i].uid == uid)
1711 eeb616b7 2023-04-14 stsp return &limits[i];
1712 eeb616b7 2023-04-14 stsp if (limits[i].uid > uid)
1713 eeb616b7 2023-04-14 stsp left = i + 1;
1714 eeb616b7 2023-04-14 stsp else
1715 eeb616b7 2023-04-14 stsp right = i - 1;
1716 9afa3de2 2023-04-04 stsp }
1717 9afa3de2 2023-04-04 stsp
1718 9afa3de2 2023-04-04 stsp return NULL;
1719 9afa3de2 2023-04-04 stsp }
1720 1963be61 2023-04-14 stsp
1721 1963be61 2023-04-14 stsp int
1722 1963be61 2023-04-14 stsp gotd_parseuid(const char *s, uid_t *uid)
1723 1963be61 2023-04-14 stsp {
1724 1963be61 2023-04-14 stsp struct passwd *pw;
1725 1963be61 2023-04-14 stsp const char *errstr;
1726 1963be61 2023-04-14 stsp
1727 1963be61 2023-04-14 stsp if ((pw = getpwnam(s)) != NULL) {
1728 1963be61 2023-04-14 stsp *uid = pw->pw_uid;
1729 1963be61 2023-04-14 stsp if (*uid == UID_MAX)
1730 1963be61 2023-04-14 stsp return -1;
1731 1963be61 2023-04-14 stsp return 0;
1732 1963be61 2023-04-14 stsp }
1733 1963be61 2023-04-14 stsp *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
1734 1963be61 2023-04-14 stsp if (errstr)
1735 1963be61 2023-04-14 stsp return -1;
1736 1963be61 2023-04-14 stsp return 0;
1737 1963be61 2023-04-14 stsp }
1738 ba97b2d7 2024-03-20 stsp
1739 ba97b2d7 2024-03-20 stsp const struct got_error *
1740 ba97b2d7 2024-03-20 stsp gotd_parse_url(char **proto, char **host, char **port,
1741 ba97b2d7 2024-03-20 stsp char **request_path, const char *url)
1742 ba97b2d7 2024-03-20 stsp {
1743 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1744 ba97b2d7 2024-03-20 stsp char *s, *p, *q;
1745 ba97b2d7 2024-03-20 stsp
1746 ba97b2d7 2024-03-20 stsp *proto = *host = *port = *request_path = NULL;
1747 ba97b2d7 2024-03-20 stsp
1748 ba97b2d7 2024-03-20 stsp p = strstr(url, "://");
1749 ba97b2d7 2024-03-20 stsp if (!p)
1750 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PARSE_URI);
1751 ba97b2d7 2024-03-20 stsp
1752 ba97b2d7 2024-03-20 stsp *proto = strndup(url, p - url);
1753 ba97b2d7 2024-03-20 stsp if (*proto == NULL) {
1754 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1755 ba97b2d7 2024-03-20 stsp goto done;
1756 ba97b2d7 2024-03-20 stsp }
1757 ba97b2d7 2024-03-20 stsp s = p + 3;
1758 ba97b2d7 2024-03-20 stsp
1759 ba97b2d7 2024-03-20 stsp p = strstr(s, "/");
1760 f9e65370 2024-03-27 op if (p == NULL) {
1761 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1762 ba97b2d7 2024-03-20 stsp goto done;
1763 ba97b2d7 2024-03-20 stsp }
1764 ba97b2d7 2024-03-20 stsp
1765 ba97b2d7 2024-03-20 stsp q = memchr(s, ':', p - s);
1766 ba97b2d7 2024-03-20 stsp if (q) {
1767 ba97b2d7 2024-03-20 stsp *host = strndup(s, q - s);
1768 ba97b2d7 2024-03-20 stsp if (*host == NULL) {
1769 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1770 ba97b2d7 2024-03-20 stsp goto done;
1771 ba97b2d7 2024-03-20 stsp }
1772 ba97b2d7 2024-03-20 stsp if ((*host)[0] == '\0') {
1773 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1774 ba97b2d7 2024-03-20 stsp goto done;
1775 ba97b2d7 2024-03-20 stsp }
1776 ba97b2d7 2024-03-20 stsp *port = strndup(q + 1, p - (q + 1));
1777 ba97b2d7 2024-03-20 stsp if (*port == NULL) {
1778 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1779 ba97b2d7 2024-03-20 stsp goto done;
1780 ba97b2d7 2024-03-20 stsp }
1781 ba97b2d7 2024-03-20 stsp if ((*port)[0] == '\0') {
1782 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1783 ba97b2d7 2024-03-20 stsp goto done;
1784 ba97b2d7 2024-03-20 stsp }
1785 ba97b2d7 2024-03-20 stsp } else {
1786 ba97b2d7 2024-03-20 stsp *host = strndup(s, p - s);
1787 ba97b2d7 2024-03-20 stsp if (*host == NULL) {
1788 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strndup");
1789 ba97b2d7 2024-03-20 stsp goto done;
1790 ba97b2d7 2024-03-20 stsp }
1791 ba97b2d7 2024-03-20 stsp if ((*host)[0] == '\0') {
1792 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1793 ba97b2d7 2024-03-20 stsp goto done;
1794 ba97b2d7 2024-03-20 stsp }
1795 ba97b2d7 2024-03-20 stsp }
1796 ba97b2d7 2024-03-20 stsp
1797 ba97b2d7 2024-03-20 stsp while (p[0] == '/' && p[1] == '/')
1798 ba97b2d7 2024-03-20 stsp p++;
1799 ba97b2d7 2024-03-20 stsp *request_path = strdup(p);
1800 ba97b2d7 2024-03-20 stsp if (*request_path == NULL) {
1801 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1802 ba97b2d7 2024-03-20 stsp goto done;
1803 ba97b2d7 2024-03-20 stsp }
1804 ba97b2d7 2024-03-20 stsp if ((*request_path)[0] == '\0') {
1805 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_PARSE_URI);
1806 ba97b2d7 2024-03-20 stsp goto done;
1807 ba97b2d7 2024-03-20 stsp }
1808 ba97b2d7 2024-03-20 stsp done:
1809 ba97b2d7 2024-03-20 stsp if (err) {
1810 ba97b2d7 2024-03-20 stsp free(*proto);
1811 ba97b2d7 2024-03-20 stsp *proto = NULL;
1812 ba97b2d7 2024-03-20 stsp free(*host);
1813 ba97b2d7 2024-03-20 stsp *host = NULL;
1814 ba97b2d7 2024-03-20 stsp free(*port);
1815 ba97b2d7 2024-03-20 stsp *port = NULL;
1816 ba97b2d7 2024-03-20 stsp free(*request_path);
1817 ba97b2d7 2024-03-20 stsp *request_path = NULL;
1818 ba97b2d7 2024-03-20 stsp }
1819 ba97b2d7 2024-03-20 stsp return err;
1820 ba97b2d7 2024-03-20 stsp }
1821 ba97b2d7 2024-03-20 stsp
1822 ba97b2d7 2024-03-20 stsp static char *
1823 ba97b2d7 2024-03-20 stsp port_sprintf(int p)
1824 ba97b2d7 2024-03-20 stsp {
1825 ba97b2d7 2024-03-20 stsp static char portno[32];
1826 ba97b2d7 2024-03-20 stsp int n;
1827 ba97b2d7 2024-03-20 stsp
1828 ba97b2d7 2024-03-20 stsp n = snprintf(portno, sizeof(portno), "%lld", (long long)p);
1829 ba97b2d7 2024-03-20 stsp if (n < 0 || (size_t)n >= sizeof(portno))
1830 ba97b2d7 2024-03-20 stsp fatalx("port number too long: %lld", (long long)p);
1831 ba97b2d7 2024-03-20 stsp
1832 ba97b2d7 2024-03-20 stsp return portno;
1833 ba97b2d7 2024-03-20 stsp }