Blob


1 /*
2 * Copyright (c) 2018, 2019 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/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
23 #include <sys/mman.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdint.h>
28 #include <imsg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sha1.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_object.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
45 static volatile sig_atomic_t sigint_received;
47 static void
48 catch_sigint(int signo)
49 {
50 sigint_received = 1;
51 }
53 static const struct got_error *
54 open_object(struct got_object **obj, struct got_pack *pack,
55 struct got_packidx *packidx, int idx, struct got_object_id *id,
56 struct got_object_cache *objcache)
57 {
58 const struct got_error *err;
60 err = got_packfile_open_object(obj, pack, packidx, idx, id);
61 if (err)
62 return err;
63 (*obj)->refcnt++;
65 err = got_object_cache_add(objcache, id, *obj);
66 if (err) {
67 if (err->code == GOT_ERR_OBJ_EXISTS ||
68 err->code == GOT_ERR_OBJ_TOO_LARGE)
69 err = NULL;
70 return err;
71 }
72 (*obj)->refcnt++;
73 return NULL;
74 }
76 static const struct got_error *
77 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
78 struct got_packidx *packidx, struct got_object_cache *objcache)
79 {
80 const struct got_error *err = NULL;
81 struct got_imsg_packed_object iobj;
82 struct got_object *obj;
83 struct got_object_id id;
84 size_t datalen;
86 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
87 if (datalen != sizeof(iobj))
88 return got_error(GOT_ERR_PRIVSEP_LEN);
89 memcpy(&iobj, imsg->data, sizeof(iobj));
90 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
92 obj = got_object_cache_get(objcache, &id);
93 if (obj) {
94 obj->refcnt++;
95 } else {
96 err = open_object(&obj, pack, packidx, iobj.idx, &id,
97 objcache);
98 if (err)
99 goto done;
102 err = got_privsep_send_obj(ibuf, obj);
103 done:
104 got_object_close(obj);
105 return err;
108 static const struct got_error *
109 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
110 struct got_packidx *packidx, struct got_object_cache *objcache)
112 const struct got_error *err = NULL;
113 struct got_imsg_packed_object iobj;
114 struct got_object *obj = NULL;
115 struct got_commit_object *commit = NULL;
116 uint8_t *buf = NULL;
117 size_t len;
118 struct got_object_id id;
119 size_t datalen;
121 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
122 if (datalen != sizeof(iobj))
123 return got_error(GOT_ERR_PRIVSEP_LEN);
124 memcpy(&iobj, imsg->data, sizeof(iobj));
125 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
127 obj = got_object_cache_get(objcache, &id);
128 if (obj) {
129 obj->refcnt++;
130 } else {
131 err = open_object(&obj, pack, packidx, iobj.idx, &id,
132 objcache);
133 if (err)
134 return err;
137 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
138 if (err)
139 goto done;
141 obj->size = len;
142 err = got_object_parse_commit(&commit, buf, len);
143 if (err)
144 goto done;
146 err = got_privsep_send_commit(ibuf, commit);
147 done:
148 free(buf);
149 got_object_close(obj);
150 if (commit)
151 got_object_commit_close(commit);
152 if (err) {
153 if (err->code == GOT_ERR_PRIVSEP_PIPE)
154 err = NULL;
155 else
156 got_privsep_send_error(ibuf, err);
159 return err;
162 static const struct got_error *
163 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
164 struct got_packidx *packidx, struct got_object_cache *objcache)
166 const struct got_error *err = NULL;
167 struct got_imsg_packed_object iobj;
168 struct got_object *obj = NULL;
169 struct got_tree_object *tree = NULL;
170 uint8_t *buf = NULL;
171 size_t len;
172 struct got_object_id id;
173 size_t datalen;
175 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
176 if (datalen != sizeof(iobj))
177 return got_error(GOT_ERR_PRIVSEP_LEN);
178 memcpy(&iobj, imsg->data, sizeof(iobj));
179 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
181 obj = got_object_cache_get(objcache, &id);
182 if (obj) {
183 obj->refcnt++;
184 } else {
185 err = open_object(&obj, pack, packidx, iobj.idx, &id,
186 objcache);
187 if (err)
188 return err;
191 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
192 if (err)
193 goto done;
195 obj->size = len;
196 err = got_object_parse_tree(&tree, buf, len);
197 if (err)
198 goto done;
200 err = got_privsep_send_tree(ibuf, tree);
201 done:
202 free(buf);
203 got_object_close(obj);
204 if (tree)
205 got_object_tree_close(tree);
206 if (err) {
207 if (err->code == GOT_ERR_PRIVSEP_PIPE)
208 err = NULL;
209 else
210 got_privsep_send_error(ibuf, err);
213 return err;
216 static const struct got_error *
217 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
219 const struct got_error *err;
220 struct imsg imsg;
221 size_t datalen;
223 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
224 if (err)
225 return err;
227 if (imsg.hdr.type != imsg_code) {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
229 goto done;
232 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
233 if (datalen != 0) {
234 err = got_error(GOT_ERR_PRIVSEP_LEN);
235 goto done;
237 if (imsg.fd == -1) {
238 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
239 goto done;
242 *f = fdopen(imsg.fd, "w+");
243 if (*f == NULL) {
244 err = got_error_from_errno("fdopen");
245 close(imsg.fd);
246 goto done;
248 done:
249 imsg_free(&imsg);
250 return err;
253 static const struct got_error *
254 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
255 struct got_packidx *packidx, struct got_object_cache *objcache)
257 const struct got_error *err = NULL;
258 struct got_imsg_packed_object iobj;
259 struct got_object *obj = NULL;
260 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
261 struct got_object_id id;
262 size_t datalen;
263 uint64_t blob_size;
264 uint8_t *buf = NULL;
266 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
267 if (datalen != sizeof(iobj))
268 return got_error(GOT_ERR_PRIVSEP_LEN);
269 memcpy(&iobj, imsg->data, sizeof(iobj));
270 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
272 obj = got_object_cache_get(objcache, &id);
273 if (obj) {
274 obj->refcnt++;
275 } else {
276 err = open_object(&obj, pack, packidx, iobj.idx, &id,
277 objcache);
278 if (err)
279 return err;
282 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
283 if (err)
284 goto done;
285 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
286 if (err)
287 goto done;
288 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
289 if (err)
290 goto done;
292 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
293 err = got_pack_get_max_delta_object_size(&blob_size, obj);
294 if (err)
295 goto done;
296 } else
297 blob_size = obj->size;
299 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
300 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
301 obj, pack);
302 else
303 err = got_packfile_extract_object(pack, obj, outfile, basefile,
304 accumfile);
305 if (err)
306 goto done;
308 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
309 done:
310 free(buf);
311 if (outfile && fclose(outfile) != 0 && err == NULL)
312 err = got_error_from_errno("fclose");
313 if (basefile && fclose(basefile) != 0 && err == NULL)
314 err = got_error_from_errno("fclose");
315 if (accumfile && fclose(accumfile) != 0 && err == NULL)
316 err = got_error_from_errno("fclose");
317 got_object_close(obj);
318 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
319 got_privsep_send_error(ibuf, err);
321 return err;
324 static const struct got_error *
325 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
326 struct got_packidx *packidx, struct got_object_cache *objcache)
328 const struct got_error *err = NULL;
329 struct got_imsg_packed_object iobj;
330 struct got_object *obj = NULL;
331 struct got_tag_object *tag = NULL;
332 uint8_t *buf = NULL;
333 size_t len;
334 struct got_object_id id;
335 size_t datalen;
337 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
338 if (datalen != sizeof(iobj))
339 return got_error(GOT_ERR_PRIVSEP_LEN);
340 memcpy(&iobj, imsg->data, sizeof(iobj));
341 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
343 obj = got_object_cache_get(objcache, &id);
344 if (obj) {
345 obj->refcnt++;
346 } else {
347 err = open_object(&obj, pack, packidx, iobj.idx, &id,
348 objcache);
349 if (err)
350 return err;
353 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
354 if (err)
355 goto done;
357 obj->size = len;
358 err = got_object_parse_tag(&tag, buf, len);
359 if (err)
360 goto done;
362 err = got_privsep_send_tag(ibuf, tag);
363 done:
364 free(buf);
365 got_object_close(obj);
366 if (tag)
367 got_object_tag_close(tag);
368 if (err) {
369 if (err->code == GOT_ERR_PRIVSEP_PIPE)
370 err = NULL;
371 else
372 got_privsep_send_error(ibuf, err);
375 return err;
378 static const struct got_error *
379 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
381 const struct got_error *err = NULL;
382 struct imsg imsg;
383 struct got_imsg_packidx ipackidx;
384 size_t datalen;
385 struct got_packidx *p;
387 *packidx = NULL;
389 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
390 if (err)
391 return err;
393 p = calloc(1, sizeof(*p));
394 if (p == NULL) {
395 err = got_error_from_errno("calloc");
396 goto done;
399 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
400 err = got_error(GOT_ERR_PRIVSEP_MSG);
401 goto done;
404 if (imsg.fd == -1) {
405 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
406 goto done;
409 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
410 if (datalen != sizeof(ipackidx)) {
411 err = got_error(GOT_ERR_PRIVSEP_LEN);
412 goto done;
414 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
416 p->len = ipackidx.len;
417 p->fd = dup(imsg.fd);
418 if (p->fd == -1) {
419 err = got_error_from_errno("dup");
420 goto done;
422 if (lseek(p->fd, 0, SEEK_SET) == -1) {
423 err = got_error_from_errno("lseek");
424 goto done;
427 #ifndef GOT_PACK_NO_MMAP
428 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
429 if (p->map == MAP_FAILED)
430 p->map = NULL; /* fall back to read(2) */
431 #endif
432 err = got_packidx_init_hdr(p, 1);
433 done:
434 if (err) {
435 if (imsg.fd != -1)
436 close(imsg.fd);
437 got_packidx_close(p);
438 } else
439 *packidx = p;
440 imsg_free(&imsg);
441 return err;
444 static const struct got_error *
445 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
447 const struct got_error *err = NULL;
448 struct imsg imsg;
449 struct got_imsg_pack ipack;
450 size_t datalen;
451 struct got_pack *pack;
453 *packp = NULL;
455 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
456 if (err)
457 return err;
459 pack = calloc(1, sizeof(*pack));
460 if (pack == NULL) {
461 err = got_error_from_errno("calloc");
462 goto done;
465 if (imsg.hdr.type != GOT_IMSG_PACK) {
466 err = got_error(GOT_ERR_PRIVSEP_MSG);
467 goto done;
470 if (imsg.fd == -1) {
471 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
472 goto done;
475 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
476 if (datalen != sizeof(ipack)) {
477 err = got_error(GOT_ERR_PRIVSEP_LEN);
478 goto done;
480 memcpy(&ipack, imsg.data, sizeof(ipack));
482 pack->filesize = ipack.filesize;
483 pack->fd = dup(imsg.fd);
484 if (pack->fd == -1) {
485 err = got_error_from_errno("dup");
486 goto done;
488 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
489 err = got_error_from_errno("lseek");
490 goto done;
492 pack->path_packfile = strdup(ipack.path_packfile);
493 if (pack->path_packfile == NULL) {
494 err = got_error_from_errno("strdup");
495 goto done;
498 #ifndef GOT_PACK_NO_MMAP
499 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
500 pack->fd, 0);
501 if (pack->map == MAP_FAILED)
502 pack->map = NULL; /* fall back to read(2) */
503 #endif
504 done:
505 if (err) {
506 if (imsg.fd != -1)
507 close(imsg.fd);
508 free(pack);
509 } else
510 *packp = pack;
511 imsg_free(&imsg);
512 return err;
515 int
516 main(int argc, char *argv[])
518 const struct got_error *err = NULL;
519 struct imsgbuf ibuf;
520 struct imsg imsg;
521 struct got_packidx *packidx = NULL;
522 struct got_pack *pack = NULL;
523 struct got_object_cache objcache;
525 //static int attached;
526 //while (!attached) sleep(1);
528 signal(SIGINT, catch_sigint);
530 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
532 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
533 if (err) {
534 err = got_error_from_errno("got_object_cache_init");
535 got_privsep_send_error(&ibuf, err);
536 return 1;
539 #ifndef PROFILE
540 /* revoke access to most system calls */
541 if (pledge("stdio recvfd", NULL) == -1) {
542 err = got_error_from_errno("pledge");
543 got_privsep_send_error(&ibuf, err);
544 return 1;
546 #endif
548 err = receive_packidx(&packidx, &ibuf);
549 if (err) {
550 got_privsep_send_error(&ibuf, err);
551 return 1;
554 err = receive_pack(&pack, &ibuf);
555 if (err) {
556 got_privsep_send_error(&ibuf, err);
557 return 1;
560 for (;;) {
561 imsg.fd = -1;
563 if (sigint_received) {
564 err = got_error(GOT_ERR_CANCELLED);
565 break;
568 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
569 if (err) {
570 if (err->code == GOT_ERR_PRIVSEP_PIPE)
571 err = NULL;
572 break;
575 if (imsg.hdr.type == GOT_IMSG_STOP)
576 break;
578 switch (imsg.hdr.type) {
579 case GOT_IMSG_PACKED_OBJECT_REQUEST:
580 err = object_request(&imsg, &ibuf, pack, packidx,
581 &objcache);
582 break;
583 case GOT_IMSG_COMMIT_REQUEST:
584 err = commit_request(&imsg, &ibuf, pack, packidx,
585 &objcache);
586 break;
587 case GOT_IMSG_TREE_REQUEST:
588 err = tree_request(&imsg, &ibuf, pack, packidx,
589 &objcache);
590 break;
591 case GOT_IMSG_BLOB_REQUEST:
592 err = blob_request(&imsg, &ibuf, pack, packidx,
593 &objcache);
594 break;
595 case GOT_IMSG_TAG_REQUEST:
596 err = tag_request(&imsg, &ibuf, pack, packidx,
597 &objcache);
598 break;
599 default:
600 err = got_error(GOT_ERR_PRIVSEP_MSG);
601 break;
604 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
605 err = got_error_from_errno("close");
606 imsg_free(&imsg);
607 if (err)
608 break;
611 if (packidx)
612 got_packidx_close(packidx);
613 if (pack)
614 got_pack_close(pack);
615 got_object_cache_close(&objcache);
616 imsg_clear(&ibuf);
617 if (err) {
618 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
619 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
620 got_privsep_send_error(&ibuf, err);
623 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
624 err = got_error_from_errno("close");
625 return err ? 1 : 0;