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             NetAccessPolicyRDB netAccessPolicy;
117             netAccessPolicy.InitRdbStore();
118             UpdateNetAccessPolicyToMapFromDB();
119             if (!Publish(DelayedSingleton<NetPolicyService>::GetInstance().get())) {
120                 NETMGR_LOG_E("Register to sa manager failed");
121             }
122         }, ffrt::task_attr().name("FfrtNetPolicyServiceInit"));
123 }
124 
SetPolicyByUid(uint32_t uid, uint32_t policy)125 int32_t NetPolicyService::SetPolicyByUid(uint32_t uid, uint32_t policy)
126 {
127     NETMGR_LOG_I("SetPolicyByUid uid[%{public}d] policy[%{public}d]", uid, policy);
128     if (netPolicyRule_ == nullptr) {
129         NETMGR_LOG_E("netPolicyRule_ is nullptr");
130         return NETMANAGER_ERR_LOCAL_PTR_NULL;
131     }
132     return netPolicyRule_->TransPolicyToRule(uid, policy);
133 }
134 
GetPolicyByUid(uint32_t uid, uint32_t &policy)135 int32_t NetPolicyService::GetPolicyByUid(uint32_t uid, uint32_t &policy)
136 {
137     NETMGR_LOG_D("GetPolicyByUid uid[%{public}d]", uid);
138     if (netPolicyRule_ == nullptr) {
139         NETMGR_LOG_E("netPolicyRule_ is nullptr");
140         return NETMANAGER_ERR_LOCAL_PTR_NULL;
141     }
142     return netPolicyRule_->GetPolicyByUid(uid, policy);
143 }
144 
GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)145 int32_t NetPolicyService::GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)
146 {
147     NETMGR_LOG_D("GetUidsByPolicy policy[%{public}d]", policy);
148     if (netPolicyRule_ == nullptr) {
149         NETMGR_LOG_E("netPolicyRule_ is nullptr");
150         return NETMANAGER_ERR_LOCAL_PTR_NULL;
151     }
152     return netPolicyRule_->GetUidsByPolicy(policy, uids);
153 }
154 
IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)155 int32_t NetPolicyService::IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)
156 {
157     NETMGR_LOG_I("IsUidNetAllowed uid[%{public}d metered[%{public}d]", uid, metered);
158     if (NetSettings::GetInstance().IsSystem(uid)) {
159         isAllowed = true;
160         return NETMANAGER_SUCCESS;
161     }
162     if (netPolicyRule_ != nullptr) {
163         return netPolicyRule_->IsUidNetAllowed(uid, metered, isAllowed);
164     }
165     return NETMANAGER_ERR_LOCAL_PTR_NULL;
166 }
167 
IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)168 int32_t NetPolicyService::IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
169 {
170     NETMGR_LOG_D("IsUidNetAllowed uid[%{public}d ifaceName[%{public}s]", uid, ifaceName.c_str());
171     const auto &vec = netPolicyTraffic_->GetMeteredIfaces();
172     if (std::find(vec.begin(), vec.end(), ifaceName) != vec.end()) {
173         return IsUidNetAllowed(uid, true, isAllowed);
174     }
175     return IsUidNetAllowed(uid, false, isAllowed);
176 }
177 
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)178 int32_t NetPolicyService::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
179 {
180     NETMGR_LOG_I("RegisterNetPolicyCallback");
181     if (callback == nullptr) {
182         NETMGR_LOG_E("RegisterNetPolicyCallback parameter callback is null");
183         return NETMANAGER_ERR_LOCAL_PTR_NULL;
184     }
185 
186     if (netPolicyCallback_ == nullptr) {
187         NETMGR_LOG_E("netPolicyCallback_ is null");
188         return NETMANAGER_ERR_LOCAL_PTR_NULL;
189     }
190 
191     return netPolicyCallback_->RegisterNetPolicyCallbackAsync(callback);
192 }
193 
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)194 int32_t NetPolicyService::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
195 {
196     NETMGR_LOG_I("UnregisterNetPolicyCallback");
197     if (callback == nullptr) {
198         NETMGR_LOG_E("UnregisterNetPolicyCallback parameter callback is null");
199         return NETMANAGER_ERR_LOCAL_PTR_NULL;
200     }
201 
202     if (netPolicyCallback_ == nullptr) {
203         NETMGR_LOG_E("netPolicyCallback_ is null");
204         return NETMANAGER_ERR_LOCAL_PTR_NULL;
205     }
206     return netPolicyCallback_->UnregisterNetPolicyCallbackAsync(callback);
207 }
208 
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)209 int32_t NetPolicyService::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
210 {
211     NETMGR_LOG_I("SetNetQuotaPolicies quotaPolicySize[%{public}zd]", quotaPolicies.size());
212     if (netPolicyTraffic_ == nullptr) {
213         NETMGR_LOG_E("netPolicyTraffic_ is nullptr");
214         return NETMANAGER_ERR_LOCAL_PTR_NULL;
215     }
216     return netPolicyTraffic_->UpdateQuotaPolicies(quotaPolicies);
217 }
218 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)219 int32_t NetPolicyService::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)
220 {
221     NETMGR_LOG_D("GetNetQuotaPolicies begin");
222     if (netPolicyTraffic_ == nullptr) {
223         NETMGR_LOG_E("netPolicyTraffic_ is nullptr");
224         return NETMANAGER_ERR_LOCAL_PTR_NULL;
225     }
226     return netPolicyTraffic_->GetNetQuotaPolicies(quotaPolicies);
227 }
228 
ResetPolicies(const std::string &simId)229 int32_t NetPolicyService::ResetPolicies(const std::string &simId)
230 {
231     NETMGR_LOG_I("ResetPolicies begin");
232     if (netPolicyRule_ != nullptr && netPolicyFirewall_ != nullptr && netPolicyTraffic_ != nullptr) {
233         netPolicyRule_->ResetPolicies();
234         netPolicyFirewall_->ResetPolicies();
235         netPolicyTraffic_->ResetPolicies(simId);
236         NETMGR_LOG_I("ResetPolicies end.");
237         return NETMANAGER_SUCCESS;
238     }
239     return NETMANAGER_ERR_LOCAL_PTR_NULL;
240 }
241 
SetBackgroundPolicy(bool allow)242 int32_t NetPolicyService::SetBackgroundPolicy(bool allow)
243 {
244     NETMGR_LOG_I("SetBackgroundPolicy allow[%{public}d]", allow);
245     if (netPolicyRule_ == nullptr) {
246         NETMGR_LOG_E("netPolicyRule_ is nullptr");
247         return NETMANAGER_ERR_LOCAL_PTR_NULL;
248     }
249     return netPolicyRule_->SetBackgroundPolicy(allow);
250 }
251 
GetBackgroundPolicy(bool &backgroundPolicy)252 int32_t NetPolicyService::GetBackgroundPolicy(bool &backgroundPolicy)
253 {
254     NETMGR_LOG_D("GetBackgroundPolicy begin");
255     if (netPolicyRule_ == nullptr) {
256         NETMGR_LOG_E("netPolicyRule_ is nullptr");
257         return NETMANAGER_ERR_LOCAL_PTR_NULL;
258     }
259     return netPolicyRule_->GetBackgroundPolicy(backgroundPolicy);
260 }
261 
GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)262 int32_t NetPolicyService::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
263 {
264     NETMGR_LOG_D("GetBackgroundPolicyByUid uid[%{public}d]", uid);
265     if (netPolicyRule_ == nullptr) {
266         NETMGR_LOG_E("netPolicyRule_ is nullptr");
267         return NETMANAGER_ERR_LOCAL_PTR_NULL;
268     }
269     return netPolicyRule_->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
270 }
271 
UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)272 int32_t NetPolicyService::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
273 {
274     NETMGR_LOG_I("UpdateRemindPolicy start");
275     if (netPolicyTraffic_ == nullptr) {
276         NETMGR_LOG_E("netPolicyTraffic_ is nullptr");
277         return NETMANAGER_ERR_LOCAL_PTR_NULL;
278     }
279     return netPolicyTraffic_->UpdateRemindPolicy(netType, simId, remindType);
280 }
281 
SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)282 int32_t NetPolicyService::SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
283 {
284     NETMGR_LOG_D("SetDeviceIdleTrustlist start");
285     if (netPolicyFirewall_ == nullptr) {
286         NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
287         return NETMANAGER_ERR_LOCAL_PTR_NULL;
288     }
289     return netPolicyFirewall_->SetDeviceIdleTrustlist(uids, isAllowed);
290 }
291 
GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)292 int32_t NetPolicyService::GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)
293 {
294     NETMGR_LOG_D("GetDeviceIdleTrustlist start");
295     if (netPolicyFirewall_ == nullptr) {
296         NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
297         return NETMANAGER_ERR_LOCAL_PTR_NULL;
298     }
299     return netPolicyFirewall_->GetDeviceIdleTrustlist(uids);
300 }
301 
SetDeviceIdlePolicy(bool enable)302 int32_t NetPolicyService::SetDeviceIdlePolicy(bool enable)
303 {
304     NETMGR_LOG_I("SetDeviceIdlePolicy enable[%{public}d]", enable);
305     if (netPolicyFirewall_ == nullptr) {
306         NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
307         return NETMANAGER_ERR_LOCAL_PTR_NULL;
308     }
309     return netPolicyFirewall_->UpdateDeviceIdlePolicy(enable);
310 }
311 
GetPowerSaveTrustlist(std::vector<uint32_t> &uids)312 int32_t NetPolicyService::GetPowerSaveTrustlist(std::vector<uint32_t> &uids)
313 {
314     NETMGR_LOG_D("GetPowerSaveTrustlist start");
315     if (netPolicyFirewall_ == nullptr) {
316         NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
317         return NETMANAGER_ERR_LOCAL_PTR_NULL;
318     }
319     return netPolicyFirewall_->GetPowerSaveTrustlist(uids);
320 }
321 
SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)322 int32_t NetPolicyService::SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
323 {
324     NETMGR_LOG_I("SetPowerSaveTrustlist start");
325     if (netPolicyFirewall_ == nullptr) {
326         NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
327         return NETMANAGER_ERR_LOCAL_PTR_NULL;
328     }
329     return netPolicyFirewall_->SetPowerSaveTrustlist(uids, isAllowed);
330 }
331 
SetPowerSavePolicy(bool enable)332 int32_t NetPolicyService::SetPowerSavePolicy(bool enable)
333 {
334     NETMGR_LOG_I("SetPowerSavePolicy enable[%{public}d]", enable);
335     if (netPolicyFirewall_ == nullptr) {
336         NETMGR_LOG_E("netPolicyFirewall_ is nullptr");
337         return NETMANAGER_ERR_LOCAL_PTR_NULL;
338     }
339     return netPolicyFirewall_->UpdatePowerSavePolicy(enable);
340 }
341 
GetDumpMessage(std::string &message)342 int32_t NetPolicyService::GetDumpMessage(std::string &message)
343 {
344     if (netPolicyRule_ == nullptr || netPolicyTraffic_ == nullptr) {
345         NETMGR_LOG_E("netPolicyFirewall_ or netPolicyTraffic_ is nullptr");
346         return NETMANAGER_ERR_LOCAL_PTR_NULL;
347     }
348     netPolicyRule_->GetDumpMessage(message);
349     netPolicyTraffic_->GetDumpMessage(message);
350     return NETMANAGER_SUCCESS;
351 }
352 
CheckPermission()353 int32_t NetPolicyService::CheckPermission()
354 {
355     return NETMANAGER_SUCCESS;
356 }
357 
OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)358 void NetPolicyService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
359 {
360     NETMGR_LOG_I("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
361     if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
362         if (hasSARemoved_) {
363             OnNetSysRestart();
364             hasSARemoved_ = false;
365         }
366 
367         EventFwk::MatchingSkills matchingSkills;
368         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
369         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
370         subscribeInfo.SetPriority(1);
371         std::shared_ptr<NetPolicyListener> subscriber = std::make_shared<NetPolicyListener>(
372             subscribeInfo, std::static_pointer_cast<NetPolicyService>(shared_from_this()));
373         EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
374     }
375 }
376 
OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)377 void NetPolicyService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
378 {
379     NETMGR_LOG_I("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
380     if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
381         hasSARemoved_ = true;
382     }
383 }
384 
OnNetSysRestart()385 void NetPolicyService::OnNetSysRestart()
386 {
387     NETMGR_LOG_I("OnNetSysRestart");
388 
389     if (netPolicyRule_ != nullptr) {
390         netPolicyRule_->TransPolicyToRule();
391     }
392 }
393 
FactoryResetPolicies()394 int32_t NetPolicyService::FactoryResetPolicies()
395 {
396     NETMGR_LOG_I("FactoryResetPolicies begin");
397     if (netPolicyRule_ != nullptr && netPolicyFirewall_ != nullptr && netPolicyTraffic_ != nullptr) {
398         netPolicyRule_->ResetPolicies();
399         netPolicyFirewall_->ResetPolicies();
400         netPolicyTraffic_->ResetPolicies();
401         NETMGR_LOG_I("FactoryResetPolicies end.");
402         return NETMANAGER_SUCCESS;
403     }
404     return NETMANAGER_ERR_LOCAL_PTR_NULL;
405 }
406 
RegisterFactoryResetCallback()407 void NetPolicyService::RegisterFactoryResetCallback()
408 {
409     NETMGR_LOG_I("RegisterFactetCallback enter.");
410 
411     if (netFactoryResetCallback_ == nullptr) {
412         netFactoryResetCallback_ =
413             (std::make_unique<FactoryResetCallBack>(std::static_pointer_cast<NetPolicyService>(shared_from_this())))
414                 .release();
415     }
416 
417     if (netFactoryResetCallback_ != nullptr) {
418         int32_t ret = NetManagerCenter::GetInstance().RegisterNetFactoryResetCallback(netFactoryResetCallback_);
419         if (ret != NETMANAGER_SUCCESS) {
420             NETMGR_LOG_E("RegisterFactoryResetCallback ret[%{public}d]", ret);
421         }
422     } else {
423         NETMGR_LOG_E("netFactoryResetCallback_ is null");
424     }
425 }
426 
UpdateNetAccessPolicyToMapFromDB()427 void NetPolicyService::UpdateNetAccessPolicyToMapFromDB()
428 {
429     NETMGR_LOG_I("UpdateNetAccessPolicyToMapFromDB enter.");
430     NetAccessPolicyRDB netAccessPolicy;
431     std::vector<NetAccessPolicyData> result = netAccessPolicy.QueryAll();
432     for (size_t i = 0; i < result.size(); i++) {
433         NetworkAccessPolicy policy;
434         policy.wifiAllow = result[i].wifiPolicy;
435         policy.cellularAllow = result[i].cellularPolicy;
436         (void)netPolicyRule_->SetNetworkAccessPolicy(result[i].uid, policy, result[i].setFromConfigFlag,
437                                                      result[i].isBroker);
438     }
439 }
440 
441 // Do not post into event handler, because this interface should have good performance
SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag)442 int32_t NetPolicyService::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag)
443 {
444     NETMGR_LOG_I("SetNetworkAccessPolicy enter.");
445     if (netPolicyRule_ == nullptr) {
446         NETMGR_LOG_E("netPolicyRule_ is nullptr");
447         return NETMANAGER_ERR_LOCAL_PTR_NULL;
448     }
449 
450     bool isBroker = CheckNetworkAccessIsBroker(uid);
451     NetAccessPolicyData data;
452     data.uid = uid;
453     data.wifiPolicy = policy.wifiAllow;
454     data.cellularPolicy = policy.cellularAllow;
455     data.setFromConfigFlag = !reconfirmFlag;
456     data.isBroker = isBroker;
457     NetAccessPolicyRDB netAccessPolicy;
458     netAccessPolicy.InsertData(data);
459 
460     return netPolicyRule_->SetNetworkAccessPolicy(uid, policy, !reconfirmFlag, isBroker);
461 }
462 
GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave &policy)463 int32_t NetPolicyService::GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave &policy)
464 {
465     NETMGR_LOG_I("GetNetworkAccessPolicy enter.");
466     NetAccessPolicyRDB netAccessPolicy;
467 
468     if (parameter.flag) {
469         NetAccessPolicyData policyData;
470         if (netAccessPolicy.QueryByUid(parameter.uid, policyData) != NETMANAGER_SUCCESS) {
471             policy.policy.wifiAllow = true;
472             policy.policy.cellularAllow = true;
473             return NETMANAGER_SUCCESS;
474         }
475         policy.policy.wifiAllow = policyData.wifiPolicy;
476         policy.policy.cellularAllow = policyData.cellularPolicy;
477         return NETMANAGER_SUCCESS;
478     }
479 
480     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
481     if (!systemAbilityManager) {
482         NETMGR_LOG_E("fail to get system ability mgr.");
483         return NETMANAGER_ERR_LOCAL_PTR_NULL;
484     }
485 
486     auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
487     if (!remoteObject) {
488         NETMGR_LOG_E("fail to get bundle manager proxy.");
489         return NETMANAGER_ERR_LOCAL_PTR_NULL;
490     }
491 
492     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
493     if (bundleMgrProxy == nullptr) {
494         NETMGR_LOG_E("Failed to get bundle manager proxy.");
495         return NETMANAGER_ERR_INTERNAL;
496     }
497 
498     std::vector<AppExecFwk::ApplicationInfo> appInfos;
499     bool retC = bundleMgrProxy->GetApplicationInfos(AppExecFwk::ApplicationFlag::GET_APPLICATION_INFO_WITH_PERMISSION,
500                                                     static_cast<uint32_t>(parameter.userId), appInfos);
501     if (!retC) {
502         NETMGR_LOG_E("GetApplicationInfos Error");
503         return NETMANAGER_ERR_INTERNAL;
504     }
505     for (const auto &appInfo : appInfos) {
506         NetworkAccessPolicy policyTmp;
507         NetAccessPolicyData policyData;
508         if (netAccessPolicy.QueryByUid(appInfo.uid, policyData) == NETMANAGER_SUCCESS) {
509             policyTmp.wifiAllow = policyData.wifiPolicy;
510             policyTmp.cellularAllow = policyData.cellularPolicy;
511         } else {
512             policyTmp.wifiAllow = true;
513             policyTmp.cellularAllow = true;
514         }
515         policy.uid_policies.insert(std::pair<uint32_t, NetworkAccessPolicy>(appInfo.uid, policyTmp));
516     }
517 
518     return NETMANAGER_SUCCESS;
519 }
520 
DeleteNetworkAccessPolicy(uint32_t uid)521 int32_t NetPolicyService::DeleteNetworkAccessPolicy(uint32_t uid)
522 {
523     if (netPolicyRule_ == nullptr) {
524         NETMGR_LOG_E("netPolicyRule_ is nullptr");
525         return NETMANAGER_ERR_LOCAL_PTR_NULL;
526     }
527 
528     return netPolicyRule_->DeleteNetworkAccessPolicy(uid);
529 }
530 
NotifyNetAccessPolicyDiag(uint32_t uid)531 int32_t NetPolicyService::NotifyNetAccessPolicyDiag(uint32_t uid)
532 {
533     NETMGR_LOG_I("NotifyNetAccessPolicyDiag");
534 
535     std::lock_guard<std::mutex> lock(mutex_);
536     void *handler = dlopen(LIB_LOAD_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
537     if (handler == nullptr) {
538         NETMGR_LOG_E("load failed, failed reason : %{public}s", dlerror());
539         return NETMANAGER_ERR_INTERNAL;
540     }
541 
542     GetNetBundleClass GetNetAccessPolicyDialog = (GetNetBundleClass)dlsym(handler, "GetNetAccessPolicyDialog");
543     if (GetNetAccessPolicyDialog == nullptr) {
544         NETMGR_LOG_E("GetNetAccessPolicyDialog faild, failed reason : %{public}s", dlerror());
545         dlclose(handler);
546         return NETMANAGER_ERR_INTERNAL;
547     }
548     auto netPolicyDialog = GetNetAccessPolicyDialog();
549     if (netPolicyDialog == nullptr) {
550         NETMGR_LOG_E("netPolicyDialog is nullptr");
551         dlclose(handler);
552         return NETMANAGER_ERR_INTERNAL;
553     }
554 
555     auto ret = netPolicyDialog->ConnectSystemUi(uid);
556     if (!ret) {
557         NETMGR_LOG_E("netPolicyDialog ConnectSystemUi failed");
558         dlclose(handler);
559         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
560     }
561 
562     NETMGR_LOG_D("NotifyNetAccessPolicyDiag success");
563     dlclose(handler);
564 
565     return NETMANAGER_SUCCESS;
566 }
567 
CheckNetworkAccessIsBroker(uint32_t uid)568 bool NetPolicyService::CheckNetworkAccessIsBroker(uint32_t uid)
569 {
570     NETMGR_LOG_I("CheckNetworkAccessIsBroker");
571 
572     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
573     if (!systemAbilityManager) {
574         NETMGR_LOG_E("fail to get system ability mgr.");
575         return false;
576     }
577 
578     auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
579     if (!remoteObject) {
580         NETMGR_LOG_E("fail to get bundle manager proxy.");
581         return false;
582     }
583 
584     sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
585     if (bundleMgrProxy == nullptr) {
586         NETMGR_LOG_E("Failed to get bundle manager proxy.");
587         return false;
588     }
589 
590     std::string bundleName = "";
591     bundleMgrProxy->GetBundleNameForUid(uid, bundleName);
592     if (bundleName == "myappliction.apps") {
593         NETMGR_LOG_E("Failed to get bundle manager proxy.");
594         return true;
595     }
596     return false;
597 }
598 
SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)599 int32_t NetPolicyService::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
600 {
601     if (netPolicyRule_ == nullptr) {
602         NETMGR_LOG_E("netPolicyRule_ is nullptr");
603         return NETMANAGER_ERR_LOCAL_PTR_NULL;
604     }
605 
606     return netPolicyRule_->PolicySetNicTrafficAllowed(ifaceNames, status);
607 }
608 } // namespace NetManagerStandard
609 } // namespace OHOS
610