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 <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_inflate.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 const struct got_error *
46 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
47 struct got_packidx *packidx, struct got_object_cache *objcache)
48 {
49 const struct got_error *err = NULL;
50 struct got_imsg_packed_object iobj;
51 struct got_object *obj;
52 size_t datalen;
54 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
55 if (datalen != sizeof(iobj))
56 return got_error(GOT_ERR_PRIVSEP_LEN);
57 memcpy(&iobj, imsg->data, sizeof(iobj));
59 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
60 if (err)
61 return err;
62 obj->refcnt++;
64 err = got_object_cache_add(objcache, &obj->id, obj);
65 if (err)
66 goto done;
67 obj->refcnt++;
69 err = got_privsep_send_obj(ibuf, obj);
70 done:
71 got_object_close(obj);
72 return err;
73 }
75 static const struct got_error *
76 get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
77 struct got_pack *pack, struct got_packidx *packidx,
78 struct got_object_cache *objcache, int type)
79 {
80 const struct got_error *err = NULL;
81 struct got_object *iobj;
83 err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
84 if (err)
85 return err;
87 if (iobj->type != type) {
88 err = got_error(GOT_ERR_OBJ_TYPE);
89 goto done;
90 }
92 if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
93 return got_error(GOT_ERR_OBJ_NOT_PACKED);
95 *obj = got_object_cache_get(objcache, &iobj->id);
96 if (*obj == NULL) {
97 err = got_packfile_open_object(obj, pack, packidx,
98 iobj->pack_idx, &iobj->id);
99 if (err)
100 goto done;
102 (*obj)->refcnt++;
103 done:
104 got_object_close(iobj);
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_object *obj = NULL;
114 struct got_commit_object *commit = NULL;
115 uint8_t *buf;
116 size_t len;
118 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
119 GOT_OBJ_TYPE_COMMIT);
120 if (err)
121 return err;
123 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
124 if (err)
125 return err;
127 obj->size = len;
128 err = got_object_parse_commit(&commit, buf, len);
129 free(buf);
131 err = got_privsep_send_commit(ibuf, commit);
132 if (obj)
133 got_object_close(obj);
134 got_object_commit_close(commit);
135 if (err) {
136 if (err->code == GOT_ERR_PRIVSEP_PIPE)
137 err = NULL;
138 else
139 got_privsep_send_error(ibuf, err);
142 return err;
145 static const struct got_error *
146 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 struct got_packidx *packidx, struct got_object_cache *objcache)
149 const struct got_error *err = NULL;
150 struct got_object *obj = NULL;
151 struct got_tree_object *tree = NULL;
152 uint8_t *buf;
153 size_t len;
155 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
156 GOT_OBJ_TYPE_TREE);
157 if (err)
158 return err;
160 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
161 if (err)
162 return err;
164 obj->size = len;
165 err = got_object_parse_tree(&tree, buf, len);
166 free(buf);
168 err = got_privsep_send_tree(ibuf, tree);
169 if (obj)
170 got_object_close(obj);
171 got_object_tree_close(tree);
172 if (err) {
173 if (err->code == GOT_ERR_PRIVSEP_PIPE)
174 err = NULL;
175 else
176 got_privsep_send_error(ibuf, err);
179 return err;
182 static const struct got_error *
183 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
185 const struct got_error *err;
186 struct imsg imsg;
187 size_t datalen;
189 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
190 if (err)
191 return err;
193 if (imsg.hdr.type != imsg_code) {
194 err = got_error(GOT_ERR_PRIVSEP_MSG);
195 goto done;
198 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
199 if (datalen != 0) {
200 err = got_error(GOT_ERR_PRIVSEP_LEN);
201 goto done;
203 if (imsg.fd == -1) {
204 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
205 goto done;
208 *f = fdopen(imsg.fd, "w+");
209 if (*f == NULL) {
210 close(imsg.fd);
211 err = got_error_from_errno();
212 goto done;
214 done:
215 imsg_free(&imsg);
216 return err;
219 static const struct got_error *
220 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
221 struct got_packidx *packidx, struct got_object_cache *objcache)
223 const struct got_error *err = NULL;
224 struct got_object *obj = NULL;
225 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
227 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
228 GOT_OBJ_TYPE_BLOB);
229 if (err)
230 return err;
232 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
233 if (err)
234 return err;
235 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
236 if (err)
237 return err;
238 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
239 if (err)
240 return err;
242 err = got_packfile_extract_object(pack, obj, outfile, basefile,
243 accumfile);
244 if (err)
245 goto done;
247 err = got_privsep_send_blob(ibuf, obj->size);
248 done:
249 if (outfile)
250 fclose(outfile);
251 if (basefile)
252 fclose(basefile);
253 if (accumfile)
254 fclose(accumfile);
255 if (obj)
256 got_object_close(obj);
257 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
258 got_privsep_send_error(ibuf, err);
260 return err;
263 static const struct got_error *
264 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
266 const struct got_error *err = NULL;
267 struct imsg imsg;
268 struct got_imsg_packidx ipackidx;
269 size_t datalen;
270 struct got_packidx *p;
272 *packidx = NULL;
274 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
275 if (err)
276 return err;
278 p = calloc(1, sizeof(*p));
279 if (p == NULL) {
280 err = got_error_from_errno();
281 goto done;
284 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
286 goto done;
289 if (imsg.fd == -1) {
290 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
291 goto done;
294 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
295 if (datalen != sizeof(ipackidx)) {
296 err = got_error(GOT_ERR_PRIVSEP_LEN);
297 goto done;
299 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
301 p->len = ipackidx.len;
302 p->fd = dup(imsg.fd);
303 if (p->fd == -1) {
304 err = got_error_from_errno();
305 goto done;
308 #ifndef GOT_PACK_NO_MMAP
309 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
310 if (p->map == MAP_FAILED)
311 p->map = NULL; /* fall back to read(2) */
312 #endif
313 err = got_packidx_init_hdr(p, 1);
314 done:
315 if (err) {
316 if (imsg.fd != -1)
317 close(imsg.fd);
318 got_packidx_close(p);
319 } else
320 *packidx = p;
321 imsg_free(&imsg);
322 return err;
325 static const struct got_error *
326 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
328 const struct got_error *err = NULL;
329 struct imsg imsg;
330 struct got_imsg_pack ipack;
331 size_t datalen;
332 struct got_pack *pack;
334 *packp = NULL;
336 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
337 if (err)
338 return err;
340 pack = calloc(1, sizeof(*pack));
341 if (pack == NULL) {
342 err = got_error_from_errno();
343 goto done;
346 if (imsg.hdr.type != GOT_IMSG_PACK) {
347 err = got_error(GOT_ERR_PRIVSEP_MSG);
348 goto done;
351 if (imsg.fd == -1) {
352 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
353 goto done;
356 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
357 if (datalen != sizeof(ipack)) {
358 err = got_error(GOT_ERR_PRIVSEP_LEN);
359 goto done;
361 memcpy(&ipack, imsg.data, sizeof(ipack));
363 pack->filesize = ipack.filesize;
364 pack->fd = dup(imsg.fd);
365 if (pack->fd == -1) {
366 err = got_error_from_errno();
367 goto done;
369 pack->path_packfile = strdup(ipack.path_packfile);
370 if (pack->path_packfile == NULL) {
371 err = got_error_from_errno();
372 goto done;
375 #ifndef GOT_PACK_NO_MMAP
376 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
377 pack->fd, 0);
378 if (pack->map == MAP_FAILED)
379 pack->map = NULL; /* fall back to read(2) */
380 #endif
381 done:
382 if (err) {
383 if (imsg.fd != -1)
384 close(imsg.fd);
385 free(pack);
386 } else
387 *packp = pack;
388 imsg_free(&imsg);
389 return err;
392 int
393 main(int argc, char *argv[])
395 const struct got_error *err = NULL;
396 struct imsgbuf ibuf;
397 struct imsg imsg;
398 struct got_packidx *packidx = NULL;
399 struct got_pack *pack = NULL;
400 struct got_object_cache objcache;
402 //static int attached;
403 //while (!attached) sleep(1);
405 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
407 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
408 if (err) {
409 err = got_error_from_errno();
410 got_privsep_send_error(&ibuf, err);
411 return 1;
414 #ifndef PROFILE
415 /* revoke access to most system calls */
416 if (pledge("stdio recvfd", NULL) == -1) {
417 err = got_error_from_errno();
418 got_privsep_send_error(&ibuf, err);
419 return 1;
421 #endif
423 err = receive_packidx(&packidx, &ibuf);
424 if (err) {
425 got_privsep_send_error(&ibuf, err);
426 return 1;
429 err = receive_pack(&pack, &ibuf);
430 if (err) {
431 got_privsep_send_error(&ibuf, err);
432 return 1;
435 while (1) {
436 imsg.fd = -1;
438 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
439 if (err) {
440 if (err->code == GOT_ERR_PRIVSEP_PIPE)
441 err = NULL;
442 break;
445 if (imsg.hdr.type == GOT_IMSG_STOP)
446 break;
448 switch (imsg.hdr.type) {
449 case GOT_IMSG_PACKED_OBJECT_REQUEST:
450 err = object_request(&imsg, &ibuf, pack, packidx,
451 &objcache);
452 break;
453 case GOT_IMSG_COMMIT_REQUEST:
454 err = commit_request(&imsg, &ibuf, pack, packidx,
455 &objcache);
456 break;
457 case GOT_IMSG_TREE_REQUEST:
458 err = tree_request(&imsg, &ibuf, pack, packidx,
459 &objcache);
460 break;
461 case GOT_IMSG_BLOB_REQUEST:
462 err = blob_request(&imsg, &ibuf, pack, packidx,
463 &objcache);
464 break;
465 default:
466 err = got_error(GOT_ERR_PRIVSEP_MSG);
467 break;
470 if (imsg.fd != -1)
471 close(imsg.fd);
472 imsg_free(&imsg);
473 if (err) {
474 if (err->code == GOT_ERR_PRIVSEP_PIPE)
475 err = NULL;
476 else
477 got_privsep_send_error(&ibuf, err);
478 break;
482 if (packidx)
483 got_packidx_close(packidx);
484 if (pack)
485 got_pack_close(pack);
486 imsg_clear(&ibuf);
487 if (err)
488 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
489 close(GOT_IMSG_FD_CHILD);
490 return err ? 1 : 0;