File indexing completed on 2024-04-21 03:53:58

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     client.h: client to access kdesud.
0008 */
0009 
0010 #ifndef KDESUCLIENT_H
0011 #define KDESUCLIENT_H
0012 
0013 #include <kdesu/kdesu_export.h>
0014 
0015 #include <QByteArray>
0016 #include <QList>
0017 #include <memory>
0018 
0019 #ifdef Q_OS_UNIX
0020 
0021 namespace KDESu
0022 {
0023 /** \class Client client.h KDESu/Client
0024  * A client class to access kdesud, the KDE su daemon. Kdesud can assist in
0025  * password caching in two ways:
0026  *
0027  * @li For high security passwords, like for su and ssh, it executes the
0028  * password requesting command for you. It feeds the password to the
0029  * command, without ever returning it to you, the user.
0030  * See exec, setPass, delCommand.
0031  *
0032  * @li For lower security passwords, like web and ftp passwords, it can act
0033  * as a persistent storage for string variables. These variables are
0034  * returned to the user.
0035  * See setVar, delVar, delGroup.
0036  *
0037  * Porting from KF5 to KF6:
0038  *
0039  * The class KDESu::KDEsuClient was renamed to KDESu::Client.
0040  *
0041  * @since 6.0
0042  */
0043 class KDESU_EXPORT Client
0044 {
0045 public:
0046     Client();
0047     ~Client();
0048 
0049     Client(const Client &) = delete;
0050     Client &operator=(const Client &) = delete;
0051 
0052     /**
0053      * Lets kdesud execute a command. If the daemon does not have a password
0054      * for this command, this will fail and you need to call setPass().
0055      *
0056      * @param command The command to execute.
0057      * @param user The user to run the command as.
0058      * @param options Extra options.
0059      * @param env Extra environment variables.
0060      * @return Zero on success, -1 on failure.
0061      */
0062     int exec(const QByteArray &command, const QByteArray &user, const QByteArray &options = nullptr, const QList<QByteArray> &env = QList<QByteArray>());
0063 
0064     /**
0065      * Wait for the last command to exit and return the exit code.
0066      * @return Exit code of last command, -1 on failure.
0067      */
0068     int exitCode();
0069 
0070     /**
0071      * Set root's password, lasts one session.
0072      *
0073      * @param pass Root's password.
0074      * @param timeout The time that a password will live.
0075      * @return Zero on success, -1 on failure.
0076      */
0077     int setPass(const char *pass, int timeout);
0078 
0079     /**
0080      * Set the target host (optional).
0081      */
0082     int setHost(const QByteArray &host);
0083 
0084     /**
0085      * Set the desired priority (optional), see StubProcess.
0086      */
0087     int setPriority(int priority);
0088 
0089     /**
0090      * Set the desired scheduler (optional), see StubProcess.
0091      */
0092     int setScheduler(int scheduler);
0093 
0094     /**
0095      * Remove a password for a user/command.
0096      * @param command The command.
0097      * @param user The user.
0098      * @return zero on success, -1 on an error
0099      */
0100     int delCommand(const QByteArray &command, const QByteArray &user);
0101 
0102     /**
0103      * Set a persistent variable.
0104      * @param key The name of the variable.
0105      * @param value Its value.
0106      * @param timeout The timeout in seconds for this key. Zero means
0107      * no timeout.
0108      * @param group Make the key part of a group. See delGroup.
0109      * @return zero on success, -1 on failure.
0110      */
0111     int setVar(const QByteArray &key, const QByteArray &value, int timeout = 0, const QByteArray &group = nullptr);
0112 
0113     /**
0114      * Get a persistent variable.
0115      * @param key The name of the variable.
0116      * @return Its value.
0117      */
0118     QByteArray getVar(const QByteArray &key);
0119 
0120     /**
0121      * Gets all the keys that are membes of the given group.
0122      * @param group the group name of the variables.
0123      * @return a list of the keys in the group.
0124      */
0125     QList<QByteArray> getKeys(const QByteArray &group);
0126 
0127     /**
0128      * Returns true if the specified group exists is
0129      * cached.
0130      *
0131      * @param group the group key
0132      * @return true if the group is found
0133      */
0134     bool findGroup(const QByteArray &group);
0135 
0136     /**
0137      * Delete a persistent variable.
0138      * @param key The name of the variable.
0139      * @return zero on success, -1 on failure.
0140      */
0141     int delVar(const QByteArray &key);
0142 
0143     /**
0144      * Delete all persistent variables with the given key.
0145      *
0146      * A specicalized variant of delVar(QByteArray) that removes all
0147      * subsets of the cached variables given by @p key. In order for all
0148      * cached variables related to this key to be deleted properly, the
0149      * value given to the @p group argument when the setVar function
0150      * was called, must be a subset of the argument given here and the key
0151      *
0152      * @note Simply supplying the group key here WILL not necessarily
0153      * work. If you only have a group key, then use delGroup instead.
0154      *
0155      * @param special_key the name of the variable.
0156      * @return zero on success, -1 on failure.
0157      */
0158     int delVars(const QByteArray &special_key);
0159 
0160     /**
0161      * Delete all persistent variables in a group.
0162      *
0163      * @param group the group name. See setVar.
0164      * @return
0165      */
0166     int delGroup(const QByteArray &group);
0167 
0168     /**
0169      * Ping kdesud. This can be used for diagnostics.
0170      * @return Zero on success, -1 on failure
0171      */
0172     int ping();
0173 
0174     /**
0175      * Stop the daemon.
0176      */
0177     int stopServer();
0178 
0179     /**
0180      * Try to start up kdesud
0181      */
0182     int startServer();
0183 
0184 private:
0185     KDESU_NO_EXPORT int connect();
0186 
0187     KDESU_NO_EXPORT int command(const QByteArray &cmd, QByteArray *result = nullptr);
0188     KDESU_NO_EXPORT QByteArray escape(const QByteArray &str);
0189 
0190 private:
0191     std::unique_ptr<class ClientPrivate> const d;
0192 };
0193 
0194 } // END namespace KDESu
0195 
0196 #endif // Q_OS_UNIX
0197 
0198 #endif // KDESUCLIENT_H