2 a440fac0 2018-09-06 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
50 a440fac0 2018-09-06 stsp #ifndef nitems
51 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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"
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 "
64 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
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)
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);
80 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
81 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
86 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
88 03fa71c8 2018-09-06 stsp free(qid->id);
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)
95 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
96 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
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);
101 ad242220 2018-09-08 stsp err = got_privsep_send_obj_req(&ibuf, fd, NULL);
104 ad242220 2018-09-08 stsp err = got_privsep_recv_obj(obj, &ibuf);
106 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
107 a440fac0 2018-09-06 stsp return err;
110 a440fac0 2018-09-06 stsp static void
111 ad242220 2018-09-08 stsp exec_privsep_child(int imsg_fds[2], const char *path)
113 a440fac0 2018-09-06 stsp close(imsg_fds[0]);
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));
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));
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));
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)
137 a440fac0 2018-09-06 stsp int imsg_fds[2];
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);
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();
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 */
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;
159 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
162 a440fac0 2018-09-06 stsp struct got_commit_object *
163 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
165 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
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;
176 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
178 a440fac0 2018-09-06 stsp return commit;
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)
185 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
186 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
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();
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;
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);
203 a440fac0 2018-09-06 stsp return err;
206 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
207 a440fac0 2018-09-06 stsp commit->nparents++;
209 a440fac0 2018-09-06 stsp return NULL;
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)
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;
219 a440fac0 2018-09-06 stsp *gmtoff = 0;
221 a440fac0 2018-09-06 stsp if (*p == '-')
223 a440fac0 2018-09-06 stsp else if (*p != '+')
224 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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'));
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');
235 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
236 a440fac0 2018-09-06 stsp return NULL;
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)
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;
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);
258 a440fac0 2018-09-06 stsp return err;
259 a440fac0 2018-09-06 stsp *space = '\0';
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);
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);
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;
278 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
279 a440fac0 2018-09-06 stsp *space = '\0';
281 a440fac0 2018-09-06 stsp return NULL;
285 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
287 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
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)
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);
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);
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)
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;
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();
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);
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);
332 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
333 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
335 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
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);
347 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
351 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
352 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
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) {
358 a440fac0 2018-09-06 stsp size_t slen;
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);
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);
372 a440fac0 2018-09-06 stsp slen = strlen(s);
373 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
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();
381 a440fac0 2018-09-06 stsp s += slen + 1;
382 a440fac0 2018-09-06 stsp remain -= slen + 1;
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) {
388 a440fac0 2018-09-06 stsp size_t slen;
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);
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);
402 a440fac0 2018-09-06 stsp slen = strlen(s);
403 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
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();
411 a440fac0 2018-09-06 stsp s += slen + 1;
412 a440fac0 2018-09-06 stsp remain -= slen + 1;
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();
422 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
423 a440fac0 2018-09-06 stsp *commit = NULL;
425 a440fac0 2018-09-06 stsp return err;
429 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
431 a440fac0 2018-09-06 stsp free(te->id);
432 a440fac0 2018-09-06 stsp free(te->name);
437 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
439 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
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)
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);
453 03fa71c8 2018-09-06 stsp free(tree);
456 a440fac0 2018-09-06 stsp struct got_tree_entry *
457 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
459 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
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;
465 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
466 a440fac0 2018-09-06 stsp if (te->id == NULL) {
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)
477 a440fac0 2018-09-06 stsp char *p = buf, *space;
478 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
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();
484 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
485 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
487 a440fac0 2018-09-06 stsp *te = NULL;
488 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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);
495 a440fac0 2018-09-06 stsp *te = NULL;
496 a440fac0 2018-09-06 stsp return err;
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);
503 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
504 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
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);
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;
518 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
519 a440fac0 2018-09-06 stsp *te = NULL;
521 a440fac0 2018-09-06 stsp return err;
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)
527 a440fac0 2018-09-06 stsp const struct got_error *err;
528 a440fac0 2018-09-06 stsp size_t remain = len;
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();
534 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
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;
540 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
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;
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);
554 a440fac0 2018-09-06 stsp return NULL;
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)
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;
565 a440fac0 2018-09-06 stsp *outbuf = NULL;
566 a440fac0 2018-09-06 stsp *outlen = 0;
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();
572 a440fac0 2018-09-06 stsp remain = blocksize;
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();
582 a440fac0 2018-09-06 stsp buf = newbuf;
583 a440fac0 2018-09-06 stsp remain += blocksize;
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);
591 a440fac0 2018-09-06 stsp break; /* EOF */
593 a440fac0 2018-09-06 stsp remain -= n;
594 a440fac0 2018-09-06 stsp total += n;
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;
603 a440fac0 2018-09-06 stsp return err;
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)
610 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
611 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
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);
616 ad242220 2018-09-08 stsp err = got_privsep_send_obj_req(&ibuf, fd,obj);
620 ad242220 2018-09-08 stsp err = got_privsep_recv_commit(commit, &ibuf);
622 a440fac0 2018-09-06 stsp imsg_clear(&ibuf);
623 ad242220 2018-09-08 stsp return err;
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)
630 a440fac0 2018-09-06 stsp int imsg_fds[2];
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);
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();
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 */
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;
652 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
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)
659 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
660 a440fac0 2018-09-06 stsp struct imsgbuf ibuf;
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);
665 ad242220 2018-09-08 stsp err = got_privsep_send_obj_req(&ibuf, fd,obj);
669 ad242220 2018-09-08 stsp err = got_privsep_recv_tree(tree, &ibuf);
671 a440fac0 2018-09-06 stsp imsg_clear(&ibuf);
672 ad242220 2018-09-08 stsp return err;
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)
679 a440fac0 2018-09-06 stsp int imsg_fds[2];
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);
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();
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 */
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;
701 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
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)
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;
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();
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);
718 ad242220 2018-09-08 stsp err = got_privsep_send_blob_req(&ibuf, outfd_child, infd);
722 ad242220 2018-09-08 stsp err = got_privsep_recv_blob(size, &ibuf);
726 ad242220 2018-09-08 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
727 ad242220 2018-09-08 stsp err = got_error_from_errno();
729 a440fac0 2018-09-06 stsp imsg_clear(&ibuf);
730 ad242220 2018-09-08 stsp return err;
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)
737 a440fac0 2018-09-06 stsp int imsg_fds[2];
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);
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();
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 */
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;
759 ad242220 2018-09-08 stsp return request_blob(size, outfd, infd, repo);