f59fe80a170adb88bab35ca57dc5085733d85793
[framework/security/security-server.git] / ace / include / ace / PolicyResult.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #ifndef _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_
18 #define _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_
19
20 #include <dpl/assert.h>
21 #include <dpl/optional.h>
22 #include <dpl/optional_typedefs.h>
23
24 #include <ace/PolicyEffect.h>
25
26 typedef DPL::Optional<PolicyEffect> OptionalPolicyEffect;
27
28 class PolicyDecision
29 {
30 public:
31     enum Value { NOT_APPLICABLE = -1 };
32
33     PolicyDecision(PolicyEffect effect)
34       : m_isPolicyEffect(true)
35       , m_effect(effect)
36     {}
37
38     PolicyDecision(const PolicyDecision &decision)
39       : m_isPolicyEffect(decision.m_isPolicyEffect)
40       , m_effect(decision.m_effect)
41     {}
42
43     PolicyDecision(Value)
44       : m_isPolicyEffect(false)
45     {}
46
47     bool operator==(const PolicyDecision &decision) const {
48         return (m_isPolicyEffect
49                  && decision.m_isPolicyEffect
50                  && m_effect == decision.m_effect)
51                || (!m_isPolicyEffect && !decision.m_isPolicyEffect);
52     }
53
54     bool operator==(Value) const {
55         return !m_isPolicyEffect;
56     }
57
58     bool operator!=(const PolicyDecision &decision) const {
59         return !(*this == decision);
60     }
61
62     bool operator!=(Value value) const {
63         return !(*this == value);
64     }
65
66     OptionalPolicyEffect getEffect() const
67     {
68         if (!m_isPolicyEffect) {
69             return OptionalPolicyEffect();
70         }
71         return OptionalPolicyEffect(m_effect);
72     }
73
74     std::ostream & toStream(std::ostream& stream) {
75         if (m_isPolicyEffect)
76             stream << m_effect;
77         else
78             stream << "NOT-APPLICABLE";
79         return stream;
80     }
81
82 private:
83     bool m_isPolicyEffect;
84     PolicyEffect m_effect;
85 };
86
87 inline static bool operator==(PolicyEffect e, const PolicyDecision &d) {
88   return d.operator==(e);
89 }
90
91 inline static bool operator!=(PolicyEffect e, const PolicyDecision &d) {
92   return !(e == d);
93 }
94
95 inline static std::ostream & operator<<(std::ostream& stream,
96                                         PolicyDecision decision)
97 {
98     return decision.toStream(stream);
99 }
100
101 class PolicyResult {
102 public:
103     enum Value { UNDETERMINED = -2 };
104
105     // This constructor is required by dpl controller and by dpl optional
106     PolicyResult()
107       : m_isDecision(false)
108       , m_decision(PolicyDecision::Value::NOT_APPLICABLE) // don't care
109     {}
110
111     PolicyResult(PolicyEffect effect)
112       : m_isDecision(true)
113       , m_decision(effect)
114     {}
115
116     PolicyResult(const PolicyDecision &decision)
117       : m_isDecision(true)
118       , m_decision(decision)
119     {}
120
121     PolicyResult(const PolicyResult &result)
122       : m_isDecision(result.m_isDecision)
123       , m_decision(result.m_decision)
124     {}
125
126     PolicyResult(PolicyDecision::Value value)
127       : m_isDecision(true)
128       , m_decision(value)
129     {}
130
131     PolicyResult(Value)
132       : m_isDecision(false)
133       , m_decision(PolicyDecision::Value::NOT_APPLICABLE) // don't care
134     {}
135
136     bool operator==(const PolicyResult &result) const {
137           return (m_isDecision
138                 && result.m_isDecision
139                 && m_decision == result.m_decision)
140                 || (!m_isDecision && !result.m_isDecision);
141     }
142
143     bool operator==(Value) const {
144         return !m_isDecision;
145     }
146
147     bool operator!=(const PolicyResult &result) const {
148         return !(*this == result);
149     }
150
151     bool operator!=(Value value) const {
152         return !(*this == value);
153     }
154
155     OptionalPolicyEffect getEffect() const
156     {
157         if (!m_isDecision) {
158             return OptionalPolicyEffect();
159         }
160         return m_decision.getEffect();
161     }
162
163     static int serialize(const PolicyResult &policyResult)
164     {
165         if (!policyResult.m_isDecision) {
166             return BD_UNDETERMINED;
167         } else if (policyResult.m_decision ==
168             PolicyDecision::Value::NOT_APPLICABLE)
169         {
170             return BD_NOT_APPLICABLE;
171         } else if (policyResult.m_decision == PolicyEffect::PROMPT_BLANKET) {
172             return BD_PROMPT_BLANKET;
173         } else if (policyResult.m_decision == PolicyEffect::PROMPT_SESSION) {
174             return BD_PROMPT_SESSION;
175         } else if (policyResult.m_decision == PolicyEffect::PROMPT_ONESHOT) {
176             return BD_PROMPT_ONESHOT;
177         } else if (policyResult.m_decision == PolicyEffect::PERMIT) {
178             return BD_PERMIT;
179         } else if (policyResult.m_decision == PolicyEffect::DENY) {
180             return BD_DENY;
181         }
182         Assert(false && "Unknown value of policyResult.");
183     }
184
185     static PolicyResult deserialize(int dec){
186         switch (dec) {
187             case BD_DENY:
188                 return PolicyEffect::DENY;
189             case BD_PERMIT:
190                 return PolicyEffect::PERMIT;
191             case BD_PROMPT_ONESHOT:
192                 return PolicyEffect::PROMPT_ONESHOT;
193             case BD_PROMPT_SESSION:
194                 return PolicyEffect::PROMPT_SESSION;
195             case BD_PROMPT_BLANKET:
196                 return PolicyEffect::PROMPT_BLANKET;
197             case BD_NOT_APPLICABLE:
198                 return PolicyDecision::Value::NOT_APPLICABLE;
199             case BD_UNDETERMINED:
200                 return Value::UNDETERMINED;
201         }
202         Assert(false && "Broken database");
203     }
204
205     std::ostream & toStream(std::ostream& stream) {
206         if (m_isDecision)
207             stream << m_decision;
208         else
209             stream << "UNDETERMINED";
210         return stream;
211     }
212
213 private:
214     static const int BD_UNDETERMINED = 6;
215     static const int BD_NOT_APPLICABLE = 5;
216     static const int BD_PROMPT_BLANKET = 4;
217     static const int BD_PROMPT_SESSION = 3;
218     static const int BD_PROMPT_ONESHOT = 2;
219     static const int BD_PERMIT = 1;
220     static const int BD_DENY = 0;
221
222     bool m_isDecision;
223     PolicyDecision m_decision;
224 };
225
226 inline static bool operator==(const PolicyDecision &d, const PolicyResult &r) {
227     return r == d;
228 }
229
230 inline static bool operator!=(const PolicyDecision &d, const PolicyResult &r) {
231     return !(d == r);
232 }
233
234 inline static bool operator==(const PolicyEffect &e, const PolicyResult &r) {
235     return e == r;
236 }
237
238 inline static bool operator!=(const PolicyEffect &e, const PolicyResult &r) {
239     return !(e == r);
240 }
241
242 inline static std::ostream & operator<<(std::ostream& stream,
243                                         PolicyResult result)
244 {
245     return result.toStream(stream);
246 }
247
248 struct ExtendedPolicyResult {
249     ExtendedPolicyResult(const PolicyResult pr = PolicyEffect::DENY, int rule = -1)
250       : policyResult(pr)
251       , ruleId(rule)
252     {}
253     PolicyResult policyResult;
254     int ruleId;
255 };
256
257 typedef DPL::Optional<ExtendedPolicyResult> OptionalExtendedPolicyResult;
258
259 #endif // _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_