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"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_poll.h"
57 #ifndef nitems
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 #endif
61 struct got_object *indexed;
62 static int chattygot;
64 static const struct got_capability got_capabilities[] = {
65 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
66 { GOT_CAPA_OFS_DELTA, NULL },
67 #if 0
68 { GOT_CAPA_SIDE_BAND_64K, NULL },
69 #endif
70 { GOT_CAPA_REPORT_STATUS, NULL },
71 { GOT_CAPA_DELETE_REFS, NULL },
72 };
74 static const struct got_error *
75 send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
76 struct got_ratelimit *rl)
77 {
78 const struct got_error *err = NULL;
79 int elapsed = 0;
81 if (rl) {
82 err = got_ratelimit_check(&elapsed, rl);
83 if (err || !elapsed)
84 return err;
85 }
87 if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
88 &bytes, sizeof(bytes)) == -1)
89 return got_error_from_errno(
90 "imsg_compose SEND_UPLOAD_PROGRESS");
92 return got_privsep_flush_imsg(ibuf);
93 }
95 static const struct got_error *
96 send_pack_request(struct imsgbuf *ibuf)
97 {
98 if (imsg_compose(ibuf, GOT_IMSG_SEND_PACK_REQUEST, 0, 0, -1,
99 NULL, 0) == -1)
100 return got_error_from_errno("imsg_compose SEND_PACK_REQUEST");
101 return got_privsep_flush_imsg(ibuf);
104 static const struct got_error *
105 send_done(struct imsgbuf *ibuf)
107 if (imsg_compose(ibuf, GOT_IMSG_SEND_DONE, 0, 0, -1, NULL, 0) == -1)
108 return got_error_from_errno("imsg_compose SEND_DONE");
109 return got_privsep_flush_imsg(ibuf);
112 static const struct got_error *
113 recv_packfd(int *packfd, struct imsgbuf *ibuf)
115 const struct got_error *err;
116 struct imsg imsg;
118 *packfd = -1;
120 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
121 if (err)
122 return err;
124 if (imsg.hdr.type == GOT_IMSG_STOP) {
125 err = got_error(GOT_ERR_CANCELLED);
126 goto done;
129 if (imsg.hdr.type != GOT_IMSG_SEND_PACKFD) {
130 err = got_error(GOT_ERR_PRIVSEP_MSG);
131 goto done;
134 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
135 err = got_error(GOT_ERR_PRIVSEP_LEN);
136 goto done;
139 *packfd = imsg.fd;
140 done:
141 imsg_free(&imsg);
142 return err;
145 static const struct got_error *
146 send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
148 const struct got_error *err;
149 unsigned char buf[8192];
150 ssize_t r;
151 off_t wtotal = 0;
152 struct got_ratelimit rl;
154 if (lseek(packfd, 0L, SEEK_SET) == -1)
155 return got_error_from_errno("lseek");
157 got_ratelimit_init(&rl, 0, 500);
159 for (;;) {
160 r = read(packfd, buf, sizeof(buf));
161 if (r == -1)
162 return got_error_from_errno("read");
163 if (r == 0)
164 break;
165 err = got_poll_write_full(sendfd, buf, r);
166 if (err)
167 return NULL;
168 wtotal += r;
169 err = send_upload_progress(ibuf, wtotal, &rl);
170 if (err)
171 return err;
174 return send_upload_progress(ibuf, wtotal, NULL);
177 static const struct got_error *
178 send_error(const char *buf, size_t len)
180 static char msg[1024];
181 size_t i;
183 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
184 if (!isprint(buf[i]))
185 return got_error_msg(GOT_ERR_BAD_PACKET,
186 "non-printable error message received from server");
187 msg[i] = buf[i];
189 msg[i] = '\0';
190 return got_error_msg(GOT_ERR_SEND_FAILED, msg);
193 static const struct got_error *
194 send_their_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
195 const char *refname)
197 struct ibuf *wbuf;
198 size_t len, reflen = strlen(refname);
200 len = sizeof(struct got_imsg_send_remote_ref) + reflen;
201 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
202 return got_error(GOT_ERR_NO_SPACE);
204 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REMOTE_REF, 0, 0, len);
205 if (wbuf == NULL)
206 return got_error_from_errno("imsg_create SEND_REMOTE_REF");
208 /* Keep in sync with struct got_imsg_send_remote_ref definition! */
209 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
210 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
211 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
212 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
213 if (imsg_add(wbuf, refname, reflen) == -1)
214 return got_error_from_errno("imsg_add SEND_REMOTE_REF");
216 wbuf->fd = -1;
217 imsg_close(ibuf, wbuf);
218 return got_privsep_flush_imsg(ibuf);
221 static const struct got_error *
222 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
223 struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
225 struct ibuf *wbuf;
226 size_t len, reflen = strlen(refname);
227 struct got_pathlist_entry *pe;
228 int ref_valid = 0;
229 char *eol;
231 eol = strchr(refname, '\n');
232 if (eol == NULL) {
233 return got_error_msg(GOT_ERR_BAD_PACKET,
234 "unexpected message from server");
236 *eol = '\0';
238 TAILQ_FOREACH(pe, refs, entry) {
239 if (strcmp(refname, pe->path) == 0) {
240 ref_valid = 1;
241 break;
244 if (!ref_valid) {
245 TAILQ_FOREACH(pe, delete_refs, entry) {
246 if (strcmp(refname, pe->path) == 0) {
247 ref_valid = 1;
248 break;
252 if (!ref_valid) {
253 return got_error_msg(GOT_ERR_BAD_PACKET,
254 "unexpected message from server");
257 len = sizeof(struct got_imsg_send_ref_status) + reflen;
258 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
259 return got_error(GOT_ERR_NO_SPACE);
261 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
262 0, 0, len);
263 if (wbuf == NULL)
264 return got_error_from_errno("imsg_create SEND_REF_STATUS");
266 /* Keep in sync with struct got_imsg_send_ref_status definition! */
267 if (imsg_add(wbuf, &success, sizeof(success)) == -1)
268 return got_error_from_errno("imsg_add SEND_REF_STATUS");
269 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1)
270 return got_error_from_errno("imsg_add SEND_REF_STATUS");
271 if (imsg_add(wbuf, refname, reflen) == -1)
272 return got_error_from_errno("imsg_add SEND_REF_STATUS");
274 wbuf->fd = -1;
275 imsg_close(ibuf, wbuf);
276 return got_privsep_flush_imsg(ibuf);
279 static const struct got_error *
280 describe_refchange(int *n, int *sent_my_capabilites,
281 const char *my_capabilities, char *buf, size_t bufsize,
282 const char *refname, const char *old_hashstr, const char *new_hashstr)
284 *n = snprintf(buf, bufsize, "%s %s %s",
285 old_hashstr, new_hashstr, refname);
286 if (*n < 0 || (size_t)*n >= bufsize)
287 return got_error(GOT_ERR_NO_SPACE);
289 /*
290 * We must announce our capabilities along with the first
291 * reference. Unfortunately, the protocol requires an embedded
292 * NUL as a separator between reference name and capabilities,
293 * which we have to deal with here.
294 * It also requires a linefeed for terminating packet data.
295 */
296 if (!*sent_my_capabilites && my_capabilities != NULL) {
297 int m;
298 if (*n >= bufsize - 1)
299 return got_error(GOT_ERR_NO_SPACE);
300 m = snprintf(buf + *n + 1, /* offset after '\0' */
301 bufsize - (*n + 1), "%s\n", my_capabilities);
302 if (m < 0 || *n + m >= bufsize)
303 return got_error(GOT_ERR_NO_SPACE);
304 *n += m;
305 *sent_my_capabilites = 1;
306 } else {
307 *n = strlcat(buf, "\n", bufsize);
308 if (*n >= bufsize)
309 return got_error(GOT_ERR_NO_SPACE);
312 return NULL;
315 static const struct got_error *
316 send_pack(int fd, struct got_pathlist_head *refs,
317 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
319 const struct got_error *err = NULL;
320 char buf[GOT_PKT_MAX];
321 const unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
322 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
323 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
324 struct got_pathlist_head their_refs;
325 int is_firstpkt = 1;
326 int n, nsent = 0;
327 int packfd = -1;
328 char *id_str = NULL, *refname = NULL;
329 struct got_object_id *id = NULL;
330 char *server_capabilities = NULL, *my_capabilities = NULL;
331 struct got_pathlist_entry *pe;
332 int sent_my_capabilites = 0;
334 TAILQ_INIT(&their_refs);
336 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
337 return got_error(GOT_ERR_SEND_EMPTY);
339 while (1) {
340 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
341 if (err)
342 goto done;
343 if (n == 0)
344 break;
345 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
346 err = send_error(&buf[4], n - 4);
347 goto done;
349 free(id_str);
350 free(refname);
351 err = got_gitproto_parse_refline(&id_str, &refname,
352 &server_capabilities, buf, n);
353 if (err)
354 goto done;
355 if (is_firstpkt) {
356 if (chattygot && server_capabilities[0] != '\0')
357 fprintf(stderr, "%s: server capabilities: %s\n",
358 getprogname(), server_capabilities);
359 err = got_gitproto_match_capabilities(&my_capabilities,
360 NULL, server_capabilities, got_capabilities,
361 nitems(got_capabilities));
362 if (err)
363 goto done;
364 if (chattygot)
365 fprintf(stderr, "%s: my capabilities:%s\n",
366 getprogname(), my_capabilities);
367 is_firstpkt = 0;
369 if (strstr(refname, "^{}")) {
370 if (chattygot) {
371 fprintf(stderr, "%s: ignoring %s\n",
372 getprogname(), refname);
374 continue;
377 id = malloc(sizeof(*id));
378 if (id == NULL) {
379 err = got_error_from_errno("malloc");
380 goto done;
382 if (!got_parse_sha1_digest(id->sha1, id_str)) {
383 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
384 goto done;
386 err = send_their_ref(ibuf, id, refname);
387 if (err)
388 goto done;
390 err = got_pathlist_append(&their_refs, refname, id);
391 if (err)
392 goto done;
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 (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
540 err = send_error(&buf[4], n - 4);
541 goto done;
542 } else if (strncmp(buf, "ok ", 3) == 0) {
543 err = send_ref_status(ibuf, buf + 3, 1,
544 refs, delete_refs);
545 if (err)
546 goto done;
547 } else if (strncmp(buf, "ng ", 3) == 0) {
548 err = send_ref_status(ibuf, buf + 3, 0,
549 refs, delete_refs);
550 if (err)
551 goto done;
552 } else {
553 err = got_error_msg(GOT_ERR_BAD_PACKET,
554 "unexpected message from server");
555 goto done;
557 nsent--;
560 err = send_done(ibuf);
561 done:
562 TAILQ_FOREACH(pe, &their_refs, entry) {
563 free((void *)pe->path);
564 free(pe->data);
566 got_pathlist_free(&their_refs);
567 free(id_str);
568 free(id);
569 free(refname);
570 free(server_capabilities);
571 return err;
574 int
575 main(int argc, char **argv)
577 const struct got_error *err = NULL;
578 int sendfd;
579 struct imsgbuf ibuf;
580 struct imsg imsg;
581 struct got_pathlist_head refs;
582 struct got_pathlist_head delete_refs;
583 struct got_pathlist_entry *pe;
584 struct got_imsg_send_request send_req;
585 struct got_imsg_send_ref href;
586 size_t datalen, i;
587 #if 0
588 static int attached;
589 while (!attached)
590 sleep (1);
591 #endif
593 TAILQ_INIT(&refs);
594 TAILQ_INIT(&delete_refs);
596 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
597 #ifndef PROFILE
598 /* revoke access to most system calls */
599 if (pledge("stdio recvfd", NULL) == -1) {
600 err = got_error_from_errno("pledge");
601 got_privsep_send_error(&ibuf, err);
602 return 1;
604 #endif
605 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
606 if (err->code == GOT_ERR_PRIVSEP_PIPE)
607 err = NULL;
608 goto done;
610 if (imsg.hdr.type == GOT_IMSG_STOP)
611 goto done;
612 if (imsg.hdr.type != GOT_IMSG_SEND_REQUEST) {
613 err = got_error(GOT_ERR_PRIVSEP_MSG);
614 goto done;
616 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
617 if (datalen < sizeof(send_req)) {
618 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 goto done;
621 memcpy(&send_req, imsg.data, sizeof(send_req));
622 sendfd = imsg.fd;
623 imsg_free(&imsg);
625 if (send_req.verbosity > 0)
626 chattygot += send_req.verbosity;
628 for (i = 0; i < send_req.nrefs; i++) {
629 struct got_object_id *id;
630 char *refname;
632 if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
633 if (err->code == GOT_ERR_PRIVSEP_PIPE)
634 err = NULL;
635 goto done;
637 if (imsg.hdr.type == GOT_IMSG_STOP)
638 goto done;
639 if (imsg.hdr.type != GOT_IMSG_SEND_REF) {
640 err = got_error(GOT_ERR_PRIVSEP_MSG);
641 goto done;
643 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
644 if (datalen < sizeof(href)) {
645 err = got_error(GOT_ERR_PRIVSEP_LEN);
646 goto done;
648 memcpy(&href, imsg.data, sizeof(href));
649 if (datalen - sizeof(href) < href.name_len) {
650 err = got_error(GOT_ERR_PRIVSEP_LEN);
651 goto done;
653 refname = malloc(href.name_len + 1);
654 if (refname == NULL) {
655 err = got_error_from_errno("malloc");
656 goto done;
658 memcpy(refname, imsg.data + sizeof(href), href.name_len);
659 refname[href.name_len] = '\0';
661 /*
662 * Prevent sending of references that won't make any
663 * sense outside the local repository's context.
664 */
665 if (strncmp(refname, "refs/got/", 9) == 0 ||
666 strncmp(refname, "refs/remotes/", 13) == 0) {
667 err = got_error_fmt(GOT_ERR_SEND_BAD_REF,
668 "%s", refname);
669 goto done;
672 id = malloc(sizeof(*id));
673 if (id == NULL) {
674 free(refname);
675 err = got_error_from_errno("malloc");
676 goto done;
678 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
679 if (href.delete)
680 err = got_pathlist_append(&delete_refs, refname, id);
681 else
682 err = got_pathlist_append(&refs, refname, id);
683 if (err) {
684 free(refname);
685 free(id);
686 goto done;
689 imsg_free(&imsg);
692 err = send_pack(sendfd, &refs, &delete_refs, &ibuf);
693 done:
694 TAILQ_FOREACH(pe, &refs, entry) {
695 free((char *)pe->path);
696 free(pe->data);
698 got_pathlist_free(&refs);
699 TAILQ_FOREACH(pe, &delete_refs, entry) {
700 free((char *)pe->path);
701 free(pe->data);
703 got_pathlist_free(&delete_refs);
704 if (sendfd != -1 && close(sendfd) == -1 && err == NULL)
705 err = got_error_from_errno("close");
706 if (err != NULL && err->code != GOT_ERR_CANCELLED) {
707 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
708 got_privsep_send_error(&ibuf, err);
711 exit(0);