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

config: Expand tilde when handling include directives

Related: #93

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2022-08-31 16:18:16 +02:00
committed by Andreas Schneider
parent 7787dad9bd
commit d69026d7a4
3 changed files with 56 additions and 1 deletions

View File

@ -548,6 +548,11 @@ ssh_config_make_absolute(ssh_session session,
return out; return out;
} }
/* paths starting with tilde are already absolute */
if (path[0] == '~') {
return ssh_path_expand_tilde(path);
}
/* Parsing user config relative to home directory (generally ~/.ssh) */ /* Parsing user config relative to home directory (generally ~/.ssh) */
if (session->opts.sshdir == NULL) { if (session->opts.sshdir == NULL) {
ssh_set_error_invalid(session); ssh_set_error_invalid(session);

View File

@ -2,6 +2,11 @@
#define LIBSSH_STATIC #define LIBSSH_STATIC
#ifndef _WIN32
#define _POSIX_PTHREAD_SEMANTICS
#include <pwd.h>
#endif
#include "torture.h" #include "torture.h"
#include "libssh/options.h" #include "libssh/options.h"
#include "libssh/session.h" #include "libssh/session.h"
@ -1710,6 +1715,27 @@ static void torture_config_make_absolute_int(void **state, bool no_sshdir_fails)
{ {
ssh_session session = *state; ssh_session session = *state;
char *result = NULL; char *result = NULL;
#ifndef _WIN32
char h[256];
char *user;
char *home;
user = getenv("USER");
if (user == NULL) {
user = getenv("LOGNAME");
}
/* in certain CIs there no such variables */
if (!user) {
struct passwd *pw = getpwuid(getuid());
if (pw){
user = pw->pw_name;
}
}
home = getenv("HOME");
assert_non_null(home);
#endif
/* Absolute path already -- should not change in any case */ /* Absolute path already -- should not change in any case */
result = ssh_config_make_absolute(session, "/etc/ssh/ssh_config.d/*.conf", 1); result = ssh_config_make_absolute(session, "/etc/ssh/ssh_config.d/*.conf", 1);
@ -1742,6 +1768,30 @@ static void torture_config_make_absolute_int(void **state, bool no_sshdir_fails)
result = ssh_config_make_absolute(session, "my_config", 0); result = ssh_config_make_absolute(session, "my_config", 0);
assert_string_equal(result, "/tmp/ssh/my_config"); assert_string_equal(result, "/tmp/ssh/my_config");
free(result); free(result);
#ifndef _WIN32
/* Tilde expansion works only in user config */
result = ssh_config_make_absolute(session, "~/.ssh/config.d/*.conf", 0);
snprintf(h, 256 - 1, "%s/.ssh/config.d/*.conf", home);
assert_string_equal(result, h);
free(result);
snprintf(h, 256 - 1, "~%s/.ssh/config.d/*.conf", user);
result = ssh_config_make_absolute(session, h, 0);
snprintf(h, 256 - 1, "%s/.ssh/config.d/*.conf", home);
assert_string_equal(result, h);
free(result);
/* in global config its just prefixed without expansion */
result = ssh_config_make_absolute(session, "~/.ssh/config.d/*.conf", 1);
assert_string_equal(result, "/etc/ssh/~/.ssh/config.d/*.conf");
free(result);
snprintf(h, 256 - 1, "~%s/.ssh/config.d/*.conf", user);
result = ssh_config_make_absolute(session, h, 1);
snprintf(h, 256 - 1, "/etc/ssh/~%s/.ssh/config.d/*.conf", user);
assert_string_equal(result, h);
free(result);
#endif
} }
static void torture_config_make_absolute(void **state) static void torture_config_make_absolute(void **state)

View File

@ -4,8 +4,8 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
#ifndef _WIN32
#ifndef _WIN32
#define _POSIX_PTHREAD_SEMANTICS #define _POSIX_PTHREAD_SEMANTICS
#include <pwd.h> #include <pwd.h>
#endif #endif