dc3a861f41832747c305045e7c2734cf72d65a85
[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 "storage_backend_flatbuffers.hpp"
26 #include "serializer.hpp"
27 #include <memory>
28 #include <sys/types.h>
29
30 namespace ldp_serialized {
31
32 class StorageBackendSerialized
33 {
34 public:
35         StorageBackendSerialized();
36         ~StorageBackendSerialized();
37
38         bool init(const char *filename, bool verify = false);
39         bool initFromData(const uint8_t *serialized_data, size_t length, bool verify = false);
40         bool initFromXML(const char *config_name);
41         void release();
42
43         void printContent(bool xml_format = false) const
44         { impl.printContent(xml_format); }
45
46         // Supported template parameters are:
47         // MatchItemOwn, MatchItemSend, MatchItemReceive
48         // and - only for Contexts - MatchItemAccess
49         template <typename MatchItem>
50         ldp_xml_parser::DecisionItem getDecisionItemContextMandatory(const MatchItem &item) const
51         { return impl.getDecisionItemContextMandatory(item); }
52         template <typename MatchItem>
53         ldp_xml_parser::DecisionItem getDecisionItemContextDefault(const MatchItem &item) const
54         { return impl.getDecisionItemContextDefault(item); }
55         template <typename MatchItem>
56         ldp_xml_parser::DecisionItem getDecisionItemUser(uid_t uid, const MatchItem &item) const
57         { return impl.getDecisionItemUser(uid, item); }
58         template <typename MatchItem>
59         ldp_xml_parser::DecisionItem getDecisionItemGroup(gid_t gid, const MatchItem &item) const
60         { return impl.getDecisionItemGroup(gid, item); }
61
62         // This works with MatchItem set to MatchItemOwn, MatchItemSend or MatchItemReceive
63         // This is needed for filtering mapGroups. Check NaivePolicyChecker.
64         template <typename MatchItem>
65         bool existsPolicyForGroup(gid_t gid) const
66         { return impl.existsPolicyForGroup<MatchItem>(gid); }
67
68 private:
69         typedef StorageBackendFlatbuffers Backend;
70         Backend impl;
71
72         // Set max size of serialized file to prevent mmap with unexpected memory size.
73         // 1MB. Adjustable
74         static const size_t MAX_SFILE_SIZE{1024 * 1024};
75
76         int __fd{-1};
77         uint8_t *__mem;
78         size_t __length{0};
79
80         std::unique_ptr<ldp_serializer::Serializer> serializer;
81
82         void releaseMMap();
83         void releaseFD();
84 };
85
86 }