1
1

Improve ssh_userauth_pubkey().

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@667 7dcaeef0-15fb-0310-b436-a5af3365683c
Этот коммит содержится в:
Andreas Schneider 2009-04-30 14:46:40 +00:00
родитель 65d09f3268
Коммит deb9d30f4d

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

@ -390,50 +390,70 @@ error:
} }
/** \brief Try to authenticate through public key /**
* \param session ssh session * @brief Try to authenticate through public key.
* \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 session The ssh session to use.
* \param publickey a public key returned by publickey_from_file() *
* \param privatekey a private key returned by privatekey_from_file() * @param username The username to authenticate. You can specify NULL if
* \returns SSH_AUTH_ERROR : a serious error happened\n * ssh_option_set_username() has been used. You cannot try
* SSH_AUTH_DENIED : Authentication failed : use another method\n * two different logins in a row.
* SSH_AUTH_PARTIAL : You've been partially authenticated, you still have to use another method\n *
* SSH_AUTH_SUCCESS : Authentication success * @param publickey A public key returned by publickey_from_file().
* \see publickey_from_file() *
* \see privatekey_from_file() * @param privatekey A private key returned by privatekey_from_file().
* \see privatekey_free() *
* \see ssh_userauth_offer_pubkey() * @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 publickey_from_file()
* @see privatekey_from_file()
* @see privatekey_free()
* @see ssh_userauth_offer_pubkey()
*/ */
int ssh_userauth_pubkey(SSH_SESSION *session, const char *username,
int ssh_userauth_pubkey(SSH_SESSION *session, const char *username, STRING *publickey, PRIVATE_KEY *privatekey){ STRING *publickey, PRIVATE_KEY *privatekey) {
STRING *user = NULL; STRING *user;
STRING *service = NULL; STRING *service;
STRING *method = NULL; STRING *method;
STRING *algo = NULL; STRING *algo;
STRING *sign = NULL; STRING *sign;
int rc = SSH_AUTH_ERROR; int rc = SSH_AUTH_ERROR;
enter_function(); enter_function();
// if(session->version==1)
// return ssh_userauth1_pubkey(session,username,publickey,privatekey); #if 0
if(!username) if (session->version == 1) {
if(!(username=session->options->username)){ return ssh_userauth1_pubkey(session, username, publickey, privatekey);
if(ssh_options_default_username(session->options)){ }
#endif
if (username == NULL) {
if (session->options->username == NULL) {
if (ssh_options_default_username(session->options) < 0) {
leave_function(); leave_function();
return rc; return rc;
} else
username=session->options->username;
} }
if(ask_userauth(session)){ }
user = string_from_char(session->options->username);
} else {
user = string_from_char(username);
}
if (user == NULL) {
leave_function(); leave_function();
return rc; return rc;
} }
user = string_from_char(username); if (ask_userauth(session) < 0) {
if (user == NULL) { string_free(user);
goto error; leave_function();
return rc;
} }
service = string_from_char("ssh-connection"); service = string_from_char("ssh-connection");
if (service == NULL) { if (service == NULL) {
goto error; goto error;
@ -448,27 +468,21 @@ int ssh_userauth_pubkey(SSH_SESSION *session, const char *username, STRING *publ
} }
/* we said previously the public key was accepted */ /* we said previously the public key was accepted */
if (buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_REQUEST) < 0) { if (buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_REQUEST) < 0 ||
goto error; buffer_add_ssh_string(session->out_buffer, user) < 0 ||
} buffer_add_ssh_string(session->out_buffer, service) < 0 ||
if (buffer_add_ssh_string(session->out_buffer, user) < 0) { buffer_add_ssh_string(session->out_buffer, method) < 0 ||
goto error; buffer_add_u8(session->out_buffer, 1) < 0 ||
} buffer_add_ssh_string(session->out_buffer, algo) < 0 ||
if (buffer_add_ssh_string(session->out_buffer, service) < 0) { buffer_add_ssh_string(session->out_buffer, publickey) < 0) {
goto error;
}
if (buffer_add_ssh_string(session->out_buffer, method) < 0) {
goto error;
}
if (buffer_add_u8(session->out_buffer, 1) < 0) {
goto error;
}
if (buffer_add_ssh_string(session->out_buffer, algo) < 0) {
goto error;
}
if (buffer_add_ssh_string(session->out_buffer, publickey) < 0) {
goto error; goto error;
} }
string_free(user);
string_free(service);
string_free(method);
string_free(algo);
sign = ssh_do_sign(session,session->out_buffer, privatekey); sign = ssh_do_sign(session,session->out_buffer, privatekey);
if (sign) { if (sign) {
if (buffer_add_ssh_string(session->out_buffer,sign) < 0) { if (buffer_add_ssh_string(session->out_buffer,sign) < 0) {
@ -483,11 +497,6 @@ int ssh_userauth_pubkey(SSH_SESSION *session, const char *username, STRING *publ
rc = wait_auth_status(session,0); rc = wait_auth_status(session,0);
} }
string_free(user);
string_free(service);
string_free(method);
string_free(algo);
leave_function(); leave_function();
return rc; return rc;
error: error: