File indexing completed on 2024-04-28 05:27:02

0001 /* vi: ts=8 sts=4 sw=4
0002  *
0003  * This file is part of the KDE project, module kdesu.
0004  * SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
0005  * SPDX-License-Identifier: Artistic-2.0
0006  */
0007 
0008 #include "sudlg.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QPushButton>
0012 #include <qstyle.h>
0013 
0014 KDEsuDialog::KDEsuDialog(QByteArray user, QByteArray authUser, bool enableKeep, const QString &icon, bool withIgnoreButton)
0015     : KPasswordDialog(nullptr, enableKeep ? ShowKeepPassword : NoFlags)
0016 {
0017     if (!icon.isEmpty()) {
0018         setIcon(QIcon::fromTheme(icon));
0019     }
0020 
0021     if (withIgnoreButton) {
0022         buttonBox()->addButton(QDialogButtonBox::Ignore);
0023     }
0024 
0025     proc.setUser(authUser);
0026 
0027     setWindowTitle(i18n("Run as %1", QString::fromLatin1(user)));
0028 
0029     QString prompt;
0030     if (proc.useUsersOwnPassword()) {
0031         prompt = i18n("Please enter your password below.");
0032     } else {
0033         if (authUser == "root") {
0034             if (withIgnoreButton) {
0035                 prompt = QStringLiteral("<qt>")
0036                     + i18n("The action you requested needs <b>root privileges</b>. "
0037                            "Please enter <b>root's</b> password below or click "
0038                            "Ignore to continue with your current privileges.")
0039                     + QStringLiteral("</qt>");
0040             } else {
0041                 prompt = QStringLiteral("<qt>")
0042                     + i18n("The action you requested needs <b>root privileges</b>. "
0043                            "Please enter <b>root's</b> password below.")
0044                     + QStringLiteral("</qt>");
0045             }
0046         } else {
0047             if (withIgnoreButton) {
0048                 prompt = QStringLiteral("<qt>")
0049                     + i18n("The action you requested needs additional privileges. "
0050                            "Please enter the password for <b>%1</b> below or click "
0051                            "Ignore to continue with your current privileges.",
0052                            QString::fromLatin1(authUser))
0053                     + QStringLiteral("</qt>");
0054             } else {
0055                 prompt = QStringLiteral("<qt>")
0056                     + i18n("The action you requested needs additional privileges. "
0057                            "Please enter the password for <b>%1</b> below.",
0058                            QString::fromLatin1(authUser))
0059                     + QStringLiteral("</qt>");
0060             }
0061         }
0062     }
0063     setPrompt(prompt);
0064 
0065     if (withIgnoreButton) {
0066         connect(buttonBox()->button(QDialogButtonBox::Ignore), &QAbstractButton::clicked, this, &KDEsuDialog::slotUser1);
0067     }
0068 }
0069 
0070 KDEsuDialog::~KDEsuDialog()
0071 {
0072 }
0073 
0074 bool KDEsuDialog::checkPassword()
0075 {
0076     int status = proc.checkInstall(password().toLocal8Bit().constData());
0077     switch (status) {
0078     case -1:
0079         showErrorMessage(i18n("Conversation with su failed."), UsernameError);
0080         return false;
0081 
0082     case 0:
0083         return true;
0084 
0085     case SuProcess::SuNotFound:
0086         showErrorMessage(i18n("The program 'su' could not be found.<br />"
0087                               "Ensure your PATH is set correctly."),
0088                          FatalError);
0089         return false;
0090 
0091     case SuProcess::SuNotAllowed:
0092         // This is actually never returned, as kdesu cannot tell the difference.
0093         showErrorMessage(QLatin1String("The impossible happened."), FatalError);
0094         return false;
0095 
0096     case SuProcess::SuIncorrectPassword:
0097         showErrorMessage(i18n("Permission denied.<br />"
0098                               "Possibly incorrect password, please try again.<br />"
0099                               "On some systems, you need to be in a special "
0100                               "group (often: wheel) to use this program."),
0101                          PasswordError);
0102         return false;
0103 
0104     default:
0105         showErrorMessage(i18n("Internal error: illegal return from "
0106                               "SuProcess::checkInstall()"),
0107                          FatalError);
0108         done(Rejected);
0109         return false;
0110     }
0111 }
0112 
0113 void KDEsuDialog::slotUser1()
0114 {
0115     done(AsUser);
0116 }
0117 
0118 #include "moc_sudlg.cpp"