Blame src/transports/ssh.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_SSH
Packit Service 20376f
#include <libssh2.h>
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#include "global.h"
Packit Service 20376f
#include "git2.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "netops.h"
Packit Service 20376f
#include "smart.h"
Packit Service 20376f
#include "cred.h"
Packit Service 20376f
#include "socket_stream.h"
Packit Service 20376f
#include "ssh.h"
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_SSH
Packit Service 20376f
Packit Service 20376f
#define OWNING_SUBTRANSPORT(s) ((ssh_subtransport *)(s)->parent.subtransport)
Packit Service 20376f
Packit Service 20376f
static const char *ssh_prefixes[] = { "ssh://", "ssh+git://", "git+ssh://" };
Packit Service 20376f
Packit Service 20376f
static const char cmd_uploadpack[] = "git-upload-pack";
Packit Service 20376f
static const char cmd_receivepack[] = "git-receive-pack";
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_smart_subtransport_stream parent;
Packit Service 20376f
	git_stream *io;
Packit Service 20376f
	LIBSSH2_SESSION *session;
Packit Service 20376f
	LIBSSH2_CHANNEL *channel;
Packit Service 20376f
	const char *cmd;
Packit Service 20376f
	char *url;
Packit Service 20376f
	unsigned sent_command : 1;
Packit Service 20376f
} ssh_stream;
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_smart_subtransport parent;
Packit Service 20376f
	transport_smart *owner;
Packit Service 20376f
	ssh_stream *current_stream;
Packit Service 20376f
	git_cred *cred;
Packit Service 20376f
	char *cmd_uploadpack;
Packit Service 20376f
	char *cmd_receivepack;
Packit Service 20376f
} ssh_subtransport;
Packit Service 20376f
Packit Service 20376f
static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username);
Packit Service 20376f
Packit Service 20376f
static void ssh_error(LIBSSH2_SESSION *session, const char *errmsg)
Packit Service 20376f
{
Packit Service 20376f
	char *ssherr;
Packit Service 20376f
	libssh2_session_last_error(session, &ssherr, NULL, 0);
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_SSH, "%s: %s", errmsg, ssherr);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Create a git protocol request.
Packit Service 20376f
 *
Packit Service 20376f
 * For example: git-upload-pack '/libgit2/libgit2'
Packit Service 20376f
 */
Packit Service 20376f
static int gen_proto(git_buf *request, const char *cmd, const char *url)
Packit Service 20376f
{
Packit Service 20376f
	char *repo;
Packit Service 20376f
	int len;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
Packit Service 20376f
		const char *p = ssh_prefixes[i];
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixcmp(url, p)) {
Packit Service 20376f
			url = url + strlen(p);
Packit Service 20376f
			repo = strchr(url, '/');
Packit Service 20376f
			if (repo && repo[1] == '~')
Packit Service 20376f
				++repo;
Packit Service 20376f
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	repo = strchr(url, ':');
Packit Service 20376f
	if (repo) repo++;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (!repo) {
Packit Service 20376f
		giterr_set(GITERR_NET, "malformed git protocol URL");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	len = strlen(cmd) + 1 /* Space */ + 1 /* Quote */ + strlen(repo) + 1 /* Quote */ + 1;
Packit Service 20376f
Packit Service 20376f
	git_buf_grow(request, len);
Packit Service 20376f
	git_buf_printf(request, "%s '%s'", cmd, repo);
Packit Service 20376f
	git_buf_putc(request, '\0');
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(request))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int send_command(ssh_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf request = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	error = gen_proto(&request, s->cmd, s->url);
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
Packit Service 20376f
	error = libssh2_channel_exec(s->channel, request.ptr);
Packit Service 20376f
	if (error < LIBSSH2_ERROR_NONE) {
Packit Service 20376f
		ssh_error(s->session, "SSH could not execute request");
Packit Service 20376f
		goto cleanup;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	s->sent_command = 1;
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_buf_free(&request);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_stream_read(
Packit Service 20376f
	git_smart_subtransport_stream *stream,
Packit Service 20376f
	char *buffer,
Packit Service 20376f
	size_t buf_size,
Packit Service 20376f
	size_t *bytes_read)
Packit Service 20376f
{
Packit Service 20376f
	int rc;
Packit Service 20376f
	ssh_stream *s = (ssh_stream *)stream;
Packit Service 20376f
Packit Service 20376f
	*bytes_read = 0;
Packit Service 20376f
Packit Service 20376f
	if (!s->sent_command && send_command(s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((rc = libssh2_channel_read(s->channel, buffer, buf_size)) < LIBSSH2_ERROR_NONE) {
Packit Service 20376f
		ssh_error(s->session, "SSH could not read data");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * If we can't get anything out of stdout, it's typically a
Packit Service 20376f
	 * not-found error, so read from stderr and signal EOF on
Packit Service 20376f
	 * stderr.
Packit Service 20376f
	 */
Packit Service 20376f
	if (rc == 0) {
Packit Service 20376f
		if ((rc = libssh2_channel_read_stderr(s->channel, buffer, buf_size)) > 0) {
Packit Service 20376f
			giterr_set(GITERR_SSH, "%*s", rc, buffer);
Packit Service 20376f
			return GIT_EEOF;
Packit Service 20376f
		} else if (rc < LIBSSH2_ERROR_NONE) {
Packit Service 20376f
			ssh_error(s->session, "SSH could not read stderr");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	*bytes_read = rc;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_stream_write(
Packit Service 20376f
	git_smart_subtransport_stream *stream,
Packit Service 20376f
	const char *buffer,
Packit Service 20376f
	size_t len)
Packit Service 20376f
{
Packit Service 20376f
	ssh_stream *s = (ssh_stream *)stream;
Packit Service 20376f
	size_t off = 0;
Packit Service 20376f
	ssize_t ret = 0;
Packit Service 20376f
Packit Service 20376f
	if (!s->sent_command && send_command(s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		ret = libssh2_channel_write(s->channel, buffer + off, len - off);
Packit Service 20376f
		if (ret < 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		off += ret;
Packit Service 20376f
Packit Service 20376f
	} while (off < len);
Packit Service 20376f
Packit Service 20376f
	if (ret < 0) {
Packit Service 20376f
		ssh_error(s->session, "SSH could not write data");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void ssh_stream_free(git_smart_subtransport_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	ssh_stream *s = (ssh_stream *)stream;
Packit Service 20376f
	ssh_subtransport *t;
Packit Service 20376f
Packit Service 20376f
	if (!stream)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	t = OWNING_SUBTRANSPORT(s);
Packit Service 20376f
	t->current_stream = NULL;
Packit Service 20376f
Packit Service 20376f
	if (s->channel) {
Packit Service 20376f
		libssh2_channel_close(s->channel);
Packit Service 20376f
		libssh2_channel_free(s->channel);
Packit Service 20376f
		s->channel = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (s->session) {
Packit Service 20376f
		libssh2_session_free(s->session);
Packit Service 20376f
		s->session = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (s->io) {
Packit Service 20376f
		git_stream_close(s->io);
Packit Service 20376f
		git_stream_free(s->io);
Packit Service 20376f
		s->io = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(s->url);
Packit Service 20376f
	git__free(s);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_stream_alloc(
Packit Service 20376f
	ssh_subtransport *t,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	const char *cmd,
Packit Service 20376f
	git_smart_subtransport_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	ssh_stream *s;
Packit Service 20376f
Packit Service 20376f
	assert(stream);
Packit Service 20376f
Packit Service 20376f
	s = git__calloc(sizeof(ssh_stream), 1);
Packit Service 20376f
	GITERR_CHECK_ALLOC(s);
Packit Service 20376f
Packit Service 20376f
	s->parent.subtransport = &t->parent;
Packit Service 20376f
	s->parent.read = ssh_stream_read;
Packit Service 20376f
	s->parent.write = ssh_stream_write;
Packit Service 20376f
	s->parent.free = ssh_stream_free;
Packit Service 20376f
Packit Service 20376f
	s->cmd = cmd;
Packit Service 20376f
Packit Service 20376f
	s->url = git__strdup(url);
Packit Service 20376f
	if (!s->url) {
Packit Service 20376f
		git__free(s);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*stream = &s->parent;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int git_ssh_extract_url_parts(
Packit Service 20376f
	char **host,
Packit Service 20376f
	char **username,
Packit Service 20376f
	const char *url)
Packit Service 20376f
{
Packit Service 20376f
	char *colon, *at;
Packit Service 20376f
	const char *start;
Packit Service 20376f
Packit Service 20376f
	colon = strchr(url, ':');
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	at = strchr(url, '@');
Packit Service 20376f
	if (at) {
Packit Service 20376f
		start = at + 1;
Packit Service 20376f
		*username = git__substrdup(url, at - url);
Packit Service 20376f
		GITERR_CHECK_ALLOC(*username);
Packit Service 20376f
	} else {
Packit Service 20376f
		start = url;
Packit Service 20376f
		*username = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (colon == NULL || (colon < start)) {
Packit Service 20376f
		giterr_set(GITERR_NET, "malformed URL");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*host = git__substrdup(start, colon - start);
Packit Service 20376f
	GITERR_CHECK_ALLOC(*host);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_agent_auth(LIBSSH2_SESSION *session, git_cred_ssh_key *c) {
Packit Service 20376f
	int rc = LIBSSH2_ERROR_NONE;
Packit Service 20376f
Packit Service 20376f
	struct libssh2_agent_publickey *curr, *prev = NULL;
Packit Service 20376f
Packit Service 20376f
	LIBSSH2_AGENT *agent = libssh2_agent_init(session);
Packit Service 20376f
Packit Service 20376f
	if (agent == NULL)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	rc = libssh2_agent_connect(agent);
Packit Service 20376f
Packit Service 20376f
	if (rc != LIBSSH2_ERROR_NONE)
Packit Service 20376f
		goto shutdown;
Packit Service 20376f
Packit Service 20376f
	rc = libssh2_agent_list_identities(agent);
Packit Service 20376f
Packit Service 20376f
	if (rc != LIBSSH2_ERROR_NONE)
Packit Service 20376f
		goto shutdown;
Packit Service 20376f
Packit Service 20376f
	while (1) {
Packit Service 20376f
		rc = libssh2_agent_get_identity(agent, &curr, prev);
Packit Service 20376f
Packit Service 20376f
		if (rc < 0)
Packit Service 20376f
			goto shutdown;
Packit Service 20376f
Packit Service 20376f
		/* rc is set to 1 whenever the ssh agent ran out of keys to check.
Packit Service 20376f
		 * Set the error code to authentication failure rather than erroring
Packit Service 20376f
		 * out with an untranslatable error code.
Packit Service 20376f
		 */
Packit Service 20376f
		if (rc == 1) {
Packit Service 20376f
			rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
Packit Service 20376f
			goto shutdown;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		rc = libssh2_agent_userauth(agent, c->username, curr);
Packit Service 20376f
Packit Service 20376f
		if (rc == 0)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		prev = curr;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
shutdown:
Packit Service 20376f
Packit Service 20376f
	if (rc != LIBSSH2_ERROR_NONE)
Packit Service 20376f
		ssh_error(session, "error authenticating");
Packit Service 20376f
Packit Service 20376f
	libssh2_agent_disconnect(agent);
Packit Service 20376f
	libssh2_agent_free(agent);
Packit Service 20376f
Packit Service 20376f
	return rc;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _git_ssh_authenticate_session(
Packit Service 20376f
	LIBSSH2_SESSION* session,
Packit Service 20376f
	git_cred* cred)
Packit Service 20376f
{
Packit Service 20376f
	int rc;
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		switch (cred->credtype) {
Packit Service 20376f
		case GIT_CREDTYPE_USERPASS_PLAINTEXT: {
Packit Service 20376f
			git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
Packit Service 20376f
			rc = libssh2_userauth_password(session, c->username, c->password);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		case GIT_CREDTYPE_SSH_KEY: {
Packit Service 20376f
			git_cred_ssh_key *c = (git_cred_ssh_key *)cred;
Packit Service 20376f
Packit Service 20376f
			if (c->privatekey)
Packit Service 20376f
				rc = libssh2_userauth_publickey_fromfile(
Packit Service 20376f
					session, c->username, c->publickey,
Packit Service 20376f
					c->privatekey, c->passphrase);
Packit Service 20376f
			else
Packit Service 20376f
				rc = ssh_agent_auth(session, c);
Packit Service 20376f
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		case GIT_CREDTYPE_SSH_CUSTOM: {
Packit Service 20376f
			git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
Packit Service 20376f
Packit Service 20376f
			rc = libssh2_userauth_publickey(
Packit Service 20376f
				session, c->username, (const unsigned char *)c->publickey,
Packit Service 20376f
				c->publickey_len, c->sign_callback, &c->payload);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		case GIT_CREDTYPE_SSH_INTERACTIVE: {
Packit Service 20376f
			void **abstract = libssh2_session_abstract(session);
Packit Service 20376f
			git_cred_ssh_interactive *c = (git_cred_ssh_interactive *)cred;
Packit Service 20376f
Packit Service 20376f
			/* ideally, we should be able to set this by calling
Packit Service 20376f
			 * libssh2_session_init_ex() instead of libssh2_session_init().
Packit Service 20376f
			 * libssh2's API is inconsistent here i.e. libssh2_userauth_publickey()
Packit Service 20376f
			 * allows you to pass the `abstract` as part of the call, whereas
Packit Service 20376f
			 * libssh2_userauth_keyboard_interactive() does not!
Packit Service 20376f
			 *
Packit Service 20376f
			 * The only way to set the `abstract` pointer is by calling
Packit Service 20376f
			 * libssh2_session_abstract(), which will replace the existing
Packit Service 20376f
			 * pointer as is done below. This is safe for now (at time of writing),
Packit Service 20376f
			 * but may not be valid in future.
Packit Service 20376f
			 */
Packit Service 20376f
			*abstract = c->payload;
Packit Service 20376f
Packit Service 20376f
			rc = libssh2_userauth_keyboard_interactive(
Packit Service 20376f
				session, c->username, c->prompt_callback);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
#ifdef GIT_SSH_MEMORY_CREDENTIALS
Packit Service 20376f
		case GIT_CREDTYPE_SSH_MEMORY: {
Packit Service 20376f
			git_cred_ssh_key *c = (git_cred_ssh_key *)cred;
Packit Service 20376f
Packit Service 20376f
			assert(c->username);
Packit Service 20376f
			assert(c->privatekey);
Packit Service 20376f
Packit Service 20376f
			rc = libssh2_userauth_publickey_frommemory(
Packit Service 20376f
				session,
Packit Service 20376f
				c->username,
Packit Service 20376f
				strlen(c->username),
Packit Service 20376f
				c->publickey,
Packit Service 20376f
				c->publickey ? strlen(c->publickey) : 0,
Packit Service 20376f
				c->privatekey,
Packit Service 20376f
				strlen(c->privatekey),
Packit Service 20376f
				c->passphrase);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
#endif
Packit Service 20376f
		default:
Packit Service 20376f
			rc = LIBSSH2_ERROR_AUTHENTICATION_FAILED;
Packit Service 20376f
		}
Packit Service 20376f
	} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
Packit Service 20376f
Packit Service 20376f
        if (rc == LIBSSH2_ERROR_PASSWORD_EXPIRED || rc == LIBSSH2_ERROR_AUTHENTICATION_FAILED)
Packit Service 20376f
                return GIT_EAUTH;
Packit Service 20376f
Packit Service 20376f
	if (rc != LIBSSH2_ERROR_NONE) {
Packit Service 20376f
		if (!giterr_last())
Packit Service 20376f
			ssh_error(session, "Failed to authenticate SSH session");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int request_creds(git_cred **out, ssh_subtransport *t, const char *user, int auth_methods)
Packit Service 20376f
{
Packit Service 20376f
	int error, no_callback = 0;
Packit Service 20376f
	git_cred *cred = NULL;
Packit Service 20376f
Packit Service 20376f
	if (!t->owner->cred_acquire_cb) {
Packit Service 20376f
		no_callback = 1;
Packit Service 20376f
	} else {
Packit Service 20376f
		error = t->owner->cred_acquire_cb(&cred, t->owner->url, user, auth_methods,
Packit Service 20376f
						  t->owner->cred_acquire_payload);
Packit Service 20376f
Packit Service 20376f
		if (error == GIT_PASSTHROUGH)
Packit Service 20376f
			no_callback = 1;
Packit Service 20376f
		else if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
		else if (!cred) {
Packit Service 20376f
			giterr_set(GITERR_SSH, "callback failed to initialize SSH credentials");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (no_callback) {
Packit Service 20376f
		giterr_set(GITERR_SSH, "authentication required but no callback set");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!(cred->credtype & auth_methods)) {
Packit Service 20376f
		cred->free(cred);
Packit Service 20376f
		giterr_set(GITERR_SSH, "callback returned unsupported credentials type");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = cred;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _git_ssh_session_create(
Packit Service 20376f
	LIBSSH2_SESSION** session,
Packit Service 20376f
	git_stream *io)
Packit Service 20376f
{
Packit Service 20376f
	int rc = 0;
Packit Service 20376f
	LIBSSH2_SESSION* s;
Packit Service 20376f
	git_socket_stream *socket = (git_socket_stream *) io;
Packit Service 20376f
Packit Service 20376f
	assert(session);
Packit Service 20376f
Packit Service 20376f
	s = libssh2_session_init();
Packit Service 20376f
	if (!s) {
Packit Service 20376f
		giterr_set(GITERR_NET, "failed to initialize SSH session");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	do {
Packit Service 20376f
		rc = libssh2_session_startup(s, socket->s);
Packit Service 20376f
	} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
Packit Service 20376f
Packit Service 20376f
	if (rc != LIBSSH2_ERROR_NONE) {
Packit Service 20376f
		ssh_error(s, "failed to start SSH session");
Packit Service 20376f
		libssh2_session_free(s);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	libssh2_session_set_blocking(s, 1);
Packit Service 20376f
Packit Service 20376f
	*session = s;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _git_ssh_setup_conn(
Packit Service 20376f
	ssh_subtransport *t,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	const char *cmd,
Packit Service 20376f
	git_smart_subtransport_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
Packit Service 20376f
	const char *default_port="22";
Packit Service 20376f
	int auth_methods, error = 0;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	ssh_stream *s;
Packit Service 20376f
	git_cred *cred = NULL;
Packit Service 20376f
	LIBSSH2_SESSION* session=NULL;
Packit Service 20376f
	LIBSSH2_CHANNEL* channel=NULL;
Packit Service 20376f
Packit Service 20376f
	t->current_stream = NULL;
Packit Service 20376f
Packit Service 20376f
	*stream = NULL;
Packit Service 20376f
	if (ssh_stream_alloc(t, url, cmd, stream) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	s = (ssh_stream *)*stream;
Packit Service 20376f
	s->session = NULL;
Packit Service 20376f
	s->channel = NULL;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
Packit Service 20376f
		const char *p = ssh_prefixes[i];
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixcmp(url, p)) {
Packit Service 20376f
			if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, default_port)) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
Packit Service 20376f
			goto post_extract;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	if ((error = git_ssh_extract_url_parts(&host, &user, url)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
	port = git__strdup(default_port);
Packit Service 20376f
	GITERR_CHECK_ALLOC(port);
Packit Service 20376f
Packit Service 20376f
post_extract:
Packit Service 20376f
	if ((error = git_socket_stream_new(&s->io, host, port)) < 0 ||
Packit Service 20376f
	    (error = git_stream_connect(s->io)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if ((error = _git_ssh_session_create(&session, s->io)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (t->owner->certificate_check_cb != NULL) {
Packit Service 20376f
		git_cert_hostkey cert = {{ 0 }}, *cert_ptr;
Packit Service 20376f
		const char *key;
Packit Service 20376f
Packit Service 20376f
		cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2;
Packit Service 20376f
Packit Service 20376f
		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
Packit Service 20376f
		if (key != NULL) {
Packit Service 20376f
			cert.type |= GIT_CERT_SSH_SHA1;
Packit Service 20376f
			memcpy(&cert.hash_sha1, key, 20);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
Packit Service 20376f
		if (key != NULL) {
Packit Service 20376f
			cert.type |= GIT_CERT_SSH_MD5;
Packit Service 20376f
			memcpy(&cert.hash_md5, key, 16);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (cert.type == 0) {
Packit Service 20376f
			giterr_set(GITERR_SSH, "unable to get the host key");
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* We don't currently trust any hostkeys */
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
Packit Service 20376f
		cert_ptr = &cer;;
Packit Service 20376f
Packit Service 20376f
		error = t->owner->certificate_check_cb((git_cert *) cert_ptr, 0, host, t->owner->message_cb_payload);
Packit Service 20376f
		if (error < 0) {
Packit Service 20376f
			if (!giterr_last())
Packit Service 20376f
				giterr_set(GITERR_NET, "user cancelled hostkey check");
Packit Service 20376f
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* we need the username to ask for auth methods */
Packit Service 20376f
	if (!user) {
Packit Service 20376f
		if ((error = request_creds(&cred, t, NULL, GIT_CREDTYPE_USERNAME)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		user = git__strdup(((git_cred_username *) cred)->username);
Packit Service 20376f
		cred->free(cred);
Packit Service 20376f
		cred = NULL;
Packit Service 20376f
		if (!user)
Packit Service 20376f
			goto done;
Packit Service 20376f
	} else if (user && pass) {
Packit Service 20376f
		if ((error = git_cred_userpass_plaintext_new(&cred, user, pass)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = list_auth_methods(&auth_methods, session, user)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	error = GIT_EAUTH;
Packit Service 20376f
	/* if we already have something to try */
Packit Service 20376f
	if (cred && auth_methods & cred->credtype)
Packit Service 20376f
		error = _git_ssh_authenticate_session(session, cred);
Packit Service 20376f
Packit Service 20376f
	while (error == GIT_EAUTH) {
Packit Service 20376f
		if (cred) {
Packit Service 20376f
			cred->free(cred);
Packit Service 20376f
			cred = NULL;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = request_creds(&cred, t, user, auth_methods)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		if (strcmp(user, git_cred__username(cred))) {
Packit Service 20376f
			giterr_set(GITERR_SSH, "username does not match previous request");
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		error = _git_ssh_authenticate_session(session, cred);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	channel = libssh2_channel_open_session(session);
Packit Service 20376f
	if (!channel) {
Packit Service 20376f
		error = -1;
Packit Service 20376f
		ssh_error(session, "Failed to open SSH channel");
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	libssh2_channel_set_blocking(channel, 1);
Packit Service 20376f
Packit Service 20376f
	s->session = session;
Packit Service 20376f
	s->channel = channel;
Packit Service 20376f
Packit Service 20376f
	t->current_stream = s;
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (error < 0) {
Packit Service 20376f
		ssh_stream_free(*stream);
Packit Service 20376f
Packit Service 20376f
		if (session)
Packit Service 20376f
			libssh2_session_free(session);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (cred)
Packit Service 20376f
		cred->free(cred);
Packit Service 20376f
Packit Service 20376f
	git__free(host);
Packit Service 20376f
	git__free(port);
Packit Service 20376f
	git__free(path);
Packit Service 20376f
	git__free(user);
Packit Service 20376f
	git__free(pass);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_uploadpack_ls(
Packit Service 20376f
	ssh_subtransport *t,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	git_smart_subtransport_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	const char *cmd = t->cmd_uploadpack ? t->cmd_uploadpack : cmd_uploadpack;
Packit Service 20376f
Packit Service 20376f
	return _git_ssh_setup_conn(t, url, cmd, stream);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_uploadpack(
Packit Service 20376f
	ssh_subtransport *t,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	git_smart_subtransport_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(url);
Packit Service 20376f
Packit Service 20376f
	if (t->current_stream) {
Packit Service 20376f
		*stream = &t->current_stream->parent;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_NET, "must call UPLOADPACK_LS before UPLOADPACK");
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_receivepack_ls(
Packit Service 20376f
	ssh_subtransport *t,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	git_smart_subtransport_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	const char *cmd = t->cmd_receivepack ? t->cmd_receivepack : cmd_receivepack;
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	return _git_ssh_setup_conn(t, url, cmd, stream);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ssh_receivepack(
Packit Service 20376f
	ssh_subtransport *t,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	git_smart_subtransport_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(url);
Packit Service 20376f
Packit Service 20376f
	if (t->current_stream) {
Packit Service 20376f
		*stream = &t->current_stream->parent;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_NET, "must call RECEIVEPACK_LS before RECEIVEPACK");
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _ssh_action(
Packit Service 20376f
	git_smart_subtransport_stream **stream,
Packit Service 20376f
	git_smart_subtransport *subtransport,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	git_smart_service_t action)
Packit Service 20376f
{
Packit Service 20376f
	ssh_subtransport *t = (ssh_subtransport *) subtransport;
Packit Service 20376f
Packit Service 20376f
	switch (action) {
Packit Service 20376f
		case GIT_SERVICE_UPLOADPACK_LS:
Packit Service 20376f
			return ssh_uploadpack_ls(t, url, stream);
Packit Service 20376f
Packit Service 20376f
		case GIT_SERVICE_UPLOADPACK:
Packit Service 20376f
			return ssh_uploadpack(t, url, stream);
Packit Service 20376f
Packit Service 20376f
		case GIT_SERVICE_RECEIVEPACK_LS:
Packit Service 20376f
			return ssh_receivepack_ls(t, url, stream);
Packit Service 20376f
Packit Service 20376f
		case GIT_SERVICE_RECEIVEPACK:
Packit Service 20376f
			return ssh_receivepack(t, url, stream);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*stream = NULL;
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _ssh_close(git_smart_subtransport *subtransport)
Packit Service 20376f
{
Packit Service 20376f
	ssh_subtransport *t = (ssh_subtransport *) subtransport;
Packit Service 20376f
Packit Service 20376f
	assert(!t->current_stream);
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(t);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void _ssh_free(git_smart_subtransport *subtransport)
Packit Service 20376f
{
Packit Service 20376f
	ssh_subtransport *t = (ssh_subtransport *) subtransport;
Packit Service 20376f
Packit Service 20376f
	assert(!t->current_stream);
Packit Service 20376f
Packit Service 20376f
	git__free(t->cmd_uploadpack);
Packit Service 20376f
	git__free(t->cmd_receivepack);
Packit Service 20376f
	git__free(t);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define SSH_AUTH_PUBLICKEY "publickey"
Packit Service 20376f
#define SSH_AUTH_PASSWORD "password"
Packit Service 20376f
#define SSH_AUTH_KEYBOARD_INTERACTIVE "keyboard-interactive"
Packit Service 20376f
Packit Service 20376f
static int list_auth_methods(int *out, LIBSSH2_SESSION *session, const char *username)
Packit Service 20376f
{
Packit Service 20376f
	const char *list, *ptr;
Packit Service 20376f
Packit Service 20376f
	*out = 0;
Packit Service 20376f
Packit Service 20376f
	list = libssh2_userauth_list(session, username, strlen(username));
Packit Service 20376f
Packit Service 20376f
	/* either error, or the remote accepts NONE auth, which is bizarre, let's punt */
Packit Service 20376f
	if (list == NULL && !libssh2_userauth_authenticated(session)) {
Packit Service 20376f
		ssh_error(session, "Failed to retrieve list of SSH authentication methods");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	ptr = list;
Packit Service 20376f
	while (ptr) {
Packit Service 20376f
		if (*ptr == ',')
Packit Service 20376f
			ptr++;
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixcmp(ptr, SSH_AUTH_PUBLICKEY)) {
Packit Service 20376f
			*out |= GIT_CREDTYPE_SSH_KEY;
Packit Service 20376f
			*out |= GIT_CREDTYPE_SSH_CUSTOM;
Packit Service 20376f
#ifdef GIT_SSH_MEMORY_CREDENTIALS
Packit Service 20376f
			*out |= GIT_CREDTYPE_SSH_MEMORY;
Packit Service 20376f
#endif
Packit Service 20376f
			ptr += strlen(SSH_AUTH_PUBLICKEY);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixcmp(ptr, SSH_AUTH_PASSWORD)) {
Packit Service 20376f
			*out |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
Packit Service 20376f
			ptr += strlen(SSH_AUTH_PASSWORD);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixcmp(ptr, SSH_AUTH_KEYBOARD_INTERACTIVE)) {
Packit Service 20376f
			*out |= GIT_CREDTYPE_SSH_INTERACTIVE;
Packit Service 20376f
			ptr += strlen(SSH_AUTH_KEYBOARD_INTERACTIVE);
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Skipt it if we don't know it */
Packit Service 20376f
		ptr = strchr(ptr, ',');
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_smart_subtransport_ssh(
Packit Service 20376f
	git_smart_subtransport **out, git_transport *owner, void *param)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_SSH
Packit Service 20376f
	ssh_subtransport *t;
Packit Service 20376f
Packit Service 20376f
	assert(out);
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(param);
Packit Service 20376f
Packit Service 20376f
	t = git__calloc(sizeof(ssh_subtransport), 1);
Packit Service 20376f
	GITERR_CHECK_ALLOC(t);
Packit Service 20376f
Packit Service 20376f
	t->owner = (transport_smart *)owner;
Packit Service 20376f
	t->parent.action = _ssh_action;
Packit Service 20376f
	t->parent.close = _ssh_close;
Packit Service 20376f
	t->parent.free = _ssh_free;
Packit Service 20376f
Packit Service 20376f
	*out = (git_smart_subtransport *) t;
Packit Service 20376f
	return 0;
Packit Service 20376f
#else
Packit Service 20376f
	GIT_UNUSED(owner);
Packit Service 20376f
	GIT_UNUSED(param);
Packit Service 20376f
Packit Service 20376f
	assert(out);
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_INVALID, "cannot create SSH transport. Library was built without SSH support");
Packit Service 20376f
	return -1;
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *payload)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_SSH
Packit Service 20376f
	git_strarray *paths = (git_strarray *) payload;
Packit Service 20376f
	git_transport *transport;
Packit Service 20376f
	transport_smart *smart;
Packit Service 20376f
	ssh_subtransport *t;
Packit Service 20376f
	int error;
Packit Service 20376f
	git_smart_subtransport_definition ssh_definition = {
Packit Service 20376f
		git_smart_subtransport_ssh,
Packit Service 20376f
		0, /* no RPC */
Packit Service 20376f
		NULL,
Packit Service 20376f
	};
Packit Service 20376f
Packit Service 20376f
	if (paths->count != 2) {
Packit Service 20376f
		giterr_set(GITERR_SSH, "invalid ssh paths, must be two strings");
Packit Service 20376f
		return GIT_EINVALIDSPEC;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_transport_smart(&transport, owner, &ssh_definition)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	smart = (transport_smart *) transport;
Packit Service 20376f
	t = (ssh_subtransport *) smart->wrapped;
Packit Service 20376f
Packit Service 20376f
	t->cmd_uploadpack = git__strdup(paths->strings[0]);
Packit Service 20376f
	GITERR_CHECK_ALLOC(t->cmd_uploadpack);
Packit Service 20376f
	t->cmd_receivepack = git__strdup(paths->strings[1]);
Packit Service 20376f
	GITERR_CHECK_ALLOC(t->cmd_receivepack);
Packit Service 20376f
Packit Service 20376f
	*out = transport;
Packit Service 20376f
	return 0;
Packit Service 20376f
#else
Packit Service 20376f
	GIT_UNUSED(owner);
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
Packit Service 20376f
	assert(out);
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_INVALID, "cannot create SSH transport. Library was built without SSH support");
Packit Service 20376f
	return -1;
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_SSH
Packit Service 20376f
static void shutdown_ssh(void)
Packit Service 20376f
{
Packit Service 20376f
    libssh2_exit();
Packit Service 20376f
}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_transport_ssh_global_init(void)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_SSH
Packit Service 20376f
	if (libssh2_init(0) < 0) {
Packit Service 20376f
		giterr_set(GITERR_SSH, "unable to initialize libssh2");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__on_shutdown(shutdown_ssh);
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
	/* Nothing to initialize */
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
}