• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEsu

  • sources
  • kde-4.12
  • kdelibs
  • kdesu
ssh.cpp
Go to the documentation of this file.
1 /* vi: ts=8 sts=4 sw=4
2 *
3 * This file is part of the KDE project, module kdesu.
4 * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
5 *
6 * This is free software; you can use this library under the GNU Library
7 * General Public License, version 2. See the file "COPYING.LIB" for the
8 * exact licensing terms.
9 *
10 * ssh.cpp: Execute a program on a remote machine using ssh.
11 */
12 
13 #include "ssh.h"
14 #include "kcookie.h"
15 
16 #include <config.h>
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <time.h>
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 
31 #include <QtCore/QBool>
32 
33 #include <kdebug.h>
34 #include <klocale.h>
35 #include <kstandarddirs.h>
36 
37 extern int kdesuDebugArea();
38 
39 namespace KDESu {
40 
41 using namespace KDESuPrivate;
42 
43 class SshProcess::SshProcessPrivate
44 {
45 public:
46  SshProcessPrivate(const QByteArray &host)
47  : m_Host( host )
48  , m_Stub( "kdesu_stub" )
49  {}
50  QByteArray m_Prompt;
51  QByteArray m_Host;
52  QByteArray m_Error;
53  QByteArray m_Stub;
54 };
55 
56 
57 SshProcess::SshProcess(const QByteArray &host, const QByteArray &user, const QByteArray &command)
58  : d( new SshProcessPrivate(host) )
59 {
60  m_User = user;
61  m_Command = command;
62  srand(time(0L));
63 }
64 
65 
66 SshProcess::~SshProcess()
67 {
68  delete d;
69 }
70 
71 void SshProcess::setHost(const QByteArray &host)
72 {
73  d->m_Host = host;
74 }
75 
76 
77 void SshProcess::setStub(const QByteArray &stub)
78 {
79  d->m_Stub = stub;
80 }
81 
82 
83 int SshProcess::checkInstall(const char *password)
84 {
85  return exec(password, 1);
86 }
87 
88 
89 int SshProcess::checkNeedPassword()
90 {
91  return exec(0L, 2);
92 }
93 
94 
95 int SshProcess::exec(const char *password, int check)
96 {
97  if (check)
98  setTerminal(true);
99 
100  QList<QByteArray> args;
101  args += "-l"; args += m_User;
102  args += "-o"; args += "StrictHostKeyChecking=no";
103  args += d->m_Host; args += d->m_Stub;
104 
105  if (StubProcess::exec("ssh", args) < 0)
106  {
107  return check ? SshNotFound : -1;
108  }
109 
110  int ret = ConverseSsh(password, check);
111  if (ret < 0)
112  {
113  if (!check)
114  kError(kdesuDebugArea()) << k_lineinfo << "Conversation with ssh failed.";
115  return ret;
116  }
117  if (check == 2)
118  {
119  if (ret == 1)
120  {
121  kill(m_Pid, SIGTERM);
122  waitForChild();
123  }
124  return ret;
125  }
126 
127  if (m_bErase && password)
128  memset(const_cast<char *>(password), 0, qstrlen(password));
129 
130  ret = ConverseStub(check);
131  if (ret < 0)
132  {
133  if (!check)
134  kError(kdesuDebugArea()) << k_lineinfo << "Conversation with kdesu_stub failed.";
135  return ret;
136  }
137  else if (ret == 1)
138  {
139  kill(m_Pid, SIGTERM);
140  waitForChild();
141  ret = SshIncorrectPassword;
142  }
143 
144  if (check == 1)
145  {
146  waitForChild();
147  return 0;
148  }
149 
150  setExitString("Waiting for forwarded connections to terminate");
151  ret = waitForChild();
152  return ret;
153 }
154 
155 QByteArray SshProcess::prompt() const
156 {
157  return d->m_Prompt;
158 }
159 
160 QByteArray SshProcess::error() const
161 {
162  return d->m_Error;
163 }
164 
165 
166 /*
167 * Conversation with ssh.
168 * If check is 0, this waits for either a "Password: " prompt,
169 * or the header of the stub. If a prompt is received, the password is
170 * written back. Used for running a command.
171 * If check is 1, operation is the same as 0 except that if a stub header is
172 * received, the stub is stopped with the "stop" command. This is used for
173 * checking a password.
174 * If check is 2, operation is the same as 1, except that no password is
175 * written. The prompt is saved to m_Prompt. Used for checking the need for
176 * a password.
177 */
178 
179 int SshProcess::ConverseSsh(const char *password, int check)
180 {
181  unsigned i, j, colon;
182 
183  QByteArray line;
184  int state = 0;
185 
186  while (state < 2)
187  {
188  line = readLine();
189  const uint len = line.length();
190  if (line.isNull())
191  return -1;
192 
193  switch (state) {
194  case 0:
195  // Check for "kdesu_stub" header.
196  if (line == "kdesu_stub")
197  {
198  unreadLine(line);
199  return 0;
200  }
201 
202  // Match "Password: " with the regex ^[^:]+:[\w]*$.
203  for (i=0,j=0,colon=0; i<len; ++i)
204  {
205  if (line[i] == ':')
206  {
207  j = i; colon++;
208  continue;
209  }
210  if (!isspace(line[i]))
211  j++;
212  }
213  if ((colon == 1) && (line[j] == ':'))
214  {
215  if (check == 2)
216  {
217  d->m_Prompt = line;
218  return SshNeedsPassword;
219  }
220  if (WaitSlave())
221  return -1;
222  write(fd(), password, strlen(password));
223  write(fd(), "\n", 1);
224  state++;
225  break;
226  }
227 
228  // Warning/error message.
229  d->m_Error += line; d->m_Error += '\n';
230  if (m_bTerminal)
231  fprintf(stderr, "ssh: %s\n", line.constData());
232  break;
233 
234  case 1:
235  if (line.isEmpty())
236  {
237  state++;
238  break;
239  }
240  return -1;
241  }
242  }
243  return 0;
244 }
245 
246 
247 // Display redirection is handled by ssh natively.
248 QByteArray SshProcess::display()
249 {
250  return "no";
251 }
252 
253 
254 QByteArray SshProcess::displayAuth()
255 {
256  return "no";
257 }
258 
259 void SshProcess::virtual_hook( int id, void* data )
260 { StubProcess::virtual_hook( id, data ); }
261 
262 }
kdesuDebugArea
int kdesuDebugArea()
Definition: su.cpp:43
KDESu::PtyProcess::m_bTerminal
bool m_bTerminal
Indicates running in a terminal, causes additional newlines to be printed after output.
Definition: process.h:181
KDESu::SshProcess::virtual_hook
virtual void virtual_hook(int id, void *data)
Standard hack to add virtual methods in a BC way.
Definition: ssh.cpp:259
KDESu::SshProcess::SshProcess
SshProcess(const QByteArray &host=QByteArray(), const QByteArray &user=QByteArray(), const QByteArray &command=QByteArray())
Definition: ssh.cpp:57
kcookie.h
kdebug.h
KDESu::SshProcess::SshNeedsPassword
Definition: ssh.h:32
ssh.h
KDESu::SshProcess::setStub
void setStub(const QByteArray &stub)
Sets the localtion of the remote stub.
Definition: ssh.cpp:77
KDESu::SshProcess::prompt
QByteArray prompt() const
Definition: ssh.cpp:155
KDESu::PtyProcess::setExitString
void setExitString(const QByteArray &exit)
Sets the exit string.
Definition: process.cpp:282
KDESu::SshProcess::exec
int exec(const char *password, int check=0)
Executes the command.
Definition: ssh.cpp:95
KDESu::SshProcess::displayAuth
virtual QByteArray displayAuth()
See display.
Definition: ssh.cpp:254
KDESu::PtyProcess::readLine
QByteArray readLine(bool block=true)
Reads a line from the program's standard out.
Definition: process.cpp:239
KDESu::SshProcess::error
QByteArray error() const
Definition: ssh.cpp:160
KDESu::SshProcess::checkNeedPassword
int checkNeedPassword()
Checks if the current user@host needs a password.
Definition: ssh.cpp:89
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KDESu::PtyProcess::waitForChild
int waitForChild()
Waits for the child to exit.
Definition: process.cpp:430
KDESu::SshProcess::SshNotFound
Definition: ssh.h:32
klocale.h
KDESu::PtyProcess::m_Pid
int m_Pid
PID of child process.
Definition: process.h:185
KDESu::StubProcess::m_User
QByteArray m_User
Definition: stub.h:93
KDESu::StubProcess::m_Command
QByteArray m_Command
Definition: stub.h:92
KDESu::StubProcess::ConverseStub
int ConverseStub(int check)
Exchange all parameters with kdesu_stub.
Definition: stub.cpp:125
KDESu::PtyProcess::m_bErase
bool m_bErase
Definition: process.h:181
isspace
#define isspace(c)
KDESu::SshProcess::SshIncorrectPassword
Definition: ssh.h:32
KDESu::SshProcess::~SshProcess
~SshProcess()
Definition: ssh.cpp:66
KDESu::PtyProcess::exec
int exec(const QByteArray &command, const QList< QByteArray > &args)
Forks off and execute a command.
Definition: process.cpp:291
KDESu::SshProcess::checkInstall
int checkInstall(const char *password)
Checks if the stub is installed and if the password is correct.
Definition: ssh.cpp:83
KDESu::StubProcess::virtual_hook
virtual void virtual_hook(int id, void *data)
Standard hack to add virtual methods in a BC way.
Definition: stub.cpp:233
KDESu::PtyProcess::WaitSlave
int WaitSlave()
Waits until the pty has cleared the ECHO flag.
Definition: process.cpp:377
kstandarddirs.h
KDESu::PtyProcess::unreadLine
void unreadLine(const QByteArray &line, bool addNewline=true)
Puts back a line of input.
Definition: process.cpp:273
KDESu::SshProcess::setHost
void setHost(const QByteArray &host)
Sets the target host.
Definition: ssh.cpp:71
KDESu::PtyProcess::fd
int fd() const
Returns the filedescriptor of the process.
Definition: process.cpp:170
k_lineinfo
#define k_lineinfo
KDESu::SshProcess::display
virtual QByteArray display()
This virtual function can be overloaded when special behavior is desired.
Definition: ssh.cpp:248
QList< QByteArray >
KDESu::PtyProcess::setTerminal
void setTerminal(bool terminal)
Enables/disables terminal output.
Definition: process.cpp:412
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEsu

Skip menu "KDEsu"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal