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;
146 struct node_ref *ref;
147 int nbranches = 0, nrefs = 0;
149 branch = repo->branch;
151 branch = branch->next;
161 iremote.nbranches = nbranches;
162 iremote.nrefs = nrefs;
163 iremote.mirror_references = repo->mirror_references;
164 iremote.fetch_all_branches = repo->fetch_all_branches;
166 iremote.name_len = strlen(repo->name);
167 len += iremote.name_len;
169 err = make_repo_url(&url, repo);
172 iremote.url_len = strlen(url);
173 len += iremote.url_len;
175 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
177 err = got_error_from_errno(
178 "imsg_create GOTCONFIG_REMOTE");
182 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
183 err = got_error_from_errno(
184 "imsg_add GOTCONFIG_REMOTE");
189 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
190 err = got_error_from_errno(
191 "imsg_add GOTCONFIG_REMOTE");
195 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
196 err = got_error_from_errno(
197 "imsg_add GOTCONFIG_REMOTE");
203 imsg_close(ibuf, wbuf);
204 err = got_privsep_flush_imsg(ibuf);
211 branch = repo->branch;
213 err = send_gotconfig_str(ibuf, branch->branch_name);
216 branch = branch->next;
221 err = send_gotconfig_str(ibuf, ref->ref_name);
232 static const struct got_error *
233 validate_config(struct gotconfig *gotconfig)
235 struct gotconfig_remote_repo *repo, *repo2;
236 static char msg[512];
238 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
239 if (repo->name == NULL) {
240 return got_error_msg(GOT_ERR_PARSE_CONFIG,
241 "name required for remote repository");
244 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
246 strcmp(repo->name, repo2->name) != 0)
248 snprintf(msg, sizeof(msg),
249 "duplicate remote repository name '%s'",
251 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
254 if (repo->server == NULL) {
255 snprintf(msg, sizeof(msg),
256 "server required for remote repository \"%s\"",
258 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
261 if (repo->protocol == NULL) {
262 snprintf(msg, sizeof(msg),
263 "protocol required for remote repository \"%s\"",
265 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
267 if (strcmp(repo->protocol, "ssh") != 0 &&
268 strcmp(repo->protocol, "git+ssh") != 0 &&
269 strcmp(repo->protocol, "git") != 0) {
270 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
271 "for remote repository \"%s\"", repo->protocol,
273 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
276 if (repo->repository == NULL) {
277 snprintf(msg, sizeof(msg),
278 "repository path required for remote "
279 "repository \"%s\"", repo->name);
280 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
288 main(int argc, char *argv[])
290 const struct got_error *err = NULL;
292 struct gotconfig *gotconfig = NULL;
294 const char *filename = "got.conf";
301 signal(SIGINT, catch_sigint);
303 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
306 /* revoke access to most system calls */
307 if (pledge("stdio recvfd", NULL) == -1) {
308 err = got_error_from_errno("pledge");
309 got_privsep_send_error(&ibuf, err);
320 memset(&imsg, 0, sizeof(imsg));
323 if (sigint_received) {
324 err = got_error(GOT_ERR_CANCELLED);
328 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
330 if (err->code == GOT_ERR_PRIVSEP_PIPE)
335 if (imsg.hdr.type == GOT_IMSG_STOP)
338 switch (imsg.hdr.type) {
339 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
340 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 err = got_error(GOT_ERR_PRIVSEP_LEN);
346 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
351 gotconfig_free(gotconfig);
352 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
355 err = validate_config(gotconfig);
357 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
358 if (gotconfig == NULL) {
359 err = got_error(GOT_ERR_PRIVSEP_MSG);
362 err = send_gotconfig_str(&ibuf,
363 gotconfig->author ? gotconfig->author : "");
365 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
366 if (gotconfig == NULL) {
367 err = got_error(GOT_ERR_PRIVSEP_MSG);
370 err = send_gotconfig_remotes(&ibuf,
371 &gotconfig->remotes, gotconfig->nremotes);
374 err = got_error(GOT_ERR_PRIVSEP_MSG);
379 if (close(imsg.fd) == -1 && err == NULL)
380 err = got_error_from_errno("close");
390 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
391 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
392 got_privsep_send_error(&ibuf, err);
395 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
396 err = got_error_from_errno("close");