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