Blob


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