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.

1765 lines
47 KiB

  1. // Copyright (c) 2000 Microsoft Corporation, All Rights Reserved
  2. #include "precomp.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <wbemint.h>
  7. #include "upgrade.h"
  8. #include "wbemutil.h"
  9. #include "reg.h"
  10. #include "export.h"
  11. #include "import.h"
  12. #include <WDMSHELL.h>
  13. #include <wmimof.h>
  14. #include <wmicom.h>
  15. #include <setupapi.h>
  16. #include <persistcfg.h>
  17. #include <str.h>
  18. #include <helper.h>
  19. //Handy pointer to the MMF arena which almost every file
  20. //to do with the on-disk representation management uses.
  21. CMMFArena2* g_pDbArena = 0;
  22. bool DoCoreUpgrade(int nInstallType )
  23. {
  24. LogMessage(MSG_INFO, "Beginning Core Upgrade");
  25. bool bRet = true;
  26. bool bCoreFailure = false;
  27. bool bExternalFailure = false;
  28. bool bOrgRepositoryPreserved = false;
  29. CMultiString mszSystemMofs;
  30. CMultiString mszExternalMofList;
  31. CString szFailedSystemMofs;
  32. CString szFailedExternalMofs;
  33. CString szMissingMofs;
  34. Registry r(WBEM_REG_WINMGMT);
  35. if (r.GetStatus() != no_error)
  36. {
  37. LogMessage(MSG_ERROR, "Unable to access registry for DoCoreUpgrade.");
  38. return false;
  39. }
  40. IWbemContext * pCtx = NULL;
  41. HRESULT hRes = CoCreateInstance(CLSID_WbemContext, 0, CLSCTX_INPROC_SERVER,
  42. IID_IWbemContext, (LPVOID *) &pCtx);
  43. if (FAILED(hRes))
  44. {
  45. LogMessage(MSG_ERROR, "Unable to create CLSID_WbemContext.");
  46. return false;
  47. }
  48. OnDelete<IUnknown *,void(*)(IUnknown *),RM> rmCtx(pCtx);
  49. _variant_t Var = true;
  50. if (FAILED(hRes = pCtx->SetValue(L"__MOFD_DO_STORE",0,&Var))) return false;
  51. IWinmgmtMofCompiler * pCompiler = NULL;
  52. SCODE sc = CoCreateInstance(CLSID_WinmgmtMofCompiler, 0, CLSCTX_INPROC_SERVER,
  53. IID_IWinmgmtMofCompiler, (LPVOID *) &pCompiler);
  54. if(SUCCEEDED(sc))
  55. {
  56. GetStandardMofs(mszSystemMofs, nInstallType);
  57. UpgradeAutoRecoveryRegistry(mszSystemMofs, mszExternalMofList, szMissingMofs);
  58. WipeOutAutoRecoveryRegistryEntries();
  59. if (DoesFSRepositoryExist())
  60. {
  61. // check whether repository needs upgrading, and perform upgrade if necessary
  62. bOrgRepositoryPreserved = UpgradeRepository();
  63. }
  64. // if we find an MMF, convert it, regardless of whether another repository already exists
  65. if (DoesMMFRepositoryExist())
  66. {
  67. bOrgRepositoryPreserved = DoConvertRepository();
  68. }
  69. bRet = LoadMofList(pCtx,pCompiler, mszSystemMofs, szFailedSystemMofs);
  70. if (bRet == false)
  71. bCoreFailure = true;
  72. // if the repository did not exist when we began,
  73. // or we had to create a new one due to an upgrade failure,
  74. // we need to reload external mofs
  75. if (!bOrgRepositoryPreserved)
  76. {
  77. bRet = LoadMofList(pCtx,pCompiler, mszExternalMofList, szFailedExternalMofs);
  78. if (bRet == false)
  79. bExternalFailure = true;
  80. }
  81. pCompiler->Release();
  82. //Part of the tidy-up code is to write back the registry entries, so here we go...
  83. WriteBackAutoRecoveryMofs(mszSystemMofs, mszExternalMofList);
  84. FILETIME ftCurTime;
  85. LARGE_INTEGER liCurTime;
  86. char szBuff[50];
  87. GetSystemTimeAsFileTime(&ftCurTime);
  88. liCurTime.LowPart = ftCurTime.dwLowDateTime;
  89. liCurTime.HighPart = ftCurTime.dwHighDateTime;
  90. _ui64toa(liCurTime.QuadPart, szBuff, 10);
  91. r.SetStr("Autorecover MOFs timestamp", szBuff);
  92. }
  93. else
  94. {
  95. bRet = false;
  96. }
  97. if (szFailedSystemMofs.Length())
  98. {
  99. LogMessage(MSG_ERROR, "The following WMI CORE MOF file(s) failed to load:");
  100. LogMessage(MSG_ERROR, szFailedSystemMofs);
  101. }
  102. else if (bCoreFailure)
  103. {
  104. LogMessage(MSG_NTSETUPERROR, "None of the WMI CORE MOFs could be loaded.");
  105. }
  106. else if (szFailedExternalMofs.Length())
  107. {
  108. LogMessage(MSG_ERROR, "The following External MOF file(s) failed to load:");
  109. LogMessage(MSG_ERROR, szFailedExternalMofs);
  110. }
  111. else if (bExternalFailure)
  112. {
  113. LogMessage(MSG_NTSETUPERROR, "None of the External MOFs could be loaded.");
  114. }
  115. else if (bRet == false)
  116. {
  117. LogMessage(MSG_NTSETUPERROR, "No MOFs could be loaded because the MOF Compiler failed to intialize.");
  118. }
  119. if (szMissingMofs.Length())
  120. {
  121. LogMessage(MSG_WARNING, "The following MOFs could not be found and were removed from the auto-recovery registry setting:");
  122. LogMessage(MSG_WARNING, szMissingMofs);
  123. }
  124. LogMessage(MSG_INFO, "Core Upgrade completed.");
  125. return bRet;
  126. }
  127. bool UpgradeAutoRecoveryRegistry(CMultiString &mszSystemMofs, CMultiString &mszExternalMofList, CString &szMissingMofs)
  128. {
  129. char* pszNewList = NULL;
  130. char* pszEmptyList = NULL;
  131. char* pszRecoveredList = NULL;
  132. try
  133. {
  134. //First we need to recover the existing entries...
  135. Registry r(WBEM_REG_WINMGMT);
  136. if (r.GetStatus() != no_error)
  137. {
  138. LogMessage(MSG_ERROR, "Unable to access registry for UpgradeAutoRecoveryRegistry.");
  139. return false;
  140. }
  141. DWORD dwSize = 0;
  142. pszNewList = r.GetMultiStr(WBEM_REG_AUTORECOVER, dwSize);
  143. pszEmptyList = r.GetMultiStr(WBEM_REG_AUTORECOVER_EMPTY, dwSize);
  144. pszRecoveredList = r.GetMultiStr(WBEM_REG_AUTORECOVER_RECOVERED, dwSize);
  145. CMultiString mszOtherMofs;
  146. //Lets work through the list in the new mof list if it exists...
  147. GetNewMofLists(pszNewList, mszSystemMofs, mszOtherMofs, szMissingMofs);
  148. //Lets work through the empty list first...
  149. GetNewMofLists(pszEmptyList, mszSystemMofs, mszOtherMofs, szMissingMofs);
  150. //Lets work through the recovered list next...
  151. GetNewMofLists(pszRecoveredList, mszSystemMofs, mszOtherMofs, szMissingMofs);
  152. //Now we copy across the other MOFs to the external list...
  153. CopyMultiString(mszOtherMofs, mszExternalMofList);
  154. }
  155. catch (...)
  156. {
  157. // assume something has corrupted the registry key, so toss out the work we've done so far (empty the lists)
  158. mszExternalMofList.Empty();
  159. szMissingMofs = "";
  160. }
  161. //Tidy up the memory...
  162. delete [] pszNewList;
  163. delete [] pszEmptyList;
  164. delete [] pszRecoveredList;
  165. //Now we are done with the registry.
  166. return true;
  167. }
  168. bool GetNewMofLists(const char *pszMofList, CMultiString &mszSystemMofs, CMultiString &mszOtherMofs, CString &szMissingMofs)
  169. {
  170. // produce a standard mof list with only filenames and no paths to be used as our search list
  171. CMultiString mszStandardMofList;
  172. const char* pszFrom = mszSystemMofs;
  173. CString path;
  174. CString filename;
  175. while (pszFrom && *pszFrom)
  176. {
  177. ExtractPathAndFilename(pszFrom, path, filename);
  178. mszStandardMofList.AddUnique(filename);
  179. pszFrom += strlen(pszFrom) + 1;
  180. }
  181. // check each file to see if it is a standard mof
  182. const char *psz = pszMofList;
  183. while (psz && *psz)
  184. {
  185. if (FileExists(psz))
  186. {
  187. if (IsStandardMof(mszStandardMofList, psz))
  188. {
  189. // This means we will be loading it with this install,
  190. // so we don't need to do anything here...
  191. }
  192. else
  193. {
  194. mszOtherMofs.AddUnique(psz);
  195. }
  196. }
  197. else
  198. {
  199. if (szMissingMofs.Length())
  200. {
  201. szMissingMofs += "\n";
  202. }
  203. szMissingMofs += psz;
  204. }
  205. //Move on to the next string...
  206. psz += strlen(psz) + 1;
  207. }
  208. return true;
  209. }
  210. bool GetMofList(const char* rgpszMofFilename[], CMultiString &mszMofs)
  211. {
  212. char* pszFullName = NULL;
  213. for (int i = 0; rgpszMofFilename[i] != NULL; i++)
  214. {
  215. pszFullName = GetFullFilename(rgpszMofFilename[i]);
  216. if (pszFullName)
  217. {
  218. if (FileExists(pszFullName))
  219. mszMofs.AddUnique(pszFullName);
  220. delete [] pszFullName;
  221. pszFullName = NULL;
  222. }
  223. else
  224. {
  225. char szTemp[MAX_MSG_TEXT_LENGTH];
  226. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "Failed GetFullFilename for %s in GetMofList.", rgpszMofFilename[i]);
  227. LogMessage(MSG_ERROR, szTemp);
  228. // do not return false here, keep processing other mofs
  229. }
  230. }
  231. return true;
  232. }
  233. bool FileExists(const char *pszFilename)
  234. {
  235. char *szExpandedFilename = NULL;
  236. DWORD nRes = ExpandEnvironmentStrings(pszFilename,NULL,0);
  237. if (nRes == 0)
  238. {
  239. size_t fileNameLen = strlen(pszFilename) + 1;
  240. szExpandedFilename = new char[fileNameLen];
  241. if (szExpandedFilename == NULL)
  242. {
  243. return false;
  244. }
  245. StringCchCopyA(szExpandedFilename, fileNameLen, pszFilename);
  246. }
  247. else
  248. {
  249. szExpandedFilename = new char[nRes];
  250. if (szExpandedFilename == NULL)
  251. {
  252. return false;
  253. }
  254. nRes = ExpandEnvironmentStrings(pszFilename,szExpandedFilename,nRes);
  255. if (nRes == 0)
  256. {
  257. delete [] szExpandedFilename;
  258. return false;
  259. }
  260. }
  261. bool bExists = false;
  262. DWORD dwAttribs = GetFileAttributes(szExpandedFilename);
  263. if (dwAttribs != 0xFFFFFFFF)
  264. {
  265. bExists = true;
  266. }
  267. delete [] szExpandedFilename;
  268. return bExists;
  269. }
  270. bool IsStandardMof(CMultiString &mszStandardMofList, const char* pszMofFile)
  271. {
  272. //For this one we need to loop though our standard MOF list to see if it appears
  273. //in the list. Ignore the path if present and compare only the filename.
  274. CString path;
  275. CString filename;
  276. ExtractPathAndFilename(pszMofFile, path, filename);
  277. bool bFound = false;
  278. const char* pszCompare = mszStandardMofList;
  279. while (pszCompare && *pszCompare)
  280. {
  281. if (_stricmp(pszCompare, filename) == 0)
  282. {
  283. bFound = true;
  284. break;
  285. }
  286. pszCompare += strlen(pszCompare) + 1;
  287. }
  288. return bFound;
  289. }
  290. bool ExtractPathAndFilename(const char *pszFullPath, CString &path, CString &filename)
  291. {
  292. size_t tmpNameLen = strlen(pszFullPath) + 1;
  293. char *pszTmpName = new char[tmpNameLen];
  294. if (pszTmpName == NULL)
  295. return false;
  296. StringCchCopyA(pszTmpName, tmpNameLen, pszFullPath);
  297. char *pszFilename = pszTmpName;
  298. char *psz = strtok(pszTmpName, "\\");
  299. while (psz != NULL)
  300. {
  301. pszFilename = psz;
  302. psz = strtok(NULL, "\\");
  303. if (psz != NULL)
  304. {
  305. path += pszFilename;
  306. path += "\\";
  307. }
  308. }
  309. filename = pszFilename;
  310. delete [] pszTmpName;
  311. return true;
  312. }
  313. bool CopyMultiString(CMultiString &mszFrom, CMultiString &mszTo)
  314. {
  315. const char *pszFrom = mszFrom;
  316. while (pszFrom && *pszFrom)
  317. {
  318. //Due to the fact that we should not have duplicates in the list, we will now do
  319. //a check to inforce this...
  320. mszTo.AddUnique(pszFrom);
  321. pszFrom += strlen(pszFrom) + 1;
  322. }
  323. return true;
  324. }
  325. bool GetStandardMofs(CMultiString &mszSystemMofs, int nCurInstallType)
  326. {
  327. // find the location of the inf
  328. char* pszWinDir = new char[_MAX_PATH+1];
  329. if (!pszWinDir)
  330. {
  331. LogMessage(MSG_ERROR, "Failed to allocate memory for pszWinDir for GetStandardMofs.");
  332. return FALSE;
  333. }
  334. if (!GetWindowsDirectory(pszWinDir, _MAX_PATH+1))
  335. {
  336. LogMessage(MSG_ERROR, "Failed to retrieve Windows directory for GetStandardMofs.");
  337. delete [] pszWinDir;
  338. return FALSE;
  339. }
  340. size_t fileNameLen = strlen(pszWinDir)+strlen("\\inf\\wbemoc.inf")+1;
  341. char* pszFileName = new char[fileNameLen];
  342. if (!pszFileName)
  343. {
  344. LogMessage(MSG_ERROR, "Failed to allocate memory for pszFileName for GetStandardMofs.");
  345. delete [] pszWinDir;
  346. return FALSE;
  347. }
  348. StringCchCopyA(pszFileName, fileNameLen, pszWinDir);
  349. StringCchCatA(pszFileName, fileNameLen, "\\inf\\wbemoc.inf");
  350. delete [] pszWinDir;
  351. // verify that inf exists
  352. if (!FileExists(pszFileName))
  353. {
  354. char szTemp[MAX_MSG_TEXT_LENGTH];
  355. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "Failed to locate inf file %s in GetStandardMofs.", pszFileName);
  356. LogMessage(MSG_ERROR, szTemp);
  357. delete [] pszFileName;
  358. return FALSE;
  359. }
  360. // GetPrivateProfileSection doesn't tell how large of a buffer is needed,
  361. // only how many chars it succeeded in copying, so I have to test to see
  362. // if I need to enlarge the buffer and try again
  363. const DWORD INITIAL_BUFFER_SIZE = 700;
  364. const DWORD BUFFER_SIZE_INCREMENT = 100;
  365. DWORD dwSize = INITIAL_BUFFER_SIZE;
  366. char* pszBuffer = new char[dwSize];
  367. if (!pszBuffer)
  368. {
  369. LogMessage(MSG_ERROR, "Failed to allocate memory for pszBuffer for GetStandardMofs.");
  370. delete [] pszFileName;
  371. return FALSE;
  372. }
  373. char* pszAppName = "WBEM.SYSTEMMOFS";
  374. DWORD dwCopied = GetPrivateProfileSection(pszAppName, pszBuffer, dwSize, pszFileName);
  375. // if buffer isn't large enough, it copies dwSize - 2, so test for this
  376. while (dwCopied == (dwSize - 2))
  377. {
  378. delete [] pszBuffer;
  379. dwSize += BUFFER_SIZE_INCREMENT;
  380. pszBuffer = new char[dwSize];
  381. if (!pszBuffer)
  382. {
  383. LogMessage(MSG_ERROR, "Failed to allocate memory for pszBuffer for GetStandardMofs.");
  384. delete [] pszFileName;
  385. return FALSE;
  386. }
  387. dwCopied = GetPrivateProfileSection(pszAppName, pszBuffer, dwSize, pszFileName);
  388. }
  389. delete [] pszFileName;
  390. // now extract all the mofs from the buffer, get the full path, and store in the mof list
  391. char* pszFullName = NULL;
  392. char* psz = pszBuffer;
  393. char* pComment = NULL;
  394. while (psz[0] != '\0')
  395. {
  396. // if a comment is present after the filename, this will cut it off
  397. if (pComment = strchr(psz, ';'))
  398. {
  399. psz = strtok(psz, " \t;"); // there may be leading space or tabs as well as the semicolon
  400. }
  401. pszFullName = GetFullFilename(psz, (InstallType)nCurInstallType);
  402. if (pszFullName)
  403. {
  404. if (nCurInstallType != MUI || strstr(_strupr(pszFullName), ".MFL") != NULL)
  405. {
  406. if (FileExists(pszFullName))
  407. mszSystemMofs.AddUnique(pszFullName);
  408. else
  409. {
  410. char szTemp[MAX_MSG_TEXT_LENGTH];
  411. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "GetStandardMofs failed to locate file %s.", pszFullName);
  412. LogMessage(MSG_ERROR, szTemp);
  413. }
  414. }
  415. delete [] pszFullName;
  416. pszFullName = NULL;
  417. }
  418. else
  419. {
  420. char szTemp[MAX_MSG_TEXT_LENGTH];
  421. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "Failed GetFullFilename for %s with install type = %i in GetStandardMofs.", psz, nCurInstallType);
  422. LogMessage(MSG_ERROR, szTemp);
  423. // do not return false here, keep processing other mofs
  424. }
  425. psz += (strlen(psz) + 1);
  426. if (pComment)
  427. {
  428. // skip over the comment at the end of the line
  429. psz += (strlen(psz) + 1);
  430. pComment = NULL;
  431. }
  432. }
  433. delete [] pszBuffer;
  434. return true;
  435. }
  436. char* GetFullFilename(const char *pszFilename, InstallType eInstallType)
  437. {
  438. char *pszDirectory = NULL;
  439. Registry r(WBEM_REG_WINMGMT);
  440. if (r.GetStatus() != no_error)
  441. {
  442. LogMessage(MSG_ERROR, "Unable to access registry for GetFullFilename.");
  443. return NULL;
  444. }
  445. if (r.GetStr("Working Directory", &pszDirectory))
  446. {
  447. LogMessage(MSG_ERROR, "Unable to retrieve Installation Directory from registry for GetFullFilename.");
  448. return NULL;
  449. }
  450. CString pszPathFilename(pszDirectory);
  451. if (eInstallType == MUI)
  452. {
  453. if (pszPathFilename.Length() && (pszPathFilename[pszPathFilename.Length()-1] != '\\'))
  454. {
  455. pszPathFilename += "\\MUI\\";
  456. pszPathFilename += g_szLangId;
  457. }
  458. }
  459. if (pszPathFilename.Length() && (pszPathFilename[pszPathFilename.Length()-1] != '\\'))
  460. {
  461. pszPathFilename += "\\";
  462. }
  463. pszPathFilename += pszFilename;
  464. delete [] pszDirectory;
  465. return pszPathFilename.Unbind();
  466. }
  467. bool WipeOutAutoRecoveryRegistryEntries()
  468. {
  469. Registry r(WBEM_REG_WINMGMT);
  470. if (r.GetStatus() != no_error)
  471. {
  472. LogMessage(MSG_ERROR, "Unable to access registry for WipeOutAutoRecoveryRegistryEntries.");
  473. return false;
  474. }
  475. else
  476. {
  477. r.SetMultiStr(WBEM_REG_AUTORECOVER, "\0", 2);
  478. r.DeleteEntry(WBEM_REG_AUTORECOVER_EMPTY);
  479. r.DeleteEntry(WBEM_REG_AUTORECOVER_RECOVERED);
  480. return true;
  481. }
  482. }
  483. bool DoesMMFRepositoryExist()
  484. {
  485. Registry r(WBEM_REG_WINMGMT);
  486. if (r.GetStatus() != no_error)
  487. {
  488. LogMessage(MSG_ERROR, "Unable to access registry for DoesMMFRepositoryExist.");
  489. return false;
  490. }
  491. char *pszDbDir = NULL;
  492. if (r.GetStr("Repository Directory", &pszDbDir))
  493. {
  494. LogMessage(MSG_ERROR, "Unable to retrieve Repository Directory from registry for DoesMMFRepositoryExist.");
  495. return false;
  496. }
  497. if (!pszDbDir)
  498. {
  499. LogMessage(MSG_ERROR, "Unable to retrieve Repository Directory from registry for DoesMMFRepositoryExist.");
  500. return false;
  501. }
  502. CString szDbFilename(pszDbDir);
  503. if (szDbFilename.Length() != 0)
  504. szDbFilename += "\\";
  505. szDbFilename += "cim.rep";
  506. delete [] pszDbDir;
  507. return FileExists(szDbFilename);
  508. }
  509. bool DoesFSRepositoryExist()
  510. {
  511. Registry r(WBEM_REG_WINMGMT);
  512. if (r.GetStatus() != no_error)
  513. {
  514. LogMessage(MSG_ERROR, "Unable to access registry for DoesMMFRepositoryExist.");
  515. return false;
  516. }
  517. char *pszDbDir = NULL;
  518. if (r.GetStr("Repository Directory", &pszDbDir))
  519. {
  520. LogMessage(MSG_ERROR, "Unable to retrieve Repository Directory from registry for DoesMMFRepositoryExist.");
  521. return false;
  522. }
  523. if (!pszDbDir || (strlen(pszDbDir) == 0))
  524. {
  525. LogMessage(MSG_ERROR, "Unable to retrieve Repository Directory from registry for DoesMMFRepositoryExist.");
  526. return false;
  527. }
  528. CString szDbFilename1(pszDbDir);
  529. szDbFilename1 += "\\FS\\MainStage.dat";
  530. CString szDbFilename2(pszDbDir);
  531. szDbFilename2 += "\\FS\\LowStage.dat";
  532. delete [] pszDbDir;
  533. return FileExists(szDbFilename1)||FileExists(szDbFilename2);
  534. }
  535. // This function is used to detect an earlier post-MMF repository version and upgrade it
  536. // Returns TRUE if repository upgrade succeeded; FALSE in all other cases
  537. bool UpgradeRepository()
  538. {
  539. LogMessage(MSG_INFO, "Beginning repository upgrade");
  540. bool bRet = false;
  541. IWbemLocator *pLocator = NULL;
  542. HRESULT hr = CoCreateInstance(CLSID_WbemLocator,NULL, CLSCTX_ALL, IID_IWbemLocator,(void**)&pLocator);
  543. if(FAILED(hr))
  544. {
  545. LogMessage(MSG_ERROR, "WMI Repository upgrade failed CoCreateInstance.");
  546. return bRet;
  547. }
  548. IWbemServices *pNamespace = NULL;
  549. BSTR tmpStr = SysAllocString(L"root");
  550. hr = pLocator->ConnectServer(tmpStr, NULL, NULL, NULL, NULL, NULL, NULL, &pNamespace);
  551. if (SUCCEEDED(hr))
  552. {
  553. pNamespace->Release();
  554. LogMessage(MSG_INFO, "WMI Repository upgrade succeeded.");
  555. bRet = true;
  556. }
  557. else
  558. {
  559. if (hr == WBEM_E_DATABASE_VER_MISMATCH)
  560. {
  561. LogMessage(MSG_ERROR, "WMI Repository upgrade failed with WBEM_E_DATABASE_VER_MISMATCH.");
  562. // shut down so we can delete the repository
  563. ShutdownWinMgmt();
  564. // delete the repository so it can be rebuilt
  565. // try multiple times in case winmgmt hasn't shut down yet
  566. int nTry = 20;
  567. while (nTry--)
  568. {
  569. hr = MoveRepository();
  570. if (SUCCEEDED(hr))
  571. {
  572. break;
  573. }
  574. Sleep(500);
  575. }
  576. if (FAILED(hr))
  577. {
  578. LogMessage(MSG_ERROR, "WMI Repository upgrade failed to move repository to backup location.");
  579. }
  580. }
  581. else
  582. {
  583. LogMessage(MSG_ERROR, "WMI Repository upgrade failed ConnectServer.");
  584. }
  585. }
  586. SysFreeString(tmpStr);
  587. pLocator->Release();
  588. LogMessage(MSG_INFO, "Repository upgrade completed.");
  589. return bRet;
  590. }
  591. // This function is used to convert an old MMF repository to the current default repository
  592. bool DoConvertRepository()
  593. {
  594. // get MMF filename
  595. Registry r(WBEM_REG_WINMGMT);
  596. if (r.GetStatus() != no_error)
  597. {
  598. LogMessage(MSG_ERROR, "Unable to access registry for DoConvertRepository.");
  599. return false;
  600. }
  601. char* pszDbDir = NULL;
  602. if (r.GetStr("Repository Directory", &pszDbDir))
  603. {
  604. LogMessage(MSG_ERROR, "Unable to get repository directory from registry for DoConvertRepository");
  605. return false;
  606. }
  607. if (!pszDbDir)
  608. {
  609. LogMessage(MSG_ERROR, "Unable to get repository directory from registry for DoConvertRepository");
  610. return false;
  611. }
  612. CString szDbFilename(pszDbDir);
  613. delete [] pszDbDir;
  614. if (szDbFilename.Length() != 0)
  615. szDbFilename += "\\";
  616. szDbFilename += "cim.rep";
  617. // check that MMF really exists
  618. if (!FileExists(szDbFilename))
  619. {
  620. LogMessage(MSG_ERROR, "MMF Repository does not exist.");
  621. return false;
  622. }
  623. { //Scope so that we delete the g_pDbArena before we try to delete the file
  624. // create arena and load MMF
  625. g_pDbArena = new CMMFArena2();
  626. if (g_pDbArena == 0)
  627. {
  628. LogMessage(MSG_ERROR, "Unable to create CMMFArena2");
  629. return false;
  630. }
  631. CDeleteMe<CMMFArena2> delMe1(g_pDbArena);
  632. if (!g_pDbArena->LoadMMF(szDbFilename) || (g_pDbArena->GetStatus() != no_error))
  633. {
  634. LogMessage(MSG_ERROR, "Error opening existing MMF");
  635. return false;
  636. }
  637. // get export filename
  638. TCHAR *pszFilename = GetFullFilename(WINMGMT_DBCONVERT_NAME);
  639. if (pszFilename == 0)
  640. {
  641. LogMessage(MSG_ERROR, "Unable to get DB name");
  642. return false;
  643. }
  644. CVectorDeleteMe<TCHAR> delMe2(pszFilename);
  645. // determine version of exporter to use
  646. CRepExporter* pExporter = NULL;
  647. DWORD dwVersion = g_pDbArena->GetVersion();
  648. MsgType msgType = MSG_INFO;
  649. char szTemp[MAX_MSG_TEXT_LENGTH];
  650. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "Upgrading repository format. Repository format version detected %lu.", dwVersion);
  651. switch (dwVersion)
  652. {
  653. case INTERNAL_DATABASE_VERSION:
  654. {
  655. pExporter = new CRepExporterV9;
  656. break;
  657. }
  658. case 3: //450 build
  659. {
  660. pExporter = new CRepExporterV1;
  661. break;
  662. }
  663. case 5: //500 series
  664. case 6: //600 series Nova M1
  665. {
  666. pExporter = new CRepExporterV5;
  667. break;
  668. }
  669. case 7: //900 series Nova M3 first attempt!
  670. case 8: //900 series... has null key trees until instance created
  671. {
  672. pExporter = new CRepExporterV7;
  673. break;
  674. }
  675. case 10: //9x version of version 9!
  676. {
  677. pExporter = new CRepExporterV9;
  678. break;
  679. }
  680. default:
  681. {
  682. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "Unsupported repository version detected. Version found = %lu, version expected = %lu.", dwVersion, DWORD(INTERNAL_DATABASE_VERSION));
  683. msgType = MSG_ERROR;
  684. }
  685. }
  686. LogMessage(msgType, szTemp);
  687. // do we have an exporter?
  688. if (!pExporter)
  689. {
  690. LogMessage(MSG_ERROR, "Unable to create exporter object.");
  691. return false;
  692. }
  693. CDeleteMe<CRepExporter> delMe3(pExporter);
  694. // export the old repository
  695. if (pExporter->Export(g_pDbArena, pszFilename) != no_error)
  696. {
  697. LogMessage(MSG_ERROR, "Failed to export old WMI Repository.");
  698. return false;
  699. }
  700. // create new repository and import into it using IWbemServices
  701. CRepImporter import;
  702. if (import.ImportRepository(pszFilename) != no_error)
  703. {
  704. LogMessage(MSG_ERROR, "Failed to import data from old WMI Repository.");
  705. return false;
  706. }
  707. DeleteFile(pszFilename);
  708. }
  709. // conversion was successful, so now delete the old stuff
  710. DeleteMMFRepository();
  711. return true;
  712. }
  713. void DeleteMMFRepository()
  714. {
  715. Registry r(WBEM_REG_WINMGMT);
  716. if (r.GetStatus() != no_error)
  717. {
  718. LogMessage(MSG_ERROR, "Unable to access registry for DeleteMMFRepository.");
  719. return;
  720. }
  721. char* pszDbDir = NULL;
  722. if (r.GetStr("Repository Directory", &pszDbDir))
  723. {
  724. LogMessage(MSG_ERROR, "Unable to get repository directory from registry for DeleteMMFRepository");
  725. return;
  726. }
  727. if (!pszDbDir)
  728. {
  729. LogMessage(MSG_ERROR, "Unable to get repository directory from registry for DeleteMMFRepository");
  730. return;
  731. }
  732. CString szDbFilename(pszDbDir);
  733. if (szDbFilename.Length() != 0)
  734. szDbFilename += "\\";
  735. szDbFilename += "cim.rep";
  736. CString szDbBackup(pszDbDir);
  737. if (szDbBackup.Length() != 0)
  738. szDbBackup += "\\";
  739. szDbBackup += "cim.rec";
  740. CString szDbNewFilename(pszDbDir);
  741. if (szDbNewFilename.Length() != 0)
  742. szDbNewFilename += "\\";
  743. szDbNewFilename += "cim.bak";
  744. delete [] pszDbDir;
  745. DeleteFile(szDbFilename);
  746. DeleteFile(szDbBackup);
  747. DeleteFile(szDbNewFilename);
  748. }
  749. void ShutdownWinMgmt()
  750. {
  751. PROCESS_INFORMATION pi;
  752. STARTUPINFO si;
  753. memset(&si, 0, sizeof(si));
  754. si.cb = sizeof(si);
  755. //Try killing WinMgmt
  756. char *pszFullPath = GetFullFilename("Winmgmt.exe");
  757. if (!pszFullPath)
  758. {
  759. LogMessage(MSG_NTSETUPERROR, "Could not shut down Winmgmt -- failed to get full path to Winmgmt.exe.");
  760. return;
  761. }
  762. size_t cmdLineLen = strlen("Winmgmt /kill") + 1;
  763. char *pszCommandLine = new char[cmdLineLen];
  764. if (!pszCommandLine)
  765. {
  766. LogMessage(MSG_NTSETUPERROR, "Could not shut down Winmgmt -- failed to allocate memory.");
  767. return;
  768. }
  769. StringCchCopyA(pszCommandLine, cmdLineLen, "Winmgmt /kill");
  770. if (CreateProcess(pszFullPath, pszCommandLine, 0, 0, FALSE, 0, 0, 0, &si, &pi))
  771. {
  772. WaitForSingleObject(pi.hProcess, INFINITE);
  773. CloseHandle(pi.hProcess);
  774. CloseHandle(pi.hThread);
  775. Sleep(10000);
  776. }
  777. else
  778. {
  779. LogMessage(MSG_NTSETUPERROR, "Could not shut down Winmgmt -- failed to create process for Winmgmt.exe.");
  780. }
  781. delete [] pszCommandLine;
  782. delete [] pszFullPath;
  783. }
  784. /******************************************************************************
  785. *
  786. * GetRepositoryDirectory
  787. *
  788. * Description:
  789. * Retrieves the location of the repository directory from the registry.
  790. *
  791. * Parameters:
  792. * wszRepositoryDirectory: Array to store location in.
  793. *
  794. * Return:
  795. * HRESULT: WBEM_S_NO_ERROR If successful
  796. * WBEM_E_OUT_OF_MEMORY If out of memory
  797. * WBEM_E_FAILED If anything else failed
  798. *
  799. ******************************************************************************
  800. */
  801. HRESULT GetRepositoryDirectory(wchar_t wszRepositoryDirectory[MAX_PATH+1])
  802. {
  803. HKEY hKey;
  804. long lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  805. L"SOFTWARE\\Microsoft\\WBEM\\CIMOM",
  806. 0, KEY_READ, &hKey);
  807. if(lRes)
  808. return WBEM_E_FAILED;
  809. wchar_t wszTmp[MAX_PATH + 1];
  810. DWORD dwLen = (MAX_PATH + 1)*sizeof(wchar_t);
  811. lRes = RegQueryValueExW(hKey, L"Repository Directory", NULL, NULL,
  812. (LPBYTE)wszTmp, &dwLen);
  813. RegCloseKey(hKey);
  814. if(lRes)
  815. return WBEM_E_FAILED;
  816. if (ExpandEnvironmentStringsW(wszTmp,wszRepositoryDirectory, MAX_PATH + 1) == 0)
  817. return WBEM_E_FAILED;
  818. return WBEM_S_NO_ERROR;
  819. }
  820. HRESULT GetLoggingDirectory(wchar_t wszLoggingDirectory[MAX_PATH+1])
  821. {
  822. HKEY hKey;
  823. long lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
  824. L"SOFTWARE\\Microsoft\\WBEM\\CIMOM",
  825. 0, KEY_READ, &hKey);
  826. if(lRes)
  827. return WBEM_E_FAILED;
  828. wchar_t wszTmp[MAX_PATH + 1];
  829. DWORD dwLen = sizeof(wszTmp);
  830. lRes = RegQueryValueExW(hKey, L"Logging Directory", NULL, NULL,
  831. (LPBYTE)wszTmp, &dwLen);
  832. RegCloseKey(hKey);
  833. if(lRes)
  834. return WBEM_E_FAILED;
  835. if (ExpandEnvironmentStringsW(wszTmp,wszLoggingDirectory, MAX_PATH + 1) == 0)
  836. return WBEM_E_FAILED;
  837. return WBEM_S_NO_ERROR;
  838. }
  839. /******************************************************************************
  840. *
  841. * MoveRepository
  842. *
  843. * Description:
  844. * Move all files and directories under the repository directory
  845. * to a backup location. The repository directory location is retrieved
  846. * from the registry.
  847. *
  848. * Parameters:
  849. * <none>
  850. *
  851. * Return:
  852. * HRESULT: WBEM_S_NO_ERROR If successful
  853. * WBEM_E_OUT_OF_MEMORY If out of memory
  854. * WBEM_E_FAILED If anything else failed
  855. *
  856. ******************************************************************************
  857. */
  858. HRESULT MoveRepository()
  859. {
  860. HRESULT hres = WBEM_S_NO_ERROR;
  861. wchar_t wszRepositoryDirectory[MAX_PATH+1];
  862. wchar_t wszRepositoryMove[MAX_PATH+1];
  863. //Get the root directory of the repository
  864. hres = GetRepositoryDirectory(wszRepositoryDirectory);
  865. if (SUCCEEDED(hres))
  866. {
  867. for (int i=1; i<999; i++)
  868. {
  869. StringCchPrintfW(wszRepositoryMove, MAX_PATH+1, L"%s.%03i", wszRepositoryDirectory, i);
  870. if (GetFileAttributesW(wszRepositoryMove) == 0xFFFFFFFF)
  871. break;
  872. }
  873. if (!MoveFileW(wszRepositoryDirectory, wszRepositoryMove))
  874. hres = WBEM_E_FAILED;
  875. else
  876. {
  877. char szTemp[MAX_MSG_TEXT_LENGTH];
  878. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "wbemupgd.dll: The WMI repository has failed to upgrade. "
  879. "The repository has been backed up to %S and a new one created.",
  880. wszRepositoryMove);
  881. LogMessage(MSG_NTSETUPERROR, szTemp);
  882. }
  883. }
  884. return hres;
  885. }
  886. bool LoadMofList(IWbemContext * pCtx, IWinmgmtMofCompiler * pCompiler, const char *mszMofs, CString &szMOFFailureList, long lOptionFlags, long lClassFlags)
  887. {
  888. LogMessage(MSG_INFO, "Beginning MOF load");
  889. bool bRet = true;
  890. WCHAR wFileName[MAX_PATH+1];
  891. const char *pszMofs = mszMofs;
  892. char szTemp[MAX_MSG_TEXT_LENGTH+1];
  893. WBEM_COMPILE_STATUS_INFO statusInfo;
  894. // get logging directory or default if failed
  895. wchar_t wszMofcompLog[MAX_PATH+1];
  896. HRESULT hres = GetLoggingDirectory(wszMofcompLog);
  897. if (SUCCEEDED(hres))
  898. {
  899. StringCchCatW(wszMofcompLog, MAX_PATH+1, L"mofcomp.log");
  900. }
  901. else
  902. {
  903. StringCchCopyW(wszMofcompLog, MAX_PATH+1, L"<systemroot>\\system32\\wbem\\logs\\mofcomp.log");
  904. }
  905. // process each MOF
  906. while (*pszMofs != '\0')
  907. {
  908. char *szExpandedFilename = NULL;
  909. DWORD nRes = ExpandEnvironmentStrings(pszMofs,NULL,0);
  910. if (nRes == 0)
  911. {
  912. size_t expandedFilenameLen = strlen(pszMofs) + 1;
  913. szExpandedFilename = new char[expandedFilenameLen];
  914. if (szExpandedFilename == NULL)
  915. {
  916. LogMessage(MSG_INFO, "Failed allocating memory for szExpandedFilename - 1.");
  917. bRet = false;
  918. break;
  919. }
  920. StringCchCopyA(szExpandedFilename, expandedFilenameLen, pszMofs);
  921. }
  922. else
  923. {
  924. szExpandedFilename = new char[nRes];
  925. if (szExpandedFilename == NULL)
  926. {
  927. LogMessage(MSG_INFO, "Failed allocating memory for szExpandedFilename - 2.");
  928. bRet = false;
  929. break;
  930. }
  931. nRes = ExpandEnvironmentStrings(pszMofs,szExpandedFilename,nRes);
  932. if (nRes == 0)
  933. {
  934. LogMessage(MSG_INFO, "Failed expanding environment strings.");
  935. delete [] szExpandedFilename;
  936. bRet = false;
  937. break;
  938. }
  939. }
  940. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "Processing %s", szExpandedFilename);
  941. LogMessage(MSG_INFO, szTemp);
  942. //Call MOF Compiler with (pszMofs);
  943. mbstowcs(wFileName, szExpandedFilename, MAX_PATH+1);
  944. SCODE sRet = pCompiler->WinmgmtCompileFile(wFileName,
  945. NULL,
  946. lOptionFlags,
  947. lClassFlags,
  948. 0,
  949. NULL, pCtx, &statusInfo);
  950. if (sRet != S_OK)
  951. {
  952. //This MOF failed to load.
  953. if (szMOFFailureList.Length())
  954. szMOFFailureList += "\n";
  955. szMOFFailureList += szExpandedFilename;
  956. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "An error occurred while compiling the following MOF file: %s "
  957. "Please refer to %S for more detailed information.",
  958. szExpandedFilename, wszMofcompLog);
  959. LogMessage(MSG_NTSETUPERROR, szTemp);
  960. bRet = false;
  961. }
  962. delete [] szExpandedFilename;
  963. //Move on to the next string
  964. pszMofs += strlen(pszMofs) + 1;
  965. } // end while
  966. LogMessage(MSG_INFO, "MOF load completed.");
  967. return bRet;
  968. }
  969. bool WriteBackAutoRecoveryMofs(CMultiString &mszSystemMofs, CMultiString &mszExternalMofList)
  970. {
  971. CMultiString mszNewList;
  972. CopyMultiString(mszSystemMofs, mszNewList);
  973. CopyMultiString(mszExternalMofList, mszNewList);
  974. Registry r(WBEM_REG_WINMGMT);
  975. if (r.GetStatus() != no_error)
  976. {
  977. LogMessage(MSG_ERROR, "Unable to access registry for WriteBackAutoRecoverMofs.");
  978. return false;
  979. }
  980. r.SetMultiStr(WBEM_REG_AUTORECOVER, mszNewList, mszNewList.Length() + 1);
  981. return true;
  982. }
  983. void LogMessage(MsgType msgType, const char *pszMessage)
  984. {
  985. //Load messages from the resource
  986. char pszSetupMessage[10];
  987. switch (msgType)
  988. {
  989. case MSG_NTSETUPERROR:
  990. LogSetupError(pszMessage);
  991. // now fall through to next case
  992. case MSG_ERROR:
  993. StringCchCopyA(pszSetupMessage, 10, "ERROR: ");
  994. break;
  995. case MSG_WARNING:
  996. StringCchCopyA(pszSetupMessage, 10, "WARNING: ");
  997. break;
  998. case MSG_INFO:
  999. default:
  1000. StringCchCopyA(pszSetupMessage, 10, "");
  1001. break;
  1002. }
  1003. size_t newMessageLen = strlen(pszMessage) + 1;
  1004. char* pszNewMessage = new char[newMessageLen];
  1005. if (!pszNewMessage)
  1006. {
  1007. // we failed to allocate memory for the message, so no logging :(
  1008. return;
  1009. }
  1010. StringCchCopyA(pszNewMessage, newMessageLen, pszMessage);
  1011. // get log file path and name
  1012. Registry r(WBEM_REG_WINMGMT);
  1013. if (r.GetStatus() != no_error)
  1014. {
  1015. // no messages will be logged because we don't know where to write the log :(
  1016. delete [] pszNewMessage;
  1017. return;
  1018. }
  1019. char* pszFullDirectory = NULL;
  1020. if (r.GetStr("Logging Directory", &pszFullDirectory))
  1021. {
  1022. // no messages will be logged because we don't know where to write the log :(
  1023. delete [] pszNewMessage;
  1024. return;
  1025. }
  1026. if (!pszFullDirectory)
  1027. {
  1028. // no messages will be logged because we don't know where to write the log :(
  1029. delete [] pszNewMessage;
  1030. return;
  1031. }
  1032. char* pszFilename = "setup.log";
  1033. size_t fullPathLen = strlen(pszFullDirectory) + strlen("\\") + strlen(pszFilename) + 1;
  1034. char* pszFullPath = new char [fullPathLen];
  1035. if (!pszFullPath)
  1036. {
  1037. // we failed to allocate memory for the path, so no logging :(
  1038. delete [] pszNewMessage;
  1039. return;
  1040. }
  1041. StringCchCopyA(pszFullPath, fullPathLen, pszFullDirectory);
  1042. StringCchCatA(pszFullPath, fullPathLen, "\\");
  1043. StringCchCatA(pszFullPath, fullPathLen, pszFilename);
  1044. delete [] pszFullDirectory;
  1045. // Get time
  1046. char timebuf[64];
  1047. time_t now = time(0);
  1048. struct tm *local = localtime(&now);
  1049. if(local)
  1050. {
  1051. StringCchCopyA(timebuf, 64, asctime(local));
  1052. timebuf[strlen(timebuf) - 1] = 0;
  1053. }
  1054. else
  1055. StringCchCopyA(timebuf,64, "unknown time");
  1056. size_t timeLen = strlen(timebuf) + strlen("(): ") + 1;
  1057. char* pszTime = new char [timeLen];
  1058. if (!pszTime)
  1059. {
  1060. // we failed to allocate memory for the time, so no logging :(
  1061. delete [] pszNewMessage;
  1062. delete [] pszFullPath;
  1063. return;
  1064. }
  1065. StringCchCopyA(pszTime, timeLen, "(");
  1066. StringCchCatA(pszTime, timeLen, timebuf);
  1067. StringCchCatA(pszTime, timeLen, "): ");
  1068. // write messages to log file
  1069. HANDLE hFile = CreateFile(pszFullPath, GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  1070. if (hFile != INVALID_HANDLE_VALUE)
  1071. {
  1072. const char* pszCR = "\r\n";
  1073. char* psz;
  1074. DWORD dwWritten;
  1075. SetFilePointer(hFile, 0, 0, FILE_END);
  1076. psz = strtok(pszNewMessage, "\n");
  1077. while (psz)
  1078. {
  1079. WriteFile(hFile, pszTime, strlen(pszTime), &dwWritten, 0);
  1080. WriteFile(hFile, pszSetupMessage, strlen(pszSetupMessage), &dwWritten, 0);
  1081. WriteFile(hFile, psz, strlen(psz), &dwWritten, 0);
  1082. WriteFile(hFile, pszCR, strlen(pszCR), &dwWritten, 0);
  1083. psz = strtok(NULL, "\n");
  1084. }
  1085. CloseHandle(hFile);
  1086. }
  1087. delete [] pszNewMessage;
  1088. delete [] pszFullPath;
  1089. delete [] pszTime;
  1090. }
  1091. void LogSetupError(const char *pszMessage)
  1092. {
  1093. size_t tempLen = strlen(pszMessage) + 1;
  1094. char* pszTemp = new char[tempLen];
  1095. if (!pszTemp)
  1096. {
  1097. // we failed to allocate memory for the message, so no logging :(
  1098. return;
  1099. }
  1100. StringCchCopyA(pszTemp, tempLen, pszMessage);
  1101. char* psz;
  1102. char* pszMessageLine;
  1103. const char* pszCR = "\r\n";
  1104. psz = strtok(pszTemp, "\n");
  1105. while (psz)
  1106. {
  1107. size_t messageLineLen = strlen(psz) + strlen(pszCR) + 1;
  1108. pszMessageLine = new char[messageLineLen];
  1109. if (!pszMessageLine)
  1110. {
  1111. delete [] pszTemp;
  1112. return;
  1113. }
  1114. StringCchCopyA(pszMessageLine, messageLineLen, psz);
  1115. StringCchCatA(pszMessageLine, messageLineLen, pszCR);
  1116. SetupLogError(pszMessageLine, LogSevError);
  1117. delete [] pszMessageLine;
  1118. psz = strtok(NULL, "\n");
  1119. }
  1120. delete [] pszTemp;
  1121. }
  1122. void ClearWMISetupRegValue()
  1123. {
  1124. Registry r(WBEM_REG_WINMGMT);
  1125. if (r.GetStatus() == no_error)
  1126. r.SetStr("WMISetup", "0");
  1127. else
  1128. LogMessage(MSG_NTSETUPERROR, "Unable to clear WMI setup reg value.");
  1129. r.DeleteEntry("KnownSvcs");
  1130. }
  1131. void SetWBEMBuildRegValue()
  1132. {
  1133. Registry r(WBEM_REG_WBEM);
  1134. if (r.GetStatus() != no_error)
  1135. {
  1136. LogMessage(MSG_NTSETUPERROR, "Unable to set WBEM build reg value.");
  1137. return;
  1138. }
  1139. char* pszBuildNo = new char[10];
  1140. OSVERSIONINFO os;
  1141. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1142. if(GetVersionEx(&os))
  1143. {
  1144. StringCchPrintfA(pszBuildNo, 10, "%lu.0000", os.dwBuildNumber);
  1145. }
  1146. r.SetStr("Build", pszBuildNo);
  1147. delete [] pszBuildNo;
  1148. }
  1149. void RecordFileVersion()
  1150. {
  1151. DWORD dwHandle;
  1152. DWORD dwLen = GetFileVersionInfoSizeW(L"wbemupgd.dll", &dwHandle);
  1153. if (dwLen)
  1154. {
  1155. BYTE* lpData = new BYTE[dwLen];
  1156. if (lpData)
  1157. {
  1158. if (GetFileVersionInfoW(L"wbemupgd.dll", dwHandle, dwLen, lpData))
  1159. {
  1160. struct LANGANDCODEPAGE {
  1161. WORD wLanguage;
  1162. WORD wCodePage;
  1163. } *lpTranslate;
  1164. UINT cbTranslate;
  1165. if (VerQueryValueW(lpData, L"\\VarFileInfo\\Translation", (LPVOID*)&lpTranslate, &cbTranslate))
  1166. {
  1167. wchar_t* pswzSubBlock = new wchar_t[dwLen];
  1168. wchar_t* pwszFileVersion = NULL;
  1169. UINT cbBytes;
  1170. for(UINT i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++)
  1171. {
  1172. StringCchPrintfW(pswzSubBlock, dwLen, L"\\StringFileInfo\\%04x%04x\\FileVersion", lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
  1173. // Retrieve file description for language and code page "i".
  1174. if (VerQueryValueW(lpData, pswzSubBlock, (LPVOID*)&pwszFileVersion, &cbBytes))
  1175. {
  1176. if (cbBytes)
  1177. {
  1178. wchar_t wszTemp[MAX_MSG_TEXT_LENGTH];
  1179. StringCchPrintfW(wszTemp, MAX_MSG_TEXT_LENGTH, L"Current build of wbemupgd.dll is %s", pwszFileVersion);
  1180. // once LogMessage is updated to handle wchars, this conversion can be removed
  1181. char* szTemp = new char[MAX_MSG_TEXT_LENGTH+1];
  1182. if (szTemp)
  1183. {
  1184. wcstombs(szTemp, wszTemp, MAX_MSG_TEXT_LENGTH+1);
  1185. LogMessage(MSG_INFO, szTemp);
  1186. delete [] szTemp;
  1187. }
  1188. }
  1189. }
  1190. }
  1191. delete [] pswzSubBlock;
  1192. }
  1193. }
  1194. delete [] lpData;
  1195. }
  1196. }
  1197. }
  1198. void CallEscapeRouteBeforeMofCompilation()
  1199. {
  1200. HMODULE hDll = NULL;
  1201. ESCDOOR_BEFORE_MOF_COMPILATION pfnEscRouteBeforeMofCompilation;
  1202. char *pszFullPath = GetFullFilename("WmiEscpe.dll");
  1203. if (!pszFullPath)
  1204. return;
  1205. hDll = LoadLibrary(pszFullPath);
  1206. delete[] pszFullPath;
  1207. if(hDll == NULL)
  1208. {
  1209. return;
  1210. }
  1211. pfnEscRouteBeforeMofCompilation =
  1212. (ESCDOOR_BEFORE_MOF_COMPILATION)GetProcAddress((HMODULE)hDll, "EscRouteBeforeMofCompilation");
  1213. if (pfnEscRouteBeforeMofCompilation == NULL)
  1214. {
  1215. if(hDll != NULL)
  1216. FreeLibrary(hDll);
  1217. return;
  1218. }
  1219. pfnEscRouteBeforeMofCompilation();
  1220. if(hDll != NULL)
  1221. FreeLibrary(hDll);
  1222. }
  1223. void CallEscapeRouteAfterMofCompilation()
  1224. {
  1225. HMODULE hDll = NULL;
  1226. ESCDOOR_AFTER_MOF_COMPILATION pfnEscRouteAfterMofCompilation;
  1227. char *pszFullPath = GetFullFilename("WmiEscpe.dll");
  1228. if (!pszFullPath)
  1229. return;
  1230. hDll = LoadLibrary(pszFullPath);
  1231. delete[] pszFullPath;
  1232. if(hDll == NULL)
  1233. {
  1234. return;
  1235. }
  1236. pfnEscRouteAfterMofCompilation =
  1237. (ESCDOOR_AFTER_MOF_COMPILATION)GetProcAddress((HMODULE)hDll, "EscRouteAfterMofCompilation");
  1238. if (pfnEscRouteAfterMofCompilation == NULL)
  1239. {
  1240. if(hDll != NULL)
  1241. FreeLibrary(hDll);
  1242. return;
  1243. }
  1244. pfnEscRouteAfterMofCompilation();
  1245. if(hDll != NULL)
  1246. FreeLibrary(hDll);
  1247. }
  1248. bool DoMofLoad(wchar_t* pComponentName, CMultiString& mszSystemMofs)
  1249. {
  1250. bool bRet = true;
  1251. bool bMofLoadFailure = false;
  1252. CString szFailedSystemMofs;
  1253. IWbemContext * pCtx = NULL;
  1254. HRESULT hRes = CoCreateInstance(CLSID_WbemContext, 0, CLSCTX_INPROC_SERVER,
  1255. IID_IWbemContext, (LPVOID *) &pCtx);
  1256. if (FAILED(hRes))
  1257. {
  1258. LogMessage(MSG_ERROR, "Unable to create CLSID_WbemContext.");
  1259. return false;
  1260. }
  1261. OnDelete<IUnknown *,void(*)(IUnknown *),RM> rmCtx(pCtx);
  1262. _variant_t Var = true;
  1263. if (FAILED(hRes = pCtx->SetValue(L"__MOFD_DO_STORE",0,&Var))) return false;
  1264. IWinmgmtMofCompiler * pCompiler = NULL;
  1265. SCODE sc = CoCreateInstance(CLSID_WinmgmtMofCompiler, 0, CLSCTX_INPROC_SERVER, IID_IWinmgmtMofCompiler, (LPVOID *) &pCompiler);
  1266. if(SUCCEEDED(sc))
  1267. {
  1268. bRet = LoadMofList(pCtx,pCompiler, mszSystemMofs, szFailedSystemMofs, WBEM_FLAG_CONNECT_REPOSITORY_ONLY, 0);
  1269. if (bRet == false)
  1270. bMofLoadFailure = true;
  1271. pCompiler->Release();
  1272. }
  1273. else
  1274. {
  1275. bRet = false;
  1276. }
  1277. if (szFailedSystemMofs.Length())
  1278. {
  1279. char szTemp[MAX_MSG_TEXT_LENGTH];
  1280. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "The following %S file(s) failed to load:", pComponentName);
  1281. LogMessage(MSG_ERROR, szTemp);
  1282. LogMessage(MSG_ERROR, szFailedSystemMofs);
  1283. }
  1284. else if (bMofLoadFailure)
  1285. {
  1286. char szTemp[MAX_MSG_TEXT_LENGTH];
  1287. StringCchPrintfA(szTemp, MAX_MSG_TEXT_LENGTH, "None of the %S files could be loaded.", pComponentName);
  1288. LogMessage(MSG_ERROR, szTemp);
  1289. }
  1290. else if (bRet == false)
  1291. {
  1292. LogMessage(MSG_ERROR, "No MOFs could be loaded because the MOF Compiler failed to intialize.");
  1293. }
  1294. return bRet;
  1295. }
  1296. // this call back is needed by the wdmlib functions called by DoWDMProviderInit()
  1297. void WINAPI EventCallbackRoutine(PWNODE_HEADER WnodeHeader, ULONG_PTR Context)
  1298. {
  1299. return;
  1300. }
  1301. bool DoWDMNamespaceInit()
  1302. {
  1303. LogMessage(MSG_INFO, "Beginning WMI(WDM) Namespace Init");
  1304. bool bRet = FALSE;
  1305. IWbemLocator *pLocator = NULL;
  1306. HRESULT hr = CoCreateInstance(CLSID_WbemAdministrativeLocator,NULL, CLSCTX_ALL, IID_IWbemLocator,(void**)&pLocator);
  1307. if(SUCCEEDED(hr))
  1308. {
  1309. BSTR tmpStr = SysAllocString(L"root\\wmi");
  1310. IWbemServices* pNamespace = NULL;
  1311. hr = pLocator->ConnectServer(tmpStr, NULL, NULL, NULL, WBEM_FLAG_CONNECT_PROVIDERS, NULL, NULL, &pNamespace);
  1312. if (SUCCEEDED(hr))
  1313. {
  1314. CHandleMap HandleMap;
  1315. CWMIBinMof Mof;
  1316. if( SUCCEEDED( Mof.Initialize(&HandleMap, TRUE, WMIGUID_EXECUTE|WMIGUID_QUERY, pNamespace, pNamespace, NULL, NULL)))
  1317. {
  1318. Mof.ProcessListOfWMIBinaryMofsFromWMI();
  1319. }
  1320. pNamespace->Release();
  1321. bRet = TRUE;
  1322. }
  1323. SysFreeString(tmpStr);
  1324. pLocator->Release();
  1325. }
  1326. if (bRet)
  1327. LogMessage(MSG_INFO, "WMI(WDM) Namespace Init Completed");
  1328. else
  1329. LogMessage(MSG_NTSETUPERROR, "WMI(WDM) Namespace Init Failed");
  1330. return bRet;
  1331. }
  1332. bool EnableESS()
  1333. {
  1334. CPersistentConfig cfg;
  1335. bool bRet1 = (cfg.SetPersistentCfgValue(PERSIST_CFGVAL_CORE_ESS_NEEDS_LOADING, 1) != 0);
  1336. bool bRet2 = (cfg.SetPersistentCfgValue(PERSIST_CFGVAL_CORE_ESS_TO_BE_INITIALIZED,1) != 0);
  1337. if (bRet1 && bRet2)
  1338. LogMessage(MSG_INFO, "ESS enabled");
  1339. else
  1340. LogMessage(MSG_ERROR, "Failed to enable ESS");
  1341. return (bRet1 && bRet2);
  1342. }
  1343. #ifdef _X86_
  1344. bool RemoveOldODBC()
  1345. {
  1346. bool bRet = true;
  1347. bool bDoUninstall = false;
  1348. WCHAR strBuff[MAX_PATH + 30];
  1349. DWORD dwSize = GetWindowsDirectoryW((LPWSTR) &strBuff, MAX_PATH);
  1350. if ((dwSize > 1) && (dwSize < MAX_PATH) && (strBuff[dwSize] == L'\0'))
  1351. {
  1352. //can be c:\ or c:\windows
  1353. if (strBuff[dwSize - 1] != L'\\')
  1354. {
  1355. StringCchCatW(strBuff, MAX_PATH+1, L"\\system32\\wbemdr32.dll");
  1356. //we want dwSize to include the slash (may be used later)...
  1357. dwSize++;
  1358. }
  1359. else
  1360. {
  1361. StringCchCatW(strBuff, MAX_PATH+1, L"system32\\wbemdr32.dll");
  1362. }
  1363. DWORD dwDummy = 0;
  1364. DWORD dwInfSize = GetFileVersionInfoSizeW(strBuff, &dwDummy);
  1365. if (dwInfSize > 0)
  1366. {
  1367. BYTE *verBuff = new BYTE[dwInfSize];
  1368. if (verBuff)
  1369. {
  1370. if (GetFileVersionInfoW(strBuff, 0, dwInfSize, (LPVOID)verBuff))
  1371. {
  1372. VS_FIXEDFILEINFO *verInfo = NULL;
  1373. UINT uVerInfoSize = 0;
  1374. if (VerQueryValueW((const LPVOID)verBuff, L"\\", (LPVOID *)&verInfo, &uVerInfoSize) &&
  1375. (uVerInfoSize == sizeof(VS_FIXEDFILEINFO)))
  1376. {
  1377. if (0x043D0000 > verInfo->dwFileVersionLS) //1085 = 43D
  1378. {
  1379. bDoUninstall = true;
  1380. LogMessage(MSG_INFO, "Detected incompatible WBEM ODBC - removing");
  1381. if (!DeleteFileW(strBuff))
  1382. {
  1383. if (!MoveFileExW(strBuff, NULL, MOVEFILE_DELAY_UNTIL_REBOOT))
  1384. {
  1385. bRet = false;
  1386. LogMessage(MSG_INFO, "Failed to delete <system32>\\wbemdr32.dll");
  1387. }
  1388. else
  1389. {
  1390. LogMessage(MSG_INFO, "Will delete <system32>\\wbemdr32.dll on next reboot");
  1391. }
  1392. }
  1393. }
  1394. }
  1395. else
  1396. {
  1397. GetLastError();
  1398. LogMessage(MSG_INFO, "Failed to read ODBC Driver version info from resource buffer");
  1399. bRet = false;
  1400. }
  1401. }
  1402. else
  1403. {
  1404. GetLastError();
  1405. LogMessage(MSG_INFO, "Failed to get ODBC Driver version info");
  1406. bRet = false;
  1407. }
  1408. delete [] verBuff;
  1409. verBuff = NULL;
  1410. }
  1411. else
  1412. {
  1413. bRet = false;
  1414. }
  1415. }
  1416. else
  1417. {
  1418. dwDummy = GetLastError();
  1419. if ((ERROR_FILE_NOT_FOUND != dwDummy) && // for some reason, the GetFileVersionInfoW function seems to be
  1420. (ERROR_RESOURCE_DATA_NOT_FOUND != dwDummy) && // returning ERROR_RESOURCE_DATA_NOT_FOUND instead of ERROR_FILE_NOT_FOUND
  1421. (ERROR_SUCCESS != dwDummy)) // when the file isn't present, so check against this value as well
  1422. {
  1423. LogMessage(MSG_INFO, "Failed to get ODBC Driver version size info");
  1424. bRet = false;
  1425. }
  1426. else
  1427. {
  1428. //the driver isn't present clean up anything lying around
  1429. LogMessage(MSG_INFO, "ODBC Driver <system32>\\wbemdr32.dll not present");
  1430. bDoUninstall = true;
  1431. }
  1432. }
  1433. }
  1434. else
  1435. {
  1436. bRet = false;
  1437. }
  1438. if (bDoUninstall)
  1439. {
  1440. //
  1441. //delete files and registry entries
  1442. //leave ini entries as they were not added by us but by ODBC Mgr
  1443. //
  1444. strBuff[dwSize] = L'\0';
  1445. StringCchCatW(strBuff, MAX_PATH + 30, L"system32\\wbem\\wbemdr32.chm");
  1446. if (!DeleteFileW(strBuff))
  1447. {
  1448. if (ERROR_FILE_NOT_FOUND != GetLastError())
  1449. {
  1450. if (!MoveFileExW(strBuff, NULL, MOVEFILE_DELAY_UNTIL_REBOOT))
  1451. {
  1452. bRet = false;
  1453. LogMessage(MSG_INFO, "Failed to delete <system32>\\wbem\\wbemdr32.chm");
  1454. }
  1455. else
  1456. {
  1457. LogMessage(MSG_INFO, "Will delete <system32>\\wbem\\wbemdr32.chm on next reboot");
  1458. }
  1459. }
  1460. }
  1461. strBuff[dwSize] = L'\0';
  1462. StringCchCatW(strBuff, MAX_PATH + 30, L"help\\wbemdr32.chm");
  1463. if (!DeleteFileW(strBuff))
  1464. {
  1465. if (ERROR_FILE_NOT_FOUND != GetLastError())
  1466. {
  1467. if (!MoveFileExW(strBuff, NULL, MOVEFILE_DELAY_UNTIL_REBOOT))
  1468. {
  1469. bRet = false;
  1470. LogMessage(MSG_INFO, "Failed to delete <windir>\\help\\wbemdr32.chm");
  1471. }
  1472. else
  1473. {
  1474. LogMessage(MSG_INFO, "Will delete <windir>\\help\\wbemdr32.chm on next reboot");
  1475. }
  1476. }
  1477. }
  1478. LONG lErr = RegDeleteKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\ODBC\\ODBC.INI\\WBEM Source");
  1479. if ((ERROR_SUCCESS != lErr) && (ERROR_FILE_NOT_FOUND != lErr))
  1480. {
  1481. LogMessage(MSG_INFO, "Failed to delete registry key: SSoftware\\Microsoft\\ODBC\\ODBC.INI\\WBEM Source");
  1482. bRet = false;
  1483. }
  1484. lErr = RegDeleteKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\ODBC\\ODBCINST.INI\\WBEM ODBC Driver");
  1485. if ((ERROR_SUCCESS != lErr) && (ERROR_FILE_NOT_FOUND != lErr))
  1486. {
  1487. LogMessage(MSG_INFO, "Failed to delete registry key: Software\\Microsoft\\ODBC\\ODBCINST.INI\\WBEM ODBC Driver");
  1488. bRet = false;
  1489. }
  1490. Registry regODBC1("Software\\Microsoft\\ODBC\\ODBC.INI\\ODBC Data Sources");
  1491. if (regODBC1.GetStatus() == no_error)
  1492. {
  1493. if (no_error != regODBC1.DeleteEntry("WBEM Source"))
  1494. {
  1495. if (ERROR_FILE_NOT_FOUND != regODBC1.GetLastError())
  1496. {
  1497. LogMessage(MSG_INFO, "Failed to delete registry value: Software\\Microsoft\\ODBC\\ODBC.INI\\ODBC Data Sources|WBEM Source");
  1498. bRet = false;
  1499. }
  1500. }
  1501. }
  1502. else
  1503. {
  1504. bRet = false;
  1505. }
  1506. Registry regODBC2("Software\\Microsoft\\ODBC\\ODBCINST.INI\\ODBC Drivers");
  1507. if (regODBC2.GetStatus() == no_error)
  1508. {
  1509. if (no_error != regODBC2.DeleteEntry("WBEM ODBC Driver"))
  1510. {
  1511. if (ERROR_FILE_NOT_FOUND != regODBC2.GetLastError())
  1512. {
  1513. LogMessage(MSG_INFO, "Failed to delete registry value: Software\\Microsoft\\ODBC\\ODBCINST.INI\\ODBC Drivers|WBEM ODBC Driver");
  1514. bRet = false;
  1515. }
  1516. }
  1517. }
  1518. else
  1519. {
  1520. bRet = false;
  1521. }
  1522. }
  1523. if (!bRet)
  1524. {
  1525. LogMessage(MSG_ERROR, "A failure in verifying or removing currently installed version of WBEM ODBC.");
  1526. }
  1527. else
  1528. {
  1529. LogMessage(MSG_INFO, "Successfully verified WBEM OBDC adapter (incompatible version removed if it was detected).");
  1530. }
  1531. return bRet;
  1532. }
  1533. #endif