Switch openports from being a nonintuitive string to using our PortInfo

struct and add comments to the /etc/ipfw.openports file when we write to
it.

Enable C++11 in the library.pro file as it's required for std::sort and
std::tie, since qSort is deprecated as of Qt 5.3
This commit is contained in:
Luke De Mouy
2016-01-11 19:28:02 -07:00
parent 17d2c131b5
commit 20ee1eaf62
3 changed files with 26 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
CONFIG += c++11
QT += core network
TARGET=sysadm

View File

@@ -6,6 +6,9 @@
#include "sysadm-firewall.h"
#include <QtCore>
#include <algorithm>
#include <tuple>
using namespace sysadm;
PortInfo Firewall::LookUpPort(int portNumber, QString portType)
{
@@ -78,24 +81,19 @@ PortInfo Firewall::LookUpPort(int portNumber, QString portType)
void Firewall::OpenPort(int port, QString type)
{
openports << QString::number(port)+"::::"+type;
openports.append(LookUpPort(port,type));
SaveOpenPorts();
}
void Firewall::ClosePort(int port, QString type)
{
openports.removeAll( QString::number(port)+"::::"+type);
openports.removeAll(LookUpPort(port,type));
SaveOpenPorts();
}
QVector<PortInfo> Firewall::OpenPorts()
{
QVector<PortInfo> returnValue = QVector<PortInfo>(openports.length());
for(int i=0; i<openports.length(); i++){
returnValue.append( LookUpPort(openports[i].section("::::",0,0).toInt(),openports[i].section("::::",1,1)));
}
return returnValue;
return openports;
}
@@ -161,7 +159,7 @@ void Firewall::readServicesFile()
{
QString line = services->readLine();
//jump down past the comments
if(line[0] == '#')
if(line[0] == '#' || line.simplified().isEmpty())
continue;
//remove all of the extraneous whitespace in the line
@@ -183,20 +181,22 @@ void Firewall::LoadOpenPorts()
QString line = in.readLine();
if(line.startsWith("#") || line.simplified().isEmpty()){ continue; }
//File format: "<type> <port>" (nice and simple)
openports << line.section(" ",1,1)+"::::"+line.section(" ",0,0);
openports.append(LookUpPort(line.section(" ",1,1).toInt(),line.section(" ",0,0)));
}
file.close();
}
openports.sort(); //order them in ascending port order
//order them in ascending order by port then port type
std::sort(openports.begin(),openports.end());
}
void Firewall::SaveOpenPorts()
{
//Convert to file format
openports.sort(); //make sure they are still sorted by port
std::sort(openports.begin(), openports.end()); //make sure they are still sorted by port
QStringList fileout;
for(int i=0; i<openports.length(); i++){
fileout << openports[i].section("::::",1,1)+" "+openports[i].section("::::",0,0);
fileout.append("#" + openports[i].Keyword + ": " + openports[i].Description + "\n" +
openports[i].PortType +" "+QString::number(openports[i].Port));
}
//Always make sure that the file always ends with a newline
if(!fileout.isEmpty()){ fileout << ""; }

View File

@@ -15,6 +15,17 @@ struct PortInfo{
QString Keyword;
QString Description;
bool Recommended;
friend bool operator<(const PortInfo lhs, const PortInfo rhs){
return std::tie(lhs.Port,lhs.PortType) < std::tie(rhs.Port,rhs.PortType);
}
friend bool operator>(const PortInfo lhs, const PortInfo rhs)
{ return rhs < lhs;}
friend bool operator==(const PortInfo lhs, const PortInfo rhs)
{
return lhs.Port == rhs.Port && lhs.PortType == rhs.PortType;
}
friend bool operator !=(const PortInfo lhs, const PortInfo rhs)
{ return !(lhs == rhs);}
};
const static int recommendedPorts[] = {22, 80};
@@ -91,7 +102,7 @@ private:
void readServicesFile();
QStringList* portStrings;
QStringList openports;
QVector<PortInfo> openports;
void LoadOpenPorts();
void SaveOpenPorts();