summaryrefslogtreecommitdiffstats
path: root/kpackage/kpPty.cpp
blob: 12f15ec3a574b31c315160a9d572a88338d01cb9 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
** Copyright (C) 1999,2000 Toivo Pedaste <[email protected]>
**
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to [email protected]
*/


#include "../config.h"

#include <tqtimer.h>
#include <tqregexp.h> 

#include <tdeprocctrl.h>
#include <kpty.h>
#include <kdebug.h>
#include <kpassdlg.h>

#include <kpPty.h>
#include <kpackage.h>
#include <kpTerm.h>
#include <options.h>
#include <utils.h>

#define SHPROMPT "# "
const int TIMEOUT = -3;
const int PASSWORD = -2;
const int PROMPT = -1;

extern Opts *opts;
//////////////////////////////////////////////////////////////////////////////

kpKProcIO::kpKProcIO ( TQTextCodec *_codec)
  : TDEProcIO(_codec)
{
}

kpKProcIO::~kpKProcIO()
{
}

bool kpKProcIO::sstart (RunMode runmode)
{

  connect (this, TQ_SIGNAL (receivedStdout (TDEProcess *, char *, int)),
	   this, TQ_SLOT (received (TDEProcess *, char *, int)));


  connect (this, TQ_SIGNAL (wroteStdin(TDEProcess *)),
	   this, TQ_SLOT (sent (TDEProcess *)));

  return TDEProcess::start (runmode,( TDEProcess::Communication) ( TDEProcess::Stdin |  TDEProcess::Stdout));
}

//////////////////////////////////////////////////////////////////////////////

kpPty::kpPty() : TQObject()
{
  pty = new kpKProcIO();
  pty->setUsePty(TDEProcess::All, false);

  connect(pty, TQ_SIGNAL(readReady(TDEProcIO *)), this,
		   TQ_SLOT(readLines()));
  connect(pty, TQ_SIGNAL(processExited(TDEProcess *)), this,
		   TQ_SLOT(done()));
  pty->pty()->setWinSize(0,80);
  tm = new TQTimer(this);
  connect(tm, TQ_SIGNAL(timeout()), this, TQ_SLOT(slotTimeout()));

  eventLoop = FALSE;
  inSession = FALSE;
  pUnterm = FALSE;
  loginSession = FALSE;
  
  codec = TQTextCodec::codecForLocale();
  TQMap<TQString, TQString> passwords;
}


kpPty::~kpPty()
{
}

void kpPty::startSu()
{
  kdDebug() << "startSu()\n";
  pty->setEnvironment("PS1", SHPROMPT);
#if defined(__FreeBSD__) || defined(__bsdi__)
  (*pty) << "su";
#else
  (*pty) << "su" << "-s" << "/bin/sh";
#endif

}

void kpPty::startSudo()
{
  kdDebug() << "startSudo()\n";
  pty->setEnvironment("PS1", SHPROMPT);
  (*pty) << "sudo" << "-p" << "Password: " << "/bin/sh";
}

void kpPty::startSsh()
{
  kdDebug() << "startSsh()\n";
  (*pty) << "/usr/bin/ssh" << "-t" << "-l" << "root";
  if (hostName.isEmpty()) {
    (*pty) << "-o" << "StrictHostKeyChecking=no" << "localhost";
  } else {
    (*pty) << hostName;
  }
  (*pty) << "env PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin PS1='" SHPROMPT "' sh";
}

bool kpPty::needSession(bool needRoot) 
{
  return (!hostName.isEmpty() || needRoot);
}

bool kpPty::startSession(bool needRoot)
{
  bool interact = FALSE; // Have interacted with user, prevents loops
  bool passwordTried = FALSE; // Have tried the current save password, so need to put up dialog
  pUnterm = FALSE;
  kdDebug() << "kpPty::startSession\n";
  if (!inSession && needSession(needRoot)) {
    // Assume !needRoot actions are simple executables
    kdDebug() << "kpPty::startSession TRUE\n";
    loginSession = TRUE;
    int ret;
    TQString s = "echo START=$?\n";

  FULL_RESTART:
    interact = FALSE;
    retList.clear();
    pty->resetAll();

    TQString passMsg;
    kdDebug() << "privCmd=" << opts->privCmd << "\n";
    if (opts->privCmd == Opts::SSHcmd || !hostName.isEmpty()) {
      passMsg = i18n("The action you requested uses ssh. Please enter the password or pass phrase.\n");
      startSsh();
    } else if (opts->privCmd == Opts::SUcmd) {
      passMsg = i18n("The action you requested needs root privileges. Please enter root's password.\n");
      startSu();
    } else if (opts->privCmd == Opts::SUDOcmd) {
      passMsg = i18n("The action you requested needs root privileges. Please enter your SUDO password.\n");
      startSudo();
    }
    pty->sstart(TDEProcess::NotifyOnExit);

  RESTART:
    tm->start(6*1000, TRUE);
    eventLoop = TRUE;
    kdDebug() << "Loopst\n";
    kapp->enter_loop();
    kdDebug() << "Loopfn Result=" << Result  <<  "\n";
    tm->stop();
    if (Result == TIMEOUT) { // timeout
      interact = TRUE;
      //      kdDebug() << "Line=" << retList.last() << "\n";
      kpstart->addText(retList);
      kpstart->run("", i18n("Login Problem: Please login manually"));

      ret = kpstart->exec();
      kdDebug() << "Sret=" << ret << "\n";
      if (ret) {
	inSession = FALSE;
      } else {
	inSession = TRUE;
      }
    } else if (Result == PASSWORD) {  // We got a password prompt
      TQString pass;
      int res;
      interact = TRUE;
      // kdDebug() << "H=" << hostName << " PH=" << passwords[hostName] << " PT=" << passwordTried <<"\n";
      if (passwords[hostName] != 0 && !passwordTried) {
        pass = passwords[hostName];
        res = 1;
      } else {
        kdDebug() << "Passwd=" << retList.last() << "\n";
        TQString msg = passMsg;
        //        kdDebug() << "privCmd=" << opts->privCmd << " host=" << hostName.isEmpty() << "\n";
        if (opts->privCmd == Opts::SSHcmd || !hostName.isEmpty()) {
          msg += retList.last();
        }
        int keep = 1;
        res = KPasswordDialog::getPassword(pass,msg,&keep);
        //     kdDebug() << "Pass=" << pass << " Keep=" << keep << " Res=" << res << "\n";
        if (keep) {
          passwords[hostName] = pass;
        } else {
          passwords.remove(hostName);
        }
      }
      pty->writeStdin(pass.append("\n"), false);
      passwordTried = TRUE;
      if (res) {
        retList.clear();
        goto RESTART;
      } else {
        inSession = FALSE;
      }
    } else if (Result == PROMPT) { // Got Prompt
      inSession = TRUE;
      kdDebug() << "kpPty::startSession TRUE\n";
    } else {  // process return code
      pty->writeStdin(TQCString("\04"), false);  // SU doesn't listen to ^C
      if (interact) {
	goto FULL_RESTART;
      } else {
	TQString errMsg = retList.join(" ");
	KpMsgE(errMsg, TRUE);
        inSession = FALSE;
      }
    }
  } else {
    kdDebug() << "kpPty::startSession Not needed\n";
  }
  
  loginSession = FALSE;
  if (!inSession)
    close();
  
  return inSession;
}

void kpPty::breakUpCmd(const TQString &cmd)
{
  kdDebug() << " kpPty::run CMD=\""<< cmd <<"\" pty = " << pty << endl;

  bool quote = FALSE;
  TQString s;
  TQStringList cl = TQStringList::split(" ", cmd);

  for ( TQStringList::Iterator it = cl.begin(); it != cl.end(); ++it ) {
    int lastPt = (*it).length() - 1;
    if ((*it)[0] == '\'') { // Start of quoted string
      s = *it;
      if ((*it)[lastPt] == '\'') { // Also End of quoted string
	s.replace("'","");
	(*pty) << s;
	quote = FALSE;
      } else {
	s += " ";
	quote = TRUE;
      }
    } else if ((*it)[lastPt] == '\'') { // End of quoted string
      s += *it;
      s.replace("'","");
      (*pty) << s;
      quote = FALSE;
    }	else if (quote) {
      s += *it;
      s += " ";
    } else {
      (*pty) << (*it);
    }
  }
}

TQStringList kpPty::run(const TQString &cmd, bool inLoop, bool needRoot)
{
  Result = 0;

  pUnterm = FALSE;

  if (!inSession && !needSession(needRoot)) {
    // Assume !needRoot actions are simple executables
    pty->resetAll();
    breakUpCmd(cmd);
    pty->setEnvironment("TERM", "dumb");
    if (!pty->sstart(TDEProcess::NotifyOnExit)) {
      kdDebug() << " kpPty::run execute=0\n";
      return 0;
    }
  } else {
    if (startSession(needRoot)) {
      kdDebug() << "CMDroot='"<< cmd <<"'\n";
      TQString s = cmd + ";echo RESULT=$?";
      pty->writeStdin(s);
      kdDebug() << " kpPty::run session\n";
    } else {
      kdDebug() << " kpPty::run other=0\n";
      return 0;
    }
  }

  retList.clear();

  if (inLoop) {
    eventLoop = TRUE;
    kapp->enter_loop();

    return retList;
  } else {
    return 0;
  }
}

void kpPty::close() {
  //  kdDebug() << "kpPty::close\n";

  pty->closeAll();
  while(pty->isRunning()) {
    TDEProcessController::theTDEProcessController->waitForProcessExit(1);
  }
  inSession = false;
}

void kpPty::finish(int ret)
{
   kdDebug() << "kpPty::finish " << ret << "\n";

  TQStringList::Iterator l;
  Result = ret;

  if (ret == PROMPT) {  // Called program executed in session
    if (!retList.empty()) {
      l = retList.fromLast();
      if ((*l).right(2) == SHPROMPT) {
	retList.remove(l);                  // Remove prompt
      }
    }

    if (!retList.empty()) {
      int p;
      l = retList.fromLast();
      if ((p = (*l).find("RESULT=")) >= 0) {
	ret = (*l).mid(p+7).toInt(0,10);
	retList.remove(l);                  // Remove return code
      } else {
	ret = 666;
      }
    }

    if (!retList.empty()) {
      l = retList.begin();
      if ( l !=  retList.end()) {
	if ((*l).find("RESULT=") >= 0) {
	  retList.remove(l);                  // Remove command at start
	}
      }
    }
  } 
  emit result(retList,ret);
 

  if (eventLoop) {
    eventLoop = FALSE;
    kapp->exit_loop();
  }
}

void kpPty::readLines()
{
  bool unterm = FALSE;

  TQString stext;
  while(pty->readln(stext, false, &unterm) >= 0)
  {
    stext = codec->toUnicode(stext.ascii(), stext.length());
    emit textIn(stext, !unterm);
//              kdDebug() << "[" << pUnterm << "-" << unterm << "-" << stext << ">\n";
    if (pUnterm) {
      TQStringList::Iterator lst = retList.fromLast();
      if (lst != retList.end())
      {
        stext = *lst + stext;
        retList.remove(lst);
      }
    }
    int i;
    if (!unterm)
    {
      while (stext.endsWith("\r")) {
          stext.truncate(stext.length()-1);
      }
       
      i = stext.findRev('\r');
      if (i > -1) {
        stext = stext.mid(i+1);
      }
    }

    pUnterm = unterm;

    retList << stext;
 //   kdDebug() << "++" << stext << "\n";
    if (stext.right(2) == SHPROMPT) {  // Shell prompt
      emit textIn("\r \n", false);
      finish(PROMPT);
    } else if (loginSession)  {
      TQRegExp rx( "^[^:]+:[\\s]*$");  // Password prompt
      if (rx.search(retList.last()) >= 0) {
        kdDebug() << loginSession << " " <<retList.last()<< " Match password p\n";
	finish(PASSWORD);
      }
    }
  }
  pty->ackRead();
}

void kpPty::keyOut(char ch)
{
  TQCString s(2);
  s[0] = ch;
  s[1] = '\0';
  pty->writeStdin(s, false);
}

void kpPty::done()
{
  int ret = pty->exitStatus();
  TQString stext;

  //kdDebug() << "Done (" << ret << ")" << endl;

  finish(ret);
}

void kpPty::slotTimeout()
{
  kdDebug() << "Timeout..............\n";
  finish(TIMEOUT);
}
#include "kpPty.moc"