Fixed pkg-mgr plugin related things (install/uninstall callbacks)
[platform/core/security/privacy-guard.git] / common / src / PrivacyIdInfo.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <set>
18 #include <libintl.h>
19 #include <system_info.h>
20 #include "PrivacyIdInfo.h"
21 #include "privacy_guard_client_types.h"
22 #include "PrivacyGuardTypes.h"
23 #include "Utils.h"
24
25 std::map< std::string, std::string > PrivacyIdInfo::m_privilegeToPrivacyMap;
26 bool PrivacyIdInfo:: m_isInitialized;
27
28 int
29 PrivacyIdInfo::initialize(void)
30 {
31         static const std::string sqlPrivilege("SELECT PRIVILEGE_ID, PRIVACY_ID from PrivilegeToPrivacyTable");
32         static const std::string sqlPrivacyInfo("SELECT FEATURE FROM PrivacyInfo where PRIVACY_ID=?");
33
34         openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
35         prepareDb(pDbHandler, sqlPrivilege.c_str(), pStmtPrivilege);
36
37         int res;
38         while ((res = sqlite3_step(pStmtPrivilege.get())) == SQLITE_ROW)
39         {
40                 const char* privilegeId =  reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivilege.get(), 0));
41                 const char* privacyId =  reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivilege.get(), 1));
42
43                 prepareDb(pDbHandler, sqlPrivacyInfo.c_str(), pStmtPrivacyInfo);
44                 res = sqlite3_bind_text(pStmtPrivacyInfo.get(), 1, privacyId, -1, SQLITE_TRANSIENT);
45                 TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
46                 res = sqlite3_step(pStmtPrivacyInfo.get());
47                 PG_LOGD("privacy id : %s", privacyId);
48                 TryReturn(res == SQLITE_DONE || res == SQLITE_ROW, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
49
50                 const char* feature =  reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivacyInfo.get(), 0));
51                 if (feature != NULL)
52                 {
53                         bool isSupported = false;
54
55                         res = isFeatureEnabled(feature, isSupported);
56                         TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "isFeatureEnabled : %d", res);
57
58                         if (!isSupported)
59                         {
60                                 continue;
61                         }
62                 }
63
64                 m_privilegeToPrivacyMap.insert(std::map< std::string, std::string >::value_type(std::string(privilegeId), std::string(privacyId)));
65         }
66
67         m_isInitialized = true;
68
69         return PRIV_GUARD_ERROR_SUCCESS;
70 }
71
72 int
73 PrivacyIdInfo::getPrivacyIdFromPrivilege(const std::string privilege, std::string& privacyId)
74 {
75         if (!m_isInitialized)
76         {
77                 initialize();
78         }
79
80         std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.find(privilege);
81         if (iter == m_privilegeToPrivacyMap.end())
82         {
83                 return PRIV_GUARD_ERROR_NO_DATA;
84         }
85         privacyId = iter->second;
86
87         return PRIV_GUARD_ERROR_SUCCESS;
88 }
89
90 int
91 PrivacyIdInfo::getPrivilegeListFromPrivacyId(const std::string privacyId, std::list< std::string >& privilegeList)
92 {
93         if (!m_isInitialized)
94         {
95                 initialize();
96         }
97
98         privilegeList.clear();
99         for (std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.begin(); iter != m_privilegeToPrivacyMap.end(); ++iter)
100         {
101                 if (privacyId.compare((iter->second)) == 0)
102                 {
103                         privilegeList.push_back(iter->first);
104                 }
105         }
106
107         if (privilegeList.size() == 0)
108         {
109                 PG_LOGE("PrivilegeList of %s privacy is empty!", privacyId.c_str());
110                 return PRIV_GUARD_ERROR_NO_DATA;
111         }
112
113         return PRIV_GUARD_ERROR_SUCCESS;
114 }
115
116 int
117 PrivacyIdInfo::getPrivacyIdListFromPrivilegeList(const std::list< std::string > privilegeList, std::list< std::string >& privacyIdList)
118 {
119         if (!m_isInitialized)
120         {
121                 initialize();
122         }
123
124         privacyIdList.clear();
125
126         std::set< std::string > privacyIdSet;
127
128         for (std::list< std::string >::const_iterator iter = privilegeList.begin(); iter != privilegeList.end(); ++iter)
129         {
130                 std::string privacyId;
131                 int res = getPrivacyIdFromPrivilege(*iter, privacyId);
132                 if (res == PRIV_GUARD_ERROR_SUCCESS)
133                 {
134                         PG_LOGD("Privacy[%s] from Privilege[%s]", privacyId.c_str(), iter->c_str());
135                         privacyIdSet.insert(privacyId);
136                 }
137         }
138
139         for (std::set< std::string >::iterator iter = privacyIdSet.begin(); iter != privacyIdSet.end(); ++iter)
140         {
141                 privacyIdList.push_back(*iter);
142         }
143
144         return PRIV_GUARD_ERROR_SUCCESS;
145 }
146
147 bool
148 PrivacyIdInfo::isValidPrivacyId(const std::string privacyId)
149 {
150         if (!m_isInitialized) {
151                 initialize();
152         }
153
154         for (std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.begin(); iter != m_privilegeToPrivacyMap.end(); ++iter) {
155                 if (privacyId.compare((iter->second)) == 0) {
156                         return true;
157                 }
158         }
159
160         return false;
161 }
162
163 int
164 PrivacyIdInfo::getAllPrivacyId(std::list< std::string >& privacyIdList)
165 {
166         static const std::string sql("SELECT PRIVACY_ID, FEATURE from PrivacyInfo");
167
168         if (!m_isInitialized)
169         {
170                 initialize();
171         }
172
173         openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
174         prepareDb(pDbHandler, sql.c_str(), pStmt);
175
176         int res;
177         while ((res = sqlite3_step(pStmt.get())) == SQLITE_ROW)
178         {
179                 const char* privacyId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
180                 const char* feature = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 1));
181                 PG_LOGD("privacy: %s, feature: %s", privacyId, feature);
182
183                 if  (feature != NULL)
184                 {
185                         bool isSupported = false;
186                         res = isFeatureEnabled(feature, isSupported);
187                         TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "isFeatureEnabled : %d", res);
188                         if (!isSupported)
189                         {
190                                 continue;
191                         }
192                 }
193
194                 privacyIdList.push_back(std::string(privacyId));
195                 PG_LOGD("privacy Id : %s", privacyId);
196         }
197
198         return PRIV_GUARD_ERROR_SUCCESS;
199 }
200
201 int
202 PrivacyIdInfo::getPrivaycDisplayName(const std::string privacyId, std::string& displayName)
203 {
204         if (!m_isInitialized)
205         {
206                 initialize();
207         }
208
209         std::string sql = std::string("SELECT STR_MODULE_ID, STR_NAME_ID from PrivacyInfo where PRIVACY_ID=?");
210
211         openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
212         prepareDb(pDbHandler, sql.c_str(), pStmt);
213
214         int res = sqlite3_bind_text(pStmt.get(), 1, privacyId.c_str(), -1, SQLITE_TRANSIENT);
215         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
216
217         if (sqlite3_step(pStmt.get()) == SQLITE_ROW)
218         {
219                 const char* pModuleId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
220                 const char* pNameId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 1));
221
222                 if (pNameId == NULL)
223                 {
224                         displayName = privacyId;
225                 }
226                 else
227                 {
228                         displayName = std::string(dgettext(pModuleId, pNameId));
229                 }
230         }
231         else
232         {
233                 PG_LOGI("Cannot find privacy string %s ", privacyId.c_str());
234                 return PRIV_GUARD_ERROR_NO_DATA;
235         }
236
237         return PRIV_GUARD_ERROR_SUCCESS;
238 }
239
240 int
241 PrivacyIdInfo::getPrivaycDescription(const std::string privacyId, std::string& displayName)
242 {
243         if (!m_isInitialized)
244         {
245                 initialize();
246         }
247
248         std::string sql = std::string("SELECT STR_MODULE_ID, STR_NAME_ID from PrivacyInfo where PRIVACY_ID=?");
249
250         openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
251         prepareDb(pDbHandler, sql.c_str(), pStmt);
252
253         int res = sqlite3_bind_text(pStmt.get(), 1, privacyId.c_str(), -1, SQLITE_TRANSIENT);
254         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
255
256         if (sqlite3_step(pStmt.get()) == SQLITE_ROW)
257         {
258                 const char* pModuleId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
259                 const char* pNameId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 1));
260
261                 displayName = std::string(dgettext(pModuleId, pNameId));
262         }
263         else
264         {
265                 PG_LOGI("Cannot find privacy string %s ", privacyId.c_str());
266                 return PRIV_GUARD_ERROR_NO_DATA;
267         }
268
269         return PRIV_GUARD_ERROR_SUCCESS;
270 }
271
272 int
273 PrivacyIdInfo::isFeatureEnabled(const char* feature, bool& enabled)
274 {
275         int res = PRIV_GUARD_ERROR_SUCCESS;
276
277         if (feature == NULL)
278         {
279                 enabled = true;
280                 return res;
281         }
282
283         res = system_info_get_platform_bool(feature, &enabled);
284         TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "system_info_get_platform_bool : %d", res);
285
286         return PRIV_GUARD_ERROR_SUCCESS;
287 }