libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
COMStuff.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "exception.hpp"
21 
22 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace NTUtils {
23 
24  /* An HRESULT is a 32-bit bit-field constructed thus:
25  31 30-27 26-16 15-0
26  Severity Reserved Facility Actual
27  code. code code, <=0x200
28  0=success Indicates
29  1=failure responsible
30  module.
31  */
32 
33  typedef enum {
34  all_ok=S_OK,
35  standard_error=S_FALSE,
36  unknown_error=MAKE_HRESULT(1,FACILITY_ITF,0x201),
37  unknown_exception=MAKE_HRESULT(1,FACILITY_ITF,0x202),
38  stl_exception=MAKE_HRESULT(1,FACILITY_ITF,0x203),
39  com_exception=MAKE_HRESULT(1,FACILITY_ITF,0x204),
40  jmmcg_exception=MAKE_HRESULT(1,FACILITY_ITF,0x205)
41  } COM_error_codes;
42 
43  class COMInit {
44  public:
45  explicit inline __stdcall COMInit(const unsigned long CoInitType = COINIT_MULTITHREADED) {
46 #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
47  const HRESULT hr=::CoInitializeEx(NULL, CoInitType);
48 #else
49  const HRESULT hr=::CoInitialize(NULL);
50 #endif
51  JMMCG_TRACE(_T("COMInit::ctor: ::CoInitializeEx(..) return code: ")<<win_exception::StrFromWinErr(hr));
52  assert(SUCCEEDED(hr));
53  }
54  inline __stdcall ~COMInit(void) noexcept(true) {
55  try {
56  ::CoUninitialize();
57  } catch (...) {
58  JMMCG_TRACE(_T("COMInit::dtor: ::CoInitializeEx(..) failed."));
59  assert(!_T("COMInit::dtor: ::CoInitializeEx(..) failed."));
60  }
61  }
62 
63  private:
64  // I don't allow copying or assigment.
65  inline __stdcall COMInit(const COMInit &) noexcept(true);
66  inline COMInit & __fastcall operator=(const COMInit &) noexcept(true);
67  };
68 
70  public:
71  inline __stdcall COMSecuritySettings(const tstring &u,const tstring &domain_n_username,const tstring &p,const unsigned long AuthnLevel=RPC_C_AUTHN_LEVEL_CONNECT,const unsigned long ImpersonationLevel=RPC_C_IMP_LEVEL_IMPERSONATE)
73  Create(AuthnLevel,ImpersonationLevel);
74  }
75  inline __stdcall COMSecuritySettings(const tstring &u,const tstring &d,const tstring &us,const tstring &p,const unsigned long AuthnLevel=RPC_C_AUTHN_LEVEL_CONNECT,const unsigned long ImpersonationLevel=RPC_C_IMP_LEVEL_IMPERSONATE)
77  Create(AuthnLevel,ImpersonationLevel);
78  }
79  inline __stdcall COMSecuritySettings(const COMSecuritySettings &ss)
81  authid.User=reinterpret_cast<USHORT *>(const_cast<TCHAR *>(username.c_str()));
82  authid.UserLength=username.size();
83  authid.Domain=reinterpret_cast<USHORT *>(const_cast<TCHAR *>(domain.c_str()));
84  authid.DomainLength=domain.size();
85  authid.Password=reinterpret_cast<USHORT *>(const_cast<TCHAR *>(password.c_str()));
86  authid.PasswordLength=password.size();
87  authid.Flags=
88 #ifdef UNICODE
89  SEC_WINNT_AUTH_IDENTITY_UNICODE;
90 #else
91  SEC_WINNT_AUTH_IDENTITY_ANSI;
92 #endif
93  authinfo.dwAuthnSvc=ss.authinfo.dwAuthnSvc;
94  authinfo.dwAuthzSvc=ss.authinfo.dwAuthzSvc;
95  authinfo.pwszServerPrincName=ss.authinfo.pwszServerPrincName;
96  authinfo.dwAuthnLevel=ss.authinfo.dwAuthnLevel;
97  authinfo.dwImpersonationLevel=ss.authinfo.dwCapabilities;
98  authinfo.pAuthIdentityData=&authid;
99  authinfo.dwCapabilities=ss.authinfo.dwCapabilities;
100  serverinfo.dwReserved1=serverinfo.dwReserved2=0;
101  serverinfo.pwszName=const_cast<wchar_t *>(unc.c_str());
102  serverinfo.pAuthInfo=&authinfo;
103  }
104  inline __stdcall ~COMSecuritySettings(void) {
105  }
106 
107  inline const COSERVERINFO & __fastcall ServerInfo(void) const noexcept(true) {
108  return serverinfo;
109  }
110  inline COSERVERINFO & __fastcall ServerInfo(void) noexcept(true) {
111  return serverinfo;
112  }
113 
114  inline const tstring & __fastcall Domain(void) const noexcept(true) {
115  return domain;
116  }
117  inline const tstring & __fastcall Username(void) const noexcept(true) {
118  return username;
119  }
120 
121  private:
122  const std::wstring unc;
123  const tstring username;
124  const tstring domain;
125  const tstring password;
126  COAUTHIDENTITY authid;
127  COAUTHINFO authinfo;
128  COSERVERINFO serverinfo;
129 
130  inline void __fastcall Create(const unsigned long AuthnLevel,const unsigned long ImpersonationLevel) {
131  authid.User=reinterpret_cast<USHORT *>(const_cast<TCHAR *>(username.c_str()));
132  authid.UserLength=username.size();
133  authid.Domain=reinterpret_cast<USHORT *>(const_cast<TCHAR *>(domain.c_str()));
134  authid.DomainLength=domain.size();
135  authid.Password=reinterpret_cast<USHORT *>(const_cast<TCHAR *>(password.c_str()));
136  authid.PasswordLength=password.size();
137  authid.Flags=
138 #ifdef UNICODE
139  SEC_WINNT_AUTH_IDENTITY_UNICODE;
140 #else
141  SEC_WINNT_AUTH_IDENTITY_ANSI;
142 #endif
143  authinfo.dwAuthnSvc=RPC_C_AUTHN_WINNT;
144  authinfo.dwAuthzSvc=RPC_C_AUTHZ_NONE;
145  authinfo.pwszServerPrincName=NULL;
146  authinfo.dwAuthnLevel=AuthnLevel;
147  authinfo.dwImpersonationLevel=ImpersonationLevel;
148  authinfo.pAuthIdentityData=&authid;
149  authinfo.dwCapabilities=EOAC_NONE;
150  serverinfo.dwReserved1=serverinfo.dwReserved2=0;
151  serverinfo.pwszName=const_cast<wchar_t *>(unc.c_str());
152  serverinfo.pAuthInfo=&authinfo;
153  }
154 
155  // I don't allow assignment.
156  inline COMSecuritySettings & __fastcall operator=(const COMSecuritySettings &) noexcept(true);
157  };
158 
160  public:
161  inline __stdcall InitCOMSecurity(void)
162  : com(),sd() {
163  // Set up authentication - any user can access this object. See pg 469,
164  // "Professional ATL COM Programming".
165  const HRESULT hr=::CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_NONE,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);
166  JMMCG_TRACE(_T("InitCOMSecurity::InitCOMSecurity(): ::CoInitializeSecurity(..) return code: ")<<win_exception::StrFromWinErr(hr));
167  }
168  inline __stdcall InitCOMSecurity(SecuritySettings *s)
169  : com(),sd(s) {
170  assert(sd.get());
171  // Set up authentication - any user can access this object. See pg 469,
172  // "Professional ATL COM Programming".
173  const HRESULT hr=::CoInitializeSecurity(&sd->SD(),-1,NULL,NULL,RPC_C_AUTHN_LEVEL_CONNECT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_SECURE_REFS,NULL);
174  JMMCG_TRACE(_T("InitCOMSecurity::InitCOMSecurity(): ::CoInitializeSecurity(..) return code: ")<<win_exception::StrFromWinErr(hr));
175  }
176  inline __stdcall ~InitCOMSecurity(void) {
177  }
178 
179  private:
180  const COMInit com;
181  const std::auto_ptr<SecuritySettings> sd;
182 
183  // I don't copying or allow assignment.
184  inline __stdcall InitCOMSecurity(const InitCOMSecurity &) noexcept(true);
185  inline InitCOMSecurity & __fastcall operator=(const InitCOMSecurity &) noexcept(true);
186  };
187 
188 } } }