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