source: src-qt4/qsudo/mainwindow.cpp @ 49e8627

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

Add ability to qsudo to show privileged command. Privileged actions without description may cause high security risc (user don't know what action will be executed

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