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