Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/tree.h>
20 #include <sys/types.h>
22 #include <event.h>
23 #include <errno.h>
24 #include <imsg.h>
25 #include <signal.h>
26 #include <siphash.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <poll.h>
32 #include <sha1.h>
33 #include <unistd.h>
34 #include <zlib.h>
36 #include "buf.h"
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_path.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_delta_cache.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_ratelimit.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pack_index.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_poll.h"
54 #include "got_lib_sha1.h" /* XXX temp include for debugging */
56 #include "log.h"
57 #include "gotd.h"
58 #include "repo_write.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 #endif
64 static struct repo_write {
65 pid_t pid;
66 const char *title;
67 struct got_repository *repo;
68 int *pack_fds;
69 int *temp_fds;
70 } repo_write;
72 struct gotd_ref_update {
73 STAILQ_ENTRY(gotd_ref_update) entry;
74 struct got_reference *ref;
75 int ref_is_new;
76 struct got_object_id old_id;
77 struct got_object_id new_id;
78 };
79 STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
81 struct repo_write_client {
82 STAILQ_ENTRY(repo_write_client) entry;
83 uint32_t id;
84 int fd;
85 int pack_pipe;
86 struct got_pack pack;
87 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
88 int packidx_fd;
89 struct gotd_ref_updates ref_updates;
90 int nref_updates;
91 };
92 STAILQ_HEAD(repo_write_clients, repo_write_client);
94 static struct repo_write_clients repo_write_clients[GOTD_CLIENT_TABLE_SIZE];
95 static SIPHASH_KEY clients_hash_key;
97 static uint64_t
98 client_hash(uint32_t client_id)
99 {
100 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
103 static void
104 add_client(struct repo_write_client *client, uint32_t client_id, int fd)
106 uint64_t slot;
108 client->id = client_id;
109 client->fd = fd;
110 client->pack_pipe = -1;
111 client->packidx_fd = -1;
112 STAILQ_INIT(&client->ref_updates);
113 client->nref_updates = 0;
114 slot = client_hash(client->id) % nitems(repo_write_clients);
115 STAILQ_INSERT_HEAD(&repo_write_clients[slot], client, entry);
118 static struct repo_write_client *
119 find_client(uint32_t client_id)
121 uint64_t slot;
122 struct repo_write_client *c;
124 slot = client_hash(client_id) % nitems(repo_write_clients);
125 STAILQ_FOREACH(c, &repo_write_clients[slot], entry) {
126 if (c->id == client_id)
127 return c;
130 return NULL;
133 static volatile sig_atomic_t sigint_received;
134 static volatile sig_atomic_t sigterm_received;
136 static void
137 catch_sigint(int signo)
139 sigint_received = 1;
142 static void
143 catch_sigterm(int signo)
145 sigterm_received = 1;
148 static const struct got_error *
149 check_cancelled(void *arg)
151 if (sigint_received || sigterm_received)
152 return got_error(GOT_ERR_CANCELLED);
154 return NULL;
157 static const struct got_error *
158 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
159 struct imsgbuf *ibuf)
161 const struct got_error *err = NULL;
162 struct got_tag_object *tag;
163 size_t namelen, len;
164 char *peeled_refname = NULL;
165 struct got_object_id *id;
166 struct ibuf *wbuf;
168 err = got_object_tag_open(&tag, repo_write.repo, obj);
169 if (err)
170 return err;
172 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
173 err = got_error_from_errno("asprintf");
174 goto done;
177 id = got_object_tag_get_object_id(tag);
178 namelen = strlen(peeled_refname);
180 len = sizeof(struct gotd_imsg_ref) + namelen;
181 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
182 err = got_error(GOT_ERR_NO_SPACE);
183 goto done;
186 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
187 repo_write.pid, len);
188 if (wbuf == NULL) {
189 err = got_error_from_errno("imsg_create REF");
190 goto done;
193 /* Keep in sync with struct gotd_imsg_ref definition. */
194 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
195 err = got_error_from_errno("imsg_add REF");
196 goto done;
198 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
199 err = got_error_from_errno("imsg_add REF");
200 goto done;
202 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
203 err = got_error_from_errno("imsg_add REF");
204 goto done;
207 wbuf->fd = -1;
208 imsg_close(ibuf, wbuf);
209 done:
210 got_object_tag_close(tag);
211 return err;
214 static const struct got_error *
215 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
217 const struct got_error *err;
218 const char *refname = got_ref_get_name(ref);
219 size_t namelen;
220 struct got_object_id *id = NULL;
221 struct got_object *obj = NULL;
222 size_t len;
223 struct ibuf *wbuf;
225 namelen = strlen(refname);
227 len = sizeof(struct gotd_imsg_ref) + namelen;
228 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
229 return got_error(GOT_ERR_NO_SPACE);
231 err = got_ref_resolve(&id, repo_write.repo, ref);
232 if (err)
233 return err;
235 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
236 repo_write.pid, len);
237 if (wbuf == NULL) {
238 err = got_error_from_errno("imsg_create REF");
239 goto done;
242 /* Keep in sync with struct gotd_imsg_ref definition. */
243 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
244 return got_error_from_errno("imsg_add REF");
245 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
246 return got_error_from_errno("imsg_add REF");
247 if (imsg_add(wbuf, refname, namelen) == -1)
248 return got_error_from_errno("imsg_add REF");
250 wbuf->fd = -1;
251 imsg_close(ibuf, wbuf);
253 err = got_object_open(&obj, repo_write.repo, id);
254 if (err)
255 goto done;
256 if (obj->type == GOT_OBJ_TYPE_TAG)
257 err = send_peeled_tag_ref(ref, obj, ibuf);
258 done:
259 if (obj)
260 got_object_close(obj);
261 free(id);
262 return err;
265 static const struct got_error *
266 list_refs(struct repo_write_client **client, struct imsg *imsg)
268 const struct got_error *err;
269 struct got_reflist_head refs;
270 struct got_reflist_entry *re;
271 struct gotd_imsg_list_refs_internal ireq;
272 size_t datalen;
273 struct gotd_imsg_reflist irefs;
274 struct imsgbuf ibuf;
275 int client_fd = imsg->fd;
277 TAILQ_INIT(&refs);
279 if (client_fd == -1)
280 return got_error(GOT_ERR_PRIVSEP_NO_FD);
282 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
283 if (datalen != sizeof(ireq))
284 return got_error(GOT_ERR_PRIVSEP_LEN);
285 memcpy(&ireq, imsg->data, sizeof(ireq));
287 *client = find_client(ireq.client_id);
288 if (*client)
289 return got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
291 *client = calloc(1, sizeof(**client));
292 if (*client == NULL)
293 return got_error_from_errno("calloc");
294 add_client(*client, ireq.client_id, client_fd);
296 imsg_init(&ibuf, client_fd);
298 err = got_ref_list(&refs, repo_write.repo, "",
299 got_ref_cmp_by_name, NULL);
300 if (err)
301 return err;
303 memset(&irefs, 0, sizeof(irefs));
304 TAILQ_FOREACH(re, &refs, entry) {
305 struct got_object_id *id;
306 int obj_type;
308 if (got_ref_is_symbolic(re->ref))
309 continue;
311 irefs.nrefs++;
313 /* Account for a peeled tag refs. */
314 err = got_ref_resolve(&id, repo_write.repo, re->ref);
315 if (err)
316 goto done;
317 err = got_object_get_type(&obj_type, repo_write.repo, id);
318 free(id);
319 if (err)
320 goto done;
321 if (obj_type == GOT_OBJ_TYPE_TAG)
322 irefs.nrefs++;
325 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
326 repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
327 err = got_error_from_errno("imsg_compose REFLIST");
328 goto done;
331 TAILQ_FOREACH(re, &refs, entry) {
332 if (got_ref_is_symbolic(re->ref))
333 continue;
334 err = send_ref(re->ref, &ibuf);
335 if (err)
336 goto done;
339 err = gotd_imsg_flush(&ibuf);
340 done:
341 got_ref_list_free(&refs);
342 imsg_clear(&ibuf);
343 return err;
346 static const struct got_error *
347 protect_ref_namespace(struct got_reference *ref, const char *namespace)
349 size_t len = strlen(namespace);
351 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
352 namespace[len -1] != '/') {
353 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
354 "reference namespace '%s'", namespace);
357 if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
358 return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
360 return NULL;
363 static const struct got_error *
364 recv_ref_update(struct repo_write_client **client, struct imsg *imsg)
366 const struct got_error *err = NULL;
367 struct gotd_imsg_ref_update iref;
368 size_t datalen;
369 char *refname = NULL;
370 struct got_reference *ref = NULL;
371 struct got_object_id *id = NULL;
372 struct imsgbuf ibuf;
373 struct gotd_ref_update *ref_update = NULL;
375 log_debug("ref-update received");
377 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
378 if (datalen < sizeof(iref))
379 return got_error(GOT_ERR_PRIVSEP_LEN);
380 memcpy(&iref, imsg->data, sizeof(iref));
381 if (datalen != sizeof(iref) + iref.name_len)
382 return got_error(GOT_ERR_PRIVSEP_LEN);
384 *client = find_client(iref.client_id);
385 if (*client == NULL)
386 return got_error(GOT_ERR_CLIENT_ID);
388 imsg_init(&ibuf, (*client)->fd);
390 refname = malloc(iref.name_len + 1);
391 if (refname == NULL)
392 return got_error_from_errno("malloc");
393 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
394 refname[iref.name_len] = '\0';
396 ref_update = calloc(1, sizeof(*ref_update));
397 if (ref_update == NULL) {
398 err = got_error_from_errno("malloc");
399 goto done;
402 memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
403 memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
405 err = got_ref_open(&ref, repo_write.repo, refname, 0);
406 if (err) {
407 if (err->code != GOT_ERR_NOT_REF)
408 goto done;
409 err = got_ref_alloc(&ref, refname, &ref_update->new_id);
410 if (err)
411 goto done;
412 ref_update->ref_is_new = 1;
414 if (got_ref_is_symbolic(ref)) {
415 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
416 "'%s' is a symbolic reference and cannot "
417 "be updated", got_ref_get_name(ref));
418 goto done;
420 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
421 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
422 "%s: does not begin with 'refs/'",
423 got_ref_get_name(ref));
424 goto done;
427 err = protect_ref_namespace(ref, "refs/got/");
428 if (err)
429 goto done;
430 err = protect_ref_namespace(ref, "refs/remotes/");
431 if (err)
432 goto done;
434 if (!ref_update->ref_is_new) {
435 /*
436 * Ensure the client's idea of this update is still valid.
437 * At this point we can only return an error, to prevent
438 * the client from uploading a pack file which will likely
439 * have to be discarded.
440 */
441 err = got_ref_resolve(&id, repo_write.repo, ref);
442 if (err)
443 goto done;
445 if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
446 err = got_error_fmt(GOT_ERR_REF_BUSY,
447 "%s has been modified by someone else "
448 "while transaction was in progress",
449 got_ref_get_name(ref));
450 goto done;
454 gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
455 repo_write.pid);
457 ref_update->ref = ref;
458 STAILQ_INSERT_HEAD(&(*client)->ref_updates, ref_update, entry);
459 (*client)->nref_updates++;
460 ref = NULL;
461 ref_update = NULL;
462 done:
463 if (ref)
464 got_ref_close(ref);
465 free(ref_update);
466 free(refname);
467 free(id);
468 return err;
471 static const struct got_error *
472 pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
473 uint32_t nobj_loose, uint32_t nobj_resolved)
475 int p_indexed = 0, p_resolved = 0;
476 int nobj_delta = nobj_total - nobj_loose;
478 if (nobj_total > 0)
479 p_indexed = (nobj_indexed * 100) / nobj_total;
481 if (nobj_delta > 0)
482 p_resolved = (nobj_resolved * 100) / nobj_delta;
484 if (p_resolved > 0) {
485 log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
486 nobj_total, p_indexed, nobj_delta, p_resolved);
487 } else
488 log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
490 return NULL;
493 static const struct got_error *
494 read_more_pack_stream(int infd, BUF *buf, size_t minsize)
496 const struct got_error *err = NULL;
497 uint8_t readahead[65536];
498 size_t have, newlen;
500 err = got_poll_read_full(infd, &have,
501 readahead, sizeof(readahead), minsize);
502 if (err)
503 return err;
505 err = buf_append(&newlen, buf, readahead, have);
506 if (err)
507 return err;
508 return NULL;
511 static const struct got_error *
512 copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
513 off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
515 const struct got_error *err = NULL;
516 uint8_t t = 0;
517 uint64_t s = 0;
518 uint8_t sizebuf[8];
519 size_t i = 0;
520 off_t obj_offset = *outsize;
522 do {
523 /* We do not support size values which don't fit in 64 bit. */
524 if (i > 9)
525 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
526 "packfile offset %lld", (long long)obj_offset);
528 if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
529 err = read_more_pack_stream(infd, buf,
530 sizeof(sizebuf[0]));
531 if (err)
532 return err;
535 sizebuf[i] = buf_getc(buf, *buf_pos);
536 *buf_pos += sizeof(sizebuf[i]);
538 if (i == 0) {
539 t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
540 GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
541 s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
542 } else {
543 size_t shift = 4 + 7 * (i - 1);
544 s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
545 shift);
547 i++;
548 } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
550 err = got_pack_hwrite(outfd, sizebuf, i, ctx);
551 if (err)
552 return err;
553 *outsize += i;
555 *type = t;
556 *size = s;
557 return NULL;
560 static const struct got_error *
561 copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
562 SHA1_CTX *ctx)
564 const struct got_error *err = NULL;
565 size_t remain = buf_len(buf) - *buf_pos;
567 if (remain < SHA1_DIGEST_LENGTH) {
568 err = read_more_pack_stream(infd, buf,
569 SHA1_DIGEST_LENGTH - remain);
570 if (err)
571 return err;
574 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
575 SHA1_DIGEST_LENGTH, ctx);
576 if (err)
577 return err;
579 *buf_pos += SHA1_DIGEST_LENGTH;
580 return NULL;
583 static const struct got_error *
584 copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
585 SHA1_CTX *ctx)
587 const struct got_error *err = NULL;
588 uint64_t o = 0;
589 uint8_t offbuf[8];
590 size_t i = 0;
591 off_t obj_offset = *outsize;
593 do {
594 /* We do not support offset values which don't fit in 64 bit. */
595 if (i > 8)
596 return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
597 "packfile offset %lld", (long long)obj_offset);
599 if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
600 err = read_more_pack_stream(infd, buf,
601 sizeof(offbuf[0]));
602 if (err)
603 return err;
606 offbuf[i] = buf_getc(buf, *buf_pos);
607 *buf_pos += sizeof(offbuf[i]);
609 if (i == 0)
610 o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
611 else {
612 o++;
613 o <<= 7;
614 o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
616 i++;
617 } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
619 if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
620 return got_error(GOT_ERR_PACK_OFFSET);
622 err = got_pack_hwrite(outfd, offbuf, i, ctx);
623 if (err)
624 return err;
626 *outsize += i;
627 return NULL;
630 static const struct got_error *
631 copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
632 SHA1_CTX *ctx)
634 const struct got_error *err = NULL;
635 z_stream z;
636 int zret;
637 char voidbuf[1024];
638 size_t consumed_total = 0;
639 off_t zstream_offset = *outsize;
641 memset(&z, 0, sizeof(z));
643 z.zalloc = Z_NULL;
644 z.zfree = Z_NULL;
645 zret = inflateInit(&z);
646 if (zret != Z_OK) {
647 if (zret == Z_ERRNO)
648 return got_error_from_errno("inflateInit");
649 if (zret == Z_MEM_ERROR) {
650 errno = ENOMEM;
651 return got_error_from_errno("inflateInit");
653 return got_error_msg(GOT_ERR_DECOMPRESSION,
654 "inflateInit failed");
657 while (zret != Z_STREAM_END) {
658 size_t last_total_in, consumed;
660 /*
661 * Decompress into the void. Object data will be parsed
662 * later, when the pack file is indexed. For now, we just
663 * want to locate the end of the compressed stream.
664 */
665 while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
666 last_total_in = z.total_in;
667 z.next_in = buf_get(buf) + *buf_pos;
668 z.avail_in = buf_len(buf) - *buf_pos;
669 z.next_out = voidbuf;
670 z.avail_out = sizeof(voidbuf);
672 zret = inflate(&z, Z_SYNC_FLUSH);
673 if (zret != Z_OK && zret != Z_BUF_ERROR &&
674 zret != Z_STREAM_END) {
675 err = got_error_fmt(GOT_ERR_DECOMPRESSION,
676 "packfile offset %lld",
677 (long long)zstream_offset);
678 goto done;
680 consumed = z.total_in - last_total_in;
682 err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
683 consumed, ctx);
684 if (err)
685 goto done;
687 err = buf_discard(buf, *buf_pos + consumed);
688 if (err)
689 goto done;
690 *buf_pos = 0;
692 consumed_total += consumed;
695 if (zret != Z_STREAM_END) {
696 err = read_more_pack_stream(infd, buf, 1);
697 if (err)
698 goto done;
702 if (err == NULL)
703 *outsize += consumed_total;
704 done:
705 inflateEnd(&z);
706 return err;
709 static const struct got_error *
710 validate_object_type(int obj_type)
712 switch (obj_type) {
713 case GOT_OBJ_TYPE_BLOB:
714 case GOT_OBJ_TYPE_COMMIT:
715 case GOT_OBJ_TYPE_TREE:
716 case GOT_OBJ_TYPE_TAG:
717 case GOT_OBJ_TYPE_REF_DELTA:
718 case GOT_OBJ_TYPE_OFFSET_DELTA:
719 return NULL;
720 default:
721 break;
724 return got_error(GOT_ERR_OBJ_TYPE);
727 static const struct got_error *
728 recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
730 const struct got_error *err;
731 struct got_packfile_hdr hdr;
732 size_t have;
733 uint32_t nobj, nhave = 0;
734 SHA1_CTX ctx;
735 uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
736 char hex[SHA1_DIGEST_STRING_LENGTH];
737 BUF *buf = NULL;
738 size_t buf_pos = 0, remain;
739 ssize_t w;
741 *outsize = 0;
742 SHA1Init(&ctx);
744 err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
745 if (err)
746 return err;
747 if (have != sizeof(hdr))
748 return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
749 *outsize += have;
751 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
752 return got_error_msg(GOT_ERR_BAD_PACKFILE,
753 "bad packfile signature");
754 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
755 return got_error_msg(GOT_ERR_BAD_PACKFILE,
756 "bad packfile version");
758 nobj = be32toh(hdr.nobjects);
759 if (nobj == 0)
760 return got_error_msg(GOT_ERR_BAD_PACKFILE,
761 "bad packfile with zero objects");
763 log_debug("expecting %d objects", nobj);
765 err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
766 if (err)
767 return err;
769 err = buf_alloc(&buf, 65536);
770 if (err)
771 return err;
773 while (nhave != nobj) {
774 uint8_t obj_type;
775 uint64_t obj_size;
777 err = copy_object_type_and_size(&obj_type, &obj_size,
778 infd, outfd, outsize, buf, &buf_pos, &ctx);
779 if (err)
780 goto done;
782 err = validate_object_type(obj_type);
783 if (err)
784 goto done;
786 if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
787 err = copy_ref_delta(infd, outfd, outsize,
788 buf, &buf_pos, &ctx);
789 if (err)
790 goto done;
791 } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
792 err = copy_offset_delta(infd, outfd, outsize,
793 buf, &buf_pos, &ctx);
794 if (err)
795 goto done;
798 err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
799 if (err)
800 goto done;
802 nhave++;
805 log_debug("received %u objects", nobj);
807 SHA1Final(expected_sha1, &ctx);
809 remain = buf_len(buf) - buf_pos;
810 if (remain < SHA1_DIGEST_LENGTH) {
811 err = read_more_pack_stream(infd, buf,
812 SHA1_DIGEST_LENGTH - remain);
813 if (err)
814 return err;
817 got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
818 log_debug("expect SHA1: %s", hex);
819 got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
820 log_debug("actual SHA1: %s", hex);
822 if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
823 SHA1_DIGEST_LENGTH) != 0) {
824 err = got_error(GOT_ERR_PACKFILE_CSUM);
825 goto done;
828 memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
830 w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
831 if (w == -1) {
832 err = got_error_from_errno("write");
833 goto done;
835 if (w != SHA1_DIGEST_LENGTH) {
836 err = got_error(GOT_ERR_IO);
837 goto done;
840 *outsize += SHA1_DIGEST_LENGTH;
842 if (fsync(outfd) == -1) {
843 err = got_error_from_errno("fsync");
844 goto done;
846 if (lseek(outfd, 0L, SEEK_SET) == -1) {
847 err = got_error_from_errno("lseek");
848 goto done;
850 done:
851 buf_free(buf);
852 return err;
855 static const struct got_error *
856 report_pack_status(struct repo_write_client *client,
857 const struct got_error *unpack_err)
859 const struct got_error *err = NULL;
860 struct gotd_imsg_packfile_status istatus;
861 struct ibuf *wbuf;
862 struct imsgbuf ibuf;
863 const char *unpack_ok = "unpack ok\n";
864 size_t len;
866 imsg_init(&ibuf, client->fd);
868 if (unpack_err)
869 istatus.reason_len = strlen(unpack_err->msg);
870 else
871 istatus.reason_len = strlen(unpack_ok);
873 len = sizeof(istatus) + istatus.reason_len;
874 wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
875 repo_write.pid, len);
876 if (wbuf == NULL) {
877 err = got_error_from_errno("imsg_create PACKFILE_STATUS");
878 goto done;
881 if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
882 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
883 goto done;
886 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
887 istatus.reason_len) == -1) {
888 err = got_error_from_errno("imsg_add PACKFILE_STATUS");
889 goto done;
892 wbuf->fd = -1;
893 imsg_close(&ibuf, wbuf);
895 err = gotd_imsg_flush(&ibuf);
896 done:
897 imsg_clear(&ibuf);
898 return err;
901 static const struct got_error *
902 recv_packfile(struct repo_write_client **client, struct imsg *imsg)
904 const struct got_error *err = NULL, *unpack_err;
905 struct gotd_imsg_recv_packfile ireq;
906 FILE *tempfiles[3] = { NULL, NULL, NULL };
907 struct repo_tempfile {
908 int fd;
909 int idx;
910 } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
911 int i;
912 size_t datalen;
913 struct imsgbuf ibuf;
914 struct got_ratelimit rl;
915 struct got_pack *pack = NULL;
916 off_t pack_filesize = 0;
918 log_debug("packfile request received");
920 got_ratelimit_init(&rl, 2, 0);
922 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
923 if (datalen != sizeof(ireq))
924 return got_error(GOT_ERR_PRIVSEP_LEN);
925 memcpy(&ireq, imsg->data, sizeof(ireq));
927 *client = find_client(ireq.client_id);
928 if (*client == NULL || STAILQ_EMPTY(&(*client)->ref_updates))
929 return got_error(GOT_ERR_CLIENT_ID);
931 if ((*client)->pack_pipe == -1 ||
932 (*client)->packidx_fd == -1)
933 return got_error(GOT_ERR_PRIVSEP_NO_FD);
935 imsg_init(&ibuf, (*client)->fd);
937 if (imsg->fd == -1)
938 return got_error(GOT_ERR_PRIVSEP_NO_FD);
940 pack = &(*client)->pack;
941 memset(pack, 0, sizeof(*pack));
942 pack->fd = imsg->fd;
943 err = got_delta_cache_alloc(&pack->delta_cache);
944 if (err)
945 return err;
947 for (i = 0; i < nitems(repo_tempfiles); i++) {
948 struct repo_tempfile *t = &repo_tempfiles[i];
949 err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
950 if (err)
951 goto done;
954 for (i = 0; i < nitems(tempfiles); i++) {
955 int fd = dup(repo_tempfiles[i].fd);
956 FILE *f;
957 if (fd == -1) {
958 err = got_error_from_errno("dup");
959 goto done;
961 f = fdopen(fd, "w+");
962 if (f == NULL) {
963 err = got_error_from_errno("dup");
964 close(fd);
965 goto done;
967 tempfiles[i] = f;
970 err = gotd_imsg_flush(&ibuf);
971 if (err)
972 goto done;
974 log_debug("receiving pack data");
975 unpack_err = recv_packdata(&pack_filesize, (*client)->pack_sha1,
976 (*client)->pack_pipe, pack->fd);
977 if (ireq.report_status) {
978 err = report_pack_status(*client, unpack_err);
979 if (err) {
980 /* Git clients hang up after sending the pack file. */
981 if (err->code == GOT_ERR_EOF)
982 err = NULL;
985 if (unpack_err)
986 err = unpack_err;
987 if (err)
988 goto done;
990 log_debug("pack data received");
992 pack->filesize = pack_filesize;
994 log_debug("begin indexing pack (%lld bytes in size)",
995 (long long)pack->filesize);
996 err = got_pack_index(pack, (*client)->packidx_fd,
997 tempfiles[0], tempfiles[1], tempfiles[2], (*client)->pack_sha1,
998 pack_index_progress, NULL, &rl);
999 if (err)
1000 goto done;
1001 log_debug("done indexing pack");
1003 if (fsync((*client)->packidx_fd) == -1) {
1004 err = got_error_from_errno("fsync");
1005 goto done;
1007 if (lseek((*client)->packidx_fd, 0L, SEEK_SET) == -1)
1008 err = got_error_from_errno("lseek");
1009 done:
1010 if (close((*client)->pack_pipe) == -1 && err == NULL)
1011 err = got_error_from_errno("close");
1012 (*client)->pack_pipe = -1;
1013 for (i = 0; i < nitems(repo_tempfiles); i++) {
1014 struct repo_tempfile *t = &repo_tempfiles[i];
1015 if (t->idx != -1)
1016 got_repo_temp_fds_put(t->idx, repo_write.repo);
1018 for (i = 0; i < nitems(tempfiles); i++) {
1019 if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1020 err = got_error_from_errno("fclose");
1022 if (err)
1023 got_pack_close(pack);
1024 imsg_clear(&ibuf);
1025 return err;
1028 static const struct got_error *
1029 verify_packfile(struct repo_write_client *client)
1031 const struct got_error *err = NULL, *close_err;
1032 struct gotd_ref_update *ref_update;
1033 struct got_packidx *packidx = NULL;
1034 struct stat sb;
1035 char *id_str = NULL;
1036 int idx = -1;
1038 if (STAILQ_EMPTY(&client->ref_updates)) {
1039 return got_error_msg(GOT_ERR_BAD_REQUEST,
1040 "cannot verify pack file without any ref-updates");
1043 if (client->pack.fd == -1) {
1044 return got_error_msg(GOT_ERR_BAD_REQUEST,
1045 "invalid pack file handle during pack verification");
1047 if (client->packidx_fd == -1) {
1048 return got_error_msg(GOT_ERR_BAD_REQUEST,
1049 "invalid pack index handle during pack verification");
1052 if (fstat(client->packidx_fd, &sb) == -1)
1053 return got_error_from_errno("pack index fstat");
1055 packidx = malloc(sizeof(*packidx));
1056 memset(packidx, 0, sizeof(*packidx));
1057 packidx->fd = client->packidx_fd;
1058 client->packidx_fd = -1;
1059 packidx->len = sb.st_size;
1061 err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1062 if (err)
1063 return err;
1065 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1066 err = got_object_id_str(&id_str, &ref_update->new_id);
1067 if (err)
1068 goto done;
1070 idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1071 if (idx == -1) {
1072 err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1073 "advertised object %s is missing from pack file",
1074 id_str);
1075 goto done;
1079 done:
1080 close_err = got_packidx_close(packidx);
1081 if (close_err && err == NULL)
1082 err = close_err;
1083 free(id_str);
1084 return err;
1087 static const struct got_error *
1088 install_packfile(struct repo_write_client *client, struct gotd_imsgev *iev)
1090 struct gotd_imsg_packfile_install inst;
1091 int ret;
1093 memset(&inst, 0, sizeof(inst));
1094 inst.client_id = client->id;
1095 memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1097 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1098 PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1099 if (ret == -1)
1100 return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1102 return NULL;
1105 static const struct got_error *
1106 send_ref_updates_start(struct repo_write_client *client, int nref_updates,
1107 struct gotd_imsgev *iev)
1109 struct gotd_imsg_ref_updates_start istart;
1110 int ret;
1112 memset(&istart, 0, sizeof(istart));
1113 istart.nref_updates = nref_updates;
1114 istart.client_id = client->id;
1116 ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1117 PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1118 if (ret == -1)
1119 return got_error_from_errno("imsg_compose REF_UPDATES_START");
1121 return NULL;
1125 static const struct got_error *
1126 send_ref_update(struct repo_write_client *client,
1127 struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1129 struct gotd_imsg_ref_update iref;
1130 const char *refname = got_ref_get_name(ref_update->ref);
1131 struct ibuf *wbuf;
1132 size_t len;
1134 memset(&iref, 0, sizeof(iref));
1135 memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1136 memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1137 iref.ref_is_new = ref_update->ref_is_new;
1138 iref.client_id = client->id;
1139 iref.name_len = strlen(refname);
1141 len = sizeof(iref) + iref.name_len;
1142 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1143 repo_write.pid, len);
1144 if (wbuf == NULL)
1145 return got_error_from_errno("imsg_create REF_UPDATE");
1147 if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1148 return got_error_from_errno("imsg_add REF_UPDATE");
1149 if (imsg_add(wbuf, refname, iref.name_len) == -1)
1150 return got_error_from_errno("imsg_add REF_UPDATE");
1152 wbuf->fd = -1;
1153 imsg_close(&iev->ibuf, wbuf);
1155 gotd_imsg_event_add(iev);
1156 return NULL;
1159 static const struct got_error *
1160 update_refs(struct repo_write_client *client, struct gotd_imsgev *iev)
1162 const struct got_error *err = NULL;
1163 struct gotd_ref_update *ref_update;
1165 err = send_ref_updates_start(client, client->nref_updates, iev);
1166 if (err)
1167 return err;
1169 STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1170 err = send_ref_update(client, ref_update, iev);
1171 if (err)
1172 goto done;
1174 done:
1175 return err;
1178 static const struct got_error *
1179 recv_disconnect(struct imsg *imsg)
1181 const struct got_error *err = NULL;
1182 struct gotd_imsg_disconnect idisconnect;
1183 size_t datalen;
1184 int client_fd = -1, pack_pipe = -1, idxfd = -1;
1185 struct repo_write_client *client = NULL;
1186 uint64_t slot;
1188 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1189 if (datalen != sizeof(idisconnect))
1190 return got_error(GOT_ERR_PRIVSEP_LEN);
1191 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1193 log_debug("client disconnecting");
1195 client = find_client(idisconnect.client_id);
1196 if (client == NULL)
1197 return got_error(GOT_ERR_CLIENT_ID);
1199 slot = client_hash(client->id) % nitems(repo_write_clients);
1200 STAILQ_REMOVE(&repo_write_clients[slot], client, repo_write_client,
1201 entry);
1202 while (!STAILQ_EMPTY(&client->ref_updates)) {
1203 struct gotd_ref_update *ref_update;
1204 ref_update = STAILQ_FIRST(&client->ref_updates);
1205 STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1206 got_ref_close(ref_update->ref);
1207 free(ref_update);
1209 err = got_pack_close(&client->pack);
1210 client_fd = client->fd;
1211 pack_pipe = client->pack_pipe;
1212 idxfd = client->packidx_fd;
1213 free(client);
1214 if (client_fd != -1 && close(client_fd) == -1)
1215 err = got_error_from_errno("close");
1216 if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1217 err = got_error_from_errno("close");
1218 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1219 err = got_error_from_errno("close");
1220 return err;
1223 static const struct got_error *
1224 receive_pack_pipe(struct repo_write_client **client, struct imsg *imsg,
1225 struct gotd_imsgev *iev)
1227 struct gotd_imsg_packfile_pipe ireq;
1228 size_t datalen;
1230 log_debug("receving pack pipe descriptor");
1232 if (imsg->fd == -1)
1233 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1235 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1236 if (datalen != sizeof(ireq))
1237 return got_error(GOT_ERR_PRIVSEP_LEN);
1238 memcpy(&ireq, imsg->data, sizeof(ireq));
1240 *client = find_client(ireq.client_id);
1241 if (*client == NULL)
1242 return got_error(GOT_ERR_CLIENT_ID);
1243 if ((*client)->pack_pipe != -1)
1244 return got_error(GOT_ERR_PRIVSEP_MSG);
1246 (*client)->pack_pipe = imsg->fd;
1247 return NULL;
1250 static const struct got_error *
1251 receive_pack_idx(struct repo_write_client **client, struct imsg *imsg,
1252 struct gotd_imsgev *iev)
1254 struct gotd_imsg_packidx_file ireq;
1255 size_t datalen;
1257 log_debug("receving pack index output file");
1259 if (imsg->fd == -1)
1260 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1262 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1263 if (datalen != sizeof(ireq))
1264 return got_error(GOT_ERR_PRIVSEP_LEN);
1265 memcpy(&ireq, imsg->data, sizeof(ireq));
1267 *client = find_client(ireq.client_id);
1268 if (*client == NULL)
1269 return got_error(GOT_ERR_CLIENT_ID);
1270 if ((*client)->packidx_fd != -1)
1271 return got_error(GOT_ERR_PRIVSEP_MSG);
1273 (*client)->packidx_fd = imsg->fd;
1274 return NULL;
1277 static void
1278 repo_write_dispatch(int fd, short event, void *arg)
1280 const struct got_error *err = NULL;
1281 struct gotd_imsgev *iev = arg;
1282 struct imsgbuf *ibuf = &iev->ibuf;
1283 struct imsg imsg;
1284 struct repo_write_client *client = NULL;
1285 ssize_t n;
1286 int shut = 0;
1288 if (event & EV_READ) {
1289 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1290 fatal("imsg_read error");
1291 if (n == 0) /* Connection closed. */
1292 shut = 1;
1295 if (event & EV_WRITE) {
1296 n = msgbuf_write(&ibuf->w);
1297 if (n == -1 && errno != EAGAIN)
1298 fatal("msgbuf_write");
1299 if (n == 0) /* Connection closed. */
1300 shut = 1;
1303 for (;;) {
1304 if ((n = imsg_get(ibuf, &imsg)) == -1)
1305 fatal("%s: imsg_get error", __func__);
1306 if (n == 0) /* No more messages. */
1307 break;
1309 switch (imsg.hdr.type) {
1310 case GOTD_IMSG_LIST_REFS_INTERNAL:
1311 err = list_refs(&client, &imsg);
1312 if (err)
1313 log_warnx("%s: ls-refs: %s", repo_write.title,
1314 err->msg);
1315 break;
1316 case GOTD_IMSG_REF_UPDATE:
1317 err = recv_ref_update(&client, &imsg);
1318 if (err)
1319 log_warnx("%s: ref-update: %s",
1320 repo_write.title, err->msg);
1321 break;
1322 case GOTD_IMSG_PACKFILE_PIPE:
1323 err = receive_pack_pipe(&client, &imsg, iev);
1324 if (err) {
1325 log_warnx("%s: receiving pack pipe: %s",
1326 repo_write.title, err->msg);
1327 break;
1329 break;
1330 case GOTD_IMSG_PACKIDX_FILE:
1331 err = receive_pack_idx(&client, &imsg, iev);
1332 if (err) {
1333 log_warnx("%s: receiving pack index: %s",
1334 repo_write.title, err->msg);
1335 break;
1337 break;
1338 case GOTD_IMSG_RECV_PACKFILE:
1339 err = recv_packfile(&client, &imsg);
1340 if (err) {
1341 log_warnx("%s: receive packfile: %s",
1342 repo_write.title, err->msg);
1343 break;
1345 err = verify_packfile(client);
1346 if (err) {
1347 log_warnx("%s: verify packfile: %s",
1348 repo_write.title, err->msg);
1349 break;
1351 err = install_packfile(client, iev);
1352 if (err) {
1353 log_warnx("%s: install packfile: %s",
1354 repo_write.title, err->msg);
1355 break;
1357 err = update_refs(client, iev);
1358 if (err) {
1359 log_warnx("%s: update refs: %s",
1360 repo_write.title, err->msg);
1362 break;
1363 case GOTD_IMSG_DISCONNECT:
1364 err = recv_disconnect(&imsg);
1365 if (err)
1366 log_warnx("%s: disconnect: %s",
1367 repo_write.title, err->msg);
1368 break;
1369 default:
1370 log_debug("%s: unexpected imsg %d", repo_write.title,
1371 imsg.hdr.type);
1372 break;
1375 imsg_free(&imsg);
1378 if (!shut && check_cancelled(NULL) == NULL) {
1379 if (err &&
1380 gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1381 client ? client->id : 0, err) == -1) {
1382 log_warnx("could not send error to parent: %s",
1383 err->msg);
1385 gotd_imsg_event_add(iev);
1386 } else {
1387 /* This pipe is dead. Remove its event handler */
1388 event_del(&iev->ev);
1389 event_loopexit(NULL);
1393 void
1394 repo_write_main(const char *title, int *pack_fds, int *temp_fds)
1396 const struct got_error *err = NULL;
1397 struct gotd_imsgev iev;
1399 repo_write.title = title;
1400 repo_write.pid = getpid();
1401 repo_write.pack_fds = pack_fds;
1402 repo_write.temp_fds = temp_fds;
1404 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1407 * Open a repository in the root directory.
1408 * We are already in chroot at this point.
1410 err = got_repo_open(&repo_write.repo, "/", NULL, pack_fds);
1411 if (err)
1412 goto done;
1413 if (!got_repo_is_bare(repo_write.repo)) {
1414 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1415 "bare git repository required");
1416 goto done;
1419 got_repo_temp_fds_set(repo_write.repo, temp_fds);
1421 signal(SIGINT, catch_sigint);
1422 signal(SIGTERM, catch_sigterm);
1423 signal(SIGPIPE, SIG_IGN);
1424 signal(SIGHUP, SIG_IGN);
1426 imsg_init(&iev.ibuf, GOTD_SOCK_FILENO);
1427 iev.handler = repo_write_dispatch;
1428 iev.events = EV_READ;
1429 iev.handler_arg = NULL;
1430 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1431 if (event_add(&iev.ev, NULL) == -1) {
1432 err = got_error_from_errno("event_add");
1433 goto done;
1436 event_dispatch();
1437 done:
1438 if (err)
1439 log_warnx("%s: %s", title, err->msg);
1440 repo_write_shutdown();
1443 void
1444 repo_write_shutdown(void)
1446 log_debug("%s: shutting down", repo_write.title);
1447 if (repo_write.repo)
1448 got_repo_close(repo_write.repo);
1449 got_repo_pack_fds_close(repo_write.pack_fds);
1450 got_repo_temp_fds_close(repo_write.temp_fds);
1451 exit(0);