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.

599 lines
18 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: _deltemp.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. ///////////////////////////////////////////////////////////////////////
  11. // LOCAL FUNCTIONS
  12. CSchemaClassInfo* _FindClassByName(LPCWSTR lpszClassName,
  13. CGrowableArr<CSchemaClassInfo>* pSchemaClassesInfoArray)
  14. {
  15. int nCount = (int) pSchemaClassesInfoArray->GetCount();
  16. for (int k=0; k < nCount; k++)
  17. {
  18. if (wcscmp(lpszClassName, (*pSchemaClassesInfoArray)[k]->GetName()) == 0)
  19. return (*pSchemaClassesInfoArray)[k];
  20. }
  21. TRACE(L"_FindClassByName(%s) failed\n", lpszClassName);
  22. return NULL;
  23. }
  24. ///////////////////////////////////////////////////////////////////////
  25. // LOCAL CLASSES
  26. ////////////////////////////////////////////////////////////////////////
  27. // CTemplateClassReferences
  28. class CTemplateClassReferences
  29. {
  30. public:
  31. CTemplateClassReferences()
  32. {
  33. m_pClassInfo = NULL;
  34. m_bScopeClass = FALSE;
  35. }
  36. CSchemaClassInfo* m_pClassInfo;
  37. BOOL m_bScopeClass;
  38. CTemplateObjectTypeListRef m_templateObjectListRef;
  39. };
  40. ////////////////////////////////////////////////////////////////////////
  41. // CTemplateClassReferencesList
  42. class CTemplateClassReferencesList : public CPtrList<CTemplateClassReferences*>
  43. {
  44. public:
  45. CTemplateClassReferencesList() :
  46. CPtrList<CTemplateClassReferences*>(FALSE)
  47. {
  48. }
  49. CTemplateClassReferences* FindReference(LPCWSTR lpszClassName)
  50. {
  51. CTemplateClassReferencesList::iterator i;
  52. for (i = begin(); i != end(); ++i)
  53. {
  54. CTemplateClassReferences* p = (*i);
  55. ASSERT(p != NULL);
  56. if (p->m_pClassInfo != NULL)
  57. {
  58. if (wcscmp(p->m_pClassInfo->GetName(),lpszClassName) == 0)
  59. {
  60. return p;
  61. }
  62. }
  63. }
  64. return NULL;
  65. }
  66. };
  67. ///////////////////////////////////////////////////////////////////////
  68. // CTemplateAccessPermissionsHolder
  69. CTemplateAccessPermissionsHolder::CTemplateAccessPermissionsHolder(CSchemaClassInfo* pClassInfo,
  70. BOOL bScopeClass)
  71. : CAccessPermissionsHolderBase(FALSE)
  72. {
  73. ASSERT(pClassInfo != NULL);
  74. m_pClassInfo = pClassInfo;
  75. m_bScopeClass = bScopeClass;
  76. }
  77. CTemplateAccessPermissionsHolder::~CTemplateAccessPermissionsHolder()
  78. {
  79. Clear();
  80. }
  81. HRESULT CTemplateAccessPermissionsHolder::_LoadAccessRightInfoArrayFromTable( BOOL /*bIgnore*/ ,BOOL /*bIgnore*/)
  82. {
  83. TRACE(L"\nCTemplateAccessPermissionsHolder::_LoadAccessRightInfoArrayFromTable()\n\n");
  84. for(_ACCESS_BIT_MAP* pCurrEntry = (_ACCESS_BIT_MAP*)GetTemplateAccessRightsMap();
  85. pCurrEntry->lpsz != NULL; pCurrEntry++)
  86. {
  87. CAccessRightInfo* pInfo = new CAccessRightInfo();
  88. if( !pInfo )
  89. return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
  90. pInfo->m_szDisplayName = pCurrEntry->lpsz;
  91. pInfo->m_fAccess = pCurrEntry->fMask;
  92. TRACE(L"Display Name = <%s>, Access = 0x%x\n",
  93. pInfo->m_szDisplayName.c_str(), pInfo->m_fAccess);
  94. m_accessRightInfoArr.Add(pInfo);
  95. }
  96. TRACE(L"\nCTemplateAccessPermissionsHolder::_LoadAccessRightInfoArrayFromTable() exiting\n\n");
  97. return S_OK;
  98. }
  99. HRESULT CTemplateAccessPermissionsHolder::GetAccessPermissions(CAdsiObject* pADSIObj)
  100. {
  101. ASSERT(m_pClassInfo != NULL);
  102. return ReadDataFromDS(pADSIObj, pADSIObj->GetNamingContext(),
  103. m_pClassInfo->GetName(),
  104. m_pClassInfo->GetSchemaGUID(), TRUE);
  105. }
  106. BOOL CTemplateAccessPermissionsHolder::SetupFromClassReferences(CTemplateObjectTypeListRef* pRefList)
  107. {
  108. ASSERT(pRefList != NULL);
  109. TRACE(L"\nStart setting up permission holder <%s> from template class references\n\n",
  110. m_pClassInfo->GetName());
  111. // now go through the list of object list references and set
  112. CTemplateObjectTypeListRef refListValidObjectTypes; // keep track of how many suceeded
  113. CTemplateObjectTypeListRef::iterator iObjectType;
  114. for (iObjectType = pRefList->begin(); iObjectType != pRefList->end(); ++iObjectType)
  115. {
  116. BOOL bSet = FALSE;
  117. CTemplateObjectType* pObjectType = (*iObjectType);
  118. ASSERT(pObjectType != NULL);
  119. CTemplatePermissionList* pPermissionList = pObjectType->GetPermissionList();
  120. CTemplatePermissionList::iterator iPermission;
  121. for (iPermission = pPermissionList->begin(); iPermission != pPermissionList->end(); ++iPermission)
  122. {
  123. CTemplatePermission* pTemplatePermission = (*iPermission);
  124. ASSERT(pTemplatePermission != NULL);
  125. // need to match the permission type
  126. if (pTemplatePermission->GetAccessMask() == 0x0)
  127. {
  128. // this is a control right
  129. if (_SetControlRight(pTemplatePermission->GetControlRight()))
  130. {
  131. bSet = TRUE;
  132. }
  133. }
  134. else
  135. {
  136. if (_SetAccessMask(pTemplatePermission->GetName(),
  137. pTemplatePermission->GetAccessMask()))
  138. {
  139. bSet = TRUE;
  140. }
  141. }
  142. } // for permission
  143. if (bSet)
  144. {
  145. // we succeded with the current one, so keep track of it
  146. refListValidObjectTypes.push_back(pObjectType);
  147. }
  148. } // for object type
  149. // verify we got something valid
  150. size_t nValidCount = refListValidObjectTypes.size();
  151. if (nValidCount == 0)
  152. {
  153. TRACE(L"Failed to set up permission holder: no valid references\n");
  154. return FALSE; // nothing was set
  155. }
  156. TRACE(L"Setting up permission holder succeeded\n");
  157. return TRUE; // got valid data
  158. }
  159. BOOL CTemplateAccessPermissionsHolder::_SetControlRight(LPCWSTR lpszControlRight)
  160. {
  161. // need to find the control right and select it
  162. UINT nRightCount = (UINT) m_controlRightInfoArr.GetCount();
  163. for (UINT k=0; k < nRightCount; k++)
  164. {
  165. //TRACE(L"_SetControlRight() comparing <%s> with <%s>\n", m_controlRightInfoArr[k]->GetDisplayName(), lpszControlRight);
  166. //NOTICE: we try to map both the display name or raw display name,
  167. // just in case somebody uses one or the other in the template
  168. if ( (wcscmp(m_controlRightInfoArr[k]->GetLocalizedName(), lpszControlRight) == 0) ||
  169. (wcscmp(m_controlRightInfoArr[k]->GetLdapDisplayName(), lpszControlRight) == 0) )
  170. {
  171. TRACE(L"_SetControlRight(%s) found a match\n", lpszControlRight);
  172. m_controlRightInfoArr[k]->Select(TRUE);
  173. return TRUE;
  174. }
  175. } // for
  176. TRACE(L"_SetControlRight(%s) failed to find a match\n", lpszControlRight);
  177. return FALSE;
  178. }
  179. BOOL CTemplateAccessPermissionsHolder::_SetAccessMask(LPCWSTR lpszName, ULONG fAccessMask)
  180. {
  181. // is it the @ for "this class"
  182. // general rights
  183. if (wcscmp(lpszName, g_lpszThisObject) == 0)
  184. {
  185. return _SetGeneralRighs(fAccessMask);
  186. }
  187. // try property rights ( read or write)
  188. if (_SetPropertyRight(lpszName, fAccessMask))
  189. return TRUE;
  190. // try subobject rigths (create or delete)
  191. if (_SetSubObjectRight(lpszName, fAccessMask))
  192. return TRUE;
  193. TRACE(L"_SetAccessMask(%s, 0x%x) failed to find a match\n", lpszName, fAccessMask);
  194. return FALSE; // no matching
  195. }
  196. BOOL CTemplateAccessPermissionsHolder::_SetGeneralRighs(ULONG fAccessMask)
  197. {
  198. // if full control, just select the first item in the selection array
  199. if (fAccessMask == _GRANT_ALL)
  200. {
  201. TRACE(L"_SetGeneralRighs(0x%x) granting full control\n", fAccessMask);
  202. m_accessRightInfoArr[0]->Select(TRUE);
  203. return TRUE;
  204. }
  205. // try to map into the array of general rights
  206. BOOL bSet = FALSE;
  207. UINT nRightCount = (UINT) m_accessRightInfoArr.GetCount();
  208. for (ULONG k=0; k<nRightCount; k++)
  209. {
  210. if (m_accessRightInfoArr[k]->GetAccess() == fAccessMask)
  211. {
  212. TRACE(L"_SetGeneralRighs(0x%x) granting %s (0x%x)\n",
  213. fAccessMask,
  214. m_accessRightInfoArr[k]->GetDisplayName(),
  215. m_accessRightInfoArr[k]->GetAccess());
  216. m_accessRightInfoArr[k]->Select(TRUE);
  217. bSet = TRUE;
  218. }
  219. } // for
  220. return bSet;
  221. }
  222. BOOL CTemplateAccessPermissionsHolder::_SetPropertyRight(LPCWSTR lpszName, ULONG fAccessMask)
  223. {
  224. for (UINT i = 0; i < m_propertyRightInfoArray.GetCount(); i++)
  225. {
  226. if (wcscmp(lpszName, m_propertyRightInfoArray[i]->GetName()) == 0)
  227. {
  228. // we found a matching property name
  229. BOOL bSet = FALSE;
  230. for (UINT j=0; j< m_propertyRightInfoArray[i]->GetRightCount(); j++)
  231. {
  232. if ((fAccessMask & m_propertyRightInfoArray[i]->GetRight(j)) != 0)
  233. {
  234. // we found a matching right
  235. TRACE(L"Setting property %s \n", m_propertyRightInfoArray[i]->GetRightDisplayString(j));
  236. m_propertyRightInfoArray[i]->SetRight(j, TRUE);
  237. bSet = TRUE;
  238. }
  239. } // for j
  240. return bSet;
  241. } // if
  242. } // for i
  243. //TRACE(L"_SetPropertyRight(%s, 0x%x) failed to match\n", lpszName, fAccessMask);
  244. return FALSE; // did not find anything
  245. }
  246. BOOL CTemplateAccessPermissionsHolder::_SetSubObjectRight(LPCWSTR lpszName, ULONG fAccessMask)
  247. {
  248. for (UINT i = 0; i < m_classRightInfoArray.GetCount(); i++)
  249. {
  250. if (wcscmp(lpszName, m_classRightInfoArray[i]->GetName()) == 0)
  251. {
  252. // we found a matching class name
  253. BOOL bSet = FALSE;
  254. for (UINT j=0; j< m_classRightInfoArray[i]->GetRightCount(); j++)
  255. {
  256. if ((fAccessMask & m_classRightInfoArray[i]->GetRight(j)) != 0)
  257. {
  258. // we found a matching right
  259. TRACE(L"Setting subobject right %s\n", m_classRightInfoArray[i]->GetRightDisplayString(j));
  260. m_classRightInfoArray[i]->SetRight(j, TRUE);
  261. bSet = TRUE;
  262. }
  263. } // for j
  264. return bSet;
  265. } // if
  266. } // for i
  267. //TRACE(L"_SetSubObjectRight(%s, 0x%x) failed to match\n", lpszName, fAccessMask);
  268. return FALSE; // did not find anything
  269. }
  270. DWORD CTemplateAccessPermissionsHolder::UpdateAccessList(CPrincipal* pPrincipal,
  271. LPCWSTR lpszServerName,
  272. LPCWSTR lpszPhysicalSchemaNamingContext,
  273. PACL *ppAcl )
  274. {
  275. // just call base class function with the embedded info
  276. ASSERT(m_pClassInfo != NULL);
  277. CSchemaClassInfo* pClassInfo = m_pClassInfo;
  278. if (m_bScopeClass)
  279. {
  280. pClassInfo = NULL;
  281. }
  282. return CAccessPermissionsHolderBase::UpdateAccessList(pPrincipal,
  283. pClassInfo,
  284. lpszServerName,
  285. lpszPhysicalSchemaNamingContext,
  286. ppAcl);
  287. }
  288. ///////////////////////////////////////////////////////////////////////
  289. // CTemplateAccessPermissionsHolderManager
  290. BOOL CTemplateAccessPermissionsHolderManager::LoadTemplates()
  291. {
  292. // REVIEW_MARCOC: need to load from registry
  293. // need to ask Praerit about the details
  294. return m_templateManager.Load(L"delegwiz.inf");
  295. }
  296. BOOL CTemplateAccessPermissionsHolderManager::HasTemplates(LPCWSTR lpszClass)
  297. {
  298. return m_templateManager.HasTemplates(lpszClass);
  299. }
  300. BOOL CTemplateAccessPermissionsHolderManager::HasSelectedTemplates()
  301. {
  302. return m_templateManager.HasSelectedTemplates();
  303. }
  304. void CTemplateAccessPermissionsHolderManager::DeselectAll()
  305. {
  306. m_templateManager.DeselectAll();
  307. }
  308. BOOL CTemplateAccessPermissionsHolderManager::
  309. InitPermissionHoldersFromSelectedTemplates(CGrowableArr<CSchemaClassInfo>* pSchemaClassesInfoArray,
  310. CAdsiObject* pADSIObj)
  311. {
  312. TRACE(L"\n\nInitPermissionHoldersFromSelectedTemplates()\n\n");
  313. // reset the array of permission holders
  314. m_permissionHolderArray.Clear();
  315. CTemplateClassReferencesList templateClassReferencesList;
  316. LPCWSTR lpszScopeClass = pADSIObj->GetClass();
  317. // loop on all templates that apply to this scope class and are selected
  318. CTemplateList::iterator iTemplate;
  319. CTemplateList* pTemplateList = m_templateManager.GetTemplateList();
  320. for (iTemplate = pTemplateList->begin(); iTemplate != pTemplateList->end(); ++iTemplate)
  321. {
  322. CTemplate* pTemplate = (*iTemplate);
  323. ASSERT(pTemplate != NULL);
  324. // the template must apply to this class and be selected
  325. if (pTemplate->AppliesToClass(lpszScopeClass) && pTemplate->m_bSelected)
  326. {
  327. // loop on all the pertinent object types in the template
  328. CTemplateObjectTypeList* pObjectTypeList = pTemplate->GetObjectTypeList();
  329. CTemplateObjectTypeList::iterator iObjectType;
  330. for (iObjectType = pObjectTypeList->begin();
  331. iObjectType != pObjectTypeList->end(); ++iObjectType)
  332. {
  333. CTemplateObjectType* pObjectType = (*iObjectType);
  334. ASSERT(pObjectType != NULL);
  335. LPCWSTR lpszCurrentClassName = pObjectType->GetObjectName();
  336. // does the object type refer to the SCOPE keyword?
  337. BOOL bScopeClass = (wcscmp(g_lpszScope, lpszCurrentClassName) == 0);
  338. if (!bScopeClass)
  339. {
  340. // if not, does the object type refer to the scope class?
  341. bScopeClass = (wcscmp(lpszScopeClass, lpszCurrentClassName) == 0);
  342. }
  343. // see if we already have a reference to it
  344. CTemplateClassReferences* pClassReference =
  345. templateClassReferencesList.FindReference(lpszCurrentClassName);
  346. if (pClassReference == NULL)
  347. {
  348. // not seen before can we find the class into the schema?
  349. CSchemaClassInfo* pChildClassInfo =
  350. _FindClassByName(bScopeClass ? lpszScopeClass : lpszCurrentClassName,
  351. pSchemaClassesInfoArray);
  352. if (pChildClassInfo != NULL)
  353. {
  354. // found the class, create a new reference
  355. pClassReference = new CTemplateClassReferences();
  356. pClassReference->m_pClassInfo = pChildClassInfo;
  357. pClassReference->m_bScopeClass = bScopeClass;
  358. // add it to the reference list
  359. templateClassReferencesList.push_back(pClassReference);
  360. }
  361. }
  362. if (pClassReference != NULL)
  363. {
  364. // we have a valid class reference
  365. ASSERT(pClassReference->m_bScopeClass == bScopeClass);
  366. ASSERT(pClassReference->m_pClassInfo != NULL);
  367. // add the object type
  368. pClassReference->m_templateObjectListRef.push_back(pObjectType);
  369. }
  370. } // for all object types
  371. } // if applicable template
  372. } // for all templates
  373. // now we have a list of references to work on
  374. // for each reference we have to create a permission holder and set it
  375. CTemplateClassReferencesList::iterator iClassRef;
  376. for (iClassRef = templateClassReferencesList.begin(); iClassRef != templateClassReferencesList.end(); ++iClassRef)
  377. {
  378. CTemplateClassReferences* pClassRef = (*iClassRef);
  379. ASSERT(pClassRef != NULL);
  380. TRACE(L"\nStart processing class references for class <%s>\n", pClassRef->m_pClassInfo->GetName());
  381. // for the given class reference, need to retain the class info
  382. CTemplateAccessPermissionsHolder* pPermissionHolder =
  383. new CTemplateAccessPermissionsHolder(pClassRef->m_pClassInfo,
  384. pClassRef->m_bScopeClass);
  385. HRESULT hr = pPermissionHolder->GetAccessPermissions(pADSIObj);
  386. if (SUCCEEDED(hr))
  387. {
  388. if (pPermissionHolder->SetupFromClassReferences(&(pClassRef->m_templateObjectListRef)))
  389. {
  390. // successfully set up, can add to the list
  391. m_permissionHolderArray.Add(pPermissionHolder);
  392. pPermissionHolder = NULL;
  393. }
  394. }
  395. if (pPermissionHolder != NULL)
  396. {
  397. TRACE(L"Invalid class references, throwing away permission holder\n");
  398. // invalid one, just throw away
  399. delete pPermissionHolder;
  400. }
  401. TRACE(L"End processing class references for class <%s>\n", pClassRef->m_pClassInfo->GetName());
  402. } // for each class reference
  403. TRACE(L"\nInitPermissionHoldersFromSelectedTemplates() has %d valid holders\n\n\n",
  404. m_permissionHolderArray.GetCount());
  405. // we must have at least a valid and set template holder
  406. return m_permissionHolderArray.GetCount() > 0;
  407. }
  408. DWORD CTemplateAccessPermissionsHolderManager::UpdateAccessList(
  409. CPrincipal* pPrincipal,
  410. LPCWSTR lpszServerName,
  411. LPCWSTR lpszPhysicalSchemaNamingContext,
  412. PACL *ppAcl)
  413. {
  414. // apply each permission holder in the list
  415. long nCount = (long) m_permissionHolderArray.GetCount();
  416. for (long k=0; k<nCount; k++)
  417. {
  418. CTemplateAccessPermissionsHolder* pCurrHolder = m_permissionHolderArray[k];
  419. DWORD dwErr = pCurrHolder->UpdateAccessList(pPrincipal,
  420. lpszServerName,
  421. lpszPhysicalSchemaNamingContext,
  422. ppAcl);
  423. if (dwErr != 0)
  424. return dwErr;
  425. }
  426. return 0;
  427. }
  428. ///////////////// UI related operations //////////////////////////////////////////
  429. BOOL CTemplateAccessPermissionsHolderManager::FillTemplatesListView(
  430. CCheckListViewHelper* pListViewHelper,
  431. LPCWSTR lpszClass)
  432. {
  433. // clear check list
  434. pListViewHelper->DeleteAllItems();
  435. ULONG iListViewItem = 0;
  436. CTemplateList::iterator i;
  437. CTemplateList* pList = m_templateManager.GetTemplateList();
  438. for (i = pList->begin(); i != pList->end(); ++i)
  439. {
  440. CTemplate* p = (*i);
  441. ASSERT(p != NULL);
  442. if (p->AppliesToClass(lpszClass))
  443. {
  444. pListViewHelper->InsertItem(iListViewItem,
  445. p->GetDescription(),
  446. (LPARAM)p,
  447. p->m_bSelected);
  448. iListViewItem++;
  449. }
  450. }
  451. return (iListViewItem > 0);
  452. }
  453. void CTemplateAccessPermissionsHolderManager::WriteSummary(CWString& szSummary,
  454. LPCWSTR lpszIdent, LPCWSTR lpszNewLine)
  455. {
  456. WriteSummaryTitleLine(szSummary, IDS_DELEGWIZ_FINISH_TEMPLATE, lpszNewLine);
  457. CTemplateList::iterator i;
  458. CTemplateList* pList = m_templateManager.GetTemplateList();
  459. for (i = pList->begin(); i != pList->end(); ++i)
  460. {
  461. CTemplate* p = (*i);
  462. ASSERT(p != NULL);
  463. if (p->m_bSelected)
  464. {
  465. WriteSummaryLine(szSummary, p->GetDescription(), lpszIdent, lpszNewLine);
  466. }
  467. }
  468. szSummary += lpszNewLine;
  469. }