Tizen 2.1 base
[framework/security/security-server.git] / ace / engine / PolicyEnforcementPoint.cpp
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  * @file    security_logic.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @author  Ming Jin(ming79.jin@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for security logic
22  */
23 #include <ace/PolicyEnforcementPoint.h>
24
25 #include <sstream>
26 #include <algorithm>
27 #include <list>
28 #include <string>
29 #include <sstream>
30 #include <stdexcept>
31 #include <cstdlib>
32 #include <map>
33
34 #include <dpl/assert.h>
35 #include <dpl/exception.h>
36 #include <dpl/log/log.h>
37
38 #include <ace/PolicyEvaluatorFactory.h>
39 #include <ace/PolicyResult.h>
40 #include <ace/Request.h>
41
42 PolicyEnforcementPoint::PolicyEnforcementPoint() :
43     m_wrt(0),
44     m_res(0),
45     m_sys(0),
46     m_pdp(0),
47     m_pip(0)
48 {}
49
50 void PolicyEnforcementPoint::terminate()
51 {
52     LogInfo("PolicyEnforcementPoint is being deinitialized.");
53
54     delete m_sys;
55     delete m_res;
56     delete m_wrt;
57     delete m_pdp;
58     delete m_pip;
59     m_sys = 0;
60     m_res = 0;
61     m_wrt = 0;
62     m_pdp = 0;
63     m_pip = 0;
64 }
65
66 PolicyEnforcementPoint::~PolicyEnforcementPoint()
67 {
68     Assert((m_sys == 0) && "You must run "
69            "PolicyEnforcementPoint::Deinitialize before exit program!");
70 }
71
72 void PolicyEnforcementPoint::initialize(
73         IWebRuntime *wrt,
74         IResourceInformation *resource,
75         IOperationSystem *operation)
76 {
77     if (m_wrt) {
78         ThrowMsg(PEPException::AlreadyInitialized,
79                  "Policy Enforcement Point is already initialzed");
80     }
81
82     m_wrt = wrt;
83     m_res = resource;
84     m_sys = operation;
85
86     if (this->m_pip != NULL) {
87         this->m_pip->update(m_wrt, m_res, m_sys);
88         return;
89     }
90
91     this->m_pip = new PolicyInformationPoint(wrt, m_res, m_sys);
92     this->m_pdp = new PolicyEvaluator(m_pip);
93
94     if (!this->m_pdp->initPDP()) {
95         Assert(0);
96     }
97 }
98
99 ExtendedPolicyResult PolicyEnforcementPoint::check(Request &request)
100 {
101     return m_pdp->getPolicyForRequest(request);
102 }
103
104 void PolicyEnforcementPoint::updatePolicy(const std::string &policy)
105 {
106     LogDebug("ACE updatePolicy: " << policy);
107     int errorCode = 0;
108
109     if (m_pdp == NULL) {
110         LogError("Evaluator not set. Ignoring message.");
111         Assert(false && "UpdateClient error on receiving event");
112     } else {
113         LogDebug("Emitting update signal.");
114         errorCode = m_pdp->updatePolicy(policy.c_str());
115     }
116
117     LogDebug("Sending reponse: " << errorCode);
118 }
119
120 void PolicyEnforcementPoint::updatePolicy()
121 {
122     LogDebug("ACE updatePolicy");
123     if (m_pdp == NULL) {
124         LogError("Evaluator not set. Ignoring message.");
125     } else {
126         m_pdp->updatePolicy();
127     }
128 }
129
130 OptionalExtendedPolicyResult PolicyEnforcementPoint::checkFromCache(Request &request)
131 {
132    return m_pdp->getPolicyForRequestFromCache(request);
133 }
134
135 OptionalExtendedPolicyResult PolicyEnforcementPoint::check(Request &request,
136                                                    bool fromCacheOnly)
137 {
138    return m_pdp->getPolicyForRequest(request, fromCacheOnly);
139 }