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.

967 lines
22 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1997.
  5. //
  6. // File: var2sec.cxx
  7. //
  8. // Contents:
  9. //
  10. // Functions:
  11. //
  12. // History: 25-Apr-97 KrishnaG Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "iis.hxx"
  16. #pragma hdrstop
  17. HRESULT
  18. ConvertSecurityDescriptorToSecDes(
  19. IADsSecurityDescriptor FAR * pSecDes,
  20. PSECURITY_DESCRIPTOR * ppSecurityDescriptor,
  21. PDWORD pdwSDLength
  22. )
  23. {
  24. HRESULT hr = S_OK;
  25. SECURITY_DESCRIPTOR AbsoluteSD;
  26. PSECURITY_DESCRIPTOR pRelative = NULL;
  27. BOOL Defaulted = FALSE;
  28. BOOL DaclPresent = FALSE;
  29. BOOL SaclPresent = FALSE;
  30. BOOL fDaclDefaulted = FALSE;
  31. BOOL fSaclDefaulted = FALSE;
  32. BOOL fOwnerDefaulted = FALSE;
  33. BOOL fGroupDefaulted = FALSE;
  34. PSID pOwnerSid = NULL;
  35. PSID pGroupSid = NULL;
  36. PACL pDacl = NULL;
  37. PACL pSacl = NULL;
  38. DWORD dwSDLength = 0;
  39. DWORD dwRet = 0;
  40. BOOL dwStatus = 0;
  41. LONG lControlFlags = 0;
  42. SECURITY_DESCRIPTOR_CONTROL dwControlFlags = 0;
  43. SECURITY_DESCRIPTOR_CONTROL dwControlMask =
  44. SE_DACL_AUTO_INHERIT_REQ | SE_DACL_AUTO_INHERITED |
  45. SE_DACL_PROTECTED | SE_SACL_AUTO_INHERIT_REQ |
  46. SE_SACL_AUTO_INHERITED | SE_SACL_PROTECTED;
  47. //
  48. // Initialize *pSizeSD = 0;
  49. //
  50. dwRet = InitializeSecurityDescriptor (
  51. &AbsoluteSD,
  52. SECURITY_DESCRIPTOR_REVISION1
  53. );
  54. if (!dwRet) {
  55. hr = E_FAIL;
  56. BAIL_ON_FAILURE(hr);
  57. }
  58. hr = pSecDes->get_Control(&lControlFlags);
  59. BAIL_ON_FAILURE(hr);
  60. dwControlFlags = dwControlMask & (SECURITY_DESCRIPTOR_CONTROL)lControlFlags;
  61. dwStatus = SetSecurityDescriptorControl(
  62. &AbsoluteSD,
  63. dwControlMask,
  64. dwControlFlags
  65. );
  66. if (!dwStatus) {
  67. hr = HRESULT_FROM_WIN32(GetLastError());
  68. BAIL_ON_FAILURE(hr);
  69. }
  70. hr = GetOwnerSecurityIdentifier(
  71. pSecDes,
  72. &pOwnerSid,
  73. &fOwnerDefaulted
  74. );
  75. BAIL_ON_FAILURE(hr);
  76. dwStatus = SetSecurityDescriptorOwner(
  77. &AbsoluteSD,
  78. pOwnerSid,
  79. fOwnerDefaulted
  80. );
  81. if (!dwStatus) {
  82. hr = HRESULT_FROM_WIN32(GetLastError());
  83. BAIL_ON_FAILURE(hr);
  84. }
  85. hr = GetGroupSecurityIdentifier(
  86. pSecDes,
  87. &pGroupSid,
  88. &fGroupDefaulted
  89. );
  90. BAIL_ON_FAILURE(hr);
  91. dwStatus = SetSecurityDescriptorGroup(
  92. &AbsoluteSD,
  93. pGroupSid,
  94. fGroupDefaulted
  95. );
  96. if (!dwStatus) {
  97. hr = HRESULT_FROM_WIN32(GetLastError());
  98. BAIL_ON_FAILURE(hr);
  99. }
  100. hr = GetDacl(
  101. pSecDes,
  102. &pDacl,
  103. &fDaclDefaulted
  104. );
  105. BAIL_ON_FAILURE(hr);
  106. if (pDacl || fDaclDefaulted) {
  107. DaclPresent = TRUE;
  108. }
  109. dwStatus = SetSecurityDescriptorDacl(
  110. &AbsoluteSD,
  111. DaclPresent,
  112. pDacl,
  113. fDaclDefaulted
  114. );
  115. if (!dwStatus) {
  116. hr = HRESULT_FROM_WIN32(GetLastError());
  117. BAIL_ON_FAILURE(hr);
  118. }
  119. hr = GetSacl(
  120. pSecDes,
  121. &pSacl,
  122. &fSaclDefaulted
  123. );
  124. BAIL_ON_FAILURE(hr);
  125. if (pSacl || fSaclDefaulted) {
  126. SaclPresent = TRUE;
  127. }
  128. dwStatus = SetSecurityDescriptorSacl(
  129. &AbsoluteSD,
  130. SaclPresent,
  131. pSacl,
  132. fSaclDefaulted
  133. );
  134. if (!dwStatus) {
  135. hr = HRESULT_FROM_WIN32(GetLastError());
  136. BAIL_ON_FAILURE(hr);
  137. }
  138. dwSDLength = GetSecurityDescriptorLength(
  139. &AbsoluteSD
  140. );
  141. pRelative = AllocADsMem(dwSDLength);
  142. if (!pRelative) {
  143. hr = E_OUTOFMEMORY;
  144. BAIL_ON_FAILURE(hr);
  145. }
  146. if (!MakeSelfRelativeSD (&AbsoluteSD, pRelative, &dwSDLength)) {
  147. FreeADsMem(pRelative);
  148. hr = HRESULT_FROM_WIN32(GetLastError());
  149. BAIL_ON_FAILURE(hr);
  150. }
  151. *ppSecurityDescriptor = pRelative;
  152. *pdwSDLength = dwSDLength;
  153. cleanup:
  154. if (pDacl) {
  155. FreeADsMem(pDacl);
  156. }
  157. if (pSacl) {
  158. FreeADsMem(pSacl);
  159. }
  160. if (pOwnerSid) {
  161. FreeADsMem(pOwnerSid);
  162. }
  163. if (pGroupSid) {
  164. FreeADsMem(pGroupSid);
  165. }
  166. RRETURN(hr);
  167. error:
  168. if (pRelative) {
  169. FreeADsMem(pRelative);
  170. }
  171. *ppSecurityDescriptor = NULL;
  172. *pdwSDLength = 0;
  173. goto cleanup;
  174. }
  175. HRESULT
  176. GetOwnerSecurityIdentifier(
  177. IADsSecurityDescriptor FAR * pSecDes,
  178. PSID * ppSid,
  179. PBOOL pfOwnerDefaulted
  180. )
  181. {
  182. BSTR bstrOwner = NULL;
  183. DWORD dwSidSize = 0;
  184. HRESULT hr = S_OK;
  185. VARIANT_BOOL varBool = VARIANT_FALSE;
  186. hr = pSecDes->get_Owner(
  187. &bstrOwner
  188. );
  189. BAIL_ON_FAILURE(hr);
  190. hr = pSecDes->get_OwnerDefaulted(
  191. &varBool
  192. );
  193. BAIL_ON_FAILURE(hr);
  194. if (varBool == VARIANT_FALSE) {
  195. if (bstrOwner && *bstrOwner) {
  196. hr = ConvertTrusteeToSid(
  197. bstrOwner,
  198. ppSid,
  199. &dwSidSize
  200. );
  201. BAIL_ON_FAILURE(hr);
  202. *pfOwnerDefaulted = FALSE;
  203. }else {
  204. *ppSid = NULL;
  205. *pfOwnerDefaulted = FALSE;
  206. }
  207. }else {
  208. *ppSid = NULL;
  209. dwSidSize = 0;
  210. *pfOwnerDefaulted = TRUE;
  211. }
  212. error:
  213. if (bstrOwner) {
  214. ADsFreeString(bstrOwner);
  215. }
  216. RRETURN(hr);
  217. }
  218. HRESULT
  219. GetGroupSecurityIdentifier(
  220. IADsSecurityDescriptor FAR * pSecDes,
  221. PSID * ppSid,
  222. PBOOL pfGroupDefaulted
  223. )
  224. {
  225. BSTR bstrGroup = NULL;
  226. DWORD dwSidSize = 0;
  227. HRESULT hr = S_OK;
  228. VARIANT_BOOL varBool = VARIANT_FALSE;
  229. hr = pSecDes->get_Group(
  230. &bstrGroup
  231. );
  232. BAIL_ON_FAILURE(hr);
  233. hr = pSecDes->get_GroupDefaulted(
  234. &varBool
  235. );
  236. BAIL_ON_FAILURE(hr);
  237. if (varBool == VARIANT_FALSE) {
  238. if (bstrGroup && *bstrGroup) {
  239. hr = ConvertTrusteeToSid(
  240. bstrGroup,
  241. ppSid,
  242. &dwSidSize
  243. );
  244. BAIL_ON_FAILURE(hr);
  245. *pfGroupDefaulted = FALSE;
  246. }else {
  247. *ppSid = NULL;
  248. *pfGroupDefaulted = FALSE;
  249. }
  250. }else {
  251. *ppSid = NULL;
  252. dwSidSize = 0;
  253. *pfGroupDefaulted = TRUE;
  254. }
  255. error:
  256. if (bstrGroup) {
  257. ADsFreeString(bstrGroup);
  258. }
  259. RRETURN(hr);
  260. }
  261. HRESULT
  262. GetDacl(
  263. IADsSecurityDescriptor FAR * pSecDes,
  264. PACL * ppDacl,
  265. PBOOL pfDaclDefaulted
  266. )
  267. {
  268. IADsAccessControlList FAR * pDiscAcl = NULL;
  269. IDispatch FAR * pDispatch = NULL;
  270. HRESULT hr = S_OK;
  271. VARIANT_BOOL varBool = VARIANT_FALSE;
  272. hr = pSecDes->get_DaclDefaulted(
  273. &varBool
  274. );
  275. BAIL_ON_FAILURE(hr);
  276. if (varBool == VARIANT_FALSE) {
  277. *pfDaclDefaulted = FALSE;
  278. }else {
  279. *pfDaclDefaulted = TRUE;
  280. }
  281. hr = pSecDes->get_DiscretionaryAcl(
  282. &pDispatch
  283. );
  284. BAIL_ON_FAILURE(hr);
  285. if (!pDispatch) {
  286. *ppDacl = NULL;
  287. goto error;
  288. }
  289. hr = pDispatch->QueryInterface(
  290. IID_IADsAccessControlList,
  291. (void **)&pDiscAcl
  292. );
  293. BAIL_ON_FAILURE(hr);
  294. hr = ConvertAccessControlListToAcl(
  295. pDiscAcl,
  296. ppDacl
  297. );
  298. BAIL_ON_FAILURE(hr);
  299. error:
  300. if (pDispatch) {
  301. pDispatch->Release();
  302. }
  303. if (pDiscAcl) {
  304. pDiscAcl->Release();
  305. }
  306. RRETURN(hr);
  307. }
  308. HRESULT
  309. GetSacl(
  310. IADsSecurityDescriptor FAR * pSecDes,
  311. PACL * ppSacl,
  312. PBOOL pfSaclDefaulted
  313. )
  314. {
  315. IADsAccessControlList FAR * pSystemAcl = NULL;
  316. IDispatch FAR * pDispatch = NULL;
  317. HRESULT hr = S_OK;
  318. VARIANT_BOOL varBool = VARIANT_FALSE;
  319. hr = pSecDes->get_SaclDefaulted(
  320. &varBool
  321. );
  322. BAIL_ON_FAILURE(hr);
  323. if (varBool == VARIANT_FALSE) {
  324. *pfSaclDefaulted = FALSE;
  325. }else {
  326. *pfSaclDefaulted = TRUE;
  327. }
  328. hr = pSecDes->get_SystemAcl(
  329. &pDispatch
  330. );
  331. BAIL_ON_FAILURE(hr);
  332. if (!pDispatch) {
  333. *ppSacl = NULL;
  334. goto error;
  335. }
  336. hr = pDispatch->QueryInterface(
  337. IID_IADsAccessControlList,
  338. (void **)&pSystemAcl
  339. );
  340. BAIL_ON_FAILURE(hr);
  341. hr = ConvertAccessControlListToAcl(
  342. pSystemAcl,
  343. ppSacl
  344. );
  345. BAIL_ON_FAILURE(hr);
  346. error:
  347. if (pDispatch) {
  348. pDispatch->Release();
  349. }
  350. if (pSystemAcl) {
  351. pSystemAcl->Release();
  352. }
  353. RRETURN(hr);
  354. }
  355. HRESULT
  356. ConvertAccessControlListToAcl(
  357. IADsAccessControlList FAR * pAccessList,
  358. PACL * ppAcl
  359. )
  360. {
  361. IUnknown * pUnknown = NULL;
  362. IEnumVARIANT * pEnumerator = NULL;
  363. HRESULT hr = S_OK;
  364. DWORD i = 0;
  365. DWORD cReturned = 0;
  366. VARIANT varAce;
  367. DWORD dwAceCount = 0;
  368. IADsAccessControlEntry FAR * pAccessControlEntry = NULL;
  369. LPBYTE pTempAce = NULL;
  370. DWORD dwCount = 0;
  371. PACL pAcl = NULL;
  372. DWORD dwAclSize = 0;
  373. PACE_HEADER * ppAceHdr = NULL;
  374. DWORD dwRet = 0;
  375. BOOL bRet = 0;
  376. DWORD dwAclRevision = 0;
  377. hr = pAccessList->get_AceCount((long *)&dwAceCount);
  378. BAIL_ON_FAILURE(hr);
  379. hr = pAccessList->get__NewEnum(
  380. &pUnknown
  381. );
  382. BAIL_ON_FAILURE(hr);
  383. hr = pUnknown->QueryInterface(
  384. IID_IEnumVARIANT,
  385. (void FAR * FAR *)&pEnumerator
  386. );
  387. BAIL_ON_FAILURE(hr);
  388. ppAceHdr = (PACE_HEADER *)AllocADsMem(sizeof(PACE_HEADER)*dwAceCount);
  389. if (!ppAceHdr) {
  390. hr = E_OUTOFMEMORY;
  391. BAIL_ON_FAILURE(hr);
  392. }
  393. for (i = 0; i < dwAceCount; i++) {
  394. VariantInit(&varAce);
  395. hr = pEnumerator->Next(
  396. 1,
  397. &varAce,
  398. &cReturned
  399. );
  400. CONTINUE_ON_FAILURE(hr);
  401. hr = (V_DISPATCH(&varAce))->QueryInterface(
  402. IID_IADsAccessControlEntry,
  403. (void **)&pAccessControlEntry
  404. );
  405. CONTINUE_ON_FAILURE(hr);
  406. hr = ConvertAccessControlEntryToAce(
  407. pAccessControlEntry,
  408. &(pTempAce)
  409. );
  410. VariantClear(&varAce);
  411. if (pAccessControlEntry) {
  412. pAccessControlEntry->Release();
  413. pAccessControlEntry = NULL;
  414. }
  415. if (SUCCEEDED(hr))
  416. {
  417. *(ppAceHdr + dwCount) = (PACE_HEADER)pTempAce;
  418. dwCount++;
  419. }
  420. }
  421. hr = ComputeTotalAclSize(ppAceHdr, dwCount, &dwAclSize);
  422. BAIL_ON_FAILURE(hr);
  423. pAcl = (PACL)AllocADsMem(dwAclSize);
  424. if (!pAcl) {
  425. hr = E_OUTOFMEMORY;
  426. BAIL_ON_FAILURE(hr);
  427. }
  428. #if 0
  429. hr = pAccessList->get_AclRevision((long *)&dwAclRevision);
  430. BAIL_ON_FAILURE(hr);
  431. #else
  432. dwAclRevision = ACL_REVISION;
  433. #endif
  434. bRet = InitializeAcl(
  435. pAcl,
  436. dwAclSize,
  437. dwAclRevision
  438. );
  439. if (!bRet) {
  440. dwRet = GetLastError();
  441. hr = HRESULT_FROM_WIN32(dwRet);
  442. BAIL_ON_FAILURE(hr);
  443. }
  444. for (i = 0; i < dwCount; i++) {
  445. bRet = AddAce(
  446. pAcl,
  447. dwAclRevision,
  448. i,
  449. (LPBYTE)*(ppAceHdr + i),
  450. (*(ppAceHdr + i))->AceSize
  451. );
  452. if (!bRet) {
  453. dwRet = GetLastError();
  454. hr = HRESULT_FROM_WIN32(dwRet);
  455. BAIL_ON_FAILURE(hr);
  456. }
  457. }
  458. *ppAcl = pAcl;
  459. error:
  460. if (ppAceHdr) {
  461. for (i = 0; i < dwCount; i++) {
  462. if (*(ppAceHdr + i)) {
  463. FreeADsMem(*(ppAceHdr + i));
  464. }
  465. }
  466. FreeADsMem(ppAceHdr);
  467. }
  468. if (pUnknown) {
  469. pUnknown->Release();
  470. }
  471. if (pEnumerator) {
  472. pEnumerator->Release();
  473. }
  474. RRETURN(hr);
  475. }
  476. HRESULT
  477. ConvertAccessControlEntryToAce(
  478. IADsAccessControlEntry * pAccessControlEntry,
  479. LPBYTE * ppAce
  480. )
  481. {
  482. DWORD dwAceType = 0;
  483. HRESULT hr = S_OK;
  484. BSTR bstrTrustee = NULL;
  485. PSID pSid = NULL;
  486. DWORD dwSidSize = 0;
  487. DWORD dwAceFlags = 0;
  488. DWORD dwAccessMask = 0;
  489. DWORD dwAceSize = 0;
  490. LPBYTE pAce = NULL;
  491. PACCESS_MASK pAccessMask = NULL;
  492. PSID pSidAddress = NULL;
  493. PUSHORT pCompoundAceType = NULL;
  494. DWORD dwCompoundAceType = 0;
  495. PACE_HEADER pAceHeader = NULL;
  496. LPBYTE pOffset = NULL;
  497. BSTR bstrObjectTypeClsid = NULL;
  498. BSTR bstrInheritedObjectTypeClsid = NULL;
  499. GUID ObjectTypeGUID;
  500. GUID InheritedObjectTypeGUID;
  501. PULONG pFlags;
  502. DWORD dwFlags = 0;
  503. hr = pAccessControlEntry->get_AceType((LONG *)&dwAceType);
  504. BAIL_ON_FAILURE(hr);
  505. hr = pAccessControlEntry->get_Trustee(&bstrTrustee);
  506. BAIL_ON_FAILURE(hr);
  507. hr = ConvertTrusteeToSid(
  508. bstrTrustee,
  509. &pSid,
  510. &dwSidSize
  511. );
  512. BAIL_ON_FAILURE(hr);
  513. hr = pAccessControlEntry->get_AceFlags((long *)&dwAceFlags);
  514. BAIL_ON_FAILURE(hr);
  515. hr = pAccessControlEntry->get_AccessMask((long *)&dwAccessMask);
  516. BAIL_ON_FAILURE(hr);
  517. //
  518. // we will compensateby adding the entire ACE size
  519. //
  520. dwAceSize = dwSidSize - sizeof(ULONG);
  521. switch (dwAceType) {
  522. case ACCESS_ALLOWED_ACE_TYPE:
  523. dwAceSize += sizeof(ACCESS_ALLOWED_ACE);
  524. pAce = (LPBYTE)AllocADsMem(dwAceSize);
  525. if (!pAce) {
  526. hr = E_OUTOFMEMORY;
  527. BAIL_ON_FAILURE(hr);
  528. }
  529. pAceHeader = (PACE_HEADER)pAce;
  530. pAceHeader->AceType = (UCHAR)dwAceType;
  531. pAceHeader->AceFlags = (UCHAR)dwAceFlags;
  532. pAceHeader->AceSize = (USHORT)dwAceSize;
  533. pAccessMask = (PACCESS_MASK)((LPBYTE)pAceHeader + sizeof(ACE_HEADER));
  534. *pAccessMask = (ACCESS_MASK)dwAccessMask;
  535. pSidAddress = (PSID)((LPBYTE)pAccessMask + sizeof(ACCESS_MASK));
  536. memcpy(pSidAddress, pSid, dwSidSize);
  537. break;
  538. case ACCESS_DENIED_ACE_TYPE:
  539. dwAceSize += sizeof(ACCESS_ALLOWED_ACE);
  540. pAce = (LPBYTE)AllocADsMem(dwAceSize);
  541. if (!pAce) {
  542. hr = E_OUTOFMEMORY;
  543. BAIL_ON_FAILURE(hr);
  544. }
  545. pAceHeader = (PACE_HEADER)pAce;
  546. pAceHeader->AceType = (UCHAR)dwAceType;
  547. pAceHeader->AceFlags = (UCHAR)dwAceFlags;
  548. pAceHeader->AceSize = (USHORT)dwAceSize;
  549. pAccessMask = (PACCESS_MASK)((LPBYTE)pAceHeader + sizeof(ACE_HEADER));
  550. *pAccessMask = (ACCESS_MASK)dwAccessMask;
  551. pSidAddress = (PSID)((LPBYTE)pAccessMask + sizeof(ACCESS_MASK));
  552. memcpy(pSidAddress, pSid, dwSidSize);
  553. break;
  554. case SYSTEM_AUDIT_ACE_TYPE:
  555. dwAceSize += sizeof(ACCESS_ALLOWED_ACE);
  556. pAce = (LPBYTE)AllocADsMem(dwAceSize);
  557. if (!pAce) {
  558. hr = E_OUTOFMEMORY;
  559. BAIL_ON_FAILURE(hr);
  560. }
  561. pAceHeader = (PACE_HEADER)pAce;
  562. pAceHeader->AceType = (UCHAR)dwAceType;
  563. pAceHeader->AceFlags = (UCHAR)dwAceFlags;
  564. pAceHeader->AceSize = (USHORT)dwAceSize;
  565. pAccessMask = (PACCESS_MASK)((LPBYTE)pAceHeader + sizeof(ACE_HEADER));
  566. *pAccessMask = (ACCESS_MASK)dwAccessMask;
  567. pSidAddress = (PSID)((LPBYTE)pAccessMask + sizeof(ACCESS_MASK));
  568. memcpy(pSidAddress, pSid, dwSidSize);
  569. break;
  570. case SYSTEM_ALARM_ACE_TYPE:
  571. dwAceSize += sizeof(ACCESS_ALLOWED_ACE);
  572. pAce = (LPBYTE)AllocADsMem(dwAceSize);
  573. if (!pAce) {
  574. hr = E_OUTOFMEMORY;
  575. BAIL_ON_FAILURE(hr);
  576. }
  577. pAceHeader = (PACE_HEADER)pAce;
  578. pAceHeader->AceType = (UCHAR)dwAceType;
  579. pAceHeader->AceFlags = (UCHAR)dwAceFlags;
  580. pAceHeader->AceSize = (USHORT)dwAceSize;
  581. pAccessMask = (PACCESS_MASK)((LPBYTE)pAceHeader + sizeof(ACE_HEADER));
  582. *pAccessMask = (ACCESS_MASK)dwAccessMask;
  583. pSidAddress = (PSID)((LPBYTE)pAccessMask + sizeof(ACCESS_MASK));
  584. memcpy(pSidAddress, pSid, dwSidSize);
  585. break;
  586. case ACCESS_ALLOWED_COMPOUND_ACE_TYPE:
  587. dwAceSize += sizeof(COMPOUND_ACCESS_ALLOWED_ACE);
  588. pAce = (LPBYTE)AllocADsMem(dwAceSize);
  589. if (!pAce) {
  590. hr = E_OUTOFMEMORY;
  591. BAIL_ON_FAILURE(hr);
  592. }
  593. pAceHeader = (PACE_HEADER)pAce;
  594. pAceHeader->AceType = (UCHAR)dwAceType;
  595. pAceHeader->AceFlags = (UCHAR)dwAceFlags;
  596. pAceHeader->AceSize = (USHORT)dwAceSize;
  597. pAccessMask = (PACCESS_MASK)((LPBYTE)pAceHeader + sizeof(ACE_HEADER));
  598. *pAccessMask = (ACCESS_MASK)dwAccessMask;
  599. pCompoundAceType = (PUSHORT)(pAccessMask + sizeof(ACCESS_MASK));
  600. *pCompoundAceType = (USHORT)dwCompoundAceType;
  601. //
  602. // Fill in the reserved field here.
  603. //
  604. pSidAddress = (PSID)((LPBYTE)pCompoundAceType + sizeof(DWORD));
  605. memcpy(pSidAddress, pSid, dwSidSize);
  606. break;
  607. case ACCESS_ALLOWED_OBJECT_ACE_TYPE:
  608. case ACCESS_DENIED_OBJECT_ACE_TYPE:
  609. case SYSTEM_AUDIT_OBJECT_ACE_TYPE:
  610. case SYSTEM_ALARM_OBJECT_ACE_TYPE:
  611. hr = pAccessControlEntry->get_AceFlags((LONG *)&dwAceFlags);
  612. BAIL_ON_FAILURE(hr);
  613. hr = pAccessControlEntry->get_Flags((LONG *)&dwFlags);
  614. BAIL_ON_FAILURE(hr);
  615. if (dwFlags & ACE_OBJECT_TYPE_PRESENT) {
  616. dwAceSize += sizeof(GUID);
  617. }
  618. if (dwFlags & ACE_INHERITED_OBJECT_TYPE_PRESENT) {
  619. dwAceSize += sizeof(GUID);
  620. }
  621. hr = pAccessControlEntry->get_ObjectType(&bstrObjectTypeClsid);
  622. BAIL_ON_FAILURE(hr);
  623. hr = CLSIDFromString(bstrObjectTypeClsid, &ObjectTypeGUID);
  624. BAIL_ON_FAILURE(hr);
  625. hr = pAccessControlEntry->get_InheritedObjectType(&bstrInheritedObjectTypeClsid);
  626. BAIL_ON_FAILURE(hr);
  627. hr = CLSIDFromString(bstrInheritedObjectTypeClsid, &InheritedObjectTypeGUID);
  628. BAIL_ON_FAILURE(hr);
  629. dwAceSize += sizeof(ACCESS_ALLOWED_OBJECT_ACE);
  630. pAce = (LPBYTE)AllocADsMem(dwAceSize);
  631. if (!pAce) {
  632. hr = E_OUTOFMEMORY;
  633. BAIL_ON_FAILURE(hr);
  634. }
  635. pAceHeader = (PACE_HEADER)pAce;
  636. pAceHeader->AceType = (UCHAR)dwAceType;
  637. pAceHeader->AceFlags = (UCHAR)dwAceFlags;
  638. pAceHeader->AceSize = (USHORT)dwAceSize;
  639. pAccessMask = (PACCESS_MASK)((LPBYTE)pAceHeader + sizeof(ACE_HEADER));
  640. *pAccessMask = (ACCESS_MASK)dwAccessMask;
  641. //
  642. // Fill in Flags
  643. //
  644. pOffset = (LPBYTE)((LPBYTE)pAceHeader + sizeof(ACE_HEADER) + sizeof(ACCESS_MASK));
  645. pFlags = (PULONG)(pOffset);
  646. *pFlags = dwFlags;
  647. pOffset += sizeof(ULONG);
  648. if (dwFlags & ACE_OBJECT_TYPE_PRESENT) {
  649. memcpy(pOffset, &ObjectTypeGUID, sizeof(GUID));
  650. pOffset += sizeof(GUID);
  651. }
  652. if (dwFlags & ACE_INHERITED_OBJECT_TYPE_PRESENT) {
  653. memcpy(pOffset, &InheritedObjectTypeGUID, sizeof(GUID));
  654. pOffset += sizeof(GUID);
  655. }
  656. pSidAddress = (PSID)((LPBYTE)pOffset);
  657. memcpy(pSidAddress, pSid, dwSidSize);
  658. break;
  659. }
  660. *ppAce = pAce;
  661. error:
  662. if (bstrTrustee) {
  663. ADsFreeString(bstrTrustee);
  664. }
  665. if (pSid) {
  666. FreeADsMem(pSid);
  667. }
  668. RRETURN(hr);
  669. }
  670. HRESULT
  671. ComputeTotalAclSize(
  672. PACE_HEADER * ppAceHdr,
  673. DWORD dwAceCount,
  674. PDWORD pdwAclSize
  675. )
  676. {
  677. DWORD i = 0;
  678. PACE_HEADER pAceHdr = NULL;
  679. DWORD dwAceSize = 0;
  680. DWORD dwAclSize = 0;
  681. for (i = 0; i < dwAceCount; i++) {
  682. pAceHdr = *(ppAceHdr + i);
  683. dwAceSize = pAceHdr->AceSize;
  684. dwAclSize += dwAceSize;
  685. }
  686. dwAclSize += sizeof(ACL);
  687. *pdwAclSize = dwAclSize;
  688. RRETURN(S_OK);
  689. }
  690. HRESULT
  691. ConvertTrusteeToSid(
  692. BSTR bstrTrustee,
  693. PSID * ppSid,
  694. PDWORD pdwSidSize
  695. )
  696. {
  697. HRESULT hr = S_OK;
  698. BYTE Sid[MAX_PATH];
  699. DWORD dwSidSize = sizeof(Sid);
  700. DWORD dwRet = 0;
  701. WCHAR szDomainName[MAX_PATH];
  702. DWORD dwDomainSize = sizeof(szDomainName)/sizeof(WCHAR);
  703. SID_NAME_USE eUse;
  704. LPWSTR pszTrustee = (LPWSTR)bstrTrustee;
  705. PSID pSid = NULL;
  706. dwSidSize = sizeof(Sid);
  707. if ((*pszTrustee == (WCHAR)'\\') || (*pszTrustee == WCHAR('/'))){
  708. pszTrustee++;
  709. }
  710. dwRet = LookupAccountName(
  711. NULL,
  712. pszTrustee,
  713. Sid,
  714. &dwSidSize,
  715. szDomainName,
  716. &dwDomainSize,
  717. (PSID_NAME_USE)&eUse
  718. );
  719. if (!dwRet) {
  720. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(GetLastError()));
  721. }
  722. pSid = AllocADsMem(dwSidSize);
  723. if (!pSid) {
  724. hr = E_OUTOFMEMORY;
  725. BAIL_ON_FAILURE(hr);
  726. }
  727. memcpy(pSid, Sid, dwSidSize);
  728. *pdwSidSize = dwSidSize;
  729. *ppSid = pSid;
  730. error:
  731. RRETURN(hr);
  732. }