Leaked source code of windows server 2003
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.

274 lines
7.8 KiB

  1. /****************************************************************************
  2. Microsoft RPC Version 1`1
  3. Copyright Microsoft Corp. 1992
  4. Hello Example
  5. FILE: kerbcli.c
  6. USAGE: client -n network_address
  7. -p protocol_sequence
  8. -e endpoint
  9. -o options
  10. PURPOSE: Client side of RPC distributed application
  11. FUNCTIONS: main() - binds to server and calls remote procedure
  12. COMMENTS:
  13. This distributed application prints a string such as "hello, world"
  14. on the server. The client manages its connection to the server.
  15. The client uses the binding handle hello_IfHandle defined in the
  16. file hello.h.
  17. ****************************************************************************/
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <rpc.h> // RPC API functions, types
  22. #include "kerbtest.h" // header file generated by MIDL compiler
  23. void Usage(char * pszProgramName)
  24. {
  25. fprintf(stderr, "Usage: %s\n", pszProgramName);
  26. fprintf(stderr, " -p protocol_sequence\n");
  27. fprintf(stderr, " -n network_address\n");
  28. fprintf(stderr, " -d delegation address\n");
  29. fprintf(stderr, " -e endpoint\n");
  30. fprintf(stderr, " -o options\n");
  31. fprintf(stderr, " -a authn level\n");
  32. fprintf(stderr, " -s authn service\n");
  33. fprintf(stderr, " -r recursiion level\n");
  34. fprintf(stderr, " -x shutdown server\n");
  35. fprintf(stderr, " -# number of times to call\n");
  36. fprintf(stderr, " -t target principal\n");
  37. exit(1);
  38. }
  39. int __cdecl
  40. main (argc, argv)
  41. int argc;
  42. char *argv[];
  43. {
  44. RPC_STATUS status; // returned by RPC API function
  45. unsigned char * pszProtocolSequence = "ncacn_ip_tcp";
  46. unsigned char * pszNetworkAddress = NULL;
  47. unsigned char * pszEndpoint = "30760";
  48. unsigned char * pszOptions = NULL;
  49. unsigned char * pszStringBinding = NULL;
  50. unsigned char * pszDelegationAddress = NULL;
  51. unsigned char * pszPrincipal = NULL;
  52. unsigned char PrincipalBuffer[100];
  53. ULONG PrincipalLength;
  54. ULONG AuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT;
  55. ULONG AuthnService = RPC_C_AUTHN_DCE_PRIVATE;
  56. ULONG RecursionLevel = 0;
  57. ULONG LoopCount = 1;
  58. BOOLEAN ShutdownService = FALSE;
  59. ULONG i;
  60. handle_t BindingHandle = NULL;
  61. // allow the user to override settings with command line switches
  62. for (i = 1; i < (ULONG) argc; i++) {
  63. if ((*argv[i] == '-') || (*argv[i] == '/')) {
  64. switch (tolower(*(argv[i]+1))) {
  65. case 'p': // protocol sequence
  66. pszProtocolSequence = argv[++i];
  67. break;
  68. case 'n': // network address
  69. pszNetworkAddress = argv[++i];
  70. break;
  71. case 'd': // network address
  72. pszDelegationAddress = argv[++i];
  73. break;
  74. case 'e':
  75. pszEndpoint = argv[++i];
  76. break;
  77. case 'o':
  78. pszOptions = argv[++i];
  79. break;
  80. case 't':
  81. pszPrincipal = argv[++i];
  82. break;
  83. case 'a':
  84. sscanf(argv[++i],"%d",&AuthnLevel);
  85. break;
  86. case 's':
  87. sscanf(argv[++i],"%d",&AuthnService);
  88. break;
  89. case 'r':
  90. sscanf(argv[++i],"%d",&RecursionLevel);
  91. break;
  92. case '#':
  93. sscanf(argv[++i],"%d",&LoopCount);
  94. break;
  95. case 'x':
  96. ShutdownService = TRUE;
  97. break;
  98. case 'h':
  99. case '?':
  100. default:
  101. Usage(argv[0]);
  102. }
  103. } else {
  104. Usage(argv[0]);
  105. }
  106. }
  107. //
  108. // If the principal is NULL, get it from the environment
  109. //
  110. if (pszPrincipal == NULL)
  111. {
  112. LPSTR pszUserRealm;
  113. LPSTR pszUserName;
  114. PrincipalBuffer[0] = '\0';
  115. pszUserRealm = getenv( "USERDOMAIN" );
  116. pszUserName = getenv( "USERNAME" );
  117. if (pszUserRealm != NULL)
  118. {
  119. strcpy(PrincipalBuffer, pszUserRealm);
  120. }
  121. if ((pszUserRealm != NULL) &&
  122. (pszUserName != NULL))
  123. {
  124. strcat(PrincipalBuffer,"\\");
  125. }
  126. if (pszUserName != NULL)
  127. {
  128. strcat(PrincipalBuffer,pszUserName);
  129. }
  130. pszPrincipal = PrincipalBuffer;
  131. }
  132. // Use a convenience function to concatenate the elements of
  133. // the string binding into the proper sequence.
  134. status = RpcStringBindingCompose(NULL,
  135. pszProtocolSequence,
  136. pszNetworkAddress,
  137. pszEndpoint,
  138. pszOptions,
  139. &pszStringBinding);
  140. if (status) {
  141. printf("RpcStringBindingCompose returned %d\n", status);
  142. exit(2);
  143. }
  144. printf("pszStringBinding = %s\n", pszStringBinding);
  145. //
  146. // Set the binding handle that will be used to bind to the server.
  147. //
  148. status = RpcBindingFromStringBinding(pszStringBinding,
  149. &BindingHandle);
  150. if (status) {
  151. printf("RpcBindingFromStringBinding returned %d\n", status);
  152. exit(2);
  153. }
  154. status = RpcStringFree(&pszStringBinding); // remote calls done; unbind
  155. if (status) {
  156. printf("RpcStringFree returned %d\n", status);
  157. exit(2);
  158. }
  159. //
  160. // Tell RPC to do the security thing.
  161. //
  162. printf("Binding auth info set to level %d, service %d, principal %s\n",
  163. AuthnLevel, AuthnService, pszPrincipal );
  164. status = RpcBindingSetAuthInfo(
  165. BindingHandle,
  166. pszPrincipal,
  167. AuthnLevel,
  168. AuthnService,
  169. NULL,
  170. RPC_C_AUTHZ_NAME );
  171. if ( status ) {
  172. printf("RpcBindingSetAuthInfo returned %ld\n", status);
  173. exit(2);
  174. }
  175. //
  176. // Do the actual RPC calls to the server.
  177. //
  178. RpcTryExcept {
  179. for (i = 0; i < LoopCount ; i++ )
  180. {
  181. status = RemoteCall(
  182. BindingHandle,
  183. 0, // no options for now
  184. pszDelegationAddress,
  185. pszProtocolSequence,
  186. pszEndpoint,
  187. pszPrincipal,
  188. pszNetworkAddress,
  189. AuthnLevel,
  190. AuthnService,
  191. RecursionLevel
  192. );
  193. if (status != 0)
  194. {
  195. printf("RemoteCall failed: 0x%x\n",status);
  196. break;
  197. }
  198. }
  199. if (ShutdownService)
  200. {
  201. Shutdown( BindingHandle );
  202. }
  203. } RpcExcept(EXCEPTION_EXECUTE_HANDLER) {
  204. printf("Runtime library reported an exception %d\n",
  205. RpcExceptionCode());
  206. } RpcEndExcept
  207. // The calls to the remote procedures are complete.
  208. // Free the binding handle
  209. status = RpcBindingFree(&BindingHandle); // remote calls done; unbind
  210. if (status) {
  211. printf("RpcBindingFree returned %d\n", status);
  212. exit(2);
  213. }
  214. return 0;
  215. }
  216. // ====================================================================
  217. // MIDL allocate and free
  218. // ====================================================================
  219. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  220. {
  221. return(malloc(len));
  222. }
  223. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  224. {
  225. free(ptr);
  226. }
  227. /* end file helloc.c */