Blame


1 a440fac0 2018-09-06 stsp /*
2 a440fac0 2018-09-06 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 a440fac0 2018-09-06 stsp *
4 a440fac0 2018-09-06 stsp * Permission to use, copy, modify, and distribute this software for any
5 a440fac0 2018-09-06 stsp * purpose with or without fee is hereby granted, provided that the above
6 a440fac0 2018-09-06 stsp * copyright notice and this permission notice appear in all copies.
7 a440fac0 2018-09-06 stsp *
8 a440fac0 2018-09-06 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a440fac0 2018-09-06 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a440fac0 2018-09-06 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a440fac0 2018-09-06 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a440fac0 2018-09-06 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a440fac0 2018-09-06 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a440fac0 2018-09-06 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 a440fac0 2018-09-06 stsp */
16 a440fac0 2018-09-06 stsp
17 a440fac0 2018-09-06 stsp #include <sys/types.h>
18 a440fac0 2018-09-06 stsp #include <sys/stat.h>
19 a440fac0 2018-09-06 stsp #include <sys/queue.h>
20 a440fac0 2018-09-06 stsp #include <sys/uio.h>
21 a440fac0 2018-09-06 stsp #include <sys/socket.h>
22 a440fac0 2018-09-06 stsp #include <sys/wait.h>
23 a440fac0 2018-09-06 stsp
24 a440fac0 2018-09-06 stsp #include <errno.h>
25 a440fac0 2018-09-06 stsp #include <stdio.h>
26 a440fac0 2018-09-06 stsp #include <stdlib.h>
27 a440fac0 2018-09-06 stsp #include <string.h>
28 a440fac0 2018-09-06 stsp #include <stdint.h>
29 a440fac0 2018-09-06 stsp #include <sha1.h>
30 a440fac0 2018-09-06 stsp #include <zlib.h>
31 a440fac0 2018-09-06 stsp #include <ctype.h>
32 a440fac0 2018-09-06 stsp #include <limits.h>
33 a440fac0 2018-09-06 stsp #include <imsg.h>
34 a440fac0 2018-09-06 stsp #include <time.h>
35 ad242220 2018-09-08 stsp #include <unistd.h>
36 a440fac0 2018-09-06 stsp
37 a440fac0 2018-09-06 stsp #include "got_error.h"
38 a440fac0 2018-09-06 stsp #include "got_object.h"
39 a440fac0 2018-09-06 stsp #include "got_repository.h"
40 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
41 a440fac0 2018-09-06 stsp
42 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
43 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
44 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
45 a440fac0 2018-09-06 stsp #include "got_lib_pack.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_inflate.h"
47 a440fac0 2018-09-06 stsp #include "got_lib_object.h"
48 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
49 a440fac0 2018-09-06 stsp
50 a440fac0 2018-09-06 stsp #ifndef nitems
51 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 a440fac0 2018-09-06 stsp #endif
53 a440fac0 2018-09-06 stsp
54 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
55 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_TREE "tree"
56 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
57 a440fac0 2018-09-06 stsp
58 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
59 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
60 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
61 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
62 a440fac0 2018-09-06 stsp
63 03fa71c8 2018-09-06 stsp void
64 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
65 03fa71c8 2018-09-06 stsp {
66 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
67 03fa71c8 2018-09-06 stsp obj->refcnt--;
68 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
69 03fa71c8 2018-09-06 stsp return;
70 03fa71c8 2018-09-06 stsp }
71 03fa71c8 2018-09-06 stsp
72 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
73 03fa71c8 2018-09-06 stsp struct got_delta *delta;
74 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
75 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
76 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
77 03fa71c8 2018-09-06 stsp got_delta_close(delta);
78 03fa71c8 2018-09-06 stsp }
79 03fa71c8 2018-09-06 stsp }
80 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
81 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
82 03fa71c8 2018-09-06 stsp free(obj);
83 03fa71c8 2018-09-06 stsp }
84 03fa71c8 2018-09-06 stsp
85 03fa71c8 2018-09-06 stsp void
86 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
87 03fa71c8 2018-09-06 stsp {
88 03fa71c8 2018-09-06 stsp free(qid->id);
89 03fa71c8 2018-09-06 stsp free(qid);
90 03fa71c8 2018-09-06 stsp }
91 03fa71c8 2018-09-06 stsp
92 a440fac0 2018-09-06 stsp static const struct got_error *
93 ad242220 2018-09-08 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
94 a440fac0 2018-09-06 stsp {
95 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
96 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
97 a440fac0 2018-09-06 stsp
98 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
99 a440fac0 2018-09-06 stsp
100 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
101 a440fac0 2018-09-06 stsp if (err)
102 3516b818 2018-09-08 stsp return err;
103 3516b818 2018-09-08 stsp
104 3516b818 2018-09-08 stsp return got_privsep_recv_obj(obj, ibuf);
105 a440fac0 2018-09-06 stsp }
106 a440fac0 2018-09-06 stsp
107 a440fac0 2018-09-06 stsp static void
108 ad242220 2018-09-08 stsp exec_privsep_child(int imsg_fds[2], const char *path)
109 a440fac0 2018-09-06 stsp {
110 a440fac0 2018-09-06 stsp close(imsg_fds[0]);
111 a440fac0 2018-09-06 stsp
112 ad242220 2018-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
113 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
114 ad242220 2018-09-08 stsp strerror(errno));
115 ad242220 2018-09-08 stsp _exit(1);
116 a440fac0 2018-09-06 stsp }
117 ad242220 2018-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
118 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
119 ad242220 2018-09-08 stsp strerror(errno));
120 ad242220 2018-09-08 stsp _exit(1);
121 ad242220 2018-09-08 stsp }
122 a440fac0 2018-09-06 stsp
123 ad242220 2018-09-08 stsp if (execl(path, path, (char *)NULL) == -1) {
124 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
125 ad242220 2018-09-08 stsp strerror(errno));
126 ad242220 2018-09-08 stsp _exit(1);
127 a440fac0 2018-09-06 stsp }
128 a440fac0 2018-09-06 stsp }
129 a440fac0 2018-09-06 stsp
130 a440fac0 2018-09-06 stsp const struct got_error *
131 ad242220 2018-09-08 stsp got_object_read_header_privsep(struct got_object **obj,
132 ad242220 2018-09-08 stsp struct got_repository *repo, int obj_fd)
133 a440fac0 2018-09-06 stsp {
134 a440fac0 2018-09-06 stsp int imsg_fds[2];
135 a440fac0 2018-09-06 stsp pid_t pid;
136 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
137 a440fac0 2018-09-06 stsp
138 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
139 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
140 ad242220 2018-09-08 stsp
141 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
142 3516b818 2018-09-08 stsp if (ibuf == NULL)
143 3516b818 2018-09-08 stsp return got_error_from_errno();
144 3516b818 2018-09-08 stsp
145 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
146 a440fac0 2018-09-06 stsp return got_error_from_errno();
147 a440fac0 2018-09-06 stsp
148 a440fac0 2018-09-06 stsp pid = fork();
149 a440fac0 2018-09-06 stsp if (pid == -1)
150 a440fac0 2018-09-06 stsp return got_error_from_errno();
151 a440fac0 2018-09-06 stsp else if (pid == 0) {
152 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT);
153 a440fac0 2018-09-06 stsp /* not reached */
154 a440fac0 2018-09-06 stsp }
155 a440fac0 2018-09-06 stsp
156 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
157 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
158 ad242220 2018-09-08 stsp imsg_fds[0];
159 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
160 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
161 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
162 ad242220 2018-09-08 stsp
163 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
164 a440fac0 2018-09-06 stsp }
165 a440fac0 2018-09-06 stsp
166 a440fac0 2018-09-06 stsp struct got_commit_object *
167 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
168 a440fac0 2018-09-06 stsp {
169 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
170 a440fac0 2018-09-06 stsp
171 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
172 a440fac0 2018-09-06 stsp if (commit == NULL)
173 a440fac0 2018-09-06 stsp return NULL;
174 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
175 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
176 a440fac0 2018-09-06 stsp free(commit);
177 a440fac0 2018-09-06 stsp return NULL;
178 a440fac0 2018-09-06 stsp }
179 a440fac0 2018-09-06 stsp
180 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
181 a440fac0 2018-09-06 stsp
182 a440fac0 2018-09-06 stsp return commit;
183 a440fac0 2018-09-06 stsp }
184 a440fac0 2018-09-06 stsp
185 a440fac0 2018-09-06 stsp const struct got_error *
186 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
187 a440fac0 2018-09-06 stsp const char *id_str)
188 a440fac0 2018-09-06 stsp {
189 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
190 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
191 a440fac0 2018-09-06 stsp
192 a440fac0 2018-09-06 stsp qid = malloc(sizeof(*qid));
193 a440fac0 2018-09-06 stsp if (qid == NULL)
194 a440fac0 2018-09-06 stsp return got_error_from_errno();
195 a440fac0 2018-09-06 stsp
196 a440fac0 2018-09-06 stsp qid->id = malloc(sizeof(*qid->id));
197 a440fac0 2018-09-06 stsp if (qid->id == NULL) {
198 a440fac0 2018-09-06 stsp err = got_error_from_errno();
199 a440fac0 2018-09-06 stsp got_object_qid_free(qid);
200 a440fac0 2018-09-06 stsp return err;
201 a440fac0 2018-09-06 stsp }
202 a440fac0 2018-09-06 stsp
203 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
204 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
205 a440fac0 2018-09-06 stsp free(qid->id);
206 a440fac0 2018-09-06 stsp free(qid);
207 a440fac0 2018-09-06 stsp return err;
208 a440fac0 2018-09-06 stsp }
209 a440fac0 2018-09-06 stsp
210 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
211 a440fac0 2018-09-06 stsp commit->nparents++;
212 a440fac0 2018-09-06 stsp
213 a440fac0 2018-09-06 stsp return NULL;
214 a440fac0 2018-09-06 stsp }
215 a440fac0 2018-09-06 stsp
216 a440fac0 2018-09-06 stsp static const struct got_error *
217 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
218 a440fac0 2018-09-06 stsp {
219 a440fac0 2018-09-06 stsp int sign = 1;
220 a440fac0 2018-09-06 stsp const char *p = tzstr;
221 a440fac0 2018-09-06 stsp time_t h, m;
222 a440fac0 2018-09-06 stsp
223 a440fac0 2018-09-06 stsp *gmtoff = 0;
224 a440fac0 2018-09-06 stsp
225 a440fac0 2018-09-06 stsp if (*p == '-')
226 a440fac0 2018-09-06 stsp sign = -1;
227 a440fac0 2018-09-06 stsp else if (*p != '+')
228 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
229 a440fac0 2018-09-06 stsp p++;
230 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
231 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
232 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
233 a440fac0 2018-09-06 stsp
234 a440fac0 2018-09-06 stsp p += 2;
235 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
236 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
237 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
238 a440fac0 2018-09-06 stsp
239 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
240 a440fac0 2018-09-06 stsp return NULL;
241 a440fac0 2018-09-06 stsp }
242 a440fac0 2018-09-06 stsp
243 a440fac0 2018-09-06 stsp static const struct got_error *
244 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
245 a440fac0 2018-09-06 stsp {
246 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
247 a440fac0 2018-09-06 stsp const char *errstr;
248 a440fac0 2018-09-06 stsp char *space, *tzstr;
249 a440fac0 2018-09-06 stsp time_t gmtoff;
250 a440fac0 2018-09-06 stsp time_t time;
251 a440fac0 2018-09-06 stsp
252 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
253 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
254 a440fac0 2018-09-06 stsp if (space == NULL)
255 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
256 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
257 a440fac0 2018-09-06 stsp if (tzstr == NULL)
258 a440fac0 2018-09-06 stsp return got_error_from_errno();
259 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
260 a440fac0 2018-09-06 stsp free(tzstr);
261 a440fac0 2018-09-06 stsp if (err)
262 a440fac0 2018-09-06 stsp return err;
263 a440fac0 2018-09-06 stsp *space = '\0';
264 a440fac0 2018-09-06 stsp
265 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
266 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
267 a440fac0 2018-09-06 stsp if (space == NULL)
268 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
269 a440fac0 2018-09-06 stsp
270 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
271 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
272 a440fac0 2018-09-06 stsp if (errstr)
273 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
274 a440fac0 2018-09-06 stsp
275 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
276 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
277 a440fac0 2018-09-06 stsp time -= gmtoff;
278 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
279 a440fac0 2018-09-06 stsp return got_error_from_errno();
280 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
281 a440fac0 2018-09-06 stsp
282 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
283 a440fac0 2018-09-06 stsp *space = '\0';
284 a440fac0 2018-09-06 stsp
285 a440fac0 2018-09-06 stsp return NULL;
286 a440fac0 2018-09-06 stsp }
287 a440fac0 2018-09-06 stsp
288 03fa71c8 2018-09-06 stsp void
289 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
290 03fa71c8 2018-09-06 stsp {
291 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
292 03fa71c8 2018-09-06 stsp
293 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
294 03fa71c8 2018-09-06 stsp commit->refcnt--;
295 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
296 03fa71c8 2018-09-06 stsp return;
297 03fa71c8 2018-09-06 stsp }
298 03fa71c8 2018-09-06 stsp
299 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
300 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
301 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
302 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
303 03fa71c8 2018-09-06 stsp }
304 03fa71c8 2018-09-06 stsp
305 03fa71c8 2018-09-06 stsp free(commit->tree_id);
306 03fa71c8 2018-09-06 stsp free(commit->author);
307 03fa71c8 2018-09-06 stsp free(commit->committer);
308 03fa71c8 2018-09-06 stsp free(commit->logmsg);
309 03fa71c8 2018-09-06 stsp free(commit);
310 03fa71c8 2018-09-06 stsp }
311 03fa71c8 2018-09-06 stsp
312 a440fac0 2018-09-06 stsp const struct got_error *
313 a440fac0 2018-09-06 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
314 a440fac0 2018-09-06 stsp {
315 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
316 a440fac0 2018-09-06 stsp char *s = buf;
317 a440fac0 2018-09-06 stsp size_t tlen;
318 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
319 a440fac0 2018-09-06 stsp
320 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
321 a440fac0 2018-09-06 stsp if (*commit == NULL)
322 a440fac0 2018-09-06 stsp return got_error_from_errno();
323 a440fac0 2018-09-06 stsp
324 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
325 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
326 a440fac0 2018-09-06 stsp remain -= tlen;
327 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
328 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
329 a440fac0 2018-09-06 stsp goto done;
330 a440fac0 2018-09-06 stsp }
331 a440fac0 2018-09-06 stsp s += tlen;
332 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
333 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
334 a440fac0 2018-09-06 stsp goto done;
335 a440fac0 2018-09-06 stsp }
336 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
337 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
338 a440fac0 2018-09-06 stsp } else {
339 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
340 a440fac0 2018-09-06 stsp goto done;
341 a440fac0 2018-09-06 stsp }
342 a440fac0 2018-09-06 stsp
343 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
344 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
345 a440fac0 2018-09-06 stsp remain -= tlen;
346 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
347 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 a440fac0 2018-09-06 stsp goto done;
349 a440fac0 2018-09-06 stsp }
350 a440fac0 2018-09-06 stsp s += tlen;
351 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
352 a440fac0 2018-09-06 stsp if (err)
353 a440fac0 2018-09-06 stsp goto done;
354 a440fac0 2018-09-06 stsp
355 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
356 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
357 a440fac0 2018-09-06 stsp }
358 a440fac0 2018-09-06 stsp
359 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
360 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
361 a440fac0 2018-09-06 stsp char *p;
362 a440fac0 2018-09-06 stsp size_t slen;
363 a440fac0 2018-09-06 stsp
364 a440fac0 2018-09-06 stsp remain -= tlen;
365 a440fac0 2018-09-06 stsp if (remain <= 0) {
366 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
367 a440fac0 2018-09-06 stsp goto done;
368 a440fac0 2018-09-06 stsp }
369 a440fac0 2018-09-06 stsp s += tlen;
370 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
371 a440fac0 2018-09-06 stsp if (p == NULL) {
372 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 a440fac0 2018-09-06 stsp goto done;
374 a440fac0 2018-09-06 stsp }
375 a440fac0 2018-09-06 stsp *p = '\0';
376 a440fac0 2018-09-06 stsp slen = strlen(s);
377 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
378 a440fac0 2018-09-06 stsp if (err)
379 a440fac0 2018-09-06 stsp goto done;
380 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
381 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
382 a440fac0 2018-09-06 stsp err = got_error_from_errno();
383 a440fac0 2018-09-06 stsp goto done;
384 a440fac0 2018-09-06 stsp }
385 a440fac0 2018-09-06 stsp s += slen + 1;
386 a440fac0 2018-09-06 stsp remain -= slen + 1;
387 a440fac0 2018-09-06 stsp }
388 a440fac0 2018-09-06 stsp
389 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
390 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
391 a440fac0 2018-09-06 stsp char *p;
392 a440fac0 2018-09-06 stsp size_t slen;
393 a440fac0 2018-09-06 stsp
394 a440fac0 2018-09-06 stsp remain -= tlen;
395 a440fac0 2018-09-06 stsp if (remain <= 0) {
396 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
397 a440fac0 2018-09-06 stsp goto done;
398 a440fac0 2018-09-06 stsp }
399 a440fac0 2018-09-06 stsp s += tlen;
400 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
401 a440fac0 2018-09-06 stsp if (p == NULL) {
402 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 a440fac0 2018-09-06 stsp goto done;
404 a440fac0 2018-09-06 stsp }
405 a440fac0 2018-09-06 stsp *p = '\0';
406 a440fac0 2018-09-06 stsp slen = strlen(s);
407 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
408 a440fac0 2018-09-06 stsp if (err)
409 a440fac0 2018-09-06 stsp goto done;
410 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
411 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
412 a440fac0 2018-09-06 stsp err = got_error_from_errno();
413 a440fac0 2018-09-06 stsp goto done;
414 a440fac0 2018-09-06 stsp }
415 a440fac0 2018-09-06 stsp s += slen + 1;
416 a440fac0 2018-09-06 stsp remain -= slen + 1;
417 a440fac0 2018-09-06 stsp }
418 a440fac0 2018-09-06 stsp
419 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
420 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
421 a440fac0 2018-09-06 stsp err = got_error_from_errno();
422 a440fac0 2018-09-06 stsp goto done;
423 a440fac0 2018-09-06 stsp }
424 a440fac0 2018-09-06 stsp done:
425 a440fac0 2018-09-06 stsp if (err) {
426 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
427 a440fac0 2018-09-06 stsp *commit = NULL;
428 a440fac0 2018-09-06 stsp }
429 a440fac0 2018-09-06 stsp return err;
430 a440fac0 2018-09-06 stsp }
431 a440fac0 2018-09-06 stsp
432 ad242220 2018-09-08 stsp void
433 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
434 a440fac0 2018-09-06 stsp {
435 a440fac0 2018-09-06 stsp free(te->id);
436 a440fac0 2018-09-06 stsp free(te->name);
437 a440fac0 2018-09-06 stsp free(te);
438 a440fac0 2018-09-06 stsp }
439 a440fac0 2018-09-06 stsp
440 03fa71c8 2018-09-06 stsp void
441 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
442 03fa71c8 2018-09-06 stsp {
443 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
444 03fa71c8 2018-09-06 stsp
445 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
446 03fa71c8 2018-09-06 stsp tree->refcnt--;
447 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
448 03fa71c8 2018-09-06 stsp return;
449 03fa71c8 2018-09-06 stsp }
450 03fa71c8 2018-09-06 stsp
451 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
452 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
453 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
454 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
455 03fa71c8 2018-09-06 stsp }
456 03fa71c8 2018-09-06 stsp
457 03fa71c8 2018-09-06 stsp free(tree);
458 03fa71c8 2018-09-06 stsp }
459 03fa71c8 2018-09-06 stsp
460 a440fac0 2018-09-06 stsp struct got_tree_entry *
461 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
462 a440fac0 2018-09-06 stsp {
463 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
464 a440fac0 2018-09-06 stsp
465 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
466 a440fac0 2018-09-06 stsp if (te == NULL)
467 a440fac0 2018-09-06 stsp return NULL;
468 a440fac0 2018-09-06 stsp
469 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
470 a440fac0 2018-09-06 stsp if (te->id == NULL) {
471 a440fac0 2018-09-06 stsp free(te);
472 a440fac0 2018-09-06 stsp te = NULL;
473 a440fac0 2018-09-06 stsp }
474 a440fac0 2018-09-06 stsp return te;
475 a440fac0 2018-09-06 stsp }
476 a440fac0 2018-09-06 stsp
477 a440fac0 2018-09-06 stsp static const struct got_error *
478 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
479 a440fac0 2018-09-06 stsp size_t maxlen)
480 a440fac0 2018-09-06 stsp {
481 a440fac0 2018-09-06 stsp char *p = buf, *space;
482 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
483 a440fac0 2018-09-06 stsp
484 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
485 a440fac0 2018-09-06 stsp if (*te == NULL)
486 a440fac0 2018-09-06 stsp return got_error_from_errno();
487 a440fac0 2018-09-06 stsp
488 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
489 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
490 a440fac0 2018-09-06 stsp free(*te);
491 a440fac0 2018-09-06 stsp *te = NULL;
492 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
493 a440fac0 2018-09-06 stsp }
494 a440fac0 2018-09-06 stsp
495 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
496 a440fac0 2018-09-06 stsp if (space == NULL) {
497 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 a440fac0 2018-09-06 stsp free(*te);
499 a440fac0 2018-09-06 stsp *te = NULL;
500 a440fac0 2018-09-06 stsp return err;
501 a440fac0 2018-09-06 stsp }
502 a440fac0 2018-09-06 stsp while (*p != ' ') {
503 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
504 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
505 a440fac0 2018-09-06 stsp goto done;
506 a440fac0 2018-09-06 stsp }
507 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
508 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
509 a440fac0 2018-09-06 stsp p++;
510 a440fac0 2018-09-06 stsp }
511 a440fac0 2018-09-06 stsp
512 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
513 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
514 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 a440fac0 2018-09-06 stsp goto done;
516 a440fac0 2018-09-06 stsp }
517 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
518 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
519 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
520 a440fac0 2018-09-06 stsp done:
521 a440fac0 2018-09-06 stsp if (err) {
522 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
523 a440fac0 2018-09-06 stsp *te = NULL;
524 a440fac0 2018-09-06 stsp }
525 a440fac0 2018-09-06 stsp return err;
526 a440fac0 2018-09-06 stsp }
527 a440fac0 2018-09-06 stsp
528 a440fac0 2018-09-06 stsp const struct got_error *
529 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
530 a440fac0 2018-09-06 stsp {
531 a440fac0 2018-09-06 stsp const struct got_error *err;
532 a440fac0 2018-09-06 stsp size_t remain = len;
533 a440fac0 2018-09-06 stsp
534 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
535 a440fac0 2018-09-06 stsp if (*tree == NULL)
536 a440fac0 2018-09-06 stsp return got_error_from_errno();
537 a440fac0 2018-09-06 stsp
538 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
539 a440fac0 2018-09-06 stsp
540 a440fac0 2018-09-06 stsp while (remain > 0) {
541 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
542 a440fac0 2018-09-06 stsp size_t elen;
543 a440fac0 2018-09-06 stsp
544 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
545 a440fac0 2018-09-06 stsp if (err)
546 a440fac0 2018-09-06 stsp return err;
547 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
548 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
549 a440fac0 2018-09-06 stsp buf += elen;
550 a440fac0 2018-09-06 stsp remain -= elen;
551 a440fac0 2018-09-06 stsp }
552 a440fac0 2018-09-06 stsp
553 a440fac0 2018-09-06 stsp if (remain != 0) {
554 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
555 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
556 a440fac0 2018-09-06 stsp }
557 a440fac0 2018-09-06 stsp
558 a440fac0 2018-09-06 stsp return NULL;
559 a440fac0 2018-09-06 stsp }
560 a440fac0 2018-09-06 stsp
561 ad242220 2018-09-08 stsp const struct got_error *
562 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
563 a440fac0 2018-09-06 stsp {
564 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
565 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
566 a440fac0 2018-09-06 stsp size_t n, total, remain;
567 a440fac0 2018-09-06 stsp uint8_t *buf;
568 a440fac0 2018-09-06 stsp
569 a440fac0 2018-09-06 stsp *outbuf = NULL;
570 a440fac0 2018-09-06 stsp *outlen = 0;
571 a440fac0 2018-09-06 stsp
572 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
573 a440fac0 2018-09-06 stsp if (buf == NULL)
574 a440fac0 2018-09-06 stsp return got_error_from_errno();
575 a440fac0 2018-09-06 stsp
576 a440fac0 2018-09-06 stsp remain = blocksize;
577 a440fac0 2018-09-06 stsp total = 0;
578 a440fac0 2018-09-06 stsp while (1) {
579 a440fac0 2018-09-06 stsp if (remain == 0) {
580 a440fac0 2018-09-06 stsp uint8_t *newbuf;
581 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
582 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
583 a440fac0 2018-09-06 stsp err = got_error_from_errno();
584 a440fac0 2018-09-06 stsp goto done;
585 a440fac0 2018-09-06 stsp }
586 a440fac0 2018-09-06 stsp buf = newbuf;
587 a440fac0 2018-09-06 stsp remain += blocksize;
588 a440fac0 2018-09-06 stsp }
589 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
590 a440fac0 2018-09-06 stsp if (n == 0) {
591 a440fac0 2018-09-06 stsp if (ferror(f)) {
592 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
593 a440fac0 2018-09-06 stsp goto done;
594 a440fac0 2018-09-06 stsp }
595 a440fac0 2018-09-06 stsp break; /* EOF */
596 a440fac0 2018-09-06 stsp }
597 a440fac0 2018-09-06 stsp remain -= n;
598 a440fac0 2018-09-06 stsp total += n;
599 a440fac0 2018-09-06 stsp };
600 a440fac0 2018-09-06 stsp
601 a440fac0 2018-09-06 stsp done:
602 a440fac0 2018-09-06 stsp if (err == NULL) {
603 a440fac0 2018-09-06 stsp *outbuf = buf;
604 a440fac0 2018-09-06 stsp *outlen = total;
605 a440fac0 2018-09-06 stsp } else
606 a440fac0 2018-09-06 stsp free(buf);
607 a440fac0 2018-09-06 stsp return err;
608 a440fac0 2018-09-06 stsp }
609 a440fac0 2018-09-06 stsp
610 a440fac0 2018-09-06 stsp static const struct got_error *
611 ad242220 2018-09-08 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
612 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
613 a440fac0 2018-09-06 stsp {
614 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
615 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
616 a440fac0 2018-09-06 stsp
617 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
618 a440fac0 2018-09-06 stsp
619 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd,obj);
620 3516b818 2018-09-08 stsp if (err)
621 3516b818 2018-09-08 stsp return err;
622 a440fac0 2018-09-06 stsp
623 3516b818 2018-09-08 stsp return got_privsep_recv_commit(commit, ibuf);
624 a440fac0 2018-09-06 stsp }
625 a440fac0 2018-09-06 stsp
626 a440fac0 2018-09-06 stsp const struct got_error *
627 a440fac0 2018-09-06 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
628 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
629 a440fac0 2018-09-06 stsp {
630 a440fac0 2018-09-06 stsp int imsg_fds[2];
631 a440fac0 2018-09-06 stsp pid_t pid;
632 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
633 a440fac0 2018-09-06 stsp
634 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
635 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
636 ad242220 2018-09-08 stsp
637 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
638 3516b818 2018-09-08 stsp if (ibuf == NULL)
639 3516b818 2018-09-08 stsp return got_error_from_errno();
640 3516b818 2018-09-08 stsp
641 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
642 a440fac0 2018-09-06 stsp return got_error_from_errno();
643 a440fac0 2018-09-06 stsp
644 a440fac0 2018-09-06 stsp pid = fork();
645 a440fac0 2018-09-06 stsp if (pid == -1)
646 a440fac0 2018-09-06 stsp return got_error_from_errno();
647 a440fac0 2018-09-06 stsp else if (pid == 0) {
648 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT);
649 a440fac0 2018-09-06 stsp /* not reached */
650 a440fac0 2018-09-06 stsp }
651 a440fac0 2018-09-06 stsp
652 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
653 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
654 ad242220 2018-09-08 stsp imsg_fds[0];
655 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
656 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
657 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
658 ad242220 2018-09-08 stsp
659 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
660 a440fac0 2018-09-06 stsp }
661 a440fac0 2018-09-06 stsp
662 a440fac0 2018-09-06 stsp static const struct got_error *
663 ad242220 2018-09-08 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
664 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
665 a440fac0 2018-09-06 stsp {
666 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
667 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
668 a440fac0 2018-09-06 stsp
669 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
670 a440fac0 2018-09-06 stsp
671 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd,obj);
672 a440fac0 2018-09-06 stsp if (err)
673 3516b818 2018-09-08 stsp return err;
674 a440fac0 2018-09-06 stsp
675 3516b818 2018-09-08 stsp return got_privsep_recv_tree(tree, ibuf);
676 a440fac0 2018-09-06 stsp }
677 a440fac0 2018-09-06 stsp
678 a440fac0 2018-09-06 stsp const struct got_error *
679 a440fac0 2018-09-06 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
680 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
681 a440fac0 2018-09-06 stsp {
682 a440fac0 2018-09-06 stsp int imsg_fds[2];
683 a440fac0 2018-09-06 stsp pid_t pid;
684 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
685 a440fac0 2018-09-06 stsp
686 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
687 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
688 ad242220 2018-09-08 stsp
689 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
690 3516b818 2018-09-08 stsp if (ibuf == NULL)
691 3516b818 2018-09-08 stsp return got_error_from_errno();
692 3516b818 2018-09-08 stsp
693 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
694 a440fac0 2018-09-06 stsp return got_error_from_errno();
695 a440fac0 2018-09-06 stsp
696 a440fac0 2018-09-06 stsp pid = fork();
697 a440fac0 2018-09-06 stsp if (pid == -1)
698 a440fac0 2018-09-06 stsp return got_error_from_errno();
699 a440fac0 2018-09-06 stsp else if (pid == 0) {
700 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE);
701 a440fac0 2018-09-06 stsp /* not reached */
702 a440fac0 2018-09-06 stsp }
703 a440fac0 2018-09-06 stsp
704 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
705 3516b818 2018-09-08 stsp
706 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
707 ad242220 2018-09-08 stsp imsg_fds[0];
708 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
709 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
710 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
711 ad242220 2018-09-08 stsp
712 3516b818 2018-09-08 stsp
713 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
714 a440fac0 2018-09-06 stsp }
715 a440fac0 2018-09-06 stsp
716 a440fac0 2018-09-06 stsp static const struct got_error *
717 ad242220 2018-09-08 stsp request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
718 a440fac0 2018-09-06 stsp {
719 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
720 ad242220 2018-09-08 stsp int outfd_child;
721 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
722 a440fac0 2018-09-06 stsp
723 ad242220 2018-09-08 stsp outfd_child = dup(outfd);
724 ad242220 2018-09-08 stsp if (outfd_child == -1)
725 ad242220 2018-09-08 stsp return got_error_from_errno();
726 a440fac0 2018-09-06 stsp
727 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
728 a440fac0 2018-09-06 stsp
729 3516b818 2018-09-08 stsp err = got_privsep_send_blob_req(ibuf, outfd_child, infd);
730 ad242220 2018-09-08 stsp if (err)
731 3516b818 2018-09-08 stsp return err;
732 ad242220 2018-09-08 stsp
733 3516b818 2018-09-08 stsp err = got_privsep_recv_blob(size, ibuf);
734 a440fac0 2018-09-06 stsp if (err)
735 3516b818 2018-09-08 stsp return err;
736 a440fac0 2018-09-06 stsp
737 ad242220 2018-09-08 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
738 3516b818 2018-09-08 stsp return got_error_from_errno();
739 3516b818 2018-09-08 stsp
740 ad242220 2018-09-08 stsp return err;
741 a440fac0 2018-09-06 stsp }
742 a440fac0 2018-09-06 stsp
743 a440fac0 2018-09-06 stsp const struct got_error *
744 ad242220 2018-09-08 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
745 ad242220 2018-09-08 stsp struct got_repository *repo)
746 a440fac0 2018-09-06 stsp {
747 a440fac0 2018-09-06 stsp int imsg_fds[2];
748 a440fac0 2018-09-06 stsp pid_t pid;
749 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
750 a440fac0 2018-09-06 stsp
751 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
752 ad242220 2018-09-08 stsp return request_blob(size, outfd, infd, repo);
753 ad242220 2018-09-08 stsp
754 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
755 3516b818 2018-09-08 stsp if (ibuf == NULL)
756 3516b818 2018-09-08 stsp return got_error_from_errno();
757 3516b818 2018-09-08 stsp
758 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
759 a440fac0 2018-09-06 stsp return got_error_from_errno();
760 a440fac0 2018-09-06 stsp
761 a440fac0 2018-09-06 stsp pid = fork();
762 a440fac0 2018-09-06 stsp if (pid == -1)
763 a440fac0 2018-09-06 stsp return got_error_from_errno();
764 a440fac0 2018-09-06 stsp else if (pid == 0) {
765 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB);
766 a440fac0 2018-09-06 stsp /* not reached */
767 a440fac0 2018-09-06 stsp }
768 a440fac0 2018-09-06 stsp
769 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
770 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
771 ad242220 2018-09-08 stsp imsg_fds[0];
772 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
773 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
774 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
775 ad242220 2018-09-08 stsp
776 ad242220 2018-09-08 stsp return request_blob(size, outfd, infd, repo);
777 a440fac0 2018-09-06 stsp }