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