2 876c234b 2018-09-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/queue.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/limits.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 876c234b 2018-09-10 stsp #include <sys/mman.h>
25 876c234b 2018-09-10 stsp #include <limits.h>
26 876c234b 2018-09-10 stsp #include <stdint.h>
27 876c234b 2018-09-10 stsp #include <imsg.h>
28 876c234b 2018-09-10 stsp #include <stdio.h>
29 876c234b 2018-09-10 stsp #include <stdlib.h>
30 876c234b 2018-09-10 stsp #include <string.h>
31 876c234b 2018-09-10 stsp #include <sha1.h>
32 876c234b 2018-09-10 stsp #include <zlib.h>
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
37 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
38 876c234b 2018-09-10 stsp #include "got_lib_inflate.h"
39 876c234b 2018-09-10 stsp #include "got_lib_object.h"
40 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
41 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
42 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
45 876c234b 2018-09-10 stsp static const struct got_error *
46 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
47 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
49 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
50 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
51 876c234b 2018-09-10 stsp struct got_object *obj;
52 876c234b 2018-09-10 stsp size_t datalen;
54 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
55 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
56 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
57 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
59 876c234b 2018-09-10 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
62 c59b3346 2018-09-11 stsp obj->refcnt++;
64 c59b3346 2018-09-11 stsp err = got_object_cache_add(objcache, &obj->id, obj);
67 c59b3346 2018-09-11 stsp obj->refcnt++;
69 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
71 876c234b 2018-09-10 stsp got_object_close(obj);
75 876c234b 2018-09-10 stsp static const struct got_error *
76 c59b3346 2018-09-11 stsp get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
77 c59b3346 2018-09-11 stsp struct got_pack *pack, struct got_packidx *packidx,
78 c59b3346 2018-09-11 stsp struct got_object_cache *objcache, int type)
80 c59b3346 2018-09-11 stsp const struct got_error *err = NULL;
81 c59b3346 2018-09-11 stsp struct got_object *iobj;
83 c59b3346 2018-09-11 stsp err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
87 c59b3346 2018-09-11 stsp if (iobj->type != type) {
88 c59b3346 2018-09-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
92 c59b3346 2018-09-11 stsp if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
93 c59b3346 2018-09-11 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
95 c59b3346 2018-09-11 stsp *obj = got_object_cache_get(objcache, &iobj->id);
96 c59b3346 2018-09-11 stsp if (*obj == NULL) {
97 c59b3346 2018-09-11 stsp err = got_packfile_open_object(obj, pack, packidx,
98 c59b3346 2018-09-11 stsp iobj->pack_idx, &iobj->id);
102 c59b3346 2018-09-11 stsp (*obj)->refcnt++;
104 c59b3346 2018-09-11 stsp got_object_close(iobj);
105 c59b3346 2018-09-11 stsp return err;
108 c59b3346 2018-09-11 stsp static const struct got_error *
109 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
110 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
112 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
113 cfd633c2 2018-09-10 stsp struct got_object *obj = NULL;
114 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
115 cfd633c2 2018-09-10 stsp uint8_t *buf;
116 cfd633c2 2018-09-10 stsp size_t len;
118 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
119 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_COMMIT);
121 cfd633c2 2018-09-10 stsp return err;
123 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
125 cfd633c2 2018-09-10 stsp return err;
127 cfd633c2 2018-09-10 stsp obj->size = len;
128 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
131 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
133 cfd633c2 2018-09-10 stsp got_object_close(obj);
134 cfd633c2 2018-09-10 stsp got_object_commit_close(commit);
136 cfd633c2 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
137 cfd633c2 2018-09-10 stsp err = NULL;
139 cfd633c2 2018-09-10 stsp got_privsep_send_error(ibuf, err);
142 cfd633c2 2018-09-10 stsp return err;
145 876c234b 2018-09-10 stsp static const struct got_error *
146 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
149 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
150 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
151 e7885405 2018-09-10 stsp struct got_tree_object *tree = NULL;
152 e7885405 2018-09-10 stsp uint8_t *buf;
153 e7885405 2018-09-10 stsp size_t len;
155 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
156 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_TREE);
158 e7885405 2018-09-10 stsp return err;
160 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
162 e7885405 2018-09-10 stsp return err;
164 e7885405 2018-09-10 stsp obj->size = len;
165 e7885405 2018-09-10 stsp err = got_object_parse_tree(&tree, buf, len);
168 e7885405 2018-09-10 stsp err = got_privsep_send_tree(ibuf, tree);
170 e7885405 2018-09-10 stsp got_object_close(obj);
171 e7885405 2018-09-10 stsp got_object_tree_close(tree);
173 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
174 e7885405 2018-09-10 stsp err = NULL;
176 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
179 e7885405 2018-09-10 stsp return err;
182 876c234b 2018-09-10 stsp static const struct got_error *
183 876c234b 2018-09-10 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
184 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
186 55da3778 2018-09-10 stsp const struct got_error *err = NULL;
187 55da3778 2018-09-10 stsp struct got_object *obj = NULL;
188 55da3778 2018-09-10 stsp FILE *f = NULL;
189 55da3778 2018-09-10 stsp struct imsg imsg_outfd;
190 55da3778 2018-09-10 stsp size_t datalen;
192 55da3778 2018-09-10 stsp memset(&imsg_outfd, 0, sizeof(imsg_outfd));
193 55da3778 2018-09-10 stsp imsg_outfd.fd = -1;
195 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
196 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_BLOB);
198 55da3778 2018-09-10 stsp return err;
200 55da3778 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg_outfd, ibuf, 0);
202 55da3778 2018-09-10 stsp return err;
204 55da3778 2018-09-10 stsp if (imsg_outfd.hdr.type != GOT_IMSG_BLOB_OUTFD) {
205 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
209 55da3778 2018-09-10 stsp datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
210 55da3778 2018-09-10 stsp if (datalen != 0) {
211 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
214 55da3778 2018-09-10 stsp if (imsg_outfd.fd == -1) {
215 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
219 55da3778 2018-09-10 stsp f = fdopen(imsg_outfd.fd, "w+");
220 55da3778 2018-09-10 stsp if (f == NULL) {
221 55da3778 2018-09-10 stsp err = got_error_from_errno();
225 55da3778 2018-09-10 stsp err = got_packfile_extract_object(pack, obj, f);
229 55da3778 2018-09-10 stsp err = got_privsep_send_blob(ibuf, obj->size);
233 55da3778 2018-09-10 stsp else if (imsg_outfd.fd != -1)
234 55da3778 2018-09-10 stsp close(imsg_outfd.fd);
235 55da3778 2018-09-10 stsp imsg_free(&imsg_outfd);
237 c59b3346 2018-09-11 stsp got_object_close(obj);
239 55da3778 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
240 55da3778 2018-09-10 stsp err = NULL;
242 55da3778 2018-09-10 stsp got_privsep_send_error(ibuf, err);
245 55da3778 2018-09-10 stsp return err;
248 876c234b 2018-09-10 stsp static const struct got_error *
249 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
251 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
252 876c234b 2018-09-10 stsp struct imsg imsg;
253 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
254 876c234b 2018-09-10 stsp size_t datalen;
255 876c234b 2018-09-10 stsp struct got_packidx *p;
257 876c234b 2018-09-10 stsp *packidx = NULL;
259 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
261 876c234b 2018-09-10 stsp return err;
263 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
264 876c234b 2018-09-10 stsp if (p == NULL) {
265 876c234b 2018-09-10 stsp err = got_error_from_errno();
269 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
270 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
274 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
275 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
279 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
280 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
281 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
284 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
286 876c234b 2018-09-10 stsp p->len = ipackidx.len;
287 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
288 876c234b 2018-09-10 stsp if (p->fd == -1) {
289 876c234b 2018-09-10 stsp err = got_error_from_errno();
293 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
294 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
295 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
296 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
298 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
301 876c234b 2018-09-10 stsp if (imsg.fd != -1)
302 876c234b 2018-09-10 stsp close(imsg.fd);
303 876c234b 2018-09-10 stsp got_packidx_close(p);
305 876c234b 2018-09-10 stsp *packidx = p;
306 876c234b 2018-09-10 stsp imsg_free(&imsg);
307 876c234b 2018-09-10 stsp return err;
310 876c234b 2018-09-10 stsp static const struct got_error *
311 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
313 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
314 876c234b 2018-09-10 stsp struct imsg imsg;
315 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
316 876c234b 2018-09-10 stsp size_t datalen;
317 876c234b 2018-09-10 stsp struct got_pack *pack;
319 876c234b 2018-09-10 stsp *packp = NULL;
321 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
323 876c234b 2018-09-10 stsp return err;
325 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
326 876c234b 2018-09-10 stsp if (pack == NULL) {
327 876c234b 2018-09-10 stsp err = got_error_from_errno();
331 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
332 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
336 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
337 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
341 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
343 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
346 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
348 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
349 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
350 876c234b 2018-09-10 stsp if (pack->fd == -1) {
351 876c234b 2018-09-10 stsp err = got_error_from_errno();
354 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
355 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
356 876c234b 2018-09-10 stsp err = got_error_from_errno();
360 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
361 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
362 876c234b 2018-09-10 stsp pack->fd, 0);
363 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
364 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
368 876c234b 2018-09-10 stsp if (imsg.fd != -1)
369 876c234b 2018-09-10 stsp close(imsg.fd);
370 876c234b 2018-09-10 stsp free(pack);
372 876c234b 2018-09-10 stsp *packp = pack;
373 876c234b 2018-09-10 stsp imsg_free(&imsg);
374 876c234b 2018-09-10 stsp return err;
378 876c234b 2018-09-10 stsp main(int argc, char *argv[])
380 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
381 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
382 876c234b 2018-09-10 stsp struct imsg imsg;
383 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
384 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
385 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
387 876c234b 2018-09-10 stsp //static int attached;
388 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
390 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
392 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
394 c59b3346 2018-09-11 stsp err = got_error_from_errno();
395 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
399 876c234b 2018-09-10 stsp /* revoke access to most system calls */
400 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
401 876c234b 2018-09-10 stsp err = got_error_from_errno();
402 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
406 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
408 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
412 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
414 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
418 876c234b 2018-09-10 stsp while (1) {
419 876c234b 2018-09-10 stsp imsg.fd = -1;
421 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
423 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
424 876c234b 2018-09-10 stsp err = NULL;
428 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
431 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
432 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
433 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
434 c59b3346 2018-09-11 stsp &objcache);
436 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
437 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
438 c59b3346 2018-09-11 stsp &objcache);
440 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
441 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
442 c59b3346 2018-09-11 stsp &objcache);
444 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
445 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
446 c59b3346 2018-09-11 stsp &objcache);
449 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
453 876c234b 2018-09-10 stsp if (imsg.fd != -1)
454 876c234b 2018-09-10 stsp close(imsg.fd);
455 876c234b 2018-09-10 stsp imsg_free(&imsg);
457 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
458 876c234b 2018-09-10 stsp err = NULL;
460 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
465 c59b3346 2018-09-11 stsp if (packidx)
466 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
468 c59b3346 2018-09-11 stsp got_pack_close(pack);
469 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
471 876c234b 2018-09-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
472 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
473 876c234b 2018-09-10 stsp return err ? 1 : 0;