client A.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:8k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include "Component Bcomponent.h"
  7. void display_proxy_info(IUnknown* pUnknown)
  8. {
  9. DWORD AuthnSvc;
  10. DWORD AuthzSvc;
  11. OLECHAR* ServerPrincName;
  12. DWORD AuthnLevel;
  13. DWORD ImpLevel;
  14. RPC_AUTH_IDENTITY_HANDLE pAuthInfo;
  15. DWORD Capabilities;
  16. HRESULT hr = CoQueryProxyBlanket(pUnknown, &AuthnSvc, &AuthzSvc, &ServerPrincName, &AuthnLevel, &ImpLevel, &pAuthInfo, &Capabilities);
  17. if(FAILED(hr))
  18. cout << "CoQueryProxyBlanket failed" << endl;
  19. cout << endl << "Proxy security blanket information follows:" << endl;
  20. switch(AuthnSvc)
  21. {
  22. case RPC_C_AUTHN_NONE:
  23. cout << "Authentication service is RPC_C_AUTHN_NONE." << endl;;
  24. break;
  25. case RPC_C_AUTHN_GSS_NEGOTIATE:
  26. cout << "Authentication service is RPC_C_AUTHN_GSS_NEGOTIATE." << endl;
  27. break;
  28. case RPC_C_AUTHN_GSS_KERBEROS:
  29. cout << "Authentication service is RPC_C_AUTHN_GSS_KERBEROS." << endl;
  30. break;
  31. case RPC_C_AUTHN_WINNT:
  32. cout << "Authentication service is RPC_C_AUTHN_WINNT." << endl;
  33. break;
  34. case RPC_C_AUTHN_DEFAULT:
  35. cout << "Authentication service is RPC_C_AUTHN_DEFAULT." << endl;
  36. break;
  37. }
  38. switch(AuthzSvc)
  39. {
  40. case RPC_C_AUTHZ_NONE:
  41. cout << "Authorization service is RPC_C_AUTHZ_NONE." << endl;
  42. break;
  43. case RPC_C_AUTHZ_NAME:
  44. cout << "Authorization service is RPC_C_AUTHZ_NAME." << endl;
  45. break;
  46. case RPC_C_AUTHZ_DCE:
  47. cout << "Authorization service is RPC_C_AUTHZ_DCE." << endl;
  48. break;
  49. case RPC_C_AUTHZ_DEFAULT:
  50. cout << "Authorization service is RPC_C_AUTHZ_DEFAULT." << endl;
  51. break;
  52. }
  53. wprintf(L"The current principal name is %s.n", ServerPrincName);
  54. CoTaskMemFree(ServerPrincName);
  55. switch(AuthnLevel)
  56. {
  57. case RPC_C_AUTHN_LEVEL_DEFAULT:
  58. cout << "Authentication level is RPC_C_AUTHN_LEVEL_DEFAULT." << endl;
  59. break;
  60. case RPC_C_AUTHN_LEVEL_NONE:
  61. cout << "Authentication level is RPC_C_AUTHN_LEVEL_NONE." << endl;
  62. break;
  63. case RPC_C_AUTHN_LEVEL_CONNECT:
  64. cout << "Authentication level is RPC_C_AUTHN_LEVEL_CONNECT." << endl;
  65. break;
  66. case RPC_C_AUTHN_LEVEL_CALL:
  67. cout << "Authentication level is RPC_C_AUTHN_LEVEL_CALL." << endl;
  68. break;
  69. case RPC_C_AUTHN_LEVEL_PKT:
  70. cout << "Authentication level is RPC_C_AUTHN_LEVEL_PKT." << endl;
  71. break;
  72. case RPC_C_AUTHN_LEVEL_PKT_INTEGRITY:
  73. cout << "Authentication level is RPC_C_AUTHN_LEVEL_PKT_INTEGRITY." << endl;
  74. break;
  75. case RPC_C_AUTHN_LEVEL_PKT_PRIVACY:
  76. cout << "Authentication level is RPC_C_AUTHN_LEVEL_PKT_PRIVACY." << endl;
  77. break;
  78. }
  79. switch(ImpLevel)
  80. {
  81. case RPC_C_IMP_LEVEL_DEFAULT:
  82. cout << "Impersonation level is RPC_C_IMP_LEVEL_DEFAULT." << endl;
  83. break;
  84. case RPC_C_IMP_LEVEL_ANONYMOUS:
  85. cout << "Impersonation level is RPC_C_IMP_LEVEL_ANONYMOUS." << endl;
  86. break;
  87. case RPC_C_IMP_LEVEL_IDENTIFY:
  88. cout << "Impersonation level is RPC_C_IMP_LEVEL_IDENTIFY." << endl;
  89. break;
  90. case RPC_C_IMP_LEVEL_IMPERSONATE:
  91. cout << "Impersonation level is RPC_C_IMP_LEVEL_IMPERSONATE." << endl;
  92. break;
  93. case RPC_C_IMP_LEVEL_DELEGATE:
  94. cout << "Impersonation level is RPC_C_IMP_LEVEL_DELEGATE." << endl;
  95. break;
  96. }
  97. // wprintf(L"The client identity user name is %s.n", ((SEC_WINNT_AUTH_IDENTITY_W*)pAuthInfo)->User);
  98. // wprintf(L"The client identity domain name is %s.n", ((SEC_WINNT_AUTH_IDENTITY_W*)pAuthInfo)->Domain);
  99. // wprintf(L"The client identity password is %s.n", ((SEC_WINNT_AUTH_IDENTITY_W*)pAuthInfo)->Password);
  100. if((Capabilities | EOAC_NONE) == 0)
  101. cout << "Capabilities include EOAC_NONE" << endl;
  102. if((Capabilities | EOAC_DEFAULT) == 0)
  103. cout << "Capabilities include EOAC_DEFAULT" << endl;
  104. if((Capabilities | EOAC_MUTUAL_AUTH) == 0)
  105. cout << "Capabilities include EOAC_MUTUAL_AUTH" << endl;
  106. if((Capabilities | EOAC_STATIC_CLOAKING) == 0)
  107. cout << "Capabilities include EOAC_STATIC_CLOAKING" << endl;
  108. if((Capabilities | EOAC_DYNAMIC_CLOAKING) == 0)
  109. cout << "Capabilities include EOAC_DYNAMIC_CLOAKING" << endl;
  110. if((Capabilities | EOAC_SECURE_REFS) == 0)
  111. cout << "Capabilities include EOAC_SECURE_REFS" << endl;
  112. if((Capabilities | EOAC_ACCESS_CONTROL) == 0)
  113. cout << "Capabilities include EOAC_ACCESS_CONTROL" << endl;
  114. if((Capabilities | EOAC_APPID) == 0)
  115. cout << "Capabilities include EOAC_APPID" << endl;
  116. if((Capabilities | EOAC_DISABLE_AAA) == 0)
  117. cout << "Capabilities include EOAC_DISABLE_AAA" << endl;
  118. // _getch();
  119. }
  120. void main()
  121. {
  122. cout << "Client: Calling CoInitializeEx()" << endl;
  123. HRESULT hr = 0;
  124. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  125. IUnknown* pUnknown;
  126. ISum* pSum;
  127. SEC_WINNT_AUTH_IDENTITY_W identity;
  128. identity.User = L"A";
  129. identity.UserLength = wcslen(L"A");
  130. identity.Domain = L"GuysDomain";
  131. identity.DomainLength = wcslen(L"GuysDomain");
  132. identity.Password = L"A";
  133. identity.PasswordLength = wcslen(L"A");
  134. identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  135. SOLE_AUTHENTICATION_INFO info;
  136. info.dwAuthnSvc = RPC_C_AUTHN_GSS_KERBEROS;
  137. info.dwAuthzSvc = RPC_C_AUTHZ_NONE;
  138. info.pAuthInfo = &identity;
  139. SOLE_AUTHENTICATION_LIST list;
  140. list.cAuthInfo = 1;
  141. list.aAuthInfo = &info;
  142. hr = CoInitializeSecurity(0, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_DELEGATE, 
  143. &list, EOAC_NONE, NULL);
  144. if(FAILED(hr))
  145. {
  146. printf("CoInitializeSecurity Failed = %0xn", hr);
  147. exit(0);
  148. }
  149. COAUTHIDENTITY AuthIdentity;
  150. AuthIdentity.User = L"A";
  151. AuthIdentity.UserLength = wcslen(L"A");
  152. AuthIdentity.Domain = L"GuysDomain";
  153. AuthIdentity.DomainLength = wcslen(L"GuysDomain");
  154. AuthIdentity.Password = L"A";
  155. AuthIdentity.PasswordLength = wcslen(L"A");
  156. AuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  157. COAUTHINFO AuthInfo;
  158. AuthInfo.dwAuthnSvc = RPC_C_AUTHN_GSS_KERBEROS;
  159. AuthInfo.dwAuthzSvc = RPC_C_AUTHZ_NONE;
  160. AuthInfo.pwszServerPrincName = L"GuysDomain\Oyarsa"; // Must specify machine prinicipal here
  161. AuthInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT;
  162. AuthInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_DELEGATE; // Computer account must be marked "Computer is trusted for delegation"
  163. AuthInfo.pAuthIdentityData = &AuthIdentity;
  164. AuthInfo.dwCapabilities = EOAC_NONE;
  165. COSERVERINFO ServerInfo;
  166. ServerInfo.dwReserved1 = 0;
  167. ServerInfo.pwszName =  L"Oyarsa";
  168. ServerInfo.pAuthInfo = &AuthInfo;
  169. ServerInfo.dwReserved2 = 0;
  170. MULTI_QI qi;
  171. qi.pIID = &IID_IUnknown;
  172. qi.pItf = NULL;
  173. qi.hr = 0;
  174. cout << "Client: Calling CoCreateInstanceEx() " << endl;
  175. hr = CoCreateInstanceEx(CLSID_InsideCOM_B, NULL, CLSCTX_REMOTE_SERVER, &ServerInfo, 1, &qi);
  176. pUnknown = qi.pItf;
  177. if(FAILED(hr))
  178. {
  179. if(hr == E_ACCESSDENIED)
  180. printf("CoCreateInstance FAILED hr = E_ACCESSDENIEDn", hr);
  181. else
  182. printf("CoCreateInstance FAILED hr = %0xn", hr);
  183. exit(0);
  184. }
  185. // display_proxy_info(pUnknown);
  186. // _getch();
  187. //
  188. // hr = CoSetProxyBlanket(pUnknown, RPC_C_AUTHN_DEFAULT, RPC_C_AUTHZ_DEFAULT, COLE_DEFAULT_PRINCIPAL, // L"GuysDomain\B", 
  189. // RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_DEFAULT, COLE_DEFAULT_AUTHINFO, EOAC_DEFAULT);
  190. // if(FAILED(hr))
  191. // {
  192. // printf("CoSetProxyBlanket 1 FAILED hr = %0xn", hr);
  193. // exit(0);
  194. // }
  195. display_proxy_info(pUnknown);
  196. cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
  197. hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
  198. if(FAILED(hr))
  199. {
  200. printf("QueryInterface for ISum FAILED %0xn", hr);
  201. exit(0);
  202. }
  203. hr = CoSetProxyBlanket(pSum, RPC_C_AUTHN_GSS_KERBEROS, RPC_C_AUTHZ_DEFAULT, COLE_DEFAULT_PRINCIPAL, // L"GuysDomain\B", 
  204. RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_DELEGATE, COLE_DEFAULT_AUTHINFO, EOAC_DEFAULT);
  205. if(FAILED(hr))
  206. printf("CoSetProxyBlanket 2 FAILED hr = %0xn", hr);
  207. display_proxy_info(pSum);
  208. cout << "Client: Calling Release() for pUnknown" << endl;
  209. hr = pUnknown->Release();
  210. cout << "Client: pSum = " << pSum << endl;
  211. int sum;
  212. hr = pSum->Sum(4, 9, &sum);
  213. if(SUCCEEDED(hr))
  214. cout << "Client: Calling Sum() return value is " << sum << endl;
  215. if(FAILED(hr))
  216. printf("Sum failed %0xn", hr);
  217. cout << "Client: Calling Release() for pSum" << endl;
  218. hr = pSum->Release();
  219. cout << "Client: Calling CoUninitialize()" << endl;
  220. CoUninitialize();
  221. }