• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "system_ability.h"
32 
33 namespace OHOS {
34 namespace EDM {
35 class EnterpriseDeviceMgrAbility : public SystemAbility, public EnterpriseDeviceMgrStub {
36     DECLARE_SYSTEM_ABILITY(EnterpriseDeviceMgrAbility);
37 
38 public:
39     using CommonEventCallbackFunc = void (EnterpriseDeviceMgrAbility::*)(const EventFwk::CommonEventData &data);
40     using AddSystemAbilityFunc =
41         void (EnterpriseDeviceMgrAbility::*)(int32_t systemAbilityId, const std::string &deviceId);
42     EnterpriseDeviceMgrAbility();
43     DISALLOW_COPY_AND_MOVE(EnterpriseDeviceMgrAbility);
44     ~EnterpriseDeviceMgrAbility() override;
45     static sptr<EnterpriseDeviceMgrAbility> GetInstance();
46 
47     ErrCode EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type, int32_t userId) override;
48     ErrCode DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId) override;
49     ErrCode DisableSuperAdmin(const std::string &bundleName) override;
50     ErrCode HandleDevicePolicy(uint32_t code, AppExecFwk::ElementName &admin, MessageParcel &data, MessageParcel &reply,
51         int32_t userId) override;
52     ErrCode GetDevicePolicy(uint32_t code, MessageParcel &data, MessageParcel &reply, int32_t userId) override;
53     ErrCode GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList) override;
54     ErrCode GetEnterpriseInfo(AppExecFwk::ElementName &admin, MessageParcel &reply) override;
55     ErrCode SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo) override;
56     ErrCode SubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override;
57     ErrCode UnsubscribeManagedEvent(const AppExecFwk::ElementName &admin, const std::vector<uint32_t> &events) override;
58     ErrCode AuthorizeAdmin(const AppExecFwk::ElementName &admin, const std::string &bundleName) override;
59     bool IsSuperAdmin(const std::string &bundleName) override;
60     bool IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId) override;
61     void ConnectAbilityOnSystemEvent(const std::string &bundleName, ManagedEvent event);
62     std::unordered_map<std::string, CommonEventCallbackFunc> commonEventFuncMap_;
63     std::unordered_map<int32_t, AddSystemAbilityFunc> addSystemAbilityFuncMap_;
64 
65 protected:
66     void OnStart() override;
67     void OnStop() override;
68     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
69     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
70     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
71 
72 private:
73     bool IsHdc();
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);
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 OnCommonEventServiceStart(int32_t systemAbilityId, const std::string &deviceId);
106     std::shared_ptr<PolicyManager> GetAndSwitchPolicyManagerByUserId(int32_t userId);
107     void InitAllPolices();
108 
109     static std::mutex mutexLock_;
110     static sptr<EnterpriseDeviceMgrAbility> instance_;
111     std::shared_ptr<PolicyManager> policyMgr_;
112     std::map<std::int32_t, std::shared_ptr<PolicyManager>> policyMgrMap_;
113     std::shared_ptr<AdminManager> adminMgr_;
114     std::shared_ptr<PluginManager> pluginMgr_;
115     bool registerToService_ = false;
116     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber = nullptr;
117     sptr<AppExecFwk::IApplicationStateObserver> appStateObserver_;
118 };
119 class EnterpriseDeviceEventSubscriber : public EventFwk::CommonEventSubscriber {
120 public:
121     EnterpriseDeviceEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
122         EnterpriseDeviceMgrAbility &listener);
123     ~EnterpriseDeviceEventSubscriber() override = default;
124 
125     void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
126 
127 private:
128     EnterpriseDeviceMgrAbility &listener_;
129 };
130 } // namespace EDM
131 } // namespace OHOS
132 #endif // SERVICES_EDM_INCLUDE_EDM_ENTERPRISE_DEVICE_MGR_ABILITY_H
133