File: sudlg.cpp

package info (click to toggle)
kde-cli-tools 4%3A6.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,396 kB
  • sloc: cpp: 4,368; sh: 42; makefile: 7
file content (118 lines) | stat: -rw-r--r-- 4,058 bytes parent folder | download
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
/* vi: ts=8 sts=4 sw=4
 *
 * This file is part of the KDE project, module kdesu.
 * SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
 * SPDX-License-Identifier: Artistic-2.0
 */

#include "sudlg.h"

#include <KLocalizedString>
#include <QPushButton>
#include <qstyle.h>

KDEsuDialog::KDEsuDialog(QByteArray user, QByteArray authUser, bool enableKeep, const QString &icon, bool withIgnoreButton)
    : KPasswordDialog(nullptr, enableKeep ? ShowKeepPassword : NoFlags)
{
    if (!icon.isEmpty()) {
        setIcon(QIcon::fromTheme(icon));
    }

    if (withIgnoreButton) {
        buttonBox()->addButton(QDialogButtonBox::Ignore);
    }

    proc.setUser(authUser);

    setWindowTitle(i18n("Run as %1", QString::fromLatin1(user)));

    QString prompt;
    if (proc.useUsersOwnPassword()) {
        prompt = i18n("Please enter your password below.");
    } else {
        if (authUser == "root") {
            if (withIgnoreButton) {
                prompt = QStringLiteral("<qt>")
                    + i18n("The action you requested needs <b>root privileges</b>. "
                           "Please enter <b>root's</b> password below or click "
                           "Ignore to continue with your current privileges.")
                    + QStringLiteral("</qt>");
            } else {
                prompt = QStringLiteral("<qt>")
                    + i18n("The action you requested needs <b>root privileges</b>. "
                           "Please enter <b>root's</b> password below.")
                    + QStringLiteral("</qt>");
            }
        } else {
            if (withIgnoreButton) {
                prompt = QStringLiteral("<qt>")
                    + i18n("The action you requested needs additional privileges. "
                           "Please enter the password for <b>%1</b> below or click "
                           "Ignore to continue with your current privileges.",
                           QString::fromLatin1(authUser))
                    + QStringLiteral("</qt>");
            } else {
                prompt = QStringLiteral("<qt>")
                    + i18n("The action you requested needs additional privileges. "
                           "Please enter the password for <b>%1</b> below.",
                           QString::fromLatin1(authUser))
                    + QStringLiteral("</qt>");
            }
        }
    }
    setPrompt(prompt);

    if (withIgnoreButton) {
        connect(buttonBox()->button(QDialogButtonBox::Ignore), &QAbstractButton::clicked, this, &KDEsuDialog::slotUser1);
    }
}

KDEsuDialog::~KDEsuDialog()
{
}

bool KDEsuDialog::checkPassword()
{
    int status = proc.checkInstall(password().toLocal8Bit().constData());
    switch (status) {
    case -1:
        showErrorMessage(i18n("Conversation with su failed."), UsernameError);
        return false;

    case 0:
        return true;

    case SuProcess::SuNotFound:
        showErrorMessage(i18n("The program 'su' could not be found.<br />"
                              "Ensure your PATH is set correctly."),
                         FatalError);
        return false;

    case SuProcess::SuNotAllowed:
        // This is actually never returned, as kdesu cannot tell the difference.
        showErrorMessage(QLatin1String("The impossible happened."), FatalError);
        return false;

    case SuProcess::SuIncorrectPassword:
        showErrorMessage(i18n("Permission denied.<br />"
                              "Possibly incorrect password, please try again.<br />"
                              "On some systems, you need to be in a special "
                              "group (often: wheel) to use this program."),
                         PasswordError);
        return false;

    default:
        showErrorMessage(i18n("Internal error: illegal return from "
                              "SuProcess::checkInstall()"),
                         FatalError);
        done(Rejected);
        return false;
    }
}

void KDEsuDialog::slotUser1()
{
    done(AsUser);
}

#include "moc_sudlg.cpp"