1 /*
2  * Copyright (C) 2022 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 #ifdef FEATURE_NETWORK_SUPPORT
17 #include "network_ability_skeleton.h"
18 
19 #include <vector>
20 
21 #include "ipc_object_stub.h"
22 #include "ipc_skeleton.h"
23 
24 #include "common_utils.h"
25 #include "location.h"
26 #include "location_log.h"
27 #include "subability_common.h"
28 #include "work_record.h"
29 #include "locationhub_ipc_interface_code.h"
30 #include "location_data_rdb_manager.h"
31 #include "permission_manager.h"
32 
33 namespace OHOS {
34 namespace Location {
InitNetworkMsgHandleMap()35 void NetworkAbilityStub::InitNetworkMsgHandleMap()
36 {
37     if (NetworkMsgHandleMap_.size() != 0) {
38         return;
39     }
40     NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST)] =
41         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
42         return SendLocationRequestInner(data, reply, identity);
43         };
44     NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS)] =
45         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
46         return SetMockLocationsInner(data, reply, identity);
47         };
48     NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SET_ENABLE)] =
49         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
50         return SetEnableInner(data, reply, identity);
51         };
52     NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::ENABLE_LOCATION_MOCK)] =
53         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
54         return EnableMockInner(data, reply, identity);
55         };
56     NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::DISABLE_LOCATION_MOCK)] =
57         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
58         return DisableMockInner(data, reply, identity);
59         };
60 }
61 
NetworkAbilityStub()62 NetworkAbilityStub::NetworkAbilityStub()
63 {
64     InitNetworkMsgHandleMap();
65 }
66 
SendLocationRequestInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)67 int NetworkAbilityStub::SendLocationRequestInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
68 {
69     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
70         return ERRCODE_PERMISSION_DENIED;
71     }
72     SendMessage(static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST), data, reply);
73     isMessageRequest_ = true;
74     return ERRCODE_SUCCESS;
75 }
76 
SetMockLocationsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)77 int NetworkAbilityStub::SetMockLocationsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
78 {
79     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
80         return ERRCODE_PERMISSION_DENIED;
81     }
82     SendMessage(static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS), data, reply);
83     isMessageRequest_ = true;
84     return ERRCODE_SUCCESS;
85 }
86 
SetEnableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)87 int NetworkAbilityStub::SetEnableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
88 {
89     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
90         return ERRCODE_PERMISSION_DENIED;
91     }
92     reply.WriteInt32(SetEnable(data.ReadBool()));
93     return ERRCODE_SUCCESS;
94 }
95 
EnableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)96 int NetworkAbilityStub::EnableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
97 {
98     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
99         return ERRCODE_PERMISSION_DENIED;
100     }
101     reply.WriteInt32(EnableMock());
102     return ERRCODE_SUCCESS;
103 }
104 
DisableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)105 int NetworkAbilityStub::DisableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
106 {
107     if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
108         return ERRCODE_PERMISSION_DENIED;
109     }
110     reply.WriteInt32(DisableMock());
111     return ERRCODE_SUCCESS;
112 }
113 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int NetworkAbilityStub::OnRemoteRequest(uint32_t code,
115     MessageParcel &data, MessageParcel &reply, MessageOption &option)
116 {
117     pid_t callingPid = IPCSkeleton::GetCallingPid();
118     pid_t callingUid = IPCSkeleton::GetCallingUid();
119     AppIdentity identity;
120     identity.SetPid(callingPid);
121     identity.SetUid(callingUid);
122     LBSLOGI(NETWORK, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
123         code, option.GetFlags(), callingPid, callingUid);
124 
125     if (data.ReadInterfaceToken() != GetDescriptor()) {
126         LBSLOGE(NETWORK, "invalid token.");
127         return ERRCODE_SERVICE_UNAVAILABLE;
128     }
129     CancelIdleState();
130     int ret = ERRCODE_SUCCESS;
131     isMessageRequest_ = false;
132     auto handleFunc = NetworkMsgHandleMap_.find(code);
133     if (handleFunc != NetworkMsgHandleMap_.end() && handleFunc->second != nullptr) {
134         auto memberFunc = handleFunc->second;
135         ret = memberFunc(data, reply, identity);
136     } else {
137         LBSLOGE(NETWORK, "OnReceived cmd = %{public}u, unsupport service.", code);
138         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
139     }
140     if (!isMessageRequest_) {
141         UnloadNetworkSystemAbility();
142     }
143     return ret;
144 }
145 
CheckLocationSwitchState(MessageParcel & reply)146 bool NetworkAbilityStub::CheckLocationSwitchState(MessageParcel &reply)
147 {
148     if (LocationDataRdbManager::QuerySwitchState() != ENABLED) {
149         LBSLOGE(NETWORK, "%{public}s: %{public}d switch state is off.", __func__, __LINE__);
150         reply.WriteInt32(ERRCODE_SWITCH_OFF);
151         return false;
152     }
153     return true;
154 }
155 } // namespace Location
156 } // namespace OHOS
157 #endif // FEATURE_NETWORK_SUPPORT
158