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)
60 SmackManager smackManager;
61 smackManager.Construct(pContext);
63 appRootPath = pContext->__rootPath;
64 PackageId packageId = pContext->__packageId;
67 // InstallerUtil::ChangeOwner(appRootPath);
68 InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
69 smackManager.AddLabelDir(packageId, appRootPath, true);
71 if (pContext->__isPreloaded == true)
73 String preloadedAppPath(PATH_USR_APPS);
74 preloadedAppPath += L"/";
75 preloadedAppPath += packageId;
77 smackManager.AddLabelDir(L"_", preloadedAppPath, true);
81 destPath = appRootPath + DIR_BIN;
82 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
83 smackManager.AddLabelDir(packageId, destPath);
86 destPath = appRootPath + DIR_INFO;
87 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
88 smackManager.AddLabelDir(packageId, destPath);
91 destPath = appRootPath + DIR_RES;
92 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
93 smackManager.AddLabelDir(packageId, destPath);
96 destPath = appRootPath + DIR_LIB;
97 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
98 smackManager.AddLabelDir(packageId, destPath);
101 destPath = appRootPath + DIR_SHARED;
102 if (File::IsFileExist(destPath) == false)
104 r = Directory::Create(destPath, false);
105 TryReturn(!IsFailed(r), INSTALLER_ERROR_INTERNAL_STATE, "Directory::Create() failed");
107 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
108 smackManager.AddLabelDir(packageId, destPath);
110 // appRoot/shared/res
111 destPath = appRootPath + DIR_SHARED_RES;
112 if (File::IsFileExist(destPath) == false)
114 String iconPath = appRootPath + DIR_ICONS;
115 InstallerUtil::CreateSymlink(iconPath, destPath);
117 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
118 smackManager.AddLabelDir(packageId, destPath);
120 // appRoot/shared/data
121 destPath = appRootPath + DIR_SHARED_DATA;
122 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
123 smackManager.AddLabelSharedDir(packageId, destPath);
125 // appRoot/shared/trusted
126 destPath = appRootPath + DIR_SHARED_TRUSTED;
127 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
128 smackManager.AddLabelSharedDir(packageId, destPath);
131 destPath = appRootPath + DIR_CONTENTS;
132 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
133 smackManager.AddLabelDir(packageId, destPath);
136 destPath = appRootPath + DIR_SETTING;
137 if (File::IsFileExist(destPath) == true)
139 String appVersion = pContext->__version;
141 String settingXmlPath;
143 //String label = packageId;
144 //label.Append("_setting");
146 srcPath = destPath + L"/setting." + appVersion + L".xml";
147 settingXmlPath = destPath + L"/setting.xml";
149 InstallerUtil::Remove(settingXmlPath);
150 InstallerUtil::CreateSymlink(srcPath, settingXmlPath);
152 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
153 smackManager.AddLabelDir(label, destPath);
157 destPath = appRootPath + DIR_DATA;
158 if (File::IsFileExist(destPath) == false)
160 r = Directory::Create(destPath, false);
161 TryReturn(!IsFailed(r), false, "Directory::Create() failed");
163 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
164 smackManager.AddLabelDir(packageId, destPath);
166 String apiVersion = pContext->__apiVersion;
168 AppLog("------------------------------------------");
169 AppLog("[Tizen::Io] # path = [%ls]", appRootPath.GetPointer());
170 AppLog("[Tizen::Io] # package = [%ls]", packageId.GetPointer());
171 AppLog("[Tizen::Io] # apiVersion = [%ls]", apiVersion.GetPointer());
173 if (pContext->__isOspCompat == true)
175 AppLog("[Tizen::Io] OSP 2.0 application");
177 res = CompatibilityManager::PrepareDataCaging(appRootPath, packageId);
178 TryReturn(res == true, false, "[Tizen::Io] CompatibilityManager::PrepareDataCaging() failed.");
180 SetSymLink(pContext);
184 AppLog("[Tizen::Io] apiVersion is equal to or greater than Tizen 2.0");
186 res = CompatibilityManager::CleanDirectories(appRootPath, packageId);
187 TryReturn(res == true, false, "[Tizen::Io] CompatibilityManager::CleanDirectories() failed.");
189 res = CompatibilityManager::LinkOspSharePath(appRootPath, packageId);
190 TryReturn(res == true, false, "[Tizen::Io] CompatibilityManager::LinkOspSharePath() failed.");
193 if (pContext->__isVirtualRoot == true)
195 res = CompatibilityManager::PrepareVirtualRoot(appRootPath, packageId);
196 TryReturn(res == true, false, "[Tizen::Io] CompatibilityManager::PrepareVirtualRoot() failed.");
198 AppLog("------------------------------------------");
204 PermissionManager::SetFile(InstallationContext* pContext)
207 String appRootPath = pContext->__rootPath;
209 IListT<AppData*>* pAppDataList = pContext->__pAppDataList;
210 TryReturn(pAppDataList, false, "pAppDataList is null");
212 for (int i = 0 ; i < pAppDataList->GetCount(); i++)
214 AppData* pAppData = null;
215 pAppDataList->GetAt(i, pAppData);
216 TryReturn(pAppData, false, "pAppData is null");
218 // set permission(755) to bin file.
219 destPath = appRootPath + DIR_BIN + L"/" + pAppData->__name + L".exe";
220 if (File::IsFileExist(destPath) == true)
222 InstallerUtil::ChangeMode(destPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
230 PermissionManager::SetSymLink(InstallationContext* pContext)
234 String appRootPath = pContext->__rootPath;
236 SmackManager smackManager;
239 oldPath = appRootPath + DIR_RES;
240 newPath = appRootPath + L"/Res";
241 InstallerUtil::CreateSymlink(oldPath, newPath);
243 newPath = appRootPath + L"/Res";
244 std::unique_ptr< char[] > pResPath(_StringConverter::CopyToCharArrayN(newPath));
245 int ret = symlink("./res", pResPath.get());
246 smackManager.AddLabelDir(pContext->__packageId, newPath);
250 oldPath = appRootPath + DIR_DATA;
251 newPath = appRootPath + L"/Home";
252 InstallerUtil::CreateSymlink(oldPath, newPath);
254 newPath = appRootPath + L"/Home";
255 std::unique_ptr< char[] > pHomePath(_StringConverter::CopyToCharArrayN(newPath));
256 ret = symlink("./data", pHomePath.get());
257 smackManager.AddLabelDir(pContext->__packageId, newPath);
260 oldPath = appRootPath + DIR_RES + L"/screen-size-normal";
261 newPath = appRootPath + L"/Res/ScreenSize-Normal";
262 InstallerUtil::CreateSymlink(oldPath, newPath);
264 oldPath = appRootPath + DIR_RES + L"/screen-density-high";
265 newPath = appRootPath + L"/Res/ScreenDensity-High";
266 InstallerUtil::CreateSymlink(oldPath, newPath);
268 oldPath = appRootPath + DIR_RES + L"/screen-density-middle";
269 newPath = appRootPath + L"/Res/ScreenDensity-Middle";
270 InstallerUtil::CreateSymlink(oldPath, newPath);
272 oldPath = appRootPath + DIR_RES + L"/screen-density-low";
273 newPath = appRootPath + L"/Res/ScreenDensity-Low";
274 InstallerUtil::CreateSymlink(oldPath, newPath);
280 PermissionManager::CopyForRds(InstallationContext* pContext, IList* pFileList, bool& isInstallRequired)
282 TryReturn(pFileList, false, "pFileList is null.");
284 for (int idx = 0; idx < pFileList->GetCount(); idx++)
286 String* pFilePath = static_cast<String *>(pFileList->GetAt(idx));
287 TryReturn(pFilePath, false, "pFilePath is null.");
289 String srcFile = DIR_APPLICATIONS_TMP;
290 srcFile += L"/" + pContext->__packageId + L"/" + *pFilePath;
291 String destFile = pContext->__rootPath + L"/" + *pFilePath;
293 AppLog("copy file from[%ls] to[%ls]", srcFile.GetPointer(), destFile.GetPointer());
296 result r = E_SUCCESS;
299 if (destFile.EndsWith(L"/") == true)
302 if (File::IsFileExist(destDir) == false)
304 Directory::Create(destDir, true);
310 r = destFile.LastIndexOf(L'/', destFile.GetLength() -1, pos);
311 if (IsFailed(r) == true)
313 AppLog("destFile is invalid[%ls]", destFile.GetPointer());
316 destFile.SubString(0, pos, destDir);
319 if (File::IsFileExist(destDir) == false)
321 Directory::Create(destDir, true);
323 InstallerUtil::Remove(destFile);
324 r = File::Copy(srcFile, destFile, true);
325 if (IsFailed(r) == true)
327 AppLog("File::Copy() failed. [%ls] -> [%ls]", srcFile.GetPointer(), destFile.GetPointer());
331 if (*pFilePath == L"info/manifest.xml")
333 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
334 isInstallRequired = true;
336 else if (pFilePath->StartsWith("setting/setting", 0) == true)
338 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
339 isInstallRequired = true;
347 PermissionManager::ApplyPermissionForRds(InstallationContext* pContext)
351 SmackManager smackManager;
352 smackManager.Construct(pContext);
354 appRootPath = pContext->__rootPath;
355 PackageId packageId = pContext->__packageId;
358 InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
359 smackManager.AddLabelDir(packageId, appRootPath, true);
362 destPath = appRootPath + DIR_BIN;
363 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
364 smackManager.AddLabelDir(packageId, destPath);
367 destPath = appRootPath + DIR_INFO;
368 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
369 smackManager.AddLabelDir(packageId, destPath);
372 destPath = appRootPath + DIR_RES;
373 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
374 smackManager.AddLabelDir(packageId, destPath);
377 destPath = appRootPath + DIR_LIB;
378 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
379 smackManager.AddLabelDir(packageId, destPath);
382 destPath = appRootPath + DIR_SHARED;
383 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
384 smackManager.AddLabelDir(packageId, destPath);
386 // appRoot/shared/res
387 destPath = appRootPath + DIR_SHARED_RES;
388 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
389 smackManager.AddLabelSharedDir(packageId, destPath);
391 // appRoot/shared/data
392 destPath = appRootPath + DIR_SHARED_DATA;
393 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
394 smackManager.AddLabelSharedDir(packageId, destPath);
396 // appRoot/shared/trusted
397 destPath = appRootPath + DIR_SHARED_TRUSTED;
398 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
399 smackManager.AddLabelSharedDir(packageId, destPath);
402 destPath = appRootPath + DIR_SETTING;
403 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
404 smackManager.AddLabelDir(packageId, destPath);
407 destPath = appRootPath + DIR_DATA;
408 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
409 smackManager.AddLabelDir(packageId, destPath);
416 PermissionManager::ApplyPermission(InstallationContext* pContext, const IList* pFileList)
418 TryReturn(pContext, false, "pContext is null");
419 TryReturn(pFileList, false, "pFileList is null");
421 // dir | type | owner | mode | smack
422 static RdsPermssions perms[] =
424 {"res", PERM_DIR_RES, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
425 {"data", PERM_DIR_DATA, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
426 {"bin", PERM_DIR_BIN, PERM_OWNER_ROOT, (PERM_BASE | PERM_EXECUTE), PERM_SMACK_0},
427 {"info", PERM_DIR_INFO, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
428 {"lib", PERM_DIR_LIB, PERM_OWNER_ROOT, (PERM_BASE | PERM_EXECUTE), PERM_SMACK_0},
429 {"setting", PERM_DIR_SETTING, PERM_OWNER_ROOT, (PERM_BASE | PERM_WRITE), PERM_SMACK_0},
430 {"shared/res", PERM_DIR_SHARED_RES, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
431 {"shared/data", PERM_DIR_SHARED_DATA, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
432 {"shared/trusted", PERM_DIR_SHARED_TRUSTED, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
433 {"shared", PERM_DIR_SHARED, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
434 {"author", PERM_DIR_ROOT, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
435 {"signature", PERM_DIR_ROOT, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
438 static int permCount = sizeof(perms) / sizeof(RdsPermssions);
439 bool isInstallRequired = false;
440 SmackManager smackManager;
441 smackManager.Construct(pContext);
443 for (int i = 0; i < pFileList->GetCount(); i++)
445 const String *pFilePath = static_cast<const String *>(pFileList->GetAt(i));
446 TryReturn(pFilePath, false, "pFilePath is null");
448 AppLog("pFilePath=(%ls)", pFilePath->GetPointer());
451 for (permIdx = 0; permIdx < permCount; permIdx++)
453 if (pFilePath->StartsWith(perms[permIdx].pDir, 0) == true)
455 if (perms[permIdx].dirType == PERM_DIR_INFO)
457 if (*pFilePath == L"info/manifest.xml")
459 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
460 isInstallRequired = true;
463 else if (perms[permIdx].dirType == PERM_DIR_SETTING)
465 if (pFilePath->StartsWith("setting/setting", 0) == true)
467 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
468 isInstallRequired = true;
472 AppLog("dirType=(%d), owner=(%d), mode=(%d), smack=(%d)",
473 perms[permIdx].dirType, perms[permIdx].ownerIndex, perms[permIdx].modeIndex, perms[permIdx].smackIndex);
475 String srcFile = DIR_APPLICATIONS_TMP;
476 srcFile += L"/" + pContext->__packageId + L"/" + *pFilePath;
477 String destFile = pContext->__rootPath + L"/" + *pFilePath;
479 AppLog("copy file: from[%ls] to[%ls]", srcFile.GetPointer(), destFile.GetPointer());
480 File::Copy(srcFile, destFile, false);
482 bool isAppOwner = false;
483 if (perms[permIdx].ownerIndex == PERM_OWNER_APP)
489 if (perms[permIdx].dirType != PERM_DIR_ROOT)
491 destPath = pContext->__rootPath + L"/" + perms[permIdx].pDir;
492 InstallerUtil::ChangeDirectoryPermission(destPath, perms[permIdx].modeIndex, isAppOwner);
496 destPath = pContext->__rootPath + L"/" + *pFilePath;
497 InstallerUtil::ChangeMode(*pFilePath, perms[permIdx].modeIndex);
499 smackManager.AddLabelSharedDir(pContext->__packageId, destPath);