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_repo_url(char **url, struct gotconfig_remote_repo *repo)
56 const struct got_error *err = NULL;
57 char *s = NULL, *p = NULL;
61 if (asprintf(&s, "%s://", repo->protocol) == -1)
62 return got_error_from_errno("asprintf");
67 if (asprintf(&s, "%s%s", p, repo->server) == -1) {
68 err = got_error_from_errno("asprintf");
78 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
79 err = got_error_from_errno("asprintf");
86 if (repo->repository) {
87 char *repo_path = repo->repository;
88 while (repo_path[0] == '/')
92 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
93 err = got_error_from_errno("asprintf");
100 got_path_strip_trailing_slashes(s);
110 static const struct got_error *
111 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
113 size_t len = value ? strlen(value) : 0;
115 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
117 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
119 return got_privsep_flush_imsg(ibuf);
122 static const struct got_error *
123 send_gotconfig_remotes(struct imsgbuf *ibuf,
124 struct gotconfig_remote_repo_list *remotes, int nremotes)
126 const struct got_error *err = NULL;
127 struct got_imsg_remotes iremotes;
128 struct gotconfig_remote_repo *repo;
131 iremotes.nremotes = nremotes;
132 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
133 &iremotes, sizeof(iremotes)) == -1)
134 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
136 err = got_privsep_flush_imsg(ibuf);
141 TAILQ_FOREACH(repo, remotes, entry) {
142 struct got_imsg_remote iremote;
143 size_t len = sizeof(iremote);
145 struct node_branch *branch;
148 branch = repo->branch;
150 branch = branch->next;
154 iremote.nbranches = nbranches;
155 iremote.mirror_references = repo->mirror_references;
157 iremote.name_len = strlen(repo->name);
158 len += iremote.name_len;
160 err = make_repo_url(&url, repo);
163 iremote.url_len = strlen(url);
164 len += iremote.url_len;
166 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
168 err = got_error_from_errno(
169 "imsg_create GOTCONFIG_REMOTE");
173 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
174 err = got_error_from_errno(
175 "imsg_add GOTCONFIG_REMOTE");
180 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
181 err = got_error_from_errno(
182 "imsg_add GOTCONFIG_REMOTE");
186 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
187 err = got_error_from_errno(
188 "imsg_add GOTCONFIG_REMOTE");
194 imsg_close(ibuf, wbuf);
195 err = got_privsep_flush_imsg(ibuf);
202 branch = repo->branch;
204 err = send_gotconfig_str(ibuf, branch->branch_name);
207 branch = branch->next;
215 static const struct got_error *
216 validate_config(struct gotconfig *gotconfig)
218 struct gotconfig_remote_repo *repo, *repo2;
219 static char msg[512];
221 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
222 if (repo->name == NULL) {
223 return got_error_msg(GOT_ERR_PARSE_CONFIG,
224 "name required for remote repository");
227 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
229 strcmp(repo->name, repo2->name) != 0)
231 snprintf(msg, sizeof(msg),
232 "duplicate remote repository name '%s'",
234 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
237 if (repo->server == NULL) {
238 snprintf(msg, sizeof(msg),
239 "server required for remote repository \"%s\"",
241 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
244 if (repo->protocol == NULL) {
245 snprintf(msg, sizeof(msg),
246 "protocol required for remote repository \"%s\"",
248 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
250 if (strcmp(repo->protocol, "ssh") != 0 &&
251 strcmp(repo->protocol, "git+ssh") != 0 &&
252 strcmp(repo->protocol, "git") != 0) {
253 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
254 "for remote repository \"%s\"", repo->protocol,
256 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
259 if (repo->repository == NULL) {
260 snprintf(msg, sizeof(msg),
261 "repository path required for remote "
262 "repository \"%s\"", repo->name);
263 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
271 main(int argc, char *argv[])
273 const struct got_error *err = NULL;
275 struct gotconfig *gotconfig = NULL;
277 const char *filename = "got.conf";
284 signal(SIGINT, catch_sigint);
286 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
289 /* revoke access to most system calls */
290 if (pledge("stdio recvfd", NULL) == -1) {
291 err = got_error_from_errno("pledge");
292 got_privsep_send_error(&ibuf, err);
303 memset(&imsg, 0, sizeof(imsg));
306 if (sigint_received) {
307 err = got_error(GOT_ERR_CANCELLED);
311 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
313 if (err->code == GOT_ERR_PRIVSEP_PIPE)
318 if (imsg.hdr.type == GOT_IMSG_STOP)
321 switch (imsg.hdr.type) {
322 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
323 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
325 err = got_error(GOT_ERR_PRIVSEP_LEN);
329 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
334 gotconfig_free(gotconfig);
335 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
338 err = validate_config(gotconfig);
340 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
341 if (gotconfig == NULL) {
342 err = got_error(GOT_ERR_PRIVSEP_MSG);
345 err = send_gotconfig_str(&ibuf,
346 gotconfig->author ? gotconfig->author : "");
348 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
349 if (gotconfig == NULL) {
350 err = got_error(GOT_ERR_PRIVSEP_MSG);
353 err = send_gotconfig_remotes(&ibuf,
354 &gotconfig->remotes, gotconfig->nremotes);
357 err = got_error(GOT_ERR_PRIVSEP_MSG);
362 if (close(imsg.fd) == -1 && err == NULL)
363 err = got_error_from_errno("close");
373 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
374 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
375 got_privsep_send_error(&ibuf, err);
378 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
379 err = got_error_from_errno("close");