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 "got_compat.h"
20 #include <sys/queue.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
33 #include "got_error.h"
36 #include "got_compat.h"
38 #include "got_lib_dial.h"
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #ifndef GOT_DIAL_PATH_SSH
54 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
58 #define GOT_DEFAULT_GIT_PORT 9418
59 #define GOT_DEFAULT_GIT_PORT_STR "9418"
61 const struct got_error *
62 got_dial_apply_unveil(const char *proto)
64 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
65 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
66 return got_error_from_errno2("unveil",
75 hassuffix(const char *base, const char *suf)
81 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
86 const struct got_error *
87 got_dial_parse_uri(char **proto, char **host, char **port,
88 char **server_path, char **repo_name, const char *uri)
90 const struct got_error *err = NULL;
93 *proto = *host = *port = *server_path = *repo_name = NULL;
95 p = strstr(uri, "://");
97 /* Try parsing Git's "scp" style URL syntax. */
98 *proto = strdup("ssh");
100 err = got_error_from_errno("strdup");
106 err = got_error(GOT_ERR_PARSE_URI);
109 /* No slashes allowed before first colon. */
112 err = got_error(GOT_ERR_PARSE_URI);
115 *host = strndup(s, q - s);
117 err = got_error_from_errno("strndup");
120 if ((*host)[0] == '\0') {
121 err = got_error(GOT_ERR_PARSE_URI);
126 *proto = strndup(uri, p - uri);
127 if (*proto == NULL) {
128 err = got_error_from_errno("strndup");
134 if (p == NULL || strlen(p) == 1) {
135 err = got_error(GOT_ERR_PARSE_URI);
139 q = memchr(s, ':', p - s);
141 *host = strndup(s, q - s);
143 err = got_error_from_errno("strndup");
146 if ((*host)[0] == '\0') {
147 err = got_error(GOT_ERR_PARSE_URI);
150 *port = strndup(q + 1, p - (q + 1));
152 err = got_error_from_errno("strndup");
155 if ((*port)[0] == '\0') {
156 err = got_error(GOT_ERR_PARSE_URI);
160 *host = strndup(s, p - s);
162 err = got_error_from_errno("strndup");
165 if ((*host)[0] == '\0') {
166 err = got_error(GOT_ERR_PARSE_URI);
172 while (p[0] == '/' && p[1] == '/')
174 *server_path = strdup(p);
175 if (*server_path == NULL) {
176 err = got_error_from_errno("strdup");
179 got_path_strip_trailing_slashes(*server_path);
180 if ((*server_path)[0] == '\0') {
181 err = got_error(GOT_ERR_PARSE_URI);
185 err = got_path_basename(repo_name, *server_path);
188 if (hassuffix(*repo_name, ".git"))
189 (*repo_name)[strlen(*repo_name) - 4] = '\0';
190 if ((*repo_name)[0] == '\0')
191 err = got_error(GOT_ERR_PARSE_URI);
209 * Escape a given path for the shell which will be started by sshd.
210 * In particular, git-shell is known to require single-quote characters
211 * around its repository path argument and will refuse to run otherwise.
213 static const struct got_error *
214 escape_path(char *buf, size_t bufsize, const char *path)
225 while (*p != '\0' && (q - buf < bufsize)) {
226 /* git escapes ! too */
227 if (*p != '\'' && *p != '!') {
232 if (q - buf + 4 >= bufsize)
240 if (*p == '\0' && (q - buf + 1 < bufsize)) {
246 return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
249 const struct got_error *
250 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
251 const char *port, const char *path, const char *direction, int verbosity)
253 const struct got_error *error = NULL;
256 char escaped_path[PATH_MAX];
257 const char *argv[11];
263 error = escape_path(escaped_path, sizeof(escaped_path), path);
267 argv[i++] = GOT_DIAL_PATH_SSH;
270 argv[i++] = (char *)port;
272 if (verbosity == -1) {
275 /* ssh(1) allows up to 3 "-v" options. */
276 for (j = 0; j < MIN(3, verbosity); j++)
280 argv[i++] = (char *)host;
281 argv[i++] = (char *)cmd;
282 argv[i++] = (char *)escaped_path;
284 assert(i <= nitems(argv));
286 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
287 return got_error_from_errno("socketpair");
291 error = got_error_from_errno("fork");
295 } else if (pid == 0) {
297 if (close(pfd[1]) == -1)
299 if (dup2(pfd[0], 0) == -1)
301 if (dup2(pfd[0], 1) == -1)
303 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
304 if (n < 0 || n >= ssizeof(cmd))
306 if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
308 abort(); /* not reached */
310 if (close(pfd[0]) == -1)
311 return got_error_from_errno("close");
318 const struct got_error *
319 got_dial_git(int *newfd, const char *host, const char *port,
320 const char *path, const char *direction)
322 const struct got_error *err = NULL;
323 struct addrinfo hints, *servinfo, *p;
325 int fd = -1, len, r, eaicode;
330 port = GOT_DEFAULT_GIT_PORT_STR;
332 memset(&hints, 0, sizeof hints);
333 hints.ai_family = AF_UNSPEC;
334 hints.ai_socktype = SOCK_STREAM;
335 eaicode = getaddrinfo(host, port, &hints, &servinfo);
338 snprintf(msg, sizeof(msg), "%s: %s", host,
339 gai_strerror(eaicode));
340 return got_error_msg(GOT_ERR_ADDRINFO, msg);
343 for (p = servinfo; p != NULL; p = p->ai_next) {
344 if ((fd = socket(p->ai_family, p->ai_socktype,
345 p->ai_protocol)) == -1)
347 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
351 err = got_error_from_errno("connect");
354 freeaddrinfo(servinfo);
358 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
359 err = got_error_from_errno("asprintf");
362 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
363 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
365 err = got_error_from_errno("dprintf");