[Installer] Add installation drm file
[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     WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
55     m_context.featureLogic =
56         FeatureLogicPtr(new FeatureLogic(dao.getHandle()));
57     m_context.job->UpdateProgress(
58         InstallerContext::INSTALL_ACE_PREPARE,
59         "Widget Access Control Check Prepared");
60 }
61
62 void TaskAceCheck::StepAceCheck()
63 {
64     WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
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     std::string devCapStr = DPL::ToUTF8String(deviceCap);
78     ace_policy_result_t policyResult = ACE_DENY;
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 == ACE_PERMIT));
91
92     m_context.featureLogic->setAceResponse(policyResult != ACE_DENY);
93 }
94
95 void TaskAceCheck::StepProcessAceResponse()
96 {
97     WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
98     if (m_context.widgetConfig.packagingType ==
99             WrtDB::PKG_TYPE_HOSTED_WEB_APP) {
100         return;
101     }
102
103     LogInfo("StepProcessAceResponse");
104     m_context.featureLogic->next();
105
106     // No device caps left to process
107     if (m_context.featureLogic->isDone()) {
108         LogInfo("All responses has been received from ACE.");
109         // Data to convert to C API
110         std::vector<std::string> devCaps;
111         std::vector<bool> devCapsSmack;
112         // Saving static dev cap permissions
113         FOREACH (cap, m_context.staticPermittedDevCaps) {
114             LogInfo("staticPermittedDevCaps : " << cap->first
115                     << " smack: " << cap->second);
116             std::string devCapStr = DPL::ToUTF8String(cap->first);
117             devCaps.push_back(devCapStr);
118             devCapsSmack.push_back(cap->second);
119         }
120         ace_requested_dev_cap_list_t list;
121         list.count = devCaps.size();
122         list.items = new ace_requested_dev_cap_t[list.count];
123
124         for (unsigned int i = 0; i < devCaps.size(); ++i) {
125             list.items[i].device_capability =
126                     const_cast<const ace_resource_t>(devCaps[i].c_str());
127             list.items[i].smack_granted =
128                     devCapsSmack[i] ? ACE_TRUE : ACE_FALSE;
129         }
130         ace_return_t ret = ace_set_requested_dev_caps(dao.getHandle(),  //TODO: (ace_widget_handle_t not int needed)
131                                                       &list);
132         if (ACE_OK != ret) {
133             ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
134                      "ACE failure");
135         }
136         delete [] list.items;
137
138         std::set<std::string> acceptedFeature;
139         auto it = m_context.featureLogic->resultBegin();
140         for (;it != m_context.featureLogic->resultEnd(); ++it) {
141             if (!(it->rejected)) {
142                 acceptedFeature.insert(DPL::ToUTF8String(it->name));
143             }
144         }
145         ace_feature_list_t featureList;
146         featureList.count = acceptedFeature.size();
147         featureList.items = new ace_string_t[featureList.count];
148
149         size_t i=0;
150         for (std::set<std::string>::const_iterator iter = acceptedFeature.begin();
151                 iter != acceptedFeature.end(); ++iter) {
152             LogDebug("Accepted feature item: " << iter->c_str());
153             featureList.items[i] = const_cast<char *>(iter->c_str());
154             i++;
155         }
156
157         ret = ace_set_accepted_feature(dao.getHandle(), &featureList);
158
159         delete [] featureList.items;
160
161         if (ACE_OK != ret) {
162             LogError("Error in ace_set_feature");
163             ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
164               "ace_set_feature failure.");
165         }
166         return;
167     }
168
169     LogInfo("Next device cap.");
170     // Process next device cap
171     SwitchToStep(&TaskAceCheck::StepAceCheck);
172 }
173
174 void TaskAceCheck::StepCheckAceResponse()
175 {
176     LogInfo("Checking ACE response");
177     if (m_context.featureLogic->isRejected()) {
178         LogError("Installation failure. Some devCap was not accepted by ACE.");
179         ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
180             "Some deviceCap was not accepted by ACE.");
181     }
182     LogInfo("Updating \"feature reject status\" in database!");
183     auto it = m_context.featureLogic->resultBegin();
184     auto end = m_context.featureLogic->resultEnd();
185     for(;it != end; ++it){
186         LogInfo("  |-  Feature: " << it->name << " has reject status: " << it->rejected);
187         if (it->rejected) {
188             WrtDB::WidgetDAO dao(m_context.locations->getPkgname());
189             dao.updateFeatureRejectStatus(*it);
190         }
191     }
192     LogInfo("Installation continues...");
193
194     m_context.job->UpdateProgress(
195         InstallerContext::INSTALL_ACE_CHECK,
196         "Widget Access Control Check Finished");
197 }
198
199 } //namespace WidgetInstall
200 } //namespace Jobs