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