Sandboxie/SandboxiePlus/SandMan/SbiePlusAPI.cpp

1250 lines
33 KiB
C++
Raw Normal View History

2021-10-16 16:19:51 +01:00
#include "stdafx.h"
#include "SbiePlusAPI.h"
2022-02-05 14:09:53 +00:00
#include "SbieProcess.h"
#include "SandMan.h"
2021-10-16 16:19:51 +01:00
#include "..\MiscHelpers\Common\Common.h"
#include <windows.h>
2022-09-29 17:28:48 +01:00
#include <Shlobj_core.h>
2022-05-16 20:30:40 +01:00
#include "BoxMonitor.h"
2022-09-29 17:28:48 +01:00
#include "..\MiscHelpers\Common\OtherFunctions.h"
#include "../QSbieAPI/SbieUtils.h"
2022-11-12 09:45:35 +00:00
#include "../MiscHelpers/Archive/Archive.h"
#include <QtConcurrent>
2023-01-29 16:10:11 +00:00
#include "Helpers/WinHelper.h"
2021-10-16 16:19:51 +01:00
CSbiePlusAPI::CSbiePlusAPI(QObject* parent) : CSbieAPI(parent)
{
2022-05-16 20:30:40 +01:00
m_BoxMonitor = new CBoxMonitor();
2022-02-18 19:09:35 +00:00
m_JobCount = 0;
2021-10-16 16:19:51 +01:00
}
CSbiePlusAPI::~CSbiePlusAPI()
{
2022-05-16 20:30:40 +01:00
delete m_BoxMonitor;
2021-10-16 16:19:51 +01:00
}
CSandBox* CSbiePlusAPI::NewSandBox(const QString& BoxName, class CSbieAPI* pAPI)
{
return new CSandBoxPlus(BoxName, pAPI);
}
CBoxedProcess* CSbiePlusAPI::NewBoxedProcess(quint32 ProcessId, class CSandBox* pBox)
{
return new CSbieProcess(ProcessId, pBox);
}
2023-04-13 18:46:51 +01:00
CBoxedProcessPtr CSbiePlusAPI::OnProcessBoxed(quint32 ProcessId, const QString& Path, const QString& Box, quint32 ParentId, const QString& CmdLine)
2021-10-16 16:19:51 +01:00
{
2023-04-13 18:46:51 +01:00
CBoxedProcessPtr pProcess = CSbieAPI::OnProcessBoxed(ProcessId, Path, Box, ParentId, CmdLine);
2021-10-16 16:19:51 +01:00
if (!pProcess.isNull() && pProcess->GetFileName().indexOf(theAPI->GetSbiePath(), 0, Qt::CaseInsensitive) != 0) {
CSandBoxPlus* pBox = pProcess.objectCast<CSbieProcess>()->GetBox();
pBox->m_RecentPrograms.insert(pProcess->GetProcessName());
}
return pProcess;
}
BOOL CALLBACK CSbiePlusAPI__WindowEnum(HWND hwnd, LPARAM lParam)
{
if (GetParent(hwnd) || GetWindow(hwnd, GW_OWNER))
return TRUE;
ULONG style = GetWindowLong(hwnd, GWL_STYLE);
if ((style & (WS_CAPTION | WS_SYSMENU)) != (WS_CAPTION | WS_SYSMENU))
return TRUE;
if (!IsWindowVisible(hwnd))
return TRUE;
/*
if ((style & WS_OVERLAPPEDWINDOW) != WS_OVERLAPPEDWINDOW &&
(style & WS_POPUPWINDOW) != WS_POPUPWINDOW)
return TRUE;
*/
ULONG pid;
GetWindowThreadProcessId(hwnd, &pid);
QMultiMap<quint32, QString>& m_WindowMap = *((QMultiMap<quint32, QString>*)(lParam));
WCHAR title[256];
GetWindowTextW(hwnd, title, 256);
m_WindowMap.insert(pid, QString::fromWCharArray(title));
return TRUE;
}
void CSbiePlusAPI::UpdateWindowMap()
{
m_WindowMap.clear();
EnumWindows(CSbiePlusAPI__WindowEnum, (LPARAM)&m_WindowMap);
}
bool CSbiePlusAPI::IsRunningAsAdmin()
{
if (m_UserSid.left(9) != "S-1-5-21-")
return false;
if (m_UserSid.right(4) != "-500")
return false;
return true;
}
2022-06-13 19:12:53 +01:00
void CSbiePlusAPI::StopMonitor()
{
m_BoxMonitor->Stop();
}
2023-07-01 17:54:53 +01:00
SB_RESULT(quint32) CSbiePlusAPI::RunStart(const QString& BoxName, const QString& Command, bool Elevated, const QString& WorkingDir, QProcess* pProcess)
2022-08-09 17:19:46 +01:00
{
if (!pProcess)
pProcess = new QProcess(this);
2023-07-01 17:54:53 +01:00
SB_RESULT(quint32) Status = CSbieAPI::RunStart(BoxName, Command, Elevated, WorkingDir, pProcess);
2022-08-09 17:19:46 +01:00
if (pProcess->parent() == this) {
if (!Status.IsError()) {
connect(pProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(OnStartFinished()));
m_PendingStarts.insert(pProcess->processId());
2023-07-01 17:54:53 +01:00
return Status;
2022-08-09 17:19:46 +01:00
}
delete pProcess;
}
return Status;
}
void CSbiePlusAPI::OnStartFinished()
{
QProcess* pProcess = (QProcess*)sender();
m_PendingStarts.remove(pProcess->processId());
pProcess->deleteLater();
}
2021-10-16 16:19:51 +01:00
///////////////////////////////////////////////////////////////////////////////
// CSandBoxPlus
//
CSandBoxPlus::CSandBoxPlus(const QString& BoxName, class CSbieAPI* pAPI) : CSandBox(BoxName, pAPI)
{
m_bINetBlocked = false;
2023-05-21 13:48:58 +01:00
m_bINetExceptions = false;
2021-10-16 16:19:51 +01:00
m_bSharesAllowed = false;
m_bDropRights = false;
2021-11-13 08:28:32 +00:00
m_bSecurityEnhanced = false;
m_bPrivacyEnhanced = false;
m_bApplicationCompartment = false;
2021-10-16 16:19:51 +01:00
m_iUnsecureDebugging = 0;
2022-08-27 19:55:35 +01:00
m_bRootAccessOpen = false;
2021-10-16 16:19:51 +01:00
2022-05-16 20:30:40 +01:00
m_TotalSize = theConf->GetValue("SizeCache/" + m_Name, -1).toLongLong();
2021-10-16 16:19:51 +01:00
m_SuspendRecovery = false;
2022-05-16 20:30:40 +01:00
m_IsEmpty = false;
2021-10-16 16:19:51 +01:00
m_pOptionsWnd = NULL;
m_pRecoveryWnd = NULL;
2022-08-10 19:14:37 +01:00
m_BoxType = eDefault;
2022-11-07 11:06:15 +00:00
m_BoxDel = false;
2023-04-29 10:55:43 +01:00
m_NoForce = false;
2022-08-10 19:14:37 +01:00
m_BoxColor = QColor(Qt::yellow).rgb();
2021-10-16 16:19:51 +01:00
}
CSandBoxPlus::~CSandBoxPlus()
{
}
2022-11-12 09:45:35 +00:00
class QFileX : public QFile {
public:
QFileX(const QString& path, const CSbieProgressPtr& pProgress, CArchive* pArchive) : QFile(path)
{
m_pProgress = pProgress;
m_pArchive = pArchive;
}
bool open(OpenMode flags) override
{
if (m_pProgress->IsCanceled())
return false;
m_pProgress->ShowMessage(Split2(fileName(), "/", true).second);
m_pProgress->SetProgress((int)(m_pArchive->GetProgress() * 100.0));
return QFile::open(flags);
}
2023-01-11 20:07:58 +00:00
qint64 size() const override
{
qint64 Size = QFile::size();
if (QFileInfo(fileName()).isShortcut())
{
QFile File(fileName());
if (File.open(QFile::ReadOnly))
Size = File.size();
File.close();
}
return Size;
}
2022-11-12 09:45:35 +00:00
protected:
CSbieProgressPtr m_pProgress;
CArchive* m_pArchive;
};
void CSandBoxPlus::ExportBoxAsync(const CSbieProgressPtr& pProgress, const QString& ExportPath, const QString& RootPath, const QString& Section)
{
//CArchive Archive(ExportPath + ".tmp");
CArchive Archive(ExportPath);
QMap<int, QIODevice*> Files;
//QTemporaryFile* pConfigFile = new QTemporaryFile;
//pConfigFile->open();
//pConfigFile->write(Section.toUtf8());
//pConfigFile->close();
//
//int ArcIndex = Archive.AddFile("BoxConfig.ini");
//Files.insert(ArcIndex, pConfigFile);
QFile File(RootPath + "\\" + "BoxConfig.ini");
if (File.open(QFile::WriteOnly)) {
File.write(Section.toUtf8());
File.close();
}
QStringList FileList = ListDir(RootPath + "\\");
foreach(const QString& File, FileList)
{
StrPair RootName = Split2(File, "\\", true);
if(RootName.second.isEmpty())
{
RootName.second = RootName.first;
RootName.first = "";
}
int ArcIndex = Archive.AddFile(RootName.second);
if(ArcIndex != -1)
{
QString FileName = (RootName.first.isEmpty() ? RootPath + "\\" : RootName.first) + RootName.second;
Files.insert(ArcIndex, new QFileX(FileName, pProgress, &Archive));
}
//else
// this file is already present in the archive, this should not happen !!!
}
SB_STATUS Status = SB_OK;
2023-07-31 20:20:29 +01:00
if (!Archive.Update(&Files, true, theConf->GetInt("Options/ExportCompression", 3))) // 0, 1 - 9
2022-11-12 09:45:35 +00:00
Status = SB_ERR((ESbieMsgCodes)SBX_7zCreateFailed);
//if(!Status.IsError() && !pProgress->IsCanceled())
// QFile::rename(ExportPath + ".tmp", ExportPath);
//else
// QFile::remove(ExportPath + ".tmp");
File.remove();
pProgress->Finish(Status);
}
SB_PROGRESS CSandBoxPlus::ExportBox(const QString& FileName)
{
if (!CArchive::IsInit())
return SB_ERR((ESbieMsgCodes)SBX_7zNotReady);
if (theAPI->HasProcesses(m_Name))
return SB_ERR(SB_SnapIsRunning); // todo
if (!IsInitialized())
return SB_ERR(SB_SnapIsEmpty); // todo
QString Section = theAPI->SbieIniGetEx(m_Name, "");
CSbieProgressPtr pProgress = CSbieProgressPtr(new CSbieProgress());
QtConcurrent::run(CSandBoxPlus::ExportBoxAsync, pProgress, FileName, m_FilePath, Section);
return SB_PROGRESS(OP_ASYNC, pProgress);
}
void CSandBoxPlus::ImportBoxAsync(const CSbieProgressPtr& pProgress, const QString& ImportPath, const QString& RootPath, const QString& BoxName)
{
CArchive Archive(ImportPath);
2023-07-31 20:20:29 +01:00
if (Archive.Open() != ERR_7Z_OK) {
2022-11-12 09:45:35 +00:00
pProgress->Finish(SB_ERR((ESbieMsgCodes)SBX_7zOpenFailed));
return;
}
bool IsBoxArchive = false;
QMap<int, QIODevice*> Files;
for (int i = 0; i < Archive.FileCount(); i++) {
int ArcIndex = Archive.FindByIndex(i);
if(Archive.FileProperty(ArcIndex, "IsDir").toBool())
continue;
QString File = Archive.FileProperty(ArcIndex, "Path").toString();
if (File == "BoxConfig.ini")
IsBoxArchive = true;
Files.insert(ArcIndex, new QFileX(CArchive::PrepareExtraction(File, RootPath + "\\"), pProgress, &Archive));
}
if(!IsBoxArchive) {
pProgress->Finish(SB_ERR((ESbieMsgCodes)SBX_NotBoxArchive));
return;
}
SB_STATUS Status = SB_OK;
if (!Archive.Extract(&Files))
Status = SB_ERR((ESbieMsgCodes)SBX_7zExtractFailed);
if (!Status.IsError() && !pProgress->IsCanceled())
{
QFile File(RootPath + "\\" + "BoxConfig.ini");
if (File.open(QFile::ReadOnly)) {
QMetaObject::invokeMethod(theAPI, "SbieIniSetSection", Qt::BlockingQueuedConnection, // run this in the main thread
Q_ARG(QString, BoxName),
Q_ARG(QString, QString::fromUtf8(File.readAll()))
);
File.close();
}
File.remove();
}
pProgress->Finish(Status);
}
SB_PROGRESS CSandBoxPlus::ImportBox(const QString& FileName)
{
if (!CArchive::IsInit())
return SB_ERR((ESbieMsgCodes)SBX_7zNotReady);
CSbieProgressPtr pProgress = CSbieProgressPtr(new CSbieProgress());
QtConcurrent::run(CSandBoxPlus::ImportBoxAsync, pProgress, FileName, m_FilePath, m_Name);
return SB_PROGRESS(OP_ASYNC, pProgress);
}
2021-10-16 16:19:51 +01:00
void CSandBoxPlus::UpdateDetails()
{
m_bINetBlocked = false;
2022-08-27 19:55:35 +01:00
foreach(const QString& Entry, GetTextList("ClosedFilePath", false)) {
2021-10-20 14:06:52 +01:00
if (Entry == "!<InternetAccess>,InternetAccessDevices") {
2021-10-16 16:19:51 +01:00
m_bINetBlocked = true;
break;
}
}
2023-05-21 13:48:58 +01:00
if (theGUI->IsWFPEnabled()) {
foreach(const QString & Entry, GetTextList("AllowNetworkAccess", false)) {
if (Entry == "!<InternetAccess>,n") {
m_bINetBlocked = true;
break;
}
}
}
if (m_bINetBlocked) {
foreach(const QString& Entry, GetTextList("ProcessGroup", true)) {
StrPair NameList = Split2(Entry, ",");
if (NameList.first == "<InternetAccess>" && !NameList.second.isEmpty()) {
m_bINetExceptions = true;
break;
}
2021-10-20 14:06:52 +01:00
}
}
2021-10-16 16:19:51 +01:00
m_bSharesAllowed = GetBool("BlockNetworkFiles", true) == false;
m_bDropRights = GetBool("DropAdminRights", false);
2022-08-27 19:55:35 +01:00
m_bRootAccessOpen = false;
2022-09-29 17:28:48 +01:00
foreach(const QString& Setting, QString("OpenFilePath|OpenKeyPath|OpenPipePath|OpenConfPath").split("|")) {
2022-08-27 19:55:35 +01:00
foreach(const QString& Entry, GetTextList(Setting, false)) {
if (Entry == "*" || Entry == "\\") {
m_bRootAccessOpen = true;
break;
}
}
}
2021-10-16 16:19:51 +01:00
if (CheckUnsecureConfig())
m_iUnsecureDebugging = 1;
else if(GetBool("ExposeBoxedSystem", false) || GetBool("UnrestrictedSCM", false) /*|| GetBool("RunServicesAsSystem", false)*/)
m_iUnsecureDebugging = 2;
else
m_iUnsecureDebugging = 0;
//GetBool("SandboxieLogon", false)
2022-08-09 17:19:46 +01:00
m_bSecurityEnhanced = m_iUnsecureDebugging == 0 && (GetBool("UseSecurityMode", false));
2021-11-13 08:28:32 +00:00
m_bApplicationCompartment = GetBool("NoSecurityIsolation", false);
2022-09-29 17:28:48 +01:00
m_bPrivacyEnhanced = !m_bRootAccessOpen && (m_iUnsecureDebugging != 1 || m_bApplicationCompartment) && (GetBool("UsePrivacyMode", false)); // app compartments are inhenrently insecure
2021-10-16 16:19:51 +01:00
CSandBox::UpdateDetails();
2022-07-10 17:28:10 +01:00
m_BoxType = GetTypeImpl();
2022-11-07 11:06:15 +00:00
m_BoxDel = GetBool("AutoDelete", false);
2023-04-29 10:55:43 +01:00
m_NoForce = GetBool("DisableForceRules", false);
2022-11-07 11:06:15 +00:00
2022-07-10 17:28:10 +01:00
QStringList BorderCfg = GetText("BorderColor").split(",");
m_BoxColor = QColor("#" + BorderCfg[0].mid(5, 2) + BorderCfg[0].mid(3, 2) + BorderCfg[0].mid(1, 2)).rgb();
2021-10-16 16:19:51 +01:00
}
2022-09-29 17:28:48 +01:00
bool CSandBoxPlus::IsBoxexPath(const QString& Path)
{
return Path.left(m_FilePath.length()).compare(m_FilePath, Qt::CaseInsensitive) == 0;
}
2023-02-03 07:00:11 +00:00
bool CSandBoxPlus::IsFileDeleted(const QString& RealPath, const QString& Snapshot, const QStringList& SnapshotList, const QMap<QString, QList<QString>>& DeletedPaths)
2022-09-29 17:28:48 +01:00
{
2023-02-03 07:00:11 +00:00
int NextIndex = SnapshotList.indexOf(Snapshot) - 1;
2023-01-29 22:50:56 +00:00
if (NextIndex < 0) return false; // no newer snapshot
QString NewerSnapshot = SnapshotList.at(NextIndex);
2023-01-30 07:42:47 +00:00
foreach(const QString &DeletedPath, DeletedPaths[NewerSnapshot]) {
int len = DeletedPath.length();
if (RealPath.length() >= len && RealPath.left(len).compare(DeletedPath, Qt::CaseInsensitive) == 0) {
if (RealPath.length() == len || RealPath[len] == '\\')
return true;
}
}
2022-09-29 17:28:48 +01:00
2023-01-30 07:42:47 +00:00
return IsFileDeleted(RealPath, NewerSnapshot, SnapshotList, DeletedPaths);
2023-01-29 22:50:56 +00:00
}
void CSandBoxPlus::ScanStartMenu()
{
2023-01-22 14:16:31 +00:00
QStringList SnapshotList;
SnapshotList.append("");
QString CurSnapshot;
QMap<QString, SBoxSnapshot> Snapshots = GetSnapshots(&CurSnapshot);
while(!CurSnapshot.isEmpty()) {
SnapshotList.append(CurSnapshot);
CurSnapshot = Snapshots[CurSnapshot].Parent;
}
2023-01-30 07:42:47 +00:00
QMap<QString, QList<QString>> DeletedPaths;
2023-01-29 22:50:56 +00:00
foreach (const QString& Snapshot, SnapshotList)
{
QString PathsFile = GetFileRoot();
if (!Snapshot.isEmpty())
PathsFile += "\\snapshot-" + Snapshot;
PathsFile += "\\FilePaths.dat";
QFile File(PathsFile);
if (File.open(QFile::ReadOnly)) {
QByteArray Data = File.readAll();
File.close();
QString Text = QString::fromWCharArray((const wchar_t*)Data.constData(), Data.size()/sizeof(wchar_t));
2023-01-30 07:42:47 +00:00
QList<QString> Deleted;
2023-01-29 22:50:56 +00:00
QTextStream in(&Text);
while (!in.atEnd()) {
QStringList Line = in.readLine().split("|");
if (Line.length() < 2 || Line[1] != "1")
continue; // not a delete entry
2023-01-30 07:42:47 +00:00
Deleted.append(theAPI->Nt2DosPath(Line[0]));
2023-01-29 22:50:56 +00:00
}
2023-01-30 07:42:47 +00:00
DeletedPaths[Snapshot] = Deleted;
2023-01-29 22:50:56 +00:00
}
}
QMap<QString, SFoundLink> FoundLinks;
2022-09-29 17:28:48 +01:00
int csidls[] = { CSIDL_DESKTOPDIRECTORY, CSIDL_COMMON_DESKTOPDIRECTORY, CSIDL_STARTMENU, CSIDL_COMMON_STARTMENU };
for (int i = 0; i < ARRAYSIZE(csidls); i++)
{
WCHAR path[2048];
if (SHGetFolderPath(NULL, csidls[i], NULL, SHGFP_TYPE_CURRENT, path) != S_OK)
continue;
2023-01-22 14:16:31 +00:00
foreach (const QString& Snapshot, SnapshotList)
2022-09-29 17:28:48 +01:00
{
2023-01-22 14:16:31 +00:00
QString BoxPath = theAPI->GetBoxedPath(this, QString::fromWCharArray(path), Snapshot);
2023-08-04 16:47:12 +01:00
QStringList Files = ListDir(BoxPath, QStringList() << "*.lnk" << "*.url", i >= 2); // no subdir scan for desktop as people like to put junk there
2023-01-22 14:16:31 +00:00
foreach(QString File, Files)
{
2023-01-29 22:50:56 +00:00
QString RealPath = theAPI->GetRealPath(this, BoxPath + "\\" + QString(File).replace("/", "\\"));
2023-01-22 14:16:31 +00:00
2023-01-29 22:50:56 +00:00
// we scan the snapshots newest to oldest
if (FoundLinks.contains(RealPath.toLower()))
continue;
// check if the shortcut file is marked deleted
2023-01-30 07:42:47 +00:00
if (IsFileDeleted(RealPath, Snapshot, SnapshotList, DeletedPaths))
2023-01-22 14:16:31 +00:00
continue;
2023-01-29 22:50:56 +00:00
SFoundLink FoundLink;
FoundLink.Snapshot = Snapshot;
FoundLink.LinkPath = BoxPath + "\\" + QString(File).replace("/", "\\");
FoundLink.RealPath = RealPath;
FoundLink.SubPath = (i >= 2 ? "" : "Desktop/") + File;
FoundLinks.insert(FoundLink.RealPath.toLower(), FoundLink);
2023-01-22 14:16:31 +00:00
}
2022-09-29 17:28:48 +01:00
}
}
2023-01-29 22:50:56 +00:00
bool bChanged = false;
auto OldStartMenu = ListToSet(m_StartMenu.keys());
foreach (const SFoundLink& FoundLink, FoundLinks) {
if (!OldStartMenu.remove(FoundLink.SubPath.toLower()))
bChanged = true;
2023-08-04 16:47:12 +01:00
SLink* pLink = NULL;
2023-01-29 22:50:56 +00:00
StrPair PathName = Split2(FoundLink.SubPath, "/", true);
StrPair NameExt = Split2(PathName.second, ".", true);
2023-08-04 16:47:12 +01:00
if (NameExt.second.toLower() == "lnk")
{
QVariantMap Link = ResolveShortcut(FoundLink.LinkPath);
if (!Link.contains("Path"))
continue;
pLink = &m_StartMenu[FoundLink.SubPath.toLower()];
pLink->Folder = PathName.first;
pLink->Name = NameExt.first;
if (!pLink->Target.isEmpty() && pLink->Target != Link["Path"].toString())
bChanged = true;
pLink->Target = Link["Path"].toString();
2023-08-12 08:15:17 +01:00
if(pLink->Target.mid(1,2) != ":\\")
pLink->Url = true;
2023-08-04 16:47:12 +01:00
pLink->Arguments = Link["Arguments"].toString();
pLink->Icon = Link["IconPath"].toString();
pLink->IconIndex = Link["IconIndex"].toInt();
pLink->WorkDir = Link["WorkingDir"].toString();
}
else if (NameExt.second.toLower() == "url")
{
std::wstring urlFile = FoundLink.LinkPath.toStdWString();
auto ReadIni = [&urlFile](const wchar_t* Section, const wchar_t* Key) {
wchar_t strBuffer[0x1000];
DWORD Read = GetPrivateProfileStringW(Section, Key, L"", strBuffer, ARRSIZE(strBuffer), urlFile.c_str());
return QString::fromWCharArray(strBuffer, Read);
};
QString Url = ReadIni(L"InternetShortcut", L"URL");
if (Url.isEmpty())
continue;
pLink = &m_StartMenu[FoundLink.SubPath.toLower()];
pLink->Folder = PathName.first;
pLink->Name = NameExt.first;
if (!pLink->Target.isEmpty() && pLink->Target != Url)
bChanged = true;
pLink->Url = true;
pLink->Target = Url;
//pLink->Arguments = ;
pLink->Icon = ReadIni(L"InternetShortcut", L"IconFile");
pLink->IconIndex = ReadIni(L"InternetShortcut", L"IconIndex").toInt();
2023-08-11 21:26:02 +01:00
if (pLink->Icon.isEmpty())
pLink->IconIndex = -1;
2023-08-04 16:47:12 +01:00
//pLink->WorkDir = ;
}
2023-01-29 22:50:56 +00:00
2023-08-08 20:06:26 +01:00
if (pLink) {
if (!pLink->Url && !pLink->Target.isEmpty() && !QFile::exists(pLink->Target) && !IsBoxexPath(pLink->Target))
pLink->Target = theAPI->GetBoxedPath(this, pLink->Target, FoundLink.Snapshot);
if (!pLink->WorkDir.isEmpty() && !QFile::exists(pLink->WorkDir) && !IsBoxexPath(pLink->WorkDir))
pLink->WorkDir = theAPI->GetBoxedPath(this, pLink->WorkDir, FoundLink.Snapshot);
if (!pLink->Icon.isEmpty() && !QFile::exists(pLink->Icon) && !IsBoxexPath(pLink->Icon))
pLink->Icon = theAPI->GetBoxedPath(this, pLink->Icon, FoundLink.Snapshot);
}
2023-01-29 22:50:56 +00:00
}
2022-09-29 17:28:48 +01:00
foreach(const QString &Path, OldStartMenu)
m_StartMenu.remove(Path);
2023-01-29 16:10:11 +00:00
if (bChanged || !OldStartMenu.isEmpty())
2022-09-29 17:28:48 +01:00
emit StartMenuChanged();
}
2022-05-16 20:30:40 +01:00
void CSandBoxPlus::SetBoxPaths(const QString& FilePath, const QString& RegPath, const QString& IpcPath)
{
2022-06-13 19:12:53 +01:00
bool bPathChanged = (FilePath != m_FilePath);
if (bPathChanged && !m_FilePath.isEmpty())
((CSbiePlusAPI*)theAPI)->m_BoxMonitor->RemoveBox(this);
2022-05-16 20:30:40 +01:00
CSandBox::SetBoxPaths(FilePath, RegPath, IpcPath);
2022-06-13 19:12:53 +01:00
if (m_FilePath.isEmpty()) {
m_IsEmpty = true;
return;
}
2023-07-01 17:54:53 +01:00
if(bPathChanged)
UpdateSize(false);
2022-09-29 17:28:48 +01:00
if (theConf->GetBool("Options/ScanStartMenu", true))
ScanStartMenu();
2022-05-16 20:30:40 +01:00
}
2023-07-01 17:54:53 +01:00
void CSandBoxPlus::UpdateSize(bool bReset)
2022-05-16 20:30:40 +01:00
{
2023-07-01 17:54:53 +01:00
if(bReset)
m_TotalSize = -1;
2022-05-16 20:30:40 +01:00
m_IsEmpty = IsEmpty();
2023-07-01 17:54:53 +01:00
if(theConf->GetBool("Options/WatchBoxSize", false) && m_TotalSize == -1)
((CSbiePlusAPI*)theAPI)->m_BoxMonitor->ScanBox(this);
2022-05-16 20:30:40 +01:00
}
void CSandBoxPlus::SetSize(quint64 Size)
{
m_TotalSize = Size;
theConf->SetValue("SizeCache/" + m_Name, Size);
}
2022-06-13 19:12:53 +01:00
bool CSandBoxPlus::IsSizePending() const
{
return ((CSbiePlusAPI*)theAPI)->m_BoxMonitor->IsScanPending(this);
}
2022-05-16 20:30:40 +01:00
void CSandBoxPlus::OpenBox()
{
CSandBox::OpenBox();
m_IsEmpty = false;
if (theConf->GetBool("Options/WatchBoxSize", false))
2022-06-13 19:12:53 +01:00
((CSbiePlusAPI*)theAPI)->m_BoxMonitor->WatchBox(this);
2022-05-16 20:30:40 +01:00
}
2021-10-16 16:19:51 +01:00
void CSandBoxPlus::CloseBox()
{
CSandBox::CloseBox();
m_SuspendRecovery = false;
2022-05-16 20:30:40 +01:00
((CSbiePlusAPI*)theAPI)->m_BoxMonitor->CloseBox(this);
2022-09-29 17:28:48 +01:00
if (theConf->GetBool("Options/ScanStartMenu", true))
ScanStartMenu();
2022-05-16 20:30:40 +01:00
}
2023-07-01 17:54:53 +01:00
SB_STATUS CSandBoxPlus::RenameBox(const QString& NewName)
{
if (GetBool("IsShadow"))
return RenameSection(NewName);
BeginModifyingBox();
SB_STATUS Status = CSandBox::RenameBox(NewName);
ConnectEndSlot(Status);
return Status;
}
SB_STATUS CSandBoxPlus::RemoveBox()
{
if (GetBool("IsShadow"))
return RemoveSection();
return CSandBox::RemoveBox();
}
2023-01-29 16:10:11 +00:00
void CSandBoxPlus::ConnectEndSlot(const SB_PROGRESS& Status)
2022-05-16 20:30:40 +01:00
{
2023-01-29 16:10:11 +00:00
CSbieProgressPtr pProgress = Status.GetValue();
if (!pProgress.isNull())
connect(pProgress.data(), SIGNAL(Finished()), this, SLOT(EndModifyingBox()));
else
EndModifyingBox();
2022-11-05 13:42:23 +00:00
}
2023-01-29 16:10:11 +00:00
void CSandBoxPlus::BeginModifyingBox()
2022-11-05 13:42:23 +00:00
{
2023-01-29 16:10:11 +00:00
//
// before altering the box structure or deleting it
// we need to ensure that sandman will not hold any open handles on the files/folders inside
//
2022-11-05 13:42:23 +00:00
2023-01-29 16:10:11 +00:00
((CSbiePlusAPI*)theAPI)->m_BoxMonitor->RemoveBox(this);
emit AboutToBeModified();
}
2022-11-05 13:42:23 +00:00
2023-01-29 16:10:11 +00:00
void CSandBoxPlus::EndModifyingBox()
{
UpdateSize();
if (theConf->GetBool("Options/ScanStartMenu", true))
ScanStartMenu();
2021-10-16 16:19:51 +01:00
}
2023-07-01 17:54:53 +01:00
bool CSandBoxPlus::IsEmpty() const
{
if (!CSandBox::IsEmpty())
return false;
return true;
}
2021-11-13 08:28:32 +00:00
bool CSandBoxPlus::CheckUnsecureConfig() const
{
2022-08-10 19:14:37 +01:00
//if (GetBool("UnsafeTemplate", false, true, true)) return true;
if (GetBool("OriginalToken", false, true, true)) return true;
if (GetBool("OpenToken", false, true, true)) return true;
if(GetBool("UnrestrictedToken", false, true, true)) return true;
if (GetBool("KeepTokenIntegrity", false, true, true)) return true;
if (GetBool("UnstrippedToken", false, true, true)) return true;
if (GetBool("KeepUserGroup", false, true, true)) return true;
if (!GetBool("AnonymousLogon", true, true, true)) return true;
if(GetBool("UnfilteredToken", false, true, true)) return true;
if (GetBool("DisableFileFilter", false, true, true)) return true;
if (GetBool("DisableKeyFilter", false, true, true)) return true;
if (GetBool("DisableObjectFilter", false, true, true)) return true;
if (GetBool("StripSystemPrivileges", false, true, true)) return true;
2021-11-13 08:28:32 +00:00
return false;
}
2021-10-16 16:19:51 +01:00
QString CSandBoxPlus::GetStatusStr() const
{
if (!m_IsEnabled)
return tr("Disabled");
2022-02-05 14:09:53 +00:00
if (!m_StatusStr.isEmpty())
return m_StatusStr;
2021-10-16 16:19:51 +01:00
QStringList Status;
2022-06-13 19:12:53 +01:00
//if (m_IsEmpty)
// Status.append(tr("Empty"));
2021-10-16 16:19:51 +01:00
2022-08-27 19:55:35 +01:00
if (m_bRootAccessOpen)
Status.append(tr("OPEN Root Access"));
else if (m_bApplicationCompartment)
2021-11-13 08:28:32 +00:00
Status.append(tr("Application Compartment"));
else if (m_iUnsecureDebugging == 1)
Status.append(tr("NOT SECURE"));
2021-10-16 16:19:51 +01:00
else if (m_iUnsecureDebugging == 2)
Status.append(tr("Reduced Isolation"));
2021-11-13 08:28:32 +00:00
else if(m_bSecurityEnhanced)
2021-10-16 16:19:51 +01:00
Status.append(tr("Enhanced Isolation"));
2021-11-13 08:28:32 +00:00
if(m_bPrivacyEnhanced)
Status.append(tr("Privacy Enhanced"));
2021-10-16 16:19:51 +01:00
2023-05-21 13:48:58 +01:00
if (m_bINetBlocked) {
if(m_bINetExceptions)
Status.append(tr("No INet (with Exceptions)"));
else
Status.append(tr("No INet"));
}
2021-10-16 16:19:51 +01:00
if (m_bSharesAllowed)
Status.append(tr("Net Share"));
2021-11-13 08:28:32 +00:00
if (m_bDropRights && !m_bSecurityEnhanced)
2021-10-16 16:19:51 +01:00
Status.append(tr("No Admin"));
2022-12-20 09:12:35 +00:00
if(m_BoxDel)
Status.append(tr("Auto Delete"));
2021-10-16 16:19:51 +01:00
if (Status.isEmpty())
return tr("Normal");
return Status.join(", ");
}
2022-07-10 17:28:10 +01:00
CSandBoxPlus::EBoxTypes CSandBoxPlus::GetTypeImpl() const
2021-10-16 16:19:51 +01:00
{
2022-08-27 19:55:35 +01:00
if (m_bRootAccessOpen)
return eOpen;
2021-11-13 08:28:32 +00:00
if (m_bApplicationCompartment && m_bPrivacyEnhanced)
return eAppBoxPlus;
if (m_bApplicationCompartment)
return eAppBox;
if (m_iUnsecureDebugging != 0)
return eInsecure;
if (m_bSecurityEnhanced && m_bPrivacyEnhanced)
return eHardenedPlus;
if (m_bSecurityEnhanced)
return eHardened;
if (m_bPrivacyEnhanced)
return eDefaultPlus;
return eDefault;
2021-10-16 16:19:51 +01:00
}
void CSandBoxPlus::SetINetBlock(bool bEnable)
{
2023-05-21 13:48:58 +01:00
if (bEnable) {
if(theGUI->IsWFPEnabled())
InsertText("AllowNetworkAccess", "!<InternetAccess>,n");
else
InsertText("ClosedFilePath", "!<InternetAccess>,InternetAccessDevices");
}
2021-10-16 16:19:51 +01:00
else
{
foreach(const QString& Entry, GetTextList("ClosedFilePath", false))
{
if (Entry.contains("InternetAccessDevices"))
DelValue("ClosedFilePath", Entry);
}
2023-05-21 13:48:58 +01:00
foreach(const QString& Entry, GetTextList("AllowNetworkAccess", false))
{
if (Entry.contains("!<InternetAccess>,n"))
DelValue("AllowNetworkAccess", Entry);
}
2021-10-16 16:19:51 +01:00
}
}
void CSandBoxPlus::SetAllowShares(bool bEnable)
{
2022-11-29 15:49:16 +00:00
SetBoolSafe("BlockNetworkFiles", !bEnable);
2021-10-16 16:19:51 +01:00
}
void CSandBoxPlus::SetDropRights(bool bEnable)
{
2022-11-29 15:49:16 +00:00
SetBoolSafe("DropAdminRights", bEnable);
2021-10-16 16:19:51 +01:00
}
QStringList::iterator FindInStrList(QStringList& list, const QString& str)
{
QStringList::iterator J = list.begin();
for (; J != list.end(); ++J)
{
if (J->compare(str, Qt::CaseInsensitive) == 0)
break;
}
return J;
}
void SetInStrList(QStringList& list, const QString& str, bool bSet)
{
if (bSet)
list.append(str);
else
{
for (QStringList::iterator J = list.begin(); J != list.end();)
{
if (J->compare(str, Qt::CaseInsensitive) == 0) {
J = list.erase(J);
continue;
}
++J;
}
}
}
bool CSandBoxPlus::TestProgramGroup(const QString& Group, const QString& ProgName)
{
QStringList ProcessGroups = GetTextList("ProcessGroup", false);
foreach(const QString & ProcessGroup, ProcessGroups)
{
StrPair GroupPaths = Split2(ProcessGroup, ",");
if (GroupPaths.first.compare(Group, Qt::CaseInsensitive) == 0)
{
QStringList Programs = SplitStr(GroupPaths.second, ",");
return FindInStrList(Programs, ProgName) != Programs.end();
}
}
return false;
}
void CSandBoxPlus::EditProgramGroup(const QString& Group, const QString& ProgName, bool bSet)
{
QStringList ProcessGroups = GetTextList("ProcessGroup", false);
QStringList Programs;
QStringList::iterator I = ProcessGroups.begin();
for (; I != ProcessGroups.end(); ++I)
{
StrPair GroupPaths = Split2(*I, ",");
if (GroupPaths.first.compare(Group, Qt::CaseInsensitive) == 0)
{
Programs = SplitStr(GroupPaths.second, ",");
break;
}
}
if (I == ProcessGroups.end())
I = ProcessGroups.insert(I, "");
SetInStrList(Programs, ProgName, bSet);
*I = Group + "," + Programs.join(",");
UpdateTextList("ProcessGroup", ProcessGroups, false);
}
void CSandBoxPlus::BlockProgram(const QString& ProgName)
{
bool WhiteList = false;
bool BlackList = false;
foreach(const QString& Entry, GetTextList("ClosedIpcPath", false))
{
StrPair ProgPath = Split2(Entry, ",");
if (ProgPath.second != "*")
continue;
if (ProgPath.first.compare("!<StartRunAccess>", Qt::CaseInsensitive) == 0)
WhiteList = true;
else if (ProgPath.first.compare("<StartRunAccess>", Qt::CaseInsensitive) == 0)
BlackList = true;
else
continue;
break;
}
if (!WhiteList && !BlackList)
{
BlackList = true;
InsertText("ClosedIpcPath", "<StartRunAccess>,*");
}
EditProgramGroup("<StartRunAccess>", ProgName, !WhiteList);
}
void CSandBoxPlus::SetInternetAccess(const QString& ProgName, bool bSet)
{
EditProgramGroup("<InternetAccess>", ProgName, bSet);
}
bool CSandBoxPlus::HasInternetAccess(const QString& ProgName)
{
return TestProgramGroup("<InternetAccess>", ProgName);
}
void CSandBoxPlus::SetForcedProgram(const QString& ProgName, bool bSet)
{
QStringList Programs = GetTextList("ForceProcess", false);
SetInStrList(Programs, ProgName, bSet);
UpdateTextList("ForceProcess", Programs, false);
}
bool CSandBoxPlus::IsForcedProgram(const QString& ProgName)
{
QStringList Programs = GetTextList("ForceProcess", false);
return FindInStrList(Programs, ProgName) != Programs.end();
}
void CSandBoxPlus::SetLingeringProgram(const QString& ProgName, bool bSet)
{
QStringList Programs = GetTextList("LingerProcess", false);
SetInStrList(Programs, ProgName, bSet);
UpdateTextList("LingerProcess", Programs, false);
}
int CSandBoxPlus::IsLingeringProgram(const QString& ProgName)
{
QStringList Programs = GetTextList("LingerProcess", false);
if (FindInStrList(Programs, ProgName) != Programs.end())
return 1;
Programs = GetTextList("LingerProcess", true);
if (FindInStrList(Programs, ProgName) != Programs.end())
return 2;
return 0;
}
void CSandBoxPlus::SetLeaderProgram(const QString& ProgName, bool bSet)
{
QStringList Programs = GetTextList("LeaderProcess", false);
SetInStrList(Programs, ProgName, bSet);
UpdateTextList("LeaderProcess", Programs, false);
}
int CSandBoxPlus::IsLeaderProgram(const QString& ProgName)
{
QStringList Programs = GetTextList("LeaderProcess", false);
return FindInStrList(Programs, ProgName) != Programs.end() ? 1 : 0;
}
2023-02-03 07:00:11 +00:00
SB_STATUS CSandBoxPlus::DeleteContentAsync(bool DeleteSnapshots, bool bOnAutoDelete)
2022-02-05 14:09:53 +00:00
{
if (GetBool("NeverDelete", false))
return SB_ERR(SB_DeleteProtect);
2021-10-16 16:19:51 +01:00
2022-02-05 14:09:53 +00:00
SB_STATUS Status = TerminateAll();
if (Status.IsError())
return Status;
m_ActiveProcessCount = 0; // to ensure CleanBox will be happy
foreach(const QString& Command, GetTextList("OnBoxDelete", true, false, true)) {
CBoxJob* pJob = new COnDeleteJob(this, Expand(Command));
AddJobToQueue(pJob);
2021-10-16 16:19:51 +01:00
}
2022-02-05 14:09:53 +00:00
2023-02-03 07:00:11 +00:00
CBoxJob* pJob = new CCleanUpJob(this, DeleteSnapshots, bOnAutoDelete);
2022-02-05 14:09:53 +00:00
AddJobToQueue(pJob);
return SB_OK;
2021-10-16 16:19:51 +01:00
}
2022-02-05 14:09:53 +00:00
void CSandBoxPlus::AddJobToQueue(CBoxJob* pJob)
2021-10-16 16:19:51 +01:00
{
2022-02-18 19:09:35 +00:00
theAPI->m_JobCount++;
2022-02-05 14:09:53 +00:00
m_JobQueue.append(QSharedPointer<CBoxJob>(pJob));
if (m_JobQueue.count() == 1)
StartNextJob();
}
void CSandBoxPlus::StartNextJob()
{
next:
Q_ASSERT(m_JobQueue.count() > 0);
Q_ASSERT(m_JobQueue.first()->GetProgress().isNull());
QSharedPointer<CBoxJob> pJob = m_JobQueue.first();
SB_PROGRESS Status = pJob->Start();
if (Status.GetStatus() == OP_ASYNC)
{
m_StatusStr = pJob->GetDescription();
CSbieProgressPtr pProgress = Status.GetValue();
connect(pProgress.data(), SIGNAL(Message(const QString&)), this, SLOT(OnAsyncMessage(const QString&)));
connect(pProgress.data(), SIGNAL(Progress(int)), this, SLOT(OnAsyncProgress(int)));
connect(pProgress.data(), SIGNAL(Finished()), this, SLOT(OnAsyncFinished()));
2021-10-16 16:19:51 +01:00
}
2022-02-05 14:09:53 +00:00
else
{
m_JobQueue.removeFirst();
2022-02-18 19:09:35 +00:00
theAPI->m_JobCount--;
2022-02-05 14:09:53 +00:00
if (Status.IsError()) {
2023-05-21 08:38:49 +01:00
theAPI->m_JobCount -= m_JobQueue.count();
2022-02-05 14:09:53 +00:00
m_JobQueue.clear();
2023-07-01 17:54:53 +01:00
theGUI->CheckResults(QList<SB_STATUS>() << Status, theGUI);
2022-02-05 14:09:53 +00:00
return;
}
if (!m_JobQueue.isEmpty())
goto next;
}
}
void CSandBoxPlus::OnAsyncFinished()
{
Q_ASSERT(m_JobQueue.count() > 0);
Q_ASSERT(!m_JobQueue.first()->GetProgress().isNull());
2021-10-16 16:19:51 +01:00
2022-02-05 14:09:53 +00:00
m_StatusStr.clear();
2021-10-16 16:19:51 +01:00
2022-02-05 14:09:53 +00:00
QSharedPointer<CBoxJob> pJob = m_JobQueue.takeFirst();
2022-02-18 19:09:35 +00:00
theAPI->m_JobCount--;
2022-02-05 14:09:53 +00:00
CSbieProgressPtr pProgress = pJob->GetProgress();
if (pProgress->IsCanceled()) {
2023-05-21 08:38:49 +01:00
theAPI->m_JobCount -= m_JobQueue.count();
2022-02-05 14:09:53 +00:00
m_JobQueue.clear();
return;
}
2021-10-16 16:19:51 +01:00
2022-02-05 14:09:53 +00:00
SB_STATUS Status = pProgress->GetStatus();
if (Status.IsError()) {
2023-05-21 08:38:49 +01:00
theAPI->m_JobCount -= m_JobQueue.count();
2022-02-05 14:09:53 +00:00
m_JobQueue.clear();
2023-07-01 17:54:53 +01:00
theGUI->CheckResults(QList<SB_STATUS>() << Status, theGUI, true);
2022-02-05 14:09:53 +00:00
return;
2021-10-16 16:19:51 +01:00
}
2023-01-29 09:49:41 +00:00
else
pJob->Finished();
2021-10-16 16:19:51 +01:00
2022-02-05 14:09:53 +00:00
if (!m_JobQueue.isEmpty())
StartNextJob();
2021-10-16 16:19:51 +01:00
}
2022-02-05 14:09:53 +00:00
void CSandBoxPlus::OnAsyncMessage(const QString& Text)
{
m_StatusStr = Text;
}
void CSandBoxPlus::OnAsyncProgress(int Progress)
{
}
void CSandBoxPlus::OnCancelAsync()
{
if (m_JobQueue.isEmpty())
return;
Q_ASSERT(!m_JobQueue.first()->GetProgress().isNull());
QSharedPointer<CBoxJob> pJob = m_JobQueue.first();
CSbieProgressPtr pProgress = pJob->GetProgress();
pProgress->Cancel();
2022-06-13 19:49:53 +01:00
}
2022-09-29 17:28:48 +01:00
2023-05-21 18:23:43 +01:00
QString CSandBoxPlus::MakeBoxCommand(const QString& FileName)
{
QString BoxFileName = FileName;
//if (BoxFileName.indexOf(m_FilePath, Qt::CaseInsensitive) == 0) {
// BoxFileName.remove(0, m_FilePath.length());
// if (BoxFileName.at(0) != '\\')
// BoxFileName.prepend('\\');
//}
return BoxFileName.replace(m_FilePath, "%BoxRoot%", Qt::CaseInsensitive);
}
2022-09-29 17:28:48 +01:00
QString CSandBoxPlus::GetCommandFile(const QString& Command)
{
QString Path = Command;
if (Path.left(1) == "\"") {
int End = Path.indexOf("\"", 1);
if (End != -1) Path = Path.mid(1, End - 1);
}
else {
int End = Path.indexOf(" ");
if (End != -1) Path.truncate(End);
}
2023-05-21 18:23:43 +01:00
//if (Path.left(1) == "\\")
// Path.prepend(m_FilePath);
return Path.replace("%BoxRoot%", m_FilePath, Qt::CaseInsensitive);
2023-01-11 20:07:58 +00:00
}
2023-05-21 18:23:43 +01:00
QString CSandBoxPlus::GetFullCommand(const QString& Command)
{
QString FullCmd = Command;
//if(FullCmd.left(1) == "\\")
// FullCmd.prepend(m_FilePath);
//else if(FullCmd.left(2) == "\"\\")
// FullCmd.insert(1, m_FilePath);
return FullCmd.replace("%BoxRoot%", m_FilePath, Qt::CaseInsensitive);
2023-07-01 17:54:53 +01:00
}
2023-07-02 17:09:18 +01:00
///////////////////////////////////////////////////////////////////////////////
// CSbieTemplatesEx
//
2023-08-15 08:49:03 +01:00
//#include <ntstatus.h>
2023-07-02 17:09:18 +01:00
#define WIN32_NO_STATUS
typedef long NTSTATUS;
#include <Windows.h>
//#include <Winternl.h>
#include "..\..\Sandboxie\common\win32_ntddk.h"
#define MAX_KEY_NAME 255
#define MAX_VALUE_NAME 16383
#define MAX_VALUE_DATA 1024000
#include <comdef.h>
#include <wuapi.h>
void CSbieTemplatesEx::CollectUpdates()
{
IUpdateSession* updateSession = NULL;
IUpdateSearcher* updateSearcher = NULL;
ISearchResult* searchResult = NULL;
IUpdateCollection* updates = NULL;
HRESULT res;
res = CoInitializeEx(NULL, 0);
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to initialize COM"));
goto cleanup;
}
res = CoCreateInstance(CLSID_UpdateSession, NULL, CLSCTX_INPROC_SERVER, IID_IUpdateSession, (LPVOID*)&updateSession);
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to create update session"));
goto cleanup;
}
res = updateSession->CreateUpdateSearcher(&updateSearcher);
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to create update searcher"));
goto cleanup;
}
res = updateSearcher->put_IncludePotentiallySupersededUpdates(VARIANT_TRUE);
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to set search options"));
goto cleanup;
}
{BSTR criteria = SysAllocString(L"IsInstalled=1"); // or IsHidden=1
res = updateSearcher->Search(criteria, &searchResult);
SysFreeString(criteria); }
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to search for updates"));
goto cleanup;
}
res = searchResult->get_Updates(&updates);
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to retrieve update list from search result"));
goto cleanup;
}
LONG updateCount;
res = updates->get_Count(&updateCount);
if (FAILED(res)) {
theGUI->OnLogMessage(tr("Failed to get update count"));
goto cleanup;
}
for (LONG i = 0L; i < updateCount; ++i)
{
QVariantMap entry;
IUpdate* update = NULL;
res = updates->get_Item(i, &update);
if (!FAILED(res))
{
IStringCollection* updateKBIDs = NULL;
res = update->get_KBArticleIDs(&updateKBIDs);
if (SUCCEEDED(res)) {
LONG kbIDCount;
res = updateKBIDs->get_Count(&kbIDCount);
if (SUCCEEDED(res)) {
QVariantList kb;
for (LONG j = 0L; j < kbIDCount; ++j) {
BSTR kbID;
res = updateKBIDs->get_Item(j, &kbID);
if (FAILED(res))
continue;
kb.append("KB" + QString::fromWCharArray(kbID));
SysFreeString(kbID);
}
entry["kb"] = kb;
}
updateKBIDs->Release();
}
BSTR updateTitle;
res = update->get_Title(&updateTitle);
if (!FAILED(res)) {
entry["title"] = QString::fromWCharArray(updateTitle);
SysFreeString(updateTitle);
}
update->Release();
}
m_Updates.append(entry);
}
cleanup:
if (updates != NULL) updates->Release();
if (searchResult != NULL) searchResult->Release();
if (updateSearcher != NULL) updateSearcher->Release();
if (updateSession != NULL) updateSession->Release();
CoUninitialize();
}
void CSbieTemplatesEx::Reset()
{
CSbieTemplates::Reset();
m_Updates.clear();
}
QList<QVariantMap> CSbieTemplatesEx::GetUpdates()
{
if (m_Updates.isEmpty())
CollectUpdates();
return m_Updates;
}
bool CSbieTemplatesEx::CheckUpdates(const QString& value)
{
if (m_Updates.isEmpty())
CollectUpdates();
auto I = std::find_if(m_Updates.begin(), m_Updates.end(), [value](const QVariantMap& cur)->bool {
return cur["kb"].toStringList().contains(value, Qt::CaseInsensitive);
});
bool bRet = (I != m_Updates.end());
return bRet;
}