Leaked source code of windows server 2003
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.

2802 lines
85 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000 - 2001.
  5. //
  6. // File: headers.h
  7. //
  8. // Contents:
  9. //
  10. // History: 07-26-2001 Hiteshr Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "headers.h"
  14. //+----------------------------------------------------------------------------
  15. // Function:IsValidStoreType
  16. // Synopsis:Vaildates the store type
  17. //-----------------------------------------------------------------------------
  18. BOOL IsValidStoreType(ULONG lStoreType)
  19. {
  20. if((lStoreType == AZ_ADMIN_STORE_XML) ||
  21. (lStoreType == AZ_ADMIN_STORE_AD))
  22. return TRUE;
  23. return FALSE;
  24. }
  25. //+----------------------------------------------------------------------------
  26. // Function: AddColumnToListView
  27. // Synopsis: Add Columns to Listview and set column width according to
  28. // percentage specified in the COL_FOR_LV
  29. // Arguments:IN pListCtrl: ListCtrl pointer
  30. // IN pColForLV: Column Infomration Array
  31. //
  32. // Returns:
  33. //-----------------------------------------------------------------------------
  34. VOID
  35. AddColumnToListView(IN CListCtrl* pListCtrl,
  36. IN COL_FOR_LV* pColForLV)
  37. {
  38. if(!pListCtrl || !pColForLV)
  39. {
  40. ASSERT(pListCtrl);
  41. ASSERT(pColForLV);
  42. }
  43. UINT iTotal = 0;
  44. RECT rc;
  45. pListCtrl->GetClientRect(&rc);
  46. for( UINT iCol = 0; pColForLV[iCol].idText != LAST_COL_ENTRY_IDTEXT; ++iCol)
  47. {
  48. CString strHeader;
  49. VERIFY(strHeader.LoadString(pColForLV[iCol].idText));
  50. LV_COLUMN col;
  51. col.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
  52. col.fmt = LVCFMT_LEFT;
  53. col.pszText = (LPWSTR)(LPCTSTR)strHeader;
  54. col.iSubItem = iCol;
  55. col.cx = (rc.right * pColForLV[iCol].iPercent) / 100;
  56. pListCtrl->InsertColumn(iCol,&col);
  57. iTotal += col.cx;
  58. }
  59. }
  60. //+----------------------------------------------------------------------------
  61. // Function: BaseAzInListCtrl
  62. // Synopsis: Checks if Object of type eObjectTypeAz named strName is
  63. // in the Listview. If its present, returns
  64. // its index else returns -1.
  65. // Arguments:IN pListCtrl: ListCtrl pointer
  66. // IN strName: string to search for
  67. // IN eObjectTypeAz Compare the object of this type only
  68. //
  69. // Returns: If its present, returns its index else returns -1
  70. //-----------------------------------------------------------------------------
  71. int
  72. BaseAzInListCtrl(IN CListCtrl* pListCtrl,
  73. IN const CString& strName,
  74. IN OBJECT_TYPE_AZ eObjectTypeAz)
  75. {
  76. if(!pListCtrl)
  77. {
  78. ASSERT(pListCtrl);
  79. return -1;
  80. }
  81. int iCount = pListCtrl->GetItemCount();
  82. for( int i = 0; i < iCount; ++i)
  83. {
  84. CBaseAz* pBaseAz = (CBaseAz*)pListCtrl->GetItemData(i);
  85. if(pBaseAz)
  86. {
  87. if((pBaseAz->GetObjectType() == eObjectTypeAz) &&
  88. (pBaseAz->GetName() == strName))
  89. return i;
  90. }
  91. }
  92. return -1;
  93. }
  94. //+----------------------------------------------------------------------------
  95. // Function: AddBaseAzFromListToListCtrl
  96. // Synopsis: Take the items from List and Add them to ListCtrl. Doesn't
  97. // add the items which are already in ListCtrl
  98. // Arguments:listBaseAz: List of items
  99. // pListCtrl: ListControl Pointer
  100. // uiFlags : Column Info
  101. // bCheckDuplicate: Check for duplicate items
  102. // Returns: The index of the new item if it is successful; otherwise, -1
  103. // NOTE: Function assume the Order of column is Name, Type and Description
  104. //-----------------------------------------------------------------------------
  105. void
  106. AddBaseAzFromListToListCtrl(IN CList<CBaseAz*, CBaseAz*>& listBaseAz,
  107. IN CListCtrl* pListCtrl,
  108. IN UINT uiFlags,
  109. IN BOOL bCheckDuplicate)
  110. {
  111. //Remember index of selected item
  112. int iFirstSelectedItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
  113. POSITION pos = listBaseAz.GetHeadPosition();
  114. for (int i = 0; i < listBaseAz.GetCount(); i++)
  115. {
  116. CBaseAz* pBaseAz = listBaseAz.GetNext(pos);
  117. //Check if item is in ListControl
  118. if(!bCheckDuplicate ||
  119. BaseAzInListCtrl(pListCtrl,
  120. pBaseAz->GetName(),
  121. pBaseAz->GetObjectType()) == -1)
  122. {
  123. VERIFY( AddBaseAzToListCtrl(pListCtrl,
  124. pListCtrl->GetItemCount(),
  125. pBaseAz,
  126. uiFlags) != -1);
  127. }
  128. }
  129. //Restore Selection
  130. if(pListCtrl->GetItemCount() <= iFirstSelectedItem)
  131. --iFirstSelectedItem;
  132. SelectListCtrlItem(pListCtrl, iFirstSelectedItem);
  133. }
  134. //+----------------------------------------------------------------------------
  135. // Function: AddActionItemFromListToListCtrl
  136. // Synopsis: Take the Actions items from List and Add them to ListCtrl.
  137. // Arguments:listBaseAz: List of items
  138. // pListCtrl: ListControl Pointer
  139. // uiFlags : Column Info
  140. // bCheckDuplicate: Check for duplicate items
  141. // Returns: The index of the new item if it is successful; otherwise, -1
  142. // NOTE: Function assume the Order of column is Name, Type and Description
  143. //-----------------------------------------------------------------------------
  144. void
  145. AddActionItemFromListToListCtrl(IN CList<ActionItem*, ActionItem*>& listActionItem,
  146. IN CListCtrl* pListCtrl,
  147. IN UINT uiFlags,
  148. IN BOOL bCheckDuplicate)
  149. {
  150. //Remember index of selected item
  151. int iFirstSelectedItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
  152. POSITION pos = listActionItem.GetHeadPosition();
  153. for (int i = 0; i < listActionItem.GetCount(); i++)
  154. {
  155. ActionItem* pActionItem = listActionItem.GetNext(pos);
  156. if(pActionItem->action == ACTION_REMOVE ||
  157. pActionItem->action == ACTION_REMOVED)
  158. continue;
  159. //Check if item is in ListControl
  160. if(!bCheckDuplicate ||
  161. BaseAzInListCtrl(pListCtrl,
  162. (pActionItem->m_pMemberAz)->GetName(),
  163. (pActionItem->m_pMemberAz)->GetObjectType()) == -1)
  164. {
  165. VERIFY( AddActionItemToListCtrl(pListCtrl,
  166. pListCtrl->GetItemCount(),
  167. pActionItem,
  168. uiFlags) != -1);
  169. }
  170. }
  171. //Restore Selection
  172. if(pListCtrl->GetItemCount() <= iFirstSelectedItem)
  173. --iFirstSelectedItem;
  174. SelectListCtrlItem(pListCtrl, iFirstSelectedItem);
  175. }
  176. //+----------------------------------------------------------------------------
  177. // Function: AddActionItemFromMapToListCtrl
  178. // Synopsis: Take the Actions items from Map and Add them to ListCtrl.
  179. // Arguments:listBaseAz: List of items
  180. // pListCtrl: ListControl Pointer
  181. // uiFlags : Column Info
  182. // bCheckDuplicate: Check for duplicate items
  183. // Returns: The index of the new item if it is successful; otherwise, -1
  184. // NOTE: Function assume the Order of column is Name, Type and Description
  185. //-----------------------------------------------------------------------------
  186. void
  187. AddActionItemFromMapToListCtrl(IN ActionMap& mapActionItem,
  188. IN CListCtrl* pListCtrl,
  189. IN UINT uiFlags,
  190. IN BOOL bCheckDuplicate)
  191. {
  192. //Remember index of selected item
  193. int iFirstSelectedItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
  194. for (ActionMap::iterator it = mapActionItem.begin();
  195. it != mapActionItem.end();
  196. ++it)
  197. {
  198. ActionItem* pActionItem = (*it).second;
  199. if(pActionItem->action == ACTION_REMOVE ||
  200. pActionItem->action == ACTION_REMOVED)
  201. continue;
  202. //Check if item is in ListControl
  203. if(!bCheckDuplicate ||
  204. BaseAzInListCtrl(pListCtrl,
  205. (pActionItem->m_pMemberAz)->GetName(),
  206. (pActionItem->m_pMemberAz)->GetObjectType()) == -1)
  207. {
  208. VERIFY( AddActionItemToListCtrl(pListCtrl,
  209. pListCtrl->GetItemCount(),
  210. pActionItem,
  211. uiFlags) != -1);
  212. }
  213. }
  214. //Restore Selection
  215. if(pListCtrl->GetItemCount() <= iFirstSelectedItem)
  216. --iFirstSelectedItem;
  217. SelectListCtrlItem(pListCtrl, iFirstSelectedItem);
  218. }
  219. //+----------------------------------------------------------------------------
  220. // Function: AddBaseAzToListCtrl
  221. // Synopsis: Adds a new item to ListCtrl.
  222. // Arguments:pListCtrl: ListControl Pointer
  223. // iIndex: Index at which to Add
  224. // pBaseAz: Item to add
  225. // uiFlags: column info
  226. // Returns: The index of the new item if it is successful; otherwise, -1
  227. //-----------------------------------------------------------------------------
  228. int
  229. AddBaseAzToListCtrl(IN CListCtrl* pListCtrl,
  230. IN int iIndex,
  231. IN CBaseAz* pBaseAz,
  232. IN UINT uiFlags)
  233. {
  234. if(!pListCtrl || !pBaseAz)
  235. {
  236. ASSERT(pListCtrl);
  237. ASSERT(pBaseAz);
  238. return -1;
  239. }
  240. //Add Name and Item Data
  241. CString strName = pBaseAz->GetName();
  242. int iNewIndex = pListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE|LVIF_PARAM|LVIF_IMAGE,
  243. iIndex,strName,0,0,
  244. pBaseAz->GetImageIndex(),
  245. (LPARAM)pBaseAz);
  246. if(iNewIndex == -1)
  247. return iNewIndex;
  248. int iCol = 1;
  249. if(uiFlags & COL_TYPE )
  250. {
  251. CString strType = pBaseAz->GetType();
  252. pListCtrl->SetItemText(iNewIndex,
  253. iCol,
  254. (LPWSTR)(LPCTSTR)strType);
  255. iCol++;
  256. }
  257. if(uiFlags & COL_PARENT_TYPE)
  258. {
  259. CString strParentType = pBaseAz->GetParentType();
  260. pListCtrl->SetItemText(iNewIndex,
  261. iCol,
  262. (LPWSTR)(LPCTSTR)strParentType);
  263. iCol++;
  264. }
  265. if(uiFlags & COL_DESCRIPTION)
  266. {
  267. //Add Description
  268. CString strDesc = pBaseAz->GetDescription();
  269. pListCtrl->SetItemText(iNewIndex,
  270. iCol,
  271. (LPWSTR)(LPCTSTR)strDesc);
  272. }
  273. return iNewIndex;
  274. }
  275. //+----------------------------------------------------------------------------
  276. // Function: AddActionItemToListCtrl
  277. // Synopsis: Adds a new item to ListCtrl.
  278. // Arguments:pListCtrl: ListControl Pointer
  279. // iIndex: Index at which to Add
  280. // pActionItem: Item to add
  281. // uiFlags: column info
  282. // Returns: The index of the new item if it is successful; otherwise, -1
  283. //-----------------------------------------------------------------------------
  284. int
  285. AddActionItemToListCtrl(IN CListCtrl* pListCtrl,
  286. IN int iIndex,
  287. IN ActionItem* pActionItem,
  288. IN UINT uiFlags)
  289. {
  290. if(!pListCtrl || !pActionItem)
  291. {
  292. ASSERT(pListCtrl);
  293. ASSERT(pActionItem);
  294. return -1;
  295. }
  296. CBaseAz* pBaseAz = pActionItem->m_pMemberAz;
  297. //Add Name and Item Data
  298. CString strName = pBaseAz->GetName();
  299. int iNewIndex = pListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE|LVIF_PARAM|LVIF_IMAGE,
  300. iIndex,
  301. strName,
  302. 0,0,
  303. pBaseAz->GetImageIndex(),
  304. (LPARAM)pActionItem);
  305. if(iNewIndex == -1)
  306. return iNewIndex;
  307. int iCol = 1;
  308. if(uiFlags & COL_TYPE )
  309. {
  310. CString strType = pBaseAz->GetType();
  311. pListCtrl->SetItemText(iNewIndex,
  312. iCol,
  313. (LPWSTR)(LPCTSTR)strType);
  314. iCol++;
  315. }
  316. if(uiFlags & COL_PARENT_TYPE)
  317. {
  318. CString strParentType = pBaseAz->GetParentType();
  319. pListCtrl->SetItemText(iNewIndex,
  320. iCol,
  321. (LPWSTR)(LPCTSTR)strParentType);
  322. iCol++;
  323. }
  324. if(uiFlags & COL_DESCRIPTION)
  325. {
  326. //Add Description
  327. CString strDesc = pBaseAz->GetDescription();
  328. pListCtrl->SetItemText(iNewIndex,
  329. iCol,
  330. (LPWSTR)(LPCTSTR)strDesc);
  331. }
  332. return iNewIndex;
  333. }
  334. //+----------------------------------------------------------------------------
  335. // Function: EnableButtonIfSelectedInListCtrl
  336. // Synopsis: Enables the button if something is selected in Listctrl
  337. // Arguments:
  338. // Returns: TRUE if Enabled the button, FALSE if didn't.
  339. //-----------------------------------------------------------------------------
  340. BOOL
  341. EnableButtonIfSelectedInListCtrl(IN CListCtrl * pListCtrl,
  342. IN CButton* pButton)
  343. {
  344. if(!pListCtrl || !pButton)
  345. {
  346. ASSERT(pListCtrl);
  347. ASSERT(pButton);
  348. return FALSE;
  349. }
  350. int nSelectedItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
  351. if (nSelectedItem != -1)
  352. {
  353. pButton->EnableWindow(TRUE);
  354. return TRUE;
  355. }
  356. else
  357. {
  358. if(pButton->GetFocus() == pButton)
  359. pListCtrl->SetFocus();
  360. pButton->EnableWindow(FALSE);
  361. return FALSE;
  362. }
  363. }
  364. //+----------------------------------------------------------------------------
  365. // Function: DeleteSelectedRows
  366. // Synopsis: Deletes the selected rows
  367. //-----------------------------------------------------------------------------
  368. void
  369. DeleteSelectedRows(IN CListCtrl* pListCtrl)
  370. {
  371. //Remember the Position of first selected entry.
  372. //In the end set the position back to it
  373. int iFirstSelectedItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
  374. int iSelectedItem = -1;
  375. while( (iSelectedItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED)) != -1)
  376. {
  377. pListCtrl->DeleteItem(iSelectedItem);
  378. }
  379. if(pListCtrl->GetItemCount() <= iFirstSelectedItem)
  380. --iFirstSelectedItem;
  381. SelectListCtrlItem(pListCtrl, iFirstSelectedItem);
  382. }
  383. //+----------------------------------------------------------------------------
  384. // Function: SelectListCtrlItem
  385. // Synopsis: Select the item in List Ctrl and mark it visible
  386. //-----------------------------------------------------------------------------
  387. void
  388. SelectListCtrlItem(IN CListCtrl* pListCtrl,
  389. IN int iSelected)
  390. {
  391. if(iSelected == -1)
  392. iSelected = 0;
  393. pListCtrl->SetItemState(iSelected,
  394. LVIS_SELECTED| LVIS_FOCUSED,
  395. LVIS_SELECTED| LVIS_FOCUSED);
  396. pListCtrl->EnsureVisible(iSelected,FALSE);
  397. }
  398. //+----------------------------------------------------------------------------
  399. // Function: AddBaseAzItemsFromListCtrlToList
  400. // Synopsis: Add items from ListCtrl to List
  401. //-----------------------------------------------------------------------------
  402. VOID
  403. AddBaseAzItemsFromListCtrlToList(IN CListCtrl* pListCtrl,
  404. OUT CList<CBaseAz*,CBaseAz*>& listBaseAz)
  405. {
  406. if(!pListCtrl)
  407. {
  408. ASSERT(pListCtrl);
  409. return;
  410. }
  411. int iCount = pListCtrl->GetItemCount();
  412. for( int i = 0; i < iCount; ++i)
  413. {
  414. CBaseAz* pBaseAz = (CBaseAz*)pListCtrl->GetItemData(i);
  415. listBaseAz.AddTail(pBaseAz);
  416. }
  417. }
  418. //+----------------------------------------------------------------------------
  419. // Synopsis: Gets long value from Edit box
  420. // Returns: FALSE if edit box is empty. Assumes only numeric can be entered
  421. // in edit box
  422. //-----------------------------------------------------------------------------
  423. GetLongValue(CEdit& refEdit, LONG& reflValue, HWND hWnd)
  424. {
  425. CString strValue;
  426. refEdit.GetWindowText(strValue);
  427. if(strValue.IsEmpty())
  428. {
  429. reflValue = 0;
  430. return TRUE;
  431. }
  432. return ConvertStringToLong(strValue, reflValue, hWnd);
  433. }
  434. //+----------------------------------------------------------------------------
  435. // Synopsis: Sets long value in Edit box
  436. //-----------------------------------------------------------------------------
  437. VOID SetLongValue(CEdit* pEdit, LONG lValue)
  438. {
  439. //Maximum required size for _itow is 33.
  440. //When radix is 2, 32 char for 32bits + NULL terminator
  441. WCHAR buffer[33];
  442. _ltow(lValue,buffer,10);
  443. pEdit->SetWindowText(buffer);
  444. return;
  445. }
  446. //+----------------------------------------------------------------------------
  447. // Synopsis: Converts a sid in binary format to a sid in string format
  448. //-----------------------------------------------------------------------------
  449. BOOL ConvertSidToStringSid(IN PSID pSid, OUT CString* pstrSid)
  450. {
  451. if(!pSid || !pstrSid)
  452. {
  453. ASSERT(pSid);
  454. ASSERT(pstrSid);
  455. return FALSE;
  456. }
  457. LPWSTR pszSid = NULL;
  458. if(ConvertSidToStringSid(pSid, &pszSid))
  459. {
  460. ASSERT(pszSid);
  461. *pstrSid = pszSid;
  462. LocalFree(pszSid);
  463. return TRUE;
  464. }
  465. return FALSE;
  466. }
  467. //+----------------------------------------------------------------------------
  468. // Synopsis: Converts a sid in string format to sid in binary format
  469. //-----------------------------------------------------------------------------
  470. BOOL ConvertStringSidToSid(IN const CString& strSid, OUT PSID *ppSid)
  471. {
  472. if(!ppSid)
  473. {
  474. ASSERT(ppSid);
  475. return FALSE;
  476. }
  477. return ::ConvertStringSidToSid((LPCTSTR)strSid,ppSid);
  478. }
  479. //+----------------------------------------------------------------------------
  480. // Function:GetStringSidFromSidCachecAz
  481. // Synopsis:Gets the string sid from CSidCacheAz object
  482. //-----------------------------------------------------------------------------
  483. BOOL
  484. GetStringSidFromSidCachecAz(CBaseAz* pBaseAz,
  485. CString* pstrStringSid)
  486. {
  487. if(!pBaseAz || !pstrStringSid)
  488. {
  489. ASSERT(pBaseAz);
  490. ASSERT(pstrStringSid);
  491. return FALSE;
  492. }
  493. CSidCacheAz *pSidCacheAz = dynamic_cast<CSidCacheAz*>(pBaseAz);
  494. if(!pSidCacheAz)
  495. {
  496. ASSERT(pSidCacheAz);
  497. return FALSE;
  498. }
  499. return ConvertSidToStringSid(pSidCacheAz->GetSid(), pstrStringSid);
  500. }
  501. //+----------------------------------------------------------------------------
  502. // Function: AddAzObjectNodesToList
  503. // Synopsis: Adds the nodes for object of type eObjectType to the Container
  504. // node.
  505. // Arguments:IN eObjectType: Type of object
  506. // IN listAzChildObject: List of objects to be added
  507. // IN pContainerNode: Container in snapin to which new nodes will
  508. // be added.
  509. // Returns:
  510. //-----------------------------------------------------------------------------
  511. HRESULT
  512. AddAzObjectNodesToList(IN OBJECT_TYPE_AZ eObjectType,
  513. IN CList<CBaseAz*, CBaseAz*>& listAzChildObject,
  514. IN CBaseContainerNode* pContainerNode)
  515. {
  516. if(!pContainerNode)
  517. {
  518. ASSERT(pContainerNode);
  519. return E_POINTER;
  520. }
  521. switch (eObjectType)
  522. {
  523. case APPLICATION_AZ:
  524. return ADD_APPLICATION_FUNCTION::DoEnum(listAzChildObject,pContainerNode);
  525. case SCOPE_AZ:
  526. return ADD_SCOPE_FUNCTION::DoEnum(listAzChildObject,pContainerNode);
  527. case GROUP_AZ:
  528. return ADD_GROUP_FUNCTION::DoEnum(listAzChildObject,pContainerNode);
  529. case TASK_AZ:
  530. return ADD_TASK_FUNCTION::DoEnum(listAzChildObject,pContainerNode);
  531. case ROLE_AZ:
  532. return ADD_ROLE_FUNCTION::DoEnum(listAzChildObject,pContainerNode);
  533. case OPERATION_AZ:
  534. return ADD_OPERATION_FUNCTION::DoEnum(listAzChildObject,pContainerNode);
  535. }
  536. ASSERT(FALSE);
  537. return E_UNEXPECTED;
  538. }
  539. //
  540. //Error Handling and Message Display Routines
  541. //
  542. VOID
  543. vFormatString(CString &strOutput, UINT nIDPrompt, va_list *pargs)
  544. {
  545. CString strFormat;
  546. if(!strFormat.LoadString(nIDPrompt))
  547. return;
  548. LPWSTR pszResult = NULL;
  549. if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  550. strFormat,
  551. 0,
  552. 0,
  553. (LPTSTR)&pszResult,
  554. 1,
  555. pargs) && pszResult)
  556. {
  557. strOutput = pszResult;
  558. LocalFree(pszResult);
  559. }
  560. return;
  561. }
  562. VOID
  563. FormatString(CString &strOutput, UINT nIDPrompt, ...)
  564. {
  565. CString strFormat;
  566. if(!strFormat.LoadString(nIDPrompt))
  567. return;
  568. va_list args;
  569. va_start(args, nIDPrompt);
  570. LPWSTR pszResult = NULL;
  571. if(FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  572. strFormat,
  573. 0,
  574. 0,
  575. (LPTSTR)&pszResult,
  576. 1,
  577. &args))
  578. {
  579. strOutput = pszResult;
  580. LocalFree(pszResult);
  581. }
  582. va_end(args);
  583. return;
  584. }
  585. VOID
  586. GetSystemError(CString &strOutput, DWORD dwErr)
  587. {
  588. LPWSTR pszErrorMsg = NULL;
  589. if( FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  590. NULL,
  591. dwErr,
  592. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  593. (LPWSTR) &pszErrorMsg,
  594. 0,
  595. NULL) && pszErrorMsg)
  596. {
  597. strOutput = pszErrorMsg;
  598. LocalFree(pszErrorMsg);
  599. }
  600. else
  601. {
  602. strOutput.Format(L"<0x%08x>",dwErr);
  603. }
  604. return;
  605. }
  606. int DisplayMessageBox(HWND hWnd,
  607. const CString& strMessage,
  608. UINT uStyle)
  609. {
  610. CThemeContextActivator activator;
  611. CString strTitle;
  612. strTitle.LoadString(IDS_SNAPIN_NAME);
  613. return ::MessageBox(hWnd, strMessage, strTitle, uStyle|MB_TASKMODAL);
  614. }
  615. int FormatAndDisplayMessageBox(HWND hWnd,
  616. UINT uStyle,
  617. UINT nIDPrompt,
  618. va_list &args)
  619. {
  620. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  621. CThemeContextActivator activator;
  622. CString strMessage;
  623. vFormatString(strMessage,nIDPrompt,&args);
  624. int iReturn = 0;
  625. if(!strMessage.IsEmpty())
  626. iReturn = DisplayMessageBox(hWnd, strMessage,uStyle);
  627. va_end(args);
  628. return iReturn;
  629. }
  630. void
  631. DisplayError(HWND hWnd, UINT nIDPrompt, ...)
  632. {
  633. va_list args;
  634. va_start(args, nIDPrompt);
  635. FormatAndDisplayMessageBox(hWnd, MB_OK | MB_ICONERROR,nIDPrompt,args);
  636. va_end(args);
  637. }
  638. void
  639. DisplayInformation(HWND hWnd, UINT nIDPrompt, ...)
  640. {
  641. va_list args;
  642. va_start(args, nIDPrompt);
  643. FormatAndDisplayMessageBox(hWnd, MB_OK | MB_ICONINFORMATION,nIDPrompt,args);
  644. va_end(args);
  645. }
  646. void
  647. DisplayWarning(HWND hWnd, UINT nIDPrompt, ...)
  648. {
  649. va_list args;
  650. va_start(args, nIDPrompt);
  651. FormatAndDisplayMessageBox(hWnd, MB_OK | MB_ICONWARNING, nIDPrompt,args);
  652. va_end(args);
  653. }
  654. int
  655. DisplayConfirmation(HWND hwnd, UINT nIDPrompt,...)
  656. {
  657. va_list args;
  658. va_start(args, nIDPrompt);
  659. int iReturn = FormatAndDisplayMessageBox(hwnd, MB_YESNO | MB_ICONEXCLAMATION,nIDPrompt,args);
  660. va_end(args);
  661. return iReturn;
  662. }
  663. BOOL
  664. IsDeleteConfirmed(HWND hwndOwner,
  665. CBaseAz& refBaseAz)
  666. {
  667. CString strType = refBaseAz.GetType();
  668. strType.MakeLower();
  669. return IDYES == DisplayConfirmation(hwndOwner,
  670. IDS_DELETE_CONFIRMATION,
  671. (LPCTSTR)strType,
  672. (LPCTSTR)refBaseAz.GetName());
  673. }
  674. //
  675. //Error Map for object types containing some common error info for
  676. //each object type
  677. //
  678. ErrorMap ERROR_MAP[] =
  679. {
  680. { ADMIN_MANAGER_AZ,
  681. IDS_TYPE_ADMIN_MANAGER,
  682. IDS_ADMIN_MANAGER_NAME_EXIST,
  683. IDS_ADMIN_MANAGER_NAME_INVAILD,
  684. L"\\ / : * ? \" < > | [tab]",
  685. },
  686. { APPLICATION_AZ,
  687. IDS_TYPE_APPLICATION,
  688. IDS_APPLICATION_NAME_EXIST,
  689. IDS_APPLICATION_NAME_INVAILD,
  690. L"\\ / : * ? \" < > | [tab]",
  691. },
  692. { SCOPE_AZ,
  693. IDS_TYPE_SCOPE,
  694. IDS_SCOPE_NAME_EXIST,
  695. IDS_SCOPE_NAME_INVAILD,
  696. L"",
  697. },
  698. { GROUP_AZ,
  699. IDS_TYPE_GROUP,
  700. IDS_GROUP_NAME_EXIST,
  701. IDS_GROUP_NAME_INVAILD,
  702. L"\\ / : * ? \" < > | [tab]",
  703. },
  704. { TASK_AZ,
  705. IDS_TYPE_TASK,
  706. IDS_TASK_OP_ALREADY_EXIST,
  707. IDS_TASK_NAME_INVAILD,
  708. L"\\ / : * ? \" < > | [tab]",
  709. },
  710. { ROLE_AZ,
  711. IDS_TYPE_ROLE,
  712. IDS_ROLE_NAME_EXIST,
  713. IDS_ROLE_NAME_INVAILD,
  714. L"\\ / : * ? \" < > | [tab]",
  715. },
  716. { OPERATION_AZ,
  717. IDS_TYPE_OPERATION,
  718. IDS_TASK_OP_ALREADY_EXIST,
  719. IDS_OPERATION_NAME_INVAILD,
  720. L"\\ / : * ? \" < > | [tab]",
  721. },
  722. };
  723. ErrorMap *GetErrorMap(OBJECT_TYPE_AZ eObjectType)
  724. {
  725. for(int i = 0; i < ARRAYLEN(ERROR_MAP); ++i)
  726. {
  727. if(ERROR_MAP[i].eObjectType == eObjectType)
  728. return ERROR_MAP + i;
  729. }
  730. return NULL;
  731. }
  732. //+----------------------------------------------------------------------------
  733. // Function: GetLSAConnection
  734. // Synopsis: Wrapper for LsaOpenPolicy
  735. //-----------------------------------------------------------------------------
  736. LSA_HANDLE
  737. GetLSAConnection(IN const CString& strServer,
  738. IN DWORD dwAccessDesired)
  739. {
  740. TRACE_FUNCTION_EX(DEB_SNAPIN,GetLSAConnection)
  741. LSA_OBJECT_ATTRIBUTES oa;
  742. SECURITY_QUALITY_OF_SERVICE sqos;
  743. sqos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
  744. sqos.ImpersonationLevel = SecurityImpersonation;
  745. sqos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  746. sqos.EffectiveOnly = FALSE;
  747. InitializeObjectAttributes(&oa, NULL, 0, NULL, NULL);
  748. oa.SecurityQualityOfService = &sqos;
  749. LSA_UNICODE_STRING uszServer = {0};
  750. LSA_UNICODE_STRING *puszServer = NULL;
  751. if (!strServer.IsEmpty() &&
  752. RtlCreateUnicodeString(&uszServer, (LPCTSTR)strServer))
  753. {
  754. puszServer = &uszServer;
  755. }
  756. LSA_HANDLE hPolicy = NULL;
  757. LsaOpenPolicy(puszServer, &oa, dwAccessDesired, &hPolicy);
  758. if (puszServer)
  759. RtlFreeUnicodeString(puszServer);
  760. return hPolicy;
  761. }
  762. //+----------------------------------------------------------------------------
  763. // Function:GetFileName
  764. // Synopsis:Displays FileOpen dialog box and return file selected by user
  765. // Arguments:hwndOwner : owner window
  766. // bOpen: File must exist
  767. // nIDTitle : title of open dialog box
  768. // pszFilter : filter
  769. // strFileName : gets selected file name
  770. //
  771. //-----------------------------------------------------------------------------
  772. BOOL
  773. GetFileName(IN HWND hwndOwner,
  774. IN BOOL bOpen,
  775. IN INT nIDTitle,
  776. IN const CString& strInitFolderPath,
  777. IN LPCTSTR pszFilter,
  778. IN OUT CString& strFileName)
  779. {
  780. TRACE_FUNCTION_EX(DEB_SNAPIN,GetFileName)
  781. OPENFILENAME of;
  782. ZeroMemory(&of,sizeof(OPENFILENAME));
  783. WCHAR szFilePathBuffer[MAX_PATH];
  784. ZeroMemory(szFilePathBuffer,sizeof(szFilePathBuffer));
  785. of.lStructSize = sizeof(OPENFILENAME);
  786. of.hwndOwner = hwndOwner;
  787. of.lpstrFilter = pszFilter;
  788. of.nFilterIndex = 1;
  789. of.lpstrFile = szFilePathBuffer;
  790. of.nMaxFile = MAX_PATH;
  791. of.lpstrInitialDir = (LPCWSTR)strInitFolderPath;
  792. if(nIDTitle)
  793. {
  794. CString strTitle;
  795. if(strTitle.LoadString(nIDTitle))
  796. of.lpstrTitle = (LPWSTR)(LPCTSTR)strTitle;
  797. }
  798. of.Flags = OFN_HIDEREADONLY;
  799. if(bOpen)
  800. of.Flags |= (OFN_FILEMUSTEXIST |OFN_PATHMUSTEXIST);
  801. if(GetOpenFileName(&of))
  802. {
  803. strFileName = of.lpstrFile;
  804. return TRUE;
  805. }
  806. return FALSE;
  807. }
  808. int ServerBrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM /*lParam*/, LPARAM lpData)
  809. {
  810. switch (uMsg)
  811. {
  812. case BFFM_INITIALIZED:
  813. SendMessage(hwnd, BFFM_SETEXPANDED, TRUE, lpData);
  814. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
  815. break;
  816. }
  817. return 0;
  818. }
  819. //+----------------------------------------------------------------------------
  820. // Function:GetFolderName
  821. // Synopsis:Displays Folder selection dialog box and return folder selected
  822. // by user
  823. // Arguments:hwndOwner : owner window
  824. // nIDTitle : title of dialog box
  825. // strInitBrowseRoot : location of the root folder from which to
  826. // start browsing
  827. // strFolderName : gets selected file name
  828. //-----------------------------------------------------------------------------
  829. BOOL
  830. GetFolderName(IN HWND hwndOwner,
  831. IN INT nIDTitle,
  832. IN const CString& strInitBrowseRoot,
  833. IN OUT CString& strFolderName)
  834. {
  835. TRACE_FUNCTION_EX(DEB_SNAPIN,GetFolderName)
  836. BROWSEINFO bi;
  837. ZeroMemory(&bi,sizeof(bi));
  838. WCHAR szBuffer[MAX_PATH];
  839. szBuffer[0] = 0;
  840. CString strTitle;
  841. VERIFY(strTitle.LoadString(nIDTitle));
  842. bi.hwndOwner = hwndOwner;
  843. bi.pszDisplayName = szBuffer;
  844. bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
  845. bi.lpszTitle = strTitle;
  846. bi.lpfn = ServerBrowseCallbackProc;
  847. bi.lParam = (LPARAM)(LPCWSTR)strInitBrowseRoot;
  848. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  849. if(pidl)
  850. {
  851. WCHAR szFolderPath[MAX_PATH];
  852. if(SHGetPathFromIDList(pidl,szFolderPath))
  853. {
  854. PathAddBackslash(szFolderPath);
  855. strFolderName = szFolderPath;
  856. }
  857. }
  858. return !strFolderName.IsEmpty();
  859. }
  860. //+----------------------------------------------------------------------------
  861. // Function:GetADContainerPath
  862. // Synopsis:Displays a dialog box to allow for selecting a AD container
  863. //-----------------------------------------------------------------------------
  864. BOOL
  865. GetADContainerPath(HWND hWndOwner,
  866. ULONG nIDCaption,
  867. ULONG nIDTitle,
  868. CString& strPath,
  869. CADInfo& refAdInfo)
  870. {
  871. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  872. TRACE_FUNCTION_EX(DEB_SNAPIN,GetADContainerPath)
  873. HRESULT hr = refAdInfo.GetRootDSE();
  874. CHECK_HRESULT(hr);
  875. if(FAILED(hr))
  876. {
  877. DisplayError(hWndOwner,IDS_CANNOT_ACCESS_AD);
  878. return FALSE;
  879. }
  880. CString strRootDomainPath;
  881. if(!refAdInfo.GetRootDomainDn().IsEmpty())
  882. {
  883. if(!refAdInfo.GetRootDomainDCName().IsEmpty())
  884. {
  885. strRootDomainPath = L"LDAP://" +
  886. refAdInfo.GetRootDomainDCName() +
  887. L"/" +
  888. refAdInfo.GetRootDomainDn();
  889. }
  890. else
  891. {
  892. strRootDomainPath = L"LDAP://" + refAdInfo.GetRootDomainDn();
  893. }
  894. }
  895. DSBROWSEINFOW dsbrowse;
  896. ZeroMemory(&dsbrowse,sizeof(dsbrowse));
  897. dsbrowse.cbStruct = sizeof(dsbrowse);
  898. //Set Root of search to forest root
  899. if(!strRootDomainPath.IsEmpty())
  900. dsbrowse.pszRoot = (LPCTSTR)strRootDomainPath;
  901. //Construct the path to which tree will expand on opening of
  902. //dialog box
  903. CString strInitialPath;
  904. GetDefaultADContainerPath(refAdInfo,TRUE,TRUE,strInitialPath);
  905. WCHAR szPath[MAX_PATH];
  906. ZeroMemory(szPath,sizeof(szPath));
  907. if(!strInitialPath.IsEmpty() && MAX_PATH > strInitialPath.GetLength())
  908. {
  909. wcscpy(szPath,(LPCTSTR)strInitialPath);
  910. }
  911. dsbrowse.hwndOwner = hWndOwner;
  912. CString strCaption;
  913. if(nIDCaption)
  914. {
  915. strCaption.LoadString(nIDCaption);
  916. dsbrowse.pszCaption = strCaption;
  917. }
  918. CString strTitle;
  919. if(nIDTitle)
  920. {
  921. strTitle.LoadString(nIDTitle);
  922. dsbrowse.pszTitle = strTitle;
  923. }
  924. dsbrowse.pszPath = szPath;
  925. dsbrowse.cchPath = MAX_PATH;
  926. dsbrowse.dwFlags = DSBI_ENTIREDIRECTORY|DSBI_RETURN_FORMAT|DSBI_INCLUDEHIDDEN|DSBI_EXPANDONOPEN;
  927. dsbrowse.dwReturnFormat = ADS_FORMAT_X500_NO_SERVER;
  928. BOOL bRet = FALSE;
  929. BOOL iRet = 0;
  930. iRet = DsBrowseForContainer(&dsbrowse);
  931. if(IDOK == iRet)
  932. {
  933. //Path contains LDAP:// string which we don't want
  934. size_t nLen = wcslen(g_szLDAP);
  935. if(_wcsnicmp(dsbrowse.pszPath,g_szLDAP,nLen) == 0 )
  936. strPath = (dsbrowse.pszPath + nLen);
  937. else
  938. strPath = dsbrowse.pszPath;
  939. bRet = TRUE;
  940. }
  941. else if (-1 == iRet)
  942. {
  943. Dbg(DEB_SNAPIN, "DsBrowseForContainer Failed\n");
  944. DisplayError(hWndOwner,IDS_CANNOT_ACCESS_AD);
  945. }
  946. return bRet;
  947. }
  948. //+----------------------------------------------------------------------------
  949. // Function:CompareBaseAzObjects
  950. // Synopsis:Compares two baseaz objects for equivalance. If two pAzA and pAzB
  951. // are two different instances of same coreaz object, they are equal
  952. //-----------------------------------------------------------------------------
  953. BOOL
  954. CompareBaseAzObjects(CBaseAz* pAzA, CBaseAz* pAzB)
  955. {
  956. if(pAzA == pAzB)
  957. return TRUE;
  958. if(!pAzA || !pAzB)
  959. return FALSE;
  960. if(!(pAzA->GetObjectType() == pAzB->GetObjectType() &&
  961. pAzA->GetName() == pAzB->GetName()))
  962. return FALSE;
  963. //If object if of type AdminManager, it must be same node
  964. if(pAzA->GetObjectType() == ADMIN_MANAGER_AZ)
  965. return (pAzA == pAzB);
  966. //Two objects with same name and object type can exist under different
  967. //parent-> Check if their parents are same->
  968. if(pAzA->GetParentAz()->GetName() == pAzB->GetParentAz()->GetName())
  969. return TRUE;
  970. return FALSE;
  971. }
  972. //+----------------------------------------------------------------------------
  973. //
  974. //Below Code maps dialog box id to Help Map
  975. //Ported from DNS Manager
  976. //
  977. //-----------------------------------------------------------------------------
  978. #include "HelpMap.H"
  979. #define BEGIN_HELP_MAP(map) static DWORD_PTR map[] = {
  980. #define HELP_MAP_ENTRY(x) x, (DWORD_PTR)&g_aHelpIDs_##x ,
  981. #define END_HELP_MAP 0, 0 };
  982. #define NEXT_HELP_MAP_ENTRY(p) ((p)+2)
  983. #define MAP_ENTRY_DLG_ID(p) (*p)
  984. #define MAP_ENTRY_TABLE(p) ((DWORD*)(*(p+1)))
  985. #define IS_LAST_MAP_ENTRY(p) (MAP_ENTRY_DLG_ID(p) == 0)
  986. BEGIN_HELP_MAP(AuthManContextHelpMap)
  987. HELP_MAP_ENTRY(IDD_ADD_GROUP)
  988. HELP_MAP_ENTRY(IDD_ADD_OPERATION)
  989. HELP_MAP_ENTRY(IDD_ADD_ROLE_DEFINITION)
  990. HELP_MAP_ENTRY(IDD_ADD_TASK)
  991. HELP_MAP_ENTRY(IDD_ADMIN_MANAGER_ADVANCED_PROPERTY)
  992. HELP_MAP_ENTRY(IDD_ADMIN_MANAGER_GENERAL_PROPERTY)
  993. HELP_MAP_ENTRY(IDD_APPLICATION_GENERAL_PROPERTY)
  994. HELP_MAP_ENTRY(IDD_AUDIT)
  995. HELP_MAP_ENTRY(IDD_GROUP_GENERAL_PROPERTY)
  996. HELP_MAP_ENTRY(IDD_GROUP_LDAP_QUERY)
  997. HELP_MAP_ENTRY(IDD_GROUP_MEMBER)
  998. HELP_MAP_ENTRY(IDD_GROUP_NON_MEMBER)
  999. HELP_MAP_ENTRY(IDD_NEW_APPLICATION)
  1000. HELP_MAP_ENTRY(IDD_NEW_AUTHORIZATION_STORE)
  1001. HELP_MAP_ENTRY(IDD_NEW_GROUP)
  1002. HELP_MAP_ENTRY(IDD_NEW_OPERATION)
  1003. HELP_MAP_ENTRY(IDD_NEW_ROLE_DEFINITION)
  1004. HELP_MAP_ENTRY(IDD_NEW_SCOPE)
  1005. HELP_MAP_ENTRY(IDD_NEW_TASK)
  1006. HELP_MAP_ENTRY(IDD_OPEN_AUTHORIZATION_STORE)
  1007. HELP_MAP_ENTRY(IDD_OPERATION_GENERAL_PROPERTY)
  1008. HELP_MAP_ENTRY(IDD_ROLE_DEFINITION_PROPERTY)
  1009. HELP_MAP_ENTRY(IDD_SCOPE_GENERAL_PROPERTY)
  1010. HELP_MAP_ENTRY(IDD_ROLE_DEFINITION_PROPERTY)
  1011. HELP_MAP_ENTRY(IDD_SECURITY)
  1012. HELP_MAP_ENTRY(IDD_TASK_DEFINITION_PROPERTY)
  1013. HELP_MAP_ENTRY(IDD_TASK_GENERAL_PROPERTY)
  1014. HELP_MAP_ENTRY(IDD_ROLE_DEFINITION_GENERAL_PROPERTY)
  1015. HELP_MAP_ENTRY(IDD_ROLE_GENERAL_PROPERTY)
  1016. HELP_MAP_ENTRY(IDD_ROLE_DEF_DIALOG)
  1017. HELP_MAP_ENTRY(IDD_BROWSE_AD_STORE)
  1018. HELP_MAP_ENTRY(IDD_SCRIPT)
  1019. HELP_MAP_ENTRY(IDD_ADD_ROLE_DEFINITION_1)
  1020. END_HELP_MAP
  1021. //+----------------------------------------------------------------------------
  1022. // Function:FindDialogContextTopic
  1023. // Synopsis:Finds the helpmap for a given dialog id
  1024. //-----------------------------------------------------------------------------
  1025. BOOL
  1026. FindDialogContextTopic(IN UINT nDialogID,
  1027. OUT DWORD_PTR* pMap)
  1028. {
  1029. if(!pMap)
  1030. {
  1031. ASSERT(pMap);
  1032. return FALSE;
  1033. }
  1034. const DWORD_PTR* pMapEntry = AuthManContextHelpMap;
  1035. while (!IS_LAST_MAP_ENTRY(pMapEntry))
  1036. {
  1037. if (nDialogID == MAP_ENTRY_DLG_ID(pMapEntry))
  1038. {
  1039. DWORD_PTR pTable = (DWORD_PTR)MAP_ENTRY_TABLE(pMapEntry);
  1040. ASSERT(pTable);
  1041. *pMap = pTable;
  1042. return TRUE;
  1043. }
  1044. pMapEntry = NEXT_HELP_MAP_ENTRY(pMapEntry);
  1045. }
  1046. return FALSE;
  1047. }
  1048. //+----------------------------------------------------------------------------
  1049. // Function:CanReadOneProperty
  1050. // Synopsis:Function tries to read IsWriteable property. If that fails displays
  1051. // an error message. This is used before adding property pages.
  1052. // if we cannot read IsWritable property,thereisn't much hope.
  1053. //-----------------------------------------------------------------------------
  1054. BOOL CanReadOneProperty(LPCWSTR pszName,
  1055. CBaseAz* pBaseAz)
  1056. {
  1057. TRACE_FUNCTION_EX(DEB_SNAPIN,CanReadOneProperty)
  1058. if(!pBaseAz)
  1059. {
  1060. ASSERT(pBaseAz);
  1061. return FALSE;
  1062. }
  1063. BOOL bWrite;
  1064. HRESULT hr = pBaseAz->IsWritable(bWrite);
  1065. if(SUCCEEDED(hr))
  1066. {
  1067. return TRUE;
  1068. }
  1069. else
  1070. {
  1071. if(hr == HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE))
  1072. {
  1073. DisplayError(NULL,
  1074. IDS_PROP_ERROR_OBJECT_DELETED,
  1075. pszName);
  1076. }
  1077. else
  1078. {
  1079. //Display Generic Error
  1080. CString strError;
  1081. GetSystemError(strError, hr);
  1082. DisplayError(NULL,
  1083. IDS_GENERIC_PROP_DISPLAY_ERROR,
  1084. (LPCWSTR)strError,
  1085. pszName);
  1086. }
  1087. return FALSE;
  1088. }
  1089. }
  1090. //+----------------------------------------------------------------------------
  1091. // Function: ListCompareProc
  1092. // Synopsis: Comparison function used by list control
  1093. //-----------------------------------------------------------------------------
  1094. int CALLBACK
  1095. ListCompareProc(LPARAM lParam1,
  1096. LPARAM lParam2,
  1097. LPARAM lParamSort)
  1098. {
  1099. int iResult = CSTR_EQUAL;
  1100. if(!lParam1 || !lParam2 || !lParamSort)
  1101. {
  1102. ASSERT(lParam1);
  1103. ASSERT(lParam2);
  1104. ASSERT(lParamSort);
  1105. return iResult;
  1106. }
  1107. CompareInfo *pCompareInfo = (CompareInfo*)lParamSort;
  1108. CBaseAz* pBaseAz1 = NULL;
  1109. CBaseAz* pBaseAz2 = NULL;
  1110. if(pCompareInfo->bActionItem)
  1111. {
  1112. pBaseAz1 = ((ActionItem*)lParam1)->m_pMemberAz;
  1113. pBaseAz2 = ((ActionItem*)lParam2)->m_pMemberAz;
  1114. }
  1115. else
  1116. {
  1117. pBaseAz1 = (CBaseAz*)lParam1;
  1118. pBaseAz2 = (CBaseAz*)lParam2;
  1119. }
  1120. CString str1;
  1121. CString str2;
  1122. int iColType = -1;
  1123. int iColParentType = -1;
  1124. int iColDescription = -1;
  1125. int iCol = 1;
  1126. if(pCompareInfo->uiFlags & COL_TYPE )
  1127. {
  1128. iColType = iCol++;
  1129. }
  1130. if(pCompareInfo->uiFlags & COL_PARENT_TYPE)
  1131. {
  1132. iColParentType = iCol++;
  1133. }
  1134. if(pCompareInfo->uiFlags & COL_DESCRIPTION)
  1135. {
  1136. iColDescription = iCol++;
  1137. }
  1138. if (pBaseAz1 && pBaseAz2)
  1139. {
  1140. if(pCompareInfo->iColumn == 0)
  1141. {
  1142. str1 = pBaseAz1->GetName();
  1143. str2 = pBaseAz2->GetName();
  1144. }
  1145. else if(pCompareInfo->iColumn == iColType)
  1146. {
  1147. str1 = pBaseAz1->GetType();
  1148. str2 = pBaseAz2->GetType();
  1149. }
  1150. else if(pCompareInfo->iColumn == iColDescription)
  1151. {
  1152. str1 = pBaseAz1->GetDescription();
  1153. str2 = pBaseAz2->GetDescription();
  1154. }
  1155. else if(pCompareInfo->iColumn == iColParentType)
  1156. {
  1157. str1 = pBaseAz1->GetParentType();
  1158. str2 = pBaseAz2->GetParentType();
  1159. }
  1160. iResult = CompareString(LOCALE_USER_DEFAULT, 0, str1, -1, str2, -1) - 2;
  1161. iResult *= pCompareInfo->iSortDirection;
  1162. }
  1163. return iResult;
  1164. }
  1165. //+----------------------------------------------------------------------------
  1166. // Function:SortListControl
  1167. // Synopsis:Sorts a list control
  1168. // Arguments:pListCtrl: List control to sort
  1169. // iColumnClicked: column clicked
  1170. // iSortDirection: direction in which to sort
  1171. // uiFlags: column info
  1172. // bActionItem: if item in list is actionitem
  1173. //-----------------------------------------------------------------------------
  1174. void
  1175. SortListControl(CListCtrl* pListCtrl,
  1176. int iColumnClicked,
  1177. int iSortDirection,
  1178. UINT uiFlags,
  1179. BOOL bActionItem)
  1180. {
  1181. if(!pListCtrl)
  1182. {
  1183. ASSERT(pListCtrl);
  1184. return;
  1185. }
  1186. CompareInfo compareInfo;
  1187. compareInfo.bActionItem = bActionItem;
  1188. compareInfo.iColumn = iColumnClicked;
  1189. compareInfo.iSortDirection = iSortDirection;
  1190. compareInfo.uiFlags = uiFlags;
  1191. pListCtrl->SortItems(ListCompareProc,
  1192. (DWORD_PTR)&compareInfo);
  1193. }
  1194. //+----------------------------------------------------------------------------
  1195. // Synopsis: Ensures that selection in listview control is visible
  1196. //-----------------------------------------------------------------------------
  1197. void
  1198. EnsureListViewSelectionIsVisible(CListCtrl *pListCtrl)
  1199. {
  1200. ASSERT(pListCtrl);
  1201. int iSelected = pListCtrl->GetNextItem(-1, LVNI_SELECTED);
  1202. if (-1 != iSelected)
  1203. pListCtrl->EnsureVisible(iSelected, FALSE);
  1204. }
  1205. //+----------------------------------------------------------------------------
  1206. // Synopsis:Convert input number in string format to long. if number is out
  1207. // of range displays a message.
  1208. //-----------------------------------------------------------------------------
  1209. BOOL
  1210. ConvertStringToLong(LPCWSTR pszInput,
  1211. long &reflongOutput,
  1212. HWND hWnd)
  1213. {
  1214. if(!pszInput)
  1215. {
  1216. ASSERT(pszInput);
  1217. return FALSE;
  1218. }
  1219. //
  1220. //Get the Max len of long
  1221. long lMaxLong = LONG_MAX;
  1222. WCHAR szMaxLongBuffer[34];
  1223. _ltow(lMaxLong,szMaxLongBuffer,10);
  1224. size_t nMaxLen = wcslen(szMaxLongBuffer);
  1225. //
  1226. //Get the length of input
  1227. LPCWSTR pszTempInput = pszInput;
  1228. if(pszInput[0] == L'-')
  1229. {
  1230. pszTempInput++;
  1231. }
  1232. //
  1233. //Length of input greater than length of Max Long, than
  1234. //input is out of range.
  1235. size_t nInputLen = wcslen(pszTempInput);
  1236. if(nInputLen > nMaxLen)
  1237. {
  1238. if(hWnd)
  1239. {
  1240. DisplayError(hWnd,IDS_GREATER_THAN_MAX_LONG,pszTempInput,szMaxLongBuffer);
  1241. }
  1242. return FALSE;
  1243. }
  1244. //
  1245. //Convert input to int64 and check its less that max integer
  1246. //
  1247. __int64 i64Input = _wtoi64(pszTempInput);
  1248. if(i64Input > (__int64)lMaxLong)
  1249. {
  1250. if(hWnd)
  1251. {
  1252. DisplayError(hWnd,IDS_GREATER_THAN_MAX_LONG,pszTempInput,szMaxLongBuffer);
  1253. }
  1254. return FALSE;
  1255. }
  1256. //
  1257. //Value is good
  1258. //
  1259. reflongOutput = _wtol(pszInput);
  1260. return TRUE;
  1261. }
  1262. VOID
  1263. SetSel(CEdit& refEdit)
  1264. {
  1265. refEdit.SetFocus();
  1266. refEdit.SetSel(0,-1);
  1267. }
  1268. //+----------------------------------------------------------------------------
  1269. // Function: ValidateStoreTypeAndName
  1270. // Synopsis: Validates the user entered AD Store Name
  1271. // Arguments: strName: User entered store name to verify
  1272. // Returns: TRUE if name is valid else false
  1273. //-----------------------------------------------------------------------------
  1274. BOOL
  1275. ValidateStoreTypeAndName(HWND hWnd,
  1276. LONG ulStoreType,
  1277. const CString& strName)
  1278. {
  1279. TRACE_FUNCTION_EX(DEB_SNAPIN,ValidateStoreTypeAndName)
  1280. //Store Name is not entered in the valid format. We should come here only
  1281. //when store name is entered at the command line.
  1282. if((AZ_ADMIN_STORE_INVALID == ulStoreType) ||
  1283. strName.IsEmpty())
  1284. {
  1285. DisplayError(hWnd,IDS_INVALIDSTORE_ON_COMMANDLINE);
  1286. return FALSE;
  1287. }
  1288. //No validation is required for XML Store
  1289. if(ulStoreType == AZ_ADMIN_STORE_XML)
  1290. {
  1291. return TRUE;
  1292. }
  1293. ASSERT(ulStoreType == AZ_ADMIN_STORE_AD);
  1294. BOOL bRet = FALSE;
  1295. PDS_NAME_RESULT pResult = NULL;
  1296. do
  1297. {
  1298. //Get the store name with LDAP:// prefix
  1299. CString strFormalName;
  1300. NameToStoreName(AZ_ADMIN_STORE_AD,
  1301. strName,
  1302. TRUE,
  1303. strFormalName);
  1304. Dbg(DEB_SNAPIN, "AD store name entered is: %ws\n", CHECK_NULL(strFormalName));
  1305. CComPtr<IADsPathname> spPathName;
  1306. HRESULT hr = spPathName.CoCreateInstance(CLSID_Pathname,
  1307. NULL,
  1308. CLSCTX_INPROC_SERVER);
  1309. BREAK_ON_FAIL_HRESULT(hr);
  1310. hr = spPathName->Set(CComBSTR(strFormalName),
  1311. ADS_SETTYPE_FULL);
  1312. BREAK_ON_FAIL_HRESULT(hr);
  1313. //Get the DN entered by user
  1314. CComBSTR bstrDN;
  1315. hr = spPathName->Retrieve(ADS_FORMAT_X500_DN,&bstrDN);
  1316. BREAK_ON_FAIL_HRESULT(hr);
  1317. if(bstrDN.Length() == 0)
  1318. {
  1319. Dbg(DEB_SNAPIN, "spPathName->Retrieve returned 0 len DN\n");
  1320. break;
  1321. }
  1322. //Do a syntactical crack for the DN to see if its a valid DN
  1323. LPCWSTR pszName = bstrDN;
  1324. if( DsCrackNames(NULL,
  1325. DS_NAME_FLAG_SYNTACTICAL_ONLY,
  1326. DS_FQDN_1779_NAME,
  1327. DS_CANONICAL_NAME,
  1328. 1,
  1329. &pszName,
  1330. &pResult) != DS_NAME_NO_ERROR)
  1331. {
  1332. Dbg(DEB_SNAPIN, "DsCrackName failed to crack the name");
  1333. break;
  1334. }
  1335. ASSERT(pResult);
  1336. if(pResult->rItems->status == DS_NAME_NO_ERROR)
  1337. {
  1338. bRet = TRUE;
  1339. }
  1340. }while(0);
  1341. if(pResult)
  1342. DsFreeNameResult(pResult);
  1343. if(!bRet)
  1344. {
  1345. DisplayError(hWnd,IDS_INVALID_AD_STORE_NAME);
  1346. }
  1347. return bRet;
  1348. }
  1349. //+----------------------------------------------------------------------------
  1350. // Function: NameToFormatStoreName
  1351. // Synopsis: Converts user entered name to format which core understands
  1352. // Arguments: ulStoreType: Type of store
  1353. // strName: User entered store name
  1354. // bUseLDAP:use LDAP string to format AD name instead msldap
  1355. // strFormalName: gets the output ldap name
  1356. //-----------------------------------------------------------------------------
  1357. void
  1358. NameToStoreName(IN LONG lStoreType,
  1359. IN const CString& strName,
  1360. IN BOOL bUseLDAP,
  1361. OUT CString& strFormalName)
  1362. {
  1363. strFormalName.Empty();
  1364. if(lStoreType == AZ_ADMIN_STORE_XML)
  1365. {
  1366. if(_wcsnicmp(strName,g_szMSXML,wcslen(g_szMSXML)) == 0 )
  1367. {
  1368. strFormalName = strName;
  1369. }
  1370. else
  1371. {
  1372. strFormalName = g_szMSXML + strName;
  1373. }
  1374. return;
  1375. }
  1376. else if(lStoreType == AZ_ADMIN_STORE_AD)
  1377. {
  1378. LPCWSTR lpcszPrefix = bUseLDAP ? g_szLDAP : g_szMSLDAP;
  1379. LPCWSTR lpcszOtherPrefix = bUseLDAP ? g_szMSLDAP : g_szLDAP;
  1380. if(_wcsnicmp(strName,lpcszPrefix,wcslen(lpcszPrefix)) == 0 )
  1381. {
  1382. strFormalName = strName;
  1383. }
  1384. else
  1385. {
  1386. size_t nlen = wcslen(lpcszOtherPrefix);
  1387. //Check if user has put other prefix in the name
  1388. if(_wcsnicmp(strName,lpcszOtherPrefix,nlen) == 0 )
  1389. {
  1390. strFormalName = lpcszPrefix + strName.Right(strName.GetLength() - (int)nlen);
  1391. }
  1392. else
  1393. {
  1394. strFormalName = lpcszPrefix + strName;
  1395. }
  1396. }
  1397. return;
  1398. }
  1399. ASSERT(FALSE);
  1400. return;
  1401. }
  1402. void
  1403. TrimWhiteSpace(CString& str)
  1404. {
  1405. str.TrimLeft(L"\t ");
  1406. str.TrimRight(L"\t ");
  1407. }
  1408. BOOL
  1409. TranslateNameFromDnToDns(const CString& strInputDN,
  1410. CString& strOutputDNS)
  1411. {
  1412. TRACE_FUNCTION_EX(DEB_SNAPIN,TranslateNameFromDnToDns)
  1413. if(strInputDN.IsEmpty())
  1414. return FALSE;
  1415. strOutputDNS.Empty();
  1416. Dbg(DEB_SNAPIN, "DN of Forest is %ws\n", (LPCWSTR)strInputDN);
  1417. LPCWSTR pstrName = strInputDN;
  1418. PDS_NAME_RESULT pResult;
  1419. if( DS_NAME_NO_ERROR
  1420. == DsCrackNames(NULL,
  1421. DS_NAME_FLAG_SYNTACTICAL_ONLY,
  1422. DS_FQDN_1779_NAME,
  1423. DS_CANONICAL_NAME,
  1424. 1,
  1425. (LPWSTR*)(&pstrName),
  1426. &pResult))
  1427. {
  1428. if(pResult &&
  1429. pResult->cItems == 1 &&
  1430. pResult->rItems[0].status == DS_NAME_NO_ERROR &&
  1431. pResult->rItems[0].pDomain)
  1432. {
  1433. strOutputDNS = pResult->rItems[0].pDomain;
  1434. Dbg(DEB_SNAPIN, "DNS of Forest is %ws\n", (LPCWSTR)strOutputDNS);
  1435. }
  1436. if(pResult)
  1437. {
  1438. DsFreeNameResult(pResult);
  1439. }
  1440. }
  1441. return !strOutputDNS.IsEmpty();
  1442. }
  1443. //+----------------------------------------------------------------------------
  1444. // Function:GetSearchObject
  1445. // Synopsis:Get IDirectorySearch object to search at forest
  1446. //-----------------------------------------------------------------------------
  1447. HRESULT
  1448. GetSearchObject(IN CADInfo& refAdInfo,
  1449. OUT CComPtr<IDirectorySearch>& refspSearchObject)
  1450. {
  1451. TRACE_FUNCTION_EX(DEB_SNAPIN,GetSearchObject)
  1452. HRESULT hr = S_OK;
  1453. do
  1454. {
  1455. //
  1456. hr = refAdInfo.GetRootDSE();
  1457. BREAK_ON_FAIL_HRESULT(hr);
  1458. const CString& strForestDNS = refAdInfo.GetRootDomainDnsName();
  1459. CString strGCPath = L"GC://";
  1460. strGCPath += strForestDNS;
  1461. //
  1462. //Get IDirectorySearch Object
  1463. //
  1464. hr = AzRoleAdsOpenObject((LPWSTR)(LPCWSTR)strGCPath,
  1465. NULL,
  1466. NULL,
  1467. IID_IDirectorySearch,
  1468. (void**)&refspSearchObject);
  1469. BREAK_ON_FAIL_HRESULT(hr);
  1470. }while(0);
  1471. return hr;
  1472. }
  1473. //
  1474. // Attributes that we want the Object Picker to retrieve
  1475. //
  1476. static const LPCTSTR g_aszOPAttributes[] =
  1477. {
  1478. TEXT("distinguishedName"),
  1479. };
  1480. //+----------------------------------------------------------------------------
  1481. // Function:GetListOfAuthorizationStore
  1482. // Synopsis:Search at GC for AD policy stores and returns list.
  1483. //-----------------------------------------------------------------------------
  1484. HRESULT
  1485. GetListOfAuthorizationStore(IN CADInfo& refAdInfo,
  1486. OUT CList<CString,CString> &strList)
  1487. {
  1488. HRESULT hr = S_OK;
  1489. //If List is not empty, empty it
  1490. while(!strList.IsEmpty())
  1491. strList.RemoveHead();
  1492. do
  1493. {
  1494. TIMER("Time to search GC for AD Stores");
  1495. CComPtr<IDirectorySearch> spSearchObject;
  1496. CString str;
  1497. hr = GetSearchObject(refAdInfo,
  1498. spSearchObject);
  1499. BREAK_ON_FAIL_HRESULT(hr);
  1500. CDSSearch searchObject(spSearchObject);
  1501. searchObject.SetFilterString((LPWSTR)g_pszAuthorizationStoreQueryFilter);
  1502. searchObject.SetAttributeList((LPWSTR*)g_aszOPAttributes,1);
  1503. hr = searchObject.DoQuery();
  1504. BREAK_ON_FAIL_HRESULT(hr);
  1505. int iIndex = 0;
  1506. while(TRUE)
  1507. {
  1508. hr = searchObject.GetNextRow();
  1509. //We are done
  1510. if(hr == S_ADS_NOMORE_ROWS)
  1511. {
  1512. hr = S_OK;
  1513. break;
  1514. }
  1515. BREAK_ON_FAIL_HRESULT(hr);
  1516. ADS_SEARCH_COLUMN ColumnData;
  1517. hr = searchObject.GetColumn((LPWSTR)g_aszOPAttributes[0], &ColumnData);
  1518. if(SUCCEEDED(hr))
  1519. {
  1520. ASSERT(ADSTYPE_DN_STRING == ColumnData.dwADsType);
  1521. strList.AddTail(ColumnData.pADsValues->DNString);
  1522. searchObject.FreeColumn(&ColumnData);
  1523. }
  1524. }//End of while loop
  1525. }while(0);
  1526. if(!strList.IsEmpty())
  1527. return S_OK;
  1528. return hr;
  1529. }
  1530. /******************************************************************************
  1531. Class: CBaseAddDialog
  1532. Purpose:Displays a dialog box with list of AD Policy stores
  1533. ******************************************************************************/
  1534. class CBrowseADStoreDlg : public CHelpEnabledDialog
  1535. {
  1536. public:
  1537. CBrowseADStoreDlg(CList<CString,CString>&strList,
  1538. CString& strSelectedADStorePath)
  1539. :CHelpEnabledDialog(IDD_BROWSE_AD_STORE),
  1540. m_strList(strList),
  1541. m_strSelectedADStorePath(strSelectedADStorePath)
  1542. {
  1543. }
  1544. virtual BOOL
  1545. OnInitDialog();
  1546. virtual void
  1547. OnOK();
  1548. private:
  1549. CList<CString,CString> &m_strList;
  1550. CString& m_strSelectedADStorePath;
  1551. };
  1552. BOOL
  1553. CBrowseADStoreDlg::
  1554. OnInitDialog()
  1555. {
  1556. CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
  1557. ASSERT(pListCtrl);
  1558. //
  1559. //Initialize the list control
  1560. //
  1561. ListView_SetImageList(pListCtrl->GetSafeHwnd(),
  1562. LoadImageList(::AfxGetInstanceHandle (),
  1563. MAKEINTRESOURCE(IDB_ICONS)),
  1564. LVSIL_SMALL);
  1565. //Add ListBox Extended Style
  1566. pListCtrl->SetExtendedStyle(LVS_EX_FULLROWSELECT |
  1567. LVS_EX_INFOTIP);
  1568. //Add List box Columns
  1569. AddColumnToListView(pListCtrl,Col_For_Browse_ADStore_Page);
  1570. POSITION pos = m_strList.GetHeadPosition();
  1571. for (int i=0;i < m_strList.GetCount();i++)
  1572. {
  1573. pListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE|LVIF_IMAGE,
  1574. i,
  1575. m_strList.GetNext(pos),
  1576. 0,
  1577. 0,
  1578. iIconStore,
  1579. NULL);
  1580. }
  1581. return TRUE;
  1582. }
  1583. void
  1584. CBrowseADStoreDlg::
  1585. OnOK()
  1586. {
  1587. TRACE_METHOD_EX(DEB_SNAPIN,CBrowseADStoreDlg,OnOK)
  1588. CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST);
  1589. int iSelected = -1;
  1590. if((iSelected = pListCtrl->GetNextItem(-1, LVIS_SELECTED)) != -1)
  1591. {
  1592. m_strSelectedADStorePath = pListCtrl->GetItemText(iSelected,0);
  1593. }
  1594. CHelpEnabledDialog::OnOK();
  1595. }
  1596. //+----------------------------------------------------------------------------
  1597. // Function:BrowseAdStores
  1598. // Synopsis:Displays a dialog box with list of AD stores available.
  1599. // Arguments:strDN: Gets the selected ad store name
  1600. //-----------------------------------------------------------------------------
  1601. void
  1602. BrowseAdStores(IN HWND hwndOwner,
  1603. OUT CString& strDN,
  1604. IN CADInfo& refAdInfo)
  1605. {
  1606. TRACE_FUNCTION_EX(DEB_SNAPIN,BrowseAdStores)
  1607. CList<CString,CString> strList;
  1608. if(SUCCEEDED(GetListOfAuthorizationStore(refAdInfo,
  1609. strList)))
  1610. {
  1611. if(!strList.IsEmpty())
  1612. {
  1613. CBrowseADStoreDlg dlg(strList,strDN);
  1614. dlg.DoModal();
  1615. }
  1616. else
  1617. {
  1618. DisplayInformation(hwndOwner,
  1619. IDS_NO_AD_STORE);
  1620. }
  1621. }
  1622. else
  1623. {
  1624. //Display Error
  1625. DisplayError(hwndOwner,
  1626. IDS_CANNOT_ACCESS_AD);
  1627. }
  1628. }
  1629. //+----------------------------------------------------------------------------
  1630. // Function:LoadIcons
  1631. // Synopsis:Adds icons to imagelist
  1632. //-----------------------------------------------------------------------------
  1633. HRESULT
  1634. LoadIcons(LPIMAGELIST pImageList)
  1635. {
  1636. TRACE_FUNCTION_EX(DEB_SNAPIN,LoadIcons)
  1637. if(!pImageList)
  1638. {
  1639. ASSERT(pImageList);
  1640. return E_POINTER;
  1641. }
  1642. struct RESID2IICON
  1643. {
  1644. UINT uIconId; // Icon resource ID
  1645. int iIcon; // Index of the icon in the image list
  1646. };
  1647. const static RESID2IICON rgzLoadIconList[] =
  1648. {
  1649. {IDI_UNKNOWN_SID, iIconUnknownSid},
  1650. {IDI_COMPUTER, iIconComputerSid},
  1651. {IDI_GROUP, iIconGroup},
  1652. //iIconLocalGroup, //This is not used, but since its in the imagelist
  1653. // //i added an entry here
  1654. {IDI_USER, iIconUser,},
  1655. { IDI_BASIC_GROUP, iIconBasicGroup},
  1656. { IDI_LDAP_GROUP, iIconLdapGroup},
  1657. { IDI_OPERATION, iIconOperation},
  1658. { IDI_TASK, iIconTask},
  1659. { IDI_ROLE_DEFINITION, iIconRoleDefinition},
  1660. { IDI_STORE, iIconStore},
  1661. { IDI_APP, iIconApplication},
  1662. { IDI_ROLE, iIconRole},
  1663. { IDI_ROLE_SNAPIN, iIconRoleSnapin},
  1664. { IDI_SCOPE, iIconScope},
  1665. { IDI_CONTAINER, iIconContainer},
  1666. { 0, 0} // Must be last
  1667. };
  1668. for (int i = 0; rgzLoadIconList[i].uIconId != 0; i++)
  1669. {
  1670. HICON hIcon =
  1671. ::LoadIcon (AfxGetInstanceHandle (),
  1672. MAKEINTRESOURCE (rgzLoadIconList[i].uIconId));
  1673. ASSERT (hIcon && "Icon ID not found in resources");
  1674. pImageList->ImageListSetIcon ((PLONG_PTR) hIcon,
  1675. rgzLoadIconList[i].iIcon);
  1676. }
  1677. return S_OK;
  1678. }
  1679. //+----------------------------------------------------------------------------
  1680. // Function: LoadImageList
  1681. // Synopsis: Loads image list
  1682. //-----------------------------------------------------------------------------
  1683. HIMAGELIST
  1684. LoadImageList(HINSTANCE hInstance, LPCTSTR pszBitmapID)
  1685. {
  1686. HIMAGELIST himl = NULL;
  1687. HBITMAP hbm = LoadBitmap(hInstance, pszBitmapID);
  1688. if (hbm != NULL)
  1689. {
  1690. BITMAP bm;
  1691. GetObject(hbm, sizeof(BITMAP), &bm);
  1692. himl = ImageList_Create(bm.bmHeight, // height == width
  1693. bm.bmHeight,
  1694. ILC_COLOR | ILC_MASK,
  1695. bm.bmWidth / bm.bmHeight,
  1696. 0); // don't need to grow
  1697. if (himl != NULL)
  1698. ImageList_AddMasked(himl, hbm, CLR_DEFAULT);
  1699. DeleteObject(hbm);
  1700. }
  1701. return himl;
  1702. }
  1703. //+----------------------------------------------------------------------------
  1704. // Function: AddExtensionToFileName
  1705. // Synopsis: Functions adds .xml extension to name of file if no extension
  1706. // is present.
  1707. // Arguments:
  1708. // Returns:
  1709. //-----------------------------------------------------------------------------
  1710. VOID
  1711. AddExtensionToFileName(IN OUT CString& strFileName)
  1712. {
  1713. if(strFileName.IsEmpty())
  1714. return;
  1715. //if the last char is "\" don't do anything
  1716. if((strFileName.ReverseFind(L'\\') + 1) == strFileName.GetLength())
  1717. return;
  1718. int iLastDot = strFileName.ReverseFind(L'.');
  1719. if(iLastDot != -1)
  1720. {
  1721. //if there are three chars after last dot,
  1722. //file has extension. Index returned is zero based
  1723. if(strFileName.GetLength() == (iLastDot + 3 + 1))
  1724. return;
  1725. }
  1726. //File doesn't have extension. Add extension to the file.
  1727. strFileName += g_pszFileStoreExtension;
  1728. }
  1729. //+----------------------------------------------------------------------------
  1730. // Function: GetFileExtension
  1731. // Synopsis: Get the extension of the file.
  1732. //-----------------------------------------------------------------------------
  1733. BOOL
  1734. GetFileExtension(IN const CString& strFileName,
  1735. OUT CString& strExtension)
  1736. {
  1737. if(strFileName.IsEmpty())
  1738. return FALSE;
  1739. //Find the position of last dot
  1740. int iLastDot = strFileName.ReverseFind(L'.');
  1741. if(iLastDot != -1)
  1742. {
  1743. strExtension = strFileName.Right(strFileName.GetLength() - (iLastDot+1));
  1744. return TRUE;
  1745. }
  1746. return FALSE;
  1747. }
  1748. //+----------------------------------------------------------------------------
  1749. // Function:GetCurrentWorkingDirectory
  1750. // Synopsis:Gets the current working directory
  1751. //-----------------------------------------------------------------------------
  1752. BOOL
  1753. GetCurrentWorkingDirectory(IN OUT CString& strCWD)
  1754. {
  1755. TRACE_FUNCTION_EX(DEB_SNAPIN,GetCurrentWorkingDirectory)
  1756. strCWD.Empty();
  1757. LPWSTR pszBuffer = NULL;
  1758. do
  1759. {
  1760. int nLen = 0;
  1761. WCHAR szBuffer[MAX_PATH];
  1762. if((nLen = GetCurrentDirectory(MAX_PATH,szBuffer)) != 0)
  1763. {
  1764. //If the return value is less than MAX_PATH, function
  1765. //was successful. If its greater, buffer is not big enough,
  1766. //dynamically allocate it below.
  1767. if(nLen < MAX_PATH)
  1768. {
  1769. strCWD = szBuffer;
  1770. break;
  1771. }
  1772. //Bigger buffer is required
  1773. pszBuffer = new WCHAR[nLen];
  1774. if(pszBuffer)
  1775. {
  1776. if((nLen = GetCurrentDirectory(nLen,pszBuffer)) != 0)
  1777. {
  1778. strCWD = pszBuffer;
  1779. break;
  1780. }
  1781. }
  1782. }
  1783. }while(0);//FALSE LOOP
  1784. if(pszBuffer)
  1785. delete [] pszBuffer;
  1786. //Add \ at the end of string
  1787. if(!strCWD.IsEmpty() &&
  1788. ((strCWD.ReverseFind(L'\\') + 1) != strCWD.GetLength()))
  1789. {
  1790. strCWD += L'\\';
  1791. }
  1792. return !strCWD.IsEmpty();
  1793. }
  1794. VOID
  1795. RemoveItemsFromActionMap(ActionMap& mapActionItem)
  1796. {
  1797. for (ActionMap::iterator it = mapActionItem.begin();
  1798. it != mapActionItem.end();
  1799. ++it)
  1800. {
  1801. delete (*it).second;
  1802. }
  1803. }
  1804. /******************************************************************************
  1805. Class: CADInfo
  1806. Purpose:Keeps a cache of Active Directory info avoiding multiple binds
  1807. ******************************************************************************/
  1808. HRESULT
  1809. CADInfo::
  1810. GetRootDSE()
  1811. {
  1812. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetRootDSE)
  1813. HRESULT hr = S_OK;
  1814. if(m_spRootDSE == NULL)
  1815. {
  1816. //
  1817. //Bind to RootDSE
  1818. //
  1819. hr = AzRoleAdsOpenObject(L"LDAP://RootDSE",
  1820. NULL,
  1821. NULL,
  1822. IID_IADs,
  1823. (void**)&m_spRootDSE);
  1824. CHECK_HRESULT(hr);
  1825. }
  1826. return hr;
  1827. }
  1828. const CString&
  1829. CADInfo::GetDomainDnsName()
  1830. {
  1831. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetDomainDnsName)
  1832. if(m_strDomainDnsName.IsEmpty())
  1833. {
  1834. if(m_strDomainDn.IsEmpty())
  1835. GetDomainDn();
  1836. if(!m_strDomainDn.IsEmpty())
  1837. {
  1838. TranslateNameFromDnToDns(m_strDomainDn,m_strDomainDnsName);
  1839. Dbg(DEB_SNAPIN, "Domain Dns is: %ws\n", CHECK_NULL((LPCTSTR)m_strDomainDnsName));
  1840. }
  1841. }
  1842. return m_strDomainDnsName;
  1843. }
  1844. const CString&
  1845. CADInfo::GetDomainDn()
  1846. {
  1847. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetDomainDn)
  1848. if((m_spRootDSE != NULL) && m_strDomainDn.IsEmpty())
  1849. {
  1850. // Get the default name
  1851. VARIANT Default;
  1852. VariantInit(&Default);
  1853. HRESULT hr = m_spRootDSE->Get (CComBSTR(L"defaultNamingContext"), &Default);
  1854. if(SUCCEEDED(hr))
  1855. {
  1856. ASSERT(VT_BSTR == Default.vt);
  1857. if(VT_BSTR == Default.vt)
  1858. {
  1859. m_strDomainDn = Default.bstrVal;
  1860. ::VariantClear(&Default);
  1861. Dbg(DEB_SNAPIN, "Domain Dn is: %ws\n", CHECK_NULL((LPCTSTR)m_strDomainDn));
  1862. }
  1863. }
  1864. }
  1865. return m_strDomainDn;
  1866. }
  1867. const CString&
  1868. CADInfo::GetRootDomainDnsName()
  1869. {
  1870. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetRootDomainDnsName)
  1871. if(m_strRootDomainDnsName.IsEmpty())
  1872. {
  1873. if(m_strRootDomainDn.IsEmpty())
  1874. GetRootDomainDn();
  1875. if(!m_strRootDomainDn.IsEmpty())
  1876. {
  1877. TranslateNameFromDnToDns(m_strRootDomainDn,m_strRootDomainDnsName);
  1878. Dbg(DEB_SNAPIN, "Root Domain Dns is: %ws\n", CHECK_NULL((LPCTSTR)m_strRootDomainDnsName));
  1879. }
  1880. }
  1881. return m_strRootDomainDnsName;
  1882. }
  1883. const CString&
  1884. CADInfo::GetRootDomainDn()
  1885. {
  1886. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetRootDomainDn)
  1887. if((m_spRootDSE != NULL) && m_strRootDomainDn.IsEmpty())
  1888. {
  1889. // Get the default name
  1890. VARIANT Default;
  1891. VariantInit(&Default);
  1892. HRESULT hr = m_spRootDSE->Get(CComBSTR(L"rootDomainNamingContext"), &Default);
  1893. if(SUCCEEDED(hr))
  1894. {
  1895. //Convert DN to DNS name
  1896. m_strRootDomainDn = Default.bstrVal;
  1897. ::VariantClear(&Default);
  1898. Dbg(DEB_SNAPIN, "Root Domain DN is: %ws\n", CHECK_NULL((LPCTSTR)m_strRootDomainDn));
  1899. }
  1900. }
  1901. return m_strRootDomainDn;
  1902. }
  1903. BOOL GetDcNameForDomain(IN const CString& strDomainName,
  1904. OUT CString& strDCName)
  1905. {
  1906. TRACE_FUNCTION_EX(DEB_SNAPIN,GetDcNameForDomain)
  1907. if(strDomainName.IsEmpty())
  1908. {
  1909. return FALSE;
  1910. }
  1911. strDCName.Empty();
  1912. PDOMAIN_CONTROLLER_INFO pDomainInfo = NULL;
  1913. //Get the DC Name
  1914. DWORD dwErr = DsGetDcName(NULL,
  1915. (LPCWSTR)strDomainName,
  1916. NULL,
  1917. NULL,
  1918. DS_IS_DNS_NAME|DS_DIRECTORY_SERVICE_REQUIRED,
  1919. &pDomainInfo);
  1920. if((ERROR_SUCCESS == dwErr) && pDomainInfo)
  1921. {
  1922. LPWSTR pszDCName = pDomainInfo->DomainControllerName;
  1923. //The returned computer name is prefixed with \\
  1924. //Remove backslashes
  1925. if(pszDCName[0] == L'\\' && pszDCName[1] == L'\\')
  1926. pszDCName += 2;
  1927. //If a DNS-style name is returned, it is terminated with a period,
  1928. //indicating that the returned name is an absolute (non-relative)
  1929. //DNS name.
  1930. //We don't need period, remove it
  1931. //DomainControllerName is in DNS format.
  1932. if(pDomainInfo->Flags & DS_DNS_CONTROLLER_FLAG)
  1933. {
  1934. size_t dwLen = wcslen(pszDCName);
  1935. if(dwLen && (L'.' == pszDCName[dwLen -1]))
  1936. {
  1937. pszDCName[dwLen -1] = L'\0';
  1938. }
  1939. }
  1940. Dbg(DEB_SNAPIN, "DC is %ws\n", pszDCName);
  1941. strDCName = pszDCName;
  1942. NetApiBufferFree(pDomainInfo);
  1943. return TRUE;
  1944. }
  1945. return FALSE;
  1946. }
  1947. const CString&
  1948. CADInfo::
  1949. GetRootDomainDCName()
  1950. {
  1951. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetRootDomainDCName)
  1952. if((m_spRootDSE != NULL) && m_strRootDomainDcName.IsEmpty())
  1953. {
  1954. GetDcNameForDomain(GetRootDomainDnsName(),m_strRootDomainDcName);
  1955. }
  1956. return m_strRootDomainDcName;
  1957. }
  1958. const CString&
  1959. CADInfo::
  1960. GetDomainDCName()
  1961. {
  1962. TRACE_METHOD_EX(DEB_SNAPIN,CADInfo,GetDomainDCName)
  1963. if((m_spRootDSE != NULL) && m_strDomainDcName.IsEmpty())
  1964. {
  1965. GetDcNameForDomain(GetDomainDnsName(),m_strDomainDcName);
  1966. }
  1967. return m_strDomainDcName;
  1968. }
  1969. //+--------------------------------------------------------------------------
  1970. // Function: AzRoleAdsOpenObject
  1971. // Synopsis: A thin wrapper around ADsOpenObject
  1972. //+--------------------------------------------------------------------------
  1973. HRESULT AzRoleAdsOpenObject(LPWSTR lpszPathName,
  1974. LPWSTR lpszUserName,
  1975. LPWSTR lpszPassword,
  1976. REFIID riid,
  1977. VOID** ppObject,
  1978. BOOL bBindToServer)
  1979. {
  1980. static DWORD additionalFlags = GetADsOpenObjectFlags();
  1981. DWORD dwFlags = ADS_SECURE_AUTHENTICATION | additionalFlags;
  1982. if (bBindToServer)
  1983. {
  1984. //
  1985. // If we know we are connecting to a specific server and not domain in general
  1986. // then pass the ADS_SERVER_BIND flag to save ADSI the trouble of figuring it out
  1987. //
  1988. dwFlags |= ADS_SERVER_BIND;
  1989. }
  1990. //
  1991. //Get IDirectorySearch Object
  1992. //
  1993. return ADsOpenObject(lpszPathName,
  1994. lpszUserName,
  1995. lpszPassword,
  1996. additionalFlags,
  1997. riid,
  1998. ppObject);
  1999. }
  2000. VOID
  2001. GetDefaultADContainerPath(IN CADInfo& refAdInfo,
  2002. IN BOOL bAddServer,
  2003. IN BOOL bAddLdap,
  2004. OUT CString& strPath)
  2005. {
  2006. strPath.Empty();
  2007. if(!refAdInfo.GetDomainDn().IsEmpty())
  2008. {
  2009. if(!refAdInfo.GetDomainDCName().IsEmpty())
  2010. {
  2011. if(bAddLdap)
  2012. {
  2013. strPath += L"LDAP://";
  2014. }
  2015. if(bAddServer)
  2016. {
  2017. strPath += refAdInfo.GetDomainDCName();
  2018. strPath += L"/";
  2019. }
  2020. strPath += g_pszProgramDataPrefix;
  2021. strPath += refAdInfo.GetDomainDn();
  2022. }
  2023. else
  2024. {
  2025. if(bAddLdap)
  2026. {
  2027. strPath = L"LDAP://";
  2028. }
  2029. strPath += g_pszProgramDataPrefix;
  2030. strPath += refAdInfo.GetDomainDn();
  2031. }
  2032. }
  2033. }
  2034. //+--------------------------------------------------------------------------
  2035. // Function: IsBizRuleWritable
  2036. // Synopsis: Checks if bizrules are writable for this object
  2037. //+--------------------------------------------------------------------------
  2038. BOOL
  2039. IsBizRuleWritable(HWND hWnd, CContainerAz& refBaseAz)
  2040. {
  2041. CScopeAz* pScopeAz = dynamic_cast<CScopeAz*>(&refBaseAz);
  2042. //Bizrules are always writable for at
  2043. //application level
  2044. if(!pScopeAz)
  2045. {
  2046. return TRUE;
  2047. }
  2048. BOOL bBizRuleWritable = TRUE;
  2049. HRESULT hr = pScopeAz->BizRulesWritable(bBizRuleWritable);
  2050. if(SUCCEEDED(hr) && !bBizRuleWritable)
  2051. {
  2052. DisplayInformation(hWnd,
  2053. IDS_BIZRULE_NOT_ALLOWED,
  2054. pScopeAz->GetName());
  2055. return FALSE;
  2056. }
  2057. return TRUE;
  2058. }
  2059. //+--------------------------------------------------------------------------
  2060. // Function: ParseStoreURL
  2061. // Synopsis: Extracts the store name and type from a store url
  2062. // store url are in format msldap://DN or msxml://filepath
  2063. //+--------------------------------------------------------------------------
  2064. void
  2065. ParseStoreURL(IN const CString& strStoreURL,
  2066. OUT CString& refstrStoreName,
  2067. OUT LONG& reflStoreType)
  2068. {
  2069. if(_wcsnicmp(strStoreURL,g_szMSXML,wcslen(g_szMSXML)) == 0 )
  2070. {
  2071. reflStoreType = AZ_ADMIN_STORE_XML;
  2072. refstrStoreName = strStoreURL.Mid((int)wcslen(g_szMSXML));
  2073. }
  2074. else if(_wcsnicmp(strStoreURL,g_szMSLDAP,wcslen(g_szMSLDAP)) == 0 )
  2075. {
  2076. reflStoreType = AZ_ADMIN_STORE_AD;
  2077. refstrStoreName = strStoreURL.Mid((int)wcslen(g_szMSLDAP));
  2078. }
  2079. else
  2080. {
  2081. reflStoreType = AZ_ADMIN_STORE_INVALID;
  2082. refstrStoreName = strStoreURL;
  2083. }
  2084. }
  2085. /******************************************************************************
  2086. Class: CCommandLineOptions
  2087. Purpose:class for reading the command line options for console file
  2088. ******************************************************************************/
  2089. void
  2090. CCommandLineOptions::
  2091. Initialize()
  2092. {
  2093. TRACE_METHOD_EX(DEB_SNAPIN,CCommandLineOptions,Initialize)
  2094. //This should be called only once
  2095. if(m_bInit)
  2096. {
  2097. return;
  2098. }
  2099. m_bInit = TRUE;
  2100. // see if we have command line arguments
  2101. // Count of arguments
  2102. int cArgs = 0;
  2103. // Array of pointers to string
  2104. LPCWSTR * lpServiceArgVectors = (LPCWSTR *)CommandLineToArgvW(GetCommandLineW(),
  2105. &cArgs);
  2106. if (lpServiceArgVectors == NULL || cArgs <= 2)
  2107. {
  2108. // none, just return
  2109. return;
  2110. }
  2111. m_bCommandLineSpecified = TRUE;
  2112. CString strStoreURL = lpServiceArgVectors[2];
  2113. ParseStoreURL(strStoreURL,
  2114. m_strStoreName,
  2115. m_lStoreType);
  2116. Dbg(DEB_SNAPIN, "Store URL Name entered at commandline is %ws\n", CHECK_NULL(strStoreURL));
  2117. Dbg(DEB_SNAPIN, "Store Name entered at commandline is %ws\n", CHECK_NULL(m_strStoreName));
  2118. Dbg(DEB_SNAPIN, "AD store type entered is: %u\n", m_lStoreType);
  2119. }
  2120. //+----------------------------------------------------------------------------
  2121. // Function: OpenAdminManager
  2122. // Synopsis: Open an existing Authorization Store adds
  2123. // corresponding adminManager object to snapin
  2124. // Arguments:IN hWnd: Handle of window for dialog box
  2125. // IN bOpenFromSavedConsole: True if open is in response to a console
  2126. // file.
  2127. // IN lStoreType: XML or AD
  2128. // IN strName: Name of store
  2129. // IN strScriptDir : Script directory
  2130. // IN pRootData: Snapin Rootdata
  2131. // IN pComponentData: ComponentData
  2132. // Returns:
  2133. //-----------------------------------------------------------------------------
  2134. HRESULT OpenAdminManager(IN HWND hWnd,
  2135. IN BOOL bOpenFromSavedConsole,
  2136. IN ULONG lStoreType,
  2137. IN const CString& strStoreName,
  2138. IN const CString& strScriptDir,
  2139. IN CRootData* pRootData,
  2140. IN CComponentDataObject* pComponentData)
  2141. {
  2142. TRACE_FUNCTION_EX(DEB_SNAPIN,OpenAdminManager)
  2143. if(!pRootData || !pComponentData)
  2144. {
  2145. ASSERT(pRootData);
  2146. ASSERT(pComponentData);
  2147. return E_POINTER;
  2148. }
  2149. //NTRAID#NTBUG9-706617-2002/07/17-hiteshr Our validation code cannot validate
  2150. //ADAM dn. Do not do any validation.
  2151. ////Vaidate the store name
  2152. //if(!ValidateStoreTypeAndName(hWnd,
  2153. // lStoreType,
  2154. // strStoreName))
  2155. //{
  2156. // return E_INVALIDARG;
  2157. //}
  2158. HRESULT hr = OpenCreateAdminManager(FALSE,
  2159. bOpenFromSavedConsole,
  2160. lStoreType,
  2161. strStoreName,
  2162. L"",
  2163. strScriptDir,
  2164. pRootData,
  2165. pComponentData);
  2166. if(FAILED(hr))
  2167. {
  2168. //Display Error
  2169. if(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
  2170. {
  2171. if(!bOpenFromSavedConsole)
  2172. {
  2173. ::DisplayError(hWnd,
  2174. IDS_ADMIN_MANAGER_NOT_FOUND,
  2175. (LPCTSTR)strStoreName);
  2176. }
  2177. else
  2178. {
  2179. ::DisplayError(hWnd,
  2180. IDS_CANNOT_FIND_AUTHORIZATION_STORE,
  2181. (LPCTSTR)strStoreName);
  2182. }
  2183. }
  2184. else if(hr == HRESULT_FROM_WIN32(ERROR_INVALID_NAME))
  2185. {
  2186. ErrorMap * pErrorMap = GetErrorMap(ADMIN_MANAGER_AZ);
  2187. ::DisplayError(hWnd,
  2188. pErrorMap->idInvalidName,
  2189. pErrorMap->pszInvalidChars);
  2190. }
  2191. else
  2192. {
  2193. //Display Generic Error
  2194. CString strError;
  2195. GetSystemError(strError, hr);
  2196. ::DisplayError(hWnd,
  2197. IDS_OPEN_ADMIN_MANAGER_GENERIC_ERROR,
  2198. (LPCTSTR)strError);
  2199. }
  2200. }
  2201. return hr;
  2202. }
  2203. //+----------------------------------------------------------------------------
  2204. // Function: GetDisplayNameFromStoreURL
  2205. // Synopsis: Get the display name for the store.
  2206. // Arguments: strPolicyURL: This is store url in msxml://filepath or
  2207. // msldap://dn format.
  2208. // strDisplayName: This gets the display name. For xml, display
  2209. // name is name of file, for AD its name of leaf element
  2210. // Returns:
  2211. //-----------------------------------------------------------------------------
  2212. void
  2213. GetDisplayNameFromStoreURL(IN const CString& strPolicyURL,
  2214. OUT CString& strDisplayName)
  2215. {
  2216. //Store URL format has msxml:// or msldap:// prefix
  2217. //Get the store name without any prefix
  2218. CString strStorePath;
  2219. LONG lStoreType;
  2220. ParseStoreURL(strPolicyURL,
  2221. strStorePath,
  2222. lStoreType);
  2223. //Default Display Name of store is path without prefix.
  2224. strDisplayName = strStorePath;
  2225. if(AZ_ADMIN_STORE_INVALID == lStoreType)
  2226. {
  2227. ASSERT(FALSE);
  2228. return;
  2229. }
  2230. //For XML store, display name is name of the file
  2231. if(AZ_ADMIN_STORE_XML == lStoreType)
  2232. {
  2233. strDisplayName = PathFindFileName(strStorePath);
  2234. }
  2235. //For AD store, display name is name of the leaf element
  2236. else
  2237. {
  2238. do
  2239. {
  2240. CComPtr<IADsPathname> spPathName;
  2241. HRESULT hr = spPathName.CoCreateInstance(CLSID_Pathname,
  2242. NULL,
  2243. CLSCTX_INPROC_SERVER);
  2244. BREAK_ON_FAIL_HRESULT(hr);
  2245. //The path which we have right now can be dn or server/dn.
  2246. //append LDAP:// to it.
  2247. CString strLDAPStorePath = g_szLDAP + strStorePath;
  2248. hr = spPathName->Set(CComBSTR(strLDAPStorePath),
  2249. ADS_SETTYPE_FULL);
  2250. BREAK_ON_FAIL_HRESULT(hr);
  2251. //Get the leaf element. This will return leaf element in the
  2252. //format cn=foo. We only want "foo".
  2253. CComBSTR bstrLeaf;
  2254. hr = spPathName->Retrieve(ADS_FORMAT_LEAF ,&bstrLeaf);
  2255. BREAK_ON_FAIL_HRESULT(hr);
  2256. if(bstrLeaf.Length())
  2257. {
  2258. strDisplayName = bstrLeaf;
  2259. strDisplayName.Delete(0,strDisplayName.Find(L'=') + 1);
  2260. }
  2261. }while(0);
  2262. }
  2263. }
  2264. void
  2265. SetXMLStoreDirectory(IN CRoleRootData& roleRootData,
  2266. IN const CString& strXMLStorePath)
  2267. {
  2268. CString strXMLStoreDirectory = GetDirectoryFromPath(strXMLStorePath);
  2269. roleRootData.SetXMLStorePath(strXMLStoreDirectory);
  2270. }
  2271. //+----------------------------------------------------------------------------
  2272. // Function: GetDirectoryFromPath
  2273. // Synopsis: Removes the file name from the input file path and return
  2274. // the folder path. For Ex: Input is C:\temp\foo.xml. Return
  2275. // value will be C:\temp\
  2276. //-----------------------------------------------------------------------------
  2277. CString
  2278. GetDirectoryFromPath(IN const CString& strPath)
  2279. {
  2280. TRACE_FUNCTION_EX(DEB_SNAPIN,GetDirectoryFromPath)
  2281. Dbg(DEB_SNAPIN, "Input path", CHECK_NULL(strPath));
  2282. CString strDir;
  2283. if(strPath.GetLength() < MAX_PATH)
  2284. {
  2285. WCHAR szPath[MAX_PATH];
  2286. HRESULT hr = StringCchCopy(szPath,MAX_PATH,(LPCWSTR)strPath);
  2287. if(FAILED(hr))
  2288. return strDir;
  2289. if(!PathRemoveFileSpec(szPath))
  2290. return strDir;
  2291. PathAddBackslash(szPath);
  2292. strDir = szPath;
  2293. }
  2294. Dbg(DEB_SNAPIN, "Output Dir", CHECK_NULL(strDir));
  2295. return strDir;
  2296. }
  2297. //+----------------------------------------------------------------------------
  2298. // Function: ConvertToExpandedAndAbsolutePath
  2299. // Synopsis: Expands the environment variables in the input path and also
  2300. // makes it absolute if necessary.
  2301. //-----------------------------------------------------------------------------
  2302. void
  2303. ConvertToExpandedAndAbsolutePath(IN OUT CString& strPath)
  2304. {
  2305. TRACE_FUNCTION_EX(DEB_SNAPIN,ConvertToExpandedAndAbsolutePath)
  2306. Dbg(DEB_SNAPIN, "Input name", CHECK_NULL(strPath));
  2307. do
  2308. {
  2309. //
  2310. //Expand evironment variables in the path
  2311. //
  2312. WCHAR szExpandedPath[MAX_PATH];
  2313. DWORD dwSize = ExpandEnvironmentStrings(strPath,
  2314. szExpandedPath,
  2315. MAX_PATH);
  2316. //Buffer is small, allocate required buffer and try again
  2317. if(dwSize > MAX_PATH)
  2318. {
  2319. LPWSTR pszExpandedPath = (LPWSTR)LocalAlloc(LPTR,dwSize*sizeof(WCHAR));
  2320. if(pszExpandedPath)
  2321. {
  2322. dwSize = ExpandEnvironmentStrings(strPath,
  2323. pszExpandedPath,
  2324. dwSize);
  2325. if(dwSize)
  2326. {
  2327. strPath = pszExpandedPath;
  2328. }
  2329. LocalFree(pszExpandedPath);
  2330. pszExpandedPath = NULL;
  2331. if(!dwSize)
  2332. {
  2333. break;
  2334. }
  2335. }
  2336. }
  2337. else if(dwSize)
  2338. {
  2339. strPath = szExpandedPath;
  2340. }
  2341. else
  2342. {
  2343. break;
  2344. }
  2345. //Make absolute path
  2346. WCHAR szAbsolutePath[MAX_PATH];
  2347. dwSize = GetFullPathName(strPath,
  2348. MAX_PATH,
  2349. szAbsolutePath,
  2350. NULL);
  2351. //Buffer is small
  2352. if(dwSize > MAX_PATH)
  2353. {
  2354. LPWSTR pszAbsolutePath = (LPWSTR)LocalAlloc(LPTR,dwSize*sizeof(WCHAR));
  2355. if(pszAbsolutePath)
  2356. {
  2357. dwSize = GetFullPathName(strPath,
  2358. MAX_PATH,
  2359. pszAbsolutePath,
  2360. NULL);
  2361. if(dwSize)
  2362. {
  2363. strPath = pszAbsolutePath;
  2364. }
  2365. LocalFree(pszAbsolutePath);
  2366. pszAbsolutePath = NULL;
  2367. }
  2368. }
  2369. else if(dwSize)
  2370. {
  2371. strPath = szAbsolutePath;
  2372. }
  2373. }while(0);
  2374. Dbg(DEB_SNAPIN, "Output name", CHECK_NULL(strPath));
  2375. }
  2376. //+----------------------------------------------------------------------------
  2377. // Function: PreprocessScript
  2378. // Synopsis: Script is read from XML file and displayed multi line edit control.
  2379. // End of line in the XML is indicated by LF instead of CRLF sequence,
  2380. // however Edit Control requires CRLF sequence to format correctly and
  2381. // with only LF it displays everything in a single line with a box for
  2382. // LF char. This function checks if script uses LF for line termination
  2383. // and changes it with CRLF sequence.
  2384. //-----------------------------------------------------------------------------
  2385. void
  2386. PreprocessScript(CString& strScript)
  2387. {
  2388. WCHAR chLF = 0x0a;
  2389. WCHAR szCRLF[3] = {0x0D, 0x0A, 0};
  2390. if(strScript.Find(chLF) != -1 && strScript.Find(szCRLF) == -1)
  2391. {
  2392. CString strProcessedScript;
  2393. int len = strScript.GetLength();
  2394. for( int i = 0; i < len; ++i)
  2395. {
  2396. WCHAR ch = strScript.GetAt(i);
  2397. if(ch == chLF)
  2398. {
  2399. strProcessedScript += szCRLF;
  2400. }
  2401. else
  2402. {
  2403. strProcessedScript += ch;
  2404. }
  2405. }
  2406. strScript = strProcessedScript;
  2407. }
  2408. }