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.

304 lines
8.1 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation
  4. //
  5. // sanorun.cpp
  6. //
  7. // Description:
  8. //
  9. // If Setup is running unattended, look in the answer file to see if
  10. // ServerWelcome is in the GuiUnattended section.
  11. // Delete the appropriate Reg value if "ServerWelcome = No".
  12. //
  13. // On the Blade SKU, this will delete SaInstall.exe from the Run key
  14. // so that the SAK will not be installed by default.
  15. //
  16. // Header File:
  17. // sanorun.h
  18. //
  19. // History:
  20. // travisn 18-JAN-2002 Created
  21. //////////////////////////////////////////////////////////////////////////////
  22. #include "stdafx.h"
  23. #include "sanorun.h"
  24. #include "setupapi.h"
  25. #include "ocmanage.h"
  26. //
  27. // Variable that stores between setup phases whether or not
  28. // ServerWelcome = No. This is set during OC_INIT_COMPONENT and evaluated
  29. // during OC_COMPLETE_INSTALLATION.
  30. //
  31. BOOL g_bServerWelcomeIsOff = FALSE;
  32. //
  33. // The path to the Run key
  34. //
  35. LPCWSTR RUN_KEY = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  36. //
  37. // The value where sainstall.exe is found in the Run key
  38. //
  39. LPCWSTR SAINSTALL_VALUE = L"SAInstall";
  40. //
  41. // Section in the answer file to find ServerWelcome
  42. //
  43. LPCWSTR GUI_UNATTEND = L"GuiUnattended";
  44. //
  45. // Key that defines ServerWelcome
  46. //
  47. LPCWSTR SERVER_WELCOME = L"ServerWelcome";
  48. //////////////////////////////////////////////////////////////////////////////
  49. //++
  50. //
  51. // DllMain
  52. //
  53. // Description:
  54. // Entry point to load the DLL
  55. //--
  56. //////////////////////////////////////////////////////////////////////////////
  57. BOOL APIENTRY DllMain( HANDLE hModule,
  58. DWORD ul_reason_for_call,
  59. LPVOID lpReserved
  60. )
  61. {
  62. switch (ul_reason_for_call)
  63. {
  64. case DLL_PROCESS_ATTACH:
  65. case DLL_THREAD_ATTACH:
  66. case DLL_THREAD_DETACH:
  67. case DLL_PROCESS_DETACH:
  68. break;
  69. }
  70. return TRUE;
  71. }
  72. //////////////////////////////////////////////////////////////////////////////
  73. //++
  74. //
  75. // DeleteRegValue
  76. //
  77. // Description:
  78. // Deletes the given value wsValue from the given key wsKey in HKLM.
  79. //
  80. // Returns:
  81. // HRESULT indicating if the value was successfully deleted. If it did
  82. // not exist, E_FAIL is returned.
  83. //
  84. //--
  85. //////////////////////////////////////////////////////////////////////////////
  86. HRESULT DeleteRegValue(LPCWSTR wsKey, LPCWSTR wsValue)
  87. {
  88. //Open the Run key
  89. HKEY hOpenKey;
  90. if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wsKey, 0, KEY_WRITE, &hOpenKey) != ERROR_SUCCESS)
  91. {
  92. //Could not open the Run key
  93. return E_FAIL;
  94. }
  95. //Delete the value
  96. LRESULT lRes;
  97. lRes = RegDeleteValueW(hOpenKey, wsValue);
  98. RegCloseKey(hOpenKey);
  99. if (lRes == ERROR_SUCCESS)
  100. {
  101. //Deleted the SAInstall value from the Run key
  102. return S_OK;
  103. }
  104. else
  105. {
  106. //SAInstall value not found--Could not delete from Run key
  107. return E_FAIL;
  108. }
  109. }
  110. //////////////////////////////////////////////////////////////////////////////
  111. //++
  112. //
  113. // ServerWelcomeIsOff
  114. //
  115. // Description:
  116. // Attempts to open the answer file and find if "ServerWelcome = No"
  117. // is present.
  118. //
  119. // Parameter:
  120. // pInitComponent [in] Pointer to information about the setup
  121. //
  122. // Returns:
  123. // If "ServerWelcome = No" is found, returns TRUE
  124. // Otherwise, returns FALSE (ie. the answer file cannot be opened,
  125. // "ServerWelcome = Anything Else", or ServerWelcome is not found).
  126. //
  127. //--
  128. //////////////////////////////////////////////////////////////////////////////
  129. BOOL ServerWelcomeIsOff(PSETUP_INIT_COMPONENT pInitComponent)
  130. {
  131. //By default, the ServerWelcome is on
  132. BOOL bWelcomeOff = FALSE;
  133. do
  134. {
  135. //
  136. // Get a handle to the answer file
  137. //
  138. HINF hUnattendFile = pInitComponent->HelperRoutines.GetInfHandle(
  139. INFINDEX_UNATTENDED,
  140. pInitComponent->HelperRoutines.OcManagerContext
  141. );
  142. if (hUnattendFile == INVALID_HANDLE_VALUE || hUnattendFile == NULL)
  143. {
  144. break;
  145. }
  146. //
  147. // Retrieve the ServerWelcome key from the answer file
  148. //
  149. INFCONTEXT Context;
  150. if (!SetupFindFirstLine(hUnattendFile, GUI_UNATTEND, SERVER_WELCOME, &Context))
  151. break;
  152. //
  153. // Retrieve the value for the ServerWelcome key
  154. //
  155. WCHAR wsValue[MAX_PATH];
  156. if (!SetupGetStringField(&Context, 1, wsValue, MAX_PATH, NULL))
  157. break;
  158. //
  159. // Check to see if ServerWelcome = No
  160. //
  161. if (_wcsicmp(wsValue, L"No") != 0)
  162. break;
  163. //
  164. // ServerWelcome = No, so return true
  165. //
  166. bWelcomeOff = TRUE;
  167. } while (FALSE);
  168. return bWelcomeOff;
  169. }
  170. //////////////////////////////////////////////////////////////////////////////
  171. //++
  172. //
  173. // OcEntry
  174. //
  175. // Description:
  176. // Entry point that Setup calls to allow this component to initialize
  177. // itself. The only stage where this component takes action is
  178. // during OC_INIT_COMPONENT. If the setup is unattended, remove
  179. // SaInstall.exe from the Run key if "ServerWelcome = No" is found
  180. // in the answer file.
  181. //
  182. // Returns:
  183. // DWORD depending on the stage. Usually 0 indicates success
  184. //
  185. //--
  186. //////////////////////////////////////////////////////////////////////////////
  187. SANORUN_API DWORD OcEntry(
  188. IN LPCVOID ComponentId,
  189. IN LPCVOID SubcomponentId,
  190. IN UINT Function,
  191. IN UINT Param1,
  192. IN OUT PVOID Param2
  193. )
  194. {
  195. DWORD rValue = 0;//Default return signaling success
  196. try
  197. {
  198. //Declare variables used in the switch statement
  199. PSETUP_INIT_COMPONENT pInitComponent = NULL;
  200. switch (Function)
  201. {
  202. case OC_PREINITIALIZE:
  203. rValue = OCFLAG_UNICODE;
  204. break;
  205. case OC_INIT_COMPONENT:
  206. //
  207. // OC_INIT_COMPONENT is where we detect if ServerWelcome = No,
  208. // which will be used in a later phase of setup.
  209. // Param2 contains all the information we need from setup
  210. //
  211. pInitComponent = (PSETUP_INIT_COMPONENT)Param2;
  212. if (pInitComponent == NULL)
  213. {
  214. break;
  215. }
  216. //
  217. // Check if the OperationFlags include SETUPOP_BATCH, which means that
  218. // the unattendedFile is valid
  219. //
  220. if (((pInitComponent -> SetupData.OperationFlags) & SETUPOP_BATCH) == 0)
  221. {
  222. break;
  223. }
  224. //
  225. // If Setup is running unattended, look in the answer file to see if
  226. // ServerWelcome is in the GuiUnattended section.
  227. //
  228. if (ServerWelcomeIsOff(pInitComponent))
  229. {
  230. g_bServerWelcomeIsOff = TRUE;
  231. }
  232. break;
  233. case OC_SET_LANGUAGE:
  234. rValue = TRUE;//Supports all languages
  235. break;
  236. case OC_CALC_DISK_SPACE:
  237. case OC_QUEUE_FILE_OPS:
  238. case OC_ABOUT_TO_COMMIT_QUEUE:
  239. rValue = NO_ERROR;
  240. break;
  241. case OC_COMPLETE_INSTALLATION:
  242. //
  243. // Perform the work corresponding to ServerWelcome = No.
  244. // Remove SaInstall.exe from the Run key on the Blade SKU.
  245. //
  246. if (g_bServerWelcomeIsOff)
  247. {
  248. HRESULT hr = DeleteRegValue(RUN_KEY, SAINSTALL_VALUE);
  249. }
  250. rValue = NO_ERROR;
  251. break;
  252. default:
  253. //case OC_QUERY_IMAGE:
  254. //case OC_REQUEST_PAGES:
  255. //case OC_QUERY_SKIP_PAGE:
  256. //case OC_QUERY_CHANGE_SEL_STATE:
  257. //case OC_QUERY_STEP_COUNT:
  258. //case OC_CLEANUP:
  259. //case OC_NEED_MEDIA:
  260. break;
  261. }
  262. }
  263. catch (...)
  264. {
  265. //Unexpected Exception
  266. }
  267. return rValue;
  268. }