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.

570 lines
13 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001.
  5. //
  6. // File: Mngrfldr.cpp
  7. //
  8. // Contents: Wireless Policy Snapin - Policy Main Page Manager.
  9. //
  10. //
  11. // History: TaroonM
  12. // 10/30/01
  13. // Abhishev
  14. //
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "precomp.h"
  18. LPWSTR gpszWirelessCacheKey = L"SOFTWARE\\Policies\\Microsoft\\Windows\\Wireless\\Policy\\Cache";
  19. DWORD
  20. CacheDirectorytoRegistry(
  21. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject
  22. )
  23. {
  24. DWORD dwError = 0;
  25. PWIRELESS_POLICY_OBJECT pWirelessRegPolicyObject = NULL;
  26. //
  27. // Delete the existing cache.
  28. //
  29. DeleteRegistryCache();
  30. //
  31. // Create a copy of the directory policy in registry terms
  32. //
  33. dwError = CloneDirectoryPolicyObject(
  34. pWirelessPolicyObject,
  35. &pWirelessRegPolicyObject
  36. );
  37. BAIL_ON_WIN32_ERROR(dwError);
  38. //
  39. // Write the registry policy
  40. //
  41. dwError = PersistRegistryObject(
  42. pWirelessRegPolicyObject
  43. );
  44. BAIL_ON_WIN32_ERROR(dwError);
  45. cleanup:
  46. if (pWirelessRegPolicyObject) {
  47. FreeWirelessPolicyObject(
  48. pWirelessRegPolicyObject
  49. );
  50. }
  51. return(dwError);
  52. error:
  53. DeleteRegistryCache();
  54. goto cleanup;
  55. }
  56. DWORD
  57. PersistRegistryObject(
  58. PWIRELESS_POLICY_OBJECT pWirelessRegPolicyObject
  59. )
  60. {
  61. DWORD dwError = 0;
  62. HKEY hRegistryKey = NULL;
  63. DWORD dwDisposition = 0;
  64. dwError = RegCreateKeyExW(
  65. HKEY_LOCAL_MACHINE,
  66. gpszWirelessCacheKey,
  67. 0,
  68. NULL,
  69. 0,
  70. KEY_ALL_ACCESS,
  71. NULL,
  72. &hRegistryKey,
  73. &dwDisposition
  74. );
  75. BAIL_ON_WIN32_ERROR(dwError);
  76. dwError = PersistPolicyObject(
  77. hRegistryKey,
  78. pWirelessRegPolicyObject
  79. );
  80. error:
  81. if (hRegistryKey) {
  82. RegCloseKey(hRegistryKey);
  83. }
  84. return(dwError);
  85. }
  86. DWORD
  87. PersistPolicyObject(
  88. HKEY hRegistryKey,
  89. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject
  90. )
  91. {
  92. DWORD dwError = 0;
  93. HKEY hRegKey = NULL;
  94. DWORD dwDisposition = 0;
  95. dwError = RegCreateKeyExW(
  96. hRegistryKey,
  97. pWirelessPolicyObject->pszWirelessOwnersReference,
  98. 0,
  99. NULL,
  100. 0,
  101. KEY_ALL_ACCESS,
  102. NULL,
  103. &hRegKey,
  104. &dwDisposition
  105. );
  106. BAIL_ON_WIN32_ERROR(dwError);
  107. dwError = RegSetValueExW(
  108. hRegKey,
  109. L"ClassName",
  110. 0,
  111. REG_SZ,
  112. (LPBYTE) L"msieee80211-Policy",
  113. (wcslen(L"msieee80211-Policy") + 1)*sizeof(WCHAR)
  114. );
  115. BAIL_ON_WIN32_ERROR(dwError);
  116. if (pWirelessPolicyObject->pszDescription) {
  117. dwError = RegSetValueExW(
  118. hRegKey,
  119. L"description",
  120. 0,
  121. REG_SZ,
  122. (LPBYTE)pWirelessPolicyObject->pszDescription,
  123. (wcslen(pWirelessPolicyObject->pszDescription) + 1)*sizeof(WCHAR)
  124. );
  125. BAIL_ON_WIN32_ERROR(dwError);
  126. }
  127. else {
  128. (VOID) RegDeleteValueW(
  129. hRegKey,
  130. L"description"
  131. );
  132. }
  133. if (pWirelessPolicyObject->pszWirelessOwnersReference) {
  134. dwError = RegSetValueExW(
  135. hRegKey,
  136. L"name",
  137. 0,
  138. REG_SZ,
  139. (LPBYTE)pWirelessPolicyObject->pszWirelessOwnersReference,
  140. (wcslen(pWirelessPolicyObject->pszWirelessOwnersReference) + 1)*sizeof(WCHAR)
  141. );
  142. BAIL_ON_WIN32_ERROR(dwError);
  143. }
  144. if (pWirelessPolicyObject->pszWirelessName) {
  145. dwError = RegSetValueExW(
  146. hRegKey,
  147. L"WirelessName",
  148. 0,
  149. REG_SZ,
  150. (LPBYTE)pWirelessPolicyObject->pszWirelessName,
  151. (wcslen(pWirelessPolicyObject->pszWirelessName) + 1)*sizeof(WCHAR)
  152. );
  153. BAIL_ON_WIN32_ERROR(dwError);
  154. }
  155. if (pWirelessPolicyObject->pszWirelessID) {
  156. dwError = RegSetValueExW(
  157. hRegKey,
  158. L"WirelessID",
  159. 0,
  160. REG_SZ,
  161. (LPBYTE)pWirelessPolicyObject->pszWirelessID,
  162. (wcslen(pWirelessPolicyObject->pszWirelessID) + 1)*sizeof(WCHAR)
  163. );
  164. BAIL_ON_WIN32_ERROR(dwError);
  165. }
  166. dwError = RegSetValueExW(
  167. hRegKey,
  168. L"WirelessDataType",
  169. 0,
  170. REG_DWORD,
  171. (LPBYTE)&pWirelessPolicyObject->dwWirelessDataType,
  172. sizeof(DWORD)
  173. );
  174. BAIL_ON_WIN32_ERROR(dwError);
  175. if (pWirelessPolicyObject->pWirelessData) {
  176. dwError = RegSetValueExW(
  177. hRegKey,
  178. L"WirelessData",
  179. 0,
  180. REG_BINARY,
  181. pWirelessPolicyObject->pWirelessData,
  182. pWirelessPolicyObject->dwWirelessDataLen
  183. );
  184. BAIL_ON_WIN32_ERROR(dwError);
  185. }
  186. dwError = RegSetValueExW(
  187. hRegKey,
  188. L"whenChanged",
  189. 0,
  190. REG_DWORD,
  191. (LPBYTE)&pWirelessPolicyObject->dwWhenChanged,
  192. sizeof(DWORD)
  193. );
  194. BAIL_ON_WIN32_ERROR(dwError);
  195. error:
  196. if (hRegKey) {
  197. RegCloseKey(hRegKey);
  198. }
  199. return(dwError);
  200. }
  201. DWORD
  202. CloneDirectoryPolicyObject(
  203. PWIRELESS_POLICY_OBJECT pWirelessPolicyObject,
  204. PWIRELESS_POLICY_OBJECT * ppWirelessRegPolicyObject
  205. )
  206. {
  207. DWORD dwError = 0;
  208. PWIRELESS_POLICY_OBJECT pWirelessRegPolicyObject = NULL;
  209. pWirelessRegPolicyObject = (PWIRELESS_POLICY_OBJECT)AllocPolMem(
  210. sizeof(WIRELESS_POLICY_OBJECT)
  211. );
  212. if (!pWirelessRegPolicyObject) {
  213. dwError = ERROR_OUTOFMEMORY;
  214. BAIL_ON_WIN32_ERROR(dwError);
  215. }
  216. //
  217. // Now copy the rest of the data in the object
  218. //
  219. if (pWirelessPolicyObject->pszWirelessOwnersReference) {
  220. dwError = CopyPolicyDSToRegString(
  221. pWirelessPolicyObject->pszWirelessOwnersReference,
  222. &pWirelessRegPolicyObject->pszWirelessOwnersReference
  223. );
  224. BAIL_ON_WIN32_ERROR(dwError);
  225. }
  226. if (pWirelessPolicyObject->pszWirelessName) {
  227. pWirelessRegPolicyObject->pszWirelessName = AllocPolStr(
  228. pWirelessPolicyObject->pszWirelessName
  229. );
  230. if (!pWirelessRegPolicyObject->pszWirelessName) {
  231. dwError = ERROR_OUTOFMEMORY;
  232. BAIL_ON_WIN32_ERROR(dwError);
  233. }
  234. }
  235. if (pWirelessPolicyObject->pszWirelessID) {
  236. pWirelessRegPolicyObject->pszWirelessID = AllocPolStr(
  237. pWirelessPolicyObject->pszWirelessID
  238. );
  239. if (!pWirelessRegPolicyObject->pszWirelessID) {
  240. dwError = ERROR_OUTOFMEMORY;
  241. BAIL_ON_WIN32_ERROR(dwError);
  242. }
  243. }
  244. pWirelessRegPolicyObject->dwWirelessDataType =
  245. pWirelessPolicyObject->dwWirelessDataType;
  246. if (pWirelessPolicyObject->pWirelessData) {
  247. dwError = CopyBinaryValue(
  248. pWirelessPolicyObject->pWirelessData,
  249. pWirelessPolicyObject->dwWirelessDataLen,
  250. &pWirelessRegPolicyObject->pWirelessData
  251. );
  252. BAIL_ON_WIN32_ERROR(dwError);
  253. pWirelessRegPolicyObject->dwWirelessDataLen =
  254. pWirelessPolicyObject->dwWirelessDataLen;
  255. }
  256. if (pWirelessPolicyObject->pszDescription) {
  257. pWirelessRegPolicyObject->pszDescription = AllocPolStr(
  258. pWirelessPolicyObject->pszDescription
  259. );
  260. if (!pWirelessRegPolicyObject->pszDescription) {
  261. dwError = ERROR_OUTOFMEMORY;
  262. BAIL_ON_WIN32_ERROR(dwError);
  263. }
  264. }
  265. pWirelessRegPolicyObject->dwWhenChanged =
  266. pWirelessPolicyObject->dwWhenChanged;
  267. *ppWirelessRegPolicyObject = pWirelessRegPolicyObject;
  268. return(dwError);
  269. error:
  270. if (pWirelessRegPolicyObject) {
  271. FreeWirelessPolicyObject(
  272. pWirelessRegPolicyObject
  273. );
  274. }
  275. *ppWirelessRegPolicyObject = NULL;
  276. return(dwError);
  277. }
  278. DWORD
  279. CopyBinaryValue(
  280. LPBYTE pMem,
  281. DWORD dwMemSize,
  282. LPBYTE * ppNewMem
  283. )
  284. {
  285. LPBYTE pNewMem = NULL;
  286. DWORD dwError = 0;
  287. pNewMem = (LPBYTE)AllocPolMem(dwMemSize);
  288. if (!pNewMem) {
  289. dwError = ERROR_OUTOFMEMORY;
  290. BAIL_ON_WIN32_ERROR(dwError);
  291. }
  292. memcpy(pNewMem, pMem, dwMemSize);
  293. *ppNewMem = pNewMem;
  294. return(dwError);
  295. error:
  296. if (pNewMem) {
  297. FreePolMem(pNewMem);
  298. }
  299. *ppNewMem = NULL;
  300. return(dwError);
  301. }
  302. DWORD
  303. CopyPolicyDSToFQRegString(
  304. LPWSTR pszPolicyDN,
  305. LPWSTR * ppszPolicyName
  306. )
  307. {
  308. DWORD dwError = 0;
  309. WCHAR szCommonName[MAX_PATH];
  310. LPWSTR pszGuidName = NULL;
  311. LPWSTR pszPolicyName = NULL;
  312. DWORD dwStringSize = 0;
  313. dwError = ComputePrelimCN(
  314. pszPolicyDN,
  315. szCommonName
  316. );
  317. BAIL_ON_WIN32_ERROR(dwError);
  318. dwError = ComputeGUIDName(
  319. szCommonName,
  320. &pszGuidName
  321. );
  322. BAIL_ON_WIN32_ERROR(dwError);
  323. dwStringSize = wcslen(gpszWirelessCacheKey);
  324. dwStringSize += 1;
  325. dwStringSize += wcslen(pszGuidName);
  326. dwStringSize += 1;
  327. pszPolicyName = (LPWSTR)AllocPolMem(dwStringSize*sizeof(WCHAR));
  328. if (!pszPolicyName) {
  329. dwError = ERROR_OUTOFMEMORY;
  330. BAIL_ON_WIN32_ERROR(dwError);
  331. }
  332. wcscpy(pszPolicyName, gpszWirelessCacheKey);
  333. wcscat(pszPolicyName, L"\\");
  334. wcscat(pszPolicyName, pszGuidName);
  335. *ppszPolicyName = pszPolicyName;
  336. return(dwError);
  337. error:
  338. *ppszPolicyName = NULL;
  339. return(dwError);
  340. }
  341. DWORD
  342. ComputeGUIDName(
  343. LPWSTR szCommonName,
  344. LPWSTR * ppszGuidName
  345. )
  346. {
  347. LPWSTR pszGuidName = NULL;
  348. DWORD dwError = 0;
  349. pszGuidName = wcschr(szCommonName, L'=');
  350. if (!pszGuidName) {
  351. dwError = ERROR_INVALID_PARAMETER;
  352. BAIL_ON_WIN32_ERROR(dwError);
  353. }
  354. *ppszGuidName = (pszGuidName + 1);
  355. return(dwError);
  356. error:
  357. *ppszGuidName = NULL;
  358. return(dwError);
  359. }
  360. DWORD
  361. DeleteRegistryCache(
  362. )
  363. {
  364. DWORD dwError = 0;
  365. HKEY hParentKey = NULL;
  366. WCHAR lpszName[MAX_PATH];
  367. DWORD dwSize = 0;
  368. dwError = RegOpenKeyExW(
  369. HKEY_LOCAL_MACHINE,
  370. gpszWirelessCacheKey,
  371. 0,
  372. KEY_ALL_ACCESS,
  373. &hParentKey
  374. );
  375. BAIL_ON_WIN32_ERROR(dwError);
  376. memset(lpszName, 0, sizeof(WCHAR)*MAX_PATH);
  377. dwSize = MAX_PATH;
  378. while((RegEnumKeyExW(hParentKey, 0, lpszName,
  379. &dwSize, NULL,
  380. NULL, NULL,NULL)) == ERROR_SUCCESS) {
  381. dwError = RegDeleteKeyW(
  382. hParentKey,
  383. lpszName
  384. );
  385. if (dwError != ERROR_SUCCESS) {
  386. break;
  387. }
  388. memset(lpszName, 0, sizeof(WCHAR)*MAX_PATH);
  389. dwSize = MAX_PATH;
  390. }
  391. error:
  392. if (hParentKey) {
  393. RegCloseKey(hParentKey);
  394. }
  395. return(dwError);
  396. }
  397. DWORD
  398. CopyPolicyDSToRegString(
  399. LPWSTR pszPolicyDN,
  400. LPWSTR * ppszPolicyName
  401. )
  402. {
  403. DWORD dwError = 0;
  404. WCHAR szCommonName[MAX_PATH];
  405. LPWSTR pszGuidName = NULL;
  406. LPWSTR pszPolicyName = NULL;
  407. dwError = ComputePrelimCN(
  408. pszPolicyDN,
  409. szCommonName
  410. );
  411. BAIL_ON_WIN32_ERROR(dwError);
  412. dwError = ComputeGUIDName(
  413. szCommonName,
  414. &pszGuidName
  415. );
  416. BAIL_ON_WIN32_ERROR(dwError);
  417. pszPolicyName = AllocPolStr(pszGuidName);
  418. if (!pszPolicyName) {
  419. dwError = ERROR_OUTOFMEMORY;
  420. BAIL_ON_WIN32_ERROR(dwError);
  421. }
  422. *ppszPolicyName = pszPolicyName;
  423. return(dwError);
  424. error:
  425. *ppszPolicyName = NULL;
  426. return(dwError);
  427. }