session.cc 10.7 KB
Newer Older
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437

#include "rsmcore/session.hh"
#include "radixbug/bug.hh"

#include <libssh/libssh.h>

#include <cassert>
#include <sstream>

#include <QByteArray>
#include <QString>

namespace rsm
{
void assert_ssh_session(ssh_session session, const std::string& error_message)
{
  if (session == nullptr)
  {
    throw std::runtime_error(error_message.c_str());
  }
}
void assert_ssh_channel(ssh_channel channel, const std::string& error_message)
{
  if (channel == nullptr)
  {
    throw std::runtime_error(error_message.c_str());
  }
}

class ChannelImpl
{
 public:
  ssh_channel channel = nullptr;
  explicit ChannelImpl(ssh_session session)
  {
    channel = ssh_channel_new(session);
  }
  ~ChannelImpl()
  {
    if (channel != nullptr)
    {
      ssh_channel_close(channel);
      ssh_channel_free(channel);
    }
  }
};  // class ChannelImpl

Channel::Channel()
{
  radix_tagged_line("Channel::Channel()");
  p = nullptr;
}
Channel::~Channel()
{
  radix_tagged_line("Channel::~Channel()");
  if (p != nullptr) delete p;
}

bool Channel::open()
{
  assert_ssh_channel(p->channel, "open() -- Channel is not allocated.");
  int rc = ssh_channel_open_session(p->channel);
  return (rc > -1);
}

bool Channel::isOpen() const
{
  // Perhaps we should just return false
  assert_ssh_channel(p->channel, "isOpen() -- Channel is not allocated.");
  int rc = ssh_channel_is_open(p->channel);
  return (rc != 0);
}

void Channel::close()
{
  if (p->channel != nullptr)
  {
    ssh_channel_close(p->channel);
    ssh_channel_free(p->channel);
    p->channel = nullptr;
  }
}

bool Channel::exec(QString command)
{
  assert_ssh_channel(p->channel, "exec() -- Channel is not allocated.");
  int rc = ssh_channel_request_exec(p->channel, command.toStdString().c_str());
  return !(rc < 0);
}

QString Channel::readExecOut()
{
  assert_ssh_channel(p->channel, "readExecOut() -- Channel is not allocated.");
  char buffer[256];
  int nbytes = ssh_channel_read(p->channel, buffer, sizeof(buffer), 0);
  QByteArray output_buffer;
  while (nbytes > 0)
  {
    output_buffer.append(buffer, nbytes);
    nbytes = ssh_channel_read(p->channel, buffer, sizeof(buffer), 0);
  }
  return output_buffer;
}
QString Channel::readExecErr()
{
  assert_ssh_channel(p->channel, "readExecErr() -- Channel is not allocated.");
  char buffer[256];
  int nbytes = ssh_channel_read(p->channel, buffer, sizeof(buffer), 1);
  QByteArray output_buffer;
  while (nbytes > 0)
  {
    output_buffer.append(buffer, nbytes);
    nbytes = ssh_channel_read(p->channel, buffer, sizeof(buffer), 1);
  }
  return output_buffer;
}

class SessionImpl
{
 public:
  ssh_session session = nullptr;
  SessionImpl()
  {
    session = ssh_new();
    assert_ssh_session(session, "Failed to allocate new ssh session.");
  }
  ~SessionImpl()
  {
    if (session != nullptr)
    {
      ssh_free(session);
      ssh_finalize();
    }
  }
};  // class SessionImpl

Session::Session() { p = new SessionImpl(); }

Session::Session(QString host)
{
  p = new SessionImpl();
  setHost(host);
}

Session::~Session()
{
  if (p != nullptr) delete p;
}

void Session::setHost(QString host)
{
  assert_ssh_session(p->session, "setHost() -- Session is not allocated.");
  ssh_options_set(p->session, SSH_OPTIONS_HOST, host.toStdString().c_str());
}

std::string Session::host() const
{
  // This may not be necessary
  assert_ssh_session(p->session, "host() -- Session is not allocated.");
  char* host_name;
  int rv = ssh_options_get(p->session, SSH_OPTIONS_HOST, &host_name);
  if (rv == SSH_OK)
  {
    std::string host = host_name;
    delete host_name;
    return host;
  }
  else
  {
    return std::string("");
  }
}
void Session::setLogVerbosity(SessionVerbosity level)
{
  assert_ssh_session(p->session,
                     "setLogVerbosity() -- Session is not allocated.");
  int verbosity = static_cast<int>(level);
  ssh_options_set(p->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
}
void Session::setPort(int port)
{
  assert_ssh_session(p->session, "setPort() -- Session is not allocated.");
  ssh_options_set(p->session, SSH_OPTIONS_PORT, &port);
}
void Session::setUser(QString name)
{
  assert_ssh_session(p->session, "setName() -- Session is not allocated.");
  ssh_options_set(p->session, SSH_OPTIONS_USER, name.toStdString().c_str());
}

std::string Session::user() const
{
  // This may not be necessary
  assert_ssh_session(p->session, "user() -- Session is not allocated.");
  char* user_name;
  int rv = ssh_options_get(p->session, SSH_OPTIONS_USER, &user_name);
  if (rv == SSH_OK)
  {
    std::string user = user_name;
    delete user_name;
    return user;
  }
  else
  {
    return std::string("");
  }
}

void Session::setProxyCommand(QString command)
{
  assert_ssh_session(p->session,
                     "setProxyCommand() -- Session is not allocated.");
  ssh_options_set(p->session, SSH_OPTIONS_PROXYCOMMAND,
                  command.toStdString().c_str());
}
bool Session::connect()
{
  assert_ssh_session(p->session, "connect() -- Session is not allocated.");
  radix_tagged_line("connect()");
  if (ssh_is_connected(p->session) == 1)
  {
    radix_tagged_line("session already connected. disconnectinng.");
    ssh_disconnect(p->session);
  }
  {
    radix_tagged_line("Attempting connection.");
    // attempt a connection
    int rc = ssh_connect(p->session);
    return (rc == SSH_OK);
  }
}

bool Session::isConnected() const
{
  assert_ssh_session(p->session, "isConnected() -- Session is not allocated.");
  return (ssh_is_connected(p->session) != 0);
}

std::string Session::getError() const
{
  assert_ssh_session(p->session, "getError() -- Session is not allocated.");
  return ssh_get_error(p->session);
}
bool Session::disconnect()
{
  assert_ssh_session(p->session, "disconnect() -- Session is not allocated.");
  radix_tagged_line("disconnect()");
  if (ssh_is_connected(p->session) != 0)
  {
    radix_tagged_line("Disconnecting session.");
    ssh_disconnect(p->session);
    // copy options in preparation for deletion
    ssh_session new_session = ssh_new();
    ssh_options_copy(p->session, &new_session);
    ssh_free(p->session);
    p->session = new_session;
  }
  return true;
}

QString Session::getHexa() const
{
  assert_ssh_session(p->session, "getHexa() -- Session is not allocated.");
  unsigned char* hash       = nullptr;
  ssh_key server_public_key = nullptr;
  char* hexa;
  size_t hlen;
  QString qhexa;
  int rc = ssh_get_server_publickey(p->session, &server_public_key);
  if (rc < 0)
  {
    return QString();
  }

  // Probably should remove hardwired HASH_SHA1
  rc = ssh_get_publickey_hash(server_public_key, SSH_PUBLICKEY_HASH_SHA1, &hash,
                              &hlen);
  ssh_key_free(server_public_key);
  if (rc < 0)
  {
    return QString();
  }

  hexa  = ssh_get_hexa(hash, hlen);
  qhexa = hexa;
  ssh_clean_pubkey_hash(&hash);
  ssh_string_free_char(hexa);
  return qhexa;
}

SessionHostState Session::verifyKnownHost() const
{
  assert_ssh_session(p->session,
                     "verifyKnownHost() -- Session is not allocated.");
  enum ssh_known_hosts_e state;
  unsigned char* hash       = nullptr;
  ssh_key server_public_key = nullptr;
  size_t hlen;
  QString qhexa;

  radix_tagged_line("verifyKnownHost()");
  int rc = ssh_get_server_publickey(p->session, &server_public_key);
  if (rc < 0)
  {
    return SessionHostState::ERROR;
  }

  rc = ssh_get_publickey_hash(server_public_key, SSH_PUBLICKEY_HASH_SHA1, &hash,
                              &hlen);
  ssh_clean_pubkey_hash(&hash);
  ssh_key_free(server_public_key);
  if (rc < 0)
  {
    return SessionHostState::ERROR;
  }

  state = ssh_session_is_known_server(p->session);
  return static_cast<SessionHostState>(state);
}

bool Session::updateKnownHosts()
{
  assert_ssh_session(p->session,
                     "updateKnownHosts() -- Session is not allocated.");
  radix_tagged_line("updateKnownHosts()");
  int rc = ssh_session_update_known_hosts(p->session);
  return (rc == SSH_OK);
}

int Session::authenticationMethodList() const
{
  assert_ssh_session(p->session,
                     "authenticationMethodList() -- Session is not allocated.");

  // get acceptable methods
  int method = ssh_userauth_list(p->session, nullptr);
  return method;
}

SessionAuthState Session::authenticateWithPublicKey()
{
  int rc = ssh_userauth_publickey_auto(p->session, nullptr, nullptr);
  return static_cast<SessionAuthState>(rc);
}

QString Session::keyboardInteractiveName() const
{
  assert_ssh_session(p->session,
                     "keyboardInteractiveName() -- Session is not allocated.");
  QString name = ssh_userauth_kbdint_getname(p->session);
  return name;
}

QString Session::keyboardInteractiveInstruction() const
{
  assert_ssh_session(
      p->session,
      "keyboardInteractiveInstruction() -- Session is not allocated.");
  QString instruction = ssh_userauth_kbdint_getinstruction(p->session);
  return instruction;
}

QStringList Session::keyboardInteractivePrompts() const
{
  int num_prompts = ssh_userauth_kbdint_getnprompts(p->session);
  // build list of prompts
  QStringList prompts;
  char echo;
  for (int i = 0; i < num_prompts; ++i)
  {
    QString prompt = ssh_userauth_kbdint_getprompt(
        p->session, static_cast<unsigned int>(i), &echo);
    radix_tagged_line(i << ". " << prompt.toStdString());
    if (prompt.isEmpty()) break;
    prompts << prompt;
  }
  return prompts;
}

SessionAuthState Session::authenticateWithPassword(QString pswd)
{
  assert_ssh_session(p->session,
                     "authenticateWithPassword() -- Session is not allocated.");
  radix_tagged_line("Authenticate with password.");
  int rc =
      ssh_userauth_password(p->session, nullptr, pswd.toStdString().c_str());
  return static_cast<SessionAuthState>(rc);
}

QString Session::loginIssueBanner() const
{
  char* banner = ssh_get_issue_banner(p->session);
  if (banner)
  {
    QString qbanner = banner;
    delete banner;
    return qbanner;
  }
  return QString("");
}

SessionAuthState Session::authenticatePrompts(QStringList responses)
{
  int err;
  radix_tagged_line("authenticatePrompts()");
  for (int i = 0; i < responses.size(); ++i)
  {
    radix_tagged_line("Setting response " << i);
    const char* answer = responses.at(i).toStdString().c_str();
    err                = ssh_userauth_kbdint_setanswer(p->session,
                                        static_cast<unsigned int>(i), answer);
    if (err < 0)
    {
      ssh_disconnect(p->session);
      return SessionAuthState::ERROR;
    }
  }
  // check status
  err = ssh_userauth_kbdint(p->session, nullptr, nullptr);

  SessionAuthState state = static_cast<SessionAuthState>(err);
  if (state == SessionAuthState::DENIED)
  {
    ssh_disconnect(p->session);
  }
  return state;
}

Channel Session::newChannel()
{
  assert_ssh_session(p->session, "newChannel() -- Session is not allocated.");
  Channel channel;
  channel.p = new ChannelImpl(p->session);
  return channel;
}

}  // namespace rsm