2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_ace_check.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
20 * @brief Implementation file for installer task ace check
27 #include <widget_install/task_ace_check.h>
28 #include <dpl/assert.h>
29 #include <dpl/foreach.h>
31 #include <widget_install/widget_install_context.h>
32 #include <widget_install/widget_install_errors.h>
33 #include <widget_install/job_widget_install.h>
35 #include <dpl/wrt-dao-rw/widget_dao.h>
36 #include <ace_api_install.h>
38 #include <installer_log.h>
41 namespace WidgetInstall {
42 TaskAceCheck::TaskAceCheck(InstallerContext& context) :
43 DPL::TaskDecl<TaskAceCheck>(this),
46 AddStep(&TaskAceCheck::StartStep);
47 AddStep(&TaskAceCheck::StepPrepareForAce);
48 AddStep(&TaskAceCheck::StepAceCheck);
49 AddStep(&TaskAceCheck::StepProcessAceResponse);
50 AddStep(&TaskAceCheck::StepCheckAceResponse);
51 AddStep(&TaskAceCheck::EndStep);
54 void TaskAceCheck::StepPrepareForAce()
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");
63 void TaskAceCheck::StepAceCheck()
65 WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
67 // This widget does not use any device cap
68 if (m_context.featureLogic->isDone()) {
73 DPL::String deviceCap = m_context.featureLogic->getDevice();
76 _D("DevCap is : %ls", deviceCap.c_str());
78 std::string devCapStr = DPL::ToUTF8String(deviceCap);
79 ace_policy_result_t policyResult = ACE_DENY;
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;
86 ace_return_t ret = ace_get_policy_result(
87 const_cast<const ace_resource_t>(devCapStr.c_str()),
91 ThrowMsg(Exceptions::AceCheckFailed, "Instalation failure. "
96 _D("PolicyResult is : %d", static_cast<int>(policyResult));
97 m_context.staticPermittedDevCaps.insert(std::make_pair(deviceCap,
101 m_context.featureLogic->setAceResponse(policyResult != ACE_DENY);
104 void TaskAceCheck::StepProcessAceResponse()
106 WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
107 if (m_context.widgetConfig.packagingType ==
108 WrtDB::PKG_TYPE_HOSTED_WEB_APP)
113 _D("StepProcessAceResponse");
114 m_context.featureLogic->next();
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);
129 ace_requested_dev_cap_list_t list;
130 list.count = devCaps.size();
131 list.items = new ace_requested_dev_cap_t[list.count];
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;
139 //TODO: remove dao.getHandle()
140 ace_return_t ret = ace_set_requested_dev_caps(dao.getHandle(),
143 ThrowMsg(Exceptions::AceCheckFailed, "Instalation failure. "
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));
155 ace_feature_list_t featureList;
156 featureList.count = acceptedFeature.size();
157 featureList.items = new ace_string_t[featureList.count];
160 for (std::set<std::string>::const_iterator iter = acceptedFeature.begin();
161 iter != acceptedFeature.end(); ++iter)
163 _D("Accepted feature item: %s", iter->c_str());
164 featureList.items[i] = const_cast<char *>(iter->c_str());
168 //TODO: remove dao.getHandle()
169 ret = ace_set_accepted_feature(dao.getHandle(), &featureList);
171 delete[] featureList.items;
174 _E("Error in ace_set_feature");
175 ThrowMsg(Exceptions::AceCheckFailed, "Instalation failure. "
176 "ace_set_feature failure.");
181 _D("Next device cap.");
182 // Process next device cap
183 SwitchToStep(&TaskAceCheck::StepAceCheck);
186 void TaskAceCheck::StepCheckAceResponse()
188 _D("Checking ACE response");
189 if (m_context.featureLogic->isRejected()) {
190 _E("Installation failure. Some devCap was not accepted by ACE.");
192 Exceptions::PrivilegeLevelViolation,
193 "Instalation failure. "
194 "Some deviceCap was not accepted by ACE.");
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);
202 WrtDB::WidgetDAO dao(m_context.widgetConfig.tzAppid);
203 dao.updateFeatureRejectStatus(*it);
206 _D("Installation continues...");
209 void TaskAceCheck::StartStep()
211 _D("--------- <TaskAceCheck> : START ----------");
214 void TaskAceCheck::EndStep()
216 m_context.job->UpdateProgress(
217 InstallerContext::INSTALL_ACE_CHECK,
218 "Widget Access Control Check Finished");
220 _D("--------- <TaskAceCheck> : END ----------");
222 } //namespace WidgetInstall