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.

1544 lines
46 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation 1996-2001.
  5. //
  6. // File: command.cpp
  7. //
  8. // Contents: implementation of CComponentDataImpl
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include "wrapper.h"
  13. #include "snapmgr.h"
  14. #include "asgncnfg.h"
  15. #include "util.h"
  16. #include <io.h>
  17. #include <Dlgs.h>
  18. static BOOL RecursiveCreateDirectory(
  19. IN LPCTSTR pszDir
  20. )
  21. {
  22. DWORD dwAttr;
  23. DWORD dwErr;
  24. LPCTSTR psz;
  25. DWORD cch;
  26. WCHAR wch;
  27. LPTSTR pszParent = NULL;
  28. BOOL fResult = FALSE;
  29. dwAttr = GetFileAttributes(pszDir);
  30. if (0xFFFFFFFF != dwAttr)
  31. {
  32. if (FILE_ATTRIBUTE_DIRECTORY & dwAttr)
  33. fResult = TRUE;
  34. goto CommonReturn;
  35. }
  36. dwErr = GetLastError();
  37. if (!(ERROR_PATH_NOT_FOUND == dwErr || ERROR_FILE_NOT_FOUND == dwErr))
  38. return FALSE;
  39. //This is a safe usage. pszDir is a full path.
  40. if (CreateDirectory(pszDir, NULL)) // lpSecurityAttributes
  41. {
  42. fResult = TRUE;
  43. goto CommonReturn;
  44. }
  45. dwErr = GetLastError();
  46. if (!(ERROR_PATH_NOT_FOUND == dwErr || ERROR_FILE_NOT_FOUND == dwErr))
  47. goto CommonReturn;
  48. // Peal off the last path name component
  49. cch = _tcslen(pszDir);
  50. psz = pszDir + cch;
  51. while (TEXT('\\') != *psz)
  52. {
  53. if (psz == pszDir)
  54. // Path didn't have a \.
  55. goto CommonReturn;
  56. psz--;
  57. }
  58. cch = (DWORD)(psz - pszDir);
  59. if (0 == cch)
  60. // Detected leading \Path
  61. goto CommonReturn;
  62. // Check for leading \\ or x:\.
  63. wch = *(psz - 1);
  64. if ((1 == cch && TEXT('\\') == wch) || (2 == cch && TEXT(':') == wch))
  65. goto CommonReturn;
  66. if (NULL == (pszParent = (LPWSTR) LocalAlloc(0, (cch + 1) * sizeof(WCHAR))))
  67. goto CommonReturn;
  68. //This may not be a safe usage, using TCHAR instead of WCHAR. Consider fix.
  69. memcpy(pszParent, pszDir, cch * sizeof(TCHAR));
  70. pszParent[cch] = TEXT('\0');
  71. //This is a safe usage, pszParent is a full path.
  72. if (!RecursiveCreateDirectory(pszParent))
  73. goto CommonReturn;
  74. if (!CreateDirectory(pszDir, NULL)) // lpSecurityAttributes
  75. {
  76. dwErr = GetLastError();
  77. goto CommonReturn;
  78. }
  79. fResult = TRUE;
  80. CommonReturn:
  81. if (pszParent != NULL)
  82. LocalFree(pszParent);
  83. return fResult;
  84. }
  85. //+------------------------------------------------------------------------------------
  86. // CComponentDataImpl::GetWorkingDir
  87. //
  88. // Gets the default or last set directory used for the uIDDir.
  89. // Some defaults defined by this function are.
  90. //
  91. // %windir%\security\database - ANALYSIS
  92. // %windir%\security\Templates - Default profile location.
  93. //
  94. // Arguments: [uIDDir] - The ID of the working directory to set or retrieve.
  95. // [pStr] - Either the source or the return value. When the
  96. // function returns this value will be set to a directory
  97. // location.
  98. //
  99. // [bSet] - TRUE if [pStr] is to be set as the new working directory.
  100. // [bFile] - [pStr] contains a file name.
  101. //
  102. // Returns: TRUE - If the function succees.
  103. // FALSE - if something went wrong.
  104. // We don't really need to much granularity because this is not a critical
  105. // function.
  106. //+------------------------------------------------------------------------------------
  107. BOOL
  108. CComponentDataImpl::GetWorkingDir(
  109. GWD_TYPES uIDDir,
  110. LPTSTR *pStr,
  111. BOOL bSet,
  112. BOOL bFile
  113. )
  114. {
  115. BOOL fRet = FALSE;
  116. if(!pStr)
  117. {
  118. return FALSE;
  119. }
  120. LPTSTR szLocationValue = NULL;
  121. switch(uIDDir)
  122. {
  123. case GWD_CONFIGURE_LOG:
  124. szLocationValue = CONFIGURE_LOG_LOCATIONS_KEY;
  125. break;
  126. case GWD_ANALYSIS_LOG:
  127. szLocationValue = ANALYSIS_LOG_LOCATIONS_KEY;
  128. break;
  129. case GWD_OPEN_DATABASE:
  130. szLocationValue = OPEN_DATABASE_LOCATIONS_KEY;
  131. break;
  132. case GWD_IMPORT_TEMPLATE:
  133. szLocationValue = IMPORT_TEMPLATE_LOCATIONS_KEY;
  134. break;
  135. case GWD_EXPORT_TEMPLATE:
  136. szLocationValue = IMPORT_TEMPLATE_LOCATIONS_KEY; //104152, yanggao, 3/21/2001
  137. break;
  138. }
  139. LPTSTR pszPath = NULL;
  140. int i = 0;
  141. if( bSet )
  142. {
  143. if(!pStr || !(*pStr))
  144. return FALSE;
  145. i = lstrlen( *pStr );
  146. if(bFile)
  147. {
  148. //
  149. // Remove the file from the end of the string.
  150. //
  151. while(i && (*pStr)[i] != '\\')
  152. i--;
  153. }
  154. //
  155. // Create space for the new path and copy what we want.
  156. //
  157. pszPath = (LPTSTR)LocalAlloc( 0, (i + 1) * sizeof(TCHAR));
  158. if(!pszPath)
  159. return FALSE;
  160. //This is a safe usage.
  161. memcpy(pszPath, *pStr, (i * sizeof(TCHAR)));
  162. pszPath[i] = 0;
  163. MyRegSetValue(HKEY_CURRENT_USER,
  164. DEFAULT_LOCATIONS_KEY,
  165. szLocationValue,
  166. (BYTE*)pszPath,
  167. (i+1)*sizeof(TCHAR),
  168. REG_SZ);
  169. LocalFree(pszPath);
  170. return TRUE;
  171. }
  172. DWORD dwType = REG_SZ;
  173. if (MyRegQueryValue(HKEY_CURRENT_USER,
  174. DEFAULT_LOCATIONS_KEY,
  175. szLocationValue,
  176. (PVOID*)&pszPath,
  177. &dwType) == ERROR_SUCCESS)
  178. {
  179. *pStr = pszPath;
  180. return TRUE;
  181. }
  182. CString sAppend;
  183. DWORD dwRet;
  184. pszPath = NULL;
  185. switch ( uIDDir )
  186. {
  187. case GWD_CONFIGURE_LOG:
  188. case GWD_ANALYSIS_LOG:
  189. sAppend.LoadString(IDS_LOGFILE_DEFAULT);
  190. // fall through
  191. case GWD_OPEN_DATABASE:
  192. //
  193. // Used for open DB.
  194. //
  195. if (sAppend.IsEmpty())
  196. {
  197. sAppend.LoadString( IDS_DB_DEFAULT );
  198. } // else fell through
  199. //
  200. // Default directory for analysis.
  201. //
  202. pszPath = (LPTSTR)LocalAlloc( 0, (MAX_PATH + sAppend.GetLength() + 1) * sizeof(TCHAR));
  203. if (pszPath == NULL)
  204. return FALSE;
  205. if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, pszPath)))
  206. {
  207. //This is a safe usage, yanggao.
  208. lstrcpy( &(pszPath[lstrlen(pszPath)]), sAppend );
  209. //
  210. // Check to see if the directory does not exist, it may need
  211. // to be created if this is the first time this user has
  212. // opened a database.
  213. //
  214. TCHAR szTempFile[MAX_PATH];
  215. CString str;
  216. str.LoadString(IDS_TEMP_FILENAME);
  217. if (!GetTempFileName(pszPath,str,0,szTempFile)) //This is not a safe usage. Raid #555912, yanggao.
  218. {
  219. if ((GetLastError() == ERROR_DIRECTORY) && (RecursiveCreateDirectory(pszPath)))
  220. {
  221. fRet = TRUE;
  222. }
  223. else
  224. {
  225. LocalFree(pszPath);
  226. fRet = FALSE;
  227. }
  228. }
  229. else
  230. {
  231. DeleteFile(szTempFile);
  232. fRet = TRUE;
  233. }
  234. }
  235. else
  236. {
  237. LocalFree(pszPath);
  238. pszPath = NULL;
  239. }
  240. break;
  241. case GWD_IMPORT_TEMPLATE:
  242. case GWD_EXPORT_TEMPLATE:
  243. sAppend.LoadString( IDS_DEFAULT_TEMPLATE_DIR );
  244. //
  245. // Default directory for analysis.
  246. //
  247. dwRet = GetSystemWindowsDirectory( NULL, 0);
  248. if (dwRet)
  249. {
  250. pszPath = (LPTSTR)LocalAlloc( 0, (dwRet + sAppend.GetLength() + 1) * sizeof(TCHAR));
  251. if (!pszPath)
  252. return FALSE;
  253. GetSystemWindowsDirectory( pszPath, dwRet + 1);
  254. //This is a safe usage, yanggao.
  255. lstrcpy( &(pszPath[lstrlen(pszPath)]), sAppend );
  256. i = lstrlen(pszPath);
  257. //
  258. // Make sure the user can write to this directory:
  259. //
  260. TCHAR szTempFile[MAX_PATH];
  261. HANDLE hTempFile=NULL;
  262. CString str;
  263. str.LoadString(IDS_TEMP_FILENAME);
  264. szTempFile[0] = L'\0';
  265. if (GetTempFileName(pszPath,str,0,szTempFile)) //This is not a safe usage. Raid #555912, yanggao.
  266. {
  267. hTempFile = ExpandAndCreateFile(szTempFile,
  268. GENERIC_READ|GENERIC_WRITE,
  269. 0,
  270. NULL,
  271. CREATE_NEW,
  272. FILE_ATTRIBUTE_TEMPORARY,
  273. NULL);
  274. }
  275. if (hTempFile)
  276. {
  277. //
  278. // We have write access to this directory
  279. //
  280. ::CloseHandle(hTempFile);
  281. DeleteFile(szTempFile);
  282. fRet = TRUE;
  283. }
  284. else
  285. {
  286. //
  287. // We don't have write access to this directory. Find another
  288. // or we can't get a temp file name
  289. //
  290. LPTSTR szPath;
  291. LPITEMIDLIST pidl;
  292. LPMALLOC pMalloc;
  293. //
  294. // For some reason this won't compile with SHGetFolderPath()
  295. // Therefore go the long way around and deal with the pidl.
  296. //
  297. if (NOERROR == SHGetSpecialFolderLocation(m_hwndParent,CSIDL_PERSONAL,&pidl))
  298. {
  299. if (SHGetPathFromIDList(pidl,szTempFile))
  300. {
  301. szPath = (LPTSTR)LocalAlloc(LPTR, (lstrlen(szTempFile)+ 1) * sizeof(TCHAR));
  302. if (szPath)
  303. {
  304. //
  305. // If we can't create a new path then use the old one so the user
  306. // at least has a starting place to browse from
  307. //
  308. //This is a safe usage.
  309. lstrcpy(szPath,szTempFile);
  310. LocalFree(pszPath);
  311. pszPath = szPath;
  312. fRet = TRUE;
  313. }
  314. }
  315. if (SUCCEEDED(SHGetMalloc(&pMalloc)))
  316. {
  317. pMalloc->Free(pidl);
  318. pMalloc->Release();
  319. }
  320. }
  321. //
  322. // If we can't write here the user will have to browse for something
  323. //
  324. }
  325. }
  326. break;
  327. }
  328. *pStr = pszPath;
  329. return fRet;
  330. }
  331. UINT_PTR CALLBACK OFNHookProc(
  332. HWND hdlg, // handle to child dialog box
  333. UINT uiMsg, // message identifier
  334. WPARAM wParam, // message parameter
  335. LPARAM lParam // message parameter
  336. )
  337. {
  338. if ( WM_NOTIFY == uiMsg )
  339. {
  340. OFNOTIFY* pOFNotify = (OFNOTIFY*) lParam;
  341. if ( pOFNotify && CDN_FILEOK == pOFNotify->hdr.code )
  342. {
  343. //
  344. // Don't accept filenames with DBCS characters that are greater then 255 in them
  345. //
  346. CString strErr;
  347. CString strtemp;
  348. long templen = wcslen(pOFNotify->lpOFN->lpstrDefExt) + 2;//Raid #533603, yanggao, 4/17/2002.
  349. strtemp = pOFNotify->lpOFN->lpstrFile;
  350. strErr = pOFNotify->lpOFN->lpstrDefExt;
  351. if( L"\\."+ strErr == strtemp.Right(templen) || L"/."+strErr == strtemp.Right(templen) )
  352. {
  353. strErr.FormatMessage(IDS_INVALID_DBNAME, pOFNotify->lpOFN->lpstrFile, pOFNotify->lpOFN->lpstrDefExt);
  354. AppMessageBox(NULL,strErr,NULL,MB_ICONSTOP|MB_OK);
  355. SetFocus(GetDlgItem(GetParent(hdlg), cmb13));
  356. ::SetWindowLongPtr(hdlg, DWLP_MSGRESULT, TRUE);
  357. return 1;
  358. }
  359. templen = wcslen(pOFNotify->lpOFN->lpstrFile); //Raid #401060
  360. if( templen >= MAX_PATH || (templen-strtemp.ReverseFind(L'.'))-1 != wcslen(pOFNotify->lpOFN->lpstrDefExt) )
  361. {
  362. //This is a safe usage.
  363. strErr.FormatMessage(IDS_DB_NAME_SPACE_NOT_ENOUGH, pOFNotify->lpOFN->lpstrFile);
  364. strtemp.LoadString(IDS_PATH_TOO_LONG);
  365. strErr = strErr + L"\n" + strtemp;
  366. AppMessageBox(NULL,strErr,NULL,MB_ICONSTOP|MB_OK);
  367. SetFocus(GetDlgItem(GetParent(hdlg), cmb13)); //Raid #502393.
  368. ::SetWindowLongPtr(hdlg, DWLP_MSGRESULT, TRUE);
  369. return 1;
  370. }
  371. templen = templen - wcslen(pOFNotify->lpOFN->lpstrDefExt);
  372. if( _wcsicmp(pOFNotify->lpOFN->lpstrFile+templen, pOFNotify->lpOFN->lpstrDefExt) != 0 )
  373. {
  374. //This is a safe usage.
  375. strErr.FormatMessage(IDS_DB_NAME_SPACE_NOT_ENOUGH, pOFNotify->lpOFN->lpstrFile);
  376. strtemp.LoadString(IDS_ERR_FILE_EXTENSION); //Raid #567739, yanggao, 4/4/2002.
  377. strErr = strErr + L"\n" + strtemp;
  378. AppMessageBox(NULL,strErr,NULL,MB_ICONSTOP|MB_OK);
  379. SetFocus(GetDlgItem(GetParent(hdlg), cmb13)); //Raid #502393.
  380. ::SetWindowLongPtr(hdlg, DWLP_MSGRESULT, TRUE);
  381. return 1;
  382. }
  383. //Raid #263854, 4/3/2001
  384. BOOL ferr = TRUE;
  385. // This is a safe usage. yanggao
  386. if(WideCharToMultiByte(CP_ACP, 0, pOFNotify->lpOFN->lpstrFile,
  387. -1, NULL, 0, NULL, &ferr))
  388. {
  389. return 0;
  390. }
  391. strErr.LoadString(IDS_NO_DBCS);
  392. strErr += L"\r\n\r\n";
  393. strErr += pOFNotify->lpOFN->lpstrFile;
  394. AppMessageBox(NULL,strErr,NULL,MB_ICONSTOP|MB_OK);
  395. ::SetWindowLongPtr(hdlg, DWLP_MSGRESULT, TRUE);
  396. return 1;
  397. }
  398. }
  399. return 0;
  400. }
  401. //+--------------------------------------------------------------------------
  402. //
  403. // Method: OnOpenDataBase()
  404. //
  405. // Synopsis: Picks a new database for SAV to work on
  406. //
  407. //---------------------------------------------------------------------------
  408. HRESULT
  409. CComponentDataImpl::OnOpenDataBase()
  410. {
  411. //
  412. // Find the desired new database
  413. //
  414. CString strDefExtension;
  415. CString strFilter;
  416. CString strOfnTitle;
  417. CString strDir;
  418. strDefExtension.LoadString(IDS_DEFAULT_DB_EXTENSION);
  419. strFilter.LoadString(IDS_DB_FILTER);
  420. strOfnTitle.LoadString(IDS_OPEN_DB_OFN_TITLE);
  421. LPTSTR pszDir = NULL;
  422. //
  423. // Build a working directory for this object,
  424. //
  425. if (GetWorkingDir( GWD_OPEN_DATABASE, &pszDir ) )
  426. {
  427. strDir = pszDir;
  428. LocalFree(pszDir);
  429. pszDir = NULL;
  430. }
  431. WCHAR szFile[MAX_PATH];
  432. ::ZeroMemory (szFile, MAX_PATH * sizeof(WCHAR));
  433. OPENFILENAME ofn;
  434. ::ZeroMemory (&ofn, sizeof (OPENFILENAME));
  435. ofn.lStructSize = sizeof (OPENFILENAME);
  436. ofn.hwndOwner = m_hwndParent;
  437. //HINSTANCE hInstance;
  438. // Translate filter into commdlg format (lots of \0)
  439. LPTSTR szFilter = strFilter.GetBuffer(0); // modify the buffer in place
  440. // MFC delimits with '|' not '\0'
  441. LPTSTR pch = szFilter;
  442. while ((pch = _tcschr(pch, '|')) != NULL)
  443. *pch++ = '\0';
  444. // do not call ReleaseBuffer() since the string contains '\0' characters
  445. ofn.lpstrFilter = szFilter;
  446. ofn.lpstrFile = szFile;
  447. ofn.nMaxFile = MAX_PATH; //Raid #567739, yanggao, 4/4/2002.
  448. ofn.lpstrInitialDir = (PCWSTR) strDir;
  449. ofn.lpstrTitle = strOfnTitle;
  450. ofn.Flags = OFN_HIDEREADONLY| // Don't show the read only prompt
  451. OFN_SHAREAWARE|
  452. OFN_NOREADONLYRETURN|
  453. OFN_EXPLORER | // Explorer style dialog;
  454. OFN_DONTADDTORECENT|
  455. OFN_ENABLEHOOK;
  456. ofn.lpstrDefExt = (PCWSTR) strDefExtension;
  457. ofn.lpfnHook = OFNHookProc;
  458. if ( GetOpenFileName (&ofn) )
  459. {
  460. //
  461. // Set the working directory of the database.
  462. //
  463. pszDir = szFile;
  464. GetWorkingDir( GWD_OPEN_DATABASE, &pszDir, TRUE, TRUE );
  465. if( IsSystemDatabase( ofn.lpstrFile /*fo.GetPathName()*/) )
  466. {
  467. AfxMessageBox( IDS_CANT_OPEN_SYSTEM_DB, MB_OK);
  468. return S_FALSE;
  469. }
  470. SadName = ofn.lpstrFile; //fo.GetPathName();
  471. SetErroredLogFile(NULL);
  472. //
  473. // If new database doesn't exist then ask for a configuration to import
  474. //
  475. DWORD dwAttr = GetFileAttributes(SadName);
  476. if (0xFFFFFFFF == dwAttr)
  477. {
  478. SCESTATUS sceStatus = SCESTATUS_SUCCESS;
  479. //
  480. // New database, so assign a configuration
  481. //
  482. if( OnAssignConfiguration( &sceStatus ) == S_FALSE)
  483. {
  484. //
  485. // If the user decides to cancel the import of a configuration, then,
  486. // we need to unload the sad information and display the correct
  487. // error message. Set the sad errored to PROFILE_NOT_FOUND so error,
  488. // is correct. There is no need to call LoadSadinfo.
  489. //
  490. UnloadSadInfo();
  491. if( sceStatus != SCESTATUS_SUCCESS )
  492. SadErrored = sceStatus;
  493. else
  494. SadErrored = SCESTATUS_PROFILE_NOT_FOUND;
  495. if(m_AnalFolder)
  496. {
  497. m_pConsole->SelectScopeItem(m_AnalFolder->GetScopeItem()->ID);
  498. }
  499. SadName.Empty(); //Raid #459576, Yang Gao, 8/29/2001
  500. return S_OK;
  501. }
  502. }
  503. //
  504. // Invalidiate currently open database
  505. //
  506. RefreshSadInfo();
  507. return S_OK;
  508. }
  509. else
  510. {
  511. DWORD dwErr = CommDlgExtendedError();
  512. }
  513. return S_FALSE;
  514. }
  515. //+--------------------------------------------------------------------------
  516. //
  517. // Method: OnNewDataBase()
  518. //
  519. // Synopsis: Picks a new database for SAV to work on
  520. //
  521. //---------------------------------------------------------------------------
  522. HRESULT
  523. CComponentDataImpl::OnNewDatabase()
  524. {
  525. //
  526. // Find the desired new database
  527. //
  528. CString strDefExtension;
  529. CString strFilter;
  530. CString strOfnTitle;
  531. CWnd cwndParent;
  532. strDefExtension.LoadString(IDS_DEFAULT_DB_EXTENSION);
  533. strFilter.LoadString(IDS_DB_FILTER);
  534. strOfnTitle.LoadString(IDS_NEW_DB_OFN_TITLE);
  535. // Translate filter into commdlg format (lots of \0)
  536. LPTSTR szFilter = strFilter.GetBuffer(0); // modify the buffer in place
  537. LPTSTR pch = szFilter;
  538. // MFC delimits with '|' not '\0'
  539. while ((pch = _tcschr(pch, '|')) != NULL)
  540. *pch++ = '\0';
  541. // do not call ReleaseBuffer() since the string contains '\0' characters
  542. OPENFILENAME ofn;
  543. ::ZeroMemory (&ofn, sizeof (OPENFILENAME));
  544. ofn.lStructSize = sizeof(OPENFILENAME);
  545. ofn.lpstrFilter = szFilter;
  546. ofn.lpstrFile = SadName.GetBuffer(MAX_PATH);
  547. ofn.nMaxFile = MAX_PATH;
  548. ofn.lpstrDefExt = strDefExtension,
  549. ofn.hwndOwner = m_hwndParent;
  550. ofn.Flags = OFN_HIDEREADONLY |
  551. OFN_SHAREAWARE |
  552. OFN_EXPLORER |
  553. OFN_DONTADDTORECENT;
  554. ofn.lpstrTitle = strOfnTitle;
  555. if (GetOpenFileName(&ofn))
  556. {
  557. PVOID pHandle = NULL;
  558. SadName.ReleaseBuffer();
  559. //
  560. // If new database doesn't exist then ask for a configuration to import
  561. //
  562. DWORD dwAttr = GetFileAttributes(SadName);
  563. if (0xFFFFFFFF == dwAttr)
  564. {
  565. //
  566. // New database, so assign a configuration
  567. //
  568. SCESTATUS sceStatus;
  569. OnAssignConfiguration(&sceStatus);
  570. }
  571. //
  572. // Invalidiate currently open database
  573. //
  574. SetErroredLogFile(NULL);
  575. RefreshSadInfo();
  576. return S_OK;
  577. }
  578. return S_FALSE;
  579. }
  580. //+--------------------------------------------------------------------------
  581. //
  582. // Method: OnAssignConfiguration()
  583. //
  584. // Synopsis: Assigns a configuration template to SAV's currently selected
  585. // database
  586. //
  587. //---------------------------------------------------------------------------
  588. HRESULT
  589. CComponentDataImpl::OnAssignConfiguration( SCESTATUS *pSceStatus )
  590. {
  591. //
  592. //Currently pSceStatus is only used for passing back the error
  593. //when user presses cancel on select template diaglog.
  594. //
  595. // Find the desired new database
  596. //
  597. CString strDefExtension; // Default extension
  598. CString strCurFile;
  599. CString strFilter; // Extension filter
  600. CString strOfnTitle;
  601. CWnd cwndParent;
  602. BOOL bIncremental;
  603. SCESTATUS status;
  604. HKEY hKey; // HKEY of TemplateUsed.
  605. DWORD dwBufSize = 0; // Size of szTemplateUsed in bytes
  606. DWORD dwType = 0; // Type of registry item, return by query
  607. DWORD dwStatus;
  608. *pSceStatus = 0;
  609. //
  610. // Display a warning and give a chance to cancel
  611. // if they try to configure a non-system database
  612. //
  613. if (IsSystemDatabase(SadName))
  614. {
  615. BOOL bImportAnyway;
  616. bImportAnyway = AfxMessageBox(IDS_IMPORT_WARNING,MB_OKCANCEL);
  617. if (IDCANCEL == bImportAnyway)
  618. {
  619. return S_FALSE;
  620. }
  621. }
  622. strDefExtension.LoadString(IDS_PROFILE_DEF_EXT);
  623. strFilter.LoadString(IDS_PROFILE_FILTER);
  624. strOfnTitle.LoadString(IDS_ASSIGN_CONFIG_OFN_TITLE);
  625. //
  626. // Get the last directory used by templates.
  627. //
  628. LPTSTR pszDir = NULL;
  629. if(strCurFile.IsEmpty())
  630. {
  631. if (GetWorkingDir( GWD_EXPORT_TEMPLATE, &pszDir ) )
  632. {
  633. strCurFile = pszDir;
  634. LocalFree(pszDir);
  635. pszDir = NULL;
  636. }
  637. }
  638. strCurFile += TEXT("\\*.");
  639. strCurFile += strDefExtension;
  640. cwndParent.Attach(m_hwndParent);
  641. CAssignConfiguration ac(TRUE, // File Open, not file save
  642. strDefExtension, // Default Extension
  643. strCurFile, // Initial File Name == current DB
  644. OFN_HIDEREADONLY| // Don't show the read only prompt
  645. OFN_EXPLORER| // Explorer style dialog
  646. OFN_ENABLETEMPLATE| // custom template
  647. OFN_SHAREAWARE| // We're not going to need exclusive
  648. OFN_DONTADDTORECENT|
  649. OFN_FILEMUSTEXIST, // The Template must exist for us to assign it. Raid #535787, yanggao.
  650. strFilter, // Filter for allowed extensions
  651. &cwndParent); // Dialog's Parent window
  652. cwndParent.Detach();
  653. ac.m_ofn.lpstrTitle = strOfnTitle.GetBuffer(1);
  654. ac.m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_ASSIGN_CONFIG_CHECK);
  655. if (IDOK == ac.DoModal())
  656. {
  657. CThemeContextActivator activator;
  658. strCurFile = ac.GetPathName();
  659. bIncremental = ac.m_bIncremental;
  660. //
  661. // Set the working dir to this file.
  662. //
  663. pszDir = strCurFile.GetBuffer(0);
  664. GetWorkingDir( GWD_IMPORT_TEMPLATE, &pszDir, TRUE, TRUE);
  665. strCurFile.ReleaseBuffer();
  666. CWaitCursor wc;
  667. //
  668. // Unload the sad info before we choose to do an import.
  669. //
  670. UnloadSadInfo();
  671. status = AssignTemplate(
  672. strCurFile.IsEmpty() ? NULL:(LPCTSTR)strCurFile,
  673. SadName,
  674. bIncremental
  675. );
  676. if (SCESTATUS_SUCCESS != status)
  677. {
  678. CString strErr;
  679. MyFormatResMessage(status,IDS_IMPORT_FAILED,NULL,strErr);
  680. AfxMessageBox(strErr);
  681. //
  682. // We don't know if the database is still OK to read so open it anyways.
  683. //
  684. LoadSadInfo(TRUE);
  685. if( SCESTATUS_SPECIAL_ACCOUNT == SadErrored )//Raid #589139, DCR, yanggao, 4/12/2002.
  686. *pSceStatus = SadErrored;
  687. return S_FALSE;
  688. }
  689. //remove RefreshSadInfo() because we can't do it two times. The second call will delete the file
  690. //created by first call. Raid#668551, yanggao. 8/26/2002
  691. return S_OK;
  692. }
  693. *pSceStatus = SCESTATUS_NO_TEMPLATE_GIVEN;
  694. return S_FALSE;
  695. }
  696. //+--------------------------------------------------------------------------
  697. //
  698. // Method: OnSecureWizard()
  699. //
  700. // Synopsis: Launch the secure wizard (registered)
  701. //
  702. //---------------------------------------------------------------------------
  703. HRESULT
  704. CComponentDataImpl::OnSecureWizard()
  705. {
  706. HRESULT hr=S_FALSE;
  707. PWSTR pstrWizardName=NULL;
  708. if ( GetSecureWizardName(&pstrWizardName, NULL) )
  709. {
  710. PROCESS_INFORMATION ProcInfo;
  711. STARTUPINFO StartInfo;
  712. BOOL fOk;
  713. RtlZeroMemory(&StartInfo,sizeof(StartInfo));
  714. StartInfo.cb = sizeof(StartInfo);
  715. StartInfo.dwFlags = STARTF_USESHOWWINDOW;
  716. StartInfo.wShowWindow = (WORD)SW_SHOWNORMAL;
  717. //This is a safe usage if pstrWizardName is a full path for security wizard.
  718. fOk = CreateProcess(pstrWizardName, NULL,
  719. NULL, NULL, FALSE,
  720. 0,
  721. NULL,
  722. NULL,
  723. &StartInfo,
  724. &ProcInfo
  725. );
  726. if ( fOk )
  727. {
  728. ::CloseHandle(ProcInfo.hProcess);
  729. ::CloseHandle(ProcInfo.hThread);
  730. hr = S_OK;
  731. }
  732. LocalFree(pstrWizardName);
  733. }
  734. return hr;
  735. }
  736. //+--------------------------------------------------------------------------
  737. //
  738. // Method: OnSaveConfiguration()
  739. //
  740. // Synopsis: Saves the assigned computer template to an INF file
  741. //
  742. //---------------------------------------------------------------------------
  743. HRESULT
  744. CComponentDataImpl::OnSaveConfiguration()
  745. {
  746. //
  747. // Find the desired new database
  748. //
  749. CString strDefExtension;
  750. CString strFilter;
  751. CWnd cwndParent;
  752. CString strDefName;
  753. CString strName;
  754. SCESTATUS status = SCESTATUS_SUCCESS;
  755. CString strOfnTitle;
  756. strDefExtension.LoadString(IDS_PROFILE_DEF_EXT);
  757. strFilter.LoadString(IDS_PROFILE_FILTER);
  758. strOfnTitle.LoadString(IDS_EXPORT_CONFIG_OFN_TITLE);
  759. //
  760. // Get the working directory for INF files.
  761. //
  762. LPTSTR pszDir = NULL;
  763. if( GetWorkingDir( GWD_EXPORT_TEMPLATE, &pszDir ) )
  764. {
  765. strDefName = pszDir;
  766. LocalFree(pszDir);
  767. pszDir = NULL;
  768. }
  769. strDefName += TEXT("\\*.");
  770. strDefName += strDefExtension;
  771. // Translate filter into commdlg format (lots of \0)
  772. LPTSTR szFilter = strFilter.GetBuffer(0); // modify the buffer in place
  773. LPTSTR pch = szFilter;
  774. // MFC delimits with '|' not '\0'
  775. while ((pch = _tcschr(pch, '|')) != NULL)
  776. *pch++ = '\0';
  777. // do not call ReleaseBuffer() since the string contains '\0' characters
  778. OPENFILENAME ofn;
  779. ::ZeroMemory (&ofn, sizeof (OPENFILENAME));
  780. ofn.lStructSize = sizeof(OPENFILENAME);
  781. ofn.lpstrFilter = szFilter;
  782. ofn.lpstrFile = strDefName.GetBuffer(MAX_PATH),
  783. ofn.nMaxFile = MAX_PATH;
  784. ofn.lpstrDefExt = strDefExtension,
  785. ofn.hwndOwner = m_hwndParent;
  786. ofn.Flags = OFN_HIDEREADONLY |
  787. OFN_SHAREAWARE |
  788. OFN_EXPLORER |
  789. OFN_DONTADDTORECENT|OFN_ENABLEHOOK;
  790. ofn.lpstrTitle = strOfnTitle;
  791. ofn.lpfnHook = OFNHookProc; //Raid #567750, yanggao.
  792. if (GetSaveFileName(&ofn))
  793. {
  794. strDefName.ReleaseBuffer();
  795. strName = ofn.lpstrFile;
  796. //
  797. // Set the working directory for inf files.
  798. //
  799. pszDir = strName.GetBuffer(0);
  800. GetWorkingDir( GWD_EXPORT_TEMPLATE, &pszDir, TRUE, TRUE );
  801. strName.ReleaseBuffer();
  802. //
  803. // Generate new inf file
  804. //
  805. status = SceCopyBaseProfile(GetSadHandle(),
  806. SCE_ENGINE_SCP,
  807. (LPTSTR)(LPCTSTR)strName,
  808. AREA_ALL,
  809. NULL );
  810. if (SCESTATUS_SUCCESS != status)
  811. {
  812. CString str;
  813. CString strErr;
  814. MyFormatMessage( status, NULL, NULL, strErr); //Raid #621091, yanggao
  815. AfxFormatString2(str,IDS_EXPORT_FAILED,strName,strErr);
  816. AfxMessageBox(str);
  817. return S_FALSE;
  818. }
  819. return S_OK;
  820. }
  821. return S_FALSE;
  822. }
  823. //+--------------------------------------------------------------------------
  824. //
  825. // Method: OnExportPolicy()
  826. //
  827. // Synopsis: This function exports either the effective or local table
  828. // from the system security database to a file.
  829. //
  830. // The function asks the user for a file through the CFileOpen
  831. // class, then writes out the contents of either the effective
  832. // or local policy table in the system database.
  833. //
  834. // Arguments: [bEffective] - Either to export the effect or local table.
  835. //
  836. // Returns: S_OK - if everything went well.
  837. // S_FALSE - something failed.
  838. //---------------------------------------------------------------------------
  839. HRESULT
  840. CComponentDataImpl::OnExportPolicy( BOOL bEffective )
  841. {
  842. CString strDefExtension;
  843. CString strFilter;
  844. CString strOfnTitle;
  845. CString strDefName;
  846. CString strName;
  847. DWORD dwErr = 0;
  848. BOOL bCopySuccess = FALSE;
  849. //Raid #604879, yanggao, 4/24/2002
  850. PEDITTEMPLATE pTemplateInfo = NULL;
  851. AREA_INFORMATION area = AREA_SECURITY_POLICY|AREA_PRIVILEGES;
  852. if( m_Mode == SCE_MODE_LOCAL_COMPUTER )
  853. {
  854. strName = GT_LOCAL_POLICY;
  855. if (!SadHandle || ERROR_SUCCESS != SadErrored )
  856. {
  857. LoadSadInfo(FALSE);
  858. if( !SadHandle || ERROR_SUCCESS != SadErrored )
  859. return S_FALSE;
  860. }
  861. pTemplateInfo = GetTemplate(strName,area,&dwErr);
  862. if( !pTemplateInfo )
  863. {
  864. return S_FALSE;
  865. }
  866. }
  867. strDefExtension.LoadString(IDS_PROFILE_DEF_EXT);
  868. strFilter.LoadString(IDS_PROFILE_FILTER);
  869. strOfnTitle.LoadString(IDS_EXPORT_POLICY_OFN_TITLE);
  870. //
  871. // Get the working directory for locations.
  872. //
  873. LPTSTR pszDir = NULL;
  874. if( GetWorkingDir( GWD_EXPORT_TEMPLATE, &pszDir ) )
  875. {
  876. strDefName = pszDir;
  877. LocalFree(pszDir);
  878. pszDir = NULL;
  879. }
  880. strDefName += TEXT("\\*.");
  881. strDefName += strDefExtension;
  882. // Translate filter into commdlg format (lots of \0)
  883. LPTSTR szFilter = strFilter.GetBuffer(0); // modify the buffer in place
  884. LPTSTR pch = szFilter;
  885. // MFC delimits with '|' not '\0'
  886. while ((pch = _tcschr(pch, '|')) != NULL)
  887. *pch++ = '\0';
  888. // do not call ReleaseBuffer() since the string contains '\0' characters
  889. OPENFILENAME ofn;
  890. ::ZeroMemory (&ofn, sizeof (OPENFILENAME));
  891. ofn.lStructSize = sizeof(OPENFILENAME);
  892. ofn.lpstrFilter = szFilter;
  893. ofn.lpstrFile = strDefName.GetBuffer(MAX_PATH),
  894. ofn.nMaxFile = MAX_PATH;
  895. ofn.lpstrDefExt = strDefExtension,
  896. ofn.hwndOwner = m_hwndParent;
  897. ofn.Flags = OFN_HIDEREADONLY |
  898. OFN_SHAREAWARE |
  899. OFN_EXPLORER |
  900. OFN_DONTADDTORECENT;
  901. ofn.lpstrTitle = strOfnTitle;
  902. if (GetSaveFileName(&ofn))
  903. {
  904. strDefName.ReleaseBuffer();
  905. strName = ofn.lpstrFile;
  906. //
  907. // Set the working directory for locations.
  908. //
  909. pszDir = strName.GetBuffer(0);
  910. GetWorkingDir( GWD_EXPORT_TEMPLATE, &pszDir, TRUE, TRUE );
  911. strName.ReleaseBuffer();
  912. //
  913. // Make sure we can make the file.
  914. //
  915. dwErr = FileCreateError( strName, 0 ); //Raid #prefast
  916. if(dwErr == IDNO)
  917. {
  918. return S_FALSE;
  919. }
  920. //
  921. // Generate the template
  922. //
  923. SCESTATUS sceStatus;
  924. CWaitCursor wc;
  925. //Raid #604879, yanggao, 4/24/2002
  926. sceStatus = SceWriteSecurityProfileInfo(strName.GetBuffer(0),
  927. area,
  928. pTemplateInfo->pTemplate,
  929. NULL);
  930. strName.ReleaseBuffer();
  931. if(sceStatus != SCESTATUS_SUCCESS)
  932. {
  933. //
  934. // Display the error message
  935. //
  936. DWORD dwError = SceStatusToDosError( sceStatus );
  937. CString strErr;
  938. MyFormatMessage( sceStatus, NULL, NULL, strErr );
  939. strErr += strName;
  940. AfxMessageBox( strErr, MB_ICONEXCLAMATION | MB_OK );
  941. return S_FALSE;
  942. }
  943. return S_OK;
  944. }
  945. return S_FALSE;
  946. }
  947. //+--------------------------------------------------------------------------------------------
  948. // CComponentDataImpl::OnImportPolicy
  949. //
  950. // Import policy opens a file open dialog box in which the user is allowed to choose
  951. // a security configuration INF file to import into security policy.
  952. //
  953. //
  954. // Arguments: [pDataObject] - The data object associated with the folder calling
  955. // this function.
  956. // Returns: S_OK - Import was successful.
  957. // S_FALSE - Something went wrong.
  958. //---------------------------------------------------------------------------------------------
  959. HRESULT
  960. CComponentDataImpl::OnImportPolicy(LPDATAOBJECT pDataObject)
  961. {
  962. CString strDefExtension;
  963. CString strFilter;
  964. CString strOfnTitle;
  965. CWnd cwndParent;
  966. CString strDefName;
  967. CString strName;
  968. strDefExtension.LoadString(IDS_PROFILE_DEF_EXT);
  969. strFilter.LoadString(IDS_PROFILE_FILTER);
  970. strOfnTitle.LoadString(IDS_IMPORT_POLICY_OFN_TITLE);
  971. LPTSTR pszDir = NULL;
  972. if( GetWorkingDir( GWD_IMPORT_TEMPLATE, &pszDir ) )
  973. {
  974. strDefName = pszDir;
  975. LocalFree(pszDir);
  976. pszDir = NULL;
  977. }
  978. strDefName += TEXT("\\*.");
  979. strDefName += strDefExtension;
  980. cwndParent.Attach(m_hwndParent);
  981. CAssignConfiguration fo(TRUE, // File open
  982. strDefExtension, // Default Extension
  983. strDefName, // Initial File Name == current DB
  984. OFN_HIDEREADONLY| // Don't show the read only prompt
  985. OFN_SHAREAWARE|
  986. OFN_EXPLORER| // Explorer style dialog
  987. OFN_ENABLETEMPLATE| // custom template
  988. OFN_DONTADDTORECENT|
  989. OFN_PATHMUSTEXIST, // The Template must exist for us to assign it.
  990. strFilter, // Filter for allowed extensions
  991. &cwndParent); // Dialog's Parent window
  992. cwndParent.Detach();
  993. fo.m_ofn.lpstrTitle = strOfnTitle.GetBuffer(1);
  994. fo.m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_ASSIGN_CONFIG_CHECK);
  995. CThemeContextActivator activator;
  996. if (IDOK == fo.DoModal())
  997. {
  998. PVOID pHandle = NULL;
  999. BOOL bIncremental = fo.m_bIncremental;
  1000. strName = fo.GetPathName();
  1001. pszDir = strName.GetBuffer(0);
  1002. GetWorkingDir( GWD_IMPORT_TEMPLATE, &pszDir, TRUE, TRUE );
  1003. strName.ReleaseBuffer();
  1004. CWaitCursor wc;
  1005. CEditTemplate *pet = NULL; //Raid #Prefast
  1006. //
  1007. // pet will be freed in m_Templates in DeleteTemplate
  1008. //
  1009. pet = GetTemplate(strName,AREA_ALL);
  1010. CString strErr;
  1011. CString strSCE;
  1012. int ret=0;
  1013. if ( pet == NULL )
  1014. {
  1015. //
  1016. // this is an invalid template or something is wrong reading the data out
  1017. //
  1018. strSCE.LoadString(IDS_EXTENSION_NAME);
  1019. strErr.Format(IDS_IMPORT_POLICY_INVALID,strName);
  1020. m_pConsole->MessageBox(strErr,strSCE,MB_OK,&ret);
  1021. return S_FALSE;
  1022. }
  1023. else
  1024. {
  1025. // Bug 437891, Yang Gao, 7/20/2001
  1026. // Create a new template there if there isn't one already
  1027. //
  1028. PSCE_PROFILE_INFO spi = NULL;
  1029. if( CreateNewProfile(m_szSingleTemplateName,&spi) )
  1030. {
  1031. if ( ( bIncremental &&
  1032. (SCESTATUS_SUCCESS != SceAppendSecurityProfileInfo(m_szSingleTemplateName,
  1033. AREA_ALL,
  1034. pet->pTemplate,
  1035. NULL))) ||
  1036. (!bIncremental && !CopyFile(strName,m_szSingleTemplateName,FALSE) ) ) //This is a safe usage.
  1037. {
  1038. //
  1039. // Import Failed
  1040. //
  1041. strSCE.LoadString(IDS_EXTENSION_NAME);
  1042. strErr.Format(IDS_IMPORT_POLICY_FAIL,strName);
  1043. m_pConsole->MessageBox(strErr,strSCE,MB_OK,&ret);
  1044. DeleteTemplate(strName);
  1045. return S_FALSE;
  1046. }
  1047. if ( this->m_pNotifier && pet->QueryPolicy() ) //Raid #522006, 2/28/2002, yanggao
  1048. {
  1049. this->m_pNotifier->RefreshPolicy();
  1050. }
  1051. }
  1052. else
  1053. {
  1054. //
  1055. // Import Failed
  1056. //
  1057. strSCE.LoadString(IDS_EXTENSION_NAME);
  1058. strErr.Format(IDS_IMPORT_POLICY_FAIL,strName);
  1059. m_pConsole->MessageBox(strErr,strSCE,MB_OK,&ret);
  1060. DeleteTemplate(strName);
  1061. return S_FALSE;
  1062. }
  1063. DeleteTemplate(strName);
  1064. }
  1065. DeleteTemplate(m_szSingleTemplateName);
  1066. //
  1067. // Update the window.
  1068. //
  1069. pet = GetTemplate(m_szSingleTemplateName);
  1070. if(pet)
  1071. {
  1072. DWORD dwErr = pet->RefreshTemplate();
  1073. if ( 0 != dwErr )
  1074. {
  1075. CString strErr;
  1076. MyFormatResMessage (SCESTATUS_SUCCESS, dwErr, NULL, strErr);
  1077. AfxMessageBox(strErr);
  1078. return S_FALSE;
  1079. }
  1080. }
  1081. RefreshAllFolders();
  1082. return S_OK;
  1083. }
  1084. return S_FALSE;
  1085. }
  1086. //+--------------------------------------------------------------------------------------------
  1087. // CComponentDataImpl::OnImportLocalPolicy
  1088. //
  1089. // Import policy opens a file open dialog box in which the user is allowed to choose
  1090. // a security configuration INF file to import into local policy.
  1091. //
  1092. // The function asks for the file name then calls SceConfigureSystem() with the
  1093. // SCE_NO_CONFIG option. This imports the specifide file into the local policy
  1094. // database.
  1095. //
  1096. // After the database is updated, we refresh the local policy template held in memory.
  1097. //
  1098. // Arguments: [pDataObject] - The data object associated with the folder calling
  1099. // this function.
  1100. // Returns: S_OK - Import was successful.
  1101. // S_FALSE - Something went wrong.
  1102. //---------------------------------------------------------------------------------------------
  1103. HRESULT
  1104. CComponentDataImpl::OnImportLocalPolicy(LPDATAOBJECT pDataObject)
  1105. {
  1106. CString strDefExtension;
  1107. CString strFilter;
  1108. CString strOfnTitle;
  1109. CString strDefName;
  1110. CString strName;
  1111. DWORD dwErr = 0;
  1112. SCESTATUS sceStatus = SCESTATUS_SUCCESS;
  1113. CString strErr;
  1114. CString strY;
  1115. strDefExtension.LoadString(IDS_PROFILE_DEF_EXT);
  1116. strFilter.LoadString(IDS_PROFILE_FILTER);
  1117. strOfnTitle.LoadString(IDS_IMPORT_POLICY_OFN_TITLE);
  1118. // Translate filter into commdlg format (lots of \0)
  1119. LPTSTR szFilter = strFilter.GetBuffer(0); // modify the buffer in place
  1120. LPTSTR pch = szFilter;
  1121. // MFC delimits with '|' not '\0'
  1122. while ((pch = _tcschr(pch, TEXT('|'))) != NULL)
  1123. *pch++ = TEXT('\0');
  1124. // do not call ReleaseBuffer() since the string contains '\0' characters
  1125. LPTSTR pszDir = NULL;
  1126. if( GetWorkingDir( GWD_IMPORT_TEMPLATE, &pszDir ) )
  1127. {
  1128. strDefName = pszDir;
  1129. LocalFree(pszDir);
  1130. pszDir = NULL;
  1131. }
  1132. strDefName += TEXT("\\*.");
  1133. strDefName += strDefExtension;
  1134. OPENFILENAME ofn;
  1135. ::ZeroMemory (&ofn, sizeof (OPENFILENAME));
  1136. ofn.lStructSize = sizeof(OPENFILENAME);
  1137. ofn.lpstrFilter = szFilter;
  1138. ofn.lpstrFile = strDefName.GetBuffer(MAX_PATH),
  1139. ofn.nMaxFile = MAX_PATH;
  1140. ofn.lpstrDefExt = strDefExtension,
  1141. ofn.hwndOwner = m_hwndParent;
  1142. ofn.Flags = OFN_HIDEREADONLY |
  1143. OFN_SHAREAWARE |
  1144. OFN_EXPLORER |
  1145. OFN_DONTADDTORECENT|
  1146. OFN_FILEMUSTEXIST; //Raid #535787, yanggao, 4/18/2002.
  1147. ofn.lpstrTitle = strOfnTitle;
  1148. if (GetOpenFileName(&ofn))
  1149. {
  1150. PVOID pHandle = NULL;
  1151. strName = ofn.lpstrFile;
  1152. pszDir = strName.GetBuffer(0);
  1153. GetWorkingDir( GWD_IMPORT_TEMPLATE, &pszDir, TRUE, TRUE );
  1154. strName.ReleaseBuffer();
  1155. CWaitCursor wc;
  1156. CEditTemplate *pet;
  1157. //Get error info
  1158. //Raid #502571, 12/18/2001, yanggao
  1159. pet = GetTemplate(strName,AREA_SECURITY_POLICY|AREA_PRIVILEGES, &dwErr);
  1160. if (!pet)
  1161. {
  1162. strErr.LoadString(dwErr);
  1163. AppMessageBox(NULL, strErr, NULL, MB_ICONSTOP|MB_OK);
  1164. return S_FALSE;
  1165. }
  1166. //
  1167. // We're going to alter this one so make sure it doesn't save out
  1168. //
  1169. pet->SetNoSave(TRUE);
  1170. //
  1171. // Remove entries that are set by domain policy since they'll be overwritten
  1172. // anyway and we don't allow setting things that'll be overwritten.
  1173. //
  1174. RemovePolicyEntries(pet);
  1175. //
  1176. // 120502 - SceUpdateSecurityProfile doesn't work with SCE_STRUCT_INF,
  1177. // but the AREA_SECURITY_POLICY and AREA_PRIVILEGES sections are
  1178. // compatable so we can call it SCE_ENGINE_SYSTEM, which will work.
  1179. //
  1180. SCETYPE origType = pet->pTemplate->Type;
  1181. pet->pTemplate->Type = SCE_ENGINE_SYSTEM;
  1182. sceStatus = SceUpdateSecurityProfile(NULL,
  1183. AREA_SECURITY_POLICY|AREA_PRIVILEGES,
  1184. pet->pTemplate,
  1185. SCE_UPDATE_SYSTEM
  1186. );
  1187. //
  1188. // Set the type back so the engine knows how to delete it properly
  1189. //
  1190. pet->pTemplate->Type = origType;
  1191. if (SCESTATUS_SUCCESS != sceStatus)
  1192. {
  1193. goto ret_error;
  1194. }
  1195. //
  1196. // Update the window.
  1197. //
  1198. pet = GetTemplate(GT_LOCAL_POLICY);
  1199. if(pet)
  1200. {
  1201. dwErr = pet->RefreshTemplate(); //Raid #prefast
  1202. if ( 0 != dwErr )
  1203. {
  1204. CString strErr;
  1205. MyFormatResMessage (SCESTATUS_SUCCESS, dwErr, NULL, strErr);
  1206. AfxMessageBox(strErr);
  1207. return S_FALSE;
  1208. }
  1209. }
  1210. RefreshAllFolders();
  1211. return S_OK;
  1212. }
  1213. strDefName.ReleaseBuffer();
  1214. return S_FALSE;
  1215. ret_error:
  1216. strDefName.ReleaseBuffer();
  1217. MyFormatMessage( sceStatus, NULL, NULL, strErr);
  1218. strErr += strName;
  1219. strErr += L"\n";
  1220. strErr += strY;
  1221. AfxMessageBox( strErr, MB_OK);
  1222. return S_FALSE;
  1223. }
  1224. HRESULT
  1225. CComponentDataImpl::OnAnalyze()
  1226. {
  1227. PEDITTEMPLATE pet;
  1228. //
  1229. // If the computer template has been changed then save it before
  1230. // we can inspect against it
  1231. //
  1232. pet = GetTemplate(GT_COMPUTER_TEMPLATE);
  1233. if (pet && pet->IsDirty())
  1234. {
  1235. pet->Save();
  1236. }
  1237. m_pUIThread->PostThreadMessage(SCEM_ANALYZE_PROFILE,(WPARAM)(LPCTSTR)SadName,(LPARAM)this);
  1238. return S_OK;
  1239. }
  1240. BOOL
  1241. CComponentDataImpl::RemovePolicyEntries(PEDITTEMPLATE pet)
  1242. {
  1243. PEDITTEMPLATE petPol = GetTemplate(GT_EFFECTIVE_POLICY);
  1244. if (!petPol)
  1245. {
  1246. return FALSE;
  1247. }
  1248. #define CD(X) if (petPol->pTemplate->X != SCE_NO_VALUE) { pet->pTemplate->X = SCE_NO_VALUE; };
  1249. CD(MinimumPasswordAge);
  1250. CD(MaximumPasswordAge);
  1251. CD(MinimumPasswordLength);
  1252. CD(PasswordComplexity);
  1253. CD(PasswordHistorySize);
  1254. CD(LockoutBadCount);
  1255. CD(ResetLockoutCount);
  1256. CD(LockoutDuration);
  1257. CD(RequireLogonToChangePassword);
  1258. CD(ForceLogoffWhenHourExpire);
  1259. CD(EnableAdminAccount);
  1260. CD(EnableGuestAccount);
  1261. // These members aren't declared in NT4
  1262. CD(ClearTextPassword);
  1263. CD(AuditDSAccess);
  1264. CD(AuditAccountLogon);
  1265. CD(LSAAnonymousNameLookup);
  1266. CD(MaximumLogSize[0]);
  1267. CD(MaximumLogSize[1]);
  1268. CD(MaximumLogSize[2]);
  1269. CD(AuditLogRetentionPeriod[0]);
  1270. CD(AuditLogRetentionPeriod[1]);
  1271. CD(AuditLogRetentionPeriod[2]);
  1272. CD(RetentionDays[0]);
  1273. CD(RetentionDays[1]);
  1274. CD(RetentionDays[2]);
  1275. CD(RestrictGuestAccess[0]);
  1276. CD(RestrictGuestAccess[1]);
  1277. CD(RestrictGuestAccess[2]);
  1278. CD(AuditSystemEvents);
  1279. CD(AuditLogonEvents);
  1280. CD(AuditObjectAccess);
  1281. CD(AuditPrivilegeUse);
  1282. CD(AuditPolicyChange);
  1283. CD(AuditAccountManage);
  1284. CD(AuditProcessTracking);
  1285. //
  1286. // These two are strings rather than DWORDs
  1287. //
  1288. if (petPol->pTemplate->NewAdministratorName && pet->pTemplate->NewAdministratorName)
  1289. {
  1290. LocalFree(pet->pTemplate->NewAdministratorName);
  1291. pet->pTemplate->NewAdministratorName = NULL;
  1292. }
  1293. if (petPol->pTemplate->NewGuestName && pet->pTemplate->NewGuestName)
  1294. {
  1295. LocalFree(pet->pTemplate->NewGuestName);
  1296. pet->pTemplate->NewGuestName = NULL;
  1297. }
  1298. #undef CD
  1299. //
  1300. // Clear privileges set in PetPol out of pet
  1301. //
  1302. SCE_PRIVILEGE_ASSIGNMENT *ppaPet = pet->pTemplate->OtherInfo.smp.pPrivilegeAssignedTo;
  1303. SCE_PRIVILEGE_ASSIGNMENT *ppaPol = petPol->pTemplate->OtherInfo.smp.pPrivilegeAssignedTo;
  1304. SCE_PRIVILEGE_ASSIGNMENT *ppaLast = NULL;
  1305. for(SCE_PRIVILEGE_ASSIGNMENT *ppa = ppaPol; ppa != NULL ; ppa = ppa->Next)
  1306. {
  1307. for (SCE_PRIVILEGE_ASSIGNMENT *ppa2 = ppaPet;
  1308. ppa2 != NULL;
  1309. ppaLast = ppa2, ppa2 = ppa2->Next) {
  1310. if (0 == lstrcmpi(ppa->Name,ppa2->Name))
  1311. {
  1312. if (ppaLast)
  1313. {
  1314. ppaLast->Next = ppa2->Next;
  1315. }
  1316. else
  1317. {
  1318. // Front of list
  1319. ppaPet = ppa2->Next;
  1320. }
  1321. ppa2->Next = NULL;
  1322. SceFreeMemory(ppa2,SCE_STRUCT_PRIVILEGE);
  1323. ppa2 = ppaLast;
  1324. //
  1325. // Found it, so don't bother checking the rest
  1326. //
  1327. break;
  1328. }
  1329. }
  1330. }
  1331. //
  1332. // Clear reg values set in PetPol out of pet
  1333. //
  1334. SCE_REGISTRY_VALUE_INFO *rvPet = pet->pTemplate->aRegValues;
  1335. SCE_REGISTRY_VALUE_INFO *rvPol = petPol->pTemplate->aRegValues;
  1336. for(DWORD i=0;i< petPol->pTemplate->RegValueCount;i++)
  1337. {
  1338. for (DWORD j=0;j<pet->pTemplate->RegValueCount;j++)
  1339. {
  1340. if (0 == lstrcmpi(rvPol[i].FullValueName,rvPet[j].FullValueName))
  1341. {
  1342. // Match. Delete Value from pet
  1343. if (rvPet[j].Value)
  1344. {
  1345. LocalFree(rvPet[j].Value);
  1346. rvPet[j].Value = NULL;
  1347. }
  1348. //
  1349. // Found it, so don't bother checking rest
  1350. //
  1351. break;
  1352. }
  1353. }
  1354. }
  1355. return TRUE;
  1356. }