Source code of Windows XP (NT5)
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.

328 lines
9.6 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: CAUState.h
  6. //
  7. // Creator: PeterWi
  8. //
  9. // Purpose: AU state functions.
  10. //
  11. //=======================================================================
  12. #include "safefunc.h"
  13. #include "wuaulib.h"
  14. #include "wuaustate.h"
  15. #include "auwait.h"
  16. #pragma once
  17. extern BOOL gfDownloadStarted; //to be used to distinguish connection detection and actually downloading mode
  18. typedef enum
  19. {
  20. enAU_DomainPolicy,
  21. enAU_AdminPolicy,
  22. enAU_WindowsUpdatePolicy,
  23. enAU_IUControlPolicy
  24. } enumAUPolicyType;
  25. typedef enum
  26. {
  27. AUPOLICYCHANGE_NOOP,
  28. AUPOLICYCHANGE_RESETENGINE,
  29. AUPOLICYCHANGE_RESETCLIENT,
  30. AUPOLICYCHANGE_DISABLE
  31. }enumAUPOLICYCHANGEACTION;
  32. //AU configurable registry settings
  33. #ifdef DBG
  34. extern const TCHAR REG_AUCONNECTWAIT[] ;//= _T("ConnectWait"); //REG_DWORD
  35. extern const TCHAR REG_SELFUPDATE_URL[]; // = _T("SelfUpdateURL");
  36. #endif
  37. extern const TCHAR REG_WUSERVER_URL[]; // = _T("WUServer");
  38. extern const TCHAR REG_WUSTATUSSERVER_URL[]; // = _T("WUStatusServer");
  39. extern const TCHAR REG_IDENT_URL[]; // = _T("IdentServer");
  40. extern const TCHAR WU_LIVE_URL[]; // = _T("http://windowsupdate.microsoft.com/v4");
  41. BOOL fURLChanged(LPCTSTR url1, LPCTSTR url2);
  42. //read only settings
  43. class AUPolicySettings {
  44. public:
  45. AUPolicySettings() :m_enPolicyType(enAU_DomainPolicy), m_dwOption(AUOPTION_UNSPECIFIED),
  46. m_dwSchedInstallDay(0), m_dwSchedInstallTime(0),
  47. m_pszWUServerURL(NULL), m_pszWUStatusServerURL(NULL), m_fRegAUOptionsSpecified(TRUE)
  48. {};
  49. ~AUPolicySettings() {
  50. SafeFree(m_pszWUServerURL);
  51. SafeFree(m_pszWUStatusServerURL);
  52. }
  53. enumAUPolicyType m_enPolicyType;
  54. BOOL m_fRegAUOptionsSpecified;
  55. DWORD m_dwOption;
  56. DWORD m_dwSchedInstallDay;
  57. DWORD m_dwSchedInstallTime;
  58. LPTSTR m_pszWUServerURL;
  59. LPTSTR m_pszWUStatusServerURL;
  60. void Reset(void) {
  61. m_enPolicyType = enAU_DomainPolicy;
  62. m_dwOption = AUOPTION_UNSPECIFIED;
  63. m_dwSchedInstallDay = 0;
  64. m_dwSchedInstallTime = 0;
  65. m_fRegAUOptionsSpecified = TRUE;
  66. SafeFreeNULL(m_pszWUServerURL);
  67. SafeFreeNULL(m_pszWUStatusServerURL);
  68. }
  69. HRESULT m_ReadIn();
  70. HRESULT m_SetOption(AUOPTION & Option);
  71. HRESULT m_SetInstallSchedule(DWORD dwSchedInstallDay, DWORD dwSchedInstallTime);
  72. BOOL operator == (AUPolicySettings & setting2)
  73. {
  74. return m_enPolicyType == setting2.m_enPolicyType
  75. && m_dwOption == setting2.m_dwOption
  76. && m_dwSchedInstallDay == setting2.m_dwSchedInstallDay
  77. && m_dwSchedInstallTime == setting2.m_dwSchedInstallTime
  78. && !fURLChanged(m_pszWUServerURL, setting2.m_pszWUServerURL)
  79. && !fURLChanged(m_pszWUStatusServerURL, setting2.m_pszWUStatusServerURL);
  80. }
  81. HRESULT Copy (AUPolicySettings & setting2)
  82. {
  83. HRESULT hr = E_FAIL;
  84. AUASSERT(this != &setting2);
  85. if (this == &setting2)
  86. {
  87. return E_INVALIDARG;
  88. }
  89. Reset();
  90. m_enPolicyType = setting2.m_enPolicyType;
  91. m_dwOption = setting2.m_dwOption;
  92. m_dwSchedInstallDay = setting2.m_dwSchedInstallDay;
  93. m_dwSchedInstallTime = setting2.m_dwSchedInstallTime;
  94. m_fRegAUOptionsSpecified = setting2.m_fRegAUOptionsSpecified;
  95. if (NULL != setting2.m_pszWUServerURL)
  96. {
  97. size_t cchWUServerURL = lstrlen(setting2.m_pszWUServerURL) + 1;
  98. m_pszWUServerURL = (LPTSTR) malloc(cchWUServerURL * sizeof(TCHAR));
  99. if (NULL == m_pszWUServerURL)
  100. {
  101. hr = E_OUTOFMEMORY;
  102. goto done;
  103. }
  104. if (FAILED(hr = StringCchCopyEx(m_pszWUServerURL, cchWUServerURL, setting2.m_pszWUServerURL, NULL, NULL, MISTSAFE_STRING_FLAGS)))
  105. {
  106. goto done;
  107. }
  108. }
  109. if (NULL != setting2.m_pszWUStatusServerURL)
  110. {
  111. size_t cchWUStatusServerURL = lstrlen(setting2.m_pszWUStatusServerURL) + 1;
  112. m_pszWUStatusServerURL =(LPTSTR) malloc(cchWUStatusServerURL * sizeof(TCHAR));
  113. if (NULL == m_pszWUStatusServerURL)
  114. {
  115. hr = E_OUTOFMEMORY;
  116. goto done;
  117. }
  118. if (FAILED(hr = StringCchCopyEx(m_pszWUStatusServerURL, cchWUStatusServerURL, setting2.m_pszWUStatusServerURL, NULL, NULL, MISTSAFE_STRING_FLAGS)))
  119. {
  120. goto done;
  121. }
  122. }
  123. hr = S_OK;
  124. done:
  125. //in case of failure, keep the most accurate information we could have
  126. if (FAILED(hr))
  127. {
  128. SafeFreeNULL(m_pszWUServerURL);
  129. SafeFreeNULL(m_pszWUStatusServerURL);
  130. }
  131. return hr;
  132. }
  133. #ifdef DBG
  134. void m_DbgDump(void)
  135. {
  136. DEBUGMSG("Policy location: %s", (enAU_DomainPolicy == m_enPolicyType) ? "domain" : "admin");
  137. DEBUGMSG("Option: %d", m_dwOption);
  138. DEBUGMSG("ScheduledInstallDay: %d", m_dwSchedInstallDay);
  139. DEBUGMSG("ScheduledInstallTime: %d", m_dwSchedInstallTime);
  140. DEBUGMSG("WUServerURL string is: %S", m_pszWUServerURL);
  141. DEBUGMSG("WUStatusServerURL string is: %S", m_pszWUStatusServerURL);
  142. }
  143. #endif
  144. private:
  145. HRESULT m_ReadOptionPolicy(void);
  146. HRESULT m_ReadScheduledInstallPolicy(void);
  147. HRESULT m_ReadWUServerURL(void);
  148. };
  149. class CAUState
  150. {
  151. public:
  152. static HRESULT HrCreateState(void);
  153. CAUState();
  154. ~CAUState()
  155. {
  156. SafeCloseHandleNULL(m_hMutex);
  157. SafeFree(m_pszTestIdentServerURL);
  158. #ifdef DBG
  159. SafeFree(m_pszTestSelfUpdateURL);
  160. #endif
  161. }
  162. // Option methods
  163. AUOPTION GetOption(void) ;
  164. HRESULT SetOption(AUOPTION & Option);
  165. HRESULT SetInstallSchedule(DWORD dwSchedInstallDay, DWORD dwSchedInstallTime);
  166. void GetInstallSchedule(DWORD *pdwSchedInstallDay, DWORD *pdwSchedInstallTime);
  167. BOOL fOptionAutoDownload(void)
  168. {
  169. return ((AUOPTION_INSTALLONLY_NOTIFY == m_PolicySettings.m_dwOption) ||
  170. (AUOPTION_SCHEDULED == m_PolicySettings.m_dwOption));
  171. }
  172. BOOL fOptionSchedInstall(void)
  173. {
  174. return AUOPTION_SCHEDULED == m_PolicySettings.m_dwOption;
  175. }
  176. BOOL fOptionEnabled(void)
  177. {
  178. return AUOPTION_AUTOUPDATE_DISABLE != m_PolicySettings.m_dwOption;
  179. }
  180. BOOL fShouldScheduledInstall(void)
  181. { //in which case launch AU client via local system
  182. return (AUOPTION_SCHEDULED == m_PolicySettings.m_dwOption)
  183. && (AUSTATE_DOWNLOAD_COMPLETE == m_dwState);
  184. }
  185. BOOL fValidationNeededState(void)
  186. {
  187. return AUSTATE_DETECT_COMPLETE == m_dwState || AUSTATE_DOWNLOAD_COMPLETE == m_dwState;
  188. }
  189. // State methods
  190. DWORD GetState(void) { return m_dwState; }
  191. void SetState(DWORD dwState);
  192. DWORD GetCltAction(void) { return m_dwCltAction;}
  193. void SetCltAction(DWORD dwCltAction) { m_dwCltAction = dwCltAction;}
  194. // Helper functions
  195. HRESULT HrInit(BOOL fInit = FALSE);
  196. HRESULT Refresh(enumAUPOLICYCHANGEACTION *pactcode);
  197. BOOL fWasSystemRestored(void);
  198. BOOL fDisconnected(void) { return m_fDisconnected; }
  199. void SetDisconnected(BOOL fDisconnected);
  200. static HRESULT GetRegDWordValue(LPCTSTR lpszValueName, LPDWORD pdwValue, enumAUPolicyType enPolicyType = enAU_AdminPolicy);
  201. static HRESULT SetRegDWordValue(LPCTSTR lpszValueName, DWORD dwValue, enumAUPolicyType enPolicyType = enAU_AdminPolicy, DWORD options = REG_OPTION_NON_VOLATILE);
  202. static HRESULT GetRegStringValue(LPCTSTR lpszValueName, LPTSTR lpszBuffer, int nCharCount, enumAUPolicyType enPolicyType);
  203. static HRESULT SetRegStringValue(LPCTSTR lpszValueName, LPCTSTR lpszNewValue, enumAUPolicyType enPolicyType);
  204. HRESULT CalculateScheduledInstallSleepTime(DWORD *pdwSleepTime );
  205. void GetSchedInstallDate(AUFILETIME & auftSchedInstallDate) { auftSchedInstallDate = m_auftSchedInstallDate; }
  206. void SetDetectionStartTime(BOOL fOverwrite);
  207. BOOL IsUnableToConnect(void);
  208. void RemoveDetectionStartTime(void);
  209. LPCTSTR GetIdentServerURL(void)
  210. {
  211. LPCTSTR pszRet = WU_LIVE_URL;
  212. if ( NULL != m_pszTestIdentServerURL )
  213. {
  214. pszRet = m_pszTestIdentServerURL;
  215. }
  216. else if ( NULL != m_PolicySettings.m_pszWUServerURL )
  217. {
  218. pszRet = m_PolicySettings.m_pszWUServerURL;
  219. }
  220. return pszRet;
  221. }
  222. LPTSTR GetSelfUpdateServerURLOverride(void)
  223. {
  224. LPTSTR pszRet = NULL;
  225. #ifdef DBG
  226. if ( NULL != m_pszTestSelfUpdateURL )
  227. {
  228. return m_pszTestSelfUpdateURL;
  229. }
  230. #endif
  231. if ( NULL != m_PolicySettings.m_pszWUServerURL )
  232. {
  233. pszRet = m_PolicySettings.m_pszWUServerURL;
  234. }
  235. return pszRet;
  236. }
  237. BOOL fInCorpWU(void)
  238. {
  239. return (NULL != m_PolicySettings.m_pszWUStatusServerURL);
  240. }
  241. BOOL fWin2K(void) { return m_fWin2K; }
  242. BOOL fShouldAutoDownload(BOOL fUserAvailable)
  243. {
  244. return !fUserAvailable && (AUSTATE_DETECT_COMPLETE == m_dwState) && fOptionAutoDownload();
  245. }
  246. public:
  247. HANDLE m_hMutex; //protect against simultaneous writing
  248. private:
  249. HRESULT m_ReadPolicy(BOOL fInit);
  250. void m_ReadRegistrySettings(BOOL fInit);
  251. HRESULT m_ReadTestOverrides(void);
  252. HRESULT m_SetScheduledInstallDate(void);
  253. HRESULT m_CalculateScheduledInstallDate(AUFILETIME & auftSchedInstallDate, DWORD *pdwSleepTime);
  254. void m_Reset(void);
  255. BOOL m_lock(void)
  256. {
  257. AUASSERT(NULL != m_hMutex);
  258. if (WAIT_FAILED == WaitForSingleObject(m_hMutex, INFINITE))
  259. {
  260. AUASSERT(FALSE); //should never be here
  261. return FALSE;
  262. }
  263. return TRUE;
  264. }
  265. void m_unlock(void)
  266. {
  267. ReleaseMutex(m_hMutex);
  268. }
  269. #ifdef DBG
  270. void m_DbgDumpState(void);
  271. #endif
  272. AUPolicySettings m_PolicySettings;
  273. DWORD m_dwState;
  274. AUFILETIME m_auftSchedInstallDate;
  275. AUFILETIME m_auftDetectionStartTime;
  276. DWORD m_dwCltAction;
  277. BOOL m_fDisconnected;
  278. BOOL m_fWin2K;
  279. LPTSTR m_pszTestIdentServerURL;
  280. #ifdef DBG
  281. LPTSTR m_pszTestSelfUpdateURL;
  282. #endif
  283. };
  284. // global state object pointer
  285. extern CAUState *gpState;