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()) {
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()) {
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()) {
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     if (!SecurityGuardUtils::CopyFile(CONFIG_CACHE_FILES[EVENT_CFG_INDEX], CONFIG_UPTATE_FILES[EVENT_CFG_INDEX])) {
109         SGLOGE("copyFile error");
110         return false;
111     }
112     ConfigDataManager::GetInstance().ResetEventMap();
113     EventConfig::CacheEventConfig(configs);
114     EventConfig::CacheEventToTable(configs);
115     SGLOGI("cache EventConfig success");
116     return true;
117 }
118 
ParseEventConfig(std::vector<EventCfg> &configs, nlohmann::json &jsonObj)119 bool EventConfig::ParseEventConfig(std::vector<EventCfg> &configs, nlohmann::json &jsonObj)
120 {
121     bool success = JsonCfg::Unmarshal<EventCfg>(configs, jsonObj, EVENT_CFG_KEY);
122     if (success) {
123         for (EventCfg &config : configs) {
124             uint32_t maxValue = STORAGE_ROM_NUMS_MAX_VALUE;
125             if (!config.owner.empty() && config.owner.at(0) == AUDIT_MODEL_ID) {
126                 maxValue = DB_MAX_VALUE;
127             }
128             if (config.storageRomNums >= maxValue) {
129                 config.storageRomNums = maxValue;
130             }
131         }
132     }
133     return success;
134 }
135 
CacheEventConfig(const std::vector<EventCfg> &configs)136 void EventConfig::CacheEventConfig(const std::vector<EventCfg> &configs)
137 {
138     for (const EventCfg &config : configs) {
139         ConfigDataManager::GetInstance().InsertEventMap(config.eventId, config);
140     }
141 }
142 
CacheEventToTable(const std::vector<EventCfg> &configs)143 void EventConfig::CacheEventToTable(const std::vector<EventCfg> &configs)
144 {
145     for (const EventCfg &config : configs) {
146         ConfigDataManager::GetInstance().InsertEventToTableMap(config.eventId, config.dbTable);
147     }
148 }
149 } // OHOS::Security::SecurityGuard
150