tests: switch to the new extracting interface
[platform/core/system/libdbuspolicy.git] / src / internal / storage_backend_serialized.hpp
1 #pragma once
2 /*  MIT License
3  *
4  * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is furnished
11  * to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE. */
23
24 #include "policy.hpp"
25 #include "serialization_backend.hpp"
26 #include <boost/tokenizer.hpp>
27 #include <sys/types.h>
28
29 namespace ldp_serialized {
30
31 class StorageBackendSerialized
32 {
33 public:
34         StorageBackendSerialized();
35         ~StorageBackendSerialized();
36
37         bool init(const char *filename, bool verify = false);
38         bool initFromData(const uint8_t *serialized_data, size_t length, bool verify = false);
39         void release();
40
41         void printContent(bool xml_format = false) const;
42
43         // Supported template parameters are:
44         // MatchItemOwn, MatchItemSend, MatchItemReceive
45         // and - only for Contexts - MatchItemAccess
46         template <typename MatchItem>
47         ldp_xml_parser::DecisionItem getDecisionItemContextMandatory(const MatchItem &item) const;
48         template <typename MatchItem>
49         ldp_xml_parser::DecisionItem getDecisionItemContextDefault(const MatchItem &item) const;
50         template <typename MatchItem>
51         ldp_xml_parser::DecisionItem getDecisionItemUser(uid_t uid, const MatchItem &item) const;
52         template <typename MatchItem>
53         ldp_xml_parser::DecisionItem getDecisionItemGroup(gid_t gid, const MatchItem &item) const;
54
55         // This works with MatchItem set to MatchItemOwn, MatchItemSend or MatchItemReceive
56         // This is needed for filtering mapGroups. Check NaivePolicyChecker.
57         template <typename MatchItem>
58         bool existsPolicyForGroup(gid_t gid) const;
59
60         const auto &getBackend() const { return impl; }
61
62 private:
63         typedef typename ldp_serialization::SerializationBackend::Storage Backend;
64         Backend impl;
65
66         // Set max size of serialized file to prevent mmap with unexpected memory size.
67         // 1MB. Adjustable
68         static const size_t MAX_SFILE_SIZE{1024 * 1024};
69
70         int __fd{-1};
71         uint8_t *__mem;
72         size_t __length{0};
73
74         void releaseMMap();
75         void releaseFD();
76
77         template <typename MatchItem>
78         auto getPolicySet() const;
79
80         template <typename T, typename P>
81         ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P &policy, ldp_serialization::ItemsType) const;
82
83         template <typename T, typename P>
84         ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P &policy, ldp_serialization::TreeType) const;
85
86         template <typename T, typename P>
87         ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P &policy) const;
88
89         template <typename P>
90         ldp_xml_parser::DecisionItem getDecisionItem(const ldp_xml_parser::MatchItemSend &item, const P &policy) const;
91
92         template <typename MatchItem, typename Map>
93         ldp_xml_parser::DecisionItem getDecisionItem(const MatchItem &item, const Map &map, id_t id) const;
94
95         typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
96
97         template <typename OwnNode>
98         auto getDecisionItemFromTree(const OwnNode &node,
99                                          const tokenizer::iterator &tokens,
100                                          tokenizer::iterator &iterator) const;
101
102         template <typename T, typename I>
103         bool match(const T &match, const I &item) const;
104
105         template <typename I>
106         bool match(const ldp_xml_parser::MatchItemAccess &match, const I &item) const;
107
108         template <typename DI>
109         ldp_xml_parser::DecisionItem makeDecisionItem(const DI &item) const;
110
111         template <typename String>
112         boost::string_ref toStringRef(const String &str) const;
113 };
114
115 }