Fix percent expand character %d to home directory

Fixes: #349

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Pavol Žáčik <pzacik@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2026-01-27 16:21:38 +01:00
parent 31ceec02fe
commit ce0b616bc6
8 changed files with 29 additions and 32 deletions

View File

@@ -1236,7 +1236,7 @@ char *ssh_path_expand_tilde(const char *d)
/** @internal
* @brief expands a string in function of session options
* @param[in] s Format string to expand. Known parameters:
* %d SSH configuration directory (~/.ssh)
* %d user home directory (~)
* %h target host name
* %u local username
* %l local hostname
@@ -1301,10 +1301,9 @@ char *ssh_path_expand_escape(ssh_session session, const char *s)
case '%':
goto escape;
case 'd':
if (session->opts.sshdir) {
x = strdup(session->opts.sshdir);
} else {
ssh_set_error(session, SSH_FATAL, "Cannot expand sshdir");
x = ssh_get_user_home_dir(session);
if (x == NULL) {
ssh_set_error(session, SSH_FATAL, "Cannot expand homedir");
free(buf);
free(r);
return NULL;
@@ -1348,9 +1347,7 @@ char *ssh_path_expand_escape(ssh_session session, const char *s)
break;
}
default:
ssh_set_error(session,
SSH_FATAL,
"Wrong escape sequence detected");
ssh_set_error(session, SSH_FATAL, "Wrong escape sequence detected");
free(buf);
free(r);
return NULL;

View File

@@ -1963,7 +1963,7 @@ int ssh_options_parse_config(ssh_session session, const char *filename)
/* set default filename */
if (filename == NULL) {
expanded_filename = ssh_path_expand_escape(session, "%d/config");
expanded_filename = ssh_path_expand_escape(session, "%d/.ssh/config");
} else {
expanded_filename = ssh_path_expand_escape(session, filename);
}
@@ -2021,7 +2021,7 @@ int ssh_options_apply(ssh_session session)
if ((session->opts.exp_flags & SSH_OPT_EXP_FLAG_KNOWNHOSTS) == 0) {
if (session->opts.knownhosts == NULL) {
tmp = ssh_path_expand_escape(session, "%d/known_hosts");
tmp = ssh_path_expand_escape(session, "%d/.ssh/known_hosts");
} else {
tmp = ssh_path_expand_escape(session, session->opts.knownhosts);
}

View File

@@ -168,7 +168,7 @@ ssh_session ssh_new(void)
}
#endif /* WITH_GSSAPI */
id = strdup("%d/id_ed25519");
id = strdup("%d/.ssh/id_ed25519");
if (id == NULL) {
goto err;
}
@@ -179,7 +179,7 @@ ssh_session ssh_new(void)
}
#ifdef HAVE_ECC
id = strdup("%d/id_ecdsa");
id = strdup("%d/.ssh/id_ecdsa");
if (id == NULL) {
goto err;
}
@@ -189,7 +189,7 @@ ssh_session ssh_new(void)
}
#endif
id = strdup("%d/id_rsa");
id = strdup("%d/.ssh/id_rsa");
if (id == NULL) {
goto err;
}
@@ -200,7 +200,7 @@ ssh_session ssh_new(void)
#ifdef WITH_FIDO2
/* Add security key identities */
id = strdup("%d/id_ed25519_sk");
id = strdup("%d/.ssh/id_ed25519_sk");
if (id == NULL) {
goto err;
}
@@ -210,7 +210,7 @@ ssh_session ssh_new(void)
}
#ifdef HAVE_ECC
id = strdup("%d/id_ecdsa_sk");
id = strdup("%d/.ssh/id_ecdsa_sk");
if (id == NULL) {
goto err;
}

View File

@@ -376,7 +376,7 @@ torture_auth_autopubkey_protected_auth_function (const char *prompt, char *buf,
assert_int_equal(echo, 0);
assert_int_equal(verify, 0);
expected_id = ssh_path_expand_escape(data->session, "%d/id_rsa_protected");
expected_id = ssh_path_expand_escape(data->session, "%d/.ssh/id_rsa_protected");
assert_true(expected_id != NULL);
rc = ssh_userauth_publickey_auto_get_current_identity(data->session, &id);
@@ -429,7 +429,7 @@ static void torture_auth_autopubkey_protected(void **state) {
/* Try id_rsa_protected first.
*/
rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "%d/id_rsa_protected");
rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "%d/.ssh/id_rsa_protected");
assert_int_equal(rc, SSH_OK);
rc = ssh_connect(session);

View File

@@ -100,13 +100,10 @@ static int session_setup(void **state)
static int session_setup_ssh_dir(void **state)
{
struct torture_state *s = *state;
const char *no_home = "~/.no_ssh";
int rc;
session_setup(state);
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_SSH_DIR, no_home);
assert_ssh_return_code(s->ssh.session, rc);
s->ssh.session->opts.homedir = strdup("~/.no_ssh");
return 0;
}

View File

@@ -25,7 +25,7 @@ extern LIBSSH_THREAD int ssh_log_level;
#define HOSTKEYALGORITHMS "ssh-ed25519,ecdsa-sha2-nistp521,ssh-rsa"
#define PUBKEYACCEPTEDTYPES "rsa-sha2-512,ssh-rsa,ecdsa-sha2-nistp521"
#define MACS "hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-sha1-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com"
#define USER_KNOWN_HOSTS "%d/my_known_hosts"
#define USER_KNOWN_HOSTS "%d/.ssh/my_known_hosts"
#define GLOBAL_KNOWN_HOSTS "/etc/ssh/my_ssh_known_hosts"
#define BIND_ADDRESS "::1"

View File

@@ -288,7 +288,8 @@ static void torture_path_expand_escape(void **state) {
const char *s = "%d/%h/%p/by/%r";
char *e;
session->opts.sshdir = strdup("guru");
/* Set the homedir here to prevent querying the NSS DB */
session->opts.homedir = strdup("guru");
session->opts.host = strdup("meditation");
session->opts.port = 0;
session->opts.username = strdup("root");
@@ -310,9 +311,10 @@ static void torture_path_expand_known_hosts(void **state) {
ssh_session session = *state;
char *tmp;
session->opts.sshdir = strdup("/home/guru/.ssh");
/* Set the homedir here to prevent querying the NSS DB */
session->opts.homedir = strdup("/home/guru");
tmp = ssh_path_expand_escape(session, "%d/known_hosts");
tmp = ssh_path_expand_escape(session, "%d/.ssh/known_hosts");
assert_non_null(tmp);
assert_string_equal(tmp, "/home/guru/.ssh/known_hosts");
free(tmp);
@@ -322,9 +324,10 @@ static void torture_path_expand_percent(void **state) {
ssh_session session = *state;
char *tmp;
session->opts.sshdir = strdup("/home/guru/.ssh");
/* Set the homedir here to prevent querying the NSS DB */
session->opts.homedir = strdup("/home/guru");
tmp = ssh_path_expand_escape(session, "%d/config%%1");
tmp = ssh_path_expand_escape(session, "%d/.ssh/config%%1");
assert_non_null(tmp);
assert_string_equal(tmp, "/home/guru/.ssh/config%1");
free(tmp);

View File

@@ -2067,25 +2067,25 @@ static void torture_options_apply (void **state)
rc = ssh_list_append(awaited_list, id);
assert_int_equal(rc, SSH_OK);
/* append the defaults; this list is copied from ssh_new@src/session.c */
id = ssh_path_expand_escape(session, "%d/id_ed25519");
id = ssh_path_expand_escape(session, "%d/.ssh/id_ed25519");
rc = ssh_list_append(awaited_list, id);
assert_int_equal(rc, SSH_OK);
#ifdef HAVE_ECC
id = ssh_path_expand_escape(session, "%d/id_ecdsa");
id = ssh_path_expand_escape(session, "%d/.ssh/id_ecdsa");
rc = ssh_list_append(awaited_list, id);
assert_int_equal(rc, SSH_OK);
#endif
id = ssh_path_expand_escape(session, "%d/id_rsa");
id = ssh_path_expand_escape(session, "%d/.ssh/id_rsa");
rc = ssh_list_append(awaited_list, id);
assert_int_equal(rc, SSH_OK);
#ifdef WITH_FIDO2
/* Add security key identities */
id = ssh_path_expand_escape(session, "%d/id_ed25519_sk");
id = ssh_path_expand_escape(session, "%d/.ssh/id_ed25519_sk");
rc = ssh_list_append(awaited_list, id);
assert_int_equal(rc, SSH_OK);
#ifdef HAVE_ECC
id = ssh_path_expand_escape(session, "%d/id_ecdsa_sk");
id = ssh_path_expand_escape(session, "%d/.ssh/id_ecdsa_sk");
rc = ssh_list_append(awaited_list, id);
assert_int_equal(rc, SSH_OK);
#endif /* HAVE_ECC */