This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
/
dialogwarden.cpp
1702 lines (1346 loc) · 50.5 KB
/
dialogwarden.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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
****************************************************************************/
#include "dialogwarden.h"
#include <QDate>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QTextStream>
#include <QTime>
#include "dialogworking.h"
#include "dialogconfig.h"
#include "dialogdisplayoutput.h"
#include "dialogimport.h"
#include "dialogTemplates.h"
#include "wizardNewJail.h"
#include "pcbsd-utils.h"
/* Update interval in ms - Every 5 seconds */
#define UPDATE_MSEC 1000 * 5
void dialogWarden::helpAbout()
{
}
void dialogWarden::programInit()
{
// Disable these until we check if the jail is running
pushTerminal->setEnabled(false);
pushPackageManager->setEnabled(false);
// Setup our listview options
listJails->setSelectionMode(QAbstractItemView::SingleSelection);
listJails->setAllColumnsShowFocus(TRUE);
// Run this a single time, just to be sure we did any jail import / migration
// from legacy jails
system("warden list");
// Hide the identifier column / host column
listJails->setColumnHidden(0, true);
listJails->setColumnWidth(1, 200);
listJails->setColumnWidth(2, 200);
listJails->setColumnWidth(3, 150);
listJails->setColumnWidth(4, 40);
groupJailTab->setTitle(tr("<- Please select a jail"));
groupJailTab->setEnabled(false);
// Read in our configuration data
readConfig();
// Start our monitor service to check if jails are running
checkingStatus = false;
checkingUpdates = false;
currentStatusWorkingJail = "";
currentUpdateWorkingJail = "";
statusTimer = new QTimer(this);
connect( statusTimer, SIGNAL(timeout()), this, SLOT( slotMonitorJailStatus() ) );
slotMonitorJailStatus();
// Start our monitor service for jail details
checkingDetails = false;
currentDetailsWorkingJail = "";
detailsTimer = new QTimer(this);
connect( detailsTimer, SIGNAL(timeout()), this, SLOT( slotMonitorJailDetails() ) );
slotMonitorJailDetails();
// Start by refreshing our jail list
refreshJails();
// Connect the rightclick slot
listJails->setContextMenuPolicy( Qt::CustomContextMenu);
connect( listJails, SIGNAL(customContextMenuRequested (const QPoint & ) ), this, SLOT(slotJailRightClicked() ) );
connect( listJails, SIGNAL(itemSelectionChanged() ), this, SLOT(slotCurrentJailChanged() ) );
connect( pushAdd, SIGNAL(clicked()), this, SLOT(slotClickedNewJail() ) );
connect( pushRemove, SIGNAL(clicked()), this, SLOT(slotDeleteJail() ) );
connect( pushExportJail, SIGNAL(clicked()), this, SLOT(slotExportJail() ) );
connect( pushServiceGUI, SIGNAL(clicked()), this, SLOT(slotServiceGUI() ) );
connect( pushTerminal, SIGNAL(clicked()), this, SLOT(slotTerminal() ) );
connect( pushUserAdmin, SIGNAL(clicked()), this, SLOT(slotUserAdmin() ) );
connect( pushConfigure, SIGNAL(clicked()), this, SLOT(slotPushEditIP() ) );
connect( pushPackageManager, SIGNAL(clicked()), this, SLOT(slotPushPackage() ) );
connect( pushUpdate, SIGNAL(clicked()), this, SLOT(slotUpdate() ) );
connect( pushStart, SIGNAL(clicked()), this, SLOT(slotStartJail() ) );
// Snapshot / Clone support
connect( pushAddClone, SIGNAL(clicked()), this, SLOT(slotAddClone() ) );
connect( pushRemoveClone, SIGNAL(clicked()), this, SLOT(slotRemoveClone() ) );
connect( pushCreateSnap, SIGNAL(clicked()), this, SLOT(slotCreateSnap() ) );
connect( pushRestoreSnap, SIGNAL(clicked()), this, SLOT(slotRestoreSnap() ) );
connect( pushRemoveSnap, SIGNAL(clicked()), this, SLOT(slotRemoveSnap() ) );
//connect( sliderSnaps, SIGNAL(sliderMoved(int)), this, SLOT(slotSnapSliderChanged(int) ) );
connect( sliderSnaps, SIGNAL(valueChanged(int)), this, SLOT(slotSnapSliderChanged(int) ) );
// Snapshot cron support
connect( comboSnapFrequency, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCronSnapshotChanged() ) );
connect( spinSnapDays, SIGNAL(valueChanged(int)), this, SLOT(slotCronSnapshotChanged() ) );
connect( groupSnapSchedule, SIGNAL(clicked(bool)), this, SLOT(slotCronSnapshotChanged() ) );
QTimer::singleShot(100, this, SLOT(slotCheckNic()) );
// Disable the start button until status's come back
pushStart->setEnabled(false);
}
void dialogWarden::slotCheckNic()
{
QString NIC = pcbsd::Utils::getValFromPCConf("/usr/local/etc/warden.conf", "NIC");
if ( system("ifconfig " + NIC.toLatin1() + " >/dev/null 2>/dev/null") != 0 )
{
// Confirm with user
int ret = QMessageBox::warning(this, tr("Warden"),
tr("No valid network interface specified! Select one now?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if ( ret == QMessageBox::No)
return;
slotOpenConfig();
}
}
void dialogWarden::refreshJails()
{
QString jType, jName, jZFS;
QStringList jD;
QStringList mountOut;
update();
listJails->clear();
jailDetails.clear();
// Check our JailDir and see what we have
QDir d(JailDir);
d.setFilter(QDir::Dirs | QDir::Hidden);
d.setNameFilters(QStringList() << ".*.meta");
// Start mount cmd
QProcess m;
m.start(QString("mount"), QStringList());
while(m.state() == QProcess::Starting || m.state() == QProcess::Running) {
m.waitForFinished(200);
QCoreApplication::processEvents();
}
// Get output of mount now
while (m.canReadLine())
mountOut << m.readLine().simplified();
for ( unsigned int i = 0; i < d.count(); i++ ) {
// Traverse our directories, looking for jails
if ( d[i] == "." || d[i] == ".." )
continue;
jD.clear();
qDebug() << "Checking:" << d[i];
jName = d[i].replace(".meta", "").remove(0, 1);
// Check if this jail is created on ZFS
jZFS="NO";
for (int z = 0; z < mountOut.size(); ++z)
if ( mountOut.at(z).indexOf("on " + JailDir + "/" + jName) != -1 && mountOut.at(z).indexOf("(zfs,") != -1 )
jZFS="YES";
// Check for the hostname of this jail
QFile file( JailDir + "/" + d[i] + "/host" );
if ( ! file.exists() )
continue;
if ( ! file.open( QIODevice::ReadOnly ) )
continue;
QTextStream stream( &file );
QString host;
while ( !stream.atEnd() )
host = stream.readLine(); // line of text excluding '\n'
file.close();
// Build list of IPs for this jail
QString jIPs = "";
QStringList ipFiles;
ipFiles << "ipv4" << "ipv6";
for (int f = 0; f < ipFiles.size(); ++f) {
QFile fileip(JailDir + "/" + d[i] + "/" + ipFiles.at(f) );
if ( fileip.exists() ) {
if (fileip.open( QIODevice::ReadOnly ) ) {
QTextStream stream2( &fileip );
while ( !stream2.atEnd() )
jIPs = jIPs + stream2.readLine().simplified() + " "; // line of text excluding '\n'
}
fileip.close();
}
}
qDebug() << jIPs;
// Check the type of jail
jType = "Traditional";
QFile file3(JailDir + "/" + d[i] + "/jail-portjail" );
if ( file3.exists() )
jType = "Ports Jail";
QFile filelin(JailDir + "/" + d[i] + "/jail-linux" );
if ( filelin.exists() )
jType = "Linux Jail";
// Display the jail in the tree widget now
QStringList cols;
cols << jName << host;
QTreeWidgetItem *curItem = new QTreeWidgetItem( listJails, cols );
if ( ! listJails->currentItem() )
listJails->setCurrentItem(curItem);
// Save additional jail details into list
jD << jName << "Pending" << jType << jIPs << "" << "" << "" << jZFS;
jailDetails << jD;
} // end of loop
listJails->sortByColumn(1, Qt::AscendingOrder);
update();
slotCurrentJailChanged();
// Now refresh jail details
slotMonitorJailDetails();
}
void dialogWarden::readConfig()
{
ProgDir = "/usr/local/share/warden";
TMPDIR = pcbsd::Utils::getValFromPCConf("/usr/local/etc/warden.conf", "WTMP");
JailDir = pcbsd::Utils::getValFromPCConf("/usr/local/etc/warden.conf", "JDIR");
}
void dialogWarden::slotOpenConfig()
{
// Launch our AddPartitionDialog to add a new device
configDialog = new dialogConfig();
connect(configDialog, SIGNAL(saved()),this, SLOT(readConfig()) );
configDialog->programInit();
configDialog->exec();
}
void dialogWarden::slotExit()
{
exit(0);
}
void dialogWarden::slotCheckJailDetails()
{
QString oldWorking = currentDetailsWorkingJail;
int found = 0;
// Mark that we are in the middle of checking the details
checkingDetails = true;
// Read in our iterator and start checking jail status
QTreeWidgetItemIterator it( listJails );
while (*it) {
if ( currentDetailsWorkingJail.isEmpty() ) {
currentDetailsWorkingJail = (*it)->text(0);
break;
}
if ( found == 1) {
currentDetailsWorkingJail = (*it)->text(0);
break;
}
if ( currentDetailsWorkingJail == (*it)->text(0) )
found = 1;
++it;
}
// Are we ready to take a break?
if ( currentDetailsWorkingJail == oldWorking ) {
checkingDetails = false;
currentDetailsWorkingJail="";
return;
}
// Build list of IPs for this jail
QString jIPs = "";
QStringList ipFiles;
ipFiles << "ipv4" << "ipv6";
for (int f = 0; f < ipFiles.size(); ++f) {
QFile fileip(JailDir + "/." + currentDetailsWorkingJail + ".meta/" + ipFiles.at(f) );
if ( fileip.exists() ) {
if (fileip.open( QIODevice::ReadOnly ) ) {
QTextStream stream2( &fileip );
while ( !stream2.atEnd() )
jIPs = jIPs + stream2.readLine().simplified() + " "; // line of text excluding '\n'
}
fileip.close();
}
}
qDebug() << "Found IPS:" << jIPs;
// Save the new jail IPs
for (int i=0; i < jailDetails.count(); ++i) {
if ( jailDetails.at(i).at(0) != currentDetailsWorkingJail )
continue;
QStringList tmpList = jailDetails.at(i);
tmpList.replace(3, jIPs);
jailDetails.replace(i, tmpList);
}
// Run the getUpdatesDir.sh script to rsync with master server
detailsProc = new QProcess( this );
QString program = ProgDir + "/scripts/backend/details.sh";
QStringList args;
args << currentDetailsWorkingJail;
qDebug() << "Checking for details to: " << currentDetailsWorkingJail;
// Connect the exited signal and start the process
connect( detailsProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCheckDetailsReturn() ) );
detailsProc->start(program, args);
}
void dialogWarden::slotMonitorJailDetails()
{
if ( checkingDetails == true) {
// Set our timer to check again
detailsTimer->start( 1000 * 60 * 10 );
} else {
currentDetailsWorkingJail = "";
slotCheckJailDetails();
// Set our timer to check again
detailsTimer->start( 1000 * 60 * 10 );
}
if ( checkingUpdates == true) {
// Set our timer to check again
detailsTimer->start( 1000 * 60 * 10 );
} else {
currentUpdateWorkingJail = "";
slotCheckForUpdates();
// Set our timer to check again
detailsTimer->start( 1000 * 60 * 10 );
}
}
void dialogWarden::slotCheckForUpdates()
{
qDebug() << "Starting Update Check...";
QString oldWorking = currentUpdateWorkingJail;
checkingUpdates = true;
int found = 0;
// Read in our iterator and start checking jail status
QTreeWidgetItemIterator it( listJails );
while (*it) {
if ( currentUpdateWorkingJail.isEmpty() ) {
currentUpdateWorkingJail = (*it)->text(0);
break;
}
if ( found == 1)
{
currentUpdateWorkingJail = (*it)->text(0);
break;
}
if ( currentUpdateWorkingJail == (*it)->text(0) )
found = 1;
++it;
}
// Are we ready to take a break?
if ( currentUpdateWorkingJail == oldWorking ) {
checkingUpdates = false;
currentUpdateWorkingJail="";
qDebug() << "Finished Update Check...";
return;
}
updateProc = new QProcess( this );
QString program = "warden";
QStringList args;
args << "checkup" << currentUpdateWorkingJail;
// Connect the exited signal and start the process
connect( updateProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCheckUpdateReturn() ) );
updateProc->start(program, args);
}
void dialogWarden::slotCheckUpdateReturn()
{
bool haveUp = false;
QString line;
while (updateProc->canReadLine()) {
line = updateProc->readLine().simplified();
qDebug() << "Update output..." << line;
if ( line.indexOf("The following package updates") == 0 ) {
haveUp = true;
break;
}
if ( line.indexOf("The following files will be updated") == 0 ) {
haveUp = true;
break;
}
}
// Read in our iterator and update jail update status
QTreeWidgetItemIterator it( listJails );
while (*it) {
if ( currentUpdateWorkingJail == (*it)->text(0) )
break;
++it;
}
if ( ! (*it) )
return;
qDebug() << (*it)->text(0) << haveUp;
if ( haveUp ) {
(*it)->setText(4, tr("Updates available!"));
(*it)->setIcon(4, QIcon(":updateavail.png"));
} else {
(*it)->setText(4, "");
(*it)->setIcon(4, QIcon());
}
slotCheckForUpdates();
}
void dialogWarden::slotCheckDetailsReturn()
{
QString line, size, ports, connections;
// Read the output and parse
while ( detailsProc->canReadLine() )
{
line = detailsProc->readLine().simplified();
if ( line.indexOf("Disk Usage: ") == 0 ) {
line.replace("Disk Usage: ", "");
size = line;
continue;
}
if ( line.indexOf("Active Ports: ") == 0 ) {
line.replace("Active Ports: ", "");
ports = line;
continue;
}
if ( line.indexOf("Current Connections: ") == 0 ) {
line.replace("Current Connections: ", "");
connections = line;
continue;
}
}
// Now save details into our array
for (int i=0; i < jailDetails.count(); ++i) {
if ( jailDetails.at(i).at(0) != currentDetailsWorkingJail )
continue;
QStringList tmpList = jailDetails.at(i);
if ( jailDetails.at(i).at(1) == "Pending" )
tmpList.replace(1, "Complete");
tmpList.replace(4, size);
tmpList.replace(5, ports);
tmpList.replace(6, connections);
jailDetails.replace(i, tmpList);
}
// See if we can update the GUI details
refreshJailDetailsView();
// Check the next jail, if there are any more left.
slotCheckJailDetails();
}
void dialogWarden::slotCheckJailStatus()
{
QString oldWorking = currentStatusWorkingJail;
int found = 0;
// Mark that we are in the middle of checking the status
checkingStatus = true;
// Read in our iterator and start checking jail status
QTreeWidgetItemIterator it( listJails );
while (*it) {
if ( currentStatusWorkingJail.isEmpty() ) {
currentStatusWorkingJail = (*it)->text(0);
break;
}
if ( found == 1)
{
currentStatusWorkingJail = (*it)->text(0);
break;
}
if ( currentStatusWorkingJail == (*it)->text(0) )
{
found = 1;
}
++it;
}
// Are we ready to take a break?
if ( currentStatusWorkingJail == oldWorking ) {
checkingStatus = false;
currentStatusWorkingJail="";
return;
}
// Run the getUpdatesDir.sh script to rsync with master server
statusProc = new QProcess( this );
QString program = ProgDir + "/scripts/backend/checkstatus.sh";
QStringList args;
args << currentStatusWorkingJail;
// Connect the exited signal and start the process
connect( statusProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotCheckStatusReturn() ) );
statusProc->start(program, args);
}
void dialogWarden::slotMonitorJailStatus()
{
if ( checkingStatus == true) {
// Set our timer to check again
statusTimer->start( UPDATE_MSEC );
} else {
currentStatusWorkingJail = "";
slotCheckJailStatus();
// Set our timer to check again
statusTimer->start( UPDATE_MSEC );
}
}
void dialogWarden::slotCheckStatusReturn()
{
// Check the exit status
int exitStatus = statusProc->exitCode();
bool running;
// Read in our iterator and locate the jail, changing its status
QTreeWidgetItemIterator it( listJails );
while (*it) {
if ( currentStatusWorkingJail == (*it)->text(0) )
{
if ( exitStatus == 0) {
running = true;
(*it)->setIcon(1, QIcon(":running.png"));
(*it)->setText(2, "Running");
} else {
running = false;
(*it)->setIcon(1, QIcon(":stopped.png"));
(*it)->setText(2, "Not Running");
}
// See if we can update push buttons
if ( listJails->currentItem()) {
if ( listJails->currentItem()->text(0) == currentStatusWorkingJail ) {
if ( running ) {
pushPackageManager->setEnabled(true);
pushTerminal->setEnabled(true);
pushStart->setEnabled(true);
pushStart->setIcon(QIcon(":stopjail.png"));
pushStart->setIconSize(QSize(16,16));
pushStart->setToolTip(tr("Stop the selected jail"));
} else {
pushPackageManager->setEnabled(false);
pushTerminal->setEnabled(false);
pushStart->setEnabled(true);
pushStart->setIcon(QIcon(":running.png"));
pushStart->setIconSize(QSize(16,16));
pushStart->setToolTip(tr("Start the selected jail"));
}
}
}
break;
}
++it;
}
// Check the next jail, if there are any more left.
slotCheckJailStatus();
}
void dialogWarden::slotJailRightClicked()
{
QString Status, Updates;
if ( listJails->currentItem()) {
popupip = listJails->currentItem()->text(0);
Status = listJails->currentItem()->text(2);
Updates = listJails->currentItem()->text(3);
popup = new QMenu();
if ( Status == "Running" )
popup->addAction( tr("Stop this Jail") , this, SLOT(slotStopJail() ) );
else
popup->addAction( tr("Start this Jail") , this, SLOT(slotStartJail() ) );
popup->addSeparator();
if ( ! Updates.isEmpty() )
popup->addAction( tr("Start update manager") , this, SLOT(slotUpdate() ) );
popup->addAction( tr("Toggle Autostart") , this, SLOT(slotToggleAutostartClicked() ) );
popup->addAction( tr("Export jail to .wdn file") , this, SLOT(slotExportJail() ) );
popup->addSeparator();
popup->addAction( tr("Delete Jail") , this, SLOT(slotDeleteJail() ) );
popup->exec( QCursor::pos() );
}
}
void dialogWarden::slotStopJail()
{
if ( !listJails->currentItem())
return;
QString tmp;
popupip = listJails->currentItem()->text(0);
// Launch our working dialog to prevent further action until we are finished
workingDialog = new dialogWorking();
workingDialog->setModal(true);
workingDialog->programInit();
tmp = tr("Stopping Jail");
workingDialog->setDialogTitle(tmp);
tmp = tr("Stopping Jail:") + " " + popupip;
workingDialog->setDialogText(tmp);
workingDialog->show();
// Now start the script to stop this jail
stopJailProc = new QProcess( this );
QString program = "warden";
QStringList args;
args << "stop" <<popupip;
// Connect the exited signal and start the process
connect( stopJailProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotFinishedWorking() ) );
stopJailProc->start(program, args);
pushStart->setEnabled(false);
}
void dialogWarden::slotStartJail()
{
if ( !listJails->currentItem())
return;
// If jail is running, lets stop it
if ( listJails->currentItem()->text(2) == "Running" ) {
slotStopJail();
return;
}
QString tmp;
popupip = listJails->currentItem()->text(0);
// Launch our working dialog to prevent further action until we are finished
workingDialog = new dialogWorking();
workingDialog->setModal(true);
workingDialog->programInit();
tmp = tr("Starting Jail");
workingDialog->setDialogTitle(tmp);
tmp = tr("Starting Jail:") + " " + popupip;
workingDialog->setDialogText(tmp);
workingDialog->show();
// Now start the script to stop this jail
startJailProc = new QProcess( this );
QString program = "warden";
QStringList args;
args << "start" << popupip;
// Connect the exited signal and start the process
connect( startJailProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotFinishedWorking() ) );
startJailProc->start(program, args);
pushStart->setEnabled(false);
}
void dialogWarden::slotExportJail()
{
QString exportFile, tmp;
exportFile = QFileDialog::getExistingDirectory(
this,
tr("Choose a directory to save the finished .wdn file:"),
"/home",
QFileDialog::ShowDirsOnly );
if ( exportFile.isEmpty() )
{
return;
}
// Lauch the command output dialog
/*
dialogOutput = new dialogDisplayOutput(0, "Exporting Jail", Qt::WStyle_Customize | Qt::WShowModal);
dialogOutput->programInit(FALSE);
dialogOutput->setDialogCaption("Exporting Jail: " + popupip);
dialogOutput->setDialogText("");
dialogOutput->show();
*/
// Launch our working dialog to prevent further action until we are finished
workingDialog = new dialogWorking();
workingDialog->setModal(true);
workingDialog->programInit();
tmp = tr("Exporting Jail");
workingDialog->setDialogTitle(tmp);
tmp = tr("Exporting Jail:") + " " + popupip;
workingDialog->setDialogText(tmp);
workingDialog->show();
// Now start the script to stop this jail
exportJailProc = new QProcess( this );
QString program = "warden";
QStringList args;
args << "export" << popupip << "--dir=" + exportFile;
// Connect the exited signal and start the process
// connect( exportJailProc, SIGNAL(readyReadStdout ()), this, SLOT(slotReadDisplayOutput() ) );
connect( exportJailProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotFinishedWorking() ) );
exportJailProc->start(program, args);
}
void dialogWarden::slotListJailPkgs()
{
dialogOutput = new dialogDisplayOutput();
dialogOutput->setModal(true);
dialogOutput->programInit(TRUE);
dialogOutput->setDialogCaption("Jail Packages: " + popupip);
dialogOutput->setDialogText("");
dialogOutput->show();
// Now start the script to stop this jail
listPackagesProc = new QProcess( this );
QString program = ProgDir + "/scripts/backend/listpkgs.sh";
QStringList args;
args << popupip;
listPackagesProc->setProcessChannelMode(QProcess::MergedChannels);
listPackagesProc->setReadChannel(QProcess::StandardOutput);
// Connect the exited signal and start the process
connect( listPackagesProc, SIGNAL(readyReadStandardOutput ()), this, SLOT(slotReadPkgsOutput() ) );
listPackagesProc->start(program, args);
}
void dialogWarden::slotDeleteJail()
{
QString tmp;
if ( ! listJails->currentItem() )
return;
popupip = listJails->currentItem()->text(0);
// Confirm with user
int ret = QMessageBox::warning(this, tr("Warden"),
tr("Are you sure you want to delete the jail") + ": " + popupip,
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if ( ret == QMessageBox::No)
return;
// Launch our working dialog to prevent further action until we are finished
workingDialog = new dialogWorking();
workingDialog->setModal(true);
workingDialog->programInit();
tmp = tr("Deleting Jail");
workingDialog->setDialogTitle(tmp);
tmp = tr("Deleting Jail:") + " " + popupip;
workingDialog->setDialogText(tmp);
workingDialog->show();
// Now start the script to stop this jail
deleteJailProc = new QProcess( this );
QString program = ProgDir + "/scripts/backend/deletejail.sh";
QStringList args;
args << popupip;
// Connect the exited signal and start the process
connect( deleteJailProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(refreshJails() ) );
connect( deleteJailProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotFinishedWorking() ) );
deleteJailProc->start(program, args);
}
void dialogWarden::slotFinishedWorking()
{
// Finished whatever we were working on, hide the dialog now.
workingDialog->hide();
slotCurrentJailChanged();
}
void dialogWarden::runCommand( QString command )
{
FILE *file = popen(command.toLatin1(),"r");
fclose(file);
}
void dialogWarden::slotReadDisplayOutput()
{
while ( exportJailProc->canReadLine() )
{
dialogOutput->appendDialogText(exportJailProc->readLine());
}
}
void dialogWarden::slotFinishedOutput()
{
// Hide the output dialog, not needed anymore
dialogOutput->hide();
}
void dialogWarden::slotReadPkgsOutput()
{
while ( listPackagesProc->canReadLine() )
{
dialogOutput->appendDialogText(listPackagesProc->readLine());
}
}
void dialogWarden::slotClickedNewJail()
{
QStringList uH, uI;
// Launch our working dialog to prevent further action until we are finished
newJailWizard = new wizardNewJail();
newJailWizard->setModal(true);
newJailWizard->programInit();
// Build list of used jail IP / Hosts
QTreeWidgetItemIterator it( listJails );
while (*it) {
uI << (*it)->text(0);
uH << (*it)->text(2);
++it;
}
newJailWizard->setHostIPUsed(uH, uI);
connect( newJailWizard, SIGNAL(create(const QString &, const QString &, const QString &, bool, bool, const QString &, bool, bool, bool, bool, const QString &, const QString &)), this, SLOT(slotCreateNewJail(const QString &, const QString &, const QString &, bool, bool, const QString &, bool, bool, bool, bool, const QString &, const QString &) ) );
newJailWizard->show();
}
void dialogWarden::slotCreateNewJail( const QString &IP, const QString &IP6, const QString &host, bool tradjail, bool PCUtils, const QString &rootpw, bool src, bool ports, bool autostart, bool linuxJail, const QString &linuxScript, const QString &tplate )
{
if ( tradjail || linuxJail )
newRootPW=rootpw;
else
newRootPW="";
newHost=host;
/***********************************************************************************************************************
Now create the jail here
*************************************************************************************************************************/
dialogOutput = new dialogDisplayOutput();
dialogOutput->setModal(false);
dialogOutput->programInit(FALSE);
dialogOutput->setDialogCaption("Creating Jail: " + IP);
dialogOutput->setDialogText("");
dialogOutput->show();
// Now start the script to stop this jail
createJailProc = new QProcess( this );
QString program = "warden";
QStringList args;
args << "create" << host;
// Set our optional flags
if ( ! IP.isEmpty() ) {
args << "--ipv4";
args << IP;
}
if ( ! IP6.isEmpty() ) {
args << "--ipv6";
args << IP6;
}
if ( ! linuxJail && ! tplate.isEmpty() )
args << "--template" << tplate.section(" ", 0, 0);
if ( tradjail && ! PCUtils )
args << "--vanilla";
if ( ! tradjail && ! linuxJail )
args << "--portjail";
if ( src && ! linuxJail)
args << "--src";
if ( ports && ! linuxJail )
args << "--ports";
if ( linuxJail )
args << "--linuxjail" << linuxScript ;
if ( autostart )
args << "--startauto";
// Connect the exited signal and start the process
createJailProc->setProcessChannelMode(QProcess::MergedChannels);
createJailProc->setReadChannel(QProcess::StandardOutput);
connect( createJailProc, SIGNAL(readyReadStandardOutput ()), this, SLOT(slotReadCreateOutput() ) );
connect( createJailProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotFinishedJailCreate() ) );
createJailProc->start(program, args);
}
void dialogWarden::slotReadCreateOutput()
{
while ( createJailProc->canReadLine() )
{
dialogOutput->appendDialogText(createJailProc->readLine());
}
}
void dialogWarden::slotFinishedJailCreate()
{
// If this doesn't need a root pw, or doesn't have one
if ( newRootPW.isEmpty() ) {
slotFinishedUserAdd();
return;
}
// Jail is done creating, now set rootpw
userAddProc = new QProcess( this );
QString program = ProgDir + "/scripts/backend/setupusers.sh";