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 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
97 a440fac0 2018-09-06 stsp
98 ad242220 2018-09-08 stsp imsg_init(&ibuf,
99 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd);
100 a440fac0 2018-09-06 stsp
101 ad242220 2018-09-08 stsp err = got_privsep_send_obj_req(&ibuf, fd, NULL);
102 a440fac0 2018-09-06 stsp if (err)
103 ad242220 2018-09-08 stsp goto done;
104 ad242220 2018-09-08 stsp err = got_privsep_recv_obj(obj, &ibuf);
105 a440fac0 2018-09-06 stsp done:
106 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
107 a440fac0 2018-09-06 stsp return err;
108 a440fac0 2018-09-06 stsp }
109 a440fac0 2018-09-06 stsp
110 a440fac0 2018-09-06 stsp static void
111 ad242220 2018-09-08 stsp exec_privsep_child(int imsg_fds[2], const char *path)
112 a440fac0 2018-09-06 stsp {
113 a440fac0 2018-09-06 stsp close(imsg_fds[0]);
114 a440fac0 2018-09-06 stsp
115 ad242220 2018-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
116 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
117 ad242220 2018-09-08 stsp strerror(errno));
118 ad242220 2018-09-08 stsp _exit(1);
119 a440fac0 2018-09-06 stsp }
120 ad242220 2018-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
121 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
122 ad242220 2018-09-08 stsp strerror(errno));
123 ad242220 2018-09-08 stsp _exit(1);
124 ad242220 2018-09-08 stsp }
125 a440fac0 2018-09-06 stsp
126 ad242220 2018-09-08 stsp if (execl(path, path, (char *)NULL) == -1) {
127 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
128 ad242220 2018-09-08 stsp strerror(errno));
129 ad242220 2018-09-08 stsp _exit(1);
130 a440fac0 2018-09-06 stsp }
131 a440fac0 2018-09-06 stsp }
132 a440fac0 2018-09-06 stsp
133 a440fac0 2018-09-06 stsp const struct got_error *
134 ad242220 2018-09-08 stsp got_object_read_header_privsep(struct got_object **obj,
135 ad242220 2018-09-08 stsp struct got_repository *repo, int obj_fd)
136 a440fac0 2018-09-06 stsp {
137 a440fac0 2018-09-06 stsp int imsg_fds[2];
138 a440fac0 2018-09-06 stsp pid_t pid;
139 a440fac0 2018-09-06 stsp
140 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
141 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
142 ad242220 2018-09-08 stsp
143 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
144 a440fac0 2018-09-06 stsp return got_error_from_errno();
145 a440fac0 2018-09-06 stsp
146 a440fac0 2018-09-06 stsp pid = fork();
147 a440fac0 2018-09-06 stsp if (pid == -1)
148 a440fac0 2018-09-06 stsp return got_error_from_errno();
149 a440fac0 2018-09-06 stsp else if (pid == 0) {
150 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT);
151 a440fac0 2018-09-06 stsp /* not reached */
152 a440fac0 2018-09-06 stsp }
153 a440fac0 2018-09-06 stsp
154 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
155 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
156 ad242220 2018-09-08 stsp imsg_fds[0];
157 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
158 ad242220 2018-09-08 stsp
159 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
160 a440fac0 2018-09-06 stsp }
161 a440fac0 2018-09-06 stsp
162 a440fac0 2018-09-06 stsp struct got_commit_object *
163 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
164 a440fac0 2018-09-06 stsp {
165 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
166 a440fac0 2018-09-06 stsp
167 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
168 a440fac0 2018-09-06 stsp if (commit == NULL)
169 a440fac0 2018-09-06 stsp return NULL;
170 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
171 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
172 a440fac0 2018-09-06 stsp free(commit);
173 a440fac0 2018-09-06 stsp return NULL;
174 a440fac0 2018-09-06 stsp }
175 a440fac0 2018-09-06 stsp
176 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
177 a440fac0 2018-09-06 stsp
178 a440fac0 2018-09-06 stsp return commit;
179 a440fac0 2018-09-06 stsp }
180 a440fac0 2018-09-06 stsp
181 a440fac0 2018-09-06 stsp const struct got_error *
182 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
183 a440fac0 2018-09-06 stsp const char *id_str)
184 a440fac0 2018-09-06 stsp {
185 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
186 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
187 a440fac0 2018-09-06 stsp
188 a440fac0 2018-09-06 stsp qid = malloc(sizeof(*qid));
189 a440fac0 2018-09-06 stsp if (qid == NULL)
190 a440fac0 2018-09-06 stsp return got_error_from_errno();
191 a440fac0 2018-09-06 stsp
192 a440fac0 2018-09-06 stsp qid->id = malloc(sizeof(*qid->id));
193 a440fac0 2018-09-06 stsp if (qid->id == NULL) {
194 a440fac0 2018-09-06 stsp err = got_error_from_errno();
195 a440fac0 2018-09-06 stsp got_object_qid_free(qid);
196 a440fac0 2018-09-06 stsp return err;
197 a440fac0 2018-09-06 stsp }
198 a440fac0 2018-09-06 stsp
199 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
200 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
201 a440fac0 2018-09-06 stsp free(qid->id);
202 a440fac0 2018-09-06 stsp free(qid);
203 a440fac0 2018-09-06 stsp return err;
204 a440fac0 2018-09-06 stsp }
205 a440fac0 2018-09-06 stsp
206 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
207 a440fac0 2018-09-06 stsp commit->nparents++;
208 a440fac0 2018-09-06 stsp
209 a440fac0 2018-09-06 stsp return NULL;
210 a440fac0 2018-09-06 stsp }
211 a440fac0 2018-09-06 stsp
212 a440fac0 2018-09-06 stsp static const struct got_error *
213 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
214 a440fac0 2018-09-06 stsp {
215 a440fac0 2018-09-06 stsp int sign = 1;
216 a440fac0 2018-09-06 stsp const char *p = tzstr;
217 a440fac0 2018-09-06 stsp time_t h, m;
218 a440fac0 2018-09-06 stsp
219 a440fac0 2018-09-06 stsp *gmtoff = 0;
220 a440fac0 2018-09-06 stsp
221 a440fac0 2018-09-06 stsp if (*p == '-')
222 a440fac0 2018-09-06 stsp sign = -1;
223 a440fac0 2018-09-06 stsp else if (*p != '+')
224 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
225 a440fac0 2018-09-06 stsp p++;
226 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
227 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
228 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
229 a440fac0 2018-09-06 stsp
230 a440fac0 2018-09-06 stsp p += 2;
231 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
232 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
233 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
234 a440fac0 2018-09-06 stsp
235 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
236 a440fac0 2018-09-06 stsp return NULL;
237 a440fac0 2018-09-06 stsp }
238 a440fac0 2018-09-06 stsp
239 a440fac0 2018-09-06 stsp static const struct got_error *
240 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
241 a440fac0 2018-09-06 stsp {
242 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
243 a440fac0 2018-09-06 stsp const char *errstr;
244 a440fac0 2018-09-06 stsp char *space, *tzstr;
245 a440fac0 2018-09-06 stsp time_t gmtoff;
246 a440fac0 2018-09-06 stsp time_t time;
247 a440fac0 2018-09-06 stsp
248 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
249 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
250 a440fac0 2018-09-06 stsp if (space == NULL)
251 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
252 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
253 a440fac0 2018-09-06 stsp if (tzstr == NULL)
254 a440fac0 2018-09-06 stsp return got_error_from_errno();
255 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
256 a440fac0 2018-09-06 stsp free(tzstr);
257 a440fac0 2018-09-06 stsp if (err)
258 a440fac0 2018-09-06 stsp return err;
259 a440fac0 2018-09-06 stsp *space = '\0';
260 a440fac0 2018-09-06 stsp
261 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
262 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
263 a440fac0 2018-09-06 stsp if (space == NULL)
264 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
265 a440fac0 2018-09-06 stsp
266 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
267 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
268 a440fac0 2018-09-06 stsp if (errstr)
269 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
270 a440fac0 2018-09-06 stsp
271 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
272 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
273 a440fac0 2018-09-06 stsp time -= gmtoff;
274 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
275 a440fac0 2018-09-06 stsp return got_error_from_errno();
276 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
277 a440fac0 2018-09-06 stsp
278 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
279 a440fac0 2018-09-06 stsp *space = '\0';
280 a440fac0 2018-09-06 stsp
281 a440fac0 2018-09-06 stsp return NULL;
282 a440fac0 2018-09-06 stsp }
283 a440fac0 2018-09-06 stsp
284 03fa71c8 2018-09-06 stsp void
285 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
286 03fa71c8 2018-09-06 stsp {
287 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
288 03fa71c8 2018-09-06 stsp
289 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
290 03fa71c8 2018-09-06 stsp commit->refcnt--;
291 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
292 03fa71c8 2018-09-06 stsp return;
293 03fa71c8 2018-09-06 stsp }
294 03fa71c8 2018-09-06 stsp
295 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
296 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
297 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
298 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
299 03fa71c8 2018-09-06 stsp }
300 03fa71c8 2018-09-06 stsp
301 03fa71c8 2018-09-06 stsp free(commit->tree_id);
302 03fa71c8 2018-09-06 stsp free(commit->author);
303 03fa71c8 2018-09-06 stsp free(commit->committer);
304 03fa71c8 2018-09-06 stsp free(commit->logmsg);
305 03fa71c8 2018-09-06 stsp free(commit);
306 03fa71c8 2018-09-06 stsp }
307 03fa71c8 2018-09-06 stsp
308 a440fac0 2018-09-06 stsp const struct got_error *
309 a440fac0 2018-09-06 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
310 a440fac0 2018-09-06 stsp {
311 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
312 a440fac0 2018-09-06 stsp char *s = buf;
313 a440fac0 2018-09-06 stsp size_t tlen;
314 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
315 a440fac0 2018-09-06 stsp
316 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
317 a440fac0 2018-09-06 stsp if (*commit == NULL)
318 a440fac0 2018-09-06 stsp return got_error_from_errno();
319 a440fac0 2018-09-06 stsp
320 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
321 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
322 a440fac0 2018-09-06 stsp remain -= tlen;
323 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
324 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
325 a440fac0 2018-09-06 stsp goto done;
326 a440fac0 2018-09-06 stsp }
327 a440fac0 2018-09-06 stsp s += tlen;
328 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
329 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
330 a440fac0 2018-09-06 stsp goto done;
331 a440fac0 2018-09-06 stsp }
332 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
333 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
334 a440fac0 2018-09-06 stsp } else {
335 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 a440fac0 2018-09-06 stsp goto done;
337 a440fac0 2018-09-06 stsp }
338 a440fac0 2018-09-06 stsp
339 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
340 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
341 a440fac0 2018-09-06 stsp remain -= tlen;
342 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
343 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
344 a440fac0 2018-09-06 stsp goto done;
345 a440fac0 2018-09-06 stsp }
346 a440fac0 2018-09-06 stsp s += tlen;
347 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
348 a440fac0 2018-09-06 stsp if (err)
349 a440fac0 2018-09-06 stsp goto done;
350 a440fac0 2018-09-06 stsp
351 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
352 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
353 a440fac0 2018-09-06 stsp }
354 a440fac0 2018-09-06 stsp
355 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
356 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
357 a440fac0 2018-09-06 stsp char *p;
358 a440fac0 2018-09-06 stsp size_t slen;
359 a440fac0 2018-09-06 stsp
360 a440fac0 2018-09-06 stsp remain -= tlen;
361 a440fac0 2018-09-06 stsp if (remain <= 0) {
362 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
363 a440fac0 2018-09-06 stsp goto done;
364 a440fac0 2018-09-06 stsp }
365 a440fac0 2018-09-06 stsp s += tlen;
366 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
367 a440fac0 2018-09-06 stsp if (p == NULL) {
368 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
369 a440fac0 2018-09-06 stsp goto done;
370 a440fac0 2018-09-06 stsp }
371 a440fac0 2018-09-06 stsp *p = '\0';
372 a440fac0 2018-09-06 stsp slen = strlen(s);
373 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
374 a440fac0 2018-09-06 stsp if (err)
375 a440fac0 2018-09-06 stsp goto done;
376 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
377 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
378 a440fac0 2018-09-06 stsp err = got_error_from_errno();
379 a440fac0 2018-09-06 stsp goto done;
380 a440fac0 2018-09-06 stsp }
381 a440fac0 2018-09-06 stsp s += slen + 1;
382 a440fac0 2018-09-06 stsp remain -= slen + 1;
383 a440fac0 2018-09-06 stsp }
384 a440fac0 2018-09-06 stsp
385 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
386 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
387 a440fac0 2018-09-06 stsp char *p;
388 a440fac0 2018-09-06 stsp size_t slen;
389 a440fac0 2018-09-06 stsp
390 a440fac0 2018-09-06 stsp remain -= tlen;
391 a440fac0 2018-09-06 stsp if (remain <= 0) {
392 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
393 a440fac0 2018-09-06 stsp goto done;
394 a440fac0 2018-09-06 stsp }
395 a440fac0 2018-09-06 stsp s += tlen;
396 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
397 a440fac0 2018-09-06 stsp if (p == NULL) {
398 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
399 a440fac0 2018-09-06 stsp goto done;
400 a440fac0 2018-09-06 stsp }
401 a440fac0 2018-09-06 stsp *p = '\0';
402 a440fac0 2018-09-06 stsp slen = strlen(s);
403 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
404 a440fac0 2018-09-06 stsp if (err)
405 a440fac0 2018-09-06 stsp goto done;
406 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
407 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
408 a440fac0 2018-09-06 stsp err = got_error_from_errno();
409 a440fac0 2018-09-06 stsp goto done;
410 a440fac0 2018-09-06 stsp }
411 a440fac0 2018-09-06 stsp s += slen + 1;
412 a440fac0 2018-09-06 stsp remain -= slen + 1;
413 a440fac0 2018-09-06 stsp }
414 a440fac0 2018-09-06 stsp
415 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
416 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
417 a440fac0 2018-09-06 stsp err = got_error_from_errno();
418 a440fac0 2018-09-06 stsp goto done;
419 a440fac0 2018-09-06 stsp }
420 a440fac0 2018-09-06 stsp done:
421 a440fac0 2018-09-06 stsp if (err) {
422 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
423 a440fac0 2018-09-06 stsp *commit = NULL;
424 a440fac0 2018-09-06 stsp }
425 a440fac0 2018-09-06 stsp return err;
426 a440fac0 2018-09-06 stsp }
427 a440fac0 2018-09-06 stsp
428 ad242220 2018-09-08 stsp void
429 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
430 a440fac0 2018-09-06 stsp {
431 a440fac0 2018-09-06 stsp free(te->id);
432 a440fac0 2018-09-06 stsp free(te->name);
433 a440fac0 2018-09-06 stsp free(te);
434 a440fac0 2018-09-06 stsp }
435 a440fac0 2018-09-06 stsp
436 03fa71c8 2018-09-06 stsp void
437 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
438 03fa71c8 2018-09-06 stsp {
439 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
440 03fa71c8 2018-09-06 stsp
441 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
442 03fa71c8 2018-09-06 stsp tree->refcnt--;
443 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
444 03fa71c8 2018-09-06 stsp return;
445 03fa71c8 2018-09-06 stsp }
446 03fa71c8 2018-09-06 stsp
447 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
448 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
449 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
450 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
451 03fa71c8 2018-09-06 stsp }
452 03fa71c8 2018-09-06 stsp
453 03fa71c8 2018-09-06 stsp free(tree);
454 03fa71c8 2018-09-06 stsp }
455 03fa71c8 2018-09-06 stsp
456 a440fac0 2018-09-06 stsp struct got_tree_entry *
457 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
458 a440fac0 2018-09-06 stsp {
459 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
460 a440fac0 2018-09-06 stsp
461 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
462 a440fac0 2018-09-06 stsp if (te == NULL)
463 a440fac0 2018-09-06 stsp return NULL;
464 a440fac0 2018-09-06 stsp
465 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
466 a440fac0 2018-09-06 stsp if (te->id == NULL) {
467 a440fac0 2018-09-06 stsp free(te);
468 a440fac0 2018-09-06 stsp te = NULL;
469 a440fac0 2018-09-06 stsp }
470 a440fac0 2018-09-06 stsp return te;
471 a440fac0 2018-09-06 stsp }
472 a440fac0 2018-09-06 stsp
473 a440fac0 2018-09-06 stsp static const struct got_error *
474 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
475 a440fac0 2018-09-06 stsp size_t maxlen)
476 a440fac0 2018-09-06 stsp {
477 a440fac0 2018-09-06 stsp char *p = buf, *space;
478 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
479 a440fac0 2018-09-06 stsp
480 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
481 a440fac0 2018-09-06 stsp if (*te == NULL)
482 a440fac0 2018-09-06 stsp return got_error_from_errno();
483 a440fac0 2018-09-06 stsp
484 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
485 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
486 a440fac0 2018-09-06 stsp free(*te);
487 a440fac0 2018-09-06 stsp *te = NULL;
488 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
489 a440fac0 2018-09-06 stsp }
490 a440fac0 2018-09-06 stsp
491 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
492 a440fac0 2018-09-06 stsp if (space == NULL) {
493 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 a440fac0 2018-09-06 stsp free(*te);
495 a440fac0 2018-09-06 stsp *te = NULL;
496 a440fac0 2018-09-06 stsp return err;
497 a440fac0 2018-09-06 stsp }
498 a440fac0 2018-09-06 stsp while (*p != ' ') {
499 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
500 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
501 a440fac0 2018-09-06 stsp goto done;
502 a440fac0 2018-09-06 stsp }
503 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
504 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
505 a440fac0 2018-09-06 stsp p++;
506 a440fac0 2018-09-06 stsp }
507 a440fac0 2018-09-06 stsp
508 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
509 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
510 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
511 a440fac0 2018-09-06 stsp goto done;
512 a440fac0 2018-09-06 stsp }
513 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
514 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
515 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
516 a440fac0 2018-09-06 stsp done:
517 a440fac0 2018-09-06 stsp if (err) {
518 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
519 a440fac0 2018-09-06 stsp *te = NULL;
520 a440fac0 2018-09-06 stsp }
521 a440fac0 2018-09-06 stsp return err;
522 a440fac0 2018-09-06 stsp }
523 a440fac0 2018-09-06 stsp
524 a440fac0 2018-09-06 stsp const struct got_error *
525 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
526 a440fac0 2018-09-06 stsp {
527 a440fac0 2018-09-06 stsp const struct got_error *err;
528 a440fac0 2018-09-06 stsp size_t remain = len;
529 a440fac0 2018-09-06 stsp
530 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
531 a440fac0 2018-09-06 stsp if (*tree == NULL)
532 a440fac0 2018-09-06 stsp return got_error_from_errno();
533 a440fac0 2018-09-06 stsp
534 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
535 a440fac0 2018-09-06 stsp
536 a440fac0 2018-09-06 stsp while (remain > 0) {
537 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
538 a440fac0 2018-09-06 stsp size_t elen;
539 a440fac0 2018-09-06 stsp
540 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
541 a440fac0 2018-09-06 stsp if (err)
542 a440fac0 2018-09-06 stsp return err;
543 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
544 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
545 a440fac0 2018-09-06 stsp buf += elen;
546 a440fac0 2018-09-06 stsp remain -= elen;
547 a440fac0 2018-09-06 stsp }
548 a440fac0 2018-09-06 stsp
549 a440fac0 2018-09-06 stsp if (remain != 0) {
550 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
551 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
552 a440fac0 2018-09-06 stsp }
553 a440fac0 2018-09-06 stsp
554 a440fac0 2018-09-06 stsp return NULL;
555 a440fac0 2018-09-06 stsp }
556 a440fac0 2018-09-06 stsp
557 ad242220 2018-09-08 stsp const struct got_error *
558 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
559 a440fac0 2018-09-06 stsp {
560 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
561 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
562 a440fac0 2018-09-06 stsp size_t n, total, remain;
563 a440fac0 2018-09-06 stsp uint8_t *buf;
564 a440fac0 2018-09-06 stsp
565 a440fac0 2018-09-06 stsp *outbuf = NULL;
566 a440fac0 2018-09-06 stsp *outlen = 0;
567 a440fac0 2018-09-06 stsp
568 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
569 a440fac0 2018-09-06 stsp if (buf == NULL)
570 a440fac0 2018-09-06 stsp return got_error_from_errno();
571 a440fac0 2018-09-06 stsp
572 a440fac0 2018-09-06 stsp remain = blocksize;
573 a440fac0 2018-09-06 stsp total = 0;
574 a440fac0 2018-09-06 stsp while (1) {
575 a440fac0 2018-09-06 stsp if (remain == 0) {
576 a440fac0 2018-09-06 stsp uint8_t *newbuf;
577 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
578 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
579 a440fac0 2018-09-06 stsp err = got_error_from_errno();
580 a440fac0 2018-09-06 stsp goto done;
581 a440fac0 2018-09-06 stsp }
582 a440fac0 2018-09-06 stsp buf = newbuf;
583 a440fac0 2018-09-06 stsp remain += blocksize;
584 a440fac0 2018-09-06 stsp }
585 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
586 a440fac0 2018-09-06 stsp if (n == 0) {
587 a440fac0 2018-09-06 stsp if (ferror(f)) {
588 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
589 a440fac0 2018-09-06 stsp goto done;
590 a440fac0 2018-09-06 stsp }
591 a440fac0 2018-09-06 stsp break; /* EOF */
592 a440fac0 2018-09-06 stsp }
593 a440fac0 2018-09-06 stsp remain -= n;
594 a440fac0 2018-09-06 stsp total += n;
595 a440fac0 2018-09-06 stsp };
596 a440fac0 2018-09-06 stsp
597 a440fac0 2018-09-06 stsp done:
598 a440fac0 2018-09-06 stsp if (err == NULL) {
599 a440fac0 2018-09-06 stsp *outbuf = buf;
600 a440fac0 2018-09-06 stsp *outlen = total;
601 a440fac0 2018-09-06 stsp } else
602 a440fac0 2018-09-06 stsp free(buf);
603 a440fac0 2018-09-06 stsp return err;
604 a440fac0 2018-09-06 stsp }
605 a440fac0 2018-09-06 stsp
606 a440fac0 2018-09-06 stsp static const struct got_error *
607 ad242220 2018-09-08 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
608 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
609 a440fac0 2018-09-06 stsp {
610 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
611 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
612 a440fac0 2018-09-06 stsp
613 ad242220 2018-09-08 stsp imsg_init(&ibuf,
614 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd);
615 a440fac0 2018-09-06 stsp
616 ad242220 2018-09-08 stsp err = got_privsep_send_obj_req(&ibuf, fd,obj);
617 a440fac0 2018-09-06 stsp if (err)
618 a440fac0 2018-09-06 stsp goto done;
619 a440fac0 2018-09-06 stsp
620 ad242220 2018-09-08 stsp err = got_privsep_recv_commit(commit, &ibuf);
621 a440fac0 2018-09-06 stsp done:
622 a440fac0 2018-09-06 stsp imsg_clear(&ibuf);
623 ad242220 2018-09-08 stsp return err;
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 a440fac0 2018-09-06 stsp
633 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
634 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
635 ad242220 2018-09-08 stsp
636 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
637 a440fac0 2018-09-06 stsp return got_error_from_errno();
638 a440fac0 2018-09-06 stsp
639 a440fac0 2018-09-06 stsp pid = fork();
640 a440fac0 2018-09-06 stsp if (pid == -1)
641 a440fac0 2018-09-06 stsp return got_error_from_errno();
642 a440fac0 2018-09-06 stsp else if (pid == 0) {
643 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT);
644 a440fac0 2018-09-06 stsp /* not reached */
645 a440fac0 2018-09-06 stsp }
646 a440fac0 2018-09-06 stsp
647 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
648 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
649 ad242220 2018-09-08 stsp imsg_fds[0];
650 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
651 ad242220 2018-09-08 stsp
652 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
653 a440fac0 2018-09-06 stsp }
654 a440fac0 2018-09-06 stsp
655 a440fac0 2018-09-06 stsp static const struct got_error *
656 ad242220 2018-09-08 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
657 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
658 a440fac0 2018-09-06 stsp {
659 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
660 a440fac0 2018-09-06 stsp struct imsgbuf ibuf;
661 a440fac0 2018-09-06 stsp
662 ad242220 2018-09-08 stsp imsg_init(&ibuf,
663 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd);
664 a440fac0 2018-09-06 stsp
665 ad242220 2018-09-08 stsp err = got_privsep_send_obj_req(&ibuf, fd,obj);
666 a440fac0 2018-09-06 stsp if (err)
667 a440fac0 2018-09-06 stsp goto done;
668 a440fac0 2018-09-06 stsp
669 ad242220 2018-09-08 stsp err = got_privsep_recv_tree(tree, &ibuf);
670 a440fac0 2018-09-06 stsp done:
671 a440fac0 2018-09-06 stsp imsg_clear(&ibuf);
672 ad242220 2018-09-08 stsp return err;
673 a440fac0 2018-09-06 stsp }
674 a440fac0 2018-09-06 stsp
675 a440fac0 2018-09-06 stsp const struct got_error *
676 a440fac0 2018-09-06 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
677 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
678 a440fac0 2018-09-06 stsp {
679 a440fac0 2018-09-06 stsp int imsg_fds[2];
680 a440fac0 2018-09-06 stsp pid_t pid;
681 a440fac0 2018-09-06 stsp
682 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
683 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
684 ad242220 2018-09-08 stsp
685 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
686 a440fac0 2018-09-06 stsp return got_error_from_errno();
687 a440fac0 2018-09-06 stsp
688 a440fac0 2018-09-06 stsp pid = fork();
689 a440fac0 2018-09-06 stsp if (pid == -1)
690 a440fac0 2018-09-06 stsp return got_error_from_errno();
691 a440fac0 2018-09-06 stsp else if (pid == 0) {
692 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE);
693 a440fac0 2018-09-06 stsp /* not reached */
694 a440fac0 2018-09-06 stsp }
695 a440fac0 2018-09-06 stsp
696 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
697 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
698 ad242220 2018-09-08 stsp imsg_fds[0];
699 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
700 ad242220 2018-09-08 stsp
701 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
702 a440fac0 2018-09-06 stsp }
703 a440fac0 2018-09-06 stsp
704 a440fac0 2018-09-06 stsp static const struct got_error *
705 ad242220 2018-09-08 stsp request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
706 a440fac0 2018-09-06 stsp {
707 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
708 a440fac0 2018-09-06 stsp struct imsgbuf ibuf;
709 ad242220 2018-09-08 stsp int outfd_child;
710 a440fac0 2018-09-06 stsp
711 ad242220 2018-09-08 stsp outfd_child = dup(outfd);
712 ad242220 2018-09-08 stsp if (outfd_child == -1)
713 ad242220 2018-09-08 stsp return got_error_from_errno();
714 a440fac0 2018-09-06 stsp
715 ad242220 2018-09-08 stsp imsg_init(&ibuf,
716 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd);
717 a440fac0 2018-09-06 stsp
718 ad242220 2018-09-08 stsp err = got_privsep_send_blob_req(&ibuf, outfd_child, infd);
719 ad242220 2018-09-08 stsp if (err)
720 a440fac0 2018-09-06 stsp goto done;
721 ad242220 2018-09-08 stsp
722 ad242220 2018-09-08 stsp err = got_privsep_recv_blob(size, &ibuf);
723 a440fac0 2018-09-06 stsp if (err)
724 a440fac0 2018-09-06 stsp goto done;
725 a440fac0 2018-09-06 stsp
726 ad242220 2018-09-08 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
727 ad242220 2018-09-08 stsp err = got_error_from_errno();
728 a440fac0 2018-09-06 stsp done:
729 a440fac0 2018-09-06 stsp imsg_clear(&ibuf);
730 ad242220 2018-09-08 stsp return err;
731 a440fac0 2018-09-06 stsp }
732 a440fac0 2018-09-06 stsp
733 a440fac0 2018-09-06 stsp const struct got_error *
734 ad242220 2018-09-08 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
735 ad242220 2018-09-08 stsp struct got_repository *repo)
736 a440fac0 2018-09-06 stsp {
737 a440fac0 2018-09-06 stsp int imsg_fds[2];
738 a440fac0 2018-09-06 stsp pid_t pid;
739 a440fac0 2018-09-06 stsp
740 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
741 ad242220 2018-09-08 stsp return request_blob(size, outfd, infd, repo);
742 ad242220 2018-09-08 stsp
743 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
744 a440fac0 2018-09-06 stsp return got_error_from_errno();
745 a440fac0 2018-09-06 stsp
746 a440fac0 2018-09-06 stsp pid = fork();
747 a440fac0 2018-09-06 stsp if (pid == -1)
748 a440fac0 2018-09-06 stsp return got_error_from_errno();
749 a440fac0 2018-09-06 stsp else if (pid == 0) {
750 ad242220 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB);
751 a440fac0 2018-09-06 stsp /* not reached */
752 a440fac0 2018-09-06 stsp }
753 a440fac0 2018-09-06 stsp
754 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
755 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
756 ad242220 2018-09-08 stsp imsg_fds[0];
757 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
758 ad242220 2018-09-08 stsp
759 ad242220 2018-09-08 stsp return request_blob(size, outfd, infd, repo);
760 a440fac0 2018-09-06 stsp }