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 FSecurity_PrivilegeCache.cpp
20 * @brief This is the implementation for the Privilege Manager class.
23 #include <unique_ptr.h>
28 #include <sys/types.h>
29 #include <FBaseSysLog.h>
30 #include <FBaseString.h>
31 #include <FBaseColHashMap.h>
32 #include <FSecCryptoSha1Hmac.h>
34 #include "FSec_AccessControlTypes.h"
35 #include "FSec_PrivilegeCache.h"
36 #include "FSec_PrivilegeInfo.h"
38 using namespace Tizen::App;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Runtime;
43 namespace Tizen { namespace Security
46 std::unique_ptr<Mutex> _PrivilegeCache::__pMutex(null);
48 _PrivilegeCache::_PrivilegeCache(void)
49 : __pPrivilegeList(null)
54 _PrivilegeCache::~_PrivilegeCache(void)
56 __pPrivilegeList->RemoveAll(true);
60 _PrivilegeCache::Construct(void)
64 SysLog(NID_SEC, "Enter.");
66 std::unique_ptr<HashMap> pPrivilegeList(new (std::nothrow) HashMap());
67 SysTryReturnResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
69 r = pPrivilegeList->Construct(32, 0.75);
70 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
72 std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
73 SysTryReturnResult(NID_SEC, pMutex != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
76 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
78 __pPrivilegeList = std::move(pPrivilegeList);
79 __pMutex = std::move(pMutex);
81 SysLog(NID_SEC, "Exit.");
86 _PrivilegeCache::AddPrivilegeInfo(const _PrivilegeInfo& privilegeInfo)
89 result mutexResult = E_SUCCESS;
91 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
92 std::unique_ptr<String> pKey(null);
94 SysLog(NID_SEC, "Enter.");
96 SysTryReturnResult(NID_SEC, privilegeInfo.GetAppId().GetLength() == MAX_APP_ID_SIZE, E_INVALID_ARG, "The argument is invalid.");
98 pPrivilegeInfo.reset(privilegeInfo.CloneN());
99 SysTryReturnResult(NID_SEC, pPrivilegeInfo != null, E_SYSTEM, "An unexpected system error occurred.");
101 pKey.reset(new String(pPrivilegeInfo->GetAppId()));
102 SysTryReturnResult(NID_SEC, pKey != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
104 mutexResult = __pMutex->Acquire();
105 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
107 r = __pPrivilegeList->Add(*(pKey.release()), *(pPrivilegeInfo.release()));
108 SysTryCatchLabel(NID_SEC, r == E_SUCCESS, r = E_SYSTEM, CATCH2, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
110 mutexResult = __pMutex->Release();
111 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
113 SysLog(NID_SEC, "Exit.");
118 mutexResult = __pMutex->Release();
119 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, mutexResult = E_SYSTEM, E_SYSTEM, "An unexpected system error occurred.");
122 SysLog(NID_SEC, "Exit.");
127 SysLog(NID_SEC, "Exit.");
132 _PrivilegeCache::RemovePrivilegeInfo(const AppId& appId)
134 result r = E_SUCCESS;
135 result mutextResult = E_SUCCESS;
137 SysLog(NID_SEC, "Enter.");
139 mutextResult = __pMutex->Acquire();
140 SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
142 r = __pPrivilegeList->Remove(appId, true);
143 if ((r == E_SUCCESS) || (r == E_OBJ_NOT_FOUND))
151 SysLogException(NID_SEC, r, "[E_SYSTEM] An unexpected system error occurred.");
154 mutextResult = __pMutex->Release();
155 SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
157 SysLog(NID_SEC, "Exit.");
162 _PrivilegeCache::GetPrivilegeInfoN(const AppId& appId) const
164 result r = E_SUCCESS;
165 result mutexResult = E_SUCCESS;
167 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
169 SysLog(NID_SEC, "Enter.");
172 mutexResult = __pMutex->Acquire();
173 SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
175 _PrivilegeInfo* pTempInfo = static_cast< _PrivilegeInfo* >(__pPrivilegeList->GetValue(appId));
178 mutexResult = __pMutex->Release();
179 SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
183 pPrivilegeInfo.reset(new (std::nothrow) _PrivilegeInfo());
184 SysTryReturn(NID_SEC, pPrivilegeInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
186 r = pPrivilegeInfo->Construct(*pTempInfo);
187 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
189 SysLog(NID_SEC, "%ls is in the cacheList [server]", pPrivilegeInfo->GetAppId().GetPointer());
191 else if (r == E_OBJ_NOT_FOUND)
193 SetLastResult(E_DATA_NOT_FOUND);
197 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
200 SysLog(NID_SEC, "Exit.");
201 return pPrivilegeInfo.release();