1
1

reduce code duplication and return underlying error better

Этот коммит содержится в:
Daniel Stenberg 2010-03-03 23:57:22 +01:00
родитель c0436fc602
Коммит 1f406feaa4

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

@ -111,16 +111,17 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
libssh2_error(session, LIBSSH2_ERROR_EAGAIN, libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block requesting userauth list"); "Would block requesting userauth list");
return NULL; return NULL;
} else if (rc) { }
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, /* now free the packet that was sent */
"Unable to send userauth-none request");
LIBSSH2_FREE(session, session->userauth_list_data); LIBSSH2_FREE(session, session->userauth_list_data);
session->userauth_list_data = NULL; session->userauth_list_data = NULL;
if (rc) {
libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-none request");
session->userauth_list_state = libssh2_NB_state_idle; session->userauth_list_state = libssh2_NB_state_idle;
return NULL; return NULL;
} }
LIBSSH2_FREE(session, session->userauth_list_data);
session->userauth_list_data = NULL;
session->userauth_list_state = libssh2_NB_state_sent; session->userauth_list_state = libssh2_NB_state_sent;
} }
@ -157,7 +158,8 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username,
memmove(session->userauth_list_data, session->userauth_list_data + 5, memmove(session->userauth_list_data, session->userauth_list_data + 5,
methods_len); methods_len);
session->userauth_list_data[methods_len] = '\0'; session->userauth_list_data[methods_len] = '\0';
_libssh2_debug(session, LIBSSH2_TRACE_AUTH, "Permitted auth methods: %s", _libssh2_debug(session, LIBSSH2_TRACE_AUTH,
"Permitted auth methods: %s",
session->userauth_list_data); session->userauth_list_data);
} }
@ -269,15 +271,17 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
if (rc == PACKET_EAGAIN) { if (rc == PACKET_EAGAIN) {
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block writing password request"); "Would block writing password request");
} else if (rc) { }
/* now free the sent packet */
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
if (rc) {
session->userauth_pswd_state = libssh2_NB_state_idle; session->userauth_pswd_state = libssh2_NB_state_idle;
return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, return libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth-password request"); "Unable to send userauth-password request");
} }
LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL;
session->userauth_pswd_state = libssh2_NB_state_sent; session->userauth_pswd_state = libssh2_NB_state_sent;
} }
@ -417,21 +421,19 @@ userauth_password(LIBSSH2_SESSION *session, const char *username,
return libssh2_error(session, LIBSSH2_ERROR_EAGAIN, return libssh2_error(session, LIBSSH2_ERROR_EAGAIN,
"Would block waiting"); "Would block waiting");
} }
else if (rc) {
/* free the allocated packets again */
LIBSSH2_FREE(session, session->userauth_pswd_data); LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL; session->userauth_pswd_data = NULL;
LIBSSH2_FREE(session, LIBSSH2_FREE(session, session->userauth_pswd_newpw);
session->userauth_pswd_newpw);
session->userauth_pswd_newpw = NULL; session->userauth_pswd_newpw = NULL;
if (rc) {
return libssh2_error(session, return libssh2_error(session,
LIBSSH2_ERROR_SOCKET_SEND, LIBSSH2_ERROR_SOCKET_SEND,
"Unable to send userauth " "Unable to send userauth "
"password-change request"); "password-change request");
} }
LIBSSH2_FREE(session, session->userauth_pswd_data);
session->userauth_pswd_data = NULL;
LIBSSH2_FREE(session, session->userauth_pswd_newpw);
session->userauth_pswd_newpw = NULL;
/* /*
* Ugliest use of goto ever. Blame it on the * Ugliest use of goto ever. Blame it on the
@ -637,14 +639,15 @@ sign_fromfile(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len,
const LIBSSH2_HOSTKEY_METHOD *privkeyobj; const LIBSSH2_HOSTKEY_METHOD *privkeyobj;
void *hostkey_abstract; void *hostkey_abstract;
struct iovec datavec; struct iovec datavec;
int rc;
if (file_read_privatekey(session, &privkeyobj, &hostkey_abstract, rc = file_read_privatekey(session, &privkeyobj, &hostkey_abstract,
session->userauth_pblc_method, session->userauth_pblc_method,
session->userauth_pblc_method_len, session->userauth_pblc_method_len,
privkey_file->filename, privkey_file->filename,
privkey_file->passphrase)) { privkey_file->passphrase);
return -1; if(rc)
} return rc;
datavec.iov_base = (unsigned char *)data; datavec.iov_base = (unsigned char *)data;
datavec.iov_len = data_len; datavec.iov_len = data_len;
@ -769,16 +772,17 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
memcpy(session->userauth_host_s, local_username, local_username_len); memcpy(session->userauth_host_s, local_username, local_username_len);
session->userauth_host_s += local_username_len; session->userauth_host_s += local_username_len;
if (file_read_privatekey(session, &privkeyobj, &abstract, rc = file_read_privatekey(session, &privkeyobj, &abstract,
session->userauth_host_method, session->userauth_host_method,
session->userauth_host_method_len, session->userauth_host_method_len,
privatekey, passphrase)) { privatekey, passphrase);
if(rc) {
/* Note: file_read_privatekey() calls libssh2_error() */ /* Note: file_read_privatekey() calls libssh2_error() */
LIBSSH2_FREE(session, session->userauth_host_method); LIBSSH2_FREE(session, session->userauth_host_method);
session->userauth_host_method = NULL; session->userauth_host_method = NULL;
LIBSSH2_FREE(session, session->userauth_host_packet); LIBSSH2_FREE(session, session->userauth_host_packet);
session->userauth_host_packet = NULL; session->userauth_host_packet = NULL;
return -1; return rc;
} }
_libssh2_htonu32(buf, session->session_id_len); _libssh2_htonu32(buf, session->session_id_len);