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)
223 const struct got_error *err = NULL;
224 struct ibuf *wbuf;
225 size_t len, reflen = strlen(refname);
226 struct got_pathlist_entry *pe;
227 int ref_valid = 0;
228 char *eol;
230 eol = strchr(refname, '\n');
231 if (eol == NULL) {
232 return got_error_msg(GOT_ERR_BAD_PACKET,
233 "unexpected message from server");
235 *eol = '\0';
237 TAILQ_FOREACH(pe, refs, entry) {
238 if (strcmp(refname, pe->path) == 0) {
239 ref_valid = 1;
240 break;
243 if (!ref_valid) {
244 TAILQ_FOREACH(pe, delete_refs, entry) {
245 if (strcmp(refname, pe->path) == 0) {
246 ref_valid = 1;
247 break;
251 if (!ref_valid) {
252 return got_error_msg(GOT_ERR_BAD_PACKET,
253 "unexpected message from server");
256 len = sizeof(struct got_imsg_send_ref_status) + reflen;
257 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
258 return got_error(GOT_ERR_NO_SPACE);
260 wbuf = imsg_create(ibuf, GOT_IMSG_SEND_REF_STATUS,
261 0, 0, len);
262 if (wbuf == NULL)
263 return got_error_from_errno("imsg_create SEND_REF_STATUS");
265 /* Keep in sync with struct got_imsg_send_ref_status definition! */
266 if (imsg_add(wbuf, &success, sizeof(success)) == -1) {
267 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
268 ibuf_free(wbuf);
269 return err;
271 if (imsg_add(wbuf, &reflen, sizeof(reflen)) == -1) {
272 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
273 ibuf_free(wbuf);
274 return err;
276 if (imsg_add(wbuf, refname, reflen) == -1) {
277 err = got_error_from_errno("imsg_add SEND_REF_STATUS");
278 ibuf_free(wbuf);
279 return err;
282 wbuf->fd = -1;
283 imsg_close(ibuf, wbuf);
284 return got_privsep_flush_imsg(ibuf);
287 static const struct got_error *
288 describe_refchange(int *n, int *sent_my_capabilites,
289 const char *my_capabilities, char *buf, size_t bufsize,
290 const char *refname, const char *old_hashstr, const char *new_hashstr)
292 *n = snprintf(buf, bufsize, "%s %s %s",
293 old_hashstr, new_hashstr, refname);
294 if (*n >= bufsize)
295 return got_error(GOT_ERR_NO_SPACE);
297 /*
298 * We must announce our capabilities along with the first
299 * reference. Unfortunately, the protocol requires an embedded
300 * NUL as a separator between reference name and capabilities,
301 * which we have to deal with here.
302 * It also requires a linefeed for terminating packet data.
303 */
304 if (!*sent_my_capabilites && my_capabilities != NULL) {
305 int m;
306 if (*n >= bufsize - 1)
307 return got_error(GOT_ERR_NO_SPACE);
308 m = snprintf(buf + *n + 1, /* offset after '\0' */
309 bufsize - (*n + 1), "%s\n", my_capabilities);
310 if (*n + m >= bufsize)
311 return got_error(GOT_ERR_NO_SPACE);
312 *n += m;
313 *sent_my_capabilites = 1;
314 } else {
315 *n = strlcat(buf, "\n", bufsize);
316 if (*n >= bufsize)
317 return got_error(GOT_ERR_NO_SPACE);
320 return NULL;
323 static const struct got_error *
324 send_pack(int fd, struct got_pathlist_head *refs,
325 struct got_pathlist_head *delete_refs, struct imsgbuf *ibuf)
327 const struct got_error *err = NULL;
328 char buf[GOT_PKT_MAX];
329 unsigned char zero_id[SHA1_DIGEST_LENGTH] = { 0 };
330 char old_hashstr[SHA1_DIGEST_STRING_LENGTH];
331 char new_hashstr[SHA1_DIGEST_STRING_LENGTH];
332 struct got_pathlist_head their_refs;
333 int is_firstpkt = 1;
334 int n, nsent = 0;
335 int packfd = -1;
336 char *id_str = NULL, *refname = NULL;
337 struct got_object_id *id = NULL;
338 char *server_capabilities = NULL, *my_capabilities = NULL;
339 struct got_pathlist_entry *pe;
340 int sent_my_capabilites = 0;
342 TAILQ_INIT(&their_refs);
344 if (TAILQ_EMPTY(refs) && TAILQ_EMPTY(delete_refs))
345 return got_error(GOT_ERR_SEND_EMPTY);
347 while (1) {
348 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
349 if (err)
350 goto done;
351 if (n == 0)
352 break;
353 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
354 err = send_error(&buf[4], n - 4);
355 goto done;
357 err = got_gitproto_parse_refline(&id_str, &refname,
358 &server_capabilities, buf, n);
359 if (err)
360 goto done;
361 if (is_firstpkt) {
362 if (chattygot && server_capabilities[0] != '\0')
363 fprintf(stderr, "%s: server capabilities: %s\n",
364 getprogname(), server_capabilities);
365 err = got_gitproto_match_capabilities(&my_capabilities,
366 NULL, server_capabilities, got_capabilities,
367 nitems(got_capabilities));
368 if (err)
369 goto done;
370 if (chattygot)
371 fprintf(stderr, "%s: my capabilities:%s\n",
372 getprogname(), my_capabilities);
373 is_firstpkt = 0;
375 if (strstr(refname, "^{}")) {
376 if (chattygot) {
377 fprintf(stderr, "%s: ignoring %s\n",
378 getprogname(), refname);
380 continue;
383 id = malloc(sizeof(*id));
384 if (id == NULL) {
385 err = got_error_from_errno("malloc");
386 goto done;
388 if (!got_parse_sha1_digest(id->sha1, id_str)) {
389 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
390 goto done;
392 err = send_their_ref(ibuf, id, refname);
393 if (err)
394 goto done;
396 err = got_pathlist_append(&their_refs, refname, id);
397 if (chattygot)
398 fprintf(stderr, "%s: remote has %s %s\n",
399 getprogname(), refname, id_str);
400 free(id_str);
401 id_str = NULL;
402 refname = NULL; /* do not free; owned by their_refs */
403 id = NULL; /* do not free; owned by their_refs */
406 if (!TAILQ_EMPTY(delete_refs)) {
407 if (my_capabilities == NULL ||
408 strstr(my_capabilities, GOT_CAPA_DELETE_REFS) == NULL) {
409 err = got_error(GOT_ERR_CAPA_DELETE_REFS);
410 goto done;
414 TAILQ_FOREACH(pe, delete_refs, entry) {
415 const char *refname = pe->path;
416 struct got_pathlist_entry *their_pe;
417 struct got_object_id *their_id = NULL;
419 TAILQ_FOREACH(their_pe, &their_refs, entry) {
420 const char *their_refname = their_pe->path;
421 if (got_path_cmp(refname, their_refname,
422 strlen(refname), strlen(their_refname)) == 0) {
423 their_id = their_pe->data;
424 break;
427 if (their_id == NULL) {
428 err = got_error_fmt(GOT_ERR_NOT_REF,
429 "%s does not exist in remote repository",
430 refname);
431 goto done;
434 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
435 sizeof(old_hashstr));
436 got_sha1_digest_to_str(zero_id, new_hashstr,
437 sizeof(new_hashstr));
438 err = describe_refchange(&n, &sent_my_capabilites,
439 my_capabilities, buf, sizeof(buf), refname,
440 old_hashstr, new_hashstr);
441 if (err)
442 goto done;
443 err = got_pkt_writepkt(fd, buf, n, chattygot);
444 if (err)
445 goto done;
446 if (chattygot) {
447 fprintf(stderr, "%s: deleting %s %s\n",
448 getprogname(), refname, old_hashstr);
450 nsent++;
453 TAILQ_FOREACH(pe, refs, entry) {
454 const char *refname = pe->path;
455 struct got_object_id *id = pe->data;
456 struct got_object_id *their_id = NULL;
457 struct got_pathlist_entry *their_pe;
459 TAILQ_FOREACH(their_pe, &their_refs, entry) {
460 const char *their_refname = their_pe->path;
461 if (got_path_cmp(refname, their_refname,
462 strlen(refname), strlen(their_refname)) == 0) {
463 their_id = their_pe->data;
464 break;
467 if (their_id) {
468 if (got_object_id_cmp(id, their_id) == 0) {
469 if (chattygot) {
470 fprintf(stderr,
471 "%s: no change for %s\n",
472 getprogname(), refname);
474 continue;
476 got_sha1_digest_to_str(their_id->sha1, old_hashstr,
477 sizeof(old_hashstr));
478 } else {
479 got_sha1_digest_to_str(zero_id, old_hashstr,
480 sizeof(old_hashstr));
482 got_sha1_digest_to_str(id->sha1, new_hashstr,
483 sizeof(new_hashstr));
484 err = describe_refchange(&n, &sent_my_capabilites,
485 my_capabilities, buf, sizeof(buf), refname,
486 old_hashstr, new_hashstr);
487 if (err)
488 goto done;
489 err = got_pkt_writepkt(fd, buf, n, chattygot);
490 if (err)
491 goto done;
492 if (chattygot) {
493 if (their_id) {
494 fprintf(stderr, "%s: updating %s %s -> %s\n",
495 getprogname(), refname, old_hashstr,
496 new_hashstr);
497 } else {
498 fprintf(stderr, "%s: creating %s %s\n",
499 getprogname(), refname, new_hashstr);
502 nsent++;
504 err = got_pkt_flushpkt(fd, chattygot);
505 if (err)
506 goto done;
508 err = send_pack_request(ibuf);
509 if (err)
510 goto done;
512 err = recv_packfd(&packfd, ibuf);
513 if (err)
514 goto done;
516 if (packfd != -1) {
517 err = send_pack_file(fd, packfd, ibuf);
518 if (err)
519 goto done;
522 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
523 if (err)
524 goto done;
525 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
526 err = send_error(&buf[4], n - 4);
527 goto done;
528 } else if (n < 10 || strncmp(buf, "unpack ok\n", 10) != 0) {
529 err = got_error_msg(GOT_ERR_BAD_PACKET,
530 "unexpected message from server");
531 goto done;
534 while (nsent > 0) {
535 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
536 if (err)
537 goto done;
538 if (n < 3) {
539 err = got_error_msg(GOT_ERR_BAD_PACKET,
540 "unexpected message from server");
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, i;
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;
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);