1
1

Add agent functions libssh2_agent_get_identity_path() and libssh2_agent_set_identity_path() (#308)

File : agent.c

Notes : 
Libssh2 uses the SSH_AUTH_SOCK env variable to read the system agent location. However, when using a custom agent path you have to set this value using setenv which is not thread-safe. The new functions allow for a way to set a custom agent socket path in a thread safe manor.
Этот коммит содержится в:
Will Cosgrove 2019-04-11 10:11:38 -07:00 коммит произвёл GitHub
родитель ff1b155731
Коммит dce4d8c742
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 116 добавлений и 4 удалений

Просмотреть файл

@ -38,8 +38,10 @@ set(MAN_PAGES
libssh2_agent_disconnect.3
libssh2_agent_free.3
libssh2_agent_get_identity.3
libssh2_agent_get_identity_path.3
libssh2_agent_init.3
libssh2_agent_list_identities.3
libssh2_agent_set_identity_path.3
libssh2_agent_userauth.3
libssh2_banner_set.3
libssh2_base64_decode.3

Просмотреть файл

@ -8,8 +8,10 @@ dist_man_MANS = \
libssh2_agent_disconnect.3 \
libssh2_agent_free.3 \
libssh2_agent_get_identity.3 \
libssh2_agent_get_identity_path.3 \
libssh2_agent_init.3 \
libssh2_agent_list_identities.3 \
libssh2_agent_set_identity_path.3 \
libssh2_agent_userauth.3 \
libssh2_banner_set.3 \
libssh2_base64_decode.3 \

22
docs/libssh2_agent_get_identity_path.3 Обычный файл
Просмотреть файл

@ -0,0 +1,22 @@
.\"
.\" Copyright (c) 2019 by Will Cosgrove
.\"
.TH libssh2_agent_get_identity_path 3 "6 Mar 2019" "libssh2 1.9" "libssh2 manual"
.SH NAME
libssh2_agent_get_identity_path - gets the custom ssh-agent socket path
.SH SYNOPSIS
#include <libssh2.h>
const char *
libssh2_agent_get_identity_path(LIBSSH2_AGENT *agent);
.SH DESCRIPTION
Returns the custom agent identity socket path if set using libssh2_agent_set_identity_path()
.SH RETURN VALUE
Returns the socket path on disk.
.SH AVAILABILITY
Added in libssh2 1.9
.SH SEE ALSO
.BR libssh2_agent_init(3)
.BR libssh2_agent_set_identity_path(3)

22
docs/libssh2_agent_set_identity_path.3 Обычный файл
Просмотреть файл

@ -0,0 +1,22 @@
.\"
.\" Copyright (c) 2019 by Will Cosgrove
.\"
.TH libssh2_agent_set_identity_path 3 "6 Mar 2019" "libssh2 1.9" "libssh2 manual"
.SH NAME
libssh2_agent_set_identity_path - set an ssh-agent socket path on disk
.SH SYNOPSIS
#include <libssh2.h>
void
libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path);
.SH DESCRIPTION
Allows a custom agent identity socket path instead of the default SSH_AUTH_SOCK env value
.SH RETURN VALUE
Returns void
.SH AVAILABILITY
Added in libssh2 1.9
.SH SEE ALSO
.BR libssh2_agent_init(3)
.BR libssh2_agent_get_identity_path(3)

Просмотреть файл

@ -1265,6 +1265,24 @@ libssh2_agent_disconnect(LIBSSH2_AGENT *agent);
LIBSSH2_API void
libssh2_agent_free(LIBSSH2_AGENT *agent);
/*
* libssh2_agent_set_identity_path()
*
* Allows a custom agent identity socket path beyond SSH_AUTH_SOCK env
*
*/
LIBSSH2_API void
libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent,
const char *path);
/*
* libssh2_agent_get_identity_path()
*
* Returns the custom agent identity socket path if set
*
*/
LIBSSH2_API const char *
libssh2_agent_get_identity_path(LIBSSH2_AGENT *agent);
/*
* libssh2_keepalive_config()

Просмотреть файл

@ -138,6 +138,8 @@ struct _LIBSSH2_AGENT
struct agent_transaction_ctx transctx;
struct agent_publickey *identity;
struct list_head head; /* list of public keys */
char *identity_agent_path; /* Path to a custom identity agent socket */
};
#ifdef PF_UNIX
@ -147,10 +149,13 @@ agent_connect_unix(LIBSSH2_AGENT *agent)
const char *path;
struct sockaddr_un s_un;
path = getenv("SSH_AUTH_SOCK");
if(!path)
return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE,
"no auth sock variable");
path = agent->identity_agent_path;
if (!path) {
path = getenv("SSH_AUTH_SOCK");
if (!path)
return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE,
"no auth sock variable");
}
agent->fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(agent->fd < 0)
@ -673,6 +678,7 @@ libssh2_agent_init(LIBSSH2_SESSION *session)
}
agent->fd = LIBSSH2_INVALID_SOCKET;
agent->session = session;
agent->identity_agent_path = NULL;
_libssh2_list_init(&agent->head);
return agent;
@ -809,6 +815,46 @@ libssh2_agent_free(LIBSSH2_AGENT *agent)
if(agent->fd != LIBSSH2_INVALID_SOCKET) {
libssh2_agent_disconnect(agent);
}
if(agent->identity_agent_path != NULL)
LIBSSH2_FREE(agent->session, agent->identity_agent_path);
agent_free_identities(agent);
LIBSSH2_FREE(agent->session, agent);
}
/*
* libssh2_agent_set_identity_path()
*
* Allows a custom agent socket path beyond SSH_AUTH_SOCK env
*
*/
LIBSSH2_API void
libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path)
{
if (agent->identity_agent_path) {
LIBSSH2_FREE(agent->session, agent->identity_agent_path);
agent->identity_agent_path = NULL;
}
if (path != NULL) {
size_t path_len = strlen(path);
if(path_len < SIZE_MAX - 1) {
char *path_buf = LIBSSH2_ALLOC(agent->session, path_len + 1);
memcpy(path_buf, path, path_len);
path_buf[path_len] = '\0';
agent->identity_agent_path = path_buf;
}
}
}
/*
* libssh2_agent_get_identity_path()
*
* Returns the custom agent socket path if set
*
*/
LIBSSH2_API const char * libssh2_agent_get_identity_path(LIBSSH2_AGENT *agent)
{
return agent->identity_agent_path;
}