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.

385 lines
8.8 KiB

  1. /*++
  2. Copyright (c) 1990,91 Microsoft Corporation
  3. Module Name:
  4. RpcServ.c
  5. Abstract:
  6. This file contains commonly used server-side RPC functions,
  7. such as starting and stoping RPC servers.
  8. Author:
  9. Dan Lafferty danl 09-May-1991
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. 09-May-1991 Danl
  14. Created
  15. 03-July-1991 JimK
  16. Copied from a net-specific file.
  17. 18-Feb-1992 Danl
  18. Added support for multiple endpoints & interfaces per server.
  19. 10-Nov-1993 Danl
  20. Wait for RPC calls to complete before returning from
  21. RpcServerUnregisterIf. Also, do a WaitServerListen after
  22. calling StopServerListen (when the last server shuts down).
  23. Now this is similar to RpcServer functions in netlib.
  24. 29-Jun-1995 RichardW
  25. Read an alternative ACL from a key in the registry, if one exists.
  26. This ACL is used to protect the named pipe.
  27. --*/
  28. //
  29. // INCLUDES
  30. //
  31. // These must be included first:
  32. #include <nt.h> // DbgPrint
  33. #include <ntrtl.h> // DbgPrint
  34. #include <windef.h> // win32 typedefs
  35. #include <rpc.h> // rpc prototypes
  36. #include <ntrpcp.h>
  37. #include <nturtl.h> // needed for winbase.h
  38. #include <winbase.h> // LocalAlloc
  39. // These may be included in any order:
  40. #include <tstr.h> // WCSSIZE
  41. #define NT_PIPE_PREFIX_W L"\\PIPE\\"
  42. //
  43. // GLOBALS
  44. //
  45. static CRITICAL_SECTION RpcpCriticalSection;
  46. static DWORD RpcpNumInstances;
  47. NTSTATUS
  48. RpcpInitRpcServer(
  49. VOID
  50. )
  51. /*++
  52. Routine Description:
  53. This function initializes the critical section used to protect the
  54. global server handle and instance count.
  55. Arguments:
  56. none
  57. Return Value:
  58. none
  59. --*/
  60. {
  61. NTSTATUS Status;
  62. RpcpNumInstances = 0;
  63. Status = RtlInitializeCriticalSection(&RpcpCriticalSection);
  64. return Status;
  65. }
  66. NTSTATUS
  67. RpcpAddInterface(
  68. IN LPWSTR InterfaceName,
  69. IN RPC_IF_HANDLE InterfaceSpecification
  70. )
  71. /*++
  72. Routine Description:
  73. Starts an RPC Server, adds the address (or port/pipe), and adds the
  74. interface (dispatch table).
  75. Arguments:
  76. InterfaceName - points to the name of the interface.
  77. InterfaceSpecification - Supplies the interface handle for the
  78. interface which we wish to add.
  79. Return Value:
  80. NT_SUCCESS - Indicates the server was successfully started.
  81. STATUS_NO_MEMORY - An attempt to allocate memory has failed.
  82. Other - Status values that may be returned by:
  83. RpcServerRegisterIf()
  84. RpcServerUseProtseqEp()
  85. , or any RPC error codes, or any windows error codes that
  86. can be returned by LocalAlloc.
  87. --*/
  88. {
  89. RPC_STATUS RpcStatus;
  90. LPWSTR Endpoint = NULL;
  91. BOOL Bool;
  92. NTSTATUS Status;
  93. // We need to concatenate \pipe\ to the front of the interface name.
  94. Endpoint = (LPWSTR)LocalAlloc(LMEM_FIXED, sizeof(NT_PIPE_PREFIX_W) + WCSSIZE(InterfaceName));
  95. if (Endpoint == 0) {
  96. return(STATUS_NO_MEMORY);
  97. }
  98. wcscpy(Endpoint, NT_PIPE_PREFIX_W );
  99. wcscat(Endpoint,InterfaceName);
  100. //
  101. // Ignore the second argument for now.
  102. // Use default security descriptor
  103. //
  104. RpcStatus = RpcServerUseProtseqEpW(L"ncacn_np", 10, Endpoint, NULL);
  105. // If RpcpStartRpcServer and then RpcpStopRpcServer have already
  106. // been called, the endpoint will have already been added but not
  107. // removed (because there is no way to do it). If the endpoint is
  108. // already there, it is ok.
  109. if ( (RpcStatus != RPC_S_OK)
  110. && (RpcStatus != RPC_S_DUPLICATE_ENDPOINT)) {
  111. #if DBG
  112. DbgPrint("RpcServerUseProtseqW failed! rpcstatus = %u\n",RpcStatus);
  113. #endif
  114. goto CleanExit;
  115. }
  116. RpcStatus = RpcServerRegisterIf(InterfaceSpecification, 0, 0);
  117. CleanExit:
  118. if ( Endpoint != NULL ) {
  119. LocalFree(Endpoint);
  120. }
  121. return( I_RpcMapWin32Status(RpcStatus) );
  122. }
  123. NTSTATUS
  124. RpcpStartRpcServer(
  125. IN LPWSTR InterfaceName,
  126. IN RPC_IF_HANDLE InterfaceSpecification
  127. )
  128. /*++
  129. Routine Description:
  130. Starts an RPC Server, adds the address (or port/pipe), and adds the
  131. interface (dispatch table).
  132. Arguments:
  133. InterfaceName - points to the name of the interface.
  134. InterfaceSpecification - Supplies the interface handle for the
  135. interface which we wish to add.
  136. Return Value:
  137. NT_SUCCESS - Indicates the server was successfully started.
  138. STATUS_NO_MEMORY - An attempt to allocate memory has failed.
  139. Other - Status values that may be returned by:
  140. RpcServerRegisterIf()
  141. RpcServerUseProtseqEp()
  142. , or any RPC error codes, or any windows error codes that
  143. can be returned by LocalAlloc.
  144. --*/
  145. {
  146. RPC_STATUS RpcStatus;
  147. EnterCriticalSection(&RpcpCriticalSection);
  148. RpcStatus = RpcpAddInterface( InterfaceName,
  149. InterfaceSpecification );
  150. if ( RpcStatus != RPC_S_OK ) {
  151. LeaveCriticalSection(&RpcpCriticalSection);
  152. return( I_RpcMapWin32Status(RpcStatus) );
  153. }
  154. RpcpNumInstances++;
  155. if (RpcpNumInstances == 1) {
  156. // The first argument specifies the minimum number of threads to
  157. // be created to handle calls; the second argument specifies the
  158. // maximum number of concurrent calls allowed. The last argument
  159. // indicates not to wait.
  160. RpcStatus = RpcServerListen(1,12345, 1);
  161. if ( RpcStatus == RPC_S_ALREADY_LISTENING ) {
  162. RpcStatus = RPC_S_OK;
  163. }
  164. }
  165. LeaveCriticalSection(&RpcpCriticalSection);
  166. return( I_RpcMapWin32Status(RpcStatus) );
  167. }
  168. NTSTATUS
  169. RpcpDeleteInterface(
  170. IN RPC_IF_HANDLE InterfaceSpecification
  171. )
  172. /*++
  173. Routine Description:
  174. Deletes the interface. This is likely
  175. to be caused by an invalid handle. If an attempt to add the same
  176. interface or address again, then an error will be generated at that
  177. time.
  178. Arguments:
  179. InterfaceSpecification - A handle for the interface that is to be removed.
  180. Return Value:
  181. NERR_Success, or any RPC error codes that can be returned from
  182. RpcServerUnregisterIf.
  183. --*/
  184. {
  185. RPC_STATUS RpcStatus;
  186. RpcStatus = RpcServerUnregisterIf(InterfaceSpecification, 0, 1);
  187. return( I_RpcMapWin32Status(RpcStatus) );
  188. }
  189. NTSTATUS
  190. RpcpStopRpcServer(
  191. IN RPC_IF_HANDLE InterfaceSpecification
  192. )
  193. /*++
  194. Routine Description:
  195. Deletes the interface. This is likely
  196. to be caused by an invalid handle. If an attempt to add the same
  197. interface or address again, then an error will be generated at that
  198. time.
  199. Arguments:
  200. InterfaceSpecification - A handle for the interface that is to be removed.
  201. Return Value:
  202. NERR_Success, or any RPC error codes that can be returned from
  203. RpcServerUnregisterIf.
  204. --*/
  205. {
  206. RPC_STATUS RpcStatus;
  207. RpcStatus = RpcServerUnregisterIf(InterfaceSpecification, 0, 1);
  208. EnterCriticalSection(&RpcpCriticalSection);
  209. RpcpNumInstances--;
  210. if (RpcpNumInstances == 0)
  211. {
  212. //
  213. // Return value needs to be from RpcServerUnregisterIf() to maintain
  214. // semantics, so the return values from these are ignored.
  215. //
  216. (VOID)RpcMgmtStopServerListening(0);
  217. (VOID)RpcMgmtWaitServerListen();
  218. }
  219. LeaveCriticalSection(&RpcpCriticalSection);
  220. return (I_RpcMapWin32Status(RpcStatus));
  221. }
  222. NTSTATUS
  223. RpcpStopRpcServerEx(
  224. IN RPC_IF_HANDLE InterfaceSpecification
  225. )
  226. /*++
  227. Routine Description:
  228. Deletes the interface and close all context handles associated with it.
  229. This can only be called on interfaces that use strict context handles
  230. (RPC will assert and return an error otherwise).
  231. Arguments:
  232. InterfaceSpecification - A handle for the interface that is to be removed.
  233. Return Value:
  234. NERR_Success, or any RPC error codes that can be returned from
  235. RpcServerUnregisterIfEx.
  236. --*/
  237. {
  238. RPC_STATUS RpcStatus;
  239. RpcStatus = RpcServerUnregisterIfEx(InterfaceSpecification, 0, 1);
  240. EnterCriticalSection(&RpcpCriticalSection);
  241. RpcpNumInstances--;
  242. if (RpcpNumInstances == 0)
  243. {
  244. //
  245. // Return value needs to be from RpcServerUnregisterIfEx() to
  246. // maintain semantics, so the return values from these are ignored.
  247. //
  248. (VOID)RpcMgmtStopServerListening(0);
  249. (VOID)RpcMgmtWaitServerListen();
  250. }
  251. LeaveCriticalSection(&RpcpCriticalSection);
  252. return (I_RpcMapWin32Status(RpcStatus));
  253. }