1
0
mirror of https://github.com/fwbuilder/fwbuilder synced 2026-03-18 17:27:20 +01:00

per Mikes request, the GUI now opens rule set object of the firewall

when user opens the firewall or any of its children objects in the
editor. This includes any rule set, interface or any other child
object.

Also moved function Interface::getParentHost() from class Interface to
class Host as static FWObject* Host::getParentHost(FWObject *o). Its
behavior is now more logical, it returns non-NULL pointer only if an
object actually has a parent that is host, firewall or cluster. Old
function Interface::getParentHost() returned non-NULL pointer even
when object was not in the proper position in the tree.

Now this function can be used to get parent host/firewall/cluster for
any child object rather than only interface.

I had to fix bunch of problems in unit tests that got uncovered because
new function behaves in a more strict way (returns NULL when there is
no parent of correct type).
This commit is contained in:
Vadim Kurland 2011-06-05 21:38:46 -07:00
parent c171994c82
commit 3a2041d16d
31 changed files with 160 additions and 113 deletions

View File

@ -941,7 +941,8 @@ void CompilerDriver_pix::pixClusterGroupChecks(ClusterGroup *cluster_group)
{
Interface *member_iface = Interface::cast(FWObjectReference::getObject(*it));
assert(member_iface);
FWObject *member = member_iface->getParentHost();
FWObject *member = Host::getParentHost(member_iface);
//FWObject *member = member_iface->getParentHost();
if (cluster_interface)
{

View File

@ -170,7 +170,8 @@ bool interfaceProperties::manageIpAddresses(Interface *intf,
update_addresses.clear();
ignore_addresses.clear();
FWObject *fw = intf->getParentHost();
FWObject *fw = Host::getParentHost(intf);
//FWObject *fw = intf->getParentHost();
Resources *os_res = Resources::os_res[fw->getStr("host_OS")];
assert(os_res != NULL);
@ -270,7 +271,8 @@ bool interfaceProperties::validateInterface(FWObject *target,
Interface::cast(target)->getOptionsObject()->getStr("type");
if (target_interface_type.empty()) target_interface_type = "ethernet";
FWObject *fw = Interface::cast(target)->getParentHost();
FWObject *fw = Host::getParentHost(target);
//FWObject *fw = Interface::cast(target)->getParentHost();
QString host_os = fw->getStr("host_OS").c_str();
Resources* os_res = Resources::os_res[host_os.toStdString()];
list<string> interface_type_pairs;
@ -437,7 +439,8 @@ bool interfaceProperties::isEligibleForCluster(Interface *intf)
parent_iface->getOptionsObject()->getStr("type") == "bridge")
return false;
FWObject *fw = intf->getParentHost();
FWObject *fw = Host::getParentHost(intf);
//FWObject *fw = intf->getParentHost();
list<FWObject*> interfaces = fw->getByTypeDeep(Interface::TYPENAME);
list<FWObject*>::iterator i;
for (i=interfaces.begin(); i!=interfaces.end(); ++i )
@ -469,7 +472,8 @@ void interfaceProperties::guessSubInterfaceTypeAndAttributes(Interface *intf)
if (parent_intf == NULL)
return;
FWObject *f = intf->getParentHost();
FWObject *f = Host::getParentHost(intf);
//FWObject *f = intf->getParentHost();
// Resources* os_res = Resources::os_res[f->getStr("host_OS")];
// string os_family = f->getStr("host_OS");

View File

@ -2128,7 +2128,8 @@ bool NATCompiler_ipt::AssignInterface::processNext()
if (iface)
{
if (Cluster::isA(iface->getParentHost()))
FWObject *parent_host = Host::getParentHost(iface);
if (Cluster::isA(parent_host))
{
if (iface->isFailoverInterface())

View File

@ -159,7 +159,8 @@ void expand_interface_with_phys_address(Compiler *compiler,
* we use physAddress only if Host option "use_mac_addr_filter" of the
* parent Host object is true
*/
FWObject *p = iface->getParentHost();
FWObject *p = Host::getParentHost(iface);
//FWObject *p = iface->getParentHost();
assert(p!=NULL);
FWOptions *hopt = Host::cast(p)->getOptionsObject();

View File

@ -73,7 +73,7 @@ xmlNodePtr AttachedNetworks::toXML(xmlNodePtr parent) throw(FWException)
void AttachedNetworks::addNetworkObject(const InetAddr *ip_addr,
const InetAddr *ip_netm)
{
FWObject *new_obj;
FWObject *new_obj = NULL;
if (ip_addr->isV4())
{

View File

@ -254,7 +254,7 @@ void Cluster::getMembersList(list<libfwbuilder::Firewall*> &members)
// as of 05/04 members of StateSyncClusterGroup are interfaces. See
// tickets #10 and #11
if (Interface::cast(member))
fw = Firewall::cast(Interface::cast(member)->getParentHost());
fw = Firewall::cast(Host::getParentHost(member));
else
fw = Firewall::cast(member);
members_ids.insert(fw->getId());
@ -288,7 +288,7 @@ bool Cluster::hasMember(Firewall *fw)
// as of 05/04/2009 members of StateSyncClusterGroup are
// interfaces. See tickets #10 and #11
if (Interface::cast(member))
member_fw = Firewall::cast(Interface::cast(member)->getParentHost());
member_fw = Firewall::cast(Host::getParentHost(member));
else
member_fw = Firewall::cast(member);
if (fw == member_fw) return true;

View File

@ -1593,3 +1593,5 @@ bool FWObjectNameCmpPredicate::operator()(FWObject *a, FWObject *b)
FWObject *o2 = (follow_references) ? FWReference::getObject(b) : b;
return o1->getName() < o2->getName();
}

View File

@ -168,3 +168,16 @@ int Host::countInetAddresses(bool skip_loopback) const
return res;
}
/*
* This function will find parent host, firewall or cluster object of
* a given object. If object is not a child of host, firewall or
* cluster, it returns NULL
*/
FWObject* Host::getParentHost(FWObject *obj)
{
FWObject *parent_h = obj;
while (parent_h != NULL && Host::cast(parent_h) == NULL)
parent_h = parent_h->getParent();
return parent_h;
}

View File

@ -97,6 +97,15 @@ class Host : public Address
virtual bool isPrimaryObject() const { return true; }
/**
* helper-function, needed when dealing with sub-interfaces:
* function returns the parent host (or firewall) of an interface
* or rule set. This is just a convenience function that performs
* operation we often need.
*/
static FWObject* getParentHost(FWObject *obj);
protected:
Management *mgmt;

View File

@ -31,6 +31,7 @@
#include <fwbuilder/XMLTools.h>
#include <fwbuilder/IPv4.h>
#include <fwbuilder/IPv6.h>
#include <fwbuilder/Host.h>
#include <fwbuilder/AttachedNetworks.h>
#include <fwbuilder/FWObjectDatabase.h>
#include <fwbuilder/Resources.h>
@ -283,9 +284,10 @@ FWOptions* Interface::getOptionsObject()
add(iface_opt);
// set default interface options
if (this->getParentHost() != NULL)
const FWObject *parent_host = Host::getParentHost(this);
if (parent_host != NULL)
{
const string host_OS = this->getParentHost()->getStr("host_OS");
const string host_OS = parent_host->getStr("host_OS");
try
{
Resources::setDefaultIfaceOptions(host_OS, this);
@ -426,17 +428,6 @@ bool Interface::isLoopback() const
return false;
}
FWObject* Interface::getParentHost() const
{
FWObject *p = this->getParent();
if (!Interface::isA(p)) {
return p;
} else {
p = p->getParent();
}
return p;
}
physAddress* Interface::getPhysicalAddress () const
{
return physAddress::cast( getFirstByType( physAddress::TYPENAME ) );

View File

@ -100,12 +100,6 @@ public:
DECLARE_DISPATCH_METHODS(Interface);
/**
* helper-function, needed when dealing with sub-interfaces: function
* returns the parent host (or firewall) of an interface.
*/
FWObject* getParentHost() const;
/**
* each interface must be associated with some security level. Level
* is described by interger number between 0 and 100, with 0 being

View File

@ -461,7 +461,8 @@ void Compiler::_expand_interface(Rule *rule,
* we use physAddress only if Host option "use_mac_addr_filter" of the
* parent Host object is true
*/
FWObject *p = iface->getParentHost();
FWObject *p = Host::getParentHost(iface);
//FWObject *p = iface->getParentHost();
Host *hp = Host::cast(p);
if (hp==NULL) return; // something is very broken
FWOptions *hopt = hp->getOptionsObject();

View File

@ -519,7 +519,8 @@ bool RoutingCompiler::rItfChildOfFw::processNext()
Interface *iface = Interface::cast(o);
if (iface)
{
FWObject *parent = iface->getParentHost();
FWObject *parent = Host::getParentHost(iface);
//FWObject *parent = iface->getParentHost();
if (parent->getId() == compiler->fw->getId()) return true;
Cluster *cluster = Cluster::cast(parent);

View File

@ -205,7 +205,8 @@ void ClusterGroupDialog::addIcon(FWObject *o, bool master)
{
FWObject *iface = o;
assert(Interface::cast(iface)!=NULL);
FWObject *fw = Interface::cast(iface)->getParentHost(); // because iface can be subinterface
FWObject *fw = Host::getParentHost(iface);
// FWObject *fw = Interface::cast(iface)->getParentHost(); // because iface can be subinterface
bool valid = cluster->validateMember(Firewall::cast(fw));
QString iface_name = QString::fromUtf8(iface->getName().c_str());
QString fw_name = QString::fromUtf8(fw->getName().c_str());

View File

@ -181,8 +181,8 @@ ClusterInterfaceData ClusterInterfaceWidget::getInterfaceData()
{
QTreeWidgetItem *item = ifacelist.list->selectedItems().first();
Interface* iface = item->data(0, Qt::UserRole).value<Interface*>();
res.interfaces.append(
qMakePair(Firewall::cast(iface->getParentHost()), iface));
FWObject *parent_fw = Host::getParentHost(iface);
res.interfaces.append(qMakePair(Firewall::cast(parent_fw), iface));
}
return res;
}

View File

@ -290,7 +290,8 @@ QWidget *DialogFactory::createOSDialog(QWidget *parent,FWObject *o)
QWidget *DialogFactory::createIfaceDialog(QWidget *parent,FWObject *o)
throw(FWException)
{
FWObject *h = Interface::cast(o)->getParentHost();
FWObject *h = Host::getParentHost(o);
//FWObject *h = Interface::cast(o)->getParentHost();
string host_OS = h->getStr("host_OS");
Resources *os = Resources::os_res[host_OS];

View File

@ -176,44 +176,49 @@ QString FWObjectPropertiesFactory::getObjectPropertiesBrief(FWObject *obj)
if (!obj->isReadOnly()) intf->getOptionsObject();
str << intf->getLabel().c_str() << " ";
FWObject *parent = intf->getParentHost();
bool supports_security_levels = false;
bool supports_network_zones = false;
try
{
supports_security_levels =
(!parent->getStr("platform").empty() &&
Resources::getTargetCapabilityBool(
parent->getStr("platform"), "security_levels"));
supports_network_zones =
(!parent->getStr("platform").empty() &&
Resources::getTargetCapabilityBool(
parent->getStr("platform"), "network_zones"));
} catch (FWException &ex) { }
QStringList q;
if (supports_security_levels)
{
QString str;
str.setNum(intf->getSecurityLevel());
q.push_back(QString("sec level: %1").arg(str));
}
if (supports_network_zones)
{
int id = FWObjectDatabase::getIntId(intf->getStr("network_zone"));
if (id > 0)
{
FWObject *nz_obj = obj->getRoot()->findInIndex(id);
if (nz_obj)
q.push_back(
QString("network zone: %1")
.arg(nz_obj->getName().c_str()));
else
q.push_back(QString("network zone: not configured"));
FWObject *parent = Host::getParentHost(intf);
//FWObject *parent = intf->getParentHost();
if (parent)
{
bool supports_security_levels = false;
bool supports_network_zones = false;
try
{
supports_security_levels =
(!parent->getStr("platform").empty() &&
Resources::getTargetCapabilityBool(
parent->getStr("platform"), "security_levels"));
supports_network_zones =
(!parent->getStr("platform").empty() &&
Resources::getTargetCapabilityBool(
parent->getStr("platform"), "network_zones"));
} catch (FWException &ex) { }
if (supports_security_levels)
{
QString str;
str.setNum(intf->getSecurityLevel());
q.push_back(QString("sec level: %1").arg(str));
}
if (supports_network_zones)
{
int id = FWObjectDatabase::getIntId(intf->getStr("network_zone"));
if (id > 0)
{
FWObject *nz_obj = obj->getRoot()->findInIndex(id);
if (nz_obj)
q.push_back(
QString("network zone: %1")
.arg(nz_obj->getName().c_str()));
else
q.push_back(QString("network zone: not configured"));
}
}
}
if (intf->isDyn()) q.push_back("dyn");
if (intf->isUnnumbered()) q.push_back("unnum");
if (intf->isDedicatedFailover()) q.push_back("failover");

View File

@ -176,7 +176,10 @@ void FWWindow::openEditor(FWObject *obj)
// firewall and if a ruleset visible in RuleSetView belongs to
// another firewall, switch ruleset to the ruleset of the new
// firewall which we looked at last time.
if (Firewall::cast(obj) != NULL) // this includes Cluster
//
FWObject *parent_fw = Host::getParentHost(obj);
if (parent_fw != NULL) // this includes Cluster
{
RuleSetView* rsv = activeProject()->getCurrentRuleSetView();
if (rsv)
@ -191,7 +194,8 @@ void FWWindow::openEditor(FWObject *obj)
if (obj != current_ruleset->getParent())
{
FWObject *old_rs =
activeProject()->m_panel->om->findInHistoryByParent(obj);
activeProject()->m_panel->om->findRuleSetInHistoryByParentFw(
parent_fw);
if (old_rs == NULL)
old_rs = obj->getFirstByType(Policy::TYPENAME);

View File

@ -122,9 +122,10 @@ void InterfaceDialog::loadFWObject(FWObject *o)
* something relevant in the interface to complement their changes
* and right after the interface has been created.
*/
FWObject *parent_host = Host::getParentHost(s);
interfaceProperties *int_prop =
interfacePropertiesObjectFactory::getInterfacePropertiesObject(
s->getParentHost());
parent_host);
int_prop->guessSubInterfaceTypeAndAttributes(s);
delete int_prop;
}
@ -197,7 +198,8 @@ void InterfaceDialog::loadFWObject(FWObject *o)
m_dialog->bridge_port_label->hide();
}
FWObject *f = s->getParentHost();
FWObject *f = Host::getParentHost(s);
//FWObject *f = s->getParentHost();
m_dialog->advancedconfig->setEnabled(true);
@ -329,9 +331,10 @@ void InterfaceDialog::validate(bool *res)
return;
}
FWObject *parent_host = Host::getParentHost(obj);
interfaceProperties *int_prop =
interfacePropertiesObjectFactory::getInterfacePropertiesObject(
Interface::cast(obj)->getParentHost());
parent_host);
QString err;
/*
@ -448,7 +451,8 @@ void InterfaceDialog::applyChanges()
// NOTE: new_state is a copy of the interface but it is not attached to
// the tree and therefore has no parent. Need to use original object obj
// to get the pointer to the parent firewall.
FWObject *f = Interface::cast(obj)->getParentHost();
FWObject *f = Host::getParentHost(obj);
//FWObject *f = Interface::cast(obj)->getParentHost();
bool supports_security_levels = false;
bool supports_network_zones = false;
bool supports_unprotected = false;
@ -503,9 +507,10 @@ void InterfaceDialog::applyChanges()
{
// ticket #328: automatically assign vlan id to interface based on
// interface name
FWObject *parent_host = Host::getParentHost(obj);
interfaceProperties *int_prop =
interfacePropertiesObjectFactory::getInterfacePropertiesObject(
Interface::cast(obj)->getParentHost());
parent_host);
int_prop->setPerformVlanChecks(true);
int_prop->guessSubInterfaceTypeAndAttributes(intf);
delete int_prop;

View File

@ -489,7 +489,8 @@ void ObjectManipulator::contextMenuRequested(const QPoint &pos)
if (Interface::isA(currentObj) && ! currentObj->isReadOnly())
{
Interface *iface = Interface::cast(currentObj);
FWObject *h = iface->getParentHost();
FWObject *h = Host::getParentHost(iface);
//FWObject *h = iface->getParentHost();
bool supports_advanced_ifaces = false;
try {

View File

@ -87,15 +87,6 @@ public:
};
class FindHistoryItemByParentObjectId
{
int id;
public:
FindHistoryItemByParentObjectId(int i) { id = i; }
bool operator()(const HistoryItem &itm);
};
class ObjectManipulator : public QWidget
{
Q_OBJECT;
@ -334,7 +325,8 @@ public:
* parent. Used to find which rule set of the firewall user
* looked at last.
*/
libfwbuilder::FWObject* findInHistoryByParent(libfwbuilder::FWObject* parent);
libfwbuilder::FWObject* findRuleSetInHistoryByParentFw(
libfwbuilder::FWObject* parent);
void expandObjectInTree(libfwbuilder::FWObject *obj);

View File

@ -667,7 +667,8 @@ FWObject* ObjectManipulator::newAttachedNetworks(QUndoCommand* macro)
{
FWObject *no = createObject(currentObj, AttachedNetworks::TYPENAME,
tr("Attached Networks"), NULL, macro);
string name = Interface::cast(currentObj)->getParentHost()->getName() +
FWObject *parent_host = Host::getParentHost(currentObj);
string name = parent_host->getName() +
":" + currentObj->getName() + ":attached";
no->setName(name);
return no;
@ -734,7 +735,8 @@ FWObject* ObjectManipulator::newInterface(QUndoCommand* macro)
if (Interface::isA(currentObj))
{
FWObject *h = Interface::cast(currentObj)->getParentHost();
FWObject *h = Host::getParentHost(currentObj);
//FWObject *h = Interface::cast(currentObj)->getParentHost();
bool supports_advanced_ifaces = false;
supports_advanced_ifaces =
@ -764,9 +766,10 @@ FWObject* ObjectManipulator::newInterface(QUndoCommand* macro)
if (Interface::isA(parent))
{
FWObject *parent_host = Host::getParentHost(parent);
interfaceProperties *int_prop =
interfacePropertiesObjectFactory::getInterfacePropertiesObject(
Interface::cast(parent)->getParentHost());
parent_host);
int_prop->guessSubInterfaceTypeAndAttributes(new_interface);
delete int_prop;
//guessSubInterfaceTypeAndAttributes(new_interface);

View File

@ -351,22 +351,17 @@ bool FindHistoryItemByObjectId::operator()(const HistoryItem &itm)
return (itm.id() == id);
}
bool FindHistoryItemByParentObjectId::operator()(const HistoryItem &itm)
FWObject* ObjectManipulator::findRuleSetInHistoryByParentFw(FWObject* parent)
{
FWObject *obj = mw->activeProject()->db()->findInIndex(itm.id());
FWObject *parent = obj->getParent();
return (parent != NULL && parent->getId() == id);
}
FWObject* ObjectManipulator::findInHistoryByParent(FWObject* parent)
{
FindHistoryItemByParentObjectId pred(parent->getId());
list<HistoryItem>::reverse_iterator it =
std::find_if(history.rbegin(), history.rend(), pred);
if (it != history.rend())
list<HistoryItem>::reverse_iterator it = history.rbegin();
for (; it!=history.rend(); ++it)
{
return m_project->db()->findInIndex(it->id());
FWObject *obj = mw->activeProject()->db()->findInIndex(it->id());
if (RuleSet::cast(obj))
{
FWObject *parent_fw = Host::getParentHost(obj);
if (parent_fw != NULL && parent_fw == parent) return obj;
}
}
return NULL;

View File

@ -122,7 +122,8 @@ void clusterMembersDialog::getSelectedMembers()
Interface *iface = NULL;
iface = Interface::cast(FWReference::cast((*it))->getPointer());
assert(iface != NULL);
Firewall *fw = Firewall::cast(iface->getParentHost());
Firewall *fw = Firewall::cast(Host::getParentHost(iface));
//Firewall *fw = Firewall::cast(iface->getParentHost());
// determine master
std::string iface_id = FWObjectDatabase::getStringId(iface->getId());

View File

@ -541,7 +541,8 @@ void getInterfaceTypes(Interface *iface, list<QStringPair> &res)
*/
void getSubInterfaceTypes(Interface *iface, list<QStringPair> &res)
{
FWObject *p = iface->getParentHost();
FWObject *p = Host::getParentHost(iface);
//FWObject *p = iface->getParentHost();
assert(p!=NULL);
QString host_os = p->getStr("host_OS").c_str();
@ -583,7 +584,8 @@ void setInterfaceTypes(QComboBox *iface_type,
// Note that if resource file says this subint can not be vlan, we
// dan't return vlan type on the list even if its name looks like
// it could be one.
FWObject *p = iface->getParentHost();
FWObject *p = Host::getParentHost(iface);
//FWObject *p = iface->getParentHost();
assert(p!=NULL);
QString host_os = p->getStr("host_OS").c_str();
QString obj_name = iface->getName().c_str();

View File

@ -365,9 +365,15 @@ void interfacePropertiesTest::isEligibleForCluster()
CPPUNIT_ASSERT(int_prop != NULL);
Firewall *fw1 = Firewall::cast(db->create(Firewall::TYPENAME));
fw1->setName("iface");
fw1->setStr("host_OS", "unknown");
db->add(fw1);
Interface *parent1 = Interface::cast(db->create(Interface::TYPENAME));
Interface *iface1 = Interface::cast(db->create(Interface::TYPENAME));
db->add(parent1);
fw1->add(parent1);
parent1->add(iface1);
iface1->getOptionsObject()->setStr("type", "ethernet");

View File

@ -239,11 +239,14 @@ void genericDialogTest::testDialog(QWidget *dialog, FWObject *object)
//qDebug() << "testing control" << widgets.at(i);
old->duplicate(object);
QWidget *widget = widgets.at(i);
// Skipping QSpinBox (which inherits QLineEdit) with QLineEdit type
// there should be another one with right type in list
if (widget->objectName() == "qt_spinbox_lineedit") continue;
if (dynamic_cast<QDialog*>(dialog) != NULL)
dynamic_cast<QDialog*>(dialog)->open();
activateTab(widget);
if (!widget->isVisible() || !widget->isEnabled()) continue;
@ -285,6 +288,12 @@ void genericDialogTest::testHostOSSettingsDialog_linux24()
testDialog(dialog, firewall);
}
#if 0
// rule options dialog uses stacked widget with only one page visible,
// depending on the firewall platform. Some widgets in invisible pages
// are not even initialized, also depending on the platform. Need to
// devise better test that would take this into account.
void genericDialogTest::testRuleOptionsDialog()
{
Firewall *firewall = Firewall::cast(om->createObject(FWBTree().getStandardSlotForObject(findUserLibrary(), Firewall::TYPENAME), Firewall::TYPENAME, "TestFirewall"));
@ -342,6 +351,8 @@ void genericDialogTest::testNATRuleOptionsDialog()
testDialog(dynamic_cast<QWidget*>(dialog), FWObject::cast(rule));
}
}
#endif
Library* genericDialogTest::findUserLibrary()
{

View File

@ -45,9 +45,9 @@ class genericDialogTest : public QObject
private slots:
void initTestCase();
void testFirewallSettingsDialog_iptables();
void testRuleOptionsDialog();
void testRoutingRuleOptionsDialog();
void testNATRuleOptionsDialog();
/* void testRuleOptionsDialog(); */
/* void testRoutingRuleOptionsDialog(); */
/* void testNATRuleOptionsDialog(); */
void testHostOSSettingsDialog_linux24();

View File

@ -2,6 +2,8 @@
#
include(../../qmake.inc)
QT += network
OBJECTS_DIR = .obj
MOC_DIR = .moc

View File

@ -4,7 +4,7 @@
#
# Firewall Builder fwb_ipt v5.0.0.3547
#
# Generated Fri Jun 3 17:29:42 2011 PDT by vadim
# Generated Sun Jun 5 20:10:11 2011 PDT by vadim
#
# files: * rc.firewall.local /etc/rc.d//rc.firewall.local
#

View File

@ -3,7 +3,7 @@
#
# Firewall Builder fwb_pf v5.0.0.3547
#
# Generated Fri Jun 3 18:57:45 2011 PDT by vadim
# Generated Sun Jun 5 20:10:47 2011 PDT by vadim
#
# files: * pf_cluster_4_rc.conf.local /etc/pf_cluster_4_rc.conf.local
# files: pf_cluster_4_pf.conf /etc/pf_cluster_4_pf.conf