local with CoInitializeSecurity.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:10k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // local.cpp
  2. #define _WIN32_DCOM
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <iaccess.h>   // IAccessControl
  7. #include <iostream.h>  // For cout
  8. #include "registry.h"  // For registry functions
  9. #include "Component CoInitializeSecuritycomponent.h" // Generated by MIDL
  10. const IID IID_IAccessControl =
  11. {0xEEDD23E0,0x8410,0x11CE,{0xA1,0xC3,0x08,0x00,0x2B,0x2B,0x8D,0x8F}};
  12. long g_cComponents = 0;
  13. long g_cServerLocks = 0;
  14. HANDLE g_hEvent;
  15. class CInsideCOM : public ISum
  16. {
  17. public:
  18. // IUnknown
  19. ULONG __stdcall AddRef();
  20. ULONG __stdcall Release();
  21. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  22. // ISum
  23. HRESULT __stdcall Sum(int x, int y, int* retval);
  24. CInsideCOM() : m_cRef(1) { g_cComponents++; }
  25. ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
  26. private:
  27. ULONG m_cRef;
  28. };
  29. ULONG CInsideCOM::AddRef()
  30. {
  31. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  32. return ++m_cRef;
  33. }
  34. ULONG CInsideCOM::Release()
  35. {
  36. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  37. if(--m_cRef != 0)
  38. return m_cRef;
  39. SetEvent(g_hEvent); // ADD THIS!!!
  40. delete this;
  41. return 0;
  42. }
  43. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  44. {
  45. if(riid == IID_IUnknown)
  46. {
  47. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  48. *ppv = reinterpret_cast<IUnknown*>(this);
  49. }
  50. else if(riid == IID_ISum)
  51. {
  52. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  53. *ppv = (ISum*)this;
  54. }
  55. else 
  56. {
  57. *ppv = NULL;
  58. return E_NOINTERFACE;
  59. }
  60. AddRef();
  61. return S_OK;
  62. }
  63. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  64. {
  65. HRESULT hr;
  66. IServerSecurity* pServerSecurity;
  67. hr = CoGetCallContext(IID_IServerSecurity, (void**)&pServerSecurity);
  68. if(FAILED(hr))
  69. cout << "CoGetCallContext failed" << endl;
  70. DWORD AuthnSvc;
  71. DWORD AuthzSvc;
  72. OLECHAR* ServerPrincNam;
  73. DWORD AuthnLevel;
  74. RPC_AUTHZ_HANDLE Privs;
  75. DWORD Capabilities;
  76. // AuthzSvc is ignored when using the RPC_C_AUTHN_WINNT authentication service.
  77. //                             RPC_C_AUTHN_WINNT, ignored,    Administrator, RPC_C_AUTHN_LEVEL_PKT, Thing4Administrator, EOAC_NONE
  78. hr = pServerSecurity->QueryBlanket(&AuthnSvc, &AuthzSvc, &ServerPrincNam, &AuthnLevel, NULL, &Privs, &Capabilities);
  79. //                                                                       impersonation level is never returned
  80. if(FAILED(hr))
  81. cout << "QueryBlanket failed" << endl;
  82. pServerSecurity->Release();
  83. switch(AuthnSvc)
  84. {
  85. case RPC_C_AUTHN_NONE:
  86. cout << "RPC_C_AUTHN_NONE ";
  87. break;
  88. case RPC_C_AUTHN_DCE_PRIVATE:
  89. cout << "RPC_C_AUTHN_DCE_PRIVATE ";
  90. break;
  91. case RPC_C_AUTHN_DCE_PUBLIC:
  92. cout << "RPC_C_AUTHN_DCE_PUBLIC ";
  93. break;
  94. case RPC_C_AUTHN_WINNT:
  95. cout << "RPC_C_AUTHN_WINNT ";
  96. break;
  97. case RPC_C_AUTHN_DEFAULT:
  98. cout << "RPC_C_AUTHN_DEFAULT ";
  99. break;
  100. }
  101. switch(AuthzSvc)
  102. {
  103. case RPC_C_AUTHZ_NONE:
  104. cout << "RPC_C_AUTHZ_NONE ";
  105. break;
  106. case RPC_C_AUTHZ_NAME:
  107. cout << "RPC_C_AUTHZ_NAME ";
  108. break;
  109. case RPC_C_AUTHZ_DCE:
  110. cout << "RPC_C_AUTHZ_DCE ";
  111. break;
  112. }
  113. wprintf(L"ServerPrincNam %sn", ServerPrincNam);
  114. switch(AuthnLevel)
  115. {
  116. case RPC_C_AUTHN_LEVEL_NONE:
  117. cout << "RPC_C_AUTHN_LEVEL_NONE ";
  118. break;
  119. case RPC_C_AUTHN_LEVEL_CONNECT:
  120. cout << "RPC_C_AUTHN_LEVEL_CONNECT ";
  121. break;
  122. case RPC_C_AUTHN_LEVEL_CALL:
  123. cout << "RPC_C_AUTHN_LEVEL_CALL ";
  124. break;
  125. case RPC_C_AUTHN_LEVEL_PKT:
  126. cout << "RPC_C_AUTHN_LEVEL_PKT ";
  127. break;
  128. case RPC_C_AUTHN_LEVEL_PKT_INTEGRITY:
  129. cout << "RPC_C_AUTHN_LEVEL_PKT_INTEGRITY ";
  130. break;
  131. case RPC_C_AUTHN_LEVEL_PKT_PRIVACY:
  132. cout << "RPC_C_AUTHN_LEVEL_PKT_PRIVACY ";
  133. break;
  134. }
  135. wprintf(L"Privs %sn", Privs);
  136. if(Capabilities == EOAC_NONE)
  137. cout << "EOAC_NONE" << endl;
  138. CoTaskMemFree(ServerPrincNam);
  139. cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  140. *retval = x + y;
  141. // _getch();
  142. return S_OK;
  143. }
  144. class CFactory : public IClassFactory
  145. {
  146. public:
  147. // IUnknown
  148. ULONG __stdcall AddRef();
  149. ULONG __stdcall Release();
  150. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  151. // IClassFactory
  152. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  153. HRESULT __stdcall LockServer(BOOL bLock);
  154. CFactory() : m_cRef(1) { }
  155. ~CFactory() { }
  156. private:
  157. ULONG m_cRef;
  158. };
  159. ULONG CFactory::AddRef()
  160. {
  161. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  162. return ++m_cRef;
  163. }
  164. ULONG CFactory::Release()
  165. {
  166. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  167. if(--m_cRef != 0)
  168. return m_cRef;
  169. delete this;
  170. return 0;
  171. }
  172. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  173. {
  174. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  175. {
  176. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  177. *ppv = (IClassFactory*)this;
  178. }
  179. else
  180. {
  181. *ppv = NULL;
  182. return E_NOINTERFACE;
  183. }
  184. AddRef();
  185. return S_OK;
  186. }
  187. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  188. {
  189. if(pUnknownOuter != NULL)
  190. return CLASS_E_NOAGGREGATION;
  191. CInsideCOM *pInsideCOM = new CInsideCOM;
  192. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  193. if(pInsideCOM == NULL)
  194. return E_OUTOFMEMORY;
  195. // QueryInterface probably for IID_IUNKNOWN
  196. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  197. pInsideCOM->Release();
  198. return hr;
  199. }
  200. HRESULT CFactory::LockServer(BOOL bLock)
  201. {
  202. if(bLock)
  203. g_cServerLocks++;
  204. else
  205. g_cServerLocks--;
  206. return S_OK;
  207. }
  208. void RegisterComponent()
  209. {
  210. ITypeLib* pTypeLib;
  211. LoadTypeLibEx(L"Component CoInitializeSecurity.exe", REGKIND_DEFAULT, &pTypeLib);
  212. pTypeLib->Release();
  213. RegisterServer("Component CoInitializeSecurity.exe", CLSID_InsideCOM, "Inside COM Sample #1", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  214. }
  215. void CommandLineParameters(int argc, char** argv)
  216. {
  217. RegisterComponent();
  218. if(argc < 2)
  219. {
  220. cout << "No parameter, but registered anyway" << endl;
  221. exit(false);
  222. }
  223. char* szToken = strtok(argv[1], "-/"); 
  224. if(_stricmp(szToken, "RegServer") == 0)
  225. {
  226. RegisterComponent();
  227. cout << "RegServer" << endl;
  228. exit(true);
  229. }
  230. if(_stricmp(szToken, "UnregServer") == 0)
  231. {
  232. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  233. UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  234. cout << "UnregServer" << endl;
  235. exit(true);
  236. }
  237. if(_stricmp(szToken, "Embedding") != 0)
  238. {
  239. cout << "Invalid parameter" << endl;
  240. exit(false);
  241. }
  242. }
  243. void main(int argc, char** argv)
  244. {
  245. HRESULT hr;
  246. CommandLineParameters(argc, argv);
  247. cout << "Component: CoInitializeEx()" << endl;
  248. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  249. if(FAILED(hr))
  250. cout << "CoInitializeEx Failed" << endl;
  251.     // Create an COM access control object and get its IAccessControl interface
  252.     cout << "Creating an instance of CLSID_COMAccessControl" << endl;
  253.     IAccessControl* pAccessControl = NULL;     
  254.     hr = CoCreateInstance(CLSID_DCOMAccessControl, NULL, CLSCTX_INPROC_SERVER,
  255. IID_IAccessControl, (void**)&pAccessControl);
  256.     if(FAILED(hr))
  257. cout << "Couldn't create COM access control object" << endl;
  258.     // Setup the property list. We use the NULL property because we are
  259.     // trying to adjust the security of the object itself
  260.     ACTRL_ACCESSW access;
  261.     ACTRL_PROPERTY_ENTRYW propEntry;
  262.     access.cEntries = 1;
  263.     access.pPropertyAccessList = &propEntry;
  264.     
  265.     ACTRL_ACCESS_ENTRY_LISTW entryList;
  266.     propEntry.lpProperty = NULL;
  267.     propEntry.pAccessEntryList = &entryList;
  268.     propEntry.fListFlags = 0;
  269.     // Setup the access control list for the default property
  270.     ACTRL_ACCESS_ENTRYW entry;
  271.     entryList.cEntries = 1;
  272.     entryList.pAccessList = &entry;
  273.     // Setup the access control entry
  274.     entry.fAccessFlags = ACTRL_ACCESS_ALLOWED;
  275.     entry.Access = COM_RIGHTS_EXECUTE;
  276.     entry.ProvSpecificAccess = 0;
  277.     entry.Inheritance = NO_INHERITANCE;
  278.     entry.lpInheritProperty = NULL;
  279.     // NT requires the system account to have access (for launching)
  280.     entry.Trustee.pMultipleTrustee = NULL;
  281.     entry.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
  282.     entry.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  283.     entry.Trustee.TrusteeType = TRUSTEE_IS_USER;
  284.     entry.Trustee.ptstrName = L"NT Authority\System";
  285.     cout << "Setting access rights: Allow access to NT Authority\System" << endl;
  286.     hr = pAccessControl->SetAccessRights(&access);
  287.     if(FAILED(hr))
  288. cout << "Couldn't set access" << endl;
  289.     // Deny access to a user
  290.     entry.fAccessFlags = ACTRL_ACCESS_DENIED;
  291.     entry.Trustee.TrusteeType = TRUSTEE_IS_USER;
  292.     entry.Trustee.ptstrName = L"Domain\User";
  293.     wprintf(L"Setting access rights: Deny access to %sn",  entry.Trustee.ptstrName);
  294.     hr = pAccessControl->GrantAccessRights(&access);
  295.     if(FAILED(hr))
  296. cout << "Couldn't deny access" << endl;
  297.     // Grant access to everyone
  298.     entry.fAccessFlags = ACTRL_ACCESS_ALLOWED;
  299.     entry.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  300.     entry.Trustee.ptstrName = L"*";
  301.     cout << "Setting access rights: Allow access to *" << endl;
  302.     hr = pAccessControl->GrantAccessRights(&access);
  303.     if(FAILED(hr))
  304. cout << "Couldn't allow access" << endl;
  305. hr = CoInitializeSecurity(pAccessControl, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IDENTIFY, 
  306. NULL, EOAC_ACCESS_CONTROL, NULL);
  307. if(FAILED(hr))
  308. {
  309. printf("CoInitializeSecurity Failed = %0xn", hr);
  310. _getch();
  311. }
  312.     pAccessControl->Release();
  313. IClassFactory *pClassFactory = new CFactory();
  314. cout << "Component: CoRegisterClassObject()" << endl;
  315. DWORD dwRegister;
  316. CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
  317. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  318. WaitForSingleObject(g_hEvent, INFINITE);
  319. CoRevokeClassObject(dwRegister);
  320. pClassFactory->Release();
  321. CoUninitialize();
  322. }