2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FSecAccessController.cpp
19 * @brief This is the implementation for the AccessController class.
22 #include <unique_ptr.h>
23 #include <FAppTypes.h>
24 #include <FAppApplication.h>
25 #include <FApp_AppInfo.h>
26 #include <FApp_AppManagerImpl.h>
27 #include <FAppPkg_PackageInfoImpl.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseString.h>
30 #include <FBaseColArrayList.h>
32 #include <FIo_IpcClient.h>
33 #include <FSecAccessController.h>
34 #include <FSec_AccessController.h>
35 #include "FSec_AccessControlTypes.h"
36 #include "FSec_PrivilegeManager.h"
37 #include "FSec_PrivilegeManagerMessage.h"
38 #include "FSec_PrivilegeInfo.h"
41 using namespace Tizen::App;
42 using namespace Tizen::App::Package;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Io;
47 static bool isConstructed = false;
48 static std::unique_ptr<_IpcClient> pIpcClient(null);
49 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
50 static pthread_once_t ipcOnceBlock = PTHREAD_ONCE_INIT;
52 namespace Tizen { namespace Security
55 AccessController::AccessController(void)
60 AccessController::~AccessController(void)
65 static _PrivilegeInfo privilegeInfo;
69 AccessController::InitIpcClient(void)
71 std::unique_ptr<_IpcClient> pLocalIpcClient(new (std::nothrow) _IpcClient);
72 SysTryReturnVoidResult(NID_SEC, pLocalIpcClient != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
74 result r = pLocalIpcClient->Construct(L"osp.security.ipcserver.privilegemanager", null);
75 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "Failed to construct the instance of IPC.");
77 pIpcClient = std::move(pLocalIpcClient);
82 AccessController::Initialize(void)
86 if (pIpcClient == null)
88 pthread_once(&ipcOnceBlock, InitIpcClient);
92 ipcOnceBlock = PTHREAD_ONCE_INIT;
93 SysLogException(NID_SEC, r, "[%s] Propagated.", GetErrorMessage(r));
98 std::unique_ptr<String> pEncryptedPrivileges(new (std::nothrow) String());
99 SysTryReturnVoidResult(NID_SEC, pEncryptedPrivileges != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
101 std::unique_ptr<String> pChecksum(new (std::nothrow) String());
102 SysTryReturnVoidResult(NID_SEC, pChecksum != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
104 std::unique_ptr<ArrayList> pPrivilegeList(new ArrayList());
105 SysTryReturnVoidResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
107 pPrivilegeList->Construct();
109 std::unique_ptr<IPC::Message> pCipherPrivilegeMessage(new (std::nothrow) PrivilegeManagerMsg_retrieve(pEncryptedPrivileges.get(), pChecksum.get(), pPrivilegeList.get(), &r));
110 SysTryReturnVoidResult(NID_SEC, pCipherPrivilegeMessage != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
112 result ipcResult = pIpcClient->SendRequest(pCipherPrivilegeMessage.get());
113 SysTryReturnVoidResult(NID_SEC, ipcResult == E_SUCCESS, E_SYSTEM, "Failed to send IPC message.");
114 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, r, "Failed to retrieve privilege information");
116 std::unique_ptr<String> pEncryptedVisibility(new (std::nothrow) String());
117 SysTryReturnVoidResult(NID_SEC, pEncryptedVisibility != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
119 std::unique_ptr<String> pVisibilityChecksum(new (std::nothrow) String());
120 SysTryReturnVoidResult(NID_SEC, pVisibilityChecksum != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
122 std::unique_ptr<IPC::Message> pCipherVisibilityMessage(new (std::nothrow) PrivilegeManagerMsg_retrieveEx(pEncryptedVisibility.get(), pVisibilityChecksum.get(), &r));
123 SysTryReturnVoidResult(NID_SEC, pCipherVisibilityMessage != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
125 ipcResult = pIpcClient->SendRequest(pCipherVisibilityMessage.get());
126 SysTryReturnVoidResult(NID_SEC, ipcResult == E_SUCCESS, E_SYSTEM, "Failed to send IPC message.");
127 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, r, "Failed to retrieve privilege information");
129 PackageId packageId = _AppInfo::GetPackageId();
130 packageId[0] = packageId[0];
132 r = privilegeInfo.Construct(packageId, *(pEncryptedPrivileges.get()), *(pChecksum.get()), *(pEncryptedVisibility.get()), *(pVisibilityChecksum.get()), pPrivilegeList.get());
133 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
135 pPrivilegeList->RemoveAll(true);
136 isConstructed = true;
142 AccessController::CheckPrivilege(int privilege)
144 result r = E_SUCCESS;
148 SysTryReturnResult(NID_SEC, (privilege >= 0) && (privilege < _MAX_PRIVILEGE_ENUM), E_INVALID_ARG, "The privilege enumerator is invalid");
152 pthread_once(&onceBlock, Initialize);
156 if (r == E_DATA_NOT_FOUND)
158 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
163 onceBlock = PTHREAD_ONCE_INIT;
164 SysLogException(NID_SEC, r, "[%s] Propagated.", GetErrorMessage(r));
170 if (privilegeInfo.GetAppId().IsEmpty())
172 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
173 r = E_DATA_NOT_FOUND;
178 SysLog(NID_SEC, "%ls is in the cache [client]", privilegeInfo.GetAppId().GetPointer());
181 ret = privilegeInfo.HasPrivilege(static_cast< _Privilege >(privilege));
184 r = E_PRIVILEGE_DENIED;
188 r = _AccessController::CheckPrivacy(privilegeInfo.GetAppId(), static_cast< _Privilege >(privilege));
189 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
195 SysLogException(NID_SEC, r, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
200 AccessController::CheckPrivilege(const String& privilege)
202 result r = E_SUCCESS;
208 pthread_once(&onceBlock, Initialize);
212 if (r == E_DATA_NOT_FOUND)
214 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
219 onceBlock = PTHREAD_ONCE_INIT;
220 SysLogException(NID_SEC, r, "[%s] Propagated.", GetErrorMessage(r));
226 if (privilegeInfo.GetAppId().IsEmpty())
228 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
229 r = E_DATA_NOT_FOUND;
234 SysLog(NID_SEC, "%ls is in the cache [client]", privilegeInfo.GetAppId().GetPointer());
237 ret = privilegeInfo.HasPrivilege(privilege);
240 r = E_PRIVILEGE_DENIED;
244 r = _AccessController::CheckPrivacy(privilegeInfo.GetAppId(), privilege);
245 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
251 SysLogException(NID_SEC, r, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
255 _PrivilegeManager* AccessController::__pPrivilegeManager = null;
258 AccessController::CheckPrivilege(const PackageId& packageId, const String& privilege)
261 result r = _AccessController::CheckUserPrivilege(_PRV_PRIVILEGEMANAGER_READ);
262 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
265 packageId.SubString(0, MAX_APP_ID_SIZE, subAppId);
267 _PackageInfoImpl infoImpl;
268 r = infoImpl.Construct(packageId);
269 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
272 String webAppType(L"wgt");
273 String cAppType(L"rpm");
274 appType = infoImpl.GetAppType();
275 if (appType.Equals(webAppType, true))
279 else if (appType.Equals(cAppType, true))
281 r = _AccessController::CheckPrivacy(packageId, privilege);
282 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
287 if (__pPrivilegeManager == null)
289 __pPrivilegeManager = _PrivilegeManager::GetInstance();
291 SysTryReturnResult(NID_SEC, __pPrivilegeManager != null, E_SYSTEM, "An unexpected system error occurred.");
293 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(__pPrivilegeManager->RetrievePrivilegeInfoN(subAppId));
300 else if (r == E_DATA_NOT_FOUND)
302 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
307 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
311 ret = pPrivilegeInfo->HasPrivilege(privilege);
314 r = E_PRIVILEGE_DENIED;
318 r = _AccessController::CheckPrivacy(packageId, privilege);
319 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
325 SysLogException(NID_SEC, r, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");