Files

157 lines
4.0 KiB
C++
Raw Permalink Normal View History

2021-05-19 12:08:57 +02:00
#ifndef MESSAGE_H
#define MESSAGE_H
#include <memory>
2021-08-14 22:00:42 +02:00
#include <string>
2025-08-19 22:17:36 +02:00
#include <cstdint>
2021-08-14 22:00:42 +02:00
struct MountMessage {
enum Type {
Remount,
2024-02-15 23:39:04 +01:00
Umount,
CreateTmpFs // notice, path goes in 'dst'
2021-08-14 22:00:42 +02:00
};
Type type;
std::string src;
std::string dst;
};
2021-05-19 12:08:57 +02:00
2024-02-16 16:54:09 +01:00
struct CopyMessage {
std::string from;
std::string to;
};
2024-02-20 19:14:25 +01:00
struct DBusMapping {
std::string from;
std::string to;
// there are different mapping rules to be applied.
bool systemBus = false;
};
2024-02-15 21:33:28 +01:00
/**
* This class is really a 'run-request', disguised as a message.
* The message holds all the relevant information for a single application to be started,
* and can be serialized over the wire (a unix pipe) to the priviledged part of
* the server so that Runner can start the app in the right context.
2024-02-15 21:41:26 +01:00
*
* The message is created by using the builder design-pattern.
* The setters setPath(), setHomedir(), addArgument(),
* addRemount() etc are all simply appending instructions to
* the message. Order is relevant as the target will execute
* the instructions in-order.
*
* The reader uses the Message::Iterator() for reading purposes.
2024-02-15 21:33:28 +01:00
*/
2021-05-19 12:08:57 +02:00
class Message
{
public:
2024-02-15 23:39:04 +01:00
static constexpr int MAX_SIZE = 4096;
2021-05-19 12:08:57 +02:00
Message() = delete;
Message(int size);
explicit Message(char *buffer, int bufferSize);
Message(const Message &other) = default;
2021-08-14 22:00:42 +02:00
/// Sets path to the executable to be run.
2021-05-19 12:08:57 +02:00
void setPath(const std::string &path);
2021-08-14 22:00:42 +02:00
char *path() const;
2021-05-19 12:08:57 +02:00
2021-08-14 22:00:42 +02:00
/// Append an argument to the path() executable
2021-05-21 15:28:57 +02:00
void addArgument(const char *string);
2021-05-19 12:08:57 +02:00
Message &operator=(const Message &other) = default;
2024-02-15 21:41:26 +01:00
/// debug method (is a no-op unless NDEBUG is not defined)
2021-08-14 17:13:41 +02:00
void printFields() const;
2021-05-19 12:08:57 +02:00
2024-02-15 21:41:26 +01:00
/// raw getter of message-data
2021-05-19 12:08:57 +02:00
char *begin() const;
2024-02-15 21:41:26 +01:00
/// returns message size
2021-05-19 12:08:57 +02:00
int size() const;
2024-02-15 23:39:04 +01:00
void setTry(bool isTry);
2024-02-21 11:21:37 +01:00
void setJailId(uint32_t jailId);
2024-02-15 23:39:04 +01:00
2021-08-14 23:26:47 +02:00
void addRemount(const std::string &source, const std::string &destination);
2021-08-14 22:00:42 +02:00
void addUmountPoint(const std::string &dir);
2024-02-15 23:39:04 +01:00
void addMountTmpDir(const std::string &dir);
2024-02-16 16:54:09 +01:00
void addCopy(const std::string &from, const std::string &to);
2024-02-24 11:40:42 +01:00
void addInitSript(const std::string &text);
2021-08-14 22:00:42 +02:00
2024-02-17 01:03:43 +01:00
void addEnvToUnset(const std::string &propertyName);
void addEnvToSet(const std::string &envVar);
2024-05-17 11:39:44 +02:00
void setJailPassword(const std::string &pwd);
2024-02-17 01:03:43 +01:00
2024-02-20 19:14:25 +01:00
enum DBusType {
UserSessionBus,
SystemBus
};
void addDBusProxy(DBusType type, const std::string &from, const std::string &to);
2026-04-11 14:54:32 +02:00
void setVpnConfig(const std::string &configPath);
void setHasVpnPassFile(bool on);
2021-08-14 22:00:42 +02:00
class Iterator {
public:
explicit Iterator(const Message * const message);
bool isArgument() const;
2024-02-17 01:03:43 +01:00
bool isNewEnvVar() const;
bool isEnvVarUnset() const;
2021-08-14 22:00:42 +02:00
bool isUnmount() const;
bool isRemount() const;
2024-02-15 23:39:04 +01:00
bool isCreateTmp() const;
2024-02-16 16:54:09 +01:00
bool isCopy() const;
2024-02-21 11:21:37 +01:00
bool isJailId() const;
2024-02-24 11:40:42 +01:00
bool isInitSript() const;
2024-05-17 11:39:44 +02:00
bool isJailPwd() const;
2021-08-14 22:00:42 +02:00
bool isValid() const;
2024-02-15 23:39:04 +01:00
bool isTry() const {
return m_isTry;
}
2024-02-20 19:14:25 +01:00
bool isDBusMapping() const;
2026-04-11 14:54:32 +02:00
bool boolData() const;
2021-08-14 22:00:42 +02:00
2024-02-16 16:54:09 +01:00
MountMessage mountData() const;
const char *argument() const;
CopyMessage copyData() const;
2021-08-14 22:00:42 +02:00
2024-02-20 19:14:25 +01:00
DBusMapping dbusMapping() const;
2026-04-11 14:54:32 +02:00
bool isVpnConfig() const;
bool isVpnPwdBool() const;
2024-02-21 11:21:37 +01:00
uint32_t jailId() const;
2024-02-17 01:03:43 +01:00
char *stringPtr() const;
int stringLength() const;
2024-02-16 09:00:15 +01:00
bool next();
2021-08-14 22:00:42 +02:00
private:
void checkAvail(int bytes) const;
const Message *m_parent;
2024-02-17 01:03:43 +01:00
char *m_cur;
2021-08-14 22:00:42 +02:00
int m_recordSize;
2024-02-15 23:39:04 +01:00
bool m_isTry = false;
2021-08-14 22:00:42 +02:00
};
Iterator iBegin() const {
return Iterator(this);
2021-05-21 15:28:57 +02:00
}
2021-05-19 12:08:57 +02:00
private:
void addString(char type, const std::string &string);
2024-02-15 23:39:04 +01:00
void addTag(char type);
2021-05-19 12:08:57 +02:00
char *m_path = nullptr;
std::shared_ptr<char> m_buf;
char *m_writePtr = nullptr;
int m_reservedSize = 0;
2021-05-21 15:28:57 +02:00
const char *m_firstArgument = nullptr;
2021-05-19 12:08:57 +02:00
};
#endif