-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
163 lines (145 loc) · 5.91 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "basescorechecker.h"
#include <QFileDialog>
#include "runcommandscorechecker.h"
#include "emptyscorechecker.h"
#include "configpropertieswindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//confmen
statusBar()->showMessage("Program loaded.");
//RunCommandScoreChecker testcheck;
//testcheck.setCommand("ls");
//testcheck.executeCommand();
//std::cout << testcheck.getCommandOutput();
TestScoreCheckers();
//config= new ScoreCheckingConfig;
//config->vecScoreCheckers->push_back(new PathExistScoreChecker());
//config->vecScoreCheckers->push_back(new PathExistScoreChecker());
//config->vecScoreCheckers->push_back(new EmptyScoreChecker());
scoringmodel= new MyScoringModel(this, config->vecScoreCheckers);
scoringdelegate= new MyDelegate(this);
//scoringmodel->set
SetupTable();
timer= new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(scoreupdate())); //tell timer to execute scoreupdate()
}
void MainWindow::SetupTable()
{
ui->mainTable->setModel(scoringmodel); //scoringmodel);
ui->mainTable->setItemDelegate(scoringdelegate);
//ui->mainTable->setEditTriggers(QAbstractItemView::AllEditTriggers);
QHeaderView *verticalHeader = ui->mainTable->verticalHeader();
verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(30);
QHeaderView *horizontalHeader = ui->mainTable->horizontalHeader();
//horizontalHeader->setSectionResizeMode(QHeaderView::Fixed);
horizontalHeader->setDefaultSectionSize(60);
//ui->mainTable->setHorizontalHeader(QHeaderView());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionExit_triggered()
{
this->destroy(true, true);
this->close();
}
void MainWindow::on_actionInsert_Blank_triggered()
{
//this->ui->mainTable->dataChanged(this->scoringmodel->index(this->config->vecScoreCheckers->size()-1, 0, QModelIndex()),this->scoringmodel->index(this->config->vecScoreCheckers->size()-1, 6, QModelIndex()));
//this->config->vecScoreCheckers->push_back(new EmptyScoreChecker());
//scoringmodel->refresh(this->config->vecScoreCheckers->size()-1);
scoringmodel->insertChecker(new EmptyScoreChecker());
}
void MainWindow::TestScoreCheckers() //This is all a test. Not for production...
{
ScoreCheckingConfig configtosave;
PathExistScoreChecker pathcheck;
saveConfigXML(configtosave,"config_empty.xml");
configtosave.vecScoreCheckers->push_back(&pathcheck);
pathcheck.setFilepath("/etc/shadow");
pathcheck.setDesireExist(true);
pathcheck.checkState();
std::cout << "PathCheck returned " << pathcheck.getState() << std::endl;
RunCommandScoreChecker commandcheck;
configtosave.vecScoreCheckers->push_back(&commandcheck);
commandcheck.setCommand("sysctl net.ipv4.ip_forward");
commandcheck.setSearchString("net.ipv4.ip_forward = 0");
commandcheck.setSearchExist(false);
commandcheck.executeCommand();
commandcheck.checkState();
std::cout << "CommandCheck returned " << commandcheck.getState() << std::endl;
ValueScoreChecker valuecheck;
configtosave.vecScoreCheckers->push_back(&valuecheck);
valuecheck.setFilepath("/etc/ssh/sshd_config");
valuecheck.setDesireExist(true);
valuecheck.setSearchString("\nPermitRootLogin no");
valuecheck.checkState();
std::cout << "ValueCheck returned " << valuecheck.getState() << std::endl;
ScriptScoreChecker scriptcheck;
configtosave.vecScoreCheckers->push_back(&scriptcheck);
scriptcheck.setScript("#!/bin/bash\n"
"ls\n"
"lsattr /etc/passwd\n"
"#ps\n");
scriptcheck.setSearchString("--------------e---- /etc/passwd");
scriptcheck.setDesiredState(true);
scriptcheck.checkState();
std::cout << "ScriptCheck returned " << scriptcheck.getState() << std::endl;
saveConfigXML(configtosave,"config.xml");
saveConfigBIN(configtosave,"config.bin");
ScoreCheckingConfig loadedconfig;
this->config=new ScoreCheckingConfig();
loadConfigBIN(*config, "config.bin");
std::cout << "Loaded " << config->Name << std::endl;
std::cout << "SCript" << std::endl << static_cast<ScriptScoreChecker*>(config->vecScoreCheckers->at(3))->getScript() << std::endl;
this->on_actionOpen_Config_triggered();
}
void MainWindow::on_actionConfiguration_Prefs_triggered()
{
ConfigPropertiesWindow *confmenu= new ConfigPropertiesWindow(0,this->config);
confmenu->show();
}
void MainWindow::on_actionSave_Config_triggered()
{
QFileDialog savedialog(0, tr("Save Config"));
QString filename=savedialog.getSaveFileName(0,tr("Save Config"),tr("*.bin"));
saveConfigBIN(*config, filename.toStdString().c_str());
}
void MainWindow::on_actionOpen_Config_triggered()
{
QFileDialog opendialog(0, tr("Open Config"));
QString filename=opendialog.getOpenFileName(0,tr("Open Config"),tr("*.bin"));
delete this->config;
this->config=new ScoreCheckingConfig();
loadConfigBIN(*config, filename.toStdString().c_str());
scoringmodel= new MyScoringModel(this, config->vecScoreCheckers);
scoringdelegate= new MyDelegate(this);
this->SetupTable();
}
void MainWindow::scoreupdate()
{
std::cout << "Times up" << std::endl;
config->GenerateScoreReport();
}
void MainWindow::on_actionTimer_Start_toggled(bool arg1)
{
if (arg1){if (this->config->checkSeconds==0)this->config->checkSeconds=5;this->timer->start(this->config->checkSeconds*1000);}else{this->timer->stop();}
}
void MainWindow::on_actionNew_Config_triggered()
{
if (true) //ask first before erasing
{
delete this->config;
this->config=new ScoreCheckingConfig();
scoringmodel= new MyScoringModel(this, config->vecScoreCheckers);
scoringdelegate= new MyDelegate(this);
this->SetupTable();
}
}