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>
38 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_compat.h"
44 #include "got_lib_dial.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #ifndef GOT_DIAL_PATH_SSH
63 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
67 #define GOT_DEFAULT_GIT_PORT 9418
68 #define GOT_DEFAULT_GIT_PORT_STR "9418"
70 const struct got_error *
71 got_dial_apply_unveil(const char *proto)
73 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
74 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
75 return got_error_from_errno2("unveil",
80 if (strstr(proto, "http") != NULL) {
81 if (unveil(GOT_PATH_PROG_FETCH_HTTP, "x") != 0) {
82 return got_error_from_errno2("unveil",
83 GOT_PATH_PROG_FETCH_HTTP);
91 hassuffix(const char *base, const char *suf)
97 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
102 const struct got_error *
103 got_dial_parse_uri(char **proto, char **host, char **port,
104 char **server_path, char **repo_name, const char *uri)
106 const struct got_error *err = NULL;
109 *proto = *host = *port = *server_path = *repo_name = NULL;
111 p = strstr(uri, "://");
113 /* Try parsing Git's "scp" style URL syntax. */
114 *proto = strdup("ssh");
115 if (*proto == NULL) {
116 err = got_error_from_errno("strdup");
122 err = got_error(GOT_ERR_PARSE_URI);
125 /* No slashes allowed before first colon. */
128 err = got_error(GOT_ERR_PARSE_URI);
131 *host = strndup(s, q - s);
133 err = got_error_from_errno("strndup");
136 if ((*host)[0] == '\0') {
137 err = got_error(GOT_ERR_PARSE_URI);
142 *proto = strndup(uri, p - uri);
143 if (*proto == NULL) {
144 err = got_error_from_errno("strndup");
150 if (p == NULL || strlen(p) == 1) {
151 err = got_error(GOT_ERR_PARSE_URI);
155 q = memchr(s, ':', p - s);
157 *host = strndup(s, q - s);
159 err = got_error_from_errno("strndup");
162 if ((*host)[0] == '\0') {
163 err = got_error(GOT_ERR_PARSE_URI);
166 *port = strndup(q + 1, p - (q + 1));
168 err = got_error_from_errno("strndup");
171 if ((*port)[0] == '\0') {
172 err = got_error(GOT_ERR_PARSE_URI);
176 *host = strndup(s, p - s);
178 err = got_error_from_errno("strndup");
181 if ((*host)[0] == '\0') {
182 err = got_error(GOT_ERR_PARSE_URI);
188 while (p[0] == '/' && p[1] == '/')
190 *server_path = strdup(p);
191 if (*server_path == NULL) {
192 err = got_error_from_errno("strdup");
195 got_path_strip_trailing_slashes(*server_path);
196 if ((*server_path)[0] == '\0') {
197 err = got_error(GOT_ERR_PARSE_URI);
201 err = got_path_basename(repo_name, *server_path);
204 if (hassuffix(*repo_name, ".git"))
205 (*repo_name)[strlen(*repo_name) - 4] = '\0';
206 if ((*repo_name)[0] == '\0')
207 err = got_error(GOT_ERR_PARSE_URI);
225 * Escape a given path for the shell which will be started by sshd.
226 * In particular, git-shell is known to require single-quote characters
227 * around its repository path argument and will refuse to run otherwise.
229 static const struct got_error *
230 escape_path(char *buf, size_t bufsize, const char *path)
241 while (*p != '\0' && (q - buf < bufsize)) {
242 /* git escapes ! too */
243 if (*p != '\'' && *p != '!') {
248 if (q - buf + 4 >= bufsize)
256 if (*p == '\0' && (q - buf + 1 < bufsize)) {
262 return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
265 const struct got_error *
266 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
267 const char *port, const char *path, const char *command, int verbosity)
269 const struct got_error *error = NULL;
272 char escaped_path[PATH_MAX];
273 const char *argv[11];
279 error = escape_path(escaped_path, sizeof(escaped_path), path);
283 argv[i++] = GOT_DIAL_PATH_SSH;
286 argv[i++] = (char *)port;
288 if (verbosity == -1) {
291 /* ssh(1) allows up to 3 "-v" options. */
292 for (j = 0; j < MIN(3, verbosity); j++)
296 argv[i++] = (char *)host;
297 argv[i++] = (char *)cmd;
298 argv[i++] = (char *)escaped_path;
300 assert(i <= nitems(argv));
302 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
303 return got_error_from_errno("socketpair");
307 error = got_error_from_errno("fork");
311 } else if (pid == 0) {
312 if (close(pfd[1]) == -1)
314 if (dup2(pfd[0], 0) == -1)
316 if (dup2(pfd[0], 1) == -1)
318 if (strlcpy(cmd, command, sizeof(cmd)) >= sizeof(cmd))
320 if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
321 err(1, "execv %s", GOT_DIAL_PATH_SSH);
322 abort(); /* not reached */
324 if (close(pfd[0]) == -1)
325 return got_error_from_errno("close");
332 const struct got_error *
333 got_dial_git(int *newfd, const char *host, const char *port,
334 const char *path, const char *command)
336 const struct got_error *err = NULL;
337 struct addrinfo hints, *servinfo, *p;
339 int fd = -1, len, r, eaicode;
344 port = GOT_DEFAULT_GIT_PORT_STR;
346 memset(&hints, 0, sizeof hints);
347 hints.ai_family = AF_UNSPEC;
348 hints.ai_socktype = SOCK_STREAM;
349 eaicode = getaddrinfo(host, port, &hints, &servinfo);
352 snprintf(msg, sizeof(msg), "%s: %s", host,
353 gai_strerror(eaicode));
354 return got_error_msg(GOT_ERR_ADDRINFO, msg);
357 for (p = servinfo; p != NULL; p = p->ai_next) {
358 if ((fd = socket(p->ai_family, p->ai_socktype,
359 p->ai_protocol)) == -1)
361 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
365 err = got_error_from_errno("connect");
368 freeaddrinfo(servinfo);
372 if (asprintf(&cmd, "%s %s", command, path) == -1) {
373 err = got_error_from_errno("asprintf");
376 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
377 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
379 err = got_error_from_errno("dprintf");
390 const struct got_error *
391 got_dial_http(pid_t *newpid, int *newfd, const char *host,
392 const char *port, const char *path, int verbosity, int tls)
394 const struct got_error *error = NULL;
403 port = tls ? "443" : "80";
405 argv[i++] = GOT_PATH_PROG_FETCH_HTTP;
408 else if (verbosity > 0)
411 argv[i++] = tls ? "https" : "http";
416 assert(i <= nitems(argv));
418 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
419 return got_error_from_errno("socketpair");
423 error = got_error_from_errno("fork");
427 } else if (pid == 0) {
428 if (close(pfd[1]) == -1)
430 if (dup2(pfd[0], 0) == -1)
432 if (dup2(pfd[0], 1) == -1)
434 if (execv(GOT_PATH_PROG_FETCH_HTTP, (char *const *)argv) == -1)
436 abort(); /* not reached */
438 if (close(pfd[0]) == -1)
439 return got_error_from_errno("close");
446 const struct got_error *
447 got_dial_parse_command(char **command, char **repo_path, const char *gitcmd)
449 const struct got_error *err = NULL;
450 size_t len, cmdlen, pathlen;
451 char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
457 len = strlen(gitcmd);
459 if (len >= strlen(GOT_DIAL_CMD_SEND) &&
460 strncmp(gitcmd, GOT_DIAL_CMD_SEND,
461 strlen(GOT_DIAL_CMD_SEND)) == 0)
462 cmdlen = strlen(GOT_DIAL_CMD_SEND);
463 else if (len >= strlen(GOT_DIAL_CMD_FETCH) &&
464 strncmp(gitcmd, GOT_DIAL_CMD_FETCH,
465 strlen(GOT_DIAL_CMD_FETCH)) == 0)
466 cmdlen = strlen(GOT_DIAL_CMD_FETCH);
468 return got_error(GOT_ERR_BAD_PACKET);
470 if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
471 return got_error(GOT_ERR_BAD_PACKET);
473 if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
474 return got_error(GOT_ERR_BAD_PATH);
476 /* Forbid linefeeds in paths, like Git does. */
477 if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
478 return got_error(GOT_ERR_BAD_PATH);
480 path0 = strdup(&gitcmd[cmdlen + 1]);
482 return got_error_from_errno("strdup");
484 pathlen = strlen(path);
487 * Git clients send a shell command.
488 * Trim spaces and quotes around the path.
490 while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
494 while (pathlen > 0 &&
495 (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
496 path[pathlen - 1] == ' ')) {
497 path[pathlen - 1] = '\0';
501 /* Deny an empty repository path. */
502 if (path[0] == '\0' || got_path_is_root_dir(path)) {
503 err = got_error(GOT_ERR_NOT_GIT_REPO);
507 if (asprintf(&abspath, "/%s", path) == -1) {
508 err = got_error_from_errno("asprintf");
511 pathlen = strlen(abspath);
512 canonpath = malloc(pathlen + 1);
513 if (canonpath == NULL) {
514 err = got_error_from_errno("malloc");
517 err = got_canonpath(abspath, canonpath, pathlen + 1);
522 while (relpath[0] == '/')
524 *repo_path = strdup(relpath);
525 if (*repo_path == NULL) {
526 err = got_error_from_errno("strdup");
529 *command = strndup(gitcmd, cmdlen);
530 if (*command == NULL)
531 err = got_error_from_errno("strndup");