Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
23 #include <sys/mman.h>
25 #include <limits.h>
26 #include <stdint.h>
27 #include <imsg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sha1.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_parse.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
44 static const struct got_error *
45 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
46 struct got_packidx *packidx)
47 {
48 const struct got_error *err = NULL;
49 struct got_imsg_packed_object iobj;
50 struct got_object *obj;
51 size_t datalen;
53 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
54 if (datalen != sizeof(iobj))
55 return got_error(GOT_ERR_PRIVSEP_LEN);
56 memcpy(&iobj, imsg->data, sizeof(iobj));
58 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
59 if (err)
60 return err;
62 err = got_privsep_send_obj(ibuf, obj);
63 got_object_close(obj);
64 return err;
65 }
67 static const struct got_error *
68 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
69 struct got_packidx *packidx)
70 {
71 const struct got_error *err = NULL;
72 struct got_object *obj = NULL;
73 struct got_commit_object *commit = NULL;
74 uint8_t *buf;
75 size_t len;
77 err = got_privsep_get_imsg_obj(&obj, imsg, ibuf);
78 if (err)
79 return err;
81 if (obj->type != GOT_OBJ_TYPE_COMMIT)
82 return got_error(GOT_ERR_OBJ_TYPE);
84 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
85 if (err)
86 return err;
88 obj->size = len;
89 err = got_object_parse_commit(&commit, buf, len);
90 free(buf);
92 err = got_privsep_send_commit(ibuf, commit);
93 if (obj)
94 got_object_close(obj);
95 got_object_commit_close(commit);
96 if (err) {
97 if (err->code == GOT_ERR_PRIVSEP_PIPE)
98 err = NULL;
99 else
100 got_privsep_send_error(ibuf, err);
103 return err;
106 static const struct got_error *
107 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
108 struct got_packidx *packidx)
110 return got_error(GOT_ERR_NOT_IMPL);
113 static const struct got_error *
114 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
115 struct got_packidx *packidx)
117 return got_error(GOT_ERR_NOT_IMPL);
120 static const struct got_error *
121 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
123 const struct got_error *err = NULL;
124 struct imsg imsg;
125 struct got_imsg_packidx ipackidx;
126 size_t datalen;
127 struct got_packidx *p;
129 *packidx = NULL;
131 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
132 if (err)
133 return err;
135 p = calloc(1, sizeof(*p));
136 if (p == NULL) {
137 err = got_error_from_errno();
138 goto done;
141 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
142 err = got_error(GOT_ERR_PRIVSEP_MSG);
143 goto done;
146 if (imsg.fd == -1) {
147 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
148 goto done;
151 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
152 if (datalen != sizeof(ipackidx)) {
153 err = got_error(GOT_ERR_PRIVSEP_LEN);
154 goto done;
156 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
158 p->len = ipackidx.len;
159 p->fd = dup(imsg.fd);
160 if (p->fd == -1) {
161 err = got_error_from_errno();
162 goto done;
165 #ifndef GOT_PACK_NO_MMAP
166 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
167 if (p->map == MAP_FAILED)
168 p->map = NULL; /* fall back to read(2) */
169 #endif
170 err = got_packidx_init_hdr(p, 1);
171 done:
172 if (err) {
173 if (imsg.fd != -1)
174 close(imsg.fd);
175 got_packidx_close(p);
176 } else
177 *packidx = p;
178 imsg_free(&imsg);
179 return err;
182 static const struct got_error *
183 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
185 const struct got_error *err = NULL;
186 struct imsg imsg;
187 struct got_imsg_pack ipack;
188 size_t datalen;
189 struct got_pack *pack;
191 *packp = NULL;
193 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
194 if (err)
195 return err;
197 pack = calloc(1, sizeof(*pack));
198 if (pack == NULL) {
199 err = got_error_from_errno();
200 goto done;
203 if (imsg.hdr.type != GOT_IMSG_PACK) {
204 err = got_error(GOT_ERR_PRIVSEP_MSG);
205 goto done;
208 if (imsg.fd == -1) {
209 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
210 goto done;
213 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
214 if (datalen != sizeof(ipack)) {
215 err = got_error(GOT_ERR_PRIVSEP_LEN);
216 goto done;
218 memcpy(&ipack, imsg.data, sizeof(ipack));
220 pack->filesize = ipack.filesize;
221 pack->fd = dup(imsg.fd);
222 if (pack->fd == -1) {
223 err = got_error_from_errno();
224 goto done;
226 pack->path_packfile = strdup(ipack.path_packfile);
227 if (pack->path_packfile == NULL) {
228 err = got_error_from_errno();
229 goto done;
232 #ifndef GOT_PACK_NO_MMAP
233 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
234 pack->fd, 0);
235 if (pack->map == MAP_FAILED)
236 pack->map = NULL; /* fall back to read(2) */
237 #endif
238 done:
239 if (err) {
240 if (imsg.fd != -1)
241 close(imsg.fd);
242 free(pack);
243 } else
244 *packp = pack;
245 imsg_free(&imsg);
246 return err;
249 int
250 main(int argc, char *argv[])
252 const struct got_error *err = NULL;
253 struct imsgbuf ibuf;
254 struct imsg imsg;
255 struct got_packidx *packidx;
256 struct got_pack *pack;
258 //static int attached;
259 //while (!attached) sleep(1);
261 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
263 /* revoke access to most system calls */
264 if (pledge("stdio recvfd", NULL) == -1) {
265 err = got_error_from_errno();
266 got_privsep_send_error(&ibuf, err);
267 return 1;
270 err = receive_packidx(&packidx, &ibuf);
271 if (err) {
272 got_privsep_send_error(&ibuf, err);
273 return 1;
276 err = receive_pack(&pack, &ibuf);
277 if (err) {
278 got_privsep_send_error(&ibuf, err);
279 return 1;
282 while (1) {
283 imsg.fd = -1;
285 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
286 if (err) {
287 if (err->code == GOT_ERR_PRIVSEP_PIPE)
288 err = NULL;
289 break;
292 if (imsg.hdr.type == GOT_IMSG_STOP)
293 break;
295 switch (imsg.hdr.type) {
296 case GOT_IMSG_PACKED_OBJECT_REQUEST:
297 err = object_request(&imsg, &ibuf, pack, packidx);
298 break;
299 case GOT_IMSG_COMMIT_REQUEST:
300 err = commit_request(&imsg, &ibuf, pack, packidx);
301 break;
302 case GOT_IMSG_TREE_REQUEST:
303 err = tree_request(&imsg, &ibuf, pack, packidx);
304 break;
305 case GOT_IMSG_BLOB_REQUEST:
306 err = blob_request(&imsg, &ibuf, pack, packidx);
307 break;
308 default:
309 err = got_error(GOT_ERR_PRIVSEP_MSG);
310 break;
313 if (imsg.fd != -1)
314 close(imsg.fd);
315 imsg_free(&imsg);
316 if (err) {
317 if (err->code == GOT_ERR_PRIVSEP_PIPE)
318 err = NULL;
319 else
320 got_privsep_send_error(&ibuf, err);
321 break;
325 got_pack_close(pack);
326 imsg_clear(&ibuf);
327 if (err)
328 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
329 close(GOT_IMSG_FD_CHILD);
330 return err ? 1 : 0;