sshconnection.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_H
  31. #define SSHCONNECTION_H
  32. #include "ssherrors.h"
  33. #include "sshhostkeydatabase.h"
  34. #include "ssh_global.h"
  35. #include <QByteArray>
  36. #include <QFlags>
  37. #include <QMetaType>
  38. #include <QObject>
  39. #include <QSharedPointer>
  40. #include <QString>
  41. #include <QHostAddress>
  42. #include <QUrl>
  43. namespace QSsh {
  44. class SftpChannel;
  45. class SshDirectTcpIpTunnel;
  46. class SshRemoteProcess;
  47. class SshTcpIpForwardServer;
  48. namespace Internal {
  49. class SshConnectionPrivate;
  50. } // namespace Internal
  51. /*!
  52. * \brief Flags that control various general behavior
  53. */
  54. enum SshConnectionOption {
  55. /// Set this to ignore the system defined proxy
  56. SshIgnoreDefaultProxy = 0x1,
  57. /// Fail instead of warn if the remote host violates the standard
  58. SshEnableStrictConformanceChecks = 0x2,
  59. /// Set the QAbstractSocket::LowDelayOption, which is the same as TCP_NODELAY
  60. SshLowDelaySocket = 0x4
  61. };
  62. Q_DECLARE_FLAGS(SshConnectionOptions, SshConnectionOption)
  63. /*!
  64. * \brief How strict to be when checking the remote key
  65. */
  66. enum SshHostKeyCheckingMode {
  67. /// Ignore the remote key
  68. SshHostKeyCheckingNone,
  69. /// Fail connection if either there is no key stored for this host or the key is not the same as earlier
  70. SshHostKeyCheckingStrict,
  71. /// Allow connecting if there is no stored key for the host, but fail if the key has changed
  72. SshHostKeyCheckingAllowNoMatch,
  73. /// Continue connection if the key doesn't match the stored key for the host
  74. SshHostKeyCheckingAllowMismatch
  75. };
  76. /*!
  77. * \brief Class to use to specify parameters used during connection.
  78. */
  79. class QSSH_EXPORT SshConnectionParameters
  80. {
  81. public:
  82. /*!
  83. * \brief What kinds of authentication to attempt
  84. */
  85. enum AuthenticationType {
  86. AuthenticationTypePassword, ///< Only attempt to connect using the password set with setPassword().
  87. AuthenticationTypePublicKey, ///< Only attempt to authenticate with public key
  88. /// Only attempt keyboard interactive authentication.
  89. /// For now this only changes what to send to the server,
  90. /// we will still just try to use the password set here.
  91. AuthenticationTypeKeyboardInteractive,
  92. /// Any method using the password set with setPassword().
  93. /// Some servers disable \a "password", others disable \a "keyboard-interactive"
  94. AuthenticationTypeTryAllPasswordBasedMethods,
  95. /// ssh-agent authentication only
  96. AuthenticationTypeAgent,
  97. };
  98. SshConnectionParameters();
  99. /*!
  100. * \brief Returns the hostname or IP set with setHost()
  101. */
  102. QString host() const { return url.host(); }
  103. /*!
  104. * \brief Returns the port set with setPort()
  105. */
  106. int port() const { return url.port(); }
  107. /*!
  108. * \brief Returns the username set with setUsername()
  109. * \return
  110. */
  111. QString userName() const { return url.userName(); }
  112. /*!
  113. * \brief Returns the password set with setPassword()
  114. */
  115. QString password() const { return url.password(); }
  116. /*!
  117. * \brief Sets the hostname or IP to connect to
  118. * \param host The remote host
  119. */
  120. void setHost(const QString &host) { url.setHost(host); }
  121. /*!
  122. * \brief Sets the remote port to use
  123. * \param port
  124. */
  125. void setPort(int port) { url.setPort(port); }
  126. /*!
  127. * \brief Sets the username to use
  128. * \param name Username
  129. */
  130. void setUserName(const QString &name) { url.setUserName(name); }
  131. /*!
  132. * \brief Sets the password to attempt to use
  133. * \param password
  134. */
  135. void setPassword(const QString &password) { url.setPassword(password); }
  136. QUrl url;
  137. QString privateKeyFile;
  138. int timeout; // In seconds.
  139. AuthenticationType authenticationType;
  140. SshConnectionOptions options;
  141. SshHostKeyCheckingMode hostKeyCheckingMode;
  142. SshHostKeyDatabasePtr hostKeyDatabase;
  143. };
  144. /// @cond
  145. QSSH_EXPORT bool operator==(const SshConnectionParameters &p1, const SshConnectionParameters &p2);
  146. QSSH_EXPORT bool operator!=(const SshConnectionParameters &p1, const SshConnectionParameters &p2);
  147. /// @endcond
  148. /*!
  149. * \brief Network connection info.
  150. */
  151. class QSSH_EXPORT SshConnectionInfo
  152. {
  153. public:
  154. SshConnectionInfo() : localPort(0), peerPort(0) {}
  155. SshConnectionInfo(const QHostAddress &la, quint16 lp, const QHostAddress &pa, quint16 pp)
  156. : localAddress(la), localPort(lp), peerAddress(pa), peerPort(pp) {}
  157. QHostAddress localAddress;
  158. quint16 localPort;
  159. QHostAddress peerAddress;
  160. quint16 peerPort;
  161. };
  162. /*!
  163. \class QSsh::SshConnection
  164. \brief This class provides an SSH connection, implementing protocol version 2.0
  165. See acquireConnection() which provides a pool mechanism for re-use.
  166. It can spawn channels for remote execution and SFTP operations (version 3).
  167. It operates asynchronously (non-blocking) and is not thread-safe.
  168. */
  169. class QSSH_EXPORT SshConnection : public QObject
  170. {
  171. Q_OBJECT
  172. public:
  173. /*!
  174. * \brief The current state of a connection
  175. */
  176. enum State { Unconnected, Connecting, Connected };
  177. /*!
  178. * \param serverInfo serverInfo connection parameters
  179. * \param parent Parent object.
  180. */
  181. explicit SshConnection(const SshConnectionParameters &serverInfo, QObject *parent = nullptr);
  182. void connectToHost();
  183. void disconnectFromHost();
  184. /*!
  185. * \brief Current state of this connection
  186. */
  187. State state() const;
  188. /*!
  189. * \brief Returns the error state of the connection
  190. * \returns If there is no error, returns \ref SshNoError if the connection is OK
  191. */
  192. SshError errorState() const;
  193. QString errorString() const;
  194. SshConnectionParameters connectionParameters() const;
  195. SshConnectionInfo connectionInfo() const;
  196. ~SshConnection();
  197. /*!
  198. * \brief Use this to launch remote commands
  199. * \param command The command to execute
  200. */
  201. QSharedPointer<SshRemoteProcess> createRemoteProcess(const QByteArray &command);
  202. /*!
  203. * \brief Creates a remote interactive session with a shell
  204. */
  205. QSharedPointer<SshRemoteProcess> createRemoteShell();
  206. QSharedPointer<SftpChannel> createSftpChannel();
  207. QSharedPointer<SshDirectTcpIpTunnel> createDirectTunnel(const QString &originatingHost,
  208. quint16 originatingPort, const QString &remoteHost, quint16 remotePort);
  209. QSharedPointer<SshTcpIpForwardServer> createForwardServer(const QString &remoteHost,
  210. quint16 remotePort);
  211. // -1 if an error occurred, number of channels closed otherwise.
  212. int closeAllChannels();
  213. int channelCount() const;
  214. const QByteArray &hostKeyFingerprint() const;
  215. /*!
  216. * \brief The X11 display name used for X11 forwarding
  217. * \return The name of the X11 display set for this connection
  218. */
  219. QString x11DisplayName() const;
  220. signals:
  221. /*!
  222. * \brief Emitted when ready for use
  223. */
  224. void connected();
  225. /*!
  226. * \brief Emitted when the connection has been closed
  227. */
  228. void disconnected();
  229. /*!
  230. * \brief Emitted when data has been received
  231. * \param message The content of the data, same as the output you would get when running \a ssh on the command line
  232. */
  233. void dataAvailable(const QString &message);
  234. /*!
  235. * \brief Emitted when an error occured
  236. */
  237. void error(QSsh::SshError);
  238. private:
  239. Internal::SshConnectionPrivate *d;
  240. };
  241. } // namespace QSsh
  242. Q_DECLARE_METATYPE(QSsh::SshConnectionParameters::AuthenticationType)
  243. #endif // SSHCONNECTION_H