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)
43 ,__isSmackEnable(false)
45 if (IsSmackEnable() == true)
47 __isSmackEnable = true;
51 SmackManager::~SmackManager(void)
56 SmackManager::Construct(InstallationContext* pContext)
58 __pContext = pContext;
64 SmackManager::AddLabelDir(const String& label, const String& dirPath, bool rootDirectory)
66 if (__isSmackEnable == false)
74 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
75 TryReturn(pPath, false, "pPath is null.");
77 if (InstallerUtil::IsSymlink(dirPath) == true)
79 res = AddLabelDir("_", pPath.get());
80 InstallerUtil::GetRealPath(dirPath, realPath);
87 std::unique_ptr<char[]> pRealPath(_StringConverter::CopyToCharArrayN(realPath));
88 TryReturn(pRealPath, false, "pRealPath is null");
90 if (rootDirectory == true)
92 res = AddLabelDir("_", pRealPath.get());
94 else if (dirPath.Contains(L"shared") == true)
96 res = AddLabelDir("*", pRealPath.get());
100 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(label));
101 TryReturn(pPackageId, false, "pPackageId is null");
103 res = AddLabelDir(pPackageId.get(), pRealPath.get());
110 SmackManager::AddLabelSharedDir(const PackageId& packageId, const String& dirPath)
112 if (__isSmackEnable == false)
117 TryReturn(__pContext, false, "__pContext is null");
119 if (__pContext->__isVerificationMode == false)
121 AppLog("VerificationMode is off.");
126 String label = packageId;
128 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
129 TryReturn(pPackageId, false, "pPackageId is null.");
131 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
132 TryReturn(pPath, false, "pPath is null.");
134 if (dirPath.Contains(L"shared/data") == true)
137 //label.Append("_shareddata");
139 else if (dirPath.Contains(L"shared/res") == true)
142 //label.Append("_sharedres");
144 else if (dirPath.Contains(L"shared/trusted") == true)
149 X509CertificatePath* pAuthorCertPath = __pContext->GetAuthorCertPath();
150 TryReturn(pAuthorCertPath, false, "pAuthorCertPath is null.");
152 std::unique_ptr<ICertificate> pEntity(pAuthorCertPath->GetCertificateN(0));
153 TryReturn(pEntity, false, "pEntity is null.");
155 std::unique_ptr<ByteBuffer> pEncodedData(pEntity->GetEncodedDataN());
156 TryReturn(pEncodedData, false, "pEncodedData is null.");
158 std::unique_ptr<ByteBuffer> pHashValue(hash.GetHashN(*pEncodedData.get()));
159 TryReturn(pHashValue, false, "pHashValue is null.");
161 StringUtil::EncodeToBase64String(*pHashValue, base64Value);
163 std::unique_ptr<char[]> pHashEncodedValue(_StringConverter::CopyToCharArrayN(base64Value));
164 TryReturn(pHashEncodedValue, false, "pHashEncodedValue is null.");
166 label = pHashEncodedValue.get();
167 AppLog("pHashEncodedValue = [%s]", pHashEncodedValue.get());
171 AppLog("Invalid Directory = [%ls]", dirPath.GetPointer());
175 std::unique_ptr<char[]> pLabel(_StringConverter::CopyToCharArrayN(label));
176 TryReturn(pLabel, false, "pLabel is null.");
178 res = AddLabelSharedDir(pPackageId.get(), pLabel.get(), pPath.get());
184 SmackManager::AddPermissions(const PackageId& packageId)
186 if (__isSmackEnable == false)
191 TryReturn(__pContext, false, "__pContext is null");
194 String script("/usr/bin/smackload-app.sh");
195 bool exist = File::IsFileExist(script);
198 script.Append(packageId);
200 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
201 TryReturn(pPackageId, false, "pPackageId is null.");
203 std::unique_ptr<char[]> pScript(_StringConverter::CopyToCharArrayN(script));
204 TryReturn(pScript, false, "pScript is null.");
206 int count = __pContext->__pPrivilegeList->GetCount();
208 const char** pList = new (std::nothrow) const char*[count+1];
209 TryReturn(pList, false, "pList is null.");
211 for (int i = 0; i < count; i++)
213 String* pPrivilege = dynamic_cast < String* >(__pContext->__pPrivilegeList->GetAt(i));
216 char* pPrivilegeString = _StringConverter::CopyToCharArrayN(*pPrivilege);
217 TryReturn(pPrivilegeString, false, "pPrivilegeString is null.");
219 pList[i] = pPrivilegeString;
225 res = AddPermissions(pPackageId.get(), pList);
229 res = system(pScript.get());
230 AppLog("[smack] system(%s), result = [%d]", pScript.get(), res);
234 AppLog("[%ls] not found", script.GetPointer());
237 for (int i = 0; pList[i] != null; i++)
239 AppLog("delete Privilege - [%s]", pList[i]);
248 SmackManager::RevokePermissions(const PackageId& packageId)
250 if (__isSmackEnable == false)
257 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
258 TryReturn(pPackageId, false, "pPackageId is null.");
260 res = RevokePermissions(pPackageId.get());
266 SmackManager::IsSmackEnable()
270 String section(L"feature");
271 String entry(L"smack");
274 r = reg.Construct(CONFIG_PATH, "r");
275 TryReturn(!IsFailed(r), false, "CONFIG file is not found.");
277 r = reg.GetValue(section, entry, value);
278 TryReturn(!IsFailed(r), false, "GetValue is failed. entry = [%ls]", entry.GetPointer());
280 AppLog("[%ls is %ls.]", entry.GetPointer(), value.GetPointer());
291 SmackManager::AddLabelDir(const char* pLabel, const char* pDirPath)
294 void* pHandle = null;
295 char* pErrorMsg = null;
296 int (*app_label_dir)(const char*, const char*) = null;
298 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
301 AppLog("AddLabelDir(): dlopen() failed. [%s]", dlerror());
305 app_label_dir = reinterpret_cast <int (*)(const char*, const char*)>(dlsym(pHandle, "app_label_dir"));
306 pErrorMsg = dlerror();
307 if (pErrorMsg != null)
309 AppLog("AddLabelDir(): dlsym() failed. [%s]", pErrorMsg);
314 ret = app_label_dir(pLabel, pDirPath);
315 AppLog("[smack] app_label_dir(%s, %s), result = [%d]", pLabel, pDirPath, ret);
323 SmackManager::AddLabelSharedDir(const char* pLabel, const char* pSharedLabel, const char* pDirPath)
326 void* pHandle = null;
327 char* pErrorMsg = null;
328 int (*app_label_shared_dir)(const char*, const char*, const char*) = null;
330 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
333 AppLog("AddLabelSharedDir(): dlopen() failed. [%s]", dlerror());
337 app_label_shared_dir = reinterpret_cast <int (*)(const char*, const char*, const char*)>(dlsym(pHandle, "app_label_shared_dir"));
338 pErrorMsg = dlerror();
339 if (pErrorMsg != null)
341 AppLog("AddLabelSharedDir(): dlsym() failed. [%s]", pErrorMsg);
346 ret = app_label_shared_dir(pLabel, pSharedLabel, pDirPath);
347 AppLog("[smack] app_label_shared_dir(%s, %s, %s), result = [%d]", pLabel, pSharedLabel, pDirPath, ret);
355 SmackManager::AddPermissions(const char* pPackageId, const char** ppPermissions)
358 void* pHandle = null;
359 char* pErrorMsg = null;
360 int (*app_add_permissions)(const char*, const char**) = null;
362 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
365 AppLog("AddPermissions(): dlopen() failed. [%s][%s]", pPackageId, dlerror());
369 app_add_permissions = reinterpret_cast <int (*)(const char*, const char**)>(dlsym(pHandle, "app_add_permissions"));
370 pErrorMsg = dlerror();
371 if (pErrorMsg != null)
373 AppLog("AddPermissions(): dlsym() failed. [%s][%s]", pPackageId, pErrorMsg);
378 for (int i = 0; ppPermissions[i] != null; i++)
380 AppLog("Privilege - [%s]", ppPermissions[i]);
383 ret = app_add_permissions(pPackageId, ppPermissions);
384 AppLog("[smack] app_add_permissions(%s), result = [%d]", pPackageId, ret);
392 SmackManager::RevokePermissions(const char* pPackageId)
395 void* pHandle = null;
396 char* pErrorMsg = null;
397 int (*app_revoke_permissions)(const char*) = null;
399 pHandle = dlopen("libprivilege-control.so.0", RTLD_LAZY | RTLD_GLOBAL);
402 AppLog("RevokePermissions(): dlopen() failed. [%s][%s]", pPackageId, dlerror());
406 app_revoke_permissions = reinterpret_cast <int (*)(const char*)>(dlsym(pHandle, "app_revoke_permissions"));
407 pErrorMsg = dlerror();
408 if (pErrorMsg != null)
410 AppLog("RevokePermissions(): dlsym() failed. [%s][%s]", pPackageId, pErrorMsg);
415 ret = app_revoke_permissions(pPackageId);
416 AppLog("[smack] app_revoke_permissions(%s), result = [%d]", pPackageId, ret);