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/uio.h>
19 #include <sys/time.h>
21 #include <stdint.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_path.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_fetch_url(char **url, struct gotconfig_remote_repo *repo)
54 {
55 const struct got_error *err = NULL;
56 char *s = NULL, *p = NULL;
57 const char *protocol, *server, *repo_path;
58 int port;
60 *url = NULL;
62 if (repo->fetch_config && repo->fetch_config->protocol)
63 protocol = repo->fetch_config->protocol;
64 else
65 protocol = repo->protocol;
66 if (protocol == NULL)
67 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
68 "fetch protocol required for remote repository \"%s\"",
69 repo->name);
70 if (asprintf(&s, "%s://", protocol) == -1)
71 return got_error_from_errno("asprintf");
73 if (repo->fetch_config && repo->fetch_config->server)
74 server = repo->fetch_config->server;
75 else
76 server = repo->server;
77 if (server == NULL)
78 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
79 "fetch server required for remote repository \"%s\"",
80 repo->name);
81 p = s;
82 s = NULL;
83 if (asprintf(&s, "%s%s", p, server) == -1) {
84 err = got_error_from_errno("asprintf");
85 goto done;
86 }
87 free(p);
88 p = NULL;
90 if (repo->fetch_config && repo->fetch_config->server)
91 port = repo->fetch_config->port;
92 else
93 port = repo->port;
94 if (port) {
95 p = s;
96 s = NULL;
97 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
98 err = got_error_from_errno("asprintf");
99 goto done;
101 free(p);
102 p = NULL;
105 if (repo->fetch_config && repo->fetch_config->repository)
106 repo_path = repo->fetch_config->repository;
107 else
108 repo_path = repo->repository;
109 if (repo_path == NULL)
110 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
111 "fetch repository path required for remote "
112 "repository \"%s\"", repo->name);
114 while (repo_path[0] == '/')
115 repo_path++;
116 p = s;
117 s = NULL;
118 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
119 err = got_error_from_errno("asprintf");
120 goto done;
122 free(p);
123 p = NULL;
125 got_path_strip_trailing_slashes(s);
126 done:
127 if (err) {
128 free(s);
129 free(p);
130 } else
131 *url = s;
132 return err;
135 static const struct got_error *
136 make_send_url(char **url, struct gotconfig_remote_repo *repo)
138 const struct got_error *err = NULL;
139 char *s = NULL, *p = NULL;
140 const char *protocol, *server, *repo_path;
141 int port;
143 *url = NULL;
145 if (repo->send_config && repo->send_config->protocol)
146 protocol = repo->send_config->protocol;
147 else
148 protocol = repo->protocol;
149 if (protocol == NULL)
150 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
151 "send protocol required for remote repository \"%s\"",
152 repo->name);
153 if (asprintf(&s, "%s://", protocol) == -1)
154 return got_error_from_errno("asprintf");
156 if (repo->send_config && repo->send_config->server)
157 server = repo->send_config->server;
158 else
159 server = repo->server;
160 if (server == NULL)
161 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
162 "send server required for remote repository \"%s\"",
163 repo->name);
164 p = s;
165 s = NULL;
166 if (asprintf(&s, "%s%s", p, server) == -1) {
167 err = got_error_from_errno("asprintf");
168 goto done;
170 free(p);
171 p = NULL;
173 if (repo->send_config && repo->send_config->server)
174 port = repo->send_config->port;
175 else
176 port = repo->port;
177 if (port) {
178 p = s;
179 s = NULL;
180 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
181 err = got_error_from_errno("asprintf");
182 goto done;
184 free(p);
185 p = NULL;
188 if (repo->send_config && repo->send_config->repository)
189 repo_path = repo->send_config->repository;
190 else
191 repo_path = repo->repository;
192 if (repo_path == NULL)
193 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
194 "send repository path required for remote "
195 "repository \"%s\"", repo->name);
197 while (repo_path[0] == '/')
198 repo_path++;
199 p = s;
200 s = NULL;
201 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
202 err = got_error_from_errno("asprintf");
203 goto done;
205 free(p);
206 p = NULL;
208 got_path_strip_trailing_slashes(s);
209 done:
210 if (err) {
211 free(s);
212 free(p);
213 } else
214 *url = s;
215 return err;
218 static const struct got_error *
219 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
221 size_t len = value ? strlen(value) : 0;
223 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
224 value, len) == -1)
225 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
227 return got_privsep_flush_imsg(ibuf);
230 static const struct got_error *
231 send_gotconfig_remotes(struct imsgbuf *ibuf,
232 struct gotconfig_remote_repo_list *remotes, int nremotes)
234 const struct got_error *err = NULL;
235 struct got_imsg_remotes iremotes;
236 struct gotconfig_remote_repo *repo;
237 char *fetch_url = NULL, *send_url = NULL;
239 iremotes.nremotes = nremotes;
240 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
241 &iremotes, sizeof(iremotes)) == -1)
242 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
244 err = got_privsep_flush_imsg(ibuf);
245 imsg_clear(ibuf);
246 if (err)
247 return err;
249 TAILQ_FOREACH(repo, remotes, entry) {
250 struct got_imsg_remote iremote;
251 size_t len = sizeof(iremote);
252 struct ibuf *wbuf;
253 struct node_branch *branch;
254 struct node_ref *ref;
255 int nfetch_branches = 0, nsend_branches = 0, nfetch_refs = 0;
257 if (repo->fetch_config && repo->fetch_config->branch)
258 branch = repo->fetch_config->branch;
259 else
260 branch = repo->branch;
261 while (branch) {
262 branch = branch->next;
263 nfetch_branches++;
266 if (repo->send_config && repo->send_config->branch)
267 branch = repo->send_config->branch;
268 else
269 branch = repo->branch;
270 while (branch) {
271 branch = branch->next;
272 nsend_branches++;
275 ref = repo->fetch_ref;
276 while (ref) {
277 ref = ref->next;
278 nfetch_refs++;
281 iremote.nfetch_branches = nfetch_branches;
282 iremote.nsend_branches = nsend_branches;
283 iremote.nfetch_refs = nfetch_refs;
284 iremote.mirror_references = repo->mirror_references;
285 iremote.fetch_all_branches = repo->fetch_all_branches;
287 iremote.name_len = strlen(repo->name);
288 len += iremote.name_len;
290 err = make_fetch_url(&fetch_url, repo);
291 if (err)
292 break;
293 iremote.fetch_url_len = strlen(fetch_url);
294 len += iremote.fetch_url_len;
296 err = make_send_url(&send_url, repo);
297 if (err)
298 break;
299 iremote.send_url_len = strlen(send_url);
300 len += iremote.send_url_len;
302 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
303 if (wbuf == NULL) {
304 err = got_error_from_errno(
305 "imsg_create GOTCONFIG_REMOTE");
306 break;
309 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
310 err = got_error_from_errno(
311 "imsg_add GOTCONFIG_REMOTE");
312 break;
315 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
316 err = got_error_from_errno(
317 "imsg_add GOTCONFIG_REMOTE");
318 break;
320 if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
321 err = got_error_from_errno(
322 "imsg_add GOTCONFIG_REMOTE");
323 break;
325 if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
326 err = got_error_from_errno(
327 "imsg_add GOTCONFIG_REMOTE");
328 break;
331 wbuf->fd = -1;
332 imsg_close(ibuf, wbuf);
333 err = got_privsep_flush_imsg(ibuf);
334 if (err)
335 break;
337 free(fetch_url);
338 fetch_url = NULL;
339 free(send_url);
340 send_url = NULL;
342 if (repo->fetch_config && repo->fetch_config->branch)
343 branch = repo->fetch_config->branch;
344 else
345 branch = repo->branch;
346 while (branch) {
347 err = send_gotconfig_str(ibuf, branch->branch_name);
348 if (err)
349 break;
350 branch = branch->next;
353 if (repo->send_config && repo->send_config->branch)
354 branch = repo->send_config->branch;
355 else
356 branch = repo->branch;
357 while (branch) {
358 err = send_gotconfig_str(ibuf, branch->branch_name);
359 if (err)
360 break;
361 branch = branch->next;
364 ref = repo->fetch_ref;
365 while (ref) {
366 err = send_gotconfig_str(ibuf, ref->ref_name);
367 if (err)
368 break;
369 ref = ref->next;
373 free(fetch_url);
374 free(send_url);
375 return err;
378 static const struct got_error *
379 validate_protocol(const char *protocol, const char *repo_name)
381 static char msg[512];
383 if (strcmp(protocol, "ssh") != 0 &&
384 strcmp(protocol, "git+ssh") != 0 &&
385 strcmp(protocol, "git") != 0) {
386 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
387 "for remote repository \"%s\"", protocol, repo_name);
388 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
391 return NULL;
394 static const struct got_error *
395 validate_config(struct gotconfig *gotconfig)
397 const struct got_error *err;
398 struct gotconfig_remote_repo *repo, *repo2;
399 static char msg[512];
401 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
402 if (repo->name == NULL) {
403 return got_error_msg(GOT_ERR_PARSE_CONFIG,
404 "name required for remote repository");
407 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
408 if (repo == repo2 ||
409 strcmp(repo->name, repo2->name) != 0)
410 continue;
411 snprintf(msg, sizeof(msg),
412 "duplicate remote repository name '%s'",
413 repo->name);
414 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
417 if (repo->server == NULL &&
418 (repo->fetch_config == NULL ||
419 repo->fetch_config->server == NULL) &&
420 (repo->send_config == NULL ||
421 repo->send_config->server == NULL)) {
422 snprintf(msg, sizeof(msg),
423 "server required for remote repository \"%s\"",
424 repo->name);
425 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
428 if (repo->protocol == NULL &&
429 (repo->fetch_config == NULL ||
430 repo->fetch_config->protocol == NULL) &&
431 (repo->send_config == NULL ||
432 repo->send_config->protocol == NULL)) {
433 snprintf(msg, sizeof(msg),
434 "protocol required for remote repository \"%s\"",
435 repo->name);
436 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
439 if (repo->protocol) {
440 err = validate_protocol(repo->protocol, repo->name);
441 if (err)
442 return err;
444 if (repo->fetch_config && repo->fetch_config->protocol) {
445 err = validate_protocol(repo->fetch_config->protocol,
446 repo->name);
447 if (err)
448 return err;
450 if (repo->send_config && repo->send_config->protocol) {
451 err = validate_protocol(repo->send_config->protocol,
452 repo->name);
453 if (err)
454 return err;
457 if (repo->repository == NULL &&
458 (repo->fetch_config == NULL ||
459 repo->fetch_config->repository == NULL) &&
460 (repo->send_config == NULL ||
461 repo->send_config->repository == NULL)) {
462 snprintf(msg, sizeof(msg),
463 "repository path required for remote "
464 "repository \"%s\"", repo->name);
465 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
469 return NULL;
472 int
473 main(int argc, char *argv[])
475 const struct got_error *err = NULL;
476 struct imsgbuf ibuf;
477 struct gotconfig *gotconfig = NULL;
478 size_t datalen;
479 const char *filename = "got.conf";
480 #if 0
481 static int attached;
483 while (!attached)
484 sleep(1);
485 #endif
486 signal(SIGINT, catch_sigint);
488 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
490 #ifndef PROFILE
491 /* revoke access to most system calls */
492 if (pledge("stdio recvfd", NULL) == -1) {
493 err = got_error_from_errno("pledge");
494 got_privsep_send_error(&ibuf, err);
495 return 1;
498 /* revoke fs access */
499 if (landlock_no_fs() == -1) {
500 err = got_error_from_errno("landlock_no_fs");
501 got_privsep_send_error(&ibuf, err);
502 return 1;
504 if (cap_enter() == -1) {
505 err = got_error_from_errno("cap_enter");
506 got_privsep_send_error(&ibuf, err);
507 return 1;
509 #endif
511 if (argc > 1)
512 filename = argv[1];
514 for (;;) {
515 struct imsg imsg;
517 memset(&imsg, 0, sizeof(imsg));
518 imsg.fd = -1;
520 if (sigint_received) {
521 err = got_error(GOT_ERR_CANCELLED);
522 break;
525 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
526 if (err) {
527 if (err->code == GOT_ERR_PRIVSEP_PIPE)
528 err = NULL;
529 break;
532 if (imsg.hdr.type == GOT_IMSG_STOP)
533 break;
535 switch (imsg.hdr.type) {
536 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
537 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
538 if (datalen != 0) {
539 err = got_error(GOT_ERR_PRIVSEP_LEN);
540 break;
542 if (imsg.fd == -1){
543 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
544 break;
547 if (gotconfig)
548 gotconfig_free(gotconfig);
549 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
550 if (err)
551 break;
552 err = validate_config(gotconfig);
553 break;
554 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
555 if (gotconfig == NULL) {
556 err = got_error(GOT_ERR_PRIVSEP_MSG);
557 break;
559 err = send_gotconfig_str(&ibuf,
560 gotconfig->author ? gotconfig->author : "");
561 break;
562 case GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST:
563 if (gotconfig == NULL) {
564 err = got_error(GOT_ERR_PRIVSEP_MSG);
565 break;
567 err = send_gotconfig_str(&ibuf,
568 gotconfig->allowed_signers_file ?
569 gotconfig->allowed_signers_file : "");
570 break;
571 case GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST:
572 if (gotconfig == NULL) {
573 err = got_error(GOT_ERR_PRIVSEP_MSG);
574 break;
576 err = send_gotconfig_str(&ibuf,
577 gotconfig->revoked_signers_file ?
578 gotconfig->revoked_signers_file : "");
579 break;
580 case GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST:
581 if (gotconfig == NULL) {
582 err = got_error(GOT_ERR_PRIVSEP_MSG);
583 break;
585 err = send_gotconfig_str(&ibuf,
586 gotconfig->signer_id ? gotconfig->signer_id : "");
587 break;
588 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
589 if (gotconfig == NULL) {
590 err = got_error(GOT_ERR_PRIVSEP_MSG);
591 break;
593 err = send_gotconfig_remotes(&ibuf,
594 &gotconfig->remotes, gotconfig->nremotes);
595 break;
596 default:
597 err = got_error(GOT_ERR_PRIVSEP_MSG);
598 break;
601 if (imsg.fd != -1) {
602 if (close(imsg.fd) == -1 && err == NULL)
603 err = got_error_from_errno("close");
606 imsg_free(&imsg);
607 if (err)
608 break;
611 imsg_clear(&ibuf);
612 if (err) {
613 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
614 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
615 got_privsep_send_error(&ibuf, err);
618 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
619 err = got_error_from_errno("close");
620 return err ? 1 : 0;