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.

488 lines
13 KiB

  1. /*
  2. File: sap.c
  3. Performs an upgrade of ipx sap to nt5 router
  4. by munging registry values.
  5. Paul Mayfield, 9/3/97
  6. */
  7. #include "upgrade.h"
  8. // Structure for data passed to the enumeration
  9. // function.
  10. typedef struct _SAP_ENUM_DATA {
  11. PSAP_IF_CONFIG pDefaults;
  12. } SAP_ENUM_DATA;
  13. // Globals
  14. static WCHAR szIpxSapKey[] =
  15. L"System\\CurrentControlSet\\Services\\RemoteAccess\\RouterManagers\\IPX\\RoutingProtocols\\IPXSAP\\Parameters";
  16. static WCHAR szTempKey[] = L"DeleteMe";
  17. static HKEY hkRouter = NULL;
  18. static HKEY hkTemp = NULL;
  19. static HKEY hkIpxSap = NULL;
  20. static PWCHAR IpxSapParams[] =
  21. {
  22. L"SendTime",
  23. L"EntryTimeout",
  24. L"WANFilter",
  25. L"WANUpdateTime",
  26. L"MaxRecvBufferLookAhead",
  27. L"RespondForInternalServers",
  28. L"DelayRespondToGeneral",
  29. L"DelayChangeBroadcast",
  30. L"NameTableReservedHeapSize",
  31. L"NameTableSortLatency",
  32. L"MaxUnsortedNames",
  33. L"TriggeredUpdateCheckInterval",
  34. L"MaxTriggeredUpdateRequests",
  35. L"ShutdownBroadcastTimeout",
  36. L"RequestsPerInterface",
  37. L"MinimumRequests"
  38. };
  39. //
  40. // Restore the registry from from backup and make sure
  41. // all global handles are opened
  42. //
  43. DWORD SapPrepareRegistry(
  44. IN PWCHAR BackupFileName)
  45. {
  46. DWORD dwErr, dwDisposition;
  47. // Get access to the router registry key
  48. dwErr = UtlAccessRouterKey(&hkRouter);
  49. if (dwErr != ERROR_SUCCESS) {
  50. PrintMessage(L"Unable to access router key.\n");
  51. return dwErr;
  52. }
  53. // Restore the router key from backup
  54. __try {
  55. // Open up the temporary key
  56. dwErr = RegCreateKeyEx(
  57. hkRouter,
  58. szTempKey,
  59. 0,
  60. NULL,
  61. 0,
  62. KEY_ALL_ACCESS,
  63. NULL,
  64. &hkTemp,
  65. &dwDisposition);
  66. if (dwErr != ERROR_SUCCESS)
  67. return dwErr;
  68. // Restore the saved registry information
  69. // to the temp key
  70. UtlSetupRestorePrivilege(TRUE);
  71. dwErr = RegRestoreKeyW(
  72. hkTemp,
  73. BackupFileName,
  74. 0);
  75. if (dwErr != ERROR_SUCCESS)
  76. return dwErr;
  77. // Open up the ipx sap params key
  78. dwErr = RegCreateKeyEx(
  79. HKEY_LOCAL_MACHINE,
  80. szIpxSapKey,
  81. 0,
  82. NULL,
  83. 0,
  84. KEY_ALL_ACCESS,
  85. NULL,
  86. &hkIpxSap,
  87. &dwDisposition);
  88. if (dwErr != ERROR_SUCCESS)
  89. return dwErr;
  90. }
  91. __finally {
  92. UtlSetupRestorePrivilege(FALSE);
  93. }
  94. return NO_ERROR;
  95. }
  96. //
  97. // Cleanup the work done in the registry
  98. //
  99. DWORD SapCleanupRegistry() {
  100. if (hkIpxSap)
  101. RegCloseKey(hkIpxSap);
  102. if (hkTemp)
  103. RegCloseKey(hkTemp);
  104. if (hkRouter) {
  105. RegDeleteKey(hkRouter,szTempKey);
  106. RegCloseKey(hkRouter);
  107. }
  108. hkIpxSap = NULL;
  109. hkTemp = NULL;
  110. hkRouter = NULL;
  111. return NO_ERROR;
  112. }
  113. //
  114. // Restores the sap parameters that were saved before upgrade.
  115. // Assumes those parameters are being stored temporarily in hkTemp
  116. //
  117. DWORD SapRestoreParameters() {
  118. DWORD dwErr, dwVal;
  119. PWCHAR* IpxSapParamPtr = IpxSapParams;
  120. dwt NwSapParams;
  121. // Load in the parameters that were set for nwsap
  122. __try {
  123. dwErr = dwtLoadRegistyTable(&NwSapParams, hkTemp);
  124. if (dwErr != NO_ERROR)
  125. return dwErr;
  126. // Loop through the ipx params copying over any that applied
  127. // to nwsap
  128. while (IpxSapParamPtr && *IpxSapParamPtr) {
  129. dwErr = dwtGetValue(&NwSapParams, *IpxSapParamPtr, &dwVal);
  130. if (dwErr == NO_ERROR) {
  131. dwErr = RegSetValueEx(
  132. hkIpxSap,
  133. *IpxSapParamPtr,
  134. 0,
  135. REG_DWORD,
  136. (LPBYTE)&dwVal,
  137. sizeof(DWORD));
  138. if (dwErr != ERROR_SUCCESS)
  139. return dwErr;
  140. }
  141. IpxSapParamPtr++;
  142. }
  143. }
  144. __finally {
  145. dwtCleanup(&NwSapParams);
  146. }
  147. return NO_ERROR;
  148. }
  149. //
  150. // Installs sap in the router by initializing the
  151. // sap global info blob.
  152. //
  153. DWORD SapInstallTransportInfo(
  154. IN SAP_GLOBAL_INFO * pGlobal,
  155. IN SAP_IF_CONFIG * pIfDefaults)
  156. {
  157. LPBYTE pGlobalInfo = NULL, pDialinInfo = NULL;
  158. LPBYTE pNewGlobalInfo = NULL, pNewDialinInfo = NULL;
  159. HANDLE hConfig = NULL, hTrans = NULL;
  160. SAP_IF_CONFIG SapCfg, *pDialinCfg = &SapCfg;
  161. DWORD dwErr, dwGlobalInfoSize = 0, dwDialinInfoSize = 0;
  162. DWORD dwNewGlobSize = 0, dwNewDialSize = 0;
  163. do {
  164. // Connect to config server
  165. dwErr = MprConfigServerConnect(NULL, &hConfig);
  166. if (dwErr != NO_ERROR)
  167. break;
  168. // Get handle to global ipx tranport info
  169. dwErr = MprConfigTransportGetHandle (
  170. hConfig,
  171. PID_IPX,
  172. &hTrans);
  173. if (dwErr != NO_ERROR)
  174. break;
  175. // Get global ipx tranport info
  176. dwErr = MprConfigTransportGetInfo(
  177. hConfig,
  178. hTrans,
  179. &pGlobalInfo,
  180. &dwGlobalInfoSize,
  181. &pDialinInfo,
  182. &dwDialinInfoSize,
  183. NULL);
  184. if (dwErr != NO_ERROR)
  185. break;
  186. // Initialize the global info blob
  187. dwErr = UtlUpdateInfoBlock(
  188. FALSE,
  189. pGlobalInfo,
  190. IPX_PROTOCOL_SAP,
  191. sizeof(SAP_GLOBAL_INFO),
  192. 1,
  193. (LPBYTE)pGlobal,
  194. &pNewGlobalInfo,
  195. &dwNewGlobSize);
  196. if (dwErr != NO_ERROR) {
  197. if (dwErr != ERROR_ALREADY_EXISTS)
  198. break;
  199. pNewGlobalInfo = NULL;
  200. dwNewGlobSize = 0;
  201. }
  202. // Initialize the dialin info blob
  203. CopyMemory(pDialinCfg, pIfDefaults, sizeof(SAP_IF_CONFIG));
  204. pDialinCfg->SapIfInfo.UpdateMode = IPX_NO_UPDATE;
  205. dwErr = UtlUpdateInfoBlock(
  206. FALSE,
  207. pDialinInfo,
  208. IPX_PROTOCOL_SAP,
  209. sizeof(SAP_IF_CONFIG),
  210. 1,
  211. (LPBYTE)pDialinCfg,
  212. &pNewDialinInfo,
  213. &dwNewDialSize);
  214. if (dwErr != NO_ERROR) {
  215. if (dwErr != ERROR_ALREADY_EXISTS)
  216. break;
  217. pNewDialinInfo = NULL;
  218. dwNewDialSize = 0;
  219. }
  220. // Set global ipx tranport info
  221. dwErr = MprConfigTransportSetInfo(
  222. hConfig,
  223. hTrans,
  224. pNewGlobalInfo,
  225. dwNewGlobSize,
  226. pNewDialinInfo,
  227. dwNewDialSize,
  228. NULL);
  229. if (dwErr != NO_ERROR)
  230. break;
  231. } while (FALSE);
  232. // Cleanup
  233. {
  234. if (hConfig)
  235. MprConfigServerDisconnect(hConfig);
  236. if (pGlobalInfo)
  237. MprConfigBufferFree(pGlobalInfo);
  238. if (pDialinInfo)
  239. MprConfigBufferFree(pDialinInfo);
  240. if (pNewDialinInfo)
  241. MprConfigBufferFree(pNewDialinInfo);
  242. if (pNewGlobalInfo)
  243. MprConfigBufferFree(pNewGlobalInfo);
  244. }
  245. return dwErr;
  246. }
  247. //
  248. // Callback function takes an interface and updates
  249. // its ipx sap configuration.
  250. //
  251. // Returns TRUE to continue the enumerate, FALSE to
  252. // stop it
  253. //
  254. DWORD SapUpgradeInterface(
  255. IN HANDLE hConfig,
  256. IN MPR_INTERFACE_0 * pIf,
  257. IN HANDLE hUserData)
  258. {
  259. SAP_ENUM_DATA* pData = (SAP_ENUM_DATA*)hUserData;
  260. SAP_IF_CONFIG SapCfg, *pConfig = &SapCfg;
  261. LPBYTE pTransInfo=NULL, pNewTransInfo=NULL;
  262. HANDLE hTransport = NULL;
  263. DWORD dwErr, dwSize, dwNewSize = 0;
  264. // Validate input
  265. if ((hConfig == NULL) ||
  266. (pIf == NULL) ||
  267. (pData == NULL))
  268. {
  269. return FALSE;
  270. }
  271. // Initalize the config blob
  272. CopyMemory(pConfig, pData->pDefaults, sizeof(SAP_IF_CONFIG));
  273. // Customize the update mode for the router interface
  274. // type
  275. switch (pIf->dwIfType) {
  276. case ROUTER_IF_TYPE_DEDICATED:
  277. pConfig->SapIfInfo.UpdateMode = IPX_STANDARD_UPDATE;
  278. break;
  279. case ROUTER_IF_TYPE_INTERNAL:
  280. case ROUTER_IF_TYPE_CLIENT:
  281. pConfig->SapIfInfo.UpdateMode = IPX_NO_UPDATE;
  282. break;
  283. case ROUTER_IF_TYPE_HOME_ROUTER:
  284. case ROUTER_IF_TYPE_FULL_ROUTER:
  285. pConfig->SapIfInfo.UpdateMode = IPX_AUTO_STATIC_UPDATE;
  286. break;
  287. case ROUTER_IF_TYPE_LOOPBACK:
  288. case ROUTER_IF_TYPE_TUNNEL1:
  289. default:
  290. return TRUE;
  291. }
  292. do {
  293. // Get the handle to ipx info associated with this if
  294. dwErr = MprConfigInterfaceTransportGetHandle(
  295. hConfig,
  296. pIf->hInterface,
  297. PID_IPX,
  298. &hTransport);
  299. if (dwErr != NO_ERROR)
  300. break;
  301. // Get the ipx info associated with this if
  302. dwErr = MprConfigInterfaceTransportGetInfo(
  303. hConfig,
  304. pIf->hInterface,
  305. hTransport,
  306. &pTransInfo,
  307. &dwSize);
  308. if (dwErr != NO_ERROR)
  309. break;
  310. // Update the info block
  311. dwErr = UtlUpdateInfoBlock(
  312. FALSE,
  313. pTransInfo,
  314. IPX_PROTOCOL_SAP,
  315. dwSize,
  316. 1,
  317. (LPBYTE)pConfig,
  318. &pNewTransInfo,
  319. &dwNewSize);
  320. if (dwErr != NO_ERROR) {
  321. if (dwErr != ERROR_ALREADY_EXISTS)
  322. break;
  323. pNewTransInfo = NULL;
  324. dwNewSize = 0;
  325. }
  326. // Commit the change
  327. dwErr = MprConfigInterfaceTransportSetInfo(
  328. hConfig,
  329. pIf->hInterface,
  330. hTransport,
  331. pNewTransInfo,
  332. dwNewSize);
  333. if (dwErr != NO_ERROR)
  334. break;
  335. } while (FALSE);
  336. // Cleanup
  337. {
  338. if (pNewTransInfo)
  339. MprConfigBufferFree(pNewTransInfo);
  340. if (pTransInfo)
  341. MprConfigBufferFree(pTransInfo);
  342. }
  343. return TRUE;
  344. }
  345. //
  346. // Installs ipx sap in the router registry tree.
  347. //
  348. DWORD SapInstallInRouter()
  349. {
  350. DWORD dwErr;
  351. SAP_IF_CONFIG SapConfig, * pSap = &SapConfig;
  352. SAP_ENUM_DATA SapBlobs =
  353. {
  354. pSap
  355. };
  356. SAP_GLOBAL_INFO SapGlobal =
  357. {
  358. EVENTLOG_ERROR_TYPE // event log mask
  359. };
  360. // Clear all structures
  361. ZeroMemory (pSap, sizeof(SAP_IF_CONFIG));
  362. // Default lan configuration
  363. pSap->SapIfInfo.AdminState = ADMIN_STATE_ENABLED;
  364. pSap->SapIfInfo.UpdateMode = IPX_STANDARD_UPDATE;
  365. pSap->SapIfInfo.PacketType = IPX_STANDARD_PACKET_TYPE;
  366. pSap->SapIfInfo.Supply = ADMIN_STATE_ENABLED;
  367. pSap->SapIfInfo.Listen = ADMIN_STATE_ENABLED;
  368. pSap->SapIfInfo.GetNearestServerReply = ADMIN_STATE_ENABLED;
  369. pSap->SapIfInfo.PeriodicUpdateInterval = 60;
  370. pSap->SapIfInfo.AgeIntervalMultiplier = 3;
  371. pSap->SapIfFilters.SupplyFilterAction = IPX_SERVICE_FILTER_DENY;
  372. pSap->SapIfFilters.SupplyFilterCount = 0;
  373. pSap->SapIfFilters.ListenFilterAction = IPX_SERVICE_FILTER_DENY;
  374. pSap->SapIfFilters.ListenFilterCount = 0;
  375. // Install default sap global info
  376. dwErr = SapInstallTransportInfo(&SapGlobal, pSap);
  377. if (dwErr != NO_ERROR)
  378. return dwErr;
  379. // Enumerate the interfaces, updating each one with
  380. // sap config as you go.
  381. dwErr = UtlEnumerateInterfaces(
  382. SapUpgradeInterface,
  383. &SapBlobs);
  384. if (dwErr != NO_ERROR)
  385. return dwErr;
  386. return NO_ERROR;
  387. }
  388. //
  389. // Performs all of the registry updating associated with an
  390. // upgrade from ipx sap to router.
  391. //
  392. // These are the steps:
  393. // 1. Restore the parameters saved in FileName to szIpxSapKey.
  394. // 2. Remove all parameters that ipx sap does not implement.
  395. //
  396. DWORD SapToRouterUpgrade(
  397. IN PWCHAR FileName)
  398. {
  399. DWORD dwErr;
  400. __try {
  401. // Restore the registry from the backup file
  402. dwErr = SapPrepareRegistry(FileName);
  403. if (dwErr != NO_ERROR)
  404. return dwErr;
  405. // Set the new registry parameters
  406. dwErr = SapRestoreParameters();
  407. if (dwErr != NO_ERROR)
  408. return dwErr;
  409. // Install default sap global config and set default
  410. // values in all router interfaces.
  411. dwErr = SapInstallInRouter();
  412. if (dwErr != NO_ERROR)
  413. return dwErr;
  414. // Mark the computer as having been configured
  415. //
  416. dwErr = UtlMarkRouterConfigured();
  417. if (dwErr != NO_ERROR)
  418. {
  419. PrintMessage(L"Unable to mark router as configured.\n");
  420. return dwErr;
  421. }
  422. }
  423. __finally {
  424. SapCleanupRegistry();
  425. }
  426. return NO_ERROR;
  427. }