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.

409 lines
9.1 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: upg.cpp
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "upg.h"
  13. #include <time.h>
  14. //----------------------------------------------------
  15. //
  16. // Global variables
  17. //
  18. //
  19. TCHAR g_szOdbcDsn[128]=NT4LSERVER_DEFAULT_DSN; // ODBC DSN
  20. TCHAR g_szOdbcUser[128]=NT4LSERVER_DEFAULT_USER; // ODBC User Name
  21. TCHAR g_szOdbcPwd[128]=NT4LSERVER_DEFAULT_PWD; // ODBC Password
  22. TCHAR g_szMdbFile[MAX_PATH+1];
  23. //--------------------------------------------------------------------------
  24. DWORD
  25. GetNT4DbConfig(
  26. LPTSTR pszDsn,
  27. LPTSTR pszUserName,
  28. LPTSTR pszPwd,
  29. LPTSTR pszMdbFile
  30. )
  31. /*++
  32. ++*/
  33. {
  34. HKEY hKey = NULL;
  35. DWORD dwStatus = ERROR_SUCCESS;
  36. TCHAR szOdbcDsn[128]=NT4LSERVER_DEFAULT_DSN; // ODBC DSN
  37. TCHAR szOdbcUser[128]=NT4LSERVER_DEFAULT_USER; // ODBC User Name
  38. TCHAR szOdbcPwd[128]=NT4LSERVER_DEFAULT_PWD; // ODBC Password
  39. TCHAR szMdbFile[MAX_PATH+1];
  40. DWORD dwBuffer=0;
  41. PBYTE pbData = NULL;
  42. DWORD cbData = 0;
  43. BOOL bSuccess;
  44. //
  45. // Open NT4 license server specific registry key
  46. //
  47. dwStatus = RegOpenKeyEx(
  48. HKEY_LOCAL_MACHINE,
  49. NT4LSERVER_REGKEY,
  50. 0,
  51. KEY_ALL_ACCESS,
  52. &hKey
  53. );
  54. if(dwStatus != ERROR_SUCCESS)
  55. {
  56. dwStatus = ERROR_INVALID_NT4_SETUP;
  57. goto cleanup;
  58. }
  59. //
  60. // Load ODBC DSN and User name from registry,
  61. // ignore error return and use default value.
  62. //
  63. dwBuffer = sizeof(szOdbcDsn);
  64. dwStatus = RegQueryValueEx(
  65. hKey,
  66. NT4LSERVER_PARAMETERS_DSN,
  67. NULL,
  68. NULL,
  69. (LPBYTE)szOdbcDsn,
  70. &dwBuffer
  71. );
  72. if(dwStatus == ERROR_SUCCESS && pszDsn)
  73. {
  74. lstrcpy(pszDsn, szOdbcDsn);
  75. }
  76. dwBuffer = sizeof(szOdbcUser);
  77. dwStatus = RegQueryValueEx(
  78. hKey,
  79. NT4LSERVER_PARAMETERS_USER,
  80. NULL,
  81. NULL,
  82. (LPBYTE)szOdbcUser,
  83. &dwBuffer
  84. );
  85. if(dwStatus == ERROR_SUCCESS && pszUserName)
  86. {
  87. lstrcpy(pszUserName, szOdbcUser);
  88. }
  89. //
  90. // Load database password from LSA
  91. //
  92. dwStatus = RetrieveKey(
  93. LSERVER_LSA_PASSWORD_KEYNAME,
  94. &pbData,
  95. &cbData
  96. );
  97. #ifndef PRIVATE_DBG
  98. if(dwStatus != ERROR_SUCCESS)
  99. {
  100. //
  101. // Invalid NT4 license server setup or hydra beta2
  102. // license server which we don't support.
  103. //
  104. dwStatus = ERROR_INVALID_NT4_SETUP;
  105. goto cleanup;
  106. }
  107. #endif
  108. dwStatus = ERROR_SUCCESS;
  109. memset(szOdbcPwd, 0, sizeof(szOdbcPwd));
  110. memcpy(
  111. (PBYTE)szOdbcPwd,
  112. pbData,
  113. min(cbData, sizeof(szOdbcPwd) - sizeof(TCHAR))
  114. );
  115. if(pszPwd != NULL)
  116. {
  117. lstrcpy(pszPwd, szOdbcPwd);
  118. }
  119. //
  120. // Verify data source is properly installed
  121. //
  122. bSuccess = IsDataSourceInstalled(
  123. szOdbcDsn,
  124. ODBC_SYSTEM_DSN,
  125. szMdbFile,
  126. MAX_PATH
  127. );
  128. if(bSuccess == FALSE)
  129. {
  130. dwStatus = ERROR_INVALID_NT4_SETUP;
  131. goto cleanup;
  132. }
  133. if(pszMdbFile != NULL)
  134. {
  135. _tcscpy(pszMdbFile, szMdbFile);
  136. }
  137. cleanup:
  138. if(hKey != NULL)
  139. {
  140. RegCloseKey(hKey);
  141. }
  142. if(pbData != NULL)
  143. {
  144. LocalFree(pbData);
  145. }
  146. return dwStatus;
  147. }
  148. //--------------------------------------------------------------------------
  149. DWORD
  150. DeleteNT4ODBCDataSource()
  151. /*++
  152. --*/
  153. {
  154. BOOL bSuccess;
  155. DWORD dwStatus = ERROR_SUCCESS;
  156. //
  157. // Get Hydra 4 DB configuration, make sure
  158. // data source is properly config.
  159. //
  160. dwStatus = GetNT4DbConfig(
  161. g_szOdbcDsn,
  162. g_szOdbcUser,
  163. g_szOdbcPwd,
  164. g_szMdbFile
  165. );
  166. if(dwStatus == ERROR_SUCCESS)
  167. {
  168. bSuccess = ConfigDataSource(
  169. NULL,
  170. FALSE,
  171. _TEXT(SZACCESSDRIVERNAME),
  172. g_szOdbcDsn,
  173. g_szOdbcUser,
  174. g_szOdbcPwd,
  175. g_szMdbFile
  176. );
  177. if(bSuccess == FALSE)
  178. {
  179. dwStatus = ERROR_DELETE_ODBC_DSN;
  180. }
  181. }
  182. return dwStatus;
  183. }
  184. //--------------------------------------------------------------------------
  185. #define LSERVER_SOFTWARE_REGBASE \
  186. _TEXT("SOFTWARE\\Microsoft\\") _TEXT(SZSERVICENAME)
  187. #define LSERVER_CERTIFICATE_STORE _TEXT("Certificates")
  188. #define LSERVER_SERVER_CERTIFICATE_REGKEY \
  189. LSERVER_SOFTWARE_REGBASE _TEXT("\\") LSERVER_CERTIFICATE_STORE
  190. #define LSERVER_CLIENT_CERTIFICATE_ISSUER _TEXT("Parm0")
  191. #define LSERVER_SIGNATURE_CERT_KEY _TEXT("Parm1")
  192. #define LSERVER_EXCHANGE_CERT_KEY _TEXT("Parm2")
  193. void
  194. CleanLicenseServerSecret()
  195. /*++
  196. --*/
  197. {
  198. DWORD dwStatus = ERROR_SUCCESS;
  199. HKEY hKey = NULL;
  200. //
  201. // Wipe out SPK in LSA
  202. //
  203. dwStatus = StoreKey(
  204. LSERVER_LSA_LSERVERID,
  205. (PBYTE) NULL,
  206. 0
  207. );
  208. dwStatus = StoreKey(
  209. LSERVER_LSA_LASTRUN,
  210. (PBYTE) NULL,
  211. 0
  212. );
  213. dwStatus = StoreKey(
  214. LSERVER_LSA_PRIVATEKEY_EXCHANGE,
  215. (PBYTE) NULL,
  216. 0
  217. );
  218. dwStatus = StoreKey(
  219. LSERVER_LSA_PRIVATEKEY_SIGNATURE,
  220. (PBYTE) NULL,
  221. 0
  222. );
  223. dwStatus=RegOpenKeyEx(
  224. HKEY_LOCAL_MACHINE,
  225. LSERVER_SERVER_CERTIFICATE_REGKEY,
  226. 0,
  227. KEY_ALL_ACCESS,
  228. &hKey
  229. );
  230. if(dwStatus == ERROR_SUCCESS)
  231. {
  232. //
  233. // Ignore error
  234. RegDeleteValue(
  235. hKey,
  236. LSERVER_SIGNATURE_CERT_KEY
  237. );
  238. RegDeleteValue(
  239. hKey,
  240. LSERVER_EXCHANGE_CERT_KEY
  241. );
  242. RegDeleteValue(
  243. hKey,
  244. LSERVER_CLIENT_CERTIFICATE_ISSUER
  245. );
  246. }
  247. if(hKey != NULL)
  248. {
  249. RegCloseKey(hKey);
  250. }
  251. return;
  252. }
  253. DWORD
  254. MigrateOneSecret(PWCHAR wszSecretName, PWCHAR wszSecretNameOld)
  255. /*++
  256. --*/
  257. {
  258. DWORD dwErr = ERROR_SUCCESS;
  259. PBYTE pbValue = NULL;
  260. DWORD cbValue = 0;
  261. dwErr = RetrieveKey(wszSecretNameOld,&pbValue,&cbValue);
  262. if (dwErr == ERROR_SUCCESS)
  263. {
  264. dwErr = StoreKey(wszSecretName,
  265. pbValue,
  266. cbValue
  267. );
  268. if (dwErr == ERROR_SUCCESS)
  269. {
  270. StoreKey(wszSecretNameOld,
  271. (PBYTE) NULL,
  272. 0
  273. );
  274. }
  275. else
  276. {
  277. goto done;
  278. }
  279. }
  280. else if (dwErr == ERROR_FILE_NOT_FOUND)
  281. {
  282. dwErr = ERROR_SUCCESS;
  283. }
  284. else
  285. {
  286. goto done;
  287. }
  288. done:
  289. if (NULL != pbValue)
  290. {
  291. LocalFree(pbValue);
  292. }
  293. return dwErr;
  294. }
  295. DWORD
  296. MigrateLsaSecrets()
  297. /*++
  298. --*/
  299. {
  300. DWORD dwErr = ERROR_SUCCESS;
  301. dwErr = MigrateOneSecret(LSERVER_LSA_PASSWORD_KEYNAME, LSERVER_LSA_PASSWORD_KEYNAME_OLD);
  302. if (dwErr != ERROR_SUCCESS)
  303. {
  304. goto done;
  305. }
  306. dwErr = MigrateOneSecret(LSERVER_LSA_LASTRUN, LSERVER_LSA_LASTRUN_OLD);
  307. if (dwErr != ERROR_SUCCESS)
  308. {
  309. goto done;
  310. }
  311. dwErr = MigrateOneSecret(LSERVER_LSA_SETUPID, LSERVER_LSA_SETUPID_OLD);
  312. if (dwErr != ERROR_SUCCESS)
  313. {
  314. goto done;
  315. }
  316. dwErr = MigrateOneSecret(LSERVER_LSA_PRIVATEKEY_SIGNATURE, LSERVER_LSA_PRIVATEKEY_SIGNATURE_OLD);
  317. if (dwErr != ERROR_SUCCESS)
  318. {
  319. goto done;
  320. }
  321. dwErr = MigrateOneSecret(LSERVER_LSA_PRIVATEKEY_EXCHANGE, LSERVER_LSA_PRIVATEKEY_EXCHANGE_OLD);
  322. if (dwErr != ERROR_SUCCESS)
  323. {
  324. goto done;
  325. }
  326. dwErr = MigrateOneSecret(LSERVER_LSA_LSERVERID, LSERVER_LSA_LSERVERID_OLD);
  327. if (dwErr != ERROR_SUCCESS)
  328. {
  329. goto done;
  330. }
  331. done:
  332. return dwErr;
  333. }