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 <sys/types.h>
18 #include <sys/queue.h>
33 #include "got_error.h"
34 #include "got_object.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"
45 static volatile sig_atomic_t sigint_received;
48 catch_sigint(int signo)
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;
63 if (repo->fetch_config && repo->fetch_config->protocol)
64 protocol = repo->fetch_config->protocol;
66 protocol = repo->protocol;
68 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
69 "fetch protocol required for remote repository \"%s\"",
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;
77 server = repo->server;
79 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
80 "fetch server required for remote repository \"%s\"",
84 if (asprintf(&s, "%s%s", p, server) == -1) {
85 err = got_error_from_errno("asprintf");
91 if (repo->fetch_config && repo->fetch_config->server)
92 port = repo->fetch_config->port;
98 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
99 err = got_error_from_errno("asprintf");
106 if (repo->fetch_config && repo->fetch_config->repository)
107 repo_path = repo->fetch_config->repository;
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] == '/')
119 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
120 err = got_error_from_errno("asprintf");
126 got_path_strip_trailing_slashes(s);
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;
146 if (repo->send_config && repo->send_config->protocol)
147 protocol = repo->send_config->protocol;
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\"",
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;
160 server = repo->server;
162 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
163 "send server required for remote repository \"%s\"",
167 if (asprintf(&s, "%s%s", p, server) == -1) {
168 err = got_error_from_errno("asprintf");
174 if (repo->send_config && repo->send_config->server)
175 port = repo->send_config->port;
181 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
182 err = got_error_from_errno("asprintf");
189 if (repo->send_config && repo->send_config->repository)
190 repo_path = repo->send_config->repository;
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] == '/')
202 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
203 err = got_error_from_errno("asprintf");
209 got_path_strip_trailing_slashes(s);
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,
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);
250 TAILQ_FOREACH(repo, remotes, entry) {
251 struct got_imsg_remote iremote;
252 size_t len = sizeof(iremote);
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;
261 branch = branch->next;
265 branch = repo->branch;
267 branch = branch->next;
272 if (repo->send_config && repo->send_config->branch) {
273 branch = repo->send_config->branch;
275 branch = branch->next;
279 branch = repo->branch;
281 branch = branch->next;
286 ref = repo->fetch_ref;
292 iremote.nfetch_branches = nfetch_branches;
293 iremote.nsend_branches = nsend_branches;
294 iremote.nfetch_refs = nfetch_refs;
295 iremote.mirror_references = repo->mirror_references;
296 iremote.fetch_all_branches = repo->fetch_all_branches;
298 iremote.name_len = strlen(repo->name);
299 len += iremote.name_len;
301 err = make_fetch_url(&fetch_url, repo);
304 iremote.fetch_url_len = strlen(fetch_url);
305 len += iremote.fetch_url_len;
307 err = make_send_url(&send_url, repo);
310 iremote.send_url_len = strlen(send_url);
311 len += iremote.send_url_len;
313 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
315 err = got_error_from_errno(
316 "imsg_create GOTCONFIG_REMOTE");
320 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
321 err = got_error_from_errno(
322 "imsg_add GOTCONFIG_REMOTE");
327 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
328 err = got_error_from_errno(
329 "imsg_add GOTCONFIG_REMOTE");
333 if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
334 err = got_error_from_errno(
335 "imsg_add GOTCONFIG_REMOTE");
339 if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
340 err = got_error_from_errno(
341 "imsg_add GOTCONFIG_REMOTE");
347 imsg_close(ibuf, wbuf);
348 err = got_privsep_flush_imsg(ibuf);
357 if (repo->fetch_config && repo->fetch_config->branch) {
358 branch = repo->fetch_config->branch;
360 err = send_gotconfig_str(ibuf,
361 branch->branch_name);
364 branch = branch->next;
367 branch = repo->branch;
369 err = send_gotconfig_str(ibuf,
370 branch->branch_name);
373 branch = branch->next;
377 if (repo->send_config && repo->send_config->branch) {
378 branch = repo->send_config->branch;
380 err = send_gotconfig_str(ibuf,
381 branch->branch_name);
384 branch = branch->next;
387 branch = repo->branch;
389 err = send_gotconfig_str(ibuf,
390 branch->branch_name);
393 branch = branch->next;
397 ref = repo->fetch_ref;
399 err = send_gotconfig_str(ibuf, ref->ref_name);
411 static const struct got_error *
412 validate_protocol(const char *protocol, const char *repo_name)
414 static char msg[512];
416 if (strcmp(protocol, "ssh") != 0 &&
417 strcmp(protocol, "git+ssh") != 0 &&
418 strcmp(protocol, "git") != 0) {
419 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
420 "for remote repository \"%s\"", protocol, repo_name);
421 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
427 static const struct got_error *
428 validate_config(struct gotconfig *gotconfig)
430 const struct got_error *err;
431 struct gotconfig_remote_repo *repo, *repo2;
432 static char msg[512];
434 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
435 if (repo->name == NULL) {
436 return got_error_msg(GOT_ERR_PARSE_CONFIG,
437 "name required for remote repository");
440 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
442 strcmp(repo->name, repo2->name) != 0)
444 snprintf(msg, sizeof(msg),
445 "duplicate remote repository name '%s'",
447 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
450 if (repo->server == NULL &&
451 (repo->fetch_config == NULL ||
452 repo->fetch_config->server == NULL) &&
453 (repo->send_config == NULL ||
454 repo->send_config->server == NULL)) {
455 snprintf(msg, sizeof(msg),
456 "server required for remote repository \"%s\"",
458 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
461 if (repo->protocol == NULL &&
462 (repo->fetch_config == NULL ||
463 repo->fetch_config->protocol == NULL) &&
464 (repo->send_config == NULL ||
465 repo->send_config->protocol == NULL)) {
466 snprintf(msg, sizeof(msg),
467 "protocol required for remote repository \"%s\"",
469 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
472 if (repo->protocol) {
473 err = validate_protocol(repo->protocol, repo->name);
477 if (repo->fetch_config && repo->fetch_config->protocol) {
478 err = validate_protocol(repo->fetch_config->protocol,
483 if (repo->send_config && repo->send_config->protocol) {
484 err = validate_protocol(repo->send_config->protocol,
490 if (repo->repository == NULL &&
491 (repo->fetch_config == NULL ||
492 repo->fetch_config->repository == NULL) &&
493 (repo->send_config == NULL ||
494 repo->send_config->repository == NULL)) {
495 snprintf(msg, sizeof(msg),
496 "repository path required for remote "
497 "repository \"%s\"", repo->name);
498 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
506 main(int argc, char *argv[])
508 const struct got_error *err = NULL;
510 struct gotconfig *gotconfig = NULL;
512 const char *filename = "got.conf";
519 signal(SIGINT, catch_sigint);
521 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
524 /* revoke access to most system calls */
525 if (pledge("stdio recvfd", NULL) == -1) {
526 err = got_error_from_errno("pledge");
527 got_privsep_send_error(&ibuf, err);
538 memset(&imsg, 0, sizeof(imsg));
541 if (sigint_received) {
542 err = got_error(GOT_ERR_CANCELLED);
546 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
548 if (err->code == GOT_ERR_PRIVSEP_PIPE)
553 if (imsg.hdr.type == GOT_IMSG_STOP)
556 switch (imsg.hdr.type) {
557 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
558 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
560 err = got_error(GOT_ERR_PRIVSEP_LEN);
564 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
569 gotconfig_free(gotconfig);
570 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
573 err = validate_config(gotconfig);
575 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
576 if (gotconfig == NULL) {
577 err = got_error(GOT_ERR_PRIVSEP_MSG);
580 err = send_gotconfig_str(&ibuf,
581 gotconfig->author ? gotconfig->author : "");
583 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
584 if (gotconfig == NULL) {
585 err = got_error(GOT_ERR_PRIVSEP_MSG);
588 err = send_gotconfig_remotes(&ibuf,
589 &gotconfig->remotes, gotconfig->nremotes);
592 err = got_error(GOT_ERR_PRIVSEP_MSG);
597 if (close(imsg.fd) == -1 && err == NULL)
598 err = got_error_from_errno("close");
608 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
609 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
610 got_privsep_send_error(&ibuf, err);
613 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
614 err = got_error_from_errno("close");