18e745fdaSopenharmony_ci/* 28e745fdaSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License. 58e745fdaSopenharmony_ci * You may obtain a copy of the License at 68e745fdaSopenharmony_ci * 78e745fdaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88e745fdaSopenharmony_ci * 98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and 138e745fdaSopenharmony_ci * limitations under the License. 148e745fdaSopenharmony_ci */ 158e745fdaSopenharmony_ci 168e745fdaSopenharmony_ci#include "vpn_database_helper.h" 178e745fdaSopenharmony_ci 188e745fdaSopenharmony_ci#include <cstdlib> 198e745fdaSopenharmony_ci#include <filesystem> 208e745fdaSopenharmony_ci 218e745fdaSopenharmony_ci#include "net_manager_constants.h" 228e745fdaSopenharmony_ci#include "net_manager_ext_constants.h" 238e745fdaSopenharmony_ci#include "netmgr_ext_log_wrapper.h" 248e745fdaSopenharmony_ci#include "vpn_database_defines.h" 258e745fdaSopenharmony_ci 268e745fdaSopenharmony_cinamespace OHOS { 278e745fdaSopenharmony_cinamespace NetManagerStandard { 288e745fdaSopenharmony_ciusing namespace VpnDatabaseDefines; 298e745fdaSopenharmony_ci 308e745fdaSopenharmony_ciVpnDatabaseHelper &VpnDatabaseHelper::GetInstance() 318e745fdaSopenharmony_ci{ 328e745fdaSopenharmony_ci static VpnDatabaseHelper instance; 338e745fdaSopenharmony_ci return instance; 348e745fdaSopenharmony_ci} 358e745fdaSopenharmony_ci 368e745fdaSopenharmony_ciVpnDatabaseHelper::VpnDatabaseHelper() 378e745fdaSopenharmony_ci{ 388e745fdaSopenharmony_ci if (!std::filesystem::exists(VPN_DATABASE_PATH)) { 398e745fdaSopenharmony_ci std::error_code ec; 408e745fdaSopenharmony_ci if (std::filesystem::create_directories(VPN_DATABASE_PATH, ec)) { 418e745fdaSopenharmony_ci NETMGR_EXT_LOG_D("create_directories success :%{public}s", VPN_DATABASE_PATH.c_str()); 428e745fdaSopenharmony_ci } else { 438e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("create_directories error :%{public}s : %s", VPN_DATABASE_PATH.c_str(), 448e745fdaSopenharmony_ci ec.message().c_str()); 458e745fdaSopenharmony_ci } 468e745fdaSopenharmony_ci } 478e745fdaSopenharmony_ci std::string vpnDatabaseName = VPN_DATABASE_PATH + VPN_DB_NAME; 488e745fdaSopenharmony_ci int32_t errCode = OHOS::NativeRdb::E_OK; 498e745fdaSopenharmony_ci OHOS::NativeRdb::RdbStoreConfig config(vpnDatabaseName); 508e745fdaSopenharmony_ci config.SetSecurityLevel(NativeRdb::SecurityLevel::S1); 518e745fdaSopenharmony_ci VpnDataBaseCallBack sqliteOpenHelperCallback; 528e745fdaSopenharmony_ci store_ = OHOS::NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_OPEN_VERSION, sqliteOpenHelperCallback, errCode); 538e745fdaSopenharmony_ci if (errCode != OHOS::NativeRdb::E_OK && errCode != OHOS::NativeRdb::E_SQLITE_CORRUPT) { 548e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("GetRdbStore failed. errCode :%{public}d", errCode); 558e745fdaSopenharmony_ci } else { 568e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("GetRdbStore success"); 578e745fdaSopenharmony_ci } 588e745fdaSopenharmony_ci} 598e745fdaSopenharmony_ci 608e745fdaSopenharmony_ciint32_t VpnDataBaseCallBack::OnCreate(OHOS::NativeRdb::RdbStore &store) 618e745fdaSopenharmony_ci{ 628e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("DB OnCreate Enter"); 638e745fdaSopenharmony_ci std::string sql = 648e745fdaSopenharmony_ci "CREATE TABLE IF NOT EXISTS " + VPN_CONFIG_TABLE + "(" + std::string(VPN_CONFIG_TABLE_CREATE_PARAM) + ");"; 658e745fdaSopenharmony_ci int32_t ret = store.ExecuteSql(sql); 668e745fdaSopenharmony_ci if (ret != OHOS::NativeRdb::E_OK) { 678e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("Create table failed: %{public}d", ret); 688e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 698e745fdaSopenharmony_ci } 708e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 718e745fdaSopenharmony_ci} 728e745fdaSopenharmony_ci 738e745fdaSopenharmony_ciint32_t VpnDataBaseCallBack::OnUpgrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) 748e745fdaSopenharmony_ci{ 758e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("DB OnUpgrade Enter"); 768e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 778e745fdaSopenharmony_ci} 788e745fdaSopenharmony_ci 798e745fdaSopenharmony_ciint32_t VpnDataBaseCallBack::OnDowngrade(OHOS::NativeRdb::RdbStore &store, int32_t oldVersion, int32_t newVersion) 808e745fdaSopenharmony_ci{ 818e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("DB OnDowngrade Enter"); 828e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 838e745fdaSopenharmony_ci} 848e745fdaSopenharmony_ci 858e745fdaSopenharmony_ciint32_t VpnDatabaseHelper::InsertOrUpdateData(const sptr<VpnDataBean> &vpnBean) 868e745fdaSopenharmony_ci{ 878e745fdaSopenharmony_ci if (vpnBean == nullptr) { 888e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("InsertOrUpdateData vpnBean is nullptr"); 898e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_INVALID_PARAMETER; 908e745fdaSopenharmony_ci } 918e745fdaSopenharmony_ci 928e745fdaSopenharmony_ci if (IsVpnInfoExists(vpnBean->vpnId_)) { 938e745fdaSopenharmony_ci return UpdateData(vpnBean); 948e745fdaSopenharmony_ci } 958e745fdaSopenharmony_ci return InsertData(vpnBean); 968e745fdaSopenharmony_ci} 978e745fdaSopenharmony_ci 988e745fdaSopenharmony_cibool VpnDatabaseHelper::IsVpnInfoExists(const std::string &vpnId) 998e745fdaSopenharmony_ci{ 1008e745fdaSopenharmony_ci if (vpnId.empty()) { 1018e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("IsVpnInfoExists vpnId is empty"); 1028e745fdaSopenharmony_ci return false; 1038e745fdaSopenharmony_ci } 1048e745fdaSopenharmony_ci if (store_ == nullptr) { 1058e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("IsVpnInfoExists store_ is nullptr"); 1068e745fdaSopenharmony_ci return false; 1078e745fdaSopenharmony_ci } 1088e745fdaSopenharmony_ci 1098e745fdaSopenharmony_ci std::vector<std::string> columns; 1108e745fdaSopenharmony_ci OHOS::NativeRdb::RdbPredicates rdbPredicate{ VPN_CONFIG_TABLE }; 1118e745fdaSopenharmony_ci rdbPredicate.EqualTo(VPN_ID, vpnId); 1128e745fdaSopenharmony_ci auto queryResultSet = store_->Query(rdbPredicate, columns); 1138e745fdaSopenharmony_ci if (queryResultSet == nullptr) { 1148e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("Query error"); 1158e745fdaSopenharmony_ci return false; 1168e745fdaSopenharmony_ci } 1178e745fdaSopenharmony_ci 1188e745fdaSopenharmony_ci int32_t rowCount = 0; 1198e745fdaSopenharmony_ci int32_t ret = queryResultSet->GetRowCount(rowCount); 1208e745fdaSopenharmony_ci if (ret != OHOS::NativeRdb::E_OK) { 1218e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("get row count failed, ret:%{public}d", ret); 1228e745fdaSopenharmony_ci return false; 1238e745fdaSopenharmony_ci } 1248e745fdaSopenharmony_ci return rowCount == 1; 1258e745fdaSopenharmony_ci} 1268e745fdaSopenharmony_ci 1278e745fdaSopenharmony_civoid VpnDatabaseHelper::BindVpnData(NativeRdb::ValuesBucket &values, const sptr<VpnDataBean> &info) 1288e745fdaSopenharmony_ci{ 1298e745fdaSopenharmony_ci if (info == nullptr) { 1308e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("BindVpnData info is nullptr"); 1318e745fdaSopenharmony_ci return; 1328e745fdaSopenharmony_ci } 1338e745fdaSopenharmony_ci values.PutString(VPN_ID, info->vpnId_); 1348e745fdaSopenharmony_ci values.PutString(VPN_NAME, info->vpnName_); 1358e745fdaSopenharmony_ci values.PutInt(VPN_TYPE, info->vpnType_); 1368e745fdaSopenharmony_ci values.PutString(VPN_ADDRESS, info->vpnAddress_); 1378e745fdaSopenharmony_ci values.PutString(USER_NAME, info->userName_); 1388e745fdaSopenharmony_ci values.PutString(PASSWORD, info->password_); 1398e745fdaSopenharmony_ci values.PutInt(USER_ID, info->userId_); 1408e745fdaSopenharmony_ci values.PutInt(VPN_IS_LEGACY, info->isLegacy_); 1418e745fdaSopenharmony_ci values.PutInt(VPN_SAVE_LOGIN, info->saveLogin_); 1428e745fdaSopenharmony_ci values.PutString(VPN_FORWARDED_ROUTES, info->forwardingRoutes_); 1438e745fdaSopenharmony_ci values.PutString(VPN_DNS_ADDRESSES, info->dnsAddresses_); 1448e745fdaSopenharmony_ci values.PutString(VPN_SEARCH_DOMAINS, info->searchDomains_); 1458e745fdaSopenharmony_ci 1468e745fdaSopenharmony_ci values.PutString(OPENVPN_PORT, info->ovpnPort_); 1478e745fdaSopenharmony_ci values.PutInt(OPENVPN_PROTOCOL, info->ovpnProtocol_); 1488e745fdaSopenharmony_ci values.PutString(OPENVPN_CFG, info->ovpnConfig_); 1498e745fdaSopenharmony_ci values.PutInt(OPENVPN_AUTH_TYPE, info->ovpnAuthType_); 1508e745fdaSopenharmony_ci values.PutString(OPENVPN_ASKPASS, info->askpass_); 1518e745fdaSopenharmony_ci values.PutString(OPENVPN_CFG_FILE_PATH, info->ovpnConfigFilePath_); 1528e745fdaSopenharmony_ci values.PutString(OPENVPN_CA_CERT_FILE_PATH, info->ovpnCaCertFilePath_); 1538e745fdaSopenharmony_ci values.PutString(OPENVPN_USER_CERT_FILE_PATH, info->ovpnUserCertFilePath_); 1548e745fdaSopenharmony_ci values.PutString(OPENVPN_PRIVATE_KEY_FILE_PATH, info->ovpnPrivateKeyFilePath_); 1558e745fdaSopenharmony_ci 1568e745fdaSopenharmony_ci values.PutString(IPSEC_PRE_SHARE_KEY, info->ipsecPreSharedKey_); 1578e745fdaSopenharmony_ci values.PutString(IPSEC_IDENTIFIER, info->ipsecIdentifier_); 1588e745fdaSopenharmony_ci values.PutString(SWANCTL_CONF, info->swanctlConf_); 1598e745fdaSopenharmony_ci values.PutString(STRONGSWAN_CONF, info->strongswanConf_); 1608e745fdaSopenharmony_ci values.PutString(IPSEC_CA_CERT_CONF, info->ipsecCaCertConf_); 1618e745fdaSopenharmony_ci values.PutString(IPSEC_PRIVATE_USER_CERT_CONF, info->ipsecPrivateUserCertConf_); 1628e745fdaSopenharmony_ci values.PutString(IPSEC_PUBLIC_USER_CERT_CONF, info->ipsecPublicUserCertConf_); 1638e745fdaSopenharmony_ci values.PutString(IPSEC_PRIVATE_SERVER_CERT_CONF, info->ipsecPrivateServerCertConf_); 1648e745fdaSopenharmony_ci values.PutString(IPSEC_PUBLIC_SERVER_CERT_CONF, info->ipsecPublicServerCertConf_); 1658e745fdaSopenharmony_ci values.PutString(IPSEC_CA_CERT_FILE_PATH, info->ipsecCaCertFilePath_); 1668e745fdaSopenharmony_ci values.PutString(IPSEC_PRIVATE_USER_CERT_FILE_PATH, info->ipsecPrivateUserCertFilePath_); 1678e745fdaSopenharmony_ci values.PutString(IPSEC_PUBLIC_USER_CERT_FILE_PATH, info->ipsecPublicUserCertFilePath_); 1688e745fdaSopenharmony_ci values.PutString(IPSEC_PRIVATE_SERVER_CERT_FILE_PATH, info->ipsecPrivateServerCertFilePath_); 1698e745fdaSopenharmony_ci values.PutString(IPSEC_PUBLIC_SERVER_CERT_FILE_PATH, info->ipsecPublicServerCertFilePath_); 1708e745fdaSopenharmony_ci values.PutString(IPSEC_CONF, info->ipsecConf_); 1718e745fdaSopenharmony_ci values.PutString(IPSEC_SECRETS, info->ipsecSecrets_); 1728e745fdaSopenharmony_ci values.PutString(OPTIONS_L2TPD_CLIENT, info->optionsL2tpdClient_); 1738e745fdaSopenharmony_ci values.PutString(XL2TPD_CONF, info->xl2tpdConf_); 1748e745fdaSopenharmony_ci values.PutString(L2TP_SHARED_KEY, info->l2tpSharedKey_); 1758e745fdaSopenharmony_ci} 1768e745fdaSopenharmony_ci 1778e745fdaSopenharmony_ciint32_t VpnDatabaseHelper::InsertData(const sptr<VpnDataBean> &vpnBean) 1788e745fdaSopenharmony_ci{ 1798e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("InsertData"); 1808e745fdaSopenharmony_ci if (vpnBean == nullptr) { 1818e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("InsertData vpnBean is nullptr"); 1828e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_INVALID_PARAMETER; 1838e745fdaSopenharmony_ci } 1848e745fdaSopenharmony_ci if (store_ == nullptr) { 1858e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("InsertData store_ is nullptr"); 1868e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 1878e745fdaSopenharmony_ci } 1888e745fdaSopenharmony_ci 1898e745fdaSopenharmony_ci NativeRdb::ValuesBucket values; 1908e745fdaSopenharmony_ci BindVpnData(values, vpnBean); 1918e745fdaSopenharmony_ci int64_t rowId = 0; 1928e745fdaSopenharmony_ci int ret = store_->Insert(rowId, VPN_CONFIG_TABLE, values); 1938e745fdaSopenharmony_ci if (ret != NativeRdb::E_OK) { 1948e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("InsertData failed, result is %{public}d", ret); 1958e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 1968e745fdaSopenharmony_ci } 1978e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 1988e745fdaSopenharmony_ci} 1998e745fdaSopenharmony_ci 2008e745fdaSopenharmony_ciint32_t VpnDatabaseHelper::UpdateData(const sptr<VpnDataBean> &vpnBean) 2018e745fdaSopenharmony_ci{ 2028e745fdaSopenharmony_ci if (store_ == nullptr) { 2038e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("UpdateData store_ is nullptr"); 2048e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 2058e745fdaSopenharmony_ci } 2068e745fdaSopenharmony_ci if (vpnBean == nullptr) { 2078e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("UpdateData vpnBean is nullptr"); 2088e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_INVALID_PARAMETER; 2098e745fdaSopenharmony_ci } 2108e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("UpdateData"); 2118e745fdaSopenharmony_ci OHOS::NativeRdb::RdbPredicates rdbPredicate{ VPN_CONFIG_TABLE }; 2128e745fdaSopenharmony_ci rdbPredicate.EqualTo(VPN_ID, vpnBean->vpnId_); 2138e745fdaSopenharmony_ci NativeRdb::ValuesBucket values; 2148e745fdaSopenharmony_ci BindVpnData(values, vpnBean); 2158e745fdaSopenharmony_ci int32_t rowId = -1; 2168e745fdaSopenharmony_ci int32_t ret = store_->Update(rowId, values, rdbPredicate); 2178e745fdaSopenharmony_ci if (ret != OHOS::NativeRdb::E_OK) { 2188e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("UpdateData ret :%{public}d", ret); 2198e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 2208e745fdaSopenharmony_ci } 2218e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 2228e745fdaSopenharmony_ci} 2238e745fdaSopenharmony_ci 2248e745fdaSopenharmony_civoid VpnDatabaseHelper::GetVpnDataFromResultSet(const std::shared_ptr<OHOS::NativeRdb::ResultSet> &queryResultSet, 2258e745fdaSopenharmony_ci sptr<VpnDataBean> &vpnBean) 2268e745fdaSopenharmony_ci{ 2278e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_ID, vpnBean->vpnId_); 2288e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_NAME, vpnBean->vpnName_); 2298e745fdaSopenharmony_ci queryResultSet->GetInt(INDEX_VPN_TYPE, vpnBean->vpnType_); 2308e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_ADDRESS, vpnBean->vpnAddress_); 2318e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_USER_NAME, vpnBean->userName_); 2328e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_PASSWORD, vpnBean->password_); 2338e745fdaSopenharmony_ci queryResultSet->GetInt(INDEX_USER_ID, vpnBean->userId_); 2348e745fdaSopenharmony_ci queryResultSet->GetInt(INDEX_VPN_IS_LEGACY, vpnBean->isLegacy_); 2358e745fdaSopenharmony_ci queryResultSet->GetInt(INDEX_VPN_SAVE_LOGIN, vpnBean->saveLogin_); 2368e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_FORWARDED_ROUTES, vpnBean->forwardingRoutes_); 2378e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_DNS_ADDRESSES, vpnBean->dnsAddresses_); 2388e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_SEARCH_DOMAINS, vpnBean->searchDomains_); 2398e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_PORT, vpnBean->ovpnPort_); 2408e745fdaSopenharmony_ci queryResultSet->GetInt(INDEX_OPENVPN_PROTOCOL, vpnBean->ovpnProtocol_); 2418e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_CFG, vpnBean->ovpnConfig_); 2428e745fdaSopenharmony_ci queryResultSet->GetInt(INDEX_OPENVPN_AUTH_TYPE, vpnBean->ovpnAuthType_); 2438e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_ASKPASS, vpnBean->askpass_); 2448e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_CFG_FILE_PATH, vpnBean->ovpnConfigFilePath_); 2458e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_CA_CERT_FILE_PATH, vpnBean->ovpnCaCertFilePath_); 2468e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_USER_CERT_FILE_PATH, vpnBean->ovpnUserCertFilePath_); 2478e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPENVPN_PRIVATE_KEY_FILE_PATH, vpnBean->ovpnPrivateKeyFilePath_); 2488e745fdaSopenharmony_ci 2498e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PRE_SHARE_KEY, vpnBean->ipsecPreSharedKey_); 2508e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_IDENTIFIER, vpnBean->ipsecIdentifier_); 2518e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_SWANCTL_CONF, vpnBean->swanctlConf_); 2528e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_STRONGSWAN_CONF, vpnBean->strongswanConf_); 2538e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_CA_CERT_CONF, vpnBean->ipsecCaCertConf_); 2548e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PRIVATE_USER_CERT_CONF, vpnBean->ipsecPrivateUserCertConf_); 2558e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PUBLIC_USER_CERT_CONF, vpnBean->ipsecPublicUserCertConf_); 2568e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PRIVATE_SERVER_CERT_CONF, vpnBean->ipsecPrivateServerCertConf_); 2578e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PUBLIC_SERVER_CERT_CONF, vpnBean->ipsecPublicServerCertConf_); 2588e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_CA_CERT_FILE_PATH, vpnBean->ipsecCaCertFilePath_); 2598e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PRIVATE_USER_CERT_FILE_PATH, vpnBean->ipsecPrivateUserCertFilePath_); 2608e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PUBLIC_USER_CERT_FILE_PATH, vpnBean->ipsecPublicUserCertFilePath_); 2618e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PRIVATE_SERVER_CERT_FILE_PATH, vpnBean->ipsecPrivateServerCertFilePath_); 2628e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_PUBLIC_SERVER_CERT_FILE_PATH, vpnBean->ipsecPublicServerCertFilePath_); 2638e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_CONF, vpnBean->ipsecConf_); 2648e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_IPSEC_SECRETS, vpnBean->ipsecSecrets_); 2658e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_OPTIONS_L2TPD_CLIENT, vpnBean->optionsL2tpdClient_); 2668e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_XL2TPD_CONF, vpnBean->xl2tpdConf_); 2678e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_L2TP_SHARED_KEY, vpnBean->l2tpSharedKey_); 2688e745fdaSopenharmony_ci} 2698e745fdaSopenharmony_ci 2708e745fdaSopenharmony_ciint32_t VpnDatabaseHelper::QueryVpnData(sptr<VpnDataBean> &vpnBean, const std::string &vpnUuid) 2718e745fdaSopenharmony_ci{ 2728e745fdaSopenharmony_ci if (vpnBean == nullptr) { 2738e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryVpnData vpnBean is nullptr"); 2748e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_INVALID_PARAMETER; 2758e745fdaSopenharmony_ci } 2768e745fdaSopenharmony_ci if (vpnUuid.empty()) { 2778e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryVpnData vpnUuid is empty"); 2788e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_INVALID_PARAMETER; 2798e745fdaSopenharmony_ci } 2808e745fdaSopenharmony_ci 2818e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("QueryVpnData vpnUuid=%{public}s", vpnUuid.c_str()); 2828e745fdaSopenharmony_ci if (store_ == nullptr) { 2838e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryVpnData store_ is nullptr"); 2848e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 2858e745fdaSopenharmony_ci } 2868e745fdaSopenharmony_ci 2878e745fdaSopenharmony_ci std::vector<std::string> columns; 2888e745fdaSopenharmony_ci OHOS::NativeRdb::RdbPredicates rdbPredicate{ VPN_CONFIG_TABLE }; 2898e745fdaSopenharmony_ci rdbPredicate.EqualTo(VPN_ID, vpnUuid); 2908e745fdaSopenharmony_ci auto queryResultSet = store_->Query(rdbPredicate, columns); 2918e745fdaSopenharmony_ci if (queryResultSet == nullptr) { 2928e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryVpnData error"); 2938e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 2948e745fdaSopenharmony_ci } 2958e745fdaSopenharmony_ci int32_t rowCount = 0; 2968e745fdaSopenharmony_ci int ret = queryResultSet->GetRowCount(rowCount); 2978e745fdaSopenharmony_ci if (ret != OHOS::NativeRdb::E_OK) { 2988e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryVpnData failed, get row count failed, ret:%{public}d", ret); 2998e745fdaSopenharmony_ci return ret; 3008e745fdaSopenharmony_ci } 3018e745fdaSopenharmony_ci if (rowCount == 0) { 3028e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryVpnData result num is 0"); 3038e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 3048e745fdaSopenharmony_ci } 3058e745fdaSopenharmony_ci while (!queryResultSet->GoToNextRow()) { 3068e745fdaSopenharmony_ci GetVpnDataFromResultSet(queryResultSet, vpnBean); 3078e745fdaSopenharmony_ci if (vpnBean->vpnId_ == vpnUuid) { 3088e745fdaSopenharmony_ci return NETMANAGER_SUCCESS; 3098e745fdaSopenharmony_ci } 3108e745fdaSopenharmony_ci } 3118e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 3128e745fdaSopenharmony_ci} 3138e745fdaSopenharmony_ci 3148e745fdaSopenharmony_ciint32_t VpnDatabaseHelper::QueryAllData(std::vector<SysVpnConfig> &infos, const int32_t userId) 3158e745fdaSopenharmony_ci{ 3168e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("QueryAllData"); 3178e745fdaSopenharmony_ci if (store_ == nullptr) { 3188e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryAllData store_ is nullptr"); 3198e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 3208e745fdaSopenharmony_ci } 3218e745fdaSopenharmony_ci infos.clear(); 3228e745fdaSopenharmony_ci std::vector<std::string> columns; 3238e745fdaSopenharmony_ci OHOS::NativeRdb::RdbPredicates rdbPredicate{ VPN_CONFIG_TABLE }; 3248e745fdaSopenharmony_ci rdbPredicate.EqualTo(USER_ID, userId); 3258e745fdaSopenharmony_ci auto queryResultSet = store_->Query(rdbPredicate, columns); 3268e745fdaSopenharmony_ci if (queryResultSet == nullptr) { 3278e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryAllData error"); 3288e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 3298e745fdaSopenharmony_ci } 3308e745fdaSopenharmony_ci int32_t rowCount = 0; 3318e745fdaSopenharmony_ci int ret = queryResultSet->GetRowCount(rowCount); 3328e745fdaSopenharmony_ci if (ret != OHOS::NativeRdb::E_OK) { 3338e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryAllData failed, get row count failed, ret:%{public}d", ret); 3348e745fdaSopenharmony_ci return ret; 3358e745fdaSopenharmony_ci } 3368e745fdaSopenharmony_ci if (rowCount == 0) { 3378e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("QueryAllData result num is 0"); 3388e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 3398e745fdaSopenharmony_ci } 3408e745fdaSopenharmony_ci while (!queryResultSet->GoToNextRow()) { 3418e745fdaSopenharmony_ci SysVpnConfig info; 3428e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_ID, info.vpnId_); 3438e745fdaSopenharmony_ci queryResultSet->GetString(INDEX_VPN_NAME, info.vpnName_); 3448e745fdaSopenharmony_ci infos.emplace_back(info); 3458e745fdaSopenharmony_ci } 3468e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 3478e745fdaSopenharmony_ci} 3488e745fdaSopenharmony_ci 3498e745fdaSopenharmony_ciint32_t VpnDatabaseHelper::DeleteVpnData(const std::string &vpnUuid) 3508e745fdaSopenharmony_ci{ 3518e745fdaSopenharmony_ci NETMGR_EXT_LOG_I("DeleteVpnData"); 3528e745fdaSopenharmony_ci if (vpnUuid.empty()) { 3538e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("DeleteVpnData vpnUuid is empty"); 3548e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_INVALID_PARAMETER; 3558e745fdaSopenharmony_ci } 3568e745fdaSopenharmony_ci 3578e745fdaSopenharmony_ci if (store_ == nullptr) { 3588e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("DeleteVpnData store_ is nullptr"); 3598e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 3608e745fdaSopenharmony_ci } 3618e745fdaSopenharmony_ci int32_t deletedRows = -1; 3628e745fdaSopenharmony_ci OHOS::NativeRdb::RdbPredicates rdbPredicate{ VPN_CONFIG_TABLE }; 3638e745fdaSopenharmony_ci rdbPredicate.EqualTo(VPN_ID, vpnUuid); 3648e745fdaSopenharmony_ci int32_t result = store_->Delete(deletedRows, rdbPredicate); 3658e745fdaSopenharmony_ci if (result != NativeRdb::E_OK) { 3668e745fdaSopenharmony_ci NETMGR_EXT_LOG_E("DeleteVpnData failed, result is %{public}d", result); 3678e745fdaSopenharmony_ci return NETMANAGER_EXT_ERR_OPERATION_FAILED; 3688e745fdaSopenharmony_ci } 3698e745fdaSopenharmony_ci return NETMANAGER_EXT_SUCCESS; 3708e745fdaSopenharmony_ci} 3718e745fdaSopenharmony_ci} // namespace NetManagerStandard 3728e745fdaSopenharmony_ci} // namespace OHOS 373