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 std::unique_ptr<HashMap> pPrivilegeList(new (std::nothrow) HashMap());
65 SysTryReturnResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
67 r = pPrivilegeList->Construct(32, 0.75);
68 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
70 std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
71 SysTryReturnResult(NID_SEC, pMutex != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
74 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
76 __pPrivilegeList = std::move(pPrivilegeList);
77 __pMutex = std::move(pMutex);
83 _PrivilegeCache::AddPrivilegeInfo(const _PrivilegeInfo& privilegeInfo)
86 result mutexResult = E_SUCCESS;
88 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
89 std::unique_ptr<String> pKey(null);
91 SysTryReturnResult(NID_SEC, privilegeInfo.GetAppId().GetLength() == MAX_APP_ID_SIZE, E_INVALID_ARG, "The argument is invalid.");
93 pPrivilegeInfo.reset(privilegeInfo.CloneN());
94 SysTryReturnResult(NID_SEC, pPrivilegeInfo != null, E_SYSTEM, "An unexpected system error occurred.");
96 pKey.reset(new String(pPrivilegeInfo->GetAppId()));
97 SysTryReturnResult(NID_SEC, pKey != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
99 mutexResult = __pMutex->Acquire();
100 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
102 r = __pPrivilegeList->Add(*(pKey.release()), *(pPrivilegeInfo.release()));
103 SysTryCatchLabel(NID_SEC, r == E_SUCCESS, r = E_SYSTEM, CATCH2, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
105 mutexResult = __pMutex->Release();
106 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
112 mutexResult = __pMutex->Release();
113 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, mutexResult = E_SYSTEM, E_SYSTEM, "An unexpected system error occurred.");
122 _PrivilegeCache::RemovePrivilegeInfo(const AppId& appId)
124 result r = E_SUCCESS;
125 result mutextResult = E_SUCCESS;
127 mutextResult = __pMutex->Acquire();
128 SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
130 r = __pPrivilegeList->Remove(appId, true);
131 if ((r == E_SUCCESS) || (r == E_OBJ_NOT_FOUND))
139 SysLogException(NID_SEC, r, "[E_SYSTEM] An unexpected system error occurred.");
142 mutextResult = __pMutex->Release();
143 SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
149 _PrivilegeCache::GetPrivilegeInfoN(const AppId& appId) const
151 result r = E_SUCCESS;
152 result mutexResult = E_SUCCESS;
154 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
158 mutexResult = __pMutex->Acquire();
159 SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
161 _PrivilegeInfo* pTempInfo = static_cast< _PrivilegeInfo* >(__pPrivilegeList->GetValue(appId));
164 mutexResult = __pMutex->Release();
165 SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
169 pPrivilegeInfo.reset(new (std::nothrow) _PrivilegeInfo());
170 SysTryReturn(NID_SEC, pPrivilegeInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
172 r = pPrivilegeInfo->Construct(*pTempInfo);
173 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
175 SysLog(NID_SEC, "%ls is in the cacheList [server]", pPrivilegeInfo->GetAppId().GetPointer());
177 else if (r == E_OBJ_NOT_FOUND)
179 SetLastResult(E_DATA_NOT_FOUND);
183 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
186 return pPrivilegeInfo.release();