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.
22 #include <unique_ptr.h>
24 #include <FAppPkgPackageInfo.h>
25 #include <FIoDirectory.h>
27 #include <FIoRegistry.h>
28 #include <FSecCryptoSha1Hash.h>
29 #include <FAppPkg_PackageManagerImpl.h>
30 #include <FBase_StringConverter.h>
32 #include "SmackManager.h"
33 #include "InstallerDefs.h"
34 #include "InstallerUtil.h"
36 using namespace Tizen::App;
37 using namespace Tizen::App::Package;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Security::Cert;
42 using namespace Tizen::Security::Crypto;
43 using namespace Tizen::Io;
45 SmackManager::SmackManager(void)
50 SmackManager::~SmackManager(void)
55 SmackManager::Construct(InstallationContext* pContext)
57 __pContext = pContext;
63 SmackManager::Install(const PackageId& packageId)
65 TryReturn(__pContext, false, "__pContext is null");
69 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
70 TryReturn(pPackageId, false, "pPackageId is null.");
72 res = Install(pPackageId.get());
73 TryReturn(res == 0, false, "Install() failed.");
79 SmackManager::Uninstall(const PackageId& packageId)
83 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
84 TryReturn(pPackageId, false, "pPackageId is null.");
86 res = Uninstall(pPackageId.get());
92 SmackManager::AddLabelDir(const String& label, const String& dirPath, bool rootDirectory)
97 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
98 TryReturn(pPath, false, "pPath is null.");
100 if (InstallerUtil::IsSymlink(dirPath) == true)
102 res = AddLabelDir("_", pPath.get());
103 InstallerUtil::GetRealPath(dirPath, realPath);
110 std::unique_ptr<char[]> pRealPath(_StringConverter::CopyToCharArrayN(realPath));
111 TryReturn(pRealPath, false, "pRealPath is null");
113 if ((rootDirectory == true) || (realPath.Contains(L"/shared/res") == true))
115 res = AddLabelDir("_", pRealPath.get());
119 std::unique_ptr<char[]> pLabel(_StringConverter::CopyToCharArrayN(label));
120 TryReturn(pLabel, false, "pLabel is null.");
122 res = AddLabelDir(pLabel.get(), pRealPath.get());
129 SmackManager::AddLabelSymlink(const String& symlink)
133 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(symlink));
134 TryReturn(pPath, false, "pPath is null.");
136 if (InstallerUtil::IsSymlink(symlink) == true)
138 res = AddLabelDir("_", pPath.get());
142 AppLog("[%ls] is not symlink.", pPath.get());
150 SmackManager::EnablePermissions(const PackageId& packageId)
152 TryReturn(__pContext, false, "__pContext is null");
156 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
157 TryReturn(pPackageId, false, "pPackageId is null.");
159 if (__pContext->__pPrivilegeList)
161 int count = __pContext->__pPrivilegeList->GetCount();
163 const char** pList = new (std::nothrow) const char*[count+1];
164 TryReturn(pList, false, "pList is null.");
166 for (int i = 0; i < count; i++)
168 String* pPrivilege = dynamic_cast < String* >(__pContext->__pPrivilegeList->GetAt(i));
171 char* pPrivilegeString = _StringConverter::CopyToCharArrayN(*pPrivilege);
172 if (pPrivilegeString)
174 pList[i] = pPrivilegeString;
175 AppLog("Privilege - [%s]", pList[i]);
182 res = EnablePermissions(pPackageId.get(), APP_TYPE_OSP, pList, true);
184 for (int i = 0; pList[i] != null; i++)
192 const char* pNullList[1] = {null};
193 res = EnablePermissions(pPackageId.get(), APP_TYPE_OSP, pNullList, true);
200 SmackManager::RevokePermissions(const PackageId& packageId)
204 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
205 TryReturn(pPackageId, false, "pPackageId is null.");
207 res = RevokePermissions(pPackageId.get());
213 SmackManager::SetupPath(const PackageId& packageId, const String& dirPath, app_path_type_t type, const String& label)
218 if (File::IsFileExist(dirPath) == false)
220 AppLog("dirPath [%ls] is not existed. skip!", dirPath.GetPointer());
224 std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
225 TryReturn(pPackageId, false, "pPackageId is null.");
227 std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
228 TryReturn(pPath, false, "pPath is null.");
230 if (InstallerUtil::IsSymlink(dirPath) == true)
232 res = SetupPath(pPackageId.get(), pPath.get(), APP_PATH_ANY_LABEL, "_");
233 InstallerUtil::GetRealPath(dirPath, realPath);
240 std::unique_ptr<char[]> pRealPath(_StringConverter::CopyToCharArrayN(realPath));
241 TryReturn(pRealPath, false, "pRealPath is null.");
243 if (type == APP_PATH_GROUP_RW)
245 TryReturn(__pContext, false, "__pContext is null.");
249 if (__pContext->__isVerificationMode == true)
253 result r = E_SUCCESS;
255 IListT<String *>* pAuthorCertList = __pContext->__pAuthorCertList;
256 TryReturn(pAuthorCertList, false, "pAuthorCertList is null.");
258 String* pEntity = null;
259 r = pAuthorCertList->GetAt(0, pEntity);
260 TryReturn(!IsFailed(r), false, "pAuthorCertList->GetAt() is failed.");
261 TryReturn(pEntity, false, "pEntity is null.");
263 std::unique_ptr<ByteBuffer> pEncodedData(StringUtil::DecodeBase64StringN(*pEntity));
264 TryReturn(pEncodedData, false, "pEncodedData is null.");
266 std::unique_ptr<ByteBuffer> pHashValue(hash.GetHashN(*pEncodedData.get()));
267 TryReturn(pHashValue, false, "pHashValue is null.");
269 r = StringUtil::EncodeToBase64String(*pHashValue, base64Value);
270 TryReturn(!IsFailed(r), false, "EncodeToBase64String() is failed.");
272 // in smack, '/' is not allowed for label.
273 r = base64Value.Replace(L"/", L"#");
274 TryReturn(!IsFailed(r), false, "base64Value.Replace() is failed.");
276 std::unique_ptr<char[]> pHashEncodedValue(_StringConverter::CopyToCharArrayN(base64Value));
277 TryReturn(pHashEncodedValue, false, "pHashEncodedValue is null.");
279 groupId = pHashEncodedValue.get();
280 AppLog("pHashEncodedValue = [%s]", pHashEncodedValue.get());
284 groupId = __pContext->__packageId;
287 std::unique_ptr<char[]> pGroupId(_StringConverter::CopyToCharArrayN(groupId));
288 TryReturn(pGroupId, false, "pGroupId is null.");
290 res = SetupPath(pPackageId.get(), pRealPath.get(), type, pGroupId.get());
292 else if (type == APP_PATH_ANY_LABEL)
294 TryReturn(label.IsEmpty() == false, false, "label is empty.");
296 std::unique_ptr<char[]> pAnyLabel(_StringConverter::CopyToCharArrayN(label));
297 TryReturn(pAnyLabel, false, "pAnyLabel is null.");
299 res = SetupPath(pPackageId.get(), pRealPath.get(), type, pAnyLabel.get());
303 res = SetupPath(pPackageId.get(), pRealPath.get(), type);
310 SmackManager::Begin(void)
312 AppLog("[smack] perm_begin()");
313 int ret = perm_begin();
314 AppLog("[smack] perm_begin(), result = [%d]", ret);
320 SmackManager::End(void)
322 AppLog("[smack] perm_end()");
323 int ret = perm_end();
324 AppLog("[smack] perm_end(), result = [%d]", ret);
330 SmackManager::Install(const char* pPackageId)
332 AppLog("[smack] perm_app_install(%s)", pPackageId);
333 int ret = perm_app_install(pPackageId);
334 AppLog("[smack] perm_app_install(%s), result = [%d]", pPackageId, ret);
340 SmackManager::Uninstall(const char* pPackageId)
342 AppLog("[smack] perm_app_uninstall(%s)", pPackageId);
343 int ret = perm_app_uninstall(pPackageId);
344 AppLog("[smack] perm_app_uninstall(%s), result = [%d]", pPackageId, ret);
350 SmackManager::AddLabelDir(const char* pLabel, const char* pDirPath)
352 AppLog("[smack] app_label_dir(%s, %s)", pLabel, pDirPath);
353 int ret = app_label_dir(pLabel, pDirPath);
354 AppLog("[smack] app_label_dir(%s, %s), result = [%d]", pLabel, pDirPath, ret);
360 SmackManager::EnablePermissions(const char* pPackageId, app_type_t appType, const char** ppPermissions, bool persistent)
362 AppLog("[smack] perm_app_enable_permissions(%s, %d)", pPackageId, appType);
363 int ret = perm_app_enable_permissions(pPackageId, appType, ppPermissions, persistent);
364 AppLog("[smack] perm_app_enable_permissions(%s, %d), result = [%d]", pPackageId, appType, ret);
370 SmackManager::RevokePermissions(const char* pPackageId)
372 AppLog("[smack] perm_app_revoke_permissions(%s)", pPackageId);
373 int ret = perm_app_revoke_permissions(pPackageId);
374 AppLog("[smack] perm_app_revoke_permissions(%s), result = [%d]", pPackageId, ret);
380 SmackManager::SetupPath(const char* pPackageId, const char* pDirPath, app_path_type_t appPathType, const char* pGroupId)
384 if (pGroupId == null)
386 AppLog("[smack] perm_app_setup_path(%s, %s, %d)", pPackageId, pDirPath, appPathType);
387 ret = perm_app_setup_path(pPackageId, pDirPath, appPathType);
388 AppLog("[smack] perm_app_setup_path(), result = [%d]", ret);
392 AppLog("[smack] perm_app_setup_path(%s, %s, %d, %s)", pPackageId, pDirPath, appPathType, pGroupId);
393 ret = perm_app_setup_path(pPackageId, pDirPath, appPathType, pGroupId);
394 AppLog("[smack] perm_app_setup_path(), result = [%d]", ret);