File indexing completed on 2024-04-28 15:22:04

0001 /*
0002     This file is part of the KDE project, module kdesu.
0003     SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only
0006 */
0007 
0008 #ifndef KDESUSUPROCESS_H
0009 #define KDESUSUPROCESS_H
0010 
0011 #include <kdesu/kdesu_export.h>
0012 
0013 #include "stubprocess.h"
0014 
0015 namespace KDESu
0016 {
0017 class SuProcessPrivate;
0018 
0019 /** \class SuProcess suprocess.h KDESu/SuProcess
0020  * Executes a command under elevated privileges, using su.
0021  */
0022 
0023 class KDESU_EXPORT SuProcess : public StubProcess
0024 {
0025 public:
0026     enum Errors {
0027         SuNotFound = 1,
0028         SuNotAllowed,
0029         SuIncorrectPassword,
0030     };
0031 
0032     /**
0033      * Executes the command. This will wait for the command to finish.
0034      */
0035     enum checkMode {
0036         NoCheck = 0,
0037         Install = 1,
0038         NeedPassword = 2,
0039     };
0040 
0041     explicit SuProcess(const QByteArray &user = nullptr, const QByteArray &command = nullptr);
0042     ~SuProcess() override;
0043 
0044     int exec(const char *password, int check = NoCheck);
0045 
0046     /**
0047      * Checks if the stub is installed and the password is correct.
0048      * @return Zero if everything is correct, nonzero otherwise.
0049      */
0050     int checkInstall(const char *password);
0051 
0052     /**
0053      * Checks if a password is needed.
0054      */
0055     int checkNeedPassword();
0056 
0057     /**
0058      * Checks what the default super user command is, e.g. sudo, su, etc
0059      * @return the default super user command
0060      */
0061     QString superUserCommand();
0062 
0063     /**
0064      * Checks whether or not the user's password is being asked for or another
0065      * user's password. Due to usage of systems such as sudo, even when attempting
0066      * to switch to another user one may need to enter their own password.
0067      */
0068     bool useUsersOwnPassword();
0069 
0070 protected:
0071     void virtual_hook(int id, void *data) override;
0072 
0073 private:
0074     enum SuErrors {
0075         error = -1,
0076         ok = 0,
0077         killme = 1,
0078         notauthorized = 2,
0079     };
0080 
0081     KDESU_NO_EXPORT int converseSU(const char *password);
0082 
0083 private:
0084     Q_DECLARE_PRIVATE_D(PtyProcess::d, SuProcess)
0085 #if KDESU_BUILD_DEPRECATED_SINCE(5, 79)
0086     // Unused, kept for ABI compatibility
0087     const void *__kdesu_d_do_not_use;
0088 #endif
0089 };
0090 
0091 }
0092 
0093 #endif // KDESUSUPROCESS_H