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 FIXME("Using local computer name as NetworkAddr\n");
462 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
463 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
465 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
466 return RPC_S_OUT_OF_RESOURCES
;
472 static RPC_STATUS
is_pipe_listening(const char *pipe_name
)
474 return WaitNamedPipeA(pipe_name
, 1) ? RPC_S_OK
: RPC_S_NOT_LISTENING
;
477 static RPC_STATUS
rpcrt4_ncacn_np_is_server_listening(const char *endpoint
)
482 pipe_name
= ncacn_pipe_name(endpoint
);
483 status
= is_pipe_listening(pipe_name
);
484 I_RpcFree(pipe_name
);
488 static RPC_STATUS
rpcrt4_ncalrpc_np_is_server_listening(const char *endpoint
)
493 pipe_name
= ncalrpc_pipe_name(endpoint
);
494 status
= is_pipe_listening(pipe_name
);
495 I_RpcFree(pipe_name
);
499 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
501 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
504 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
506 TRACE("%s\n", old_conn
->Endpoint
);
508 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
510 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
511 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
512 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
515 /* Store the local computer name as the NetworkAddr for ncalrpc. */
516 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
517 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
519 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
520 return RPC_S_OUT_OF_RESOURCES
;
526 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
527 void *buffer
, unsigned int count
)
529 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
532 unsigned int bytes_left
= count
;
535 ZeroMemory(&ovl
, sizeof(ovl
));
536 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
541 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
542 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
543 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
544 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
546 if (!ret
|| !bytes_read
)
548 bytes_left
-= bytes_read
;
551 CloseHandle(ovl
.hEvent
);
552 return ret
? count
: -1;
555 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
556 const void *buffer
, unsigned int count
)
558 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
559 const char *buf
= buffer
;
561 unsigned int bytes_left
= count
;
564 ZeroMemory(&ovl
, sizeof(ovl
));
565 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
570 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
571 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
572 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
573 if (!ret
|| !bytes_written
)
575 bytes_left
-= bytes_written
;
576 buf
+= bytes_written
;
578 CloseHandle(ovl
.hEvent
);
579 return ret
? count
: -1;
582 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
584 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
586 FlushFileBuffers(npc
->pipe
);
587 CloseHandle(npc
->pipe
);
590 if (npc
->ovl
.hEvent
) {
591 CloseHandle(npc
->ovl
.hEvent
);
597 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
599 /* FIXME: implement when named pipe writes use overlapped I/O */
602 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
604 /* FIXME: implement when named pipe writes use overlapped I/O */
608 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
609 const char *networkaddr
,
610 const char *endpoint
)
612 twr_empty_floor_t
*smb_floor
;
613 twr_empty_floor_t
*nb_floor
;
615 size_t networkaddr_size
;
616 size_t endpoint_size
;
618 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
620 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
621 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
622 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
627 smb_floor
= (twr_empty_floor_t
*)tower_data
;
629 tower_data
+= sizeof(*smb_floor
);
631 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
632 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
633 smb_floor
->count_rhs
= endpoint_size
;
636 memcpy(tower_data
, endpoint
, endpoint_size
);
639 tower_data
+= endpoint_size
;
641 nb_floor
= (twr_empty_floor_t
*)tower_data
;
643 tower_data
+= sizeof(*nb_floor
);
645 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
646 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
647 nb_floor
->count_rhs
= networkaddr_size
;
650 memcpy(tower_data
, networkaddr
, networkaddr_size
);
657 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
662 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
663 const twr_empty_floor_t
*nb_floor
;
665 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
667 if (tower_size
< sizeof(*smb_floor
))
668 return EPT_S_NOT_REGISTERED
;
670 tower_data
+= sizeof(*smb_floor
);
671 tower_size
-= sizeof(*smb_floor
);
673 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
674 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
675 (smb_floor
->count_rhs
> tower_size
) ||
676 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
677 return EPT_S_NOT_REGISTERED
;
681 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
683 return RPC_S_OUT_OF_RESOURCES
;
684 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
686 tower_data
+= smb_floor
->count_rhs
;
687 tower_size
-= smb_floor
->count_rhs
;
689 if (tower_size
< sizeof(*nb_floor
))
690 return EPT_S_NOT_REGISTERED
;
692 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
694 tower_data
+= sizeof(*nb_floor
);
695 tower_size
-= sizeof(*nb_floor
);
697 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
698 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
699 (nb_floor
->count_rhs
> tower_size
) ||
700 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
701 return EPT_S_NOT_REGISTERED
;
705 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
710 I_RpcFree(*endpoint
);
713 return RPC_S_OUT_OF_RESOURCES
;
715 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
721 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
723 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
726 TRACE("(%p)\n", conn
);
728 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
729 return RPCRT4_default_impersonate_client(conn
);
731 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
734 DWORD error
= GetLastError();
735 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
738 case ERROR_CANNOT_IMPERSONATE
:
739 return RPC_S_NO_CONTEXT_AVAILABLE
;
745 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
749 TRACE("(%p)\n", conn
);
751 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
752 return RPCRT4_default_revert_to_self(conn
);
754 ret
= RevertToSelf();
757 WARN("RevertToSelf failed with error %u\n", GetLastError());
758 return RPC_S_NO_CONTEXT_AVAILABLE
;
763 typedef struct _RpcServerProtseq_np
765 RpcServerProtseq common
;
767 } RpcServerProtseq_np
;
769 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
771 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
773 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
777 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
779 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
780 SetEvent(npps
->mgr_event
);
783 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
785 HANDLE
*objs
= prev_array
;
786 RpcConnection_np
*conn
;
787 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
789 EnterCriticalSection(&protseq
->cs
);
791 /* open and count connections */
793 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
795 rpcrt4_conn_listen_pipe(conn
);
796 if (conn
->ovl
.hEvent
)
798 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
801 /* make array of connections */
803 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
805 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
808 ERR("couldn't allocate objs\n");
809 LeaveCriticalSection(&protseq
->cs
);
813 objs
[0] = npps
->mgr_event
;
815 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
817 if ((objs
[*count
] = conn
->ovl
.hEvent
))
819 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
821 LeaveCriticalSection(&protseq
->cs
);
825 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
827 HeapFree(GetProcessHeap(), 0, array
);
830 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
833 HANDLE
*objs
= wait_array
;
835 RpcConnection
*cconn
;
836 RpcConnection_np
*conn
;
843 /* an alertable wait isn't strictly necessary, but due to our
844 * overlapped I/O implementation in Wine we need to free some memory
845 * by the file user APC being called, even if no completion routine was
846 * specified at the time of starting the async operation */
847 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
848 } while (res
== WAIT_IO_COMPLETION
);
850 if (res
== WAIT_OBJECT_0
)
852 else if (res
== WAIT_FAILED
)
854 ERR("wait failed with error %d\n", GetLastError());
859 b_handle
= objs
[res
- WAIT_OBJECT_0
];
860 /* find which connection got a RPC */
861 EnterCriticalSection(&protseq
->cs
);
862 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
864 if (b_handle
== conn
->ovl
.hEvent
) break;
865 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
869 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
871 ERR("failed to locate connection for handle %p\n", b_handle
);
872 LeaveCriticalSection(&protseq
->cs
);
875 RPCRT4_new_client(cconn
);
882 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
883 const char *networkaddr
,
884 const char *endpoint
)
886 twr_empty_floor_t
*pipe_floor
;
888 size_t endpoint_size
;
890 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
892 endpoint_size
= strlen(endpoint
) + 1;
893 size
= sizeof(*pipe_floor
) + endpoint_size
;
898 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
900 tower_data
+= sizeof(*pipe_floor
);
902 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
903 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
904 pipe_floor
->count_rhs
= endpoint_size
;
906 memcpy(tower_data
, endpoint
, endpoint_size
);
911 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
916 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
918 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
920 if (tower_size
< sizeof(*pipe_floor
))
921 return EPT_S_NOT_REGISTERED
;
923 tower_data
+= sizeof(*pipe_floor
);
924 tower_size
-= sizeof(*pipe_floor
);
926 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
927 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
928 (pipe_floor
->count_rhs
> tower_size
) ||
929 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
930 return EPT_S_NOT_REGISTERED
;
937 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
939 return RPC_S_OUT_OF_RESOURCES
;
940 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
946 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
951 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
952 unsigned char *in_buffer
,
953 unsigned int in_size
,
954 unsigned char *out_buffer
,
955 unsigned int *out_size
)
957 /* since this protocol is local to the machine there is no need to
958 * authenticate the caller */
963 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
964 enum secure_packet_direction dir
,
965 RpcPktHdr
*hdr
, unsigned int hdr_size
,
966 unsigned char *stub_data
, unsigned int stub_data_size
,
967 RpcAuthVerifier
*auth_hdr
,
968 unsigned char *auth_value
, unsigned int auth_value_size
)
970 /* since this protocol is local to the machine there is no need to secure
975 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
976 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
977 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
979 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
980 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
984 FIXME("privs not implemented\n");
987 if (server_princ_name
)
989 FIXME("server_princ_name not implemented\n");
990 *server_princ_name
= NULL
;
992 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
993 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
996 FIXME("authorization service not implemented\n");
997 *authz_svc
= RPC_C_AUTHZ_NONE
;
1000 FIXME("flags 0x%x not implemented\n", flags
);
1005 /**** ncacn_ip_tcp support ****/
1007 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1008 const char *networkaddr
,
1009 unsigned char tcp_protid
,
1010 const char *endpoint
)
1012 twr_tcp_floor_t
*tcp_floor
;
1013 twr_ipv4_floor_t
*ipv4_floor
;
1014 struct addrinfo
*ai
;
1015 struct addrinfo hints
;
1017 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1019 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1024 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1025 tower_data
+= sizeof(*tcp_floor
);
1027 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1029 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1030 tcp_floor
->protid
= tcp_protid
;
1031 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1033 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1034 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1035 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1037 hints
.ai_flags
= AI_NUMERICHOST
;
1038 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1039 hints
.ai_family
= PF_INET
;
1040 hints
.ai_socktype
= SOCK_STREAM
;
1041 hints
.ai_protocol
= IPPROTO_TCP
;
1042 hints
.ai_addrlen
= 0;
1043 hints
.ai_addr
= NULL
;
1044 hints
.ai_canonname
= NULL
;
1045 hints
.ai_next
= NULL
;
1047 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1050 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1053 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1058 if (ai
->ai_family
== PF_INET
)
1060 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1061 tcp_floor
->port
= sin
->sin_port
;
1062 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1066 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1076 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1079 unsigned char tcp_protid
,
1082 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1083 const twr_ipv4_floor_t
*ipv4_floor
;
1084 struct in_addr in_addr
;
1086 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1088 if (tower_size
< sizeof(*tcp_floor
))
1089 return EPT_S_NOT_REGISTERED
;
1091 tower_data
+= sizeof(*tcp_floor
);
1092 tower_size
-= sizeof(*tcp_floor
);
1094 if (tower_size
< sizeof(*ipv4_floor
))
1095 return EPT_S_NOT_REGISTERED
;
1097 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1099 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1100 (tcp_floor
->protid
!= tcp_protid
) ||
1101 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1102 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1103 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1104 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1105 return EPT_S_NOT_REGISTERED
;
1109 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1111 return RPC_S_OUT_OF_RESOURCES
;
1112 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1117 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1122 I_RpcFree(*endpoint
);
1125 return RPC_S_OUT_OF_RESOURCES
;
1127 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1128 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1130 ERR("inet_ntop: %s\n", strerror(errno
));
1131 I_RpcFree(*networkaddr
);
1132 *networkaddr
= NULL
;
1135 I_RpcFree(*endpoint
);
1138 return EPT_S_NOT_REGISTERED
;
1145 typedef struct _RpcConnection_tcp
1147 RpcConnection common
;
1149 #ifdef HAVE_SOCKETPAIR
1153 HANDLE cancel_event
;
1155 } RpcConnection_tcp
;
1157 #ifdef HAVE_SOCKETPAIR
1159 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1161 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1163 ERR("socketpair() failed: %s\n", strerror(errno
));
1169 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1171 struct pollfd pfds
[2];
1172 pfds
[0].fd
= tcpc
->sock
;
1173 pfds
[0].events
= POLLIN
;
1174 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1175 pfds
[1].events
= POLLIN
;
1176 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1178 ERR("poll() failed: %s\n", strerror(errno
));
1181 if (pfds
[1].revents
& POLLIN
) /* canceled */
1184 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1190 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1193 pfd
.fd
= tcpc
->sock
;
1194 pfd
.events
= POLLOUT
;
1195 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1197 ERR("poll() failed: %s\n", strerror(errno
));
1203 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1207 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1210 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1212 close(tcpc
->cancel_fds
[0]);
1213 close(tcpc
->cancel_fds
[1]);
1216 #else /* HAVE_SOCKETPAIR */
1218 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1220 static BOOL wsa_inited
;
1224 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1225 /* Note: WSAStartup can be called more than once so we don't bother with
1226 * making accesses to wsa_inited thread-safe */
1229 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1230 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1231 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1233 ERR("event creation failed\n");
1234 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1240 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1242 HANDLE wait_handles
[2];
1244 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1246 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1249 wait_handles
[0] = tcpc
->sock_event
;
1250 wait_handles
[1] = tcpc
->cancel_event
;
1251 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1256 case WAIT_OBJECT_0
+ 1:
1259 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1264 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1267 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1269 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1272 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1278 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1283 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1285 SetEvent(tcpc
->cancel_event
);
1288 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1290 CloseHandle(tcpc
->sock_event
);
1291 CloseHandle(tcpc
->cancel_event
);
1296 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1298 RpcConnection_tcp
*tcpc
;
1299 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1303 if (!rpcrt4_sock_wait_init(tcpc
))
1305 HeapFree(GetProcessHeap(), 0, tcpc
);
1308 return &tcpc
->common
;
1311 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1313 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1316 struct addrinfo
*ai
;
1317 struct addrinfo
*ai_cur
;
1318 struct addrinfo hints
;
1320 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1322 if (tcpc
->sock
!= -1)
1326 hints
.ai_family
= PF_UNSPEC
;
1327 hints
.ai_socktype
= SOCK_STREAM
;
1328 hints
.ai_protocol
= IPPROTO_TCP
;
1329 hints
.ai_addrlen
= 0;
1330 hints
.ai_addr
= NULL
;
1331 hints
.ai_canonname
= NULL
;
1332 hints
.ai_next
= NULL
;
1334 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1337 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1338 Connection
->Endpoint
, gai_strerror(ret
));
1339 return RPC_S_SERVER_UNAVAILABLE
;
1342 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1347 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1349 TRACE("skipping non-IP/IPv6 address family\n");
1357 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1358 host
, sizeof(host
), service
, sizeof(service
),
1359 NI_NUMERICHOST
| NI_NUMERICSERV
);
1360 TRACE("trying %s:%s\n", host
, service
);
1363 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1366 WARN("socket() failed: %s\n", strerror(errno
));
1370 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1372 WARN("connect() failed: %s\n", strerror(errno
));
1377 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1379 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1381 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1386 TRACE("connected\n");
1391 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1392 return RPC_S_SERVER_UNAVAILABLE
;
1395 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1397 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1400 struct addrinfo
*ai
;
1401 struct addrinfo
*ai_cur
;
1402 struct addrinfo hints
;
1403 RpcConnection
*first_connection
= NULL
;
1405 TRACE("(%p, %s)\n", protseq
, endpoint
);
1407 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1408 hints
.ai_family
= PF_UNSPEC
;
1409 hints
.ai_socktype
= SOCK_STREAM
;
1410 hints
.ai_protocol
= IPPROTO_TCP
;
1411 hints
.ai_addrlen
= 0;
1412 hints
.ai_addr
= NULL
;
1413 hints
.ai_canonname
= NULL
;
1414 hints
.ai_next
= NULL
;
1416 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1419 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1421 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1422 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1423 return RPC_S_CANT_CREATE_ENDPOINT
;
1426 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1428 RpcConnection_tcp
*tcpc
;
1429 RPC_STATUS create_status
;
1430 struct sockaddr_storage sa
;
1432 char service
[NI_MAXSERV
];
1435 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1437 TRACE("skipping non-IP/IPv6 address family\n");
1444 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1445 host
, sizeof(host
), service
, sizeof(service
),
1446 NI_NUMERICHOST
| NI_NUMERICSERV
);
1447 TRACE("trying %s:%s\n", host
, service
);
1450 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1453 WARN("socket() failed: %s\n", strerror(errno
));
1454 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1458 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1461 WARN("bind failed: %s\n", strerror(errno
));
1463 if (errno
== EADDRINUSE
)
1464 status
= RPC_S_DUPLICATE_ENDPOINT
;
1466 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1470 sa_len
= sizeof(sa
);
1471 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1473 WARN("getsockname() failed: %s\n", strerror(errno
));
1475 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1479 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1480 NULL
, 0, service
, sizeof(service
),
1484 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1486 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1490 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1491 protseq
->Protseq
, NULL
,
1492 service
, NULL
, NULL
, NULL
, NULL
);
1493 if (create_status
!= RPC_S_OK
)
1496 status
= create_status
;
1501 ret
= listen(sock
, protseq
->MaxCalls
);
1504 WARN("listen failed: %s\n", strerror(errno
));
1505 RPCRT4_ReleaseConnection(&tcpc
->common
);
1506 status
= RPC_S_OUT_OF_RESOURCES
;
1509 /* need a non-blocking socket, otherwise accept() has a potential
1510 * race-condition (poll() says it is readable, connection drops,
1511 * and accept() blocks until the next connection comes...)
1514 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1517 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1518 RPCRT4_ReleaseConnection(&tcpc
->common
);
1519 status
= RPC_S_OUT_OF_RESOURCES
;
1523 tcpc
->common
.Next
= first_connection
;
1524 first_connection
= &tcpc
->common
;
1526 /* since IPv4 and IPv6 share the same port space, we only need one
1527 * successful bind to listen for both */
1533 /* if at least one connection was created for an endpoint then
1535 if (first_connection
)
1537 RpcConnection
*conn
;
1539 /* find last element in list */
1540 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1543 EnterCriticalSection(&protseq
->cs
);
1544 conn
->Next
= protseq
->conn
;
1545 protseq
->conn
= first_connection
;
1546 LeaveCriticalSection(&protseq
->cs
);
1548 TRACE("listening on %s\n", endpoint
);
1552 ERR("couldn't listen on port %s\n", endpoint
);
1556 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1559 struct sockaddr_in address
;
1561 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1562 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1565 addrsize
= sizeof(address
);
1566 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1569 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1570 return RPC_S_OUT_OF_RESOURCES
;
1574 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1577 client
->common
.NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, INET6_ADDRSTRLEN
);
1578 ret
= getnameinfo((struct sockaddr
*)&address
, addrsize
, client
->common
.NetworkAddr
, INET6_ADDRSTRLEN
, NULL
, 0, NI_NUMERICHOST
);
1581 ERR("Failed to retrieve the IP address, error %d\n", ret
);
1582 return RPC_S_OUT_OF_RESOURCES
;
1585 TRACE("Accepted a new TCP connection from %s\n", client
->common
.NetworkAddr
);
1589 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1590 void *buffer
, unsigned int count
)
1592 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1594 while (bytes_read
!= count
)
1596 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1601 else if (errno
== EINTR
)
1603 else if (errno
!= EAGAIN
)
1605 WARN("recv() failed: %s\n", strerror(errno
));
1610 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1614 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1618 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1619 const void *buffer
, unsigned int count
)
1621 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1622 int bytes_written
= 0;
1623 while (bytes_written
!= count
)
1625 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1628 else if (errno
== EINTR
)
1630 else if (errno
!= EAGAIN
)
1634 if (!rpcrt4_sock_wait_for_send(tcpc
))
1638 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1639 return bytes_written
;
1642 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1644 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1646 TRACE("%d\n", tcpc
->sock
);
1648 if (tcpc
->sock
!= -1)
1649 closesocket(tcpc
->sock
);
1651 rpcrt4_sock_wait_destroy(tcpc
);
1655 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1657 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1658 TRACE("%p\n", Connection
);
1659 rpcrt4_sock_wait_cancel(tcpc
);
1662 static RPC_STATUS
rpcrt4_conn_tcp_is_server_listening(const char *endpoint
)
1665 return RPC_S_ACCESS_DENIED
;
1668 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1670 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1672 TRACE("%p\n", Connection
);
1674 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1679 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1680 const char *networkaddr
,
1681 const char *endpoint
)
1683 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1684 EPM_PROTOCOL_TCP
, endpoint
);
1687 #ifdef HAVE_SOCKETPAIR
1689 typedef struct _RpcServerProtseq_sock
1691 RpcServerProtseq common
;
1694 } RpcServerProtseq_sock
;
1696 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1698 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1702 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1704 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1705 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1706 ps
->mgr_event_rcv
= fds
[0];
1707 ps
->mgr_event_snd
= fds
[1];
1711 ERR("socketpair failed with error %s\n", strerror(errno
));
1712 HeapFree(GetProcessHeap(), 0, ps
);
1719 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1721 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1723 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1726 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1728 struct pollfd
*poll_info
= prev_array
;
1729 RpcConnection_tcp
*conn
;
1730 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1732 EnterCriticalSection(&protseq
->cs
);
1734 /* open and count connections */
1736 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1738 if (conn
->sock
!= -1)
1740 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1743 /* make array of connections */
1745 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1747 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1750 ERR("couldn't allocate poll_info\n");
1751 LeaveCriticalSection(&protseq
->cs
);
1755 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1756 poll_info
[0].events
= POLLIN
;
1758 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1760 if (conn
->sock
!= -1)
1762 poll_info
[*count
].fd
= conn
->sock
;
1763 poll_info
[*count
].events
= POLLIN
;
1766 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1768 LeaveCriticalSection(&protseq
->cs
);
1772 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1774 HeapFree(GetProcessHeap(), 0, array
);
1777 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1779 struct pollfd
*poll_info
= wait_array
;
1782 RpcConnection
*cconn
;
1783 RpcConnection_tcp
*conn
;
1788 ret
= poll(poll_info
, count
, -1);
1791 ERR("poll failed with error %d\n", ret
);
1795 for (i
= 0; i
< count
; i
++)
1796 if (poll_info
[i
].revents
& POLLIN
)
1798 /* RPC server event */
1802 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1806 /* find which connection got a RPC */
1807 EnterCriticalSection(&protseq
->cs
);
1808 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1810 if (poll_info
[i
].fd
== conn
->sock
) break;
1811 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1815 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1817 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1818 LeaveCriticalSection(&protseq
->cs
);
1820 RPCRT4_new_client(cconn
);
1828 #else /* HAVE_SOCKETPAIR */
1830 typedef struct _RpcServerProtseq_sock
1832 RpcServerProtseq common
;
1834 } RpcServerProtseq_sock
;
1836 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1838 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1841 static BOOL wsa_inited
;
1845 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1846 /* Note: WSAStartup can be called more than once so we don't bother with
1847 * making accesses to wsa_inited thread-safe */
1850 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1855 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1857 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1858 SetEvent(sockps
->mgr_event
);
1861 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1863 HANDLE
*objs
= prev_array
;
1864 RpcConnection_tcp
*conn
;
1865 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1867 EnterCriticalSection(&protseq
->cs
);
1869 /* open and count connections */
1871 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1874 if (conn
->sock
!= -1)
1876 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1879 /* make array of connections */
1881 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1883 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1886 ERR("couldn't allocate objs\n");
1887 LeaveCriticalSection(&protseq
->cs
);
1891 objs
[0] = sockps
->mgr_event
;
1893 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1896 if (conn
->sock
!= -1)
1898 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1899 if (res
== SOCKET_ERROR
)
1900 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1903 objs
[*count
] = conn
->sock_event
;
1907 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1909 LeaveCriticalSection(&protseq
->cs
);
1913 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1915 HeapFree(GetProcessHeap(), 0, array
);
1918 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1921 HANDLE
*objs
= wait_array
;
1923 RpcConnection
*cconn
;
1924 RpcConnection_tcp
*conn
;
1931 /* an alertable wait isn't strictly necessary, but due to our
1932 * overlapped I/O implementation in Wine we need to free some memory
1933 * by the file user APC being called, even if no completion routine was
1934 * specified at the time of starting the async operation */
1935 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1936 } while (res
== WAIT_IO_COMPLETION
);
1938 if (res
== WAIT_OBJECT_0
)
1940 else if (res
== WAIT_FAILED
)
1942 ERR("wait failed with error %d\n", GetLastError());
1947 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1948 /* find which connection got a RPC */
1949 EnterCriticalSection(&protseq
->cs
);
1950 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1953 if (b_handle
== conn
->sock_event
) break;
1954 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1958 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1960 ERR("failed to locate connection for handle %p\n", b_handle
);
1961 LeaveCriticalSection(&protseq
->cs
);
1964 RPCRT4_new_client(cconn
);
1971 #endif /* HAVE_SOCKETPAIR */
1973 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1978 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1979 networkaddr
, EPM_PROTOCOL_TCP
,
1983 /**** ncacn_http support ****/
1985 /* 60 seconds is the period native uses */
1986 #define HTTP_IDLE_TIME 60000
1988 /* reference counted to avoid a race between a cancelled call's connection
1989 * being destroyed and the asynchronous InternetReadFileEx call being
1991 typedef struct _RpcHttpAsyncData
1994 HANDLE completion_event
;
1996 INTERNET_BUFFERSW inet_buffers
;
1997 CRITICAL_SECTION cs
;
2000 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
2002 return InterlockedIncrement(&data
->refs
);
2005 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
2007 ULONG refs
= InterlockedDecrement(&data
->refs
);
2010 TRACE("destroying async data %p\n", data
);
2011 CloseHandle(data
->completion_event
);
2012 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
2013 data
->cs
.DebugInfo
->Spare
[0] = 0;
2014 DeleteCriticalSection(&data
->cs
);
2015 HeapFree(GetProcessHeap(), 0, data
);
2020 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
2022 ResetEvent(async_data
->completion_event
);
2023 RpcHttpAsyncData_AddRef(async_data
);
2026 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
2028 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
2032 RpcHttpAsyncData_Release(async_data
);
2036 if(GetLastError() != ERROR_IO_PENDING
) {
2037 RpcHttpAsyncData_Release(async_data
);
2038 ERR("Request failed with error %d\n", GetLastError());
2039 return RPC_S_SERVER_UNAVAILABLE
;
2042 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2043 if(res
!= WAIT_OBJECT_0
) {
2044 TRACE("Cancelled\n");
2045 return RPC_S_CALL_CANCELLED
;
2048 if(async_data
->async_result
) {
2049 ERR("Async request failed with error %d\n", async_data
->async_result
);
2050 return RPC_S_SERVER_UNAVAILABLE
;
2065 unsigned int data_len
;
2066 BOOL finished
; /* finished authenticating */
2069 typedef struct _RpcConnection_http
2071 RpcConnection common
;
2074 HINTERNET in_request
;
2075 HINTERNET out_request
;
2077 HANDLE timer_cancelled
;
2078 HANDLE cancel_event
;
2079 DWORD last_sent_time
;
2080 ULONG bytes_received
;
2081 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
2082 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
2083 UUID connection_uuid
;
2086 RpcHttpAsyncData
*async_data
;
2087 } RpcConnection_http
;
2089 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
2091 RpcConnection_http
*httpc
;
2092 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
2093 if (!httpc
) return NULL
;
2094 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
2095 if (!httpc
->async_data
)
2097 HeapFree(GetProcessHeap(), 0, httpc
);
2100 TRACE("async data = %p\n", httpc
->async_data
);
2101 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2102 httpc
->async_data
->refs
= 1;
2103 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSW
);
2104 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2105 InitializeCriticalSection(&httpc
->async_data
->cs
);
2106 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
2107 return &httpc
->common
;
2110 typedef struct _HttpTimerThreadData
2113 DWORD
*last_sent_time
;
2114 HANDLE timer_cancelled
;
2115 } HttpTimerThreadData
;
2117 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
2119 HINTERNET in_request
= param
;
2120 RpcPktHdr
*idle_pkt
;
2122 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
2126 DWORD bytes_written
;
2127 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
2128 RPCRT4_FreeHeader(idle_pkt
);
2132 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
2134 DWORD cur_time
= GetTickCount();
2135 DWORD cached_last_sent_time
= *last_sent_time
;
2136 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
2139 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
2141 HttpTimerThreadData
*data_in
= param
;
2142 HttpTimerThreadData data
;
2146 HeapFree(GetProcessHeap(), 0, data_in
);
2148 for (timeout
= HTTP_IDLE_TIME
;
2149 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
2150 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
2152 /* are we too soon after last send? */
2153 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
2155 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2158 CloseHandle(data
.timer_cancelled
);
2162 static VOID WINAPI
rpcrt4_http_internet_callback(
2163 HINTERNET hInternet
,
2164 DWORD_PTR dwContext
,
2165 DWORD dwInternetStatus
,
2166 LPVOID lpvStatusInformation
,
2167 DWORD dwStatusInformationLength
)
2169 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2171 switch (dwInternetStatus
)
2173 case INTERNET_STATUS_REQUEST_COMPLETE
:
2174 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2177 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2179 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2180 SetEvent(async_data
->completion_event
);
2181 RpcHttpAsyncData_Release(async_data
);
2187 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2194 WCHAR
*status_text
= buf
;
2198 size
= sizeof(status_code
);
2199 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2201 return GetLastError();
2202 if (status_code
== HTTP_STATUS_OK
)
2206 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2207 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2209 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2210 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2213 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2214 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2216 if (status_code
== HTTP_STATUS_DENIED
)
2217 return ERROR_ACCESS_DENIED
;
2218 return RPC_S_SERVER_UNAVAILABLE
;
2221 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2223 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2224 LPWSTR proxy
= NULL
;
2226 LPWSTR password
= NULL
;
2227 LPWSTR servername
= NULL
;
2228 const WCHAR
*option
;
2231 if (httpc
->common
.QOS
&&
2232 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2234 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2235 if (http_cred
->TransportCredentials
)
2238 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2239 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2240 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2242 return RPC_S_OUT_OF_RESOURCES
;
2244 if (cred
->DomainLength
)
2246 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2247 p
+= cred
->DomainLength
;
2251 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2252 p
[cred
->UserLength
] = 0;
2254 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2258 for (option
= httpc
->common
.NetworkOptions
; option
;
2259 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2261 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2262 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2264 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2266 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2267 const WCHAR
*value_end
;
2270 value_end
= strchrW(option
, ',');
2272 value_end
= value_start
+ strlenW(value_start
);
2273 for (p
= value_start
; p
< value_end
; p
++)
2280 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2281 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2283 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2285 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2286 const WCHAR
*value_end
;
2288 value_end
= strchrW(option
, ',');
2290 value_end
= value_start
+ strlenW(value_start
);
2291 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2292 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2295 FIXME("unhandled option %s\n", debugstr_w(option
));
2298 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2299 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2300 if (!httpc
->app_info
)
2302 HeapFree(GetProcessHeap(), 0, password
);
2303 HeapFree(GetProcessHeap(), 0, user
);
2304 HeapFree(GetProcessHeap(), 0, proxy
);
2305 HeapFree(GetProcessHeap(), 0, servername
);
2306 ERR("InternetOpenW failed with error %d\n", GetLastError());
2307 return RPC_S_SERVER_UNAVAILABLE
;
2309 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2311 /* if no RpcProxy option specified, set the HTTP server address to the
2312 * RPC server address */
2315 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2318 HeapFree(GetProcessHeap(), 0, password
);
2319 HeapFree(GetProcessHeap(), 0, user
);
2320 HeapFree(GetProcessHeap(), 0, proxy
);
2321 return RPC_S_OUT_OF_RESOURCES
;
2323 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2326 port
= (httpc
->common
.QOS
&&
2327 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2328 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2329 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2331 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2332 INTERNET_SERVICE_HTTP
, 0, 0);
2334 HeapFree(GetProcessHeap(), 0, password
);
2335 HeapFree(GetProcessHeap(), 0, user
);
2336 HeapFree(GetProcessHeap(), 0, proxy
);
2338 if (!httpc
->session
)
2340 ERR("InternetConnectW failed with error %d\n", GetLastError());
2341 HeapFree(GetProcessHeap(), 0, servername
);
2342 return RPC_S_SERVER_UNAVAILABLE
;
2344 httpc
->servername
= servername
;
2348 static int rpcrt4_http_async_read(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2349 void *buffer
, unsigned int count
)
2353 unsigned int bytes_left
= count
;
2354 RPC_STATUS status
= RPC_S_OK
;
2356 async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
2360 async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2361 prepare_async_request(async_data
);
2362 ret
= InternetReadFileExW(req
, &async_data
->inet_buffers
, IRF_ASYNC
, 0);
2363 status
= wait_async_request(async_data
, ret
, cancel_event
);
2364 if (status
!= RPC_S_OK
)
2366 if (status
== RPC_S_CALL_CANCELLED
)
2367 TRACE("call cancelled\n");
2371 if (!async_data
->inet_buffers
.dwBufferLength
)
2373 memcpy(buf
, async_data
->inet_buffers
.lpvBuffer
,
2374 async_data
->inet_buffers
.dwBufferLength
);
2376 bytes_left
-= async_data
->inet_buffers
.dwBufferLength
;
2377 buf
+= async_data
->inet_buffers
.dwBufferLength
;
2380 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
2381 async_data
->inet_buffers
.lpvBuffer
= NULL
;
2383 TRACE("%p %p %u -> %u\n", req
, buffer
, count
, status
);
2384 return status
== RPC_S_OK
? count
: -1;
2387 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2393 TRACE("sending echo request to server\n");
2395 prepare_async_request(async_data
);
2396 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2397 status
= wait_async_request(async_data
, ret
, cancel_event
);
2398 if (status
!= RPC_S_OK
) return status
;
2400 status
= rpcrt4_http_check_response(req
);
2401 if (status
!= RPC_S_OK
) return status
;
2403 rpcrt4_http_async_read(req
, async_data
, cancel_event
, buf
, sizeof(buf
));
2404 /* FIXME: do something with retrieved data */
2409 static RPC_STATUS
insert_content_length_header(HINTERNET request
, DWORD len
)
2411 static const WCHAR fmtW
[] =
2412 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
2413 WCHAR header
[sizeof(fmtW
) / sizeof(fmtW
[0]) + 10];
2415 sprintfW(header
, fmtW
, len
);
2416 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
))) return RPC_S_OK
;
2417 return RPC_S_SERVER_UNAVAILABLE
;
2420 /* prepare the in pipe for use by RPC packets */
2421 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2422 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2423 const UUID
*association_uuid
, BOOL authorized
)
2428 INTERNET_BUFFERSW buffers_in
;
2429 DWORD bytes_written
;
2433 /* ask wininet to authorize, if necessary */
2434 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2435 if (status
!= RPC_S_OK
) return status
;
2437 memset(&buffers_in
, 0, sizeof(buffers_in
));
2438 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2439 /* FIXME: get this from the registry */
2440 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2441 status
= insert_content_length_header(in_request
, buffers_in
.dwBufferTotal
);
2442 if (status
!= RPC_S_OK
) return status
;
2444 prepare_async_request(async_data
);
2445 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2446 status
= wait_async_request(async_data
, ret
, cancel_event
);
2447 if (status
!= RPC_S_OK
) return status
;
2449 TRACE("sending HTTP connect header to server\n");
2450 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2451 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2452 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2453 RPCRT4_FreeHeader(hdr
);
2456 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2457 return RPC_S_SERVER_UNAVAILABLE
;
2463 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcHttpAsyncData
*async_data
,
2464 HANDLE cancel_event
, RpcPktHdr
*hdr
, BYTE
**data
)
2466 unsigned short data_len
;
2469 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, hdr
, sizeof(hdr
->common
)) < 0)
2470 return RPC_S_SERVER_UNAVAILABLE
;
2471 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2473 ERR("wrong packet type received %d or wrong frag_len %d\n",
2474 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2475 return RPC_S_PROTOCOL_ERROR
;
2478 size
= sizeof(hdr
->http
) - sizeof(hdr
->common
);
2479 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, &hdr
->common
+ 1, size
) < 0)
2480 return RPC_S_SERVER_UNAVAILABLE
;
2482 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2485 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2487 return RPC_S_OUT_OF_RESOURCES
;
2488 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, *data
, data_len
) < 0)
2490 HeapFree(GetProcessHeap(), 0, *data
);
2491 return RPC_S_SERVER_UNAVAILABLE
;
2497 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2499 ERR("invalid http packet\n");
2500 HeapFree(GetProcessHeap(), 0, *data
);
2501 return RPC_S_PROTOCOL_ERROR
;
2507 /* prepare the out pipe for use by RPC packets */
2508 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2509 HANDLE cancel_event
, const UUID
*connection_uuid
,
2510 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2516 BYTE
*data_from_server
;
2517 RpcPktHdr pkt_from_server
;
2518 ULONG field1
, field3
;
2523 /* ask wininet to authorize, if necessary */
2524 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2525 if (status
!= RPC_S_OK
) return status
;
2528 rpcrt4_http_async_read(out_request
, async_data
, cancel_event
, buf
, sizeof(buf
));
2530 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2531 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2533 status
= insert_content_length_header(out_request
, hdr
->common
.frag_len
);
2534 if (status
!= RPC_S_OK
)
2536 RPCRT4_FreeHeader(hdr
);
2540 TRACE("sending HTTP connect header to server\n");
2541 prepare_async_request(async_data
);
2542 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2543 status
= wait_async_request(async_data
, ret
, cancel_event
);
2544 RPCRT4_FreeHeader(hdr
);
2545 if (status
!= RPC_S_OK
) return status
;
2547 status
= rpcrt4_http_check_response(out_request
);
2548 if (status
!= RPC_S_OK
) return status
;
2550 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2551 &pkt_from_server
, &data_from_server
);
2552 if (status
!= RPC_S_OK
) return status
;
2553 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2555 HeapFree(GetProcessHeap(), 0, data_from_server
);
2556 if (status
!= RPC_S_OK
) return status
;
2557 TRACE("received (%d) from first prepare header\n", field1
);
2561 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2562 &pkt_from_server
, &data_from_server
);
2563 if (status
!= RPC_S_OK
) return status
;
2564 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2566 TRACE("http idle packet, waiting for real packet\n");
2567 HeapFree(GetProcessHeap(), 0, data_from_server
);
2568 if (pkt_from_server
.http
.num_data_items
!= 0)
2570 ERR("HTTP idle packet should have no data items instead of %d\n",
2571 pkt_from_server
.http
.num_data_items
);
2572 return RPC_S_PROTOCOL_ERROR
;
2575 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2576 &field1
, flow_control_increment
,
2578 HeapFree(GetProcessHeap(), 0, data_from_server
);
2579 if (status
!= RPC_S_OK
) return status
;
2580 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2585 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2587 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2592 /* first 6 bits, all from bin[0] */
2593 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2594 x
= (bin
[0] & 3) << 4;
2596 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2599 base64
[i
++] = enc
[x
];
2604 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2605 x
= (bin
[1] & 0x0f) << 2;
2607 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2610 base64
[i
++] = enc
[x
];
2614 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2616 /* last 6 bits, all from bin [2] */
2617 base64
[i
++] = enc
[bin
[2] & 0x3f];
2625 static inline char decode_char( WCHAR c
)
2627 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2628 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2629 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2630 if (c
== '+') return 62;
2631 if (c
== '/') return 63;
2635 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2638 char c0
, c1
, c2
, c3
;
2639 const WCHAR
*p
= base64
;
2643 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2644 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2645 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2646 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2650 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2651 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2652 buf
[i
+ 2] = (c2
<< 6) | c3
;
2660 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2661 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2663 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2666 else if (p
[3] == '=')
2668 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2669 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2670 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2674 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2675 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2681 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2682 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2683 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2684 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2688 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2689 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2690 buf
[i
+ 2] = (c2
<< 6) | c3
;
2697 static struct authinfo
*alloc_authinfo(void)
2699 struct authinfo
*ret
;
2701 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2703 SecInvalidateHandle(&ret
->cred
);
2704 SecInvalidateHandle(&ret
->ctx
);
2705 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2711 ret
->finished
= FALSE
;
2715 static void destroy_authinfo(struct authinfo
*info
)
2719 if (SecIsValidHandle(&info
->ctx
))
2720 DeleteSecurityContext(&info
->ctx
);
2721 if (SecIsValidHandle(&info
->cred
))
2722 FreeCredentialsHandle(&info
->cred
);
2724 HeapFree(GetProcessHeap(), 0, info
->data
);
2725 HeapFree(GetProcessHeap(), 0, info
);
2728 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2729 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2730 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2731 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2732 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2742 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2743 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2744 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2745 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2746 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2748 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2750 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2753 for (i
= 0; i
< num_auth_schemes
; i
++)
2755 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2756 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2761 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2763 DWORD len
, index
= 0;
2767 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2768 if (auth_scheme_from_header(buffer
) == scheme
) break;
2773 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2774 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2776 struct authinfo
*info
= *auth_ptr
;
2777 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2778 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2780 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2782 switch (creds
->AuthnSchemes
[0])
2784 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2786 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2787 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2789 info
->data_len
= userlen
+ passlen
+ 1;
2790 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2792 status
= RPC_S_OUT_OF_MEMORY
;
2795 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2796 info
->data
[userlen
] = ':';
2797 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2799 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2800 info
->finished
= TRUE
;
2804 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2805 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2808 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2809 SECURITY_STATUS ret
;
2810 SecBufferDesc out_desc
, in_desc
;
2812 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2816 WCHAR auth_value
[2048];
2817 DWORD size
= sizeof(auth_value
);
2820 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2821 else scheme
= negotiateW
;
2822 scheme_len
= strlenW( scheme
);
2827 SecPkgInfoW
*pkg_info
;
2829 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2830 if (ret
!= SEC_E_OK
) break;
2832 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2833 if (ret
!= SEC_E_OK
) break;
2835 info
->max_token
= pkg_info
->cbMaxToken
;
2836 FreeContextBuffer(pkg_info
);
2841 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2842 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2844 ERR("authentication scheme changed\n");
2848 in
.BufferType
= SECBUFFER_TOKEN
;
2852 in_desc
.ulVersion
= 0;
2853 in_desc
.cBuffers
= 1;
2854 in_desc
.pBuffers
= &in
;
2856 p
= auth_value
+ scheme_len
;
2857 if (!first
&& *p
== ' ')
2859 int len
= strlenW(++p
);
2860 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2861 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2862 decode_base64(p
, len
, in
.pvBuffer
);
2864 out
.BufferType
= SECBUFFER_TOKEN
;
2865 out
.cbBuffer
= info
->max_token
;
2866 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2868 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2871 out_desc
.ulVersion
= 0;
2872 out_desc
.cBuffers
= 1;
2873 out_desc
.pBuffers
= &out
;
2875 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2876 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2877 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2878 &info
->attr
, &info
->exp
);
2879 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2880 if (ret
== SEC_E_OK
)
2882 HeapFree(GetProcessHeap(), 0, info
->data
);
2883 info
->data
= out
.pvBuffer
;
2884 info
->data_len
= out
.cbBuffer
;
2885 info
->finished
= TRUE
;
2886 TRACE("sending last auth packet\n");
2889 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2891 HeapFree(GetProcessHeap(), 0, info
->data
);
2892 info
->data
= out
.pvBuffer
;
2893 info
->data_len
= out
.cbBuffer
;
2894 TRACE("sending next auth packet\n");
2899 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2900 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2903 info
->scheme
= creds
->AuthnSchemes
[0];
2907 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2911 if (status
!= RPC_S_OK
)
2913 destroy_authinfo(info
);
2921 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2923 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2924 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2925 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2926 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2927 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2928 const WCHAR
*scheme_str
;
2929 WCHAR
*header
, *ptr
;
2930 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2934 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2935 scheme_str
= basicW
;
2936 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2938 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2939 scheme_str
= negotiateW
;
2940 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2942 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2944 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2947 ERR("unknown scheme %u\n", scheme
);
2948 return RPC_S_SERVER_UNAVAILABLE
;
2950 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2952 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2953 ptr
= header
+ auth_len
;
2954 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2956 len
= encode_base64(data
, data_len
, ptr
);
2960 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2962 HeapFree(GetProcessHeap(), 0, header
);
2967 static void drain_content(HINTERNET request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2969 DWORD count
, len
= 0, size
= sizeof(len
);
2972 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2976 count
= min(sizeof(buf
), len
);
2977 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, buf
, count
) <= 0) return;
2982 static RPC_STATUS
authorize_request