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 FSec_PrivilegeInfoImpl.cpp
19 * @brief This is the implementation for the _PrivilegeInfoImpl class.
26 #include <unique_ptr.h>
28 #include <FSysSystemTime.h>
29 #include <FSecPrivilegeInfo.h>
30 #include <FBaseSysLog.h>
32 #include <FBase_StringConverter.h>
33 #include <FSec_AccessControlTypes.h>
34 #include <FBaseUtilStringTokenizer.h>
35 #include <FBase_StringConverter.h>
36 #include <privilege_info.h>
38 #include "FSec_PrivilegeInfoImpl.h"
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Utility;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Io;
46 namespace Tizen { namespace Security
49 _PrivilegeInfoImpl::_PrivilegeInfoImpl(void)
54 _PrivilegeInfoImpl::~_PrivilegeInfoImpl(void)
61 _PrivilegeInfoImpl::GetName(const String& privilege) const
64 char* pPrivilegeDisplayName = null;
65 std::unique_ptr<char[]> pPrivilegeId(null);
68 pPrivilegeId.reset(_StringConverter::CopyToCharArrayN(privilege));
69 SysTryReturn(NID_SEC, pPrivilegeId != null, displayName, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
71 ret = privilege_info_get_privilege_display_name(pPrivilegeId.get(), &pPrivilegeDisplayName);
72 displayName.Append(pPrivilegeDisplayName);
74 if (pPrivilegeDisplayName != null)
76 free(pPrivilegeDisplayName);
82 _PrivilegeInfoImpl::GetDescription(const String& privilege) const
85 char* pPrivilegeDescription = null;
86 std::unique_ptr<char[]> pPrivilegeId(null);
89 pPrivilegeId.reset(_StringConverter::CopyToCharArrayN(privilege));
90 SysTryReturn(NID_SEC, pPrivilegeId != null, description, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
92 ret = privilege_info_get_privilege_description(pPrivilegeId.get(), &pPrivilegeDescription);
93 description.Append(pPrivilegeDescription);
95 if (pPrivilegeDescription != null)
97 free(pPrivilegeDescription);
103 _PrivilegeInfoImpl::CheckExternalPrivilege(const Tizen::Base::String& privilegeId)
105 result r = E_SUCCESS;
109 DbStatement* pStmt = null;
110 DbEnumerator* pEnum = null;
112 Database* pDb = new Database;
113 SysTryReturn(NID_SEC, pDb != null, false, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
115 r = pDb->Construct(EXTERNAL_PRIVILEGE_DATABASE_FILE_NAME, false);
116 SysTryCatch(NID_SEC, r == E_SUCCESS, flag = false, E_DATABASE, "External PrivilegeDb is not exist.");
118 query.Format(1024, L"SELECT PRIVILEGE_ID FROM PrivilegeInfo WHERE PRIVILEGE_ID = '%ls'", privilegeId.GetPointer());
120 pStmt = CreateStatementN(*pDb, query);
121 SysTryCatch(NID_SEC, pStmt != null, flag = false, E_DATABASE, "[E_DATABASE] An error occurs while creating a database statement.");
123 pEnum = ExecuteStatementN(*pDb, pStmt);
126 if (pEnum->MoveNext() == E_SUCCESS)
137 SysLog(NID_SEC, "[%ls] is not exist in PrivilegeDb.", privilegeId.GetPointer());
149 _PrivilegeInfoImpl::Construct(const Tizen::Base::String& privilegeId)
151 result r = E_SUCCESS;
153 String privilegeURI = L"http://tizen.org/privilege/";
154 String privilegeSubStringURI;
155 String privilegeSubString;
158 privilegeId.SubString(0, privilegeURI.GetLength(), privilegeSubStringURI);
159 bool validStringFlag = false;
161 if (privilegeSubStringURI.Equals(privilegeURI, true))
163 privilegeId.SubString(privilegeURI.GetLength(), privilegeId.GetLength() - privilegeURI.GetLength(), privilegeSubString);
164 for (index = 0; index < _MAX_PRIVILEGE_ENUM; index++)
166 if (wcscmp(privilegeListTable[index].privilegeString, privilegeSubString.GetPointer()) == 0)
168 validStringFlag = true;
174 if (validStringFlag != true)
176 if (CheckExternalPrivilege(privilegeId))
178 validStringFlag = true;
182 SysTryReturnResult(NID_SEC, validStringFlag == true, E_INVALID_ARG, "Invalid privilege ID [%ls]", privilegeId.GetPointer());
183 __privilegeId = privilegeId;
189 _PrivilegeInfoImpl::GetId(void) const
191 return __privilegeId;
195 _PrivilegeInfoImpl::GetDisplayName(void) const
197 return GetName(__privilegeId);
201 _PrivilegeInfoImpl::GetDescription(void) const
203 return GetDescription(__privilegeId);
207 _PrivilegeInfoImpl::CreateStatementN(Database& db, const String& query)
209 result r = E_SUCCESS;
210 DbStatement* pStmt = null;
212 for (int i = 0; i < MAX_DATABASE_RETRY_COUNT; i++)
214 pStmt = db.CreateStatementN(query);
217 if (r != E_OBJECT_LOCKED)
223 SysLog(NID_SEC, "RetryCount[%d] CreateStatementN - E_OBJECT_LOCKED", i);
234 _PrivilegeInfoImpl::ExecuteStatementN(Database& db, const DbStatement* pStmt)
236 result r = E_SUCCESS;
237 DbEnumerator* pEnum = null;
239 for (int i = 0; i < MAX_DATABASE_RETRY_COUNT; i++)
241 pEnum = db.ExecuteStatementN(*pStmt);
244 if (r != E_OBJECT_LOCKED)
250 SysLog(NID_SEC, "RetryCount[%d] ExecuteStatementN - E_OBJECT_LOCKED", i);
260 } } // Tizen::Security