1 /*
2 * Copyright (c) 2023-2024 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 "restrictions_proxy.h"
17
18 #include "edm_log.h"
19 #include "func_code.h"
20
21 namespace OHOS {
22 namespace EDM {
23 std::shared_ptr<RestrictionsProxy> RestrictionsProxy::instance_ = nullptr;
24 std::once_flag RestrictionsProxy::flag_;
25 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
26
GetRestrictionsProxy()27 std::shared_ptr<RestrictionsProxy> RestrictionsProxy::GetRestrictionsProxy()
28 {
29 std::call_once(flag_, []() {
30 if (instance_ == nullptr) {
31 instance_ = std::make_shared<RestrictionsProxy>();
32 }
33 });
34 return instance_;
35 }
36
SetDisallowedPolicy(const AppExecFwk::ElementName & admin,bool disallow,int policyCode,std::string permissionTag)37 int32_t RestrictionsProxy::SetDisallowedPolicy(const AppExecFwk::ElementName &admin, bool disallow, int policyCode,
38 std::string permissionTag)
39 {
40 return EnterpriseDeviceMgrProxy::GetInstance()->SetPolicyDisabled(admin, disallow, policyCode, permissionTag);
41 }
42
SetDisallowedPolicy(MessageParcel & data,uint32_t policyCode)43 int32_t RestrictionsProxy::SetDisallowedPolicy(MessageParcel &data, uint32_t policyCode)
44 {
45 return EnterpriseDeviceMgrProxy::GetInstance()->SetPolicyDisabled(data, policyCode);
46 }
47
GetDisallowedPolicy(AppExecFwk::ElementName * admin,int policyCode,bool & result,std::string permissionTag)48 int32_t RestrictionsProxy::GetDisallowedPolicy(AppExecFwk::ElementName *admin, int policyCode, bool &result,
49 std::string permissionTag)
50 {
51 return EnterpriseDeviceMgrProxy::GetInstance()->IsPolicyDisabled(admin, policyCode, result, permissionTag);
52 }
53
GetDisallowedPolicy(MessageParcel & data,uint32_t policyCode,bool & result)54 int32_t RestrictionsProxy::GetDisallowedPolicy(MessageParcel &data, uint32_t policyCode, bool &result)
55 {
56 return EnterpriseDeviceMgrProxy::GetInstance()->IsPolicyDisabled(data, policyCode, result);
57 }
58
SetFingerprintAuthDisabled(const AppExecFwk::ElementName & admin,bool disallow)59 int32_t RestrictionsProxy::SetFingerprintAuthDisabled(const AppExecFwk::ElementName &admin, bool disallow)
60 {
61 EDMLOGD("RestrictionsProxy::SetFingerprintAuthDisabled");
62 MessageParcel data;
63 std::uint32_t funcCode =
64 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::FINGERPRINT_AUTH);
65 data.WriteInterfaceToken(DESCRIPTOR);
66 data.WriteInt32(WITHOUT_USERID);
67 data.WriteParcelable(&admin);
68 data.WriteString(WITHOUT_PERMISSION_TAG);
69 data.WriteString(EdmConstants::FINGERPRINT_AUTH_TYPE);
70 data.WriteBool(disallow);
71 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
72 }
73
IsFingerprintAuthDisabled(AppExecFwk::ElementName * admin,bool & result)74 int32_t RestrictionsProxy::IsFingerprintAuthDisabled(AppExecFwk::ElementName *admin, bool &result)
75 {
76 EDMLOGD("RestrictionsProxy::GetFingerprintAuthDisabled");
77 MessageParcel data;
78 MessageParcel reply;
79 data.WriteInterfaceToken(DESCRIPTOR);
80 data.WriteInt32(WITHOUT_USERID);
81 data.WriteString(WITHOUT_PERMISSION_TAG);
82 if (admin != nullptr) {
83 data.WriteInt32(HAS_ADMIN);
84 data.WriteParcelable(admin);
85 } else {
86 data.WriteInt32(WITHOUT_ADMIN);
87 }
88 data.WriteString(EdmConstants::FINGERPRINT_AUTH_TYPE);
89 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::FINGERPRINT_AUTH, data, reply);
90 int32_t ret = ERR_INVALID_VALUE;
91 reply.ReadInt32(ret);
92 if (ret != ERR_OK) {
93 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
94 return ret;
95 }
96 reply.ReadBool(result);
97 return ERR_OK;
98 }
99
SetDisallowedPolicyForAccount(const AppExecFwk::ElementName & admin,bool disallow,uint32_t policyCode,std::string permissionTag,int32_t accountId)100 int32_t RestrictionsProxy::SetDisallowedPolicyForAccount(const AppExecFwk::ElementName &admin, bool disallow,
101 uint32_t policyCode, std::string permissionTag, int32_t accountId)
102 {
103 EDMLOGD("RestrictionsProxy::SetDisallowedPolicyForAccount");
104 MessageParcel data;
105 std::uint32_t funcCode =
106 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyCode);
107 data.WriteInterfaceToken(DESCRIPTOR);
108 data.WriteInt32(WITHOUT_USERID);
109 data.WriteParcelable(&admin);
110 data.WriteString(permissionTag);
111 data.WriteString(EdmConstants::DISALLOW_FOR_ACCOUNT_TYPE);
112 data.WriteBool(disallow);
113 data.WriteInt32(accountId);
114 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
115 }
116
GetDisallowedPolicyForAccount(AppExecFwk::ElementName & admin,int policyCode,bool & result,std::string permissionTag,int32_t accountId)117 int32_t RestrictionsProxy::GetDisallowedPolicyForAccount(AppExecFwk::ElementName &admin, int policyCode, bool &result,
118 std::string permissionTag, int32_t accountId)
119 {
120 EDMLOGD("RestrictionsProxy::GetDisallowedPolicyForAccount");
121 MessageParcel data;
122 MessageParcel reply;
123 data.WriteInterfaceToken(DESCRIPTOR);
124 data.WriteInt32(WITHOUT_USERID);
125 data.WriteString(permissionTag);
126 data.WriteInt32(HAS_ADMIN);
127 data.WriteParcelable(&admin);
128 data.WriteString(EdmConstants::DISALLOW_FOR_ACCOUNT_TYPE);
129 data.WriteInt32(accountId);
130 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(policyCode, data, reply);
131 int32_t ret = ERR_INVALID_VALUE;
132 reply.ReadInt32(ret);
133 if (ret != ERR_OK) {
134 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
135 return ret;
136 }
137 reply.ReadBool(result);
138 return ERR_OK;
139 }
140
AddOrRemoveDisallowedListForAccount(const AppExecFwk::ElementName & admin,std::string feature,std::vector<std::string> & bundles,int32_t accountId,bool isAdd)141 int32_t RestrictionsProxy::AddOrRemoveDisallowedListForAccount(const AppExecFwk::ElementName &admin,
142 std::string feature, std::vector<std::string> &bundles, int32_t accountId, bool isAdd)
143 {
144 EDMLOGD("RestrictionsProxy::AddOrRemoveDisallowedListForAccount called");
145 MessageParcel data;
146 std::uint32_t interfaceCode = 0;
147 if (!GetDisallowedListInterfaceCode(feature, interfaceCode)) {
148 return EdmReturnErrCode::PARAM_ERROR;
149 }
150 std::uint32_t funcCode = 0;
151 if (isAdd) {
152 funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, interfaceCode);
153 } else {
154 funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, interfaceCode);
155 }
156 data.WriteInterfaceToken(DESCRIPTOR);
157 data.WriteInt32(HAS_USERID);
158 data.WriteInt32(accountId);
159 data.WriteParcelable(&admin);
160 data.WriteString(WITHOUT_PERMISSION_TAG);
161 data.WriteStringVector(bundles);
162 return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
163 }
164
GetDisallowedListForAccount(AppExecFwk::ElementName & admin,std::string feature,int32_t accountId,std::vector<std::string> & result)165 int32_t RestrictionsProxy::GetDisallowedListForAccount(AppExecFwk::ElementName &admin, std::string feature,
166 int32_t accountId, std::vector<std::string> &result)
167 {
168 EDMLOGD("RestrictionsProxy::GetDisallowedListForAccount called");
169 std::uint32_t interfaceCode = 0;
170 if (!GetDisallowedListInterfaceCode(feature, interfaceCode)) {
171 return EdmReturnErrCode::PARAM_ERROR;
172 }
173 MessageParcel data;
174 MessageParcel reply;
175 data.WriteInterfaceToken(DESCRIPTOR);
176 data.WriteInt32(HAS_USERID);
177 data.WriteInt32(accountId);
178 data.WriteString(WITHOUT_PERMISSION_TAG);
179 data.WriteInt32(HAS_ADMIN);
180 data.WriteParcelable(&admin);
181 EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(interfaceCode, data, reply);
182 int32_t ret = ERR_INVALID_VALUE;
183 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
184 if (!blRes) {
185 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
186 return ret;
187 }
188 reply.ReadStringVector(&result);
189 return ERR_OK;
190 }
191
GetDisallowedListInterfaceCode(std::string feature,std::uint32_t & interfaceCode)192 bool RestrictionsProxy::GetDisallowedListInterfaceCode(std::string feature, std::uint32_t &interfaceCode)
193 {
194 auto it = featureInterfaceMap_.find(feature);
195 if (it != featureInterfaceMap_.end()) {
196 interfaceCode = it->second;
197 return true;
198 }
199 return false;
200 }
201 } // namespace EDM
202 } // namespace OHOS
203