2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file SmackManager.cpp
19 * @brief This is the implementation file for %SmackManager class.
23 #include <unique_ptr.h>
26 #include <FIoRegistry.h>
27 #include <FSecCryptoSha1Hash.h>
28 #include <FBase_StringConverter.h>
30 #include "SmackManager.h"
31 #include "InstallerDefs.h"
32 #include "InstallerUtil.h"
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Utility;
37 using namespace Tizen::Security::Cert;
38 using namespace Tizen::Security::Crypto;
39 using namespace Tizen::Io;
41 SmackManager::SmackManager(void)
42 :__isSmackEnable(false)
44 if (IsSmackEnable() == true)
46 __isSmackEnable = true;
50 SmackManager::~SmackManager(void)
55 SmackManager::AddLabelDir(const String& label, const String& dirPath, bool rootDirectory)
57 if (__isSmackEnable == false)
65 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
66 TryReturn(pPath, false, "pPath is null.");
68 if (InstallerUtil::IsSymlink(dirPath) == true)
70 res = AddLabelDir("_", pPath.get());
71 InstallerUtil::GetRealPath(dirPath, realPath);
78 std::unique_ptr<char[]> pRealPath(_StringConverter::CopyToCharArrayN(realPath));
79 TryReturn(pRealPath, false, "pRealPath is null");
81 if (rootDirectory == true)
83 res = AddLabelDir("_", pRealPath.get());
85 else if (dirPath.Contains(L"shared") == true)
87 res = AddLabelDir("*", pRealPath.get());
91 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(label));
92 TryReturn(pPackageId, false, "pPackageId is null");
94 res = AddLabelDir(pPackageId.get(), pRealPath.get());
101 SmackManager::AddLabelSharedDir(const PackageId& packageId, const String& dirPath, InstallationContext* pContext)
103 if (__isSmackEnable == false)
108 if (pContext->__isVerificationMode == false)
110 AppLog("VerificationMode is off.");
115 String label = packageId;
117 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
118 TryReturn(pPackageId, false, "pPackageId is null.");
120 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
121 TryReturn(pPath, false, "pPath is null.");
123 if (dirPath.Contains(L"shared/data") == true)
126 //label.Append("_shareddata");
128 else if (dirPath.Contains(L"shared/res") == true)
131 //label.Append("_sharedres");
133 else if (dirPath.Contains(L"shared/trusted") == true)
138 X509CertificatePath* pAuthorCertPath = pContext->GetAuthorCertPath();
139 TryReturn(pAuthorCertPath, false, "pAuthorCertPath is null.");
141 std::unique_ptr<ICertificate> pEntity(pAuthorCertPath->GetCertificateN(0));
142 TryReturn(pEntity, false, "pEntity is null.");
144 std::unique_ptr<ByteBuffer> pEncodedData(pEntity->GetEncodedDataN());
145 TryReturn(pEncodedData, false, "pEncodedData is null.");
147 std::unique_ptr<ByteBuffer> pHashValue(hash.GetHashN(*pEncodedData.get()));
148 TryReturn(pHashValue, false, "pHashValue is null.");
150 StringUtil::EncodeToBase64String(*pHashValue, base64Value);
152 std::unique_ptr<char[]> pHashEncodedValue(_StringConverter::CopyToCharArrayN(base64Value));
153 TryReturn(pHashEncodedValue, false, "pHashEncodedValue is null.");
155 label = pHashEncodedValue.get();
156 AppLog("pHashEncodedValue = [%s]", pHashEncodedValue.get());
160 AppLog("Invalid Directory = [%ls]", dirPath.GetPointer());
164 std::unique_ptr<char[]> pLabel(_StringConverter::CopyToCharArrayN(label));
165 TryReturn(pLabel, false, "pLabel is null.");
167 res = AddLabelSharedDir(pPackageId.get(), pLabel.get(), pPath.get());
173 SmackManager::AddPermissions(const PackageId& packageId)
175 if (__isSmackEnable == false)
181 const char* pList[] = {"OSP", null};
182 String script("/usr/bin/smackload-app.sh");
183 bool exist = File::IsFileExist(script);
186 script.Append(packageId);
188 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
189 TryReturn(pPackageId, false, "pPackageId is null.");
191 std::unique_ptr<char[]> pScript(_StringConverter::CopyToCharArrayN(script));
192 TryReturn(pScript, false, "pScript is null.");
194 res = AddPermissions(pPackageId.get(), pList);
198 res = system(pScript.get());
199 AppLog("[smack] system(%s), result = [%d]", pScript.get(), res);
203 AppLog("[%ls] not found", script.GetPointer());
210 SmackManager::RevokePermissions(const PackageId& packageId)
212 if (__isSmackEnable == false)
219 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
220 TryReturn(pPackageId, false, "pPackageId is null.");
222 res = RevokePermissions(pPackageId.get());
228 SmackManager::IsSmackEnable()
232 String section(L"feature");
233 String entry(L"smack");
236 r = reg.Construct(CONFIG_PATH, "r");
237 TryReturn(!IsFailed(r), false, "CONFIG file is not found.");
239 r = reg.GetValue(section, entry, value);
240 TryReturn(!IsFailed(r), false, "GetValue is failed. entry = [%ls]", entry.GetPointer());
242 AppLog("[%ls is %ls.]", entry.GetPointer(), value.GetPointer());
253 SmackManager::AddLabelDir(const char* pLabel, const char* pDirPath)
256 void* pHandle = null;
257 char* pErrorMsg = null;
258 int (*app_label_dir)(const char*, const char*) = null;
260 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
263 AppLog("AddLabelDir(): dlopen() failed. [%s]", dlerror());
267 app_label_dir = reinterpret_cast <int (*)(const char*, const char*)>(dlsym(pHandle, "app_label_dir"));
268 pErrorMsg = dlerror();
269 if (pErrorMsg != null)
271 AppLog("AddLabelDir(): dlsym() failed. [%s]", pErrorMsg);
276 ret = app_label_dir(pLabel, pDirPath);
277 AppLog("[smack] app_label_dir(%s, %s), result = [%d]", pLabel, pDirPath, ret);
285 SmackManager::AddLabelSharedDir(const char* pLabel, const char* pSharedLabel, const char* pDirPath)
288 void* pHandle = null;
289 char* pErrorMsg = null;
290 int (*app_label_shared_dir)(const char*, const char*, const char*) = null;
292 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
295 AppLog("AddLabelSharedDir(): dlopen() failed. [%s]", dlerror());
299 app_label_shared_dir = reinterpret_cast <int (*)(const char*, const char*, const char*)>(dlsym(pHandle, "app_label_shared_dir"));
300 pErrorMsg = dlerror();
301 if (pErrorMsg != null)
303 AppLog("AddLabelSharedDir(): dlsym() failed. [%s]", pErrorMsg);
308 ret = app_label_shared_dir(pLabel, pSharedLabel, pDirPath);
309 AppLog("[smack] app_label_shared_dir(%s, %s, %s), result = [%d]", pLabel, pSharedLabel, pDirPath, ret);
317 SmackManager::AddPermissions(const char* pPackageId, const char** ppPermissions)
320 void* pHandle = null;
321 char* pErrorMsg = null;
322 int (*app_add_permissions)(const char*, const char**) = null;
324 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
327 AppLog("AddPermissions(): dlopen() failed. [%s][%s]", pPackageId, dlerror());
331 app_add_permissions = reinterpret_cast <int (*)(const char*, const char**)>(dlsym(pHandle, "app_add_permissions"));
332 pErrorMsg = dlerror();
333 if (pErrorMsg != null)
335 AppLog("AddPermissions(): dlsym() failed. [%s][%s]", pPackageId, pErrorMsg);
340 ret = app_add_permissions(pPackageId, ppPermissions);
341 AppLog("[smack] app_add_permissions(%s), result = [%d]", pPackageId, ret);
349 SmackManager::RevokePermissions(const char* pPackageId)
352 void* pHandle = null;
353 char* pErrorMsg = null;
354 int (*app_revoke_permissions)(const char*) = null;
356 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
359 AppLog("RevokePermissions(): dlopen() failed. [%s][%s]", pPackageId, dlerror());
363 app_revoke_permissions = reinterpret_cast <int (*)(const char*)>(dlsym(pHandle, "app_revoke_permissions"));
364 pErrorMsg = dlerror();
365 if (pErrorMsg != null)
367 AppLog("RevokePermissions(): dlsym() failed. [%s][%s]", pPackageId, pErrorMsg);
372 ret = app_revoke_permissions(pPackageId);
373 AppLog("[smack] app_revoke_permissions(%s), result = [%d]", pPackageId, ret);