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