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 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
98 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
100 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
102 3516b818 2018-09-08 stsp return err;
104 3516b818 2018-09-08 stsp return got_privsep_recv_obj(obj, ibuf);
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)
110 a440fac0 2018-09-06 stsp close(imsg_fds[0]);
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));
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));
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));
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)
134 a440fac0 2018-09-06 stsp int imsg_fds[2];
136 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
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);
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();
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();
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 */
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;
164 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
167 a440fac0 2018-09-06 stsp struct got_commit_object *
168 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
170 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
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;
181 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
183 a440fac0 2018-09-06 stsp return commit;
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)
190 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
191 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
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();
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;
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);
208 a440fac0 2018-09-06 stsp return err;
211 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
212 a440fac0 2018-09-06 stsp commit->nparents++;
214 a440fac0 2018-09-06 stsp return NULL;
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)
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;
224 a440fac0 2018-09-06 stsp *gmtoff = 0;
226 a440fac0 2018-09-06 stsp if (*p == '-')
228 a440fac0 2018-09-06 stsp else if (*p != '+')
229 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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'));
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');
240 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
241 a440fac0 2018-09-06 stsp return NULL;
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)
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;
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);
263 a440fac0 2018-09-06 stsp return err;
264 a440fac0 2018-09-06 stsp *space = '\0';
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);
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);
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;
283 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
284 a440fac0 2018-09-06 stsp *space = '\0';
286 a440fac0 2018-09-06 stsp return NULL;
290 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
292 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
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)
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);
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);
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)
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;
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();
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);
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);
337 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
338 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
340 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
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);
352 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
356 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
357 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
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) {
363 a440fac0 2018-09-06 stsp size_t slen;
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);
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);
377 a440fac0 2018-09-06 stsp slen = strlen(s);
378 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
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();
386 a440fac0 2018-09-06 stsp s += slen + 1;
387 a440fac0 2018-09-06 stsp remain -= slen + 1;
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) {
393 a440fac0 2018-09-06 stsp size_t slen;
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);
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);
407 a440fac0 2018-09-06 stsp slen = strlen(s);
408 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
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();
416 a440fac0 2018-09-06 stsp s += slen + 1;
417 a440fac0 2018-09-06 stsp remain -= slen + 1;
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();
427 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
428 a440fac0 2018-09-06 stsp *commit = NULL;
430 a440fac0 2018-09-06 stsp return err;
434 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
436 a440fac0 2018-09-06 stsp free(te->id);
437 a440fac0 2018-09-06 stsp free(te->name);
442 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
444 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
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)
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);
458 03fa71c8 2018-09-06 stsp free(tree);
461 a440fac0 2018-09-06 stsp struct got_tree_entry *
462 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
464 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
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;
470 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
471 a440fac0 2018-09-06 stsp if (te->id == NULL) {
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)
482 a440fac0 2018-09-06 stsp char *p = buf, *space;
483 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
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();
489 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
490 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
492 a440fac0 2018-09-06 stsp *te = NULL;
493 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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);
500 a440fac0 2018-09-06 stsp *te = NULL;
501 a440fac0 2018-09-06 stsp return err;
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);
508 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
509 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
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);
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;
523 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
524 a440fac0 2018-09-06 stsp *te = NULL;
526 a440fac0 2018-09-06 stsp return err;
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)
532 a440fac0 2018-09-06 stsp const struct got_error *err;
533 a440fac0 2018-09-06 stsp size_t remain = len;
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();
539 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
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;
545 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
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;
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);
559 a440fac0 2018-09-06 stsp return NULL;
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)
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;
570 a440fac0 2018-09-06 stsp *outbuf = NULL;
571 a440fac0 2018-09-06 stsp *outlen = 0;
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();
577 a440fac0 2018-09-06 stsp remain = blocksize;
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();
587 a440fac0 2018-09-06 stsp buf = newbuf;
588 a440fac0 2018-09-06 stsp remain += blocksize;
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);
596 a440fac0 2018-09-06 stsp break; /* EOF */
598 a440fac0 2018-09-06 stsp remain -= n;
599 a440fac0 2018-09-06 stsp total += n;
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;
608 a440fac0 2018-09-06 stsp return err;
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)
615 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
616 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
618 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
620 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd,obj);
622 3516b818 2018-09-08 stsp return err;
624 3516b818 2018-09-08 stsp return got_privsep_recv_commit(commit, ibuf);
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)
631 a440fac0 2018-09-06 stsp int imsg_fds[2];
633 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
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);
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();
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();
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 */
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;
661 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
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)
668 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
669 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
671 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
673 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd,obj);
675 3516b818 2018-09-08 stsp return err;
677 3516b818 2018-09-08 stsp return got_privsep_recv_tree(tree, ibuf);
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)
684 a440fac0 2018-09-06 stsp int imsg_fds[2];
686 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
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);
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();
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();
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 */
707 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
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;
716 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
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)
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;
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();
730 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
732 3516b818 2018-09-08 stsp err = got_privsep_send_blob_req(ibuf, outfd_child, infd);
734 3516b818 2018-09-08 stsp return err;
736 3516b818 2018-09-08 stsp err = got_privsep_recv_blob(size, ibuf);
738 3516b818 2018-09-08 stsp return err;
740 ad242220 2018-09-08 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
741 3516b818 2018-09-08 stsp return got_error_from_errno();
743 ad242220 2018-09-08 stsp return err;
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)
750 a440fac0 2018-09-06 stsp int imsg_fds[2];
752 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
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);
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();
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();
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 */
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;
780 ad242220 2018-09-08 stsp return request_blob(size, outfd, infd, repo);