Blame


1 876c234b 2018-09-10 stsp /*
2 876c234b 2018-09-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 876c234b 2018-09-10 stsp *
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.
7 876c234b 2018-09-10 stsp *
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.
15 876c234b 2018-09-10 stsp */
16 876c234b 2018-09-10 stsp
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>
24 876c234b 2018-09-10 stsp
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>
33 876c234b 2018-09-10 stsp
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
36 876c234b 2018-09-10 stsp
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"
44 876c234b 2018-09-10 stsp
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)
48 876c234b 2018-09-10 stsp {
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 106807b4 2018-09-15 stsp struct got_object_id id;
53 876c234b 2018-09-10 stsp size_t datalen;
54 876c234b 2018-09-10 stsp
55 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
56 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
57 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
58 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
59 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
60 876c234b 2018-09-10 stsp
61 106807b4 2018-09-15 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
62 876c234b 2018-09-10 stsp if (err)
63 876c234b 2018-09-10 stsp return err;
64 c59b3346 2018-09-11 stsp obj->refcnt++;
65 876c234b 2018-09-10 stsp
66 c59b3346 2018-09-11 stsp err = got_object_cache_add(objcache, &obj->id, obj);
67 c59b3346 2018-09-11 stsp if (err)
68 c59b3346 2018-09-11 stsp goto done;
69 c59b3346 2018-09-11 stsp obj->refcnt++;
70 c59b3346 2018-09-11 stsp
71 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
72 c59b3346 2018-09-11 stsp done:
73 876c234b 2018-09-10 stsp got_object_close(obj);
74 876c234b 2018-09-10 stsp return err;
75 876c234b 2018-09-10 stsp }
76 876c234b 2018-09-10 stsp
77 876c234b 2018-09-10 stsp static const struct got_error *
78 c59b3346 2018-09-11 stsp get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
79 c59b3346 2018-09-11 stsp struct got_pack *pack, struct got_packidx *packidx,
80 c59b3346 2018-09-11 stsp struct got_object_cache *objcache, int type)
81 c59b3346 2018-09-11 stsp {
82 c59b3346 2018-09-11 stsp const struct got_error *err = NULL;
83 c59b3346 2018-09-11 stsp struct got_object *iobj;
84 c59b3346 2018-09-11 stsp
85 c59b3346 2018-09-11 stsp err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
86 c59b3346 2018-09-11 stsp if (err)
87 c59b3346 2018-09-11 stsp return err;
88 c59b3346 2018-09-11 stsp
89 c59b3346 2018-09-11 stsp if (iobj->type != type) {
90 c59b3346 2018-09-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
91 c59b3346 2018-09-11 stsp goto done;
92 c59b3346 2018-09-11 stsp }
93 c59b3346 2018-09-11 stsp
94 c59b3346 2018-09-11 stsp if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
95 c59b3346 2018-09-11 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
96 c59b3346 2018-09-11 stsp
97 c59b3346 2018-09-11 stsp *obj = got_object_cache_get(objcache, &iobj->id);
98 c59b3346 2018-09-11 stsp if (*obj == NULL) {
99 c59b3346 2018-09-11 stsp err = got_packfile_open_object(obj, pack, packidx,
100 c59b3346 2018-09-11 stsp iobj->pack_idx, &iobj->id);
101 c59b3346 2018-09-11 stsp if (err)
102 c59b3346 2018-09-11 stsp goto done;
103 c59b3346 2018-09-11 stsp }
104 c59b3346 2018-09-11 stsp (*obj)->refcnt++;
105 c59b3346 2018-09-11 stsp done:
106 c59b3346 2018-09-11 stsp got_object_close(iobj);
107 c59b3346 2018-09-11 stsp return err;
108 c59b3346 2018-09-11 stsp }
109 c59b3346 2018-09-11 stsp
110 c59b3346 2018-09-11 stsp static const struct got_error *
111 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
112 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
113 876c234b 2018-09-10 stsp {
114 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
115 cfd633c2 2018-09-10 stsp struct got_object *obj = NULL;
116 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
117 cfd633c2 2018-09-10 stsp uint8_t *buf;
118 cfd633c2 2018-09-10 stsp size_t len;
119 cfd633c2 2018-09-10 stsp
120 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
121 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_COMMIT);
122 cfd633c2 2018-09-10 stsp if (err)
123 cfd633c2 2018-09-10 stsp return err;
124 cfd633c2 2018-09-10 stsp
125 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
126 cfd633c2 2018-09-10 stsp if (err)
127 cfd633c2 2018-09-10 stsp return err;
128 cfd633c2 2018-09-10 stsp
129 cfd633c2 2018-09-10 stsp obj->size = len;
130 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
131 cfd633c2 2018-09-10 stsp free(buf);
132 cfd633c2 2018-09-10 stsp
133 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
134 cfd633c2 2018-09-10 stsp if (obj)
135 cfd633c2 2018-09-10 stsp got_object_close(obj);
136 cfd633c2 2018-09-10 stsp got_object_commit_close(commit);
137 7762fe12 2018-11-05 stsp if (err) {
138 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
139 7762fe12 2018-11-05 stsp err = NULL;
140 7762fe12 2018-11-05 stsp else
141 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
142 7762fe12 2018-11-05 stsp }
143 7762fe12 2018-11-05 stsp
144 7762fe12 2018-11-05 stsp return err;
145 7762fe12 2018-11-05 stsp }
146 7762fe12 2018-11-05 stsp
147 7762fe12 2018-11-05 stsp static const struct got_error *
148 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
149 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
150 876c234b 2018-09-10 stsp {
151 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
152 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
153 e7885405 2018-09-10 stsp struct got_tree_object *tree = NULL;
154 e7885405 2018-09-10 stsp uint8_t *buf;
155 e7885405 2018-09-10 stsp size_t len;
156 e7885405 2018-09-10 stsp
157 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
158 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_TREE);
159 e7885405 2018-09-10 stsp if (err)
160 e7885405 2018-09-10 stsp return err;
161 e7885405 2018-09-10 stsp
162 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
163 e7885405 2018-09-10 stsp if (err)
164 e7885405 2018-09-10 stsp return err;
165 e7885405 2018-09-10 stsp
166 e7885405 2018-09-10 stsp obj->size = len;
167 e7885405 2018-09-10 stsp err = got_object_parse_tree(&tree, buf, len);
168 e7885405 2018-09-10 stsp free(buf);
169 e7885405 2018-09-10 stsp
170 e7885405 2018-09-10 stsp err = got_privsep_send_tree(ibuf, tree);
171 e7885405 2018-09-10 stsp if (obj)
172 e7885405 2018-09-10 stsp got_object_close(obj);
173 e7885405 2018-09-10 stsp got_object_tree_close(tree);
174 e7885405 2018-09-10 stsp if (err) {
175 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
176 e7885405 2018-09-10 stsp err = NULL;
177 e7885405 2018-09-10 stsp else
178 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
179 e7885405 2018-09-10 stsp }
180 e7885405 2018-09-10 stsp
181 e7885405 2018-09-10 stsp return err;
182 876c234b 2018-09-10 stsp }
183 876c234b 2018-09-10 stsp
184 876c234b 2018-09-10 stsp static const struct got_error *
185 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
186 876c234b 2018-09-10 stsp {
187 3840f4c9 2018-09-12 stsp const struct got_error *err;
188 3840f4c9 2018-09-12 stsp struct imsg imsg;
189 55da3778 2018-09-10 stsp size_t datalen;
190 55da3778 2018-09-10 stsp
191 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
192 55da3778 2018-09-10 stsp if (err)
193 55da3778 2018-09-10 stsp return err;
194 55da3778 2018-09-10 stsp
195 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
196 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
197 55da3778 2018-09-10 stsp goto done;
198 55da3778 2018-09-10 stsp }
199 55da3778 2018-09-10 stsp
200 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
201 55da3778 2018-09-10 stsp if (datalen != 0) {
202 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
203 55da3778 2018-09-10 stsp goto done;
204 55da3778 2018-09-10 stsp }
205 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
206 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
207 55da3778 2018-09-10 stsp goto done;
208 55da3778 2018-09-10 stsp }
209 55da3778 2018-09-10 stsp
210 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
211 3840f4c9 2018-09-12 stsp if (*f == NULL) {
212 3840f4c9 2018-09-12 stsp close(imsg.fd);
213 55da3778 2018-09-10 stsp err = got_error_from_errno();
214 55da3778 2018-09-10 stsp goto done;
215 55da3778 2018-09-10 stsp }
216 3840f4c9 2018-09-12 stsp done:
217 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
218 3840f4c9 2018-09-12 stsp return err;
219 3840f4c9 2018-09-12 stsp }
220 55da3778 2018-09-10 stsp
221 3840f4c9 2018-09-12 stsp static const struct got_error *
222 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
223 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
224 3840f4c9 2018-09-12 stsp {
225 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
226 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
227 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
228 3840f4c9 2018-09-12 stsp
229 3840f4c9 2018-09-12 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
230 3840f4c9 2018-09-12 stsp GOT_OBJ_TYPE_BLOB);
231 55da3778 2018-09-10 stsp if (err)
232 3840f4c9 2018-09-12 stsp return err;
233 3840f4c9 2018-09-12 stsp
234 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
235 3840f4c9 2018-09-12 stsp if (err)
236 3840f4c9 2018-09-12 stsp return err;
237 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
238 3840f4c9 2018-09-12 stsp if (err)
239 3840f4c9 2018-09-12 stsp return err;
240 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
241 3840f4c9 2018-09-12 stsp if (err)
242 3840f4c9 2018-09-12 stsp return err;
243 3840f4c9 2018-09-12 stsp
244 3840f4c9 2018-09-12 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
245 3840f4c9 2018-09-12 stsp accumfile);
246 3840f4c9 2018-09-12 stsp if (err)
247 55da3778 2018-09-10 stsp goto done;
248 55da3778 2018-09-10 stsp
249 55da3778 2018-09-10 stsp err = got_privsep_send_blob(ibuf, obj->size);
250 55da3778 2018-09-10 stsp done:
251 3840f4c9 2018-09-12 stsp if (outfile)
252 3840f4c9 2018-09-12 stsp fclose(outfile);
253 3840f4c9 2018-09-12 stsp if (basefile)
254 3840f4c9 2018-09-12 stsp fclose(basefile);
255 3840f4c9 2018-09-12 stsp if (accumfile)
256 3840f4c9 2018-09-12 stsp fclose(accumfile);
257 c59b3346 2018-09-11 stsp if (obj)
258 c59b3346 2018-09-11 stsp got_object_close(obj);
259 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
260 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
261 55da3778 2018-09-10 stsp
262 55da3778 2018-09-10 stsp return err;
263 876c234b 2018-09-10 stsp }
264 876c234b 2018-09-10 stsp
265 876c234b 2018-09-10 stsp static const struct got_error *
266 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
267 876c234b 2018-09-10 stsp {
268 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
269 876c234b 2018-09-10 stsp struct imsg imsg;
270 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
271 876c234b 2018-09-10 stsp size_t datalen;
272 876c234b 2018-09-10 stsp struct got_packidx *p;
273 876c234b 2018-09-10 stsp
274 876c234b 2018-09-10 stsp *packidx = NULL;
275 876c234b 2018-09-10 stsp
276 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
277 876c234b 2018-09-10 stsp if (err)
278 876c234b 2018-09-10 stsp return err;
279 876c234b 2018-09-10 stsp
280 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
281 876c234b 2018-09-10 stsp if (p == NULL) {
282 876c234b 2018-09-10 stsp err = got_error_from_errno();
283 876c234b 2018-09-10 stsp goto done;
284 876c234b 2018-09-10 stsp }
285 876c234b 2018-09-10 stsp
286 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
287 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
288 876c234b 2018-09-10 stsp goto done;
289 876c234b 2018-09-10 stsp }
290 876c234b 2018-09-10 stsp
291 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
292 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
293 876c234b 2018-09-10 stsp goto done;
294 876c234b 2018-09-10 stsp }
295 876c234b 2018-09-10 stsp
296 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
297 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
298 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
299 876c234b 2018-09-10 stsp goto done;
300 876c234b 2018-09-10 stsp }
301 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
302 876c234b 2018-09-10 stsp
303 876c234b 2018-09-10 stsp p->len = ipackidx.len;
304 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
305 876c234b 2018-09-10 stsp if (p->fd == -1) {
306 56bef47a 2018-09-15 stsp err = got_error_from_errno();
307 56bef47a 2018-09-15 stsp goto done;
308 56bef47a 2018-09-15 stsp }
309 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
310 876c234b 2018-09-10 stsp err = got_error_from_errno();
311 876c234b 2018-09-10 stsp goto done;
312 876c234b 2018-09-10 stsp }
313 876c234b 2018-09-10 stsp
314 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
315 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
316 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
317 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
318 876c234b 2018-09-10 stsp #endif
319 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
320 876c234b 2018-09-10 stsp done:
321 876c234b 2018-09-10 stsp if (err) {
322 876c234b 2018-09-10 stsp if (imsg.fd != -1)
323 876c234b 2018-09-10 stsp close(imsg.fd);
324 876c234b 2018-09-10 stsp got_packidx_close(p);
325 876c234b 2018-09-10 stsp } else
326 876c234b 2018-09-10 stsp *packidx = p;
327 876c234b 2018-09-10 stsp imsg_free(&imsg);
328 876c234b 2018-09-10 stsp return err;
329 876c234b 2018-09-10 stsp }
330 876c234b 2018-09-10 stsp
331 876c234b 2018-09-10 stsp static const struct got_error *
332 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
333 876c234b 2018-09-10 stsp {
334 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
335 876c234b 2018-09-10 stsp struct imsg imsg;
336 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
337 876c234b 2018-09-10 stsp size_t datalen;
338 876c234b 2018-09-10 stsp struct got_pack *pack;
339 876c234b 2018-09-10 stsp
340 876c234b 2018-09-10 stsp *packp = NULL;
341 876c234b 2018-09-10 stsp
342 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
343 876c234b 2018-09-10 stsp if (err)
344 876c234b 2018-09-10 stsp return err;
345 876c234b 2018-09-10 stsp
346 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
347 876c234b 2018-09-10 stsp if (pack == NULL) {
348 876c234b 2018-09-10 stsp err = got_error_from_errno();
349 876c234b 2018-09-10 stsp goto done;
350 876c234b 2018-09-10 stsp }
351 876c234b 2018-09-10 stsp
352 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
353 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
354 876c234b 2018-09-10 stsp goto done;
355 876c234b 2018-09-10 stsp }
356 876c234b 2018-09-10 stsp
357 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
358 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
359 876c234b 2018-09-10 stsp goto done;
360 876c234b 2018-09-10 stsp }
361 876c234b 2018-09-10 stsp
362 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
363 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
364 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
365 876c234b 2018-09-10 stsp goto done;
366 876c234b 2018-09-10 stsp }
367 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
368 876c234b 2018-09-10 stsp
369 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
370 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
371 876c234b 2018-09-10 stsp if (pack->fd == -1) {
372 876c234b 2018-09-10 stsp err = got_error_from_errno();
373 876c234b 2018-09-10 stsp goto done;
374 876c234b 2018-09-10 stsp }
375 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
376 56bef47a 2018-09-15 stsp err = got_error_from_errno();
377 56bef47a 2018-09-15 stsp goto done;
378 56bef47a 2018-09-15 stsp }
379 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
380 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
381 876c234b 2018-09-10 stsp err = got_error_from_errno();
382 876c234b 2018-09-10 stsp goto done;
383 876c234b 2018-09-10 stsp }
384 876c234b 2018-09-10 stsp
385 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
386 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
387 876c234b 2018-09-10 stsp pack->fd, 0);
388 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
389 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
390 876c234b 2018-09-10 stsp #endif
391 876c234b 2018-09-10 stsp done:
392 876c234b 2018-09-10 stsp if (err) {
393 876c234b 2018-09-10 stsp if (imsg.fd != -1)
394 876c234b 2018-09-10 stsp close(imsg.fd);
395 876c234b 2018-09-10 stsp free(pack);
396 876c234b 2018-09-10 stsp } else
397 876c234b 2018-09-10 stsp *packp = pack;
398 876c234b 2018-09-10 stsp imsg_free(&imsg);
399 876c234b 2018-09-10 stsp return err;
400 876c234b 2018-09-10 stsp }
401 876c234b 2018-09-10 stsp
402 876c234b 2018-09-10 stsp int
403 876c234b 2018-09-10 stsp main(int argc, char *argv[])
404 876c234b 2018-09-10 stsp {
405 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
406 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
407 876c234b 2018-09-10 stsp struct imsg imsg;
408 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
409 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
410 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
411 876c234b 2018-09-10 stsp
412 876c234b 2018-09-10 stsp //static int attached;
413 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
414 876c234b 2018-09-10 stsp
415 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
416 876c234b 2018-09-10 stsp
417 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
418 c59b3346 2018-09-11 stsp if (err) {
419 c59b3346 2018-09-11 stsp err = got_error_from_errno();
420 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
421 c59b3346 2018-09-11 stsp return 1;
422 c59b3346 2018-09-11 stsp }
423 c59b3346 2018-09-11 stsp
424 2ff12563 2018-09-15 stsp #ifndef PROFILE
425 876c234b 2018-09-10 stsp /* revoke access to most system calls */
426 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
427 876c234b 2018-09-10 stsp err = got_error_from_errno();
428 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
429 876c234b 2018-09-10 stsp return 1;
430 876c234b 2018-09-10 stsp }
431 2ff12563 2018-09-15 stsp #endif
432 876c234b 2018-09-10 stsp
433 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
434 876c234b 2018-09-10 stsp if (err) {
435 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
436 876c234b 2018-09-10 stsp return 1;
437 876c234b 2018-09-10 stsp }
438 876c234b 2018-09-10 stsp
439 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
440 876c234b 2018-09-10 stsp if (err) {
441 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
442 876c234b 2018-09-10 stsp return 1;
443 876c234b 2018-09-10 stsp }
444 876c234b 2018-09-10 stsp
445 876c234b 2018-09-10 stsp while (1) {
446 876c234b 2018-09-10 stsp imsg.fd = -1;
447 876c234b 2018-09-10 stsp
448 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
449 876c234b 2018-09-10 stsp if (err) {
450 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
451 876c234b 2018-09-10 stsp err = NULL;
452 876c234b 2018-09-10 stsp break;
453 876c234b 2018-09-10 stsp }
454 876c234b 2018-09-10 stsp
455 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
456 876c234b 2018-09-10 stsp break;
457 876c234b 2018-09-10 stsp
458 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
459 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
460 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
461 c59b3346 2018-09-11 stsp &objcache);
462 876c234b 2018-09-10 stsp break;
463 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
464 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
465 7762fe12 2018-11-05 stsp &objcache);
466 7762fe12 2018-11-05 stsp break;
467 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
468 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
469 c59b3346 2018-09-11 stsp &objcache);
470 876c234b 2018-09-10 stsp break;
471 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
472 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
473 c59b3346 2018-09-11 stsp &objcache);
474 876c234b 2018-09-10 stsp break;
475 876c234b 2018-09-10 stsp default:
476 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
477 876c234b 2018-09-10 stsp break;
478 876c234b 2018-09-10 stsp }
479 876c234b 2018-09-10 stsp
480 876c234b 2018-09-10 stsp if (imsg.fd != -1)
481 876c234b 2018-09-10 stsp close(imsg.fd);
482 876c234b 2018-09-10 stsp imsg_free(&imsg);
483 876c234b 2018-09-10 stsp if (err) {
484 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
485 876c234b 2018-09-10 stsp err = NULL;
486 876c234b 2018-09-10 stsp else
487 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
488 876c234b 2018-09-10 stsp break;
489 876c234b 2018-09-10 stsp }
490 876c234b 2018-09-10 stsp }
491 876c234b 2018-09-10 stsp
492 c59b3346 2018-09-11 stsp if (packidx)
493 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
494 c59b3346 2018-09-11 stsp if (pack)
495 c59b3346 2018-09-11 stsp got_pack_close(pack);
496 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
497 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
498 876c234b 2018-09-10 stsp if (err)
499 876c234b 2018-09-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
500 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
501 876c234b 2018-09-10 stsp return err ? 1 : 0;
502 876c234b 2018-09-10 stsp }