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.

428 lines
13 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // SYSTEM: Windows Update Critical Fix Notification
  4. //
  5. // CLASS: N/A
  6. // MODULE: Connection Detection
  7. // FILE: cfreg.CPP
  8. //
  9. /////////////////////////////////////////////////////////////////////
  10. //
  11. // DESC: This class implements all functions needed to access
  12. // machine registry to get information related to
  13. // Windows Update Critical Fix Notification feature.
  14. //
  15. // AUTHOR: Charles Ma, Windows Update Team
  16. // DATE: 7/6/1998
  17. //
  18. /////////////////////////////////////////////////////////////////////
  19. //
  20. // Revision History:
  21. //
  22. // Date Author Description
  23. // ~~~~ ~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. // 7/6/98 Charles Ma Created
  25. //
  26. /////////////////////////////////////////////////////////////////////
  27. //
  28. // Copyrights: �1998 Microsoft � Corporation
  29. //
  30. // All rights reserved.
  31. //
  32. // No portion of this source code may be reproduced
  33. // without express written permission of Microsoft Corporation.
  34. //
  35. // This source code is proprietary and confidential.
  36. /////////////////////////////////////////////////////////////////////
  37. //
  38. // CriticalFixReg.cpp: implementation of the functions used to
  39. // handle registry related operations
  40. //
  41. //////////////////////////////////////////////////////////////////////
  42. #include "pch.h"
  43. #pragma hdrstop
  44. const TCHAR AUREGKEY_HKLM_DOMAIN_POLICY[] = _T("Software\\Policies\\Microsoft\\Windows\\WindowsUpdate\\AU");
  45. const TCHAR AUREGKEY_HKLM_WINDOWSUPDATE_POLICY[] = _T("Software\\Policies\\Microsoft\\Windows\\WindowsUpdate");
  46. const TCHAR AUREGKEY_HKLM_IUCONTROL_POLICY[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\IUControl");
  47. const TCHAR AUREGKEY_HKLM_SYSTEM_WAS_RESTORED[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\SystemWasRestored");
  48. const TCHAR AUREGKEY_HKCU_USER_POLICY[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\WindowsUpdate");
  49. const TCHAR AUREGKEY_HKLM_ADMIN_POLICY[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update");
  50. const TCHAR AUREGVALUE_DISABLE_WINDOWS_UPDATE_ACCESS[] = _T("DisableWindowsUpdateAccess");
  51. const TCHAR AUREGKEY_REBOOT_REQUIRED[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\RebootRequired");
  52. ////////////////////////////////////////////////////////////////////////////
  53. //
  54. // Public Function GetRegStringValue()
  55. // Read the registry value for a REG_SZ key
  56. // Input: Name of value
  57. // Output: Buffer containing the registry value if successful
  58. // Return: HRESULT flag indicating the success of this function
  59. //
  60. ////////////////////////////////////////////////////////////////////////////
  61. HRESULT GetRegStringValue(LPCTSTR lpszValueName, LPTSTR lpszBuffer, int nCharCount, LPCTSTR lpszSubKeyName)
  62. {
  63. HKEY hKey;
  64. HRESULT hr = E_FAIL;
  65. if (lpszValueName == NULL || lpszBuffer == NULL)
  66. {
  67. return E_INVALIDARG;
  68. }
  69. if (ERROR_SUCCESS == RegOpenKeyEx(
  70. HKEY_LOCAL_MACHINE,
  71. lpszSubKeyName,
  72. 0,
  73. KEY_READ,
  74. &hKey) )
  75. {
  76. hr = SafeRegQueryStringValueCch(
  77. hKey,
  78. lpszValueName,
  79. lpszBuffer,
  80. nCharCount,
  81. NULL,
  82. NULL);
  83. RegCloseKey(hKey);
  84. }
  85. return hr;
  86. }
  87. ////////////////////////////////////////////////////////////////////////////
  88. //
  89. // Public Function SetRegStringValue()
  90. // Set the registry value of timestamp as current system local time
  91. // Input: name of the value to set. pointer to the time structure to set time. if null,
  92. // we use current system time.
  93. // Output: None
  94. // Return: HRESULT flag indicating the success of this function
  95. //
  96. ////////////////////////////////////////////////////////////////////////////
  97. HRESULT SetRegStringValue(LPCTSTR lpszValueName, LPCTSTR lpszNewValue, LPCTSTR lpszSubKeyName)
  98. {
  99. HKEY hKey;
  100. HRESULT hRet = E_FAIL;
  101. DWORD dwResult;
  102. if (lpszValueName == NULL || lpszNewValue == NULL)
  103. {
  104. return E_INVALIDARG;
  105. }
  106. //
  107. // open the key
  108. //
  109. if (RegCreateKeyEx(
  110. HKEY_LOCAL_MACHINE, // root key
  111. lpszSubKeyName, // subkey
  112. 0, // reserved
  113. NULL, // class name
  114. REG_OPTION_NON_VOLATILE, // option
  115. KEY_SET_VALUE, // security
  116. NULL, // security attribute
  117. &hKey,
  118. &dwResult) == ERROR_SUCCESS)
  119. {
  120. //
  121. // set the time to the lasttimestamp value
  122. //
  123. hRet = (RegSetValueEx(
  124. hKey,
  125. lpszValueName,
  126. 0,
  127. REG_SZ,
  128. (const unsigned char *)lpszNewValue,
  129. (lstrlen(lpszNewValue) + 1) * sizeof(*lpszNewValue)
  130. ) == ERROR_SUCCESS) ? S_OK : E_FAIL;
  131. RegCloseKey(hKey);
  132. }
  133. return hRet;
  134. }
  135. ////////////////////////////////////////////////////////////////////////////
  136. //
  137. // Public Function DeleteRegValue()
  138. // Delete the registry value entry
  139. // Input: name of the value to entry,
  140. // Output: None
  141. // Return: HRESULT flag indicating the success of this function
  142. //
  143. ////////////////////////////////////////////////////////////////////////////
  144. HRESULT DeleteRegValue(LPCTSTR lpszValueName)
  145. {
  146. HKEY hKey;
  147. HRESULT hRet = E_FAIL;
  148. if (lpszValueName == NULL)
  149. {
  150. return E_INVALIDARG;
  151. }
  152. //
  153. // open the key
  154. //
  155. if (RegOpenKeyEx(
  156. HKEY_LOCAL_MACHINE, // root key
  157. AUREGKEY_HKLM_ADMIN_POLICY, // subkey
  158. 0, // reserved
  159. KEY_WRITE, // security
  160. &hKey) == ERROR_SUCCESS)
  161. {
  162. //
  163. // set the time to the lasttimestamp value
  164. //
  165. hRet = (RegDeleteValue(
  166. hKey,
  167. lpszValueName
  168. ) == ERROR_SUCCESS) ? S_OK : E_FAIL;
  169. RegCloseKey(hKey);
  170. }
  171. else
  172. {
  173. DEBUGMSG("Fail to reg open key with error %d", GetLastError());
  174. }
  175. return hRet;
  176. }
  177. //=======================================================================
  178. // GetRegDWordValue
  179. //=======================================================================
  180. HRESULT GetRegDWordValue(LPCTSTR lpszValueName, LPDWORD pdwValue, LPCTSTR lpszSubKeyName)
  181. {
  182. HKEY hKey;
  183. int iRet;
  184. DWORD dwType = REG_DWORD, dwSize = sizeof(DWORD);
  185. if (lpszValueName == NULL)
  186. {
  187. return E_INVALIDARG;
  188. }
  189. //
  190. // open critical fix key
  191. //
  192. if (RegOpenKeyEx(
  193. HKEY_LOCAL_MACHINE,
  194. lpszSubKeyName,
  195. 0,
  196. KEY_READ,
  197. &hKey) == ERROR_SUCCESS)
  198. {
  199. //
  200. // query the last timestamp value
  201. //
  202. iRet = RegQueryValueEx(
  203. hKey,
  204. lpszValueName,
  205. NULL,
  206. &dwType,
  207. (LPBYTE)pdwValue,
  208. &dwSize);
  209. RegCloseKey(hKey);
  210. if (iRet == ERROR_SUCCESS && dwType == REG_DWORD && dwSize == sizeof(DWORD))
  211. {
  212. return S_OK;
  213. }
  214. }
  215. return E_FAIL;
  216. }
  217. ////////////////////////////////////////////////////////////////////////////
  218. //
  219. // Public Function SetRegDWordValue()
  220. // Set the registry value as a DWORD
  221. // Input: name of the value to set. value to set
  222. // Output: None
  223. // Return: HRESULT flag indicating the success of this function
  224. //
  225. ////////////////////////////////////////////////////////////////////////////
  226. HRESULT SetRegDWordValue(LPCTSTR lpszValueName, DWORD dwValue, DWORD options, LPCTSTR lpszSubKeyName)
  227. {
  228. HKEY hKey;
  229. HRESULT hRet = E_FAIL;
  230. DWORD dwResult;
  231. if (lpszValueName == NULL)
  232. {
  233. return E_INVALIDARG;
  234. }
  235. //
  236. // open the key
  237. //
  238. if (RegCreateKeyEx(
  239. HKEY_LOCAL_MACHINE, // root key
  240. lpszSubKeyName, // subkey
  241. 0, // reserved
  242. NULL, // class name
  243. options, // option
  244. KEY_SET_VALUE, // security
  245. NULL, // security attribute
  246. &hKey,
  247. &dwResult) == ERROR_SUCCESS)
  248. {
  249. //
  250. // set the time to the lasttimestamp value
  251. //
  252. hRet = (RegSetValueEx(
  253. hKey,
  254. lpszValueName,
  255. 0,
  256. REG_DWORD,
  257. (LPBYTE)&dwValue,
  258. sizeof(DWORD)
  259. ) == ERROR_SUCCESS) ? S_OK : E_FAIL;
  260. RegCloseKey(hKey);
  261. }
  262. return hRet;
  263. }
  264. BOOL fRegKeyCreate(LPCTSTR tszKey, DWORD dwOptions)
  265. {
  266. HKEY hKey;
  267. DWORD dwResult;
  268. //
  269. // open the key
  270. //
  271. if ( RegCreateKeyEx(
  272. HKEY_LOCAL_MACHINE, // root key
  273. tszKey, // subkey
  274. 0, // reserved
  275. NULL, // class name
  276. dwOptions, // option
  277. KEY_WRITE, // security
  278. NULL, // security attribute
  279. &hKey,
  280. &dwResult) == ERROR_SUCCESS )
  281. {
  282. RegCloseKey(hKey);
  283. return TRUE;
  284. }
  285. return FALSE;
  286. }
  287. BOOL fRegKeyExists(LPCTSTR tszSubKey, HKEY hRootKey)
  288. {
  289. HKEY hKey;
  290. BOOL fRet = FALSE;
  291. if (RegOpenKeyEx(
  292. // HKEY_LOCAL_MACHINE,
  293. hRootKey,
  294. tszSubKey,
  295. 0,
  296. KEY_READ,
  297. &hKey) == ERROR_SUCCESS)
  298. {
  299. RegCloseKey(hKey);
  300. fRet = TRUE;
  301. }
  302. return fRet;
  303. }
  304. DWORD getTimeOut()
  305. {
  306. DWORD dwValue = 0;
  307. GetRegDWordValue(_T("TimeOut"), &dwValue);
  308. return dwValue;
  309. }
  310. HRESULT setAddedTimeout(DWORD timeout, LPCTSTR strkey)
  311. {
  312. HKEY hAUKey;
  313. SYSTEMTIME tmCurr;
  314. SYSTEMTIME tmTimeOut;
  315. TCHAR szCurr[50];
  316. HRESULT hr = E_FAIL;
  317. GetSystemTime(&tmCurr);
  318. if (FAILED(TimeAddSeconds(tmCurr, timeout, &tmTimeOut)))
  319. {
  320. return E_FAIL;
  321. }
  322. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE ,
  323. AUTOUPDATEKEY,
  324. 0, TEXT(""),
  325. REG_OPTION_NON_VOLATILE,
  326. KEY_SET_VALUE,
  327. NULL,
  328. &hAUKey,
  329. NULL) != ERROR_SUCCESS)
  330. {
  331. return E_FAIL;
  332. }
  333. if (SUCCEEDED(SystemTime2String(tmTimeOut, szCurr, ARRAYSIZE(szCurr))) &&
  334. RegSetValueEx(hAUKey,
  335. strkey,
  336. 0, REG_SZ,
  337. (BYTE *)szCurr,
  338. sizeof(TCHAR)*(lstrlen(szCurr)+1)) == ERROR_SUCCESS)
  339. {
  340. hr = S_OK;
  341. }
  342. RegCloseKey(hAUKey);
  343. return hr;
  344. }
  345. HRESULT getAddedTimeout(DWORD *pdwTimeDiff, LPCTSTR strkey)
  346. {
  347. HKEY hAUKey;
  348. LONG lRet;
  349. TCHAR szTimeBuf[50];
  350. DWORD dwType = REG_SZ, dwSize = sizeof(szTimeBuf);
  351. SYSTEMTIME tmCurr, tmReminder;
  352. *pdwTimeDiff = 0;
  353. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE ,
  354. AUTOUPDATEKEY,
  355. 0,
  356. KEY_READ,
  357. &hAUKey) != ERROR_SUCCESS)
  358. {
  359. return E_FAIL;
  360. }
  361. lRet = RegQueryValueEx(
  362. hAUKey,
  363. strkey,
  364. NULL,
  365. &dwType,
  366. (LPBYTE)szTimeBuf,
  367. &dwSize);
  368. RegCloseKey(hAUKey);
  369. if (lRet != ERROR_SUCCESS || dwType != REG_SZ ||
  370. FAILED(String2SystemTime(szTimeBuf, &tmReminder)))
  371. {
  372. return E_FAIL;
  373. }
  374. GetSystemTime(&tmCurr);
  375. *pdwTimeDiff = max (TimeDiff(tmCurr, tmReminder),0);
  376. return S_OK;
  377. }