1 /* 2 * Copyright (c) 2022-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 #ifndef SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H 17 #define SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H 18 19 #include <bundle_mgr_interface.h> 20 21 #include <string> 22 23 #include "admin_manager.h" 24 #include "app_mgr_interface.h" 25 #include "common_event_subscriber.h" 26 #include "enterprise_admin_proxy.h" 27 #include "enterprise_device_mgr_stub.h" 28 #include "hilog/log.h" 29 #include "plugin_manager.h" 30 #include "policy_manager.h" 31 #include "policy_struct.h" 32 #include "system_ability.h" 33 34 namespace OHOS { 35 namespace EDM { 36 class EnterpriseDeviceMgrAbility : public SystemAbility, public EnterpriseDeviceMgrStub { 37 DECLARE_SYSTEM_ABILITY(EnterpriseDeviceMgrAbility); 38 39 public: 40 using CommonEventCallbackFunc = void (EnterpriseDeviceMgrAbility::*)(const EventFwk::CommonEventData &data); 41 using AddSystemAbilityFunc = 42 void (EnterpriseDeviceMgrAbility::*)(int32_t systemAbilityId, const std::string &deviceId); 43 EnterpriseDeviceMgrAbility(); 44 DISALLOW_COPY_AND_MOVE(EnterpriseDeviceMgrAbility); 45 ~EnterpriseDeviceMgrAbility() override; 46 static sptr<EnterpriseDeviceMgrAbility> GetInstance(); 47 48 ErrCode EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type, int32_t userId) override; 49 ErrCode DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId) override; 50 ErrCode DisableSuperAdmin(const std::string &bundleName) override; 51 ErrCode HandleDevicePolicy(uint32_t code, AppExecFwk::ElementName &admin, MessageParcel &data, MessageParcel &reply, 52 int32_t userId) override; 53 ErrCode GetDevicePolicy(uint32_t code, MessageParcel &data, MessageParcel &reply, int32_t userId) override; 54 ErrCode GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList) override; 55 ErrCode GetEnterpriseInfo(AppExecFwk::ElementName &admin, MessageParcel &reply) override; 56 ErrCode SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo) override; 57 ErrCode SubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override; 58 ErrCode UnsubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override; 59 ErrCode AuthorizeAdmin(const AppExecFwk::ElementName &admin, const std::string &bundleName) override; 60 bool IsSuperAdmin(const std::string &bundleName) override; 61 bool IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId) override; 62 void ConnectAbilityOnSystemEvent(const std::string &bundleName, ManagedEvent event); 63 std::unordered_map<std::string, CommonEventCallbackFunc> commonEventFuncMap_; 64 std::unordered_map<int32_t, AddSystemAbilityFunc> addSystemAbilityFuncMap_; 65 66 protected: 67 void OnStart() override; 68 void OnStop() override; 69 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 70 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 71 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 72 73 private: 74 void AddCommonEventFuncMap(); 75 void AddOnAddSystemAbilityFuncMap(); 76 bool SubscribeAppState(); 77 bool UnsubscribeAppState(); 78 ErrCode CheckCallingUid(const std::string &bundleName); 79 ErrCode RemoveAdminItem(const std::string &adminName, const std::string &policyName, const std::string &policyValue, 80 int32_t userId); 81 ErrCode RemoveAdmin(const std::string &adminName, int32_t userId); 82 ErrCode RemovePolicyAndAdmin(const std::string &bundleName); 83 ErrCode GetAllPermissionsByAdmin(const std::string &bundleInfoName, std::vector<std::string> &permissionList, 84 int32_t userId); 85 int32_t GetCurrentUserId(); 86 ErrCode HandleApplicationEvent(const std::vector<uint32_t> &events, bool subscribe); 87 ErrCode UpdateDeviceAdmin(AppExecFwk::ElementName &admin); 88 ErrCode VerifyEnableAdminCondition(AppExecFwk::ElementName &admin, AdminType type, int32_t userId); 89 ErrCode VerifyManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events); 90 ErrCode UpdateDevicePolicy(uint32_t code, AppExecFwk::ElementName &admin, MessageParcel &data, MessageParcel &reply, 91 int32_t userId); 92 ErrCode CheckGetPolicyPermission(MessageParcel &data, MessageParcel &reply, const std::string &getPermission, 93 std::string &adminName, const int32_t userId); 94 bool VerifyCallingPermission(const std::string &permissionName); 95 sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr(); 96 sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr(); 97 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateEnterpriseDeviceEventSubscriber( 98 EnterpriseDeviceMgrAbility &listener); 99 void OnCommonEventUserRemoved(const EventFwk::CommonEventData &data); 100 void OnCommonEventPackageAdded(const EventFwk::CommonEventData &data); 101 void OnCommonEventPackageRemoved(const EventFwk::CommonEventData &data); 102 bool ShouldUnsubscribeAppState(const std::string &adminName, int32_t userId); 103 bool CheckManagedEvent(uint32_t event); 104 void OnAppManagerServiceStart(int32_t systemAbilityId, const std::string &deviceId); 105 void OnAbilityManagerServiceStart(int32_t systemAbilityId, const std::string &deviceId); 106 void OnCommonEventServiceStart(int32_t systemAbilityId, const std::string &deviceId); 107 void CreateSecurityContent(std::shared_ptr<Admin> deviceAdmin, std::shared_ptr<IPlugin> plugin); 108 std::shared_ptr<PolicyManager> GetAndSwitchPolicyManagerByUserId(int32_t userId); 109 void InitAllPolices(); 110 void ConnectAbilityOnSystemUpdate(const UpdateInfo &updateInfo); 111 void OnCommonEventSystemUpdate(const EventFwk::CommonEventData &data); 112 113 static std::mutex mutexLock_; 114 static sptr<EnterpriseDeviceMgrAbility> instance_; 115 std::shared_ptr<PolicyManager> policyMgr_; 116 std::map<std::int32_t, std::shared_ptr<PolicyManager>> policyMgrMap_; 117 std::shared_ptr<AdminManager> adminMgr_; 118 std::shared_ptr<PluginManager> pluginMgr_; 119 bool registerToService_ = false; 120 std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber = nullptr; 121 sptr<AppExecFwk::IApplicationStateObserver> appStateObserver_; 122 }; 123 class EnterpriseDeviceEventSubscriber : public EventFwk::CommonEventSubscriber { 124 public: 125 EnterpriseDeviceEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, 126 EnterpriseDeviceMgrAbility &listener); 127 ~EnterpriseDeviceEventSubscriber() override = default; 128 129 void OnReceiveEvent(const EventFwk::CommonEventData &data) override; 130 131 private: 132 EnterpriseDeviceMgrAbility &listener_; 133 }; 134 } // namespace EDM 135 } // namespace OHOS 136 #endif // SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H 137