summaryrefslogtreecommitdiffstats
path: root/tdesu/ssh.cpp
blob: c23ca459bc1dd1072a63ef1eed22d22e13ec6cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* vi: ts=8 sts=4 sw=4
*
* $Id$
*
* This file is part of the KDE project, module tdesu.
* Copyright (C) 2000 Geert Jansen <jansen@kde.org>
*
* This is free software; you can use this library under the GNU Library
* General Public License, version 2. See the file "COPYING.LIB" for the
* exact licensing terms.
*
* ssh.cpp: Execute a program on a remote machine using ssh.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <time.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <tqglobal.h>
#include <tqcstring.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <kstandarddirs.h>

#include "ssh.h"
#include "kcookie.h"


SshProcess::SshProcess(const TQCString &host, const TQCString &user, const TQCString &command)
{
    m_Host = host;
    m_User = user;
    m_Command = command;
    m_Stub = "tdesu_stub";
    srand(time(0L));
}


SshProcess::~SshProcess()
{
}


void SshProcess::setStub(const TQCString &stub)
{
    m_Stub = stub;
}


int SshProcess::checkInstall(const char *password)
{
    return exec(password, 1);
}


int SshProcess::checkNeedPassword()
{
    return exec(0L, 2);
}


int SshProcess::exec(const char *password, int check)
{
    if (check)
    setTerminal(true);

    QCStringList args;
    args += "-l"; args += m_User;
    args += "-o"; args += "StrictHostKeyChecking=no";
    args += m_Host; args += m_Stub;

    if (StubProcess::exec("ssh", args) < 0)
    {
        return check ? SshNotFound : -1;
    }

    int ret = ConverseSsh(password, check);
    if (ret < 0)
    {
        if (!check)
            kdError(900) << k_lineinfo << "Conversation with ssh failed\n";
        return ret;
    }
    if (check == 2)
    {
        if (ret == 1)
        {
            kill(m_Pid, SIGTERM);
            waitForChild();
        }
        return ret;
    }

    if (m_bErase && password)
    {
        char *ptr = const_cast<char *>(password);
        const uint plen = strlen(password);
        for (unsigned i=0; i < plen; i++)
            ptr[i] = '\000';
    }

    ret = ConverseStub(check);
    if (ret < 0)
    {
        if (!check)
            kdError(900) << k_lineinfo << "Converstation with tdesu_stub failed\n";
        return ret;
    }
    else if (ret == 1)
    {
        kill(m_Pid, SIGTERM);
        waitForChild();
        ret = SshIncorrectPassword;
    }

    if (check == 1)
    {
        waitForChild();
        return 0;
    }

    setExitString("Waiting for forwarded connections to terminate");
    ret = waitForChild();
    return ret;
}

/*
* Create a port forwarding for DCOP. For the remote port, we take a pseudo
* random number between 10k and 50k. This is not ok, of course, but I see
* no other way. There is, afaik, no security issue involved here. If the port
* happens to be occupied, ssh will refuse to start.
*
* 14/SEP/2000: DCOP forwarding is not used anymore.
*/

TQCString SshProcess::dcopForward()
{
    TQCString result;

    setDcopTransport("tcp");

    TQCString srv = StubProcess::dcopServer();
    if (srv.isEmpty())
        return result;

    int i = srv.find('/');
    if (i == -1)
        return result;
    if (srv.left(i) != "tcp")
        return result;
    int j = srv.find(':', ++i);
    if (j == -1)
        return result;
    TQCString host = srv.mid(i, j-i);
    bool ok;
    int port = srv.mid(++j).toInt(&ok);
    if (!ok)
        return result;

    m_dcopPort = 10000 + (int) ((40000.0 * rand()) / (1.0 + RAND_MAX));
    result.sprintf("%d:%s:%d", m_dcopPort, host.data(), port);
    return result;
}


/*
* Conversation with ssh.
* If check is 0, this waits for either a "Password: " prompt,
* or the header of the stub. If a prompt is received, the password is
* written back. Used for running a command.
* If check is 1, operation is the same as 0 except that if a stub header is
* received, the stub is stopped with the "stop" command. This is used for
* checking a password.
* If check is 2, operation is the same as 1, except that no password is
* written. The prompt is saved to m_Prompt. Used for checking the need for
* a password.
*/

int SshProcess::ConverseSsh(const char *password, int check)
{
    unsigned i, j, colon;

    TQCString line;
    int state = 0;

    while (state < 2)
    {
        line = readLine();
        const uint len = line.length();
        if (line.isNull())
            return -1;

        switch (state) {
        case 0:
            // Check for "tdesu_stub" header.
            if (line == "tdesu_stub")
            {
                unreadLine(line);
                return 0;
            }
    
            // Match "Password: " with the regex ^[^:]+:[\w]*$.
            for (i=0,j=0,colon=0; i<len; i++)
            {
                if (line[i] == ':')
                {
                    j = i; colon++;
                    continue;
                }
                if (!isspace(line[i]))
                    j++;
            }
            if ((colon == 1) && (line[j] == ':'))
            {
                if (check == 2)
                {
                    m_Prompt = line;
                    return SshNeedsPassword;
                }
                WaitSlave();
                write(m_Fd, password, strlen(password));
                write(m_Fd, "\n", 1);
                state++;
                break;
            }
    
            // Warning/error message.
            m_Error += line; m_Error += "\n";
            if (m_bTerminal)
                fprintf(stderr, "ssh: %s\n", line.data());
            break;
    
        case 1:
            if (line.isEmpty())
            {
                state++;
                break;
            }
            return -1;
        }
    }
    return 0;
}


// Display redirection is handled by ssh natively.
TQCString SshProcess::display()
{
    return "no";
}


TQCString SshProcess::displayAuth()
{
    return "no";
}


// Return the remote end of the forwarded connection.
TQCString SshProcess::dcopServer()
{
    return TQCString().sprintf("tcp/localhost:%d", m_dcopPort);
}

void SshProcess::virtual_hook( int id, void* data )
{ StubProcess::virtual_hook( id, data ); }