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"
49 static volatile sig_atomic_t sigint_received;
52 catch_sigint(int signo)
57 static const struct got_error *
58 parse_object_header(struct got_object **obj, char *buf, size_t len)
60 const char *obj_tags[] = {
65 const int obj_types[] = {
71 size_t size = 0, hdrlen = 0;
73 char *p = strchr(buf, '\0');
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);
85 if (strncmp(buf, tag, tlen) != 0)
90 return got_error(GOT_ERR_BAD_OBJ_HDR);
91 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
93 return got_error(GOT_ERR_BAD_OBJ_HDR);
98 return got_error(GOT_ERR_BAD_OBJ_HDR);
100 *obj = calloc(1, sizeof(**obj));
102 return got_error_from_errno();
104 (*obj)->hdrlen = hdrlen;
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;
115 const size_t zbsize = 64;
116 size_t outlen, totlen;
119 buf = malloc(zbsize);
121 return got_error_from_errno();
123 err = got_inflate_init(&zb, buf, zbsize);
129 err = got_inflate_read_fd(&zb, fd, &outlen);
135 if (strchr(zb.outbuf, '\0') == NULL) {
138 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
139 if (newbuf == NULL) {
140 err = got_error_from_errno();
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);
152 got_inflate_end(&zb);
158 main(int argc, char *argv[])
160 const struct got_error *err = NULL;
161 struct got_object *obj = NULL;
166 signal(SIGINT, catch_sigint);
168 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
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);
180 if (sigint_received) {
181 err = got_error(GOT_ERR_CANCELLED);
185 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
187 if (err->code == GOT_ERR_PRIVSEP_PIPE)
192 if (imsg.hdr.type == GOT_IMSG_STOP)
195 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
196 err = got_error(GOT_ERR_PRIVSEP_MSG);
200 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
202 err = got_error(GOT_ERR_PRIVSEP_LEN);
206 err = read_object_header(&obj, imsg.fd);
210 err = got_privsep_send_obj(&ibuf, obj);
215 got_object_close(obj);
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);