1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* Copyright (c) 2010, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "stdafx.h"
#include "psiclient.h"
#include "sshsession.h"
#include <algorithm>
#include <winsock2.h>
SSHSession::SSHSession(const ssh_credentials& credentials)
: m_server_host(credentials.server_host), m_username(credentials.username),
m_password(credentials.password), m_ssh_server_fingerprint(credentials.ssh_server_fingerprint)
{
transform(m_ssh_server_fingerprint.begin(), m_ssh_server_fingerprint.end(), m_ssh_server_fingerprint.begin(), ::toupper);
m_server_port = credentials.server_port;
m_socket = -1;
m_session = 0;
m_connected = false;
}
SSHSession::~SSHSession(void)
{
TearDown();
}
bool SSHSession::Connect(void)
{
if (IsConnected())
{
TearDown();
}
try
{
int rc;
struct sockaddr_in sin;
hostent* pHost;
m_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (!(pHost = gethostbyname(m_server_host.c_str())))
{
my_print(false, _T("gethostbyname failed: %d"), WSAGetLastError());
throw 0;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = *(u_long*)(pHost->h_addr);
sin.sin_port = htons(m_server_port);
if (connect(m_socket, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0)
{
my_print(false, _T("failed to connect"));
throw 0;
}
// Create a session instance
m_session = libssh2_session_init();
if (!m_session)
{
my_print(false, _T("Could not initialize SSH session"));
throw 0;
}
// Start it up. This will trade welcome banners, exchange keys,
// and setup crypto, compression, and MAC layers
rc = libssh2_session_startup(m_session, m_socket);
if (rc)
{
my_print(false, _T("Error when starting up SSH session: %d"), rc);
throw 0;
}
// At this point we haven't yet authenticated. The first thing to do
// is check the hostkey's fingerprint
const char *fingerprint = libssh2_hostkey_hash(m_session, LIBSSH2_HOSTKEY_HASH_MD5);
char check_fingerprint[48];
char hex_byte[3];
check_fingerprint[0] = '\0';
for (int i = 0; i < 16; i++)
{
sprintf(hex_byte, "%02X", (unsigned char)fingerprint[i]);
strcat(check_fingerprint, hex_byte);
if (i < 15)
{
strcat(check_fingerprint, ":");
}
}
my_print(true, _T("Server fingerprint:"));
my_print(true, _T("%hs"), check_fingerprint);
my_print(true, _T("Expected server fingerprint:"));
my_print(true, _T("%hs"), m_ssh_server_fingerprint.c_str());
if (strcmp(check_fingerprint, m_ssh_server_fingerprint.c_str()))
{
my_print(false, _T("unexpected server fingerprint"));
throw 0;
}
// check what authentication methods are available
char *userauthlist = libssh2_userauth_list(m_session, m_username.c_str(), m_username.length());
if (!strstr(userauthlist, "password"))
{
my_print(false, _T("password authentication not supported"));
throw 0;
}
if (libssh2_userauth_password(m_session, m_username.c_str(), m_password.c_str()))
{
my_print(false, _T("Authentication by password failed"));
throw 0;
}
m_connected = true;
}
catch (...)
{
TearDown();
}
// TODO: Don't throw 0. Throw and catch a std::exception.
// Maybe put the error message in the exception.
return IsConnected();
}
bool SSHSession::IsConnected(void)
{
// TODO: better check?
return m_connected;
}
LIBSSH2_SESSION* SSHSession::GetSession(void)
{
return m_session;
}
void SSHSession::TearDown(void)
{
if (m_session)
{
libssh2_session_disconnect(m_session, "Client disconnecting normally");
libssh2_session_free(m_session);
m_session = 0;
}
if (m_socket >= 0)
{
closesocket(m_socket);
m_socket = -1;
}
m_connected = false;
}
|