1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "SystemChecker"
16 #include "system_checker.h"
17 #include "accesstoken_kit.h"
18 #include "log_print.h"
19 namespace OHOS {
20 namespace DistributedData {
21 using namespace Security::AccessToken;
22 __attribute__((used)) SystemChecker SystemChecker::instance_;
SystemChecker()23 SystemChecker::SystemChecker() noexcept
24 {
25     CheckerManager::GetInstance().RegisterPlugin(
26         "SystemChecker", [this]() -> auto { return this; });
27 }
28 
~SystemChecker()29 SystemChecker::~SystemChecker()
30 {
31 }
32 
Initialize()33 void SystemChecker::Initialize()
34 {
35 }
36 
SetTrustInfo(const CheckerManager::Trust & trust)37 bool SystemChecker::SetTrustInfo(const CheckerManager::Trust &trust)
38 {
39     trusts_[trust.bundleName] = trust.appId;
40     return true;
41 }
42 
SetDistrustInfo(const CheckerManager::Distrust & distrust)43 bool SystemChecker::SetDistrustInfo(const CheckerManager::Distrust &distrust)
44 {
45     distrusts_[distrust.bundleName] = distrust.appId;
46     return true;
47 }
48 
SetSwitchesInfo(const CheckerManager::Switches & switches)49 bool SystemChecker::SetSwitchesInfo(const CheckerManager::Switches &switches)
50 {
51     switches_[switches.bundleName] = switches.appId;
52     return true;
53 }
54 
GetAppId(const CheckerManager::StoreInfo & info)55 std::string SystemChecker::GetAppId(const CheckerManager::StoreInfo &info)
56 {
57     if (!IsValid(info)) {
58         return "";
59     }
60     std::string appId = (trusts_.find(info.bundleName) != trusts_.end()) ? trusts_[info.bundleName] : info.bundleName;
61     ZLOGD("bundleName:%{public}s, appId:%{public}s", info.bundleName.c_str(), appId.c_str());
62     return appId;
63 }
64 
IsValid(const CheckerManager::StoreInfo & info)65 bool SystemChecker::IsValid(const CheckerManager::StoreInfo &info)
66 {
67     auto type = AccessTokenKit::GetTokenTypeFlag(info.tokenId);
68     return (type == TOKEN_NATIVE || type == TOKEN_SHELL);
69 }
70 
IsDistrust(const CheckerManager::StoreInfo & info)71 bool SystemChecker::IsDistrust(const CheckerManager::StoreInfo &info)
72 {
73     if (!IsValid(info)) {
74         return false;
75     }
76     auto it = distrusts_.find(info.bundleName);
77     if (it != distrusts_.end()) {
78         return true;
79     }
80     return false;
81 }
82 
IsSwitches(const CheckerManager::StoreInfo & info)83 bool SystemChecker::IsSwitches(const CheckerManager::StoreInfo &info)
84 {
85     if (!IsValid(info)) {
86         return false;
87     }
88     return switches_.find(info.bundleName) != switches_.end();
89 }
90 
GetDynamicStores()91 std::vector<CheckerManager::StoreInfo> SystemChecker::GetDynamicStores()
92 {
93     return dynamicStores_;
94 }
GetStaticStores()95 std::vector<CheckerManager::StoreInfo> SystemChecker::GetStaticStores()
96 {
97     return staticStores_;
98 }
99 
IsDynamic(const CheckerManager::StoreInfo & info)100 bool SystemChecker::IsDynamic(const CheckerManager::StoreInfo &info)
101 {
102     for (const auto &store : dynamicStores_) {
103         if (info.bundleName == store.bundleName && info.storeId == store.storeId) {
104             return true;
105         }
106     }
107     return false;
108 }
109 
IsStatic(const CheckerManager::StoreInfo & info)110 bool SystemChecker::IsStatic(const CheckerManager::StoreInfo &info)
111 {
112     for (const auto &store : staticStores_) {
113         if (info.bundleName == store.bundleName && info.storeId == store.storeId) {
114             return true;
115         }
116     }
117     return false;
118 }
119 
AddDynamicStore(const CheckerManager::StoreInfo & storeInfo)120 bool SystemChecker::AddDynamicStore(const CheckerManager::StoreInfo &storeInfo)
121 {
122     dynamicStores_.push_back(storeInfo);
123     return true;
124 }
125 
AddStaticStore(const CheckerManager::StoreInfo & storeInfo)126 bool SystemChecker::AddStaticStore(const CheckerManager::StoreInfo &storeInfo)
127 {
128     staticStores_.push_back(storeInfo);
129     return true;
130 }
131 } // namespace DistributedData
132 } // namespace OHOS