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.

243 lines
7.3 KiB

  1. /****************************************************************************
  2. Microsoft RPC Version 1`1
  3. Copyright Microsoft Corp. 1992
  4. Hello Example
  5. FILE: helloc.c
  6. USAGE: client -n network_address
  7. -p protocol_sequence
  8. -e endpoint
  9. -o options
  10. -u uuid
  11. PURPOSE: Client side of RPC distributed application
  12. FUNCTIONS: main() - binds to server and calls remote procedure
  13. COMMENTS:
  14. This distributed application prints a string such as "hello, world"
  15. on the server. The client manages its connection to the server.
  16. The client uses the binding handle hello_IfHandle defined in the
  17. file hello.h.
  18. ****************************************************************************/
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <rpc.h> // RPC API functions, types
  23. #include "hello.h" // header file generated by MIDL compiler
  24. void Usage(char * pszProgramName)
  25. {
  26. fprintf(stderr, "Usage: %s\n", pszProgramName);
  27. fprintf(stderr, " -i security package name (ntlm, kerberos or negotiate)\n");
  28. fprintf(stderr, " -c target principal name (necessary for kerberos or negotiate). For e.g, domainname\\username\n");
  29. fprintf(stderr, " -p protocol_sequence\n");
  30. fprintf(stderr, " -n network_address\n");
  31. fprintf(stderr, " -e endpoint\n");
  32. fprintf(stderr, " -o options\n");
  33. fprintf(stderr, " -u uuid\n");
  34. fprintf(stderr, " -s string\n");
  35. fprintf(stderr, " -w password\n");
  36. fprintf(stderr, " -a user account name\n");
  37. fprintf(stderr, " -d user account domain\n");
  38. exit(1);
  39. }
  40. int __cdecl
  41. main (argc, argv)
  42. int argc;
  43. char *argv[];
  44. {
  45. RPC_STATUS status; // returned by RPC API function
  46. unsigned char * pszUuid = "12345678-1234-1234-1234-123456789ABC";
  47. unsigned char * pszProtocolSequence = "ncacn_np";
  48. unsigned char * pszSecPackage = "negotiate";
  49. unsigned char * pszPrincipalName = NULL;
  50. unsigned char * pszNetworkAddress = NULL;
  51. unsigned char * pszEndpoint = "\\pipe\\hello";
  52. unsigned char * pszOptions = NULL;
  53. unsigned char * pszStringBinding = NULL;
  54. unsigned char * pszString = "hello, world";
  55. unsigned char * pszUserName = NULL;
  56. unsigned char * pszPassword;
  57. unsigned char * pszDomain;
  58. SEC_WINNT_AUTH_IDENTITY sID;
  59. unsigned long SecPackageId = RPC_C_AUTHN_WINNT;
  60. int i;
  61. sID.User = NULL;
  62. sID.UserLength = 0;
  63. sID.Domain = NULL;
  64. sID.DomainLength = 0;
  65. sID.Password = NULL;
  66. sID.PasswordLength = 0;
  67. sID.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
  68. // allow the user to override settings with command line switches
  69. for (i = 1; i < argc; i++) {
  70. if ((*argv[i] == '-') || (*argv[i] == '/')) {
  71. switch (tolower(*(argv[i]+1))) {
  72. case 'i': // secpackage
  73. pszSecPackage = argv[++i];
  74. break;
  75. case 'p': // protocol sequence
  76. pszProtocolSequence = argv[++i];
  77. break;
  78. case 'c': // principal name
  79. pszPrincipalName = argv[++i];
  80. break;
  81. case 'n': // network address
  82. pszNetworkAddress = argv[++i];
  83. break;
  84. case 'e':
  85. pszEndpoint = argv[++i];
  86. break;
  87. case 'o':
  88. pszOptions = argv[++i];
  89. break;
  90. case 'u':
  91. pszUuid = argv[++i];
  92. break;
  93. case 's':
  94. pszString = argv[++i];
  95. break;
  96. case 'a':
  97. sID.User = argv[++i];
  98. sID.UserLength = strlen(sID.User);
  99. break;
  100. case 'w':
  101. sID.Password = argv[++i];
  102. sID.PasswordLength = strlen(sID.Password);
  103. break;
  104. case 'd':
  105. sID.Domain = argv[++i];
  106. sID.DomainLength = strlen(sID.Domain);
  107. break;
  108. case 'h':
  109. case '?':
  110. default:
  111. Usage(argv[0]);
  112. }
  113. } else {
  114. Usage(argv[0]);
  115. }
  116. }
  117. // Use a convenience function to concatenate the elements of
  118. // the string binding into the proper sequence.
  119. status = RpcStringBindingCompose(pszUuid,
  120. pszProtocolSequence,
  121. pszNetworkAddress,
  122. pszEndpoint,
  123. pszOptions,
  124. &pszStringBinding);
  125. if (status) {
  126. printf("RpcStringBindingCompose returned 0x%x\n", status);
  127. exit(2);
  128. }
  129. printf("pszStringBinding = %s\n", pszStringBinding);
  130. //
  131. // Set the binding handle that will be used to bind to the server.
  132. //
  133. status = RpcBindingFromStringBinding(pszStringBinding,
  134. &hello_IfHandle);
  135. if (status) {
  136. printf("RpcBindingFromStringBinding returned 0x%x\n", status);
  137. exit(2);
  138. }
  139. // default package is ntlm
  140. if ( lstrcmpi(pszSecPackage, "ntlm") == 0)
  141. {
  142. SecPackageId = RPC_C_AUTHN_WINNT;
  143. }
  144. else if ( lstrcmpi(pszSecPackage, "kerberos") == 0)
  145. {
  146. SecPackageId = RPC_C_AUTHN_GSS_KERBEROS;
  147. }
  148. else if ( lstrcmpi(pszSecPackage, "negotiate") == 0)
  149. {
  150. SecPackageId = RPC_C_AUTHN_GSS_NEGOTIATE;
  151. }
  152. //
  153. // Tell RPC to do the security thing.
  154. //
  155. status = RpcBindingSetAuthInfo(
  156. hello_IfHandle,
  157. pszPrincipalName,
  158. RPC_C_AUTHN_LEVEL_CONNECT,
  159. SecPackageId,
  160. &sID,
  161. RPC_C_AUTHZ_NAME );
  162. if ( status ) {
  163. printf("RpcBindingSetAuthInfo returned %ld\n", status);
  164. exit(2);
  165. }
  166. //
  167. // Do the actual RPC calls to the server.
  168. //
  169. printf(" print the string '%s' on the server\n", pszString);
  170. RpcTryExcept {
  171. int i;
  172. for ( i=0; i<100 ; i++ ) {
  173. HelloProc(pszString); // make call with user message
  174. }
  175. Shutdown(); // shut down the server side
  176. } RpcExcept(1) {
  177. printf("Runtime library reported an exception 0x%lx\n",
  178. RpcExceptionCode());
  179. } RpcEndExcept
  180. // The calls to the remote procedures are complete.
  181. // Free the string and the binding handle
  182. status = RpcStringFree(&pszStringBinding); // remote calls done; unbind
  183. if (status) {
  184. printf("RpcStringFree returned 0x%x\n", status);
  185. exit(2);
  186. }
  187. status = RpcBindingFree(&hello_IfHandle); // remote calls done; unbind
  188. if (status) {
  189. printf("RpcBindingFree returned 0x%x\n", status);
  190. exit(2);
  191. }
  192. return 0;
  193. }
  194. // ====================================================================
  195. // MIDL allocate and free
  196. // ====================================================================
  197. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  198. {
  199. return(malloc(len));
  200. }
  201. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  202. {
  203. free(ptr);
  204. }
  205. /* end file helloc.c */