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"
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Io;
41 using namespace Tizen::App;
43 PermissionManager::PermissionManager(void)
47 PermissionManager::~PermissionManager(void)
52 PermissionManager::SetDirectory(InstallationContext* pContext)
58 SmackManager smackManager;
59 smackManager.Construct(pContext);
61 appRootPath = pContext->__rootPath;
62 PackageId packageId = pContext->__packageId;
65 // InstallerUtil::ChangeOwner(appRootPath);
66 InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
67 smackManager.AddLabelDir(packageId, appRootPath, true);
69 if (pContext->__isPreloaded == true)
71 String preloadedAppPath(PATH_USR_APPS);
72 preloadedAppPath += L"/";
73 preloadedAppPath += packageId;
75 smackManager.AddLabelDir(L"_", preloadedAppPath, true);
79 destPath = appRootPath + DIR_BIN;
80 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
81 smackManager.AddLabelDir(packageId, destPath);
84 destPath = appRootPath + DIR_INFO;
85 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
86 smackManager.AddLabelDir(packageId, destPath);
89 destPath = appRootPath + DIR_RES;
90 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
91 smackManager.AddLabelDir(packageId, destPath);
94 destPath = appRootPath + DIR_LIB;
95 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
96 smackManager.AddLabelDir(packageId, destPath);
99 destPath = appRootPath + DIR_SHARED;
100 if (File::IsFileExist(destPath) == false)
102 r = Directory::Create(destPath, false);
103 TryReturn(!IsFailed(r), INSTALLER_ERROR_INTERNAL_STATE, "Directory::Create() failed");
105 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
106 smackManager.AddLabelDir(packageId, destPath);
108 // appRoot/shared/res
109 destPath = appRootPath + DIR_SHARED_RES;
110 if (File::IsFileExist(destPath) == false)
112 String iconPath = appRootPath + DIR_ICONS;
113 InstallerUtil::CreateSymlink(iconPath, destPath);
115 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
116 smackManager.AddLabelDir(packageId, destPath);
118 // appRoot/shared/data
119 destPath = appRootPath + DIR_SHARED_DATA;
120 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
121 smackManager.AddLabelSharedDir(packageId, destPath);
123 // appRoot/shared/trusted
124 destPath = appRootPath + DIR_SHARED_TRUSTED;
125 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
126 smackManager.AddLabelSharedDir(packageId, destPath);
129 destPath = appRootPath + DIR_CONTENTS;
130 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
131 smackManager.AddLabelDir(packageId, destPath);
134 destPath = appRootPath + DIR_SETTING;
135 if (File::IsFileExist(destPath) == true)
137 String appVersion = pContext->__version;
139 String settingXmlPath;
141 //String label = packageId;
142 //label.Append("_setting");
144 srcPath = destPath + L"/setting." + appVersion + L".xml";
145 settingXmlPath = destPath + L"/setting.xml";
147 InstallerUtil::Remove(settingXmlPath);
148 InstallerUtil::CreateSymlink(srcPath, settingXmlPath);
150 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
151 smackManager.AddLabelDir(label, destPath);
155 destPath = appRootPath + DIR_DATA;
156 if (File::IsFileExist(destPath) == false)
158 r = Directory::Create(destPath, false);
159 TryReturn(!IsFailed(r), false, "Directory::Create() failed");
161 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
162 smackManager.AddLabelDir(packageId, destPath);
164 String apiVersion = pContext->__apiVersion;
166 AppLog("------------------------------------------");
167 AppLog("[Tizen::Io] # path = [%ls]", appRootPath.GetPointer());
168 AppLog("[Tizen::Io] # package = [%ls]", packageId.GetPointer());
169 AppLog("[Tizen::Io] # apiVersion = [%ls]", apiVersion.GetPointer());
171 if (pContext->__isOspCompat == true)
173 AppLog("[Tizen::Io] OSP 2.0 application");
175 if (_FileImpl::PrepareDataCaging(appRootPath, packageId) == false)
177 AppLog("[Tizen::Io] _FileImpl::PrepareDataCaging() failed");
181 SetSymLink(pContext);
185 AppLog("[Tizen::Io] apiVersion is equal to or greater than Tizen 2.0");
187 if (_FileImpl::CreateOspApplicationDirectories(appRootPath, packageId) == false)
189 AppLog("[Tizen::Io] _FileImpl::CreateOspApplicationDirectories() failed");
194 if (pContext->__isVirtualRoot == true)
196 if (pContext->__pSymbolicLinkList->GetCount() > 0)
198 std::unique_ptr< IMapEnumeratorT<String*, String*> > pEnum(pContext->__pSymbolicLinkList->GetMapEnumeratorN());
199 TryReturn(pEnum, false, "pSymbolicLinkList->GetMapEnumeratorN() failed.");
200 while (pEnum->MoveNext() == E_SUCCESS)
203 String* pValue = null;
206 pEnum->GetValue(pValue);
208 AppLog("SymbolicLink - Src = [%ls], Des = [%ls]", pKey->GetPointer(), pValue->GetPointer());
212 AppLog("------------------------------------------");
218 PermissionManager::SetFile(InstallationContext* pContext)
221 String appRootPath = pContext->__rootPath;
223 IListT<AppData*>* pAppDataList = pContext->__pAppDataList;
224 TryReturn(pAppDataList, false, "pAppDataList is null");
226 for (int i = 0 ; i < pAppDataList->GetCount(); i++)
228 AppData* pAppData = null;
229 pAppDataList->GetAt(i, pAppData);
230 TryReturn(pAppData, false, "pAppData is null");
232 // set permission(755) to bin file.
233 destPath = appRootPath + DIR_BIN + L"/" + pAppData->__name + L".exe";
234 if (File::IsFileExist(destPath) == true)
236 InstallerUtil::ChangeMode(destPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
244 PermissionManager::SetSymLink(InstallationContext* pContext)
248 String appRootPath = pContext->__rootPath;
250 SmackManager smackManager;
253 oldPath = appRootPath + DIR_RES;
254 newPath = appRootPath + L"/Res";
255 InstallerUtil::CreateSymlink(oldPath, newPath);
257 newPath = appRootPath + L"/Res";
258 std::unique_ptr< char[] > pResPath(_StringConverter::CopyToCharArrayN(newPath));
259 int ret = symlink("./res", pResPath.get());
260 smackManager.AddLabelDir(pContext->__packageId, newPath);
264 oldPath = appRootPath + DIR_DATA;
265 newPath = appRootPath + L"/Home";
266 InstallerUtil::CreateSymlink(oldPath, newPath);
268 newPath = appRootPath + L"/Home";
269 std::unique_ptr< char[] > pHomePath(_StringConverter::CopyToCharArrayN(newPath));
270 ret = symlink("./data", pHomePath.get());
271 smackManager.AddLabelDir(pContext->__packageId, newPath);
274 oldPath = appRootPath + DIR_RES + L"/screen-size-normal";
275 newPath = appRootPath + L"/Res/ScreenSize-Normal";
276 InstallerUtil::CreateSymlink(oldPath, newPath);
278 oldPath = appRootPath + DIR_RES + L"/screen-density-high";
279 newPath = appRootPath + L"/Res/ScreenDensity-High";
280 InstallerUtil::CreateSymlink(oldPath, newPath);
282 oldPath = appRootPath + DIR_RES + L"/screen-density-middle";
283 newPath = appRootPath + L"/Res/ScreenDensity-Middle";
284 InstallerUtil::CreateSymlink(oldPath, newPath);
286 oldPath = appRootPath + DIR_RES + L"/screen-density-low";
287 newPath = appRootPath + L"/Res/ScreenDensity-Low";
288 InstallerUtil::CreateSymlink(oldPath, newPath);
294 PermissionManager::CopyForRds(InstallationContext* pContext, IList* pFileList, bool& isInstallRequired)
296 TryReturn(pFileList, false, "pFileList is null.");
298 for (int idx = 0; idx < pFileList->GetCount(); idx++)
300 String* pFilePath = static_cast<String *>(pFileList->GetAt(idx));
301 TryReturn(pFilePath, false, "pFilePath is null.");
303 String srcFile = DIR_APPLICATIONS_TMP;
304 srcFile += L"/" + pContext->__packageId + L"/" + *pFilePath;
305 String destFile = pContext->__rootPath + L"/" + *pFilePath;
307 AppLog("copy file from[%ls] to[%ls]", srcFile.GetPointer(), destFile.GetPointer());
310 result r = E_SUCCESS;
313 if (destFile.EndsWith(L"/") == true)
316 if (File::IsFileExist(destDir) == false)
318 Directory::Create(destDir, true);
324 r = destFile.LastIndexOf(L'/', destFile.GetLength() -1, pos);
325 if (IsFailed(r) == true)
327 AppLog("destFile is invalid[%ls]", destFile.GetPointer());
330 destFile.SubString(0, pos, destDir);
333 if (File::IsFileExist(destDir) == false)
335 Directory::Create(destDir, true);
338 InstallerUtil::Remove(destFile);
339 r = File::Copy(srcFile, destFile, true);
340 if (IsFailed(r) == true)
342 AppLog("File::Copy() failed. [%ls] -> [%ls]", srcFile.GetPointer(), destFile.GetPointer());
346 if (*pFilePath == L"info/manifest.xml")
348 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
349 isInstallRequired = true;
351 else if (pFilePath->StartsWith("setting/setting", 0) == true)
353 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
354 isInstallRequired = true;
362 PermissionManager::ApplyPermissionForRds(InstallationContext* pContext)
366 SmackManager smackManager;
367 smackManager.Construct(pContext);
369 appRootPath = pContext->__rootPath;
370 PackageId packageId = pContext->__packageId;
373 InstallerUtil::ChangeMode(appRootPath, PERM_BASE | PERM_EXECUTE);
374 smackManager.AddLabelDir(packageId, appRootPath, true);
377 destPath = appRootPath + DIR_BIN;
378 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
379 smackManager.AddLabelDir(packageId, destPath);
382 destPath = appRootPath + DIR_INFO;
383 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
384 smackManager.AddLabelDir(packageId, destPath);
387 destPath = appRootPath + DIR_RES;
388 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
389 smackManager.AddLabelDir(packageId, destPath);
392 destPath = appRootPath + DIR_LIB;
393 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_EXECUTE, false);
394 smackManager.AddLabelDir(packageId, destPath);
397 destPath = appRootPath + DIR_SHARED;
398 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
399 smackManager.AddLabelDir(packageId, destPath);
401 // appRoot/shared/res
402 destPath = appRootPath + DIR_SHARED_RES;
403 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, false);
404 smackManager.AddLabelSharedDir(packageId, destPath);
406 // appRoot/shared/data
407 destPath = appRootPath + DIR_SHARED_DATA;
408 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
409 smackManager.AddLabelSharedDir(packageId, destPath);
411 // appRoot/shared/trusted
412 destPath = appRootPath + DIR_SHARED_TRUSTED;
413 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
414 smackManager.AddLabelSharedDir(packageId, destPath);
417 destPath = appRootPath + DIR_SETTING;
418 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE | PERM_WRITE, false);
419 smackManager.AddLabelDir(packageId, destPath);
422 destPath = appRootPath + DIR_DATA;
423 InstallerUtil::ChangeDirectoryPermission(destPath, PERM_BASE, true);
424 smackManager.AddLabelDir(packageId, destPath);
431 PermissionManager::ApplyPermission(InstallationContext* pContext, const IList* pFileList)
433 TryReturn(pContext, false, "pContext is null");
434 TryReturn(pFileList, false, "pFileList is null");
436 // dir | type | owner | mode | smack
437 static RdsPermssions perms[] =
439 {"res", PERM_DIR_RES, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
440 {"data", PERM_DIR_DATA, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
441 {"bin", PERM_DIR_BIN, PERM_OWNER_ROOT, (PERM_BASE | PERM_EXECUTE), PERM_SMACK_0},
442 {"info", PERM_DIR_INFO, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
443 {"lib", PERM_DIR_LIB, PERM_OWNER_ROOT, (PERM_BASE | PERM_EXECUTE), PERM_SMACK_0},
444 {"setting", PERM_DIR_SETTING, PERM_OWNER_ROOT, (PERM_BASE | PERM_WRITE), PERM_SMACK_0},
445 {"shared/res", PERM_DIR_SHARED_RES, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
446 {"shared/data", PERM_DIR_SHARED_DATA, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
447 {"shared/trusted", PERM_DIR_SHARED_TRUSTED, PERM_OWNER_APP, PERM_BASE, PERM_SMACK_0},
448 {"shared", PERM_DIR_SHARED, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
449 {"author", PERM_DIR_ROOT, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
450 {"signature", PERM_DIR_ROOT, PERM_OWNER_ROOT, PERM_BASE, PERM_SMACK_0},
453 static int permCount = sizeof(perms) / sizeof(RdsPermssions);
454 bool isInstallRequired = false;
455 SmackManager smackManager;
456 smackManager.Construct(pContext);
458 for (int i = 0; i < pFileList->GetCount(); i++)
460 const String *pFilePath = static_cast<const String *>(pFileList->GetAt(i));
461 TryReturn(pFilePath, false, "pFilePath is null");
463 AppLog("pFilePath=(%ls)", pFilePath->GetPointer());
466 for (permIdx = 0; permIdx < permCount; permIdx++)
468 if (pFilePath->StartsWith(perms[permIdx].pDir, 0) == true)
470 if (perms[permIdx].dirType == PERM_DIR_INFO)
472 if (*pFilePath == L"info/manifest.xml")
474 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
475 isInstallRequired = true;
478 else if (perms[permIdx].dirType == PERM_DIR_SETTING)
480 if (pFilePath->StartsWith("setting/setting", 0) == true)
482 AppLog("Install required, [%ls] is found", pFilePath->GetPointer());
483 isInstallRequired = true;
487 AppLog("dirType=(%d), owner=(%d), mode=(%d), smack=(%d)",
488 perms[permIdx].dirType, perms[permIdx].ownerIndex, perms[permIdx].modeIndex, perms[permIdx].smackIndex);
490 String srcFile = DIR_APPLICATIONS_TMP;
491 srcFile += L"/" + pContext->__packageId + L"/" + *pFilePath;
492 String destFile = pContext->__rootPath + L"/" + *pFilePath;
494 AppLog("copy file: from[%ls] to[%ls]", srcFile.GetPointer(), destFile.GetPointer());
495 File::Copy(srcFile, destFile, false);
497 bool isAppOwner = false;
498 if (perms[permIdx].ownerIndex == PERM_OWNER_APP)
504 if (perms[permIdx].dirType != PERM_DIR_ROOT)
506 destPath = pContext->__rootPath + L"/" + perms[permIdx].pDir;
507 InstallerUtil::ChangeDirectoryPermission(destPath, perms[permIdx].modeIndex, isAppOwner);
511 destPath = pContext->__rootPath + L"/" + *pFilePath;
512 InstallerUtil::ChangeMode(*pFilePath, perms[permIdx].modeIndex);
514 smackManager.AddLabelSharedDir(pContext->__packageId, destPath);