2 257add31 2020-09-09 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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 5822e79e 2023-02-23 op #include <sha2.h>
31 e12e0e21 2020-09-14 naddy #include <unistd.h>
32 257add31 2020-09-09 stsp #include <zlib.h>
34 257add31 2020-09-09 stsp #include "got_error.h"
35 257add31 2020-09-09 stsp #include "got_object.h"
36 5e082626 2020-09-24 stsp #include "got_path.h"
37 257add31 2020-09-09 stsp #include "got_repository.h"
39 257add31 2020-09-09 stsp #include "got_lib_delta.h"
40 257add31 2020-09-09 stsp #include "got_lib_object.h"
41 257add31 2020-09-09 stsp #include "got_lib_privsep.h"
43 257add31 2020-09-09 stsp #include "gotconfig.h"
45 257add31 2020-09-09 stsp /* parse.y */
46 257add31 2020-09-09 stsp static volatile sig_atomic_t sigint_received;
49 257add31 2020-09-09 stsp catch_sigint(int signo)
51 257add31 2020-09-09 stsp sigint_received = 1;
54 257add31 2020-09-09 stsp static const struct got_error *
55 6480c871 2021-08-30 stsp make_fetch_url(char **url, struct gotconfig_remote_repo *repo)
57 257add31 2020-09-09 stsp const struct got_error *err = NULL;
58 257add31 2020-09-09 stsp char *s = NULL, *p = NULL;
59 6480c871 2021-08-30 stsp const char *protocol, *server, *repo_path;
62 257add31 2020-09-09 stsp *url = NULL;
64 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->protocol)
65 6480c871 2021-08-30 stsp protocol = repo->fetch_config->protocol;
67 6480c871 2021-08-30 stsp protocol = repo->protocol;
68 6480c871 2021-08-30 stsp if (protocol == NULL)
69 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
70 6480c871 2021-08-30 stsp "fetch protocol required for remote repository \"%s\"",
71 6480c871 2021-08-30 stsp repo->name);
72 6480c871 2021-08-30 stsp if (asprintf(&s, "%s://", protocol) == -1)
73 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
75 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->server)
76 6480c871 2021-08-30 stsp server = repo->fetch_config->server;
78 6480c871 2021-08-30 stsp server = repo->server;
79 6480c871 2021-08-30 stsp if (server == NULL)
80 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
81 6480c871 2021-08-30 stsp "fetch server required for remote repository \"%s\"",
82 6480c871 2021-08-30 stsp repo->name);
85 6480c871 2021-08-30 stsp if (asprintf(&s, "%s%s", p, server) == -1) {
86 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
92 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->server)
93 6480c871 2021-08-30 stsp port = repo->fetch_config->port;
95 6480c871 2021-08-30 stsp port = repo->port;
99 257add31 2020-09-09 stsp if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
100 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
107 6480c871 2021-08-30 stsp if (repo->fetch_config && repo->fetch_config->repository)
108 6480c871 2021-08-30 stsp repo_path = repo->fetch_config->repository;
110 6480c871 2021-08-30 stsp repo_path = repo->repository;
111 6480c871 2021-08-30 stsp if (repo_path == NULL)
112 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
113 6480c871 2021-08-30 stsp "fetch repository path required for remote "
114 6480c871 2021-08-30 stsp "repository \"%s\"", repo->name);
116 6480c871 2021-08-30 stsp while (repo_path[0] == '/')
117 6480c871 2021-08-30 stsp repo_path++;
120 6480c871 2021-08-30 stsp if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
121 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
127 6480c871 2021-08-30 stsp got_path_strip_trailing_slashes(s);
134 6480c871 2021-08-30 stsp return err;
137 6480c871 2021-08-30 stsp static const struct got_error *
138 6480c871 2021-08-30 stsp make_send_url(char **url, struct gotconfig_remote_repo *repo)
140 6480c871 2021-08-30 stsp const struct got_error *err = NULL;
141 6480c871 2021-08-30 stsp char *s = NULL, *p = NULL;
142 6480c871 2021-08-30 stsp const char *protocol, *server, *repo_path;
145 6480c871 2021-08-30 stsp *url = NULL;
147 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->protocol)
148 6480c871 2021-08-30 stsp protocol = repo->send_config->protocol;
150 6480c871 2021-08-30 stsp protocol = repo->protocol;
151 6480c871 2021-08-30 stsp if (protocol == NULL)
152 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
153 6480c871 2021-08-30 stsp "send protocol required for remote repository \"%s\"",
154 6480c871 2021-08-30 stsp repo->name);
155 6480c871 2021-08-30 stsp if (asprintf(&s, "%s://", protocol) == -1)
156 6480c871 2021-08-30 stsp return got_error_from_errno("asprintf");
158 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->server)
159 6480c871 2021-08-30 stsp server = repo->send_config->server;
161 6480c871 2021-08-30 stsp server = repo->server;
162 6480c871 2021-08-30 stsp if (server == NULL)
163 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
164 6480c871 2021-08-30 stsp "send server required for remote repository \"%s\"",
165 6480c871 2021-08-30 stsp repo->name);
168 6480c871 2021-08-30 stsp if (asprintf(&s, "%s%s", p, server) == -1) {
169 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
175 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->server)
176 6480c871 2021-08-30 stsp port = repo->send_config->port;
178 6480c871 2021-08-30 stsp port = repo->port;
179 6480c871 2021-08-30 stsp if (port) {
182 6480c871 2021-08-30 stsp if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
183 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
190 6480c871 2021-08-30 stsp if (repo->send_config && repo->send_config->repository)
191 6480c871 2021-08-30 stsp repo_path = repo->send_config->repository;
193 6480c871 2021-08-30 stsp repo_path = repo->repository;
194 6480c871 2021-08-30 stsp if (repo_path == NULL)
195 6480c871 2021-08-30 stsp return got_error_fmt(GOT_ERR_PARSE_CONFIG,
196 6480c871 2021-08-30 stsp "send repository path required for remote "
197 6480c871 2021-08-30 stsp "repository \"%s\"", repo->name);
199 6480c871 2021-08-30 stsp while (repo_path[0] == '/')
200 6480c871 2021-08-30 stsp repo_path++;
203 6480c871 2021-08-30 stsp if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
204 6480c871 2021-08-30 stsp err = got_error_from_errno("asprintf");
210 5e082626 2020-09-24 stsp got_path_strip_trailing_slashes(s);
217 257add31 2020-09-09 stsp return err;
220 257add31 2020-09-09 stsp static const struct got_error *
221 257add31 2020-09-09 stsp send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
223 be96c417 2020-09-17 stsp size_t len = value ? strlen(value) : 0;
225 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
226 257add31 2020-09-09 stsp value, len) == -1)
227 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
229 257add31 2020-09-09 stsp return got_privsep_flush_imsg(ibuf);
232 257add31 2020-09-09 stsp static const struct got_error *
233 257add31 2020-09-09 stsp send_gotconfig_remotes(struct imsgbuf *ibuf,
234 257add31 2020-09-09 stsp struct gotconfig_remote_repo_list *remotes, int nremotes)
236 257add31 2020-09-09 stsp const struct got_error *err = NULL;
237 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
238 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo;
239 6480c871 2021-08-30 stsp char *fetch_url = NULL, *send_url = NULL;
241 257add31 2020-09-09 stsp iremotes.nremotes = nremotes;
242 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
243 257add31 2020-09-09 stsp &iremotes, sizeof(iremotes)) == -1)
244 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
246 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
247 257add31 2020-09-09 stsp imsg_clear(ibuf);
249 257add31 2020-09-09 stsp return err;
251 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, remotes, entry) {
252 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
253 257add31 2020-09-09 stsp size_t len = sizeof(iremote);
254 257add31 2020-09-09 stsp struct ibuf *wbuf;
255 b8adfa55 2020-09-25 stsp struct node_branch *branch;
256 99495ddb 2021-01-10 stsp struct node_ref *ref;
257 6480c871 2021-08-30 stsp int nfetch_branches = 0, nsend_branches = 0, nfetch_refs = 0;
259 93f8a337 2021-08-30 naddy if (repo->fetch_config && repo->fetch_config->branch)
260 6480c871 2021-08-30 stsp branch = repo->fetch_config->branch;
262 6480c871 2021-08-30 stsp branch = repo->branch;
263 93f8a337 2021-08-30 naddy while (branch) {
264 93f8a337 2021-08-30 naddy branch = branch->next;
265 93f8a337 2021-08-30 naddy nfetch_branches++;
268 93f8a337 2021-08-30 naddy if (repo->send_config && repo->send_config->branch)
269 6480c871 2021-08-30 stsp branch = repo->send_config->branch;
271 6480c871 2021-08-30 stsp branch = repo->branch;
272 93f8a337 2021-08-30 naddy while (branch) {
273 93f8a337 2021-08-30 naddy branch = branch->next;
274 93f8a337 2021-08-30 naddy nsend_branches++;
277 6480c871 2021-08-30 stsp ref = repo->fetch_ref;
278 99495ddb 2021-01-10 stsp while (ref) {
279 99495ddb 2021-01-10 stsp ref = ref->next;
280 6480c871 2021-08-30 stsp nfetch_refs++;
283 6480c871 2021-08-30 stsp iremote.nfetch_branches = nfetch_branches;
284 6480c871 2021-08-30 stsp iremote.nsend_branches = nsend_branches;
285 6480c871 2021-08-30 stsp iremote.nfetch_refs = nfetch_refs;
286 257add31 2020-09-09 stsp iremote.mirror_references = repo->mirror_references;
287 0c8b29c5 2021-01-05 stsp iremote.fetch_all_branches = repo->fetch_all_branches;
289 257add31 2020-09-09 stsp iremote.name_len = strlen(repo->name);
290 257add31 2020-09-09 stsp len += iremote.name_len;
292 6480c871 2021-08-30 stsp err = make_fetch_url(&fetch_url, repo);
295 6480c871 2021-08-30 stsp iremote.fetch_url_len = strlen(fetch_url);
296 6480c871 2021-08-30 stsp len += iremote.fetch_url_len;
298 6480c871 2021-08-30 stsp err = make_send_url(&send_url, repo);
301 6480c871 2021-08-30 stsp iremote.send_url_len = strlen(send_url);
302 6480c871 2021-08-30 stsp len += iremote.send_url_len;
304 257add31 2020-09-09 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
305 257add31 2020-09-09 stsp if (wbuf == NULL) {
306 257add31 2020-09-09 stsp err = got_error_from_errno(
307 257add31 2020-09-09 stsp "imsg_create GOTCONFIG_REMOTE");
311 257add31 2020-09-09 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
312 257add31 2020-09-09 stsp err = got_error_from_errno(
313 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
317 257add31 2020-09-09 stsp if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
318 257add31 2020-09-09 stsp err = got_error_from_errno(
319 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
322 6480c871 2021-08-30 stsp if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
323 257add31 2020-09-09 stsp err = got_error_from_errno(
324 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
327 6480c871 2021-08-30 stsp if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
328 6480c871 2021-08-30 stsp err = got_error_from_errno(
329 6480c871 2021-08-30 stsp "imsg_add GOTCONFIG_REMOTE");
333 257add31 2020-09-09 stsp imsg_close(ibuf, wbuf);
334 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
338 6480c871 2021-08-30 stsp free(fetch_url);
339 6480c871 2021-08-30 stsp fetch_url = NULL;
340 6480c871 2021-08-30 stsp free(send_url);
341 6480c871 2021-08-30 stsp send_url = NULL;
343 93f8a337 2021-08-30 naddy if (repo->fetch_config && repo->fetch_config->branch)
344 6480c871 2021-08-30 stsp branch = repo->fetch_config->branch;
346 6480c871 2021-08-30 stsp branch = repo->branch;
347 93f8a337 2021-08-30 naddy while (branch) {
348 93f8a337 2021-08-30 naddy err = send_gotconfig_str(ibuf, branch->branch_name);
351 93f8a337 2021-08-30 naddy branch = branch->next;
354 93f8a337 2021-08-30 naddy if (repo->send_config && repo->send_config->branch)
355 6480c871 2021-08-30 stsp branch = repo->send_config->branch;
357 6480c871 2021-08-30 stsp branch = repo->branch;
358 93f8a337 2021-08-30 naddy while (branch) {
359 93f8a337 2021-08-30 naddy err = send_gotconfig_str(ibuf, branch->branch_name);
362 93f8a337 2021-08-30 naddy branch = branch->next;
365 6480c871 2021-08-30 stsp ref = repo->fetch_ref;
366 99495ddb 2021-01-10 stsp while (ref) {
367 99495ddb 2021-01-10 stsp err = send_gotconfig_str(ibuf, ref->ref_name);
370 99495ddb 2021-01-10 stsp ref = ref->next;
374 6480c871 2021-08-30 stsp free(fetch_url);
375 6480c871 2021-08-30 stsp free(send_url);
376 257add31 2020-09-09 stsp return err;
379 257add31 2020-09-09 stsp static const struct got_error *
380 f1cacac7 2021-08-29 stsp validate_protocol(const char *protocol, const char *repo_name)
382 f1cacac7 2021-08-29 stsp static char msg[512];
384 f1cacac7 2021-08-29 stsp if (strcmp(protocol, "ssh") != 0 &&
385 f1cacac7 2021-08-29 stsp strcmp(protocol, "git+ssh") != 0 &&
386 ced242c2 2024-04-14 me strcmp(protocol, "git") != 0 &&
387 ced242c2 2024-04-14 me strcmp(protocol, "git+http") != 0 &&
388 ced242c2 2024-04-14 me strcmp(protocol, "http") != 0 &&
389 ced242c2 2024-04-14 me strcmp(protocol, "https") != 0 &&
390 ced242c2 2024-04-14 me strcmp(protocol, "git+https") != 0) {
391 f1cacac7 2021-08-29 stsp snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
392 f1cacac7 2021-08-29 stsp "for remote repository \"%s\"", protocol, repo_name);
393 f1cacac7 2021-08-29 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
396 f1cacac7 2021-08-29 stsp return NULL;
399 f1cacac7 2021-08-29 stsp static const struct got_error *
400 257add31 2020-09-09 stsp validate_config(struct gotconfig *gotconfig)
402 f1cacac7 2021-08-29 stsp const struct got_error *err;
403 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo, *repo2;
404 257add31 2020-09-09 stsp static char msg[512];
406 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
407 257add31 2020-09-09 stsp if (repo->name == NULL) {
408 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG,
409 257add31 2020-09-09 stsp "name required for remote repository");
412 257add31 2020-09-09 stsp TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
413 257add31 2020-09-09 stsp if (repo == repo2 ||
414 257add31 2020-09-09 stsp strcmp(repo->name, repo2->name) != 0)
416 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
417 257add31 2020-09-09 stsp "duplicate remote repository name '%s'",
418 257add31 2020-09-09 stsp repo->name);
419 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
422 f1cacac7 2021-08-29 stsp if (repo->server == NULL &&
423 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
424 f1cacac7 2021-08-29 stsp repo->fetch_config->server == NULL) &&
425 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
426 f1cacac7 2021-08-29 stsp repo->send_config->server == NULL)) {
427 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
428 257add31 2020-09-09 stsp "server required for remote repository \"%s\"",
429 257add31 2020-09-09 stsp repo->name);
430 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
433 f1cacac7 2021-08-29 stsp if (repo->protocol == NULL &&
434 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
435 f1cacac7 2021-08-29 stsp repo->fetch_config->protocol == NULL) &&
436 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
437 f1cacac7 2021-08-29 stsp repo->send_config->protocol == NULL)) {
438 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
439 257add31 2020-09-09 stsp "protocol required for remote repository \"%s\"",
440 257add31 2020-09-09 stsp repo->name);
441 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
444 f1cacac7 2021-08-29 stsp if (repo->protocol) {
445 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->protocol, repo->name);
447 f1cacac7 2021-08-29 stsp return err;
449 f1cacac7 2021-08-29 stsp if (repo->fetch_config && repo->fetch_config->protocol) {
450 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->fetch_config->protocol,
451 257add31 2020-09-09 stsp repo->name);
453 f1cacac7 2021-08-29 stsp return err;
455 f1cacac7 2021-08-29 stsp if (repo->send_config && repo->send_config->protocol) {
456 f1cacac7 2021-08-29 stsp err = validate_protocol(repo->send_config->protocol,
457 f1cacac7 2021-08-29 stsp repo->name);
459 f1cacac7 2021-08-29 stsp return err;
462 f1cacac7 2021-08-29 stsp if (repo->repository == NULL &&
463 f1cacac7 2021-08-29 stsp (repo->fetch_config == NULL ||
464 f1cacac7 2021-08-29 stsp repo->fetch_config->repository == NULL) &&
465 f1cacac7 2021-08-29 stsp (repo->send_config == NULL ||
466 f1cacac7 2021-08-29 stsp repo->send_config->repository == NULL)) {
467 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
468 257add31 2020-09-09 stsp "repository path required for remote "
469 257add31 2020-09-09 stsp "repository \"%s\"", repo->name);
470 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
474 257add31 2020-09-09 stsp return NULL;
478 257add31 2020-09-09 stsp main(int argc, char *argv[])
480 257add31 2020-09-09 stsp const struct got_error *err = NULL;
481 257add31 2020-09-09 stsp struct imsgbuf ibuf;
482 53dfa00d 2020-09-10 stsp struct gotconfig *gotconfig = NULL;
483 257add31 2020-09-09 stsp size_t datalen;
484 257add31 2020-09-09 stsp const char *filename = "got.conf";
486 257add31 2020-09-09 stsp static int attached;
488 257add31 2020-09-09 stsp while (!attached)
491 257add31 2020-09-09 stsp signal(SIGINT, catch_sigint);
493 257add31 2020-09-09 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
495 257add31 2020-09-09 stsp #ifndef PROFILE
496 257add31 2020-09-09 stsp /* revoke access to most system calls */
497 257add31 2020-09-09 stsp if (pledge("stdio recvfd", NULL) == -1) {
498 257add31 2020-09-09 stsp err = got_error_from_errno("pledge");
499 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
504 257add31 2020-09-09 stsp if (argc > 1)
505 257add31 2020-09-09 stsp filename = argv[1];
508 257add31 2020-09-09 stsp struct imsg imsg;
511 257add31 2020-09-09 stsp memset(&imsg, 0, sizeof(imsg));
513 257add31 2020-09-09 stsp if (sigint_received) {
514 257add31 2020-09-09 stsp err = got_error(GOT_ERR_CANCELLED);
518 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
520 257add31 2020-09-09 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
521 257add31 2020-09-09 stsp err = NULL;
525 257add31 2020-09-09 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
528 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
529 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
530 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
531 257add31 2020-09-09 stsp if (datalen != 0) {
532 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
535 2c52c623 2024-01-30 op fd = imsg_get_fd(&imsg);
536 2c52c623 2024-01-30 op if (fd == -1){
537 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
541 257add31 2020-09-09 stsp if (gotconfig)
542 257add31 2020-09-09 stsp gotconfig_free(gotconfig);
543 2c52c623 2024-01-30 op err = gotconfig_parse(&gotconfig, filename, &fd);
546 257add31 2020-09-09 stsp err = validate_config(gotconfig);
548 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
549 257add31 2020-09-09 stsp if (gotconfig == NULL) {
550 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
553 257add31 2020-09-09 stsp err = send_gotconfig_str(&ibuf,
554 257add31 2020-09-09 stsp gotconfig->author ? gotconfig->author : "");
556 4d5ee956 2022-07-02 jrick case GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST:
557 4d5ee956 2022-07-02 jrick if (gotconfig == NULL) {
558 4d5ee956 2022-07-02 jrick err = got_error(GOT_ERR_PRIVSEP_MSG);
561 4d5ee956 2022-07-02 jrick err = send_gotconfig_str(&ibuf,
562 4d5ee956 2022-07-02 jrick gotconfig->allowed_signers_file ?
563 4d5ee956 2022-07-02 jrick gotconfig->allowed_signers_file : "");
565 4d5ee956 2022-07-02 jrick case GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST:
566 4d5ee956 2022-07-02 jrick if (gotconfig == NULL) {
567 4d5ee956 2022-07-02 jrick err = got_error(GOT_ERR_PRIVSEP_MSG);
570 4d5ee956 2022-07-02 jrick err = send_gotconfig_str(&ibuf,
571 4d5ee956 2022-07-02 jrick gotconfig->revoked_signers_file ?
572 4d5ee956 2022-07-02 jrick gotconfig->revoked_signers_file : "");
574 d68f2c0e 2022-07-05 jrick case GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST:
575 d68f2c0e 2022-07-05 jrick if (gotconfig == NULL) {
576 d68f2c0e 2022-07-05 jrick err = got_error(GOT_ERR_PRIVSEP_MSG);
579 d68f2c0e 2022-07-05 jrick err = send_gotconfig_str(&ibuf,
580 d68f2c0e 2022-07-05 jrick gotconfig->signer_id ? gotconfig->signer_id : "");
582 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
583 257add31 2020-09-09 stsp if (gotconfig == NULL) {
584 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
587 257add31 2020-09-09 stsp err = send_gotconfig_remotes(&ibuf,
588 257add31 2020-09-09 stsp &gotconfig->remotes, gotconfig->nremotes);
591 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
595 2c52c623 2024-01-30 op if (fd != -1) {
596 2c52c623 2024-01-30 op if (close(fd) == -1 && err == NULL)
597 257add31 2020-09-09 stsp err = got_error_from_errno("close");
600 257add31 2020-09-09 stsp imsg_free(&imsg);
605 257add31 2020-09-09 stsp imsg_clear(&ibuf);
607 257add31 2020-09-09 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
608 257add31 2020-09-09 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
609 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
612 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
613 257add31 2020-09-09 stsp err = got_error_from_errno("close");
614 257add31 2020-09-09 stsp return err ? 1 : 0;