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 18c37abb 2024-04-17 stsp #include "got_lib_pkt.h"
36 4c316997 2024-04-15 me #include "bufio.h"
38 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_ADV "application/x-git-upload-pack-advertisement"
39 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_REQ "application/x-git-upload-pack-request"
40 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_RES "application/x-git-upload-pack-result"
42 ad3b5b58 2024-04-14 me #define GOT_USERAGENT "got/" GOT_VERSION_STR
43 ad3b5b58 2024-04-14 me #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
44 ad3b5b58 2024-04-14 me #define hasprfx(str, p) (strncasecmp(str, p, strlen(p)) == 0)
48 ad3b5b58 2024-04-14 me static int verbose;
51 4c316997 2024-04-15 me bufio_getdelim_sync(struct bufio *bio, const char *nl, size_t *len)
56 4c316997 2024-04-15 me r = bufio_read(bio);
57 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
58 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
59 4c316997 2024-04-15 me } while (r == -1 && errno == EAGAIN);
60 4c316997 2024-04-15 me return buf_getdelim(&bio->rbuf, nl, len);
64 4c316997 2024-04-15 me bufio_drain_sync(struct bufio *bio, void *d, size_t len)
69 4c316997 2024-04-15 me r = bufio_read(bio);
70 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
71 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
72 4c316997 2024-04-15 me } while (r == -1 && errno == EAGAIN);
73 4c316997 2024-04-15 me return bufio_drain(bio, d, len);
77 4c316997 2024-04-15 me bufio_close_sync(struct bufio *bio)
82 4c316997 2024-04-15 me r = bufio_close(bio);
83 4c316997 2024-04-15 me if (r == -1 && errno == EAGAIN)
84 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
85 4c316997 2024-04-15 me } while (r == -1 && errno == EAGAIN);
88 ad3b5b58 2024-04-14 me static long long
89 ad3b5b58 2024-04-14 me hexstrtonum(const char *str, long long min, long long max, const char **errstr)
91 ad3b5b58 2024-04-14 me long long lval;
95 ad3b5b58 2024-04-14 me lval = strtoll(str, &cp, 16);
96 ad3b5b58 2024-04-14 me if (*str == '\0' || *cp != '\0') {
97 ad3b5b58 2024-04-14 me *errstr = "not a number";
100 ad3b5b58 2024-04-14 me if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
101 ad3b5b58 2024-04-14 me lval < min || lval > max) {
102 ad3b5b58 2024-04-14 me *errstr = "out of range";
106 ad3b5b58 2024-04-14 me *errstr = NULL;
111 ad3b5b58 2024-04-14 me dial(int https, const char *host, const char *port)
113 ad3b5b58 2024-04-14 me struct addrinfo hints, *res, *res0;
114 4c316997 2024-04-15 me int error, saved_errno, fd = -1;
115 ad3b5b58 2024-04-14 me const char *cause = NULL;
117 ad3b5b58 2024-04-14 me memset(&hints, 0, sizeof(hints));
118 ad3b5b58 2024-04-14 me hints.ai_family = AF_UNSPEC;
119 ad3b5b58 2024-04-14 me hints.ai_socktype = SOCK_STREAM;
120 ad3b5b58 2024-04-14 me error = getaddrinfo(host, port, &hints, &res0);
122 ad3b5b58 2024-04-14 me warnx("%s", gai_strerror(error));
126 ad3b5b58 2024-04-14 me for (res = res0; res; res = res->ai_next) {
127 ad3b5b58 2024-04-14 me fd = socket(res->ai_family, res->ai_socktype,
128 ad3b5b58 2024-04-14 me res->ai_protocol);
129 ad3b5b58 2024-04-14 me if (fd == -1) {
130 ad3b5b58 2024-04-14 me cause = "socket";
134 ad3b5b58 2024-04-14 me if (connect(fd, res->ai_addr, res->ai_addrlen) == 0)
137 ad3b5b58 2024-04-14 me cause = "connect";
138 ad3b5b58 2024-04-14 me saved_errno = errno;
141 ad3b5b58 2024-04-14 me errno = saved_errno;
143 ad3b5b58 2024-04-14 me freeaddrinfo(res0);
145 ad3b5b58 2024-04-14 me if (fd == -1) {
146 ad3b5b58 2024-04-14 me warn("%s", cause);
154 4c316997 2024-04-15 me http_open(struct bufio *bio, int https, const char *method, const char *host, const char *port,
155 4c316997 2024-04-15 me const char *path, const char *path_sufx, const char *query, const char *ctype)
157 ad3b5b58 2024-04-14 me const char *chdr = NULL, *te = "";
158 ad3b5b58 2024-04-14 me char *p, *req;
161 ad3b5b58 2024-04-14 me if (path_sufx != NULL && *path && path[strlen(path) - 1] == '/')
162 ad3b5b58 2024-04-14 me path_sufx++; /* skip the slash */
164 ad3b5b58 2024-04-14 me if (strcmp(method, "POST") == 0)
165 ad3b5b58 2024-04-14 me te = "\r\nTransfer-Encoding: chunked\r\n";
168 ad3b5b58 2024-04-14 me chdr = "Content-Type: ";
170 ad3b5b58 2024-04-14 me r = asprintf(&p, "%s/%s%s%s", path, path_sufx,
171 ad3b5b58 2024-04-14 me query ? "?" : "", query ? query : "");
173 ad3b5b58 2024-04-14 me err(1, "asprintf");
175 ad3b5b58 2024-04-14 me r = asprintf(&req, "%s %s HTTP/1.1\r\n"
176 ad3b5b58 2024-04-14 me "Host: %s\r\n"
177 ad3b5b58 2024-04-14 me "Connection: close\r\n"
178 ad3b5b58 2024-04-14 me "User-agent: %s\r\n"
179 ad3b5b58 2024-04-14 me "%s%s%s\r\n",
180 ad3b5b58 2024-04-14 me method, p, host, GOT_USERAGENT,
181 ad3b5b58 2024-04-14 me chdr ? chdr : "", ctype ? ctype : "", te);
183 ad3b5b58 2024-04-14 me err(1, "asprintf");
186 ad3b5b58 2024-04-14 me if (verbose > 0)
187 4c094842 2024-04-17 stsp fprintf(stderr, "%s: request: %s\n", getprogname(), req);
190 4c316997 2024-04-15 me r = bufio_compose(bio, req, r);
192 4c316997 2024-04-15 me err(1, "bufio_compose_fmt");
196 4c316997 2024-04-15 me r = bufio_write(bio);
197 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
198 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
199 4c316997 2024-04-15 me } while (bio->wbuf.len != 0);
205 4c316997 2024-04-15 me http_parse_reply(struct bufio *bio, int *chunked, const char *expected_ctype)
207 4c316997 2024-04-15 me char *cp, *line;
208 4c316997 2024-04-15 me size_t linelen;
210 ad3b5b58 2024-04-14 me *chunked = 0;
212 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
213 4c316997 2024-04-15 me if (line == NULL) {
214 4c316997 2024-04-15 me warnx("%s: bufio_getdelim_sync()", __func__);
218 fe24f0a3 2024-04-14 stsp if (verbose > 0)
219 4c094842 2024-04-17 stsp fprintf(stderr, "%s: response: %s\n", getprogname(), line);
221 ad3b5b58 2024-04-14 me if ((cp = strchr(line, ' ')) == NULL) {
222 ad3b5b58 2024-04-14 me warnx("malformed HTTP response");
227 ad3b5b58 2024-04-14 me if (strncmp(cp, "200 ", 4) != 0) {
228 ad3b5b58 2024-04-14 me warnx("malformed HTTP response");
231 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
234 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
235 4c316997 2024-04-15 me if (line == NULL) {
236 4c316997 2024-04-15 me warnx("%s: bufio_getdelim_sync()", __func__);
239 4c316997 2024-04-15 me if (*line == '\0') {
240 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
244 ad3b5b58 2024-04-14 me if (hasprfx(line, "content-type:")) {
245 ad3b5b58 2024-04-14 me cp = strchr(line, ':') + 1;
246 ad3b5b58 2024-04-14 me cp += strspn(cp, " \t");
247 ad3b5b58 2024-04-14 me cp[strcspn(cp, " \t")] = '\0';
248 ad3b5b58 2024-04-14 me if (strcmp(cp, expected_ctype) != 0) {
249 ad3b5b58 2024-04-14 me warnx("server not using the \"smart\" "
250 ad3b5b58 2024-04-14 me "HTTP protocol.");
254 ad3b5b58 2024-04-14 me if (hasprfx(line, "transfer-encoding:")) {
255 ad3b5b58 2024-04-14 me cp = strchr(line, ':') + 1;
256 ad3b5b58 2024-04-14 me cp += strspn(cp, " \t");
257 ad3b5b58 2024-04-14 me cp[strcspn(cp, " \t")] = '\0';
258 ad3b5b58 2024-04-14 me if (strcmp(cp, "chunked") != 0) {
259 ad3b5b58 2024-04-14 me warnx("unknown transfer-encoding");
262 ad3b5b58 2024-04-14 me *chunked = 1;
264 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
270 ad3b5b58 2024-04-14 me static ssize_t
271 4c316997 2024-04-15 me http_read(struct bufio *bio, int chunked, size_t *chunksz, char *buf, size_t bufsz)
273 ad3b5b58 2024-04-14 me const char *errstr;
274 4c316997 2024-04-15 me char *line = NULL;
276 ad3b5b58 2024-04-14 me ssize_t ret = 0, linelen;
278 ad3b5b58 2024-04-14 me if (!chunked) {
279 4c316997 2024-04-15 me r = bufio_drain_sync(bio, buf, bufsz);
285 ad3b5b58 2024-04-14 me while (bufsz > 0) {
286 ad3b5b58 2024-04-14 me if (*chunksz == 0) {
288 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
289 4c316997 2024-04-15 me if (line == NULL) {
290 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
293 4c316997 2024-04-15 me if (*line == '\0') {
294 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
295 4c316997 2024-04-15 me goto again; /* was the CRLF after the chunk */
298 ad3b5b58 2024-04-14 me *chunksz = hexstrtonum(line, 0, INT_MAX, &errstr);
299 ad3b5b58 2024-04-14 me if (errstr != NULL) {
300 ad3b5b58 2024-04-14 me warnx("invalid HTTP chunk: size is %s (%s)",
301 ad3b5b58 2024-04-14 me errstr, line);
306 4c316997 2024-04-15 me if (*chunksz == 0) {
307 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
310 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
313 4c316997 2024-04-15 me r = bufio_drain_sync(bio, buf, MINIMUM(*chunksz, bufsz));
314 ad3b5b58 2024-04-14 me if (r == 0) {
321 ad3b5b58 2024-04-14 me *chunksz -= r;
328 4c316997 2024-04-15 me http_chunk(struct bufio *bio, const void *buf, size_t len)
332 4c316997 2024-04-15 me if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
333 4c316997 2024-04-15 me bufio_compose(bio, buf, len) ||
334 4c316997 2024-04-15 me bufio_compose(bio, "\r\n", 2))
338 4c316997 2024-04-15 me r = bufio_write(bio);
339 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
340 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
341 4c316997 2024-04-15 me } while (bio->wbuf.len != 0);
347 ad3b5b58 2024-04-14 me get_refs(int https, const char *host, const char *port, const char *path)
349 4c316997 2024-04-15 me struct bufio bio;
350 18c37abb 2024-04-17 stsp char buf[GOT_PKT_MAX];
351 ad3b5b58 2024-04-14 me const char *errstr, *sufx = "/info/refs";
352 ad3b5b58 2024-04-14 me size_t skip, chunksz = 0;
356 4c316997 2024-04-15 me int ret = -1;
358 4c316997 2024-04-15 me if ((sock = dial(https, host, port)) == -1)
361 4c316997 2024-04-15 me if (bufio_init(&bio)) {
362 4c316997 2024-04-15 me warnx("bufio_init");
365 4c316997 2024-04-15 me bufio_set_fd(&bio, sock);
366 4c316997 2024-04-15 me if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
367 4c316997 2024-04-15 me warnx("bufio_starttls");
371 4c316997 2024-04-15 me if (http_open(&bio, https, "GET", host, port, path, sufx,
372 4c316997 2024-04-15 me "service=git-upload-pack", NULL) == -1)
375 b1ebf3b3 2024-04-16 stsp /* Fetch the initial reference announcement from the server. */
376 4c316997 2024-04-15 me if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_ADV) == -1)
379 ad3b5b58 2024-04-14 me /* skip first pack; why git over http is like this? */
380 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, 4);
383 ad3b5b58 2024-04-14 me buf[4] = '\0';
384 ad3b5b58 2024-04-14 me skip = hexstrtonum(buf, 0, INT_MAX, &errstr);
385 ad3b5b58 2024-04-14 me if (errstr != NULL) {
386 ad3b5b58 2024-04-14 me warnx("pktlen is %s", errstr);
390 ad3b5b58 2024-04-14 me /* TODO: validate it's # service=git-upload-pack\n */
391 ad3b5b58 2024-04-14 me while (skip > 0) {
392 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf,
393 ad3b5b58 2024-04-14 me MINIMUM(skip, sizeof(buf)));
400 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
407 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, stdout);
410 ad3b5b58 2024-04-14 me fflush(stdout);
413 4c316997 2024-04-15 me bufio_close_sync(&bio);
414 4c316997 2024-04-15 me bufio_free(&bio);
419 ad3b5b58 2024-04-14 me upload_request(int https, const char *host, const char *port, const char *path,
422 4c316997 2024-04-15 me struct bufio bio;
423 ad3b5b58 2024-04-14 me const char *errstr;
424 18c37abb 2024-04-17 stsp char buf[GOT_PKT_MAX];
426 ad3b5b58 2024-04-14 me size_t chunksz = 0;
430 4c316997 2024-04-15 me int ret = -1;
432 4c316997 2024-04-15 me if ((sock = dial(https, host, port)) == -1)
435 4c316997 2024-04-15 me if (bufio_init(&bio)) {
436 4c316997 2024-04-15 me warnx("bufio_init");
439 4c316997 2024-04-15 me bufio_set_fd(&bio, sock);
440 4c316997 2024-04-15 me if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
441 4c316997 2024-04-15 me warnx("bufio_starttls");
444 44740114 2024-04-17 stsp #ifndef PROFILE
445 44740114 2024-04-17 stsp /* TODO: can we push this upwards such that get_refs() is covered? */
446 44740114 2024-04-17 stsp if (pledge("stdio", NULL) == -1)
447 44740114 2024-04-17 stsp err(1, "pledge");
449 4c316997 2024-04-15 me if (http_open(&bio, https, "POST", host, port, path, "/git-upload-pack",
450 4c316997 2024-04-15 me NULL, UPLOAD_PACK_REQ) == -1)
454 b1ebf3b3 2024-04-16 stsp * Read have/want lines generated by got-fetch-pack and forward
455 b1ebf3b3 2024-04-16 stsp * them to the server in the POST request body.
458 ad3b5b58 2024-04-14 me r = fread(buf, 1, 4, in);
462 ad3b5b58 2024-04-14 me buf[4] = '\0';
463 ad3b5b58 2024-04-14 me t = hexstrtonum(buf, 0, sizeof(buf), &errstr);
464 ad3b5b58 2024-04-14 me if (errstr != NULL) {
465 ad3b5b58 2024-04-14 me warnx("pktline len is %s", errstr);
469 ad3b5b58 2024-04-14 me /* no idea why 0000 is not enough. */
470 ad3b5b58 2024-04-14 me if (t == 0) {
471 ad3b5b58 2024-04-14 me const char *x = "00000009done\n";
472 4c316997 2024-04-15 me if (http_chunk(&bio, x, strlen(x)) ||
473 4c316997 2024-04-15 me http_chunk(&bio, NULL, 0))
479 ad3b5b58 2024-04-14 me warnx("pktline len is too small");
483 ad3b5b58 2024-04-14 me r = fread(buf + 4, 1, t - 4, in);
484 ad3b5b58 2024-04-14 me if (r != t - 4)
487 4c316997 2024-04-15 me if (http_chunk(&bio, buf, t))
491 4c316997 2024-04-15 me if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_RES) == -1)
494 b1ebf3b3 2024-04-16 stsp /* Fetch pack file data from server. */
496 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
503 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, stdout);
508 4c316997 2024-04-15 me bufio_close_sync(&bio);
509 4c316997 2024-04-15 me bufio_free(&bio);
513 ad3b5b58 2024-04-14 me static __dead void
516 ad3b5b58 2024-04-14 me fprintf(stderr, "usage: %s [-qv] proto host port path\n",
517 ad3b5b58 2024-04-14 me getprogname());
522 ad3b5b58 2024-04-14 me main(int argc, char **argv)
524 ad3b5b58 2024-04-14 me struct pollfd pfd;
525 ad3b5b58 2024-04-14 me const char *host, *port, *path;
526 ad3b5b58 2024-04-14 me int https = 0;
529 c4caaee8 2024-04-17 stsp #ifndef PROFILE
530 e8452664 2024-04-17 stsp if (pledge("stdio rpath inet dns unveil", NULL) == -1)
531 ad3b5b58 2024-04-14 me err(1, "pledge");
534 ad3b5b58 2024-04-14 me while ((ch = getopt(argc, argv, "qv")) != -1) {
535 ad3b5b58 2024-04-14 me switch (ch) {
537 ad3b5b58 2024-04-14 me verbose = -1;
546 ad3b5b58 2024-04-14 me argc -= optind;
547 ad3b5b58 2024-04-14 me argv += optind;
549 ad3b5b58 2024-04-14 me if (argc != 4)
552 ad3b5b58 2024-04-14 me https = strcmp(argv[0], "https") == 0;
553 625c83c9 2024-04-17 stsp #ifndef PROFILE
554 e8452664 2024-04-17 stsp if (https) {
555 e8452664 2024-04-17 stsp if (unveil("/etc/ssl/cert.pem", "r") == -1)
556 e8452664 2024-04-17 stsp err(1, "unveil /etc/ssl/cert.pem");
558 625c83c9 2024-04-17 stsp /* drop "rpath" */
559 e8452664 2024-04-17 stsp if (pledge("stdio inet dns unveil", NULL) == -1)
560 625c83c9 2024-04-17 stsp err(1, "pledge");
563 e8452664 2024-04-17 stsp if (unveil("gmon.out", "rwc") != 0)
564 e8452664 2024-04-17 stsp err(1, "unveil gmon.out");
566 e8452664 2024-04-17 stsp if (unveil(NULL, NULL) == -1)
567 e8452664 2024-04-17 stsp err(1, "unveil NULL");
569 ad3b5b58 2024-04-14 me host = argv[1];
570 ad3b5b58 2024-04-14 me port = argv[2];
571 ad3b5b58 2024-04-14 me path = argv[3];
573 ad3b5b58 2024-04-14 me if (get_refs(https, host, port, path) == -1)
574 ad3b5b58 2024-04-14 me errx(1, "failed to get refs");
577 ad3b5b58 2024-04-14 me pfd.events = POLLIN;
578 ad3b5b58 2024-04-14 me if (poll(&pfd, 1, INFTIM) == -1)
579 ad3b5b58 2024-04-14 me err(1, "poll");
581 ad3b5b58 2024-04-14 me if ((ch = fgetc(stdin)) == EOF)
584 ad3b5b58 2024-04-14 me ungetc(ch, stdin);
585 ad3b5b58 2024-04-14 me if (upload_request(https, host, port, path, stdin) == -1) {
587 ad3b5b58 2024-04-14 me errx(1, "failed to upload request");