Blame


1 ad242220 2018-09-08 stsp /*
2 ad242220 2018-09-08 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 ad242220 2018-09-08 stsp *
4 ad242220 2018-09-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 ad242220 2018-09-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 ad242220 2018-09-08 stsp * copyright notice and this permission notice appear in all copies.
7 ad242220 2018-09-08 stsp *
8 ad242220 2018-09-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ad242220 2018-09-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ad242220 2018-09-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ad242220 2018-09-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ad242220 2018-09-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ad242220 2018-09-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ad242220 2018-09-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ad242220 2018-09-08 stsp */
16 ad242220 2018-09-08 stsp
17 ad242220 2018-09-08 stsp #include <sys/types.h>
18 ad242220 2018-09-08 stsp #include <sys/queue.h>
19 ad242220 2018-09-08 stsp #include <sys/uio.h>
20 ad242220 2018-09-08 stsp #include <sys/time.h>
21 ad242220 2018-09-08 stsp #include <sys/limits.h>
22 ad242220 2018-09-08 stsp
23 ad242220 2018-09-08 stsp #include <stdint.h>
24 ad242220 2018-09-08 stsp #include <imsg.h>
25 ad242220 2018-09-08 stsp #include <stdio.h>
26 ad242220 2018-09-08 stsp #include <stdlib.h>
27 ad242220 2018-09-08 stsp #include <string.h>
28 ad242220 2018-09-08 stsp #include <sha1.h>
29 ad242220 2018-09-08 stsp #include <zlib.h>
30 ad242220 2018-09-08 stsp
31 ad242220 2018-09-08 stsp #include "got_error.h"
32 ad242220 2018-09-08 stsp #include "got_object.h"
33 ad242220 2018-09-08 stsp
34 ad242220 2018-09-08 stsp #include "got_lib_delta.h"
35 ad242220 2018-09-08 stsp #include "got_lib_inflate.h"
36 ad242220 2018-09-08 stsp #include "got_lib_object.h"
37 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
38 ad242220 2018-09-08 stsp
39 ad242220 2018-09-08 stsp #ifndef nitems
40 ad242220 2018-09-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 ad242220 2018-09-08 stsp #endif
42 ad242220 2018-09-08 stsp
43 ad242220 2018-09-08 stsp #define GOT_OBJ_TAG_COMMIT "commit"
44 ad242220 2018-09-08 stsp #define GOT_OBJ_TAG_TREE "tree"
45 ad242220 2018-09-08 stsp #define GOT_OBJ_TAG_BLOB "blob"
46 ad242220 2018-09-08 stsp
47 ad242220 2018-09-08 stsp static const struct got_error *
48 ad242220 2018-09-08 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
49 ad242220 2018-09-08 stsp {
50 ad242220 2018-09-08 stsp const char *obj_tags[] = {
51 ad242220 2018-09-08 stsp GOT_OBJ_TAG_COMMIT,
52 ad242220 2018-09-08 stsp GOT_OBJ_TAG_TREE,
53 ad242220 2018-09-08 stsp GOT_OBJ_TAG_BLOB
54 ad242220 2018-09-08 stsp };
55 ad242220 2018-09-08 stsp const int obj_types[] = {
56 ad242220 2018-09-08 stsp GOT_OBJ_TYPE_COMMIT,
57 ad242220 2018-09-08 stsp GOT_OBJ_TYPE_TREE,
58 ad242220 2018-09-08 stsp GOT_OBJ_TYPE_BLOB,
59 ad242220 2018-09-08 stsp };
60 ad242220 2018-09-08 stsp int type = 0;
61 ad242220 2018-09-08 stsp size_t size = 0, hdrlen = 0;
62 ad242220 2018-09-08 stsp int i;
63 ad242220 2018-09-08 stsp char *p = strchr(buf, '\0');
64 ad242220 2018-09-08 stsp
65 ad242220 2018-09-08 stsp if (p == NULL)
66 ad242220 2018-09-08 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
67 ad242220 2018-09-08 stsp
68 ad242220 2018-09-08 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
69 ad242220 2018-09-08 stsp
70 ad242220 2018-09-08 stsp for (i = 0; i < nitems(obj_tags); i++) {
71 ad242220 2018-09-08 stsp const char *tag = obj_tags[i];
72 ad242220 2018-09-08 stsp size_t tlen = strlen(tag);
73 ad242220 2018-09-08 stsp const char *errstr;
74 ad242220 2018-09-08 stsp
75 ad242220 2018-09-08 stsp if (strncmp(buf, tag, tlen) != 0)
76 ad242220 2018-09-08 stsp continue;
77 ad242220 2018-09-08 stsp
78 ad242220 2018-09-08 stsp type = obj_types[i];
79 ad242220 2018-09-08 stsp if (len <= tlen)
80 ad242220 2018-09-08 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
81 ad242220 2018-09-08 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
82 ad242220 2018-09-08 stsp if (errstr != NULL)
83 ad242220 2018-09-08 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
84 ad242220 2018-09-08 stsp break;
85 ad242220 2018-09-08 stsp }
86 ad242220 2018-09-08 stsp
87 ad242220 2018-09-08 stsp if (type == 0)
88 ad242220 2018-09-08 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
89 ad242220 2018-09-08 stsp
90 ad242220 2018-09-08 stsp *obj = calloc(1, sizeof(**obj));
91 ad242220 2018-09-08 stsp if (*obj == NULL)
92 ad242220 2018-09-08 stsp return got_error_from_errno();
93 ad242220 2018-09-08 stsp (*obj)->type = type;
94 ad242220 2018-09-08 stsp (*obj)->hdrlen = hdrlen;
95 ad242220 2018-09-08 stsp (*obj)->size = size;
96 ad242220 2018-09-08 stsp return NULL;
97 ad242220 2018-09-08 stsp }
98 ad242220 2018-09-08 stsp
99 ad242220 2018-09-08 stsp static const struct got_error *
100 ad242220 2018-09-08 stsp read_object_header(struct got_object **obj, int fd)
101 ad242220 2018-09-08 stsp {
102 ad242220 2018-09-08 stsp const struct got_error *err;
103 ad242220 2018-09-08 stsp struct got_zstream_buf zb;
104 ad242220 2018-09-08 stsp char *buf;
105 ad242220 2018-09-08 stsp const size_t zbsize = 64;
106 ad242220 2018-09-08 stsp size_t outlen, totlen;
107 ad242220 2018-09-08 stsp int nbuf = 1;
108 ad242220 2018-09-08 stsp
109 ad242220 2018-09-08 stsp buf = malloc(zbsize);
110 ad242220 2018-09-08 stsp if (buf == NULL)
111 ad242220 2018-09-08 stsp return got_error_from_errno();
112 ad242220 2018-09-08 stsp
113 ad242220 2018-09-08 stsp err = got_inflate_init(&zb, buf, zbsize);
114 ad242220 2018-09-08 stsp if (err)
115 ad242220 2018-09-08 stsp return err;
116 ad242220 2018-09-08 stsp
117 ad242220 2018-09-08 stsp totlen = 0;
118 ad242220 2018-09-08 stsp do {
119 ad242220 2018-09-08 stsp err = got_inflate_read_fd(&zb, fd, &outlen);
120 ad242220 2018-09-08 stsp if (err)
121 ad242220 2018-09-08 stsp goto done;
122 ad242220 2018-09-08 stsp if (outlen == 0)
123 ad242220 2018-09-08 stsp break;
124 ad242220 2018-09-08 stsp totlen += outlen;
125 ad242220 2018-09-08 stsp if (strchr(zb.outbuf, '\0') == NULL) {
126 ad242220 2018-09-08 stsp char *newbuf;
127 ad242220 2018-09-08 stsp nbuf++;
128 ad242220 2018-09-08 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
129 ad242220 2018-09-08 stsp if (newbuf == NULL) {
130 ad242220 2018-09-08 stsp err = got_error_from_errno();
131 ad242220 2018-09-08 stsp goto done;
132 ad242220 2018-09-08 stsp }
133 ad242220 2018-09-08 stsp buf = newbuf;
134 ad242220 2018-09-08 stsp zb.outbuf = newbuf + totlen;
135 ad242220 2018-09-08 stsp zb.outlen = (nbuf * zbsize) - totlen;
136 ad242220 2018-09-08 stsp }
137 ad242220 2018-09-08 stsp } while (strchr(zb.outbuf, '\0') == NULL);
138 ad242220 2018-09-08 stsp
139 ad242220 2018-09-08 stsp err = parse_object_header(obj, buf, totlen);
140 ad242220 2018-09-08 stsp done:
141 ad242220 2018-09-08 stsp free(buf);
142 ad242220 2018-09-08 stsp got_inflate_end(&zb);
143 ad242220 2018-09-08 stsp return err;
144 ad242220 2018-09-08 stsp }
145 ad242220 2018-09-08 stsp
146 ad242220 2018-09-08 stsp
147 ad242220 2018-09-08 stsp int
148 ad242220 2018-09-08 stsp main(int argc, char *argv[])
149 ad242220 2018-09-08 stsp {
150 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
151 ad242220 2018-09-08 stsp struct got_object *obj = NULL;
152 ad242220 2018-09-08 stsp struct imsg imsg;
153 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
154 ad242220 2018-09-08 stsp size_t datalen;
155 ad242220 2018-09-08 stsp
156 ad242220 2018-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
157 ad242220 2018-09-08 stsp
158 ad242220 2018-09-08 stsp /* revoke access to most system calls */
159 ad242220 2018-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
160 ad242220 2018-09-08 stsp err = got_error_from_errno();
161 ad242220 2018-09-08 stsp got_privsep_send_error(&ibuf, err);
162 ad242220 2018-09-08 stsp return 1;
163 ad242220 2018-09-08 stsp }
164 ad242220 2018-09-08 stsp
165 ad242220 2018-09-08 stsp while (1) {
166 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
167 ad242220 2018-09-08 stsp if (err) {
168 ad242220 2018-09-08 stsp if (imsg.hdr.len == 0)
169 ad242220 2018-09-08 stsp err = NULL;
170 ad242220 2018-09-08 stsp break;
171 ad242220 2018-09-08 stsp }
172 ad242220 2018-09-08 stsp
173 ad242220 2018-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
174 ad242220 2018-09-08 stsp break;
175 ad242220 2018-09-08 stsp
176 ad242220 2018-09-08 stsp if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
177 ad242220 2018-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
178 ad242220 2018-09-08 stsp goto done;
179 ad242220 2018-09-08 stsp }
180 ad242220 2018-09-08 stsp
181 ad242220 2018-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
182 ad242220 2018-09-08 stsp if (datalen != 0) {
183 ad242220 2018-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
184 ad242220 2018-09-08 stsp goto done;
185 ad242220 2018-09-08 stsp }
186 ad242220 2018-09-08 stsp
187 ad242220 2018-09-08 stsp err = read_object_header(&obj, imsg.fd);
188 ad242220 2018-09-08 stsp if (err)
189 ad242220 2018-09-08 stsp goto done;
190 ad242220 2018-09-08 stsp
191 ad242220 2018-09-08 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
192 ad242220 2018-09-08 stsp done:
193 ad242220 2018-09-08 stsp close(imsg.fd);
194 ad242220 2018-09-08 stsp imsg_free(&imsg);
195 ad242220 2018-09-08 stsp if (obj)
196 ad242220 2018-09-08 stsp got_object_close(obj);
197 ad242220 2018-09-08 stsp if (err) {
198 ad242220 2018-09-08 stsp got_privsep_send_error(&ibuf, err);
199 ad242220 2018-09-08 stsp break;
200 ad242220 2018-09-08 stsp }
201 ad242220 2018-09-08 stsp }
202 ad242220 2018-09-08 stsp
203 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
204 ad242220 2018-09-08 stsp if (err)
205 ad242220 2018-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
206 ad242220 2018-09-08 stsp close(GOT_IMSG_FD_CHILD);
207 ad242220 2018-09-08 stsp return err ? 1 : 0;
208 ad242220 2018-09-08 stsp }