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.

395 lines
9.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: _tempcor.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "tempcore.h"
  11. //////////////////////////////////////////////////////////////////////////////
  12. // Keywords for the INF file format
  13. LPCWSTR g_lpszTemplates = L"Templates";
  14. LPCWSTR g_lpszDelegationTemplates = L"DelegationTemplates";
  15. LPCWSTR g_lpszDescription = L"Description";
  16. LPCWSTR g_lpszAppliesToClasses = L"AppliesToClasses";
  17. LPCWSTR g_lpszScope = L"SCOPE";
  18. LPCWSTR g_lpszControlRight = L"CONTROLRIGHT";
  19. LPCWSTR g_lpszThisObject = L"@";
  20. LPCWSTR g_lpszObjectTypes = L"ObjectTypes";
  21. //////////////////////////////////////////////////////////////////////////////////
  22. // parsing of access bits
  23. const int g_nGrantAll = 0; // first in the array
  24. const _ACCESS_BIT_MAP g_AccessBitMap[] = {
  25. { L"GA", _GRANT_ALL },
  26. { L"CC", ACTRL_DS_CREATE_CHILD },
  27. { L"DC", ACTRL_DS_DELETE_CHILD },
  28. { L"RP", ACTRL_DS_READ_PROP },
  29. { L"WP", ACTRL_DS_WRITE_PROP },
  30. { L"SW", ACTRL_DS_SELF },
  31. { L"LC", ACTRL_DS_LIST },
  32. { L"LO", ACTRL_DS_LIST_OBJECT },
  33. { L"DT", ACTRL_DS_DELETE_TREE },
  34. { L"RC", READ_CONTROL },
  35. { L"WD", WRITE_DAC },
  36. { L"WO", WRITE_OWNER },
  37. { L"SD", DELETE },
  38. { NULL, 0x0} // end of table
  39. };
  40. const _ACCESS_BIT_MAP* GetTemplateAccessRightsMap()
  41. {
  42. return g_AccessBitMap;
  43. }
  44. ULONG GetAccessMaskFromString(LPCWSTR lpszAccessBit)
  45. {
  46. if (wcscmp(lpszAccessBit, g_AccessBitMap[g_nGrantAll].lpsz) == 0 )
  47. {
  48. return g_AccessBitMap[g_nGrantAll].fMask;
  49. }
  50. _ACCESS_BIT_MAP* pEntry = (_ACCESS_BIT_MAP*)g_AccessBitMap+1;
  51. while (pEntry->lpsz != NULL)
  52. {
  53. if (wcscmp(lpszAccessBit, pEntry->lpsz) == 0 )
  54. {
  55. return pEntry->fMask;
  56. }
  57. pEntry++;
  58. }
  59. return 0x0;
  60. }
  61. void GetStringFromAccessMask(ULONG fAccessMask, wstring& szAccessMask)
  62. {
  63. if (fAccessMask == g_AccessBitMap[g_nGrantAll].fMask)
  64. {
  65. szAccessMask = g_AccessBitMap[g_nGrantAll].lpsz;
  66. return;
  67. }
  68. szAccessMask = L"";
  69. _ACCESS_BIT_MAP* pEntry = (_ACCESS_BIT_MAP*)(g_AccessBitMap+1);
  70. while (pEntry->lpsz != NULL)
  71. {
  72. if ( fAccessMask & pEntry->fMask)
  73. {
  74. szAccessMask += pEntry->lpsz;
  75. szAccessMask += L"";
  76. }
  77. pEntry++;
  78. }
  79. }
  80. ////////////////////////////////////////////////////////////////////////
  81. // CTemplateManager
  82. BOOL CTemplateManager::HasTemplates(LPCWSTR lpszClass)
  83. {
  84. CTemplateList::iterator i;
  85. for (i = m_templateList.begin(); i != m_templateList.end(); ++i)
  86. {
  87. if ((*i)->AppliesToClass(lpszClass))
  88. {
  89. return TRUE;
  90. }
  91. }
  92. return FALSE;
  93. }
  94. BOOL CTemplateManager::HasSelectedTemplates()
  95. {
  96. CTemplateList::iterator i;
  97. for (i = m_templateList.begin(); i != m_templateList.end(); ++i)
  98. {
  99. if ((*i)->m_bSelected)
  100. {
  101. return TRUE;
  102. }
  103. }
  104. return FALSE;
  105. }
  106. void CTemplateManager::DeselectAll()
  107. {
  108. CTemplateList::iterator i;
  109. for (i = m_templateList.begin(); i != m_templateList.end(); ++i)
  110. {
  111. (*i)->m_bSelected = FALSE;
  112. }
  113. }
  114. HRESULT
  115. CTemplateManager::_LoadTemplatePermission(HINF InfHandle,
  116. LPCWSTR lpszPermissionSection,
  117. LPCWSTR lpszPermission,
  118. CTemplateObjectType* pObjectType)
  119. {
  120. CInfList permissionList(InfHandle);
  121. if (!permissionList.Bind(lpszPermissionSection, lpszPermission))
  122. {
  123. return E_FAIL;
  124. }
  125. TRACE(L"_LoadTemplatePermission(%s)\n", lpszPermission);
  126. // special case control access
  127. if (wcscmp(lpszPermission, g_lpszControlRight) == 0)
  128. {
  129. // read the list of controlright strings
  130. while(permissionList.MoveNext())
  131. {
  132. // read the rights
  133. TRACE(L"control right <%s>\n", permissionList.GetBuf());
  134. CTemplatePermission* pCurrPermission = new CTemplatePermission();
  135. if(pCurrPermission)
  136. {
  137. pCurrPermission->SetControlRight(permissionList.GetBuf());
  138. pObjectType->GetPermissionList()->push_back(pCurrPermission);
  139. }
  140. else
  141. {
  142. return E_OUTOFMEMORY;
  143. }
  144. } // while
  145. }
  146. else
  147. {
  148. // any other access mask (including g_lpszThisObject == "@")
  149. ULONG fAccessMask = 0;
  150. while(permissionList.MoveNext())
  151. {
  152. // read the rights
  153. TRACE(L"right <%s>\n", permissionList.GetBuf());
  154. fAccessMask |= GetAccessMaskFromString(permissionList.GetBuf());
  155. } // while
  156. TRACE(L"fAccessMask = 0x%x\n", fAccessMask);
  157. if (fAccessMask != 0)
  158. {
  159. CTemplatePermission* pCurrPermission = new CTemplatePermission();
  160. if( pCurrPermission )
  161. {
  162. pCurrPermission->SetAccessMask(lpszPermission, fAccessMask);
  163. pObjectType->GetPermissionList()->push_back(pCurrPermission);
  164. }
  165. else
  166. {
  167. return E_OUTOFMEMORY;
  168. }
  169. }
  170. else
  171. {
  172. return E_FAIL;
  173. }
  174. }
  175. return S_OK;
  176. }
  177. HRESULT CTemplateManager::_LoadTemplatePermissionsSection(HINF InfHandle,
  178. LPCWSTR lpszTemplateName,
  179. LPCWSTR lpszObject,
  180. CTemplate* pCurrTemplate)
  181. {
  182. WCHAR szPermissionSection[N_BUF_LEN];
  183. HRESULT hr = S_OK;
  184. hr = StringCchPrintf(szPermissionSection, N_BUF_LEN, L"%s.%s", lpszTemplateName, lpszObject);
  185. if(FAILED(hr))
  186. return hr;
  187. TRACE(L" szPermissionSection = <%s>\n", szPermissionSection);
  188. CInfSectionKeys permissionSection(InfHandle);
  189. if (!permissionSection.Bind(szPermissionSection))
  190. {
  191. return hr;
  192. }
  193. CTemplateObjectType* pObjectType = NULL;
  194. while (permissionSection.MoveNext())
  195. {
  196. if (pObjectType == NULL)
  197. {
  198. pObjectType = new CTemplateObjectType(lpszObject);
  199. }
  200. TRACE(L" <%s>\n", permissionSection.GetBuf());
  201. hr = _LoadTemplatePermission(InfHandle, szPermissionSection, permissionSection.GetBuf(), pObjectType);
  202. if(FAILED(hr))
  203. {
  204. delete pObjectType;
  205. pObjectType = NULL;
  206. break;
  207. }
  208. } // while
  209. if (pObjectType != NULL)
  210. {
  211. // need to validate template data
  212. if (pObjectType->GetPermissionList()->size() > 0)
  213. {
  214. pCurrTemplate->GetObjectTypeList()->push_back(pObjectType);
  215. }
  216. else
  217. {
  218. delete pObjectType;
  219. }
  220. }
  221. return hr;
  222. }
  223. void CTemplateManager::_LoadTemplate(HINF InfHandle, LPCWSTR lpszTemplateName)
  224. {
  225. // read the template description
  226. CInfLine descriptionLine(InfHandle);
  227. if (!descriptionLine.Bind(lpszTemplateName, g_lpszDescription))
  228. {
  229. TRACE(L"Invalid Template: missing description entry\n");
  230. return; // missing entry
  231. }
  232. TRACE(L" Description = <%s>\n", descriptionLine.GetBuf());
  233. if (lstrlen(descriptionLine.GetBuf()) == 0)
  234. {
  235. TRACE(L"Invalid Template: empty description\n");
  236. return; // empty description
  237. }
  238. // read the object types field
  239. CInfList currTemplate(InfHandle);
  240. if (!currTemplate.Bind(lpszTemplateName, g_lpszObjectTypes))
  241. {
  242. TRACE(L"Invalid Template: no objects specified\n");
  243. return; // no objects specified
  244. }
  245. // load the object type sections
  246. CTemplate* pCurrTemplate = NULL;
  247. while (currTemplate.MoveNext())
  248. {
  249. if (pCurrTemplate == NULL)
  250. pCurrTemplate = new CTemplate(descriptionLine.GetBuf());
  251. if(FAILED(_LoadTemplatePermissionsSection(InfHandle, lpszTemplateName, currTemplate.GetBuf(), pCurrTemplate)))
  252. {
  253. if(pCurrTemplate)
  254. delete pCurrTemplate;
  255. pCurrTemplate = NULL;
  256. return;
  257. }
  258. } // while
  259. // add to template list, if not empty
  260. if (pCurrTemplate != NULL)
  261. {
  262. // need to validate template data
  263. if (pCurrTemplate->GetObjectTypeList()->size() > 0)
  264. {
  265. GetTemplateList()->push_back(pCurrTemplate);
  266. }
  267. else
  268. {
  269. TRACE(L"Discarding template: no valid object type sections\n");
  270. delete pCurrTemplate;
  271. pCurrTemplate = NULL;
  272. }
  273. }
  274. if (pCurrTemplate != NULL)
  275. {
  276. // read the applicable classes list, if any
  277. CInfList applicableClasses(InfHandle);
  278. if (applicableClasses.Bind(lpszTemplateName, g_lpszAppliesToClasses))
  279. {
  280. TRACE(L"Applicable to: ");
  281. while (applicableClasses.MoveNext())
  282. {
  283. TRACE(L"<%s>", applicableClasses.GetBuf());
  284. pCurrTemplate->AddAppliesToClass(applicableClasses.GetBuf());
  285. }
  286. TRACE(L"\n");
  287. }
  288. TRACE(L"\nTemplate successfully read into memory\n\n");
  289. } // if
  290. }
  291. BOOL CTemplateManager::_LoadTemplateList(HINF InfHandle)
  292. {
  293. TRACE(L"CTemplateManager::_LoadTemplateList()\n");
  294. // acquire the list of templates in the file
  295. CInfList templatelist(InfHandle);
  296. if (!templatelist.Bind(g_lpszDelegationTemplates, g_lpszTemplates))
  297. {
  298. TRACE(L"CTemplateManager::_LoadTemplateList() failed: invalid template list entry\n");
  299. return FALSE;
  300. }
  301. // loop through the templates and load them
  302. while(templatelist.MoveNext())
  303. {
  304. // process
  305. TRACE(L"\nTemplate = <%s>\n", templatelist.GetBuf());
  306. _LoadTemplate(InfHandle, templatelist.GetBuf());
  307. } // while
  308. if (GetTemplateList()->size() == 0)
  309. {
  310. TRACE(L"CTemplateManager::_LoadTemplateList() failed no valid templates\n");
  311. return FALSE;
  312. }
  313. #ifdef _DUMP
  314. TRACE(L"\n\n\n======= LOADED TEMPLATES ====================\n");
  315. GetTemplateList()->Dump();
  316. TRACE(L"\n===========================\n\n\n");
  317. #endif // _DUMP
  318. return TRUE;
  319. }