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.

551 lines
16 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: I P S Y S P R P . C P P
  7. //
  8. // Contents: Handle the TCP/IP parameters in the sysprep
  9. //
  10. // Notes:
  11. //
  12. // Author: nsun
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "tcpipobj.h"
  18. #include "ncsetup.h"
  19. #include "tcpconst.h"
  20. #include "tcputil.h"
  21. #include "ncreg.h"
  22. #include "afilestr.h"
  23. typedef struct {
  24. LPCWSTR pszRegValName;
  25. LPCWSTR pszFileValName;
  26. DWORD dwType;
  27. } SysprepValueNameTypeMapping;
  28. SysprepValueNameTypeMapping c_TcpipValueTypeMapping [] =
  29. {
  30. {c_szDefaultGateway, c_szAfDefaultGateway, REG_MULTI_SZ},
  31. {RGAS_DEFAULTGATEWAYMETRIC, RGAS_DEFAULTGATEWAYMETRIC, REG_MULTI_SZ},
  32. {c_szDomain, c_szAfDnsDomain, REG_SZ},
  33. {RGAS_ENABLE_DHCP, c_szAfDhcp, REG_BOOL},
  34. {c_szInterfaceMetric, c_szInterfaceMetric, REG_DWORD},
  35. {RGAS_IPADDRESS, c_szAfIpaddress, REG_MULTI_SZ},
  36. {RGAS_NAMESERVER, c_szAfDnsServerSearchOrder, REG_SZ},
  37. {RGAS_FILTERING_IP, c_szAfIpAllowedProtocols, REG_MULTI_SZ},
  38. {RGAS_SUBNETMASK, c_szAfSubnetmask, REG_MULTI_SZ},
  39. {RGAS_FILTERING_TCP, c_szAfTcpAllowedPorts, REG_MULTI_SZ},
  40. {RGAS_FILTERING_UDP, c_szAfUdpAllowedPorts, REG_MULTI_SZ}
  41. };
  42. SysprepValueNameTypeMapping c_NetBTValueTypeMapping [] =
  43. {
  44. {RGAS_NETBT_NAMESERVERLIST, c_szAfWinsServerList, REG_MULTI_SZ},
  45. {RGAS_NETBT_NETBIOSOPTIONS, c_szAfNetBIOSOptions, REG_DWORD}
  46. };
  47. HRESULT HrSysPrepSaveInterfaceParams(
  48. INetCfgSysPrep* pncsp,
  49. LPCWSTR pszSection,
  50. HKEY hkey,
  51. const SysprepValueNameTypeMapping * prgVtpParams,
  52. UINT cParams
  53. );
  54. HRESULT HrSysPrepLoadInterfaceParams(
  55. HINF hinf,
  56. PCWSTR pszSection,
  57. HKEY hkeyParam,
  58. const SysprepValueNameTypeMapping * prgVtpParams,
  59. UINT cParams
  60. );
  61. HRESULT CTcpipcfg::HrOpenTcpipInterfaceKey(
  62. const GUID & guidInterface,
  63. HKEY * phKey,
  64. REGSAM sam
  65. )
  66. {
  67. if (NULL == phKey)
  68. return E_INVALIDARG;
  69. HRESULT hr = S_OK;
  70. *phKey = NULL;
  71. if (NULL == m_pnccTcpip)
  72. return E_FAIL;
  73. HKEY hkeyTcpipParam = NULL;
  74. HKEY hkeyInterface = NULL;
  75. tstring strInterfaceRegPath;
  76. WCHAR szGuid [c_cchGuidWithTerm];
  77. StringFromGUID2(
  78. guidInterface,
  79. szGuid,
  80. c_cchGuidWithTerm
  81. );
  82. CORg(m_pnccTcpip->OpenParamKey(&hkeyTcpipParam));
  83. Assert(hkeyTcpipParam);
  84. strInterfaceRegPath = c_szInterfacesRegKey;
  85. strInterfaceRegPath += L"\\";
  86. strInterfaceRegPath += szGuid;
  87. CORg(HrRegOpenKeyEx(
  88. hkeyTcpipParam,
  89. strInterfaceRegPath.c_str(),
  90. sam,
  91. &hkeyInterface
  92. ));
  93. Error:
  94. if (SUCCEEDED(hr))
  95. {
  96. *phKey = hkeyInterface;
  97. }
  98. RegSafeCloseKey(hkeyTcpipParam);
  99. return hr;
  100. }
  101. HRESULT CTcpipcfg::HrOpenNetBtInterfaceKey(
  102. const GUID & guidInterface,
  103. HKEY * phKey,
  104. REGSAM sam
  105. )
  106. {
  107. if (NULL == phKey)
  108. return E_INVALIDARG;
  109. HRESULT hr = S_OK;
  110. *phKey = NULL;
  111. if (NULL == m_pnccWins)
  112. return E_FAIL;
  113. HKEY hkeyNetBtParam = NULL;
  114. HKEY hkeyInterface = NULL;
  115. tstring strInterfaceRegPath;
  116. WCHAR szGuid [c_cchGuidWithTerm];
  117. StringFromGUID2(
  118. guidInterface,
  119. szGuid,
  120. c_cchGuidWithTerm
  121. );
  122. CORg(m_pnccWins->OpenParamKey(&hkeyNetBtParam));
  123. Assert(hkeyNetBtParam);
  124. strInterfaceRegPath = c_szInterfacesRegKey;
  125. strInterfaceRegPath += L"\\";
  126. strInterfaceRegPath += c_szTcpip_;
  127. strInterfaceRegPath += szGuid;
  128. CORg(HrRegOpenKeyEx(
  129. hkeyNetBtParam,
  130. strInterfaceRegPath.c_str(),
  131. sam,
  132. &hkeyInterface
  133. ));
  134. Error:
  135. if (SUCCEEDED(hr))
  136. {
  137. *phKey = hkeyInterface;
  138. }
  139. RegSafeCloseKey(hkeyNetBtParam);
  140. return hr;
  141. }
  142. STDMETHODIMP CTcpipcfg::SaveAdapterParameters(
  143. INetCfgSysPrep* pncsp,
  144. LPCWSTR pszwAnswerSections,
  145. GUID* pAdapterInstanceGuid
  146. )
  147. {
  148. if (NULL == pncsp || NULL == pAdapterInstanceGuid)
  149. return E_INVALIDARG;
  150. Assert(m_pnccTcpip);
  151. Assert(m_pnccWins);
  152. if (NULL == m_pnccTcpip || NULL == m_pnccWins)
  153. return E_FAIL;
  154. HRESULT hr = S_OK;
  155. HRESULT hrTmp = S_OK;
  156. HKEY hkeyTcpipParam = NULL;
  157. HKEY hkeyNetBtParam = NULL;
  158. //Write the TCP/IP settings
  159. hr = HrOpenTcpipInterfaceKey(*pAdapterInstanceGuid, &hkeyTcpipParam, KEY_READ);
  160. if (SUCCEEDED(hr))
  161. {
  162. Assert(hkeyTcpipParam);
  163. hr = HrSysPrepSaveInterfaceParams(
  164. pncsp,
  165. pszwAnswerSections,
  166. hkeyTcpipParam,
  167. c_TcpipValueTypeMapping,
  168. celems(c_TcpipValueTypeMapping)
  169. );
  170. RegSafeCloseKey(hkeyTcpipParam);
  171. }
  172. //Write the DNS update settings
  173. WCHAR szGuid [c_cchGuidWithTerm] = {0};
  174. BOOL fTemp = FALSE;
  175. StringFromGUID2(
  176. *pAdapterInstanceGuid,
  177. szGuid,
  178. c_cchGuidWithTerm
  179. );
  180. fTemp = !DnsIsDynamicRegistrationEnabled(szGuid);
  181. hrTmp = pncsp->HrSetupSetFirstStringAsBool(
  182. pszwAnswerSections,
  183. c_szAfDisableDynamicUpdate,
  184. fTemp
  185. );
  186. if (SUCCEEDED(hr))
  187. hr = hrTmp;
  188. fTemp = DnsIsAdapterDomainNameRegistrationEnabled(szGuid);
  189. hrTmp = pncsp->HrSetupSetFirstStringAsBool(
  190. pszwAnswerSections,
  191. c_szAfEnableAdapterDomainNameRegistration,
  192. fTemp
  193. );
  194. if (SUCCEEDED(hr))
  195. hr = hrTmp;
  196. //Write the NetBT settings
  197. hrTmp = HrOpenNetBtInterfaceKey(*pAdapterInstanceGuid, &hkeyNetBtParam, KEY_READ);
  198. if (SUCCEEDED(hrTmp))
  199. {
  200. hrTmp = HrSysPrepSaveInterfaceParams(
  201. pncsp,
  202. pszwAnswerSections,
  203. hkeyNetBtParam,
  204. c_NetBTValueTypeMapping,
  205. celems(c_NetBTValueTypeMapping)
  206. );
  207. RegSafeCloseKey(hkeyNetBtParam);
  208. }
  209. if (SUCCEEDED(hr))
  210. hr = hrTmp;
  211. return hr;
  212. }
  213. STDMETHODIMP CTcpipcfg::RestoreAdapterParameters(
  214. LPCWSTR pszwAnswerFile,
  215. LPCWSTR pszwAnswerSection,
  216. GUID* pAdapterInstanceGuid
  217. )
  218. {
  219. AssertSz(pszwAnswerFile, "Answer file string is NULL!");
  220. AssertSz(pszwAnswerSection, "Answer file sections string is NULL!");
  221. if (NULL == pszwAnswerFile ||
  222. NULL == pszwAnswerSection ||
  223. NULL == pAdapterInstanceGuid)
  224. {
  225. return E_INVALIDARG;
  226. }
  227. HRESULT hr = S_OK;
  228. CSetupInfFile caf; // Class to process answer file
  229. // Open the answer file.
  230. hr = caf.HrOpen(pszwAnswerFile, NULL, INF_STYLE_OLDNT | INF_STYLE_WIN4, NULL);
  231. if (FAILED(hr))
  232. return hr;
  233. HKEY hkeyTcpipParam = NULL;
  234. HKEY hkeyNetBtParam = NULL;
  235. hr = HrOpenTcpipInterfaceKey(*pAdapterInstanceGuid, &hkeyTcpipParam, KEY_READ_WRITE);
  236. if (SUCCEEDED(hr))
  237. {
  238. Assert(hkeyTcpipParam);
  239. hr = HrSysPrepLoadInterfaceParams(
  240. caf.Hinf(),
  241. pszwAnswerSection,
  242. hkeyTcpipParam,
  243. c_TcpipValueTypeMapping,
  244. celems(c_TcpipValueTypeMapping)
  245. );
  246. }
  247. HRESULT hrTmp = S_OK;
  248. hrTmp = HrOpenNetBtInterfaceKey(*pAdapterInstanceGuid, &hkeyNetBtParam, KEY_READ_WRITE);
  249. if (SUCCEEDED(hrTmp))
  250. {
  251. Assert(hkeyNetBtParam);
  252. hrTmp = HrSysPrepLoadInterfaceParams(
  253. caf.Hinf(),
  254. pszwAnswerSection,
  255. hkeyNetBtParam,
  256. c_NetBTValueTypeMapping,
  257. celems(c_NetBTValueTypeMapping)
  258. );
  259. }
  260. if (SUCCEEDED(hr))
  261. hr = hrTmp;
  262. caf.Close();
  263. return hr;
  264. }
  265. HRESULT HrSysPrepSaveInterfaceParams(
  266. INetCfgSysPrep* pncsp,
  267. LPCWSTR pszSection,
  268. HKEY hkey,
  269. const SysprepValueNameTypeMapping * prgVtpParams,
  270. UINT cParams
  271. )
  272. {
  273. Assert(pncsp);
  274. Assert(pszSection);
  275. Assert(hkey);
  276. if (NULL == pncsp || NULL == pszSection || NULL == hkey || NULL == prgVtpParams)
  277. {
  278. return E_INVALIDARG;
  279. }
  280. HRESULT hr = S_OK;
  281. HRESULT hrTmp = S_OK;
  282. BOOL fTmp;
  283. DWORD dwTmp;
  284. tstring strTmp;
  285. WCHAR * mszTmp;
  286. UINT i = 0;
  287. for (i = 0; i < cParams; i++)
  288. {
  289. hrTmp = S_OK;
  290. switch(prgVtpParams[i].dwType)
  291. {
  292. case REG_BOOL:
  293. if (SUCCEEDED(HrRegQueryDword(
  294. hkey,
  295. prgVtpParams[i].pszRegValName,
  296. &dwTmp
  297. )))
  298. {
  299. fTmp = !!dwTmp;
  300. hrTmp = pncsp->HrSetupSetFirstStringAsBool(
  301. pszSection,
  302. prgVtpParams[i].pszFileValName,
  303. fTmp
  304. );
  305. }
  306. break;
  307. case REG_DWORD:
  308. if (SUCCEEDED(HrRegQueryDword(
  309. hkey,
  310. prgVtpParams[i].pszRegValName,
  311. &dwTmp
  312. )))
  313. {
  314. hrTmp = pncsp->HrSetupSetFirstDword(
  315. pszSection,
  316. prgVtpParams[i].pszFileValName,
  317. dwTmp
  318. );
  319. }
  320. break;
  321. case REG_SZ:
  322. if (SUCCEEDED(HrRegQueryString(
  323. hkey,
  324. prgVtpParams[i].pszRegValName,
  325. &strTmp
  326. )))
  327. {
  328. hrTmp = pncsp->HrSetupSetFirstString(
  329. pszSection,
  330. prgVtpParams[i].pszFileValName,
  331. strTmp.c_str()
  332. );
  333. }
  334. break;
  335. case REG_MULTI_SZ:
  336. if (SUCCEEDED(HrRegQueryMultiSzWithAlloc(
  337. hkey,
  338. prgVtpParams[i].pszRegValName,
  339. &mszTmp
  340. )))
  341. {
  342. hrTmp = pncsp->HrSetupSetFirstMultiSzField(
  343. pszSection,
  344. prgVtpParams[i].pszFileValName,
  345. mszTmp
  346. );
  347. delete [] mszTmp;
  348. }
  349. break;
  350. }
  351. //we dont pass the error of hrTmp out of this function because
  352. //there is not much we can do with this error
  353. #ifdef ENABLETRACE
  354. if (FAILED(hrTmp))
  355. {
  356. TraceTag(ttidError, "Tcpip: HrSysPrepSaveInterfaceParams: failed to set %S to the registry. hr = %x.",
  357. prgVtpParams[i].pszFileValName, hrTmp);
  358. }
  359. #endif
  360. }
  361. return hr;
  362. }
  363. HRESULT HrSysPrepLoadInterfaceParams(
  364. HINF hinf,
  365. PCWSTR pszSection,
  366. HKEY hkeyParam,
  367. const SysprepValueNameTypeMapping * prgVtpParams,
  368. UINT cParams
  369. )
  370. {
  371. Assert(prgVtpParams);
  372. if (NULL == hinf || NULL == pszSection || NULL == hkeyParam || NULL == prgVtpParams)
  373. return E_INVALIDARG;
  374. HRESULT hr = S_OK;
  375. HRESULT hrTmp = S_OK;
  376. HRESULT hrReg = S_OK;
  377. BOOL fTmp;
  378. DWORD dwTmp;
  379. tstring strTmp;
  380. WCHAR * mszTmp;
  381. for(UINT i = 0; i < cParams; i++)
  382. {
  383. hrReg = S_OK;
  384. hrTmp = S_OK;
  385. switch(prgVtpParams[i].dwType)
  386. {
  387. case REG_BOOL:
  388. hrTmp = HrSetupGetFirstStringAsBool(
  389. hinf,
  390. pszSection,
  391. prgVtpParams[i].pszFileValName,
  392. &fTmp
  393. );
  394. if (SUCCEEDED(hrTmp))
  395. hrReg = HrRegSetDword(hkeyParam,
  396. prgVtpParams[i].pszRegValName,
  397. fTmp);
  398. break;
  399. case REG_DWORD:
  400. hrTmp = HrSetupGetFirstDword(
  401. hinf,
  402. pszSection,
  403. prgVtpParams[i].pszFileValName,
  404. &dwTmp
  405. );
  406. if (SUCCEEDED(hrTmp))
  407. hrReg = HrRegSetDword(hkeyParam,
  408. prgVtpParams[i].pszRegValName,
  409. dwTmp);
  410. break;
  411. case REG_SZ:
  412. hrTmp = HrSetupGetFirstString(
  413. hinf,
  414. pszSection,
  415. prgVtpParams[i].pszFileValName,
  416. &strTmp
  417. );
  418. if (SUCCEEDED(hrTmp))
  419. hrReg = HrRegSetString(hkeyParam,
  420. prgVtpParams[i].pszRegValName,
  421. strTmp);
  422. break;
  423. case REG_MULTI_SZ:
  424. hrTmp = HrSetupGetFirstMultiSzFieldWithAlloc(
  425. hinf,
  426. pszSection,
  427. prgVtpParams[i].pszFileValName,
  428. &mszTmp
  429. );
  430. if (SUCCEEDED(hrTmp))
  431. {
  432. hrReg = HrRegSetMultiSz(hkeyParam,
  433. prgVtpParams[i].pszRegValName,
  434. mszTmp);
  435. delete [] mszTmp;
  436. }
  437. break;
  438. }
  439. if(FAILED(hrTmp))
  440. {
  441. if(hrTmp == SPAPI_E_LINE_NOT_FOUND)
  442. hrTmp = S_OK;
  443. else
  444. {
  445. TraceTag(ttidError,
  446. "Tcpip: HrSysPrepLoadInterfaceParams: failed to load %S from the answer file. hr = %x.",
  447. prgVtpParams[i].pszFileValName,
  448. hrTmp
  449. );
  450. }
  451. }
  452. #ifdef ENABLETRACE
  453. if(FAILED(hrReg))
  454. {
  455. TraceTag(ttidError,
  456. "HrSysPrepLoadInterfaceParams: failed to set %S to the registry. hr = %x.",
  457. prgVtpParams[i].pszRegValName,
  458. hrReg);
  459. }
  460. #endif
  461. //we dont pass the error of hrTmp out of this function because
  462. //there is not much we can do with this error
  463. }
  464. TraceError("CTcpipcfg::HrSysPrepLoadInterfaceParams", hr);
  465. return hr;
  466. }