Blob


1 /*
2 * Copyright (c) 2018 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 get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
88 struct got_pack *pack, struct got_packidx *packidx,
89 struct got_object_cache *objcache, int type)
90 {
91 const struct got_error *err = NULL;
92 struct got_object *iobj;
94 err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
95 if (err)
96 return err;
98 if (iobj->type != type) {
99 err = got_error(GOT_ERR_OBJ_TYPE);
100 goto done;
103 if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
104 return got_error(GOT_ERR_OBJ_NOT_PACKED);
106 *obj = got_object_cache_get(objcache, &iobj->id);
107 if (*obj == NULL) {
108 err = got_packfile_open_object(obj, pack, packidx,
109 iobj->pack_idx, &iobj->id);
110 if (err)
111 goto done;
113 (*obj)->refcnt++;
114 done:
115 got_object_close(iobj);
116 return err;
119 static const struct got_error *
120 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
121 struct got_packidx *packidx, struct got_object_cache *objcache)
123 const struct got_error *err = NULL;
124 struct got_imsg_packed_object iobj;
125 struct got_object *obj;
126 struct got_commit_object *commit = NULL;
127 uint8_t *buf;
128 size_t len;
129 struct got_object_id id;
130 size_t datalen;
132 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
133 if (datalen != sizeof(iobj))
134 return got_error(GOT_ERR_PRIVSEP_LEN);
135 memcpy(&iobj, imsg->data, sizeof(iobj));
136 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
138 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
139 if (err)
140 return err;
142 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
143 if (err)
144 return err;
146 obj->size = len;
147 err = got_object_parse_commit(&commit, buf, len);
148 free(buf);
149 if (err) {
150 got_object_close(obj);
151 return err;
154 err = got_privsep_send_commit(ibuf, commit);
155 got_object_commit_close(commit);
156 if (err) {
157 if (err->code == GOT_ERR_PRIVSEP_PIPE)
158 err = NULL;
159 else
160 got_privsep_send_error(ibuf, err);
163 return err;
166 static const struct got_error *
167 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
168 struct got_packidx *packidx, struct got_object_cache *objcache)
170 const struct got_error *err = NULL;
171 struct got_object *obj = NULL;
172 struct got_tree_object *tree = NULL;
173 uint8_t *buf;
174 size_t len;
176 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
177 GOT_OBJ_TYPE_TREE);
178 if (err)
179 return err;
181 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
182 if (err)
183 return err;
185 obj->size = len;
186 err = got_object_parse_tree(&tree, buf, len);
187 free(buf);
189 err = got_privsep_send_tree(ibuf, tree);
190 if (obj)
191 got_object_close(obj);
192 got_object_tree_close(tree);
193 if (err) {
194 if (err->code == GOT_ERR_PRIVSEP_PIPE)
195 err = NULL;
196 else
197 got_privsep_send_error(ibuf, err);
200 return err;
203 static const struct got_error *
204 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
206 const struct got_error *err;
207 struct imsg imsg;
208 size_t datalen;
210 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
211 if (err)
212 return err;
214 if (imsg.hdr.type != imsg_code) {
215 err = got_error(GOT_ERR_PRIVSEP_MSG);
216 goto done;
219 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 if (datalen != 0) {
221 err = got_error(GOT_ERR_PRIVSEP_LEN);
222 goto done;
224 if (imsg.fd == -1) {
225 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
226 goto done;
229 *f = fdopen(imsg.fd, "w+");
230 if (*f == NULL) {
231 close(imsg.fd);
232 err = got_error_from_errno();
233 goto done;
235 done:
236 imsg_free(&imsg);
237 return err;
240 static const struct got_error *
241 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
242 struct got_packidx *packidx, struct got_object_cache *objcache)
244 const struct got_error *err = NULL;
245 struct got_object *obj = NULL;
246 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
248 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
249 GOT_OBJ_TYPE_BLOB);
250 if (err)
251 return err;
253 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
254 if (err)
255 return err;
256 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
257 if (err)
258 return err;
259 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
260 if (err)
261 return err;
263 err = got_packfile_extract_object(pack, obj, outfile, basefile,
264 accumfile);
265 if (err)
266 goto done;
268 err = got_privsep_send_blob(ibuf, obj->size);
269 done:
270 if (outfile)
271 fclose(outfile);
272 if (basefile)
273 fclose(basefile);
274 if (accumfile)
275 fclose(accumfile);
276 if (obj)
277 got_object_close(obj);
278 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
279 got_privsep_send_error(ibuf, err);
281 return err;
284 static const struct got_error *
285 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
286 struct got_packidx *packidx, struct got_object_cache *objcache)
288 const struct got_error *err = NULL;
289 struct got_object *obj = NULL;
290 struct got_tag_object *tag = NULL;
291 uint8_t *buf;
292 size_t len;
294 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
295 GOT_OBJ_TYPE_TAG);
296 if (err)
297 return err;
299 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
300 if (err)
301 return err;
303 obj->size = len;
304 err = got_object_parse_tag(&tag, buf, len);
305 free(buf);
307 err = got_privsep_send_tag(ibuf, tag);
308 if (obj)
309 got_object_close(obj);
310 got_object_tag_close(tag);
311 if (err) {
312 if (err->code == GOT_ERR_PRIVSEP_PIPE)
313 err = NULL;
314 else
315 got_privsep_send_error(ibuf, err);
318 return err;
321 static const struct got_error *
322 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
324 const struct got_error *err = NULL;
325 struct imsg imsg;
326 struct got_imsg_packidx ipackidx;
327 size_t datalen;
328 struct got_packidx *p;
330 *packidx = NULL;
332 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
333 if (err)
334 return err;
336 p = calloc(1, sizeof(*p));
337 if (p == NULL) {
338 err = got_error_from_errno();
339 goto done;
342 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
343 err = got_error(GOT_ERR_PRIVSEP_MSG);
344 goto done;
347 if (imsg.fd == -1) {
348 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
349 goto done;
352 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
353 if (datalen != sizeof(ipackidx)) {
354 err = got_error(GOT_ERR_PRIVSEP_LEN);
355 goto done;
357 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
359 p->len = ipackidx.len;
360 p->fd = dup(imsg.fd);
361 if (p->fd == -1) {
362 err = got_error_from_errno();
363 goto done;
365 if (lseek(p->fd, 0, SEEK_SET) == -1) {
366 err = got_error_from_errno();
367 goto done;
370 #ifndef GOT_PACK_NO_MMAP
371 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
372 if (p->map == MAP_FAILED)
373 p->map = NULL; /* fall back to read(2) */
374 #endif
375 err = got_packidx_init_hdr(p, 1);
376 done:
377 if (err) {
378 if (imsg.fd != -1)
379 close(imsg.fd);
380 got_packidx_close(p);
381 } else
382 *packidx = p;
383 imsg_free(&imsg);
384 return err;
387 static const struct got_error *
388 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
390 const struct got_error *err = NULL;
391 struct imsg imsg;
392 struct got_imsg_pack ipack;
393 size_t datalen;
394 struct got_pack *pack;
396 *packp = NULL;
398 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
399 if (err)
400 return err;
402 pack = calloc(1, sizeof(*pack));
403 if (pack == NULL) {
404 err = got_error_from_errno();
405 goto done;
408 if (imsg.hdr.type != GOT_IMSG_PACK) {
409 err = got_error(GOT_ERR_PRIVSEP_MSG);
410 goto done;
413 if (imsg.fd == -1) {
414 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
415 goto done;
418 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
419 if (datalen != sizeof(ipack)) {
420 err = got_error(GOT_ERR_PRIVSEP_LEN);
421 goto done;
423 memcpy(&ipack, imsg.data, sizeof(ipack));
425 pack->filesize = ipack.filesize;
426 pack->fd = dup(imsg.fd);
427 if (pack->fd == -1) {
428 err = got_error_from_errno();
429 goto done;
431 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
432 err = got_error_from_errno();
433 goto done;
435 pack->path_packfile = strdup(ipack.path_packfile);
436 if (pack->path_packfile == NULL) {
437 err = got_error_from_errno();
438 goto done;
441 #ifndef GOT_PACK_NO_MMAP
442 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
443 pack->fd, 0);
444 if (pack->map == MAP_FAILED)
445 pack->map = NULL; /* fall back to read(2) */
446 #endif
447 done:
448 if (err) {
449 if (imsg.fd != -1)
450 close(imsg.fd);
451 free(pack);
452 } else
453 *packp = pack;
454 imsg_free(&imsg);
455 return err;
458 int
459 main(int argc, char *argv[])
461 const struct got_error *err = NULL;
462 struct imsgbuf ibuf;
463 struct imsg imsg;
464 struct got_packidx *packidx = NULL;
465 struct got_pack *pack = NULL;
466 struct got_object_cache objcache;
468 //static int attached;
469 //while (!attached) sleep(1);
471 signal(SIGINT, catch_sigint);
473 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
475 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
476 if (err) {
477 err = got_error_from_errno();
478 got_privsep_send_error(&ibuf, err);
479 return 1;
482 #ifndef PROFILE
483 /* revoke access to most system calls */
484 if (pledge("stdio recvfd", NULL) == -1) {
485 err = got_error_from_errno();
486 got_privsep_send_error(&ibuf, err);
487 return 1;
489 #endif
491 err = receive_packidx(&packidx, &ibuf);
492 if (err) {
493 got_privsep_send_error(&ibuf, err);
494 return 1;
497 err = receive_pack(&pack, &ibuf);
498 if (err) {
499 got_privsep_send_error(&ibuf, err);
500 return 1;
503 while (1) {
504 imsg.fd = -1;
506 if (sigint_received) {
507 err = got_error(GOT_ERR_CANCELLED);
508 break;
511 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
512 if (err) {
513 if (err->code == GOT_ERR_PRIVSEP_PIPE)
514 err = NULL;
515 break;
518 if (imsg.hdr.type == GOT_IMSG_STOP)
519 break;
521 switch (imsg.hdr.type) {
522 case GOT_IMSG_PACKED_OBJECT_REQUEST:
523 err = object_request(&imsg, &ibuf, pack, packidx,
524 &objcache);
525 break;
526 case GOT_IMSG_COMMIT_REQUEST:
527 err = commit_request(&imsg, &ibuf, pack, packidx,
528 &objcache);
529 break;
530 case GOT_IMSG_TREE_REQUEST:
531 err = tree_request(&imsg, &ibuf, pack, packidx,
532 &objcache);
533 break;
534 case GOT_IMSG_BLOB_REQUEST:
535 err = blob_request(&imsg, &ibuf, pack, packidx,
536 &objcache);
537 break;
538 case GOT_IMSG_TAG_REQUEST:
539 err = tag_request(&imsg, &ibuf, pack, packidx,
540 &objcache);
541 break;
542 default:
543 err = got_error(GOT_ERR_PRIVSEP_MSG);
544 break;
547 if (imsg.fd != -1)
548 close(imsg.fd);
549 imsg_free(&imsg);
550 if (err)
551 break;
554 if (packidx)
555 got_packidx_close(packidx);
556 if (pack)
557 got_pack_close(pack);
558 got_object_cache_close(&objcache);
559 imsg_clear(&ibuf);
560 if (err) {
561 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
562 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
563 got_privsep_send_error(&ibuf, err);
566 close(GOT_IMSG_FD_CHILD);
567 return err ? 1 : 0;