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.

592 lines
20 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 2001.
  5. //
  6. // File: copyutil.cpp
  7. //
  8. // Contents: Utility routines for copying SCE sections to the clipboard
  9. //
  10. // HISTORY: 10-Nov-97 robcap Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "stdafx.h"
  14. #include "snapmgr.h"
  15. #include "wrapper.h"
  16. #include "util.h"
  17. #include <secedit.h>
  18. //+--------------------------------------------------------------------------
  19. //
  20. // Method: GetFolderCopyPasteInfo
  21. //
  22. // Synopsis: Finds the SCE area and clipboard format that correspond
  23. // to the folder type given in [Folder]
  24. //
  25. // Arguments: [Folder] - the folder type to find the area and cf for
  26. // [*Area] - output only
  27. // [*cf] - output only
  28. //
  29. // Returns: *[Area] - the SCE area that corresponds to [Folder]
  30. // *[cf] - the clipboard format that corresponds to [Folder]
  31. //
  32. //
  33. // History: 10-Nov-1997 RobCap created
  34. //
  35. //---------------------------------------------------------------------------
  36. BOOL
  37. CComponentDataImpl::GetFolderCopyPasteInfo(FOLDER_TYPES Folder, // In
  38. AREA_INFORMATION *Area, // Out
  39. UINT *cf) { // Out
  40. switch (Folder) {
  41. case POLICY_ACCOUNT:
  42. case POLICY_PASSWORD:
  43. case POLICY_KERBEROS:
  44. case POLICY_LOCKOUT:
  45. case POLICY_AUDIT:
  46. *Area = AREA_SECURITY_POLICY;
  47. *cf = cfSceAccountArea;
  48. break;
  49. case POLICY_LOCAL:
  50. case POLICY_OTHER:
  51. case AREA_PRIVILEGE:
  52. *Area = AREA_SECURITY_POLICY | AREA_PRIVILEGES;
  53. *cf = cfSceLocalArea;
  54. break;
  55. case POLICY_EVENTLOG:
  56. case POLICY_LOG:
  57. *Area = AREA_SECURITY_POLICY;
  58. *cf = cfSceEventLogArea;
  59. break;
  60. case AREA_GROUPS:
  61. *Area = AREA_GROUP_MEMBERSHIP;
  62. *cf = cfSceGroupsArea;
  63. break;
  64. case AREA_SERVICE:
  65. *Area = AREA_SYSTEM_SERVICE;
  66. *cf = cfSceServiceArea;
  67. break;
  68. case AREA_REGISTRY:
  69. *Area = AREA_REGISTRY_SECURITY;
  70. *cf = cfSceRegistryArea;
  71. break;
  72. case AREA_FILESTORE:
  73. *Area = AREA_FILE_SECURITY;
  74. *cf = cfSceFileArea;
  75. break;
  76. default:
  77. return FALSE;
  78. }
  79. return TRUE;
  80. }
  81. //+--------------------------------------------------------------------------
  82. //
  83. // Method: OnCopyArea
  84. //
  85. // Synopsis: Copy a folder to the clipboard
  86. //
  87. // Arguments: [szTemplate] - the name of the template file to copy from
  88. // [ft] - the type of folder to copy
  89. //
  90. // Returns: HRESULT
  91. //
  92. // History: 10-Nov-1997 RobCap created
  93. //
  94. //---------------------------------------------------------------------------
  95. HRESULT
  96. CComponentDataImpl::OnCopyArea(LPCTSTR szTemplate,FOLDER_TYPES ft) {
  97. HRESULT hr;
  98. SCESTATUS status;
  99. PEDITTEMPLATE pTemp;
  100. CString strPath,strFile;
  101. LPTSTR szPath,szFile;
  102. DWORD dw;
  103. CFile pFile;
  104. HANDLE hBuf,hSecBuf;
  105. PVOID pBuf,pSecBuf;
  106. PSCE_ERROR_LOG_INFO ErrLog;
  107. AREA_INFORMATION Area;
  108. UINT cf;
  109. hr = S_OK;
  110. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  111. CWaitCursor wc;
  112. //
  113. // Get a temporary directory path in strPath
  114. // If our buffer isn't large enough then keep reallocating until it is
  115. //
  116. dw = MAX_PATH;
  117. do {
  118. szPath = strPath.GetBuffer(dw);
  119. dw = GetTempPath(MAX_PATH,szPath);
  120. strPath.ReleaseBuffer();
  121. } while (dw > (DWORD)strPath.GetLength() );
  122. //
  123. // Can't get a path to the temporary directory
  124. //
  125. if (!dw) {
  126. return E_FAIL;
  127. }
  128. //
  129. // Get a temporary file in that directory
  130. //
  131. szFile = strFile.GetBuffer(dw+MAX_PATH);
  132. if (!GetTempFileName(szPath,L"SCE",0,szFile)) {
  133. strFile.ReleaseBuffer();
  134. return E_FAIL;
  135. }
  136. strFile.ReleaseBuffer();
  137. //
  138. // Get the template that we're trying to copy
  139. //
  140. pTemp = GetTemplate(szTemplate);
  141. if (!pTemp) {
  142. return E_FAIL;
  143. }
  144. if (!GetFolderCopyPasteInfo(ft,&Area,&cf)) {
  145. return E_FAIL;
  146. }
  147. status = SceWriteSecurityProfileInfo(szFile,
  148. Area,
  149. pTemp->pTemplate,
  150. NULL);
  151. if (SCESTATUS_SUCCESS == status) {
  152. if (!pFile.Open(szFile,CFile::modeRead)) {
  153. return E_FAIL;
  154. }
  155. dw = pFile.GetLength();
  156. hBuf = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,dw);
  157. if (!hBuf) {
  158. return E_OUTOFMEMORY;
  159. }
  160. //Raid #488205, yanggao, 11/15/2001
  161. //After SetClipboardData is called, the system owns the object identified by the hMem parameter.
  162. //The application can read the data, but must not free the handle or leave it locked until the CloseClipboard function is called.
  163. hSecBuf = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,dw);
  164. if (!hSecBuf) {
  165. GlobalFree(hBuf); //Free both hBuf and hSecBuf when failed. Raid #551257. yanggao, 3/6/02.
  166. return E_OUTOFMEMORY;
  167. }
  168. pBuf = GlobalLock(hBuf);
  169. if (!pBuf) {
  170. GlobalFree(hBuf);
  171. GlobalFree(hSecBuf);
  172. return E_FAIL;
  173. }
  174. pSecBuf = GlobalLock(hSecBuf);
  175. if (!pSecBuf) {
  176. GlobalUnlock(hBuf);
  177. GlobalFree(hBuf);
  178. GlobalFree(hSecBuf);
  179. return E_FAIL;
  180. }
  181. pFile.Read(pBuf,dw);
  182. memcpy(pSecBuf, pBuf, dw);
  183. GlobalUnlock(hBuf);
  184. GlobalUnlock(hSecBuf);
  185. if (OpenClipboard(NULL)) {
  186. EmptyClipboard();
  187. //
  188. // Add the data to the clipboard in CF_TEXT format, so it
  189. // can be pasted to Notepad
  190. //
  191. SetClipboardData(CF_TEXT,hSecBuf);
  192. //
  193. // Add the data to the clipboard in our custom format, so
  194. // we can read it back in on paste
  195. //
  196. SetClipboardData(cf,hBuf);
  197. CloseClipboard();
  198. } else {
  199. hr = E_FAIL;
  200. }
  201. pFile.Close();
  202. pFile.Remove(szFile);
  203. } else {
  204. return E_FAIL;
  205. }
  206. return hr;
  207. }
  208. //+--------------------------------------------------------------------------
  209. //
  210. // Method: OnPasteArea
  211. //
  212. // Synopsis: Paste an area from the clipboard
  213. //
  214. // Arguments: [szTemplate] - the name of the template file to paste from
  215. // [ft] - the type of folder to paste
  216. //
  217. // Returns: HRESULT
  218. //
  219. // History: 10-Nov-1997 RobCap created
  220. //
  221. //---------------------------------------------------------------------------
  222. HRESULT
  223. CComponentDataImpl::OnPasteArea(LPCTSTR szTemplate,FOLDER_TYPES ft) {
  224. SCESTATUS status;
  225. PEDITTEMPLATE pTemp;
  226. PSCE_PROFILE_INFO spi;
  227. CString strPath;
  228. CString strFile;
  229. LPTSTR szPath,szFile;
  230. AREA_INFORMATION Area;
  231. UINT cf;
  232. int k;
  233. DWORD dw;
  234. CFile *pFile;
  235. CFile pFileOut;
  236. PVOID pBuf;
  237. PVOID pHandle;
  238. HRESULT hr = S_OK;
  239. COleDataObject DataObject;
  240. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  241. CWaitCursor wc;
  242. //
  243. // Find the SCE Area and clipboard format for ft
  244. //
  245. if (!GetFolderCopyPasteInfo(ft,&Area,&cf)) {
  246. return E_FAIL;
  247. }
  248. //
  249. // Get a temporary directory path in strPath
  250. // If our buffer isn't large enough then keep reallocating until it is
  251. //
  252. dw = MAX_PATH;
  253. do {
  254. szPath = strPath.GetBuffer(dw);
  255. dw = GetTempPath(MAX_PATH,szPath);
  256. strPath.ReleaseBuffer();
  257. } while (dw > (DWORD)strPath.GetLength() );
  258. //
  259. // Can't get a path to the temporary directory
  260. //
  261. if (!dw) {
  262. return E_FAIL;
  263. }
  264. //
  265. // Get a temporary file in that directory
  266. //
  267. szFile = strFile.GetBuffer(dw+MAX_PATH);
  268. if (!GetTempFileName(szPath,L"SCE",0,szFile)) {
  269. strFile.ReleaseBuffer();
  270. return E_FAIL;
  271. }
  272. strFile.ReleaseBuffer();
  273. //
  274. // Get the template we're pasting into
  275. //
  276. pTemp = GetTemplate(szTemplate);
  277. if (!pTemp) {
  278. return E_FAIL;
  279. }
  280. //
  281. // Attach the data object to the clipboard; we don't need
  282. // to worry about releasing it since that will be done in
  283. // DataObject's destructor
  284. //
  285. if (!DataObject.AttachClipboard()) {
  286. return E_FAIL;
  287. }
  288. if (!DataObject.IsDataAvailable((CLIPFORMAT)cf)) {
  289. return E_FAIL;
  290. }
  291. pFile = DataObject.GetFileData((CLIPFORMAT)cf);
  292. if (pFile) {
  293. //
  294. // Write the data from the clipboard to a temporary file
  295. //
  296. if ( pFileOut.Open(szFile,CFile::modeWrite) ) {
  297. dw = pFile->GetLength();
  298. pBuf = new BYTE [dw];
  299. if (NULL != pBuf) {
  300. pFile->Read(pBuf,dw);
  301. pFileOut.Write(pBuf,dw);
  302. } else {
  303. hr = E_FAIL;
  304. }
  305. pFileOut.Close();
  306. }
  307. pFile->Close();
  308. } else {
  309. return E_FAIL;
  310. }
  311. if (S_OK == hr) {
  312. //
  313. // Have the engine open the temporary file as a template
  314. //
  315. if (EngineOpenProfile(szFile,OPEN_PROFILE_CONFIGURE,&pHandle) != SCESTATUS_SUCCESS) {
  316. delete []pBuf; //Raid #prefast
  317. return E_FAIL;
  318. }
  319. //
  320. // Load the temporary template area into our scratch SCE_PROFILE_INFO
  321. //
  322. //
  323. // SceGetSecurityProfileInfo will allocate an SCE_PROFILE_INFO struct
  324. // if a pointer to a NULL one is passed in
  325. //
  326. spi = NULL;
  327. status = SceGetSecurityProfileInfo(pHandle,
  328. SCE_ENGINE_SCP,
  329. Area,
  330. &spi,
  331. NULL);
  332. SceCloseProfile(&pHandle);
  333. pHandle = NULL;
  334. if (SCESTATUS_SUCCESS == status) {
  335. PSCE_REGISTRY_VALUE_INFO pRegValues;
  336. //
  337. // The load succeeded, so free the appropriate old area and copy the
  338. // new version from the scratch SCE_PROFILE_INFO
  339. //
  340. switch(ft) {
  341. case POLICY_ACCOUNT:
  342. pTemp->pTemplate->MinimumPasswordAge = spi->MinimumPasswordAge;
  343. pTemp->pTemplate->MaximumPasswordAge = spi->MaximumPasswordAge;
  344. pTemp->pTemplate->PasswordComplexity = spi->PasswordComplexity;
  345. pTemp->pTemplate->ClearTextPassword = spi->ClearTextPassword;
  346. pTemp->pTemplate->PasswordHistorySize = spi->PasswordHistorySize;
  347. pTemp->pTemplate->RequireLogonToChangePassword = spi->RequireLogonToChangePassword;
  348. pTemp->pTemplate->MinimumPasswordLength = spi->MinimumPasswordLength;
  349. pTemp->pTemplate->LockoutBadCount = spi->LockoutBadCount;
  350. pTemp->pTemplate->ResetLockoutCount = spi->ResetLockoutCount;
  351. pTemp->pTemplate->LockoutDuration = spi->LockoutDuration;
  352. if (spi->pKerberosInfo) {
  353. if (!pTemp->pTemplate->pKerberosInfo) {
  354. pTemp->pTemplate->pKerberosInfo = (PSCE_KERBEROS_TICKET_INFO) LocalAlloc(LPTR,sizeof(SCE_KERBEROS_TICKET_INFO));
  355. }
  356. if (pTemp->pTemplate->pKerberosInfo) {
  357. pTemp->pTemplate->pKerberosInfo->MaxTicketAge = spi->pKerberosInfo->MaxTicketAge;
  358. pTemp->pTemplate->pKerberosInfo->MaxServiceAge = spi->pKerberosInfo->MaxServiceAge;
  359. pTemp->pTemplate->pKerberosInfo->MaxClockSkew = spi->pKerberosInfo->MaxClockSkew;
  360. pTemp->pTemplate->pKerberosInfo->MaxRenewAge = spi->pKerberosInfo->MaxRenewAge;
  361. pTemp->pTemplate->pKerberosInfo->TicketValidateClient = spi->pKerberosInfo->TicketValidateClient;
  362. }
  363. } else if (pTemp->pTemplate->pKerberosInfo) {
  364. LocalFree(pTemp->pTemplate->pKerberosInfo);
  365. pTemp->pTemplate->pKerberosInfo = NULL;
  366. }
  367. break;
  368. case POLICY_LOCAL:
  369. pTemp->pTemplate->AuditAccountManage = spi->AuditAccountManage;
  370. pTemp->pTemplate->AuditLogonEvents = spi->AuditLogonEvents;
  371. pTemp->pTemplate->AuditObjectAccess = spi->AuditObjectAccess;
  372. pTemp->pTemplate->AuditPolicyChange = spi->AuditPolicyChange;
  373. pTemp->pTemplate->AuditPrivilegeUse = spi->AuditPrivilegeUse;
  374. pTemp->pTemplate->AuditProcessTracking = spi->AuditProcessTracking;
  375. pTemp->pTemplate->AuditSystemEvents = spi->AuditSystemEvents;
  376. pTemp->pTemplate->AuditDSAccess = spi->AuditDSAccess;
  377. pTemp->pTemplate->AuditAccountLogon = spi->AuditAccountLogon;
  378. pTemp->pTemplate->LSAAnonymousNameLookup = spi->LSAAnonymousNameLookup;
  379. pTemp->pTemplate->ForceLogoffWhenHourExpire = spi->ForceLogoffWhenHourExpire;
  380. pTemp->pTemplate->EnableAdminAccount = spi->EnableAdminAccount;
  381. pTemp->pTemplate->EnableGuestAccount = spi->EnableGuestAccount;
  382. pTemp->pTemplate->NewAdministratorName = spi->NewAdministratorName;
  383. pTemp->pTemplate->NewGuestName = spi->NewGuestName;
  384. spi->NewAdministratorName = NULL;
  385. spi->NewGuestName = NULL;
  386. //
  387. // copy reg value section too
  388. //
  389. dw = pTemp->pTemplate->RegValueCount;
  390. pRegValues = pTemp->pTemplate->aRegValues;
  391. pTemp->pTemplate->RegValueCount = spi->RegValueCount;
  392. pTemp->pTemplate->aRegValues = spi->aRegValues;
  393. spi->RegValueCount = dw;
  394. spi->aRegValues = pRegValues;
  395. SceRegEnumAllValues(
  396. &(pTemp->pTemplate->RegValueCount),
  397. &(pTemp->pTemplate->aRegValues)
  398. );
  399. //
  400. // copy user rights
  401. //
  402. SceFreeMemory(pTemp->pTemplate->OtherInfo.scp.u.pPrivilegeAssignedTo,SCE_STRUCT_PRIVILEGE);
  403. pTemp->pTemplate->OtherInfo.scp.u.pPrivilegeAssignedTo = spi->OtherInfo.scp.u.pPrivilegeAssignedTo;
  404. spi->OtherInfo.scp.u.pPrivilegeAssignedTo = NULL;
  405. break;
  406. case POLICY_PASSWORD:
  407. pTemp->pTemplate->MinimumPasswordAge = spi->MinimumPasswordAge;
  408. pTemp->pTemplate->MaximumPasswordAge = spi->MaximumPasswordAge;
  409. pTemp->pTemplate->PasswordComplexity = spi->PasswordComplexity;
  410. pTemp->pTemplate->ClearTextPassword = spi->ClearTextPassword;
  411. pTemp->pTemplate->PasswordHistorySize = spi->PasswordHistorySize;
  412. pTemp->pTemplate->RequireLogonToChangePassword = spi->RequireLogonToChangePassword;
  413. pTemp->pTemplate->MinimumPasswordLength = spi->MinimumPasswordLength;
  414. break;
  415. case POLICY_LOCKOUT:
  416. pTemp->pTemplate->LockoutBadCount = spi->LockoutBadCount;
  417. pTemp->pTemplate->ResetLockoutCount = spi->ResetLockoutCount;
  418. pTemp->pTemplate->LockoutDuration = spi->LockoutDuration;
  419. break;
  420. case POLICY_KERBEROS:
  421. pTemp->pTemplate->pKerberosInfo->MaxTicketAge = spi->pKerberosInfo->MaxTicketAge;
  422. pTemp->pTemplate->pKerberosInfo->MaxServiceAge = spi->pKerberosInfo->MaxServiceAge;
  423. pTemp->pTemplate->pKerberosInfo->MaxClockSkew = spi->pKerberosInfo->MaxClockSkew;
  424. pTemp->pTemplate->pKerberosInfo->MaxRenewAge = spi->pKerberosInfo->MaxRenewAge;
  425. pTemp->pTemplate->pKerberosInfo->TicketValidateClient = spi->pKerberosInfo->TicketValidateClient;
  426. break;
  427. case POLICY_AUDIT:
  428. pTemp->pTemplate->AuditAccountManage = spi->AuditAccountManage;
  429. pTemp->pTemplate->AuditLogonEvents = spi->AuditLogonEvents;
  430. pTemp->pTemplate->AuditObjectAccess = spi->AuditObjectAccess;
  431. pTemp->pTemplate->AuditPolicyChange = spi->AuditPolicyChange;
  432. pTemp->pTemplate->AuditPrivilegeUse = spi->AuditPrivilegeUse;
  433. pTemp->pTemplate->AuditProcessTracking = spi->AuditProcessTracking;
  434. pTemp->pTemplate->AuditSystemEvents = spi->AuditSystemEvents;
  435. pTemp->pTemplate->AuditDSAccess = spi->AuditDSAccess;
  436. pTemp->pTemplate->AuditAccountLogon = spi->AuditAccountLogon;
  437. break;
  438. case POLICY_OTHER:
  439. pTemp->pTemplate->ForceLogoffWhenHourExpire = spi->ForceLogoffWhenHourExpire;
  440. pTemp->pTemplate->EnableGuestAccount = spi->EnableGuestAccount;
  441. pTemp->pTemplate->EnableAdminAccount = spi->EnableAdminAccount;
  442. pTemp->pTemplate->LSAAnonymousNameLookup = spi->LSAAnonymousNameLookup;
  443. pTemp->pTemplate->NewAdministratorName = spi->NewAdministratorName;
  444. pTemp->pTemplate->NewGuestName = spi->NewGuestName;
  445. spi->NewAdministratorName = NULL;
  446. spi->NewGuestName = NULL;
  447. //
  448. // copy reg value section too
  449. //
  450. dw = pTemp->pTemplate->RegValueCount;
  451. pRegValues = pTemp->pTemplate->aRegValues;
  452. pTemp->pTemplate->RegValueCount = spi->RegValueCount;
  453. pTemp->pTemplate->aRegValues = spi->aRegValues;
  454. spi->RegValueCount = dw;
  455. spi->aRegValues = pRegValues;
  456. SceRegEnumAllValues(
  457. &(pTemp->pTemplate->RegValueCount),
  458. &(pTemp->pTemplate->aRegValues)
  459. );
  460. break;
  461. case AREA_PRIVILEGE:
  462. SceFreeMemory(pTemp->pTemplate->OtherInfo.scp.u.pPrivilegeAssignedTo,SCE_STRUCT_PRIVILEGE);
  463. pTemp->pTemplate->OtherInfo.scp.u.pPrivilegeAssignedTo = spi->OtherInfo.scp.u.pPrivilegeAssignedTo;
  464. spi->OtherInfo.scp.u.pPrivilegeAssignedTo = NULL;
  465. break;
  466. case POLICY_EVENTLOG:
  467. case POLICY_LOG:
  468. for(k=0;k<3;k++) {
  469. pTemp->pTemplate->MaximumLogSize[k] = spi->MaximumLogSize[k];
  470. pTemp->pTemplate->AuditLogRetentionPeriod[k] = spi->AuditLogRetentionPeriod[k];
  471. pTemp->pTemplate->RetentionDays[k] = spi->RetentionDays[k];
  472. pTemp->pTemplate->RestrictGuestAccess[k] = spi->RestrictGuestAccess[k];
  473. }
  474. break;
  475. case AREA_GROUPS:
  476. SceFreeMemory(pTemp->pTemplate->pGroupMembership,SCE_STRUCT_GROUP);
  477. pTemp->pTemplate->pGroupMembership = spi->pGroupMembership;
  478. spi->pGroupMembership = NULL;
  479. break;
  480. case AREA_SERVICE:
  481. SceFreeMemory(pTemp->pTemplate->pServices,SCE_STRUCT_SERVICES);
  482. pTemp->pTemplate->pServices = spi->pServices;
  483. spi->pServices = NULL;
  484. break;
  485. case AREA_REGISTRY:
  486. SceFreeMemory(pTemp->pTemplate->pRegistryKeys.pAllNodes,SCE_STRUCT_OBJECT_ARRAY);
  487. pTemp->pTemplate->pRegistryKeys = spi->pRegistryKeys;
  488. spi->pRegistryKeys.pAllNodes = NULL;
  489. break;
  490. case AREA_FILESTORE:
  491. SceFreeMemory(pTemp->pTemplate->pFiles.pAllNodes,SCE_STRUCT_OBJECT_ARRAY);
  492. pTemp->pTemplate->pFiles = spi->pFiles;
  493. spi->pFiles.pAllNodes = NULL;
  494. break;
  495. default:
  496. break;
  497. }
  498. }
  499. SceFreeProfileMemory(spi);
  500. pTemp->SetDirty(Area);
  501. RefreshAllFolders();
  502. } else {
  503. //
  504. // Don't do anything special, just be sure to clean up below....
  505. //
  506. }
  507. //
  508. // Delete the temporary file
  509. //
  510. pFileOut.Remove(szFile);
  511. if (pBuf) {
  512. delete []pBuf; //Raid #prefast
  513. }
  514. if (pFile) {
  515. delete pFile;
  516. }
  517. return hr;
  518. }