home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / General / ADQI / security.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-01  |  8.8 KB  |  416 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. //  Microsoft Active Directory 2.5 Sample Code
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. //  File:       security.cpp
  8. //
  9. //  Contents:   IADsSecurityDescriptor, IADsAccessControlList, IADsAccessControlEntry usage
  10. //
  11. //
  12. //----------------------------------------------------------------------------
  13.  
  14. #include "stdafx.h"
  15. #include "ADQI.h"
  16. #include "directorysearch.h"
  17. #include "security.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CDlgIADsSecurityDescriptor dialog
  27.  
  28.  
  29. CDlgIADsSecurityDescriptor::CDlgIADsSecurityDescriptor(LPUNKNOWN pUnk, CWnd* pParent /*=NULL*/)
  30.     : CDialog(CDlgIADsSecurityDescriptor::IDD, pParent)
  31. {
  32.     //{{AFX_DATA_INIT(CDlgIADsSecurityDescriptor)
  33.     //}}AFX_DATA_INIT
  34.  
  35.  
  36.     
  37.     HRESULT hr;    
  38.     m_pSecurityDesc = NULL;
  39.  
  40.     
  41.     hr = pUnk->QueryInterface( IID_IADsSecurityDescriptor, (void **) &m_pSecurityDesc );
  42.     pUnk->Release();
  43.  
  44.     if ( !SUCCEEDED(hr) )
  45.     {
  46.         AfxMessageBox(_T("Fatal Error! QI for IADsSecurityDescriptor failed"));
  47.         return;
  48.     }
  49.     
  50.     
  51.  
  52. }
  53.  
  54. CDlgIADsSecurityDescriptor::~CDlgIADsSecurityDescriptor()
  55. {
  56.     ResetAcePtr();
  57.     if ( m_pSecurityDesc )
  58.     {
  59.         m_pSecurityDesc->Release();
  60.     }
  61.     
  62. }
  63.  
  64.  
  65. void CDlgIADsSecurityDescriptor::DoDataExchange(CDataExchange* pDX)
  66. {
  67.     CDialog::DoDataExchange(pDX);
  68.     //{{AFX_DATA_MAP(CDlgIADsSecurityDescriptor)
  69.     DDX_Control(pDX, IDC_ACELIST, m_cACEList);
  70.     //}}AFX_DATA_MAP
  71. }
  72.  
  73.  
  74. BEGIN_MESSAGE_MAP(CDlgIADsSecurityDescriptor, CDialog)
  75.     //{{AFX_MSG_MAP(CDlgIADsSecurityDescriptor)
  76.     ON_LBN_SELCHANGE(IDC_ACELIST, OnSelChangeAceList)
  77.     //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CDlgIADsSecurityDescriptor message handlers
  82.  
  83. void CDlgIADsSecurityDescriptor::PopulateACL(IADsAccessControlList * pACL)
  84. {
  85.     ASSERT( pACL );
  86.     HRESULT hr;
  87.     ULONG   lFetch;
  88.  
  89.     IEnumVARIANT *pEnum;
  90.     LPUNKNOWN     pUnk;
  91.     VARIANT       var;
  92.     IDispatch     *pDisp;
  93.     BSTR          bstr;
  94.     CString          s;
  95.  
  96.     IADsAccessControlEntry *pACE;
  97.     
  98.     
  99.     m_cACEList.ResetContent();
  100.     ResetAcePtr();
  101.  
  102.     
  103.     hr = pACL->get__NewEnum( &pUnk );
  104.     if ( !SUCCEEDED(hr) )
  105.     {
  106.         return;
  107.     }
  108.  
  109.     hr = pUnk->QueryInterface( IID_IEnumVARIANT, (void**) &pEnum );
  110.     if ( !SUCCEEDED(hr) )
  111.     {
  112.         return;
  113.     }
  114.  
  115.     hr = pEnum->Next( 1, &var, &lFetch );
  116.     
  117.     while( hr == S_OK )
  118.     {        
  119.         if ( lFetch == 1 )
  120.         {
  121.             if ( VT_DISPATCH != V_VT(&var) )
  122.             {
  123.                 break;
  124.             }
  125.         
  126.  
  127.                pDisp = V_DISPATCH(&var);
  128.             ///////////////////////////
  129.             // Get the ACE
  130.             /////////////////////////////
  131.             hr = pDisp->QueryInterface( IID_IADsAccessControlEntry, (void**)&pACE ); 
  132.             if ( SUCCEEDED(hr) )
  133.             {
  134.                 pACE->get_Trustee(&bstr);
  135.                 s = bstr;
  136.                 SysFreeString(bstr);
  137.                 m_cACEList.AddString( s );
  138.                 m_acePtrList.AddTail( pACE ); //save the pointer for future use, 
  139.                                            // we don't need to Release() it.
  140.  
  141.             }
  142.  
  143.             VariantClear(&var);
  144.         }
  145.         hr = pEnum->Next( 1, &var, &lFetch );
  146.         
  147.     };
  148.  
  149.  
  150.  
  151.     pACL->Release();
  152. }
  153.  
  154.  
  155. void CDlgIADsSecurityDescriptor::ResetAcePtr()
  156. {
  157.     POSITION pos;
  158.     IADsAccessControlList *pACE;
  159.     pos = m_acePtrList.GetHeadPosition();
  160.     while( pos != NULL )
  161.     {
  162.         pACE = (IADsAccessControlList*) m_acePtrList.GetAt( pos );
  163.         pACE->Release();
  164.         m_acePtrList.GetNext( pos );
  165.     }
  166.     m_acePtrList.RemoveAll();
  167. }
  168.  
  169.  
  170. BOOL CDlgIADsSecurityDescriptor::OnInitDialog() 
  171. {
  172.     CDialog::OnInitDialog();
  173.  
  174.     if ( m_pSecurityDesc == NULL )
  175.     {
  176.         return TRUE;
  177.     }
  178.     
  179.     
  180.     /////////////////////////////////////////////
  181.     // Populate the DACL 
  182.     ////////////////////////////////////////////
  183.     HRESULT hr;
  184.     IDispatch *pDisp;
  185.  
  186.     IADsAccessControlList *pACL;
  187.     hr = m_pSecurityDesc->get_DiscretionaryAcl( &pDisp );
  188.     if (!SUCCEEDED(hr) )
  189.     {
  190.         AfxMessageBox(GetErrorMessage(hr));
  191.         return TRUE;
  192.     }
  193.  
  194.  
  195.     hr = pDisp->QueryInterface( IID_IADsAccessControlList, (void**) &pACL );
  196.     pDisp->Release();
  197.  
  198.     if ( SUCCEEDED(hr) )
  199.     {
  200.         PopulateACL( pACL );
  201.     }
  202.  
  203.     
  204.     return TRUE;  // return TRUE unless you set the focus to a control
  205.                   // EXCEPTION: OCX Property Pages should return FALSE
  206. }
  207.  
  208. void CDlgIADsSecurityDescriptor::OnSelChangeAceList() 
  209. {
  210.     int idx=0;
  211.     
  212.     ///////////////////////////////////////////////////
  213.     // Seach the ACEs
  214.     ////////////////////////////////////////////////
  215.     idx = m_cACEList.GetCurSel();
  216.     if ( idx == LB_ERR )
  217.     {
  218.         return;
  219.     }
  220.  
  221.     POSITION pos = m_acePtrList.FindIndex( idx );
  222.     IADsAccessControlEntry *pACE = NULL;
  223.     if ( !pos )
  224.     {
  225.         return;
  226.     }
  227.  
  228.     ///////////////////////////////////////////////////
  229.     // Now we can disply the ACE characteristics
  230.     ////////////////////////////////////////////////////
  231.     pACE = (IADsAccessControlEntry *) m_acePtrList.GetAt( pos );
  232.     if ( pACE )
  233.     {
  234.         pACE->AddRef();
  235.         DisplayACE( pACE );
  236.     }
  237. }
  238.  
  239.  
  240.  
  241. void CDlgIADsSecurityDescriptor::DisplayACE( IADsAccessControlEntry *pACE )
  242. {
  243.     LONG aceMask;
  244.     LONG aceType;
  245.     int idx;
  246.     BSTR bstr;
  247.     CString sObjectType;
  248.  
  249.     ASSERT( pACE );
  250.  
  251.     ///////////////////////////////////////////////////
  252.     //    Get Access Mask, Ace Type and Object Type
  253.     ////////////////////////////////////////////////////
  254.     if( !SUCCEEDED(pACE->get_AccessMask(&aceMask)) )
  255.     {
  256.         return;
  257.     }
  258.     if ( !SUCCEEDED(pACE->get_AceType(&aceType)) )
  259.     {
  260.         return;
  261.     }
  262.     
  263.     if ( !SUCCEEDED(pACE->get_ObjectType(&bstr)) )
  264.     {
  265.         return;
  266.     }
  267.  
  268.     sObjectType = bstr;
  269.     SysFreeString( bstr );
  270.  
  271.     
  272.  
  273.     //////////////////////////////////////
  274.     // Display the type
  275.     //////////////////////////////////////
  276.     int aceTypeList[] = 
  277.     {
  278.         ADS_ACETYPE_ACCESS_ALLOWED,
  279.         ADS_ACETYPE_ACCESS_DENIED,
  280.         ADS_ACETYPE_ACCESS_ALLOWED_OBJECT,
  281.         ADS_ACETYPE_ACCESS_DENIED_OBJECT,
  282.         ADS_ACETYPE_SYSTEM_AUDIT,
  283.         ADS_ACETYPE_SYSTEM_AUDIT_OBJECT,
  284.     };
  285.  
  286.  
  287.     int sel = -1;
  288.     for( idx=0; idx < (sizeof(aceTypeList)/sizeof(int)); idx++ )
  289.     {
  290.         if ( aceType  == aceTypeList[idx] )
  291.         {
  292.             sel = idx;
  293.             break;
  294.         }
  295.     }
  296.     ((CComboBox*)GetDlgItem(IDC_ACETYPE))->SetCurSel( sel );
  297.     
  298.     /////////////////////////////////////
  299.     // Display the aces mask
  300.     /////////////////////////////////////
  301.     ResetAceMaskControls();    // Reset the UI
  302.  
  303.     // Standard ACE Rights
  304.     if ( aceMask &     ADS_RIGHT_DELETE ) 
  305.     {
  306.         CheckDlgButton( IDC_DELETEOBJECT, TRUE );
  307.     }
  308.  
  309.     if ( aceMask & ADS_RIGHT_READ_CONTROL )
  310.     {
  311.         CheckDlgButton( IDC_READPERMISSION, TRUE );
  312.     }
  313.  
  314.     if ( aceMask & ADS_RIGHT_WRITE_DAC )
  315.     {
  316.         CheckDlgButton( IDC_MODIFYPERMISSION, TRUE );
  317.     }
  318.  
  319.     if ( aceMask & ADS_RIGHT_WRITE_OWNER )
  320.     {
  321.         CheckDlgButton( IDC_MODIFYOWNER, TRUE );
  322.     }
  323.  
  324.  
  325.     // Directory ACE Rights
  326.     if ( aceMask & ADS_RIGHT_DS_CREATE_CHILD )
  327.     {
  328.         CheckDlgButton( IDC_CREATECHILD, TRUE );
  329.         DisplayAceObjectType( IDC_CREATECHILD_EDIT, sObjectType );
  330.     }
  331.  
  332.     if ( aceMask & ADS_RIGHT_DS_DELETE_CHILD )
  333.     {
  334.        CheckDlgButton( IDC_DELETECHILD, TRUE );
  335.        DisplayAceObjectType( IDC_DELETECHILD_EDIT, sObjectType );
  336.     }
  337.  
  338.     if ( aceMask & ADS_RIGHT_ACTRL_DS_LIST )
  339.     {
  340.        CheckDlgButton( IDC_LISTCONTENT, TRUE );
  341.     }
  342.  
  343.     if ( aceMask & ADS_RIGHT_DS_SELF )
  344.     {
  345.         CheckDlgButton( IDC_LISTOBJECT, TRUE );
  346.     }
  347.  
  348.     if ( aceMask & ADS_RIGHT_DS_DELETE_TREE )
  349.     {
  350.         CheckDlgButton( IDC_DELETETREE, TRUE );
  351.     }
  352.  
  353.     if ( aceMask & ( ADS_RIGHT_DS_READ_PROP  |  ADS_RIGHT_DS_WRITE_PROP | 0x100) &&
  354.          ( (aceType == ADS_ACETYPE_ACCESS_ALLOWED_OBJECT)  | 
  355.            (aceType == ADS_ACETYPE_ACCESS_DENIED_OBJECT)  ) 
  356.        )
  357.     {
  358.         CheckDlgButton( IDC_EXTENDEDRIGHT, TRUE );
  359.         DisplayAceObjectType( IDC_EXTENDEDRIGHT_EDIT, sObjectType );
  360.  
  361.     }
  362.     else
  363.     {
  364.         if ( aceMask & ADS_RIGHT_DS_READ_PROP )
  365.         {
  366.             CheckDlgButton( IDC_READPROP, TRUE );
  367.             DisplayAceObjectType( IDC_READPROP_EDIT, sObjectType );
  368.         }
  369.  
  370.         if ( aceMask & ADS_RIGHT_DS_WRITE_PROP )
  371.         {
  372.             CheckDlgButton( IDC_WRITEPROP, TRUE );
  373.             DisplayAceObjectType( IDC_WRITEPROP_EDIT, sObjectType );
  374.         }
  375.     }
  376.  
  377.  
  378.  
  379.  
  380.       
  381. }
  382.  
  383.  
  384. void CDlgIADsSecurityDescriptor::DisplayAceObjectType( UINT nID, CString &sObjectType )
  385. {
  386.     if ( sObjectType.IsEmpty() )
  387.     {
  388.         SetDlgItemText( nID, _T("ALL") );
  389.     }
  390.     else
  391.     {
  392.         SetDlgItemText( nID, sObjectType );
  393.     }
  394.  
  395.  
  396. }
  397. void CDlgIADsSecurityDescriptor::ResetAceMaskControls()
  398. {
  399.    CheckDlgButton( IDC_DELETEOBJECT, FALSE );
  400.    CheckDlgButton( IDC_READPERMISSION, FALSE );
  401.    CheckDlgButton( IDC_CREATECHILD, FALSE );
  402.    CheckDlgButton( IDC_DELETECHILD, FALSE );
  403.    CheckDlgButton( IDC_LISTCONTENT, FALSE );
  404.    CheckDlgButton( IDC_LISTOBJECT, FALSE );
  405.    CheckDlgButton( IDC_DELETETREE, FALSE );
  406.    CheckDlgButton( IDC_READPROP, FALSE );
  407.    CheckDlgButton( IDC_WRITEPROP, FALSE );
  408.    
  409.    SetDlgItemText( IDC_CREATECHILD_EDIT, _T("") );
  410.    SetDlgItemText( IDC_DELETECHILD_EDIT, _T("") );
  411.    SetDlgItemText( IDC_READPROP_EDIT, _T("") );
  412.    SetDlgItemText( IDC_WRITEPROP_EDIT, _T("") );
  413.    SetDlgItemText( IDC_EXTENDEDRIGHT_EDIT, _T("") );
  414.  
  415. }
  416.