sshconnection_p.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**************************************************************************
  2. **
  3. ** This file is part of Qt Creator
  4. **
  5. ** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
  6. **
  7. ** Contact: http://www.qt-project.org/
  8. **
  9. **
  10. ** GNU Lesser General Public License Usage
  11. **
  12. ** This file may be used under the terms of the GNU Lesser General Public
  13. ** License version 2.1 as published by the Free Software Foundation and
  14. ** appearing in the file LICENSE.LGPL included in the packaging of this file.
  15. ** Please review the following information to ensure the GNU Lesser General
  16. ** Public License version 2.1 requirements will be met:
  17. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  18. **
  19. ** In addition, as a special exception, Nokia gives you certain additional
  20. ** rights. These rights are described in the Nokia Qt LGPL Exception
  21. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  22. **
  23. ** Other Usage
  24. **
  25. ** Alternatively, this file may be used in accordance with the terms and
  26. ** conditions contained in a signed written agreement between you and Nokia.
  27. **
  28. **
  29. **************************************************************************/
  30. #ifndef SSHCONNECTION_P_H
  31. #define SSHCONNECTION_P_H
  32. #include "sshconnection.h"
  33. #include "sshexception_p.h"
  34. #include "sshincomingpacket_p.h"
  35. #include "sshsendfacility_p.h"
  36. #include <QHash>
  37. #include <QList>
  38. #include <QQueue>
  39. #include <QObject>
  40. #include <QPair>
  41. #include <QScopedPointer>
  42. #include <QTimer>
  43. QT_BEGIN_NAMESPACE
  44. class QTcpSocket;
  45. QT_END_NAMESPACE
  46. namespace QSsh {
  47. class SftpChannel;
  48. class SshRemoteProcess;
  49. class SshDirectTcpIpTunnel;
  50. class SshTcpIpForwardServer;
  51. namespace Internal {
  52. class SshChannelManager;
  53. // NOTE: When you add stuff here, don't forget to update m_packetHandlers.
  54. enum SshStateInternal {
  55. SocketUnconnected, // initial and after disconnect
  56. SocketConnecting, // After connectToHost()
  57. SocketConnected, // After socket's connected() signal
  58. UserAuthServiceRequested,
  59. WaitingForAgentKeys,
  60. UserAuthRequested,
  61. ConnectionEstablished // After service has been started
  62. // ...
  63. };
  64. enum SshKeyExchangeState {
  65. NoKeyExchange,
  66. KexInitSent,
  67. DhInitSent,
  68. NewKeysSent,
  69. KeyExchangeSuccess // After server's DH_REPLY message
  70. };
  71. class SshConnectionPrivate : public QObject
  72. {
  73. Q_OBJECT
  74. friend class QSsh::SshConnection;
  75. public:
  76. SshConnectionPrivate(SshConnection *conn,
  77. const SshConnectionParameters &serverInfo);
  78. ~SshConnectionPrivate();
  79. void connectToHost();
  80. void closeConnection(SshErrorCode sshError, SshError userError,
  81. const QByteArray &serverErrorString, const QString &userErrorString);
  82. QSharedPointer<SshRemoteProcess> createRemoteProcess(const QByteArray &command);
  83. QSharedPointer<SshRemoteProcess> createRemoteShell();
  84. QSharedPointer<SftpChannel> createSftpChannel();
  85. QSharedPointer<SshDirectTcpIpTunnel> createDirectTunnel(const QString &originatingHost,
  86. quint16 originatingPort, const QString &remoteHost, quint16 remotePort);
  87. QSharedPointer<SshTcpIpForwardServer> createForwardServer(const QString &remoteHost,
  88. quint16 remotePort);
  89. SshStateInternal state() const { return m_state; }
  90. SshError errorState() const { return m_error; }
  91. QString errorString() const { return m_errorString; }
  92. const QByteArray &hostKeyFingerprint() const { return m_hostFingerprint; }
  93. signals:
  94. void connected();
  95. void disconnected();
  96. void dataAvailable(const QString &message);
  97. void error(QSsh::SshError);
  98. private:
  99. void handleSocketConnected();
  100. void handleIncomingData();
  101. void handleSocketError();
  102. void handleSocketDisconnected();
  103. void handleTimeout();
  104. void sendKeepAlivePacket();
  105. void handleAgentKeysUpdated();
  106. void handleSignatureFromAgent(const QByteArray &key, const QByteArray &signature, uint token);
  107. void tryAllAgentKeys();
  108. void authenticateWithPublicKey();
  109. void setAgentError();
  110. void handleServerId();
  111. void handlePackets();
  112. void handleCurrentPacket();
  113. void handleKeyExchangeInitPacket();
  114. void handleKeyExchangeReplyPacket();
  115. void handleNewKeysPacket();
  116. void handleServiceAcceptPacket();
  117. void handlePasswordExpiredPacket();
  118. void handleUserAuthInfoRequestPacket();
  119. void handleUserAuthSuccessPacket();
  120. void handleUserAuthFailurePacket();
  121. void handleUserAuthKeyOkPacket();
  122. void handleUserAuthBannerPacket();
  123. void handleUnexpectedPacket();
  124. void handleGlobalRequest();
  125. void handleDebugPacket();
  126. void handleUnimplementedPacket();
  127. void handleChannelRequest();
  128. void handleChannelOpen();
  129. void handleChannelOpenFailure();
  130. void handleChannelOpenConfirmation();
  131. void handleChannelSuccess();
  132. void handleChannelFailure();
  133. void handleChannelWindowAdjust();
  134. void handleChannelData();
  135. void handleChannelExtendedData();
  136. void handleChannelEof();
  137. void handleChannelClose();
  138. void handleDisconnect();
  139. void handleRequestSuccess();
  140. void handleRequestFailure();
  141. bool canUseSocket() const;
  142. void createPrivateKey();
  143. void sendData(const QByteArray &data);
  144. uint tokenForAgent() const;
  145. typedef void (SshConnectionPrivate::*PacketHandler)();
  146. typedef QList<SshStateInternal> StateList;
  147. void setupPacketHandlers();
  148. void setupPacketHandler(SshPacketType type, const StateList &states,
  149. PacketHandler handler);
  150. typedef QPair<StateList, PacketHandler> HandlerInStates;
  151. QHash<SshPacketType, HandlerInStates> m_packetHandlers;
  152. static const quint64 InvalidSeqNr;
  153. QTcpSocket *m_socket;
  154. SshStateInternal m_state;
  155. SshKeyExchangeState m_keyExchangeState;
  156. SshIncomingPacket m_incomingPacket;
  157. SshSendFacility m_sendFacility;
  158. SshChannelManager * const m_channelManager;
  159. const SshConnectionParameters m_connParams;
  160. QByteArray m_incomingData;
  161. SshError m_error;
  162. QString m_errorString;
  163. QScopedPointer<SshKeyExchange> m_keyExchange;
  164. QByteArray m_hostFingerprint;
  165. QTimer m_timeoutTimer;
  166. QTimer m_keepAliveTimer;
  167. bool m_ignoreNextPacket;
  168. SshConnection *m_conn;
  169. quint64 m_lastInvalidMsgSeqNr;
  170. QByteArray m_serverId;
  171. QByteArray m_agentSignature;
  172. QQueue<QByteArray> m_pendingKeyChecks;
  173. QByteArray m_agentKeyToUse;
  174. bool m_serverHasSentDataBeforeId;
  175. bool m_triedAllPasswordBasedMethods;
  176. bool m_agentKeysUpToDate;
  177. };
  178. } // namespace Internal
  179. } // namespace QSsh
  180. #endif // SSHCONNECTION_P_H