From 682ed1812364bd42fef6f331486c5d8f5a72ef21 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Sat, 26 Dec 2009 20:24:07 +0100 Subject: [PATCH] New packet handler for SSH_USERAUTH_BANNER --- include/libssh/auth.h | 28 ++++++++++++++++++++++++++ include/libssh/session.h | 2 +- libssh/auth.c | 43 +++++++++++++++++++++++++--------------- libssh/packet.c | 3 ++- 4 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 include/libssh/auth.h diff --git a/include/libssh/auth.h b/include/libssh/auth.h new file mode 100644 index 00000000..86b31ea4 --- /dev/null +++ b/include/libssh/auth.h @@ -0,0 +1,28 @@ +/* + * This file is part of the SSH Library + * + * Copyright (c) 2009 by Aris Adamantiadis + * + * The SSH Library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + * + * The SSH Library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the SSH Library; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#ifndef AUTH_H_ +#define AUTH_H_ +#include "libssh/callbacks.h" +SSH_PACKET_CALLBACK(ssh_packet_userauth_banner); + + +#endif /* AUTH_H_ */ diff --git a/include/libssh/session.h b/include/libssh/session.h index 5cf5af5a..f4ed895c 100644 --- a/include/libssh/session.h +++ b/include/libssh/session.h @@ -29,7 +29,7 @@ typedef struct ssh_kbdint_struct* ssh_kbdint; /* These are the different states a SSH session can be into its life */ enum ssh_session_state_e { - SSH_SESSION_STATE_NONE, + SSH_SESSION_STATE_NONE=0, SSH_SESSION_STATE_CONNECTING, SSH_SESSION_STATE_SOCKET_CONNECTED, SSH_SESSION_STATE_BANNER_RECEIVED, diff --git a/libssh/auth.c b/libssh/auth.c index 409917fa..4636c3e7 100644 --- a/libssh/auth.c +++ b/libssh/auth.c @@ -38,6 +38,7 @@ #include "libssh/packet.h" #include "libssh/session.h" #include "libssh/keys.h" +#include "libssh/auth.h" /** \defgroup ssh_auth SSH Authentication functions * \brief functions to authenticate to servers @@ -66,6 +67,31 @@ static int ask_userauth(ssh_session session) { return rc; } +/** @internal + * @handles a SSH_USERAUTH_BANNER packet + * This banner should be shown to user prior to authentication + */ +SSH_PACKET_CALLBACK(ssh_packet_userauth_banner){ + ssh_string banner; + (void)type; + (void)user; + enter_function(); + banner = buffer_get_ssh_string(packet); + if (banner == NULL) { + ssh_log(session, SSH_LOG_RARE, + "Invalid SSH_USERAUTH_BANNER packet"); + } else { + ssh_log(session, SSH_LOG_PACKET, + "Received SSH_USERAUTH_BANNER packet"); + if(session->banner != NULL) + string_free(session->banner); + session->banner = banner; + } + leave_function(); + return SSH_PACKET_USED; +} + + static int wait_auth_status(ssh_session session, int kbdint) { char *auth_methods = NULL; ssh_string auth; @@ -147,22 +173,7 @@ static int wait_auth_status(ssh_session session, int kbdint) { rc = SSH_AUTH_SUCCESS; cont = 0; break; - case SSH2_MSG_USERAUTH_BANNER: - { - ssh_string banner; - banner = buffer_get_ssh_string(session->in_buffer); - if (banner == NULL) { - ssh_log(session, SSH_LOG_PACKET, - "The banner message was invalid. Continuing though\n"); - break; - } - ssh_log(session, SSH_LOG_PACKET, - "Received a message banner\n"); - string_free(session->banner); /* erase the older one */ - session->banner = banner; - break; - } default: //packet_parse(session); //FIXME: broken @@ -270,7 +281,7 @@ int ssh_userauth_none(ssh_session session, const char *username) { string_free(method); string_free(user); - if (packet_send(session) != SSH_OK) { + if (packet_send(session) == SSH_ERROR) { leave_function(); return rc; } diff --git a/libssh/packet.c b/libssh/packet.c index c4222346..f3263cc1 100644 --- a/libssh/packet.c +++ b/libssh/packet.c @@ -45,6 +45,7 @@ #include "libssh/messages.h" #include "libssh/pcap.h" #include "libssh/kex.h" +#include "libssh/auth.h" ssh_packet_callback default_packet_handlers[]= { @@ -69,7 +70,7 @@ ssh_packet_callback default_packet_handlers[]= { NULL, //#define SSH2_MSG_USERAUTH_REQUEST 50 NULL, //#define SSH2_MSG_USERAUTH_FAILURE 51 NULL, //#define SSH2_MSG_USERAUTH_SUCCESS 52 - NULL, //#define SSH2_MSG_USERAUTH_BANNER 53 + ssh_packet_userauth_banner, //#define SSH2_MSG_USERAUTH_BANNER 53 NULL, //#define SSH2_MSG_USERAUTH_PK_OK 60 SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ 60 //SSH2_MSG_USERAUTH_INFO_REQUEST 60 NULL, //#define SSH2_MSG_USERAUTH_INFO_RESPONSE 61