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.
19 * @file FSecAccessController.cpp
20 * @brief This is the implementation for the AccessController class.
23 #include <unique_ptr.h>
24 #include <FAppTypes.h>
25 #include <FAppApplication.h>
26 #include <FApp_AppInfo.h>
27 #include <FApp_AppManagerImpl.h>
28 #include <FAppPkg_PackageInfoImpl.h>
29 #include <FBaseSysLog.h>
30 #include <FBaseString.h>
31 #include <FBaseColArrayList.h>
33 #include <FIo_IpcClient.h>
34 #include <FSecAccessController.h>
35 #include <FSec_AccessController.h>
36 #include "FSec_AccessControlTypes.h"
37 #include "FSec_PrivilegeManager.h"
38 #include "FSec_PrivilegeManagerMessage.h"
39 #include "FSec_PrivilegeInfo.h"
42 using namespace Tizen::App;
43 using namespace Tizen::App::Package;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Io;
48 static _IpcClient ipcClient;
49 static bool isConstructed = false;
50 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
52 namespace Tizen { namespace Security
55 AccessController::AccessController(void)
60 AccessController::~AccessController(void)
65 static _PrivilegeInfo privilegeInfo;
69 AccessController::Initialize(void)
72 result ipcResult = E_SUCCESS;
74 std::unique_ptr<String> pEncryptedPrivileges(null);
75 std::unique_ptr<String> pChecksum(null);
76 std::unique_ptr<String> pEncryptedVisibility(null);
77 std::unique_ptr<String> pVisibilityChecksum(null);
79 std::unique_ptr<IPC::Message> pCipherPrivilegeMessage(null);
80 std::unique_ptr<IPC::Message> pCipherVisibilityMessage(null);
81 std::unique_ptr<ArrayList> pPrivilegeList(null);
83 r = ipcClient.Construct(L"osp.security.ipcserver.privilegemanager", null);
84 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "Failed to construct the instance of IPC.");
86 pEncryptedPrivileges.reset(new (std::nothrow) String());
87 SysTryReturnVoidResult(NID_SEC, pEncryptedPrivileges != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
89 pChecksum.reset(new (std::nothrow) String());
90 SysTryReturnVoidResult(NID_SEC, pChecksum != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
92 pPrivilegeList.reset(new ArrayList());
93 SysTryReturnVoidResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
95 pPrivilegeList->Construct();
97 pCipherPrivilegeMessage.reset(new (std::nothrow) PrivilegeManagerMsg_retrieve(pEncryptedPrivileges.get(), pChecksum.get(), pPrivilegeList.get(), &r));
98 SysTryReturnVoidResult(NID_SEC, pCipherPrivilegeMessage != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
100 ipcResult = ipcClient.SendRequest(pCipherPrivilegeMessage.get());
101 SysTryReturnVoidResult(NID_SEC, ipcResult == E_SUCCESS, E_SYSTEM, "Failed to send IPC message.");
102 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, r, "Failed to retrieve privilege information");
104 pEncryptedVisibility.reset(new (std::nothrow) String());
105 SysTryReturnVoidResult(NID_SEC, pEncryptedVisibility != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
107 pVisibilityChecksum.reset(new (std::nothrow) String());
108 SysTryReturnVoidResult(NID_SEC, pVisibilityChecksum != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
110 pCipherVisibilityMessage.reset(new (std::nothrow) PrivilegeManagerMsg_retrieveEx(pEncryptedVisibility.get(), pVisibilityChecksum.get(), &r));
111 SysTryReturnVoidResult(NID_SEC, pCipherVisibilityMessage != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
113 ipcResult = ipcClient.SendRequest(pCipherVisibilityMessage.get());
114 SysTryReturnVoidResult(NID_SEC, ipcResult == E_SUCCESS, E_SYSTEM, "Failed to send IPC message.");
115 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, r, "Failed to retrieve privilege information");
117 PackageId packageId = _AppInfo::GetPackageId();
118 packageId[0] = packageId[0];
120 r = privilegeInfo.Construct(packageId, *(pEncryptedPrivileges.get()), *(pChecksum.get()), *(pEncryptedVisibility.get()), *(pVisibilityChecksum.get()), pPrivilegeList.get());
121 SysTryReturnVoidResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
123 pPrivilegeList->RemoveAll(true);
124 isConstructed = true;
129 AccessController::CheckPrivilege(int privilege)
131 result r = E_SUCCESS;
136 SysTryReturnResult(NID_SEC, privilege < _MAX_PRIVILEGE_ENUM, E_INVALID_ARG, "The privilege enumerator is invalid");
140 pthread_once(&onceBlock, Initialize);
144 if (r == E_DATA_NOT_FOUND)
146 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
151 onceBlock = PTHREAD_ONCE_INIT;
152 SysLogException(NID_SEC, r, "[%s] Propagated.", GetErrorMessage(r));
158 if (privilegeInfo.GetAppId().IsEmpty())
160 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
161 r = E_DATA_NOT_FOUND;
166 SysLog(NID_SEC, "%ls is in the cache [client]", privilegeInfo.GetAppId().GetPointer());
169 ret = privilegeInfo.HasPrivilege(static_cast< _Privilege >(privilege));
172 r = E_PRIVILEGE_DENIED;
176 r = _AccessController::CheckPrivacy(privilegeInfo.GetAppId(), static_cast< _Privilege >(privilege));
177 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
183 SysLogException(NID_SEC, r, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
188 AccessController::CheckPrivilege(const String& privilege)
190 result r = E_SUCCESS;
197 pthread_once(&onceBlock, Initialize);
201 if (r == E_DATA_NOT_FOUND)
203 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
208 onceBlock = PTHREAD_ONCE_INIT;
209 SysLogException(NID_SEC, r, "[%s] Propagated.", GetErrorMessage(r));
215 if (privilegeInfo.GetAppId().IsEmpty())
217 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
218 r = E_DATA_NOT_FOUND;
223 SysLog(NID_SEC, "%ls is in the cache [client]", privilegeInfo.GetAppId().GetPointer());
226 ret = privilegeInfo.HasPrivilege(privilege);
229 r = E_PRIVILEGE_DENIED;
233 r = _AccessController::CheckPrivacy(privilegeInfo.GetAppId(), privilege);
234 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
240 SysLogException(NID_SEC, r, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
244 _PrivilegeManager* AccessController::__pPrivilegeManager = null;
247 AccessController::CheckPrivilege(const PackageId& packageId, const String& privilege)
249 result r = E_SUCCESS;
252 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
254 _PackageInfoImpl infoImpl;
256 String webAppType(L"wgt");
258 r = _AccessController::CheckUserPrivilege(_PRV_PRIVILEGEMANAGER_READ);
259 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
261 packageId.SubString(0, MAX_APP_ID_SIZE, subAppId);
263 r = infoImpl.Construct(subAppId);
264 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
266 appType = infoImpl.GetAppType();
267 if (appType.Equals(webAppType, true))
272 if (__pPrivilegeManager == null)
274 __pPrivilegeManager = _PrivilegeManager::GetInstance();
276 SysTryReturnResult(NID_SEC, __pPrivilegeManager != null, E_SYSTEM, "An unexpected system error occurred.");
278 pPrivilegeInfo.reset(__pPrivilegeManager->RetrievePrivilegeInfoN(subAppId));
285 else if (r == E_DATA_NOT_FOUND)
287 SysLogException(NID_SEC, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] The privilege information does not exist.");
292 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
296 ret = pPrivilegeInfo->HasPrivilege(privilege);
299 r = E_PRIVILEGE_DENIED;
303 r = _AccessController::CheckPrivacy(packageId, privilege);
304 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_USER_NOT_CONSENTED, "The user blocks an application from calling the method.");
310 SysLogException(NID_SEC, r, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");