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/queue.h>
20 #include <sys/socket.h>
33 #include "got_error.h"
35 #include "got_version.h"
37 #include "got_lib_pkt.h"
41 #define UPLOAD_PACK_ADV "application/x-git-upload-pack-advertisement"
42 #define UPLOAD_PACK_REQ "application/x-git-upload-pack-request"
43 #define UPLOAD_PACK_RES "application/x-git-upload-pack-result"
45 #define GOT_USERAGENT "got/" GOT_VERSION_STR
46 #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
47 #define hasprfx(str, p) (strncasecmp(str, p, strlen(p)) == 0)
54 bufio_getdelim_sync(struct bufio *bio, const char *nl, size_t *len)
60 if (r == -1 && errno != EAGAIN)
61 errx(1, "bufio_read: %s", bufio_io_err(bio));
62 } while (r == -1 && errno == EAGAIN);
63 return buf_getdelim(&bio->rbuf, nl, len);
67 bufio_drain_sync(struct bufio *bio, void *d, size_t len)
73 if (r == -1 && errno != EAGAIN)
74 errx(1, "bufio_read: %s", bufio_io_err(bio));
75 } while (r == -1 && errno == EAGAIN);
76 return bufio_drain(bio, d, len);
80 bufio_close_sync(struct bufio *bio)
86 if (r == -1 && errno == EAGAIN)
87 errx(1, "bufio_read: %s", bufio_io_err(bio));
88 } while (r == -1 && errno == EAGAIN);
92 hexstrtonum(const char *str, long long min, long long max, const char **errstr)
98 lval = strtoll(str, &cp, 16);
99 if (*str == '\0' || *cp != '\0') {
100 *errstr = "not a number";
103 if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
104 lval < min || lval > max) {
105 *errstr = "out of range";
114 dial(int https, const char *host, const char *port)
116 struct addrinfo hints, *res, *res0;
117 int error, saved_errno, fd = -1;
118 const char *cause = NULL;
120 memset(&hints, 0, sizeof(hints));
121 hints.ai_family = AF_UNSPEC;
122 hints.ai_socktype = SOCK_STREAM;
123 error = getaddrinfo(host, port, &hints, &res0);
125 warnx("%s", gai_strerror(error));
129 for (res = res0; res; res = res->ai_next) {
130 fd = socket(res->ai_family, res->ai_socktype,
137 if (connect(fd, res->ai_addr, res->ai_addrlen) == 0)
157 http_open(struct bufio *bio, int https, const char *method, const char *host, const char *port,
158 const char *path, const char *path_sufx, const char *query, const char *ctype)
160 const char *chdr = NULL, *te = "";
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%s", got_path_is_absolute(path) ? "" :"/",
171 path, path_sufx, 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 return bufio_drain_sync(bio, buf, bufsz);
284 line = bufio_getdelim_sync(bio, "\r\n", &linelen);
286 buf_drain(&bio->rbuf, linelen);
290 buf_drain(&bio->rbuf, linelen);
291 goto again; /* was the CRLF after the chunk */
294 *chunksz = hexstrtonum(line, 0, INT_MAX, &errstr);
295 if (errstr != NULL) {
296 warnx("invalid HTTP chunk: size is %s (%s)",
303 buf_drain(&bio->rbuf, linelen);
306 buf_drain(&bio->rbuf, linelen);
309 r = bufio_drain_sync(bio, buf, MINIMUM(*chunksz, bufsz));
324 http_chunk(struct bufio *bio, const void *buf, size_t len)
328 if (bufio_compose_fmt(bio, "%zx\r\n", len) ||
329 bufio_compose(bio, buf, len) ||
330 bufio_compose(bio, "\r\n", 2))
334 r = bufio_write(bio);
335 if (r == -1 && errno != EAGAIN)
336 errx(1, "bufio_read: %s", bufio_io_err(bio));
337 } while (bio->wbuf.len != 0);
343 get_refs(int https, const char *host, const char *port, const char *path)
346 char buf[GOT_PKT_MAX];
347 const struct got_error *e;
355 if ((sock = dial(https, host, port)) == -1)
358 if (bufio_init(&bio)) {
362 bufio_set_fd(&bio, sock);
363 if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
364 warnx("bufio_starttls");
368 if (http_open(&bio, https, "GET", host, port, path, "info/refs",
369 "service=git-upload-pack", NULL) == -1)
372 /* Fetch the initial reference announcement from the server. */
373 if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_ADV) == -1)
376 /* skip first pack; why git over http is like this? */
377 r = http_read(&bio, chunked, &chunksz, buf, 4);
381 e = got_pkt_readlen(&skip, buf, verbose);
387 /* TODO: validate it's # service=git-upload-pack\n */
389 r = http_read(&bio, chunked, &chunksz, buf,
390 MINIMUM(skip, sizeof(buf)));
397 r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
404 fwrite(buf, 1, r, stdout);
410 bufio_close_sync(&bio);
416 upload_request(int https, const char *host, const char *port, const char *path,
420 char buf[GOT_PKT_MAX];
421 const struct got_error *e;
429 if ((sock = dial(https, host, port)) == -1)
432 if (bufio_init(&bio)) {
436 bufio_set_fd(&bio, sock);
437 if (https && bufio_starttls(&bio, host, 0, NULL, 0, NULL, 0) == -1) {
438 warnx("bufio_starttls");
442 /* TODO: can we push this upwards such that get_refs() is covered? */
443 if (pledge("stdio", NULL) == -1)
446 if (http_open(&bio, https, "POST", host, port, path, "git-upload-pack",
447 NULL, UPLOAD_PACK_REQ) == -1)
451 * Read have/want lines generated by got-fetch-pack and forward
452 * them to the server in the POST request body.
455 r = fread(buf, 1, 4, in);
459 e = got_pkt_readlen(&t, buf, verbose);
466 const char *flushpkt = "0000";
467 if (http_chunk(&bio, flushpkt, strlen(flushpkt)))
469 continue; /* got-fetch-pack will send "done" */
473 warnx("pktline len is too small");
477 r = fread(buf + 4, 1, t - 4, in);
481 if (http_chunk(&bio, buf, t))
485 * Once got-fetch-pack is done the server will
486 * send pack file data.
488 if (t == 9 && strncmp(buf + 4, "done\n", 5) == 0) {
489 if (http_chunk(&bio, NULL, 0))
495 if (http_parse_reply(&bio, &chunked, UPLOAD_PACK_RES) == -1)
498 /* Fetch pack file data from server. */
500 r = http_read(&bio, chunked, &chunksz, buf, sizeof(buf));
507 fwrite(buf, 1, r, stdout);
512 bufio_close_sync(&bio);
520 fprintf(stderr, "usage: %s [-qv] proto host port path\n",
526 main(int argc, char **argv)
529 const char *host, *port;
535 if (pledge("stdio rpath inet dns unveil", NULL) == -1)
539 while ((ch = getopt(argc, argv, "qv")) != -1) {
557 https = strcmp(argv[0], "https") == 0;
560 if (unveil("/etc/ssl/cert.pem", "r") == -1)
561 err(1, "unveil /etc/ssl/cert.pem");
564 if (pledge("stdio inet dns unveil", NULL) == -1)
568 if (unveil("gmon.out", "rwc") != 0)
569 err(1, "unveil gmon.out");
571 if (unveil(NULL, NULL) == -1)
572 err(1, "unveil NULL");
577 got_path_strip_trailing_slashes(path);
579 if (get_refs(https, host, port, path) == -1)
580 errx(1, "failed to get refs");
584 if (poll(&pfd, 1, INFTIM) == -1)
587 if ((ch = fgetc(stdin)) == EOF)
591 if (upload_request(https, host, port, path, stdin) == -1) {
593 errx(1, "failed to upload request");