diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 1fe38a93..6abf0e49 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -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 diff --git a/docs/Makefile.am b/docs/Makefile.am index 688d8d00..6df03371 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -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 \ diff --git a/docs/libssh2_agent_get_identity_path.3 b/docs/libssh2_agent_get_identity_path.3 new file mode 100644 index 00000000..58d6dd56 --- /dev/null +++ b/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 + +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) + diff --git a/docs/libssh2_agent_set_identity_path.3 b/docs/libssh2_agent_set_identity_path.3 new file mode 100644 index 00000000..73e1266d --- /dev/null +++ b/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 + +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) + diff --git a/include/libssh2.h b/include/libssh2.h index a4fd0c85..edcdcf1e 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -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() diff --git a/src/agent.c b/src/agent.c index c0d408fa..47d944dc 100644 --- a/src/agent.c +++ b/src/agent.c @@ -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; +}