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
|
/*
This file is part of the KDE project, module kdesu.
SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
SPDX-License-Identifier: GPL-2.0-only
ssh.cpp: Execute a program on a remote machine using ssh.
*/
#include "sshprocess.h"
#include "kcookie_p.h"
#include "stubprocess_p.h"
#include <ksu_debug.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
extern int kdesuDebugArea();
namespace KDESu
{
using namespace KDESuPrivate;
class SshProcessPrivate : public StubProcessPrivate
{
public:
SshProcessPrivate(const QByteArray &host)
: host(host)
, stub("kdesu_stub")
{
}
QByteArray prompt;
QByteArray host;
QByteArray error;
QByteArray stub;
};
SshProcess::SshProcess(const QByteArray &host, const QByteArray &user, const QByteArray &command)
: StubProcess(*new SshProcessPrivate(host))
{
m_user = user;
m_command = command;
srand(time(nullptr));
}
SshProcess::~SshProcess() = default;
void SshProcess::setHost(const QByteArray &host)
{
Q_D(SshProcess);
d->host = host;
}
void SshProcess::setStub(const QByteArray &stub)
{
Q_D(SshProcess);
d->stub = stub;
}
int SshProcess::checkInstall(const char *password)
{
return exec(password, 1);
}
int SshProcess::checkNeedPassword()
{
return exec(nullptr, 2);
}
int SshProcess::exec(const char *password, int check)
{
Q_D(SshProcess);
if (check) {
setTerminal(true);
}
QList<QByteArray> args;
args += "-l";
args += m_user;
args += "-o";
args += "StrictHostKeyChecking=no";
args += d->host;
args += d->stub;
if (StubProcess::exec("ssh", args) < 0) {
return check ? SshNotFound : -1;
}
int ret = converseSsh(password, check);
if (ret < 0) {
if (!check) {
qCCritical(KSU_LOG) << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Conversation with ssh failed.";
}
return ret;
}
if (check == 2) {
if (ret == 1) {
kill(m_pid, SIGTERM);
waitForChild();
}
return ret;
}
if (m_erase && password) {
memset(const_cast<char *>(password), 0, qstrlen(password));
}
ret = converseStub(check);
if (ret < 0) {
if (!check) {
qCCritical(KSU_LOG) << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Conversation with kdesu_stub failed.";
}
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;
}
QByteArray SshProcess::prompt() const
{
Q_D(const SshProcess);
return d->prompt;
}
QByteArray SshProcess::error() const
{
Q_D(const SshProcess);
return d->error;
}
/*
* 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 prompt. Used for checking the need for
* a password.
*/
int SshProcess::converseSsh(const char *password, int check)
{
Q_D(SshProcess);
unsigned i;
unsigned j;
unsigned colon;
QByteArray 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 "kdesu_stub" header.
if (line == "kdesu_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) {
d->prompt = line;
return SshNeedsPassword;
}
if (waitSlave()) {
return -1;
}
write(fd(), password, strlen(password));
write(fd(), "\n", 1);
state++;
break;
}
// Warning/error message.
d->error += line;
d->error += '\n';
if (m_terminal) {
fprintf(stderr, "ssh: %s\n", line.constData());
}
break;
case 1:
if (line.isEmpty()) {
state++;
break;
}
return -1;
}
}
return 0;
}
// Display redirection is handled by ssh natively.
QByteArray SshProcess::display()
{
return "no";
}
QByteArray SshProcess::displayAuth()
{
return "no";
}
void SshProcess::virtual_hook(int id, void *data)
{
StubProcess::virtual_hook(id, data);
}
} // namespace KDESu
|