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 "fingerprint_auth_plugin.h"
17
18 #include "edm_ipc_interface_code.h"
19 #include "fingerprint_policy_serializer.h"
20 #include "iplugin_manager.h"
21 #include "user_auth_client.h"
22
23 namespace OHOS {
24 namespace EDM {
25 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(std::make_shared<FingerprintAuthPlugin>());
FingerprintAuthPlugin()26 FingerprintAuthPlugin::FingerprintAuthPlugin()
27 {
28 policyCode_ = EdmInterfaceCode::FINGERPRINT_AUTH;
29 policyName_ = "fingerprint_auth";
30 permissionConfig_.typePermissions.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
31 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_RESTRICTIONS);
32 permissionConfig_.apiType = IPlugin::ApiType::PUBLIC;
33 needSave_ = true;
34 }
35
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)36 ErrCode FingerprintAuthPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
37 HandlePolicyData &policyData, int32_t userId)
38 {
39 EDMLOGI("FingerprintAuthPlugin OnSetPolicy");
40 std::string type = data.ReadString();
41 bool disallow = data.ReadBool();
42 ErrCode ret = ERR_INVALID_VALUE;
43 auto serializer = FingerprintPolicySerializer::GetInstance();
44 FingerprintPolicy currentPolicy;
45 FingerprintPolicy mergePolicy;
46 if (!serializer->Deserialize(policyData.policyData, currentPolicy) ||
47 !serializer->Deserialize(policyData.mergePolicyData, mergePolicy)) {
48 EDMLOGE("FingerprintAuthPlugin::OnHandlePolicy Deserialize current policy and merge policy failed.");
49 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
50 }
51 if (type == EdmConstants::FINGERPRINT_AUTH_TYPE) {
52 ret = HandleFingerprintAuthPolicy(disallow, currentPolicy, mergePolicy);
53 } else if (type == EdmConstants::DISALLOW_FOR_ACCOUNT_TYPE) {
54 int32_t accountId = data.ReadInt32();
55 ret = HandleFingerprintForAccountPolicy(disallow, accountId, currentPolicy, mergePolicy);
56 }
57 if (ret != ERR_OK) {
58 return ret;
59 }
60 ret = SetGlobalConfigParam(mergePolicy);
61 if (ret != ERR_OK) {
62 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
63 }
64
65 std::string afterHandle;
66 std::string afterMerge;
67 if (!serializer->Serialize(currentPolicy, afterHandle) || !serializer->Serialize(mergePolicy, afterMerge)) {
68 EDMLOGE("FingerprintAuthPlugin Serialize current policy and merge policy failed");
69 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
70 }
71 policyData.isChanged = (afterHandle != policyData.policyData);
72 if (policyData.isChanged) {
73 policyData.policyData = afterHandle;
74 }
75 policyData.mergePolicyData = afterMerge;
76 return ERR_OK;
77 }
78
HandleFingerprintAuthPolicy(bool disallow,FingerprintPolicy & currentPolicy,FingerprintPolicy & mergePolicy)79 ErrCode FingerprintAuthPlugin::HandleFingerprintAuthPolicy(bool disallow, FingerprintPolicy ¤tPolicy,
80 FingerprintPolicy &mergePolicy)
81 {
82 if (disallow) {
83 currentPolicy.globalDisallow = true;
84 currentPolicy.accountIds.clear();
85 mergePolicy.globalDisallow = true;
86 mergePolicy.accountIds.clear();
87 return ERR_OK;
88 }
89 if (!currentPolicy.accountIds.empty()) {
90 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
91 }
92 currentPolicy.globalDisallow = false;
93 return ERR_OK;
94 }
95
HandleFingerprintForAccountPolicy(bool disallow,int32_t accountId,FingerprintPolicy & currentPolicy,FingerprintPolicy & mergePolicy)96 ErrCode FingerprintAuthPlugin::HandleFingerprintForAccountPolicy(bool disallow, int32_t accountId,
97 FingerprintPolicy ¤tPolicy, FingerprintPolicy &mergePolicy)
98 {
99 if (currentPolicy.globalDisallow) {
100 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
101 }
102 if (disallow) {
103 currentPolicy.accountIds.insert(accountId);
104 } else {
105 auto it = currentPolicy.accountIds.find(accountId);
106 if (it != currentPolicy.accountIds.end()) {
107 currentPolicy.accountIds.erase(it);
108 }
109 }
110 if (!mergePolicy.globalDisallow) {
111 for (auto item : currentPolicy.accountIds) {
112 mergePolicy.accountIds.insert(item);
113 }
114 }
115 return ERR_OK;
116 }
117
SetGlobalConfigParam(FingerprintPolicy policy)118 ErrCode FingerprintAuthPlugin::SetGlobalConfigParam(FingerprintPolicy policy)
119 {
120 std::vector<int32_t> userIds(policy.accountIds.size());
121 std::copy(policy.accountIds.begin(), policy.accountIds.end(), userIds.begin());
122 UserIam::UserAuth::GlobalConfigParam param;
123 param.userIds = userIds;
124 param.authTypes.push_back(UserIam::UserAuth::AuthType::FINGERPRINT);
125 param.type = UserIam::UserAuth::GlobalConfigType::ENABLE_STATUS;
126 param.value.enableStatus = !policy.globalDisallow && userIds.size() == 0;
127 return UserIam::UserAuth::UserAuthClient::GetInstance().SetGlobalConfigParam(param);
128 }
129
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)130 ErrCode FingerprintAuthPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data,
131 MessageParcel &reply, int32_t userId)
132 {
133 EDMLOGI("FingerprintAuthPlugin OnGetPolicy");
134 auto serializer_ = FingerprintPolicySerializer::GetInstance();
135 FingerprintPolicy policy;
136 serializer_->Deserialize(policyData, policy);
137 std::string type = data.ReadString();
138 bool isDisallow = false;
139 if (type == EdmConstants::FINGERPRINT_AUTH_TYPE) {
140 isDisallow = policy.globalDisallow;
141 } else if (type == EdmConstants::DISALLOW_FOR_ACCOUNT_TYPE) {
142 int32_t accountId = data.ReadInt32();
143 auto it = policy.accountIds.find(accountId);
144 isDisallow = (it != policy.accountIds.end());
145 }
146 reply.WriteInt32(ERR_OK);
147 reply.WriteBool(isDisallow);
148 EDMLOGI("FingerprintAuthPlugin OnGetPolicy result %{public}d", isDisallow);
149 return ERR_OK;
150 }
151
GetOthersMergePolicyData(const std::string & adminName,std::string & othersMergePolicyData)152 ErrCode FingerprintAuthPlugin::GetOthersMergePolicyData(const std::string &adminName,
153 std::string &othersMergePolicyData)
154 {
155 std::unordered_map<std::string, std::string> adminValues;
156 IPolicyManager::GetInstance()->GetAdminByPolicyName(GetPolicyName(), adminValues);
157 EDMLOGI("FingerprintAuthPlugin::GetOthersMergePolicyData %{public}s value size %{public}d.",
158 GetPolicyName().c_str(), (uint32_t)adminValues.size());
159 if (adminValues.empty()) {
160 return ERR_OK;
161 }
162 auto entry = adminValues.find(adminName);
163 // Remove the current admin policy value from the cache map.
164 if (entry != adminValues.end()) {
165 adminValues.erase(entry);
166 }
167 if (adminValues.empty()) {
168 return ERR_OK;
169 }
170 auto serializer = FingerprintPolicySerializer::GetInstance();
171 std::vector<FingerprintPolicy> data;
172 for (const auto &item : adminValues) {
173 FingerprintPolicy dataItem;
174 if (!item.second.empty()) {
175 if (!serializer->Deserialize(item.second, dataItem)) {
176 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
177 }
178 data.push_back(dataItem);
179 }
180 }
181 FingerprintPolicy result;
182 if (!serializer->MergePolicy(data, result)) {
183 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
184 }
185 if (!serializer->Serialize(result, othersMergePolicyData)) {
186 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
187 }
188 return ERR_OK;
189 }
190
OnAdminRemove(const std::string & adminName,const std::string & policyData,const std::string & mergeData,int32_t userId)191 ErrCode FingerprintAuthPlugin::OnAdminRemove(const std::string &adminName, const std::string &policyData,
192 const std::string &mergeData, int32_t userId)
193 {
194 FingerprintPolicy policy;
195 FingerprintPolicySerializer::GetInstance()->Deserialize(mergeData, policy);
196 ErrCode ret = SetGlobalConfigParam(policy);
197 if (ret != ERR_OK) {
198 EDMLOGE("FingerprintAuthPlugin OnAdminRemove set global config param failed.");
199 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
200 }
201 return ERR_OK;
202 }
203 } // namespace EDM
204 } // namespace OHOS
205