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"
49 static volatile sig_atomic_t sigint_received;
51 static void
52 catch_sigint(int signo)
53 {
54 sigint_received = 1;
55 }
57 static const struct got_error *
58 parse_object_header(struct got_object **obj, char *buf, size_t len)
59 {
60 const char *obj_tags[] = {
61 GOT_OBJ_TAG_COMMIT,
62 GOT_OBJ_TAG_TREE,
63 GOT_OBJ_TAG_BLOB
64 };
65 const int obj_types[] = {
66 GOT_OBJ_TYPE_COMMIT,
67 GOT_OBJ_TYPE_TREE,
68 GOT_OBJ_TYPE_BLOB,
69 };
70 int type = 0;
71 size_t size = 0, hdrlen = 0;
72 int i;
73 char *p = strchr(buf, '\0');
75 if (p == NULL)
76 return got_error(GOT_ERR_BAD_OBJ_HDR);
78 hdrlen = strlen(buf) + 1 /* '\0' */;
80 for (i = 0; i < nitems(obj_tags); i++) {
81 const char *tag = obj_tags[i];
82 size_t tlen = strlen(tag);
83 const char *errstr;
85 if (strncmp(buf, tag, tlen) != 0)
86 continue;
88 type = obj_types[i];
89 if (len <= tlen)
90 return got_error(GOT_ERR_BAD_OBJ_HDR);
91 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
92 if (errstr != NULL)
93 return got_error(GOT_ERR_BAD_OBJ_HDR);
94 break;
95 }
97 if (type == 0)
98 return got_error(GOT_ERR_BAD_OBJ_HDR);
100 *obj = calloc(1, sizeof(**obj));
101 if (*obj == NULL)
102 return got_error_from_errno();
103 (*obj)->type = type;
104 (*obj)->hdrlen = hdrlen;
105 (*obj)->size = size;
106 return NULL;
109 static const struct got_error *
110 read_object_header(struct got_object **obj, int fd)
112 const struct got_error *err;
113 struct got_zstream_buf zb;
114 char *buf;
115 const size_t zbsize = 64;
116 size_t outlen, totlen;
117 int nbuf = 1;
119 buf = malloc(zbsize);
120 if (buf == NULL)
121 return got_error_from_errno();
123 err = got_inflate_init(&zb, buf, zbsize);
124 if (err)
125 return err;
127 totlen = 0;
128 do {
129 err = got_inflate_read_fd(&zb, fd, &outlen);
130 if (err)
131 goto done;
132 if (outlen == 0)
133 break;
134 totlen += outlen;
135 if (strchr(zb.outbuf, '\0') == NULL) {
136 char *newbuf;
137 nbuf++;
138 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
139 if (newbuf == NULL) {
140 err = got_error_from_errno();
141 goto done;
143 buf = newbuf;
144 zb.outbuf = newbuf + totlen;
145 zb.outlen = (nbuf * zbsize) - totlen;
147 } while (strchr(zb.outbuf, '\0') == NULL);
149 err = parse_object_header(obj, buf, totlen);
150 done:
151 free(buf);
152 got_inflate_end(&zb);
153 return err;
157 int
158 main(int argc, char *argv[])
160 const struct got_error *err = NULL;
161 struct got_object *obj = NULL;
162 struct imsg imsg;
163 struct imsgbuf ibuf;
164 size_t datalen;
166 signal(SIGINT, catch_sigint);
168 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
170 #ifndef PROFILE
171 /* revoke access to most system calls */
172 if (pledge("stdio recvfd", NULL) == -1) {
173 err = got_error_from_errno();
174 got_privsep_send_error(&ibuf, err);
175 return 1;
177 #endif
179 while (1) {
180 if (sigint_received) {
181 err = got_error(GOT_ERR_CANCELLED);
182 break;
185 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
186 if (err) {
187 if (err->code == GOT_ERR_PRIVSEP_PIPE)
188 err = NULL;
189 break;
192 if (imsg.hdr.type == GOT_IMSG_STOP)
193 break;
195 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
196 err = got_error(GOT_ERR_PRIVSEP_MSG);
197 goto done;
200 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
201 if (datalen != 0) {
202 err = got_error(GOT_ERR_PRIVSEP_LEN);
203 goto done;
206 err = read_object_header(&obj, imsg.fd);
207 if (err)
208 goto done;
210 err = got_privsep_send_obj(&ibuf, obj);
211 done:
212 close(imsg.fd);
213 imsg_free(&imsg);
214 if (obj)
215 got_object_close(obj);
216 if (err)
217 break;
220 imsg_clear(&ibuf);
221 if (err) {
222 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
223 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
224 got_privsep_send_error(&ibuf, err);
227 close(GOT_IMSG_FD_CHILD);
228 return err ? 1 : 0;