component B.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:15k
- // local.cpp
- #define _WIN32_DCOM
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <iaccess.h> // IAccessControl
- #include <iostream.h> // For cout
- #include "registry.h" // For registry functions
- #include "Component Bcomponent.h" // Generated by MIDL
- #define MB_SERVICE_NOTIFICATION 0x00200000L
- void display_proxy_info(IUnknown* pUnknown)
- {
- DWORD AuthnSvc;
- DWORD AuthzSvc;
- OLECHAR* ServerPrincName;
- DWORD AuthnLevel;
- DWORD ImpLevel;
- RPC_AUTH_IDENTITY_HANDLE pAuthInfo;
- DWORD Capabilities;
- HRESULT hr = CoQueryProxyBlanket(pUnknown, &AuthnSvc, &AuthzSvc, &ServerPrincName, &AuthnLevel, &ImpLevel, &pAuthInfo, &Capabilities);
- if(FAILED(hr))
- cout << "CoQueryProxyBlanket failed" << endl;
- cout << endl << "Proxy security blanket information follows:" << endl;
- switch(AuthnSvc)
- {
- case RPC_C_AUTHN_NONE:
- cout << "Authentication service is RPC_C_AUTHN_NONE." << endl;;
- break;
- case RPC_C_AUTHN_GSS_NEGOTIATE:
- cout << "Authentication service is RPC_C_AUTHN_GSS_NEGOTIATE." << endl;
- break;
- case RPC_C_AUTHN_GSS_KERBEROS:
- cout << "Authentication service is RPC_C_AUTHN_GSS_KERBEROS." << endl;
- break;
- case RPC_C_AUTHN_WINNT:
- cout << "Authentication service is RPC_C_AUTHN_WINNT." << endl;
- break;
- case RPC_C_AUTHN_DEFAULT:
- cout << "Authentication service is RPC_C_AUTHN_DEFAULT." << endl;
- break;
- }
- switch(AuthzSvc)
- {
- case RPC_C_AUTHZ_NONE:
- cout << "Authorization service is RPC_C_AUTHZ_NONE." << endl;
- break;
- case RPC_C_AUTHZ_NAME:
- cout << "Authorization service is RPC_C_AUTHZ_NAME." << endl;
- break;
- case RPC_C_AUTHZ_DCE:
- cout << "Authorization service is RPC_C_AUTHZ_DCE." << endl;
- break;
- case RPC_C_AUTHZ_DEFAULT:
- cout << "Authorization service is RPC_C_AUTHZ_DEFAULT." << endl;
- break;
- }
- wprintf(L"The current principal name is %s.n", ServerPrincName);
- CoTaskMemFree(ServerPrincName);
- switch(AuthnLevel)
- {
- case RPC_C_AUTHN_LEVEL_DEFAULT:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_DEFAULT." << endl;
- break;
- case RPC_C_AUTHN_LEVEL_NONE:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_NONE." << endl;
- break;
- case RPC_C_AUTHN_LEVEL_CONNECT:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_CONNECT." << endl;
- break;
- case RPC_C_AUTHN_LEVEL_CALL:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_CALL." << endl;
- break;
- case RPC_C_AUTHN_LEVEL_PKT:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_PKT." << endl;
- break;
- case RPC_C_AUTHN_LEVEL_PKT_INTEGRITY:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_PKT_INTEGRITY." << endl;
- break;
- case RPC_C_AUTHN_LEVEL_PKT_PRIVACY:
- cout << "Authentication level is RPC_C_AUTHN_LEVEL_PKT_PRIVACY." << endl;
- break;
- }
- switch(ImpLevel)
- {
- case RPC_C_IMP_LEVEL_DEFAULT:
- cout << "Impersonation level is RPC_C_IMP_LEVEL_DEFAULT." << endl;
- break;
- case RPC_C_IMP_LEVEL_ANONYMOUS:
- cout << "Impersonation level is RPC_C_IMP_LEVEL_ANONYMOUS." << endl;
- break;
- case RPC_C_IMP_LEVEL_IDENTIFY:
- cout << "Impersonation level is RPC_C_IMP_LEVEL_IDENTIFY." << endl;
- break;
- case RPC_C_IMP_LEVEL_IMPERSONATE:
- cout << "Impersonation level is RPC_C_IMP_LEVEL_IMPERSONATE." << endl;
- break;
- case RPC_C_IMP_LEVEL_DELEGATE:
- cout << "Impersonation level is RPC_C_IMP_LEVEL_DELEGATE." << endl;
- break;
- }
- // wprintf(L"The client identity user name is %s.n", ((SEC_WINNT_AUTH_IDENTITY_W*)pAuthInfo)->User);
- // wprintf(L"The client identity domain name is %s.n", ((SEC_WINNT_AUTH_IDENTITY_W*)pAuthInfo)->Domain);
- // wprintf(L"The client identity password is %s.n", ((SEC_WINNT_AUTH_IDENTITY_W*)pAuthInfo)->Password);
- if((Capabilities | EOAC_NONE) == 0)
- cout << "Capabilities include EOAC_NONE" << endl;
- if((Capabilities | EOAC_DEFAULT) == 0)
- cout << "Capabilities include EOAC_DEFAULT" << endl;
- if((Capabilities | EOAC_MUTUAL_AUTH) == 0)
- cout << "Capabilities include EOAC_MUTUAL_AUTH" << endl;
- if((Capabilities | EOAC_STATIC_CLOAKING) == 0)
- cout << "Capabilities include EOAC_STATIC_CLOAKING" << endl;
- if((Capabilities | EOAC_DYNAMIC_CLOAKING) == 0)
- cout << "Capabilities include EOAC_DYNAMIC_CLOAKING" << endl;
- if((Capabilities | EOAC_SECURE_REFS) == 0)
- cout << "Capabilities include EOAC_SECURE_REFS" << endl;
- if((Capabilities | EOAC_ACCESS_CONTROL) == 0)
- cout << "Capabilities include EOAC_ACCESS_CONTROL" << endl;
- if((Capabilities | EOAC_APPID) == 0)
- cout << "Capabilities include EOAC_APPID" << endl;
- if((Capabilities | EOAC_DISABLE_AAA) == 0)
- cout << "Capabilities include EOAC_DISABLE_AAA" << endl;
- // _getch();
- }
- void funcs()
- {
- DWORD AuthnSvc = 0;
- DWORD AuthzSvc = 0;
- DWORD ImpLevel = 0;
- OLECHAR* ServerPrincName;
- DWORD AuthnLevel = 0;
- RPC_AUTHZ_HANDLE Privs = 0;
- DWORD Capabilities = 0;
- HRESULT hr = CoQueryClientBlanket(&AuthnSvc, &AuthzSvc, &ServerPrincName, &AuthnLevel, NULL, &Privs, &Capabilities);
- if(FAILED(hr))
- cout << "CoQueryClientBlanket failed" << endl;
- switch(AuthnSvc)
- {
- case RPC_C_AUTHN_NONE:
- cout << "RPC_C_AUTHN_NONE ";
- break;
- case RPC_C_AUTHN_GSS_NEGOTIATE :
- cout << "RPC_C_AUTHN_GSS_NEGOTIATE ";
- break;
- case RPC_C_AUTHN_GSS_KERBEROS :
- cout << "RPC_C_AUTHN_GSS_KERBEROS ";
- break;
- case RPC_C_AUTHN_WINNT:
- cout << "RPC_C_AUTHN_WINNT ";
- break;
- case RPC_C_AUTHN_DEFAULT:
- cout << "RPC_C_AUTHN_DEFAULT ";
- break;
- }
- switch(AuthzSvc)
- {
- case RPC_C_AUTHZ_NONE:
- cout << "RPC_C_AUTHZ_NONE ";
- break;
- case RPC_C_AUTHZ_NAME:
- cout << "RPC_C_AUTHZ_NAME ";
- break;
- case RPC_C_AUTHZ_DCE:
- cout << "RPC_C_AUTHZ_DCE ";
- break;
- }
- wprintf(L"ServerPrincName %s.n", ServerPrincName);
- CoTaskMemFree(ServerPrincName);
- switch(AuthnLevel)
- {
- case RPC_C_AUTHN_LEVEL_NONE:
- cout << "RPC_C_AUTHN_LEVEL_NONE ";
- break;
- case RPC_C_AUTHN_LEVEL_CONNECT:
- cout << "RPC_C_AUTHN_LEVEL_CONNECT ";
- break;
- case RPC_C_AUTHN_LEVEL_CALL:
- cout << "RPC_C_AUTHN_LEVEL_CALL ";
- break;
- case RPC_C_AUTHN_LEVEL_PKT:
- cout << "RPC_C_AUTHN_LEVEL_PKT ";
- break;
- case RPC_C_AUTHN_LEVEL_PKT_INTEGRITY:
- cout << "RPC_C_AUTHN_LEVEL_PKT_INTEGRITY ";
- break;
- case RPC_C_AUTHN_LEVEL_PKT_PRIVACY:
- cout << "RPC_C_AUTHN_LEVEL_PKT_PRIVACY ";
- break;
- }
- switch(ImpLevel)
- {
- case RPC_C_IMP_LEVEL_DEFAULT :
- cout << "RPC_C_IMP_LEVEL_DEFAULT ";
- break;
- case RPC_C_IMP_LEVEL_ANONYMOUS :
- cout << "RPC_C_IMP_LEVEL_ANONYMOUS ";
- break;
- case RPC_C_IMP_LEVEL_IDENTIFY :
- cout << "RPC_C_IMP_LEVEL_IDENTIFY ";
- break;
- case RPC_C_IMP_LEVEL_IMPERSONATE :
- cout << "RPC_C_IMP_LEVEL_IMPERSONATE ";
- break;
- case RPC_C_IMP_LEVEL_DELEGATE :
- cout << "RPC_C_IMP_LEVEL_DELEGATE ";
- break;
- }
- cout << endl;
- wprintf(L"Privs %sn", Privs);
- if((Capabilities | EOAC_NONE) == 0)
- cout << "Capabilities include EOAC_NONE" << endl;
- if((Capabilities | EOAC_DEFAULT) == 0)
- cout << "Capabilities include EOAC_DEFAULT" << endl;
- if((Capabilities | EOAC_MUTUAL_AUTH) == 0)
- cout << "Capabilities include EOAC_MUTUAL_AUTH" << endl;
- if((Capabilities | EOAC_STATIC_CLOAKING) == 0)
- cout << "Capabilities include EOAC_STATIC_CLOAKING" << endl;
- if((Capabilities | EOAC_DYNAMIC_CLOAKING) == 0)
- cout << "Capabilities include EOAC_DYNAMIC_CLOAKING" << endl;
- if((Capabilities | EOAC_SECURE_REFS) == 0)
- cout << "Capabilities include EOAC_SECURE_REFS" << endl;
- if((Capabilities | EOAC_ACCESS_CONTROL) == 0)
- cout << "Capabilities include EOAC_ACCESS_CONTROL" << endl;
- if((Capabilities | EOAC_APPID) == 0)
- cout << "Capabilities include EOAC_APPID" << endl;
- if((Capabilities | EOAC_DISABLE_AAA) == 0)
- cout << "Capabilities include EOAC_DISABLE_AAA" << endl;
- }
- const IID IID_IAccessControl = {0xEEDD23E0,0x8410,0x11CE,{0xA1,0xC3,0x08,0x00,0x2B,0x2B,0x8D,0x8F}};
- long g_cComponents = 0;
- long g_cServerLocks = 0;
- HANDLE g_hEvent;
- class CInsideCOM : public ISum
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // ISum
- HRESULT __stdcall Sum(int x, int y, int* retval);
- CInsideCOM() : m_cRef(1) { g_cComponents++; }
- ~CInsideCOM() { g_cComponents--; }
- private:
- ULONG m_cRef;
- };
- ULONG CInsideCOM::AddRef()
- {
- return ++m_cRef;
- }
- ULONG CInsideCOM::Release()
- {
- if(--m_cRef != 0)
- return m_cRef;
- SetEvent(g_hEvent);
- delete this;
- return 0;
- }
- HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- *ppv = reinterpret_cast<IUnknown*>(this);
- else if(riid == IID_ISum)
- *ppv = (ISum*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CInsideCOM::Sum(int x, int y, int* retval)
- {
- ISum* pSum = 0;
- IUnknown* pUnknown = 0;
- HRESULT hr;
- COSERVERINFO ServerInfo;
- ServerInfo.dwReserved1 = 0;
- ServerInfo.pwszName = L"Guy-2031";
- ServerInfo.pAuthInfo = 0;
- ServerInfo.dwReserved2 = 0;
- MULTI_QI qi;
- qi.pIID = &IID_IUnknown;
- qi.pItf = NULL;
- qi.hr = 0;
- /*
- HANDLE handle = 0;
- handle = CreateFile("\\Guy-2031\C\test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(handle != INVALID_HANDLE_VALUE)
- {
- char buffer[25];
- DWORD bytes_read = 0;
- ReadFile(handle, buffer, 25, &bytes_read, NULL);
- CloseHandle(handle);
- buffer[bytes_read] = 0;
- cout << buffer << " Read from file" << endl;
- }
- else
- cout << "access denied to file" << endl;
- */
- hr = CoImpersonateClient();
- if(FAILED(hr))
- printf("CoImpersonateClient failed %0xn", hr);
- hr = CoCreateInstanceEx(CLSID_InsideCOM_C, NULL, CLSCTX_REMOTE_SERVER, &ServerInfo, 1, &qi);
- pUnknown = qi.pItf;
- if(FAILED(hr))
- {
- if(hr == E_ACCESSDENIED)
- printf("CoCreateInstance FAILED hr = E_ACCESSDENIEDn", hr);
- else
- printf("CoCreateInstance FAILED hr = %0xn", hr);
- _getch();
- exit(0);
- }
- hr = CoSetProxyBlanket(pUnknown, RPC_C_AUTHN_GSS_KERBEROS, RPC_C_AUTHZ_DEFAULT, COLE_DEFAULT_PRINCIPAL, // L"GuysDomain\B",
- RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_DEFAULT, COLE_DEFAULT_AUTHINFO, EOAC_DEFAULT);
- display_proxy_info(pUnknown);
- cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
- hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
- if(FAILED(hr))
- {
- printf("QueryInterface ISUm FAILED %0xn", hr);
- _getch();
- exit(0);
- }
- cout << "Client: Calling Release() for pUnknown" << endl;
- hr = pUnknown->Release();
- cout << "Client: pSum = " << pSum << endl;
- display_proxy_info(pSum);
- _getch();
- hr = CoSetProxyBlanket(pSum, RPC_C_AUTHN_GSS_KERBEROS, RPC_C_AUTHZ_DEFAULT, COLE_DEFAULT_PRINCIPAL, // L"GuysDomain\B",
- RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_DEFAULT, COLE_DEFAULT_AUTHINFO, EOAC_DEFAULT);
- if(FAILED(hr))
- printf("CoSetProxyBlanket 2 FAILED hr = %0xn", hr);
- display_proxy_info(pSum);
- _getch();
- int sum;
- hr = pSum->Sum(4, 9, &sum);
- if(SUCCEEDED(hr))
- cout << "Client: Calling Sum() return value is " << sum << endl;
- if(FAILED(hr))
- printf("Sum failed %0xn", hr);
- cout << "Client: Calling Release() for pSum" << endl;
- hr = pSum->Release();
- *retval = sum;
- CoRevertToSelf();
- return S_OK;
- }
- class CFactory : public IClassFactory
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // IClassFactory
- HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
- HRESULT __stdcall LockServer(BOOL bLock);
- CFactory() : m_cRef(1) { }
- ~CFactory() { }
- private:
- ULONG m_cRef;
- };
- ULONG CFactory::AddRef()
- {
- return ++m_cRef;
- }
- ULONG CFactory::Release()
- {
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
- {
- if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
- *ppv = (IClassFactory*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
- {
- if(pUnknownOuter != NULL)
- return CLASS_E_NOAGGREGATION;
- CInsideCOM *pInsideCOM = new CInsideCOM;
- cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
- if(pInsideCOM == NULL)
- return E_OUTOFMEMORY;
- // QueryInterface probably for IID_IUNKNOWN
- HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
- pInsideCOM->Release();
- return hr;
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- if(bLock)
- g_cServerLocks++;
- else
- g_cServerLocks--;
- return S_OK;
- }
- void RegisterComponent()
- {
- ITypeLib* pTypeLib;
- LoadTypeLibEx(L"Component B.exe", REGKIND_DEFAULT, &pTypeLib);
- pTypeLib->Release();
- RegisterServer("Component B.exe", CLSID_InsideCOM_B, "Inside COM+ Sample", "Component.InsideCOM_B", "Component.InsideCOM_B.1", NULL);
- }
- void CommandLineParameters(int argc, char** argv)
- {
- RegisterComponent();
- if(argc < 2)
- {
- cout << "No parameter, but registered anyway" << endl;
- exit(false);
- }
- char* szToken = strtok(argv[1], "-/");
- if(_stricmp(szToken, "RegServer") == 0)
- {
- RegisterComponent();
- cout << "RegServer" << endl;
- exit(true);
- }
- if(_stricmp(szToken, "UnregServer") == 0)
- {
- UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
- UnregisterServer(CLSID_InsideCOM_B, "Component.InsideCOM_B", "Component.InsideCOM_B.1");
- cout << "UnregServer" << endl;
- exit(true);
- }
- if(_stricmp(szToken, "Embedding") != 0)
- {
- cout << "Invalid parameter" << endl;
- exit(false);
- }
- }
- void main(int argc, char** argv)
- {
- HRESULT hr;
- CommandLineParameters(argc, argv);
- cout << "Component: CoInitializeEx()" << endl;
- hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
- if(FAILED(hr))
- cout << "CoInitializeEx Failed" << endl;
- SEC_WINNT_AUTH_IDENTITY_W identity;
- identity.User = L"B";
- identity.UserLength = wcslen(L"B");
- identity.Domain = L"GuysDomain";
- identity.DomainLength = wcslen(L"GuysDomain");
- identity.Password = L"B";
- identity.PasswordLength = wcslen(L"B");
- identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
- SOLE_AUTHENTICATION_INFO info;
- info.dwAuthnSvc = RPC_C_AUTHN_GSS_KERBEROS;
- info.dwAuthzSvc = RPC_C_AUTHZ_NONE;
- info.pAuthInfo = &identity;
-
- SOLE_AUTHENTICATION_LIST list;
- list.cAuthInfo = 1;
- list.aAuthInfo = &info;
- SOLE_AUTHENTICATION_SERVICE service;
- service.dwAuthnSvc = RPC_C_AUTHN_GSS_KERBEROS;
- service.dwAuthzSvc = RPC_C_AUTHZ_NONE;
- service.hr = 0;
- service.pPrincipalName = L"GuysDomain\B";
- hr = CoInitializeSecurity(0, 1, &service, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_DELEGATE,
- NULL, EOAC_NONE, NULL);
- if(FAILED(hr))
- {
- printf("CoInitializeSecurity Failed = %0xn", hr);
- _getch();
- }
- printf("CoInitializeSecurity service.hr = %0xn", service.hr);
- IClassFactory *pClassFactory = new CFactory();
- cout << "Component: CoRegisterClassObject()" << endl;
- DWORD dwRegister;
- CoRegisterClassObject(CLSID_InsideCOM_B, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
- g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- WaitForSingleObject(g_hEvent, INFINITE);
- CoRevokeClassObject(dwRegister);
- pClassFactory->Release();
- CoUninitialize();
- cout << "By Bye" << endl;
- // _getch();
- }