mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-06 10:27:22 +09:00
misc: Cache user home directory in session
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:
@@ -43,7 +43,7 @@ extern "C" {
|
||||
|
||||
/* in misc.c */
|
||||
/* gets the user home dir. */
|
||||
char *ssh_get_user_home_dir(void);
|
||||
char *ssh_get_user_home_dir(ssh_session session);
|
||||
char *ssh_get_local_username(void);
|
||||
int ssh_file_readaccess_ok(const char *file);
|
||||
int ssh_dir_writeable(const char *path);
|
||||
|
||||
@@ -253,6 +253,7 @@ struct ssh_session_struct {
|
||||
char *username;
|
||||
char *host;
|
||||
char *bindaddr; /* bind the client to an ip addr */
|
||||
char *homedir;
|
||||
char *sshdir;
|
||||
char *knownhosts;
|
||||
char *global_knownhosts;
|
||||
|
||||
30
src/misc.c
30
src/misc.c
@@ -108,7 +108,7 @@
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
char *ssh_get_user_home_dir(void)
|
||||
static char *ssh_get_user_home_dir_internal(void)
|
||||
{
|
||||
char tmp[PATH_MAX] = {0};
|
||||
char *szPath = NULL;
|
||||
@@ -298,7 +298,7 @@ int ssh_is_ipaddr(const char *str)
|
||||
#define NSS_BUFLEN_PASSWD 4096
|
||||
#endif /* NSS_BUFLEN_PASSWD */
|
||||
|
||||
char *ssh_get_user_home_dir(void)
|
||||
static char *ssh_get_user_home_dir_internal(void)
|
||||
{
|
||||
char *szPath = NULL;
|
||||
struct passwd pwd;
|
||||
@@ -313,7 +313,6 @@ char *ssh_get_user_home_dir(void)
|
||||
return NULL;
|
||||
}
|
||||
snprintf(buf, sizeof(buf), "%s", szPath);
|
||||
|
||||
return strdup(buf);
|
||||
}
|
||||
|
||||
@@ -428,6 +427,29 @@ int ssh_is_ipaddr(const char *str)
|
||||
|
||||
#endif /* _WIN32 */
|
||||
|
||||
char *ssh_get_user_home_dir(ssh_session session)
|
||||
{
|
||||
char *szPath = NULL;
|
||||
|
||||
/* If used previously, reuse cached value */
|
||||
if (session != NULL && session->opts.homedir != NULL) {
|
||||
return strdup(session->opts.homedir);
|
||||
}
|
||||
|
||||
szPath = ssh_get_user_home_dir_internal();
|
||||
if (szPath == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (session != NULL) {
|
||||
/* cache it:
|
||||
* failure is not fatal -- at worst we will just not cache it */
|
||||
session->opts.homedir = strdup(szPath);
|
||||
}
|
||||
|
||||
return szPath;
|
||||
}
|
||||
|
||||
char *ssh_lowercase(const char* str)
|
||||
{
|
||||
char *new = NULL, *p = NULL;
|
||||
@@ -1189,7 +1211,7 @@ char *ssh_path_expand_tilde(const char *d)
|
||||
} else {
|
||||
ld = strlen(d);
|
||||
p = (char *) d;
|
||||
h = ssh_get_user_home_dir();
|
||||
h = ssh_get_user_home_dir(NULL);
|
||||
}
|
||||
if (h == NULL) {
|
||||
return NULL;
|
||||
|
||||
@@ -405,6 +405,7 @@ void ssh_free(ssh_session session)
|
||||
SAFE_FREE(session->opts.bindaddr);
|
||||
SAFE_FREE(session->opts.username);
|
||||
SAFE_FREE(session->opts.host);
|
||||
SAFE_FREE(session->opts.homedir);
|
||||
SAFE_FREE(session->opts.sshdir);
|
||||
SAFE_FREE(session->opts.knownhosts);
|
||||
SAFE_FREE(session->opts.global_knownhosts);
|
||||
|
||||
@@ -52,7 +52,7 @@ static void torture_get_user_home_dir(void **state) {
|
||||
|
||||
(void) state;
|
||||
|
||||
user = ssh_get_user_home_dir();
|
||||
user = ssh_get_user_home_dir(NULL);
|
||||
assert_non_null(user);
|
||||
#ifndef _WIN32
|
||||
assert_string_equal(user, pwd->pw_dir);
|
||||
|
||||
Reference in New Issue
Block a user