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.

5005 lines
162 KiB

  1. ////////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Microsoft Windows //
  4. // Copyright (C) Microsoft Corporation, 1999. //
  5. // //
  6. // File: rewrite.cxx //
  7. // //
  8. // Contents: New marta rewrite functions. //
  9. // //
  10. // History: 4/99 KedarD Created //
  11. // //
  12. ////////////////////////////////////////////////////////////////////////////////
  13. #include <aclpch.hxx>
  14. #pragma hdrstop
  15. extern "C"
  16. {
  17. #include <stdio.h>
  18. #include <permit.h>
  19. #include <dsgetdc.h>
  20. #include <lmapibuf.h>
  21. #include <wmistr.h>
  22. #include <ntprov.hxx>
  23. #include <strings.h>
  24. #include <seopaque.h>
  25. #include <sertlp.h>
  26. #include <tables.h>
  27. }
  28. #define MARTA_DEBUG_NO 0
  29. ////////////////////////////////////////////////////////////////////////////////
  30. // //
  31. // STRUCTURES DEFINITIONS TO HOLD FUNCTION POINTERS START HERE //
  32. // //
  33. ////////////////////////////////////////////////////////////////////////////////
  34. typedef struct _MARTA_GET_FUNCTION_CONTEXT {
  35. FN_ADD_REF_CONTEXT fAddRefContext;
  36. FN_CLOSE_CONTEXT fCloseContext;
  37. FN_GET_DESIRED_ACCESS fGetDesiredAccess;
  38. FN_GET_PARENT_CONTEXT fGetParentContext;
  39. FN_GET_PROPERTIES fGetProperties;
  40. FN_GET_TYPE_PROPERTIES fGetTypeProperties;
  41. FN_OPEN_NAMED_OBJECT fOpenNamedObject;
  42. FN_OPEN_HANDLE_OBJECT fOpenHandleObject;
  43. FN_GET_RIGHTS fGetRights;
  44. } MARTA_GET_FUNCTION_CONTEXT, *PMARTA_GET_FUNCTION_CONTEXT;
  45. typedef struct _MARTA_SET_FUNCTION_CONTEXT {
  46. FN_ADD_REF_CONTEXT fAddRefContext;
  47. FN_CLOSE_CONTEXT fCloseContext;
  48. FN_FIND_FIRST fFindFirst;
  49. FN_FIND_NEXT fFindNext;
  50. FN_GET_DESIRED_ACCESS fGetDesiredAccess;
  51. FN_GET_PARENT_CONTEXT fGetParentContext;
  52. FN_GET_PROPERTIES fGetProperties;
  53. FN_GET_TYPE_PROPERTIES fGetTypeProperties;
  54. FN_GET_RIGHTS fGetRights;
  55. FN_OPEN_NAMED_OBJECT fOpenNamedObject;
  56. FN_OPEN_HANDLE_OBJECT fOpenHandleObject;
  57. FN_SET_RIGHTS fSetRights;
  58. FN_REOPEN_CONTEXT fReopenContext;
  59. FN_REOPEN_ORIG_CONTEXT fReopenOrigContext;
  60. FN_GET_NAME_FROM_CONTEXT fGetNameFromContext;
  61. } MARTA_SET_FUNCTION_CONTEXT, *PMARTA_SET_FUNCTION_CONTEXT;
  62. typedef struct _MARTA_INDEX_FUNCTION_CONTEXT {
  63. FN_OPEN_NAMED_OBJECT fOpenNamedObject;
  64. FN_CLOSE_CONTEXT fCloseContext;
  65. FN_GET_RIGHTS fGetRights;
  66. FN_GET_PARENT_NAME fGetParentName;
  67. } MARTA_INDEX_FUNCTION_CONTEXT, *PMARTA_INDEX_FUNCTION_CONTEXT;
  68. typedef struct _MARTA_RESET_FUNCTION_CONTEXT {
  69. FN_ADD_REF_CONTEXT fAddRefContext;
  70. FN_CLOSE_CONTEXT fCloseContext;
  71. FN_GET_DESIRED_ACCESS fGetDesiredAccess;
  72. FN_GET_PARENT_CONTEXT fGetParentContext;
  73. FN_GET_PROPERTIES fGetProperties;
  74. FN_GET_TYPE_PROPERTIES fGetTypeProperties;
  75. FN_OPEN_NAMED_OBJECT fOpenNamedObject;
  76. FN_GET_RIGHTS fGetRights;
  77. } MARTA_RESET_FUNCTION_CONTEXT, *PMARTA_RESET_FUNCTION_CONTEXT;
  78. ////////////////////////////////////////////////////////////////////////////////
  79. // //
  80. // MACRO DEFINITIONS START HERE //
  81. // //
  82. ////////////////////////////////////////////////////////////////////////////////
  83. #define MARTA_DACL_NOT_PROTECTED(sd, si) \
  84. (FLAG_ON((si), DACL_SECURITY_INFORMATION) && \
  85. !FLAG_ON((sd)->Control, SE_DACL_PROTECTED))
  86. #define MARTA_SACL_NOT_PROTECTED(sd, si) \
  87. (FLAG_ON((si), SACL_SECURITY_INFORMATION) && \
  88. !FLAG_ON((sd)->Control, SE_SACL_PROTECTED))
  89. #define MARTA_SD_NOT_PROTECTED(sd, si) \
  90. ((MARTA_DACL_NOT_PROTECTED((sd), (si))) || \
  91. (MARTA_SACL_NOT_PROTECTED((sd), (si))))
  92. #define MARTA_NT5_FLAGS_ON(c) \
  93. (FLAG_ON((c), (SE_SACL_AUTO_INHERITED | SE_DACL_AUTO_INHERITED | \
  94. SE_DACL_PROTECTED | SE_SACL_PROTECTED | \
  95. SE_DACL_AUTO_INHERIT_REQ | SE_SACL_AUTO_INHERIT_REQ)))
  96. #if 1
  97. #define MARTA_TURN_OFF_IMPERSONATION \
  98. if (OpenThreadToken( \
  99. GetCurrentThread(), \
  100. MAXIMUM_ALLOWED, \
  101. TRUE, \
  102. &ThreadHandle \
  103. )) \
  104. { RevertToSelf(); } \
  105. else \
  106. { ThreadHandle = NULL; }
  107. #define MARTA_TURN_ON_IMPERSONATION \
  108. if (ThreadHandle != NULL) \
  109. { \
  110. (VOID) SetThreadToken(NULL, ThreadHandle); \
  111. CloseHandle(ThreadHandle); \
  112. ThreadHandle = NULL; \
  113. }
  114. #else
  115. #define MARTA_TURN_ON_IMPERSONATION
  116. #define MARTA_TURN_OFF_IMPERSONATION
  117. #endif
  118. ////////////////////////////////////////////////////////////////////////////////
  119. // //
  120. // FUNCTION PROTOTYPES START HERE //
  121. // //
  122. ////////////////////////////////////////////////////////////////////////////////
  123. DWORD
  124. AccRewriteSetHandleRights(
  125. IN HANDLE Handle,
  126. IN SE_OBJECT_TYPE ObjectType,
  127. IN SECURITY_INFORMATION SecurityInfo,
  128. IN OUT PSECURITY_DESCRIPTOR pSecurityDescriptor
  129. );
  130. DWORD
  131. AccRewriteSetNamedRights(
  132. IN LPWSTR pObjectName,
  133. IN SE_OBJECT_TYPE ObjectType,
  134. IN SECURITY_INFORMATION SecurityInfo,
  135. IN OUT PSECURITY_DESCRIPTOR pSecurityDescriptor,
  136. IN BOOL bSkipInheritanceComputation
  137. );
  138. VOID
  139. MartaInitializeGetContext(
  140. IN SE_OBJECT_TYPE ObjectType,
  141. OUT PMARTA_GET_FUNCTION_CONTEXT pFunctionContext
  142. );
  143. VOID
  144. MartaInitializeIndexContext(
  145. IN SE_OBJECT_TYPE ObjectType,
  146. OUT PMARTA_INDEX_FUNCTION_CONTEXT pFunctionContext
  147. );
  148. BOOL
  149. MartaUpdateTree(
  150. IN SECURITY_INFORMATION SecurityInfo,
  151. IN PSECURITY_DESCRIPTOR pNewSD,
  152. IN PSECURITY_DESCRIPTOR pOldSD,
  153. IN MARTA_CONTEXT Context,
  154. IN HANDLE ProcessHandle,
  155. IN PMARTA_SET_FUNCTION_CONTEXT pMartaSetFunctionContext,
  156. IN PGENERIC_MAPPING pGenMap
  157. );
  158. BOOL
  159. MartaResetTree(
  160. IN SECURITY_INFORMATION SecurityInfo,
  161. IN SECURITY_INFORMATION TmpSeInfo,
  162. IN PSECURITY_DESCRIPTOR pNewSD,
  163. IN PSECURITY_DESCRIPTOR pEmptySD,
  164. IN MARTA_CONTEXT Context,
  165. IN HANDLE ProcessHandle,
  166. IN PMARTA_SET_FUNCTION_CONTEXT pMartaSetFunctionContext,
  167. IN PGENERIC_MAPPING pGenMap,
  168. IN ACCESS_MASK MaxAccessMask,
  169. IN ACCESS_MASK AccessMask,
  170. IN ACCESS_MASK RetryAccessMask,
  171. IN OUT PPROG_INVOKE_SETTING pOperation,
  172. IN FN_PROGRESS fnProgress,
  173. IN PVOID Args,
  174. IN BOOL KeepExplicit
  175. );
  176. DWORD
  177. MartaGetNT4NodeSD(
  178. IN PSECURITY_DESCRIPTOR pOldSD,
  179. IN OUT PSECURITY_DESCRIPTOR pOldChildSD,
  180. IN HANDLE ProcessHandle,
  181. IN BOOL bIsChildContainer,
  182. IN PGENERIC_MAPPING pGenMap,
  183. IN SECURITY_INFORMATION SecurityInfo
  184. );
  185. DWORD
  186. MartaCompareAndMarkInheritedAces(
  187. IN PACL pParentAcl,
  188. IN PACL pChildAcl,
  189. IN BOOL bIsChildContainer,
  190. OUT PBOOL pCompareStatus
  191. );
  192. BOOL
  193. MartaEqualAce(
  194. IN PACE_HEADER pParentAce,
  195. IN PACE_HEADER pChildAce,
  196. IN BOOL bIsChildContainer
  197. );
  198. DWORD
  199. MartaManualPropagation(
  200. IN MARTA_CONTEXT Context,
  201. IN SECURITY_INFORMATION SecurityInfo,
  202. IN OUT PSECURITY_DESCRIPTOR pSD,
  203. IN PGENERIC_MAPPING pGenMap,
  204. IN BOOL bDoPropagate,
  205. IN BOOL bReadOldProtectedBits,
  206. IN PMARTA_SET_FUNCTION_CONTEXT pMartaSetFunctionContext,
  207. IN BOOL bSkipInheritanceComputation
  208. );
  209. VOID
  210. MartaInitializeSetContext(
  211. IN SE_OBJECT_TYPE ObjectType,
  212. OUT PMARTA_SET_FUNCTION_CONTEXT pFunctionContext
  213. );
  214. DWORD
  215. AccRewriteGetHandleRights(
  216. IN HANDLE Handle,
  217. IN SE_OBJECT_TYPE ObjectType,
  218. IN SECURITY_INFORMATION SecurityInfo,
  219. OUT PSID * ppSidOwner,
  220. OUT PSID * ppSidGroup,
  221. OUT PACL * ppDacl,
  222. OUT PACL * ppSacl,
  223. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  224. );
  225. DWORD
  226. AccRewriteGetNamedRights(
  227. IN LPWSTR pObjectName,
  228. IN SE_OBJECT_TYPE ObjectType,
  229. IN SECURITY_INFORMATION SecurityInfo,
  230. OUT PSID * ppSidOwner,
  231. OUT PSID * ppSidGroup,
  232. OUT PACL * ppDacl,
  233. OUT PACL * ppSacl,
  234. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  235. );
  236. DWORD
  237. MartaGetRightsFromContext(
  238. IN MARTA_CONTEXT Context,
  239. IN PMARTA_GET_FUNCTION_CONTEXT pGetFunctionContext,
  240. IN SECURITY_INFORMATION SecurityInfo,
  241. OUT PSID * ppSidOwner,
  242. OUT PSID * ppSidGroup,
  243. OUT PACL * ppDacl,
  244. OUT PACL * ppSacl,
  245. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  246. );
  247. VOID
  248. MartaGetSidsAndAclsFromSD(
  249. IN SECURITY_INFORMATION SecurityInfo,
  250. IN PSECURITY_DESCRIPTOR pSD,
  251. OUT PSID * ppSidOwner,
  252. OUT PSID * ppSidGroup,
  253. OUT PACL * ppDacl,
  254. OUT PACL * ppSacl,
  255. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  256. );
  257. BOOL
  258. MartaIsSDNT5Style(
  259. IN PSECURITY_DESCRIPTOR SD
  260. );
  261. BOOL
  262. MartaIsAclNt5Style(
  263. PACL pAcl
  264. );
  265. ////////////////////////////////////////////////////////////////////////////////
  266. // //
  267. // FUNCTIONS START HERE //
  268. // //
  269. ////////////////////////////////////////////////////////////////////////////////
  270. ////////////////////////////////////////////////////////////////////////////////
  271. // //
  272. // Function: MartaIsSDNT5Style //
  273. // //
  274. // Description: Determine if the Security Descriptor is NT5 style. //
  275. // //
  276. // Arguments: //
  277. // //
  278. // [IN pSD] Security Descriptor //
  279. // //
  280. // Returns: TRUE if any of the following is true //
  281. // Presence of Protected/AutoInherited in the contol bits of SD //
  282. // Presence of INHERITED_ACE flag in DACL/SACL //
  283. // //
  284. ////////////////////////////////////////////////////////////////////////////////
  285. BOOL
  286. MartaIsSDNT5Style(
  287. IN PSECURITY_DESCRIPTOR pSD
  288. )
  289. {
  290. BOOL bRetval = TRUE;
  291. PACL pAcl = NULL;
  292. PISECURITY_DESCRIPTOR pISD = (PISECURITY_DESCRIPTOR) pSD;
  293. if (MARTA_NT5_FLAGS_ON(pISD->Control))
  294. {
  295. return TRUE;
  296. }
  297. pAcl = RtlpDaclAddrSecurityDescriptor(pISD);
  298. if (NULL != pAcl)
  299. {
  300. bRetval = FALSE;
  301. if (MartaIsAclNt5Style(pAcl))
  302. {
  303. return TRUE;
  304. }
  305. }
  306. pAcl = RtlpSaclAddrSecurityDescriptor(pISD);
  307. if (NULL != pAcl)
  308. {
  309. bRetval = FALSE;
  310. if (MartaIsAclNt5Style(pAcl))
  311. {
  312. return TRUE;
  313. }
  314. }
  315. return bRetval;
  316. }
  317. ////////////////////////////////////////////////////////////////////////////////
  318. // //
  319. // Function: MartaIsAclNT5Style //
  320. // //
  321. // Description: Determine if the Acl is NT5 style. //
  322. // //
  323. // Arguments: //
  324. // //
  325. // [IN pAcl] ACL //
  326. // //
  327. // Returns: TRUE if INHERITED_ACE flags exists in the AceFlags //
  328. // FALSE otherwise //
  329. ////////////////////////////////////////////////////////////////////////////////
  330. BOOL
  331. MartaIsAclNt5Style(
  332. PACL pAcl
  333. )
  334. {
  335. ULONG i = 0;
  336. PACE_HEADER pAce = (PACE_HEADER) FirstAce(pAcl);
  337. for (; i < pAcl->AceCount; i++, pAce = (PACE_HEADER) NextAce(pAce))
  338. {
  339. if (FLAG_ON(pAce->AceFlags, INHERITED_ACE))
  340. {
  341. return TRUE;
  342. }
  343. }
  344. return FALSE;
  345. }
  346. ////////////////////////////////////////////////////////////////////////////////
  347. // //
  348. // Function: MartaInitializeGetContext //
  349. // //
  350. // Description: Initializes the function pointers based on object-type. //
  351. // //
  352. // Arguments: //
  353. // //
  354. // [IN ObjectType] Type of the object //
  355. // [OUT pFunctionContext] Structure to hold function pointers //
  356. // //
  357. ////////////////////////////////////////////////////////////////////////////////
  358. VOID
  359. MartaInitializeGetContext(
  360. IN SE_OBJECT_TYPE ObjectType,
  361. OUT PMARTA_GET_FUNCTION_CONTEXT pFunctionContext
  362. )
  363. {
  364. pFunctionContext->fAddRefContext = MartaAddRefContext[ObjectType];
  365. pFunctionContext->fCloseContext = MartaCloseContext[ObjectType];
  366. pFunctionContext->fOpenNamedObject = MartaOpenNamedObject[ObjectType];
  367. pFunctionContext->fOpenHandleObject = MartaOpenHandleObject[ObjectType];
  368. pFunctionContext->fGetRights = MartaGetRights[ObjectType];
  369. pFunctionContext->fGetDesiredAccess = MartaGetDesiredAccess[ObjectType];
  370. pFunctionContext->fGetParentContext = MartaGetParentContext[ObjectType];
  371. pFunctionContext->fGetTypeProperties = MartaGetTypeProperties[ObjectType];
  372. pFunctionContext->fGetProperties = MartaGetProperties[ObjectType];
  373. }
  374. ////////////////////////////////////////////////////////////////////////////////
  375. // //
  376. // Function: MartaGetSidsAndAclsFromSD //
  377. // //
  378. // Description: Fill in the fields requested by GetSecurity API. //
  379. // //
  380. // Arguments: //
  381. // //
  382. // [IN SecurityInfo] Security Information requested //
  383. // [IN pSD] Security Descriptor from which the out //
  384. // fields will be returned //
  385. // //
  386. // [OUT ppSIdOwner] To return the owner //
  387. // [OUT ppSidGroup] To return the group //
  388. // [OUT ppDacl] To return the Dacl //
  389. // [OUT ppSacl] To return the Sacl //
  390. // [OUT ppSecurityDescriptor] To return the Security Descriptor //
  391. // //
  392. ////////////////////////////////////////////////////////////////////////////////
  393. VOID
  394. MartaGetSidsAndAclsFromSD(
  395. IN SECURITY_INFORMATION SecurityInfo,
  396. IN PSECURITY_DESCRIPTOR pSD,
  397. OUT PSID * ppSidOwner,
  398. OUT PSID * ppSidGroup,
  399. OUT PACL * ppDacl,
  400. OUT PACL * ppSacl,
  401. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  402. )
  403. {
  404. PISECURITY_DESCRIPTOR pISD = (PISECURITY_DESCRIPTOR) pSD;
  405. if (FLAG_ON(SecurityInfo, OWNER_SECURITY_INFORMATION) && (NULL != ppSidOwner))
  406. {
  407. *ppSidOwner = RtlpOwnerAddrSecurityDescriptor(pISD);
  408. }
  409. if (FLAG_ON(SecurityInfo, GROUP_SECURITY_INFORMATION) && (NULL != ppSidGroup))
  410. {
  411. *ppSidGroup = RtlpGroupAddrSecurityDescriptor(pISD);
  412. }
  413. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION) && (NULL != ppDacl))
  414. {
  415. *ppDacl = RtlpDaclAddrSecurityDescriptor(pISD);
  416. }
  417. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION) && (ppSacl != NULL))
  418. {
  419. *ppSacl = RtlpSaclAddrSecurityDescriptor(pISD);
  420. }
  421. if (NULL != ppSecurityDescriptor)
  422. {
  423. *ppSecurityDescriptor = pSD;
  424. }
  425. }
  426. ////////////////////////////////////////////////////////////////////////////////
  427. // //
  428. // Function: MartaGetRightsFromContext //
  429. // //
  430. // Description: Get the security information requested given the Context. //
  431. // //
  432. // Arguments: //
  433. // //
  434. // [IN Context] Context structure for the object //
  435. // [IN pGetFunctionContext] Structure holding the function pointers //
  436. // [IN SecurityInfo] Security Information requested //
  437. // //
  438. // [OUT ppSIdOwner] To return the owner //
  439. // [OUT ppSidGroup] To return the group //
  440. // [OUT ppDacl] To return the Dacl //
  441. // [OUT ppSacl] To return the Sacl //
  442. // [OUT ppSecurityDescriptor] To return the Security Descriptor //
  443. // //
  444. ////////////////////////////////////////////////////////////////////////////////
  445. DWORD
  446. MartaGetRightsFromContext(
  447. IN MARTA_CONTEXT Context,
  448. IN PMARTA_GET_FUNCTION_CONTEXT pGetFunctionContext,
  449. IN SECURITY_INFORMATION SecurityInfo,
  450. OUT PSID * ppSidOwner,
  451. OUT PSID * ppSidGroup,
  452. OUT PACL * ppDacl,
  453. OUT PACL * ppSacl,
  454. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  455. )
  456. {
  457. DWORD dwErr = ERROR_SUCCESS;
  458. PSECURITY_DESCRIPTOR pSD = NULL;
  459. PSECURITY_DESCRIPTOR pParentSD = NULL;
  460. HANDLE ProcessHandle = NULL;
  461. GENERIC_MAPPING ZeroGenMap = {0, 0, 0, 0};
  462. MARTA_CONTEXT ParentContext = NULL_MARTA_CONTEXT;
  463. BOOL bIsContainer = FALSE;
  464. MARTA_OBJECT_PROPERTIES ObjectProperties;
  465. MARTA_OBJECT_TYPE_PROPERTIES ObjectTypeProperties;
  466. dwErr = (*(pGetFunctionContext->fGetRights))(
  467. Context,
  468. SecurityInfo,
  469. &pSD
  470. );
  471. CONDITIONAL_RETURN(dwErr);
  472. if (NULL == pSD)
  473. {
  474. goto End;
  475. }
  476. if (!FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION))
  477. {
  478. goto GetResults;
  479. }
  480. //
  481. // If the SD is NT4 && acl requested is not PROTECTED && ManualPropagation
  482. // is required then
  483. // Get the ParentSD and convert the SD into NT5 style
  484. // else
  485. // Goto GetResults
  486. //
  487. if (!MARTA_SD_NOT_PROTECTED((PISECURITY_DESCRIPTOR) pSD, SecurityInfo))
  488. {
  489. goto GetResults;
  490. }
  491. if (MartaIsSDNT5Style(pSD))
  492. {
  493. goto GetResults;
  494. }
  495. //
  496. // Get the "Type" properties for the object,
  497. //
  498. ObjectTypeProperties.cbSize = sizeof(ObjectTypeProperties);
  499. ObjectTypeProperties.dwFlags = 0;
  500. ObjectTypeProperties.GenMap = ZeroGenMap;
  501. dwErr = (*(pGetFunctionContext->fGetTypeProperties))(&ObjectTypeProperties);
  502. CONDITIONAL_EXIT(dwErr, End);
  503. if (!FLAG_ON(ObjectTypeProperties.dwFlags, MARTA_OBJECT_TYPE_MANUAL_PROPAGATION_NEEDED_FLAG))
  504. {
  505. goto GetResults;
  506. }
  507. dwErr = (*(pGetFunctionContext->fGetParentContext))(
  508. Context,
  509. (*(pGetFunctionContext->fGetDesiredAccess))(READ_ACCESS_RIGHTS, FALSE, SecurityInfo),
  510. &ParentContext
  511. );
  512. CONDITIONAL_EXIT(dwErr, End);
  513. //
  514. // The SD is NT4 style. Read the parent SD to determine whether the aces are
  515. // the "same" on both the parent and the child.
  516. //
  517. if (NULL == ParentContext)
  518. {
  519. goto GetResults;
  520. }
  521. dwErr = (*(pGetFunctionContext->fGetRights))(
  522. ParentContext,
  523. SecurityInfo,
  524. &pParentSD
  525. );
  526. (VOID) (*(pGetFunctionContext->fCloseContext))(ParentContext);
  527. CONDITIONAL_EXIT(dwErr, End);
  528. if (NULL == pParentSD)
  529. {
  530. goto GetResults;
  531. }
  532. dwErr = GetCurrentToken(&ProcessHandle);
  533. CONDITIONAL_EXIT(dwErr, End);
  534. if (!((FLAG_ON(SecurityInfo, OWNER_SECURITY_INFORMATION)) &&
  535. (FLAG_ON(SecurityInfo, GROUP_SECURITY_INFORMATION))))
  536. {
  537. AccFree(pSD);
  538. pSD = NULL;
  539. dwErr = (*(pGetFunctionContext->fGetRights))(
  540. Context,
  541. (SecurityInfo | OWNER_SECURITY_INFORMATION |
  542. GROUP_SECURITY_INFORMATION),
  543. &pSD
  544. );
  545. CONDITIONAL_EXIT(dwErr, End);
  546. }
  547. ObjectProperties.cbSize = sizeof(ObjectProperties);
  548. ObjectProperties.dwFlags = 0;
  549. dwErr = (*(pGetFunctionContext->fGetProperties))(
  550. Context,
  551. &ObjectProperties
  552. );
  553. CONDITIONAL_EXIT(dwErr, End);
  554. bIsContainer = FLAG_ON(ObjectProperties.dwFlags, MARTA_OBJECT_IS_CONTAINER);
  555. dwErr = MartaGetNT4NodeSD(
  556. pParentSD,
  557. pSD,
  558. ProcessHandle,
  559. bIsContainer,
  560. &(ObjectTypeProperties.GenMap),
  561. SecurityInfo
  562. );
  563. CONDITIONAL_EXIT(dwErr, End);
  564. GetResults:
  565. MartaGetSidsAndAclsFromSD(
  566. SecurityInfo,
  567. pSD,
  568. ppSidOwner,
  569. ppSidGroup,
  570. ppDacl,
  571. ppSacl,
  572. ppSecurityDescriptor
  573. );
  574. End:
  575. if (NULL != pParentSD)
  576. {
  577. AccFree(pParentSD);
  578. }
  579. if (NULL != ProcessHandle)
  580. {
  581. CloseHandle(ProcessHandle);
  582. }
  583. if (ERROR_SUCCESS != dwErr)
  584. {
  585. AccFree(pSD);
  586. }
  587. return dwErr;
  588. }
  589. ////////////////////////////////////////////////////////////////////////////////
  590. // //
  591. // Function: AccRewriteGetNamedRights //
  592. // //
  593. // Description: Get the security information requested given the object //
  594. // name and information. This is the routine that is called by //
  595. // advapi32. //
  596. // //
  597. // Arguments: //
  598. // //
  599. // [IN pObjectName] Name of the Object //
  600. // [IN ObjectType] Type of the object //
  601. // [IN SecurityInfo] Security Information requested //
  602. // //
  603. // [OUT ppSIdOwner] To return the owner //
  604. // [OUT ppSidGroup] To return the group //
  605. // [OUT ppDacl] To return the Dacl //
  606. // [OUT ppSacl] To return the Sacl //
  607. // [OUT ppSecurityDescriptor] To return the Security Descriptor //
  608. // //
  609. ////////////////////////////////////////////////////////////////////////////////
  610. DWORD
  611. AccRewriteGetNamedRights(
  612. IN LPWSTR pObjectName,
  613. IN SE_OBJECT_TYPE ObjectType,
  614. IN SECURITY_INFORMATION SecurityInfo,
  615. OUT PSID * ppSidOwner,
  616. OUT PSID * ppSidGroup,
  617. OUT PACL * ppDacl,
  618. OUT PACL * ppSacl,
  619. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  620. )
  621. {
  622. DWORD dwErr = ERROR_SUCCESS;
  623. MARTA_CONTEXT Context = NULL_MARTA_CONTEXT;
  624. MARTA_GET_FUNCTION_CONTEXT MartaGetFunctionContext;
  625. switch (ObjectType)
  626. {
  627. case SE_FILE_OBJECT:
  628. case SE_SERVICE:
  629. case SE_PRINTER:
  630. case SE_REGISTRY_KEY:
  631. case SE_REGISTRY_WOW64_32KEY:
  632. case SE_LMSHARE:
  633. case SE_KERNEL_OBJECT:
  634. case SE_WINDOW_OBJECT:
  635. case SE_WMIGUID_OBJECT:
  636. case SE_DS_OBJECT:
  637. case SE_DS_OBJECT_ALL:
  638. break;
  639. case SE_PROVIDER_DEFINED_OBJECT:
  640. case SE_UNKNOWN_OBJECT_TYPE:
  641. default:
  642. return ERROR_INVALID_PARAMETER;
  643. }
  644. MartaInitializeGetContext(ObjectType, &MartaGetFunctionContext);
  645. //
  646. // Open the object with permissions to read the object type as well. If that
  647. // fails, open the object with just read permissions. This has to be done in
  648. // order to accomodate NT4 SDs.
  649. //
  650. dwErr = (*(MartaGetFunctionContext.fOpenNamedObject))(
  651. pObjectName,
  652. (*(MartaGetFunctionContext.fGetDesiredAccess))(READ_ACCESS_RIGHTS, TRUE, SecurityInfo),
  653. &Context
  654. );
  655. if (ERROR_SUCCESS != dwErr)
  656. {
  657. dwErr = (*(MartaGetFunctionContext.fOpenNamedObject))(
  658. pObjectName,
  659. (*(MartaGetFunctionContext.fGetDesiredAccess))(READ_ACCESS_RIGHTS, FALSE, SecurityInfo),
  660. &Context
  661. );
  662. CONDITIONAL_EXIT(dwErr, End);
  663. }
  664. dwErr = MartaGetRightsFromContext(
  665. Context,
  666. &MartaGetFunctionContext,
  667. SecurityInfo,
  668. ppSidOwner,
  669. ppSidGroup,
  670. ppDacl,
  671. ppSacl,
  672. ppSecurityDescriptor
  673. );
  674. (VOID) (*(MartaGetFunctionContext.fCloseContext))(Context);
  675. End:
  676. return dwErr;
  677. }
  678. ////////////////////////////////////////////////////////////////////////////////
  679. // //
  680. // Function: AccRewriteGetHandleRights //
  681. // //
  682. // Description: Get the security information requested given the object //
  683. // handle and information. This is the routine that is called by //
  684. // advapi32. //
  685. // //
  686. // Arguments: //
  687. // //
  688. // [IN Handle] Handle to the Object //
  689. // [IN pGetFunctionContext] Structure holding the function pointers //
  690. // [IN SecurityInfo] Security Information requested //
  691. // //
  692. // [OUT ppSIdOwner] To return the owner //
  693. // [OUT ppSidGroup] To return the group //
  694. // [OUT ppDacl] To return the Dacl //
  695. // [OUT ppSacl] To return the Sacl //
  696. // [OUT ppSecurityDescriptor] To return the Security Descriptor //
  697. // //
  698. ////////////////////////////////////////////////////////////////////////////////
  699. DWORD
  700. AccRewriteGetHandleRights(
  701. IN HANDLE Handle,
  702. IN SE_OBJECT_TYPE ObjectType,
  703. IN SECURITY_INFORMATION SecurityInfo,
  704. OUT PSID * ppSidOwner,
  705. OUT PSID * ppSidGroup,
  706. OUT PACL * ppDacl,
  707. OUT PACL * ppSacl,
  708. OUT PSECURITY_DESCRIPTOR * ppSecurityDescriptor
  709. )
  710. {
  711. DWORD dwErr = ERROR_SUCCESS;
  712. MARTA_CONTEXT Context = NULL_MARTA_CONTEXT;
  713. MARTA_GET_FUNCTION_CONTEXT MartaGetFunctionContext;
  714. switch (ObjectType)
  715. {
  716. case SE_FILE_OBJECT:
  717. case SE_SERVICE:
  718. case SE_PRINTER:
  719. case SE_REGISTRY_KEY:
  720. case SE_LMSHARE:
  721. case SE_KERNEL_OBJECT:
  722. case SE_WINDOW_OBJECT:
  723. case SE_WMIGUID_OBJECT:
  724. break;
  725. case SE_DS_OBJECT:
  726. case SE_DS_OBJECT_ALL:
  727. case SE_PROVIDER_DEFINED_OBJECT:
  728. case SE_UNKNOWN_OBJECT_TYPE:
  729. default:
  730. return ERROR_INVALID_PARAMETER;
  731. }
  732. MartaInitializeGetContext(ObjectType, &MartaGetFunctionContext);
  733. //
  734. // Open the object with permissions to read the object type as well. If that
  735. // fails, open the object with just read permissions. This has to be done in
  736. // order to accomodate NT4 SDs.
  737. //
  738. dwErr = (*(MartaGetFunctionContext.fOpenHandleObject))(
  739. Handle,
  740. (*(MartaGetFunctionContext.fGetDesiredAccess))(READ_ACCESS_RIGHTS, TRUE, SecurityInfo),
  741. &Context
  742. );
  743. if (ERROR_SUCCESS != dwErr)
  744. {
  745. dwErr = (*(MartaGetFunctionContext.fOpenHandleObject))(
  746. Handle,
  747. (*(MartaGetFunctionContext.fGetDesiredAccess))(READ_ACCESS_RIGHTS, FALSE, SecurityInfo),
  748. &Context
  749. );
  750. CONDITIONAL_EXIT(dwErr, End);
  751. }
  752. dwErr = MartaGetRightsFromContext(
  753. Context,
  754. &MartaGetFunctionContext,
  755. SecurityInfo,
  756. ppSidOwner,
  757. ppSidGroup,
  758. ppDacl,
  759. ppSacl,
  760. ppSecurityDescriptor
  761. );
  762. (VOID) (*(MartaGetFunctionContext.fCloseContext))(Context);
  763. End:
  764. return dwErr;
  765. }
  766. ////////////////////////////////////////////////////////////////////////////////
  767. // //
  768. // Function: MartaInitializeSetContext //
  769. // //
  770. // Description: Initializes the function pointers based on object-type. //
  771. // //
  772. // Arguments: //
  773. // //
  774. // [IN ObjectType] Type of the object //
  775. // [OUT pFunctionContext] Structure to hold function pointers //
  776. // //
  777. ////////////////////////////////////////////////////////////////////////////////
  778. VOID
  779. MartaInitializeSetContext(
  780. IN SE_OBJECT_TYPE ObjectType,
  781. OUT PMARTA_SET_FUNCTION_CONTEXT pFunctionContext
  782. )
  783. {
  784. pFunctionContext->fAddRefContext = MartaAddRefContext[ObjectType];
  785. pFunctionContext->fCloseContext = MartaCloseContext[ObjectType];
  786. pFunctionContext->fFindFirst = MartaFindFirst[ObjectType];
  787. pFunctionContext->fFindNext = MartaFindNext[ObjectType];
  788. pFunctionContext->fGetParentContext = MartaGetParentContext[ObjectType];
  789. pFunctionContext->fGetProperties = MartaGetProperties[ObjectType];
  790. pFunctionContext->fGetTypeProperties = MartaGetTypeProperties[ObjectType];
  791. pFunctionContext->fGetRights = MartaGetRights[ObjectType];
  792. pFunctionContext->fOpenNamedObject = MartaOpenNamedObject[ObjectType];
  793. pFunctionContext->fOpenHandleObject = MartaOpenHandleObject[ObjectType];
  794. pFunctionContext->fSetRights = MartaSetRights[ObjectType];
  795. pFunctionContext->fGetDesiredAccess = MartaGetDesiredAccess[ObjectType];
  796. pFunctionContext->fReopenContext = MartaReopenContext[ObjectType];
  797. pFunctionContext->fReopenOrigContext = MartaReopenOrigContext[ObjectType];
  798. pFunctionContext->fGetNameFromContext = MartaGetNameFromContext[ObjectType];
  799. }
  800. ////////////////////////////////////////////////////////////////////////////////
  801. // //
  802. // Function: MartaManualPropagation //
  803. // //
  804. // Description: Stamp the security descriptor on the object referred by the //
  805. // context and propagate the inheritable aces to its children. //
  806. // //
  807. // Arguments: //
  808. // //
  809. // [IN Context] Context structure for the object //
  810. // [IN SecurityInfo] Security Information requested //
  811. // [IN OUT pSD] Security Descriptor to be stamped on the //
  812. // object in absolute format. //
  813. // [IN pGenMap] Generic mapping of the object rights //
  814. // [IN bDoPropagate] Whether propagation _can_ be done //
  815. // [IN bReadOldProtectedBits] Whether to read existing protection info //
  816. // [IN pSetFunctionContext] Structure holding the function pointers //
  817. // [IN bSkipInheritanceComputation] Whether to compute inherited aces //
  818. // from the parent
  819. // //
  820. ////////////////////////////////////////////////////////////////////////////////
  821. DWORD
  822. MartaManualPropagation(
  823. IN MARTA_CONTEXT Context,
  824. IN SECURITY_INFORMATION SecurityInfo,
  825. IN OUT PSECURITY_DESCRIPTOR pSD,
  826. IN PGENERIC_MAPPING pGenMap,
  827. IN BOOL bDoPropagate,
  828. IN BOOL bReadOldProtectedBits,
  829. IN PMARTA_SET_FUNCTION_CONTEXT pMartaSetFunctionContext,
  830. IN BOOL bSkipInheritanceComputation
  831. )
  832. {
  833. DWORD dwErr = ERROR_SUCCESS;
  834. BOOL bProtected = TRUE;
  835. BOOL bIsChildContainer = FALSE;
  836. BOOL bRetryPropagation = FALSE;
  837. PSECURITY_DESCRIPTOR pParentSD = NULL;
  838. PSECURITY_DESCRIPTOR pOldSD = NULL;
  839. PSECURITY_DESCRIPTOR pNewSD = NULL;
  840. PSID pSidOwner = NULL;
  841. HANDLE ProcessHandle = NULL;
  842. HANDLE ThreadHandle = NULL;
  843. MARTA_CONTEXT ParentContext = NULL_MARTA_CONTEXT;
  844. SECURITY_DESCRIPTOR_CONTROL LocalControl = (SECURITY_DESCRIPTOR_CONTROL) 0;
  845. MARTA_OBJECT_PROPERTIES ObjectProperties;
  846. //
  847. // Check if manual propagation should be done. Propagation is not tried if
  848. // any errors are encountered.
  849. //
  850. ObjectProperties.cbSize = sizeof(ObjectProperties);
  851. ObjectProperties.dwFlags = 0;
  852. dwErr = (*(pMartaSetFunctionContext->fGetProperties))(
  853. Context,
  854. &ObjectProperties
  855. );
  856. CONDITIONAL_EXIT(dwErr, End);
  857. bIsChildContainer = FLAG_ON(ObjectProperties.dwFlags, MARTA_OBJECT_IS_CONTAINER);
  858. dwErr = GetCurrentToken(&ProcessHandle);
  859. CONDITIONAL_EXIT(dwErr, End);
  860. //
  861. // Compute inherited aces if the caller has not already. This is the usual
  862. // case.
  863. //
  864. if (FALSE == bSkipInheritanceComputation)
  865. {
  866. //
  867. // Read the parent ACL only if xACL is to be stamped on is not protected.
  868. //
  869. if (MARTA_SD_NOT_PROTECTED((PISECURITY_DESCRIPTOR) pSD, SecurityInfo))
  870. {
  871. bProtected = FALSE;
  872. dwErr = (*(pMartaSetFunctionContext->fGetParentContext))(
  873. Context,
  874. (*(pMartaSetFunctionContext->fGetDesiredAccess))(READ_ACCESS_RIGHTS, FALSE, SecurityInfo),
  875. &ParentContext
  876. );
  877. CONDITIONAL_EXIT(dwErr, End);
  878. if (NULL != ParentContext)
  879. {
  880. dwErr = (*(pMartaSetFunctionContext->fGetRights))(
  881. ParentContext,
  882. SecurityInfo,
  883. &pParentSD
  884. );
  885. (VOID) (*(pMartaSetFunctionContext->fCloseContext))(ParentContext);
  886. CONDITIONAL_EXIT(dwErr, End);
  887. if (NULL != pParentSD)
  888. {
  889. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  890. {
  891. ((PISECURITY_DESCRIPTOR) pParentSD)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  892. }
  893. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  894. {
  895. ((PISECURITY_DESCRIPTOR) pParentSD)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  896. }
  897. }
  898. }
  899. }
  900. //
  901. // Read the old security descriptor on the child if xAcl is not protected
  902. // and is to be stamped on the child.
  903. // To take case of creator-owner/group aces, read in Owner/Group info as
  904. // well and set it in the SD passed in if it is not already present.
  905. //
  906. if (FALSE == bProtected)
  907. {
  908. SECURITY_INFORMATION LocalSeInfo = SecurityInfo;
  909. if (NULL == RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pSD))
  910. {
  911. LocalSeInfo |= OWNER_SECURITY_INFORMATION;
  912. }
  913. if (NULL == RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pSD))
  914. {
  915. LocalSeInfo |= GROUP_SECURITY_INFORMATION;
  916. }
  917. dwErr = (*(pMartaSetFunctionContext->fGetRights))(
  918. Context,
  919. LocalSeInfo,
  920. &pOldSD
  921. );
  922. CONDITIONAL_EXIT(dwErr, End);
  923. if (NULL == RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pSD))
  924. {
  925. if (FALSE == SetSecurityDescriptorOwner(
  926. pSD,
  927. RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldSD),
  928. FALSE))
  929. {
  930. dwErr = GetLastError();
  931. }
  932. CONDITIONAL_EXIT(dwErr, End);
  933. }
  934. if (NULL == RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pSD))
  935. {
  936. if (FALSE == SetSecurityDescriptorGroup(
  937. pSD,
  938. RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldSD),
  939. FALSE))
  940. {
  941. dwErr = GetLastError();
  942. }
  943. CONDITIONAL_EXIT(dwErr, End);
  944. }
  945. }
  946. //
  947. // Read the old security descriptor on the child if xAcl it is a container.
  948. //
  949. else if (bIsChildContainer || bReadOldProtectedBits)
  950. {
  951. dwErr = (*(pMartaSetFunctionContext->fGetRights))(
  952. Context,
  953. SecurityInfo,
  954. &pOldSD
  955. );
  956. CONDITIONAL_EXIT(dwErr, End);
  957. }
  958. //
  959. // If none of the PROTECTED flags are passed in then do the "right" thing.
  960. // Read the PROTECTED bit from the existing security descriptor and set it
  961. // in the new one.
  962. //
  963. if (bReadOldProtectedBits && (NULL != pOldSD))
  964. {
  965. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  966. {
  967. if (!FLAG_ON(SecurityInfo, (PROTECTED_DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION)))
  968. {
  969. ((PISECURITY_DESCRIPTOR) pSD)->Control |= ((PISECURITY_DESCRIPTOR) pOldSD)->Control & SE_DACL_PROTECTED;
  970. }
  971. }
  972. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  973. {
  974. if (!FLAG_ON(SecurityInfo, (PROTECTED_SACL_SECURITY_INFORMATION | UNPROTECTED_SACL_SECURITY_INFORMATION)))
  975. {
  976. ((PISECURITY_DESCRIPTOR) pSD)->Control |= ((PISECURITY_DESCRIPTOR) pOldSD)->Control & SE_SACL_PROTECTED;
  977. }
  978. }
  979. }
  980. //
  981. // Merge the SD with the parent SD whether or not it is protected.
  982. // This is done to lose the inherited aces if the child is protected.
  983. //
  984. MARTA_TURN_OFF_IMPERSONATION;
  985. if (FALSE == CreatePrivateObjectSecurityEx(
  986. pParentSD,
  987. pSD,
  988. &pNewSD,
  989. NULL,
  990. bIsChildContainer,
  991. (SEF_DACL_AUTO_INHERIT | SEF_SACL_AUTO_INHERIT | SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK),
  992. ProcessHandle,
  993. pGenMap
  994. ))
  995. {
  996. dwErr = GetLastError();
  997. }
  998. MARTA_TURN_ON_IMPERSONATION;
  999. CONDITIONAL_EXIT(dwErr, End);
  1000. }
  1001. else
  1002. {
  1003. //
  1004. // Stamp the security descriptor as prvided by the caller. The only
  1005. // caller of this is SCE.
  1006. //
  1007. pNewSD = pSD;
  1008. //
  1009. // Read the old security descriptor on the child if xAcl it is a container.
  1010. //
  1011. if (bIsChildContainer)
  1012. {
  1013. dwErr = (*(pMartaSetFunctionContext->fGetRights))(
  1014. Context,
  1015. (SecurityInfo & (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)),
  1016. &pOldSD
  1017. );
  1018. CONDITIONAL_EXIT(dwErr, End);
  1019. }
  1020. }
  1021. //
  1022. // If the child is a container then update the subtree underneath it.
  1023. //
  1024. if (bIsChildContainer)
  1025. {
  1026. if (bDoPropagate)
  1027. {
  1028. bRetryPropagation = MartaUpdateTree(
  1029. SecurityInfo,
  1030. pNewSD,
  1031. pOldSD,
  1032. Context,
  1033. ProcessHandle,
  1034. pMartaSetFunctionContext,
  1035. pGenMap
  1036. );
  1037. }
  1038. else
  1039. {
  1040. bRetryPropagation = TRUE;
  1041. }
  1042. }
  1043. //
  1044. // Stamp NewNodeSD on the node
  1045. //
  1046. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  1047. {
  1048. ((PISECURITY_DESCRIPTOR) pNewSD)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  1049. }
  1050. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  1051. {
  1052. ((PISECURITY_DESCRIPTOR) pNewSD)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  1053. }
  1054. dwErr = (*(pMartaSetFunctionContext->fSetRights))(
  1055. Context,
  1056. SecurityInfo,
  1057. pNewSD
  1058. );
  1059. CONDITIONAL_EXIT(dwErr, End);
  1060. //
  1061. // If propagation had failed in the first attept then try again. This is to
  1062. // cover the case when the container can be enumerated after setting the new
  1063. // security.
  1064. if (bRetryPropagation && (SecurityInfo & DACL_SECURITY_INFORMATION))
  1065. {
  1066. ACCESS_MASK Access = (*(pMartaSetFunctionContext->fGetDesiredAccess))(
  1067. NO_ACCESS_RIGHTS,
  1068. TRUE,
  1069. SecurityInfo & (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)
  1070. );
  1071. DWORD lErr = (*(pMartaSetFunctionContext->fReopenOrigContext))(
  1072. Context,
  1073. Access
  1074. );
  1075. CONDITIONAL_EXIT(lErr, End);
  1076. (VOID) MartaUpdateTree(
  1077. SecurityInfo,
  1078. pNewSD,
  1079. pOldSD,
  1080. Context,
  1081. ProcessHandle,
  1082. pMartaSetFunctionContext,
  1083. pGenMap
  1084. );
  1085. }
  1086. End:
  1087. if (NULL != ProcessHandle)
  1088. {
  1089. CloseHandle(ProcessHandle);
  1090. }
  1091. if (NULL != pOldSD)
  1092. {
  1093. AccFree(pOldSD);
  1094. }
  1095. if (NULL != pParentSD)
  1096. {
  1097. AccFree(pParentSD);
  1098. }
  1099. if ((NULL != pNewSD) && (pNewSD != pSD))
  1100. {
  1101. DestroyPrivateObjectSecurity(&pNewSD);
  1102. }
  1103. return dwErr;
  1104. }
  1105. ////////////////////////////////////////////////////////////////////////////////
  1106. // //
  1107. // Function: MartaEqualAce //
  1108. // //
  1109. // Description: Compare an ace from child to an ace from parent to determine //
  1110. // if the child ace has been inherited from its parent. //
  1111. // //
  1112. // Arguments: //
  1113. // //
  1114. // [IN pParentAce] The ace for the parent object //
  1115. // [IN pChildAce] The ace for the child object //
  1116. // [IN bIsChildContainer] Whether the child object is a Container //
  1117. // //
  1118. // Returns: TRUE if the two aces are equal //
  1119. // FALSE otherwise //
  1120. // //
  1121. // Notes: No ace should contain generic bits and the parent ace should not //
  1122. // have INHERIT_ONLY bit. //
  1123. // Inherit flags are ignored. //
  1124. // //
  1125. ////////////////////////////////////////////////////////////////////////////////
  1126. BOOL
  1127. MartaEqualAce(
  1128. IN PACE_HEADER pParentAce,
  1129. IN PACE_HEADER pChildAce,
  1130. IN BOOL bIsChildContainer
  1131. )
  1132. {
  1133. PSID pSid1 = NULL;
  1134. PSID pSid2 = NULL;
  1135. ACCESS_MASK Access1 = 0;
  1136. ACCESS_MASK Access2 = 0;
  1137. ULONG Length1 = 0;
  1138. ULONG Length2 = 0;
  1139. if ((NULL == pParentAce) || (NULL == pChildAce))
  1140. {
  1141. return FALSE;
  1142. }
  1143. //
  1144. // Compare ACE type.
  1145. //
  1146. if (pParentAce->AceType != pChildAce->AceType)
  1147. {
  1148. return FALSE;
  1149. }
  1150. if ((pParentAce->AceFlags & ~INHERITED_ACE) != (pChildAce->AceFlags))
  1151. {
  1152. return FALSE;
  1153. }
  1154. if (bIsChildContainer)
  1155. {
  1156. //
  1157. // Note: the flag shouldn't be compared because
  1158. // it will be different even for the "equal" ace
  1159. //
  1160. // then we have a bug here, for example
  1161. // parentSD is Admin CI F
  1162. // childSD is Admin CIOI F
  1163. // these two SDs should be marked as "different" but from
  1164. // this routine, it will mark them "equal".
  1165. //
  1166. }
  1167. //
  1168. // Get access mask and SID pointer.
  1169. //
  1170. switch (pParentAce->AceType) {
  1171. case ACCESS_ALLOWED_ACE_TYPE:
  1172. case ACCESS_DENIED_ACE_TYPE:
  1173. case SYSTEM_AUDIT_ACE_TYPE:
  1174. case SYSTEM_ALARM_ACE_TYPE:
  1175. pSid1 = (PSID) &((PKNOWN_ACE) pParentAce)->SidStart;
  1176. pSid2 = (PSID) &((PKNOWN_ACE) pChildAce)->SidStart;
  1177. Access1 = ((PKNOWN_ACE) pParentAce)->Mask;
  1178. Access2 = ((PKNOWN_ACE) pChildAce)->Mask;
  1179. break;
  1180. case ACCESS_ALLOWED_OBJECT_ACE_TYPE:
  1181. case ACCESS_DENIED_OBJECT_ACE_TYPE:
  1182. case SYSTEM_AUDIT_OBJECT_ACE_TYPE:
  1183. case SYSTEM_ALARM_OBJECT_ACE_TYPE:
  1184. if (((PKNOWN_OBJECT_ACE) pParentAce)->Flags !=
  1185. ((PKNOWN_OBJECT_ACE) pChildAce)->Flags )
  1186. {
  1187. return FALSE;
  1188. }
  1189. if (((PKNOWN_OBJECT_ACE) pParentAce)->Flags & ACE_OBJECT_TYPE_PRESENT)
  1190. {
  1191. if (!RtlpIsEqualGuid(
  1192. RtlObjectAceObjectType(pParentAce),
  1193. RtlObjectAceObjectType(pChildAce)))
  1194. {
  1195. return FALSE;
  1196. }
  1197. }
  1198. if (((PKNOWN_OBJECT_ACE) pParentAce)->Flags & ACE_INHERITED_OBJECT_TYPE_PRESENT)
  1199. {
  1200. if (!RtlpIsEqualGuid(
  1201. RtlObjectAceInheritedObjectType(pParentAce),
  1202. RtlObjectAceInheritedObjectType(pChildAce)))
  1203. {
  1204. return FALSE;
  1205. }
  1206. }
  1207. pSid1 = RtlObjectAceSid(pParentAce);
  1208. pSid2 = RtlObjectAceSid(pChildAce);
  1209. Access1 = ((PKNOWN_OBJECT_ACE) pParentAce)->Mask;
  1210. Access2 = ((PKNOWN_OBJECT_ACE) pChildAce)->Mask;
  1211. break;
  1212. case ACCESS_ALLOWED_COMPOUND_ACE_TYPE:
  1213. if (((PKNOWN_COMPOUND_ACE) pParentAce)->CompoundAceType !=
  1214. ((PKNOWN_COMPOUND_ACE) pChildAce)->CompoundAceType)
  1215. {
  1216. return FALSE;
  1217. }
  1218. pSid1 = (PSID) &((PKNOWN_COMPOUND_ACE) pParentAce)->SidStart;
  1219. pSid2 = (PSID) &((PKNOWN_COMPOUND_ACE) pParentAce)->SidStart;
  1220. if ((!RtlValidSid(pSid1)) || (!RtlValidSid(pSid2)))
  1221. {
  1222. return FALSE;
  1223. }
  1224. if (!RtlEqualSid(pSid1, pSid2))
  1225. {
  1226. return FALSE;
  1227. }
  1228. Length1 = RtlLengthSid(pSid1);
  1229. Length2 = RtlLengthSid(pSid2);
  1230. pSid1 = (PSID) (((PUCHAR) pSid1) + Length1);
  1231. pSid2 = (PSID) (((PUCHAR) pSid2) + Length2);
  1232. Access1 = ((PKNOWN_COMPOUND_ACE) pParentAce)->Mask;
  1233. Access2 = ((PKNOWN_COMPOUND_ACE) pChildAce)->Mask;
  1234. break;
  1235. default:
  1236. return FALSE;
  1237. }
  1238. //
  1239. // Compare access mask. There should be no generic mask and both the parent
  1240. // object and the child object should have the same object type.
  1241. //
  1242. if (Access1 != Access2) {
  1243. return FALSE;
  1244. }
  1245. //
  1246. // Compare the Sids.
  1247. //
  1248. if ((!RtlValidSid(pSid1)) || (!RtlValidSid(pSid2)))
  1249. {
  1250. return FALSE;
  1251. }
  1252. if (!RtlEqualSid(pSid1, pSid2))
  1253. {
  1254. return FALSE;
  1255. }
  1256. return TRUE;
  1257. }
  1258. ////////////////////////////////////////////////////////////////////////////////
  1259. // //
  1260. // Function: CompareAndMarkInheritedAces //
  1261. // //
  1262. // Description: Compare the parent acl with the child. If all the effective //
  1263. // aces from parent are present in the child then mark those //
  1264. // aces in the child with INHERITED_ACE bit. //
  1265. // //
  1266. // Arguments: //
  1267. // //
  1268. // [IN pParentAcl] The acl for the parent object //
  1269. // [IN OUT pChildAcl] The acl for the child object //
  1270. // [IN bIsChildContainer] Whether the child object is a Container //
  1271. // //
  1272. // [OUT pCompareStatus] To return the Security Descriptor //
  1273. // //
  1274. // Returns: TRUE if the all effective parent aces are present in the child //
  1275. // FALSE otherwise //
  1276. // //
  1277. ////////////////////////////////////////////////////////////////////////////////
  1278. DWORD
  1279. MartaCompareAndMarkInheritedAces(
  1280. IN PACL pParentAcl,
  1281. IN OUT PACL pChildAcl,
  1282. IN BOOL bIsChildContainer,
  1283. OUT PBOOL pCompareStatus
  1284. )
  1285. {
  1286. DWORD dwErr = ERROR_SUCCESS;
  1287. LONG ParentAceCnt = 0;
  1288. LONG ChildAceCnt = 0;
  1289. LONG i = 0;
  1290. LONG j = 0;
  1291. LONG LastDenyAce = -1;
  1292. LONG LastExplicitAce = -1;
  1293. LONG LastInheritedDenyAce = -1;
  1294. LONG FirstAllowAce = ChildAceCnt;
  1295. LONG FirstInheritedAce = ChildAceCnt;
  1296. LONG FirstExplicitAllowAce = ChildAceCnt;
  1297. PACE_HEADER pParentAce = NULL;
  1298. PACE_HEADER pChildAce = NULL;
  1299. PBOOL Flags = NULL;
  1300. PUCHAR Buffer = NULL;
  1301. PUCHAR CurrentBuffer = NULL;
  1302. //
  1303. // If the ChildAcl is NULL then it is a superset of the parent ACL.
  1304. //
  1305. if (NULL == pChildAcl)
  1306. {
  1307. *pCompareStatus = FALSE;
  1308. goto End;
  1309. }
  1310. //
  1311. // If the ParentAcl is NULL then it is a superset of the child ACL.
  1312. // Since Child Acl is non-null at this point return TRUE.
  1313. //
  1314. if (NULL == pParentAcl)
  1315. {
  1316. *pCompareStatus = TRUE;
  1317. goto End;
  1318. }
  1319. //
  1320. // If the parent has no aces that could have been inherited then all the
  1321. // child aces must be explicit.
  1322. //
  1323. ParentAceCnt = pParentAcl->AceCount;
  1324. if (0 == ParentAceCnt)
  1325. {
  1326. *pCompareStatus = TRUE;
  1327. goto End;
  1328. }
  1329. //
  1330. // If the parent has one/more inheritable aces but the child has none then
  1331. // the acl must be protected.
  1332. //
  1333. ChildAceCnt = pChildAcl->AceCount;
  1334. if (0 == ChildAceCnt)
  1335. {
  1336. *pCompareStatus = FALSE;
  1337. goto End;
  1338. }
  1339. Flags = (PBOOL) AccAlloc(sizeof(BOOL) * ChildAceCnt);
  1340. if (NULL == Flags)
  1341. {
  1342. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  1343. goto End;
  1344. }
  1345. for (i = 0; i < ChildAceCnt; i++)
  1346. {
  1347. Flags[i] = FALSE;
  1348. }
  1349. //
  1350. // For all aces present in ParentAcl
  1351. // If the ace is not present in ChildAcl
  1352. // return FALSE
  1353. // else
  1354. // Mark the position of the this ace in Child Acl in fFlags.
  1355. // These will later be marked as INHERITED if the acl can be
  1356. // rearranged to be canonical.
  1357. //
  1358. i = 0;
  1359. pParentAce = (PACE_HEADER) FirstAce(pParentAcl);
  1360. for (; i < ParentAceCnt; i++, pParentAce = (PACE_HEADER) NextAce(pParentAce))
  1361. {
  1362. j = 0;
  1363. pChildAce = (PACE_HEADER) FirstAce(pChildAcl);
  1364. for (; j < ChildAceCnt; j++, pChildAce = (PACE_HEADER) NextAce(pChildAce))
  1365. {
  1366. if (TRUE == MartaEqualAce(pParentAce, pChildAce, bIsChildContainer))
  1367. {
  1368. Flags[j] = TRUE;
  1369. break;
  1370. }
  1371. }
  1372. if (ChildAceCnt == j)
  1373. {
  1374. *pCompareStatus = FALSE;
  1375. goto End;
  1376. }
  1377. }
  1378. //
  1379. // Mark all the aces that we had marked as INHERITED.
  1380. // This will make sure that they are not DUPLICATED.
  1381. //
  1382. LastDenyAce = -1;
  1383. LastExplicitAce = -1;
  1384. LastInheritedDenyAce = -1;
  1385. FirstAllowAce = ChildAceCnt;
  1386. FirstInheritedAce = ChildAceCnt;
  1387. FirstExplicitAllowAce = ChildAceCnt;
  1388. //
  1389. // Run thru the acl and mark the positions of aces. These will be later used
  1390. // to dtermine what should be done with the acl.
  1391. //
  1392. j = 0;
  1393. pChildAce = (PACE_HEADER) FirstAce(pChildAcl);
  1394. for (; j < ChildAceCnt; j++, pChildAce = (PACE_HEADER) NextAce(pChildAce))
  1395. {
  1396. switch (pChildAce->AceType)
  1397. {
  1398. case ACCESS_ALLOWED_ACE_TYPE:
  1399. case ACCESS_ALLOWED_OBJECT_ACE_TYPE:
  1400. case ACCESS_ALLOWED_COMPOUND_ACE_TYPE:
  1401. if (FALSE == Flags[j])
  1402. {
  1403. if (ChildAceCnt == FirstExplicitAllowAce)
  1404. {
  1405. FirstExplicitAllowAce = j;
  1406. }
  1407. }
  1408. if (ChildAceCnt == FirstAllowAce)
  1409. {
  1410. FirstAllowAce = j;
  1411. }
  1412. break;
  1413. case ACCESS_DENIED_ACE_TYPE:
  1414. case ACCESS_DENIED_OBJECT_ACE_TYPE:
  1415. if (TRUE == Flags[j])
  1416. {
  1417. LastInheritedDenyAce = j;
  1418. }
  1419. LastDenyAce = j;
  1420. break;
  1421. default:
  1422. break;
  1423. }
  1424. if (FALSE == Flags[j])
  1425. {
  1426. LastExplicitAce = j;
  1427. }
  1428. else
  1429. {
  1430. if (ChildAceCnt == FirstInheritedAce)
  1431. {
  1432. FirstInheritedAce = j;
  1433. }
  1434. }
  1435. }
  1436. //
  1437. // This a non-canonical acl. Do not try to correct it.
  1438. //
  1439. if ((ChildAceCnt != FirstAllowAce) && (LastDenyAce > FirstAllowAce))
  1440. {
  1441. *pCompareStatus = FALSE;
  1442. goto End;
  1443. }
  1444. //
  1445. // Do not try to rearrange the acl if
  1446. // 1. an inherited deny ace exists AND
  1447. // 2. an explicit allow ace exists.
  1448. //
  1449. if ((-1 != LastInheritedDenyAce) && (ChildAceCnt != FirstExplicitAllowAce))
  1450. {
  1451. *pCompareStatus = FALSE;
  1452. goto End;
  1453. }
  1454. //
  1455. // The acl need not be rearranged since all the explicit aces are ahead of
  1456. // the inherited ones.
  1457. //
  1458. if (LastExplicitAce < FirstInheritedAce)
  1459. {
  1460. j = 0;
  1461. pChildAce = (PACE_HEADER) FirstAce(pChildAcl);
  1462. for (; j < ChildAceCnt; j++, pChildAce = (PACE_HEADER) NextAce(pChildAce))
  1463. {
  1464. if (TRUE == Flags[j])
  1465. {
  1466. pChildAce->AceFlags |= INHERITED_ACE;
  1467. }
  1468. }
  1469. }
  1470. //
  1471. // At least one inherited ace exists before an explicit one.
  1472. // Rearrange the acl to get it in canonical form.
  1473. //
  1474. else
  1475. {
  1476. Buffer = (PUCHAR) AccAlloc(pChildAcl->AclSize - sizeof(ACL));
  1477. CurrentBuffer = Buffer;
  1478. if (NULL == Buffer)
  1479. {
  1480. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  1481. goto End;
  1482. }
  1483. j = 0;
  1484. pChildAce = (PACE_HEADER) FirstAce(pChildAcl);
  1485. for (; j <= LastExplicitAce; j++, pChildAce = (PACE_HEADER) NextAce(pChildAce))
  1486. {
  1487. if (FALSE == Flags[j])
  1488. {
  1489. memcpy(CurrentBuffer, (PUCHAR) pChildAce, pChildAce->AceSize);
  1490. CurrentBuffer += pChildAce->AceSize;
  1491. }
  1492. }
  1493. j = 0;
  1494. pChildAce = (PACE_HEADER) FirstAce(pChildAcl);
  1495. for (; j < ChildAceCnt; j++, pChildAce = (PACE_HEADER) NextAce(pChildAce))
  1496. {
  1497. if (TRUE == Flags[j])
  1498. {
  1499. memcpy(CurrentBuffer, (PUCHAR) pChildAce, pChildAce->AceSize);
  1500. ((PACE_HEADER) CurrentBuffer)->AceFlags |= INHERITED_ACE;
  1501. CurrentBuffer += pChildAce->AceSize;
  1502. }
  1503. }
  1504. memcpy(
  1505. ((PUCHAR) pChildAcl) + sizeof(ACL),
  1506. Buffer,
  1507. pChildAcl->AclSize - sizeof(ACL)
  1508. );
  1509. }
  1510. *pCompareStatus = TRUE;
  1511. End:
  1512. if (NULL != Flags)
  1513. {
  1514. AccFree(Flags);
  1515. }
  1516. if (NULL != Buffer)
  1517. {
  1518. AccFree(Buffer);
  1519. }
  1520. return dwErr;;
  1521. }
  1522. ////////////////////////////////////////////////////////////////////////////////
  1523. // //
  1524. // Function: MartaGetNT4NodeSD //
  1525. // //
  1526. // Description: Converts the child security descriptor NT4 ACL into NT5 ACL //
  1527. // by comparing it to the parent ACL. //
  1528. // //
  1529. // Arguments: //
  1530. // //
  1531. // [IN pOldSD] Old parent security descriptor //
  1532. // [IN OUT pOldChildSD] Old child security descriptor //
  1533. // [IN Processhandle] Process Handle //
  1534. // [IN bIsChildContainer] Whether the child object is a Container //
  1535. // [IN pGenMap] Generic mapping of the object rights //
  1536. // [IN SecurityInfo] Security Information requested //
  1537. // //
  1538. // Algorithm: //
  1539. // if child acl and parent acl differ then //
  1540. // mark the child acl PROTECTED //
  1541. // //
  1542. // Returns: ERROR_SUCCESS on successful completion of the routine //
  1543. // ERROR_XXXX Otherwise //
  1544. // //
  1545. ////////////////////////////////////////////////////////////////////////////////
  1546. DWORD
  1547. MartaGetNT4NodeSD(
  1548. IN PSECURITY_DESCRIPTOR pOldSD,
  1549. IN OUT PSECURITY_DESCRIPTOR pOldChildSD,
  1550. IN HANDLE ProcessHandle,
  1551. IN BOOL bIsChildContainer,
  1552. IN PGENERIC_MAPPING pGenMap,
  1553. IN SECURITY_INFORMATION SecurityInfo
  1554. )
  1555. {
  1556. SECURITY_DESCRIPTOR NullSD;
  1557. DWORD dwErr = ERROR_SUCCESS;
  1558. BOOL CompareStatus = FALSE;
  1559. PACL pChildAcl = NULL;
  1560. PACL pParentAcl = NULL;
  1561. HANDLE ThreadHandle = NULL;
  1562. PSECURITY_DESCRIPTOR pTmpSD = NULL;
  1563. UCHAR Buffer[2 * sizeof(ACL)];
  1564. PACL pDacl = (PACL) Buffer;
  1565. PACL pSacl = (PACL) (Buffer + sizeof(ACL));
  1566. if (!FLAG_ON(SecurityInfo, (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)))
  1567. {
  1568. return ERROR_SUCCESS;
  1569. }
  1570. InitializeSecurityDescriptor(&NullSD, SECURITY_DESCRIPTOR_REVISION);
  1571. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  1572. {
  1573. if (FALSE == InitializeAcl(pDacl, sizeof(ACL), ACL_REVISION))
  1574. {
  1575. return ERROR_ACCESS_DENIED;
  1576. }
  1577. if (FALSE == SetSecurityDescriptorDacl(
  1578. &NullSD,
  1579. TRUE,
  1580. pDacl,
  1581. FALSE))
  1582. {
  1583. return GetLastError();
  1584. }
  1585. }
  1586. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  1587. {
  1588. if (FALSE == InitializeAcl(pSacl, sizeof(ACL), ACL_REVISION))
  1589. {
  1590. return ERROR_ACCESS_DENIED;
  1591. }
  1592. if (FALSE == SetSecurityDescriptorSacl(
  1593. &NullSD,
  1594. TRUE,
  1595. pSacl,
  1596. FALSE))
  1597. {
  1598. return GetLastError();
  1599. }
  1600. }
  1601. if (FALSE == SetSecurityDescriptorOwner(
  1602. &NullSD,
  1603. RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD),
  1604. FALSE))
  1605. {
  1606. return GetLastError();
  1607. }
  1608. if (FALSE == SetSecurityDescriptorGroup(
  1609. &NullSD,
  1610. RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD),
  1611. FALSE))
  1612. {
  1613. return GetLastError();
  1614. }
  1615. MARTA_TURN_OFF_IMPERSONATION;
  1616. if (FALSE == CreatePrivateObjectSecurityEx(
  1617. pOldSD,
  1618. &NullSD,
  1619. &pTmpSD,
  1620. NULL,
  1621. bIsChildContainer,
  1622. (SEF_DACL_AUTO_INHERIT | SEF_SACL_AUTO_INHERIT | SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK),
  1623. ProcessHandle,
  1624. pGenMap
  1625. ))
  1626. {
  1627. dwErr = GetLastError();
  1628. }
  1629. MARTA_TURN_ON_IMPERSONATION;
  1630. CONDITIONAL_EXIT(dwErr, End);
  1631. //
  1632. // Mark the aces from the child DACL, which are present in the parent.
  1633. //
  1634. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  1635. {
  1636. pChildAcl = RtlpDaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD);
  1637. pParentAcl = RtlpDaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pTmpSD);
  1638. dwErr = MartaCompareAndMarkInheritedAces(
  1639. pParentAcl,
  1640. pChildAcl,
  1641. bIsChildContainer,
  1642. &CompareStatus
  1643. );
  1644. CONDITIONAL_EXIT(dwErr, End);
  1645. if (FALSE == CompareStatus)
  1646. {
  1647. ((PISECURITY_DESCRIPTOR) pOldChildSD)->Control |= SE_DACL_PROTECTED;
  1648. }
  1649. }
  1650. //
  1651. // Mark the aces from the child SACL, which are present in the parent.
  1652. //
  1653. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  1654. {
  1655. pChildAcl = RtlpSaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD);
  1656. pParentAcl = RtlpSaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pTmpSD);
  1657. dwErr = MartaCompareAndMarkInheritedAces(
  1658. pParentAcl,
  1659. pChildAcl,
  1660. bIsChildContainer,
  1661. &CompareStatus
  1662. );
  1663. CONDITIONAL_EXIT(dwErr, End);
  1664. if (FALSE == CompareStatus)
  1665. {
  1666. ((PISECURITY_DESCRIPTOR) pOldChildSD)->Control |= SE_SACL_PROTECTED;
  1667. }
  1668. CONDITIONAL_EXIT(dwErr, End);
  1669. }
  1670. End:
  1671. if (NULL != pTmpSD)
  1672. {
  1673. DestroyPrivateObjectSecurity(&pTmpSD);
  1674. }
  1675. return dwErr;
  1676. }
  1677. ////////////////////////////////////////////////////////////////////////////////
  1678. // //
  1679. // Function: MartaUpdateTree //
  1680. // //
  1681. // Description: Propagate the inheritable aces to the children. //
  1682. // //
  1683. // Arguments: //
  1684. // //
  1685. // [IN SecurityInfo] Security Information requested //
  1686. // [IN pNewSD] New parent security descriptor //
  1687. // [IN pOldSD] Old parent security descriptor //
  1688. // [IN Context] Context structure for the object //
  1689. // [IN Processhandle] Process Handle //
  1690. // [IN pSetFunctionContext] Structure holding the function pointers //
  1691. // [IN pGenMap] Generic mapping of the object rights //
  1692. // //
  1693. // Algorithm: //
  1694. // For all children that are not "Protected" //
  1695. // if OldChildSD = NT4 style //
  1696. // Convert it into NT5 style //
  1697. // NewChildSD = Merge(ParentSD, OldChildSD) //
  1698. // UpdateTree(Child) //
  1699. // Stamp NewChildSD on Child //
  1700. // //
  1701. // Note: An error in the propagation is ignored. //
  1702. // //
  1703. ////////////////////////////////////////////////////////////////////////////////
  1704. BOOL
  1705. MartaUpdateTree(
  1706. IN SECURITY_INFORMATION SecurityInfo,
  1707. IN PSECURITY_DESCRIPTOR pNewSD,
  1708. IN PSECURITY_DESCRIPTOR pOldSD,
  1709. IN MARTA_CONTEXT Context,
  1710. IN HANDLE ProcessHandle,
  1711. IN PMARTA_SET_FUNCTION_CONTEXT pMartaSetFunctionContext,
  1712. IN PGENERIC_MAPPING pGenMap
  1713. )
  1714. {
  1715. MARTA_OBJECT_PROPERTIES ObjectProperties;
  1716. DWORD dwErr = ERROR_SUCCESS;
  1717. BOOL bIsChildContainer = FALSE;
  1718. BOOL bRetryPropagation = FALSE;
  1719. BOOL bDoPropagate = TRUE;
  1720. HANDLE ThreadHandle = NULL;
  1721. PSECURITY_DESCRIPTOR pOldChildSD = NULL;
  1722. PSECURITY_DESCRIPTOR pNewChildSD = NULL;
  1723. MARTA_CONTEXT ChildContext = NULL_MARTA_CONTEXT;
  1724. //
  1725. // Get the first child to update.
  1726. //
  1727. dwErr = (*(pMartaSetFunctionContext->fFindFirst))(
  1728. Context,
  1729. (*(pMartaSetFunctionContext->fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, TRUE, SecurityInfo),
  1730. &ChildContext
  1731. );
  1732. if (ERROR_SUCCESS != dwErr)
  1733. {
  1734. if (NULL == ChildContext)
  1735. {
  1736. dwErr = (*(pMartaSetFunctionContext->fFindFirst))(
  1737. Context,
  1738. (*(pMartaSetFunctionContext->fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo),
  1739. &ChildContext
  1740. );
  1741. }
  1742. else
  1743. {
  1744. dwErr = (*(pMartaSetFunctionContext->fReopenContext))(
  1745. ChildContext,
  1746. (*(pMartaSetFunctionContext->fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo)
  1747. );
  1748. }
  1749. bDoPropagate = FALSE;
  1750. }
  1751. if (NULL == ChildContext)
  1752. {
  1753. return TRUE;
  1754. }
  1755. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1756. //
  1757. // Note: On any intermediate error the current child is skipped.
  1758. //
  1759. while (ChildContext)
  1760. {
  1761. ObjectProperties.cbSize = sizeof(ObjectProperties);
  1762. ObjectProperties.dwFlags = 0;
  1763. dwErr = (*(pMartaSetFunctionContext->fGetProperties))(
  1764. ChildContext,
  1765. &ObjectProperties
  1766. );
  1767. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1768. bIsChildContainer = FLAG_ON(ObjectProperties.dwFlags, MARTA_OBJECT_IS_CONTAINER);
  1769. dwErr = (*(pMartaSetFunctionContext->fGetRights))(
  1770. ChildContext,
  1771. (SecurityInfo | OWNER_SECURITY_INFORMATION |
  1772. GROUP_SECURITY_INFORMATION),
  1773. &pOldChildSD
  1774. );
  1775. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1776. //
  1777. // Skip the children that are protected.
  1778. //
  1779. if (!MARTA_SD_NOT_PROTECTED((PISECURITY_DESCRIPTOR) pOldChildSD, SecurityInfo))
  1780. {
  1781. goto EndOfWhile;
  1782. }
  1783. //
  1784. // Convert NT4 SD to NT5 style.
  1785. //
  1786. if (FALSE == MartaIsSDNT5Style(pOldChildSD))
  1787. {
  1788. //
  1789. // Note that this modifies OldChildSD in one of the two ways:
  1790. // 1. If any of the inheritable aces from the OldSD are missing
  1791. // from OldChild then
  1792. // Mark the acl PROTECTED.
  1793. // 2. else
  1794. // Mark the common aces in ChildSD as INHERITED.
  1795. //
  1796. dwErr = MartaGetNT4NodeSD(
  1797. pOldSD,
  1798. pOldChildSD,
  1799. ProcessHandle,
  1800. bIsChildContainer,
  1801. pGenMap,
  1802. SecurityInfo
  1803. );
  1804. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1805. }
  1806. MARTA_TURN_OFF_IMPERSONATION;
  1807. //
  1808. // Merge the NewParentSD and the OldChildSD to create NewChildSD.
  1809. //
  1810. if (FALSE == CreatePrivateObjectSecurityEx(
  1811. pNewSD,
  1812. pOldChildSD,
  1813. &pNewChildSD,
  1814. NULL,
  1815. bIsChildContainer,
  1816. (SEF_DACL_AUTO_INHERIT | SEF_SACL_AUTO_INHERIT |
  1817. SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK),
  1818. ProcessHandle,
  1819. pGenMap
  1820. ))
  1821. {
  1822. dwErr = GetLastError();
  1823. }
  1824. MARTA_TURN_ON_IMPERSONATION;
  1825. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1826. //
  1827. // Update the subtree undrneath this child.
  1828. //
  1829. if (bIsChildContainer)
  1830. {
  1831. if (bDoPropagate)
  1832. {
  1833. bRetryPropagation = MartaUpdateTree(
  1834. SecurityInfo,
  1835. pNewChildSD,
  1836. pOldChildSD,
  1837. ChildContext,
  1838. ProcessHandle,
  1839. pMartaSetFunctionContext,
  1840. pGenMap
  1841. );
  1842. }
  1843. else
  1844. {
  1845. bRetryPropagation = TRUE;
  1846. }
  1847. }
  1848. //
  1849. // Stamp NewChildSD on child.
  1850. //
  1851. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  1852. {
  1853. ((PISECURITY_DESCRIPTOR) pNewChildSD)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  1854. }
  1855. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  1856. {
  1857. ((PISECURITY_DESCRIPTOR) pNewChildSD)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  1858. }
  1859. dwErr = (*(pMartaSetFunctionContext->fSetRights))(
  1860. ChildContext,
  1861. SecurityInfo,
  1862. pNewChildSD
  1863. );
  1864. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1865. //
  1866. // If propagation had failed in the first attept then try again. This is to
  1867. // cover the case when the container can be enumerated after setting the new
  1868. // security.
  1869. if (bRetryPropagation && (SecurityInfo & DACL_SECURITY_INFORMATION))
  1870. {
  1871. ACCESS_MASK Access = (*(pMartaSetFunctionContext->fGetDesiredAccess))(
  1872. NO_ACCESS_RIGHTS,
  1873. TRUE,
  1874. SecurityInfo & (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)
  1875. );
  1876. dwErr = (*(pMartaSetFunctionContext->fReopenContext))(
  1877. ChildContext,
  1878. Access
  1879. );
  1880. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  1881. (VOID) MartaUpdateTree(
  1882. SecurityInfo,
  1883. pNewChildSD,
  1884. pOldChildSD,
  1885. ChildContext,
  1886. ProcessHandle,
  1887. pMartaSetFunctionContext,
  1888. pGenMap
  1889. );
  1890. }
  1891. EndOfWhile:
  1892. bRetryPropagation = FALSE;
  1893. if (NULL != pOldChildSD)
  1894. {
  1895. AccFree(pOldChildSD);
  1896. pOldChildSD = NULL;
  1897. }
  1898. if (NULL != pNewChildSD)
  1899. {
  1900. DestroyPrivateObjectSecurity(&pNewChildSD);
  1901. pNewChildSD = NULL;
  1902. }
  1903. //
  1904. // Get the next child.
  1905. //
  1906. do {
  1907. dwErr = (*(pMartaSetFunctionContext->fFindNext))(
  1908. ChildContext,
  1909. (*(pMartaSetFunctionContext->fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, TRUE, SecurityInfo),
  1910. &ChildContext
  1911. );
  1912. if (ERROR_SUCCESS != dwErr)
  1913. {
  1914. dwErr = (*(pMartaSetFunctionContext->fReopenContext))(
  1915. ChildContext,
  1916. (*(pMartaSetFunctionContext->fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo)
  1917. );
  1918. bDoPropagate = FALSE;
  1919. }
  1920. else
  1921. {
  1922. bDoPropagate = TRUE;
  1923. }
  1924. } while ((ERROR_SUCCESS != dwErr) && (NULL != ChildContext));
  1925. }
  1926. return FALSE;
  1927. }
  1928. ////////////////////////////////////////////////////////////////////////////////
  1929. // //
  1930. // Function: AccRewriteSetNamedRights //
  1931. // //
  1932. // Description: Set the security descriptor passed in on the object passed in //
  1933. // by Name. //
  1934. // This routine is exported by ntmarta and called by advapi32. //
  1935. // //
  1936. // Arguments: //
  1937. // //
  1938. // [IN pObjectName] Name of the Object //
  1939. // [IN ObjectType] Type of the object //
  1940. // [IN SecurityInfo] Security Information to be stamped //
  1941. // [IN pSecurityDescriptor] Security descriptor to be stamped //
  1942. // [IN bSkipInheritanceComputation] Whether to compute inherited aces //
  1943. // from the parent
  1944. // //
  1945. ////////////////////////////////////////////////////////////////////////////////
  1946. DWORD
  1947. AccRewriteSetNamedRights(
  1948. IN LPWSTR pObjectName,
  1949. IN SE_OBJECT_TYPE ObjectType,
  1950. IN SECURITY_INFORMATION SecurityInfo,
  1951. IN OUT PSECURITY_DESCRIPTOR pSecurityDescriptor,
  1952. IN BOOL bSkipInheritanceComputation
  1953. )
  1954. {
  1955. DWORD dwErr = ERROR_SUCCESS;
  1956. MARTA_CONTEXT Context = NULL_MARTA_CONTEXT;
  1957. BOOL bDoPropagate = FALSE;
  1958. BOOL bReadOldProtectedBits = FALSE;
  1959. MARTA_OBJECT_TYPE_PROPERTIES ObjectTypeProperties;
  1960. MARTA_SET_FUNCTION_CONTEXT MartaSetFunctionContext;
  1961. GENERIC_MAPPING ZeroGenMap = {0, 0, 0, 0};
  1962. SECURITY_INFORMATION LocalSecurityInfo = (SECURITY_INFORMATION) 0;
  1963. PSECURITY_DESCRIPTOR pOldSD = NULL;
  1964. //
  1965. // Named calls are not valid only for the following object types.
  1966. //
  1967. switch (ObjectType)
  1968. {
  1969. case SE_FILE_OBJECT:
  1970. case SE_SERVICE:
  1971. case SE_PRINTER:
  1972. case SE_REGISTRY_KEY:
  1973. case SE_REGISTRY_WOW64_32KEY:
  1974. case SE_LMSHARE:
  1975. case SE_KERNEL_OBJECT:
  1976. case SE_WINDOW_OBJECT:
  1977. case SE_WMIGUID_OBJECT:
  1978. case SE_DS_OBJECT:
  1979. case SE_DS_OBJECT_ALL:
  1980. break;
  1981. case SE_PROVIDER_DEFINED_OBJECT:
  1982. case SE_UNKNOWN_OBJECT_TYPE:
  1983. default:
  1984. return ERROR_INVALID_PARAMETER;
  1985. }
  1986. //
  1987. // Initialize the structure to hold the function pointers.
  1988. //
  1989. MartaInitializeSetContext(ObjectType, &MartaSetFunctionContext);
  1990. //
  1991. // Get the "Type" properties for the object,
  1992. //
  1993. ObjectTypeProperties.cbSize = sizeof(ObjectTypeProperties);
  1994. ObjectTypeProperties.dwFlags = 0;
  1995. ObjectTypeProperties.GenMap = ZeroGenMap;
  1996. dwErr = (*(MartaSetFunctionContext.fGetTypeProperties))(&ObjectTypeProperties);
  1997. CONDITIONAL_EXIT(dwErr, End);
  1998. //
  1999. // To make sure that NT4 applications do not wipe out PROTECTED bits, make a
  2000. // note whether the caller knows what he is doing i.e. has passed in the
  2001. // appropriate PROTECTED flags.
  2002. //
  2003. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  2004. {
  2005. if (!FLAG_ON(SecurityInfo, (PROTECTED_DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION)))
  2006. {
  2007. bReadOldProtectedBits = TRUE;
  2008. LocalSecurityInfo |= DACL_SECURITY_INFORMATION;
  2009. }
  2010. }
  2011. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  2012. {
  2013. if (!FLAG_ON(SecurityInfo, (PROTECTED_SACL_SECURITY_INFORMATION | UNPROTECTED_SACL_SECURITY_INFORMATION)))
  2014. {
  2015. bReadOldProtectedBits = TRUE;
  2016. LocalSecurityInfo |= SACL_SECURITY_INFORMATION;
  2017. }
  2018. }
  2019. //
  2020. // Even for objects like Files/RegistryKeys manual propagation is required
  2021. // only if DACl/SACL is to be set.
  2022. //
  2023. if ((FLAG_ON(ObjectTypeProperties.dwFlags, MARTA_OBJECT_TYPE_MANUAL_PROPAGATION_NEEDED_FLAG)) &&
  2024. (FLAG_ON(SecurityInfo, (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION))))
  2025. {
  2026. dwErr = (*(MartaSetFunctionContext.fOpenNamedObject))(
  2027. pObjectName,
  2028. (*(MartaSetFunctionContext.fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, TRUE, SecurityInfo),
  2029. &Context
  2030. );
  2031. if (ERROR_SUCCESS != dwErr)
  2032. {
  2033. bDoPropagate = FALSE;
  2034. dwErr = (*(MartaSetFunctionContext.fOpenNamedObject))(
  2035. pObjectName,
  2036. (*(MartaSetFunctionContext.fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo),
  2037. &Context
  2038. );
  2039. CONDITIONAL_EXIT(dwErr, End);
  2040. }
  2041. else
  2042. {
  2043. bDoPropagate = TRUE;
  2044. }
  2045. dwErr = MartaManualPropagation(
  2046. Context,
  2047. SecurityInfo,
  2048. pSecurityDescriptor,
  2049. &(ObjectTypeProperties.GenMap),
  2050. bDoPropagate,
  2051. bReadOldProtectedBits,
  2052. &MartaSetFunctionContext,
  2053. bSkipInheritanceComputation
  2054. );
  2055. CONDITIONAL_EXIT(dwErr, End);
  2056. }
  2057. //
  2058. // For object for which manual propagation is not required, stamp the SD
  2059. // on the object.
  2060. //
  2061. else
  2062. {
  2063. if ((FLAG_ON(ObjectTypeProperties.dwFlags, MARTA_OBJECT_TYPE_INHERITANCE_MODEL_PRESENT_FLAG)) &&
  2064. bReadOldProtectedBits)
  2065. {
  2066. dwErr = (*(MartaSetFunctionContext.fOpenNamedObject))(
  2067. pObjectName,
  2068. (*(MartaSetFunctionContext.fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo),
  2069. &Context
  2070. );
  2071. CONDITIONAL_EXIT(dwErr, End);
  2072. dwErr = (*(MartaSetFunctionContext.fGetRights))(
  2073. Context,
  2074. LocalSecurityInfo,
  2075. &pOldSD
  2076. );
  2077. CONDITIONAL_EXIT(dwErr, End);
  2078. //
  2079. // If none of the PROTECTED flags are passed in then do the "right" thing.
  2080. // Read the PROTECTED bit from the existing security descriptor and set it
  2081. // in the new one.
  2082. //
  2083. if (NULL != pOldSD)
  2084. {
  2085. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  2086. {
  2087. if (!FLAG_ON(SecurityInfo, (PROTECTED_DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION)))
  2088. {
  2089. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= ((PISECURITY_DESCRIPTOR) pOldSD)->Control & SE_DACL_PROTECTED;
  2090. }
  2091. }
  2092. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  2093. {
  2094. if (!FLAG_ON(SecurityInfo, (PROTECTED_SACL_SECURITY_INFORMATION | UNPROTECTED_SACL_SECURITY_INFORMATION)))
  2095. {
  2096. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= ((PISECURITY_DESCRIPTOR) pOldSD)->Control & SE_SACL_PROTECTED;
  2097. }
  2098. }
  2099. }
  2100. }
  2101. else
  2102. {
  2103. dwErr = (*(MartaSetFunctionContext.fOpenNamedObject))(
  2104. pObjectName,
  2105. (*(MartaSetFunctionContext.fGetDesiredAccess))(WRITE_ACCESS_RIGHTS, FALSE, SecurityInfo),
  2106. &Context
  2107. );
  2108. CONDITIONAL_EXIT(dwErr, End);
  2109. }
  2110. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  2111. {
  2112. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  2113. }
  2114. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  2115. {
  2116. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  2117. }
  2118. dwErr = (*(MartaSetFunctionContext.fSetRights))(
  2119. Context,
  2120. SecurityInfo,
  2121. pSecurityDescriptor
  2122. );
  2123. CONDITIONAL_EXIT(dwErr, End);
  2124. }
  2125. End:
  2126. if (NULL != Context)
  2127. {
  2128. (VOID) (*(MartaSetFunctionContext.fCloseContext))(Context);
  2129. }
  2130. if (NULL != pOldSD)
  2131. {
  2132. AccFree(pOldSD);
  2133. }
  2134. return dwErr;
  2135. }
  2136. ////////////////////////////////////////////////////////////////////////////////
  2137. // //
  2138. // Function: AccRewriteSetHandleRights //
  2139. // //
  2140. // Description: Set the security descriptor passed in on the object passed in //
  2141. // as Handle. //
  2142. // This routine is exported by ntmarta and called by advapi32. //
  2143. // //
  2144. // Arguments: //
  2145. // //
  2146. // [IN Handle] Handle to the Object //
  2147. // [IN ObjectType] Type of the object //
  2148. // [IN SecurityInfo] Security Information to be stamped //
  2149. // [IN pSecurityDescriptor] Security descriptor to be stamped //
  2150. // //
  2151. ////////////////////////////////////////////////////////////////////////////////
  2152. DWORD
  2153. AccRewriteSetHandleRights(
  2154. IN HANDLE Handle,
  2155. IN SE_OBJECT_TYPE ObjectType,
  2156. IN SECURITY_INFORMATION SecurityInfo,
  2157. IN OUT PSECURITY_DESCRIPTOR pSecurityDescriptor
  2158. )
  2159. {
  2160. DWORD dwErr = ERROR_SUCCESS;
  2161. MARTA_CONTEXT Context = NULL_MARTA_CONTEXT;
  2162. BOOL bDoPropagate = FALSE;
  2163. BOOL bReadOldProtectedBits = FALSE;
  2164. MARTA_OBJECT_TYPE_PROPERTIES ObjectTypeProperties;
  2165. MARTA_SET_FUNCTION_CONTEXT MartaSetFunctionContext;
  2166. GENERIC_MAPPING ZeroGenMap = {0, 0, 0, 0};
  2167. SECURITY_INFORMATION LocalSecurityInfo = (SECURITY_INFORMATION) 0;
  2168. PSECURITY_DESCRIPTOR pOldSD = NULL;
  2169. //
  2170. // Handle calls are not valid for all object types.
  2171. //
  2172. switch (ObjectType)
  2173. {
  2174. case SE_FILE_OBJECT:
  2175. case SE_SERVICE:
  2176. case SE_PRINTER:
  2177. case SE_REGISTRY_KEY:
  2178. case SE_LMSHARE:
  2179. case SE_KERNEL_OBJECT:
  2180. case SE_WINDOW_OBJECT:
  2181. case SE_WMIGUID_OBJECT:
  2182. break;
  2183. case SE_DS_OBJECT:
  2184. case SE_DS_OBJECT_ALL:
  2185. case SE_PROVIDER_DEFINED_OBJECT:
  2186. case SE_UNKNOWN_OBJECT_TYPE:
  2187. default:
  2188. return ERROR_INVALID_PARAMETER;
  2189. }
  2190. //
  2191. // Initialize the structure to hold the function pointers.
  2192. //
  2193. MartaInitializeSetContext(ObjectType, &MartaSetFunctionContext);
  2194. //
  2195. // Get the "Type" properties for the object,
  2196. //
  2197. ObjectTypeProperties.cbSize = sizeof(ObjectTypeProperties);
  2198. ObjectTypeProperties.dwFlags = 0;
  2199. ObjectTypeProperties.GenMap = ZeroGenMap;
  2200. dwErr = (*(MartaSetFunctionContext.fGetTypeProperties))(&ObjectTypeProperties);
  2201. CONDITIONAL_EXIT(dwErr, End);
  2202. //
  2203. // To make sure that NT4 applications do not wipe out PROTECTED bits, make a
  2204. // note whether the caller knows what he is doing i.e. has passed in the
  2205. // appropriate PROTECTED flags.
  2206. //
  2207. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  2208. {
  2209. if (!FLAG_ON(SecurityInfo, (PROTECTED_DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION)))
  2210. {
  2211. bReadOldProtectedBits = TRUE;
  2212. LocalSecurityInfo |= DACL_SECURITY_INFORMATION;
  2213. }
  2214. }
  2215. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  2216. {
  2217. if (!FLAG_ON(SecurityInfo, (PROTECTED_SACL_SECURITY_INFORMATION | UNPROTECTED_SACL_SECURITY_INFORMATION)))
  2218. {
  2219. bReadOldProtectedBits = TRUE;
  2220. LocalSecurityInfo |= SACL_SECURITY_INFORMATION;
  2221. }
  2222. }
  2223. //
  2224. // Even for objects like Files/RegistryKeys manual propagation is required
  2225. // only if DACl/SACL is to be set.
  2226. //
  2227. if ((FLAG_ON(ObjectTypeProperties.dwFlags, MARTA_OBJECT_TYPE_MANUAL_PROPAGATION_NEEDED_FLAG)) &&
  2228. (FLAG_ON(SecurityInfo, (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION))))
  2229. {
  2230. dwErr = (*(MartaSetFunctionContext.fOpenHandleObject))(
  2231. Handle,
  2232. (*(MartaSetFunctionContext.fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, TRUE, SecurityInfo),
  2233. &Context
  2234. );
  2235. if (ERROR_SUCCESS != dwErr)
  2236. {
  2237. bDoPropagate = FALSE;
  2238. dwErr = (*(MartaSetFunctionContext.fOpenHandleObject))(
  2239. Handle,
  2240. (*(MartaSetFunctionContext.fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo),
  2241. &Context
  2242. );
  2243. CONDITIONAL_EXIT(dwErr, End);
  2244. }
  2245. else
  2246. {
  2247. bDoPropagate = TRUE;
  2248. }
  2249. dwErr = MartaManualPropagation(
  2250. Context,
  2251. SecurityInfo,
  2252. pSecurityDescriptor,
  2253. &(ObjectTypeProperties.GenMap),
  2254. bDoPropagate,
  2255. bReadOldProtectedBits,
  2256. &MartaSetFunctionContext,
  2257. FALSE // Do not skip inheritance computation
  2258. );
  2259. CONDITIONAL_EXIT(dwErr, End);
  2260. }
  2261. //
  2262. // For object for which manual propagation is not required, stamp the SD
  2263. // on the object.
  2264. //
  2265. else
  2266. {
  2267. if ((FLAG_ON(ObjectTypeProperties.dwFlags, MARTA_OBJECT_TYPE_INHERITANCE_MODEL_PRESENT_FLAG)) &&
  2268. bReadOldProtectedBits)
  2269. {
  2270. dwErr = (*(MartaSetFunctionContext.fOpenHandleObject))(
  2271. Handle,
  2272. (*(MartaSetFunctionContext.fGetDesiredAccess))(MODIFY_ACCESS_RIGHTS, FALSE, SecurityInfo),
  2273. &Context
  2274. );
  2275. CONDITIONAL_EXIT(dwErr, End);
  2276. dwErr = (*(MartaSetFunctionContext.fGetRights))(
  2277. Context,
  2278. LocalSecurityInfo,
  2279. &pOldSD
  2280. );
  2281. CONDITIONAL_EXIT(dwErr, End);
  2282. //
  2283. // If none of the PROTECTED flags are passed in then do the "right" thing.
  2284. // Read the PROTECTED bit from the existing security descriptor and set it
  2285. // in the new one.
  2286. //
  2287. if (NULL != pOldSD)
  2288. {
  2289. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  2290. {
  2291. if (!FLAG_ON(SecurityInfo, (PROTECTED_DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION)))
  2292. {
  2293. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= ((PISECURITY_DESCRIPTOR) pOldSD)->Control & SE_DACL_PROTECTED;
  2294. }
  2295. }
  2296. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  2297. {
  2298. if (!FLAG_ON(SecurityInfo, (PROTECTED_SACL_SECURITY_INFORMATION | UNPROTECTED_SACL_SECURITY_INFORMATION)))
  2299. {
  2300. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= ((PISECURITY_DESCRIPTOR) pOldSD)->Control & SE_SACL_PROTECTED;
  2301. }
  2302. }
  2303. }
  2304. }
  2305. else
  2306. {
  2307. dwErr = (*(MartaSetFunctionContext.fOpenHandleObject))(
  2308. Handle,
  2309. (*(MartaSetFunctionContext.fGetDesiredAccess))(WRITE_ACCESS_RIGHTS, FALSE, SecurityInfo),
  2310. &Context
  2311. );
  2312. CONDITIONAL_EXIT(dwErr, End);
  2313. }
  2314. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  2315. {
  2316. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  2317. }
  2318. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  2319. {
  2320. ((PISECURITY_DESCRIPTOR) pSecurityDescriptor)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  2321. }
  2322. dwErr = (*(MartaSetFunctionContext.fSetRights))(
  2323. Context,
  2324. SecurityInfo,
  2325. pSecurityDescriptor
  2326. );
  2327. CONDITIONAL_EXIT(dwErr, End);
  2328. }
  2329. End:
  2330. if (NULL != Context)
  2331. {
  2332. (VOID) (*(MartaSetFunctionContext.fCloseContext))(Context);
  2333. }
  2334. if (NULL != pOldSD)
  2335. {
  2336. AccFree(pOldSD);
  2337. }
  2338. return dwErr;
  2339. }
  2340. typedef struct _FN_OBJECT_FUNCTIONS
  2341. {
  2342. ULONG Flags;
  2343. } FN_OBJECT_FUNCTIONS, *PFN_OBJECT_FUNCTIONS;
  2344. #define MARTA_NO_PARENT (LONG) -1
  2345. #define MARTA_EXPLICIT_ACE 0
  2346. VOID
  2347. MartaInitializeIndexContext(
  2348. IN SE_OBJECT_TYPE ObjectType,
  2349. OUT PMARTA_INDEX_FUNCTION_CONTEXT pFunctionContext
  2350. )
  2351. {
  2352. pFunctionContext->fCloseContext = MartaCloseContext[ObjectType];
  2353. pFunctionContext->fOpenNamedObject = MartaOpenNamedObject[ObjectType];
  2354. pFunctionContext->fGetRights = MartaGetRights[ObjectType];
  2355. pFunctionContext->fGetParentName = MartaGetParentName[ObjectType];
  2356. }
  2357. typedef DWORD (*PFN_FREE) (IN PVOID Mem);
  2358. DWORD
  2359. AccFreeIndexArray(
  2360. IN OUT PINHERITED_FROMW pInheritArray,
  2361. IN USHORT AceCnt,
  2362. IN PFN_FREE pfnFree
  2363. );
  2364. ////////////////////////////////////////////////////////////////////////////////
  2365. // //
  2366. // Function: AccGetInheritanceSource //
  2367. // //
  2368. // Description: Get the source of every inherited ace in the gicen acl. //
  2369. // //
  2370. // Arguments: //
  2371. // //
  2372. // [IN pObjectName] Name of the object //
  2373. // [IN ObjecType] Type of the object ex. File/Reg/DS //
  2374. // [IN SecurityInfo] Whether DACL/SACL //
  2375. // [IN Container] Whether Object or Container //
  2376. // [IN ObjecTypeGuid] Type of the object (for DS objects) //
  2377. // [IN pAcl] DACL or SACL depending on SecurityInfo //
  2378. // [IN pGenericMapping] GenericMapping for the object type //
  2379. // [IN pfnArray] Function pointers when we support non-DS/FS/Reg //
  2380. // [OUT pInheritArray] To return the results //
  2381. // //
  2382. // //
  2383. // Algorithm: //
  2384. // Initialize the output structure. //
  2385. // Read the Owner/Group info needed for CreatePrivateObjectSecurityEx. //
  2386. // while (unmarked inherited aces exist) //
  2387. // Get the parent at the next level. //
  2388. // if we are at the root, //
  2389. // break //
  2390. // Get the xAcl for the parent. //
  2391. // for ancestors other than the immediate parent //
  2392. // Mask off inheritance flags for ACES with NP or ID //
  2393. // for immediate parent //
  2394. // Mask off inheritance flags for ACES with ID //
  2395. // Call CreatePrivateObjectSecurityEx with the empty SD and ParentSD //
  2396. // to get ExpectedSD //
  2397. // From the input xAcl, //
  2398. // mark unmarked common inherited aces //
  2399. // Update count //
  2400. // If the parent xAcl was protected, //
  2401. // break //
  2402. // //
  2403. // Returns: ERROR_SUCCESS on successful completion of the routine //
  2404. // ERROR_XXXX Otherwise //
  2405. // //
  2406. ////////////////////////////////////////////////////////////////////////////////
  2407. DWORD
  2408. AccGetInheritanceSource(
  2409. IN LPWSTR pObjectName,
  2410. IN SE_OBJECT_TYPE ObjectType,
  2411. IN SECURITY_INFORMATION SecurityInfo,
  2412. IN BOOL Container,
  2413. IN GUID ** pObjectTypeGuid OPTIONAL,
  2414. IN DWORD GuidCount,
  2415. IN PACL pAcl,
  2416. IN PGENERIC_MAPPING pGenericMapping,
  2417. IN PFN_OBJECT_MGR_FUNCTS pfnArray OPTIONAL,
  2418. OUT PINHERITED_FROMW pInheritArray
  2419. )
  2420. {
  2421. SECURITY_DESCRIPTOR EmptySD;
  2422. MARTA_INDEX_FUNCTION_CONTEXT MartaIndexFunctionContext;
  2423. USHORT i = 0;
  2424. USHORT j = 0;
  2425. USHORT AceCnt = 0;
  2426. USHORT ParentAceCnt = 0;
  2427. USHORT ExpectedAceCnt = 0;
  2428. USHORT InheritedAceCnt = 0;
  2429. USHORT ParentIndex = 0;
  2430. ULONG ProtectedBit = 0;
  2431. UCHAR FlagsToKeep = 0;
  2432. DWORD dwErr = ERROR_SUCCESS;
  2433. BOOL bKnownObject = FALSE;
  2434. BOOL Match = TRUE;
  2435. BOOL ProtectedParent = FALSE;
  2436. LPWSTR ParentName = NULL;
  2437. LPWSTR OldName = NULL;
  2438. PACL pParentAcl = NULL;
  2439. PACL pExpectedAcl = NULL;
  2440. PACE_HEADER pAce = NULL;
  2441. PACE_HEADER pParentAce = NULL;
  2442. PACE_HEADER pExpectedAce = NULL;
  2443. PSECURITY_DESCRIPTOR pSD = NULL;
  2444. PSECURITY_DESCRIPTOR pNewSD = NULL;
  2445. PSECURITY_DESCRIPTOR pParentSD = NULL;
  2446. MARTA_CONTEXT Context = NULL_MARTA_CONTEXT;
  2447. //
  2448. // Simple error checks to make sure that the input is valid.
  2449. //
  2450. if ((NULL == pAcl) || (!RtlValidAcl(pAcl)))
  2451. {
  2452. return ERROR_INVALID_PARAMETER;
  2453. }
  2454. if ((NULL == pObjectName) || (NULL == pInheritArray) || (NULL == pGenericMapping))
  2455. {
  2456. return ERROR_INVALID_PARAMETER;
  2457. }
  2458. //
  2459. // Make sure that the caller requested for either the SACL or the DACL and
  2460. // nothing else.
  2461. // Record the corresponding protected flag. This will be used later on.
  2462. //
  2463. switch (SecurityInfo)
  2464. {
  2465. case DACL_SECURITY_INFORMATION:
  2466. ProtectedBit = SE_DACL_PROTECTED;
  2467. break;
  2468. case SACL_SECURITY_INFORMATION:
  2469. ProtectedBit = SE_SACL_PROTECTED;
  2470. break;
  2471. default:
  2472. return ERROR_INVALID_PARAMETER;
  2473. }
  2474. //
  2475. // The function is supported for just files, registry, and DS objects.
  2476. //
  2477. switch (ObjectType)
  2478. {
  2479. case SE_FILE_OBJECT:
  2480. case SE_REGISTRY_KEY:
  2481. case SE_DS_OBJECT:
  2482. case SE_DS_OBJECT_ALL:
  2483. bKnownObject = TRUE;
  2484. MartaInitializeIndexContext(ObjectType, &MartaIndexFunctionContext);
  2485. break;
  2486. default:
  2487. return ERROR_INVALID_PARAMETER;
  2488. }
  2489. AceCnt = pAcl->AceCount;
  2490. //
  2491. // Return for an empty acl.
  2492. //
  2493. if (0 == AceCnt)
  2494. {
  2495. return ERROR_SUCCESS;
  2496. }
  2497. //
  2498. // We need to mask off certain aceflags for ancestors other than the parent.
  2499. //
  2500. if (Container)
  2501. {
  2502. FlagsToKeep = ~(OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE);
  2503. }
  2504. else
  2505. {
  2506. FlagsToKeep = ~OBJECT_INHERIT_ACE;
  2507. }
  2508. //
  2509. // Run thru the acl and mark the aces as either INHERITED or EXPLICIT.
  2510. // Keep a count of inherited aces and set return values to default.
  2511. //
  2512. pAce = (PACE_HEADER) FirstAce(pAcl);
  2513. for (i = 0; i < AceCnt; pAce = (PACE_HEADER) NextAce(pAce), i++)
  2514. {
  2515. if (FLAG_ON(pAce->AceFlags, INHERITED_ACE))
  2516. {
  2517. pInheritArray[i].GenerationGap = MARTA_NO_PARENT;
  2518. InheritedAceCnt++;
  2519. }
  2520. else
  2521. {
  2522. pInheritArray[i].GenerationGap = MARTA_EXPLICIT_ACE;
  2523. }
  2524. pInheritArray[i].AncestorName = NULL;
  2525. }
  2526. //
  2527. // Return for an acl with no inherited aces.
  2528. //
  2529. if (0 == InheritedAceCnt)
  2530. {
  2531. return ERROR_SUCCESS;
  2532. }
  2533. InitializeSecurityDescriptor(&EmptySD, SECURITY_DESCRIPTOR_REVISION);
  2534. if (bKnownObject)
  2535. {
  2536. //
  2537. // Read the owner and group information on the object.
  2538. //
  2539. dwErr = (*(MartaIndexFunctionContext.fOpenNamedObject))(
  2540. pObjectName,
  2541. READ_CONTROL,
  2542. &Context
  2543. );
  2544. CONDITIONAL_EXIT(dwErr, End);
  2545. dwErr = (*(MartaIndexFunctionContext.fGetRights))(
  2546. Context,
  2547. OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
  2548. &pSD
  2549. );
  2550. // Note: This has to be freed immediately!!
  2551. (VOID) (*(MartaIndexFunctionContext.fCloseContext))(Context);
  2552. CONDITIONAL_EXIT(dwErr, End);
  2553. }
  2554. else
  2555. {
  2556. //
  2557. // Get the owner and group information.
  2558. //
  2559. }
  2560. //
  2561. // Set the owner and group information in pSD.
  2562. //
  2563. if (!SetSecurityDescriptorOwner(
  2564. &EmptySD,
  2565. RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pSD),
  2566. FALSE))
  2567. {
  2568. dwErr = GetLastError();
  2569. goto End;
  2570. }
  2571. if (!SetSecurityDescriptorGroup(
  2572. &EmptySD,
  2573. RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pSD),
  2574. FALSE))
  2575. {
  2576. dwErr = GetLastError();
  2577. goto End;
  2578. }
  2579. if (bKnownObject)
  2580. {
  2581. dwErr = (*(MartaIndexFunctionContext.fGetParentName))(
  2582. pObjectName,
  2583. &ParentName
  2584. );
  2585. CONDITIONAL_EXIT(dwErr, End);
  2586. //
  2587. // A null parent name means we have hit the root of the hierarchy.
  2588. //
  2589. if (NULL == ParentName)
  2590. {
  2591. goto End;
  2592. }
  2593. }
  2594. else
  2595. {
  2596. }
  2597. OldName = ParentName;
  2598. //
  2599. // Run thru the list of ancestors as long as
  2600. // 1. we have inherited aces whose ancestor is yet to be found AND
  2601. // 2. we have not yet hit an ancestor that is PROTECTED.
  2602. //
  2603. for (ParentIndex = 1; InheritedAceCnt > 0 && !ProtectedParent; ParentIndex++)
  2604. {
  2605. if (bKnownObject)
  2606. {
  2607. #ifdef MARTA_DEBUG
  2608. wprintf(L"\n\nParentIndex = %d, ParentName = %s\n", ParentIndex, ParentName);
  2609. #endif
  2610. //
  2611. // Get parent security descriptor
  2612. //
  2613. dwErr = AccRewriteGetNamedRights(
  2614. ParentName,
  2615. ObjectType,
  2616. SecurityInfo,
  2617. NULL,
  2618. NULL,
  2619. NULL,
  2620. NULL,
  2621. &pParentSD
  2622. );
  2623. CONDITIONAL_EXIT(dwErr, End);
  2624. }
  2625. else
  2626. {
  2627. // Get the ParentName and ParentSD
  2628. }
  2629. //
  2630. // This might happen if we were to extend the API to support new object
  2631. // types.
  2632. //
  2633. if (NULL == pParentSD)
  2634. {
  2635. dwErr = ERROR_ACCESS_DENIED;
  2636. goto End;
  2637. }
  2638. //
  2639. // Get the Acl
  2640. //
  2641. if (DACL_SECURITY_INFORMATION == SecurityInfo)
  2642. {
  2643. pParentAcl = RtlpDaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pParentSD);
  2644. }
  2645. else
  2646. {
  2647. pParentAcl = RtlpSaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pParentSD);
  2648. }
  2649. //
  2650. // Null acls are really protected.
  2651. //
  2652. if (NULL == pParentAcl)
  2653. {
  2654. break;
  2655. }
  2656. ParentAceCnt = pParentAcl->AceCount;
  2657. if (ParentIndex > 1)
  2658. {
  2659. //
  2660. // This is an ancestor other then the immediate parent. Mask off its
  2661. // inheritable inherited aces as well as those with NP flag in them.
  2662. // Now we will not see them in the inherited part of the acl after
  2663. // calling CreatePrivateObjectSecurityEx.
  2664. // We save on multiple calls to CreatePrivateObjectSecurityEx by
  2665. // masking off the NP aces.
  2666. //
  2667. pParentAce = (PACE_HEADER) FirstAce(pParentAcl);
  2668. for (i = 0; i < ParentAceCnt; pParentAce = (PACE_HEADER) NextAce(pParentAce), i++)
  2669. {
  2670. if (FLAG_ON(pParentAce->AceFlags, INHERITED_ACE) ||
  2671. FLAG_ON(pParentAce->AceFlags, NO_PROPAGATE_INHERIT_ACE))
  2672. {
  2673. pParentAce->AceFlags &= FlagsToKeep;
  2674. }
  2675. }
  2676. }
  2677. else
  2678. {
  2679. //
  2680. // This is the immediate parent. Mask off its inheritable inherited
  2681. // aces so that we will not see them in the inherited part of the
  2682. // acl after calling CreatePrivateObjectSecurityEx.
  2683. //
  2684. pParentAce = (PACE_HEADER) FirstAce(pParentAcl);
  2685. for (i = 0; i < ParentAceCnt; pParentAce = (PACE_HEADER) NextAce(pParentAce), i++)
  2686. {
  2687. if (FLAG_ON(pParentAce->AceFlags, INHERITED_ACE))
  2688. {
  2689. pParentAce->AceFlags &= FlagsToKeep;
  2690. }
  2691. }
  2692. }
  2693. //
  2694. // Merge the Empty SD with the modified parent SD.
  2695. //
  2696. if (!CreatePrivateObjectSecurityWithMultipleInheritance(
  2697. pParentSD,
  2698. &EmptySD,
  2699. &pNewSD,
  2700. pObjectTypeGuid,
  2701. GuidCount,
  2702. Container,
  2703. (SEF_DACL_AUTO_INHERIT | SEF_SACL_AUTO_INHERIT | SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK),
  2704. NULL,
  2705. pGenericMapping
  2706. ))
  2707. {
  2708. dwErr = GetLastError();
  2709. goto End;
  2710. }
  2711. //
  2712. // Get the ChildAcl
  2713. //
  2714. if (DACL_SECURITY_INFORMATION == SecurityInfo)
  2715. {
  2716. pExpectedAcl = RtlpDaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pNewSD);
  2717. }
  2718. else
  2719. {
  2720. pExpectedAcl = RtlpSaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pNewSD);
  2721. }
  2722. if (NULL == pExpectedAcl)
  2723. {
  2724. #ifdef MARTA_DEBUG
  2725. wprintf(L"ExpectedAcl is NULL!!!\n\n");
  2726. #endif
  2727. pExpectedAce = NULL;
  2728. ExpectedAceCnt = 0;
  2729. }
  2730. else
  2731. {
  2732. pExpectedAce = (PACE_HEADER) FirstAce(pExpectedAcl);
  2733. ExpectedAceCnt = pExpectedAcl->AceCount;
  2734. }
  2735. Match = FALSE;
  2736. #ifdef MARTA_DEBUG
  2737. wprintf(L"ExpectedAceCnt = %d, AceCnt = %d\n", ExpectedAceCnt, AceCnt);
  2738. #endif
  2739. //
  2740. // for all expected inherited aces
  2741. // if there's a match with an "avaliable" ace in the supplied ACL
  2742. // Record the name and the level of the ancestor in the output array
  2743. //
  2744. for (i = 0; i < ExpectedAceCnt; pExpectedAce = (PACE_HEADER) NextAce(pExpectedAce), i++)
  2745. {
  2746. pAce = (PACE_HEADER) FirstAce(pAcl);
  2747. for (j = 0; j < AceCnt; pAce = (PACE_HEADER) NextAce(pAce), j++)
  2748. {
  2749. //
  2750. // Skip aces whose ancestor has already been determined.
  2751. //
  2752. if (MARTA_NO_PARENT != pInheritArray[j].GenerationGap)
  2753. {
  2754. #ifdef MARTA_DEBUG
  2755. wprintf(L"Ace %d taken by level %d, name = %s\n", j,
  2756. pInheritArray[j].GenerationGap, pInheritArray[j].AncestorName);
  2757. #endif
  2758. continue;
  2759. }
  2760. #ifdef MARTA_DEBUG
  2761. wprintf(L"Ace matching i = %d, j = %d, Parent = %s\n", i, j, ParentName);
  2762. #endif
  2763. //
  2764. // Check if the aces match.
  2765. //
  2766. if ((pAce->AceSize == pExpectedAce->AceSize) &&
  2767. !memcmp(pAce, pExpectedAce, pAce->AceSize))
  2768. {
  2769. #ifdef MARTA_DEBUG
  2770. wprintf(L"Ace match found i = %d, j = %d, left = %d, Parent = %s\n",
  2771. i, j, InheritedAceCnt, ParentName);
  2772. #endif
  2773. //
  2774. // Record the name and level of the parent.
  2775. //
  2776. pInheritArray[j].GenerationGap = ParentIndex;
  2777. pInheritArray[j].AncestorName = ParentName;
  2778. //
  2779. // A match has been found.
  2780. //
  2781. Match = TRUE;
  2782. //
  2783. // Decrement the "available" ace count.
  2784. //
  2785. InheritedAceCnt--;
  2786. break;
  2787. }
  2788. }
  2789. }
  2790. //
  2791. // Check if the ancestor is protected. The loop stops after we have
  2792. // processed a protected ancestor.
  2793. //
  2794. ProtectedParent = FLAG_ON(((PISECURITY_DESCRIPTOR) pParentSD)->Control, ProtectedBit);
  2795. if (NULL != pParentSD)
  2796. {
  2797. if (bKnownObject)
  2798. {
  2799. LocalFree(pParentSD);
  2800. }
  2801. else
  2802. {
  2803. }
  2804. pParentSD = NULL;
  2805. }
  2806. ParentName = NULL;
  2807. //
  2808. // Get the name of the next ancestor only if we still need to continue.
  2809. //
  2810. if (InheritedAceCnt > 0 && !ProtectedParent)
  2811. {
  2812. dwErr = (*(MartaIndexFunctionContext.fGetParentName))(
  2813. OldName,
  2814. &ParentName
  2815. );
  2816. }
  2817. //
  2818. // Free the ancestor name if no aces were inherited from it.
  2819. //
  2820. if (!Match)
  2821. {
  2822. if (bKnownObject)
  2823. {
  2824. LocalFree(OldName);
  2825. }
  2826. else
  2827. {
  2828. }
  2829. }
  2830. OldName = NULL;
  2831. CONDITIONAL_EXIT(dwErr, End);
  2832. //
  2833. // A null parent name means we have hit the root of the hierarchy.
  2834. //
  2835. if (NULL == ParentName)
  2836. {
  2837. break;
  2838. }
  2839. OldName = ParentName;
  2840. }
  2841. End:
  2842. if (bKnownObject)
  2843. {
  2844. if (ERROR_SUCCESS != dwErr)
  2845. {
  2846. AccFreeIndexArray(pInheritArray, AceCnt, NULL);
  2847. }
  2848. if (NULL != pSD)
  2849. {
  2850. LocalFree(pSD);
  2851. }
  2852. if (NULL != OldName)
  2853. {
  2854. LocalFree(OldName);
  2855. }
  2856. if (NULL != pParentSD)
  2857. {
  2858. LocalFree(pParentSD);
  2859. }
  2860. }
  2861. else
  2862. {
  2863. }
  2864. return dwErr;
  2865. }
  2866. #ifdef MAX_INDEX_LEVEL
  2867. #undef MAX_INDEX_LEVEL
  2868. #endif
  2869. #define MAX_INDEX_LEVEL 10
  2870. ////////////////////////////////////////////////////////////////////////////////
  2871. // //
  2872. // Function: AccFreeIndexArray //
  2873. // //
  2874. // Description: Free the strings allocated and stored in the array. //
  2875. // //
  2876. // Arguments: //
  2877. // //
  2878. // [IN OUT pInheritArray] Array to free results from //
  2879. // [IN AceCnt] Number of elements in the array //
  2880. // [IN pfnFree] Function to use for freeing //
  2881. // //
  2882. // Algorithm: //
  2883. // Note that there is a single allocated string for all nodes at the same //
  2884. // level. //
  2885. // In (1, p1), (2, p2), (3, p3), (1, p1) (1, p1) (2, p2) //
  2886. // we free just three strings p1, p2 and p3. //
  2887. // Initialize the boolean 'freed' array to TRUE //
  2888. // For all elements of the array, //
  2889. // if (InheritedAce AND Ancestor name non-NULL AND Not already freed) //
  2890. // Mark as Freed and free the string. //
  2891. // //
  2892. // Returns: ERROR_SUCCESS on successful completion of the routine //
  2893. // ERROR_XXXX Otherwise //
  2894. // //
  2895. ////////////////////////////////////////////////////////////////////////////////
  2896. DWORD
  2897. AccFreeIndexArray(
  2898. IN OUT PINHERITED_FROMW pInheritArray,
  2899. IN USHORT AceCnt,
  2900. IN PFN_FREE pfnFree
  2901. )
  2902. {
  2903. USHORT i = 0;
  2904. LONG MaxLevel = 0;
  2905. BOOL LevelBuffer[MAX_INDEX_LEVEL + 1];
  2906. PBOOL pLevelStatus = (PBOOL) LevelBuffer;
  2907. for (i = 0; i < AceCnt; i++)
  2908. {
  2909. if (pInheritArray[i].GenerationGap > MaxLevel)
  2910. {
  2911. MaxLevel = pInheritArray[i].GenerationGap;
  2912. }
  2913. }
  2914. if (MaxLevel <= 0)
  2915. {
  2916. return ERROR_SUCCESS;
  2917. }
  2918. if (MaxLevel > MAX_INDEX_LEVEL)
  2919. {
  2920. pLevelStatus = (PBOOL) AccAlloc(sizeof(BOOL) * (MaxLevel + 1));
  2921. if (NULL == pLevelStatus)
  2922. {
  2923. return ERROR_NOT_ENOUGH_MEMORY;
  2924. }
  2925. }
  2926. for (i = 0; i <= MaxLevel; i++)
  2927. {
  2928. pLevelStatus[i] = TRUE;
  2929. }
  2930. for (i = 0; i < AceCnt; i++)
  2931. {
  2932. if ((pInheritArray[i].GenerationGap > 0) &&
  2933. (NULL != pInheritArray[i].AncestorName) &&
  2934. (pLevelStatus[pInheritArray[i].GenerationGap]))
  2935. {
  2936. pLevelStatus[pInheritArray[i].GenerationGap] = FALSE;
  2937. if (NULL == pfnFree)
  2938. {
  2939. AccFree(pInheritArray[i].AncestorName);
  2940. }
  2941. else
  2942. {
  2943. pfnFree(pInheritArray[i].AncestorName);
  2944. }
  2945. }
  2946. }
  2947. if (pLevelStatus != (PBOOL) LevelBuffer)
  2948. {
  2949. AccFree(pLevelStatus);
  2950. }
  2951. return ERROR_SUCCESS;
  2952. }
  2953. ////////////////////////////////////////////////////////////////////////////////
  2954. // //
  2955. // Function: MartaResetTree //
  2956. // //
  2957. // Description: Reset permissions on the subtree starting at pObjectName. //
  2958. // //
  2959. // Arguments: //
  2960. // //
  2961. // [IN pObjectName] Name of the Object //
  2962. // [IN ObjectType] Type of the object //
  2963. // [IN SecurityInfo] Security info to reset //
  2964. // [IN pOwner] Owner sid to reset //
  2965. // [IN pGroup] Group sid to reset //
  2966. // [IN pDacl] Dacl to reset //
  2967. // [IN pSacl] Sacl to reset //
  2968. // [IN KeepExplicit] if TRUE, retain explicit aces on the subtree, not //
  2969. // including the object. //
  2970. // [IN fnProgress] Caller supplied callback function //
  2971. // [IN pOperation] To determine if callback should be invoked. //
  2972. // callback function. //
  2973. // [IN Args] Arguments supplied by the caller //
  2974. // //
  2975. // Returns: TRUE if reset succeeded. //
  2976. // FALSE o/w //
  2977. // //
  2978. ////////////////////////////////////////////////////////////////////////////////
  2979. DWORD
  2980. AccTreeResetNamedSecurityInfo(
  2981. IN LPWSTR pObjectName,
  2982. IN SE_OBJECT_TYPE ObjectType,
  2983. IN SECURITY_INFORMATION SecurityInfo,
  2984. IN PSID pOwner OPTIONAL,
  2985. IN PSID pGroup OPTIONAL,
  2986. IN PACL pDacl OPTIONAL,
  2987. IN PACL pSacl OPTIONAL,
  2988. IN BOOL KeepExplicit,
  2989. IN FN_PROGRESS fnProgress OPTIONAL,
  2990. IN PROG_INVOKE_SETTING ProgressInvokeSetting,
  2991. IN PVOID Args OPTIONAL
  2992. )
  2993. {
  2994. MARTA_OBJECT_TYPE_PROPERTIES ObjectTypeProperties;
  2995. MARTA_SET_FUNCTION_CONTEXT MartaSetFunctionContext;
  2996. MARTA_OBJECT_PROPERTIES ObjectProperties;
  2997. SECURITY_DESCRIPTOR TmpSD;
  2998. UCHAR Buffer[2 * sizeof(ACL)];
  2999. ACL EmptyDacl;
  3000. ACL EmptySacl;
  3001. SECURITY_INFORMATION LocalSeInfo = 0;
  3002. PSID LocalGroup = pGroup;
  3003. PSID LocalOwner = pOwner;
  3004. ACCESS_MASK LocalAccessMask = 0;
  3005. MARTA_CONTEXT Context = NULL_MARTA_CONTEXT;
  3006. MARTA_CONTEXT ParentContext = NULL_MARTA_CONTEXT;
  3007. GENERIC_MAPPING ZeroGenMap = {0, 0, 0, 0};
  3008. PSECURITY_DESCRIPTOR pOldSD = NULL;
  3009. PSECURITY_DESCRIPTOR pParentSD = NULL;
  3010. PSECURITY_DESCRIPTOR pNewSD = NULL;
  3011. DWORD dwErr = ERROR_SUCCESS;
  3012. BOOL bIsContainer = FALSE;
  3013. HANDLE ProcessHandle = NULL;
  3014. HANDLE ThreadHandle = NULL;
  3015. ACCESS_MASK AccessMask = 0;
  3016. ACCESS_MASK RetryAccessMask = 0;
  3017. ACCESS_MASK MaxAccessMask = 0;
  3018. BOOL bRetry = FALSE;
  3019. BOOL bSetWorked = FALSE;
  3020. PROG_INVOKE_SETTING Operation = ProgressInvokeSetting;
  3021. SECURITY_INFORMATION TmpSeInfo = (OWNER_SECURITY_INFORMATION |
  3022. GROUP_SECURITY_INFORMATION );
  3023. LPWSTR NewObjectName = NULL;
  3024. //
  3025. // Convert the file object name into a dos name.
  3026. // For all other objects, use the name as supplied.
  3027. //
  3028. if (SE_FILE_OBJECT == ObjectType)
  3029. {
  3030. UNICODE_STRING FileName;
  3031. RTL_RELATIVE_NAME RelativeName;
  3032. if (!RtlDosPathNameToNtPathName_U(
  3033. pObjectName,
  3034. &FileName,
  3035. NULL,
  3036. &RelativeName
  3037. ))
  3038. {
  3039. return ERROR_INVALID_NAME;
  3040. }
  3041. if (RelativeName.RelativeName.Length)
  3042. {
  3043. FileName = *(PUNICODE_STRING)&RelativeName.RelativeName;
  3044. }
  3045. NewObjectName = FileName.Buffer;
  3046. }
  3047. else
  3048. {
  3049. NewObjectName = pObjectName;
  3050. }
  3051. //
  3052. // TmpSeInfo records whether Owner + Group sids have been supplied by the
  3053. // caller.
  3054. //
  3055. TmpSeInfo = (SecurityInfo & TmpSeInfo) ^ TmpSeInfo;
  3056. //
  3057. // Initialize the dummy security descriptor. This may be changed by the
  3058. // recursive calls.
  3059. //
  3060. InitializeSecurityDescriptor(&TmpSD, SECURITY_DESCRIPTOR_REVISION);
  3061. //
  3062. // Initialize the function pointers based on the object type.
  3063. //
  3064. MartaInitializeSetContext(ObjectType, &MartaSetFunctionContext);
  3065. //
  3066. // Basic error checks for owner and group.
  3067. //
  3068. if (FLAG_ON(SecurityInfo, OWNER_SECURITY_INFORMATION))
  3069. {
  3070. if ((NULL == pOwner) || !RtlValidSid(pOwner))
  3071. {
  3072. return ERROR_INVALID_SID;
  3073. }
  3074. }
  3075. if (FLAG_ON(SecurityInfo, GROUP_SECURITY_INFORMATION))
  3076. {
  3077. if ((NULL == pGroup) || !RtlValidSid(pGroup))
  3078. {
  3079. return ERROR_INVALID_SID;
  3080. }
  3081. }
  3082. //
  3083. // For both DACL and SACL:
  3084. // If the caller requested for inheritance blocking
  3085. // set the appropriate bit in TmpSD
  3086. // else
  3087. // note that the parent Acl should be read for computing inherited aces
  3088. // Do basic error checks on the Acl.
  3089. //
  3090. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  3091. {
  3092. if (FLAG_ON(SecurityInfo, PROTECTED_DACL_SECURITY_INFORMATION))
  3093. {
  3094. TmpSD.Control |= SE_DACL_PROTECTED;
  3095. }
  3096. else
  3097. {
  3098. LocalSeInfo |= DACL_SECURITY_INFORMATION;
  3099. }
  3100. if ((NULL == pDacl) || !RtlValidAcl(pDacl))
  3101. {
  3102. return ERROR_INVALID_ACL;
  3103. }
  3104. if (FALSE == SetSecurityDescriptorDacl(&TmpSD, TRUE, pDacl, FALSE))
  3105. {
  3106. return GetLastError();
  3107. }
  3108. }
  3109. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  3110. {
  3111. if (FLAG_ON(SecurityInfo, PROTECTED_SACL_SECURITY_INFORMATION))
  3112. {
  3113. TmpSD.Control |= SE_SACL_PROTECTED;
  3114. }
  3115. else
  3116. {
  3117. LocalSeInfo |= SACL_SECURITY_INFORMATION;
  3118. }
  3119. if ((NULL == pSacl) || !RtlValidAcl(pSacl))
  3120. {
  3121. return ERROR_INVALID_ACL;
  3122. }
  3123. if (FALSE == SetSecurityDescriptorSacl(&TmpSD, TRUE, pSacl, FALSE))
  3124. {
  3125. return GetLastError();
  3126. }
  3127. }
  3128. if (FLAG_ON(SecurityInfo, (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)))
  3129. {
  3130. //
  3131. // We need read as well as write access to take care of CO/CG.
  3132. //
  3133. MaxAccessMask = (*(MartaSetFunctionContext.fGetDesiredAccess))(
  3134. MODIFY_ACCESS_RIGHTS,
  3135. TRUE,
  3136. SecurityInfo
  3137. );
  3138. AccessMask = (*(MartaSetFunctionContext.fGetDesiredAccess))(
  3139. MODIFY_ACCESS_RIGHTS,
  3140. FALSE,
  3141. SecurityInfo
  3142. );
  3143. }
  3144. else
  3145. {
  3146. //
  3147. // We only need WRITE access if we are not setting any ACL.
  3148. //
  3149. MaxAccessMask = (*(MartaSetFunctionContext.fGetDesiredAccess))(
  3150. WRITE_ACCESS_RIGHTS,
  3151. TRUE,
  3152. SecurityInfo
  3153. );
  3154. AccessMask = (*(MartaSetFunctionContext.fGetDesiredAccess))(
  3155. WRITE_ACCESS_RIGHTS,
  3156. FALSE,
  3157. SecurityInfo
  3158. );
  3159. }
  3160. RetryAccessMask = (*(MartaSetFunctionContext.fGetDesiredAccess))(
  3161. NO_ACCESS_RIGHTS,
  3162. TRUE,
  3163. SecurityInfo
  3164. );
  3165. //
  3166. // Open the object for maximum access that may be needed for computing
  3167. // new SD, setting it on the object and Listing the subtree below this node.
  3168. //
  3169. dwErr = (*(MartaSetFunctionContext.fOpenNamedObject))(
  3170. pObjectName,
  3171. MaxAccessMask,
  3172. &Context
  3173. );
  3174. if (ERROR_SUCCESS != dwErr)
  3175. {
  3176. //
  3177. // We did not have List permission. Open the object for computing the
  3178. // new SD and setting it on the object.
  3179. //
  3180. dwErr = (*(MartaSetFunctionContext.fOpenNamedObject))(
  3181. pObjectName,
  3182. AccessMask,
  3183. &Context
  3184. );
  3185. CONDITIONAL_EXIT(dwErr, End);
  3186. //
  3187. // Note that we must retry List and propagate on the subtree.
  3188. //
  3189. bRetry = TRUE;
  3190. }
  3191. //
  3192. // Read the object atributes to figure out whether the object is a container.
  3193. //
  3194. ObjectProperties.cbSize = sizeof(ObjectProperties);
  3195. ObjectProperties.dwFlags = 0;
  3196. dwErr = (*(MartaSetFunctionContext.fGetProperties))(
  3197. Context,
  3198. &ObjectProperties
  3199. );
  3200. CONDITIONAL_EXIT(dwErr, End);
  3201. bIsContainer = FLAG_ON(ObjectProperties.dwFlags, MARTA_OBJECT_IS_CONTAINER);
  3202. //
  3203. // LocalSeInfo != corresponds to
  3204. // Resetting ACL(s) in LocalSeInfo and they should not be PROTECTED.
  3205. //
  3206. // Read the existing ACL(s) on the parent.
  3207. //
  3208. if (0 != LocalSeInfo)
  3209. {
  3210. LocalAccessMask = (*(MartaSetFunctionContext.fGetDesiredAccess))(
  3211. READ_ACCESS_RIGHTS,
  3212. FALSE,
  3213. LocalSeInfo
  3214. );
  3215. dwErr = (*(MartaSetFunctionContext.fGetParentContext))(
  3216. Context,
  3217. LocalAccessMask,
  3218. &ParentContext
  3219. );
  3220. CONDITIONAL_EXIT(dwErr, End);
  3221. if (NULL != ParentContext)
  3222. {
  3223. dwErr = (*(MartaSetFunctionContext.fGetRights))(
  3224. ParentContext,
  3225. SecurityInfo,
  3226. &pParentSD
  3227. );
  3228. (VOID) (*(MartaSetFunctionContext.fCloseContext))(ParentContext);
  3229. CONDITIONAL_EXIT(dwErr, End);
  3230. }
  3231. }
  3232. if (FLAG_ON(SecurityInfo, (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)))
  3233. {
  3234. //
  3235. // Read the owner and group info from the object if it has not been
  3236. // supplied by the caller.
  3237. //
  3238. if (!((FLAG_ON(SecurityInfo, OWNER_SECURITY_INFORMATION)) &&
  3239. (FLAG_ON(SecurityInfo, GROUP_SECURITY_INFORMATION))))
  3240. {
  3241. dwErr = (*(MartaSetFunctionContext.fGetRights))(
  3242. Context,
  3243. TmpSeInfo,
  3244. &pOldSD
  3245. );
  3246. CONDITIONAL_EXIT(dwErr, End);
  3247. }
  3248. //
  3249. // If Owner sid has not been provided by the caller, pick it up from the
  3250. // existing security descriptor.
  3251. //
  3252. LocalOwner = pOwner;
  3253. if (NULL == LocalOwner)
  3254. {
  3255. LocalOwner = RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldSD);
  3256. }
  3257. //
  3258. // Set the owner sid in the TmpSd.
  3259. //
  3260. if (FALSE == SetSecurityDescriptorOwner(
  3261. &TmpSD,
  3262. LocalOwner,
  3263. FALSE))
  3264. {
  3265. dwErr = GetLastError();
  3266. CONDITIONAL_EXIT(dwErr, End);
  3267. }
  3268. //
  3269. // If Group sid has not been provided by the caller, pick it up from the
  3270. // existing security descriptor.
  3271. //
  3272. if (NULL == LocalGroup)
  3273. {
  3274. LocalGroup = RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldSD);
  3275. }
  3276. //
  3277. // Set the group sid in the TmpSd.
  3278. //
  3279. if (FALSE == SetSecurityDescriptorGroup(
  3280. &TmpSD,
  3281. LocalGroup,
  3282. FALSE))
  3283. {
  3284. dwErr = GetLastError();
  3285. CONDITIONAL_EXIT(dwErr, End);
  3286. }
  3287. dwErr = GetCurrentToken(&ProcessHandle);
  3288. CONDITIONAL_EXIT(dwErr, End);
  3289. ObjectTypeProperties.cbSize = sizeof(ObjectTypeProperties);
  3290. ObjectTypeProperties.dwFlags = 0;
  3291. ObjectTypeProperties.GenMap = ZeroGenMap;
  3292. //
  3293. // Get information regarding the object type.
  3294. //
  3295. dwErr = (*(MartaSetFunctionContext.fGetTypeProperties))(&ObjectTypeProperties);
  3296. CONDITIONAL_EXIT(dwErr, End);
  3297. MARTA_TURN_OFF_IMPERSONATION;
  3298. //
  3299. // Compute the new security descriptor.
  3300. //
  3301. if (FALSE == CreatePrivateObjectSecurityEx(
  3302. pParentSD,
  3303. &TmpSD,
  3304. &pNewSD,
  3305. NULL,
  3306. bIsContainer,
  3307. (SEF_DACL_AUTO_INHERIT | SEF_SACL_AUTO_INHERIT | SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK),
  3308. ProcessHandle,
  3309. &ObjectTypeProperties.GenMap
  3310. ))
  3311. {
  3312. dwErr = GetLastError();
  3313. }
  3314. MARTA_TURN_ON_IMPERSONATION;
  3315. CONDITIONAL_EXIT(dwErr, End);
  3316. //
  3317. // Set the owner and the group fields in TmpSD to NULL if the caller
  3318. // did not want to set these.
  3319. //
  3320. if (NULL == pOwner)
  3321. {
  3322. if (FALSE == SetSecurityDescriptorOwner(
  3323. &TmpSD,
  3324. NULL,
  3325. FALSE))
  3326. {
  3327. dwErr = GetLastError();
  3328. }
  3329. CONDITIONAL_EXIT(dwErr, End);
  3330. }
  3331. if (NULL == pGroup)
  3332. {
  3333. if (FALSE == SetSecurityDescriptorGroup(
  3334. &TmpSD,
  3335. NULL,
  3336. FALSE))
  3337. {
  3338. dwErr = GetLastError();
  3339. }
  3340. CONDITIONAL_EXIT(dwErr, End);
  3341. }
  3342. //
  3343. // Set the DACL to Empty if the caller requested for resetting the DACL.
  3344. //
  3345. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  3346. {
  3347. if (FALSE == InitializeAcl(&EmptyDacl, sizeof(ACL), ACL_REVISION))
  3348. {
  3349. return ERROR_ACCESS_DENIED;
  3350. }
  3351. if (FALSE == SetSecurityDescriptorDacl(
  3352. &TmpSD,
  3353. TRUE,
  3354. &EmptyDacl,
  3355. FALSE))
  3356. {
  3357. return GetLastError();
  3358. }
  3359. }
  3360. //
  3361. // Set the SACL to Empty if the caller requested for resetting the SACL.
  3362. //
  3363. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  3364. {
  3365. if (FALSE == InitializeAcl(&EmptySacl, sizeof(ACL), ACL_REVISION))
  3366. {
  3367. return ERROR_ACCESS_DENIED;
  3368. }
  3369. if (FALSE == SetSecurityDescriptorSacl(
  3370. &TmpSD,
  3371. TRUE,
  3372. &EmptySacl,
  3373. FALSE))
  3374. {
  3375. return GetLastError();
  3376. }
  3377. }
  3378. //
  3379. // We now have TmpSD with
  3380. // Owner Sid if SecurityInfo contains OWNER_SECURITY_INFORMATION
  3381. // Group Sid if SecurityInfo contains GROUP_SECURITY_INFORMATION
  3382. // Empty DACL if SecurityInfo contains DACL_SECURITY_INFORMATION
  3383. // Empty SACL if SecurityInfo contains SACL_SECURITY_INFORMATION
  3384. //
  3385. }
  3386. else
  3387. {
  3388. //
  3389. // The caller requested for resetting owner and/or group.
  3390. // Set these in the TmpSD which will be passed to the recursive routine.
  3391. //
  3392. if (FLAG_ON(SecurityInfo, OWNER_SECURITY_INFORMATION))
  3393. {
  3394. if (FALSE == SetSecurityDescriptorOwner(
  3395. &TmpSD,
  3396. LocalOwner,
  3397. FALSE))
  3398. {
  3399. dwErr = GetLastError();
  3400. }
  3401. CONDITIONAL_EXIT(dwErr, End);
  3402. }
  3403. if (FLAG_ON(SecurityInfo, GROUP_SECURITY_INFORMATION))
  3404. {
  3405. if (FALSE == SetSecurityDescriptorGroup(
  3406. &TmpSD,
  3407. LocalGroup,
  3408. FALSE))
  3409. {
  3410. dwErr = GetLastError();
  3411. }
  3412. CONDITIONAL_EXIT(dwErr, End);
  3413. }
  3414. //
  3415. // This is also out New SD to set on the object.
  3416. //
  3417. pNewSD = &TmpSD;
  3418. }
  3419. //
  3420. // If the child is a container then update the subtree underneath it.
  3421. //
  3422. if (bIsContainer)
  3423. {
  3424. TmpSD.Control &= ~(SE_DACL_PROTECTED | SE_SACL_PROTECTED);
  3425. if (!bRetry)
  3426. {
  3427. bRetry = MartaResetTree(
  3428. SecurityInfo,
  3429. TmpSeInfo,
  3430. pNewSD,
  3431. &TmpSD,
  3432. Context,
  3433. ProcessHandle,
  3434. &MartaSetFunctionContext,
  3435. &ObjectTypeProperties.GenMap,
  3436. MaxAccessMask,
  3437. AccessMask,
  3438. RetryAccessMask,
  3439. &Operation,
  3440. fnProgress,
  3441. Args,
  3442. KeepExplicit
  3443. );
  3444. }
  3445. }
  3446. else
  3447. {
  3448. bRetry = FALSE;
  3449. }
  3450. //
  3451. // Set the computed security descriptor on the object.
  3452. //
  3453. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  3454. {
  3455. ((PISECURITY_DESCRIPTOR) pNewSD)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  3456. }
  3457. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  3458. {
  3459. ((PISECURITY_DESCRIPTOR) pNewSD)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  3460. }
  3461. dwErr = (*(MartaSetFunctionContext.fSetRights))(
  3462. Context,
  3463. SecurityInfo,
  3464. pNewSD
  3465. );
  3466. CONDITIONAL_EXIT(dwErr, End);
  3467. //
  3468. // Note that the set operation on the object is successful.
  3469. //
  3470. bSetWorked = TRUE;
  3471. //
  3472. // Abort if the progress function requested a "Cancel" in the subtree
  3473. // below this node. The value is propagated all the way to the root as
  3474. // the stack unwinds.
  3475. //
  3476. if (ProgressCancelOperation == Operation)
  3477. {
  3478. goto End;
  3479. }
  3480. //
  3481. // If propagation had failed in the first attept then try again. This is to
  3482. // cover the case when the container can be enumerated after setting the new
  3483. // security.
  3484. //
  3485. if (bRetry && bIsContainer && (SecurityInfo & DACL_SECURITY_INFORMATION))
  3486. {
  3487. Retry:
  3488. bRetry = FALSE;
  3489. //
  3490. // Reopen the object for List and retry on the subtree. Note that for
  3491. // file objects this is just a dummy routine. The actual reopen happens
  3492. // in FindFirst.
  3493. //
  3494. dwErr = (*(MartaSetFunctionContext.fReopenOrigContext))(
  3495. Context,
  3496. RetryAccessMask
  3497. );
  3498. CONDITIONAL_EXIT(dwErr, End);
  3499. bRetry = MartaResetTree(
  3500. SecurityInfo,
  3501. TmpSeInfo,
  3502. pNewSD,
  3503. &TmpSD,
  3504. Context,
  3505. ProcessHandle,
  3506. &MartaSetFunctionContext,
  3507. &ObjectTypeProperties.GenMap,
  3508. MaxAccessMask,
  3509. AccessMask,
  3510. RetryAccessMask,
  3511. &Operation,
  3512. fnProgress,
  3513. Args,
  3514. KeepExplicit
  3515. );
  3516. //
  3517. // Retry failed. We should give a callback stating enum failed.
  3518. //
  3519. if (bRetry)
  3520. {
  3521. switch (Operation)
  3522. {
  3523. case ProgressInvokeNever:
  3524. break;
  3525. case ProgressInvokeOnError:
  3526. if (ERROR_SUCCESS == dwErr)
  3527. {
  3528. break;
  3529. }
  3530. //
  3531. // Fallthrough is intended!!
  3532. //
  3533. case ProgressInvokeEveryObject:
  3534. if (NULL != fnProgress)
  3535. {
  3536. fnProgress(
  3537. NewObjectName,
  3538. ERROR_ACCESS_DENIED,
  3539. &Operation,
  3540. Args,
  3541. TRUE
  3542. );
  3543. //
  3544. // This was the latest feature request by HiteshR. At this
  3545. // point, retry has failed, but the caller has made some
  3546. // changes and expects retry to work okay now.
  3547. //
  3548. if (ProgressRetryOperation == Operation)
  3549. {
  3550. Operation = ProgressInvokeEveryObject;
  3551. goto Retry;
  3552. }
  3553. }
  3554. break;
  3555. default:
  3556. break;
  3557. }
  3558. }
  3559. }
  3560. End:
  3561. if (bRetry && bIsContainer)
  3562. {
  3563. dwErr = ERROR_ACCESS_DENIED;
  3564. }
  3565. switch (Operation)
  3566. {
  3567. case ProgressInvokeNever:
  3568. break;
  3569. case ProgressInvokeOnError:
  3570. if (ERROR_SUCCESS == dwErr)
  3571. {
  3572. break;
  3573. }
  3574. //
  3575. // Fallthrough is intended!!
  3576. //
  3577. case ProgressInvokeEveryObject:
  3578. if (NULL != fnProgress)
  3579. {
  3580. fnProgress(
  3581. NewObjectName,
  3582. dwErr,
  3583. &Operation,
  3584. Args,
  3585. bSetWorked
  3586. );
  3587. //
  3588. // This was the latest feature request by HiteshR. At this
  3589. // point, retry has failed, but the caller has made some
  3590. // changes and expects retry to work okay now.
  3591. //
  3592. if (ProgressRetryOperation == Operation)
  3593. {
  3594. Operation = ProgressInvokeEveryObject;
  3595. goto Retry;
  3596. }
  3597. }
  3598. break;
  3599. default:
  3600. break;
  3601. }
  3602. if (NULL != ProcessHandle)
  3603. {
  3604. CloseHandle(ProcessHandle);
  3605. }
  3606. if (NULL != pOldSD)
  3607. {
  3608. AccFree(pOldSD);
  3609. }
  3610. if (NULL != pParentSD)
  3611. {
  3612. AccFree(pParentSD);
  3613. }
  3614. if ((NULL != pNewSD) && (&TmpSD != pNewSD))
  3615. {
  3616. DestroyPrivateObjectSecurity(&pNewSD);
  3617. }
  3618. if (NULL != Context)
  3619. {
  3620. (VOID) (*(MartaSetFunctionContext.fCloseContext))(Context);
  3621. }
  3622. if (NewObjectName != pObjectName)
  3623. {
  3624. RtlFreeHeap(RtlProcessHeap(), 0, NewObjectName);
  3625. }
  3626. return dwErr;
  3627. }
  3628. ////////////////////////////////////////////////////////////////////////////////
  3629. // //
  3630. // Function: MartaResetTree //
  3631. // //
  3632. // Description: Reset permissions on the subtree below the node represented //
  3633. // by Context.
  3634. // //
  3635. // Arguments: //
  3636. // //
  3637. // [IN SecurityInfo] Security info to reset //
  3638. // [IN TmpSeInfo] Info that needs to be read from the //
  3639. // object //
  3640. // [IN pNewSD] New security Descriptor on the parent //
  3641. // [IN pEmptySD] Security Descriptor with owner/group //
  3642. // [IN Context] Context for the root of the subtree //
  3643. // [IN ProcessHandle] Handle to the process token //
  3644. // [IN pMartaSetFunctionContext] Struct holding function pointers //
  3645. // [IN pGenMap] Generic mapping for the object //
  3646. // [IN MaxAccessMask] Desired access mask for R, W, List //
  3647. // [IN AccessMask] Desired access mask for W //
  3648. // [IN RetryMask] Desired access mask for List //
  3649. // [IN OUT pOperation] To determine if callback should be //
  3650. // invoked. Value may be changed by the //
  3651. // callback function. //
  3652. // [IN Args] Arguments supplied by the caller //
  3653. // [IN fnProgress] Caller supplied callback function //
  3654. // [IN KeepExplicit] if TRUE, retain explicit aces //
  3655. // //
  3656. // //
  3657. // Algorithm: //
  3658. // Open the first child for R, W, List //
  3659. // if open failed //
  3660. // Try again for just R, W and note that a retry is needed. //
  3661. // Return TRUE if no children exist. //
  3662. // Return FALSE if we can not list //
  3663. // for all children //
  3664. // If resetting xAcl (and maybe owner/group) //
  3665. // if KeepExplicit //
  3666. // read old xAcl and (owner/group if not provided by the caller) //
  3667. // else //
  3668. // read owner/group if not provided by the caller) //
  3669. // Compute NewChildSD using this computed info and new parent SD //
  3670. // else //
  3671. // NewChildSD = EmptySD //
  3672. // Invoke callback depending on the flag. //
  3673. // Retry propagation if it failed the first time. //
  3674. // If at any time, the callback function requests a cancel //
  3675. // Abort. //
  3676. //
  3677. // Returns: TRUE if propagation succeeded //
  3678. // //
  3679. ////////////////////////////////////////////////////////////////////////////////
  3680. BOOL
  3681. MartaResetTree(
  3682. IN SECURITY_INFORMATION SecurityInfo,
  3683. IN SECURITY_INFORMATION TmpSeInfo,
  3684. IN PSECURITY_DESCRIPTOR pNewSD,
  3685. IN PSECURITY_DESCRIPTOR pEmptySD,
  3686. IN MARTA_CONTEXT Context,
  3687. IN HANDLE ProcessHandle,
  3688. IN PMARTA_SET_FUNCTION_CONTEXT pMartaSetFunctionContext,
  3689. IN PGENERIC_MAPPING pGenMap,
  3690. IN ACCESS_MASK MaxAccessMask,
  3691. IN ACCESS_MASK AccessMask,
  3692. IN ACCESS_MASK RetryAccessMask,
  3693. IN OUT PPROG_INVOKE_SETTING pOperation,
  3694. IN FN_PROGRESS fnProgress,
  3695. IN PVOID Args,
  3696. IN BOOL KeepExplicit
  3697. )
  3698. {
  3699. MARTA_OBJECT_PROPERTIES ObjectProperties;
  3700. MARTA_CONTEXT ChildContext = NULL_MARTA_CONTEXT;
  3701. PSECURITY_DESCRIPTOR pNewChildSD = NULL;
  3702. PSECURITY_DESCRIPTOR pOldChildSD = NULL;
  3703. DWORD dwErr = ERROR_SUCCESS;
  3704. BOOL bIsContainer = FALSE;
  3705. HANDLE ThreadHandle = NULL;
  3706. BOOL bRetry = FALSE;
  3707. BOOL bSetWorked = FALSE;
  3708. //
  3709. // Get the first child of this container. In the first attempt try to open
  3710. // the child with read/write as well as list.
  3711. //
  3712. dwErr = (*(pMartaSetFunctionContext->fFindFirst))(
  3713. Context,
  3714. MaxAccessMask,
  3715. &ChildContext
  3716. );
  3717. if (ERROR_SUCCESS != dwErr)
  3718. {
  3719. #ifdef MARTA_DEBUG
  3720. wprintf(L"FindFirst failed\n");
  3721. #endif
  3722. if (NULL == ChildContext)
  3723. {
  3724. //
  3725. // This should never happen. A NULL ChildContext represents no
  3726. // more children. We have this code path just in case some resource
  3727. // manager cannot open the object for list.
  3728. //
  3729. dwErr = (*(pMartaSetFunctionContext->fFindFirst))(
  3730. Context,
  3731. AccessMask,
  3732. &ChildContext
  3733. );
  3734. }
  3735. else
  3736. {
  3737. //
  3738. // Try opening the child again, this time with permissions sufficent
  3739. // for computing security info to set and setting it.
  3740. //
  3741. dwErr = (*(pMartaSetFunctionContext->fReopenContext))(
  3742. ChildContext,
  3743. AccessMask
  3744. );
  3745. }
  3746. //
  3747. // We failed to open the object for list. Record this failure and open
  3748. // the child again if it is a container.
  3749. //
  3750. bRetry = TRUE;
  3751. }
  3752. else
  3753. {
  3754. #ifdef MARTA_DEBUG
  3755. wprintf(L"FindFirst succeeded\n");
  3756. #endif
  3757. }
  3758. if (NULL == ChildContext)
  3759. {
  3760. //
  3761. // The parent does not have any children.
  3762. //
  3763. if (ERROR_SUCCESS == dwErr)
  3764. {
  3765. #ifdef MARTA_DEBUG
  3766. wprintf(L"The container does not have any children\n");
  3767. #endif
  3768. return FALSE;
  3769. }
  3770. #ifdef MARTA_DEBUG
  3771. wprintf(L"Can not list objects in the current container. Retry needed\n");
  3772. #endif
  3773. //
  3774. // We need a propagation retry for the parent.
  3775. //
  3776. return TRUE;
  3777. }
  3778. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3779. //
  3780. // Child context becomes NULL when there are no more children.
  3781. //
  3782. while (ChildContext)
  3783. {
  3784. ObjectProperties.cbSize = sizeof(ObjectProperties);
  3785. ObjectProperties.dwFlags = 0;
  3786. //
  3787. // Get information about the current child. We need to know whether
  3788. // it is a container.
  3789. //
  3790. //
  3791. dwErr = (*(pMartaSetFunctionContext->fGetProperties))(
  3792. ChildContext,
  3793. &ObjectProperties
  3794. );
  3795. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3796. #ifdef MARTA_DEBUG
  3797. wprintf(L"GetProperties succeeded\n");
  3798. #endif
  3799. bIsContainer = FLAG_ON(ObjectProperties.dwFlags, MARTA_OBJECT_IS_CONTAINER);
  3800. //
  3801. // If we are setting any of the ACLs then the security descriptor must
  3802. // be recomputed.
  3803. //
  3804. if (FLAG_ON(SecurityInfo, (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION)))
  3805. {
  3806. //
  3807. // TmpSeInfo is ZERO if the caller is resetting both OWNER and GROUP
  3808. // as well.
  3809. // We have to read the old ACLs if the caller wants to retain explicit
  3810. // aces.
  3811. //
  3812. if (KeepExplicit)
  3813. {
  3814. TmpSeInfo |= (SecurityInfo & (DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION));
  3815. }
  3816. //
  3817. // Read the existing security on the child.
  3818. //
  3819. if (0 != TmpSeInfo)
  3820. {
  3821. dwErr = (*(pMartaSetFunctionContext->fGetRights))(
  3822. ChildContext,
  3823. TmpSeInfo,
  3824. &pOldChildSD
  3825. );
  3826. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3827. //
  3828. // Set the existing owner information in the empty security descriptor
  3829. // if the caller has not provided an Owner sid to set.
  3830. //
  3831. if (!FLAG_ON(SecurityInfo, OWNER_SECURITY_INFORMATION))
  3832. {
  3833. if (FALSE == SetSecurityDescriptorOwner(
  3834. pEmptySD,
  3835. RtlpOwnerAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD),
  3836. FALSE))
  3837. {
  3838. dwErr = GetLastError();
  3839. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3840. }
  3841. }
  3842. if (KeepExplicit)
  3843. {
  3844. //
  3845. // Set the ACLs in the EmptySD to existing ones in order to
  3846. // retain explicit aces.
  3847. //
  3848. if (FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  3849. {
  3850. if (FALSE == SetSecurityDescriptorDacl(
  3851. pEmptySD,
  3852. TRUE,
  3853. RtlpDaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD),
  3854. FALSE))
  3855. {
  3856. dwErr = GetLastError();
  3857. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3858. }
  3859. }
  3860. if (FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  3861. {
  3862. if (FALSE == SetSecurityDescriptorSacl(
  3863. pEmptySD,
  3864. TRUE,
  3865. RtlpSaclAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD),
  3866. FALSE))
  3867. {
  3868. dwErr = GetLastError();
  3869. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3870. }
  3871. }
  3872. }
  3873. //
  3874. // Set the existing group information in the empty security descriptor
  3875. // if the caller has not provided a group sid to set.
  3876. //
  3877. if (!FLAG_ON(SecurityInfo, GROUP_SECURITY_INFORMATION))
  3878. {
  3879. if (FALSE == SetSecurityDescriptorGroup(
  3880. pEmptySD,
  3881. RtlpGroupAddrSecurityDescriptor((PISECURITY_DESCRIPTOR) pOldChildSD),
  3882. FALSE))
  3883. {
  3884. dwErr = GetLastError();
  3885. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3886. }
  3887. }
  3888. }
  3889. MARTA_TURN_OFF_IMPERSONATION;
  3890. //
  3891. // Merge the NewParentSD and the OldChildSD to create NewChildSD.
  3892. //
  3893. if (FALSE == CreatePrivateObjectSecurityEx(
  3894. pNewSD,
  3895. pEmptySD,
  3896. &pNewChildSD,
  3897. NULL,
  3898. bIsContainer,
  3899. (SEF_DACL_AUTO_INHERIT | SEF_SACL_AUTO_INHERIT |
  3900. SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK),
  3901. ProcessHandle,
  3902. pGenMap
  3903. ))
  3904. {
  3905. dwErr = GetLastError();
  3906. }
  3907. MARTA_TURN_ON_IMPERSONATION;
  3908. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3909. }
  3910. else
  3911. {
  3912. //
  3913. // The new ChildSD does not have to computed. We only want to set
  3914. // Owner/Group information.
  3915. //
  3916. pNewChildSD = pEmptySD;
  3917. }
  3918. //
  3919. // Update the subtree undrneath this child.
  3920. //
  3921. if (bIsContainer)
  3922. {
  3923. if (!bRetry)
  3924. {
  3925. #ifdef MARTA_DEBUG
  3926. wprintf(L"Trying reset \n");
  3927. #endif
  3928. bRetry = MartaResetTree(
  3929. SecurityInfo,
  3930. TmpSeInfo,
  3931. pNewChildSD,
  3932. pEmptySD,
  3933. ChildContext,
  3934. ProcessHandle,
  3935. pMartaSetFunctionContext,
  3936. pGenMap,
  3937. MaxAccessMask,
  3938. AccessMask,
  3939. RetryAccessMask,
  3940. pOperation,
  3941. fnProgress,
  3942. Args,
  3943. KeepExplicit
  3944. );
  3945. }
  3946. }
  3947. else
  3948. {
  3949. bRetry = FALSE;
  3950. }
  3951. //
  3952. // Stamp NewChildSD on child.
  3953. //
  3954. if(FLAG_ON(SecurityInfo, DACL_SECURITY_INFORMATION))
  3955. {
  3956. ((PISECURITY_DESCRIPTOR) pNewChildSD)->Control |= SE_DACL_AUTO_INHERIT_REQ;
  3957. }
  3958. if(FLAG_ON(SecurityInfo, SACL_SECURITY_INFORMATION))
  3959. {
  3960. ((PISECURITY_DESCRIPTOR) pNewChildSD)->Control |= SE_SACL_AUTO_INHERIT_REQ;
  3961. }
  3962. dwErr = (*(pMartaSetFunctionContext->fSetRights))(
  3963. ChildContext,
  3964. SecurityInfo,
  3965. pNewChildSD
  3966. );
  3967. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  3968. #ifdef MARTA_DEBUG
  3969. wprintf(L"Set right succeeded\n");
  3970. #endif
  3971. //
  3972. // Note down that we were able to set security on this object.
  3973. //
  3974. bSetWorked = TRUE;
  3975. //
  3976. // Abort if the progress function requested a "Cancel" in the subtree
  3977. // below this node. The value is propagated all the way to the root as
  3978. // the stack unwinds.
  3979. //
  3980. if (ProgressCancelOperation == *pOperation)
  3981. {
  3982. goto EndOfWhile;
  3983. }
  3984. //
  3985. // If propagation had failed in the first attept then try again. This is to
  3986. // cover the case when the container can be enumerated after setting the new
  3987. // security.
  3988. //
  3989. if (bRetry && bIsContainer && (SecurityInfo & DACL_SECURITY_INFORMATION))
  3990. {
  3991. Retry:
  3992. bRetry = FALSE;
  3993. //
  3994. // Reopen the object for List and retry on the subtree.
  3995. //
  3996. dwErr = (*(pMartaSetFunctionContext->fReopenContext))(
  3997. ChildContext,
  3998. RetryAccessMask
  3999. );
  4000. CONDITIONAL_EXIT(dwErr, EndOfWhile);
  4001. bRetry = MartaResetTree(
  4002. SecurityInfo,
  4003. TmpSeInfo,
  4004. pNewSD,
  4005. pEmptySD,
  4006. ChildContext,
  4007. ProcessHandle,
  4008. pMartaSetFunctionContext,
  4009. pGenMap,
  4010. MaxAccessMask,
  4011. AccessMask,
  4012. RetryAccessMask,
  4013. pOperation,
  4014. fnProgress,
  4015. Args,
  4016. KeepExplicit
  4017. );
  4018. }
  4019. EndOfWhile:
  4020. if (bRetry && bIsContainer)
  4021. {
  4022. dwErr = ERROR_ACCESS_DENIED;
  4023. }
  4024. switch (*pOperation)
  4025. {
  4026. case ProgressInvokeNever:
  4027. break;
  4028. case ProgressInvokeOnError:
  4029. if (ERROR_SUCCESS == dwErr)
  4030. {
  4031. break;
  4032. }
  4033. //
  4034. // Fallthough is intended!!
  4035. //
  4036. case ProgressInvokeEveryObject:
  4037. if (NULL != fnProgress)
  4038. {
  4039. LPWSTR Name = NULL;
  4040. //
  4041. // Get the name of the current object from the context and call
  4042. // the progress function with the arguments provided by the
  4043. // caller of ResetTree API.
  4044. //
  4045. DWORD Error = (*(pMartaSetFunctionContext->fGetNameFromContext))(
  4046. ChildContext,
  4047. &Name
  4048. );
  4049. if (ERROR_SUCCESS == Error)
  4050. {
  4051. fnProgress(
  4052. Name,
  4053. dwErr,
  4054. pOperation,
  4055. Args,
  4056. bSetWorked
  4057. );
  4058. LocalFree(Name);
  4059. //
  4060. // This was the latest feature request by HiteshR. At this
  4061. // point, retry has failed, but the caller has made some
  4062. // changes and expects retry to work okay now.
  4063. //
  4064. if (ProgressRetryOperation == *pOperation)
  4065. {
  4066. *pOperation = ProgressInvokeEveryObject;
  4067. goto Retry;
  4068. }
  4069. }
  4070. }
  4071. break;
  4072. default:
  4073. break;
  4074. }
  4075. bSetWorked = bRetry = FALSE;
  4076. if ((NULL != pNewChildSD) && (pEmptySD != pNewChildSD))
  4077. {
  4078. DestroyPrivateObjectSecurity(&pNewChildSD);
  4079. pNewChildSD = NULL;
  4080. }
  4081. if (NULL != pOldChildSD)
  4082. {
  4083. LocalFree(pOldChildSD);
  4084. pOldChildSD = NULL;
  4085. }
  4086. //
  4087. // Abort if the progress function requested a "Cancel".
  4088. //
  4089. if (ProgressCancelOperation == *pOperation)
  4090. {
  4091. (*(pMartaSetFunctionContext->fCloseContext)) (ChildContext);
  4092. return TRUE;
  4093. }
  4094. //
  4095. // Get the next child.
  4096. //
  4097. do {
  4098. dwErr = (*(pMartaSetFunctionContext->fFindNext))(
  4099. ChildContext,
  4100. MaxAccessMask,
  4101. &ChildContext
  4102. );
  4103. if (ERROR_SUCCESS != dwErr)
  4104. {
  4105. #ifdef MARTA_DEBUG
  4106. wprintf(L"FindNext failed\n");
  4107. #endif
  4108. dwErr = (*(pMartaSetFunctionContext->fReopenContext))(
  4109. ChildContext,
  4110. AccessMask
  4111. );
  4112. #ifdef MARTA_DEBUG
  4113. if (dwErr == ERROR_SUCCESS)
  4114. {
  4115. wprintf(L"FindNext failed\n");
  4116. }
  4117. #endif
  4118. bRetry = TRUE;
  4119. }
  4120. else
  4121. {
  4122. #ifdef MARTA_DEBUG
  4123. wprintf(L"Findnext succeeded\n");
  4124. #endif
  4125. }
  4126. switch (*pOperation)
  4127. {
  4128. case ProgressInvokeNever:
  4129. case ProgressInvokeEveryObject:
  4130. break;
  4131. case ProgressInvokeOnError:
  4132. //
  4133. // If we encountered an error in FindNext then report it to the
  4134. // caller.
  4135. //
  4136. if (ERROR_SUCCESS == dwErr)
  4137. {
  4138. break;
  4139. }
  4140. if (NULL != fnProgress)
  4141. {
  4142. LPWSTR Name = NULL;
  4143. //
  4144. // Get the name of the current object.
  4145. //
  4146. DWORD Error = (*(pMartaSetFunctionContext->fGetNameFromContext))(
  4147. ChildContext,
  4148. &Name
  4149. );
  4150. if (ERROR_SUCCESS == Error)
  4151. {
  4152. fnProgress(
  4153. Name,
  4154. dwErr,
  4155. pOperation,
  4156. Args,
  4157. bSetWorked
  4158. );
  4159. LocalFree(Name);
  4160. }
  4161. }
  4162. break;
  4163. default:
  4164. break;
  4165. }
  4166. //
  4167. // Abort if the progress function requested a "Cancel".
  4168. //
  4169. if (ProgressCancelOperation == *pOperation)
  4170. {
  4171. (*(pMartaSetFunctionContext->fCloseContext)) (ChildContext);
  4172. return TRUE;
  4173. }
  4174. } while ((ERROR_SUCCESS != dwErr) && (NULL != ChildContext));
  4175. }
  4176. return FALSE;
  4177. }