1 /* 2 * Copyright (c) 2021-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 NET_POLICY_FILE_H 17 #define NET_POLICY_FILE_H 18 19 #include <climits> 20 #include <fcntl.h> 21 #include <fstream> 22 #include <iostream> 23 #include <memory> 24 #include <mutex> 25 #include <sstream> 26 #include <sys/sendfile.h> 27 #include <sys/stat.h> 28 #include <sys/types.h> 29 #include <unistd.h> 30 #include <vector> 31 32 #include <json/json.h> 33 34 #include "singleton.h" 35 36 #include "netmanager_base_common_utils.h" 37 #include "net_policy_constants.h" 38 #include "net_policy_file_event_handler.h" 39 #include "net_policy_inner_define.h" 40 #include "net_quota_policy.h" 41 42 namespace OHOS { 43 namespace NetManagerStandard { 44 enum NetUidPolicyOpType { 45 NET_POLICY_UID_OP_TYPE_DO_NOTHING = 0, 46 NET_POLICY_UID_OP_TYPE_ADD = 1, 47 NET_POLICY_UID_OP_TYPE_DELETE = 2, 48 NET_POLICY_UID_OP_TYPE_UPDATE = 3, 49 }; 50 51 class NetPolicyFile : public std::enable_shared_from_this<NetPolicyFile> { 52 DECLARE_DELAYED_SINGLETON(NetPolicyFile); 53 54 public: 55 /** 56 * Init by reading policy from file. 57 * @return true Return true means init policy successful. 58 * @return false Return false means init policy failed. 59 */ 60 bool InitPolicy(); 61 62 /** 63 * Reset policy to default. 64 */ 65 int32_t ResetPolicies(); 66 67 /** 68 * Used by net_policy_rule.cpp to get policy from file. 69 * 70 * @return const std::vector<UidPolicy>& 71 */ 72 const std::vector<UidPolicy> &ReadUidPolicies(); 73 74 /** 75 * Used by net_policy_rule.cpp to write policy to file. 76 * 77 * @param uid The specified UID of app. 78 * @param policy The network policy for application. 79 * For details, see {@link NetUidPolicy}. 80 */ 81 void WritePolicyByUid(uint32_t uid, uint32_t policy); 82 83 /** 84 * Used by net_policy_traffic.cpp to get quota policy from file. 85 * 86 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 87 */ 88 void ReadQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies); 89 90 /** 91 * Used by net_policy_rule.cpp to write quota policy to file. 92 * 93 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 94 * @return true Return true means successful. 95 * @return false Return false means failed. 96 */ 97 bool WriteQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies); 98 99 /** 100 * Used by net_policy_rule.cpp to get background policy from file. 101 * 102 * @return true Return true means allow access net on background. 103 * @return false Return false means reject access net on background. 104 */ 105 bool ReadBackgroundPolicy(); 106 107 /** 108 * Used by net_policy_rule.cpp to write background policy to file. 109 * 110 * @param allowBackground Allow or Reject access net on background. 111 */ 112 void WriteBackgroundPolicy(bool allowBackground); 113 114 /** 115 * Used by net_policy_firewall.cpp to get firewall policy from file. 116 * 117 * @param chainType The firewall's type.Include "Powersave" or "DeviceIdle". 118 * @param allowedList Firewall's allowed list. 119 * @param deniedList Firewall's denied list. 120 */ 121 int32_t ReadFirewallRules(uint32_t chainType, std::set<uint32_t> &allowedList, std::set<uint32_t> &deniedList); 122 123 /** 124 * Used by net_policy_firewall.cpp to write firewall policy from file. 125 * 126 * @param chainType The firewall's type.Include "Powersave" or "DeviceIdle". 127 * @param allowedList Firewall's allowed list. 128 * @param deniedList Firewall's denied list. 129 */ 130 void WriteFirewallRules(uint32_t chainType, const std::set<uint32_t> &allowedList, 131 const std::set<uint32_t> &deniedList); 132 133 /** 134 * Used by net_policy_rule.cpp, when an app is removed from system, 135 * this uid will be also remove from file. 136 * 137 * @param uid The specified UID of app that removed. 138 */ 139 void RemoveInexistentUid(uint32_t uid); 140 141 private: 142 bool Json2Obj(const std::string &content, NetPolicy &netPolicy); 143 bool Obj2Json(const NetPolicy &netPolicy, std::string &content); 144 145 bool ReadFile(const std::string &filePath); 146 bool ReadFile(); 147 bool WriteFile(); 148 149 void AddUidPolicy(Json::Value &root); 150 void AddBackgroundPolicy(Json::Value &root); 151 void AddQuotaPolicy(Json::Value &root); 152 void AddFirewallRule(Json::Value &root); 153 154 void ParseUidPolicy(const Json::Value &root, NetPolicy &netPolicy); 155 void ParseBackgroundPolicy(const Json::Value &root, NetPolicy &netPolicy); 156 void ParseQuotaPolicy(const Json::Value &root, NetPolicy &netPolicy); 157 void ParseFirewallRule(const Json::Value &root, NetPolicy &netPolicy); 158 159 bool UpdateQuotaPolicyExist(const NetQuotaPolicy "aPolicy); 160 uint32_t ArbitrationWritePolicyToFile(uint32_t uid, uint32_t policy); 161 void WritePolicyByUid(uint32_t netUidPolicyOpType, uint32_t uid, uint32_t policy); 162 ToQuotaPolicy(const NetPolicyQuota & netPolicyQuota,NetQuotaPolicy & quotaPolicy)163 inline void ToQuotaPolicy(const NetPolicyQuota& netPolicyQuota, NetQuotaPolicy "aPolicy) 164 { 165 quotaPolicy.quotapolicy.lastLimitRemind = CommonUtils::StrToLong(netPolicyQuota.lastLimitSnooze, REMIND_NEVER); 166 quotaPolicy.quotapolicy.limitBytes = CommonUtils::StrToLong(netPolicyQuota.limitBytes, DATA_USAGE_UNKNOWN); 167 quotaPolicy.quotapolicy.metered = CommonUtils::StrToBool(netPolicyQuota.metered, false); 168 quotaPolicy.networkmatchrule.netType = CommonUtils::StrToInt(netPolicyQuota.netType, BEARER_DEFAULT); 169 quotaPolicy.quotapolicy.periodDuration = netPolicyQuota.periodDuration; 170 quotaPolicy.quotapolicy.periodStartTime = CommonUtils::StrToLong(netPolicyQuota.periodStartTime); 171 quotaPolicy.networkmatchrule.simId = netPolicyQuota.simId; 172 quotaPolicy.quotapolicy.warningBytes = CommonUtils::StrToLong(netPolicyQuota.warningBytes, DATA_USAGE_UNKNOWN); 173 quotaPolicy.networkmatchrule.ident = netPolicyQuota.ident; 174 } 175 176 std::shared_ptr<NetPolicyFileEventHandler> GetHandler(); 177 178 public: 179 NetPolicy netPolicy_; 180 }; 181 } // namespace NetManagerStandard 182 } // namespace OHOS 183 #endif // NET_POLICY_FILE_H 184