1 /* 2 * Copyright (C) 2021 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 #ifndef HDC_DAEMON_H 16 #define HDC_DAEMON_H 17 #include "daemon_common.h" 18 19 namespace Hdc { 20 enum UserPermit { 21 REFUSE = 0, 22 ALLOWONCE = 1, 23 ALLOWFORVER = 2, 24 }; 25 struct HdcDaemonAuthInfo { 26 HdcSessionBase::AuthType authtype; 27 string token; 28 string pubkey; 29 string authmsg; 30 }; 31 class HdcDaemon : public HdcSessionBase { 32 public: 33 #ifdef USE_CONFIG_UV_THREADS 34 HdcDaemon(bool serverOrDaemonIn, size_t uvThreadSize = SIZE_THREAD_POOL); 35 #else 36 HdcDaemon(bool serverOrDaemonIn); 37 #endif 38 virtual ~HdcDaemon(); 39 #ifdef HDC_SUPPORT_UART 40 #ifdef HDC_EMULATOR 41 void InitMod(bool bEnableTCP, bool bEnableUSB, bool bEnableBridge, bool bEnableUART); 42 #endif 43 void InitMod(bool bEnableTCP, bool bEnableUSB, bool bEnableUART); 44 #else 45 #ifdef HDC_EMULATOR 46 void InitMod(bool bEnableTCP, bool bEnableUSB, bool bEnableBridge); 47 #endif 48 void InitMod(bool bEnableTCP, bool bEnableUSB); 49 #endif 50 bool FetchCommand(HSession hSession, const uint32_t channelId, const uint16_t command, uint8_t *payload, 51 const int payloadSize) override; 52 bool ServerCommand(const uint32_t sessionId, const uint32_t channelId, const uint16_t command, uint8_t *bufPtr, 53 const int size) override; 54 void ClearKnownHosts(); 55 void *clsTCPServ; 56 void *clsUSBServ; 57 #ifdef HDC_EMULATOR 58 void *clsBridgeServ; 59 #endif 60 #ifdef HDC_SUPPORT_UART 61 void *clsUARTServ; 62 #endif 63 void *clsJdwp; 64 65 private: 66 bool RemoveInstanceTask(const uint8_t op, HTaskInfo hTask) override; 67 bool RedirectToTask(HTaskInfo hTaskInfo, HSession hSession, const uint32_t channelId, const uint16_t command, 68 uint8_t *payload, const int payloadSize) override; 69 void JdwpNewFileDescriptor(const uint8_t *buf, const int bytesIO) override; 70 static bool CheckControl(const uint16_t command); 71 static bool IsExpectedParam(const std::string& param, const std::string& expect); 72 bool HandDaemonAuth(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 73 bool GetHostPubkeyInfo(const string& buf, string& hostname, string& pubkey); 74 bool AlreadyInKnownHosts(const string& key); 75 void UpdateKnownHosts(const string& key); 76 void ClearInstanceResource() override; 77 void DaemonSessionHandshakeInit(HSession &hSession, SessionHandShake &handshake); 78 bool DaemonSessionHandshake(HSession hSession, const uint32_t channelId, uint8_t *payload, int payloadSize); 79 void TryStopInstance(); 80 UserPermit PostUIConfirm(string hostname); 81 bool ShowPermitDialog(); 82 bool HandDaemonAuthInit(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 83 bool HandDaemonAuthPubkey(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 84 bool HandDaemonAuthSignature(HSession hSession, const uint32_t channelId, SessionHandShake &handshake); 85 // deprecated, remove later 86 #ifdef HDC_SUPPORT_FLASHD 87 // null 88 #else 89 void NotifyInstanceSessionFree(HSession hSession, bool freeOrClear) override; 90 #endif 91 92 bool HandDaemonAuthBypass(void); 93 void SendAuthSignMsg(SessionHandShake &handshake, 94 uint32_t channelId, uint32_t sessionid, string pubkey, string token); 95 void SendAuthOkMsg(SessionHandShake &handshake, uint32_t channelid, 96 uint32_t sessionid, string msg = "", string daemonAuthResult = DAEOMN_AUTH_SUCCESS); 97 void AuthRejectLowClient(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid); 98 void EchoHandshakeMsg(SessionHandShake &handshake, uint32_t channelid, uint32_t sessionid, string msg); 99 bool AuthVerify(HSession hSession, string encryptToken); 100 void InitSessionAuthInfo(uint32_t sessionid, string token); 101 void UpdateSessionAuthOk(uint32_t sessionid); 102 void UpdateSessionAuthmsg(uint32_t sessionid, string authmsg); 103 void UpdateSessionAuthPubkey(uint32_t sessionid, string pubkey); 104 void DeleteSessionAuthStatus(uint32_t sessionid); 105 AuthType GetSessionAuthStatus(uint32_t sessionid); 106 string GetSessionAuthmsg(uint32_t sessionid); 107 string GetSessionAuthToken(uint32_t sessionid); 108 string GetSessionAuthPubkey(uint32_t sessionid); 109 std::map<uint32_t, HdcDaemonAuthInfo> mapAuthStatus; 110 std::mutex mapAuthStatusMutex; 111 bool enableSecure; 112 }; 113 } // namespace Hdc 114 #endif 115