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.

559 lines
12 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. FaxSecurity.cpp
  5. Abstract:
  6. Implementation of CFaxSecurity Class.
  7. Author:
  8. Iv Garber (IvG) Jun, 2000
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "FaxComEx.h"
  13. #include "FaxSecurity.h"
  14. #include "faxutil.h"
  15. //
  16. //================== INFORMATION TYPE ===========================================
  17. //
  18. STDMETHODIMP
  19. CFaxSecurity::get_InformationType(
  20. long *plInformationType
  21. )
  22. /*++
  23. Routine name : CFaxSecurity::get_InformationType
  24. Routine description:
  25. Return current SecurityInformation value
  26. Author:
  27. Iv Garber (IvG), May, 2001
  28. Arguments:
  29. plInformationType [out] - the SecurityInformation data to be returned
  30. Return Value:
  31. Standard HRESULT code
  32. --*/
  33. {
  34. HRESULT hr = S_OK;
  35. DBG_ENTER (TEXT("CFaxSecurity::get_InformationType"), hr);
  36. hr = GetLong(plInformationType, m_dwSecurityInformation);
  37. if (FAILED(hr))
  38. {
  39. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  40. return hr;
  41. }
  42. return hr;
  43. }
  44. STDMETHODIMP
  45. CFaxSecurity::put_InformationType(
  46. long lInformationType
  47. )
  48. /*++
  49. Routine name : CFaxSecurity::put_InformationType
  50. Routine description:
  51. Set SecurityInformation for the Descriptor
  52. Author:
  53. Iv Garber (IvG), May, 2001
  54. Arguments:
  55. lInformationType [in] - the SecurityInformation data to set
  56. Return Value:
  57. Standard HRESULT code
  58. --*/
  59. {
  60. HRESULT hr = S_OK;
  61. DWORD dwSecInfo = ( OWNER_SECURITY_INFORMATION |
  62. GROUP_SECURITY_INFORMATION |
  63. DACL_SECURITY_INFORMATION |
  64. SACL_SECURITY_INFORMATION );
  65. DBG_ENTER (_T("CFaxSecurity::put_InformationType"), hr, _T("%ld"), lInformationType);
  66. if (m_dwSecurityInformation != lInformationType)
  67. {
  68. //
  69. // check that lInformationType is valid
  70. //
  71. if (0 == (lInformationType & dwSecInfo))
  72. {
  73. hr = E_INVALIDARG;
  74. CALL_FAIL(GENERAL_ERR, _T("lInformationType does not contain good bits."), hr);
  75. AtlReportError(
  76. CLSID_FaxSecurity,
  77. IDS_ERROR_INVALID_ARGUMENT,
  78. IID_IFaxSecurity,
  79. hr);
  80. return hr;
  81. }
  82. if (0 != (lInformationType & ~dwSecInfo))
  83. {
  84. hr = E_INVALIDARG;
  85. CALL_FAIL(GENERAL_ERR, _T("lInformationType contains bad bits."), hr);
  86. AtlReportError(
  87. CLSID_FaxSecurity,
  88. IDS_ERROR_INVALID_ARGUMENT,
  89. IID_IFaxSecurity,
  90. hr);
  91. return hr;
  92. }
  93. m_dwSecurityInformation = lInformationType;
  94. //
  95. // we want to discard current Descriptor, because its security_information is different now
  96. //
  97. m_bInited = false;
  98. }
  99. return hr;
  100. }
  101. //
  102. //================== DESCRIPTOR ===========================================
  103. //
  104. STDMETHODIMP
  105. CFaxSecurity::put_Descriptor(
  106. /*[out, retval]*/ VARIANT vDescriptor
  107. )
  108. /*++
  109. Routine name : CFaxSecurity::put_Descriptor
  110. Routine description:
  111. Set the given Security Descriptor
  112. Author:
  113. Iv Garber (IvG), Jun, 2000
  114. Arguments:
  115. sabDescriptor [in] - the given Security Descriptor
  116. Return Value:
  117. Standard HRESULT code
  118. --*/
  119. {
  120. HRESULT hr = S_OK;
  121. DBG_ENTER(_T("CFaxSecurity::put_Descriptor"), hr);
  122. //
  123. // First, initialize the FaxSecurity object
  124. //
  125. if (!m_bInited)
  126. {
  127. hr = Refresh();
  128. if (FAILED(hr))
  129. {
  130. return hr;
  131. }
  132. }
  133. //
  134. // Before filling the m_pbSD with the User's value, store its current value for roll-back
  135. //
  136. CFaxPtrLocal<BYTE> pSDTmp;
  137. pSDTmp = m_pbSD.Detach();
  138. hr = VarByteSA2Binary(vDescriptor, &m_pbSD);
  139. if (FAILED(hr))
  140. {
  141. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  142. goto exit;
  143. }
  144. //
  145. // Check that we have got a valid Descriptor
  146. //
  147. if (!::IsValidSecurityDescriptor(m_pbSD))
  148. {
  149. hr = E_INVALIDARG;
  150. CALL_FAIL(GENERAL_ERR, _T("IsValidSecurityDescriptor(m_pbSD)"), hr);
  151. AtlReportError(CLSID_FaxSecurity, IDS_ERROR_INVALID_ARGUMENT, IID_IFaxSecurity, hr);
  152. goto exit;
  153. }
  154. //
  155. // Check that we have got a Self-Relative Descriptor
  156. //
  157. SECURITY_DESCRIPTOR_CONTROL sdControl;
  158. DWORD dwRevision;
  159. if (!::GetSecurityDescriptorControl(m_pbSD, &sdControl, &dwRevision))
  160. {
  161. hr = Fax_HRESULT_FROM_WIN32(GetLastError());
  162. CALL_FAIL(GENERAL_ERR, _T("GetSecurityDescriptorContrl(m_pbSD, &sdControl, ...)"), hr);
  163. AtlReportError(CLSID_FaxSecurity, IDS_ERROR_INVALID_ARGUMENT, IID_IFaxSecurity, hr);
  164. goto exit;
  165. }
  166. if (!(sdControl & SE_SELF_RELATIVE))
  167. {
  168. //
  169. // Security Descriptor is not Self-Relative
  170. //
  171. hr = E_INVALIDARG;
  172. CALL_FAIL(GENERAL_ERR, _T("Security Descriptor is not Self-Relative"), hr);
  173. AtlReportError(CLSID_FaxSecurity, IDS_ERROR_SDNOTSELFRELATIVE, IID_IFaxSecurity, hr);
  174. goto exit;
  175. }
  176. //
  177. // we have valid Descriptor. Old one will be deallocated by pSecDescTmp
  178. //
  179. return hr;
  180. exit:
  181. //
  182. // Set previous value for the Descriptor
  183. //
  184. m_pbSD = pSDTmp.Detach();
  185. return hr;
  186. }
  187. STDMETHODIMP
  188. CFaxSecurity::get_Descriptor(
  189. /*[out, retval]*/ VARIANT *pvDescriptor
  190. )
  191. /*++
  192. Routine name : CFaxSecurity::get_Descriptor
  193. Routine description:
  194. Return the current Security Descriptor
  195. Author:
  196. Iv Garber (IvG), Jun, 2000
  197. Arguments:
  198. Return Value:
  199. Standard HRESULT code
  200. --*/
  201. {
  202. HRESULT hr = S_OK;
  203. DBG_ENTER(_T("CFaxSecurity::get_Descriptor"), hr);
  204. //
  205. // check that we have got good ptr
  206. //
  207. if (::IsBadWritePtr(pvDescriptor, sizeof(VARIANT)))
  208. {
  209. hr = E_POINTER;
  210. CALL_FAIL(GENERAL_ERR, _T("::IsBadWritePtr(pvDescriptor, sizeof(VARIANT))"), hr);
  211. AtlReportError(CLSID_FaxSecurity, IDS_ERROR_INVALID_ARGUMENT, IID_IFaxSecurity, hr);
  212. return hr;
  213. }
  214. //
  215. // Bring the data from the Server, if not brought yet
  216. //
  217. if (!m_bInited)
  218. {
  219. hr = Refresh();
  220. if (FAILED(hr))
  221. {
  222. return hr;
  223. }
  224. }
  225. //
  226. // Find size of the Security Descriptor
  227. //
  228. DWORD dwLength = GetSecurityDescriptorLength(m_pbSD);
  229. //
  230. // Convert the byte blob into variant containing Safe Array of bytes
  231. //
  232. hr = Binary2VarByteSA(m_pbSD, pvDescriptor, dwLength);
  233. if (FAILED(hr))
  234. {
  235. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  236. return hr;
  237. }
  238. return hr;
  239. }
  240. //
  241. //================== SAVE ===========================================
  242. //
  243. STDMETHODIMP
  244. CFaxSecurity::Save()
  245. /*++
  246. Routine name : CFaxSecurity::Save
  247. Routine description:
  248. Save the Object's contents to the Server
  249. Author:
  250. Iv Garber (IvG), Jun, 2000
  251. Arguments:
  252. Return Value:
  253. Standard HRESULT code
  254. --*/
  255. {
  256. HRESULT hr = S_OK;
  257. DBG_ENTER(_T("CFaxSecurity::Save"), hr);
  258. //
  259. // No changes ==> nothing to update at the Server
  260. //
  261. if (!m_bInited)
  262. {
  263. return hr;
  264. }
  265. //
  266. // Get Fax Server Handle
  267. //
  268. HANDLE hFaxHandle = NULL;
  269. hr = GetFaxHandle(&hFaxHandle);
  270. if (FAILED(hr))
  271. {
  272. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  273. return hr;
  274. }
  275. //
  276. // Set Security Data at the Server
  277. //
  278. if (!FaxSetSecurity(hFaxHandle, m_dwSecurityInformation, m_pbSD))
  279. {
  280. hr = Fax_HRESULT_FROM_WIN32(GetLastError());
  281. CALL_FAIL(GENERAL_ERR, _T("FaxSetSecurity(hFaxHandle, dwSecInfo, m_pbSD)"), hr);
  282. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  283. return hr;
  284. }
  285. return hr;
  286. }
  287. //
  288. //================== GET GRANTED RIGHTS ===========================================
  289. //
  290. STDMETHODIMP
  291. CFaxSecurity::get_GrantedRights(
  292. FAX_ACCESS_RIGHTS_ENUM *pGrantedRights
  293. )
  294. /*++
  295. Routine name : CFaxSecurity::get_GrantedRights
  296. Routine description:
  297. Return current Access Rights of a user
  298. Author:
  299. Iv Garber (IvG), Jun, 2000
  300. Arguments:
  301. pGrantedRights [out, retval] - Bit-Wise combination of the granted rights of the user
  302. Return Value:
  303. Standard HRESULT code
  304. --*/
  305. {
  306. HRESULT hr = S_OK;
  307. DBG_ENTER(_T("CFaxSecurity::get_GrantedRights"), hr);
  308. //
  309. // Check that we have got good Ptr
  310. //
  311. if (::IsBadWritePtr(pGrantedRights, sizeof(FAX_ACCESS_RIGHTS_ENUM)))
  312. {
  313. hr = E_POINTER;
  314. CALL_FAIL(GENERAL_ERR, _T("::IsBadWritePtr(pGrantedRights, sizeof(FAX_ACCESS_RIGHTS_ENUM))"), hr);
  315. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  316. return hr;
  317. }
  318. //
  319. // Bring the data from the Server, if not brought yet
  320. //
  321. if (!m_bInited)
  322. {
  323. hr = Refresh();
  324. if (FAILED(hr))
  325. {
  326. return hr;
  327. }
  328. }
  329. *pGrantedRights = FAX_ACCESS_RIGHTS_ENUM(m_dwAccessRights);
  330. return hr;
  331. }
  332. //
  333. //================== REFRESH ===========================================
  334. //
  335. STDMETHODIMP
  336. CFaxSecurity::Refresh()
  337. /*++
  338. Routine name : CFaxSecurity::Refresh
  339. Routine description:
  340. Refresh the Object's contents : bring new Security data from the Server.
  341. Author:
  342. Iv Garber (IvG), Jun, 2000
  343. Arguments:
  344. Return Value:
  345. Standard HRESULT code
  346. --*/
  347. {
  348. HRESULT hr = S_OK;
  349. DBG_ENTER(_T("CFaxSecurity::Refresh"), hr);
  350. //
  351. // Get Fax Server Handle
  352. //
  353. HANDLE hFaxHandle = NULL;
  354. hr = GetFaxHandle(&hFaxHandle);
  355. if (FAILED(hr))
  356. {
  357. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  358. return hr;
  359. }
  360. //
  361. // Ask the Server for the Access Rights Data
  362. //
  363. if (!FaxAccessCheckEx(hFaxHandle, MAXIMUM_ALLOWED, &m_dwAccessRights))
  364. {
  365. hr = Fax_HRESULT_FROM_WIN32(GetLastError());
  366. CALL_FAIL(GENERAL_ERR, _T("FaxAccessCheckEx(hFaxHandle, MAXIMUM_ALLOWED, &m_dwAccessRights)"), hr);
  367. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  368. return hr;
  369. }
  370. //
  371. // Ask the Server for the SD
  372. //
  373. PSECURITY_DESCRIPTOR pSecDesc = NULL;
  374. if (!FaxGetSecurityEx(hFaxHandle, m_dwSecurityInformation, &pSecDesc))
  375. {
  376. hr = Fax_HRESULT_FROM_WIN32(GetLastError());
  377. CALL_FAIL(GENERAL_ERR, _T("FaxGetSecurityEx(hFaxHandle, m_dwSecurityInformation, &m_pSecurityDescriptor)"), hr);
  378. AtlReportError(CLSID_FaxSecurity, GetErrorMsgId(hr), IID_IFaxSecurity, hr);
  379. return hr;
  380. }
  381. //
  382. // Copy the given SD to m_pbSD
  383. //
  384. DWORD dwLength = GetSecurityDescriptorLength(pSecDesc);
  385. m_pbSD = (BYTE *)MemAlloc(dwLength);
  386. if (!m_pbSD)
  387. {
  388. hr = E_OUTOFMEMORY;
  389. CALL_FAIL(MEM_ERR, _T("MemAlloc(dwLength)"), hr);
  390. AtlReportError(CLSID_FaxSecurity, IDS_ERROR_OUTOFMEMORY, IID_IFaxSecurity, hr);
  391. FaxFreeBuffer(pSecDesc);
  392. return hr;
  393. }
  394. memcpy(m_pbSD, pSecDesc, dwLength);
  395. //
  396. // Free the Server's memory for SD
  397. //
  398. FaxFreeBuffer(pSecDesc);
  399. m_bInited = true;
  400. return hr;
  401. } // CFaxSecurity::Refresh
  402. //
  403. //============================ SUPPORT ERROR INFO =======================================
  404. //
  405. STDMETHODIMP
  406. CFaxSecurity::InterfaceSupportsErrorInfo(
  407. REFIID riid
  408. )
  409. /*++
  410. Routine name : CFaxSecurity::InterfaceSupportsErrorInfo
  411. Routine description:
  412. ATL's implementation of Support Error Info mechanism.
  413. Author:
  414. Iv Garber (IvG), Jun, 2000
  415. Arguments:
  416. riid [in] - Reference to the Interface to check.
  417. Return Value:
  418. Standard HRESULT code
  419. --*/
  420. {
  421. static const IID* arr[] =
  422. {
  423. &IID_IFaxSecurity
  424. };
  425. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  426. {
  427. if (InlineIsEqualGUID(*arr[i],riid))
  428. return S_OK;
  429. }
  430. return S_FALSE;
  431. }