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 "event_config.h"
17
18 #include "file_ex.h"
19
20 #include "config_data_manager.h"
21 #include "json_cfg.h"
22 #include "model_analysis_define.h"
23 #include "model_cfg_marshalling.h"
24 #include "security_guard_log.h"
25 #include "security_guard_utils.h"
26
27 namespace OHOS::Security::SecurityGuard {
28 namespace {
29 constexpr const char *AUDIT_MODEL_ID = "3001000003";
30 constexpr int32_t DB_MAX_VALUE = 100000;
31 constexpr int32_t STORAGE_ROM_NUMS_MAX_VALUE = 1000;
32 }
33
Load(int mode)34 bool EventConfig::Load(int mode)
35 {
36 std::string path;
37 if (mode == INIT_MODE) {
38 if (FileExists(CONFIG_UPTATE_FILES[EVENT_CFG_INDEX])) {
39 path = CONFIG_UPTATE_FILES[EVENT_CFG_INDEX];
40 } else if (FileExists(CONFIG_PRESET_FILES[EVENT_CFG_INDEX])) {
41 path = CONFIG_PRESET_FILES[EVENT_CFG_INDEX];
42 }
43 } else if (mode == UPDATE_MODE) {
44 if (FileExists(CONFIG_CACHE_FILES[EVENT_CFG_INDEX])) {
45 path = CONFIG_CACHE_FILES[EVENT_CFG_INDEX];
46 }
47 }
48 SGLOGD("path=%{public}s", path.c_str());
49 if (path.empty()) {
50 SGLOGE("path is empty");
51 return false;
52 }
53 stream_ = std::ifstream(path, std::ios::in);
54 if (!stream_.is_open() || !stream_) {
55 SGLOGE("stream error, %{public}s", strerror(errno));
56 return false;
57 }
58 return true;
59 }
60
Parse()61 bool EventConfig::Parse()
62 {
63 if (!stream_.is_open() || !stream_) {
64 SGLOGE("stream error");
65 return false;
66 }
67 nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
68 stream_.close();
69
70 if (jsonObj.is_discarded()) {
71 SGLOGI("json is discarded");
72 return false;
73 }
74
75 std::vector<EventCfg> configs;
76 bool success = EventConfig::ParseEventConfig(configs, jsonObj);
77 if (!success) {
78 SGLOGE("parse EventConfig error");
79 return false;
80 }
81 EventConfig::CacheEventConfig(configs);
82 EventConfig::CacheEventToTable(configs);
83 SGLOGI("cache EventConfig success");
84 return true;
85 }
86
Update()87 bool EventConfig::Update()
88 {
89 if (!stream_.is_open() || !stream_) {
90 SGLOGE("stream error");
91 return false;
92 }
93 nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
94 stream_.close();
95
96 if (jsonObj.is_discarded()) {
97 SGLOGI("json is discarded");
98 return false;
99 }
100
101 std::vector<EventCfg> configs;
102 bool success = EventConfig::ParseEventConfig(configs, jsonObj);
103 if (!success) {
104 SGLOGE("parse EventConfig error");
105 return false;
106 }
107
108 SecurityGuardUtils::CopyFile(CONFIG_CACHE_FILES[EVENT_CFG_INDEX], CONFIG_UPTATE_FILES[EVENT_CFG_INDEX]);
109 ConfigDataManager::GetInstance().ResetEventMap();
110 EventConfig::CacheEventConfig(configs);
111 EventConfig::CacheEventToTable(configs);
112 SGLOGI("cache EventConfig success");
113 return true;
114 }
115
ParseEventConfig(std::vector<EventCfg> & configs,nlohmann::json & jsonObj)116 bool EventConfig::ParseEventConfig(std::vector<EventCfg> &configs, nlohmann::json &jsonObj)
117 {
118 bool success = JsonCfg::Unmarshal<EventCfg>(configs, jsonObj, EVENT_CFG_KEY);
119 if (success) {
120 for (EventCfg &config : configs) {
121 uint32_t maxValue = STORAGE_ROM_NUMS_MAX_VALUE;
122 if (!config.owner.empty() && config.owner.at(0) == AUDIT_MODEL_ID) {
123 maxValue = DB_MAX_VALUE;
124 }
125 if (config.storageRomNums >= maxValue) {
126 config.storageRomNums = maxValue;
127 }
128 }
129 }
130 return success;
131 }
132
CacheEventConfig(const std::vector<EventCfg> & configs)133 void EventConfig::CacheEventConfig(const std::vector<EventCfg> &configs)
134 {
135 for (const EventCfg &config : configs) {
136 ConfigDataManager::GetInstance().InsertEventMap(config.eventId, config);
137 }
138 }
139
CacheEventToTable(const std::vector<EventCfg> & configs)140 void EventConfig::CacheEventToTable(const std::vector<EventCfg> &configs)
141 {
142 for (const EventCfg &config : configs) {
143 ConfigDataManager::GetInstance().InsertEventToTableMap(config.eventId, config.dbTable);
144 }
145 }
146 } // OHOS::Security::SecurityGuard
147