1 /**************************************************************************
2 *   Copyright (C) 2005-2020 by Oleksandr Shneyder                         *
3 *                              <o.shneyder@phoca-gmbh.de>                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *   This program is distributed in the hope that it will be useful,       *
10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 *   GNU General Public License for more details.                          *
13 *                                                                         *
14 *   You should have received a copy of the GNU General Public License     *
15 *   along with this program.  If not, see <https://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17 
18 #ifndef SSHPROCESS_H
19 #define SSHPROCESS_H
20 
21 #ifndef Q_OS_WIN
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #endif
27 #include <libssh/libssh.h>
28 #include <QObject>
29 #include <QProcess>
30 #ifndef Q_OS_WIN
31 #include <netinet/in.h>
32 #endif
33 
34 #include "sshmasterconnection.h"
35 
36 class SshProcess : public QObject
37 {
38     Q_OBJECT
39     friend class SshMasterConnection;
40 private:
41 
42     SshProcess(SshMasterConnection* master, int pid);
43     ~SshProcess();
44 
45     void startNormal(const QString& cmd, bool overridePath);
46     void startTunnel(const QString& forwardHost, uint forwardPort, const QString& localHost,
47                      uint localPort, bool reverse=false);
48     void start_cp(QString src, QString dst);
getSource()49     QString getSource()
50     {
51         return scpSource;
52     }
53 
54     void tunnelLoop();
55 #ifdef Q_OS_WIN
56     void    addPuttyReg(QString host, QString uuidStr);
57     void    rmPuttyReg(QString uuidStr);
58 #endif
59 
60 private:
61     SshMasterConnection* masterCon;
62     SshMasterConnection* tunnelConnection;
63     int pid;
64     QString forwardHost;
65     QString localHost;
66     QString command;
67     QString scpSource;
68     quint16 forwardPort;
69     quint16 localPort;
70     uint serverSocket;
71     struct sockaddr_in address;
72 #ifndef  Q_OS_WIN
73     socklen_t addrlen;
74 #else
75     int addrlen;
76 #endif
77     QString stdOutString;
78     QString stdErrString;
79     QString abortString;
80     bool tunnel;
81     bool normalExited;
82 //only to use with krb (until no GSSAPI support in libssh)
83     QProcess* proc;
84     QString procUuid;
85     bool execProcess;
86     bool tunnelOkEmited;
87 
88 private slots:
89     void slotCheckNewConnection();
90     void slotStdErr(SshProcess* creator, QByteArray data);
91     void slotStdOut(SshProcess* creator, QByteArray data);
92     void slotIOerr(SshProcess* creator,QString message, QString sshSessionErr);
93     void slotChannelClosed(SshProcess* creator, QString uuid);
94     void slotReverseTunnelOk(SshProcess* creator);
95     void slotReverseTunnelFailed(SshProcess* creator, QString error);
96     void slotCopyOk(SshProcess* creator);
97     void slotCopyErr(SshProcess* creator,QString message, QString sshSessionErr);
98     //krb stuff
99     void slotSshProcFinished( int exitCode, QProcess::ExitStatus exitStatus);
100     void slotSshProcStdErr();
101     void slotSshProcStdOut();
102 signals:
103     void sshFinished ( bool result, QString output, int processId);
104     void sshTunnelOk(int processId);
105 };
106 
107 #endif // SSHPROCESS_H
108