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>
21 #include <sys/syslimits.h>
33 #include "got_error.h"
34 #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_repo_url(char **url, struct gotconfig_remote_repo *repo)
55 const struct got_error *err = NULL;
56 char *s = NULL, *p = NULL;
60 if (asprintf(&s, "%s://", repo->protocol) == -1)
61 return got_error_from_errno("asprintf");
66 if (asprintf(&s, "%s%s", p, repo->server) == -1) {
67 err = got_error_from_errno("asprintf");
77 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
78 err = got_error_from_errno("asprintf");
85 if (repo->repository) {
88 if (asprintf(&s, "%s/%s", p, repo->repository) == -1) {
89 err = got_error_from_errno("asprintf");
104 static const struct got_error *
105 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
107 size_t len = value ? strlen(value) + 1 : 0;
109 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
111 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
113 return got_privsep_flush_imsg(ibuf);
116 static const struct got_error *
117 send_gotconfig_remotes(struct imsgbuf *ibuf,
118 struct gotconfig_remote_repo_list *remotes, int nremotes)
120 const struct got_error *err = NULL;
121 struct got_imsg_remotes iremotes;
122 struct gotconfig_remote_repo *repo;
125 iremotes.nremotes = nremotes;
126 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
127 &iremotes, sizeof(iremotes)) == -1)
128 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
130 err = got_privsep_flush_imsg(ibuf);
135 TAILQ_FOREACH(repo, remotes, entry) {
136 struct got_imsg_remote iremote;
137 size_t len = sizeof(iremote);
140 iremote.mirror_references = repo->mirror_references;
142 iremote.name_len = strlen(repo->name);
143 len += iremote.name_len;
145 err = make_repo_url(&url, repo);
148 iremote.url_len = strlen(url);
149 len += iremote.url_len;
151 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
153 err = got_error_from_errno(
154 "imsg_create GOTCONFIG_REMOTE");
158 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
159 err = got_error_from_errno(
160 "imsg_add GOTCONFIG_REMOTE");
165 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
166 err = got_error_from_errno(
167 "imsg_add GOTCONFIG_REMOTE");
171 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
172 err = got_error_from_errno(
173 "imsg_add GOTCONFIG_REMOTE");
179 imsg_close(ibuf, wbuf);
180 err = got_privsep_flush_imsg(ibuf);
192 static const struct got_error *
193 validate_config(struct gotconfig *gotconfig)
195 struct gotconfig_remote_repo *repo, *repo2;
196 static char msg[512];
198 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
199 if (repo->name == NULL) {
200 return got_error_msg(GOT_ERR_PARSE_CONFIG,
201 "name required for remote repository");
204 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
206 strcmp(repo->name, repo2->name) != 0)
208 snprintf(msg, sizeof(msg),
209 "duplicate remote repository name '%s'",
211 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
214 if (repo->server == NULL) {
215 snprintf(msg, sizeof(msg),
216 "server required for remote repository \"%s\"",
218 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
221 if (repo->protocol == NULL) {
222 snprintf(msg, sizeof(msg),
223 "protocol required for remote repository \"%s\"",
225 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
227 if (strcmp(repo->protocol, "ssh") != 0 &&
228 strcmp(repo->protocol, "git+ssh") != 0 &&
229 strcmp(repo->protocol, "git") != 0) {
230 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
231 "for remote repository \"%s\"", repo->protocol,
233 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
236 if (repo->repository == NULL) {
237 snprintf(msg, sizeof(msg),
238 "repository path required for remote "
239 "repository \"%s\"", repo->name);
240 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
248 main(int argc, char *argv[])
250 const struct got_error *err = NULL;
252 struct gotconfig *gotconfig;
254 const char *filename = "got.conf";
261 signal(SIGINT, catch_sigint);
263 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
266 /* revoke access to most system calls */
267 if (pledge("stdio recvfd", NULL) == -1) {
268 err = got_error_from_errno("pledge");
269 got_privsep_send_error(&ibuf, err);
280 memset(&imsg, 0, sizeof(imsg));
283 if (sigint_received) {
284 err = got_error(GOT_ERR_CANCELLED);
288 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
290 if (err->code == GOT_ERR_PRIVSEP_PIPE)
295 if (imsg.hdr.type == GOT_IMSG_STOP)
298 switch (imsg.hdr.type) {
299 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
300 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
302 err = got_error(GOT_ERR_PRIVSEP_LEN);
306 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
311 gotconfig_free(gotconfig);
312 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
315 err = validate_config(gotconfig);
317 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
318 if (gotconfig == NULL) {
319 err = got_error(GOT_ERR_PRIVSEP_MSG);
322 err = send_gotconfig_str(&ibuf,
323 gotconfig->author ? gotconfig->author : "");
325 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
326 if (gotconfig == NULL) {
327 err = got_error(GOT_ERR_PRIVSEP_MSG);
330 err = send_gotconfig_remotes(&ibuf,
331 &gotconfig->remotes, gotconfig->nremotes);
334 err = got_error(GOT_ERR_PRIVSEP_MSG);
339 if (close(imsg.fd) == -1 && err == NULL)
340 err = got_error_from_errno("close");
350 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
351 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
352 got_privsep_send_error(&ibuf, err);
355 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
356 err = got_error_from_errno("close");