1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-02 01:17:52 +03:00

Fixed ssh_get_home_dir and ssh dir to be more portable on UNIX systems.

Thanks to Pino Toscano.
This commit is contained in:
Andreas Schneider
2009-10-15 14:53:11 +02:00
parent c360ed1d9a
commit cbf012c337
4 changed files with 36 additions and 24 deletions

View File

@@ -774,16 +774,16 @@ static struct ssh_keys_struct keytab[] = {
/* This requires GCC extensions */
static struct ssh_keys_struct keytab[] = {
{
.privatekey = "%s/.ssh/identity",
.publickey = "%s/.ssh/identity.pub"
.privatekey = "identity",
.publickey = "identity.pub"
},
{
.privatekey = "%s/.ssh/id_dsa",
.publickey = "%s/.ssh/id_dsa.pub",
.privatekey = "id_dsa",
.publickey = "id_dsa.pub",
},
{
.privatekey = "%s/.ssh/id_rsa",
.publickey = "%s/.ssh/id_rsa.pub",
.privatekey = "id_rsa",
.publickey = "id_rsa.pub",
},
{
.privatekey = NULL,

View File

@@ -940,8 +940,6 @@ 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,
char **privkeyfile, int *type) {
static char *home = NULL;
char public[256] = {0};
char private[256] = {0};
const char *priv;
@@ -949,14 +947,6 @@ ssh_string try_publickey_from_file(ssh_session session, struct ssh_keys_struct k
char *new;
ssh_string pubkey;
if (home == NULL) {
home = ssh_get_user_home_dir();
if (home == NULL) {
ssh_set_error(session,SSH_FATAL,"User home dir impossible to guess");
return NULL;
}
}
pub = keytab.publickey;
if (pub == NULL) {
return NULL;
@@ -966,15 +956,22 @@ ssh_string try_publickey_from_file(ssh_session session, struct ssh_keys_struct k
return NULL;
}
if (session->sshdir == NULL) {
if (ssh_options_set(session, SSH_OPTIONS_SSH_DIR, NULL) < 0) {
return NULL;
}
}
/* are them readable ? */
snprintf(public, sizeof(public), pub, home);
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);
if (!ssh_file_readaccess_ok(public)) {
ssh_log(session, SSH_LOG_PACKET, "Failed to open publickey %s", public);
return NULL;
}
snprintf(private, sizeof(private), priv, home);
ssh_log(session, SSH_LOG_PACKET, "Trying to open privatekey %s", private);
if (!ssh_file_readaccess_ok(private)) {
ssh_log(session, SSH_LOG_PACKET, "Failed to open privatekey %s", private);

View File

@@ -71,9 +71,16 @@
#ifdef _WIN32
char *ssh_get_user_home_dir(void) {
static char szPath[MAX_PATH] = {0};
char tmp[MAX_PATH] = {0};
char szPath = NULL;
if (SHGetSpecialFolderPathA(NULL, szPath, CSIDL_PROFILE, TRUE)) {
if (SHGetSpecialFolderPathA(NULL, tmp, CSIDL_PROFILE, TRUE)) {
szPath = malloc(strlen(szPath) + 1);
if (home == NULL) {
return NULL;
}
strcpy(szPath, tmp);
return szPath;
}
@@ -90,7 +97,7 @@ char *ssh_get_user_home_dir(void) {
}
#else /* _WIN32 */
char *ssh_get_user_home_dir(void) {
static char szPath[PATH_MAX] = {0};
char *szPath = NULL;
struct passwd *pwd = NULL;
pwd = getpwuid(getuid());
@@ -98,7 +105,7 @@ char *ssh_get_user_home_dir(void) {
return NULL;
}
snprintf(szPath, PATH_MAX - 1, "%s", pwd->pw_dir);
szPath = strdup(pwd->pw_dir);
return szPath;
}

View File

@@ -163,15 +163,23 @@ static char *dir_expand_dup(ssh_session session, const char *value, int allowssh
char *new;
if (value[0] == '~' && value[1] == '/') {
const char *homedir = ssh_get_user_home_dir();
size_t lv = strlen(value + 1), lh = strlen(homedir);
char *homedir = ssh_get_user_home_dir();
size_t lv, lh;
if (homedir == NULL) {
return NULL;
}
lv = strlen(value + 1);
lh = strlen(homedir);
new = malloc(lv + lh + 1);
if (new == NULL) {
ssh_set_error_oom(session);
SAFE_FREE(homedir);
return NULL;
}
memcpy(new, homedir, lh);
SAFE_FREE(homedir);
memcpy(new + lh, value + 1, lv + 1);
return new;
}