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.

228 lines
8.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/security/security_initialisers.hpp (originally MOSecFns.h, ::SynesisCom)
  3. *
  4. * Purpose: Contains classes for initialising COM/OLE.
  5. *
  6. * Created: 1st February 1998
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1998-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file comstl/security/security_initialisers.hpp
  40. *
  41. * \brief [C++ only] Functions for initialising COM security
  42. * (\ref group__library__security "Security" Library).
  43. */
  44. #ifndef COMSTL_INCL_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS
  45. #define COMSTL_INCL_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define COMSTL_VER_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS_MAJOR 4
  48. # define COMSTL_VER_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS_MINOR 1
  49. # define COMSTL_VER_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS_REVISION 2
  50. # define COMSTL_VER_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS_EDIT 46
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  56. # include <comstl/comstl.h>
  57. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  58. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_UNION_CAST
  59. # include <stlsoft/conversion/union_cast.hpp>
  60. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_UNION_CAST */
  61. #if ( !defined(_WIN32_WINNT) || \
  62. (_WIN32_WINNT < 0x0400)) && \
  63. !defined(_WIN32_DCOM)
  64. # error This file can only be used in the context of DCOM compilations.
  65. #endif /* _WIN32_WINNT < 0x0400 && !_WIN32_DCOM */
  66. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  67. _MSC_VER >= 1200
  68. # include <iaccess.h>
  69. #endif /* compiler */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Namespace
  72. */
  73. #ifndef _COMSTL_NO_NAMESPACE
  74. # if defined(_STLSOFT_NO_NAMESPACE) || \
  75. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  76. /* There is no stlsoft namespace, so must define ::comstl */
  77. namespace comstl
  78. {
  79. # else
  80. /* Define stlsoft::comstl_project */
  81. namespace stlsoft
  82. {
  83. namespace comstl_project
  84. {
  85. # endif /* _STLSOFT_NO_NAMESPACE */
  86. #endif /* !_COMSTL_NO_NAMESPACE */
  87. /* /////////////////////////////////////////////////////////////////////////
  88. * Constants & definitions.
  89. */
  90. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  91. const DWORD _EOAC_SEC_DESC = 0x0;
  92. const DWORD _EOAC_ACCESS_CONTROL = 0x4;
  93. const DWORD _EOAC_APPID = 0x8;
  94. const DWORD _EOAC_SECURITY_MASK = ( _EOAC_SEC_DESC |
  95. _EOAC_APPID |
  96. _EOAC_ACCESS_CONTROL);
  97. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  98. /* /////////////////////////////////////////////////////////////////////////
  99. * Functions
  100. */
  101. // function CoInitSecurity
  102. //
  103. // The three overloaded CoInitSecurity functions
  104. // provide a type-safe interface to the CoInitializeSecurity
  105. // API, discriminating between IAccessControl, AppID, and
  106. // SECURITY_DESCRIPTOR security information parameters.
  107. /** \brief Initialises the COM security libraries with an IAccessControl instance
  108. *
  109. * \ingroup group__library__security
  110. *
  111. */
  112. #ifdef __IAccessControl_INTERFACE_DEFINED__
  113. inline HRESULT CoInitSecurity( LPUNKNOWN punkAccessControl,
  114. LONG cAuthSvc,
  115. SOLE_AUTHENTICATION_SERVICE *asAuthSvc,
  116. DWORD dwAuthnLevel,
  117. DWORD dwImpLevel,
  118. RPC_AUTH_IDENTITY_HANDLE pAuthInfo,
  119. DWORD dwCapabilities)
  120. {
  121. // Use a "fake" IID constant, to avoid linker errors with compilers with old UUID.lib
  122. static const CLSID IID_IAccessControl__ = { 0xEEDD23E0, 0x8410, 0x11CE, { 0xA1, 0xC3, 0x08, 0x00, 0x2B, 0x2B, 0x8D, 0x8F } };
  123. HRESULT hr;
  124. IAccessControl *pac;
  125. /* Ensure correct flag. */
  126. dwCapabilities &= ~_EOAC_SECURITY_MASK;
  127. dwCapabilities |= _EOAC_ACCESS_CONTROL;
  128. hr = punkAccessControl->QueryInterface(IID_IAccessControl__, (void**)&pac);
  129. if(SUCCEEDED(hr))
  130. {
  131. hr = ::CoInitializeSecurity(pac, cAuthSvc, asAuthSvc, NULL, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities, NULL);
  132. pac->Release();
  133. }
  134. return hr;
  135. }
  136. #endif // __IAccessControl_INTERFACE_DEFINED__
  137. /** \brief Initialises the COM security libraries with an APPID
  138. *
  139. * \ingroup group__library__security
  140. *
  141. */
  142. inline HRESULT CoInitSecurity( GUID const &appid,
  143. LONG cAuthSvc,
  144. SOLE_AUTHENTICATION_SERVICE *asAuthSvc,
  145. DWORD dwAuthnLevel,
  146. DWORD dwImpLevel,
  147. RPC_AUTH_IDENTITY_HANDLE pAuthInfo,
  148. DWORD dwCapabilities)
  149. {
  150. /* Ensure correct flag. */
  151. dwCapabilities &= ~_EOAC_SECURITY_MASK;
  152. dwCapabilities |= _EOAC_APPID;
  153. // Since some compilers define CIS to take SECURITY_DESCRIPTOR*, we need to
  154. // do better than just cast the address of appid to void*
  155. return ::CoInitializeSecurity(stlsoft_ns_qual(union_cast)<SECURITY_DESCRIPTOR*>(&appid), cAuthSvc, asAuthSvc, NULL, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities, NULL);
  156. }
  157. /** \brief Initialises the COM security libraries with a security descriptor
  158. *
  159. * \ingroup group__library__security
  160. *
  161. */
  162. inline HRESULT CoInitSecurity( SECURITY_DESCRIPTOR *psd,
  163. LONG cAuthSvc,
  164. SOLE_AUTHENTICATION_SERVICE *asAuthSvc,
  165. DWORD dwAuthnLevel,
  166. DWORD dwImpLevel,
  167. RPC_AUTH_IDENTITY_HANDLE pAuthInfo,
  168. DWORD dwCapabilities)
  169. {
  170. /* Ensure correct flag. */
  171. dwCapabilities &= ~_EOAC_SECURITY_MASK;
  172. dwCapabilities |= _EOAC_SEC_DESC;
  173. return ::CoInitializeSecurity(psd, cAuthSvc, asAuthSvc, NULL, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities, NULL);
  174. }
  175. ////////////////////////////////////////////////////////////////////////////
  176. // Unit-testing
  177. #ifdef STLSOFT_UNITTEST
  178. # include "./unittest/security_initialisers_unittest_.h"
  179. #endif /* STLSOFT_UNITTEST */
  180. /* ////////////////////////////////////////////////////////////////////// */
  181. #ifndef _COMSTL_NO_NAMESPACE
  182. # if defined(_STLSOFT_NO_NAMESPACE) || \
  183. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  184. } // namespace comstl
  185. # else
  186. } // namespace stlsoft::comstl_project
  187. } // namespace stlsoft
  188. # endif /* _STLSOFT_NO_NAMESPACE */
  189. #endif /* !_COMSTL_NO_NAMESPACE */
  190. /* ////////////////////////////////////////////////////////////////////// */
  191. #endif /* !COMSTL_INCL_COMSTL_SECURITY_HPP_SECURITY_INITIALISERS */
  192. /* ///////////////////////////// end of file //////////////////////////// */