Fixed pkg-mgr plugin related things (install/uninstall callbacks)
[platform/core/security/privacy-guard.git] / server / src / PrivacyGuardDb.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 <fstream>
19 #include <sqlite3.h>
20 #include <pkgmgr-info.h>
21 #include <time.h>
22 #include "Utils.h"
23 #include "PrivacyGuardDb.h"
24 #include "PrivacyIdInfo.h"
25 #if 0
26 // [CYNARA]
27 #include "CynaraService.h"
28
29 //static cynara_monitor_configuration *p_conf;
30 static cynara_monitor *p_cynara_monitor;
31 static cynara_monitor_entry **monitor_entries;
32 #endif
33
34 std::mutex PrivacyGuardDb::m_singletonMutex;
35 PrivacyGuardDb* PrivacyGuardDb::m_pInstance = NULL;
36
37 static const char* privacy_list[5] = { "http://tizen.org/privacy/location",
38                                                                  "http://tizen.org/privacy/contact",
39                                                                  "http://tizen.org/privacy/calendar",
40                                                                  "http://tizen.org/privacy/messaging",
41                                                                  "http://tizen.org/privacy/callhistory" };
42
43 #ifdef __FILTER_LISTED_PKG
44 const std::string PrivacyGuardDb::PRIVACY_FILTER_LIST_FILE = std::string("/usr/share/privacy-guard/privacy-guard-list.ini");
45 const std::string PrivacyGuardDb::FILTER_KEY = std::string("package_id");
46 std::map < std::string, bool > PrivacyGuardDb::m_filteredPkgList;
47 #endif
48
49 void
50 PrivacyGuardDb::createDB(void)
51 {
52
53 }
54
55 void
56 PrivacyGuardDb::openSqliteDB(void)
57 {
58         int res = -1;
59         res = sqlite3_open_v2(PRIVACY_DB_PATH, &m_sqlHandler, SQLITE_OPEN_READWRITE, NULL);
60         if(res == SQLITE_OK)    {
61                 PG_LOGI("monitor db is opened successfully");
62 //              sqlite3_wal_autocheckpoint(m_sqlHandler, 1);
63                 m_bDBOpen = true;
64         }
65         else {
66                 PG_LOGE("fail : monitor db open(%d)", res);
67         }
68 }
69
70 int
71 PrivacyGuardDb::PgAddPrivacyAccessLog(const int userId, std::list < std::pair < std::string, std::string > > logInfoList)
72 {
73         time_t current_date;
74         struct tm tm;
75         current_date = time(NULL);
76         localtime_r(&current_date, &tm);
77
78         if(current_date <= 0) {
79                 return PRIV_GUARD_ERROR_INVALID_PARAMETER;
80         }
81
82         int res = -1;
83
84         static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
85
86         m_dbMutex.lock();
87         // open db
88         if(m_bDBOpen == false) {
89                 openSqliteDB();
90         }
91         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
92
93         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
94
95         // prepare
96         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
97         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
98
99         for (std::list <std::pair <std::string, std::string>>::iterator iter = logInfoList.begin(); iter != logInfoList.end(); ++iter) {
100                 PG_LOGD("packageID : %s, PrivacyID : %s", iter->first.c_str(), iter->second.c_str());
101
102                 // bind
103                 res = sqlite3_bind_int(m_stmt, 1, userId);
104                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
105
106                 res = sqlite3_bind_text(m_stmt, 2, iter->first.c_str(), -1, SQLITE_TRANSIENT);
107                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
108
109                 res = sqlite3_bind_text(m_stmt, 3, iter->second.c_str(), -1, SQLITE_TRANSIENT);
110                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
111
112                 res = sqlite3_bind_int(m_stmt, 4, current_date);
113                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
114
115                 res = sqlite3_step(m_stmt);
116                 TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
117
118                 sqlite3_reset(m_stmt);
119         }
120         m_dbMutex.unlock();
121
122         return PRIV_GUARD_ERROR_SUCCESS;
123 }
124
125 int
126 PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::string packageId, const std::string privilege, const timespec* timestamp)
127 {
128         if(timestamp->tv_sec <= 0) {
129                 return PRIV_GUARD_ERROR_INVALID_PARAMETER;
130         }
131
132         int res = SQLITE_OK;
133
134         // change from privilege to privacy
135         std::string privacyId;
136         res = PrivacyIdInfo::getPrivacyIdFromPrivilege(privilege, privacyId);
137         if (res == PRIV_GUARD_ERROR_NO_DATA) {
138                 return PRIV_GUARD_ERROR_SUCCESS;
139         }
140         TryReturn( res == PRIV_GUARD_ERROR_SUCCESS, res, , "getPrivacyIdFromPrivilege : %d", res);
141
142         // change from timespec to time_t
143         time_t logging_date;
144         logging_date = timestamp->tv_sec;
145
146         static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
147
148         m_dbMutex.lock();
149         // open db
150         if(m_bDBOpen == false) {
151                 openSqliteDB();
152         }
153         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
154
155         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
156
157         // prepare
158         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
159         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
160
161         // bind
162         res = sqlite3_bind_int(m_stmt, 1, userId);
163         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
164
165         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
166         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
167
168         res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
169         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
170
171         res = sqlite3_bind_int(m_stmt, 4, logging_date);
172         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
173
174         res = sqlite3_step(m_stmt);
175         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
176
177         sqlite3_reset(m_stmt);
178
179         m_dbMutex.unlock();
180
181         return PRIV_GUARD_ERROR_SUCCESS;
182 }
183
184 int
185 PrivacyGuardDb::PgAddPrivacyAccessLogTest(const int userId, const std::string packageId, const std::string privacyId)
186 {
187         time_t current_date;
188         struct tm tm;
189         current_date = time(NULL);
190         localtime_r(&current_date, &tm);
191
192         if(current_date <= 0) {
193                 return PRIV_GUARD_ERROR_INVALID_PARAMETER;
194         }
195
196         int res = SQLITE_OK;
197
198         static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
199
200         m_dbMutex.lock();
201         // open db
202         if(m_bDBOpen == false) {
203                 openSqliteDB();
204         }
205         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
206
207         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
208
209         // prepare
210         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
211         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
212
213         // bind
214         res = sqlite3_bind_int(m_stmt, 1, userId);
215         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
216
217         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
218         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
219
220         res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
221         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
222
223         res = sqlite3_bind_int(m_stmt, 4, current_date);
224         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
225
226         res = sqlite3_step(m_stmt);
227         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
228
229         sqlite3_reset(m_stmt);
230
231         m_dbMutex.unlock();
232
233         return PRIV_GUARD_ERROR_SUCCESS;
234 }
235
236
237 int
238 PrivacyGuardDb::PgAddMonitorPolicy(const int userId, const std::string packageId, const std::list < std::string > privacyList, bool monitorPolicy)
239 {
240         int res = -1;
241
242         static const std::string QUERY_INSERT = std::string("INSERT INTO MonitorPolicy(USER_ID, PKG_ID, PRIVACY_ID, MONITOR_POLICY) VALUES(?, ?, ?, ?)");
243
244         m_dbMutex.lock();
245         // open db
246         if(m_bDBOpen == false) {
247                 openSqliteDB();
248         }
249         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
250
251         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
252
253         // prepare
254         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
255         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
256
257         for (std::list <std::string>::const_iterator iter = privacyList.begin(); iter != privacyList.end(); ++iter) {
258                 PG_LOGD("PrivacyID : %s", iter->c_str());
259
260                 // bind
261                 res = sqlite3_bind_int(m_stmt, 1, userId);
262                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
263
264                 res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
265                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
266
267                 res = sqlite3_bind_text(m_stmt, 3, iter->c_str(), -1, SQLITE_TRANSIENT);
268                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
269
270                 res = sqlite3_bind_int(m_stmt, 4, monitorPolicy);
271                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
272
273                 res = sqlite3_step(m_stmt);
274                 TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
275
276                 sqlite3_reset(m_stmt);
277         }
278         m_dbMutex.unlock();
279
280         return PRIV_GUARD_ERROR_SUCCESS;
281 }
282
283 int
284 PrivacyGuardDb::PgCheckPrivacyPackage(const int userId, const std::string packageId, bool &isPrivacyPackage)
285 {
286         int res = -1;
287         static const std::string query = std::string("SELECT COUNT(*) FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=?");
288
289         m_dbMutex.lock();
290         // open db
291         if(m_bDBOpen == false) {
292                 openSqliteDB();
293         }
294         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
295
296         // prepare
297         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
298         TryCatchResLogReturn( res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
299
300         // bind
301         res = sqlite3_bind_int(m_stmt, 1, userId);
302         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
303
304         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
305         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
306
307         int count = -1;
308
309         // step
310         if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
311                 count = sqlite3_column_int(m_stmt, 0);
312         }
313         m_dbMutex.unlock();
314
315         if (count > 0) {
316                 isPrivacyPackage = true;
317         }
318         else {
319                 isPrivacyPackage = false;
320         }
321
322         return PRIV_GUARD_ERROR_SUCCESS;
323 }
324
325 int
326 PrivacyGuardDb::PgDeleteAllLogsAndMonitorPolicy(void)
327 {
328         PG_LOGD("PgDeleteAllLogsAndMonitorPolicy");
329
330         int res = -1;
331
332         static const std::string LOG_DELETE = std::string("DELETE FROM StatisticsMonitorInfo");
333         static const std::string POLICY_DELETE = std::string("DELETE FROM MonitorPolicy");
334         static const std::string MAIN_POLICY_DELETE = std::string("DELETE FROM MainMonitorPolicy");
335
336         m_dbMutex.lock();
337         // open db
338         if(m_bDBOpen == false) {
339                 openSqliteDB();
340         }
341         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
342
343         // prepare
344         res = sqlite3_prepare_v2(m_sqlHandler, LOG_DELETE.c_str(), -1, &m_stmt, NULL);
345         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
346
347         res = sqlite3_step(m_stmt);
348         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
349
350         res = sqlite3_prepare_v2(m_sqlHandler, POLICY_DELETE.c_str(), -1, &m_stmt, NULL);
351         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
352
353         res = sqlite3_step(m_stmt);
354         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
355
356         res = sqlite3_prepare_v2(m_sqlHandler, MAIN_POLICY_DELETE.c_str(), -1, &m_stmt, NULL);
357         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
358
359         res = sqlite3_step(m_stmt);
360         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
361
362         m_dbMutex.unlock();
363
364         return PRIV_GUARD_ERROR_SUCCESS;
365 }
366
367
368 int
369 PrivacyGuardDb::PgDeleteLogsByPackageId(const std::string packageId)
370 {
371         PG_LOGD("PrivacyGuardDb::PgDeleteLogsByPackageId packageid : %s", packageId.c_str());
372
373         int res = -1;
374
375         static const std::string QUERY_DELETE = std::string("DELETE FROM StatisticsMonitorInfo WHERE PKG_ID=?");
376
377         m_dbMutex.lock();
378         // open db
379         if(m_bDBOpen == false) {
380                 openSqliteDB();
381         }
382         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
383
384         // prepare
385         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_DELETE.c_str(), -1, &m_stmt, NULL);
386         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
387
388         // bind
389         res = sqlite3_bind_text(m_stmt, 1, packageId.c_str(), -1, SQLITE_TRANSIENT);
390         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
391
392         // step
393         res = sqlite3_step(m_stmt);
394         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
395
396         m_dbMutex.unlock();
397
398         return PRIV_GUARD_ERROR_SUCCESS;
399 }
400
401 int
402 PrivacyGuardDb::PgDeleteMonitorPolicyByPackageId(const std::string packageId)
403 {
404         PG_LOGD("PrivacyGuardDb::PgDeleteMonitorPolicyByPackageId packageid : %s", packageId.c_str());
405
406         int res = -1;
407
408         static const std::string QUERY_DELETE = std::string("DELETE FROM MonitorPolicy WHERE PKG_ID=?");
409
410         m_dbMutex.lock();
411         // open db
412         if(m_bDBOpen == false) {
413                 openSqliteDB();
414         }
415         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
416
417         // prepare
418         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_DELETE.c_str(), -1, &m_stmt, NULL);
419         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
420
421         // bind
422         res = sqlite3_bind_text(m_stmt, 1, packageId.c_str(), -1, SQLITE_TRANSIENT);
423         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
424
425         // step
426         res = sqlite3_step(m_stmt);
427         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
428
429         m_dbMutex.unlock();
430
431         return PRIV_GUARD_ERROR_SUCCESS;
432 }
433
434 int
435 PrivacyGuardDb::PgForeachTotalPrivacyCountOfPackage(const int userId, const int startDate, const int endDate,
436                 std::list < std::pair < std::string, int > >& packageInfoList)
437 {
438         int res = -1;
439
440 #if 0
441         // [CYNARA] Fluch Entries
442         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
443         if(ret != CYNARA_API_SUCCESS){
444                 PG_LOGE("cynara_monitor_entries_flush FAIL");
445                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
446         }
447
448         // [CYNARA] Get Entries
449         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
450         if(ret != CYNARA_API_SUCCESS){
451                 PG_LOGE("cynara_monitor_entries_get FAIL");
452                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
453         }
454
455         // [CYNARA] Update DB
456         ret = CynaraService::updateDb(monitor_entries);
457         if(ret != PRIV_GUARD_ERROR_SUCCESS){
458                 PG_LOGE("updateDb FAIL");
459                 return ret;
460         }
461 #endif
462
463         static const std::string PKGID_SELECT = std::string("SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE USER_ID=? AND USE_DATE>=? AND USE_DATE<=?");
464         static const std::string PKGINFO_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND USE_DATE>=? AND USE_DATE<=?");
465         sqlite3_stmt* infoStmt;
466
467         m_dbMutex.lock();
468         // open db
469         if(m_bDBOpen == false) {
470                 openSqliteDB();
471         }
472         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
473
474         // prepare
475         res = sqlite3_prepare_v2(m_sqlHandler, PKGID_SELECT.c_str(), -1, &m_stmt, NULL);
476         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
477
478         res = sqlite3_bind_int(m_stmt, 1, userId);
479         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
480
481         res = sqlite3_bind_int(m_stmt, 2, startDate);
482         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
483
484         res = sqlite3_bind_int(m_stmt, 3, endDate);
485         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
486
487         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
488                 const char* packageId = reinterpret_cast < const char* > (sqlite3_column_text(m_stmt, 0));
489                 if(packageId == NULL) { continue; }
490
491                 // prepare
492                 res = sqlite3_prepare_v2(m_sqlHandler, PKGINFO_SELECT.c_str(), -1, &infoStmt, NULL);
493                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
494
495                 res = sqlite3_bind_int(infoStmt, 1, userId);
496                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
497
498                 res = sqlite3_bind_text(infoStmt, 2, packageId, -1, SQLITE_TRANSIENT);
499                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
500
501                 res = sqlite3_bind_int(infoStmt, 3, startDate);
502                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
503
504                 res = sqlite3_bind_int(infoStmt, 4, endDate);
505                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
506
507                 while ((res = sqlite3_step(infoStmt)) == SQLITE_ROW) {
508                         int count = sqlite3_column_int(infoStmt, 0);
509                         if (count == 0) {
510                                 continue;
511                         }
512                         packageInfoList.push_back(std::pair <std::string, int> (std::string(packageId), count));
513                 }
514                 sqlite3_reset(infoStmt);
515         }
516         m_dbMutex.unlock();
517
518         return PRIV_GUARD_ERROR_SUCCESS;
519 }
520
521 int
522 PrivacyGuardDb::PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int startDate, const int endDate,
523                 std::list < std::pair < std::string, int > >& privacyInfoList)
524 {
525         int res = -1;
526
527 #if 0
528         // [CYNARA] Fluch Entries
529         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
530         if(ret != CYNARA_API_SUCCESS){
531                 PG_LOGE("cynara_monitor_entries_flush FAIL");
532                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
533         }
534
535         // [CYNARA] Get Entries
536         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
537         if(ret != CYNARA_API_SUCCESS){
538                 PG_LOGE("cynara_monitor_entries_get FAIL");
539                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
540         }
541
542         // [CYNARA] Update DB
543         ret = CynaraService::updateDb(monitor_entries);
544         if(ret != PRIV_GUARD_ERROR_SUCCESS){
545                 PG_LOGE("updateDb FAIL");
546                 return ret;
547         }
548 #endif
549
550         static const std::string PRIVACY_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
551
552         m_dbMutex.lock();
553         // open db
554         if(m_bDBOpen == false) {
555                 openSqliteDB();
556         }
557         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
558
559         int i;
560         int cnt_privacy = sizeof(privacy_list) / sizeof(privacy_list[0]);
561
562         for (i = 0; i < cnt_privacy; i++) {
563                 // prepare
564                 res = sqlite3_prepare_v2(m_sqlHandler, PRIVACY_SELECT.c_str(), -1, &m_stmt, NULL);
565                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
566
567                 // bind
568                 res = sqlite3_bind_int(m_stmt, 1, userId);
569                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
570
571                 res = sqlite3_bind_text(m_stmt, 2, privacy_list[i], -1, SQLITE_TRANSIENT);
572                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
573
574                 res = sqlite3_bind_int(m_stmt, 3, startDate);
575                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
576
577                 res = sqlite3_bind_int(m_stmt, 4, endDate);
578                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
579
580                 // step
581                 if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
582                         int count = sqlite3_column_int(m_stmt, 0);
583                         if (count == 0) {
584                                 continue;
585                         }
586                         const char* privacyId = privacy_list[i];
587                         privacyInfoList.push_back(std::pair <std::string, int> (std::string(privacyId), count));
588                 }
589                 sqlite3_reset(m_stmt);
590         }
591
592         m_dbMutex.unlock();
593
594         return PRIV_GUARD_ERROR_SUCCESS;
595 }
596
597 int
598 PrivacyGuardDb::PgForeachPrivacyCountByPrivacyId(const int userId, const int startDate, const int endDate,
599                 const std::string privacyId, std::list < std::pair < std::string, int > >& packageInfoList)
600 {
601         int res = -1;
602
603 #if 0
604         // [CYNARA] Fluch Entries
605         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
606         if(ret != CYNARA_API_SUCCESS){
607                 PG_LOGE("cynara_monitor_entries_flush FAIL");
608                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
609         }
610
611         // [CYNARA] Get Entries
612         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
613         if(ret != CYNARA_API_SUCCESS){
614                 PG_LOGE("cynara_monitor_entries_get FAIL");
615                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
616         }
617
618         // [CYNARA] Update DB
619         ret = CynaraService::updateDb(monitor_entries);
620         if(ret != PRIV_GUARD_ERROR_SUCCESS){
621                 PG_LOGE("updateDb FAIL");
622                 return ret;
623         }
624 #endif
625
626         static const std::string PKGID_SELECT = std::string("SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE USER_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
627         static const std::string PKGINFO_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
628         sqlite3_stmt* infoStmt;
629
630         m_dbMutex.lock();
631         // open db
632         if(m_bDBOpen == false) {
633                 openSqliteDB();
634         }
635         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
636
637         // prepare
638         res = sqlite3_prepare_v2(m_sqlHandler, PKGID_SELECT.c_str(), -1, &m_stmt, NULL);
639         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
640
641         // bind
642         res = sqlite3_bind_int(m_stmt, 1, userId);
643         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
644
645         res = sqlite3_bind_text(m_stmt, 2, privacyId.c_str(), -1, SQLITE_TRANSIENT);
646         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
647
648         res = sqlite3_bind_int(m_stmt, 3, startDate);
649         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
650
651         res = sqlite3_bind_int(m_stmt, 4, endDate);
652         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
653
654         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
655                 const char* packageId =  reinterpret_cast < const char* > (sqlite3_column_text(m_stmt, 0));
656                 if(packageId == NULL) { continue; }
657
658                 // prepare
659                 res = sqlite3_prepare_v2(m_sqlHandler, PKGINFO_SELECT.c_str(), -1, &infoStmt, NULL);
660                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
661
662                 // bind
663                 res = sqlite3_bind_int(infoStmt, 1, userId);
664                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
665
666                 res = sqlite3_bind_text(infoStmt, 2, packageId, -1, SQLITE_TRANSIENT);
667                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
668
669                 res = sqlite3_bind_text(infoStmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
670                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
671
672                 res = sqlite3_bind_int(infoStmt, 4, startDate);
673                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
674
675                 res = sqlite3_bind_int(infoStmt, 5, endDate);
676                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
677
678                 if ((res = sqlite3_step(infoStmt)) == SQLITE_ROW) {
679                         int count = sqlite3_column_int(infoStmt, 0);
680                         if (count == 0) {
681                                 continue;
682                         }
683                         packageInfoList.push_back(std::pair <std::string, int> (std::string(packageId), count));
684                 }
685                 sqlite3_reset(infoStmt);
686         }
687
688         m_dbMutex.unlock();
689
690         return PRIV_GUARD_ERROR_SUCCESS;
691 }
692
693 int
694 PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int startDate, const int endDate,
695                 const std::string packageId, std::list < std::pair < std::string, int > >& privacyInfoList)
696 {
697         int res = -1;
698
699 #if 0
700         // [CYNARA] Fluch Entries
701         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
702         if(ret != CYNARA_API_SUCCESS){
703                 PG_LOGE("cynara_monitor_entries_flush FAIL");
704                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
705         }
706
707         // [CYNARA] Get Entries
708         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
709         if(ret != CYNARA_API_SUCCESS){
710                 PG_LOGE("cynara_monitor_entries_get FAIL");
711                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
712         }
713
714         // [CYNARA] Update DB
715         ret = CynaraService::updateDb(monitor_entries);
716         if(ret != PRIV_GUARD_ERROR_SUCCESS){
717                 PG_LOGE("updateDb FAIL");
718                 return ret;
719         }
720 #endif
721
722         static const std::string PRIVACY_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
723
724         m_dbMutex.lock();
725         // open db
726         if(m_bDBOpen == false) {
727                 openSqliteDB();
728         }
729         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
730
731         int i;
732         int cnt_privacy = sizeof(privacy_list) / sizeof(privacy_list[0]);
733
734         for (i = 0; i < cnt_privacy; i++) {
735                 // prepare
736                 res = sqlite3_prepare_v2(m_sqlHandler, PRIVACY_SELECT.c_str(), -1, &m_stmt, NULL);
737                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
738
739                 //bind
740                 res = sqlite3_bind_int(m_stmt, 1, userId);
741                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
742
743                 res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
744                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
745
746                 res = sqlite3_bind_text(m_stmt, 3, privacy_list[i], -1, SQLITE_TRANSIENT);
747                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
748
749                 res = sqlite3_bind_int(m_stmt, 4, startDate);
750                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
751
752                 res = sqlite3_bind_int(m_stmt, 5, endDate);
753                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
754
755                 if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
756                         int count = sqlite3_column_int(m_stmt, 0);
757                         if (count == 0) {
758                                 continue;
759                         }
760                         const char* privacyId = privacy_list[i];
761                         privacyInfoList.push_back(std::pair <std::string, int> (std::string(privacyId), count));
762                 }
763                 sqlite3_reset(m_stmt);
764         }
765
766         m_dbMutex.unlock();
767
768         return PRIV_GUARD_ERROR_SUCCESS;
769 }
770
771 int
772 PrivacyGuardDb::PgGetMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int& monitorPolicy)
773 {
774
775         int res = -1;
776         static const std::string query = std::string("SELECT MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=?");
777
778         m_dbMutex.lock();
779         // open db
780         if(m_bDBOpen == false) {
781                 openSqliteDB();
782         }
783         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
784
785         // prepare
786         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
787         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
788
789         // bind
790         res = sqlite3_bind_int(m_stmt, 1, userId);
791         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
792
793         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
794         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
795
796         res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
797         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
798
799         // step
800         monitorPolicy = 0;
801         if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
802                 monitorPolicy = sqlite3_column_int(m_stmt, 0);
803         }
804         m_dbMutex.unlock();
805
806         return PRIV_GUARD_ERROR_SUCCESS;
807 }
808
809 int
810 PrivacyGuardDb::PgGetAllMonitorPolicy(std::list < std::pair < std::string, int > >& monitorPolicyList)
811 {
812         int res = -1;
813
814         static const std::string MONITOR_POLICY_SELECT = std::string("SELECT * FROM MonitorPolicy");
815
816         m_dbMutex.lock();
817         // open db
818         if(m_bDBOpen == false) {
819                 openSqliteDB();
820         }
821         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
822
823         // prepare
824         res = sqlite3_prepare_v2(m_sqlHandler, MONITOR_POLICY_SELECT.c_str(), -1, &m_stmt, NULL);
825         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
826
827         // step
828         int monitorPolicy = 0;
829         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
830                 int userId = sqlite3_column_int(m_stmt, 0);
831                 char* tmpPkgId = (char*)sqlite3_column_text(m_stmt, 1);
832                 char* tmpPrivacyId = (char*)sqlite3_column_text(m_stmt, 2);
833                 if(tmpPkgId == NULL || tmpPrivacyId == NULL) {
834                         continue;
835                 }
836                 std::string userPkgIdPrivacyId = std::to_string(userId);
837                 userPkgIdPrivacyId.append("|").append(std::string(tmpPkgId));
838                 userPkgIdPrivacyId.append("|").append(std::string(tmpPrivacyId));
839                 monitorPolicy = sqlite3_column_int(m_stmt, 3);
840                 monitorPolicyList.push_back(std::pair < std::string, int > (userPkgIdPrivacyId, monitorPolicy));
841         }
842
843         m_dbMutex.unlock();
844         if(monitorPolicyList.size() > 0) {
845                 res = PRIV_GUARD_ERROR_SUCCESS;
846         }
847         else {
848                 res = PRIV_GUARD_ERROR_NO_DATA;
849         }
850
851         return res;
852 }
853
854 int
855 PrivacyGuardDb::PgForeachMonitorPolicyByPackageId(const int userId, const std::string packageId, std::list <privacy_data_s>& privacyInfoList)
856 {
857         int res = -1;
858         static const std::string query = std::string("SELECT DISTINCT PRIVACY_ID, MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=?");
859
860         m_dbMutex.lock();
861         // open db
862         if(m_bDBOpen == false) {
863                 openSqliteDB();
864         }
865         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
866
867         // prepare
868         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
869         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
870
871         // bind
872         res = sqlite3_bind_int(m_stmt, 1, userId);
873         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
874
875         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
876         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
877
878         // step
879         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
880
881                 char* tmp_data = (char*)sqlite3_column_text(m_stmt, 0);
882                 if(tmp_data == NULL) {
883                         continue;
884                 }
885                 privacy_data_s p_data;
886                 p_data.privacy_id = strdup(tmp_data);
887                 p_data.monitor_policy= sqlite3_column_int(m_stmt, 1);
888
889                 privacyInfoList.push_back(p_data);
890         }
891         m_dbMutex.unlock();
892
893         return PRIV_GUARD_ERROR_SUCCESS;
894 }
895
896 int
897 PrivacyGuardDb::PgForeachPrivacyPackageId(const int userId, std::list < std::string > &packageList)
898 {
899         int res = -1;
900         static const std::string query = std::string("SELECT DISTINCT PKG_ID FROM MonitorPolicy WHERE USER_ID=?");
901
902         m_dbMutex.lock();
903         // open db
904         if(m_bDBOpen == false) {
905                 openSqliteDB();
906         }
907         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
908
909         // prepare
910         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
911         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
912
913         // bind
914         res = sqlite3_bind_int(m_stmt, 1, userId);
915         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
916
917         // step
918         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
919                 char* p_data = (char*)sqlite3_column_text(m_stmt, 0);
920                 if(p_data == NULL) {
921                         continue;
922                 }
923                 packageList.push_back(std::string(p_data));
924         }
925         m_dbMutex.unlock();
926
927         return PRIV_GUARD_ERROR_SUCCESS;
928 }
929
930 int
931 PrivacyGuardDb::PgForeachPackageByPrivacyId(const int userId, const std::string privacyId, std::list < std::string > &packageList)
932 {
933         int res = -1;
934         static const std::string query = std::string("SELECT DISTINCT PKG_ID FROM MonitorPolicy WHERE USER_ID=? AND PRIVACY_ID=?");
935
936         m_dbMutex.lock();
937         // open db
938         if(m_bDBOpen == false) {
939                 openSqliteDB();
940         }
941         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
942
943         // prepare
944         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
945         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
946
947         // bind
948         res = sqlite3_bind_int(m_stmt, 1, userId);
949         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
950
951         res = sqlite3_bind_text(m_stmt, 2, privacyId.c_str(), -1, SQLITE_TRANSIENT);
952         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
953
954         // step
955         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
956                 char* p_data = (char*)sqlite3_column_text(m_stmt, 0);
957                 if(p_data == NULL) {
958                         continue;
959                 }
960                 packageList.push_back(std::string(p_data));
961         }
962         m_dbMutex.unlock();
963
964         return PRIV_GUARD_ERROR_SUCCESS;
965 }
966
967 int
968 PrivacyGuardDb::PgUpdateMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, const int monitorPolicy)
969 {
970         int res = -1;
971         static const std::string query = std::string("UPDATE MonitorPolicy SET MONITOR_POLICY=? WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=?");
972
973         m_dbMutex.lock();
974         // open db
975         if(m_bDBOpen == false) {
976                 openSqliteDB();
977         }
978         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
979
980         // prepare
981         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
982         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
983
984         // bind
985         res = sqlite3_bind_int(m_stmt, 1, monitorPolicy);
986         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
987
988         res = sqlite3_bind_int(m_stmt, 2, userId);
989         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
990
991         res = sqlite3_bind_text(m_stmt, 3, packageId.c_str(), -1, SQLITE_TRANSIENT);
992         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
993
994         res = sqlite3_bind_text(m_stmt, 4, privacyId.c_str(), -1, SQLITE_TRANSIENT);
995         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
996
997         // step
998         res = sqlite3_step(m_stmt);
999         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1000
1001         m_dbMutex.unlock();
1002
1003         return PRIV_GUARD_ERROR_SUCCESS;
1004 }
1005
1006 int
1007 PrivacyGuardDb::PgAddMainMonitorPolicy(const int userId)
1008 {
1009         int res = -1;
1010
1011         static const std::string QUERY_INSERT = std::string("INSERT INTO MainMonitorPolicy(USER_ID) VALUES(?)");
1012
1013         m_dbMutex.lock();
1014         // open db
1015         if(m_bDBOpen == false) {
1016                 openSqliteDB();
1017         }
1018         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1019
1020         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
1021
1022         // prepare
1023         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
1024         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1025
1026         //bind
1027         res = sqlite3_bind_int(m_stmt, 1, userId);
1028         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1029
1030         //step
1031         res = sqlite3_step(m_stmt);
1032         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1033
1034         m_dbMutex.unlock();
1035
1036         return PRIV_GUARD_ERROR_SUCCESS;
1037 }
1038
1039 int
1040 PrivacyGuardDb::PgUpdateMainMonitorPolicy(const int userId, const bool mainMonitorPolicy)
1041 {
1042         int res = -1;
1043         static const std::string query = std::string("UPDATE MainMonitorPolicy SET MAIN_MONITOR_POLICY=? WHERE USER_ID=?");
1044
1045         m_dbMutex.lock();
1046         // open db
1047         if(m_bDBOpen == false) {
1048                 openSqliteDB();
1049         }
1050         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1051
1052         // prepare
1053         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
1054         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1055
1056         // bind
1057         res = sqlite3_bind_int(m_stmt, 1, mainMonitorPolicy);
1058         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1059
1060         res = sqlite3_bind_int(m_stmt, 2, userId);
1061         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1062
1063         // step
1064         res = sqlite3_step(m_stmt);
1065         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1066
1067         m_dbMutex.unlock();
1068
1069 #if 0
1070         // [CYNARA] Set Filter
1071         cynara_monitor_configuration_set_filter();
1072 #endif
1073
1074         return PRIV_GUARD_ERROR_SUCCESS;
1075 }
1076
1077 int
1078 PrivacyGuardDb::PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy)
1079 {
1080
1081         int res = -1;
1082         static const std::string query = std::string("SELECT MAIN_MONITOR_POLICY FROM MainMonitorPolicy WHERE USER_ID=?");
1083
1084         m_dbMutex.lock();
1085         // open db
1086         if(m_bDBOpen == false) {
1087                 openSqliteDB();
1088         }
1089         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1090
1091         // prepare
1092         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
1093         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1094
1095         // bind
1096         res = sqlite3_bind_int(m_stmt, 1, userId);
1097         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1098
1099         // step
1100         mainMonitorPolicy = false;
1101         if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
1102                 mainMonitorPolicy = sqlite3_column_int(m_stmt, 0);
1103                 m_dbMutex.unlock();
1104         }
1105         else {
1106                 m_dbMutex.unlock();
1107                 res = PgAddMainMonitorPolicy(userId);
1108                 TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "PgAddMainMonitorPolicy failed : %d", res);
1109         }
1110
1111         return PRIV_GUARD_ERROR_SUCCESS;
1112 }
1113
1114 int
1115 PrivacyGuardDb::PgDeleteMainMonitorPolicyByUserId(const int userId)
1116 {
1117         int res = -1;
1118
1119         static const std::string QUERY_DELETE = std::string("DELETE FROM MainMonitorPolicy WHERE USER_ID=?");
1120
1121         m_dbMutex.lock();
1122         // open db
1123         if(m_bDBOpen == false) {
1124                 openSqliteDB();
1125         }
1126         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1127
1128         // prepare
1129         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_DELETE.c_str(), -1, &m_stmt, NULL);
1130         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1131
1132         // bind
1133         res = sqlite3_bind_int(m_stmt, 1, userId);
1134         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1135
1136         // step
1137         res = sqlite3_step(m_stmt);
1138         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1139
1140         m_dbMutex.unlock();
1141
1142         return PRIV_GUARD_ERROR_SUCCESS;
1143 }
1144
1145 PrivacyGuardDb::PrivacyGuardDb(void)
1146 {
1147
1148         // open DB
1149         m_bDBOpen = false;
1150         m_sqlHandler = NULL;
1151         m_dbMutex.lock();
1152         openSqliteDB();
1153         m_dbMutex.unlock();
1154         m_stmt = NULL;
1155 }
1156
1157 PrivacyGuardDb::~PrivacyGuardDb(void)
1158 {
1159         // close DB
1160         if(m_bDBOpen == true) {
1161                 m_dbMutex.lock();
1162                 sqlite3_finalize(m_stmt);
1163                 sqlite3_close(m_sqlHandler);
1164                 m_bDBOpen = false;
1165                 m_dbMutex.unlock();
1166         }
1167 }
1168
1169 PrivacyGuardDb*
1170 PrivacyGuardDb::getInstance(void)
1171 {
1172         std::lock_guard < std::mutex > guard(m_singletonMutex);
1173
1174         if (m_pInstance == NULL) {
1175                 m_pInstance = new PrivacyGuardDb();
1176         }
1177
1178         return m_pInstance;
1179 }