LCOV - code coverage report
Current view: top level - manager-lib - sudo.h (source / functions) Coverage Total Hit
Test: QtApplicationManager Lines: 50.0 % 2 1
Test Date: 2024-03-11 10:33:33 Functions: - 0 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 2 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2021 The Qt Company Ltd.
       2                 :             : // Copyright (C) 2019 Luxoft Sweden AB
       3                 :             : // Copyright (C) 2018 Pelagicore AG
       4                 :             : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
       5                 :             : 
       6                 :             : #ifndef SUDO_H
       7                 :             : #define SUDO_H
       8                 :             : 
       9                 :             : #include <QtCore/QString>
      10                 :             : #include <QtCore/QByteArray>
      11                 :             : #include <QtCore/QMutex>
      12                 :             : #include <qplatformdefs.h>
      13                 :             : 
      14                 :             : #ifdef Q_OS_UNIX
      15                 :             : #  include <sys/types.h>
      16                 :             : #else
      17                 :             : typedef uint uid_t;
      18                 :             : typedef uint gid_t;
      19                 :             : //typedef uint mode_t; // already typedef'ed in qplatformdefs.h
      20                 :             : #endif
      21                 :             : 
      22                 :             : #include <QtAppManCommon/global.h>
      23                 :             : 
      24                 :             : QT_BEGIN_NAMESPACE_AM
      25                 :             : 
      26                 :             : class Sudo
      27                 :             : {
      28                 :             : public:
      29                 :             :     enum DropPrivileges {
      30                 :             :         DropPrivilegesPermanently,
      31                 :             :         DropPrivilegesRegainable, // only use this for auto-tests
      32                 :             :     };
      33                 :             : 
      34                 :             :     static void forkServer(DropPrivileges dropPrivileges) noexcept(false);
      35                 :             : };
      36                 :             : 
      37                 :             : class SudoInterface
      38                 :             : {
      39                 :             : public:
      40                 :             :     virtual ~SudoInterface() = default;
      41                 :             : 
      42                 :             :     virtual bool removeRecursive(const QString &fileOrDir) = 0;
      43                 :             :     virtual bool setOwnerAndPermissionsRecursive(const QString &fileOrDir, uid_t user, gid_t group, mode_t permissions) = 0;
      44                 :             :     virtual bool bindMountFileSystem(const QString &from, const QString &to, bool readOnly, quint64 namespacePid = 0) = 0;
      45                 :             : 
      46                 :             : protected:
      47                 :             :     enum MessageType { Request, Reply };
      48                 :             : 
      49                 :             : #ifdef Q_OS_LINUX
      50                 :             :     QByteArray receiveMessage(int socket, MessageType type, QString *errorString);
      51                 :             :     bool sendMessage(int socket, const QByteArray &msg, MessageType type, const QString &errorString = QString());
      52                 :             : #endif
      53                 :             :     QByteArray receive(const QByteArray &packet);
      54                 :             : 
      55                 :             : protected:
      56                 :             :     SudoInterface();
      57                 :             : private:
      58                 :             :     Q_DISABLE_COPY_MOVE(SudoInterface)
      59                 :             : };
      60                 :             : 
      61                 :             : class SudoServer;
      62                 :             : 
      63                 :             : class SudoClient : public SudoInterface
      64                 :             : {
      65                 :             : public:
      66                 :             :     static SudoClient *createInstance(int socketFd, SudoServer *shortCircuit = nullptr);
      67                 :             : 
      68                 :             :     static SudoClient *instance();
      69                 :             : 
      70                 :             :     bool isFallbackImplementation() const;
      71                 :             : 
      72                 :             :     bool removeRecursive(const QString &fileOrDir) override;
      73                 :             :     bool setOwnerAndPermissionsRecursive(const QString &fileOrDir, uid_t user, gid_t group, mode_t permissions) override;
      74                 :             :     bool bindMountFileSystem(const QString &from, const QString &to, bool readOnly, quint64 namespacePid) override;
      75                 :             : 
      76                 :             :     void stopServer();
      77                 :             : 
      78         [ #  # ]:           0 :     QString lastError() const { return m_errorString; }
      79                 :             : 
      80                 :             : private:
      81                 :             :     SudoClient(int socketFd);
      82                 :             : 
      83                 :             :     QByteArray call(const QByteArray &msg);
      84                 :             : 
      85                 :             :     int m_socket;
      86                 :             :     QString m_errorString;
      87                 :             :     QMutex m_mutex;
      88                 :             :     SudoServer *m_shortCircuit = nullptr;
      89                 :             : 
      90                 :             :     static SudoClient *s_instance;
      91                 :             : };
      92                 :             : 
      93                 :             : class SudoServer : public SudoInterface
      94                 :             : {
      95                 :             : public:
      96                 :             :     static SudoServer *createInstance(int socketFd);
      97                 :             : 
      98                 :             :     static SudoServer *instance();
      99                 :             : 
     100                 :             :     bool removeRecursive(const QString &fileOrDir) override;
     101                 :             :     bool setOwnerAndPermissionsRecursive(const QString &fileOrDir, uid_t user, gid_t group, mode_t permissions) override;
     102                 :             :     bool bindMountFileSystem(const QString &from, const QString &to, bool readOnly, quint64 namespacePid) override;
     103                 :             : 
     104                 :          43 :     QString lastError() const { return m_errorString; }
     105                 :             : 
     106                 :             :     Q_NORETURN void run();
     107                 :             : 
     108                 :             : private:
     109                 :             :     SudoServer(int socketFd);
     110                 :             : 
     111                 :             :     QByteArray receive(const QByteArray &msg);
     112                 :             :     friend class SudoClient;
     113                 :             : 
     114                 :             :     int m_socket;
     115                 :             :     QString m_errorString;
     116                 :             :     bool m_stop = false;
     117                 :             : 
     118                 :             :     static SudoServer *s_instance;
     119                 :             : };
     120                 :             : 
     121                 :             : QT_END_NAMESPACE_AM
     122                 :             : 
     123                 :             : #endif // SUDO_H
        

Generated by: LCOV version 2.0-1