1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "sftpdefs.h"
29 #include "ssh_global.h"
30 
31 #include <QByteArray>
32 #include <QFlags>
33 #include <QMetaType>
34 #include <QObject>
35 #include <QString>
36 #include <QHostAddress>
37 #include <QUrl>
38 
39 #include <memory>
40 
41 namespace Utils { class FilePath; }
42 
43 namespace QSsh {
44 class SshRemoteProcess;
45 
46 enum SshHostKeyCheckingMode {
47     SshHostKeyCheckingNone,
48     SshHostKeyCheckingStrict,
49     SshHostKeyCheckingAllowNoMatch,
50 };
51 
52 class QSSH_EXPORT SshConnectionParameters
53 {
54 public:
55     enum AuthenticationType {
56         AuthenticationTypeAll,
57         AuthenticationTypeSpecificKey,
58     };
59 
60     SshConnectionParameters();
61 
host()62     QString host() const { return url.host(); }
port()63     quint16 port() const { return url.port(); }
userName()64     QString userName() const { return url.userName(); }
setHost(const QString & host)65     void setHost(const QString &host) { url.setHost(host); }
setPort(int port)66     void setPort(int port) { url.setPort(port); }
setUserName(const QString & name)67     void setUserName(const QString &name) { url.setUserName(name); }
68 
69     QUrl url;
70     QString privateKeyFile;
71     QString x11DisplayName;
72     int timeout = 0; // In seconds.
73     AuthenticationType authenticationType = AuthenticationTypeAll;
74     SshHostKeyCheckingMode hostKeyCheckingMode = SshHostKeyCheckingAllowNoMatch;
75 };
76 
77 QSSH_EXPORT bool operator==(const SshConnectionParameters &p1, const SshConnectionParameters &p2);
78 QSSH_EXPORT bool operator!=(const SshConnectionParameters &p1, const SshConnectionParameters &p2);
79 
80 class QSSH_EXPORT SshConnectionInfo
81 {
82 public:
83     SshConnectionInfo() = default;
SshConnectionInfo(const QHostAddress & la,quint16 lp,const QHostAddress & pa,quint16 pp)84     SshConnectionInfo(const QHostAddress &la, quint16 lp, const QHostAddress &pa, quint16 pp)
85         : localAddress(la), localPort(lp), peerAddress(pa), peerPort(pp) {}
86 
isValid()87     bool isValid() const { return peerPort != 0; }
88 
89     QHostAddress localAddress;
90     quint16 localPort = 0;
91     QHostAddress peerAddress;
92     quint16 peerPort = 0;
93 };
94 
95 using SshRemoteProcessPtr = std::unique_ptr<SshRemoteProcess>;
96 
97 class QSSH_EXPORT SshConnection : public QObject
98 {
99     Q_OBJECT
100 
101 public:
102     enum State { Unconnected, Connecting, Connected, Disconnecting };
103 
104     explicit SshConnection(const SshConnectionParameters &serverInfo, QObject *parent = nullptr);
105 
106     void connectToHost();
107     void disconnectFromHost();
108     State state() const;
109     QString errorString() const;
110     SshConnectionParameters connectionParameters() const;
111     SshConnectionInfo connectionInfo() const;
112     QStringList connectionOptions(const Utils::FilePath &binary) const;
113     bool sharingEnabled() const;
114     ~SshConnection();
115 
116     SshRemoteProcessPtr createRemoteProcess(const QString &command);
117     SshRemoteProcessPtr createRemoteShell();
118     SftpTransferPtr createUpload(const FilesToTransfer &files,
119                                  FileTransferErrorHandling errorHandlingMode);
120     SftpTransferPtr createDownload(const FilesToTransfer &files,
121                                    FileTransferErrorHandling errorHandlingMode);
122     SftpSessionPtr createSftpSession();
123 
124 signals:
125     void connected();
126     void disconnected();
127     void dataAvailable(const QString &message);
128     void errorOccurred();
129 
130 private:
131     void doConnectToHost();
132     void emitError(const QString &reason);
133     void emitConnected();
134     void emitDisconnected();
135     SftpTransferPtr setupTransfer(const FilesToTransfer &files, Internal::FileTransferType type,
136                                   FileTransferErrorHandling errorHandlingMode);
137 
138     struct SshConnectionPrivate;
139     SshConnectionPrivate * const d;
140 };
141 
142 } // namespace QSsh
143 
144 Q_DECLARE_METATYPE(QSsh::SshConnectionParameters::AuthenticationType)
145