sync with master
[platform/framework/native/appfw.git] / src / security / FSec_PrivilegeCache.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FSecurity_PrivilegeCache.cpp
20  * @brief               This is the implementation for the Privilege Manager class.
21  */
22
23 #include <unique_ptr.h>
24 #include <stdio.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <FBaseSysLog.h>
30 #include <FBaseString.h>
31 #include <FBaseColHashMap.h>
32 #include <FSecCryptoSha1Hmac.h>
33
34 #include "FSec_AccessControlTypes.h"
35 #include "FSec_PrivilegeCache.h"
36 #include "FSec_PrivilegeInfo.h"
37
38 using namespace Tizen::App;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Runtime;
42
43 namespace Tizen { namespace Security
44 {
45
46 std::unique_ptr<Mutex> _PrivilegeCache::__pMutex(null);
47
48 _PrivilegeCache::_PrivilegeCache(void)
49         : __pPrivilegeList(null)
50 {
51
52 }
53
54 _PrivilegeCache::~_PrivilegeCache(void)
55 {
56         __pPrivilegeList->RemoveAll(true);
57 }
58
59 result
60 _PrivilegeCache::Construct(void)
61 {
62         result r = E_SUCCESS;
63
64         std::unique_ptr<HashMap> pPrivilegeList(new (std::nothrow) HashMap());
65         SysTryReturnResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
66
67         r = pPrivilegeList->Construct(32, 0.75);
68         SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
69
70         std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
71         SysTryReturnResult(NID_SEC, pMutex != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
72
73         r = pMutex->Create();
74         SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
75
76         __pPrivilegeList = std::move(pPrivilegeList);
77         __pMutex = std::move(pMutex);
78
79         return r;
80 }
81
82 result
83 _PrivilegeCache::AddPrivilegeInfo(const _PrivilegeInfo& privilegeInfo)
84 {
85         result r = E_SUCCESS;
86         result mutexResult = E_SUCCESS;
87
88         std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
89         std::unique_ptr<String> pKey(null);
90
91         SysTryReturnResult(NID_SEC, privilegeInfo.GetAppId().GetLength() == MAX_APP_ID_SIZE, E_INVALID_ARG, "The argument is invalid.");
92
93         pPrivilegeInfo.reset(privilegeInfo.CloneN());
94         SysTryReturnResult(NID_SEC, pPrivilegeInfo != null, E_SYSTEM, "An unexpected system error occurred.");
95
96         pKey.reset(new String(pPrivilegeInfo->GetAppId()));
97         SysTryReturnResult(NID_SEC, pKey != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
98
99         mutexResult = __pMutex->Acquire();
100         SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
101
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.");
104
105         mutexResult = __pMutex->Release();
106         SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
107
108         return r;
109
110 CATCH2:
111
112         mutexResult = __pMutex->Release();
113         SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, mutexResult = E_SYSTEM, E_SYSTEM, "An unexpected system error occurred.");
114
115         return r;
116
117 CATCH:
118         return r;
119 }
120
121 result
122 _PrivilegeCache::RemovePrivilegeInfo(const AppId& appId)
123 {
124         result r = E_SUCCESS;
125         result mutextResult = E_SUCCESS;
126
127         mutextResult = __pMutex->Acquire();
128         SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
129
130         r = __pPrivilegeList->Remove(appId, true);
131         if ((r == E_SUCCESS) || (r == E_OBJ_NOT_FOUND))
132         {
133                 r = E_SUCCESS;
134                 SetLastResult(r);
135         }
136         else
137         {
138                 r = E_SYSTEM;
139                 SysLogException(NID_SEC, r, "[E_SYSTEM] An unexpected system error occurred.");
140         }
141
142         mutextResult = __pMutex->Release();
143         SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
144
145         return r;
146 }
147
148 _PrivilegeInfo*
149 _PrivilegeCache::GetPrivilegeInfoN(const AppId& appId) const
150 {
151         result r = E_SUCCESS;
152         result mutexResult = E_SUCCESS;
153
154         std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
155
156         ClearLastResult();
157
158         mutexResult = __pMutex->Acquire();
159         SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
160
161         _PrivilegeInfo* pTempInfo = static_cast< _PrivilegeInfo* >(__pPrivilegeList->GetValue(appId));
162         r = GetLastResult();
163
164         mutexResult = __pMutex->Release();
165         SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
166
167         if (r == E_SUCCESS)
168         {
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.");
171
172                 r = pPrivilegeInfo->Construct(*pTempInfo);
173                 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
174
175                 SysLog(NID_SEC, "%ls is in the cacheList [server]", pPrivilegeInfo->GetAppId().GetPointer());
176         }
177         else if (r == E_OBJ_NOT_FOUND)
178         {
179                 SetLastResult(E_DATA_NOT_FOUND);
180         }
181         else
182         {
183                 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
184         }
185
186         return pPrivilegeInfo.release();
187 }
188
189 }} //Tizen::Security