Commit 70a8c78f by Carlos Martín Nieto

Rename the ssh credentials

The names from libssh2 are somewhat obtuse for us. We can simplify the
usual key/passphrase credential's name, as well as make clearer what the
custom signature function is.
parent 1c74686e
...@@ -33,11 +33,11 @@ typedef enum { ...@@ -33,11 +33,11 @@ typedef enum {
/* git_cred_userpass_plaintext */ /* git_cred_userpass_plaintext */
GIT_CREDTYPE_USERPASS_PLAINTEXT = (1u << 0), GIT_CREDTYPE_USERPASS_PLAINTEXT = (1u << 0),
/* git_cred_ssh_keyfile_passphrase */ /* git_cred_ssh_key */
GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE = (1u << 1), GIT_CREDTYPE_SSH_KEY = (1u << 1),
/* git_cred_ssh_publickey */ /* git_cred_ssh_custom */
GIT_CREDTYPE_SSH_PUBLICKEY = (1u << 2), GIT_CREDTYPE_SSH_CUSTOM = (1u << 2),
} git_credtype_t; } git_credtype_t;
/* The base structure for all credential types */ /* The base structure for all credential types */
...@@ -61,24 +61,28 @@ typedef LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*git_cred_sign_callback)); ...@@ -61,24 +61,28 @@ typedef LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*git_cred_sign_callback));
typedef int (*git_cred_sign_callback)(void *, ...); typedef int (*git_cred_sign_callback)(void *, ...);
#endif #endif
/* An ssh key file and passphrase */ /**
typedef struct git_cred_ssh_keyfile_passphrase { * A ssh key from disk
*/
typedef struct git_cred_ssh_key {
git_cred parent; git_cred parent;
char *username; char *username;
char *publickey; char *publickey;
char *privatekey; char *privatekey;
char *passphrase; char *passphrase;
} git_cred_ssh_keyfile_passphrase; } git_cred_ssh_key;
/* An ssh public key and authentication callback */ /**
typedef struct git_cred_ssh_publickey { * A key with a custom signature function
*/
typedef struct git_cred_ssh_custom {
git_cred parent; git_cred parent;
char *username; char *username;
char *publickey; char *publickey;
size_t publickey_len; size_t publickey_len;
void *sign_callback; void *sign_callback;
void *sign_data; void *sign_data;
} git_cred_ssh_publickey; } git_cred_ssh_custom;
/** /**
* Check whether a credential object contains username information. * Check whether a credential object contains username information.
...@@ -89,7 +93,7 @@ typedef struct git_cred_ssh_publickey { ...@@ -89,7 +93,7 @@ typedef struct git_cred_ssh_publickey {
GIT_EXTERN(int) git_cred_has_username(git_cred *cred); GIT_EXTERN(int) git_cred_has_username(git_cred *cred);
/** /**
* Creates a new plain-text username and password credential object. * Create 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.
* *
* @param out The newly created credential object. * @param out The newly created credential object.
...@@ -103,7 +107,7 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new( ...@@ -103,7 +107,7 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new(
const char *password); const char *password);
/** /**
* Creates a new ssh key file and passphrase credential object. * Create a new passphrase-protected ssh key credential object.
* The supplied credential parameter will be internally duplicated. * The supplied credential parameter will be internally duplicated.
* *
* @param out The newly created credential object. * @param out The newly created credential object.
...@@ -113,15 +117,21 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new( ...@@ -113,15 +117,21 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new(
* @param passphrase The passphrase of the credential. * @param passphrase The passphrase of the credential.
* @return 0 for success or an error code for failure * @return 0 for success or an error code for failure
*/ */
GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new( GIT_EXTERN(int) git_cred_ssh_key_new(
git_cred **out, git_cred **out,
const char *username, const char *username,
const char *publickey, const char *publickey,
const char *privatekey, const char *privatekey,
const char *passphrase); const char *passphrase);
/** /**
* Creates a new ssh public key credential object. * Create an ssh key credential with a custom signing function.
*
* This lets you use your own function to sign the challenge.
*
* This function and its credential type is provided for completeness
* and wraps `libssh2_userauth_publickey()`, which is undocumented.
*
* The supplied credential parameter will be internally duplicated. * The supplied credential parameter will be internally duplicated.
* *
* @param out The newly created credential object. * @param out The newly created credential object.
...@@ -132,7 +142,7 @@ GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new( ...@@ -132,7 +142,7 @@ GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new(
* @param sign_data The data to pass to the sign function. * @param sign_data The data to pass to the sign function.
* @return 0 for success or an error code for failure * @return 0 for success or an error code for failure
*/ */
GIT_EXTERN(int) git_cred_ssh_publickey_new( GIT_EXTERN(int) git_cred_ssh_custom_new(
git_cred **out, git_cred **out,
const char *username, const char *username,
const char *publickey, const char *publickey,
......
...@@ -19,13 +19,13 @@ int git_cred_has_username(git_cred *cred) ...@@ -19,13 +19,13 @@ int git_cred_has_username(git_cred *cred)
ret = !!c->username; ret = !!c->username;
break; break;
} }
case GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE: { case GIT_CREDTYPE_SSH_KEY: {
git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred; git_cred_ssh_key *c = (git_cred_ssh_key *)cred;
ret = !!c->username; ret = !!c->username;
break; break;
} }
case GIT_CREDTYPE_SSH_PUBLICKEY: { case GIT_CREDTYPE_SSH_CUSTOM: {
git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred; git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
ret = !!c->username; ret = !!c->username;
break; break;
} }
...@@ -84,10 +84,10 @@ int git_cred_userpass_plaintext_new( ...@@ -84,10 +84,10 @@ int git_cred_userpass_plaintext_new(
return 0; return 0;
} }
static void ssh_keyfile_passphrase_free(struct git_cred *cred) static void ssh_key_free(struct git_cred *cred)
{ {
git_cred_ssh_keyfile_passphrase *c = git_cred_ssh_key *c =
(git_cred_ssh_keyfile_passphrase *)cred; (git_cred_ssh_key *)cred;
git__free(c->username); git__free(c->username);
git__free(c->publickey); git__free(c->publickey);
...@@ -104,9 +104,9 @@ static void ssh_keyfile_passphrase_free(struct git_cred *cred) ...@@ -104,9 +104,9 @@ static void ssh_keyfile_passphrase_free(struct git_cred *cred)
git__free(c); git__free(c);
} }
static void ssh_publickey_free(struct git_cred *cred) static void ssh_custom_free(struct git_cred *cred)
{ {
git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred; git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
git__free(c->username); git__free(c->username);
git__free(c->publickey); git__free(c->publickey);
...@@ -115,22 +115,22 @@ static void ssh_publickey_free(struct git_cred *cred) ...@@ -115,22 +115,22 @@ static void ssh_publickey_free(struct git_cred *cred)
git__free(c); git__free(c);
} }
int git_cred_ssh_keyfile_passphrase_new( int git_cred_ssh_key_new(
git_cred **cred, git_cred **cred,
const char *username, const char *username,
const char *publickey, const char *publickey,
const char *privatekey, const char *privatekey,
const char *passphrase) const char *passphrase)
{ {
git_cred_ssh_keyfile_passphrase *c; git_cred_ssh_key *c;
assert(cred && privatekey); assert(cred && privatekey);
c = git__calloc(1, sizeof(git_cred_ssh_keyfile_passphrase)); c = git__calloc(1, sizeof(git_cred_ssh_key));
GITERR_CHECK_ALLOC(c); GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE; c->parent.credtype = GIT_CREDTYPE_SSH_KEY;
c->parent.free = ssh_keyfile_passphrase_free; c->parent.free = ssh_key_free;
if (username) { if (username) {
c->username = git__strdup(username); c->username = git__strdup(username);
...@@ -154,7 +154,7 @@ int git_cred_ssh_keyfile_passphrase_new( ...@@ -154,7 +154,7 @@ int git_cred_ssh_keyfile_passphrase_new(
return 0; return 0;
} }
int git_cred_ssh_publickey_new( int git_cred_ssh_custom_new(
git_cred **cred, git_cred **cred,
const char *username, const char *username,
const char *publickey, const char *publickey,
...@@ -162,15 +162,15 @@ int git_cred_ssh_publickey_new( ...@@ -162,15 +162,15 @@ int git_cred_ssh_publickey_new(
git_cred_sign_callback sign_callback, git_cred_sign_callback sign_callback,
void *sign_data) void *sign_data)
{ {
git_cred_ssh_publickey *c; git_cred_ssh_custom *c;
assert(cred); assert(cred);
c = git__calloc(1, sizeof(git_cred_ssh_publickey)); c = git__calloc(1, sizeof(git_cred_ssh_custom));
GITERR_CHECK_ALLOC(c); GITERR_CHECK_ALLOC(c);
c->parent.credtype = GIT_CREDTYPE_SSH_PUBLICKEY; c->parent.credtype = GIT_CREDTYPE_SSH_CUSTOM;
c->parent.free = ssh_publickey_free; c->parent.free = ssh_custom_free;
if (username) { if (username) {
c->username = git__strdup(username); c->username = git__strdup(username);
......
...@@ -249,15 +249,15 @@ static int _git_ssh_authenticate_session( ...@@ -249,15 +249,15 @@ static int _git_ssh_authenticate_session(
rc = libssh2_userauth_password(session, user, c->password); rc = libssh2_userauth_password(session, user, c->password);
break; break;
} }
case GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE: { case GIT_CREDTYPE_SSH_KEY: {
git_cred_ssh_keyfile_passphrase *c = (git_cred_ssh_keyfile_passphrase *)cred; git_cred_ssh_key *c = (git_cred_ssh_key *)cred;
user = c->username ? c->username : user; user = c->username ? c->username : user;
rc = libssh2_userauth_publickey_fromfile( rc = libssh2_userauth_publickey_fromfile(
session, c->username, c->publickey, c->privatekey, c->passphrase); session, c->username, c->publickey, c->privatekey, c->passphrase);
break; break;
} }
case GIT_CREDTYPE_SSH_PUBLICKEY: { case GIT_CREDTYPE_SSH_CUSTOM: {
git_cred_ssh_publickey *c = (git_cred_ssh_publickey *)cred; git_cred_ssh_custom *c = (git_cred_ssh_custom *)cred;
user = c->username ? c->username : user; user = c->username ? c->username : user;
rc = libssh2_userauth_publickey( rc = libssh2_userauth_publickey(
...@@ -349,8 +349,8 @@ static int _git_ssh_setup_conn( ...@@ -349,8 +349,8 @@ static int _git_ssh_setup_conn(
if (t->owner->cred_acquire_cb( if (t->owner->cred_acquire_cb(
&t->cred, t->owner->url, user, &t->cred, t->owner->url, user,
GIT_CREDTYPE_USERPASS_PLAINTEXT | GIT_CREDTYPE_USERPASS_PLAINTEXT |
GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE | GIT_CREDTYPE_SSH_KEY |
GIT_CREDTYPE_SSH_PUBLICKEY, GIT_CREDTYPE_SSH_CUSTOM,
t->owner->cred_acquire_payload) < 0) t->owner->cred_acquire_payload) < 0)
goto on_error; goto on_error;
......
...@@ -47,12 +47,12 @@ static int cred_acquire_cb( ...@@ -47,12 +47,12 @@ static int cred_acquire_cb(
GIT_UNUSED(user_from_url); GIT_UNUSED(user_from_url);
GIT_UNUSED(payload); GIT_UNUSED(payload);
if (GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE & allowed_types) { if (GIT_CREDTYPE_SSH_KEY & allowed_types) {
if (!_remote_user || !_remote_ssh_pubkey || !_remote_ssh_key || !_remote_ssh_passphrase) { if (!_remote_user || !_remote_ssh_pubkey || !_remote_ssh_key || !_remote_ssh_passphrase) {
printf("GITTEST_REMOTE_USER, GITTEST_REMOTE_SSH_PUBKEY, GITTEST_REMOTE_SSH_KEY and GITTEST_REMOTE_SSH_PASSPHRASE must be set\n"); printf("GITTEST_REMOTE_USER, GITTEST_REMOTE_SSH_PUBKEY, GITTEST_REMOTE_SSH_KEY and GITTEST_REMOTE_SSH_PASSPHRASE must be set\n");
return -1; return -1;
} }
return git_cred_ssh_keyfile_passphrase_new(cred, _remote_user, _remote_ssh_pubkey, _remote_ssh_key, _remote_ssh_passphrase); return git_cred_ssh_key_new(cred, _remote_user, _remote_ssh_pubkey, _remote_ssh_key, _remote_ssh_passphrase);
} }
if (GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) { if (GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) {
......
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