• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "networkvpn_service_stub.h"
17 #include "net_manager_constants.h"
18 #include "netmanager_base_permission.h"
19 #include "netmgr_ext_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 
NetworkVpnServiceStub()24 NetworkVpnServiceStub::NetworkVpnServiceStub()
25 {
26     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_PREPARE] = {
27         Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyPrepare};
28     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_START_VPN] = {
29         Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplySetUpVpn};
30     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_PROTECT] = {
31         Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyProtect};
32     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_STOP_VPN] = {
33         Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyDestroyVpn};
34     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_REGISTER_EVENT_CALLBACK] = {
35         Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyRegisterVpnEvent};
36     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_UNREGISTER_EVENT_CALLBACK] = {
37         Permission::MANAGE_VPN, &NetworkVpnServiceStub::ReplyUnregisterVpnEvent};
38     permissionAndFuncMap_[INetworkVpnService::MessageCode::CMD_CREATE_VPN_CONNECTION] = {
39         "", &NetworkVpnServiceStub::ReplyCreateVpnConnection};
40 }
41 
CheckVpnPermission(std::string & strPermission)42 int32_t NetworkVpnServiceStub::CheckVpnPermission(std::string &strPermission)
43 {
44     if (!NetManagerPermission::IsSystemCaller()) {
45         NETMGR_EXT_LOG_E("is not system call");
46         return NETMANAGER_ERR_NOT_SYSTEM_CALL;
47     }
48 
49     if (!strPermission.empty() && !NetManagerPermission::CheckPermission(strPermission)) {
50         NETMGR_EXT_LOG_E("Permission denied permission: %{public}s", strPermission.c_str());
51         return NETMANAGER_ERR_PERMISSION_DENIED;
52     }
53     return NETMANAGER_SUCCESS;
54 }
55 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)56 int32_t NetworkVpnServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
57                                                MessageOption &option)
58 {
59     if (NetworkVpnServiceStub::GetDescriptor() != data.ReadInterfaceToken()) {
60         NETMGR_EXT_LOG_E("descriptor checked failed");
61         return NETMANAGER_EXT_ERR_DESCRIPTOR_MISMATCH;
62     }
63 
64     auto itr = permissionAndFuncMap_.find(static_cast<INetworkVpnService::MessageCode>(code));
65     if (itr != permissionAndFuncMap_.end()) {
66         int32_t checkResult = CheckVpnPermission(itr->second.strPermission);
67         if (checkResult != NETMANAGER_SUCCESS) {
68             return checkResult;
69         }
70         auto serviceFunc = itr->second.serviceFunc;
71         if (serviceFunc != nullptr) {
72             return (this->*serviceFunc)(data, reply);
73         }
74     }
75 
76     NETMGR_EXT_LOG_I("stub default case, need check");
77     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 }
79 
ReplyPrepare(MessageParcel & data,MessageParcel & reply)80 int32_t NetworkVpnServiceStub::ReplyPrepare(MessageParcel &data, MessageParcel &reply)
81 {
82     bool isExist = false;
83     bool isRun = false;
84     std::string pkg;
85     int32_t ret = Prepare(isExist, isRun, pkg);
86     bool allOK = reply.WriteInt32(ret) && reply.WriteBool(isExist) && reply.WriteBool(isRun) && reply.WriteString(pkg);
87     return allOK ? NETMANAGER_EXT_SUCCESS : NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
88 }
89 
ReplySetUpVpn(MessageParcel & data,MessageParcel & reply)90 int32_t NetworkVpnServiceStub::ReplySetUpVpn(MessageParcel &data, MessageParcel &reply)
91 {
92     sptr<VpnConfig> config = VpnConfig::Unmarshalling(data);
93     if (config == nullptr) {
94         return NETMANAGER_EXT_ERR_READ_DATA_FAIL;
95     }
96 
97     int32_t result = SetUpVpn(config);
98     if (!reply.WriteInt32(result)) {
99         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
100     }
101     return NETMANAGER_EXT_SUCCESS;
102 }
103 
ReplyProtect(MessageParcel & data,MessageParcel & reply)104 int32_t NetworkVpnServiceStub::ReplyProtect(MessageParcel &data, MessageParcel &reply)
105 {
106     int32_t result = Protect();
107     if (!reply.WriteInt32(result)) {
108         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
109     }
110     return NETMANAGER_EXT_SUCCESS;
111 }
112 
ReplyDestroyVpn(MessageParcel & data,MessageParcel & reply)113 int32_t NetworkVpnServiceStub::ReplyDestroyVpn(MessageParcel &data, MessageParcel &reply)
114 {
115     int32_t result = DestroyVpn();
116     if (!reply.WriteInt32(result)) {
117         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
118     }
119     return NETMANAGER_EXT_SUCCESS;
120 }
121 
ReplyRegisterVpnEvent(MessageParcel & data,MessageParcel & reply)122 int32_t NetworkVpnServiceStub::ReplyRegisterVpnEvent(MessageParcel &data, MessageParcel &reply)
123 {
124     sptr<IVpnEventCallback> callback = iface_cast<IVpnEventCallback>(data.ReadRemoteObject());
125     if (callback == nullptr) {
126         NETMGR_EXT_LOG_E("ReplyRegisterVpnEvent callback is null.");
127         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
128     }
129 
130     int32_t result = RegisterVpnEvent(callback);
131     if (!reply.WriteInt32(result)) {
132         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
133     }
134     return NETMANAGER_EXT_SUCCESS;
135 }
136 
ReplyUnregisterVpnEvent(MessageParcel & data,MessageParcel & reply)137 int32_t NetworkVpnServiceStub::ReplyUnregisterVpnEvent(MessageParcel &data, MessageParcel &reply)
138 {
139     sptr<IVpnEventCallback> callback = iface_cast<IVpnEventCallback>(data.ReadRemoteObject());
140     if (callback == nullptr) {
141         NETMGR_EXT_LOG_E("ReplyUnregisterVpnEvent callback is null.");
142         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
143     }
144 
145     int32_t result = UnregisterVpnEvent(callback);
146     if (!reply.WriteInt32(result)) {
147         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
148     }
149     return NETMANAGER_EXT_SUCCESS;
150 }
151 
ReplyCreateVpnConnection(MessageParcel & data,MessageParcel & reply)152 int32_t NetworkVpnServiceStub::ReplyCreateVpnConnection(MessageParcel &data, MessageParcel &reply)
153 {
154     int32_t result = CreateVpnConnection();
155     if (!reply.WriteInt32(result)) {
156         return NETMANAGER_EXT_ERR_WRITE_REPLY_FAIL;
157     }
158     return NETMANAGER_EXT_SUCCESS;
159 }
160 
161 } // namespace NetManagerStandard
162 } // namespace OHOS
163