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.

386 lines
9.0 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: rules-w.c
  7. //
  8. // Contents: Rule management for WMI.
  9. //
  10. //
  11. // History: KrishnaG.
  12. // AbhisheV.
  13. // t-hhsu
  14. //
  15. //----------------------------------------------------------------------------
  16. #include "precomp.h"
  17. //extern LPWSTR NFADNAttributes[];
  18. DWORD
  19. WMIEnumNFADataEx(
  20. IWbemServices *pWbemServices,
  21. GUID PolicyIdentifier,
  22. PIPSEC_NFA_DATA ** pppIpsecNFAData,
  23. PDWORD pdwNumNFAObjects
  24. )
  25. {
  26. DWORD dwError = 0;
  27. DWORD i = 0;
  28. PIPSEC_NFA_OBJECT * ppIpsecNFAObject = NULL;
  29. DWORD dwNumNFAObjects = 0;
  30. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  31. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  32. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  33. LPWSTR pszAbsPolicyReference = NULL;
  34. LPWSTR pszRelPolicyReference = NULL;
  35. DWORD dwRootPathLen = 0; ////wcslen(pszIpsecRootContainer);
  36. DWORD j = 0;
  37. dwError = ConvertGuidToPolicyStringEx(
  38. PolicyIdentifier,
  39. &pszAbsPolicyReference
  40. );
  41. BAIL_ON_WIN32_ERROR(dwError);
  42. pszRelPolicyReference = pszAbsPolicyReference;
  43. dwError = WMIEnumNFAObjectsEx(
  44. pWbemServices,
  45. pszRelPolicyReference, ////'ipsecNFARef{xxx}'
  46. &ppIpsecNFAObject,
  47. &dwNumNFAObjects
  48. );
  49. BAIL_ON_WIN32_ERROR(dwError);
  50. if (dwNumNFAObjects) {
  51. ppIpsecNFAData = (PIPSEC_NFA_DATA *)AllocPolMem(
  52. sizeof(PIPSEC_NFA_DATA)*dwNumNFAObjects
  53. );
  54. if (!ppIpsecNFAData) {
  55. dwError = ERROR_OUTOFMEMORY;
  56. BAIL_ON_WIN32_ERROR(dwError);
  57. }
  58. }
  59. for (i = 0; i < dwNumNFAObjects; i++) {
  60. pIpsecNFAObject = *(ppIpsecNFAObject + i);
  61. dwError = WMIUnmarshallNFAData(
  62. pIpsecNFAObject,
  63. &pIpsecNFAData
  64. );
  65. if (!dwError) {
  66. *(ppIpsecNFAData + j) = pIpsecNFAData;
  67. j++;
  68. }
  69. }
  70. if (j == 0) {
  71. if (ppIpsecNFAData) {
  72. FreePolMem(ppIpsecNFAData);
  73. ppIpsecNFAData = NULL;
  74. }
  75. }
  76. *pppIpsecNFAData = ppIpsecNFAData;
  77. *pdwNumNFAObjects = j;
  78. dwError = ERROR_SUCCESS;
  79. cleanup:
  80. if (ppIpsecNFAObject) {
  81. FreeIpsecNFAObjects(
  82. ppIpsecNFAObject,
  83. dwNumNFAObjects
  84. );
  85. }
  86. if (pszAbsPolicyReference) {
  87. FreePolStr(pszAbsPolicyReference);
  88. }
  89. return(dwError);
  90. error:
  91. if (ppIpsecNFAData) {
  92. FreeMulIpsecNFAData(
  93. ppIpsecNFAData,
  94. i
  95. );
  96. }
  97. *pppIpsecNFAData = NULL;
  98. *pdwNumNFAObjects = 0;
  99. goto cleanup;
  100. }
  101. DWORD
  102. WMIEnumNFAObjectsEx(
  103. IWbemServices *pWbemServices,
  104. LPWSTR pszIpsecRelPolicyName,
  105. PIPSEC_NFA_OBJECT ** pppIpsecNFAObjects,
  106. PDWORD pdwNumNFAObjects
  107. )
  108. {
  109. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  110. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  111. DWORD dwNumNFAObjects = 0;
  112. DWORD dwError = 0;
  113. HRESULT hr = S_OK;
  114. DWORD dwSize = 0;
  115. HKEY hRegKey = 0;
  116. DWORD i = 0;
  117. DWORD dwCount = 0;
  118. LPWSTR * ppszIpsecNFANames = NULL;
  119. LPWSTR pszIpsecNFAName = NULL;
  120. LPWSTR pszTemp = NULL;
  121. LPWSTR pszString = NULL;
  122. LPWSTR pszIpsecNFAReference = NULL;
  123. LPWSTR pszFilterReference = NULL;
  124. LPWSTR pszNegPolReference = NULL;
  125. ///wbem
  126. IWbemClassObject *pObj = NULL;
  127. LPWSTR objPathA = L"RSOP_IPSECPolicySetting.id=";
  128. LPWSTR objPath = NULL;
  129. BSTR bstrObjPath = NULL;
  130. *pppIpsecNFAObjects = NULL;
  131. *pdwNumNFAObjects = 0;
  132. if(!pszIpsecRelPolicyName || !*pszIpsecRelPolicyName) {
  133. dwError = ERROR_INVALID_DATA;
  134. BAIL_ON_WIN32_ERROR(dwError);
  135. }
  136. objPath = (LPWSTR)AllocPolMem(
  137. sizeof(WCHAR)*(wcslen(objPathA)+wcslen(pszIpsecRelPolicyName)+3)
  138. );
  139. if(!objPath) {
  140. dwError = ERROR_OUTOFMEMORY;
  141. BAIL_ON_WIN32_ERROR(dwError);
  142. }
  143. wcscpy(objPath, objPathA);
  144. wcscat(objPath, L"\"");
  145. wcscat(objPath, pszIpsecRelPolicyName);
  146. wcscat(objPath, L"\"");
  147. bstrObjPath = SysAllocString(objPath);
  148. if(!bstrObjPath) {
  149. dwError = ERROR_OUTOFMEMORY;
  150. BAIL_ON_WIN32_ERROR(dwError);
  151. }
  152. hr = IWbemServices_GetObject(
  153. pWbemServices,
  154. bstrObjPath,
  155. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  156. 0,
  157. &pObj,
  158. 0
  159. );
  160. SysFreeString(bstrObjPath);
  161. BAIL_ON_WMI_ERROR_WITH_WIN32(hr, dwError);
  162. dwError = WMIstoreQueryValue(
  163. pObj,
  164. L"ipsecNFAReference",
  165. VT_ARRAY|VT_BSTR,
  166. (LPBYTE *)&pszIpsecNFAReference,
  167. &dwSize
  168. );
  169. BAIL_ON_WIN32_ERROR(dwError);
  170. pszTemp = pszIpsecNFAReference;
  171. while (*pszTemp != L'\0') {
  172. pszTemp += wcslen(pszTemp) + 1;
  173. dwCount++;
  174. }
  175. if (!dwCount) {
  176. dwError = ERROR_NO_DATA;
  177. BAIL_ON_WIN32_ERROR(dwError);
  178. }
  179. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  180. sizeof(LPWSTR)*dwCount
  181. );
  182. if (!ppszIpsecNFANames) {
  183. dwError = ERROR_OUTOFMEMORY;
  184. BAIL_ON_WIN32_ERROR(dwError);
  185. }
  186. pszTemp = pszIpsecNFAReference;
  187. for (i = 0; i < dwCount; i++) {
  188. pszString = AllocPolStr(pszTemp);
  189. if (!pszString) {
  190. dwError = ERROR_OUTOFMEMORY;
  191. BAIL_ON_WIN32_ERROR(dwError);
  192. }
  193. *(ppszIpsecNFANames + i) = pszString;
  194. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  195. }
  196. //ppszIpsecNFANames => now an array of strings w/ nfa refs
  197. ppIpsecNFAObjects = (PIPSEC_NFA_OBJECT *)AllocPolMem(
  198. sizeof(PIPSEC_NFA_OBJECT)*dwCount
  199. );
  200. if (!ppIpsecNFAObjects) {
  201. dwError = ERROR_OUTOFMEMORY;
  202. BAIL_ON_WIN32_ERROR(dwError);
  203. }
  204. for (i = 0; i < dwCount; i++) {
  205. dwError = UnMarshallWMINFAObject(
  206. pWbemServices,
  207. *(ppszIpsecNFANames + i),
  208. &pIpsecNFAObject,
  209. &pszFilterReference,
  210. &pszNegPolReference
  211. );
  212. if (dwError == ERROR_SUCCESS) {
  213. *(ppIpsecNFAObjects + dwNumNFAObjects) = pIpsecNFAObject;
  214. dwNumNFAObjects++;
  215. if (pszFilterReference) {
  216. FreePolStr(pszFilterReference);
  217. }
  218. if (pszNegPolReference) {
  219. FreePolStr(pszNegPolReference);
  220. }
  221. }
  222. }
  223. *pppIpsecNFAObjects = ppIpsecNFAObjects;
  224. *pdwNumNFAObjects = dwNumNFAObjects;
  225. dwError = ERROR_SUCCESS;
  226. cleanup:
  227. //free
  228. if(pObj)
  229. IWbemClassObject_Release(pObj);
  230. if (pszIpsecNFAReference) {
  231. FreePolStr(pszIpsecNFAReference);
  232. }
  233. if (ppszIpsecNFANames) {
  234. FreeNFAReferences(
  235. ppszIpsecNFANames,
  236. dwCount
  237. );
  238. }
  239. if(objPath) {
  240. FreePolStr(objPath);
  241. }
  242. return(dwError);
  243. error:
  244. if (ppIpsecNFAObjects) {
  245. FreeIpsecNFAObjects(
  246. ppIpsecNFAObjects,
  247. dwNumNFAObjects
  248. );
  249. }
  250. *pppIpsecNFAObjects = NULL;
  251. *pdwNumNFAObjects = 0;
  252. goto cleanup;
  253. }
  254. DWORD
  255. WMIUnmarshallNFAData(
  256. PIPSEC_NFA_OBJECT pIpsecNFAObject,
  257. PIPSEC_NFA_DATA * ppIpsecNFAData
  258. )
  259. {
  260. DWORD dwError = 0;
  261. dwError = UnmarshallNFAObject(
  262. pIpsecNFAObject,
  263. IPSEC_WMI_PROVIDER,
  264. ppIpsecNFAData
  265. );
  266. BAIL_ON_WIN32_ERROR(dwError);
  267. if (*ppIpsecNFAData) {
  268. (*ppIpsecNFAData)->dwFlags |= POLSTORE_READONLY;
  269. }
  270. error:
  271. return(dwError);
  272. }
  273. DWORD
  274. ConvertGuidToPolicyStringEx(
  275. GUID PolicyIdentifier,
  276. LPWSTR * ppszIpsecPolicyReference
  277. )
  278. {
  279. DWORD dwError = 0;
  280. WCHAR szPolicyReference[MAX_PATH];
  281. LPWSTR pszIpsecPolicyReference = NULL;
  282. WCHAR szGuidString[MAX_PATH];
  283. LPWSTR pszStringUuid = NULL;
  284. dwError = UuidToString(
  285. &PolicyIdentifier,
  286. &pszStringUuid
  287. );
  288. BAIL_ON_WIN32_ERROR(dwError);
  289. szGuidString[0] = L'\0';
  290. wcscpy(szGuidString, L"{");
  291. wcscat(szGuidString, pszStringUuid);
  292. wcscat(szGuidString, L"}");
  293. szPolicyReference[0] = L'\0';
  294. wcscpy(szPolicyReference, L"ipsecPolicy");
  295. wcscat(szPolicyReference, szGuidString);
  296. pszIpsecPolicyReference = AllocPolStr(
  297. szPolicyReference
  298. );
  299. if (!pszIpsecPolicyReference) {
  300. dwError = ERROR_OUTOFMEMORY;
  301. BAIL_ON_WIN32_ERROR(dwError);
  302. }
  303. *ppszIpsecPolicyReference = pszIpsecPolicyReference;
  304. cleanup:
  305. if (pszStringUuid) {
  306. RpcStringFree(&pszStringUuid);
  307. }
  308. return(dwError);
  309. error:
  310. *ppszIpsecPolicyReference = NULL;
  311. goto cleanup;
  312. }