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 21679dc5 2024-04-22 me #include "got_error.h"
33 ad3b5b58 2024-04-14 me #include "got_version.h"
35 18c37abb 2024-04-17 stsp #include "got_lib_pkt.h"
37 4c316997 2024-04-15 me #include "bufio.h"
39 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_ADV "application/x-git-upload-pack-advertisement"
40 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_REQ "application/x-git-upload-pack-request"
41 ad3b5b58 2024-04-14 me #define UPLOAD_PACK_RES "application/x-git-upload-pack-result"
43 ad3b5b58 2024-04-14 me #define GOT_USERAGENT "got/" GOT_VERSION_STR
44 ad3b5b58 2024-04-14 me #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
45 ad3b5b58 2024-04-14 me #define hasprfx(str, p) (strncasecmp(str, p, strlen(p)) == 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);
245 ad3b5b58 2024-04-14 me if (hasprfx(line, "content-type:")) {
246 ad3b5b58 2024-04-14 me cp = strchr(line, ':') + 1;
247 ad3b5b58 2024-04-14 me cp += strspn(cp, " \t");
248 ad3b5b58 2024-04-14 me cp[strcspn(cp, " \t")] = '\0';
249 ad3b5b58 2024-04-14 me if (strcmp(cp, expected_ctype) != 0) {
250 ad3b5b58 2024-04-14 me warnx("server not using the \"smart\" "
251 ad3b5b58 2024-04-14 me "HTTP protocol.");
255 ad3b5b58 2024-04-14 me if (hasprfx(line, "transfer-encoding:")) {
256 ad3b5b58 2024-04-14 me cp = strchr(line, ':') + 1;
257 ad3b5b58 2024-04-14 me cp += strspn(cp, " \t");
258 ad3b5b58 2024-04-14 me cp[strcspn(cp, " \t")] = '\0';
259 ad3b5b58 2024-04-14 me if (strcmp(cp, "chunked") != 0) {
260 ad3b5b58 2024-04-14 me warnx("unknown transfer-encoding");
263 ad3b5b58 2024-04-14 me *chunked = 1;
265 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
271 ad3b5b58 2024-04-14 me static ssize_t
272 4c316997 2024-04-15 me http_read(struct bufio *bio, int chunked, size_t *chunksz, char *buf, size_t bufsz)
274 ad3b5b58 2024-04-14 me const char *errstr;
275 4c316997 2024-04-15 me char *line = NULL;
277 ad3b5b58 2024-04-14 me ssize_t ret = 0, linelen;
279 ad3b5b58 2024-04-14 me if (!chunked) {
280 4c316997 2024-04-15 me r = bufio_drain_sync(bio, buf, bufsz);
286 ad3b5b58 2024-04-14 me while (bufsz > 0) {
287 ad3b5b58 2024-04-14 me if (*chunksz == 0) {
289 4c316997 2024-04-15 me line = bufio_getdelim_sync(bio, "\r\n", &linelen);
290 4c316997 2024-04-15 me if (line == NULL) {
291 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
294 4c316997 2024-04-15 me if (*line == '\0') {
295 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
296 4c316997 2024-04-15 me goto again; /* was the CRLF after the chunk */
299 ad3b5b58 2024-04-14 me *chunksz = hexstrtonum(line, 0, INT_MAX, &errstr);
300 ad3b5b58 2024-04-14 me if (errstr != NULL) {
301 ad3b5b58 2024-04-14 me warnx("invalid HTTP chunk: size is %s (%s)",
302 ad3b5b58 2024-04-14 me errstr, line);
307 4c316997 2024-04-15 me if (*chunksz == 0) {
308 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
311 4c316997 2024-04-15 me buf_drain(&bio->rbuf, linelen);
314 4c316997 2024-04-15 me r = bufio_drain_sync(bio, buf, MINIMUM(*chunksz, bufsz));
315 ad3b5b58 2024-04-14 me if (r == 0) {
322 ad3b5b58 2024-04-14 me *chunksz -= r;
329 4c316997 2024-04-15 me http_chunk(struct bufio *bio, const void *buf, size_t len)
333 4c316997 2024-04-15 me if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
334 4c316997 2024-04-15 me bufio_compose(bio, buf, len) ||
335 4c316997 2024-04-15 me bufio_compose(bio, "\r\n", 2))
339 4c316997 2024-04-15 me r = bufio_write(bio);
340 4c316997 2024-04-15 me if (r == -1 && errno != EAGAIN)
341 4c316997 2024-04-15 me errx(1, "bufio_read: %s", bufio_io_err(bio));
342 4c316997 2024-04-15 me } while (bio->wbuf.len != 0);
348 ad3b5b58 2024-04-14 me get_refs(int https, const char *host, const char *port, const char *path)
350 21679dc5 2024-04-22 me struct bufio bio;
351 21679dc5 2024-04-22 me char buf[GOT_PKT_MAX];
352 21679dc5 2024-04-22 me const struct got_error *e;
353 21679dc5 2024-04-22 me const char *sufx = "/info/refs";
354 21679dc5 2024-04-22 me size_t chunksz = 0;
359 21679dc5 2024-04-22 me int ret = -1;
361 4c316997 2024-04-15 me if ((sock = dial(https, host, port)) == -1)
364 4c316997 2024-04-15 me if (bufio_init(&bio)) {
365 4c316997 2024-04-15 me warnx("bufio_init");
368 4c316997 2024-04-15 me bufio_set_fd(&bio, sock);
369 4c316997 2024-04-15 me if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
370 4c316997 2024-04-15 me warnx("bufio_starttls");
374 4c316997 2024-04-15 me if (http_open(&bio, https, "GET", host, port, path, sufx,
375 4c316997 2024-04-15 me "service=git-upload-pack", NULL) == -1)
378 b1ebf3b3 2024-04-16 stsp /* Fetch the initial reference announcement from the server. */
379 4c316997 2024-04-15 me if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_ADV) == -1)
382 ad3b5b58 2024-04-14 me /* skip first pack; why git over http is like this? */
383 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, 4);
387 21679dc5 2024-04-22 me e = got_pkt_readlen(&skip, buf, verbose);
389 21679dc5 2024-04-22 me warnx("%s", e->msg);
393 ad3b5b58 2024-04-14 me /* TODO: validate it's # service=git-upload-pack\n */
394 ad3b5b58 2024-04-14 me while (skip > 0) {
395 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf,
396 ad3b5b58 2024-04-14 me MINIMUM(skip, sizeof(buf)));
403 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
410 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, stdout);
413 ad3b5b58 2024-04-14 me fflush(stdout);
416 4c316997 2024-04-15 me bufio_close_sync(&bio);
417 4c316997 2024-04-15 me bufio_free(&bio);
422 ad3b5b58 2024-04-14 me upload_request(int https, const char *host, const char *port, const char *path,
425 21679dc5 2024-04-22 me struct bufio bio;
426 21679dc5 2024-04-22 me char buf[GOT_PKT_MAX];
427 21679dc5 2024-04-22 me const struct got_error *e;
429 21679dc5 2024-04-22 me size_t chunksz = 0;
433 21679dc5 2024-04-22 me int ret = -1;
435 4c316997 2024-04-15 me if ((sock = dial(https, host, port)) == -1)
438 4c316997 2024-04-15 me if (bufio_init(&bio)) {
439 4c316997 2024-04-15 me warnx("bufio_init");
442 4c316997 2024-04-15 me bufio_set_fd(&bio, sock);
443 4c316997 2024-04-15 me if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
444 4c316997 2024-04-15 me warnx("bufio_starttls");
447 44740114 2024-04-17 stsp #ifndef PROFILE
448 44740114 2024-04-17 stsp /* TODO: can we push this upwards such that get_refs() is covered? */
449 44740114 2024-04-17 stsp if (pledge("stdio", NULL) == -1)
450 44740114 2024-04-17 stsp err(1, "pledge");
452 4c316997 2024-04-15 me if (http_open(&bio, https, "POST", host, port, path, "/git-upload-pack",
453 4c316997 2024-04-15 me NULL, UPLOAD_PACK_REQ) == -1)
457 b1ebf3b3 2024-04-16 stsp * Read have/want lines generated by got-fetch-pack and forward
458 b1ebf3b3 2024-04-16 stsp * them to the server in the POST request body.
461 ad3b5b58 2024-04-14 me r = fread(buf, 1, 4, in);
465 21679dc5 2024-04-22 me e = got_pkt_readlen(&t, buf, verbose);
467 21679dc5 2024-04-22 me warnx("%s", e->msg);
471 ad3b5b58 2024-04-14 me if (t == 0) {
472 d7988696 2024-04-19 stsp const char *flushpkt = "0000";
473 d7988696 2024-04-19 stsp if (http_chunk(&bio, flushpkt, strlen(flushpkt)))
475 d7988696 2024-04-19 stsp continue; /* got-fetch-pack will send "done" */
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 d7988696 2024-04-19 stsp * Once got-fetch-pack is done the server will
492 d7988696 2024-04-19 stsp * send pack file data.
494 d7988696 2024-04-19 stsp if (t == 9 && strncmp(buf + 4, "done\n", 5) == 0) {
495 d7988696 2024-04-19 stsp if (http_chunk(&bio, NULL, 0))
501 4c316997 2024-04-15 me if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_RES) == -1)
504 b1ebf3b3 2024-04-16 stsp /* Fetch pack file data from server. */
506 4c316997 2024-04-15 me r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
513 ad3b5b58 2024-04-14 me fwrite(buf, 1, r, stdout);
518 4c316997 2024-04-15 me bufio_close_sync(&bio);
519 4c316997 2024-04-15 me bufio_free(&bio);
523 ad3b5b58 2024-04-14 me static __dead void
526 ad3b5b58 2024-04-14 me fprintf(stderr, "usage: %s [-qv] proto host port path\n",
527 ad3b5b58 2024-04-14 me getprogname());
532 ad3b5b58 2024-04-14 me main(int argc, char **argv)
534 ad3b5b58 2024-04-14 me struct pollfd pfd;
535 ad3b5b58 2024-04-14 me const char *host, *port, *path;
536 ad3b5b58 2024-04-14 me int https = 0;
539 c4caaee8 2024-04-17 stsp #ifndef PROFILE
540 e8452664 2024-04-17 stsp if (pledge("stdio rpath inet dns unveil", NULL) == -1)
541 ad3b5b58 2024-04-14 me err(1, "pledge");
544 ad3b5b58 2024-04-14 me while ((ch = getopt(argc, argv, "qv")) != -1) {
545 ad3b5b58 2024-04-14 me switch (ch) {
547 ad3b5b58 2024-04-14 me verbose = -1;
556 ad3b5b58 2024-04-14 me argc -= optind;
557 ad3b5b58 2024-04-14 me argv += optind;
559 ad3b5b58 2024-04-14 me if (argc != 4)
562 ad3b5b58 2024-04-14 me https = strcmp(argv[0], "https") == 0;
563 625c83c9 2024-04-17 stsp #ifndef PROFILE
564 e8452664 2024-04-17 stsp if (https) {
565 e8452664 2024-04-17 stsp if (unveil("/etc/ssl/cert.pem", "r") == -1)
566 e8452664 2024-04-17 stsp err(1, "unveil /etc/ssl/cert.pem");
568 625c83c9 2024-04-17 stsp /* drop "rpath" */
569 e8452664 2024-04-17 stsp if (pledge("stdio inet dns unveil", NULL) == -1)
570 625c83c9 2024-04-17 stsp err(1, "pledge");
573 e8452664 2024-04-17 stsp if (unveil("gmon.out", "rwc") != 0)
574 e8452664 2024-04-17 stsp err(1, "unveil gmon.out");
576 e8452664 2024-04-17 stsp if (unveil(NULL, NULL) == -1)
577 e8452664 2024-04-17 stsp err(1, "unveil NULL");
579 ad3b5b58 2024-04-14 me host = argv[1];
580 ad3b5b58 2024-04-14 me port = argv[2];
581 ad3b5b58 2024-04-14 me path = argv[3];
583 ad3b5b58 2024-04-14 me if (get_refs(https, host, port, path) == -1)
584 ad3b5b58 2024-04-14 me errx(1, "failed to get refs");
587 ad3b5b58 2024-04-14 me pfd.events = POLLIN;
588 ad3b5b58 2024-04-14 me if (poll(&pfd, 1, INFTIM) == -1)
589 ad3b5b58 2024-04-14 me err(1, "poll");
591 ad3b5b58 2024-04-14 me if ((ch = fgetc(stdin)) == EOF)
594 ad3b5b58 2024-04-14 me ungetc(ch, stdin);
595 ad3b5b58 2024-04-14 me if (upload_request(https, host, port, path, stdin) == -1) {
597 ad3b5b58 2024-04-14 me errx(1, "failed to upload request");