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>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sha1.h>
29 #include <zlib.h>
31 #include "got_error.h"
32 #include "got_object.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_inflate.h"
36 #include "got_lib_object.h"
37 #include "got_lib_privsep.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #endif
43 #define GOT_OBJ_TAG_COMMIT "commit"
44 #define GOT_OBJ_TAG_TREE "tree"
45 #define GOT_OBJ_TAG_BLOB "blob"
47 static const struct got_error *
48 parse_object_header(struct got_object **obj, char *buf, size_t len)
49 {
50 const char *obj_tags[] = {
51 GOT_OBJ_TAG_COMMIT,
52 GOT_OBJ_TAG_TREE,
53 GOT_OBJ_TAG_BLOB
54 };
55 const int obj_types[] = {
56 GOT_OBJ_TYPE_COMMIT,
57 GOT_OBJ_TYPE_TREE,
58 GOT_OBJ_TYPE_BLOB,
59 };
60 int type = 0;
61 size_t size = 0, hdrlen = 0;
62 int i;
63 char *p = strchr(buf, '\0');
65 if (p == NULL)
66 return got_error(GOT_ERR_BAD_OBJ_HDR);
68 hdrlen = strlen(buf) + 1 /* '\0' */;
70 for (i = 0; i < nitems(obj_tags); i++) {
71 const char *tag = obj_tags[i];
72 size_t tlen = strlen(tag);
73 const char *errstr;
75 if (strncmp(buf, tag, tlen) != 0)
76 continue;
78 type = obj_types[i];
79 if (len <= tlen)
80 return got_error(GOT_ERR_BAD_OBJ_HDR);
81 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
82 if (errstr != NULL)
83 return got_error(GOT_ERR_BAD_OBJ_HDR);
84 break;
85 }
87 if (type == 0)
88 return got_error(GOT_ERR_BAD_OBJ_HDR);
90 *obj = calloc(1, sizeof(**obj));
91 if (*obj == NULL)
92 return got_error_from_errno();
93 (*obj)->type = type;
94 (*obj)->hdrlen = hdrlen;
95 (*obj)->size = size;
96 return NULL;
97 }
99 static const struct got_error *
100 read_object_header(struct got_object **obj, int fd)
102 const struct got_error *err;
103 struct got_zstream_buf zb;
104 char *buf;
105 const size_t zbsize = 64;
106 size_t outlen, totlen;
107 int nbuf = 1;
109 buf = malloc(zbsize);
110 if (buf == NULL)
111 return got_error_from_errno();
113 err = got_inflate_init(&zb, buf, zbsize);
114 if (err)
115 return err;
117 totlen = 0;
118 do {
119 err = got_inflate_read_fd(&zb, fd, &outlen);
120 if (err)
121 goto done;
122 if (outlen == 0)
123 break;
124 totlen += outlen;
125 if (strchr(zb.outbuf, '\0') == NULL) {
126 char *newbuf;
127 nbuf++;
128 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
129 if (newbuf == NULL) {
130 err = got_error_from_errno();
131 goto done;
133 buf = newbuf;
134 zb.outbuf = newbuf + totlen;
135 zb.outlen = (nbuf * zbsize) - totlen;
137 } while (strchr(zb.outbuf, '\0') == NULL);
139 err = parse_object_header(obj, buf, totlen);
140 done:
141 free(buf);
142 got_inflate_end(&zb);
143 return err;
147 int
148 main(int argc, char *argv[])
150 const struct got_error *err = NULL;
151 struct got_object *obj = NULL;
152 struct imsg imsg;
153 struct imsgbuf ibuf;
154 size_t datalen;
156 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
158 /* revoke access to most system calls */
159 if (pledge("stdio recvfd", NULL) == -1) {
160 err = got_error_from_errno();
161 got_privsep_send_error(&ibuf, err);
162 return 1;
165 while (1) {
166 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
167 if (err) {
168 if (imsg.hdr.len == 0)
169 err = NULL;
170 break;
173 if (imsg.hdr.type == GOT_IMSG_STOP)
174 break;
176 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
177 err = got_error(GOT_ERR_PRIVSEP_MSG);
178 goto done;
181 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
182 if (datalen != 0) {
183 err = got_error(GOT_ERR_PRIVSEP_LEN);
184 goto done;
187 err = read_object_header(&obj, imsg.fd);
188 if (err)
189 goto done;
191 err = got_privsep_send_obj(&ibuf, obj, 0);
192 done:
193 close(imsg.fd);
194 imsg_free(&imsg);
195 if (obj)
196 got_object_close(obj);
197 if (err) {
198 got_privsep_send_error(&ibuf, err);
199 break;
203 imsg_clear(&ibuf);
204 if (err)
205 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
206 close(GOT_IMSG_FD_CHILD);
207 return err ? 1 : 0;