Add Privacy-Guard
[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 <dlog.h>
18 #include <set>
19 #include <libintl.h>
20 #include <system_info.h>
21 #include "PrivacyIdInfo.h"
22 #include "privacy_guard_client_types.h"
23 #include "PrivacyGuardTypes.h"
24 #include "Utils.h"
25
26 std::map< std::string, std::string > PrivacyIdInfo::m_privilegeToPrivacyMap;
27 bool PrivacyIdInfo:: m_isInitialized;
28
29 int
30 PrivacyIdInfo::initialize(void)
31 {
32         static const std::string sqlPrivilege("SELECT PRIVILEGE_ID, PRIVACY_ID from PrivilegeToPrivacyTable");
33         static const std::string sqlPrivacyInfo("SELECT FEATURE FROM PrivacyInfo where PRIVACY_ID=?");
34
35         openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
36         prepareDb(pDbHandler, sqlPrivilege.c_str(), pStmtPrivilege);
37
38         int res;
39         while ((res = sqlite3_step(pStmtPrivilege.get())) == SQLITE_ROW)
40         {
41                 const char* privilegeId =  reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivilege.get(), 0));
42                 const char* privacyId =  reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivilege.get(), 1));
43
44                 prepareDb(pDbHandler, sqlPrivacyInfo.c_str(), pStmtPrivacyInfo);
45                 res = sqlite3_bind_text(pStmtPrivacyInfo.get(), 1, privacyId, -1, SQLITE_TRANSIENT);
46                 TryReturn(res == SQLITE_OK, PRIV_FLTR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
47                 res = sqlite3_step(pStmtPrivacyInfo.get());
48                 LOGD("privacy id : %s", privacyId);
49                 TryReturn(res == SQLITE_DONE || res == SQLITE_ROW, PRIV_FLTR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
50
51                 const char* feature =  reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivacyInfo.get(), 0));
52                 if (feature != NULL)
53                 {
54                         bool isSupported = false;
55
56                         res = isFeatureEnabled(feature, isSupported);
57                         TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "isFeatureEnabled : %d", res);
58
59                         if (!isSupported)
60                         {
61                                 continue;
62                         }
63                 }
64
65                 m_privilegeToPrivacyMap.insert(std::map< std::string, std::string >::value_type(std::string(privilegeId), std::string(privacyId)));
66         }
67
68         m_isInitialized = true;
69
70         return PRIV_FLTR_ERROR_SUCCESS;
71 }
72
73 int
74 PrivacyIdInfo::getPrivacyIdFromPrivilege(const std::string privilege, std::string& privacyId)
75 {
76         if (!m_isInitialized)
77         {
78                 initialize();
79         }
80
81         std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.find(privilege);
82         if (iter == m_privilegeToPrivacyMap.end())
83         {
84                 return PRIV_FLTR_ERROR_NO_DATA;
85         }
86         privacyId = iter->second;
87
88         return PRIV_FLTR_ERROR_SUCCESS;
89 }
90
91 int
92 PrivacyIdInfo::getPrivilegeListFromPrivacyId(const std::string privacyId, std::list< std::string >& privilegeList)
93 {
94         if (!m_isInitialized)
95         {
96                 initialize();
97         }
98
99         privilegeList.clear();
100         for (std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.begin(); iter != m_privilegeToPrivacyMap.end(); ++iter)
101         {
102                 if (privacyId.compare((iter->second)) == 0)
103                 {
104                         privilegeList.push_back(iter->first);
105                 }
106         }
107
108         if (privilegeList.size() == 0)
109         {
110                 LOGE("PrivilegeList of %s privacy is empty!", privacyId.c_str());
111                 return PRIV_FLTR_ERROR_NO_DATA;
112         }
113
114         return PRIV_FLTR_ERROR_SUCCESS;
115 }
116
117 int
118 PrivacyIdInfo::getPrivacyIdListFromPrivilegeList(const std::list< std::string > privilegeList, std::list< std::string >& privacyIdList)
119 {
120         if (!m_isInitialized)
121         {
122                 initialize();
123         }
124
125         privacyIdList.clear();
126
127         std::set< std::string > privacyIdSet;
128
129         for (std::list< std::string >::const_iterator iter = privilegeList.begin(); iter != privilegeList.end(); ++iter)
130         {
131                 std::string privacyId;
132                 int res = getPrivacyIdFromPrivilege(*iter, privacyId);
133                 if (res == PRIV_FLTR_ERROR_SUCCESS)
134                 {
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_FLTR_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                 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_FLTR_ERROR_SUCCESS, res, , "isFeatureEnabled : %d", res);
188                         if (!isSupported)
189                         {
190                                 continue;
191                         }
192                 }
193
194                 privacyIdList.push_back(std::string(privacyId));
195                 SECURE_LOGD(" privacy Id : %s", privacyId);
196         }
197
198         return PRIV_FLTR_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_FLTR_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                 LOGI("Cannot find privacy string %s ", privacyId.c_str());
234                 return PRIV_FLTR_ERROR_NO_DATA;
235         }
236
237         return PRIV_FLTR_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_FLTR_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                 LOGI("Cannot find privacy string %s ", privacyId.c_str());
266                 return PRIV_FLTR_ERROR_NO_DATA;
267         }
268
269         return PRIV_FLTR_ERROR_SUCCESS;
270 }
271
272 int
273 PrivacyIdInfo::isFeatureEnabled(const char* feature, bool& enabled)
274 {
275         int res = PRIV_FLTR_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_FLTR_ERROR_SUCCESS, PRIV_FLTR_ERROR_SYSTEM_ERROR, , "system_info_get_platform_bool : %d", res);
285
286         return PRIV_FLTR_ERROR_SUCCESS;
287 }