Blame


1 ad242220 2018-09-08 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 ad242220 2018-09-08 stsp *
4 ad242220 2018-09-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 ad242220 2018-09-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 ad242220 2018-09-08 stsp * copyright notice and this permission notice appear in all copies.
7 ad242220 2018-09-08 stsp *
8 ad242220 2018-09-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ad242220 2018-09-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ad242220 2018-09-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ad242220 2018-09-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ad242220 2018-09-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ad242220 2018-09-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ad242220 2018-09-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ad242220 2018-09-08 stsp */
16 ad242220 2018-09-08 stsp
17 ad242220 2018-09-08 stsp #include <sys/types.h>
18 ad242220 2018-09-08 stsp #include <sys/uio.h>
19 ad242220 2018-09-08 stsp #include <sys/time.h>
20 ad242220 2018-09-08 stsp
21 ad242220 2018-09-08 stsp #include <stdint.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 99437157 2018-11-11 stsp #include <signal.h>
24 ad242220 2018-09-08 stsp #include <stdio.h>
25 ad242220 2018-09-08 stsp #include <stdlib.h>
26 ad242220 2018-09-08 stsp #include <string.h>
27 81a12da5 2020-09-09 naddy #include <unistd.h>
28 ad242220 2018-09-08 stsp #include <zlib.h>
29 ad242220 2018-09-08 stsp
30 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
31 dd038bc6 2021-09-21 thomas.ad
32 ad242220 2018-09-08 stsp #include "got_error.h"
33 ad242220 2018-09-08 stsp #include "got_object.h"
34 3022d272 2019-11-14 stsp #include "got_path.h"
35 ad242220 2018-09-08 stsp
36 ad242220 2018-09-08 stsp #include "got_lib_delta.h"
37 ad242220 2018-09-08 stsp #include "got_lib_inflate.h"
38 ad242220 2018-09-08 stsp #include "got_lib_object.h"
39 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
40 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
41 d5c81d44 2021-07-08 stsp #include "got_lib_sha1.h"
42 ad242220 2018-09-08 stsp
43 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
44 99437157 2018-11-11 stsp
45 99437157 2018-11-11 stsp static void
46 99437157 2018-11-11 stsp catch_sigint(int signo)
47 99437157 2018-11-11 stsp {
48 99437157 2018-11-11 stsp sigint_received = 1;
49 99437157 2018-11-11 stsp }
50 3022d272 2019-11-14 stsp
51 ad242220 2018-09-08 stsp static const struct got_error *
52 78e7b7b8 2022-05-19 thomas read_tree_object(struct got_parsed_tree_entry **entries, int *nentries,
53 d5c81d44 2021-07-08 stsp uint8_t **p, FILE *f, struct got_object_id *expected_id)
54 ad242220 2018-09-08 stsp {
55 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
56 d5c81d44 2021-07-08 stsp struct got_object *obj = NULL;
57 ad242220 2018-09-08 stsp size_t len;
58 d5c81d44 2021-07-08 stsp struct got_inflate_checksum csum;
59 d5c81d44 2021-07-08 stsp SHA1_CTX sha1_ctx;
60 d5c81d44 2021-07-08 stsp struct got_object_id id;
61 ad242220 2018-09-08 stsp
62 d5c81d44 2021-07-08 stsp SHA1Init(&sha1_ctx);
63 d5c81d44 2021-07-08 stsp memset(&csum, 0, sizeof(csum));
64 d5c81d44 2021-07-08 stsp csum.output_sha1 = &sha1_ctx;
65 d5c81d44 2021-07-08 stsp
66 d5c81d44 2021-07-08 stsp err = got_inflate_to_mem(p, &len, NULL, &csum, f);
67 ad242220 2018-09-08 stsp if (err)
68 ad242220 2018-09-08 stsp return err;
69 ad242220 2018-09-08 stsp
70 d5c81d44 2021-07-08 stsp SHA1Final(id.sha1, &sha1_ctx);
71 d5c81d44 2021-07-08 stsp if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
72 d5c81d44 2021-07-08 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
73 d5c81d44 2021-07-08 stsp err = got_error_fmt(GOT_ERR_OBJ_CSUM,
74 d5c81d44 2021-07-08 stsp "checksum failure for object %s",
75 d5c81d44 2021-07-08 stsp got_sha1_digest_to_str(expected_id->sha1, buf,
76 d5c81d44 2021-07-08 stsp sizeof(buf)));
77 d5c81d44 2021-07-08 stsp goto done;
78 d5c81d44 2021-07-08 stsp }
79 d5c81d44 2021-07-08 stsp
80 3022d272 2019-11-14 stsp err = got_object_parse_header(&obj, *p, len);
81 13c729f7 2018-12-24 stsp if (err)
82 d5c81d44 2021-07-08 stsp goto done;
83 13c729f7 2018-12-24 stsp
84 ad242220 2018-09-08 stsp if (len < obj->hdrlen + obj->size) {
85 ad242220 2018-09-08 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
86 ad242220 2018-09-08 stsp goto done;
87 ad242220 2018-09-08 stsp }
88 ad242220 2018-09-08 stsp
89 ad242220 2018-09-08 stsp /* Skip object header. */
90 ad242220 2018-09-08 stsp len -= obj->hdrlen;
91 3022d272 2019-11-14 stsp err = got_object_parse_tree(entries, nentries, *p + obj->hdrlen, len);
92 ad242220 2018-09-08 stsp done:
93 d5c81d44 2021-07-08 stsp if (obj)
94 d5c81d44 2021-07-08 stsp got_object_close(obj);
95 ad242220 2018-09-08 stsp return err;
96 ad242220 2018-09-08 stsp }
97 ad242220 2018-09-08 stsp
98 ad242220 2018-09-08 stsp int
99 ad242220 2018-09-08 stsp main(int argc, char *argv[])
100 ad242220 2018-09-08 stsp {
101 ad242220 2018-09-08 stsp const struct got_error *err = NULL;
102 ad242220 2018-09-08 stsp struct imsgbuf ibuf;
103 d5c81d44 2021-07-08 stsp size_t datalen;
104 ad242220 2018-09-08 stsp
105 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
106 99437157 2018-11-11 stsp
107 ad242220 2018-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
108 ad242220 2018-09-08 stsp
109 2ff12563 2018-09-15 stsp #ifndef PROFILE
110 ad242220 2018-09-08 stsp /* revoke access to most system calls */
111 ad242220 2018-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
112 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
113 ad242220 2018-09-08 stsp got_privsep_send_error(&ibuf, err);
114 ad242220 2018-09-08 stsp return 1;
115 ad242220 2018-09-08 stsp }
116 97799ccd 2022-02-06 thomas
117 97799ccd 2022-02-06 thomas /* revoke fs access */
118 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
119 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
120 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
121 97799ccd 2022-02-06 thomas return 1;
122 97799ccd 2022-02-06 thomas }
123 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
124 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
125 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
126 5d120ea8 2022-06-23 op return 1;
127 5d120ea8 2022-06-23 op }
128 2ff12563 2018-09-15 stsp #endif
129 ad242220 2018-09-08 stsp
130 656b1f76 2019-05-11 jcs for (;;) {
131 ad242220 2018-09-08 stsp struct imsg imsg;
132 ad242220 2018-09-08 stsp FILE *f = NULL;
133 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *entries = NULL;
134 3022d272 2019-11-14 stsp int nentries = 0;
135 3022d272 2019-11-14 stsp uint8_t *buf = NULL;
136 d5c81d44 2021-07-08 stsp struct got_object_id expected_id;
137 99437157 2018-11-11 stsp
138 99437157 2018-11-11 stsp if (sigint_received) {
139 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
140 99437157 2018-11-11 stsp break;
141 99437157 2018-11-11 stsp }
142 230a42bd 2019-05-11 jcs
143 ad242220 2018-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
144 ad242220 2018-09-08 stsp if (err) {
145 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
146 ad242220 2018-09-08 stsp err = NULL;
147 ad242220 2018-09-08 stsp break;
148 ad242220 2018-09-08 stsp }
149 ad242220 2018-09-08 stsp
150 ad242220 2018-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
151 ad242220 2018-09-08 stsp break;
152 ad242220 2018-09-08 stsp
153 ad242220 2018-09-08 stsp if (imsg.hdr.type != GOT_IMSG_TREE_REQUEST) {
154 ad242220 2018-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
155 ad242220 2018-09-08 stsp goto done;
156 ad242220 2018-09-08 stsp }
157 ad242220 2018-09-08 stsp
158 d5c81d44 2021-07-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
159 d5c81d44 2021-07-08 stsp if (datalen != sizeof(expected_id)) {
160 d5c81d44 2021-07-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
161 d5c81d44 2021-07-08 stsp goto done;
162 d5c81d44 2021-07-08 stsp }
163 d5c81d44 2021-07-08 stsp memcpy(&expected_id, imsg.data, sizeof(expected_id));
164 d5c81d44 2021-07-08 stsp
165 ad242220 2018-09-08 stsp if (imsg.fd == -1) {
166 ad242220 2018-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
167 ad242220 2018-09-08 stsp goto done;
168 ad242220 2018-09-08 stsp }
169 ad242220 2018-09-08 stsp
170 ad242220 2018-09-08 stsp /* Always assume file offset zero. */
171 ad242220 2018-09-08 stsp f = fdopen(imsg.fd, "rb");
172 ad242220 2018-09-08 stsp if (f == NULL) {
173 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
174 ad242220 2018-09-08 stsp goto done;
175 ad242220 2018-09-08 stsp }
176 ad242220 2018-09-08 stsp
177 d5c81d44 2021-07-08 stsp err = read_tree_object(&entries, &nentries, &buf, f,
178 d5c81d44 2021-07-08 stsp &expected_id);
179 ad242220 2018-09-08 stsp if (err)
180 ad242220 2018-09-08 stsp goto done;
181 ad242220 2018-09-08 stsp
182 78e7b7b8 2022-05-19 thomas err = got_privsep_send_tree(&ibuf, entries, nentries);
183 ad242220 2018-09-08 stsp done:
184 78e7b7b8 2022-05-19 thomas free(entries);
185 3022d272 2019-11-14 stsp free(buf);
186 fb43ecf1 2019-02-11 stsp if (f) {
187 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
188 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
189 3a6ce05a 2019-02-11 stsp } else if (imsg.fd != -1) {
190 08578a35 2021-01-22 stsp if (close(imsg.fd) == -1 && err == NULL)
191 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
192 3a6ce05a 2019-02-11 stsp }
193 ad242220 2018-09-08 stsp imsg_free(&imsg);
194 99437157 2018-11-11 stsp if (err)
195 ad242220 2018-09-08 stsp break;
196 ad242220 2018-09-08 stsp }
197 ad242220 2018-09-08 stsp
198 ad242220 2018-09-08 stsp imsg_clear(&ibuf);
199 99437157 2018-11-11 stsp if (err) {
200 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
201 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
202 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
203 80d5f134 2018-11-11 stsp }
204 99437157 2018-11-11 stsp }
205 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
206 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
207 ad242220 2018-09-08 stsp return err ? 1 : 0;
208 ad242220 2018-09-08 stsp }