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.

305 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. dhcbind.c
  5. Abstract:
  6. Routines which use RPC to bind and unbind the client to the
  7. DHCP server service.
  8. Author:
  9. Madan Appiah (madana) 10-Sep-1993
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. #include "dhcpcli.h"
  15. static WCHAR LocalMachineName[MAX_COMPUTERNAME_LENGTH + 1] = L"";
  16. //
  17. // Bind should timeout in a max of 15 sec
  18. //
  19. const int DHCP_RPC_BIND_TIMEOUT_VALUE = ( 1000 * 15 );
  20. BOOL fShortTimeOut = FALSE;
  21. DWORD
  22. FindProtocolToUse(
  23. LPWSTR ServerIpAddress
  24. )
  25. /*++
  26. Routine Description:
  27. This function returns the protocol binding to be used. It examines
  28. the ServerIpAddress string, if it is :
  29. 1. NULL or local IPAddress or Local Name - use "ncalrpc"
  30. 2. IpAddress - (of the form "ppp.qqq.rrr.sss") - use "ncacn_ip_tcp"
  31. 3. otherwise use "ncacn_np" protocol.
  32. Arguments:
  33. ServerIpAddress - The IP address of the server to bind to.
  34. Return Value:
  35. One of the following values :
  36. DHCP_SERVER_USE_RPC_OVER_TCPIP 0x1
  37. DHCP_SERVER_USE_RPC_OVER_NP 0x2
  38. DHCP_SERVER_USE_RPC_OVER_LPC 0x4
  39. --*/
  40. {
  41. DWORD DotCount = 0;
  42. LPWSTR String = ServerIpAddress;
  43. DWORD ComputerNameLength;
  44. if( (ServerIpAddress == NULL) ||
  45. (*ServerIpAddress == L'\0') ) {
  46. return( DHCP_SERVER_USE_RPC_OVER_LPC );
  47. }
  48. while ( (String = wcschr( String, L'.' )) != NULL ) {
  49. //
  50. // found another DOT.
  51. //
  52. DotCount++;
  53. String++; // skip this dot.
  54. }
  55. //
  56. // if the string has 3 DOTs exactly then this string must represent
  57. // an IpAddress.
  58. //
  59. if( DotCount == 3) {
  60. //
  61. // if this is local IP Address, use LPC
  62. //
  63. if( _wcsicmp(L"127.0.0.1" , ServerIpAddress) == 0 ) {
  64. return( DHCP_SERVER_USE_RPC_OVER_LPC );
  65. }
  66. //
  67. // ?? determine whether this address is local IPAddress.
  68. //
  69. return(DHCP_SERVER_USE_RPC_OVER_TCPIP);
  70. }
  71. //
  72. // It is a computer name string. Check to see this is local
  73. // computer name. If so use LPC, otherwise use NP.
  74. //
  75. if( *LocalMachineName == L'\0' ) {
  76. ComputerNameLength = MAX_COMPUTERNAME_LENGTH;
  77. if( !GetComputerName(
  78. LocalMachineName,
  79. &ComputerNameLength ) ) {
  80. *LocalMachineName = L'\0';
  81. }
  82. }
  83. //
  84. // if know machine ..
  85. //
  86. if( (*LocalMachineName != L'\0') ) {
  87. BOOL LocalMachine;
  88. //
  89. // if the machine has "\\" skip it for name compare.
  90. //
  91. if( *ServerIpAddress == L'\\' ) {
  92. LocalMachine = !_wcsicmp( LocalMachineName, ServerIpAddress + 2);
  93. }
  94. else {
  95. LocalMachine = !_wcsicmp( LocalMachineName, ServerIpAddress);
  96. }
  97. if( LocalMachine ) {
  98. return( DHCP_SERVER_USE_RPC_OVER_LPC );
  99. }
  100. }
  101. return( DHCP_SERVER_USE_RPC_OVER_NP );
  102. }
  103. handle_t
  104. DHCP_SRV_HANDLE_bind(
  105. DHCP_SRV_HANDLE ServerIpAddress
  106. )
  107. /*++
  108. Routine Description:
  109. This routine is called from the DHCP server service client stubs when
  110. it is necessary create an RPC binding to the server end.
  111. Arguments:
  112. ServerIpAddress - The IP address of the server to bind to.
  113. Return Value:
  114. The binding handle is returned to the stub routine. If the bind is
  115. unsuccessful, a NULL will be returned.
  116. --*/
  117. {
  118. RPC_STATUS rpcStatus;
  119. LPWSTR binding = NULL;
  120. handle_t bindingHandle;
  121. DWORD RpcProtocol;
  122. //
  123. // examine the ServerIpAddress string, if it is :
  124. //
  125. // 1. NULL or local IPAddress or Local Name - use "ncalrpc"
  126. // 2. IpAddress - (of the form "ppp.qqq.rrr.sss") - use "ncacn_ip_tcp"
  127. // 3. otherwise use "ncacn_np" protocol.
  128. //
  129. RpcProtocol = FindProtocolToUse( ServerIpAddress );
  130. if( RpcProtocol == DHCP_SERVER_USE_RPC_OVER_LPC ) {
  131. rpcStatus = RpcStringBindingComposeW(
  132. 0,
  133. L"ncalrpc",
  134. NULL,
  135. DHCP_LPC_EP,
  136. // L"Security=Impersonation Dynamic False",
  137. L"Security=Impersonation Static True",
  138. &binding);
  139. }
  140. else if( RpcProtocol == DHCP_SERVER_USE_RPC_OVER_NP ) {
  141. rpcStatus = RpcStringBindingComposeW(
  142. 0,
  143. L"ncacn_np",
  144. ServerIpAddress,
  145. DHCP_NAMED_PIPE,
  146. L"Security=Impersonation Static True",
  147. &binding);
  148. }
  149. else {
  150. rpcStatus = RpcStringBindingComposeW(
  151. 0,
  152. L"ncacn_ip_tcp",
  153. ServerIpAddress,
  154. DHCP_SERVER_BIND_PORT,
  155. NULL,
  156. &binding);
  157. }
  158. if ( rpcStatus != RPC_S_OK ) {
  159. goto Cleanup;
  160. }
  161. rpcStatus = RpcBindingFromStringBindingW( binding, &bindingHandle );
  162. if ( rpcStatus != RPC_S_OK ) {
  163. goto Cleanup;
  164. }
  165. if( RpcProtocol == DHCP_SERVER_USE_RPC_OVER_TCPIP ) {
  166. //
  167. // Tell RPC to do the security thing.
  168. //
  169. if( DhcpGlobalTryDownlevel ) {
  170. rpcStatus = RpcBindingSetAuthInfo(
  171. bindingHandle, // binding handle
  172. DHCP_SERVER_SECURITY, // app name to security provider
  173. RPC_C_AUTHN_LEVEL_CONNECT, // auth level
  174. DHCP_SERVER_SECURITY_AUTH_ID, // Auth package ID
  175. NULL, // client auth info, NULL specified logon info.
  176. RPC_C_AUTHZ_NAME );
  177. } else {
  178. rpcStatus = RpcBindingSetAuthInfo(
  179. bindingHandle, NULL,
  180. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  181. RPC_C_AUTHN_GSS_NEGOTIATE, NULL, RPC_C_AUTHZ_NAME );
  182. }
  183. } // if
  184. rpcStatus = RpcBindingSetOption( bindingHandle, RPC_C_OPT_CALL_TIMEOUT,
  185. fShortTimeOut ? DHCP_RPC_BIND_TIMEOUT_VALUE : 0);
  186. Cleanup:
  187. if ( NULL != binding ) {
  188. RpcStringFreeW(&binding);
  189. }
  190. if ( rpcStatus != RPC_S_OK ) {
  191. SetLastError( rpcStatus );
  192. return( NULL );
  193. }
  194. return bindingHandle;
  195. }
  196. void
  197. DHCP_SRV_HANDLE_unbind(
  198. DHCP_SRV_HANDLE ServerIpAddress,
  199. handle_t BindHandle
  200. )
  201. /*++
  202. Routine Description:
  203. This routine is called from the DHCP server service client stubs
  204. when it is necessary to unbind from the server end.
  205. Arguments:
  206. ServerIpAddress - This is the IP address of the server from which to unbind.
  207. BindingHandle - This is the binding handle that is to be closed.
  208. Return Value:
  209. None.
  210. --*/
  211. {
  212. fShortTimeOut = FALSE;
  213. (VOID)RpcBindingFree(&BindHandle);
  214. }