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