-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshconnection.cpp
More file actions
386 lines (334 loc) · 12.5 KB
/
sshconnection.cpp
File metadata and controls
386 lines (334 loc) · 12.5 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* ----------------------------------------------------------------------------
* SFTP file manager for QT
*
* Secure file manager applet plug-in for QT
*
* Copyright (C) 2013 Ibrahim Can Yuce <canyuce[[at]]gmail.com>
*
* ----------------------------------------------------------------------------
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*/
#include "sshconnection.h"
#include "mainwindow.h"
#include <errno.h>
#include <iostream>
#include <QInputDialog>
#include <QMessageBox>
SSHConnection::SSHConnection (
const QString host,
const QString password,
const quint16 port,
const QString userName)
{
this->host = host;
this->port = (unsigned short)port;
this->password = password;
this->userName = userName;
this->session = NULL;
this->options = NULL;
this->sshDir = "%s/.ssh/";
}
SSHConnection::~SSHConnection ()
{ }
void
SSHConnection::disconnect ()
{
if (session) {
ssh_disconnect(session);
ssh_finalize();
}
}
int
SSHConnection::connect ()
{
MainWindow::logScreen->appendText(QString(
"SSH connection attempt to ").append(
userName)
.append("@").append(host).append(" port:")
.append(QString::number(port)));
if (setOptions()) {
MainWindow::logScreen->appendText(QString(
"SSH connection attempt failed: setOptions()"));
return 1;
}
if (connectToServer()) {
MainWindow::logScreen->appendText(QString(
"SSH connection attempt failed: connectToServer()"));
return 1;
}
if (authenticateToServer()) {
MainWindow::logScreen->appendText(QString(
"SSH connection attempt failed: authenticateToServer()"));
return 1;
}
return 0;
}
int
SSHConnection::executeCommand (
const char* command, char* output)
{
ssize_t len;
CHANNEL* channel;
if (this->session == NULL) {
MainWindow::logScreen->appendText(
"SSH error: Command execution operation failed. "
"SSH session has not started yet.");
return -1;
}
if (!(channel = channel_new(session))) {
MainWindow::logScreen->appendText(QString(
"Error opening channel : %1").arg(
ssh_get_error(session)));
return -1;
}
if (channel_open_session(channel)) {
MainWindow::logScreen->appendText(QString(
"Error opening channel : %1").arg(
ssh_get_error(session)));
return -1;
}
if (channel_request_exec(channel, command)) {
MainWindow::logScreen->appendText(QString(
"Error executing \"%1\" : %2").
arg(command, ssh_get_error(session)));
return -1;
}
/* TODO
* Reads till EOF; what happens if the command does not put an EOF(Is this a valid case?)
*/
len = channel_read(channel, output, BUFFSIZE, 0);
output[len] = '\0';
channel_free(channel);
return len;
}
int
SSHConnection::setOptions ()
{
options = ssh_options_new();
if (ssh_options_set_username(options,
userName.toStdString().c_str()) < 0)
{
MainWindow::logScreen->appendText("ssh_options_set_username failed!");
ssh_options_free(options);
return 1;
}
if (ssh_options_set_host(options, host.toStdString().c_str()) < 0) {
MainWindow::logScreen->appendText("ssh_options_set_host failed!");
ssh_options_free(options);
return 1;
}
if (ssh_options_set_port(options, port) < 0) {
MainWindow::logScreen->appendText("ssh_options_set_port failed!");
ssh_options_free(options);
return 1;
}
if (ssh_options_set_ssh_dir(options, this->sshDir) < 0) {
MainWindow::logScreen->appendText("ssh_options_set_ssh_dir failed!");
ssh_options_free(options);
return 1;
}
return 0;
}
int
SSHConnection::connectToServer ()
{
unsigned char* hash = NULL;
int hlen, state;
char* hexa;
this->session = ssh_new();
ssh_set_options(session, options);
if (ssh_connect(session)) {
MainWindow::logScreen->appendText(QString(
"Connection attempt to server failed: ")
.append(ssh_get_error(session)));
disconnect();
return 1;
}
state = ssh_is_server_known(session);
hlen = ssh_get_pubkey_hash(session, &hash);
if (hlen < 0) {
disconnect();
return 1;
}
switch (state) {
case SSH_SERVER_KNOWN_OK:
break; /* ok */
case SSH_SERVER_KNOWN_CHANGED:
MainWindow::logScreen->appendText(QString("Host key for server changed"));
free(hash);
MainWindow::logScreen->appendText(
"For security reason, connection will be stopped");
disconnect();
return 1;
case SSH_SERVER_FOUND_OTHER:
MainWindow::logScreen->appendText(
"The host key for this server was not found but an other type of key exists.");
MainWindow::logScreen->appendText(
"An attacker might change the default server key to confuse your client into thinking the key does not exist.");
MainWindow::logScreen->appendText(
"For security reason, connection will be stopped.");
disconnect();
return 1;
case SSH_SERVER_FILE_NOT_FOUND:
MainWindow::logScreen->appendText(QString(
"Could not find known hosts' file. If you accept the host key here ")
.append(
"the file will be automatically created."));
// fallback to SSH_SERVER_NOT_KNOWN behaviour
case SSH_SERVER_NOT_KNOWN:
hexa = ssh_get_hexa(hash, hlen);
if (QMessageBox::question(0, "SSH: Unknown Server",
QString(
"The server is unknown. Do you trust the host key ?\n\n")
.
append("Public key hash: %1\n").arg(QString(
hexa)),
QMessageBox::Cancel | QMessageBox::No |
QMessageBox::Yes) == QMessageBox::Yes)
{
if (QMessageBox::question(0, "SSH: New Key",
QString(
"This new key will be written on disk for further usage. Do you agree ?\n"),
QMessageBox::Cancel | QMessageBox::No |
QMessageBox::Yes) == QMessageBox::Yes)
{
if (ssh_write_knownhost(session) < 0) {
free(hash);
MainWindow::logScreen->appendText(QString(
"ssh_write_knownhost : error )")
.append(strerror(errno)));
disconnect();
return 1;
} else
MainWindow::logScreen->appendText(
"Host public key was written to disk successfully!");
} else
MainWindow::logScreen->appendText(
"Host public key won't be written to disk, discarded.");
} else {
free(hexa);
MainWindow::logScreen->appendText(
"User does not trust the host key, connection will discontinue.");
disconnect();
return 1;
}
break;
case SSH_SERVER_ERROR:
free(hash);
MainWindow::logScreen->appendText(QString("error : ").append(
ssh_get_error(session)));
disconnect();
return 1;
}
free(hash);
return 0;
}
int
SSHConnection::authenticateToServer ()
{
int auth;
char* banner;
auth = ssh_userauth_autopubkey(session, NULL);
if (auth == SSH_AUTH_ERROR) {
MainWindow::logScreen->appendText(QString(
"Authenticating with pubkey: %1")
.arg(ssh_get_error(session)));
disconnect();
return 1;
}
banner = ssh_get_issue_banner(session);
if (banner) {
MainWindow::logScreen->appendText(banner);
free(banner);
}
if (auth != SSH_AUTH_SUCCESS) {
auth = authKbdInteractive();
if (auth == SSH_AUTH_ERROR) {
MainWindow::logScreen->appendText(QString(
"authenticating with keyb-interactive: %1")
.
arg(ssh_get_error(session)));
disconnect();
return 1;
}
}
if (auth != SSH_AUTH_SUCCESS) {
if (ssh_userauth_password(session, NULL,
password.toStdString().c_str()) !=
SSH_AUTH_SUCCESS)
{
MainWindow::logScreen->appendText(QString(
"Authentication failed: %1").
arg(ssh_get_error(session)));
disconnect();
return 1;
}
// memset(password, 0, strlen(password) );
}
MainWindow::logScreen->appendText(QString("SSH Authentication succeeded!"));
return 0;
}
int
SSHConnection::authKbdInteractive ()
{
const char* name, * instruction, * prompt;
const char* buffer;
int i, n;
char echo;
int err = ssh_userauth_kbdint(session, NULL, NULL);
while (err == SSH_AUTH_INFO) {
name = ssh_userauth_kbdint_getname(session);
instruction = ssh_userauth_kbdint_getinstruction(session);
n = ssh_userauth_kbdint_getnprompts(session);
MainWindow::logScreen->appendText(QString(
"SSH Authorization : Number of prompts = %1")
.arg(n));
if (strlen(name) > 0)
MainWindow::logScreen->appendText(QString("name %1").arg(name));
if (strlen(instruction) > 0)
MainWindow::logScreen->appendText(QString("instruction %1").arg(
instruction));
for (i = 0 ; i < n ; ++i) {
prompt = ssh_userauth_kbdint_getprompt(session, i, &echo);
if (echo) {
bool ok;
QString text = QInputDialog::getText(0, QString("Prompt"),
QString("%1").arg(
prompt),
QLineEdit::Normal,
"", &ok);
/* TODO
* Buffer length check
*/
if (ok)
buffer = text.toStdString().c_str();
if (ssh_userauth_kbdint_setanswer(session, i, buffer) < 0) {
return SSH_AUTH_ERROR;
}
} else {
if (ssh_userauth_kbdint_setanswer(session, i,
password.toStdString().c_str())
< 0)
{
return SSH_AUTH_ERROR;
}
}
}
err = ssh_userauth_kbdint(session, NULL, NULL);
}
return err;
}