Source code of Windows XP (NT5)
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.

968 lines
23 KiB

  1. /*---------------------------------------------------------------------------
  2. File: SD.cpp
  3. Comments: class for manipulating security descriptors
  4. This class provides a thin wrapper around the security descriptor APIs, and
  5. also helps us with some of our processing heuristics.
  6. The NT APIs to read and write security descriptors for files, etc. generally
  7. return a self-relative SD when reading, but expect an absolute SD when writing
  8. Thus, we keep the original data of the SD as read, in self-relative form in m_relSD,
  9. and store any changes in absolute form in m_absSD. This allows us to easily track
  10. which parts of the SD have been modified, and to compare the before and after versions
  11. of the SD.
  12. As an optimization in our ACL translation, we can compare each SD to the initial
  13. state of the last SD we processed. If it is the same, we can simply write the
  14. result we have already calculated, instead of calculating it again.
  15. (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  16. Proprietary and confidential to Mission Critical Software, Inc.
  17. REVISION LOG ENTRY
  18. Revision By: Christy Boles
  19. Revised on 01-Oct-98 15:51:41
  20. ---------------------------------------------------------------------------
  21. */
  22. #include <windows.h>
  23. #include "McsDebug.h"
  24. #include "McsDbgU.h"
  25. #include "SD.hpp"
  26. TSD::TSD(
  27. SECURITY_DESCRIPTOR * pSD, // in - pointer to self-relative security descriptor
  28. SecuredObjectType objectType, // in - type of object this security descriptor secures
  29. BOOL bResponsibleForDelete // in - if TRUE, this class will delete the memory for pSD
  30. )
  31. {
  32. m_absSD = MakeAbsSD(pSD);
  33. m_bOwnerChanged = FALSE;
  34. m_bGroupChanged = FALSE;
  35. m_bDACLChanged = FALSE;
  36. m_bSACLChanged = FALSE;
  37. m_bNeedToFreeSD = TRUE;
  38. m_bNeedToFreeOwner = TRUE;
  39. m_bNeedToFreeGroup = TRUE;
  40. m_bNeedToFreeDacl = TRUE;
  41. m_bNeedToFreeSacl = TRUE;
  42. m_ObjectType = objectType;
  43. if ( bResponsibleForDelete )
  44. {
  45. free(pSD);
  46. }
  47. }
  48. TSD::TSD(
  49. TSD * pSD // in - another security descriptor
  50. )
  51. {
  52. MCSVERIFY(pSD);
  53. if ( pSD )
  54. {
  55. m_absSD = pSD->MakeAbsSD();
  56. m_bOwnerChanged = FALSE;
  57. m_bGroupChanged = FALSE;
  58. m_bDACLChanged = FALSE;
  59. m_bSACLChanged = FALSE;
  60. m_bNeedToFreeSD = TRUE;
  61. m_bNeedToFreeOwner = TRUE;
  62. m_bNeedToFreeGroup = TRUE;
  63. m_bNeedToFreeDacl = TRUE;
  64. m_bNeedToFreeSacl = TRUE;
  65. m_ObjectType = pSD->GetType();
  66. }
  67. }
  68. TSD::~TSD()
  69. {
  70. MCSVERIFY(m_absSD);
  71. if ( m_bNeedToFreeSD )
  72. {
  73. if (m_absSD)
  74. FreeAbsSD(m_absSD,FALSE);
  75. m_absSD = NULL;
  76. }
  77. }
  78. void
  79. TSD::FreeAbsSD(
  80. SECURITY_DESCRIPTOR * pSD, // in - pointer to security descriptor to free
  81. BOOL bAll // in - flag whether to free all parts of the SD, or only those
  82. // allocated by this class
  83. )
  84. {
  85. PSID sid = NULL;
  86. PACL acl = NULL;
  87. BOOL defaulted;
  88. BOOL present;
  89. GetSecurityDescriptorOwner(pSD,&sid,&defaulted);
  90. if ( sid && ( m_bNeedToFreeOwner || bAll ) )
  91. {
  92. free(sid);
  93. sid = NULL;
  94. }
  95. GetSecurityDescriptorGroup(pSD,&sid,&defaulted);
  96. if ( sid && ( m_bNeedToFreeGroup || bAll ) )
  97. {
  98. free(sid);
  99. sid = NULL;
  100. }
  101. GetSecurityDescriptorDacl(pSD,&present,&acl,&defaulted);
  102. if ( acl && (m_bNeedToFreeDacl || bAll ) )
  103. {
  104. free(acl);
  105. acl = NULL;
  106. }
  107. GetSecurityDescriptorSacl(pSD,&present,&acl,&defaulted);
  108. if ( acl && ( m_bNeedToFreeSacl || bAll ) )
  109. {
  110. free(acl);
  111. acl = NULL;
  112. }
  113. free(pSD);
  114. }
  115. SECURITY_DESCRIPTOR * // ret- copy of the SD, in Absolute format
  116. TSD::MakeAbsSD(
  117. SECURITY_DESCRIPTOR * pSD // in - security descriptor to copy
  118. ) const
  119. {
  120. DWORD sd_size = (sizeof SECURITY_DESCRIPTOR);
  121. DWORD dacl_size = 0;
  122. DWORD sacl_size = 0;
  123. DWORD owner_size = 0;
  124. DWORD group_size = 0;
  125. // Allocate space for the SD and its parts
  126. SECURITY_DESCRIPTOR * absSD = (SECURITY_DESCRIPTOR *) malloc(sd_size);
  127. if (!absSD || !pSD)
  128. {
  129. if (absSD)
  130. {
  131. free(absSD);
  132. absSD = NULL;
  133. }
  134. return NULL;
  135. }
  136. PACL absDacl = NULL;
  137. PACL absSacl = NULL;
  138. PSID absOwner = NULL;
  139. PSID absGroup = NULL;
  140. if ( ! MakeAbsoluteSD(pSD,absSD,&sd_size,absDacl,&dacl_size,absSacl,&sacl_size
  141. ,absOwner,&owner_size,absGroup,&group_size) )
  142. {
  143. // DWORD rc = GetLastError();
  144. // didn't work: increase sizes and try again
  145. if ( sd_size > (sizeof SECURITY_DESCRIPTOR) )
  146. {
  147. free(absSD);
  148. absSD = (SECURITY_DESCRIPTOR *) malloc(sd_size);
  149. if (!absSD)
  150. return NULL;
  151. }
  152. if ( dacl_size )
  153. {
  154. absDacl = (PACL)malloc(dacl_size);
  155. if (!absDacl)
  156. {
  157. free(absSD);
  158. absSD = NULL;
  159. return NULL;
  160. }
  161. }
  162. if ( sacl_size )
  163. {
  164. absSacl = (PACL)malloc(sacl_size);
  165. if (!absSacl)
  166. {
  167. free(absSD);
  168. free(absDacl);
  169. absSD = NULL;
  170. absDacl = NULL;
  171. return NULL;
  172. }
  173. }
  174. if ( owner_size )
  175. {
  176. absOwner = (PSID)malloc(owner_size);
  177. if (!absOwner)
  178. {
  179. free(absSD);
  180. free(absDacl);
  181. free(absSacl);
  182. absSD = NULL;
  183. absDacl = NULL;
  184. absSacl = NULL;
  185. return NULL;
  186. }
  187. }
  188. if ( group_size )
  189. {
  190. absGroup = (PSID)malloc(group_size);
  191. if (!absGroup)
  192. {
  193. free(absSD);
  194. free(absDacl);
  195. free(absSacl);
  196. free(absOwner);
  197. absSD = NULL;
  198. absDacl = NULL;
  199. absSacl = NULL;
  200. absOwner = NULL;
  201. return NULL;
  202. }
  203. }
  204. // try again with bigger buffers
  205. if ( ! MakeAbsoluteSD(pSD,absSD,&sd_size,absDacl,&dacl_size,absSacl,&sacl_size
  206. ,absOwner,&owner_size,absGroup,&group_size) )
  207. {
  208. free(absSD);
  209. free(absDacl);
  210. free(absSacl);
  211. free(absOwner);
  212. free(absGroup);
  213. absSD = NULL;
  214. absDacl = NULL;
  215. absSacl = NULL;
  216. absOwner = NULL;
  217. absGroup = NULL;
  218. }
  219. }
  220. return absSD;
  221. }
  222. SECURITY_DESCRIPTOR * // ret- copy of the SD, in Absolute format
  223. TSD::MakeAbsSD() const
  224. {
  225. SECURITY_DESCRIPTOR * absSD = NULL;
  226. SECURITY_DESCRIPTOR * relSD = MakeRelSD();
  227. if (relSD)
  228. {
  229. absSD = MakeAbsSD(relSD);
  230. free(relSD);
  231. }
  232. return absSD;
  233. }
  234. SECURITY_DESCRIPTOR * // ret- copy of the SD, in self-relative form
  235. TSD::MakeRelSD() const
  236. {
  237. DWORD nBytes;
  238. SECURITY_DESCRIPTOR * relSD = NULL;
  239. nBytes = GetSecurityDescriptorLength(m_absSD);
  240. relSD = (SECURITY_DESCRIPTOR *)malloc(nBytes);
  241. if (!relSD)
  242. return NULL;
  243. if (! MakeSelfRelativeSD(m_absSD,relSD,&nBytes) )
  244. {
  245. free(relSD);
  246. relSD = NULL;
  247. }
  248. return relSD;
  249. }
  250. PSID const // ret- sid for security descriptor owner field
  251. TSD::GetOwner() const
  252. {
  253. PSID ownersid = NULL;
  254. BOOL ownerDefaulted;
  255. GetSecurityDescriptorOwner(m_absSD,&ownersid,&ownerDefaulted);
  256. return ownersid;
  257. }
  258. void
  259. TSD::SetOwner(
  260. PSID pNewOwner // in - new value for owner field
  261. )
  262. {
  263. MCSVERIFY(IsValidSecurityDescriptor(m_absSD));
  264. if ( IsValidSid(pNewOwner) )
  265. {
  266. if ( m_bNeedToFreeOwner )
  267. {
  268. PSID old = GetOwner();
  269. free(old);
  270. }
  271. SetSecurityDescriptorOwner(m_absSD,pNewOwner,FALSE);
  272. m_bOwnerChanged = TRUE;
  273. m_bNeedToFreeOwner = TRUE;
  274. }
  275. else
  276. {
  277. MCSVERIFY(FALSE);
  278. }
  279. }
  280. PSID const // ret- sid for security descriptor owner field
  281. TSD::GetGroup() const
  282. {
  283. PSID grpsid = NULL;
  284. BOOL grpDefaulted;
  285. MCSVERIFY(IsValidSecurityDescriptor(m_absSD));
  286. GetSecurityDescriptorGroup(m_absSD,&grpsid,&grpDefaulted);
  287. return grpsid;
  288. }
  289. void
  290. TSD::SetGroup(
  291. PSID pNewGroup // in - new value for primary group field.
  292. )
  293. {
  294. MCSVERIFY(IsValidSecurityDescriptor(m_absSD));
  295. if ( IsValidSid(pNewGroup) )
  296. {
  297. if ( m_bNeedToFreeGroup )
  298. {
  299. PSID old = GetGroup();
  300. free(old);
  301. }
  302. SetSecurityDescriptorGroup(m_absSD,pNewGroup,FALSE);
  303. m_bGroupChanged = TRUE;
  304. m_bNeedToFreeGroup = TRUE;
  305. }
  306. else
  307. {
  308. MCSVERIFY(FALSE);
  309. }
  310. }
  311. PACL const // ret- pointer to DACL
  312. TSD::GetDacl() const
  313. {
  314. PACL acl = NULL;
  315. BOOL defaulted;
  316. BOOL present;
  317. GetSecurityDescriptorDacl(m_absSD,&present,&acl,&defaulted);
  318. return acl;
  319. }
  320. void
  321. TSD::SetDacl(
  322. PACL pNewAcl, // in - new DACL
  323. BOOL present // in - flag, TRUE means DACL is present.
  324. )
  325. {
  326. BOOL defaulted = FALSE;
  327. if ( IsValidAcl(pNewAcl) )
  328. {
  329. if ( m_bNeedToFreeDacl )
  330. {
  331. PACL old = GetDacl();
  332. if ( old != pNewAcl )
  333. {
  334. free(old);
  335. }
  336. }
  337. if (! SetSecurityDescriptorDacl(m_absSD,present,pNewAcl,defaulted) )
  338. {
  339. // DWORD rc = GetLastError();
  340. }
  341. m_bDACLChanged = TRUE;
  342. m_bNeedToFreeDacl = TRUE;
  343. }
  344. else
  345. {
  346. MCSVERIFY(FALSE);
  347. }
  348. }
  349. PACL const // ret- pointer to SACL
  350. TSD::GetSacl() const
  351. {
  352. PACL acl = NULL;
  353. BOOL defaulted;
  354. BOOL present;
  355. GetSecurityDescriptorSacl(m_absSD,&present,&acl,&defaulted);
  356. return acl;
  357. }
  358. void
  359. TSD::SetSacl(
  360. PACL pNewAcl, // in - new SACL
  361. BOOL present // in - flag, TRUE means SACL is present
  362. )
  363. {
  364. BOOL defaulted = FALSE;
  365. if ( IsValidAcl(pNewAcl) )
  366. {
  367. if ( m_bNeedToFreeSacl )
  368. {
  369. PACL old = GetSacl();
  370. if ( old != pNewAcl )
  371. {
  372. free(old);
  373. }
  374. }
  375. SetSecurityDescriptorSacl(m_absSD,present,pNewAcl,defaulted);
  376. m_bSACLChanged = TRUE;
  377. m_bNeedToFreeSacl = TRUE;
  378. }
  379. else
  380. {
  381. MCSVERIFY(FALSE);
  382. }
  383. }
  384. BOOL
  385. TSD::IsOwnerDefaulted() const
  386. {
  387. PSID ownersid = NULL;
  388. BOOL ownerDefaulted = FALSE;
  389. GetSecurityDescriptorOwner(m_absSD,&ownersid,&ownerDefaulted);
  390. return ownerDefaulted;
  391. }
  392. BOOL
  393. TSD::IsGroupDefaulted() const
  394. {
  395. PSID groupsid = NULL;
  396. BOOL groupDefaulted = FALSE;
  397. GetSecurityDescriptorGroup(m_absSD,&groupsid,&groupDefaulted);
  398. return groupDefaulted;
  399. }
  400. BOOL
  401. TSD::IsDaclDefaulted() const
  402. {
  403. PACL acl = NULL;
  404. BOOL defaulted = FALSE;
  405. BOOL present;
  406. GetSecurityDescriptorDacl(m_absSD,&present,&acl,&defaulted);
  407. return defaulted;
  408. }
  409. BOOL
  410. TSD::IsDaclPresent() const
  411. {
  412. PACL acl = NULL;
  413. BOOL defaulted;
  414. BOOL present = FALSE;
  415. GetSecurityDescriptorDacl(m_absSD,&present,&acl,&defaulted);
  416. return present;
  417. }
  418. BOOL
  419. TSD::IsSaclDefaulted() const
  420. {
  421. PACL acl = NULL;
  422. BOOL defaulted = FALSE;
  423. BOOL present;
  424. GetSecurityDescriptorSacl(m_absSD,&present,&acl,&defaulted);
  425. return defaulted;
  426. }
  427. BOOL
  428. TSD::IsSaclPresent() const
  429. {
  430. PACL acl = NULL;
  431. BOOL defaulted;
  432. BOOL present = FALSE;
  433. GetSecurityDescriptorSacl(m_absSD,&present,&acl,&defaulted);
  434. return present;
  435. }
  436. BOOL
  437. TSD::operator == (TSD & otherSD)
  438. {
  439. return EqualSD(&otherSD);
  440. }
  441. // Compares the relative SDs (relSD) for 2 TSecurityDescriptor's.
  442. // true -> SDs are equal, false -> SDs are not equal
  443. BOOL // ret -true iff *(this->relSD) == *(otherSD->relSD)
  444. TSD::EqualSD(
  445. TSD * otherSD // in -TSecurityDescriptor to compare this SD to
  446. )
  447. {
  448. PSID ps1;
  449. PSID ps2;
  450. PACL acl1;
  451. PACL acl2;
  452. BOOL diffound = FALSE;
  453. // both must be valid SD's
  454. ps1 = NULL;
  455. ps2 = NULL;
  456. // if GetSecurityDescriptor* fails, EqualSid, or ACLCompare will fail
  457. // Compare owners
  458. ps1 = GetOwner();
  459. ps2 = otherSD->GetOwner();
  460. if ((ps1) && (ps2) && (! EqualSid(ps1,ps2)))
  461. {
  462. diffound = TRUE;
  463. }
  464. ps1 = NULL;
  465. ps2 = NULL;
  466. // Compare Groups
  467. if ( ! diffound )
  468. {
  469. ps1 = GetGroup();
  470. ps2 = otherSD->GetGroup();
  471. if ((ps1) && (ps2) && (! EqualSid(ps1,ps2)))
  472. {
  473. diffound = TRUE;
  474. }
  475. acl1 = NULL;
  476. acl2 = NULL;
  477. }
  478. // Compare DACLs
  479. if ( ! diffound )
  480. {
  481. acl1 = GetDacl();
  482. acl2 = otherSD->GetDacl();
  483. if ((acl1) && (acl2) &&
  484. (!ACLCompare(acl1,IsDaclPresent(),acl2,otherSD->IsDaclPresent())))
  485. {
  486. diffound = TRUE;
  487. }
  488. }
  489. // Compare SACLs
  490. if ( ! diffound )
  491. {
  492. acl1 = GetSacl();
  493. acl2 = otherSD->GetSacl();
  494. if ((acl1) && (acl2))
  495. diffound = (! ACLCompare(acl1,IsSaclPresent(),acl2,otherSD->IsSaclPresent()) );
  496. }
  497. return ! diffound;
  498. }
  499. BOOL // ret- TRUE if both ACLs are the same
  500. TSD::ACLCompare(
  501. PACL acl1, // in -ptr to first acl to compare
  502. BOOL present1, // in -present flag for acl1
  503. PACL acl2, // in -ptr to second acl to compare
  504. BOOL present2 // in -present flag for acl2
  505. )
  506. {
  507. DWORD size1;
  508. DWORD size2;
  509. bool same = true;
  510. if ( present1 && present2 )
  511. {
  512. if ( ( !acl1 && acl2 )
  513. || ( acl1 && ! acl2 ) )
  514. {
  515. same = false;
  516. }
  517. if ( acl1 && acl2)
  518. {
  519. size1 = acl1->AclSize;
  520. size2 = acl2->AclSize;
  521. if ( size1 != size2 )
  522. {
  523. same = false;
  524. }
  525. else
  526. {
  527. if ( memcmp(acl1,acl2,size1) )
  528. {
  529. same = false;
  530. }
  531. }
  532. }
  533. }
  534. else
  535. {
  536. if ( present1 || present2 )
  537. {
  538. same = false;
  539. }
  540. }
  541. return same;
  542. }
  543. int // ret- number of aces in the ACL
  544. TSD::ACLGetNumAces(
  545. PACL acl // in - DACL or SACL
  546. )
  547. {
  548. int nAces = 0;
  549. ACL_SIZE_INFORMATION info;
  550. if ( acl )
  551. {
  552. if ( GetAclInformation(acl,&info,(sizeof info),AclSizeInformation) )
  553. {
  554. nAces = info.AceCount;
  555. }
  556. else
  557. {
  558. // DWORD rc=GetLastError();
  559. }
  560. }
  561. return nAces;
  562. }
  563. DWORD // ret- number of free bytes in the ACL
  564. TSD::ACLGetFreeBytes(
  565. PACL acl // in - DACL or SACL
  566. )
  567. {
  568. int nFree = 0;
  569. ACL_SIZE_INFORMATION info;
  570. if ( acl )
  571. {
  572. if ( GetAclInformation(acl,&info,(sizeof info),AclSizeInformation) )
  573. {
  574. nFree = info.AclBytesFree;
  575. }
  576. }
  577. return nFree;
  578. }
  579. DWORD // ret- number of used bytes in the ACL
  580. TSD::ACLGetBytesInUse(
  581. PACL acl // in - DACL or SACL
  582. )
  583. {
  584. int nBytes = 0;
  585. ACL_SIZE_INFORMATION info;
  586. if ( acl )
  587. {
  588. if ( GetAclInformation(acl,&info,(sizeof info),AclSizeInformation) )
  589. {
  590. nBytes = info.AclBytesInUse;
  591. }
  592. }
  593. return nBytes;
  594. }
  595. void * // ret- pointer to ace
  596. TSD::ACLGetAce(
  597. PACL acl, // in - DACL or SACL
  598. int ndx // in - index of ace to retrieve
  599. )
  600. {
  601. void * ace = NULL;
  602. if ( ndx < ACLGetNumAces(acl) )
  603. {
  604. if ( ! GetAce(acl,ndx,(void**)&ace) )
  605. {
  606. ace = NULL;
  607. }
  608. }
  609. else
  610. {
  611. MCSASSERT(FALSE); // you specified a non-existant index
  612. }
  613. return ace;
  614. }
  615. void
  616. TSD::ACLDeleteAce(
  617. PACL acl, // in - DACL or SACL
  618. int ndx // in - index of ace to delete
  619. )
  620. {
  621. int nAces = ACLGetNumAces(acl);
  622. if ( ndx < nAces )
  623. {
  624. DeleteAce(acl,ndx);
  625. }
  626. else
  627. {
  628. MCSASSERT(FALSE); // you specified an invalid index
  629. }
  630. }
  631. // Access allowed aces are added at the beginning of the list, access denied aces are added at the end of the list
  632. void
  633. TSD::ACLAddAce(
  634. PACL * ppAcl, // i/o- DACL or SACL (this function may reallocate if the acl doesn't have room
  635. TACE * pAce, // in - ACE to add
  636. int pos // in - position
  637. )
  638. {
  639. DWORD ndx = (DWORD)pos;
  640. DWORD rc;
  641. PACL acl = (*ppAcl);
  642. PACL newAcl;
  643. DWORD numaces = ACLGetNumAces(acl);
  644. DWORD freebytes = ACLGetFreeBytes(acl);
  645. if (!pAce->GetBuffer())
  646. return;
  647. // allocate a new ACL if it doesn't already exist
  648. if ( ! acl )
  649. {
  650. acl = (PACL) malloc(SD_DEFAULT_ACL_SIZE);
  651. if (!acl)
  652. return;
  653. InitializeAcl(acl,SD_DEFAULT_ACL_SIZE,ACL_REVISION);
  654. numaces = ACLGetNumAces(acl);
  655. freebytes = ACLGetFreeBytes(acl);
  656. }
  657. if ( pos == -1 )
  658. {
  659. if ( pAce->IsAccessAllowedAce() )
  660. {
  661. ndx = 0;
  662. }
  663. else
  664. {
  665. ndx = MAXDWORD; // insert at end of the list
  666. }
  667. }
  668. WORD currAceSize = pAce->GetSize();
  669. if ( freebytes < currAceSize ) // we must allocate more space for the ace
  670. {
  671. //make a bigger acl
  672. newAcl = (ACL*)malloc(ACLGetBytesInUse(acl) + freebytes + currAceSize);
  673. if (!newAcl)
  674. {
  675. free(acl);
  676. acl = NULL;
  677. return;
  678. }
  679. memcpy(newAcl,acl,ACLGetBytesInUse(acl) + freebytes);
  680. newAcl->AclSize +=currAceSize;
  681. free(acl);
  682. acl = newAcl;
  683. }
  684. if (! AddAce(acl,ACL_REVISION,ndx,pAce->GetBuffer(),currAceSize) )
  685. {
  686. rc = GetLastError();
  687. }
  688. (*ppAcl) = acl;
  689. }
  690. // creates a new ace with the specified properties
  691. TACE::TACE(
  692. BYTE type, // in - type of ace (ACCESS_ALLOWED_ACE_TYPE, etc.)
  693. BYTE flags, // in - ace flags (controls inheritance, etc. use 0 for files)
  694. DWORD mask, // in - access control mask (see constants in sd.hpp)
  695. PSID sid // in - pointer to sid for this ace
  696. )
  697. {
  698. MCSVERIFY(sid);
  699. // allocate memory for the new ace
  700. DWORD size = (sizeof ACCESS_ALLOWED_ACE) + GetLengthSid(sid) - (sizeof DWORD);
  701. m_pAce = (ACCESS_ALLOWED_ACE *)malloc(size);
  702. // Initialize the ACE
  703. if (m_pAce)
  704. {
  705. m_bNeedToFree = TRUE;
  706. m_pAce->Header.AceType = type;
  707. m_pAce->Header.AceFlags = flags;
  708. m_pAce->Header.AceSize = (WORD) size;
  709. m_pAce->Mask = mask;
  710. memcpy(&m_pAce->SidStart,sid,GetLengthSid(sid));
  711. }
  712. }
  713. BYTE // ret- ace type (ACCESS_ALLOWED_ACE_TYPE, etc.)
  714. TACE::GetType()
  715. {
  716. MCSVERIFY(m_pAce);
  717. BYTE type = ACCESS_ALLOWED_ACE_TYPE;
  718. if (m_pAce)
  719. type = m_pAce->Header.AceType;
  720. return type;
  721. }
  722. BYTE // ret- ace flags (OBJECT_INHERIT_ACE, etc.)
  723. TACE::GetFlags()
  724. {
  725. MCSVERIFY(m_pAce);
  726. BYTE flags = OBJECT_INHERIT_ACE;
  727. if (m_pAce)
  728. flags = m_pAce->Header.AceFlags;
  729. return flags;
  730. }
  731. DWORD // ret- access control mask
  732. TACE::GetMask()
  733. {
  734. MCSVERIFY(m_pAce);
  735. DWORD mask = 0;
  736. if (m_pAce)
  737. mask = m_pAce->Mask;
  738. return mask;
  739. }
  740. PSID // ret- sid for this ace
  741. TACE::GetSid()
  742. {
  743. MCSVERIFY(m_pAce);
  744. PSID pSid = NULL;
  745. if (m_pAce)
  746. pSid = &m_pAce->SidStart;
  747. return pSid;
  748. }
  749. WORD // ret- size of the ace, in bytes
  750. TACE::GetSize()
  751. {
  752. MCSVERIFY(m_pAce);
  753. WORD size = 0;
  754. if (m_pAce)
  755. size = m_pAce->Header.AceSize;
  756. return size;
  757. }
  758. BOOL
  759. TACE::SetType(
  760. BYTE newType // in -new type for ace
  761. )
  762. {
  763. MCSVERIFY(m_pAce);
  764. if (!m_pAce)
  765. return FALSE;
  766. MCSASSERT( newType=ACCESS_ALLOWED_ACE_TYPE ||
  767. newType==ACCESS_DENIED_ACE_TYPE ||
  768. newType==SYSTEM_AUDIT_ACE_TYPE );
  769. m_pAce->Header.AceType = newType;
  770. return TRUE;
  771. }
  772. BOOL
  773. TACE::SetFlags(
  774. BYTE newFlags // in - new flags for ace
  775. )
  776. {
  777. MCSVERIFY(m_pAce);
  778. if (!m_pAce)
  779. return FALSE;
  780. m_pAce->Header.AceFlags = newFlags;
  781. return TRUE;
  782. }
  783. BOOL
  784. TACE::SetMask(
  785. DWORD newMask // in - new access control mask
  786. )
  787. {
  788. MCSVERIFY(m_pAce);
  789. if (!m_pAce)
  790. return FALSE;
  791. m_pAce->Mask = newMask;
  792. return TRUE;
  793. }
  794. BOOL
  795. TACE::SetSid(
  796. PSID sid // in - new SID for this ace
  797. )
  798. {
  799. BOOL result = FALSE;
  800. MCSVERIFY( m_pAce );
  801. MCSASSERT( IsValidSid(sid) );
  802. MCSASSERT( GetLengthSid(sid) == GetLengthSid(GetSid()) );
  803. if (!m_pAce)
  804. return FALSE;
  805. if ( GetLengthSid(sid) <= GetLengthSid(GetSid()) )
  806. {
  807. memcpy(&m_pAce->SidStart,sid,GetLengthSid(sid));
  808. result = TRUE;
  809. }
  810. return result;
  811. }
  812. BOOL
  813. TACE::IsAccessAllowedAce()
  814. {
  815. MCSVERIFY(m_pAce);
  816. return ( GetType() == ACCESS_ALLOWED_ACE_TYPE );
  817. }