summaryrefslogtreecommitdiff
path: root/src/internal/serializer.cpp
blob: 55957512a885927c2b71a4e35da2522ce8bcbb65 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
#include "include/fb_generated.h"
#include "include/flatbuffers/flatbuffers.h"
#include "serializer.hpp"
#include "tslog.hpp"
#include <iostream>
#include <string>

using namespace ldp_xml_parser;

namespace ldp_serializer {

std::map<Decision, FB::Decision> decisions_map {
	{ Decision::ANY, FB::Decision_ANY },
	{ Decision::DENY, FB::Decision_DENY },
	{ Decision::ALLOW, FB::Decision_ALLOW },
	{ Decision::CHECK, FB::Decision_CHECK }
};

std::map<BusAccessType, FB::BusAccessType> bus_access_map {
	{ BusAccessType::USER, FB::BusAccessType_USER },
	{ BusAccessType::GROUP, FB::BusAccessType_GROUP },
	{ BusAccessType::ALL_USERS, FB::BusAccessType_ALL_USERS },
	{ BusAccessType::ALL_GROUPS, FB::BusAccessType_ALL_GROUPS },
};

std::map<MessageType, FB::MessageType> message_type_map {
	{ MessageType::ANY, FB::MessageType_ANY },
	{ MessageType::METHOD_CALL, FB::MessageType_METHOD_CALL },
	{ MessageType::METHOD_RETURN, FB::MessageType_METHOD_RETURN },
	{ MessageType::ERROR, FB::MessageType_ERROR },
	{ MessageType::SIGNAL, FB::MessageType_SIGNAL }
};

template <>
struct Serializer::type_helper<PolicyOwn> {
	typedef struct FB::OwnSet set;
	typedef struct FB::OwnSetBuilder builder;
	typedef struct FB::PolicyOwn policy;
	typedef struct FB::PolicyOwnPair pair;
	static constexpr auto create_set = &FB::CreateOwnSet;
	static constexpr auto create_policy = &FB::CreatePolicyOwn;
	static constexpr auto create_policy_pair = &FB::CreatePolicyOwnPair;
};

template <>
struct Serializer::type_helper<PolicySend> {
	typedef struct FB::SendSet set;
	typedef struct FB::SendSetBuilder builder;
	typedef struct FB::PolicySend policy;
	typedef struct FB::PolicySendPair pair;
	typedef struct FB::ItemSend item;
	static constexpr auto create_set = &FB::CreateSendSet;
	static constexpr auto create_policy = &FB::CreatePolicySend;
	static constexpr auto create_policy_pair = &FB::CreatePolicySendPair;
	static constexpr auto create_item = &FB::CreateItemSend;
};

template <>
struct Serializer::type_helper<PolicyReceive> {
	typedef struct FB::ReceiveSet set;
	typedef struct FB::ReceiveSetBuilder builder;
	typedef struct FB::PolicyReceive policy;
	typedef struct FB::PolicyReceivePair pair;
	typedef struct FB::ItemReceive item;
	static constexpr auto create_set = &FB::CreateReceiveSet;
	static constexpr auto create_policy = &FB::CreatePolicyReceive;
	static constexpr auto create_policy_pair = &FB::CreatePolicyReceivePair;
	static constexpr auto create_item = &FB::CreateItemReceive;
};

template <>
struct Serializer::type_helper<PolicyAccess> {
	typedef struct FB::AccessSet set;
	typedef struct FB::AccessSetBuilder builder;
	typedef struct FB::PolicyAccess policy;
	typedef struct FB::ItemAccess item;
	static constexpr auto create_set = &FB::CreateAccessSet;
	static constexpr auto create_policy = &FB::CreatePolicyAccess;
	static constexpr auto create_item = &FB::CreateItemAccess;
};

const uint8_t* Serializer::serialize(const ldp_xml::StorageBackendXML &db, size_t &size) {
	m_db = &db;

	auto own_set = serialize_set<PolicyOwn>();
	auto send_set = serialize_set<PolicySend>();
	auto receive_set = serialize_set<PolicyReceive>();
	auto access_set = serialize_set<PolicyAccess>();

	auto file = FB::CreateFile(m_builder,
				   own_set,
				   send_set,
				   receive_set,
				   access_set);

	m_builder.Finish(file, FB::FileIdentifier());
	auto buf = m_builder.GetBufferPointer();
	size = m_builder.GetSize();

	return buf;
}

const uint8_t* Serializer::serialize(const std::string config_path, size_t &size) {
	tslog::init(tslog::ldp_log_level::DEFAULT);
	ldp_xml::StorageBackendXML xmlStorage;

	if (!xmlStorage.init(config_path.c_str())) {
		std::cout << "xmlStorage init error" << std::endl;
		return nullptr;
	}

	return serialize(xmlStorage, size);
}

const uint8_t *Serializer::serialize(const std::string config_path, std::ostream &output) {
	size_t size = 0;
	auto buf = serialize(config_path, size);

	output.write(reinterpret_cast<const char *>(buf), size);
	return buf;
}

template <typename T>
auto Serializer::get_create_set() -> decltype(type_helper<T>::create_set) {
	return type_helper<T>::create_set;
}

template <typename T>
auto Serializer::get_create_policy() -> decltype(type_helper<T>::create_policy) {
	return type_helper<T>::create_policy;
}

template <typename T>
auto Serializer::get_create_policy_pair() -> decltype(type_helper<T>::create_policy_pair) {
	return type_helper<T>::create_policy_pair;
}

template <typename T>
auto Serializer::get_create_item() -> decltype(type_helper<T>::create_item) {
	return type_helper<T>::create_item;
}

FbOff<FB::PolicyOwn> Serializer::serialize_tree(const OwnershipTree &tree) {
	auto tree_item = serialize_tree(tree.getRoot());
	auto policy = FB::CreatePolicyOwn(m_builder, tree_item);

	return policy;
}

FbOff<FB::PolicyOwnNode> Serializer::serialize_tree(const std::shared_ptr<TreeNode> &node) {
	auto prefix_decision_item = serialize_decision(node->getOwnPrefixDecisionItem());
	auto decision_item = serialize_decision(node->getOwnDecisionItem());

	std::vector<FbOff<FB::PolicyOwnNode>> children;

	for (const auto &subnode : node->getChildren()) {
		auto child = serialize_tree(subnode.second);
		children.push_back(child);
	}

	auto policy_own = FB::CreatePolicyOwnNode(m_builder,
						  m_builder.CreateString(node->getToken()),
						  prefix_decision_item,
						  decision_item,
						  m_builder.CreateVectorOfSortedTables(&children));
	return policy_own;
}

FbOff<FB::DecisionItem> Serializer::serialize_decision(const DecisionItem &item) {
	return FB::CreateDecisionItem(m_builder,
				      decisions_map[item.getDecision()],
				      m_builder.CreateString(item.getPrivilege()));
}

template <>
auto Serializer::serialize_item<PolicyAccess>(const ItemAccess &item) -> FbOff<FB::ItemAccess> {
	auto create_item = get_create_item<PolicyAccess>();

	return create_item(m_builder,
			   item.getUid(),
			   item.getGid(),
			   serialize_decision(item.getDecision()),
			   bus_access_map[item.getType()]);
}

template <typename T, typename P>
auto Serializer::serialize_item(const P &item) -> FbOff<typename type_helper<T>::item> {
	auto create_item = get_create_item<T>();
	return create_item(m_builder,
			   serialize_decision(item.getDecision()),
			   m_builder.CreateString(item.getName()),
			   m_builder.CreateString(item.getInterface()),
			   m_builder.CreateString(item.getMember()),
			   m_builder.CreateString(item.getPath()),
			   message_type_map[item.getType()],
			   item.isNamePrefix());
}

template <typename T>
auto Serializer::serialize_policy(const std::vector<FbOff<typename type_helper<T>::item>> items)
				    -> FbOff<typename type_helper<T>::policy> {
	auto create_policy = get_create_policy<T>();
	return create_policy(m_builder, m_builder.CreateVector(items));
}

template <>
auto Serializer::serialize_policy(const PolicyOwn &policy)
				    -> FbOff<FB::PolicyOwn> {
	return serialize_tree(policy.getTree());
}

template <>
auto Serializer::serialize_policy(const PolicySend &policy)
				    -> FbOff<FB::PolicySend> {
	std::vector<FbOff<FB::ItemSend>> items;

	// this maps a name to a pair (highest score + list of scores/ids for this name)
	std::map<boost::string_ref, std::pair<uint32_t, std::vector<uint32_t>>> policyIndex;
	// prefix index is just a list of ids
	std::vector<uint32_t> prefixIndex;
	uint32_t cnt = 1;

	for (const auto &item : policy.getItems()) {
		items.push_back(serialize_item<PolicySend>(item));

		// create indexes
		if (!item.isNamePrefix()) {
			auto &elem = policyIndex[item.getName()];   // create or get an entry
			elem.second.push_back(cnt);                 // add score/id to the list
			// we insert items in increasing score/id order, so we just update the highest score on each add
			elem.first = cnt;
		} else {
			// just collect the prefix rules
			prefixIndex.push_back(cnt);
		}
		++cnt;
	}

	// serialize main index
	std::vector<FbOff<FB::NameScoresPair>> index;

	for (auto &it: policyIndex)
		index.push_back(FB::CreateNameScoresPairDirect(m_builder,
														it.first.data(),     // name
														it.second.first,     // best_score
														&it.second.second)); // vector of scores/ids

	return FB::CreatePolicySend(m_builder,
			m_builder.CreateVector(items),
			m_builder.CreateVector(index),
			m_builder.CreateVector(prefixIndex));
}

template <typename T>
auto Serializer::serialize_policy(const T &policy) -> FbOff<typename type_helper<T>::policy> {
	std::vector<FbOff<typename type_helper<T>::item>> items;

	for (const auto &item : policy.getItems()) {
		items.push_back(serialize_item<T>(item));
	}

	return serialize_policy<T>(items);
}

template <typename T, typename P>
auto Serializer::serialize_pair(const long int id, const P policy)
				  -> FbOff<typename type_helper<T>::pair> {
	auto create_policy_pair = get_create_policy_pair<T>();
	return create_policy_pair(m_builder, id, serialize_policy(policy));
}

template <typename TP>
auto Serializer::serialize_set() -> FbOff<typename type_helper<TP>::set> {
	auto context_default = serialize_policy<TP>(m_db->getPolicyContextDefault<TP>());
	auto context_mandatory = serialize_policy<TP>(m_db->getPolicyContextMandatory<TP>());

	return serialize_set<TP>(context_default, context_mandatory);
}

template <>
auto Serializer::serialize_set<PolicyAccess>(FbOff<FB::PolicyAccess> context_default,
					     FbOff<FB::PolicyAccess> context_mandatory)
					       -> FbOff<typename type_helper<PolicyAccess>::set>
{
	return FB::CreateAccessSet(m_builder, context_default, context_mandatory);
}

template <typename TP, typename TFP>
auto Serializer::serialize_set(FbOff<TFP> context_default,
			       FbOff<TFP> context_mandatory)
				 -> FbOff<typename type_helper<TP>::set>
{
	std::vector<FbOff<typename type_helper<TP>::pair>> user;
	std::vector<FbOff<typename type_helper<TP>::pair>> group;
	std::vector<long long> uid;
	std::vector<long long> gid;

	for (const auto &u : m_db->getPoliciesUser<TP>())
		user.push_back(serialize_pair<TP>(u.first, u.second));

	for (const auto &g : m_db->getPoliciesGroup<TP>())
		group.push_back(serialize_pair<TP>(g.first, g.second));

	auto func = get_create_set<TP>();
	return func(m_builder, context_default, context_mandatory,
				m_builder.CreateVectorOfSortedTables(&user),
				m_builder.CreateVectorOfSortedTables(&group));
}

}