source: src-qt4/qsudo/mainwindow.cpp @ b3051eb

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since b3051eb was b3051eb, checked in by yurkis <yurkis@…>, 17 months ago

Fix qsudo password input box behaviour

  • Property mode set to 100644
File size: 4.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 <QMessageBox>
15#include <QString>
16#include <QProcess>
17#include <QTextStream>
18#include <QApplication>
19#include <QDialogButtonBox>
20#include <QPushButton>
21#include "mainwindow.h"
22#include "../config.h"
23
24void MainWindow::ProgramInit()
25{
26  if( ! checkUserGroup() ) {
27    QMessageBox::critical(this, tr("Access Denied"),
28                                tr("This user does not have administrator permissions on this system!"),
29                                QMessageBox::Ok,
30                                QMessageBox::Ok);
31    exit(1);
32  }
33
34  tries=3;
35  connect(buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(slotButtonClicked(QAbstractButton *)));
36  connect(passwordLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed()));
37  connect(passwordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotPwdTextChanged(QString)));
38
39  QPushButton* btn= buttonBox->button(QDialogButtonBox::Ok);
40  if (btn)
41      btn->setEnabled(false);
42}
43
44void MainWindow::slotReturnPressed()
45{
46    if (passwordLineEdit->text().length())
47        testPass();
48}
49
50void MainWindow::testPass()
51{
52
53  QString program = "sudo";
54  QStringList arguments;
55  arguments << "-S";
56  arguments << "-k";
57  arguments << "true";
58
59  QProcess *tP = new QProcess(this);
60  tP->setProcessChannelMode(QProcess::MergedChannels);
61  tP->start(program, arguments);
62  tP->write(passwordLineEdit->text().toLatin1() + "\n");
63  tP->write(passwordLineEdit->text().toLatin1() + "\n");
64  tP->write(passwordLineEdit->text().toLatin1() + "\n");
65  while(tP->state() == QProcess::Starting || tP->state() == QProcess::Running ) {
66     tP->waitForFinished(500);
67     QCoreApplication::processEvents();
68  }
69  if ( tP->exitCode() != 0 )
70  {
71     QString tmp;
72     tmp.setNum(tries-1);
73     labelBadPW->setText(tr("Invalid Password! Tries Left: %1").arg(tmp) );
74     tries--;
75     if ( tries == 0 )
76       exit(1);
77     passwordLineEdit->setText("");
78  } else {
79     startSudo();
80  }
81}
82
83void MainWindow::startSudo()
84{
85  setVisible(false);
86  QString program = "sudo";
87  QStringList arguments;
88  arguments << "-S";
89  for ( int i = 1; i< qApp->argc() ; i++)
90    arguments << qApp->argv()[i];
91
92  sudoProc = new QProcess(this);
93  sudoProc->start(program, arguments);
94  sudoProc->write(passwordLineEdit->text().toLatin1() + "\n");
95  connect( sudoProc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotProcDone() ) );
96  connect( sudoProc, SIGNAL(readyReadStandardError()), this, SLOT(slotPrintStdErr() ) );
97  connect( sudoProc, SIGNAL(readyReadStandardOutput()), this, SLOT(slotPrintStdOut() ) );
98  while(sudoProc->state() == QProcess::Starting ) {
99     sudoProc->waitForFinished(500);
100     QCoreApplication::processEvents();
101  }
102}
103
104void MainWindow::slotPrintStdErr()
105{
106  QTextStream cout(stderr); 
107  cout << sudoProc->readAllStandardError();
108}
109
110void MainWindow::slotPrintStdOut()
111{
112  QTextStream cout(stdout); 
113  cout << sudoProc->readAllStandardOutput();
114}
115
116void MainWindow::slotPwdTextChanged(const QString &text)
117{
118    QPushButton* btn= buttonBox->button(QDialogButtonBox::Ok);
119    if (btn)
120        btn->setEnabled(text.length()?true:false);
121}
122
123void MainWindow::slotProcDone()
124{
125   exit(sudoProc->exitCode());
126}
127
128void MainWindow::slotButtonClicked(QAbstractButton *myBut)
129{
130  if ( buttonBox->buttonRole(myBut) == QDialogButtonBox::AcceptRole ) 
131     testPass();
132  close();
133}
134
135bool MainWindow::checkUserGroup()
136{
137   QString loginName = getlogin();
138   QString groupName = "wheel"; // group to check
139   QStringList gNames;
140   if ( loginName == "root" )
141     return true;
142   
143   QString tmp;
144   QFile iFile("/etc/group");
145   if ( ! iFile.open(QIODevice::ReadOnly | QIODevice::Text))
146     return true; //or FALSE?
147 
148   while ( !iFile.atEnd() ) {
149     tmp = iFile.readLine().simplified();
150     if ( tmp.indexOf(groupName) == 0 ) {
151        gNames = tmp.section(":", 3, 3).split(",");
152        break;
153     }
154   }
155   iFile.close();
156
157   if ( gNames.isEmpty() )
158      return false;
159
160   for ( int i = 0; i < gNames.size(); ++i )
161      if ( gNames.at(i).indexOf(loginName) == 0 )
162            return true;
163
164   return false;
165}
166
Note: See TracBrowser for help on using the repository browser.