4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
7 * Copyright 2006 Mike McCormack
8 * Copyright 2006 Damjan Jovanovic
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #if defined(__MINGW32__) || defined (_MSC_VER)
29 # include <ws2tcpip.h>
31 # define EADDRINUSE WSAEADDRINUSE
34 # define EAGAIN WSAEWOULDBLOCK
37 # define errno WSAGetLastError()
44 # ifdef HAVE_SYS_SOCKET_H
45 # include <sys/socket.h>
47 # ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
50 # ifdef HAVE_NETINET_TCP_H
51 # include <netinet/tcp.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h>
59 # ifdef HAVE_SYS_POLL_H
60 # include <sys/poll.h>
62 # ifdef HAVE_SYS_FILIO_H
63 # include <sys/filio.h>
65 # ifdef HAVE_SYS_IOCTL_H
66 # include <sys/ioctl.h>
68 # define closesocket close
69 # define ioctlsocket ioctl
70 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
74 #include "epm_towers.h"
77 # define SOL_TCP IPPROTO_TCP
80 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
83 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
85 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
87 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
89 /**** ncacn_np support ****/
91 typedef struct _RpcConnection_np
99 static RpcConnection
*rpcrt4_conn_np_alloc(void)
101 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_np
));
105 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
106 npc
->listening
= FALSE
;
111 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
116 npc
->listening
= TRUE
;
119 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
122 switch(GetLastError())
124 case ERROR_PIPE_CONNECTED
:
125 SetEvent(npc
->ovl
.hEvent
);
127 case ERROR_IO_PENDING
:
128 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
130 case ERROR_NO_DATA_DETECTED
:
131 /* client has disconnected, retry */
132 DisconnectNamedPipe( npc
->pipe
);
135 npc
->listening
= FALSE
;
136 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
137 return RPC_S_OUT_OF_RESOURCES
;
143 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
148 npc
->listening
= TRUE
;
149 npc
->listen_thread
= CreateThread(NULL
, 0, listen_thread
, npc
, 0, NULL
);
150 if (!npc
->listen_thread
)
152 npc
->listening
= FALSE
;
153 ERR("Couldn't create listen thread (error was %d)\n", GetLastError());
154 return RPC_S_OUT_OF_RESOURCES
;
160 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
162 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
163 TRACE("listening on %s\n", pname
);
165 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
166 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
167 PIPE_UNLIMITED_INSTANCES
,
168 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
169 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
170 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
171 if (GetLastError() == ERROR_FILE_EXISTS
)
172 return RPC_S_DUPLICATE_ENDPOINT
;
174 return RPC_S_CANT_CREATE_ENDPOINT
;
177 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
178 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
180 /* Note: we don't call ConnectNamedPipe here because it must be done in the
181 * server thread as the thread must be alertable */
185 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
187 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
191 TRACE("connecting to %s\n", pname
);
197 dwFlags
= SECURITY_SQOS_PRESENT
;
198 switch (Connection
->QOS
->qos
->ImpersonationType
)
200 case RPC_C_IMP_LEVEL_DEFAULT
:
201 /* FIXME: what to do here? */
203 case RPC_C_IMP_LEVEL_ANONYMOUS
:
204 dwFlags
|= SECURITY_ANONYMOUS
;
206 case RPC_C_IMP_LEVEL_IDENTIFY
:
207 dwFlags
|= SECURITY_IDENTIFICATION
;
209 case RPC_C_IMP_LEVEL_IMPERSONATE
:
210 dwFlags
|= SECURITY_IMPERSONATION
;
212 case RPC_C_IMP_LEVEL_DELEGATE
:
213 dwFlags
|= SECURITY_DELEGATION
;
216 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
217 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
219 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
220 OPEN_EXISTING
, dwFlags
, 0);
221 if (pipe
!= INVALID_HANDLE_VALUE
) break;
222 err
= GetLastError();
223 if (err
== ERROR_PIPE_BUSY
) {
224 TRACE("connection failed, error=%x\n", err
);
225 return RPC_S_SERVER_TOO_BUSY
;
226 } else if (err
== ERROR_BAD_NETPATH
) {
227 TRACE("connection failed, error=%x\n", err
);
228 return RPC_S_SERVER_UNAVAILABLE
;
230 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
231 err
= GetLastError();
232 WARN("connection failed, error=%x\n", err
);
233 return RPC_S_SERVER_UNAVAILABLE
;
238 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
239 /* pipe is connected; change to message-read mode. */
240 dwMode
= PIPE_READMODE_MESSAGE
;
241 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
242 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
248 static char *ncalrpc_pipe_name(const char *endpoint
)
250 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
253 /* protseq=ncalrpc: supposed to use NT LPC ports,
254 * but we'll implement it with named pipes for now */
255 pipe_name
= I_RpcAllocate(sizeof(prefix
) + strlen(endpoint
));
256 strcat(strcpy(pipe_name
, prefix
), endpoint
);
260 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
262 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
263 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
267 /* already connected? */
271 /* protseq=ncalrpc: supposed to use NT LPC ports,
272 * but we'll implement it with named pipes for now */
273 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
274 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
275 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
281 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
283 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
286 RpcConnection
*Connection
;
287 char generated_endpoint
[22];
291 static LONG lrpc_nameless_id
;
292 DWORD process_id
= GetCurrentProcessId();
293 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
294 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
295 "LRPC%08x.%08x", process_id
, id
);
296 endpoint
= generated_endpoint
;
299 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
300 endpoint
, NULL
, NULL
, NULL
, NULL
);
304 /* protseq=ncalrpc: supposed to use NT LPC ports,
305 * but we'll implement it with named pipes for now */
306 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
307 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
308 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
311 EnterCriticalSection(&protseq
->cs
);
312 Connection
->Next
= protseq
->conn
;
313 protseq
->conn
= Connection
;
314 LeaveCriticalSection(&protseq
->cs
);
319 static char *ncacn_pipe_name(const char *endpoint
)
321 static const char prefix
[] = "\\\\.";
324 /* protseq=ncacn_np: named pipes */
325 pipe_name
= I_RpcAllocate(sizeof(prefix
) + strlen(endpoint
));
326 strcat(strcpy(pipe_name
, prefix
), endpoint
);
330 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
332 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
333 static const char prefix
[] = "\\\\";
334 static const char local
[] = ".";
335 BOOL bUseLocalName
= TRUE
;
336 CHAR ComputerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
337 DWORD bufLen
= sizeof(ComputerName
)/sizeof(ComputerName
[0]);
343 /* already connected? */
347 /* protseq=ncacn_np: named pipes */
348 size
= strlen(prefix
);
350 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
352 bUseLocalName
= TRUE
;
353 size
+= strlen(local
);
357 NetworkAddr
= Connection
->NetworkAddr
;
358 if (NetworkAddr
[0] == '\\' && NetworkAddr
[1] == '\\')
361 if (GetComputerNameA(ComputerName
, &bufLen
))
363 if (stricmp(ComputerName
, NetworkAddr
) == 0)
365 bUseLocalName
= TRUE
;
366 size
+= strlen(local
);
370 bUseLocalName
= FALSE
;
371 size
+= strlen(NetworkAddr
);
376 bUseLocalName
= FALSE
;
377 size
+= strlen(NetworkAddr
);
381 size
+= strlen(Connection
->Endpoint
) + 1;
383 pname
= I_RpcAllocate(size
);
384 strcpy(pname
, prefix
);
386 strcat(pname
, local
);
388 strcat(pname
, NetworkAddr
);
389 strcat(pname
, Connection
->Endpoint
);
390 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
396 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
398 static const char prefix
[] = "\\\\.";
401 RpcConnection
*Connection
;
402 char generated_endpoint
[21];
406 static LONG np_nameless_id
;
407 DWORD process_id
= GetCurrentProcessId();
408 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
409 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
410 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
411 endpoint
= generated_endpoint
;
414 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
415 endpoint
, NULL
, NULL
, NULL
, NULL
);
419 /* protseq=ncacn_np: named pipes */
420 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
421 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
422 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
425 EnterCriticalSection(&protseq
->cs
);
426 Connection
->Next
= protseq
->conn
;
427 protseq
->conn
= Connection
;
428 LeaveCriticalSection(&protseq
->cs
);
433 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
435 /* because of the way named pipes work, we'll transfer the connected pipe
436 * to the child, then reopen the server binding to continue listening */
438 new_npc
->pipe
= old_npc
->pipe
;
439 new_npc
->ovl
= old_npc
->ovl
;
441 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
442 old_npc
->listening
= FALSE
;
445 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
447 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
450 static const char prefix
[] = "\\\\.";
452 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
454 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
455 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
456 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
459 /* Store the local computer name as the NetworkAddr for ncacn_np as long as
460 * we don't support named pipes over the network. */
461 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
462 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
464 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
465 return RPC_S_OUT_OF_RESOURCES
;
471 static RPC_STATUS
is_pipe_listening(const char *pipe_name
)
473 return WaitNamedPipeA(pipe_name
, 1) ? RPC_S_OK
: RPC_S_NOT_LISTENING
;
476 static RPC_STATUS
rpcrt4_ncacn_np_is_server_listening(const char *endpoint
)
481 pipe_name
= ncacn_pipe_name(endpoint
);
482 status
= is_pipe_listening(pipe_name
);
483 I_RpcFree(pipe_name
);
487 static RPC_STATUS
rpcrt4_ncalrpc_np_is_server_listening(const char *endpoint
)
492 pipe_name
= ncalrpc_pipe_name(endpoint
);
493 status
= is_pipe_listening(pipe_name
);
494 I_RpcFree(pipe_name
);
498 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
500 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
503 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
505 TRACE("%s\n", old_conn
->Endpoint
);
507 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
509 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
510 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
511 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
514 /* Store the local computer name as the NetworkAddr for ncalrpc. */
515 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
516 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
518 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
519 return RPC_S_OUT_OF_RESOURCES
;
525 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
526 void *buffer
, unsigned int count
)
528 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
531 unsigned int bytes_left
= count
;
534 ZeroMemory(&ovl
, sizeof(ovl
));
535 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
540 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
541 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
542 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
543 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
545 if (!ret
|| !bytes_read
)
547 bytes_left
-= bytes_read
;
550 CloseHandle(ovl
.hEvent
);
551 return ret
? count
: -1;
554 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
555 const void *buffer
, unsigned int count
)
557 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
558 const char *buf
= buffer
;
560 unsigned int bytes_left
= count
;
563 ZeroMemory(&ovl
, sizeof(ovl
));
564 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
569 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
570 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
571 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
572 if (!ret
|| !bytes_written
)
574 bytes_left
-= bytes_written
;
575 buf
+= bytes_written
;
577 CloseHandle(ovl
.hEvent
);
578 return ret
? count
: -1;
581 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
583 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
585 FlushFileBuffers(npc
->pipe
);
586 CloseHandle(npc
->pipe
);
589 if (npc
->ovl
.hEvent
) {
590 CloseHandle(npc
->ovl
.hEvent
);
596 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
598 /* FIXME: implement when named pipe writes use overlapped I/O */
601 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
603 /* FIXME: implement when named pipe writes use overlapped I/O */
607 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
608 const char *networkaddr
,
609 const char *endpoint
)
611 twr_empty_floor_t
*smb_floor
;
612 twr_empty_floor_t
*nb_floor
;
614 size_t networkaddr_size
;
615 size_t endpoint_size
;
617 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
619 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
620 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
621 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
626 smb_floor
= (twr_empty_floor_t
*)tower_data
;
628 tower_data
+= sizeof(*smb_floor
);
630 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
631 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
632 smb_floor
->count_rhs
= endpoint_size
;
635 memcpy(tower_data
, endpoint
, endpoint_size
);
638 tower_data
+= endpoint_size
;
640 nb_floor
= (twr_empty_floor_t
*)tower_data
;
642 tower_data
+= sizeof(*nb_floor
);
644 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
645 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
646 nb_floor
->count_rhs
= networkaddr_size
;
649 memcpy(tower_data
, networkaddr
, networkaddr_size
);
656 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
661 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
662 const twr_empty_floor_t
*nb_floor
;
664 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
666 if (tower_size
< sizeof(*smb_floor
))
667 return EPT_S_NOT_REGISTERED
;
669 tower_data
+= sizeof(*smb_floor
);
670 tower_size
-= sizeof(*smb_floor
);
672 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
673 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
674 (smb_floor
->count_rhs
> tower_size
) ||
675 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
676 return EPT_S_NOT_REGISTERED
;
680 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
682 return RPC_S_OUT_OF_RESOURCES
;
683 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
685 tower_data
+= smb_floor
->count_rhs
;
686 tower_size
-= smb_floor
->count_rhs
;
688 if (tower_size
< sizeof(*nb_floor
))
689 return EPT_S_NOT_REGISTERED
;
691 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
693 tower_data
+= sizeof(*nb_floor
);
694 tower_size
-= sizeof(*nb_floor
);
696 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
697 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
698 (nb_floor
->count_rhs
> tower_size
) ||
699 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
700 return EPT_S_NOT_REGISTERED
;
704 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
709 I_RpcFree(*endpoint
);
712 return RPC_S_OUT_OF_RESOURCES
;
714 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
720 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
722 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
725 TRACE("(%p)\n", conn
);
727 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
728 return RPCRT4_default_impersonate_client(conn
);
730 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
733 DWORD error
= GetLastError();
734 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
737 case ERROR_CANNOT_IMPERSONATE
:
738 return RPC_S_NO_CONTEXT_AVAILABLE
;
744 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
748 TRACE("(%p)\n", conn
);
750 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
751 return RPCRT4_default_revert_to_self(conn
);
753 ret
= RevertToSelf();
756 WARN("RevertToSelf failed with error %u\n", GetLastError());
757 return RPC_S_NO_CONTEXT_AVAILABLE
;
762 typedef struct _RpcServerProtseq_np
764 RpcServerProtseq common
;
766 } RpcServerProtseq_np
;
768 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
770 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
772 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
776 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
778 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
779 SetEvent(npps
->mgr_event
);
782 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
784 HANDLE
*objs
= prev_array
;
785 RpcConnection_np
*conn
;
786 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
788 EnterCriticalSection(&protseq
->cs
);
790 /* open and count connections */
792 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
794 rpcrt4_conn_listen_pipe(conn
);
795 if (conn
->ovl
.hEvent
)
797 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
800 /* make array of connections */
802 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
804 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
807 ERR("couldn't allocate objs\n");
808 LeaveCriticalSection(&protseq
->cs
);
812 objs
[0] = npps
->mgr_event
;
814 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
816 if ((objs
[*count
] = conn
->ovl
.hEvent
))
818 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
820 LeaveCriticalSection(&protseq
->cs
);
824 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
826 HeapFree(GetProcessHeap(), 0, array
);
829 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
832 HANDLE
*objs
= wait_array
;
834 RpcConnection
*cconn
;
835 RpcConnection_np
*conn
;
842 /* an alertable wait isn't strictly necessary, but due to our
843 * overlapped I/O implementation in Wine we need to free some memory
844 * by the file user APC being called, even if no completion routine was
845 * specified at the time of starting the async operation */
846 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
847 } while (res
== WAIT_IO_COMPLETION
);
849 if (res
== WAIT_OBJECT_0
)
851 else if (res
== WAIT_FAILED
)
853 ERR("wait failed with error %d\n", GetLastError());
858 b_handle
= objs
[res
- WAIT_OBJECT_0
];
859 /* find which connection got a RPC */
860 EnterCriticalSection(&protseq
->cs
);
861 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
863 if (b_handle
== conn
->ovl
.hEvent
) break;
864 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
868 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
870 ERR("failed to locate connection for handle %p\n", b_handle
);
871 LeaveCriticalSection(&protseq
->cs
);
874 RPCRT4_new_client(cconn
);
881 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
882 const char *networkaddr
,
883 const char *endpoint
)
885 twr_empty_floor_t
*pipe_floor
;
887 size_t endpoint_size
;
889 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
891 endpoint_size
= strlen(endpoint
) + 1;
892 size
= sizeof(*pipe_floor
) + endpoint_size
;
897 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
899 tower_data
+= sizeof(*pipe_floor
);
901 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
902 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
903 pipe_floor
->count_rhs
= endpoint_size
;
905 memcpy(tower_data
, endpoint
, endpoint_size
);
910 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
915 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
917 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
919 if (tower_size
< sizeof(*pipe_floor
))
920 return EPT_S_NOT_REGISTERED
;
922 tower_data
+= sizeof(*pipe_floor
);
923 tower_size
-= sizeof(*pipe_floor
);
925 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
926 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
927 (pipe_floor
->count_rhs
> tower_size
) ||
928 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
929 return EPT_S_NOT_REGISTERED
;
936 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
938 return RPC_S_OUT_OF_RESOURCES
;
939 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
945 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
950 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
951 unsigned char *in_buffer
,
952 unsigned int in_size
,
953 unsigned char *out_buffer
,
954 unsigned int *out_size
)
956 /* since this protocol is local to the machine there is no need to
957 * authenticate the caller */
962 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
963 enum secure_packet_direction dir
,
964 RpcPktHdr
*hdr
, unsigned int hdr_size
,
965 unsigned char *stub_data
, unsigned int stub_data_size
,
966 RpcAuthVerifier
*auth_hdr
,
967 unsigned char *auth_value
, unsigned int auth_value_size
)
969 /* since this protocol is local to the machine there is no need to secure
974 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
975 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
976 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
978 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
979 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
983 FIXME("privs not implemented\n");
986 if (server_princ_name
)
988 FIXME("server_princ_name not implemented\n");
989 *server_princ_name
= NULL
;
991 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
992 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
995 FIXME("authorization service not implemented\n");
996 *authz_svc
= RPC_C_AUTHZ_NONE
;
999 FIXME("flags 0x%x not implemented\n", flags
);
1004 /**** ncacn_ip_tcp support ****/
1006 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1007 const char *networkaddr
,
1008 unsigned char tcp_protid
,
1009 const char *endpoint
)
1011 twr_tcp_floor_t
*tcp_floor
;
1012 twr_ipv4_floor_t
*ipv4_floor
;
1013 struct addrinfo
*ai
;
1014 struct addrinfo hints
;
1016 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1018 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1023 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1024 tower_data
+= sizeof(*tcp_floor
);
1026 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1028 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1029 tcp_floor
->protid
= tcp_protid
;
1030 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1032 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1033 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1034 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1036 hints
.ai_flags
= AI_NUMERICHOST
;
1037 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1038 hints
.ai_family
= PF_INET
;
1039 hints
.ai_socktype
= SOCK_STREAM
;
1040 hints
.ai_protocol
= IPPROTO_TCP
;
1041 hints
.ai_addrlen
= 0;
1042 hints
.ai_addr
= NULL
;
1043 hints
.ai_canonname
= NULL
;
1044 hints
.ai_next
= NULL
;
1046 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1049 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1052 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1057 if (ai
->ai_family
== PF_INET
)
1059 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1060 tcp_floor
->port
= sin
->sin_port
;
1061 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1065 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1075 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1078 unsigned char tcp_protid
,
1081 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1082 const twr_ipv4_floor_t
*ipv4_floor
;
1083 struct in_addr in_addr
;
1085 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1087 if (tower_size
< sizeof(*tcp_floor
))
1088 return EPT_S_NOT_REGISTERED
;
1090 tower_data
+= sizeof(*tcp_floor
);
1091 tower_size
-= sizeof(*tcp_floor
);
1093 if (tower_size
< sizeof(*ipv4_floor
))
1094 return EPT_S_NOT_REGISTERED
;
1096 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1098 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1099 (tcp_floor
->protid
!= tcp_protid
) ||
1100 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1101 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1102 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1103 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1104 return EPT_S_NOT_REGISTERED
;
1108 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1110 return RPC_S_OUT_OF_RESOURCES
;
1111 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1116 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1121 I_RpcFree(*endpoint
);
1124 return RPC_S_OUT_OF_RESOURCES
;
1126 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1127 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1129 ERR("inet_ntop: %s\n", strerror(errno
));
1130 I_RpcFree(*networkaddr
);
1131 *networkaddr
= NULL
;
1134 I_RpcFree(*endpoint
);
1137 return EPT_S_NOT_REGISTERED
;
1144 typedef struct _RpcConnection_tcp
1146 RpcConnection common
;
1148 #ifdef HAVE_SOCKETPAIR
1152 HANDLE cancel_event
;
1154 } RpcConnection_tcp
;
1156 #ifdef HAVE_SOCKETPAIR
1158 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1160 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1162 ERR("socketpair() failed: %s\n", strerror(errno
));
1168 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1170 struct pollfd pfds
[2];
1171 pfds
[0].fd
= tcpc
->sock
;
1172 pfds
[0].events
= POLLIN
;
1173 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1174 pfds
[1].events
= POLLIN
;
1175 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1177 ERR("poll() failed: %s\n", strerror(errno
));
1180 if (pfds
[1].revents
& POLLIN
) /* canceled */
1183 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1189 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1192 pfd
.fd
= tcpc
->sock
;
1193 pfd
.events
= POLLOUT
;
1194 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1196 ERR("poll() failed: %s\n", strerror(errno
));
1202 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1206 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1209 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1211 close(tcpc
->cancel_fds
[0]);
1212 close(tcpc
->cancel_fds
[1]);
1215 #else /* HAVE_SOCKETPAIR */
1217 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1219 static BOOL wsa_inited
;
1223 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1224 /* Note: WSAStartup can be called more than once so we don't bother with
1225 * making accesses to wsa_inited thread-safe */
1228 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1229 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1230 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1232 ERR("event creation failed\n");
1233 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1239 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1241 HANDLE wait_handles
[2];
1243 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1245 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1248 wait_handles
[0] = tcpc
->sock_event
;
1249 wait_handles
[1] = tcpc
->cancel_event
;
1250 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1255 case WAIT_OBJECT_0
+ 1:
1258 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1263 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1266 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1268 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1271 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1277 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1282 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1284 SetEvent(tcpc
->cancel_event
);
1287 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1289 CloseHandle(tcpc
->sock_event
);
1290 CloseHandle(tcpc
->cancel_event
);
1295 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1297 RpcConnection_tcp
*tcpc
;
1298 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1302 if (!rpcrt4_sock_wait_init(tcpc
))
1304 HeapFree(GetProcessHeap(), 0, tcpc
);
1307 return &tcpc
->common
;
1310 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1312 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1315 struct addrinfo
*ai
;
1316 struct addrinfo
*ai_cur
;
1317 struct addrinfo hints
;
1319 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1321 if (tcpc
->sock
!= -1)
1325 hints
.ai_family
= PF_UNSPEC
;
1326 hints
.ai_socktype
= SOCK_STREAM
;
1327 hints
.ai_protocol
= IPPROTO_TCP
;
1328 hints
.ai_addrlen
= 0;
1329 hints
.ai_addr
= NULL
;
1330 hints
.ai_canonname
= NULL
;
1331 hints
.ai_next
= NULL
;
1333 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1336 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1337 Connection
->Endpoint
, gai_strerror(ret
));
1338 return RPC_S_SERVER_UNAVAILABLE
;
1341 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1346 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1348 TRACE("skipping non-IP/IPv6 address family\n");
1356 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1357 host
, sizeof(host
), service
, sizeof(service
),
1358 NI_NUMERICHOST
| NI_NUMERICSERV
);
1359 TRACE("trying %s:%s\n", host
, service
);
1362 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1365 WARN("socket() failed: %s\n", strerror(errno
));
1369 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1371 WARN("connect() failed: %s\n", strerror(errno
));
1376 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1378 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1380 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1385 TRACE("connected\n");
1390 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1391 return RPC_S_SERVER_UNAVAILABLE
;
1394 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1396 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1399 struct addrinfo
*ai
;
1400 struct addrinfo
*ai_cur
;
1401 struct addrinfo hints
;
1402 RpcConnection
*first_connection
= NULL
;
1404 TRACE("(%p, %s)\n", protseq
, endpoint
);
1406 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1407 hints
.ai_family
= PF_UNSPEC
;
1408 hints
.ai_socktype
= SOCK_STREAM
;
1409 hints
.ai_protocol
= IPPROTO_TCP
;
1410 hints
.ai_addrlen
= 0;
1411 hints
.ai_addr
= NULL
;
1412 hints
.ai_canonname
= NULL
;
1413 hints
.ai_next
= NULL
;
1415 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1418 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1420 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1421 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1422 return RPC_S_CANT_CREATE_ENDPOINT
;
1425 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1427 RpcConnection_tcp
*tcpc
;
1428 RPC_STATUS create_status
;
1429 struct sockaddr_storage sa
;
1431 char service
[NI_MAXSERV
];
1434 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1436 TRACE("skipping non-IP/IPv6 address family\n");
1443 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1444 host
, sizeof(host
), service
, sizeof(service
),
1445 NI_NUMERICHOST
| NI_NUMERICSERV
);
1446 TRACE("trying %s:%s\n", host
, service
);
1449 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1452 WARN("socket() failed: %s\n", strerror(errno
));
1453 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1457 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1460 WARN("bind failed: %s\n", strerror(errno
));
1462 if (errno
== EADDRINUSE
)
1463 status
= RPC_S_DUPLICATE_ENDPOINT
;
1465 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1469 sa_len
= sizeof(sa
);
1470 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1472 WARN("getsockname() failed: %s\n", strerror(errno
));
1474 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1478 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1479 NULL
, 0, service
, sizeof(service
),
1483 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1485 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1489 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1490 protseq
->Protseq
, NULL
,
1491 service
, NULL
, NULL
, NULL
, NULL
);
1492 if (create_status
!= RPC_S_OK
)
1495 status
= create_status
;
1500 ret
= listen(sock
, protseq
->MaxCalls
);
1503 WARN("listen failed: %s\n", strerror(errno
));
1504 RPCRT4_ReleaseConnection(&tcpc
->common
);
1505 status
= RPC_S_OUT_OF_RESOURCES
;
1508 /* need a non-blocking socket, otherwise accept() has a potential
1509 * race-condition (poll() says it is readable, connection drops,
1510 * and accept() blocks until the next connection comes...)
1513 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1516 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1517 RPCRT4_ReleaseConnection(&tcpc
->common
);
1518 status
= RPC_S_OUT_OF_RESOURCES
;
1522 tcpc
->common
.Next
= first_connection
;
1523 first_connection
= &tcpc
->common
;
1525 /* since IPv4 and IPv6 share the same port space, we only need one
1526 * successful bind to listen for both */
1532 /* if at least one connection was created for an endpoint then
1534 if (first_connection
)
1536 RpcConnection
*conn
;
1538 /* find last element in list */
1539 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1542 EnterCriticalSection(&protseq
->cs
);
1543 conn
->Next
= protseq
->conn
;
1544 protseq
->conn
= first_connection
;
1545 LeaveCriticalSection(&protseq
->cs
);
1547 TRACE("listening on %s\n", endpoint
);
1551 ERR("couldn't listen on port %s\n", endpoint
);
1555 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1558 struct sockaddr_in address
;
1560 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1561 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1564 addrsize
= sizeof(address
);
1565 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1568 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1569 return RPC_S_OUT_OF_RESOURCES
;
1573 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1576 client
->common
.NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, INET6_ADDRSTRLEN
);
1577 ret
= getnameinfo((struct sockaddr
*)&address
, addrsize
, client
->common
.NetworkAddr
, INET6_ADDRSTRLEN
, NULL
, 0, NI_NUMERICHOST
);
1580 ERR("Failed to retrieve the IP address, error %d\n", ret
);
1581 return RPC_S_OUT_OF_RESOURCES
;
1584 TRACE("Accepted a new TCP connection from %s\n", client
->common
.NetworkAddr
);
1588 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1589 void *buffer
, unsigned int count
)
1591 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1593 while (bytes_read
!= count
)
1595 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1600 else if (errno
== EINTR
)
1602 else if (errno
!= EAGAIN
)
1604 WARN("recv() failed: %s\n", strerror(errno
));
1609 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1613 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1617 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1618 const void *buffer
, unsigned int count
)
1620 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1621 int bytes_written
= 0;
1622 while (bytes_written
!= count
)
1624 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1627 else if (errno
== EINTR
)
1629 else if (errno
!= EAGAIN
)
1633 if (!rpcrt4_sock_wait_for_send(tcpc
))
1637 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1638 return bytes_written
;
1641 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1643 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1645 TRACE("%d\n", tcpc
->sock
);
1647 if (tcpc
->sock
!= -1)
1648 closesocket(tcpc
->sock
);
1650 rpcrt4_sock_wait_destroy(tcpc
);
1654 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1656 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1657 TRACE("%p\n", Connection
);
1658 rpcrt4_sock_wait_cancel(tcpc
);
1661 static RPC_STATUS
rpcrt4_conn_tcp_is_server_listening(const char *endpoint
)
1664 return RPC_S_ACCESS_DENIED
;
1667 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1669 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1671 TRACE("%p\n", Connection
);
1673 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1678 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1679 const char *networkaddr
,
1680 const char *endpoint
)
1682 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1683 EPM_PROTOCOL_TCP
, endpoint
);
1686 #ifdef HAVE_SOCKETPAIR
1688 typedef struct _RpcServerProtseq_sock
1690 RpcServerProtseq common
;
1693 } RpcServerProtseq_sock
;
1695 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1697 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1701 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1703 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1704 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1705 ps
->mgr_event_rcv
= fds
[0];
1706 ps
->mgr_event_snd
= fds
[1];
1710 ERR("socketpair failed with error %s\n", strerror(errno
));
1711 HeapFree(GetProcessHeap(), 0, ps
);
1718 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1720 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1722 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1725 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1727 struct pollfd
*poll_info
= prev_array
;
1728 RpcConnection_tcp
*conn
;
1729 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1731 EnterCriticalSection(&protseq
->cs
);
1733 /* open and count connections */
1735 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1737 if (conn
->sock
!= -1)
1739 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1742 /* make array of connections */
1744 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1746 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1749 ERR("couldn't allocate poll_info\n");
1750 LeaveCriticalSection(&protseq
->cs
);
1754 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1755 poll_info
[0].events
= POLLIN
;
1757 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1759 if (conn
->sock
!= -1)
1761 poll_info
[*count
].fd
= conn
->sock
;
1762 poll_info
[*count
].events
= POLLIN
;
1765 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1767 LeaveCriticalSection(&protseq
->cs
);
1771 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1773 HeapFree(GetProcessHeap(), 0, array
);
1776 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1778 struct pollfd
*poll_info
= wait_array
;
1781 RpcConnection
*cconn
;
1782 RpcConnection_tcp
*conn
;
1787 ret
= poll(poll_info
, count
, -1);
1790 ERR("poll failed with error %d\n", ret
);
1794 for (i
= 0; i
< count
; i
++)
1795 if (poll_info
[i
].revents
& POLLIN
)
1797 /* RPC server event */
1801 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1805 /* find which connection got a RPC */
1806 EnterCriticalSection(&protseq
->cs
);
1807 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1809 if (poll_info
[i
].fd
== conn
->sock
) break;
1810 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1814 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1816 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1817 LeaveCriticalSection(&protseq
->cs
);
1819 RPCRT4_new_client(cconn
);
1827 #else /* HAVE_SOCKETPAIR */
1829 typedef struct _RpcServerProtseq_sock
1831 RpcServerProtseq common
;
1833 } RpcServerProtseq_sock
;
1835 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1837 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1840 static BOOL wsa_inited
;
1844 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1845 /* Note: WSAStartup can be called more than once so we don't bother with
1846 * making accesses to wsa_inited thread-safe */
1849 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1854 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1856 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1857 SetEvent(sockps
->mgr_event
);
1860 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1862 HANDLE
*objs
= prev_array
;
1863 RpcConnection_tcp
*conn
;
1864 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1866 EnterCriticalSection(&protseq
->cs
);
1868 /* open and count connections */
1870 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1873 if (conn
->sock
!= -1)
1875 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1878 /* make array of connections */
1880 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1882 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1885 ERR("couldn't allocate objs\n");
1886 LeaveCriticalSection(&protseq
->cs
);
1890 objs
[0] = sockps
->mgr_event
;
1892 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1895 if (conn
->sock
!= -1)
1897 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1898 if (res
== SOCKET_ERROR
)
1899 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1902 objs
[*count
] = conn
->sock_event
;
1906 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1908 LeaveCriticalSection(&protseq
->cs
);
1912 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1914 HeapFree(GetProcessHeap(), 0, array
);
1917 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1920 HANDLE
*objs
= wait_array
;
1922 RpcConnection
*cconn
;
1923 RpcConnection_tcp
*conn
;
1930 /* an alertable wait isn't strictly necessary, but due to our
1931 * overlapped I/O implementation in Wine we need to free some memory
1932 * by the file user APC being called, even if no completion routine was
1933 * specified at the time of starting the async operation */
1934 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1935 } while (res
== WAIT_IO_COMPLETION
);
1937 if (res
== WAIT_OBJECT_0
)
1939 else if (res
== WAIT_FAILED
)
1941 ERR("wait failed with error %d\n", GetLastError());
1946 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1947 /* find which connection got a RPC */
1948 EnterCriticalSection(&protseq
->cs
);
1949 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1952 if (b_handle
== conn
->sock_event
) break;
1953 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1957 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1959 ERR("failed to locate connection for handle %p\n", b_handle
);
1960 LeaveCriticalSection(&protseq
->cs
);
1963 RPCRT4_new_client(cconn
);
1970 #endif /* HAVE_SOCKETPAIR */
1972 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1977 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1978 networkaddr
, EPM_PROTOCOL_TCP
,
1982 /**** ncacn_http support ****/
1984 /* 60 seconds is the period native uses */
1985 #define HTTP_IDLE_TIME 60000
1987 /* reference counted to avoid a race between a cancelled call's connection
1988 * being destroyed and the asynchronous InternetReadFileEx call being
1990 typedef struct _RpcHttpAsyncData
1993 HANDLE completion_event
;
1995 INTERNET_BUFFERSW inet_buffers
;
1996 CRITICAL_SECTION cs
;
1999 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
2001 return InterlockedIncrement(&data
->refs
);
2004 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
2006 ULONG refs
= InterlockedDecrement(&data
->refs
);
2009 TRACE("destroying async data %p\n", data
);
2010 CloseHandle(data
->completion_event
);
2011 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
2012 data
->cs
.DebugInfo
->Spare
[0] = 0;
2013 DeleteCriticalSection(&data
->cs
);
2014 HeapFree(GetProcessHeap(), 0, data
);
2019 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
2021 ResetEvent(async_data
->completion_event
);
2022 RpcHttpAsyncData_AddRef(async_data
);
2025 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
2027 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
2031 RpcHttpAsyncData_Release(async_data
);
2035 if(GetLastError() != ERROR_IO_PENDING
) {
2036 RpcHttpAsyncData_Release(async_data
);
2037 ERR("Request failed with error %d\n", GetLastError());
2038 return RPC_S_SERVER_UNAVAILABLE
;
2041 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2042 if(res
!= WAIT_OBJECT_0
) {
2043 TRACE("Cancelled\n");
2044 return RPC_S_CALL_CANCELLED
;
2047 if(async_data
->async_result
) {
2048 ERR("Async request failed with error %d\n", async_data
->async_result
);
2049 return RPC_S_SERVER_UNAVAILABLE
;
2064 unsigned int data_len
;
2065 BOOL finished
; /* finished authenticating */
2068 typedef struct _RpcConnection_http
2070 RpcConnection common
;
2073 HINTERNET in_request
;
2074 HINTERNET out_request
;
2076 HANDLE timer_cancelled
;
2077 HANDLE cancel_event
;
2078 DWORD last_sent_time
;
2079 ULONG bytes_received
;
2080 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
2081 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
2082 UUID connection_uuid
;
2085 RpcHttpAsyncData
*async_data
;
2086 } RpcConnection_http
;
2088 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
2090 RpcConnection_http
*httpc
;
2091 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
2092 if (!httpc
) return NULL
;
2093 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
2094 if (!httpc
->async_data
)
2096 HeapFree(GetProcessHeap(), 0, httpc
);
2099 TRACE("async data = %p\n", httpc
->async_data
);
2100 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2101 httpc
->async_data
->refs
= 1;
2102 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSW
);
2103 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2104 InitializeCriticalSection(&httpc
->async_data
->cs
);
2105 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
2106 return &httpc
->common
;
2109 typedef struct _HttpTimerThreadData
2112 DWORD
*last_sent_time
;
2113 HANDLE timer_cancelled
;
2114 } HttpTimerThreadData
;
2116 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
2118 HINTERNET in_request
= param
;
2119 RpcPktHdr
*idle_pkt
;
2121 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
2125 DWORD bytes_written
;
2126 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
2127 RPCRT4_FreeHeader(idle_pkt
);
2131 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
2133 DWORD cur_time
= GetTickCount();
2134 DWORD cached_last_sent_time
= *last_sent_time
;
2135 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
2138 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
2140 HttpTimerThreadData
*data_in
= param
;
2141 HttpTimerThreadData data
;
2145 HeapFree(GetProcessHeap(), 0, data_in
);
2147 for (timeout
= HTTP_IDLE_TIME
;
2148 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
2149 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
2151 /* are we too soon after last send? */
2152 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
2154 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2157 CloseHandle(data
.timer_cancelled
);
2161 static VOID WINAPI
rpcrt4_http_internet_callback(
2162 HINTERNET hInternet
,
2163 DWORD_PTR dwContext
,
2164 DWORD dwInternetStatus
,
2165 LPVOID lpvStatusInformation
,
2166 DWORD dwStatusInformationLength
)
2168 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2170 switch (dwInternetStatus
)
2172 case INTERNET_STATUS_REQUEST_COMPLETE
:
2173 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2176 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2178 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2179 SetEvent(async_data
->completion_event
);
2180 RpcHttpAsyncData_Release(async_data
);
2186 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2193 WCHAR
*status_text
= buf
;
2197 size
= sizeof(status_code
);
2198 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2200 return GetLastError();
2201 if (status_code
== HTTP_STATUS_OK
)
2205 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2206 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2208 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2209 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2212 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2213 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2215 if (status_code
== HTTP_STATUS_DENIED
)
2216 return ERROR_ACCESS_DENIED
;
2217 return RPC_S_SERVER_UNAVAILABLE
;
2220 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2222 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2223 LPWSTR proxy
= NULL
;
2225 LPWSTR password
= NULL
;
2226 LPWSTR servername
= NULL
;
2227 const WCHAR
*option
;
2230 if (httpc
->common
.QOS
&&
2231 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2233 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2234 if (http_cred
->TransportCredentials
)
2237 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2238 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2239 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2241 return RPC_S_OUT_OF_RESOURCES
;
2243 if (cred
->DomainLength
)
2245 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2246 p
+= cred
->DomainLength
;
2250 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2251 p
[cred
->UserLength
] = 0;
2253 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2257 for (option
= httpc
->common
.NetworkOptions
; option
;
2258 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2260 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2261 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2263 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2265 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2266 const WCHAR
*value_end
;
2269 value_end
= strchrW(option
, ',');
2271 value_end
= value_start
+ strlenW(value_start
);
2272 for (p
= value_start
; p
< value_end
; p
++)
2279 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2280 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2282 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2284 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2285 const WCHAR
*value_end
;
2287 value_end
= strchrW(option
, ',');
2289 value_end
= value_start
+ strlenW(value_start
);
2290 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2291 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2294 FIXME("unhandled option %s\n", debugstr_w(option
));
2297 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2298 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2299 if (!httpc
->app_info
)
2301 HeapFree(GetProcessHeap(), 0, password
);
2302 HeapFree(GetProcessHeap(), 0, user
);
2303 HeapFree(GetProcessHeap(), 0, proxy
);
2304 HeapFree(GetProcessHeap(), 0, servername
);
2305 ERR("InternetOpenW failed with error %d\n", GetLastError());
2306 return RPC_S_SERVER_UNAVAILABLE
;
2308 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2310 /* if no RpcProxy option specified, set the HTTP server address to the
2311 * RPC server address */
2314 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2317 HeapFree(GetProcessHeap(), 0, password
);
2318 HeapFree(GetProcessHeap(), 0, user
);
2319 HeapFree(GetProcessHeap(), 0, proxy
);
2320 return RPC_S_OUT_OF_RESOURCES
;
2322 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2325 port
= (httpc
->common
.QOS
&&
2326 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2327 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2328 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2330 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2331 INTERNET_SERVICE_HTTP
, 0, 0);
2333 HeapFree(GetProcessHeap(), 0, password
);
2334 HeapFree(GetProcessHeap(), 0, user
);
2335 HeapFree(GetProcessHeap(), 0, proxy
);
2337 if (!httpc
->session
)
2339 ERR("InternetConnectW failed with error %d\n", GetLastError());
2340 HeapFree(GetProcessHeap(), 0, servername
);
2341 return RPC_S_SERVER_UNAVAILABLE
;
2343 httpc
->servername
= servername
;
2347 static int rpcrt4_http_async_read(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2348 void *buffer
, unsigned int count
)
2352 unsigned int bytes_left
= count
;
2353 RPC_STATUS status
= RPC_S_OK
;
2355 async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
2359 async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2360 prepare_async_request(async_data
);
2361 ret
= InternetReadFileExW(req
, &async_data
->inet_buffers
, IRF_ASYNC
, 0);
2362 status
= wait_async_request(async_data
, ret
, cancel_event
);
2363 if (status
!= RPC_S_OK
)
2365 if (status
== RPC_S_CALL_CANCELLED
)
2366 TRACE("call cancelled\n");
2370 if (!async_data
->inet_buffers
.dwBufferLength
)
2372 memcpy(buf
, async_data
->inet_buffers
.lpvBuffer
,
2373 async_data
->inet_buffers
.dwBufferLength
);
2375 bytes_left
-= async_data
->inet_buffers
.dwBufferLength
;
2376 buf
+= async_data
->inet_buffers
.dwBufferLength
;
2379 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
2380 async_data
->inet_buffers
.lpvBuffer
= NULL
;
2382 TRACE("%p %p %u -> %u\n", req
, buffer
, count
, status
);
2383 return status
== RPC_S_OK
? count
: -1;
2386 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2392 TRACE("sending echo request to server\n");
2394 prepare_async_request(async_data
);
2395 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2396 status
= wait_async_request(async_data
, ret
, cancel_event
);
2397 if (status
!= RPC_S_OK
) return status
;
2399 status
= rpcrt4_http_check_response(req
);
2400 if (status
!= RPC_S_OK
) return status
;
2402 rpcrt4_http_async_read(req
, async_data
, cancel_event
, buf
, sizeof(buf
));
2403 /* FIXME: do something with retrieved data */
2408 static RPC_STATUS
insert_content_length_header(HINTERNET request
, DWORD len
)
2410 static const WCHAR fmtW
[] =
2411 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
2412 WCHAR header
[sizeof(fmtW
) / sizeof(fmtW
[0]) + 10];
2414 sprintfW(header
, fmtW
, len
);
2415 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
))) return RPC_S_OK
;
2416 return RPC_S_SERVER_UNAVAILABLE
;
2419 /* prepare the in pipe for use by RPC packets */
2420 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2421 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2422 const UUID
*association_uuid
, BOOL authorized
)
2427 INTERNET_BUFFERSW buffers_in
;
2428 DWORD bytes_written
;
2432 /* ask wininet to authorize, if necessary */
2433 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2434 if (status
!= RPC_S_OK
) return status
;
2436 memset(&buffers_in
, 0, sizeof(buffers_in
));
2437 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2438 /* FIXME: get this from the registry */
2439 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2440 status
= insert_content_length_header(in_request
, buffers_in
.dwBufferTotal
);
2441 if (status
!= RPC_S_OK
) return status
;
2443 prepare_async_request(async_data
);
2444 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2445 status
= wait_async_request(async_data
, ret
, cancel_event
);
2446 if (status
!= RPC_S_OK
) return status
;
2448 TRACE("sending HTTP connect header to server\n");
2449 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2450 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2451 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2452 RPCRT4_FreeHeader(hdr
);
2455 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2456 return RPC_S_SERVER_UNAVAILABLE
;
2462 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcHttpAsyncData
*async_data
,
2463 HANDLE cancel_event
, RpcPktHdr
*hdr
, BYTE
**data
)
2465 unsigned short data_len
;
2468 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, hdr
, sizeof(hdr
->common
)) < 0)
2469 return RPC_S_SERVER_UNAVAILABLE
;
2470 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2472 ERR("wrong packet type received %d or wrong frag_len %d\n",
2473 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2474 return RPC_S_PROTOCOL_ERROR
;
2477 size
= sizeof(hdr
->http
) - sizeof(hdr
->common
);
2478 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, &hdr
->common
+ 1, size
) < 0)
2479 return RPC_S_SERVER_UNAVAILABLE
;
2481 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2484 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2486 return RPC_S_OUT_OF_RESOURCES
;
2487 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, *data
, data_len
) < 0)
2489 HeapFree(GetProcessHeap(), 0, *data
);
2490 return RPC_S_SERVER_UNAVAILABLE
;
2496 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2498 ERR("invalid http packet\n");
2499 HeapFree(GetProcessHeap(), 0, *data
);
2500 return RPC_S_PROTOCOL_ERROR
;
2506 /* prepare the out pipe for use by RPC packets */
2507 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2508 HANDLE cancel_event
, const UUID
*connection_uuid
,
2509 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2515 BYTE
*data_from_server
;
2516 RpcPktHdr pkt_from_server
;
2517 ULONG field1
, field3
;
2522 /* ask wininet to authorize, if necessary */
2523 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2524 if (status
!= RPC_S_OK
) return status
;
2527 rpcrt4_http_async_read(out_request
, async_data
, cancel_event
, buf
, sizeof(buf
));
2529 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2530 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2532 status
= insert_content_length_header(out_request
, hdr
->common
.frag_len
);
2533 if (status
!= RPC_S_OK
)
2535 RPCRT4_FreeHeader(hdr
);
2539 TRACE("sending HTTP connect header to server\n");
2540 prepare_async_request(async_data
);
2541 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2542 status
= wait_async_request(async_data
, ret
, cancel_event
);
2543 RPCRT4_FreeHeader(hdr
);
2544 if (status
!= RPC_S_OK
) return status
;
2546 status
= rpcrt4_http_check_response(out_request
);
2547 if (status
!= RPC_S_OK
) return status
;
2549 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2550 &pkt_from_server
, &data_from_server
);
2551 if (status
!= RPC_S_OK
) return status
;
2552 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2554 HeapFree(GetProcessHeap(), 0, data_from_server
);
2555 if (status
!= RPC_S_OK
) return status
;
2556 TRACE("received (%d) from first prepare header\n", field1
);
2560 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2561 &pkt_from_server
, &data_from_server
);
2562 if (status
!= RPC_S_OK
) return status
;
2563 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2565 TRACE("http idle packet, waiting for real packet\n");
2566 HeapFree(GetProcessHeap(), 0, data_from_server
);
2567 if (pkt_from_server
.http
.num_data_items
!= 0)
2569 ERR("HTTP idle packet should have no data items instead of %d\n",
2570 pkt_from_server
.http
.num_data_items
);
2571 return RPC_S_PROTOCOL_ERROR
;
2574 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2575 &field1
, flow_control_increment
,
2577 HeapFree(GetProcessHeap(), 0, data_from_server
);
2578 if (status
!= RPC_S_OK
) return status
;
2579 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2584 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2586 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2591 /* first 6 bits, all from bin[0] */
2592 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2593 x
= (bin
[0] & 3) << 4;
2595 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2598 base64
[i
++] = enc
[x
];
2603 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2604 x
= (bin
[1] & 0x0f) << 2;
2606 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2609 base64
[i
++] = enc
[x
];
2613 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2615 /* last 6 bits, all from bin [2] */
2616 base64
[i
++] = enc
[bin
[2] & 0x3f];
2624 static inline char decode_char( WCHAR c
)
2626 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2627 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2628 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2629 if (c
== '+') return 62;
2630 if (c
== '/') return 63;
2634 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2637 char c0
, c1
, c2
, c3
;
2638 const WCHAR
*p
= base64
;
2642 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2643 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2644 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2645 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2649 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2650 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2651 buf
[i
+ 2] = (c2
<< 6) | c3
;
2659 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2660 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2662 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2665 else if (p
[3] == '=')
2667 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2668 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2669 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2673 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2674 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2680 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2681 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2682 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2683 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2687 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2688 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2689 buf
[i
+ 2] = (c2
<< 6) | c3
;
2696 static struct authinfo
*alloc_authinfo(void)
2698 struct authinfo
*ret
;
2700 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2702 SecInvalidateHandle(&ret
->cred
);
2703 SecInvalidateHandle(&ret
->ctx
);
2704 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2710 ret
->finished
= FALSE
;
2714 static void destroy_authinfo(struct authinfo
*info
)
2718 if (SecIsValidHandle(&info
->ctx
))
2719 DeleteSecurityContext(&info
->ctx
);
2720 if (SecIsValidHandle(&info
->cred
))
2721 FreeCredentialsHandle(&info
->cred
);
2723 HeapFree(GetProcessHeap(), 0, info
->data
);
2724 HeapFree(GetProcessHeap(), 0, info
);
2727 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2728 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2729 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2730 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2731 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2741 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2742 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2743 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2744 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2745 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2747 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2749 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2752 for (i
= 0; i
< num_auth_schemes
; i
++)
2754 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2755 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2760 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2762 DWORD len
, index
= 0;
2766 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2767 if (auth_scheme_from_header(buffer
) == scheme
) break;
2772 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2773 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2775 struct authinfo
*info
= *auth_ptr
;
2776 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2777 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2779 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2781 switch (creds
->AuthnSchemes
[0])
2783 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2785 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2786 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2788 info
->data_len
= userlen
+ passlen
+ 1;
2789 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2791 status
= RPC_S_OUT_OF_MEMORY
;
2794 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2795 info
->data
[userlen
] = ':';
2796 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2798 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2799 info
->finished
= TRUE
;
2803 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2804 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2807 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2808 SECURITY_STATUS ret
;
2809 SecBufferDesc out_desc
, in_desc
;
2811 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2815 WCHAR auth_value
[2048];
2816 DWORD size
= sizeof(auth_value
);
2819 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2820 else scheme
= negotiateW
;
2821 scheme_len
= strlenW( scheme
);
2826 SecPkgInfoW
*pkg_info
;
2828 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2829 if (ret
!= SEC_E_OK
) break;
2831 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2832 if (ret
!= SEC_E_OK
) break;
2834 info
->max_token
= pkg_info
->cbMaxToken
;
2835 FreeContextBuffer(pkg_info
);
2840 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2841 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2843 ERR("authentication scheme changed\n");
2847 in
.BufferType
= SECBUFFER_TOKEN
;
2851 in_desc
.ulVersion
= 0;
2852 in_desc
.cBuffers
= 1;
2853 in_desc
.pBuffers
= &in
;
2855 p
= auth_value
+ scheme_len
;
2856 if (!first
&& *p
== ' ')
2858 int len
= strlenW(++p
);
2859 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2860 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2861 decode_base64(p
, len
, in
.pvBuffer
);
2863 out
.BufferType
= SECBUFFER_TOKEN
;
2864 out
.cbBuffer
= info
->max_token
;
2865 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2867 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2870 out_desc
.ulVersion
= 0;
2871 out_desc
.cBuffers
= 1;
2872 out_desc
.pBuffers
= &out
;
2874 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2875 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2876 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2877 &info
->attr
, &info
->exp
);
2878 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2879 if (ret
== SEC_E_OK
)
2881 HeapFree(GetProcessHeap(), 0, info
->data
);
2882 info
->data
= out
.pvBuffer
;
2883 info
->data_len
= out
.cbBuffer
;
2884 info
->finished
= TRUE
;
2885 TRACE("sending last auth packet\n");
2888 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2890 HeapFree(GetProcessHeap(), 0, info
->data
);
2891 info
->data
= out
.pvBuffer
;
2892 info
->data_len
= out
.cbBuffer
;
2893 TRACE("sending next auth packet\n");
2898 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2899 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2902 info
->scheme
= creds
->AuthnSchemes
[0];
2906 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2910 if (status
!= RPC_S_OK
)
2912 destroy_authinfo(info
);
2920 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2922 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2923 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2924 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2925 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2926 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2927 const WCHAR
*scheme_str
;
2928 WCHAR
*header
, *ptr
;
2929 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2933 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2934 scheme_str
= basicW
;
2935 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2937 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2938 scheme_str
= negotiateW
;
2939 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2941 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2943 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2946 ERR("unknown scheme %u\n", scheme
);
2947 return RPC_S_SERVER_UNAVAILABLE
;
2949 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2951 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2952 ptr
= header
+ auth_len
;
2953 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2955 len
= encode_base64(data
, data_len
, ptr
);
2959 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2961 HeapFree(GetProcessHeap(), 0, header
);
2966 static void drain_content(HINTERNET request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2968 DWORD count
, len
= 0, size
= sizeof(len
);
2971 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2975 count
= min(sizeof(buf
), len
);
2976 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, buf
, count
) <= 0) return;
2981 static RPC_STATUS
authorize_request(RpcConnection_http
*httpc
, HINTERNET request
)
2983 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2984 struct authinfo
*info
= NULL
;
2990 status
= do_authorization(request
, httpc
->servername
, httpc
->common
.QOS
->qos
->u
.HttpCredentials
, &info
);
2991 if (status
!= RPC_S_OK
) break;
2993 status
= insert_authorization_header(request
, info
->scheme
, info
->data
, info
->data_len
);
2994 if (status
!= RPC_S_OK
) break;
2996 prepare_async_request(httpc
->async_data
);
2997 ret
= HttpSendRequestW(request
, NULL
, 0, NULL
, 0);
2998 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2999 if (status
!= RPC_S_OK
|| info
->finished
) break;
3001 status
= rpcrt4_http_check_response(request
);
3002 if (status
!= RPC_S_OK
&& status
!= ERROR_ACCESS_DENIED
) break;
3003 drain_content(request
, httpc
->async_data
, httpc
->cancel_event
);
3006 if (info
->scheme
!= RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
3007 HttpAddRequestHeadersW(request
, authW
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
3009 destroy_authinfo(info
);
3013 static BOOL
has_credentials(RpcConnection_http
*httpc
)
3015 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
3016 SEC_WINNT_AUTH_IDENTITY_W
*id
;
3018 if (!httpc
->common
.QOS
|| httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
3021 creds
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
3022 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
3025 id
= creds
->TransportCredentials
;
3026 if (!id
|| !id
->User
|| !id
->Password
) return FALSE
;
3031 static BOOL
is_secure(RpcConnection_http
*httpc
)
3033 return httpc
->common
.QOS
&&
3034 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
3035 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
3038 static RPC_STATUS
set_auth_cookie(RpcConnection_http
*httpc
, const WCHAR
*value
)
3040 static WCHAR httpW
[] = {'h','t','t','p',0};
3041 static WCHAR httpsW
[] = {'h','t','t','p','s',0};
3047 if (!value
) return RPC_S_OK
;
3049 uc
.dwStructSize
= sizeof(uc
);
3050 uc
.lpszScheme
= is_secure(httpc
) ? httpsW
: httpW
;
3051 uc
.dwSchemeLength
= 0;
3052 uc
.lpszHostName
= httpc
->servername
;
3053 uc
.dwHostNameLength
= 0;
3055 uc
.lpszUserName
= NULL
;
3056 uc
.dwUserNameLength
= 0;
3057 uc
.lpszPassword
= NULL
;
3058 uc
.dwPasswordLength
= 0;
3059 uc
.lpszUrlPath
= NULL
;
3060 uc
.dwUrlPathLength
= 0;
3061 uc
.lpszExtraInfo
= NULL
;
3062 uc
.dwExtraInfoLength
= 0;
3064 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3065 return RPC_S_SERVER_UNAVAILABLE
;
3067 if (!(url
= HeapAlloc(GetProcessHeap(), 0, len
))) return RPC_S_OUT_OF_MEMORY
;
3069 len
= len
/ sizeof(WCHAR
) - 1;
3070 if (!InternetCreateUrlW(&uc
, 0, url
, &len
))
3072 HeapFree(GetProcessHeap(), 0, url
);
3073 return RPC_S_SERVER_UNAVAILABLE
;
3076 ret
= InternetSetCookieW(url
, NULL
, value
);
3077 HeapFree(GetProcessHeap(), 0, url
);
3078 if (!ret
) return RPC_S_SERVER_UNAVAILABLE
;
3083 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
3085 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
3086 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
3087 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
3088 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
3089 static const WCHAR wszColon
[] = {':',0};
3090 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
3091 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
3095 BOOL secure
, credentials
;
3096 HttpTimerThreadData
*timer_data
;
3099 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
3101 if (Connection
->server
)
3103 ERR("ncacn_http servers not supported yet\n");
3104 return RPC_S_SERVER_UNAVAILABLE
;
3107 if (httpc
->in_request
)
3110 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
3112 UuidCreate(&httpc
->connection_uuid
);
3113 UuidCreate(&httpc
->in_pipe_uuid
);
3114 UuidCreate(&httpc
->out_pipe_uuid
);
3116 status
= rpcrt4_http_internet_connect(httpc
);
3117 if (status
!= RPC_S_OK
)
3120 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
3122 return RPC_S_OUT_OF_MEMORY
;
3123 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
3124 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
3125 strcatW(url
, wszColon
);
3126 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
3128 secure
= is_secure(httpc
);
3129 credentials
= has_credentials(httpc
);
3131 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
3132 INTERNET_FLAG_NO_AUTO_REDIRECT
;
3133 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
3134 if (credentials
) flags
|= INTERNET_FLAG_NO_AUTH
;
3136 status
= set_auth_cookie(httpc
, Connection
->CookieAuth
);
3137 if (status
!= RPC_S_OK
)
3139 HeapFree(GetProcessHeap(), 0, url
);
3142 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
3143 flags
, (DWORD_PTR
)httpc
->async_data
);
3144 if (!httpc
->in_request
)
3146 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
3147 HeapFree(GetProcessHeap(), 0, url
);
3148 return RPC_S_SERVER_UNAVAILABLE
;
3153 status
= authorize_request(httpc
, httpc
->in_request
);
3154 if (status
!= RPC_S_OK
)
3156 HeapFree(GetProcessHeap(), 0, url
);
3159 status
= rpcrt4_http_check_response(httpc
->in_request
);
3160 if (status
!= RPC_S_OK
)
3162 HeapFree(GetProcessHeap(), 0, url
);
3165 drain_content(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
);
3168 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
, wszAcceptTypes
,
3169 flags
, (DWORD_PTR
)httpc
->async_data
);
3170 HeapFree(GetProcessHeap(), 0, url
);
3171 if (!httpc
->out_request
)
3173 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
3174 return RPC_S_SERVER_UNAVAILABLE
;
3179 status
= authorize_request(httpc
, httpc
->out_request
);
3180 if (status
!= RPC_S_OK
)
3184 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
,
3185 &httpc
->connection_uuid
, &httpc
->in_pipe_uuid
,
3186 &Connection
->assoc
->http_uuid
, credentials
);
3187 if (status
!= RPC_S_OK
)
3190 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
,
3191 &httpc
->connection_uuid
, &httpc
->out_pipe_uuid
,
3192 &httpc
->flow_control_increment
, credentials
);
3193 if (status
!= RPC_S_OK
)
3196 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
3197 httpc
->last_sent_time
= GetTickCount();
3198 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
3200 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
3202 return ERROR_OUTOFMEMORY
;
3203 timer_data
->timer_param
= httpc
->in_request
;
3204 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
3205 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
3206 /* FIXME: should use CreateTimerQueueTimer when implemented */
3207 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
3210 HeapFree(GetProcessHeap(), 0, timer_data
);
3211 return GetLastError();
3213 CloseHandle(thread
);
3218 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
3221 return RPC_S_SERVER_UNAVAILABLE
;
3224 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
3225 void *buffer
, unsigned int count
)
3227 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3228 return rpcrt4_http_async_read(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
, buffer
, count
);
3231 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
3233 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3237 RpcPktCommonHdr common_hdr
;
3241 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
3244 /* read packet common header */
3245 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
3246 if (dwRead
!= sizeof(common_hdr
)) {
3247 WARN("Short read of header, %d bytes\n", dwRead
);
3248 status
= RPC_S_PROTOCOL_ERROR
;
3251 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
3252 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
3254 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
3255 status
= RPC_S_PROTOCOL_ERROR
;
3259 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
3260 if (status
!= RPC_S_OK
) goto fail
;
3262 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
3263 if (hdr_length
== 0) {
3264 WARN("header length == 0\n");
3265 status
= RPC_S_PROTOCOL_ERROR
;
3269 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
3272 status
= RPC_S_OUT_OF_RESOURCES
;
3275 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
3277 /* read the rest of packet header */
3278 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
3279 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
3280 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
3281 status
= RPC_S_PROTOCOL_ERROR
;
3285 if (common_hdr
.frag_len
- hdr_length
)
3287 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
3290 status
= RPC_S_OUT_OF_RESOURCES
;
3294 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
3295 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
3297 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
3298 status
= RPC_S_PROTOCOL_ERROR
;
3305 if ((*Header
)->common
.ptype
== PKT_HTTP
)
3307 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
3309 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
3310 status
= RPC_S_PROTOCOL_ERROR
;
3313 if ((*Header
)->http
.flags
== 0x0001)
3315 TRACE("http idle packet, waiting for real packet\n");
3316 if ((*Header
)->http
.num_data_items
!= 0)
3318 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
3319 status
= RPC_S_PROTOCOL_ERROR
;
3323 else if ((*Header
)->http
.flags
== 0x0002)
3325 ULONG bytes_transmitted
;
3326 ULONG flow_control_increment
;
3328 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
3331 &flow_control_increment
,
3333 if (status
!= RPC_S_OK
)
3335 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
3336 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
3337 /* FIXME: do something with parsed data */
3341 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
3342 status
= RPC_S_PROTOCOL_ERROR
;
3345 RPCRT4_FreeHeader(*Header
);
3347 HeapFree(GetProcessHeap(), 0, *Payload
);
3355 httpc
->bytes_received
+= common_hdr
.frag_len
;
3357 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
3359 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
3361 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
3362 httpc
->bytes_received
,
3363 httpc
->flow_control_increment
,
3364 &httpc
->out_pipe_uuid
);
3367 DWORD bytes_written
;
3369 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
3370 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
3371 RPCRT4_FreeHeader(hdr
);
3373 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
3378 if (status
!= RPC_S_OK
) {
3379 RPCRT4_FreeHeader(*Header
);
3381 HeapFree(GetProcessHeap(), 0, *Payload
);
3387 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
3388 const void *buffer
, unsigned int count
)
3390 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3391 DWORD bytes_written
;
3394 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
3395 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
3396 httpc
->last_sent_time
= GetTickCount();
3397 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
3398 return ret
? bytes_written
: -1;
3401 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
3403 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3407 SetEvent(httpc
->timer_cancelled
);
3408 if (httpc
->in_request
)
3409 InternetCloseHandle(httpc
->in_request
);
3410 httpc
->in_request
= NULL
;
3411 if (httpc
->out_request
)
3412 InternetCloseHandle(httpc
->out_request
);
3413 httpc
->out_request
= NULL
;
3414 if (httpc
->app_info
)
3415 InternetCloseHandle(httpc
->app_info
);
3416 httpc
->app_info
= NULL
;
3418 InternetCloseHandle(httpc
->session
);
3419 httpc
->session
= NULL
;
3420 RpcHttpAsyncData_Release(httpc
->async_data
);
3421 if (httpc
->cancel_event
)
3422 CloseHandle(httpc
->cancel_event
);
3423 HeapFree(GetProcessHeap(), 0, httpc
->servername
);
3424 httpc
->servername
= NULL
;
3429 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
3431 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3433 SetEvent(httpc
->cancel_event
);
3436 static RPC_STATUS
rpcrt4_ncacn_http_is_server_listening(const char *endpoint
)
3439 return RPC_S_ACCESS_DENIED
;
3442 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
3444 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3448 prepare_async_request(httpc
->async_data
);
3449 ret
= InternetQueryDataAvailable(httpc
->out_request
,
3450 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
3451 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3452 return status
== RPC_S_OK
? 0 : -1;
3455 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
3456 const char *networkaddr
,
3457 const char *endpoint
)
3459 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
3460 EPM_PROTOCOL_HTTP
, endpoint
);
3463 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
3468 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
3469 networkaddr
, EPM_PROTOCOL_HTTP
,
3473 static const struct connection_ops conn_protseq_list
[] = {
3475 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
3476 rpcrt4_conn_np_alloc
,
3477 rpcrt4_ncacn_np_open
,
3478 rpcrt4_ncacn_np_handoff
,
3479 rpcrt4_conn_np_read
,
3480 rpcrt4_conn_np_write
,
3481 rpcrt4_conn_np_close
,
3482 rpcrt4_conn_np_cancel_call
,
3483 rpcrt4_ncacn_np_is_server_listening
,
3484 rpcrt4_conn_np_wait_for_incoming_data
,
3485 rpcrt4_ncacn_np_get_top_of_tower
,
3486 rpcrt4_ncacn_np_parse_top_of_tower
,
3488 RPCRT4_default_is_authorized
,
3489 RPCRT4_default_authorize
,
3490 RPCRT4_default_secure_packet
,
3491 rpcrt4_conn_np_impersonate_client
,
3492 rpcrt4_conn_np_revert_to_self
,
3493 RPCRT4_default_inquire_auth_client
,
3496 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
3497 rpcrt4_conn_np_alloc
,
3498 rpcrt4_ncalrpc_open
,
3499 rpcrt4_ncalrpc_handoff
,
3500 rpcrt4_conn_np_read
,
3501 rpcrt4_conn_np_write
,
3502 rpcrt4_conn_np_close
,
3503 rpcrt4_conn_np_cancel_call
,
3504 rpcrt4_ncalrpc_np_is_server_listening
,
3505 rpcrt4_conn_np_wait_for_incoming_data
,
3506 rpcrt4_ncalrpc_get_top_of_tower
,
3507 rpcrt4_ncalrpc_parse_top_of_tower
,
3509 rpcrt4_ncalrpc_is_authorized
,
3510 rpcrt4_ncalrpc_authorize
,
3511 rpcrt4_ncalrpc_secure_packet
,
3512 rpcrt4_conn_np_impersonate_client
,
3513 rpcrt4_conn_np_revert_to_self
,
3514 rpcrt4_ncalrpc_inquire_auth_client
,
3517 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
3518 rpcrt4_conn_tcp_alloc
,
3519 rpcrt4_ncacn_ip_tcp_open
,
3520 rpcrt4_conn_tcp_handoff
,
3521 rpcrt4_conn_tcp_read
,
3522 rpcrt4_conn_tcp_write
,
3523 rpcrt4_conn_tcp_close
,
3524 rpcrt4_conn_tcp_cancel_call
,
3525 rpcrt4_conn_tcp_is_server_listening
,
3526 rpcrt4_conn_tcp_wait_for_incoming_data
,
3527 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
3528 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
3530 RPCRT4_default_is_authorized
,
3531 RPCRT4_default_authorize
,
3532 RPCRT4_default_secure_packet
,
3533 RPCRT4_default_impersonate_client
,
3534 RPCRT4_default_revert_to_self
,
3535 RPCRT4_default_inquire_auth_client
,
3538 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
3539 rpcrt4_ncacn_http_alloc
,
3540 rpcrt4_ncacn_http_open
,
3541 rpcrt4_ncacn_http_handoff
,
3542 rpcrt4_ncacn_http_read
,
3543 rpcrt4_ncacn_http_write
,
3544 rpcrt4_ncacn_http_close
,
3545 rpcrt4_ncacn_http_cancel_call
,
3546 rpcrt4_ncacn_http_is_server_listening
,
3547 rpcrt4_ncacn_http_wait_for_incoming_data
,
3548 rpcrt4_ncacn_http_get_top_of_tower
,
3549 rpcrt4_ncacn_http_parse_top_of_tower
,
3550 rpcrt4_ncacn_http_receive_fragment
,
3551 RPCRT4_default_is_authorized
,
3552 RPCRT4_default_authorize
,
3553 RPCRT4_default_secure_packet
,
3554 RPCRT4_default_impersonate_client
,
3555 RPCRT4_default_revert_to_self
,
3556 RPCRT4_default_inquire_auth_client
,
3561 static const struct protseq_ops protseq_list
[] =
3565 rpcrt4_protseq_np_alloc
,
3566 rpcrt4_protseq_np_signal_state_changed
,
3567 rpcrt4_protseq_np_get_wait_array
,
3568 rpcrt4_protseq_np_free_wait_array
,
3569 rpcrt4_protseq_np_wait_for_new_connection
,
3570 rpcrt4_protseq_ncacn_np_open_endpoint
,
3574 rpcrt4_protseq_np_alloc
,
3575 rpcrt4_protseq_np_signal_state_changed
,
3576 rpcrt4_protseq_np_get_wait_array
,
3577 rpcrt4_protseq_np_free_wait_array
,
3578 rpcrt4_protseq_np_wait_for_new_connection
,
3579 rpcrt4_protseq_ncalrpc_open_endpoint
,
3583 rpcrt4_protseq_sock_alloc
,
3584 rpcrt4_protseq_sock_signal_state_changed
,
3585 rpcrt4_protseq_sock_get_wait_array
,
3586 rpcrt4_protseq_sock_free_wait_array
,
3587 rpcrt4_protseq_sock_wait_for_new_connection
,
3588 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
3592 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
3595 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
3596 if (!strcmp(protseq_list
[i
].name
, protseq
))
3597 return &protseq_list
[i
];
3601 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
3604 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
3605 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
3606 return &conn_protseq_list
[i
];
3610 /**** interface to rest of code ****/
3612 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
3614 TRACE("(Connection == ^%p)\n", Connection
);
3616 assert(!Connection
->server
);
3617 return Connection
->ops
->open_connection_client(Connection
);
3620 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
3622 TRACE("(Connection == ^%p)\n", Connection
);
3623 if (SecIsValidHandle(&Connection
->ctx
))
3625 DeleteSecurityContext(&Connection
->ctx
);
3626 SecInvalidateHandle(&Connection
->ctx
);
3628 rpcrt4_conn_close(Connection
);
3632 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
3633 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
3634 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
, LPCWSTR CookieAuth
)
3636 static LONG next_id
;
3637 const struct connection_ops
*ops
;
3638 RpcConnection
* NewConnection
;
3640 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
3643 FIXME("not supported for protseq %s\n", Protseq
);
3644 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
3647 NewConnection
= ops
->alloc();
3648 NewConnection
->ref
= 1;
3649 NewConnection
->Next
= NULL
;
3650 NewConnection
->server_binding
= NULL
;
3651 NewConnection
->server
= server
;
3652 NewConnection
->ops
= ops
;
3653 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
3654 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
3655 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
3656 NewConnection
->CookieAuth
= RPCRT4_strdupW(CookieAuth
);
3657 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
3658 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
3659 NewConnection
->NextCallId
= 1;
3661 SecInvalidateHandle(&NewConnection
->ctx
);
3662 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
3663 NewConnection
->attr
= 0;
3664 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
3665 NewConnection
->AuthInfo
= AuthInfo
;
3666 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
3667 NewConnection
->encryption_auth_len
= 0;
3668 NewConnection
->signature_auth_len
= 0;
3669 if (QOS
) RpcQualityOfService_AddRef(QOS
);
3670 NewConnection
->QOS
= QOS
;
3672 list_init(&NewConnection
->conn_pool_entry
);
3673 NewConnection
->async_state
= NULL
;
3675 TRACE("connection: %p\n", NewConnection
);
3676 *Connection
= NewConnection
;
3681 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3685 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
, rpcrt4_conn_get_name(OldConnection
),
3686 OldConnection
->NetworkAddr
, OldConnection
->Endpoint
, NULL
,
3687 OldConnection
->AuthInfo
, OldConnection
->QOS
, OldConnection
->CookieAuth
);
3688 if (err
== RPC_S_OK
)
3689 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3693 RpcConnection
*RPCRT4_GrabConnection( RpcConnection
*conn
)
3695 InterlockedIncrement( &conn
->ref
);
3699 RPC_STATUS
RPCRT4_ReleaseConnection(RpcConnection
* Connection
)
3701 if (InterlockedDecrement( &Connection
->ref
) > 0) return RPC_S_OK
;
3703 TRACE("destroying connection %p\n", Connection
);
3705 RPCRT4_CloseConnection(Connection
);
3706 RPCRT4_strfree(Connection
->Endpoint
);
3707 RPCRT4_strfree(Connection
->NetworkAddr
);
3708 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3709 HeapFree(GetProcessHeap(), 0, Connection
->CookieAuth
);
3710 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3711 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3714 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3716 HeapFree(GetProcessHeap(), 0, Connection
);
3720 RPC_STATUS
RPCRT4_IsServerListening(const char *protseq
, const char *endpoint
)
3722 const struct connection_ops
*ops
;
3724 ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3727 FIXME("not supported for protseq %s\n", protseq
);
3728 return RPC_S_INVALID_BINDING
;
3731 return ops
->is_server_listening(endpoint
);
3734 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3736 const char *protseq
,
3737 const char *networkaddr
,
3738 const char *endpoint
)
3740 twr_empty_floor_t
*protocol_floor
;
3741 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3746 return RPC_S_INVALID_RPC_PROTSEQ
;
3750 *tower_size
= sizeof(*protocol_floor
);
3751 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3755 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3756 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3757 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3758 protocol_floor
->count_rhs
= 0;
3760 tower_data
+= sizeof(*protocol_floor
);
3762 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3764 return EPT_S_NOT_REGISTERED
;
3766 *tower_size
+= sizeof(*protocol_floor
);
3771 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3777 const twr_empty_floor_t
*protocol_floor
;
3778 const twr_empty_floor_t
*floor4
;
3779 const struct connection_ops
*protseq_ops
= NULL
;
3783 if (tower_size
< sizeof(*protocol_floor
))
3784 return EPT_S_NOT_REGISTERED
;
3786 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3787 tower_data
+= sizeof(*protocol_floor
);
3788 tower_size
-= sizeof(*protocol_floor
);
3789 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3790 (protocol_floor
->count_rhs
> tower_size
))
3791 return EPT_S_NOT_REGISTERED
;
3792 tower_data
+= protocol_floor
->count_rhs
;
3793 tower_size
-= protocol_floor
->count_rhs
;
3795 floor4
= (const twr_empty_floor_t
*)tower_data
;
3796 if ((tower_size
< sizeof(*floor4
)) ||
3797 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3798 return EPT_S_NOT_REGISTERED
;
3800 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3801 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3802 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3804 protseq_ops
= &conn_protseq_list
[i
];
3809 return EPT_S_NOT_REGISTERED
;
3811 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3813 if ((status
== RPC_S_OK
) && protseq
)
3815 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3816 strcpy(*protseq
, protseq_ops
->name
);
3822 /***********************************************************************
3823 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3825 * Checks if the given protocol sequence is known by the RPC system.
3826 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3829 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3833 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3834 ps
, sizeof ps
, NULL
, NULL
);
3835 if (rpcrt4_get_conn_protseq_ops(ps
))
3838 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3840 return RPC_S_INVALID_RPC_PROTSEQ
;
3843 /***********************************************************************
3844 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3846 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3848 UNICODE_STRING protseqW
;
3850 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3852 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3853 RtlFreeUnicodeString(&protseqW
);
3856 return RPC_S_OUT_OF_MEMORY
;
3859 /***********************************************************************
3860 * RpcProtseqVectorFreeA (RPCRT4.@)
3862 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3864 TRACE("(%p)\n", protseqs
);
3869 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3870 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3871 HeapFree(GetProcessHeap(), 0, *protseqs
);
3877 /***********************************************************************
3878 * RpcProtseqVectorFreeW (RPCRT4.@)
3880 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3882 TRACE("(%p)\n", protseqs
);
3887 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3888 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3889 HeapFree(GetProcessHeap(), 0, *protseqs
);
3895 /***********************************************************************
3896 * RpcNetworkInqProtseqsW (RPCRT4.@)
3898 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3900 RPC_PROTSEQ_VECTORW
*pvector
;
3902 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3904 TRACE("(%p)\n", protseqs
);
3906 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3909 pvector
= *protseqs
;
3911 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3913 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3914 if (pvector
->Protseq
[i
] == NULL
)
3916 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3917 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3923 if (status
!= RPC_S_OK
)
3924 RpcProtseqVectorFreeW(protseqs
);
3928 /***********************************************************************
3929 * RpcNetworkInqProtseqsA (RPCRT4.@)
3931 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3933 RPC_PROTSEQ_VECTORA
*pvector
;
3935 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3937 TRACE("(%p)\n", protseqs
);
3939 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3942 pvector
= *protseqs
;
3944 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3946 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3947 if (pvector
->Protseq
[i
] == NULL
)
3949 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3955 if (status
!= RPC_S_OK
)
3956 RpcProtseqVectorFreeA(protseqs
);