Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
9.3 KiB

  1. // EnumTestDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "EnumTest.h"
  5. #include "EnumTestDlg.h"
  6. #include "EnumVar.h"
  7. #include <sddl.h>
  8. #import "\bin\NetEnum.tlb" no_namespace
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16. class CAboutDlg : public CDialog
  17. {
  18. public:
  19. CAboutDlg();
  20. // Dialog Data
  21. //{{AFX_DATA(CAboutDlg)
  22. enum { IDD = IDD_ABOUTBOX };
  23. //}}AFX_DATA
  24. // ClassWizard generated virtual function overrides
  25. //{{AFX_VIRTUAL(CAboutDlg)
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  28. //}}AFX_VIRTUAL
  29. // Implementation
  30. protected:
  31. //{{AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{{AFX_DATA_INIT(CAboutDlg)
  38. //}}AFX_DATA_INIT
  39. }
  40. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CAboutDlg)
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  47. //{{AFX_MSG_MAP(CAboutDlg)
  48. // No message handlers
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CEnumTestDlg dialog
  53. CEnumTestDlg::CEnumTestDlg(CWnd* pParent /*=NULL*/)
  54. : CDialog(CEnumTestDlg::IDD, pParent)
  55. {
  56. //{{AFX_DATA_INIT(CEnumTestDlg)
  57. m_strContainer = _T("");
  58. m_strDomain = _T("");
  59. //}}AFX_DATA_INIT
  60. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. }
  63. void CEnumTestDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65. CDialog::DoDataExchange(pDX);
  66. //{{AFX_DATA_MAP(CEnumTestDlg)
  67. DDX_Control(pDX, IDC_LIST_MEMBERS, m_listBox);
  68. DDX_Text(pDX, IDC_EDIT_Container, m_strContainer);
  69. DDX_Text(pDX, IDC_EDIT_DOMAIN, m_strDomain);
  70. DDX_Text(pDX, IDC_EDIT_QUERY, m_strQuery);
  71. //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(CEnumTestDlg, CDialog)
  74. //{{AFX_MSG_MAP(CEnumTestDlg)
  75. ON_WM_SYSCOMMAND()
  76. ON_WM_PAINT()
  77. ON_WM_QUERYDRAGICON()
  78. ON_NOTIFY(NM_DBLCLK, IDC_LIST_MEMBERS, OnDblclkListMembers)
  79. ON_BN_CLICKED(IDC_BACKTRACK, OnBacktrack)
  80. //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CEnumTestDlg message handlers
  84. BOOL CEnumTestDlg::OnInitDialog()
  85. {
  86. CDialog::OnInitDialog();
  87. // Add "About..." menu item to system menu.
  88. // IDM_ABOUTBOX must be in the system command range.
  89. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  90. ASSERT(IDM_ABOUTBOX < 0xF000);
  91. CMenu* pSysMenu = GetSystemMenu(FALSE);
  92. if (pSysMenu != NULL)
  93. {
  94. CString strAboutMenu;
  95. strAboutMenu.LoadString(IDS_ABOUTBOX);
  96. if (!strAboutMenu.IsEmpty())
  97. {
  98. pSysMenu->AppendMenu(MF_SEPARATOR);
  99. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  100. }
  101. }
  102. // Set the icon for this dialog. The framework does this automatically
  103. // when the application's main window is not a dialog
  104. SetIcon(m_hIcon, TRUE); // Set big icon
  105. SetIcon(m_hIcon, FALSE); // Set small icon
  106. m_strContainer = L"OU=ShamTest";
  107. m_strDomain = L"devblewerg";
  108. m_strQuery = L"(objectClass=*)";
  109. UpdateData(FALSE);
  110. return TRUE; // return TRUE unless you set the focus to a control
  111. }
  112. void CEnumTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  113. {
  114. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  115. {
  116. CAboutDlg dlgAbout;
  117. dlgAbout.DoModal();
  118. }
  119. else
  120. {
  121. CDialog::OnSysCommand(nID, lParam);
  122. }
  123. }
  124. // If you add a minimize button to your dialog, you will need the code below
  125. // to draw the icon. For MFC applications using the document/view model,
  126. // this is automatically done for you by the framework.
  127. void CEnumTestDlg::OnPaint()
  128. {
  129. if (IsIconic())
  130. {
  131. CPaintDC dc(this); // device context for painting
  132. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  133. // Center icon in client rectangle
  134. int cxIcon = GetSystemMetrics(SM_CXICON);
  135. int cyIcon = GetSystemMetrics(SM_CYICON);
  136. CRect rect;
  137. GetClientRect(&rect);
  138. int x = (rect.Width() - cxIcon + 1) / 2;
  139. int y = (rect.Height() - cyIcon + 1) / 2;
  140. // Draw the icon
  141. dc.DrawIcon(x, y, m_hIcon);
  142. }
  143. else
  144. {
  145. CDialog::OnPaint();
  146. }
  147. }
  148. // The system calls this to obtain the cursor to display while the user drags
  149. // the minimized window.
  150. HCURSOR CEnumTestDlg::OnQueryDragIcon()
  151. {
  152. return (HCURSOR) m_hIcon;
  153. }
  154. void CEnumTestDlg::OnOK()
  155. {
  156. /* m_listBox.DeleteAllItems();
  157. UpdateData();
  158. INetObjEnumeratorPtr pNetObj(__uuidof(NetObjEnumerator));
  159. BSTR sContName = m_strContainer.AllocSysString();
  160. BSTR sDomain = m_strDomain.AllocSysString();
  161. IEnumVARIANT * pEnum;
  162. HRESULT hr;
  163. VARIANT varEnum;
  164. ULONG ulFetch = 0;
  165. VariantInit(&varEnum);
  166. m_listBox.InsertItem(0, "====================");
  167. hr = pNetObj->raw_GetContainerEnum( sContName, sDomain, &pEnum);
  168. if ( FAILED(hr) )
  169. {
  170. m_listBox.InsertItem(0, "Invalid Container");
  171. return;
  172. }
  173. ::SysFreeString(sContName);
  174. ::SysFreeString(sDomain);
  175. hr = S_OK;
  176. SAttrInfo sInfo;
  177. CString disp;
  178. long flag = 15; // Get all four values.
  179. if (pEnum)
  180. {
  181. CEnumVar enumVar(pEnum);
  182. while ( enumVar.Next(flag, &sInfo) )
  183. {
  184. disp.Format("%ls<%ls>", sInfo.sName, sInfo.sClass);
  185. m_listBox.InsertItem(0, disp);
  186. }
  187. ADsFreeEnumerator(pEnum);
  188. }
  189. */
  190. m_listBox.DeleteAllItems();
  191. bool bFullPath;
  192. UpdateData();
  193. BSTR sContName = m_strContainer.AllocSysString();
  194. if ( m_strDomain.Left(5) == L"GC://" )
  195. bFullPath = true;
  196. BSTR sDomain = m_strDomain.AllocSysString();
  197. BSTR sQuery = m_strQuery.AllocSysString();
  198. INetObjEnumeratorPtr pNetObj(__uuidof(NetObjEnumerator));
  199. IEnumVARIANT * pEnum;
  200. DWORD ulFet=0;
  201. _variant_t var, var2;
  202. _variant_t * pVars;
  203. CString sX;
  204. CString strDisp;
  205. BSTR HUGEP * pData;
  206. // BSTR pBSTR[] = { L"name", L"accountExpires", L"sAMAccountName", L"objectClass", L"objectSID", L"ou", L"cn" };
  207. BSTR pBSTR[] = { L"name", L"dc"};
  208. long ind = sizeof(pBSTR)/sizeof(BSTR);
  209. SAFEARRAYBOUND b = { ind, 0 };
  210. SAFEARRAY * pArray = NULL;
  211. pArray = ::SafeArrayCreate(VT_BSTR, 1, &b);
  212. ::SafeArrayAccessData(pArray, (void HUGEP **)&pData);
  213. for (long i = 0; i < ind; i++)
  214. pData[i] = pBSTR[i];
  215. ::SafeArrayUnaccessData(pArray);
  216. try
  217. {
  218. // pNetObj->SetQuery(sContName, sDomain, sQuery, ADS_SCOPE_ONELEVEL );
  219. pNetObj->SetQuery(sContName, sDomain, sQuery, ADS_SCOPE_SUBTREE );
  220. pNetObj->SetColumns((long) pArray);
  221. pNetObj->Execute(&pEnum);
  222. }
  223. catch (const _com_error &e)
  224. {
  225. ::AfxMessageBox(e.ErrorMessage());
  226. return;
  227. }
  228. if (pEnum)
  229. {
  230. while (pEnum->Next(1, &var, &ulFet) == S_OK)
  231. {
  232. if ( ulFet )
  233. {
  234. pArray = var.parray;
  235. long ub, lb;
  236. ::SafeArrayGetUBound(pArray, 1, &ub);
  237. ::SafeArrayGetLBound(pArray, 1, &lb);
  238. ::SafeArrayAccessData(pArray, (void HUGEP **)&pVars);
  239. // for ( long x = lb; x <= ub - 2; x++)
  240. for ( long x = lb; x <= ub ; x++)
  241. {
  242. if ( x > lb )
  243. {
  244. if ( pVars[x].vt == VT_BSTR)
  245. strDisp = strDisp + "<" + CString(pVars[x].bstrVal) + ">";
  246. else
  247. if ( pVars[x].vt == (VT_ARRAY | VT_UI4) )
  248. // Octet string
  249. strDisp = strDisp + "<" + CString(GetSidFromVar(pVars[x])) + ">";
  250. else
  251. {
  252. // an integer
  253. sX.Format("%s<%d>", strDisp, pVars[x].lVal);
  254. strDisp = sX;
  255. }
  256. }
  257. else
  258. {
  259. if ( !CString(pVars[ub-1].bstrVal).IsEmpty() )
  260. strDisp = "OU=";
  261. else
  262. // it is a CN
  263. strDisp = "CN=";
  264. strDisp = strDisp + CString(pVars[x].bstrVal);
  265. }
  266. }
  267. m_listBox.InsertItem(0, strDisp);
  268. ::SafeArrayUnaccessData(pArray);
  269. }
  270. }
  271. }
  272. ::SysFreeString(sDomain);
  273. ::SysFreeString(sQuery);
  274. ::SysFreeString(sContName);
  275. }
  276. void CEnumTestDlg::OnDblclkListMembers(NMHDR* pNMHDR, LRESULT* pResult)
  277. {
  278. int len = m_strContainer.GetLength();
  279. UpdateData();
  280. CString str = m_listBox.GetItemText(m_listBox.GetSelectionMark(),0);
  281. str = str.Left(str.Find("<"));
  282. if ( len )
  283. m_strContainer = str + "," + m_strContainer;
  284. else
  285. m_strContainer = str;
  286. UpdateData(FALSE);
  287. OnOK();
  288. *pResult = 0;
  289. }
  290. void CEnumTestDlg::OnBacktrack()
  291. {
  292. UpdateData();
  293. int ndx = m_strContainer.Find(",");
  294. if ( ndx != -1 )
  295. m_strContainer = m_strContainer.Mid(ndx + 1);
  296. else
  297. m_strContainer = "";
  298. UpdateData(FALSE);
  299. OnOK();
  300. }
  301. char * CEnumTestDlg::GetSidFromVar(_variant_t var)
  302. {
  303. void HUGEP *pArray;
  304. PSID pSid;
  305. char * sSid;
  306. HRESULT hr = SafeArrayAccessData( V_ARRAY(&var), &pArray );
  307. if ( SUCCEEDED(hr) )
  308. pSid = (PSID)pArray;
  309. ::ConvertSidToStringSid(pSid, &sSid);
  310. hr = ::SafeArrayUnaccessData(V_ARRAY(&var));
  311. return sSid;
  312. }