Added username and password auth for ssh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 6e81c25..8a2f9e3 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -224,6 +224,43 @@ static int git_ssh_extract_url_parts(
return 0;
}
+static int _git_ssh_authenticate_session(
+ LIBSSH2_SESSION* session,
+ const char *user,
+ git_cred* cred
+)
+{
+ int rc;
+ do {
+ switch (cred->credtype) {
+ case GIT_CREDTYPE_USERPASS_PLAINTEXT: {
+ git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
+ rc = libssh2_userauth_password(
+ session,
+ c->username,
+ c->password
+ );
+ break;
+ }
+ case GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE: {
+ git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred;
+ rc = libssh2_userauth_publickey_fromfile(
+ session,
+ user,
+ c->publickey,
+ c->privatekey,
+ c->passphrase
+ );
+ break;
+ }
+ default:
+ rc = -1;
+ }
+ } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
+
+ return rc;
+}
+
static int _git_ssh_setup_conn(
ssh_subtransport *t,
const char *url,
@@ -244,7 +281,7 @@ static int _git_ssh_setup_conn(
if (!git__prefixcmp(url, prefix_ssh)) {
url = url + strlen(prefix_ssh);
if (gitno_extract_url_parts(&host, &port, &user, &pass, url, default_port) < 0)
- return -1;
+ goto on_error;
} else {
if (git_ssh_extract_url_parts(&host, &user, url) < 0)
goto on_error;
@@ -270,8 +307,6 @@ static int _git_ssh_setup_conn(
user = git__strdup(default_user);
}
- git_cred_ssh_keyfile_passphrase *cred = (git_cred_ssh_keyfile_passphrase *)t->cred;
-
LIBSSH2_SESSION* session = libssh2_session_init();
if (!session)
goto on_error;
@@ -288,20 +323,9 @@ static int _git_ssh_setup_conn(
libssh2_trace(session, 0x1FF);
libssh2_session_set_blocking(session, 1);
- do {
- rc = libssh2_userauth_publickey_fromfile_ex(
- session,
- user,
- strlen(user),
- cred->publickey,
- cred->privatekey,
- cred->passphrase
- );
- } while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
-
- if (0 != rc) {
- goto on_error;
- }
+ if (_git_ssh_authenticate_session(session, user, t->cred) < 0) {
+ goto on_error;
+ }
LIBSSH2_CHANNEL* channel = NULL;
do {