Blame


1 09876a9d 2024-04-25 thomas.ad /*
2 452122e2 2024-04-25 thomas.ad * Copyright (c) 2024 Tobias Heider <me@tobhe.de>
3 09876a9d 2024-04-25 thomas.ad * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 09876a9d 2024-04-25 thomas.ad *
5 09876a9d 2024-04-25 thomas.ad * Permission to use, copy, modify, and distribute this software for any
6 09876a9d 2024-04-25 thomas.ad * purpose with or without fee is hereby granted, provided that the above
7 09876a9d 2024-04-25 thomas.ad * copyright notice and this permission notice appear in all copies.
8 09876a9d 2024-04-25 thomas.ad *
9 09876a9d 2024-04-25 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 09876a9d 2024-04-25 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 09876a9d 2024-04-25 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 09876a9d 2024-04-25 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 09876a9d 2024-04-25 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 09876a9d 2024-04-25 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 09876a9d 2024-04-25 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 09876a9d 2024-04-25 thomas.ad */
17 09876a9d 2024-04-25 thomas.ad
18 09876a9d 2024-04-25 thomas.ad #include <sys/types.h>
19 09876a9d 2024-04-25 thomas.ad #include <sys/socket.h>
20 09876a9d 2024-04-25 thomas.ad
21 09876a9d 2024-04-25 thomas.ad #include <err.h>
22 09876a9d 2024-04-25 thomas.ad #include <errno.h>
23 09876a9d 2024-04-25 thomas.ad #include <limits.h>
24 09876a9d 2024-04-25 thomas.ad #include <netdb.h>
25 09876a9d 2024-04-25 thomas.ad #include <poll.h>
26 09876a9d 2024-04-25 thomas.ad #include <stdio.h>
27 09876a9d 2024-04-25 thomas.ad #include <stdlib.h>
28 09876a9d 2024-04-25 thomas.ad #include <string.h>
29 09876a9d 2024-04-25 thomas.ad #include <tls.h>
30 09876a9d 2024-04-25 thomas.ad #include <unistd.h>
31 09876a9d 2024-04-25 thomas.ad
32 507e5154 2024-04-25 thomas.ad #include "got_error.h"
33 09876a9d 2024-04-25 thomas.ad #include "got_version.h"
34 09876a9d 2024-04-25 thomas.ad
35 a36700cf 2024-04-25 thomas.ad #include "got_lib_pkt.h"
36 a36700cf 2024-04-25 thomas.ad
37 452122e2 2024-04-25 thomas.ad #include "bufio.h"
38 452122e2 2024-04-25 thomas.ad
39 09876a9d 2024-04-25 thomas.ad #define UPLOAD_PACK_ADV "application/x-git-upload-pack-advertisement"
40 09876a9d 2024-04-25 thomas.ad #define UPLOAD_PACK_REQ "application/x-git-upload-pack-request"
41 09876a9d 2024-04-25 thomas.ad #define UPLOAD_PACK_RES "application/x-git-upload-pack-result"
42 09876a9d 2024-04-25 thomas.ad
43 09876a9d 2024-04-25 thomas.ad #define GOT_USERAGENT "got/" GOT_VERSION_STR
44 09876a9d 2024-04-25 thomas.ad #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
45 09876a9d 2024-04-25 thomas.ad #define hasprfx(str, p) (strncasecmp(str, p, strlen(p)) == 0)
46 09876a9d 2024-04-25 thomas.ad
47 09876a9d 2024-04-25 thomas.ad FILE *tmp;
48 09876a9d 2024-04-25 thomas.ad
49 09876a9d 2024-04-25 thomas.ad static int verbose;
50 09876a9d 2024-04-25 thomas.ad
51 452122e2 2024-04-25 thomas.ad static char *
52 452122e2 2024-04-25 thomas.ad bufio_getdelim_sync(struct bufio *bio, const char *nl, size_t *len)
53 452122e2 2024-04-25 thomas.ad {
54 452122e2 2024-04-25 thomas.ad int r;
55 452122e2 2024-04-25 thomas.ad
56 452122e2 2024-04-25 thomas.ad do {
57 452122e2 2024-04-25 thomas.ad r = bufio_read(bio);
58 452122e2 2024-04-25 thomas.ad if (r == -1 && errno != EAGAIN)
59 452122e2 2024-04-25 thomas.ad errx(1, "bufio_read: %s", bufio_io_err(bio));
60 452122e2 2024-04-25 thomas.ad } while (r == -1 && errno == EAGAIN);
61 452122e2 2024-04-25 thomas.ad return buf_getdelim(&bio->rbuf, nl, len);
62 452122e2 2024-04-25 thomas.ad }
63 452122e2 2024-04-25 thomas.ad
64 452122e2 2024-04-25 thomas.ad static size_t
65 452122e2 2024-04-25 thomas.ad bufio_drain_sync(struct bufio *bio, void *d, size_t len)
66 452122e2 2024-04-25 thomas.ad {
67 452122e2 2024-04-25 thomas.ad int r;
68 452122e2 2024-04-25 thomas.ad
69 452122e2 2024-04-25 thomas.ad do {
70 452122e2 2024-04-25 thomas.ad r = bufio_read(bio);
71 452122e2 2024-04-25 thomas.ad if (r == -1 && errno != EAGAIN)
72 452122e2 2024-04-25 thomas.ad errx(1, "bufio_read: %s", bufio_io_err(bio));
73 452122e2 2024-04-25 thomas.ad } while (r == -1 && errno == EAGAIN);
74 452122e2 2024-04-25 thomas.ad return bufio_drain(bio, d, len);
75 452122e2 2024-04-25 thomas.ad }
76 452122e2 2024-04-25 thomas.ad
77 452122e2 2024-04-25 thomas.ad static void
78 452122e2 2024-04-25 thomas.ad bufio_close_sync(struct bufio *bio)
79 452122e2 2024-04-25 thomas.ad {
80 452122e2 2024-04-25 thomas.ad int r;
81 452122e2 2024-04-25 thomas.ad
82 452122e2 2024-04-25 thomas.ad do {
83 452122e2 2024-04-25 thomas.ad r = bufio_close(bio);
84 452122e2 2024-04-25 thomas.ad if (r == -1 && errno == EAGAIN)
85 452122e2 2024-04-25 thomas.ad errx(1, "bufio_read: %s", bufio_io_err(bio));
86 452122e2 2024-04-25 thomas.ad } while (r == -1 && errno == EAGAIN);
87 452122e2 2024-04-25 thomas.ad }
88 452122e2 2024-04-25 thomas.ad
89 09876a9d 2024-04-25 thomas.ad static long long
90 09876a9d 2024-04-25 thomas.ad hexstrtonum(const char *str, long long min, long long max, const char **errstr)
91 09876a9d 2024-04-25 thomas.ad {
92 09876a9d 2024-04-25 thomas.ad long long lval;
93 09876a9d 2024-04-25 thomas.ad char *cp;
94 09876a9d 2024-04-25 thomas.ad
95 09876a9d 2024-04-25 thomas.ad errno = 0;
96 09876a9d 2024-04-25 thomas.ad lval = strtoll(str, &cp, 16);
97 09876a9d 2024-04-25 thomas.ad if (*str == '\0' || *cp != '\0') {
98 09876a9d 2024-04-25 thomas.ad *errstr = "not a number";
99 09876a9d 2024-04-25 thomas.ad return 0;
100 09876a9d 2024-04-25 thomas.ad }
101 09876a9d 2024-04-25 thomas.ad if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
102 09876a9d 2024-04-25 thomas.ad lval < min || lval > max) {
103 09876a9d 2024-04-25 thomas.ad *errstr = "out of range";
104 09876a9d 2024-04-25 thomas.ad return 0;
105 09876a9d 2024-04-25 thomas.ad }
106 09876a9d 2024-04-25 thomas.ad
107 09876a9d 2024-04-25 thomas.ad *errstr = NULL;
108 09876a9d 2024-04-25 thomas.ad return lval;
109 09876a9d 2024-04-25 thomas.ad }
110 09876a9d 2024-04-25 thomas.ad
111 09876a9d 2024-04-25 thomas.ad static int
112 09876a9d 2024-04-25 thomas.ad dial(int https, const char *host, const char *port)
113 09876a9d 2024-04-25 thomas.ad {
114 09876a9d 2024-04-25 thomas.ad struct addrinfo hints, *res, *res0;
115 452122e2 2024-04-25 thomas.ad int error, saved_errno, fd = -1;
116 09876a9d 2024-04-25 thomas.ad const char *cause = NULL;
117 09876a9d 2024-04-25 thomas.ad
118 09876a9d 2024-04-25 thomas.ad memset(&hints, 0, sizeof(hints));
119 09876a9d 2024-04-25 thomas.ad hints.ai_family = AF_UNSPEC;
120 09876a9d 2024-04-25 thomas.ad hints.ai_socktype = SOCK_STREAM;
121 09876a9d 2024-04-25 thomas.ad error = getaddrinfo(host, port, &hints, &res0);
122 09876a9d 2024-04-25 thomas.ad if (error) {
123 09876a9d 2024-04-25 thomas.ad warnx("%s", gai_strerror(error));
124 452122e2 2024-04-25 thomas.ad return -1;
125 09876a9d 2024-04-25 thomas.ad }
126 09876a9d 2024-04-25 thomas.ad
127 09876a9d 2024-04-25 thomas.ad for (res = res0; res; res = res->ai_next) {
128 09876a9d 2024-04-25 thomas.ad fd = socket(res->ai_family, res->ai_socktype,
129 09876a9d 2024-04-25 thomas.ad res->ai_protocol);
130 09876a9d 2024-04-25 thomas.ad if (fd == -1) {
131 09876a9d 2024-04-25 thomas.ad cause = "socket";
132 09876a9d 2024-04-25 thomas.ad continue;
133 09876a9d 2024-04-25 thomas.ad }
134 09876a9d 2024-04-25 thomas.ad
135 09876a9d 2024-04-25 thomas.ad if (connect(fd, res->ai_addr, res->ai_addrlen) == 0)
136 09876a9d 2024-04-25 thomas.ad break;
137 09876a9d 2024-04-25 thomas.ad
138 09876a9d 2024-04-25 thomas.ad cause = "connect";
139 09876a9d 2024-04-25 thomas.ad saved_errno = errno;
140 09876a9d 2024-04-25 thomas.ad close(fd);
141 09876a9d 2024-04-25 thomas.ad fd = -1;
142 09876a9d 2024-04-25 thomas.ad errno = saved_errno;
143 09876a9d 2024-04-25 thomas.ad }
144 09876a9d 2024-04-25 thomas.ad freeaddrinfo(res0);
145 09876a9d 2024-04-25 thomas.ad
146 09876a9d 2024-04-25 thomas.ad if (fd == -1) {
147 09876a9d 2024-04-25 thomas.ad warn("%s", cause);
148 452122e2 2024-04-25 thomas.ad return -1;
149 09876a9d 2024-04-25 thomas.ad }
150 09876a9d 2024-04-25 thomas.ad
151 452122e2 2024-04-25 thomas.ad return fd;
152 09876a9d 2024-04-25 thomas.ad }
153 09876a9d 2024-04-25 thomas.ad
154 452122e2 2024-04-25 thomas.ad static int
155 452122e2 2024-04-25 thomas.ad http_open(struct bufio *bio, int https, const char *method, const char *host, const char *port,
156 452122e2 2024-04-25 thomas.ad const char *path, const char *path_sufx, const char *query, const char *ctype)
157 09876a9d 2024-04-25 thomas.ad {
158 09876a9d 2024-04-25 thomas.ad const char *chdr = NULL, *te = "";
159 09876a9d 2024-04-25 thomas.ad char *p, *req;
160 09876a9d 2024-04-25 thomas.ad int r;
161 452122e2 2024-04-25 thomas.ad
162 09876a9d 2024-04-25 thomas.ad if (path_sufx != NULL && *path && path[strlen(path) - 1] == '/')
163 09876a9d 2024-04-25 thomas.ad path_sufx++; /* skip the slash */
164 09876a9d 2024-04-25 thomas.ad
165 09876a9d 2024-04-25 thomas.ad if (strcmp(method, "POST") == 0)
166 09876a9d 2024-04-25 thomas.ad te = "\r\nTransfer-Encoding: chunked\r\n";
167 09876a9d 2024-04-25 thomas.ad
168 09876a9d 2024-04-25 thomas.ad if (ctype)
169 09876a9d 2024-04-25 thomas.ad chdr = "Content-Type: ";
170 09876a9d 2024-04-25 thomas.ad
171 09876a9d 2024-04-25 thomas.ad r = asprintf(&p, "%s/%s%s%s", path, path_sufx,
172 09876a9d 2024-04-25 thomas.ad query ? "?" : "", query ? query : "");
173 09876a9d 2024-04-25 thomas.ad if (r == -1)
174 09876a9d 2024-04-25 thomas.ad err(1, "asprintf");
175 09876a9d 2024-04-25 thomas.ad
176 09876a9d 2024-04-25 thomas.ad r = asprintf(&req, "%s %s HTTP/1.1\r\n"
177 09876a9d 2024-04-25 thomas.ad "Host: %s\r\n"
178 09876a9d 2024-04-25 thomas.ad "Connection: close\r\n"
179 09876a9d 2024-04-25 thomas.ad "User-agent: %s\r\n"
180 09876a9d 2024-04-25 thomas.ad "%s%s%s\r\n",
181 09876a9d 2024-04-25 thomas.ad method, p, host, GOT_USERAGENT,
182 09876a9d 2024-04-25 thomas.ad chdr ? chdr : "", ctype ? ctype : "", te);
183 09876a9d 2024-04-25 thomas.ad if (r == -1)
184 09876a9d 2024-04-25 thomas.ad err(1, "asprintf");
185 452122e2 2024-04-25 thomas.ad free(p);
186 09876a9d 2024-04-25 thomas.ad
187 09876a9d 2024-04-25 thomas.ad if (verbose > 0)
188 b39e70da 2024-04-25 thomas.ad fprintf(stderr, "%s: request: %s\n", getprogname(), req);
189 452122e2 2024-04-25 thomas.ad
190 09876a9d 2024-04-25 thomas.ad
191 452122e2 2024-04-25 thomas.ad r = bufio_compose(bio, req, r);
192 452122e2 2024-04-25 thomas.ad if (r == -1)
193 452122e2 2024-04-25 thomas.ad err(1, "bufio_compose_fmt");
194 09876a9d 2024-04-25 thomas.ad free(req);
195 09876a9d 2024-04-25 thomas.ad
196 452122e2 2024-04-25 thomas.ad do {
197 452122e2 2024-04-25 thomas.ad r = bufio_write(bio);
198 452122e2 2024-04-25 thomas.ad if (r == -1 && errno != EAGAIN)
199 452122e2 2024-04-25 thomas.ad errx(1, "bufio_read: %s", bufio_io_err(bio));
200 452122e2 2024-04-25 thomas.ad } while (bio->wbuf.len != 0);
201 452122e2 2024-04-25 thomas.ad
202 452122e2 2024-04-25 thomas.ad return 0;
203 09876a9d 2024-04-25 thomas.ad }
204 09876a9d 2024-04-25 thomas.ad
205 09876a9d 2024-04-25 thomas.ad static int
206 452122e2 2024-04-25 thomas.ad http_parse_reply(struct bufio *bio, int *chunked, const char *expected_ctype)
207 09876a9d 2024-04-25 thomas.ad {
208 452122e2 2024-04-25 thomas.ad char *cp, *line;
209 452122e2 2024-04-25 thomas.ad size_t linelen;
210 09876a9d 2024-04-25 thomas.ad
211 09876a9d 2024-04-25 thomas.ad *chunked = 0;
212 09876a9d 2024-04-25 thomas.ad
213 452122e2 2024-04-25 thomas.ad line = bufio_getdelim_sync(bio, "\r\n", &linelen);
214 452122e2 2024-04-25 thomas.ad if (line == NULL) {
215 452122e2 2024-04-25 thomas.ad warnx("%s: bufio_getdelim_sync()", __func__);
216 09876a9d 2024-04-25 thomas.ad return -1;
217 09876a9d 2024-04-25 thomas.ad }
218 09876a9d 2024-04-25 thomas.ad
219 29460ff0 2024-04-25 thomas.ad if (verbose > 0)
220 b39e70da 2024-04-25 thomas.ad fprintf(stderr, "%s: response: %s\n", getprogname(), line);
221 29460ff0 2024-04-25 thomas.ad
222 09876a9d 2024-04-25 thomas.ad if ((cp = strchr(line, ' ')) == NULL) {
223 09876a9d 2024-04-25 thomas.ad warnx("malformed HTTP response");
224 452122e2 2024-04-25 thomas.ad return -1;
225 09876a9d 2024-04-25 thomas.ad }
226 09876a9d 2024-04-25 thomas.ad cp++;
227 09876a9d 2024-04-25 thomas.ad
228 09876a9d 2024-04-25 thomas.ad if (strncmp(cp, "200 ", 4) != 0) {
229 09876a9d 2024-04-25 thomas.ad warnx("malformed HTTP response");
230 452122e2 2024-04-25 thomas.ad return -1;
231 09876a9d 2024-04-25 thomas.ad }
232 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
233 09876a9d 2024-04-25 thomas.ad
234 452122e2 2024-04-25 thomas.ad while(1) {
235 452122e2 2024-04-25 thomas.ad line = bufio_getdelim_sync(bio, "\r\n", &linelen);
236 452122e2 2024-04-25 thomas.ad if (line == NULL) {
237 452122e2 2024-04-25 thomas.ad warnx("%s: bufio_getdelim_sync()", __func__);
238 452122e2 2024-04-25 thomas.ad return -1;
239 452122e2 2024-04-25 thomas.ad }
240 452122e2 2024-04-25 thomas.ad if (*line == '\0') {
241 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
242 09876a9d 2024-04-25 thomas.ad break;
243 452122e2 2024-04-25 thomas.ad }
244 09876a9d 2024-04-25 thomas.ad
245 09876a9d 2024-04-25 thomas.ad if (hasprfx(line, "content-type:")) {
246 09876a9d 2024-04-25 thomas.ad cp = strchr(line, ':') + 1;
247 09876a9d 2024-04-25 thomas.ad cp += strspn(cp, " \t");
248 09876a9d 2024-04-25 thomas.ad cp[strcspn(cp, " \t")] = '\0';
249 09876a9d 2024-04-25 thomas.ad if (strcmp(cp, expected_ctype) != 0) {
250 09876a9d 2024-04-25 thomas.ad warnx("server not using the \"smart\" "
251 09876a9d 2024-04-25 thomas.ad "HTTP protocol.");
252 452122e2 2024-04-25 thomas.ad return -1;
253 09876a9d 2024-04-25 thomas.ad }
254 09876a9d 2024-04-25 thomas.ad }
255 09876a9d 2024-04-25 thomas.ad if (hasprfx(line, "transfer-encoding:")) {
256 09876a9d 2024-04-25 thomas.ad cp = strchr(line, ':') + 1;
257 09876a9d 2024-04-25 thomas.ad cp += strspn(cp, " \t");
258 09876a9d 2024-04-25 thomas.ad cp[strcspn(cp, " \t")] = '\0';
259 09876a9d 2024-04-25 thomas.ad if (strcmp(cp, "chunked") != 0) {
260 09876a9d 2024-04-25 thomas.ad warnx("unknown transfer-encoding");
261 452122e2 2024-04-25 thomas.ad return -1;
262 09876a9d 2024-04-25 thomas.ad }
263 09876a9d 2024-04-25 thomas.ad *chunked = 1;
264 09876a9d 2024-04-25 thomas.ad }
265 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
266 09876a9d 2024-04-25 thomas.ad }
267 09876a9d 2024-04-25 thomas.ad
268 09876a9d 2024-04-25 thomas.ad return 0;
269 09876a9d 2024-04-25 thomas.ad }
270 09876a9d 2024-04-25 thomas.ad
271 09876a9d 2024-04-25 thomas.ad static ssize_t
272 452122e2 2024-04-25 thomas.ad http_read(struct bufio *bio, int chunked, size_t *chunksz, char *buf, size_t bufsz)
273 09876a9d 2024-04-25 thomas.ad {
274 09876a9d 2024-04-25 thomas.ad const char *errstr;
275 452122e2 2024-04-25 thomas.ad char *line = NULL;
276 452122e2 2024-04-25 thomas.ad size_t r;
277 09876a9d 2024-04-25 thomas.ad ssize_t ret = 0, linelen;
278 09876a9d 2024-04-25 thomas.ad
279 09876a9d 2024-04-25 thomas.ad if (!chunked) {
280 452122e2 2024-04-25 thomas.ad r = bufio_drain_sync(bio, buf, bufsz);
281 452122e2 2024-04-25 thomas.ad if (r == 0)
282 09876a9d 2024-04-25 thomas.ad return -1;
283 09876a9d 2024-04-25 thomas.ad return r;
284 09876a9d 2024-04-25 thomas.ad }
285 09876a9d 2024-04-25 thomas.ad
286 09876a9d 2024-04-25 thomas.ad while (bufsz > 0) {
287 09876a9d 2024-04-25 thomas.ad if (*chunksz == 0) {
288 09876a9d 2024-04-25 thomas.ad again:
289 452122e2 2024-04-25 thomas.ad line = bufio_getdelim_sync(bio, "\r\n", &linelen);
290 452122e2 2024-04-25 thomas.ad if (line == NULL) {
291 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
292 09876a9d 2024-04-25 thomas.ad break;
293 09876a9d 2024-04-25 thomas.ad }
294 452122e2 2024-04-25 thomas.ad if (*line == '\0') {
295 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
296 452122e2 2024-04-25 thomas.ad goto again; /* was the CRLF after the chunk */
297 09876a9d 2024-04-25 thomas.ad }
298 09876a9d 2024-04-25 thomas.ad
299 09876a9d 2024-04-25 thomas.ad *chunksz = hexstrtonum(line, 0, INT_MAX, &errstr);
300 09876a9d 2024-04-25 thomas.ad if (errstr != NULL) {
301 09876a9d 2024-04-25 thomas.ad warnx("invalid HTTP chunk: size is %s (%s)",
302 09876a9d 2024-04-25 thomas.ad errstr, line);
303 09876a9d 2024-04-25 thomas.ad ret = -1;
304 09876a9d 2024-04-25 thomas.ad break;
305 09876a9d 2024-04-25 thomas.ad }
306 09876a9d 2024-04-25 thomas.ad
307 452122e2 2024-04-25 thomas.ad if (*chunksz == 0) {
308 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
309 09876a9d 2024-04-25 thomas.ad break;
310 452122e2 2024-04-25 thomas.ad }
311 452122e2 2024-04-25 thomas.ad buf_drain(&bio->rbuf, linelen);
312 09876a9d 2024-04-25 thomas.ad }
313 09876a9d 2024-04-25 thomas.ad
314 452122e2 2024-04-25 thomas.ad r = bufio_drain_sync(bio, buf, MINIMUM(*chunksz, bufsz));
315 09876a9d 2024-04-25 thomas.ad if (r == 0) {
316 09876a9d 2024-04-25 thomas.ad break;
317 09876a9d 2024-04-25 thomas.ad }
318 09876a9d 2024-04-25 thomas.ad
319 09876a9d 2024-04-25 thomas.ad ret += r;
320 09876a9d 2024-04-25 thomas.ad buf += r;
321 09876a9d 2024-04-25 thomas.ad bufsz -= r;
322 09876a9d 2024-04-25 thomas.ad *chunksz -= r;
323 09876a9d 2024-04-25 thomas.ad }
324 09876a9d 2024-04-25 thomas.ad
325 09876a9d 2024-04-25 thomas.ad return ret;
326 09876a9d 2024-04-25 thomas.ad }
327 09876a9d 2024-04-25 thomas.ad
328 452122e2 2024-04-25 thomas.ad static int
329 452122e2 2024-04-25 thomas.ad http_chunk(struct bufio *bio, const void *buf, size_t len)
330 09876a9d 2024-04-25 thomas.ad {
331 452122e2 2024-04-25 thomas.ad int r;
332 09876a9d 2024-04-25 thomas.ad
333 452122e2 2024-04-25 thomas.ad if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
334 452122e2 2024-04-25 thomas.ad bufio_compose(bio, buf, len) ||
335 452122e2 2024-04-25 thomas.ad bufio_compose(bio, "\r\n", 2))
336 452122e2 2024-04-25 thomas.ad return 1;
337 452122e2 2024-04-25 thomas.ad
338 452122e2 2024-04-25 thomas.ad do {
339 452122e2 2024-04-25 thomas.ad r = bufio_write(bio);
340 452122e2 2024-04-25 thomas.ad if (r == -1 && errno != EAGAIN)
341 452122e2 2024-04-25 thomas.ad errx(1, "bufio_read: %s", bufio_io_err(bio));
342 452122e2 2024-04-25 thomas.ad } while (bio->wbuf.len != 0);
343 452122e2 2024-04-25 thomas.ad
344 452122e2 2024-04-25 thomas.ad return 0;
345 09876a9d 2024-04-25 thomas.ad }
346 09876a9d 2024-04-25 thomas.ad
347 09876a9d 2024-04-25 thomas.ad static int
348 09876a9d 2024-04-25 thomas.ad get_refs(int https, const char *host, const char *port, const char *path)
349 09876a9d 2024-04-25 thomas.ad {
350 507e5154 2024-04-25 thomas.ad struct bufio bio;
351 507e5154 2024-04-25 thomas.ad char buf[GOT_PKT_MAX];
352 507e5154 2024-04-25 thomas.ad const struct got_error *e;
353 507e5154 2024-04-25 thomas.ad const char *sufx = "/info/refs";
354 507e5154 2024-04-25 thomas.ad size_t chunksz = 0;
355 507e5154 2024-04-25 thomas.ad ssize_t r;
356 507e5154 2024-04-25 thomas.ad int skip;
357 507e5154 2024-04-25 thomas.ad int chunked;
358 507e5154 2024-04-25 thomas.ad int sock;
359 507e5154 2024-04-25 thomas.ad int ret = -1;
360 09876a9d 2024-04-25 thomas.ad
361 452122e2 2024-04-25 thomas.ad if ((sock = dial(https, host, port)) == -1)
362 09876a9d 2024-04-25 thomas.ad return -1;
363 09876a9d 2024-04-25 thomas.ad
364 452122e2 2024-04-25 thomas.ad if (bufio_init(&bio)) {
365 452122e2 2024-04-25 thomas.ad warnx("bufio_init");
366 452122e2 2024-04-25 thomas.ad goto err;
367 452122e2 2024-04-25 thomas.ad }
368 452122e2 2024-04-25 thomas.ad bufio_set_fd(&bio, sock);
369 452122e2 2024-04-25 thomas.ad if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
370 452122e2 2024-04-25 thomas.ad warnx("bufio_starttls");
371 452122e2 2024-04-25 thomas.ad goto err;
372 09876a9d 2024-04-25 thomas.ad }
373 09876a9d 2024-04-25 thomas.ad
374 452122e2 2024-04-25 thomas.ad if (http_open(&bio, https, "GET", host, port, path, sufx,
375 452122e2 2024-04-25 thomas.ad "service=git-upload-pack", NULL) == -1)
376 452122e2 2024-04-25 thomas.ad goto err;
377 452122e2 2024-04-25 thomas.ad
378 ff4d9c2e 2024-04-25 thomas.ad /* Fetch the initial reference announcement from the server. */
379 452122e2 2024-04-25 thomas.ad if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_ADV) == -1)
380 452122e2 2024-04-25 thomas.ad goto err;
381 452122e2 2024-04-25 thomas.ad
382 09876a9d 2024-04-25 thomas.ad /* skip first pack; why git over http is like this? */
383 452122e2 2024-04-25 thomas.ad r = http_read(&bio, chunked, &chunksz, buf, 4);
384 452122e2 2024-04-25 thomas.ad if (r <= 0)
385 452122e2 2024-04-25 thomas.ad goto err;
386 507e5154 2024-04-25 thomas.ad
387 507e5154 2024-04-25 thomas.ad e = got_pkt_readlen(&skip, buf, verbose);
388 507e5154 2024-04-25 thomas.ad if (e) {
389 507e5154 2024-04-25 thomas.ad warnx("%s", e->msg);
390 452122e2 2024-04-25 thomas.ad goto err;
391 09876a9d 2024-04-25 thomas.ad }
392 09876a9d 2024-04-25 thomas.ad
393 09876a9d 2024-04-25 thomas.ad /* TODO: validate it's # service=git-upload-pack\n */
394 09876a9d 2024-04-25 thomas.ad while (skip > 0) {
395 452122e2 2024-04-25 thomas.ad r = http_read(&bio, chunked, &chunksz, buf,
396 09876a9d 2024-04-25 thomas.ad MINIMUM(skip, sizeof(buf)));
397 452122e2 2024-04-25 thomas.ad if (r <= 0)
398 452122e2 2024-04-25 thomas.ad goto err;
399 09876a9d 2024-04-25 thomas.ad skip -= r;
400 09876a9d 2024-04-25 thomas.ad }
401 09876a9d 2024-04-25 thomas.ad
402 09876a9d 2024-04-25 thomas.ad for (;;) {
403 452122e2 2024-04-25 thomas.ad r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
404 452122e2 2024-04-25 thomas.ad if (r == -1)
405 452122e2 2024-04-25 thomas.ad goto err;
406 09876a9d 2024-04-25 thomas.ad
407 09876a9d 2024-04-25 thomas.ad if (r == 0)
408 09876a9d 2024-04-25 thomas.ad break;
409 09876a9d 2024-04-25 thomas.ad
410 09876a9d 2024-04-25 thomas.ad fwrite(buf, 1, r, stdout);
411 09876a9d 2024-04-25 thomas.ad }
412 09876a9d 2024-04-25 thomas.ad
413 09876a9d 2024-04-25 thomas.ad fflush(stdout);
414 452122e2 2024-04-25 thomas.ad ret = 0;
415 452122e2 2024-04-25 thomas.ad err:
416 452122e2 2024-04-25 thomas.ad bufio_close_sync(&bio);
417 452122e2 2024-04-25 thomas.ad bufio_free(&bio);
418 452122e2 2024-04-25 thomas.ad return ret;
419 09876a9d 2024-04-25 thomas.ad }
420 09876a9d 2024-04-25 thomas.ad
421 09876a9d 2024-04-25 thomas.ad static int
422 09876a9d 2024-04-25 thomas.ad upload_request(int https, const char *host, const char *port, const char *path,
423 09876a9d 2024-04-25 thomas.ad FILE *in)
424 09876a9d 2024-04-25 thomas.ad {
425 507e5154 2024-04-25 thomas.ad struct bufio bio;
426 507e5154 2024-04-25 thomas.ad char buf[GOT_PKT_MAX];
427 507e5154 2024-04-25 thomas.ad const struct got_error *e;
428 507e5154 2024-04-25 thomas.ad ssize_t r;
429 507e5154 2024-04-25 thomas.ad size_t chunksz = 0;
430 507e5154 2024-04-25 thomas.ad int t;
431 507e5154 2024-04-25 thomas.ad int chunked;
432 507e5154 2024-04-25 thomas.ad int sock;
433 507e5154 2024-04-25 thomas.ad int ret = -1;
434 09876a9d 2024-04-25 thomas.ad
435 452122e2 2024-04-25 thomas.ad if ((sock = dial(https, host, port)) == -1)
436 09876a9d 2024-04-25 thomas.ad return -1;
437 ed77be68 2024-04-25 thomas.ad
438 452122e2 2024-04-25 thomas.ad if (bufio_init(&bio)) {
439 452122e2 2024-04-25 thomas.ad warnx("bufio_init");
440 452122e2 2024-04-25 thomas.ad goto err;
441 452122e2 2024-04-25 thomas.ad }
442 452122e2 2024-04-25 thomas.ad bufio_set_fd(&bio, sock);
443 452122e2 2024-04-25 thomas.ad if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
444 452122e2 2024-04-25 thomas.ad warnx("bufio_starttls");
445 452122e2 2024-04-25 thomas.ad goto err;
446 452122e2 2024-04-25 thomas.ad }
447 ed77be68 2024-04-25 thomas.ad #ifndef PROFILE
448 ed77be68 2024-04-25 thomas.ad /* TODO: can we push this upwards such that get_refs() is covered? */
449 ed77be68 2024-04-25 thomas.ad if (pledge("stdio", NULL) == -1)
450 ed77be68 2024-04-25 thomas.ad err(1, "pledge");
451 ed77be68 2024-04-25 thomas.ad #endif
452 452122e2 2024-04-25 thomas.ad if (http_open(&bio, https, "POST", host, port, path, "/git-upload-pack",
453 452122e2 2024-04-25 thomas.ad NULL, UPLOAD_PACK_REQ) == -1)
454 452122e2 2024-04-25 thomas.ad goto err;
455 452122e2 2024-04-25 thomas.ad
456 ff4d9c2e 2024-04-25 thomas.ad /*
457 ff4d9c2e 2024-04-25 thomas.ad * Read have/want lines generated by got-fetch-pack and forward
458 ff4d9c2e 2024-04-25 thomas.ad * them to the server in the POST request body.
459 ff4d9c2e 2024-04-25 thomas.ad */
460 09876a9d 2024-04-25 thomas.ad for (;;) {
461 09876a9d 2024-04-25 thomas.ad r = fread(buf, 1, 4, in);
462 09876a9d 2024-04-25 thomas.ad if (r != 4)
463 09876a9d 2024-04-25 thomas.ad goto err;
464 09876a9d 2024-04-25 thomas.ad
465 507e5154 2024-04-25 thomas.ad e = got_pkt_readlen(&t, buf, verbose);
466 507e5154 2024-04-25 thomas.ad if (e) {
467 507e5154 2024-04-25 thomas.ad warnx("%s", e->msg);
468 09876a9d 2024-04-25 thomas.ad goto err;
469 09876a9d 2024-04-25 thomas.ad }
470 09876a9d 2024-04-25 thomas.ad
471 09876a9d 2024-04-25 thomas.ad if (t == 0) {
472 06dc3607 2024-04-25 thomas.ad const char *flushpkt = "0000";
473 06dc3607 2024-04-25 thomas.ad if (http_chunk(&bio, flushpkt, strlen(flushpkt)))
474 452122e2 2024-04-25 thomas.ad goto err;
475 06dc3607 2024-04-25 thomas.ad continue; /* got-fetch-pack will send "done" */
476 09876a9d 2024-04-25 thomas.ad }
477 09876a9d 2024-04-25 thomas.ad
478 09876a9d 2024-04-25 thomas.ad if (t < 6) {
479 09876a9d 2024-04-25 thomas.ad warnx("pktline len is too small");
480 09876a9d 2024-04-25 thomas.ad goto err;
481 09876a9d 2024-04-25 thomas.ad }
482 09876a9d 2024-04-25 thomas.ad
483 09876a9d 2024-04-25 thomas.ad r = fread(buf + 4, 1, t - 4, in);
484 09876a9d 2024-04-25 thomas.ad if (r != t - 4)
485 09876a9d 2024-04-25 thomas.ad goto err;
486 09876a9d 2024-04-25 thomas.ad
487 452122e2 2024-04-25 thomas.ad if (http_chunk(&bio, buf, t))
488 452122e2 2024-04-25 thomas.ad goto err;
489 06dc3607 2024-04-25 thomas.ad
490 06dc3607 2024-04-25 thomas.ad /*
491 06dc3607 2024-04-25 thomas.ad * Once got-fetch-pack is done the server will
492 06dc3607 2024-04-25 thomas.ad * send pack file data.
493 06dc3607 2024-04-25 thomas.ad */
494 06dc3607 2024-04-25 thomas.ad if (t == 9 && strncmp(buf + 4, "done\n", 5) == 0) {
495 06dc3607 2024-04-25 thomas.ad if (http_chunk(&bio, NULL, 0))
496 06dc3607 2024-04-25 thomas.ad goto err;
497 06dc3607 2024-04-25 thomas.ad break;
498 06dc3607 2024-04-25 thomas.ad }
499 09876a9d 2024-04-25 thomas.ad }
500 09876a9d 2024-04-25 thomas.ad
501 452122e2 2024-04-25 thomas.ad if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_RES) == -1)
502 09876a9d 2024-04-25 thomas.ad goto err;
503 09876a9d 2024-04-25 thomas.ad
504 ff4d9c2e 2024-04-25 thomas.ad /* Fetch pack file data from server. */
505 09876a9d 2024-04-25 thomas.ad for (;;) {
506 452122e2 2024-04-25 thomas.ad r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
507 452122e2 2024-04-25 thomas.ad if (r == -1)
508 452122e2 2024-04-25 thomas.ad goto err;
509 09876a9d 2024-04-25 thomas.ad
510 09876a9d 2024-04-25 thomas.ad if (r == 0)
511 09876a9d 2024-04-25 thomas.ad break;
512 09876a9d 2024-04-25 thomas.ad
513 09876a9d 2024-04-25 thomas.ad fwrite(buf, 1, r, stdout);
514 09876a9d 2024-04-25 thomas.ad }
515 09876a9d 2024-04-25 thomas.ad
516 452122e2 2024-04-25 thomas.ad ret = 0;
517 09876a9d 2024-04-25 thomas.ad err:
518 452122e2 2024-04-25 thomas.ad bufio_close_sync(&bio);
519 452122e2 2024-04-25 thomas.ad bufio_free(&bio);
520 452122e2 2024-04-25 thomas.ad return ret;
521 09876a9d 2024-04-25 thomas.ad }
522 09876a9d 2024-04-25 thomas.ad
523 09876a9d 2024-04-25 thomas.ad static __dead void
524 09876a9d 2024-04-25 thomas.ad usage(void)
525 09876a9d 2024-04-25 thomas.ad {
526 09876a9d 2024-04-25 thomas.ad fprintf(stderr, "usage: %s [-qv] proto host port path\n",
527 09876a9d 2024-04-25 thomas.ad getprogname());
528 09876a9d 2024-04-25 thomas.ad exit(1);
529 09876a9d 2024-04-25 thomas.ad }
530 09876a9d 2024-04-25 thomas.ad
531 09876a9d 2024-04-25 thomas.ad int
532 09876a9d 2024-04-25 thomas.ad main(int argc, char **argv)
533 09876a9d 2024-04-25 thomas.ad {
534 09876a9d 2024-04-25 thomas.ad struct pollfd pfd;
535 09876a9d 2024-04-25 thomas.ad const char *host, *port, *path;
536 09876a9d 2024-04-25 thomas.ad int https = 0;
537 09876a9d 2024-04-25 thomas.ad int ch;
538 09876a9d 2024-04-25 thomas.ad
539 cf87b1d1 2024-04-25 thomas.ad #ifndef PROFILE
540 a9a48d93 2024-04-25 thomas.ad if (pledge("stdio rpath inet dns unveil", NULL) == -1)
541 09876a9d 2024-04-25 thomas.ad err(1, "pledge");
542 09876a9d 2024-04-25 thomas.ad #endif
543 09876a9d 2024-04-25 thomas.ad
544 09876a9d 2024-04-25 thomas.ad while ((ch = getopt(argc, argv, "qv")) != -1) {
545 09876a9d 2024-04-25 thomas.ad switch (ch) {
546 09876a9d 2024-04-25 thomas.ad case 'q':
547 09876a9d 2024-04-25 thomas.ad verbose = -1;
548 09876a9d 2024-04-25 thomas.ad break;
549 09876a9d 2024-04-25 thomas.ad case 'v':
550 09876a9d 2024-04-25 thomas.ad verbose++;
551 09876a9d 2024-04-25 thomas.ad break;
552 09876a9d 2024-04-25 thomas.ad default:
553 09876a9d 2024-04-25 thomas.ad usage();
554 09876a9d 2024-04-25 thomas.ad }
555 09876a9d 2024-04-25 thomas.ad }
556 09876a9d 2024-04-25 thomas.ad argc -= optind;
557 09876a9d 2024-04-25 thomas.ad argv += optind;
558 09876a9d 2024-04-25 thomas.ad
559 09876a9d 2024-04-25 thomas.ad if (argc != 4)
560 09876a9d 2024-04-25 thomas.ad usage();
561 09876a9d 2024-04-25 thomas.ad
562 09876a9d 2024-04-25 thomas.ad https = strcmp(argv[0], "https") == 0;
563 e9495ffd 2024-04-25 thomas.ad #ifndef PROFILE
564 a9a48d93 2024-04-25 thomas.ad if (https) {
565 a9a48d93 2024-04-25 thomas.ad if (unveil("/etc/ssl/cert.pem", "r") == -1)
566 a9a48d93 2024-04-25 thomas.ad err(1, "unveil /etc/ssl/cert.pem");
567 a9a48d93 2024-04-25 thomas.ad } else {
568 e9495ffd 2024-04-25 thomas.ad /* drop "rpath" */
569 a9a48d93 2024-04-25 thomas.ad if (pledge("stdio inet dns unveil", NULL) == -1)
570 e9495ffd 2024-04-25 thomas.ad err(1, "pledge");
571 e9495ffd 2024-04-25 thomas.ad }
572 a9a48d93 2024-04-25 thomas.ad #else
573 a9a48d93 2024-04-25 thomas.ad if (unveil("gmon.out", "rwc") != 0)
574 a9a48d93 2024-04-25 thomas.ad err(1, "unveil gmon.out");
575 e9495ffd 2024-04-25 thomas.ad #endif
576 a9a48d93 2024-04-25 thomas.ad if (unveil(NULL, NULL) == -1)
577 a9a48d93 2024-04-25 thomas.ad err(1, "unveil NULL");
578 a9a48d93 2024-04-25 thomas.ad
579 09876a9d 2024-04-25 thomas.ad host = argv[1];
580 09876a9d 2024-04-25 thomas.ad port = argv[2];
581 09876a9d 2024-04-25 thomas.ad path = argv[3];
582 09876a9d 2024-04-25 thomas.ad
583 09876a9d 2024-04-25 thomas.ad if (get_refs(https, host, port, path) == -1)
584 09876a9d 2024-04-25 thomas.ad errx(1, "failed to get refs");
585 09876a9d 2024-04-25 thomas.ad
586 09876a9d 2024-04-25 thomas.ad pfd.fd = 0;
587 09876a9d 2024-04-25 thomas.ad pfd.events = POLLIN;
588 09876a9d 2024-04-25 thomas.ad if (poll(&pfd, 1, INFTIM) == -1)
589 09876a9d 2024-04-25 thomas.ad err(1, "poll");
590 09876a9d 2024-04-25 thomas.ad
591 09876a9d 2024-04-25 thomas.ad if ((ch = fgetc(stdin)) == EOF)
592 09876a9d 2024-04-25 thomas.ad return 0;
593 09876a9d 2024-04-25 thomas.ad
594 09876a9d 2024-04-25 thomas.ad ungetc(ch, stdin);
595 09876a9d 2024-04-25 thomas.ad if (upload_request(https, host, port, path, stdin) == -1) {
596 09876a9d 2024-04-25 thomas.ad fflush(tmp);
597 09876a9d 2024-04-25 thomas.ad errx(1, "failed to upload request");
598 09876a9d 2024-04-25 thomas.ad }
599 09876a9d 2024-04-25 thomas.ad
600 09876a9d 2024-04-25 thomas.ad return 0;
601 09876a9d 2024-04-25 thomas.ad }