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);
52 void TaskSmack::SmackFolderLabelingStep()
55 "----------------> SMACK: \
56 Jobs::WidgetInstall::TaskSmack::SmackFolderLabelingStep()");
58 #ifdef WRT_SMACK_ENABLED
59 /* /opt/usr/apps/[pkgid] directory's label is "_" */
60 std::string tzPkgid = DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
61 if (PC_OPERATION_SUCCESS != app_label_dir("_",
63 getPackageInstallationDir().
66 LogError("Set smack failure. Failed to add label for app root directory");
67 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
72 std::string resDir = m_context.locations->getPackageInstallationDir() +
74 if (PC_OPERATION_SUCCESS != app_label_dir(tzPkgid.c_str(),
77 LogError("Set smack failure. Failed to add label for resource directory");
78 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
83 if (PC_OPERATION_SUCCESS != app_label_dir(tzPkgid.c_str(),
84 m_context.locations->getBinaryDir()
87 LogError("Set smack failure. Failed to add label for binary directory");
88 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
93 if (PC_OPERATION_SUCCESS != app_label_dir(tzPkgid.c_str(),
95 getPrivateStorageDir().c_str()))
97 LogError("Set smack failure. Failed to add label for private storage directory");
98 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
105 void TaskSmack::SmackPrivilegeStep()
108 "----------------> SMACK: \
109 Jobs::WidgetInstall::TaskSmack::SmackPrivilegeStep()");
110 #ifdef WRT_SMACK_ENABLED
111 WrtDB::TizenPkgId tzPkgid = m_context.widgetConfig.tzPkgid;
113 char** perm_list = new char*[m_context.staticPermittedDevCaps.size()];
116 FOREACH(it, m_context.staticPermittedDevCaps) {
118 LogInfo("Permission : " << it->first);
120 const_cast<char*>(DPL::ToUTF8String(it->first).c_str());
123 perm_list[index] = NULL;
125 int result = app_add_permissions(
126 DPL::ToUTF8String(tzPkgid).c_str(),
127 const_cast<const char**>(perm_list));
130 const char* perm_list[0];
133 if (m_context.job->getInstallerStruct().m_installMode
134 != InstallMode::INSTALL_MODE_PRELOAD)
136 int result = app_add_permissions(
137 DPL::ToUTF8String(tzPkgid).c_str(), perm_list);
138 if (PC_OPERATION_SUCCESS != result) {
139 LogError("Failed to add permission to privilege");
140 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
141 "SMACK check failure");
145 m_context.job->UpdateProgress(
146 InstallerContext::INSTALL_SMACK_ENABLE,
147 "Widget SMACK Enabled");
151 void TaskSmack::SmackTemporaryStep()
153 #ifdef WRT_SMACK_ENABLED
154 //This step is temporary for smack
156 LogInfo("----------------> SMACK: \
157 Jobs::WidgetInstall::TaskSmack::SmackTemporaryStep()");
158 std::ostringstream commStr;
159 std::string tzPkgid = DPL::ToUTF8String(m_context.widgetConfig.tzPkgid);
160 commStr << SMACK_RULE_STR << " " << BashUtils::escape_arg(tzPkgid);
161 LogDebug("set smack rule command : " << commStr.str());
163 char readBuf[MAX_BUF_SIZE];
164 memset(readBuf, 0x00, MAX_BUF_SIZE);
167 fd = popen(commStr.str().c_str(), "r");
169 LogError("Set smack rule failure. Failed to call script.");
170 ThrowMsg(Exceptions::NotAllowed, "Instalation failure. "
171 "SMACK check failure");
177 } //namespace WidgetInstall