tizen beta release
[framework/web/wrt-installer.git] / src / security / security_logic.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  * This class simply redirects the access requests to access control engine.
18  * The aim is to hide access control engine specific details from WRT modules.
19  * It also implements WRT_INTERFACE.h interfaces, so that ACE could access
20  * WRT specific and other information during the decision making.
21  *
22  * @file    security_controller.h
23  # @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
24  * @author  Ming Jin(ming79.jin@samsung.com)
25  * @author  Piotr Kozbial (p.kozbial@samsung.com)
26  * @version 1.0
27  * @brief   Header file for security logic
28  */
29
30 #include <dpl/ace/PromptDecision.h>
31 #include <security_logic.h>
32 #include <attribute_facade.h>
33 #ifdef WRT_SMACK_ENABLED
34 #include <privilege-control.h>
35 #endif
36 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
37
38 void SecurityLogic::initialize() {
39     m_policyEnforcementPoint.initialize(new WebRuntimeImpl(),
40                                         new ResourceInformationImpl(),
41                                         new OperationSystemImpl());
42 }
43
44 void SecurityLogic::terminate() {
45     m_policyEnforcementPoint.terminate();
46 }
47
48 void SecurityLogic::authorizeWidgetInstall(
49     Request *request,
50     AbstractPolicyEnforcementPoint::ResponseReceiver receiver)
51 {
52     PolicyResult result = m_policyEnforcementPoint.check(*request);
53
54     // this is bad idea, what about context in request ??
55     // We could resolve problem with memory allocation by adding default
56     // constructor to Request and pass object by value.
57     delete request;
58
59     receiver(result);
60 }
61
62 PolicyResult SecurityLogic::checkFunctionCall(Request* request)
63 {
64     PolicyResult aceResult = m_policyEnforcementPoint.check(*request);
65     if (aceResult == PolicyEffect::PERMIT) {
66 #ifdef WRT_SMACK_ENABLED
67       try {
68         WrtDB::WidgetDAOReadOnly dao(request->getWidgetHandle());
69         DPL::OptionalString pkgName = dao.getPkgname();
70         Assert(!pkgName.IsNull() && "widget doesn't have a pkg name");
71         const char *devCap = "";
72         int ret = grant_rules_forWAC(DPL::ToUTF8String(*pkgName).c_str(), devCap);
73         Assert(ret==PC_OPERATION_SUCCESS && "smack rules couldn't be granted");
74       } catch (WrtDB::WidgetDAOReadOnly::Exception) {
75         Assert(false && "can't access widget data");
76       }
77 #endif
78       return PolicyEffect::PERMIT;
79     } else if (aceResult == PolicyEffect::PROMPT_ONESHOT ||
80                aceResult == PolicyEffect::PROMPT_SESSION ||
81                aceResult == PolicyEffect::PROMPT_BLANKET)
82     {
83         // TODO: check stored user answers!!!
84         // if necessary, grant SMACK rules
85         // return appropriately - the following is a dummy:
86         return aceResult;
87     } else {
88         return PolicyEffect::DENY;
89     }
90 }
91
92 //void SecurityLogic::setWidgetPreference(
93 //    std::string devCap,
94 //    WidgetHandle widgetHandle,
95 //    AceDB::PreferenceTypes preference)
96 //{
97 //    m_aceSettingsLogic.setWidgetPreference(devCap,
98 //                                           widgetHandle,
99 //                                           preference);
100 //}
101 //
102 //void SecurityLogic::setResourcePreference(
103 //    std::string devCap,
104 //    AceDB::PreferenceTypes preference)
105 //{
106 //    m_aceSettingsLogic.setResourcePreference(devCap, preference);
107 //}
108 //
109 //AceSettings::WidgetsPreferences SecurityLogic::getWidgetsPreferences() {
110 //    return m_aceSettingsLogic.getWidgetsPreferences();
111 //}
112 //
113 //AceSettings::ResourcesPreferences SecurityLogic::getResourcesPreferences() {
114 //    return m_aceSettingsLogic.getResourcesPreferences();
115 //}
116 //
117 //void SecurityLogic::resetWidgetsPreferences() {
118 //    m_aceSettingsLogic.resetWidgetsPreferences();
119 //}
120 //
121 //void SecurityLogic::resetResourcesPreferences() {
122 //    m_aceSettingsLogic.resetResourcesPreferences();
123 //}