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_object *obj = NULL;
125 struct got_commit_object *commit = NULL;
126 uint8_t *buf;
127 size_t len;
129 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
130 GOT_OBJ_TYPE_COMMIT);
131 if (err)
132 return err;
134 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
135 if (err)
136 return err;
138 obj->size = len;
139 err = got_object_parse_commit(&commit, buf, len);
140 free(buf);
142 err = got_privsep_send_commit(ibuf, commit);
143 if (obj)
144 got_object_close(obj);
145 got_object_commit_close(commit);
146 if (err) {
147 if (err->code == GOT_ERR_PRIVSEP_PIPE)
148 err = NULL;
149 else
150 got_privsep_send_error(ibuf, err);
153 return err;
156 static const struct got_error *
157 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
158 struct got_packidx *packidx, struct got_object_cache *objcache)
160 const struct got_error *err = NULL;
161 struct got_object *obj = NULL;
162 struct got_tree_object *tree = NULL;
163 uint8_t *buf;
164 size_t len;
166 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
167 GOT_OBJ_TYPE_TREE);
168 if (err)
169 return err;
171 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
172 if (err)
173 return err;
175 obj->size = len;
176 err = got_object_parse_tree(&tree, buf, len);
177 free(buf);
179 err = got_privsep_send_tree(ibuf, tree);
180 if (obj)
181 got_object_close(obj);
182 got_object_tree_close(tree);
183 if (err) {
184 if (err->code == GOT_ERR_PRIVSEP_PIPE)
185 err = NULL;
186 else
187 got_privsep_send_error(ibuf, err);
190 return err;
193 static const struct got_error *
194 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
196 const struct got_error *err;
197 struct imsg imsg;
198 size_t datalen;
200 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
201 if (err)
202 return err;
204 if (imsg.hdr.type != imsg_code) {
205 err = got_error(GOT_ERR_PRIVSEP_MSG);
206 goto done;
209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
210 if (datalen != 0) {
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
212 goto done;
214 if (imsg.fd == -1) {
215 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
216 goto done;
219 *f = fdopen(imsg.fd, "w+");
220 if (*f == NULL) {
221 close(imsg.fd);
222 err = got_error_from_errno();
223 goto done;
225 done:
226 imsg_free(&imsg);
227 return err;
230 static const struct got_error *
231 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
232 struct got_packidx *packidx, struct got_object_cache *objcache)
234 const struct got_error *err = NULL;
235 struct got_object *obj = NULL;
236 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
238 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
239 GOT_OBJ_TYPE_BLOB);
240 if (err)
241 return err;
243 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
244 if (err)
245 return err;
246 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
247 if (err)
248 return err;
249 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
250 if (err)
251 return err;
253 err = got_packfile_extract_object(pack, obj, outfile, basefile,
254 accumfile);
255 if (err)
256 goto done;
258 err = got_privsep_send_blob(ibuf, obj->size);
259 done:
260 if (outfile)
261 fclose(outfile);
262 if (basefile)
263 fclose(basefile);
264 if (accumfile)
265 fclose(accumfile);
266 if (obj)
267 got_object_close(obj);
268 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
269 got_privsep_send_error(ibuf, err);
271 return err;
274 static const struct got_error *
275 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
277 const struct got_error *err = NULL;
278 struct imsg imsg;
279 struct got_imsg_packidx ipackidx;
280 size_t datalen;
281 struct got_packidx *p;
283 *packidx = NULL;
285 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
286 if (err)
287 return err;
289 p = calloc(1, sizeof(*p));
290 if (p == NULL) {
291 err = got_error_from_errno();
292 goto done;
295 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
296 err = got_error(GOT_ERR_PRIVSEP_MSG);
297 goto done;
300 if (imsg.fd == -1) {
301 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
302 goto done;
305 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
306 if (datalen != sizeof(ipackidx)) {
307 err = got_error(GOT_ERR_PRIVSEP_LEN);
308 goto done;
310 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
312 p->len = ipackidx.len;
313 p->fd = dup(imsg.fd);
314 if (p->fd == -1) {
315 err = got_error_from_errno();
316 goto done;
318 if (lseek(p->fd, 0, SEEK_SET) == -1) {
319 err = got_error_from_errno();
320 goto done;
323 #ifndef GOT_PACK_NO_MMAP
324 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
325 if (p->map == MAP_FAILED)
326 p->map = NULL; /* fall back to read(2) */
327 #endif
328 err = got_packidx_init_hdr(p, 1);
329 done:
330 if (err) {
331 if (imsg.fd != -1)
332 close(imsg.fd);
333 got_packidx_close(p);
334 } else
335 *packidx = p;
336 imsg_free(&imsg);
337 return err;
340 static const struct got_error *
341 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
343 const struct got_error *err = NULL;
344 struct imsg imsg;
345 struct got_imsg_pack ipack;
346 size_t datalen;
347 struct got_pack *pack;
349 *packp = NULL;
351 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
352 if (err)
353 return err;
355 pack = calloc(1, sizeof(*pack));
356 if (pack == NULL) {
357 err = got_error_from_errno();
358 goto done;
361 if (imsg.hdr.type != GOT_IMSG_PACK) {
362 err = got_error(GOT_ERR_PRIVSEP_MSG);
363 goto done;
366 if (imsg.fd == -1) {
367 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
368 goto done;
371 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
372 if (datalen != sizeof(ipack)) {
373 err = got_error(GOT_ERR_PRIVSEP_LEN);
374 goto done;
376 memcpy(&ipack, imsg.data, sizeof(ipack));
378 pack->filesize = ipack.filesize;
379 pack->fd = dup(imsg.fd);
380 if (pack->fd == -1) {
381 err = got_error_from_errno();
382 goto done;
384 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
385 err = got_error_from_errno();
386 goto done;
388 pack->path_packfile = strdup(ipack.path_packfile);
389 if (pack->path_packfile == NULL) {
390 err = got_error_from_errno();
391 goto done;
394 #ifndef GOT_PACK_NO_MMAP
395 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
396 pack->fd, 0);
397 if (pack->map == MAP_FAILED)
398 pack->map = NULL; /* fall back to read(2) */
399 #endif
400 done:
401 if (err) {
402 if (imsg.fd != -1)
403 close(imsg.fd);
404 free(pack);
405 } else
406 *packp = pack;
407 imsg_free(&imsg);
408 return err;
411 int
412 main(int argc, char *argv[])
414 const struct got_error *err = NULL;
415 struct imsgbuf ibuf;
416 struct imsg imsg;
417 struct got_packidx *packidx = NULL;
418 struct got_pack *pack = NULL;
419 struct got_object_cache objcache;
421 //static int attached;
422 //while (!attached) sleep(1);
424 signal(SIGINT, catch_sigint);
426 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
428 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
429 if (err) {
430 err = got_error_from_errno();
431 got_privsep_send_error(&ibuf, err);
432 return 1;
435 #ifndef PROFILE
436 /* revoke access to most system calls */
437 if (pledge("stdio recvfd", NULL) == -1) {
438 err = got_error_from_errno();
439 got_privsep_send_error(&ibuf, err);
440 return 1;
442 #endif
444 err = receive_packidx(&packidx, &ibuf);
445 if (err) {
446 got_privsep_send_error(&ibuf, err);
447 return 1;
450 err = receive_pack(&pack, &ibuf);
451 if (err) {
452 got_privsep_send_error(&ibuf, err);
453 return 1;
456 while (1) {
457 imsg.fd = -1;
459 if (sigint_received) {
460 err = got_error(GOT_ERR_CANCELLED);
461 break;
464 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
465 if (err) {
466 if (err->code == GOT_ERR_PRIVSEP_PIPE)
467 err = NULL;
468 break;
471 if (imsg.hdr.type == GOT_IMSG_STOP)
472 break;
474 switch (imsg.hdr.type) {
475 case GOT_IMSG_PACKED_OBJECT_REQUEST:
476 err = object_request(&imsg, &ibuf, pack, packidx,
477 &objcache);
478 break;
479 case GOT_IMSG_COMMIT_REQUEST:
480 err = commit_request(&imsg, &ibuf, pack, packidx,
481 &objcache);
482 break;
483 case GOT_IMSG_TREE_REQUEST:
484 err = tree_request(&imsg, &ibuf, pack, packidx,
485 &objcache);
486 break;
487 case GOT_IMSG_BLOB_REQUEST:
488 err = blob_request(&imsg, &ibuf, pack, packidx,
489 &objcache);
490 break;
491 default:
492 err = got_error(GOT_ERR_PRIVSEP_MSG);
493 break;
496 if (imsg.fd != -1)
497 close(imsg.fd);
498 imsg_free(&imsg);
499 if (err)
500 break;
503 if (packidx)
504 got_packidx_close(packidx);
505 if (pack)
506 got_pack_close(pack);
507 got_object_cache_close(&objcache);
508 imsg_clear(&ibuf);
509 if (err) {
510 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
511 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE)
512 got_privsep_send_error(&ibuf, err);
514 close(GOT_IMSG_FD_CHILD);
515 return err ? 1 : 0;