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.

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