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>
22 #include <stdint.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <sha2.h>
31 #include <unistd.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_path.h"
37 #include "got_repository.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_privsep.h"
43 #include "gotconfig.h"
45 /* parse.y */
46 static volatile sig_atomic_t sigint_received;
48 static void
49 catch_sigint(int signo)
50 {
51 sigint_received = 1;
52 }
54 static const struct got_error *
55 make_fetch_url(char **url, struct gotconfig_remote_repo *repo)
56 {
57 const struct got_error *err = NULL;
58 char *s = NULL, *p = NULL;
59 const char *protocol, *server, *repo_path;
60 int port;
62 *url = NULL;
64 if (repo->fetch_config && repo->fetch_config->protocol)
65 protocol = repo->fetch_config->protocol;
66 else
67 protocol = repo->protocol;
68 if (protocol == NULL)
69 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
70 "fetch protocol required for remote repository \"%s\"",
71 repo->name);
72 if (asprintf(&s, "%s://", protocol) == -1)
73 return got_error_from_errno("asprintf");
75 if (repo->fetch_config && repo->fetch_config->server)
76 server = repo->fetch_config->server;
77 else
78 server = repo->server;
79 if (server == NULL)
80 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
81 "fetch server required for remote repository \"%s\"",
82 repo->name);
83 p = s;
84 s = NULL;
85 if (asprintf(&s, "%s%s", p, server) == -1) {
86 err = got_error_from_errno("asprintf");
87 goto done;
88 }
89 free(p);
90 p = NULL;
92 if (repo->fetch_config && repo->fetch_config->server)
93 port = repo->fetch_config->port;
94 else
95 port = repo->port;
96 if (port) {
97 p = s;
98 s = NULL;
99 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
100 err = got_error_from_errno("asprintf");
101 goto done;
103 free(p);
104 p = NULL;
107 if (repo->fetch_config && repo->fetch_config->repository)
108 repo_path = repo->fetch_config->repository;
109 else
110 repo_path = repo->repository;
111 if (repo_path == NULL)
112 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
113 "fetch repository path required for remote "
114 "repository \"%s\"", repo->name);
116 while (repo_path[0] == '/')
117 repo_path++;
118 p = s;
119 s = NULL;
120 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
121 err = got_error_from_errno("asprintf");
122 goto done;
124 free(p);
125 p = NULL;
127 got_path_strip_trailing_slashes(s);
128 done:
129 if (err) {
130 free(s);
131 free(p);
132 } else
133 *url = s;
134 return err;
137 static const struct got_error *
138 make_send_url(char **url, struct gotconfig_remote_repo *repo)
140 const struct got_error *err = NULL;
141 char *s = NULL, *p = NULL;
142 const char *protocol, *server, *repo_path;
143 int port;
145 *url = NULL;
147 if (repo->send_config && repo->send_config->protocol)
148 protocol = repo->send_config->protocol;
149 else
150 protocol = repo->protocol;
151 if (protocol == NULL)
152 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
153 "send protocol required for remote repository \"%s\"",
154 repo->name);
155 if (asprintf(&s, "%s://", protocol) == -1)
156 return got_error_from_errno("asprintf");
158 if (repo->send_config && repo->send_config->server)
159 server = repo->send_config->server;
160 else
161 server = repo->server;
162 if (server == NULL)
163 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
164 "send server required for remote repository \"%s\"",
165 repo->name);
166 p = s;
167 s = NULL;
168 if (asprintf(&s, "%s%s", p, server) == -1) {
169 err = got_error_from_errno("asprintf");
170 goto done;
172 free(p);
173 p = NULL;
175 if (repo->send_config && repo->send_config->server)
176 port = repo->send_config->port;
177 else
178 port = repo->port;
179 if (port) {
180 p = s;
181 s = NULL;
182 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
183 err = got_error_from_errno("asprintf");
184 goto done;
186 free(p);
187 p = NULL;
190 if (repo->send_config && repo->send_config->repository)
191 repo_path = repo->send_config->repository;
192 else
193 repo_path = repo->repository;
194 if (repo_path == NULL)
195 return got_error_fmt(GOT_ERR_PARSE_CONFIG,
196 "send repository path required for remote "
197 "repository \"%s\"", repo->name);
199 while (repo_path[0] == '/')
200 repo_path++;
201 p = s;
202 s = NULL;
203 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
204 err = got_error_from_errno("asprintf");
205 goto done;
207 free(p);
208 p = NULL;
210 got_path_strip_trailing_slashes(s);
211 done:
212 if (err) {
213 free(s);
214 free(p);
215 } else
216 *url = s;
217 return err;
220 static const struct got_error *
221 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
223 size_t len = value ? strlen(value) : 0;
225 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
226 value, len) == -1)
227 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
229 return got_privsep_flush_imsg(ibuf);
232 static const struct got_error *
233 send_gotconfig_remotes(struct imsgbuf *ibuf,
234 struct gotconfig_remote_repo_list *remotes, int nremotes)
236 const struct got_error *err = NULL;
237 struct got_imsg_remotes iremotes;
238 struct gotconfig_remote_repo *repo;
239 char *fetch_url = NULL, *send_url = NULL;
241 iremotes.nremotes = nremotes;
242 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
243 &iremotes, sizeof(iremotes)) == -1)
244 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
246 err = got_privsep_flush_imsg(ibuf);
247 imsg_clear(ibuf);
248 if (err)
249 return err;
251 TAILQ_FOREACH(repo, remotes, entry) {
252 struct got_imsg_remote iremote;
253 size_t len = sizeof(iremote);
254 struct ibuf *wbuf;
255 struct node_branch *branch;
256 struct node_ref *ref;
257 int nfetch_branches = 0, nsend_branches = 0, nfetch_refs = 0;
259 if (repo->fetch_config && repo->fetch_config->branch)
260 branch = repo->fetch_config->branch;
261 else
262 branch = repo->branch;
263 while (branch) {
264 branch = branch->next;
265 nfetch_branches++;
268 if (repo->send_config && repo->send_config->branch)
269 branch = repo->send_config->branch;
270 else
271 branch = repo->branch;
272 while (branch) {
273 branch = branch->next;
274 nsend_branches++;
277 ref = repo->fetch_ref;
278 while (ref) {
279 ref = ref->next;
280 nfetch_refs++;
283 iremote.nfetch_branches = nfetch_branches;
284 iremote.nsend_branches = nsend_branches;
285 iremote.nfetch_refs = nfetch_refs;
286 iremote.mirror_references = repo->mirror_references;
287 iremote.fetch_all_branches = repo->fetch_all_branches;
289 iremote.name_len = strlen(repo->name);
290 len += iremote.name_len;
292 err = make_fetch_url(&fetch_url, repo);
293 if (err)
294 break;
295 iremote.fetch_url_len = strlen(fetch_url);
296 len += iremote.fetch_url_len;
298 err = make_send_url(&send_url, repo);
299 if (err)
300 break;
301 iremote.send_url_len = strlen(send_url);
302 len += iremote.send_url_len;
304 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
305 if (wbuf == NULL) {
306 err = got_error_from_errno(
307 "imsg_create GOTCONFIG_REMOTE");
308 break;
311 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
312 err = got_error_from_errno(
313 "imsg_add GOTCONFIG_REMOTE");
314 break;
317 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
318 err = got_error_from_errno(
319 "imsg_add GOTCONFIG_REMOTE");
320 break;
322 if (imsg_add(wbuf, fetch_url, iremote.fetch_url_len) == -1) {
323 err = got_error_from_errno(
324 "imsg_add GOTCONFIG_REMOTE");
325 break;
327 if (imsg_add(wbuf, send_url, iremote.send_url_len) == -1) {
328 err = got_error_from_errno(
329 "imsg_add GOTCONFIG_REMOTE");
330 break;
333 imsg_close(ibuf, wbuf);
334 err = got_privsep_flush_imsg(ibuf);
335 if (err)
336 break;
338 free(fetch_url);
339 fetch_url = NULL;
340 free(send_url);
341 send_url = NULL;
343 if (repo->fetch_config && repo->fetch_config->branch)
344 branch = repo->fetch_config->branch;
345 else
346 branch = repo->branch;
347 while (branch) {
348 err = send_gotconfig_str(ibuf, branch->branch_name);
349 if (err)
350 break;
351 branch = branch->next;
354 if (repo->send_config && repo->send_config->branch)
355 branch = repo->send_config->branch;
356 else
357 branch = repo->branch;
358 while (branch) {
359 err = send_gotconfig_str(ibuf, branch->branch_name);
360 if (err)
361 break;
362 branch = branch->next;
365 ref = repo->fetch_ref;
366 while (ref) {
367 err = send_gotconfig_str(ibuf, ref->ref_name);
368 if (err)
369 break;
370 ref = ref->next;
374 free(fetch_url);
375 free(send_url);
376 return err;
379 static const struct got_error *
380 validate_protocol(const char *protocol, const char *repo_name)
382 static char msg[512];
384 if (strcmp(protocol, "ssh") != 0 &&
385 strcmp(protocol, "git+ssh") != 0 &&
386 strcmp(protocol, "git") != 0 &&
387 strcmp(protocol, "git+http") != 0 &&
388 strcmp(protocol, "http") != 0 &&
389 strcmp(protocol, "https") != 0 &&
390 strcmp(protocol, "git+https") != 0) {
391 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
392 "for remote repository \"%s\"", protocol, repo_name);
393 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
396 return NULL;
399 static const struct got_error *
400 validate_config(struct gotconfig *gotconfig)
402 const struct got_error *err;
403 struct gotconfig_remote_repo *repo, *repo2;
404 static char msg[512];
406 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
407 if (repo->name == NULL) {
408 return got_error_msg(GOT_ERR_PARSE_CONFIG,
409 "name required for remote repository");
412 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
413 if (repo == repo2 ||
414 strcmp(repo->name, repo2->name) != 0)
415 continue;
416 snprintf(msg, sizeof(msg),
417 "duplicate remote repository name '%s'",
418 repo->name);
419 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
422 if (repo->server == NULL &&
423 (repo->fetch_config == NULL ||
424 repo->fetch_config->server == NULL) &&
425 (repo->send_config == NULL ||
426 repo->send_config->server == NULL)) {
427 snprintf(msg, sizeof(msg),
428 "server required for remote repository \"%s\"",
429 repo->name);
430 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
433 if (repo->protocol == NULL &&
434 (repo->fetch_config == NULL ||
435 repo->fetch_config->protocol == NULL) &&
436 (repo->send_config == NULL ||
437 repo->send_config->protocol == NULL)) {
438 snprintf(msg, sizeof(msg),
439 "protocol required for remote repository \"%s\"",
440 repo->name);
441 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
444 if (repo->protocol) {
445 err = validate_protocol(repo->protocol, repo->name);
446 if (err)
447 return err;
449 if (repo->fetch_config && repo->fetch_config->protocol) {
450 err = validate_protocol(repo->fetch_config->protocol,
451 repo->name);
452 if (err)
453 return err;
455 if (repo->send_config && repo->send_config->protocol) {
456 err = validate_protocol(repo->send_config->protocol,
457 repo->name);
458 if (err)
459 return err;
462 if (repo->repository == NULL &&
463 (repo->fetch_config == NULL ||
464 repo->fetch_config->repository == NULL) &&
465 (repo->send_config == NULL ||
466 repo->send_config->repository == NULL)) {
467 snprintf(msg, sizeof(msg),
468 "repository path required for remote "
469 "repository \"%s\"", repo->name);
470 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
474 return NULL;
477 int
478 main(int argc, char *argv[])
480 const struct got_error *err = NULL;
481 struct imsgbuf ibuf;
482 struct gotconfig *gotconfig = NULL;
483 size_t datalen;
484 const char *filename = "got.conf";
485 #if 0
486 static int attached;
488 while (!attached)
489 sleep(1);
490 #endif
491 signal(SIGINT, catch_sigint);
493 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
495 #ifndef PROFILE
496 /* revoke access to most system calls */
497 if (pledge("stdio recvfd", NULL) == -1) {
498 err = got_error_from_errno("pledge");
499 got_privsep_send_error(&ibuf, err);
500 return 1;
502 #endif
504 if (argc > 1)
505 filename = argv[1];
507 for (;;) {
508 struct imsg imsg;
509 int fd = -1;
511 memset(&imsg, 0, sizeof(imsg));
513 if (sigint_received) {
514 err = got_error(GOT_ERR_CANCELLED);
515 break;
518 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
519 if (err) {
520 if (err->code == GOT_ERR_PRIVSEP_PIPE)
521 err = NULL;
522 break;
525 if (imsg.hdr.type == GOT_IMSG_STOP)
526 break;
528 switch (imsg.hdr.type) {
529 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
530 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
531 if (datalen != 0) {
532 err = got_error(GOT_ERR_PRIVSEP_LEN);
533 break;
535 fd = imsg_get_fd(&imsg);
536 if (fd == -1){
537 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
538 break;
541 if (gotconfig)
542 gotconfig_free(gotconfig);
543 err = gotconfig_parse(&gotconfig, filename, &fd);
544 if (err)
545 break;
546 err = validate_config(gotconfig);
547 break;
548 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
549 if (gotconfig == NULL) {
550 err = got_error(GOT_ERR_PRIVSEP_MSG);
551 break;
553 err = send_gotconfig_str(&ibuf,
554 gotconfig->author ? gotconfig->author : "");
555 break;
556 case GOT_IMSG_GOTCONFIG_ALLOWEDSIGNERS_REQUEST:
557 if (gotconfig == NULL) {
558 err = got_error(GOT_ERR_PRIVSEP_MSG);
559 break;
561 err = send_gotconfig_str(&ibuf,
562 gotconfig->allowed_signers_file ?
563 gotconfig->allowed_signers_file : "");
564 break;
565 case GOT_IMSG_GOTCONFIG_REVOKEDSIGNERS_REQUEST:
566 if (gotconfig == NULL) {
567 err = got_error(GOT_ERR_PRIVSEP_MSG);
568 break;
570 err = send_gotconfig_str(&ibuf,
571 gotconfig->revoked_signers_file ?
572 gotconfig->revoked_signers_file : "");
573 break;
574 case GOT_IMSG_GOTCONFIG_SIGNERID_REQUEST:
575 if (gotconfig == NULL) {
576 err = got_error(GOT_ERR_PRIVSEP_MSG);
577 break;
579 err = send_gotconfig_str(&ibuf,
580 gotconfig->signer_id ? gotconfig->signer_id : "");
581 break;
582 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
583 if (gotconfig == NULL) {
584 err = got_error(GOT_ERR_PRIVSEP_MSG);
585 break;
587 err = send_gotconfig_remotes(&ibuf,
588 &gotconfig->remotes, gotconfig->nremotes);
589 break;
590 default:
591 err = got_error(GOT_ERR_PRIVSEP_MSG);
592 break;
595 if (fd != -1) {
596 if (close(fd) == -1 && err == NULL)
597 err = got_error_from_errno("close");
600 imsg_free(&imsg);
601 if (err)
602 break;
605 imsg_clear(&ibuf);
606 if (err) {
607 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
608 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
609 got_privsep_send_error(&ibuf, err);
612 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
613 err = got_error_from_errno("close");
614 return err ? 1 : 0;