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 PermissionManager.cpp
19 * @brief This is the implementation file for %PermissionManager class.
25 #include <unique_ptr.h>
27 #include <FBaseUtilStringUtil.h>
29 #include <FIoDirectory.h>
30 #include <FBase_StringConverter.h>
31 #include <FIo_FileImpl.h>
33 #include "PermissionManager.h"
34 #include "InstallerUtil.h"
35 #include "SmackManager.h"
36 #include "CompatibilityManager.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Io;
42 using namespace Tizen::App;
44 PermissionManager::PermissionManager(void)
48 PermissionManager::~PermissionManager(void)
53 PermissionManager::SetDirectory(InstallationContext* pContext)
59 SmackManager smackManager;
60 smackManager.Construct(pContext);
62 appRootPath = pContext->__rootPath;
63 PackageId packageId = pContext->__packageId;
66 // InstallerUtil::ChangeOwner(appRootPath);
67 InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
68 smackManager.AddLabelDir(packageId, appRootPath, true);
70 if (pContext->__isPreloaded == true)
72 String preloadedAppPath(PATH_USR_APPS);
73 preloadedAppPath += L"/";
74 preloadedAppPath += packageId;
76 smackManager.AddLabelDir(L"_", preloadedAppPath, true);
80 destPath = appRootPath + DIR_BIN;
81 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
82 smackManager.AddLabelDir(packageId, destPath);
85 destPath = appRootPath + DIR_INFO;
86 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
87 smackManager.AddLabelDir(packageId, destPath);
90 destPath = appRootPath + DIR_RES;
91 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
92 smackManager.AddLabelDir(packageId, destPath);
95 destPath = appRootPath + DIR_LIB;
96 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
97 smackManager.AddLabelDir(packageId, destPath);
100 destPath = appRootPath + DIR_SHARED;
101 if (File::IsFileExist(destPath) == false)
103 r = Directory::Create(destPath, false);
104 TryReturn(!IsFailed(r), INSTALLER_ERROR_INTERNAL_STATE, "Directory::Create() failed");
106 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
107 smackManager.AddLabelDir(packageId, destPath);
109 // appRoot/shared/res
110 destPath = appRootPath + DIR_SHARED_RES;
111 if (File::IsFileExist(destPath) == false)
113 String iconPath = appRootPath + DIR_ICONS;
114 InstallerUtil::CreateSymlink(iconPath, destPath);
116 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
117 smackManager.AddLabelDir(packageId, destPath);
119 // appRoot/shared/data
120 destPath = appRootPath + DIR_SHARED_DATA;
121 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
122 smackManager.AddLabelSharedDir(packageId, destPath);
124 // appRoot/shared/trusted
125 destPath = appRootPath + DIR_SHARED_TRUSTED;
126 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
127 smackManager.AddLabelSharedDir(packageId, destPath);
130 destPath = appRootPath + DIR_CONTENTS;
131 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
132 smackManager.AddLabelDir(packageId, destPath);
135 destPath = appRootPath + DIR_SETTING;
136 if (File::IsFileExist(destPath) == true)
138 String appVersion = pContext->__version;
140 String settingXmlPath;
142 //String label = packageId;
143 //label.Append("_setting");
145 srcPath = destPath + L"/setting." + appVersion + L".xml";
146 settingXmlPath = destPath + L"/setting.xml";
148 InstallerUtil::Remove(settingXmlPath);
149 InstallerUtil::CreateSymlink(srcPath, settingXmlPath);
151 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
152 smackManager.AddLabelDir(label, destPath);
156 destPath = appRootPath + DIR_DATA;
157 if (File::IsFileExist(destPath) == false)
159 r = Directory::Create(destPath, false);
160 TryReturn(!IsFailed(r), false, "Directory::Create() failed");
162 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
163 smackManager.AddLabelDir(packageId, destPath);
165 String apiVersion = pContext->__apiVersion;
167 AppLog("------------------------------------------");
168 AppLog("[Tizen::Io] # path = [%ls]", appRootPath.GetPointer());
169 AppLog("[Tizen::Io] # package = [%ls]", packageId.GetPointer());
170 AppLog("[Tizen::Io] # apiVersion = [%ls]", apiVersion.GetPointer());
172 if (pContext->__isOspCompat == true)
174 AppLog("[Tizen::Io] OSP 2.0 application");
176 if (CompatibilityManager::PrepareDataCaging(appRootPath, packageId) == false)
178 AppLog("[Tizen::Io] CompatibilityManager::PrepareDataCaging() failed.");
182 SetSymLink(pContext);
186 AppLog("[Tizen::Io] apiVersion is equal to or greater than Tizen 2.0");
188 if (CompatibilityManager::CleanDirectories(appRootPath, packageId) == false)
190 AppLog("[Tizen::Io] CompatibilityManager::CleanDirectories() failed.");
194 if (CompatibilityManager::LinkOspSharePath(appRootPath, packageId) == false)
196 AppLog("[Tizen::Io] CompatibilityManager::LinkOspSharePath() failed.");
201 if (pContext->__isVirtualRoot == true)
203 if (pContext->__pSymbolicLinkList->GetCount() > 0)
205 std::unique_ptr< IMapEnumeratorT<String*, String*> > pEnum(pContext->__pSymbolicLinkList->GetMapEnumeratorN());
206 TryReturn(pEnum, false, "pSymbolicLinkList->GetMapEnumeratorN() failed.");
207 while (pEnum->MoveNext() == E_SUCCESS)
210 String* pValue = null;
213 pEnum->GetValue(pValue);
215 AppLog("SymbolicLink - Src = [%ls], Des = [%ls]", pKey->GetPointer(), pValue->GetPointer());
219 AppLog("------------------------------------------");
225 PermissionManager::SetFile(InstallationContext* pContext)
228 String appRootPath = pContext->__rootPath;
230 IListT<AppData*>* pAppDataList = pContext->__pAppDataList;
231 TryReturn(pAppDataList, false, "pAppDataList is null");
233 for (int i = 0 ; i < pAppDataList->GetCount(); i++)
235 AppData* pAppData = null;
236 pAppDataList->GetAt(i, pAppData);
237 TryReturn(pAppData, false, "pAppData is null");
239 // set permission(755) to bin file.
240 destPath = appRootPath + DIR_BIN + L"/" + pAppData->__name + L".exe";
241 if (File::IsFileExist(destPath) == true)
243 InstallerUtil::ChangeMode(destPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
251 PermissionManager::SetSymLink(InstallationContext* pContext)
255 String appRootPath = pContext->__rootPath;
257 SmackManager smackManager;
260 oldPath = appRootPath + DIR_RES;
261 newPath = appRootPath + L"/Res";
262 InstallerUtil::CreateSymlink(oldPath, newPath);
264 newPath = appRootPath + L"/Res";
265 std::unique_ptr< char[] > pResPath(_StringConverter::CopyToCharArrayN(newPath));
266 int ret = symlink("./res", pResPath.get());
267 smackManager.AddLabelDir(pContext->__packageId, newPath);
271 oldPath = appRootPath + DIR_DATA;
272 newPath = appRootPath + L"/Home";
273 InstallerUtil::CreateSymlink(oldPath, newPath);
275 newPath = appRootPath + L"/Home";
276 std::unique_ptr< char[] > pHomePath(_StringConverter::CopyToCharArrayN(newPath));
277 ret = symlink("./data", pHomePath.get());
278 smackManager.AddLabelDir(pContext->__packageId, newPath);
281 oldPath = appRootPath + DIR_RES + L"/screen-size-normal";
282 newPath = appRootPath + L"/Res/ScreenSize-Normal";
283 InstallerUtil::CreateSymlink(oldPath, newPath);
285 oldPath = appRootPath + DIR_RES + L"/screen-density-high";
286 newPath = appRootPath + L"/Res/ScreenDensity-High";
287 InstallerUtil::CreateSymlink(oldPath, newPath);
289 oldPath = appRootPath + DIR_RES + L"/screen-density-middle";
290 newPath = appRootPath + L"/Res/ScreenDensity-Middle";
291 InstallerUtil::CreateSymlink(oldPath, newPath);
293 oldPath = appRootPath + DIR_RES + L"/screen-density-low";
294 newPath = appRootPath + L"/Res/ScreenDensity-Low";
295 InstallerUtil::CreateSymlink(oldPath, newPath);
301 PermissionManager::CopyForRds(InstallationContext* pContext, IList* pFileList, bool& isInstallRequired)
303 TryReturn(pFileList, false, "pFileList is null.");
305 for (int idx = 0; idx < pFileList->GetCount(); idx++)
307 String* pFilePath = static_cast<String *>(pFileList->GetAt(idx));
308 TryReturn(pFilePath, false, "pFilePath is null.");
310 String srcFile = DIR_APPLICATIONS_TMP;
311 srcFile += L"/" + pContext->__packageId + L"/" + *pFilePath;
312 String destFile = pContext->__rootPath + L"/" + *pFilePath;
314 AppLog("copy file from[%ls] to[%ls]", srcFile.GetPointer(), destFile.GetPointer());
317 result r = E_SUCCESS;
320 if (destFile.EndsWith(L"/") == true)
323 if (File::IsFileExist(destDir) == false)
325 Directory::Create(destDir, true);
331 r = destFile.LastIndexOf(L'/', destFile.GetLength() -1, pos);
332 if (IsFailed(r) == true)
334 AppLog("destFile is invalid[%ls]", destFile.GetPointer());
337 destFile.SubString(0, pos, destDir);
340 if (File::IsFileExist(destDir) == false)
342 Directory::Create(destDir, true);
344 InstallerUtil::Remove(destFile);
345 r = File::Copy(srcFile, destFile, true);
346 if (IsFailed(r) == true)
348 AppLog("File::Copy() failed. [%ls] -> [%ls]", srcFile.GetPointer(), destFile.GetPointer());
352 if (*pFilePath == L"info/manifest.xml")
354 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
355 isInstallRequired = true;
357 else if (pFilePath->StartsWith("setting/setting", 0) == true)
359 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
360 isInstallRequired = true;
368 PermissionManager::ApplyPermissionForRds(InstallationContext* pContext)
372 SmackManager smackManager;
373 smackManager.Construct(pContext);
375 appRootPath = pContext->__rootPath;
376 PackageId packageId = pContext->__packageId;
379 InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
380 smackManager.AddLabelDir(packageId, appRootPath, true);
383 destPath = appRootPath + DIR_BIN;
384 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
385 smackManager.AddLabelDir(packageId, destPath);
388 destPath = appRootPath + DIR_INFO;
389 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
390 smackManager.AddLabelDir(packageId, destPath);
393 destPath = appRootPath + DIR_RES;
394 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
395 smackManager.AddLabelDir(packageId, destPath);
398 destPath = appRootPath + DIR_LIB;
399 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
400 smackManager.AddLabelDir(packageId, destPath);
403 destPath = appRootPath + DIR_SHARED;
404 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
405 smackManager.AddLabelDir(packageId, destPath);
407 // appRoot/shared/res
408 destPath = appRootPath + DIR_SHARED_RES;
409 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
410 smackManager.AddLabelSharedDir(packageId, destPath);
412 // appRoot/shared/data
413 destPath = appRootPath + DIR_SHARED_DATA;
414 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
415 smackManager.AddLabelSharedDir(packageId, destPath);
417 // appRoot/shared/trusted
418 destPath = appRootPath + DIR_SHARED_TRUSTED;
419 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
420 smackManager.AddLabelSharedDir(packageId, destPath);
423 destPath = appRootPath + DIR_SETTING;
424 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
425 smackManager.AddLabelDir(packageId, destPath);
428 destPath = appRootPath + DIR_DATA;
429 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
430 smackManager.AddLabelDir(packageId, destPath);
437 PermissionManager::ApplyPermission(InstallationContext* pContext, const IList* pFileList)
439 TryReturn(pContext, false, "pContext is null");
440 TryReturn(pFileList, false, "pFileList is null");
442 // dir | type | owner | mode | smack
443 static RdsPermssions perms[] =
445 {"res", PERM_DIR_RES, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
446 {"data", PERM_DIR_DATA, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
447 {"bin", PERM_DIR_BIN, PERM_OWNER_ROOT, (PERM_BASE | PERM_EXECUTE), PERM_SMACK_0},
448 {"info", PERM_DIR_INFO, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
449 {"lib", PERM_DIR_LIB, PERM_OWNER_ROOT, (PERM_BASE | PERM_EXECUTE), PERM_SMACK_0},
450 {"setting", PERM_DIR_SETTING, PERM_OWNER_ROOT, (PERM_BASE | PERM_WRITE), PERM_SMACK_0},
451 {"shared/res", PERM_DIR_SHARED_RES, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
452 {"shared/data", PERM_DIR_SHARED_DATA, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
453 {"shared/trusted", PERM_DIR_SHARED_TRUSTED, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
454 {"shared", PERM_DIR_SHARED, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
455 {"author", PERM_DIR_ROOT, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
456 {"signature", PERM_DIR_ROOT, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
459 static int permCount = sizeof(perms) / sizeof(RdsPermssions);
460 bool isInstallRequired = false;
461 SmackManager smackManager;
462 smackManager.Construct(pContext);
464 for (int i = 0; i < pFileList->GetCount(); i++)
466 const String *pFilePath = static_cast<const String *>(pFileList->GetAt(i));
467 TryReturn(pFilePath, false, "pFilePath is null");
469 AppLog("pFilePath=(%ls)", pFilePath->GetPointer());
472 for (permIdx = 0; permIdx < permCount; permIdx++)
474 if (pFilePath->StartsWith(perms[permIdx].pDir, 0) == true)
476 if (perms[permIdx].dirType == PERM_DIR_INFO)
478 if (*pFilePath == L"info/manifest.xml")
480 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
481 isInstallRequired = true;
484 else if (perms[permIdx].dirType == PERM_DIR_SETTING)
486 if (pFilePath->StartsWith("setting/setting", 0) == true)
488 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
489 isInstallRequired = true;
493 AppLog("dirType=(%d), owner=(%d), mode=(%d), smack=(%d)",
494 perms[permIdx].dirType, perms[permIdx].ownerIndex, perms[permIdx].modeIndex, perms[permIdx].smackIndex);
496 String srcFile = DIR_APPLICATIONS_TMP;
497 srcFile += L"/" + pContext->__packageId + L"/" + *pFilePath;
498 String destFile = pContext->__rootPath + L"/" + *pFilePath;
500 AppLog("copy file: from[%ls] to[%ls]", srcFile.GetPointer(), destFile.GetPointer());
501 File::Copy(srcFile, destFile, false);
503 bool isAppOwner = false;
504 if (perms[permIdx].ownerIndex == PERM_OWNER_APP)
510 if (perms[permIdx].dirType != PERM_DIR_ROOT)
512 destPath = pContext->__rootPath + L"/" + perms[permIdx].pDir;
513 InstallerUtil::ChangeDirectoryPermission(destPath, perms[permIdx].modeIndex, isAppOwner);
517 destPath = pContext->__rootPath + L"/" + *pFilePath;
518 InstallerUtil::ChangeMode(*pFilePath, perms[permIdx].modeIndex);
520 smackManager.AddLabelSharedDir(pContext->__packageId, destPath);