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/syslimits.h>
22 #include <sys/mman.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <imsg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sha1.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_cache.h"
40 #include "got_lib_object_parse.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
44 static volatile sig_atomic_t sigint_received;
46 static void
47 catch_sigint(int signo)
48 {
49 sigint_received = 1;
50 }
52 static const struct got_error *
53 open_object(struct got_object **obj, struct got_pack *pack,
54 struct got_packidx *packidx, int idx, struct got_object_id *id,
55 struct got_object_cache *objcache)
56 {
57 const struct got_error *err;
59 err = got_packfile_open_object(obj, pack, packidx, idx, id);
60 if (err)
61 return err;
62 (*obj)->refcnt++;
64 err = got_object_cache_add(objcache, id, *obj);
65 if (err) {
66 if (err->code == GOT_ERR_OBJ_EXISTS ||
67 err->code == GOT_ERR_OBJ_TOO_LARGE)
68 err = NULL;
69 return err;
70 }
71 (*obj)->refcnt++;
72 return NULL;
73 }
75 static const struct got_error *
76 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
77 struct got_packidx *packidx, struct got_object_cache *objcache)
78 {
79 const struct got_error *err = NULL;
80 struct got_imsg_packed_object iobj;
81 struct got_object *obj;
82 struct got_object_id id;
83 size_t datalen;
85 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
86 if (datalen != sizeof(iobj))
87 return got_error(GOT_ERR_PRIVSEP_LEN);
88 memcpy(&iobj, imsg->data, sizeof(iobj));
89 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
91 obj = got_object_cache_get(objcache, &id);
92 if (obj) {
93 obj->refcnt++;
94 } else {
95 err = open_object(&obj, pack, packidx, iobj.idx, &id,
96 objcache);
97 if (err)
98 goto done;
99 }
101 err = got_privsep_send_obj(ibuf, obj);
102 done:
103 got_object_close(obj);
104 return err;
107 static const struct got_error *
108 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
109 struct got_packidx *packidx, struct got_object_cache *objcache)
111 const struct got_error *err = NULL;
112 struct got_imsg_packed_object iobj;
113 struct got_object *obj = NULL;
114 struct got_commit_object *commit = NULL;
115 uint8_t *buf = NULL;
116 size_t len;
117 struct got_object_id id;
118 size_t datalen;
120 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
121 if (datalen != sizeof(iobj))
122 return got_error(GOT_ERR_PRIVSEP_LEN);
123 memcpy(&iobj, imsg->data, sizeof(iobj));
124 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
126 obj = got_object_cache_get(objcache, &id);
127 if (obj) {
128 obj->refcnt++;
129 } else {
130 err = open_object(&obj, pack, packidx, iobj.idx, &id,
131 objcache);
132 if (err)
133 return err;
136 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
137 if (err)
138 goto done;
140 obj->size = len;
141 err = got_object_parse_commit(&commit, buf, len);
142 if (err)
143 goto done;
145 err = got_privsep_send_commit(ibuf, commit);
146 done:
147 free(buf);
148 got_object_close(obj);
149 if (commit)
150 got_object_commit_close(commit);
151 if (err) {
152 if (err->code == GOT_ERR_PRIVSEP_PIPE)
153 err = NULL;
154 else
155 got_privsep_send_error(ibuf, err);
158 return err;
161 static const struct got_error *
162 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
163 struct got_packidx *packidx, struct got_object_cache *objcache)
165 const struct got_error *err = NULL;
166 struct got_imsg_packed_object iobj;
167 struct got_object *obj = NULL;
168 struct got_tree_object *tree = NULL;
169 uint8_t *buf = NULL;
170 size_t len;
171 struct got_object_id id;
172 size_t datalen;
174 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
175 if (datalen != sizeof(iobj))
176 return got_error(GOT_ERR_PRIVSEP_LEN);
177 memcpy(&iobj, imsg->data, sizeof(iobj));
178 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
180 obj = got_object_cache_get(objcache, &id);
181 if (obj) {
182 obj->refcnt++;
183 } else {
184 err = open_object(&obj, pack, packidx, iobj.idx, &id,
185 objcache);
186 if (err)
187 return err;
190 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
191 if (err)
192 goto done;
194 obj->size = len;
195 err = got_object_parse_tree(&tree, buf, len);
196 if (err)
197 goto done;
199 err = got_privsep_send_tree(ibuf, tree);
200 done:
201 free(buf);
202 got_object_close(obj);
203 if (tree)
204 got_object_tree_close(tree);
205 if (err) {
206 if (err->code == GOT_ERR_PRIVSEP_PIPE)
207 err = NULL;
208 else
209 got_privsep_send_error(ibuf, err);
212 return err;
215 static const struct got_error *
216 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
218 const struct got_error *err;
219 struct imsg imsg;
220 size_t datalen;
222 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
223 if (err)
224 return err;
226 if (imsg.hdr.type != imsg_code) {
227 err = got_error(GOT_ERR_PRIVSEP_MSG);
228 goto done;
231 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
232 if (datalen != 0) {
233 err = got_error(GOT_ERR_PRIVSEP_LEN);
234 goto done;
236 if (imsg.fd == -1) {
237 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
238 goto done;
241 *f = fdopen(imsg.fd, "w+");
242 if (*f == NULL) {
243 err = got_error_from_errno("fdopen");
244 close(imsg.fd);
245 goto done;
247 done:
248 imsg_free(&imsg);
249 return err;
252 static const struct got_error *
253 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
254 struct got_packidx *packidx, struct got_object_cache *objcache)
256 const struct got_error *err = NULL;
257 struct got_imsg_packed_object iobj;
258 struct got_object *obj = NULL;
259 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
260 struct got_object_id id;
261 size_t datalen;
262 uint64_t blob_size;
263 uint8_t *buf = NULL;
265 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
266 if (datalen != sizeof(iobj))
267 return got_error(GOT_ERR_PRIVSEP_LEN);
268 memcpy(&iobj, imsg->data, sizeof(iobj));
269 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
271 obj = got_object_cache_get(objcache, &id);
272 if (obj) {
273 obj->refcnt++;
274 } else {
275 err = open_object(&obj, pack, packidx, iobj.idx, &id,
276 objcache);
277 if (err)
278 return err;
281 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
282 if (err)
283 goto done;
284 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
285 if (err)
286 goto done;
287 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
288 if (err)
289 goto done;
291 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
292 err = got_pack_get_max_delta_object_size(&blob_size, obj);
293 if (err)
294 goto done;
295 } else
296 blob_size = obj->size;
298 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
299 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
300 obj, pack);
301 else
302 err = got_packfile_extract_object(pack, obj, outfile, basefile,
303 accumfile);
304 if (err)
305 goto done;
307 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
308 done:
309 free(buf);
310 if (outfile && fclose(outfile) != 0 && err == NULL)
311 err = got_error_from_errno("fclose");
312 if (basefile && fclose(basefile) != 0 && err == NULL)
313 err = got_error_from_errno("fclose");
314 if (accumfile && fclose(accumfile) != 0 && err == NULL)
315 err = got_error_from_errno("fclose");
316 got_object_close(obj);
317 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
318 got_privsep_send_error(ibuf, err);
320 return err;
323 static const struct got_error *
324 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
325 struct got_packidx *packidx, struct got_object_cache *objcache)
327 const struct got_error *err = NULL;
328 struct got_imsg_packed_object iobj;
329 struct got_object *obj = NULL;
330 struct got_tag_object *tag = NULL;
331 uint8_t *buf = NULL;
332 size_t len;
333 struct got_object_id id;
334 size_t datalen;
336 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
337 if (datalen != sizeof(iobj))
338 return got_error(GOT_ERR_PRIVSEP_LEN);
339 memcpy(&iobj, imsg->data, sizeof(iobj));
340 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
342 obj = got_object_cache_get(objcache, &id);
343 if (obj) {
344 obj->refcnt++;
345 } else {
346 err = open_object(&obj, pack, packidx, iobj.idx, &id,
347 objcache);
348 if (err)
349 return err;
352 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
353 if (err)
354 goto done;
356 obj->size = len;
357 err = got_object_parse_tag(&tag, buf, len);
358 if (err)
359 goto done;
361 err = got_privsep_send_tag(ibuf, tag);
362 done:
363 free(buf);
364 got_object_close(obj);
365 if (tag)
366 got_object_tag_close(tag);
367 if (err) {
368 if (err->code == GOT_ERR_PRIVSEP_PIPE)
369 err = NULL;
370 else
371 got_privsep_send_error(ibuf, err);
374 return err;
377 static const struct got_error *
378 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
380 const struct got_error *err = NULL;
381 struct imsg imsg;
382 struct got_imsg_packidx ipackidx;
383 size_t datalen;
384 struct got_packidx *p;
386 *packidx = NULL;
388 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
389 if (err)
390 return err;
392 p = calloc(1, sizeof(*p));
393 if (p == NULL) {
394 err = got_error_from_errno("calloc");
395 goto done;
398 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
399 err = got_error(GOT_ERR_PRIVSEP_MSG);
400 goto done;
403 if (imsg.fd == -1) {
404 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
405 goto done;
408 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
409 if (datalen != sizeof(ipackidx)) {
410 err = got_error(GOT_ERR_PRIVSEP_LEN);
411 goto done;
413 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
415 p->len = ipackidx.len;
416 p->fd = dup(imsg.fd);
417 if (p->fd == -1) {
418 err = got_error_from_errno("dup");
419 goto done;
421 if (lseek(p->fd, 0, SEEK_SET) == -1) {
422 err = got_error_from_errno("lseek");
423 goto done;
426 #ifndef GOT_PACK_NO_MMAP
427 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
428 if (p->map == MAP_FAILED)
429 p->map = NULL; /* fall back to read(2) */
430 #endif
431 err = got_packidx_init_hdr(p, 1);
432 done:
433 if (err) {
434 if (imsg.fd != -1)
435 close(imsg.fd);
436 got_packidx_close(p);
437 } else
438 *packidx = p;
439 imsg_free(&imsg);
440 return err;
443 static const struct got_error *
444 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
446 const struct got_error *err = NULL;
447 struct imsg imsg;
448 struct got_imsg_pack ipack;
449 size_t datalen;
450 struct got_pack *pack;
452 *packp = NULL;
454 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
455 if (err)
456 return err;
458 pack = calloc(1, sizeof(*pack));
459 if (pack == NULL) {
460 err = got_error_from_errno("calloc");
461 goto done;
464 if (imsg.hdr.type != GOT_IMSG_PACK) {
465 err = got_error(GOT_ERR_PRIVSEP_MSG);
466 goto done;
469 if (imsg.fd == -1) {
470 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
471 goto done;
474 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
475 if (datalen != sizeof(ipack)) {
476 err = got_error(GOT_ERR_PRIVSEP_LEN);
477 goto done;
479 memcpy(&ipack, imsg.data, sizeof(ipack));
481 pack->filesize = ipack.filesize;
482 pack->fd = dup(imsg.fd);
483 if (pack->fd == -1) {
484 err = got_error_from_errno("dup");
485 goto done;
487 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
488 err = got_error_from_errno("lseek");
489 goto done;
491 pack->path_packfile = strdup(ipack.path_packfile);
492 if (pack->path_packfile == NULL) {
493 err = got_error_from_errno("strdup");
494 goto done;
497 #ifndef GOT_PACK_NO_MMAP
498 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
499 pack->fd, 0);
500 if (pack->map == MAP_FAILED)
501 pack->map = NULL; /* fall back to read(2) */
502 #endif
503 done:
504 if (err) {
505 if (imsg.fd != -1)
506 close(imsg.fd);
507 free(pack);
508 } else
509 *packp = pack;
510 imsg_free(&imsg);
511 return err;
514 int
515 main(int argc, char *argv[])
517 const struct got_error *err = NULL;
518 struct imsgbuf ibuf;
519 struct imsg imsg;
520 struct got_packidx *packidx = NULL;
521 struct got_pack *pack = NULL;
522 struct got_object_cache objcache;
524 //static int attached;
525 //while (!attached) sleep(1);
527 signal(SIGINT, catch_sigint);
529 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
531 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
532 if (err) {
533 err = got_error_from_errno("got_object_cache_init");
534 got_privsep_send_error(&ibuf, err);
535 return 1;
538 #ifndef PROFILE
539 /* revoke access to most system calls */
540 if (pledge("stdio recvfd", NULL) == -1) {
541 err = got_error_from_errno("pledge");
542 got_privsep_send_error(&ibuf, err);
543 return 1;
545 #endif
547 err = receive_packidx(&packidx, &ibuf);
548 if (err) {
549 got_privsep_send_error(&ibuf, err);
550 return 1;
553 err = receive_pack(&pack, &ibuf);
554 if (err) {
555 got_privsep_send_error(&ibuf, err);
556 return 1;
559 for (;;) {
560 imsg.fd = -1;
562 if (sigint_received) {
563 err = got_error(GOT_ERR_CANCELLED);
564 break;
567 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
568 if (err) {
569 if (err->code == GOT_ERR_PRIVSEP_PIPE)
570 err = NULL;
571 break;
574 if (imsg.hdr.type == GOT_IMSG_STOP)
575 break;
577 switch (imsg.hdr.type) {
578 case GOT_IMSG_PACKED_OBJECT_REQUEST:
579 err = object_request(&imsg, &ibuf, pack, packidx,
580 &objcache);
581 break;
582 case GOT_IMSG_COMMIT_REQUEST:
583 err = commit_request(&imsg, &ibuf, pack, packidx,
584 &objcache);
585 break;
586 case GOT_IMSG_TREE_REQUEST:
587 err = tree_request(&imsg, &ibuf, pack, packidx,
588 &objcache);
589 break;
590 case GOT_IMSG_BLOB_REQUEST:
591 err = blob_request(&imsg, &ibuf, pack, packidx,
592 &objcache);
593 break;
594 case GOT_IMSG_TAG_REQUEST:
595 err = tag_request(&imsg, &ibuf, pack, packidx,
596 &objcache);
597 break;
598 default:
599 err = got_error(GOT_ERR_PRIVSEP_MSG);
600 break;
603 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
604 err = got_error_from_errno("close");
605 imsg_free(&imsg);
606 if (err)
607 break;
610 if (packidx)
611 got_packidx_close(packidx);
612 if (pack)
613 got_pack_close(pack);
614 got_object_cache_close(&objcache);
615 imsg_clear(&ibuf);
616 if (err) {
617 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
618 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
619 got_privsep_send_error(&ibuf, err);
622 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
623 err = got_error_from_errno("close");
624 return err ? 1 : 0;