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.

294 lines
8.2 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 "sspitest.h" // header file generated by MIDL compiler
  23. int 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, " -a delegation address\n");
  29. fprintf(stderr, " -e endpoint\n");
  30. fprintf(stderr, " -o network options\n");
  31. fprintf(stderr, " -l authn level\n");
  32. fprintf(stderr, " -s authn service\n");
  33. fprintf(stderr, " -r recursiion level\n");
  34. fprintf(stderr, " -u username\n");
  35. fprintf(stderr, " -k password\n");
  36. fprintf(stderr, " -d domain\n");
  37. fprintf(stderr, " -x shutdown server\n");
  38. fprintf(stderr, " -# number of times to call\n");
  39. fprintf(stderr, " -t target principal\n");
  40. exit(1);
  41. }
  42. #ifndef UNLEN
  43. #define UNLEN 256
  44. #endif
  45. int __cdecl
  46. main (argc, argv)
  47. int argc;
  48. PSTR argv[];
  49. {
  50. RPC_STATUS status; // returned by RPC API function
  51. PSTR pszProtocolSequence = "ncacn_ip_tcp";
  52. PSTR pszNetworkAddress = NULL;
  53. PSTR pszEndpoint = "30760";
  54. PSTR pszOptions = NULL;
  55. PSTR pszStringBinding = NULL;
  56. PSTR pszDelegationAddress = NULL;
  57. PSTR pszPrincipal = NULL;
  58. CHAR PrincipalBuffer[UNLEN] = {0};
  59. ULONG PrincipalLength;
  60. ULONG AuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
  61. ULONG AuthnService = RPC_C_AUTHN_WINNT;
  62. ULONG RecursionLevel = 0;
  63. ULONG LoopCount = 1;
  64. BOOLEAN ShutdownService = FALSE;
  65. ULONG i;
  66. handle_t BindingHandle = NULL;
  67. SEC_WINNT_AUTH_IDENTITY_A sID = {0};
  68. sID.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
  69. // allow the user to override settings with command line switches
  70. for (i = 1; i < (ULONG) argc; i++)
  71. {
  72. if ((*argv[i] == '-') || (*argv[i] == '/'))
  73. {
  74. switch (tolower(*(argv[i] + 1))) {
  75. case 'p': // protocol sequence
  76. pszProtocolSequence = argv[++i];
  77. break;
  78. case 'n': // network address
  79. pszNetworkAddress = argv[++i];
  80. break;
  81. case 'a': // network address
  82. pszDelegationAddress = argv[++i];
  83. break;
  84. case 'e':
  85. pszEndpoint = argv[++i];
  86. break;
  87. case 'o':
  88. pszOptions = argv[++i];
  89. break;
  90. case 't':
  91. pszPrincipal = argv[++i];
  92. break;
  93. case 'u':
  94. sID.User = argv[++i];
  95. sID.UserLength = strlen(sID.User);
  96. break;
  97. case 'd':
  98. sID.Domain = argv[++i];
  99. sID.DomainLength = strlen(sID.Domain);
  100. break;
  101. case 'k':
  102. sID.Password = argv[++i];
  103. sID.PasswordLength = strlen(sID.Password);
  104. break;
  105. case 'l':
  106. AuthnLevel = strtol(argv[++i], NULL, 0);
  107. break;
  108. case 's':
  109. AuthnService = strtol(argv[++i], NULL, 0);
  110. break;
  111. case 'r':
  112. RecursionLevel = strtol(argv[++i], NULL, 0);
  113. break;
  114. case '#':
  115. LoopCount = strtol(argv[++i], NULL, 0);
  116. break;
  117. case 'x':
  118. ShutdownService = TRUE;
  119. break;
  120. case 'h':
  121. case '?':
  122. default:
  123. Usage(argv[0]);
  124. }
  125. }
  126. else
  127. {
  128. Usage(argv[0]);
  129. }
  130. }
  131. //
  132. // If the principal is NULL, get it from the environment
  133. //
  134. if (pszPrincipal == NULL)
  135. {
  136. PSTR pszUserRealm;
  137. PSTR pszUserName;
  138. PrincipalBuffer[0] = '\0';
  139. pszUserRealm = getenv( "USERDOMAIN" );
  140. pszUserName = getenv( "USERNAME" );
  141. if (pszUserRealm != NULL)
  142. {
  143. strcpy(PrincipalBuffer, pszUserRealm);
  144. }
  145. if ((pszUserRealm != NULL) &&
  146. (pszUserName != NULL))
  147. {
  148. strcat(PrincipalBuffer, "\\");
  149. }
  150. if (pszUserName != NULL)
  151. {
  152. strcat(PrincipalBuffer, pszUserName);
  153. }
  154. pszPrincipal = PrincipalBuffer;
  155. }
  156. // Use a convenience function to concatenate the elements of
  157. // the string binding into the proper sequence.
  158. status = RpcStringBindingCompose(NULL,
  159. pszProtocolSequence,
  160. pszNetworkAddress,
  161. pszEndpoint,
  162. pszOptions,
  163. &pszStringBinding);
  164. if (status)
  165. {
  166. printf("RpcStringBindingCompose returned %d\n", status);
  167. exit(2);
  168. }
  169. printf("pszStringBinding = %s\n", pszStringBinding);
  170. //
  171. // Set the binding handle that will be used to bind to the server.
  172. //
  173. status = RpcBindingFromStringBinding(pszStringBinding,
  174. &BindingHandle);
  175. if (status) {
  176. printf("RpcBindingFromStringBinding returned %d\n", status);
  177. exit(2);
  178. }
  179. status = RpcStringFree(&pszStringBinding); // remote calls done; unbind
  180. if (status)
  181. {
  182. printf("RpcStringFree returned %d\n", status);
  183. exit(2);
  184. }
  185. //
  186. // Tell RPC to do the security thing.
  187. //
  188. printf("Binding auth info set to level %d, service %d, principal %s\n",
  189. AuthnLevel, AuthnService, pszPrincipal);
  190. status = RpcBindingSetAuthInfo(BindingHandle,
  191. pszPrincipal,
  192. AuthnLevel,
  193. AuthnService,
  194. sID.UserLength || sID.DomainLength || sID.PasswordLength ? &sID : NULL,
  195. RPC_C_AUTHZ_NAME);
  196. if ( status )
  197. {
  198. printf("RpcBindingSetAuthInfo returned %ld\n", status);
  199. exit(2);
  200. }
  201. //
  202. // Do the actual RPC calls to the server.
  203. //
  204. RpcTryExcept
  205. {
  206. for (i = 0; i < LoopCount; i++)
  207. {
  208. status = RemoteCall(
  209. BindingHandle,
  210. 0, // no options for now
  211. pszDelegationAddress,
  212. pszProtocolSequence,
  213. pszEndpoint,
  214. pszPrincipal,
  215. pszNetworkAddress,
  216. AuthnLevel,
  217. AuthnService,
  218. RecursionLevel
  219. );
  220. if (status != 0)
  221. {
  222. printf("RemoteCall failed: 0x%x\n",status);
  223. break;
  224. }
  225. }
  226. if (ShutdownService)
  227. {
  228. Shutdown( BindingHandle );
  229. }
  230. }
  231. RpcExcept(EXCEPTION_EXECUTE_HANDLER)
  232. {
  233. printf("Runtime library reported an exception %d\n", RpcExceptionCode());
  234. } RpcEndExcept
  235. // The calls to the remote procedures are complete.
  236. // Free the binding handle
  237. status = RpcBindingFree(&BindingHandle); // remote calls done; unbind
  238. if (status)
  239. {
  240. printf("RpcBindingFree returned %d\n", status);
  241. exit(2);
  242. }
  243. return 0;
  244. }
  245. // ====================================================================
  246. // MIDL allocate and free
  247. // ====================================================================
  248. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  249. {
  250. return malloc(len);
  251. }
  252. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  253. {
  254. free(ptr);
  255. }
  256. /* end file helloc.c */