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