/
session_policy.h
266 lines (208 loc) · 6.55 KB
/
session_policy.h
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
/*
* \brief Utilities for handling server-side session policies
* \author Norman Feske
* \date 2011-09-13
*/
/*
* Copyright (C) 2011-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__OS__SESSION_POLICY_H_
#define _INCLUDE__OS__SESSION_POLICY_H_
#include <base/session_label.h>
#include <base/log.h>
#include <session/session.h>
#include <util/arg_string.h>
#include <util/xml_node.h>
namespace Genode {
struct Xml_node_label_score;
template <size_t N, typename MATCH_FN, typename NO_MATCH_FN>
void with_matching_policy(String<N> const &, Xml_node const &,
MATCH_FN const &, NO_MATCH_FN const &);
class Session_policy;
}
/**
* Score for matching an Xml_node against a label
*
* The score is based on the attributes 'label', 'label_prefix', and
* 'label_suffix'.
*/
struct Genode::Xml_node_label_score
{
bool label_present = true;
bool prefix_present = true;
bool suffix_present = true;
bool label_match = false;
/*
* The match values contain the number of matching characters + 1.
* If 0, there is a conflict. If 1, an empty string matched.
*/
enum { CONFLICT = 0 };
size_t prefix_match = CONFLICT;
size_t suffix_match = CONFLICT;
Xml_node_label_score() { }
template <size_t N>
Xml_node_label_score(Xml_node node, String<N> const &label)
:
label_present (node.has_attribute("label")),
prefix_present(node.has_attribute("label_prefix")),
suffix_present(node.has_attribute("label_suffix"))
{
if (label_present)
label_match = node.attribute_value("label", String<N>()) == label;
if (prefix_present) {
typedef String<N> Prefix;
Prefix const prefix = node.attribute_value("label_prefix", Prefix());
if (!strcmp(label.string(), prefix.string(), prefix.length() - 1))
prefix_match = prefix.length();
}
if (suffix_present) {
typedef String<N> Suffix;
Suffix const suffix = node.attribute_value("label_suffix", Suffix());
if (label.length() >= suffix.length()) {
size_t const offset = label.length() - suffix.length();
if (!strcmp(label.string() + offset, suffix.string()))
suffix_match = suffix.length();
}
}
}
bool conflict() const
{
return (label_present && !label_match)
|| (prefix_present && !prefix_match)
|| (suffix_present && !suffix_match);
}
/**
* Return true if this node's score is higher than 'other'
*/
bool stronger(Xml_node_label_score const &other) const
{
/* something must match */
if (!(label_present || prefix_present || suffix_present))
return false;
/* if we are in conflict, we have a lower score than any other node */
if (conflict())
return false;
/* there are no conflicts */
/* we have a higher score than another conflicting node */
if (other.conflict())
return true;
if (label_present && !other.label_present)
return true;
if (other.label_present)
return false;
/* labels are equally good */
if (prefix_present && !other.prefix_present)
return true;
if (!prefix_present && other.prefix_present)
return false;
if (prefix_present && other.prefix_present) {
if (prefix_match > other.prefix_match)
return true;
if (prefix_match < other.prefix_match)
return false;
}
/* prefixes are equally good */
if (suffix_present && !other.suffix_present)
return true;
if (!suffix_present && other.suffix_present)
return false;
if (suffix_present && other.suffix_present) {
if (suffix_match > other.suffix_match)
return true;
if (suffix_match < other.suffix_match)
return false;
}
/* nodes are equally good */
return false;
}
};
/**
* Call 'match_fn' with the policy that matches best the given 'label'
*
* \param policies XML node that contains potentially many '<policy>'
* nodes and an optional '<default-policy>' node.
* \param match_fn functor called with best matching policy XML node
* argmument
* \param no_match_fn functor called if no matching policy exists
*/
template <Genode::size_t N, typename MATCH_FN, typename NO_MATCH_FN>
void Genode::with_matching_policy(String<N> const &label,
Xml_node const &policies,
MATCH_FN const &match_fn,
NO_MATCH_FN const &no_match_fn)
{
/*
* Find policy node that matches best
*/
Xml_node best_match("<none/>");
Xml_node_label_score best_score;
policies.for_each_sub_node("policy", [&] (Xml_node const &policy) {
Xml_node_label_score const score(policy, label);
if (score.stronger(best_score)) {
best_match = policy;
best_score = score;
}
});
/* fall back to default policy if no match exists */
if (best_match.has_type("none"))
policies.with_optional_sub_node("default-policy", [&] (Xml_node const &policy) {
best_match = policy; });
if (best_match.has_type("none"))
no_match_fn();
else
match_fn(best_match);
}
/**
* Query server-side policy for a session request
*/
class Genode::Session_policy : public Xml_node
{
public:
/**
* Exception type
*/
class No_policy_defined : public Service_denied { };
private:
/**
* Query session policy from session label
*/
template <size_t N>
static Xml_node _query_policy(String<N> const &label, Xml_node config)
{
Xml_node result("<none/>");
with_matching_policy(label, config,
[&] (Xml_node const &policy) {
result = policy; },
[&] () {
warning("no policy defined for label '", label, "'");
throw No_policy_defined(); });
return result;
}
public:
/**
* Constructor
*
* \param label label used as the selector of a policy
* \param config XML node that contains the policies as sub nodes
*
* \throw No_policy_defined the server configuration has no
* policy defined for the specified label
*
* On construction, the 'Session_policy' looks up the 'policy' XML node
* that matches the label provided as argument. The server-side
* policies are defined in one or more policy subnodes of the server's
* 'config' node. Each policy node has a label attribute. If the policy
* label matches the first part of the label as delivered as session
* argument, the policy matches. If multiple policies match, the one
* with the longest label is selected.
*/
template <size_t N>
Session_policy(String<N> const &label, Xml_node config)
:
Xml_node(_query_policy(label, config))
{ }
};
#endif /* _INCLUDE__OS__SESSION_POLICY_H_ */