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.

404 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C D H C P S . C P P
  7. //
  8. // Contents: Installation support for DHCP Server
  9. //
  10. // Notes:
  11. //
  12. // Author: jeffspr 13 May 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "ncdhcps.h"
  18. #include "ncreg.h"
  19. #include "ncsvc.h"
  20. #include "netoc.h"
  21. #include "ncnetcfg.h"
  22. #include "netcfgp.h"
  23. #include "ncmisc.h"
  24. #include "netcfgn.h"
  25. extern const WCHAR c_szInfId_MS_DHCPServer[];
  26. static const WCHAR c_szDHCPServerParamPath[] = L"System\\CurrentControlSet\\Services\\DHCPServer\\Parameters";
  27. //$ REVIEW (jeffspr) 13 May 1997: These obviously need to be localized.
  28. static const WCHAR c_szDisplayName[] = L"DHCP Server";
  29. static const WCHAR c_szManufacturer[] = L"Microsoft";
  30. static const WCHAR c_szProduct[] = L"DHCPServer";
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Function: HrInstallDHCPServerNotifyObject
  34. //
  35. // Purpose: Handles the installation of DHCP Server on behalf of the DHCP
  36. // Server optional component. Calls into the INetCfg interface
  37. // to do the install.
  38. //
  39. // Arguments:
  40. // (none)
  41. //
  42. // Returns: S_OK if successful, Win32 error otherwise.
  43. //
  44. // Author: jeffspr 13 May 1997
  45. //
  46. // Notes:
  47. //
  48. HRESULT HrInstallDHCPServerNotifyObject(PNETOCDATA pnocd)
  49. {
  50. HRESULT hr = S_OK;
  51. INetCfg * pnc = NULL;
  52. INetCfgComponent* pncc = NULL;
  53. INetCfgComponentSetup* pnccSetup = NULL;
  54. hr = HrOcGetINetCfg(pnocd, TRUE, &pnc);
  55. if (SUCCEEDED(hr))
  56. {
  57. NETWORK_INSTALL_PARAMS nip = {0};
  58. nip.dwSetupFlags = FInSystemSetup() ? NSF_PRIMARYINSTALL :
  59. NSF_POSTSYSINSTALL;
  60. Assert(pnocd);
  61. TraceTag(ttidNetOc, "Installing DHCP Server notify object");
  62. hr = HrInstallComponentOboUser(
  63. pnc,
  64. &nip,
  65. GUID_DEVCLASS_NETSERVICE,
  66. c_szInfId_MS_DHCPServer,
  67. &pncc);
  68. if (SUCCEEDED(hr))
  69. {
  70. TraceTag(ttidNetOc, "QI'ing INetCfgComponentPrivate from DHCP pncc");
  71. // Need to query for the private component interface which
  72. // gives us access to the notify object.
  73. //
  74. INetCfgComponentPrivate* pnccPrivate = NULL;
  75. hr = pncc->QueryInterface(
  76. IID_INetCfgComponentPrivate,
  77. reinterpret_cast<void**>(&pnccPrivate));
  78. if (S_OK == hr)
  79. {
  80. TraceTag(ttidNetOc, "Getting notify object INetCfgComponentSetup from pnccSetup");
  81. // Query the notify object for its setup interface.
  82. // If it doesn't support it, that's okay, we can continue.
  83. //
  84. hr = pnccPrivate->QueryNotifyObject(
  85. IID_INetCfgComponentSetup,
  86. (void**) &pnccSetup);
  87. if (S_OK == hr)
  88. {
  89. TraceTag(ttidNetOc, "Calling pnccSetup->ReadAnswerFile()");
  90. (VOID) pnccSetup->ReadAnswerFile(g_ocmData.sic.SetupData.UnattendFile, pnocd->pszSection);
  91. hr = pnc->Apply();
  92. ReleaseObj(pnccSetup);
  93. }
  94. ReleaseObj(pnccPrivate);
  95. }
  96. ReleaseObj(pncc);
  97. }
  98. (VOID) HrUninitializeAndReleaseINetCfg(TRUE, pnc, TRUE);
  99. }
  100. TraceError("HrInstallDHCPServerNotifyObject", hr);
  101. return hr;
  102. }
  103. //+---------------------------------------------------------------------------
  104. //
  105. // Function: HrRemoveDHCPServerNotifyObject
  106. //
  107. // Purpose: Handles the removal of the DHCP Server on behalf of the DHCP
  108. // Server optional component. Calls into the INetCfg interface
  109. // to do the actual removal.
  110. //
  111. // Arguments:
  112. // (none)
  113. //
  114. // Returns: S_OK if successful, Win32 error otherwise.
  115. //
  116. // Author: jeffspr 13 Jun 1997
  117. //
  118. // Notes:
  119. //
  120. HRESULT HrRemoveDHCPServerNotifyObject(PNETOCDATA pnocd)
  121. {
  122. HRESULT hr = S_OK;
  123. INetCfg * pnc = NULL;
  124. hr = HrOcGetINetCfg(pnocd, TRUE, &pnc);
  125. if (SUCCEEDED(hr))
  126. {
  127. // Ignore the return from this. This is purely to remove the
  128. // spurious user refcount from NT4 to NT5 upgrade. This will
  129. // likely fail when removing a fresh install of DHCP Server.
  130. //
  131. hr = HrRemoveComponentOboUser(
  132. pnc,
  133. GUID_DEVCLASS_NETSERVICE,
  134. c_szInfId_MS_DHCPServer);
  135. if (SUCCEEDED(hr))
  136. {
  137. hr = pnc->Apply();
  138. }
  139. (VOID) HrUninitializeAndReleaseINetCfg(TRUE, pnc, TRUE);
  140. }
  141. TraceError("HrRemoveDHCPServerNotifyObject", hr);
  142. return hr;
  143. }
  144. //+---------------------------------------------------------------------------
  145. //
  146. // Function: HrSetDhcpServiceRecoveryOption
  147. //
  148. // Purpose: Sets the recovery options for the DHCPServer service
  149. //
  150. // Arguments:
  151. // pnocd [in] Pointer to NETOC data.
  152. //
  153. // Returns: S_OK if successful, Win32 error otherwise.
  154. //
  155. // Author: danielwe 26 May 1999
  156. //
  157. // Notes:
  158. //
  159. HRESULT HrSetDhcpServiceRecoveryOption(PNETOCDATA pnocd)
  160. {
  161. CServiceManager sm;
  162. CService service;
  163. HRESULT hr = S_OK;
  164. SC_ACTION sra [4] =
  165. {
  166. { SC_ACTION_RESTART, 15*1000 }, // restart after 15 seconds
  167. { SC_ACTION_RESTART, 15*1000 }, // restart after 15 seconds
  168. { SC_ACTION_RESTART, 15*1000 }, // restart after 15 seconds
  169. { SC_ACTION_NONE, 30*1000 },
  170. };
  171. SERVICE_FAILURE_ACTIONS sfa =
  172. {
  173. 60 * 60, // dwResetPeriod is 1 hr
  174. L"", // no reboot message
  175. L"", // no command to execute
  176. 4, // 3 attempts to restart the server and stop after that
  177. sra
  178. };
  179. hr = sm.HrOpenService(&service, L"DHCPServer");
  180. if (S_OK == hr)
  181. {
  182. hr = service.HrSetServiceRestartRecoveryOption(&sfa);
  183. }
  184. TraceError("HrSetDhcpServiceRecoveryOption", hr);
  185. return hr;
  186. }
  187. //+---------------------------------------------------------------------------
  188. //
  189. // Function: HrInstallDHCPServer
  190. //
  191. // Purpose: Called when DHCP Server is being installed. Handles all of the
  192. // additional installation for DHCPS beyond that of the INF file.
  193. //
  194. // Arguments:
  195. // pnocd [in] Pointer to NETOC data.
  196. //
  197. // Returns: S_OK if successful, Win32 error otherwise.
  198. //
  199. // Author: jeffspr 13 May 1997
  200. //
  201. // Notes:
  202. //
  203. HRESULT HrInstallDHCPServer(PNETOCDATA pnocd)
  204. {
  205. HRESULT hr = S_OK;
  206. CServiceManager sm;
  207. CService srv;
  208. Assert(pnocd);
  209. hr = HrHandleStaticIpDependency(pnocd);
  210. if (SUCCEEDED(hr))
  211. {
  212. hr = HrInstallDHCPServerNotifyObject(pnocd);
  213. if (SUCCEEDED(hr))
  214. {
  215. hr = HrSetDhcpServiceRecoveryOption(pnocd);
  216. }
  217. }
  218. TraceError("HrInstallDHCPServer", hr);
  219. return hr;
  220. }
  221. //+---------------------------------------------------------------------------
  222. //
  223. // Function: HrRemoveDHCPServer
  224. //
  225. // Purpose: Handles additional removal requirements for DHCP Server
  226. // component.
  227. //
  228. // hwnd [in] Parent window for displaying UI.
  229. // poc [in] Pointer to optional component being installed.
  230. //
  231. // Returns: S_OK if successful, Win32 error otherwise.
  232. //
  233. // Author: jeffspr 13 May 1997
  234. //
  235. // Notes:
  236. //
  237. HRESULT HrRemoveDHCPServer(PNETOCDATA pnocd)
  238. {
  239. Assert(pnocd);
  240. // Get the path to the database files from the regsitry.
  241. // Important to do this before removing the component because
  242. // the registry locations get removed when removing the component.
  243. //
  244. tstring strDatabasePath;
  245. tstring strBackupDatabasePath;
  246. HKEY hkeyParams;
  247. HRESULT hr = HrRegOpenKeyEx(HKEY_LOCAL_MACHINE,
  248. c_szDHCPServerParamPath,
  249. KEY_READ,
  250. &hkeyParams);
  251. if (SUCCEEDED(hr))
  252. {
  253. (VOID) HrRegQueryExpandString (
  254. hkeyParams,
  255. L"DatabasePath",
  256. &strDatabasePath);
  257. (VOID) HrRegQueryExpandString (
  258. hkeyParams,
  259. L"BackupDatabasePath",
  260. &strBackupDatabasePath);
  261. RegCloseKey (hkeyParams);
  262. }
  263. // Remove DHCP server.
  264. //
  265. hr = HrRemoveDHCPServerNotifyObject(pnocd);
  266. if (SUCCEEDED(hr) &&
  267. !(strDatabasePath.empty() && strBackupDatabasePath.empty()))
  268. {
  269. (VOID) HrDeleteFileSpecification (
  270. L"*.mdb",
  271. strDatabasePath.c_str());
  272. (VOID) HrDeleteFileSpecification (
  273. L"*.log",
  274. strDatabasePath.c_str());
  275. (VOID) HrDeleteFileSpecification (
  276. L"*.mdb",
  277. strBackupDatabasePath.c_str());
  278. (VOID) HrDeleteFileSpecification (
  279. L"*.log",
  280. strBackupDatabasePath.c_str());
  281. }
  282. TraceError("HrRemoveDHCPServer", hr);
  283. return hr;
  284. }
  285. //+---------------------------------------------------------------------------
  286. //
  287. // Function: HrOcExtDHCPServer
  288. //
  289. // Purpose: NetOC external message handler
  290. //
  291. // Arguments:
  292. // pnocd []
  293. // uMsg []
  294. // wParam []
  295. // lParam []
  296. //
  297. // Returns:
  298. //
  299. // Author: danielwe 17 Sep 1998
  300. //
  301. // Notes:
  302. //
  303. HRESULT HrOcExtDHCPServer(PNETOCDATA pnocd, UINT uMsg,
  304. WPARAM wParam, LPARAM lParam)
  305. {
  306. HRESULT hr = S_OK;
  307. Assert(pnocd);
  308. switch (uMsg)
  309. {
  310. case NETOCM_POST_INSTALL:
  311. hr = HrOcDhcpOnInstall(pnocd);
  312. break;
  313. }
  314. TraceError("HrOcExtDHCPServer", hr);
  315. return hr;
  316. }
  317. //+---------------------------------------------------------------------------
  318. //
  319. // Function: HrOcDhcpOnInstall
  320. //
  321. // Purpose: Called by optional components installer code to handle
  322. // additional installation requirements for DHCP Server
  323. //
  324. // Arguments:
  325. // pnocd [in] Pointer to NETOC data
  326. //
  327. // Returns: S_OK if successful, Win32 error otherwise.
  328. //
  329. // Author: jeffspr 13 May 1997
  330. //
  331. // Notes:
  332. //
  333. HRESULT HrOcDhcpOnInstall(PNETOCDATA pnocd)
  334. {
  335. HRESULT hr = S_OK;
  336. switch(pnocd->eit)
  337. {
  338. // Install DHCP
  339. case IT_INSTALL:
  340. hr = HrInstallDHCPServer(pnocd);
  341. break;
  342. // Remove DHCP
  343. case IT_REMOVE:
  344. hr = HrRemoveDHCPServer(pnocd);
  345. break;
  346. case IT_UPGRADE:
  347. hr = HrSetDhcpServiceRecoveryOption(pnocd);
  348. break;
  349. }
  350. TraceError("HrOcDhcpOnInstall", hr);
  351. return hr;
  352. }