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 FSecurity_PrivilegeCache.cpp
19 * @brief This is the implementation for the Privilege Manager class.
22 #include <unique_ptr.h>
27 #include <sys/types.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseString.h>
30 #include <FBaseColHashMap.h>
31 #include <FSecCryptoSha1Hmac.h>
33 #include "FSec_AccessControlTypes.h"
34 #include "FSec_PrivilegeCache.h"
35 #include "FSec_PrivilegeInfo.h"
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Runtime;
42 namespace Tizen { namespace Security
45 std::unique_ptr<Mutex> _PrivilegeCache::__pMutex(null);
47 _PrivilegeCache::_PrivilegeCache(void)
48 : __pPrivilegeList(null)
53 _PrivilegeCache::~_PrivilegeCache(void)
55 __pPrivilegeList->RemoveAll(true);
59 _PrivilegeCache::Construct(void)
63 std::unique_ptr<HashMap> pPrivilegeList(new (std::nothrow) HashMap());
64 SysTryReturnResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
66 r = pPrivilegeList->Construct(32, 0.75);
67 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
69 std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
70 SysTryReturnResult(NID_SEC, pMutex != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
73 SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
75 __pPrivilegeList = std::move(pPrivilegeList);
76 __pMutex = std::move(pMutex);
82 _PrivilegeCache::AddPrivilegeInfo(const _PrivilegeInfo& privilegeInfo)
85 result mutexResult = E_SUCCESS;
87 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
88 std::unique_ptr<String> pKey(null);
90 SysTryReturnResult(NID_SEC, privilegeInfo.GetAppId().GetLength() == MAX_APP_ID_SIZE, E_INVALID_ARG, "The argument is invalid.");
92 pPrivilegeInfo.reset(privilegeInfo.CloneN());
93 SysTryReturnResult(NID_SEC, pPrivilegeInfo != null, E_SYSTEM, "An unexpected system error occurred.");
95 pKey.reset(new String(pPrivilegeInfo->GetAppId()));
96 SysTryReturnResult(NID_SEC, pKey != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
98 mutexResult = __pMutex->Acquire();
99 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
101 r = __pPrivilegeList->Add(*(pKey.release()), *(pPrivilegeInfo.release()));
102 SysTryCatchLabel(NID_SEC, r == E_SUCCESS, r = E_SYSTEM, CATCH2, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
104 mutexResult = __pMutex->Release();
105 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
111 mutexResult = __pMutex->Release();
112 SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, mutexResult = E_SYSTEM, E_SYSTEM, "An unexpected system error occurred.");
121 _PrivilegeCache::RemovePrivilegeInfo(const AppId& appId)
123 result r = E_SUCCESS;
124 result mutextResult = E_SUCCESS;
126 mutextResult = __pMutex->Acquire();
127 SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
129 r = __pPrivilegeList->Remove(appId, true);
130 if ((r == E_SUCCESS) || (r == E_OBJ_NOT_FOUND))
138 SysLogException(NID_SEC, r, "[E_SYSTEM] An unexpected system error occurred.");
141 mutextResult = __pMutex->Release();
142 SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
148 _PrivilegeCache::GetPrivilegeInfoN(const AppId& appId) const
150 result r = E_SUCCESS;
151 result mutexResult = E_SUCCESS;
153 std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
157 mutexResult = __pMutex->Acquire();
158 SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
160 _PrivilegeInfo* pTempInfo = static_cast< _PrivilegeInfo* >(__pPrivilegeList->GetValue(appId));
163 mutexResult = __pMutex->Release();
164 SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
168 pPrivilegeInfo.reset(new (std::nothrow) _PrivilegeInfo());
169 SysTryReturn(NID_SEC, pPrivilegeInfo != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation is failed.");
171 r = pPrivilegeInfo->Construct(*pTempInfo);
172 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
174 SysLog(NID_SEC, "%ls is in the cacheList [server]", pPrivilegeInfo->GetAppId().GetPointer());
176 else if (r == E_OBJ_NOT_FOUND)
178 SetLastResult(E_DATA_NOT_FOUND);
182 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
185 return pPrivilegeInfo.release();