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>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_repository.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_privsep.h"
43 #include "gotconfig.h"
46 static volatile sig_atomic_t sigint_received;
49 catch_sigint(int signo)
54 static const struct got_error *
55 make_fetch_url(char **url, struct gotconfig_remote_repo *repo)
57 const struct got_error *err = NULL;
58 char *s = NULL, *p = NULL;
59 const char *protocol, *server, *repo_path;
64 if (repo->fetch_config && repo->fetch_config->protocol)
65 protocol = repo->fetch_config->protocol;
67 protocol = repo->protocol;
69 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
70 "fetch protocol required for remote repository \"%s\"",
72 if (asprintf(&s, "%s://", protocol) == -1)
73 return got_error_from_errno("asprintf");
75 if (repo->fetch_config && repo->fetch_config->server)
76 server = repo->fetch_config->server;
78 server = repo->server;
80 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
81 "fetch server required for remote repository \"%s\"",
85 if (asprintf(&s, "%s%s", p, server) == -1) {
86 err = got_error_from_errno("asprintf");
92 if (repo->fetch_config && repo->fetch_config->server)
93 port = repo->fetch_config->port;
99 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
100 err = got_error_from_errno("asprintf");
107 if (repo->fetch_config && repo->fetch_config->repository)
108 repo_path = repo->fetch_config->repository;
110 repo_path = repo->repository;
111 if (repo_path == NULL)
112 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
113 "fetch repository path required for remote "
114 "repository \"%s\"", repo->name);
116 while (repo_path[0] == '/')
120 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
121 err = got_error_from_errno("asprintf");
127 got_path_strip_trailing_slashes(s);
137 static const struct got_error *
138 make_send_url(char **url, struct gotconfig_remote_repo *repo)
140 const struct got_error *err = NULL;
141 char *s = NULL, *p = NULL;
142 const char *protocol, *server, *repo_path;
147 if (repo->send_config && repo->send_config->protocol)
148 protocol = repo->send_config->protocol;
150 protocol = repo->protocol;
151 if (protocol == NULL)
152 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
153 "send protocol required for remote repository \"%s\"",
155 if (asprintf(&s, "%s://", protocol) == -1)
156 return got_error_from_errno("asprintf");
158 if (repo->send_config && repo->send_config->server)
159 server = repo->send_config->server;
161 server = repo->server;
163 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
164 "send server required for remote repository \"%s\"",
168 if (asprintf(&s, "%s%s", p, server) == -1) {
169 err = got_error_from_errno("asprintf");
175 if (repo->send_config && repo->send_config->server)
176 port = repo->send_config->port;
182 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
183 err = got_error_from_errno("asprintf");
190 if (repo->send_config && repo->send_config->repository)
191 repo_path = repo->send_config->repository;
193 repo_path = repo->repository;
194 if (repo_path == NULL)
195 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
196 "send repository path required for remote "
197 "repository \"%s\"", repo->name);
199 while (repo_path[0] == '/')
203 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
204 err = got_error_from_errno("asprintf");
210 got_path_strip_trailing_slashes(s);
220 static const struct got_error *
221 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
223 size_t len = value ? strlen(value) : 0;
225 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
227 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
229 return got_privsep_flush_imsg(ibuf);
232 static const struct got_error *
233 send_gotconfig_remotes(struct imsgbuf *ibuf,
234 struct gotconfig_remote_repo_list *remotes, int nremotes)
236 const struct got_error *err = NULL;
237 struct got_imsg_remotes iremotes;
238 struct gotconfig_remote_repo *repo;
239 char *fetch_url = NULL, *send_url = NULL;
241 iremotes.nremotes = nremotes;
242 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
243 &iremotes, sizeof(iremotes)) == -1)
244 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
246 err = got_privsep_flush_imsg(ibuf);
251 TAILQ_FOREACH(repo, remotes, entry) {
252 struct got_imsg_remote iremote;
253 size_t len = sizeof(iremote);
255 struct node_branch *branch;
256 struct node_ref *ref;
257 int nfetch_branches = 0, nsend_branches = 0, nfetch_refs = 0;
259 if (repo->fetch_config && repo->fetch_config->branch)
260 branch = repo->fetch_config->branch;
262 branch = repo->branch;
264 branch = branch->next;
268 if (repo->send_config && repo->send_config->branch)
269 branch = repo->send_config->branch;
271 branch = repo->branch;
273 branch = branch->next;
277 ref = repo->fetch_ref;
283 iremote.nfetch_branches = nfetch_branches;
284 iremote.nsend_branches = nsend_branches;
285 iremote.nfetch_refs = nfetch_refs;
286 iremote.mirror_references = repo->mirror_references;
287 iremote.fetch_all_branches = repo->fetch_all_branches;
289 iremote.name_len = strlen(repo->name);
290 len += iremote.name_len;
292 err = make_fetch_url(&fetch_url, repo);
295 iremote.fetch_url_len = strlen(fetch_url);
296 len += iremote.fetch_url_len;
298 err = make_send_url(&send_url, repo);
301 iremote.send_url_len = strlen(send_url);
302 len += iremote.send_url_len;
304 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
306 err = got_error_from_errno(
307 "imsg_create GOTCONFIG_REMOTE");
311 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
312 err = got_error_from_errno(
313 "imsg_add GOTCONFIG_REMOTE");
317 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
318 err = got_error_from_errno(
319 "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");
327 if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
328 err = got_error_from_errno(
329 "imsg_add GOTCONFIG_REMOTE");
334 imsg_close(ibuf, wbuf);
335 err = got_privsep_flush_imsg(ibuf);
344 if (repo->fetch_config && repo->fetch_config->branch)
345 branch = repo->fetch_config->branch;
347 branch = repo->branch;
349 err = send_gotconfig_str(ibuf, branch->branch_name);
352 branch = branch->next;
355 if (repo->send_config && repo->send_config->branch)
356 branch = repo->send_config->branch;
358 branch = repo->branch;
360 err = send_gotconfig_str(ibuf, branch->branch_name);
363 branch = branch->next;
366 ref = repo->fetch_ref;
368 err = send_gotconfig_str(ibuf, ref->ref_name);
380 static const struct got_error *
381 validate_protocol(const char *protocol, const char *repo_name)
383 static char msg[512];
385 if (strcmp(protocol, "ssh") != 0 &&
386 strcmp(protocol, "git+ssh") != 0 &&
387 strcmp(protocol, "git") != 0) {
388 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
389 "for remote repository \"%s\"", protocol, repo_name);
390 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
396 static const struct got_error *
397 validate_config(struct gotconfig *gotconfig)
399 const struct got_error *err;
400 struct gotconfig_remote_repo *repo, *repo2;
401 static char msg[512];
403 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
404 if (repo->name == NULL) {
405 return got_error_msg(GOT_ERR_PARSE_CONFIG,
406 "name required for remote repository");
409 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
411 strcmp(repo->name, repo2->name) != 0)
413 snprintf(msg, sizeof(msg),
414 "duplicate remote repository name '%s'",
416 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
419 if (repo->server == NULL &&
420 (repo->fetch_config == NULL ||
421 repo->fetch_config->server == NULL) &&
422 (repo->send_config == NULL ||
423 repo->send_config->server == NULL)) {
424 snprintf(msg, sizeof(msg),
425 "server required for remote repository \"%s\"",
427 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
430 if (repo->protocol == NULL &&
431 (repo->fetch_config == NULL ||
432 repo->fetch_config->protocol == NULL) &&
433 (repo->send_config == NULL ||
434 repo->send_config->protocol == NULL)) {
435 snprintf(msg, sizeof(msg),
436 "protocol required for remote repository \"%s\"",
438 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
441 if (repo->protocol) {
442 err = validate_protocol(repo->protocol, repo->name);
446 if (repo->fetch_config && repo->fetch_config->protocol) {
447 err = validate_protocol(repo->fetch_config->protocol,
452 if (repo->send_config && repo->send_config->protocol) {
453 err = validate_protocol(repo->send_config->protocol,
459 if (repo->repository == NULL &&
460 (repo->fetch_config == NULL ||
461 repo->fetch_config->repository == NULL) &&
462 (repo->send_config == NULL ||
463 repo->send_config->repository == NULL)) {
464 snprintf(msg, sizeof(msg),
465 "repository path required for remote "
466 "repository \"%s\"", repo->name);
467 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
475 main(int argc, char *argv[])
477 const struct got_error *err = NULL;
479 struct gotconfig *gotconfig = NULL;
481 const char *filename = "got.conf";
488 signal(SIGINT, catch_sigint);
490 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
493 /* revoke access to most system calls */
494 if (pledge("stdio recvfd", NULL) == -1) {
495 err = got_error_from_errno("pledge");
496 got_privsep_send_error(&ibuf, err);
507 memset(&imsg, 0, sizeof(imsg));
510 if (sigint_received) {
511 err = got_error(GOT_ERR_CANCELLED);
515 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
517 if (err->code == GOT_ERR_PRIVSEP_PIPE)
522 if (imsg.hdr.type == GOT_IMSG_STOP)
525 switch (imsg.hdr.type) {
526 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
527 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
529 err = got_error(GOT_ERR_PRIVSEP_LEN);
533 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
538 gotconfig_free(gotconfig);
539 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
542 err = validate_config(gotconfig);
544 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
545 if (gotconfig == NULL) {
546 err = got_error(GOT_ERR_PRIVSEP_MSG);
549 err = send_gotconfig_str(&ibuf,
550 gotconfig->author ? gotconfig->author : "");
552 case GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST:
553 if (gotconfig == NULL) {
554 err = got_error(GOT_ERR_PRIVSEP_MSG);
557 err = send_gotconfig_str(&ibuf,
558 gotconfig->allowed_signers_file ?
559 gotconfig->allowed_signers_file : "");
561 case GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST:
562 if (gotconfig == NULL) {
563 err = got_error(GOT_ERR_PRIVSEP_MSG);
566 err = send_gotconfig_str(&ibuf,
567 gotconfig->revoked_signers_file ?
568 gotconfig->revoked_signers_file : "");
570 case GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST:
571 if (gotconfig == NULL) {
572 err = got_error(GOT_ERR_PRIVSEP_MSG);
575 err = send_gotconfig_str(&ibuf,
576 gotconfig->signer_id ? gotconfig->signer_id : "");
578 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
579 if (gotconfig == NULL) {
580 err = got_error(GOT_ERR_PRIVSEP_MSG);
583 err = send_gotconfig_remotes(&ibuf,
584 &gotconfig->remotes, gotconfig->nremotes);
587 err = got_error(GOT_ERR_PRIVSEP_MSG);
592 if (close(imsg.fd) == -1 && err == NULL)
593 err = got_error_from_errno("close");
603 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
604 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
605 got_privsep_send_error(&ibuf, err);
608 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
609 err = got_error_from_errno("close");