Merge pull request #1 from ldemouy/master

Adds in the LookUpPort function call as well as the basic framework for a new sysadm-firewall management class.
This commit is contained in:
Ken Moore
2016-01-11 14:27:45 -05:00
3 changed files with 155 additions and 3 deletions

View File

@@ -13,13 +13,14 @@ VERSION = 1.0.0
HEADERS += sysadm-global.h \
sysadm-general.h \
sysadm-lifepreserver.h \
sysadm-network.h
sysadm-network.h \
sysadm-firewall.h
SOURCES += sysadm-general.cpp \
sysadm-lifepreserver.cpp \
sysadm-network.cpp \
NetDevice.cpp
NetDevice.cpp \
sysadm-firewall.cpp
include.path=/usr/local/include/
include.files=sysadm-*.h

View File

@@ -0,0 +1,106 @@
#include "sysadm-firewall.h"
#include <QtCore>
using namespace sysadm;
PortInfo Firewall::LookUpPort(int portNumber, QString portType)
{
//Make sure that the port is valid
if (portNumber < 0 || portNumber > 65535)
{
PortInfo returnValue;
returnValue.Port = -1;
returnValue.Description = "Port out of bounds";
return returnValue;
}
//Check to see if things have been initialized
if(portStrings == NULL)
readServicesFile();
PortInfo returnValue;
//the port number is valid so set it
returnValue.Port = portNumber;
//make sure that the portType is cased in lower to match the service file and
//then store it in the returnValue, since there isn't a huge point in checking
//the validitiy of the type since /etc/services lists more than udp/tcp
portType = portType.toLower();
returnValue.PortType = portType;
//Check to see if it's a recommended port
returnValue.Recommended = false;
for(int i = 0; i < recommendedPortsSize; i++)
{
if (portNumber == recommendedPorts[i])
{
returnValue.Recommended = true;
}
}
//Check to see if the port number is listed. The format in the file
// is portname/portType. ex.: 22/tcp
QStringList port = portStrings->filter(QString::number(portNumber) + "/" + portType);
if(port.size() > 0)
{
//grab the first one, there may be duplicates due to colliding ports in the /etc/services file
//but those are listed after the declaration for what the port officially should be used for
QString line = port.at(0);
//Split across spaces since it's whitespace delimited
QStringList lineList = line.split(' ');
//the keyword associated with the port is the first element in a line
returnValue.Keyword = lineList.at(0);
//if the size of the list is less than 3 then there is no description
if(lineList.size() > 2)
{
QString description = lineList.at(2);
//String the description back together from the end of the list
for(int i = 3; i < lineList.size(); i++)
{
description += " " + lineList.at(i);
}
returnValue.Description = description;
}
}
return returnValue;
}
Firewall::Firewall()
{
readServicesFile();
}
Firewall::~Firewall()
{
delete portStrings;
}
void Firewall::readServicesFile()
{
portStrings = new QStringList();
// /etc/services contains a file that lists the various port numbers
// and their descriptions
QFile* services = new QFile("/etc/services");
services->open(QFile::ReadOnly);
while(!services->atEnd())
{
QString line = services->readLine();
//jump down past the comments
if(line[0] == '#')
continue;
//remove all of the extraneous whitespace in the line
line = line.simplified();
portStrings->append(line);
}
services->close();
delete services;
}

View File

@@ -0,0 +1,45 @@
//===========================================
// PC-BSD source code
// Copyright (c) 2015, PC-BSD Software/iXsystems
// Available under the 3-clause BSD license
// See the LICENSE file for full details
#ifndef PORTLOOKUP_H
#define PORTLOOKUP_H
#include <QtCore>
namespace sysadm
{
struct PortInfo{
int Port;
QString PortType;
QString Keyword;
QString Description;
bool Recommended;
};
const static int recommendedPorts[] = {22, 80};
const static int recommendedPortsSize = 2;
class Firewall
{
public:
/**
* @description Returns a structure containing information about the port
* including its port type, keyword, description, and whether it's a
* recommended port
*
* @parameter portNumber a port number between 0 and 2^16 - 1
*
* @ErrorConditions Port Number is set to -1 and a description of the error is stored in the description variable
*/
PortInfo LookUpPort(int portNumber, QString portType);
Firewall();
~Firewall();
private:
void readServicesFile();
QStringList* portStrings;
};
}
#endif // PORTLOOKUP_H