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/uio.h>
20 #include <sys/time.h>
21 #include <sys/stat.h>
23 #include <stdint.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <zlib.h>
34 #include <err.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_version.h"
40 #include "got_fetch.h"
41 #include "got_reference.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_pkt.h"
50 #include "got_lib_gitproto.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct got_object *indexed;
57 static int chattygot;
59 static const struct got_capability got_capabilities[] = {
60 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
61 { GOT_CAPA_OFS_DELTA, NULL },
62 #if 0
63 { GOT_CAPA_SIDE_BAND_64K, NULL },
64 #endif
65 { GOT_CAPA_REPORT_STATUS, NULL },
66 { GOT_CAPA_DELETE_REFS, NULL },
67 };
69 static const struct got_error *
70 send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
71 {
72 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
73 &bytes, sizeof(bytes)) == -1)
74 return got_error_from_errno(
75 "imsg_compose SEND_UPLOAD_PROGRESS");
77 return got_privsep_flush_imsg(ibuf);
78 }
80 static const struct got_error *
81 send_pack_request(struct imsgbuf *ibuf)
82 {
83 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
84 NULL, 0) == -1)
85 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
86 return got_privsep_flush_imsg(ibuf);
87 }
89 static const struct got_error *
90 send_done(struct imsgbuf *ibuf)
91 {
92 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
93 return got_error_from_errno("imsg_compose SEND_DONE");
94 return got_privsep_flush_imsg(ibuf);
95 }
97 static const struct got_error *
98 recv_packfd(int *packfd, struct imsgbuf *ibuf)
99 {
100 const struct got_error *err;
101 struct imsg imsg;
103 *packfd = -1;
105 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
106 if (err)
107 return err;
109 if (imsg.hdr.type == GOT_IMSG_STOP) {
110 err = got_error(GOT_ERR_CANCELLED);
111 goto done;
114 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
115 err = got_error(GOT_ERR_PRIVSEP_MSG);
116 goto done;
119 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
120 err = got_error(GOT_ERR_PRIVSEP_LEN);
121 goto done;
124 *packfd = imsg.fd;
125 done:
126 imsg_free(&imsg);
127 return err;
130 static const struct got_error *
131 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
133 const struct got_error *err;
134 unsigned char buf[8192];
135 ssize_t r, w;
136 off_t wtotal = 0;
138 if (lseek(packfd, 0L, SEEK_SET) == -1)
139 return got_error_from_errno("lseek");
141 for (;;) {
142 r = read(packfd, buf, sizeof(buf));
143 if (r == -1)
144 return got_error_from_errno("read");
145 if (r == 0)
146 break;
147 w = write(sendfd, buf, r);
148 if (w == -1)
149 return got_error_from_errno("write");
150 if (w != r)
151 return got_error(GOT_ERR_IO);
152 wtotal += w;
153 err = send_upload_progress(ibuf, wtotal);
154 if (err)
155 return err;
158 return NULL;
161 static const struct got_error *
162 send_error(const char *buf, size_t len)
164 static char msg[1024];
165 size_t i;
167 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
168 if (!isprint(buf[i]))
169 return got_error_msg(GOT_ERR_BAD_PACKET,
170 "non-printable error message received from server");
171 msg[i] = buf[i];
173 msg[i] = '\0';
174 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
177 static const struct got_error *
178 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
179 const char *refname)
181 const struct got_error *err = NULL;
182 struct ibuf *wbuf;
183 size_t len, reflen = strlen(refname);
185 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
186 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
187 return got_error(GOT_ERR_NO_SPACE);
189 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
190 if (wbuf == NULL)
191 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
193 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
194 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
195 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
196 ibuf_free(wbuf);
197 return err;
199 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
200 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
201 ibuf_free(wbuf);
202 return err;
204 if (imsg_add(wbuf, refname, reflen) == -1) {
205 err = got_error_from_errno("imsg_add SEND_REMOTE_REF");
206 ibuf_free(wbuf);
207 return err;
210 wbuf->fd = -1;
211 imsg_close(ibuf, wbuf);
212 return got_privsep_flush_imsg(ibuf);
215 static const struct got_error *
216 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
217 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
220 const struct got_error *err = NULL;
221 struct ibuf *wbuf;
222 size_t len, reflen = strlen(refname);
223 struct got_pathlist_entry *pe;
224 int ref_valid = 0;
225 char *eol;
227 eol = strchr(refname, '\n');
228 if (eol == NULL) {
229 return got_error_msg(GOT_ERR_BAD_PACKET,
230 "unexpected message from server");
232 *eol = '\0';
234 TAILQ_FOREACH(pe, refs, entry) {
235 if (strcmp(refname, pe->path) == 0) {
236 ref_valid = 1;
237 break;
240 if (!ref_valid) {
241 TAILQ_FOREACH(pe, delete_refs, entry) {
242 if (strcmp(refname, pe->path) == 0) {
243 ref_valid = 1;
244 break;
248 if (!ref_valid) {
249 return got_error_msg(GOT_ERR_BAD_PACKET,
250 "unexpected message from server");
253 len = sizeof(struct got_imsg_send_ref_status) + reflen;
254 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
255 return got_error(GOT_ERR_NO_SPACE);
257 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
258 0, 0, len);
259 if (wbuf == NULL)
260 return got_error_from_errno("imsg_create SEND_REF_STATUS");
262 /* Keep in sync with struct got_imsg_send_ref_status definition! */
263 if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
264 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
265 ibuf_free(wbuf);
266 return err;
268 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
269 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
270 ibuf_free(wbuf);
271 return err;
273 if (imsg_add(wbuf, refname, reflen) == -1) {
274 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
275 ibuf_free(wbuf);
276 return err;
279 wbuf->fd = -1;
280 imsg_close(ibuf, wbuf);
281 return got_privsep_flush_imsg(ibuf);
284 static const struct got_error *
285 describe_refchange(int *n, int *sent_my_capabilites,
286 const char *my_capabilities, char *buf, size_t bufsize,
287 const char *refname, const char *old_hashstr, const char *new_hashstr)
289 *n = snprintf(buf, bufsize, "%s %s %s",
290 old_hashstr, new_hashstr, refname);
291 if (*n >= bufsize)
292 return got_error(GOT_ERR_NO_SPACE);
294 /*
295 * We must announce our capabilities along with the first
296 * reference. Unfortunately, the protocol requires an embedded
297 * NUL as a separator between reference name and capabilities,
298 * which we have to deal with here.
299 * It also requires a linefeed for terminating packet data.
300 */
301 if (!*sent_my_capabilites && my_capabilities != NULL) {
302 int m;
303 if (*n >= bufsize - 1)
304 return got_error(GOT_ERR_NO_SPACE);
305 m = snprintf(buf + *n + 1, /* offset after '\0' */
306 bufsize - (*n + 1), "%s\n", my_capabilities);
307 if (*n + m >= bufsize)
308 return got_error(GOT_ERR_NO_SPACE);
309 *n += m;
310 *sent_my_capabilites = 1;
311 } else {
312 *n = strlcat(buf, "\n", bufsize);
313 if (*n >= bufsize)
314 return got_error(GOT_ERR_NO_SPACE);
317 return NULL;
320 static const struct got_error *
321 send_pack(int fd, struct got_pathlist_head *refs,
322 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
324 const struct got_error *err = NULL;
325 char buf[GOT_PKT_MAX];
326 unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
327 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
328 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
329 struct got_pathlist_head their_refs;
330 int is_firstpkt = 1;
331 int n, nsent = 0;
332 int packfd = -1;
333 char *id_str = NULL, *refname = NULL;
334 struct got_object_id *id = NULL;
335 char *server_capabilities = NULL, *my_capabilities = NULL;
336 struct got_pathlist_entry *pe;
337 int sent_my_capabilites = 0;
339 TAILQ_INIT(&their_refs);
341 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
342 return got_error(GOT_ERR_SEND_EMPTY);
344 while (1) {
345 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
346 if (err)
347 goto done;
348 if (n == 0)
349 break;
350 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
351 err = send_error(&buf[4], n - 4);
352 goto done;
354 err = got_gitproto_parse_refline(&id_str, &refname,
355 &server_capabilities, buf, n);
356 if (err)
357 goto done;
358 if (is_firstpkt) {
359 if (chattygot && server_capabilities[0] != '\0')
360 fprintf(stderr, "%s: server capabilities: %s\n",
361 getprogname(), server_capabilities);
362 err = got_gitproto_match_capabilities(&my_capabilities,
363 NULL, server_capabilities, got_capabilities,
364 nitems(got_capabilities));
365 if (err)
366 goto done;
367 if (chattygot)
368 fprintf(stderr, "%s: my capabilities:%s\n",
369 getprogname(), my_capabilities);
370 is_firstpkt = 0;
372 if (strstr(refname, "^{}")) {
373 if (chattygot) {
374 fprintf(stderr, "%s: ignoring %s\n",
375 getprogname(), refname);
377 continue;
380 id = malloc(sizeof(*id));
381 if (id == NULL) {
382 err = got_error_from_errno("malloc");
383 goto done;
385 if (!got_parse_sha1_digest(id->sha1, id_str)) {
386 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
387 goto done;
389 err = send_their_ref(ibuf, id, refname);
390 if (err)
391 goto done;
393 err = got_pathlist_append(&their_refs, refname, id);
394 if (chattygot)
395 fprintf(stderr, "%s: remote has %s %s\n",
396 getprogname(), refname, id_str);
397 free(id_str);
398 id_str = NULL;
399 refname = NULL; /* do not free; owned by their_refs */
400 id = NULL; /* do not free; owned by their_refs */
403 if (!TAILQ_EMPTY(delete_refs)) {
404 if (my_capabilities == NULL ||
405 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
406 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
407 goto done;
411 TAILQ_FOREACH(pe, delete_refs, entry) {
412 const char *refname = pe->path;
413 struct got_pathlist_entry *their_pe;
414 struct got_object_id *their_id = NULL;
416 TAILQ_FOREACH(their_pe, &their_refs, entry) {
417 const char *their_refname = their_pe->path;
418 if (got_path_cmp(refname, their_refname,
419 strlen(refname), strlen(their_refname)) == 0) {
420 their_id = their_pe->data;
421 break;
424 if (their_id == NULL) {
425 err = got_error_fmt(GOT_ERR_NOT_REF,
426 "%s does not exist in remote repository",
427 refname);
428 goto done;
431 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
432 sizeof(old_hashstr));
433 got_sha1_digest_to_str(zero_id, new_hashstr,
434 sizeof(new_hashstr));
435 err = describe_refchange(&n, &sent_my_capabilites,
436 my_capabilities, buf, sizeof(buf), refname,
437 old_hashstr, new_hashstr);
438 if (err)
439 goto done;
440 err = got_pkt_writepkt(fd, buf, n, chattygot);
441 if (err)
442 goto done;
443 if (chattygot) {
444 fprintf(stderr, "%s: deleting %s %s\n",
445 getprogname(), refname, old_hashstr);
447 nsent++;
450 TAILQ_FOREACH(pe, refs, entry) {
451 const char *refname = pe->path;
452 struct got_object_id *id = pe->data;
453 struct got_object_id *their_id = NULL;
454 struct got_pathlist_entry *their_pe;
456 TAILQ_FOREACH(their_pe, &their_refs, entry) {
457 const char *their_refname = their_pe->path;
458 if (got_path_cmp(refname, their_refname,
459 strlen(refname), strlen(their_refname)) == 0) {
460 their_id = their_pe->data;
461 break;
464 if (their_id) {
465 if (got_object_id_cmp(id, their_id) == 0) {
466 if (chattygot) {
467 fprintf(stderr,
468 "%s: no change for %s\n",
469 getprogname(), refname);
471 continue;
473 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
474 sizeof(old_hashstr));
475 } else {
476 got_sha1_digest_to_str(zero_id, old_hashstr,
477 sizeof(old_hashstr));
479 got_sha1_digest_to_str(id->sha1, new_hashstr,
480 sizeof(new_hashstr));
481 err = describe_refchange(&n, &sent_my_capabilites,
482 my_capabilities, buf, sizeof(buf), refname,
483 old_hashstr, new_hashstr);
484 if (err)
485 goto done;
486 err = got_pkt_writepkt(fd, buf, n, chattygot);
487 if (err)
488 goto done;
489 if (chattygot) {
490 if (their_id) {
491 fprintf(stderr, "%s: updating %s %s -> %s\n",
492 getprogname(), refname, old_hashstr,
493 new_hashstr);
494 } else {
495 fprintf(stderr, "%s: creating %s %s\n",
496 getprogname(), refname, new_hashstr);
499 nsent++;
501 err = got_pkt_flushpkt(fd, chattygot);
502 if (err)
503 goto done;
505 err = send_pack_request(ibuf);
506 if (err)
507 goto done;
509 err = recv_packfd(&packfd, ibuf);
510 if (err)
511 goto done;
513 if (packfd != -1) {
514 err = send_pack_file(fd, packfd, ibuf);
515 if (err)
516 goto done;
519 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
520 if (err)
521 goto done;
522 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
523 err = send_error(&buf[4], n - 4);
524 goto done;
525 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
526 err = got_error_msg(GOT_ERR_BAD_PACKET,
527 "unexpected message from server");
528 goto done;
531 while (nsent > 0) {
532 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
533 if (err)
534 goto done;
535 if (n < 3) {
536 err = got_error_msg(GOT_ERR_BAD_PACKET,
537 "unexpected message from server");
538 goto done;
539 } else if (strncmp(buf, "ok ", 3) == 0) {
540 err = send_ref_status(ibuf, buf + 3, 1,
541 refs, delete_refs);
542 if (err)
543 goto done;
544 } else if (strncmp(buf, "ng ", 3) == 0) {
545 err = send_ref_status(ibuf, buf + 3, 0,
546 refs, delete_refs);
547 if (err)
548 goto done;
549 } else {
550 err = got_error_msg(GOT_ERR_BAD_PACKET,
551 "unexpected message from server");
552 goto done;
554 nsent--;
557 err = send_done(ibuf);
558 done:
559 TAILQ_FOREACH(pe, &their_refs, entry) {
560 free((void *)pe->path);
561 free(pe->data);
563 got_pathlist_free(&their_refs);
564 free(id_str);
565 free(id);
566 free(refname);
567 free(server_capabilities);
568 return err;
571 int
572 main(int argc, char **argv)
574 const struct got_error *err = NULL;
575 int sendfd, i;
576 struct imsgbuf ibuf;
577 struct imsg imsg;
578 struct got_pathlist_head refs;
579 struct got_pathlist_head delete_refs;
580 struct got_pathlist_entry *pe;
581 struct got_imsg_send_request send_req;
582 struct got_imsg_send_ref href;
583 size_t datalen;
584 #if 0
585 static int attached;
586 while (!attached)
587 sleep (1);
588 #endif
590 TAILQ_INIT(&refs);
591 TAILQ_INIT(&delete_refs);
593 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
594 #ifndef PROFILE
595 /* revoke access to most system calls */
596 if (pledge("stdio recvfd", NULL) == -1) {
597 err = got_error_from_errno("pledge");
598 got_privsep_send_error(&ibuf, err);
599 return 1;
601 #endif
602 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
603 if (err->code == GOT_ERR_PRIVSEP_PIPE)
604 err = NULL;
605 goto done;
607 if (imsg.hdr.type == GOT_IMSG_STOP)
608 goto done;
609 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
610 err = got_error(GOT_ERR_PRIVSEP_MSG);
611 goto done;
613 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
614 if (datalen < sizeof(send_req)) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
616 goto done;
618 memcpy(&send_req, imsg.data, sizeof(send_req));
619 sendfd = imsg.fd;
620 imsg_free(&imsg);
622 if (send_req.verbosity > 0)
623 chattygot += send_req.verbosity;
625 for (i = 0; i < send_req.nrefs; i++) {
626 struct got_object_id *id;
627 char *refname;
629 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
630 if (err->code == GOT_ERR_PRIVSEP_PIPE)
631 err = NULL;
632 goto done;
634 if (imsg.hdr.type == GOT_IMSG_STOP)
635 goto done;
636 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
637 err = got_error(GOT_ERR_PRIVSEP_MSG);
638 goto done;
640 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
641 if (datalen < sizeof(href)) {
642 err = got_error(GOT_ERR_PRIVSEP_LEN);
643 goto done;
645 memcpy(&href, imsg.data, sizeof(href));
646 if (datalen - sizeof(href) < href.name_len) {
647 err = got_error(GOT_ERR_PRIVSEP_LEN);
648 goto done;
650 refname = malloc(href.name_len + 1);
651 if (refname == NULL) {
652 err = got_error_from_errno("malloc");
653 goto done;
655 memcpy(refname, imsg.data + sizeof(href), href.name_len);
656 refname[href.name_len] = '\0';
658 /*
659 * Prevent sending of references that won't make any
660 * sense outside the local repository's context.
661 */
662 if (strncmp(refname, "refs/got/", 9) == 0 ||
663 strncmp(refname, "refs/remotes/", 13) == 0) {
664 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
665 "%s", refname);
666 goto done;
669 id = malloc(sizeof(*id));
670 if (id == NULL) {
671 free(refname);
672 err = got_error_from_errno("malloc");
673 goto done;
675 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
676 if (href.delete)
677 err = got_pathlist_append(&delete_refs, refname, id);
678 else
679 err = got_pathlist_append(&refs, refname, id);
680 if (err) {
681 free(refname);
682 free(id);
683 goto done;
686 imsg_free(&imsg);
689 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
690 done:
691 TAILQ_FOREACH(pe, &refs, entry) {
692 free((char *)pe->path);
693 free(pe->data);
695 got_pathlist_free(&refs);
696 TAILQ_FOREACH(pe, &delete_refs, entry) {
697 free((char *)pe->path);
698 free(pe->data);
700 got_pathlist_free(&delete_refs);
701 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
702 err = got_error_from_errno("close");
703 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
704 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
705 got_privsep_send_error(&ibuf, err);
708 exit(0);