2 4c316997 2024-04-15 me * Copyright (c) 2024 Tobias Heider <me@tobhe.de>
3 ad3b5b58 2024-04-14 me * Copyright (c) 2022 Omar Polo <op@openbsd.org>
5 ad3b5b58 2024-04-14 me * Permission to use, copy, modify, and distribute this software for any
6 ad3b5b58 2024-04-14 me * purpose with or without fee is hereby granted, provided that the above
7 ad3b5b58 2024-04-14 me * copyright notice and this permission notice appear in all copies.
9 ad3b5b58 2024-04-14 me * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 ad3b5b58 2024-04-14 me * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 ad3b5b58 2024-04-14 me * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 ad3b5b58 2024-04-14 me * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 ad3b5b58 2024-04-14 me * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 ad3b5b58 2024-04-14 me * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 ad3b5b58 2024-04-14 me * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ad3b5b58 2024-04-14 me #include <sys/types.h>
19 ad3b5b58 2024-04-14 me #include <sys/socket.h>
21 ad3b5b58 2024-04-14 me #include <err.h>
22 ad3b5b58 2024-04-14 me #include <errno.h>
23 ad3b5b58 2024-04-14 me #include <limits.h>
24 ad3b5b58 2024-04-14 me #include <netdb.h>
25 ad3b5b58 2024-04-14 me #include <poll.h>
26 ad3b5b58 2024-04-14 me #include <stdio.h>
27 ad3b5b58 2024-04-14 me #include <stdlib.h>
28 ad3b5b58 2024-04-14 me #include <string.h>
29 ad3b5b58 2024-04-14 me #include <tls.h>
30 ad3b5b58 2024-04-14 me #include <unistd.h>
32 ad3b5b58 2024-04-14 me #include "got_version.h"
34 4c316997 2024-04-15 me #include "bufio.h"
36 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_ADV "application/x-git-upload-pack-advertisement"
37 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_REQ "application/x-git-upload-pack-request"
38 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_RES "application/x-git-upload-pack-result"
40 ad3b5b58 2024-04-14 me #define HTTP_BUFSIZ 4096
41 ad3b5b58 2024-04-14 me #define GOT_USERAGENT "got/" GOT_VERSION_STR
42 ad3b5b58 2024-04-14 me #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
43 ad3b5b58 2024-04-14 me #define hasprfx(str, p) (strncasecmp(str, p, strlen(p)) == 0)
45 4c316997 2024-04-15 me #define DEBUG_HTTP 0
49 ad3b5b58 2024-04-14 me static int verbose;
52 4c316997 2024-04-15 me bufio_getdelim_sync(struct bufio *bio, const char *nl, size_t *len)
57 4c316997 2024-04-15 me r = bufio_read(bio);
58 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
59 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
60 4c316997 2024-04-15 me } while (r == -1 && errno == EAGAIN);
61 4c316997 2024-04-15 me return buf_getdelim(&bio->rbuf, nl, len);
65 4c316997 2024-04-15 me bufio_drain_sync(struct bufio *bio, void *d, size_t len)
70 4c316997 2024-04-15 me r = bufio_read(bio);
71 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
72 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
73 4c316997 2024-04-15 me } while (r == -1 && errno == EAGAIN);
74 4c316997 2024-04-15 me return bufio_drain(bio, d, len);
78 4c316997 2024-04-15 me bufio_close_sync(struct bufio *bio)
83 4c316997 2024-04-15 me r = bufio_close(bio);
84 4c316997 2024-04-15 me if (r == -1 && errno == EAGAIN)
85 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
86 4c316997 2024-04-15 me } while (r == -1 && errno == EAGAIN);
89 ad3b5b58 2024-04-14 me static long long
90 ad3b5b58 2024-04-14 me hexstrtonum(const char *str, long long min, long long max, const char **errstr)
92 ad3b5b58 2024-04-14 me long long lval;
96 ad3b5b58 2024-04-14 me lval = strtoll(str, &cp, 16);
97 ad3b5b58 2024-04-14 me if (*str == '\0' || *cp != '\0') {
98 ad3b5b58 2024-04-14 me *errstr = "not a number";
101 ad3b5b58 2024-04-14 me if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
102 ad3b5b58 2024-04-14 me lval < min || lval > max) {
103 ad3b5b58 2024-04-14 me *errstr = "out of range";
107 ad3b5b58 2024-04-14 me *errstr = NULL;
112 ad3b5b58 2024-04-14 me dial(int https, const char *host, const char *port)
114 ad3b5b58 2024-04-14 me struct addrinfo hints, *res, *res0;
115 4c316997 2024-04-15 me int error, saved_errno, fd = -1;
116 ad3b5b58 2024-04-14 me const char *cause = NULL;
118 ad3b5b58 2024-04-14 me memset(&hints, 0, sizeof(hints));
119 ad3b5b58 2024-04-14 me hints.ai_family = AF_UNSPEC;
120 ad3b5b58 2024-04-14 me hints.ai_socktype = SOCK_STREAM;
121 ad3b5b58 2024-04-14 me error = getaddrinfo(host, port, &hints, &res0);
123 ad3b5b58 2024-04-14 me warnx("%s", gai_strerror(error));
127 ad3b5b58 2024-04-14 me for (res = res0; res; res = res->ai_next) {
128 ad3b5b58 2024-04-14 me fd = socket(res->ai_family, res->ai_socktype,
129 ad3b5b58 2024-04-14 me res->ai_protocol);
130 ad3b5b58 2024-04-14 me if (fd == -1) {
131 ad3b5b58 2024-04-14 me cause = "socket";
135 ad3b5b58 2024-04-14 me if (connect(fd, res->ai_addr, res->ai_addrlen) == 0)
138 ad3b5b58 2024-04-14 me cause = "connect";
139 ad3b5b58 2024-04-14 me saved_errno = errno;
142 ad3b5b58 2024-04-14 me errno = saved_errno;
144 ad3b5b58 2024-04-14 me freeaddrinfo(res0);
146 ad3b5b58 2024-04-14 me if (fd == -1) {
147 ad3b5b58 2024-04-14 me warn("%s", cause);
155 4c316997 2024-04-15 me http_open(struct bufio *bio, int https, const char *method, const char *host, const char *port,
156 4c316997 2024-04-15 me const char *path, const char *path_sufx, const char *query, const char *ctype)
158 ad3b5b58 2024-04-14 me const char *chdr = NULL, *te = "";
159 ad3b5b58 2024-04-14 me char *p, *req;
162 ad3b5b58 2024-04-14 me if (path_sufx != NULL && *path && path[strlen(path) - 1] == '/')
163 ad3b5b58 2024-04-14 me path_sufx++; /* skip the slash */
165 ad3b5b58 2024-04-14 me if (strcmp(method, "POST") == 0)
166 ad3b5b58 2024-04-14 me te = "\r\nTransfer-Encoding: chunked\r\n";
169 ad3b5b58 2024-04-14 me chdr = "Content-Type: ";
171 ad3b5b58 2024-04-14 me r = asprintf(&p, "%s/%s%s%s", path, path_sufx,
172 ad3b5b58 2024-04-14 me query ? "?" : "", query ? query : "");
174 ad3b5b58 2024-04-14 me err(1, "asprintf");
176 ad3b5b58 2024-04-14 me r = asprintf(&req, "%s %s HTTP/1.1\r\n"
177 ad3b5b58 2024-04-14 me "Host: %s\r\n"
178 ad3b5b58 2024-04-14 me "Connection: close\r\n"
179 ad3b5b58 2024-04-14 me "User-agent: %s\r\n"
180 ad3b5b58 2024-04-14 me "%s%s%s\r\n",
181 ad3b5b58 2024-04-14 me method, p, host, GOT_USERAGENT,
182 ad3b5b58 2024-04-14 me chdr ? chdr : "", ctype ? ctype : "", te);
184 ad3b5b58 2024-04-14 me err(1, "asprintf");
187 ad3b5b58 2024-04-14 me if (verbose > 0)
188 4c094842 2024-04-17 stsp fprintf(stderr, "%s: request: %s\n", getprogname(), req);
191 4c316997 2024-04-15 me r = bufio_compose(bio, req, r);
193 4c316997 2024-04-15 me err(1, "bufio_compose_fmt");
197 4c316997 2024-04-15 me r = bufio_write(bio);
198 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
199 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
200 4c316997 2024-04-15 me } while (bio->wbuf.len != 0);
206 4c316997 2024-04-15 me http_parse_reply(struct bufio *bio, int *chunked, const char *expected_ctype)
208 4c316997 2024-04-15 me char *cp, *line;
209 4c316997 2024-04-15 me size_t linelen;
211 ad3b5b58 2024-04-14 me *chunked = 0;
213 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
214 4c316997 2024-04-15 me if (line == NULL) {
215 4c316997 2024-04-15 me warnx("%s: bufio_getdelim_sync()", __func__);
219 fe24f0a3 2024-04-14 stsp if (verbose > 0)
220 4c094842 2024-04-17 stsp fprintf(stderr, "%s: response: %s\n", getprogname(), line);
222 ad3b5b58 2024-04-14 me if ((cp = strchr(line, ' ')) == NULL) {
223 ad3b5b58 2024-04-14 me warnx("malformed HTTP response");
228 ad3b5b58 2024-04-14 me if (strncmp(cp, "200 ", 4) != 0) {
229 ad3b5b58 2024-04-14 me warnx("malformed HTTP response");
232 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
235 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
236 4c316997 2024-04-15 me if (line == NULL) {
237 4c316997 2024-04-15 me warnx("%s: bufio_getdelim_sync()", __func__);
240 4c316997 2024-04-15 me if (*line == '\0') {
241 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
244 4c316997 2024-04-15 me #if DEBUG_HTTP
245 4c316997 2024-04-15 me fprintf(stderr, "%s: %s\n", __func__, line);
248 ad3b5b58 2024-04-14 me if (hasprfx(line, "content-type:")) {
249 ad3b5b58 2024-04-14 me cp = strchr(line, ':') + 1;
250 ad3b5b58 2024-04-14 me cp += strspn(cp, " \t");
251 ad3b5b58 2024-04-14 me cp[strcspn(cp, " \t")] = '\0';
252 ad3b5b58 2024-04-14 me if (strcmp(cp, expected_ctype) != 0) {
253 ad3b5b58 2024-04-14 me warnx("server not using the \"smart\" "
254 ad3b5b58 2024-04-14 me "HTTP protocol.");
258 ad3b5b58 2024-04-14 me if (hasprfx(line, "transfer-encoding:")) {
259 ad3b5b58 2024-04-14 me cp = strchr(line, ':') + 1;
260 ad3b5b58 2024-04-14 me cp += strspn(cp, " \t");
261 ad3b5b58 2024-04-14 me cp[strcspn(cp, " \t")] = '\0';
262 ad3b5b58 2024-04-14 me if (strcmp(cp, "chunked") != 0) {
263 ad3b5b58 2024-04-14 me warnx("unknown transfer-encoding");
266 ad3b5b58 2024-04-14 me *chunked = 1;
268 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
274 ad3b5b58 2024-04-14 me static ssize_t
275 4c316997 2024-04-15 me http_read(struct bufio *bio, int chunked, size_t *chunksz, char *buf, size_t bufsz)
277 ad3b5b58 2024-04-14 me const char *errstr;
278 4c316997 2024-04-15 me char *line = NULL;
280 ad3b5b58 2024-04-14 me ssize_t ret = 0, linelen;
282 ad3b5b58 2024-04-14 me if (!chunked) {
283 4c316997 2024-04-15 me r = bufio_drain_sync(bio, buf, bufsz);
289 ad3b5b58 2024-04-14 me while (bufsz > 0) {
290 ad3b5b58 2024-04-14 me if (*chunksz == 0) {
292 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
293 4c316997 2024-04-15 me if (line == NULL) {
294 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
297 4c316997 2024-04-15 me if (*line == '\0') {
298 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
299 4c316997 2024-04-15 me goto again; /* was the CRLF after the chunk */
302 ad3b5b58 2024-04-14 me *chunksz = hexstrtonum(line, 0, INT_MAX, &errstr);
303 ad3b5b58 2024-04-14 me if (errstr != NULL) {
304 ad3b5b58 2024-04-14 me warnx("invalid HTTP chunk: size is %s (%s)",
305 ad3b5b58 2024-04-14 me errstr, line);
310 4c316997 2024-04-15 me if (*chunksz == 0) {
311 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
314 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
317 4c316997 2024-04-15 me r = bufio_drain_sync(bio, buf, MINIMUM(*chunksz, bufsz));
318 ad3b5b58 2024-04-14 me if (r == 0) {
322 ad3b5b58 2024-04-14 me #if DEBUG_HTTP
324 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, tmp);
325 ad3b5b58 2024-04-14 me /* fwrite(buf, 1, r, stderr); */
330 ad3b5b58 2024-04-14 me *chunksz -= r;
337 4c316997 2024-04-15 me http_chunk(struct bufio *bio, const void *buf, size_t len)
341 4c316997 2024-04-15 me if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
342 4c316997 2024-04-15 me bufio_compose(bio, buf, len) ||
343 4c316997 2024-04-15 me bufio_compose(bio, "\r\n", 2))
347 4c316997 2024-04-15 me r = bufio_write(bio);
348 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
349 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
350 4c316997 2024-04-15 me } while (bio->wbuf.len != 0);
356 ad3b5b58 2024-04-14 me get_refs(int https, const char *host, const char *port, const char *path)
358 4c316997 2024-04-15 me struct bufio bio;
359 ad3b5b58 2024-04-14 me char buf[HTTP_BUFSIZ];
360 ad3b5b58 2024-04-14 me const char *errstr, *sufx = "/info/refs";
361 ad3b5b58 2024-04-14 me size_t skip, chunksz = 0;
365 4c316997 2024-04-15 me int ret = -1;
367 4c316997 2024-04-15 me if ((sock = dial(https, host, port)) == -1)
370 4c316997 2024-04-15 me if (bufio_init(&bio)) {
371 4c316997 2024-04-15 me warnx("bufio_init");
374 4c316997 2024-04-15 me bufio_set_fd(&bio, sock);
375 4c316997 2024-04-15 me if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
376 4c316997 2024-04-15 me warnx("bufio_starttls");
380 4c316997 2024-04-15 me if (http_open(&bio, https, "GET", host, port, path, sufx,
381 4c316997 2024-04-15 me "service=git-upload-pack", NULL) == -1)
384 b1ebf3b3 2024-04-16 stsp /* Fetch the initial reference announcement from the server. */
385 4c316997 2024-04-15 me if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_ADV) == -1)
388 ad3b5b58 2024-04-14 me /* skip first pack; why git over http is like this? */
389 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, 4);
392 ad3b5b58 2024-04-14 me buf[4] = '\0';
393 ad3b5b58 2024-04-14 me skip = hexstrtonum(buf, 0, INT_MAX, &errstr);
394 ad3b5b58 2024-04-14 me if (errstr != NULL) {
395 ad3b5b58 2024-04-14 me warnx("pktlen is %s", errstr);
399 ad3b5b58 2024-04-14 me /* TODO: validate it's # service=git-upload-pack\n */
400 ad3b5b58 2024-04-14 me while (skip > 0) {
401 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf,
402 ad3b5b58 2024-04-14 me MINIMUM(skip, sizeof(buf)));
409 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
416 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, stdout);
419 ad3b5b58 2024-04-14 me fflush(stdout);
422 4c316997 2024-04-15 me bufio_close_sync(&bio);
423 4c316997 2024-04-15 me bufio_free(&bio);
428 ad3b5b58 2024-04-14 me upload_request(int https, const char *host, const char *port, const char *path,
431 4c316997 2024-04-15 me struct bufio bio;
432 ad3b5b58 2024-04-14 me const char *errstr;
433 ad3b5b58 2024-04-14 me char buf[HTTP_BUFSIZ];
435 ad3b5b58 2024-04-14 me size_t chunksz = 0;
439 4c316997 2024-04-15 me int ret = -1;
441 4c316997 2024-04-15 me if ((sock = dial(https, host, port)) == -1)
444 4c316997 2024-04-15 me if (bufio_init(&bio)) {
445 4c316997 2024-04-15 me warnx("bufio_init");
448 4c316997 2024-04-15 me bufio_set_fd(&bio, sock);
449 4c316997 2024-04-15 me if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
450 4c316997 2024-04-15 me warnx("bufio_starttls");
454 4c316997 2024-04-15 me if (http_open(&bio, https, "POST", host, port, path, "/git-upload-pack",
455 4c316997 2024-04-15 me NULL, UPLOAD_PACK_REQ) == -1)
459 b1ebf3b3 2024-04-16 stsp * Read have/want lines generated by got-fetch-pack and forward
460 b1ebf3b3 2024-04-16 stsp * them to the server in the POST request body.
463 ad3b5b58 2024-04-14 me r = fread(buf, 1, 4, in);
467 ad3b5b58 2024-04-14 me buf[4] = '\0';
468 ad3b5b58 2024-04-14 me t = hexstrtonum(buf, 0, sizeof(buf), &errstr);
469 ad3b5b58 2024-04-14 me if (errstr != NULL) {
470 ad3b5b58 2024-04-14 me warnx("pktline len is %s", errstr);
474 ad3b5b58 2024-04-14 me /* no idea why 0000 is not enough. */
475 ad3b5b58 2024-04-14 me if (t == 0) {
476 ad3b5b58 2024-04-14 me const char *x = "00000009done\n";
477 4c316997 2024-04-15 me if (http_chunk(&bio, x, strlen(x)) ||
478 4c316997 2024-04-15 me http_chunk(&bio, NULL, 0))
484 ad3b5b58 2024-04-14 me warnx("pktline len is too small");
488 ad3b5b58 2024-04-14 me r = fread(buf + 4, 1, t - 4, in);
489 ad3b5b58 2024-04-14 me if (r != t - 4)
492 4c316997 2024-04-15 me if (http_chunk(&bio, buf, t))
496 4c316997 2024-04-15 me if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_RES) == -1)
499 b1ebf3b3 2024-04-16 stsp /* Fetch pack file data from server. */
501 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
508 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, stdout);
513 4c316997 2024-04-15 me bufio_close_sync(&bio);
514 4c316997 2024-04-15 me bufio_free(&bio);
518 ad3b5b58 2024-04-14 me static __dead void
521 ad3b5b58 2024-04-14 me fprintf(stderr, "usage: %s [-qv] proto host port path\n",
522 ad3b5b58 2024-04-14 me getprogname());
527 ad3b5b58 2024-04-14 me main(int argc, char **argv)
529 ad3b5b58 2024-04-14 me struct pollfd pfd;
530 ad3b5b58 2024-04-14 me const char *host, *port, *path;
531 ad3b5b58 2024-04-14 me int https = 0;
534 ad3b5b58 2024-04-14 me #if !DEBUG_HTTP || defined(PROFILE)
535 4c316997 2024-04-15 me if (pledge("stdio rpath inet dns", NULL) == -1)
536 ad3b5b58 2024-04-14 me err(1, "pledge");
539 ad3b5b58 2024-04-14 me while ((ch = getopt(argc, argv, "qv")) != -1) {
540 ad3b5b58 2024-04-14 me switch (ch) {
542 ad3b5b58 2024-04-14 me verbose = -1;
551 ad3b5b58 2024-04-14 me argc -= optind;
552 ad3b5b58 2024-04-14 me argv += optind;
554 ad3b5b58 2024-04-14 me if (argc != 4)
557 ad3b5b58 2024-04-14 me https = strcmp(argv[0], "https") == 0;
559 ad3b5b58 2024-04-14 me host = argv[1];
560 ad3b5b58 2024-04-14 me port = argv[2];
561 ad3b5b58 2024-04-14 me path = argv[3];
563 ad3b5b58 2024-04-14 me if (get_refs(https, host, port, path) == -1)
564 ad3b5b58 2024-04-14 me errx(1, "failed to get refs");
566 ad3b5b58 2024-04-14 me #if DEBUG_HTTP
567 ad3b5b58 2024-04-14 me tmp = fopen("/tmp/pck", "w");
571 ad3b5b58 2024-04-14 me pfd.events = POLLIN;
572 ad3b5b58 2024-04-14 me if (poll(&pfd, 1, INFTIM) == -1)
573 ad3b5b58 2024-04-14 me err(1, "poll");
575 ad3b5b58 2024-04-14 me if ((ch = fgetc(stdin)) == EOF)
578 ad3b5b58 2024-04-14 me ungetc(ch, stdin);
579 ad3b5b58 2024-04-14 me if (upload_request(https, host, port, path, stdin) == -1) {
581 ad3b5b58 2024-04-14 me errx(1, "failed to upload request");