sshsendfacility.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "sshsendfacility_p.h"
  31. #include "sshkeyexchange_p.h"
  32. #include "sshlogging_p.h"
  33. #include "sshoutgoingpacket_p.h"
  34. #include <QTcpSocket>
  35. namespace QSsh {
  36. namespace Internal {
  37. SshSendFacility::SshSendFacility(QTcpSocket *socket)
  38. : m_clientSeqNr(0), m_socket(socket),
  39. m_outgoingPacket(m_encrypter, m_clientSeqNr)
  40. {
  41. }
  42. void SshSendFacility::sendPacket()
  43. {
  44. qCDebug(sshLog, "Sending packet, client seq nr is %u", m_clientSeqNr);
  45. if (m_socket->isValid()
  46. && m_socket->state() == QAbstractSocket::ConnectedState) {
  47. m_socket->write(m_outgoingPacket.rawData());
  48. ++m_clientSeqNr;
  49. }
  50. }
  51. void SshSendFacility::reset()
  52. {
  53. m_clientSeqNr = 0;
  54. m_encrypter.clearKeys();
  55. }
  56. void SshSendFacility::recreateKeys(const SshKeyExchange &keyExchange)
  57. {
  58. m_encrypter.recreateKeys(keyExchange);
  59. }
  60. void SshSendFacility::createAuthenticationKey(const QByteArray &privKeyFileContents)
  61. {
  62. m_encrypter.createAuthenticationKey(privKeyFileContents);
  63. }
  64. QByteArray SshSendFacility::sendKeyExchangeInitPacket()
  65. {
  66. const QByteArray &payLoad = m_outgoingPacket.generateKeyExchangeInitPacket();
  67. sendPacket();
  68. return payLoad;
  69. }
  70. void SshSendFacility::sendKeyDhInitPacket(const Botan::BigInt &e)
  71. {
  72. m_outgoingPacket.generateKeyDhInitPacket(e);
  73. sendPacket();
  74. }
  75. void SshSendFacility::sendKeyEcdhInitPacket(const QByteArray &clientQ)
  76. {
  77. m_outgoingPacket.generateKeyEcdhInitPacket(clientQ);
  78. sendPacket();
  79. }
  80. void SshSendFacility::sendNewKeysPacket()
  81. {
  82. m_outgoingPacket.generateNewKeysPacket();
  83. sendPacket();
  84. }
  85. void SshSendFacility::sendDisconnectPacket(SshErrorCode reason,
  86. const QByteArray &reasonString)
  87. {
  88. m_outgoingPacket.generateDisconnectPacket(reason, reasonString);
  89. sendPacket();
  90. }
  91. void SshSendFacility::sendMsgUnimplementedPacket(quint32 serverSeqNr)
  92. {
  93. m_outgoingPacket.generateMsgUnimplementedPacket(serverSeqNr);
  94. sendPacket();
  95. }
  96. void SshSendFacility::sendUserAuthServiceRequestPacket()
  97. {
  98. m_outgoingPacket.generateUserAuthServiceRequestPacket();
  99. sendPacket();
  100. }
  101. void SshSendFacility::sendUserAuthByPasswordRequestPacket(const QByteArray &user,
  102. const QByteArray &service, const QByteArray &pwd)
  103. {
  104. m_outgoingPacket.generateUserAuthByPasswordRequestPacket(user, service, pwd);
  105. sendPacket();
  106. }
  107. void SshSendFacility::sendUserAuthByPublicKeyRequestPacket(const QByteArray &user,
  108. const QByteArray &service, const QByteArray &key, const QByteArray &signature)
  109. {
  110. m_outgoingPacket.generateUserAuthByPublicKeyRequestPacket(user, service, key, signature);
  111. sendPacket();
  112. }
  113. void SshSendFacility::sendQueryPublicKeyPacket(const QByteArray &user, const QByteArray &service,
  114. const QByteArray &publicKey)
  115. {
  116. m_outgoingPacket.generateQueryPublicKeyPacket(user, service, publicKey);
  117. sendPacket();
  118. }
  119. void SshSendFacility::sendUserAuthByKeyboardInteractiveRequestPacket(const QByteArray &user,
  120. const QByteArray &service)
  121. {
  122. m_outgoingPacket.generateUserAuthByKeyboardInteractiveRequestPacket(user, service);
  123. sendPacket();
  124. }
  125. void SshSendFacility::sendUserAuthInfoResponsePacket(const QStringList &responses)
  126. {
  127. m_outgoingPacket.generateUserAuthInfoResponsePacket(responses);
  128. sendPacket();
  129. }
  130. void SshSendFacility::sendRequestFailurePacket()
  131. {
  132. m_outgoingPacket.generateRequestFailurePacket();
  133. sendPacket();
  134. }
  135. void SshSendFacility::sendIgnorePacket()
  136. {
  137. m_outgoingPacket.generateIgnorePacket();
  138. sendPacket();
  139. }
  140. void SshSendFacility::sendInvalidPacket()
  141. {
  142. m_outgoingPacket.generateInvalidMessagePacket();
  143. sendPacket();
  144. }
  145. void SshSendFacility::sendSessionPacket(quint32 channelId, quint32 windowSize,
  146. quint32 maxPacketSize)
  147. {
  148. m_outgoingPacket.generateSessionPacket(channelId, windowSize,
  149. maxPacketSize);
  150. sendPacket();
  151. }
  152. void SshSendFacility::sendDirectTcpIpPacket(quint32 channelId, quint32 windowSize,
  153. quint32 maxPacketSize, const QByteArray &remoteHost, quint32 remotePort,
  154. const QByteArray &localIpAddress, quint32 localPort)
  155. {
  156. m_outgoingPacket.generateDirectTcpIpPacket(channelId, windowSize, maxPacketSize, remoteHost,
  157. remotePort, localIpAddress, localPort);
  158. sendPacket();
  159. }
  160. void SshSendFacility::sendTcpIpForwardPacket(const QByteArray &bindAddress, quint32 bindPort)
  161. {
  162. m_outgoingPacket.generateTcpIpForwardPacket(bindAddress, bindPort);
  163. sendPacket();
  164. }
  165. void SshSendFacility::sendCancelTcpIpForwardPacket(const QByteArray &bindAddress, quint32 bindPort)
  166. {
  167. m_outgoingPacket.generateCancelTcpIpForwardPacket(bindAddress, bindPort);
  168. sendPacket();
  169. }
  170. void SshSendFacility::sendPtyRequestPacket(quint32 remoteChannel,
  171. const SshPseudoTerminal &terminal)
  172. {
  173. m_outgoingPacket.generatePtyRequestPacket(remoteChannel, terminal);
  174. sendPacket();
  175. }
  176. void SshSendFacility::sendEnvPacket(quint32 remoteChannel,
  177. const QByteArray &var, const QByteArray &value)
  178. {
  179. m_outgoingPacket.generateEnvPacket(remoteChannel, var, value);
  180. sendPacket();
  181. }
  182. void SshSendFacility::sendX11ForwardingPacket(quint32 remoteChannel, const QByteArray &protocol,
  183. const QByteArray &cookie, quint32 screenNumber)
  184. {
  185. m_outgoingPacket.generateX11ForwardingPacket(remoteChannel, protocol, cookie, screenNumber);
  186. sendPacket();
  187. }
  188. void SshSendFacility::sendExecPacket(quint32 remoteChannel,
  189. const QByteArray &command)
  190. {
  191. m_outgoingPacket.generateExecPacket(remoteChannel, command);
  192. sendPacket();
  193. }
  194. void SshSendFacility::sendShellPacket(quint32 remoteChannel)
  195. {
  196. m_outgoingPacket.generateShellPacket(remoteChannel);
  197. sendPacket();
  198. }
  199. void SshSendFacility::sendSftpPacket(quint32 remoteChannel)
  200. {
  201. m_outgoingPacket.generateSftpPacket(remoteChannel);
  202. sendPacket();
  203. }
  204. void SshSendFacility::sendWindowAdjustPacket(quint32 remoteChannel,
  205. quint32 bytesToAdd)
  206. {
  207. m_outgoingPacket.generateWindowAdjustPacket(remoteChannel, bytesToAdd);
  208. sendPacket();
  209. }
  210. void SshSendFacility::sendChannelDataPacket(quint32 remoteChannel,
  211. const QByteArray &data)
  212. {
  213. m_outgoingPacket.generateChannelDataPacket(remoteChannel, data);
  214. sendPacket();
  215. }
  216. void SshSendFacility::sendChannelSignalPacket(quint32 remoteChannel,
  217. const QByteArray &signalName)
  218. {
  219. m_outgoingPacket.generateChannelSignalPacket(remoteChannel, signalName);
  220. sendPacket();
  221. }
  222. void SshSendFacility::sendChannelEofPacket(quint32 remoteChannel)
  223. {
  224. m_outgoingPacket.generateChannelEofPacket(remoteChannel);
  225. sendPacket();
  226. }
  227. void SshSendFacility::sendChannelClosePacket(quint32 remoteChannel)
  228. {
  229. m_outgoingPacket.generateChannelClosePacket(remoteChannel);
  230. sendPacket();
  231. }
  232. void SshSendFacility::sendChannelOpenConfirmationPacket(quint32 remoteChannel, quint32 localChannel,
  233. quint32 localWindowSize, quint32 maxPacketSize)
  234. {
  235. m_outgoingPacket.generateChannelOpenConfirmationPacket(remoteChannel, localChannel,
  236. localWindowSize, maxPacketSize);
  237. sendPacket();
  238. }
  239. void SshSendFacility::sendChannelOpenFailurePacket(quint32 remoteChannel, quint32 reason,
  240. const QByteArray &reasonString)
  241. {
  242. m_outgoingPacket.generateChannelOpenFailurePacket(remoteChannel, reason, reasonString);
  243. sendPacket();
  244. }
  245. } // namespace Internal
  246. } // namespace QSsh