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 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
25 a440fac0 2018-09-06 stsp #include <errno.h>
26 a440fac0 2018-09-06 stsp #include <stdio.h>
27 a440fac0 2018-09-06 stsp #include <stdlib.h>
28 a440fac0 2018-09-06 stsp #include <string.h>
29 a440fac0 2018-09-06 stsp #include <stdint.h>
30 a440fac0 2018-09-06 stsp #include <sha1.h>
31 a440fac0 2018-09-06 stsp #include <zlib.h>
32 a440fac0 2018-09-06 stsp #include <ctype.h>
33 a440fac0 2018-09-06 stsp #include <limits.h>
34 a440fac0 2018-09-06 stsp #include <imsg.h>
35 a440fac0 2018-09-06 stsp #include <time.h>
36 ad242220 2018-09-08 stsp #include <unistd.h>
38 a440fac0 2018-09-06 stsp #include "got_error.h"
39 a440fac0 2018-09-06 stsp #include "got_object.h"
40 a440fac0 2018-09-06 stsp #include "got_repository.h"
41 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
43 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
45 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_pack.h"
47 a440fac0 2018-09-06 stsp #include "got_lib_inflate.h"
48 a440fac0 2018-09-06 stsp #include "got_lib_object.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
50 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
52 a440fac0 2018-09-06 stsp #ifndef nitems
53 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_TREE "tree"
58 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
60 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
61 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
62 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
63 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
66 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
68 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
69 03fa71c8 2018-09-06 stsp obj->refcnt--;
70 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
74 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
75 03fa71c8 2018-09-06 stsp struct got_delta *delta;
76 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
77 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
78 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
79 03fa71c8 2018-09-06 stsp got_delta_close(delta);
82 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
83 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
88 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
90 03fa71c8 2018-09-06 stsp free(qid->id);
94 a440fac0 2018-09-06 stsp static const struct got_error *
95 ad242220 2018-09-08 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
97 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
98 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
100 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
102 3516b818 2018-09-08 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
104 3516b818 2018-09-08 stsp return err;
106 3516b818 2018-09-08 stsp return got_privsep_recv_obj(obj, ibuf);
109 a440fac0 2018-09-06 stsp static void
110 3cab8b4d 2018-09-08 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
112 a440fac0 2018-09-06 stsp close(imsg_fds[0]);
114 ad242220 2018-09-08 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
115 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
116 ad242220 2018-09-08 stsp strerror(errno));
119 ad242220 2018-09-08 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
120 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(),
121 ad242220 2018-09-08 stsp strerror(errno));
125 3cab8b4d 2018-09-08 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
126 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
127 ad242220 2018-09-08 stsp strerror(errno));
132 a440fac0 2018-09-06 stsp const struct got_error *
133 ad242220 2018-09-08 stsp got_object_read_header_privsep(struct got_object **obj,
134 ad242220 2018-09-08 stsp struct got_repository *repo, int obj_fd)
136 a440fac0 2018-09-06 stsp int imsg_fds[2];
138 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
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 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
144 3516b818 2018-09-08 stsp if (ibuf == NULL)
145 3516b818 2018-09-08 stsp return got_error_from_errno();
147 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
148 a440fac0 2018-09-06 stsp return got_error_from_errno();
150 a440fac0 2018-09-06 stsp pid = fork();
151 a440fac0 2018-09-06 stsp if (pid == -1)
152 a440fac0 2018-09-06 stsp return got_error_from_errno();
153 a440fac0 2018-09-06 stsp else if (pid == 0) {
154 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
155 3cab8b4d 2018-09-08 stsp repo->path);
156 a440fac0 2018-09-06 stsp /* not reached */
159 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
160 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
161 ad242220 2018-09-08 stsp imsg_fds[0];
162 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
163 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
164 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
166 ad242220 2018-09-08 stsp return request_object(obj, repo, obj_fd);
169 876c234b 2018-09-10 stsp static const struct got_error *
170 876c234b 2018-09-10 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
171 876c234b 2018-09-10 stsp struct got_object_id *id)
173 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
174 876c234b 2018-09-10 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
176 876c234b 2018-09-10 stsp err = got_privsep_send_packed_obj_req(ibuf, idx);
178 876c234b 2018-09-10 stsp return err;
180 876c234b 2018-09-10 stsp err = got_privsep_recv_obj(obj, ibuf);
182 876c234b 2018-09-10 stsp return err;
184 876c234b 2018-09-10 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
185 876c234b 2018-09-10 stsp if ((*obj)->path_packfile == NULL) {
186 876c234b 2018-09-10 stsp err = got_error_from_errno();
187 876c234b 2018-09-10 stsp return err;
189 876c234b 2018-09-10 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
191 876c234b 2018-09-10 stsp return NULL;
194 876c234b 2018-09-10 stsp const struct got_error *
195 876c234b 2018-09-10 stsp got_object_packed_read_privsep(struct got_object **obj,
196 876c234b 2018-09-10 stsp struct got_repository *repo, struct got_pack *pack,
197 876c234b 2018-09-10 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
199 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
200 876c234b 2018-09-10 stsp int imsg_fds[2];
202 876c234b 2018-09-10 stsp struct imsgbuf *ibuf;
204 876c234b 2018-09-10 stsp if (pack->privsep_child)
205 876c234b 2018-09-10 stsp return request_packed_object(obj, pack, idx, id);
207 876c234b 2018-09-10 stsp ibuf = calloc(1, sizeof(*ibuf));
208 876c234b 2018-09-10 stsp if (ibuf == NULL)
209 876c234b 2018-09-10 stsp return got_error_from_errno();
211 876c234b 2018-09-10 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
212 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL) {
213 876c234b 2018-09-10 stsp err = got_error_from_errno();
214 876c234b 2018-09-10 stsp free(ibuf);
215 876c234b 2018-09-10 stsp return err;
218 876c234b 2018-09-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
219 876c234b 2018-09-10 stsp err = got_error_from_errno();
223 876c234b 2018-09-10 stsp pid = fork();
224 876c234b 2018-09-10 stsp if (pid == -1) {
225 876c234b 2018-09-10 stsp err = got_error_from_errno();
227 876c234b 2018-09-10 stsp } else if (pid == 0) {
228 876c234b 2018-09-10 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
229 876c234b 2018-09-10 stsp pack->path_packfile);
230 876c234b 2018-09-10 stsp /* not reached */
233 876c234b 2018-09-10 stsp close(imsg_fds[1]);
234 876c234b 2018-09-10 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
235 876c234b 2018-09-10 stsp pack->privsep_child->pid = pid;
236 876c234b 2018-09-10 stsp imsg_init(ibuf, imsg_fds[0]);
237 876c234b 2018-09-10 stsp pack->privsep_child->ibuf = ibuf;
239 876c234b 2018-09-10 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
241 876c234b 2018-09-10 stsp const struct got_error *child_err;
242 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
243 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
244 876c234b 2018-09-10 stsp pack->privsep_child->pid);
245 876c234b 2018-09-10 stsp if (child_err && err == NULL)
246 876c234b 2018-09-10 stsp err = child_err;
247 876c234b 2018-09-10 stsp free(ibuf);
248 876c234b 2018-09-10 stsp free(pack->privsep_child);
249 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
250 876c234b 2018-09-10 stsp return err;
255 876c234b 2018-09-10 stsp free(ibuf);
256 876c234b 2018-09-10 stsp free(pack->privsep_child);
257 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
259 876c234b 2018-09-10 stsp err = request_packed_object(obj, pack, idx, id);
260 876c234b 2018-09-10 stsp return err;
263 a440fac0 2018-09-06 stsp struct got_commit_object *
264 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
266 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
268 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
269 a440fac0 2018-09-06 stsp if (commit == NULL)
270 a440fac0 2018-09-06 stsp return NULL;
271 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
272 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
273 a440fac0 2018-09-06 stsp free(commit);
274 a440fac0 2018-09-06 stsp return NULL;
277 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
279 a440fac0 2018-09-06 stsp return commit;
282 a440fac0 2018-09-06 stsp const struct got_error *
283 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
284 a440fac0 2018-09-06 stsp const char *id_str)
286 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
287 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
289 a440fac0 2018-09-06 stsp qid = malloc(sizeof(*qid));
290 a440fac0 2018-09-06 stsp if (qid == NULL)
291 a440fac0 2018-09-06 stsp return got_error_from_errno();
293 a440fac0 2018-09-06 stsp qid->id = malloc(sizeof(*qid->id));
294 a440fac0 2018-09-06 stsp if (qid->id == NULL) {
295 a440fac0 2018-09-06 stsp err = got_error_from_errno();
296 a440fac0 2018-09-06 stsp got_object_qid_free(qid);
297 a440fac0 2018-09-06 stsp return err;
300 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
301 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
302 a440fac0 2018-09-06 stsp free(qid->id);
304 a440fac0 2018-09-06 stsp return err;
307 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
308 a440fac0 2018-09-06 stsp commit->nparents++;
310 a440fac0 2018-09-06 stsp return NULL;
313 a440fac0 2018-09-06 stsp static const struct got_error *
314 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
316 a440fac0 2018-09-06 stsp int sign = 1;
317 a440fac0 2018-09-06 stsp const char *p = tzstr;
318 a440fac0 2018-09-06 stsp time_t h, m;
320 a440fac0 2018-09-06 stsp *gmtoff = 0;
322 a440fac0 2018-09-06 stsp if (*p == '-')
324 a440fac0 2018-09-06 stsp else if (*p != '+')
325 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
327 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
328 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
329 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
332 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
333 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
334 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
336 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
337 a440fac0 2018-09-06 stsp return NULL;
340 a440fac0 2018-09-06 stsp static const struct got_error *
341 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
343 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
344 a440fac0 2018-09-06 stsp const char *errstr;
345 a440fac0 2018-09-06 stsp char *space, *tzstr;
346 a440fac0 2018-09-06 stsp time_t gmtoff;
347 a440fac0 2018-09-06 stsp time_t time;
349 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
350 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
351 a440fac0 2018-09-06 stsp if (space == NULL)
352 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
353 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
354 a440fac0 2018-09-06 stsp if (tzstr == NULL)
355 a440fac0 2018-09-06 stsp return got_error_from_errno();
356 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
357 a440fac0 2018-09-06 stsp free(tzstr);
359 a440fac0 2018-09-06 stsp return err;
360 a440fac0 2018-09-06 stsp *space = '\0';
362 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
363 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
364 a440fac0 2018-09-06 stsp if (space == NULL)
365 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
367 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
368 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
369 a440fac0 2018-09-06 stsp if (errstr)
370 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
372 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
373 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
374 a440fac0 2018-09-06 stsp time -= gmtoff;
375 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
376 a440fac0 2018-09-06 stsp return got_error_from_errno();
377 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
379 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
380 a440fac0 2018-09-06 stsp *space = '\0';
382 a440fac0 2018-09-06 stsp return NULL;
386 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
388 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
390 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
391 03fa71c8 2018-09-06 stsp commit->refcnt--;
392 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
396 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
397 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
398 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
399 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
402 03fa71c8 2018-09-06 stsp free(commit->tree_id);
403 03fa71c8 2018-09-06 stsp free(commit->author);
404 03fa71c8 2018-09-06 stsp free(commit->committer);
405 03fa71c8 2018-09-06 stsp free(commit->logmsg);
406 03fa71c8 2018-09-06 stsp free(commit);
409 a440fac0 2018-09-06 stsp const struct got_error *
410 a440fac0 2018-09-06 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
412 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
413 a440fac0 2018-09-06 stsp char *s = buf;
414 a440fac0 2018-09-06 stsp size_t tlen;
415 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
417 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
418 a440fac0 2018-09-06 stsp if (*commit == NULL)
419 a440fac0 2018-09-06 stsp return got_error_from_errno();
421 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
422 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
423 a440fac0 2018-09-06 stsp remain -= tlen;
424 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
425 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
430 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
433 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
434 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
436 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
440 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
441 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
442 a440fac0 2018-09-06 stsp remain -= tlen;
443 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
444 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
448 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
452 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
453 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
456 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
457 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
459 a440fac0 2018-09-06 stsp size_t slen;
461 a440fac0 2018-09-06 stsp remain -= tlen;
462 a440fac0 2018-09-06 stsp if (remain <= 0) {
463 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
468 a440fac0 2018-09-06 stsp if (p == NULL) {
469 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
473 a440fac0 2018-09-06 stsp slen = strlen(s);
474 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
477 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
478 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
479 a440fac0 2018-09-06 stsp err = got_error_from_errno();
482 a440fac0 2018-09-06 stsp s += slen + 1;
483 a440fac0 2018-09-06 stsp remain -= slen + 1;
486 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
487 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
489 a440fac0 2018-09-06 stsp size_t slen;
491 a440fac0 2018-09-06 stsp remain -= tlen;
492 a440fac0 2018-09-06 stsp if (remain <= 0) {
493 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
497 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
498 a440fac0 2018-09-06 stsp if (p == NULL) {
499 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 a440fac0 2018-09-06 stsp slen = strlen(s);
504 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
507 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
508 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
509 a440fac0 2018-09-06 stsp err = got_error_from_errno();
512 a440fac0 2018-09-06 stsp s += slen + 1;
513 a440fac0 2018-09-06 stsp remain -= slen + 1;
516 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
517 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
518 a440fac0 2018-09-06 stsp err = got_error_from_errno();
523 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
524 a440fac0 2018-09-06 stsp *commit = NULL;
526 a440fac0 2018-09-06 stsp return err;
530 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
532 a440fac0 2018-09-06 stsp free(te->id);
533 a440fac0 2018-09-06 stsp free(te->name);
538 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
540 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
542 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
543 03fa71c8 2018-09-06 stsp tree->refcnt--;
544 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
548 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
549 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
550 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
551 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
554 03fa71c8 2018-09-06 stsp free(tree);
557 a440fac0 2018-09-06 stsp struct got_tree_entry *
558 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
560 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
562 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
563 a440fac0 2018-09-06 stsp if (te == NULL)
564 a440fac0 2018-09-06 stsp return NULL;
566 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
567 a440fac0 2018-09-06 stsp if (te->id == NULL) {
574 a440fac0 2018-09-06 stsp static const struct got_error *
575 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
576 a440fac0 2018-09-06 stsp size_t maxlen)
578 a440fac0 2018-09-06 stsp char *p = buf, *space;
579 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
581 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
582 a440fac0 2018-09-06 stsp if (*te == NULL)
583 a440fac0 2018-09-06 stsp return got_error_from_errno();
585 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
586 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
588 a440fac0 2018-09-06 stsp *te = NULL;
589 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
592 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
593 a440fac0 2018-09-06 stsp if (space == NULL) {
594 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 a440fac0 2018-09-06 stsp *te = NULL;
597 a440fac0 2018-09-06 stsp return err;
599 a440fac0 2018-09-06 stsp while (*p != ' ') {
600 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
601 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
605 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
609 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
610 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
611 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
614 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
615 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
616 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
619 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
620 a440fac0 2018-09-06 stsp *te = NULL;
622 a440fac0 2018-09-06 stsp return err;
625 a440fac0 2018-09-06 stsp const struct got_error *
626 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
628 a440fac0 2018-09-06 stsp const struct got_error *err;
629 a440fac0 2018-09-06 stsp size_t remain = len;
631 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
632 a440fac0 2018-09-06 stsp if (*tree == NULL)
633 a440fac0 2018-09-06 stsp return got_error_from_errno();
635 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
637 a440fac0 2018-09-06 stsp while (remain > 0) {
638 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
639 a440fac0 2018-09-06 stsp size_t elen;
641 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
643 a440fac0 2018-09-06 stsp return err;
644 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
645 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
646 a440fac0 2018-09-06 stsp buf += elen;
647 a440fac0 2018-09-06 stsp remain -= elen;
650 a440fac0 2018-09-06 stsp if (remain != 0) {
651 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
652 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
655 a440fac0 2018-09-06 stsp return NULL;
658 ad242220 2018-09-08 stsp const struct got_error *
659 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
661 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
662 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
663 a440fac0 2018-09-06 stsp size_t n, total, remain;
664 a440fac0 2018-09-06 stsp uint8_t *buf;
666 a440fac0 2018-09-06 stsp *outbuf = NULL;
667 a440fac0 2018-09-06 stsp *outlen = 0;
669 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
670 a440fac0 2018-09-06 stsp if (buf == NULL)
671 a440fac0 2018-09-06 stsp return got_error_from_errno();
673 a440fac0 2018-09-06 stsp remain = blocksize;
675 a440fac0 2018-09-06 stsp while (1) {
676 a440fac0 2018-09-06 stsp if (remain == 0) {
677 a440fac0 2018-09-06 stsp uint8_t *newbuf;
678 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
679 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
680 a440fac0 2018-09-06 stsp err = got_error_from_errno();
683 a440fac0 2018-09-06 stsp buf = newbuf;
684 a440fac0 2018-09-06 stsp remain += blocksize;
686 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
687 a440fac0 2018-09-06 stsp if (n == 0) {
688 a440fac0 2018-09-06 stsp if (ferror(f)) {
689 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
692 a440fac0 2018-09-06 stsp break; /* EOF */
694 a440fac0 2018-09-06 stsp remain -= n;
695 a440fac0 2018-09-06 stsp total += n;
699 a440fac0 2018-09-06 stsp if (err == NULL) {
700 a440fac0 2018-09-06 stsp *outbuf = buf;
701 a440fac0 2018-09-06 stsp *outlen = total;
704 a440fac0 2018-09-06 stsp return err;
707 a440fac0 2018-09-06 stsp static const struct got_error *
708 ad242220 2018-09-08 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
709 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
711 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
712 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
714 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
716 cfd633c2 2018-09-10 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
718 3516b818 2018-09-08 stsp return err;
720 3516b818 2018-09-08 stsp return got_privsep_recv_commit(commit, ibuf);
723 a440fac0 2018-09-06 stsp const struct got_error *
724 cfd633c2 2018-09-10 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
725 cfd633c2 2018-09-10 stsp struct got_object *obj, struct got_pack *pack)
727 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
729 cfd633c2 2018-09-10 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
731 cfd633c2 2018-09-10 stsp return err;
733 cfd633c2 2018-09-10 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
736 cfd633c2 2018-09-10 stsp const struct got_error *
737 a440fac0 2018-09-06 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
738 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
740 a440fac0 2018-09-06 stsp int imsg_fds[2];
742 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
744 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
745 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
747 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
748 3516b818 2018-09-08 stsp if (ibuf == NULL)
749 3516b818 2018-09-08 stsp return got_error_from_errno();
751 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
752 a440fac0 2018-09-06 stsp return got_error_from_errno();
754 a440fac0 2018-09-06 stsp pid = fork();
755 a440fac0 2018-09-06 stsp if (pid == -1)
756 a440fac0 2018-09-06 stsp return got_error_from_errno();
757 a440fac0 2018-09-06 stsp else if (pid == 0) {
758 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
759 3cab8b4d 2018-09-08 stsp repo->path);
760 a440fac0 2018-09-06 stsp /* not reached */
763 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
764 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
765 ad242220 2018-09-08 stsp imsg_fds[0];
766 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
767 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
768 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
770 ad242220 2018-09-08 stsp return request_commit(commit, repo, obj, obj_fd);
773 a440fac0 2018-09-06 stsp static const struct got_error *
774 ad242220 2018-09-08 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
775 ad242220 2018-09-08 stsp struct got_object *obj, int fd)
777 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
778 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
780 3516b818 2018-09-08 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
782 cfd633c2 2018-09-10 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
784 3516b818 2018-09-08 stsp return err;
786 3516b818 2018-09-08 stsp return got_privsep_recv_tree(tree, ibuf);
789 a440fac0 2018-09-06 stsp const struct got_error *
790 a440fac0 2018-09-06 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
791 ad242220 2018-09-08 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
793 a440fac0 2018-09-06 stsp int imsg_fds[2];
795 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
797 ad242220 2018-09-08 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
798 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
800 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
801 3516b818 2018-09-08 stsp if (ibuf == NULL)
802 3516b818 2018-09-08 stsp return got_error_from_errno();
804 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
805 a440fac0 2018-09-06 stsp return got_error_from_errno();
807 a440fac0 2018-09-06 stsp pid = fork();
808 a440fac0 2018-09-06 stsp if (pid == -1)
809 a440fac0 2018-09-06 stsp return got_error_from_errno();
810 a440fac0 2018-09-06 stsp else if (pid == 0) {
811 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
812 3cab8b4d 2018-09-08 stsp repo->path);
813 a440fac0 2018-09-06 stsp /* not reached */
816 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
818 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
819 ad242220 2018-09-08 stsp imsg_fds[0];
820 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
821 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
822 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
825 ad242220 2018-09-08 stsp return request_tree(tree, repo, obj, obj_fd);
828 e7885405 2018-09-10 stsp const struct got_error *
829 e7885405 2018-09-10 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
830 e7885405 2018-09-10 stsp struct got_object *obj, struct got_pack *pack)
832 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
834 e7885405 2018-09-10 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
836 e7885405 2018-09-10 stsp return err;
838 e7885405 2018-09-10 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
841 55da3778 2018-09-10 stsp static const struct got_error *
842 55da3778 2018-09-10 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
844 55da3778 2018-09-10 stsp const struct got_error *err = NULL;
845 55da3778 2018-09-10 stsp int outfd_child;
847 55da3778 2018-09-10 stsp outfd_child = dup(outfd);
848 55da3778 2018-09-10 stsp if (outfd_child == -1)
849 55da3778 2018-09-10 stsp return got_error_from_errno();
851 55da3778 2018-09-10 stsp err = got_privsep_send_blob_req(ibuf, infd);
853 55da3778 2018-09-10 stsp return err;
855 55da3778 2018-09-10 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
857 55da3778 2018-09-10 stsp close(outfd_child);
858 55da3778 2018-09-10 stsp return err;
861 3516b818 2018-09-08 stsp err = got_privsep_recv_blob(size, ibuf);
863 3516b818 2018-09-08 stsp return err;
865 ad242220 2018-09-08 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
866 3516b818 2018-09-08 stsp return got_error_from_errno();
868 ad242220 2018-09-08 stsp return err;
871 a440fac0 2018-09-06 stsp const struct got_error *
872 ad242220 2018-09-08 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
873 ad242220 2018-09-08 stsp struct got_repository *repo)
875 a440fac0 2018-09-06 stsp int imsg_fds[2];
877 3516b818 2018-09-08 stsp struct imsgbuf *ibuf;
879 55da3778 2018-09-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
880 55da3778 2018-09-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
881 55da3778 2018-09-10 stsp return request_blob(size, outfd, infd, ibuf);
884 3516b818 2018-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
885 3516b818 2018-09-08 stsp if (ibuf == NULL)
886 3516b818 2018-09-08 stsp return got_error_from_errno();
888 a440fac0 2018-09-06 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
889 a440fac0 2018-09-06 stsp return got_error_from_errno();
891 a440fac0 2018-09-06 stsp pid = fork();
892 a440fac0 2018-09-06 stsp if (pid == -1)
893 a440fac0 2018-09-06 stsp return got_error_from_errno();
894 a440fac0 2018-09-06 stsp else if (pid == 0) {
895 3cab8b4d 2018-09-08 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
896 3cab8b4d 2018-09-08 stsp repo->path);
897 a440fac0 2018-09-06 stsp /* not reached */
900 a440fac0 2018-09-06 stsp close(imsg_fds[1]);
901 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
902 ad242220 2018-09-08 stsp imsg_fds[0];
903 ad242220 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
904 3516b818 2018-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
905 3516b818 2018-09-08 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
907 55da3778 2018-09-10 stsp return request_blob(size, outfd, infd, ibuf);