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 "config_subscriber.h"
17
18 #include <fstream>
19 #include <mutex>
20
21 #include "directory_ex.h"
22 #include "string_ex.h"
23
24 #include "bigdata.h"
25 #include "event_config.h"
26 #include "config_define.h"
27 #include "config_manager.h"
28 #include "model_config.h"
29 #include "local_app_config.h"
30 #include "global_app_config.h"
31 #include "security_guard_log.h"
32 #include "security_guard_utils.h"
33
34 namespace OHOS::Security::SecurityGuard {
35 TimeEventRelatedCallBack ConfigSubscriber::timeEventCallBack_ {nullptr};
36 std::mutex ConfigSubscriber::callBackMutex_ {};
37
RegisterTimeEventRelatedCallBack(const TimeEventRelatedCallBack & callBack)38 bool ConfigSubscriber::RegisterTimeEventRelatedCallBack(const TimeEventRelatedCallBack &callBack)
39 {
40 std::lock_guard<std::mutex> lock(callBackMutex_);
41 if (callBack == nullptr) {
42 SGLOGE("callBack is null");
43 return false;
44 }
45 SGLOGI("RegisterTimeEventRelatedCallBack...");
46 timeEventCallBack_ = callBack;
47 return true;
48 }
49
UpdateRelatedEventAnalysisCfg(const std::string & file)50 bool ConfigSubscriber::UpdateRelatedEventAnalysisCfg(const std::string &file)
51 {
52 bool isSuccess = false;
53 {
54 std::lock_guard<std::mutex> lock(callBackMutex_);
55 if (timeEventCallBack_ != nullptr) {
56 isSuccess = timeEventCallBack_();
57 }
58 }
59 if (isSuccess) {
60 return SecurityGuardUtils::CopyFile(file, CONFIG_UPTATE_FILES[RELATED_EVENT_ANALYSIS_CFG_INDEX]);
61 }
62 return isSuccess;
63 }
64
UpdateConfig(const std::string & file)65 bool ConfigSubscriber::UpdateConfig(const std::string &file)
66 {
67 ConfigUpdateEvent event{};
68 bool isSuccess = false;
69 if (file == CONFIG_CACHE_FILES[EVENT_CFG_INDEX]) {
70 isSuccess = ConfigManager::UpdataConfig<EventConfig>();
71 } else if (file == CONFIG_CACHE_FILES[MODEL_CFG_INDEX]) {
72 isSuccess = ConfigManager::UpdataConfig<ModelConfig>();
73 } else if (file == CONFIG_CACHE_FILES[SIG_RULE_CFG_INDEX]) {
74 isSuccess = SecurityGuardUtils::CopyFile(file, CONFIG_UPTATE_FILES[SIG_RULE_CFG_INDEX]);
75 } else if (file == CONFIG_CACHE_FILES[URL_RULE_CFG_INDEX]) {
76 isSuccess = SecurityGuardUtils::CopyFile(file, CONFIG_UPTATE_FILES[URL_RULE_CFG_INDEX]);
77 } else if (file == CONFIG_CACHE_FILES[RELATED_EVENT_ANALYSIS_CFG_INDEX]) {
78 isSuccess = UpdateRelatedEventAnalysisCfg(file);
79 } else if (file == CONFIG_CACHE_FILES[LOCAL_APP_CFG_INDEX]) {
80 isSuccess = ConfigManager::UpdataConfig<LocalAppConfig>();
81 } else if (file == CONFIG_CACHE_FILES[GLOBAL_APP_CFG_INDEX]) {
82 isSuccess = ConfigManager::UpdataConfig<GlobalAppConfig>();
83 }
84 event.path = file;
85 event.time = SecurityGuardUtils::GetDate();
86 event.ret = isSuccess ? SUCCESS : FAILED;
87 SGLOGD("file path=%{public}s, TIME=%{public}s, ret=%{public}d", event.path.c_str(), event.time.c_str(),
88 event.ret);
89 BigData::ReportConfigUpdateEvent(event);
90 bool success = RemoveFile(file);
91 if (!success) {
92 SGLOGW("remove file error, %{public}s", strerror(errno));
93 }
94 return isSuccess;
95 }
96 } // OHOS::Security::SecurityGuard
97