f60ea9f50d153d4959717ebf6ab04ac8548f3bd3
[reactos.git] / reactos / dll / win32 / rpcrt4 / rpc_transport.c
1 /*
2 * RPC transport layer
3 *
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
9 *
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.
14 *
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.
19 *
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
23 *
24 */
25
26 #include "precomp.h"
27
28 #if defined(__MINGW32__) || defined (_MSC_VER)
29 # include <ws2tcpip.h>
30 # ifndef EADDRINUSE
31 # define EADDRINUSE WSAEADDRINUSE
32 # endif
33 # ifndef EAGAIN
34 # define EAGAIN WSAEWOULDBLOCK
35 # endif
36 # undef errno
37 # define errno WSAGetLastError()
38 #else
39 # include <errno.h>
40 # ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 # endif
43 # include <fcntl.h>
44 # ifdef HAVE_SYS_SOCKET_H
45 # include <sys/socket.h>
46 # endif
47 # ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
49 # endif
50 # ifdef HAVE_NETINET_TCP_H
51 # include <netinet/tcp.h>
52 # endif
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h>
55 # endif
56 # ifdef HAVE_NETDB_H
57 # include <netdb.h>
58 # endif
59 # ifdef HAVE_SYS_POLL_H
60 # include <sys/poll.h>
61 # endif
62 # ifdef HAVE_SYS_FILIO_H
63 # include <sys/filio.h>
64 # endif
65 # ifdef HAVE_SYS_IOCTL_H
66 # include <sys/ioctl.h>
67 # endif
68 # define closesocket close
69 # define ioctlsocket ioctl
70 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
71
72 #include <wininet.h>
73
74 #include "epm_towers.h"
75
76 #ifndef SOL_TCP
77 # define SOL_TCP IPPROTO_TCP
78 #endif
79
80 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
81
82 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
83
84 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
85
86 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection);
87
88 /**** ncacn_np support ****/
89
90 typedef struct _RpcConnection_np
91 {
92 RpcConnection common;
93 HANDLE pipe;
94 OVERLAPPED ovl;
95 BOOL listening;
96 } RpcConnection_np;
97
98 static RpcConnection *rpcrt4_conn_np_alloc(void)
99 {
100 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcConnection_np));
101 if (npc)
102 {
103 npc->pipe = NULL;
104 memset(&npc->ovl, 0, sizeof(npc->ovl));
105 npc->listening = FALSE;
106 }
107 return &npc->common;
108 }
109
110 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
111 {
112 if (npc->listening)
113 return RPC_S_OK;
114
115 npc->listening = TRUE;
116 for (;;)
117 {
118 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
119 return RPC_S_OK;
120
121 switch(GetLastError())
122 {
123 case ERROR_PIPE_CONNECTED:
124 SetEvent(npc->ovl.hEvent);
125 return RPC_S_OK;
126 case ERROR_IO_PENDING:
127 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
128 return RPC_S_OK;
129 case ERROR_NO_DATA_DETECTED:
130 /* client has disconnected, retry */
131 DisconnectNamedPipe( npc->pipe );
132 break;
133 default:
134 npc->listening = FALSE;
135 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
136 return RPC_S_OUT_OF_RESOURCES;
137 }
138 }
139 }
140
141 #ifndef __REACTOS__
142 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
143 {
144 if (npc->listening)
145 return RPC_S_OK;
146
147 npc->listening = TRUE;
148 npc->listen_thread = CreateThread(NULL, 0, listen_thread, npc, 0, NULL);
149 if (!npc->listen_thread)
150 {
151 npc->listening = FALSE;
152 ERR("Couldn't create listen thread (error was %d)\n", GetLastError());
153 return RPC_S_OUT_OF_RESOURCES;
154 }
155 return RPC_S_OK;
156 }
157 #endif
158
159 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
160 {
161 RpcConnection_np *npc = (RpcConnection_np *) Connection;
162 TRACE("listening on %s\n", pname);
163
164 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
165 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
166 PIPE_UNLIMITED_INSTANCES,
167 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
168 if (npc->pipe == INVALID_HANDLE_VALUE) {
169 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
170 if (GetLastError() == ERROR_FILE_EXISTS)
171 return RPC_S_DUPLICATE_ENDPOINT;
172 else
173 return RPC_S_CANT_CREATE_ENDPOINT;
174 }
175
176 memset(&npc->ovl, 0, sizeof(npc->ovl));
177 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
178
179 /* Note: we don't call ConnectNamedPipe here because it must be done in the
180 * server thread as the thread must be alertable */
181 return RPC_S_OK;
182 }
183
184 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
185 {
186 RpcConnection_np *npc = (RpcConnection_np *) Connection;
187 HANDLE pipe;
188 DWORD err, dwMode;
189
190 TRACE("connecting to %s\n", pname);
191
192 while (TRUE) {
193 DWORD dwFlags = 0;
194 if (Connection->QOS)
195 {
196 dwFlags = SECURITY_SQOS_PRESENT;
197 switch (Connection->QOS->qos->ImpersonationType)
198 {
199 case RPC_C_IMP_LEVEL_DEFAULT:
200 /* FIXME: what to do here? */
201 break;
202 case RPC_C_IMP_LEVEL_ANONYMOUS:
203 dwFlags |= SECURITY_ANONYMOUS;
204 break;
205 case RPC_C_IMP_LEVEL_IDENTIFY:
206 dwFlags |= SECURITY_IDENTIFICATION;
207 break;
208 case RPC_C_IMP_LEVEL_IMPERSONATE:
209 dwFlags |= SECURITY_IMPERSONATION;
210 break;
211 case RPC_C_IMP_LEVEL_DELEGATE:
212 dwFlags |= SECURITY_DELEGATION;
213 break;
214 }
215 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTITY_DYNAMIC)
216 dwFlags |= SECURITY_CONTEXT_TRACKING;
217 }
218 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
219 OPEN_EXISTING, dwFlags, 0);
220 if (pipe != INVALID_HANDLE_VALUE) break;
221 err = GetLastError();
222 if (err == ERROR_PIPE_BUSY) {
223 TRACE("connection failed, error=%x\n", err);
224 return RPC_S_SERVER_TOO_BUSY;
225 } else if (err == ERROR_BAD_NETPATH) {
226 TRACE("connection failed, error=%x\n", err);
227 return RPC_S_SERVER_UNAVAILABLE;
228 }
229 if (!wait || !WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
230 err = GetLastError();
231 WARN("connection failed, error=%x\n", err);
232 return RPC_S_SERVER_UNAVAILABLE;
233 }
234 }
235
236 /* success */
237 memset(&npc->ovl, 0, sizeof(npc->ovl));
238 /* pipe is connected; change to message-read mode. */
239 dwMode = PIPE_READMODE_MESSAGE;
240 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
241 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
242 npc->pipe = pipe;
243
244 return RPC_S_OK;
245 }
246
247 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
248 {
249 RpcConnection_np *npc = (RpcConnection_np *) Connection;
250 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
251 RPC_STATUS r;
252 LPSTR pname;
253
254 /* already connected? */
255 if (npc->pipe)
256 return RPC_S_OK;
257
258 /* protseq=ncalrpc: supposed to use NT LPC ports,
259 * but we'll implement it with named pipes for now */
260 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
261 strcat(strcpy(pname, prefix), Connection->Endpoint);
262 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
263 I_RpcFree(pname);
264
265 return r;
266 }
267
268 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, const char *endpoint)
269 {
270 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
271 RPC_STATUS r;
272 LPSTR pname;
273 RpcConnection *Connection;
274 char generated_endpoint[22];
275
276 if (!endpoint)
277 {
278 static LONG lrpc_nameless_id;
279 DWORD process_id = GetCurrentProcessId();
280 ULONG id = InterlockedIncrement(&lrpc_nameless_id);
281 snprintf(generated_endpoint, sizeof(generated_endpoint),
282 "LRPC%08x.%08x", process_id, id);
283 endpoint = generated_endpoint;
284 }
285
286 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
287 endpoint, NULL, NULL, NULL, NULL);
288 if (r != RPC_S_OK)
289 return r;
290
291 /* protseq=ncalrpc: supposed to use NT LPC ports,
292 * but we'll implement it with named pipes for now */
293 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
294 strcat(strcpy(pname, prefix), Connection->Endpoint);
295 r = rpcrt4_conn_create_pipe(Connection, pname);
296 I_RpcFree(pname);
297
298 EnterCriticalSection(&protseq->cs);
299 Connection->Next = protseq->conn;
300 protseq->conn = Connection;
301 LeaveCriticalSection(&protseq->cs);
302
303 return r;
304 }
305
306 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
307 {
308 RpcConnection_np *npc = (RpcConnection_np *) Connection;
309 static const char prefix[] = "\\\\";
310 static const char local[] =".";
311 RPC_STATUS r;
312 LPSTR pname;
313 INT size;
314
315 /* already connected? */
316 if (npc->pipe)
317 return RPC_S_OK;
318
319 /* protseq=ncacn_np: named pipes */
320 size = strlen(prefix);
321 if (Connection->NetworkAddr == NULL || strlen(Connection->NetworkAddr) == 0)
322 size += strlen(local);
323 else
324 size += strlen(Connection->NetworkAddr);
325 size += strlen(Connection->Endpoint) + 1;
326
327 pname = I_RpcAllocate(size);
328 strcpy(pname, prefix);
329 if (Connection->NetworkAddr == NULL || strlen(Connection->NetworkAddr) == 0)
330 strcat(pname, local);
331 else
332 strcat(pname, Connection->NetworkAddr);
333 strcat(pname, Connection->Endpoint);
334 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
335 I_RpcFree(pname);
336
337 return r;
338 }
339
340 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
341 {
342 static const char prefix[] = "\\\\.";
343 RPC_STATUS r;
344 LPSTR pname;
345 RpcConnection *Connection;
346 char generated_endpoint[21];
347
348 if (!endpoint)
349 {
350 static LONG np_nameless_id;
351 DWORD process_id = GetCurrentProcessId();
352 ULONG id = InterlockedExchangeAdd(&np_nameless_id, 1 );
353 snprintf(generated_endpoint, sizeof(generated_endpoint),
354 "\\\\pipe\\\\%08x.%03x", process_id, id);
355 endpoint = generated_endpoint;
356 }
357
358 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
359 endpoint, NULL, NULL, NULL, NULL);
360 if (r != RPC_S_OK)
361 return r;
362
363 /* protseq=ncacn_np: named pipes */
364 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
365 strcat(strcpy(pname, prefix), Connection->Endpoint);
366 r = rpcrt4_conn_create_pipe(Connection, pname);
367 I_RpcFree(pname);
368
369 EnterCriticalSection(&protseq->cs);
370 Connection->Next = protseq->conn;
371 protseq->conn = Connection;
372 LeaveCriticalSection(&protseq->cs);
373
374 return r;
375 }
376
377 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
378 {
379 /* because of the way named pipes work, we'll transfer the connected pipe
380 * to the child, then reopen the server binding to continue listening */
381
382 new_npc->pipe = old_npc->pipe;
383 new_npc->ovl = old_npc->ovl;
384 old_npc->pipe = 0;
385 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
386 old_npc->listening = FALSE;
387 }
388
389 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
390 {
391 RPC_STATUS status;
392 LPSTR pname;
393 static const char prefix[] = "\\\\.";
394
395 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
396
397 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
398 strcat(strcpy(pname, prefix), old_conn->Endpoint);
399 status = rpcrt4_conn_create_pipe(old_conn, pname);
400 I_RpcFree(pname);
401
402 return status;
403 }
404
405 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
406 {
407 RPC_STATUS status;
408 LPSTR pname;
409 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
410
411 TRACE("%s\n", old_conn->Endpoint);
412
413 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
414
415 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
416 strcat(strcpy(pname, prefix), old_conn->Endpoint);
417 status = rpcrt4_conn_create_pipe(old_conn, pname);
418 I_RpcFree(pname);
419
420 return status;
421 }
422
423 static int rpcrt4_conn_np_read(RpcConnection *Connection,
424 void *buffer, unsigned int count)
425 {
426 RpcConnection_np *npc = (RpcConnection_np *) Connection;
427 char *buf = buffer;
428 BOOL ret = TRUE;
429 unsigned int bytes_left = count;
430 OVERLAPPED ovl;
431
432 ZeroMemory(&ovl, sizeof(ovl));
433 ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
434
435 while (bytes_left)
436 {
437 DWORD bytes_read;
438 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, &ovl);
439 if (!ret && GetLastError() == ERROR_IO_PENDING)
440 ret = GetOverlappedResult(npc->pipe, &ovl, &bytes_read, TRUE);
441 if (!ret && GetLastError() == ERROR_MORE_DATA)
442 ret = TRUE;
443 if (!ret || !bytes_read)
444 break;
445 bytes_left -= bytes_read;
446 buf += bytes_read;
447 }
448 CloseHandle(ovl.hEvent);
449 return ret ? count : -1;
450 }
451
452 static int rpcrt4_conn_np_write(RpcConnection *Connection,
453 const void *buffer, unsigned int count)
454 {
455 RpcConnection_np *npc = (RpcConnection_np *) Connection;
456 const char *buf = buffer;
457 BOOL ret = TRUE;
458 unsigned int bytes_left = count;
459 OVERLAPPED ovl;
460
461 ZeroMemory(&ovl, sizeof(ovl));
462 ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
463
464 while (bytes_left)
465 {
466 DWORD bytes_written;
467 ret = WriteFile(npc->pipe, buf, bytes_left, &bytes_written, &ovl);
468 if (!ret && GetLastError() == ERROR_IO_PENDING)
469 ret = GetOverlappedResult(npc->pipe, &ovl, &bytes_written, TRUE);
470 if (!ret || !bytes_written)
471 break;
472 bytes_left -= bytes_written;
473 buf += bytes_written;
474 }
475 CloseHandle(ovl.hEvent);
476 return ret ? count : -1;
477 }
478
479 static int rpcrt4_conn_np_close(RpcConnection *Connection)
480 {
481 RpcConnection_np *npc = (RpcConnection_np *) Connection;
482 if (npc->pipe) {
483 FlushFileBuffers(npc->pipe);
484 CloseHandle(npc->pipe);
485 npc->pipe = 0;
486 }
487 if (npc->ovl.hEvent) {
488 CloseHandle(npc->ovl.hEvent);
489 npc->ovl.hEvent = 0;
490 }
491 return 0;
492 }
493
494 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
495 {
496 /* FIXME: implement when named pipe writes use overlapped I/O */
497 }
498
499 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
500 {
501 /* FIXME: implement when named pipe writes use overlapped I/O */
502 return -1;
503 }
504
505 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
506 const char *networkaddr,
507 const char *endpoint)
508 {
509 twr_empty_floor_t *smb_floor;
510 twr_empty_floor_t *nb_floor;
511 size_t size;
512 size_t networkaddr_size;
513 size_t endpoint_size;
514
515 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
516
517 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
518 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
519 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
520
521 if (!tower_data)
522 return size;
523
524 smb_floor = (twr_empty_floor_t *)tower_data;
525
526 tower_data += sizeof(*smb_floor);
527
528 smb_floor->count_lhs = sizeof(smb_floor->protid);
529 smb_floor->protid = EPM_PROTOCOL_SMB;
530 smb_floor->count_rhs = endpoint_size;
531
532 if (endpoint)
533 memcpy(tower_data, endpoint, endpoint_size);
534 else
535 tower_data[0] = 0;
536 tower_data += endpoint_size;
537
538 nb_floor = (twr_empty_floor_t *)tower_data;
539
540 tower_data += sizeof(*nb_floor);
541
542 nb_floor->count_lhs = sizeof(nb_floor->protid);
543 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
544 nb_floor->count_rhs = networkaddr_size;
545
546 if (networkaddr)
547 memcpy(tower_data, networkaddr, networkaddr_size);
548 else
549 tower_data[0] = 0;
550
551 return size;
552 }
553
554 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
555 size_t tower_size,
556 char **networkaddr,
557 char **endpoint)
558 {
559 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
560 const twr_empty_floor_t *nb_floor;
561
562 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
563
564 if (tower_size < sizeof(*smb_floor))
565 return EPT_S_NOT_REGISTERED;
566
567 tower_data += sizeof(*smb_floor);
568 tower_size -= sizeof(*smb_floor);
569
570 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
571 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
572 (smb_floor->count_rhs > tower_size) ||
573 (tower_data[smb_floor->count_rhs - 1] != '\0'))
574 return EPT_S_NOT_REGISTERED;
575
576 if (endpoint)
577 {
578 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
579 if (!*endpoint)
580 return RPC_S_OUT_OF_RESOURCES;
581 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
582 }
583 tower_data += smb_floor->count_rhs;
584 tower_size -= smb_floor->count_rhs;
585
586 if (tower_size < sizeof(*nb_floor))
587 return EPT_S_NOT_REGISTERED;
588
589 nb_floor = (const twr_empty_floor_t *)tower_data;
590
591 tower_data += sizeof(*nb_floor);
592 tower_size -= sizeof(*nb_floor);
593
594 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
595 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
596 (nb_floor->count_rhs > tower_size) ||
597 (tower_data[nb_floor->count_rhs - 1] != '\0'))
598 return EPT_S_NOT_REGISTERED;
599
600 if (networkaddr)
601 {
602 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
603 if (!*networkaddr)
604 {
605 if (endpoint)
606 {
607 I_RpcFree(*endpoint);
608 *endpoint = NULL;
609 }
610 return RPC_S_OUT_OF_RESOURCES;
611 }
612 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
613 }
614
615 return RPC_S_OK;
616 }
617
618 static RPC_STATUS rpcrt4_conn_np_impersonate_client(RpcConnection *conn)
619 {
620 RpcConnection_np *npc = (RpcConnection_np *)conn;
621 BOOL ret;
622
623 TRACE("(%p)\n", conn);
624
625 if (conn->AuthInfo && SecIsValidHandle(&conn->ctx))
626 return RPCRT4_default_impersonate_client(conn);
627
628 ret = ImpersonateNamedPipeClient(npc->pipe);
629 if (!ret)
630 {
631 DWORD error = GetLastError();
632 WARN("ImpersonateNamedPipeClient failed with error %u\n", error);
633 switch (error)
634 {
635 case ERROR_CANNOT_IMPERSONATE:
636 return RPC_S_NO_CONTEXT_AVAILABLE;
637 }
638 }
639 return RPC_S_OK;
640 }
641
642 static RPC_STATUS rpcrt4_conn_np_revert_to_self(RpcConnection *conn)
643 {
644 BOOL ret;
645
646 TRACE("(%p)\n", conn);
647
648 if (conn->AuthInfo && SecIsValidHandle(&conn->ctx))
649 return RPCRT4_default_revert_to_self(conn);
650
651 ret = RevertToSelf();
652 if (!ret)
653 {
654 WARN("RevertToSelf failed with error %u\n", GetLastError());
655 return RPC_S_NO_CONTEXT_AVAILABLE;
656 }
657 return RPC_S_OK;
658 }
659
660 typedef struct _RpcServerProtseq_np
661 {
662 RpcServerProtseq common;
663 HANDLE mgr_event;
664 } RpcServerProtseq_np;
665
666 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
667 {
668 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
669 if (ps)
670 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
671 return &ps->common;
672 }
673
674 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
675 {
676 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
677 SetEvent(npps->mgr_event);
678 }
679
680 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
681 {
682 HANDLE *objs = prev_array;
683 RpcConnection_np *conn;
684 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
685
686 EnterCriticalSection(&protseq->cs);
687
688 /* open and count connections */
689 *count = 1;
690 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
691 while (conn) {
692 rpcrt4_conn_listen_pipe(conn);
693 if (conn->ovl.hEvent)
694 (*count)++;
695 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
696 }
697
698 /* make array of connections */
699 if (objs)
700 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
701 else
702 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
703 if (!objs)
704 {
705 ERR("couldn't allocate objs\n");
706 LeaveCriticalSection(&protseq->cs);
707 return NULL;
708 }
709
710 objs[0] = npps->mgr_event;
711 *count = 1;
712 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
713 while (conn) {
714 if ((objs[*count] = conn->ovl.hEvent))
715 (*count)++;
716 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
717 }
718 LeaveCriticalSection(&protseq->cs);
719 return objs;
720 }
721
722 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
723 {
724 HeapFree(GetProcessHeap(), 0, array);
725 }
726
727 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
728 {
729 HANDLE b_handle;
730 HANDLE *objs = wait_array;
731 DWORD res;
732 RpcConnection *cconn;
733 RpcConnection_np *conn;
734
735 if (!objs)
736 return -1;
737
738 do
739 {
740 /* an alertable wait isn't strictly necessary, but due to our
741 * overlapped I/O implementation in Wine we need to free some memory
742 * by the file user APC being called, even if no completion routine was
743 * specified at the time of starting the async operation */
744 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
745 } while (res == WAIT_IO_COMPLETION);
746
747 if (res == WAIT_OBJECT_0)
748 return 0;
749 else if (res == WAIT_FAILED)
750 {
751 ERR("wait failed with error %d\n", GetLastError());
752 return -1;
753 }
754 else
755 {
756 b_handle = objs[res - WAIT_OBJECT_0];
757 /* find which connection got a RPC */
758 EnterCriticalSection(&protseq->cs);
759 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
760 while (conn) {
761 if (b_handle == conn->ovl.hEvent) break;
762 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
763 }
764 cconn = NULL;
765 if (conn)
766 RPCRT4_SpawnConnection(&cconn, &conn->common);
767 else
768 ERR("failed to locate connection for handle %p\n", b_handle);
769 LeaveCriticalSection(&protseq->cs);
770 if (cconn)
771 {
772 RPCRT4_new_client(cconn);
773 return 1;
774 }
775 else return -1;
776 }
777 }
778
779 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
780 const char *networkaddr,
781 const char *endpoint)
782 {
783 twr_empty_floor_t *pipe_floor;
784 size_t size;
785 size_t endpoint_size;
786
787 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
788
789 endpoint_size = strlen(endpoint) + 1;
790 size = sizeof(*pipe_floor) + endpoint_size;
791
792 if (!tower_data)
793 return size;
794
795 pipe_floor = (twr_empty_floor_t *)tower_data;
796
797 tower_data += sizeof(*pipe_floor);
798
799 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
800 pipe_floor->protid = EPM_PROTOCOL_PIPE;
801 pipe_floor->count_rhs = endpoint_size;
802
803 memcpy(tower_data, endpoint, endpoint_size);
804
805 return size;
806 }
807
808 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
809 size_t tower_size,
810 char **networkaddr,
811 char **endpoint)
812 {
813 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
814
815 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
816
817 if (tower_size < sizeof(*pipe_floor))
818 return EPT_S_NOT_REGISTERED;
819
820 tower_data += sizeof(*pipe_floor);
821 tower_size -= sizeof(*pipe_floor);
822
823 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
824 (pipe_floor->protid != EPM_PROTOCOL_PIPE) ||
825 (pipe_floor->count_rhs > tower_size) ||
826 (tower_data[pipe_floor->count_rhs - 1] != '\0'))
827 return EPT_S_NOT_REGISTERED;
828
829 if (networkaddr)
830 *networkaddr = NULL;
831
832 if (endpoint)
833 {
834 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
835 if (!*endpoint)
836 return RPC_S_OUT_OF_RESOURCES;
837 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
838 }
839
840 return RPC_S_OK;
841 }
842
843 static BOOL rpcrt4_ncalrpc_is_authorized(RpcConnection *conn)
844 {
845 return FALSE;
846 }
847
848 static RPC_STATUS rpcrt4_ncalrpc_authorize(RpcConnection *conn, BOOL first_time,
849 unsigned char *in_buffer,
850 unsigned int in_size,
851 unsigned char *out_buffer,
852 unsigned int *out_size)
853 {
854 /* since this protocol is local to the machine there is no need to
855 * authenticate the caller */
856 *out_size = 0;
857 return RPC_S_OK;
858 }
859
860 static RPC_STATUS rpcrt4_ncalrpc_secure_packet(RpcConnection *conn,
861 enum secure_packet_direction dir,
862 RpcPktHdr *hdr, unsigned int hdr_size,
863 unsigned char *stub_data, unsigned int stub_data_size,
864 RpcAuthVerifier *auth_hdr,
865 unsigned char *auth_value, unsigned int auth_value_size)
866 {
867 /* since this protocol is local to the machine there is no need to secure
868 * the packet */
869 return RPC_S_OK;
870 }
871
872 static RPC_STATUS rpcrt4_ncalrpc_inquire_auth_client(
873 RpcConnection *conn, RPC_AUTHZ_HANDLE *privs, RPC_WSTR *server_princ_name,
874 ULONG *authn_level, ULONG *authn_svc, ULONG *authz_svc, ULONG flags)
875 {
876 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn, privs,
877 server_princ_name, authn_level, authn_svc, authz_svc, flags);
878
879 if (privs)
880 {
881 FIXME("privs not implemented\n");
882 *privs = NULL;
883 }
884 if (server_princ_name)
885 {
886 FIXME("server_princ_name not implemented\n");
887 *server_princ_name = NULL;
888 }
889 if (authn_level) *authn_level = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
890 if (authn_svc) *authn_svc = RPC_C_AUTHN_WINNT;
891 if (authz_svc)
892 {
893 FIXME("authorization service not implemented\n");
894 *authz_svc = RPC_C_AUTHZ_NONE;
895 }
896 if (flags)
897 FIXME("flags 0x%x not implemented\n", flags);
898
899 return RPC_S_OK;
900 }
901
902 /**** ncacn_ip_tcp support ****/
903
904 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data,
905 const char *networkaddr,
906 unsigned char tcp_protid,
907 const char *endpoint)
908 {
909 twr_tcp_floor_t *tcp_floor;
910 twr_ipv4_floor_t *ipv4_floor;
911 struct addrinfo *ai;
912 struct addrinfo hints;
913 int ret;
914 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
915
916 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
917
918 if (!tower_data)
919 return size;
920
921 tcp_floor = (twr_tcp_floor_t *)tower_data;
922 tower_data += sizeof(*tcp_floor);
923
924 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
925
926 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
927 tcp_floor->protid = tcp_protid;
928 tcp_floor->count_rhs = sizeof(tcp_floor->port);
929
930 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
931 ipv4_floor->protid = EPM_PROTOCOL_IP;
932 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
933
934 hints.ai_flags = AI_NUMERICHOST;
935 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
936 hints.ai_family = PF_INET;
937 hints.ai_socktype = SOCK_STREAM;
938 hints.ai_protocol = IPPROTO_TCP;
939 hints.ai_addrlen = 0;
940 hints.ai_addr = NULL;
941 hints.ai_canonname = NULL;
942 hints.ai_next = NULL;
943
944 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
945 if (ret)
946 {
947 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
948 if (ret)
949 {
950 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
951 return 0;
952 }
953 }
954
955 if (ai->ai_family == PF_INET)
956 {
957 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
958 tcp_floor->port = sin->sin_port;
959 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
960 }
961 else
962 {
963 ERR("unexpected protocol family %d\n", ai->ai_family);
964 return 0;
965 }
966
967 freeaddrinfo(ai);
968
969 return size;
970 }
971
972 static RPC_STATUS rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
973 size_t tower_size,
974 char **networkaddr,
975 unsigned char tcp_protid,
976 char **endpoint)
977 {
978 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
979 const twr_ipv4_floor_t *ipv4_floor;
980 struct in_addr in_addr;
981
982 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
983
984 if (tower_size < sizeof(*tcp_floor))
985 return EPT_S_NOT_REGISTERED;
986
987 tower_data += sizeof(*tcp_floor);
988 tower_size -= sizeof(*tcp_floor);
989
990 if (tower_size < sizeof(*ipv4_floor))
991 return EPT_S_NOT_REGISTERED;
992
993 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
994
995 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
996 (tcp_floor->protid != tcp_protid) ||
997 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
998 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
999 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1000 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1001 return EPT_S_NOT_REGISTERED;
1002
1003 if (endpoint)
1004 {
1005 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1006 if (!*endpoint)
1007 return RPC_S_OUT_OF_RESOURCES;
1008 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1009 }
1010
1011 if (networkaddr)
1012 {
1013 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1014 if (!*networkaddr)
1015 {
1016 if (endpoint)
1017 {
1018 I_RpcFree(*endpoint);
1019 *endpoint = NULL;
1020 }
1021 return RPC_S_OUT_OF_RESOURCES;
1022 }
1023 in_addr.s_addr = ipv4_floor->ipv4addr;
1024 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1025 {
1026 ERR("inet_ntop: %s\n", strerror(errno));
1027 I_RpcFree(*networkaddr);
1028 *networkaddr = NULL;
1029 if (endpoint)
1030 {
1031 I_RpcFree(*endpoint);
1032 *endpoint = NULL;
1033 }
1034 return EPT_S_NOT_REGISTERED;
1035 }
1036 }
1037
1038 return RPC_S_OK;
1039 }
1040
1041 typedef struct _RpcConnection_tcp
1042 {
1043 RpcConnection common;
1044 int sock;
1045 #ifdef HAVE_SOCKETPAIR
1046 int cancel_fds[2];
1047 #else
1048 HANDLE sock_event;
1049 HANDLE cancel_event;
1050 #endif
1051 } RpcConnection_tcp;
1052
1053 #ifdef HAVE_SOCKETPAIR
1054
1055 static BOOL rpcrt4_sock_wait_init(RpcConnection_tcp *tcpc)
1056 {
1057 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
1058 {
1059 ERR("socketpair() failed: %s\n", strerror(errno));
1060 return FALSE;
1061 }
1062 return TRUE;
1063 }
1064
1065 static BOOL rpcrt4_sock_wait_for_recv(RpcConnection_tcp *tcpc)
1066 {
1067 struct pollfd pfds[2];
1068 pfds[0].fd = tcpc->sock;
1069 pfds[0].events = POLLIN;
1070 pfds[1].fd = tcpc->cancel_fds[0];
1071 pfds[1].events = POLLIN;
1072 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1073 {
1074 ERR("poll() failed: %s\n", strerror(errno));
1075 return FALSE;
1076 }
1077 if (pfds[1].revents & POLLIN) /* canceled */
1078 {
1079 char dummy;
1080 read(pfds[1].fd, &dummy, sizeof(dummy));
1081 return FALSE;
1082 }
1083 return TRUE;
1084 }
1085
1086 static BOOL rpcrt4_sock_wait_for_send(RpcConnection_tcp *tcpc)
1087 {
1088 struct pollfd pfd;
1089 pfd.fd = tcpc->sock;
1090 pfd.events = POLLOUT;
1091 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1092 {
1093 ERR("poll() failed: %s\n", strerror(errno));
1094 return FALSE;
1095 }
1096 return TRUE;
1097 }
1098
1099 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp *tcpc)
1100 {
1101 char dummy = 1;
1102
1103 write(tcpc->cancel_fds[1], &dummy, 1);
1104 }
1105
1106 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp *tcpc)
1107 {
1108 close(tcpc->cancel_fds[0]);
1109 close(tcpc->cancel_fds[1]);
1110 }
1111
1112 #else /* HAVE_SOCKETPAIR */
1113
1114 static BOOL rpcrt4_sock_wait_init(RpcConnection_tcp *tcpc)
1115 {
1116 static BOOL wsa_inited;
1117 if (!wsa_inited)
1118 {
1119 WSADATA wsadata;
1120 WSAStartup(MAKEWORD(2, 2), &wsadata);
1121 /* Note: WSAStartup can be called more than once so we don't bother with
1122 * making accesses to wsa_inited thread-safe */
1123 wsa_inited = TRUE;
1124 }
1125 tcpc->sock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1126 tcpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1127 if (!tcpc->sock_event || !tcpc->cancel_event)
1128 {
1129 ERR("event creation failed\n");
1130 if (tcpc->sock_event) CloseHandle(tcpc->sock_event);
1131 return FALSE;
1132 }
1133 return TRUE;
1134 }
1135
1136 static BOOL rpcrt4_sock_wait_for_recv(RpcConnection_tcp *tcpc)
1137 {
1138 HANDLE wait_handles[2];
1139 DWORD res;
1140 if (WSAEventSelect(tcpc->sock, tcpc->sock_event, FD_READ | FD_CLOSE) == SOCKET_ERROR)
1141 {
1142 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1143 return FALSE;
1144 }
1145 wait_handles[0] = tcpc->sock_event;
1146 wait_handles[1] = tcpc->cancel_event;
1147 res = WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE);
1148 switch (res)
1149 {
1150 case WAIT_OBJECT_0:
1151 return TRUE;
1152 case WAIT_OBJECT_0 + 1:
1153 return FALSE;
1154 default:
1155 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1156 return FALSE;
1157 }
1158 }
1159
1160 static BOOL rpcrt4_sock_wait_for_send(RpcConnection_tcp *tcpc)
1161 {
1162 DWORD res;
1163 if (WSAEventSelect(tcpc->sock, tcpc->sock_event, FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
1164 {
1165 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1166 return FALSE;
1167 }
1168 res = WaitForSingleObject(tcpc->sock_event, INFINITE);
1169 switch (res)
1170 {
1171 case WAIT_OBJECT_0:
1172 return TRUE;
1173 default:
1174 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1175 return FALSE;
1176 }
1177 }
1178
1179 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp *tcpc)
1180 {
1181 SetEvent(tcpc->cancel_event);
1182 }
1183
1184 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp *tcpc)
1185 {
1186 CloseHandle(tcpc->sock_event);
1187 CloseHandle(tcpc->cancel_event);
1188 }
1189
1190 #endif
1191
1192 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
1193 {
1194 RpcConnection_tcp *tcpc;
1195 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
1196 if (tcpc == NULL)
1197 return NULL;
1198 tcpc->sock = -1;
1199 if (!rpcrt4_sock_wait_init(tcpc))
1200 {
1201 HeapFree(GetProcessHeap(), 0, tcpc);
1202 return NULL;
1203 }
1204 return &tcpc->common;
1205 }
1206
1207 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
1208 {
1209 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1210 int sock;
1211 int ret;
1212 struct addrinfo *ai;
1213 struct addrinfo *ai_cur;
1214 struct addrinfo hints;
1215
1216 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
1217
1218 if (tcpc->sock != -1)
1219 return RPC_S_OK;
1220
1221 hints.ai_flags = 0;
1222 hints.ai_family = PF_UNSPEC;
1223 hints.ai_socktype = SOCK_STREAM;
1224 hints.ai_protocol = IPPROTO_TCP;
1225 hints.ai_addrlen = 0;
1226 hints.ai_addr = NULL;
1227 hints.ai_canonname = NULL;
1228 hints.ai_next = NULL;
1229
1230 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
1231 if (ret)
1232 {
1233 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
1234 Connection->Endpoint, gai_strerror(ret));
1235 return RPC_S_SERVER_UNAVAILABLE;
1236 }
1237
1238 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
1239 {
1240 int val;
1241 u_long nonblocking;
1242
1243 if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
1244 {
1245 TRACE("skipping non-IP/IPv6 address family\n");
1246 continue;
1247 }
1248
1249 if (TRACE_ON(rpc))
1250 {
1251 char host[256];
1252 char service[256];
1253 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
1254 host, sizeof(host), service, sizeof(service),
1255 NI_NUMERICHOST | NI_NUMERICSERV);
1256 TRACE("trying %s:%s\n", host, service);
1257 }
1258
1259 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
1260 if (sock == -1)
1261 {
1262 WARN("socket() failed: %s\n", strerror(errno));
1263 continue;
1264 }
1265
1266 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
1267 {
1268 WARN("connect() failed: %s\n", strerror(errno));
1269 closesocket(sock);
1270 continue;
1271 }
1272
1273 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1274 val = 1;
1275 setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
1276 nonblocking = 1;
1277 ioctlsocket(sock, FIONBIO, &nonblocking);
1278
1279 tcpc->sock = sock;
1280
1281 freeaddrinfo(ai);
1282 TRACE("connected\n");
1283 return RPC_S_OK;
1284 }
1285
1286 freeaddrinfo(ai);
1287 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
1288 return RPC_S_SERVER_UNAVAILABLE;
1289 }
1290
1291 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
1292 {
1293 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
1294 int sock;
1295 int ret;
1296 struct addrinfo *ai;
1297 struct addrinfo *ai_cur;
1298 struct addrinfo hints;
1299 RpcConnection *first_connection = NULL;
1300
1301 TRACE("(%p, %s)\n", protseq, endpoint);
1302
1303 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
1304 hints.ai_family = PF_UNSPEC;
1305 hints.ai_socktype = SOCK_STREAM;
1306 hints.ai_protocol = IPPROTO_TCP;
1307 hints.ai_addrlen = 0;
1308 hints.ai_addr = NULL;
1309 hints.ai_canonname = NULL;
1310 hints.ai_next = NULL;
1311
1312 ret = getaddrinfo(NULL, endpoint ? endpoint : "0", &hints, &ai);
1313 if (ret)
1314 {
1315 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
1316 gai_strerror(ret));
1317 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
1318 return RPC_S_INVALID_ENDPOINT_FORMAT;
1319 return RPC_S_CANT_CREATE_ENDPOINT;
1320 }
1321
1322 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
1323 {
1324 RpcConnection_tcp *tcpc;
1325 RPC_STATUS create_status;
1326 struct sockaddr_storage sa;
1327 socklen_t sa_len;
1328 char service[NI_MAXSERV];
1329 u_long nonblocking;
1330
1331 if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
1332 {
1333 TRACE("skipping non-IP/IPv6 address family\n");
1334 continue;
1335 }
1336
1337 if (TRACE_ON(rpc))
1338 {
1339 char host[256];
1340 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
1341 host, sizeof(host), service, sizeof(service),
1342 NI_NUMERICHOST | NI_NUMERICSERV);
1343 TRACE("trying %s:%s\n", host, service);
1344 }
1345
1346 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
1347 if (sock == -1)
1348 {
1349 WARN("socket() failed: %s\n", strerror(errno));
1350 status = RPC_S_CANT_CREATE_ENDPOINT;
1351 continue;
1352 }
1353
1354 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
1355 if (ret < 0)
1356 {
1357 WARN("bind failed: %s\n", strerror(errno));
1358 closesocket(sock);
1359 if (errno == EADDRINUSE)
1360 status = RPC_S_DUPLICATE_ENDPOINT;
1361 else
1362 status = RPC_S_CANT_CREATE_ENDPOINT;
1363 continue;
1364 }
1365
1366 sa_len = sizeof(sa);
1367 if (getsockname(sock, (struct sockaddr *)&sa, &sa_len))
1368 {
1369 WARN("getsockname() failed: %s\n", strerror(errno));
1370 closesocket(sock);
1371 status = RPC_S_CANT_CREATE_ENDPOINT;
1372 continue;
1373 }
1374
1375 ret = getnameinfo((struct sockaddr *)&sa, sa_len,
1376 NULL, 0, service, sizeof(service),
1377 NI_NUMERICSERV);
1378 if (ret)
1379 {
1380 WARN("getnameinfo failed: %s\n", gai_strerror(ret));
1381 closesocket(sock);
1382 status = RPC_S_CANT_CREATE_ENDPOINT;
1383 continue;
1384 }
1385
1386 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
1387 protseq->Protseq, NULL,
1388 service, NULL, NULL, NULL, NULL);
1389 if (create_status != RPC_S_OK)
1390 {
1391 closesocket(sock);
1392 status = create_status;
1393 continue;
1394 }
1395
1396 tcpc->sock = sock;
1397 ret = listen(sock, protseq->MaxCalls);
1398 if (ret < 0)
1399 {
1400 WARN("listen failed: %s\n", strerror(errno));
1401 RPCRT4_ReleaseConnection(&tcpc->common);
1402 status = RPC_S_OUT_OF_RESOURCES;
1403 continue;
1404 }
1405 /* need a non-blocking socket, otherwise accept() has a potential
1406 * race-condition (poll() says it is readable, connection drops,
1407 * and accept() blocks until the next connection comes...)
1408 */
1409 nonblocking = 1;
1410 ret = ioctlsocket(sock, FIONBIO, &nonblocking);
1411 if (ret < 0)
1412 {
1413 WARN("couldn't make socket non-blocking, error %d\n", ret);
1414 RPCRT4_ReleaseConnection(&tcpc->common);
1415 status = RPC_S_OUT_OF_RESOURCES;
1416 continue;
1417 }
1418
1419 tcpc->common.Next = first_connection;
1420 first_connection = &tcpc->common;
1421
1422 /* since IPv4 and IPv6 share the same port space, we only need one
1423 * successful bind to listen for both */
1424 break;
1425 }
1426
1427 freeaddrinfo(ai);
1428
1429 /* if at least one connection was created for an endpoint then
1430 * return success */
1431 if (first_connection)
1432 {
1433 RpcConnection *conn;
1434
1435 /* find last element in list */
1436 for (conn = first_connection; conn->Next; conn = conn->Next)
1437 ;
1438
1439 EnterCriticalSection(&protseq->cs);
1440 conn->Next = protseq->conn;
1441 protseq->conn = first_connection;
1442 LeaveCriticalSection(&protseq->cs);
1443
1444 TRACE("listening on %s\n", endpoint);
1445 return RPC_S_OK;
1446 }
1447
1448 ERR("couldn't listen on port %s\n", endpoint);
1449 return status;
1450 }
1451
1452 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
1453 {
1454 int ret;
1455 struct sockaddr_in address;
1456 socklen_t addrsize;
1457 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
1458 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
1459 u_long nonblocking;
1460
1461 addrsize = sizeof(address);
1462 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
1463 if (ret < 0)
1464 {
1465 ERR("Failed to accept a TCP connection: error %d\n", ret);
1466 return RPC_S_OUT_OF_RESOURCES;
1467 }
1468 nonblocking = 1;
1469 ioctlsocket(ret, FIONBIO, &nonblocking);
1470 client->sock = ret;
1471 TRACE("Accepted a new TCP connection\n");
1472 return RPC_S_OK;
1473 }
1474
1475 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
1476 void *buffer, unsigned int count)
1477 {
1478 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1479 int bytes_read = 0;
1480 while (bytes_read != count)
1481 {
1482 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
1483 if (!r)
1484 return -1;
1485 else if (r > 0)
1486 bytes_read += r;
1487 else if (errno != EAGAIN)
1488 {
1489 WARN("recv() failed: %s\n", strerror(errno));
1490 return -1;
1491 }
1492 else
1493 {
1494 if (!rpcrt4_sock_wait_for_recv(tcpc))
1495 return -1;
1496 }
1497 }
1498 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1499 return bytes_read;
1500 }
1501
1502 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1503 const void *buffer, unsigned int count)
1504 {
1505 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1506 int bytes_written = 0;
1507 while (bytes_written != count)
1508 {
1509 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1510 if (r >= 0)
1511 bytes_written += r;
1512 else if (errno != EAGAIN)
1513 return -1;
1514 else
1515 {
1516 if (!rpcrt4_sock_wait_for_send(tcpc))
1517 return -1;
1518 }
1519 }
1520 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1521 return bytes_written;
1522 }
1523
1524 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1525 {
1526 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1527
1528 TRACE("%d\n", tcpc->sock);
1529
1530 if (tcpc->sock != -1)
1531 closesocket(tcpc->sock);
1532 tcpc->sock = -1;
1533 rpcrt4_sock_wait_destroy(tcpc);
1534 return 0;
1535 }
1536
1537 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1538 {
1539 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1540 TRACE("%p\n", Connection);
1541 rpcrt4_sock_wait_cancel(tcpc);
1542 }
1543
1544 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1545 {
1546 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1547
1548 TRACE("%p\n", Connection);
1549
1550 if (!rpcrt4_sock_wait_for_recv(tcpc))
1551 return -1;
1552 return 0;
1553 }
1554
1555 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1556 const char *networkaddr,
1557 const char *endpoint)
1558 {
1559 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
1560 EPM_PROTOCOL_TCP, endpoint);
1561 }
1562
1563 #ifdef HAVE_SOCKETPAIR
1564
1565 typedef struct _RpcServerProtseq_sock
1566 {
1567 RpcServerProtseq common;
1568 int mgr_event_rcv;
1569 int mgr_event_snd;
1570 } RpcServerProtseq_sock;
1571
1572 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1573 {
1574 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1575 if (ps)
1576 {
1577 int fds[2];
1578 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1579 {
1580 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1581 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1582 ps->mgr_event_rcv = fds[0];
1583 ps->mgr_event_snd = fds[1];
1584 }
1585 else
1586 {
1587 ERR("socketpair failed with error %s\n", strerror(errno));
1588 HeapFree(GetProcessHeap(), 0, ps);
1589 return NULL;
1590 }
1591 }
1592 return &ps->common;
1593 }
1594
1595 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1596 {
1597 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1598 char dummy = 1;
1599 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1600 }
1601
1602 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1603 {
1604 struct pollfd *poll_info = prev_array;
1605 RpcConnection_tcp *conn;
1606 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1607
1608 EnterCriticalSection(&protseq->cs);
1609
1610 /* open and count connections */
1611 *count = 1;
1612 conn = (RpcConnection_tcp *)protseq->conn;
1613 while (conn) {
1614 if (conn->sock != -1)
1615 (*count)++;
1616 conn = (RpcConnection_tcp *)conn->common.Next;
1617 }
1618
1619 /* make array of connections */
1620 if (poll_info)
1621 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1622 else
1623 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1624 if (!poll_info)
1625 {
1626 ERR("couldn't allocate poll_info\n");
1627 LeaveCriticalSection(&protseq->cs);
1628 return NULL;
1629 }
1630
1631 poll_info[0].fd = sockps->mgr_event_rcv;
1632 poll_info[0].events = POLLIN;
1633 *count = 1;
1634 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1635 while (conn) {
1636 if (conn->sock != -1)
1637 {
1638 poll_info[*count].fd = conn->sock;
1639 poll_info[*count].events = POLLIN;
1640 (*count)++;
1641 }
1642 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1643 }
1644 LeaveCriticalSection(&protseq->cs);
1645 return poll_info;
1646 }
1647
1648 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1649 {
1650 HeapFree(GetProcessHeap(), 0, array);
1651 }
1652
1653 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1654 {
1655 struct pollfd *poll_info = wait_array;
1656 int ret;
1657 unsigned int i;
1658 RpcConnection *cconn;
1659 RpcConnection_tcp *conn;
1660
1661 if (!poll_info)
1662 return -1;
1663
1664 ret = poll(poll_info, count, -1);
1665 if (ret < 0)
1666 {
1667 ERR("poll failed with error %d\n", ret);
1668 return -1;
1669 }
1670
1671 for (i = 0; i < count; i++)
1672 if (poll_info[i].revents & POLLIN)
1673 {
1674 /* RPC server event */
1675 if (i == 0)
1676 {
1677 char dummy;
1678 read(poll_info[0].fd, &dummy, sizeof(dummy));
1679 return 0;
1680 }
1681
1682 /* find which connection got a RPC */
1683 EnterCriticalSection(&protseq->cs);
1684 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1685 while (conn) {
1686 if (poll_info[i].fd == conn->sock) break;
1687 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1688 }
1689 cconn = NULL;
1690 if (conn)
1691 RPCRT4_SpawnConnection(&cconn, &conn->common);
1692 else
1693 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1694 LeaveCriticalSection(&protseq->cs);
1695 if (cconn)
1696 RPCRT4_new_client(cconn);
1697 else
1698 return -1;
1699 }
1700
1701 return 1;
1702 }
1703
1704 #else /* HAVE_SOCKETPAIR */
1705
1706 typedef struct _RpcServerProtseq_sock
1707 {
1708 RpcServerProtseq common;
1709 HANDLE mgr_event;
1710 } RpcServerProtseq_sock;
1711
1712 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1713 {
1714 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1715 if (ps)
1716 {
1717 static BOOL wsa_inited;
1718 if (!wsa_inited)
1719 {
1720 WSADATA wsadata;
1721 WSAStartup(MAKEWORD(2, 2), &wsadata);
1722 /* Note: WSAStartup can be called more than once so we don't bother with
1723 * making accesses to wsa_inited thread-safe */
1724 wsa_inited = TRUE;
1725 }
1726 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1727 }
1728 return &ps->common;
1729 }
1730
1731 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1732 {
1733 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1734 SetEvent(sockps->mgr_event);
1735 }
1736
1737 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1738 {
1739 HANDLE *objs = prev_array;
1740 RpcConnection_tcp *conn;
1741 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1742
1743 EnterCriticalSection(&protseq->cs);
1744
1745 /* open and count connections */
1746 *count = 1;
1747 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1748 while (conn)
1749 {
1750 if (conn->sock != -1)
1751 (*count)++;
1752 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1753 }
1754
1755 /* make array of connections */
1756 if (objs)
1757 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
1758 else
1759 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
1760 if (!objs)
1761 {
1762 ERR("couldn't allocate objs\n");
1763 LeaveCriticalSection(&protseq->cs);
1764 return NULL;
1765 }
1766
1767 objs[0] = sockps->mgr_event;
1768 *count = 1;
1769 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1770 while (conn)
1771 {
1772 if (conn->sock != -1)
1773 {
1774 int res = WSAEventSelect(conn->sock, conn->sock_event, FD_ACCEPT);
1775 if (res == SOCKET_ERROR)
1776 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1777 else
1778 {
1779 objs[*count] = conn->sock_event;
1780 (*count)++;
1781 }
1782 }
1783 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1784 }
1785 LeaveCriticalSection(&protseq->cs);
1786 return objs;
1787 }
1788
1789 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1790 {
1791 HeapFree(GetProcessHeap(), 0, array);
1792 }
1793
1794 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1795 {
1796 HANDLE b_handle;
1797 HANDLE *objs = wait_array;
1798 DWORD res;
1799 RpcConnection *cconn;
1800 RpcConnection_tcp *conn;
1801
1802 if (!objs)
1803 return -1;
1804
1805 do
1806 {
1807 /* an alertable wait isn't strictly necessary, but due to our
1808 * overlapped I/O implementation in Wine we need to free some memory
1809 * by the file user APC being called, even if no completion routine was
1810 * specified at the time of starting the async operation */
1811 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
1812 } while (res == WAIT_IO_COMPLETION);
1813
1814 if (res == WAIT_OBJECT_0)
1815 return 0;
1816 else if (res == WAIT_FAILED)
1817 {
1818 ERR("wait failed with error %d\n", GetLastError());
1819 return -1;
1820 }
1821 else
1822 {
1823 b_handle = objs[res - WAIT_OBJECT_0];
1824 /* find which connection got a RPC */
1825 EnterCriticalSection(&protseq->cs);
1826 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1827 while (conn)
1828 {
1829 if (b_handle == conn->sock_event) break;
1830 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1831 }
1832 cconn = NULL;
1833 if (conn)
1834 RPCRT4_SpawnConnection(&cconn, &conn->common);
1835 else
1836 ERR("failed to locate connection for handle %p\n", b_handle);
1837 LeaveCriticalSection(&protseq->cs);
1838 if (cconn)
1839 {
1840 RPCRT4_new_client(cconn);
1841 return 1;
1842 }
1843 else return -1;
1844 }
1845 }
1846
1847 #endif /* HAVE_SOCKETPAIR */
1848
1849 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1850 size_t tower_size,
1851 char **networkaddr,
1852 char **endpoint)
1853 {
1854 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
1855 networkaddr, EPM_PROTOCOL_TCP,
1856 endpoint);
1857 }
1858
1859 /**** ncacn_http support ****/
1860
1861 /* 60 seconds is the period native uses */
1862 #define HTTP_IDLE_TIME 60000
1863
1864 /* reference counted to avoid a race between a cancelled call's connection
1865 * being destroyed and the asynchronous InternetReadFileEx call being
1866 * completed */
1867 typedef struct _RpcHttpAsyncData
1868 {
1869 LONG refs;
1870 HANDLE completion_event;
1871 WORD async_result;
1872 INTERNET_BUFFERSA inet_buffers;
1873 CRITICAL_SECTION cs;
1874 } RpcHttpAsyncData;
1875
1876 static ULONG RpcHttpAsyncData_AddRef(RpcHttpAsyncData *data)
1877 {
1878 return InterlockedIncrement(&data->refs);
1879 }
1880
1881 static ULONG RpcHttpAsyncData_Release(RpcHttpAsyncData *data)
1882 {
1883 ULONG refs = InterlockedDecrement(&data->refs);
1884 if (!refs)
1885 {
1886 TRACE("destroying async data %p\n", data);
1887 CloseHandle(data->completion_event);
1888 HeapFree(GetProcessHeap(), 0, data->inet_buffers.lpvBuffer);
1889 data->cs.DebugInfo->Spare[0] = 0;
1890 DeleteCriticalSection(&data->cs);
1891 HeapFree(GetProcessHeap(), 0, data);
1892 }
1893 return refs;
1894 }
1895
1896 static void prepare_async_request(RpcHttpAsyncData *async_data)
1897 {
1898 ResetEvent(async_data->completion_event);
1899 RpcHttpAsyncData_AddRef(async_data);
1900 }
1901
1902 static RPC_STATUS wait_async_request(RpcHttpAsyncData *async_data, BOOL call_ret, HANDLE cancel_event)
1903 {
1904 HANDLE handles[2] = { async_data->completion_event, cancel_event };
1905 DWORD res;
1906
1907 if(call_ret) {
1908 RpcHttpAsyncData_Release(async_data);
1909 return RPC_S_OK;
1910 }
1911
1912 if(GetLastError() != ERROR_IO_PENDING) {
1913 RpcHttpAsyncData_Release(async_data);
1914 ERR("Request failed with error %d\n", GetLastError());
1915 return RPC_S_SERVER_UNAVAILABLE;
1916 }
1917
1918 res = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
1919 if(res != WAIT_OBJECT_0) {
1920 TRACE("Cancelled\n");
1921 return RPC_S_CALL_CANCELLED;
1922 }
1923
1924 if(async_data->async_result) {
1925 ERR("Async request failed with error %d\n", async_data->async_result);
1926 return RPC_S_SERVER_UNAVAILABLE;
1927 }
1928
1929 return RPC_S_OK;
1930 }
1931
1932 struct authinfo
1933 {
1934 DWORD scheme;
1935 CredHandle cred;
1936 CtxtHandle ctx;
1937 TimeStamp exp;
1938 ULONG attr;
1939 ULONG max_token;
1940 char *data;
1941 unsigned int data_len;
1942 BOOL finished; /* finished authenticating */
1943 };
1944
1945 typedef struct _RpcConnection_http
1946 {
1947 RpcConnection common;
1948 HINTERNET app_info;
1949 HINTERNET session;
1950 HINTERNET in_request;
1951 HINTERNET out_request;
1952 WCHAR *servername;
1953 HANDLE timer_cancelled;
1954 HANDLE cancel_event;
1955 DWORD last_sent_time;
1956 ULONG bytes_received;
1957 ULONG flow_control_mark; /* send a control packet to the server when this many bytes received */
1958 ULONG flow_control_increment; /* number of bytes to increment flow_control_mark by */
1959 UUID connection_uuid;
1960 UUID in_pipe_uuid;
1961 UUID out_pipe_uuid;
1962 RpcHttpAsyncData *async_data;
1963 } RpcConnection_http;
1964
1965 static RpcConnection *rpcrt4_ncacn_http_alloc(void)
1966 {
1967 RpcConnection_http *httpc;
1968 httpc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*httpc));
1969 if (!httpc) return NULL;
1970 httpc->async_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcHttpAsyncData));
1971 if (!httpc->async_data)
1972 {
1973 HeapFree(GetProcessHeap(), 0, httpc);
1974 return NULL;
1975 }
1976 TRACE("async data = %p\n", httpc->async_data);
1977 httpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1978 httpc->async_data->refs = 1;
1979 httpc->async_data->inet_buffers.dwStructSize = sizeof(INTERNET_BUFFERSA);
1980 httpc->async_data->inet_buffers.lpvBuffer = NULL;
1981 InitializeCriticalSection(&httpc->async_data->cs);
1982 httpc->async_data->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RpcHttpAsyncData.cs");
1983 return &httpc->common;
1984 }
1985
1986 typedef struct _HttpTimerThreadData
1987 {
1988 PVOID timer_param;
1989 DWORD *last_sent_time;
1990 HANDLE timer_cancelled;
1991 } HttpTimerThreadData;
1992
1993 static VOID rpcrt4_http_keep_connection_active_timer_proc(PVOID param, BOOLEAN dummy)
1994 {
1995 HINTERNET in_request = param;
1996 RpcPktHdr *idle_pkt;
1997
1998 idle_pkt = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x0001,
1999 0, 0);
2000 if (idle_pkt)
2001 {
2002 DWORD bytes_written;
2003 InternetWriteFile(in_request, idle_pkt, idle_pkt->common.frag_len, &bytes_written);
2004 RPCRT4_FreeHeader(idle_pkt);
2005 }
2006 }
2007
2008 static inline DWORD rpcrt4_http_timer_calc_timeout(DWORD *last_sent_time)
2009 {
2010 DWORD cur_time = GetTickCount();
2011 DWORD cached_last_sent_time = *last_sent_time;
2012 return HTTP_IDLE_TIME - (cur_time - cached_last_sent_time > HTTP_IDLE_TIME ? 0 : cur_time - cached_last_sent_time);
2013 }
2014
2015 static DWORD CALLBACK rpcrt4_http_timer_thread(PVOID param)
2016 {
2017 HttpTimerThreadData *data_in = param;
2018 HttpTimerThreadData data;
2019 DWORD timeout;
2020
2021 data = *data_in;
2022 HeapFree(GetProcessHeap(), 0, data_in);
2023
2024 for (timeout = HTTP_IDLE_TIME;
2025 WaitForSingleObject(data.timer_cancelled, timeout) == WAIT_TIMEOUT;
2026 timeout = rpcrt4_http_timer_calc_timeout(data.last_sent_time))
2027 {
2028 /* are we too soon after last send? */
2029 if (GetTickCount() - *data.last_sent_time < HTTP_IDLE_TIME)
2030 continue;
2031 rpcrt4_http_keep_connection_active_timer_proc(data.timer_param, TRUE);
2032 }
2033
2034 CloseHandle(data.timer_cancelled);
2035 return 0;
2036 }
2037
2038 static VOID WINAPI rpcrt4_http_internet_callback(
2039 HINTERNET hInternet,
2040 DWORD_PTR dwContext,
2041 DWORD dwInternetStatus,
2042 LPVOID lpvStatusInformation,
2043 DWORD dwStatusInformationLength)
2044 {
2045 RpcHttpAsyncData *async_data = (RpcHttpAsyncData *)dwContext;
2046
2047 switch (dwInternetStatus)
2048 {
2049 case INTERNET_STATUS_REQUEST_COMPLETE:
2050 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2051 if (async_data)
2052 {
2053 INTERNET_ASYNC_RESULT *async_result = lpvStatusInformation;
2054
2055 async_data->async_result = async_result->dwResult ? ERROR_SUCCESS : async_result->dwError;
2056 SetEvent(async_data->completion_event);
2057 RpcHttpAsyncData_Release(async_data);
2058 }
2059 break;
2060 }
2061 }
2062
2063 static RPC_STATUS rpcrt4_http_check_response(HINTERNET hor)
2064 {
2065 BOOL ret;
2066 DWORD status_code;
2067 DWORD size;
2068 DWORD index;
2069 WCHAR buf[32];
2070 WCHAR *status_text = buf;
2071 TRACE("\n");
2072
2073 index = 0;
2074 size = sizeof(status_code);
2075 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status_code, &size, &index);
2076 if (!ret)
2077 return GetLastError();
2078 if (status_code == HTTP_STATUS_OK)
2079 return RPC_S_OK;
2080 index = 0;
2081 size = sizeof(buf);
2082 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
2083 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2084 {
2085 status_text = HeapAlloc(GetProcessHeap(), 0, size);
2086 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
2087 }
2088
2089 ERR("server returned: %d %s\n", status_code, ret ? debugstr_w(status_text) : "<status text unavailable>");
2090 if(status_text != buf) HeapFree(GetProcessHeap(), 0, status_text);
2091
2092 if (status_code == HTTP_STATUS_DENIED)
2093 return ERROR_ACCESS_DENIED;
2094 return RPC_S_SERVER_UNAVAILABLE;
2095 }
2096
2097 static RPC_STATUS rpcrt4_http_internet_connect(RpcConnection_http *httpc)
2098 {
2099 static const WCHAR wszUserAgent[] = {'M','S','R','P','C',0};
2100 LPWSTR proxy = NULL;
2101 LPWSTR user = NULL;
2102 LPWSTR password = NULL;
2103 LPWSTR servername = NULL;
2104 const WCHAR *option;
2105 INTERNET_PORT port;
2106
2107 if (httpc->common.QOS &&
2108 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP))
2109 {
2110 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_cred = httpc->common.QOS->qos->u.HttpCredentials;
2111 if (http_cred->TransportCredentials)
2112 {
2113 WCHAR *p;
2114 const SEC_WINNT_AUTH_IDENTITY_W *cred = http_cred->TransportCredentials;
2115 ULONG len = cred->DomainLength + 1 + cred->UserLength;
2116 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2117 if (!user)
2118 return RPC_S_OUT_OF_RESOURCES;
2119 p = user;
2120 if (cred->DomainLength)
2121 {
2122 memcpy(p, cred->Domain, cred->DomainLength * sizeof(WCHAR));
2123 p += cred->DomainLength;
2124 *p = '\\';
2125 p++;
2126 }
2127 memcpy(p, cred->User, cred->UserLength * sizeof(WCHAR));
2128 p[cred->UserLength] = 0;
2129
2130 password = RPCRT4_strndupW(cred->Password, cred->PasswordLength);
2131 }
2132 }
2133
2134 for (option = httpc->common.NetworkOptions; option;
2135 option = (strchrW(option, ',') ? strchrW(option, ',')+1 : NULL))
2136 {
2137 static const WCHAR wszRpcProxy[] = {'R','p','c','P','r','o','x','y','=',0};
2138 static const WCHAR wszHttpProxy[] = {'H','t','t','p','P','r','o','x','y','=',0};
2139
2140 if (!strncmpiW(option, wszRpcProxy, sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1))
2141 {
2142 const WCHAR *value_start = option + sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1;
2143 const WCHAR *value_end;
2144 const WCHAR *p;
2145
2146 value_end = strchrW(option, ',');
2147 if (!value_end)
2148 value_end = value_start + strlenW(value_start);
2149 for (p = value_start; p < value_end; p++)
2150 if (*p == ':')
2151 {
2152 port = atoiW(p+1);
2153 value_end = p;
2154 break;
2155 }
2156 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
2157 servername = RPCRT4_strndupW(value_start, value_end-value_start);
2158 }
2159 else if (!strncmpiW(option, wszHttpProxy, sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1))
2160 {
2161 const WCHAR *value_start = option + sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1;
2162 const WCHAR *value_end;
2163
2164 value_end = strchrW(option, ',');
2165 if (!value_end)
2166 value_end = value_start + strlenW(value_start);
2167 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
2168 proxy = RPCRT4_strndupW(value_start, value_end-value_start);
2169 }
2170 else
2171 FIXME("unhandled option %s\n", debugstr_w(option));
2172 }
2173
2174 httpc->app_info = InternetOpenW(wszUserAgent, proxy ? INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_PRECONFIG,
2175 NULL, NULL, INTERNET_FLAG_ASYNC);
2176 if (!httpc->app_info)
2177 {
2178 HeapFree(GetProcessHeap(), 0, password);
2179 HeapFree(GetProcessHeap(), 0, user);
2180 HeapFree(GetProcessHeap(), 0, proxy);
2181 HeapFree(GetProcessHeap(), 0, servername);
2182 ERR("InternetOpenW failed with error %d\n", GetLastError());
2183 return RPC_S_SERVER_UNAVAILABLE;
2184 }
2185 InternetSetStatusCallbackW(httpc->app_info, rpcrt4_http_internet_callback);
2186
2187 /* if no RpcProxy option specified, set the HTTP server address to the
2188 * RPC server address */
2189 if (!servername)
2190 {
2191 servername = HeapAlloc(GetProcessHeap(), 0, (strlen(httpc->common.NetworkAddr) + 1)*sizeof(WCHAR));
2192 if (!servername)
2193 {
2194 HeapFree(GetProcessHeap(), 0, password);
2195 HeapFree(GetProcessHeap(), 0, user);
2196 HeapFree(GetProcessHeap(), 0, proxy);
2197 return RPC_S_OUT_OF_RESOURCES;
2198 }
2199 MultiByteToWideChar(CP_ACP, 0, httpc->common.NetworkAddr, -1, servername, strlen(httpc->common.NetworkAddr) + 1);
2200 }
2201
2202 port = (httpc->common.QOS &&
2203 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
2204 (httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL)) ?
2205 INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
2206
2207 httpc->session = InternetConnectW(httpc->app_info, servername, port, user, password,
2208 INTERNET_SERVICE_HTTP, 0, 0);
2209
2210 HeapFree(GetProcessHeap(), 0, password);
2211 HeapFree(GetProcessHeap(), 0, user);
2212 HeapFree(GetProcessHeap(), 0, proxy);
2213
2214 if (!httpc->session)
2215 {
2216 ERR("InternetConnectW failed with error %d\n", GetLastError());
2217 HeapFree(GetProcessHeap(), 0, servername);
2218 return RPC_S_SERVER_UNAVAILABLE;
2219 }
2220 httpc->servername = servername;
2221 return RPC_S_OK;
2222 }
2223
2224 static RPC_STATUS send_echo_request(HINTERNET req, RpcHttpAsyncData *async_data, HANDLE cancel_event)
2225 {
2226 DWORD bytes_read;
2227 BYTE buf[20];
2228 BOOL ret;
2229 RPC_STATUS status;
2230
2231 TRACE("sending echo request to server\n");
2232
2233 prepare_async_request(async_data);
2234 ret = HttpSendRequestW(req, NULL, 0, NULL, 0);
2235 status = wait_async_request(async_data, ret, cancel_event);
2236 if (status != RPC_S_OK) return status;
2237
2238 status = rpcrt4_http_check_response(req);
2239 if (status != RPC_S_OK) return status;
2240
2241 InternetReadFile(req, buf, sizeof(buf), &bytes_read);
2242 /* FIXME: do something with retrieved data */
2243
2244 return RPC_S_OK;
2245 }
2246
2247 /* prepare the in pipe for use by RPC packets */
2248 static RPC_STATUS rpcrt4_http_prepare_in_pipe(HINTERNET in_request, RpcHttpAsyncData *async_data, HANDLE cancel_event,
2249 const UUID *connection_uuid, const UUID *in_pipe_uuid,
2250 const UUID *association_uuid, BOOL authorized)
2251 {
2252 BOOL ret;
2253 RPC_STATUS status;
2254 RpcPktHdr *hdr;
2255 INTERNET_BUFFERSW buffers_in;
2256 DWORD bytes_written;
2257
2258 if (!authorized)
2259 {
2260 /* ask wininet to authorize, if necessary */
2261 status = send_echo_request(in_request, async_data, cancel_event);
2262 if (status != RPC_S_OK) return status;
2263 }
2264 memset(&buffers_in, 0, sizeof(buffers_in));
2265 buffers_in.dwStructSize = sizeof(buffers_in);
2266 /* FIXME: get this from the registry */
2267 buffers_in.dwBufferTotal = 1024 * 1024 * 1024; /* 1Gb */
2268 prepare_async_request(async_data);
2269 ret = HttpSendRequestExW(in_request, &buffers_in, NULL, 0, 0);
2270 status = wait_async_request(async_data, ret, cancel_event);
2271 if (status != RPC_S_OK) return status;
2272
2273 TRACE("sending HTTP connect header to server\n");
2274 hdr = RPCRT4_BuildHttpConnectHeader(FALSE, connection_uuid, in_pipe_uuid, association_uuid);
2275 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
2276 ret = InternetWriteFile(in_request, hdr, hdr->common.frag_len, &bytes_written);
2277 RPCRT4_FreeHeader(hdr);
2278 if (!ret)
2279 {
2280 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2281 return RPC_S_SERVER_UNAVAILABLE;
2282 }
2283
2284 return RPC_S_OK;
2285 }
2286
2287 static RPC_STATUS rpcrt4_http_read_http_packet(HINTERNET request, RpcPktHdr *hdr, BYTE **data)
2288 {
2289 BOOL ret;
2290 DWORD bytes_read;
2291 unsigned short data_len;
2292
2293 ret = InternetReadFile(request, hdr, sizeof(hdr->common), &bytes_read);
2294 if (!ret)
2295 return RPC_S_SERVER_UNAVAILABLE;
2296 if (hdr->common.ptype != PKT_HTTP || hdr->common.frag_len < sizeof(hdr->http))
2297 {
2298 ERR("wrong packet type received %d or wrong frag_len %d\n",
2299 hdr->common.ptype, hdr->common.frag_len);
2300 return RPC_S_PROTOCOL_ERROR;
2301 }
2302
2303 ret = InternetReadFile(request, &hdr->common + 1, sizeof(hdr->http) - sizeof(hdr->common), &bytes_read);
2304 if (!ret)
2305 return RPC_S_SERVER_UNAVAILABLE;
2306
2307 data_len = hdr->common.frag_len - sizeof(hdr->http);
2308 if (data_len)
2309 {
2310 *data = HeapAlloc(GetProcessHeap(), 0, data_len);
2311 if (!*data)
2312 return RPC_S_OUT_OF_RESOURCES;
2313 ret = InternetReadFile(request, *data, data_len, &bytes_read);
2314 if (!ret)
2315 {
2316 HeapFree(GetProcessHeap(), 0, *data);
2317 return RPC_S_SERVER_UNAVAILABLE;
2318 }
2319 }
2320 else
2321 *data = NULL;
2322
2323 if (!RPCRT4_IsValidHttpPacket(hdr, *data, data_len))
2324 {
2325 ERR("invalid http packet\n");
2326 return RPC_S_PROTOCOL_ERROR;
2327 }
2328
2329 return RPC_S_OK;
2330 }
2331
2332 /* prepare the out pipe for use by RPC packets */
2333 static RPC_STATUS rpcrt4_http_prepare_out_pipe(HINTERNET out_request, RpcHttpAsyncData *async_data,
2334 HANDLE cancel_event, const UUID *connection_uuid,
2335 const UUID *out_pipe_uuid, ULONG *flow_control_increment,
2336 BOOL authorized)
2337 {
2338 BOOL ret;
2339 RPC_STATUS status;
2340 RpcPktHdr *hdr;
2341 BYTE *data_from_server;
2342 RpcPktHdr pkt_from_server;
2343 ULONG field1, field3;
2344 DWORD bytes_read;
2345 BYTE buf[20];
2346
2347 if (!authorized)
2348 {
2349 /* ask wininet to authorize, if necessary */
2350 status = send_echo_request(out_request, async_data, cancel_event);
2351 if (status != RPC_S_OK) return status;
2352 }
2353 else
2354 InternetReadFile(out_request, buf, sizeof(buf), &bytes_read);
2355
2356 hdr = RPCRT4_BuildHttpConnectHeader(TRUE, connection_uuid, out_pipe_uuid, NULL);
2357 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
2358
2359 TRACE("sending HTTP connect header to server\n");
2360 prepare_async_request(async_data);
2361 ret = HttpSendRequestW(out_request, NULL, 0, hdr, hdr->common.frag_len);
2362 status = wait_async_request(async_data, ret, cancel_event);
2363 RPCRT4_FreeHeader(hdr);
2364 if (status != RPC_S_OK) return status;
2365
2366 status = rpcrt4_http_check_response(out_request);
2367 if (status != RPC_S_OK) return status;
2368
2369 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
2370 &data_from_server);
2371 if (status != RPC_S_OK) return status;
2372 status = RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server, data_from_server,
2373 &field1);
2374 HeapFree(GetProcessHeap(), 0, data_from_server);
2375 if (status != RPC_S_OK) return status;
2376 TRACE("received (%d) from first prepare header\n", field1);
2377
2378 for (;;)
2379 {
2380 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
2381 &data_from_server);
2382 if (status != RPC_S_OK) return status;
2383 if (pkt_from_server.http.flags != 0x0001) break;
2384
2385 TRACE("http idle packet, waiting for real packet\n");
2386 HeapFree(GetProcessHeap(), 0, data_from_server);
2387 if (pkt_from_server.http.num_data_items != 0)
2388 {
2389 ERR("HTTP idle packet should have no data items instead of %d\n",
2390 pkt_from_server.http.num_data_items);
2391 return RPC_S_PROTOCOL_ERROR;
2392 }
2393 }
2394 status = RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server, data_from_server,
2395 &field1, flow_control_increment,
2396 &field3);
2397 HeapFree(GetProcessHeap(), 0, data_from_server);
2398 if (status != RPC_S_OK) return status;
2399 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1, *flow_control_increment, field3);
2400
2401 return RPC_S_OK;
2402 }
2403
2404 static UINT encode_base64(const char *bin, unsigned int len, WCHAR *base64)
2405 {
2406 static const char enc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2407 UINT i = 0, x;
2408
2409 while (len > 0)
2410 {
2411 /* first 6 bits, all from bin[0] */
2412 base64[i++] = enc[(bin[0] & 0xfc) >> 2];
2413 x = (bin[0] & 3) << 4;
2414
2415 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2416 if (len == 1)
2417 {
2418 base64[i++] = enc[x];
2419 base64[i++] = '=';
2420 base64[i++] = '=';
2421 break;
2422 }
2423 base64[i++] = enc[x | ((bin[1] & 0xf0) >> 4)];
2424 x = (bin[1] & 0x0f) << 2;
2425
2426 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2427 if (len == 2)
2428 {
2429 base64[i++] = enc[x];
2430 base64[i++] = '=';
2431 break;
2432 }
2433 base64[i++] = enc[x | ((bin[2] & 0xc0) >> 6)];
2434
2435 /* last 6 bits, all from bin [2] */
2436 base64[i++] = enc[bin[2] & 0x3f];
2437 bin += 3;
2438 len -= 3;
2439 }
2440 base64[i] = 0;
2441 return i;
2442 }
2443
2444 static inline char decode_char( WCHAR c )
2445 {
2446 if (c >= 'A' && c <= 'Z') return c - 'A';
2447 if (c >= 'a' && c <= 'z') return c - 'a' + 26;
2448 if (c >= '0' && c <= '9') return c - '0' + 52;
2449 if (c == '+') return 62;
2450 if (c == '/') return 63;
2451 return 64;
2452 }
2453
2454 static unsigned int decode_base64( const WCHAR *base64, unsigned int len, char *buf )
2455 {
2456 unsigned int i = 0;
2457 char c0, c1, c2, c3;
2458 const WCHAR *p = base64;
2459
2460 while (len > 4)
2461 {
2462 if ((c0 = decode_char( p[0] )) > 63) return 0;
2463 if ((c1 = decode_char( p[1] )) > 63) return 0;
2464 if ((c2 = decode_char( p[2] )) > 63) return 0;
2465 if ((c3 = decode_char( p[3] )) > 63) return 0;
2466
2467 if (buf)
2468 {
2469 buf[i + 0] = (c0 << 2) | (c1 >> 4);
2470 buf[i + 1] = (c1 << 4) | (c2 >> 2);
2471 buf[i + 2] = (c2 << 6) | c3;
2472 }
2473 len -= 4;
2474 i += 3;
2475 p += 4;
2476 }
2477 if (p[2] == '=')
2478 {
2479 if ((c0 = decode_char( p[0] )) > 63) return 0;
2480 if ((c1 = decode_char( p[1] )) > 63) return 0;
2481
2482 if (buf) buf[i] = (c0 << 2) | (c1 >> 4);
2483 i++;
2484 }
2485 else if (p[3] == '=')
2486 {
2487 if ((c0 = decode_char( p[0] )) > 63) return 0;
2488 if ((c1 = decode_char( p[1] )) > 63) return 0;
2489 if ((c2 = decode_char( p[2] )) > 63) return 0;
2490
2491 if (buf)
2492 {
2493 buf[i + 0] = (c0 << 2) | (c1 >> 4);
2494 buf[i + 1] = (c1 << 4) | (c2 >> 2);
2495 }
2496 i += 2;
2497 }
2498 else
2499 {
2500 if ((c0 = decode_char( p[0] )) > 63) return 0;
2501 if ((c1 = decode_char( p[1] )) > 63) return 0;
2502 if ((c2 = decode_char( p[2] )) > 63) return 0;
2503 if ((c3 = decode_char( p[3] )) > 63) return 0;
2504
2505 if (buf)
2506 {
2507 buf[i + 0] = (c0 << 2) | (c1 >> 4);
2508 buf[i + 1] = (c1 << 4) | (c2 >> 2);
2509 buf[i + 2] = (c2 << 6) | c3;
2510 }
2511 i += 3;
2512 }
2513 return i;
2514 }
2515
2516 static struct authinfo *alloc_authinfo(void)
2517 {
2518 struct authinfo *ret;
2519
2520 if (!(ret = HeapAlloc(GetProcessHeap(), 0, sizeof(*ret) ))) return NULL;
2521
2522 SecInvalidateHandle(&ret->cred);
2523 SecInvalidateHandle(&ret->ctx);
2524 memset(&ret->exp, 0, sizeof(ret->exp));
2525 ret->scheme = 0;
2526 ret->attr = 0;
2527 ret->max_token = 0;
2528 ret->data = NULL;
2529 ret->data_len = 0;
2530 ret->finished = FALSE;
2531 return ret;
2532 }
2533
2534 static void destroy_authinfo(struct authinfo *info)
2535 {
2536 if (!info) return;
2537
2538 if (SecIsValidHandle(&info->ctx))
2539 DeleteSecurityContext(&info->ctx);
2540 if (SecIsValidHandle(&info->cred))
2541 FreeCredentialsHandle(&info->cred);
2542
2543 HeapFree(GetProcessHeap(), 0, info->data);
2544 HeapFree(GetProcessHeap(), 0, info);
2545 }
2546
2547 static const WCHAR basicW[] = {'B','a','s','i','c',0};
2548 static const WCHAR ntlmW[] = {'N','T','L','M',0};
2549 static const WCHAR passportW[] = {'P','a','s','s','p','o','r','t',0};
2550 static const WCHAR digestW[] = {'D','i','g','e','s','t',0};
2551 static const WCHAR negotiateW[] = {'N','e','g','o','t','i','a','t','e',0};
2552
2553 static const struct
2554 {
2555 const WCHAR *str;
2556 unsigned int len;
2557 DWORD scheme;
2558 }
2559 auth_schemes[] =
2560 {
2561 { basicW, ARRAYSIZE(basicW) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC },
2562 { ntlmW, ARRAYSIZE(ntlmW) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM },
2563 { passportW, ARRAYSIZE(passportW) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT },
2564 { digestW, ARRAYSIZE(digestW) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST },
2565 { negotiateW, ARRAYSIZE(negotiateW) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE }
2566 };
2567 static const unsigned int num_auth_schemes = sizeof(auth_schemes)/sizeof(auth_schemes[0]);
2568
2569 static DWORD auth_scheme_from_header( const WCHAR *header )
2570 {
2571 unsigned int i;
2572 for (i = 0; i < num_auth_schemes; i++)
2573 {
2574 if (!strncmpiW( header, auth_schemes[i].str, auth_schemes[i].len ) &&
2575 (header[auth_schemes[i].len] == ' ' || !header[auth_schemes[i].len])) return auth_schemes[i].scheme;
2576 }
2577 return 0;
2578 }
2579
2580 static BOOL get_authvalue(HINTERNET request, DWORD scheme, WCHAR *buffer, DWORD buflen)
2581 {
2582 DWORD len, index = 0;
2583 for (;;)
2584 {
2585 len = buflen;
2586 if (!HttpQueryInfoW(request, HTTP_QUERY_WWW_AUTHENTICATE, buffer, &len, &index)) return FALSE;
2587 if (auth_scheme_from_header(buffer) == scheme) break;
2588 }
2589 return TRUE;
2590 }
2591
2592 static RPC_STATUS do_authorization(HINTERNET request, SEC_WCHAR *servername,
2593 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *creds, struct authinfo **auth_ptr)
2594 {
2595 struct authinfo *info = *auth_ptr;
2596 SEC_WINNT_AUTH_IDENTITY_W *id = creds->TransportCredentials;
2597 RPC_STATUS status = RPC_S_SERVER_UNAVAILABLE;
2598
2599 if ((!info && !(info = alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE;
2600
2601 switch (creds->AuthnSchemes[0])
2602 {
2603 case RPC_C_HTTP_AUTHN_SCHEME_BASIC:
2604 {
2605 int userlen = WideCharToMultiByte(CP_UTF8, 0, id->User, id->UserLength, NULL, 0, NULL, NULL);
2606 int passlen = WideCharToMultiByte(CP_UTF8, 0, id->Password, id->PasswordLength, NULL, 0, NULL, NULL);
2607
2608 info->data_len = userlen + passlen + 1;
2609 if (!(info->data = HeapAlloc(GetProcessHeap(), 0, info->data_len)))
2610 {
2611 status = RPC_S_OUT_OF_MEMORY;
2612 break;
2613 }
2614 WideCharToMultiByte(CP_UTF8, 0, id->User, id->UserLength, info->data, userlen, NULL, NULL);
2615 info->data[userlen] = ':';
2616 WideCharToMultiByte(CP_UTF8, 0, id->Password, id->PasswordLength, info->data + userlen + 1, passlen, NULL, NULL);
2617
2618 info->scheme = RPC_C_HTTP_AUTHN_SCHEME_BASIC;
2619 info->finished = TRUE;
2620 status = RPC_S_OK;
2621 break;
2622 }
2623 case RPC_C_HTTP_AUTHN_SCHEME_NTLM:
2624 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE:
2625 {
2626
2627 static SEC_WCHAR ntlmW[] = {'N','T','L','M',0}, negotiateW[] = {'N','e','g','o','t','i','a','t','e',0};
2628 SECURITY_STATUS ret;
2629 SecBufferDesc out_desc, in_desc;
2630 SecBuffer out, in;
2631 ULONG flags = ISC_REQ_CONNECTION|ISC_REQ_USE_DCE_STYLE|ISC_REQ_MUTUAL_AUTH|ISC_REQ_DELEGATE;
2632 SEC_WCHAR *scheme;
2633 int scheme_len;
2634 const WCHAR *p;
2635 WCHAR auth_value[2048];
2636 DWORD size = sizeof(auth_value);
2637 BOOL first = FALSE;
2638
2639 if (creds->AuthnSchemes[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM) scheme = ntlmW;
2640 else scheme = negotiateW;
2641 scheme_len = strlenW( scheme );
2642
2643 if (!*auth_ptr)
2644 {
2645 TimeStamp exp;
2646 SecPkgInfoW *pkg_info;
2647
2648 ret = AcquireCredentialsHandleW(NULL, scheme, SECPKG_CRED_OUTBOUND, NULL, id, NULL, NULL, &info->cred, &exp);
2649 if (ret != SEC_E_OK) break;
2650
2651 ret = QuerySecurityPackageInfoW(scheme, &pkg_info);
2652 if (ret != SEC_E_OK) break;
2653
2654 info->max_token = pkg_info->cbMaxToken;
2655 FreeContextBuffer(pkg_info);
2656 first = TRUE;
2657 }
2658 else
2659 {
2660 if (info->finished || !get_authvalue(request, creds->AuthnSchemes[0], auth_value, size)) break;
2661 if (auth_scheme_from_header(auth_value) != info->scheme)
2662 {
2663 ERR("authentication scheme changed\n");
2664 break;
2665 }
2666 }
2667 in.BufferType = SECBUFFER_TOKEN;
2668 in.cbBuffer = 0;
2669 in.pvBuffer = NULL;
2670
2671 in_desc.ulVersion = 0;
2672 in_desc.cBuffers = 1;
2673 in_desc.pBuffers = &in;
2674
2675 p = auth_value + scheme_len;
2676 if (!first && *p == ' ')
2677 {
2678 int len = strlenW(++p);
2679 in.cbBuffer = decode_base64(p, len, NULL);
2680 if (!(in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer))) break;
2681 decode_base64(p, len, in.pvBuffer);
2682 }
2683 out.BufferType = SECBUFFER_TOKEN;
2684 out.cbBuffer = info->max_token;
2685 if (!(out.pvBuffer = HeapAlloc(GetProcessHeap(), 0, out.cbBuffer)))
2686 {
2687 HeapFree(GetProcessHeap(), 0, in.pvBuffer);
2688 break;
2689 }
2690 out_desc.ulVersion = 0;
2691 out_desc.cBuffers = 1;
2692 out_desc.pBuffers = &out;
2693
2694 ret = InitializeSecurityContextW(first ? &info->cred : NULL, first ? NULL : &info->ctx,
2695 first ? servername : NULL, flags, 0, SECURITY_NETWORK_DREP,
2696 in.pvBuffer ? &in_desc : NULL, 0, &info->ctx, &out_desc,
2697 &info->attr, &info->exp);
2698 HeapFree(GetProcessHeap(), 0, in.pvBuffer);
2699 if (ret == SEC_E_OK)
2700 {
2701 HeapFree(GetProcessHeap(), 0, info->data);
2702 info->data = out.pvBuffer;
2703 info->data_len = out.cbBuffer;
2704 info->finished = TRUE;
2705 TRACE("sending last auth packet\n");
2706 status = RPC_S_OK;
2707 }
2708 else if (ret == SEC_I_CONTINUE_NEEDED)
2709 {
2710 HeapFree(GetProcessHeap(), 0, info->data);
2711 info->data = out.pvBuffer;
2712 info->data_len = out.cbBuffer;
2713 TRACE("sending next auth packet\n");
2714 status = RPC_S_OK;
2715 }
2716 else
2717 {
2718 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret);
2719 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
2720 break;
2721 }
2722 info->scheme = creds->AuthnSchemes[0];
2723 break;
2724 }
2725 default:
2726 FIXME("scheme %u not supported\n", creds->AuthnSchemes[0]);
2727 break;
2728 }
2729
2730 if (status != RPC_S_OK)
2731 {
2732 destroy_authinfo(info);
2733 *auth_ptr = NULL;
2734 return status;
2735 }
2736 *auth_ptr = info;
2737 return RPC_S_OK;
2738 }
2739
2740 static RPC_STATUS insert_authorization_header(HINTERNET request, ULONG scheme, char *data, int data_len)
2741 {
2742 static const WCHAR authW[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2743 static const WCHAR basicW[] = {'B','a','s','i','c',' '};
2744 static const WCHAR negotiateW[] = {'N','e','g','o','t','i','a','t','e',' '};
2745 static const WCHAR ntlmW[] = {'N','T','L','M',' '};
2746 int scheme_len, auth_len = sizeof(authW) / sizeof(authW[0]), len = ((data_len + 2) * 4) / 3;
2747 const WCHAR *scheme_str;
2748 WCHAR *header, *ptr;
2749 RPC_STATUS status = RPC_S_SERVER_UNAVAILABLE;
2750
2751 switch (scheme)
2752 {
2753 case RPC_C_HTTP_AUTHN_SCHEME_BASIC:
2754 scheme_str = basicW;
2755 scheme_len = sizeof(basicW) / sizeof(basicW[0]);
2756 break;
2757 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE:
2758 scheme_str = negotiateW;
2759 scheme_len = sizeof(negotiateW) / sizeof(negotiateW[0]);
2760 break;
2761 case RPC_C_HTTP_AUTHN_SCHEME_NTLM:
2762 scheme_str = ntlmW;
2763 scheme_len = sizeof(ntlmW) / sizeof(ntlmW[0]);
2764 break;
2765 default:
2766 ERR("unknown scheme %u\n", scheme);
2767 return RPC_S_SERVER_UNAVAILABLE;
2768 }
2769 if ((header = HeapAlloc(GetProcessHeap(), 0, (auth_len + scheme_len + len + 2) * sizeof(WCHAR))))
2770 {
2771 memcpy(header, authW, auth_len * sizeof(WCHAR));
2772 ptr = header + auth_len;
2773 memcpy(ptr, scheme_str, scheme_len * sizeof(WCHAR));
2774 ptr += scheme_len;
2775 len = encode_base64(data, data_len, ptr);
2776 ptr[len++] = '\r';
2777 ptr[len++] = '\n';
2778 ptr[len] = 0;
2779 if (HttpAddRequestHeadersW(request, header, -1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE))
2780 status = RPC_S_OK;
2781 HeapFree(GetProcessHeap(), 0, header);
2782 }
2783 return status;
2784 }
2785
2786 static void drain_content(HINTERNET request)
2787 {
2788 DWORD count, len = 0, size = sizeof(len);
2789 char buf[2048];
2790
2791 HttpQueryInfoW(request, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, &len, &size, NULL);
2792 if (!len) return;
2793 for (;;)
2794 {
2795 count = min(sizeof(buf), len);
2796 if (!InternetReadFile(request, buf, count, &count) || !count) return;
2797 len -= count;
2798 }
2799 }
2800
2801 static RPC_STATUS authorize_request(RpcConnection_http *httpc, HINTERNET request)
2802 {
2803 static const WCHAR authW[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2804 struct authinfo *info = NULL;
2805 RPC_STATUS status;
2806 BOOL ret;
2807
2808 for (;;)
2809 {
2810 status = do_authorization(request, httpc->servername, httpc->common.QOS->qos->u.HttpCredentials, &info);
2811 if (status != RPC_S_OK) break;
2812
2813 status = insert_authorization_header(request, info->scheme, info->data, info->data_len);
2814 if (status != RPC_S_OK) break;
2815
2816 prepare_async_request(httpc->async_data);
2817 ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
2818 status = wait_async_request(httpc->async_data, ret, httpc->cancel_event);
2819 if (status != RPC_S_OK || info->finished) break;
2820
2821 status = rpcrt4_http_check_response(request);
2822 if (status != RPC_S_OK && status != ERROR_ACCESS_DENIED) break;
2823 drain_content(request);
2824 }
2825
2826 if (info->scheme != RPC_C_HTTP_AUTHN_SCHEME_BASIC)
2827 HttpAddRequestHeadersW(request, authW, -1, HTTP_ADDREQ_FLAG_REPLACE);
2828
2829 destroy_authinfo(info);
2830 return status;
2831 }
2832
2833 static RPC_STATUS insert_cookie_header(HINTERNET request, const WCHAR *value)
2834 {
2835 static const WCHAR cookieW[] = {'C','o','o','k','i','e',':',' '};
2836 WCHAR *header, *ptr;
2837 int len;
2838 RPC_STATUS status = RPC_S_SERVER_UNAVAILABLE;
2839
2840 if (!value) return RPC_S_OK;
2841
2842 len = strlenW(value);
2843 if ((header = HeapAlloc(GetProcessHeap(), 0, sizeof(cookieW) + (len + 3) * sizeof(WCHAR))))
2844 {
2845 memcpy(header, cookieW, sizeof(cookieW));
2846 ptr = header + sizeof(cookieW) / sizeof(cookieW[0]);
2847 memcpy(ptr, value, len * sizeof(WCHAR));
2848 ptr[len++] = '\r';
2849 ptr[len++] = '\n';
2850 ptr[len] = 0;
2851 if ((HttpAddRequestHeadersW(request, header, -1, HTTP_ADDREQ_FLAG_ADD_IF_NEW))) status = RPC_S_OK;
2852 HeapFree(GetProcessHeap(), 0, header);
2853 }
2854 return status;
2855 }
2856
2857 static BOOL has_credentials(RpcConnection_http *httpc)
2858 {
2859 RPC_HTTP_TRANSPORT_CREDENTIALS_W *creds;
2860 SEC_WINNT_AUTH_IDENTITY_W *id;
2861
2862 if (!httpc->common.QOS || httpc->common.QOS->qos->AdditionalSecurityInfoType != RPC_C_AUTHN_INFO_TYPE_HTTP)
2863 return FALSE;
2864
2865 creds = httpc->common.QOS->qos->u.HttpCredentials;
2866 if (creds->AuthenticationTarget != RPC_C_HTTP_AUTHN_TARGET_SERVER || !creds->NumberOfAuthnSchemes)
2867 return FALSE;
2868
2869 id = creds->TransportCredentials;
2870 if (!id || !id->User || !id->Password) return FALSE;
2871
2872 return TRUE;
2873 }
2874
2875 static BOOL is_secure(RpcConnection_http *httpc)
2876 {
2877 return httpc->common.QOS &&
2878 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
2879 (httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL);
2880 }
2881
2882 static RPC_STATUS rpcrt4_ncacn_http_open(RpcConnection* Connection)
2883 {
2884 RpcConnection_http *httpc = (RpcConnection_http *)Connection;
2885 static const WCHAR wszVerbIn[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2886 static const WCHAR wszVerbOut[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2887 static const WCHAR wszRpcProxyPrefix[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2888 static const WCHAR wszColon[] = {':',0};
2889 static const WCHAR wszAcceptType[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2890 LPCWSTR wszAcceptTypes[] = { wszAcceptType, NULL };
2891 DWORD flags;
2892 WCHAR *url;
2893 RPC_STATUS status;
2894 BOOL secure, credentials;
2895 HttpTimerThreadData *timer_data;
2896 HANDLE thread;
2897
2898 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
2899
2900 if (Connection->server)
2901 {
2902 ERR("ncacn_http servers not supported yet\n");
2903 return RPC_S_SERVER_UNAVAILABLE;
2904 }
2905
2906 if (httpc->in_request)
2907 return RPC_S_OK;
2908
2909 httpc->async_data->completion_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2910
2911 status = UuidCreate(&httpc->connection_uuid);
2912 status = UuidCreate(&httpc->in_pipe_uuid);
2913 status = UuidCreate(&httpc->out_pipe_uuid);
2914
2915 status = rpcrt4_http_internet_connect(httpc);
2916 if (status != RPC_S_OK)
2917 return status;
2918
2919 url = HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix) + (strlen(Connection->NetworkAddr) + 1 + strlen(Connection->Endpoint))*sizeof(WCHAR));
2920 if (!url)
2921 return RPC_S_OUT_OF_MEMORY;
2922 memcpy(url, wszRpcProxyPrefix, sizeof(wszRpcProxyPrefix));
2923 MultiByteToWideChar(CP_ACP, 0, Connection->NetworkAddr, -1, url+sizeof(wszRpcProxyPrefix)/sizeof(wszRpcProxyPrefix[0])-1, strlen(Connection->NetworkAddr)+1);
2924 strcatW(url, wszColon);
2925 MultiByteToWideChar(CP_ACP, 0, Connection->Endpoint, -1, url+strlenW(url), strlen(Connection->Endpoint)+1);
2926
2927 secure = is_secure(httpc);
2928 credentials = has_credentials(httpc);
2929
2930 flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE |
2931 INTERNET_FLAG_NO_AUTO_REDIRECT;
2932 if (secure) flags |= INTERNET_FLAG_SECURE;
2933 if (credentials) flags |= INTERNET_FLAG_NO_AUTH;
2934
2935 httpc->in_request = HttpOpenRequestW(httpc->session, wszVerbIn, url, NULL, NULL, wszAcceptTypes,
2936 flags, (DWORD_PTR)httpc->async_data);
2937 if (!httpc->in_request)
2938 {
2939 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2940 HeapFree(GetProcessHeap(), 0, url);
2941 return RPC_S_SERVER_UNAVAILABLE;
2942 }
2943 status = insert_cookie_header(httpc->in_request, Connection->CookieAuth);
2944 if (status != RPC_S_OK)
2945 {
2946 HeapFree(GetProcessHeap(), 0, url);
2947 return status;
2948 }
2949 if (credentials)
2950 {
2951 status = authorize_request(httpc, httpc->in_request);
2952 if (status != RPC_S_OK)
2953 {
2954 HeapFree(GetProcessHeap(), 0, url);
2955 return status;
2956 }
2957 status = rpcrt4_http_check_response(httpc->in_request);
2958 if (status != RPC_S_OK)
2959 {
2960 HeapFree(GetProcessHeap(), 0, url);
2961 return status;
2962 }
2963 drain_content(httpc->in_request);
2964 }
2965
2966 httpc->out_request = HttpOpenRequestW(httpc->session, wszVerbOut, url, NULL, NULL, wszAcceptTypes,
2967 flags, (DWORD_PTR)httpc->async_data);
2968 HeapFree(GetProcessHeap(), 0, url);
2969 if (!httpc->out_request)
2970 {
2971 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2972 return RPC_S_SERVER_UNAVAILABLE;
2973 }
2974 status = insert_cookie_header(httpc->out_request, Connection->CookieAuth);
2975 if (status != RPC_S_OK)
2976 return status;
2977
2978 if (credentials)
2979 {
2980 status = authorize_request(httpc, httpc->out_request);
2981 if (status != RPC_S_OK)
2982 return status;
2983 }
2984
2985 status = rpcrt4_http_prepare_in_pipe(httpc->in_request, httpc->async_data, httpc->cancel_event,
2986 &httpc->connection_uuid, &httpc->in_pipe_uuid,
2987 &Connection->assoc->http_uuid, credentials);
2988 if (status != RPC_S_OK)
2989 return status;
2990
2991 status = rpcrt4_http_prepare_out_pipe(httpc->out_request, httpc->async_data, httpc->cancel_event,
2992 &httpc->connection_uuid, &httpc->out_pipe_uuid,
2993 &httpc->flow_control_increment, credentials);
2994 if (status != RPC_S_OK)
2995 return status;
2996
2997 httpc->flow_control_mark = httpc->flow_control_increment / 2;
2998 httpc->last_sent_time = GetTickCount();
2999 httpc->timer_cancelled = CreateEventW(NULL, FALSE, FALSE, NULL);
3000
3001 timer_data = HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data));
3002 if (!timer_data)
3003 return ERROR_OUTOFMEMORY;
3004 timer_data->timer_param = httpc->in_request;
3005 timer_data->last_sent_time = &httpc->last_sent_time;
3006 timer_data->timer_cancelled = httpc->timer_cancelled;
3007 /* FIXME: should use CreateTimerQueueTimer when implemented */
3008 thread = CreateThread(NULL, 0, rpcrt4_http_timer_thread, timer_data, 0, NULL);
3009 if (!thread)
3010 {
3011 HeapFree(GetProcessHeap(), 0, timer_data);
3012 return GetLastError();
3013 }
3014 CloseHandle(thread);
3015
3016 return RPC_S_OK;
3017 }
3018
3019 static RPC_STATUS rpcrt4_ncacn_http_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
3020 {
3021 assert(0);
3022 return RPC_S_SERVER_UNAVAILABLE;
3023 }
3024
3025 static int rpcrt4_ncacn_http_read(RpcConnection *Connection,
3026 void *buffer, unsigned int count)
3027 {
3028 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
3029 char *buf = buffer;
3030 BOOL ret;
3031 unsigned int bytes_left = count;
3032 RPC_STATUS status = RPC_S_OK;
3033
3034 httpc->async_data->inet_buffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, count);
3035
3036 while (bytes_left)
3037 {
3038 httpc->async_data->inet_buffers.dwBufferLength = bytes_left;
3039 prepare_async_request(httpc->async_data);
3040 ret = InternetReadFileExA(httpc->out_request, &httpc->async_data->inet_buffers, IRF_ASYNC, 0);
3041 status = wait_async_request(httpc->async_data, ret, httpc->cancel_event);
3042 if(status != RPC_S_OK) {
3043 if(status == RPC_S_CALL_CANCELLED)
3044 TRACE("call cancelled\n");
3045 break;
3046 }
3047
3048 if(!httpc->async_data->inet_buffers.dwBufferLength)
3049 break;
3050 memcpy(buf, httpc->async_data->inet_buffers.lpvBuffer,
3051 httpc->async_data->inet_buffers.dwBufferLength);
3052
3053 bytes_left -= httpc->async_data->inet_buffers.dwBufferLength;
3054 buf += httpc->async_data->inet_buffers.dwBufferLength;
3055 }
3056
3057 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
3058 httpc->async_data->inet_buffers.lpvBuffer = NULL;
3059
3060 TRACE("%p %p %u -> %u\n", httpc->out_request, buffer, count, status);
3061 return status == RPC_S_OK ? count : -1;
3062 }
3063
3064 static RPC_STATUS rpcrt4_ncacn_http_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
3065 {
3066 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
3067 RPC_STATUS status;
3068 DWORD hdr_length;
3069 LONG dwRead;
3070 RpcPktCommonHdr common_hdr;
3071
3072 *Header = NULL;
3073
3074 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
3075
3076 again:
3077 /* read packet common header */
3078 dwRead = rpcrt4_ncacn_http_read(Connection, &common_hdr, sizeof(common_hdr));
3079 if (dwRead != sizeof(common_hdr)) {
3080 WARN("Short read of header, %d bytes\n", dwRead);
3081 status = RPC_S_PROTOCOL_ERROR;
3082 goto fail;
3083 }
3084 if (!memcmp(&common_hdr, "HTTP/1.1", sizeof("HTTP/1.1")) ||
3085 !memcmp(&common_hdr, "HTTP/1.0", sizeof("HTTP/1.0")))
3086 {
3087 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr));
3088 status = RPC_S_PROTOCOL_ERROR;
3089 goto fail;
3090 }
3091
3092 status = RPCRT4_ValidateCommonHeader(&common_hdr);
3093 if (status != RPC_S_OK) goto fail;
3094
3095 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
3096 if (hdr_length == 0) {
3097 WARN("header length == 0\n");
3098 status = RPC_S_PROTOCOL_ERROR;
3099 goto fail;
3100 }
3101
3102 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
3103 if (!*Header)
3104 {
3105 status = RPC_S_OUT_OF_RESOURCES;
3106 goto fail;
3107 }
3108 memcpy(*Header, &common_hdr, sizeof(common_hdr));
3109
3110 /* read the rest of packet header */
3111 dwRead = rpcrt4_ncacn_http_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
3112 if (dwRead != hdr_length - sizeof(common_hdr)) {
3113 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
3114 status = RPC_S_PROTOCOL_ERROR;
3115 goto fail;
3116 }
3117
3118 if (common_hdr.frag_len - hdr_length)
3119 {
3120 *Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
3121 if (!*Payload)
3122 {
3123 status = RPC_S_OUT_OF_RESOURCES;
3124 goto fail;
3125 }
3126
3127 dwRead = rpcrt4_ncacn_http_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
3128 if (dwRead != common_hdr.frag_len - hdr_length)
3129 {
3130 WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
3131 status = RPC_S_PROTOCOL_ERROR;
3132 goto fail;
3133 }
3134 }
3135 else
3136 *Payload = NULL;
3137
3138 if ((*Header)->common.ptype == PKT_HTTP)
3139 {
3140 if (!RPCRT4_IsValidHttpPacket(*Header, *Payload, common_hdr.frag_len - hdr_length))
3141 {
3142 ERR("invalid http packet of length %d bytes\n", (*Header)->common.frag_len);
3143 status = RPC_S_PROTOCOL_ERROR;
3144 goto fail;
3145 }
3146 if ((*Header)->http.flags == 0x0001)
3147 {
3148 TRACE("http idle packet, waiting for real packet\n");
3149 if ((*Header)->http.num_data_items != 0)
3150 {
3151 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header)->http.num_data_items);
3152 status = RPC_S_PROTOCOL_ERROR;
3153 goto fail;
3154 }
3155 }
3156 else if ((*Header)->http.flags == 0x0002)
3157 {
3158 ULONG bytes_transmitted;
3159 ULONG flow_control_increment;
3160 UUID pipe_uuid;
3161 status = RPCRT4_ParseHttpFlowControlHeader(*Header, *Payload,
3162 Connection->server,
3163 &bytes_transmitted,
3164 &flow_control_increment,
3165 &pipe_uuid);
3166 if (status != RPC_S_OK)
3167 goto fail;
3168 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
3169 bytes_transmitted, flow_control_increment, debugstr_guid(&pipe_uuid));
3170 /* FIXME: do something with parsed data */
3171 }
3172 else
3173 {
3174 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header)->http.flags);
3175 status = RPC_S_PROTOCOL_ERROR;
3176 goto fail;
3177 }
3178 RPCRT4_FreeHeader(*Header);
3179 *Header = NULL;
3180 HeapFree(GetProcessHeap(), 0, *Payload);
3181 *Payload = NULL;
3182 goto again;
3183 }
3184
3185 /* success */
3186 status = RPC_S_OK;
3187
3188 httpc->bytes_received += common_hdr.frag_len;
3189
3190 TRACE("httpc->bytes_received = 0x%x\n", httpc->bytes_received);
3191
3192 if (httpc->bytes_received > httpc->flow_control_mark)
3193 {
3194 RpcPktHdr *hdr = RPCRT4_BuildHttpFlowControlHeader(httpc->common.server,
3195 httpc->bytes_received,
3196 httpc->flow_control_increment,
3197 &httpc->out_pipe_uuid);
3198 if (hdr)
3199 {
3200 DWORD bytes_written;
3201 BOOL ret2;
3202 TRACE("sending flow control packet at 0x%x\n", httpc->bytes_received);
3203 ret2 = InternetWriteFile(httpc->in_request, hdr, hdr->common.frag_len, &bytes_written);
3204 RPCRT4_FreeHeader(hdr);
3205 if (ret2)
3206 httpc->flow_control_mark = httpc->bytes_received + httpc->flow_control_increment / 2;
3207 }
3208 }
3209
3210 fail:
3211 if (status != RPC_S_OK) {
3212 RPCRT4_FreeHeader(*Header);
3213 *Header = NULL;
3214 HeapFree(GetProcessHeap(), 0, *Payload);
3215 *Payload = NULL;
3216 }
3217 return status;
3218 }
3219
3220 static int rpcrt4_ncacn_http_write(RpcConnection *Connection,
3221 const void *buffer, unsigned int count)
3222 {
3223 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
3224 DWORD bytes_written;
3225 BOOL ret;
3226
3227 httpc->last_sent_time = ~0U; /* disable idle packet sending */
3228 ret = InternetWriteFile(httpc->in_request, buffer, count, &bytes_written);
3229 httpc->last_sent_time = GetTickCount();
3230 TRACE("%p %p %u -> %s\n", httpc->in_request, buffer, count, ret ? "TRUE" : "FALSE");
3231 return ret ? bytes_written : -1;
3232 }
3233
3234 static int rpcrt4_ncacn_http_close(RpcConnection *Connection)
3235 {
3236 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
3237
3238 TRACE("\n");
3239
3240 SetEvent(httpc->timer_cancelled);
3241 if (httpc->in_request)
3242 InternetCloseHandle(httpc->in_request);
3243 httpc->in_request = NULL;
3244 if (httpc->out_request)
3245 InternetCloseHandle(httpc->out_request);
3246 httpc->out_request = NULL;
3247 if (httpc->app_info)
3248 InternetCloseHandle(httpc->app_info);
3249 httpc->app_info = NULL;
3250 if (httpc->session)
3251 InternetCloseHandle(httpc->session);
3252 httpc->session = NULL;
3253 RpcHttpAsyncData_Release(httpc->async_data);
3254 if (httpc->cancel_event)
3255 CloseHandle(httpc->cancel_event);
3256 HeapFree(GetProcessHeap(), 0, httpc->servername);
3257 httpc->servername = NULL;
3258
3259 return 0;
3260 }
3261
3262 static void rpcrt4_ncacn_http_cancel_call(RpcConnection *Connection)
3263 {
3264 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
3265
3266 SetEvent(httpc->cancel_event);
3267 }
3268
3269 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection *Connection)
3270 {
3271 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
3272 BOOL ret;
3273 RPC_STATUS status;
3274
3275 prepare_async_request(httpc->async_data);
3276 ret = InternetQueryDataAvailable(httpc->out_request,
3277 &httpc->async_data->inet_buffers.dwBufferLength, IRF_ASYNC, 0);
3278 status = wait_async_request(httpc->async_data, ret, httpc->cancel_event);
3279 return status == RPC_S_OK ? 0 : -1;
3280 }
3281
3282 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data,
3283 const char *networkaddr,
3284 const char *endpoint)
3285 {
3286 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
3287 EPM_PROTOCOL_HTTP, endpoint);
3288 }
3289
3290 static RPC_STATUS rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data,
3291 size_t tower_size,
3292 char **networkaddr,
3293 char **endpoint)
3294 {
3295 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
3296 networkaddr, EPM_PROTOCOL_HTTP,
3297 endpoint);
3298 }
3299
3300 static const struct connection_ops conn_protseq_list[] = {
3301 { "ncacn_np",
3302 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
3303 rpcrt4_conn_np_alloc,
3304 rpcrt4_ncacn_np_open,
3305 rpcrt4_ncacn_np_handoff,
3306 rpcrt4_conn_np_read,
3307 rpcrt4_conn_np_write,
3308 rpcrt4_conn_np_close,
3309 rpcrt4_conn_np_cancel_call,
3310 rpcrt4_conn_np_wait_for_incoming_data,
3311 rpcrt4_ncacn_np_get_top_of_tower,
3312 rpcrt4_ncacn_np_parse_top_of_tower,
3313 NULL,
3314 RPCRT4_default_is_authorized,
3315 RPCRT4_default_authorize,
3316 RPCRT4_default_secure_packet,
3317 rpcrt4_conn_np_impersonate_client,
3318 rpcrt4_conn_np_revert_to_self,
3319 RPCRT4_default_inquire_auth_client,
3320 },
3321 { "ncalrpc",
3322 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
3323 rpcrt4_conn_np_alloc,
3324 rpcrt4_ncalrpc_open,
3325 rpcrt4_ncalrpc_handoff,
3326 rpcrt4_conn_np_read,
3327 rpcrt4_conn_np_write,
3328 rpcrt4_conn_np_close,
3329 rpcrt4_conn_np_cancel_call,
3330 rpcrt4_conn_np_wait_for_incoming_data,
3331 rpcrt4_ncalrpc_get_top_of_tower,
3332 rpcrt4_ncalrpc_parse_top_of_tower,
3333 NULL,
3334 rpcrt4_ncalrpc_is_authorized,
3335 rpcrt4_ncalrpc_authorize,
3336 rpcrt4_ncalrpc_secure_packet,
3337 rpcrt4_conn_np_impersonate_client,
3338 rpcrt4_conn_np_revert_to_self,
3339 rpcrt4_ncalrpc_inquire_auth_client,
3340 },
3341 { "ncacn_ip_tcp",
3342 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
3343 rpcrt4_conn_tcp_alloc,
3344 rpcrt4_ncacn_ip_tcp_open,
3345 rpcrt4_conn_tcp_handoff,
3346 rpcrt4_conn_tcp_read,
3347 rpcrt4_conn_tcp_write,
3348 rpcrt4_conn_tcp_close,
3349 rpcrt4_conn_tcp_cancel_call,
3350 rpcrt4_conn_tcp_wait_for_incoming_data,
3351 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
3352 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
3353 NULL,
3354 RPCRT4_default_is_authorized,
3355 RPCRT4_default_authorize,
3356 RPCRT4_default_secure_packet,
3357 RPCRT4_default_impersonate_client,
3358 RPCRT4_default_revert_to_self,
3359 RPCRT4_default_inquire_auth_client,
3360 },
3361 { "ncacn_http",
3362 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP },
3363 rpcrt4_ncacn_http_alloc,
3364 rpcrt4_ncacn_http_open,
3365 rpcrt4_ncacn_http_handoff,
3366 rpcrt4_ncacn_http_read,
3367 rpcrt4_ncacn_http_write,
3368 rpcrt4_ncacn_http_close,
3369 rpcrt4_ncacn_http_cancel_call,
3370 rpcrt4_ncacn_http_wait_for_incoming_data,
3371 rpcrt4_ncacn_http_get_top_of_tower,
3372 rpcrt4_ncacn_http_parse_top_of_tower,
3373 rpcrt4_ncacn_http_receive_fragment,
3374 RPCRT4_default_is_authorized,
3375 RPCRT4_default_authorize,
3376 RPCRT4_default_secure_packet,
3377 RPCRT4_default_impersonate_client,
3378 RPCRT4_default_revert_to_self,
3379 RPCRT4_default_inquire_auth_client,
3380 },
3381 };
3382
3383
3384 static const struct protseq_ops protseq_list[] =
3385 {
3386 {
3387 "ncacn_np",
3388 rpcrt4_protseq_np_alloc,
3389 rpcrt4_protseq_np_signal_state_changed,
3390 rpcrt4_protseq_np_get_wait_array,
3391 rpcrt4_protseq_np_free_wait_array,
3392 rpcrt4_protseq_np_wait_for_new_connection,
3393 rpcrt4_protseq_ncacn_np_open_endpoint,
3394 },
3395 {
3396 "ncalrpc",
3397 rpcrt4_protseq_np_alloc,
3398 rpcrt4_protseq_np_signal_state_changed,
3399 rpcrt4_protseq_np_get_wait_array,
3400 rpcrt4_protseq_np_free_wait_array,
3401 rpcrt4_protseq_np_wait_for_new_connection,
3402 rpcrt4_protseq_ncalrpc_open_endpoint,
3403 },
3404 {
3405 "ncacn_ip_tcp"<