Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/time.h>
22 #include <sys/stat.h>
24 #include <stdint.h>
25 #include <errno.h>
26 #include <imsg.h>
27 #include <limits.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <sha1.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <err.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_version.h"
43 #include "got_fetch.h"
44 #include "got_reference.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_pkt.h"
53 #include "got_lib_gitproto.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 struct got_object *indexed;
60 static int chattygot;
62 static const struct got_capability got_capabilities[] = {
63 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
64 { GOT_CAPA_OFS_DELTA, NULL },
65 #if 0
66 { GOT_CAPA_SIDE_BAND_64K, NULL },
67 #endif
68 { GOT_CAPA_REPORT_STATUS, NULL },
69 { GOT_CAPA_DELETE_REFS, NULL },
70 };
72 static const struct got_error *
73 send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
74 {
75 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
76 &bytes, sizeof(bytes)) == -1)
77 return got_error_from_errno(
78 "imsg_compose SEND_UPLOAD_PROGRESS");
80 return got_privsep_flush_imsg(ibuf);
81 }
83 static const struct got_error *
84 send_pack_request(struct imsgbuf *ibuf)
85 {
86 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
87 NULL, 0) == -1)
88 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
89 return got_privsep_flush_imsg(ibuf);
90 }
92 static const struct got_error *
93 send_done(struct imsgbuf *ibuf)
94 {
95 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
96 return got_error_from_errno("imsg_compose SEND_DONE");
97 return got_privsep_flush_imsg(ibuf);
98 }
100 static const struct got_error *
101 recv_packfd(int *packfd, struct imsgbuf *ibuf)
103 const struct got_error *err;
104 struct imsg imsg;
106 *packfd = -1;
108 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
109 if (err)
110 return err;
112 if (imsg.hdr.type == GOT_IMSG_STOP) {
113 err = got_error(GOT_ERR_CANCELLED);
114 goto done;
117 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
118 err = got_error(GOT_ERR_PRIVSEP_MSG);
119 goto done;
122 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
123 err = got_error(GOT_ERR_PRIVSEP_LEN);
124 goto done;
127 *packfd = imsg.fd;
128 done:
129 imsg_free(&imsg);
130 return err;
133 static const struct got_error *
134 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
136 const struct got_error *err;
137 unsigned char buf[8192];
138 ssize_t r, w;
139 off_t wtotal = 0;
141 if (lseek(packfd, 0L, SEEK_SET) == -1)
142 return got_error_from_errno("lseek");
144 for (;;) {
145 r = read(packfd, buf, sizeof(buf));
146 if (r == -1)
147 return got_error_from_errno("read");
148 if (r == 0)
149 break;
150 w = write(sendfd, buf, r);
151 if (w == -1)
152 return got_error_from_errno("write");
153 if (w != r)
154 return got_error(GOT_ERR_IO);
155 wtotal += w;
156 err = send_upload_progress(ibuf, wtotal);
157 if (err)
158 return err;
161 return NULL;
164 static const struct got_error *
165 send_error(const char *buf, size_t len)
167 static char msg[1024];
168 size_t i;
170 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
171 if (!isprint(buf[i]))
172 return got_error_msg(GOT_ERR_BAD_PACKET,
173 "non-printable error message received from server");
174 msg[i] = buf[i];
176 msg[i] = '\0';
177 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
180 static const struct got_error *
181 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
182 const char *refname)
184 const struct got_error *err = NULL;
185 struct ibuf *wbuf;
186 size_t len, reflen = strlen(refname);
188 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
189 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 return got_error(GOT_ERR_NO_SPACE);
192 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
193 if (wbuf == NULL)
194 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
196 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
197 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
198 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
199 ibuf_free(wbuf);
200 return err;
202 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
203 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
204 ibuf_free(wbuf);
205 return err;
207 if (imsg_add(wbuf, refname, reflen) == -1) {
208 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
209 ibuf_free(wbuf);
210 return err;
213 wbuf->fd = -1;
214 imsg_close(ibuf, wbuf);
215 return got_privsep_flush_imsg(ibuf);
218 static const struct got_error *
219 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
220 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
222 const struct got_error *err = NULL;
223 struct ibuf *wbuf;
224 size_t len, reflen = strlen(refname);
225 struct got_pathlist_entry *pe;
226 int ref_valid = 0;
227 char *eol;
229 eol = strchr(refname, '\n');
230 if (eol == NULL) {
231 return got_error_msg(GOT_ERR_BAD_PACKET,
232 "unexpected message from server");
234 *eol = '\0';
236 TAILQ_FOREACH(pe, refs, entry) {
237 if (strcmp(refname, pe->path) == 0) {
238 ref_valid = 1;
239 break;
242 if (!ref_valid) {
243 TAILQ_FOREACH(pe, delete_refs, entry) {
244 if (strcmp(refname, pe->path) == 0) {
245 ref_valid = 1;
246 break;
250 if (!ref_valid) {
251 return got_error_msg(GOT_ERR_BAD_PACKET,
252 "unexpected message from server");
255 len = sizeof(struct got_imsg_send_ref_status) + reflen;
256 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
257 return got_error(GOT_ERR_NO_SPACE);
259 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
260 0, 0, len);
261 if (wbuf == NULL)
262 return got_error_from_errno("imsg_create SEND_REF_STATUS");
264 /* Keep in sync with struct got_imsg_send_ref_status definition! */
265 if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
266 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
267 ibuf_free(wbuf);
268 return err;
270 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
271 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
272 ibuf_free(wbuf);
273 return err;
275 if (imsg_add(wbuf, refname, reflen) == -1) {
276 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
277 ibuf_free(wbuf);
278 return err;
281 wbuf->fd = -1;
282 imsg_close(ibuf, wbuf);
283 return got_privsep_flush_imsg(ibuf);
286 static const struct got_error *
287 describe_refchange(int *n, int *sent_my_capabilites,
288 const char *my_capabilities, char *buf, size_t bufsize,
289 const char *refname, const char *old_hashstr, const char *new_hashstr)
291 *n = snprintf(buf, bufsize, "%s %s %s",
292 old_hashstr, new_hashstr, refname);
293 if (*n >= bufsize)
294 return got_error(GOT_ERR_NO_SPACE);
296 /*
297 * We must announce our capabilities along with the first
298 * reference. Unfortunately, the protocol requires an embedded
299 * NUL as a separator between reference name and capabilities,
300 * which we have to deal with here.
301 * It also requires a linefeed for terminating packet data.
302 */
303 if (!*sent_my_capabilites && my_capabilities != NULL) {
304 int m;
305 if (*n >= bufsize - 1)
306 return got_error(GOT_ERR_NO_SPACE);
307 m = snprintf(buf + *n + 1, /* offset after '\0' */
308 bufsize - (*n + 1), "%s\n", my_capabilities);
309 if (*n + m >= bufsize)
310 return got_error(GOT_ERR_NO_SPACE);
311 *n += m;
312 *sent_my_capabilites = 1;
313 } else {
314 *n = strlcat(buf, "\n", bufsize);
315 if (*n >= bufsize)
316 return got_error(GOT_ERR_NO_SPACE);
319 return NULL;
322 static const struct got_error *
323 send_pack(int fd, struct got_pathlist_head *refs,
324 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
326 const struct got_error *err = NULL;
327 char buf[GOT_PKT_MAX];
328 unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
329 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
330 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
331 struct got_pathlist_head their_refs;
332 int is_firstpkt = 1;
333 int n, nsent = 0;
334 int packfd = -1;
335 char *id_str = NULL, *refname = NULL;
336 struct got_object_id *id = NULL;
337 char *server_capabilities = NULL, *my_capabilities = NULL;
338 struct got_pathlist_entry *pe;
339 int sent_my_capabilites = 0;
341 TAILQ_INIT(&their_refs);
343 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
344 return got_error(GOT_ERR_SEND_EMPTY);
346 while (1) {
347 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
348 if (err)
349 goto done;
350 if (n == 0)
351 break;
352 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
353 err = send_error(&buf[4], n - 4);
354 goto done;
356 err = got_gitproto_parse_refline(&id_str, &refname,
357 &server_capabilities, buf, n);
358 if (err)
359 goto done;
360 if (is_firstpkt) {
361 if (chattygot && server_capabilities[0] != '\0')
362 fprintf(stderr, "%s: server capabilities: %s\n",
363 getprogname(), server_capabilities);
364 err = got_gitproto_match_capabilities(&my_capabilities,
365 NULL, server_capabilities, got_capabilities,
366 nitems(got_capabilities));
367 if (err)
368 goto done;
369 if (chattygot)
370 fprintf(stderr, "%s: my capabilities:%s\n",
371 getprogname(), my_capabilities);
372 is_firstpkt = 0;
374 if (strstr(refname, "^{}")) {
375 if (chattygot) {
376 fprintf(stderr, "%s: ignoring %s\n",
377 getprogname(), refname);
379 continue;
382 id = malloc(sizeof(*id));
383 if (id == NULL) {
384 err = got_error_from_errno("malloc");
385 goto done;
387 if (!got_parse_sha1_digest(id->sha1, id_str)) {
388 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
389 goto done;
391 err = send_their_ref(ibuf, id, refname);
392 if (err)
393 goto done;
395 err = got_pathlist_append(&their_refs, refname, id);
396 if (chattygot)
397 fprintf(stderr, "%s: remote has %s %s\n",
398 getprogname(), refname, id_str);
399 free(id_str);
400 id_str = NULL;
401 refname = NULL; /* do not free; owned by their_refs */
402 id = NULL; /* do not free; owned by their_refs */
405 if (!TAILQ_EMPTY(delete_refs)) {
406 if (my_capabilities == NULL ||
407 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
408 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
409 goto done;
413 TAILQ_FOREACH(pe, delete_refs, entry) {
414 const char *refname = pe->path;
415 struct got_pathlist_entry *their_pe;
416 struct got_object_id *their_id = NULL;
418 TAILQ_FOREACH(their_pe, &their_refs, entry) {
419 const char *their_refname = their_pe->path;
420 if (got_path_cmp(refname, their_refname,
421 strlen(refname), strlen(their_refname)) == 0) {
422 their_id = their_pe->data;
423 break;
426 if (their_id == NULL) {
427 err = got_error_fmt(GOT_ERR_NOT_REF,
428 "%s does not exist in remote repository",
429 refname);
430 goto done;
433 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
434 sizeof(old_hashstr));
435 got_sha1_digest_to_str(zero_id, new_hashstr,
436 sizeof(new_hashstr));
437 err = describe_refchange(&n, &sent_my_capabilites,
438 my_capabilities, buf, sizeof(buf), refname,
439 old_hashstr, new_hashstr);
440 if (err)
441 goto done;
442 err = got_pkt_writepkt(fd, buf, n, chattygot);
443 if (err)
444 goto done;
445 if (chattygot) {
446 fprintf(stderr, "%s: deleting %s %s\n",
447 getprogname(), refname, old_hashstr);
449 nsent++;
452 TAILQ_FOREACH(pe, refs, entry) {
453 const char *refname = pe->path;
454 struct got_object_id *id = pe->data;
455 struct got_object_id *their_id = NULL;
456 struct got_pathlist_entry *their_pe;
458 TAILQ_FOREACH(their_pe, &their_refs, entry) {
459 const char *their_refname = their_pe->path;
460 if (got_path_cmp(refname, their_refname,
461 strlen(refname), strlen(their_refname)) == 0) {
462 their_id = their_pe->data;
463 break;
466 if (their_id) {
467 if (got_object_id_cmp(id, their_id) == 0) {
468 if (chattygot) {
469 fprintf(stderr,
470 "%s: no change for %s\n",
471 getprogname(), refname);
473 continue;
475 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
476 sizeof(old_hashstr));
477 } else {
478 got_sha1_digest_to_str(zero_id, old_hashstr,
479 sizeof(old_hashstr));
481 got_sha1_digest_to_str(id->sha1, new_hashstr,
482 sizeof(new_hashstr));
483 err = describe_refchange(&n, &sent_my_capabilites,
484 my_capabilities, buf, sizeof(buf), refname,
485 old_hashstr, new_hashstr);
486 if (err)
487 goto done;
488 err = got_pkt_writepkt(fd, buf, n, chattygot);
489 if (err)
490 goto done;
491 if (chattygot) {
492 if (their_id) {
493 fprintf(stderr, "%s: updating %s %s -> %s\n",
494 getprogname(), refname, old_hashstr,
495 new_hashstr);
496 } else {
497 fprintf(stderr, "%s: creating %s %s\n",
498 getprogname(), refname, new_hashstr);
501 nsent++;
503 err = got_pkt_flushpkt(fd, chattygot);
504 if (err)
505 goto done;
507 err = send_pack_request(ibuf);
508 if (err)
509 goto done;
511 err = recv_packfd(&packfd, ibuf);
512 if (err)
513 goto done;
515 if (packfd != -1) {
516 err = send_pack_file(fd, packfd, ibuf);
517 if (err)
518 goto done;
521 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
522 if (err)
523 goto done;
524 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
525 err = send_error(&buf[4], n - 4);
526 goto done;
527 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
528 err = got_error_msg(GOT_ERR_BAD_PACKET,
529 "unexpected message from server");
530 goto done;
533 while (nsent > 0) {
534 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
535 if (err)
536 goto done;
537 if (n < 3) {
538 err = got_error_msg(GOT_ERR_BAD_PACKET,
539 "unexpected message from server");
540 goto done;
541 } else if (strncmp(buf, "ok ", 3) == 0) {
542 err = send_ref_status(ibuf, buf + 3, 1,
543 refs, delete_refs);
544 if (err)
545 goto done;
546 } else if (strncmp(buf, "ng ", 3) == 0) {
547 err = send_ref_status(ibuf, buf + 3, 0,
548 refs, delete_refs);
549 if (err)
550 goto done;
551 } else {
552 err = got_error_msg(GOT_ERR_BAD_PACKET,
553 "unexpected message from server");
554 goto done;
556 nsent--;
559 err = send_done(ibuf);
560 done:
561 TAILQ_FOREACH(pe, &their_refs, entry) {
562 free((void *)pe->path);
563 free(pe->data);
565 got_pathlist_free(&their_refs);
566 free(id_str);
567 free(id);
568 free(refname);
569 free(server_capabilities);
570 return err;
573 int
574 main(int argc, char **argv)
576 const struct got_error *err = NULL;
577 int sendfd;
578 struct imsgbuf ibuf;
579 struct imsg imsg;
580 struct got_pathlist_head refs;
581 struct got_pathlist_head delete_refs;
582 struct got_pathlist_entry *pe;
583 struct got_imsg_send_request send_req;
584 struct got_imsg_send_ref href;
585 size_t datalen, i;
586 #if 0
587 static int attached;
588 while (!attached)
589 sleep (1);
590 #endif
592 TAILQ_INIT(&refs);
593 TAILQ_INIT(&delete_refs);
595 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
596 #ifndef PROFILE
597 /* revoke access to most system calls */
598 if (pledge("stdio recvfd", NULL) == -1) {
599 err = got_error_from_errno("pledge");
600 got_privsep_send_error(&ibuf, err);
601 return 1;
603 #endif
604 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
605 if (err->code == GOT_ERR_PRIVSEP_PIPE)
606 err = NULL;
607 goto done;
609 if (imsg.hdr.type == GOT_IMSG_STOP)
610 goto done;
611 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
612 err = got_error(GOT_ERR_PRIVSEP_MSG);
613 goto done;
615 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
616 if (datalen < sizeof(send_req)) {
617 err = got_error(GOT_ERR_PRIVSEP_LEN);
618 goto done;
620 memcpy(&send_req, imsg.data, sizeof(send_req));
621 sendfd = imsg.fd;
622 imsg_free(&imsg);
624 if (send_req.verbosity > 0)
625 chattygot += send_req.verbosity;
627 for (i = 0; i < send_req.nrefs; i++) {
628 struct got_object_id *id;
629 char *refname;
631 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
632 if (err->code == GOT_ERR_PRIVSEP_PIPE)
633 err = NULL;
634 goto done;
636 if (imsg.hdr.type == GOT_IMSG_STOP)
637 goto done;
638 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
639 err = got_error(GOT_ERR_PRIVSEP_MSG);
640 goto done;
642 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
643 if (datalen < sizeof(href)) {
644 err = got_error(GOT_ERR_PRIVSEP_LEN);
645 goto done;
647 memcpy(&href, imsg.data, sizeof(href));
648 if (datalen - sizeof(href) < href.name_len) {
649 err = got_error(GOT_ERR_PRIVSEP_LEN);
650 goto done;
652 refname = malloc(href.name_len + 1);
653 if (refname == NULL) {
654 err = got_error_from_errno("malloc");
655 goto done;
657 memcpy(refname, imsg.data + sizeof(href), href.name_len);
658 refname[href.name_len] = '\0';
660 /*
661 * Prevent sending of references that won't make any
662 * sense outside the local repository's context.
663 */
664 if (strncmp(refname, "refs/got/", 9) == 0 ||
665 strncmp(refname, "refs/remotes/", 13) == 0) {
666 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
667 "%s", refname);
668 goto done;
671 id = malloc(sizeof(*id));
672 if (id == NULL) {
673 free(refname);
674 err = got_error_from_errno("malloc");
675 goto done;
677 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
678 if (href.delete)
679 err = got_pathlist_append(&delete_refs, refname, id);
680 else
681 err = got_pathlist_append(&refs, refname, id);
682 if (err) {
683 free(refname);
684 free(id);
685 goto done;
688 imsg_free(&imsg);
691 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
692 done:
693 TAILQ_FOREACH(pe, &refs, entry) {
694 free((char *)pe->path);
695 free(pe->data);
697 got_pathlist_free(&refs);
698 TAILQ_FOREACH(pe, &delete_refs, entry) {
699 free((char *)pe->path);
700 free(pe->data);
702 got_pathlist_free(&delete_refs);
703 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
704 err = got_error_from_errno("close");
705 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
706 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
707 got_privsep_send_error(&ibuf, err);
710 exit(0);