1 /*
2  * Copyright (c) 2021 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 "common_event_permission_manager.h"
17 
18 #include <algorithm>
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include "common_event_support.h"
25 #include "event_log_wrapper.h"
26 
27 namespace OHOS {
28 namespace EventFwk {
29 constexpr size_t REVERSE = 3;
30 
31 constexpr int32_t PHONE_UID = 1001;
32 constexpr int32_t WIFI_UID = 1010;
33 constexpr int32_t CONNECT_UID = 1099;
34 constexpr int32_t BOOT_UID = 1101;
35 constexpr int32_t TIME_UID = 3013;
36 constexpr int32_t ACCOUNT_UID = 3058;
37 constexpr int32_t TIME_TICK_UID = 3819;
38 constexpr int32_t BMS_UID = 5523;
39 constexpr int32_t POWER_UID = 5528;
40 constexpr int32_t BROKER_UID = 5557;
41 
42 static const std::unordered_map<std::string, std::pair<PermissionState, std::vector<std::string>>> COMMON_EVENT_MAP {
43     {CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED,
44         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
45     },
46     {CommonEventSupport::COMMON_EVENT_LOCKED_BOOT_COMPLETED,
47         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
48     },
49     {CommonEventSupport::COMMON_EVENT_USER_SWITCHED,
50         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
51     },
52     {CommonEventSupport::COMMON_EVENT_USER_STARTING,
53         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
54     },
55     {CommonEventSupport::COMMON_EVENT_USER_STOPPING,
56         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
57     },
58     {CommonEventSupport::COMMON_EVENT_WIFI_SCAN_FINISHED,
59         {PermissionState::DEFAULT, {"ohos.permission.LOCATION"}}
60     },
61     {CommonEventSupport::COMMON_EVENT_WIFI_RSSI_VALUE,
62         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
63     },
64     {CommonEventSupport::COMMON_EVENT_WIFI_AP_STA_JOIN,
65         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
66     },
67     {CommonEventSupport::COMMON_EVENT_WIFI_AP_STA_LEAVE,
68         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
69     },
70     {CommonEventSupport::COMMON_EVENT_WIFI_MPLINK_STATE_CHANGE,
71         {PermissionState::DEFAULT, {"ohos.permission.MPLINK_CHANGE_STATE"}}
72     },
73     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_CONN_STATE,
74         {PermissionState::AND, {"ohos.permission.GET_WIFI_INFO", "ohos.permission.LOCATION"}}
75     },
76     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_STATE_CHANGED,
77         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
78     },
79     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_PEERS_STATE_CHANGED,
80         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
81     },
82     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_PEERS_DISCOVERY_STATE_CHANGED,
83         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
84     },
85     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_CURRENT_DEVICE_STATE_CHANGED,
86         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
87     },
88     {CommonEventSupport::COMMON_EVENT_WIFI_P2P_GROUP_STATE_CHANGED,
89         {PermissionState::DEFAULT, {"ohos.permission.GET_WIFI_INFO"}}
90     },
91     {CommonEventSupport::COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED,
92         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SECURE_SETTINGS"}}
93     },
94     {CommonEventSupport::COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED,
95         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SECURE_SETTINGS"}}
96     },
97     {CommonEventSupport::COMMON_EVENT_USER_ADDED,
98         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
99     },
100     {CommonEventSupport::COMMON_EVENT_USER_REMOVED,
101         {PermissionState::DEFAULT, {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"}}
102     },
103     {CommonEventSupport::COMMON_EVENT_ABILITY_ADDED,
104         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
105     },
106     {CommonEventSupport::COMMON_EVENT_ABILITY_REMOVED,
107         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
108     },
109     {CommonEventSupport::COMMON_EVENT_ABILITY_UPDATED,
110         {PermissionState::DEFAULT, {"ohos.permission.LISTEN_BUNDLE_CHANGE"}}
111     },
112     {CommonEventSupport::COMMON_EVENT_DISK_REMOVED,
113         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
114     },
115     {CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTED,
116         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
117     },
118     {CommonEventSupport::COMMON_EVENT_DISK_MOUNTED,
119         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
120     },
121     {CommonEventSupport::COMMON_EVENT_DISK_BAD_REMOVAL,
122         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
123     },
124     {CommonEventSupport::COMMON_EVENT_DISK_UNMOUNTABLE,
125         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
126     },
127     {CommonEventSupport::COMMON_EVENT_DISK_EJECT,
128         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
129     },
130     {CommonEventSupport::COMMON_EVENT_VOLUME_REMOVED,
131         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
132     },
133     {CommonEventSupport::COMMON_EVENT_VOLUME_UNMOUNTED,
134         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
135     },
136     {CommonEventSupport::COMMON_EVENT_VOLUME_MOUNTED,
137         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
138     },
139     {CommonEventSupport::COMMON_EVENT_VOLUME_BAD_REMOVAL,
140         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
141     },
142     {CommonEventSupport::COMMON_EVENT_VOLUME_EJECT,
143         {PermissionState::DEFAULT, {"ohos.permission.STORAGE_MANAGER"}}
144     },
145     {CommonEventSupport::COMMON_EVENT_VISIBLE_ACCOUNTS_UPDATED,
146             {PermissionState::DEFAULT, {"ohos.permission.GET_APP_ACCOUNTS"}}
147     },
148     {CommonEventSupport::COMMON_EVENT_ACCOUNT_DELETED,
149         {PermissionState::DEFAULT, {"ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS"}}
150     },
151     {CommonEventSupport::COMMON_EVENT_FOUNDATION_READY,
152         {PermissionState::DEFAULT, {"ohos.permission.RECEIVER_STARTUP_COMPLETED"}}
153     },
154     {CommonEventSupport::COMMON_EVENT_SLOT_CHANGE,
155         {PermissionState::DEFAULT, {"ohos.permission.NOTIFICATION_CONTROLLER"}}
156     },
157     {CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED,
158         {PermissionState::DEFAULT, {"ohos.permission.RECEIVE_SMS"}}
159     },
160     {CommonEventSupport::COMMON_EVENT_BUNDLE_RESOURCES_CHANGED,
161         {PermissionState::DEFAULT, {"ohos.permission.GET_BUNDLE_RESOURCES"}}
162     },
163     {CommonEventSupport::COMMON_EVENT_VPN_CONNECTION_STATUS_CHANGED,
164         {PermissionState::DEFAULT, {"ohos.permission.GET_NETWORK_INFO"}}
165     },
166     {CommonEventSupport::COMMON_EVENT_RESTORE_START,
167         {PermissionState::DEFAULT, {"ohos.permission.START_RESTORE_NOTIFICATION"}}
168     },
169 };
170 
171 static const std::unordered_set<std::string> SYSTEM_API_COMMON_EVENTS {
172     CommonEventSupport::COMMON_EVENT_DOMAIN_ACCOUNT_STATUS_CHANGED,
173     CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_EXEMPTION_LIST_UPDATED,
174     CommonEventSupport::COMMON_EVENT_PACKAGE_INSTALLATION_STARTED,
175     CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED,
176     CommonEventSupport::COMMON_EVENT_DYNAMIC_ICON_CHANGED,
177     CommonEventSupport::COMMON_EVENT_RESTORE_START
178 };
179 
180 static const std::vector<std::string> SENSITIVE_COMMON_EVENTS {
181     CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED
182 };
183 
CommonEventPermissionManager()184 CommonEventPermissionManager::CommonEventPermissionManager()
185 {
186     Init();
187 }
188 
Init()189 void CommonEventPermissionManager::Init()
190 {
191     EVENT_LOGD("enter");
192     Permission per;
193     per.names.reserve(REVERSE);
194 
195     for (auto &[eventName, permissions] : COMMON_EVENT_MAP) {
196         per.state = permissions.first;
197         for (auto &permissionName : permissions.second) {
198             per.names.emplace_back(permissionName);
199         }
200         if (IsSensitiveEvent(eventName)) {
201             per.isSensitive = true;
202         }
203         eventMap_.insert(std::make_pair(eventName, per));
204         per.names.clear();
205     }
206 }
207 
GetEventPermission(const std::string & event)208 Permission __attribute__((weak)) CommonEventPermissionManager::GetEventPermission(const std::string &event)
209 {
210     EVENT_LOGD("enter");
211     if (eventMap_.find(event) != eventMap_.end()) {
212         return eventMap_.find(event)->second;
213     }
214     Permission per;
215     return per;
216 }
217 
IsSensitiveEvent(const std::string & event)218 bool CommonEventPermissionManager::IsSensitiveEvent(const std::string &event)
219 {
220     auto it = find(SENSITIVE_COMMON_EVENTS.begin(), SENSITIVE_COMMON_EVENTS.end(), event);
221     return it != SENSITIVE_COMMON_EVENTS.end();
222 }
223 
IsSystemAPIEvent(const std::string & event)224 bool CommonEventPermissionManager::IsSystemAPIEvent(const std::string &event)
225 {
226     return SYSTEM_API_COMMON_EVENTS.find(event) != SYSTEM_API_COMMON_EVENTS.end();
227 }
228 }  // namespace EventFwk
229 }  // namespace OHOS
230