• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "disallowed_running_bundles_plugin.h"
17 
18 #include <system_ability_definition.h>
19 
20 #include "app_control/app_control_proxy.h"
21 #include "array_string_serializer.h"
22 #include "edm_constants.h"
23 #include "edm_ipc_interface_code.h"
24 #include "edm_sys_manager.h"
25 #include "iplugin_manager.h"
26 
27 namespace OHOS {
28 namespace EDM {
29 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedRunningBundlesPlugin::GetPlugin());
30 
InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedRunningBundlesPlugin,std::vector<std::string>>> ptr)31 void DisallowedRunningBundlesPlugin::InitPlugin(
32     std::shared_ptr<IPluginTemplate<DisallowedRunningBundlesPlugin, std::vector<std::string>>> ptr)
33 {
34     EDMLOGI("DisallowedRunningBundlesPlugin InitPlugin...");
35     std::map<std::string, std::map<IPlugin::PermissionType, std::string>> tagPermissions;
36     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag11;
37     std::map<IPlugin::PermissionType, std::string> typePermissionsForTag12;
38     typePermissionsForTag11.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
39         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_SET_APP_RUNNING_POLICY);
40     typePermissionsForTag12.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
41         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_APPLICATION);
42     tagPermissions.emplace(EdmConstants::PERMISSION_TAG_VERSION_11, typePermissionsForTag11);
43     tagPermissions.emplace(EdmConstants::PERMISSION_TAG_VERSION_12, typePermissionsForTag12);
44 
45     IPlugin::PolicyPermissionConfig config = IPlugin::PolicyPermissionConfig(tagPermissions, IPlugin::ApiType::PUBLIC);
46     ptr->InitAttribute(EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES, PolicyName::POLICY_DISALLOW_RUNNING_BUNDLES,
47         config, true);
48     ptr->SetSerializer(ArrayStringSerializer::GetInstance());
49     ptr->SetOnHandlePolicyListener(&DisallowedRunningBundlesPlugin::OnBasicSetPolicy, FuncOperateType::SET);
50     ptr->SetOnHandlePolicyListener(&DisallowedRunningBundlesPlugin::OnBasicRemovePolicy, FuncOperateType::REMOVE);
51     ptr->SetOnAdminRemoveListener(&DisallowedRunningBundlesPlugin::OnBasicAdminRemove);
52     maxListSize_ = EdmConstants::APPID_MAX_SIZE;
53 }
54 
SetOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)55 ErrCode DisallowedRunningBundlesPlugin::SetOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
56     std::vector<std::string> &failedData)
57 {
58     EDMLOGI("DisallowedRunningBundlesPlugin OnSetPolicy");
59     std::vector<AppExecFwk::AppRunningControlRule> controlRules;
60     std::for_each(data.begin(), data.end(), [&](const std::string &str) {
61         AppExecFwk::AppRunningControlRule controlRule;
62         controlRule.appId = str;
63         controlRules.push_back(controlRule);
64     });
65 
66     auto appControlProxy = GetAppControlProxy();
67     if (!appControlProxy) {
68         EDMLOGE("DisallowedRunningBundlesPlugin OnSetPolicy GetAppControlProxy failed.");
69         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
70     }
71     ErrCode res = appControlProxy->AddAppRunningControlRule(controlRules, userId);
72     if (res != ERR_OK) {
73         EDMLOGE("DisallowedRunningBundlesPlugin OnSetPolicyDone Faild %{public}d:", res);
74         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
75     }
76     return ERR_OK;
77 }
78 
RemoveOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)79 ErrCode DisallowedRunningBundlesPlugin::RemoveOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
80     std::vector<std::string> &failedData)
81 {
82     EDMLOGD("DisallowedRunningBundlesPlugin OnRemovePolicy");
83     std::vector<AppExecFwk::AppRunningControlRule> controlRules;
84     std::for_each(data.begin(), data.end(), [&](const std::string &str) {
85         AppExecFwk::AppRunningControlRule controlRule;
86         controlRule.appId = str;
87         controlRules.push_back(controlRule);
88     });
89     auto appControlProxy = GetAppControlProxy();
90     if (!appControlProxy) {
91         EDMLOGE("DisallowedRunningBundlesPlugin OnRemovePolicy GetAppControlProxy failed.");
92         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
93     }
94     ErrCode res = appControlProxy->DeleteAppRunningControlRule(controlRules, userId);
95     if (res != ERR_OK) {
96         EDMLOGE("DisallowedRunningBundlesPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res);
97         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
98     }
99     return ERR_OK;
100 }
101 
GetAppControlProxy()102 sptr<AppExecFwk::IAppControlMgr> DisallowedRunningBundlesPlugin::GetAppControlProxy()
103 {
104     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
105     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
106     return proxy->GetAppControlProxy();
107 }
108 } // namespace EDM
109 } // namespace OHOS
110