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, "disallow_running_bundles", config, true);
47 ptr->SetSerializer(ArrayStringSerializer::GetInstance());
48 ptr->SetOnHandlePolicyListener(&DisallowedRunningBundlesPlugin::OnBasicSetPolicy, FuncOperateType::SET);
49 ptr->SetOnHandlePolicyListener(&DisallowedRunningBundlesPlugin::OnBasicRemovePolicy, FuncOperateType::REMOVE);
50 ptr->SetOnAdminRemoveListener(&DisallowedRunningBundlesPlugin::OnBasicAdminRemove);
51 maxListSize_ = EdmConstants::APPID_MAX_SIZE;
52 }
53
SetOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)54 ErrCode DisallowedRunningBundlesPlugin::SetOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
55 std::vector<std::string> &failedData)
56 {
57 EDMLOGI("DisallowedRunningBundlesPlugin OnSetPolicy userId = %{public}d", userId);
58 std::vector<AppExecFwk::AppRunningControlRule> controlRules;
59 std::for_each(data.begin(), data.end(), [&](const std::string &str) {
60 AppExecFwk::AppRunningControlRule controlRule;
61 controlRule.appId = str;
62 controlRules.push_back(controlRule);
63 });
64
65 auto appControlProxy = GetAppControlProxy();
66 if (!appControlProxy) {
67 EDMLOGE("DisallowedRunningBundlesPlugin OnSetPolicy GetAppControlProxy failed.");
68 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
69 }
70 ErrCode res = appControlProxy->AddAppRunningControlRule(controlRules, userId);
71 if (res != ERR_OK) {
72 EDMLOGE("DisallowedRunningBundlesPlugin OnSetPolicyDone Faild %{public}d:", res);
73 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
74 }
75 return ERR_OK;
76 }
77
RemoveOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)78 ErrCode DisallowedRunningBundlesPlugin::RemoveOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
79 std::vector<std::string> &failedData)
80 {
81 EDMLOGD("DisallowedRunningBundlesPlugin OnRemovePolicy userId : %{public}d:", userId);
82 std::vector<AppExecFwk::AppRunningControlRule> controlRules;
83 std::for_each(data.begin(), data.end(), [&](const std::string &str) {
84 AppExecFwk::AppRunningControlRule controlRule;
85 controlRule.appId = str;
86 controlRules.push_back(controlRule);
87 });
88 auto appControlProxy = GetAppControlProxy();
89 if (!appControlProxy) {
90 EDMLOGE("DisallowedRunningBundlesPlugin OnRemovePolicy GetAppControlProxy failed.");
91 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
92 }
93 ErrCode res = appControlProxy->DeleteAppRunningControlRule(controlRules, userId);
94 if (res != ERR_OK) {
95 EDMLOGE("DisallowedRunningBundlesPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res);
96 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
97 }
98 return ERR_OK;
99 }
100
GetAppControlProxy()101 sptr<AppExecFwk::IAppControlMgr> DisallowedRunningBundlesPlugin::GetAppControlProxy()
102 {
103 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
104 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
105 return proxy->GetAppControlProxy();
106 }
107 } // namespace EDM
108 } // namespace OHOS
109