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.

235 lines
6.6 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 <secext.h>
  22. #include "hello.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, " -i security package (ntlm, kerberos or negotiate)\n");
  28. fprintf(stderr, " -n network_address\n");
  29. fprintf(stderr, " -e endpoint\n");
  30. fprintf(stderr, " -o options\n");
  31. fprintf(stderr, " -u uuid\n");
  32. exit(1);
  33. }
  34. HANDLE TerminateEvent;
  35. int __cdecl
  36. main (argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40. RPC_STATUS status;
  41. unsigned char * pszUuid = "12345678-1234-1234-1234-123456789ABC";
  42. unsigned char * pszProtocolSequence = "ncacn_np";
  43. unsigned char * pszSecPackage = "negotiate";
  44. unsigned char * pszNetworkAddress = NULL;
  45. unsigned char * pszEndpoint = "\\pipe\\hello";
  46. unsigned char * pszOptions = NULL;
  47. unsigned char * pszStringBinding = NULL;
  48. int i;
  49. unsigned long SecPackageId = RPC_C_AUTHN_WINNT;
  50. DWORD WaitStatus;
  51. // allow the user to override settings with command line switches
  52. for (i = 1; i < argc; i++) {
  53. if ((*argv[i] == '-') || (*argv[i] == '/')) {
  54. switch (tolower(*(argv[i]+1))) {
  55. case 'p': // protocol sequence
  56. pszProtocolSequence = argv[++i];
  57. break;
  58. case 'i': // security package
  59. pszSecPackage = argv[++i];
  60. break;
  61. case 'n': // network address
  62. pszNetworkAddress = argv[++i];
  63. break;
  64. case 'e':
  65. pszEndpoint = argv[++i];
  66. break;
  67. case 'o':
  68. pszOptions = argv[++i];
  69. break;
  70. case 'u':
  71. pszUuid = argv[++i];
  72. break;
  73. case 'h':
  74. case '?':
  75. default:
  76. Usage(argv[0]);
  77. }
  78. }
  79. else
  80. Usage(argv[0]);
  81. }
  82. //
  83. // Create an event to wait on
  84. //
  85. TerminateEvent = CreateEvent( NULL, // No security attributes
  86. TRUE, // Must be manually reset
  87. FALSE, // Initially not signaled
  88. NULL ); // No name
  89. if ( TerminateEvent == NULL ) {
  90. printf( "Couldn't CreateEvent %ld\n", GetLastError() );
  91. return 2;
  92. }
  93. status = RpcServerUseProtseqEp(pszProtocolSequence,
  94. 1, // maximum concurrent calls
  95. pszEndpoint,
  96. 0);
  97. if (status) {
  98. printf("RpcServerUseProtseqEp returned 0x%x\n", status);
  99. exit(2);
  100. }
  101. status = RpcServerRegisterIf(hello_v1_0_s_ifspec, 0, 0);
  102. if (status) {
  103. printf("RpcServerRegisterIf returned 0x%x\n", status);
  104. exit(2);
  105. }
  106. // default package is ntlm
  107. if ( lstrcmpi(pszSecPackage, "ntlm") == 0)
  108. {
  109. SecPackageId = RPC_C_AUTHN_WINNT;
  110. }
  111. else if ( lstrcmpi(pszSecPackage, "kerberos") == 0)
  112. {
  113. SecPackageId = RPC_C_AUTHN_GSS_KERBEROS;
  114. }
  115. else if ( lstrcmpi(pszSecPackage, "negotiate") == 0)
  116. {
  117. SecPackageId = RPC_C_AUTHN_GSS_NEGOTIATE;
  118. }
  119. status = RpcServerRegisterAuthInfo( "HelloS", SecPackageId, NULL, NULL );
  120. if (status) {
  121. printf("RpcServerRegisterAuthInfo returned 0x%x\n", status);
  122. exit(2);
  123. }
  124. printf("Calling RpcServerListen\n");
  125. status = RpcServerListen(1,12345,1);
  126. if (status) {
  127. printf("RpcServerListen returned: 0x%x\n", status);
  128. exit(2);
  129. }
  130. WaitStatus = WaitForSingleObject( TerminateEvent, INFINITE );
  131. if ( WaitStatus != WAIT_OBJECT_0 ) {
  132. printf( "Couldn't WaitForSingleObject %ld %ld\n", WaitStatus, GetLastError() );
  133. return 2;
  134. }
  135. return 0;
  136. } /* end main() */
  137. // ====================================================================
  138. // MIDL allocate and free
  139. // ====================================================================
  140. void __RPC_FAR * __RPC_API MIDL_user_allocate(size_t len)
  141. {
  142. return(malloc(len));
  143. }
  144. void __RPC_API MIDL_user_free(void __RPC_FAR * ptr)
  145. {
  146. free(ptr);
  147. }
  148. /*
  149. PURPOSE: Remote procedures that are linked with the server
  150. side of RPC distributed application
  151. FUNCTIONS: HelloProc() - prints "hello, world" or other string
  152. sent by client to server
  153. COMMENTS:
  154. This version of the distributed application that prints
  155. "hello, world" (or other string) on the server features a client
  156. that manages its connection to the server. It uses the binding
  157. handle hello_IfHandle, defined in the file hello.h.
  158. ****************************************************************************/
  159. void HelloProc(unsigned char * pszString)
  160. {
  161. RPC_STATUS RpcStatus;
  162. char szUser[256];
  163. DWORD iSize;
  164. RpcStatus = RpcImpersonateClient( NULL );
  165. if ( RpcStatus != RPC_S_OK ) {
  166. printf( "RpcImpersonateClient Failed %ld\n", RpcStatus );
  167. };
  168. // GetUserNameEx(NameUserPrincipal, szUser, sizeof(szUser)-1);
  169. iSize = sizeof(szUser)-1;
  170. GetUserName(szUser, &iSize);
  171. printf("User '%s' says '%s'\n", szUser, pszString);
  172. RpcStatus = RpcRevertToSelf();
  173. if ( RpcStatus != RPC_S_OK ) {
  174. printf( "RpcRevertToSelf Failed %ld\n", RpcStatus );
  175. }
  176. }
  177. void Shutdown(void)
  178. {
  179. RPC_STATUS status;
  180. printf("Calling RpcMgmtStopServerListening\n");
  181. status = RpcMgmtStopServerListening(NULL);
  182. if (status) {
  183. printf("RpcMgmtStopServerListening returned: 0x%x\n", status);
  184. exit(2);
  185. }
  186. printf("Calling RpcServerUnregisterIf\n");
  187. status = RpcServerUnregisterIf(NULL, NULL, FALSE);
  188. if (status) {
  189. printf("RpcServerUnregisterIf returned 0x%x\n", status);
  190. exit(2);
  191. }
  192. if ( !SetEvent( TerminateEvent) ) {
  193. printf( "Couldn't SetEvent %ld\n", GetLastError() );
  194. }
  195. }
  196. /* end hellos.c */