• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "ipolicy_query.h"
17 
18 #include "admin_manager.h"
19 #include "array_string_serializer.h"
20 #include "bool_serializer.h"
21 #include "edm_errors.h"
22 #include "edm_log.h"
23 #include "element_name.h"
24 #include "func_code_utils.h"
25 #include "parameters.h"
26 #include "permission_checker.h"
27 #include "security_report.h"
28 
29 namespace OHOS {
30 namespace EDM {
31 
GetApiType()32 IPlugin::ApiType IPolicyQuery::GetApiType()
33 {
34     return IPlugin::ApiType::PUBLIC;
35 }
36 
IsPolicySaved()37 bool IPolicyQuery::IsPolicySaved()
38 {
39     return true;
40 }
41 
GetBoolPolicy(const std::string & policyData,MessageParcel & reply)42 ErrCode IPolicyQuery::GetBoolPolicy(const std::string &policyData, MessageParcel &reply)
43 {
44     bool policy = false;
45     BoolSerializer::GetInstance()->Deserialize(policyData, policy);
46     EDMLOGI("IPolicyQuery OnGetPolicy paramKey result %{public}d", policy);
47     reply.WriteInt32(ERR_OK);
48     reply.WriteBool(policy);
49     return ERR_OK;
50 }
51 
GetArrayStringPolicy(const std::string & policyData,MessageParcel & reply)52 ErrCode IPolicyQuery::GetArrayStringPolicy(const std::string &policyData, MessageParcel &reply)
53 {
54     std::vector<std::string> policy;
55     ArrayStringSerializer::GetInstance()->Deserialize(policyData, policy);
56     reply.WriteInt32(ERR_OK);
57     reply.WriteStringVector(policy);
58     return ERR_OK;
59 }
60 
GetPolicy(std::shared_ptr<PolicyManager> policyManager,uint32_t code,MessageParcel & data,MessageParcel & reply,int32_t userId)61 ErrCode IPolicyQuery::GetPolicy(std::shared_ptr<PolicyManager> policyManager, uint32_t code, MessageParcel &data,
62     MessageParcel &reply, int32_t userId)
63 {
64     EDMLOGW("IPolicyQuery: GetPolicy start");
65     std::string permissionTag = data.ReadString();
66     ErrCode systemCallingCheck =
67         PermissionChecker::GetInstance()->CheckSystemCalling(this->GetApiType(), permissionTag);
68     if (FAILED(systemCallingCheck)) {
69         return systemCallingCheck;
70     }
71     EDMLOGW("IPolicyQuery: GetPolicy read want");
72     AppExecFwk::ElementName elementName;
73     if (data.ReadInt32() == 0) {
74         std::unique_ptr<AppExecFwk::ElementName> admin(data.ReadParcelable<AppExecFwk::ElementName>());
75         if (!admin) {
76             EDMLOGW("GetDevicePolicy: ReadParcelable failed");
77             return EdmReturnErrCode::PARAM_ERROR;
78         }
79         std::shared_ptr<Admin> deviceAdmin = AdminManager::GetInstance()->GetAdminByPkgName(admin->GetBundleName(),
80             PermissionChecker::GetInstance()->GetCurrentUserId());
81         if (deviceAdmin == nullptr) {
82             return EdmReturnErrCode::ADMIN_INACTIVE;
83         }
84         IPlugin::PermissionType permissionType =
85             PermissionChecker::GetInstance()->AdminTypeToPermissionType(deviceAdmin->GetAdminType());
86         ErrCode ret = PermissionChecker::GetInstance()->CheckHandlePolicyPermission(FuncOperateType::GET,
87             admin->GetBundleName(), this->GetPolicyName(), this->GetPermission(permissionType, permissionTag), userId);
88         if (FAILED(ret)) {
89             return ret;
90         }
91         elementName.SetBundleName(admin->GetBundleName());
92         elementName.SetAbilityName(admin->GetAbilityName());
93     } else {
94         if (!PermissionChecker::GetInstance()->CheckElementNullPermission(code,
95             this->GetPermission(IPlugin::PermissionType::SUPER_DEVICE_ADMIN, permissionTag))) {
96             EDMLOGE("IPolicyQuery: permission check failed");
97             return EdmReturnErrCode::PERMISSION_DENIED;
98         }
99     }
100 
101     std::string policyName = this->GetPolicyName();
102     std::string policyValue;
103     if (this->IsPolicySaved()) {
104         policyManager->GetPolicy(elementName.GetBundleName(), policyName, policyValue, userId);
105     }
106     ErrCode getRet = this->QueryPolicy(policyValue, data, reply, userId);
107     ReportInfo reportInfo = ReportInfo(FuncCodeUtils::GetOperateType(code), policyName, std::to_string(getRet));
108     SecurityReport::ReportSecurityInfo(elementName.GetBundleName(), elementName.GetAbilityName(), reportInfo, true);
109     return getRet;
110 }
111 } // namespace EDM
112 } // namespace OHOS