1 /*
2 * Copyright (C) 2021 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 "parser_util.h"
17
18 #include <cstdio>
19 #include <unistd.h>
20
21 #include "climits"
22 #include "cstdint"
23 #include "cstdio"
24 #include "cstdlib"
25 #include "cstring"
26 #include "config_policy_utils.h"
27 #include "data_storage_errors.h"
28 #include "data_storage_log_wrapper.h"
29 #include "json/config.h"
30 #include "json/reader.h"
31 #include "json/value.h"
32 #include "memory"
33 #include "new"
34 #include "opkey_data.h"
35 #include "pdp_profile_data.h"
36 #include "preferences_util.h"
37 #include "values_bucket.h"
38 #include "vector"
39
40 namespace OHOS {
41 namespace Telephony {
42 const char *PATH = "/system/etc/telephony/pdp_profile.json";
43 const char *ITEM_OPERATOR_INFOS = "operator_infos";
44 const char *ITEM_OPERATOR_NAME = "operator_name";
45 const char *ITEM_AUTH_USER = "auth_user";
46 const char *ITEM_AUTH_PWD = "auth_pwd";
47 const char *ITEM_AUTH_TYPE = "auth_type";
48 const char *ITEM_MCC = "mcc";
49 const char *ITEM_MNC = "mnc";
50 const char *ITEM_APN = "apn";
51 const char *ITEM_APN_TYPES = "apn_types";
52 const char *ITEM_IP_ADDRESS = "ip_addr";
53 const char *ITEM_MMS_IP_ADDRESS = "mms_ip_addr";
54 const char *ITEM_HOME_URL = "home_url";
55 const char *APN_VERSION = "apn_version";
56 const char *OPKEY_INFO_PATH = "etc/telephony/OpkeyInfo.json";
57 const char *ITEM_OPERATOR_ID = "operator_id";
58 const char *ITEM_RULE = "rule";
59 const char *ITEM_MCCMNC = "mcc_mnc";
60 const char *ITEM_GID_ONE = "gid1";
61 const char *ITEM_GID_TWO = "gid2";
62 const char *ITEM_IMSI = "imsi";
63 const char *ITEM_SPN = "spn";
64 const char *ITEM_ICCID = "iccid";
65 const char *ITEM_OPERATOR_NAME_OPKEY = "operator_name";
66 const char *ITEM_OPERATOR_KEY = "operator_key";
67 const char *ITEM_OPERATOR_KEY_EXT = "operator_key_ext";
68 const int MAX_BYTE_LEN = 10 * 1024 * 1024;
69
ParserPdpProfileJson(std::vector<PdpProfile> & vec)70 int ParserUtil::ParserPdpProfileJson(std::vector<PdpProfile> &vec)
71 {
72 char *content = nullptr;
73 int ret = LoaderJsonFile(content, PATH);
74 if (ret != DATA_STORAGE_SUCCESS) {
75 DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson LoaderJsonFile is fail!\n");
76 return ret;
77 }
78 const int contentLength = strlen(content);
79 const std::string rawJson(content);
80 free(content);
81 content = nullptr;
82 JSONCPP_STRING err;
83 Json::Value root;
84 Json::CharReaderBuilder builder;
85 Json::CharReader *reader(builder.newCharReader());
86 if (!reader->parse(rawJson.c_str(), rawJson.c_str() + contentLength, &root, &err)) {
87 DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson reader is error!\n");
88 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
89 }
90 delete reader;
91 reader = nullptr;
92 Json::Value itemRoots = root[ITEM_OPERATOR_INFOS];
93 if (itemRoots.size() == 0) {
94 DATA_STORAGE_LOGE("ParserUtil::ParserPdpProfileJson itemRoots size == 0!\n");
95 return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
96 }
97 ParserPdpProfileInfos(vec, itemRoots);
98 return DATA_STORAGE_SUCCESS;
99 }
100
ParserPdpProfileInfos(std::vector<PdpProfile> & vec,Json::Value & root)101 void ParserUtil::ParserPdpProfileInfos(std::vector<PdpProfile> &vec, Json::Value &root)
102 {
103 for (int32_t i = 0; i < static_cast<int32_t>(root.size()); i++) {
104 Json::Value itemRoot = root[i];
105 PdpProfile bean;
106 bean.profileName = itemRoot[ITEM_OPERATOR_NAME].asString();
107 bean.authUser = itemRoot[ITEM_AUTH_USER].asString();
108 bean.authPwd = itemRoot[ITEM_AUTH_PWD].asString();
109 std::string authTypeStr = itemRoot[ITEM_AUTH_TYPE].asString();
110 if (authTypeStr.empty()) {
111 bean.authType = 0;
112 } else {
113 bean.authType = atoi(authTypeStr.c_str());
114 }
115 bean.mcc = itemRoot[ITEM_MCC].asString();
116 bean.mnc = itemRoot[ITEM_MNC].asString();
117 bean.apn = itemRoot[ITEM_APN].asString();
118 bean.apnTypes = itemRoot[ITEM_APN_TYPES].asString();
119 bean.mmsIpAddress = itemRoot[ITEM_MMS_IP_ADDRESS].asString();
120 bean.proxyIpAddress = itemRoot[ITEM_IP_ADDRESS].asString();
121 bean.homeUrl = itemRoot[ITEM_HOME_URL].asString();
122 vec.push_back(bean);
123 }
124 }
125
ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket & value,const PdpProfile & bean)126 void ParserUtil::ParserPdpProfileToValuesBucket(NativeRdb::ValuesBucket &value, const PdpProfile &bean)
127 {
128 value.PutString(PdpProfileData::PROFILE_NAME, bean.profileName);
129 value.PutString(PdpProfileData::MCC, bean.mcc);
130 value.PutString(PdpProfileData::MNC, bean.mnc);
131 std::string mccmnc(bean.mcc);
132 mccmnc.append(bean.mnc);
133 value.PutString(PdpProfileData::MCCMNC, mccmnc);
134 value.PutString(PdpProfileData::APN, bean.apn);
135 value.PutInt(PdpProfileData::AUTH_TYPE, bean.authType);
136 value.PutString(PdpProfileData::AUTH_USER, bean.authUser);
137 value.PutString(PdpProfileData::AUTH_PWD, bean.authPwd);
138 value.PutString(PdpProfileData::APN_TYPES, bean.apnTypes);
139 value.PutBool(PdpProfileData::IS_ROAMING_APN, bean.isRoamingApn);
140 value.PutString(PdpProfileData::HOME_URL, bean.homeUrl);
141 value.PutString(PdpProfileData::PROXY_IP_ADDRESS, bean.proxyIpAddress);
142 value.PutString(PdpProfileData::MMS_IP_ADDRESS, bean.mmsIpAddress);
143 value.PutString(PdpProfileData::APN_PROTOCOL, bean.pdpProtocol);
144 value.PutString(PdpProfileData::APN_ROAM_PROTOCOL, bean.roamPdpProtocol);
145 }
146
ParseFromCustomSystem(std::vector<OpKey> & vec)147 bool ParserUtil::ParseFromCustomSystem(std::vector<OpKey> &vec)
148 {
149 DATA_STORAGE_LOGI("ParserUtil ParseFromCustomSystem");
150 CfgFiles *cfgFiles = GetCfgFiles(OPKEY_INFO_PATH);
151 if (cfgFiles == nullptr) {
152 DATA_STORAGE_LOGE("ParserUtil ParseFromCustomSystem cfgFiles is null");
153 return false;
154 }
155 char *filePath = nullptr;
156 int result = DATA_STORAGE_ERROR;
157 for (int32_t i = MAX_CFG_POLICY_DIRS_CNT - 1; i >= 0; i--) {
158 filePath = cfgFiles->paths[i];
159 if (filePath && *filePath != '\0') {
160 if (ParserOpKeyJson(vec, filePath) == DATA_STORAGE_SUCCESS) {
161 result = DATA_STORAGE_SUCCESS;
162 }
163 }
164 }
165 FreeCfgFiles(cfgFiles);
166 return result == DATA_STORAGE_SUCCESS;
167 }
168
ParserOpKeyJson(std::vector<OpKey> & vec,const char * path)169 int ParserUtil::ParserOpKeyJson(std::vector<OpKey> &vec, const char *path)
170 {
171 char *content = nullptr;
172 int ret = LoaderJsonFile(content, path);
173 if (ret != DATA_STORAGE_SUCCESS) {
174 DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyJson LoaderJsonFile is fail!\n");
175 return ret;
176 }
177 if (content == nullptr) {
178 DATA_STORAGE_LOGE("ParserUtil::content is nullptr!\n");
179 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
180 }
181 const int contentLength = strlen(content);
182 const std::string rawJson(content);
183 free(content);
184 content = nullptr;
185 JSONCPP_STRING err;
186 Json::Value root;
187 Json::CharReaderBuilder builder;
188 Json::CharReader *reader(builder.newCharReader());
189 if (!reader->parse(rawJson.c_str(), rawJson.c_str() + contentLength, &root, &err)) {
190 DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos reader is error!\n");
191 return static_cast<int>(LoadProFileErrorType::FILE_PARSER_ERROR);
192 }
193 delete reader;
194 reader = nullptr;
195 Json::Value itemRoots = root[ITEM_OPERATOR_ID];
196 if (itemRoots.size() == 0) {
197 DATA_STORAGE_LOGE("ParserUtil::ParserOpKeyInfos itemRoots size == 0!\n");
198 return static_cast<int>(LoadProFileErrorType::ITEM_SIZE_IS_NULL);
199 }
200 ParserOpKeyInfos(vec, itemRoots);
201 return DATA_STORAGE_SUCCESS;
202 }
203
ParserOpKeyInfos(std::vector<OpKey> & vec,Json::Value & root)204 void ParserUtil::ParserOpKeyInfos(std::vector<OpKey> &vec, Json::Value &root)
205 {
206 for (int i = 0; i < (int)root.size(); i++) {
207 Json::Value itemRoot = root[i];
208 OpKey bean;
209 Json::Value ruleRoot = itemRoot[ITEM_RULE];
210 bean.mccmnc = ruleRoot[ITEM_MCCMNC].asString();
211 bean.gid1 = ruleRoot[ITEM_GID_ONE].asString();
212 bean.gid2 = ruleRoot[ITEM_GID_TWO].asString();
213 bean.imsi = ruleRoot[ITEM_IMSI].asString();
214 bean.spn = ruleRoot[ITEM_SPN].asString();
215 bean.iccid = ruleRoot[ITEM_ICCID].asString();
216 bean.operatorName = itemRoot[ITEM_OPERATOR_NAME_OPKEY].asString();
217 bean.operatorKey = itemRoot[ITEM_OPERATOR_KEY].asString();
218 bean.operatorKeyExt = itemRoot[ITEM_OPERATOR_KEY_EXT].asString();
219 int ruleId = static_cast<int32_t>(RuleID::RULE_EMPTY);
220 if (!bean.mccmnc.empty()) {
221 ruleId += static_cast<int32_t>(RuleID::RULE_MCCMNC);
222 }
223 if (!bean.iccid.empty()) {
224 ruleId += static_cast<int32_t>(RuleID::RULE_ICCID);
225 }
226 if (!bean.imsi.empty()) {
227 ruleId += static_cast<int32_t>(RuleID::RULE_IMSI);
228 }
229 if (!bean.spn.empty()) {
230 ruleId += static_cast<int32_t>(RuleID::RULE_SPN);
231 }
232 if (!bean.gid1.empty()) {
233 ruleId += static_cast<int32_t>(RuleID::RULE_GID1);
234 }
235 if (!bean.gid2.empty()) {
236 ruleId += static_cast<int32_t>(RuleID::RULE_GID2);
237 }
238 bean.ruleId = ruleId;
239 vec.push_back(bean);
240 }
241 }
242
ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket & value,const OpKey & bean)243 void ParserUtil::ParserOpKeyToValuesBucket(NativeRdb::ValuesBucket &value, const OpKey &bean)
244 {
245 value.PutString(OpKeyData::MCCMNC, bean.mccmnc);
246 value.PutString(OpKeyData::GID1, bean.gid1);
247 value.PutString(OpKeyData::GID2, bean.gid2);
248 value.PutString(OpKeyData::IMSI, bean.imsi);
249 value.PutString(OpKeyData::SPN, bean.spn);
250 value.PutString(OpKeyData::ICCID, bean.iccid);
251 value.PutString(OpKeyData::OPERATOR_NAME, bean.operatorName);
252 value.PutString(OpKeyData::OPERATOR_KEY, bean.operatorKey);
253 value.PutString(OpKeyData::OPERATOR_KEY_EXT, bean.operatorKeyExt);
254 value.PutInt(OpKeyData::RULE_ID, bean.ruleId);
255 }
256
LoaderJsonFile(char * & content,const char * path) const257 int ParserUtil::LoaderJsonFile(char *&content, const char *path) const
258 {
259 long len = 0;
260 char realPath[PATH_MAX] = { 0x00 };
261 if (realpath(path, realPath) == nullptr) {
262 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile realpath fail! #PATH: %{public}s", path);
263 return static_cast<int>(LoadProFileErrorType::REALPATH_FAIL);
264 }
265 FILE *f = fopen(realPath, "rb");
266 if (f == nullptr) {
267 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile file is null!");
268 return static_cast<int>(LoadProFileErrorType::OPEN_FILE_ERROR);
269 }
270 int ret_seek_end = fseek(f, 0, SEEK_END);
271 if (ret_seek_end != 0) {
272 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_end != 0!");
273 CloseFile(f);
274 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
275 }
276 len = ftell(f);
277 int ret_seek_set = fseek(f, 0, SEEK_SET);
278 if (ret_seek_set != 0) {
279 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_seek_set != 0!");
280 CloseFile(f);
281 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
282 }
283 if (len == 0 || len > static_cast<long>(MAX_BYTE_LEN)) {
284 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile len <= 0 or len > LONG_MAX!");
285 CloseFile(f);
286 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
287 }
288 content = static_cast<char *>(malloc(len + 1));
289 if (content == nullptr) {
290 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile malloc content fail!");
291 CloseFile(f);
292 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
293 }
294 if (memset_s(content, len + 1, 0, len + 1) != EOK) {
295 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile memset_s failed");
296 free(content);
297 content = nullptr;
298 CloseFile(f);
299 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
300 }
301 size_t ret_read = fread(content, 1, len, f);
302 if (ret_read != static_cast<size_t>(len)) {
303 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_read != len!");
304 free(content);
305 content = nullptr;
306 CloseFile(f);
307 return static_cast<int>(LoadProFileErrorType::LOAD_FILE_ERROR);
308 }
309 return CloseFile(f);
310 }
311
CloseFile(FILE * f) const312 int ParserUtil::CloseFile(FILE *f) const
313 {
314 int ret_close = fclose(f);
315 if (ret_close != 0) {
316 DATA_STORAGE_LOGE("ParserUtil::LoaderJsonFile ret_close != 0!");
317 return static_cast<int>(LoadProFileErrorType::CLOSE_FILE_ERROR);
318 }
319 return DATA_STORAGE_SUCCESS;
320 }
321 } // namespace Telephony
322 } // namespace OHOS
323