2002-12-02 22:17:04 +01:00
|
|
|
/*
|
2008-10-18 19:22:19 +02:00
|
|
|
* Copyright 2001, Ove Kåven, TransGaming Technologies Inc.
|
2002-12-02 22:17:04 +01:00
|
|
|
* Copyright 2002 Greg Turner
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-12-02 22:17:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2008-12-03 16:41:39 +01:00
|
|
|
#include <stdarg.h>
|
2002-12-02 22:17:04 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2008-12-03 16:41:39 +01:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2002-12-02 22:17:04 +01:00
|
|
|
#include "winnt.h"
|
2016-08-31 15:21:17 +02:00
|
|
|
#include "winsvc.h"
|
2007-09-10 17:10:45 +02:00
|
|
|
#include "irot.h"
|
2008-06-25 19:26:48 +02:00
|
|
|
#include "epm.h"
|
2019-11-07 21:00:00 +01:00
|
|
|
#include "irpcss.h"
|
2002-12-02 22:17:04 +01:00
|
|
|
|
|
|
|
#include "wine/debug.h"
|
2020-09-02 13:14:49 +02:00
|
|
|
#include "wine/heap.h"
|
|
|
|
#include "wine/list.h"
|
2002-12-02 22:17:04 +01:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ole);
|
|
|
|
|
2020-10-18 23:33:30 +02:00
|
|
|
static WCHAR rpcssW[] = L"RpcSs";
|
2007-06-19 14:12:07 +02:00
|
|
|
static HANDLE exit_event;
|
2016-08-31 15:21:17 +02:00
|
|
|
static SERVICE_STATUS_HANDLE service_handle;
|
2002-12-02 22:17:04 +01:00
|
|
|
|
2020-09-02 13:14:49 +02:00
|
|
|
struct registered_class
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
GUID clsid;
|
|
|
|
unsigned int cookie;
|
|
|
|
PMInterfacePointer object;
|
|
|
|
unsigned int single_use : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
static CRITICAL_SECTION registered_classes_cs = { NULL, -1, 0, 0, 0, 0 };
|
|
|
|
static struct list registered_classes = LIST_INIT(registered_classes);
|
|
|
|
|
|
|
|
HRESULT __cdecl irpcss_server_register(handle_t h, const GUID *clsid, DWORD flags,
|
|
|
|
PMInterfacePointer object, unsigned int *cookie)
|
|
|
|
{
|
|
|
|
struct registered_class *entry;
|
|
|
|
static int next_cookie;
|
|
|
|
|
|
|
|
if (!(entry = heap_alloc_zero(sizeof(*entry))))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
entry->clsid = *clsid;
|
|
|
|
entry->single_use = !(flags & (REGCLS_MULTIPLEUSE | REGCLS_MULTI_SEPARATE));
|
|
|
|
if (!(entry->object = heap_alloc(FIELD_OFFSET(MInterfacePointer, abData[object->ulCntData]))))
|
|
|
|
{
|
|
|
|
heap_free(entry);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
entry->object->ulCntData = object->ulCntData;
|
|
|
|
memcpy(&entry->object->abData, object->abData, object->ulCntData);
|
|
|
|
*cookie = entry->cookie = InterlockedIncrement(&next_cookie);
|
|
|
|
|
|
|
|
EnterCriticalSection(®istered_classes_cs);
|
|
|
|
list_add_tail(®istered_classes, &entry->entry);
|
|
|
|
LeaveCriticalSection(®istered_classes_cs);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scm_revoke_class(struct registered_class *_class)
|
|
|
|
{
|
|
|
|
list_remove(&_class->entry);
|
|
|
|
heap_free(_class->object);
|
|
|
|
heap_free(_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __cdecl irpcss_server_revoke(handle_t h, unsigned int cookie)
|
|
|
|
{
|
|
|
|
struct registered_class *cur;
|
|
|
|
|
|
|
|
EnterCriticalSection(®istered_classes_cs);
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY(cur, ®istered_classes, struct registered_class, entry)
|
|
|
|
{
|
|
|
|
if (cur->cookie == cookie)
|
|
|
|
{
|
|
|
|
scm_revoke_class(cur);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(®istered_classes_cs);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __cdecl irpcss_get_class_object(handle_t h, const GUID *clsid,
|
|
|
|
PMInterfacePointer *object)
|
|
|
|
{
|
|
|
|
struct registered_class *cur;
|
|
|
|
|
|
|
|
*object = NULL;
|
|
|
|
|
|
|
|
EnterCriticalSection(®istered_classes_cs);
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY(cur, ®istered_classes, struct registered_class, entry)
|
|
|
|
{
|
|
|
|
if (!memcmp(clsid, &cur->clsid, sizeof(*clsid)))
|
|
|
|
{
|
|
|
|
*object = MIDL_user_allocate(FIELD_OFFSET(MInterfacePointer, abData[cur->object->ulCntData]));
|
|
|
|
if (*object)
|
|
|
|
{
|
|
|
|
(*object)->ulCntData = cur->object->ulCntData;
|
|
|
|
memcpy((*object)->abData, cur->object->abData, cur->object->ulCntData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur->single_use)
|
|
|
|
scm_revoke_class(cur);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(®istered_classes_cs);
|
|
|
|
|
|
|
|
return *object ? S_OK : E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:00:00 +01:00
|
|
|
HRESULT __cdecl irpcss_get_thread_seq_id(handle_t h, DWORD *id)
|
|
|
|
{
|
|
|
|
static LONG thread_seq_id;
|
|
|
|
*id = InterlockedIncrement(&thread_seq_id);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:59:59 +01:00
|
|
|
static RPC_STATUS RPCSS_Initialize(void)
|
2002-12-02 22:17:04 +01:00
|
|
|
{
|
2019-11-07 20:59:59 +01:00
|
|
|
static unsigned short irot_protseq[] = IROT_PROTSEQ;
|
|
|
|
static unsigned short irot_endpoint[] = IROT_ENDPOINT;
|
2020-10-18 23:33:30 +02:00
|
|
|
static unsigned short epm_protseq[] = L"ncacn_np";
|
|
|
|
static unsigned short epm_endpoint[] = L"\\pipe\\epmapper";
|
|
|
|
static unsigned short epm_protseq_lrpc[] = L"ncalrpc";
|
|
|
|
static unsigned short epm_endpoint_lrpc[] = L"epmapper";
|
2019-11-07 21:00:00 +01:00
|
|
|
static unsigned short irpcss_protseq[] = IRPCSS_PROTSEQ;
|
|
|
|
static unsigned short irpcss_endpoint[] = IRPCSS_ENDPOINT;
|
2019-11-07 20:59:59 +01:00
|
|
|
static const struct protseq_map
|
|
|
|
{
|
|
|
|
unsigned short *protseq;
|
|
|
|
unsigned short *endpoint;
|
|
|
|
} protseqs[] =
|
|
|
|
{
|
|
|
|
{ epm_protseq, epm_endpoint },
|
|
|
|
{ epm_protseq_lrpc, epm_endpoint_lrpc },
|
|
|
|
{ irot_protseq, irot_endpoint },
|
2019-11-07 21:00:00 +01:00
|
|
|
{ irpcss_protseq, irpcss_endpoint },
|
2019-11-07 20:59:59 +01:00
|
|
|
};
|
|
|
|
RPC_IF_HANDLE ifspecs[] =
|
|
|
|
{
|
|
|
|
epm_v3_0_s_ifspec,
|
|
|
|
Irot_v0_2_s_ifspec,
|
2019-11-07 21:00:00 +01:00
|
|
|
Irpcss_v0_0_s_ifspec,
|
2019-11-07 20:59:59 +01:00
|
|
|
};
|
|
|
|
RPC_STATUS status;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
WINE_TRACE("\n");
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < ARRAY_SIZE(ifspecs); ++i, j = i)
|
|
|
|
{
|
|
|
|
status = RpcServerRegisterIf(ifspecs[i], NULL, NULL);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(protseqs); ++i)
|
|
|
|
{
|
|
|
|
status = RpcServerUseProtseqEpW(protseqs[i].protseq, RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
|
|
|
|
protseqs[i].endpoint, NULL);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
2008-06-25 19:26:48 +02:00
|
|
|
|
|
|
|
fail:
|
2019-11-07 20:59:59 +01:00
|
|
|
for (i = 0; i < j; ++i)
|
|
|
|
RpcServerUnregisterIf(ifspecs[i], NULL, FALSE);
|
|
|
|
|
|
|
|
return status;
|
2002-12-02 22:17:04 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 15:21:17 +02:00
|
|
|
static DWORD WINAPI service_handler( DWORD ctrl, DWORD event_type, LPVOID event_data, LPVOID context )
|
|
|
|
{
|
|
|
|
SERVICE_STATUS status;
|
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 0;
|
|
|
|
|
|
|
|
switch (ctrl)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
TRACE( "shutting down\n" );
|
|
|
|
RpcMgmtStopServerListening( NULL );
|
|
|
|
RpcServerUnregisterIf( epm_v3_0_s_ifspec, NULL, TRUE );
|
|
|
|
RpcServerUnregisterIf( Irot_v0_2_s_ifspec, NULL, TRUE );
|
|
|
|
status.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
SetEvent( exit_event );
|
|
|
|
return NO_ERROR;
|
|
|
|
default:
|
|
|
|
FIXME( "got service ctrl %x\n", ctrl );
|
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WINAPI ServiceMain( DWORD argc, LPWSTR *argv )
|
2002-12-02 22:17:04 +01:00
|
|
|
{
|
2016-08-31 15:21:17 +02:00
|
|
|
SERVICE_STATUS status;
|
2019-11-07 20:59:59 +01:00
|
|
|
RPC_STATUS ret;
|
2007-09-10 17:10:45 +02:00
|
|
|
|
2016-08-31 15:21:17 +02:00
|
|
|
TRACE( "starting service\n" );
|
2007-06-19 14:12:07 +02:00
|
|
|
|
2019-11-07 20:59:59 +01:00
|
|
|
if ((ret = RPCSS_Initialize()))
|
|
|
|
{
|
|
|
|
WARN("Failed to initialize rpc interfaces, status %d.\n", ret);
|
|
|
|
return;
|
|
|
|
}
|
2016-08-31 15:21:17 +02:00
|
|
|
|
|
|
|
exit_event = CreateEventW( NULL, TRUE, FALSE, NULL );
|
|
|
|
|
|
|
|
service_handle = RegisterServiceCtrlHandlerExW( rpcssW, service_handler, NULL );
|
|
|
|
if (!service_handle) return;
|
|
|
|
|
|
|
|
status.dwServiceType = SERVICE_WIN32;
|
|
|
|
status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
status.dwWin32ExitCode = 0;
|
|
|
|
status.dwServiceSpecificExitCode = 0;
|
|
|
|
status.dwCheckPoint = 0;
|
|
|
|
status.dwWaitHint = 10000;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
|
|
|
|
WaitForSingleObject( exit_event, INFINITE );
|
|
|
|
|
|
|
|
status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
status.dwControlsAccepted = 0;
|
|
|
|
SetServiceStatus( service_handle, &status );
|
|
|
|
TRACE( "service stopped\n" );
|
2002-12-02 22:17:04 +01:00
|
|
|
}
|
|
|
|
|
2019-10-17 17:05:47 +02:00
|
|
|
int __cdecl wmain( int argc, WCHAR *argv[] )
|
2002-12-02 22:17:04 +01:00
|
|
|
{
|
2016-08-31 15:21:17 +02:00
|
|
|
static const SERVICE_TABLE_ENTRYW service_table[] =
|
|
|
|
{
|
|
|
|
{ rpcssW, ServiceMain },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
StartServiceCtrlDispatcherW( service_table );
|
|
|
|
return 0;
|
2002-12-02 22:17:04 +01:00
|
|
|
}
|