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