2 * Copyright (c) 2024 Tobias Heider <me@tobhe.de>
3 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/socket.h>
32 #include "got_version.h"
34 #include "got_lib_pkt.h"
38 #define UPLOAD_PACK_ADV "application/x-git-upload-pack-advertisement"
39 #define UPLOAD_PACK_REQ "application/x-git-upload-pack-request"
40 #define UPLOAD_PACK_RES "application/x-git-upload-pack-result"
42 #define GOT_USERAGENT "got/" GOT_VERSION_STR
43 #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
44 #define hasprfx(str, p) (strncasecmp(str, p, strlen(p)) == 0)
51 bufio_getdelim_sync(struct bufio *bio, const char *nl, size_t *len)
57 if (r == -1 && errno != EAGAIN)
58 errx(1, "bufio_read: %s", bufio_io_err(bio));
59 } while (r == -1 && errno == EAGAIN);
60 return buf_getdelim(&bio->rbuf, nl, len);
64 bufio_drain_sync(struct bufio *bio, void *d, size_t len)
70 if (r == -1 && errno != EAGAIN)
71 errx(1, "bufio_read: %s", bufio_io_err(bio));
72 } while (r == -1 && errno == EAGAIN);
73 return bufio_drain(bio, d, len);
77 bufio_close_sync(struct bufio *bio)
83 if (r == -1 && errno == EAGAIN)
84 errx(1, "bufio_read: %s", bufio_io_err(bio));
85 } while (r == -1 && errno == EAGAIN);
89 hexstrtonum(const char *str, long long min, long long max, const char **errstr)
95 lval = strtoll(str, &cp, 16);
96 if (*str == '\0' || *cp != '\0') {
97 *errstr = "not a number";
100 if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
101 lval < min || lval > max) {
102 *errstr = "out of range";
111 dial(int https, const char *host, const char *port)
113 struct addrinfo hints, *res, *res0;
114 int error, saved_errno, fd = -1;
115 const char *cause = NULL;
117 memset(&hints, 0, sizeof(hints));
118 hints.ai_family = AF_UNSPEC;
119 hints.ai_socktype = SOCK_STREAM;
120 error = getaddrinfo(host, port, &hints, &res0);
122 warnx("%s", gai_strerror(error));
126 for (res = res0; res; res = res->ai_next) {
127 fd = socket(res->ai_family, res->ai_socktype,
134 if (connect(fd, res->ai_addr, res->ai_addrlen) == 0)
154 http_open(struct bufio *bio, int https, const char *method, const char *host, const char *port,
155 const char *path, const char *path_sufx, const char *query, const char *ctype)
157 const char *chdr = NULL, *te = "";
161 if (path_sufx != NULL && *path && path[strlen(path) - 1] == '/')
162 path_sufx++; /* skip the slash */
164 if (strcmp(method, "POST") == 0)
165 te = "\r\nTransfer-Encoding: chunked\r\n";
168 chdr = "Content-Type: ";
170 r = asprintf(&p, "%s/%s%s%s", path, path_sufx,
171 query ? "?" : "", query ? query : "");
175 r = asprintf(&req, "%s %s HTTP/1.1\r\n"
177 "Connection: close\r\n"
180 method, p, host, GOT_USERAGENT,
181 chdr ? chdr : "", ctype ? ctype : "", te);
187 fprintf(stderr, "%s: request: %s\n", getprogname(), req);
190 r = bufio_compose(bio, req, r);
192 err(1, "bufio_compose_fmt");
196 r = bufio_write(bio);
197 if (r == -1 && errno != EAGAIN)
198 errx(1, "bufio_read: %s", bufio_io_err(bio));
199 } while (bio->wbuf.len != 0);
205 http_parse_reply(struct bufio *bio, int *chunked, const char *expected_ctype)
212 line = bufio_getdelim_sync(bio, "\r\n", &linelen);
214 warnx("%s: bufio_getdelim_sync()", __func__);
219 fprintf(stderr, "%s: response: %s\n", getprogname(), line);
221 if ((cp = strchr(line, ' ')) == NULL) {
222 warnx("malformed HTTP response");
227 if (strncmp(cp, "200 ", 4) != 0) {
228 warnx("malformed HTTP response");
231 buf_drain(&bio->rbuf, linelen);
234 line = bufio_getdelim_sync(bio, "\r\n", &linelen);
236 warnx("%s: bufio_getdelim_sync()", __func__);
240 buf_drain(&bio->rbuf, linelen);
244 if (hasprfx(line, "content-type:")) {
245 cp = strchr(line, ':') + 1;
246 cp += strspn(cp, " \t");
247 cp[strcspn(cp, " \t")] = '\0';
248 if (strcmp(cp, expected_ctype) != 0) {
249 warnx("server not using the \"smart\" "
254 if (hasprfx(line, "transfer-encoding:")) {
255 cp = strchr(line, ':') + 1;
256 cp += strspn(cp, " \t");
257 cp[strcspn(cp, " \t")] = '\0';
258 if (strcmp(cp, "chunked") != 0) {
259 warnx("unknown transfer-encoding");
264 buf_drain(&bio->rbuf, linelen);
271 http_read(struct bufio *bio, int chunked, size_t *chunksz, char *buf, size_t bufsz)
276 ssize_t ret = 0, linelen;
279 r = bufio_drain_sync(bio, buf, bufsz);
288 line = bufio_getdelim_sync(bio, "\r\n", &linelen);
290 buf_drain(&bio->rbuf, linelen);
294 buf_drain(&bio->rbuf, linelen);
295 goto again; /* was the CRLF after the chunk */
298 *chunksz = hexstrtonum(line, 0, INT_MAX, &errstr);
299 if (errstr != NULL) {
300 warnx("invalid HTTP chunk: size is %s (%s)",
307 buf_drain(&bio->rbuf, linelen);
310 buf_drain(&bio->rbuf, linelen);
313 r = bufio_drain_sync(bio, buf, MINIMUM(*chunksz, bufsz));
328 http_chunk(struct bufio *bio, const void *buf, size_t len)
332 if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
333 bufio_compose(bio, buf, len) ||
334 bufio_compose(bio, "\r\n", 2))
338 r = bufio_write(bio);
339 if (r == -1 && errno != EAGAIN)
340 errx(1, "bufio_read: %s", bufio_io_err(bio));
341 } while (bio->wbuf.len != 0);
347 get_refs(int https, const char *host, const char *port, const char *path)
350 char buf[GOT_PKT_MAX];
351 const char *errstr, *sufx = "/info/refs";
352 size_t skip, chunksz = 0;
358 if ((sock = dial(https, host, port)) == -1)
361 if (bufio_init(&bio)) {
365 bufio_set_fd(&bio, sock);
366 if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
367 warnx("bufio_starttls");
371 if (http_open(&bio, https, "GET", host, port, path, sufx,
372 "service=git-upload-pack", NULL) == -1)
375 /* Fetch the initial reference announcement from the server. */
376 if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_ADV) == -1)
379 /* skip first pack; why git over http is like this? */
380 r = http_read(&bio, chunked, &chunksz, buf, 4);
384 skip = hexstrtonum(buf, 0, INT_MAX, &errstr);
385 if (errstr != NULL) {
386 warnx("pktlen is %s", errstr);
390 /* TODO: validate it's # service=git-upload-pack\n */
392 r = http_read(&bio, chunked, &chunksz, buf,
393 MINIMUM(skip, sizeof(buf)));
400 r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
407 fwrite(buf, 1, r, stdout);
413 bufio_close_sync(&bio);
419 upload_request(int https, const char *host, const char *port, const char *path,
424 char buf[GOT_PKT_MAX];
432 if ((sock = dial(https, host, port)) == -1)
435 if (bufio_init(&bio)) {
439 bufio_set_fd(&bio, sock);
440 if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
441 warnx("bufio_starttls");
445 /* TODO: can we push this upwards such that get_refs() is covered? */
446 if (pledge("stdio", NULL) == -1)
449 if (http_open(&bio, https, "POST", host, port, path, "/git-upload-pack",
450 NULL, UPLOAD_PACK_REQ) == -1)
454 * Read have/want lines generated by got-fetch-pack and forward
455 * them to the server in the POST request body.
458 r = fread(buf, 1, 4, in);
463 t = hexstrtonum(buf, 0, sizeof(buf), &errstr);
464 if (errstr != NULL) {
465 warnx("pktline len is %s", errstr);
469 /* no idea why 0000 is not enough. */
471 const char *x = "00000009done\n";
472 if (http_chunk(&bio, x, strlen(x)) ||
473 http_chunk(&bio, NULL, 0))
479 warnx("pktline len is too small");
483 r = fread(buf + 4, 1, t - 4, in);
487 if (http_chunk(&bio, buf, t))
491 if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_RES) == -1)
494 /* Fetch pack file data from server. */
496 r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
503 fwrite(buf, 1, r, stdout);
508 bufio_close_sync(&bio);
516 fprintf(stderr, "usage: %s [-qv] proto host port path\n",
522 main(int argc, char **argv)
525 const char *host, *port, *path;
530 if (pledge("stdio rpath inet dns unveil", NULL) == -1)
534 while ((ch = getopt(argc, argv, "qv")) != -1) {
552 https = strcmp(argv[0], "https") == 0;
555 if (unveil("/etc/ssl/cert.pem", "r") == -1)
556 err(1, "unveil /etc/ssl/cert.pem");
559 if (pledge("stdio inet dns unveil", NULL) == -1)
563 if (unveil("gmon.out", "rwc") != 0)
564 err(1, "unveil gmon.out");
566 if (unveil(NULL, NULL) == -1)
567 err(1, "unveil NULL");
573 if (get_refs(https, host, port, path) == -1)
574 errx(1, "failed to get refs");
578 if (poll(&pfd, 1, INFTIM) == -1)
581 if ((ch = fgetc(stdin)) == EOF)
585 if (upload_request(https, host, port, path, stdin) == -1) {
587 errx(1, "failed to upload request");