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.

436 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998.
  5. //
  6. // File: WSecStor.cxx
  7. //
  8. // Contents: Wrapper around the security store
  9. //
  10. // Classes: CSecurityStoreWrapper
  11. //
  12. // History: 7-14-97 srikants Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.cxx>
  16. #pragma hdrstop
  17. #include "wsecstor.hxx"
  18. #include <imprsnat.hxx>
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Member: CSecurityStoreWrapper::CSecurityStoreWrapper
  22. //
  23. // Synopsis: Constructor of the security store wrapper. It stores the
  24. // ICiCAdviseStatus interface for use later during initialization
  25. // save and load.
  26. //
  27. // Arguments: [pAdviseStatus] - The ICiCAdviseStatus interface needed by
  28. // the CiStorage object.
  29. // [cMegToLeaveOnDisk] - Megabytes not to write to on disk
  30. //
  31. // History: 7-20-97 srikants Created
  32. // 01-Nov-98 KLam Added cMegToLeaveOnDisk to constructor
  33. //
  34. //----------------------------------------------------------------------------
  35. CSecurityStoreWrapper::CSecurityStoreWrapper( ICiCAdviseStatus *pAdviseStatus,
  36. ULONG cMegToLeaveOnDisk )
  37. :_lRefCount(1),
  38. _cMegToLeaveOnDisk( cMegToLeaveOnDisk )
  39. {
  40. Win4Assert( 0 != pAdviseStatus );
  41. pAdviseStatus->AddRef();
  42. _xAdviseStatus.Set( pAdviseStatus );
  43. }
  44. CSecurityStoreWrapper::~CSecurityStoreWrapper()
  45. {
  46. Win4Assert( 0 == _lRefCount );
  47. }
  48. //+---------------------------------------------------------------------------
  49. //
  50. // Member: CSecurityStoreWrapper::Init
  51. //
  52. // Synopsis: Initializes the security store using the given directory.
  53. // If a security store already exists in the directory, then
  54. // it is loaded. Otherwise, a new empty security store is
  55. // created.
  56. //
  57. // Arguments: [pwszDirectory] - Directory from which to load the security
  58. // table.
  59. //
  60. // Returns: S_OK if successful; Other error code as appropriate.
  61. //
  62. // Notes: Assumed to be operating in the system security context
  63. //
  64. // History: 7-20-97 srikants Created
  65. // 01-Nov-98 KLam Pass _cMegToLeaveOnDisk to CiStorage
  66. //
  67. //----------------------------------------------------------------------------
  68. SCODE CSecurityStoreWrapper::Init( WCHAR const * pwszDirectory )
  69. {
  70. if ( !_xStorage.IsNull() )
  71. return CI_E_ALREADY_INITIALIZED;
  72. SCODE sc = S_OK;
  73. TRY
  74. {
  75. XPtr<CiStorage> xStorage(new CiStorage( pwszDirectory,
  76. _xAdviseStatus.GetReference(),
  77. _cMegToLeaveOnDisk ) );
  78. _secStore.Init( xStorage.GetPointer() );
  79. _xStorage.Set( xStorage.Acquire() );
  80. }
  81. CATCH( CException, e )
  82. {
  83. sc = e.GetErrorCode();
  84. ciDebugOut(( DEB_ERROR,
  85. "Exception 0x%X caught in CSecurityStoreWrapper::Load\n",sc ));
  86. }
  87. END_CATCH
  88. return sc;
  89. }
  90. //+---------------------------------------------------------------------------
  91. //
  92. // Member: CSecurityStoreWrapper::Load
  93. //
  94. // Synopsis: Loads the data that was produced using a "Save". The files
  95. // specified in the pFileList are moved/copied to the target
  96. // directory. Note that the security table is not initialized
  97. // as a result of calling Load. An explicit Init must be called
  98. // after the Load to cause the new files to be used by the
  99. // security store.
  100. //
  101. // Arguments: [pwszDestDir] - Destination directory in which to
  102. // copy/move the files.
  103. // [pFileList] - List of files that must be loaded.
  104. // [pProgressNotify] - Progress notification (optional)
  105. // [fCallerOwnsFiles] - Set to TRUE if the caller owns files.
  106. // If set to FALSE, the caller expects us to cleanup the files
  107. // before returning.
  108. // [pfAbort] - Flag set to TRUE if the load must be
  109. // aborted prematurely.
  110. //
  111. // Returns: S_OK if successful. Other ole error code if there is an error.
  112. //
  113. // Notes: Assumed to be operating in the system security context
  114. //
  115. // History: 7-20-97 srikants Created
  116. // 01-Nov-98 KLam Passed _cMegToLeaveOnDisk to CiStorage
  117. //
  118. //----------------------------------------------------------------------------
  119. SCODE CSecurityStoreWrapper::Load(
  120. WCHAR const * pwszDestDir, // dest dir
  121. IEnumString * pFileList, // list of files to copy
  122. IProgressNotify * pProgressNotify,
  123. BOOL fCallerOwnsFiles,
  124. BOOL * pfAbort )
  125. {
  126. SCODE sc = S_OK;
  127. TRY
  128. {
  129. XPtr<CiStorage> xStorage(new CiStorage( pwszDestDir,
  130. _xAdviseStatus.GetReference(),
  131. _cMegToLeaveOnDisk ) );
  132. _secStore.Load( xStorage.GetPointer(),
  133. pFileList,
  134. pProgressNotify,
  135. fCallerOwnsFiles,
  136. pfAbort);
  137. }
  138. CATCH( CException, e )
  139. {
  140. sc = e.GetErrorCode();
  141. ciDebugOut((DEB_ERROR, "CSecurityStoreWrapper::Load caught exception 0x%X\n", sc));
  142. }
  143. END_CATCH
  144. return sc;
  145. }
  146. //+---------------------------------------------------------------------------
  147. //
  148. // Member: CSecurityStoreWrapper::Save
  149. //
  150. // Synopsis: Saves the current security table to the specified target
  151. // directory.
  152. //
  153. // Arguments: [pwszSaveDir] - Directory in which to serialize the
  154. // security store.
  155. // [pfAbort] - Flag to abort the save process prematurely.
  156. // [ppFileList] - If successful, on output will have the
  157. // list of files serialized.
  158. // [pProgress] - (optional) Progress Notification.
  159. //
  160. // Returns: S_OK if successful; Other error code as appropriate.
  161. //
  162. // Notes: Assumed to be operating in the system security context
  163. //
  164. // History: 7-20-97 srikants Created
  165. // 01-Nov-98 KLam Passed _cMegToLeaveOnDisk to CiStorage
  166. //
  167. //----------------------------------------------------------------------------
  168. SCODE
  169. CSecurityStoreWrapper::Save( WCHAR const * pwszSaveDir,
  170. BOOL * pfAbort,
  171. IEnumString ** ppFileList,
  172. IProgressNotify * pProgress )
  173. {
  174. if ( pwszSaveDir == 0 || pfAbort == 0 || ppFileList == 0 )
  175. return E_POINTER;
  176. SCODE sc= S_OK;
  177. TRY
  178. {
  179. XPtr<CiStorage> xStorage(
  180. new CiStorage( pwszSaveDir,
  181. _xAdviseStatus.GetReference(),
  182. _cMegToLeaveOnDisk ) );
  183. _secStore.Save( pProgress,
  184. *pfAbort,
  185. xStorage.GetReference(),
  186. ppFileList );
  187. }
  188. CATCH( CException, e )
  189. {
  190. sc = e.GetErrorCode();
  191. }
  192. END_CATCH
  193. return sc;
  194. }
  195. //+---------------------------------------------------------------------------
  196. //
  197. // Member: CSecurityStoreWrapper::Empty
  198. //
  199. // Synopsis: Empties the contents of the security store.
  200. //
  201. // Notes: Assumed to be operating in the system security context
  202. //
  203. // History: 7-20-97 srikants Created
  204. //
  205. //----------------------------------------------------------------------------
  206. SCODE
  207. CSecurityStoreWrapper::Empty()
  208. {
  209. SCODE sc = S_OK;
  210. TRY
  211. {
  212. _secStore.Empty();
  213. }
  214. CATCH( CException, e )
  215. {
  216. sc = e.GetErrorCode();
  217. ciDebugOut(( DEB_ERROR, "SecurityStoreWrapper::Empty exception 0x%X\n", sc ));
  218. }
  219. END_CATCH
  220. return sc;
  221. }
  222. //+---------------------------------------------------------------------------
  223. //
  224. // Member: CSecurityStoreWrapper::LookupSDID
  225. //
  226. // Synopsis: Looks up the SDID for the given security descriptor. If one
  227. // doesn't exist, a new SDID will be created for the SD.
  228. //
  229. // Arguments: [pSD] - Security descriptor.
  230. // [cbSD] - Length of the security descriptor.
  231. // [sdid] - [out] SDID of the security descriptor.
  232. //
  233. // Returns: S_OK if successful; Other error code as appropriate.
  234. //
  235. // History: 7-20-97 srikants Created
  236. //
  237. //----------------------------------------------------------------------------
  238. SCODE
  239. CSecurityStoreWrapper::LookupSDID( PSECURITY_DESCRIPTOR pSD,
  240. ULONG cbSD,
  241. SDID & sdid )
  242. {
  243. Win4Assert( !_xStorage.IsNull() );
  244. SCODE sc = S_OK;
  245. TRY
  246. {
  247. CImpersonateSystem impersonate;
  248. sdid = _secStore.LookupSDID( pSD, cbSD );
  249. }
  250. CATCH( CException, e )
  251. {
  252. sc = e.GetErrorCode();
  253. ciDebugOut(( DEB_ERROR, "Exception 0x%X in LookupSDID\n", sc ));
  254. }
  255. END_CATCH
  256. return sc;
  257. }
  258. //+---------------------------------------------------------------------------
  259. //
  260. // Member: CSecurityStoreWrapper::AccessCheck
  261. //
  262. // Synopsis: Verifies if the given token has the requested access to the
  263. // document with the given SDID.
  264. //
  265. // Arguments: [sdid] - The SDID to check security against.
  266. // [hToken] - The security token against which to check the
  267. // security.
  268. // [am] - Access mode requested.
  269. // [fGranted] - [out] Set to TRUE if access is granted. FALSE if
  270. // access is granted.
  271. // This parameter is valid only if this method returns success.
  272. //
  273. // Returns: S_OK if successful;
  274. // CI_E_NOT_FOUND if the given SDID not a valid one.
  275. // Other error code as appropriate.
  276. //
  277. // History: 7-20-97 srikants Created
  278. //
  279. //----------------------------------------------------------------------------
  280. SCODE
  281. CSecurityStoreWrapper::AccessCheck( SDID sdid,
  282. HANDLE hToken,
  283. ACCESS_MASK am,
  284. BOOL & fGranted )
  285. {
  286. Win4Assert( !_xStorage.IsNull() );
  287. SCODE sc = S_OK;
  288. TRY
  289. {
  290. CImpersonateSystem impersonate;
  291. if ( !_secStore.AccessCheck( sdid, hToken, am, fGranted ) )
  292. {
  293. sc = CI_E_NOT_FOUND;
  294. }
  295. }
  296. CATCH( CException, e )
  297. {
  298. sc = e.GetErrorCode();
  299. }
  300. END_CATCH
  301. return sc;
  302. }
  303. //+---------------------------------------------------------------------------
  304. //
  305. // Member: CSecurityStoreWrapper::GetSecurityDescriptor
  306. //
  307. // Synopsis: Obtains the security descriptor of the given SDID.
  308. //
  309. // Arguments: [sdid] - SDID whose Security Descriptor is needed.
  310. // [psd] - Pointer to the buffer in which to write the
  311. // security desciptor.
  312. // [cbSdIn] - Number of bytes in the psd buffer.
  313. // [cbSdOut] - Number of bytes in the security descriptor.
  314. //
  315. // Returns: S_OK if successful;
  316. // CI_E_NOT_FOUND if the given SDID is an invalid SDID.
  317. // S_FALSE if the sdid is found but the buffer is not big enough
  318. // to hold the security descriptor. cbSdOut will have the size
  319. // of the buffer needed to hold the security descriptor.
  320. //
  321. // History: 7-20-97 srikants Created
  322. //
  323. //----------------------------------------------------------------------------
  324. SCODE
  325. CSecurityStoreWrapper::GetSecurityDescriptor( SDID sdid,
  326. PSECURITY_DESCRIPTOR psd,
  327. ULONG cbSdIn,
  328. ULONG & cbSdOut )
  329. {
  330. Win4Assert( !_xStorage.IsNull() );
  331. SCODE sc = S_OK;
  332. TRY
  333. {
  334. CImpersonateSystem impersonate;
  335. sc = _secStore.GetSecurityDescriptor( sdid, psd, cbSdIn, cbSdOut );
  336. }
  337. CATCH( CException, e )
  338. {
  339. sc = e.GetErrorCode();
  340. }
  341. END_CATCH
  342. return sc;
  343. }
  344. //+---------------------------------------------------------------------------
  345. //
  346. // Function: CreateSecurityStore
  347. //
  348. // Synopsis: Creates the security store object and returns its pointer.
  349. //
  350. // Arguments: [ppSecurityStore] - Out parameter to hold the security store
  351. // pointer.
  352. // [cMegToLeaveOnDisk] - Number of megabytes to leave on disk
  353. //
  354. // Returns: S_OK if successful;
  355. // Other error code as appropriate.
  356. //
  357. // History: 7-14-97 srikants Created
  358. // 02-Nov-98 KLam Added cbDiskSpaceToLeave parameter
  359. //
  360. //----------------------------------------------------------------------------
  361. SCODE CreateSecurityStore( ICiCAdviseStatus * pAdviseStatus,
  362. PSecurityStore ** ppSecurityStore,
  363. ULONG cMegToLeaveOnDisk )
  364. {
  365. if ( 0 == ppSecurityStore)
  366. return E_POINTER;
  367. *ppSecurityStore = 0;
  368. SCODE sc = S_OK;
  369. TRY
  370. {
  371. *ppSecurityStore = new CSecurityStoreWrapper( pAdviseStatus, cMegToLeaveOnDisk );
  372. }
  373. CATCH( CException, e)
  374. {
  375. sc = e.GetErrorCode();
  376. ciDebugOut((DEB_ERROR, "CreateSecurityStore caught exception 0x%X\n", sc));
  377. }
  378. END_CATCH
  379. return sc;
  380. }