Commit e3107e0e by Vicent Martí

Merge pull request #1558 from bmorganpa/ssh_transport

SSH Transport
parents c2d282cf 84ac625d
...@@ -24,8 +24,8 @@ msvc/Release/ ...@@ -24,8 +24,8 @@ msvc/Release/
*.sdf *.sdf
*.opensdf *.opensdf
*.aps *.aps
CMake*
*.cmake *.cmake
!cmake/Modules/*.cmake
.DS_Store .DS_Store
*~ *~
tags tags
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
PROJECT(libgit2 C) PROJECT(libgit2 C)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
# Build options # Build options
# #
OPTION( SONAME "Set the (SO)VERSION of the target" ON ) OPTION( SONAME "Set the (SO)VERSION of the target" ON )
...@@ -141,6 +143,15 @@ ELSEIF (NOT ZLIB_LIBRARY) ...@@ -141,6 +143,15 @@ ELSEIF (NOT ZLIB_LIBRARY)
FILE(GLOB SRC_ZLIB deps/zlib/*.c) FILE(GLOB SRC_ZLIB deps/zlib/*.c)
ENDIF() ENDIF()
IF(NOT LIBSSH2_LIBRARY)
FIND_PACKAGE(LIBSSH2 QUIET)
ENDIF()
IF (LIBSSH2_FOUND)
ADD_DEFINITIONS(-DGIT_SSH)
INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIR})
SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
ENDIF()
# Platform specific compilation flags # Platform specific compilation flags
IF (MSVC) IF (MSVC)
...@@ -292,6 +303,7 @@ ENDIF() ...@@ -292,6 +303,7 @@ ENDIF()
# Compile and link libgit2 # Compile and link libgit2
ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) ADD_LIBRARY(git2 ${SRC_GIT2} ${SRC_OS} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC})
TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES}) TARGET_LINK_LIBRARIES(git2 ${SSL_LIBRARIES})
TARGET_LINK_LIBRARIES(git2 ${SSH_LIBRARIES})
TARGET_OS_LIBRARIES(git2) TARGET_OS_LIBRARIES(git2)
# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240) # Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
...@@ -355,6 +367,7 @@ IF (BUILD_CLAR) ...@@ -355,6 +367,7 @@ IF (BUILD_CLAR)
ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1}) ADD_EXECUTABLE(libgit2_clar ${SRC_GIT2} ${SRC_OS} ${SRC_CLAR} ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1})
TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES}) TARGET_LINK_LIBRARIES(libgit2_clar ${SSL_LIBRARIES})
TARGET_LINK_LIBRARIES(libgit2_clar ${SSH_LIBRARIES})
TARGET_OS_LIBRARIES(libgit2_clar) TARGET_OS_LIBRARIES(libgit2_clar)
MSVC_SPLIT_SOURCES(libgit2_clar) MSVC_SPLIT_SOURCES(libgit2_clar)
......
if (LIBSSH2_LIBRARIES AND LIBSSH2_INCLUDE_DIRS)
set(LIBSSH2_FOUND TRUE)
else (LIBSSH2_LIBRARIES AND LIBSSH2_INCLUDE_DIRS)
find_path(LIBSSH2_INCLUDE_DIR
NAMES
libssh2.h
PATHS
/usr/include
/usr/local/include
/opt/local/include
/sw/include
${CMAKE_INCLUDE_PATH}
${CMAKE_INSTALL_PREFIX}/include
)
find_library(LIBSSH2_LIBRARY
NAMES
ssh2
libssh2
PATHS
/usr/lib
/usr/local/lib
/opt/local/lib
/sw/lib
${CMAKE_LIBRARY_PATH}
${CMAKE_INSTALL_PREFIX}/lib
)
if (LIBSSH2_INCLUDE_DIR AND LIBSSH2_LIBRARY)
set(LIBSSH2_FOUND TRUE)
endif (LIBSSH2_INCLUDE_DIR AND LIBSSH2_LIBRARY)
if (LIBSSH2_FOUND)
set(LIBSSH2_INCLUDE_DIRS
${LIBSSH2_INCLUDE_DIR}
)
set(LIBSSH2_LIBRARIES
${LIBSSH2_LIBRARIES}
${LIBSSH2_LIBRARY}
)
endif (LIBSSH2_FOUND)
endif (LIBSSH2_LIBRARIES AND LIBSSH2_INCLUDE_DIRS)
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
#include "net.h" #include "net.h"
#include "types.h" #include "types.h"
#ifdef GIT_SSH
#include <libssh2.h>
#endif
/** /**
* @file git2/transport.h * @file git2/transport.h
* @brief Git transport interfaces and functions * @brief Git transport interfaces and functions
...@@ -27,6 +31,8 @@ GIT_BEGIN_DECL ...@@ -27,6 +31,8 @@ GIT_BEGIN_DECL
typedef enum { typedef enum {
/* git_cred_userpass_plaintext */ /* git_cred_userpass_plaintext */
GIT_CREDTYPE_USERPASS_PLAINTEXT = 1, GIT_CREDTYPE_USERPASS_PLAINTEXT = 1,
GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE = 2,
GIT_CREDTYPE_SSH_PUBLICKEY = 3,
} git_credtype_t; } git_credtype_t;
/* The base structure for all credential types */ /* The base structure for all credential types */
...@@ -43,6 +49,27 @@ typedef struct git_cred_userpass_plaintext { ...@@ -43,6 +49,27 @@ typedef struct git_cred_userpass_plaintext {
char *password; char *password;
} git_cred_userpass_plaintext; } git_cred_userpass_plaintext;
#ifdef GIT_SSH
typedef LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*git_cred_sign_callback));
/* A ssh key file and passphrase */
typedef struct git_cred_ssh_keyfile_passphrase {
git_cred parent;
char *publickey;
char *privatekey;
char *passphrase;
} git_cred_ssh_keyfile_passphrase;
/* A ssh public key and authentication callback */
typedef struct git_cred_ssh_publickey {
git_cred parent;
char *publickey;
size_t publickey_len;
void *sign_callback;
void *sign_data;
} git_cred_ssh_publickey;
#endif
/** /**
* Creates a new plain-text username and password credential object. * Creates a new plain-text username and password credential object.
* The supplied credential parameter will be internally duplicated. * The supplied credential parameter will be internally duplicated.
...@@ -57,6 +84,42 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new( ...@@ -57,6 +84,42 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new(
const char *username, const char *username,
const char *password); const char *password);
#ifdef GIT_SSH
/**
* Creates a new ssh key file and passphrase credential object.
* The supplied credential parameter will be internally duplicated.
*
* @param out The newly created credential object.
* @param publickey The path to the public key of the credential.
* @param privatekey The path to the private key of the credential.
* @param passphrase The passphrase of the credential.
* @return 0 for success or an error code for failure
*/
GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new(
git_cred **out,
const char *publickey,
const char *privatekey,
const char *passphrase);
/**
* Creates a new ssh public key credential object.
* The supplied credential parameter will be internally duplicated.
*
* @param out The newly created credential object.
* @param publickey The bytes of the public key.
* @param publickey_len The length of the public key in bytes.
* @param sign_callback The callback method for authenticating.
* @param sign_data The abstract data sent to the sign_callback method.
* @return 0 for success or an error code for failure
*/
GIT_EXTERN(int) git_cred_ssh_publickey_new(
git_cred **out,
const char *publickey,
size_t publickey_len,
git_cred_sign_callback,
void *sign_data);
#endif
/** /**
* Signature of a function which acquires a credential object. * Signature of a function which acquires a credential object.
* *
...@@ -319,6 +382,17 @@ GIT_EXTERN(int) git_smart_subtransport_git( ...@@ -319,6 +382,17 @@ GIT_EXTERN(int) git_smart_subtransport_git(
git_smart_subtransport **out, git_smart_subtransport **out,
git_transport* owner); git_transport* owner);
/**
* Create an instance of the ssh subtransport.
*
* @param out The newly created subtransport
* @param owner The smart transport to own this subtransport
* @return 0 or an error code
*/
GIT_EXTERN(int) git_smart_subtransport_ssh(
git_smart_subtransport **out,
git_transport* owner);
/* /*
*** End interface for subtransports for the smart transport *** *** End interface for subtransports for the smart transport ***
*/ */
......
...@@ -18,19 +18,27 @@ typedef struct transport_definition { ...@@ -18,19 +18,27 @@ typedef struct transport_definition {
void *param; void *param;
} transport_definition; } transport_definition;
static transport_definition local_transport_definition = { "file://", 1, git_transport_local, NULL };
static transport_definition dummy_transport_definition = { NULL, 1, git_transport_dummy, NULL };
static git_smart_subtransport_definition http_subtransport_definition = { git_smart_subtransport_http, 1 }; static git_smart_subtransport_definition http_subtransport_definition = { git_smart_subtransport_http, 1 };
static git_smart_subtransport_definition git_subtransport_definition = { git_smart_subtransport_git, 0 }; static git_smart_subtransport_definition git_subtransport_definition = { git_smart_subtransport_git, 0 };
#ifdef GIT_SSH
static git_smart_subtransport_definition ssh_subtransport_definition = { git_smart_subtransport_ssh, 0 };
#endif
static transport_definition local_transport_definition = { "file://", 1, git_transport_local, NULL };
#ifdef GIT_SSH
static transport_definition ssh_transport_definition = { "ssh://", 1, git_transport_smart, &ssh_subtransport_definition };
#else
static transport_definition dummy_transport_definition = { NULL, 1, git_transport_dummy, NULL };
#endif
static transport_definition transports[] = { static transport_definition transports[] = {
{"git://", 1, git_transport_smart, &git_subtransport_definition}, {"git://", 1, git_transport_smart, &git_subtransport_definition},
{"http://", 1, git_transport_smart, &http_subtransport_definition}, {"http://", 1, git_transport_smart, &http_subtransport_definition},
{"https://", 1, git_transport_smart, &http_subtransport_definition}, {"https://", 1, git_transport_smart, &http_subtransport_definition},
{"file://", 1, git_transport_local, NULL}, {"file://", 1, git_transport_local, NULL},
{"git+ssh://", 1, git_transport_dummy, NULL}, #ifdef GIT_SSH
{"ssh+git://", 1, git_transport_dummy, NULL}, {"ssh://", 1, git_transport_smart, &ssh_subtransport_definition},
#endif
{NULL, 0, 0} {NULL, 0, 0}
}; };
...@@ -73,7 +81,11 @@ static int transport_find_fn(const char *url, git_transport_cb *callback, void * ...@@ -73,7 +81,11 @@ static int transport_find_fn(const char *url, git_transport_cb *callback, void *
/* It could be a SSH remote path. Check to see if there's a : /* It could be a SSH remote path. Check to see if there's a :
* SSH is an unsupported transport mechanism in this version of libgit2 */ * SSH is an unsupported transport mechanism in this version of libgit2 */
if (!definition && strrchr(url, ':')) if (!definition && strrchr(url, ':'))
#ifdef GIT_SSH
definition = &ssh_transport_definition;
#else
definition = &dummy_transport_definition; definition = &dummy_transport_definition;
#endif
/* Check to see if the path points to a file on the local file system */ /* Check to see if the path points to a file on the local file system */
if (!definition && git_path_exists(url) && git_path_isdir(url)) if (!definition && git_path_exists(url) && git_path_isdir(url))
......
...@@ -58,3 +58,105 @@ int git_cred_userpass_plaintext_new( ...@@ -58,3 +58,105 @@ int git_cred_userpass_plaintext_new(
*cred = &c->parent; *cred = &c->parent;
return 0; return 0;
} }
#ifdef GIT_SSH
static void ssh_keyfile_passphrase_free(struct git_cred *cred)
{
git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred;
size_t pass_len = strlen(c->passphrase);
if (c->publickey) {
git__free(c->publickey);
}
git__free(c->privatekey);
if (c->passphrase) {
/* Zero the memory which previously held the passphrase */
memset(c->passphrase, 0x0, pass_len);
git__free(c->passphrase);
}
memset(c, 0, sizeof(*c));
git__free(c);
}
int git_cred_ssh_keyfile_passphrase_new(
git_cred **cred,
const char *publickey,
const char *privatekey,
const char *passphrase)
{
git_cred_ssh_keyfile_passphrase *c;
assert(cred && privatekey);
c = git__calloc(1, sizeof(git_cred_ssh_keyfile_passphrase));
GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE;
c->parent.free = ssh_keyfile_passphrase_free;
c->privatekey = git__strdup(privatekey);
GITERR_CHECK_ALLOC(c->privatekey);
if (publickey) {
c->publickey = git__strdup(publickey);
GITERR_CHECK_ALLOC(c->publickey);
}
if (passphrase) {
c->passphrase = git__strdup(passphrase);
GITERR_CHECK_ALLOC(c->passphrase);
}
*cred = &c->parent;
return 0;
}
static void ssh_publickey_free(struct git_cred *cred)
{
git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred;
git__free(c->publickey);
c->sign_callback = NULL;
c->sign_data = NULL;
memset(c, 0, sizeof(*c));
git__free(c);
}
int git_cred_ssh_publickey_new(
git_cred **cred,
const char *publickey,
size_t publickey_len,
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
void *sign_data)
{
git_cred_ssh_publickey *c;
if (!cred)
return -1;
c = git__malloc(sizeof(git_cred_ssh_publickey));
GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_PUBLICKEY;
c->parent.free = ssh_publickey_free;
c->publickey = git__malloc(publickey_len);
GITERR_CHECK_ALLOC(c->publickey);
memcpy(c->publickey, publickey, publickey_len);
c->publickey_len = publickey_len;
c->sign_callback = sign_callback;
c->sign_data = sign_data;
*cred = &c->parent;
return 0;
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment