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.

304 lines
7.8 KiB

  1. /*****************************************************************************\
  2. * MODULE: prnsec.cpp
  3. *
  4. * PURPOSE: Implementations
  5. *
  6. * Copyright (C) 1999 Microsoft Corporation
  7. *
  8. * History:
  9. *
  10. * 09/2/99 mlawrenc First implemented the security templates
  11. *
  12. \*****************************************************************************/
  13. #include "stdafx.h"
  14. #include <stdio.h>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // Static Data Members
  17. ///////////////////////////////////////////////////////////////////////////////
  18. LPTSTR COlePrnSecurity::m_MsgStrings[EndMessages*2] = { NULL };
  19. const DWORD COlePrnSecurity::dwMaxResBuf = 256;
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // Methods
  22. ///////////////////////////////////////////////////////////////////////////////
  23. COlePrnSecurity::COlePrnSecurity(IN IUnknown *&iSite,
  24. IN DWORD &dwSafety )
  25. /*++
  26. Routine Description:
  27. This initialises all of the required members
  28. Arguments:
  29. iSite - A reference to the Site Interface pointer
  30. dwSafety - A reference to the ATL Safety Flags member
  31. --*/
  32. : m_iSite(iSite),
  33. m_dwSafetyFlags(dwSafety),
  34. m_bDisplayUIonDisallow(TRUE),
  35. m_iSecurity(NULL) {
  36. }
  37. COlePrnSecurity::~COlePrnSecurity()
  38. /*++
  39. Routine Description:
  40. This clears any memory we have had to allocate
  41. --*/
  42. {
  43. if (m_iSecurity)
  44. m_iSecurity->Release();
  45. }
  46. HRESULT COlePrnSecurity::GetActionPolicy(IN DWORD dwAction,
  47. OUT DWORD &dwPolicy)
  48. /*++
  49. Routine Description:
  50. Sees whether the requested action is allowed by the site.
  51. Arguments:
  52. dwAction : The action which we want to perform
  53. dwPolicy : The policy associated with the action
  54. Return Value:
  55. S_OK or S_FAIL the Policy was returned, S_OK generally means don't prompt
  56. E_XXXXX
  57. --*/
  58. {
  59. HRESULT hr = S_OK;
  60. dwPolicy = URLPOLICY_DISALLOW;
  61. if (!(m_dwSafetyFlags & INTERFACESAFE_FOR_UNTRUSTED_CALLER)) {
  62. dwPolicy = URLPOLICY_ALLOW;
  63. goto Cleanup;
  64. }
  65. if (NULL == m_iSecurity &&
  66. FAILED( hr = SetSecurityManager()) )
  67. goto Cleanup;
  68. _ASSERTE(m_iSecurity != NULL);
  69. hr = m_iSecurity->ProcessUrlAction(dwAction,
  70. (LPBYTE)&dwPolicy,
  71. sizeof(dwPolicy),
  72. NULL,
  73. 0,
  74. m_bDisplayUIonDisallow ? PUAF_WARN_IF_DENIED : PUAF_NOUI,
  75. 0);
  76. if (FAILED(hr)) {
  77. dwPolicy = URLPOLICY_DISALLOW;
  78. goto Cleanup;
  79. }
  80. Cleanup:
  81. return hr;
  82. }
  83. HRESULT COlePrnSecurity::SetSecurityManager(void)
  84. /*++
  85. Routine Description:
  86. Sets up the security manager
  87. Return Value:
  88. E_FAIL - Failed to instantiate
  89. E_NOINTERFACE - There was no security Manager
  90. S_OK - We instantiated the security manager
  91. --*/
  92. {
  93. HRESULT hr = E_NOINTERFACE;
  94. IServiceProvider *iServiceProvider = NULL;
  95. if (NULL != m_iSecurity) {
  96. hr = S_OK;
  97. goto Cleanup;
  98. }
  99. if (NULL == m_iSite)
  100. goto Cleanup;
  101. if ( FAILED(hr = m_iSite->QueryInterface(IID_IServiceProvider,
  102. (LPVOID *)&iServiceProvider) ) )
  103. goto Cleanup;
  104. // From the Service Provider, we can get the security Manager if there is one
  105. hr = iServiceProvider->QueryService(SID_SInternetHostSecurityManager,
  106. IID_IInternetHostSecurityManager,
  107. (LPVOID *)&m_iSecurity);
  108. // Either of these are equivalent to allowing the policy to go through
  109. // We have a Security Manager
  110. Cleanup:
  111. if (iServiceProvider)
  112. iServiceProvider->Release();
  113. return hr;
  114. }
  115. LPTSTR COlePrnSecurity::LoadResString(UINT uResId)
  116. /*++
  117. Routine Description:
  118. Allocate and return a resource string.
  119. Parameters:
  120. uResId - Resource Id to load
  121. Return Value:
  122. The String or NULL
  123. --*/
  124. {
  125. TCHAR szStr[dwMaxResBuf];
  126. DWORD dwLength;
  127. LPTSTR lpszRet = NULL;
  128. dwLength = LoadString(_Module.GetResourceInstance(), uResId, szStr, dwMaxResBuf);
  129. if (dwLength == 0)
  130. goto Cleanup;
  131. dwLength = (dwLength + 1)*sizeof(TCHAR);
  132. lpszRet = (LPTSTR)LocalAlloc( LPTR, dwLength );
  133. if (NULL == lpszRet)
  134. goto Cleanup;
  135. lstrcpy( lpszRet, szStr );
  136. Cleanup:
  137. return lpszRet;
  138. }
  139. BOOL COlePrnSecurity::InitStrings(void)
  140. /*++
  141. Routine Description:
  142. Initialise all of the security strings. It either allocates all of them or none
  143. Return Value:
  144. TRUE if successful, False otherwise
  145. --*/
  146. {
  147. BOOL bRet = TRUE;
  148. for(DWORD dwIndex = StartMessages; dwIndex < (EndMessages*2); dwIndex++) {
  149. m_MsgStrings[dwIndex] = LoadResString(START_SECURITY_DIALOGUE_RES + dwIndex);
  150. if (NULL == m_MsgStrings[dwIndex]) {
  151. DeallocStrings(); // Deallocate any we have allocated
  152. bRet = FALSE;
  153. break;
  154. }
  155. }
  156. return bRet;
  157. }
  158. void COlePrnSecurity::DeallocStrings(void)
  159. /*++
  160. Routine Description:
  161. Deallocate all of the security strings
  162. --*/
  163. {
  164. for(DWORD dwIndex = StartMessages; dwIndex < (EndMessages*2); dwIndex++) {
  165. if (NULL != m_MsgStrings[dwIndex]) {
  166. LocalFree( m_MsgStrings[dwIndex]);
  167. m_MsgStrings[dwIndex] = NULL;
  168. }
  169. }
  170. }
  171. HRESULT COlePrnSecurity::PromptUser(SecurityMessage eMessage,
  172. LPTSTR lpszOther)
  173. /*++
  174. Routine Description:
  175. Prompt the user with a [Yes]/[No] Message Box based on the message passed in and
  176. the other string passed in (which is substituted in with sprintf()
  177. Parameters:
  178. eMessage - The Message to display
  179. lpszOther - Other Data to display
  180. Return Value:
  181. E_POINTER - lpszOther was NULL
  182. E_OUTOFMEMORY - Could not allocate temporary storage
  183. E_UNEXPECTED - sprintf wrote more character than we thought
  184. S_OK - The Dialogue Box was displayed and the user selected [Yes]
  185. S_FALSE - The Dialogue Box was displayed and the user selected [No]
  186. --*/
  187. {
  188. HRESULT hr = E_POINTER;
  189. DWORD dwIndex = ((DWORD)eMessage)*2;
  190. LPTSTR lpszMessage = NULL;
  191. DWORD dwLength;
  192. int iMBRes;
  193. if (NULL == lpszOther)
  194. goto Cleanup;
  195. _ASSERTE( dwIndex < EndMessages ); // Must be a valid message
  196. _ASSERTE( m_MsgStrings[dwIndex ] != NULL ); // The table must have been initialised
  197. _ASSERTE( m_MsgStrings[dwIndex + 1] != NULL );
  198. // Required Length of the message string
  199. dwLength = lstrlen( m_MsgStrings[dwIndex+1] ) + lstrlen( lpszOther ) + 1;
  200. lpszMessage = (LPTSTR)LocalAlloc( LPTR , dwLength * sizeof(TCHAR) );
  201. if (NULL == lpszMessage)
  202. goto Cleanup;
  203. if (_sntprintf(lpszMessage, dwLength, m_MsgStrings[dwIndex+1], lpszOther ) < 0) {
  204. hr = E_UNEXPECTED;
  205. goto Cleanup;
  206. }
  207. // Now display the MessageBox
  208. iMBRes = MessageBox( NULL,
  209. lpszMessage,
  210. m_MsgStrings[dwIndex],
  211. MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2 );
  212. switch(iMBRes) {
  213. case IDYES: hr = S_OK; break;
  214. case IDNO: hr = S_FALSE; break;
  215. default: hr = E_UNEXPECTED; break;
  216. }
  217. Cleanup:
  218. if (NULL != lpszMessage)
  219. LocalFree( lpszMessage );
  220. return hr;
  221. }
  222. /***********************************************************************************
  223. ** End of File (prnsec.cpp)
  224. **********************************************************************************/