source: src-qt4/qsudo/mainwindow.cpp @ 33b8b3a

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 33b8b3a was 33b8b3a, checked in by Kris Moore <kris@…>, 9 months ago

Update qsudo, now it is smarter, will test passwords and let you retry up to 3x

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/****************************************************************************
2** ui.h extension file, included from the uic-generated form implementation.
3**
4** If you want to add, delete, or rename functions or slots, use
5** Qt Designer to update this file, preserving your code.
6**
7** You should not define a constructor or destructor in this file.
8** Instead, write your code in functions called init() and destroy().
9** These will automatically be called by the form's constructor and
10** destructor.
11*****************************************************************************/
12#include <QDebug>
13#include <QDir>
14#include <QString>
15#include <QProcess>
16#include <QTextStream>
17#include <QApplication>
18#include "mainwindow.h"
19#include "../config.h"
20
21void MainWindow::ProgramInit()
22{
23  tries=3;
24  connect(buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(slotButtonClicked(QAbstractButton *)));
25  connect(passwordLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed()));
26}
27
28void MainWindow::slotReturnPressed()
29{
30  testPass();
31}
32
33void MainWindow::testPass()
34{
35
36  QString program = "sudo";
37  QStringList arguments;
38  arguments << "-S";
39  arguments << "-k";
40  arguments << "true";
41
42  QProcess *tP = new QProcess(this);
43  tP->start(program, arguments);
44  tP->write(passwordLineEdit->text().toLatin1() + "\n");
45  tP->write(passwordLineEdit->text().toLatin1() + "\n");
46  tP->write(passwordLineEdit->text().toLatin1() + "\n");
47  while(tP->state() == QProcess::Starting || tP->state() == QProcess::Running ) {
48     tP->waitForFinished(500);
49     QCoreApplication::processEvents();
50     qDebug() << "Waiting...";
51  }
52  if ( tP->exitCode() != 0 )
53  {
54     QString tmp;
55     tmp.setNum(tries-1);
56     labelBadPW->setText(tr("Invalid Password! Tries Left: %1").arg(tmp) );
57     tries--;
58     if ( tries == 0 )
59       exit(1);
60  } else {
61     startSudo();
62  }
63}
64
65void MainWindow::startSudo()
66{
67  setVisible(false);
68  QString program = "sudo";
69  QStringList arguments;
70  arguments << "-S";
71  for ( int i = 1; i< qApp->argc() ; i++)
72    arguments << qApp->argv()[i];
73
74  sudoProc = new QProcess(this);
75  sudoProc->start(program, arguments);
76  sudoProc->write(passwordLineEdit->text().toLatin1() + "\n");
77  connect( sudoProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotProcDone() ) );
78  while(sudoProc->state() == QProcess::Starting ) {
79     sudoProc->waitForFinished(500);
80     QCoreApplication::processEvents();
81  }
82}
83
84void MainWindow::slotProcDone()
85{
86   exit(sudoProc->exitCode());
87}
88
89void MainWindow::slotButtonClicked(QAbstractButton *myBut)
90{
91  if ( buttonBox->buttonRole(myBut) == QDialogButtonBox::AcceptRole ) 
92     testPass();
93  close();
94}
Note: See TracBrowser for help on using the repository browser.