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.

198 lines
5.9 KiB

  1. //+------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1993, Microsoft Corporation.
  4. //
  5. // File: filesec.cxx
  6. //
  7. // Classes: CFileSecurity class encapsulating SECURITY_DESCRIPTOR
  8. //
  9. // History: Nov-93 DaveMont Created.
  10. // Oct-96 BrunoSc Modified
  11. //
  12. //-------------------------------------------------------------------
  13. #include "pch.h"
  14. #include "filesec.hxx"
  15. #include "caclsmsg.h"
  16. //+---------------------------------------------------------------------------
  17. // Function: Add2Ptr
  18. //
  19. // Synopsis: Add an unscaled increment to a ptr regardless of type.
  20. //
  21. // Arguments: [pv] -- Initial ptr.
  22. // [cb] -- Increment
  23. //
  24. // Returns: Incremented ptr.
  25. //
  26. //----------------------------------------------------------------------------
  27. VOID * Add2Ptr(VOID *pv, ULONG cb)
  28. {
  29. return((BYTE *) pv + cb);
  30. }
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Member: CFileSecurity::CFileSecurity, public
  34. //
  35. // Synopsis: initializes data members
  36. // constructor will not throw
  37. //
  38. // Arguments: [filename] - name of file to apply security descriptor to
  39. //
  40. //----------------------------------------------------------------------------
  41. CFileSecurity::CFileSecurity(LPWSTR filename)
  42. : _psd(NULL),
  43. _pwfilename(filename)
  44. {
  45. }
  46. //+---------------------------------------------------------------------------
  47. //
  48. // Member: CFileSecurity::Init, public
  49. //
  50. // Synopsis: Init must be called before any other methods - this
  51. // is not enforced. gets security descriptor from file
  52. //
  53. // Arguments: none
  54. //
  55. //----------------------------------------------------------------------------
  56. ULONG CFileSecurity::Init()
  57. {
  58. ULONG ret;
  59. ULONG cpsd;
  60. // get the size of the security buffer
  61. if (!GetFileSecurity((LPCTSTR)_pwfilename,
  62. DACL_SECURITY_INFORMATION |
  63. GROUP_SECURITY_INFORMATION |
  64. OWNER_SECURITY_INFORMATION,
  65. NULL,
  66. 0,
  67. &cpsd) )
  68. {
  69. if (ERROR_INSUFFICIENT_BUFFER == (ret = GetLastError()))
  70. {
  71. if (NULL == (_psd = (BYTE *)LocalAlloc(LMEM_FIXED, cpsd)))
  72. {
  73. return(ERROR_NOT_ENOUGH_MEMORY);
  74. }
  75. // actually get the buffer this time
  76. if ( GetFileSecurity((LPCTSTR)_pwfilename,
  77. DACL_SECURITY_INFORMATION |
  78. GROUP_SECURITY_INFORMATION |
  79. OWNER_SECURITY_INFORMATION,
  80. _psd,
  81. cpsd,
  82. &cpsd) )
  83. ret = ERROR_SUCCESS;
  84. else
  85. ret = GetLastError();
  86. }
  87. // the following section was inserted to test an error when access to a file
  88. // has been denied
  89. else
  90. { if (ret == ERROR_ACCESS_DENIED)
  91. { return(ret);
  92. }
  93. }
  94. } else
  95. return(ERROR_NO_SECURITY_ON_OBJECT);
  96. return(ret);
  97. }
  98. //+---------------------------------------------------------------------------
  99. //
  100. // Member: Dtor, public
  101. //
  102. // Synopsis: frees security descriptor if allocated
  103. //
  104. // Arguments: none
  105. //
  106. //----------------------------------------------------------------------------
  107. CFileSecurity::~CFileSecurity()
  108. {
  109. if (_psd)
  110. {
  111. LocalFree(_psd);
  112. }
  113. }
  114. //+---------------------------------------------------------------------------
  115. //
  116. // Member: CFileSecurity::SetFS, public
  117. //
  118. // Synopsis: sets or modifies the security descriptor DACL on the specified file
  119. //
  120. // Arguments: IN - [fmodify] - TRUE = modify ACL, FALSE = replace ACL
  121. // IN - [pcdw] - wrapper around new ACEs
  122. // IN - [fdir] - TRUE = directory
  123. //
  124. // Returns: status
  125. //
  126. //----------------------------------------------------------------------------
  127. ULONG CFileSecurity::SetFS(BOOL fmodify, CDaclWrap *pcdw, BOOL fdir)
  128. {
  129. BOOL fdaclpresent;
  130. BOOL cod;
  131. ACL *pdacl;
  132. ULONG ret;
  133. // get the ACL from the security descriptor
  134. if ( GetSecurityDescriptorDacl(_psd,
  135. &fdaclpresent,
  136. &pdacl,
  137. &cod) )
  138. {
  139. if (fdaclpresent)
  140. {
  141. // build the new ACL (from the new ACEs and the old ACL)
  142. PACL pnewdacl = NULL;
  143. if (ERROR_SUCCESS == (ret = pcdw->BuildAcl(&pnewdacl,
  144. fmodify ? pdacl : NULL,
  145. pdacl ? pdacl->AclRevision : ACL_REVISION,
  146. fdir)
  147. ))
  148. {
  149. // make a new security descriptor
  150. SECURITY_DESCRIPTOR newsd;
  151. //NTRAID#NTBUG9-547532-2002/03/28-hiteshr
  152. if(!InitializeSecurityDescriptor( &newsd, SECURITY_DESCRIPTOR_REVISION ))
  153. {
  154. LocalFree(pnewdacl);
  155. return GetLastError();
  156. }
  157. if(!SetSecurityDescriptorDacl( &newsd, TRUE, pnewdacl, FALSE ))
  158. {
  159. LocalFree(pnewdacl);
  160. return GetLastError();
  161. }
  162. //
  163. // apply it to the file
  164. //
  165. if (!SetFileSecurity(_pwfilename,
  166. DACL_SECURITY_INFORMATION,
  167. &newsd))
  168. {
  169. ret = GetLastError();
  170. }
  171. LocalFree(pnewdacl);
  172. }
  173. }
  174. else
  175. return(ERROR_NO_SECURITY_ON_OBJECT);
  176. } else
  177. {
  178. ret = GetLastError();
  179. }
  180. return(ret);
  181. }