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