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
6.7 KiB

  1. #include "faxutil.h"
  2. #include <tchar.h>
  3. RPC_STATUS
  4. GetRpcStringBindingInfo (
  5. IN handle_t hBinding,
  6. OUT LPTSTR* pptszNetworkAddress,
  7. OUT LPTSTR* pptszProtSeq
  8. )
  9. /*++
  10. Routine name : GetRpcStringBindingInfo
  11. Routine description:
  12. A utility function to retrieve the machine name and\or protSeq of the RPC client from the
  13. server binding handle.
  14. Arguments:
  15. hBinding - Server binding handle
  16. Return Value:
  17. Returns RPC_S_OK if successfully allocated strings of the client machine name and protSeq.
  18. The caller should free these strings with MemFree().
  19. otherwise RPC_STATUS error code.
  20. --*/
  21. {
  22. RPC_STATUS ec = RPC_S_OK;
  23. LPTSTR lptstrNetworkAddressRetVal = NULL;
  24. LPTSTR lptstrProtSeqRetVal = NULL;
  25. #ifdef UNICODE
  26. unsigned short* tszStringBinding = NULL;
  27. unsigned short* tszNetworkAddress = NULL;
  28. unsigned short* tszProtSeq = NULL;
  29. #else
  30. unsigned char* tszStringBinding = NULL;
  31. unsigned char* tszNetworkAddress = NULL;
  32. unsigned char* tszProtSeq = NULL;
  33. #endif
  34. RPC_BINDING_HANDLE hServer = INVALID_HANDLE_VALUE;
  35. DEBUG_FUNCTION_NAME(TEXT("GetRpcStringBindingInfo"));
  36. Assert (pptszNetworkAddress || pptszProtSeq);
  37. //
  38. // Get server partially-bound handle from client binding handle
  39. //
  40. ec = RpcBindingServerFromClient (hBinding, &hServer);
  41. if (RPC_S_OK != ec)
  42. {
  43. DebugPrintEx(
  44. DEBUG_ERR,
  45. TEXT("RpcBindingServerFromClient failed with %ld"),
  46. ec);
  47. goto exit;
  48. }
  49. //
  50. // Convert binding handle to string represntation
  51. //
  52. ec = RpcBindingToStringBinding (hServer, &tszStringBinding);
  53. if (RPC_S_OK != ec)
  54. {
  55. DebugPrintEx(
  56. DEBUG_ERR,
  57. TEXT("RpcBindingToStringBinding failed with %ld"),
  58. ec);
  59. goto exit;
  60. }
  61. //
  62. // Parse the returned string, looking for the NetworkAddress
  63. //
  64. ec = RpcStringBindingParse (tszStringBinding, NULL, &tszProtSeq, &tszNetworkAddress, NULL, NULL);
  65. if (RPC_S_OK != ec)
  66. {
  67. DebugPrintEx(
  68. DEBUG_ERR,
  69. TEXT("RpcStringBindingParse failed with %ld"),
  70. ec);
  71. goto exit;
  72. }
  73. //
  74. // Now, just copy the results to the return buffer
  75. //
  76. if (pptszNetworkAddress)
  77. {
  78. //
  79. // The user asked for NetworkAddress
  80. //
  81. if (!tszNetworkAddress)
  82. {
  83. //
  84. // Unacceptable client machine name
  85. //
  86. DebugPrintEx(
  87. DEBUG_ERR,
  88. TEXT("Client machine name is invalid"));
  89. ec = ERROR_GEN_FAILURE;
  90. goto exit;
  91. }
  92. lptstrNetworkAddressRetVal = StringDup ((LPCTSTR)tszNetworkAddress);
  93. if (!lptstrNetworkAddressRetVal)
  94. {
  95. ec = GetLastError();
  96. goto exit;
  97. }
  98. }
  99. if (pptszProtSeq)
  100. {
  101. //
  102. // The user asked for NetworkAddress
  103. //
  104. if (!tszProtSeq)
  105. {
  106. //
  107. // Unacceptable client machine name
  108. //
  109. DebugPrintEx(
  110. DEBUG_ERR,
  111. TEXT("Client ProtSeq name is invalid"));
  112. ec = ERROR_GEN_FAILURE;
  113. goto exit;
  114. }
  115. lptstrProtSeqRetVal = StringDup ((LPCTSTR)tszProtSeq);
  116. if (!lptstrProtSeqRetVal)
  117. {
  118. ec = GetLastError();
  119. goto exit;
  120. }
  121. }
  122. if (pptszNetworkAddress)
  123. {
  124. *pptszNetworkAddress = lptstrNetworkAddressRetVal;
  125. }
  126. if (pptszProtSeq)
  127. {
  128. *pptszProtSeq = lptstrProtSeqRetVal;
  129. }
  130. Assert(RPC_S_OK == ec);
  131. exit:
  132. if (INVALID_HANDLE_VALUE != hServer)
  133. {
  134. RpcBindingFree (&hServer);
  135. }
  136. if (tszStringBinding)
  137. {
  138. RpcStringFree (&tszStringBinding);
  139. }
  140. if (tszNetworkAddress)
  141. {
  142. RpcStringFree (&tszNetworkAddress);
  143. }
  144. if (RPC_S_OK != ec)
  145. {
  146. MemFree(lptstrNetworkAddressRetVal);
  147. MemFree(lptstrProtSeqRetVal);
  148. }
  149. return ec;
  150. } // GetRpcStringBindingInfo
  151. RPC_STATUS
  152. IsLocalRPCConnectionNP( PBOOL pbIsLocal)
  153. {
  154. /*++
  155. Routine name : IsLocalRPCConnectionNP
  156. Routine description:
  157. Checks whether the RPC call on named pipe ProtSeq to the calling procedure is local
  158. Author:
  159. Caliv Nir (t-nicali), Oct, 2001
  160. Arguments:
  161. [OUT] pbIsLocal - returns TRUE if the connection is local
  162. Return Value:
  163. RPC_STATUS Error code
  164. RPC_S_OK - The call succeeded.
  165. anything else - The call failed.
  166. --*/
  167. RPC_STATUS rc;
  168. UINT LocalFlag;
  169. DEBUG_FUNCTION_NAME(TEXT("IsLocalRPCConnectionNP"));
  170. Assert(pbIsLocal);
  171. //
  172. // Inquire if local RPC call
  173. //
  174. rc = I_RpcBindingIsClientLocal( 0, // Active RPC call we are servicing
  175. &LocalFlag);
  176. if( RPC_S_OK != rc)
  177. {
  178. DebugPrintEx(DEBUG_ERR,
  179. TEXT("I_RpcBindingIsClientLocal failed. (ec: %ld)"),
  180. rc);
  181. goto Exit;
  182. }
  183. Assert (RPC_S_OK == rc);
  184. if( !LocalFlag )
  185. {
  186. // Not a local connection
  187. *pbIsLocal = FALSE;
  188. }
  189. else
  190. {
  191. *pbIsLocal = TRUE;
  192. }
  193. Exit:
  194. return rc;
  195. } // IsLocalRPCConnectionNP
  196. RPC_STATUS
  197. IsLocalRPCConnectionIpTcp(
  198. handle_t hBinding,
  199. PBOOL pbIsLocal)
  200. {
  201. /*++
  202. Routine name : IsLocalRPCConnectionIpTcp
  203. Routine description:
  204. Checks whether the RPC call to the calling procedure is local.
  205. Works for ncacn_ip_tcp protocol only.
  206. Author:
  207. Oded Sacher (OdedS), April, 2002
  208. Arguments:
  209. [IN] hBinding - Server binding handle
  210. [OUT] pbIsLocal - returns TRUE if the connection is local
  211. Return Value:
  212. Win32 Error code
  213. --*/
  214. RPC_STATUS ec;
  215. LPTSTR lptstrMachineName = NULL;
  216. DEBUG_FUNCTION_NAME(TEXT("IsLocalRPCConnectionIpTcp"));
  217. Assert (pbIsLocal);
  218. ec = GetRpcStringBindingInfo(hBinding,
  219. &lptstrMachineName,
  220. NULL);
  221. if (RPC_S_OK != ec)
  222. {
  223. DebugPrintEx(
  224. DEBUG_ERR,
  225. TEXT("GetClientMachineName failed %ld"),
  226. ec);
  227. return ec;
  228. }
  229. if (0 == _tcscmp(lptstrMachineName, LOCAL_HOST_ADDRESS))
  230. {
  231. *pbIsLocal = TRUE;
  232. }
  233. else
  234. {
  235. *pbIsLocal = FALSE;
  236. }
  237. MemFree(lptstrMachineName);
  238. return ec;
  239. } // IsLocalRPCConnectionIpTcp