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.

1602 lines
49 KiB

  1. /**MOD+**********************************************************************/
  2. /* Module: reglic.cpp */
  3. /* */
  4. /* Purpose: Creates and sets appropriate ACLs on MSLicensing registry key */
  5. /* This is done during DllRegisterServer which should be called */
  6. /* by an Admin so the user should have rights to complete this */
  7. /* We do this during control registration as it is the only valid*/
  8. /* place to do it during the control setup...e.g web CAB setup */
  9. /* and Iexpress setups can't handle this by themselves */
  10. /* */
  11. /* This code was shamelessly stolen from the client ACME setup */
  12. /* Copyright(C) Microsoft Corporation 1998-2000 */
  13. /* */
  14. /****************************************************************************/
  15. #ifndef OS_WINCE
  16. #include <adcg.h>
  17. #include <cryptkey.h>
  18. #include "reglic.h"
  19. #define MSLICENSING_STORE_KEY _T("SOFTWARE\\Microsoft\\MSLicensing\\Store")
  20. typedef DWORD (*PSETENTRIESINACL)(
  21. ULONG cCountOfExplicitEntries, // number of entries
  22. PEXPLICIT_ACCESS pListOfExplicitEntries, // buffer
  23. PACL OldAcl, // original ACL
  24. PACL *NewAcl // new ACL
  25. );
  26. //Note, only a user with sufficient privileges will
  27. //be able to complete this operation (same as the DllRegisterServer)
  28. //We don't do anything on failure
  29. BOOL SetupMSLicensingKey()
  30. {
  31. OSVERSIONINFOA OsVer;
  32. memset(&OsVer, 0x0, sizeof(OSVERSIONINFOA));
  33. OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
  34. GetVersionExA(&OsVer);
  35. if (VER_PLATFORM_WIN32_NT == OsVer.dwPlatformId) //It should be Windows NT
  36. {
  37. if(CreateRegAddAcl())
  38. {
  39. // generate and write the HWID
  40. if (CreateAndWriteHWID())
  41. {
  42. return TRUE;
  43. }
  44. else
  45. {
  46. OutputDebugString(_T("FAILED to add Terminal Services MSLicensing HWID"));
  47. }
  48. }
  49. else
  50. {
  51. OutputDebugString(_T("FAILED to add Terminal Services MSLicensing key"));
  52. }
  53. }
  54. return FALSE;
  55. }
  56. BOOL
  57. AddACLToObjectSecurityDescriptor(
  58. HANDLE hObject,
  59. SE_OBJECT_TYPE ObjectType
  60. )
  61. {
  62. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  63. SID_IDENTIFIER_AUTHORITY CreatorAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  64. PSID pAdminSid = NULL;
  65. PSID pSystemSid = NULL;
  66. PSID pPowerUsersSid = NULL;
  67. PSID pCreatorSid = NULL;
  68. PSID pUsersSid = NULL;
  69. PACL pNewDACL = NULL;
  70. DWORD dwError;
  71. BOOL bSuccess = TRUE;
  72. DWORD i;
  73. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid = NULL;
  74. PSETENTRIESINACL pSetEntriesInAcl = NULL;
  75. HMODULE pAdvApi32 = NULL;
  76. PFREESID_FN pFreeSid = NULL;
  77. PSETSECURITYINFO_FN pSetSecurityInfo;
  78. pAdvApi32 = LoadLibrary(ADVAPI_32_DLL);
  79. if (!pAdvApi32) {
  80. return(FALSE);
  81. }
  82. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  83. GetProcAddress(pAdvApi32,
  84. ALLOCATE_AND_INITITIALIZE_SID);
  85. if (pAllocateAndInitializeSid == NULL)
  86. {
  87. goto ErrorCleanup;
  88. }
  89. #ifdef UNICODE
  90. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclW" ));
  91. #else
  92. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclA" ));
  93. #endif
  94. if (!pSetEntriesInAcl) {
  95. FreeLibrary( pAdvApi32 );
  96. return(FALSE);
  97. }
  98. EXPLICIT_ACCESS ExplicitAccess[5];
  99. //
  100. // Create SIDs - Admins and System
  101. //
  102. bSuccess = pAllocateAndInitializeSid( &NtAuthority,
  103. 2,
  104. SECURITY_BUILTIN_DOMAIN_RID,
  105. DOMAIN_ALIAS_RID_ADMINS,
  106. 0, 0, 0, 0, 0, 0,
  107. &pAdminSid);
  108. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  109. 1,
  110. SECURITY_LOCAL_SYSTEM_RID,
  111. 0, 0, 0, 0, 0, 0, 0,
  112. &pSystemSid);
  113. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  114. 2,
  115. SECURITY_BUILTIN_DOMAIN_RID,
  116. DOMAIN_ALIAS_RID_POWER_USERS,
  117. 0, 0, 0, 0, 0, 0,
  118. &pPowerUsersSid);
  119. bSuccess = bSuccess && pAllocateAndInitializeSid( &CreatorAuthority,
  120. 1,
  121. SECURITY_CREATOR_OWNER_RID,
  122. 0, 0, 0, 0, 0, 0, 0,
  123. &pCreatorSid);
  124. bSuccess = bSuccess && pAllocateAndInitializeSid(&NtAuthority,
  125. 2,
  126. SECURITY_BUILTIN_DOMAIN_RID,
  127. DOMAIN_ALIAS_RID_USERS,
  128. 0, 0, 0, 0, 0, 0,
  129. &pUsersSid);
  130. if (bSuccess) {
  131. //
  132. // Initialize Access structures describing the ACEs we want:
  133. // System Full Control
  134. // Admins Full Control
  135. //
  136. // We'll take advantage of the fact that the unlocked private keys is
  137. // the same as the device parameters key and they are a superset of the
  138. // locked private keys.
  139. //
  140. // When we create the DACL for the private key we'll specify a subset of
  141. // the ExplicitAccess array.
  142. //
  143. for (i = 0; i < 5; i++) {
  144. ExplicitAccess[i].grfAccessMode = SET_ACCESS;
  145. ExplicitAccess[i].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  146. ExplicitAccess[i].Trustee.pMultipleTrustee = NULL;
  147. ExplicitAccess[i].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  148. ExplicitAccess[i].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  149. ExplicitAccess[i].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  150. }
  151. ExplicitAccess[0].grfAccessPermissions = KEY_ALL_ACCESS;
  152. ExplicitAccess[0].Trustee.ptstrName = (LPTSTR)pAdminSid;
  153. ExplicitAccess[1].grfAccessPermissions = KEY_ALL_ACCESS;
  154. ExplicitAccess[1].Trustee.ptstrName = (LPTSTR)pSystemSid;
  155. ExplicitAccess[2].grfAccessPermissions = KEY_ALL_ACCESS;
  156. ExplicitAccess[2].Trustee.ptstrName = (LPTSTR)pCreatorSid;
  157. ExplicitAccess[3].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
  158. ExplicitAccess[3].Trustee.ptstrName = (LPTSTR)pPowerUsersSid;
  159. ExplicitAccess[4].grfAccessPermissions = GENERIC_READ;
  160. ExplicitAccess[4].Trustee.ptstrName = (LPTSTR)pUsersSid;
  161. dwError = (DWORD)pSetEntriesInAcl( 5,
  162. ExplicitAccess,
  163. NULL,
  164. &pNewDACL );
  165. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(pAdvApi32,SET_SECURITY_INFO);
  166. if (pSetSecurityInfo == NULL)
  167. {
  168. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  169. goto ErrorCleanup;
  170. }
  171. dwError = pSetSecurityInfo(
  172. hObject,
  173. ObjectType,
  174. DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
  175. NULL,
  176. NULL,
  177. pNewDACL,
  178. NULL
  179. );
  180. }
  181. ErrorCleanup:
  182. pFreeSid = (PFREESID_FN)
  183. GetProcAddress(pAdvApi32,
  184. FREE_SID);
  185. if(pFreeSid)
  186. {
  187. if(pAdminSid)
  188. pFreeSid(pAdminSid);
  189. if(pSystemSid)
  190. pFreeSid(pSystemSid);
  191. if(pPowerUsersSid)
  192. pFreeSid(pPowerUsersSid);
  193. if(pCreatorSid)
  194. pFreeSid(pCreatorSid);
  195. if(pUsersSid)
  196. pFreeSid(pUsersSid);
  197. }
  198. if(pNewDACL)
  199. LocalFree(pNewDACL);
  200. if(pAdvApi32)
  201. FreeLibrary( pAdvApi32 );
  202. return bSuccess;
  203. }
  204. BOOL
  205. AddACLToStoreObjectSecurityDescriptor(
  206. HANDLE hObject,
  207. SE_OBJECT_TYPE ObjectType
  208. )
  209. {
  210. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  211. SID_IDENTIFIER_AUTHORITY CreatorAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  212. PSID pAdminSid = NULL;
  213. PSID pSystemSid = NULL;
  214. PSID pPowerUsersSid = NULL;
  215. PSID pCreatorSid = NULL;
  216. PSID pUsersSid = NULL;
  217. PACL pNewDACL = NULL;
  218. DWORD dwError;
  219. BOOL bSuccess = TRUE;
  220. DWORD i;
  221. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid = NULL;
  222. PSETENTRIESINACL pSetEntriesInAcl = NULL;
  223. HMODULE pAdvApi32 = NULL;
  224. PFREESID_FN pFreeSid = NULL;
  225. PSETSECURITYINFO_FN pSetSecurityInfo;
  226. EXPLICIT_ACCESS ExplicitAccess[6];
  227. pAdvApi32 = LoadLibrary(ADVAPI_32_DLL);
  228. if (!pAdvApi32) {
  229. return(FALSE);
  230. }
  231. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  232. GetProcAddress(pAdvApi32,
  233. ALLOCATE_AND_INITITIALIZE_SID);
  234. if (pAllocateAndInitializeSid == NULL)
  235. {
  236. goto ErrorCleanup;
  237. }
  238. #ifdef UNICODE
  239. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclW" ));
  240. #else
  241. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclA" ));
  242. #endif
  243. if (!pSetEntriesInAcl) {
  244. FreeLibrary( pAdvApi32 );
  245. return(FALSE);
  246. }
  247. //
  248. // Create SIDs - Admins and System
  249. //
  250. bSuccess = pAllocateAndInitializeSid( &NtAuthority,
  251. 2,
  252. SECURITY_BUILTIN_DOMAIN_RID,
  253. DOMAIN_ALIAS_RID_ADMINS,
  254. 0, 0, 0, 0, 0, 0,
  255. &pAdminSid);
  256. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  257. 1,
  258. SECURITY_LOCAL_SYSTEM_RID,
  259. 0, 0, 0, 0, 0, 0, 0,
  260. &pSystemSid);
  261. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  262. 2,
  263. SECURITY_BUILTIN_DOMAIN_RID,
  264. DOMAIN_ALIAS_RID_POWER_USERS,
  265. 0, 0, 0, 0, 0, 0,
  266. &pPowerUsersSid);
  267. bSuccess = bSuccess && pAllocateAndInitializeSid( &CreatorAuthority,
  268. 1,
  269. SECURITY_CREATOR_OWNER_RID,
  270. 0, 0, 0, 0, 0, 0, 0,
  271. &pCreatorSid);
  272. bSuccess = bSuccess && pAllocateAndInitializeSid(&NtAuthority,
  273. 2,
  274. SECURITY_BUILTIN_DOMAIN_RID,
  275. DOMAIN_ALIAS_RID_USERS,
  276. 0, 0, 0, 0, 0, 0,
  277. &pUsersSid);
  278. if (bSuccess) {
  279. //
  280. // Initialize Access structures describing the ACEs we want:
  281. // System Full Control
  282. // Admins Full Control
  283. //
  284. // We'll take advantage of the fact that the unlocked private keys is
  285. // the same as the device parameters key and they are a superset of the
  286. // locked private keys.
  287. //
  288. // When we create the DACL for the private key we'll specify a subset of
  289. // the ExplicitAccess array.
  290. //
  291. for (i = 0; i < 6; i++) {
  292. ExplicitAccess[i].grfAccessMode = SET_ACCESS;
  293. ExplicitAccess[i].Trustee.pMultipleTrustee = NULL;
  294. ExplicitAccess[i].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  295. ExplicitAccess[i].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  296. ExplicitAccess[i].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  297. }
  298. ExplicitAccess[0].grfAccessPermissions = KEY_ALL_ACCESS;
  299. ExplicitAccess[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  300. ExplicitAccess[0].Trustee.ptstrName = (LPTSTR)pAdminSid;
  301. ExplicitAccess[1].grfAccessPermissions = KEY_ALL_ACCESS;
  302. ExplicitAccess[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  303. ExplicitAccess[1].Trustee.ptstrName = (LPTSTR)pSystemSid;
  304. ExplicitAccess[2].grfAccessPermissions = KEY_ALL_ACCESS;
  305. ExplicitAccess[2].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  306. ExplicitAccess[2].Trustee.ptstrName = (LPTSTR)pCreatorSid;
  307. ExplicitAccess[3].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE;
  308. ExplicitAccess[3].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  309. ExplicitAccess[3].Trustee.ptstrName = (LPTSTR)pPowerUsersSid;
  310. ExplicitAccess[4].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE| KEY_CREATE_SUB_KEY |KEY_SET_VALUE;
  311. ExplicitAccess[4].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  312. ExplicitAccess[4].Trustee.ptstrName = (LPTSTR)pUsersSid;
  313. ExplicitAccess[5].grfAccessPermissions = DELETE;
  314. ExplicitAccess[5].grfInheritance = INHERIT_ONLY_ACE | SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  315. ExplicitAccess[5].Trustee.ptstrName = (LPTSTR)pUsersSid;
  316. dwError = (DWORD)pSetEntriesInAcl( 6,
  317. ExplicitAccess,
  318. NULL,
  319. &pNewDACL );
  320. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(pAdvApi32,SET_SECURITY_INFO);
  321. if (pSetSecurityInfo == NULL)
  322. {
  323. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  324. goto ErrorCleanup;
  325. }
  326. dwError = pSetSecurityInfo(
  327. hObject,
  328. ObjectType,
  329. DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
  330. NULL,
  331. NULL,
  332. pNewDACL,
  333. NULL
  334. );
  335. }
  336. ErrorCleanup:
  337. pFreeSid = (PFREESID_FN)
  338. GetProcAddress(pAdvApi32,
  339. FREE_SID);
  340. if(pFreeSid)
  341. {
  342. if(pAdminSid)
  343. pFreeSid(pAdminSid);
  344. if(pSystemSid)
  345. pFreeSid(pSystemSid);
  346. if(pPowerUsersSid)
  347. pFreeSid(pPowerUsersSid);
  348. if(pCreatorSid)
  349. pFreeSid(pCreatorSid);
  350. if(pUsersSid)
  351. pFreeSid(pUsersSid);
  352. }
  353. if(pNewDACL)
  354. LocalFree(pNewDACL);
  355. if(pAdvApi32)
  356. FreeLibrary( pAdvApi32 );
  357. return bSuccess;
  358. }
  359. BOOL
  360. RestoreACLOnKey(
  361. HANDLE hObject,
  362. SE_OBJECT_TYPE ObjectType
  363. )
  364. {
  365. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  366. SID_IDENTIFIER_AUTHORITY CreatorAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  367. PSID pAdminSid = NULL;
  368. PSID pSystemSid = NULL;
  369. PSID pPowerUsersSid = NULL;
  370. PSID pCreatorSid = NULL;
  371. PSID pUsersSid = NULL;
  372. PACL pNewDACL = NULL;
  373. DWORD dwError;
  374. BOOL bSuccess = TRUE;
  375. DWORD i;
  376. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid = NULL;
  377. PSETENTRIESINACL pSetEntriesInAcl = NULL;
  378. HMODULE pAdvApi32 = NULL;
  379. PFREESID_FN pFreeSid = NULL;
  380. PSETSECURITYINFO_FN pSetSecurityInfo;
  381. pAdvApi32 = LoadLibrary(ADVAPI_32_DLL);
  382. if (!pAdvApi32) {
  383. return(FALSE);
  384. }
  385. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  386. GetProcAddress(pAdvApi32,
  387. ALLOCATE_AND_INITITIALIZE_SID);
  388. if (pAllocateAndInitializeSid == NULL)
  389. {
  390. goto ErrorCleanup;
  391. }
  392. #ifdef UNICODE
  393. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclW" ));
  394. #else
  395. pSetEntriesInAcl = reinterpret_cast<PSETENTRIESINACL>(GetProcAddress( pAdvApi32, "SetEntriesInAclA" ));
  396. #endif
  397. if (!pSetEntriesInAcl) {
  398. FreeLibrary( pAdvApi32 );
  399. return(FALSE);
  400. }
  401. EXPLICIT_ACCESS ExplicitAccess[5];
  402. //
  403. // Create SIDs - Admins and System
  404. //
  405. bSuccess = pAllocateAndInitializeSid( &NtAuthority,
  406. 2,
  407. SECURITY_BUILTIN_DOMAIN_RID,
  408. DOMAIN_ALIAS_RID_ADMINS,
  409. 0, 0, 0, 0, 0, 0,
  410. &pAdminSid);
  411. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  412. 1,
  413. SECURITY_LOCAL_SYSTEM_RID,
  414. 0, 0, 0, 0, 0, 0, 0,
  415. &pSystemSid);
  416. bSuccess = bSuccess && pAllocateAndInitializeSid( &NtAuthority,
  417. 2,
  418. SECURITY_BUILTIN_DOMAIN_RID,
  419. DOMAIN_ALIAS_RID_POWER_USERS,
  420. 0, 0, 0, 0, 0, 0,
  421. &pPowerUsersSid);
  422. bSuccess = bSuccess && pAllocateAndInitializeSid( &CreatorAuthority,
  423. 1,
  424. SECURITY_CREATOR_OWNER_RID,
  425. 0, 0, 0, 0, 0, 0, 0,
  426. &pCreatorSid);
  427. bSuccess = bSuccess && pAllocateAndInitializeSid(&NtAuthority,
  428. 2,
  429. SECURITY_BUILTIN_DOMAIN_RID,
  430. DOMAIN_ALIAS_RID_USERS,
  431. 0, 0, 0, 0, 0, 0,
  432. &pUsersSid);
  433. if (bSuccess) {
  434. //
  435. // Initialize Access structures describing the ACEs we want:
  436. // System Full Control
  437. // Admins Full Control
  438. //
  439. // We'll take advantage of the fact that the unlocked private keys is
  440. // the same as the device parameters key and they are a superset of the
  441. // locked private keys.
  442. //
  443. // When we create the DACL for the private key we'll specify a subset of
  444. // the ExplicitAccess array.
  445. //
  446. for (i = 0; i < 5; i++) {
  447. ExplicitAccess[i].grfAccessMode = SET_ACCESS;
  448. ExplicitAccess[i].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  449. ExplicitAccess[i].Trustee.pMultipleTrustee = NULL;
  450. ExplicitAccess[i].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  451. ExplicitAccess[i].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  452. ExplicitAccess[i].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  453. }
  454. ExplicitAccess[0].grfAccessPermissions = KEY_ALL_ACCESS;
  455. ExplicitAccess[0].Trustee.ptstrName = (LPTSTR)pAdminSid;
  456. ExplicitAccess[1].grfAccessPermissions = KEY_ALL_ACCESS;
  457. ExplicitAccess[1].Trustee.ptstrName = (LPTSTR)pSystemSid;
  458. ExplicitAccess[2].grfAccessPermissions = KEY_ALL_ACCESS;
  459. ExplicitAccess[2].Trustee.ptstrName = (LPTSTR)pCreatorSid;
  460. ExplicitAccess[3].grfAccessPermissions = KEY_ALL_ACCESS;
  461. ExplicitAccess[3].Trustee.ptstrName = (LPTSTR)pPowerUsersSid;
  462. ExplicitAccess[4].grfAccessPermissions = KEY_ALL_ACCESS;
  463. ExplicitAccess[4].Trustee.ptstrName = (LPTSTR)pUsersSid;
  464. dwError = (DWORD)pSetEntriesInAcl( 5,
  465. ExplicitAccess,
  466. NULL,
  467. &pNewDACL );
  468. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(pAdvApi32,SET_SECURITY_INFO);
  469. if (pSetSecurityInfo == NULL)
  470. {
  471. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  472. goto ErrorCleanup;
  473. }
  474. dwError = pSetSecurityInfo(
  475. hObject,
  476. ObjectType,
  477. DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
  478. NULL,
  479. NULL,
  480. pNewDACL,
  481. NULL
  482. );
  483. }
  484. ErrorCleanup:
  485. pFreeSid = (PFREESID_FN)
  486. GetProcAddress(pAdvApi32,
  487. FREE_SID);
  488. if(pFreeSid)
  489. {
  490. if(pAdminSid)
  491. pFreeSid(pAdminSid);
  492. if(pSystemSid)
  493. pFreeSid(pSystemSid);
  494. if(pPowerUsersSid)
  495. pFreeSid(pPowerUsersSid);
  496. if(pCreatorSid)
  497. pFreeSid(pCreatorSid);
  498. if(pUsersSid)
  499. pFreeSid(pUsersSid);
  500. }
  501. if(pNewDACL)
  502. LocalFree(pNewDACL);
  503. if(pAdvApi32)
  504. FreeLibrary( pAdvApi32 );
  505. return bSuccess;
  506. }
  507. //
  508. // Create and ACL HKLM\Software\Microsoft\MSLicensing\Store
  509. //
  510. // 1) Set the ACL on MSLicensing, so that Users can only read (inherited).
  511. // 2) Create the Store subkey, and set an ACL so that Users can
  512. // read (inherited), write (inherited), create subkeys, enumerate subkeys,
  513. // and delete subkeys, but not do anything else.
  514. //
  515. BOOL CreateRegAddAcl(VOID)
  516. {
  517. BOOL fRet = FALSE;
  518. DWORD dwDisposition, dwError = NO_ERROR;
  519. HKEY hKey = NULL, hKeyStore = NULL;
  520. dwError = RegCreateKeyEx(
  521. HKEY_LOCAL_MACHINE,
  522. MSLICENSING_REG_KEY,
  523. 0,
  524. NULL,
  525. REG_OPTION_NON_VOLATILE,
  526. KEY_ALL_ACCESS,
  527. NULL,
  528. &hKey,
  529. &dwDisposition
  530. );
  531. if (dwError != ERROR_SUCCESS) {
  532. return FALSE;
  533. }
  534. fRet = AddACLToObjectSecurityDescriptor(
  535. hKey,
  536. SE_REGISTRY_KEY
  537. );
  538. if (!fRet) {
  539. goto cleanup;
  540. }
  541. dwError = RegCreateKeyEx(
  542. hKey,
  543. MSLICENSING_STORE_SUBKEY,
  544. 0,
  545. NULL,
  546. REG_OPTION_NON_VOLATILE,
  547. KEY_ALL_ACCESS,
  548. NULL,
  549. &hKeyStore,
  550. &dwDisposition
  551. );
  552. if (dwError != ERROR_SUCCESS) {
  553. fRet = FALSE;
  554. goto cleanup;
  555. }
  556. fRet = AddACLToStoreObjectSecurityDescriptor(
  557. hKeyStore,
  558. SE_REGISTRY_KEY
  559. );
  560. cleanup:
  561. if (NULL != hKey)
  562. {
  563. RegCloseKey( hKey );
  564. }
  565. if (NULL != hKeyStore)
  566. {
  567. RegCloseKey( hKeyStore );
  568. }
  569. return fRet;
  570. }
  571. //
  572. // Restore ACL on HKLM\Software\Microsoft\MSLicensing, HardwareID and Store
  573. // and subkeys if they exist on uninstall.
  574. // Grant Users Full control. This is to make it operational with downlevel clients.
  575. //
  576. void RestoreRegAcl(VOID)
  577. {
  578. DWORD dwDisposition, dwError = NO_ERROR;
  579. HKEY hKey = NULL;
  580. dwError = RegCreateKeyEx(
  581. HKEY_LOCAL_MACHINE,
  582. MSLICENSING_REG_KEY,
  583. 0,
  584. NULL,
  585. REG_OPTION_NON_VOLATILE,
  586. KEY_ALL_ACCESS,
  587. NULL,
  588. &hKey,
  589. &dwDisposition
  590. );
  591. if (dwError == ERROR_SUCCESS)
  592. {
  593. RestoreACLOnKey(
  594. hKey,
  595. SE_REGISTRY_KEY
  596. );
  597. if (NULL != hKey)
  598. {
  599. RegCloseKey( hKey );
  600. hKey = NULL;
  601. }
  602. }
  603. // Write HWID to registry
  604. dwError = RegOpenKeyEx(
  605. HKEY_LOCAL_MACHINE,
  606. MSLICENSING_HWID_KEY,
  607. 0,
  608. KEY_ALL_ACCESS,
  609. &hKey
  610. );
  611. if (dwError == ERROR_SUCCESS)
  612. {
  613. RestoreACLOnKey(
  614. hKey,
  615. SE_REGISTRY_KEY
  616. );
  617. if (NULL != hKey)
  618. {
  619. RegCloseKey( hKey );
  620. hKey = NULL;
  621. }
  622. }
  623. dwError = RegOpenKeyEx(
  624. HKEY_LOCAL_MACHINE,
  625. MSLICENSING_STORE_KEY,
  626. 0,
  627. KEY_ALL_ACCESS,
  628. &hKey
  629. );
  630. if (dwError == ERROR_SUCCESS)
  631. {
  632. RestoreACLOnKey(
  633. hKey,
  634. SE_REGISTRY_KEY
  635. );
  636. if (NULL != hKey)
  637. {
  638. RegCloseKey( hKey );
  639. hKey = NULL;
  640. }
  641. }
  642. return;
  643. }
  644. //
  645. // Note: this uses low-level ACL APIs because the higher level APIs
  646. // don't exist or have bugs on NT4
  647. //
  648. BOOL
  649. AddSidToObjectsSecurityDescriptor(
  650. HANDLE hObject,
  651. SE_OBJECT_TYPE ObjectType,
  652. PSID pSid,
  653. DWORD dwNewAccess,
  654. ACCESS_MODE AccessMode,
  655. DWORD dwInheritance,
  656. BOOL fKeepExistingAcl
  657. )
  658. {
  659. BOOL fReturn = FALSE;
  660. DWORD dwRet;
  661. PACL pOldDacl = NULL, pNewDacl = NULL;
  662. PSECURITY_DESCRIPTOR pSecDesc = NULL;
  663. HMODULE hModAdvapiDll = NULL;
  664. PGETSECURITYINFO_FN pGetSecurityInfo;
  665. PSETSECURITYINFO_FN pSetSecurityInfo;
  666. PADDACE_FN pAddAce;
  667. PGETLENGTHSID_FN pGetLengthSid;
  668. PCOPYSID_FN pCopySid;
  669. WORD cbAcl;
  670. PACCESS_ALLOWED_ACE pAceAllow = NULL;
  671. PACCESS_DENIED_ACE pAceDeny = NULL;
  672. PISVALIDSID_FN pIsValidSid;
  673. WORD cbAce;
  674. DWORD cbSid;
  675. PSID pSidLocation;
  676. //
  677. // pSid cannot be NULL.
  678. //
  679. if (pSid == NULL) {
  680. SetLastError(ERROR_INVALID_PARAMETER);
  681. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: NULL pSid"));
  682. return(FALSE);
  683. }
  684. if ((AccessMode != GRANT_ACCESS) && (AccessMode != DENY_ACCESS)){
  685. SetLastError(ERROR_INVALID_PARAMETER);
  686. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: only Grant Aces and Deny Access supported"));
  687. return(FALSE);
  688. }
  689. if (!fKeepExistingAcl)
  690. {
  691. SetLastError(ERROR_INVALID_PARAMETER);
  692. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: only fKeepExistingAcl supported"));
  693. return(FALSE);
  694. }
  695. hModAdvapiDll = LoadLibrary(ADVAPI_32_DLL);
  696. if (hModAdvapiDll == NULL) {
  697. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: can't load Advapi32.dll"));
  698. return(FALSE);
  699. }
  700. pIsValidSid = (PISVALIDSID_FN)GetProcAddress(hModAdvapiDll, IS_VALID_SID);
  701. if (pIsValidSid == NULL)
  702. {
  703. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc IsValidSid"));
  704. goto ErrorCleanup;
  705. }
  706. if (!pIsValidSid(pSid)) {
  707. SetLastError(ERROR_INVALID_PARAMETER);
  708. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: bad pSid"));
  709. goto ErrorCleanup;
  710. }
  711. if (fKeepExistingAcl) {
  712. //
  713. // Get the objects security descriptor and current DACL.
  714. //
  715. pGetSecurityInfo = (PGETSECURITYINFO_FN)GetProcAddress(hModAdvapiDll, GET_SECURITY_INFO);
  716. if (pGetSecurityInfo == NULL)
  717. {
  718. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc GetSecurityInfo"));
  719. goto ErrorCleanup;
  720. }
  721. dwRet = pGetSecurityInfo(
  722. hObject,
  723. ObjectType,
  724. DACL_SECURITY_INFORMATION,
  725. NULL,
  726. NULL,
  727. &pOldDacl,
  728. NULL,
  729. &pSecDesc
  730. );
  731. if (dwRet != ERROR_SUCCESS) {
  732. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: GetSecurityInfo failed"));
  733. goto ErrorCleanup;
  734. }
  735. }
  736. //
  737. // Merge the new ACE into the existing DACL.
  738. //
  739. //
  740. // Calculate size of new ACL, and create it
  741. //
  742. pGetLengthSid = (PGETLENGTHSID_FN)GetProcAddress(hModAdvapiDll,GET_LENGTH_SID);
  743. if (pGetLengthSid == NULL)
  744. {
  745. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc GetLengthSid"));
  746. goto ErrorCleanup;
  747. }
  748. cbSid = pGetLengthSid(pSid);
  749. cbAcl = (WORD) (pOldDacl->AclSize + cbSid - sizeof(DWORD));
  750. if (AccessMode == GRANT_ACCESS)
  751. {
  752. cbAcl += (WORD) sizeof(ACCESS_ALLOWED_ACE);
  753. }
  754. else
  755. {
  756. cbAcl += (WORD) sizeof(ACCESS_DENIED_ACE);
  757. }
  758. pNewDacl = (PACL) LocalAlloc(LPTR, cbAcl);
  759. if (NULL == pNewDacl)
  760. {
  761. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't allocate ACL"));
  762. goto ErrorCleanup;
  763. }
  764. CopyMemory(pNewDacl,pOldDacl,pOldDacl->AclSize);
  765. pNewDacl->AclSize = cbAcl;
  766. //
  767. // Create New Ace to be added
  768. //
  769. if (AccessMode == GRANT_ACCESS)
  770. {
  771. cbAce = (WORD) (sizeof(ACCESS_ALLOWED_ACE) + cbSid - sizeof(DWORD));
  772. pAceAllow = (PACCESS_ALLOWED_ACE) LocalAlloc(LPTR,cbAce);
  773. if (NULL == pAceAllow)
  774. {
  775. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't allocate Ace"));
  776. goto ErrorCleanup;
  777. }
  778. pAceAllow->Header.AceFlags = (BYTE) dwInheritance;
  779. pAceAllow->Header.AceSize = cbAce;
  780. pAceAllow->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
  781. pAceAllow->Mask = dwNewAccess;
  782. pSidLocation = (PSID)(&pAceAllow->SidStart);
  783. }
  784. else
  785. {
  786. cbAce = (WORD) (sizeof(ACCESS_DENIED_ACE) + cbSid - sizeof(DWORD));
  787. pAceDeny = (PACCESS_DENIED_ACE) LocalAlloc(LPTR,cbAce);
  788. if (NULL == pAceDeny)
  789. {
  790. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't allocate Ace"));
  791. goto ErrorCleanup;
  792. }
  793. pAceDeny->Header.AceFlags = (BYTE) dwInheritance;
  794. pAceDeny->Header.AceSize = cbAce;
  795. pAceDeny->Header.AceType = ACCESS_DENIED_ACE_TYPE;
  796. pAceDeny->Mask = dwNewAccess;
  797. pSidLocation = (PSID)(&pAceDeny->SidStart);
  798. }
  799. pCopySid = (PCOPYSID_FN)GetProcAddress(hModAdvapiDll,COPY_SID);
  800. if (pCopySid == NULL)
  801. {
  802. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc CopySid"));
  803. goto ErrorCleanup;
  804. }
  805. if (!pCopySid(cbSid, pSidLocation, pSid))
  806. {
  807. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: CopySid failed"));
  808. goto ErrorCleanup;
  809. }
  810. pAddAce = (PADDACE_FN)GetProcAddress(hModAdvapiDll,ADD_ACE);
  811. if (pAddAce == NULL)
  812. {
  813. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc AddAce"));
  814. goto ErrorCleanup;
  815. }
  816. if (!pAddAce(pNewDacl,
  817. ACL_REVISION,
  818. (AccessMode == GRANT_ACCESS) ? MAXDWORD : 0,
  819. ((AccessMode == GRANT_ACCESS) ? (PVOID)pAceAllow : (PVOID)pAceDeny),
  820. cbAce))
  821. {
  822. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: AddAce failed"));
  823. goto ErrorCleanup;
  824. }
  825. //
  826. // Set the new security for the object.
  827. //
  828. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(hModAdvapiDll,SET_SECURITY_INFO);
  829. if (pSetSecurityInfo == NULL)
  830. {
  831. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  832. goto ErrorCleanup;
  833. }
  834. dwRet = pSetSecurityInfo(
  835. hObject,
  836. ObjectType,
  837. DACL_SECURITY_INFORMATION,
  838. NULL,
  839. NULL,
  840. pNewDacl,
  841. NULL
  842. );
  843. if (dwRet != ERROR_SUCCESS) {
  844. OutputDebugString(_T("AddSidToObjectsSecurityDescriptor: SetSecurityInfo failed"));
  845. goto ErrorCleanup;
  846. }
  847. fReturn = TRUE;
  848. ErrorCleanup:
  849. if (hModAdvapiDll != NULL) {
  850. FreeLibrary(hModAdvapiDll);
  851. }
  852. if (pNewDacl != NULL) {
  853. LocalFree(pNewDacl);
  854. }
  855. if (pSecDesc != NULL) {
  856. LocalFree(pSecDesc);
  857. }
  858. if (pAceAllow != NULL) {
  859. LocalFree(pAceAllow);
  860. }
  861. if (pAceDeny != NULL) {
  862. LocalFree(pAceDeny);
  863. }
  864. return(fReturn);
  865. }
  866. BOOL
  867. DeleteAceFromObjectsSecurityDescriptor(
  868. HANDLE hObject,
  869. SE_OBJECT_TYPE ObjectType,
  870. PSID pSid,
  871. DWORD dwAccess,
  872. ACCESS_MODE AccessMode,
  873. DWORD dwInheritance
  874. )
  875. {
  876. BOOL fReturn = FALSE;
  877. DWORD dwRet;
  878. PACL pDacl = NULL;
  879. PSECURITY_DESCRIPTOR pSecDesc = NULL;
  880. HMODULE hModAdvapiDll = NULL;
  881. PGETSECURITYINFO_FN pGetSecurityInfo;
  882. PSETSECURITYINFO_FN pSetSecurityInfo;
  883. PGETACLINFORMATION_FN pGetAclInformation;
  884. PDELETEACE_FN pDeleteAce;
  885. PGETACE_FN pGetAce;
  886. PACCESS_ALLOWED_ACE pAce = NULL;
  887. PISVALIDSID_FN pIsValidSid;
  888. PEQUALSID_FN pEqualSid;
  889. DWORD cAce;
  890. ACL_SIZE_INFORMATION aclSizeInfo;
  891. PACE_HEADER aceHeader;
  892. PACCESS_ALLOWED_ACE aceAllowed;
  893. //
  894. // pSid cannot be NULL.
  895. //
  896. if (pSid == NULL) {
  897. SetLastError(ERROR_INVALID_PARAMETER);
  898. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: NULL pSid"));
  899. return(FALSE);
  900. }
  901. //
  902. // We only support GRANT_ACCESS Aces
  903. if (AccessMode != GRANT_ACCESS) {
  904. SetLastError(ERROR_INVALID_PARAMETER);
  905. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: only Grant Aces allowed"));
  906. return(FALSE);
  907. }
  908. //
  909. // Load all the functions we'll need
  910. //
  911. hModAdvapiDll = LoadLibrary(ADVAPI_32_DLL);
  912. if (hModAdvapiDll == NULL) {
  913. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: can't load Advapi32.dll"));
  914. return(FALSE);
  915. }
  916. pIsValidSid = (PISVALIDSID_FN)GetProcAddress(hModAdvapiDll, IS_VALID_SID);
  917. if (pIsValidSid == NULL)
  918. {
  919. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: Can't get proc IsValidSid"));
  920. goto ErrorCleanup;
  921. }
  922. pEqualSid = (PEQUALSID_FN)GetProcAddress(hModAdvapiDll, EQUAL_SID);
  923. if (pEqualSid == NULL)
  924. {
  925. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: Can't get proc EqualSid"));
  926. goto ErrorCleanup;
  927. }
  928. pDeleteAce = (PDELETEACE_FN)GetProcAddress(hModAdvapiDll,DELETE_ACE);
  929. if (pDeleteAce == NULL)
  930. {
  931. OutputDebugString(_T("DeleteSidToObjectsSecurityDescriptor: Can't get proc DeleteAce"));
  932. goto ErrorCleanup;
  933. }
  934. pGetAce = (PGETACE_FN)GetProcAddress(hModAdvapiDll,GET_ACE);
  935. if (pGetAce == NULL)
  936. {
  937. OutputDebugString(_T("DeleteSidToObjectsSecurityDescriptor: Can't get proc GetAce"));
  938. goto ErrorCleanup;
  939. }
  940. pGetSecurityInfo = (PGETSECURITYINFO_FN)GetProcAddress(hModAdvapiDll, GET_SECURITY_INFO);
  941. if (pGetSecurityInfo == NULL)
  942. {
  943. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: Can't get proc GetSecurityInfo"));
  944. goto ErrorCleanup;
  945. }
  946. pGetAclInformation = (PGETACLINFORMATION_FN)GetProcAddress(hModAdvapiDll, GET_ACL_INFORMATION);
  947. if (pGetAclInformation == NULL)
  948. {
  949. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: Can't get proc GetAclInformation"));
  950. goto ErrorCleanup;
  951. }
  952. pSetSecurityInfo = (PSETSECURITYINFO_FN)GetProcAddress(hModAdvapiDll,SET_SECURITY_INFO);
  953. if (pSetSecurityInfo == NULL)
  954. {
  955. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: Can't get proc SetSecurityInfo"));
  956. goto ErrorCleanup;
  957. }
  958. //
  959. // Make sure Sid passed in is valid
  960. //
  961. if (!pIsValidSid(pSid)) {
  962. SetLastError(ERROR_INVALID_PARAMETER);
  963. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: bad pSid"));
  964. goto ErrorCleanup;
  965. }
  966. //
  967. // Get the object's security descriptor and current DACL.
  968. //
  969. dwRet = pGetSecurityInfo(
  970. hObject,
  971. ObjectType,
  972. DACL_SECURITY_INFORMATION,
  973. NULL,
  974. NULL,
  975. &pDacl,
  976. NULL,
  977. &pSecDesc
  978. );
  979. if (dwRet != ERROR_SUCCESS) {
  980. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: GetSecurityInfo failed"));
  981. goto ErrorCleanup;
  982. }
  983. //
  984. // Find the ACE to be deleted
  985. //
  986. if (!pGetAclInformation(pDacl, (LPVOID) &aclSizeInfo, (DWORD) sizeof(ACL_SIZE_INFORMATION), AclSizeInformation))
  987. {
  988. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: GetAclInformation failed"));
  989. goto ErrorCleanup;
  990. }
  991. for (cAce = 0; cAce < aclSizeInfo.AceCount; cAce++)
  992. {
  993. if (!pGetAce(pDacl, cAce, (LPVOID *) &aceHeader))
  994. {
  995. continue;
  996. }
  997. if (aceHeader->AceType != ACCESS_ALLOWED_ACE_TYPE)
  998. {
  999. continue;
  1000. }
  1001. aceAllowed = (PACCESS_ALLOWED_ACE)aceHeader;
  1002. if ((aceAllowed->Header.AceFlags & INHERITED_ACE) != (dwInheritance & INHERITED_ACE))
  1003. {
  1004. continue;
  1005. }
  1006. if ((aceAllowed->Header.AceFlags & CONTAINER_INHERIT_ACE) != (dwInheritance & CONTAINER_INHERIT_ACE))
  1007. {
  1008. continue;
  1009. }
  1010. if ((aceAllowed->Header.AceFlags & OBJECT_INHERIT_ACE) != (dwInheritance & OBJECT_INHERIT_ACE))
  1011. {
  1012. continue;
  1013. }
  1014. if (aceAllowed->Mask != dwAccess)
  1015. {
  1016. continue;
  1017. }
  1018. if (0 == pEqualSid((PSID)&(aceAllowed->SidStart), pSid))
  1019. {
  1020. continue;
  1021. }
  1022. break;
  1023. }
  1024. if (cAce != aclSizeInfo.AceCount)
  1025. {
  1026. //
  1027. // found one
  1028. //
  1029. if (!pDeleteAce(pDacl,cAce))
  1030. {
  1031. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: DeleteAce failed"));
  1032. goto ErrorCleanup;
  1033. }
  1034. //
  1035. // Set the new security for the object.
  1036. //
  1037. dwRet = pSetSecurityInfo(
  1038. hObject,
  1039. ObjectType,
  1040. DACL_SECURITY_INFORMATION,
  1041. NULL,
  1042. NULL,
  1043. pDacl,
  1044. NULL
  1045. );
  1046. if (dwRet != ERROR_SUCCESS) {
  1047. OutputDebugString(_T("DeleteAceFromObjectsSecurityDescriptor: SetSecurityInfo failed"));
  1048. goto ErrorCleanup;
  1049. }
  1050. }
  1051. fReturn = TRUE;
  1052. ErrorCleanup:
  1053. if (hModAdvapiDll != NULL) {
  1054. FreeLibrary(hModAdvapiDll);
  1055. }
  1056. if (pSecDesc != NULL) {
  1057. LocalFree(pSecDesc);
  1058. }
  1059. return(fReturn);
  1060. }
  1061. BOOL
  1062. AddUsersGroupToObjectsSecurityDescriptor(
  1063. HANDLE hObject,
  1064. SE_OBJECT_TYPE ObjectType,
  1065. ACCESS_MASK AccessMask,
  1066. ACCESS_MODE AccessMode,
  1067. DWORD Inheritance,
  1068. BOOL fKeepExistingAcl
  1069. )
  1070. {
  1071. BOOL fReturn = FALSE;
  1072. DWORD dwRet = ERROR_SUCCESS;
  1073. PSID pSid = NULL;
  1074. SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  1075. HMODULE hModAdvapiDll = NULL;
  1076. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid;
  1077. PFREESID_FN pFreeSid;
  1078. //
  1079. // Create the SID
  1080. //
  1081. hModAdvapiDll = LoadLibrary(ADVAPI_32_DLL);
  1082. if (hModAdvapiDll == NULL) {
  1083. return(FALSE);
  1084. }
  1085. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  1086. GetProcAddress(hModAdvapiDll,
  1087. ALLOCATE_AND_INITITIALIZE_SID);
  1088. if (pAllocateAndInitializeSid == NULL)
  1089. {
  1090. goto ErrorCleanup;
  1091. }
  1092. if (!pAllocateAndInitializeSid(
  1093. &SepNtAuthority,
  1094. 2,
  1095. SECURITY_BUILTIN_DOMAIN_RID,
  1096. DOMAIN_ALIAS_RID_USERS,
  1097. 0, 0, 0, 0, 0, 0,
  1098. &pSid
  1099. )) {
  1100. dwRet = GetLastError();
  1101. OutputDebugString(_T("Failed to AllocateAndInitializeSid()"));
  1102. goto ErrorCleanup;
  1103. }
  1104. if (!AddSidToObjectsSecurityDescriptor(
  1105. hObject,
  1106. ObjectType,
  1107. pSid,
  1108. AccessMask,
  1109. AccessMode,
  1110. Inheritance,
  1111. fKeepExistingAcl
  1112. )) {
  1113. dwRet = GetLastError();
  1114. OutputDebugString(_T("Failed to AddSidToObjectsSecurityDescriptor()"));
  1115. goto ErrorCleanup;
  1116. }
  1117. fReturn = TRUE;
  1118. ErrorCleanup:
  1119. if ((hModAdvapiDll != NULL) && (pSid != NULL)) {
  1120. pFreeSid = (PFREESID_FN)
  1121. GetProcAddress(hModAdvapiDll,
  1122. FREE_SID);
  1123. if (pFreeSid != NULL) {
  1124. pFreeSid(pSid);
  1125. }
  1126. }
  1127. if (hModAdvapiDll != NULL) {
  1128. FreeLibrary(hModAdvapiDll);
  1129. }
  1130. SetLastError(dwRet);
  1131. return(fReturn);
  1132. }
  1133. BOOL
  1134. AddTSUsersGroupToObjectsSecurityDescriptor(
  1135. HANDLE hObject,
  1136. SE_OBJECT_TYPE ObjectType,
  1137. ACCESS_MASK AccessMask,
  1138. ACCESS_MODE AccessMode,
  1139. DWORD Inheritance,
  1140. BOOL fKeepExistingAcl
  1141. )
  1142. {
  1143. BOOL fReturn = FALSE;
  1144. DWORD dwRet = ERROR_SUCCESS;
  1145. PSID pSid = NULL;
  1146. SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  1147. HMODULE hModAdvapiDll = NULL;
  1148. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid;
  1149. PFREESID_FN pFreeSid;
  1150. //
  1151. // Create the SID
  1152. //
  1153. hModAdvapiDll = LoadLibrary(ADVAPI_32_DLL);
  1154. if (hModAdvapiDll == NULL) {
  1155. return(FALSE);
  1156. }
  1157. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  1158. GetProcAddress(hModAdvapiDll,
  1159. ALLOCATE_AND_INITITIALIZE_SID);
  1160. if (pAllocateAndInitializeSid == NULL)
  1161. {
  1162. goto ErrorCleanup;
  1163. }
  1164. if (!pAllocateAndInitializeSid(
  1165. &SepNtAuthority,
  1166. 1,
  1167. SECURITY_TERMINAL_SERVER_RID,
  1168. 0, 0, 0, 0, 0, 0, 0,
  1169. &pSid
  1170. )) {
  1171. dwRet = GetLastError();
  1172. OutputDebugString(_T("Failed to AllocateAndInitializeSid()"));
  1173. goto ErrorCleanup;
  1174. }
  1175. if (!AddSidToObjectsSecurityDescriptor(
  1176. hObject,
  1177. ObjectType,
  1178. pSid,
  1179. AccessMask,
  1180. AccessMode,
  1181. Inheritance,
  1182. fKeepExistingAcl
  1183. )) {
  1184. dwRet = GetLastError();
  1185. OutputDebugString(_T("Failed to AddSidToObjectsSecurityDescriptor()"));
  1186. goto ErrorCleanup;
  1187. }
  1188. fReturn = TRUE;
  1189. ErrorCleanup:
  1190. if ((hModAdvapiDll != NULL) && (pSid != NULL)) {
  1191. pFreeSid = (PFREESID_FN)
  1192. GetProcAddress(hModAdvapiDll,
  1193. FREE_SID);
  1194. if (pFreeSid != NULL) {
  1195. pFreeSid(pSid);
  1196. }
  1197. }
  1198. if (hModAdvapiDll != NULL) {
  1199. FreeLibrary(hModAdvapiDll);
  1200. }
  1201. SetLastError(dwRet);
  1202. return(fReturn);
  1203. }
  1204. BOOL
  1205. DeleteUsersGroupAceFromObjectsSecurityDescriptor(
  1206. HANDLE hObject,
  1207. SE_OBJECT_TYPE ObjectType,
  1208. ACCESS_MASK AccessMask,
  1209. ACCESS_MODE AccessMode,
  1210. DWORD Inheritance
  1211. )
  1212. {
  1213. BOOL fReturn = FALSE;
  1214. DWORD dwRet = ERROR_SUCCESS;
  1215. PSID pSid = NULL;
  1216. SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  1217. HMODULE hModAdvapiDll = NULL;
  1218. PALLOCATEANDINITIALIZESID_FN pAllocateAndInitializeSid;
  1219. PFREESID_FN pFreeSid;
  1220. //
  1221. // Create the SID
  1222. //
  1223. hModAdvapiDll = LoadLibrary(ADVAPI_32_DLL);
  1224. if (hModAdvapiDll == NULL) {
  1225. return(FALSE);
  1226. }
  1227. pAllocateAndInitializeSid = (PALLOCATEANDINITIALIZESID_FN)
  1228. GetProcAddress(hModAdvapiDll,
  1229. ALLOCATE_AND_INITITIALIZE_SID);
  1230. if (pAllocateAndInitializeSid == NULL)
  1231. {
  1232. goto ErrorCleanup;
  1233. }
  1234. if (!pAllocateAndInitializeSid(
  1235. &SepNtAuthority,
  1236. 2,
  1237. SECURITY_BUILTIN_DOMAIN_RID,
  1238. DOMAIN_ALIAS_RID_USERS,
  1239. 0, 0, 0, 0, 0, 0,
  1240. &pSid
  1241. )) {
  1242. dwRet = GetLastError();
  1243. OutputDebugString(_T("Failed to AllocateAndInitializeSid()"));
  1244. goto ErrorCleanup;
  1245. }
  1246. if (!DeleteAceFromObjectsSecurityDescriptor(
  1247. hObject,
  1248. ObjectType,
  1249. pSid,
  1250. AccessMask,
  1251. AccessMode,
  1252. Inheritance
  1253. )) {
  1254. dwRet = GetLastError();
  1255. OutputDebugString(_T("Failed to DeleteAceFromObjectsSecurityDescriptor()"));
  1256. goto ErrorCleanup;
  1257. }
  1258. fReturn = TRUE;
  1259. ErrorCleanup:
  1260. if ((hModAdvapiDll != NULL) && (pSid != NULL)) {
  1261. pFreeSid = (PFREESID_FN)
  1262. GetProcAddress(hModAdvapiDll,
  1263. FREE_SID);
  1264. if (pFreeSid != NULL) {
  1265. pFreeSid(pSid);
  1266. }
  1267. }
  1268. if (hModAdvapiDll != NULL) {
  1269. FreeLibrary(hModAdvapiDll);
  1270. }
  1271. SetLastError(dwRet);
  1272. return(fReturn);
  1273. }
  1274. BOOL
  1275. CreateAndWriteHWID(VOID)
  1276. {
  1277. BOOL fRet = FALSE;
  1278. DWORD dwDisposition, dwError = NO_ERROR;
  1279. HKEY hKey = NULL;
  1280. HWID hwid;
  1281. // Write HWID to registry
  1282. dwError = RegCreateKeyEx(
  1283. HKEY_LOCAL_MACHINE,
  1284. MSLICENSING_HWID_KEY,
  1285. 0,
  1286. NULL,
  1287. REG_OPTION_NON_VOLATILE,
  1288. KEY_ALL_ACCESS,
  1289. NULL,
  1290. &hKey,
  1291. &dwDisposition
  1292. );
  1293. if (dwError != ERROR_SUCCESS) {
  1294. goto cleanup;
  1295. }
  1296. if (dwDisposition == REG_CREATED_NEW_KEY)
  1297. {
  1298. // generate HWID
  1299. if (LICENSE_STATUS_OK == GenerateClientHWID(&hwid))
  1300. {
  1301. dwError = RegSetValueEx(hKey,
  1302. MSLICENSING_HWID_VALUE,
  1303. 0,
  1304. REG_BINARY,
  1305. (LPBYTE)&hwid,
  1306. sizeof(HWID));
  1307. if (dwError != ERROR_SUCCESS) {
  1308. goto cleanup;
  1309. }
  1310. }
  1311. fRet = TRUE;
  1312. }
  1313. else
  1314. {
  1315. fRet = TRUE;
  1316. }
  1317. cleanup:
  1318. if (NULL != hKey)
  1319. {
  1320. RegCloseKey( hKey );
  1321. }
  1322. return fRet;
  1323. }
  1324. #endif //OS_WINCE