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 13b2bc37 2022-10-23 stsp #include <sha1.h>
37 5822e79e 2023-02-23 op #include <sha2.h>
38 13b2bc37 2022-10-23 stsp #include <stdarg.h>
39 13b2bc37 2022-10-23 stsp #include <stdlib.h>
40 13b2bc37 2022-10-23 stsp #include <stdio.h>
41 13b2bc37 2022-10-23 stsp #include <string.h>
42 13b2bc37 2022-10-23 stsp #include <syslog.h>
43 13b2bc37 2022-10-23 stsp #include <unistd.h>
44 13b2bc37 2022-10-23 stsp
45 13b2bc37 2022-10-23 stsp #include "got_error.h"
46 13b2bc37 2022-10-23 stsp #include "got_path.h"
47 9afa3de2 2023-04-04 stsp #include "got_reference.h"
48 13b2bc37 2022-10-23 stsp
49 13b2bc37 2022-10-23 stsp #include "log.h"
50 13b2bc37 2022-10-23 stsp #include "gotd.h"
51 40b85cca 2023-01-03 stsp #include "auth.h"
52 40b85cca 2023-01-03 stsp #include "listen.h"
53 13b2bc37 2022-10-23 stsp
54 13b2bc37 2022-10-23 stsp TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
55 13b2bc37 2022-10-23 stsp static struct file {
56 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(file) entry;
57 13b2bc37 2022-10-23 stsp FILE *stream;
58 13b2bc37 2022-10-23 stsp char *name;
59 13b2bc37 2022-10-23 stsp int lineno;
60 13b2bc37 2022-10-23 stsp int errors;
61 13b2bc37 2022-10-23 stsp } *file;
62 e9e0377f 2023-03-29 stsp struct file *newfile(const char *, int, int);
63 13b2bc37 2022-10-23 stsp static void closefile(struct file *);
64 13b2bc37 2022-10-23 stsp int check_file_secrecy(int, const char *);
65 13b2bc37 2022-10-23 stsp int yyparse(void);
66 13b2bc37 2022-10-23 stsp int yylex(void);
67 13b2bc37 2022-10-23 stsp int yyerror(const char *, ...)
68 13b2bc37 2022-10-23 stsp __attribute__((__format__ (printf, 1, 2)))
69 13b2bc37 2022-10-23 stsp __attribute__((__nonnull__ (1)));
70 13b2bc37 2022-10-23 stsp int kw_cmp(const void *, const void *);
71 13b2bc37 2022-10-23 stsp int lookup(char *);
72 13b2bc37 2022-10-23 stsp int lgetc(int);
73 13b2bc37 2022-10-23 stsp int lungetc(int);
74 13b2bc37 2022-10-23 stsp int findeol(void);
75 13b2bc37 2022-10-23 stsp
76 13b2bc37 2022-10-23 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 13b2bc37 2022-10-23 stsp struct sym {
78 13b2bc37 2022-10-23 stsp TAILQ_ENTRY(sym) entry;
79 13b2bc37 2022-10-23 stsp int used;
80 13b2bc37 2022-10-23 stsp int persist;
81 13b2bc37 2022-10-23 stsp char *nam;
82 13b2bc37 2022-10-23 stsp char *val;
83 13b2bc37 2022-10-23 stsp };
84 13b2bc37 2022-10-23 stsp
85 13b2bc37 2022-10-23 stsp int symset(const char *, const char *, int);
86 13b2bc37 2022-10-23 stsp char *symget(const char *);
87 13b2bc37 2022-10-23 stsp
88 13b2bc37 2022-10-23 stsp static int errors;
89 13b2bc37 2022-10-23 stsp
90 13b2bc37 2022-10-23 stsp static struct gotd *gotd;
91 13b2bc37 2022-10-23 stsp static struct gotd_repo *new_repo;
92 fc89c900 2023-01-03 op static int conf_limit_user_connections(const char *, int);
93 13b2bc37 2022-10-23 stsp static struct gotd_repo *conf_new_repo(const char *);
94 533abaaa 2022-11-18 op static void conf_new_access_rule(struct gotd_repo *,
95 533abaaa 2022-11-18 op enum gotd_access, int, char *);
96 9afa3de2 2023-04-04 stsp static int conf_protect_ref_namespace(
97 9afa3de2 2023-04-04 stsp struct got_pathlist_head *, char *);
98 9afa3de2 2023-04-04 stsp static int conf_protect_tag_namespace(struct gotd_repo *,
99 9afa3de2 2023-04-04 stsp char *);
100 9afa3de2 2023-04-04 stsp static int conf_protect_branch_namespace(
101 9afa3de2 2023-04-04 stsp struct gotd_repo *, char *);
102 9afa3de2 2023-04-04 stsp static int conf_protect_branch(struct gotd_repo *,
103 9afa3de2 2023-04-04 stsp char *);
104 13b2bc37 2022-10-23 stsp static enum gotd_procid gotd_proc_id;
105 13b2bc37 2022-10-23 stsp
106 13b2bc37 2022-10-23 stsp typedef struct {
107 13b2bc37 2022-10-23 stsp union {
108 13b2bc37 2022-10-23 stsp long long number;
109 13b2bc37 2022-10-23 stsp char *string;
110 40b85cca 2023-01-03 stsp struct timeval tv;
111 13b2bc37 2022-10-23 stsp } v;
112 13b2bc37 2022-10-23 stsp int lineno;
113 13b2bc37 2022-10-23 stsp } YYSTYPE;
114 13b2bc37 2022-10-23 stsp
115 13b2bc37 2022-10-23 stsp %}
116 13b2bc37 2022-10-23 stsp
117 83577462 2023-01-05 stsp %token PATH ERROR LISTEN ON USER REPOSITORY PERMIT DENY
118 40b85cca 2023-01-03 stsp %token RO RW CONNECTION LIMIT REQUEST TIMEOUT
119 9afa3de2 2023-04-04 stsp %token PROTECT NAMESPACE BRANCH TAG
120 13b2bc37 2022-10-23 stsp
121 13b2bc37 2022-10-23 stsp %token <v.string> STRING
122 13b2bc37 2022-10-23 stsp %token <v.number> NUMBER
123 40b85cca 2023-01-03 stsp %type <v.tv> timeout
124 13b2bc37 2022-10-23 stsp
125 13b2bc37 2022-10-23 stsp %%
126 13b2bc37 2022-10-23 stsp
127 13b2bc37 2022-10-23 stsp grammar :
128 13b2bc37 2022-10-23 stsp | grammar '\n'
129 13b2bc37 2022-10-23 stsp | grammar main '\n'
130 13b2bc37 2022-10-23 stsp | grammar repository '\n'
131 13b2bc37 2022-10-23 stsp ;
132 13b2bc37 2022-10-23 stsp
133 40b85cca 2023-01-03 stsp timeout : NUMBER {
134 40b85cca 2023-01-03 stsp if ($1 < 0) {
135 40b85cca 2023-01-03 stsp yyerror("invalid timeout: %lld", $1);
136 40b85cca 2023-01-03 stsp YYERROR;
137 40b85cca 2023-01-03 stsp }
138 40b85cca 2023-01-03 stsp $$.tv_sec = $1;
139 40b85cca 2023-01-03 stsp $$.tv_usec = 0;
140 40b85cca 2023-01-03 stsp }
141 2be11cde 2023-01-03 op | STRING {
142 2be11cde 2023-01-03 op const char *errstr;
143 2be11cde 2023-01-03 op const char *type = "seconds";
144 2be11cde 2023-01-03 op size_t len;
145 2be11cde 2023-01-03 op int mul = 1;
146 2be11cde 2023-01-03 op
147 2be11cde 2023-01-03 op if (*$1 == '\0') {
148 2be11cde 2023-01-03 op yyerror("invalid number of seconds: %s", $1);
149 2be11cde 2023-01-03 op free($1);
150 2be11cde 2023-01-03 op YYERROR;
151 2be11cde 2023-01-03 op }
152 2be11cde 2023-01-03 op
153 2be11cde 2023-01-03 op len = strlen($1);
154 2be11cde 2023-01-03 op switch ($1[len - 1]) {
155 2be11cde 2023-01-03 op case 'S':
156 2be11cde 2023-01-03 op case 's':
157 2be11cde 2023-01-03 op $1[len - 1] = '\0';
158 2be11cde 2023-01-03 op break;
159 2be11cde 2023-01-03 op case 'M':
160 2be11cde 2023-01-03 op case 'm':
161 2be11cde 2023-01-03 op type = "minutes";
162 2be11cde 2023-01-03 op mul = 60;
163 2be11cde 2023-01-03 op $1[len - 1] = '\0';
164 2be11cde 2023-01-03 op break;
165 2be11cde 2023-01-03 op case 'H':
166 2be11cde 2023-01-03 op case 'h':
167 2be11cde 2023-01-03 op type = "hours";
168 2be11cde 2023-01-03 op mul = 60 * 60;
169 2be11cde 2023-01-03 op $1[len - 1] = '\0';
170 2be11cde 2023-01-03 op break;
171 2be11cde 2023-01-03 op }
172 2be11cde 2023-01-03 op
173 2be11cde 2023-01-03 op $$.tv_usec = 0;
174 71cd355c 2023-01-03 op $$.tv_sec = strtonum($1, 0, INT_MAX / mul, &errstr);
175 2be11cde 2023-01-03 op if (errstr) {
176 2be11cde 2023-01-03 op yyerror("number of %s is %s: %s", type,
177 2be11cde 2023-01-03 op errstr, $1);
178 2be11cde 2023-01-03 op free($1);
179 2be11cde 2023-01-03 op YYERROR;
180 2be11cde 2023-01-03 op }
181 2be11cde 2023-01-03 op
182 2be11cde 2023-01-03 op $$.tv_sec *= mul;
183 2be11cde 2023-01-03 op free($1);
184 2be11cde 2023-01-03 op }
185 40b85cca 2023-01-03 stsp ;
186 40b85cca 2023-01-03 stsp
187 83577462 2023-01-05 stsp main : LISTEN ON STRING {
188 abe844e2 2023-01-18 op if (!got_path_is_absolute($3))
189 abe844e2 2023-01-18 op yyerror("bad unix socket path \"%s\": "
190 abe844e2 2023-01-18 op "must be an absolute path", $3);
191 abe844e2 2023-01-18 op
192 d93ecf7d 2022-12-14 stsp if (gotd_proc_id == PROC_LISTEN) {
193 83577462 2023-01-05 stsp if (strlcpy(gotd->unix_socket_path, $3,
194 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >=
195 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) {
196 13b2bc37 2022-10-23 stsp yyerror("%s: unix socket path too long",
197 13b2bc37 2022-10-23 stsp __func__);
198 83577462 2023-01-05 stsp free($3);
199 13b2bc37 2022-10-23 stsp YYERROR;
200 13b2bc37 2022-10-23 stsp }
201 13b2bc37 2022-10-23 stsp }
202 83577462 2023-01-05 stsp free($3);
203 13b2bc37 2022-10-23 stsp }
204 13b2bc37 2022-10-23 stsp | USER STRING {
205 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, $2,
206 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >=
207 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) {
208 13b2bc37 2022-10-23 stsp yyerror("%s: user name too long", __func__);
209 13b2bc37 2022-10-23 stsp free($2);
210 13b2bc37 2022-10-23 stsp YYERROR;
211 13b2bc37 2022-10-23 stsp }
212 13b2bc37 2022-10-23 stsp free($2);
213 13b2bc37 2022-10-23 stsp }
214 40b85cca 2023-01-03 stsp | connection
215 13b2bc37 2022-10-23 stsp ;
216 13b2bc37 2022-10-23 stsp
217 40b85cca 2023-01-03 stsp connection : CONNECTION '{' optnl conflags_l '}'
218 40b85cca 2023-01-03 stsp | CONNECTION conflags
219 40b85cca 2023-01-03 stsp
220 40b85cca 2023-01-03 stsp conflags_l : conflags optnl conflags_l
221 40b85cca 2023-01-03 stsp | conflags optnl
222 40b85cca 2023-01-03 stsp ;
223 40b85cca 2023-01-03 stsp
224 40b85cca 2023-01-03 stsp conflags : REQUEST TIMEOUT timeout {
225 46e48ac7 2023-01-03 stsp if ($3.tv_sec <= 0) {
226 46e48ac7 2023-01-03 stsp yyerror("invalid timeout: %lld", $3.tv_sec);
227 46e48ac7 2023-01-03 stsp YYERROR;
228 46e48ac7 2023-01-03 stsp }
229 40b85cca 2023-01-03 stsp memcpy(&gotd->request_timeout, &$3,
230 40b85cca 2023-01-03 stsp sizeof(gotd->request_timeout));
231 40b85cca 2023-01-03 stsp }
232 40b85cca 2023-01-03 stsp | LIMIT USER STRING NUMBER {
233 40b85cca 2023-01-03 stsp if (gotd_proc_id == PROC_LISTEN &&
234 40b85cca 2023-01-03 stsp conf_limit_user_connections($3, $4) == -1) {
235 40b85cca 2023-01-03 stsp free($3);
236 40b85cca 2023-01-03 stsp YYERROR;
237 40b85cca 2023-01-03 stsp }
238 40b85cca 2023-01-03 stsp free($3);
239 40b85cca 2023-01-03 stsp }
240 40b85cca 2023-01-03 stsp ;
241 40b85cca 2023-01-03 stsp
242 9afa3de2 2023-04-04 stsp protect : PROTECT '{' optnl protectflags_l '}'
243 9afa3de2 2023-04-04 stsp | PROTECT protectflags
244 9afa3de2 2023-04-04 stsp
245 9afa3de2 2023-04-04 stsp protectflags_l : protectflags optnl protectflags_l
246 9afa3de2 2023-04-04 stsp | protectflags optnl
247 9afa3de2 2023-04-04 stsp ;
248 9afa3de2 2023-04-04 stsp
249 9afa3de2 2023-04-04 stsp protectflags : TAG NAMESPACE STRING {
250 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
251 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
252 9afa3de2 2023-04-04 stsp if (conf_protect_tag_namespace(new_repo, $3)) {
253 9afa3de2 2023-04-04 stsp free($3);
254 9afa3de2 2023-04-04 stsp YYERROR;
255 9afa3de2 2023-04-04 stsp }
256 9afa3de2 2023-04-04 stsp }
257 584140c2 2023-04-05 op free($3);
258 9afa3de2 2023-04-04 stsp }
259 9afa3de2 2023-04-04 stsp | BRANCH NAMESPACE STRING {
260 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
261 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
262 9afa3de2 2023-04-04 stsp if (conf_protect_branch_namespace(new_repo,
263 9afa3de2 2023-04-04 stsp $3)) {
264 9afa3de2 2023-04-04 stsp free($3);
265 9afa3de2 2023-04-04 stsp YYERROR;
266 9afa3de2 2023-04-04 stsp }
267 9afa3de2 2023-04-04 stsp }
268 584140c2 2023-04-05 op free($3);
269 9afa3de2 2023-04-04 stsp }
270 9afa3de2 2023-04-04 stsp | BRANCH STRING {
271 9afa3de2 2023-04-04 stsp if (gotd_proc_id == PROC_GOTD ||
272 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
273 9afa3de2 2023-04-04 stsp if (conf_protect_branch(new_repo, $2)) {
274 9afa3de2 2023-04-04 stsp free($2);
275 9afa3de2 2023-04-04 stsp YYERROR;
276 9afa3de2 2023-04-04 stsp }
277 9afa3de2 2023-04-04 stsp }
278 584140c2 2023-04-05 op free($2);
279 9afa3de2 2023-04-04 stsp }
280 9afa3de2 2023-04-04 stsp ;
281 9afa3de2 2023-04-04 stsp
282 13b2bc37 2022-10-23 stsp repository : REPOSITORY STRING {
283 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
284 13b2bc37 2022-10-23 stsp
285 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
286 13b2bc37 2022-10-23 stsp if (strcmp(repo->name, $2) == 0) {
287 13b2bc37 2022-10-23 stsp yyerror("duplicate repository '%s'", $2);
288 13b2bc37 2022-10-23 stsp free($2);
289 13b2bc37 2022-10-23 stsp YYERROR;
290 13b2bc37 2022-10-23 stsp }
291 13b2bc37 2022-10-23 stsp }
292 13b2bc37 2022-10-23 stsp
293 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
294 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
295 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
296 13b2bc37 2022-10-23 stsp new_repo = conf_new_repo($2);
297 13b2bc37 2022-10-23 stsp }
298 13b2bc37 2022-10-23 stsp free($2);
299 13b2bc37 2022-10-23 stsp } '{' optnl repoopts2 '}' {
300 13b2bc37 2022-10-23 stsp }
301 13b2bc37 2022-10-23 stsp ;
302 13b2bc37 2022-10-23 stsp
303 13b2bc37 2022-10-23 stsp repoopts1 : PATH STRING {
304 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_GOTD ||
305 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_AUTH ||
306 9afa3de2 2023-04-04 stsp gotd_proc_id == PROC_REPO_WRITE) {
307 13b2bc37 2022-10-23 stsp if (!got_path_is_absolute($2)) {
308 13b2bc37 2022-10-23 stsp yyerror("%s: path %s is not absolute",
309 13b2bc37 2022-10-23 stsp __func__, $2);
310 13b2bc37 2022-10-23 stsp free($2);
311 13b2bc37 2022-10-23 stsp YYERROR;
312 13b2bc37 2022-10-23 stsp }
313 9b7f22a6 2023-01-08 stsp if (realpath($2, new_repo->path) == NULL) {
314 9b7f22a6 2023-01-08 stsp yyerror("realpath %s: %s", $2, strerror(errno));
315 13b2bc37 2022-10-23 stsp free($2);
316 13b2bc37 2022-10-23 stsp YYERROR;
317 13b2bc37 2022-10-23 stsp }
318 13b2bc37 2022-10-23 stsp }
319 13b2bc37 2022-10-23 stsp free($2);
320 0ccf3acb 2022-11-16 stsp }
321 0ccf3acb 2022-11-16 stsp | PERMIT RO STRING {
322 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
323 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
324 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED, GOTD_AUTH_READ, $3);
325 584140c2 2023-04-05 op } else
326 584140c2 2023-04-05 op free($3);
327 0ccf3acb 2022-11-16 stsp }
328 0ccf3acb 2022-11-16 stsp | PERMIT RW STRING {
329 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
330 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
331 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_PERMITTED,
332 0ccf3acb 2022-11-16 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE, $3);
333 584140c2 2023-04-05 op } else
334 584140c2 2023-04-05 op free($3);
335 13b2bc37 2022-10-23 stsp }
336 0ccf3acb 2022-11-16 stsp | DENY STRING {
337 5e25db14 2022-12-29 stsp if (gotd_proc_id == PROC_AUTH) {
338 0ccf3acb 2022-11-16 stsp conf_new_access_rule(new_repo,
339 0ccf3acb 2022-11-16 stsp GOTD_ACCESS_DENIED, 0, $2);
340 584140c2 2023-04-05 op } else
341 584140c2 2023-04-05 op free($2);
342 0ccf3acb 2022-11-16 stsp }
343 9afa3de2 2023-04-04 stsp | protect
344 13b2bc37 2022-10-23 stsp ;
345 13b2bc37 2022-10-23 stsp
346 13b2bc37 2022-10-23 stsp repoopts2 : repoopts2 repoopts1 nl
347 13b2bc37 2022-10-23 stsp | repoopts1 optnl
348 13b2bc37 2022-10-23 stsp ;
349 13b2bc37 2022-10-23 stsp
350 13b2bc37 2022-10-23 stsp nl : '\n' optnl
351 13b2bc37 2022-10-23 stsp ;
352 13b2bc37 2022-10-23 stsp
353 13b2bc37 2022-10-23 stsp optnl : '\n' optnl /* zero or more newlines */
354 13b2bc37 2022-10-23 stsp | /* empty */
355 13b2bc37 2022-10-23 stsp ;
356 13b2bc37 2022-10-23 stsp
357 13b2bc37 2022-10-23 stsp %%
358 13b2bc37 2022-10-23 stsp
359 13b2bc37 2022-10-23 stsp struct keywords {
360 13b2bc37 2022-10-23 stsp const char *k_name;
361 13b2bc37 2022-10-23 stsp int k_val;
362 13b2bc37 2022-10-23 stsp };
363 13b2bc37 2022-10-23 stsp
364 13b2bc37 2022-10-23 stsp int
365 13b2bc37 2022-10-23 stsp yyerror(const char *fmt, ...)
366 13b2bc37 2022-10-23 stsp {
367 13b2bc37 2022-10-23 stsp va_list ap;
368 13b2bc37 2022-10-23 stsp char *msg;
369 13b2bc37 2022-10-23 stsp
370 13b2bc37 2022-10-23 stsp file->errors++;
371 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
372 13b2bc37 2022-10-23 stsp if (vasprintf(&msg, fmt, ap) == -1)
373 13b2bc37 2022-10-23 stsp fatalx("yyerror vasprintf");
374 13b2bc37 2022-10-23 stsp va_end(ap);
375 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
376 13b2bc37 2022-10-23 stsp free(msg);
377 13b2bc37 2022-10-23 stsp return (0);
378 13b2bc37 2022-10-23 stsp }
379 13b2bc37 2022-10-23 stsp
380 13b2bc37 2022-10-23 stsp int
381 13b2bc37 2022-10-23 stsp kw_cmp(const void *k, const void *e)
382 13b2bc37 2022-10-23 stsp {
383 13b2bc37 2022-10-23 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
384 13b2bc37 2022-10-23 stsp }
385 13b2bc37 2022-10-23 stsp
386 13b2bc37 2022-10-23 stsp int
387 13b2bc37 2022-10-23 stsp lookup(char *s)
388 13b2bc37 2022-10-23 stsp {
389 13b2bc37 2022-10-23 stsp /* This has to be sorted always. */
390 13b2bc37 2022-10-23 stsp static const struct keywords keywords[] = {
391 9afa3de2 2023-04-04 stsp { "branch", BRANCH },
392 40b85cca 2023-01-03 stsp { "connection", CONNECTION },
393 0ccf3acb 2022-11-16 stsp { "deny", DENY },
394 40b85cca 2023-01-03 stsp { "limit", LIMIT },
395 83577462 2023-01-05 stsp { "listen", LISTEN },
396 9afa3de2 2023-04-04 stsp { "namespace", NAMESPACE },
397 13b2bc37 2022-10-23 stsp { "on", ON },
398 13b2bc37 2022-10-23 stsp { "path", PATH },
399 0ccf3acb 2022-11-16 stsp { "permit", PERMIT },
400 9afa3de2 2023-04-04 stsp { "protect", PROTECT },
401 13b2bc37 2022-10-23 stsp { "repository", REPOSITORY },
402 40b85cca 2023-01-03 stsp { "request", REQUEST },
403 0ccf3acb 2022-11-16 stsp { "ro", RO },
404 0ccf3acb 2022-11-16 stsp { "rw", RW },
405 9afa3de2 2023-04-04 stsp { "tag", TAG },
406 40b85cca 2023-01-03 stsp { "timeout", TIMEOUT },
407 13b2bc37 2022-10-23 stsp { "user", USER },
408 13b2bc37 2022-10-23 stsp };
409 13b2bc37 2022-10-23 stsp const struct keywords *p;
410 13b2bc37 2022-10-23 stsp
411 13b2bc37 2022-10-23 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
412 13b2bc37 2022-10-23 stsp sizeof(keywords[0]), kw_cmp);
413 13b2bc37 2022-10-23 stsp
414 13b2bc37 2022-10-23 stsp if (p)
415 13b2bc37 2022-10-23 stsp return (p->k_val);
416 13b2bc37 2022-10-23 stsp else
417 13b2bc37 2022-10-23 stsp return (STRING);
418 13b2bc37 2022-10-23 stsp }
419 13b2bc37 2022-10-23 stsp
420 13b2bc37 2022-10-23 stsp #define MAXPUSHBACK 128
421 13b2bc37 2022-10-23 stsp
422 13b2bc37 2022-10-23 stsp unsigned char *parsebuf;
423 13b2bc37 2022-10-23 stsp int parseindex;
424 13b2bc37 2022-10-23 stsp unsigned char pushback_buffer[MAXPUSHBACK];
425 13b2bc37 2022-10-23 stsp int pushback_index = 0;
426 13b2bc37 2022-10-23 stsp
427 13b2bc37 2022-10-23 stsp int
428 13b2bc37 2022-10-23 stsp lgetc(int quotec)
429 13b2bc37 2022-10-23 stsp {
430 13b2bc37 2022-10-23 stsp int c, next;
431 13b2bc37 2022-10-23 stsp
432 13b2bc37 2022-10-23 stsp if (parsebuf) {
433 13b2bc37 2022-10-23 stsp /* Read character from the parsebuffer instead of input. */
434 13b2bc37 2022-10-23 stsp if (parseindex >= 0) {
435 13b2bc37 2022-10-23 stsp c = parsebuf[parseindex++];
436 13b2bc37 2022-10-23 stsp if (c != '\0')
437 13b2bc37 2022-10-23 stsp return (c);
438 13b2bc37 2022-10-23 stsp parsebuf = NULL;
439 13b2bc37 2022-10-23 stsp } else
440 13b2bc37 2022-10-23 stsp parseindex++;
441 13b2bc37 2022-10-23 stsp }
442 13b2bc37 2022-10-23 stsp
443 13b2bc37 2022-10-23 stsp if (pushback_index)
444 13b2bc37 2022-10-23 stsp return (pushback_buffer[--pushback_index]);
445 13b2bc37 2022-10-23 stsp
446 13b2bc37 2022-10-23 stsp if (quotec) {
447 13b2bc37 2022-10-23 stsp c = getc(file->stream);
448 13b2bc37 2022-10-23 stsp if (c == EOF)
449 13b2bc37 2022-10-23 stsp yyerror("reached end of file while parsing "
450 13b2bc37 2022-10-23 stsp "quoted string");
451 13b2bc37 2022-10-23 stsp return (c);
452 13b2bc37 2022-10-23 stsp }
453 13b2bc37 2022-10-23 stsp
454 13b2bc37 2022-10-23 stsp c = getc(file->stream);
455 13b2bc37 2022-10-23 stsp while (c == '\\') {
456 13b2bc37 2022-10-23 stsp next = getc(file->stream);
457 13b2bc37 2022-10-23 stsp if (next != '\n') {
458 13b2bc37 2022-10-23 stsp c = next;
459 13b2bc37 2022-10-23 stsp break;
460 13b2bc37 2022-10-23 stsp }
461 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
462 13b2bc37 2022-10-23 stsp file->lineno++;
463 13b2bc37 2022-10-23 stsp c = getc(file->stream);
464 13b2bc37 2022-10-23 stsp }
465 13b2bc37 2022-10-23 stsp
466 13b2bc37 2022-10-23 stsp return (c);
467 13b2bc37 2022-10-23 stsp }
468 13b2bc37 2022-10-23 stsp
469 13b2bc37 2022-10-23 stsp int
470 13b2bc37 2022-10-23 stsp lungetc(int c)
471 13b2bc37 2022-10-23 stsp {
472 13b2bc37 2022-10-23 stsp if (c == EOF)
473 13b2bc37 2022-10-23 stsp return (EOF);
474 13b2bc37 2022-10-23 stsp if (parsebuf) {
475 13b2bc37 2022-10-23 stsp parseindex--;
476 13b2bc37 2022-10-23 stsp if (parseindex >= 0)
477 13b2bc37 2022-10-23 stsp return (c);
478 13b2bc37 2022-10-23 stsp }
479 13b2bc37 2022-10-23 stsp if (pushback_index < MAXPUSHBACK-1)
480 13b2bc37 2022-10-23 stsp return (pushback_buffer[pushback_index++] = c);
481 13b2bc37 2022-10-23 stsp else
482 13b2bc37 2022-10-23 stsp return (EOF);
483 13b2bc37 2022-10-23 stsp }
484 13b2bc37 2022-10-23 stsp
485 13b2bc37 2022-10-23 stsp int
486 13b2bc37 2022-10-23 stsp findeol(void)
487 13b2bc37 2022-10-23 stsp {
488 13b2bc37 2022-10-23 stsp int c;
489 13b2bc37 2022-10-23 stsp
490 13b2bc37 2022-10-23 stsp parsebuf = NULL;
491 13b2bc37 2022-10-23 stsp
492 13b2bc37 2022-10-23 stsp /* Skip to either EOF or the first real EOL. */
493 13b2bc37 2022-10-23 stsp while (1) {
494 13b2bc37 2022-10-23 stsp if (pushback_index)
495 13b2bc37 2022-10-23 stsp c = pushback_buffer[--pushback_index];
496 13b2bc37 2022-10-23 stsp else
497 13b2bc37 2022-10-23 stsp c = lgetc(0);
498 13b2bc37 2022-10-23 stsp if (c == '\n') {
499 13b2bc37 2022-10-23 stsp file->lineno++;
500 13b2bc37 2022-10-23 stsp break;
501 13b2bc37 2022-10-23 stsp }
502 13b2bc37 2022-10-23 stsp if (c == EOF)
503 13b2bc37 2022-10-23 stsp break;
504 13b2bc37 2022-10-23 stsp }
505 13b2bc37 2022-10-23 stsp return (ERROR);
506 13b2bc37 2022-10-23 stsp }
507 13b2bc37 2022-10-23 stsp
508 13b2bc37 2022-10-23 stsp int
509 13b2bc37 2022-10-23 stsp yylex(void)
510 13b2bc37 2022-10-23 stsp {
511 13b2bc37 2022-10-23 stsp unsigned char buf[8096];
512 13b2bc37 2022-10-23 stsp unsigned char *p, *val;
513 13b2bc37 2022-10-23 stsp int quotec, next, c;
514 13b2bc37 2022-10-23 stsp int token;
515 13b2bc37 2022-10-23 stsp
516 13b2bc37 2022-10-23 stsp top:
517 13b2bc37 2022-10-23 stsp p = buf;
518 13b2bc37 2022-10-23 stsp c = lgetc(0);
519 13b2bc37 2022-10-23 stsp while (c == ' ' || c == '\t')
520 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
521 13b2bc37 2022-10-23 stsp
522 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
523 13b2bc37 2022-10-23 stsp if (c == '#') {
524 13b2bc37 2022-10-23 stsp c = lgetc(0);
525 13b2bc37 2022-10-23 stsp while (c != '\n' && c != EOF)
526 13b2bc37 2022-10-23 stsp c = lgetc(0); /* nothing */
527 13b2bc37 2022-10-23 stsp }
528 13b2bc37 2022-10-23 stsp if (c == '$' && parsebuf == NULL) {
529 13b2bc37 2022-10-23 stsp while (1) {
530 13b2bc37 2022-10-23 stsp c = lgetc(0);
531 13b2bc37 2022-10-23 stsp if (c == EOF)
532 13b2bc37 2022-10-23 stsp return (0);
533 13b2bc37 2022-10-23 stsp
534 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
535 13b2bc37 2022-10-23 stsp yyerror("string too long");
536 13b2bc37 2022-10-23 stsp return (findeol());
537 13b2bc37 2022-10-23 stsp }
538 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == '_') {
539 13b2bc37 2022-10-23 stsp *p++ = c;
540 13b2bc37 2022-10-23 stsp continue;
541 13b2bc37 2022-10-23 stsp }
542 13b2bc37 2022-10-23 stsp *p = '\0';
543 13b2bc37 2022-10-23 stsp lungetc(c);
544 13b2bc37 2022-10-23 stsp break;
545 13b2bc37 2022-10-23 stsp }
546 13b2bc37 2022-10-23 stsp val = symget(buf);
547 13b2bc37 2022-10-23 stsp if (val == NULL) {
548 13b2bc37 2022-10-23 stsp yyerror("macro '%s' not defined", buf);
549 13b2bc37 2022-10-23 stsp return (findeol());
550 13b2bc37 2022-10-23 stsp }
551 13b2bc37 2022-10-23 stsp parsebuf = val;
552 13b2bc37 2022-10-23 stsp parseindex = 0;
553 13b2bc37 2022-10-23 stsp goto top;
554 13b2bc37 2022-10-23 stsp }
555 13b2bc37 2022-10-23 stsp
556 13b2bc37 2022-10-23 stsp switch (c) {
557 13b2bc37 2022-10-23 stsp case '\'':
558 13b2bc37 2022-10-23 stsp case '"':
559 13b2bc37 2022-10-23 stsp quotec = c;
560 13b2bc37 2022-10-23 stsp while (1) {
561 13b2bc37 2022-10-23 stsp c = lgetc(quotec);
562 13b2bc37 2022-10-23 stsp if (c == EOF)
563 13b2bc37 2022-10-23 stsp return (0);
564 13b2bc37 2022-10-23 stsp if (c == '\n') {
565 13b2bc37 2022-10-23 stsp file->lineno++;
566 13b2bc37 2022-10-23 stsp continue;
567 13b2bc37 2022-10-23 stsp } else if (c == '\\') {
568 13b2bc37 2022-10-23 stsp next = lgetc(quotec);
569 13b2bc37 2022-10-23 stsp if (next == EOF)
570 13b2bc37 2022-10-23 stsp return (0);
571 13b2bc37 2022-10-23 stsp if (next == quotec || c == ' ' || c == '\t')
572 13b2bc37 2022-10-23 stsp c = next;
573 13b2bc37 2022-10-23 stsp else if (next == '\n') {
574 13b2bc37 2022-10-23 stsp file->lineno++;
575 13b2bc37 2022-10-23 stsp continue;
576 13b2bc37 2022-10-23 stsp } else
577 13b2bc37 2022-10-23 stsp lungetc(next);
578 13b2bc37 2022-10-23 stsp } else if (c == quotec) {
579 13b2bc37 2022-10-23 stsp *p = '\0';
580 13b2bc37 2022-10-23 stsp break;
581 13b2bc37 2022-10-23 stsp } else if (c == '\0') {
582 13b2bc37 2022-10-23 stsp yyerror("syntax error");
583 13b2bc37 2022-10-23 stsp return (findeol());
584 13b2bc37 2022-10-23 stsp }
585 13b2bc37 2022-10-23 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
586 13b2bc37 2022-10-23 stsp yyerror("string too long");
587 13b2bc37 2022-10-23 stsp return (findeol());
588 13b2bc37 2022-10-23 stsp }
589 13b2bc37 2022-10-23 stsp *p++ = c;
590 13b2bc37 2022-10-23 stsp }
591 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
592 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
593 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
594 13b2bc37 2022-10-23 stsp return (STRING);
595 13b2bc37 2022-10-23 stsp }
596 13b2bc37 2022-10-23 stsp
597 13b2bc37 2022-10-23 stsp #define allowed_to_end_number(x) \
598 13b2bc37 2022-10-23 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
599 13b2bc37 2022-10-23 stsp
600 13b2bc37 2022-10-23 stsp if (c == '-' || isdigit(c)) {
601 13b2bc37 2022-10-23 stsp do {
602 13b2bc37 2022-10-23 stsp *p++ = c;
603 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
604 13b2bc37 2022-10-23 stsp yyerror("string too long");
605 13b2bc37 2022-10-23 stsp return (findeol());
606 13b2bc37 2022-10-23 stsp }
607 13b2bc37 2022-10-23 stsp c = lgetc(0);
608 13b2bc37 2022-10-23 stsp } while (c != EOF && isdigit(c));
609 13b2bc37 2022-10-23 stsp lungetc(c);
610 13b2bc37 2022-10-23 stsp if (p == buf + 1 && buf[0] == '-')
611 13b2bc37 2022-10-23 stsp goto nodigits;
612 13b2bc37 2022-10-23 stsp if (c == EOF || allowed_to_end_number(c)) {
613 13b2bc37 2022-10-23 stsp const char *errstr = NULL;
614 13b2bc37 2022-10-23 stsp
615 13b2bc37 2022-10-23 stsp *p = '\0';
616 13b2bc37 2022-10-23 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
617 13b2bc37 2022-10-23 stsp LLONG_MAX, &errstr);
618 13b2bc37 2022-10-23 stsp if (errstr) {
619 13b2bc37 2022-10-23 stsp yyerror("\"%s\" invalid number: %s",
620 13b2bc37 2022-10-23 stsp buf, errstr);
621 13b2bc37 2022-10-23 stsp return (findeol());
622 13b2bc37 2022-10-23 stsp }
623 13b2bc37 2022-10-23 stsp return (NUMBER);
624 13b2bc37 2022-10-23 stsp } else {
625 13b2bc37 2022-10-23 stsp nodigits:
626 13b2bc37 2022-10-23 stsp while (p > buf + 1)
627 13b2bc37 2022-10-23 stsp lungetc(*--p);
628 13b2bc37 2022-10-23 stsp c = *--p;
629 13b2bc37 2022-10-23 stsp if (c == '-')
630 13b2bc37 2022-10-23 stsp return (c);
631 13b2bc37 2022-10-23 stsp }
632 13b2bc37 2022-10-23 stsp }
633 13b2bc37 2022-10-23 stsp
634 13b2bc37 2022-10-23 stsp #define allowed_in_string(x) \
635 13b2bc37 2022-10-23 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
636 13b2bc37 2022-10-23 stsp x != '{' && x != '}' && \
637 13b2bc37 2022-10-23 stsp x != '!' && x != '=' && x != '#' && \
638 13b2bc37 2022-10-23 stsp x != ','))
639 13b2bc37 2022-10-23 stsp
640 13b2bc37 2022-10-23 stsp if (isalnum(c) || c == ':' || c == '_') {
641 13b2bc37 2022-10-23 stsp do {
642 13b2bc37 2022-10-23 stsp *p++ = c;
643 13b2bc37 2022-10-23 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
644 13b2bc37 2022-10-23 stsp yyerror("string too long");
645 13b2bc37 2022-10-23 stsp return (findeol());
646 13b2bc37 2022-10-23 stsp }
647 13b2bc37 2022-10-23 stsp c = lgetc(0);
648 13b2bc37 2022-10-23 stsp } while (c != EOF && (allowed_in_string(c)));
649 13b2bc37 2022-10-23 stsp lungetc(c);
650 13b2bc37 2022-10-23 stsp *p = '\0';
651 13b2bc37 2022-10-23 stsp token = lookup(buf);
652 13b2bc37 2022-10-23 stsp if (token == STRING) {
653 13b2bc37 2022-10-23 stsp yylval.v.string = strdup(buf);
654 13b2bc37 2022-10-23 stsp if (yylval.v.string == NULL)
655 13b2bc37 2022-10-23 stsp err(1, "yylex: strdup");
656 13b2bc37 2022-10-23 stsp }
657 13b2bc37 2022-10-23 stsp return (token);
658 13b2bc37 2022-10-23 stsp }
659 13b2bc37 2022-10-23 stsp if (c == '\n') {
660 13b2bc37 2022-10-23 stsp yylval.lineno = file->lineno;
661 13b2bc37 2022-10-23 stsp file->lineno++;
662 13b2bc37 2022-10-23 stsp }
663 13b2bc37 2022-10-23 stsp if (c == EOF)
664 13b2bc37 2022-10-23 stsp return (0);
665 13b2bc37 2022-10-23 stsp return (c);
666 13b2bc37 2022-10-23 stsp }
667 13b2bc37 2022-10-23 stsp
668 13b2bc37 2022-10-23 stsp int
669 13b2bc37 2022-10-23 stsp check_file_secrecy(int fd, const char *fname)
670 13b2bc37 2022-10-23 stsp {
671 13b2bc37 2022-10-23 stsp struct stat st;
672 13b2bc37 2022-10-23 stsp
673 13b2bc37 2022-10-23 stsp if (fstat(fd, &st)) {
674 13b2bc37 2022-10-23 stsp log_warn("cannot stat %s", fname);
675 13b2bc37 2022-10-23 stsp return (-1);
676 13b2bc37 2022-10-23 stsp }
677 13b2bc37 2022-10-23 stsp if (st.st_uid != 0 && st.st_uid != getuid()) {
678 13b2bc37 2022-10-23 stsp log_warnx("%s: owner not root or current user", fname);
679 13b2bc37 2022-10-23 stsp return (-1);
680 13b2bc37 2022-10-23 stsp }
681 13b2bc37 2022-10-23 stsp if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
682 13b2bc37 2022-10-23 stsp log_warnx("%s: group writable or world read/writable", fname);
683 13b2bc37 2022-10-23 stsp return (-1);
684 13b2bc37 2022-10-23 stsp }
685 13b2bc37 2022-10-23 stsp return (0);
686 13b2bc37 2022-10-23 stsp }
687 13b2bc37 2022-10-23 stsp
688 13b2bc37 2022-10-23 stsp struct file *
689 e9e0377f 2023-03-29 stsp newfile(const char *name, int secret, int required)
690 13b2bc37 2022-10-23 stsp {
691 13b2bc37 2022-10-23 stsp struct file *nfile;
692 13b2bc37 2022-10-23 stsp
693 13b2bc37 2022-10-23 stsp nfile = calloc(1, sizeof(struct file));
694 13b2bc37 2022-10-23 stsp if (nfile == NULL) {
695 13b2bc37 2022-10-23 stsp log_warn("calloc");
696 13b2bc37 2022-10-23 stsp return (NULL);
697 13b2bc37 2022-10-23 stsp }
698 13b2bc37 2022-10-23 stsp nfile->name = strdup(name);
699 13b2bc37 2022-10-23 stsp if (nfile->name == NULL) {
700 13b2bc37 2022-10-23 stsp log_warn("strdup");
701 13b2bc37 2022-10-23 stsp free(nfile);
702 13b2bc37 2022-10-23 stsp return (NULL);
703 13b2bc37 2022-10-23 stsp }
704 13b2bc37 2022-10-23 stsp nfile->stream = fopen(nfile->name, "r");
705 13b2bc37 2022-10-23 stsp if (nfile->stream == NULL) {
706 e9e0377f 2023-03-29 stsp if (required)
707 e9e0377f 2023-03-29 stsp log_warn("open %s", nfile->name);
708 13b2bc37 2022-10-23 stsp free(nfile->name);
709 13b2bc37 2022-10-23 stsp free(nfile);
710 13b2bc37 2022-10-23 stsp return (NULL);
711 13b2bc37 2022-10-23 stsp } else if (secret &&
712 13b2bc37 2022-10-23 stsp check_file_secrecy(fileno(nfile->stream), nfile->name)) {
713 13b2bc37 2022-10-23 stsp fclose(nfile->stream);
714 13b2bc37 2022-10-23 stsp free(nfile->name);
715 13b2bc37 2022-10-23 stsp free(nfile);
716 13b2bc37 2022-10-23 stsp return (NULL);
717 13b2bc37 2022-10-23 stsp }
718 13b2bc37 2022-10-23 stsp nfile->lineno = 1;
719 13b2bc37 2022-10-23 stsp return (nfile);
720 13b2bc37 2022-10-23 stsp }
721 13b2bc37 2022-10-23 stsp
722 13b2bc37 2022-10-23 stsp static void
723 13b2bc37 2022-10-23 stsp closefile(struct file *xfile)
724 13b2bc37 2022-10-23 stsp {
725 13b2bc37 2022-10-23 stsp fclose(xfile->stream);
726 13b2bc37 2022-10-23 stsp free(xfile->name);
727 13b2bc37 2022-10-23 stsp free(xfile);
728 13b2bc37 2022-10-23 stsp }
729 13b2bc37 2022-10-23 stsp
730 13b2bc37 2022-10-23 stsp int
731 13b2bc37 2022-10-23 stsp parse_config(const char *filename, enum gotd_procid proc_id,
732 e9e0377f 2023-03-29 stsp struct gotd *env, int require_config_file)
733 13b2bc37 2022-10-23 stsp {
734 13b2bc37 2022-10-23 stsp struct sym *sym, *next;
735 3b706203 2023-01-02 stsp struct gotd_repo *repo;
736 13b2bc37 2022-10-23 stsp
737 13b2bc37 2022-10-23 stsp memset(env, 0, sizeof(*env));
738 13b2bc37 2022-10-23 stsp
739 13b2bc37 2022-10-23 stsp gotd = env;
740 13b2bc37 2022-10-23 stsp gotd_proc_id = proc_id;
741 13b2bc37 2022-10-23 stsp TAILQ_INIT(&gotd->repos);
742 13b2bc37 2022-10-23 stsp
743 13b2bc37 2022-10-23 stsp /* Apply default values. */
744 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->unix_socket_path, GOTD_UNIX_SOCKET,
745 13b2bc37 2022-10-23 stsp sizeof(gotd->unix_socket_path)) >= sizeof(gotd->unix_socket_path)) {
746 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: unix socket path too long", __func__);
747 13b2bc37 2022-10-23 stsp return -1;
748 13b2bc37 2022-10-23 stsp }
749 13b2bc37 2022-10-23 stsp if (strlcpy(gotd->user_name, GOTD_USER,
750 13b2bc37 2022-10-23 stsp sizeof(gotd->user_name)) >= sizeof(gotd->user_name)) {
751 13b2bc37 2022-10-23 stsp fprintf(stderr, "%s: user name too long", __func__);
752 13b2bc37 2022-10-23 stsp return -1;
753 13b2bc37 2022-10-23 stsp }
754 40b85cca 2023-01-03 stsp
755 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_sec = GOTD_DEFAULT_REQUEST_TIMEOUT;
756 40b85cca 2023-01-03 stsp gotd->request_timeout.tv_usec = 0;
757 13b2bc37 2022-10-23 stsp
758 e9e0377f 2023-03-29 stsp file = newfile(filename, 0, require_config_file);
759 3fa763e5 2023-03-01 stsp if (file == NULL)
760 e9e0377f 2023-03-29 stsp return require_config_file ? -1 : 0;
761 13b2bc37 2022-10-23 stsp
762 13b2bc37 2022-10-23 stsp yyparse();
763 13b2bc37 2022-10-23 stsp errors = file->errors;
764 13b2bc37 2022-10-23 stsp closefile(file);
765 13b2bc37 2022-10-23 stsp
766 13b2bc37 2022-10-23 stsp /* Free macros and check which have not been used. */
767 13b2bc37 2022-10-23 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
768 13b2bc37 2022-10-23 stsp if ((gotd->verbosity > 1) && !sym->used)
769 13b2bc37 2022-10-23 stsp fprintf(stderr, "warning: macro '%s' not used\n",
770 13b2bc37 2022-10-23 stsp sym->nam);
771 13b2bc37 2022-10-23 stsp if (!sym->persist) {
772 13b2bc37 2022-10-23 stsp free(sym->nam);
773 13b2bc37 2022-10-23 stsp free(sym->val);
774 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
775 13b2bc37 2022-10-23 stsp free(sym);
776 13b2bc37 2022-10-23 stsp }
777 13b2bc37 2022-10-23 stsp }
778 13b2bc37 2022-10-23 stsp
779 13b2bc37 2022-10-23 stsp if (errors)
780 13b2bc37 2022-10-23 stsp return (-1);
781 13b2bc37 2022-10-23 stsp
782 3b706203 2023-01-02 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
783 3b706203 2023-01-02 stsp if (repo->path[0] == '\0') {
784 2507ffb7 2023-01-02 stsp log_warnx("repository \"%s\": no path provided in "
785 2507ffb7 2023-01-02 stsp "configuration file", repo->name);
786 3b706203 2023-01-02 stsp return (-1);
787 3b706203 2023-01-02 stsp }
788 3b706203 2023-01-02 stsp }
789 3b706203 2023-01-02 stsp
790 809a54db 2023-01-18 op if (proc_id == PROC_GOTD && TAILQ_EMPTY(&gotd->repos)) {
791 809a54db 2023-01-18 op log_warnx("no repository defined in configuration file");
792 809a54db 2023-01-18 op return (-1);
793 809a54db 2023-01-18 op }
794 809a54db 2023-01-18 op
795 13b2bc37 2022-10-23 stsp return (0);
796 13b2bc37 2022-10-23 stsp }
797 13b2bc37 2022-10-23 stsp
798 40b85cca 2023-01-03 stsp static int
799 40b85cca 2023-01-03 stsp uid_connection_limit_cmp(const void *pa, const void *pb)
800 40b85cca 2023-01-03 stsp {
801 40b85cca 2023-01-03 stsp const struct gotd_uid_connection_limit *a = pa, *b = pb;
802 40b85cca 2023-01-03 stsp
803 40b85cca 2023-01-03 stsp if (a->uid < b->uid)
804 40b85cca 2023-01-03 stsp return -1;
805 40b85cca 2023-01-03 stsp else if (a->uid > b->uid);
806 40b85cca 2023-01-03 stsp return 1;
807 40b85cca 2023-01-03 stsp
808 40b85cca 2023-01-03 stsp return 0;
809 40b85cca 2023-01-03 stsp }
810 40b85cca 2023-01-03 stsp
811 40b85cca 2023-01-03 stsp static int
812 40b85cca 2023-01-03 stsp conf_limit_user_connections(const char *user, int maximum)
813 40b85cca 2023-01-03 stsp {
814 40b85cca 2023-01-03 stsp uid_t uid;
815 40b85cca 2023-01-03 stsp struct gotd_uid_connection_limit *limit;
816 40b85cca 2023-01-03 stsp size_t nlimits;
817 40b85cca 2023-01-03 stsp
818 40b85cca 2023-01-03 stsp if (maximum < 1) {
819 40b85cca 2023-01-03 stsp yyerror("max connections cannot be smaller 1");
820 40b85cca 2023-01-03 stsp return -1;
821 40b85cca 2023-01-03 stsp }
822 40b85cca 2023-01-03 stsp if (maximum > GOTD_MAXCLIENTS) {
823 40b85cca 2023-01-03 stsp yyerror("max connections must be <= %d", GOTD_MAXCLIENTS);
824 40b85cca 2023-01-03 stsp return -1;
825 40b85cca 2023-01-03 stsp }
826 40b85cca 2023-01-03 stsp
827 40b85cca 2023-01-03 stsp if (gotd_auth_parseuid(user, &uid) == -1) {
828 40b85cca 2023-01-03 stsp yyerror("%s: no such user", user);
829 40b85cca 2023-01-03 stsp return -1;
830 40b85cca 2023-01-03 stsp }
831 40b85cca 2023-01-03 stsp
832 40b85cca 2023-01-03 stsp limit = gotd_find_uid_connection_limit(gotd->connection_limits,
833 40b85cca 2023-01-03 stsp gotd->nconnection_limits, uid);
834 40b85cca 2023-01-03 stsp if (limit) {
835 40b85cca 2023-01-03 stsp limit->max_connections = maximum;
836 40b85cca 2023-01-03 stsp return 0;
837 40b85cca 2023-01-03 stsp }
838 40b85cca 2023-01-03 stsp
839 40b85cca 2023-01-03 stsp limit = gotd->connection_limits;
840 40b85cca 2023-01-03 stsp nlimits = gotd->nconnection_limits + 1;
841 40b85cca 2023-01-03 stsp limit = reallocarray(limit, nlimits, sizeof(*limit));
842 40b85cca 2023-01-03 stsp if (limit == NULL)
843 40b85cca 2023-01-03 stsp fatal("reallocarray");
844 40b85cca 2023-01-03 stsp
845 40b85cca 2023-01-03 stsp limit[nlimits - 1].uid = uid;
846 40b85cca 2023-01-03 stsp limit[nlimits - 1].max_connections = maximum;
847 40b85cca 2023-01-03 stsp
848 40b85cca 2023-01-03 stsp gotd->connection_limits = limit;
849 40b85cca 2023-01-03 stsp gotd->nconnection_limits = nlimits;
850 40b85cca 2023-01-03 stsp qsort(gotd->connection_limits, gotd->nconnection_limits,
851 40b85cca 2023-01-03 stsp sizeof(gotd->connection_limits[0]), uid_connection_limit_cmp);
852 40b85cca 2023-01-03 stsp
853 40b85cca 2023-01-03 stsp return 0;
854 40b85cca 2023-01-03 stsp }
855 40b85cca 2023-01-03 stsp
856 13b2bc37 2022-10-23 stsp static struct gotd_repo *
857 13b2bc37 2022-10-23 stsp conf_new_repo(const char *name)
858 13b2bc37 2022-10-23 stsp {
859 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
860 13b2bc37 2022-10-23 stsp
861 7683f79a 2023-01-02 stsp if (name[0] == '\0') {
862 7683f79a 2023-01-02 stsp fatalx("syntax error: empty repository name found in %s",
863 7683f79a 2023-01-02 stsp file->name);
864 7683f79a 2023-01-02 stsp }
865 7683f79a 2023-01-02 stsp
866 2507ffb7 2023-01-02 stsp if (strchr(name, '\n') != NULL)
867 2507ffb7 2023-01-02 stsp fatalx("repository names must not contain linefeeds: %s", name);
868 13b2bc37 2022-10-23 stsp
869 13b2bc37 2022-10-23 stsp repo = calloc(1, sizeof(*repo));
870 13b2bc37 2022-10-23 stsp if (repo == NULL)
871 13b2bc37 2022-10-23 stsp fatalx("%s: calloc", __func__);
872 13b2bc37 2022-10-23 stsp
873 0ccf3acb 2022-11-16 stsp STAILQ_INIT(&repo->rules);
874 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_tag_namespaces);
875 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branch_namespaces);
876 9afa3de2 2023-04-04 stsp TAILQ_INIT(&repo->protected_branches);
877 0ccf3acb 2022-11-16 stsp
878 13b2bc37 2022-10-23 stsp if (strlcpy(repo->name, name, sizeof(repo->name)) >=
879 13b2bc37 2022-10-23 stsp sizeof(repo->name))
880 13b2bc37 2022-10-23 stsp fatalx("%s: strlcpy", __func__);
881 13b2bc37 2022-10-23 stsp
882 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&gotd->repos, repo, entry);
883 13b2bc37 2022-10-23 stsp gotd->nrepos++;
884 13b2bc37 2022-10-23 stsp
885 13b2bc37 2022-10-23 stsp return repo;
886 13b2bc37 2022-10-23 stsp };
887 0ccf3acb 2022-11-16 stsp
888 0ccf3acb 2022-11-16 stsp static void
889 0ccf3acb 2022-11-16 stsp conf_new_access_rule(struct gotd_repo *repo, enum gotd_access access,
890 0ccf3acb 2022-11-16 stsp int authorization, char *identifier)
891 0ccf3acb 2022-11-16 stsp {
892 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
893 0ccf3acb 2022-11-16 stsp
894 0ccf3acb 2022-11-16 stsp rule = calloc(1, sizeof(*rule));
895 0ccf3acb 2022-11-16 stsp if (rule == NULL)
896 0ccf3acb 2022-11-16 stsp fatal("calloc");
897 13b2bc37 2022-10-23 stsp
898 0ccf3acb 2022-11-16 stsp rule->access = access;
899 0ccf3acb 2022-11-16 stsp rule->authorization = authorization;
900 0ccf3acb 2022-11-16 stsp rule->identifier = identifier;
901 0ccf3acb 2022-11-16 stsp
902 0ccf3acb 2022-11-16 stsp STAILQ_INSERT_TAIL(&repo->rules, rule, entry);
903 0ccf3acb 2022-11-16 stsp }
904 0ccf3acb 2022-11-16 stsp
905 9afa3de2 2023-04-04 stsp static int
906 9afa3de2 2023-04-04 stsp refname_is_valid(char *refname)
907 9afa3de2 2023-04-04 stsp {
908 9afa3de2 2023-04-04 stsp if (strlen(refname) < 5 || strncmp(refname, "refs/", 5) != 0) {
909 9afa3de2 2023-04-04 stsp yyerror("reference name must begin with \"refs/\": %s",
910 9afa3de2 2023-04-04 stsp refname);
911 9afa3de2 2023-04-04 stsp return 0;
912 9afa3de2 2023-04-04 stsp }
913 9afa3de2 2023-04-04 stsp
914 9afa3de2 2023-04-04 stsp if (!got_ref_name_is_valid(refname)) {
915 9afa3de2 2023-04-04 stsp yyerror("invalid reference name: %s", refname);
916 9afa3de2 2023-04-04 stsp return 0;
917 9afa3de2 2023-04-04 stsp }
918 9afa3de2 2023-04-04 stsp
919 9afa3de2 2023-04-04 stsp return 1;
920 9afa3de2 2023-04-04 stsp }
921 9afa3de2 2023-04-04 stsp
922 9afa3de2 2023-04-04 stsp static int
923 9afa3de2 2023-04-04 stsp conf_protect_ref_namespace(struct got_pathlist_head *refs, char *namespace)
924 9afa3de2 2023-04-04 stsp {
925 9afa3de2 2023-04-04 stsp const struct got_error *error;
926 f0426190 2023-04-05 op struct got_pathlist_entry *new;
927 9afa3de2 2023-04-04 stsp char *s;
928 9afa3de2 2023-04-04 stsp
929 9afa3de2 2023-04-04 stsp got_path_strip_trailing_slashes(namespace);
930 9afa3de2 2023-04-04 stsp if (!refname_is_valid(namespace))
931 9afa3de2 2023-04-04 stsp return -1;
932 9afa3de2 2023-04-04 stsp if (asprintf(&s, "%s/", namespace) == -1) {
933 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
934 9afa3de2 2023-04-04 stsp return -1;
935 9afa3de2 2023-04-04 stsp }
936 9afa3de2 2023-04-04 stsp
937 f0426190 2023-04-05 op error = got_pathlist_insert(&new, refs, s, NULL);
938 f0426190 2023-04-05 op if (error || new == NULL) {
939 f0426190 2023-04-05 op free(s);
940 f0426190 2023-04-05 op if (error)
941 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
942 f0426190 2023-04-05 op else
943 f0426190 2023-04-05 op yyerror("duplicate protect namespace %s", namespace);
944 9afa3de2 2023-04-04 stsp return -1;
945 9afa3de2 2023-04-04 stsp }
946 9afa3de2 2023-04-04 stsp
947 9afa3de2 2023-04-04 stsp return 0;
948 9afa3de2 2023-04-04 stsp }
949 9afa3de2 2023-04-04 stsp
950 9afa3de2 2023-04-04 stsp static int
951 9afa3de2 2023-04-04 stsp conf_protect_tag_namespace(struct gotd_repo *repo, char *namespace)
952 9afa3de2 2023-04-04 stsp {
953 9afa3de2 2023-04-04 stsp return conf_protect_ref_namespace(&repo->protected_tag_namespaces,
954 9afa3de2 2023-04-04 stsp namespace);
955 9afa3de2 2023-04-04 stsp }
956 9afa3de2 2023-04-04 stsp
957 9afa3de2 2023-04-04 stsp static int
958 9afa3de2 2023-04-04 stsp conf_protect_branch_namespace(struct gotd_repo *repo, char *namespace)
959 9afa3de2 2023-04-04 stsp {
960 9afa3de2 2023-04-04 stsp return conf_protect_ref_namespace(&repo->protected_branch_namespaces,
961 9afa3de2 2023-04-04 stsp namespace);
962 9afa3de2 2023-04-04 stsp }
963 9afa3de2 2023-04-04 stsp
964 9afa3de2 2023-04-04 stsp static int
965 9afa3de2 2023-04-04 stsp conf_protect_branch(struct gotd_repo *repo, char *branchname)
966 9afa3de2 2023-04-04 stsp {
967 9afa3de2 2023-04-04 stsp const struct got_error *error;
968 f0426190 2023-04-05 op struct got_pathlist_entry *new;
969 9afa3de2 2023-04-04 stsp char *refname;
970 9afa3de2 2023-04-04 stsp
971 9afa3de2 2023-04-04 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
972 9afa3de2 2023-04-04 stsp if (asprintf(&refname, "refs/heads/%s", branchname) == -1) {
973 9afa3de2 2023-04-04 stsp yyerror("asprintf: %s", strerror(errno));
974 9afa3de2 2023-04-04 stsp return -1;
975 9afa3de2 2023-04-04 stsp }
976 9afa3de2 2023-04-04 stsp } else {
977 9afa3de2 2023-04-04 stsp refname = strdup(branchname);
978 9afa3de2 2023-04-04 stsp if (refname == NULL) {
979 9afa3de2 2023-04-04 stsp yyerror("strdup: %s", strerror(errno));
980 9afa3de2 2023-04-04 stsp return -1;
981 9afa3de2 2023-04-04 stsp }
982 9afa3de2 2023-04-04 stsp }
983 9afa3de2 2023-04-04 stsp
984 9afa3de2 2023-04-04 stsp if (!refname_is_valid(refname)) {
985 9afa3de2 2023-04-04 stsp free(refname);
986 9afa3de2 2023-04-04 stsp return -1;
987 9afa3de2 2023-04-04 stsp }
988 9afa3de2 2023-04-04 stsp
989 f0426190 2023-04-05 op error = got_pathlist_insert(&new, &repo->protected_branches,
990 9afa3de2 2023-04-04 stsp refname, NULL);
991 f0426190 2023-04-05 op if (error || new == NULL) {
992 f0426190 2023-04-05 op free(refname);
993 f0426190 2023-04-05 op if (error)
994 f0426190 2023-04-05 op yyerror("got_pathlist_insert: %s", error->msg);
995 f0426190 2023-04-05 op else
996 f0426190 2023-04-05 op yyerror("duplicate protect branch %s", branchname);
997 9afa3de2 2023-04-04 stsp return -1;
998 9afa3de2 2023-04-04 stsp }
999 9afa3de2 2023-04-04 stsp
1000 9afa3de2 2023-04-04 stsp return 0;
1001 9afa3de2 2023-04-04 stsp }
1002 9afa3de2 2023-04-04 stsp
1003 13b2bc37 2022-10-23 stsp int
1004 13b2bc37 2022-10-23 stsp symset(const char *nam, const char *val, int persist)
1005 13b2bc37 2022-10-23 stsp {
1006 13b2bc37 2022-10-23 stsp struct sym *sym;
1007 13b2bc37 2022-10-23 stsp
1008 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1009 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0)
1010 13b2bc37 2022-10-23 stsp break;
1011 13b2bc37 2022-10-23 stsp }
1012 13b2bc37 2022-10-23 stsp
1013 13b2bc37 2022-10-23 stsp if (sym != NULL) {
1014 13b2bc37 2022-10-23 stsp if (sym->persist == 1)
1015 13b2bc37 2022-10-23 stsp return (0);
1016 13b2bc37 2022-10-23 stsp else {
1017 13b2bc37 2022-10-23 stsp free(sym->nam);
1018 13b2bc37 2022-10-23 stsp free(sym->val);
1019 13b2bc37 2022-10-23 stsp TAILQ_REMOVE(&symhead, sym, entry);
1020 13b2bc37 2022-10-23 stsp free(sym);
1021 13b2bc37 2022-10-23 stsp }
1022 13b2bc37 2022-10-23 stsp }
1023 13b2bc37 2022-10-23 stsp sym = calloc(1, sizeof(*sym));
1024 13b2bc37 2022-10-23 stsp if (sym == NULL)
1025 13b2bc37 2022-10-23 stsp return (-1);
1026 13b2bc37 2022-10-23 stsp
1027 13b2bc37 2022-10-23 stsp sym->nam = strdup(nam);
1028 13b2bc37 2022-10-23 stsp if (sym->nam == NULL) {
1029 13b2bc37 2022-10-23 stsp free(sym);
1030 13b2bc37 2022-10-23 stsp return (-1);
1031 13b2bc37 2022-10-23 stsp }
1032 13b2bc37 2022-10-23 stsp sym->val = strdup(val);
1033 13b2bc37 2022-10-23 stsp if (sym->val == NULL) {
1034 13b2bc37 2022-10-23 stsp free(sym->nam);
1035 13b2bc37 2022-10-23 stsp free(sym);
1036 13b2bc37 2022-10-23 stsp return (-1);
1037 13b2bc37 2022-10-23 stsp }
1038 13b2bc37 2022-10-23 stsp sym->used = 0;
1039 13b2bc37 2022-10-23 stsp sym->persist = persist;
1040 13b2bc37 2022-10-23 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
1041 13b2bc37 2022-10-23 stsp return (0);
1042 13b2bc37 2022-10-23 stsp }
1043 13b2bc37 2022-10-23 stsp
1044 13b2bc37 2022-10-23 stsp char *
1045 13b2bc37 2022-10-23 stsp symget(const char *nam)
1046 13b2bc37 2022-10-23 stsp {
1047 13b2bc37 2022-10-23 stsp struct sym *sym;
1048 13b2bc37 2022-10-23 stsp
1049 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(sym, &symhead, entry) {
1050 13b2bc37 2022-10-23 stsp if (strcmp(nam, sym->nam) == 0) {
1051 13b2bc37 2022-10-23 stsp sym->used = 1;
1052 13b2bc37 2022-10-23 stsp return (sym->val);
1053 13b2bc37 2022-10-23 stsp }
1054 13b2bc37 2022-10-23 stsp }
1055 13b2bc37 2022-10-23 stsp return (NULL);
1056 b09c1279 2023-03-28 stsp }
1057 b09c1279 2023-03-28 stsp
1058 b09c1279 2023-03-28 stsp struct gotd_repo *
1059 b09c1279 2023-03-28 stsp gotd_find_repo_by_name(const char *repo_name, struct gotd *gotd)
1060 b09c1279 2023-03-28 stsp {
1061 b09c1279 2023-03-28 stsp struct gotd_repo *repo;
1062 b09c1279 2023-03-28 stsp size_t namelen;
1063 b09c1279 2023-03-28 stsp
1064 b09c1279 2023-03-28 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1065 b09c1279 2023-03-28 stsp namelen = strlen(repo->name);
1066 b09c1279 2023-03-28 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
1067 b09c1279 2023-03-28 stsp continue;
1068 b09c1279 2023-03-28 stsp if (repo_name[namelen] == '\0' ||
1069 b09c1279 2023-03-28 stsp strcmp(&repo_name[namelen], ".git") == 0)
1070 b09c1279 2023-03-28 stsp return repo;
1071 b09c1279 2023-03-28 stsp }
1072 b09c1279 2023-03-28 stsp
1073 b09c1279 2023-03-28 stsp return NULL;
1074 13b2bc37 2022-10-23 stsp }
1075 9afa3de2 2023-04-04 stsp
1076 9afa3de2 2023-04-04 stsp struct gotd_repo *
1077 9afa3de2 2023-04-04 stsp gotd_find_repo_by_path(const char *repo_path, struct gotd *gotd)
1078 9afa3de2 2023-04-04 stsp {
1079 9afa3de2 2023-04-04 stsp struct gotd_repo *repo;
1080 9afa3de2 2023-04-04 stsp
1081 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(repo, &gotd->repos, entry) {
1082 9afa3de2 2023-04-04 stsp if (strcmp(repo->path, repo_path) == 0)
1083 9afa3de2 2023-04-04 stsp return repo;
1084 9afa3de2 2023-04-04 stsp }
1085 9afa3de2 2023-04-04 stsp
1086 9afa3de2 2023-04-04 stsp return NULL;
1087 9afa3de2 2023-04-04 stsp }