2021-10-16 16:19:51 +01:00
# include "stdafx.h"
# include "OptionsWindow.h"
# include "SandMan.h"
# include "SettingsWindow.h"
# include "../MiscHelpers/Common/Settings.h"
# include "../MiscHelpers/Common/Common.h"
# include "../MiscHelpers/Common/ComboInputDialog.h"
# include "../MiscHelpers/Common/SettingsWidgets.h"
2022-08-28 11:43:08 +01:00
# include "../MiscHelpers/Common/CheckableMessageBox.h"
2021-10-16 16:19:51 +01:00
# include "Helpers/WinAdmin.h"
void COptionsWindow : : CreateAccess ( )
{
2021-11-13 08:28:32 +00:00
// Resource Access
connect ( ui . chkPrivacy , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAccessChanged ( ) ) ) ;
connect ( ui . chkUseSpecificity , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAccessChanged ( ) ) ) ;
connect ( ui . chkCloseForBox , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAccessChanged ( ) ) ) ;
connect ( ui . chkNoOpenForBox , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAccessChanged ( ) ) ) ;
//
2021-10-16 16:19:51 +01:00
connect ( ui . btnAddFile , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAddFile ( ) ) ) ;
QMenu * pFileBtnMenu = new QMenu ( ui . btnAddFile ) ;
pFileBtnMenu - > addAction ( tr ( " Browse for File " ) , this , SLOT ( OnBrowseFile ( ) ) ) ;
pFileBtnMenu - > addAction ( tr ( " Browse for Folder " ) , this , SLOT ( OnBrowseFolder ( ) ) ) ;
ui . btnAddFile - > setPopupMode ( QToolButton : : MenuButtonPopup ) ;
ui . btnAddFile - > setMenu ( pFileBtnMenu ) ;
2022-09-29 17:28:48 +01:00
connect ( ui . chkShowFilesTmpl , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnShowFilesTmpl ( ) ) ) ;
connect ( ui . btnDelFile , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnDelFile ( ) ) ) ;
2021-10-16 16:19:51 +01:00
connect ( ui . btnAddKey , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAddKey ( ) ) ) ;
2022-09-29 17:28:48 +01:00
connect ( ui . chkShowKeysTmpl , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnShowKeysTmpl ( ) ) ) ;
connect ( ui . btnDelKey , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnDelKey ( ) ) ) ;
2021-10-16 16:19:51 +01:00
connect ( ui . btnAddIPC , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAddIPC ( ) ) ) ;
2022-09-29 17:28:48 +01:00
connect ( ui . chkShowIPCTmpl , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnShowIPCTmpl ( ) ) ) ;
connect ( ui . btnDelIPC , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnDelIPC ( ) ) ) ;
2021-10-16 16:19:51 +01:00
connect ( ui . btnAddWnd , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAddWnd ( ) ) ) ;
2022-09-29 17:28:48 +01:00
connect ( ui . chkShowWndTmpl , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnShowWndTmpl ( ) ) ) ;
connect ( ui . btnDelWnd , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnDelWnd ( ) ) ) ;
2021-10-16 16:19:51 +01:00
connect ( ui . btnAddCOM , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnAddCOM ( ) ) ) ;
2022-09-29 17:28:48 +01:00
connect ( ui . chkShowCOMTmpl , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnShowCOMTmpl ( ) ) ) ;
connect ( ui . btnDelCOM , SIGNAL ( clicked ( bool ) ) , this , SLOT ( OnDelCOM ( ) ) ) ;
//connect(ui.chkShowAccessTmpl, SIGNAL(clicked(bool)), this, SLOT(OnShowAccessTmpl()));
//connect(ui.btnDelAccess, SIGNAL(clicked(bool)), this, SLOT(OnDelAccess()));
QTreeWidget * pTrees [ ] = { ui . treeFiles , ui . treeKeys , ui . treeIPC , ui . treeWnd , ui . treeCOM , NULL } ;
for ( QTreeWidget * * pTree = pTrees ; * pTree ; pTree + + ) {
//connect(*pTree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(OnAccessItemClicked(QTreeWidgetItem*, int)));
connect ( * pTree , SIGNAL ( itemDoubleClicked ( QTreeWidgetItem * , int ) ) , this , SLOT ( OnAccessItemDoubleClicked ( QTreeWidgetItem * , int ) ) ) ;
connect ( * pTree , SIGNAL ( itemSelectionChanged ( ) ) , this , SLOT ( OnAccessSelectionChanged ( ) ) ) ;
connect ( * pTree , SIGNAL ( itemChanged ( QTreeWidgetItem * , int ) ) , this , SLOT ( OnAccessChanged ( QTreeWidgetItem * , int ) ) ) ;
}
2021-10-16 16:19:51 +01:00
2022-09-29 17:28:48 +01:00
connect ( ui . tabsAccess , SIGNAL ( currentChanged ( int ) ) , this , SLOT ( OnAccessTab ( ) ) ) ;
2021-10-16 16:19:51 +01:00
}
2021-11-13 08:28:32 +00:00
void COptionsWindow : : OnAccessChanged ( )
{
2022-08-15 08:35:36 +01:00
if ( sender ( ) = = ui . chkPrivacy | | sender ( ) = = ui . chkUseSpecificity ) {
if ( ui . chkPrivacy - > isChecked ( ) | | ( ui . chkUseSpecificity - > isEnabled ( ) & & ui . chkUseSpecificity - > isChecked ( ) ) )
theGUI - > CheckCertificate ( this ) ;
}
2021-11-13 08:28:32 +00:00
UpdateAccessPolicy ( ) ;
2022-08-14 16:40:55 +01:00
if ( ( sender ( ) = = ui . chkPrivacy | | sender ( ) = = ui . chkRestrictDevices ) & & ! ( ui . chkPrivacy - > isChecked ( ) | | ui . chkRestrictDevices - > isChecked ( ) ) ) {
ui . chkUseSpecificity - > setChecked ( m_pBox - > GetBool ( " UseRuleSpecificity " , false ) ) ;
}
2021-11-13 08:28:32 +00:00
m_AccessChanged = true ;
OnOptChanged ( ) ;
}
void COptionsWindow : : UpdateAccessPolicy ( )
{
2022-08-14 16:40:55 +01:00
ui . chkUseSpecificity - > setEnabled ( ! ( ui . chkPrivacy - > isChecked ( ) | | ui . chkRestrictDevices - > isChecked ( ) ) ) ;
2022-08-09 17:19:46 +01:00
if ( ui . chkPrivacy - > isChecked ( ) | | ui . chkRestrictDevices - > isChecked ( ) ) {
2021-11-13 08:28:32 +00:00
ui . chkUseSpecificity - > setChecked ( true ) ;
}
}
2021-10-16 16:19:51 +01:00
QTreeWidgetItem * COptionsWindow : : GetAccessEntry ( EAccessType Type , const QString & Program , EAccessMode Mode , const QString & Path )
{
2022-09-29 17:28:48 +01:00
QTreeWidget * pTree = GetAccessTree ( Type ) ;
for ( int i = 0 ; i < pTree - > topLevelItemCount ( ) ; i + + )
2021-10-16 16:19:51 +01:00
{
2022-09-29 17:28:48 +01:00
QTreeWidgetItem * pItem = pTree - > topLevelItem ( i ) ;
2021-10-16 16:19:51 +01:00
if ( pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) = = Type
2022-09-29 17:28:48 +01:00
& & pItem - > data ( 1 , Qt : : UserRole ) . toString ( ) . compare ( Program , Qt : : CaseInsensitive ) = = 0
& & pItem - > data ( 2 , Qt : : UserRole ) . toInt ( ) = = Mode
& & pItem - > data ( 3 , Qt : : UserRole ) . toString ( ) . compare ( Path , Qt : : CaseInsensitive ) = = 0 )
2021-10-16 16:19:51 +01:00
return pItem ;
}
return NULL ;
}
void COptionsWindow : : SetAccessEntry ( EAccessType Type , const QString & Program , EAccessMode Mode , const QString & Path )
{
if ( GetAccessEntry ( Type , Program , Mode , Path ) ! = NULL )
return ; // already set
m_AccessChanged = true ;
2021-11-13 08:28:32 +00:00
OnOptChanged ( ) ;
2021-10-16 16:19:51 +01:00
AddAccessEntry ( Type , Mode , Program , Path ) ;
}
void COptionsWindow : : DelAccessEntry ( EAccessType Type , const QString & Program , EAccessMode Mode , const QString & Path )
{
if ( QTreeWidgetItem * pItem = GetAccessEntry ( Type , Program , Mode , Path ) )
{
delete pItem ;
m_AccessChanged = true ;
2021-11-13 08:28:32 +00:00
OnOptChanged ( ) ;
2021-10-16 16:19:51 +01:00
}
}
QString COptionsWindow : : AccessTypeToName ( EAccessEntry Type )
{
switch ( Type )
{
2021-11-13 08:28:32 +00:00
case eNormalFilePath : return " NormalFilePath " ;
2021-10-16 16:19:51 +01:00
case eOpenFilePath : return " OpenFilePath " ;
case eOpenPipePath : return " OpenPipePath " ;
case eClosedFilePath : return " ClosedFilePath " ;
case eReadFilePath : return " ReadFilePath " ;
case eWriteFilePath : return " WriteFilePath " ;
2021-11-13 08:28:32 +00:00
case eNormalKeyPath : return " NormalKeyPath " ;
2021-10-16 16:19:51 +01:00
case eOpenKeyPath : return " OpenKeyPath " ;
2021-11-13 08:28:32 +00:00
case eOpenConfPath : return " OpenConfPath " ;
2021-10-16 16:19:51 +01:00
case eClosedKeyPath : return " ClosedKeyPath " ;
case eReadKeyPath : return " ReadKeyPath " ;
case eWriteKeyPath : return " WriteKeyPath " ;
2021-11-13 08:28:32 +00:00
case eNormalIpcPath : return " NormalIpcPath " ;
2021-10-16 16:19:51 +01:00
case eOpenIpcPath : return " OpenIpcPath " ;
case eClosedIpcPath : return " ClosedIpcPath " ;
2022-03-29 18:36:23 +01:00
case eReadIpcPath : return " ReadIpcPath " ;
2021-10-16 16:19:51 +01:00
case eOpenWinClass : return " OpenWinClass " ;
2022-09-17 20:59:11 +01:00
case eNoRenameWinClass : return " NoRenameWinClass " ;
2021-10-16 16:19:51 +01:00
case eOpenCOM : return " OpenClsid " ;
case eClosedCOM : return " ClosedClsid " ;
case eClosedCOM_RT : return " ClosedRT " ;
}
return " Unknown " ;
}
void COptionsWindow : : LoadAccessList ( )
{
2021-11-13 08:28:32 +00:00
ui . chkPrivacy - > setChecked ( m_pBox - > GetBool ( " UsePrivacyMode " , false ) ) ;
ui . chkUseSpecificity - > setChecked ( m_pBox - > GetBool ( " UseRuleSpecificity " , false ) ) ;
2021-10-16 16:19:51 +01:00
ui . chkCloseForBox - > setChecked ( m_pBox - > GetBool ( " AlwaysCloseForBoxed " , true ) ) ;
2021-11-13 08:28:32 +00:00
ui . chkNoOpenForBox - > setChecked ( m_pBox - > GetBool ( " DontOpenForBoxed " , true ) ) ;
2021-10-16 16:19:51 +01:00
2022-09-29 17:28:48 +01:00
QTreeWidget * pTrees [ ] = { ui . treeFiles , ui . treeKeys , ui . treeIPC , ui . treeWnd , ui . treeCOM , NULL } ;
for ( QTreeWidget * * pTree = pTrees ; * pTree ; pTree + + )
( * pTree ) - > clear ( ) ;
2021-10-16 16:19:51 +01:00
2022-09-29 17:28:48 +01:00
for ( int i = 0 ; i < eMaxAccessEntry ; i + + )
2021-10-16 16:19:51 +01:00
{
foreach ( const QString & Value , m_pBox - > GetTextList ( AccessTypeToName ( ( EAccessEntry ) i ) , m_Template ) )
ParseAndAddAccessEntry ( ( EAccessEntry ) i , Value ) ;
2021-12-03 19:26:09 +00:00
foreach ( const QString & Value , m_pBox - > GetTextList ( AccessTypeToName ( ( EAccessEntry ) i ) + " Disabled " , m_Template ) )
ParseAndAddAccessEntry ( ( EAccessEntry ) i , Value , true ) ;
2021-10-16 16:19:51 +01:00
}
LoadAccessListTmpl ( ) ;
2021-11-13 08:28:32 +00:00
UpdateAccessPolicy ( ) ;
2021-10-16 16:19:51 +01:00
m_AccessChanged = false ;
}
void COptionsWindow : : LoadAccessListTmpl ( bool bUpdate )
{
2022-09-29 17:28:48 +01:00
for ( int i = 0 ; i < eMaxAccessType ; i + + ) {
QCheckBox * pCheck = NULL ;
switch ( i )
{
case eFile : pCheck = ui . chkShowFilesTmpl ; break ;
case eKey : pCheck = ui . chkShowKeysTmpl ; break ;
case eIPC : pCheck = ui . chkShowIPCTmpl ; break ;
case eWnd : pCheck = ui . chkShowWndTmpl ; break ;
case eCOM : pCheck = ui . chkShowCOMTmpl ; break ;
}
LoadAccessListTmpl ( ( EAccessType ) i , pCheck - > isChecked ( ) , bUpdate ) ;
}
}
QTreeWidget * COptionsWindow : : GetAccessTree ( EAccessType Type )
{
QTreeWidget * pTree = NULL ;
switch ( Type )
{
case eFile : pTree = ui . treeFiles ; break ;
case eKey : pTree = ui . treeKeys ; break ;
case eIPC : pTree = ui . treeIPC ; break ;
case eWnd : pTree = ui . treeWnd ; break ;
case eCOM : pTree = ui . treeCOM ; break ;
}
return pTree ;
}
void COptionsWindow : : LoadAccessListTmpl ( EAccessType Type , bool bChecked , bool bUpdate )
{
if ( bChecked )
2021-10-16 16:19:51 +01:00
{
2022-09-29 17:28:48 +01:00
foreach ( EAccessMode Mode , GetAccessModes ( ( EAccessType ) Type ) )
2021-10-16 16:19:51 +01:00
{
2022-09-29 17:28:48 +01:00
foreach ( const QString & Template , m_pBox - > GetTemplates ( ) )
2021-10-16 16:19:51 +01:00
{
2022-09-29 17:28:48 +01:00
foreach ( const QString & Value , m_pBox - > GetTextListTmpl ( MakeAccessStr ( Type , Mode ) , Template ) )
ParseAndAddAccessEntry ( Type , Mode , Value , false , Template ) ;
2021-10-16 16:19:51 +01:00
}
}
}
else if ( bUpdate )
{
2022-09-29 17:28:48 +01:00
QTreeWidget * pTree = GetAccessTree ( Type ) ;
for ( int i = 0 ; i < pTree - > topLevelItemCount ( ) ; )
2021-10-16 16:19:51 +01:00
{
2022-09-29 17:28:48 +01:00
QTreeWidgetItem * pItem = pTree - > topLevelItem ( i ) ;
2021-10-16 16:19:51 +01:00
int Type = pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) ;
if ( Type = = - 1 ) {
delete pItem ;
continue ; // entry from template
}
i + + ;
}
}
}
2021-12-03 19:26:09 +00:00
void COptionsWindow : : ParseAndAddAccessEntry ( EAccessEntry EntryType , const QString & Value , bool disabled , const QString & Template )
2021-10-16 16:19:51 +01:00
{
EAccessType Type ;
EAccessMode Mode ;
switch ( EntryType )
{
2021-11-13 08:28:32 +00:00
case eNormalFilePath : Type = eFile ; Mode = eNormal ; break ;
case eOpenFilePath : Type = eFile ; Mode = eOpen ; break ;
case eOpenPipePath : Type = eFile ; Mode = eOpen4All ; break ;
2021-10-16 16:19:51 +01:00
case eClosedFilePath : Type = eFile ; Mode = eClosed ; break ;
case eReadFilePath : Type = eFile ; Mode = eReadOnly ; break ;
2022-02-13 11:55:52 +00:00
case eWriteFilePath : Type = eFile ; Mode = eBoxOnly ; break ;
2021-10-16 16:19:51 +01:00
2021-11-13 08:28:32 +00:00
case eNormalKeyPath : Type = eKey ; Mode = eNormal ; break ;
case eOpenKeyPath : Type = eKey ; Mode = eOpen ; break ;
2022-09-29 17:28:48 +01:00
case eOpenConfPath : Type = eKey ; Mode = eOpen4All ; break ;
2021-10-16 16:19:51 +01:00
case eClosedKeyPath : Type = eKey ; Mode = eClosed ; break ;
case eReadKeyPath : Type = eKey ; Mode = eReadOnly ; break ;
2022-02-13 11:55:52 +00:00
case eWriteKeyPath : Type = eKey ; Mode = eBoxOnly ; break ;
2021-10-16 16:19:51 +01:00
2021-11-13 08:28:32 +00:00
case eNormalIpcPath : Type = eIPC ; Mode = eNormal ; break ;
case eOpenIpcPath : Type = eIPC ; Mode = eOpen ; break ;
2021-10-16 16:19:51 +01:00
case eClosedIpcPath : Type = eIPC ; Mode = eClosed ; break ;
2022-02-13 11:55:52 +00:00
case eReadIpcPath : Type = eIPC ; Mode = eReadOnly ; break ;
2021-10-16 16:19:51 +01:00
2021-11-13 08:28:32 +00:00
case eOpenWinClass : Type = eWnd ; Mode = eOpen ; break ;
2022-09-17 20:59:11 +01:00
case eNoRenameWinClass : Type = eWnd ; Mode = eNoRename ; break ;
2021-10-16 16:19:51 +01:00
2021-11-13 08:28:32 +00:00
case eOpenCOM : Type = eCOM ; Mode = eOpen ; break ;
2021-10-16 16:19:51 +01:00
case eClosedCOM : Type = eCOM ; Mode = eClosed ; break ;
case eClosedCOM_RT : Type = eCOM ; Mode = eClosedRT ; break ;
default : return ;
}
2022-09-29 17:28:48 +01:00
ParseAndAddAccessEntry ( Type , Mode , Value , disabled , Template ) ;
}
void COptionsWindow : : ParseAndAddAccessEntry ( EAccessType Type , EAccessMode Mode , const QString & Value , bool disabled , const QString & Template )
{
2021-10-16 16:19:51 +01:00
//
// Mind this special cases
// OpenIpcPath=$:program.exe <- full access into the address space of a target process running outside the sandbox.
// OpenWinClass=$:program.exe <- permits to use the PostThreadMessage API to send a message directly to a thread running outside the sandbox.
// This form of the setting does not support wildcards.
//
QStringList Values = Value . split ( " , " ) ;
2022-09-29 17:28:48 +01:00
if ( Type = = eWnd & & Mode = = eOpen ) {
int pos = Values . count ( ) > = 2 ? 1 : 0 ;
if ( Values [ pos ] . right ( 11 ) . compare ( " /IgnoreUIPI " , Qt : : CaseInsensitive ) = = 0 ) {
Mode = eIgnoreUIPI ;
Values [ pos ] . truncate ( Values [ pos ] . length ( ) - 11 ) ;
}
}
2021-10-16 16:19:51 +01:00
if ( Values . count ( ) > = 2 )
2021-12-03 19:26:09 +00:00
AddAccessEntry ( Type , Mode , Values [ 0 ] , Values [ 1 ] , disabled , Template ) ;
2021-10-16 16:19:51 +01:00
else // all programs
2021-12-03 19:26:09 +00:00
AddAccessEntry ( Type , Mode , " " , Values [ 0 ] , disabled , Template ) ;
2021-10-16 16:19:51 +01:00
}
QString COptionsWindow : : GetAccessModeStr ( EAccessMode Mode )
{
switch ( Mode )
{
2021-11-13 08:28:32 +00:00
case eNormal : return tr ( " Normal " ) ;
case eOpen : return tr ( " Open " ) ;
case eOpen4All : return tr ( " Open for All " ) ;
2022-09-17 20:59:11 +01:00
case eNoRename : return tr ( " No Rename " ) ;
2021-10-16 16:19:51 +01:00
case eClosed : return tr ( " Closed " ) ;
case eClosedRT : return tr ( " Closed RT " ) ;
case eReadOnly : return tr ( " Read Only " ) ;
2022-02-13 11:55:52 +00:00
case eBoxOnly : return tr ( " Box Only (Write Only) " ) ;
2022-09-29 17:28:48 +01:00
case eIgnoreUIPI : return tr ( " Ignore UIPI " ) ;
2021-10-16 16:19:51 +01:00
}
return tr ( " Unknown " ) ;
}
2022-10-07 12:16:22 +01:00
QString COptionsWindow : : GetAccessModeTip ( EAccessMode Mode )
{
switch ( Mode )
{
2022-10-07 14:23:19 +01:00
case eNormal : return tr ( " Regular Sandboxie behavior - allow read and also copy on write. " ) ;
case eOpen : return tr ( " Allow write-access outside the sandbox. " ) ;
case eOpen4All : return tr ( " Allow write-access outside the sandbox, also for applications installed inside the sandbox. " ) ;
2022-10-07 12:16:22 +01:00
case eNoRename : return tr ( " Don't rename window classes. " ) ;
2022-10-07 14:23:19 +01:00
case eClosed : return tr ( " Deny access to host location and prevent creation of sandboxed copies. " ) ;
2022-10-07 12:16:22 +01:00
case eClosedRT : return tr ( " Block access to WinRT class. " ) ;
2022-10-07 14:23:19 +01:00
case eReadOnly : return tr ( " Allow read-only access only. " ) ;
2022-10-07 12:16:22 +01:00
case eBoxOnly : return tr ( " Hide host files, folders or registry keys from sandboxed processes. " ) ;
case eIgnoreUIPI : return tr ( " Ignore UIPI restrictions for processes. " ) ;
}
return tr ( " Unknown " ) ;
}
2021-10-16 16:19:51 +01:00
QString COptionsWindow : : GetAccessTypeStr ( EAccessType Type )
{
switch ( Type )
{
case eFile : return tr ( " File/Folder " ) ;
case eKey : return tr ( " Registry " ) ;
case eIPC : return tr ( " IPC Path " ) ;
case eWnd : return tr ( " Wnd Class " ) ;
case eCOM : return tr ( " COM Object " ) ;
}
return tr ( " Unknown " ) ;
}
void COptionsWindow : : OnBrowseFile ( )
{
QString Value = QFileDialog : : getOpenFileName ( this , tr ( " Select File " ) , " " , tr ( " All Files (*.*) " ) ) . replace ( " / " , " \\ " ) ;
if ( Value . isEmpty ( ) )
return ;
2021-11-13 08:28:32 +00:00
AddAccessEntry ( eFile , eOpen , " " , Value ) ;
2021-10-16 16:19:51 +01:00
m_AccessChanged = true ;
2021-11-13 08:28:32 +00:00
OnOptChanged ( ) ;
2021-10-16 16:19:51 +01:00
}
void COptionsWindow : : OnBrowseFolder ( )
{
QString Value = QFileDialog : : getExistingDirectory ( this , tr ( " Select Directory " ) ) . replace ( " / " , " \\ " ) ;
if ( Value . isEmpty ( ) )
return ;
2021-11-13 08:28:32 +00:00
AddAccessEntry ( eFile , eOpen , " " , Value ) ;
2021-10-16 16:19:51 +01:00
m_AccessChanged = true ;
2021-11-13 08:28:32 +00:00
OnOptChanged ( ) ;
2021-10-16 16:19:51 +01:00
}
2022-09-29 17:28:48 +01:00
void COptionsWindow : : AddAccessEntry ( EAccessType Type , EAccessMode Mode , QString Program , const QString & Path , bool disabled , const QString & Template )
2021-10-16 16:19:51 +01:00
{
QTreeWidgetItem * pItem = new QTreeWidgetItem ( ) ;
pItem - > setText ( 0 , GetAccessTypeStr ( Type ) + ( Template . isEmpty ( ) ? " " : " ( " + Template + " ) " ) ) ;
pItem - > setData ( 0 , Qt : : UserRole , ! Template . isEmpty ( ) ? - 1 : ( int ) Type ) ;
pItem - > setData ( 1 , Qt : : UserRole , Program ) ;
bool bAll = Program . isEmpty ( ) ;
if ( bAll )
Program = tr ( " All Programs " ) ;
bool Not = Program . left ( 1 ) = = " ! " ;
if ( Not )
Program . remove ( 0 , 1 ) ;
if ( Program . left ( 1 ) = = " < " )
Program = tr ( " Group: %1 " ) . arg ( Program . mid ( 1 , Program . length ( ) - 2 ) ) ;
else if ( ! bAll )
m_Programs . insert ( Program ) ;
pItem - > setText ( 1 , ( Not ? " NOT " : " " ) + Program ) ;
pItem - > setText ( 2 , GetAccessModeStr ( Mode ) ) ;
pItem - > setData ( 2 , Qt : : UserRole , ( int ) Mode ) ;
2022-08-28 11:43:08 +01:00
//////////////////////////////////////////////////////////
// File and Registry entries auto append a '*' wildcard
// when thay don't contain any.
// Prepanding '|' disables this behavioure
//
2022-07-28 07:40:57 +01:00
QString sPath = Path ;
2022-08-28 11:43:08 +01:00
if ( Type = = eFile | | Type = = eKey ) {
if ( ! sPath . isEmpty ( ) & & sPath . left ( 1 ) ! = " | " & & ! sPath . contains ( " * " ) & & sPath . right ( 1 ) ! = " * " )
2022-07-28 07:40:57 +01:00
sPath . append ( " * " ) ;
}
pItem - > setText ( 3 , sPath ) ;
2021-10-16 16:19:51 +01:00
pItem - > setData ( 3 , Qt : : UserRole , Path ) ;
2021-12-03 19:26:09 +00:00
if ( Template . isEmpty ( ) )
pItem - > setCheckState ( 0 , disabled ? Qt : : Unchecked : Qt : : Checked ) ;
2022-09-29 17:28:48 +01:00
QTreeWidget * pTree = GetAccessTree ( Type ) ;
pTree - > addTopLevelItem ( pItem ) ;
2021-10-16 16:19:51 +01:00
}
QString COptionsWindow : : MakeAccessStr ( EAccessType Type , EAccessMode Mode )
{
switch ( Type )
{
case eFile :
switch ( Mode )
{
2021-11-13 08:28:32 +00:00
case eNormal : return " NormalFilePath " ;
case eOpen : return " OpenFilePath " ;
case eOpen4All : return " OpenPipePath " ;
2021-10-16 16:19:51 +01:00
case eClosed : return " ClosedFilePath " ;
case eReadOnly : return " ReadFilePath " ;
2022-02-13 11:55:52 +00:00
case eBoxOnly : return " WriteFilePath " ;
2021-10-16 16:19:51 +01:00
}
break ;
case eKey :
switch ( Mode )
{
2021-11-13 08:28:32 +00:00
case eNormal : return " NormalKeyPath " ;
case eOpen : return " OpenKeyPath " ;
case eOpen4All : return " OpenConfPath " ;
2021-10-16 16:19:51 +01:00
case eClosed : return " ClosedKeyPath " ;
case eReadOnly : return " ReadKeyPath " ;
2022-02-13 11:55:52 +00:00
case eBoxOnly : return " WriteKeyPath " ;
2021-10-16 16:19:51 +01:00
}
break ;
case eIPC :
switch ( Mode )
{
2021-11-13 08:28:32 +00:00
case eNormal : return " NormalIpcPath " ;
case eOpen : return " OpenIpcPath " ;
2021-10-16 16:19:51 +01:00
case eClosed : return " ClosedIpcPath " ;
2022-02-13 11:55:52 +00:00
case eReadOnly : return " ReadIpcPath " ;
2021-10-16 16:19:51 +01:00
}
break ;
case eWnd :
switch ( Mode )
{
2021-11-13 08:28:32 +00:00
case eOpen : return " OpenWinClass " ;
2022-09-17 20:59:11 +01:00
case eNoRename : return " NoRenameWinClass " ;
2021-10-16 16:19:51 +01:00
}
break ;
case eCOM :
switch ( Mode )
{
2021-11-13 08:28:32 +00:00
case eOpen : return " OpenClsid " ;
2021-10-16 16:19:51 +01:00
case eClosed : return " ClosedClsid " ;
case eClosedRT : return " ClosedRT " ;
}
break ;
}
return " Unknown " ;
}
/*void COptionsWindow::OnAccessItemClicked(QTreeWidgetItem* pItem, int Column)
{
if ( Column ! = 0 )
return ;
CloseAccessEdit ( pItem ) ;
} */
void COptionsWindow : : CloseAccessEdit ( bool bSave )
{
2022-09-29 17:28:48 +01:00
QTreeWidget * pTrees [ ] = { ui . treeFiles , ui . treeKeys , ui . treeIPC , ui . treeWnd , ui . treeCOM , NULL } ;
for ( QTreeWidget * * pTree = pTrees ; * pTree ; pTree + + ) {
for ( int i = 0 ; i < ( * pTree ) - > topLevelItemCount ( ) ; i + + )
{
QTreeWidgetItem * pItem = ( * pTree ) - > topLevelItem ( i ) ;
CloseAccessEdit ( pItem , bSave ) ;
}
2021-10-16 16:19:51 +01:00
}
}
void COptionsWindow : : CloseAccessEdit ( QTreeWidgetItem * pItem , bool bSave )
{
2022-09-29 17:28:48 +01:00
QTreeWidget * pTree = pItem - > treeWidget ( ) ;
QWidget * pProgram = pTree - > itemWidget ( pItem , 1 ) ;
2021-10-16 16:19:51 +01:00
if ( ! pProgram )
return ;
if ( bSave )
{
2022-07-10 19:00:26 +01:00
QHBoxLayout * pLayout = ( QHBoxLayout * ) pProgram - > layout ( ) ;
QToolButton * pNot = ( QToolButton * ) pLayout - > itemAt ( 0 ) - > widget ( ) ;
QComboBox * pCombo = ( QComboBox * ) pLayout - > itemAt ( 1 ) - > widget ( ) ;
2022-09-29 17:28:48 +01:00
QComboBox * pMode = ( QComboBox * ) pTree - > itemWidget ( pItem , 2 ) ;
QLineEdit * pPath = ( QLineEdit * ) pTree - > itemWidget ( pItem , 3 ) ;
2022-07-10 19:00:26 +01:00
QString Program = pCombo - > currentText ( ) ;
int Index = pCombo - > findText ( Program ) ;
if ( Index ! = - 1 )
Program = pCombo - > itemData ( Index , Qt : : UserRole ) . toString ( ) ;
if ( ! Program . isEmpty ( ) & & Program . left ( 1 ) ! = " < " )
m_Programs . insert ( Program ) ;
2022-09-18 09:39:48 +01:00
EAccessMode Mode = ( EAccessMode ) pMode - > currentData ( ) . toInt ( ) ;
QString Path = pPath - > text ( ) ;
if ( pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) = = eCOM & & ! Path . isEmpty ( ) )
2021-10-16 16:19:51 +01:00
{
2022-09-18 09:39:48 +01:00
bool isGUID = Path . length ( ) = = 38 & & Path . left ( 1 ) = = " { " & & Path . right ( 1 ) = = " } " ;
switch ( Mode )
2021-10-16 16:19:51 +01:00
{
2021-11-13 08:28:32 +00:00
case eOpen :
2021-10-16 16:19:51 +01:00
case eClosed :
if ( ! isGUID ) {
QMessageBox : : critical ( this , " SandboxiePlus " , tr ( " COM objects must be specified by their GUID, like: {00000000-0000-0000-0000-000000000000} " ) ) ;
return ;
}
break ;
case eClosedRT :
if ( isGUID ) {
QMessageBox : : critical ( this , " SandboxiePlus " , tr ( " RT interfaces must be specified by their name. " ) ) ;
return ;
}
break ;
}
}
2022-09-18 09:39:48 +01:00
if ( pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) = = eIPC & & Mode = = eOpen
& & ( ( Path = = " * " & & pItem - > data ( 3 , Qt : : UserRole ) . toString ( ) ! = " * " )
| | ( Path = = " \\ * " & & pItem - > data ( 3 , Qt : : UserRole ) . toString ( ) ! = " \\ * " ) )
2022-08-28 11:43:08 +01:00
& & ! m_BoxTemplates . contains ( " BoxedCOM " ) )
{
if ( theConf - > GetInt ( " Options/WarnOpenCOM " , - 1 ) = = - 1 ) {
bool State = false ;
2022-09-18 10:08:14 +01:00
if ( CCheckableMessageBox : : question ( this , " Sandboxie-Plus " , tr ( " Opening all IPC access also opens COM access, do you still want to restrict COM to the sandbox? " )
2022-08-28 11:43:08 +01:00
, tr ( " Don't ask in future " ) , & State , QDialogButtonBox : : Yes | QDialogButtonBox : : No , QDialogButtonBox : : Yes ) = = QDialogButtonBox : : Yes )
SetTemplate ( " BoxedCOM " , true ) ; // Normal overrides Open even without rule specificity :D
if ( State )
theConf - > SetValue ( " Options/WarnOpenCOM " , 1 ) ;
}
}
2022-09-18 09:39:48 +01:00
if ( pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) = = eWnd & & Mode = = eOpen & & Path = = " # " & & ! Program . isEmpty ( ) )
{
QMessageBox : : warning ( this , " Sandboxie-Plus " , tr ( " 'OpenWinClass=program.exe,#' is not supported, use 'NoRenameWinClass=program.exe,*' instead " ) ) ;
Mode = eNoRename ;
Path = " * " ;
}
2021-10-16 16:19:51 +01:00
pItem - > setText ( 1 , ( pNot - > isChecked ( ) ? " NOT " : " " ) + pCombo - > currentText ( ) ) ;
pItem - > setData ( 1 , Qt : : UserRole , ( pNot - > isChecked ( ) ? " ! " : " " ) + Program ) ;
2022-09-18 09:39:48 +01:00
pItem - > setText ( 2 , GetAccessModeStr ( Mode ) ) ;
2021-10-16 16:19:51 +01:00
pItem - > setData ( 2 , Qt : : UserRole , pMode - > currentData ( ) ) ;
2022-09-18 09:39:48 +01:00
pItem - > setText ( 3 , Path ) ;
2021-10-16 16:19:51 +01:00
pItem - > setData ( 3 , Qt : : UserRole , pPath - > text ( ) ) ;
m_AccessChanged = true ;
2021-11-13 08:28:32 +00:00
OnOptChanged ( ) ;
2021-10-16 16:19:51 +01:00
}
2022-09-29 17:28:48 +01:00
pTree - > setItemWidget ( pItem , 1 , NULL ) ;
pTree - > setItemWidget ( pItem , 2 , NULL ) ;
pTree - > setItemWidget ( pItem , 3 , NULL ) ;
2021-10-16 16:19:51 +01:00
}
QList < COptionsWindow : : EAccessMode > COptionsWindow : : GetAccessModes ( EAccessType Type )
{
switch ( Type )
{
2022-02-13 11:55:52 +00:00
case eFile : return QList < EAccessMode > ( ) < < eNormal < < eOpen < < eOpen4All < < eClosed < < eReadOnly < < eBoxOnly ;
case eKey : return QList < EAccessMode > ( ) < < eNormal < < eOpen < < eOpen4All < < eClosed < < eReadOnly < < eBoxOnly ;
2022-03-29 18:36:23 +01:00
case eIPC : return QList < EAccessMode > ( ) < < eNormal < < eOpen < < eClosed < < eReadOnly ;
2022-09-29 17:28:48 +01:00
case eWnd : return QList < EAccessMode > ( ) < < eOpen < < eNoRename < < eIgnoreUIPI ;
2021-11-13 08:28:32 +00:00
case eCOM : return QList < EAccessMode > ( ) < < eOpen < < eClosed < < eClosedRT ;
2021-10-16 16:19:51 +01:00
}
return QList < EAccessMode > ( ) ;
}
void COptionsWindow : : OnAccessItemDoubleClicked ( QTreeWidgetItem * pItem , int Column )
{
//if (Column == 0)
// return;
2022-09-29 17:28:48 +01:00
QTreeWidget * pTree = ( QTreeWidget * ) sender ( ) ;
2021-10-16 16:19:51 +01:00
int Type = pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) ;
if ( Type = = - 1 ) {
QMessageBox : : warning ( this , " SandboxiePlus " , tr ( " Template values can not be edited. " ) ) ;
return ;
}
QString Program = pItem - > data ( 1 , Qt : : UserRole ) . toString ( ) ;
QWidget * pProgram = new QWidget ( ) ;
pProgram - > setAutoFillBackground ( true ) ;
QHBoxLayout * pLayout = new QHBoxLayout ( ) ;
2022-09-29 17:28:48 +01:00
pLayout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
2021-10-16 16:19:51 +01:00
pLayout - > setSpacing ( 0 ) ;
pProgram - > setLayout ( pLayout ) ;
QToolButton * pNot = new QToolButton ( pProgram ) ;
pNot - > setText ( " ! " ) ;
pNot - > setCheckable ( true ) ;
if ( Program . left ( 1 ) = = " ! " ) {
pNot - > setChecked ( true ) ;
Program . remove ( 0 , 1 ) ;
}
pLayout - > addWidget ( pNot ) ;
QComboBox * pCombo = new QComboBox ( pProgram ) ;
pCombo - > addItem ( tr ( " All Programs " ) , " " ) ;
2021-11-13 08:28:32 +00:00
foreach ( const QString Group , GetCurrentGroups ( ) ) {
QString GroupName = Group . mid ( 1 , Group . length ( ) - 2 ) ;
2021-12-03 19:26:09 +00:00
pCombo - > addItem ( tr ( " Group: %1 " ) . arg ( GroupName ) , Group ) ;
2021-10-16 16:19:51 +01:00
}
foreach ( const QString & Name , m_Programs )
pCombo - > addItem ( Name , Name ) ;
pCombo - > setEditable ( true ) ;
int Index = pCombo - > findData ( Program ) ;
pCombo - > setCurrentIndex ( Index ) ;
if ( Index = = - 1 )
pCombo - > setCurrentText ( Program ) ;
pLayout - > addWidget ( pCombo ) ;
2022-09-29 17:28:48 +01:00
pTree - > setItemWidget ( pItem , 1 , pProgram ) ;
2021-10-16 16:19:51 +01:00
QComboBox * pMode = new QComboBox ( ) ;
2022-10-07 12:16:22 +01:00
foreach ( EAccessMode Mode , GetAccessModes ( ( EAccessType ) Type ) ) {
2021-10-16 16:19:51 +01:00
pMode - > addItem ( GetAccessModeStr ( Mode ) , ( int ) Mode ) ;
2022-10-07 12:16:22 +01:00
pMode - > setItemData ( pMode - > count ( ) - 1 , GetAccessModeTip ( Mode ) , Qt : : ToolTipRole ) ;
}
2021-10-16 16:19:51 +01:00
pMode - > setCurrentIndex ( pMode - > findData ( pItem - > data ( 2 , Qt : : UserRole ) ) ) ;
2022-09-29 17:28:48 +01:00
pTree - > setItemWidget ( pItem , 2 , pMode ) ;
2021-10-16 16:19:51 +01:00
QLineEdit * pPath = new QLineEdit ( ) ;
pPath - > setText ( pItem - > data ( 3 , Qt : : UserRole ) . toString ( ) ) ;
2022-09-29 17:28:48 +01:00
pTree - > setItemWidget ( pItem , 3 , pPath ) ;
2021-10-16 16:19:51 +01:00
}
2021-12-03 19:26:09 +00:00
void COptionsWindow : : OnAccessChanged ( QTreeWidgetItem * pItem , int Column )
{
if ( Column ! = 0 )
return ;
m_AccessChanged = true ;
OnOptChanged ( ) ;
}
2022-09-29 17:28:48 +01:00
void COptionsWindow : : DeleteAccessEntry ( QTreeWidgetItem * pItem , int Column )
2021-10-16 16:19:51 +01:00
{
if ( ! pItem )
return ;
2022-09-29 17:28:48 +01:00
if ( pItem - > data ( Column , Qt : : UserRole ) . toInt ( ) = = - 1 ) {
2021-10-16 16:19:51 +01:00
QMessageBox : : warning ( this , " SandboxiePlus " , tr ( " Template values can not be removed. " ) ) ;
return ;
}
delete pItem ;
}
void COptionsWindow : : SaveAccessList ( )
{
2021-11-13 08:28:32 +00:00
WriteAdvancedCheck ( ui . chkPrivacy , " UsePrivacyMode " , " y " , " " ) ;
WriteAdvancedCheck ( ui . chkUseSpecificity , " UseRuleSpecificity " , " y " , " " ) ;
2021-10-16 16:19:51 +01:00
WriteAdvancedCheck ( ui . chkCloseForBox , " AlwaysCloseForBoxed " , " " , " n " ) ;
2021-11-13 08:28:32 +00:00
WriteAdvancedCheck ( ui . chkNoOpenForBox , " DontOpenForBoxed " , " " , " n " ) ;
2021-10-16 16:19:51 +01:00
CloseAccessEdit ( true ) ;
2021-12-06 19:59:29 +00:00
QStringList Keys = QStringList ( )
2021-11-13 08:28:32 +00:00
< < " NormalFilePath " < < " OpenFilePath " < < " OpenPipePath " < < " ClosedFilePath " < < " ReadFilePath " < < " WriteFilePath "
< < " NormalKeyPath " < < " OpenKeyPath " < < " OpenConfPath " < < " ClosedKeyPath " < < " ReadKeyPath " < < " WriteKeyPath "
2022-09-17 20:59:11 +01:00
< < " NormalIpcPath " < < " OpenIpcPath " < < " ClosedIpcPath " < < " ReadIpcPath "
< < " OpenWinClass " < < " NoRenameWinClass "
< < " OpenClsid " < < " ClosedClsid " < < " ClosedRT " ;
2021-10-16 16:19:51 +01:00
QMap < QString , QList < QString > > AccessMap ;
2022-09-29 17:28:48 +01:00
QTreeWidget * pTrees [ ] = { ui . treeFiles , ui . treeKeys , ui . treeIPC , ui . treeWnd , ui . treeCOM , NULL } ;
for ( QTreeWidget * * pTree = pTrees ; * pTree ; pTree + + )
2021-10-16 16:19:51 +01:00
{
2022-09-29 17:28:48 +01:00
for ( int i = 0 ; i < ( * pTree ) - > topLevelItemCount ( ) ; i + + )
{
QTreeWidgetItem * pItem = ( * pTree ) - > topLevelItem ( i ) ;
int Type = pItem - > data ( 0 , Qt : : UserRole ) . toInt ( ) ;
if ( Type = = - 1 )
continue ; // entry from template
int Mode = pItem - > data ( 2 , Qt : : UserRole ) . toInt ( ) ;
QString Program = pItem - > data ( 1 , Qt : : UserRole ) . toString ( ) ;
QString Value = pItem - > data ( 3 , Qt : : UserRole ) . toString ( ) ;
if ( ! Program . isEmpty ( ) )
Value . prepend ( Program + " , " ) ;
if ( Type = = eWnd & & Mode = = eIgnoreUIPI ) {
Mode = eOpen ;
Value . append ( " /IgnoreUIPI " ) ;
}
QString AccessStr = MakeAccessStr ( ( EAccessType ) Type , ( EAccessMode ) Mode ) ;
if ( pItem - > checkState ( 0 ) = = Qt : : Unchecked )
AccessStr + = " Disabled " ;
AccessMap [ AccessStr ] . append ( Value ) ;
}
2021-10-16 16:19:51 +01:00
}
2021-12-06 19:59:29 +00:00
foreach ( const QString & Key , Keys ) {
2021-10-16 16:19:51 +01:00
WriteTextList ( Key , AccessMap [ Key ] ) ;
2021-12-03 19:26:09 +00:00
WriteTextList ( Key + " Disabled " , AccessMap [ Key + " Disabled " ] ) ;
}
2021-10-16 16:19:51 +01:00
m_AccessChanged = false ;
}