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);
146 iremote.mirror_references = repo->mirror_references;
148 iremote.name_len = strlen(repo->name);
149 len += iremote.name_len;
151 err = make_repo_url(&url, repo);
154 iremote.url_len = strlen(url);
155 len += iremote.url_len;
157 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
159 err = got_error_from_errno(
160 "imsg_create GOTCONFIG_REMOTE");
164 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
165 err = got_error_from_errno(
166 "imsg_add GOTCONFIG_REMOTE");
171 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
172 err = got_error_from_errno(
173 "imsg_add GOTCONFIG_REMOTE");
177 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
178 err = got_error_from_errno(
179 "imsg_add GOTCONFIG_REMOTE");
185 imsg_close(ibuf, wbuf);
186 err = got_privsep_flush_imsg(ibuf);
198 static const struct got_error *
199 validate_config(struct gotconfig *gotconfig)
201 struct gotconfig_remote_repo *repo, *repo2;
202 static char msg[512];
204 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
205 if (repo->name == NULL) {
206 return got_error_msg(GOT_ERR_PARSE_CONFIG,
207 "name required for remote repository");
210 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
212 strcmp(repo->name, repo2->name) != 0)
214 snprintf(msg, sizeof(msg),
215 "duplicate remote repository name '%s'",
217 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
220 if (repo->server == NULL) {
221 snprintf(msg, sizeof(msg),
222 "server required for remote repository \"%s\"",
224 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
227 if (repo->protocol == NULL) {
228 snprintf(msg, sizeof(msg),
229 "protocol required for remote repository \"%s\"",
231 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
233 if (strcmp(repo->protocol, "ssh") != 0 &&
234 strcmp(repo->protocol, "git+ssh") != 0 &&
235 strcmp(repo->protocol, "git") != 0) {
236 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
237 "for remote repository \"%s\"", repo->protocol,
239 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
242 if (repo->repository == NULL) {
243 snprintf(msg, sizeof(msg),
244 "repository path required for remote "
245 "repository \"%s\"", repo->name);
246 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
254 main(int argc, char *argv[])
256 const struct got_error *err = NULL;
258 struct gotconfig *gotconfig = NULL;
260 const char *filename = "got.conf";
267 signal(SIGINT, catch_sigint);
269 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
272 /* revoke access to most system calls */
273 if (pledge("stdio recvfd", NULL) == -1) {
274 err = got_error_from_errno("pledge");
275 got_privsep_send_error(&ibuf, err);
286 memset(&imsg, 0, sizeof(imsg));
289 if (sigint_received) {
290 err = got_error(GOT_ERR_CANCELLED);
294 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
296 if (err->code == GOT_ERR_PRIVSEP_PIPE)
301 if (imsg.hdr.type == GOT_IMSG_STOP)
304 switch (imsg.hdr.type) {
305 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
308 err = got_error(GOT_ERR_PRIVSEP_LEN);
312 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
317 gotconfig_free(gotconfig);
318 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
321 err = validate_config(gotconfig);
323 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
324 if (gotconfig == NULL) {
325 err = got_error(GOT_ERR_PRIVSEP_MSG);
328 err = send_gotconfig_str(&ibuf,
329 gotconfig->author ? gotconfig->author : "");
331 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
332 if (gotconfig == NULL) {
333 err = got_error(GOT_ERR_PRIVSEP_MSG);
336 err = send_gotconfig_remotes(&ibuf,
337 &gotconfig->remotes, gotconfig->nremotes);
340 err = got_error(GOT_ERR_PRIVSEP_MSG);
345 if (close(imsg.fd) == -1 && err == NULL)
346 err = got_error_from_errno("close");
356 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
357 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
358 got_privsep_send_error(&ibuf, err);
361 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
362 err = got_error_from_errno("close");