Source code of Windows XP (NT5)
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.

315 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. bind.c
  5. Abstract:
  6. Contains the RPC bind and un-bind routines
  7. Author:
  8. Dave Snipp (davesn) 01-Jun-1991
  9. Environment:
  10. User Mode -Win32
  11. Revision History:
  12. --*/
  13. //
  14. // INCLUDES
  15. //
  16. #include <nt.h>
  17. #include <ntrtl.h>
  18. #include <nturtl.h>
  19. #include <excpt.h>
  20. #include <rpc.h> // DataTypes and runtime APIs
  21. #include <winspl.h> // generated by the MIDL complier
  22. #include <string.h>
  23. #include <splcom.h>
  24. LPWSTR InterfaceAddress = L"\\pipe\\spoolss";
  25. handle_t GlobalBindHandle;
  26. #ifdef DEBUG_BIND_CREF
  27. #include "spllib.hxx"
  28. //
  29. // This code can be used to track down any bind leaks.
  30. //
  31. HANDLE gpbtHandleBind;
  32. HANDLE gpbtStringBind;
  33. PDBG_POINTERS gpDbgPointers;
  34. UINT gcHandleBind;
  35. UINT gcStringBind;
  36. CRITICAL_SECTION csBind;
  37. VOID
  38. InitDebug(
  39. VOID
  40. )
  41. {
  42. InitializeCriticalSection( &csBind );
  43. gpDbgPointers = DbgGetPointers();
  44. if( gpDbgPointers ){
  45. gpbtHandleBind = gpDbgPointers->pfnAllocBackTraceMem();;
  46. gpbtStringBind = gpDbgPointers->pfnAllocBackTraceMem();;
  47. }
  48. }
  49. VOID
  50. DbgBindIncRef(
  51. HANDLE hBackTrace,
  52. PUINT pcBind
  53. )
  54. {
  55. EnterCriticalSection( &csBind );
  56. ++(*pcBind);
  57. gpDbgPointers->pfnCaptureBackTrace( hBackTrace,
  58. *pcBind-1,
  59. *pcBind,
  60. 0 );
  61. LeaveCriticalSection( &csBind );
  62. }
  63. VOID
  64. DbgBindDecRef(
  65. HANDLE hBackTrace,
  66. PUINT pcBind
  67. )
  68. {
  69. EnterCriticalSection( &csBind );
  70. --(*pcBind);
  71. gpDbgPointers->pfnCaptureBackTrace( hBackTrace,
  72. *pcBind+1,
  73. *pcBind,
  74. 0 );
  75. LeaveCriticalSection( &csBind );
  76. }
  77. #endif
  78. handle_t
  79. PRINTER_HANDLE_bind (
  80. PRINTER_HANDLE hPrinter)
  81. /*++
  82. Routine Description:
  83. This routine is used to obtain a binding to the printer spooler.
  84. Arguments:
  85. Server - Supplies the name of the server where the printer spooler
  86. should be binded with.
  87. Return Value:
  88. A binding to the server will be returned, unless an error occurs,
  89. in which case zero will be returned.
  90. --*/
  91. {
  92. RPC_STATUS RpcStatus;
  93. LPWSTR StringBinding;
  94. handle_t BindingHandle;
  95. RpcStatus = RpcStringBindingComposeW(0, L"ncacn_np", 0, InterfaceAddress,
  96. L"Security=Impersonation Static False", &StringBinding);
  97. if ( RpcStatus != RPC_S_OK ) {
  98. return( 0 );
  99. }
  100. RpcStatus = RpcBindingFromStringBindingW(StringBinding, &BindingHandle);
  101. RpcStringFreeW(&StringBinding);
  102. if ( RpcStatus != RPC_S_OK ) {
  103. return(0);
  104. }
  105. #ifdef DEBUG_BIND_CREF
  106. DbgBindIncRef( gpbtHandleBind,
  107. &gcHandleBind );
  108. #endif
  109. return(BindingHandle);
  110. }
  111. void
  112. PRINTER_HANDLE_unbind (
  113. PRINTER_HANDLE hPrinter,
  114. handle_t BindingHandle)
  115. /*++
  116. Routine Description:
  117. This routine calls a common unbind routine that is shared by
  118. all services.
  119. This routine is called from the server service client stubs when
  120. it is necessary to unbind to a server.
  121. Arguments:
  122. ServerName - This is the name of the server from which to unbind.
  123. BindingHandle - This is the binding handle that is to be closed.
  124. Return Value:
  125. none.
  126. --*/
  127. {
  128. RPC_STATUS RpcStatus;
  129. RpcStatus = RpcBindingFree(&BindingHandle);
  130. ASSERT(RpcStatus == RPC_S_OK);
  131. #ifdef DEBUG_BIND_CREF
  132. DbgBindDecRef( gpbtHandleBind,
  133. &gcHandleBind );
  134. #endif
  135. return;
  136. }
  137. handle_t
  138. STRING_HANDLE_bind (
  139. STRING_HANDLE lpStr)
  140. /*++
  141. Routine Description:
  142. This routine calls a common bind routine that is shared by all services.
  143. This routine is called from the server service client stubs when
  144. it is necessary to bind to a server.
  145. Arguments:
  146. lpStr - \\ServerName\PrinterName
  147. Return Value:
  148. The binding handle is returned to the stub routine. If the
  149. binding is unsuccessful, a NULL will be returned.
  150. --*/
  151. {
  152. RPC_STATUS RpcStatus;
  153. LPWSTR StringBinding;
  154. handle_t BindingHandle;
  155. WCHAR ServerName[MAX_PATH + 2];
  156. DWORD i;
  157. if (lpStr && lpStr[0] == L'\\' && lpStr[1] == L'\\') {
  158. // We have a servername
  159. for (i = 2 ; lpStr[i] && lpStr[i] != L'\\' ; ++i)
  160. ;
  161. if (i >= COUNTOF(ServerName))
  162. return FALSE;
  163. wcsncpy(ServerName, lpStr, i);
  164. ServerName[i] = L'\0';
  165. } else
  166. return FALSE;
  167. RpcStatus = RpcStringBindingComposeW(0, L"ncacn_np", ServerName,
  168. InterfaceAddress,
  169. L"Security=Impersonation Dynamic True",
  170. &StringBinding);
  171. if ( RpcStatus != RPC_S_OK ) {
  172. return( 0 );
  173. }
  174. RpcStatus = RpcBindingFromStringBindingW(StringBinding, &BindingHandle);
  175. RpcStringFreeW(&StringBinding);
  176. if ( RpcStatus != RPC_S_OK ) {
  177. return(0);
  178. }
  179. #ifdef DEBUG_BIND_CREF
  180. DbgBindIncRef( gpbtStringBind,
  181. &gcStringBind );
  182. #endif
  183. return(BindingHandle);
  184. }
  185. void
  186. STRING_HANDLE_unbind (
  187. STRING_HANDLE lpStr,
  188. handle_t BindingHandle)
  189. /*++
  190. Routine Description:
  191. This routine calls a common unbind routine that is shared by
  192. all services.
  193. This routine is called from the server service client stubs when
  194. it is necessary to unbind to a server.
  195. Arguments:
  196. ServerName - This is the name of the server from which to unbind.
  197. BindingHandle - This is the binding handle that is to be closed.
  198. Return Value:
  199. none.
  200. --*/
  201. {
  202. RPC_STATUS RpcStatus;
  203. RpcStatus = RpcBindingFree(&BindingHandle);
  204. ASSERT(RpcStatus != RPC_S_INVALID_BINDING);
  205. #ifdef DEBUG_BIND_CREF
  206. DbgBindDecRef( gpbtStringBind,
  207. &gcStringBind );
  208. #endif
  209. return;
  210. }