sync with tizen_2.0
[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         SysLog(NID_SEC, "Enter.");
65
66         std::unique_ptr<HashMap> pPrivilegeList(new (std::nothrow) HashMap());
67         SysTryReturnResult(NID_SEC, pPrivilegeList != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
68
69         r = pPrivilegeList->Construct(32, 0.75);
70         SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
71
72         std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
73         SysTryReturnResult(NID_SEC, pMutex != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
74
75         r = pMutex->Create();
76         SysTryReturnResult(NID_SEC, r == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
77
78         __pPrivilegeList = std::move(pPrivilegeList);
79         __pMutex = std::move(pMutex);
80
81         SysLog(NID_SEC, "Exit.");
82         return r;
83 }
84
85 result
86 _PrivilegeCache::AddPrivilegeInfo(const _PrivilegeInfo& privilegeInfo)
87 {
88         result r = E_SUCCESS;
89         result mutexResult = E_SUCCESS;
90
91         std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
92         std::unique_ptr<String> pKey(null);
93
94         SysLog(NID_SEC, "Enter.");
95
96         SysTryReturnResult(NID_SEC, privilegeInfo.GetAppId().GetLength() == MAX_APP_ID_SIZE, E_INVALID_ARG, "The argument is invalid.");
97
98         pPrivilegeInfo.reset(privilegeInfo.CloneN());
99         SysTryReturnResult(NID_SEC, pPrivilegeInfo != null, E_SYSTEM, "An unexpected system error occurred.");
100
101         pKey.reset(new String(pPrivilegeInfo->GetAppId()));
102         SysTryReturnResult(NID_SEC, pKey != null, E_OUT_OF_MEMORY, "Memory allocation is failed.");
103
104         mutexResult = __pMutex->Acquire();
105         SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
106
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.");
109
110         mutexResult = __pMutex->Release();
111         SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
112
113         SysLog(NID_SEC, "Exit.");
114         return r;
115
116 CATCH2:
117
118         mutexResult = __pMutex->Release();
119         SysTryCatch(NID_SEC, mutexResult == E_SUCCESS, mutexResult = E_SYSTEM, E_SYSTEM, "An unexpected system error occurred.");
120
121
122         SysLog(NID_SEC, "Exit.");
123         return r;
124
125 CATCH:
126
127         SysLog(NID_SEC, "Exit.");
128         return r;
129 }
130
131 result
132 _PrivilegeCache::RemovePrivilegeInfo(const AppId& appId)
133 {
134         result r = E_SUCCESS;
135         result mutextResult = E_SUCCESS;
136
137         SysLog(NID_SEC, "Enter.");
138
139         mutextResult = __pMutex->Acquire();
140         SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
141
142         r = __pPrivilegeList->Remove(appId, true);
143         if ((r == E_SUCCESS) || (r == E_OBJ_NOT_FOUND))
144         {
145                 r = E_SUCCESS;
146                 SetLastResult(r);
147         }
148         else
149         {
150                 r = E_SYSTEM;
151                 SysLogException(NID_SEC, r, "[E_SYSTEM] An unexpected system error occurred.");
152         }
153
154         mutextResult = __pMutex->Release();
155         SysTryReturnResult(NID_SEC, mutextResult == E_SUCCESS, E_SYSTEM, "An unexpected system error occurred.");
156
157         SysLog(NID_SEC, "Exit.");
158         return r;
159 }
160
161 _PrivilegeInfo*
162 _PrivilegeCache::GetPrivilegeInfoN(const AppId& appId) const
163 {
164         result r = E_SUCCESS;
165         result mutexResult = E_SUCCESS;
166
167         std::unique_ptr<_PrivilegeInfo> pPrivilegeInfo(null);
168
169         SysLog(NID_SEC, "Enter.");
170         ClearLastResult();
171
172         mutexResult = __pMutex->Acquire();
173         SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
174
175         _PrivilegeInfo* pTempInfo = static_cast< _PrivilegeInfo* >(__pPrivilegeList->GetValue(appId));
176         r = GetLastResult();
177
178         mutexResult = __pMutex->Release();
179         SysTryReturn(NID_SEC, mutexResult == E_SUCCESS, null, E_SYSTEM, "An unexpected system error occurred.");
180
181         if (r == E_SUCCESS)
182         {
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.");
185
186                 r = pPrivilegeInfo->Construct(*pTempInfo);
187                 SysTryReturn(NID_SEC, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
188
189                 SysLog(NID_SEC, "%ls is in the cacheList [server]", pPrivilegeInfo->GetAppId().GetPointer());
190         }
191         else if (r == E_OBJ_NOT_FOUND)
192         {
193                 SetLastResult(E_DATA_NOT_FOUND);
194         }
195         else
196         {
197                 SysLogException(NID_SEC, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
198         }
199
200         SysLog(NID_SEC, "Exit.");
201         return pPrivilegeInfo.release();
202 }
203
204 }} //Tizen::Security