summaryrefslogtreecommitdiff
path: root/src/internal/storage_backend_serialized.hpp
blob: e010b72cade80ca3b164476235f77dd583493c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
/*  MIT License
 *
 * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. */

#include "policy.hpp"
#include "serialization_backend.hpp"
#include <boost/tokenizer.hpp>
#include <sys/types.h>

namespace ldp_serialized {

class StorageBackendSerialized
{
public:
	StorageBackendSerialized();
	~StorageBackendSerialized();

	bool init(const char *filename, bool verify = false);
	bool initFromData(const uint8_t *serialized_data, size_t length, bool verify = false);
	void release();

	void printContent(bool xml_format = false) const;

	// Supported template parameters are:
	// MatchItemOwn, MatchItemSend, MatchItemReceive
	// and - only for Contexts - MatchItemAccess
	template <typename MatchItem>
	ldp_xml_parser::DecisionItem getDecisionItemContextMandatory(const MatchItem &item) const;
	template <typename MatchItem>
	ldp_xml_parser::DecisionItem getDecisionItemContextDefault(const MatchItem &item) const;
	template <typename MatchItem>
	ldp_xml_parser::DecisionItem getDecisionItemUser(uid_t uid, const MatchItem &item) const;
	template <typename MatchItem>
	ldp_xml_parser::DecisionItem getDecisionItemGroup(gid_t gid, const MatchItem &item) const;

	// This works with MatchItem set to MatchItemOwn, MatchItemSend or MatchItemReceive
	// This is needed for filtering mapGroups. Check NaivePolicyChecker.
	template <typename MatchItem>
	bool existsPolicyForGroup(gid_t gid) const;

private:
	typedef typename ldp_serialization::SerializationBackend::Storage Backend;
	Backend impl;

	// Set max size of serialized file to prevent mmap with unexpected memory size.
	// 1MB. Adjustable
	static const size_t MAX_SFILE_SIZE{1024 * 1024};

	int __fd{-1};
	uint8_t *__mem;
	size_t __length{0};

	void releaseMMap();
	void releaseFD();

	template <typename MatchItem>
	auto getPolicySet() const;

	template <typename T, typename P>
	ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P &policy, ldp_serialization::ItemsType) const;

	template <typename T, typename P>
	ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P &policy, ldp_serialization::TreeType) const;

	template <typename T, typename P>
	ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P &policy) const;

	template <typename P>
	ldp_xml_parser::DecisionItem getDecisionItem(const ldp_xml_parser::MatchItemSend &item, const P &policy) const;

	template <typename MatchItem, typename Map>
	ldp_xml_parser::DecisionItem getDecisionItem(const MatchItem &item, const Map &map, id_t id) const;

	typedef boost::tokenizer<boost::char_separator<char>> tokenizer;

	template <typename OwnNode>
	auto getDecisionItemFromTree(const OwnNode &node,
		                         const tokenizer::iterator &tokens,
		                         tokenizer::iterator &iterator) const;

	template <typename T, typename I>
	bool match(const T &match, const I &item) const;

	template <typename I>
	bool match(const ldp_xml_parser::MatchItemAccess &match, const I &item) const;

	template <typename DI>
	ldp_xml_parser::DecisionItem makeDecisionItem(const DI &item) const;

	template <typename String>
	boost::string_ref toStringRef(const String &str) const;
};

}