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.

356 lines
9.4 KiB

  1. /****************************************************************************
  2. Microsoft RPC Version 1.0
  3. Copyright Microsoft Corp. 1992
  4. Hello Example
  5. FILE: hellos.c
  6. USAGE: hellos
  7. PURPOSE: Server side of RPC distributed application hello
  8. FUNCTIONS: main() - registers server as RPC server
  9. COMMENTS:
  10. This distributed application prints "hello, world" on the server.
  11. This version features a client that manages its connection to
  12. the server. It uses the binding handle hello_IfHandle that is defined
  13. in the generated header file hello.h.
  14. ****************************************************************************/
  15. #include <stdlib.h>
  16. #include <windows.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <rpc.h> // RPC data structures and APIs
  21. #include "kerbtest.h" // header file generated by MIDL compiler
  22. void Usage(char * pszProgramName)
  23. {
  24. fprintf(stderr, "Usage: %s\n", pszProgramName);
  25. fprintf(stderr, " -p protocol_sequence\n");
  26. fprintf(stderr, " -e endpoint\n");
  27. fprintf(stderr, " -o options\n");
  28. fprintf(stderr, " -s authn service\n");
  29. exit(1);
  30. }
  31. HANDLE TerminateEvent;
  32. ULONG AuthnService = RPC_C_AUTHN_GSS_KERBEROS;
  33. int __cdecl
  34. main (argc, argv)
  35. int argc;
  36. char *argv[];
  37. {
  38. RPC_STATUS status;
  39. unsigned char * pszProtocolSequence = "ncacn_ip_tcp";
  40. unsigned char * pszEndpoint = "30760";
  41. unsigned char * pszOptions = NULL;
  42. unsigned char * pszStringBinding = NULL;
  43. unsigned char * PrincipalName = NULL;
  44. int i;
  45. DWORD WaitStatus;
  46. // allow the user to override settings with command line switches
  47. for (i = 1; i < argc; i++) {
  48. if ((*argv[i] == '-') || (*argv[i] == '/')) {
  49. switch (tolower(*(argv[i]+1))) {
  50. case 'p': // protocol sequence
  51. pszProtocolSequence = argv[++i];
  52. break;
  53. case 'e':
  54. pszEndpoint = argv[++i];
  55. break;
  56. case 'o':
  57. pszOptions = argv[++i];
  58. break;
  59. case 's':
  60. sscanf(argv[++i],"%d",&AuthnService);
  61. break;
  62. case 'h':
  63. case '?':
  64. default:
  65. Usage(argv[0]);
  66. }
  67. }
  68. else
  69. Usage(argv[0]);
  70. }
  71. //
  72. // Create an event to wait on
  73. //
  74. TerminateEvent = CreateEvent( NULL, // No security attributes
  75. TRUE, // Must be manually reset
  76. FALSE, // Initially not signaled
  77. NULL ); // No name
  78. if ( TerminateEvent == NULL ) {
  79. printf( "Couldn't CreateEvent %ld\n", GetLastError() );
  80. return 2;
  81. }
  82. printf("Server using protseq %s endpoint %s\n",pszProtocolSequence, pszEndpoint );
  83. status = RpcServerUseProtseqEp(pszProtocolSequence,
  84. 3, // maximum concurrent calls
  85. pszEndpoint,
  86. 0);
  87. if (status) {
  88. printf("RpcServerUseProtseqEp returned 0x%x\n", status);
  89. exit(2);
  90. }
  91. status = RpcServerRegisterIf(srv_kerbtest_ServerIfHandle, 0, 0);
  92. if (status) {
  93. printf("RpcServerRegisterIf returned 0x%x\n", status);
  94. exit(2);
  95. }
  96. status = RpcServerInqDefaultPrincName(
  97. AuthnService,
  98. &PrincipalName
  99. );
  100. if (status)
  101. {
  102. printf("RpcServerInqDefaultPrincName returned %d\n",status);
  103. exit(2);
  104. }
  105. status = RpcServerRegisterAuthInfo(
  106. PrincipalName,
  107. AuthnService,
  108. NULL,
  109. NULL
  110. );
  111. if (status) {
  112. printf("RpcServerRegisterAuthInfo returned 0x%x\n", status);
  113. exit(2);
  114. }
  115. printf("Calling RpcServerListen\n");
  116. status = RpcServerListen(1,12345,1);
  117. if (status) {
  118. printf("RpcServerListen returned: 0x%x\n", status);
  119. exit(2);
  120. }
  121. WaitStatus = WaitForSingleObject( TerminateEvent, INFINITE );
  122. if ( WaitStatus != WAIT_OBJECT_0 ) {
  123. printf( "Couldn't WaitForSingleObject %ld %ld\n", WaitStatus, GetLastError() );
  124. return 2;
  125. }
  126. return 0;
  127. } /* end main() */
  128. // ====================================================================
  129. // MIDL allocate and free
  130. // ====================================================================
  131. void __RPC_FAR * __RPC_API
  132. MIDL_user_allocate(size_t len)
  133. {
  134. return(malloc(len));
  135. }
  136. void __RPC_API
  137. MIDL_user_free(void __RPC_FAR * ptr)
  138. {
  139. free(ptr);
  140. }
  141. ULONG
  142. RecurseRemoteCall(
  143. ULONG Options,
  144. LPSTR RemoteAddress,
  145. LPSTR RemoteProtocol,
  146. LPSTR RemoteEndpoint,
  147. LPSTR Principal,
  148. LPSTR Address,
  149. ULONG AuthnLevel,
  150. ULONG AuthnSvc,
  151. ULONG RecursionLevel
  152. )
  153. {
  154. unsigned char * pszStringBinding;
  155. RPC_STATUS status;
  156. handle_t BindingHandle;
  157. // Use a convenience function to concatenate the elements of
  158. // the string binding into the proper sequence.
  159. status = RpcStringBindingCompose(NULL,
  160. RemoteProtocol,
  161. RemoteAddress,
  162. RemoteEndpoint,
  163. NULL,
  164. &pszStringBinding);
  165. if (status) {
  166. printf("RpcStringBindingCompose returned %d\n", status);
  167. return(status);
  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. RpcStringFree(&pszStringBinding);
  176. if (status) {
  177. printf("RpcBindingFromStringBinding returned %d\n", status);
  178. return(status);
  179. }
  180. //
  181. // Tell RPC to do the security thing.
  182. //
  183. printf("Binding auth info set to level %d, service %d, principal %s\n",
  184. AuthnLevel, AuthnService, Principal );
  185. status = RpcBindingSetAuthInfo(
  186. BindingHandle,
  187. Principal,
  188. AuthnLevel,
  189. AuthnService,
  190. NULL,
  191. RPC_C_AUTHZ_NAME );
  192. if ( status ) {
  193. printf("RpcBindingSetAuthInfo returned %ld\n", status);
  194. return( status );
  195. }
  196. //
  197. // Do the actual RPC calls to the server.
  198. //
  199. RpcTryExcept {
  200. status = RemoteCall(
  201. BindingHandle,
  202. Options,
  203. Address,
  204. RemoteProtocol,
  205. RemoteEndpoint,
  206. Principal,
  207. RemoteAddress,
  208. AuthnLevel,
  209. AuthnService,
  210. RecursionLevel
  211. );
  212. if (status != 0)
  213. {
  214. printf("RemoteCall failed: 0x%x\n",status);
  215. }
  216. } RpcExcept(EXCEPTION_EXECUTE_HANDLER) {
  217. printf("Runtime library reported an exception %d\n",
  218. RpcExceptionCode());
  219. } RpcEndExcept
  220. // The calls to the remote procedures are complete.
  221. // Free the binding handle
  222. status = RpcBindingFree(&BindingHandle); // remote calls done; unbind
  223. if (status) {
  224. printf("RpcBindingFree returned %d\n", status);
  225. exit(2);
  226. }
  227. }
  228. ULONG
  229. srv_RemoteCall(
  230. handle_t BindingHandle,
  231. ULONG Options,
  232. LPSTR RemoteAddress,
  233. LPSTR RemoteProtocol,
  234. LPSTR RemoteEndpoint,
  235. LPSTR Principal,
  236. LPSTR Address,
  237. ULONG AuthnLevel,
  238. ULONG AuthnSvc,
  239. ULONG RecursionLevel
  240. )
  241. {
  242. RPC_STATUS RpcStatus;
  243. CHAR ClientName[100];
  244. ULONG NameLen = sizeof(ClientName);
  245. RpcStatus = RpcImpersonateClient( NULL );
  246. if ( RpcStatus != RPC_S_OK ) {
  247. printf( "RpcImpersonateClient Failed %ld\n", RpcStatus );
  248. goto Cleanup;
  249. }
  250. GetUserName(ClientName,&NameLen);
  251. printf("Recursion %d: Client called: name = %s\n",RecursionLevel, ClientName);
  252. if (RecursionLevel != 0)
  253. {
  254. RpcStatus = RecurseRemoteCall(
  255. Options,
  256. RemoteAddress,
  257. RemoteProtocol,
  258. RemoteEndpoint,
  259. Principal,
  260. Address,
  261. AuthnLevel,
  262. AuthnSvc,
  263. RecursionLevel - 1
  264. );
  265. }
  266. RpcRevertToSelf();
  267. Cleanup:
  268. return(RpcStatus);
  269. }
  270. void
  271. srv_Shutdown(
  272. handle_t BindingHandle
  273. )
  274. {
  275. RPC_STATUS status;
  276. status = RpcMgmtStopServerListening(NULL);
  277. if (status) {
  278. printf("RpcMgmtStopServerListening returned: 0x%x\n", status);
  279. exit(2);
  280. }
  281. status = RpcServerUnregisterIf(NULL, NULL, FALSE);
  282. if (status) {
  283. printf("RpcServerUnregisterIf returned 0x%x\n", status);
  284. exit(2);
  285. }
  286. if ( !SetEvent( TerminateEvent) ) {
  287. printf( "Couldn't SetEvent %ld\n", GetLastError() );
  288. }
  289. }