dbuspolicy-printer: add xml format 64/211164/2
authorsanghyeok.oh <sanghyeok.oh@samsung.com>
Tue, 30 Jul 2019 13:01:33 +0000 (22:01 +0900)
committersanghyeok.oh <sanghyeok.oh@samsung.com>
Tue, 30 Jul 2019 13:03:06 +0000 (22:03 +0900)
Change-Id: I67cc1f29fb8e4f2fded530dea7719c46f74c0568
Signed-off-by: sanghyeok.oh <sanghyeok.oh@samsung.com>
src/dbuspolicy_printer.cpp
src/internal/print_content.cpp
src/internal/print_content.hpp
src/internal/storage_backend_serialized.cpp
src/internal/storage_backend_serialized.hpp

index 691ea20..778ff69 100644 (file)
@@ -17,6 +17,7 @@ static void print_help(const char *name) {
        cout << "usage: " << name << " [-i serialized filename] [-v]" << endl;
        cout << "       " << name << " {--system|--session} [-v]" << endl;
        cout << " -v - just verify, don't print anything" << endl;
+       cout << " -x - print rule as xml format" << endl;
        cout << endl;
 }
 
@@ -24,6 +25,7 @@ int main(int argc, char *argv[]) {
        std::string input_filename;
        int c;
        bool just_verify = false;
+       bool xml_format = false;
 
        if (argc < 2) {
                print_help(argv[0]);
@@ -32,7 +34,7 @@ int main(int argc, char *argv[]) {
 
        while (1) {
                int option_index;
-               c = getopt_long(argc, argv, "i:v", options, &option_index);
+               c = getopt_long(argc, argv, "i:v:x", options, &option_index);
                if (c == -1)
                        break;
 
@@ -50,6 +52,9 @@ int main(int argc, char *argv[]) {
                case 'v':
                        just_verify = true;
                        break;
+               case 'x':
+                       xml_format = true;
+                       break;
                case '?':
                        print_help(argv[0]);
                        return EXIT_FAILURE;
@@ -80,7 +85,7 @@ int main(int argc, char *argv[]) {
                return EXIT_SUCCESS;
        }
 
-       storage.printContent();
+       storage.printContent(xml_format);
 
        return EXIT_SUCCESS;
 }
index 5c67497..d65a433 100644 (file)
@@ -31,6 +31,13 @@ inline const char* __access_type_to_str(ldp_xml_parser::BusAccessType type) {
 }
 }
 
+namespace print_content {
+static bool xml_format = false;
+void use_xml_format(const bool xml) {
+       xml_format = xml;
+}
+}
+
 template <typename T>
 std::ostream &print_val(std::ostream &stream, const boost::string_ref &name, const T &val) {
        return stream << name << "(" << val << ")";
@@ -68,10 +75,14 @@ std::ostream &print_content_item_sr(std::ostream &stream,
                                                                        const boost::string_ref &member,
                                                                        const boost::string_ref &path,
                                                                        ldp_xml_parser::MessageType type,
-                                                                       const ldp_xml_parser::DecisionItem &decisionItem)
+                                                                       const ldp_xml_parser::DecisionItem &decisionItem,
+                                                                       const bool is_prefix)
 {
        stream << item_type << ": ";
-       print_val(stream, "name", name);
+       if (is_prefix)
+               print_val(stream, "name_prefix", name);
+       else
+               print_val(stream, "name", name);
        print_next_val(stream, "inter", interface);
        print_next_val(stream, "member", member);
        print_next_val(stream, "path", path);
@@ -79,6 +90,49 @@ std::ostream &print_content_item_sr(std::ostream &stream,
        return print_next_val(stream, "decision", decisionItem);
 }
 
+std::ostream &print_content_item_sr_xml(std::ostream &stream,
+                                                                       const boost::string_ref &item_type,
+                                                                       const boost::string_ref &name,
+                                                                       const boost::string_ref &interface,
+                                                                       const boost::string_ref &member,
+                                                                       const boost::string_ref &path,
+                                                                       ldp_xml_parser::MessageType type,
+                                                                       const ldp_xml_parser::DecisionItem &decisionItem,
+                                                                       const bool is_prefix)
+{
+       const char *strDecision[] = {"any", "allow", "deny", "check"};
+       const char *sr;
+       std::string type_str;
+
+       if (item_type == "ItemSend")
+               sr = "send";
+       else
+               sr = "receive";
+
+       stream << "<" << strDecision[static_cast<std::size_t>(decisionItem.getDecision())] << " ";
+       if (is_prefix)
+               stream << sr << "_destination_prefix=\"" << name << "\" ";
+       else if (!name.empty())
+               stream << sr << "_" << (item_type == "ItemSend" ? "destination" : "sender") << "=\"" << name << "\" ";
+
+       if (!path.empty())
+               stream << sr << "_" << "path=\"" << path << "\" ";
+       if (!interface.empty())
+               stream << sr << "_" << "interface=\"" << interface << "\" ";
+       if (!member.empty())
+               stream << sr << "_" << "member=\"" << member << "\" ";
+       if (!decisionItem.getPrivilege().empty())
+               stream << sr << "_" << "privilege=\"" << decisionItem.getPrivilege() << "\" ";
+
+       type_str = __message_type_to_str(type);
+       std::transform(type_str.begin(), type_str.end(), type_str.begin(), [](unsigned char c){ return std::tolower(c); });
+
+       if (type_str != "any")
+               stream << sr << "_" << "type=\"" << type_str << "\" ";
+
+       return stream << "/>";
+}
+
 namespace {
 static const char* message_decision[] = {"NO_DECISION", "ALLOW", "DENY", "CHECK"};
 static inline const char* __decision_to_str(ldp_xml_parser::Decision dec) {
@@ -105,9 +159,10 @@ template <> void printContentItem(std::ostream &stream, const FB::ItemAccess *it
 
 template <typename T>
 void printContentItemSR(std::ostream &stream, const boost::string_ref &item_type, const T *item) {
-       print_content_item_sr(stream, item_type, item->name()->c_str(), item->interface()->c_str(),
+       auto print_func = (print_content::xml_format ? print_content_item_sr_xml : print_content_item_sr);
+       print_func(stream, item_type, item->name()->c_str(), item->interface()->c_str(),
                        item->member()->c_str(), item->path()->c_str(), makeMessageType(item->type()),
-                       makeDecisionItem(item->decision()));
+                       makeDecisionItem(item->decision()), item->is_name_prefix());
 }
 
 template <> void printContentItem(std::ostream &stream, const FB::ItemSend *item) {
@@ -189,13 +244,15 @@ std::ostream &operator<<(std::ostream &stream, const ldp_xml_parser::ItemOwn &it
 }
 
 std::ostream &operator<<(std::ostream &stream, const ldp_xml_parser::ItemSend &item) {
-       return print_content_item_sr(stream, "ItemSend", item.getName(), item.getInterface(), item.getMember(),
-                       item.getPath(), item.getType(), item.getDecision());
+       auto print_func = (print_content::xml_format ? print_content_item_sr_xml : print_content_item_sr);
+       return print_func(stream, "ItemSend", item.getName(), item.getInterface(), item.getMember(),
+                       item.getPath(), item.getType(), item.getDecision(), item.isNamePrefix());
 }
 
 std::ostream &operator<<(std::ostream &stream, const ldp_xml_parser::ItemReceive &item) {
-       return print_content_item_sr(stream, "ItemReceive", item.getName(), item.getInterface(), item.getMember(),
-                       item.getPath(), item.getType(), item.getDecision());
+       auto print_func = (print_content::xml_format ? print_content_item_sr_xml : print_content_item_sr);
+       return print_func(stream, "ItemReceive", item.getName(), item.getInterface(), item.getMember(),
+                       item.getPath(), item.getType(), item.getDecision(), item.isNamePrefix());
 }
 
 std::ostream &operator<<(std::ostream &stream, const ldp_xml_parser::ItemAccess &item) {
index c0ffc8f..78f6381 100644 (file)
 
 #include <ostream>
 
+namespace print_content {
+void use_xml_format(const bool xml);
+}
+
 namespace FB {
 std::ostream &operator<<(std::ostream &stream, const FB::File &file);
 }
index ef0855b..4859951 100644 (file)
@@ -76,7 +76,7 @@ public:
        ldp_xml_parser::DecisionItem getDecisionFromSendIndex(const MatchItemSend &item);
        ldp_xml_parser::DecisionItem getDecisionFromSendIndex(const MatchItemSend &item, uid_t uid);
 
-       void printContent() const;
+       void printContent(const bool xml_format = false) const;
 
        template <typename T, typename M = typename type_helper<T>::policy_set_type>
        const M *getPolicySet();
@@ -288,7 +288,8 @@ bool StorageBackendSerialized::StorageBackendSerializedImpl::initFromXML(const c
        return init(FB::GetFile(data));
 }
 
-void StorageBackendSerialized::StorageBackendSerializedImpl::printContent() const {
+void StorageBackendSerialized::StorageBackendSerializedImpl::printContent(const bool xml_format) const {
+       print_content::use_xml_format(xml_format);
        std::cerr << *file;
 }
 
@@ -328,8 +329,8 @@ void StorageBackendSerialized::release() {
        pimpl->release();
 }
 
-void StorageBackendSerialized::printContent() const {
-       pimpl->printContent();
+void StorageBackendSerialized::printContent(const bool xml_format) const {
+       pimpl->printContent(xml_format);
 }
 
 template <> bool match(const ldp_xml_parser::MatchItemAccess &match, const FB::ItemAccess *item) {
index 658e197..ef40013 100644 (file)
@@ -34,7 +34,7 @@ public:
        bool initFromXML(const char *config_name);
        void release();
 
-       void printContent() const;
+       void printContent(const bool xml_format = false) const;
 
        // Supported template parameters are:
        // MatchPolicyOwn, MatchPolicySend, MatchPolicyReceive