Blame


1 257add31 2020-09-09 stsp /*
2 257add31 2020-09-09 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 257add31 2020-09-09 stsp *
4 257add31 2020-09-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 257add31 2020-09-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 257add31 2020-09-09 stsp * copyright notice and this permission notice appear in all copies.
7 257add31 2020-09-09 stsp *
8 257add31 2020-09-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 257add31 2020-09-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 257add31 2020-09-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 257add31 2020-09-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 257add31 2020-09-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 257add31 2020-09-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 257add31 2020-09-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 257add31 2020-09-09 stsp */
16 257add31 2020-09-09 stsp
17 257add31 2020-09-09 stsp #include <sys/types.h>
18 257add31 2020-09-09 stsp #include <sys/uio.h>
19 257add31 2020-09-09 stsp #include <sys/time.h>
20 257add31 2020-09-09 stsp
21 257add31 2020-09-09 stsp #include <stdint.h>
22 257add31 2020-09-09 stsp #include <limits.h>
23 257add31 2020-09-09 stsp #include <signal.h>
24 257add31 2020-09-09 stsp #include <stdio.h>
25 257add31 2020-09-09 stsp #include <stdlib.h>
26 257add31 2020-09-09 stsp #include <string.h>
27 e12e0e21 2020-09-14 naddy #include <unistd.h>
28 257add31 2020-09-09 stsp #include <zlib.h>
29 dd038bc6 2021-09-21 thomas.ad
30 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
31 257add31 2020-09-09 stsp
32 257add31 2020-09-09 stsp #include "got_error.h"
33 257add31 2020-09-09 stsp #include "got_object.h"
34 5e082626 2020-09-24 stsp #include "got_path.h"
35 257add31 2020-09-09 stsp #include "got_repository.h"
36 257add31 2020-09-09 stsp
37 257add31 2020-09-09 stsp #include "got_lib_delta.h"
38 257add31 2020-09-09 stsp #include "got_lib_object.h"
39 257add31 2020-09-09 stsp #include "got_lib_privsep.h"
40 257add31 2020-09-09 stsp
41 257add31 2020-09-09 stsp #include "gotconfig.h"
42 257add31 2020-09-09 stsp
43 257add31 2020-09-09 stsp /* parse.y */
44 257add31 2020-09-09 stsp static volatile sig_atomic_t sigint_received;
45 257add31 2020-09-09 stsp
46 257add31 2020-09-09 stsp static void
47 257add31 2020-09-09 stsp catch_sigint(int signo)
48 257add31 2020-09-09 stsp {
49 257add31 2020-09-09 stsp sigint_received = 1;
50 257add31 2020-09-09 stsp }
51 257add31 2020-09-09 stsp
52 257add31 2020-09-09 stsp static const struct got_error *
53 6480c871 2021-08-30 stsp make_fetch_url(char **url, struct gotconfig_remote_repo *repo)
54 257add31 2020-09-09 stsp {
55 257add31 2020-09-09 stsp const struct got_error *err = NULL;
56 257add31 2020-09-09 stsp char *s = NULL, *p = NULL;
57 6480c871 2021-08-30 stsp const char *protocol, *server, *repo_path;
58 6480c871 2021-08-30 stsp int port;
59 257add31 2020-09-09 stsp
60 257add31 2020-09-09 stsp *url = NULL;
61 257add31 2020-09-09 stsp
62 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->protocol)
63 6480c871 2021-08-30 stsp protocol = repo->fetch_config->protocol;
64 6480c871 2021-08-30 stsp else
65 6480c871 2021-08-30 stsp protocol = repo->protocol;
66 6480c871 2021-08-30 stsp if (protocol == NULL)
67 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
68 6480c871 2021-08-30 stsp "fetch protocol required for remote repository \"%s\"",
69 6480c871 2021-08-30 stsp repo->name);
70 6480c871 2021-08-30 stsp if (asprintf(&s, "%s://", protocol) == -1)
71 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
72 257add31 2020-09-09 stsp
73 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->server)
74 6480c871 2021-08-30 stsp server = repo->fetch_config->server;
75 6480c871 2021-08-30 stsp else
76 6480c871 2021-08-30 stsp server = repo->server;
77 6480c871 2021-08-30 stsp if (server == NULL)
78 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
79 6480c871 2021-08-30 stsp "fetch server required for remote repository \"%s\"",
80 6480c871 2021-08-30 stsp repo->name);
81 6480c871 2021-08-30 stsp p = s;
82 6480c871 2021-08-30 stsp s = NULL;
83 6480c871 2021-08-30 stsp if (asprintf(&s, "%s%s", p, server) == -1) {
84 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
85 6480c871 2021-08-30 stsp goto done;
86 257add31 2020-09-09 stsp }
87 6480c871 2021-08-30 stsp free(p);
88 6480c871 2021-08-30 stsp p = NULL;
89 257add31 2020-09-09 stsp
90 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->server)
91 6480c871 2021-08-30 stsp port = repo->fetch_config->port;
92 6480c871 2021-08-30 stsp else
93 6480c871 2021-08-30 stsp port = repo->port;
94 6480c871 2021-08-30 stsp if (port) {
95 257add31 2020-09-09 stsp p = s;
96 257add31 2020-09-09 stsp s = NULL;
97 257add31 2020-09-09 stsp if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
98 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
99 257add31 2020-09-09 stsp goto done;
100 257add31 2020-09-09 stsp }
101 257add31 2020-09-09 stsp free(p);
102 257add31 2020-09-09 stsp p = NULL;
103 257add31 2020-09-09 stsp }
104 257add31 2020-09-09 stsp
105 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->repository)
106 6480c871 2021-08-30 stsp repo_path = repo->fetch_config->repository;
107 6480c871 2021-08-30 stsp else
108 6480c871 2021-08-30 stsp repo_path = repo->repository;
109 6480c871 2021-08-30 stsp if (repo_path == NULL)
110 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
111 6480c871 2021-08-30 stsp "fetch repository path required for remote "
112 6480c871 2021-08-30 stsp "repository \"%s\"", repo->name);
113 6480c871 2021-08-30 stsp
114 6480c871 2021-08-30 stsp while (repo_path[0] == '/')
115 6480c871 2021-08-30 stsp repo_path++;
116 6480c871 2021-08-30 stsp p = s;
117 6480c871 2021-08-30 stsp s = NULL;
118 6480c871 2021-08-30 stsp if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
119 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
120 6480c871 2021-08-30 stsp goto done;
121 6480c871 2021-08-30 stsp }
122 6480c871 2021-08-30 stsp free(p);
123 6480c871 2021-08-30 stsp p = NULL;
124 6480c871 2021-08-30 stsp
125 6480c871 2021-08-30 stsp got_path_strip_trailing_slashes(s);
126 6480c871 2021-08-30 stsp done:
127 6480c871 2021-08-30 stsp if (err) {
128 6480c871 2021-08-30 stsp free(s);
129 6480c871 2021-08-30 stsp free(p);
130 6480c871 2021-08-30 stsp } else
131 6480c871 2021-08-30 stsp *url = s;
132 6480c871 2021-08-30 stsp return err;
133 6480c871 2021-08-30 stsp }
134 6480c871 2021-08-30 stsp
135 6480c871 2021-08-30 stsp static const struct got_error *
136 6480c871 2021-08-30 stsp make_send_url(char **url, struct gotconfig_remote_repo *repo)
137 6480c871 2021-08-30 stsp {
138 6480c871 2021-08-30 stsp const struct got_error *err = NULL;
139 6480c871 2021-08-30 stsp char *s = NULL, *p = NULL;
140 6480c871 2021-08-30 stsp const char *protocol, *server, *repo_path;
141 6480c871 2021-08-30 stsp int port;
142 6480c871 2021-08-30 stsp
143 6480c871 2021-08-30 stsp *url = NULL;
144 6480c871 2021-08-30 stsp
145 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->protocol)
146 6480c871 2021-08-30 stsp protocol = repo->send_config->protocol;
147 6480c871 2021-08-30 stsp else
148 6480c871 2021-08-30 stsp protocol = repo->protocol;
149 6480c871 2021-08-30 stsp if (protocol == NULL)
150 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
151 6480c871 2021-08-30 stsp "send protocol required for remote repository \"%s\"",
152 6480c871 2021-08-30 stsp repo->name);
153 6480c871 2021-08-30 stsp if (asprintf(&s, "%s://", protocol) == -1)
154 6480c871 2021-08-30 stsp return got_error_from_errno("asprintf");
155 6480c871 2021-08-30 stsp
156 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->server)
157 6480c871 2021-08-30 stsp server = repo->send_config->server;
158 6480c871 2021-08-30 stsp else
159 6480c871 2021-08-30 stsp server = repo->server;
160 6480c871 2021-08-30 stsp if (server == NULL)
161 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
162 6480c871 2021-08-30 stsp "send server required for remote repository \"%s\"",
163 6480c871 2021-08-30 stsp repo->name);
164 6480c871 2021-08-30 stsp p = s;
165 6480c871 2021-08-30 stsp s = NULL;
166 6480c871 2021-08-30 stsp if (asprintf(&s, "%s%s", p, server) == -1) {
167 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
168 6480c871 2021-08-30 stsp goto done;
169 6480c871 2021-08-30 stsp }
170 6480c871 2021-08-30 stsp free(p);
171 6480c871 2021-08-30 stsp p = NULL;
172 6480c871 2021-08-30 stsp
173 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->server)
174 6480c871 2021-08-30 stsp port = repo->send_config->port;
175 6480c871 2021-08-30 stsp else
176 6480c871 2021-08-30 stsp port = repo->port;
177 6480c871 2021-08-30 stsp if (port) {
178 257add31 2020-09-09 stsp p = s;
179 257add31 2020-09-09 stsp s = NULL;
180 6480c871 2021-08-30 stsp if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
181 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
182 257add31 2020-09-09 stsp goto done;
183 257add31 2020-09-09 stsp }
184 257add31 2020-09-09 stsp free(p);
185 257add31 2020-09-09 stsp p = NULL;
186 257add31 2020-09-09 stsp }
187 5e082626 2020-09-24 stsp
188 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->repository)
189 6480c871 2021-08-30 stsp repo_path = repo->send_config->repository;
190 6480c871 2021-08-30 stsp else
191 6480c871 2021-08-30 stsp repo_path = repo->repository;
192 6480c871 2021-08-30 stsp if (repo_path == NULL)
193 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
194 6480c871 2021-08-30 stsp "send repository path required for remote "
195 6480c871 2021-08-30 stsp "repository \"%s\"", repo->name);
196 6480c871 2021-08-30 stsp
197 6480c871 2021-08-30 stsp while (repo_path[0] == '/')
198 6480c871 2021-08-30 stsp repo_path++;
199 6480c871 2021-08-30 stsp p = s;
200 6480c871 2021-08-30 stsp s = NULL;
201 6480c871 2021-08-30 stsp if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
202 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
203 6480c871 2021-08-30 stsp goto done;
204 6480c871 2021-08-30 stsp }
205 6480c871 2021-08-30 stsp free(p);
206 6480c871 2021-08-30 stsp p = NULL;
207 6480c871 2021-08-30 stsp
208 5e082626 2020-09-24 stsp got_path_strip_trailing_slashes(s);
209 257add31 2020-09-09 stsp done:
210 257add31 2020-09-09 stsp if (err) {
211 257add31 2020-09-09 stsp free(s);
212 257add31 2020-09-09 stsp free(p);
213 257add31 2020-09-09 stsp } else
214 257add31 2020-09-09 stsp *url = s;
215 257add31 2020-09-09 stsp return err;
216 257add31 2020-09-09 stsp }
217 257add31 2020-09-09 stsp
218 257add31 2020-09-09 stsp static const struct got_error *
219 257add31 2020-09-09 stsp send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
220 257add31 2020-09-09 stsp {
221 be96c417 2020-09-17 stsp size_t len = value ? strlen(value) : 0;
222 257add31 2020-09-09 stsp
223 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
224 257add31 2020-09-09 stsp value, len) == -1)
225 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
226 257add31 2020-09-09 stsp
227 257add31 2020-09-09 stsp return got_privsep_flush_imsg(ibuf);
228 257add31 2020-09-09 stsp }
229 257add31 2020-09-09 stsp
230 257add31 2020-09-09 stsp static const struct got_error *
231 257add31 2020-09-09 stsp send_gotconfig_remotes(struct imsgbuf *ibuf,
232 257add31 2020-09-09 stsp struct gotconfig_remote_repo_list *remotes, int nremotes)
233 257add31 2020-09-09 stsp {
234 257add31 2020-09-09 stsp const struct got_error *err = NULL;
235 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
236 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo;
237 6480c871 2021-08-30 stsp char *fetch_url = NULL, *send_url = NULL;
238 257add31 2020-09-09 stsp
239 257add31 2020-09-09 stsp iremotes.nremotes = nremotes;
240 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
241 257add31 2020-09-09 stsp &iremotes, sizeof(iremotes)) == -1)
242 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
243 257add31 2020-09-09 stsp
244 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
245 257add31 2020-09-09 stsp imsg_clear(ibuf);
246 257add31 2020-09-09 stsp if (err)
247 257add31 2020-09-09 stsp return err;
248 257add31 2020-09-09 stsp
249 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, remotes, entry) {
250 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
251 257add31 2020-09-09 stsp size_t len = sizeof(iremote);
252 257add31 2020-09-09 stsp struct ibuf *wbuf;
253 b8adfa55 2020-09-25 stsp struct node_branch *branch;
254 99495ddb 2021-01-10 stsp struct node_ref *ref;
255 6480c871 2021-08-30 stsp int nfetch_branches = 0, nsend_branches = 0, nfetch_refs = 0;
256 257add31 2020-09-09 stsp
257 93f8a337 2021-08-30 naddy if (repo->fetch_config && repo->fetch_config->branch)
258 6480c871 2021-08-30 stsp branch = repo->fetch_config->branch;
259 93f8a337 2021-08-30 naddy else
260 6480c871 2021-08-30 stsp branch = repo->branch;
261 93f8a337 2021-08-30 naddy while (branch) {
262 93f8a337 2021-08-30 naddy branch = branch->next;
263 93f8a337 2021-08-30 naddy nfetch_branches++;
264 99495ddb 2021-01-10 stsp }
265 99495ddb 2021-01-10 stsp
266 93f8a337 2021-08-30 naddy if (repo->send_config && repo->send_config->branch)
267 6480c871 2021-08-30 stsp branch = repo->send_config->branch;
268 93f8a337 2021-08-30 naddy else
269 6480c871 2021-08-30 stsp branch = repo->branch;
270 93f8a337 2021-08-30 naddy while (branch) {
271 93f8a337 2021-08-30 naddy branch = branch->next;
272 93f8a337 2021-08-30 naddy nsend_branches++;
273 6480c871 2021-08-30 stsp }
274 6480c871 2021-08-30 stsp
275 6480c871 2021-08-30 stsp ref = repo->fetch_ref;
276 99495ddb 2021-01-10 stsp while (ref) {
277 99495ddb 2021-01-10 stsp ref = ref->next;
278 6480c871 2021-08-30 stsp nfetch_refs++;
279 b8adfa55 2020-09-25 stsp }
280 b8adfa55 2020-09-25 stsp
281 6480c871 2021-08-30 stsp iremote.nfetch_branches = nfetch_branches;
282 6480c871 2021-08-30 stsp iremote.nsend_branches = nsend_branches;
283 6480c871 2021-08-30 stsp iremote.nfetch_refs = nfetch_refs;
284 257add31 2020-09-09 stsp iremote.mirror_references = repo->mirror_references;
285 0c8b29c5 2021-01-05 stsp iremote.fetch_all_branches = repo->fetch_all_branches;
286 257add31 2020-09-09 stsp
287 257add31 2020-09-09 stsp iremote.name_len = strlen(repo->name);
288 257add31 2020-09-09 stsp len += iremote.name_len;
289 257add31 2020-09-09 stsp
290 6480c871 2021-08-30 stsp err = make_fetch_url(&fetch_url, repo);
291 257add31 2020-09-09 stsp if (err)
292 257add31 2020-09-09 stsp break;
293 6480c871 2021-08-30 stsp iremote.fetch_url_len = strlen(fetch_url);
294 6480c871 2021-08-30 stsp len += iremote.fetch_url_len;
295 6480c871 2021-08-30 stsp
296 6480c871 2021-08-30 stsp err = make_send_url(&send_url, repo);
297 6480c871 2021-08-30 stsp if (err)
298 6480c871 2021-08-30 stsp break;
299 6480c871 2021-08-30 stsp iremote.send_url_len = strlen(send_url);
300 6480c871 2021-08-30 stsp len += iremote.send_url_len;
301 257add31 2020-09-09 stsp
302 257add31 2020-09-09 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
303 257add31 2020-09-09 stsp if (wbuf == NULL) {
304 257add31 2020-09-09 stsp err = got_error_from_errno(
305 257add31 2020-09-09 stsp "imsg_create GOTCONFIG_REMOTE");
306 257add31 2020-09-09 stsp break;
307 257add31 2020-09-09 stsp }
308 257add31 2020-09-09 stsp
309 257add31 2020-09-09 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
310 257add31 2020-09-09 stsp err = got_error_from_errno(
311 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
312 257add31 2020-09-09 stsp break;
313 257add31 2020-09-09 stsp }
314 257add31 2020-09-09 stsp
315 257add31 2020-09-09 stsp if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
316 257add31 2020-09-09 stsp err = got_error_from_errno(
317 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
318 257add31 2020-09-09 stsp break;
319 257add31 2020-09-09 stsp }
320 6480c871 2021-08-30 stsp if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
321 257add31 2020-09-09 stsp err = got_error_from_errno(
322 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
323 257add31 2020-09-09 stsp break;
324 257add31 2020-09-09 stsp }
325 6480c871 2021-08-30 stsp if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
326 6480c871 2021-08-30 stsp err = got_error_from_errno(
327 6480c871 2021-08-30 stsp "imsg_add GOTCONFIG_REMOTE");
328 6480c871 2021-08-30 stsp break;
329 6480c871 2021-08-30 stsp }
330 257add31 2020-09-09 stsp
331 257add31 2020-09-09 stsp wbuf->fd = -1;
332 257add31 2020-09-09 stsp imsg_close(ibuf, wbuf);
333 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
334 257add31 2020-09-09 stsp if (err)
335 257add31 2020-09-09 stsp break;
336 257add31 2020-09-09 stsp
337 6480c871 2021-08-30 stsp free(fetch_url);
338 6480c871 2021-08-30 stsp fetch_url = NULL;
339 6480c871 2021-08-30 stsp free(send_url);
340 6480c871 2021-08-30 stsp send_url = NULL;
341 b8adfa55 2020-09-25 stsp
342 93f8a337 2021-08-30 naddy if (repo->fetch_config && repo->fetch_config->branch)
343 6480c871 2021-08-30 stsp branch = repo->fetch_config->branch;
344 93f8a337 2021-08-30 naddy else
345 6480c871 2021-08-30 stsp branch = repo->branch;
346 93f8a337 2021-08-30 naddy while (branch) {
347 93f8a337 2021-08-30 naddy err = send_gotconfig_str(ibuf, branch->branch_name);
348 93f8a337 2021-08-30 naddy if (err)
349 93f8a337 2021-08-30 naddy break;
350 93f8a337 2021-08-30 naddy branch = branch->next;
351 b8adfa55 2020-09-25 stsp }
352 99495ddb 2021-01-10 stsp
353 93f8a337 2021-08-30 naddy if (repo->send_config && repo->send_config->branch)
354 6480c871 2021-08-30 stsp branch = repo->send_config->branch;
355 93f8a337 2021-08-30 naddy else
356 6480c871 2021-08-30 stsp branch = repo->branch;
357 93f8a337 2021-08-30 naddy while (branch) {
358 93f8a337 2021-08-30 naddy err = send_gotconfig_str(ibuf, branch->branch_name);
359 93f8a337 2021-08-30 naddy if (err)
360 93f8a337 2021-08-30 naddy break;
361 93f8a337 2021-08-30 naddy branch = branch->next;
362 6480c871 2021-08-30 stsp }
363 6480c871 2021-08-30 stsp
364 6480c871 2021-08-30 stsp ref = repo->fetch_ref;
365 99495ddb 2021-01-10 stsp while (ref) {
366 99495ddb 2021-01-10 stsp err = send_gotconfig_str(ibuf, ref->ref_name);
367 99495ddb 2021-01-10 stsp if (err)
368 99495ddb 2021-01-10 stsp break;
369 99495ddb 2021-01-10 stsp ref = ref->next;
370 99495ddb 2021-01-10 stsp }
371 257add31 2020-09-09 stsp }
372 257add31 2020-09-09 stsp
373 6480c871 2021-08-30 stsp free(fetch_url);
374 6480c871 2021-08-30 stsp free(send_url);
375 257add31 2020-09-09 stsp return err;
376 257add31 2020-09-09 stsp }
377 257add31 2020-09-09 stsp
378 257add31 2020-09-09 stsp static const struct got_error *
379 f1cacac7 2021-08-29 stsp validate_protocol(const char *protocol, const char *repo_name)
380 f1cacac7 2021-08-29 stsp {
381 f1cacac7 2021-08-29 stsp static char msg[512];
382 f1cacac7 2021-08-29 stsp
383 f1cacac7 2021-08-29 stsp if (strcmp(protocol, "ssh") != 0 &&
384 f1cacac7 2021-08-29 stsp strcmp(protocol, "git+ssh") != 0 &&
385 f1cacac7 2021-08-29 stsp strcmp(protocol, "git") != 0) {
386 f1cacac7 2021-08-29 stsp snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
387 f1cacac7 2021-08-29 stsp "for remote repository \"%s\"", protocol, repo_name);
388 f1cacac7 2021-08-29 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
389 f1cacac7 2021-08-29 stsp }
390 f1cacac7 2021-08-29 stsp
391 f1cacac7 2021-08-29 stsp return NULL;
392 f1cacac7 2021-08-29 stsp }
393 f1cacac7 2021-08-29 stsp
394 f1cacac7 2021-08-29 stsp static const struct got_error *
395 257add31 2020-09-09 stsp validate_config(struct gotconfig *gotconfig)
396 257add31 2020-09-09 stsp {
397 f1cacac7 2021-08-29 stsp const struct got_error *err;
398 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo, *repo2;
399 257add31 2020-09-09 stsp static char msg[512];
400 257add31 2020-09-09 stsp
401 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
402 257add31 2020-09-09 stsp if (repo->name == NULL) {
403 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG,
404 257add31 2020-09-09 stsp "name required for remote repository");
405 257add31 2020-09-09 stsp }
406 257add31 2020-09-09 stsp
407 257add31 2020-09-09 stsp TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
408 257add31 2020-09-09 stsp if (repo == repo2 ||
409 257add31 2020-09-09 stsp strcmp(repo->name, repo2->name) != 0)
410 257add31 2020-09-09 stsp continue;
411 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
412 257add31 2020-09-09 stsp "duplicate remote repository name '%s'",
413 257add31 2020-09-09 stsp repo->name);
414 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
415 257add31 2020-09-09 stsp }
416 257add31 2020-09-09 stsp
417 f1cacac7 2021-08-29 stsp if (repo->server == NULL &&
418 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
419 f1cacac7 2021-08-29 stsp repo->fetch_config->server == NULL) &&
420 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
421 f1cacac7 2021-08-29 stsp repo->send_config->server == NULL)) {
422 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
423 257add31 2020-09-09 stsp "server required for remote repository \"%s\"",
424 257add31 2020-09-09 stsp repo->name);
425 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
426 257add31 2020-09-09 stsp }
427 257add31 2020-09-09 stsp
428 f1cacac7 2021-08-29 stsp if (repo->protocol == NULL &&
429 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
430 f1cacac7 2021-08-29 stsp repo->fetch_config->protocol == NULL) &&
431 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
432 f1cacac7 2021-08-29 stsp repo->send_config->protocol == NULL)) {
433 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
434 257add31 2020-09-09 stsp "protocol required for remote repository \"%s\"",
435 257add31 2020-09-09 stsp repo->name);
436 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
437 257add31 2020-09-09 stsp }
438 f1cacac7 2021-08-29 stsp
439 f1cacac7 2021-08-29 stsp if (repo->protocol) {
440 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->protocol, repo->name);
441 f1cacac7 2021-08-29 stsp if (err)
442 f1cacac7 2021-08-29 stsp return err;
443 f1cacac7 2021-08-29 stsp }
444 f1cacac7 2021-08-29 stsp if (repo->fetch_config && repo->fetch_config->protocol) {
445 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->fetch_config->protocol,
446 257add31 2020-09-09 stsp repo->name);
447 f1cacac7 2021-08-29 stsp if (err)
448 f1cacac7 2021-08-29 stsp return err;
449 257add31 2020-09-09 stsp }
450 f1cacac7 2021-08-29 stsp if (repo->send_config && repo->send_config->protocol) {
451 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->send_config->protocol,
452 f1cacac7 2021-08-29 stsp repo->name);
453 f1cacac7 2021-08-29 stsp if (err)
454 f1cacac7 2021-08-29 stsp return err;
455 f1cacac7 2021-08-29 stsp }
456 257add31 2020-09-09 stsp
457 f1cacac7 2021-08-29 stsp if (repo->repository == NULL &&
458 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
459 f1cacac7 2021-08-29 stsp repo->fetch_config->repository == NULL) &&
460 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
461 f1cacac7 2021-08-29 stsp repo->send_config->repository == NULL)) {
462 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
463 257add31 2020-09-09 stsp "repository path required for remote "
464 257add31 2020-09-09 stsp "repository \"%s\"", repo->name);
465 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
466 257add31 2020-09-09 stsp }
467 257add31 2020-09-09 stsp }
468 257add31 2020-09-09 stsp
469 257add31 2020-09-09 stsp return NULL;
470 257add31 2020-09-09 stsp }
471 257add31 2020-09-09 stsp
472 257add31 2020-09-09 stsp int
473 257add31 2020-09-09 stsp main(int argc, char *argv[])
474 257add31 2020-09-09 stsp {
475 257add31 2020-09-09 stsp const struct got_error *err = NULL;
476 257add31 2020-09-09 stsp struct imsgbuf ibuf;
477 53dfa00d 2020-09-10 stsp struct gotconfig *gotconfig = NULL;
478 257add31 2020-09-09 stsp size_t datalen;
479 257add31 2020-09-09 stsp const char *filename = "got.conf";
480 257add31 2020-09-09 stsp #if 0
481 257add31 2020-09-09 stsp static int attached;
482 257add31 2020-09-09 stsp
483 257add31 2020-09-09 stsp while (!attached)
484 257add31 2020-09-09 stsp sleep(1);
485 257add31 2020-09-09 stsp #endif
486 257add31 2020-09-09 stsp signal(SIGINT, catch_sigint);
487 257add31 2020-09-09 stsp
488 257add31 2020-09-09 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
489 257add31 2020-09-09 stsp
490 257add31 2020-09-09 stsp #ifndef PROFILE
491 257add31 2020-09-09 stsp /* revoke access to most system calls */
492 257add31 2020-09-09 stsp if (pledge("stdio recvfd", NULL) == -1) {
493 257add31 2020-09-09 stsp err = got_error_from_errno("pledge");
494 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
495 257add31 2020-09-09 stsp return 1;
496 257add31 2020-09-09 stsp }
497 97799ccd 2022-02-06 thomas
498 97799ccd 2022-02-06 thomas /* revoke fs access */
499 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
500 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
501 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
502 97799ccd 2022-02-06 thomas return 1;
503 97799ccd 2022-02-06 thomas }
504 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
505 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
506 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
507 5d120ea8 2022-06-23 op return 1;
508 5d120ea8 2022-06-23 op }
509 257add31 2020-09-09 stsp #endif
510 257add31 2020-09-09 stsp
511 257add31 2020-09-09 stsp if (argc > 1)
512 257add31 2020-09-09 stsp filename = argv[1];
513 257add31 2020-09-09 stsp
514 257add31 2020-09-09 stsp for (;;) {
515 257add31 2020-09-09 stsp struct imsg imsg;
516 257add31 2020-09-09 stsp
517 257add31 2020-09-09 stsp memset(&imsg, 0, sizeof(imsg));
518 257add31 2020-09-09 stsp imsg.fd = -1;
519 257add31 2020-09-09 stsp
520 257add31 2020-09-09 stsp if (sigint_received) {
521 257add31 2020-09-09 stsp err = got_error(GOT_ERR_CANCELLED);
522 257add31 2020-09-09 stsp break;
523 257add31 2020-09-09 stsp }
524 257add31 2020-09-09 stsp
525 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
526 257add31 2020-09-09 stsp if (err) {
527 257add31 2020-09-09 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
528 257add31 2020-09-09 stsp err = NULL;
529 257add31 2020-09-09 stsp break;
530 257add31 2020-09-09 stsp }
531 257add31 2020-09-09 stsp
532 257add31 2020-09-09 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
533 257add31 2020-09-09 stsp break;
534 257add31 2020-09-09 stsp
535 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
536 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
537 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
538 257add31 2020-09-09 stsp if (datalen != 0) {
539 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
540 257add31 2020-09-09 stsp break;
541 257add31 2020-09-09 stsp }
542 257add31 2020-09-09 stsp if (imsg.fd == -1){
543 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
544 257add31 2020-09-09 stsp break;
545 257add31 2020-09-09 stsp }
546 257add31 2020-09-09 stsp
547 257add31 2020-09-09 stsp if (gotconfig)
548 257add31 2020-09-09 stsp gotconfig_free(gotconfig);
549 257add31 2020-09-09 stsp err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
550 257add31 2020-09-09 stsp if (err)
551 257add31 2020-09-09 stsp break;
552 257add31 2020-09-09 stsp err = validate_config(gotconfig);
553 257add31 2020-09-09 stsp break;
554 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
555 257add31 2020-09-09 stsp if (gotconfig == NULL) {
556 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
557 257add31 2020-09-09 stsp break;
558 257add31 2020-09-09 stsp }
559 257add31 2020-09-09 stsp err = send_gotconfig_str(&ibuf,
560 257add31 2020-09-09 stsp gotconfig->author ? gotconfig->author : "");
561 871bd038 2022-07-03 thomas break;
562 871bd038 2022-07-03 thomas case GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST:
563 871bd038 2022-07-03 thomas if (gotconfig == NULL) {
564 871bd038 2022-07-03 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
565 871bd038 2022-07-03 thomas break;
566 871bd038 2022-07-03 thomas }
567 871bd038 2022-07-03 thomas err = send_gotconfig_str(&ibuf,
568 871bd038 2022-07-03 thomas gotconfig->allowed_signers_file ?
569 871bd038 2022-07-03 thomas gotconfig->allowed_signers_file : "");
570 871bd038 2022-07-03 thomas break;
571 871bd038 2022-07-03 thomas case GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST:
572 871bd038 2022-07-03 thomas if (gotconfig == NULL) {
573 871bd038 2022-07-03 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
574 871bd038 2022-07-03 thomas break;
575 871bd038 2022-07-03 thomas }
576 871bd038 2022-07-03 thomas err = send_gotconfig_str(&ibuf,
577 871bd038 2022-07-03 thomas gotconfig->revoked_signers_file ?
578 871bd038 2022-07-03 thomas gotconfig->revoked_signers_file : "");
579 ff5e1f09 2022-07-06 thomas break;
580 ff5e1f09 2022-07-06 thomas case GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST:
581 ff5e1f09 2022-07-06 thomas if (gotconfig == NULL) {
582 ff5e1f09 2022-07-06 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
583 ff5e1f09 2022-07-06 thomas break;
584 ff5e1f09 2022-07-06 thomas }
585 ff5e1f09 2022-07-06 thomas err = send_gotconfig_str(&ibuf,
586 ff5e1f09 2022-07-06 thomas gotconfig->signer_id ? gotconfig->signer_id : "");
587 257add31 2020-09-09 stsp break;
588 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
589 257add31 2020-09-09 stsp if (gotconfig == NULL) {
590 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
591 257add31 2020-09-09 stsp break;
592 257add31 2020-09-09 stsp }
593 257add31 2020-09-09 stsp err = send_gotconfig_remotes(&ibuf,
594 257add31 2020-09-09 stsp &gotconfig->remotes, gotconfig->nremotes);
595 257add31 2020-09-09 stsp break;
596 257add31 2020-09-09 stsp default:
597 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
598 257add31 2020-09-09 stsp break;
599 257add31 2020-09-09 stsp }
600 257add31 2020-09-09 stsp
601 257add31 2020-09-09 stsp if (imsg.fd != -1) {
602 257add31 2020-09-09 stsp if (close(imsg.fd) == -1 && err == NULL)
603 257add31 2020-09-09 stsp err = got_error_from_errno("close");
604 257add31 2020-09-09 stsp }
605 257add31 2020-09-09 stsp
606 257add31 2020-09-09 stsp imsg_free(&imsg);
607 257add31 2020-09-09 stsp if (err)
608 257add31 2020-09-09 stsp break;
609 257add31 2020-09-09 stsp }
610 257add31 2020-09-09 stsp
611 257add31 2020-09-09 stsp imsg_clear(&ibuf);
612 257add31 2020-09-09 stsp if (err) {
613 257add31 2020-09-09 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
614 257add31 2020-09-09 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
615 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
616 257add31 2020-09-09 stsp }
617 257add31 2020-09-09 stsp }
618 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
619 257add31 2020-09-09 stsp err = got_error_from_errno("close");
620 257add31 2020-09-09 stsp return err ? 1 : 0;
621 257add31 2020-09-09 stsp }