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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_object.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #endif
44 #define GOT_OBJ_TAG_COMMIT "commit"
45 #define GOT_OBJ_TAG_TREE "tree"
46 #define GOT_OBJ_TAG_BLOB "blob"
48 static const struct got_error *
49 parse_object_header(struct got_object **obj, char *buf, size_t len)
50 {
51 const char *obj_tags[] = {
52 GOT_OBJ_TAG_COMMIT,
53 GOT_OBJ_TAG_TREE,
54 GOT_OBJ_TAG_BLOB
55 };
56 const int obj_types[] = {
57 GOT_OBJ_TYPE_COMMIT,
58 GOT_OBJ_TYPE_TREE,
59 GOT_OBJ_TYPE_BLOB,
60 };
61 int type = 0;
62 size_t size = 0, hdrlen = 0;
63 int i;
64 char *p = strchr(buf, '\0');
66 if (p == NULL)
67 return got_error(GOT_ERR_BAD_OBJ_HDR);
69 hdrlen = strlen(buf) + 1 /* '\0' */;
71 for (i = 0; i < nitems(obj_tags); i++) {
72 const char *tag = obj_tags[i];
73 size_t tlen = strlen(tag);
74 const char *errstr;
76 if (strncmp(buf, tag, tlen) != 0)
77 continue;
79 type = obj_types[i];
80 if (len <= tlen)
81 return got_error(GOT_ERR_BAD_OBJ_HDR);
82 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
83 if (errstr != NULL)
84 return got_error(GOT_ERR_BAD_OBJ_HDR);
85 break;
86 }
88 if (type == 0)
89 return got_error(GOT_ERR_BAD_OBJ_HDR);
91 *obj = calloc(1, sizeof(**obj));
92 if (*obj == NULL)
93 return got_error_from_errno();
94 (*obj)->type = type;
95 (*obj)->hdrlen = hdrlen;
96 (*obj)->size = size;
97 return NULL;
98 }
100 static const struct got_error *
101 read_object_header(struct got_object **obj, int fd)
103 const struct got_error *err;
104 struct got_zstream_buf zb;
105 char *buf;
106 const size_t zbsize = 64;
107 size_t outlen, totlen;
108 int nbuf = 1;
110 buf = malloc(zbsize);
111 if (buf == NULL)
112 return got_error_from_errno();
114 err = got_inflate_init(&zb, buf, zbsize);
115 if (err)
116 return err;
118 totlen = 0;
119 do {
120 err = got_inflate_read_fd(&zb, fd, &outlen);
121 if (err)
122 goto done;
123 if (outlen == 0)
124 break;
125 totlen += outlen;
126 if (strchr(zb.outbuf, '\0') == NULL) {
127 char *newbuf;
128 nbuf++;
129 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
130 if (newbuf == NULL) {
131 err = got_error_from_errno();
132 goto done;
134 buf = newbuf;
135 zb.outbuf = newbuf + totlen;
136 zb.outlen = (nbuf * zbsize) - totlen;
138 } while (strchr(zb.outbuf, '\0') == NULL);
140 err = parse_object_header(obj, buf, totlen);
141 done:
142 free(buf);
143 got_inflate_end(&zb);
144 return err;
148 int
149 main(int argc, char *argv[])
151 const struct got_error *err = NULL;
152 struct got_object *obj = NULL;
153 struct imsg imsg;
154 struct imsgbuf ibuf;
155 size_t datalen;
157 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
159 /* revoke access to most system calls */
160 if (pledge("stdio recvfd", NULL) == -1) {
161 err = got_error_from_errno();
162 got_privsep_send_error(&ibuf, err);
163 return 1;
166 while (1) {
167 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
168 if (err) {
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
170 err = NULL;
171 break;
174 if (imsg.hdr.type == GOT_IMSG_STOP)
175 break;
177 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
178 err = got_error(GOT_ERR_PRIVSEP_MSG);
179 goto done;
182 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
183 if (datalen != 0) {
184 err = got_error(GOT_ERR_PRIVSEP_LEN);
185 goto done;
188 err = read_object_header(&obj, imsg.fd);
189 if (err)
190 goto done;
192 err = got_privsep_send_obj(&ibuf, obj);
193 done:
194 close(imsg.fd);
195 imsg_free(&imsg);
196 if (obj)
197 got_object_close(obj);
198 if (err) {
199 if (err->code == GOT_ERR_PRIVSEP_PIPE)
200 err = NULL;
201 else
202 got_privsep_send_error(&ibuf, err);
203 break;
207 imsg_clear(&ibuf);
208 if (err)
209 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
210 close(GOT_IMSG_FD_CHILD);
211 return err ? 1 : 0;