1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "database_manager.h"
17 #include <cinttypes>
18 #include "config_data_manager.h"
19 #include "risk_event_rdb_helper.h"
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 #include "store_define.h"
23 #include "security_guard_utils.h"
24 #include "bigdata.h"
25 #include "ffrt.h"
26 
27 namespace OHOS::Security::SecurityGuard {
GetInstance()28 DatabaseManager &DatabaseManager::GetInstance()
29 {
30     static DatabaseManager instance;
31     return instance;
32 }
33 
Init()34 void DatabaseManager::Init()
35 {
36     // init database
37     int32_t ret = RiskEventRdbHelper::GetInstance().Init();
38     SGLOGI("risk event rdb init result is %{public}d", ret);
39 }
40 
InsertEvent(uint32_t source, SecEvent& event)41 int DatabaseManager::InsertEvent(uint32_t source, SecEvent& event)
42 {
43     EventCfg config;
44     bool success = ConfigDataManager::GetInstance().GetEventConfig(event.eventId, config);
45     if (!success) {
46         SGLOGE("not found event, id=%{public}" PRId64, event.eventId);
47         return NOT_FOUND;
48     }
49 
50     if (config.source == source) {
51         std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(event.eventId);
52         SGLOGD("table=%{public}s, eventId=%{public}" PRId64, table.c_str(), config.eventId);
53         if (table == AUDIT_TABLE) {
54             SGLOGD("audit event insert");
55             DbChanged(IDbListener::INSERT, event);
56             return SUCCESS;
57         }
58         SGLOGD("risk event insert, eventId=%{public}" PRId64, event.eventId);
59         // notify changed
60         DbChanged(IDbListener::INSERT, event);
61         std::lock_guard<std::mutex> lock(delMutex_);
62         // Check whether the upper limit is reached.
63         int64_t count = RiskEventRdbHelper::GetInstance().CountEventByEventId(event.eventId);
64         if (count >= config.storageRomNums) {
65             (void) RiskEventRdbHelper::GetInstance().DeleteOldEventByEventId(event.eventId,
66                 count + 1 - config.storageRomNums);
67         }
68         return RiskEventRdbHelper::GetInstance().InsertEvent(event);
69     }
70 
71     // notify changed
72     DbChanged(IDbListener::INSERT, event);
73     return SUCCESS;
74 }
75 
QueryAllEvent(std::string table, std::vector<SecEvent> &events)76 int DatabaseManager::QueryAllEvent(std::string table, std::vector<SecEvent> &events)
77 {
78     if (table == RISK_TABLE) {
79         return RiskEventRdbHelper::GetInstance().QueryAllEvent(events);
80     }
81     return NOT_SUPPORT;
82 }
83 
QueryRecentEventByEventId(int64_t eventId, SecEvent &event)84 int DatabaseManager::QueryRecentEventByEventId(int64_t eventId, SecEvent &event)
85 {
86     std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(eventId);
87     if (table == RISK_TABLE) {
88         return RiskEventRdbHelper::GetInstance().QueryRecentEventByEventId(eventId, event);
89     }
90     return NOT_SUPPORT;
91 }
92 
QueryRecentEventByEventId(std::string table, const std::vector<int64_t> &eventId, std::vector<SecEvent> &event)93 int DatabaseManager::QueryRecentEventByEventId(std::string table, const std::vector<int64_t> &eventId,
94     std::vector<SecEvent> &event)
95 {
96     if (table == RISK_TABLE) {
97         return RiskEventRdbHelper::GetInstance().QueryRecentEventByEventId(eventId, event);
98     }
99     return NOT_SUPPORT;
100 }
101 
QueryEventByEventIdAndDate(std::string table, std::vector<int64_t> &eventIds, std::vector<SecEvent> &events, std::string beginTime, std::string endTime)102 int DatabaseManager::QueryEventByEventIdAndDate(std::string table, std::vector<int64_t> &eventIds,
103     std::vector<SecEvent> &events, std::string beginTime, std::string endTime)
104 {
105     if (table == RISK_TABLE) {
106         return RiskEventRdbHelper::GetInstance().QueryEventByEventIdAndDate(eventIds, events, beginTime, endTime);
107     }
108     return NOT_SUPPORT;
109 }
110 
QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events)111 int DatabaseManager::QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events)
112 {
113     std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(eventId);
114     if (table == RISK_TABLE) {
115         return RiskEventRdbHelper::GetInstance().QueryEventByEventId(eventId, events);
116     }
117     return NOT_SUPPORT;
118 }
119 
QueryEventByEventId(std::string table, std::vector<int64_t> &eventIds, std::vector<SecEvent> &events)120 int DatabaseManager::QueryEventByEventId(std::string table, std::vector<int64_t> &eventIds,
121     std::vector<SecEvent> &events)
122 {
123     if (table == RISK_TABLE) {
124         return RiskEventRdbHelper::GetInstance().QueryEventByEventId(eventIds, events);
125     }
126     return NOT_SUPPORT;
127 }
128 
QueryEventByEventType(std::string table, int32_t eventType, std::vector<SecEvent> &events)129 int DatabaseManager::QueryEventByEventType(std::string table, int32_t eventType, std::vector<SecEvent> &events)
130 {
131     if (table == RISK_TABLE) {
132         return RiskEventRdbHelper::GetInstance().QueryEventByEventType(eventType, events);
133     }
134     return NOT_SUPPORT;
135 }
136 
QueryEventByLevel(std::string table, int32_t level, std::vector<SecEvent> &events)137 int DatabaseManager::QueryEventByLevel(std::string table, int32_t level, std::vector<SecEvent> &events)
138 {
139     if (table == RISK_TABLE) {
140         return RiskEventRdbHelper::GetInstance().QueryEventByLevel(level, events);
141     }
142     return NOT_SUPPORT;
143 }
144 
QueryEventByOwner(std::string table, std::string owner, std::vector<SecEvent> &events)145 int DatabaseManager::QueryEventByOwner(std::string table, std::string owner, std::vector<SecEvent> &events)
146 {
147     if (table == RISK_TABLE) {
148         return RiskEventRdbHelper::GetInstance().QueryEventByOwner(owner, events);
149     }
150     return NOT_SUPPORT;
151 }
152 
CountAllEvent(std::string table)153 int64_t DatabaseManager::CountAllEvent(std::string table)
154 {
155     if (table == RISK_TABLE) {
156         return RiskEventRdbHelper::GetInstance().CountAllEvent();
157     }
158     return 0;
159 }
160 
CountEventByEventId(int64_t eventId)161 int64_t DatabaseManager::CountEventByEventId(int64_t eventId)
162 {
163     std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(eventId);
164     if (table == RISK_TABLE) {
165         return RiskEventRdbHelper::GetInstance().CountEventByEventId(eventId);
166     }
167     return 0;
168 }
169 
DeleteOldEventByEventId(int64_t eventId, int64_t count)170 int DatabaseManager::DeleteOldEventByEventId(int64_t eventId, int64_t count)
171 {
172     std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(eventId);
173     if (table == RISK_TABLE) {
174         return RiskEventRdbHelper::GetInstance().DeleteOldEventByEventId(eventId, count);
175     }
176     return NOT_SUPPORT;
177 }
178 
DeleteAllEventByEventId(int64_t eventId)179 int DatabaseManager::DeleteAllEventByEventId(int64_t eventId)
180 {
181     std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(eventId);
182     if (table == RISK_TABLE) {
183         return RiskEventRdbHelper::GetInstance().DeleteAllEventByEventId(eventId);
184     }
185     return NOT_SUPPORT;
186 }
187 
SubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener)188 int32_t DatabaseManager::SubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener)
189 {
190     if (listener == nullptr) {
191         SGLOGE("listener is nullptr");
192         return NULL_OBJECT;
193     }
194     std::lock_guard<std::mutex> lock(mutex_);
195     for (int64_t eventId : eventIds) {
196         SGLOGI("SubscribeDb EVENTID %{public}" PRId64, eventId);
197         listenerMap_[eventId].insert(listener);
198     }
199     return SUCCESS;
200 }
201 
UnSubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener)202 int32_t DatabaseManager::UnSubscribeDb(std::vector<int64_t> eventIds, std::shared_ptr<IDbListener> listener)
203 {
204     if (listener == nullptr) {
205         return NULL_OBJECT;
206     }
207     std::lock_guard<std::mutex> lock(mutex_);
208     for (int64_t eventId : eventIds) {
209         listenerMap_[eventId].erase(listener);
210         SGLOGI("size=%{public}zu", listenerMap_[eventId].size());
211         if (listenerMap_[eventId].size() == 0) {
212             listenerMap_.erase(eventId);
213         }
214     }
215     return SUCCESS;
216 }
217 
DbChanged(int32_t optType, const SecEvent &event)218 void DatabaseManager::DbChanged(int32_t optType, const SecEvent &event)
219 {
220     std::lock_guard<std::mutex> lock(mutex_);
221     std::set<std::shared_ptr<IDbListener>> listeners = listenerMap_[event.eventId];
222     if (listeners.empty()) {
223         return;
224     }
225     SGLOGD("eventId=%{public}" PRId64 ", listener size=%{public}zu", event.eventId, listeners.size());
226     auto task = [listeners, optType, event] () {
227         for (auto &listener : listeners) {
228             if (listener != nullptr) {
229                 listener->OnChange(optType, event);
230             }
231         }
232     };
233     ffrt::submit(task);
234     return;
235 }
236 } // namespace OHOS::Security::SecurityGuard