summaryrefslogtreecommitdiffstats
path: root/src/fbgui/fbgui.cpp
blob: fcc652c751bae5ae71cf243b7787791d85f3c79c (plain) (blame)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include "fbgui.h"
#include "sysinfo.h"
#include "downloadmanager.h"
#include "javascriptinterface.h"


#include <log4cxx/logger.h>
#include "qlog4cxx.h"

using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr coreLogger(Logger::getLogger("fbgui.core"));


#include <iostream>
#include <QThread>
#include <QtWebKit>

QThread dmThread;
QString logFilePath("");
QString ipConfigFilePath("");
QString binPath("");
QUrl baseURL("");
QString downloadPath("");
int updateInterval = -1;
QString fileToTriggerURL("");
QString serialLocation("");
QString sessionID("");
bool sslSupport;
int debugMode = -1;

//-------------------------------------------------------------------------------------------
/**
 * A constructor.
 *
 * The constructor of the fbgui class. It initializes the main objects
 * which are needed while the program is running.
 * The appearance of the webView is here also defined.
 *
 * @see JavascriptInterface
 * @see DownloadManager
 */
fbgui::fbgui() {
}
fbgui::~fbgui() {
   dmThread.quit();
}



/**
 * init function.
 */
void fbgui::init() {
	// start fbgui
	LOG4CXX_DEBUG(coreLogger, "Initializing fbgui...");

	if(sslSupport)
		LOG4CXX_DEBUG(coreLogger, "SSL enabled.");

	  _watcher = new QFileSystemWatcher(this);

	setupLayout();
	createActions();

	// initialize javascript interface
	JavascriptInterface* jsi = new JavascriptInterface(
			_webView->page()->mainFrame());
	QObject::connect(jsi, SIGNAL(quitFbgui()), this, SLOT(close()));
	QObject::connect(jsi, SIGNAL(shutDownClient()), this,
			SLOT(performShutDown()));
	QObject::connect(_webView->page()->mainFrame(), SIGNAL(
			javaScriptWindowObjectCleared()), jsi, SLOT(attachToDOM()));

	// initialize download manager
	DownloadManager* dm = new DownloadManager();
	QObject::connect(dm, SIGNAL(downloadInfo(const QString&, const double&)),
			jsi, SLOT(downloadInfo(const QString&, const double&)));
	QObject::connect(dm, SIGNAL(notify(const QString&)), jsi,
			SLOT(notify(const QString&)));
	QObject::connect(jsi, SIGNAL(requestFile(const QString&)), dm,
			SLOT(downloadFile(const QString&)));
	QObject::connect(
			dm,
			SIGNAL(updateProgress(const int&, const double&, const QString&)),
			jsi,
			SLOT(updateProgressBar(const int&, const double&, const QString&)));
	QObject::connect(dm, SIGNAL(downloadQueueEmpty()), jsi, SLOT(
			callbackOnFinished()));
	QObject::connect(dm, SIGNAL(downloadQueueEmpty()), this, SLOT(loadSystem()));

	// move download manager to its own thread
	dm->moveToThread(&dmThread);
	dmThread.start();

	// show "waiting for internet" page until triggered.
	if (debugMode > -1) {
		_webView->load(QUrl("qrc:/html/preload-debug.html"));
	} else {
		_webView->load(QUrl("qrc:/html/preload.html"));
	}

	// watcher is not needed anymore since we guarantee internet connection with the networkDiscovery.
	// start watching for fileToTriggerURL
	//watchForTrigger();
	loadURL();

	// set properties
	setWindowTitle("fbgui");
	setAttribute(Qt::WA_QuitOnClose, true);
	setWindowFlags(Qt::FramelessWindowHint);
	showFullScreen();
	this->show();
}
//-------------------------------------------------------------------------------------------
//                               Layout / actions setup
//-------------------------------------------------------------------------------------------
/**
 * This method sets the used Layout.
 *
 * This method sets the used Layout. Possible layout are:
 *  - browser mode: only the browser is visible
 *  - debug mode: the screen is divided into the browser and a debug
 *      out console
 */
void fbgui::setupLayout() {
   // setup layout of the gui: debug split or browser
   _webView = new QWebView(this);
   if (debugMode == 1) {
      // split main window in browser & debug console
      createDebugConsole();
      _splitter = new QSplitter(Qt::Vertical, this);
      _splitter->addWidget(_webView);
      _splitter->addWidget(_debugConsole);
      setCentralWidget(_splitter);
   } else
      setCentralWidget(_webView);
}
//-------------------------------------------------------------------------------------------
/**
 * This method enables a shortcut for closing the program.
 * The shortcut itself is not configurable: CTRL + X
 */
void fbgui::createActions() {
   _quit = new QAction(tr("&quit"), this);
   _quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_X));
   this->addAction(_quit);
   connect(_quit, SIGNAL(triggered()), this, SLOT(close()));
}
//-------------------------------------------------------------------------------------------
//                                 File system watching
//-------------------------------------------------------------------------------------------
/**
 * This method sets a "watchdog" to a special file.
 *
 * This method sets a "watchdog" to a special file. If needed it creates the
 * file which it has to watch over. It than connects a QFileSystemWatcher with
 * this file. If changes happen to this file, the
 * fbgui::checkForTrigger(const QString& dirname) method will be called.
 *
 */
void fbgui::watchForTrigger() {
   // check if fileToTriggerURL already exists
   QFile file(fileToTriggerURL);
   if (file.exists()) {
      LOG4CXX_DEBUG(coreLogger, "[watcher] " << fileToTriggerURL << " found.");
      // try to load URL
      loadURL();
   } else {
      // create it
      if (file.open(QIODevice::WriteOnly)) {
         LOG4CXX_DEBUG(coreLogger, "Created: " << fileToTriggerURL);
         file.close();
      } else {
         LOG4CXX_DEBUG(coreLogger, "Creation of " << fileToTriggerURL << " failed!");
         LOG4CXX_DEBUG(coreLogger, "Exiting in 5 seconds...");
         QTimer::singleShot(5000, this, SLOT(close()));
      }
   }
   // watch the path to trigger file
   LOG4CXX_DEBUG(coreLogger, "[watcher] Watching " << fileToTriggerURL);
   _watcher->addPath(fileToTriggerURL);
   //_watcher = new QFileSystemWatcher(QStringList(fileToTriggerURL, logFilePath), this);
QObject::connect(_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(prepareURLLoad(const QString&)));

}
//-------------------------------------------------------------------------------------------
/**
 * This method checks if the trigger was valid.
 *
 * This method checks if the trigger was valid. If yes,
 * we have received an IP Address an can load the main screen.
 * If not, something some error happened.
 *
 * @see fbgui::checkHost()
 * @see fbgui::loadURL()
 */
void fbgui::prepareURLLoad(const QString& fileName) {
   if (fileName == fileToTriggerURL) {
      LOG4CXX_DEBUG(coreLogger, "[watcher] " << fileToTriggerURL << " changed!");
      // disconnect _watcher, his job is done
      LOG4CXX_DEBUG(coreLogger, "[watcher] disconnected.");
      _watcher->disconnect(this);
      _watcher->deleteLater();
      // try to load URL
      loadURL();
   }
}
//-------------------------------------------------------------------------------------------
//                            Preparations for URL load
//-------------------------------------------------------------------------------------------
/**
 * This method checks the existance of the host.
 *
 * This method checks if the host exists / can be found.
 * The host is from the URL given through the configuration.
 */
bool fbgui::checkHost() const {
   QHostInfo hostInfo = QHostInfo::fromName(baseURL.host());
   if (hostInfo.error() != QHostInfo::NoError) {
      LOG4CXX_DEBUG(coreLogger, "Lookup of " << baseURL.host() << "failed.");
      LOG4CXX_DEBUG(coreLogger, "Host can not be reached.");
      return false;
   } else {
      LOG4CXX_DEBUG(coreLogger, "Lookup of " << baseURL.host() << " succeeded.");
      return true;
   }
}
//-------------------------------------------------------------------------------------------
/**
 * This method tries loads the URL.
 *
 * This method loads the main screen via an POST request. If also disconnects the watcher
 * of the file, (Watcher is set in the fbgui::watchForTrigger() method).
 * and generates the POST data body.
 *
 * @see fbgui::watchForTrigger()
 * @see fbgui::generatePOSTData()
 */
void fbgui::loadURL() {
   if (checkHost()) {
      LOG4CXX_DEBUG(coreLogger, "Loading URL: " << baseURL.toString() << " ...");

      // Generate POST identification data needed by PBS.
      QByteArray postData = generatePOSTData();
      QNetworkRequest req(baseURL);

      //Connect webViews NetworkAccessManager to SSLErrorHandler SLOT
      QObject::connect(_webView->page()->networkAccessManager(),
              SIGNAL(finished(QNetworkReply*)),
              this,
              SLOT(errorHandler(QNetworkReply*)));

      //Connect webViews NetworkAccessManager to ErrorHandler SLOT
      QObject::connect(_webView->page()->networkAccessManager(),
              SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError> & )),
              this,
              SLOT(sslErrorHandler(QNetworkReply*, const QList<QSslError> & )));

      // show cursor again since user is about to interact.
      //QWSServer::instance()->setCursorVisible(true); //TODO: ?enabled in original
      req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
      QObject::connect(_webView, SIGNAL(loadFinished(bool)), this, SLOT(loadURLDone(bool)));
      _webView->load(req, QNetworkAccessManager::PostOperation, postData);
   }
   // TODO: error page if no host.
}

void fbgui::loadURLDone(bool success) {
   // done contains the success of the loading: false / true
   if (!success) {
      LOG4CXX_DEBUG(coreLogger, "Loading failed. URL: "  << _webView->url().toString());
      LOG4CXX_DEBUG(coreLogger, "You can quit with CTRL + X ...");
      // TODO handle failure properly...
   } else {
      LOG4CXX_DEBUG(coreLogger, "Loaded URL: " << _webView->url().toString());
   }
}


//Handles QNetworkReply SSL Errors
void fbgui::sslErrorHandler(QNetworkReply* reply, const QList<QSslError> & errlist)
{

  foreach (QSslError err, errlist)
	LOG4CXX_DEBUG(coreLogger, "SSL Error: " << err.errorString());

   reply->ignoreSslErrors();
}

//Handles QNetworkReply Errors
void fbgui::errorHandler(QNetworkReply* reply)
{

	LOG4CXX_DEBUG(coreLogger, "HTTP Error: " << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString());


}


//-------------------------------------------------------------------------------------------
/**
 * This method generates the POST data body.
 *
 * This method generates the POST data body. The body contains the
 * MAC address, an hardwarehash and a specific serial number.
 * The hardwarehash is a MD5 hash over the MAC address and the
 * mainboard serial number.
 * The specific serial number is set at the creation of the usb boot stick.
 * This file has to be present on the directory specified in
 * the configuration for this to work.
 *
 * @see SysInfo::getMACAddress()
 * @see SysInfo::getMainboardSerial()
 */
QByteArray fbgui::generatePOSTData() {
   LOG4CXX_DEBUG(coreLogger, "Generating POST data...");
   // use MAC address as base data
   SysInfo si;
   QByteArray data(si.getInfo("mac").toUtf8());
   // append mainboard serial to the mac address for more unique hardwarehash
   QByteArray mbserial(si.getInfo("mbserial").toUtf8());
   if (!mbserial.isEmpty())
      data.append(mbserial);
   else {
      LOG4CXX_DEBUG(coreLogger, "Mainboard serial was empty. Not appending to base hash data.");
   }
   LOG4CXX_DEBUG(coreLogger, "[post] Hashing: " << data);
   // generate MD5 hash of data
   QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
   LOG4CXX_DEBUG(coreLogger, "[post] MD5 Hash: " << hash.toHex());

   // fetch serial number from usb
   QByteArray serial;
   QFile file(serialLocation);
   if (!file.open(QIODevice::ReadOnly)) {
      LOG4CXX_DEBUG(coreLogger, "[post] No such file: " << file.fileName());
   }
   // everything ok, read data
   serial = file.readAll();
   file.close();
   serial.chop(1); // chop EOF
   LOG4CXX_DEBUG(coreLogger, "[post] Serial number is: " << serial);

   // construct final byte array
   QByteArray postData("mac=");
   postData.append(si.getInfo("mac"));
   postData.append("&hardwarehash=" + hash.toHex());
   postData.append("&serialnumber=" + serial);
   LOG4CXX_DEBUG(coreLogger, "[post] POST data: " << postData);
   return postData;
}

//-------------------------------------------------------------------------------------------
//                         Shutdown / Reboot of the client
//-------------------------------------------------------------------------------------------
// TODO One function for reboot and shutdown, with parameter for the action.
// for example: doSystemCall(_REBOOT_);
/**
 * This method performs the shutdown of the client.
 *
 * This method performs the shutdown of the client. It is triggered by the
 * JavascriptInterface::shutDownClient() signal which will be emited in the
 * JavascriptInterface::shutDown() method.
 * This method writes the character 'o' in /proc/sysrq-trigger
 * which will shutdown the computer immediatly.
 * (See linux magic keys)
 *
 * @see JavascriptInterface::shutDownClient()
 * @see JavascriptInterface::shutDown()
 */
void fbgui::performShutDown() {
   QFile file("/proc/sysrq-trigger");
   if (file.open(QIODevice::WriteOnly)) {
      file.write("o");
      file.close();
   } else {
      LOG4CXX_DEBUG(coreLogger, "Could not open /proc/sysrq-trigger");
   }
}
//-------------------------------------------------------------------------------------------
/**
 * This method performs the reboot of the client.
 *
 * This method performs the reboot of the client. It is triggered by the
 * JavascriptInterface::rebootClient() signal which will be emited in the
 * JavascriptInterface::reboot() method.
 * This method writes the character 'b' in /proc/sysrq-trigger
 * which will shutdown the computer immediatly.
 * (See linux magic keys)
 *
 * @see JavascriptInterface::rebootClient()
 * @see JavascriptInterface::reboot()
 */
void fbgui::performReboot() {
   QFile file("/proc/sysrq-trigger");
   if (file.open(QIODevice::WriteOnly)) {
      file.write("b");
      file.close();
   } else {
      LOG4CXX_DEBUG(coreLogger, "Could not open /proc/sysrq-trigger");
   }
}
//-------------------------------------------------------------------------------------------
//                   Preparing Kernel Switch per kexec (initiating Stage 3)
//-------------------------------------------------------------------------------------------
void fbgui::loadSystem() {
   //show loading system page.
   //_webView->disconnect(this);
   //QObject::connect(_webView, SIGNAL(loadFinished(bool)), this, SLOT(prepareKexec()));
   _webView->load(QUrl("qrc:/html/loadsystem.html"));
   QTimer::singleShot(1000, this, SLOT(prepareKexec()));
}
//-------------------------------------------------------------------------------------------
/**
 * This method prepares kexec.
 *
 * The kernel command line file that should have been downloaded from the Preboot-Server
 * and the ip config file (created by udhcpc) are merged into the final completed KCL.
 *
 * A process is then started to load the kernel, initramfs and kcl into kexec.
 * The process tries to execute kexec -l with these parameters.
 *
 * If this succeeds, runKexec() is called
 *
 * @see fbgui::runKexec()
 *
 */
void fbgui::prepareKexec() {

   LOG4CXX_DEBUG(coreLogger, "Preparing kexec ...");
   // try to read KCL file that was downloaded.
   QFile file(downloadPath + "/kcl");
   if (!file.open(QIODevice::ReadOnly)) {
      LOG4CXX_DEBUG(coreLogger, "No such file: " << file.fileName());
   }
   // everything ok, read data.
   QString kcl = file.readAll();
   file.close();
   LOG4CXX_DEBUG(coreLogger, "KCL from PBS: " << kcl);

   // try to read ipconfig file generated by udhcpc.
   file.setFileName("/tmp/ip_config_fbgui");
   if (!file.open(QIODevice::ReadOnly)) {
      LOG4CXX_DEBUG(coreLogger, "No such file: " << file.fileName());
   }
   // everything ok, read data.
   QString ipConfig = file.readAll();
   file.close();
   LOG4CXX_DEBUG(coreLogger, "IP config: " << ipConfig);

   // append ipConfig
   kcl.append(" ip=");
   kcl.append(ipConfig);
   LOG4CXX_DEBUG(coreLogger, "Complete KCL: " << kcl);

   // load the kernel + initramfs + append of kcl into kexec.
   QProcess *process = new QProcess(this);
   QString cmdline = "kexec -l " + downloadPath.toUtf8() + "/kernel --initrd="
         + downloadPath.toUtf8() + "/initramfs --append=\"" + kcl.toUtf8()
         + "\"";
   LOG4CXX_DEBUG(coreLogger, "kexec cmdline: " << cmdline);
   process->start(cmdline);
   bool ret = process->waitForFinished();
   if (!ret) {
      LOG4CXX_DEBUG(coreLogger, "Failed to execute: " << cmdline);
      LOG4CXX_DEBUG(coreLogger, "Exiting in 5 seconds...");
      QTimer::singleShot(5000, this, SLOT(close()));
   } else {
      LOG4CXX_DEBUG(coreLogger, "Kexec load was successfull.");
      if (debugMode < 0) {
         // if process successfully finished, try to run kexec -e
         runKexec();
      } else {
         LOG4CXX_DEBUG(coreLogger, "Skipping execution of kexec - open debug shell.");
         LOG4CXX_DEBUG(coreLogger, "To start the system execute \"kexec -e\" in your shell.");
         close();
      }
   }
}
//-------------------------------------------------------------------------------------------
/**
 * This method tries to execute: kexec -e
 *
 * This method tries to execute: kexec -e
 *
 */
void fbgui::runKexec() {
   QProcess *process = new QProcess(this);
   process->start("kexec -e");
   if (!process->waitForStarted()) {
      LOG4CXX_DEBUG(coreLogger, "Failed to execute: kexec -e");
      LOG4CXX_DEBUG(coreLogger, "Exiting in 5 seconds...");
      QTimer::singleShot(5000, this, SLOT(close()));
      //TODO: Handle failure properly...
   }
}
//-------------------------------------------------------------------------------------------
//                              Debug console setup / control
//-------------------------------------------------------------------------------------------
/**
 * This method creates a debug console as a widget.
 *
 * It is basicly a QTextEdit widget as provided by QT's Framework.
 * An action to toggle this widget is implemented (CTRL + D).
 *
 * @see fbgui::toggleDebugConsole()
 */
void fbgui::createDebugConsole() {
   // create the debug console widget
   _debugConsole = new QTextEdit(this);
   _debugConsole->setWindowFlags(Qt::FramelessWindowHint);
   // fanciness
   QPalette pal;
   pal.setColor(QPalette::Base, Qt::black);
   _debugConsole->setPalette(pal);
   _debugConsole->setTextColor(Qt::white);
   // CTRL + D  toggles debug window
   _toggleDebugConsole = new QAction(tr("&toggleDebug"), this);
   _toggleDebugConsole->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_D));
   addAction(_toggleDebugConsole);
   connect(_toggleDebugConsole, SIGNAL(triggered()),
           this, SLOT(toggleDebugConsole()));

   // read from log file
   _logFile = new QFile(logFilePath);
   _logFileIn = new QTextStream(_logFile);

   if (!_logFile->open(QFile::ReadOnly | QFile::Text)) {
      //do error
   }
   _debugConsole->setPlainText(_logFileIn->readAll());
   _debugConsole->moveCursor(QTextCursor::End);
   LOG4CXX_DEBUG(coreLogger, "Log file opened.");
   // watch log file
   _watcher->addPath(logFilePath);
   QObject::connect(_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(refreshDebugConsole(const QString&)));

}
//-------------------------------------------------------------------------------------------
void fbgui::refreshDebugConsole(const QString& fileName) {
   if (fileName == logFilePath) {
      while (!_logFileIn->atEnd()) {
         _debugConsole->append(_logFileIn->readLine());
      }
      _debugConsole->moveCursor(QTextCursor::End);
   }
}
//-------------------------------------------------------------------------------------------
/**
 * This method toggles the debug console.
 *
 * Toggle the visibility of the debug console if the action _toggleDebugConsole is triggered.
 *
 * @see fbgui::createDebugConsole()
 */
void fbgui::toggleDebugConsole() {
   (_debugConsole->isVisible()) ? _debugConsole->hide() : _debugConsole->show();
}