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.

1518 lines
46 KiB

  1. #include "stdafx.h"
  2. #include <SHlWapi.h>
  3. #include "hydraoc.h"
  4. #include "subcomp.h"
  5. #include "secupgrd.h"
  6. #include "lscsp.h"
  7. #include "reglic.h"
  8. #include "cryptkey.h"
  9. //
  10. // Define copy from newclient\inc\reglic.h
  11. //
  12. #define MSLICENSING_REG_KEY _T("SOFTWARE\\Microsoft\\MSLicensing")
  13. #define BETA_LICENSING_TIME_BOMB_5_1 L"L$BETA3TIMEBOMB_1320153D-8DA3-4e8e-B27B-0D888223A588"
  14. #define RTM_LICENSING_TIME_BOMB_5_1 L"L$RTMTIMEBOMB_1320153D-8DA3-4e8e-B27B-0D888223A588"
  15. typedef struct __SysPrepCmd {
  16. LPCTSTR pszFullExePath;
  17. LPCTSTR pszExeParm;
  18. } SYSPREPCMD, *PSYSPREPCMD;
  19. typedef DWORD (*PSETENTRIESINACL)(
  20. ULONG cCountOfExplicitEntries, // number of entries
  21. PEXPLICIT_ACCESS pListOfExplicitEntries, // buffer
  22. PACL OldAcl, // original ACL
  23. PACL *NewAcl // new ACL
  24. );
  25. BOOL
  26. AddACLToObjectSecurityDescriptor(
  27. HANDLE hObject,
  28. SE_OBJECT_TYPE ObjectType
  29. )
  30. {
  31. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  32. SID_IDENTIFIER_AUTHORITY CreatorAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  33. PSID pAdminSid = NULL;
  34. PSID pSystemSid = NULL;
  35. PSID pPowerUsersSid = NULL;
  36. PSID pCreatorSid = NULL;
  37. PSID pUsersSid = NULL;
  38. PACL pNewDACL;
  39. DWORD dwError;
  40. BOOL bSuccess;
  41. DWORD i;
  42. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid = NULL;
  43. PSETENTRIESINACL pSetEntriesInAcl = NULL;
  44. HMODULE pAdvApi32 = NULL;
  45. PFREESID_FN pFreeSid = NULL;
  46. PSETSECURITYINFO_FN pSetSecurityInfo;
  47. pAdvApi32 = LoadLibrary(ADVAPI_32_DLL);
  48. if (!pAdvApi32) {
  49. return(FALSE);
  50. }
  51. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  52. GetProcAddress(pAdvApi32,
  53. ALLOCATE_AND_INITITIALIZE_SID);
  54. if (pAllocateAndInitializeSid == NULL)
  55. {
  56. goto ErrorCleanup;
  57. }
  58. #ifdef UNICODE
  59. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclW" ));
  60. #else
  61. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclA" ));
  62. #endif
  63. if (!pSetEntriesInAcl) {
  64. FreeLibrary( pAdvApi32 );
  65. return(FALSE);
  66. }
  67. EXPLICIT_ACCESS ExplicitAccess[5];
  68. //
  69. // Create SIDs - Admins and System
  70. //
  71. bSuccess = pAllocateAndInitializeSid( &NtAuthority,
  72. 2,
  73. SECURITY_BUILTIN_DOMAIN_RID,
  74. DOMAIN_ALIAS_RID_ADMINS,
  75. 0, 0, 0, 0, 0, 0,
  76. &pAdminSid);
  77. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  78. 1,
  79. SECURITY_LOCAL_SYSTEM_RID,
  80. 0, 0, 0, 0, 0, 0, 0,
  81. &pSystemSid);
  82. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  83. 2,
  84. SECURITY_BUILTIN_DOMAIN_RID,
  85. DOMAIN_ALIAS_RID_POWER_USERS,
  86. 0, 0, 0, 0, 0, 0,
  87. &pPowerUsersSid);
  88. bSuccess = bSuccess && pAllocateAndInitializeSid( &CreatorAuthority,
  89. 1,
  90. SECURITY_CREATOR_OWNER_RID,
  91. 0, 0, 0, 0, 0, 0, 0,
  92. &pCreatorSid);
  93. bSuccess = bSuccess && pAllocateAndInitializeSid(&NtAuthority,
  94. 2,
  95. SECURITY_BUILTIN_DOMAIN_RID,
  96. DOMAIN_ALIAS_RID_USERS,
  97. 0, 0, 0, 0, 0, 0,
  98. &pUsersSid);
  99. if (bSuccess) {
  100. //
  101. // Initialize Access structures describing the ACEs we want:
  102. // System Full Control
  103. // Admins Full Control
  104. //
  105. // We'll take advantage of the fact that the unlocked private keys is
  106. // the same as the device parameters key and they are a superset of the
  107. // locked private keys.
  108. //
  109. // When we create the DACL for the private key we'll specify a subset of
  110. // the ExplicitAccess array.
  111. //
  112. for (i = 0; i < 5; i++) {
  113. ExplicitAccess[i].grfAccessMode = SET_ACCESS;
  114. ExplicitAccess[i].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  115. ExplicitAccess[i].Trustee.pMultipleTrustee = NULL;
  116. ExplicitAccess[i].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  117. ExplicitAccess[i].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  118. ExplicitAccess[i].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  119. }
  120. ExplicitAccess[0].grfAccessPermissions = KEY_ALL_ACCESS;
  121. ExplicitAccess[0].Trustee.ptstrName = (LPTSTR)pAdminSid;
  122. ExplicitAccess[1].grfAccessPermissions = KEY_ALL_ACCESS;
  123. ExplicitAccess[1].Trustee.ptstrName = (LPTSTR)pSystemSid;
  124. ExplicitAccess[2].grfAccessPermissions = KEY_ALL_ACCESS;
  125. ExplicitAccess[2].Trustee.ptstrName = (LPTSTR)pCreatorSid;
  126. ExplicitAccess[3].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
  127. ExplicitAccess[3].Trustee.ptstrName = (LPTSTR)pPowerUsersSid;
  128. ExplicitAccess[4].grfAccessPermissions = GENERIC_READ;
  129. ExplicitAccess[4].Trustee.ptstrName = (LPTSTR)pUsersSid;
  130. dwError = (DWORD)pSetEntriesInAcl( 5,
  131. ExplicitAccess,
  132. NULL,
  133. &pNewDACL );
  134. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(pAdvApi32,SET_SECURITY_INFO);
  135. if (pSetSecurityInfo == NULL)
  136. {
  137. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  138. goto ErrorCleanup;
  139. }
  140. dwError = pSetSecurityInfo(
  141. hObject,
  142. ObjectType,
  143. DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
  144. NULL,
  145. NULL,
  146. pNewDACL,
  147. NULL
  148. );
  149. }
  150. ErrorCleanup:
  151. pFreeSid = (PFREESID_FN)
  152. GetProcAddress(pAdvApi32,
  153. FREE_SID);
  154. if(pAdminSid)
  155. pFreeSid(pAdminSid);
  156. if(pSystemSid)
  157. pFreeSid(pSystemSid);
  158. if(pPowerUsersSid)
  159. pFreeSid(pPowerUsersSid);
  160. if(pCreatorSid)
  161. pFreeSid(pCreatorSid);
  162. if(pUsersSid)
  163. pFreeSid(pUsersSid);
  164. if(pNewDACL)
  165. LocalFree(pNewDACL);
  166. if(pAdvApi32)
  167. FreeLibrary( pAdvApi32 );
  168. return bSuccess;
  169. }
  170. BOOL
  171. AddACLToStoreObjectSecurityDescriptor(
  172. HANDLE hObject,
  173. SE_OBJECT_TYPE ObjectType
  174. )
  175. {
  176. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  177. SID_IDENTIFIER_AUTHORITY CreatorAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  178. PSID pAdminSid = NULL;
  179. PSID pSystemSid = NULL;
  180. PSID pPowerUsersSid = NULL;
  181. PSID pCreatorSid = NULL;
  182. PSID pUsersSid = NULL;
  183. PACL pNewDACL;
  184. DWORD dwError;
  185. BOOL bSuccess;
  186. DWORD i;
  187. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid = NULL;
  188. PSETENTRIESINACL pSetEntriesInAcl = NULL;
  189. HMODULE pAdvApi32 = NULL;
  190. PFREESID_FN pFreeSid = NULL;
  191. PSETSECURITYINFO_FN pSetSecurityInfo;
  192. EXPLICIT_ACCESS ExplicitAccess[6];
  193. pAdvApi32 = LoadLibrary(ADVAPI_32_DLL);
  194. if (!pAdvApi32) {
  195. return(FALSE);
  196. }
  197. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  198. GetProcAddress(pAdvApi32,
  199. ALLOCATE_AND_INITITIALIZE_SID);
  200. if (pAllocateAndInitializeSid == NULL)
  201. {
  202. goto ErrorCleanup;
  203. }
  204. #ifdef UNICODE
  205. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclW" ));
  206. #else
  207. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclA" ));
  208. #endif
  209. if (!pSetEntriesInAcl) {
  210. FreeLibrary( pAdvApi32 );
  211. return(FALSE);
  212. }
  213. //
  214. // Create SIDs - Admins and System
  215. //
  216. bSuccess = pAllocateAndInitializeSid( &NtAuthority,
  217. 2,
  218. SECURITY_BUILTIN_DOMAIN_RID,
  219. DOMAIN_ALIAS_RID_ADMINS,
  220. 0, 0, 0, 0, 0, 0,
  221. &pAdminSid);
  222. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  223. 1,
  224. SECURITY_LOCAL_SYSTEM_RID,
  225. 0, 0, 0, 0, 0, 0, 0,
  226. &pSystemSid);
  227. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  228. 2,
  229. SECURITY_BUILTIN_DOMAIN_RID,
  230. DOMAIN_ALIAS_RID_POWER_USERS,
  231. 0, 0, 0, 0, 0, 0,
  232. &pPowerUsersSid);
  233. bSuccess = bSuccess && pAllocateAndInitializeSid( &CreatorAuthority,
  234. 1,
  235. SECURITY_CREATOR_OWNER_RID,
  236. 0, 0, 0, 0, 0, 0, 0,
  237. &pCreatorSid);
  238. bSuccess = bSuccess && pAllocateAndInitializeSid(&NtAuthority,
  239. 2,
  240. SECURITY_BUILTIN_DOMAIN_RID,
  241. DOMAIN_ALIAS_RID_USERS,
  242. 0, 0, 0, 0, 0, 0,
  243. &pUsersSid);
  244. if (bSuccess) {
  245. //
  246. // Initialize Access structures describing the ACEs we want:
  247. // System Full Control
  248. // Admins Full Control
  249. //
  250. // We'll take advantage of the fact that the unlocked private keys is
  251. // the same as the device parameters key and they are a superset of the
  252. // locked private keys.
  253. //
  254. // When we create the DACL for the private key we'll specify a subset of
  255. // the ExplicitAccess array.
  256. //
  257. for (i = 0; i < 6; i++) {
  258. ExplicitAccess[i].grfAccessMode = SET_ACCESS;
  259. ExplicitAccess[i].Trustee.pMultipleTrustee = NULL;
  260. ExplicitAccess[i].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  261. ExplicitAccess[i].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  262. ExplicitAccess[i].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  263. }
  264. ExplicitAccess[0].grfAccessPermissions = KEY_ALL_ACCESS;
  265. ExplicitAccess[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  266. ExplicitAccess[0].Trustee.ptstrName = (LPTSTR)pAdminSid;
  267. ExplicitAccess[1].grfAccessPermissions = KEY_ALL_ACCESS;
  268. ExplicitAccess[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  269. ExplicitAccess[1].Trustee.ptstrName = (LPTSTR)pSystemSid;
  270. ExplicitAccess[2].grfAccessPermissions = KEY_ALL_ACCESS;
  271. ExplicitAccess[2].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  272. ExplicitAccess[2].Trustee.ptstrName = (LPTSTR)pCreatorSid;
  273. ExplicitAccess[3].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
  274. ExplicitAccess[3].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  275. ExplicitAccess[3].Trustee.ptstrName = (LPTSTR)pPowerUsersSid;
  276. ExplicitAccess[4].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE| KEY_CREATE_SUB_KEY |KEY_SET_VALUE;
  277. ExplicitAccess[4].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  278. ExplicitAccess[4].Trustee.ptstrName = (LPTSTR)pUsersSid;
  279. ExplicitAccess[5].grfAccessPermissions = DELETE;
  280. ExplicitAccess[5].grfInheritance = INHERIT_ONLY_ACE | SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  281. ExplicitAccess[5].Trustee.ptstrName = (LPTSTR)pUsersSid;
  282. dwError = (DWORD)pSetEntriesInAcl( 6,
  283. ExplicitAccess,
  284. NULL,
  285. &pNewDACL );
  286. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(pAdvApi32,SET_SECURITY_INFO);
  287. if (pSetSecurityInfo == NULL)
  288. {
  289. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  290. goto ErrorCleanup;
  291. }
  292. dwError = pSetSecurityInfo(
  293. hObject,
  294. ObjectType,
  295. DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
  296. NULL,
  297. NULL,
  298. pNewDACL,
  299. NULL
  300. );
  301. }
  302. ErrorCleanup:
  303. pFreeSid = (PFREESID_FN)
  304. GetProcAddress(pAdvApi32,
  305. FREE_SID);
  306. if(pAdminSid)
  307. pFreeSid(pAdminSid);
  308. if(pSystemSid)
  309. pFreeSid(pSystemSid);
  310. if(pPowerUsersSid)
  311. pFreeSid(pPowerUsersSid);
  312. if(pCreatorSid)
  313. pFreeSid(pCreatorSid);
  314. if(pUsersSid)
  315. pFreeSid(pUsersSid);
  316. if(pNewDACL)
  317. LocalFree(pNewDACL);
  318. if(pAdvApi32)
  319. FreeLibrary( pAdvApi32 );
  320. return bSuccess;
  321. }
  322. BOOL CreateRegAddAcl(VOID)
  323. {
  324. BOOL fRet = FALSE;
  325. DWORD dwDisposition, dwError = NO_ERROR;
  326. HKEY hKey = NULL, hKeyStore = NULL;
  327. dwError = RegCreateKeyEx(
  328. HKEY_LOCAL_MACHINE,
  329. MSLICENSING_REG_KEY,
  330. 0,
  331. NULL,
  332. REG_OPTION_NON_VOLATILE,
  333. KEY_ALL_ACCESS,
  334. NULL,
  335. &hKey,
  336. &dwDisposition
  337. );
  338. if (dwError != ERROR_SUCCESS) {
  339. return FALSE;
  340. }
  341. fRet = AddACLToObjectSecurityDescriptor(
  342. hKey,
  343. SE_REGISTRY_KEY
  344. );
  345. if (!fRet) {
  346. goto cleanup;
  347. }
  348. dwError = RegCreateKeyEx(
  349. hKey,
  350. MSLICENSING_STORE_SUBKEY,
  351. 0,
  352. NULL,
  353. REG_OPTION_NON_VOLATILE,
  354. KEY_ALL_ACCESS,
  355. NULL,
  356. &hKeyStore,
  357. &dwDisposition
  358. );
  359. if (dwError != ERROR_SUCCESS) {
  360. fRet = FALSE;
  361. goto cleanup;
  362. }
  363. fRet = AddACLToStoreObjectSecurityDescriptor(
  364. hKeyStore,
  365. SE_REGISTRY_KEY
  366. );
  367. cleanup:
  368. if (NULL != hKey)
  369. {
  370. RegCloseKey( hKey );
  371. }
  372. if (NULL != hKeyStore)
  373. {
  374. RegCloseKey( hKeyStore );
  375. }
  376. return fRet;
  377. }
  378. BOOL
  379. CreateAndWriteHWID(VOID)
  380. {
  381. BOOL fRet = FALSE;
  382. DWORD dwDisposition, dwError = NO_ERROR;
  383. HKEY hKey = NULL;
  384. HWID hwid;
  385. // Write HWID to registry
  386. dwError = RegCreateKeyEx(
  387. HKEY_LOCAL_MACHINE,
  388. MSLICENSING_HWID_KEY,
  389. 0,
  390. NULL,
  391. REG_OPTION_NON_VOLATILE,
  392. KEY_ALL_ACCESS,
  393. NULL,
  394. &hKey,
  395. &dwDisposition
  396. );
  397. if (dwError != ERROR_SUCCESS) {
  398. goto cleanup;
  399. }
  400. // generate HWID
  401. if (LICENSE_STATUS_OK == GenerateClientHWID(&hwid))
  402. {
  403. dwError = RegSetValueEx(hKey,
  404. MSLICENSING_HWID_VALUE,
  405. 0,
  406. REG_BINARY,
  407. (LPBYTE)&hwid,
  408. sizeof(HWID));
  409. if (dwError != ERROR_SUCCESS) {
  410. goto cleanup;
  411. }
  412. }
  413. fRet = TRUE;
  414. cleanup:
  415. if (NULL != hKey)
  416. {
  417. RegCloseKey( hKey );
  418. }
  419. return fRet;
  420. }
  421. BOOL SetupMSLicensingKey()
  422. {
  423. OSVERSIONINFOA OsVer;
  424. memset(&OsVer, 0x0, sizeof(OSVERSIONINFOA));
  425. OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
  426. GetVersionExA(&OsVer);
  427. if (VER_PLATFORM_WIN32_NT == OsVer.dwPlatformId) //It should be Windows NT
  428. {
  429. if(CreateRegAddAcl())
  430. {
  431. // generate and write the HWID
  432. if (CreateAndWriteHWID())
  433. {
  434. return TRUE;
  435. }
  436. }
  437. }
  438. return FALSE;
  439. }
  440. SYSPREPCMD sysPrepBackupCmd[] = { {_T("%windir%\\system32\\sessmgr.exe"), _T("-unregserver")} };
  441. DWORD numSysPrepBackupCmd = sizeof(sysPrepBackupCmd) / sizeof(sysPrepBackupCmd[0]);
  442. SYSPREPCMD sysPrepRestoreCmd[] = { {_T("%windir%\\system32\\sessmgr.exe"), _T("-service")} };
  443. DWORD numSysPrepRestoreCmd = sizeof(sysPrepRestoreCmd) / sizeof(sysPrepRestoreCmd[0]);
  444. class CNameSIDList : public list<CNameSID>
  445. {
  446. public:
  447. BOOL Save(HKEY hKey);
  448. BOOL LoadAndDelete(HKEY hKey);
  449. BOOL Find(PSID pSid, LPCWSTR *pwszName);
  450. void AddIfNotExist(CNameSID &NameSID);
  451. };
  452. DWORD
  453. BackupTSCustomSercurity();
  454. DWORD
  455. RestoreTSCustomSercurity();
  456. DWORD
  457. GetLocalSIDs(
  458. IN PSECURITY_DESCRIPTOR pSD,
  459. IN OUT CNameSIDList &NameSIDList);
  460. DWORD
  461. RenewLocalSIDs(
  462. IN OUT PSECURITY_DESCRIPTOR &pSD,
  463. IN OUT CNameSIDList &NameSIDList);
  464. DWORD
  465. ResetTSPublicPrivateKeys();
  466. BOOL
  467. CNameSIDList::Save(HKEY hKey)
  468. {
  469. BOOL bResult = FALSE;
  470. CNameSIDList::iterator it;
  471. //
  472. //calc the size of the buffer we need
  473. //
  474. DWORD dwBufSize = 0;
  475. LPCWSTR wszTmp1,wszTmp2;
  476. for(it=begin();it!=end(); it++)
  477. {
  478. wszTmp1 = (*it).GetName();
  479. wszTmp2 = (*it).GetTextSID();
  480. if(wszTmp1 && wszTmp2)
  481. {
  482. dwBufSize += (wcslen(wszTmp1)+wcslen(wszTmp2)+2)*sizeof(WCHAR);
  483. }
  484. }
  485. //
  486. //for second terminating 0.
  487. //
  488. dwBufSize += sizeof(WCHAR);
  489. //
  490. //Allocate buffer (this will also zeroinit it).
  491. //
  492. LPWSTR wszBuf = (LPWSTR)LocalAlloc(LPTR,dwBufSize);
  493. DWORD dwPos = 0;
  494. if(wszBuf)
  495. {
  496. //
  497. //Fill buffer with data
  498. //
  499. for(it=begin();it!=end(); it++)
  500. {
  501. wszTmp1 = (*it).GetName();
  502. wszTmp2 = (*it).GetTextSID();
  503. if(wszTmp1 && wszTmp2)
  504. {
  505. wcscpy(wszBuf+dwPos,wszTmp1);
  506. dwPos += wcslen(wszTmp1)+1;
  507. wcscpy(wszBuf+dwPos,wszTmp2);
  508. dwPos += wcslen(wszTmp2)+1;
  509. }
  510. }
  511. //
  512. //Save data in the registry
  513. //
  514. if(dwPos && RegSetValueExW(hKey,L"BackupSids",0,REG_MULTI_SZ,
  515. (CONST BYTE *)wszBuf,dwBufSize)==ERROR_SUCCESS)
  516. {
  517. bResult = TRUE;
  518. }
  519. LocalFree(wszBuf);
  520. }
  521. return bResult;
  522. }
  523. BOOL
  524. CNameSIDList::LoadAndDelete(HKEY hKey)
  525. {
  526. BOOL bResult = FALSE;
  527. DWORD err;
  528. DWORD ValueSize = 0;
  529. DWORD ValueType = 0;
  530. //Get the name of this computer
  531. //We will add it to every user's name,
  532. //to avoid confusion with global users.
  533. WCHAR wszComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  534. DWORD cComputerName = MAX_COMPUTERNAME_LENGTH + 1;
  535. if(!GetComputerNameW(wszComputerName, &cComputerName))
  536. {
  537. return FALSE;
  538. }
  539. err = RegQueryValueExW( hKey, L"BackupSids", NULL, &ValueType, NULL, &ValueSize );
  540. if(err == ERROR_SUCCESS && ValueType == REG_MULTI_SZ && ValueSize)
  541. {
  542. LPWSTR wszBuf = (LPWSTR)LocalAlloc(LPTR,ValueSize);
  543. if ( wszBuf )
  544. {
  545. err = RegQueryValueExW( hKey, L"BackupSids", NULL, &ValueType,
  546. (BYTE *) wszBuf, &ValueSize );
  547. RegDeleteValueW(hKey,L"BackupSids");
  548. if(err == ERROR_SUCCESS )
  549. {
  550. LPCWSTR wszTmp1,wszTmp2;
  551. DWORD dwPos = 0,dwMaxPos = ValueSize/sizeof(WCHAR);
  552. while(dwPos < dwMaxPos)
  553. {
  554. wszTmp1 = wszBuf + dwPos;
  555. dwPos += wcslen(wszTmp1) + 1;
  556. wszTmp2 = wszBuf + dwPos;
  557. dwPos += wcslen(wszTmp2) + 1;
  558. PSID pSid;
  559. if(ConvertStringSidToSidW(wszTmp2,&pSid))
  560. {
  561. LPWSTR wszUserName = (LPWSTR) LocalAlloc(LPTR,
  562. (wcslen(wszTmp1) + cComputerName + 2)*sizeof(WCHAR));
  563. if(!wszUserName)
  564. {
  565. LocalFree(pSid);
  566. LocalFree(wszBuf);
  567. return FALSE;
  568. }
  569. wcsncpy(wszUserName, wszComputerName, cComputerName);
  570. wcscat(wszUserName, L"\\");
  571. wcscat(wszUserName, wszTmp1);
  572. try
  573. {
  574. CNameSID NameSID(wszUserName,pSid);
  575. push_back(NameSID);
  576. }
  577. catch(DWORD Except)
  578. {
  579. LocalFree(pSid);
  580. LocalFree(wszUserName);
  581. LocalFree(wszBuf);
  582. return FALSE;
  583. }
  584. LocalFree(pSid);
  585. LocalFree(wszUserName);
  586. }
  587. }
  588. bResult = TRUE;
  589. }
  590. LocalFree(wszBuf);
  591. }
  592. }
  593. return bResult;
  594. }
  595. BOOL
  596. CNameSIDList::Find(PSID pSid, LPCWSTR *pwszName)
  597. {
  598. *pwszName = NULL;
  599. CNameSIDList::iterator it;
  600. for(it=begin();it!=end(); it++)
  601. {
  602. if(EqualSid(pSid,(*it).GetSID()))
  603. {
  604. *pwszName = (*it).GetName();
  605. return TRUE;
  606. }
  607. }
  608. return FALSE;
  609. }
  610. void
  611. CNameSIDList::AddIfNotExist(CNameSID &NameSID)
  612. {
  613. LPCWSTR wszName;
  614. if(!Find(NameSID.GetSID(),&wszName))
  615. {
  616. push_back(NameSID);
  617. }
  618. }
  619. VOID
  620. RunSysPrepCommands( PSYSPREPCMD pszCmds )
  621. {
  622. STARTUPINFO startupinfo;
  623. PROCESS_INFORMATION process_information;
  624. BOOL bSuccess;
  625. DWORD dwErr;
  626. TCHAR pszCommand[ MAX_PATH + 1 ];
  627. TCHAR pszParm[ MAX_PATH + 1 ];
  628. DWORD nExpandedExeName;
  629. try {
  630. //
  631. // CreateProcessW() will fail if lpCommandLine is a const string
  632. // AV if it is actually point to LPCTSTR.
  633. //
  634. nExpandedExeName = ExpandEnvironmentStrings(
  635. pszCmds->pszFullExePath,
  636. pszCommand,
  637. sizeof(pszCommand) / sizeof(pszCommand[0])
  638. );
  639. if( 0 == nExpandedExeName || nExpandedExeName >= sizeof(pszCommand) / sizeof(pszCommand[0]) )
  640. {
  641. // can't expand full path exe name or expanded string has too many
  642. // chars, bail out
  643. LOGMESSAGE0(_T("Can't expand environment string.") );
  644. return;
  645. }
  646. lstrcpy( pszParm, pszCmds->pszExeParm );
  647. ZeroMemory( &startupinfo, sizeof(startupinfo) );
  648. startupinfo.cb = sizeof(startupinfo);
  649. startupinfo.dwFlags = STARTF_USESHOWWINDOW;
  650. startupinfo.wShowWindow = SW_HIDE | SW_SHOWMINNOACTIVE;
  651. LOGMESSAGE2(_T("Running command %s %s"), pszCmds, pszParm);
  652. bSuccess = CreateProcess( pszCommand,
  653. pszParm,
  654. NULL,
  655. NULL,
  656. FALSE,
  657. CREATE_DEFAULT_ERROR_MODE,
  658. NULL,
  659. NULL,
  660. &startupinfo,
  661. &process_information );
  662. if ( !bSuccess )
  663. {
  664. LOGMESSAGE2(_T("ERROR: failed to spawn %s %s process."), pszCommand, pszParm);
  665. }
  666. else
  667. {
  668. dwErr = WaitForSingleObject( process_information.hProcess, RUNONCE_DEFAULTWAIT );
  669. if ( dwErr != NO_ERROR )
  670. {
  671. LOGMESSAGE2(_T("ERROR: process %s %s failed to complete in time."), pszCommand, pszParm);
  672. }
  673. else
  674. {
  675. LOGMESSAGE2(_T("INFO: process %s %s completed successfully."), pszCommand, pszParm);
  676. }
  677. CloseHandle( process_information.hProcess );
  678. CloseHandle( process_information.hThread );
  679. }
  680. }
  681. catch(...) {
  682. LOGMESSAGE0(_T("Command caused exception.") );
  683. }
  684. return;
  685. }
  686. __declspec( dllexport )
  687. VOID
  688. SysPrepBackup( void )
  689. {
  690. DWORD dwIndex;
  691. TCHAR szLogFile[MAX_PATH + 1];
  692. ZeroMemory(szLogFile, sizeof(szLogFile));
  693. ExpandEnvironmentStrings(LOGFILE, szLogFile, MAX_PATH);
  694. LOGMESSAGEINIT(szLogFile, MODULENAME);
  695. LOGMESSAGE0( _T("Entering SysPrepBackup") );
  696. for(dwIndex = 0; dwIndex < numSysPrepBackupCmd; dwIndex++ )
  697. {
  698. RunSysPrepCommands( &sysPrepBackupCmd[dwIndex] );
  699. }
  700. DWORD err = BackupTSCustomSercurity();
  701. if(err != ERROR_SUCCESS)
  702. {
  703. LOGMESSAGE1(_T("ERROR: BackupTSCustomSercurity() FAILED: %d"),err );
  704. }
  705. LOGMESSAGE0( _T("SysPrepBackup completed") );
  706. }
  707. __declspec( dllexport )
  708. VOID
  709. SysPrepRestore( void )
  710. {
  711. DWORD dwIndex;
  712. TCHAR szLogFile[MAX_PATH + 1];
  713. ZeroMemory( szLogFile, sizeof(szLogFile) );
  714. ExpandEnvironmentStrings(LOGFILE, szLogFile, MAX_PATH);
  715. LOGMESSAGEINIT(szLogFile, MODULENAME);
  716. LOGMESSAGE0( _T("Entering SysPrepRestore") );
  717. for(dwIndex = 0; dwIndex < numSysPrepRestoreCmd; dwIndex++ )
  718. {
  719. RunSysPrepCommands( &sysPrepRestoreCmd[dwIndex] );
  720. }
  721. DWORD err = RestoreTSCustomSercurity();
  722. if(err != ERROR_SUCCESS)
  723. {
  724. LOGMESSAGE1(_T("ERROR: RestoreTSCustomSercurity() FAILED: %d"),err );
  725. }
  726. err = ResetTSPublicPrivateKeys();
  727. if(err != ERROR_SUCCESS)
  728. {
  729. LOGMESSAGE1(_T("ERROR: ResetTSPublicPrivateKeys() FAILED: %d"),err );
  730. }
  731. //
  732. // This can be done at SysPrep time instead of SysRestore time; however, sysprep
  733. // might support back out sysprep so we delay deleting licensing key at restore time,
  734. // also, to keep it consistent with ResetTSPublicPrivateKey().
  735. //
  736. err = SHDeleteKey( HKEY_LOCAL_MACHINE, MSLICENSING_REG_KEY );
  737. if(err != ERROR_SUCCESS)
  738. {
  739. LOGMESSAGE1(_T("ERROR: Deleting MSLicensing key FAILED: %d"),err );
  740. }
  741. err = SetupMSLicensingKey();
  742. if(err != ERROR_SUCCESS)
  743. {
  744. LOGMESSAGE1(_T("ERROR: SetupMSLicensingKey() FAILED: %d"),err );
  745. }
  746. LOGMESSAGE0( _T("SysPrepRestore completed") );
  747. }
  748. //*************************************************************
  749. //
  750. // BackupTSCustomSercurity()
  751. //
  752. // Purpose: Creates a list of all local SIDs and
  753. // corresponding names included in WinStation's
  754. // security descriptors, and saves it
  755. // in the registry.
  756. //
  757. // Parameters: NONE
  758. //
  759. // Return: error code if fails, ERROR_SUCCESS otherwise
  760. //
  761. // Comments:
  762. //
  763. // History: Date Author Comment
  764. // 03/13/01 skuzin Created
  765. //
  766. //*************************************************************
  767. DWORD
  768. BackupTSCustomSercurity()
  769. {
  770. HKEY hKey;
  771. DWORD err;
  772. //
  773. //Open "SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations" key
  774. //
  775. err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  776. _T("SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations"),
  777. 0,KEY_READ|KEY_WRITE, &hKey );
  778. if(err != ERROR_SUCCESS)
  779. {
  780. LOGMESSAGE1(_T("ERROR: BackupTSCustomSercurity - RegOpenKeyEx FAILED: %d"),err );
  781. return err;
  782. }
  783. CNameAndSDList NameSDList; //List of security descriptors
  784. CNameSIDList NameSIDList; //List of local SIDs
  785. err=EnumWinStationSecurityDescriptors( hKey, &NameSDList);
  786. if(err == ERROR_SUCCESS)
  787. {
  788. CNameAndSDList::iterator it;
  789. for(it=NameSDList.begin();it!=NameSDList.end(); it++)
  790. {
  791. //
  792. //If SD was not customized in most cases it is NULL
  793. //
  794. if((*it).m_pSD)
  795. {
  796. err = GetLocalSIDs((*it).m_pSD, NameSIDList);
  797. if(err != ERROR_SUCCESS)
  798. {
  799. LOGMESSAGE2(_T("ERROR: GetLocalSIDs for %s FAILED: %d"), (*it).m_pName, err );
  800. break;
  801. }
  802. }
  803. }
  804. if(err == ERROR_SUCCESS)
  805. {
  806. if(!NameSIDList.Save(hKey))
  807. {
  808. LOGMESSAGE0(_T("ERROR: BackupTSCustomSercurity - NameSIDList.Save FAILED"));
  809. err = ERROR_FILE_NOT_FOUND;
  810. }
  811. }
  812. }
  813. else
  814. {
  815. LOGMESSAGE1(_T("ERROR: RestoreTSCustomSercurity - ")
  816. _T("EnumWinStationSecurityDescriptors FAILED: %d"),err );
  817. }
  818. RegCloseKey(hKey);
  819. return err;
  820. }
  821. //*************************************************************
  822. //
  823. // RestoreTSCustomSercurity()
  824. //
  825. // Purpose: Gets a list of local SIDs and corresponding names
  826. // (saved by BackupTSCustomSercurity)
  827. // from the registry and updates all WinStation's
  828. // security descriptors with new SID for each
  829. // local account.
  830. //
  831. // Parameters: NONE
  832. //
  833. // Return: error code if fails, ERROR_SUCCESS otherwise
  834. //
  835. // Comments:
  836. //
  837. // History: Date Author Comment
  838. // 03/13/01 skuzin Created
  839. //
  840. //*************************************************************
  841. DWORD
  842. RestoreTSCustomSercurity()
  843. {
  844. HKEY hKey;
  845. DWORD err;
  846. //
  847. //Open "SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations" key
  848. //
  849. err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  850. _T("SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations"),
  851. 0,KEY_READ|KEY_WRITE, &hKey );
  852. if(err != ERROR_SUCCESS)
  853. {
  854. LOGMESSAGE1(_T("ERROR: RestoreTSCustomSercurity - RegOpenKeyEx FAILED: %d"),err );
  855. return err;
  856. }
  857. CNameAndSDList NameSDList; //List of security descriptors
  858. CNameSIDList NameSIDList; //List of local SIDs
  859. //Now load
  860. if(!NameSIDList.LoadAndDelete(hKey))
  861. {
  862. LOGMESSAGE0(_T("ERROR: RestoreTSCustomSercurity - NameSIDList.LoadAndDelete FAILED"));
  863. RegCloseKey(hKey);
  864. return ERROR_FILE_NOT_FOUND;
  865. }
  866. err=EnumWinStationSecurityDescriptors( hKey, &NameSDList);
  867. if(err == ERROR_SUCCESS)
  868. {
  869. CNameAndSDList::iterator it;
  870. for(it=NameSDList.begin();it!=NameSDList.end(); it++)
  871. {
  872. //
  873. //If SD was not customized in most cases it is NULL
  874. //
  875. if((*it).m_pSD)
  876. {
  877. err = RenewLocalSIDs((*it).m_pSD, NameSIDList);
  878. if(err == ERROR_SUCCESS)
  879. {
  880. err = SetWinStationSecurity( hKey, (*it).m_pName, (*it).m_pSD );
  881. if(err !=ERROR_SUCCESS)
  882. {
  883. LOGMESSAGE2(_T("ERROR: SetWinStationSecurity for %s FAILED: %d"),
  884. (*it).m_pName, err );
  885. break;
  886. }
  887. }
  888. else
  889. {
  890. LOGMESSAGE2(_T("ERROR: RenewLocalSIDs for %s FAILED: %d"),
  891. (*it).m_pName, err );
  892. break;
  893. }
  894. }
  895. }
  896. }
  897. else
  898. {
  899. LOGMESSAGE1(_T("ERROR: RestoreTSCustomSercurity - ")
  900. _T("EnumWinStationSecurityDescriptors FAILED: %d"),err );
  901. }
  902. RegCloseKey(hKey);
  903. return err;
  904. }
  905. //*************************************************************
  906. //
  907. // GetLocalSIDs()
  908. //
  909. // Purpose: Gets local SIDs from a security descriptor
  910. // and puts them in the list
  911. //
  912. // Parameters:
  913. // IN PSECURITY_DESCRIPTOR pSD,
  914. // IN OUT CNameSIDList &NameSIDList
  915. //
  916. // Return: error code if fails, ERROR_SUCCESS otherwise
  917. //
  918. // Comments:
  919. //
  920. // History: Date Author Comment
  921. // 03/13/01 skuzin Created
  922. //
  923. //*************************************************************
  924. DWORD
  925. GetLocalSIDs(
  926. IN PSECURITY_DESCRIPTOR pSD,
  927. IN OUT CNameSIDList &NameSIDList)
  928. {
  929. PACL pDacl;
  930. PACL pSacl;
  931. DWORD dwResult;
  932. DWORD cEntries = 0;
  933. dwResult = GetDacl(pSD,&pDacl);
  934. if(dwResult != ERROR_SUCCESS)
  935. {
  936. return dwResult;
  937. }
  938. dwResult = GetSacl(pSD,&pSacl);
  939. if(dwResult != ERROR_SUCCESS)
  940. {
  941. return dwResult;
  942. }
  943. DWORD dwCompNameSize = MAX_COMPUTERNAME_LENGTH + 1;
  944. WCHAR wszComputerName[MAX_COMPUTERNAME_LENGTH + 1];
  945. if(!GetComputerNameW(wszComputerName, &dwCompNameSize))
  946. {
  947. return GetLastError();
  948. }
  949. ACL_SIZE_INFORMATION asiAclSize;
  950. DWORD dwBufLength=sizeof(asiAclSize);
  951. ACCESS_ALLOWED_ACE *pAllowedAce;
  952. SYSTEM_AUDIT_ACE *pSystemAce;
  953. DWORD dwAcl_i;
  954. LPWSTR wszName;
  955. SID_NAME_USE eUse;
  956. if(pDacl)
  957. {
  958. if (GetAclInformation(pDacl,
  959. (LPVOID)&asiAclSize,
  960. (DWORD)dwBufLength,
  961. (ACL_INFORMATION_CLASS)AclSizeInformation))
  962. {
  963. for (dwAcl_i = 0; dwAcl_i < asiAclSize.AceCount; dwAcl_i++)
  964. {
  965. if(GetAce( pDacl, dwAcl_i, (LPVOID *)&pAllowedAce))
  966. {
  967. if(LookupSid((PSID)&(pAllowedAce->SidStart),&wszName,&eUse))
  968. {
  969. if(IsLocal(wszComputerName, wszName))
  970. {
  971. try
  972. {
  973. NameSIDList.AddIfNotExist(CNameSID(wszName,
  974. (PSID)&(pAllowedAce->SidStart)));
  975. }
  976. catch(DWORD Except)
  977. {
  978. LocalFree(wszName);
  979. return Except;
  980. }
  981. }
  982. LocalFree(wszName);
  983. }
  984. }
  985. }
  986. }
  987. }
  988. if(pSacl)
  989. {
  990. if (GetAclInformation(pSacl,
  991. (LPVOID)&asiAclSize,
  992. (DWORD)dwBufLength,
  993. (ACL_INFORMATION_CLASS)AclSizeInformation))
  994. {
  995. for (dwAcl_i = 0; dwAcl_i < asiAclSize.AceCount; dwAcl_i++)
  996. {
  997. if(GetAce( pSacl, dwAcl_i, (LPVOID *)&pSystemAce))
  998. {
  999. if(LookupSid((PSID)&(pSystemAce->SidStart),&wszName,&eUse))
  1000. {
  1001. if(IsLocal(wszComputerName, wszName))
  1002. {
  1003. try
  1004. {
  1005. NameSIDList.AddIfNotExist(CNameSID(wszName,
  1006. (PSID)&(pSystemAce->SidStart)));
  1007. }
  1008. catch(DWORD Except)
  1009. {
  1010. LocalFree(wszName);
  1011. return Except;
  1012. }
  1013. }
  1014. LocalFree(wszName);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. }
  1020. return ERROR_SUCCESS;
  1021. }
  1022. //*************************************************************
  1023. //
  1024. // RenewLocalSIDs()
  1025. //
  1026. // Purpose: Replaces all the local SIDs in a security
  1027. // descriptor with the new ones.
  1028. //
  1029. // Parameters:
  1030. // IN OUT PSECURITY_DESCRIPTOR &pSD,
  1031. // IN OUT CNameSIDList &NameSIDList
  1032. //
  1033. // Return: error code if fails, ERROR_SUCCESS otherwise
  1034. //
  1035. // Comments:
  1036. //
  1037. // History: Date Author Comment
  1038. // 03/13/01 skuzin Created
  1039. //
  1040. //*************************************************************
  1041. DWORD
  1042. RenewLocalSIDs(
  1043. IN OUT PSECURITY_DESCRIPTOR &pSD,
  1044. IN OUT CNameSIDList &NameSIDList)
  1045. {
  1046. PSECURITY_DESCRIPTOR pAbsoluteSD = NULL;
  1047. PACL pDacl = NULL;
  1048. PACL pSacl = NULL;
  1049. PSID pOwner = NULL;
  1050. PSID pPrimaryGroup = NULL;
  1051. DWORD dwResult;
  1052. dwResult = GetAbsoluteSD(
  1053. pSD,
  1054. &pAbsoluteSD,
  1055. &pDacl,
  1056. &pSacl,
  1057. &pOwner,
  1058. &pPrimaryGroup);
  1059. if(dwResult != ERROR_SUCCESS)
  1060. {
  1061. return dwResult;
  1062. }
  1063. ULONG cEntries;
  1064. PEXPLICIT_ACCESS_W pListOfEntries;
  1065. LPCWSTR wszName;
  1066. PACL pNewDacl = NULL, pNewSacl = NULL;
  1067. __try
  1068. {
  1069. if(pDacl)
  1070. {
  1071. dwResult = GetExplicitEntriesFromAclW(pDacl, &cEntries, &pListOfEntries);
  1072. if(dwResult != ERROR_SUCCESS)
  1073. {
  1074. return dwResult;
  1075. }
  1076. for(ULONG i=0;i<cEntries;i++)
  1077. {
  1078. if(pListOfEntries[i].Trustee.TrusteeForm == TRUSTEE_IS_SID &&
  1079. NameSIDList.Find((PSID)pListOfEntries[i].Trustee.ptstrName, &wszName))
  1080. {
  1081. pListOfEntries[i].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  1082. pListOfEntries[i].Trustee.ptstrName = const_cast<LPWSTR>(wszName);
  1083. }
  1084. }
  1085. dwResult = SetEntriesInAclW(cEntries,pListOfEntries,NULL,&pNewDacl);
  1086. LocalFree(pListOfEntries);
  1087. if(dwResult != ERROR_SUCCESS)
  1088. {
  1089. return dwResult;
  1090. }
  1091. if(!SetSecurityDescriptorDacl(pAbsoluteSD,TRUE,pNewDacl,FALSE))
  1092. {
  1093. return GetLastError();
  1094. }
  1095. }
  1096. if(pSacl)
  1097. {
  1098. dwResult = GetExplicitEntriesFromAclW(pSacl, &cEntries, &pListOfEntries);
  1099. if(dwResult != ERROR_SUCCESS)
  1100. {
  1101. return dwResult;
  1102. }
  1103. for(ULONG i=0;i<cEntries;i++)
  1104. {
  1105. if(pListOfEntries[i].Trustee.TrusteeForm == TRUSTEE_IS_SID &&
  1106. NameSIDList.Find((PSID)pListOfEntries[i].Trustee.ptstrName, &wszName))
  1107. {
  1108. pListOfEntries[i].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  1109. pListOfEntries[i].Trustee.ptstrName = const_cast<LPWSTR>(wszName);
  1110. }
  1111. }
  1112. dwResult = SetEntriesInAclW(cEntries,pListOfEntries,NULL,&pNewSacl);
  1113. LocalFree(pListOfEntries);
  1114. if(dwResult != ERROR_SUCCESS)
  1115. {
  1116. return dwResult;
  1117. }
  1118. if(!SetSecurityDescriptorSacl(pAbsoluteSD,TRUE,pNewSacl,FALSE))
  1119. {
  1120. return GetLastError();
  1121. }
  1122. }
  1123. PSECURITY_DESCRIPTOR pTmpSD;
  1124. dwResult = GetSelfRelativeSD(pAbsoluteSD,&pTmpSD);
  1125. if(dwResult != ERROR_SUCCESS)
  1126. {
  1127. return dwResult;
  1128. }
  1129. LocalFree(pSD);
  1130. pSD = pTmpSD;
  1131. }
  1132. __finally
  1133. {
  1134. if(pAbsoluteSD)
  1135. {
  1136. LocalFree(pAbsoluteSD);
  1137. }
  1138. if(pDacl)
  1139. {
  1140. LocalFree(pDacl);
  1141. }
  1142. if(pSacl)
  1143. {
  1144. LocalFree(pSacl);
  1145. }
  1146. if(pOwner)
  1147. {
  1148. LocalFree(pOwner);
  1149. }
  1150. if(pPrimaryGroup)
  1151. {
  1152. LocalFree(pPrimaryGroup);
  1153. }
  1154. if(pNewDacl)
  1155. {
  1156. LocalFree(pNewDacl);
  1157. }
  1158. if(pNewSacl)
  1159. {
  1160. LocalFree(pNewSacl);
  1161. }
  1162. }
  1163. return ERROR_SUCCESS;
  1164. }
  1165. //*************************************************************
  1166. //
  1167. // ResetTSPublicPrivateKeys()
  1168. //
  1169. // Purpose: Deletes keys from LSA secret, so that sysprep'd
  1170. // machines don't share the same keys
  1171. //
  1172. // Parameters: NONE
  1173. //
  1174. // Return: error code if fails, ERROR_SUCCESS otherwise
  1175. //
  1176. // Comments:
  1177. //
  1178. // History: Date Author Comment
  1179. // 06/12/01 robleit Created
  1180. //
  1181. //*************************************************************
  1182. DWORD
  1183. ResetTSPublicPrivateKeys()
  1184. {
  1185. LSA_HANDLE
  1186. PolicyHandle;
  1187. UNICODE_STRING
  1188. SecretKeyName;
  1189. DWORD
  1190. Status;
  1191. LOGMESSAGE0(_T("INFO: Starting ResetTSPublicPrivateKeys.") );
  1192. Status = OpenPolicy( NULL, POLICY_CREATE_SECRET, &PolicyHandle );
  1193. if( ERROR_SUCCESS != Status )
  1194. {
  1195. return LsaNtStatusToWinError(Status);
  1196. }
  1197. SecretKeyName.Buffer = PRIVATE_KEY_NAME;
  1198. SecretKeyName.Length = sizeof(PRIVATE_KEY_NAME) - sizeof(WCHAR);
  1199. SecretKeyName.MaximumLength = sizeof(PRIVATE_KEY_NAME) ;
  1200. Status = LsaStorePrivateData(
  1201. PolicyHandle,
  1202. &SecretKeyName,
  1203. NULL
  1204. );
  1205. if (Status != STATUS_SUCCESS)
  1206. {
  1207. LOGMESSAGE1(_T("ERROR: ResetTSPublicPrivateKeys() FAILED to delete private key: %d"),Status );
  1208. }
  1209. SecretKeyName.Buffer = X509_CERT_PRIVATE_KEY_NAME;
  1210. SecretKeyName.Length = sizeof(X509_CERT_PRIVATE_KEY_NAME) - sizeof(WCHAR);
  1211. SecretKeyName.MaximumLength = sizeof(X509_CERT_PRIVATE_KEY_NAME);
  1212. Status = LsaStorePrivateData(
  1213. PolicyHandle,
  1214. &SecretKeyName,
  1215. NULL
  1216. );
  1217. if (Status != STATUS_SUCCESS)
  1218. {
  1219. LOGMESSAGE1(_T("WARNING: ResetTSPublicPrivateKeys() FAILED to delete X509 private key: %d"),Status );
  1220. }
  1221. SecretKeyName.Buffer = X509_CERT_PUBLIC_KEY_NAME;
  1222. SecretKeyName.Length = sizeof(X509_CERT_PUBLIC_KEY_NAME) - sizeof(WCHAR);
  1223. SecretKeyName.MaximumLength = sizeof(X509_CERT_PUBLIC_KEY_NAME);
  1224. Status = LsaStorePrivateData(
  1225. PolicyHandle,
  1226. &SecretKeyName,
  1227. NULL
  1228. );
  1229. if (Status != STATUS_SUCCESS)
  1230. {
  1231. LOGMESSAGE1(_T("WARNING: ResetTSPublicPrivateKeys() FAILED to delete X509 public key: %d"),Status );
  1232. }
  1233. SecretKeyName.Buffer = BETA_LICENSING_TIME_BOMB_5_1;
  1234. SecretKeyName.Length = sizeof(BETA_LICENSING_TIME_BOMB_5_1) - sizeof(WCHAR);
  1235. SecretKeyName.MaximumLength = sizeof(BETA_LICENSING_TIME_BOMB_5_1);
  1236. Status = LsaStorePrivateData(
  1237. PolicyHandle,
  1238. &SecretKeyName,
  1239. NULL
  1240. );
  1241. if (Status != STATUS_SUCCESS)
  1242. {
  1243. LOGMESSAGE1(_T("WARNING: ResetTSPublicPrivateKeys() FAILED to delete BETA_LICENSING_TIME_BOMB_5_1 key: %d"),Status );
  1244. }
  1245. SecretKeyName.Buffer = RTM_LICENSING_TIME_BOMB_5_1;
  1246. SecretKeyName.Length = sizeof(RTM_LICENSING_TIME_BOMB_5_1) - sizeof(WCHAR);
  1247. SecretKeyName.MaximumLength = sizeof(RTM_LICENSING_TIME_BOMB_5_1);
  1248. Status = LsaStorePrivateData(
  1249. PolicyHandle,
  1250. &SecretKeyName,
  1251. NULL
  1252. );
  1253. if (Status != STATUS_SUCCESS)
  1254. {
  1255. LOGMESSAGE1(_T("WARNING: ResetTSPublicPrivateKeys() FAILED to delete RTM_LICENSING_TIME_BOMB_5_1 key: %d"),Status );
  1256. }
  1257. LsaClose( PolicyHandle );
  1258. Status = LsaNtStatusToWinError( Status );
  1259. return Status;
  1260. }