27#define WIN32_NO_STATUS
29#define NONAMELESSUNION
43#include "wine/winternl.h"
57#define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
144 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate Everyone SID (error code %d)\n",
GetLastError());
154 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate Anonymous SID (error code %d)\n",
GetLastError());
166 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate Admins SID (error code %d)\n",
GetLastError());
174 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate absolute SD!\n");
181 ERR(
"rpcrt4_create_pipe_security(): Failed to create absolute SD (error code %d)\n",
GetLastError());
195 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate DACL!\n");
202 ERR(
"rpcrt4_create_pipe_security(): Failed to create DACL (error code %d)\n",
GetLastError());
212 ERR(
"rpcrt4_create_pipe_security(): Failed to set up ACE for Everyone SID (error code %d)\n",
GetLastError());
222 ERR(
"rpcrt4_create_pipe_security(): Failed to set up ACE for Anonymous SID (error code %d)\n",
GetLastError());
232 ERR(
"rpcrt4_create_pipe_security(): Failed to set up ACE for Admins SID (error code %d)\n",
GetLastError());
239 ERR(
"rpcrt4_create_pipe_security(): Failed to set DACL to absolute SD (error code %d)\n",
GetLastError());
246 ERR(
"rpcrt4_create_pipe_security(): Failed to set SD owner (error code %d)\n",
GetLastError());
253 ERR(
"rpcrt4_create_pipe_security(): Failed to set SD group (error code %d)\n",
GetLastError());
260 ERR(
"rpcrt4_create_pipe_security(): Unexpected error code (error code %d -- must be ERROR_INSUFFICIENT_BUFFER)\n",
GetLastError());
268 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate relative SD!\n");
275 ERR(
"rpcrt4_create_pipe_security(): Failed to allocate relative SD, buffer too smal (expected size %lu)\n", RelSDSize);
280 TRACE(
"rpcrt4_create_pipe_security(): Success!\n");
293 if (EveryoneSid !=
NULL)
298 if (AnonymousSid !=
NULL)
303 if (AdminsSid !=
NULL)
334 ErrCode = rpcrt4_create_pipe_security(&PipeSecDesc);
337 ERR(
"rpcrt4_conn_create_pipe(): Pipe security descriptor creation failed!\n");
412 TRACE(
"retrying busy server\n");
415 TRACE(
"connection failed, error=%x\n",
err);
419 TRACE(
"connection failed, error=%x\n",
err);
425 WARN(
"connection failed, error=%x\n",
err);
441 static const char prefix[] =
"\\\\.\\pipe\\lrpc\\";
472 char generated_endpoint[22];
476 static LONG lrpc_nameless_id;
479 snprintf(generated_endpoint,
sizeof(generated_endpoint),
480 "LRPC%08x.%08x", process_id,
id);
507 static const char prefix[] =
"\\\\";
508 static const char local[] =
".";
512 static const char prefix[] =
"\\\\.";
540 strcpy(pipe_name, prefix);
575 char generated_endpoint[26];
579 static LONG np_nameless_id;
582 snprintf(generated_endpoint,
sizeof(generated_endpoint),
583 "\\\\pipe\\\\%08x.%03x", process_id,
id);
630 ERR(
"Failed to retrieve the computer name, error %u\n",
GetLastError());
682 ERR(
"Failed to retrieve the computer name, error %u\n",
GetLastError());
750 if (connection->
pipe)
754 connection->
pipe = 0;
794 const char *networkaddr,
800 size_t networkaddr_size;
801 size_t endpoint_size;
805 networkaddr_size = networkaddr ?
strlen(networkaddr) + 1 : 1;
807 size =
sizeof(*smb_floor) + endpoint_size +
sizeof(*nb_floor) + networkaddr_size;
814 tower_data +=
sizeof(*smb_floor);
824 tower_data += endpoint_size;
828 tower_data +=
sizeof(*nb_floor);
835 memcpy(tower_data, networkaddr, networkaddr_size);
850 TRACE(
"(%p, %d, %p, %p)\n", tower_data, (
int)tower_size, networkaddr,
endpoint);
852 if (tower_size <
sizeof(*smb_floor))
855 tower_data +=
sizeof(*smb_floor);
856 tower_size -=
sizeof(*smb_floor);
861 (tower_data[smb_floor->
count_rhs - 1] !=
'\0'))
874 if (tower_size <
sizeof(*nb_floor))
879 tower_data +=
sizeof(*nb_floor);
880 tower_size -=
sizeof(*nb_floor);
885 (tower_data[nb_floor->
count_rhs - 1] !=
'\0'))
911 TRACE(
"(%p)\n", conn);
920 WARN(
"ImpersonateNamedPipeClient failed with error %u\n",
error);
934 TRACE(
"(%p)\n", conn);
970 HANDLE *objs = prev_array;
1018 ERR(
"couldn't allocate objs\n");
1042 HANDLE *objs = wait_array;
1087 ERR(
"failed to locate connection for handle %p\n", b_handle);
1096 const char *networkaddr,
1101 size_t endpoint_size;
1106 size =
sizeof(*pipe_floor) + endpoint_size;
1113 tower_data +=
sizeof(*pipe_floor);
1131 TRACE(
"(%p, %d, %p, %p)\n", tower_data, (
int)tower_size, networkaddr,
endpoint);
1133 if (tower_size <
sizeof(*pipe_floor))
1136 tower_data +=
sizeof(*pipe_floor);
1137 tower_size -=
sizeof(*pipe_floor);
1142 (tower_data[pipe_floor->
count_rhs - 1] !=
'\0'))
1146 *networkaddr =
NULL;
1179 unsigned char *stub_data,
unsigned int stub_data_size,
1181 unsigned char *auth_value,
unsigned int auth_value_size)
1192 TRACE(
"(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn, privs,
1193 server_princ_name, authn_level, authn_svc, authz_svc,
flags);
1197 FIXME(
"privs not implemented\n");
1200 if (server_princ_name)
1202 FIXME(
"server_princ_name not implemented\n");
1203 *server_princ_name =
NULL;
1209 FIXME(
"authorization service not implemented\n");
1213 FIXME(
"flags 0x%x not implemented\n",
flags);
1221 const char *networkaddr,
1222 unsigned char tcp_protid,
1230 size_t size =
sizeof(*tcp_floor) +
sizeof(*ipv4_floor);
1238 tower_data +=
sizeof(*tcp_floor);
1243 tcp_floor->
protid = tcp_protid;
1255 hints.ai_addrlen = 0;
1261 static BOOL wsa_inited;
1278 ERR(
"getaddrinfo failed: %s\n", gai_strerror(
ret));
1286 tcp_floor->
port =
sin->sin_port;
1304 unsigned char tcp_protid,
1311 TRACE(
"(%p, %d, %p, %p)\n", tower_data, (
int)tower_size, networkaddr,
endpoint);
1313 if (tower_size <
sizeof(*tcp_floor))
1316 tower_data +=
sizeof(*tcp_floor);
1317 tower_size -=
sizeof(*tcp_floor);
1319 if (tower_size <
sizeof(*ipv4_floor))
1325 (tcp_floor->
protid != tcp_protid) ||
1357 *networkaddr =
NULL;
1380 static BOOL wsa_inited;
1393 ERR(
"event creation failed\n");
1419 ERR(
"WaitForMultipleObjects() failed with error %d\n",
GetLastError());
1438 ERR(
"WaitForMultipleObjects() failed with error %d\n",
GetLastError());
1469 if (tcpc->
sock != -1)
1476 hints.ai_addrlen = 0;
1484 ERR(
"getaddrinfo for %s:%s failed: %s\n", Connection->
NetworkAddr,
1489 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->
ai_next)
1496 TRACE(
"skipping non-IP/IPv6 address family\n");
1505 host,
sizeof(
host), service,
sizeof(service),
1533 TRACE(
"connected\n");
1557 hints.ai_addrlen = 0;
1565 ERR(
"getaddrinfo for port %s failed: %s\n",
endpoint,
1572 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->
ai_next)
1583 TRACE(
"skipping non-IP/IPv6 address family\n");
1591 host,
sizeof(
host), service,
sizeof(service),
1616 sa_len =
sizeof(
sa);
1626 NULL, 0, service,
sizeof(service),
1630 WARN(
"getnameinfo failed: %s\n", gai_strerror(
ret));
1663 WARN(
"couldn't make socket non-blocking, error %d\n",
ret);
1700 ERR(
"Failed to accept a TCP connection: error %d\n",
ret);
1712 ERR(
"Failed to retrieve the IP address, error %d\n",
ret);
1716 TRACE(
"Accepted a new TCP connection from %s\n",
client->common.NetworkAddr);
1725 while (bytes_read !=
count)
1753 int bytes_written = 0;
1754 while (bytes_written !=
count)
1770 return bytes_written;
1779 if (connection->
sock != -1)
1781 connection->
sock = -1;
1797 TRACE(
"%p\n", connection);
1812 TRACE(
"%p\n", Connection);
1820 const char *networkaddr,
1838 static BOOL wsa_inited;
1860 HANDLE *objs = prev_array;
1870 if (conn->
sock != -1)
1881 ERR(
"couldn't allocate objs\n");
1890 if (conn->
sock != -1)
1914 HANDLE *objs = wait_array;
1954 ERR(
"failed to locate connection for handle %p\n", b_handle);
1975#define HTTP_IDLE_TIME 60000
1999 TRACE(
"destroying async data %p\n",
data);
2002 data->cs.DebugInfo->Spare[0] = 0;
2033 TRACE(
"Cancelled\n");
2038 ERR(
"Async request failed with error %d\n", async_data->
async_result);
2082 if (!httpc)
return NULL;
2114 DWORD bytes_written;
2123 DWORD cached_last_sent_time = *last_sent_time;
2153 DWORD dwInternetStatus,
2154 LPVOID lpvStatusInformation,
2155 DWORD dwStatusInformationLength)
2159 switch (dwInternetStatus)
2162 TRACE(
"INTERNET_STATUS_REQUEST_COMPLETED\n");
2211 static const WCHAR wszUserAgent[] = {
'M',
'S',
'R',
'P',
'C',0};
2249 static const WCHAR wszRpcProxy[] = {
'R',
'p',
'c',
'P',
'r',
'o',
'x',
'y',
'=',0};
2250 static const WCHAR wszHttpProxy[] = {
'H',
't',
't',
'p',
'P',
'r',
'o',
'x',
'y',
'=',0};
2255 const WCHAR *value_end;
2260 value_end = value_start +
lstrlenW(value_start);
2261 for (
p = value_start;
p < value_end;
p++)
2268 TRACE(
"RpcProxy value is %s\n",
debugstr_wn(value_start, value_end-value_start));
2274 const WCHAR *value_end;
2278 value_end = value_start +
lstrlenW(value_start);
2279 TRACE(
"HttpProxy value is %s\n",
debugstr_wn(value_start, value_end-value_start));
2341 unsigned int bytes_left =
count;
2355 TRACE(
"call cancelled\n");
2381 TRACE(
"sending echo request to server\n");
2399 static const WCHAR fmtW[] =
2400 {
'C',
'o',
'n',
't',
'e',
'n',
't',
'-',
'L',
'e',
'n',
'g',
't',
'h',
':',
' ',
'%',
'u',
'\r',
'\n',0};
2410 const UUID *connection_uuid,
const UUID *in_pipe_uuid,
2411 const UUID *association_uuid,
BOOL authorized)
2417 DWORD bytes_written;
2425 memset(&buffers_in, 0,
sizeof(buffers_in));
2437 TRACE(
"sending HTTP connect header to server\n");
2454 unsigned short data_len;
2461 ERR(
"wrong packet type received %d or wrong frag_len %d\n",
2462 hdr->common.ptype,
hdr->common.frag_len);
2466 size =
sizeof(
hdr->http) -
sizeof(
hdr->common);
2470 data_len =
hdr->common.frag_len -
sizeof(
hdr->http);
2487 ERR(
"invalid http packet\n");
2497 HANDLE cancel_event,
const UUID *connection_uuid,
2498 const UUID *out_pipe_uuid,
ULONG *flow_control_increment,
2504 BYTE *data_from_server;
2506 ULONG field1, field3;
2528 TRACE(
"sending HTTP connect header to server\n");
2539 &pkt_from_server, &data_from_server);
2545 TRACE(
"received (%d) from first prepare header\n", field1);
2550 &pkt_from_server, &data_from_server);
2552 if (pkt_from_server.
http.
flags != 0x0001)
break;
2554 TRACE(
"http idle packet, waiting for real packet\n");
2558 ERR(
"HTTP idle packet should have no data items instead of %d\n",
2564 &field1, flow_control_increment,
2568 TRACE(
"received (0x%08x 0x%08x %d) from second prepare header\n", field1, *flow_control_increment, field3);
2575 static const char enc[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2581 base64[
i++] = enc[(
bin[0] & 0xfc) >> 2];
2582 x = (
bin[0] & 3) << 4;
2587 base64[
i++] = enc[
x];
2592 base64[
i++] = enc[
x | ((
bin[1] & 0xf0) >> 4)];
2593 x = (
bin[1] & 0x0f) << 2;
2598 base64[
i++] = enc[
x];
2602 base64[
i++] = enc[
x | ((
bin[2] & 0xc0) >> 6)];
2605 base64[
i++] = enc[
bin[2] & 0x3f];
2615 if (
c >=
'A' &&
c <=
'Z')
return c -
'A';
2616 if (
c >=
'a' &&
c <=
'z')
return c -
'a' + 26;
2617 if (
c >=
'0' &&
c <=
'9')
return c -
'0' + 52;
2618 if (
c ==
'+')
return 62;
2619 if (
c ==
'/')
return 63;
2626 char c0, c1, c2, c3;
2638 buf[
i + 0] = (c0 << 2) | (c1 >> 4);
2639 buf[
i + 1] = (c1 << 4) | (c2 >> 2);
2640 buf[
i + 2] = (c2 << 6) | c3;
2651 if (
buf)
buf[
i] = (c0 << 2) | (c1 >> 4);
2654 else if (
p[3] ==
'=')
2662 buf[
i + 0] = (c0 << 2) | (c1 >> 4);
2663 buf[
i + 1] = (c1 << 4) | (c2 >> 2);
2676 buf[
i + 0] = (c0 << 2) | (c1 >> 4);
2677 buf[
i + 1] = (c1 << 4) | (c2 >> 2);
2678 buf[
i + 2] = (c2 << 6) | c3;
2776 info->data_len = userlen + passlen + 1;
2783 info->data[userlen] =
':';
2795 static SEC_WCHAR ntlmW[] = {
'N',
'T',
'L',
'M',0},
negotiateW[] = {
'N',
'e',
'g',
'o',
't',
'i',
'a',
't',
'e',0};
2803 WCHAR auth_value[2048];
2831 ERR(
"authentication scheme changed\n");
2841 in_desc.pBuffers = &
in;
2843 p = auth_value + scheme_len;
2852 out.cbBuffer =
info->max_token;
2860 out_desc.pBuffers = &
out;
2864 in.pvBuffer ? &in_desc :
NULL, 0, &
info->ctx, &out_desc,
2871 info->data_len =
out.cbBuffer;
2873 TRACE(
"sending last auth packet\n");
2880 info->data_len =
out.cbBuffer;
2881 TRACE(
"sending next auth packet\n");
2886 ERR(
"InitializeSecurityContextW failed with error 0x%08x\n",
ret);
2910 static const WCHAR authW[] = {
'A',
'u',
't',
'h',
'o',
'r',
'i',
'z',
'a',
't',
'i',
'o',
'n',
':',
' '};
2911 static const WCHAR basicW[] = {
'B',
'a',
's',
'i',
'c',
' '};
2912 static const WCHAR negotiateW[] = {
'N',
'e',
'g',
'o',
't',
'i',
'a',
't',
'e',
' '};
2913 static const WCHAR ntlmW[] = {
'N',
'T',
'L',
'M',
' '};
2915 const WCHAR *scheme_str;
2971 static const WCHAR authW[] = {
'A',
'u',
't',
'h',
'o',
'r',
'i',
'z',
'a',
't',
'i',
'o',
'n',
':',
'\r',
'\n',0};
3014 if (!
id || !
id->User || !
id->Password)
return FALSE;
3029 static WCHAR httpsW[] = {
'h',
't',
't',
'p',
's',0};
3074 static const WCHAR wszVerbIn[] = {
'R',
'P',
'C',
'_',
'I',
'N',
'_',
'D',
'A',
'T',
'A',0};
3075 static const WCHAR wszVerbOut[] = {
'R',
'P',
'C',
'_',
'O',
'U',
'T',
'_',
'D',
'A',
'T',
'A',0};
3076 static const WCHAR wszRpcProxyPrefix[] = {
'/',
'r',
'p',
'c',
'/',
'r',
'p',
'c',
'p',
'r',
'o',
'x',
'y',
'.',
'd',
'l',
'l',
'?',0};
3077 static const WCHAR wszColon[] = {
':',0};
3078 static const WCHAR wszAcceptType[] = {
'a',
'p',
'p',
'l',
'i',
'c',
'a',
't',
'i',
'o',
'n',
'/',
'r',
'p',
'c',0};
3079 LPCWSTR wszAcceptTypes[] = { wszAcceptType,
NULL };
3091 ERR(
"ncacn_http servers not supported yet\n");
3111 memcpy(
url, wszRpcProxyPrefix,
sizeof(wszRpcProxyPrefix));
3175 &Connection->
assoc->http_uuid, credentials);
3230 TRACE(
"(%p, %p, %p)\n", Connection,
Header, Payload);
3235 if (dwRead !=
sizeof(common_hdr)) {
3236 WARN(
"Short read of header, %d bytes\n", dwRead);
3240 if (!
memcmp(&common_hdr,
"HTTP/1.1",
sizeof(
"HTTP/1.1")) ||
3241 !
memcmp(&common_hdr,
"HTTP/1.0",
sizeof(
"HTTP/1.0")))
3252 if (hdr_length == 0) {
3253 WARN(
"header length == 0\n");
3268 if (dwRead != hdr_length -
sizeof(common_hdr)) {
3269 WARN(
"bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
3274 if (common_hdr.
frag_len - hdr_length)
3284 if (dwRead != common_hdr.
frag_len - hdr_length)
3286 WARN(
"bad data length, %d/%d\n", dwRead, common_hdr.
frag_len - hdr_length);
3294 if ((*Header)->common.ptype ==
PKT_HTTP)
3298 ERR(
"invalid http packet of length %d bytes\n", (*Header)->common.frag_len);
3302 if ((*Header)->http.flags == 0x0001)
3304 TRACE(
"http idle packet, waiting for real packet\n");
3305 if ((*Header)->http.num_data_items != 0)
3307 ERR(
"HTTP idle packet should have no data items instead of %d\n", (*Header)->http.num_data_items);
3312 else if ((*Header)->http.flags == 0x0002)
3314 ULONG bytes_transmitted;
3315 ULONG flow_control_increment;
3320 &flow_control_increment,
3324 TRACE(
"received http flow control header (0x%x, 0x%x, %s)\n",
3325 bytes_transmitted, flow_control_increment,
debugstr_guid(&pipe_uuid));
3330 FIXME(
"unrecognised http packet with flags 0x%04x\n", (*Header)->http.flags);
3356 DWORD bytes_written;
3380 DWORD bytes_written;
3387 return ret ? bytes_written : -1;
3450 const char *networkaddr,
3612 TRACE(
"(Connection == ^%p)\n", Connection);
3615 return Connection->
ops->open_connection_client(Connection);
3620 TRACE(
"(Connection == ^%p)\n", Connection);
3634 static LONG next_id;
3641 FIXME(
"not supported for protseq %s\n", Protseq);
3645 NewConnection = ops->
alloc();
3646 NewConnection->
ref = 1;
3648 NewConnection->
ops = ops;
3658 NewConnection->
AuthInfo = AuthInfo;
3661 NewConnection->
QOS =
QOS;
3666 TRACE(
"connection: %p\n", NewConnection);
3667 *Connection = NewConnection;
3698 if (connection->
ref > 1)
3713 TRACE(
"%p ref=%u\n", connection,
ref);
3738 TRACE(
"%p ref=%u\n", connection,
ref);
3767 FIXME(
"not supported for protseq %s\n", protseq);
3776 const char *protseq,
3777 const char *networkaddr,
3790 *tower_size =
sizeof(*protocol_floor);
3800 tower_data +=
sizeof(*protocol_floor);
3806 *tower_size +=
sizeof(*protocol_floor);
3823 if (tower_size <
sizeof(*protocol_floor))
3827 tower_data +=
sizeof(*protocol_floor);
3828 tower_size -=
sizeof(*protocol_floor);
3830 (protocol_floor->
count_rhs > tower_size))
3832 tower_data += protocol_floor->
count_rhs;
3833 tower_size -= protocol_floor->
count_rhs;
3836 if ((tower_size <
sizeof(*floor4)) ||
3909 for (
i = 0;
i < (*protseqs)->Count;
i++)
3927 for (
i = 0;
i < (*protseqs)->Count;
i++)