Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
6.5 KiB

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