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.

599 lines
19 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) == m_accessRightInfoArr[k]->GetAccess())
  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. m_propertyRightInfoArray[i]->SetRight(j, TRUE);
  235. bSet = TRUE;
  236. }
  237. } // for j
  238. return bSet;
  239. } // if
  240. } // for i
  241. //TRACE(L"_SetPropertyRight(%s, 0x%x) failed to match\n", lpszName, fAccessMask);
  242. return FALSE; // did not find anything
  243. }
  244. BOOL CTemplateAccessPermissionsHolder::_SetSubObjectRight(LPCWSTR lpszName, ULONG fAccessMask)
  245. {
  246. for (UINT i = 0; i < m_classRightInfoArray.GetCount(); i++)
  247. {
  248. if (wcscmp(lpszName, m_classRightInfoArray[i]->GetName()) == 0)
  249. {
  250. // we found a matching class name
  251. BOOL bSet = FALSE;
  252. for (UINT j=0; j< m_classRightInfoArray[i]->GetRightCount(); j++)
  253. {
  254. if ((fAccessMask & m_classRightInfoArray[i]->GetRight(j)) != 0)
  255. {
  256. // we found a matching right
  257. m_classRightInfoArray[i]->SetRight(j, TRUE);
  258. bSet = TRUE;
  259. }
  260. } // for j
  261. return bSet;
  262. } // if
  263. } // for i
  264. //TRACE(L"_SetSubObjectRight(%s, 0x%x) failed to match\n", lpszName, fAccessMask);
  265. return FALSE; // did not find anything
  266. }
  267. DWORD CTemplateAccessPermissionsHolder::UpdateAccessList(CPrincipal* pPrincipal,
  268. LPCWSTR lpszServerName,
  269. LPCWSTR lpszPhysicalSchemaNamingContext,
  270. PACL *ppAcl )
  271. {
  272. // just call base class function with the embedded info
  273. ASSERT(m_pClassInfo != NULL);
  274. CSchemaClassInfo* pClassInfo = m_pClassInfo;
  275. if (m_bScopeClass)
  276. {
  277. pClassInfo = NULL;
  278. }
  279. return CAccessPermissionsHolderBase::UpdateAccessList(pPrincipal,
  280. pClassInfo,
  281. lpszServerName,
  282. lpszPhysicalSchemaNamingContext,
  283. ppAcl);
  284. }
  285. ///////////////////////////////////////////////////////////////////////
  286. // CTemplateAccessPermissionsHolderManager
  287. BOOL CTemplateAccessPermissionsHolderManager::LoadTemplates()
  288. {
  289. // REVIEW_MARCOC: need to load from registry
  290. // need to ask Praerit about the details
  291. return m_templateManager.Load(L"delegwiz.inf");
  292. }
  293. BOOL CTemplateAccessPermissionsHolderManager::HasTemplates(LPCWSTR lpszClass)
  294. {
  295. return m_templateManager.HasTemplates(lpszClass);
  296. }
  297. BOOL CTemplateAccessPermissionsHolderManager::HasSelectedTemplates()
  298. {
  299. return m_templateManager.HasSelectedTemplates();
  300. }
  301. void CTemplateAccessPermissionsHolderManager::DeselectAll()
  302. {
  303. m_templateManager.DeselectAll();
  304. }
  305. BOOL CTemplateAccessPermissionsHolderManager::
  306. InitPermissionHoldersFromSelectedTemplates(CGrowableArr<CSchemaClassInfo>* pSchemaClassesInfoArray,
  307. CAdsiObject* pADSIObj)
  308. {
  309. TRACE(L"\n\nInitPermissionHoldersFromSelectedTemplates()\n\n");
  310. // reset the array of permission holders
  311. m_permissionHolderArray.Clear();
  312. CTemplateClassReferencesList templateClassReferencesList;
  313. LPCWSTR lpszScopeClass = pADSIObj->GetClass();
  314. // loop on all templates that apply to this scope class and are selected
  315. CTemplateList::iterator iTemplate;
  316. CTemplateList* pTemplateList = m_templateManager.GetTemplateList();
  317. for (iTemplate = pTemplateList->begin(); iTemplate != pTemplateList->end(); ++iTemplate)
  318. {
  319. CTemplate* pTemplate = (*iTemplate);
  320. ASSERT(pTemplate != NULL);
  321. // the template must apply to this class and be selected
  322. if (pTemplate->AppliesToClass(lpszScopeClass) && pTemplate->m_bSelected)
  323. {
  324. // loop on all the pertinent object types in the template
  325. CTemplateObjectTypeList* pObjectTypeList = pTemplate->GetObjectTypeList();
  326. CTemplateObjectTypeList::iterator iObjectType;
  327. for (iObjectType = pObjectTypeList->begin();
  328. iObjectType != pObjectTypeList->end(); ++iObjectType)
  329. {
  330. CTemplateObjectType* pObjectType = (*iObjectType);
  331. ASSERT(pObjectType != NULL);
  332. LPCWSTR lpszCurrentClassName = pObjectType->GetObjectName();
  333. // does the object type refer to the SCOPE keyword?
  334. BOOL bScopeClass = (wcscmp(g_lpszScope, lpszCurrentClassName) == 0);
  335. if (!bScopeClass)
  336. {
  337. // if not, does the object type refer to the scope class?
  338. bScopeClass = (wcscmp(lpszScopeClass, lpszCurrentClassName) == 0);
  339. }
  340. // see if we already have a reference to it
  341. CTemplateClassReferences* pClassReference =
  342. templateClassReferencesList.FindReference(lpszCurrentClassName);
  343. if (pClassReference == NULL)
  344. {
  345. // not seen before can we find the class into the schema?
  346. CSchemaClassInfo* pChildClassInfo =
  347. _FindClassByName(bScopeClass ? lpszScopeClass : lpszCurrentClassName,
  348. pSchemaClassesInfoArray);
  349. if (pChildClassInfo != NULL)
  350. {
  351. // found the class, create a new reference
  352. pClassReference = new CTemplateClassReferences();
  353. pClassReference->m_pClassInfo = pChildClassInfo;
  354. pClassReference->m_bScopeClass = bScopeClass;
  355. // add it to the reference list
  356. templateClassReferencesList.push_back(pClassReference);
  357. }
  358. }
  359. if (pClassReference != NULL)
  360. {
  361. // we have a valid class reference
  362. ASSERT(pClassReference->m_bScopeClass == bScopeClass);
  363. ASSERT(pClassReference->m_pClassInfo != NULL);
  364. // add the object type
  365. pClassReference->m_templateObjectListRef.push_back(pObjectType);
  366. }
  367. } // for all object types
  368. } // if applicable template
  369. } // for all templates
  370. // now we have a list of references to work on
  371. // for each reference we have to create a permission holder and set it
  372. CTemplateClassReferencesList::iterator iClassRef;
  373. for (iClassRef = templateClassReferencesList.begin(); iClassRef != templateClassReferencesList.end(); ++iClassRef)
  374. {
  375. CTemplateClassReferences* pClassRef = (*iClassRef);
  376. ASSERT(pClassRef != NULL);
  377. TRACE(L"\nStart processing class references for class <%s>\n", pClassRef->m_pClassInfo->GetName());
  378. // for the given class reference, need to retain the class info
  379. CTemplateAccessPermissionsHolder* pPermissionHolder =
  380. new CTemplateAccessPermissionsHolder(pClassRef->m_pClassInfo,
  381. pClassRef->m_bScopeClass);
  382. HRESULT hr = pPermissionHolder->GetAccessPermissions(pADSIObj);
  383. if (SUCCEEDED(hr))
  384. {
  385. if (pPermissionHolder->SetupFromClassReferences(&(pClassRef->m_templateObjectListRef)))
  386. {
  387. // successfully set up, can add to the list
  388. m_permissionHolderArray.Add(pPermissionHolder);
  389. pPermissionHolder = NULL;
  390. }
  391. }
  392. if (pPermissionHolder != NULL)
  393. {
  394. TRACE(L"Invalid class references, throwing away permission holder\n");
  395. // invalid one, just throw away
  396. delete pPermissionHolder;
  397. //Don't proceed if single template is in error.
  398. m_permissionHolderArray.Clear();
  399. break;
  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. }