Sandboxie/SandboxiePlus/QSbieAPI/SbieAPI.h

314 lines
11 KiB
C
Raw Permalink Normal View History

2021-10-16 16:19:51 +01:00
/*
*
2023-05-27 08:03:42 +01:00
* Copyright (c) 2020-2023, David Xanatos
2021-10-16 16:19:51 +01:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QThread>
#include <QFileSystemWatcher>
#include "qsbieapi_global.h"
#include "SbieStatus.h"
#include "SbieTrace.h"
#include "./Sandboxie/SandBox.h"
#include "./Sandboxie/BoxedProcess.h"
class QSBIEAPI_EXPORT CSbieAPI : public QThread
{
Q_OBJECT
public:
CSbieAPI(QObject* parent = 0);
virtual ~CSbieAPI();
static bool IsSbieCtrlRunning();
static bool TerminateSbieCtrl();
2022-01-30 08:41:19 +00:00
virtual SB_STATUS Connect(bool takeOver, bool withQueue);
2021-10-16 16:19:51 +01:00
virtual SB_STATUS Disconnect();
virtual bool IsConnected() const;
2022-06-27 07:33:15 +01:00
static bool IsWow64();
2021-10-16 16:19:51 +01:00
virtual QString GetVersion();
virtual SB_STATUS TakeOver();
2022-05-16 20:30:40 +01:00
virtual SB_STATUS WatchIni(bool bEnable = true, bool bReLoad = true);
2021-10-16 16:19:51 +01:00
virtual QString GetSbiePath() const { return m_SbiePath; }
virtual QString GetIniPath() const { return m_IniPath; }
2022-05-08 14:26:59 +01:00
virtual QString ResolveAbsolutePath(const QString& Path);
2021-10-16 16:19:51 +01:00
virtual void UpdateDriveLetters();
virtual QString Nt2DosPath(QString NtPath, bool* pOk = NULL) const;
2023-10-19 17:42:39 +01:00
static quint32 GetVolumeSN(const wchar_t* path, std::wstring* pLabel = NULL);
2021-10-16 16:19:51 +01:00
2022-05-16 20:30:40 +01:00
virtual SB_STATUS ReloadBoxes(bool bForceUpdate = false);
2021-10-16 16:19:51 +01:00
static SB_STATUS ValidateName(const QString& BoxName);
2023-07-01 17:54:53 +01:00
virtual QString MkNewName(QString Name);
2021-10-16 16:19:51 +01:00
virtual SB_STATUS CreateBox(const QString& BoxName, bool bReLoad = true);
2023-07-01 17:54:53 +01:00
virtual SB_STATUS UpdateProcesses(int iKeep, bool bAllSessions);
2021-10-16 16:19:51 +01:00
virtual QMap<QString, CSandBoxPtr> GetAllBoxes() { return m_SandBoxes; }
virtual QMap<quint32, CBoxedProcessPtr> GetAllProcesses() { return m_BoxedProxesses; }
virtual CSandBoxPtr GetBoxByProcessId(quint32 ProcessId) const;
virtual CSandBoxPtr GetBoxByName(const QString &BoxName) const { return m_SandBoxes.value(BoxName.toLower()); }
virtual CBoxedProcessPtr GetProcessById(quint32 ProcessId) const;
2023-10-22 12:33:08 +01:00
virtual SB_STATUS TerminateAll(bool bNoExceptions = false);
2021-10-16 16:19:51 +01:00
virtual SB_STATUS SetProcessExemption(quint32 process_id, quint32 action_id, bool NewState);
virtual bool GetProcessExemption(quint32 process_id, quint32 action_id);
virtual QString GetBoxedPath(const QString& BoxName, const QString& Path);
2023-01-22 14:16:31 +00:00
virtual QString GetBoxedPath(CSandBox* pBox, const QString& Path, const QString& Snapshot = QString());
2022-05-19 17:12:09 +01:00
virtual QString GetRealPath(CSandBox* pBox, const QString& Path);
2021-10-16 16:19:51 +01:00
enum ESetMode
{
eIniUpdate = 0,
eIniAppend,
eIniInsert,
eIniDelete
};
// Config
virtual SB_STATUS ReloadConfig(bool ReconfigureDrv = false);
virtual SB_STATUS ReloadCert();
virtual void CommitIniChanges();
virtual QString SbieIniGet(const QString& Section, const QString& Setting, quint32 Index = 0, qint32* ErrCode = NULL);
2023-04-13 18:46:51 +01:00
virtual QString SbieIniGet2(const QString& Section, const QString& Setting, quint32 Index = 0, bool bWithGlobal = false, bool bNoExpand = true, bool withTemplates = false);
2021-10-16 16:19:51 +01:00
virtual QString SbieIniGetEx(const QString& Section, const QString& Setting);
virtual SB_STATUS SbieIniSet(const QString& Section, const QString& Setting, const QString& Value, ESetMode Mode = eIniUpdate, bool bRefresh = true);
virtual bool IsBox(const QString& BoxName, bool& bIsEnabled);
2023-07-01 17:54:53 +01:00
virtual QSharedPointer<CSbieIni> GetGlobalSettings() const { return m_pGlobalSection; }
virtual QSharedPointer<CSbieIni> GetUserSettings() const { return m_pUserSection; }
2021-10-16 16:19:51 +01:00
virtual QString GetCurrentUserName() const { return m_UserName; }
virtual QString GetCurrentUserSid() const { return m_UserSid; }
virtual bool IsConfigLocked();
virtual SB_STATUS UnlockConfig(const QString& Password);
virtual SB_STATUS LockConfig(const QString& NewPassword);
virtual void ClearPassword();
2023-08-15 08:49:03 +01:00
virtual bool GetDriverInfo(quint32 InfoClass, void* pBuffer, size_t Size);
2021-11-13 08:28:32 +00:00
enum EFeatureFlags
{
eSbieFeatureWFP = 0x00000001,
eSbieFeatureObCB = 0x00000002,
eSbieFeaturePMod = 0x00000004,
eSbieFeatureAppC = 0x00000008,
eSbieFeatureSbiL = 0x00000010,
2022-08-09 17:19:46 +01:00
eSbieFeatureARM64 = 0x40000000,
2021-11-13 08:28:32 +00:00
eSbieFeatureCert = 0x80000000
};
2021-10-16 16:19:51 +01:00
virtual quint32 GetFeatureFlags();
virtual QString GetFeatureStr();
2024-02-10 16:52:00 +00:00
virtual int IsDyndataActive();
2021-10-16 16:19:51 +01:00
// Forced Processes
virtual SB_STATUS DisableForceProcess(bool Set, int Seconds = 0);
virtual bool AreForceProcessDisabled();
2023-08-24 17:39:00 +01:00
// Mount Manager
virtual SB_STATUS ImBoxCreate(CSandBox* pBox, quint64 uSizeKb, const QString& Password = QString());
virtual SB_STATUS ImBoxMount(CSandBox* pBox, const QString& Password = QString(), bool bProtect = false, bool bAutoUnmount = false);
virtual SB_STATUS ImBoxUnmount(CSandBox* pBox);
virtual SB_RESULT(QStringList) ImBoxEnum();
virtual SB_RESULT(QVariantMap) ImBoxQuery(const QString& Root = QString());
//virtual SB_STATUS ImBoxUpdate( // todo
2021-10-16 16:19:51 +01:00
// Monitor
virtual SB_STATUS EnableMonitor(bool Enable);
virtual bool IsMonitoring();
2023-01-07 15:57:55 +00:00
virtual const QVector<CTraceEntryPtr>& GetTrace();
virtual int GetTraceCount() const { return m_TraceList.count(); }
virtual void ClearTrace() { m_TraceList.clear(); QMutexLocker Lock(&m_TraceMutex); m_TraceCache.clear(); }
2021-10-16 16:19:51 +01:00
// Other
2023-01-28 19:57:20 +00:00
virtual quint64 QueryProcessInfo(quint32 ProcessId, quint32 InfoClass = 0);
2021-10-16 16:19:51 +01:00
virtual QString GetSbieMsgStr(quint32 code, quint32 Lang = 1033);
2023-07-01 17:54:53 +01:00
virtual SB_RESULT(quint32) RunStart(const QString& BoxName, const QString& Command, bool Elevated = false, const QString& WorkingDir = QString(), QProcess* pProcess = NULL);
2021-10-16 16:19:51 +01:00
virtual QString GetStartPath() const;
virtual quint32 GetSessionID() const;
2022-08-09 17:19:46 +01:00
virtual SB_STATUS SetSecureParam(const QString& Name, const void* data, size_t size);
2023-10-19 17:42:39 +01:00
virtual SB_STATUS GetSecureParam(const QString& Name, void* data, size_t size, quint32* size_out = NULL, bool bVerify = false);
2022-08-09 17:19:46 +01:00
2021-10-16 16:19:51 +01:00
enum ESbieQueuedRequests
{
ePrintSpooler = -1,
eInvalidQueuedRequests = 0,
eFileMigration = 1,
2022-08-09 17:19:46 +01:00
eInetBlockade = 2,
2021-10-16 16:19:51 +01:00
};
2022-08-09 17:19:46 +01:00
void LoadEventLog();
2022-11-20 16:23:15 +00:00
virtual SB_RESULT(int) RunUpdateUtility(const QStringList& Params, quint32 Elevate = 0, bool Wait = false);
2021-10-16 16:19:51 +01:00
public slots:
virtual void SendReplyData(quint32 RequestId, const QVariantMap& Result);
signals:
void StatusChanged();
void ConfigReloaded();
//void LogMessage(const QString& Message, bool bNotify = true);
void LogSbieMessage(quint32 MsgCode, const QStringList& MsgData, quint32 ProcessId);
2023-04-13 18:46:51 +01:00
void ProcessBoxed(quint32 ProcessId, const QString& Path, const QString& Box, quint32 ParentId, const QString& CmdLine);
2021-10-16 16:19:51 +01:00
void FileToRecover(const QString& BoxName, const QString& FilePath, const QString& BoxPath, quint32 ProcessId);
2022-06-13 19:12:53 +01:00
void BoxAdded(const CSandBoxPtr& pBox);
void BoxOpened(const CSandBoxPtr& pBox);
void BoxClosed(const CSandBoxPtr& pBox);
void BoxRemoved(const CSandBoxPtr& pBox);
2021-10-16 16:19:51 +01:00
void NotAuthorized(bool bLoginRequired, bool &bRetry);
void QueuedRequest(quint32 ClientPid, quint32 ClientTid, quint32 RequestId, const QVariantMap& Data);
protected slots:
virtual void OnIniChanged(const QString &path);
virtual void OnReloadConfig();
2023-04-13 18:46:51 +01:00
virtual CBoxedProcessPtr OnProcessBoxed(quint32 ProcessId, const QString& Path, const QString& Box, quint32 ParentId, const QString& CmdLine);
2021-10-16 16:19:51 +01:00
protected:
friend class CSandBox;
friend class CBoxedProcess;
virtual SB_STATUS ReloadConf(quint32 flags, quint32 SessionId = -1);
virtual CSandBox* NewSandBox(const QString& BoxName, class CSbieAPI* pAPI);
virtual CBoxedProcess* NewBoxedProcess(quint32 ProcessId, class CSandBox* pBox);
virtual QString GetSbieHome() const;
virtual QString GetIniPath(bool* IsHome) const;
virtual QString GetUserSection(QString* pUserName = NULL, bool* pIsAdmin = NULL) const;
virtual bool HasProcesses(const QString& BoxName);
virtual bool GetQueue();
virtual bool GetLog();
virtual bool GetMonitor();
virtual SB_STATUS TerminateAll(const QString& BoxName);
virtual SB_STATUS Terminate(quint32 ProcessId);
2023-11-27 18:14:16 +00:00
virtual SB_STATUS SetSuspendedAll(const QString& BoxName, bool bSuspended);
virtual SB_STATUS SetSuspended(quint32 ProcessId, bool bSuspended);
2021-10-16 16:19:51 +01:00
virtual SB_STATUS RunSandboxed(const QString& BoxName, const QString& Command, QString WrkDir = QString(), quint32 Flags = 0);
2023-07-01 17:54:53 +01:00
virtual SB_STATUS UpdateBoxPaths(CSandBox* pSandBox);
2021-10-16 16:19:51 +01:00
virtual SB_STATUS UpdateProcessInfo(const CBoxedProcessPtr& pProcess);
2023-11-27 18:14:16 +00:00
virtual SB_STATUS GetProcessInfo(quint32 ProcessId, quint32* pParentId = NULL, quint32* pInfo = NULL, bool* pSuspended = NULL, QString* pImagePath = NULL, QString* pCommandLine = NULL, QString* pWorkingDir = NULL);
2021-10-16 16:19:51 +01:00
virtual void GetUserPaths();
virtual QString GetDeviceMap();
virtual QByteArray MakeEnvironment(bool AddDeviceMap);
virtual void run();
QMap<QString, CSandBoxPtr> m_SandBoxes;
QMap<quint32, CBoxedProcessPtr> m_BoxedProxesses;
2023-01-07 15:57:55 +00:00
mutable QMutex m_TraceMutex;
QVector<CTraceEntryPtr> m_TraceCache;
2021-10-16 16:19:51 +01:00
QVector<CTraceEntryPtr> m_TraceList;
mutable QReadWriteLock m_DriveLettersMutex;
struct SDrive
{
QString Letter;
QString NtPath;
enum EType
{
EVolume = 0,
EShare
} Type;
QString Aux;
};
QMap<QString, SDrive> m_DriveLetters;
QString m_SbiePath;
QString m_IniPath;
QFileSystemWatcher m_IniWatcher;
2022-05-16 20:30:40 +01:00
bool m_IniReLoad;
2021-10-16 16:19:51 +01:00
bool m_bReloadPending;
2022-05-16 20:30:40 +01:00
bool m_bBoxesDirty;
2021-10-16 16:19:51 +01:00
bool m_bWithQueue;
bool m_bTerminate;
2023-07-01 17:54:53 +01:00
QSharedPointer<CSbieIni>m_pGlobalSection;
QSharedPointer<CSbieIni>m_pUserSection;
2021-10-16 16:19:51 +01:00
QString m_UserName;
QString m_UserSid;
QString m_ProgramDataDir;
QString m_PublicDir;
QString m_UserDir;
2023-05-27 08:03:42 +01:00
public:
struct SScopedVoid {
~SScopedVoid() { if (ptr) free(ptr); }
inline void Assign(void* p) {Q_ASSERT(!ptr); ptr = p;}
protected:
SScopedVoid(void* p) : ptr(p) {}
void* ptr;
};
template <typename T>
struct SScoped : public SScopedVoid {
SScoped(void* p = NULL) : SScopedVoid(p) {}
inline T* Detache() {T* p = ptr; ptr = NULL; return p;}
inline T* Value() {return (T*)ptr;}
inline T* operator->() const {return (T*)ptr;}
inline T& operator*() const {return *((T*)ptr);}
inline operator T*() const {return (T*)ptr;}
private:
SScoped(const SScoped& other) {} // copying is explicitly forbidden
SScoped<T>& operator=(T* p) {return *this;}
SScoped<T>& operator=(const SScoped<T>& other) {return *this;}
};
2021-10-16 16:19:51 +01:00
private:
mutable QMutex m_ThreadMutex;
mutable QWaitCondition m_ThreadWait;
2023-05-27 08:03:42 +01:00
SB_STATUS CallServer(void* req, SScopedVoid* prpl) const;
2021-10-16 16:19:51 +01:00
SB_STATUS SbieIniSet(void *RequestBuf, void *pPasswordWithinRequestBuf, const QString& SectionName, const QString& SettingName);
struct SSbieAPI* m;
};