rsmportalexample.cc 8.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * @file:   mainwindow.cpp
 * @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
 *
 * Created on June 24, 2018, 10:52 AM
 */

#include "rsmportalexample.hh"
#include <QApplication>
#include <QGridLayout>
#include <QHeaderView>
12
#include <QInputDialog>
13
#include <QLabel>
14
#include <QMessageBox>
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

#include "radixbug/bug.hh"
using namespace rsm;

ExamplePortalWidget::ExamplePortalWidget(QWidget *parent)
    : QWidget(parent)
{
  QGridLayout *layout   = new QGridLayout(this);
  QLabel *hostLabel     = new QLabel("Host:", this);
  QLabel *portLabel     = new QLabel("Port:", this);
  QLabel *userNameLabel = new QLabel("Username:", this);

  mHostEdit            = new QLineEdit(this);
  mPortEdit            = new QLineEdit("22", this);
  mUserNameEdit        = new QLineEdit(this);
  mConnectButton       = new QPushButton("Connect", this);
  mCommandEdit         = new QLineEdit(this);
  mCommandSubmitButton = new QPushButton("Submit", this);

  mTextEdit = new QTextEdit(this);

  layout->addWidget(hostLabel, 0, 0);
  layout->addWidget(mHostEdit, 0, 1);
  layout->addWidget(portLabel, 1, 0);
  layout->addWidget(mPortEdit, 1, 1);
  layout->addWidget(userNameLabel, 2, 0);
  layout->addWidget(mUserNameEdit, 2, 1);
  layout->addWidget(mConnectButton, 3, 1);
  layout->addWidget(mCommandEdit, 4, 0, 1, 2);
  layout->addWidget(mCommandSubmitButton, 5, 1);

  layout->addWidget(mTextEdit, 6, 0, 2, 2);

  connect(mConnectButton, &QPushButton::pressed, this,
          &ExamplePortalWidget::connectToHost);
50
51
  connect(mCommandSubmitButton, &QPushButton::pressed, this,
          &ExamplePortalWidget::submitCommandToHost);
52
53
54
55
56
57

  mSession = new SessionController();
  connect(mSession, &SessionController::connectionFailed, this,
          &ExamplePortalWidget::connectionFailed);
  connect(mSession, &SessionController::connectionSuccessful, this,
          &ExamplePortalWidget::connectionSuccessful);
58
59
60
61
  connect(mSession, &SessionController::disconnectSuccessful, this,
          &ExamplePortalWidget::disconnectSuccessful);
  connect(mSession, &SessionController::interactiveAuthenticationRequested,
          this, &ExamplePortalWidget::interactiveAuthenticationRequested);
62
63
64
65
66
67
  connect(mSession, &SessionController::getServerPublicKeyFailed, this,
          &ExamplePortalWidget::getServerPublicKeyFailed);
  connect(mSession, &SessionController::hostUnknown, this,
          &ExamplePortalWidget::hostUnknown);
  connect(mSession, &SessionController::hostPublicKeyChanged, this,
          &ExamplePortalWidget::hostPublicKeyChanged);
68
69
  connect(mSession, &SessionController::verifyKnownHostSuccessful, this,
          &ExamplePortalWidget::verifyKnownHostSuccessful);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  connect(mSession, &SessionController::knownHostError, this,
          &ExamplePortalWidget::knownHostError);
  connect(mSession, &SessionController::authenticationError, this,
          &ExamplePortalWidget::authenticationError);
  connect(mSession, &SessionController::authenticationSucceeded, this,
          &ExamplePortalWidget::authenticationSucceeded);
  connect(mSession, &SessionController::passwordRequested, this,
          &ExamplePortalWidget::passwordRequested);
  connect(mSession, &SessionController::loginBannerIssued, this,
          &ExamplePortalWidget::loginBannerIssued);
  connect(mSession, &SessionController::execOutputReady, this,
          &ExamplePortalWidget::execOutputReady);
  connect(mSession, &SessionController::execFailed, this,
          &ExamplePortalWidget::execFailed);
  connect(mSession, &SessionController::execFinished, this,
          &ExamplePortalWidget::execFinished);
}

void ExamplePortalWidget::connectToHost()
{
  radix_tagged_line("Host:" << mHostEdit->text().toStdString());

92
93
94
95
96
97
98
99
100
101
102
103
  if (mConnectButton->text().compare("Disconnect") == 0)
  {
    mSession->disconnect();
    mConnectButton->setText("Connect");
  }
  else
  {
    mSession->setHost(mHostEdit->text());
    mSession->setPort(mPortEdit->text().toInt());
    mSession->setUser(mUserNameEdit->text());
    mSession->connect();
  }
104
105
}

106
107
108
109
110
111
void ExamplePortalWidget::submitCommandToHost()
{
  radix_tagged_line("submitCommandToHost()");
  QString command = mCommandEdit->text();
  mSession->requestExec(command);
}
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

void ExamplePortalWidget::slotExecOutputReady()
{
  QByteArray output = mSession->readExecOutput();
  mTextEdit->append(output);
}

void ExamplePortalWidget::connectionFailed(QString message)
{
  mTextEdit->append("Connection failed.\n");
  mTextEdit->append(message);
}

void ExamplePortalWidget::connectionSuccessful()
{
  mTextEdit->append("Connection successful.\nVerifying host...\n");
  mSession->verifyKnownHost();
}

131
132
133
134
135
136
void ExamplePortalWidget::disconnectSuccessful()
{
  mTextEdit->append("Disconnected.\n");
}

void ExamplePortalWidget::verifyKnownHostSuccessful()
137
{
138
  mTextEdit->append("Verification successful.\nAuthenticating...\n");
139
140
141
  mSession->authenticate();
}

142
143
144
145
146
147
148
149
150
151
152
153
void ExamplePortalWidget::interactiveAuthenticationRequested(
    QString instruction, QString name, QStringList prompts)
{
  mTextEdit->append("Interactive authentication requested.");
  mTextEdit->append("Instruction: ");
  mTextEdit->append(instruction);
  mTextEdit->append("Name:");
  mTextEdit->append(name);
  for (int i = 0; i < prompts.size(); ++i)
  {
    mTextEdit->append(prompts.at(i));
  }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

  if (!prompts.isEmpty())
  {
    QString text = QInputDialog::getText(this, "Authentication", prompts.at(0),
                                         QLineEdit::Password);
    if (text.isEmpty())
    {
      mSession->disconnect();
    }
    else
    {
      QStringList responses;
      responses << text;
      mSession->authenticatePrompts(responses);
    }
  }
  else
  {
    radix_tagged_line("Reponsing with empty handshake.");
    QStringList empty;
    mSession->authenticatePrompts(empty);
  }
176
177
}

178
179
180
181
182
183
184
185
void ExamplePortalWidget::getServerPublicKeyFailed()
{
  mTextEdit->append("Retrieval of host's public key failed.\n");
  mSession->disconnect();
}

void ExamplePortalWidget::hostUnknown(QString host_hash)
{
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  int ret = QMessageBox::warning(
      this, tr("Host Unknown"),
      QString("The remote host is unknown.\n")
          .append("Are you sure you want to continue connecting?\n")
          .append("Remote host key:")
          .append(host_hash),
      QMessageBox::Yes | QMessageBox::Cancel);
  if (ret == QMessageBox::Yes)
  {
    mSession->acceptHostPublicKeyUpdate();
  }
  else
  {
    mSession->disconnect();
  }
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
}

void ExamplePortalWidget::hostPublicKeyChanged(QString host_hash)
{
  mTextEdit->append("Host key has changed.\nChanged key:");
  mTextEdit->append(host_hash);
  mTextEdit->append("This could be a man-in-the-middle attach.\n");
  mSession->disconnect();
}

void ExamplePortalWidget::hostPublicKeyUnavailable()
{
  mTextEdit->append("Host key is unavaible.\n");
  mSession->disconnect();
}

void ExamplePortalWidget::knownHostError(QString message)
{
  mTextEdit->append("Error verifying known host.\n");
  mTextEdit->append(message);
  mTextEdit->append("\n");

  mSession->disconnect();
}

void ExamplePortalWidget::authenticationError(QString message)
{
  mTextEdit->append("Authentication error.\n");
  mTextEdit->append(message);
  mTextEdit->append("\n");
  mSession->disconnect();
}

void ExamplePortalWidget::authenticationSucceeded()
{
  mTextEdit->append("Authentication succeeded.");
  mTextEdit->append("\n");
238
  mConnectButton->setText("Disconnect");
239
240
241
242
243
244
}

void ExamplePortalWidget::passwordRequested()
{
  mTextEdit->append("Password requested.");
  mTextEdit->append("\n");
245
246
247
248
249
250
251
252
253
254
  QString text = QInputDialog::getText(this, "Authentication",
                                       "Password:", QLineEdit::Password);
  if (text.isEmpty())
  {
    mSession->disconnect();
  }
  else
  {
    mSession->authenticateWithPassword(text);
  }
255
256
257
258
}

void ExamplePortalWidget::loginBannerIssued(QString message)
{
259
260
261
262
263
264
  int ret = QMessageBox::warning(this, tr("Host Login Banner"), message,
                                 QMessageBox::Ok | QMessageBox::Cancel);
  if (ret != QMessageBox::Ok)
  {
    mSession->disconnect();
  }
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
  mTextEdit->append(message);
  mTextEdit->append("\n");
}

void ExamplePortalWidget::execOutputReady()
{
  mTextEdit->append(mSession->readExecOutput());
}

void ExamplePortalWidget::execFailed(QString message)
{
  mTextEdit->append(message);
  mTextEdit->append("\n");
}

void ExamplePortalWidget::execFinished()
{
  mTextEdit->append("Execution finished.\n");
}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
  setGeometry(400, 250, 542, 390);

  ExamplePortalWidget *portalView = new ExamplePortalWidget(this);
  setCentralWidget(portalView);
}

MainWindow::~MainWindow() {}

/******************************************************************************/
/******************************** MAIN PROGRAM ********************************/
/******************************************************************************/

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  MainWindow mainWindow;
  mainWindow.show();
  mainWindow.raise();

  return app.exec();
}