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 257add31 2020-09-09 stsp make_repo_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 257add31 2020-09-09 stsp
59 257add31 2020-09-09 stsp *url = NULL;
60 257add31 2020-09-09 stsp
61 257add31 2020-09-09 stsp if (asprintf(&s, "%s://", repo->protocol) == -1)
62 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
63 257add31 2020-09-09 stsp
64 257add31 2020-09-09 stsp if (repo->server) {
65 257add31 2020-09-09 stsp p = s;
66 257add31 2020-09-09 stsp s = NULL;
67 257add31 2020-09-09 stsp if (asprintf(&s, "%s%s", p, repo->server) == -1) {
68 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
69 257add31 2020-09-09 stsp goto done;
70 257add31 2020-09-09 stsp }
71 257add31 2020-09-09 stsp free(p);
72 257add31 2020-09-09 stsp p = NULL;
73 257add31 2020-09-09 stsp }
74 257add31 2020-09-09 stsp
75 257add31 2020-09-09 stsp if (repo->port) {
76 257add31 2020-09-09 stsp p = s;
77 257add31 2020-09-09 stsp s = NULL;
78 257add31 2020-09-09 stsp if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
79 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
80 257add31 2020-09-09 stsp goto done;
81 257add31 2020-09-09 stsp }
82 257add31 2020-09-09 stsp free(p);
83 257add31 2020-09-09 stsp p = NULL;
84 257add31 2020-09-09 stsp }
85 257add31 2020-09-09 stsp
86 257add31 2020-09-09 stsp if (repo->repository) {
87 5e082626 2020-09-24 stsp char *repo_path = repo->repository;
88 5e082626 2020-09-24 stsp while (repo_path[0] == '/')
89 5e082626 2020-09-24 stsp repo_path++;
90 257add31 2020-09-09 stsp p = s;
91 257add31 2020-09-09 stsp s = NULL;
92 5e082626 2020-09-24 stsp if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
93 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
94 257add31 2020-09-09 stsp goto done;
95 257add31 2020-09-09 stsp }
96 257add31 2020-09-09 stsp free(p);
97 257add31 2020-09-09 stsp p = NULL;
98 257add31 2020-09-09 stsp }
99 5e082626 2020-09-24 stsp
100 5e082626 2020-09-24 stsp got_path_strip_trailing_slashes(s);
101 257add31 2020-09-09 stsp done:
102 257add31 2020-09-09 stsp if (err) {
103 257add31 2020-09-09 stsp free(s);
104 257add31 2020-09-09 stsp free(p);
105 257add31 2020-09-09 stsp } else
106 257add31 2020-09-09 stsp *url = s;
107 257add31 2020-09-09 stsp return err;
108 257add31 2020-09-09 stsp }
109 257add31 2020-09-09 stsp
110 257add31 2020-09-09 stsp static const struct got_error *
111 257add31 2020-09-09 stsp send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
112 257add31 2020-09-09 stsp {
113 be96c417 2020-09-17 stsp size_t len = value ? strlen(value) : 0;
114 257add31 2020-09-09 stsp
115 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
116 257add31 2020-09-09 stsp value, len) == -1)
117 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
118 257add31 2020-09-09 stsp
119 257add31 2020-09-09 stsp return got_privsep_flush_imsg(ibuf);
120 257add31 2020-09-09 stsp }
121 257add31 2020-09-09 stsp
122 257add31 2020-09-09 stsp static const struct got_error *
123 257add31 2020-09-09 stsp send_gotconfig_remotes(struct imsgbuf *ibuf,
124 257add31 2020-09-09 stsp struct gotconfig_remote_repo_list *remotes, int nremotes)
125 257add31 2020-09-09 stsp {
126 257add31 2020-09-09 stsp const struct got_error *err = NULL;
127 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
128 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo;
129 257add31 2020-09-09 stsp char *url = NULL;
130 257add31 2020-09-09 stsp
131 257add31 2020-09-09 stsp iremotes.nremotes = nremotes;
132 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
133 257add31 2020-09-09 stsp &iremotes, sizeof(iremotes)) == -1)
134 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
135 257add31 2020-09-09 stsp
136 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
137 257add31 2020-09-09 stsp imsg_clear(ibuf);
138 257add31 2020-09-09 stsp if (err)
139 257add31 2020-09-09 stsp return err;
140 257add31 2020-09-09 stsp
141 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, remotes, entry) {
142 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
143 257add31 2020-09-09 stsp size_t len = sizeof(iremote);
144 257add31 2020-09-09 stsp struct ibuf *wbuf;
145 b8adfa55 2020-09-25 stsp struct node_branch *branch;
146 99495ddb 2021-01-10 stsp struct node_ref *ref;
147 99495ddb 2021-01-10 stsp int nbranches = 0, nrefs = 0;
148 257add31 2020-09-09 stsp
149 b8adfa55 2020-09-25 stsp branch = repo->branch;
150 b8adfa55 2020-09-25 stsp while (branch) {
151 b8adfa55 2020-09-25 stsp branch = branch->next;
152 b8adfa55 2020-09-25 stsp nbranches++;
153 99495ddb 2021-01-10 stsp }
154 99495ddb 2021-01-10 stsp
155 99495ddb 2021-01-10 stsp ref = repo->ref;
156 99495ddb 2021-01-10 stsp while (ref) {
157 99495ddb 2021-01-10 stsp ref = ref->next;
158 99495ddb 2021-01-10 stsp nrefs++;
159 b8adfa55 2020-09-25 stsp }
160 b8adfa55 2020-09-25 stsp
161 b8adfa55 2020-09-25 stsp iremote.nbranches = nbranches;
162 99495ddb 2021-01-10 stsp iremote.nrefs = nrefs;
163 257add31 2020-09-09 stsp iremote.mirror_references = repo->mirror_references;
164 0c8b29c5 2021-01-05 stsp iremote.fetch_all_branches = repo->fetch_all_branches;
165 257add31 2020-09-09 stsp
166 257add31 2020-09-09 stsp iremote.name_len = strlen(repo->name);
167 257add31 2020-09-09 stsp len += iremote.name_len;
168 257add31 2020-09-09 stsp
169 257add31 2020-09-09 stsp err = make_repo_url(&url, repo);
170 257add31 2020-09-09 stsp if (err)
171 257add31 2020-09-09 stsp break;
172 257add31 2020-09-09 stsp iremote.url_len = strlen(url);
173 257add31 2020-09-09 stsp len += iremote.url_len;
174 257add31 2020-09-09 stsp
175 257add31 2020-09-09 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
176 257add31 2020-09-09 stsp if (wbuf == NULL) {
177 257add31 2020-09-09 stsp err = got_error_from_errno(
178 257add31 2020-09-09 stsp "imsg_create GOTCONFIG_REMOTE");
179 257add31 2020-09-09 stsp break;
180 257add31 2020-09-09 stsp }
181 257add31 2020-09-09 stsp
182 257add31 2020-09-09 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
183 257add31 2020-09-09 stsp err = got_error_from_errno(
184 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
185 257add31 2020-09-09 stsp ibuf_free(wbuf);
186 257add31 2020-09-09 stsp break;
187 257add31 2020-09-09 stsp }
188 257add31 2020-09-09 stsp
189 257add31 2020-09-09 stsp if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
190 257add31 2020-09-09 stsp err = got_error_from_errno(
191 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
192 257add31 2020-09-09 stsp ibuf_free(wbuf);
193 257add31 2020-09-09 stsp break;
194 257add31 2020-09-09 stsp }
195 257add31 2020-09-09 stsp if (imsg_add(wbuf, url, iremote.url_len) == -1) {
196 257add31 2020-09-09 stsp err = got_error_from_errno(
197 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
198 257add31 2020-09-09 stsp ibuf_free(wbuf);
199 257add31 2020-09-09 stsp break;
200 257add31 2020-09-09 stsp }
201 257add31 2020-09-09 stsp
202 257add31 2020-09-09 stsp wbuf->fd = -1;
203 257add31 2020-09-09 stsp imsg_close(ibuf, wbuf);
204 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
205 257add31 2020-09-09 stsp if (err)
206 257add31 2020-09-09 stsp break;
207 257add31 2020-09-09 stsp
208 257add31 2020-09-09 stsp free(url);
209 257add31 2020-09-09 stsp url = NULL;
210 b8adfa55 2020-09-25 stsp
211 b8adfa55 2020-09-25 stsp branch = repo->branch;
212 b8adfa55 2020-09-25 stsp while (branch) {
213 b8adfa55 2020-09-25 stsp err = send_gotconfig_str(ibuf, branch->branch_name);
214 b8adfa55 2020-09-25 stsp if (err)
215 b8adfa55 2020-09-25 stsp break;
216 b8adfa55 2020-09-25 stsp branch = branch->next;
217 b8adfa55 2020-09-25 stsp }
218 99495ddb 2021-01-10 stsp
219 99495ddb 2021-01-10 stsp ref = repo->ref;
220 99495ddb 2021-01-10 stsp while (ref) {
221 99495ddb 2021-01-10 stsp err = send_gotconfig_str(ibuf, ref->ref_name);
222 99495ddb 2021-01-10 stsp if (err)
223 99495ddb 2021-01-10 stsp break;
224 99495ddb 2021-01-10 stsp ref = ref->next;
225 99495ddb 2021-01-10 stsp }
226 257add31 2020-09-09 stsp }
227 257add31 2020-09-09 stsp
228 257add31 2020-09-09 stsp free(url);
229 257add31 2020-09-09 stsp return err;
230 257add31 2020-09-09 stsp }
231 257add31 2020-09-09 stsp
232 257add31 2020-09-09 stsp static const struct got_error *
233 f1cacac7 2021-08-29 stsp validate_protocol(const char *protocol, const char *repo_name)
234 f1cacac7 2021-08-29 stsp {
235 f1cacac7 2021-08-29 stsp static char msg[512];
236 f1cacac7 2021-08-29 stsp
237 f1cacac7 2021-08-29 stsp if (strcmp(protocol, "ssh") != 0 &&
238 f1cacac7 2021-08-29 stsp strcmp(protocol, "git+ssh") != 0 &&
239 f1cacac7 2021-08-29 stsp strcmp(protocol, "git") != 0) {
240 f1cacac7 2021-08-29 stsp snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
241 f1cacac7 2021-08-29 stsp "for remote repository \"%s\"", protocol, repo_name);
242 f1cacac7 2021-08-29 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
243 f1cacac7 2021-08-29 stsp }
244 f1cacac7 2021-08-29 stsp
245 f1cacac7 2021-08-29 stsp return NULL;
246 f1cacac7 2021-08-29 stsp }
247 f1cacac7 2021-08-29 stsp
248 f1cacac7 2021-08-29 stsp static const struct got_error *
249 257add31 2020-09-09 stsp validate_config(struct gotconfig *gotconfig)
250 257add31 2020-09-09 stsp {
251 f1cacac7 2021-08-29 stsp const struct got_error *err;
252 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo, *repo2;
253 257add31 2020-09-09 stsp static char msg[512];
254 257add31 2020-09-09 stsp
255 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
256 257add31 2020-09-09 stsp if (repo->name == NULL) {
257 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG,
258 257add31 2020-09-09 stsp "name required for remote repository");
259 257add31 2020-09-09 stsp }
260 257add31 2020-09-09 stsp
261 257add31 2020-09-09 stsp TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
262 257add31 2020-09-09 stsp if (repo == repo2 ||
263 257add31 2020-09-09 stsp strcmp(repo->name, repo2->name) != 0)
264 257add31 2020-09-09 stsp continue;
265 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
266 257add31 2020-09-09 stsp "duplicate remote repository name '%s'",
267 257add31 2020-09-09 stsp repo->name);
268 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
269 257add31 2020-09-09 stsp }
270 257add31 2020-09-09 stsp
271 f1cacac7 2021-08-29 stsp if (repo->server == NULL &&
272 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
273 f1cacac7 2021-08-29 stsp repo->fetch_config->server == NULL) &&
274 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
275 f1cacac7 2021-08-29 stsp repo->send_config->server == NULL)) {
276 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
277 257add31 2020-09-09 stsp "server required for remote repository \"%s\"",
278 257add31 2020-09-09 stsp repo->name);
279 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
280 257add31 2020-09-09 stsp }
281 257add31 2020-09-09 stsp
282 f1cacac7 2021-08-29 stsp if (repo->protocol == NULL &&
283 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
284 f1cacac7 2021-08-29 stsp repo->fetch_config->protocol == NULL) &&
285 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
286 f1cacac7 2021-08-29 stsp repo->send_config->protocol == NULL)) {
287 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
288 257add31 2020-09-09 stsp "protocol required for remote repository \"%s\"",
289 257add31 2020-09-09 stsp repo->name);
290 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
291 257add31 2020-09-09 stsp }
292 f1cacac7 2021-08-29 stsp
293 f1cacac7 2021-08-29 stsp if (repo->protocol) {
294 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->protocol, repo->name);
295 f1cacac7 2021-08-29 stsp if (err)
296 f1cacac7 2021-08-29 stsp return err;
297 f1cacac7 2021-08-29 stsp }
298 f1cacac7 2021-08-29 stsp if (repo->fetch_config && repo->fetch_config->protocol) {
299 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->fetch_config->protocol,
300 257add31 2020-09-09 stsp repo->name);
301 f1cacac7 2021-08-29 stsp if (err)
302 f1cacac7 2021-08-29 stsp return err;
303 257add31 2020-09-09 stsp }
304 f1cacac7 2021-08-29 stsp if (repo->send_config && repo->send_config->protocol) {
305 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->send_config->protocol,
306 f1cacac7 2021-08-29 stsp repo->name);
307 f1cacac7 2021-08-29 stsp if (err)
308 f1cacac7 2021-08-29 stsp return err;
309 f1cacac7 2021-08-29 stsp }
310 257add31 2020-09-09 stsp
311 f1cacac7 2021-08-29 stsp if (repo->repository == NULL &&
312 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
313 f1cacac7 2021-08-29 stsp repo->fetch_config->repository == NULL) &&
314 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
315 f1cacac7 2021-08-29 stsp repo->send_config->repository == NULL)) {
316 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
317 257add31 2020-09-09 stsp "repository path required for remote "
318 257add31 2020-09-09 stsp "repository \"%s\"", repo->name);
319 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
320 257add31 2020-09-09 stsp }
321 257add31 2020-09-09 stsp }
322 257add31 2020-09-09 stsp
323 257add31 2020-09-09 stsp return NULL;
324 257add31 2020-09-09 stsp }
325 257add31 2020-09-09 stsp
326 257add31 2020-09-09 stsp int
327 257add31 2020-09-09 stsp main(int argc, char *argv[])
328 257add31 2020-09-09 stsp {
329 257add31 2020-09-09 stsp const struct got_error *err = NULL;
330 257add31 2020-09-09 stsp struct imsgbuf ibuf;
331 53dfa00d 2020-09-10 stsp struct gotconfig *gotconfig = NULL;
332 257add31 2020-09-09 stsp size_t datalen;
333 257add31 2020-09-09 stsp const char *filename = "got.conf";
334 257add31 2020-09-09 stsp #if 0
335 257add31 2020-09-09 stsp static int attached;
336 257add31 2020-09-09 stsp
337 257add31 2020-09-09 stsp while (!attached)
338 257add31 2020-09-09 stsp sleep(1);
339 257add31 2020-09-09 stsp #endif
340 257add31 2020-09-09 stsp signal(SIGINT, catch_sigint);
341 257add31 2020-09-09 stsp
342 257add31 2020-09-09 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
343 257add31 2020-09-09 stsp
344 257add31 2020-09-09 stsp #ifndef PROFILE
345 257add31 2020-09-09 stsp /* revoke access to most system calls */
346 257add31 2020-09-09 stsp if (pledge("stdio recvfd", NULL) == -1) {
347 257add31 2020-09-09 stsp err = got_error_from_errno("pledge");
348 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
349 257add31 2020-09-09 stsp return 1;
350 257add31 2020-09-09 stsp }
351 257add31 2020-09-09 stsp #endif
352 257add31 2020-09-09 stsp
353 257add31 2020-09-09 stsp if (argc > 1)
354 257add31 2020-09-09 stsp filename = argv[1];
355 257add31 2020-09-09 stsp
356 257add31 2020-09-09 stsp for (;;) {
357 257add31 2020-09-09 stsp struct imsg imsg;
358 257add31 2020-09-09 stsp
359 257add31 2020-09-09 stsp memset(&imsg, 0, sizeof(imsg));
360 257add31 2020-09-09 stsp imsg.fd = -1;
361 257add31 2020-09-09 stsp
362 257add31 2020-09-09 stsp if (sigint_received) {
363 257add31 2020-09-09 stsp err = got_error(GOT_ERR_CANCELLED);
364 257add31 2020-09-09 stsp break;
365 257add31 2020-09-09 stsp }
366 257add31 2020-09-09 stsp
367 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
368 257add31 2020-09-09 stsp if (err) {
369 257add31 2020-09-09 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
370 257add31 2020-09-09 stsp err = NULL;
371 257add31 2020-09-09 stsp break;
372 257add31 2020-09-09 stsp }
373 257add31 2020-09-09 stsp
374 257add31 2020-09-09 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
375 257add31 2020-09-09 stsp break;
376 257add31 2020-09-09 stsp
377 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
378 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
379 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
380 257add31 2020-09-09 stsp if (datalen != 0) {
381 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
382 257add31 2020-09-09 stsp break;
383 257add31 2020-09-09 stsp }
384 257add31 2020-09-09 stsp if (imsg.fd == -1){
385 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
386 257add31 2020-09-09 stsp break;
387 257add31 2020-09-09 stsp }
388 257add31 2020-09-09 stsp
389 257add31 2020-09-09 stsp if (gotconfig)
390 257add31 2020-09-09 stsp gotconfig_free(gotconfig);
391 257add31 2020-09-09 stsp err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
392 257add31 2020-09-09 stsp if (err)
393 257add31 2020-09-09 stsp break;
394 257add31 2020-09-09 stsp err = validate_config(gotconfig);
395 257add31 2020-09-09 stsp break;
396 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
397 257add31 2020-09-09 stsp if (gotconfig == NULL) {
398 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
399 257add31 2020-09-09 stsp break;
400 257add31 2020-09-09 stsp }
401 257add31 2020-09-09 stsp err = send_gotconfig_str(&ibuf,
402 257add31 2020-09-09 stsp gotconfig->author ? gotconfig->author : "");
403 257add31 2020-09-09 stsp break;
404 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
405 257add31 2020-09-09 stsp if (gotconfig == NULL) {
406 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
407 257add31 2020-09-09 stsp break;
408 257add31 2020-09-09 stsp }
409 257add31 2020-09-09 stsp err = send_gotconfig_remotes(&ibuf,
410 257add31 2020-09-09 stsp &gotconfig->remotes, gotconfig->nremotes);
411 257add31 2020-09-09 stsp break;
412 257add31 2020-09-09 stsp default:
413 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
414 257add31 2020-09-09 stsp break;
415 257add31 2020-09-09 stsp }
416 257add31 2020-09-09 stsp
417 257add31 2020-09-09 stsp if (imsg.fd != -1) {
418 257add31 2020-09-09 stsp if (close(imsg.fd) == -1 && err == NULL)
419 257add31 2020-09-09 stsp err = got_error_from_errno("close");
420 257add31 2020-09-09 stsp }
421 257add31 2020-09-09 stsp
422 257add31 2020-09-09 stsp imsg_free(&imsg);
423 257add31 2020-09-09 stsp if (err)
424 257add31 2020-09-09 stsp break;
425 257add31 2020-09-09 stsp }
426 257add31 2020-09-09 stsp
427 257add31 2020-09-09 stsp imsg_clear(&ibuf);
428 257add31 2020-09-09 stsp if (err) {
429 257add31 2020-09-09 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
430 257add31 2020-09-09 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
431 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
432 257add31 2020-09-09 stsp }
433 257add31 2020-09-09 stsp }
434 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
435 257add31 2020-09-09 stsp err = got_error_from_errno("close");
436 257add31 2020-09-09 stsp return err ? 1 : 0;
437 257add31 2020-09-09 stsp }