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