1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#include "net_proxy_userinfo.h" 17b1b8bc3fSopenharmony_ci 18b1b8bc3fSopenharmony_ci#include <cinttypes> 19b1b8bc3fSopenharmony_ci#include <securec.h> 20b1b8bc3fSopenharmony_ci#include <unistd.h> 21b1b8bc3fSopenharmony_ci 22b1b8bc3fSopenharmony_ci#include "net_mgr_log_wrapper.h" 23b1b8bc3fSopenharmony_ci#include "rdb_common.h" 24b1b8bc3fSopenharmony_ci 25b1b8bc3fSopenharmony_ciusing namespace OHOS::NativeRdb; 26b1b8bc3fSopenharmony_ciusing namespace OHOS::NetManagerStandard; 27b1b8bc3fSopenharmony_ci 28b1b8bc3fSopenharmony_cinamespace { 29b1b8bc3fSopenharmony_cistatic const int32_t RDB_VERSION = 0; 30b1b8bc3fSopenharmony_cistatic const std::string NET_CONN_PROXY_DATABASE_FILE = "net_conn_proxy.db"; 31b1b8bc3fSopenharmony_ci 32b1b8bc3fSopenharmony_cistatic const std::string NETCONNPROXY_TABLE_NAME = "net_userinfo"; 33b1b8bc3fSopenharmony_cistatic const std::string NETCONNPROXY_PRIM_KEY_COL = "proxypasswd"; 34b1b8bc3fSopenharmony_cistatic const std::string NETCONNPROXY_HOST_COL = "host"; 35b1b8bc3fSopenharmony_cistatic const std::string NETCONNPROXY_PASS_COL = "pass"; 36b1b8bc3fSopenharmony_cistatic const std::string NETCONNPROXY_PRIMARY_KEY = "ProxyPasswd"; 37b1b8bc3fSopenharmony_cistatic const std::string NETCONNPROXY_BUNDLENAME = "net_conn_permission"; 38b1b8bc3fSopenharmony_ci 39b1b8bc3fSopenharmony_cistatic const std::string CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + NETCONNPROXY_TABLE_NAME + " (" + 40b1b8bc3fSopenharmony_ci NETCONNPROXY_PRIM_KEY_COL + " TEXT PRIMARY KEY, " + NETCONNPROXY_HOST_COL + 41b1b8bc3fSopenharmony_ci " TEXT, " + NETCONNPROXY_PASS_COL + " TEXT);"; 42b1b8bc3fSopenharmony_ci 43b1b8bc3fSopenharmony_ciconst std::string NETMGR_BASE_PATH = "/data/service/el1/public/netmanager/"; 44b1b8bc3fSopenharmony_ci} // namespace 45b1b8bc3fSopenharmony_ci 46b1b8bc3fSopenharmony_ciint32_t DataBaseRdbOpenCallBack::OnCreate(NativeRdb::RdbStore &store) 47b1b8bc3fSopenharmony_ci{ 48b1b8bc3fSopenharmony_ci NETMGR_LOG_D("net_conn permission database onCreate"); 49b1b8bc3fSopenharmony_ci return NativeRdb::E_OK; 50b1b8bc3fSopenharmony_ci} 51b1b8bc3fSopenharmony_ci 52b1b8bc3fSopenharmony_ciint32_t DataBaseRdbOpenCallBack::OnOpen(NativeRdb::RdbStore &store) 53b1b8bc3fSopenharmony_ci{ 54b1b8bc3fSopenharmony_ci NETMGR_LOG_D("net_conn permission database onOpen, create table"); 55b1b8bc3fSopenharmony_ci return store.ExecuteSql(CREATE_TABLE); 56b1b8bc3fSopenharmony_ci} 57b1b8bc3fSopenharmony_ci 58b1b8bc3fSopenharmony_ciint32_t DataBaseRdbOpenCallBack::OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion) 59b1b8bc3fSopenharmony_ci{ 60b1b8bc3fSopenharmony_ci NETMGR_LOG_D("net_conn permission database upgrade"); 61b1b8bc3fSopenharmony_ci return OHOS::NativeRdb::E_OK; 62b1b8bc3fSopenharmony_ci} 63b1b8bc3fSopenharmony_ci 64b1b8bc3fSopenharmony_ciNetProxyUserinfo &NetProxyUserinfo::GetInstance() 65b1b8bc3fSopenharmony_ci{ 66b1b8bc3fSopenharmony_ci static NetProxyUserinfo instance; 67b1b8bc3fSopenharmony_ci return instance; 68b1b8bc3fSopenharmony_ci} 69b1b8bc3fSopenharmony_ci 70b1b8bc3fSopenharmony_civoid NetProxyUserinfo::SaveHttpProxyHostPass(const HttpProxy &httpProxy) 71b1b8bc3fSopenharmony_ci{ 72b1b8bc3fSopenharmony_ci NETMGR_LOG_I("net_conn database save user and pass info"); 73b1b8bc3fSopenharmony_ci if (rdbStore_ == nullptr) { 74b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn save rdbStore_ is empty"); 75b1b8bc3fSopenharmony_ci return; 76b1b8bc3fSopenharmony_ci } 77b1b8bc3fSopenharmony_ci 78b1b8bc3fSopenharmony_ci if (httpProxy.GetUsername().size() == 0 || httpProxy.GetPassword().size() == 0) { 79b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn userPass info is empty"); 80b1b8bc3fSopenharmony_ci return; 81b1b8bc3fSopenharmony_ci } 82b1b8bc3fSopenharmony_ci 83b1b8bc3fSopenharmony_ci NativeRdb::ValuesBucket valuesBucket; 84b1b8bc3fSopenharmony_ci std::string userTemp = httpProxy.GetUsername(); 85b1b8bc3fSopenharmony_ci std::string passTemp = httpProxy.GetPassword(); 86b1b8bc3fSopenharmony_ci valuesBucket.Clear(); 87b1b8bc3fSopenharmony_ci valuesBucket.PutString(NETCONNPROXY_PRIM_KEY_COL, NETCONNPROXY_PRIMARY_KEY); 88b1b8bc3fSopenharmony_ci valuesBucket.PutString(NETCONNPROXY_HOST_COL, userTemp); 89b1b8bc3fSopenharmony_ci valuesBucket.PutString(NETCONNPROXY_PASS_COL, passTemp); 90b1b8bc3fSopenharmony_ci 91b1b8bc3fSopenharmony_ci errno_t userErrCode = memset_s(userTemp.data(), userTemp.size(), 0, userTemp.size()); 92b1b8bc3fSopenharmony_ci if (userErrCode != 0) { 93b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn userData memory clearing failed, errCode=%{public}d", userErrCode); 94b1b8bc3fSopenharmony_ci } 95b1b8bc3fSopenharmony_ci errno_t passErrCode = memset_s(passTemp.data(), passTemp.size(), 0, passTemp.size()); 96b1b8bc3fSopenharmony_ci if (passErrCode != 0) { 97b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn passData memory clearing failed, errCode=%{public}d", passErrCode); 98b1b8bc3fSopenharmony_ci } 99b1b8bc3fSopenharmony_ci 100b1b8bc3fSopenharmony_ci int64_t outRowId; 101b1b8bc3fSopenharmony_ci int32_t errCode = rdbStore_->InsertWithConflictResolution(outRowId, NETCONNPROXY_TABLE_NAME, valuesBucket, 102b1b8bc3fSopenharmony_ci ConflictResolution::ON_CONFLICT_REPLACE); 103b1b8bc3fSopenharmony_ci if (errCode != NativeRdb::E_OK) { 104b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn database rdb store insert failed, errCode=%{public}d", errCode); 105b1b8bc3fSopenharmony_ci return; 106b1b8bc3fSopenharmony_ci } 107b1b8bc3fSopenharmony_ci NETMGR_LOG_D("net_conn database save user and pass info end"); 108b1b8bc3fSopenharmony_ci} 109b1b8bc3fSopenharmony_ci 110b1b8bc3fSopenharmony_civoid NetProxyUserinfo::GetHttpProxyHostPass(HttpProxy &httpProxy) 111b1b8bc3fSopenharmony_ci{ 112b1b8bc3fSopenharmony_ci if (rdbStore_ == nullptr) { 113b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn get rdbStore_ is empty"); 114b1b8bc3fSopenharmony_ci return; 115b1b8bc3fSopenharmony_ci } 116b1b8bc3fSopenharmony_ci 117b1b8bc3fSopenharmony_ci std::vector<std::string> columns; 118b1b8bc3fSopenharmony_ci NativeRdb::AbsRdbPredicates dirAbsPred(NETCONNPROXY_TABLE_NAME); 119b1b8bc3fSopenharmony_ci dirAbsPred.EqualTo(NETCONNPROXY_PRIM_KEY_COL, NETCONNPROXY_PRIMARY_KEY); 120b1b8bc3fSopenharmony_ci auto resultSet = rdbStore_->Query(dirAbsPred, columns); 121b1b8bc3fSopenharmony_ci if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) { 122b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn database rdb store query failed"); 123b1b8bc3fSopenharmony_ci return; 124b1b8bc3fSopenharmony_ci } 125b1b8bc3fSopenharmony_ci 126b1b8bc3fSopenharmony_ci int32_t columnIndex; 127b1b8bc3fSopenharmony_ci std::string user; 128b1b8bc3fSopenharmony_ci std::string pass; 129b1b8bc3fSopenharmony_ci resultSet->GetColumnIndex(NETCONNPROXY_HOST_COL, columnIndex); 130b1b8bc3fSopenharmony_ci resultSet->GetString(columnIndex, user); 131b1b8bc3fSopenharmony_ci resultSet->GetColumnIndex(NETCONNPROXY_PASS_COL, columnIndex); 132b1b8bc3fSopenharmony_ci resultSet->GetString(columnIndex, pass); 133b1b8bc3fSopenharmony_ci 134b1b8bc3fSopenharmony_ci SecureData userData; 135b1b8bc3fSopenharmony_ci userData.append(user.c_str(), user.size()); 136b1b8bc3fSopenharmony_ci httpProxy.SetUserName(userData); 137b1b8bc3fSopenharmony_ci SecureData passData; 138b1b8bc3fSopenharmony_ci passData.append(pass.c_str(), pass.size()); 139b1b8bc3fSopenharmony_ci httpProxy.SetPassword(passData); 140b1b8bc3fSopenharmony_ci 141b1b8bc3fSopenharmony_ci errno_t userErrCode = memset_s(user.data(), user.size(), 0, user.size()); 142b1b8bc3fSopenharmony_ci if (userErrCode != 0) { 143b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn userData memory clearing failed, errCode=%{public}d", userErrCode); 144b1b8bc3fSopenharmony_ci } 145b1b8bc3fSopenharmony_ci errno_t passErrCode = memset_s(pass.data(), pass.size(), 0, pass.size()); 146b1b8bc3fSopenharmony_ci if (passErrCode != 0) { 147b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn passData memory clearing failed, errCode=%{public}d", passErrCode); 148b1b8bc3fSopenharmony_ci } 149b1b8bc3fSopenharmony_ci NETMGR_LOG_D("net_conn database get host and pass info end"); 150b1b8bc3fSopenharmony_ci} 151b1b8bc3fSopenharmony_ci 152b1b8bc3fSopenharmony_ciNetProxyUserinfo::NetProxyUserinfo() 153b1b8bc3fSopenharmony_ci{ 154b1b8bc3fSopenharmony_ci std::string databaseDir = NETMGR_BASE_PATH; 155b1b8bc3fSopenharmony_ci std::string bundleName = NETCONNPROXY_BUNDLENAME; 156b1b8bc3fSopenharmony_ci std::string name = NET_CONN_PROXY_DATABASE_FILE; 157b1b8bc3fSopenharmony_ci std::string realPath = databaseDir + name; 158b1b8bc3fSopenharmony_ci NativeRdb::RdbStoreConfig config(std::move(realPath)); 159b1b8bc3fSopenharmony_ci config.SetBundleName(bundleName); 160b1b8bc3fSopenharmony_ci config.SetName(std::move(name)); 161b1b8bc3fSopenharmony_ci config.SetEncryptStatus(true); 162b1b8bc3fSopenharmony_ci 163b1b8bc3fSopenharmony_ci int32_t errCode = NativeRdb::E_OK; 164b1b8bc3fSopenharmony_ci DataBaseRdbOpenCallBack callBack; 165b1b8bc3fSopenharmony_ci rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(config, RDB_VERSION, callBack, errCode); 166b1b8bc3fSopenharmony_ci if (rdbStore_ == nullptr) { 167b1b8bc3fSopenharmony_ci NETMGR_LOG_E("net_conn database get rdb store failed, errCode=%{public}d", errCode); 168b1b8bc3fSopenharmony_ci } 169b1b8bc3fSopenharmony_ci} 170