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