summaryrefslogtreecommitdiffstats
path: root/libssh/auth.c
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2009-04-30 14:58:08 +0000
committerAndreas Schneider <mail@cynapses.org>2009-04-30 14:58:08 +0000
commit015b1649b1a9e9d80336409066e5085584206e78 (patch)
treec48917689e71917bf6572306b71b231d0671944b /libssh/auth.c
parent5b1c21593cbea0c4dc2ec4437d25c06f4d125787 (diff)
downloadlibssh-015b1649b1a9e9d80336409066e5085584206e78.tar.gz
libssh-015b1649b1a9e9d80336409066e5085584206e78.tar.xz
libssh-015b1649b1a9e9d80336409066e5085584206e78.zip
Improve ssh_userauth_password().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@670 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh/auth.c')
-rw-r--r--libssh/auth.c175
1 files changed, 91 insertions, 84 deletions
diff --git a/libssh/auth.c b/libssh/auth.c
index e9b2d55f..c884af4c 100644
--- a/libssh/auth.c
+++ b/libssh/auth.c
@@ -637,107 +637,114 @@ error:
}
#endif /* _WIN32 */
-/** \brief Try to authenticate by password
- * \param session ssh session
- * \param username username to authenticate. You can specify NULL if
- * ssh_option_set_username() has been used. You cannot try two different logins in a row.
- * \param password password to use. Take care to clean it after authentication
- * \returns SSH_AUTH_ERROR : a serious error happened\n
- * SSH_AUTH_DENIED : Authentication failed : use another method\n
- * SSH_AUTH_PARTIAL : You've been partially authenticated, you still have to use another method\n
- * SSH_AUTH_SUCCESS : Authentication success
- * \see ssh_userauth_kbdint()
+/**
+ * @brief Try to authenticate by password.
+ *
+ * @param session The ssh session to use.
+ *
+ * @param username The username to authenticate. You can specify NULL if
+ * ssh_option_set_username() has been used. You cannot try
+ * two different logins in a row.
+ *
+ * @param password The password to use. Take care to clean it after
+ * the authentication.
+ *
+ * @returns SSH_AUTH_ERROR: A serious error happened.\n
+ * SSH_AUTH_DENIED: Authentication failed: use another method.\n
+ * SSH_AUTH_PARTIAL: You've been partially authenticated, you still
+ * have to use another method.\n
+ * SSH_AUTH_SUCCESS: Authentication successful.
+ *
+ * @see ssh_userauth_kbdint()
+ * @see BURN_STRING
*/
+int ssh_userauth_password(SSH_SESSION *session, const char *username,
+ const char *password) {
+ STRING *user = NULL;
+ STRING *service = NULL;
+ STRING *method = NULL;
+ STRING *pwd = NULL;
+ int rc = SSH_AUTH_ERROR;
+ enter_function();
-int ssh_userauth_password(SSH_SESSION *session, const char *username, const char *password){
- STRING *user = NULL;
- STRING *service = NULL;
- STRING *method = NULL;
- STRING *pwd = NULL;
- int rc = SSH_AUTH_ERROR;
-
- enter_function();
#ifdef HAVE_SSH1
- if(session->version==1){
- rc = ssh_userauth1_password(session,username,password);
+ if (session->version == 1) {
+ rc = ssh_userauth1_password(session, username, password);
+ leave_function();
+ return rc;
+ }
+#endif
+
+ if (username == NULL) {
+ if (session->options->username == NULL) {
+ if (ssh_options_default_username(session->options) < 0) {
leave_function();
return rc;
+ }
}
-#endif
- if(!username)
- if(!(username=session->options->username)){
- if(ssh_options_default_username(session->options)){
- leave_function();
- return rc;
- } else
- username=session->options->username;
- }
- if(ask_userauth(session)) {
- leave_function();
- return rc;
- }
-
+ user = string_from_char(session->options->username);
+ } else {
user = string_from_char(username);
- if (user == NULL) {
- goto error;
- }
- service = string_from_char("ssh-connection");
- if (service == NULL) {
- goto error;
- }
- method = string_from_char("password");
- if (method == NULL) {
- goto error;
- }
- pwd = string_from_char(password);
- if (pwd == NULL) {
- goto error;
- }
+ }
- if (buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_REQUEST) < 0) {
- goto error;
- }
- if (buffer_add_ssh_string(session->out_buffer, user) < 0) {
- goto error;
- }
- if (buffer_add_ssh_string(session->out_buffer, service) < 0) {
- goto error;
- }
- if (buffer_add_ssh_string(session->out_buffer, method) < 0) {
- goto error;
- }
- if (buffer_add_u8(session->out_buffer, 0) < 0) {
- goto error;
- }
- if (buffer_add_ssh_string(session->out_buffer, pwd) < 0) {
- goto error;
- }
+ if (user == NULL) {
+ leave_function();
+ return rc;
+ }
+ if (ask_userauth(session) < 0) {
string_free(user);
- string_free(service);
- string_free(method);
- string_burn(pwd);
- string_free(pwd);
+ leave_function();
+ return rc;
+ }
- if (packet_send(session) != SSH_OK) {
- leave_function();
- return rc;
- }
- rc = wait_auth_status(session, 0);
+ service = string_from_char("ssh-connection");
+ if (service == NULL) {
+ goto error;
+ }
+ method = string_from_char("password");
+ if (method == NULL) {
+ goto error;
+ }
+ pwd = string_from_char(password);
+ if (pwd == NULL) {
+ goto error;
+ }
+ if (buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_REQUEST) < 0 ||
+ buffer_add_ssh_string(session->out_buffer, user) < 0 ||
+ buffer_add_ssh_string(session->out_buffer, service) < 0 ||
+ buffer_add_ssh_string(session->out_buffer, method) < 0 ||
+ buffer_add_u8(session->out_buffer, 0) < 0 ||
+ buffer_add_ssh_string(session->out_buffer, pwd) < 0) {
+ goto error;
+ }
+
+ string_free(user);
+ string_free(service);
+ string_free(method);
+ string_burn(pwd);
+ string_free(pwd);
+
+ if (packet_send(session) != SSH_OK) {
leave_function();
return rc;
+ }
+ rc = wait_auth_status(session, 0);
+
+ leave_function();
+ return rc;
error:
- buffer_free(session->out_buffer);
- string_free(user);
- string_free(service);
- string_free(method);
- string_burn(pwd);
- string_free(pwd);
+ buffer_free(session->out_buffer);
+ string_free(user);
+ string_free(service);
+ string_free(method);
+ string_burn(pwd);
+ string_free(pwd);
- leave_function();
- return rc;
+ leave_function();
+ return rc;
}
static struct keys_struct keytab[] = {