1 /*
2 * Copyright (c) 2023 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 <memory>
17
18 #include "networkvpn_service_stub.h"
19 #include "net_manager_constants.h"
20 #include "netmanager_base_permission.h"
21 #include "netmgr_ext_log_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25
NetworkVpnServiceStub()26 NetworkVpnServiceStub::NetworkVpnServiceStub()
27 {
28 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_PREPARE] = {
29 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyPrepare};
30 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_START_VPN] = {
31 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplySetUpVpn};
32 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_PROTECT] = {
33 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyProtect};
34 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_STOP_VPN] = {
35 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyDestroyVpn};
36 #ifdef SUPPORT_SYSVPN
37 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_ADD_SYS_VPN_CONFIG] = {
38 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyAddSysVpnConfig};
39 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_DELETE_SYS_VPN_CONFIG] = {
40 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyDeleteSysVpnConfig};
41 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_GET_SYS_VPN_CONFIG_LIST] = {
42 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyGetSysVpnConfigList};
43 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_GET_SYS_VPN_CONFIG] = {
44 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyGetSysVpnConfig};
45 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_GET_CONNECTED_SYS_VPN_CONFIG] = {
46 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyGetConnectedSysVpnConfig};
47 #endif // SUPPORT_SYSVPN
48 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_REGISTER_EVENT_CALLBACK] = {
49 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyRegisterVpnEvent};
50 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_UNREGISTER_EVENT_CALLBACK] = {
51 Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyUnregisterVpnEvent};
52 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_CREATE_VPN_CONNECTION] = {
53 "", &NetworkVpnServiceStub::ReplyCreateVpnConnection};
54 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_FACTORYRESET_VPN] = {
55 "", &NetworkVpnServiceStub::ReplyFactoryResetVpn};
56 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_CREATE_VPN_CONNECTION_EXT] = {
57 "", &NetworkVpnServiceStub::ReplyCreateVpnConnection};
58 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_START_VPN_EXT] = {
59 "", &NetworkVpnServiceStub::ReplySetUpVpn};
60 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_PROTECT_EXT] = {
61 "", &NetworkVpnServiceStub::ReplyProtect};
62 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_STOP_VPN_EXT] = {
63 "", &NetworkVpnServiceStub::ReplyDestroyVpn};
64 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_REGISTER_BUNDLENAME] = {
65 "", &NetworkVpnServiceStub::ReplyRegisterBundleName};
66 permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_GET_SELF_APP_NAME] = {
67 "", &NetworkVpnServiceStub::ReplyGetSelfAppName};
68 }
69
CheckVpnPermission(std::string & strPermission)70 int32_t NetworkVpnServiceStub::CheckVpnPermission(std::string &strPermission)
71 {
72 if (!NetManagerPermission::IsSystemCaller()) {
73 NETMGR_EXT_LOG_E("is not system call");
74 return NETMANAGER_ERR_NOT_SYSTEM_CALL;
75 }
76 if (!strPermission.empty() && !NetManagerPermission::CheckPermission(strPermission)) {
77 NETMGR_EXT_LOG_E("Permission denied permission: %{public}s", strPermission.c_str());
78 return NETMANAGER_ERR_PERMISSION_DENIED;
79 }
80 return NETMANAGER_SUCCESS;
81 }
82
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)83 int32_t NetworkVpnServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
84 MessageOption &option)
85 {
86 if (NetworkVpnServiceStub::GetDescriptor() != data.ReadInterfaceToken()) {
87 NETMGR_EXT_LOG_E("descriptor checked failed");
88 return NETMANAGER_EXT_ERR_DESCRIPTOR_MISMATCH;
89 }
90 auto itr = permissionAndFuncMap_.find(static_cast<INetworkVpnService::MessageCode>(code));
91 if (itr != permissionAndFuncMap_.end()) {
92 if (itr->first >= INetworkVpnService::MessageCode::CMD_START_VPN_EXT &&
93 itr->first <= INetworkVpnService::MessageCode::CMD_GET_SELF_APP_NAME) {
94 NETMGR_EXT_LOG_I("enter OnRemoteRequest code %{public}d:", code);
95 auto serviceFunc = itr->second.serviceFunc;
96 if (serviceFunc != nullptr) {
97 return (this->*serviceFunc)(data, reply);
98 }
99 } else {
100 NETMGR_EXT_LOG_I("enter OnRemoteRequest code %{public}d:", code);
101 int32_t checkResult = CheckVpnPermission(itr->second.strPermission);
102 if (checkResult != NETMANAGER_SUCCESS) {
103 return checkResult;
104 }
105 auto serviceFunc = itr->second.serviceFunc;
106 if (serviceFunc != nullptr) {
107 return (this->*serviceFunc)(data, reply);
108 }
109 }
110 }
111
112 NETMGR_EXT_LOG_I("stub default case, need check");
113 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
114 }
115
ReplyPrepare(MessageParcel & data,MessageParcel & reply)116 int32_t NetworkVpnServiceStub::ReplyPrepare(MessageParcel &data, MessageParcel &reply)
117 {
118 bool isExist = false;
119 bool isRun = false;
120 std::string pkg;
121 int32_t ret = Prepare(isExist, isRun, pkg);
122 bool allOK = reply.WriteInt32(ret) && reply.WriteBool(isExist) && reply.WriteBool(isRun) && reply.WriteString(pkg);
123 return allOK ? NETMANAGER_EXT_SUCCESS : NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
124 }
125
ReplySetUpVpn(MessageParcel & data,MessageParcel & reply)126 int32_t NetworkVpnServiceStub::ReplySetUpVpn(MessageParcel &data, MessageParcel &reply)
127 {
128 sptr<VpnConfig> config = VpnConfig::Unmarshalling(data);
129 if (config == nullptr) {
130 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
131 }
132
133 int32_t result = SetUpVpn(config);
134 if (!reply.WriteInt32(result)) {
135 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
136 }
137 return NETMANAGER_EXT_SUCCESS;
138 }
139
ReplyProtect(MessageParcel & data,MessageParcel & reply)140 int32_t NetworkVpnServiceStub::ReplyProtect(MessageParcel &data, MessageParcel &reply)
141 {
142 int32_t result = Protect();
143 if (!reply.WriteInt32(result)) {
144 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
145 }
146 return NETMANAGER_EXT_SUCCESS;
147 }
148
ReplyDestroyVpn(MessageParcel & data,MessageParcel & reply)149 int32_t NetworkVpnServiceStub::ReplyDestroyVpn(MessageParcel &data, MessageParcel &reply)
150 {
151 int32_t result = DestroyVpn();
152 if (!reply.WriteInt32(result)) {
153 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
154 }
155 return NETMANAGER_EXT_SUCCESS;
156 }
157
158 #ifdef SUPPORT_SYSVPN
ReplyAddSysVpnConfig(MessageParcel & data,MessageParcel & reply)159 int32_t NetworkVpnServiceStub::ReplyAddSysVpnConfig(MessageParcel &data, MessageParcel &reply)
160 {
161 NETMGR_EXT_LOG_D("ReplyAddSysVpnConfig start");
162 sptr<SysVpnConfig> config = SysVpnConfig::Unmarshalling(data);
163 if (config == nullptr) {
164 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
165 }
166 int32_t result = AddSysVpnConfig(config);
167 if (!reply.WriteInt32(result)) {
168 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
169 }
170 return NETMANAGER_EXT_SUCCESS;
171 }
172
ReplyDeleteSysVpnConfig(MessageParcel & data,MessageParcel & reply)173 int32_t NetworkVpnServiceStub::ReplyDeleteSysVpnConfig(MessageParcel &data, MessageParcel &reply)
174 {
175 NETMGR_EXT_LOG_D("ReplyDeleteSysVpnConfig start");
176 std::string vpnId;
177 if (!data.ReadString(vpnId)) {
178 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
179 }
180 int32_t result = DeleteSysVpnConfig(vpnId);
181 if (!reply.WriteInt32(result)) {
182 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
183 }
184 return NETMANAGER_EXT_SUCCESS;
185 }
186
ReplyGetSysVpnConfigList(MessageParcel & data,MessageParcel & reply)187 int32_t NetworkVpnServiceStub::ReplyGetSysVpnConfigList(MessageParcel &data, MessageParcel &reply)
188 {
189 NETMGR_EXT_LOG_D("ReplyGetSysVpnConfigList start");
190 std::vector<SysVpnConfig> vpnList;
191 int32_t result = GetSysVpnConfigList(vpnList);
192 if (result != NETMANAGER_EXT_SUCCESS) {
193 return result;
194 }
195 int32_t vpnListSize = static_cast<int32_t>(vpnList.size());
196 if (!reply.WriteInt32(vpnListSize)) {
197 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
198 }
199 for (auto &config : vpnList) {
200 if (!(reply.WriteString(config.vpnId_) &&
201 reply.WriteString(config.vpnName_) &&
202 reply.WriteInt32(config.vpnType_))) {
203 return NETMANAGER_EXT_ERR_WRITE_DATA_FAIL;
204 }
205 }
206 return NETMANAGER_EXT_SUCCESS;
207 }
208
ReplyGetSysVpnConfig(MessageParcel & data,MessageParcel & reply)209 int32_t NetworkVpnServiceStub::ReplyGetSysVpnConfig(MessageParcel &data, MessageParcel &reply)
210 {
211 NETMGR_EXT_LOG_D("ReplyGetSysVpnConfig start");
212 std::string vpnId;
213 if (!data.ReadString(vpnId)) {
214 return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
215 }
216 sptr<SysVpnConfig> config = nullptr;
217 int32_t result = GetSysVpnConfig(config, vpnId);
218 if (result != NETMANAGER_EXT_SUCCESS) {
219 return result;
220 }
221 if (config != nullptr && !config->Marshalling(reply)) {
222 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
223 }
224 return NETMANAGER_EXT_SUCCESS;
225 }
226
ReplyGetConnectedSysVpnConfig(MessageParcel & data,MessageParcel & reply)227 int32_t NetworkVpnServiceStub::ReplyGetConnectedSysVpnConfig(MessageParcel &data, MessageParcel &reply)
228 {
229 NETMGR_EXT_LOG_D("ReplyGetConnectedSysVpnConfig start");
230 sptr<SysVpnConfig> config = nullptr;
231 int32_t result = GetConnectedSysVpnConfig(config);
232 if (result != NETMANAGER_EXT_SUCCESS) {
233 return result;
234 }
235 if (config != nullptr && !config->Marshalling(reply)) {
236 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
237 }
238 return NETMANAGER_EXT_SUCCESS;
239 }
240 #endif // SUPPORT_SYSVPN
241
ReplyRegisterVpnEvent(MessageParcel & data,MessageParcel & reply)242 int32_t NetworkVpnServiceStub::ReplyRegisterVpnEvent(MessageParcel &data, MessageParcel &reply)
243 {
244 sptr<IVpnEventCallback> callback = iface_cast<IVpnEventCallback>(data.ReadRemoteObject());
245 if (callback == nullptr) {
246 NETMGR_EXT_LOG_E("ReplyRegisterVpnEvent callback is null.");
247 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
248 }
249
250 int32_t result = RegisterVpnEvent(callback);
251 if (!reply.WriteInt32(result)) {
252 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
253 }
254 return NETMANAGER_EXT_SUCCESS;
255 }
256
ReplyUnregisterVpnEvent(MessageParcel & data,MessageParcel & reply)257 int32_t NetworkVpnServiceStub::ReplyUnregisterVpnEvent(MessageParcel &data, MessageParcel &reply)
258 {
259 sptr<IVpnEventCallback> callback = iface_cast<IVpnEventCallback>(data.ReadRemoteObject());
260 if (callback == nullptr) {
261 NETMGR_EXT_LOG_E("ReplyUnregisterVpnEvent callback is null.");
262 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
263 }
264
265 int32_t result = UnregisterVpnEvent(callback);
266 if (!reply.WriteInt32(result)) {
267 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
268 }
269 return NETMANAGER_EXT_SUCCESS;
270 }
271
ReplyCreateVpnConnection(MessageParcel & data,MessageParcel & reply)272 int32_t NetworkVpnServiceStub::ReplyCreateVpnConnection(MessageParcel &data, MessageParcel &reply)
273 {
274 int32_t result = CreateVpnConnection();
275 if (!reply.WriteInt32(result)) {
276 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
277 }
278 return NETMANAGER_EXT_SUCCESS;
279 }
280
ReplyFactoryResetVpn(MessageParcel & data,MessageParcel & reply)281 int32_t NetworkVpnServiceStub::ReplyFactoryResetVpn(MessageParcel &data, MessageParcel &reply)
282 {
283 return NETMANAGER_EXT_SUCCESS;
284 }
285
ReplyRegisterBundleName(MessageParcel & data,MessageParcel & reply)286 int32_t NetworkVpnServiceStub::ReplyRegisterBundleName(MessageParcel &data, MessageParcel &reply)
287 {
288 return NETMANAGER_EXT_SUCCESS;
289 }
290
ReplyGetSelfAppName(MessageParcel & data,MessageParcel & reply)291 int32_t NetworkVpnServiceStub::ReplyGetSelfAppName(MessageParcel &data, MessageParcel &reply)
292 {
293 std::string selfAppName;
294 int32_t result = GetSelfAppName(selfAppName);
295 if (result != ERR_NONE) {
296 NETMGR_EXT_LOG_E("GetSelfAppName failed on service");
297 return result;
298 }
299 if (!reply.WriteString(selfAppName)) {
300 return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
301 }
302 return NETMANAGER_EXT_SUCCESS;
303 }
304 } // namespace NetManagerStandard
305 } // namespace OHOS
306