8709203f7c0e20041b6a9e83e2642c23fbece893
[platform/core/security/privacy-manager.git] / common / src / PrivacyDb.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 <sstream>
18 #include <dlog.h>
19 #include <Utils.h>
20 #include <PrivacyDb.h>
21 #include <PrivacyManagerTypes.h>
22 #include <sqlite3.h>
23 #include <pkgmgr-info.h>
24
25 std::mutex PrivacyDb::m_singletonMutex;
26 PrivacyDb* PrivacyDb::m_pInstance = NULL;
27
28 void
29 PrivacyDb::createDB(void)
30 {
31
32 }
33
34 bool PrivacyDb::isFilteredPackage(const std::string pkgId) const
35 {
36         pkgmgrinfo_pkginfo_h handle;
37
38         int res = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
39
40         if (res != PMINFO_R_OK)
41                 return false;
42
43         bool preloaded = false;
44         res = pkgmgrinfo_pkginfo_is_preload(handle, &preloaded);
45         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
46
47         return preloaded;
48 }
49
50 int
51 PrivacyDb::setPrivacySetting(const std::string pkgId, const std::string privacyId, bool enabled)
52 {
53         LOGI("enter");
54
55         static const std::string query = std::string("UPDATE PrivacyInfo set IS_ENABLED =? where PKG_ID=? and PRIVACY_ID=?");
56
57         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
58         prepareDb(pDbHandler, query.c_str(), pStmt);
59
60         int res = sqlite3_bind_int(pStmt.get(), 1, enabled);
61         TryReturn(res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
62
63         res = sqlite3_bind_text(pStmt.get(), 2, pkgId.c_str(), -1, SQLITE_TRANSIENT);
64         TryReturn(res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
65
66         res = sqlite3_bind_text(pStmt.get(), 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
67         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
68         
69         res = sqlite3_step(pStmt.get());
70         TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
71
72         LOGI("leave");
73
74         return 0;
75 }
76
77 int
78 PrivacyDb::getPrivacyAppPackages(std::list <std::string>& list) const
79 {
80         LOGI("enter");
81
82         std::string query = "SELECT PKG_ID from PackageInfo";
83
84         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READONLY);
85         prepareDb(pDbHandler, query.c_str(), pStmt);
86
87         while ( sqlite3_step(pStmt.get()) == SQLITE_ROW )
88         {
89                 const char* pValue =  reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
90
91                 LOGD("PkgId found : %s ", pValue);
92                 std::string pkgId = std::string(pValue);
93
94                 if (isFilteredPackage(pkgId))
95                 {
96                         LOGD("%s is Filtered", pValue);
97                         continue;
98                 }
99                 list.push_back(std::string(pValue));
100         }
101
102         LOGI("leave");
103
104         return 0;
105 }
106
107 int
108 PrivacyDb::getAppPackagePrivacyInfo(const std::string pkgId, std::list < std::pair < std::string, bool > >& privacyInfoList) const
109 {
110         LOGI("enter");
111
112         static const std::string query = "SELECT PRIVACY_ID, IS_ENABLED from PrivacyInfo where PKG_ID=?";
113
114         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READONLY);
115         prepareDb(pDbHandler, query.c_str(), pStmt);
116
117         int res = sqlite3_bind_text(pStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
118         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
119
120         while ( (res= sqlite3_step(pStmt.get())) == SQLITE_ROW )
121         {
122                 const char* privacyId =  reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
123                 bool privacyEnabled = sqlite3_column_int(pStmt.get(), 1) > 0 ? true : false;
124
125                 privacyInfoList.push_back( std::pair <std::string, bool> (std::string(privacyId), privacyEnabled) );
126
127                 LOGD("Privacy found : %s %d", privacyId, privacyEnabled);
128         }
129
130         LOGI("leave");
131
132         return 0;
133 }
134
135
136 int
137 PrivacyDb::addAppPackagePrivacyInfo(const std::string pkgId, const std::list < std::string > privilegeList)
138 {
139         LOGI("enter");
140
141         static const std::string pkgInfoQuery("INSERT INTO PackageInfo(PKG_ID, IS_SET) VALUES(?, ?)");
142         static const std::string privacyQuery("INSERT INTO PrivacyInfo(PKG_ID, PRIVACY_ID, IS_ENABLED) VALUES(?, ?, ?)");
143         
144         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
145         prepareDb(pDbHandler, pkgInfoQuery.c_str(), pPkgInfoStmt);
146
147         int res = sqlite3_bind_text(pPkgInfoStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
148         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
149         
150         res = sqlite3_bind_int(pPkgInfoStmt.get(), 2, 0);
151         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
152
153         res = sqlite3_step(pPkgInfoStmt.get());
154         TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
155         
156         for ( std::list <std::string>::const_iterator iter = privilegeList.begin(); iter != privilegeList.end(); ++iter)
157         {
158                 LOGD(" install privacy: %s", iter->c_str());
159                 prepareDb(pDbHandler, privacyQuery.c_str(), pPrivacyStmt);
160                 
161                 res = sqlite3_bind_text(pPrivacyStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
162                 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
163
164                 res = sqlite3_bind_text(pPrivacyStmt.get(), 2, iter->c_str(), -1, SQLITE_TRANSIENT);
165                 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
166                 
167                 // Before setting app and popup is ready, default value is true
168                 res = sqlite3_bind_int(pPrivacyStmt.get(), 3, 1);
169                 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
170
171                 res = sqlite3_step(pPrivacyStmt.get());
172                 TryReturn( res == SQLITE_DONE || res == SQLITE_CONSTRAINT, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
173
174                 sqlite3_reset(pPrivacyStmt.get());
175         }
176
177         return 0;
178 }
179
180 int
181 PrivacyDb::removeAppPackagePrivacyInfo(const std::string pkgId)
182 {
183         LOGI("enter");
184
185         static const std::string pkgInfoQuery("DELETE FROM PackageInfo WHERE PKG_ID=?");
186         static const std::string privacyQuery("DELETE FROM PrivacyInfo WHERE PKG_ID=?");
187
188         int res;
189
190         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
191         prepareDb(pDbHandler, pkgInfoQuery.c_str(), pPkgInfoStmt);
192
193         res = sqlite3_bind_text(pPkgInfoStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
194         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
195
196         res = sqlite3_step(pPkgInfoStmt.get());
197         TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
198
199         prepareDb(pDbHandler, privacyQuery.c_str(), pPrivacyStmt);
200
201         res = sqlite3_bind_text(pPrivacyStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
202         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res); 
203
204         res = sqlite3_step(pPrivacyStmt.get());
205         TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
206
207         return 0;
208 }
209
210 int
211 PrivacyDb::isUserPrompted(const std::string pkgId, bool& isPrompted) const
212 {
213         LOGI("enter");
214
215         static const std::string query = "SELECT IS_SET from PackageInfo where PKG_ID=?";
216
217         isPrompted = true;
218
219         if (isFilteredPackage(pkgId))
220         {
221                 LOGD("%s is Filtered", pkgId.c_str());
222                 return 0;
223         }
224
225         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READONLY);
226         prepareDb(pDbHandler, query.c_str(), pStmt);
227
228         int res = sqlite3_bind_text(pStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
229         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
230
231         std::unique_ptr < std::list < std::pair < std:: string, bool > > > pList ( new std::list < std::pair < std:: string, bool > >);
232
233         if ((res = sqlite3_step(pStmt.get())) == SQLITE_ROW)
234         {
235                 isPrompted = sqlite3_column_int(pStmt.get(), 0) > 0 ? true : false;
236         }
237         else
238         {
239                 LOGE("The package[%s] can not access privacy", pkgId.c_str());
240                 return PRIV_MGR_ERROR_SUCCESS;
241         }
242
243         return 0;
244 }
245
246 int
247 PrivacyDb::setUserPrompted(const std::string pkgId, bool prompted)
248 {
249         LOGI("enter");
250
251         std::string query = std::string("UPDATE PackageInfo set IS_SET =? where PKG_ID=?");
252
253         int res;
254
255         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
256         prepareDb(pDbHandler, query.c_str(), pStmt);
257
258         res = sqlite3_bind_int(pStmt.get(), 1, prompted? 1 : 0);
259         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
260
261         res = sqlite3_bind_text(pStmt.get(), 2, pkgId.c_str(), -1, SQLITE_TRANSIENT);
262         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
263
264         res = sqlite3_step(pStmt.get());
265         TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
266
267         LOGI("leave");
268
269         return 0;
270 }
271
272 int
273 PrivacyDb::getAppPackagesbyPrivacyId(std::string privacyId, std::list < std::pair < std::string, bool > >& list) const
274 {
275         LOGI("enter");
276
277         std::string sql = std::string("SELECT PKG_ID, IS_ENABLED from PrivacyInfo where PRIVACY_ID=?");
278
279         openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
280         prepareDb(pDbHandler, sql.c_str(), pStmt);
281
282         LOGD("privacy id : %s", privacyId.c_str());
283         int res = sqlite3_bind_text(pStmt.get(), 1, privacyId.c_str(), -1, SQLITE_TRANSIENT);
284         TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
285
286         while ( sqlite3_step(pStmt.get()) == SQLITE_ROW )
287         {
288                 
289                 const char* pPkgId =  reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
290                 bool isEnabled = sqlite3_column_int(pStmt.get(), 1) > 0 ? true : false;
291         std::string pkgId = std::string(pPkgId);
292                 if (isFilteredPackage(pkgId))
293                 {
294                         LOGD("%s is Filtered", pPkgId);
295                         continue;
296                 }
297
298                 list.push_back( std::pair <std::string, bool >(pkgId, isEnabled) );
299         }
300
301         LOGI("leave");
302
303         return PRIV_MGR_ERROR_SUCCESS;
304 }
305
306 PrivacyDb::PrivacyDb(void)
307 {
308
309 }
310
311 PrivacyDb::~PrivacyDb(void)
312 {
313
314 }
315
316 PrivacyDb*
317 PrivacyDb::getInstance(void)
318 {
319         LOGI("enter");
320         std::lock_guard < std::mutex > guard(m_singletonMutex);
321
322         if (m_pInstance == NULL)
323         {       
324                 m_pInstance = new PrivacyDb();
325         }
326
327         LOGI("leave");
328
329         return m_pInstance;
330 }