00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Security.h"
00026 #include "Network.h"
00027 #include "LiveList.h"
00028 #include "WndSecurity.h"
00029 #include "DlgSecureRule.h"
00030 #include "XML.h"
00031
00032 #ifdef _DEBUG
00033 #define new DEBUG_NEW
00034 #undef THIS_FILE
00035 static char THIS_FILE[] = __FILE__;
00036 #endif
00037
00038 IMPLEMENT_SERIAL(CSecurityWnd, CPanelWnd, 0)
00039
00040 BEGIN_MESSAGE_MAP(CSecurityWnd, CPanelWnd)
00041
00042 ON_WM_CREATE()
00043 ON_WM_DESTROY()
00044 ON_WM_SIZE()
00045 ON_WM_TIMER()
00046 ON_NOTIFY(NM_CUSTOMDRAW, IDC_RULES, OnCustomDrawList)
00047 ON_NOTIFY(NM_DBLCLK, IDC_RULES, OnDblClkList)
00048 ON_NOTIFY(LVN_COLUMNCLICK, IDC_RULES, OnSortList)
00049 ON_UPDATE_COMMAND_UI(ID_SECURITY_EDIT, OnUpdateSecurityEdit)
00050 ON_COMMAND(ID_SECURITY_EDIT, OnSecurityEdit)
00051 ON_UPDATE_COMMAND_UI(ID_SECURITY_RESET, OnUpdateSecurityReset)
00052 ON_COMMAND(ID_SECURITY_RESET, OnSecurityReset)
00053 ON_UPDATE_COMMAND_UI(ID_SECURITY_REMOVE, OnUpdateSecurityRemove)
00054 ON_COMMAND(ID_SECURITY_REMOVE, OnSecurityRemove)
00055 ON_COMMAND(ID_SECURITY_ADD, OnSecurityAdd)
00056 ON_UPDATE_COMMAND_UI(ID_SECURITY_POLICY_ACCEPT, OnUpdateSecurityPolicyAccept)
00057 ON_COMMAND(ID_SECURITY_POLICY_ACCEPT, OnSecurityPolicyAccept)
00058 ON_UPDATE_COMMAND_UI(ID_SECURITY_POLICY_DENY, OnUpdateSecurityPolicyDeny)
00059 ON_COMMAND(ID_SECURITY_POLICY_DENY, OnSecurityPolicyDeny)
00060 ON_WM_CONTEXTMENU()
00061 ON_UPDATE_COMMAND_UI(ID_SECURITY_MOVE_UP, OnUpdateSecurityMoveUp)
00062 ON_COMMAND(ID_SECURITY_MOVE_UP, OnSecurityMoveUp)
00063 ON_UPDATE_COMMAND_UI(ID_SECURITY_MOVE_DOWN, OnUpdateSecurityMoveDown)
00064 ON_COMMAND(ID_SECURITY_MOVE_DOWN, OnSecurityMoveDown)
00065 ON_UPDATE_COMMAND_UI(ID_SECURITY_EXPORT, OnUpdateSecurityExport)
00066 ON_COMMAND(ID_SECURITY_EXPORT, OnSecurityExport)
00067 ON_COMMAND(ID_SECURITY_IMPORT, OnSecurityImport)
00068
00069 END_MESSAGE_MAP()
00070
00071
00073
00074
00075 CSecurityWnd::CSecurityWnd()
00076 {
00077 Create( IDR_SECURITYFRAME );
00078 }
00079
00080 CSecurityWnd::~CSecurityWnd()
00081 {
00082 }
00083
00085
00086
00087 int CSecurityWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
00088 {
00089 if ( CPanelWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00090
00091 m_wndList.Create( WS_VISIBLE|LVS_ICON|LVS_AUTOARRANGE|LVS_REPORT|LVS_SHOWSELALWAYS,
00092 rectDefault, this, IDC_RULES );
00093
00094 m_pSizer.Attach( &m_wndList );
00095
00096 m_wndList.SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE,
00097 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP,
00098 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP );
00099
00100 CBitmap bmBase;
00101 bmBase.LoadBitmap( IDB_SECURITY );
00102 if ( theApp.m_bRTL )
00103 bmBase.m_hObject = CreateMirroredBitmap( (HBITMAP) bmBase.m_hObject );
00104
00105 m_gdiImageList.Create( 16, 16, ILC_MASK|ILC_COLOR16, 3, 1 );
00106 m_gdiImageList.Add( &bmBase, RGB( 0, 255, 0 ) );
00107 m_wndList.SetImageList( &m_gdiImageList, LVSIL_SMALL );
00108
00109 m_wndList.InsertColumn( 0, _T("Address / Content"), LVCFMT_LEFT, 200, -1 );
00110 m_wndList.InsertColumn( 1, _T("Action"), LVCFMT_CENTER, 100, 0 );
00111 m_wndList.InsertColumn( 2, _T("Expires"), LVCFMT_CENTER, 100, 1 );
00112 m_wndList.InsertColumn( 3, _T("Precedence"), LVCFMT_CENTER, 00, 2 );
00113 m_wndList.InsertColumn( 4, _T("Hits"), LVCFMT_CENTER, 60, 3 );
00114 m_wndList.InsertColumn( 5, _T("Comment"), LVCFMT_LEFT, 100, 4 );
00115
00116 m_wndList.SetFont( &theApp.m_gdiFont );
00117
00118 LoadState( _T("CSecurityWnd"), TRUE );
00119
00120 CSingleLock pLock( &Network.m_pSection );
00121
00122 if ( pLock.Lock( 50 ) ) Update();
00123
00124 return 0;
00125 }
00126
00127 void CSecurityWnd::OnDestroy()
00128 {
00129 Security.Save( TRUE );
00130
00131 Settings.SaveList( _T("CSecurityWnd"), &m_wndList );
00132 SaveState( _T("CSecurityWnd") );
00133
00134 CPanelWnd::OnDestroy();
00135 }
00136
00138
00139
00140 void CSecurityWnd::Update(int nColumn, BOOL bSort)
00141 {
00142 CSingleLock pLock( &Network.m_pSection, TRUE );
00143 CLiveList pLiveList( 6 );
00144
00145 CLiveItem* pDefault = pLiveList.Add( (LPVOID)0 );
00146 pDefault->Set( 0, _T("Default Policy") );
00147 pDefault->Set( 1, Security.m_bDenyPolicy ? _T("Deny") : _T("Accept") );
00148 pDefault->Set( 3, _T("X") );
00149 pDefault->m_nImage = Security.m_bDenyPolicy ? theApp.m_bRTL ? 0 : 2 : 1;
00150
00151 Security.Expire();
00152
00153 DWORD nNow = time( NULL );
00154 int nCount = 1;
00155
00156 for ( POSITION pos = Security.GetIterator() ; pos ; nCount++ )
00157 {
00158 CSecureRule* pRule = Security.GetNext( pos );
00159
00160 CLiveItem* pItem = pLiveList.Add( pRule );
00161
00162 pItem->m_nImage = theApp.m_bRTL ? 2 - pRule->m_nAction : pRule->m_nAction;
00163
00164 if ( pRule->m_nType == CSecureRule::srAddress )
00165 {
00166 if ( *(DWORD*)pRule->m_nMask == 0xFFFFFFFF )
00167 {
00168 pItem->Format( 0, _T("%u.%u.%u.%u"),
00169 unsigned( pRule->m_nIP[0] ), unsigned( pRule->m_nIP[1] ),
00170 unsigned( pRule->m_nIP[2] ), unsigned( pRule->m_nIP[3] ) );
00171 }
00172 else
00173 {
00174 pItem->Format( 0, _T("%u.%u.%u.%u/%u.%u.%u.%u"),
00175 unsigned( pRule->m_nIP[0] ), unsigned( pRule->m_nIP[1] ),
00176 unsigned( pRule->m_nIP[2] ), unsigned( pRule->m_nIP[3] ),
00177 unsigned( pRule->m_nMask[0] ), unsigned( pRule->m_nMask[1] ),
00178 unsigned( pRule->m_nMask[2] ), unsigned( pRule->m_nMask[3] ) );
00179 }
00180 }
00181 else if ( pRule->m_nType == CSecureRule::srContent )
00182 {
00183 pItem->Set( 0, pRule->GetContentWords() );
00184 }
00185
00186 switch ( pRule->m_nAction )
00187 {
00188 case CSecureRule::srNull:
00189 pItem->Set( 1, _T("N/A") );
00190 break;
00191 case CSecureRule::srAccept:
00192 pItem->Set( 1, _T("Accept") );
00193 break;
00194 case CSecureRule::srDeny:
00195 pItem->Set( 1, _T("Deny") );
00196 break;
00197 }
00198
00199 if ( pRule->m_nExpire == CSecureRule::srIndefinite )
00200 {
00201 pItem->Set( 2, _T("Never") );
00202 }
00203 else if ( pRule->m_nExpire == CSecureRule::srSession )
00204 {
00205 pItem->Set( 2, _T("Session") );
00206 }
00207 else if ( pRule->m_nExpire >= nNow )
00208 {
00209 DWORD nTime = ( pRule->m_nExpire - nNow );
00210 pItem->Format( 2, _T("%ud %uh %um"), nTime / 86400u, (nTime % 86400u) / 3600u, ( nTime % 3600u ) / 60u );
00211
00212 }
00213
00214 pItem->Format( 3, _T("%i"), nCount );
00215 pItem->Format( 4, _T("%u (%u)"), pRule->m_nToday, pRule->m_nEver );
00216 pItem->Set( 5, pRule->m_sComment );
00217 }
00218
00219 if ( nColumn >= 0 )
00220 {
00221 SetWindowLong( m_wndList.GetSafeHwnd(), GWL_USERDATA, 0 - nColumn - 1 );
00222 }
00223
00224 pLiveList.Apply( &m_wndList, bSort );
00225
00226 tLastUpdate = GetTickCount();
00227 }
00228
00229 CSecureRule* CSecurityWnd::GetItem(int nItem)
00230 {
00231 if ( m_wndList.GetItemState( nItem, LVIS_SELECTED ) )
00232 {
00233 CSecureRule* pRule = (CSecureRule*)m_wndList.GetItemData( nItem );
00234 if ( Security.Check( pRule ) ) return pRule;
00235 }
00236
00237 return NULL;
00238 }
00239
00241
00242
00243 void CSecurityWnd::OnSize(UINT nType, int cx, int cy)
00244 {
00245 CPanelWnd::OnSize( nType, cx, cy );
00246 m_pSizer.Resize( cx );
00247 m_wndList.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER );
00248 }
00249
00250 void CSecurityWnd::OnTimer(UINT nIDEvent)
00251 {
00252 if ( ( nIDEvent == 1 ) && ( IsPartiallyVisible() ) )
00253 {
00254 DWORD tTicks = GetTickCount();
00255 DWORD tDelay = max( ( 2 * Security.GetCount() ), 1000 );
00256
00257 if ( ( tTicks - tLastUpdate ) > tDelay )
00258 {
00259 if ( tDelay < 2000 ) Update();
00260 else Update( -1, FALSE );
00261 }
00262 }
00263 }
00264
00265 void CSecurityWnd::OnCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult)
00266 {
00267 NMLVCUSTOMDRAW* pDraw = (NMLVCUSTOMDRAW*)pNMHDR;
00268
00269 if ( pDraw->nmcd.dwDrawStage == CDDS_PREPAINT )
00270 {
00271 *pResult = CDRF_NOTIFYITEMDRAW;
00272 }
00273 else if ( pDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT )
00274 {
00275 LV_ITEM pItem;
00276 pItem.mask = LVIF_IMAGE;
00277 pItem.iItem = pDraw->nmcd.dwItemSpec;
00278 pItem.iSubItem = 0;
00279 m_wndList.GetItem( &pItem );
00280
00281 switch ( theApp.m_bRTL ? 2 - pItem.iImage : pItem.iImage )
00282 {
00283 case CSecureRule::srAccept:
00284 pDraw->clrText = RGB( 0, 127, 0 );
00285 break;
00286 case CSecureRule::srDeny:
00287 pDraw->clrText = RGB( 255, 0, 0 );
00288 break;
00289 }
00290
00291 *pResult = CDRF_DODEFAULT;
00292 }
00293 }
00294
00295 void CSecurityWnd::OnDblClkList(NMHDR* pNMHDR, LRESULT* pResult)
00296 {
00297 OnSecurityEdit();
00298 *pResult = 0;
00299 }
00300
00301 void CSecurityWnd::OnSortList(NMHDR* pNotifyStruct, LRESULT *pResult)
00302 {
00303 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNotifyStruct;
00304 CLiveList::Sort( &m_wndList, pNMListView->iSubItem );
00305 *pResult = 0;
00306 }
00307
00308 void CSecurityWnd::OnContextMenu(CWnd* pWnd, CPoint point)
00309 {
00310 TrackPopupMenu( _T("CSecurityWnd"), point, ID_SECURITY_EDIT );
00311 }
00312
00313 void CSecurityWnd::OnUpdateSecurityEdit(CCmdUI* pCmdUI)
00314 {
00315 pCmdUI->Enable( m_wndList.GetSelectedCount() == 1 );
00316 }
00317
00318 void CSecurityWnd::OnSecurityEdit()
00319 {
00320 CSingleLock pLock( &Network.m_pSection, TRUE );
00321
00322 CSecureRule* pRule = GetItem( m_wndList.GetNextItem( -1, LVIS_SELECTED ) );
00323 if ( ! pRule ) return;
00324
00325 pLock.Unlock();
00326
00327 CSecureRuleDlg dlg( NULL, pRule );
00328
00329 if ( dlg.DoModal() == IDOK )
00330 {
00331 Security.Save( TRUE );
00332 Update();
00333 }
00334 }
00335
00336 void CSecurityWnd::OnUpdateSecurityReset(CCmdUI* pCmdUI)
00337 {
00338 pCmdUI->Enable( m_wndList.GetSelectedCount() > 0 );
00339 }
00340
00341 void CSecurityWnd::OnSecurityReset()
00342 {
00343 CSingleLock pLock( &Network.m_pSection, TRUE );
00344
00345 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00346 {
00347 if ( CSecureRule* pRule = GetItem( nItem ) )
00348 {
00349 pRule->Reset();
00350 }
00351 }
00352
00353 Security.Save( TRUE );
00354 Update();
00355 }
00356
00357 void CSecurityWnd::OnUpdateSecurityRemove(CCmdUI* pCmdUI)
00358 {
00359 pCmdUI->Enable( m_wndList.GetSelectedCount() > 0 );
00360 }
00361
00362 void CSecurityWnd::OnSecurityRemove()
00363 {
00364 CSingleLock pLock( &Network.m_pSection, TRUE );
00365
00366 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00367 {
00368 if ( CSecureRule* pRule = GetItem( nItem ) )
00369 {
00370 Security.Remove( pRule );
00371 }
00372 }
00373
00374 Security.Save( TRUE );
00375 Update();
00376 }
00377
00378 void CSecurityWnd::OnUpdateSecurityMoveUp(CCmdUI* pCmdUI)
00379 {
00380 pCmdUI->Enable( m_wndList.GetSelectedCount() > 0 );
00381 }
00382
00383 void CSecurityWnd::OnSecurityMoveUp()
00384 {
00385 CSingleLock pLock( &Network.m_pSection, TRUE );
00386
00387 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00388 {
00389 if ( CSecureRule* pRule = GetItem( nItem ) )
00390 {
00391 Security.MoveUp( pRule );
00392 }
00393 }
00394
00395 Security.Save( TRUE );
00396 Update( 3 );
00397 }
00398
00399 void CSecurityWnd::OnUpdateSecurityMoveDown(CCmdUI* pCmdUI)
00400 {
00401 pCmdUI->Enable( m_wndList.GetSelectedCount() > 0 );
00402 }
00403
00404 void CSecurityWnd::OnSecurityMoveDown()
00405 {
00406 CSingleLock pLock( &Network.m_pSection, TRUE );
00407 CPtrList pList;
00408
00409 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00410 {
00411 pList.AddHead( GetItem( nItem ) );
00412 }
00413
00414 while ( pList.GetCount() )
00415 {
00416 CSecureRule* pRule = (CSecureRule*)pList.RemoveHead();
00417 if ( pRule ) Security.MoveDown( pRule );
00418 }
00419
00420 Security.Save( TRUE );
00421 Update( 3 );
00422 }
00423
00424 void CSecurityWnd::OnSecurityAdd()
00425 {
00426 CSecureRuleDlg dlg;
00427
00428 if ( dlg.DoModal() == IDOK )
00429 {
00430 Security.Save( TRUE );
00431 Update();
00432 }
00433 }
00434
00435 void CSecurityWnd::OnUpdateSecurityExport(CCmdUI* pCmdUI)
00436 {
00437 pCmdUI->Enable( m_wndList.GetSelectedCount() > 0 );
00438 }
00439
00440 void CSecurityWnd::OnSecurityExport()
00441 {
00442 CFileDialog dlg( FALSE, _T("xml"), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
00443 _T("XML Security Files|*.xml|NET Security Files|*.net|All Files|*.*||") );
00444
00445 if ( dlg.DoModal() != IDOK ) return;
00446
00447 CString strText;
00448 CFile pFile;
00449
00450 if ( ! pFile.Open( dlg.GetPathName(), CFile::modeWrite|CFile::modeCreate ) )
00451 {
00452
00453 AfxMessageBox( _T("Error") );
00454 return;
00455 }
00456
00457 CWaitCursor pCursor;
00458
00459 if ( dlg.GetFileExt().CompareNoCase( _T("net") ) == 0 )
00460 {
00461 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00462 {
00463 if ( CSecureRule* pRule = GetItem( nItem ) )
00464 {
00465 strText = pRule->ToGnucleusString();
00466
00467 if ( strText.GetLength() )
00468 {
00469 strText += _T("\r\n");
00470
00471 int nBytes = WideCharToMultiByte( CP_ACP, 0, strText, strText.GetLength(), NULL, 0, NULL, NULL );
00472 LPSTR pBytes = new CHAR[nBytes];
00473 WideCharToMultiByte( CP_ACP, 0, strText, strText.GetLength(), pBytes, nBytes, NULL, NULL );
00474 pFile.Write( pBytes, nBytes );
00475 delete [] pBytes;
00476
00477 }
00478 }
00479 }
00480 }
00481 else
00482 {
00483 CXMLElement* pXML = new CXMLElement( NULL, _T("security") );
00484
00485 pXML->AddAttribute( _T("xmlns"), CSecurity::xmlns );
00486
00487 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00488 {
00489 if ( CSecureRule* pRule = GetItem( nItem ) )
00490 {
00491 pXML->AddElement( pRule->ToXML() );
00492 }
00493 }
00494
00495 strText = pXML->ToString( TRUE, TRUE );
00496
00497 int nBytes = WideCharToMultiByte( CP_ACP, 0, strText, strText.GetLength(), NULL, 0, NULL, NULL );
00498 LPSTR pBytes = new CHAR[nBytes];
00499 WideCharToMultiByte( CP_ACP, 0, strText, strText.GetLength(), pBytes, nBytes, NULL, NULL );
00500 pFile.Write( pBytes, nBytes );
00501 delete [] pBytes;
00502
00503 delete pXML;
00504 }
00505
00506 pFile.Close();
00507 }
00508
00509 void CSecurityWnd::OnSecurityImport()
00510 {
00511 CFileDialog dlg( TRUE, _T("xml"), NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
00512 _T("Security Rules|*.xml;*.net|XML Files|*.xml|NET Files|*.net|All Files|*.*||") );
00513
00514 if ( dlg.DoModal() != IDOK ) return;
00515
00516 CWaitCursor pCursor;
00517
00518 if ( Security.Import( dlg.GetPathName() ) )
00519 {
00520 Security.Save( TRUE );
00521 }
00522 else
00523 {
00524
00525 AfxMessageBox( _T("Error") );
00526 }
00527 }
00528
00529 void CSecurityWnd::OnSkinChange()
00530 {
00531 CPanelWnd::OnSkinChange();
00532 Settings.LoadList( _T("CSecurityWnd"), &m_wndList, -4 );
00533 }
00534
00535 void CSecurityWnd::OnUpdateSecurityPolicyAccept(CCmdUI* pCmdUI)
00536 {
00537 pCmdUI->SetCheck( Security.m_bDenyPolicy == FALSE );
00538 }
00539
00540 void CSecurityWnd::OnSecurityPolicyAccept()
00541 {
00542 Security.m_bDenyPolicy = FALSE;
00543 Update();
00544 m_wndList.RedrawItems( 0, m_wndList.GetItemCount() - 1 );
00545 }
00546
00547 void CSecurityWnd::OnUpdateSecurityPolicyDeny(CCmdUI* pCmdUI)
00548 {
00549 pCmdUI->SetCheck( Security.m_bDenyPolicy == TRUE );
00550 }
00551
00552 void CSecurityWnd::OnSecurityPolicyDeny()
00553 {
00554 Security.m_bDenyPolicy = TRUE;
00555 Update();
00556 m_wndList.RedrawItems( 0, m_wndList.GetItemCount() - 1 );
00557 }
00558
00559
00560 BOOL CSecurityWnd::PreTranslateMessage(MSG* pMsg)
00561 {
00562 if ( pMsg->message == WM_KEYDOWN )
00563 {
00564 if ( GetAsyncKeyState( VK_CONTROL ) & 0x8000 )
00565 {
00566 if ( pMsg->wParam == VK_UP )
00567 {
00568 OnSecurityMoveUp();
00569 return TRUE;
00570 }
00571 else if ( pMsg->wParam == VK_DOWN )
00572 {
00573 OnSecurityMoveDown();
00574 return TRUE;
00575 }
00576 }
00577 else if ( pMsg->wParam == VK_DELETE )
00578 {
00579 OnSecurityRemove();
00580 return TRUE;
00581 }
00582 else if ( pMsg->wParam == VK_INSERT )
00583 {
00584 PostMessage( WM_COMMAND, ID_SECURITY_ADD );
00585 return TRUE;
00586 }
00587 }
00588
00589 return CPanelWnd::PreTranslateMessage( pMsg );
00590 }
00591