1 /*
2 * Copyright (c) 2021-2024 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 "net_policy_service.h"
17
18 #include <algorithm>
19 #include <dlfcn.h>
20
21 #include "system_ability_definition.h"
22
23 #include "bundle_constants.h"
24 #include "bundle_mgr_proxy.h"
25 #include "iservice_registry.h"
26 #include "net_manager_center.h"
27 #include "net_manager_constants.h"
28 #include "net_mgr_log_wrapper.h"
29 #include "net_policy_base.h"
30 #include "net_policy_constants.h"
31 #include "net_policy_core.h"
32 #include "net_policy_file.h"
33 #include "net_policy_inner_define.h"
34 #include "net_quota_policy.h"
35 #include "net_settings.h"
36 #include "netmanager_base_permission.h"
37 #include "system_ability_definition.h"
38 #include "net_policy_listener.h"
39 #include "net_access_policy_dialog.h"
40
41 #ifdef __LP64__
42 const std::string LIB_LOAD_PATH = "/system/lib64/libnet_access_policy_dialog.z.so";
43 #else
44 const std::string LIB_LOAD_PATH = "/system/lib/libnet_access_policy_dialog.z.so";
45 #endif
46
47 using GetNetBundleClass = OHOS::NetManagerStandard::INetAccessPolicyDialog *(*)();
48 namespace OHOS {
49 namespace NetManagerStandard {
50 static std::atomic<bool> g_RegisterToService(
51 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetPolicyService>::GetInstance().get()));
52
NetPolicyService()53 NetPolicyService::NetPolicyService()
54 : SystemAbility(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID, true), state_(STATE_STOPPED)
55 {
56 }
57
58 NetPolicyService::~NetPolicyService() = default;
59
OnStart()60 void NetPolicyService::OnStart()
61 {
62 NETMGR_LOG_I("OnStart");
63 if (state_ == STATE_RUNNING) {
64 NETMGR_LOG_W("NetPolicyService already start.");
65 return;
66 }
67
68 if (!g_RegisterToService) {
69 g_RegisterToService =
70 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetPolicyService>::GetInstance().get());
71 if (!g_RegisterToService) {
72 NETMGR_LOG_E("Register to local sa manager failed again, give up.");
73 return;
74 }
75 }
76
77 state_ = STATE_RUNNING;
78 Init();
79 }
80
OnStop()81 void NetPolicyService::OnStop()
82 {
83 handler_.reset();
84 netPolicyCore_.reset();
85 netPolicyCallback_.reset();
86 netPolicyTraffic_.reset();
87 netPolicyFirewall_.reset();
88 netPolicyRule_.reset();
89 state_ = STATE_STOPPED;
90 g_RegisterToService = false;
91 }
92
Dump(int32_t fd,const std::vector<std::u16string> & args)93 int32_t NetPolicyService::Dump(int32_t fd, const std::vector<std::u16string> &args)
94 {
95 NETMGR_LOG_D("Start policy Dump, fd: %{public}d", fd);
96 std::string result;
97 GetDumpMessage(result);
98 int32_t ret = dprintf(fd, "%s\n", result.c_str());
99 return ret < 0 ? NETMANAGER_ERR_PARAMETER_ERROR : NETMANAGER_SUCCESS;
100 }
101
Init()102 void NetPolicyService::Init()
103 {
104 NETMGR_LOG_D("Init");
105 AddSystemAbilityListener(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
106 ffrtQueue_.submit(
107 [this]() {
108 serviceComm_ = (std::make_unique<NetPolicyServiceCommon>()).release();
109 NetManagerCenter::GetInstance().RegisterPolicyService(serviceComm_);
110 netPolicyCore_ = DelayedSingleton<NetPolicyCore>::GetInstance();
111 netPolicyCallback_ = DelayedSingleton<NetPolicyCallback>::GetInstance();
112 netPolicyTraffic_ = netPolicyCore_->CreateCore<NetPolicyTraffic>();
113 netPolicyFirewall_ = netPolicyCore_->CreateCore<NetPolicyFirewall>();
114 netPolicyRule_ = netPolicyCore_->CreateCore<NetPolicyRule>();
115 RegisterFactoryResetCallback();
116 netAccessPolicy_.InitRdbStore();
117 UpdateNetAccessPolicyToMapFromDB();
118 if (!Publish(DelayedSingleton<NetPolicyService>::GetInstance().get())) {
119 NETMGR_LOG_E("Register to sa manager failed");
120 }
121 }, ffrt::task_attr().name("FfrtNetPolicyServiceInit"));
122 }
123
SetPolicyByUid(uint32_t uid,uint32_t policy)124 int32_t NetPolicyService::SetPolicyByUid(uint32_t uid, uint32_t policy)
125 {
126 NETMGR_LOG_I("SetPolicyByUid uid[%{public}d] policy[%{public}d]", uid, policy);
127 if (netPolicyRule_ == nullptr) {
128 NETMGR_LOG_E("netPolicyRule_ is nullptr");
129 return NETMANAGER_ERR_LOCAL_PTR_NULL;
130 }
131 return netPolicyRule_->TransPolicyToRule(uid, policy);
132 }
133
GetPolicyByUid(uint32_t uid,uint32_t & policy)134 int32_t NetPolicyService::GetPolicyByUid(uint32_t uid, uint32_t &policy)
135 {
136 NETMGR_LOG_D("GetPolicyByUid uid[%{public}d]", uid);
137 if (netPolicyRule_ == nullptr) {
138 NETMGR_LOG_E("netPolicyRule_ is nullptr");
139 return NETMANAGER_ERR_LOCAL_PTR_NULL;
140 }
141 return netPolicyRule_->GetPolicyByUid(uid, policy);
142 }
143
GetUidsByPolicy(uint32_t policy,std::vector<uint32_t> & uids)144 int32_t NetPolicyService::GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)
145 {
146 NETMGR_LOG_D("GetUidsByPolicy policy[%{public}d]", policy);
147 if (netPolicyRule_ == nullptr) {
148 NETMGR_LOG_E("netPolicyRule_ is nullptr");
149 return NETMANAGER_ERR_LOCAL_PTR_NULL;
150 }
151 return netPolicyRule_->GetUidsByPolicy(policy, uids);
152 }
153
IsUidNetAllowed(uint32_t uid,bool metered,bool & isAllowed)154 int32_t NetPolicyService::IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)
155 {
156 NETMGR_LOG_I("IsUidNetAllowed uid[%{public}d metered[%{public}d]", uid, metered);
157 if (NetSettings::GetInstance().IsSystem(uid)) {
158 isAllowed = true;
159 return NETMANAGER_SUCCESS;
160 }
161 if (netPolicyRule_ != nullptr) {
162 return netPolicyRule_->IsUidNetAllowed(uid, metered, isAllowed);
163 }
164 return NETMANAGER_ERR_LOCAL_PTR_NULL;
165 }
166
IsUidNetAllowed(uint32_t uid,const std::string & ifaceName,bool & isAllowed)167 int32_t NetPolicyService::IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
168 {
169 NETMGR_LOG_D("IsUidNetAllowed uid[%{public}d ifaceName[%{public}s]", uid, ifaceName.c_str());
170 const auto &vec = netPolicyTraffic_->GetMeteredIfaces();
171 if (std::find(vec.begin(), vec.end(), ifaceName) != vec.end()) {
172 return IsUidNetAllowed(uid, true, isAllowed);
173 }
174 return IsUidNetAllowed(uid, false, isAllowed);
175 }
176
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)177 int32_t NetPolicyService::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
178 {
179 NETMGR_LOG_I("RegisterNetPolicyCallback");
180 if (callback == nullptr) {
181 NETMGR_LOG_E("RegisterNetPolicyCallback parameter callback is null");
182 return NETMANAGER_ERR_LOCAL_PTR_NULL;
183 }
184
185 if (netPolicyCallback_ == nullptr) {
186 NETMGR_LOG_E("netPolicyCallback_ is null");
187 return NETMANAGER_ERR_LOCAL_PTR_NULL;
188 }
189
190 return netPolicyCallback_->RegisterNetPolicyCallbackAsync(callback);
191 }
192
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)193 int32_t NetPolicyService::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
194 {
195 NETMGR_LOG_I("UnregisterNetPolicyCallback");
196 if (callback == nullptr) {
197 NETMGR_LOG_E("UnregisterNetPolicyCallback parameter callback is null");
198 return NETMANAGER_ERR_LOCAL_PTR_NULL;
199 }
200
201 if (netPolicyCallback_ == nullptr) {
202 NETMGR_LOG_E("netPolicyCallback_ is null");
203 return NETMANAGER_ERR_LOCAL_PTR_NULL;
204 }
205 return netPolicyCallback_->UnregisterNetPolicyCallbackAsync(callback);
206 }
207
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)208 int32_t NetPolicyService::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies)
209 {
210 NETMGR_LOG_I("SetNetQuotaPolicies quotaPolicySize[%{public}zd]", quotaPolicies.size());
211 if (netPolicyTraffic_ == nullptr) {
212 NETMGR_LOG_E("netPolicyTraffic_ is nullptr");
213 return NETMANAGER_ERR_LOCAL_PTR_NULL;
214 }
215 return netPolicyTraffic_->UpdateQuotaPolicies(quotaPolicies);
216 }
217
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)218 int32_t NetPolicyService::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies)
219 {
220 NETMGR_LOG_D("GetNetQuotaPolicies begin");
221 if (netPolicyTraffic_ == nullptr) {
222 NETMGR_LOG_E("netPolicyTraffic_ is nullptr");
223 return NETMANAGER_ERR_LOCAL_PTR_NULL;
224 }
225 return netPolicyTraffic_->GetNetQuotaPolicies(quotaPolicies);
226 }
227
ResetPolicies(const std::string & simId)228 int32_t NetPolicyService::ResetPolicies(const std::string &simId)
229 {
230 NETMGR_LOG_I("ResetPolicies begin");
231 if (netPolicyRule_ != nullptr && netPolicyFirewall_ != nullptr && netPolicyTraffic_ != nullptr) {
232 netPolicyRule_->ResetPolicies();
233 netPolicyFirewall_->ResetPolicies();
234 netPolicyTraffic_->ResetPolicies(simId);
235 NETMGR_LOG_I("ResetPolicies end.");
236 return NETMANAGER_SUCCESS;
237 }
238 return NETMANAGER_ERR_LOCAL_PTR_NULL;
239 }
240
SetBackgroundPolicy(bool allow)241 int32_t NetPolicyService::SetBackgroundPolicy(bool allow)
242 {
243 NETMGR_LOG_I("SetBackgroundPolicy allow[%{public}d]", allow);
244 if (netPolicyRule_ == nullptr) {
245 NETMGR_LOG_E("netPolicyRule_ is nullptr");
246 return NETMANAGER_ERR_LOCAL_PTR_NULL;
247 }
248 return netPolicyRule_->SetBackgroundPolicy(allow);
249 }
250
GetBackgroundPolicy(bool & backgroundPolicy)251 int32_t NetPolicyService::GetBackgroundPolicy(bool &backgroundPolicy)
252 {
253 NETMGR_LOG_D("GetBackgroundPolicy begin");
254 if (netPolicyRule_ == nullptr) {
255 NETMGR_LOG_E("netPolicyRule_ is nullptr");
256 return NETMANAGER_ERR_LOCAL_PTR_NULL;
257 }
258 return netPolicyRule_->GetBackgroundPolicy(backgroundPolicy);
259 }
260
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)261 int32_t NetPolicyService::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
262 {
263 NETMGR_LOG_D("GetBackgroundPolicyByUid uid[%{public}d]", uid);
264 if (netPolicyRule_ == nullptr) {
265 NETMGR_LOG_E("netPolicyRule_ is nullptr");
266 return NETMANAGER_ERR_LOCAL_PTR_NULL;
267 }
268 return netPolicyRule_->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
269 }
270
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)271 int32_t NetPolicyService::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
272 {
273 NETMGR_LOG_I("UpdateRemindPolicy start");
274 if (netPolicyTraffic_ == nullptr) {
275 NETMGR_LOG_E("netPolicyTraffic_ is nullptr");
276 return NETMANAGER_ERR_LOCAL_PTR_NULL;
277 }
278 return netPolicyTraffic_->UpdateRemindPolicy(netType, simId, remindType);
279 }
280
SetDeviceIdleTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)281 int32_t NetPolicyService::SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
282 {
283 NETMGR_LOG_D("SetDeviceIdleTrustlist start");
284 if (netPolicyFirewall_ == nullptr) {
285 NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
286 return NETMANAGER_ERR_LOCAL_PTR_NULL;
287 }
288 return netPolicyFirewall_->SetDeviceIdleTrustlist(uids, isAllowed);
289 }
290
GetDeviceIdleTrustlist(std::vector<uint32_t> & uids)291 int32_t NetPolicyService::GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)
292 {
293 NETMGR_LOG_D("GetDeviceIdleTrustlist start");
294 if (netPolicyFirewall_ == nullptr) {
295 NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
296 return NETMANAGER_ERR_LOCAL_PTR_NULL;
297 }
298 return netPolicyFirewall_->GetDeviceIdleTrustlist(uids);
299 }
300
SetDeviceIdlePolicy(bool enable)301 int32_t NetPolicyService::SetDeviceIdlePolicy(bool enable)
302 {
303 NETMGR_LOG_I("SetDeviceIdlePolicy enable[%{public}d]", enable);
304 if (netPolicyFirewall_ == nullptr) {
305 NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
306 return NETMANAGER_ERR_LOCAL_PTR_NULL;
307 }
308 return netPolicyFirewall_->UpdateDeviceIdlePolicy(enable);
309 }
310
GetPowerSaveTrustlist(std::vector<uint32_t> & uids)311 int32_t NetPolicyService::GetPowerSaveTrustlist(std::vector<uint32_t> &uids)
312 {
313 NETMGR_LOG_D("GetPowerSaveTrustlist start");
314 if (netPolicyFirewall_ == nullptr) {
315 NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
316 return NETMANAGER_ERR_LOCAL_PTR_NULL;
317 }
318 return netPolicyFirewall_->GetPowerSaveTrustlist(uids);
319 }
320
SetPowerSaveTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)321 int32_t NetPolicyService::SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
322 {
323 NETMGR_LOG_I("SetPowerSaveTrustlist start");
324 if (netPolicyFirewall_ == nullptr) {
325 NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
326 return NETMANAGER_ERR_LOCAL_PTR_NULL;
327 }
328 return netPolicyFirewall_->SetPowerSaveTrustlist(uids, isAllowed);
329 }
330
SetPowerSavePolicy(bool enable)331 int32_t NetPolicyService::SetPowerSavePolicy(bool enable)
332 {
333 NETMGR_LOG_I("SetPowerSavePolicy enable[%{public}d]", enable);
334 if (netPolicyFirewall_ == nullptr) {
335 NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
336 return NETMANAGER_ERR_LOCAL_PTR_NULL;
337 }
338 return netPolicyFirewall_->UpdatePowerSavePolicy(enable);
339 }
340
GetDumpMessage(std::string & message)341 int32_t NetPolicyService::GetDumpMessage(std::string &message)
342 {
343 if (netPolicyRule_ == nullptr || netPolicyTraffic_ == nullptr) {
344 NETMGR_LOG_E("netPolicyFirewall_ or netPolicyTraffic_ is nullptr");
345 return NETMANAGER_ERR_LOCAL_PTR_NULL;
346 }
347 netPolicyRule_->GetDumpMessage(message);
348 netPolicyTraffic_->GetDumpMessage(message);
349 return NETMANAGER_SUCCESS;
350 }
351
CheckPermission()352 int32_t NetPolicyService::CheckPermission()
353 {
354 return NETMANAGER_SUCCESS;
355 }
356
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)357 void NetPolicyService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
358 {
359 NETMGR_LOG_I("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
360 if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
361 if (hasSARemoved_) {
362 OnNetSysRestart();
363 hasSARemoved_ = false;
364 }
365
366 EventFwk::MatchingSkills matchingSkills;
367 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
368 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
369 subscribeInfo.SetPriority(1);
370 std::shared_ptr<NetPolicyListener> subscriber = std::make_shared<NetPolicyListener>(
371 subscribeInfo, std::static_pointer_cast<NetPolicyService>(shared_from_this()));
372 EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
373 }
374 }
375
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)376 void NetPolicyService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
377 {
378 NETMGR_LOG_I("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
379 if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
380 hasSARemoved_ = true;
381 }
382 }
383
OnNetSysRestart()384 void NetPolicyService::OnNetSysRestart()
385 {
386 NETMGR_LOG_I("OnNetSysRestart");
387
388 if (netPolicyRule_ != nullptr) {
389 netPolicyRule_->TransPolicyToRule();
390 }
391 }
392
FactoryResetPolicies()393 int32_t NetPolicyService::FactoryResetPolicies()
394 {
395 NETMGR_LOG_I("FactoryResetPolicies begin");
396 if (netPolicyRule_ != nullptr && netPolicyFirewall_ != nullptr && netPolicyTraffic_ != nullptr) {
397 netPolicyRule_->ResetPolicies();
398 netPolicyFirewall_->ResetPolicies();
399 netPolicyTraffic_->ResetPolicies();
400 NETMGR_LOG_I("FactoryResetPolicies end.");
401 return NETMANAGER_SUCCESS;
402 }
403 return NETMANAGER_ERR_LOCAL_PTR_NULL;
404 }
405
RegisterFactoryResetCallback()406 void NetPolicyService::RegisterFactoryResetCallback()
407 {
408 NETMGR_LOG_I("RegisterFactetCallback enter.");
409
410 if (netFactoryResetCallback_ == nullptr) {
411 netFactoryResetCallback_ =
412 (std::make_unique<FactoryResetCallBack>(std::static_pointer_cast<NetPolicyService>(shared_from_this())))
413 .release();
414 }
415
416 if (netFactoryResetCallback_ != nullptr) {
417 int32_t ret = NetManagerCenter::GetInstance().RegisterNetFactoryResetCallback(netFactoryResetCallback_);
418 if (ret != NETMANAGER_SUCCESS) {
419 NETMGR_LOG_E("RegisterFactoryResetCallback ret[%{public}d]", ret);
420 }
421 } else {
422 NETMGR_LOG_E("netFactoryResetCallback_ is null");
423 }
424 }
425
UpdateNetAccessPolicyToMapFromDB()426 void NetPolicyService::UpdateNetAccessPolicyToMapFromDB()
427 {
428 NETMGR_LOG_I("UpdateNetAccessPolicyToMapFromDB enter.");
429 std::vector<NetAccessPolicyData> result = netAccessPolicy_.QueryAll();
430 for (size_t i = 0; i < result.size(); i++) {
431 NetworkAccessPolicy policy;
432 policy.wifiAllow = result[i].wifiPolicy;
433 policy.cellularAllow = result[i].cellularPolicy;
434 (void)netPolicyRule_->SetNetworkAccessPolicy(result[i].uid, policy, result[i].setFromConfigFlag,
435 result[i].isBroker);
436 }
437 }
438
439 // Do not post into event handler, because this interface should have good performance
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag)440 int32_t NetPolicyService::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag)
441 {
442 NETMGR_LOG_I("SetNetworkAccessPolicy enter.");
443 if (netPolicyRule_ == nullptr) {
444 NETMGR_LOG_E("netPolicyRule_ is nullptr");
445 return NETMANAGER_ERR_LOCAL_PTR_NULL;
446 }
447
448 bool isBroker = CheckNetworkAccessIsBroker(uid);
449 NetAccessPolicyData data;
450 data.uid = uid;
451 data.wifiPolicy = policy.wifiAllow;
452 data.cellularPolicy = policy.cellularAllow;
453 data.setFromConfigFlag = !reconfirmFlag;
454 data.isBroker = isBroker;
455 netAccessPolicy_.InsertData(data);
456 return netPolicyRule_->SetNetworkAccessPolicy(uid, policy, !reconfirmFlag, isBroker);
457 }
458
GetNetworkAccessPolicy(AccessPolicyParameter parameter,AccessPolicySave & policy)459 int32_t NetPolicyService::GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave &policy)
460 {
461 NETMGR_LOG_I("GetNetworkAccessPolicy enter.");
462 if (parameter.flag) {
463 NetAccessPolicyData policyData;
464 if (netAccessPolicy_.QueryByUid(parameter.uid, policyData) != NETMANAGER_SUCCESS) {
465 policy.policy.wifiAllow = true;
466 policy.policy.cellularAllow = true;
467 return NETMANAGER_SUCCESS;
468 }
469 policy.policy.wifiAllow = policyData.wifiPolicy;
470 policy.policy.cellularAllow = policyData.cellularPolicy;
471 return NETMANAGER_SUCCESS;
472 }
473
474 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
475 if (!systemAbilityManager) {
476 NETMGR_LOG_E("fail to get system ability mgr.");
477 return NETMANAGER_ERR_LOCAL_PTR_NULL;
478 }
479
480 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
481 if (!remoteObject) {
482 NETMGR_LOG_E("fail to get bundle manager proxy.");
483 return NETMANAGER_ERR_LOCAL_PTR_NULL;
484 }
485
486 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
487 if (bundleMgrProxy == nullptr) {
488 NETMGR_LOG_E("Failed to get bundle manager proxy.");
489 return NETMANAGER_ERR_INTERNAL;
490 }
491
492 std::vector<AppExecFwk::ApplicationInfo> appInfos;
493 bool retC = bundleMgrProxy->GetApplicationInfos(AppExecFwk::ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION,
494 static_cast<uint32_t>(parameter.userId), appInfos);
495 if (!retC) {
496 NETMGR_LOG_E("GetApplicationInfos Error");
497 return NETMANAGER_ERR_INTERNAL;
498 }
499 for (const auto &appInfo : appInfos) {
500 NetworkAccessPolicy policyTmp;
501 NetAccessPolicyData policyData;
502 if (netAccessPolicy_.QueryByUid(appInfo.uid, policyData) == NETMANAGER_SUCCESS) {
503 policyTmp.wifiAllow = policyData.wifiPolicy;
504 policyTmp.cellularAllow = policyData.cellularPolicy;
505 } else {
506 policyTmp.wifiAllow = true;
507 policyTmp.cellularAllow = true;
508 }
509 policy.uid_policies.insert(std::pair<uint32_t, NetworkAccessPolicy>(appInfo.uid, policyTmp));
510 }
511
512 return NETMANAGER_SUCCESS;
513 }
514
DeleteNetworkAccessPolicy(uint32_t uid)515 int32_t NetPolicyService::DeleteNetworkAccessPolicy(uint32_t uid)
516 {
517 if (netPolicyRule_ == nullptr) {
518 NETMGR_LOG_E("netPolicyRule_ is nullptr");
519 return NETMANAGER_ERR_LOCAL_PTR_NULL;
520 }
521
522 return netPolicyRule_->DeleteNetworkAccessPolicy(uid);
523 }
524
NotifyNetAccessPolicyDiag(uint32_t uid)525 int32_t NetPolicyService::NotifyNetAccessPolicyDiag(uint32_t uid)
526 {
527 NETMGR_LOG_I("NotifyNetAccessPolicyDiag");
528
529 std::lock_guard<std::mutex> lock(mutex_);
530 void *handler = dlopen(LIB_LOAD_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
531 if (handler == nullptr) {
532 NETMGR_LOG_E("load failed, failed reason : %{public}s", dlerror());
533 return NETMANAGER_ERR_INTERNAL;
534 }
535
536 GetNetBundleClass GetNetAccessPolicyDialog = (GetNetBundleClass)dlsym(handler, "GetNetAccessPolicyDialog");
537 if (GetNetAccessPolicyDialog == nullptr) {
538 NETMGR_LOG_E("GetNetAccessPolicyDialog faild, failed reason : %{public}s", dlerror());
539 dlclose(handler);
540 return NETMANAGER_ERR_INTERNAL;
541 }
542 auto netPolicyDialog = GetNetAccessPolicyDialog();
543 if (netPolicyDialog == nullptr) {
544 NETMGR_LOG_E("netPolicyDialog is nullptr");
545 dlclose(handler);
546 return NETMANAGER_ERR_INTERNAL;
547 }
548
549 auto ret = netPolicyDialog->ConnectSystemUi(uid);
550 if (!ret) {
551 NETMGR_LOG_E("netPolicyDialog ConnectSystemUi failed");
552 dlclose(handler);
553 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
554 }
555
556 NETMGR_LOG_D("NotifyNetAccessPolicyDiag success");
557 dlclose(handler);
558
559 return NETMANAGER_SUCCESS;
560 }
561
CheckNetworkAccessIsBroker(uint32_t uid)562 bool NetPolicyService::CheckNetworkAccessIsBroker(uint32_t uid)
563 {
564 NETMGR_LOG_I("CheckNetworkAccessIsBroker");
565
566 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
567 if (!systemAbilityManager) {
568 NETMGR_LOG_E("fail to get system ability mgr.");
569 return false;
570 }
571
572 auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
573 if (!remoteObject) {
574 NETMGR_LOG_E("fail to get bundle manager proxy.");
575 return false;
576 }
577
578 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
579 if (bundleMgrProxy == nullptr) {
580 NETMGR_LOG_E("Failed to get bundle manager proxy.");
581 return false;
582 }
583
584 std::string bundleName = "";
585 bundleMgrProxy->GetBundleNameForUid(uid, bundleName);
586 if (bundleName == "myappliction.apps") {
587 NETMGR_LOG_E("Failed to get bundle manager proxy.");
588 return true;
589 }
590 return false;
591 }
592
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)593 int32_t NetPolicyService::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
594 {
595 if (netPolicyRule_ == nullptr) {
596 NETMGR_LOG_E("netPolicyRule_ is nullptr");
597 return NETMANAGER_ERR_LOCAL_PTR_NULL;
598 }
599
600 return netPolicyRule_->PolicySetNicTrafficAllowed(ifaceNames, status);
601 }
602 } // namespace NetManagerStandard
603 } // namespace OHOS
604