1 /* 2 * Copyright (c) 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 #ifndef FIREWALL_DATABASE_H 17 #define FIREWALL_DATABASE_H 18 19 #include <string> 20 21 #include "rdb_common.h" 22 #include "rdb_errno.h" 23 #include "rdb_helper.h" 24 #include "rdb_open_callback.h" 25 #include "rdb_predicates.h" 26 #include "rdb_store.h" 27 #include "result_set.h" 28 #include "system_ability.h" 29 #include "value_object.h" 30 31 namespace OHOS { 32 namespace NetManagerStandard { 33 static std::string FIREWALL_DB_PATH = "/data/service/el1/public/netmanager/"; 34 35 constexpr const char *FIREWALL_DB_NAME = "netfirewall.db"; 36 constexpr const char *FIREWALL_BACKUP_DB_NAME = "netfirewall_back.db"; 37 constexpr const char *FIREWALL_TABLE_NAME = "firewallRule"; 38 constexpr const char *INTERCEPT_RECORD_TABLE = "interceptRecord"; 39 constexpr int32_t DATABASE_OPEN_VERSION = 1; 40 constexpr int32_t DATABASE_NEW_VERSION = 2; 41 42 constexpr const char *CREATE_FIREWALL_TABLE = "CREATE TABLE IF NOT EXISTS [firewallRule](" 43 "[ruleId] INTEGER PRIMARY KEY, " 44 "[name] TEXT NOT NULL, " 45 "[description] TEXT, " 46 "[userId] INTEGER NOT NULL, " 47 "[direction] INTEGER NOT NULL, " 48 "[action] INTEGER NOT NULL, " 49 "[type] INTEGER NOT NULL, " 50 "[isEnabled] INTEGER NOT NULL, " 51 "[appUid] INTEGER, " 52 "[protocol] INTEGER, " 53 "[primaryDns] TEXT, " 54 "[standbyDns] TEXT, " 55 "[localIps] BLOB, " 56 "[remoteIps] BLOB, " 57 "[localPorts] BLOB, " 58 "[remotePorts] BLOB, " 59 "[domainNum] INTEGER, " 60 "[fuzzyDomainNum] INTEGER, " 61 "[domains] BLOB );"; 62 63 constexpr const char *CREATE_RECORD_TABLE = "CREATE TABLE IF NOT EXISTS [interceptRecord](" 64 "[id] INTEGER PRIMARY KEY, " 65 "[userId] INTEGER NOT NULL, " 66 "[time] INTEGER NOT NULL, " 67 "[localIp] TEXT, " 68 "[remoteIp] TEXT, " 69 "[localPort] INTEGER, " 70 "[remotePort] INTEGER, " 71 "[protocol] INTEGER, " 72 "[appUid] INTEGER NOT NULL, " 73 "[domain] TEXT);"; 74 75 class NetFirewallDataBase : public NoCopyable { 76 public: 77 static std::shared_ptr<NetFirewallDataBase> GetInstance(); 78 79 /** 80 * Insert value into the table 81 * 82 * @param insertValues Value inserted 83 * @param tableName Table name 84 * @return Error or row id. when rdb store is not exsit, or store inserted return value is not OK, 85 * it will return to error 86 */ 87 int64_t Insert(const OHOS::NativeRdb::ValuesBucket &insertValues, const std::string tableName); 88 89 /** 90 * Update value in table 91 * 92 * @param tableName Table name 93 * @param changedRows Changed rows 94 * @param values Update value 95 * @param whereClause Where clause 96 * @param whereArgs Condition arguments 97 * @return Returns 0 success. Otherwise fail 98 */ 99 int32_t Update(const std::string &tableName, int32_t &changedRows, const OHOS::NativeRdb::ValuesBucket &values, 100 const std::string &whereClause, const std::vector<std::string> &whereArgs); 101 102 /** 103 * Delete rows in table 104 * 105 * @param tableName Table name 106 * @param changedRows Changed rows 107 * @param whereClause Where clause 108 * @param whereArgs Condition arguments 109 * @return Returns 0 success. Otherwise fail 110 */ 111 int32_t Delete(const std::string &tableName, int32_t &changedRows, const std::string &whereClause, 112 const std::vector<std::string> &whereArgs); 113 114 /** 115 * Query columns in table 116 * 117 * @param predicates Matching criteria 118 * @param columns Column 119 * @return Shared pointer of ResultSet 120 */ 121 std::shared_ptr<OHOS::NativeRdb::ResultSet> Query(const OHOS::NativeRdb::AbsRdbPredicates &predicates, 122 const std::vector<std::string> &columns); 123 124 std::shared_ptr<OHOS::NativeRdb::ResultSet> QuerySql(const std::string &sql, 125 const std::vector<std::string> &selectionArgs); 126 127 int32_t BeginTransaction(); 128 129 int32_t Commit(); 130 131 int32_t RollBack(); 132 133 /** 134 * Count 135 * 136 * @param outValue Number of queries found 137 * @param predicates Matching criteria 138 * @return Returns 0 success. Otherwise fail 139 */ 140 int32_t Count(int64_t &outValue, const OHOS::NativeRdb::AbsRdbPredicates &predicates); 141 142 private: 143 NetFirewallDataBase(); 144 void BackupDatebase(); 145 bool RestoreDatabaseWhenInit(); 146 bool RestoreDatabase(); 147 148 static std::shared_ptr<NetFirewallDataBase> instance_; 149 std::shared_ptr<OHOS::NativeRdb::RdbStore> store_; 150 std::atomic<bool> backing_ = false; 151 }; 152 153 class NetFirewallDataBaseCallBack : public OHOS::NativeRdb::RdbOpenCallback { 154 public: 155 int32_t OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override; 156 157 int32_t OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 158 159 int32_t OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) override; 160 }; 161 162 enum FirewallDBErrCode { 163 FIREWALL_OK = 0, 164 FIREWALL_FAILURE = -1, 165 FIREWALL_RDB_EXECUTE_FAILTURE = -2, 166 FIREWALL_RDB_NO_INIT = -3, 167 FIREWALL_RDB_EMPTY = -4, 168 FIREWALL_PERMISSION_DENIED = -5, 169 FIREWALL_NOP = -6, 170 FIREWALL_OVERFLOW = -7, 171 }; 172 } // namespace NetManagerStandard 173 } // namespace OHOS 174 175 #endif // FIREWALL_DATABASE_H