Repository :
http://git.fedorahosted.org/cgit/firewalld-recode.git
On branch : master
---------------------------------------------------------------
commit 13c4a0288b55be7de5762442f7b7b423ba56e883
Author: Daniel Kopecek <dkopecek(a)redhat.com>
Date: Tue Oct 14 10:51:45 2014 +0200
Implemented XMLWrite method for ICMPType, Service, Zone and LockdownWhitelist objects
---------------------------------------------------------------
src/Common/XMLStorage.cpp | 8 +++++
src/Common/XMLStorage.hpp | 3 ++
src/Daemon/Firewall/ICMPType.cpp | 11 +++++++
src/Daemon/Firewall/ICMPType.hpp | 1 +
src/Daemon/Firewall/Service.cpp | 21 +++++++++++++
src/Daemon/Firewall/Service.hpp | 1 +
src/Daemon/Firewall/Zone.cpp | 60 ++++++++++++++++++++++++++++++++++++++
src/Daemon/Firewall/Zone.hpp | 1 +
src/Daemon/LockdownWhitelist.cpp | 21 +++++++++++++
src/Daemon/LockdownWhitelist.hpp | 1 +
10 files changed, 128 insertions(+), 0 deletions(-)
diff --git a/src/Common/XMLStorage.cpp b/src/Common/XMLStorage.cpp
index bdc48da..c5d922c 100644
--- a/src/Common/XMLStorage.cpp
+++ b/src/Common/XMLStorage.cpp
@@ -134,4 +134,12 @@ namespace firewalld
return;
}
+ void XMLStorage::store(const String& filepath)
+ {
+ std::ofstream stream(filepath, std::ios_base::trunc);
+ stream << "<?xml version=\"1.0\"
encoding=\"utf-8\"?>" << std::endl;
+ XMLWrite(stream);
+ return;
+ }
+
} /* namespace firewalld */
diff --git a/src/Common/XMLStorage.hpp b/src/Common/XMLStorage.hpp
index d6a1693..5f9cbfe 100644
--- a/src/Common/XMLStorage.hpp
+++ b/src/Common/XMLStorage.hpp
@@ -23,6 +23,7 @@
#include <expat.h>
#include <initializer_list>
+#include <fstream>
namespace firewalld
{
@@ -34,6 +35,7 @@ namespace firewalld
virtual void XMLElementStart(const UString& name, const
UStringKeyMap<UString>& attr) = 0;
virtual void XMLElementEnd(const UString& name) = 0;
virtual void XMLElementStringValue(const UString& name, const UString& value)
= 0;
+ virtual void XMLWrite(std::ofstream& stream) = 0;
static void expatElementStartHandler(void *arg0, const XML_Char* name, const
XML_Char** attr);
static void expatElementEndHandler(void *arg0, const XML_Char* name);
@@ -46,6 +48,7 @@ namespace firewalld
bool elementStackState(std::initializer_list<UString> list) const;
void load(const String& filepath);
+ void store(const String& filepath);
private:
UStringList *_element_stack;
diff --git a/src/Daemon/Firewall/ICMPType.cpp b/src/Daemon/Firewall/ICMPType.cpp
index 529f074..4a4e81f 100644
--- a/src/Daemon/Firewall/ICMPType.cpp
+++ b/src/Daemon/Firewall/ICMPType.cpp
@@ -112,4 +112,15 @@ namespace firewalld
}
}
+ void ICMPType::XMLWrite(std::ofstream& stream)
+ {
+ stream << "<icmptype>" << std::endl;
+ stream << "\t<short>" << _name <<
"</short>" << std::endl;
+ stream << "\t<description>" << _description <<
"</description>" << std::endl;
+ stream << "\t<destination ipv4=\"" <<
(_flags.test(Flags::IPv4) ? "true" : "false") <<
"\"";
+ stream << " ipv6=\"" << (_flags.test(Flags::IPv6) ? "
true" : "false") <<"\"/>" << std::endl;
+ stream << "</icmptype>" << std::endl;
+ return;
+ }
+
} /* namespace firewalld*/
diff --git a/src/Daemon/Firewall/ICMPType.hpp b/src/Daemon/Firewall/ICMPType.hpp
index 852d046..1cd8af0 100644
--- a/src/Daemon/Firewall/ICMPType.hpp
+++ b/src/Daemon/Firewall/ICMPType.hpp
@@ -48,6 +48,7 @@ namespace firewalld
void XMLElementStart(const UString& name, const UStringKeyMap<UString>&
attr);
void XMLElementEnd(const UString& name);
void XMLElementStringValue(const UString& name, const UString& value);
+ void XMLWrite(std::ofstream& stream);
private:
// Unique identifier of the service.
diff --git a/src/Daemon/Firewall/Service.cpp b/src/Daemon/Firewall/Service.cpp
index 45d6c7f..5a95cfb 100644
--- a/src/Daemon/Firewall/Service.cpp
+++ b/src/Daemon/Firewall/Service.cpp
@@ -139,4 +139,25 @@ namespace firewalld
return;
}
}
+
+ void Service::XMLWrite(std::ofstream& stream)
+ {
+ stream << "<service>" << std::endl;
+ stream << "\t<short>" << _name <<
"</short>" << std::endl;
+ stream << "\t<description>" << _description <<
"</description>" << std::endl;
+ for (Pointer<const Port> port : _ports) {
+ stream << "\t<port port=\"" <<
port->range().toString() << "\"";
+ stream << " protocol=\"" << (port->protocol() ==
Port::Protocol::TCP ? "tcp" : "udp");
+ stream << "\"/>" << std::endl;
+ }
+ for (Pointer<const KernelModule> module : _modules) {
+ stream << "\t<module name=\"" << module->name()
<< "\"/>" << std::endl;
+ }
+
+ // TODO: Implement destinations
+ //stream << "<destination ipv4=""
ipv6=""/>" << std::endl;
+
+ stream << "</service>" << std::endl;
+ return;
+ }
}
diff --git a/src/Daemon/Firewall/Service.hpp b/src/Daemon/Firewall/Service.hpp
index c50c0f2..82f43b8 100644
--- a/src/Daemon/Firewall/Service.hpp
+++ b/src/Daemon/Firewall/Service.hpp
@@ -48,6 +48,7 @@ namespace firewalld
void XMLElementStart(const UString& name, const UStringKeyMap<UString>&
attr);
void XMLElementEnd(const UString& name);
void XMLElementStringValue(const UString& name, const UString& value);
+ void XMLWrite(std::ofstream& stream);
private:
// Unique identifier of the service.
diff --git a/src/Daemon/Firewall/Zone.cpp b/src/Daemon/Firewall/Zone.cpp
index 9c179c1..2b5fb16 100644
--- a/src/Daemon/Firewall/Zone.cpp
+++ b/src/Daemon/Firewall/Zone.cpp
@@ -483,4 +483,64 @@ namespace firewalld
return;
}
+ void Zone::XMLWrite(std::ofstream& stream)
+ {
+ stream << "<zone>" << std::endl;
+ stream << "\t<short>" << _name <<
"</short>" << std::endl;
+ stream << "\t<description>" << _description <<
"</description>" << std::endl;
+ // interface
+ for (auto const& map_entry : _interface_map) {
+ const UString& interface_name = map_entry.first;
+ stream << "\t<interface name=\"" << interface_name
<< "\"/>" << std::endl;
+ }
+ // source
+ for (auto const& map_entry : _source_map) {
+ const UString& address = map_entry.second->identifier();
+ stream << "\t<source address=\"" << address <<
"\"/>" << std::endl;
+ }
+ // service
+ for (auto const& map_entry : _service_map) {
+ const UString& service_name = map_entry.first;
+ stream << "\t<service name=\"" << service_name
<< "\"/>" << std::endl;
+ }
+ // port
+ for (auto const& map_entry : _tcp_port_map) {
+ Pointer<const Port> port = map_entry.second;
+ stream << "\t<port port=\"" <<
port->range().toString() << "\" protocol=\"tcp\"/>"
<< std::endl;
+ }
+ for (auto const& map_entry : _udp_port_map) {
+ Pointer<const Port> port = map_entry.second;
+ stream << "\t<port port=\"" <<
port->range().toString() << "\" protocol=\"udp\"/>"
<< std::endl;
+ }
+ // icmp-block
+ for (auto const& map_entry : _icmp_block_map) {
+ const UString& icmptype = map_entry.first;
+ stream << "\t<icmp-block name=\"" << icmptype
<< "\"/>" << std::endl;
+ }
+ // masquerade
+ if (_flags.test(Flags::Masquerade)) {
+ stream << "\t<masquerade/>" << std::endl;
+ }
+
+ // forward-port
+ for (auto const& map_entry : _port_forward_map) {
+ Pointer<const PortForward> forward = map_entry.second;
+ stream << "\t<forward-port port=\"" <<
forward->sourcePort()->range().toString() << "\"";
+ stream << " protocol=\"" << (forward->protocol() ==
Port::Protocol::TCP ? "tcp" : "udp") << "\"";
+
+ if (forward->sourcePort() != forward->destinationPort()) {
+ stream << " to-port=\"" <<
forward->destinationPort()->range().toString() << "\"";
+ }
+ if (!forward->destinationAddress().empty()) {
+ stream << " to-addr=\"" << forward->destinationAddress()
<< "\"";
+ }
+
+ stream << "/>" << std::endl;
+ }
+
+ // rule
+ // TODO: Implement rich rules
+
+ stream << "</zone>" << std::endl;
+ }
}
diff --git a/src/Daemon/Firewall/Zone.hpp b/src/Daemon/Firewall/Zone.hpp
index 410b6b0..3fd0d04 100644
--- a/src/Daemon/Firewall/Zone.hpp
+++ b/src/Daemon/Firewall/Zone.hpp
@@ -113,6 +113,7 @@ namespace firewalld
void XMLElementStart(const UString& name, const UStringKeyMap<UString>&
attr);
void XMLElementEnd(const UString& name);
void XMLElementStringValue(const UString& name, const UString& value);
+ void XMLWrite(std::ofstream& stream);
private:
// Unique identifier of the zone.
diff --git a/src/Daemon/LockdownWhitelist.cpp b/src/Daemon/LockdownWhitelist.cpp
index ddbdf30..9dd4ec1 100644
--- a/src/Daemon/LockdownWhitelist.cpp
+++ b/src/Daemon/LockdownWhitelist.cpp
@@ -162,4 +162,25 @@ namespace firewalld
{
}
+ void LockdownWhitelist::XMLWrite(std::ofstream& stream)
+ {
+ stream << "<whitelist>" << std::endl;
+ for (auto const& map_entry : _secontexts) {
+ stream << "\t<selinux context=\"" << map_entry.second
<< "\"/>" << std::endl;
+ }
+ for (auto const& map_entry : _commands_full) {
+ stream << "\t<command name=\"" << map_entry.second
<< "\"/>" << std::endl;
+ }
+ for (auto const& list_entry : _commands_partial) {
+ stream << "\t<command name=\"" << list_entry <<
"\"/>" << std::endl;
+ }
+ for (auto const& map_entry : _usernames) {
+ stream << "\t<user name=\"" << map_entry.second
<< "\"/>" << std::endl;
+ }
+ for (auto const& map_entry : _uids) {
+ stream << "\t<user id=\"" <<
numberToString(map_entry.second) << "\"/>" << std::endl;
+ }
+ stream << "</whitelist>" << std::endl;
+ }
+
} /* namespace firewalld */
diff --git a/src/Daemon/LockdownWhitelist.hpp b/src/Daemon/LockdownWhitelist.hpp
index b635331..986fcba 100644
--- a/src/Daemon/LockdownWhitelist.hpp
+++ b/src/Daemon/LockdownWhitelist.hpp
@@ -52,6 +52,7 @@ namespace firewalld
void XMLElementStart(const UString& name, const UStringKeyMap<UString>&
attr);
void XMLElementEnd(const UString& name);
void XMLElementStringValue(const UString& name, const UString& value);
+ void XMLWrite(std::ofstream& stream);
private:
template<typename value_type>