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>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
35 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
41 #include "gotconfig.h"
44 static volatile sig_atomic_t sigint_received;
47 catch_sigint(int signo)
52 static const struct got_error *
53 make_fetch_url(char **url, struct gotconfig_remote_repo *repo)
55 const struct got_error *err = NULL;
56 char *s = NULL, *p = NULL;
57 const char *protocol, *server, *repo_path;
62 if (repo->fetch_config && repo->fetch_config->protocol)
63 protocol = repo->fetch_config->protocol;
65 protocol = repo->protocol;
67 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
68 "fetch protocol required for remote repository \"%s\"",
70 if (asprintf(&s, "%s://", protocol) == -1)
71 return got_error_from_errno("asprintf");
73 if (repo->fetch_config && repo->fetch_config->server)
74 server = repo->fetch_config->server;
76 server = repo->server;
78 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
79 "fetch server required for remote repository \"%s\"",
83 if (asprintf(&s, "%s%s", p, server) == -1) {
84 err = got_error_from_errno("asprintf");
90 if (repo->fetch_config && repo->fetch_config->server)
91 port = repo->fetch_config->port;
97 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
98 err = got_error_from_errno("asprintf");
105 if (repo->fetch_config && repo->fetch_config->repository)
106 repo_path = repo->fetch_config->repository;
108 repo_path = repo->repository;
109 if (repo_path == NULL)
110 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
111 "fetch repository path required for remote "
112 "repository \"%s\"", repo->name);
114 while (repo_path[0] == '/')
118 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
119 err = got_error_from_errno("asprintf");
125 got_path_strip_trailing_slashes(s);
135 static const struct got_error *
136 make_send_url(char **url, struct gotconfig_remote_repo *repo)
138 const struct got_error *err = NULL;
139 char *s = NULL, *p = NULL;
140 const char *protocol, *server, *repo_path;
145 if (repo->send_config && repo->send_config->protocol)
146 protocol = repo->send_config->protocol;
148 protocol = repo->protocol;
149 if (protocol == NULL)
150 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
151 "send protocol required for remote repository \"%s\"",
153 if (asprintf(&s, "%s://", protocol) == -1)
154 return got_error_from_errno("asprintf");
156 if (repo->send_config && repo->send_config->server)
157 server = repo->send_config->server;
159 server = repo->server;
161 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
162 "send server required for remote repository \"%s\"",
166 if (asprintf(&s, "%s%s", p, server) == -1) {
167 err = got_error_from_errno("asprintf");
173 if (repo->send_config && repo->send_config->server)
174 port = repo->send_config->port;
180 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
181 err = got_error_from_errno("asprintf");
188 if (repo->send_config && repo->send_config->repository)
189 repo_path = repo->send_config->repository;
191 repo_path = repo->repository;
192 if (repo_path == NULL)
193 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
194 "send repository path required for remote "
195 "repository \"%s\"", repo->name);
197 while (repo_path[0] == '/')
201 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
202 err = got_error_from_errno("asprintf");
208 got_path_strip_trailing_slashes(s);
218 static const struct got_error *
219 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
221 size_t len = value ? strlen(value) : 0;
223 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
225 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
227 return got_privsep_flush_imsg(ibuf);
230 static const struct got_error *
231 send_gotconfig_remotes(struct imsgbuf *ibuf,
232 struct gotconfig_remote_repo_list *remotes, int nremotes)
234 const struct got_error *err = NULL;
235 struct got_imsg_remotes iremotes;
236 struct gotconfig_remote_repo *repo;
237 char *fetch_url = NULL, *send_url = NULL;
239 iremotes.nremotes = nremotes;
240 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
241 &iremotes, sizeof(iremotes)) == -1)
242 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
244 err = got_privsep_flush_imsg(ibuf);
249 TAILQ_FOREACH(repo, remotes, entry) {
250 struct got_imsg_remote iremote;
251 size_t len = sizeof(iremote);
253 struct node_branch *branch;
254 struct node_ref *ref;
255 int nfetch_branches = 0, nsend_branches = 0, nfetch_refs = 0;
257 if (repo->fetch_config && repo->fetch_config->branch)
258 branch = repo->fetch_config->branch;
260 branch = repo->branch;
262 branch = branch->next;
266 if (repo->send_config && repo->send_config->branch)
267 branch = repo->send_config->branch;
269 branch = repo->branch;
271 branch = branch->next;
275 ref = repo->fetch_ref;
281 iremote.nfetch_branches = nfetch_branches;
282 iremote.nsend_branches = nsend_branches;
283 iremote.nfetch_refs = nfetch_refs;
284 iremote.mirror_references = repo->mirror_references;
285 iremote.fetch_all_branches = repo->fetch_all_branches;
287 iremote.name_len = strlen(repo->name);
288 len += iremote.name_len;
290 err = make_fetch_url(&fetch_url, repo);
293 iremote.fetch_url_len = strlen(fetch_url);
294 len += iremote.fetch_url_len;
296 err = make_send_url(&send_url, repo);
299 iremote.send_url_len = strlen(send_url);
300 len += iremote.send_url_len;
302 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
304 err = got_error_from_errno(
305 "imsg_create GOTCONFIG_REMOTE");
309 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
310 err = got_error_from_errno(
311 "imsg_add GOTCONFIG_REMOTE");
316 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
317 err = got_error_from_errno(
318 "imsg_add GOTCONFIG_REMOTE");
322 if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
323 err = got_error_from_errno(
324 "imsg_add GOTCONFIG_REMOTE");
328 if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
329 err = got_error_from_errno(
330 "imsg_add GOTCONFIG_REMOTE");
336 imsg_close(ibuf, wbuf);
337 err = got_privsep_flush_imsg(ibuf);
346 if (repo->fetch_config && repo->fetch_config->branch)
347 branch = repo->fetch_config->branch;
349 branch = repo->branch;
351 err = send_gotconfig_str(ibuf, branch->branch_name);
354 branch = branch->next;
357 if (repo->send_config && repo->send_config->branch)
358 branch = repo->send_config->branch;
360 branch = repo->branch;
362 err = send_gotconfig_str(ibuf, branch->branch_name);
365 branch = branch->next;
368 ref = repo->fetch_ref;
370 err = send_gotconfig_str(ibuf, ref->ref_name);
382 static const struct got_error *
383 validate_protocol(const char *protocol, const char *repo_name)
385 static char msg[512];
387 if (strcmp(protocol, "ssh") != 0 &&
388 strcmp(protocol, "git+ssh") != 0 &&
389 strcmp(protocol, "git") != 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);
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) {
413 strcmp(repo->name, repo2->name) != 0)
415 snprintf(msg, sizeof(msg),
416 "duplicate remote repository name '%s'",
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\"",
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\"",
440 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
443 if (repo->protocol) {
444 err = validate_protocol(repo->protocol, repo->name);
448 if (repo->fetch_config && repo->fetch_config->protocol) {
449 err = validate_protocol(repo->fetch_config->protocol,
454 if (repo->send_config && repo->send_config->protocol) {
455 err = validate_protocol(repo->send_config->protocol,
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);
477 main(int argc, char *argv[])
479 const struct got_error *err = NULL;
481 struct gotconfig *gotconfig = NULL;
483 const char *filename = "got.conf";
490 signal(SIGINT, catch_sigint);
492 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
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);
509 memset(&imsg, 0, sizeof(imsg));
512 if (sigint_received) {
513 err = got_error(GOT_ERR_CANCELLED);
517 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
519 if (err->code == GOT_ERR_PRIVSEP_PIPE)
524 if (imsg.hdr.type == GOT_IMSG_STOP)
527 switch (imsg.hdr.type) {
528 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
529 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
531 err = got_error(GOT_ERR_PRIVSEP_LEN);
535 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
540 gotconfig_free(gotconfig);
541 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
544 err = validate_config(gotconfig);
546 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
547 if (gotconfig == NULL) {
548 err = got_error(GOT_ERR_PRIVSEP_MSG);
551 err = send_gotconfig_str(&ibuf,
552 gotconfig->author ? gotconfig->author : "");
554 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
555 if (gotconfig == NULL) {
556 err = got_error(GOT_ERR_PRIVSEP_MSG);
559 err = send_gotconfig_remotes(&ibuf,
560 &gotconfig->remotes, gotconfig->nremotes);
563 err = got_error(GOT_ERR_PRIVSEP_MSG);
568 if (close(imsg.fd) == -1 && err == NULL)
569 err = got_error_from_errno("close");
579 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
580 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
581 got_privsep_send_error(&ibuf, err);
584 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
585 err = got_error_from_errno("close");