diff --git a/src/agent.c b/src/agent.c index 3a15183e..0fe1c5f7 100644 --- a/src/agent.c +++ b/src/agent.c @@ -1129,6 +1129,10 @@ LIBSSH2_AGENT *libssh2_agent_init(LIBSSH2_SESSION *session) int libssh2_agent_connect(LIBSSH2_AGENT *agent) { int i, rc = LIBSSH2_ERROR_METHOD_NOT_SUPPORTED; + + if(!agent) + return LIBSSH2_ERROR_BAD_USE; + for(i = 0; agent_supported_backends[i].name; i++) { agent->ops = agent_supported_backends[i].ops; rc = agent->ops->connect(agent); @@ -1145,6 +1149,9 @@ int libssh2_agent_connect(LIBSSH2_AGENT *agent) */ int libssh2_agent_list_identities(LIBSSH2_AGENT *agent) { + if(!agent) + return LIBSSH2_ERROR_BAD_USE; + memset(&agent->transctx, 0, sizeof(agent->transctx)); /* Abandon the last fetched identities */ agent_free_identities(agent); @@ -1166,6 +1173,10 @@ int libssh2_agent_get_identity(LIBSSH2_AGENT *agent, struct libssh2_agent_publickey *prev) { struct agent_publickey *node; + + if(!agent) + return LIBSSH2_ERROR_BAD_USE; + if(prev && prev->node) { /* we have a starting point */ struct agent_publickey *prev_node = prev->node; @@ -1197,6 +1208,9 @@ int libssh2_agent_userauth(LIBSSH2_AGENT *agent, void *abstract = agent; int rc; + if(!agent) + return LIBSSH2_ERROR_BAD_USE; + if(agent->session->userauth_pblc_state == ssh2_NB_state_idle) { memset(&agent->transctx, 0, sizeof(agent->transctx)); agent->identity = identity->node; @@ -1230,6 +1244,9 @@ int libssh2_agent_sign(LIBSSH2_AGENT *agent, int rc; uint32_t key_kind_len; + if(!agent) + return LIBSSH2_ERROR_BAD_USE; + if(agent->session->userauth_pblc_state == ssh2_NB_state_idle) { memset(&agent->transctx, 0, sizeof(agent->transctx)); agent->identity = identity->node; @@ -1273,6 +1290,9 @@ cleanup: */ int libssh2_agent_disconnect(LIBSSH2_AGENT *agent) { + if(!agent) + return LIBSSH2_ERROR_BAD_USE; + if(agent->ops && agent->fd != LIBSSH2_INVALID_SOCKET) return agent->ops->disconnect(agent); return LIBSSH2_ERROR_NONE; @@ -1284,6 +1304,9 @@ int libssh2_agent_disconnect(LIBSSH2_AGENT *agent) */ void libssh2_agent_free(LIBSSH2_AGENT *agent) { + if(!agent) + return; + /* Allow connection freeing when the socket has lost its connection */ if(agent->fd != LIBSSH2_INVALID_SOCKET) libssh2_agent_disconnect(agent); @@ -1300,6 +1323,9 @@ void libssh2_agent_free(LIBSSH2_AGENT *agent) */ void libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path) { + if(!agent) + return; + if(agent->identity_agent_path) SSH2_SAFEFREE(agent->session, agent->identity_agent_path); @@ -1321,5 +1347,8 @@ void libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path) */ const char *libssh2_agent_get_identity_path(LIBSSH2_AGENT *agent) { + if(!agent) + return NULL; + return agent->identity_agent_path; }