add ssh -i identity-file support to commands which use the network
[got-portable.git] / libexec / got-read-blob / got-read-blob.c
blob422e04b7301dc1eabacadbf80babcfbf4a4a780c
1 /*
2 * Copyright (c) 2018, 2019 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>
19 #include <sys/uio.h>
20 #include <sys/time.h>
22 #include <err.h>
23 #include <stdint.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <zlib.h>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_hash.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
44 static volatile sig_atomic_t sigint_received;
46 static void
47 catch_sigint(int signo)
49 sigint_received = 1;
52 int
53 main(int argc, char *argv[])
55 const struct got_error *err = NULL;
56 struct imsgbuf ibuf;
57 size_t datalen;
59 signal(SIGINT, catch_sigint);
61 if (imsgbuf_init(&ibuf, GOT_IMSG_FD_CHILD) == -1) {
62 warn("imsgbuf_init");
63 return 1;
65 imsgbuf_allow_fdpass(&ibuf);
67 #ifndef PROFILE
68 /* revoke access to most system calls */
69 if (pledge("stdio recvfd", NULL) == -1) {
70 err = got_error_from_errno("pledge");
71 got_privsep_send_error(&ibuf, err);
72 imsgbuf_clear(&ibuf);
73 return 1;
76 /* revoke fs access */
77 if (landlock_no_fs() == -1) {
78 err = got_error_from_errno("landlock_no_fs");
79 got_privsep_send_error(&ibuf, err);
80 return 1;
82 if (cap_enter() == -1) {
83 err = got_error_from_errno("cap_enter");
84 got_privsep_send_error(&ibuf, err);
85 return 1;
87 #endif
89 for (;;) {
90 struct imsg imsg, imsg_outfd;
91 FILE *f = NULL;
92 int fd = -1, outfd = -1;
93 size_t size;
94 struct got_object *obj = NULL;
95 uint8_t *buf = NULL;
96 struct got_object_id id;
97 struct got_object_id expected_id;
98 struct got_inflate_checksum csum;
99 struct got_hash ctx;
101 memset(&imsg, 0, sizeof(imsg));
102 memset(&imsg_outfd, 0, sizeof(imsg_outfd));
104 if (sigint_received) {
105 err = got_error(GOT_ERR_CANCELLED);
106 break;
109 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
110 if (err) {
111 if (err->code == GOT_ERR_PRIVSEP_PIPE)
112 err = NULL;
113 break;
116 if (imsg.hdr.type == GOT_IMSG_STOP)
117 break;
119 if (imsg.hdr.type != GOT_IMSG_BLOB_REQUEST) {
120 err = got_error(GOT_ERR_PRIVSEP_MSG);
121 goto done;
124 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
125 if (datalen != sizeof(expected_id)) {
126 err = got_error(GOT_ERR_PRIVSEP_LEN);
127 goto done;
129 memcpy(&expected_id, imsg.data, sizeof(expected_id));
131 fd = imsg_get_fd(&imsg);
132 if (fd == -1) {
133 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
134 goto done;
137 err = got_privsep_recv_imsg(&imsg_outfd, &ibuf, 0);
138 if (err) {
139 if (imsg.hdr.len == 0)
140 err = NULL;
141 break;
144 if (imsg_outfd.hdr.type == GOT_IMSG_STOP)
145 break;
147 if (imsg_outfd.hdr.type != GOT_IMSG_BLOB_OUTFD) {
148 err = got_error(GOT_ERR_PRIVSEP_MSG);
149 goto done;
152 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
153 if (datalen != 0) {
154 err = got_error(GOT_ERR_PRIVSEP_LEN);
155 goto done;
157 outfd = imsg_get_fd(&imsg_outfd);
158 if (outfd == -1) {
159 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
160 goto done;
163 err = got_object_read_header(&obj, fd);
164 if (err)
165 goto done;
167 if (lseek(fd, SEEK_SET, 0) == -1) {
168 err = got_error_from_errno("lseek");
169 goto done;
172 f = fdopen(fd, "rb");
173 if (f == NULL) {
174 err = got_error_from_errno("fdopen");
175 goto done;
177 fd = -1;
179 got_hash_init(&ctx, expected_id.algo);
180 memset(&csum, 0, sizeof(csum));
181 csum.output_ctx = &ctx;
183 if (obj->size + obj->hdrlen <=
184 GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
185 err = got_inflate_to_mem(&buf, &size, NULL, &csum, f);
186 if (err)
187 goto done;
188 } else {
189 err = got_inflate_to_fd(&size, f, &csum, outfd);
190 if (err)
191 goto done;
193 got_hash_final_object_id(&ctx, &id);
194 if (got_object_id_cmp(&expected_id, &id) != 0) {
195 err = got_error_checksum(&expected_id);
196 goto done;
199 if (size < obj->hdrlen) {
200 err = got_error(GOT_ERR_BAD_OBJ_HDR);
201 goto done;
204 err = got_privsep_send_blob(&ibuf, size, obj->hdrlen, buf);
205 done:
206 free(buf);
207 if (f && fclose(f) == EOF && err == NULL)
208 err = got_error_from_errno("fclose");
209 if (fd != -1 && close(fd) == -1 && err == NULL)
210 err = got_error_from_errno("close");
211 if (outfd != -1 && close(outfd) == -1 && err == NULL)
212 err = got_error_from_errno("close");
214 imsg_free(&imsg);
215 imsg_free(&imsg_outfd);
216 if (obj)
217 got_object_close(obj);
218 if (err)
219 break;
222 if (err) {
223 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
224 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
225 got_privsep_send_error(&ibuf, err);
228 imsgbuf_clear(&ibuf);
229 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
230 err = got_error_from_errno("close");
231 return err ? 1 : 0;