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_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
48 static void
49 catch_sigint(int signo)
50 {
51 sigint_received = 1;
52 }
54 static const struct got_error *
55 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
56 struct got_packidx *packidx, struct got_object_cache *objcache)
57 {
58 const struct got_error *err = NULL;
59 struct got_imsg_packed_object iobj;
60 struct got_object *obj;
61 struct got_object_id id;
62 size_t datalen;
64 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
65 if (datalen != sizeof(iobj))
66 return got_error(GOT_ERR_PRIVSEP_LEN);
67 memcpy(&iobj, imsg->data, sizeof(iobj));
68 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
70 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
71 if (err)
72 return err;
73 obj->refcnt++;
75 err = got_object_cache_add(objcache, &obj->id, obj);
76 if (err)
77 goto done;
78 obj->refcnt++;
80 err = got_privsep_send_obj(ibuf, obj);
81 done:
82 got_object_close(obj);
83 return err;
84 }
86 static const struct got_error *
87 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
88 struct got_packidx *packidx, struct got_object_cache *objcache)
89 {
90 const struct got_error *err = NULL;
91 struct got_imsg_packed_object iobj;
92 struct got_object *obj;
93 struct got_commit_object *commit = NULL;
94 uint8_t *buf;
95 size_t len;
96 struct got_object_id id;
97 size_t datalen;
99 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
100 if (datalen != sizeof(iobj))
101 return got_error(GOT_ERR_PRIVSEP_LEN);
102 memcpy(&iobj, imsg->data, sizeof(iobj));
103 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
105 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
106 if (err)
107 return err;
109 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
110 if (err)
111 return err;
113 obj->size = len;
114 err = got_object_parse_commit(&commit, buf, len);
115 free(buf);
116 if (err) {
117 got_object_close(obj);
118 return err;
121 err = got_privsep_send_commit(ibuf, commit);
122 got_object_commit_close(commit);
123 if (err) {
124 if (err->code == GOT_ERR_PRIVSEP_PIPE)
125 err = NULL;
126 else
127 got_privsep_send_error(ibuf, err);
130 return err;
133 static const struct got_error *
134 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
135 struct got_packidx *packidx, struct got_object_cache *objcache)
137 const struct got_error *err = NULL;
138 struct got_imsg_packed_object iobj;
139 struct got_object *obj = NULL;
140 struct got_tree_object *tree = NULL;
141 uint8_t *buf;
142 size_t len;
143 struct got_object_id id;
144 size_t datalen;
146 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
147 if (datalen != sizeof(iobj))
148 return got_error(GOT_ERR_PRIVSEP_LEN);
149 memcpy(&iobj, imsg->data, sizeof(iobj));
150 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
152 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
153 if (err)
154 return err;
156 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
157 if (err)
158 return err;
160 obj->size = len;
161 err = got_object_parse_tree(&tree, buf, len);
162 free(buf);
164 err = got_privsep_send_tree(ibuf, tree);
165 if (obj)
166 got_object_close(obj);
167 got_object_tree_close(tree);
168 if (err) {
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
170 err = NULL;
171 else
172 got_privsep_send_error(ibuf, err);
175 return err;
178 static const struct got_error *
179 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
181 const struct got_error *err;
182 struct imsg imsg;
183 size_t datalen;
185 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
186 if (err)
187 return err;
189 if (imsg.hdr.type != imsg_code) {
190 err = got_error(GOT_ERR_PRIVSEP_MSG);
191 goto done;
194 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
195 if (datalen != 0) {
196 err = got_error(GOT_ERR_PRIVSEP_LEN);
197 goto done;
199 if (imsg.fd == -1) {
200 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
201 goto done;
204 *f = fdopen(imsg.fd, "w+");
205 if (*f == NULL) {
206 err = got_error_from_errno();
207 close(imsg.fd);
208 goto done;
210 done:
211 imsg_free(&imsg);
212 return err;
215 static const struct got_error *
216 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 struct got_packidx *packidx, struct got_object_cache *objcache)
219 const struct got_error *err = NULL;
220 struct got_imsg_packed_object iobj;
221 struct got_object *obj = NULL;
222 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
223 struct got_object_id id;
224 size_t datalen;
225 uint64_t blob_size;
226 uint8_t *buf = NULL;
228 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
229 if (datalen != sizeof(iobj))
230 return got_error(GOT_ERR_PRIVSEP_LEN);
231 memcpy(&iobj, imsg->data, sizeof(iobj));
232 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
234 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
235 if (err)
236 return err;
238 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
239 if (err)
240 goto done;
241 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
242 if (err)
243 goto done;
244 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
245 if (err)
246 goto done;
248 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
249 err = got_pack_get_max_delta_object_size(&blob_size, obj);
250 if (err)
251 goto done;
252 } else
253 blob_size = obj->size;
255 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
256 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
257 obj, pack);
258 else
259 err = got_packfile_extract_object(pack, obj, outfile, basefile,
260 accumfile);
261 if (err)
262 goto done;
264 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
265 done:
266 free(buf);
267 if (outfile && fclose(outfile) != 0 && err == NULL)
268 err = got_error_from_errno();
269 if (basefile && fclose(basefile) != 0 && err == NULL)
270 err = got_error_from_errno();
271 if (accumfile && fclose(accumfile) != 0 && err == NULL)
272 err = got_error_from_errno();
273 if (obj)
274 got_object_close(obj);
275 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
276 got_privsep_send_error(ibuf, err);
278 return err;
281 static const struct got_error *
282 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
283 struct got_packidx *packidx, struct got_object_cache *objcache)
285 const struct got_error *err = NULL;
286 struct got_imsg_packed_object iobj;
287 struct got_object *obj = NULL;
288 struct got_tag_object *tag = NULL;
289 uint8_t *buf;
290 size_t len;
291 struct got_object_id id;
292 size_t datalen;
294 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
295 if (datalen != sizeof(iobj))
296 return got_error(GOT_ERR_PRIVSEP_LEN);
297 memcpy(&iobj, imsg->data, sizeof(iobj));
298 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
300 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
301 if (err)
302 return err;
304 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
305 if (err)
306 return err;
308 obj->size = len;
309 err = got_object_parse_tag(&tag, buf, len);
310 free(buf);
311 if (err)
312 return err;
314 err = got_privsep_send_tag(ibuf, tag);
315 if (obj)
316 got_object_close(obj);
317 got_object_tag_close(tag);
318 if (err) {
319 if (err->code == GOT_ERR_PRIVSEP_PIPE)
320 err = NULL;
321 else
322 got_privsep_send_error(ibuf, err);
325 return err;
328 static const struct got_error *
329 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
331 const struct got_error *err = NULL;
332 struct imsg imsg;
333 struct got_imsg_packidx ipackidx;
334 size_t datalen;
335 struct got_packidx *p;
337 *packidx = NULL;
339 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
340 if (err)
341 return err;
343 p = calloc(1, sizeof(*p));
344 if (p == NULL) {
345 err = got_error_from_errno();
346 goto done;
349 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
350 err = got_error(GOT_ERR_PRIVSEP_MSG);
351 goto done;
354 if (imsg.fd == -1) {
355 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
356 goto done;
359 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
360 if (datalen != sizeof(ipackidx)) {
361 err = got_error(GOT_ERR_PRIVSEP_LEN);
362 goto done;
364 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
366 p->len = ipackidx.len;
367 p->fd = dup(imsg.fd);
368 if (p->fd == -1) {
369 err = got_error_from_errno();
370 goto done;
372 if (lseek(p->fd, 0, SEEK_SET) == -1) {
373 err = got_error_from_errno();
374 goto done;
377 #ifndef GOT_PACK_NO_MMAP
378 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
379 if (p->map == MAP_FAILED)
380 p->map = NULL; /* fall back to read(2) */
381 #endif
382 err = got_packidx_init_hdr(p, 1);
383 done:
384 if (err) {
385 if (imsg.fd != -1)
386 close(imsg.fd);
387 got_packidx_close(p);
388 } else
389 *packidx = p;
390 imsg_free(&imsg);
391 return err;
394 static const struct got_error *
395 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
397 const struct got_error *err = NULL;
398 struct imsg imsg;
399 struct got_imsg_pack ipack;
400 size_t datalen;
401 struct got_pack *pack;
403 *packp = NULL;
405 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
406 if (err)
407 return err;
409 pack = calloc(1, sizeof(*pack));
410 if (pack == NULL) {
411 err = got_error_from_errno();
412 goto done;
415 if (imsg.hdr.type != GOT_IMSG_PACK) {
416 err = got_error(GOT_ERR_PRIVSEP_MSG);
417 goto done;
420 if (imsg.fd == -1) {
421 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
422 goto done;
425 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
426 if (datalen != sizeof(ipack)) {
427 err = got_error(GOT_ERR_PRIVSEP_LEN);
428 goto done;
430 memcpy(&ipack, imsg.data, sizeof(ipack));
432 pack->filesize = ipack.filesize;
433 pack->fd = dup(imsg.fd);
434 if (pack->fd == -1) {
435 err = got_error_from_errno();
436 goto done;
438 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
439 err = got_error_from_errno();
440 goto done;
442 pack->path_packfile = strdup(ipack.path_packfile);
443 if (pack->path_packfile == NULL) {
444 err = got_error_from_errno();
445 goto done;
448 #ifndef GOT_PACK_NO_MMAP
449 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
450 pack->fd, 0);
451 if (pack->map == MAP_FAILED)
452 pack->map = NULL; /* fall back to read(2) */
453 #endif
454 done:
455 if (err) {
456 if (imsg.fd != -1)
457 close(imsg.fd);
458 free(pack);
459 } else
460 *packp = pack;
461 imsg_free(&imsg);
462 return err;
465 int
466 main(int argc, char *argv[])
468 const struct got_error *err = NULL;
469 struct imsgbuf ibuf;
470 struct imsg imsg;
471 struct got_packidx *packidx = NULL;
472 struct got_pack *pack = NULL;
473 struct got_object_cache objcache;
475 //static int attached;
476 //while (!attached) sleep(1);
478 signal(SIGINT, catch_sigint);
480 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
482 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
483 if (err) {
484 err = got_error_from_errno();
485 got_privsep_send_error(&ibuf, err);
486 return 1;
489 #ifndef PROFILE
490 /* revoke access to most system calls */
491 if (pledge("stdio recvfd", NULL) == -1) {
492 err = got_error_from_errno();
493 got_privsep_send_error(&ibuf, err);
494 return 1;
496 #endif
498 err = receive_packidx(&packidx, &ibuf);
499 if (err) {
500 got_privsep_send_error(&ibuf, err);
501 return 1;
504 err = receive_pack(&pack, &ibuf);
505 if (err) {
506 got_privsep_send_error(&ibuf, err);
507 return 1;
510 while (1) {
511 imsg.fd = -1;
513 if (sigint_received) {
514 err = got_error(GOT_ERR_CANCELLED);
515 break;
518 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
519 if (err) {
520 if (err->code == GOT_ERR_PRIVSEP_PIPE)
521 err = NULL;
522 break;
525 if (imsg.hdr.type == GOT_IMSG_STOP)
526 break;
528 switch (imsg.hdr.type) {
529 case GOT_IMSG_PACKED_OBJECT_REQUEST:
530 err = object_request(&imsg, &ibuf, pack, packidx,
531 &objcache);
532 break;
533 case GOT_IMSG_COMMIT_REQUEST:
534 err = commit_request(&imsg, &ibuf, pack, packidx,
535 &objcache);
536 break;
537 case GOT_IMSG_TREE_REQUEST:
538 err = tree_request(&imsg, &ibuf, pack, packidx,
539 &objcache);
540 break;
541 case GOT_IMSG_BLOB_REQUEST:
542 err = blob_request(&imsg, &ibuf, pack, packidx,
543 &objcache);
544 break;
545 case GOT_IMSG_TAG_REQUEST:
546 err = tag_request(&imsg, &ibuf, pack, packidx,
547 &objcache);
548 break;
549 default:
550 err = got_error(GOT_ERR_PRIVSEP_MSG);
551 break;
554 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
555 err = got_error_from_errno();
556 imsg_free(&imsg);
557 if (err)
558 break;
561 if (packidx)
562 got_packidx_close(packidx);
563 if (pack)
564 got_pack_close(pack);
565 got_object_cache_close(&objcache);
566 imsg_clear(&ibuf);
567 if (err) {
568 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
569 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
570 got_privsep_send_error(&ibuf, err);
573 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
574 err = got_error_from_errno();
575 return err ? 1 : 0;