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