Rename invalid WRT_SMACK_LABEL macro to WRT_SMACK_ENABLED
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / task_ace_check.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    task_ace_check.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task ace check
21  */
22
23 #include <utility>
24 #include <vector>
25 #include <string>
26
27 #include <widget_install/task_ace_check.h>
28 #include <dpl/assert.h>
29 #include <dpl/foreach.h>
30
31 #include <widget_install/widget_install_context.h>
32 #include <widget_install/widget_install_errors.h>
33 #include <widget_install/job_widget_install.h>
34
35 #include <dpl/wrt-dao-rw/widget_dao.h>
36 #include <ace_api_install.h>
37
38 #include <installer_log.h>
39
40 namespace Jobs {
41 namespace WidgetInstall {
42 TaskAceCheck::TaskAceCheck(InstallerContext& context) :
43     DPL::TaskDecl<TaskAceCheck>(this),
44     m_context(context)
45 {
46     AddStep(&TaskAceCheck::StartStep);
47     AddStep(&TaskAceCheck::StepPrepareForAce);
48     AddStep(&TaskAceCheck::StepAceCheck);
49     AddStep(&TaskAceCheck::StepProcessAceResponse);
50     AddStep(&TaskAceCheck::StepCheckAceResponse);
51     AddStep(&TaskAceCheck::EndStep);
52 }
53
54 void TaskAceCheck::StepPrepareForAce()
55 {
56     m_context.featureLogic =
57         FeatureLogicPtr(new FeatureLogic(m_context.widgetConfig.tzAppid));
58     m_context.job->UpdateProgress(
59         InstallerContext::INSTALL_ACE_PREPARE,
60         "Widget Access Control Check Prepared");
61 }
62
63 void TaskAceCheck::StepAceCheck()
64 {
65     WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
66     _D("StepAceCheck!");
67     // This widget does not use any device cap
68     if (m_context.featureLogic->isDone()) {
69         return;
70     }
71
72     _D("StepAceCheck!");
73     DPL::String deviceCap = m_context.featureLogic->getDevice();
74
75     _D("StepAceCheck!");
76     _D("DevCap is : %ls", deviceCap.c_str());
77
78     std::string devCapStr = DPL::ToUTF8String(deviceCap);
79     ace_policy_result_t policyResult = ACE_DENY;
80
81     //TODO: remove dao.getHandle()
82     if (m_context.mode.installTime == InstallMode::InstallTime::PRELOAD) {
83         _D("This widget is preloaded. So ace check will be skiped");
84         policyResult = ACE_PERMIT;
85     } else {
86         ace_return_t ret = ace_get_policy_result(
87                 const_cast<const ace_resource_t>(devCapStr.c_str()),
88                 dao.getHandle(),
89                 &policyResult);
90         if (ACE_OK != ret) {
91             ThrowMsg(Exceptions::AceCheckFailed, "Instalation failure. "
92                     "ACE check failure");
93         }
94     }
95
96     _D("PolicyResult is : %d", static_cast<int>(policyResult));
97     m_context.staticPermittedDevCaps.insert(std::make_pair(deviceCap,
98                                                            policyResult ==
99                                                            ACE_PERMIT));
100
101     m_context.featureLogic->setAceResponse(policyResult != ACE_DENY);
102 }
103
104 void TaskAceCheck::StepProcessAceResponse()
105 {
106     WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
107     if (m_context.widgetConfig.packagingType ==
108         WrtDB::PKG_TYPE_HOSTED_WEB_APP)
109     {
110         return;
111     }
112
113     _D("StepProcessAceResponse");
114     m_context.featureLogic->next();
115
116     // No device caps left to process
117     if (m_context.featureLogic->isDone()) {
118         _D("All responses has been received from ACE.");
119         // Data to convert to C API
120         std::vector<std::string> devCaps;
121         std::vector<bool> devCapsSmack;
122         // Saving static dev cap permissions
123         FOREACH(cap, m_context.staticPermittedDevCaps) {
124             _D("staticPermittedDevCaps : %ls smack: %d", cap->first.c_str(), cap->second);
125             std::string devCapStr = DPL::ToUTF8String(cap->first);
126             devCaps.push_back(devCapStr);
127             devCapsSmack.push_back(cap->second);
128         }
129         ace_requested_dev_cap_list_t list;
130         list.count = devCaps.size();
131         list.items = new ace_requested_dev_cap_t[list.count];
132
133         for (unsigned int i = 0; i < devCaps.size(); ++i) {
134             list.items[i].device_capability =
135                 const_cast<const ace_resource_t>(devCaps[i].c_str());
136             list.items[i].smack_granted =
137                 devCapsSmack[i] ? ACE_TRUE : ACE_FALSE;
138         }
139         //TODO: remove dao.getHandle()
140         ace_return_t ret = ace_set_requested_dev_caps(dao.getHandle(),
141                                                       &list);
142         if (ACE_OK != ret) {
143             ThrowMsg(Exceptions::AceCheckFailed, "Instalation failure. "
144                                              "ACE failure");
145         }
146         delete[] list.items;
147
148         std::set<std::string> acceptedFeature;
149         auto it = m_context.featureLogic->resultBegin();
150         for (; it != m_context.featureLogic->resultEnd(); ++it) {
151             if (!(it->rejected)) {
152                 acceptedFeature.insert(DPL::ToUTF8String(it->name));
153             }
154         }
155         ace_feature_list_t featureList;
156         featureList.count = acceptedFeature.size();
157         featureList.items = new ace_string_t[featureList.count];
158
159         size_t i = 0;
160         for (std::set<std::string>::const_iterator iter = acceptedFeature.begin();
161              iter != acceptedFeature.end(); ++iter)
162         {
163             _D("Accepted feature item: %s", iter->c_str());
164             featureList.items[i] = const_cast<char *>(iter->c_str());
165             i++;
166         }
167
168         //TODO: remove dao.getHandle()
169         ret = ace_set_accepted_feature(dao.getHandle(), &featureList);
170
171         delete[] featureList.items;
172
173         if (ACE_OK != ret) {
174             _E("Error in ace_set_feature");
175             ThrowMsg(Exceptions::AceCheckFailed, "Instalation failure. "
176                                              "ace_set_feature failure.");
177         }
178         return;
179     }
180
181     _D("Next device cap.");
182     // Process next device cap
183     SwitchToStep(&TaskAceCheck::StepAceCheck);
184 }
185
186 void TaskAceCheck::StepCheckAceResponse()
187 {
188     _D("Checking ACE response");
189     if (m_context.featureLogic->isRejected()) {
190         _E("Installation failure. Some devCap was not accepted by ACE.");
191         ThrowMsg(
192             Exceptions::PrivilegeLevelViolation,
193             "Instalation failure. "
194             "Some deviceCap was not accepted by ACE.");
195     }
196     _D("Updating \"feature reject status\" in database!");
197     auto it = m_context.featureLogic->resultBegin();
198     auto end = m_context.featureLogic->resultEnd();
199     for (; it != end; ++it) {
200         _D("  |-  Feature: %ls has reject status: %d", it->name.c_str(), it->rejected);
201         if (it->rejected) {
202             WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
203             dao.updateFeatureRejectStatus(*it);
204         }
205     }
206     _D("Installation continues...");
207 }
208
209 void TaskAceCheck::StartStep()
210 {
211     _D("--------- <TaskAceCheck> : START ----------");
212 }
213
214 void TaskAceCheck::EndStep()
215 {
216     m_context.job->UpdateProgress(
217         InstallerContext::INSTALL_ACE_CHECK,
218         "Widget Access Control Check Finished");
219
220     _D("--------- <TaskAceCheck> : END ----------");
221 }
222 } //namespace WidgetInstall
223 } //namespace Jobs