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_smack.cpp
18 * @author Piotr Kozbial (p.kozbial@samsung.com)
20 * @brief Implementation file for installer task smack
23 #include <widget_install/task_smack.h>
24 #include <widget_install/widget_install_context.h>
25 #include <widget_install/widget_install_errors.h>
26 #include <widget_install/job_widget_install.h>
27 #include <dpl/foreach.h>
28 #include <dpl/wrt-dao-ro/common_dao_types.h>
29 #include <dpl/utils/bash_utils.h>
30 #ifdef WRT_SMACK_ENABLED
31 #include <privilege-control.h>
37 const int MAX_BUF_SIZE = 128;
38 const char* SMACK_RULE_STR = "/usr/bin/smackload-app.sh";
42 namespace WidgetInstall {
43 TaskSmack::TaskSmack(InstallerContext& context) :
44 DPL::TaskDecl<TaskSmack>(this),
47 AddStep(&TaskSmack::SmackFolderLabelingStep);
48 AddStep(&TaskSmack::SmackPrivilegeStep);
49 AddStep(&TaskSmack::SmackTemporaryStep);
50 AddStep(&TaskSmack::SetEndofInstallation);
53 void TaskSmack::SmackFolderLabelingStep()
56 "----------------> SMACK: \
57 Jobs::WidgetInstall::TaskSmack::SmackFolderLabelingStep()");
59 #ifdef WRT_SMACK_ENABLED
60 /* /opt/usr/apps/[pkgid] directory's label is "_" */
61 std::string tzPkgid = DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
62 if (PC_OPERATION_SUCCESS != app_label_dir("_",
64 getPackageInstallationDir().
67 LogError("Set smack failure. Failed to add label for app root directory");
68 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
73 std::string resDir = m_context.locations->getPackageInstallationDir() +
75 if (PC_OPERATION_SUCCESS != app_label_dir(tzPkgid.c_str(),
78 LogError("Set smack failure. Failed to add label for resource directory");
79 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
84 if (PC_OPERATION_SUCCESS != app_label_dir(tzPkgid.c_str(),
85 m_context.locations->getBinaryDir()
88 LogError("Set smack failure. Failed to add label for binary directory");
89 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
94 if (PC_OPERATION_SUCCESS != app_label_dir(tzPkgid.c_str(),
96 getPrivateStorageDir().c_str()))
98 LogError("Set smack failure. Failed to add label for private storage directory");
99 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
100 "Add Label failure");
106 void TaskSmack::SmackPrivilegeStep()
109 "----------------> SMACK: \
110 Jobs::WidgetInstall::TaskSmack::SmackPrivilegeStep()");
111 #ifdef WRT_SMACK_ENABLED
112 WrtDB::TizenPkgId tzPkgid = m_context.widgetConfig.tzPkgid;
114 char** perm_list = new char*[m_context.staticPermittedDevCaps.size()];
117 FOREACH(it, m_context.staticPermittedDevCaps) {
119 LogInfo("Permission : " << it->first);
121 const_cast<char*>(DPL::ToUTF8String(it->first).c_str());
124 perm_list[index] = NULL;
126 int result = app_add_permissions(
127 DPL::ToUTF8String(tzPkgid).c_str(),
128 const_cast<const char**>(perm_list));
131 const char* perm_list[0];
134 if (m_context.job->getInstallerStruct().m_installMode
135 != InstallMode::INSTALL_MODE_PRELOAD)
137 int result = app_add_permissions(
138 DPL::ToUTF8String(tzPkgid).c_str(), perm_list);
139 if (PC_OPERATION_SUCCESS != result) {
140 LogError("Failed to add permission to privilege");
141 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
142 "SMACK check failure");
146 m_context.job->UpdateProgress(
147 InstallerContext::INSTALL_SMACK_ENABLE,
148 "Widget SMACK Enabled");
152 void TaskSmack::SmackTemporaryStep()
154 #ifdef WRT_SMACK_ENABLED
155 //This step is temporary for smack
157 LogInfo("----------------> SMACK: \
158 Jobs::WidgetInstall::TaskSmack::SmackTemporaryStep()");
159 std::ostringstream commStr;
160 std::string tzPkgid = DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
161 commStr << SMACK_RULE_STR << " " << BashUtils::escape_arg(tzPkgid);
162 LogDebug("set smack rule command : " << commStr.str());
164 char readBuf[MAX_BUF_SIZE];
165 memset(readBuf, 0x00, MAX_BUF_SIZE);
168 fd = popen(commStr.str().c_str(), "r");
170 LogError("Set smack rule failure. Failed to call script.");
171 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
172 "SMACK check failure");
178 void TaskSmack::SetEndofInstallation()
180 m_context.job->UpdateProgress(
181 InstallerContext::INSTALL_END,
184 } //namespace WidgetInstall