2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@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/queue.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
36 #include "got_error.h"
38 #include "got_object.h"
40 #include "got_lib_dial.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_privsep.h"
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #ifndef GOT_DIAL_PATH_SSH
59 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
63 #define GOT_DEFAULT_GIT_PORT 9418
64 #define GOT_DEFAULT_GIT_PORT_STR "9418"
66 const struct got_error *
67 got_dial_apply_unveil(const char *proto)
69 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
70 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
71 return got_error_from_errno2("unveil",
76 if (strstr(proto, "http") != NULL) {
77 if (unveil(GOT_PATH_PROG_FETCH_HTTP, "x") != 0) {
78 return got_error_from_errno2("unveil",
79 GOT_PATH_PROG_FETCH_HTTP);
87 hassuffix(const char *base, const char *suf)
93 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
98 const struct got_error *
99 got_dial_parse_uri(char **proto, char **host, char **port,
100 char **server_path, char **repo_name, const char *uri)
102 const struct got_error *err = NULL;
105 *proto = *host = *port = *server_path = *repo_name = NULL;
107 p = strstr(uri, "://");
109 /* Try parsing Git's "scp" style URL syntax. */
110 *proto = strdup("ssh");
111 if (*proto == NULL) {
112 err = got_error_from_errno("strdup");
118 err = got_error(GOT_ERR_PARSE_URI);
121 /* No slashes allowed before first colon. */
124 err = got_error(GOT_ERR_PARSE_URI);
127 *host = strndup(s, q - s);
129 err = got_error_from_errno("strndup");
132 if ((*host)[0] == '\0') {
133 err = got_error(GOT_ERR_PARSE_URI);
138 *proto = strndup(uri, p - uri);
139 if (*proto == NULL) {
140 err = got_error_from_errno("strndup");
146 if (p == NULL || strlen(p) == 1) {
147 err = got_error(GOT_ERR_PARSE_URI);
151 q = memchr(s, ':', p - s);
153 *host = strndup(s, q - s);
155 err = got_error_from_errno("strndup");
158 if ((*host)[0] == '\0') {
159 err = got_error(GOT_ERR_PARSE_URI);
162 *port = strndup(q + 1, p - (q + 1));
164 err = got_error_from_errno("strndup");
167 if ((*port)[0] == '\0') {
168 err = got_error(GOT_ERR_PARSE_URI);
172 *host = strndup(s, p - s);
174 err = got_error_from_errno("strndup");
177 if ((*host)[0] == '\0') {
178 err = got_error(GOT_ERR_PARSE_URI);
184 while (p[0] == '/' && (p[1] == '/' || p[1] == '~'))
186 *server_path = strdup(p);
187 if (*server_path == NULL) {
188 err = got_error_from_errno("strdup");
191 got_path_strip_trailing_slashes(*server_path);
192 if ((*server_path)[0] == '\0') {
193 err = got_error(GOT_ERR_PARSE_URI);
197 err = got_path_basename(repo_name, *server_path);
200 if (hassuffix(*repo_name, ".git"))
201 (*repo_name)[strlen(*repo_name) - 4] = '\0';
202 if ((*repo_name)[0] == '\0')
203 err = got_error(GOT_ERR_PARSE_URI);
221 * Escape a given path for the shell which will be started by sshd.
222 * In particular, git-shell is known to require single-quote characters
223 * around its repository path argument and will refuse to run otherwise.
225 static const struct got_error *
226 escape_path(char *buf, size_t bufsize, const char *path)
237 while (*p != '\0' && (q - buf < bufsize)) {
238 /* git escapes ! too */
239 if (*p != '\'' && *p != '!') {
244 if (q - buf + 4 >= bufsize)
252 if (*p == '\0' && (q - buf + 1 < bufsize)) {
258 return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
261 const struct got_error *
262 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
263 const char *port, const char *path, const char *command, int verbosity)
265 const struct got_error *error = NULL;
268 char escaped_path[PATH_MAX];
269 const char *argv[11];
275 error = escape_path(escaped_path, sizeof(escaped_path), path);
279 argv[i++] = GOT_DIAL_PATH_SSH;
282 argv[i++] = (char *)port;
284 if (verbosity == -1) {
287 /* ssh(1) allows up to 3 "-v" options. */
288 for (j = 0; j < MIN(3, verbosity); j++)
292 argv[i++] = (char *)host;
293 argv[i++] = (char *)cmd;
294 argv[i++] = (char *)escaped_path;
296 assert(i <= nitems(argv));
298 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
299 return got_error_from_errno("socketpair");
303 error = got_error_from_errno("fork");
307 } else if (pid == 0) {
308 if (close(pfd[1]) == -1)
310 if (dup2(pfd[0], 0) == -1)
312 if (dup2(pfd[0], 1) == -1)
314 if (strlcpy(cmd, command, sizeof(cmd)) >= sizeof(cmd))
316 if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
317 err(1, "execv %s", GOT_DIAL_PATH_SSH);
318 abort(); /* not reached */
320 if (close(pfd[0]) == -1)
321 return got_error_from_errno("close");
328 const struct got_error *
329 got_dial_git(int *newfd, const char *host, const char *port,
330 const char *path, const char *command)
332 const struct got_error *err = NULL;
333 struct addrinfo hints, *servinfo, *p;
335 int fd = -1, len, r, eaicode;
340 port = GOT_DEFAULT_GIT_PORT_STR;
342 memset(&hints, 0, sizeof hints);
343 hints.ai_family = AF_UNSPEC;
344 hints.ai_socktype = SOCK_STREAM;
345 eaicode = getaddrinfo(host, port, &hints, &servinfo);
348 snprintf(msg, sizeof(msg), "%s: %s", host,
349 gai_strerror(eaicode));
350 return got_error_msg(GOT_ERR_ADDRINFO, msg);
353 for (p = servinfo; p != NULL; p = p->ai_next) {
354 if ((fd = socket(p->ai_family, p->ai_socktype,
355 p->ai_protocol)) == -1)
357 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
361 err = got_error_from_errno("connect");
364 freeaddrinfo(servinfo);
368 if (asprintf(&cmd, "%s %s", command, path) == -1) {
369 err = got_error_from_errno("asprintf");
372 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
373 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
375 err = got_error_from_errno("dprintf");
386 const struct got_error *
387 got_dial_http(pid_t *newpid, int *newfd, const char *host,
388 const char *port, const char *path, int verbosity, int tls)
390 const struct got_error *error = NULL;
399 port = tls ? "443" : "80";
401 argv[i++] = GOT_PATH_PROG_FETCH_HTTP;
404 else if (verbosity > 0)
407 argv[i++] = tls ? "https" : "http";
412 assert(i <= nitems(argv));
414 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
415 return got_error_from_errno("socketpair");
419 error = got_error_from_errno("fork");
423 } else if (pid == 0) {
424 if (close(pfd[1]) == -1)
426 if (dup2(pfd[0], 0) == -1)
428 if (dup2(pfd[0], 1) == -1)
430 if (execv(GOT_PATH_PROG_FETCH_HTTP, (char *const *)argv) == -1)
431 err(1, "execv %s", GOT_PATH_PROG_FETCH_HTTP);
432 abort(); /* not reached */
434 if (close(pfd[0]) == -1)
435 return got_error_from_errno("close");
442 const struct got_error *
443 got_dial_parse_command(char **command, char **repo_path, const char *gitcmd)
445 const struct got_error *err = NULL;
446 size_t len, cmdlen, pathlen;
447 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
453 len = strlen(gitcmd);
455 if (len >= strlen(GOT_DIAL_CMD_SEND) &&
456 strncmp(gitcmd, GOT_DIAL_CMD_SEND,
457 strlen(GOT_DIAL_CMD_SEND)) == 0)
458 cmdlen = strlen(GOT_DIAL_CMD_SEND);
459 else if (len >= strlen(GOT_DIAL_CMD_FETCH) &&
460 strncmp(gitcmd, GOT_DIAL_CMD_FETCH,
461 strlen(GOT_DIAL_CMD_FETCH)) == 0)
462 cmdlen = strlen(GOT_DIAL_CMD_FETCH);
464 return got_error(GOT_ERR_BAD_PACKET);
466 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
467 return got_error(GOT_ERR_BAD_PACKET);
469 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
470 return got_error(GOT_ERR_BAD_PATH);
472 /* Forbid linefeeds in paths, like Git does. */
473 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
474 return got_error(GOT_ERR_BAD_PATH);
476 path0 = strdup(&gitcmd[cmdlen + 1]);
478 return got_error_from_errno("strdup");
480 pathlen = strlen(path);
483 * Git clients send a shell command.
484 * Trim spaces and quotes around the path.
486 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
490 while (pathlen > 0 &&
491 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
492 path[pathlen - 1] == ' ')) {
493 path[pathlen - 1] = '\0';
497 /* Deny an empty repository path. */
498 if (path[0] == '\0' || got_path_is_root_dir(path)) {
499 err = got_error(GOT_ERR_NOT_GIT_REPO);
503 if (asprintf(&abspath, "/%s", path) == -1) {
504 err = got_error_from_errno("asprintf");
507 pathlen = strlen(abspath);
508 canonpath = malloc(pathlen + 1);
509 if (canonpath == NULL) {
510 err = got_error_from_errno("malloc");
513 err = got_canonpath(abspath, canonpath, pathlen + 1);
518 while (relpath[0] == '/')
520 *repo_path = strdup(relpath);
521 if (*repo_path == NULL) {
522 err = got_error_from_errno("strdup");
525 *command = strndup(gitcmd, cmdlen);
526 if (*command == NULL)
527 err = got_error_from_errno("strndup");