2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.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"
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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;
53 catch_sigint(int signo)
58 static const struct got_error *
59 parse_object_header(struct got_object **obj, char *buf, size_t len)
61 const char *obj_tags[] = {
67 const int obj_types[] = {
74 size_t size = 0, hdrlen = 0;
76 char *p = strchr(buf, '\0');
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);
90 if (strncmp(buf, tag, tlen) != 0)
95 return got_error(GOT_ERR_BAD_OBJ_HDR);
96 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
98 return got_error(GOT_ERR_BAD_OBJ_HDR);
103 return got_error(GOT_ERR_BAD_OBJ_HDR);
105 *obj = calloc(1, sizeof(**obj));
107 return got_error_from_errno();
109 (*obj)->hdrlen = hdrlen;
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;
120 const size_t zbsize = 64;
121 size_t outlen, totlen;
126 buf = malloc(zbsize);
128 return got_error_from_errno();
130 err = got_inflate_init(&zb, buf, zbsize);
136 err = got_inflate_read_fd(&zb, fd, &outlen);
142 if (strchr(zb.outbuf, '\0') == NULL) {
145 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
146 if (newbuf == NULL) {
147 err = got_error_from_errno();
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);
159 got_inflate_end(&zb);
165 main(int argc, char *argv[])
167 const struct got_error *err = NULL;
168 struct got_object *obj = NULL;
173 signal(SIGINT, catch_sigint);
175 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
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);
187 if (sigint_received) {
188 err = got_error(GOT_ERR_CANCELLED);
192 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
194 if (err->code == GOT_ERR_PRIVSEP_PIPE)
199 if (imsg.hdr.type == GOT_IMSG_STOP)
202 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
203 err = got_error(GOT_ERR_PRIVSEP_MSG);
207 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
209 err = got_error(GOT_ERR_PRIVSEP_LEN);
213 err = read_object_header(&obj, imsg.fd);
217 err = got_privsep_send_obj(&ibuf, obj);
222 got_object_close(obj);
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);