2006-04-21 08:38:57 +02:00
|
|
|
/*
|
|
|
|
* RPC transport layer
|
|
|
|
*
|
2008-10-18 19:20:44 +02:00
|
|
|
* Copyright 2001 Ove Kåven, TransGaming Technologies
|
2006-04-21 08:38:57 +02:00
|
|
|
* Copyright 2003 Mike Hearn
|
|
|
|
* Copyright 2004 Filip Navara
|
|
|
|
* Copyright 2006 Mike McCormack
|
2006-10-18 18:14:11 +02:00
|
|
|
* Copyright 2006 Damjan Jovanovic
|
2006-04-21 08:38:57 +02:00
|
|
|
*
|
|
|
|
* 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
|
2006-04-21 08:38:57 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-05-16 17:47:50 +02:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "ws2tcpip.h"
|
2006-04-21 11:20:16 +02:00
|
|
|
|
2006-04-21 08:38:57 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2006-04-21 11:20:16 +02:00
|
|
|
|
2006-04-21 08:38:57 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
#include "winerror.h"
|
2009-03-18 12:13:19 +01:00
|
|
|
#include "wininet.h"
|
2006-04-21 08:38:57 +02:00
|
|
|
#include "winternl.h"
|
2017-05-22 16:04:31 +02:00
|
|
|
#include "winioctl.h"
|
2006-04-21 08:38:57 +02:00
|
|
|
|
|
|
|
#include "rpc.h"
|
|
|
|
#include "rpcndr.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
#include "rpc_binding.h"
|
2009-03-18 12:13:19 +01:00
|
|
|
#include "rpc_assoc.h"
|
2006-04-21 08:38:57 +02:00
|
|
|
#include "rpc_message.h"
|
2006-10-25 14:28:56 +02:00
|
|
|
#include "rpc_server.h"
|
2006-06-01 21:25:06 +02:00
|
|
|
#include "epm_towers.h"
|
2006-04-21 08:38:57 +02:00
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
#define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
|
|
|
|
|
2006-04-21 08:38:57 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(rpc);
|
|
|
|
|
2017-05-23 17:39:39 +02:00
|
|
|
static RpcConnection *rpcrt4_spawn_connection(RpcConnection *old_connection);
|
2009-01-17 18:45:06 +01:00
|
|
|
|
2006-04-21 11:20:16 +02:00
|
|
|
/**** ncacn_np support ****/
|
|
|
|
|
2006-04-21 08:40:06 +02:00
|
|
|
typedef struct _RpcConnection_np
|
|
|
|
{
|
2017-05-22 16:04:31 +02:00
|
|
|
RpcConnection common;
|
|
|
|
HANDLE pipe;
|
|
|
|
HANDLE listen_event;
|
2017-05-31 17:30:23 +02:00
|
|
|
char *listen_pipe;
|
2017-05-22 16:04:31 +02:00
|
|
|
IO_STATUS_BLOCK io_status;
|
2017-05-22 16:04:43 +02:00
|
|
|
HANDLE event_cache;
|
2017-05-24 13:52:14 +02:00
|
|
|
BOOL read_closed;
|
2006-04-21 08:40:06 +02:00
|
|
|
} RpcConnection_np;
|
|
|
|
|
|
|
|
static RpcConnection *rpcrt4_conn_np_alloc(void)
|
|
|
|
{
|
2011-09-14 14:19:29 +02:00
|
|
|
RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcConnection_np));
|
2006-10-03 15:40:57 +02:00
|
|
|
return &npc->common;
|
2006-04-21 08:40:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:04:43 +02:00
|
|
|
static HANDLE get_np_event(RpcConnection_np *connection)
|
2017-05-22 16:04:15 +02:00
|
|
|
{
|
2017-05-22 16:04:43 +02:00
|
|
|
HANDLE event = InterlockedExchangePointer(&connection->event_cache, NULL);
|
|
|
|
return event ? event : CreateEventW(NULL, TRUE, FALSE, NULL);
|
2017-05-22 16:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:04:43 +02:00
|
|
|
static void release_np_event(RpcConnection_np *connection, HANDLE event)
|
2017-05-22 16:04:15 +02:00
|
|
|
{
|
2017-05-22 16:04:43 +02:00
|
|
|
event = InterlockedExchangePointer(&connection->event_cache, event);
|
|
|
|
if (event)
|
|
|
|
CloseHandle(event);
|
2017-05-22 16:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 17:30:23 +02:00
|
|
|
static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *conn)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2017-05-31 17:30:23 +02:00
|
|
|
RpcConnection_np *connection = (RpcConnection_np *) conn;
|
2006-05-19 12:03:29 +02:00
|
|
|
|
2017-05-31 17:30:23 +02:00
|
|
|
TRACE("listening on %s\n", connection->listen_pipe);
|
|
|
|
|
|
|
|
connection->pipe = CreateNamedPipeA(connection->listen_pipe, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
|
|
|
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
|
|
|
|
PIPE_UNLIMITED_INSTANCES,
|
|
|
|
RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
|
|
|
|
if (connection->pipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
WARN("CreateNamedPipe failed with error %d\n", GetLastError());
|
|
|
|
if (GetLastError() == ERROR_FILE_EXISTS)
|
|
|
|
return RPC_S_DUPLICATE_ENDPOINT;
|
|
|
|
else
|
|
|
|
return RPC_S_CANT_CREATE_ENDPOINT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2006-04-21 08:40:06 +02:00
|
|
|
RpcConnection_np *npc = (RpcConnection_np *) Connection;
|
|
|
|
HANDLE pipe;
|
2006-04-21 08:38:57 +02:00
|
|
|
DWORD err, dwMode;
|
|
|
|
|
|
|
|
TRACE("connecting to %s\n", pname);
|
|
|
|
|
|
|
|
while (TRUE) {
|
2007-01-19 14:01:23 +01:00
|
|
|
DWORD dwFlags = 0;
|
|
|
|
if (Connection->QOS)
|
|
|
|
{
|
2007-01-24 01:27:01 +01:00
|
|
|
dwFlags = SECURITY_SQOS_PRESENT;
|
2007-01-19 14:01:23 +01:00
|
|
|
switch (Connection->QOS->qos->ImpersonationType)
|
|
|
|
{
|
|
|
|
case RPC_C_IMP_LEVEL_DEFAULT:
|
|
|
|
/* FIXME: what to do here? */
|
|
|
|
break;
|
|
|
|
case RPC_C_IMP_LEVEL_ANONYMOUS:
|
|
|
|
dwFlags |= SECURITY_ANONYMOUS;
|
|
|
|
break;
|
|
|
|
case RPC_C_IMP_LEVEL_IDENTIFY:
|
|
|
|
dwFlags |= SECURITY_IDENTIFICATION;
|
|
|
|
break;
|
|
|
|
case RPC_C_IMP_LEVEL_IMPERSONATE:
|
|
|
|
dwFlags |= SECURITY_IMPERSONATION;
|
|
|
|
break;
|
|
|
|
case RPC_C_IMP_LEVEL_DELEGATE:
|
|
|
|
dwFlags |= SECURITY_DELEGATION;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-04 09:10:30 +01:00
|
|
|
if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTITY_DYNAMIC)
|
2007-01-19 14:01:23 +01:00
|
|
|
dwFlags |= SECURITY_CONTEXT_TRACKING;
|
|
|
|
}
|
2006-04-21 08:40:06 +02:00
|
|
|
pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
|
2017-05-22 16:04:15 +02:00
|
|
|
OPEN_EXISTING, dwFlags | FILE_FLAG_OVERLAPPED, 0);
|
2006-04-21 08:40:06 +02:00
|
|
|
if (pipe != INVALID_HANDLE_VALUE) break;
|
2006-04-21 08:38:57 +02:00
|
|
|
err = GetLastError();
|
|
|
|
if (err == ERROR_PIPE_BUSY) {
|
2017-09-27 20:18:07 +02:00
|
|
|
if (WaitNamedPipeA(pname, NMPWAIT_USE_DEFAULT_WAIT)) {
|
|
|
|
TRACE("retrying busy server\n");
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-09 23:04:48 +01:00
|
|
|
TRACE("connection failed, error=%x\n", err);
|
2006-04-21 08:38:57 +02:00
|
|
|
return RPC_S_SERVER_TOO_BUSY;
|
|
|
|
}
|
2008-08-14 10:15:09 +02:00
|
|
|
if (!wait || !WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
|
2006-04-21 08:38:57 +02:00
|
|
|
err = GetLastError();
|
2006-11-09 23:04:48 +01:00
|
|
|
WARN("connection failed, error=%x\n", err);
|
2006-04-21 08:38:57 +02:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
/* pipe is connected; change to message-read mode. */
|
|
|
|
dwMode = PIPE_READMODE_MESSAGE;
|
2006-04-21 08:40:06 +02:00
|
|
|
SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
|
|
|
|
npc->pipe = pipe;
|
2006-04-21 08:38:57 +02:00
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-27 19:27:48 +01:00
|
|
|
static char *ncalrpc_pipe_name(const char *endpoint)
|
|
|
|
{
|
|
|
|
static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
|
|
|
|
char *pipe_name;
|
|
|
|
|
|
|
|
/* protseq=ncalrpc: supposed to use NT LPC ports,
|
|
|
|
* but we'll implement it with named pipes for now */
|
|
|
|
pipe_name = I_RpcAllocate(sizeof(prefix) + strlen(endpoint));
|
|
|
|
strcat(strcpy(pipe_name, prefix), endpoint);
|
|
|
|
return pipe_name;
|
|
|
|
}
|
|
|
|
|
2006-04-21 08:38:57 +02:00
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
|
|
|
|
{
|
2006-04-21 08:40:06 +02:00
|
|
|
RpcConnection_np *npc = (RpcConnection_np *) Connection;
|
2006-04-21 08:38:57 +02:00
|
|
|
RPC_STATUS r;
|
|
|
|
LPSTR pname;
|
|
|
|
|
2006-04-21 08:39:41 +02:00
|
|
|
/* already connected? */
|
2006-04-21 08:40:06 +02:00
|
|
|
if (npc->pipe)
|
2006-04-21 08:39:41 +02:00
|
|
|
return RPC_S_OK;
|
|
|
|
|
2015-10-27 19:27:48 +01:00
|
|
|
pname = ncalrpc_pipe_name(Connection->Endpoint);
|
2006-11-08 21:45:19 +01:00
|
|
|
r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
|
|
|
|
I_RpcFree(pname);
|
2006-04-21 08:38:57 +02:00
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-03-29 19:42:35 +02:00
|
|
|
static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, const char *endpoint)
|
2006-11-08 21:45:19 +01:00
|
|
|
{
|
|
|
|
RPC_STATUS r;
|
|
|
|
RpcConnection *Connection;
|
2009-03-24 12:29:12 +01:00
|
|
|
char generated_endpoint[22];
|
|
|
|
|
|
|
|
if (!endpoint)
|
|
|
|
{
|
|
|
|
static LONG lrpc_nameless_id;
|
|
|
|
DWORD process_id = GetCurrentProcessId();
|
|
|
|
ULONG id = InterlockedIncrement(&lrpc_nameless_id);
|
|
|
|
snprintf(generated_endpoint, sizeof(generated_endpoint),
|
|
|
|
"LRPC%08x.%08x", process_id, id);
|
|
|
|
endpoint = generated_endpoint;
|
|
|
|
}
|
2006-11-08 21:45:19 +01:00
|
|
|
|
|
|
|
r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
|
2013-08-28 13:30:03 +02:00
|
|
|
endpoint, NULL, NULL, NULL, NULL);
|
2006-11-08 21:45:19 +01:00
|
|
|
if (r != RPC_S_OK)
|
|
|
|
return r;
|
|
|
|
|
2017-05-31 17:30:23 +02:00
|
|
|
((RpcConnection_np*)Connection)->listen_pipe = ncalrpc_pipe_name(Connection->Endpoint);
|
|
|
|
r = rpcrt4_conn_create_pipe(Connection);
|
2006-04-21 08:38:57 +02:00
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
EnterCriticalSection(&protseq->cs);
|
2017-05-23 17:39:14 +02:00
|
|
|
list_add_head(&protseq->listeners, &Connection->protseq_entry);
|
2017-05-23 17:39:30 +02:00
|
|
|
Connection->protseq = protseq;
|
2006-11-08 21:45:19 +01:00
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
|
2006-04-21 08:38:57 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-10-27 19:27:48 +01:00
|
|
|
static char *ncacn_pipe_name(const char *endpoint)
|
|
|
|
{
|
|
|
|
static const char prefix[] = "\\\\.";
|
|
|
|
char *pipe_name;
|
|
|
|
|
|
|
|
/* protseq=ncacn_np: named pipes */
|
|
|
|
pipe_name = I_RpcAllocate(sizeof(prefix) + strlen(endpoint));
|
|
|
|
strcat(strcpy(pipe_name, prefix), endpoint);
|
|
|
|
return pipe_name;
|
|
|
|
}
|
|
|
|
|
2006-04-21 08:38:57 +02:00
|
|
|
static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
|
|
|
|
{
|
2006-04-21 08:40:06 +02:00
|
|
|
RpcConnection_np *npc = (RpcConnection_np *) Connection;
|
2006-04-21 08:38:57 +02:00
|
|
|
RPC_STATUS r;
|
|
|
|
LPSTR pname;
|
|
|
|
|
2006-04-21 08:39:41 +02:00
|
|
|
/* already connected? */
|
2006-04-21 08:40:06 +02:00
|
|
|
if (npc->pipe)
|
2006-04-21 08:39:41 +02:00
|
|
|
return RPC_S_OK;
|
|
|
|
|
2015-10-27 19:27:48 +01:00
|
|
|
pname = ncacn_pipe_name(Connection->Endpoint);
|
2006-11-08 21:45:19 +01:00
|
|
|
r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
|
2006-10-03 15:40:48 +02:00
|
|
|
I_RpcFree(pname);
|
2006-04-21 08:38:57 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-03-29 19:42:35 +02:00
|
|
|
static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
|
2006-04-21 08:39:27 +02:00
|
|
|
{
|
2006-11-08 21:45:19 +01:00
|
|
|
RPC_STATUS r;
|
|
|
|
RpcConnection *Connection;
|
2017-01-21 16:32:22 +01:00
|
|
|
char generated_endpoint[26];
|
2009-03-24 12:29:12 +01:00
|
|
|
|
|
|
|
if (!endpoint)
|
|
|
|
{
|
|
|
|
static LONG np_nameless_id;
|
|
|
|
DWORD process_id = GetCurrentProcessId();
|
|
|
|
ULONG id = InterlockedExchangeAdd(&np_nameless_id, 1 );
|
|
|
|
snprintf(generated_endpoint, sizeof(generated_endpoint),
|
|
|
|
"\\\\pipe\\\\%08x.%03x", process_id, id);
|
|
|
|
endpoint = generated_endpoint;
|
|
|
|
}
|
2006-11-08 21:45:19 +01:00
|
|
|
|
|
|
|
r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
|
2013-08-28 13:30:03 +02:00
|
|
|
endpoint, NULL, NULL, NULL, NULL);
|
2006-11-08 21:45:19 +01:00
|
|
|
if (r != RPC_S_OK)
|
|
|
|
return r;
|
|
|
|
|
2017-05-31 17:30:23 +02:00
|
|
|
((RpcConnection_np*)Connection)->listen_pipe = ncacn_pipe_name(Connection->Endpoint);
|
|
|
|
r = rpcrt4_conn_create_pipe(Connection);
|
2007-02-12 14:13:22 +01:00
|
|
|
|
|
|
|
EnterCriticalSection(&protseq->cs);
|
2017-05-23 17:39:14 +02:00
|
|
|
list_add_head(&protseq->listeners, &Connection->protseq_entry);
|
2017-05-23 17:39:30 +02:00
|
|
|
Connection->protseq = protseq;
|
2007-02-12 14:13:22 +01:00
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
|
|
|
|
{
|
2017-05-22 16:04:31 +02:00
|
|
|
/* because of the way named pipes work, we'll transfer the connected pipe
|
|
|
|
* to the child, then reopen the server binding to continue listening */
|
|
|
|
|
|
|
|
new_npc->pipe = old_npc->pipe;
|
|
|
|
old_npc->pipe = 0;
|
|
|
|
assert(!old_npc->listen_event);
|
2006-11-08 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
|
|
|
|
{
|
2016-10-12 17:14:11 +02:00
|
|
|
DWORD len = MAX_COMPUTERNAME_LENGTH + 1;
|
2006-11-08 21:45:19 +01:00
|
|
|
RPC_STATUS status;
|
|
|
|
|
|
|
|
rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
|
2017-05-31 17:30:23 +02:00
|
|
|
status = rpcrt4_conn_create_pipe(old_conn);
|
2006-11-08 21:45:19 +01:00
|
|
|
|
2016-10-12 17:14:11 +02:00
|
|
|
/* Store the local computer name as the NetworkAddr for ncacn_np as long as
|
|
|
|
* we don't support named pipes over the network. */
|
|
|
|
new_conn->NetworkAddr = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
if (!GetComputerNameA(new_conn->NetworkAddr, &len))
|
|
|
|
{
|
|
|
|
ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-10-27 19:28:28 +01:00
|
|
|
static RPC_STATUS is_pipe_listening(const char *pipe_name)
|
|
|
|
{
|
|
|
|
return WaitNamedPipeA(pipe_name, 1) ? RPC_S_OK : RPC_S_NOT_LISTENING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_np_is_server_listening(const char *endpoint)
|
|
|
|
{
|
|
|
|
char *pipe_name;
|
|
|
|
RPC_STATUS status;
|
|
|
|
|
|
|
|
pipe_name = ncacn_pipe_name(endpoint);
|
|
|
|
status = is_pipe_listening(pipe_name);
|
|
|
|
I_RpcFree(pipe_name);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_np_is_server_listening(const char *endpoint)
|
|
|
|
{
|
|
|
|
char *pipe_name;
|
|
|
|
RPC_STATUS status;
|
|
|
|
|
|
|
|
pipe_name = ncalrpc_pipe_name(endpoint);
|
|
|
|
status = is_pipe_listening(pipe_name);
|
|
|
|
I_RpcFree(pipe_name);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
|
|
|
|
{
|
2016-10-12 17:14:11 +02:00
|
|
|
DWORD len = MAX_COMPUTERNAME_LENGTH + 1;
|
2006-11-08 21:45:19 +01:00
|
|
|
RPC_STATUS status;
|
|
|
|
|
|
|
|
TRACE("%s\n", old_conn->Endpoint);
|
|
|
|
|
|
|
|
rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
|
2017-05-31 17:30:23 +02:00
|
|
|
status = rpcrt4_conn_create_pipe(old_conn);
|
2016-10-12 17:14:11 +02:00
|
|
|
|
|
|
|
/* Store the local computer name as the NetworkAddr for ncalrpc. */
|
|
|
|
new_conn->NetworkAddr = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
if (!GetComputerNameA(new_conn->NetworkAddr, &len))
|
|
|
|
{
|
|
|
|
ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
return status;
|
2006-04-21 08:39:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:04:02 +02:00
|
|
|
static int rpcrt4_conn_np_read(RpcConnection *conn, void *buffer, unsigned int count)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2017-05-22 16:04:02 +02:00
|
|
|
RpcConnection_np *connection = (RpcConnection_np *) conn;
|
2017-05-22 16:04:15 +02:00
|
|
|
HANDLE event;
|
2017-05-22 16:04:02 +02:00
|
|
|
NTSTATUS status;
|
2007-01-22 22:50:24 +01:00
|
|
|
|
2017-05-22 16:04:43 +02:00
|
|
|
event = get_np_event(connection);
|
2017-05-22 16:04:15 +02:00
|
|
|
if (!event)
|
|
|
|
return -1;
|
|
|
|
|
2017-05-24 13:52:14 +02:00
|
|
|
if (connection->read_closed)
|
|
|
|
status = STATUS_CANCELLED;
|
|
|
|
else
|
|
|
|
status = NtReadFile(connection->pipe, event, NULL, NULL, &connection->io_status, buffer, count, NULL, NULL);
|
2017-05-22 16:04:15 +02:00
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
2017-05-24 13:52:14 +02:00
|
|
|
/* check read_closed again before waiting to avoid a race */
|
|
|
|
if (connection->read_closed)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK io_status;
|
|
|
|
NtCancelIoFileEx(connection->pipe, &connection->io_status, &io_status);
|
|
|
|
}
|
2017-05-22 16:04:15 +02:00
|
|
|
WaitForSingleObject(event, INFINITE);
|
2017-05-24 13:52:14 +02:00
|
|
|
status = connection->io_status.Status;
|
2017-05-22 16:04:15 +02:00
|
|
|
}
|
2017-05-22 16:04:43 +02:00
|
|
|
release_np_event(connection, event);
|
2017-05-24 13:52:14 +02:00
|
|
|
return status && status != STATUS_BUFFER_OVERFLOW ? -1 : connection->io_status.Information;
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:03:46 +02:00
|
|
|
static int rpcrt4_conn_np_write(RpcConnection *conn, const void *buffer, unsigned int count)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2017-05-22 16:03:46 +02:00
|
|
|
RpcConnection_np *connection = (RpcConnection_np *) conn;
|
|
|
|
IO_STATUS_BLOCK io_status;
|
2017-05-22 16:04:15 +02:00
|
|
|
HANDLE event;
|
2017-05-22 16:03:46 +02:00
|
|
|
NTSTATUS status;
|
2007-01-22 22:50:24 +01:00
|
|
|
|
2017-05-22 16:04:43 +02:00
|
|
|
event = get_np_event(connection);
|
2017-05-22 16:04:15 +02:00
|
|
|
if (!event)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
status = NtWriteFile(connection->pipe, event, NULL, NULL, &io_status, buffer, count, NULL, NULL);
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(event, INFINITE);
|
|
|
|
status = io_status.Status;
|
|
|
|
}
|
2017-05-22 16:04:43 +02:00
|
|
|
release_np_event(connection, event);
|
2017-05-22 16:03:46 +02:00
|
|
|
if (status)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
assert(io_status.Information == count);
|
|
|
|
return count;
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:04:31 +02:00
|
|
|
static int rpcrt4_conn_np_close(RpcConnection *conn)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2017-05-22 16:04:31 +02:00
|
|
|
RpcConnection_np *connection = (RpcConnection_np *) conn;
|
|
|
|
if (connection->pipe)
|
|
|
|
{
|
|
|
|
FlushFileBuffers(connection->pipe);
|
|
|
|
CloseHandle(connection->pipe);
|
|
|
|
connection->pipe = 0;
|
|
|
|
}
|
|
|
|
if (connection->listen_event)
|
|
|
|
{
|
|
|
|
CloseHandle(connection->listen_event);
|
|
|
|
connection->listen_event = 0;
|
|
|
|
}
|
2017-05-22 16:04:43 +02:00
|
|
|
if (connection->event_cache)
|
|
|
|
{
|
|
|
|
CloseHandle(connection->event_cache);
|
|
|
|
connection->event_cache = 0;
|
|
|
|
}
|
2017-05-22 16:04:31 +02:00
|
|
|
return 0;
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 13:52:07 +02:00
|
|
|
static void rpcrt4_conn_np_close_read(RpcConnection *conn)
|
|
|
|
{
|
2017-05-24 13:52:14 +02:00
|
|
|
RpcConnection_np *connection = (RpcConnection_np*)conn;
|
|
|
|
IO_STATUS_BLOCK io_status;
|
|
|
|
|
|
|
|
connection->read_closed = TRUE;
|
|
|
|
NtCancelIoFileEx(connection->pipe, &connection->io_status, &io_status);
|
2017-05-24 13:52:07 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:04:50 +02:00
|
|
|
static void rpcrt4_conn_np_cancel_call(RpcConnection *conn)
|
2007-11-12 21:10:19 +01:00
|
|
|
{
|
2017-05-22 16:04:50 +02:00
|
|
|
RpcConnection_np *connection = (RpcConnection_np *)conn;
|
|
|
|
CancelIoEx(connection->pipe, NULL);
|
2007-11-12 21:10:19 +01:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:04:54 +02:00
|
|
|
static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *conn)
|
2008-01-21 11:31:40 +01:00
|
|
|
{
|
2019-09-25 09:25:20 +02:00
|
|
|
return rpcrt4_conn_np_read(conn, NULL, 0);
|
2008-01-21 11:31:40 +01:00
|
|
|
}
|
|
|
|
|
2006-06-01 21:25:06 +02:00
|
|
|
static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
|
|
|
|
const char *networkaddr,
|
|
|
|
const char *endpoint)
|
|
|
|
{
|
|
|
|
twr_empty_floor_t *smb_floor;
|
|
|
|
twr_empty_floor_t *nb_floor;
|
|
|
|
size_t size;
|
|
|
|
size_t networkaddr_size;
|
|
|
|
size_t endpoint_size;
|
|
|
|
|
|
|
|
TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
|
|
|
|
|
2008-01-15 13:09:52 +01:00
|
|
|
networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
|
|
|
|
endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
|
2006-06-01 21:25:06 +02:00
|
|
|
size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
|
|
|
|
|
|
|
|
if (!tower_data)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
smb_floor = (twr_empty_floor_t *)tower_data;
|
|
|
|
|
|
|
|
tower_data += sizeof(*smb_floor);
|
|
|
|
|
|
|
|
smb_floor->count_lhs = sizeof(smb_floor->protid);
|
|
|
|
smb_floor->protid = EPM_PROTOCOL_SMB;
|
|
|
|
smb_floor->count_rhs = endpoint_size;
|
|
|
|
|
2008-01-15 13:09:52 +01:00
|
|
|
if (endpoint)
|
|
|
|
memcpy(tower_data, endpoint, endpoint_size);
|
|
|
|
else
|
|
|
|
tower_data[0] = 0;
|
2006-06-01 21:25:06 +02:00
|
|
|
tower_data += endpoint_size;
|
|
|
|
|
|
|
|
nb_floor = (twr_empty_floor_t *)tower_data;
|
|
|
|
|
|
|
|
tower_data += sizeof(*nb_floor);
|
|
|
|
|
|
|
|
nb_floor->count_lhs = sizeof(nb_floor->protid);
|
|
|
|
nb_floor->protid = EPM_PROTOCOL_NETBIOS;
|
|
|
|
nb_floor->count_rhs = networkaddr_size;
|
|
|
|
|
2008-01-15 13:09:52 +01:00
|
|
|
if (networkaddr)
|
|
|
|
memcpy(tower_data, networkaddr, networkaddr_size);
|
|
|
|
else
|
|
|
|
tower_data[0] = 0;
|
2006-06-01 21:25:06 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
|
|
|
|
size_t tower_size,
|
|
|
|
char **networkaddr,
|
|
|
|
char **endpoint)
|
|
|
|
{
|
|
|
|
const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
|
|
|
|
const twr_empty_floor_t *nb_floor;
|
|
|
|
|
2006-06-13 14:11:28 +02:00
|
|
|
TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
|
2006-06-01 21:25:06 +02:00
|
|
|
|
|
|
|
if (tower_size < sizeof(*smb_floor))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
tower_data += sizeof(*smb_floor);
|
|
|
|
tower_size -= sizeof(*smb_floor);
|
|
|
|
|
|
|
|
if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
|
|
|
|
(smb_floor->protid != EPM_PROTOCOL_SMB) ||
|
2008-08-10 19:24:35 +02:00
|
|
|
(smb_floor->count_rhs > tower_size) ||
|
|
|
|
(tower_data[smb_floor->count_rhs - 1] != '\0'))
|
2006-06-01 21:25:06 +02:00
|
|
|
return EPT_S_NOT_REGISTERED;
|
2006-06-01 21:25:26 +02:00
|
|
|
|
|
|
|
if (endpoint)
|
|
|
|
{
|
2006-10-03 15:40:48 +02:00
|
|
|
*endpoint = I_RpcAllocate(smb_floor->count_rhs);
|
2006-06-01 21:25:26 +02:00
|
|
|
if (!*endpoint)
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
memcpy(*endpoint, tower_data, smb_floor->count_rhs);
|
|
|
|
}
|
2006-06-01 21:25:06 +02:00
|
|
|
tower_data += smb_floor->count_rhs;
|
|
|
|
tower_size -= smb_floor->count_rhs;
|
|
|
|
|
|
|
|
if (tower_size < sizeof(*nb_floor))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
nb_floor = (const twr_empty_floor_t *)tower_data;
|
|
|
|
|
|
|
|
tower_data += sizeof(*nb_floor);
|
|
|
|
tower_size -= sizeof(*nb_floor);
|
|
|
|
|
|
|
|
if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
|
|
|
|
(nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
|
2008-08-10 19:24:35 +02:00
|
|
|
(nb_floor->count_rhs > tower_size) ||
|
|
|
|
(tower_data[nb_floor->count_rhs - 1] != '\0'))
|
2006-06-01 21:25:06 +02:00
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
2006-06-01 21:25:26 +02:00
|
|
|
if (networkaddr)
|
2006-06-01 21:25:06 +02:00
|
|
|
{
|
2006-10-03 15:40:48 +02:00
|
|
|
*networkaddr = I_RpcAllocate(nb_floor->count_rhs);
|
2006-06-01 21:25:26 +02:00
|
|
|
if (!*networkaddr)
|
|
|
|
{
|
|
|
|
if (endpoint)
|
|
|
|
{
|
2006-10-03 15:40:48 +02:00
|
|
|
I_RpcFree(*endpoint);
|
2006-06-01 21:25:26 +02:00
|
|
|
*endpoint = NULL;
|
|
|
|
}
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
|
2006-06-01 21:25:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-13 22:36:01 +01:00
|
|
|
static RPC_STATUS rpcrt4_conn_np_impersonate_client(RpcConnection *conn)
|
|
|
|
{
|
|
|
|
RpcConnection_np *npc = (RpcConnection_np *)conn;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", conn);
|
|
|
|
|
|
|
|
if (conn->AuthInfo && SecIsValidHandle(&conn->ctx))
|
|
|
|
return RPCRT4_default_impersonate_client(conn);
|
|
|
|
|
|
|
|
ret = ImpersonateNamedPipeClient(npc->pipe);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
WARN("ImpersonateNamedPipeClient failed with error %u\n", error);
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case ERROR_CANNOT_IMPERSONATE:
|
|
|
|
return RPC_S_NO_CONTEXT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_conn_np_revert_to_self(RpcConnection *conn)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", conn);
|
|
|
|
|
|
|
|
if (conn->AuthInfo && SecIsValidHandle(&conn->ctx))
|
|
|
|
return RPCRT4_default_revert_to_self(conn);
|
|
|
|
|
|
|
|
ret = RevertToSelf();
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
WARN("RevertToSelf failed with error %u\n", GetLastError());
|
|
|
|
return RPC_S_NO_CONTEXT_AVAILABLE;
|
|
|
|
}
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2006-10-25 14:28:56 +02:00
|
|
|
typedef struct _RpcServerProtseq_np
|
|
|
|
{
|
|
|
|
RpcServerProtseq common;
|
|
|
|
HANDLE mgr_event;
|
|
|
|
} RpcServerProtseq_np;
|
|
|
|
|
|
|
|
static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
|
|
|
|
{
|
2017-05-24 13:52:24 +02:00
|
|
|
RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ps));
|
2006-10-25 14:28:56 +02:00
|
|
|
if (ps)
|
|
|
|
ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
return &ps->common;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
|
|
|
|
{
|
|
|
|
RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
|
|
|
|
SetEvent(npps->mgr_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
|
|
|
|
{
|
|
|
|
HANDLE *objs = prev_array;
|
2006-10-31 20:00:45 +01:00
|
|
|
RpcConnection_np *conn;
|
2006-10-25 14:28:56 +02:00
|
|
|
RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
|
|
|
|
|
|
|
|
EnterCriticalSection(&protseq->cs);
|
|
|
|
|
|
|
|
/* open and count connections */
|
|
|
|
*count = 1;
|
2017-05-23 17:39:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY(conn, &protseq->listeners, RpcConnection_np, common.protseq_entry)
|
|
|
|
{
|
2017-05-31 17:30:29 +02:00
|
|
|
if (!conn->pipe && rpcrt4_conn_create_pipe(&conn->common) != RPC_S_OK)
|
|
|
|
continue;
|
2017-05-22 16:04:31 +02:00
|
|
|
if (!conn->listen_event)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE event;
|
|
|
|
|
2017-05-22 16:04:43 +02:00
|
|
|
event = get_np_event(conn);
|
2017-05-22 16:04:31 +02:00
|
|
|
if (!event)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
status = NtFsControlFile(conn->pipe, event, NULL, NULL, &conn->io_status, FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_SUCCESS:
|
|
|
|
case STATUS_PIPE_CONNECTED:
|
|
|
|
conn->io_status.Status = status;
|
|
|
|
SetEvent(event);
|
|
|
|
break;
|
|
|
|
case STATUS_PENDING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR("pipe listen error %x\n", status);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->listen_event = event;
|
|
|
|
}
|
|
|
|
(*count)++;
|
2006-10-25 14:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make array of connections */
|
|
|
|
if (objs)
|
|
|
|
objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
|
|
|
|
else
|
|
|
|
objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
|
|
|
|
if (!objs)
|
|
|
|
{
|
|
|
|
ERR("couldn't allocate objs\n");
|
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
objs[0] = npps->mgr_event;
|
|
|
|
*count = 1;
|
2017-05-23 17:39:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY(conn, &protseq->listeners, RpcConnection_np, common.protseq_entry)
|
|
|
|
{
|
2017-05-22 16:04:31 +02:00
|
|
|
if (conn->listen_event)
|
|
|
|
objs[(*count)++] = conn->listen_event;
|
2006-10-25 14:28:56 +02:00
|
|
|
}
|
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, array);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
|
|
|
|
{
|
|
|
|
HANDLE b_handle;
|
|
|
|
HANDLE *objs = wait_array;
|
|
|
|
DWORD res;
|
2017-05-16 17:48:01 +02:00
|
|
|
RpcConnection *cconn = NULL;
|
2006-10-31 20:00:45 +01:00
|
|
|
RpcConnection_np *conn;
|
2006-10-25 14:28:56 +02:00
|
|
|
|
|
|
|
if (!objs)
|
|
|
|
return -1;
|
2007-12-06 16:08:11 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* an alertable wait isn't strictly necessary, but due to our
|
|
|
|
* overlapped I/O implementation in Wine we need to free some memory
|
|
|
|
* by the file user APC being called, even if no completion routine was
|
|
|
|
* specified at the time of starting the async operation */
|
|
|
|
res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
|
|
|
|
} while (res == WAIT_IO_COMPLETION);
|
|
|
|
|
2006-10-25 14:28:56 +02:00
|
|
|
if (res == WAIT_OBJECT_0)
|
|
|
|
return 0;
|
|
|
|
else if (res == WAIT_FAILED)
|
|
|
|
{
|
2006-11-09 23:04:48 +01:00
|
|
|
ERR("wait failed with error %d\n", GetLastError());
|
2006-10-25 14:28:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b_handle = objs[res - WAIT_OBJECT_0];
|
|
|
|
/* find which connection got a RPC */
|
|
|
|
EnterCriticalSection(&protseq->cs);
|
2017-05-23 17:39:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY(conn, &protseq->listeners, RpcConnection_np, common.protseq_entry)
|
2017-05-22 16:04:31 +02:00
|
|
|
{
|
|
|
|
if (b_handle == conn->listen_event)
|
|
|
|
{
|
2017-05-22 16:04:43 +02:00
|
|
|
release_np_event(conn, conn->listen_event);
|
2017-05-22 16:04:31 +02:00
|
|
|
conn->listen_event = NULL;
|
|
|
|
if (conn->io_status.Status == STATUS_SUCCESS || conn->io_status.Status == STATUS_PIPE_CONNECTED)
|
2017-05-23 17:39:39 +02:00
|
|
|
cconn = rpcrt4_spawn_connection(&conn->common);
|
2017-05-22 16:04:31 +02:00
|
|
|
else
|
|
|
|
ERR("listen failed %x\n", conn->io_status.Status);
|
2017-05-16 17:48:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-09-19 13:01:36 +02:00
|
|
|
}
|
2006-10-25 14:28:56 +02:00
|
|
|
LeaveCriticalSection(&protseq->cs);
|
2017-05-16 17:48:01 +02:00
|
|
|
if (!cconn)
|
2006-10-25 14:28:56 +02:00
|
|
|
{
|
2017-05-16 17:48:01 +02:00
|
|
|
ERR("failed to locate connection for handle %p\n", b_handle);
|
|
|
|
return -1;
|
2006-10-25 14:28:56 +02:00
|
|
|
}
|
2017-05-16 17:48:01 +02:00
|
|
|
RPCRT4_new_client(cconn);
|
|
|
|
return 1;
|
2006-10-25 14:28:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-01 21:25:06 +02:00
|
|
|
static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
|
|
|
|
const char *networkaddr,
|
|
|
|
const char *endpoint)
|
|
|
|
{
|
|
|
|
twr_empty_floor_t *pipe_floor;
|
|
|
|
size_t size;
|
|
|
|
size_t endpoint_size;
|
|
|
|
|
|
|
|
TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
|
|
|
|
|
2008-08-10 19:23:31 +02:00
|
|
|
endpoint_size = strlen(endpoint) + 1;
|
2006-06-01 21:25:06 +02:00
|
|
|
size = sizeof(*pipe_floor) + endpoint_size;
|
|
|
|
|
|
|
|
if (!tower_data)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
pipe_floor = (twr_empty_floor_t *)tower_data;
|
|
|
|
|
|
|
|
tower_data += sizeof(*pipe_floor);
|
|
|
|
|
|
|
|
pipe_floor->count_lhs = sizeof(pipe_floor->protid);
|
2008-08-10 19:23:50 +02:00
|
|
|
pipe_floor->protid = EPM_PROTOCOL_PIPE;
|
2006-06-01 21:25:06 +02:00
|
|
|
pipe_floor->count_rhs = endpoint_size;
|
|
|
|
|
|
|
|
memcpy(tower_data, endpoint, endpoint_size);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
|
|
|
|
size_t tower_size,
|
|
|
|
char **networkaddr,
|
|
|
|
char **endpoint)
|
|
|
|
{
|
|
|
|
const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
|
|
|
|
|
2006-06-13 14:11:28 +02:00
|
|
|
TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
|
2006-06-01 21:25:06 +02:00
|
|
|
|
|
|
|
if (tower_size < sizeof(*pipe_floor))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
tower_data += sizeof(*pipe_floor);
|
|
|
|
tower_size -= sizeof(*pipe_floor);
|
|
|
|
|
|
|
|
if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
|
2008-08-10 19:23:50 +02:00
|
|
|
(pipe_floor->protid != EPM_PROTOCOL_PIPE) ||
|
2008-08-10 19:24:35 +02:00
|
|
|
(pipe_floor->count_rhs > tower_size) ||
|
|
|
|
(tower_data[pipe_floor->count_rhs - 1] != '\0'))
|
2006-06-01 21:25:06 +02:00
|
|
|
return EPT_S_NOT_REGISTERED;
|
2006-06-01 21:25:26 +02:00
|
|
|
|
2008-08-10 19:23:40 +02:00
|
|
|
if (networkaddr)
|
|
|
|
*networkaddr = NULL;
|
|
|
|
|
2006-06-01 21:25:26 +02:00
|
|
|
if (endpoint)
|
|
|
|
{
|
2006-10-03 15:40:48 +02:00
|
|
|
*endpoint = I_RpcAllocate(pipe_floor->count_rhs);
|
2006-06-01 21:25:26 +02:00
|
|
|
if (!*endpoint)
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
|
|
|
|
}
|
2006-06-01 21:25:06 +02:00
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-13 22:35:54 +01:00
|
|
|
static BOOL rpcrt4_ncalrpc_is_authorized(RpcConnection *conn)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_authorize(RpcConnection *conn, BOOL first_time,
|
|
|
|
unsigned char *in_buffer,
|
|
|
|
unsigned int in_size,
|
|
|
|
unsigned char *out_buffer,
|
|
|
|
unsigned int *out_size)
|
|
|
|
{
|
|
|
|
/* since this protocol is local to the machine there is no need to
|
|
|
|
* authenticate the caller */
|
|
|
|
*out_size = 0;
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_secure_packet(RpcConnection *conn,
|
|
|
|
enum secure_packet_direction dir,
|
|
|
|
RpcPktHdr *hdr, unsigned int hdr_size,
|
|
|
|
unsigned char *stub_data, unsigned int stub_data_size,
|
|
|
|
RpcAuthVerifier *auth_hdr,
|
|
|
|
unsigned char *auth_value, unsigned int auth_value_size)
|
|
|
|
{
|
|
|
|
/* since this protocol is local to the machine there is no need to secure
|
|
|
|
* the packet */
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-13 22:36:09 +01:00
|
|
|
static RPC_STATUS rpcrt4_ncalrpc_inquire_auth_client(
|
|
|
|
RpcConnection *conn, RPC_AUTHZ_HANDLE *privs, RPC_WSTR *server_princ_name,
|
|
|
|
ULONG *authn_level, ULONG *authn_svc, ULONG *authz_svc, ULONG flags)
|
|
|
|
{
|
|
|
|
TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn, privs,
|
|
|
|
server_princ_name, authn_level, authn_svc, authz_svc, flags);
|
|
|
|
|
|
|
|
if (privs)
|
|
|
|
{
|
|
|
|
FIXME("privs not implemented\n");
|
|
|
|
*privs = NULL;
|
|
|
|
}
|
|
|
|
if (server_princ_name)
|
|
|
|
{
|
|
|
|
FIXME("server_princ_name not implemented\n");
|
|
|
|
*server_princ_name = NULL;
|
|
|
|
}
|
|
|
|
if (authn_level) *authn_level = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
|
|
|
|
if (authn_svc) *authn_svc = RPC_C_AUTHN_WINNT;
|
|
|
|
if (authz_svc)
|
|
|
|
{
|
|
|
|
FIXME("authorization service not implemented\n");
|
|
|
|
*authz_svc = RPC_C_AUTHZ_NONE;
|
|
|
|
}
|
|
|
|
if (flags)
|
|
|
|
FIXME("flags 0x%x not implemented\n", flags);
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2006-04-21 11:20:16 +02:00
|
|
|
/**** ncacn_ip_tcp support ****/
|
|
|
|
|
2009-04-07 13:28:42 +02:00
|
|
|
static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data,
|
|
|
|
const char *networkaddr,
|
|
|
|
unsigned char tcp_protid,
|
|
|
|
const char *endpoint)
|
|
|
|
{
|
|
|
|
twr_tcp_floor_t *tcp_floor;
|
|
|
|
twr_ipv4_floor_t *ipv4_floor;
|
|
|
|
struct addrinfo *ai;
|
|
|
|
struct addrinfo hints;
|
|
|
|
int ret;
|
|
|
|
size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
|
|
|
|
|
|
|
|
TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
|
|
|
|
|
|
|
|
if (!tower_data)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
tcp_floor = (twr_tcp_floor_t *)tower_data;
|
|
|
|
tower_data += sizeof(*tcp_floor);
|
|
|
|
|
|
|
|
ipv4_floor = (twr_ipv4_floor_t *)tower_data;
|
|
|
|
|
|
|
|
tcp_floor->count_lhs = sizeof(tcp_floor->protid);
|
|
|
|
tcp_floor->protid = tcp_protid;
|
|
|
|
tcp_floor->count_rhs = sizeof(tcp_floor->port);
|
|
|
|
|
|
|
|
ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
|
|
|
|
ipv4_floor->protid = EPM_PROTOCOL_IP;
|
|
|
|
ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
|
|
|
|
|
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
|
|
|
/* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
|
|
|
|
hints.ai_family = PF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_addrlen = 0;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
|
|
|
ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ai->ai_family == PF_INET)
|
|
|
|
{
|
|
|
|
const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
|
|
|
|
tcp_floor->port = sin->sin_port;
|
|
|
|
ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("unexpected protocol family %d\n", ai->ai_family);
|
2014-07-15 10:00:43 +02:00
|
|
|
freeaddrinfo(ai);
|
2009-04-07 13:28:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
|
|
|
|
size_t tower_size,
|
|
|
|
char **networkaddr,
|
|
|
|
unsigned char tcp_protid,
|
|
|
|
char **endpoint)
|
|
|
|
{
|
|
|
|
const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
|
|
|
|
const twr_ipv4_floor_t *ipv4_floor;
|
|
|
|
struct in_addr in_addr;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
|
|
|
|
|
|
|
|
if (tower_size < sizeof(*tcp_floor))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
tower_data += sizeof(*tcp_floor);
|
|
|
|
tower_size -= sizeof(*tcp_floor);
|
|
|
|
|
|
|
|
if (tower_size < sizeof(*ipv4_floor))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
|
|
|
|
|
|
|
|
if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
|
|
|
|
(tcp_floor->protid != tcp_protid) ||
|
|
|
|
(tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
|
|
|
|
(ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
|
|
|
|
(ipv4_floor->protid != EPM_PROTOCOL_IP) ||
|
|
|
|
(ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
if (endpoint)
|
|
|
|
{
|
|
|
|
*endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
|
|
|
|
if (!*endpoint)
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (networkaddr)
|
|
|
|
{
|
|
|
|
*networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
|
|
|
|
if (!*networkaddr)
|
|
|
|
{
|
|
|
|
if (endpoint)
|
|
|
|
{
|
|
|
|
I_RpcFree(*endpoint);
|
|
|
|
*endpoint = NULL;
|
|
|
|
}
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
in_addr.s_addr = ipv4_floor->ipv4addr;
|
|
|
|
if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
|
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
ERR("inet_ntop: %u\n", WSAGetLastError());
|
2009-04-07 13:28:42 +02:00
|
|
|
I_RpcFree(*networkaddr);
|
|
|
|
*networkaddr = NULL;
|
|
|
|
if (endpoint)
|
|
|
|
{
|
|
|
|
I_RpcFree(*endpoint);
|
|
|
|
*endpoint = NULL;
|
|
|
|
}
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2006-04-21 09:07:35 +02:00
|
|
|
typedef struct _RpcConnection_tcp
|
|
|
|
{
|
|
|
|
RpcConnection common;
|
2006-04-21 11:20:16 +02:00
|
|
|
int sock;
|
2009-04-08 13:55:38 +02:00
|
|
|
HANDLE sock_event;
|
|
|
|
HANDLE cancel_event;
|
2006-04-21 09:07:35 +02:00
|
|
|
} RpcConnection_tcp;
|
|
|
|
|
2009-04-08 13:54:43 +02:00
|
|
|
static BOOL rpcrt4_sock_wait_init(RpcConnection_tcp *tcpc)
|
|
|
|
{
|
2009-04-08 13:55:38 +02:00
|
|
|
static BOOL wsa_inited;
|
|
|
|
if (!wsa_inited)
|
|
|
|
{
|
|
|
|
WSADATA wsadata;
|
|
|
|
WSAStartup(MAKEWORD(2, 2), &wsadata);
|
|
|
|
/* Note: WSAStartup can be called more than once so we don't bother with
|
|
|
|
* making accesses to wsa_inited thread-safe */
|
|
|
|
wsa_inited = TRUE;
|
|
|
|
}
|
|
|
|
tcpc->sock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
tcpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!tcpc->sock_event || !tcpc->cancel_event)
|
|
|
|
{
|
|
|
|
ERR("event creation failed\n");
|
|
|
|
if (tcpc->sock_event) CloseHandle(tcpc->sock_event);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2009-04-08 13:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL rpcrt4_sock_wait_for_recv(RpcConnection_tcp *tcpc)
|
|
|
|
{
|
2009-04-08 13:55:38 +02:00
|
|
|
HANDLE wait_handles[2];
|
|
|
|
DWORD res;
|
|
|
|
if (WSAEventSelect(tcpc->sock, tcpc->sock_event, FD_READ | FD_CLOSE) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
wait_handles[0] = tcpc->sock_event;
|
|
|
|
wait_handles[1] = tcpc->cancel_event;
|
|
|
|
res = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return TRUE;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
return FALSE;
|
|
|
|
default:
|
|
|
|
ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-08 13:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL rpcrt4_sock_wait_for_send(RpcConnection_tcp *tcpc)
|
|
|
|
{
|
2009-04-08 13:55:38 +02:00
|
|
|
DWORD res;
|
|
|
|
if (WSAEventSelect(tcpc->sock, tcpc->sock_event, FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
res = WaitForSingleObject(tcpc->sock_event, INFINITE);
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-08 13:54:43 +02:00
|
|
|
}
|
|
|
|
|
2006-04-21 09:07:35 +02:00
|
|
|
static RpcConnection *rpcrt4_conn_tcp_alloc(void)
|
|
|
|
{
|
2006-04-21 11:38:01 +02:00
|
|
|
RpcConnection_tcp *tcpc;
|
2017-05-23 17:39:20 +02:00
|
|
|
tcpc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcConnection_tcp));
|
2006-10-18 18:14:11 +02:00
|
|
|
if (tcpc == NULL)
|
|
|
|
return NULL;
|
|
|
|
tcpc->sock = -1;
|
2009-04-08 13:54:43 +02:00
|
|
|
if (!rpcrt4_sock_wait_init(tcpc))
|
2007-11-12 21:10:19 +01:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, tcpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-21 11:38:01 +02:00
|
|
|
return &tcpc->common;
|
2006-04-21 09:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
|
|
|
|
{
|
2006-04-21 11:20:16 +02:00
|
|
|
RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
|
|
|
|
int sock;
|
2006-05-01 11:37:32 +02:00
|
|
|
int ret;
|
|
|
|
struct addrinfo *ai;
|
|
|
|
struct addrinfo *ai_cur;
|
|
|
|
struct addrinfo hints;
|
2006-04-21 11:20:16 +02:00
|
|
|
|
|
|
|
TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
|
|
|
|
|
2006-04-21 11:38:01 +02:00
|
|
|
if (tcpc->sock != -1)
|
2006-04-21 11:20:16 +02:00
|
|
|
return RPC_S_OK;
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
hints.ai_flags = 0;
|
2006-05-01 11:37:32 +02:00
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_addrlen = 0;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
|
|
|
ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
|
2006-06-13 11:59:42 +02:00
|
|
|
if (ret)
|
2006-04-21 11:20:16 +02:00
|
|
|
{
|
2006-10-31 20:03:20 +01:00
|
|
|
ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
|
|
|
|
Connection->Endpoint, gai_strerror(ret));
|
2006-05-01 11:37:32 +02:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
2006-04-21 11:20:16 +02:00
|
|
|
}
|
|
|
|
|
2006-10-31 20:01:33 +01:00
|
|
|
for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
|
2006-04-21 11:20:16 +02:00
|
|
|
{
|
2007-02-27 19:14:03 +01:00
|
|
|
int val;
|
2009-04-08 13:54:43 +02:00
|
|
|
u_long nonblocking;
|
2007-02-27 19:14:03 +01:00
|
|
|
|
2009-03-26 14:35:57 +01:00
|
|
|
if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
|
|
|
|
{
|
|
|
|
TRACE("skipping non-IP/IPv6 address family\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-05-01 11:37:32 +02:00
|
|
|
if (TRACE_ON(rpc))
|
|
|
|
{
|
|
|
|
char host[256];
|
|
|
|
char service[256];
|
|
|
|
getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
|
|
|
|
host, sizeof(host), service, sizeof(service),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
TRACE("trying %s:%s\n", host, service);
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
|
2007-11-26 23:09:28 +01:00
|
|
|
if (sock == -1)
|
2006-05-01 11:37:32 +02:00
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("socket() failed: %u\n", WSAGetLastError());
|
2006-05-01 11:37:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-04-21 11:20:16 +02:00
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
|
2006-05-01 11:37:32 +02:00
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("connect() failed: %u\n", WSAGetLastError());
|
2008-03-06 15:59:02 +01:00
|
|
|
closesocket(sock);
|
2006-11-08 21:45:19 +01:00
|
|
|
continue;
|
2006-05-01 11:37:32 +02:00
|
|
|
}
|
2007-02-27 19:14:03 +01:00
|
|
|
|
|
|
|
/* RPC depends on having minimal latency so disable the Nagle algorithm */
|
|
|
|
val = 1;
|
2017-05-16 17:47:50 +02:00
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
|
2009-04-08 13:54:43 +02:00
|
|
|
nonblocking = 1;
|
|
|
|
ioctlsocket(sock, FIONBIO, &nonblocking);
|
2007-02-27 19:14:03 +01:00
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
tcpc->sock = sock;
|
2006-05-01 11:37:32 +02:00
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
TRACE("connected\n");
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
2006-04-21 11:20:16 +02:00
|
|
|
|
2006-05-01 11:37:32 +02:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
|
2006-04-21 09:07:35 +02:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2009-03-29 19:42:35 +02:00
|
|
|
static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
|
2006-11-08 21:45:19 +01:00
|
|
|
{
|
2006-11-08 21:45:55 +01:00
|
|
|
RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
|
2006-11-08 21:45:19 +01:00
|
|
|
int sock;
|
|
|
|
int ret;
|
|
|
|
struct addrinfo *ai;
|
|
|
|
struct addrinfo *ai_cur;
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
TRACE("(%p, %s)\n", protseq, endpoint);
|
|
|
|
|
|
|
|
hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_addrlen = 0;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
2009-03-26 14:35:43 +01:00
|
|
|
ret = getaddrinfo(NULL, endpoint ? endpoint : "0", &hints, &ai);
|
2006-11-08 21:45:19 +01:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ERR("getaddrinfo for port %s failed: %s\n", endpoint,
|
|
|
|
gai_strerror(ret));
|
2006-11-08 21:45:55 +01:00
|
|
|
if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
|
|
|
|
return RPC_S_INVALID_ENDPOINT_FORMAT;
|
|
|
|
return RPC_S_CANT_CREATE_ENDPOINT;
|
2006-11-08 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
|
|
|
|
{
|
|
|
|
RpcConnection_tcp *tcpc;
|
2006-11-08 21:47:08 +01:00
|
|
|
RPC_STATUS create_status;
|
2009-03-26 14:35:43 +01:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t sa_len;
|
|
|
|
char service[NI_MAXSERV];
|
2009-04-08 13:54:43 +02:00
|
|
|
u_long nonblocking;
|
2006-11-08 21:47:08 +01:00
|
|
|
|
2009-03-26 14:35:57 +01:00
|
|
|
if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
|
|
|
|
{
|
|
|
|
TRACE("skipping non-IP/IPv6 address family\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
if (TRACE_ON(rpc))
|
|
|
|
{
|
|
|
|
char host[256];
|
|
|
|
getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
|
|
|
|
host, sizeof(host), service, sizeof(service),
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
TRACE("trying %s:%s\n", host, service);
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
|
2007-11-26 23:09:28 +01:00
|
|
|
if (sock == -1)
|
2006-11-08 21:45:19 +01:00
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("socket() failed: %u\n", WSAGetLastError());
|
2006-11-08 21:45:55 +01:00
|
|
|
status = RPC_S_CANT_CREATE_ENDPOINT;
|
2006-11-08 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("bind failed: %u\n", WSAGetLastError());
|
2008-03-06 15:59:02 +01:00
|
|
|
closesocket(sock);
|
2017-05-16 17:47:50 +02:00
|
|
|
if (WSAGetLastError() == WSAEADDRINUSE)
|
2006-11-08 21:45:55 +01:00
|
|
|
status = RPC_S_DUPLICATE_ENDPOINT;
|
|
|
|
else
|
|
|
|
status = RPC_S_CANT_CREATE_ENDPOINT;
|
2006-11-08 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-03-26 14:35:43 +01:00
|
|
|
|
|
|
|
sa_len = sizeof(sa);
|
|
|
|
if (getsockname(sock, (struct sockaddr *)&sa, &sa_len))
|
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("getsockname() failed: %u\n", WSAGetLastError());
|
2012-10-31 09:43:46 +01:00
|
|
|
closesocket(sock);
|
2009-03-26 14:35:43 +01:00
|
|
|
status = RPC_S_CANT_CREATE_ENDPOINT;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = getnameinfo((struct sockaddr *)&sa, sa_len,
|
|
|
|
NULL, 0, service, sizeof(service),
|
|
|
|
NI_NUMERICSERV);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
WARN("getnameinfo failed: %s\n", gai_strerror(ret));
|
2012-10-31 09:43:46 +01:00
|
|
|
closesocket(sock);
|
2009-03-26 14:35:43 +01:00
|
|
|
status = RPC_S_CANT_CREATE_ENDPOINT;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-11-08 21:47:08 +01:00
|
|
|
create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
|
|
|
|
protseq->Protseq, NULL,
|
2013-08-28 13:30:03 +02:00
|
|
|
service, NULL, NULL, NULL, NULL);
|
2006-11-08 21:47:08 +01:00
|
|
|
if (create_status != RPC_S_OK)
|
2006-11-08 21:45:19 +01:00
|
|
|
{
|
2008-03-06 15:59:02 +01:00
|
|
|
closesocket(sock);
|
2006-11-08 21:47:08 +01:00
|
|
|
status = create_status;
|
2006-11-08 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-11-08 21:47:08 +01:00
|
|
|
tcpc->sock = sock;
|
2006-11-08 21:46:32 +01:00
|
|
|
ret = listen(sock, protseq->MaxCalls);
|
2006-11-08 21:45:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("listen failed: %u\n", WSAGetLastError());
|
2012-02-14 16:19:32 +01:00
|
|
|
RPCRT4_ReleaseConnection(&tcpc->common);
|
2006-11-08 21:45:55 +01:00
|
|
|
status = RPC_S_OUT_OF_RESOURCES;
|
2006-11-08 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* need a non-blocking socket, otherwise accept() has a potential
|
|
|
|
* race-condition (poll() says it is readable, connection drops,
|
|
|
|
* and accept() blocks until the next connection comes...)
|
|
|
|
*/
|
2009-04-08 13:54:43 +02:00
|
|
|
nonblocking = 1;
|
|
|
|
ret = ioctlsocket(sock, FIONBIO, &nonblocking);
|
2006-11-08 21:45:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
WARN("couldn't make socket non-blocking, error %d\n", ret);
|
2012-02-14 16:19:32 +01:00
|
|
|
RPCRT4_ReleaseConnection(&tcpc->common);
|
2006-11-08 21:45:55 +01:00
|
|
|
status = RPC_S_OUT_OF_RESOURCES;
|
2006-11-08 21:45:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-16 17:48:01 +02:00
|
|
|
EnterCriticalSection(&protseq->cs);
|
2017-05-23 17:39:14 +02:00
|
|
|
list_add_tail(&protseq->listeners, &tcpc->common.protseq_entry);
|
2017-05-23 17:39:30 +02:00
|
|
|
tcpc->common.protseq = protseq;
|
2017-05-16 17:48:01 +02:00
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
2009-03-26 14:35:43 +01:00
|
|
|
|
|
|
|
/* since IPv4 and IPv6 share the same port space, we only need one
|
|
|
|
* successful bind to listen for both */
|
2006-11-08 21:45:19 +01:00
|
|
|
TRACE("listening on %s\n", endpoint);
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-16 17:48:01 +02:00
|
|
|
freeaddrinfo(ai);
|
2006-11-08 21:45:19 +01:00
|
|
|
ERR("couldn't listen on port %s\n", endpoint);
|
2006-11-08 21:45:55 +01:00
|
|
|
return status;
|
2006-11-08 21:45:19 +01:00
|
|
|
}
|
|
|
|
|
2006-04-21 09:07:35 +02:00
|
|
|
static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
|
|
|
|
{
|
2006-10-18 18:14:11 +02:00
|
|
|
int ret;
|
|
|
|
struct sockaddr_in address;
|
|
|
|
socklen_t addrsize;
|
|
|
|
RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
|
|
|
|
RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
|
2009-04-08 13:54:43 +02:00
|
|
|
u_long nonblocking;
|
2006-10-18 18:14:11 +02:00
|
|
|
|
|
|
|
addrsize = sizeof(address);
|
|
|
|
ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ERR("Failed to accept a TCP connection: error %d\n", ret);
|
2006-11-08 21:45:55 +01:00
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
2006-10-18 18:14:11 +02:00
|
|
|
}
|
2016-10-12 17:14:11 +02:00
|
|
|
|
2009-04-08 13:54:43 +02:00
|
|
|
nonblocking = 1;
|
|
|
|
ioctlsocket(ret, FIONBIO, &nonblocking);
|
2006-10-18 18:14:11 +02:00
|
|
|
client->sock = ret;
|
2016-10-12 17:14:11 +02:00
|
|
|
|
|
|
|
client->common.NetworkAddr = HeapAlloc(GetProcessHeap(), 0, INET6_ADDRSTRLEN);
|
|
|
|
ret = getnameinfo((struct sockaddr*)&address, addrsize, client->common.NetworkAddr, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
ERR("Failed to retrieve the IP address, error %d\n", ret);
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Accepted a new TCP connection from %s\n", client->common.NetworkAddr);
|
2006-10-18 18:14:11 +02:00
|
|
|
return RPC_S_OK;
|
2006-04-21 09:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
|
|
|
|
void *buffer, unsigned int count)
|
|
|
|
{
|
2006-04-21 11:20:16 +02:00
|
|
|
RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
|
2007-11-12 21:10:19 +01:00
|
|
|
int bytes_read = 0;
|
2009-12-13 22:35:34 +01:00
|
|
|
while (bytes_read != count)
|
2007-11-12 21:10:19 +01:00
|
|
|
{
|
|
|
|
int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
|
2007-12-16 13:07:01 +01:00
|
|
|
if (!r)
|
|
|
|
return -1;
|
|
|
|
else if (r > 0)
|
2007-11-12 21:10:19 +01:00
|
|
|
bytes_read += r;
|
2017-05-16 17:47:50 +02:00
|
|
|
else if (WSAGetLastError() == WSAEINTR)
|
2014-06-12 06:52:28 +02:00
|
|
|
continue;
|
2017-05-16 17:47:50 +02:00
|
|
|
else if (WSAGetLastError() != WSAEWOULDBLOCK)
|
2007-12-16 13:07:01 +01:00
|
|
|
{
|
2017-05-16 17:47:50 +02:00
|
|
|
WARN("recv() failed: %u\n", WSAGetLastError());
|
2007-11-12 21:10:19 +01:00
|
|
|
return -1;
|
2007-12-16 13:07:01 +01:00
|
|
|
}
|
2007-11-12 21:10:19 +01:00
|
|
|
else
|
|
|
|
{
|
2009-04-08 13:54:43 +02:00
|
|
|
if (!rpcrt4_sock_wait_for_recv(tcpc))
|
2007-11-12 21:10:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-13 22:35:34 +01:00
|
|
|
}
|
2007-11-12 21:10:19 +01:00
|
|
|
TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
|
|
|
|
return bytes_read;
|
2006-04-21 09:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
|
|
|
|
const void *buffer, unsigned int count)
|
|
|
|
{
|
2006-04-21 11:20:16 +02:00
|
|
|
RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
|
2007-11-12 21:10:19 +01:00
|
|
|
int bytes_written = 0;
|
2009-12-13 22:35:34 +01:00
|
|
|
while (bytes_written != count)
|
2007-11-12 21:10:19 +01:00
|
|
|
{
|
2008-01-08 19:13:52 +01:00
|
|
|
int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
|
2007-11-12 21:10:19 +01:00
|
|
|
if (r >= 0)
|
|
|
|
bytes_written += r;
|
2017-05-16 17:47:50 +02:00
|
|
|
else if (WSAGetLastError() == WSAEINTR)
|
2014-06-12 06:52:28 +02:00
|
|
|
continue;
|
2017-05-16 17:47:50 +02:00
|
|
|
else if (WSAGetLastError() != WSAEWOULDBLOCK)
|
2007-11-12 21:10:19 +01:00
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
{
|
2009-04-08 13:54:43 +02:00
|
|
|
if (!rpcrt4_sock_wait_for_send(tcpc))
|
2007-11-12 21:10:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2009-12-13 22:35:34 +01:00
|
|
|
}
|
2007-11-12 21:10:19 +01:00
|
|
|
TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
|
|
|
|
return bytes_written;
|
2006-04-21 09:07:35 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 17:38:43 +02:00
|
|
|
static int rpcrt4_conn_tcp_close(RpcConnection *conn)
|
2006-04-21 09:07:35 +02:00
|
|
|
{
|
2017-05-23 17:38:43 +02:00
|
|
|
RpcConnection_tcp *connection = (RpcConnection_tcp *) conn;
|
2006-04-21 11:20:16 +02:00
|
|
|
|
2017-05-23 17:38:43 +02:00
|
|
|
TRACE("%d\n", connection->sock);
|
2006-10-18 18:14:11 +02:00
|
|
|
|
2017-05-23 17:38:43 +02:00
|
|
|
if (connection->sock != -1)
|
|
|
|
closesocket(connection->sock);
|
|
|
|
connection->sock = -1;
|
|
|
|
CloseHandle(connection->sock_event);
|
|
|
|
CloseHandle(connection->cancel_event);
|
|
|
|
return 0;
|
2006-04-21 09:07:35 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 13:52:07 +02:00
|
|
|
static void rpcrt4_conn_tcp_close_read(RpcConnection *conn)
|
|
|
|
{
|
2017-05-24 13:52:19 +02:00
|
|
|
RpcConnection_tcp *connection = (RpcConnection_tcp *) conn;
|
|
|
|
shutdown(connection->sock, SD_RECEIVE);
|
2017-05-24 13:52:07 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 17:38:43 +02:00
|
|
|
static void rpcrt4_conn_tcp_cancel_call(RpcConnection *conn)
|
2007-11-12 21:10:19 +01:00
|
|
|
{
|
2017-05-23 17:38:43 +02:00
|
|
|
RpcConnection_tcp *connection = (RpcConnection_tcp *) conn;
|
|
|
|
|
|
|
|
TRACE("%p\n", connection);
|
|
|
|
|
|
|
|
SetEvent(connection->cancel_event);
|
2007-11-12 21:10:19 +01:00
|
|
|
}
|
|
|
|
|
2015-10-27 19:28:28 +01:00
|
|
|
static RPC_STATUS rpcrt4_conn_tcp_is_server_listening(const char *endpoint)
|
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return RPC_S_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
2008-01-21 11:31:40 +01:00
|
|
|
static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
|
|
|
|
{
|
|
|
|
RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
|
|
|
|
|
|
|
|
TRACE("%p\n", Connection);
|
|
|
|
|
2009-04-08 13:54:43 +02:00
|
|
|
if (!rpcrt4_sock_wait_for_recv(tcpc))
|
|
|
|
return -1;
|
2008-01-21 11:31:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
|
|
|
|
const char *networkaddr,
|
|
|
|
const char *endpoint)
|
|
|
|
{
|
|
|
|
return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
|
|
|
|
EPM_PROTOCOL_TCP, endpoint);
|
|
|
|
}
|
|
|
|
|
2009-04-08 13:55:38 +02:00
|
|
|
typedef struct _RpcServerProtseq_sock
|
|
|
|
{
|
|
|
|
RpcServerProtseq common;
|
|
|
|
HANDLE mgr_event;
|
|
|
|
} RpcServerProtseq_sock;
|
|
|
|
|
|
|
|
static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
|
|
|
|
{
|
2017-05-24 13:52:24 +02:00
|
|
|
RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ps));
|
2009-04-08 13:55:38 +02:00
|
|
|
if (ps)
|
|
|
|
{
|
|
|
|
static BOOL wsa_inited;
|
|
|
|
if (!wsa_inited)
|
|
|
|
{
|
|
|
|
WSADATA wsadata;
|
|
|
|
WSAStartup(MAKEWORD(2, 2), &wsadata);
|
|
|
|
/* Note: WSAStartup can be called more than once so we don't bother with
|
|
|
|
* making accesses to wsa_inited thread-safe */
|
|
|
|
wsa_inited = TRUE;
|
|
|
|
}
|
|
|
|
ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
}
|
|
|
|
return &ps->common;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
|
|
|
|
{
|
|
|
|
RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
|
|
|
|
SetEvent(sockps->mgr_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
|
|
|
|
{
|
|
|
|
HANDLE *objs = prev_array;
|
|
|
|
RpcConnection_tcp *conn;
|
|
|
|
RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
|
|
|
|
|
|
|
|
EnterCriticalSection(&protseq->cs);
|
|
|
|
|
|
|
|
/* open and count connections */
|
|
|
|
*count = 1;
|
2017-05-23 17:39:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY(conn, &protseq->listeners, RpcConnection_tcp, common.protseq_entry)
|
2009-04-08 13:55:38 +02:00
|
|
|
{
|
|
|
|
if (conn->sock != -1)
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make array of connections */
|
|
|
|
if (objs)
|
|
|
|
objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
|
|
|
|
else
|
|
|
|
objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
|
|
|
|
if (!objs)
|
|
|
|
{
|
|
|
|
ERR("couldn't allocate objs\n");
|
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
objs[0] = sockps->mgr_event;
|
|
|
|
*count = 1;
|
2017-05-23 17:39:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY(conn, &protseq->listeners, RpcConnection_tcp, common.protseq_entry)
|
2009-04-08 13:55:38 +02:00
|
|
|
{
|
|
|
|
if (conn->sock != -1)
|
|
|
|
{
|
|
|
|
int res = WSAEventSelect(conn->sock, conn->sock_event, FD_ACCEPT);
|
|
|
|
if (res == SOCKET_ERROR)
|
|
|
|
ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
objs[*count] = conn->sock_event;
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, array);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
|
|
|
|
{
|
|
|
|
HANDLE b_handle;
|
|
|
|
HANDLE *objs = wait_array;
|
|
|
|
DWORD res;
|
2017-05-16 17:48:01 +02:00
|
|
|
RpcConnection *cconn = NULL;
|
2009-04-08 13:55:38 +02:00
|
|
|
RpcConnection_tcp *conn;
|
|
|
|
|
|
|
|
if (!objs)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* an alertable wait isn't strictly necessary, but due to our
|
|
|
|
* overlapped I/O implementation in Wine we need to free some memory
|
|
|
|
* by the file user APC being called, even if no completion routine was
|
|
|
|
* specified at the time of starting the async operation */
|
|
|
|
res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
|
|
|
|
} while (res == WAIT_IO_COMPLETION);
|
|
|
|
|
|
|
|
if (res == WAIT_OBJECT_0)
|
|
|
|
return 0;
|
2017-05-16 17:48:01 +02:00
|
|
|
if (res == WAIT_FAILED)
|
2009-04-08 13:55:38 +02:00
|
|
|
{
|
|
|
|
ERR("wait failed with error %d\n", GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
2017-05-16 17:48:01 +02:00
|
|
|
|
|
|
|
b_handle = objs[res - WAIT_OBJECT_0];
|
|
|
|
|
|
|
|
/* find which connection got a RPC */
|
|
|
|
EnterCriticalSection(&protseq->cs);
|
2017-05-23 17:39:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY(conn, &protseq->listeners, RpcConnection_tcp, common.protseq_entry)
|
2009-04-08 13:55:38 +02:00
|
|
|
{
|
2017-05-16 17:48:01 +02:00
|
|
|
if (b_handle == conn->sock_event)
|
2009-04-08 13:55:38 +02:00
|
|
|
{
|
2017-05-23 17:39:39 +02:00
|
|
|
cconn = rpcrt4_spawn_connection(&conn->common);
|
2017-05-16 17:48:01 +02:00
|
|
|
break;
|
2009-04-08 13:55:38 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-16 17:48:01 +02:00
|
|
|
LeaveCriticalSection(&protseq->cs);
|
|
|
|
if (!cconn)
|
|
|
|
{
|
|
|
|
ERR("failed to locate connection for handle %p\n", b_handle);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
RPCRT4_new_client(cconn);
|
|
|
|
return 1;
|
2009-04-08 13:55:38 +02:00
|
|
|
}
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
|
|
|
|
size_t tower_size,
|
|
|
|
char **networkaddr,
|
|
|
|
char **endpoint)
|
|
|
|
{
|
|
|
|
return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
|
|
|
|
networkaddr, EPM_PROTOCOL_TCP,
|
|
|
|
endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**** ncacn_http support ****/
|
|
|
|
|
|
|
|
/* 60 seconds is the period native uses */
|
|
|
|
#define HTTP_IDLE_TIME 60000
|
|
|
|
|
|
|
|
/* reference counted to avoid a race between a cancelled call's connection
|
|
|
|
* being destroyed and the asynchronous InternetReadFileEx call being
|
|
|
|
* completed */
|
|
|
|
typedef struct _RpcHttpAsyncData
|
|
|
|
{
|
|
|
|
LONG refs;
|
|
|
|
HANDLE completion_event;
|
2012-08-14 13:35:46 +02:00
|
|
|
WORD async_result;
|
2015-05-13 12:12:47 +02:00
|
|
|
INTERNET_BUFFERSW inet_buffers;
|
2009-03-18 12:13:19 +01:00
|
|
|
CRITICAL_SECTION cs;
|
|
|
|
} RpcHttpAsyncData;
|
|
|
|
|
|
|
|
static ULONG RpcHttpAsyncData_AddRef(RpcHttpAsyncData *data)
|
|
|
|
{
|
|
|
|
return InterlockedIncrement(&data->refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG RpcHttpAsyncData_Release(RpcHttpAsyncData *data)
|
|
|
|
{
|
|
|
|
ULONG refs = InterlockedDecrement(&data->refs);
|
|
|
|
if (!refs)
|
|
|
|
{
|
|
|
|
TRACE("destroying async data %p\n", data);
|
|
|
|
CloseHandle(data->completion_event);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data->inet_buffers.lpvBuffer);
|
2011-11-14 15:10:01 +01:00
|
|
|
data->cs.DebugInfo->Spare[0] = 0;
|
2009-03-18 12:13:19 +01:00
|
|
|
DeleteCriticalSection(&data->cs);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data);
|
|
|
|
}
|
|
|
|
return refs;
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:35:06 +02:00
|
|
|
static void prepare_async_request(RpcHttpAsyncData *async_data)
|
|
|
|
{
|
|
|
|
ResetEvent(async_data->completion_event);
|
|
|
|
RpcHttpAsyncData_AddRef(async_data);
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:35:17 +02:00
|
|
|
static RPC_STATUS wait_async_request(RpcHttpAsyncData *async_data, BOOL call_ret, HANDLE cancel_event)
|
|
|
|
{
|
2012-08-15 10:22:39 +02:00
|
|
|
HANDLE handles[2] = { async_data->completion_event, cancel_event };
|
|
|
|
DWORD res;
|
|
|
|
|
2012-08-14 13:35:17 +02:00
|
|
|
if(call_ret) {
|
|
|
|
RpcHttpAsyncData_Release(async_data);
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GetLastError() != ERROR_IO_PENDING) {
|
|
|
|
RpcHttpAsyncData_Release(async_data);
|
|
|
|
ERR("Request failed with error %d\n", GetLastError());
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-08-15 10:22:39 +02:00
|
|
|
res = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
|
|
|
|
if(res != WAIT_OBJECT_0) {
|
|
|
|
TRACE("Cancelled\n");
|
|
|
|
return RPC_S_CALL_CANCELLED;
|
2012-08-14 13:35:17 +02:00
|
|
|
}
|
2012-08-15 10:22:39 +02:00
|
|
|
|
2012-08-14 13:35:46 +02:00
|
|
|
if(async_data->async_result) {
|
|
|
|
ERR("Async request failed with error %d\n", async_data->async_result);
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:35:17 +02:00
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
struct authinfo
|
|
|
|
{
|
|
|
|
DWORD scheme;
|
|
|
|
CredHandle cred;
|
|
|
|
CtxtHandle ctx;
|
|
|
|
TimeStamp exp;
|
|
|
|
ULONG attr;
|
|
|
|
ULONG max_token;
|
|
|
|
char *data;
|
|
|
|
unsigned int data_len;
|
|
|
|
BOOL finished; /* finished authenticating */
|
|
|
|
};
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
typedef struct _RpcConnection_http
|
|
|
|
{
|
|
|
|
RpcConnection common;
|
|
|
|
HINTERNET app_info;
|
|
|
|
HINTERNET session;
|
|
|
|
HINTERNET in_request;
|
|
|
|
HINTERNET out_request;
|
2013-09-04 15:01:10 +02:00
|
|
|
WCHAR *servername;
|
2009-03-18 12:13:19 +01:00
|
|
|
HANDLE timer_cancelled;
|
|
|
|
HANDLE cancel_event;
|
|
|
|
DWORD last_sent_time;
|
|
|
|
ULONG bytes_received;
|
|
|
|
ULONG flow_control_mark; /* send a control packet to the server when this many bytes received */
|
|
|
|
ULONG flow_control_increment; /* number of bytes to increment flow_control_mark by */
|
|
|
|
UUID connection_uuid;
|
|
|
|
UUID in_pipe_uuid;
|
|
|
|
UUID out_pipe_uuid;
|
|
|
|
RpcHttpAsyncData *async_data;
|
|
|
|
} RpcConnection_http;
|
|
|
|
|
|
|
|
static RpcConnection *rpcrt4_ncacn_http_alloc(void)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc;
|
|
|
|
httpc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*httpc));
|
|
|
|
if (!httpc) return NULL;
|
|
|
|
httpc->async_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcHttpAsyncData));
|
|
|
|
if (!httpc->async_data)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, httpc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
TRACE("async data = %p\n", httpc->async_data);
|
|
|
|
httpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
httpc->async_data->refs = 1;
|
2015-05-13 12:12:47 +02:00
|
|
|
httpc->async_data->inet_buffers.dwStructSize = sizeof(INTERNET_BUFFERSW);
|
2009-03-18 12:13:19 +01:00
|
|
|
InitializeCriticalSection(&httpc->async_data->cs);
|
2011-11-14 15:10:01 +01:00
|
|
|
httpc->async_data->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RpcHttpAsyncData.cs");
|
2009-03-18 12:13:19 +01:00
|
|
|
return &httpc->common;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _HttpTimerThreadData
|
|
|
|
{
|
|
|
|
PVOID timer_param;
|
|
|
|
DWORD *last_sent_time;
|
|
|
|
HANDLE timer_cancelled;
|
|
|
|
} HttpTimerThreadData;
|
|
|
|
|
2009-05-06 03:43:50 +02:00
|
|
|
static VOID rpcrt4_http_keep_connection_active_timer_proc(PVOID param, BOOLEAN dummy)
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
|
|
|
HINTERNET in_request = param;
|
|
|
|
RpcPktHdr *idle_pkt;
|
|
|
|
|
|
|
|
idle_pkt = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x0001,
|
|
|
|
0, 0);
|
|
|
|
if (idle_pkt)
|
|
|
|
{
|
|
|
|
DWORD bytes_written;
|
|
|
|
InternetWriteFile(in_request, idle_pkt, idle_pkt->common.frag_len, &bytes_written);
|
|
|
|
RPCRT4_FreeHeader(idle_pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline DWORD rpcrt4_http_timer_calc_timeout(DWORD *last_sent_time)
|
|
|
|
{
|
|
|
|
DWORD cur_time = GetTickCount();
|
|
|
|
DWORD cached_last_sent_time = *last_sent_time;
|
|
|
|
return HTTP_IDLE_TIME - (cur_time - cached_last_sent_time > HTTP_IDLE_TIME ? 0 : cur_time - cached_last_sent_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD CALLBACK rpcrt4_http_timer_thread(PVOID param)
|
|
|
|
{
|
|
|
|
HttpTimerThreadData *data_in = param;
|
|
|
|
HttpTimerThreadData data;
|
|
|
|
DWORD timeout;
|
|
|
|
|
|
|
|
data = *data_in;
|
|
|
|
HeapFree(GetProcessHeap(), 0, data_in);
|
|
|
|
|
|
|
|
for (timeout = HTTP_IDLE_TIME;
|
|
|
|
WaitForSingleObject(data.timer_cancelled, timeout) == WAIT_TIMEOUT;
|
|
|
|
timeout = rpcrt4_http_timer_calc_timeout(data.last_sent_time))
|
|
|
|
{
|
|
|
|
/* are we too soon after last send? */
|
2013-01-24 15:15:16 +01:00
|
|
|
if (GetTickCount() - *data.last_sent_time < HTTP_IDLE_TIME)
|
2009-03-18 12:13:19 +01:00
|
|
|
continue;
|
|
|
|
rpcrt4_http_keep_connection_active_timer_proc(data.timer_param, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(data.timer_cancelled);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID WINAPI rpcrt4_http_internet_callback(
|
|
|
|
HINTERNET hInternet,
|
|
|
|
DWORD_PTR dwContext,
|
|
|
|
DWORD dwInternetStatus,
|
|
|
|
LPVOID lpvStatusInformation,
|
|
|
|
DWORD dwStatusInformationLength)
|
|
|
|
{
|
|
|
|
RpcHttpAsyncData *async_data = (RpcHttpAsyncData *)dwContext;
|
|
|
|
|
|
|
|
switch (dwInternetStatus)
|
|
|
|
{
|
|
|
|
case INTERNET_STATUS_REQUEST_COMPLETE:
|
|
|
|
TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
|
|
|
|
if (async_data)
|
|
|
|
{
|
2012-08-14 13:35:46 +02:00
|
|
|
INTERNET_ASYNC_RESULT *async_result = lpvStatusInformation;
|
|
|
|
|
|
|
|
async_data->async_result = async_result->dwResult ? ERROR_SUCCESS : async_result->dwError;
|
2009-03-18 12:13:19 +01:00
|
|
|
SetEvent(async_data->completion_event);
|
|
|
|
RpcHttpAsyncData_Release(async_data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_http_check_response(HINTERNET hor)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD status_code;
|
|
|
|
DWORD size;
|
|
|
|
DWORD index;
|
2009-03-19 13:23:45 +01:00
|
|
|
WCHAR buf[32];
|
|
|
|
WCHAR *status_text = buf;
|
2009-03-18 12:13:19 +01:00
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
size = sizeof(status_code);
|
|
|
|
ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status_code, &size, &index);
|
|
|
|
if (!ret)
|
|
|
|
return GetLastError();
|
2013-08-28 13:31:55 +02:00
|
|
|
if (status_code == HTTP_STATUS_OK)
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_OK;
|
|
|
|
index = 0;
|
2009-03-19 13:23:45 +01:00
|
|
|
size = sizeof(buf);
|
2009-03-18 12:13:19 +01:00
|
|
|
ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
|
2009-03-19 13:23:45 +01:00
|
|
|
if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
{
|
|
|
|
status_text = HeapAlloc(GetProcessHeap(), 0, size);
|
|
|
|
ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR("server returned: %d %s\n", status_code, ret ? debugstr_w(status_text) : "<status text unavailable>");
|
|
|
|
if(status_text != buf) HeapFree(GetProcessHeap(), 0, status_text);
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
if (status_code == HTTP_STATUS_DENIED)
|
|
|
|
return ERROR_ACCESS_DENIED;
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_http_internet_connect(RpcConnection_http *httpc)
|
|
|
|
{
|
|
|
|
static const WCHAR wszUserAgent[] = {'M','S','R','P','C',0};
|
|
|
|
LPWSTR proxy = NULL;
|
|
|
|
LPWSTR user = NULL;
|
|
|
|
LPWSTR password = NULL;
|
|
|
|
LPWSTR servername = NULL;
|
|
|
|
const WCHAR *option;
|
2013-03-25 13:26:03 +01:00
|
|
|
INTERNET_PORT port;
|
2009-03-18 12:13:19 +01:00
|
|
|
|
|
|
|
if (httpc->common.QOS &&
|
|
|
|
(httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP))
|
|
|
|
{
|
|
|
|
const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_cred = httpc->common.QOS->qos->u.HttpCredentials;
|
|
|
|
if (http_cred->TransportCredentials)
|
|
|
|
{
|
|
|
|
WCHAR *p;
|
|
|
|
const SEC_WINNT_AUTH_IDENTITY_W *cred = http_cred->TransportCredentials;
|
|
|
|
ULONG len = cred->DomainLength + 1 + cred->UserLength;
|
|
|
|
user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
|
|
|
|
if (!user)
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
p = user;
|
|
|
|
if (cred->DomainLength)
|
|
|
|
{
|
|
|
|
memcpy(p, cred->Domain, cred->DomainLength * sizeof(WCHAR));
|
|
|
|
p += cred->DomainLength;
|
|
|
|
*p = '\\';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
memcpy(p, cred->User, cred->UserLength * sizeof(WCHAR));
|
|
|
|
p[cred->UserLength] = 0;
|
|
|
|
|
|
|
|
password = RPCRT4_strndupW(cred->Password, cred->PasswordLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (option = httpc->common.NetworkOptions; option;
|
2019-06-19 10:32:06 +02:00
|
|
|
option = (wcschr(option, ',') ? wcschr(option, ',')+1 : NULL))
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
|
|
|
static const WCHAR wszRpcProxy[] = {'R','p','c','P','r','o','x','y','=',0};
|
|
|
|
static const WCHAR wszHttpProxy[] = {'H','t','t','p','P','r','o','x','y','=',0};
|
|
|
|
|
2019-06-19 10:32:06 +02:00
|
|
|
if (!wcsnicmp(option, wszRpcProxy, ARRAY_SIZE(wszRpcProxy)-1))
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
2018-10-19 22:11:34 +02:00
|
|
|
const WCHAR *value_start = option + ARRAY_SIZE(wszRpcProxy)-1;
|
2009-03-18 12:13:19 +01:00
|
|
|
const WCHAR *value_end;
|
|
|
|
const WCHAR *p;
|
|
|
|
|
2019-06-19 10:32:06 +02:00
|
|
|
value_end = wcschr(option, ',');
|
2009-03-18 12:13:19 +01:00
|
|
|
if (!value_end)
|
2019-06-19 10:32:06 +02:00
|
|
|
value_end = value_start + lstrlenW(value_start);
|
2009-03-18 12:13:19 +01:00
|
|
|
for (p = value_start; p < value_end; p++)
|
|
|
|
if (*p == ':')
|
|
|
|
{
|
2019-06-19 10:32:06 +02:00
|
|
|
port = wcstol(p+1, NULL, 10);
|
2009-03-18 12:13:19 +01:00
|
|
|
value_end = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TRACE("RpcProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
|
|
|
|
servername = RPCRT4_strndupW(value_start, value_end-value_start);
|
|
|
|
}
|
2019-06-19 10:32:06 +02:00
|
|
|
else if (!wcsnicmp(option, wszHttpProxy, ARRAY_SIZE(wszHttpProxy)-1))
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
2018-10-19 22:11:34 +02:00
|
|
|
const WCHAR *value_start = option + ARRAY_SIZE(wszHttpProxy)-1;
|
2009-03-18 12:13:19 +01:00
|
|
|
const WCHAR *value_end;
|
|
|
|
|
2019-06-19 10:32:06 +02:00
|
|
|
value_end = wcschr(option, ',');
|
2009-03-18 12:13:19 +01:00
|
|
|
if (!value_end)
|
2019-06-19 10:32:06 +02:00
|
|
|
value_end = value_start + lstrlenW(value_start);
|
2009-03-18 12:13:19 +01:00
|
|
|
TRACE("HttpProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
|
|
|
|
proxy = RPCRT4_strndupW(value_start, value_end-value_start);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
FIXME("unhandled option %s\n", debugstr_w(option));
|
|
|
|
}
|
|
|
|
|
|
|
|
httpc->app_info = InternetOpenW(wszUserAgent, proxy ? INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_PRECONFIG,
|
|
|
|
NULL, NULL, INTERNET_FLAG_ASYNC);
|
|
|
|
if (!httpc->app_info)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, password);
|
|
|
|
HeapFree(GetProcessHeap(), 0, user);
|
2012-10-31 09:43:46 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, proxy);
|
|
|
|
HeapFree(GetProcessHeap(), 0, servername);
|
2009-03-18 12:13:19 +01:00
|
|
|
ERR("InternetOpenW failed with error %d\n", GetLastError());
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
InternetSetStatusCallbackW(httpc->app_info, rpcrt4_http_internet_callback);
|
|
|
|
|
|
|
|
/* if no RpcProxy option specified, set the HTTP server address to the
|
|
|
|
* RPC server address */
|
|
|
|
if (!servername)
|
|
|
|
{
|
|
|
|
servername = HeapAlloc(GetProcessHeap(), 0, (strlen(httpc->common.NetworkAddr) + 1)*sizeof(WCHAR));
|
|
|
|
if (!servername)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, password);
|
|
|
|
HeapFree(GetProcessHeap(), 0, user);
|
2012-10-31 09:43:46 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, proxy);
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, httpc->common.NetworkAddr, -1, servername, strlen(httpc->common.NetworkAddr) + 1);
|
|
|
|
}
|
|
|
|
|
2013-03-25 13:26:03 +01:00
|
|
|
port = (httpc->common.QOS &&
|
|
|
|
(httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
|
|
|
|
(httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL)) ?
|
|
|
|
INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
httpc->session = InternetConnectW(httpc->app_info, servername, port, user, password,
|
|
|
|
INTERNET_SERVICE_HTTP, 0, 0);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, password);
|
|
|
|
HeapFree(GetProcessHeap(), 0, user);
|
2012-10-31 09:43:46 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, proxy);
|
2009-03-18 12:13:19 +01:00
|
|
|
|
|
|
|
if (!httpc->session)
|
|
|
|
{
|
|
|
|
ERR("InternetConnectW failed with error %d\n", GetLastError());
|
2013-09-04 15:01:10 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, servername);
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
2013-09-04 15:01:10 +02:00
|
|
|
httpc->servername = servername;
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
static int rpcrt4_http_async_read(HINTERNET req, RpcHttpAsyncData *async_data, HANDLE cancel_event,
|
|
|
|
void *buffer, unsigned int count)
|
|
|
|
{
|
|
|
|
char *buf = buffer;
|
|
|
|
BOOL ret;
|
|
|
|
unsigned int bytes_left = count;
|
|
|
|
RPC_STATUS status = RPC_S_OK;
|
|
|
|
|
|
|
|
async_data->inet_buffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, count);
|
|
|
|
|
|
|
|
while (bytes_left)
|
|
|
|
{
|
|
|
|
async_data->inet_buffers.dwBufferLength = bytes_left;
|
|
|
|
prepare_async_request(async_data);
|
|
|
|
ret = InternetReadFileExW(req, &async_data->inet_buffers, IRF_ASYNC, 0);
|
|
|
|
status = wait_async_request(async_data, ret, cancel_event);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
{
|
|
|
|
if (status == RPC_S_CALL_CANCELLED)
|
|
|
|
TRACE("call cancelled\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!async_data->inet_buffers.dwBufferLength)
|
|
|
|
break;
|
|
|
|
memcpy(buf, async_data->inet_buffers.lpvBuffer,
|
|
|
|
async_data->inet_buffers.dwBufferLength);
|
|
|
|
|
|
|
|
bytes_left -= async_data->inet_buffers.dwBufferLength;
|
|
|
|
buf += async_data->inet_buffers.dwBufferLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, async_data->inet_buffers.lpvBuffer);
|
|
|
|
async_data->inet_buffers.lpvBuffer = NULL;
|
|
|
|
|
|
|
|
TRACE("%p %p %u -> %u\n", req, buffer, count, status);
|
|
|
|
return status == RPC_S_OK ? count : -1;
|
|
|
|
}
|
|
|
|
|
2012-08-15 10:23:07 +02:00
|
|
|
static RPC_STATUS send_echo_request(HINTERNET req, RpcHttpAsyncData *async_data, HANDLE cancel_event)
|
|
|
|
{
|
|
|
|
BYTE buf[20];
|
|
|
|
BOOL ret;
|
|
|
|
RPC_STATUS status;
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
TRACE("sending echo request to server\n");
|
|
|
|
|
2012-08-15 10:23:07 +02:00
|
|
|
prepare_async_request(async_data);
|
|
|
|
ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
|
|
|
|
status = wait_async_request(async_data, ret, cancel_event);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
|
|
|
status = rpcrt4_http_check_response(req);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
rpcrt4_http_async_read(req, async_data, cancel_event, buf, sizeof(buf));
|
2012-08-15 10:23:07 +02:00
|
|
|
/* FIXME: do something with retrieved data */
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:13:20 +02:00
|
|
|
static RPC_STATUS insert_content_length_header(HINTERNET request, DWORD len)
|
|
|
|
{
|
|
|
|
static const WCHAR fmtW[] =
|
|
|
|
{'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
|
2018-10-19 22:11:34 +02:00
|
|
|
WCHAR header[ARRAY_SIZE(fmtW) + 10];
|
2015-05-13 12:13:20 +02:00
|
|
|
|
2019-06-19 10:32:06 +02:00
|
|
|
swprintf(header, ARRAY_SIZE(header), fmtW, len);
|
2015-06-25 12:27:43 +02:00
|
|
|
if ((HttpAddRequestHeadersW(request, header, -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))) return RPC_S_OK;
|
2015-05-13 12:13:20 +02:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
/* prepare the in pipe for use by RPC packets */
|
2012-08-15 10:22:39 +02:00
|
|
|
static RPC_STATUS rpcrt4_http_prepare_in_pipe(HINTERNET in_request, RpcHttpAsyncData *async_data, HANDLE cancel_event,
|
2013-09-04 15:01:10 +02:00
|
|
|
const UUID *connection_uuid, const UUID *in_pipe_uuid,
|
|
|
|
const UUID *association_uuid, BOOL authorized)
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
RPC_STATUS status;
|
|
|
|
RpcPktHdr *hdr;
|
|
|
|
INTERNET_BUFFERSW buffers_in;
|
2012-08-15 10:23:07 +02:00
|
|
|
DWORD bytes_written;
|
2009-03-18 12:13:19 +01:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
if (!authorized)
|
|
|
|
{
|
|
|
|
/* ask wininet to authorize, if necessary */
|
|
|
|
status = send_echo_request(in_request, async_data, cancel_event);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
}
|
2009-03-18 12:13:19 +01:00
|
|
|
memset(&buffers_in, 0, sizeof(buffers_in));
|
|
|
|
buffers_in.dwStructSize = sizeof(buffers_in);
|
|
|
|
/* FIXME: get this from the registry */
|
|
|
|
buffers_in.dwBufferTotal = 1024 * 1024 * 1024; /* 1Gb */
|
2015-05-13 12:13:20 +02:00
|
|
|
status = insert_content_length_header(in_request, buffers_in.dwBufferTotal);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
2012-08-14 13:35:06 +02:00
|
|
|
prepare_async_request(async_data);
|
2009-03-18 12:13:19 +01:00
|
|
|
ret = HttpSendRequestExW(in_request, &buffers_in, NULL, 0, 0);
|
2012-08-15 10:22:39 +02:00
|
|
|
status = wait_async_request(async_data, ret, cancel_event);
|
2012-08-14 13:35:17 +02:00
|
|
|
if (status != RPC_S_OK) return status;
|
2009-03-18 12:13:19 +01:00
|
|
|
|
|
|
|
TRACE("sending HTTP connect header to server\n");
|
2013-08-28 13:30:41 +02:00
|
|
|
hdr = RPCRT4_BuildHttpConnectHeader(FALSE, connection_uuid, in_pipe_uuid, association_uuid);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (!hdr) return RPC_S_OUT_OF_RESOURCES;
|
|
|
|
ret = InternetWriteFile(in_request, hdr, hdr->common.frag_len, &bytes_written);
|
|
|
|
RPCRT4_FreeHeader(hdr);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ERR("InternetWriteFile failed with error %d\n", GetLastError());
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
static RPC_STATUS rpcrt4_http_read_http_packet(HINTERNET request, RpcHttpAsyncData *async_data,
|
|
|
|
HANDLE cancel_event, RpcPktHdr *hdr, BYTE **data)
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
|
|
|
unsigned short data_len;
|
2015-05-13 12:13:47 +02:00
|
|
|
unsigned int size;
|
2009-03-18 12:13:19 +01:00
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
if (rpcrt4_http_async_read(request, async_data, cancel_event, hdr, sizeof(hdr->common)) < 0)
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
if (hdr->common.ptype != PKT_HTTP || hdr->common.frag_len < sizeof(hdr->http))
|
|
|
|
{
|
|
|
|
ERR("wrong packet type received %d or wrong frag_len %d\n",
|
|
|
|
hdr->common.ptype, hdr->common.frag_len);
|
|
|
|
return RPC_S_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
size = sizeof(hdr->http) - sizeof(hdr->common);
|
|
|
|
if (rpcrt4_http_async_read(request, async_data, cancel_event, &hdr->common + 1, size) < 0)
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
|
|
|
|
data_len = hdr->common.frag_len - sizeof(hdr->http);
|
|
|
|
if (data_len)
|
|
|
|
{
|
|
|
|
*data = HeapAlloc(GetProcessHeap(), 0, data_len);
|
|
|
|
if (!*data)
|
|
|
|
return RPC_S_OUT_OF_RESOURCES;
|
2015-05-13 12:13:47 +02:00
|
|
|
if (rpcrt4_http_async_read(request, async_data, cancel_event, *data, data_len) < 0)
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, *data);
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*data = NULL;
|
|
|
|
|
|
|
|
if (!RPCRT4_IsValidHttpPacket(hdr, *data, data_len))
|
|
|
|
{
|
|
|
|
ERR("invalid http packet\n");
|
2015-05-21 00:10:57 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, *data);
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare the out pipe for use by RPC packets */
|
2013-09-04 15:01:10 +02:00
|
|
|
static RPC_STATUS rpcrt4_http_prepare_out_pipe(HINTERNET out_request, RpcHttpAsyncData *async_data,
|
|
|
|
HANDLE cancel_event, const UUID *connection_uuid,
|
|
|
|
const UUID *out_pipe_uuid, ULONG *flow_control_increment,
|
|
|
|
BOOL authorized)
|
2009-03-18 12:13:19 +01:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
RPC_STATUS status;
|
|
|
|
RpcPktHdr *hdr;
|
|
|
|
BYTE *data_from_server;
|
|
|
|
RpcPktHdr pkt_from_server;
|
|
|
|
ULONG field1, field3;
|
2013-09-04 15:01:10 +02:00
|
|
|
BYTE buf[20];
|
2009-03-18 12:13:19 +01:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
if (!authorized)
|
|
|
|
{
|
|
|
|
/* ask wininet to authorize, if necessary */
|
|
|
|
status = send_echo_request(out_request, async_data, cancel_event);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
}
|
|
|
|
else
|
2015-05-13 12:13:47 +02:00
|
|
|
rpcrt4_http_async_read(out_request, async_data, cancel_event, buf, sizeof(buf));
|
2012-08-14 13:35:17 +02:00
|
|
|
|
2013-08-28 13:30:41 +02:00
|
|
|
hdr = RPCRT4_BuildHttpConnectHeader(TRUE, connection_uuid, out_pipe_uuid, NULL);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (!hdr) return RPC_S_OUT_OF_RESOURCES;
|
2012-08-14 13:35:06 +02:00
|
|
|
|
2015-05-13 12:13:20 +02:00
|
|
|
status = insert_content_length_header(out_request, hdr->common.frag_len);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
{
|
|
|
|
RPCRT4_FreeHeader(hdr);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
TRACE("sending HTTP connect header to server\n");
|
2012-08-14 13:35:06 +02:00
|
|
|
prepare_async_request(async_data);
|
2015-05-13 12:13:20 +02:00
|
|
|
ret = HttpSendRequestW(out_request, NULL, 0, hdr, hdr->common.frag_len);
|
2012-08-15 10:22:39 +02:00
|
|
|
status = wait_async_request(async_data, ret, cancel_event);
|
2009-03-18 12:13:19 +01:00
|
|
|
RPCRT4_FreeHeader(hdr);
|
2012-08-14 13:35:17 +02:00
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
status = rpcrt4_http_check_response(out_request);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
status = rpcrt4_http_read_http_packet(out_request, async_data, cancel_event,
|
|
|
|
&pkt_from_server, &data_from_server);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
status = RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server, data_from_server,
|
|
|
|
&field1);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data_from_server);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
TRACE("received (%d) from first prepare header\n", field1);
|
|
|
|
|
2013-02-21 10:47:45 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
2015-05-13 12:13:47 +02:00
|
|
|
status = rpcrt4_http_read_http_packet(out_request, async_data, cancel_event,
|
|
|
|
&pkt_from_server, &data_from_server);
|
2013-02-21 10:47:45 +01:00
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
if (pkt_from_server.http.flags != 0x0001) break;
|
|
|
|
|
|
|
|
TRACE("http idle packet, waiting for real packet\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, data_from_server);
|
|
|
|
if (pkt_from_server.http.num_data_items != 0)
|
|
|
|
{
|
|
|
|
ERR("HTTP idle packet should have no data items instead of %d\n",
|
|
|
|
pkt_from_server.http.num_data_items);
|
|
|
|
return RPC_S_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2009-03-18 12:13:19 +01:00
|
|
|
status = RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server, data_from_server,
|
|
|
|
&field1, flow_control_increment,
|
|
|
|
&field3);
|
|
|
|
HeapFree(GetProcessHeap(), 0, data_from_server);
|
|
|
|
if (status != RPC_S_OK) return status;
|
|
|
|
TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1, *flow_control_increment, field3);
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-20 12:55:13 +02:00
|
|
|
static UINT encode_base64(const char *bin, unsigned int len, WCHAR *base64)
|
|
|
|
{
|
2013-12-17 22:17:40 +01:00
|
|
|
static const char enc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
2013-08-20 12:55:13 +02:00
|
|
|
UINT i = 0, x;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
/* first 6 bits, all from bin[0] */
|
|
|
|
base64[i++] = enc[(bin[0] & 0xfc) >> 2];
|
|
|
|
x = (bin[0] & 3) << 4;
|
|
|
|
|
|
|
|
/* next 6 bits, 2 from bin[0] and 4 from bin[1] */
|
|
|
|
if (len == 1)
|
|
|
|
{
|
|
|
|
base64[i++] = enc[x];
|
|
|
|
base64[i++] = '=';
|
|
|
|
base64[i++] = '=';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
base64[i++] = enc[x | ((bin[1] & 0xf0) >> 4)];
|
|
|
|
x = (bin[1] & 0x0f) << 2;
|
|
|
|
|
|
|
|
/* next 6 bits 4 from bin[1] and 2 from bin[2] */
|
|
|
|
if (len == 2)
|
|
|
|
{
|
|
|
|
base64[i++] = enc[x];
|
|
|
|
base64[i++] = '=';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
base64[i++] = enc[x | ((bin[2] & 0xc0) >> 6)];
|
|
|
|
|
|
|
|
/* last 6 bits, all from bin [2] */
|
|
|
|
base64[i++] = enc[bin[2] & 0x3f];
|
|
|
|
bin += 3;
|
|
|
|
len -= 3;
|
|
|
|
}
|
|
|
|
base64[i] = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
static inline char decode_char( WCHAR c )
|
2013-08-20 12:55:13 +02:00
|
|
|
{
|
2013-09-04 15:01:10 +02:00
|
|
|
if (c >= 'A' && c <= 'Z') return c - 'A';
|
|
|
|
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
|
|
|
if (c >= '0' && c <= '9') return c - '0' + 52;
|
|
|
|
if (c == '+') return 62;
|
|
|
|
if (c == '/') return 63;
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int decode_base64( const WCHAR *base64, unsigned int len, char *buf )
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
char c0, c1, c2, c3;
|
|
|
|
const WCHAR *p = base64;
|
|
|
|
|
|
|
|
while (len > 4)
|
|
|
|
{
|
|
|
|
if ((c0 = decode_char( p[0] )) > 63) return 0;
|
|
|
|
if ((c1 = decode_char( p[1] )) > 63) return 0;
|
|
|
|
if ((c2 = decode_char( p[2] )) > 63) return 0;
|
|
|
|
if ((c3 = decode_char( p[3] )) > 63) return 0;
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
buf[i + 0] = (c0 << 2) | (c1 >> 4);
|
|
|
|
buf[i + 1] = (c1 << 4) | (c2 >> 2);
|
|
|
|
buf[i + 2] = (c2 << 6) | c3;
|
|
|
|
}
|
|
|
|
len -= 4;
|
|
|
|
i += 3;
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
if (p[2] == '=')
|
|
|
|
{
|
|
|
|
if ((c0 = decode_char( p[0] )) > 63) return 0;
|
|
|
|
if ((c1 = decode_char( p[1] )) > 63) return 0;
|
|
|
|
|
|
|
|
if (buf) buf[i] = (c0 << 2) | (c1 >> 4);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (p[3] == '=')
|
|
|
|
{
|
|
|
|
if ((c0 = decode_char( p[0] )) > 63) return 0;
|
|
|
|
if ((c1 = decode_char( p[1] )) > 63) return 0;
|
|
|
|
if ((c2 = decode_char( p[2] )) > 63) return 0;
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
buf[i + 0] = (c0 << 2) | (c1 >> 4);
|
|
|
|
buf[i + 1] = (c1 << 4) | (c2 >> 2);
|
|
|
|
}
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((c0 = decode_char( p[0] )) > 63) return 0;
|
|
|
|
if ((c1 = decode_char( p[1] )) > 63) return 0;
|
|
|
|
if ((c2 = decode_char( p[2] )) > 63) return 0;
|
|
|
|
if ((c3 = decode_char( p[3] )) > 63) return 0;
|
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
buf[i + 0] = (c0 << 2) | (c1 >> 4);
|
|
|
|
buf[i + 1] = (c1 << 4) | (c2 >> 2);
|
|
|
|
buf[i + 2] = (c2 << 6) | c3;
|
|
|
|
}
|
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct authinfo *alloc_authinfo(void)
|
|
|
|
{
|
|
|
|
struct authinfo *ret;
|
|
|
|
|
|
|
|
if (!(ret = HeapAlloc(GetProcessHeap(), 0, sizeof(*ret) ))) return NULL;
|
|
|
|
|
|
|
|
SecInvalidateHandle(&ret->cred);
|
|
|
|
SecInvalidateHandle(&ret->ctx);
|
|
|
|
memset(&ret->exp, 0, sizeof(ret->exp));
|
|
|
|
ret->scheme = 0;
|
|
|
|
ret->attr = 0;
|
|
|
|
ret->max_token = 0;
|
|
|
|
ret->data = NULL;
|
|
|
|
ret->data_len = 0;
|
|
|
|
ret->finished = FALSE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_authinfo(struct authinfo *info)
|
|
|
|
{
|
|
|
|
if (!info) return;
|
|
|
|
|
|
|
|
if (SecIsValidHandle(&info->ctx))
|
|
|
|
DeleteSecurityContext(&info->ctx);
|
|
|
|
if (SecIsValidHandle(&info->cred))
|
|
|
|
FreeCredentialsHandle(&info->cred);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, info->data);
|
|
|
|
HeapFree(GetProcessHeap(), 0, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const WCHAR basicW[] = {'B','a','s','i','c',0};
|
|
|
|
static const WCHAR ntlmW[] = {'N','T','L','M',0};
|
|
|
|
static const WCHAR passportW[] = {'P','a','s','s','p','o','r','t',0};
|
|
|
|
static const WCHAR digestW[] = {'D','i','g','e','s','t',0};
|
|
|
|
static const WCHAR negotiateW[] = {'N','e','g','o','t','i','a','t','e',0};
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
const WCHAR *str;
|
|
|
|
unsigned int len;
|
|
|
|
DWORD scheme;
|
|
|
|
}
|
|
|
|
auth_schemes[] =
|
|
|
|
{
|
2018-10-19 22:11:35 +02:00
|
|
|
{ basicW, ARRAY_SIZE(basicW) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC },
|
|
|
|
{ ntlmW, ARRAY_SIZE(ntlmW) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM },
|
|
|
|
{ passportW, ARRAY_SIZE(passportW) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT },
|
|
|
|
{ digestW, ARRAY_SIZE(digestW) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST },
|
|
|
|
{ negotiateW, ARRAY_SIZE(negotiateW) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE }
|
2013-09-04 15:01:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static DWORD auth_scheme_from_header( const WCHAR *header )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2018-10-19 22:11:33 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(auth_schemes); i++)
|
2013-09-04 15:01:10 +02:00
|
|
|
{
|
2019-06-19 10:32:06 +02:00
|
|
|
if (!wcsnicmp( header, auth_schemes[i].str, auth_schemes[i].len ) &&
|
2013-09-04 15:01:10 +02:00
|
|
|
(header[auth_schemes[i].len] == ' ' || !header[auth_schemes[i].len])) return auth_schemes[i].scheme;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL get_authvalue(HINTERNET request, DWORD scheme, WCHAR *buffer, DWORD buflen)
|
|
|
|
{
|
|
|
|
DWORD len, index = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
len = buflen;
|
|
|
|
if (!HttpQueryInfoW(request, HTTP_QUERY_WWW_AUTHENTICATE, buffer, &len, &index)) return FALSE;
|
|
|
|
if (auth_scheme_from_header(buffer) == scheme) break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS do_authorization(HINTERNET request, SEC_WCHAR *servername,
|
|
|
|
const RPC_HTTP_TRANSPORT_CREDENTIALS_W *creds, struct authinfo **auth_ptr)
|
|
|
|
{
|
|
|
|
struct authinfo *info = *auth_ptr;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY_W *id = creds->TransportCredentials;
|
2013-08-20 12:55:13 +02:00
|
|
|
RPC_STATUS status = RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
if ((!info && !(info = alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE;
|
2013-08-20 12:55:13 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
switch (creds->AuthnSchemes[0])
|
|
|
|
{
|
|
|
|
case RPC_C_HTTP_AUTHN_SCHEME_BASIC:
|
|
|
|
{
|
|
|
|
int userlen = WideCharToMultiByte(CP_UTF8, 0, id->User, id->UserLength, NULL, 0, NULL, NULL);
|
|
|
|
int passlen = WideCharToMultiByte(CP_UTF8, 0, id->Password, id->PasswordLength, NULL, 0, NULL, NULL);
|
2013-08-20 12:55:13 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
info->data_len = userlen + passlen + 1;
|
|
|
|
if (!(info->data = HeapAlloc(GetProcessHeap(), 0, info->data_len)))
|
|
|
|
{
|
|
|
|
status = RPC_S_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, id->User, id->UserLength, info->data, userlen, NULL, NULL);
|
|
|
|
info->data[userlen] = ':';
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, id->Password, id->PasswordLength, info->data + userlen + 1, passlen, NULL, NULL);
|
|
|
|
|
|
|
|
info->scheme = RPC_C_HTTP_AUTHN_SCHEME_BASIC;
|
|
|
|
info->finished = TRUE;
|
|
|
|
status = RPC_S_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RPC_C_HTTP_AUTHN_SCHEME_NTLM:
|
|
|
|
case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE:
|
2013-08-20 12:55:13 +02:00
|
|
|
{
|
2013-09-04 15:01:10 +02:00
|
|
|
|
|
|
|
static SEC_WCHAR ntlmW[] = {'N','T','L','M',0}, negotiateW[] = {'N','e','g','o','t','i','a','t','e',0};
|
|
|
|
SECURITY_STATUS ret;
|
|
|
|
SecBufferDesc out_desc, in_desc;
|
|
|
|
SecBuffer out, in;
|
|
|
|
ULONG flags = ISC_REQ_CONNECTION|ISC_REQ_USE_DCE_STYLE|ISC_REQ_MUTUAL_AUTH|ISC_REQ_DELEGATE;
|
|
|
|
SEC_WCHAR *scheme;
|
|
|
|
int scheme_len;
|
|
|
|
const WCHAR *p;
|
|
|
|
WCHAR auth_value[2048];
|
|
|
|
DWORD size = sizeof(auth_value);
|
|
|
|
BOOL first = FALSE;
|
|
|
|
|
|
|
|
if (creds->AuthnSchemes[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM) scheme = ntlmW;
|
|
|
|
else scheme = negotiateW;
|
2019-06-19 10:32:06 +02:00
|
|
|
scheme_len = lstrlenW( scheme );
|
2013-09-04 15:01:10 +02:00
|
|
|
|
|
|
|
if (!*auth_ptr)
|
|
|
|
{
|
|
|
|
TimeStamp exp;
|
|
|
|
SecPkgInfoW *pkg_info;
|
|
|
|
|
|
|
|
ret = AcquireCredentialsHandleW(NULL, scheme, SECPKG_CRED_OUTBOUND, NULL, id, NULL, NULL, &info->cred, &exp);
|
|
|
|
if (ret != SEC_E_OK) break;
|
|
|
|
|
|
|
|
ret = QuerySecurityPackageInfoW(scheme, &pkg_info);
|
|
|
|
if (ret != SEC_E_OK) break;
|
|
|
|
|
|
|
|
info->max_token = pkg_info->cbMaxToken;
|
|
|
|
FreeContextBuffer(pkg_info);
|
|
|
|
first = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (info->finished || !get_authvalue(request, creds->AuthnSchemes[0], auth_value, size)) break;
|
|
|
|
if (auth_scheme_from_header(auth_value) != info->scheme)
|
|
|
|
{
|
|
|
|
ERR("authentication scheme changed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
in.BufferType = SECBUFFER_TOKEN;
|
|
|
|
in.cbBuffer = 0;
|
|
|
|
in.pvBuffer = NULL;
|
|
|
|
|
|
|
|
in_desc.ulVersion = 0;
|
|
|
|
in_desc.cBuffers = 1;
|
|
|
|
in_desc.pBuffers = ∈
|
|
|
|
|
|
|
|
p = auth_value + scheme_len;
|
2013-09-11 13:49:40 +02:00
|
|
|
if (!first && *p == ' ')
|
2013-09-04 15:01:10 +02:00
|
|
|
{
|
2019-06-19 10:32:06 +02:00
|
|
|
int len = lstrlenW(++p);
|
2013-09-04 15:01:10 +02:00
|
|
|
in.cbBuffer = decode_base64(p, len, NULL);
|
|
|
|
if (!(in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer))) break;
|
|
|
|
decode_base64(p, len, in.pvBuffer);
|
|
|
|
}
|
|
|
|
out.BufferType = SECBUFFER_TOKEN;
|
|
|
|
out.cbBuffer = info->max_token;
|
|
|
|
if (!(out.pvBuffer = HeapAlloc(GetProcessHeap(), 0, out.cbBuffer)))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, in.pvBuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out_desc.ulVersion = 0;
|
|
|
|
out_desc.cBuffers = 1;
|
|
|
|
out_desc.pBuffers = &out;
|
|
|
|
|
|
|
|
ret = InitializeSecurityContextW(first ? &info->cred : NULL, first ? NULL : &info->ctx,
|
|
|
|
first ? servername : NULL, flags, 0, SECURITY_NETWORK_DREP,
|
|
|
|
in.pvBuffer ? &in_desc : NULL, 0, &info->ctx, &out_desc,
|
|
|
|
&info->attr, &info->exp);
|
|
|
|
HeapFree(GetProcessHeap(), 0, in.pvBuffer);
|
|
|
|
if (ret == SEC_E_OK)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, info->data);
|
|
|
|
info->data = out.pvBuffer;
|
|
|
|
info->data_len = out.cbBuffer;
|
|
|
|
info->finished = TRUE;
|
|
|
|
TRACE("sending last auth packet\n");
|
|
|
|
status = RPC_S_OK;
|
|
|
|
}
|
|
|
|
else if (ret == SEC_I_CONTINUE_NEEDED)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, info->data);
|
|
|
|
info->data = out.pvBuffer;
|
|
|
|
info->data_len = out.cbBuffer;
|
|
|
|
TRACE("sending next auth packet\n");
|
|
|
|
status = RPC_S_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret);
|
|
|
|
HeapFree(GetProcessHeap(), 0, out.pvBuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
info->scheme = creds->AuthnSchemes[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2013-08-20 12:55:13 +02:00
|
|
|
FIXME("scheme %u not supported\n", creds->AuthnSchemes[0]);
|
2013-09-04 15:01:10 +02:00
|
|
|
break;
|
2013-08-20 12:55:13 +02:00
|
|
|
}
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
if (status != RPC_S_OK)
|
|
|
|
{
|
|
|
|
destroy_authinfo(info);
|
|
|
|
*auth_ptr = NULL;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
*auth_ptr = info;
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
2013-08-20 12:55:13 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
static RPC_STATUS insert_authorization_header(HINTERNET request, ULONG scheme, char *data, int data_len)
|
|
|
|
{
|
|
|
|
static const WCHAR authW[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
|
|
|
|
static const WCHAR basicW[] = {'B','a','s','i','c',' '};
|
|
|
|
static const WCHAR negotiateW[] = {'N','e','g','o','t','i','a','t','e',' '};
|
|
|
|
static const WCHAR ntlmW[] = {'N','T','L','M',' '};
|
2018-10-19 22:11:34 +02:00
|
|
|
int scheme_len, auth_len = ARRAY_SIZE(authW), len = ((data_len + 2) * 4) / 3;
|
2013-09-04 15:01:10 +02:00
|
|
|
const WCHAR *scheme_str;
|
|
|
|
WCHAR *header, *ptr;
|
|
|
|
RPC_STATUS status = RPC_S_SERVER_UNAVAILABLE;
|
2013-08-20 12:55:13 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
switch (scheme)
|
2013-08-20 12:55:13 +02:00
|
|
|
{
|
2013-09-04 15:01:10 +02:00
|
|
|
case RPC_C_HTTP_AUTHN_SCHEME_BASIC:
|
|
|
|
scheme_str = basicW;
|
2018-10-19 22:11:34 +02:00
|
|
|
scheme_len = ARRAY_SIZE(basicW);
|
2013-09-04 15:01:10 +02:00
|
|
|
break;
|
|
|
|
case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE:
|
|
|
|
scheme_str = negotiateW;
|
2018-10-19 22:11:34 +02:00
|
|
|
scheme_len = ARRAY_SIZE(negotiateW);
|
2013-09-04 15:01:10 +02:00
|
|
|
break;
|
|
|
|
case RPC_C_HTTP_AUTHN_SCHEME_NTLM:
|
|
|
|
scheme_str = ntlmW;
|
2018-10-19 22:11:34 +02:00
|
|
|
scheme_len = ARRAY_SIZE(ntlmW);
|
2013-09-04 15:01:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR("unknown scheme %u\n", scheme);
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
if ((header = HeapAlloc(GetProcessHeap(), 0, (auth_len + scheme_len + len + 2) * sizeof(WCHAR))))
|
|
|
|
{
|
|
|
|
memcpy(header, authW, auth_len * sizeof(WCHAR));
|
|
|
|
ptr = header + auth_len;
|
|
|
|
memcpy(ptr, scheme_str, scheme_len * sizeof(WCHAR));
|
|
|
|
ptr += scheme_len;
|
|
|
|
len = encode_base64(data, data_len, ptr);
|
2013-08-20 12:55:13 +02:00
|
|
|
ptr[len++] = '\r';
|
|
|
|
ptr[len++] = '\n';
|
|
|
|
ptr[len] = 0;
|
2013-09-04 15:01:10 +02:00
|
|
|
if (HttpAddRequestHeadersW(request, header, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE))
|
|
|
|
status = RPC_S_OK;
|
2013-08-20 12:55:13 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, header);
|
|
|
|
}
|
2013-09-04 15:01:10 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-05-13 12:13:47 +02:00
|
|
|
static void drain_content(HINTERNET request, RpcHttpAsyncData *async_data, HANDLE cancel_event)
|
2013-09-04 15:01:10 +02:00
|
|
|
{
|
|
|
|
DWORD count, len = 0, size = sizeof(len);
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, &len, &size, NULL);
|
|
|
|
if (!len) return;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
count = min(sizeof(buf), len);
|
2015-05-13 12:13:47 +02:00
|
|
|
if (rpcrt4_http_async_read(request, async_data, cancel_event, buf, count) <= 0) return;
|
2013-09-04 15:01:10 +02:00
|
|
|
len -= count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS authorize_request(RpcConnection_http *httpc, HINTERNET request)
|
|
|
|
{
|
|
|
|
static const WCHAR authW[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
|
|
|
|
struct authinfo *info = NULL;
|
|
|
|
RPC_STATUS status;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
status = do_authorization(request, httpc->servername, httpc->common.QOS->qos->u.HttpCredentials, &info);
|
|
|
|
if (status != RPC_S_OK) break;
|
|
|
|
|
|
|
|
status = insert_authorization_header(request, info->scheme, info->data, info->data_len);
|
|
|
|
if (status != RPC_S_OK) break;
|
|
|
|
|
|
|
|
prepare_async_request(httpc->async_data);
|
|
|
|
ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
|
|
|
|
status = wait_async_request(httpc->async_data, ret, httpc->cancel_event);
|
|
|
|
if (status != RPC_S_OK || info->finished) break;
|
|
|
|
|
|
|
|
status = rpcrt4_http_check_response(request);
|
|
|
|
if (status != RPC_S_OK && status != ERROR_ACCESS_DENIED) break;
|
2015-05-13 12:13:47 +02:00
|
|
|
drain_content(request, httpc->async_data, httpc->cancel_event);
|
2013-09-04 15:01:10 +02:00
|
|
|
}
|
|
|
|
|
2013-09-11 13:49:05 +02:00
|
|
|
if (info->scheme != RPC_C_HTTP_AUTHN_SCHEME_BASIC)
|
2015-06-25 12:27:43 +02:00
|
|
|
HttpAddRequestHeadersW(request, authW, -1, HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD);
|
2013-09-11 13:49:05 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
destroy_authinfo(info);
|
2013-08-20 12:55:13 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
static BOOL has_credentials(RpcConnection_http *httpc)
|
|
|
|
{
|
|
|
|
RPC_HTTP_TRANSPORT_CREDENTIALS_W *creds;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY_W *id;
|
|
|
|
|
|
|
|
if (!httpc->common.QOS || httpc->common.QOS->qos->AdditionalSecurityInfoType != RPC_C_AUTHN_INFO_TYPE_HTTP)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
creds = httpc->common.QOS->qos->u.HttpCredentials;
|
|
|
|
if (creds->AuthenticationTarget != RPC_C_HTTP_AUTHN_TARGET_SERVER || !creds->NumberOfAuthnSchemes)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
id = creds->TransportCredentials;
|
|
|
|
if (!id || !id->User || !id->Password) return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL is_secure(RpcConnection_http *httpc)
|
|
|
|
{
|
|
|
|
return httpc->common.QOS &&
|
|
|
|
(httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
|
|
|
|
(httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL);
|
|
|
|
}
|
|
|
|
|
2015-05-21 10:46:44 +02:00
|
|
|
static RPC_STATUS set_auth_cookie(RpcConnection_http *httpc, const WCHAR *value)
|
|
|
|
{
|
|
|
|
static WCHAR httpW[] = {'h','t','t','p',0};
|
|
|
|
static WCHAR httpsW[] = {'h','t','t','p','s',0};
|
|
|
|
URL_COMPONENTSW uc;
|
|
|
|
DWORD len;
|
|
|
|
WCHAR *url;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (!value) return RPC_S_OK;
|
|
|
|
|
|
|
|
uc.dwStructSize = sizeof(uc);
|
|
|
|
uc.lpszScheme = is_secure(httpc) ? httpsW : httpW;
|
|
|
|
uc.dwSchemeLength = 0;
|
|
|
|
uc.lpszHostName = httpc->servername;
|
|
|
|
uc.dwHostNameLength = 0;
|
|
|
|
uc.nPort = 0;
|
|
|
|
uc.lpszUserName = NULL;
|
|
|
|
uc.dwUserNameLength = 0;
|
|
|
|
uc.lpszPassword = NULL;
|
|
|
|
uc.dwPasswordLength = 0;
|
|
|
|
uc.lpszUrlPath = NULL;
|
|
|
|
uc.dwUrlPathLength = 0;
|
|
|
|
uc.lpszExtraInfo = NULL;
|
|
|
|
uc.dwExtraInfoLength = 0;
|
|
|
|
|
|
|
|
if (!InternetCreateUrlW(&uc, 0, NULL, &len) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
|
|
|
|
if (!(url = HeapAlloc(GetProcessHeap(), 0, len))) return RPC_S_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
len = len / sizeof(WCHAR) - 1;
|
|
|
|
if (!InternetCreateUrlW(&uc, 0, url, &len))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = InternetSetCookieW(url, NULL, value);
|
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
|
|
|
if (!ret) return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
static RPC_STATUS rpcrt4_ncacn_http_open(RpcConnection* Connection)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *)Connection;
|
|
|
|
static const WCHAR wszVerbIn[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
|
|
|
|
static const WCHAR wszVerbOut[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
|
|
|
|
static const WCHAR wszRpcProxyPrefix[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
|
|
|
|
static const WCHAR wszColon[] = {':',0};
|
|
|
|
static const WCHAR wszAcceptType[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
|
|
|
|
LPCWSTR wszAcceptTypes[] = { wszAcceptType, NULL };
|
2013-05-31 16:04:15 +02:00
|
|
|
DWORD flags;
|
2009-03-18 12:13:19 +01:00
|
|
|
WCHAR *url;
|
|
|
|
RPC_STATUS status;
|
2013-09-04 15:01:10 +02:00
|
|
|
BOOL secure, credentials;
|
2009-03-18 12:13:19 +01:00
|
|
|
HttpTimerThreadData *timer_data;
|
|
|
|
HANDLE thread;
|
|
|
|
|
|
|
|
TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
|
|
|
|
|
|
|
|
if (Connection->server)
|
|
|
|
{
|
|
|
|
ERR("ncacn_http servers not supported yet\n");
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (httpc->in_request)
|
|
|
|
return RPC_S_OK;
|
|
|
|
|
|
|
|
httpc->async_data->completion_event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
|
2015-05-21 10:47:32 +02:00
|
|
|
UuidCreate(&httpc->connection_uuid);
|
|
|
|
UuidCreate(&httpc->in_pipe_uuid);
|
|
|
|
UuidCreate(&httpc->out_pipe_uuid);
|
2009-03-18 12:13:19 +01:00
|
|
|
|
|
|
|
status = rpcrt4_http_internet_connect(httpc);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
url = HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix) + (strlen(Connection->NetworkAddr) + 1 + strlen(Connection->Endpoint))*sizeof(WCHAR));
|
|
|
|
if (!url)
|
|
|
|
return RPC_S_OUT_OF_MEMORY;
|
|
|
|
memcpy(url, wszRpcProxyPrefix, sizeof(wszRpcProxyPrefix));
|
2018-10-19 22:11:34 +02:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, Connection->NetworkAddr, -1, url+ARRAY_SIZE(wszRpcProxyPrefix)-1,
|
|
|
|
strlen(Connection->NetworkAddr)+1);
|
2019-06-19 10:32:06 +02:00
|
|
|
lstrcatW(url, wszColon);
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, Connection->Endpoint, -1, url+lstrlenW(url), strlen(Connection->Endpoint)+1);
|
2009-03-18 12:13:19 +01:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
secure = is_secure(httpc);
|
|
|
|
credentials = has_credentials(httpc);
|
2009-03-18 12:13:19 +01:00
|
|
|
|
2013-08-28 13:31:23 +02:00
|
|
|
flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE |
|
|
|
|
INTERNET_FLAG_NO_AUTO_REDIRECT;
|
2013-05-31 16:04:15 +02:00
|
|
|
if (secure) flags |= INTERNET_FLAG_SECURE;
|
2013-09-04 15:01:10 +02:00
|
|
|
if (credentials) flags |= INTERNET_FLAG_NO_AUTH;
|
2013-05-31 16:04:15 +02:00
|
|
|
|
2015-05-21 10:46:44 +02:00
|
|
|
status = set_auth_cookie(httpc, Connection->CookieAuth);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
|
|
|
return status;
|
|
|
|
}
|
2013-05-31 16:04:15 +02:00
|
|
|
httpc->in_request = HttpOpenRequestW(httpc->session, wszVerbIn, url, NULL, NULL, wszAcceptTypes,
|
|
|
|
flags, (DWORD_PTR)httpc->async_data);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (!httpc->in_request)
|
|
|
|
{
|
|
|
|
ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
|
2012-08-15 10:22:56 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
2009-03-18 12:13:19 +01:00
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
2015-05-21 10:46:44 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
if (credentials)
|
|
|
|
{
|
|
|
|
status = authorize_request(httpc, httpc->in_request);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
status = rpcrt4_http_check_response(httpc->in_request);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
|
|
|
return status;
|
|
|
|
}
|
2015-05-13 12:13:47 +02:00
|
|
|
drain_content(httpc->in_request, httpc->async_data, httpc->cancel_event);
|
2013-09-04 15:01:10 +02:00
|
|
|
}
|
2013-08-28 13:30:03 +02:00
|
|
|
|
2013-05-31 16:04:15 +02:00
|
|
|
httpc->out_request = HttpOpenRequestW(httpc->session, wszVerbOut, url, NULL, NULL, wszAcceptTypes,
|
|
|
|
flags, (DWORD_PTR)httpc->async_data);
|
2012-08-15 10:22:56 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, url);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (!httpc->out_request)
|
|
|
|
{
|
|
|
|
ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
2013-08-28 13:30:03 +02:00
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
if (credentials)
|
|
|
|
{
|
|
|
|
status = authorize_request(httpc, httpc->out_request);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = rpcrt4_http_prepare_in_pipe(httpc->in_request, httpc->async_data, httpc->cancel_event,
|
|
|
|
&httpc->connection_uuid, &httpc->in_pipe_uuid,
|
|
|
|
&Connection->assoc->http_uuid, credentials);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (status != RPC_S_OK)
|
|
|
|
return status;
|
|
|
|
|
2013-09-04 15:01:10 +02:00
|
|
|
status = rpcrt4_http_prepare_out_pipe(httpc->out_request, httpc->async_data, httpc->cancel_event,
|
|
|
|
&httpc->connection_uuid, &httpc->out_pipe_uuid,
|
|
|
|
&httpc->flow_control_increment, credentials);
|
2009-03-18 12:13:19 +01:00
|
|
|
if (status != RPC_S_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
httpc->flow_control_mark = httpc->flow_control_increment / 2;
|
|
|
|
httpc->last_sent_time = GetTickCount();
|
|
|
|
httpc->timer_cancelled = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
|
|
|
|
timer_data = HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data));
|
|
|
|
if (!timer_data)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
timer_data->timer_param = httpc->in_request;
|
|
|
|
timer_data->last_sent_time = &httpc->last_sent_time;
|
|
|
|
timer_data->timer_cancelled = httpc->timer_cancelled;
|
|
|
|
/* FIXME: should use CreateTimerQueueTimer when implemented */
|
|
|
|
thread = CreateThread(NULL, 0, rpcrt4_http_timer_thread, timer_data, 0, NULL);
|
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, timer_data);
|
|
|
|
return GetLastError();
|
|
|
|
}
|
|
|
|
CloseHandle(thread);
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_http_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return RPC_S_SERVER_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_ncacn_http_read(RpcConnection *Connection,
|
|
|
|
void *buffer, unsigned int count)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *) Connection;
|
2015-05-13 12:13:47 +02:00
|
|
|
return rpcrt4_http_async_read(httpc->out_request, httpc->async_data, httpc->cancel_event, buffer, count);
|
2009-03-18 12:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_http_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *) Connection;
|
|
|
|
RPC_STATUS status;
|
|
|
|
DWORD hdr_length;
|
|
|
|
LONG dwRead;
|
|
|
|
RpcPktCommonHdr common_hdr;
|
|
|
|
|
|
|
|
*Header = NULL;
|
|
|
|
|
|
|
|
TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
|
|
|
|
|
|
|
|
again:
|
|
|
|
/* read packet common header */
|
|
|
|
dwRead = rpcrt4_ncacn_http_read(Connection, &common_hdr, sizeof(common_hdr));
|
|
|
|
if (dwRead != sizeof(common_hdr)) {
|
|
|
|
WARN("Short read of header, %d bytes\n", dwRead);
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!memcmp(&common_hdr, "HTTP/1.1", sizeof("HTTP/1.1")) ||
|
|
|
|
!memcmp(&common_hdr, "HTTP/1.0", sizeof("HTTP/1.0")))
|
|
|
|
{
|
|
|
|
FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr));
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = RPCRT4_ValidateCommonHeader(&common_hdr);
|
|
|
|
if (status != RPC_S_OK) goto fail;
|
|
|
|
|
|
|
|
hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
|
|
|
|
if (hdr_length == 0) {
|
|
|
|
WARN("header length == 0\n");
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
|
|
|
|
if (!*Header)
|
|
|
|
{
|
|
|
|
status = RPC_S_OUT_OF_RESOURCES;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memcpy(*Header, &common_hdr, sizeof(common_hdr));
|
|
|
|
|
|
|
|
/* read the rest of packet header */
|
|
|
|
dwRead = rpcrt4_ncacn_http_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
|
|
|
|
if (dwRead != hdr_length - sizeof(common_hdr)) {
|
|
|
|
WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (common_hdr.frag_len - hdr_length)
|
|
|
|
{
|
|
|
|
*Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
|
|
|
|
if (!*Payload)
|
|
|
|
{
|
|
|
|
status = RPC_S_OUT_OF_RESOURCES;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwRead = rpcrt4_ncacn_http_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
|
|
|
|
if (dwRead != common_hdr.frag_len - hdr_length)
|
|
|
|
{
|
|
|
|
WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*Payload = NULL;
|
|
|
|
|
|
|
|
if ((*Header)->common.ptype == PKT_HTTP)
|
|
|
|
{
|
|
|
|
if (!RPCRT4_IsValidHttpPacket(*Header, *Payload, common_hdr.frag_len - hdr_length))
|
|
|
|
{
|
|
|
|
ERR("invalid http packet of length %d bytes\n", (*Header)->common.frag_len);
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((*Header)->http.flags == 0x0001)
|
|
|
|
{
|
|
|
|
TRACE("http idle packet, waiting for real packet\n");
|
|
|
|
if ((*Header)->http.num_data_items != 0)
|
|
|
|
{
|
|
|
|
ERR("HTTP idle packet should have no data items instead of %d\n", (*Header)->http.num_data_items);
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((*Header)->http.flags == 0x0002)
|
|
|
|
{
|
|
|
|
ULONG bytes_transmitted;
|
|
|
|
ULONG flow_control_increment;
|
|
|
|
UUID pipe_uuid;
|
|
|
|
status = RPCRT4_ParseHttpFlowControlHeader(*Header, *Payload,
|
|
|
|
Connection->server,
|
|
|
|
&bytes_transmitted,
|
|
|
|
&flow_control_increment,
|
|
|
|
&pipe_uuid);
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
goto fail;
|
|
|
|
TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
|
|
|
|
bytes_transmitted, flow_control_increment, debugstr_guid(&pipe_uuid));
|
|
|
|
/* FIXME: do something with parsed data */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FIXME("unrecognised http packet with flags 0x%04x\n", (*Header)->http.flags);
|
|
|
|
status = RPC_S_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
RPCRT4_FreeHeader(*Header);
|
|
|
|
*Header = NULL;
|
|
|
|
HeapFree(GetProcessHeap(), 0, *Payload);
|
|
|
|
*Payload = NULL;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
status = RPC_S_OK;
|
|
|
|
|
|
|
|
httpc->bytes_received += common_hdr.frag_len;
|
|
|
|
|
|
|
|
TRACE("httpc->bytes_received = 0x%x\n", httpc->bytes_received);
|
|
|
|
|
|
|
|
if (httpc->bytes_received > httpc->flow_control_mark)
|
|
|
|
{
|
|
|
|
RpcPktHdr *hdr = RPCRT4_BuildHttpFlowControlHeader(httpc->common.server,
|
|
|
|
httpc->bytes_received,
|
|
|
|
httpc->flow_control_increment,
|
|
|
|
&httpc->out_pipe_uuid);
|
|
|
|
if (hdr)
|
|
|
|
{
|
|
|
|
DWORD bytes_written;
|
|
|
|
BOOL ret2;
|
|
|
|
TRACE("sending flow control packet at 0x%x\n", httpc->bytes_received);
|
|
|
|
ret2 = InternetWriteFile(httpc->in_request, hdr, hdr->common.frag_len, &bytes_written);
|
|
|
|
RPCRT4_FreeHeader(hdr);
|
|
|
|
if (ret2)
|
|
|
|
httpc->flow_control_mark = httpc->bytes_received + httpc->flow_control_increment / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (status != RPC_S_OK) {
|
|
|
|
RPCRT4_FreeHeader(*Header);
|
|
|
|
*Header = NULL;
|
|
|
|
HeapFree(GetProcessHeap(), 0, *Payload);
|
|
|
|
*Payload = NULL;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_ncacn_http_write(RpcConnection *Connection,
|
|
|
|
const void *buffer, unsigned int count)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *) Connection;
|
|
|
|
DWORD bytes_written;
|
|
|
|
BOOL ret;
|
|
|
|
|
2009-05-28 11:56:30 +02:00
|
|
|
httpc->last_sent_time = ~0U; /* disable idle packet sending */
|
2009-03-18 12:13:19 +01:00
|
|
|
ret = InternetWriteFile(httpc->in_request, buffer, count, &bytes_written);
|
|
|
|
httpc->last_sent_time = GetTickCount();
|
|
|
|
TRACE("%p %p %u -> %s\n", httpc->in_request, buffer, count, ret ? "TRUE" : "FALSE");
|
|
|
|
return ret ? bytes_written : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpcrt4_ncacn_http_close(RpcConnection *Connection)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *) Connection;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
SetEvent(httpc->timer_cancelled);
|
|
|
|
if (httpc->in_request)
|
|
|
|
InternetCloseHandle(httpc->in_request);
|
|
|
|
httpc->in_request = NULL;
|
|
|
|
if (httpc->out_request)
|
|
|
|
InternetCloseHandle(httpc->out_request);
|
|
|
|
httpc->out_request = NULL;
|
|
|
|
if (httpc->app_info)
|
|
|
|
InternetCloseHandle(httpc->app_info);
|
|
|
|
httpc->app_info = NULL;
|
|
|
|
if (httpc->session)
|
|
|
|
InternetCloseHandle(httpc->session);
|
|
|
|
httpc->session = NULL;
|
|
|
|
RpcHttpAsyncData_Release(httpc->async_data);
|
|
|
|
if (httpc->cancel_event)
|
|
|
|
CloseHandle(httpc->cancel_event);
|
2013-09-04 15:01:10 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, httpc->servername);
|
|
|
|
httpc->servername = NULL;
|
2009-03-18 12:13:19 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-24 13:52:07 +02:00
|
|
|
static void rpcrt4_ncacn_http_close_read(RpcConnection *conn)
|
|
|
|
{
|
|
|
|
rpcrt4_ncacn_http_close(conn); /* FIXME */
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
static void rpcrt4_ncacn_http_cancel_call(RpcConnection *Connection)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *) Connection;
|
|
|
|
|
|
|
|
SetEvent(httpc->cancel_event);
|
|
|
|
}
|
|
|
|
|
2015-10-27 19:28:28 +01:00
|
|
|
static RPC_STATUS rpcrt4_ncacn_http_is_server_listening(const char *endpoint)
|
|
|
|
{
|
|
|
|
FIXME("\n");
|
|
|
|
return RPC_S_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
2009-03-18 12:13:19 +01:00
|
|
|
static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection *Connection)
|
|
|
|
{
|
|
|
|
RpcConnection_http *httpc = (RpcConnection_http *) Connection;
|
2012-08-14 13:35:17 +02:00
|
|
|
BOOL ret;
|
|
|
|
RPC_STATUS status;
|
2009-03-18 12:13:19 +01:00
|
|
|
|
2012-08-14 13:35:17 +02:00
|
|
|
prepare_async_request(httpc->async_data);
|
2009-03-18 12:13:19 +01:00
|
|
|
ret = InternetQueryDataAvailable(httpc->out_request,
|
|
|
|
&httpc->async_data->inet_buffers.dwBufferLength, IRF_ASYNC, 0);
|
2012-08-14 13:35:17 +02:00
|
|
|
status = wait_async_request(httpc->async_data, ret, httpc->cancel_event);
|
|
|
|
return status == RPC_S_OK ? 0 : -1;
|
2009-03-18 12:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data,
|
|
|
|
const char *networkaddr,
|
|
|
|
const char *endpoint)
|
|
|
|
{
|
|
|
|
return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
|
|
|
|
EPM_PROTOCOL_HTTP, endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RPC_STATUS rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data,
|
|
|
|
size_t tower_size,
|
|
|
|
char **networkaddr,
|
|
|
|
char **endpoint)
|
|
|
|
{
|
|
|
|
return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
|
|
|
|
networkaddr, EPM_PROTOCOL_HTTP,
|
|
|
|
endpoint);
|
|
|
|
}
|
|
|
|
|
2006-10-25 14:28:56 +02:00
|
|
|
static const struct connection_ops conn_protseq_list[] = {
|
2006-04-21 08:38:57 +02:00
|
|
|
{ "ncacn_np",
|
2006-06-01 21:25:06 +02:00
|
|
|
{ EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
|
2006-04-21 08:40:06 +02:00
|
|
|
rpcrt4_conn_np_alloc,
|
2006-05-18 18:27:35 +02:00
|
|
|
rpcrt4_ncacn_np_open,
|
2006-11-08 21:45:19 +01:00
|
|
|
rpcrt4_ncacn_np_handoff,
|
2006-04-21 08:38:57 +02:00
|
|
|
rpcrt4_conn_np_read,
|
|
|
|
rpcrt4_conn_np_write,
|
|
|
|
rpcrt4_conn_np_close,
|
2017-05-24 13:52:07 +02:00
|
|
|
rpcrt4_conn_np_close_read,
|
2007-11-12 21:10:19 +01:00
|
|
|
rpcrt4_conn_np_cancel_call,
|
2015-10-27 19:28:28 +01:00
|
|
|
rpcrt4_ncacn_np_is_server_listening,
|
2008-01-21 11:31:40 +01:00
|
|
|
rpcrt4_conn_np_wait_for_incoming_data,
|
2006-06-01 21:25:06 +02:00
|
|
|
rpcrt4_ncacn_np_get_top_of_tower,
|
|
|
|
rpcrt4_ncacn_np_parse_top_of_tower,
|
2009-03-18 12:13:19 +01:00
|
|
|
NULL,
|
2009-12-13 22:35:47 +01:00
|
|
|
RPCRT4_default_is_authorized,
|
|
|
|
RPCRT4_default_authorize,
|
|
|
|
RPCRT4_default_secure_packet,
|
2009-12-13 22:36:01 +01:00
|
|
|
rpcrt4_conn_np_impersonate_client,
|
|
|
|
rpcrt4_conn_np_revert_to_self,
|
2009-12-13 22:36:05 +01:00
|
|
|
RPCRT4_default_inquire_auth_client,
|
2006-04-21 08:38:57 +02:00
|
|
|
},
|
|
|
|
{ "ncalrpc",
|
2006-06-01 21:25:06 +02:00
|
|
|
{ EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
|
2006-04-21 08:40:06 +02:00
|
|
|
rpcrt4_conn_np_alloc,
|
2006-05-18 18:27:35 +02:00
|
|
|
rpcrt4_ncalrpc_open,
|
2006-11-08 21:45:19 +01:00
|
|
|
rpcrt4_ncalrpc_handoff,
|
2006-04-21 08:38:57 +02:00
|
|
|
rpcrt4_conn_np_read,
|
|
|
|
rpcrt4_conn_np_write,
|
|
|
|
rpcrt4_conn_np_close,
|
2017-05-24 13:52:07 +02:00
|
|
|
rpcrt4_conn_np_close_read,
|
2007-11-12 21:10:19 +01:00
|
|
|
rpcrt4_conn_np_cancel_call,
|
2015-10-27 19:28:28 +01:00
|
|
|
rpcrt4_ncalrpc_np_is_server_listening,
|
2008-01-21 11:31:40 +01:00
|
|
|
rpcrt4_conn_np_wait_for_incoming_data,
|
2006-06-01 21:25:06 +02:00
|
|
|
rpcrt4_ncalrpc_get_top_of_tower,
|
|
|
|
rpcrt4_ncalrpc_parse_top_of_tower,
|
2009-03-18 12:13:19 +01:00
|
|
|
NULL,
|
2009-12-13 22:35:54 +01:00
|
|
|
rpcrt4_ncalrpc_is_authorized,
|
|
|
|
rpcrt4_ncalrpc_authorize,
|
|
|
|
rpcrt4_ncalrpc_secure_packet,
|
2009-12-13 22:36:01 +01:00
|
|
|
rpcrt4_conn_np_impersonate_client,
|
|
|
|
rpcrt4_conn_np_revert_to_self,
|
2009-12-13 22:36:09 +01:00
|
|
|
rpcrt4_ncalrpc_inquire_auth_client,
|
2006-04-21 08:38:57 +02:00
|
|
|
},
|
2006-04-21 09:07:35 +02:00
|
|
|
{ "ncacn_ip_tcp",
|
2006-06-01 21:25:06 +02:00
|
|
|
{ EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
|
2006-04-21 09:07:35 +02:00
|
|
|
rpcrt4_conn_tcp_alloc,
|
|
|
|
rpcrt4_ncacn_ip_tcp_open,
|
|
|
|
rpcrt4_conn_tcp_handoff,
|
|
|
|
rpcrt4_conn_tcp_read,
|
|
|
|
rpcrt4_conn_tcp_write,
|
|
|
|
rpcrt4_conn_tcp_close,
|
2017-05-24 13:52:07 +02:00
|
|
|
rpcrt4_conn_tcp_close_read,
|
2007-11-12 21:10:19 +01:00
|
|
|
rpcrt4_conn_tcp_cancel_call,
|
2015-10-27 19:28:28 +01:00
|
|
|
rpcrt4_conn_tcp_is_server_listening,
|
2008-01-21 11:31:40 +01:00
|
|
|
rpcrt4_conn_tcp_wait_for_incoming_data,
|
2006-06-01 21:25:06 +02:00
|
|
|
rpcrt4_ncacn_ip_tcp_get_top_of_tower,
|
|
|
|
rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
|
2009-03-18 12:13:19 +01:00
|
|
|
NULL,
|
2009-12-13 22:35:47 +01:00
|
|
|
RPCRT4_default_is_authorized,
|
|
|
|
RPCRT4_default_authorize,
|
|
|
|
RPCRT4_default_secure_packet,
|
2009-12-13 22:36:01 +01:00
|
|
|
RPCRT4_default_impersonate_client,
|
|
|
|
RPCRT4_default_revert_to_self,
|
2009-12-13 22:36:05 +01:00
|
|
|
RPCRT4_default_inquire_auth_client,
|
2009-03-18 12:13:19 +01:00
|
|
|
},
|
|
|
|
{ "ncacn_http",
|
|
|
|
{ EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP },
|
|
|
|
rpcrt4_ncacn_http_alloc,
|
|
|
|
rpcrt4_ncacn_http_open,
|
|
|
|
rpcrt4_ncacn_http_handoff,
|
|
|
|
rpcrt4_ncacn_http_read,
|
|
|
|
rpcrt4_ncacn_http_write,
|
|
|
|
rpcrt4_ncacn_http_close,
|
2017-05-24 13:52:07 +02:00
|
|
|
rpcrt4_ncacn_http_close_read,
|
2009-03-18 12:13:19 +01:00
|
|
|
rpcrt4_ncacn_http_cancel_call,
|
2015-10-27 19:28:28 +01:00
|
|
|
rpcrt4_ncacn_http_is_server_listening,
|
2009-03-18 12:13:19 +01:00
|
|
|
rpcrt4_ncacn_http_wait_for_incoming_data,
|
|
|
|
rpcrt4_ncacn_http_get_top_of_tower,
|
|
|
|
rpcrt4_ncacn_http_parse_top_of_tower,
|
|
|
|
rpcrt4_ncacn_http_receive_fragment,
|
2009-12-13 22:35:47 +01:00
|
|
|
RPCRT4_default_is_authorized,
|
|
|
|
RPCRT4_default_authorize,
|
|
|
|
RPCRT4_default_secure_packet,
|
2009-12-13 22:36:01 +01:00
|
|
|
RPCRT4_default_impersonate_client,
|
|
|
|
RPCRT4_default_revert_to_self,
|
2009-12-13 22:36:05 +01:00
|
|
|
RPCRT4_default_inquire_auth_client,
|
2009-03-18 12:13:19 +01:00
|
|
|
},
|
2006-04-21 08:38:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-10-25 14:28:56 +02:00
|
|
|
static const struct protseq_ops protseq_list[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"ncacn_np",
|
|
|
|
rpcrt4_protseq_np_alloc,
|
|
|
|
rpcrt4_protseq_np_signal_state_changed,
|
|
|
|
rpcrt4_protseq_np_get_wait_array,
|
|
|
|
rpcrt4_protseq_np_free_wait_array,
|
|
|
|
rpcrt4_protseq_np_wait_for_new_connection,
|
2006-11-08 21:45:19 +01:00
|
|
|
rpcrt4_protseq_ncacn_np_open_endpoint,
|
2006-10-25 14:28:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"ncalrpc",
|
|
|
|
rpcrt4_protseq_np_alloc,
|
|
|
|
rpcrt4_protseq_np_signal_state_changed,
|
|
|
|
rpcrt4_protseq_np_get_wait_array,
|
|
|
|
rpcrt4_protseq_np_free_wait_array,
|
|
|
|
rpcrt4_protseq_np_wait_for_new_connection,
|
2006-11-08 21:45:19 +01:00
|
|
|
rpcrt4_protseq_ncalrpc_open_endpoint,
|
2006-10-25 14:28:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"ncacn_ip_tcp",
|
2006-10-31 19:59:55 +01:00
|
|
|
rpcrt4_protseq_sock_alloc,
|
|
|
|
rpcrt4_protseq_sock_signal_state_changed,
|
|
|
|
rpcrt4_protseq_sock_get_wait_array,
|
|
|
|
rpcrt4_protseq_sock_free_wait_array,
|
|
|
|
rpcrt4_protseq_sock_wait_for_new_connection,
|
2006-11-08 21:45:19 +01:00
|
|
|
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
|
2006-10-25 14:28:56 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2008-11-06 22:17:53 +01:00
|
|
|
unsigned int i;
|
2018-10-19 22:11:35 +02:00
|
|
|
for(i = 0; i < ARRAY_SIZE(protseq_list); i++)
|
2006-04-21 08:38:57 +02:00
|
|
|
if (!strcmp(protseq_list[i].name, protseq))
|
|
|
|
return &protseq_list[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-25 14:28:56 +02:00
|
|
|
static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
|
|
|
|
{
|
2008-11-06 22:17:53 +01:00
|
|
|
unsigned int i;
|
2018-10-19 22:11:35 +02:00
|
|
|
for(i = 0; i < ARRAY_SIZE(conn_protseq_list); i++)
|
2006-10-25 14:28:56 +02:00
|
|
|
if (!strcmp(conn_protseq_list[i].name, protseq))
|
|
|
|
return &conn_protseq_list[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-04-21 11:20:16 +02:00
|
|
|
/**** interface to rest of code ****/
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
|
|
|
TRACE("(Connection == ^%p)\n", Connection);
|
|
|
|
|
2006-11-08 21:45:19 +01:00
|
|
|
assert(!Connection->server);
|
|
|
|
return Connection->ops->open_connection_client(Connection);
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
|
|
|
|
{
|
|
|
|
TRACE("(Connection == ^%p)\n", Connection);
|
2006-12-18 00:45:22 +01:00
|
|
|
if (SecIsValidHandle(&Connection->ctx))
|
|
|
|
{
|
|
|
|
DeleteSecurityContext(&Connection->ctx);
|
|
|
|
SecInvalidateHandle(&Connection->ctx);
|
|
|
|
}
|
2006-04-21 08:38:57 +02:00
|
|
|
rpcrt4_conn_close(Connection);
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-18 04:40:42 +02:00
|
|
|
RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
|
|
|
|
LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
|
2013-08-28 13:30:03 +02:00
|
|
|
LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS, LPCWSTR CookieAuth)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2009-08-18 20:15:01 +02:00
|
|
|
static LONG next_id;
|
2006-10-16 17:47:39 +02:00
|
|
|
const struct connection_ops *ops;
|
2006-04-21 08:38:57 +02:00
|
|
|
RpcConnection* NewConnection;
|
|
|
|
|
2006-10-25 14:28:56 +02:00
|
|
|
ops = rpcrt4_get_conn_protseq_ops(Protseq);
|
2006-04-21 08:38:57 +02:00
|
|
|
if (!ops)
|
2006-11-15 02:00:06 +01:00
|
|
|
{
|
|
|
|
FIXME("not supported for protseq %s\n", Protseq);
|
2006-04-21 08:38:57 +02:00
|
|
|
return RPC_S_PROTSEQ_NOT_SUPPORTED;
|
2006-11-15 02:00:06 +01:00
|
|
|
}
|
2006-04-21 08:38:57 +02:00
|
|
|
|
2006-04-21 08:40:06 +02:00
|
|
|
NewConnection = ops->alloc();
|
2012-02-14 16:19:32 +01:00
|
|
|
NewConnection->ref = 1;
|
2006-04-21 08:38:57 +02:00
|
|
|
NewConnection->server = server;
|
|
|
|
NewConnection->ops = ops;
|
|
|
|
NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
|
|
|
|
NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
|
2007-01-25 11:23:11 +01:00
|
|
|
NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
|
2013-08-28 13:30:03 +02:00
|
|
|
NewConnection->CookieAuth = RPCRT4_strdupW(CookieAuth);
|
2006-04-21 08:38:57 +02:00
|
|
|
NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
|
2006-05-18 04:39:42 +02:00
|
|
|
NewConnection->NextCallId = 1;
|
2006-10-03 15:40:57 +02:00
|
|
|
|
2006-11-15 02:01:21 +01:00
|
|
|
SecInvalidateHandle(&NewConnection->ctx);
|
2006-05-18 04:40:42 +02:00
|
|
|
if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
|
|
|
|
NewConnection->AuthInfo = AuthInfo;
|
2009-08-18 20:15:01 +02:00
|
|
|
NewConnection->auth_context_id = InterlockedIncrement( &next_id );
|
2007-01-19 13:59:35 +01:00
|
|
|
if (QOS) RpcQualityOfService_AddRef(QOS);
|
|
|
|
NewConnection->QOS = QOS;
|
2007-03-26 19:17:15 +02:00
|
|
|
|
2006-06-07 21:12:34 +02:00
|
|
|
list_init(&NewConnection->conn_pool_entry);
|
2017-05-16 17:48:01 +02:00
|
|
|
list_init(&NewConnection->protseq_entry);
|
2006-04-21 08:38:57 +02:00
|
|
|
|
|
|
|
TRACE("connection: %p\n", NewConnection);
|
|
|
|
*Connection = NewConnection;
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:39:39 +02:00
|
|
|
static RpcConnection *rpcrt4_spawn_connection(RpcConnection *old_connection)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2017-05-23 17:39:39 +02:00
|
|
|
RpcConnection *connection;
|
|
|
|
RPC_STATUS err;
|
2006-04-21 08:38:57 +02:00
|
|
|
|
2017-05-23 17:39:39 +02:00
|
|
|
err = RPCRT4_CreateConnection(&connection, old_connection->server, rpcrt4_conn_get_name(old_connection),
|
|
|
|
old_connection->NetworkAddr, old_connection->Endpoint, NULL,
|
|
|
|
old_connection->AuthInfo, old_connection->QOS, old_connection->CookieAuth);
|
|
|
|
if (err != RPC_S_OK)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rpcrt4_conn_handoff(old_connection, connection);
|
|
|
|
if (old_connection->protseq)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&old_connection->protseq->cs);
|
|
|
|
connection->protseq = old_connection->protseq;
|
|
|
|
list_add_tail(&old_connection->protseq->connections, &connection->protseq_entry);
|
|
|
|
LeaveCriticalSection(&old_connection->protseq->cs);
|
|
|
|
}
|
|
|
|
return connection;
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 17:30:40 +02:00
|
|
|
void rpcrt4_conn_release_and_wait(RpcConnection *connection)
|
|
|
|
{
|
|
|
|
HANDLE event = NULL;
|
|
|
|
|
|
|
|
if (connection->ref > 1)
|
|
|
|
event = connection->wait_release = CreateEventW(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
|
|
|
RPCRT4_ReleaseConnection(connection);
|
|
|
|
|
|
|
|
if(event)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(event, INFINITE);
|
|
|
|
CloseHandle(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:16:50 +02:00
|
|
|
RpcConnection *RPCRT4_GrabConnection(RpcConnection *connection)
|
2006-04-21 08:38:57 +02:00
|
|
|
{
|
2017-06-08 14:16:50 +02:00
|
|
|
LONG ref = InterlockedIncrement(&connection->ref);
|
|
|
|
TRACE("%p ref=%u\n", connection, ref);
|
|
|
|
return connection;
|
2012-02-14 16:19:32 +01:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:16:56 +02:00
|
|
|
void RPCRT4_ReleaseConnection(RpcConnection *connection)
|
2012-02-14 16:19:32 +01:00
|
|
|
{
|
2017-08-02 15:51:09 +02:00
|
|
|
LONG ref;
|
2012-02-14 16:19:32 +01:00
|
|
|
|
2017-08-02 15:51:09 +02:00
|
|
|
/* protseq stores a list of active connections, but does not own references to them.
|
|
|
|
* It may need to grab a connection from the list, which could lead to a race if
|
|
|
|
* connection is being released, but not yet removed from the list. We handle that
|
|
|
|
* by synchronizing on CS here. */
|
|
|
|
if (connection->protseq)
|
2017-06-08 14:16:50 +02:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&connection->protseq->cs);
|
2017-08-02 15:51:09 +02:00
|
|
|
ref = InterlockedDecrement(&connection->ref);
|
2017-06-08 14:16:50 +02:00
|
|
|
if (!ref)
|
|
|
|
list_remove(&connection->protseq_entry);
|
|
|
|
LeaveCriticalSection(&connection->protseq->cs);
|
|
|
|
}
|
2017-08-02 15:51:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ref = InterlockedDecrement(&connection->ref);
|
|
|
|
}
|
2006-04-21 08:38:57 +02:00
|
|
|
|
2017-06-08 14:16:50 +02:00
|
|
|
TRACE("%p ref=%u\n", connection, ref);
|
2007-12-16 13:07:30 +01:00
|
|
|
|
2017-06-08 14:16:50 +02:00
|
|
|
if (!ref)
|
|
|
|
{
|
|
|
|
RPCRT4_CloseConnection(connection);
|
|
|
|
RPCRT4_strfree(connection->Endpoint);
|
|
|
|
RPCRT4_strfree(connection->NetworkAddr);
|
|
|
|
HeapFree(GetProcessHeap(), 0, connection->NetworkOptions);
|
|
|
|
HeapFree(GetProcessHeap(), 0, connection->CookieAuth);
|
|
|
|
if (connection->AuthInfo) RpcAuthInfo_Release(connection->AuthInfo);
|
|
|
|
if (connection->QOS) RpcQualityOfService_Release(connection->QOS);
|
2007-12-16 13:07:30 +01:00
|
|
|
|
2017-06-08 14:16:50 +02:00
|
|
|
/* server-only */
|
|
|
|
if (connection->server_binding) RPCRT4_ReleaseBinding(connection->server_binding);
|
2018-10-16 14:38:35 +02:00
|
|
|
else if (connection->assoc) RpcAssoc_ConnectionReleased(connection->assoc);
|
2017-05-23 17:39:30 +02:00
|
|
|
|
2017-06-08 14:16:50 +02:00
|
|
|
if (connection->wait_release) SetEvent(connection->wait_release);
|
2017-05-31 17:30:40 +02:00
|
|
|
|
2017-06-08 14:16:50 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, connection);
|
|
|
|
}
|
2006-04-21 08:38:57 +02:00
|
|
|
}
|
2006-05-18 12:06:30 +02:00
|
|
|
|
2015-10-27 19:28:28 +01:00
|
|
|
RPC_STATUS RPCRT4_IsServerListening(const char *protseq, const char *endpoint)
|
|
|
|
{
|
|
|
|
const struct connection_ops *ops;
|
|
|
|
|
|
|
|
ops = rpcrt4_get_conn_protseq_ops(protseq);
|
|
|
|
if (!ops)
|
|
|
|
{
|
|
|
|
FIXME("not supported for protseq %s\n", protseq);
|
|
|
|
return RPC_S_INVALID_BINDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops->is_server_listening(endpoint);
|
|
|
|
}
|
|
|
|
|
2006-06-01 21:25:06 +02:00
|
|
|
RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
|
|
|
|
size_t *tower_size,
|
|
|
|
const char *protseq,
|
|
|
|
const char *networkaddr,
|
|
|
|
const char *endpoint)
|
|
|
|
{
|
|
|
|
twr_empty_floor_t *protocol_floor;
|
2006-10-25 14:28:56 +02:00
|
|
|
const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
|
2006-06-01 21:25:06 +02:00
|
|
|
|
|
|
|
*tower_size = 0;
|
|
|
|
|
|
|
|
if (!protseq_ops)
|
|
|
|
return RPC_S_INVALID_RPC_PROTSEQ;
|
|
|
|
|
|
|
|
if (!tower_data)
|
|
|
|
{
|
|
|
|
*tower_size = sizeof(*protocol_floor);
|
|
|
|
*tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol_floor = (twr_empty_floor_t *)tower_data;
|
|
|
|
protocol_floor->count_lhs = sizeof(protocol_floor->protid);
|
|
|
|
protocol_floor->protid = protseq_ops->epm_protocols[0];
|
|
|
|
protocol_floor->count_rhs = 0;
|
|
|
|
|
|
|
|
tower_data += sizeof(*protocol_floor);
|
|
|
|
|
|
|
|
*tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
|
|
|
|
if (!*tower_size)
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
*tower_size += sizeof(*protocol_floor);
|
|
|
|
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
|
|
|
|
size_t tower_size,
|
|
|
|
char **protseq,
|
|
|
|
char **networkaddr,
|
|
|
|
char **endpoint)
|
|
|
|
{
|
2006-09-25 22:26:52 +02:00
|
|
|
const twr_empty_floor_t *protocol_floor;
|
|
|
|
const twr_empty_floor_t *floor4;
|
2006-10-16 17:47:39 +02:00
|
|
|
const struct connection_ops *protseq_ops = NULL;
|
2006-06-01 21:25:06 +02:00
|
|
|
RPC_STATUS status;
|
2008-11-06 22:17:53 +01:00
|
|
|
unsigned int i;
|
2006-06-01 21:25:06 +02:00
|
|
|
|
|
|
|
if (tower_size < sizeof(*protocol_floor))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
2006-09-25 22:26:52 +02:00
|
|
|
protocol_floor = (const twr_empty_floor_t *)tower_data;
|
2006-06-01 21:25:06 +02:00
|
|
|
tower_data += sizeof(*protocol_floor);
|
|
|
|
tower_size -= sizeof(*protocol_floor);
|
|
|
|
if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
|
|
|
|
(protocol_floor->count_rhs > tower_size))
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
2006-06-02 21:45:09 +02:00
|
|
|
tower_data += protocol_floor->count_rhs;
|
|
|
|
tower_size -= protocol_floor->count_rhs;
|
2006-06-01 21:25:06 +02:00
|
|
|
|
2006-09-25 22:26:52 +02:00
|
|
|
floor4 = (const twr_empty_floor_t *)tower_data;
|
2006-06-01 21:25:06 +02:00
|
|
|
if ((tower_size < sizeof(*floor4)) ||
|
2006-06-02 21:45:09 +02:00
|
|
|
(floor4->count_lhs != sizeof(floor4->protid)))
|
2006-06-01 21:25:06 +02:00
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
2018-10-19 22:11:35 +02:00
|
|
|
for(i = 0; i < ARRAY_SIZE(conn_protseq_list); i++)
|
2006-10-25 14:28:56 +02:00
|
|
|
if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
|
|
|
|
(floor4->protid == conn_protseq_list[i].epm_protocols[1]))
|
2006-06-01 21:25:06 +02:00
|
|
|
{
|
2006-10-25 14:28:56 +02:00
|
|
|
protseq_ops = &conn_protseq_list[i];
|
2006-06-01 21:25:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!protseq_ops)
|
|
|
|
return EPT_S_NOT_REGISTERED;
|
|
|
|
|
|
|
|
status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
|
|
|
|
|
2006-06-01 21:25:26 +02:00
|
|
|
if ((status == RPC_S_OK) && protseq)
|
2006-06-01 21:25:06 +02:00
|
|
|
{
|
2006-10-03 15:40:48 +02:00
|
|
|
*protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
|
2006-06-01 21:25:06 +02:00
|
|
|
strcpy(*protseq, protseq_ops->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-05-18 12:06:30 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* RpcNetworkIsProtseqValidW (RPCRT4.@)
|
|
|
|
*
|
|
|
|
* Checks if the given protocol sequence is known by the RPC system.
|
|
|
|
* If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
|
|
|
|
*
|
|
|
|
*/
|
2006-10-13 02:19:15 +02:00
|
|
|
RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
|
2006-05-18 12:06:30 +02:00
|
|
|
{
|
|
|
|
char ps[0x10];
|
|
|
|
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, protseq, -1,
|
|
|
|
ps, sizeof ps, NULL, NULL);
|
2006-10-25 14:28:56 +02:00
|
|
|
if (rpcrt4_get_conn_protseq_ops(ps))
|
2006-05-18 12:06:30 +02:00
|
|
|
return RPC_S_OK;
|
|
|
|
|
|
|
|
FIXME("Unknown protseq %s\n", debugstr_w(protseq));
|
|
|
|
|
|
|
|
return RPC_S_INVALID_RPC_PROTSEQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RpcNetworkIsProtseqValidA (RPCRT4.@)
|
|
|
|
*/
|
2006-10-13 02:19:15 +02:00
|
|
|
RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
|
2006-05-18 12:06:30 +02:00
|
|
|
{
|
|
|
|
UNICODE_STRING protseqW;
|
|
|
|
|
|
|
|
if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
|
|
|
|
{
|
|
|
|
RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
|
|
|
|
RtlFreeUnicodeString(&protseqW);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return RPC_S_OUT_OF_MEMORY;
|
|
|
|
}
|
2011-06-19 18:06:08 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RpcProtseqVectorFreeA (RPCRT4.@)
|
|
|
|
*/
|
|
|
|
RPC_STATUS WINAPI RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA **protseqs)
|
|
|
|
{
|
|
|
|
TRACE("(%p)\n", protseqs);
|
|
|
|
|
|
|
|
if (*protseqs)
|
|
|
|
{
|
2013-02-25 22:13:56 +01:00
|
|
|
unsigned int i;
|
2011-06-19 18:06:08 +02:00
|
|
|
for (i = 0; i < (*protseqs)->Count; i++)
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*protseqs)->Protseq[i]);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *protseqs);
|
|
|
|
*protseqs = NULL;
|
|
|
|
}
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RpcProtseqVectorFreeW (RPCRT4.@)
|
|
|
|
*/
|
|
|
|
RPC_STATUS WINAPI RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW **protseqs)
|
|
|
|
{
|
|
|
|
TRACE("(%p)\n", protseqs);
|
|
|
|
|
|
|
|
if (*protseqs)
|
|
|
|
{
|
2013-02-25 22:13:56 +01:00
|
|
|
unsigned int i;
|
2011-06-19 18:06:08 +02:00
|
|
|
for (i = 0; i < (*protseqs)->Count; i++)
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*protseqs)->Protseq[i]);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *protseqs);
|
|
|
|
*protseqs = NULL;
|
|
|
|
}
|
|
|
|
return RPC_S_OK;
|
|
|
|
}
|
2011-06-23 07:15:40 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RpcNetworkInqProtseqsW (RPCRT4.@)
|
|
|
|
*/
|
|
|
|
RPC_STATUS WINAPI RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW** protseqs )
|
|
|
|
{
|
|
|
|
RPC_PROTSEQ_VECTORW *pvector;
|
2013-02-25 22:13:56 +01:00
|
|
|
unsigned int i;
|
2011-06-23 07:15:40 +02:00
|
|
|
RPC_STATUS status = RPC_S_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", protseqs);
|
|
|
|
|
2018-10-19 22:11:35 +02:00
|
|
|
*protseqs = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW)+(sizeof(unsigned short*)*ARRAY_SIZE(protseq_list)));
|
2011-06-23 07:15:40 +02:00
|
|
|
if (!*protseqs)
|
|
|
|
goto end;
|
|
|
|
pvector = *protseqs;
|
|
|
|
pvector->Count = 0;
|
2018-10-19 22:11:35 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(protseq_list); i++)
|
2011-06-23 07:15:40 +02:00
|
|
|
{
|
|
|
|
pvector->Protseq[i] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list[i].name)+1)*sizeof(unsigned short));
|
|
|
|
if (pvector->Protseq[i] == NULL)
|
|
|
|
goto end;
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, (CHAR*)protseq_list[i].name, -1,
|
|
|
|
(WCHAR*)pvector->Protseq[i], strlen(protseq_list[i].name) + 1);
|
|
|
|
pvector->Count++;
|
|
|
|
}
|
|
|
|
status = RPC_S_OK;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
RpcProtseqVectorFreeW(protseqs);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RpcNetworkInqProtseqsA (RPCRT4.@)
|
|
|
|
*/
|
|
|
|
RPC_STATUS WINAPI RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA** protseqs)
|
|
|
|
{
|
|
|
|
RPC_PROTSEQ_VECTORA *pvector;
|
2013-02-25 22:13:56 +01:00
|
|
|
unsigned int i;
|
2011-06-23 07:15:40 +02:00
|
|
|
RPC_STATUS status = RPC_S_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
TRACE("(%p)\n", protseqs);
|
|
|
|
|
2018-10-19 22:11:35 +02:00
|
|
|
*protseqs = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW)+(sizeof(unsigned char*)*ARRAY_SIZE(protseq_list)));
|
2011-06-23 07:15:40 +02:00
|
|
|
if (!*protseqs)
|
|
|
|
goto end;
|
|
|
|
pvector = *protseqs;
|
|
|
|
pvector->Count = 0;
|
2018-10-19 22:11:35 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(protseq_list); i++)
|
2011-06-23 07:15:40 +02:00
|
|
|
{
|
|
|
|
pvector->Protseq[i] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list[i].name)+1);
|
|
|
|
if (pvector->Protseq[i] == NULL)
|
|
|
|
goto end;
|
|
|
|
strcpy((char*)pvector->Protseq[i], protseq_list[i].name);
|
|
|
|
pvector->Count++;
|
|
|
|
}
|
|
|
|
status = RPC_S_OK;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (status != RPC_S_OK)
|
|
|
|
RpcProtseqVectorFreeA(protseqs);
|
|
|
|
return status;
|
|
|
|
}
|