• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "set_apn_plugin.h"
17 
18 #include <memory>
19 #include "edm_constants.h"
20 #include "iplugin_manager.h"
21 #include "func_code_utils.h"
22 #include "edm_ipc_interface_code.h"
23 
24 namespace OHOS {
25 namespace EDM {
26 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(std::make_shared<SetApnPlugin>());
27 const std::set<std::string> ALL_APN_INFO_FIELD = {
28     "profile_name", "mcc", "mnc",
29     "apn", "apn_types", "auth_user",
30     "proxy_ip_address", "mms_ip_address", "auth_type",
31 };
32 
SetApnPlugin()33 SetApnPlugin::SetApnPlugin()
34 {
35     policyCode_ = EdmInterfaceCode::SET_APN_INFO;
36     policyName_ = PolicyName::POLICY_SET_APN_INFO;
37     permissionConfig_.typePermissions.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
38         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_APN);
39     permissionConfig_.apiType = IPlugin::ApiType::PUBLIC;
40     needSave_ = false;
41 }
42 
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)43 ErrCode SetApnPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
44     HandlePolicyData &policyData, int32_t userId)
45 {
46     EDMLOGI("SetApnPlugin::OnHandlePolicy start");
47     uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
48     FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
49     if (type == FuncOperateType::SET) {
50         const std::string flag = data.ReadString();
51         if (flag == EdmConstants::SetApn::ADD_FLAG) {
52             return HandleAdd(data);
53         } else if (flag == EdmConstants::SetApn::UPDATE_FLAG) {
54             return HandleUpdate(data);
55         } else if (flag == EdmConstants::SetApn::SET_PREFER_FLAG) {
56             return HandleSetPrefer(data);
57         } else {
58             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
59         }
60     } else if (type == FuncOperateType::REMOVE) {
61         return HandleRemove(data);
62     } else {
63         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
64     }
65 }
66 
HandleAdd(MessageParcel & data)67 ErrCode SetApnPlugin::HandleAdd(MessageParcel &data)
68 {
69     EDMLOGI("SetApnPlugin::HandleAdd start");
70     ApnUtilsPassword apnUtilsPassword;
71     std::map<std::string, std::string> apnInfo = ParserApnMap(data, apnUtilsPassword);
72     if (apnInfo.size() == 0) {
73         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
74     }
75 
76     if (apnUtilsPassword.passwordSize > ApnPassword::MAX_PASSWORD_SIZE) {
77         EDMLOGE("SetApnPlugin::HandleAdd password size over");
78         return EdmReturnErrCode::PARAM_ERROR;
79     }
80     return ApnUtils::ApnInsert(apnInfo, apnUtilsPassword);
81 }
82 
HandleUpdate(MessageParcel & data)83 ErrCode SetApnPlugin::HandleUpdate(MessageParcel &data)
84 {
85     EDMLOGI("SetApnPlugin::HandleUpdate start");
86     std::string apnId;
87     ApnUtilsPassword apnUtilsPassword;
88     if (data.ReadString(apnId)) {
89         if (apnId.empty()) {
90             return EdmReturnErrCode::PARAM_ERROR;
91         }
92         std::map<std::string, std::string> apnInfo = ParserApnMap(data, apnUtilsPassword);
93         if (apnInfo.size() == 0 && apnUtilsPassword.password == nullptr) {
94             return EdmReturnErrCode::PARAM_ERROR;
95         }
96         if (apnUtilsPassword.passwordSize > ApnPassword::MAX_PASSWORD_SIZE) {
97             EDMLOGE("SetApnPlugin::HandleUpdate password size over");
98             return EdmReturnErrCode::PARAM_ERROR;
99         }
100         return ApnUtils::ApnUpdate(apnInfo, apnId, apnUtilsPassword);
101     } else {
102         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
103     }
104 }
105 
HandleSetPrefer(MessageParcel & data)106 ErrCode SetApnPlugin::HandleSetPrefer(MessageParcel &data)
107 {
108     EDMLOGI("SetApnPlugin::HandleSetPrefer start");
109     std::string apnId;
110     if (data.ReadString(apnId)) {
111         if (apnId.empty()) {
112             return EdmReturnErrCode::PARAM_ERROR;
113         }
114         return ApnUtils::ApnSetPrefer(apnId);
115     } else {
116         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
117     }
118 }
119 
HandleRemove(MessageParcel & data)120 ErrCode SetApnPlugin::HandleRemove(MessageParcel &data)
121 {
122     EDMLOGI("SetApnPlugin::HandleRemove start");
123     std::string apnId;
124     if (data.ReadString(apnId)) {
125         if (apnId.empty()) {
126             return EdmReturnErrCode::PARAM_ERROR;
127         }
128         return ApnUtils::ApnDelete(apnId);
129     } else {
130         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
131     }
132 }
133 
ParserApnMap(MessageParcel & data,ApnUtilsPassword & apnPassword)134 std::map<std::string, std::string> SetApnPlugin::ParserApnMap(MessageParcel &data, ApnUtilsPassword &apnPassword)
135 {
136     EDMLOGI("SetApnPlugin::ParserApnMap start");
137     std::map<std::string, std::string> result;
138     int32_t mapSize = -1;
139     if (!data.ReadInt32(mapSize) || static_cast<uint32_t>(mapSize) > EdmConstants::SetApn::MAX_MAP_SIZE) {
140         EDMLOGE("ParserApnMap size error");
141         return {};
142     }
143     std::vector<std::string> keys(mapSize);
144     for (int32_t idx = 0; idx < mapSize; ++idx) {
145         keys[idx] = data.ReadString();
146     }
147     std::vector<std::string> values(mapSize);
148     for (int32_t idx = 0; idx < mapSize; ++idx) {
149         values[idx] = data.ReadString();
150     }
151     for (int32_t idx = 0; idx < mapSize; ++idx) {
152         result[keys[idx]] = values[idx];
153     }
154     int32_t pwdLen = data.ReadInt32();
155     if (pwdLen >= 0) {
156         apnPassword.passwordSize = static_cast<uint32_t>(pwdLen);
157         apnPassword.password = const_cast<char *>(data.ReadCString());
158     }
159     return result;
160 }
161 
GenerateApnMap(std::map<std::string,std::string> & apnInfo,MessageParcel & reply)162 void SetApnPlugin::GenerateApnMap(std::map<std::string, std::string> &apnInfo, MessageParcel &reply)
163 {
164     EDMLOGI("SetApnPlugin::GenerateApnMap start");
165     if (apnInfo.size() > EdmConstants::SetApn::MAX_MAP_SIZE) {
166         EDMLOGE("GenerateApnMap size error");
167         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
168         return;
169     }
170     int32_t size = 0;
171     for (const auto &[key, value] : apnInfo) {
172         if (ALL_APN_INFO_FIELD.find(key) != ALL_APN_INFO_FIELD.end()) {
173             size++;
174         }
175     }
176     reply.WriteInt32(ERR_OK);
177     reply.WriteInt32(size);
178     for (const auto &[key, value] : apnInfo) {
179         if (ALL_APN_INFO_FIELD.find(key) != ALL_APN_INFO_FIELD.end()) {
180             reply.WriteString(key);
181         }
182     }
183     for (const auto &[key, value] : apnInfo) {
184         if (ALL_APN_INFO_FIELD.find(key) != ALL_APN_INFO_FIELD.end()) {
185             reply.WriteString(value);
186         }
187     }
188 }
189 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)190 ErrCode SetApnPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId)
191 {
192     EDMLOGI("SetApnPlugin::OnGetPolicy start");
193     const std::string flag = data.ReadString();
194     if (flag == EdmConstants::SetApn::QUERY_ID_FLAG) {
195         return QueryId(data, reply);
196     } else if (flag == EdmConstants::SetApn::QUERY_INFO_FLAG) {
197         return QueryInfo(data, reply);
198     } else {
199         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
200         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
201     }
202     return ERR_OK;
203 }
204 
QueryInfo(MessageParcel & data,MessageParcel & reply)205 ErrCode SetApnPlugin::QueryInfo(MessageParcel &data, MessageParcel &reply)
206 {
207     EDMLOGI("SetApnPlugin::QueryInfo start");
208     std::string apnId;
209     if (data.ReadString(apnId)) {
210         if (apnId.empty()) {
211             return EdmReturnErrCode::PARAM_ERROR;
212         }
213         std::map<std::string, std::string> apnInfo = ApnUtils::ApnQuery(apnId);
214         GenerateApnMap(apnInfo, reply);
215         return ERR_OK;
216     } else {
217         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
218         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
219     }
220 }
221 
QueryId(MessageParcel & data,MessageParcel & reply)222 ErrCode SetApnPlugin::QueryId(MessageParcel &data, MessageParcel &reply)
223 {
224     EDMLOGI("SetApnPlugin::QueryId start");
225     ApnUtilsPassword apnUtilsPassword;
226     std::map<std::string, std::string> apnInfo = ParserApnMap(data, apnUtilsPassword);
227     std::vector<std::string> result = ApnUtils::ApnQuery(apnInfo);
228     reply.WriteInt32(ERR_OK);
229     reply.WriteInt32(static_cast<int32_t>(result.size()));
230     for (const auto &ele : result) {
231         reply.WriteString(ele);
232     }
233     return ERR_OK;
234 }
235 } // namespace EDM
236 } // namespace OHOS