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)
219 const struct got_error *err = NULL;
220 struct ibuf *wbuf;
221 size_t len, reflen = strlen(refname);
222 struct got_pathlist_entry *pe;
223 int ref_valid = 0;
224 char *eol;
226 eol = strchr(refname, '\n');
227 if (eol == NULL) {
228 return got_error_msg(GOT_ERR_BAD_PACKET,
229 "unexpected message from server");
231 *eol = '\0';
233 TAILQ_FOREACH(pe, refs, entry) {
234 if (strcmp(refname, pe->path) == 0) {
235 ref_valid = 1;
236 break;
239 if (!ref_valid) {
240 TAILQ_FOREACH(pe, delete_refs, entry) {
241 if (strcmp(refname, pe->path) == 0) {
242 ref_valid = 1;
243 break;
247 if (!ref_valid) {
248 return got_error_msg(GOT_ERR_BAD_PACKET,
249 "unexpected message from server");
252 len = sizeof(struct got_imsg_send_ref_status) + reflen;
253 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
254 return got_error(GOT_ERR_NO_SPACE);
256 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
257 0, 0, len);
258 if (wbuf == NULL)
259 return got_error_from_errno("imsg_create SEND_REF_STATUS");
261 /* Keep in sync with struct got_imsg_send_ref_status definition! */
262 if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
263 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
264 ibuf_free(wbuf);
265 return err;
267 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
268 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
269 ibuf_free(wbuf);
270 return err;
272 if (imsg_add(wbuf, refname, reflen) == -1) {
273 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
274 ibuf_free(wbuf);
275 return err;
278 wbuf->fd = -1;
279 imsg_close(ibuf, wbuf);
280 return got_privsep_flush_imsg(ibuf);
283 static const struct got_error *
284 describe_refchange(int *n, int *sent_my_capabilites,
285 const char *my_capabilities, char *buf, size_t bufsize,
286 const char *refname, const char *old_hashstr, const char *new_hashstr)
288 *n = snprintf(buf, bufsize, "%s %s %s",
289 old_hashstr, new_hashstr, refname);
290 if (*n >= bufsize)
291 return got_error(GOT_ERR_NO_SPACE);
293 /*
294 * We must announce our capabilities along with the first
295 * reference. Unfortunately, the protocol requires an embedded
296 * NUL as a separator between reference name and capabilities,
297 * which we have to deal with here.
298 * It also requires a linefeed for terminating packet data.
299 */
300 if (!*sent_my_capabilites && my_capabilities != NULL) {
301 int m;
302 if (*n >= bufsize - 1)
303 return got_error(GOT_ERR_NO_SPACE);
304 m = snprintf(buf + *n + 1, /* offset after '\0' */
305 bufsize - (*n + 1), "%s\n", my_capabilities);
306 if (*n + m >= bufsize)
307 return got_error(GOT_ERR_NO_SPACE);
308 *n += m;
309 *sent_my_capabilites = 1;
310 } else {
311 *n = strlcat(buf, "\n", bufsize);
312 if (*n >= bufsize)
313 return got_error(GOT_ERR_NO_SPACE);
316 return NULL;
319 static const struct got_error *
320 send_pack(int fd, struct got_pathlist_head *refs,
321 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
323 const struct got_error *err = NULL;
324 char buf[GOT_PKT_MAX];
325 unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
326 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
327 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
328 struct got_pathlist_head their_refs;
329 int is_firstpkt = 1;
330 int n, nsent = 0;
331 int packfd = -1;
332 char *id_str = NULL, *refname = NULL;
333 struct got_object_id *id = NULL;
334 char *server_capabilities = NULL, *my_capabilities = NULL;
335 struct got_pathlist_entry *pe;
336 int sent_my_capabilites = 0;
338 TAILQ_INIT(&their_refs);
340 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
341 return got_error(GOT_ERR_SEND_EMPTY);
343 while (1) {
344 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
345 if (err)
346 goto done;
347 if (n == 0)
348 break;
349 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
350 err = send_error(&buf[4], n - 4);
351 goto done;
353 err = got_gitproto_parse_refline(&id_str, &refname,
354 &server_capabilities, buf, n);
355 if (err)
356 goto done;
357 if (is_firstpkt) {
358 if (chattygot && server_capabilities[0] != '\0')
359 fprintf(stderr, "%s: server capabilities: %s\n",
360 getprogname(), server_capabilities);
361 err = got_gitproto_match_capabilities(&my_capabilities,
362 NULL, server_capabilities, got_capabilities,
363 nitems(got_capabilities));
364 if (err)
365 goto done;
366 if (chattygot)
367 fprintf(stderr, "%s: my capabilities:%s\n",
368 getprogname(), my_capabilities);
369 is_firstpkt = 0;
371 if (strstr(refname, "^{}")) {
372 if (chattygot) {
373 fprintf(stderr, "%s: ignoring %s\n",
374 getprogname(), refname);
376 continue;
379 id = malloc(sizeof(*id));
380 if (id == NULL) {
381 err = got_error_from_errno("malloc");
382 goto done;
384 if (!got_parse_sha1_digest(id->sha1, id_str)) {
385 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
386 goto done;
388 err = send_their_ref(ibuf, id, refname);
389 if (err)
390 goto done;
392 err = got_pathlist_append(&their_refs, refname, id);
393 if (chattygot)
394 fprintf(stderr, "%s: remote has %s %s\n",
395 getprogname(), refname, id_str);
396 free(id_str);
397 id_str = NULL;
398 refname = NULL; /* do not free; owned by their_refs */
399 id = NULL; /* do not free; owned by their_refs */
402 if (!TAILQ_EMPTY(delete_refs)) {
403 if (my_capabilities == NULL ||
404 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
405 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
406 goto done;
410 TAILQ_FOREACH(pe, delete_refs, entry) {
411 const char *refname = pe->path;
412 struct got_pathlist_entry *their_pe;
413 struct got_object_id *their_id = NULL;
415 TAILQ_FOREACH(their_pe, &their_refs, entry) {
416 const char *their_refname = their_pe->path;
417 if (got_path_cmp(refname, their_refname,
418 strlen(refname), strlen(their_refname)) == 0) {
419 their_id = their_pe->data;
420 break;
423 if (their_id == NULL) {
424 err = got_error_fmt(GOT_ERR_NOT_REF,
425 "%s does not exist in remote repository",
426 refname);
427 goto done;
430 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
431 sizeof(old_hashstr));
432 got_sha1_digest_to_str(zero_id, new_hashstr,
433 sizeof(new_hashstr));
434 err = describe_refchange(&n, &sent_my_capabilites,
435 my_capabilities, buf, sizeof(buf), refname,
436 old_hashstr, new_hashstr);
437 if (err)
438 goto done;
439 err = got_pkt_writepkt(fd, buf, n, chattygot);
440 if (err)
441 goto done;
442 if (chattygot) {
443 fprintf(stderr, "%s: deleting %s %s\n",
444 getprogname(), refname, old_hashstr);
446 nsent++;
449 TAILQ_FOREACH(pe, refs, entry) {
450 const char *refname = pe->path;
451 struct got_object_id *id = pe->data;
452 struct got_object_id *their_id = NULL;
453 struct got_pathlist_entry *their_pe;
455 TAILQ_FOREACH(their_pe, &their_refs, entry) {
456 const char *their_refname = their_pe->path;
457 if (got_path_cmp(refname, their_refname,
458 strlen(refname), strlen(their_refname)) == 0) {
459 their_id = their_pe->data;
460 break;
463 if (their_id) {
464 if (got_object_id_cmp(id, their_id) == 0) {
465 if (chattygot) {
466 fprintf(stderr,
467 "%s: no change for %s\n",
468 getprogname(), refname);
470 continue;
472 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
473 sizeof(old_hashstr));
474 } else {
475 got_sha1_digest_to_str(zero_id, old_hashstr,
476 sizeof(old_hashstr));
478 got_sha1_digest_to_str(id->sha1, new_hashstr,
479 sizeof(new_hashstr));
480 err = describe_refchange(&n, &sent_my_capabilites,
481 my_capabilities, buf, sizeof(buf), refname,
482 old_hashstr, new_hashstr);
483 if (err)
484 goto done;
485 err = got_pkt_writepkt(fd, buf, n, chattygot);
486 if (err)
487 goto done;
488 if (chattygot) {
489 if (their_id) {
490 fprintf(stderr, "%s: updating %s %s -> %s\n",
491 getprogname(), refname, old_hashstr,
492 new_hashstr);
493 } else {
494 fprintf(stderr, "%s: creating %s %s\n",
495 getprogname(), refname, new_hashstr);
498 nsent++;
500 err = got_pkt_flushpkt(fd, chattygot);
501 if (err)
502 goto done;
504 err = send_pack_request(ibuf);
505 if (err)
506 goto done;
508 err = recv_packfd(&packfd, ibuf);
509 if (err)
510 goto done;
512 if (packfd != -1) {
513 err = send_pack_file(fd, packfd, ibuf);
514 if (err)
515 goto done;
518 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
519 if (err)
520 goto done;
521 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
522 err = send_error(&buf[4], n - 4);
523 goto done;
524 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
525 err = got_error_msg(GOT_ERR_BAD_PACKET,
526 "unexpected message from server");
527 goto done;
530 while (nsent > 0) {
531 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
532 if (err)
533 goto done;
534 if (n < 3) {
535 err = got_error_msg(GOT_ERR_BAD_PACKET,
536 "unexpected message from server");
537 goto done;
538 } else if (strncmp(buf, "ok ", 3) == 0) {
539 err = send_ref_status(ibuf, buf + 3, 1,
540 refs, delete_refs);
541 if (err)
542 goto done;
543 } else if (strncmp(buf, "ng ", 3) == 0) {
544 err = send_ref_status(ibuf, buf + 3, 0,
545 refs, delete_refs);
546 if (err)
547 goto done;
548 } else {
549 err = got_error_msg(GOT_ERR_BAD_PACKET,
550 "unexpected message from server");
551 goto done;
553 nsent--;
556 err = send_done(ibuf);
557 done:
558 TAILQ_FOREACH(pe, &their_refs, entry) {
559 free((void *)pe->path);
560 free(pe->data);
562 got_pathlist_free(&their_refs);
563 free(id_str);
564 free(id);
565 free(refname);
566 free(server_capabilities);
567 return err;
570 int
571 main(int argc, char **argv)
573 const struct got_error *err = NULL;
574 int sendfd;
575 struct imsgbuf ibuf;
576 struct imsg imsg;
577 struct got_pathlist_head refs;
578 struct got_pathlist_head delete_refs;
579 struct got_pathlist_entry *pe;
580 struct got_imsg_send_request send_req;
581 struct got_imsg_send_ref href;
582 size_t datalen, i;
583 #if 0
584 static int attached;
585 while (!attached)
586 sleep (1);
587 #endif
589 TAILQ_INIT(&refs);
590 TAILQ_INIT(&delete_refs);
592 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
593 #ifndef PROFILE
594 /* revoke access to most system calls */
595 if (pledge("stdio recvfd", NULL) == -1) {
596 err = got_error_from_errno("pledge");
597 got_privsep_send_error(&ibuf, err);
598 return 1;
600 #endif
601 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
602 if (err->code == GOT_ERR_PRIVSEP_PIPE)
603 err = NULL;
604 goto done;
606 if (imsg.hdr.type == GOT_IMSG_STOP)
607 goto done;
608 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
609 err = got_error(GOT_ERR_PRIVSEP_MSG);
610 goto done;
612 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
613 if (datalen < sizeof(send_req)) {
614 err = got_error(GOT_ERR_PRIVSEP_LEN);
615 goto done;
617 memcpy(&send_req, imsg.data, sizeof(send_req));
618 sendfd = imsg.fd;
619 imsg_free(&imsg);
621 if (send_req.verbosity > 0)
622 chattygot += send_req.verbosity;
624 for (i = 0; i < send_req.nrefs; i++) {
625 struct got_object_id *id;
626 char *refname;
628 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
629 if (err->code == GOT_ERR_PRIVSEP_PIPE)
630 err = NULL;
631 goto done;
633 if (imsg.hdr.type == GOT_IMSG_STOP)
634 goto done;
635 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
636 err = got_error(GOT_ERR_PRIVSEP_MSG);
637 goto done;
639 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
640 if (datalen < sizeof(href)) {
641 err = got_error(GOT_ERR_PRIVSEP_LEN);
642 goto done;
644 memcpy(&href, imsg.data, sizeof(href));
645 if (datalen - sizeof(href) < href.name_len) {
646 err = got_error(GOT_ERR_PRIVSEP_LEN);
647 goto done;
649 refname = malloc(href.name_len + 1);
650 if (refname == NULL) {
651 err = got_error_from_errno("malloc");
652 goto done;
654 memcpy(refname, imsg.data + sizeof(href), href.name_len);
655 refname[href.name_len] = '\0';
657 /*
658 * Prevent sending of references that won't make any
659 * sense outside the local repository's context.
660 */
661 if (strncmp(refname, "refs/got/", 9) == 0 ||
662 strncmp(refname, "refs/remotes/", 13) == 0) {
663 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
664 "%s", refname);
665 goto done;
668 id = malloc(sizeof(*id));
669 if (id == NULL) {
670 free(refname);
671 err = got_error_from_errno("malloc");
672 goto done;
674 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
675 if (href.delete)
676 err = got_pathlist_append(&delete_refs, refname, id);
677 else
678 err = got_pathlist_append(&refs, refname, id);
679 if (err) {
680 free(refname);
681 free(id);
682 goto done;
685 imsg_free(&imsg);
688 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
689 done:
690 TAILQ_FOREACH(pe, &refs, entry) {
691 free((char *)pe->path);
692 free(pe->data);
694 got_pathlist_free(&refs);
695 TAILQ_FOREACH(pe, &delete_refs, entry) {
696 free((char *)pe->path);
697 free(pe->data);
699 got_pathlist_free(&delete_refs);
700 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
701 err = got_error_from_errno("close");
702 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
703 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
704 got_privsep_send_error(&ibuf, err);
707 exit(0);