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 #include "access_token_helper.h"
27
28 namespace OHOS {
29 namespace EventFwk {
30 constexpr size_t REVERSE = 3;
31
32 constexpr int32_t PHONE_UID = 1001;
33 constexpr int32_t WIFI_UID = 1010;
34 constexpr int32_t CONNECT_UID = 1099;
35 constexpr int32_t BOOT_UID = 1101;
36 constexpr int32_t TIME_UID = 3013;
37 constexpr int32_t ACCOUNT_UID = 3058;
38 constexpr int32_t TIME_TICK_UID = 3819;
39 constexpr int32_t BMS_UID = 5523;
40 constexpr int32_t POWER_UID = 5528;
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.GET_WIFI_PEERS_MAC"}}
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.GET_WIFI_PEERS_MAC"}}
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 {CommonEventSupport::COMMON_EVENT_DEFAULT_APPLICATION_CHANGED,
170 {PermissionState::DEFAULT, {"ohos.permission.CHANGE_DEFAULT_APPLICATION"}}
171 },
172 {CommonEventSupport::COMMON_EVENT_SHORTCUT_CHANGED,
173 {PermissionState::DEFAULT, {"ohos.permission.MANAGE_SHORTCUTS"}}
174 },
175 {CommonEventSupport::COMMON_EVENT_TRUSTED_RING_CHECKIN,
176 {PermissionState::DEFAULT, {"ohos.permission.USE_TRUSTED_RING"}}
177 },
178 {CommonEventSupport::COMMON_EVENT_TRUSTED_RING_CHECKOUT,
179 {PermissionState::DEFAULT, {"ohos.permission.USE_TRUSTED_RING"}}
180 },
181 {CommonEventSupport::COMMON_EVENT_TRUSTED_RING_RESET,
182 {PermissionState::DEFAULT, {"ohos.permission.USE_TRUSTED_RING"}}
183 },
184 };
185
186 static const std::unordered_set<std::string> SYSTEM_API_COMMON_EVENTS {
187 CommonEventSupport::COMMON_EVENT_DOMAIN_ACCOUNT_STATUS_CHANGED,
188 CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_EXEMPTION_LIST_UPDATED,
189 CommonEventSupport::COMMON_EVENT_PACKAGE_INSTALLATION_STARTED,
190 CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED,
191 CommonEventSupport::COMMON_EVENT_DYNAMIC_ICON_CHANGED,
192 CommonEventSupport::COMMON_EVENT_RESTORE_START,
193 CommonEventSupport::COMMON_EVENT_DEFAULT_APPLICATION_CHANGED,
194 CommonEventSupport::COMMON_EVENT_SHORTCUT_CHANGED,
195 CommonEventSupport::COMMON_EVENT_TRUSTED_RING_CHECKIN,
196 CommonEventSupport::COMMON_EVENT_TRUSTED_RING_CHECKOUT,
197 CommonEventSupport::COMMON_EVENT_TRUSTED_RING_RESET,
198 CommonEventSupport::COMMON_EVENT_USER_LOCKING,
199 CommonEventSupport::COMMON_EVENT_USER_LOCKED,
200 CommonEventSupport::COMMON_EVENT_CUSTOM_CONFIG_POLICY_UPDATED,
201 CommonEventSupport::COMMON_EVENT_CUSTOM_ROAMING_REGION_UPDATED,
202 CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED
203 };
204
205 static const std::vector<std::string> SENSITIVE_COMMON_EVENTS {
206 CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED
207 };
208
CommonEventPermissionManager()209 CommonEventPermissionManager::CommonEventPermissionManager()
210 {
211 Init();
212 }
213
Init()214 void CommonEventPermissionManager::Init()
215 {
216 EVENT_LOGD("enter");
217 Permission per;
218 per.names.reserve(REVERSE);
219
220 for (auto &[eventName, permissions] : COMMON_EVENT_MAP) {
221 per.state = permissions.first;
222 for (auto &permissionName : permissions.second) {
223 per.names.emplace_back(permissionName);
224 }
225 if (IsSensitiveEvent(eventName)) {
226 per.isSensitive = true;
227 }
228 eventMap_.insert(std::make_pair(eventName, per));
229 per.names.clear();
230 }
231 }
232
GetEventPermission(const std::string & event)233 Permission __attribute__((weak)) CommonEventPermissionManager::GetEventPermission(const std::string &event)
234 {
235 EVENT_LOGD("enter");
236 if (eventMap_.find(event) != eventMap_.end()) {
237 return eventMap_.find(event)->second;
238 }
239 Permission per;
240 return per;
241 }
242
IsSensitiveEvent(const std::string & event)243 bool CommonEventPermissionManager::IsSensitiveEvent(const std::string &event)
244 {
245 auto it = find(SENSITIVE_COMMON_EVENTS.begin(), SENSITIVE_COMMON_EVENTS.end(), event);
246 return it != SENSITIVE_COMMON_EVENTS.end();
247 }
248
IsSystemAPIEvent(const std::string & event)249 bool CommonEventPermissionManager::IsSystemAPIEvent(const std::string &event)
250 {
251 return SYSTEM_API_COMMON_EVENTS.find(event) != SYSTEM_API_COMMON_EVENTS.end();
252 }
253 } // namespace EventFwk
254 } // namespace OHOS
255