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 #include "net_proxy_userinfo.h"
17
18 #include <cinttypes>
19 #include <securec.h>
20 #include <unistd.h>
21
22 #include "net_mgr_log_wrapper.h"
23 #include "rdb_common.h"
24
25 using namespace OHOS::NativeRdb;
26 using namespace OHOS::NetManagerStandard;
27
28 namespace {
29 static const int32_t RDB_VERSION = 0;
30 static const std::string NET_CONN_PROXY_DATABASE_FILE = "net_conn_proxy.db";
31
32 static const std::string NETCONNPROXY_TABLE_NAME = "net_userinfo";
33 static const std::string NETCONNPROXY_PRIM_KEY_COL = "proxypasswd";
34 static const std::string NETCONNPROXY_HOST_COL = "host";
35 static const std::string NETCONNPROXY_PASS_COL = "pass";
36 static const std::string NETCONNPROXY_PRIMARY_KEY = "ProxyPasswd";
37 static const std::string NETCONNPROXY_BUNDLENAME = "net_conn_permission";
38
39 static const std::string CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + NETCONNPROXY_TABLE_NAME + " (" +
40 NETCONNPROXY_PRIM_KEY_COL + " TEXT PRIMARY KEY, " + NETCONNPROXY_HOST_COL +
41 " TEXT, " + NETCONNPROXY_PASS_COL + " TEXT);";
42
43 const std::string NETMGR_BASE_PATH = "/data/service/el1/public/netmanager/";
44 } // namespace
45
OnCreate(NativeRdb::RdbStore & store)46 int32_t DataBaseRdbOpenCallBack::OnCreate(NativeRdb::RdbStore &store)
47 {
48 NETMGR_LOG_D("net_conn permission database onCreate");
49 return NativeRdb::E_OK;
50 }
51
OnOpen(NativeRdb::RdbStore & store)52 int32_t DataBaseRdbOpenCallBack::OnOpen(NativeRdb::RdbStore &store)
53 {
54 NETMGR_LOG_D("net_conn permission database onOpen, create table");
55 return store.ExecuteSql(CREATE_TABLE);
56 }
57
OnUpgrade(NativeRdb::RdbStore & rdbStore,int32_t currentVersion,int32_t targetVersion)58 int32_t DataBaseRdbOpenCallBack::OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t currentVersion, int32_t targetVersion)
59 {
60 NETMGR_LOG_D("net_conn permission database upgrade");
61 return OHOS::NativeRdb::E_OK;
62 }
63
GetInstance()64 NetProxyUserinfo &NetProxyUserinfo::GetInstance()
65 {
66 static NetProxyUserinfo instance;
67 return instance;
68 }
69
SaveHttpProxyHostPass(const HttpProxy & httpProxy)70 void NetProxyUserinfo::SaveHttpProxyHostPass(const HttpProxy &httpProxy)
71 {
72 NETMGR_LOG_I("net_conn database save user and pass info");
73 if (rdbStore_ == nullptr) {
74 NETMGR_LOG_E("net_conn save rdbStore_ is empty");
75 return;
76 }
77
78 if (httpProxy.GetUsername().size() == 0 || httpProxy.GetPassword().size() == 0) {
79 NETMGR_LOG_E("net_conn userPass info is empty");
80 return;
81 }
82
83 NativeRdb::ValuesBucket valuesBucket;
84 std::string userTemp = httpProxy.GetUsername();
85 std::string passTemp = httpProxy.GetPassword();
86 valuesBucket.Clear();
87 valuesBucket.PutString(NETCONNPROXY_PRIM_KEY_COL, NETCONNPROXY_PRIMARY_KEY);
88 valuesBucket.PutString(NETCONNPROXY_HOST_COL, userTemp);
89 valuesBucket.PutString(NETCONNPROXY_PASS_COL, passTemp);
90
91 errno_t userErrCode = memset_s(userTemp.data(), userTemp.size(), 0, userTemp.size());
92 if (userErrCode != 0) {
93 NETMGR_LOG_E("net_conn userData memory clearing failed, errCode=%{public}d", userErrCode);
94 }
95 errno_t passErrCode = memset_s(passTemp.data(), passTemp.size(), 0, passTemp.size());
96 if (passErrCode != 0) {
97 NETMGR_LOG_E("net_conn passData memory clearing failed, errCode=%{public}d", passErrCode);
98 }
99
100 int64_t outRowId;
101 int32_t errCode = rdbStore_->InsertWithConflictResolution(outRowId, NETCONNPROXY_TABLE_NAME, valuesBucket,
102 ConflictResolution::ON_CONFLICT_REPLACE);
103 if (errCode != NativeRdb::E_OK) {
104 NETMGR_LOG_E("net_conn database rdb store insert failed, errCode=%{public}d", errCode);
105 return;
106 }
107 NETMGR_LOG_D("net_conn database save user and pass info end");
108 }
109
GetHttpProxyHostPass(HttpProxy & httpProxy)110 void NetProxyUserinfo::GetHttpProxyHostPass(HttpProxy &httpProxy)
111 {
112 if (rdbStore_ == nullptr) {
113 NETMGR_LOG_E("net_conn get rdbStore_ is empty");
114 return;
115 }
116
117 std::vector<std::string> columns;
118 NativeRdb::AbsRdbPredicates dirAbsPred(NETCONNPROXY_TABLE_NAME);
119 dirAbsPred.EqualTo(NETCONNPROXY_PRIM_KEY_COL, NETCONNPROXY_PRIMARY_KEY);
120 auto resultSet = rdbStore_->Query(dirAbsPred, columns);
121 if ((resultSet == nullptr) || (resultSet->GoToFirstRow() != NativeRdb::E_OK)) {
122 NETMGR_LOG_E("net_conn database rdb store query failed");
123 return;
124 }
125
126 int32_t columnIndex;
127 std::string user;
128 std::string pass;
129 resultSet->GetColumnIndex(NETCONNPROXY_HOST_COL, columnIndex);
130 resultSet->GetString(columnIndex, user);
131 resultSet->GetColumnIndex(NETCONNPROXY_PASS_COL, columnIndex);
132 resultSet->GetString(columnIndex, pass);
133
134 SecureData userData;
135 userData.append(user.c_str(), user.size());
136 httpProxy.SetUserName(userData);
137 SecureData passData;
138 passData.append(pass.c_str(), pass.size());
139 httpProxy.SetPassword(passData);
140
141 errno_t userErrCode = memset_s(user.data(), user.size(), 0, user.size());
142 if (userErrCode != 0) {
143 NETMGR_LOG_E("net_conn userData memory clearing failed, errCode=%{public}d", userErrCode);
144 }
145 errno_t passErrCode = memset_s(pass.data(), pass.size(), 0, pass.size());
146 if (passErrCode != 0) {
147 NETMGR_LOG_E("net_conn passData memory clearing failed, errCode=%{public}d", passErrCode);
148 }
149 NETMGR_LOG_D("net_conn database get host and pass info end");
150 }
151
NetProxyUserinfo()152 NetProxyUserinfo::NetProxyUserinfo()
153 {
154 std::string databaseDir = NETMGR_BASE_PATH;
155 std::string bundleName = NETCONNPROXY_BUNDLENAME;
156 std::string name = NET_CONN_PROXY_DATABASE_FILE;
157 std::string realPath = databaseDir + name;
158 NativeRdb::RdbStoreConfig config(std::move(realPath));
159 config.SetBundleName(bundleName);
160 config.SetName(std::move(name));
161 config.SetEncryptStatus(true);
162
163 int32_t errCode = NativeRdb::E_OK;
164 DataBaseRdbOpenCallBack callBack;
165 rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(config, RDB_VERSION, callBack, errCode);
166 if (rdbStore_ == nullptr) {
167 NETMGR_LOG_E("net_conn database get rdb store failed, errCode=%{public}d", errCode);
168 }
169 }
170