Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/syslimits.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <zlib.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"
43 /* parse.y */
44 static volatile sig_atomic_t sigint_received;
46 static void
47 catch_sigint(int signo)
48 {
49 sigint_received = 1;
50 }
52 static const struct got_error *
53 make_repo_url(char **url, struct gotconfig_remote_repo *repo)
54 {
55 const struct got_error *err = NULL;
56 char *s = NULL, *p = NULL;
58 *url = NULL;
60 if (asprintf(&s, "%s://", repo->protocol) == -1)
61 return got_error_from_errno("asprintf");
63 if (repo->server) {
64 p = s;
65 s = NULL;
66 if (asprintf(&s, "%s%s", p, repo->server) == -1) {
67 err = got_error_from_errno("asprintf");
68 goto done;
69 }
70 free(p);
71 p = NULL;
72 }
74 if (repo->port) {
75 p = s;
76 s = NULL;
77 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
78 err = got_error_from_errno("asprintf");
79 goto done;
80 }
81 free(p);
82 p = NULL;
83 }
85 if (repo->repository) {
86 p = s;
87 s = NULL;
88 if (asprintf(&s, "%s/%s", p, repo->repository) == -1) {
89 err = got_error_from_errno("asprintf");
90 goto done;
91 }
92 free(p);
93 p = NULL;
94 }
95 done:
96 if (err) {
97 free(s);
98 free(p);
99 } else
100 *url = s;
101 return err;
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,
110 value, len) == -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;
123 char *url = NULL;
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);
131 imsg_clear(ibuf);
132 if (err)
133 return err;
135 TAILQ_FOREACH(repo, remotes, entry) {
136 struct got_imsg_remote iremote;
137 size_t len = sizeof(iremote);
138 struct ibuf *wbuf;
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);
146 if (err)
147 break;
148 iremote.url_len = strlen(url);
149 len += iremote.url_len;
151 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
152 if (wbuf == NULL) {
153 err = got_error_from_errno(
154 "imsg_create GOTCONFIG_REMOTE");
155 break;
158 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
159 err = got_error_from_errno(
160 "imsg_add GOTCONFIG_REMOTE");
161 ibuf_free(wbuf);
162 break;
165 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
166 err = got_error_from_errno(
167 "imsg_add GOTCONFIG_REMOTE");
168 ibuf_free(wbuf);
169 break;
171 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
172 err = got_error_from_errno(
173 "imsg_add GOTCONFIG_REMOTE");
174 ibuf_free(wbuf);
175 break;
178 wbuf->fd = -1;
179 imsg_close(ibuf, wbuf);
180 err = got_privsep_flush_imsg(ibuf);
181 if (err)
182 break;
184 free(url);
185 url = NULL;
188 free(url);
189 return err;
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) {
205 if (repo == repo2 ||
206 strcmp(repo->name, repo2->name) != 0)
207 continue;
208 snprintf(msg, sizeof(msg),
209 "duplicate remote repository name '%s'",
210 repo->name);
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\"",
217 repo->name);
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\"",
224 repo->name);
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,
232 repo->name);
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);
244 return NULL;
247 int
248 main(int argc, char *argv[])
250 const struct got_error *err = NULL;
251 struct imsgbuf ibuf;
252 struct gotconfig *gotconfig;
253 size_t datalen;
254 const char *filename = "got.conf";
255 #if 0
256 static int attached;
258 while (!attached)
259 sleep(1);
260 #endif
261 signal(SIGINT, catch_sigint);
263 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
265 #ifndef PROFILE
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);
270 return 1;
272 #endif
274 if (argc > 1)
275 filename = argv[1];
277 for (;;) {
278 struct imsg imsg;
280 memset(&imsg, 0, sizeof(imsg));
281 imsg.fd = -1;
283 if (sigint_received) {
284 err = got_error(GOT_ERR_CANCELLED);
285 break;
288 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
289 if (err) {
290 if (err->code == GOT_ERR_PRIVSEP_PIPE)
291 err = NULL;
292 break;
295 if (imsg.hdr.type == GOT_IMSG_STOP)
296 break;
298 switch (imsg.hdr.type) {
299 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
300 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
301 if (datalen != 0) {
302 err = got_error(GOT_ERR_PRIVSEP_LEN);
303 break;
305 if (imsg.fd == -1){
306 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
307 break;
310 if (gotconfig)
311 gotconfig_free(gotconfig);
312 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
313 if (err)
314 break;
315 err = validate_config(gotconfig);
316 break;
317 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
318 if (gotconfig == NULL) {
319 err = got_error(GOT_ERR_PRIVSEP_MSG);
320 break;
322 err = send_gotconfig_str(&ibuf,
323 gotconfig->author ? gotconfig->author : "");
324 break;
325 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
326 if (gotconfig == NULL) {
327 err = got_error(GOT_ERR_PRIVSEP_MSG);
328 break;
330 err = send_gotconfig_remotes(&ibuf,
331 &gotconfig->remotes, gotconfig->nremotes);
332 break;
333 default:
334 err = got_error(GOT_ERR_PRIVSEP_MSG);
335 break;
338 if (imsg.fd != -1) {
339 if (close(imsg.fd) == -1 && err == NULL)
340 err = got_error_from_errno("close");
343 imsg_free(&imsg);
344 if (err)
345 break;
348 imsg_clear(&ibuf);
349 if (err) {
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");
357 return err ? 1 : 0;