Blame


1 2178c42e 2018-04-22 stsp /*
2 2178c42e 2018-04-22 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 2178c42e 2018-04-22 stsp *
4 2178c42e 2018-04-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 2178c42e 2018-04-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 2178c42e 2018-04-22 stsp * copyright notice and this permission notice appear in all copies.
7 2178c42e 2018-04-22 stsp *
8 2178c42e 2018-04-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2178c42e 2018-04-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2178c42e 2018-04-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2178c42e 2018-04-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2178c42e 2018-04-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2178c42e 2018-04-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2178c42e 2018-04-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 2178c42e 2018-04-22 stsp */
16 2178c42e 2018-04-22 stsp
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 2178c42e 2018-04-22 stsp #include <sys/queue.h>
19 2178c42e 2018-04-22 stsp #include <sys/uio.h>
20 2178c42e 2018-04-22 stsp
21 2178c42e 2018-04-22 stsp #include <stdio.h>
22 2178c42e 2018-04-22 stsp #include <stdlib.h>
23 2178c42e 2018-04-22 stsp #include <string.h>
24 2178c42e 2018-04-22 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <stdint.h>
26 2178c42e 2018-04-22 stsp #include <poll.h>
27 2178c42e 2018-04-22 stsp #include <imsg.h>
28 2178c42e 2018-04-22 stsp #include <sha1.h>
29 2178c42e 2018-04-22 stsp #include <zlib.h>
30 2178c42e 2018-04-22 stsp
31 2178c42e 2018-04-22 stsp #include "got_object.h"
32 2178c42e 2018-04-22 stsp #include "got_error.h"
33 2178c42e 2018-04-22 stsp
34 2178c42e 2018-04-22 stsp #include "got_lib_sha1.h"
35 2178c42e 2018-04-22 stsp #include "got_lib_delta.h"
36 2178c42e 2018-04-22 stsp #include "got_lib_zbuf.h"
37 2178c42e 2018-04-22 stsp #include "got_lib_object.h"
38 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
39 2178c42e 2018-04-22 stsp
40 2178c42e 2018-04-22 stsp #ifndef MIN
41 2178c42e 2018-04-22 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 2178c42e 2018-04-22 stsp #endif
43 2178c42e 2018-04-22 stsp
44 2178c42e 2018-04-22 stsp static const struct got_error *
45 2178c42e 2018-04-22 stsp poll_fd(int fd, int events, int timeout)
46 2178c42e 2018-04-22 stsp {
47 2178c42e 2018-04-22 stsp struct pollfd pfd[1];
48 2178c42e 2018-04-22 stsp int n;
49 2178c42e 2018-04-22 stsp
50 2178c42e 2018-04-22 stsp pfd[0].fd = fd;
51 2178c42e 2018-04-22 stsp pfd[0].events = events;
52 2178c42e 2018-04-22 stsp
53 2178c42e 2018-04-22 stsp n = poll(pfd, 1, timeout);
54 2178c42e 2018-04-22 stsp if (n == -1)
55 2178c42e 2018-04-22 stsp return got_error_from_errno();
56 2178c42e 2018-04-22 stsp if (n == 0)
57 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_TIMEOUT);
58 2178c42e 2018-04-22 stsp if (pfd[0].revents & (POLLERR | POLLNVAL))
59 2178c42e 2018-04-22 stsp return got_error_from_errno();
60 2178c42e 2018-04-22 stsp if (pfd[0].revents & (events | POLLHUP))
61 2178c42e 2018-04-22 stsp return NULL;
62 2178c42e 2018-04-22 stsp
63 2178c42e 2018-04-22 stsp return got_error(GOT_ERR_INTERRUPT);
64 2178c42e 2018-04-22 stsp }
65 2178c42e 2018-04-22 stsp
66 c4eae628 2018-04-23 stsp static const struct got_error *
67 fe36cf76 2018-04-23 stsp recv_one_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
68 fe36cf76 2018-04-23 stsp {
69 fe36cf76 2018-04-23 stsp const struct got_error *err;
70 fe36cf76 2018-04-23 stsp ssize_t n, m;
71 fe36cf76 2018-04-23 stsp
72 fe36cf76 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLIN, INFTIM);
73 fe36cf76 2018-04-23 stsp if (err)
74 fe36cf76 2018-04-23 stsp return err;
75 fe36cf76 2018-04-23 stsp
76 fe36cf76 2018-04-23 stsp n = imsg_read(ibuf);
77 fe36cf76 2018-04-23 stsp if (n == -1) {
78 fe36cf76 2018-04-23 stsp if (errno == EAGAIN) /* Could be a file-descriptor leak. */
79 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
80 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
81 fe36cf76 2018-04-23 stsp }
82 fe36cf76 2018-04-23 stsp if (n == 0)
83 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_PIPE);
84 fe36cf76 2018-04-23 stsp
85 fe36cf76 2018-04-23 stsp m = imsg_get(ibuf, imsg);
86 fe36cf76 2018-04-23 stsp if (m == 0)
87 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
88 fe36cf76 2018-04-23 stsp
89 fe36cf76 2018-04-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
90 fe36cf76 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
91 fe36cf76 2018-04-23 stsp
92 fe36cf76 2018-04-23 stsp return NULL;
93 fe36cf76 2018-04-23 stsp }
94 fe36cf76 2018-04-23 stsp
95 fe36cf76 2018-04-23 stsp static const struct got_error *
96 c4eae628 2018-04-23 stsp recv_imsg_error(struct imsg *imsg, size_t datalen)
97 c4eae628 2018-04-23 stsp {
98 c4eae628 2018-04-23 stsp struct got_imsg_error ierr;
99 c4eae628 2018-04-23 stsp
100 c4eae628 2018-04-23 stsp if (datalen != sizeof(ierr))
101 c4eae628 2018-04-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
102 c4eae628 2018-04-23 stsp
103 c4eae628 2018-04-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
104 c4eae628 2018-04-23 stsp if (ierr.code == GOT_ERR_ERRNO) {
105 c4eae628 2018-04-23 stsp static struct got_error serr;
106 c4eae628 2018-04-23 stsp serr.code = GOT_ERR_ERRNO;
107 c4eae628 2018-04-23 stsp serr.msg = strerror(ierr.errno_code);
108 c4eae628 2018-04-23 stsp return &serr;
109 c4eae628 2018-04-23 stsp }
110 c4eae628 2018-04-23 stsp
111 c4eae628 2018-04-23 stsp return got_error(ierr.code);
112 c4eae628 2018-04-23 stsp }
113 c4eae628 2018-04-23 stsp
114 2178c42e 2018-04-22 stsp /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
115 2178c42e 2018-04-22 stsp void
116 2178c42e 2018-04-22 stsp got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
117 2178c42e 2018-04-22 stsp {
118 2178c42e 2018-04-22 stsp const struct got_error *poll_err;
119 2178c42e 2018-04-22 stsp struct got_imsg_error ierr;
120 2178c42e 2018-04-22 stsp int ret;
121 2178c42e 2018-04-22 stsp
122 2178c42e 2018-04-22 stsp ierr.code = err->code;
123 2178c42e 2018-04-22 stsp if (err->code == GOT_ERR_ERRNO)
124 2178c42e 2018-04-22 stsp ierr.errno_code = errno;
125 2178c42e 2018-04-22 stsp else
126 2178c42e 2018-04-22 stsp ierr.errno_code = 0;
127 2178c42e 2018-04-22 stsp ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
128 2178c42e 2018-04-22 stsp if (ret != -1) {
129 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
130 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
131 5d43e84d 2018-04-23 stsp return;
132 2178c42e 2018-04-22 stsp }
133 2178c42e 2018-04-22 stsp
134 2178c42e 2018-04-22 stsp poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
135 5d43e84d 2018-04-23 stsp if (poll_err) {
136 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
137 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, poll_err->msg);
138 5d43e84d 2018-04-23 stsp return;
139 5d43e84d 2018-04-23 stsp }
140 2178c42e 2018-04-22 stsp
141 2178c42e 2018-04-22 stsp ret = imsg_flush(ibuf);
142 5d43e84d 2018-04-23 stsp if (ret == -1) {
143 2178c42e 2018-04-22 stsp fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
144 2178c42e 2018-04-22 stsp getprogname(), err->code, err->msg, strerror(errno));
145 5d43e84d 2018-04-23 stsp return;
146 5d43e84d 2018-04-23 stsp }
147 2178c42e 2018-04-22 stsp }
148 2178c42e 2018-04-22 stsp
149 2178c42e 2018-04-22 stsp const struct got_error *
150 2178c42e 2018-04-22 stsp got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj, int ndeltas)
151 2178c42e 2018-04-22 stsp {
152 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
153 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
154 2178c42e 2018-04-22 stsp
155 2178c42e 2018-04-22 stsp iobj.type = obj->type;
156 2178c42e 2018-04-22 stsp iobj.flags = obj->flags;
157 2178c42e 2018-04-22 stsp iobj.hdrlen = obj->hdrlen;
158 2178c42e 2018-04-22 stsp iobj.size = obj->size;
159 2178c42e 2018-04-22 stsp iobj.ndeltas = ndeltas;
160 2178c42e 2018-04-22 stsp
161 2178c42e 2018-04-22 stsp if (ndeltas > 0) {
162 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
163 2178c42e 2018-04-22 stsp }
164 2178c42e 2018-04-22 stsp
165 2178c42e 2018-04-22 stsp if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
166 2178c42e 2018-04-22 stsp == -1)
167 2178c42e 2018-04-22 stsp return got_error_from_errno();
168 2178c42e 2018-04-22 stsp
169 2178c42e 2018-04-22 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
170 2178c42e 2018-04-22 stsp if (err)
171 2178c42e 2018-04-22 stsp return err;
172 2178c42e 2018-04-22 stsp
173 2178c42e 2018-04-22 stsp if (imsg_flush(ibuf) == -1)
174 2178c42e 2018-04-22 stsp return got_error_from_errno();
175 2178c42e 2018-04-22 stsp
176 2178c42e 2018-04-22 stsp return NULL;
177 2178c42e 2018-04-22 stsp }
178 2178c42e 2018-04-22 stsp
179 2178c42e 2018-04-22 stsp const struct got_error *
180 2178c42e 2018-04-22 stsp got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
181 2178c42e 2018-04-22 stsp {
182 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
183 2178c42e 2018-04-22 stsp struct imsg imsg;
184 2178c42e 2018-04-22 stsp struct got_imsg_object iobj;
185 2178c42e 2018-04-22 stsp size_t datalen;
186 2178c42e 2018-04-22 stsp int i;
187 c4eae628 2018-04-23 stsp const size_t min_datalen =
188 c4eae628 2018-04-23 stsp MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
189 2178c42e 2018-04-22 stsp
190 2178c42e 2018-04-22 stsp *obj = NULL;
191 2178c42e 2018-04-22 stsp
192 fe36cf76 2018-04-23 stsp err = recv_one_imsg(&imsg, ibuf, min_datalen);
193 2178c42e 2018-04-22 stsp if (err)
194 2178c42e 2018-04-22 stsp return err;
195 2178c42e 2018-04-22 stsp
196 2178c42e 2018-04-22 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
197 2178c42e 2018-04-22 stsp
198 2178c42e 2018-04-22 stsp switch (imsg.hdr.type) {
199 2178c42e 2018-04-22 stsp case GOT_IMSG_ERROR:
200 c4eae628 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
201 2178c42e 2018-04-22 stsp break;
202 2178c42e 2018-04-22 stsp case GOT_IMSG_OBJECT:
203 2178c42e 2018-04-22 stsp if (datalen != sizeof(iobj)) {
204 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
205 2178c42e 2018-04-22 stsp break;
206 2178c42e 2018-04-22 stsp }
207 2178c42e 2018-04-22 stsp
208 2178c42e 2018-04-22 stsp memcpy(&iobj, imsg.data, sizeof(iobj));
209 2178c42e 2018-04-22 stsp if (iobj.ndeltas < 0 ||
210 2178c42e 2018-04-22 stsp iobj.ndeltas > GOT_DELTA_CHAIN_RECURSION_MAX) {
211 2178c42e 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
212 2178c42e 2018-04-22 stsp break;
213 2178c42e 2018-04-22 stsp }
214 2178c42e 2018-04-22 stsp
215 2178c42e 2018-04-22 stsp *obj = calloc(1, sizeof(**obj));
216 2178c42e 2018-04-22 stsp if (*obj == NULL) {
217 2178c42e 2018-04-22 stsp err = got_error_from_errno();
218 2178c42e 2018-04-22 stsp break;
219 2178c42e 2018-04-22 stsp }
220 2178c42e 2018-04-22 stsp
221 2178c42e 2018-04-22 stsp (*obj)->type = iobj.type;
222 2178c42e 2018-04-22 stsp (*obj)->hdrlen = iobj.hdrlen;
223 2178c42e 2018-04-22 stsp (*obj)->size = iobj.size;
224 2178c42e 2018-04-22 stsp for (i = 0; i < iobj.ndeltas; i++) {
225 2178c42e 2018-04-22 stsp /* TODO: Handle deltas */
226 bff6ca00 2018-04-23 stsp }
227 bff6ca00 2018-04-23 stsp break;
228 bff6ca00 2018-04-23 stsp default:
229 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
230 bff6ca00 2018-04-23 stsp break;
231 bff6ca00 2018-04-23 stsp }
232 bff6ca00 2018-04-23 stsp
233 bff6ca00 2018-04-23 stsp imsg_free(&imsg);
234 bff6ca00 2018-04-23 stsp
235 bff6ca00 2018-04-23 stsp return err;
236 bff6ca00 2018-04-23 stsp }
237 bff6ca00 2018-04-23 stsp
238 bff6ca00 2018-04-23 stsp const struct got_error *
239 bff6ca00 2018-04-23 stsp got_privsep_send_commit_obj(struct imsgbuf *ibuf, struct got_commit_object *commit)
240 bff6ca00 2018-04-23 stsp {
241 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
242 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
243 bff6ca00 2018-04-23 stsp uint8_t *buf;
244 bff6ca00 2018-04-23 stsp size_t len, total;
245 bff6ca00 2018-04-23 stsp struct got_parent_id *pid;
246 bff6ca00 2018-04-23 stsp
247 bff6ca00 2018-04-23 stsp if (got_sha1_digest_to_str(commit->tree_id->sha1, icommit.tree_id,
248 bff6ca00 2018-04-23 stsp sizeof(icommit.tree_id)) == NULL)
249 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
250 bff6ca00 2018-04-23 stsp icommit.author_len = strlen(commit->author);
251 bff6ca00 2018-04-23 stsp icommit.committer_len = strlen(commit->committer);
252 bff6ca00 2018-04-23 stsp icommit.logmsg_len = strlen(commit->logmsg);
253 bff6ca00 2018-04-23 stsp icommit.nparents = commit->nparents;
254 bff6ca00 2018-04-23 stsp
255 bff6ca00 2018-04-23 stsp total = sizeof(icommit) + icommit.author_len +
256 bff6ca00 2018-04-23 stsp icommit.committer_len + icommit.logmsg_len +
257 bff6ca00 2018-04-23 stsp icommit.nparents * (SHA1_DIGEST_STRING_LENGTH);
258 bff6ca00 2018-04-23 stsp /* XXX TODO support very large log messages properly */
259 bff6ca00 2018-04-23 stsp if (total > MAX_IMSGSIZE)
260 bff6ca00 2018-04-23 stsp return got_error(GOT_ERR_NO_SPACE);
261 bff6ca00 2018-04-23 stsp
262 bff6ca00 2018-04-23 stsp buf = malloc(total);
263 bff6ca00 2018-04-23 stsp if (buf == NULL)
264 bff6ca00 2018-04-23 stsp return got_error_from_errno();
265 bff6ca00 2018-04-23 stsp
266 bff6ca00 2018-04-23 stsp len = 0;
267 bff6ca00 2018-04-23 stsp memcpy(buf + len, &icommit, sizeof(icommit));
268 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
269 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->author, icommit.author_len);
270 bff6ca00 2018-04-23 stsp len += icommit.author_len;
271 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->committer, icommit.committer_len);
272 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
273 bff6ca00 2018-04-23 stsp memcpy(buf + len, commit->logmsg, icommit.logmsg_len);
274 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
275 bff6ca00 2018-04-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
276 bff6ca00 2018-04-23 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
277 bff6ca00 2018-04-23 stsp if (got_sha1_digest_to_str(pid->id->sha1, id_str,
278 bff6ca00 2018-04-23 stsp sizeof(id_str)) == NULL) {
279 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
280 bff6ca00 2018-04-23 stsp goto done;
281 bff6ca00 2018-04-23 stsp }
282 bff6ca00 2018-04-23 stsp memcpy(buf + len, id_str, SHA1_DIGEST_STRING_LENGTH);
283 bff6ca00 2018-04-23 stsp len += SHA1_DIGEST_STRING_LENGTH;
284 bff6ca00 2018-04-23 stsp }
285 bff6ca00 2018-04-23 stsp
286 bff6ca00 2018-04-23 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
287 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
288 bff6ca00 2018-04-23 stsp goto done;
289 bff6ca00 2018-04-23 stsp }
290 bff6ca00 2018-04-23 stsp
291 bff6ca00 2018-04-23 stsp err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
292 bff6ca00 2018-04-23 stsp if (err)
293 bff6ca00 2018-04-23 stsp goto done;
294 bff6ca00 2018-04-23 stsp
295 bff6ca00 2018-04-23 stsp if (imsg_flush(ibuf) == -1) {
296 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
297 bff6ca00 2018-04-23 stsp goto done;
298 bff6ca00 2018-04-23 stsp }
299 bff6ca00 2018-04-23 stsp
300 bff6ca00 2018-04-23 stsp done:
301 bff6ca00 2018-04-23 stsp free(buf);
302 bff6ca00 2018-04-23 stsp return err;
303 bff6ca00 2018-04-23 stsp }
304 bff6ca00 2018-04-23 stsp const struct got_error *
305 bff6ca00 2018-04-23 stsp got_privsep_recv_commit_obj(struct got_commit_object **commit,
306 bff6ca00 2018-04-23 stsp struct imsgbuf *ibuf)
307 bff6ca00 2018-04-23 stsp {
308 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
309 bff6ca00 2018-04-23 stsp struct imsg imsg;
310 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object icommit;
311 bff6ca00 2018-04-23 stsp size_t len, datalen;
312 bff6ca00 2018-04-23 stsp int i;
313 bff6ca00 2018-04-23 stsp const size_t min_datalen =
314 bff6ca00 2018-04-23 stsp MIN(sizeof(struct got_imsg_error),
315 bff6ca00 2018-04-23 stsp sizeof(struct got_imsg_commit_object));
316 bff6ca00 2018-04-23 stsp uint8_t *data;
317 bff6ca00 2018-04-23 stsp
318 bff6ca00 2018-04-23 stsp *commit = NULL;
319 bff6ca00 2018-04-23 stsp
320 bff6ca00 2018-04-23 stsp err = recv_one_imsg(&imsg, ibuf, min_datalen);
321 bff6ca00 2018-04-23 stsp if (err)
322 bff6ca00 2018-04-23 stsp return err;
323 bff6ca00 2018-04-23 stsp
324 bff6ca00 2018-04-23 stsp data = imsg.data;
325 bff6ca00 2018-04-23 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
326 bff6ca00 2018-04-23 stsp len = 0;
327 bff6ca00 2018-04-23 stsp
328 bff6ca00 2018-04-23 stsp switch (imsg.hdr.type) {
329 bff6ca00 2018-04-23 stsp case GOT_IMSG_ERROR:
330 bff6ca00 2018-04-23 stsp err = recv_imsg_error(&imsg, datalen);
331 bff6ca00 2018-04-23 stsp break;
332 bff6ca00 2018-04-23 stsp case GOT_IMSG_COMMIT:
333 bff6ca00 2018-04-23 stsp if (datalen < sizeof(icommit)) {
334 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
335 bff6ca00 2018-04-23 stsp break;
336 2178c42e 2018-04-22 stsp }
337 bff6ca00 2018-04-23 stsp
338 bff6ca00 2018-04-23 stsp memcpy(&icommit, data, sizeof(icommit));
339 bff6ca00 2018-04-23 stsp if (datalen != sizeof(icommit) + icommit.author_len +
340 bff6ca00 2018-04-23 stsp icommit.committer_len + icommit.logmsg_len +
341 bff6ca00 2018-04-23 stsp icommit.nparents * (SHA1_DIGEST_STRING_LENGTH)) {
342 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
343 bff6ca00 2018-04-23 stsp break;
344 bff6ca00 2018-04-23 stsp }
345 bff6ca00 2018-04-23 stsp if (icommit.nparents < 0) {
346 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
347 bff6ca00 2018-04-23 stsp break;
348 bff6ca00 2018-04-23 stsp }
349 bff6ca00 2018-04-23 stsp len += sizeof(icommit);
350 bff6ca00 2018-04-23 stsp
351 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
352 bff6ca00 2018-04-23 stsp if (*commit == NULL) {
353 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
354 bff6ca00 2018-04-23 stsp break;
355 bff6ca00 2018-04-23 stsp }
356 bff6ca00 2018-04-23 stsp
357 bff6ca00 2018-04-23 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1,
358 bff6ca00 2018-04-23 stsp icommit.tree_id)) {
359 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
360 bff6ca00 2018-04-23 stsp break;
361 bff6ca00 2018-04-23 stsp }
362 bff6ca00 2018-04-23 stsp
363 bff6ca00 2018-04-23 stsp if (icommit.author_len == 0) {
364 bff6ca00 2018-04-23 stsp (*commit)->author = strdup("");
365 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
366 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
367 bff6ca00 2018-04-23 stsp break;
368 bff6ca00 2018-04-23 stsp }
369 bff6ca00 2018-04-23 stsp } else {
370 bff6ca00 2018-04-23 stsp (*commit)->author = malloc(icommit.author_len + 1);
371 bff6ca00 2018-04-23 stsp if ((*commit)->author == NULL) {
372 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
373 bff6ca00 2018-04-23 stsp break;
374 bff6ca00 2018-04-23 stsp }
375 bff6ca00 2018-04-23 stsp memcpy((*commit)->author, data + len,
376 bff6ca00 2018-04-23 stsp icommit.author_len);
377 bff6ca00 2018-04-23 stsp (*commit)->author[icommit.author_len] = '\0';
378 bff6ca00 2018-04-23 stsp }
379 bff6ca00 2018-04-23 stsp len += icommit.author_len;
380 bff6ca00 2018-04-23 stsp
381 bff6ca00 2018-04-23 stsp if (icommit.committer_len == 0) {
382 bff6ca00 2018-04-23 stsp (*commit)->committer = strdup("");
383 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
384 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
385 bff6ca00 2018-04-23 stsp break;
386 bff6ca00 2018-04-23 stsp }
387 bff6ca00 2018-04-23 stsp } else {
388 bff6ca00 2018-04-23 stsp (*commit)->committer =
389 bff6ca00 2018-04-23 stsp malloc(icommit.committer_len + 1);
390 bff6ca00 2018-04-23 stsp if ((*commit)->committer == NULL) {
391 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
392 bff6ca00 2018-04-23 stsp break;
393 bff6ca00 2018-04-23 stsp }
394 bff6ca00 2018-04-23 stsp memcpy((*commit)->committer, data + len,
395 bff6ca00 2018-04-23 stsp icommit.committer_len);
396 bff6ca00 2018-04-23 stsp (*commit)->committer[icommit.committer_len] = '\0';
397 bff6ca00 2018-04-23 stsp }
398 bff6ca00 2018-04-23 stsp len += icommit.committer_len;
399 bff6ca00 2018-04-23 stsp
400 bff6ca00 2018-04-23 stsp if (icommit.logmsg_len == 0) {
401 bff6ca00 2018-04-23 stsp (*commit)->logmsg = strdup("");
402 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
403 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
404 bff6ca00 2018-04-23 stsp break;
405 bff6ca00 2018-04-23 stsp }
406 bff6ca00 2018-04-23 stsp } else {
407 bff6ca00 2018-04-23 stsp (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
408 bff6ca00 2018-04-23 stsp if ((*commit)->logmsg == NULL) {
409 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
410 bff6ca00 2018-04-23 stsp break;
411 bff6ca00 2018-04-23 stsp }
412 bff6ca00 2018-04-23 stsp memcpy((*commit)->logmsg, data + len,
413 bff6ca00 2018-04-23 stsp icommit.logmsg_len);
414 bff6ca00 2018-04-23 stsp (*commit)->logmsg[icommit.logmsg_len] = '\0';
415 bff6ca00 2018-04-23 stsp }
416 bff6ca00 2018-04-23 stsp len += icommit.logmsg_len;
417 bff6ca00 2018-04-23 stsp
418 bff6ca00 2018-04-23 stsp for (i = 0; i < icommit.nparents; i++) {
419 bff6ca00 2018-04-23 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
420 bff6ca00 2018-04-23 stsp memcpy(id_str, data + len +
421 bff6ca00 2018-04-23 stsp i * SHA1_DIGEST_STRING_LENGTH, sizeof(id_str));
422 bff6ca00 2018-04-23 stsp id_str[SHA1_DIGEST_STRING_LENGTH - 1] = '\0';
423 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, id_str);
424 bff6ca00 2018-04-23 stsp if (err)
425 bff6ca00 2018-04-23 stsp break;
426 bff6ca00 2018-04-23 stsp }
427 2178c42e 2018-04-22 stsp break;
428 8c580685 2018-04-22 stsp default:
429 8c580685 2018-04-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
430 8c580685 2018-04-22 stsp break;
431 2178c42e 2018-04-22 stsp }
432 2178c42e 2018-04-22 stsp
433 2178c42e 2018-04-22 stsp imsg_free(&imsg);
434 2178c42e 2018-04-22 stsp
435 2178c42e 2018-04-22 stsp return err;
436 2178c42e 2018-04-22 stsp }