316 lines
5.8 KiB
C
316 lines
5.8 KiB
C
/*++
|
||
|
||
Copyright (c) 1990 Microsoft Corporation
|
||
|
||
Module Name:
|
||
|
||
bind.c
|
||
|
||
Abstract:
|
||
|
||
Contains the RPC bind and un-bind routines
|
||
|
||
Author:
|
||
|
||
Dave Snipp (davesn) 01-Jun-1991
|
||
|
||
Environment:
|
||
|
||
User Mode -Win32
|
||
|
||
Revision History:
|
||
|
||
--*/
|
||
|
||
//
|
||
// INCLUDES
|
||
//
|
||
#include <nt.h>
|
||
#include <ntrtl.h>
|
||
#include <nturtl.h>
|
||
|
||
#include <excpt.h>
|
||
#include <rpc.h> // DataTypes and runtime APIs
|
||
#include <winspl.h> // generated by the MIDL complier
|
||
#include <string.h>
|
||
#include <splcom.h>
|
||
|
||
LPWSTR InterfaceAddress = L"\\pipe\\spoolss";
|
||
handle_t GlobalBindHandle;
|
||
|
||
#ifdef DEBUG_BIND_CREF
|
||
#include "spllib.hxx"
|
||
|
||
//
|
||
// This code can be used to track down any bind leaks.
|
||
//
|
||
|
||
HANDLE gpbtHandleBind;
|
||
HANDLE gpbtStringBind;
|
||
|
||
PDBG_POINTERS gpDbgPointers;
|
||
|
||
UINT gcHandleBind;
|
||
UINT gcStringBind;
|
||
|
||
CRITICAL_SECTION csBind;
|
||
|
||
VOID
|
||
InitDebug(
|
||
VOID
|
||
)
|
||
{
|
||
InitializeCriticalSection( &csBind );
|
||
gpDbgPointers = DbgGetPointers();
|
||
|
||
if( gpDbgPointers ){
|
||
|
||
gpbtHandleBind = gpDbgPointers->pfnAllocBackTraceMem();;
|
||
gpbtStringBind = gpDbgPointers->pfnAllocBackTraceMem();;
|
||
}
|
||
}
|
||
|
||
VOID
|
||
DbgBindIncRef(
|
||
HANDLE hBackTrace,
|
||
PUINT pcBind
|
||
)
|
||
{
|
||
EnterCriticalSection( &csBind );
|
||
|
||
++(*pcBind);
|
||
|
||
gpDbgPointers->pfnCaptureBackTrace( hBackTrace,
|
||
*pcBind-1,
|
||
*pcBind,
|
||
0 );
|
||
|
||
LeaveCriticalSection( &csBind );
|
||
}
|
||
|
||
|
||
VOID
|
||
DbgBindDecRef(
|
||
HANDLE hBackTrace,
|
||
PUINT pcBind
|
||
)
|
||
{
|
||
EnterCriticalSection( &csBind );
|
||
|
||
--(*pcBind);
|
||
|
||
gpDbgPointers->pfnCaptureBackTrace( hBackTrace,
|
||
*pcBind+1,
|
||
*pcBind,
|
||
0 );
|
||
|
||
LeaveCriticalSection( &csBind );
|
||
}
|
||
#endif
|
||
|
||
|
||
handle_t
|
||
PRINTER_HANDLE_bind (
|
||
PRINTER_HANDLE hPrinter)
|
||
|
||
/*++
|
||
|
||
Routine Description:
|
||
|
||
This routine is used to obtain a binding to the printer spooler.
|
||
|
||
Arguments:
|
||
|
||
Server - Supplies the name of the server where the printer spooler
|
||
should be binded with.
|
||
|
||
Return Value:
|
||
|
||
A binding to the server will be returned, unless an error occurs,
|
||
in which case zero will be returned.
|
||
|
||
--*/
|
||
{
|
||
RPC_STATUS RpcStatus;
|
||
LPWSTR StringBinding;
|
||
handle_t BindingHandle;
|
||
|
||
RpcStatus = RpcStringBindingComposeW(0, L"ncacn_np", 0, InterfaceAddress,
|
||
L"Security=Impersonation Static False", &StringBinding);
|
||
|
||
if ( RpcStatus != RPC_S_OK ) {
|
||
return( 0 );
|
||
}
|
||
|
||
RpcStatus = RpcBindingFromStringBindingW(StringBinding, &BindingHandle);
|
||
|
||
RpcStringFreeW(&StringBinding);
|
||
|
||
if ( RpcStatus != RPC_S_OK ) {
|
||
return(0);
|
||
}
|
||
|
||
#ifdef DEBUG_BIND_CREF
|
||
DbgBindIncRef( gpbtHandleBind,
|
||
&gcHandleBind );
|
||
#endif
|
||
|
||
return(BindingHandle);
|
||
}
|
||
|
||
|
||
|
||
void
|
||
PRINTER_HANDLE_unbind (
|
||
PRINTER_HANDLE hPrinter,
|
||
handle_t BindingHandle)
|
||
|
||
/*++
|
||
|
||
Routine Description:
|
||
|
||
This routine calls a common unbind routine that is shared by
|
||
all services.
|
||
This routine is called from the server service client stubs when
|
||
it is necessary to unbind to a server.
|
||
|
||
|
||
Arguments:
|
||
|
||
ServerName - This is the name of the server from which to unbind.
|
||
|
||
BindingHandle - This is the binding handle that is to be closed.
|
||
|
||
Return Value:
|
||
|
||
none.
|
||
|
||
--*/
|
||
{
|
||
RPC_STATUS RpcStatus;
|
||
|
||
RpcStatus = RpcBindingFree(&BindingHandle);
|
||
ASSERT(RpcStatus == RPC_S_OK);
|
||
|
||
|
||
#ifdef DEBUG_BIND_CREF
|
||
DbgBindDecRef( gpbtHandleBind,
|
||
&gcHandleBind );
|
||
#endif
|
||
|
||
return;
|
||
}
|
||
|
||
|
||
handle_t
|
||
STRING_HANDLE_bind (
|
||
STRING_HANDLE lpStr)
|
||
|
||
/*++
|
||
|
||
Routine Description:
|
||
This routine calls a common bind routine that is shared by all services.
|
||
This routine is called from the server service client stubs when
|
||
it is necessary to bind to a server.
|
||
|
||
Arguments:
|
||
|
||
lpStr - \\ServerName\PrinterName
|
||
|
||
Return Value:
|
||
|
||
The binding handle is returned to the stub routine. If the
|
||
binding is unsuccessful, a NULL will be returned.
|
||
|
||
--*/
|
||
{
|
||
RPC_STATUS RpcStatus;
|
||
LPWSTR StringBinding;
|
||
handle_t BindingHandle;
|
||
WCHAR ServerName[MAX_PATH + 2];
|
||
DWORD i;
|
||
|
||
|
||
if (lpStr && lpStr[0] == L'\\' && lpStr[1] == L'\\') {
|
||
|
||
// We have a servername
|
||
for (i = 2 ; lpStr[i] && lpStr[i] != L'\\' ; ++i)
|
||
;
|
||
|
||
if (i >= COUNTOF(ServerName))
|
||
return FALSE;
|
||
|
||
wcsncpy(ServerName, lpStr, i);
|
||
ServerName[i] = L'\0';
|
||
|
||
} else
|
||
|
||
return FALSE;
|
||
|
||
RpcStatus = RpcStringBindingComposeW(0, L"ncacn_np", ServerName,
|
||
InterfaceAddress,
|
||
L"Security=Impersonation Dynamic True",
|
||
&StringBinding);
|
||
|
||
if ( RpcStatus != RPC_S_OK ) {
|
||
return( 0 );
|
||
}
|
||
|
||
RpcStatus = RpcBindingFromStringBindingW(StringBinding, &BindingHandle);
|
||
|
||
RpcStringFreeW(&StringBinding);
|
||
|
||
if ( RpcStatus != RPC_S_OK ) {
|
||
return(0);
|
||
}
|
||
|
||
#ifdef DEBUG_BIND_CREF
|
||
DbgBindIncRef( gpbtStringBind,
|
||
&gcStringBind );
|
||
#endif
|
||
|
||
return(BindingHandle);
|
||
}
|
||
|
||
|
||
|
||
void
|
||
STRING_HANDLE_unbind (
|
||
STRING_HANDLE lpStr,
|
||
handle_t BindingHandle)
|
||
|
||
/*++
|
||
|
||
Routine Description:
|
||
|
||
This routine calls a common unbind routine that is shared by
|
||
all services.
|
||
This routine is called from the server service client stubs when
|
||
it is necessary to unbind to a server.
|
||
|
||
|
||
Arguments:
|
||
|
||
ServerName - This is the name of the server from which to unbind.
|
||
|
||
BindingHandle - This is the binding handle that is to be closed.
|
||
|
||
Return Value:
|
||
|
||
none.
|
||
|
||
--*/
|
||
{
|
||
RPC_STATUS RpcStatus;
|
||
|
||
RpcStatus = RpcBindingFree(&BindingHandle);
|
||
ASSERT(RpcStatus != RPC_S_INVALID_BINDING);
|
||
|
||
#ifdef DEBUG_BIND_CREF
|
||
DbgBindDecRef( gpbtStringBind,
|
||
&gcStringBind );
|
||
#endif
|
||
|
||
return;
|
||
}
|