mirror of
https://github.com/libssh2/libssh2.git
synced 2025-07-04 09:22:28 +03:00
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.
This commit is contained in:
@ -38,8 +38,10 @@ set(MAN_PAGES
|
|||||||
libssh2_agent_disconnect.3
|
libssh2_agent_disconnect.3
|
||||||
libssh2_agent_free.3
|
libssh2_agent_free.3
|
||||||
libssh2_agent_get_identity.3
|
libssh2_agent_get_identity.3
|
||||||
|
libssh2_agent_get_identity_path.3
|
||||||
libssh2_agent_init.3
|
libssh2_agent_init.3
|
||||||
libssh2_agent_list_identities.3
|
libssh2_agent_list_identities.3
|
||||||
|
libssh2_agent_set_identity_path.3
|
||||||
libssh2_agent_userauth.3
|
libssh2_agent_userauth.3
|
||||||
libssh2_banner_set.3
|
libssh2_banner_set.3
|
||||||
libssh2_base64_decode.3
|
libssh2_base64_decode.3
|
||||||
|
@ -8,8 +8,10 @@ dist_man_MANS = \
|
|||||||
libssh2_agent_disconnect.3 \
|
libssh2_agent_disconnect.3 \
|
||||||
libssh2_agent_free.3 \
|
libssh2_agent_free.3 \
|
||||||
libssh2_agent_get_identity.3 \
|
libssh2_agent_get_identity.3 \
|
||||||
|
libssh2_agent_get_identity_path.3 \
|
||||||
libssh2_agent_init.3 \
|
libssh2_agent_init.3 \
|
||||||
libssh2_agent_list_identities.3 \
|
libssh2_agent_list_identities.3 \
|
||||||
|
libssh2_agent_set_identity_path.3 \
|
||||||
libssh2_agent_userauth.3 \
|
libssh2_agent_userauth.3 \
|
||||||
libssh2_banner_set.3 \
|
libssh2_banner_set.3 \
|
||||||
libssh2_base64_decode.3 \
|
libssh2_base64_decode.3 \
|
||||||
|
22
docs/libssh2_agent_get_identity_path.3
Normal file
22
docs/libssh2_agent_get_identity_path.3
Normal file
@ -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
Normal file
22
docs/libssh2_agent_set_identity_path.3
Normal file
@ -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_API void
|
||||||
libssh2_agent_free(LIBSSH2_AGENT *agent);
|
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()
|
* libssh2_keepalive_config()
|
||||||
|
54
src/agent.c
54
src/agent.c
@ -138,6 +138,8 @@ struct _LIBSSH2_AGENT
|
|||||||
struct agent_transaction_ctx transctx;
|
struct agent_transaction_ctx transctx;
|
||||||
struct agent_publickey *identity;
|
struct agent_publickey *identity;
|
||||||
struct list_head head; /* list of public keys */
|
struct list_head head; /* list of public keys */
|
||||||
|
|
||||||
|
char *identity_agent_path; /* Path to a custom identity agent socket */
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef PF_UNIX
|
#ifdef PF_UNIX
|
||||||
@ -147,10 +149,13 @@ agent_connect_unix(LIBSSH2_AGENT *agent)
|
|||||||
const char *path;
|
const char *path;
|
||||||
struct sockaddr_un s_un;
|
struct sockaddr_un s_un;
|
||||||
|
|
||||||
path = getenv("SSH_AUTH_SOCK");
|
path = agent->identity_agent_path;
|
||||||
if(!path)
|
if (!path) {
|
||||||
return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE,
|
path = getenv("SSH_AUTH_SOCK");
|
||||||
"no auth sock variable");
|
if (!path)
|
||||||
|
return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE,
|
||||||
|
"no auth sock variable");
|
||||||
|
}
|
||||||
|
|
||||||
agent->fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
agent->fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||||
if(agent->fd < 0)
|
if(agent->fd < 0)
|
||||||
@ -673,6 +678,7 @@ libssh2_agent_init(LIBSSH2_SESSION *session)
|
|||||||
}
|
}
|
||||||
agent->fd = LIBSSH2_INVALID_SOCKET;
|
agent->fd = LIBSSH2_INVALID_SOCKET;
|
||||||
agent->session = session;
|
agent->session = session;
|
||||||
|
agent->identity_agent_path = NULL;
|
||||||
_libssh2_list_init(&agent->head);
|
_libssh2_list_init(&agent->head);
|
||||||
|
|
||||||
return agent;
|
return agent;
|
||||||
@ -809,6 +815,46 @@ libssh2_agent_free(LIBSSH2_AGENT *agent)
|
|||||||
if(agent->fd != LIBSSH2_INVALID_SOCKET) {
|
if(agent->fd != LIBSSH2_INVALID_SOCKET) {
|
||||||
libssh2_agent_disconnect(agent);
|
libssh2_agent_disconnect(agent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(agent->identity_agent_path != NULL)
|
||||||
|
LIBSSH2_FREE(agent->session, agent->identity_agent_path);
|
||||||
|
|
||||||
agent_free_identities(agent);
|
agent_free_identities(agent);
|
||||||
LIBSSH2_FREE(agent->session, 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;
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user