Blame


1 876c234b 2018-09-10 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
207 3a6ce05a 2019-02-11 stsp close(imsg.fd);
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 ac544f8c 2019-01-13 stsp uint64_t blob_size;
226 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
227 3840f4c9 2018-09-12 stsp
228 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
229 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
230 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
231 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
232 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
233 ebc55e2d 2018-12-24 stsp
234 ebc55e2d 2018-12-24 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
235 55da3778 2018-09-10 stsp if (err)
236 3840f4c9 2018-09-12 stsp return err;
237 3840f4c9 2018-09-12 stsp
238 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
239 3840f4c9 2018-09-12 stsp if (err)
240 ac544f8c 2019-01-13 stsp goto done;
241 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
242 3840f4c9 2018-09-12 stsp if (err)
243 ac544f8c 2019-01-13 stsp goto done;
244 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
245 3840f4c9 2018-09-12 stsp if (err)
246 ac544f8c 2019-01-13 stsp goto done;
247 3840f4c9 2018-09-12 stsp
248 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
249 85a703fa 2019-01-13 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj);
250 ac544f8c 2019-01-13 stsp if (err)
251 ac544f8c 2019-01-13 stsp goto done;
252 ac544f8c 2019-01-13 stsp } else
253 ac544f8c 2019-01-13 stsp blob_size = obj->size;
254 ac544f8c 2019-01-13 stsp
255 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
256 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
257 ac544f8c 2019-01-13 stsp obj, pack);
258 ac544f8c 2019-01-13 stsp else
259 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
260 ac544f8c 2019-01-13 stsp accumfile);
261 3840f4c9 2018-09-12 stsp if (err)
262 55da3778 2018-09-10 stsp goto done;
263 55da3778 2018-09-10 stsp
264 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
265 55da3778 2018-09-10 stsp done:
266 ac544f8c 2019-01-13 stsp free(buf);
267 fb43ecf1 2019-02-11 stsp if (outfile && fclose(outfile) != 0 && err == NULL)
268 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
269 638f9024 2019-05-13 stsp if (basefile && fclose(basefile) != 0 && err == NULL)
270 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
271 fb43ecf1 2019-02-11 stsp if (accumfile && fclose(accumfile) != 0 && err == NULL)
272 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
273 c59b3346 2018-09-11 stsp if (obj)
274 c59b3346 2018-09-11 stsp got_object_close(obj);
275 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
276 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
277 55da3778 2018-09-10 stsp
278 55da3778 2018-09-10 stsp return err;
279 876c234b 2018-09-10 stsp }
280 876c234b 2018-09-10 stsp
281 876c234b 2018-09-10 stsp static const struct got_error *
282 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
283 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
284 f4a881ce 2018-11-17 stsp {
285 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
286 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
287 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
288 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
289 f4a881ce 2018-11-17 stsp uint8_t *buf;
290 f4a881ce 2018-11-17 stsp size_t len;
291 268f7291 2018-12-24 stsp struct got_object_id id;
292 268f7291 2018-12-24 stsp size_t datalen;
293 f4a881ce 2018-11-17 stsp
294 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
295 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
296 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
297 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
298 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
299 268f7291 2018-12-24 stsp
300 268f7291 2018-12-24 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
301 f4a881ce 2018-11-17 stsp if (err)
302 f4a881ce 2018-11-17 stsp return err;
303 f4a881ce 2018-11-17 stsp
304 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
305 f4a881ce 2018-11-17 stsp if (err)
306 f4a881ce 2018-11-17 stsp return err;
307 f4a881ce 2018-11-17 stsp
308 f4a881ce 2018-11-17 stsp obj->size = len;
309 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
310 f4a881ce 2018-11-17 stsp free(buf);
311 0ae4af15 2019-02-01 stsp if (err)
312 0ae4af15 2019-02-01 stsp return err;
313 f4a881ce 2018-11-17 stsp
314 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
315 f4a881ce 2018-11-17 stsp if (obj)
316 f4a881ce 2018-11-17 stsp got_object_close(obj);
317 f4a881ce 2018-11-17 stsp got_object_tag_close(tag);
318 f4a881ce 2018-11-17 stsp if (err) {
319 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
320 f4a881ce 2018-11-17 stsp err = NULL;
321 f4a881ce 2018-11-17 stsp else
322 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
323 f4a881ce 2018-11-17 stsp }
324 f4a881ce 2018-11-17 stsp
325 f4a881ce 2018-11-17 stsp return err;
326 f4a881ce 2018-11-17 stsp }
327 f4a881ce 2018-11-17 stsp
328 f4a881ce 2018-11-17 stsp static const struct got_error *
329 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
330 876c234b 2018-09-10 stsp {
331 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
332 876c234b 2018-09-10 stsp struct imsg imsg;
333 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
334 876c234b 2018-09-10 stsp size_t datalen;
335 876c234b 2018-09-10 stsp struct got_packidx *p;
336 876c234b 2018-09-10 stsp
337 876c234b 2018-09-10 stsp *packidx = NULL;
338 876c234b 2018-09-10 stsp
339 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
340 876c234b 2018-09-10 stsp if (err)
341 876c234b 2018-09-10 stsp return err;
342 876c234b 2018-09-10 stsp
343 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
344 876c234b 2018-09-10 stsp if (p == NULL) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
346 876c234b 2018-09-10 stsp goto done;
347 876c234b 2018-09-10 stsp }
348 876c234b 2018-09-10 stsp
349 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
350 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
351 876c234b 2018-09-10 stsp goto done;
352 876c234b 2018-09-10 stsp }
353 876c234b 2018-09-10 stsp
354 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
355 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
356 876c234b 2018-09-10 stsp goto done;
357 876c234b 2018-09-10 stsp }
358 876c234b 2018-09-10 stsp
359 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
360 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
361 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
362 876c234b 2018-09-10 stsp goto done;
363 876c234b 2018-09-10 stsp }
364 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
365 876c234b 2018-09-10 stsp
366 876c234b 2018-09-10 stsp p->len = ipackidx.len;
367 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
368 876c234b 2018-09-10 stsp if (p->fd == -1) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
370 56bef47a 2018-09-15 stsp goto done;
371 56bef47a 2018-09-15 stsp }
372 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
373 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
374 876c234b 2018-09-10 stsp goto done;
375 876c234b 2018-09-10 stsp }
376 876c234b 2018-09-10 stsp
377 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
378 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
379 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
380 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
381 876c234b 2018-09-10 stsp #endif
382 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
383 876c234b 2018-09-10 stsp done:
384 876c234b 2018-09-10 stsp if (err) {
385 876c234b 2018-09-10 stsp if (imsg.fd != -1)
386 876c234b 2018-09-10 stsp close(imsg.fd);
387 876c234b 2018-09-10 stsp got_packidx_close(p);
388 876c234b 2018-09-10 stsp } else
389 876c234b 2018-09-10 stsp *packidx = p;
390 876c234b 2018-09-10 stsp imsg_free(&imsg);
391 876c234b 2018-09-10 stsp return err;
392 876c234b 2018-09-10 stsp }
393 876c234b 2018-09-10 stsp
394 876c234b 2018-09-10 stsp static const struct got_error *
395 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
396 876c234b 2018-09-10 stsp {
397 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
398 876c234b 2018-09-10 stsp struct imsg imsg;
399 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
400 876c234b 2018-09-10 stsp size_t datalen;
401 876c234b 2018-09-10 stsp struct got_pack *pack;
402 876c234b 2018-09-10 stsp
403 876c234b 2018-09-10 stsp *packp = NULL;
404 876c234b 2018-09-10 stsp
405 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
406 876c234b 2018-09-10 stsp if (err)
407 876c234b 2018-09-10 stsp return err;
408 876c234b 2018-09-10 stsp
409 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
410 876c234b 2018-09-10 stsp if (pack == NULL) {
411 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
412 876c234b 2018-09-10 stsp goto done;
413 876c234b 2018-09-10 stsp }
414 876c234b 2018-09-10 stsp
415 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
416 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
417 876c234b 2018-09-10 stsp goto done;
418 876c234b 2018-09-10 stsp }
419 876c234b 2018-09-10 stsp
420 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
421 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
422 876c234b 2018-09-10 stsp goto done;
423 876c234b 2018-09-10 stsp }
424 876c234b 2018-09-10 stsp
425 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
426 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
427 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
428 876c234b 2018-09-10 stsp goto done;
429 876c234b 2018-09-10 stsp }
430 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
431 876c234b 2018-09-10 stsp
432 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
433 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
434 876c234b 2018-09-10 stsp if (pack->fd == -1) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
436 876c234b 2018-09-10 stsp goto done;
437 876c234b 2018-09-10 stsp }
438 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
439 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
440 56bef47a 2018-09-15 stsp goto done;
441 56bef47a 2018-09-15 stsp }
442 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
443 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
444 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
445 876c234b 2018-09-10 stsp goto done;
446 876c234b 2018-09-10 stsp }
447 876c234b 2018-09-10 stsp
448 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
449 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
450 876c234b 2018-09-10 stsp pack->fd, 0);
451 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
452 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
453 876c234b 2018-09-10 stsp #endif
454 876c234b 2018-09-10 stsp done:
455 876c234b 2018-09-10 stsp if (err) {
456 876c234b 2018-09-10 stsp if (imsg.fd != -1)
457 876c234b 2018-09-10 stsp close(imsg.fd);
458 876c234b 2018-09-10 stsp free(pack);
459 876c234b 2018-09-10 stsp } else
460 876c234b 2018-09-10 stsp *packp = pack;
461 876c234b 2018-09-10 stsp imsg_free(&imsg);
462 876c234b 2018-09-10 stsp return err;
463 876c234b 2018-09-10 stsp }
464 876c234b 2018-09-10 stsp
465 876c234b 2018-09-10 stsp int
466 876c234b 2018-09-10 stsp main(int argc, char *argv[])
467 876c234b 2018-09-10 stsp {
468 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
469 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
470 876c234b 2018-09-10 stsp struct imsg imsg;
471 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
472 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
473 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
474 876c234b 2018-09-10 stsp
475 876c234b 2018-09-10 stsp //static int attached;
476 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
477 876c234b 2018-09-10 stsp
478 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
479 99437157 2018-11-11 stsp
480 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
481 876c234b 2018-09-10 stsp
482 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
483 c59b3346 2018-09-11 stsp if (err) {
484 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
485 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
486 c59b3346 2018-09-11 stsp return 1;
487 c59b3346 2018-09-11 stsp }
488 c59b3346 2018-09-11 stsp
489 2ff12563 2018-09-15 stsp #ifndef PROFILE
490 876c234b 2018-09-10 stsp /* revoke access to most system calls */
491 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
492 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
493 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
494 876c234b 2018-09-10 stsp return 1;
495 876c234b 2018-09-10 stsp }
496 2ff12563 2018-09-15 stsp #endif
497 876c234b 2018-09-10 stsp
498 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
499 876c234b 2018-09-10 stsp if (err) {
500 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
501 876c234b 2018-09-10 stsp return 1;
502 876c234b 2018-09-10 stsp }
503 876c234b 2018-09-10 stsp
504 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
505 876c234b 2018-09-10 stsp if (err) {
506 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
507 876c234b 2018-09-10 stsp return 1;
508 876c234b 2018-09-10 stsp }
509 876c234b 2018-09-10 stsp
510 656b1f76 2019-05-11 jcs for (;;) {
511 876c234b 2018-09-10 stsp imsg.fd = -1;
512 99437157 2018-11-11 stsp
513 99437157 2018-11-11 stsp if (sigint_received) {
514 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
515 99437157 2018-11-11 stsp break;
516 99437157 2018-11-11 stsp }
517 876c234b 2018-09-10 stsp
518 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
519 876c234b 2018-09-10 stsp if (err) {
520 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
521 876c234b 2018-09-10 stsp err = NULL;
522 876c234b 2018-09-10 stsp break;
523 876c234b 2018-09-10 stsp }
524 876c234b 2018-09-10 stsp
525 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
526 876c234b 2018-09-10 stsp break;
527 876c234b 2018-09-10 stsp
528 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
529 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
530 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
531 c59b3346 2018-09-11 stsp &objcache);
532 876c234b 2018-09-10 stsp break;
533 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
534 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
535 7762fe12 2018-11-05 stsp &objcache);
536 7762fe12 2018-11-05 stsp break;
537 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
538 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
539 c59b3346 2018-09-11 stsp &objcache);
540 876c234b 2018-09-10 stsp break;
541 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
542 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
543 c59b3346 2018-09-11 stsp &objcache);
544 876c234b 2018-09-10 stsp break;
545 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
546 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
547 f4a881ce 2018-11-17 stsp &objcache);
548 f4a881ce 2018-11-17 stsp break;
549 876c234b 2018-09-10 stsp default:
550 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
551 876c234b 2018-09-10 stsp break;
552 876c234b 2018-09-10 stsp }
553 876c234b 2018-09-10 stsp
554 3a6ce05a 2019-02-11 stsp if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
555 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
556 876c234b 2018-09-10 stsp imsg_free(&imsg);
557 99437157 2018-11-11 stsp if (err)
558 876c234b 2018-09-10 stsp break;
559 876c234b 2018-09-10 stsp }
560 876c234b 2018-09-10 stsp
561 c59b3346 2018-09-11 stsp if (packidx)
562 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
563 c59b3346 2018-09-11 stsp if (pack)
564 c59b3346 2018-09-11 stsp got_pack_close(pack);
565 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
566 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
567 99437157 2018-11-11 stsp if (err) {
568 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
569 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
570 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
571 80d5f134 2018-11-11 stsp }
572 99437157 2018-11-11 stsp }
573 3a6ce05a 2019-02-11 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
574 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
575 876c234b 2018-09-10 stsp return err ? 1 : 0;
576 876c234b 2018-09-10 stsp }