-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.cpp
701 lines (611 loc) · 28.3 KB
/
console.cpp
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#include <iostream>
#include <QFileDialog>
#include <QMessageBox>
#include <QTemporaryFile>
#include <QTextStream>
#include <QNetworkInterface>
#include <QAbstractSocket>
#include <QNetworkAddressEntry>
#include "stdlib.h"
#include "console.h"
#include "ui_console.h"
#ifdef Q_OS_WIN32
extern const char fusinv[];
extern const int fusinv_size;
#else
extern const char _fusinv[];
extern const int _fusinv_size;
#endif
Console::Console(QWidget *parent) :
QDialog(parent),
execStatus(Console::NoError),
ui(new Ui::Console)
{
ui->setupUi(this);
}
Console::~Console()
{
delete ui;
}
void Console::setConfig(Config *config) {
this->config = config;
}
bool Console::startLocal() {
this->show();
ui->pushButtonOK->setText("Close");
this->setWindowTitle(tr("Localhost Inventory"));
this->repaint();
QString program;
QStringList arguments;
#ifdef Q_OS_WIN32
program = QString("%1\\perl\\bin\\perl.exe")
.arg(config->get("agent-win"));
arguments << QString( "%1\\perl\\bin\\fusioninventory-agent")
.arg(QDir::toNativeSeparators(config->get("agent-win")));
#else
program = QString("%1/fusioninventory-agent")
.arg(config->get("agent-unix"));
#endif
arguments << "--debug";
arguments << QString("--tag=%1")
.arg(config->get("tag"));
arguments << QString("--server=%1")
.arg(config->get("server"));
myProcess = new QProcess();
connect ( myProcess, SIGNAL(readyReadStandardError()), this,
SLOT(updateConsole()));
connect ( myProcess, SIGNAL(readyReadStandardOutput()), this,
SLOT(updateConsole()));
connect ( myProcess, SIGNAL(error(QProcess::ProcessError)), this,
SLOT(execError(QProcess::ProcessError)));
ui->plainTextConsoleOut->clear();
ui->plainTextConsoleErr->clear();
ui->plainTextConsoleOut->appendPlainText(program);
/* foreach(QString tempStr, arguments) {
ui->plainTextConsoleOut->appendPlainText(tempStr);
}
*/
execStatus = NoError;
myProcess->start(program, arguments);
ui->pushButtonOK->setText("Kill / Close");
while(myProcess->state() != QProcess::NotRunning) {
qApp->processEvents(QEventLoop::AllEvents, 700);
}
updateConsole();
//FIXME Detect Error Code 3 (ERROR_PATH_NOT_FOUND) and suggest installation
ui->plainTextConsoleOut->appendPlainText("---------- Process Completed ----------");
int exitCode = myProcess->exitCode();
ui->plainTextConsoleOut->appendPlainText(QString("Exit code: %1").arg(exitCode));
ui->pushButtonOK->setText("OK");
return (execStatus ==NoError) && ! exitCode;
}
Console::remoteExecError Console::startRemoteWin(QString curHost="") {
this->show();
ui->pushButtonOK->setText("Close");
this->repaint();
#ifdef Q_OS_WIN32
QString tempPsExec = createPsExecFile();
if (tempPsExec.isEmpty()) {
return UnknownError;
}
QString targetHost = curHost.isEmpty()? config->get("host") : curHost;
this->setWindowTitle(tr("Remote Windows Agent Installation on ") + targetHost);
QStringList arguments;
arguments << QString("\\\\%1").arg(targetHost);
arguments << "-u"
<< QString("%1").arg(config->get("host-user"))
<< "-p"
<< QString("%1").arg(config->get("host-password"));
arguments << QString("%1\\perl\\bin\\perl.exe").arg(config->get("agent-win"));
arguments << QString("%1\\perl\\bin\\fusioninventory-agent").arg(config->get("agent-win"));
arguments << "--debug";
arguments << QString("--tag=%1").arg(config->get("tag"));
arguments << QString("--server=%1").arg(config->get("server"));
processExec(tempPsExec, arguments);
checkConsoleOut(targetHost);
QFile(tempPsExec).remove();
#else
QString tempWinexe = createWinexeFile();
if (tempWinexe .isEmpty()) {
return UnknownError;
}
QString targetHost = curHost.isEmpty()? config->get("host") : curHost;
this->setWindowTitle(tr("Remote Windows Host Inventory of ") + targetHost);
QStringList arguments;
arguments << "--user"
<< QString("%1%%2")
.arg(config->get("host-user"))
.arg(config->get("host-password"));
arguments << "-d" << "11";
arguments << QString("//%1").arg(targetHost);
arguments << QString("\"%1\\perl\\bin\\perl.exe\" \"%1\\perl\\bin\\fusioninventory-agent\" --debug '--tag=%2' --server %3")
.arg(config->get("agent-win"))
.arg(config->get("tag"))
.arg(config->get("server"));
processExec(tempWinexe, arguments);
checkConsoleOut(targetHost);
QFile(tempWinexe).remove();
#endif
ui->pushButtonOK->setText("OK");
return execStatus;
}
bool Console::instLocal() {
this->show();
ui->pushButtonOK->setText("Close");
this->setWindowTitle(tr("Local Agent Installation"));
this->repaint();
#ifdef Q_OS_WIN32
QString installationFile = createInstWinFile();
if (installationFile .isEmpty()) {
return false;
}
QString program = QDir::toNativeSeparators(installationFile);
QStringList arguments;
arguments << "/S" << QString("/tag=%1").arg(config->get("tag"))
<< QString("/server=%1").arg(config->get("server"))
<< QString("/user=%1").arg(config->get("server-user"))
<< QString("/pass=%1").arg(config->get("server-pass"))
<< QString("/proxy=%1").arg(config->get("proxy"))
<< QString("/installdir=%1").arg(config->get("agent-win"))
<< QString("/runnow");
#else
QString instUnixFile = QFileDialog::getOpenFileName(this,
tr("Unix installation file"),
QDir::currentPath(),
"ZIP(*.zip);;Gzip compressed tar(*.tar.gz *.tgz);;Bzip2 compressed tar(*.tar.bz2);;tar(*.tar)");
if (instUnixFile.isEmpty()) {
return false;
}
QFileInfo instUnixPathInfo(instUnixFile);
if (!instUnixPathInfo.isFile() || !instUnixPathInfo.isReadable()) {
QMessageBox::critical(this, tr("installation file error"),
tr("Installation file is not readable."),
QMessageBox::Ok);
return false;
}
QTemporaryFile tempFile(QString("%1/%2-XXXXXX.sh")
.arg(QDir::tempPath())
.arg(QFileInfo(qApp->applicationFilePath()).fileName()));
if (! tempFile.open() || ! createInstScript(&tempFile, instUnixFile) ) {
QMessageBox::critical(this, tr("installation script error"),
tr("Could not create a temporary installation script."),
QMessageBox::Ok);
return false;
}
/* Making sure that AutoRemove is set and closing the file descriptor
*/
tempFile.setAutoRemove(true);
tempFile.close();
if( ! tempFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
QFile::ReadGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::ExeOther) ) {
QMessageBox::critical(this, tr("installation script error"),
tr("Could not set executable permission on the temporary installation script."),
QMessageBox::Ok);
return false;
}
QString program = "sh";
QStringList arguments;
arguments << tempFile.fileName();
//TODO
/* Might be a good idea to run the shell by "errexit" option
arguments << "-o" << "errexit";
*/
#endif
myProcess = new QProcess();
connect ( myProcess, SIGNAL(readyReadStandardError()), this,
SLOT(updateConsole()));
connect ( myProcess, SIGNAL(readyReadStandardOutput()), this,
SLOT(updateConsole()));
connect ( myProcess, SIGNAL(error(QProcess::ProcessError)), this,
SLOT(execError(QProcess::ProcessError)));
ui->plainTextConsoleOut->clear();
ui->plainTextConsoleErr->clear();
ui->plainTextConsoleOut->appendPlainText(program);
/* foreach(QString tempStr, arguments) {
ui->plainTextConsoleOut->appendPlainText(tempStr);
}
*/
execStatus = NoError;
myProcess->start(program, arguments);
ui->pushButtonOK->setText("Kill / Close");
while(myProcess->state() != QProcess::NotRunning) {
qApp->processEvents(QEventLoop::AllEvents, 700);
}
updateConsole();
ui->plainTextConsoleOut->appendPlainText("---------- Process Completed ----------");
int exitCode = myProcess->exitCode();
ui->plainTextConsoleOut->appendPlainText(QString("Exit code: %1").arg(exitCode));
ui->pushButtonOK->setText("OK");
/* Remove the installation script/program before returning
*/
#ifdef Q_OS_WIN32
QFile::setPermissions(installationFile, QFile::ReadOwner | QFile::WriteOwner);
QFile::remove(installationFile);
#else
/* AutoRemove is set, .remove is not necessary.
*/
// tempFile.remove();
#endif
return (execStatus == NoError) && ! exitCode;
}
Console::remoteExecError Console::instRemoteWin(QString curHost="") {
this->show();
ui->pushButtonOK->setText("Close");
this->repaint();
#ifdef Q_OS_WIN32
return instRemoteWinFromWin(curHost);
#else
return instRemoteWinFromUnix(curHost);
#endif
}
Console::remoteExecError Console::instRemoteWinFromWin(QString curHost="") {
QString tempPsExec = createPsExecFile();
if (tempPsExec.isEmpty()) {
return UnknownError;
}
QString targetHost = curHost.isEmpty()? config->get("host") : curHost;
this->setWindowTitle(tr("Remote Windows Agent Installation on ") + targetHost);
QStringList arguments;
arguments << QString("\\\\%1").arg(targetHost);
arguments << "-u"
<< QString("%1").arg(config->get("host-user"))
<< "-p"
<< QString("%1").arg(config->get("host-password"));
arguments << "-c" << "-f";
QString installationFile = createInstWinFile();
if (installationFile .isEmpty()) {
return UnknownError;
}
arguments << installationFile;
arguments << "/S";
arguments << QString("/tag=%1").arg(config->get("tag"));
arguments << QString("/server=%1").arg(config->get("server"));
arguments << "/runnow";
if(!config->get("server-user").isEmpty()) {
arguments << QString("/user=%1").arg(config->get("server-user"));
}
if(!config->get("server-pass").isEmpty()) {
arguments << QString("/pass=%1").arg(config->get("server-pass"));
}
if(!config->get("proxy").isEmpty()) {
arguments << QString("/proxy=%1").arg(config->get("proxy"));
}
if(!config->get("agent-win").isEmpty()) {
arguments << QString("/installdir=%1").arg(config->get("agent-win"));
}
ui->plainTextConsoleOut->appendPlainText("---------- Installation in progress ----------");
ui->plainTextConsoleErr->appendPlainText("---------- Installation in progress ----------");
processExec(tempPsExec, arguments);
//TODO check the return value
QFile(tempPsExec).remove();
ui->pushButtonOK->setText("OK");
return execStatus;
}
Console::remoteExecError Console::instRemoteWinFromUnix(QString curHost="") {
QString tempWinexe = createWinexeFile();
if (tempWinexe.isEmpty()) {
return UnknownError;
}
QString targetHost = curHost.isEmpty()? config->get("host") : curHost;
this->setWindowTitle(tr("Remote Windows Agent Installation on ") + targetHost);
QStringList arguments;
arguments << "--user"
<< QString("%1%%2")
.arg(config->get("host-user"))
.arg(config->get("host-password"));
arguments << QString("--runas=%1%%2")
.arg(config->get("host-user"))
.arg(config->get("host-password"));
arguments << "-d" << "11";
arguments << QString("//%1").arg(targetHost);
QStringList tempArguments;
tempArguments = QStringList(arguments);
tempArguments << "CMD.EXE /C \"MKDIR %TEMP%\\FusInv\"";
processExec(tempWinexe, tempArguments);
/* This directory might already be there. Ignoring the return value */
tempArguments = QStringList(arguments);
tempArguments << "CMD.EXE /C \"NET SHARE FusInv=%TEMP%\\FusInv /UNLIMITED\"";
processExec(tempWinexe, tempArguments);
//TODO check the return value
QString installationFile = createInstWinFile();
if (installationFile .isEmpty()) {
return UnknownError;
}
tempArguments = QStringList();
tempArguments << QString("//%1/FusInv").arg(targetHost);
tempArguments << config->get("host-password");
tempArguments << "-U" << config->get("host-user");
tempArguments << "-c" << QString("lcd %1 ; put %2")
.arg(QFileInfo(installationFile).absolutePath())
.arg(QFileInfo(installationFile).fileName());
processExec("smbclient", tempArguments);
QFile(installationFile).remove();
//TODO check the return value
tempArguments = QStringList(arguments);
tempArguments << "CMD.EXE /C \"NET SHARE FusInv /d\"";
processExec(tempWinexe, tempArguments);
tempArguments = QStringList(arguments);
//QString tempCMD = QString("CMD.EXE /C \"%TEMP%\\FusInv\\%1\" /S '/tag=%2' '/server=%3' /runnow")
QString tempCMD = QString("CMD.EXE /S /C \"\"%TEMP%\\FusInv\\%1\" /S \"/tag=%2\" \"/server=%3\" /runnow")
.arg(QFileInfo(installationFile).fileName())
.arg(config->get("tag"))
.arg(config->get("server"));
if(!config->get("server-user").isEmpty()) {
tempCMD.append(QString(" \"/user=%1\"").arg(config->get("server-user")));
}
if(!config->get("server-pass").isEmpty()) {
tempCMD.append(QString(" \"/pass=%1\"").arg(config->get("server-pass")));
}
if(!config->get("proxy").isEmpty()) {
tempCMD.append(QString(" \"/proxy=%1\"").arg(config->get("proxy")));
}
if(!config->get("agent-win").isEmpty()) {
tempCMD.append(QString(" \"/installdir=%1\"").arg(config->get("agent-win")));
}
tempCMD.append("\"");
tempArguments << tempCMD;
ui->plainTextConsoleOut->appendPlainText("---------- Installation in progress ----------");
ui->plainTextConsoleErr->appendPlainText("---------- Installation in progress ----------");
processExec(tempWinexe, tempArguments);
//TODO check the return value
tempArguments = QStringList(arguments);
tempArguments << "CMD.EXE /C \"RMDIR /S /Q %TEMP%\\FusInv\"";
processExec(tempWinexe, tempArguments);
QFile(tempWinexe).remove();
ui->pushButtonOK->setText("OK");
return execStatus;
}
void Console::updateConsole () {
QByteArray stdOutText = myProcess->readAllStandardOutput();
QByteArray stdErrText = myProcess->readAllStandardError();
if(stdOutText.size()>0) {
ui->plainTextConsoleOut->appendPlainText(stdOutText.data());
}
if(stdErrText.size()>0) { /* This condition would be always true */
ui->plainTextConsoleErr->appendPlainText(stdErrText.data());
}
}
void Console::execError(QProcess::ProcessError error) {
if (execStatus == NoError) { execStatus = UnknownError; }
ui->plainTextConsoleErr->appendPlainText(QString("Execution failed due to error no. %1").arg(error));
if(error == QProcess::FailedToStart) {
ui->plainTextConsoleErr->appendPlainText(tr("The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program."));
}
}
void Console::on_pushButtonOK_clicked()
{
if (myProcess->state() == QProcess::Running) {
myProcess->kill();
execStatus = Killed;
}
this->hide();
}
bool Console::createInstScript(QFile * scriptFile, QString fileName) {
// Generating a decompression command for each file type
// ZIP(*.zip);;Gzip compressed tar(*.tar.gz *.tgz);;Bzip2 compressed tar(*.tar.bz2);;tar(*.tar)
QString decompressCmd;
if(fileName.right(4).toLower() == ".zip") {
decompressCmd = QString("unzip '%1' -d \"$TEMP_DIR\"").arg(fileName);
} else if(fileName.right(4).toLower() == ".tar") {
decompressCmd = QString("tar xvf '%1' -C \"$TEMP_DIR\"").arg(fileName);
} else if(fileName.right(4).toLower() == ".tgz" || fileName.right(7).toLower() == ".tar.gz") {
decompressCmd = QString("gzip -dc '%1' | tar xvf - -C \"$TEMP_DIR\"").arg(fileName);
} else if(fileName.right(8).toLower() == ".tar.bz2") {
decompressCmd = QString("bzip2 -dc '%1' | tar xvf - -C \"$TEMP_DIR\"").arg(fileName);
} else {
QMessageBox::information(this, tr("not implemented yet"),
tr("Installation using this type of file is not supported yet."),
QMessageBox::Ok);
return false;
}
//TODO
//List exit codes
QTextStream scriptTextStream(scriptFile);
scriptTextStream << "#!/bin/sh";
scriptTextStream << "\n";
QString instParentPath = QDir(QFileInfo(config->get("agent-unix").replace(QRegExp("/$"),"")).absolutePath()).absolutePath();
/* Checking the parent directory of the installation directory to be writable
*/
scriptTextStream << "\n" << QString("if [ -d '%1' ]").arg(instParentPath);
scriptTextStream << "\n" << "then";
scriptTextStream << "\n" << QString(" if [ ! -w '%1' ]").arg(instParentPath);
scriptTextStream << "\n" << " then";
scriptTextStream << "\n" << QString(" echo '%1'").arg(tr("Parent directory of the installation path is not writable."));
scriptTextStream << "\n" << " exit 1";
scriptTextStream << "\n" << " fi";
scriptTextStream << "\n" << "else";
scriptTextStream << "\n" << QString(" mkdir -p '%1'").arg(instParentPath);
scriptTextStream << "\n" << QString(" if [ ! -d '%1' -o !-w '%1' ]; then echo '%2'; exit 2; fi").arg(instParentPath).arg(tr("Could not create the parent directory of the installation path."));
scriptTextStream << "\n" << "fi";
scriptTextStream << "\n";
scriptTextStream << "\n" << QString("TEMP_DIR='%1/%2.%3.%4'")
.arg(instParentPath)
.arg(QFileInfo(qApp->applicationFilePath()).fileName())
.arg(qApp->applicationPid())
.arg(rand() % 100);
scriptTextStream << "\n" << "mkdir -p \"$TEMP_DIR\"";
scriptTextStream << "\n" << QString("if [ ! -d \"$TEMP_DIR\" -o ! -w \"$TEMP_DIR\" ]; then echo '%1'; exit 3; fi").arg(tr("Could not create the temp directory."));
scriptTextStream << "\n" << decompressCmd;
scriptTextStream << "\n" << QString("rm -rf '%1'").arg(config->get("agent-unix"));
scriptTextStream << "\n" << QString("mv \"$TEMP_DIR\"/* '%2'").arg(config->get("agent-unix"));
scriptTextStream << "\n" << "rm -rf \"$TEMP_DIR\"";
scriptTextStream << "\n\n";
return true;
}
QString Console::createPsExecFile() {
QString fileName = QString("%1/psexec.%2.%3.exe")
.arg(QDir::tempPath())
.arg(qApp->applicationPid())
.arg(rand() % 100);
if ( QFile::copy(":/fusinv/psexec.exe", fileName) &&
QFile(fileName).setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
QFile::ReadGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::ExeOther) ) {
return fileName;
} else {
QMessageBox::critical(this, tr("Embedded PsExec.exe file error"),
tr("Could not copy the embedded PsExec.exe file into a regular file."),
QMessageBox::Ok);
return "";
}
}
QString Console::createWinexeFile() {
QString fileName = QString("%1/winexe.%2.%3")
.arg(QDir::tempPath())
.arg(qApp->applicationPid())
.arg(rand() % 100);
if ( QFile::copy(":/fusinv/winexe", fileName) &&
QFile(fileName).setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
QFile::ReadGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::ExeOther) ) {
return fileName;
} else {
QMessageBox::critical(this, tr("Embedded winexe file error"),
tr("Could not copy the embedded winexe file into a regular file."),
QMessageBox::Ok);
return "";
}
}
QString Console::createInstWinFile() {
QString fileName = QDir::toNativeSeparators(
QString("%1/FusInvAgentInst.%2.%3.exe")
.arg(QDir::tempPath())
.arg(qApp->applicationPid())
.arg(rand() % 100) );
QFile instWinFile(fileName);
QDataStream tempStream(&instWinFile);
if ( instWinFile.open(QIODevice::WriteOnly) &&
#ifdef Q_OS_WIN32
tempStream.writeRawData(fusinv, fusinv_size) == fusinv_size &&
#else
tempStream.writeRawData(_fusinv, _fusinv_size) == _fusinv_size &&
#endif
instWinFile.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
QFile::ReadGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::ExeOther) ) {
return fileName;
} else {
QMessageBox::critical(this, tr("Embedded Installation File error"),
tr("Could not copy the embedded installation file into a regular file."),
QMessageBox::Ok);
return "";
}
}
void Console::performRemoteWindowsInventory(QString curHost) {
static bool askEachInstall = true;
if(startRemoteWin(curHost) == NotInstalled) {
if(askEachInstall) {
QMessageBox::StandardButton tempAnswer = QMessageBox::question(this,
tr("Agent execution failed"),
tr("Agent execution failed. Would you like to try to install/reinstall the agent?"),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
if (tempAnswer == QMessageBox::YesToAll) {
askEachInstall = false;
} else if (tempAnswer == QMessageBox::No) {
return;
}
}
if(instRemoteWin(curHost) == NoError) {
if(askEachInstall) {
QMessageBox::information(this, tr("Installation completed"),
tr("Installation completed successfuly. Retrying agent execution..."),
QMessageBox::Ok);
}
startRemoteWin(curHost);
} else if(askEachInstall) {
QMessageBox::critical(this, tr("Installation failed"),
tr("Installation failed"),
QMessageBox::Ok);
}
}
}
void Console::performRemoteWindowsInventoryOnIPv4Range(quint32 startIP, quint32 endIP) {
quint32 curIPv4Address;
QString tempIPv4RangeName = QString("\n%1 - %2\n").arg(QHostAddress(startIP).toString()).arg(QHostAddress(endIP).toString());
bool askEachIPv4Inventory = true;
for (curIPv4Address = startIP; curIPv4Address <= endIP; curIPv4Address++) {
if (askEachIPv4Inventory || execStatus == Killed) {
QMessageBox::StandardButton tempAnswer = QMessageBox::question(this,
tr("IP Address"),
tr("Should we continue inventory of")
+ tempIPv4RangeName
+ tr("with the following IP address?")
+ "\n" + QHostAddress(curIPv4Address).toString(),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
if (tempAnswer == QMessageBox::YesToAll) {
askEachIPv4Inventory = false;
} else if (tempAnswer == QMessageBox::No) {
return;
}
}
performRemoteWindowsInventory(QHostAddress(curIPv4Address).toString());
}
}
void Console::checkConsoleOut(QString curHost) {
/* Followong output means that the agent is not installed on the remote win host (for winexe)
Error: error Creating process("C:\Program Files\FusionInventory-Agent\perl\bin\perl.exe" "C:\Program Files\FusionInventory-Agent\perl\bin\fusioninventory-agent" --debug --tag=Testing-by_GUI --server http://ocs/ocsinventory) 3
Or (for PsExec.exe)
PsExec could not start D:\FusIn Agent\perl\bin\perl.exe on 10.10.2.12:
*/
if(ui->plainTextConsoleOut->find(
QString("Error: error Creating process(%1) 3").arg(QString("\"%1\\perl\\bin\\perl.exe\" \"%1\\perl\\bin\\fusioninventory-agent\" --debug '--tag=%2' --server %3")
.arg(config->get("agent-win"))
.arg(config->get("tag"))
.arg(config->get("server"))),
QTextDocument::FindBackward) ||
ui->plainTextConsoleErr->find(
QString("PsExec could not start %1\\perl\\bin\\perl.exe on %2:")
.arg(config->get("agent-win"))
.arg(curHost),
QTextDocument::FindBackward)
) {
ui->plainTextConsoleErr->appendPlainText(tr("Agent is not installed on the remote win host"));
execStatus = NotInstalled;
} else if(ui->plainTextConsoleOut->find("ERROR: Failed to open connection - NT_STATUS_LOGON_FAILURE",
QTextDocument::FindBackward)) {
ui->plainTextConsoleErr->appendPlainText(tr("Could not log onto the remote win host."));
execStatus = LogonFailure;
} else if(ui->plainTextConsoleOut->find("ERROR: Failed to open connection - NT_STATUS_HOST_UNREACHABLE",
QTextDocument::FindBackward)) {
ui->plainTextConsoleErr->appendPlainText(tr("No such a host exists or host unreachable."));
execStatus = HostUnreachable;
} else if(ui->plainTextConsoleOut->find("ERROR: Failed to open connection - NT_STATUS_IO_TIMEOUT",
QTextDocument::FindBackward)) {
ui->plainTextConsoleErr->appendPlainText(tr("Remote host could not be contacted (timed out)."));
execStatus = IOTimeout;
}
}
void Console::processExec(QString program, QStringList arguments) {
myProcess = new QProcess();
connect ( myProcess, SIGNAL(readyReadStandardError()), this,
SLOT(updateConsole()));
connect ( myProcess, SIGNAL(readyReadStandardOutput()), this,
SLOT(updateConsole()));
connect ( myProcess, SIGNAL(error(QProcess::ProcessError)), this,
SLOT(execError(QProcess::ProcessError)));
ui->plainTextConsoleOut->clear();
ui->plainTextConsoleErr->clear();
ui->plainTextConsoleOut->appendPlainText(program);
/* foreach(QString tempStr, arguments) {
ui->plainTextConsoleOut->appendPlainText(tempStr);
}
*/
execStatus = NoError;
myProcess->start(program, arguments);
ui->pushButtonOK->setText("Kill / Close");
while(myProcess->state() != QProcess::NotRunning) {
qApp->processEvents(QEventLoop::AllEvents, 700);
}
updateConsole();
ui->plainTextConsoleOut->appendPlainText("---------- Process Completed ----------");
int exitCode = myProcess->exitCode();
ui->plainTextConsoleOut->appendPlainText(QString("Exit code: %1").arg(exitCode));
if ( execStatus == NoError && exitCode) {
execStatus = UnknownError;
}
}