Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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/uio.h>
19 #include <sys/time.h>
21 #include <stdint.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_parse.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_sha1.h"
43 static volatile sig_atomic_t sigint_received;
45 static void
46 catch_sigint(int signo)
47 {
48 sigint_received = 1;
49 }
51 static const struct got_error *
52 read_tree_object(struct got_parsed_tree_entry **entries, int *nentries,
53 uint8_t **p, FILE *f, struct got_object_id *expected_id)
54 {
55 const struct got_error *err = NULL;
56 struct got_object *obj = NULL;
57 size_t len;
58 struct got_inflate_checksum csum;
59 SHA1_CTX sha1_ctx;
60 struct got_object_id id;
62 SHA1Init(&sha1_ctx);
63 memset(&csum, 0, sizeof(csum));
64 csum.output_sha1 = &sha1_ctx;
66 err = got_inflate_to_mem(p, &len, NULL, &csum, f);
67 if (err)
68 return err;
70 SHA1Final(id.sha1, &sha1_ctx);
71 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
72 char buf[SHA1_DIGEST_STRING_LENGTH];
73 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
74 "checksum failure for object %s",
75 got_sha1_digest_to_str(expected_id->sha1, buf,
76 sizeof(buf)));
77 goto done;
78 }
80 err = got_object_parse_header(&obj, *p, len);
81 if (err)
82 goto done;
84 if (len < obj->hdrlen + obj->size) {
85 err = got_error(GOT_ERR_BAD_OBJ_DATA);
86 goto done;
87 }
89 /* Skip object header. */
90 len -= obj->hdrlen;
91 err = got_object_parse_tree(entries, nentries, *p + obj->hdrlen, len);
92 done:
93 if (obj)
94 got_object_close(obj);
95 return err;
96 }
98 int
99 main(int argc, char *argv[])
101 const struct got_error *err = NULL;
102 struct imsgbuf ibuf;
103 size_t datalen;
105 signal(SIGINT, catch_sigint);
107 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
109 #ifndef PROFILE
110 /* revoke access to most system calls */
111 if (pledge("stdio recvfd", NULL) == -1) {
112 err = got_error_from_errno("pledge");
113 got_privsep_send_error(&ibuf, err);
114 return 1;
117 /* revoke fs access */
118 if (landlock_no_fs() == -1) {
119 err = got_error_from_errno("landlock_no_fs");
120 got_privsep_send_error(&ibuf, err);
121 return 1;
123 if (cap_enter() == -1) {
124 err = got_error_from_errno("cap_enter");
125 got_privsep_send_error(&ibuf, err);
126 return 1;
128 #endif
130 for (;;) {
131 struct imsg imsg;
132 FILE *f = NULL;
133 struct got_parsed_tree_entry *entries = NULL;
134 int nentries = 0;
135 uint8_t *buf = NULL;
136 struct got_object_id expected_id;
138 if (sigint_received) {
139 err = got_error(GOT_ERR_CANCELLED);
140 break;
143 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
144 if (err) {
145 if (err->code == GOT_ERR_PRIVSEP_PIPE)
146 err = NULL;
147 break;
150 if (imsg.hdr.type == GOT_IMSG_STOP)
151 break;
153 if (imsg.hdr.type != GOT_IMSG_TREE_REQUEST) {
154 err = got_error(GOT_ERR_PRIVSEP_MSG);
155 goto done;
158 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
159 if (datalen != sizeof(expected_id)) {
160 err = got_error(GOT_ERR_PRIVSEP_LEN);
161 goto done;
163 memcpy(&expected_id, imsg.data, sizeof(expected_id));
165 if (imsg.fd == -1) {
166 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
167 goto done;
170 /* Always assume file offset zero. */
171 f = fdopen(imsg.fd, "rb");
172 if (f == NULL) {
173 err = got_error_from_errno("fdopen");
174 goto done;
177 err = read_tree_object(&entries, &nentries, &buf, f,
178 &expected_id);
179 if (err)
180 goto done;
182 err = got_privsep_send_tree(&ibuf, entries, nentries);
183 done:
184 free(entries);
185 free(buf);
186 if (f) {
187 if (fclose(f) == EOF && err == NULL)
188 err = got_error_from_errno("fclose");
189 } else if (imsg.fd != -1) {
190 if (close(imsg.fd) == -1 && err == NULL)
191 err = got_error_from_errno("close");
193 imsg_free(&imsg);
194 if (err)
195 break;
198 imsg_clear(&ibuf);
199 if (err) {
200 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
201 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
202 got_privsep_send_error(&ibuf, err);
205 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
206 err = got_error_from_errno("close");
207 return err ? 1 : 0;