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 #include "remote_command_manager.h"
17 #include <thread>
18 #ifdef EVENTHANDLER_ENABLE
19 #include "access_event_handler.h"
20 #endif
21 #include "device_info_manager.h"
22 #include "remote_command_factory.h"
23 #include "token_sync_manager_service.h"
24 #include "accesstoken_kit.h"
25 #include "constant_common.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 namespace {
31 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "RemoteCommandManager"};
32 std::recursive_mutex g_instanceMutex;
33 }
RemoteCommandManager()34 RemoteCommandManager::RemoteCommandManager() : executors_(), mutex_()
35 {
36     ACCESSTOKEN_LOG_DEBUG(LABEL, "RemoteCommandManager()");
37 }
38 
~RemoteCommandManager()39 RemoteCommandManager::~RemoteCommandManager()
40 {
41     ACCESSTOKEN_LOG_DEBUG(LABEL, "~RemoteCommandManager()");
42 }
43 
GetInstance()44 RemoteCommandManager &RemoteCommandManager::GetInstance()
45 {
46     static RemoteCommandManager* instance = nullptr;
47     if (instance == nullptr) {
48         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
49         if (instance == nullptr) {
50             instance = new RemoteCommandManager();
51         }
52     }
53     return *instance;
54 }
55 
Init()56 void RemoteCommandManager::Init()
57 {
58     ACCESSTOKEN_LOG_DEBUG(LABEL, "Init()");
59 }
60 
AddCommand(const std::string & udid,const std::shared_ptr<BaseRemoteCommand> & command)61 int RemoteCommandManager::AddCommand(const std::string &udid, const std::shared_ptr<BaseRemoteCommand>& command)
62 {
63     if (udid.empty() || command == nullptr) {
64         ACCESSTOKEN_LOG_WARN(LABEL, "Invalid udid, or null command");
65         return Constant::FAILURE;
66     }
67     ACCESSTOKEN_LOG_INFO(LABEL, "Add uniqueId");
68 
69     std::shared_ptr<RemoteCommandExecutor> executor = GetOrCreateRemoteCommandExecutor(udid);
70     if (executor == nullptr) {
71         ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot get or create remote command executor");
72         return Constant::FAILURE;
73     }
74 
75     int result = executor->AddCommand(command);
76     ACCESSTOKEN_LOG_INFO(LABEL, "Add command result: %{public}d ", result);
77     return result;
78 }
79 
RemoveCommand(const std::string & udid)80 void RemoteCommandManager::RemoveCommand(const std::string &udid)
81 {
82     ACCESSTOKEN_LOG_INFO(LABEL, "Remove command");
83     executors_.erase(udid);
84 }
85 
ExecuteCommand(const std::string & udid,const std::shared_ptr<BaseRemoteCommand> & command)86 int RemoteCommandManager::ExecuteCommand(const std::string &udid, const std::shared_ptr<BaseRemoteCommand>& command)
87 {
88     if (udid.empty() || command == nullptr) {
89         ACCESSTOKEN_LOG_WARN(LABEL, "Invalid udid: %{public}s, or null command",
90             ConstantCommon::EncryptDevId(udid).c_str());
91         return Constant::FAILURE;
92     }
93     std::string uniqueId = command->remoteProtocol_.uniqueId;
94     ACCESSTOKEN_LOG_INFO(LABEL, "Start with udid: %{public}s , uniqueId: %{public}s ",
95         ConstantCommon::EncryptDevId(udid).c_str(), ConstantCommon::EncryptDevId(uniqueId).c_str());
96 
97     std::shared_ptr<RemoteCommandExecutor> executor = GetOrCreateRemoteCommandExecutor(udid);
98     if (executor == nullptr) {
99         ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot get or create remote command executor");
100         return Constant::FAILURE;
101     }
102 
103     int result = executor->ProcessOneCommand(command);
104     ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor processOneCommand result:%{public}d ", result);
105     return result;
106 }
107 
ProcessDeviceCommandImmediately(const std::string & udid)108 int RemoteCommandManager::ProcessDeviceCommandImmediately(const std::string &udid)
109 {
110     if (udid.empty()) {
111         ACCESSTOKEN_LOG_WARN(LABEL, "Invalid udid: %{public}s", ConstantCommon::EncryptDevId(udid).c_str());
112         return Constant::FAILURE;
113     }
114     ACCESSTOKEN_LOG_INFO(LABEL, "Start with udid:%{public}s ", ConstantCommon::EncryptDevId(udid).c_str());
115 
116     std::unique_lock<std::mutex> lock(mutex_);
117     auto executorIt = executors_.find(udid);
118     if (executorIt == executors_.end()) {
119         ACCESSTOKEN_LOG_ERROR(LABEL, "No executor found, udid:%{public}s", ConstantCommon::EncryptDevId(udid).c_str());
120         return Constant::FAILURE;
121     }
122 
123     auto executor = executorIt->second;
124     if (executor == nullptr) {
125         ACCESSTOKEN_LOG_INFO(LABEL, "RemoteCommandExecutor is null for udid %{public}s ",
126             ConstantCommon::EncryptDevId(udid).c_str());
127         return Constant::FAILURE;
128     }
129 
130     int result = executor->ProcessBufferedCommands();
131     ACCESSTOKEN_LOG_INFO(LABEL, "ProcessBufferedCommands result: %{public}d", result);
132     return result;
133 }
134 
Loop()135 int RemoteCommandManager::Loop()
136 {
137     ACCESSTOKEN_LOG_INFO(LABEL, "Start");
138     std::unique_lock<std::mutex> lock(mutex_);
139     for (auto it = executors_.begin(); it != executors_.end(); it++) {
140         ACCESSTOKEN_LOG_INFO(LABEL, "Udid:%{public}s", ConstantCommon::EncryptDevId(it->first).c_str());
141         (*it).second->ProcessBufferedCommandsWithThread();
142     }
143     return Constant::SUCCESS;
144 }
145 
146 /**
147  * caller: service connection listener
148  */
Clear()149 void RemoteCommandManager::Clear()
150 {
151     ACCESSTOKEN_LOG_INFO(LABEL, "Remove all remote command executors.");
152 
153     std::map<std::string, std::shared_ptr<RemoteCommandExecutor>> dummy;
154     std::unique_lock<std::mutex> lock(mutex_);
155     executors_.swap(dummy);
156     executors_.clear();
157 }
158 
159 /**
160  * caller: device listener
161  */
NotifyDeviceOnline(const std::string & nodeId)162 int RemoteCommandManager::NotifyDeviceOnline(const std::string &nodeId)
163 {
164     if (!DataValidator::IsDeviceIdValid(nodeId)) {
165         ACCESSTOKEN_LOG_INFO(LABEL, "Invalid nodeId: %{public}s", ConstantCommon::EncryptDevId(nodeId).c_str());
166         return Constant::FAILURE;
167     }
168     ACCESSTOKEN_LOG_INFO(LABEL, "Operation start with nodeId:  %{public}s",
169         ConstantCommon::EncryptDevId(nodeId).c_str());
170 
171     auto executor = GetOrCreateRemoteCommandExecutor(nodeId);
172     std::unique_lock<std::mutex> lock(mutex_);
173     if (executor == nullptr) {
174         ACCESSTOKEN_LOG_ERROR(LABEL, "Cannot get or create remote command executor");
175         return Constant::FAILURE;
176     }
177 
178     if (executor->GetChannel() == nullptr) {
179         auto channel = RemoteCommandExecutor::CreateChannel(nodeId);
180         if (channel == nullptr) {
181             ACCESSTOKEN_LOG_ERROR(LABEL, "Create channel failed.");
182             return Constant::FAILURE;
183         }
184         executor->SetChannel(channel);
185     }
186 
187     lock.unlock();
188 
189     return Constant::SUCCESS;
190 }
191 
192 /**
193  * caller: device listener
194  */
NotifyDeviceOffline(const std::string & nodeId)195 int RemoteCommandManager::NotifyDeviceOffline(const std::string &nodeId)
196 {
197     if (!DataValidator::IsDeviceIdValid(nodeId)) {
198         ACCESSTOKEN_LOG_INFO(LABEL, "Invalid nodeId: %{public}s", ConstantCommon::EncryptDevId(nodeId).c_str());
199         return Constant::FAILURE;
200     }
201     ACCESSTOKEN_LOG_INFO(LABEL, "Operation start with nodeId:  %{public}s",
202         ConstantCommon::EncryptDevId(nodeId).c_str());
203 
204     auto channel = GetExecutorChannel(nodeId);
205     if (channel != nullptr) {
206         channel->Release();
207     }
208 
209     std::unique_lock<std::mutex> lock(mutex_);
210     RemoveCommand(nodeId);
211     lock.unlock();
212 
213     DeviceInfo devInfo;
214     bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(nodeId, DeviceIdType::UNKNOWN, devInfo);
215     if (!result) {
216         ACCESSTOKEN_LOG_INFO(LABEL, "Get remote networkId failed");
217         return Constant::FAILURE;
218     }
219     std::string uniqueDeviceId = devInfo.deviceId.uniqueDeviceId;
220     std::function<void()> delayed = ([uniqueDeviceId]() {
221         AccessTokenKit::DeleteRemoteDeviceTokens(uniqueDeviceId);
222     });
223 
224 #ifdef EVENTHANDLER_ENABLE
225     std::shared_ptr<AccessEventHandler> handler =
226         DelayedSingleton<TokenSyncManagerService>::GetInstance()->GetSendEventHandler();
227     if (handler == nullptr) {
228         ACCESSTOKEN_LOG_ERROR(LABEL, "Fail to get EventHandler");
229         return Constant::FAILURE;
230     }
231     handler->ProxyPostTask(delayed, "HandleDeviceOffline");
232 #endif
233 
234     ACCESSTOKEN_LOG_INFO(LABEL, "Complete");
235     return Constant::SUCCESS;
236 }
237 
GetOrCreateRemoteCommandExecutor(const std::string & nodeId)238 std::shared_ptr<RemoteCommandExecutor> RemoteCommandManager::GetOrCreateRemoteCommandExecutor(const std::string &nodeId)
239 {
240     ACCESSTOKEN_LOG_DEBUG(LABEL, "Begin, nodeId %{public}s", ConstantCommon::EncryptDevId(nodeId).c_str());
241 
242     std::unique_lock<std::mutex> lock(mutex_);
243     auto executorIter = executors_.find(nodeId);
244     if (executorIter != executors_.end()) {
245         return executorIter->second;
246     }
247 
248     auto executor = std::make_shared<RemoteCommandExecutor>(nodeId);
249     executors_.insert(std::pair<std::string, std::shared_ptr<RemoteCommandExecutor>>(nodeId, executor));
250     ACCESSTOKEN_LOG_DEBUG(LABEL, "Executor added, nodeId: %{public}s", ConstantCommon::EncryptDevId(nodeId).c_str());
251     return executor;
252 }
253 
254 /**
255  * caller: session listener(OnBytes)
256  */
GetExecutorChannel(const std::string & nodeId)257 std::shared_ptr<RpcChannel> RemoteCommandManager::GetExecutorChannel(const std::string &nodeId)
258 {
259     ACCESSTOKEN_LOG_DEBUG(LABEL, "Convert udid start, nodeId:%{public}s", ConstantCommon::EncryptDevId(nodeId).c_str());
260     std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(nodeId);
261     if (!DataValidator::IsDeviceIdValid(udid)) {
262         ACCESSTOKEN_LOG_WARN(
263             LABEL, "Converted udid is invalid, nodeId:%{public}s", ConstantCommon::EncryptDevId(nodeId).c_str());
264         return nullptr;
265     }
266 
267     std::unique_lock<std::mutex> lock(mutex_);
268     std::map<std::string, std::shared_ptr<RemoteCommandExecutor>>::iterator iter = executors_.find(udid);
269     if (iter == executors_.end()) {
270         ACCESSTOKEN_LOG_INFO(LABEL, "Executor not found");
271         return nullptr;
272     }
273     std::shared_ptr<RemoteCommandExecutor> executor = iter->second;
274     if (executor == nullptr) {
275         ACCESSTOKEN_LOG_INFO(LABEL, "Executor is null");
276         return nullptr;
277     }
278     return executor->GetChannel();
279 }
280 }  // namespace AccessToken
281 }  // namespace Security
282 }  // namespace OHOS
283