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.

996 lines
30 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 2001.
  5. //
  6. // File: edittemp.cpp
  7. //
  8. // Contents: CEditTemplate class to handle editing of SCE's INF files
  9. //
  10. // History:
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "stdafx.h"
  14. #include "resource.h"
  15. #include "edittemp.h"
  16. #include "util.h"
  17. #include "snapmgr.h"
  18. #include <secedit.h>
  19. #include "wrapper.h"
  20. #include "wmihooks.h"
  21. #include <sceattch.h>
  22. #include <locale.h>
  23. //+--------------------------------------------------------------------------
  24. //
  25. // Method: AddService
  26. //
  27. // Synopsis: Adds a service attachment to the template
  28. //
  29. // Arguments: [szService] - [in] the name of the new service
  30. // [pPersistInfo] - [in] A pointer to the service extensions'
  31. // persistance interface
  32. //
  33. // Returns: TRUE if successfull, FALSE if either argument is null
  34. //
  35. // Modifies: m_Services
  36. //
  37. // History:
  38. //
  39. //---------------------------------------------------------------------------
  40. BOOL
  41. CEditTemplate::AddService(LPCTSTR szService, LPSCESVCATTACHMENTPERSISTINFO pPersistInfo) {
  42. if (!szService || !pPersistInfo) {
  43. return FALSE;
  44. }
  45. m_Services.SetAt(szService,pPersistInfo);
  46. return TRUE;
  47. }
  48. //+--------------------------------------------------------------------------
  49. //
  50. // Method: IsDirty
  51. //
  52. // Synopsis: Queries whether or not there is unsaved data in the template
  53. //
  54. // Returns: TRUE if there is unsaved information, FALSE otherwise
  55. //
  56. // Modifies:
  57. //
  58. // History:
  59. //
  60. //---------------------------------------------------------------------------
  61. BOOL
  62. CEditTemplate::IsDirty() {
  63. //
  64. // Some area is dirty
  65. //
  66. if (0 != m_AreaDirty) {
  67. return TRUE;
  68. }
  69. //
  70. // Loop through services until we find one that is dirty
  71. // or there are no more to check.
  72. //
  73. CString strService;
  74. LPSCESVCATTACHMENTPERSISTINFO pAttachPI;
  75. POSITION pos;
  76. pos = m_Services.GetStartPosition();
  77. while (pos) {
  78. m_Services.GetNextAssoc(pos,strService,pAttachPI);
  79. if (pAttachPI && (S_OK == pAttachPI->IsDirty(m_szInfFile))) {
  80. return TRUE;
  81. }
  82. }
  83. //
  84. // We didn't find anything dirty
  85. //
  86. return FALSE;
  87. }
  88. //+--------------------------------------------------------------------------
  89. //
  90. // Method: SetDirty
  91. //
  92. // Synopsis: Notify the template that some data within it has been changed.
  93. //
  94. // Returns: TRUE if successful, FALSE otherwise
  95. //
  96. // Modifies:
  97. //
  98. // History:
  99. //
  100. //---------------------------------------------------------------------------
  101. BOOL
  102. CEditTemplate::SetDirty(AREA_INFORMATION Area) {
  103. DWORD AreaDirtyOld;
  104. AreaDirtyOld = m_AreaDirty;
  105. m_AreaDirty |= Area;
  106. //
  107. // If the template is supposed to immediately save any changes then
  108. // do so.
  109. //
  110. if (QueryWriteThrough() && !m_bLocked) {
  111. SetWriteThroughDirty(TRUE);
  112. if (Save()) {
  113. //
  114. // #204628 - don't call PolicyChanged twiced when writing through
  115. // Call it in SetDirty and then skip it in Save, so we don't call it
  116. // once in SetDirty's call to Save and a second time when Save is called
  117. // on its own
  118. //
  119. // #204779 - call the notification window rather than directly calling
  120. // the IGPEInformation interface
  121. //
  122. if (m_pNotify && QueryPolicy()) {
  123. m_pNotify->RefreshPolicy();
  124. }
  125. } else {
  126. m_AreaDirty = AreaDirtyOld;
  127. return FALSE;
  128. }
  129. }
  130. return TRUE;
  131. }
  132. //+--------------------------------------------------------------------------------------
  133. // CEditTemplate::SetTemplateDefaults
  134. //
  135. // The caller will have to remove all memory objects used by this template if
  136. // this function
  137. // is called. Everything becomes NULL and nothing is freed.
  138. //+--------------------------------------------------------------------------------------
  139. void CEditTemplate::SetTemplateDefaults()
  140. {
  141. //
  142. // Local Policy Changes. Initialize everything to not changed
  143. //
  144. SCE_PROFILE_INFO *ppi = pTemplate;
  145. m_AreaLoaded = 0;
  146. m_AreaDirty = 0;
  147. if(!ppi){
  148. ppi = pTemplate = (PSCE_PROFILE_INFO) LocalAlloc(LPTR,sizeof(SCE_PROFILE_INFO));
  149. if (!pTemplate) {
  150. return;
  151. }
  152. }
  153. //
  154. // Must keep to type of this template.
  155. //
  156. SCETYPE dwType = ppi->Type;
  157. PSCE_KERBEROS_TICKET_INFO pKerberosInfo = ppi->pKerberosInfo;
  158. ZeroMemory( ppi, sizeof(SCE_PROFILE_INFO));
  159. ppi->Type = dwType;
  160. //
  161. // Set defaults to the rest of the template.
  162. //
  163. ppi->MinimumPasswordAge=SCE_NO_VALUE;
  164. ppi->MaximumPasswordAge=SCE_NO_VALUE;
  165. ppi->MinimumPasswordLength=SCE_NO_VALUE;
  166. ppi->PasswordComplexity=SCE_NO_VALUE;
  167. ppi->PasswordHistorySize=SCE_NO_VALUE;
  168. ppi->LockoutBadCount=SCE_NO_VALUE;
  169. ppi->ResetLockoutCount=SCE_NO_VALUE;
  170. ppi->LockoutDuration=SCE_NO_VALUE;
  171. ppi->RequireLogonToChangePassword=SCE_NO_VALUE;
  172. ppi->ForceLogoffWhenHourExpire=SCE_NO_VALUE;
  173. ppi->EnableAdminAccount=SCE_NO_VALUE;
  174. ppi->EnableGuestAccount=SCE_NO_VALUE;
  175. ppi->ClearTextPassword=SCE_NO_VALUE;
  176. ppi->LSAAnonymousNameLookup=SCE_NO_VALUE;
  177. for (int i=0;i<3;i++) {
  178. ppi->MaximumLogSize[i]=SCE_NO_VALUE;
  179. ppi->AuditLogRetentionPeriod[i]=SCE_NO_VALUE;
  180. ppi->RetentionDays[i]=SCE_NO_VALUE;
  181. ppi->RestrictGuestAccess[i]=SCE_NO_VALUE;
  182. }
  183. ppi->AuditSystemEvents=SCE_NO_VALUE;
  184. ppi->AuditLogonEvents=SCE_NO_VALUE;
  185. ppi->AuditObjectAccess=SCE_NO_VALUE;
  186. ppi->AuditPrivilegeUse=SCE_NO_VALUE;
  187. ppi->AuditPolicyChange=SCE_NO_VALUE;
  188. ppi->AuditAccountManage=SCE_NO_VALUE;
  189. ppi->AuditProcessTracking=SCE_NO_VALUE;
  190. ppi->AuditDSAccess=SCE_NO_VALUE;
  191. ppi->AuditAccountLogon=SCE_NO_VALUE;
  192. //
  193. // String values
  194. //
  195. ppi->NewAdministratorName=NULL;
  196. ppi->NewGuestName=NULL;
  197. //
  198. // registry values
  199. //
  200. ppi->RegValueCount= 0;
  201. ppi->aRegValues = NULL;
  202. //
  203. // Kerberos information, if it was created then set the values.
  204. //
  205. if(pKerberosInfo){
  206. pKerberosInfo->MaxTicketAge = SCE_NO_VALUE;
  207. pKerberosInfo->MaxRenewAge = SCE_NO_VALUE;
  208. pKerberosInfo->MaxServiceAge = SCE_NO_VALUE;
  209. pKerberosInfo->MaxClockSkew = SCE_NO_VALUE;
  210. pKerberosInfo->TicketValidateClient = SCE_NO_VALUE;
  211. ppi->pKerberosInfo = pKerberosInfo;
  212. }
  213. }
  214. //+--------------------------------------------------------------------------
  215. //
  216. // Method: Save
  217. //
  218. // Synopsis: Save the template to disk
  219. //
  220. // Arguments: [szName] - [in] [optional] the name of the INF file to save to
  221. //
  222. // Returns: TRUE if the save is successful, False otherwise
  223. //
  224. // Modifies: m_AreaDirty
  225. //
  226. // History:
  227. //
  228. //---------------------------------------------------------------------------
  229. BOOL
  230. CEditTemplate::Save(LPCTSTR szName) {
  231. DWORD AreaDirty;
  232. BOOL bSaveAs = FALSE;
  233. BOOL bSaveDescription = FALSE;
  234. setlocale(LC_ALL, ".OCP");
  235. SCESTATUS status = SCESTATUS_OTHER_ERROR;
  236. PSCE_ERROR_LOG_INFO errBuf = NULL;
  237. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  238. if (QueryNoSave()) {
  239. m_AreaDirty = 0;
  240. return TRUE;
  241. }
  242. AreaDirty = m_AreaDirty;
  243. //
  244. // If szName isn't given then default to m_szInfFile
  245. //
  246. if (!szName) {
  247. szName = m_szInfFile;
  248. //
  249. // We should never be able to get into a situation where
  250. // szName still isn't set, but just in case somebody called
  251. // us without szName or m_szInfFile
  252. //
  253. ASSERT(szName);
  254. if (!szName) {
  255. return FALSE;
  256. }
  257. } else {
  258. if (lstrcmp(szName,m_szInfFile) != 0) {
  259. //
  260. // Saving to a different name (Save As)
  261. //
  262. //
  263. // Make sure the path to that filename exists:
  264. //
  265. if (SCESTATUS_SUCCESS != SceCreateDirectory( m_szInfFile, FALSE, NULL )) {
  266. return FALSE;
  267. }
  268. AreaDirty = AREA_ALL|AREA_DESCRIPTION;
  269. bSaveAs = TRUE;
  270. }
  271. }
  272. if (AreaDirty & AREA_DESCRIPTION) {
  273. bSaveDescription = TRUE;
  274. AreaDirty &= ~AREA_DESCRIPTION;
  275. if (!AreaDirty) {
  276. //
  277. // Make sure we have something else to save and
  278. // create the file. AREA_SECURITY_POLICY is cheap.
  279. //
  280. AreaDirty |= AREA_SECURITY_POLICY;
  281. }
  282. //
  283. // Bug 365485 - make sure we only write this to an already
  284. // existing temp file so that we don't accidentally create
  285. // an ansi one instead of unicode. We can easily do this
  286. // by writing the description section last since we can
  287. // depend on the engine getting the rest right
  288. //
  289. }
  290. if (AreaDirty) {
  291. //
  292. // Save the dirty areas of the profile
  293. if (_wcsicmp(GT_COMPUTER_TEMPLATE,szName) == 0) { //Prefast warning 400: Yields unexpected results in non-English locales. Comments: It is not localizable.
  294. if (m_hProfile) {
  295. //
  296. // do not update object area
  297. //
  298. status = SceUpdateSecurityProfile(m_hProfile,
  299. AreaDirty & ~(AREA_FILE_SECURITY | AREA_REGISTRY_SECURITY | AREA_DS_OBJECTS),
  300. pTemplate,
  301. 0
  302. );
  303. }
  304. ASSERT(m_pCDI);
  305. if (m_pCDI) {
  306. m_pCDI->EngineCommitTransaction();
  307. }
  308. } else if (lstrcmp(GT_LOCAL_POLICY_DELTA,szName) == 0) {
  309. //
  310. // Save Changes only to Local Policy
  311. //
  312. status = SceUpdateSecurityProfile(NULL,
  313. AreaDirty & ~(AREA_FILE_SECURITY | AREA_REGISTRY_SECURITY | AREA_DS_OBJECTS),
  314. pTemplate,
  315. SCE_UPDATE_SYSTEM
  316. );
  317. SetTemplateDefaults();
  318. if (!bSaveAs) {
  319. m_AreaDirty = 0;
  320. m_AreaLoaded = 0;
  321. }
  322. } else if ((lstrcmp(GT_LAST_INSPECTION,szName) != 0) &&
  323. (lstrcmp(GT_RSOP_TEMPLATE,szName) != 0) &&
  324. (lstrcmp(GT_LOCAL_POLICY,szName) != 0) &&
  325. (lstrcmp(GT_EFFECTIVE_POLICY,szName) != 0)) {
  326. status = SceWriteSecurityProfileInfo(szName,
  327. AreaDirty,
  328. pTemplate,
  329. &errBuf);
  330. } else {
  331. //
  332. // No need (or way) to save the last inspection area
  333. //
  334. status = SCESTATUS_SUCCESS;
  335. }
  336. if (SCESTATUS_SUCCESS == status) {
  337. //
  338. // Those areas are no longer dirty.
  339. //
  340. if (!bSaveAs) {
  341. m_AreaDirty = 0;
  342. }
  343. } else {
  344. //
  345. // Save failed; Notify the user & return false
  346. //
  347. CString strMsg,strBase;
  348. strBase.LoadString(IDS_SAVE_FAILED); //Raid #485372, yanggao, 11/28/2001
  349. strBase += GetFriendlyName();
  350. strBase += L".";
  351. MyFormatMessage(status, (LPCTSTR)strBase, NULL, strMsg);
  352. AfxMessageBox(strMsg);
  353. return FALSE;
  354. }
  355. }
  356. if (bSaveDescription) {
  357. if (m_szDesc) {
  358. if (WritePrivateProfileSection(
  359. szDescription,
  360. NULL,
  361. szName)) {
  362. WritePrivateProfileString(
  363. szDescription,
  364. L"Description",
  365. m_szDesc,
  366. szName);
  367. }
  368. }
  369. }
  370. //
  371. // Save any dirty services
  372. //
  373. CString strService;
  374. LPSCESVCATTACHMENTPERSISTINFO pAttachPI;
  375. POSITION pos;
  376. SCESVCP_HANDLE *scesvcHandle;
  377. PVOID pvData;
  378. BOOL bOverwriteAll;
  379. pos = m_Services.GetStartPosition();
  380. while (pos) {
  381. m_Services.GetNextAssoc(pos,strService,pAttachPI);
  382. if (S_OK == pAttachPI->IsDirty( (LPTSTR)szName )) {
  383. if (SUCCEEDED(pAttachPI->Save( (LPTSTR)szName,(SCESVC_HANDLE *)&scesvcHandle,&pvData,&bOverwriteAll ))) {
  384. if (scesvcHandle) {
  385. if (lstrcmp(GT_COMPUTER_TEMPLATE,szName) == 0) {
  386. //
  387. // database
  388. //
  389. status = SceSvcUpdateInfo(
  390. m_hProfile,
  391. scesvcHandle->ServiceName,
  392. (PSCESVC_CONFIGURATION_INFO)pvData
  393. );
  394. } else {
  395. //
  396. // inf templates
  397. //
  398. status = SceSvcSetInformationTemplate(scesvcHandle->TemplateName,
  399. scesvcHandle->ServiceName,
  400. bOverwriteAll,
  401. (PSCESVC_CONFIGURATION_INFO)pvData);
  402. }
  403. if (SCESTATUS_SUCCESS != status) {
  404. CString strTitle,strMsg,strBase;
  405. strTitle.LoadString(IDS_NODENAME);
  406. strBase.LoadString(IDS_SAVE_FAILED);
  407. strBase += scesvcHandle->ServiceName; //szName;
  408. MyFormatMessage(status, (LPCTSTR)strBase, errBuf,strMsg);
  409. AfxMessageBox(strMsg);
  410. }
  411. }
  412. }
  413. }
  414. }
  415. return TRUE;
  416. }
  417. //+--------------------------------------------------------------------------
  418. //
  419. // Method: SetInfFile
  420. //
  421. // Synopsis: Set the name of the INF file this template is associated with
  422. //
  423. // Arguments: [szFile] - [in] the name of the INF file to associate with
  424. //
  425. // Returns: TRUE if the filename is set successfully, FALSE otherwise
  426. //
  427. // Modifies: m_szInfFile
  428. //
  429. // History:
  430. //
  431. //---------------------------------------------------------------------------
  432. BOOL
  433. CEditTemplate::SetInfFile(LPCTSTR szFile) {
  434. LPTSTR szInfFile;
  435. if (szFile) {
  436. szInfFile = new TCHAR[lstrlen(szFile)+1];
  437. if (szInfFile) {
  438. //This is a safe usage.
  439. lstrcpy(szInfFile,szFile);
  440. if (m_szInfFile) {
  441. delete[] m_szInfFile;
  442. }
  443. m_szInfFile = szInfFile;
  444. } else {
  445. return FALSE;
  446. }
  447. }
  448. return szFile != 0;
  449. }
  450. //+--------------------------------------------------------------------------
  451. //
  452. // Method: SetDescription
  453. //
  454. // Synopsis: Set the description for this template file
  455. //
  456. // Arguments: [szDesc] [in] the description for the template
  457. //
  458. // Returns: TRUE if the description is set successfully, FALSE otherwise
  459. //
  460. // Modifies: m_szDesc
  461. //
  462. // History:
  463. //
  464. //---------------------------------------------------------------------------
  465. BOOL
  466. CEditTemplate::SetDescription(LPCTSTR szDesc) {
  467. LPTSTR szDescriptionLoc; //Raid #prefast
  468. if (szDesc) {
  469. szDescriptionLoc = new TCHAR[lstrlen(szDesc)+1];
  470. if (szDescriptionLoc) {
  471. //This is a safe usage.
  472. lstrcpy(szDescriptionLoc,szDesc);
  473. if (m_szDesc) {
  474. delete[] m_szDesc;
  475. }
  476. m_szDesc = szDescriptionLoc;
  477. SetDirty(AREA_DESCRIPTION);
  478. } else {
  479. return FALSE;
  480. }
  481. }
  482. return szDesc != 0;
  483. }
  484. //+--------------------------------------------------------------------------
  485. //
  486. // Method: CEditTemplate
  487. //
  488. // Synopsis: Constructor for CEditTemplate
  489. //
  490. // History:
  491. //
  492. //---------------------------------------------------------------------------
  493. CEditTemplate::CEditTemplate() {
  494. m_AreaDirty = 0;
  495. m_AreaLoaded = 0;
  496. m_bWriteThrough = FALSE;
  497. m_bWriteThroughDirty = FALSE;
  498. m_hProfile = NULL;
  499. m_szInfFile = NULL;
  500. m_pNotify = NULL;
  501. m_pCDI = NULL;
  502. m_bNoSave = FALSE;
  503. m_strFriendlyName.Empty();
  504. m_szDesc = NULL;
  505. m_bWMI = NULL;
  506. m_bPolicy = FALSE;
  507. m_bLocked = FALSE;
  508. pTemplate = NULL;
  509. }
  510. //+--------------------------------------------------------------------------
  511. //
  512. // Method: ~CEditTemplate
  513. //
  514. // Synopsis: Destructor for CEditTemplate
  515. //
  516. // History:
  517. //
  518. //---------------------------------------------------------------------------
  519. CEditTemplate::~CEditTemplate() {
  520. POSITION pos;
  521. CString strKey;
  522. pos = m_Services.GetStartPosition();
  523. LPSCESVCATTACHMENTPERSISTINFO pAttachPI;
  524. while (pos) {
  525. m_Services.GetNextAssoc(pos,strKey,pAttachPI);
  526. delete pAttachPI;
  527. }
  528. if (m_szInfFile) {
  529. delete[] m_szInfFile;
  530. }
  531. if (m_szDesc) {
  532. delete[] m_szDesc;
  533. }
  534. if (pTemplate) {
  535. if (m_bWMI) {
  536. FreeWMI_SCE_PROFILE_INFO((PWMI_SCE_PROFILE_INFO)pTemplate);
  537. } else {
  538. SceFreeProfileMemory(pTemplate);
  539. }
  540. pTemplate = NULL;
  541. }
  542. m_AreaDirty = 0;
  543. }
  544. //+--------------------------------------------------------------------------
  545. //
  546. // Method: RefreshTemplate
  547. //
  548. // Synopsis: Reload the loaded parts of the template
  549. //
  550. // Arguments: [aiArea] - Areas to load even if not previously loaded
  551. //
  552. // Returns: 0 if the template is reloaded successfully, an error code otherwise
  553. //
  554. // Modifies: pTemplate;
  555. //---------------------------------------------------------------------------
  556. DWORD
  557. CEditTemplate::RefreshTemplate(AREA_INFORMATION aiAreaToAdd) {
  558. AREA_INFORMATION aiArea;
  559. PVOID pHandle = NULL;
  560. SCESTATUS rc;
  561. aiArea = m_AreaLoaded | aiAreaToAdd;
  562. if (!m_szInfFile) {
  563. return 1;
  564. }
  565. m_AreaDirty = 0;
  566. if (pTemplate) {
  567. if (m_bWMI) {
  568. FreeWMI_SCE_PROFILE_INFO((PWMI_SCE_PROFILE_INFO)pTemplate);
  569. } else {
  570. SceFreeProfileMemory(pTemplate);
  571. }
  572. pTemplate = NULL;
  573. }
  574. //Prefast warning 400: Yields unexpected results in non-English locales. Comments: They are not localizable.
  575. if ((_wcsicmp(GT_COMPUTER_TEMPLATE,m_szInfFile) == 0) ||
  576. (_wcsicmp(GT_LAST_INSPECTION,m_szInfFile) == 0) ||
  577. (_wcsicmp(GT_LOCAL_POLICY, m_szInfFile) == 0) ||
  578. (_wcsicmp(GT_EFFECTIVE_POLICY, m_szInfFile) == 0) ) {
  579. //
  580. // Analysis pane areas from jet database, not INF files
  581. //
  582. SCETYPE sceType;
  583. PSCE_ERROR_LOG_INFO perr = NULL;
  584. if (_wcsicmp(GT_COMPUTER_TEMPLATE,m_szInfFile) == 0) { //Prefast warning 400: Yields unexpected results in non-English locales. Comments: It is not localizable.
  585. sceType = SCE_ENGINE_SMP;
  586. } else if (_wcsicmp(GT_LOCAL_POLICY, m_szInfFile) == 0) {
  587. sceType = SCE_ENGINE_SYSTEM;
  588. if (!IsAdmin()) {
  589. m_hProfile = NULL;
  590. }
  591. } else if (_wcsicmp(GT_EFFECTIVE_POLICY,m_szInfFile) == 0){
  592. sceType = SCE_ENGINE_GPO;
  593. } else {
  594. sceType = SCE_ENGINE_SAP;
  595. }
  596. rc = SceGetSecurityProfileInfo(m_hProfile, // hProfile
  597. sceType, // Profile type
  598. aiArea, // Area
  599. &pTemplate, // SCE_PROFILE_INFO [out]
  600. &perr); // Error List [out]
  601. if (SCESTATUS_SUCCESS != rc) {
  602. if ((SCE_ENGINE_GPO == sceType) &&
  603. (0 == _wcsicmp(GT_EFFECTIVE_POLICY,m_szInfFile))) {
  604. SetTemplateDefaults();
  605. return 0;
  606. } else {
  607. return IDS_ERROR_CANT_GET_PROFILE_INFO;
  608. }
  609. }
  610. } else if (_wcsicmp(GT_RSOP_TEMPLATE, m_szInfFile) == 0) { //Prefast warning 400: Yields unexpected results in non-English locales. Comments: It is not localizable.
  611. if (!m_pCDI) {
  612. return IDS_ERROR_CANT_GET_PROFILE_INFO;
  613. }
  614. m_bWMI = TRUE;
  615. CWMIRsop Rsop(m_pCDI->m_pRSOPInfo);
  616. HRESULT hr;
  617. PWMI_SCE_PROFILE_INFO pProfileInfo;
  618. //
  619. // GetPrecedenceOneRSOPInfo should (but doesn't) support
  620. // getting just the requested area.
  621. //
  622. hr = Rsop.GetPrecedenceOneRSOPInfo(&pProfileInfo);
  623. if (FAILED(hr)) {
  624. return IDS_ERROR_CANT_GET_PROFILE_INFO;
  625. }
  626. pTemplate = pProfileInfo;
  627. //
  628. // Since it doesn't, set all areas not just the ones that
  629. // were asked for
  630. //
  631. AddArea(AREA_ALL);
  632. return 0;
  633. } else {
  634. LPTSTR szInfFile=NULL;
  635. if (_wcsicmp(GT_DEFAULT_TEMPLATE,m_szInfFile) == 0) { //Prefast warning 400: Yields unexpected results in non-English locales. Comments: It is not localizable.
  636. DWORD RegType;
  637. rc = MyRegQueryValue(HKEY_LOCAL_MACHINE,
  638. SCE_REGISTRY_KEY,
  639. SCE_REGISTRY_DEFAULT_TEMPLATE,
  640. (PVOID *)&szInfFile,
  641. &RegType );
  642. if (ERROR_SUCCESS != rc) {
  643. if (szInfFile) {
  644. LocalFree(szInfFile);
  645. szInfFile = NULL;
  646. }
  647. return IDS_ERROR_CANT_GET_PROFILE_INFO;
  648. }
  649. if (EngineOpenProfile(szInfFile,OPEN_PROFILE_CONFIGURE,&pHandle) != SCESTATUS_SUCCESS) {
  650. SetTemplateDefaults();
  651. LocalFree(szInfFile);
  652. szInfFile = NULL;
  653. return 0;
  654. }
  655. LocalFree(szInfFile);
  656. szInfFile = NULL;
  657. } else {
  658. if (EngineOpenProfile(m_szInfFile,OPEN_PROFILE_CONFIGURE,&pHandle) != SCESTATUS_SUCCESS) {
  659. return IDS_ERROR_CANT_OPEN_PROFILE;
  660. }
  661. }
  662. ASSERT(pHandle); //Check the pHandle and return IDS_ERROR_CANT_GET_PROFILE_INFO if it fails.
  663. //
  664. // get information from this template
  665. //
  666. PSCE_ERROR_LOG_INFO perr = NULL;
  667. if( pHandle ) //Raid #550912, yanggao.
  668. {
  669. rc = SceGetSecurityProfileInfo(pHandle,
  670. SCE_ENGINE_SCP,
  671. aiArea,
  672. &pTemplate,
  673. &perr //NULL // &ErrBuf do not care errors
  674. );
  675. if (SCESTATUS_SUCCESS != rc) {
  676. // Oops!
  677. }
  678. SceCloseProfile(&pHandle);
  679. pHandle = NULL;
  680. }
  681. else
  682. {
  683. return IDS_ERROR_CANT_OPEN_PROFILE;
  684. }
  685. }
  686. /*
  687. if do not care errors, no need to use this buffer
  688. if ( ErrBuf ) {
  689. SceFreeMemory((PVOID)ErrBuf, SCE_STRUCT_ERROR_LOG_INFO);
  690. ErrBuf = NULL;
  691. }
  692. */
  693. if (rc != SCESTATUS_SUCCESS) {
  694. return IDS_ERROR_CANT_GET_PROFILE_INFO;
  695. }
  696. //
  697. // Set the area in the template
  698. //
  699. AddArea(aiArea);
  700. if ( aiArea & AREA_SECURITY_POLICY && pTemplate ) {
  701. //
  702. // expand registry value section based on registry values list on local machine
  703. //
  704. SceRegEnumAllValues(
  705. &(pTemplate->RegValueCount),
  706. &(pTemplate->aRegValues)
  707. );
  708. }
  709. return 0;
  710. }
  711. //+----------------------------------------------------------------------------------
  712. //Method: UpdatePrivilegeAssignedTo
  713. //
  714. //Synopsis: Updates a priviledge item, depending on the [bRemove] argument.
  715. // if [bRemove] is
  716. // FALSE - A new link is created and the pointer is returned through
  717. // ppaLink
  718. // TRUE - The link is removed from the list.
  719. //
  720. //Arguments: [bRemove] - Weither to remove or add an item.
  721. // [ppaLink] - The link to be removed or added. This paramter is
  722. // set to NULL if remove is successful or a pointer
  723. // to a new SCE_PRIVILEGE_ASSIGNMENT item.
  724. // [pszName] - Only used when adding a new item.
  725. //
  726. //Returns: ERROR_INVALID_PARAMETER - [ppaLink] is NULL or if removing
  727. // [*ppaLink] is NULL.
  728. // if adding then if [pszName] is NULL
  729. // ERROR_RESOURCE_NOT_FOUND - If the link could not be found
  730. // in this template.
  731. // E_POINTER - If [pszName] is a bad pointer or
  732. // [ppaLink] is bad.
  733. // E_OUTOFMEMORY - Not enough resources to complete the
  734. // operation.
  735. // ERROR_SUCCESS - The opration was successful.
  736. //----------------------------------------------------------------------------------+
  737. DWORD
  738. CEditTemplate::UpdatePrivilegeAssignedTo(
  739. BOOL bRemove,
  740. PSCE_PRIVILEGE_ASSIGNMENT *ppaLink,
  741. LPCTSTR pszName
  742. )
  743. {
  744. if(!ppaLink){
  745. return ERROR_INVALID_PARAMETER;
  746. }
  747. PSCE_PRIVILEGE_ASSIGNMENT *pNext = NULL;
  748. PSCE_PRIVILEGE_ASSIGNMENT pCurrent = NULL;
  749. if(bRemove) {
  750. __try {
  751. if(!*ppaLink){
  752. return ERROR_INVALID_PARAMETER;
  753. }
  754. } __except(EXCEPTION_EXECUTE_HANDLER) { //Raid #630245, yanggao, 6/05/2002.
  755. return (DWORD)E_POINTER;
  756. }
  757. //
  758. // Remove the link from the list.
  759. //
  760. pCurrent = pTemplate->OtherInfo.smp.pPrivilegeAssignedTo;
  761. if(pCurrent == (*ppaLink) ){
  762. pNext = &(pTemplate->OtherInfo.smp.pPrivilegeAssignedTo);
  763. } else if(pCurrent && pCurrent != (PSCE_PRIVILEGE_ASSIGNMENT)ULongToPtr(SCE_NO_VALUE)) {
  764. while( pCurrent->Next ){
  765. if(pCurrent->Next == *ppaLink){
  766. pNext = &(pCurrent->Next);
  767. break;
  768. }
  769. pCurrent = pCurrent->Next;
  770. }
  771. }
  772. if(pNext && pCurrent){
  773. (*pNext) = (*ppaLink)->Next;
  774. if( (*ppaLink)->Name){
  775. LocalFree( (*ppaLink)->Name);
  776. (*ppaLink)->Name = NULL;
  777. }
  778. SceFreeMemory( (*ppaLink)->AssignedTo, SCE_STRUCT_NAME_LIST);
  779. LocalFree( *ppaLink );
  780. *ppaLink = NULL;
  781. } else {
  782. return ERROR_RESOURCE_NOT_FOUND;
  783. }
  784. } else {
  785. int iLen;
  786. if(!pszName){
  787. return ERROR_INVALID_PARAMETER;
  788. }
  789. __try {
  790. iLen = lstrlen( pszName );
  791. } __except(EXCEPTION_CONTINUE_EXECUTION){
  792. return (DWORD)E_POINTER;
  793. }
  794. //
  795. // Create a new link.
  796. //
  797. pCurrent = (PSCE_PRIVILEGE_ASSIGNMENT)LocalAlloc( 0, sizeof(SCE_PRIVILEGE_ASSIGNMENT));
  798. if(!pCurrent){
  799. return (DWORD)E_OUTOFMEMORY;
  800. }
  801. ZeroMemory(pCurrent, sizeof(SCE_PRIVILEGE_ASSIGNMENT));
  802. //
  803. // Allocate space for the name.
  804. //
  805. pCurrent->Name = (LPTSTR)LocalAlloc( 0, sizeof(TCHAR) * (iLen + 1));
  806. if(!pCurrent->Name){
  807. LocalFree(pCurrent);
  808. return (DWORD)E_OUTOFMEMORY;
  809. }
  810. //This may not be a safe usage. pCurrent->Name is PWSTR. Consider fix.
  811. lstrcpy(pCurrent->Name, pszName);
  812. if (*ppaLink) {
  813. pCurrent->Status = (*ppaLink)->Status;
  814. pCurrent->AssignedTo = (*ppaLink)->AssignedTo;
  815. }
  816. //
  817. // Assign it to the link.
  818. //
  819. pCurrent->Next = pTemplate->OtherInfo.smp.pPrivilegeAssignedTo;
  820. pTemplate->OtherInfo.smp.pPrivilegeAssignedTo = pCurrent;
  821. *ppaLink = pCurrent;
  822. }
  823. return ERROR_SUCCESS;
  824. }
  825. DWORD
  826. CEditTemplate::ComputeStatus(
  827. PSCE_PRIVILEGE_ASSIGNMENT pEdit,
  828. PSCE_PRIVILEGE_ASSIGNMENT pAnal
  829. )
  830. {
  831. if (!pEdit || (PSCE_PRIVILEGE_ASSIGNMENT)ULongToPtr(SCE_NO_VALUE) == pEdit) {
  832. return SCE_STATUS_NOT_CONFIGURED;
  833. } else if (pEdit->Status == SCE_STATUS_NOT_CONFIGURED) {
  834. return SCE_STATUS_NOT_CONFIGURED;
  835. } else if (!pAnal || (PSCE_PRIVILEGE_ASSIGNMENT)ULongToPtr(SCE_NO_VALUE) == pAnal) {
  836. return SCE_STATUS_MISMATCH;
  837. } else if (SceCompareNameList(pEdit->AssignedTo, pAnal->AssignedTo)) {
  838. return SCE_STATUS_GOOD;
  839. }
  840. return pAnal->Status;
  841. }
  842. DWORD
  843. CEditTemplate::ComputeStatus(
  844. PSCE_REGISTRY_VALUE_INFO prvEdit,
  845. PSCE_REGISTRY_VALUE_INFO prvAnal
  846. )
  847. {
  848. //
  849. // Calculate information.
  850. //
  851. if(!prvEdit){
  852. return SCE_STATUS_NOT_CONFIGURED;
  853. }
  854. if(!prvAnal || (PSCE_REGISTRY_VALUE_INFO)ULongToPtr(SCE_NO_VALUE) == prvAnal){
  855. return SCE_STATUS_ERROR_NOT_AVAILABLE;
  856. }
  857. //
  858. // Calulate base on other information
  859. //
  860. if ( !(prvEdit->Value) ) {
  861. return SCE_STATUS_NOT_CONFIGURED;
  862. } else if ( (prvAnal->Value == NULL || prvAnal->Value == (LPTSTR)ULongToPtr(SCE_ERROR_VALUE))) {
  863. return prvAnal->Status;
  864. } else if ( _wcsicmp(prvEdit->Value, prvAnal->Value) != 0 ) {
  865. return SCE_STATUS_MISMATCH;
  866. }
  867. return SCE_STATUS_GOOD;
  868. }
  869. void
  870. CEditTemplate::LockWriteThrough() {
  871. ASSERT(!m_bLocked);
  872. m_bLocked = TRUE;
  873. }
  874. void
  875. CEditTemplate::UnLockWriteThrough() {
  876. ASSERT(m_bLocked);
  877. BOOL bSave = m_bLocked;
  878. m_bLocked = FALSE;
  879. //
  880. // Set dirty to save out any still dirty changes that
  881. // would have been written out had we not been locked
  882. //
  883. if ( bSave ) {
  884. SetDirty(0);
  885. SetTemplateDefaults();
  886. }
  887. }
  888. //Bug 212287, Yanggao, 3/20/2001
  889. LPCTSTR CEditTemplate::GetDesc() const
  890. {
  891. return m_szDesc;
  892. }