1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-03 13:31:11 +03:00

Allow use of SSH_DIR/ when expanding key paths

This commit is contained in:
Aris Adamantiadis
2009-10-17 18:10:42 +02:00
parent a479b30298
commit 27d25752e9
4 changed files with 31 additions and 23 deletions

View File

@@ -215,6 +215,10 @@ int match_hostname(const char *host, const char *pattern, unsigned int len);
#define leave_function() (void)session #define leave_function() (void)session
#endif #endif
/* options.c */
char *dir_expand_dup(ssh_session session, const char *value, int allowsshdir);
/** Free memory space */ /** Free memory space */
#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0) #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)

View File

@@ -757,12 +757,12 @@ error:
} }
#ifdef _MSC_VER #ifdef _MSC_VER
static const char privKey_1[] = "%s/.ssh/identity"; static const char privKey_1[] = "SSH_DIR/identity";
static const char pubKey_1[] = "%s/.ssh/identity.pub"; static const char pubKey_1[] = "SSH_DIR/identity.pub";
static const char privKey_2[] = "%s/.ssh/id_dsa"; static const char privKey_2[] = "SSH_DIR/id_dsa";
static const char pubKey_2[] = "%s/.ssh/id_dsa.pub"; static const char pubKey_2[] = "SSH_DIR/id_dsa.pub";
static const char privKey_3[] = "%s/.ssh/id_rsa"; static const char privKey_3[] = "SSH_DIR/id_rsa";
static const char pubKey_3[] = "%s/.ssh/id_rsa.pub"; static const char pubKey_3[] = "SSH_DIR/id_rsa.pub";
/** Used different var to allow const char[] declaration */ /** Used different var to allow const char[] declaration */
static struct ssh_keys_struct keytab[] = { static struct ssh_keys_struct keytab[] = {
{ privKey_1, pubKey_1}, { privKey_1, pubKey_1},
@@ -774,16 +774,16 @@ static struct ssh_keys_struct keytab[] = {
/* This requires GCC extensions */ /* This requires GCC extensions */
static struct ssh_keys_struct keytab[] = { static struct ssh_keys_struct keytab[] = {
{ {
.privatekey = "identity", .privatekey = "SSH_DIR/identity",
.publickey = "identity.pub" .publickey = "SSH_DIR/identity.pub"
}, },
{ {
.privatekey = "id_dsa", .privatekey = "SSH_DIR/id_dsa",
.publickey = "id_dsa.pub", .publickey = "SSH_DIR/id_dsa.pub",
}, },
{ {
.privatekey = "id_rsa", .privatekey = "SSH_DIR/id_rsa",
.publickey = "id_rsa.pub", .publickey = "SSH_DIR/id_rsa.pub",
}, },
{ {
.privatekey = NULL, .privatekey = NULL,

View File

@@ -940,12 +940,12 @@ ssh_string publickey_from_file(ssh_session session, const char *filename,
ssh_string try_publickey_from_file(ssh_session session, struct ssh_keys_struct keytab, ssh_string try_publickey_from_file(ssh_session session, struct ssh_keys_struct keytab,
char **privkeyfile, int *type) { char **privkeyfile, int *type) {
char public[256] = {0}; char *public;
char private[256] = {0}; char *private;
const char *priv; const char *priv;
const char *pub; const char *pub;
char *new; char *new;
ssh_string pubkey; ssh_string pubkey=NULL;
pub = keytab.publickey; pub = keytab.publickey;
if (pub == NULL) { if (pub == NULL) {
@@ -963,19 +963,21 @@ ssh_string try_publickey_from_file(ssh_session session, struct ssh_keys_struct k
} }
/* are them readable ? */ /* are them readable ? */
snprintf(public, sizeof(public), "%s/%s", session->sshdir, pub); public=dir_expand_dup(session,pub,1);
snprintf(private, sizeof(private), "%s/%s", session->sshdir, priv); private=dir_expand_dup(session,priv,1);
//snprintf(public, sizeof(public), "%s/%s", session->sshdir, pub);
//snprintf(private, sizeof(private), "%s/%s", session->sshdir, priv);
ssh_log(session, SSH_LOG_PACKET, "Trying to open publickey %s", public); ssh_log(session, SSH_LOG_PACKET, "Trying to open publickey %s", public);
if (!ssh_file_readaccess_ok(public)) { if (!ssh_file_readaccess_ok(public)) {
ssh_log(session, SSH_LOG_PACKET, "Failed to open publickey %s", public); ssh_log(session, SSH_LOG_PACKET, "Failed to open publickey %s", public);
return NULL; goto error;
} }
ssh_log(session, SSH_LOG_PACKET, "Trying to open privatekey %s", private); ssh_log(session, SSH_LOG_PACKET, "Trying to open privatekey %s", private);
if (!ssh_file_readaccess_ok(private)) { if (!ssh_file_readaccess_ok(private)) {
ssh_log(session, SSH_LOG_PACKET, "Failed to open privatekey %s", private); ssh_log(session, SSH_LOG_PACKET, "Failed to open privatekey %s", private);
return NULL; goto error;
} }
ssh_log(session, SSH_LOG_PACKET, "Success opening public and private key"); ssh_log(session, SSH_LOG_PACKET, "Success opening public and private key");
@@ -990,18 +992,20 @@ ssh_string try_publickey_from_file(ssh_session session, struct ssh_keys_struct k
"Wasn't able to open public key file %s: %s", "Wasn't able to open public key file %s: %s",
public, public,
ssh_get_error(session)); ssh_get_error(session));
return NULL; goto error;
} }
new = realloc(*privkeyfile, strlen(private) + 1); new = realloc(*privkeyfile, strlen(private) + 1);
if (new == NULL) { if (new == NULL) {
string_free(pubkey); string_free(pubkey);
return NULL; goto error;
} }
strcpy(new, private); strcpy(new, private);
*privkeyfile = new; *privkeyfile = new;
error:
SAFE_FREE(public);
SAFE_FREE(private);
return pubkey; return pubkey;
} }

View File

@@ -159,7 +159,7 @@ static int ssh_options_set_algo(ssh_session session, int algo,
return 0; return 0;
} }
static char *dir_expand_dup(ssh_session session, const char *value, int allowsshdir) { char *dir_expand_dup(ssh_session session, const char *value, int allowsshdir) {
char *new; char *new;
if (value[0] == '~' && value[1] == '/') { if (value[0] == '~' && value[1] == '/') {