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.

1101 lines
28 KiB

  1. // Copyright (c) 1999 Microsoft Corporation
  2. #include "precomp.h"
  3. #include "BackupPage.h"
  4. #include <chstring1.h>
  5. #include <util.h>
  6. #include "resource.h"
  7. #include "wbemerror.h"
  8. #include <process.h>
  9. #include "WMIHelp.h"
  10. #include "ShlWapi.h"
  11. #include <cominit.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #define REC_WILDCARD _T("*.rec")
  15. #define ALL_WILDCARD _T("*.*")
  16. const static DWORD buPageHelpIDs[] = { // Context Help IDs
  17. IDC_BACKUP_ENABLED, IDH_WMI_CTRL_BACKUP_AUTOMATIC_CHECKBOX,
  18. IDC_BACKUPINTERVAL, IDH_WMI_CTRL_BACKUP_TIME,
  19. IDC_BACKUP_UNITS, IDH_WMI_CTRL_BACKUP_MINUTES_HOURS,
  20. IDC_LASTBACKUP_LABEL, IDH_WMI_CTRL_BACKUP_LAST,
  21. IDC_LASTBACKUP, IDH_WMI_CTRL_BACKUP_LAST,
  22. IDC_RESTORE_FM_AUTO, IDH_WMI_CTRL_BACKUP_RESTORE_AUTO,
  23. IDC_BACKUP_BTN, IDH_WMI_CTRL_BACKUP_BACKUP_MANUAL,
  24. IDC_RESTORE_BTN, IDH_WMI_CTRL_BACKUP_RESTORE_MANUAL,
  25. IDC_DBDIRECTORY_LABEL, IDH_WMI_CTRL_ADVANCED_REPOSITORY_LOC,
  26. IDC_DB_DIR, IDH_WMI_CTRL_ADVANCED_REPOSITORY_LOC,
  27. IDC_ADV_NOW_TEXT, -1,
  28. IDC_ADV_NOW_TEXT2, -1,
  29. 65535, -1,
  30. 0, 0
  31. };
  32. const double WIN2K_CORE_VERSION = 1085.0005; //Win2K Core Version
  33. CBackupPage::CBackupPage(DataSource *ds, bool htmlSupport):
  34. CUIHelpers(ds, &(ds->m_rootThread), htmlSupport),
  35. m_AVIbox(NULL),
  36. m_wszArgs(NULL),
  37. m_cred(NULL),
  38. m_bWhistlerCore(false),
  39. m_hAccessToken(NULL),
  40. m_fClearToken(false),
  41. m_cloak(false)
  42. {
  43. memset(&m_luid,0,sizeof(LUID));
  44. }
  45. CBackupPage::~CBackupPage(void)
  46. {
  47. }
  48. //-------------------------------------------------------------------------
  49. void CBackupPage::InitDlg(HWND hDlg)
  50. {
  51. m_hDlg = hDlg;
  52. ::SendMessage(GetDlgItem(hDlg, IDC_BACKUPINTERVAL),
  53. EM_LIMITTEXT, 3, 0);
  54. HWND hWnd = GetDlgItem(hDlg, IDC_BACKUP_UNITS);
  55. if(hWnd)
  56. {
  57. CHString1 str;
  58. str.LoadString(IDS_MINUTES);
  59. ComboBox_AddString(hWnd, str);
  60. str.LoadString(IDS_HOURS);
  61. ComboBox_AddString(hWnd, str);
  62. }
  63. if(m_DS)
  64. {
  65. USES_CONVERSION;
  66. CHString1 strVersion = _T("0.0");
  67. m_DS->GetBldNbr(strVersion);
  68. double ver = _tstof(strVersion);
  69. if(ver > WIN2K_CORE_VERSION)
  70. {
  71. HideAutomaticBackupControls(hDlg);
  72. }
  73. }
  74. }
  75. //---------------------------------------------------------------------------
  76. // NOTE: This must match to order of the combobox.
  77. #define UNIT_MINUTE 0
  78. #define UNIT_HOUR 1
  79. #define UNIT_DAY 2
  80. #define MINS_IN_HOUR 60
  81. #define MINS_IN_DAY 1440
  82. #define DISABLE_BACKUP -1
  83. void CBackupPage::SetInterval(HWND hDlg, UINT minutes)
  84. {
  85. int m_CBIdx = UNIT_MINUTE;
  86. UINT value = minutes;
  87. if((minutes % MINS_IN_HOUR) == 0)
  88. {
  89. m_CBIdx = UNIT_HOUR;
  90. value = minutes / MINS_IN_HOUR;
  91. }
  92. else if((minutes % MINS_IN_DAY) == 0)
  93. {
  94. m_CBIdx = UNIT_DAY;
  95. value = minutes / MINS_IN_DAY;
  96. }
  97. CHString1 temp;
  98. temp.Format(_T("%d"), value);
  99. SetWindowText(GetDlgItem(hDlg, IDC_BACKUPINTERVAL), temp);
  100. ComboBox_SetCurSel(GetDlgItem(hDlg, IDC_BACKUP_UNITS), m_CBIdx);
  101. }
  102. //---------------------------------------------------------------------------
  103. bool CBackupPage::GetInterval(HWND hDlg, UINT &iValue, bool &valid)
  104. {
  105. int idx = ComboBox_GetCurSel(GetDlgItem(hDlg, IDC_BACKUP_UNITS));
  106. TCHAR value[4] = {0};
  107. valid = false;
  108. iValue = 0;
  109. ::GetWindowText(GetDlgItem(hDlg, IDC_BACKUPINTERVAL), value, 4);
  110. iValue = _ttoi(value);
  111. // scale to minutes based on the combo box.
  112. switch(idx)
  113. {
  114. case UNIT_HOUR:
  115. iValue *= MINS_IN_HOUR;
  116. break;
  117. case UNIT_DAY:
  118. iValue *= MINS_IN_DAY;
  119. break;
  120. }
  121. if((iValue == 0) ||
  122. ((iValue >= 5) && (iValue <= 1440)))
  123. {
  124. valid = true;
  125. }
  126. else
  127. {
  128. CHString1 caption, threat;
  129. caption.LoadString(IDS_SHORT_NAME);
  130. threat.LoadString(IDS_BAD_INTERVAL);
  131. MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  132. MB_OK|MB_ICONEXCLAMATION);
  133. }
  134. return (m_CBIdx != idx);
  135. }
  136. //---------------------------------------------------------------------------
  137. void CBackupPage::Refresh(HWND hDlg, bool force)
  138. {
  139. if(force ||
  140. (m_DS && m_DS->IsNewConnection(&m_sessionID)))
  141. {
  142. CHString1 temp;
  143. UINT iTemp = 0;
  144. CHString1 szNotRemoteable, szUnavailable;
  145. HRESULT hr = S_OK;
  146. BOOL enable = TRUE;
  147. szNotRemoteable.LoadString(IDS_NOT_REMOTEABLE);
  148. szUnavailable.LoadString(IDS_UNAVAILABLE);
  149. PageChanged(PB_BACKUP, false);
  150. // - - - - - - - - - - - - - -
  151. // Interval:
  152. iTemp = 0;
  153. hr = m_DS->GetBackupInterval(iTemp);
  154. if(SUCCEEDED(hr))
  155. {
  156. if(iTemp == 0)
  157. {
  158. Button_SetCheck(GetDlgItem(hDlg, IDC_BACKUP_ENABLED), BST_UNCHECKED);
  159. SetWindowText(GetDlgItem(hDlg, IDC_BACKUPINTERVAL), _T("0"));
  160. ComboBox_SetCurSel(GetDlgItem(hDlg, IDC_BACKUP_UNITS), -1);
  161. enable = FALSE;
  162. }
  163. else
  164. {
  165. Button_SetCheck(GetDlgItem(hDlg, IDC_BACKUP_ENABLED), BST_CHECKED);
  166. SetInterval(hDlg, iTemp);
  167. enable = TRUE;
  168. }
  169. }
  170. else //failed
  171. {
  172. enable = FALSE;
  173. SetWindowText(GetDlgItem(hDlg, IDC_BACKUPINTERVAL),
  174. szUnavailable);
  175. ::EnableWindow(GetDlgItem(hDlg, IDC_BACKUP_ENABLED), enable);
  176. }
  177. ::EnableWindow(GetDlgItem(hDlg, IDC_BACKUPINTERVAL), enable);
  178. ::EnableWindow(GetDlgItem(hDlg, IDC_BACKUP_UNITS), enable);
  179. // - - - - - - - - - - - - - -
  180. // DB dir:
  181. HWND hWnd = GetDlgItem(hDlg, IDC_DB_DIR);
  182. if(hWnd)
  183. {
  184. CHString1 dir;
  185. hr = m_DS->GetDBLocation(dir);
  186. if(SUCCEEDED(hr))
  187. {
  188. enable = TRUE;
  189. TCHAR shortPath[40] = {0};
  190. PathCompactPathEx(shortPath, dir, 35,0);
  191. Edit_SetText(hWnd, (LPCTSTR)shortPath);
  192. }
  193. else
  194. {
  195. enable = FALSE;
  196. SetWindowText(hWnd,szUnavailable);
  197. }
  198. ::EnableWindow(hWnd, enable);
  199. ::EnableWindow (GetDlgItem(hDlg, IDC_DBDIRECTORY_LABEL), enable);
  200. }
  201. // - - - - - - - - - - - - - -
  202. // Last backup:
  203. temp.Empty();
  204. hr = m_DS->GetLastBackup(temp);
  205. if(SUCCEEDED(hr))
  206. {
  207. enable = (hr != WBEM_S_FALSE);
  208. SetWindowText(GetDlgItem(hDlg, IDC_LASTBACKUP),
  209. temp);
  210. }
  211. else //failed
  212. {
  213. enable = FALSE;
  214. SetWindowText(GetDlgItem(hDlg, IDC_LASTBACKUP),
  215. szUnavailable);
  216. }
  217. ::EnableWindow(GetDlgItem(hDlg, IDC_LASTBACKUP_LABEL), enable);
  218. ::EnableWindow(GetDlgItem(hDlg, IDC_LASTBACKUP), enable);
  219. }
  220. }
  221. //------------------------------------------------------------------------
  222. void CBackupPage::OnApply(HWND hDlg, bool bClose)
  223. {
  224. if(m_bWhistlerCore == false)
  225. {
  226. HWND intervalHWND = GetDlgItem(hDlg, IDC_BACKUPINTERVAL);
  227. bool needToPut = false;
  228. UINT iValue = 0;
  229. bool valid = false;
  230. bool changed = GetInterval(hDlg, iValue, valid);
  231. if((changed || Edit_GetModify(intervalHWND)) && valid)
  232. {
  233. m_DS->SetBackupInterval(iValue);
  234. needToPut = true;
  235. }
  236. if(needToPut)
  237. {
  238. NeedToPut(PB_BACKUP, !bClose);
  239. if(!bClose)
  240. Refresh(hDlg);
  241. }
  242. }
  243. }
  244. //-----------------------------------------------------
  245. void CBackupPage::Reconnect(void)
  246. {
  247. HRESULT hr = 0;
  248. LOGIN_CREDENTIALS *credentials = m_DS->GetCredentials();
  249. m_DS->Disconnect();
  250. hr = m_DS->Connect(credentials);
  251. if(SUCCEEDED(hr))
  252. {
  253. m_alreadyAsked = false;
  254. if(ServiceIsReady(NO_UI, 0,0))
  255. {
  256. m_DS->Initialize(0);
  257. }
  258. else
  259. {
  260. TCHAR caption[100] = {0}, msg[256] = {0};
  261. ::LoadString(_Module.GetModuleInstance(), IDS_SHORT_NAME,
  262. caption, 100);
  263. ::LoadString(_Module.GetModuleInstance(), IDS_CONNECTING,
  264. msg, 256);
  265. if(DisplayAVIBox(m_hDlg, caption, msg, &m_AVIbox) == IDCANCEL)
  266. {
  267. g_serviceThread->Cancel();
  268. }
  269. }
  270. }
  271. }
  272. //-----------------------------------------------------
  273. void CBackupPage::Reconnect2(void)
  274. {
  275. m_DS->Disconnect();
  276. TCHAR caption[100] = {0}, msg[256] = {0};
  277. ::LoadString(_Module.GetModuleInstance(), IDS_SHORT_NAME,
  278. caption, 100);
  279. ::LoadString(_Module.GetModuleInstance(), IDS_POST_RESTORE,
  280. msg, 256);
  281. MessageBox(m_hDlg, (LPCTSTR)msg, (LPCTSTR)caption,
  282. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  283. PropSheet_SetCurSel(GetParent(m_hDlg), 0, 0);
  284. }
  285. //---------------------------------------------------------------------------
  286. void CBackupPage::OnFinishConnected(HWND hDlg, LPARAM lParam)
  287. {
  288. if(m_AVIbox)
  289. {
  290. PostMessage(m_AVIbox,
  291. WM_ASYNC_CIMOM_CONNECTED,
  292. 0, 0);
  293. m_AVIbox = 0;
  294. }
  295. IStream *pStream = (IStream *)lParam;
  296. IWbemServices *pServices = 0;
  297. HRESULT hr = CoGetInterfaceAndReleaseStream(pStream,
  298. IID_IWbemServices,
  299. (void**)&pServices);
  300. SetWbemService(pServices);
  301. if(ServiceIsReady(NO_UI, 0,0))
  302. {
  303. m_DS->Initialize(pServices);
  304. }
  305. }
  306. //---------------------------------------------------------------
  307. void CBackupPage::SetPriv(LPCTSTR privName, IWbemBackupRestore *br)
  308. {
  309. if (ImpersonateSelf(SecurityImpersonation))
  310. {
  311. if(OpenThreadToken( GetCurrentThread(),
  312. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  313. FALSE, &m_hAccessToken ) )
  314. {
  315. m_fClearToken = true;
  316. // Now, get the LUID for the privilege from the local system
  317. ZeroMemory(&m_luid, sizeof(m_luid));
  318. LookupPrivilegeValue(NULL, privName, &m_luid);
  319. // m_cloak = true;
  320. EnablePriv(true, br);
  321. }
  322. else
  323. {
  324. DWORD err = GetLastError();
  325. }
  326. }
  327. else
  328. {
  329. DWORD err = GetLastError();
  330. }
  331. }
  332. //---------------------------------------------------------------------
  333. bool CBackupPage::IsClientNT5OrMore(void)
  334. {
  335. OSVERSIONINFO os;
  336. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  337. if(!GetVersionEx(&os))
  338. return FALSE; // should never happen
  339. return ( os.dwPlatformId == VER_PLATFORM_WIN32_NT ) && ( os.dwMajorVersion >= 5 ) ;
  340. }
  341. //---------------------------------------------------------------
  342. DWORD CBackupPage::EnablePriv(bool fEnable, IWbemBackupRestore *br)
  343. {
  344. DWORD dwError = ERROR_SUCCESS;
  345. TOKEN_PRIVILEGES tokenPrivileges;
  346. tokenPrivileges.PrivilegeCount = 1;
  347. tokenPrivileges.Privileges[0].Luid = m_luid;
  348. tokenPrivileges.Privileges[0].Attributes = ( fEnable ? SE_PRIVILEGE_ENABLED : 0 );
  349. if(AdjustTokenPrivileges(m_hAccessToken,
  350. FALSE,
  351. &tokenPrivileges,
  352. 0, NULL, NULL) != 0)
  353. {
  354. HRESULT hr = E_FAIL;
  355. if(br)
  356. {
  357. //Privileges for backup/restore are only set for the local box
  358. //case, so in this case we need to set cloaking on the
  359. //interface for the privileges to be transfered with the call.
  360. //Make sure this is never called remotely, since in that case
  361. //the authident needs to be used and privileges are transferred
  362. //remotely so cloaking is NOT needed for remote (and if set
  363. //will null the user authident out !!)
  364. try
  365. {
  366. hr = SetInterfaceSecurityEx(
  367. br,
  368. m_cred->authIdent, //for local this is actually not relevant...
  369. NULL,
  370. RPC_C_AUTHN_LEVEL_DEFAULT,
  371. RPC_C_IMP_LEVEL_IMPERSONATE,
  372. IsClientNT5OrMore() ? EOAC_DYNAMIC_CLOAKING :
  373. EOAC_NONE);
  374. }
  375. catch( ... )
  376. {
  377. hr = E_FAIL;
  378. }
  379. }
  380. }
  381. else
  382. {
  383. dwError = ::GetLastError();
  384. }
  385. return dwError;
  386. }
  387. //---------------------------------------------------------------
  388. void CBackupPage::ClearPriv(void)
  389. {
  390. // m_cloak = true;
  391. EnablePriv(false);
  392. if(m_fClearToken)
  393. {
  394. CloseHandle(m_hAccessToken);
  395. m_hAccessToken = 0;
  396. m_fClearToken = false;
  397. }
  398. RevertToSelf(); //to make sure token is removed from thread (counterpart to ImpersonateSelf() in SetPriv())
  399. }
  400. //-----------------------------------------------------
  401. void __cdecl BackupRestoreThread(LPVOID lpParameter)
  402. {
  403. CBackupPage *me = (CBackupPage *)lpParameter;
  404. MULTI_QI qi = {&IID_IWbemBackupRestore, 0, 0};
  405. CoInitialize(0);
  406. if(me->m_DS->IsLocal())
  407. {
  408. me->m_backupHr = CoCreateInstanceEx(CLSID_WbemBackupRestore, 0,
  409. CLSCTX_LOCAL_SERVER, 0, 1, &qi);
  410. }
  411. else
  412. {
  413. COSERVERINFO server = {0,0,0,0};
  414. WCHAR machine[MAXCOMPUTER_NAME + 1] = {0};
  415. #ifdef UNICODE
  416. wcscpy(machine, me->m_DS->m_whackedMachineName);
  417. #else
  418. mbstowcs(machine,
  419. (LPCTSTR)me->m_DS->m_whackedMachineName,
  420. _tcslen(me->m_DS->m_whackedMachineName) + 1);
  421. #endif
  422. server.pwszName = machine;
  423. COAUTHINFO authInfo = {10,0, 0, RPC_C_AUTHN_LEVEL_DEFAULT,
  424. RPC_C_IMP_LEVEL_IMPERSONATE,
  425. me->m_cred->authIdent, 0};
  426. server.pAuthInfo = &authInfo;
  427. me->m_backupHr = CoCreateInstanceEx(CLSID_WbemBackupRestore, 0,
  428. CLSCTX_REMOTE_SERVER,
  429. &server, 1, &qi);
  430. }
  431. if(SUCCEEDED(me->m_backupHr) && SUCCEEDED(qi.hr))
  432. {
  433. IWbemBackupRestore *pBR = (IWbemBackupRestore *)qi.pItf;
  434. SetInterfaceSecurityEx(pBR,
  435. me->m_cred->authIdent,
  436. NULL,
  437. RPC_C_AUTHN_LEVEL_DEFAULT,
  438. RPC_C_IMP_LEVEL_IMPERSONATE,
  439. 0);
  440. ATLTRACE(_T("begin backup/restore\n"));
  441. //Sleep(1000);
  442. CHString1 verStr;
  443. bool TgtisNT5 = true;
  444. if(me->m_DS)
  445. {
  446. me->m_DS->GetOSVersion(verStr);
  447. if(verStr[0] == _T('4'))
  448. TgtisNT5 = false;
  449. }
  450. if(me->m_doingBackup)
  451. {
  452. //Only set the privilege if we are local - otherwise the privilege is there already
  453. //in the thread token...
  454. if( me->m_DS->IsLocal() && TgtisNT5)
  455. me->SetPriv(SE_BACKUP_NAME, pBR);
  456. me->m_backupHr = pBR->Backup(me->m_wszArgs, 0);
  457. }
  458. else
  459. {
  460. //Only set the privilege if we are local - otherwise the privilege is there already
  461. //in the thread token...
  462. if( me->m_DS->IsLocal() && TgtisNT5)
  463. me->SetPriv(SE_RESTORE_NAME, pBR);
  464. me->m_backupHr = pBR->Restore(me->m_wszArgs,
  465. WBEM_FLAG_BACKUP_RESTORE_FORCE_SHUTDOWN);
  466. }
  467. if(TgtisNT5)
  468. me->ClearPriv();
  469. ATLTRACE(_T("done backup/restore\n"));
  470. qi.pItf->Release();
  471. }
  472. CoUninitialize();
  473. // kill the distraction.
  474. if(me->m_AVIbox)
  475. {
  476. ::PostMessage(me->m_AVIbox, WM_ASYNC_CIMOM_CONNECTED, 0, 0);
  477. }
  478. _endthread();
  479. }
  480. //------------------------------------------------------------------------
  481. void CBackupPage::DealWithPath(LPCTSTR pathFile)
  482. {
  483. #ifdef UNICODE
  484. if (m_wszArgs != NULL)
  485. delete m_wszArgs;
  486. m_wszArgs = new TCHAR[lstrlen(pathFile) + 1];
  487. if (m_wszArgs != NULL)
  488. lstrcpy(m_wszArgs, pathFile);
  489. #else
  490. size_t nSize = mbstowcs(NULL, (LPCTSTR)pathFile, _tcslen(pathFile) + 1);
  491. m_wszArgs = new wchar_t[nSize + 1];
  492. mbstowcs(m_wszArgs, (LPCTSTR)pathFile, _tcslen(pathFile) + 1);
  493. #endif
  494. }
  495. //------------------------------------------------------------------------
  496. void CBackupPage::DealWithDomain(void)
  497. {
  498. m_cred = m_DS->GetCredentials();
  499. // 54062- Nt 4.0 rpc crashes if given a null domain name along with a valid user name
  500. if((m_cred->authIdent != 0) &&
  501. (m_cred->authIdent->DomainLength == 0) &&
  502. (m_cred->authIdent->UserLength > 0) && IsNT(4))
  503. {
  504. LPTSTR pNTDomain = NULL;
  505. CNtSid sid(CNtSid::CURRENT_USER);
  506. DWORD dwRet = sid.GetInfo(NULL, &pNTDomain, NULL);
  507. if(dwRet == 0)
  508. {
  509. #ifdef UNICODE
  510. if(m_cred->authIdent->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
  511. {
  512. // convert UNICODE to ansi.
  513. int size = wcstombs(NULL, pNTDomain, 0);
  514. m_cred->authIdent->Domain =
  515. (LPWSTR)CoTaskMemAlloc((size+1) * sizeof(TCHAR));
  516. if (m_cred->authIdent->Domain != NULL)
  517. {
  518. memset(m_cred->authIdent->Domain, 0,
  519. (size+1) * sizeof(TCHAR));
  520. wcstombs((char *)m_cred->authIdent->Domain, pNTDomain,
  521. size);
  522. m_cred->authIdent->DomainLength = size;
  523. }
  524. }
  525. else
  526. {
  527. //straight unicode copy.
  528. m_cred->authIdent->DomainLength = wcslen(pNTDomain);
  529. m_cred->authIdent->Domain = (LPWSTR)pNTDomain;
  530. }
  531. #else // ANSI
  532. if(m_cred->authIdent->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
  533. {
  534. //straight ansi copy.
  535. m_cred->authIdent->DomainLength = strlen(pNTDomain);
  536. m_cred->authIdent->Domain = (LPWSTR)pNTDomain;
  537. }
  538. else
  539. {
  540. // convert ansi to UNICODE.
  541. int size = mbstowcs(NULL, pNTDomain, 0);
  542. WCHAR temp[100] = {0};
  543. m_cred->authIdent->Domain =
  544. (LPWSTR)CoTaskMemAlloc((size+1) * sizeof(WCHAR));
  545. if (m_cred->authIdent->Domain != NULL)
  546. {
  547. memset(m_cred->authIdent->Domain, 0,
  548. (size+1) * sizeof(WCHAR));
  549. mbstowcs(temp, pNTDomain, min(99, size)); //so it's always null terminated
  550. wcscpy(m_cred->authIdent->Domain, temp);
  551. m_cred->authIdent->DomainLength = size;
  552. }
  553. }
  554. #endif UNICODE
  555. }
  556. }
  557. }
  558. //----------------------------------------------------------
  559. BOOL CBackupPage::BackupMethod(HWND hDlg, LPCTSTR pathFile)
  560. {
  561. UINT prompt = IDYES;
  562. TCHAR drive[_MAX_DRIVE] = {0}, path[_MAX_DIR] = {0},
  563. temp[_MAX_PATH] = {0};
  564. // rip it apart.
  565. _tsplitpath(pathFile, drive, path, NULL, NULL);
  566. _tcscpy(temp, drive);
  567. _tcscat(temp, path);
  568. if(!m_DS->IsValidDir(CHString1(temp)))
  569. {
  570. CHString1 caption, threat;
  571. caption.LoadString(IDS_SHORT_NAME);
  572. threat.LoadString(IDS_NEED_EXISTING_DIR);
  573. MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  574. MB_OK|MB_DEFBUTTON1|MB_ICONEXCLAMATION);
  575. return FALSE;
  576. }
  577. else if(m_DS->IsValidFile(pathFile))
  578. {
  579. CHString1 caption, threat;
  580. caption.LoadString(IDS_SHORT_NAME);
  581. threat.LoadString(IDS_BACKUP_OVERWRITE);
  582. prompt = MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  583. MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION);
  584. }
  585. if(prompt == IDYES)
  586. {
  587. DealWithPath(pathFile);
  588. // UI
  589. CHString1 title, msg, fmt;
  590. m_AVIbox = 0;
  591. TCHAR shortPath[33] = {0};
  592. m_backupHr = S_OK;
  593. title.LoadString(IDS_BACKUP_TITLE);
  594. fmt.LoadString(IDS_BACKUP_FMT);
  595. PathCompactPathEx(shortPath, pathFile, 32,0);
  596. msg.Format(fmt, shortPath);
  597. DealWithDomain();
  598. // m_DS->m_rootThread.m_WbemServices.SetPriv(SE_BACKUP_NAME);
  599. m_doingBackup = true;
  600. if(_beginthread(BackupRestoreThread, 0,
  601. (LPVOID)this) != -1)
  602. {
  603. DisplayAVIBox(hDlg, title, msg, &m_AVIbox, FALSE);
  604. }
  605. if(FAILED(m_backupHr))
  606. {
  607. TCHAR msg[256] = {0};
  608. if(ErrorStringEx(m_backupHr, msg, 256))
  609. {
  610. CHString1 caption;
  611. caption.LoadString(IDS_SHORT_NAME);
  612. MessageBox(hDlg, msg, caption, MB_OK| MB_ICONWARNING);
  613. }
  614. }
  615. // m_DS->m_rootThread.m_WbemServices.ClearPriv();
  616. } //endif doIt
  617. return SUCCEEDED(m_backupHr);
  618. }
  619. //------------------------------------------------------------------------
  620. HRESULT CBackupPage::RestoreMethod(HWND hDlg, LPCTSTR pathFile)
  621. {
  622. HRESULT hr = WBEM_E_NOT_FOUND;
  623. if(m_DS->IsValidFile(pathFile))
  624. {
  625. DealWithPath(pathFile);
  626. // UI
  627. CHString1 title, msg, fmt;
  628. m_AVIbox = 0;
  629. TCHAR shortPath[33] = {0};
  630. title.LoadString(IDS_RESTORE_TITLE);
  631. fmt.LoadString(IDS_RESTORE_FMT);
  632. PathCompactPathEx(shortPath, pathFile, 32,0);
  633. msg.Format(fmt, shortPath);
  634. DealWithDomain();
  635. m_doingBackup = false;
  636. if(_beginthread(BackupRestoreThread, 0,
  637. (LPVOID)this) != -1)
  638. {
  639. DisplayAVIBox(hDlg, title, msg, &m_AVIbox, FALSE);
  640. Reconnect2();
  641. }
  642. if(FAILED(m_backupHr))
  643. {
  644. TCHAR msg[256] = {0};
  645. if(ErrorStringEx(m_backupHr, msg, 256))
  646. {
  647. CHString1 caption;
  648. caption.LoadString(IDS_SHORT_NAME);
  649. MessageBox(hDlg, msg, caption, MB_OK| MB_ICONWARNING);
  650. }
  651. }
  652. hr = m_backupHr;
  653. }
  654. else
  655. {
  656. // NOTE: shouldn't ever get here... but..
  657. CHString1 caption, threat;
  658. caption.LoadString(IDS_SHORT_NAME);
  659. threat.LoadString(IDS_NO_BACKUP_FILE);
  660. MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  661. MB_OK|MB_ICONEXCLAMATION);
  662. }
  663. return hr;
  664. }
  665. //------------------------------------------------------------------------
  666. BOOL CBackupPage::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  667. {
  668. TCHAR * pszFilter;
  669. switch(uMsg)
  670. {
  671. case WM_INITDIALOG:
  672. InitDlg(hDlg);
  673. break;
  674. case WM_ASYNC_CIMOM_CONNECTED:
  675. {
  676. OnFinishConnected(hDlg, lParam);
  677. Refresh(hDlg);
  678. }
  679. break;
  680. case WM_NOTIFY:
  681. switch(((LPNMHDR)lParam)->code)
  682. {
  683. case PSN_SETACTIVE:
  684. Refresh(hDlg);
  685. break;
  686. case PSN_HELP:
  687. HTMLHelper(hDlg);
  688. break;
  689. case PSN_APPLY:
  690. OnApply(hDlg, (((LPPSHNOTIFY)lParam)->lParam == 1));
  691. break;
  692. }
  693. break;
  694. case WM_COMMAND:
  695. switch(LOWORD(wParam))
  696. {
  697. case IDC_BACKUPINTERVAL:
  698. if(HIWORD(wParam) == EN_KILLFOCUS)
  699. {
  700. TCHAR buf[4] = {0};
  701. int iVal = 0;
  702. ::GetWindowText((HWND)lParam, buf, ARRAYSIZE(buf));
  703. iVal = _ttoi(buf);
  704. if(iVal == 0)
  705. {
  706. BOOL enable = TRUE;
  707. CHString1 caption, threat;
  708. caption.LoadString(IDS_SHORT_NAME);
  709. threat.LoadString(IDS_BACKUP_THREAT);
  710. if(MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  711. MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION) == IDYES)
  712. {
  713. ComboBox_SetCurSel(GetDlgItem(hDlg, IDC_BACKUP_UNITS), -1);
  714. ::EnableWindow((HWND)lParam, FALSE);
  715. ::EnableWindow(GetDlgItem(hDlg, IDC_BACKUP_UNITS), FALSE);
  716. Button_SetCheck(GetDlgItem(hDlg, IDC_BACKUP_ENABLED), BST_UNCHECKED);
  717. }
  718. else
  719. {
  720. Button_SetCheck(GetDlgItem(hDlg, IDC_BACKUP_ENABLED), BST_CHECKED);
  721. Refresh(hDlg, true);
  722. return TRUE;
  723. }
  724. }
  725. PageChanged(PB_BACKUP, true);
  726. }
  727. else if((HIWORD(wParam) == EN_CHANGE) &&
  728. Edit_GetModify((HWND)lParam))
  729. {
  730. PageChanged(PB_BACKUP, true);
  731. return TRUE;
  732. }
  733. break;
  734. case IDC_BACKUP_UNITS:
  735. if(HIWORD(wParam) == CBN_SELCHANGE)
  736. {
  737. PageChanged(PB_BACKUP, true);
  738. return TRUE;
  739. }
  740. break;
  741. case IDC_RESTORE_FM_AUTO:
  742. if(HIWORD(wParam) == BN_CLICKED)
  743. {
  744. CHString1 dbPath;
  745. m_DS->GetDBDir(dbPath);
  746. dbPath += _T("\\Cim.rec");
  747. CHString1 caption, threat;
  748. caption.LoadString(IDS_AUTORESTORE);
  749. threat.LoadString(IDS_SURE);
  750. if(MessageBox(hDlg, threat, caption,
  751. MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION) == IDYES)
  752. {
  753. RestoreMethod(hDlg, dbPath);
  754. }
  755. }
  756. break;
  757. case IDC_BACKUP_BTN:
  758. if(HIWORD(wParam) == BN_CLICKED)
  759. {
  760. TCHAR pathFile[MAX_PATH] = {0};
  761. CHString1 dbPath;
  762. TCHAR recFilter[200] = {0}, all[100] = {0};
  763. TCHAR *pTemp = recFilter;
  764. m_DS->GetDBDir(dbPath);
  765. int recSize = ::LoadString(_Module.GetModuleInstance(), IDS_REC_FILTER,
  766. recFilter, 100);
  767. int allSize = ::LoadString(_Module.GetModuleInstance(), IDS_ALL_FILTER,
  768. all, 100);
  769. // Build this string with the words coming from the string table.
  770. //_T("WMI Recovery Files (*.rec)\0*.rec\0All Files (*.*)\0*.*\0\0");
  771. pszFilter = new TCHAR[_tcslen(recFilter) + 1 +
  772. ARRAYSIZE(REC_WILDCARD) +
  773. _tcslen(all) + 1 +
  774. ARRAYSIZE(ALL_WILDCARD) + 1];
  775. if (pszFilter != NULL)
  776. {
  777. TCHAR * psz = pszFilter;
  778. _tcscpy(psz, recFilter);
  779. psz += recSize + 1;
  780. _tcscpy(psz,REC_WILDCARD);
  781. psz += ARRAYSIZE(REC_WILDCARD);
  782. _tcscpy(psz, all);
  783. psz += allSize + 1;
  784. _tcscpy(psz, ALL_WILDCARD);
  785. psz += ARRAYSIZE(ALL_WILDCARD);
  786. *psz = _T('\0');
  787. if(BrowseForFile(hDlg,
  788. IDS_OPEN_BACKUP,
  789. recFilter,
  790. (LPCTSTR)dbPath,
  791. pathFile,
  792. MAX_PATH))
  793. {
  794. BackupMethod(hDlg, pathFile);
  795. }
  796. delete pszFilter;
  797. }
  798. }
  799. break;
  800. case IDC_RESTORE_BTN:
  801. if(HIWORD(wParam) == BN_CLICKED)
  802. {
  803. TCHAR pathFile[MAX_PATH] = {0};
  804. CHString1 dbPath, title;
  805. TCHAR recFilter[100] = {0}, all[100] = {0};
  806. m_DS->GetDBDir(dbPath);
  807. int recSize = ::LoadString(_Module.GetModuleInstance(), IDS_REC_FILTER,
  808. recFilter, 100);
  809. int allSize = ::LoadString(_Module.GetModuleInstance(), IDS_ALL_FILTER,
  810. all, 100);
  811. // Build this string with the words coming from the string table.
  812. //_T("WMI Recovery Files (*.rec)\0*.rec\0All Files (*.*)\0*.*\0\0");
  813. pszFilter = new TCHAR[_tcslen(recFilter) + 1 +
  814. ARRAYSIZE(REC_WILDCARD) +
  815. _tcslen(all) + 1 +
  816. ARRAYSIZE(ALL_WILDCARD) + 1];
  817. if (pszFilter != NULL)
  818. {
  819. TCHAR * psz = pszFilter;
  820. _tcscpy(psz, recFilter);
  821. psz += recSize + 1;
  822. _tcscpy(psz,REC_WILDCARD);
  823. psz += ARRAYSIZE(REC_WILDCARD);
  824. _tcscpy(psz, all);
  825. psz += allSize + 1;
  826. _tcscpy(psz, ALL_WILDCARD);
  827. psz += ARRAYSIZE(ALL_WILDCARD);
  828. *psz = _T('\0');
  829. if(BrowseForFile(hDlg,
  830. IDS_OPEN_RESTORE,
  831. pszFilter,
  832. (LPCTSTR)dbPath,
  833. pathFile,
  834. MAX_PATH,
  835. 0))
  836. {
  837. RestoreMethod(hDlg, pathFile);
  838. }
  839. delete pszFilter;
  840. }
  841. }
  842. break;
  843. case IDC_BACKUP_ENABLED:
  844. if(HIWORD(wParam) == BN_CLICKED)
  845. {
  846. int x = Button_GetState((HWND)lParam);
  847. BOOL checked = (x & BST_CHECKED);
  848. BOOL enable = FALSE;
  849. if(checked)
  850. {
  851. // turn on and repopulate the edit fields.
  852. CHString1 temp;
  853. UINT iTemp = 30;
  854. SetInterval(hDlg, iTemp);
  855. enable = TRUE;
  856. }
  857. else // turning off.
  858. {
  859. CHString1 caption, threat;
  860. caption.LoadString(IDS_SHORT_NAME);
  861. threat.LoadString(IDS_BACKUP_THREAT);
  862. if(MessageBox(hDlg, (LPCTSTR)threat, (LPCTSTR)caption,
  863. MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION) == IDYES)
  864. {
  865. SetWindowText(GetDlgItem(hDlg, IDC_BACKUPINTERVAL), _T("0"));
  866. ComboBox_SetCurSel(GetDlgItem(hDlg, IDC_BACKUP_UNITS), -1);
  867. enable = FALSE;
  868. }
  869. else
  870. {
  871. Button_SetCheck(GetDlgItem(hDlg, IDC_BACKUP_ENABLED), BST_CHECKED);
  872. Refresh(hDlg);
  873. return TRUE;
  874. }
  875. }
  876. ::EnableWindow(GetDlgItem(hDlg, IDC_BACKUPINTERVAL), enable);
  877. ::EnableWindow(GetDlgItem(hDlg, IDC_BACKUP_UNITS), enable);
  878. PageChanged(PB_BACKUP, true);
  879. return TRUE;
  880. }
  881. break;
  882. default: break;
  883. } //endswitch(LOWORD(wParam))
  884. break;
  885. case WM_HELP:
  886. if (IsWindowEnabled(hDlg))
  887. {
  888. WinHelp((HWND)((LPHELPINFO)lParam)->hItemHandle,
  889. c_HelpFile,
  890. HELP_WM_HELP,
  891. (ULONG_PTR)buPageHelpIDs);
  892. }
  893. break;
  894. case WM_CONTEXTMENU:
  895. if (IsWindowEnabled(hDlg))
  896. {
  897. WinHelp(hDlg, c_HelpFile,
  898. HELP_CONTEXTMENU,
  899. (ULONG_PTR)buPageHelpIDs);
  900. }
  901. break;
  902. default:
  903. return FALSE;
  904. }
  905. return TRUE;
  906. }
  907. void CBackupPage::HideAutomaticBackupControls(HWND hDlg)
  908. {
  909. ShowWindow(GetDlgItem(hDlg,IDC_AUTOMATIC_GROUPBOX),SW_HIDE);
  910. ShowWindow(GetDlgItem(hDlg,IDC_ADV_NOW_TEXT),SW_HIDE);
  911. ShowWindow(GetDlgItem(hDlg,IDC_BACKUP_ENABLED),SW_HIDE);
  912. ShowWindow(GetDlgItem(hDlg,IDC_BACKUPINTERVAL),SW_HIDE);
  913. ShowWindow(GetDlgItem(hDlg,IDC_BACKUP_UNITS),SW_HIDE);
  914. ShowWindow(GetDlgItem(hDlg,IDC_LASTBACKUP_LABEL),SW_HIDE);
  915. ShowWindow(GetDlgItem(hDlg,IDC_LASTBACKUP),SW_HIDE);
  916. ShowWindow(GetDlgItem(hDlg,IDC_RESTORE_FM_AUTO),SW_HIDE);
  917. ShowWindow(GetDlgItem(hDlg,IDC_BACKUP_UNITS),SW_HIDE);
  918. ShowWindow(GetDlgItem(hDlg,IDC_DBDIRECTORY_LABEL),SW_HIDE);
  919. ShowWindow(GetDlgItem(hDlg,IDC_DB_DIR),SW_HIDE);
  920. SetWindowPos(GetDlgItem(hDlg,IDC_MANUAL_GROUPBOX),NULL,7,12,0,0,SWP_NOSIZE | SWP_NOZORDER);
  921. SetWindowPos(GetDlgItem(hDlg,IDC_ADV_NOW_TEXT2),NULL,32,35,0,0,SWP_NOSIZE | SWP_NOZORDER);
  922. SetWindowPos(GetDlgItem(hDlg,IDC_BACKUP_BTN),NULL,40,97,87,14,SWP_NOSIZE | SWP_NOZORDER);
  923. SetWindowPos(GetDlgItem(hDlg,IDC_RESTORE_BTN),NULL,195,97,87,14,SWP_NOSIZE | SWP_NOZORDER);
  924. m_bWhistlerCore = true;
  925. }