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.

210 lines
6.0 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1995, Microsoft Corporation.
  4. //
  5. // File: DumpSec.cxx
  6. //
  7. // Contents: class to dump file security ACL
  8. //
  9. // Classes: CDumpSecurity
  10. //
  11. // History: Nov-93 DaveMont Created.
  12. //
  13. //-------------------------------------------------------------------
  14. #include <DumpSec.hxx>
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Member: CDumpSecurity::CDumpSecurity, public
  18. //
  19. // Synopsis: initialized data members, constructor will not throw
  20. //
  21. // Arguments: IN [pfilename] - name of file to dump security for
  22. //
  23. //----------------------------------------------------------------------------
  24. CDumpSecurity::CDumpSecurity(WCHAR *pfilename)
  25. : _psd(NULL),
  26. _pwfilename(pfilename),
  27. _pdacl(NULL),
  28. _pah(NULL),
  29. _psid(NULL),
  30. _cacethissid(0)
  31. {
  32. }
  33. //+---------------------------------------------------------------------------
  34. //
  35. // Member: CDumpSecurity::Init, public
  36. //
  37. // Synopsis: Init must be called before any other methods - this
  38. // is not enforced. Init gets the security descriptor and
  39. // ACL for the file
  40. //
  41. // Arguments: none
  42. //
  43. //----------------------------------------------------------------------------
  44. ULONG CDumpSecurity::Init()
  45. {
  46. ULONG ret;
  47. ULONG cpsd;
  48. // get the size of the security buffer
  49. if (!GetFileSecurity(_pwfilename,
  50. DACL_SECURITY_INFORMATION |
  51. GROUP_SECURITY_INFORMATION |
  52. OWNER_SECURITY_INFORMATION,
  53. NULL,
  54. 0,
  55. &cpsd) )
  56. {
  57. if (ERROR_INSUFFICIENT_BUFFER == (ret = GetLastError()))
  58. {
  59. if ( NULL == ( _psd = (BYTE *)LocalAlloc(LMEM_FIXED, cpsd)))
  60. {
  61. return(ERROR_NOT_ENOUGH_MEMORY);
  62. }
  63. // actually get the buffer this time
  64. if ( GetFileSecurity(_pwfilename,
  65. DACL_SECURITY_INFORMATION |
  66. GROUP_SECURITY_INFORMATION |
  67. OWNER_SECURITY_INFORMATION,
  68. _psd,
  69. cpsd,
  70. &cpsd) )
  71. {
  72. BOOL fdaclpresent;
  73. BOOL cod;
  74. // get the ACL
  75. if ( GetSecurityDescriptorDacl(_psd,
  76. &fdaclpresent,
  77. &_pdacl,
  78. &cod) )
  79. {
  80. if (!fdaclpresent)
  81. {
  82. _pdacl = NULL;
  83. return(ERROR_NO_SECURITY_ON_OBJECT);
  84. }
  85. // save the ACL location
  86. _pah = (ACE_HEADER *)Add2Ptr(_pdacl, sizeof(ACL));
  87. return(ERROR_SUCCESS);
  88. } else
  89. return(GetLastError());
  90. } else
  91. return(GetLastError());
  92. }
  93. } else
  94. return(ERROR_NO_SECURITY_ON_OBJECT);
  95. return(ret);
  96. }
  97. //+---------------------------------------------------------------------------
  98. //
  99. // Member: Dtor, public
  100. //
  101. // Synopsis: frees the security descriptor
  102. //
  103. // Arguments: none
  104. //
  105. //----------------------------------------------------------------------------
  106. CDumpSecurity::~CDumpSecurity()
  107. {
  108. if (_psd)
  109. {
  110. LocalFree(_psd);
  111. }
  112. }
  113. //+---------------------------------------------------------------------------
  114. //
  115. // Member: CDumpSecurity::GetSDOwner, public
  116. //
  117. // Synopsis: returns the owner of the file
  118. //
  119. // Arguments: OUT [psid] - address of the returned sid
  120. //
  121. //----------------------------------------------------------------------------
  122. ULONG CDumpSecurity::GetSDOwner(SID **psid)
  123. {
  124. BOOL cod;
  125. if ( GetSecurityDescriptorOwner(_psd, (void **)psid, &cod) )
  126. return(0);
  127. else
  128. return(GetLastError());
  129. }
  130. //+---------------------------------------------------------------------------
  131. //
  132. // Member: CDumpSecurity::GetSDGroup, public
  133. //
  134. // Synopsis: returns the group from the file
  135. //
  136. // Arguments: OUT [pgsid] - address of the returned group sid
  137. //
  138. //----------------------------------------------------------------------------
  139. ULONG CDumpSecurity::GetSDGroup(SID **pgsid)
  140. {
  141. BOOL cod;
  142. if ( GetSecurityDescriptorGroup(_psd, (void **)pgsid, &cod) )
  143. return(0);
  144. else
  145. return(GetLastError());
  146. }
  147. //+---------------------------------------------------------------------------
  148. //
  149. // Member: CDumpSecurity::ResetAce, public
  150. //
  151. // Synopsis: sets the 'ace' index to the start of the DACL
  152. //
  153. // Arguments: IN - [psid] - the SID to find aces for
  154. //
  155. //----------------------------------------------------------------------------
  156. VOID CDumpSecurity::ResetAce(SID *psid)
  157. {
  158. _psid = psid;
  159. _cacethissid = 0;
  160. if (_pdacl)
  161. _pah = (ACE_HEADER *)Add2Ptr(_pdacl, sizeof(ACL));
  162. }
  163. //+---------------------------------------------------------------------------
  164. //
  165. // Member: CDumpSecurity::GetNextAce, public
  166. //
  167. // Synopsis: gets the next ACE from the DACL for the specified SID
  168. //
  169. // Arguments: OUT [pace] - pointer to the next ace for the SID passed
  170. // in at the last reset.
  171. //
  172. // Returns: the number of the ACE
  173. //
  174. //----------------------------------------------------------------------------
  175. LONG CDumpSecurity::GetNextAce(ACE_HEADER **paceh)
  176. {
  177. LONG ret = -1;
  178. if (_pdacl)
  179. {
  180. for (;_cacethissid < _pdacl->AceCount;
  181. _cacethissid++, _pah = (ACE_HEADER *)Add2Ptr(_pah, _pah->AceSize))
  182. {
  183. if (!_psid || EqualSid(_psid,(SID *)&((ACCESS_ALLOWED_ACE *)_pah)->SidStart) )
  184. {
  185. *paceh = _pah;
  186. ret = _cacethissid++;
  187. _pah = (ACE_HEADER *)Add2Ptr(_pah, _pah->AceSize);
  188. break;
  189. }
  190. }
  191. }
  192. return(ret);
  193. }