4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006 CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
37 #include "wine/debug.h"
39 #include "rpc_binding.h"
41 #include "rpc_message.h"
42 #include "rpc_assoc.h"
43 #include "ncastatus.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
47 /* note: the DCE/RPC spec says the alignment amount should be 4, but
48 * MS/RPC servers seem to always use 16 */
49 #define AUTH_ALIGNMENT 16
51 /* gets the amount needed to round a value up to the specified alignment */
52 #define ROUND_UP_AMOUNT(value, alignment) \
53 (((alignment) - (((value) % (alignment)))) % (alignment))
54 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
56 static RPC_STATUS
I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg
);
58 DWORD
RPCRT4_GetHeaderSize(const RpcPktHdr
*Header
)
60 static const DWORD header_sizes
[] = {
61 sizeof(Header
->request
), 0, sizeof(Header
->response
),
62 sizeof(Header
->fault
), 0, 0, 0, 0, 0, 0, 0, sizeof(Header
->bind
),
63 sizeof(Header
->bind_ack
), sizeof(Header
->bind_nack
),
64 0, 0, sizeof(Header
->auth3
), 0, 0, 0, sizeof(Header
->http
)
68 if (Header
->common
.ptype
< ARRAY_SIZE(header_sizes
)) {
69 ret
= header_sizes
[Header
->common
.ptype
];
71 FIXME("unhandled packet type %u\n", Header
->common
.ptype
);
72 if (Header
->common
.flags
& RPC_FLG_OBJECT_UUID
)
75 WARN("invalid packet type %u\n", Header
->common
.ptype
);
81 static BOOL
packet_has_body(const RpcPktHdr
*Header
)
83 return (Header
->common
.ptype
== PKT_FAULT
) ||
84 (Header
->common
.ptype
== PKT_REQUEST
) ||
85 (Header
->common
.ptype
== PKT_RESPONSE
);
88 static BOOL
packet_has_auth_verifier(const RpcPktHdr
*Header
)
90 return !(Header
->common
.ptype
== PKT_BIND_NACK
) &&
91 !(Header
->common
.ptype
== PKT_SHUTDOWN
);
94 static BOOL
packet_does_auth_negotiation(const RpcPktHdr
*Header
)
96 switch (Header
->common
.ptype
)
101 case PKT_ALTER_CONTEXT
:
102 case PKT_ALTER_CONTEXT_RESP
:
109 static VOID
RPCRT4_BuildCommonHeader(RpcPktCommonHdr
*Header
, unsigned char PacketType
,
110 ULONG DataRepresentation
)
112 Header
->rpc_ver
= RPC_VER_MAJOR
;
113 Header
->rpc_ver_minor
= RPC_VER_MINOR
;
114 Header
->ptype
= PacketType
;
115 Header
->drep
[0] = LOBYTE(LOWORD(DataRepresentation
));
116 Header
->drep
[1] = HIBYTE(LOWORD(DataRepresentation
));
117 Header
->drep
[2] = LOBYTE(HIWORD(DataRepresentation
));
118 Header
->drep
[3] = HIBYTE(HIWORD(DataRepresentation
));
119 Header
->auth_len
= 0;
122 /* Flags and fragment length are computed in RPCRT4_Send. */
125 static RpcPktHdr
*RPCRT4_BuildRequestHeader(ULONG DataRepresentation
,
127 unsigned short ProcNum
,
134 has_object
= (ObjectUuid
!= NULL
&& !UuidIsNil(ObjectUuid
, &status
));
135 header
= calloc(1, sizeof(header
->request
) + (has_object
? sizeof(UUID
) : 0));
136 if (header
== NULL
) {
140 RPCRT4_BuildCommonHeader(&header
->common
, PKT_REQUEST
, DataRepresentation
);
141 header
->common
.frag_len
= sizeof(header
->request
);
142 header
->request
.alloc_hint
= BufferLength
;
143 header
->request
.context_id
= 0;
144 header
->request
.opnum
= ProcNum
;
146 header
->common
.flags
|= RPC_FLG_OBJECT_UUID
;
147 header
->common
.frag_len
+= sizeof(UUID
);
148 memcpy(&header
->request
+ 1, ObjectUuid
, sizeof(UUID
));
154 RpcPktHdr
*RPCRT4_BuildResponseHeader(ULONG DataRepresentation
, ULONG BufferLength
)
156 RpcPktResponseHdr
*header
;
158 header
= calloc(1, sizeof(*header
));
159 if (header
== NULL
) {
163 RPCRT4_BuildCommonHeader(&header
->common
, PKT_RESPONSE
, DataRepresentation
);
164 header
->common
.frag_len
= sizeof(*header
);
165 header
->alloc_hint
= BufferLength
;
167 return (RpcPktHdr
*)header
;
170 RpcPktHdr
*RPCRT4_BuildFaultHeader(ULONG DataRepresentation
, RPC_STATUS Status
)
172 RpcPktFaultHdr
*header
;
174 header
= calloc(1, sizeof(*header
));
175 if (header
== NULL
) {
179 RPCRT4_BuildCommonHeader(&header
->common
, PKT_FAULT
, DataRepresentation
);
180 header
->common
.frag_len
= sizeof(*header
);
181 header
->status
= Status
;
183 return (RpcPktHdr
*)header
;
186 RpcPktHdr
*RPCRT4_BuildBindHeader(ULONG DataRepresentation
,
187 unsigned short MaxTransmissionSize
,
188 unsigned short MaxReceiveSize
,
190 const RPC_SYNTAX_IDENTIFIER
*AbstractId
,
191 const RPC_SYNTAX_IDENTIFIER
*TransferId
)
194 RpcContextElement
*ctxt_elem
;
196 header
= calloc(1, sizeof(header
->bind
) + FIELD_OFFSET(RpcContextElement
, transfer_syntaxes
[1]));
197 if (header
== NULL
) {
200 ctxt_elem
= (RpcContextElement
*)(&header
->bind
+ 1);
202 RPCRT4_BuildCommonHeader(&header
->common
, PKT_BIND
, DataRepresentation
);
203 header
->common
.frag_len
= sizeof(header
->bind
) + FIELD_OFFSET(RpcContextElement
, transfer_syntaxes
[1]);
204 header
->bind
.max_tsize
= MaxTransmissionSize
;
205 header
->bind
.max_rsize
= MaxReceiveSize
;
206 header
->bind
.assoc_gid
= AssocGroupId
;
207 header
->bind
.num_elements
= 1;
208 ctxt_elem
->num_syntaxes
= 1;
209 ctxt_elem
->abstract_syntax
= *AbstractId
;
210 ctxt_elem
->transfer_syntaxes
[0] = *TransferId
;
215 static RpcPktHdr
*RPCRT4_BuildAuthHeader(ULONG DataRepresentation
)
217 RpcPktAuth3Hdr
*header
;
219 header
= calloc(1, sizeof(*header
));
223 RPCRT4_BuildCommonHeader(&header
->common
, PKT_AUTH3
, DataRepresentation
);
224 header
->common
.frag_len
= sizeof(*header
);
226 return (RpcPktHdr
*)header
;
229 RpcPktHdr
*RPCRT4_BuildBindNackHeader(ULONG DataRepresentation
,
230 unsigned char RpcVersion
,
231 unsigned char RpcVersionMinor
,
232 unsigned short RejectReason
)
234 RpcPktBindNAckHdr
*header
;
235 C_ASSERT(sizeof(*header
) >= FIELD_OFFSET(RpcPktBindNAckHdr
, protocols
[1]));
237 header
= calloc(1, sizeof(*header
));
238 if (header
== NULL
) {
242 RPCRT4_BuildCommonHeader(&header
->common
, PKT_BIND_NACK
, DataRepresentation
);
243 header
->common
.frag_len
= FIELD_OFFSET(RpcPktBindNAckHdr
, protocols
[1]);
244 header
->reject_reason
= RejectReason
;
245 header
->protocols_count
= 1;
246 header
->protocols
[0].rpc_ver
= RpcVersion
;
247 header
->protocols
[0].rpc_ver_minor
= RpcVersionMinor
;
249 return (RpcPktHdr
*)header
;
252 RpcPktHdr
*RPCRT4_BuildBindAckHeader(ULONG DataRepresentation
,
253 unsigned short MaxTransmissionSize
,
254 unsigned short MaxReceiveSize
,
256 LPCSTR ServerAddress
,
257 unsigned char ResultCount
,
258 const RpcResult
*Results
)
262 RpcAddressString
*server_address
;
263 RpcResultList
*results
;
265 header_size
= sizeof(header
->bind_ack
) +
266 ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[strlen(ServerAddress
) + 1]), 4) +
267 FIELD_OFFSET(RpcResultList
, results
[ResultCount
]);
269 header
= calloc(1, header_size
);
270 if (header
== NULL
) {
274 RPCRT4_BuildCommonHeader(&header
->common
, PKT_BIND_ACK
, DataRepresentation
);
275 header
->common
.frag_len
= header_size
;
276 header
->bind_ack
.max_tsize
= MaxTransmissionSize
;
277 header
->bind_ack
.max_rsize
= MaxReceiveSize
;
278 header
->bind_ack
.assoc_gid
= AssocGroupId
;
279 server_address
= (RpcAddressString
*)(&header
->bind_ack
+ 1);
280 server_address
->length
= strlen(ServerAddress
) + 1;
281 strcpy(server_address
->string
, ServerAddress
);
282 /* results is 4-byte aligned */
283 results
= (RpcResultList
*)((ULONG_PTR
)server_address
+ ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[server_address
->length
]), 4));
284 results
->num_results
= ResultCount
;
285 memcpy(&results
->results
[0], Results
, ResultCount
* sizeof(*Results
));
290 RpcPktHdr
*RPCRT4_BuildHttpHeader(ULONG DataRepresentation
,
291 unsigned short flags
,
292 unsigned short num_data_items
,
293 unsigned int payload_size
)
297 header
= calloc(1, sizeof(header
->http
) + payload_size
);
298 if (header
== NULL
) {
299 ERR("failed to allocate memory\n");
303 RPCRT4_BuildCommonHeader(&header
->common
, PKT_HTTP
, DataRepresentation
);
304 /* since the packet isn't current sent using RPCRT4_Send, set the flags
306 header
->common
.flags
= RPC_FLG_FIRST
|RPC_FLG_LAST
;
307 header
->common
.call_id
= 0;
308 header
->common
.frag_len
= sizeof(header
->http
) + payload_size
;
309 header
->http
.flags
= flags
;
310 header
->http
.num_data_items
= num_data_items
;
315 #define WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, type, value) \
317 *(unsigned int *)(payload) = (type); \
319 *(unsigned int *)(payload) = (value); \
323 #define WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, type, uuid) \
325 *(unsigned int *)(payload) = (type); \
327 *(UUID *)(payload) = (uuid); \
328 (payload) += sizeof(UUID); \
331 #define WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted, flow_control_increment, uuid) \
333 *(unsigned int *)(payload) = 0x00000001; \
335 *(unsigned int *)(payload) = (bytes_transmitted); \
337 *(unsigned int *)(payload) = (flow_control_increment); \
339 *(UUID *)(payload) = (uuid); \
340 (payload) += sizeof(UUID); \
343 RpcPktHdr
*RPCRT4_BuildHttpConnectHeader(int out_pipe
,
344 const UUID
*connection_uuid
,
345 const UUID
*pipe_uuid
,
346 const UUID
*association_uuid
)
352 size
= 8 + 4 + sizeof(UUID
) + 4 + sizeof(UUID
) + 8;
354 size
+= 8 + 4 + sizeof(UUID
);
356 header
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0,
357 out_pipe
? 4 : 6, size
);
358 if (!header
) return NULL
;
359 payload
= (char *)(&header
->http
+1);
361 /* FIXME: what does this part of the payload do? */
362 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload
, 0x00000006, 0x00000001);
364 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload
, 0x00000003, *connection_uuid
);
365 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload
, 0x00000003, *pipe_uuid
);
368 /* FIXME: what does this part of the payload do? */
369 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload
, 0x00000000, 0x00010000);
372 /* FIXME: what does this part of the payload do? */
373 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload
, 0x00000004, 0x40000000);
374 /* FIXME: what does this part of the payload do? */
375 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload
, 0x00000005, 0x000493e0);
377 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload
, 0x0000000c, *association_uuid
);
383 RpcPktHdr
*RPCRT4_BuildHttpFlowControlHeader(BOOL server
, ULONG bytes_transmitted
,
384 ULONG flow_control_increment
,
385 const UUID
*pipe_uuid
)
390 header
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x2, 2,
391 5 * sizeof(ULONG
) + sizeof(UUID
));
392 if (!header
) return NULL
;
393 payload
= (char *)(&header
->http
+1);
395 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload
, 0x0000000d, (server
? 0x0 : 0x3));
397 WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload
, bytes_transmitted
,
398 flow_control_increment
, *pipe_uuid
);
402 NCA_STATUS
RPC2NCA_STATUS(RPC_STATUS status
)
406 case ERROR_INVALID_HANDLE
: return NCA_S_FAULT_CONTEXT_MISMATCH
;
407 case ERROR_OUTOFMEMORY
: return NCA_S_FAULT_REMOTE_NO_MEMORY
;
408 case RPC_S_NOT_LISTENING
: return NCA_S_SERVER_TOO_BUSY
;
409 case RPC_S_UNKNOWN_IF
: return NCA_S_UNK_IF
;
410 case RPC_S_SERVER_TOO_BUSY
: return NCA_S_SERVER_TOO_BUSY
;
411 case RPC_S_CALL_FAILED
: return NCA_S_FAULT_UNSPEC
;
412 case RPC_S_CALL_FAILED_DNE
: return NCA_S_MANAGER_NOT_ENTERED
;
413 case RPC_S_PROTOCOL_ERROR
: return NCA_S_PROTO_ERROR
;
414 case RPC_S_UNSUPPORTED_TYPE
: return NCA_S_UNSUPPORTED_TYPE
;
415 case RPC_S_INVALID_TAG
: return NCA_S_FAULT_INVALID_TAG
;
416 case RPC_S_INVALID_BOUND
: return NCA_S_FAULT_INVALID_BOUND
;
417 case RPC_S_PROCNUM_OUT_OF_RANGE
: return NCA_S_OP_RNG_ERROR
;
418 case RPC_X_SS_HANDLES_MISMATCH
: return NCA_S_FAULT_CONTEXT_MISMATCH
;
419 case RPC_S_CALL_CANCELLED
: return NCA_S_FAULT_CANCEL
;
420 case RPC_S_COMM_FAILURE
: return NCA_S_COMM_FAILURE
;
421 case RPC_X_WRONG_PIPE_ORDER
: return NCA_S_FAULT_PIPE_ORDER
;
422 case RPC_X_PIPE_CLOSED
: return NCA_S_FAULT_PIPE_CLOSED
;
423 case RPC_X_PIPE_DISCIPLINE_ERROR
: return NCA_S_FAULT_PIPE_DISCIPLINE
;
424 case RPC_X_PIPE_EMPTY
: return NCA_S_FAULT_PIPE_EMPTY
;
425 case STATUS_FLOAT_DIVIDE_BY_ZERO
: return NCA_S_FAULT_FP_DIV_ZERO
;
426 case STATUS_FLOAT_INVALID_OPERATION
: return NCA_S_FAULT_FP_ERROR
;
427 case STATUS_FLOAT_OVERFLOW
: return NCA_S_FAULT_FP_OVERFLOW
;
428 case STATUS_FLOAT_UNDERFLOW
: return NCA_S_FAULT_FP_UNDERFLOW
;
429 case STATUS_INTEGER_DIVIDE_BY_ZERO
: return NCA_S_FAULT_INT_DIV_BY_ZERO
;
430 case STATUS_INTEGER_OVERFLOW
: return NCA_S_FAULT_INT_OVERFLOW
;
431 default: return status
;
435 static RPC_STATUS
NCA2RPC_STATUS(NCA_STATUS status
)
439 case NCA_S_COMM_FAILURE
: return RPC_S_COMM_FAILURE
;
440 case NCA_S_OP_RNG_ERROR
: return RPC_S_PROCNUM_OUT_OF_RANGE
;
441 case NCA_S_UNK_IF
: return RPC_S_UNKNOWN_IF
;
442 case NCA_S_YOU_CRASHED
: return RPC_S_CALL_FAILED
;
443 case NCA_S_PROTO_ERROR
: return RPC_S_PROTOCOL_ERROR
;
444 case NCA_S_OUT_ARGS_TOO_BIG
: return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
445 case NCA_S_SERVER_TOO_BUSY
: return RPC_S_SERVER_TOO_BUSY
;
446 case NCA_S_UNSUPPORTED_TYPE
: return RPC_S_UNSUPPORTED_TYPE
;
447 case NCA_S_FAULT_INT_DIV_BY_ZERO
: return RPC_S_ZERO_DIVIDE
;
448 case NCA_S_FAULT_ADDR_ERROR
: return RPC_S_ADDRESS_ERROR
;
449 case NCA_S_FAULT_FP_DIV_ZERO
: return RPC_S_FP_DIV_ZERO
;
450 case NCA_S_FAULT_FP_UNDERFLOW
: return RPC_S_FP_UNDERFLOW
;
451 case NCA_S_FAULT_FP_OVERFLOW
: return RPC_S_FP_OVERFLOW
;
452 case NCA_S_FAULT_INVALID_TAG
: return RPC_S_INVALID_TAG
;
453 case NCA_S_FAULT_INVALID_BOUND
: return RPC_S_INVALID_BOUND
;
454 case NCA_S_RPC_VERSION_MISMATCH
: return RPC_S_PROTOCOL_ERROR
;
455 case NCA_S_UNSPEC_REJECT
: return RPC_S_CALL_FAILED_DNE
;
456 case NCA_S_BAD_ACTID
: return RPC_S_CALL_FAILED_DNE
;
457 case NCA_S_WHO_ARE_YOU_FAILED
: return RPC_S_CALL_FAILED
;
458 case NCA_S_MANAGER_NOT_ENTERED
: return RPC_S_CALL_FAILED_DNE
;
459 case NCA_S_FAULT_CANCEL
: return RPC_S_CALL_CANCELLED
;
460 case NCA_S_FAULT_ILL_INST
: return RPC_S_ADDRESS_ERROR
;
461 case NCA_S_FAULT_FP_ERROR
: return RPC_S_FP_OVERFLOW
;
462 case NCA_S_FAULT_INT_OVERFLOW
: return RPC_S_ADDRESS_ERROR
;
463 case NCA_S_FAULT_UNSPEC
: return RPC_S_CALL_FAILED
;
464 case NCA_S_FAULT_PIPE_EMPTY
: return RPC_X_PIPE_EMPTY
;
465 case NCA_S_FAULT_PIPE_CLOSED
: return RPC_X_PIPE_CLOSED
;
466 case NCA_S_FAULT_PIPE_ORDER
: return RPC_X_WRONG_PIPE_ORDER
;
467 case NCA_S_FAULT_PIPE_DISCIPLINE
: return RPC_X_PIPE_DISCIPLINE_ERROR
;
468 case NCA_S_FAULT_PIPE_COMM_ERROR
: return RPC_S_COMM_FAILURE
;
469 case NCA_S_FAULT_PIPE_MEMORY
: return ERROR_OUTOFMEMORY
;
470 case NCA_S_FAULT_CONTEXT_MISMATCH
: return ERROR_INVALID_HANDLE
;
471 case NCA_S_FAULT_REMOTE_NO_MEMORY
: return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
472 default: return status
;
476 /* assumes the common header fields have already been validated */
477 BOOL
RPCRT4_IsValidHttpPacket(RpcPktHdr
*hdr
, unsigned char *data
,
478 unsigned short data_len
)
483 for (i
= 0; i
< hdr
->http
.num_data_items
; i
++)
487 if (data_len
< sizeof(ULONG
))
492 data_len
-= sizeof(ULONG
);
498 if (data_len
< sizeof(GUID
))
501 data_len
-= sizeof(GUID
);
509 if (data_len
< sizeof(ULONG
))
512 data_len
-= sizeof(ULONG
);
521 FIXME("unimplemented type 0x%lx\n", type
);
528 /* assumes the HTTP packet has been validated */
529 static unsigned char *RPCRT4_NextHttpHeaderField(unsigned char *data
)
533 type
= *(ULONG
*)data
;
534 data
+= sizeof(ULONG
);
540 return data
+ sizeof(GUID
);
547 return data
+ sizeof(ULONG
);
551 FIXME("unimplemented type 0x%lx\n", type
);
556 #define READ_HTTP_PAYLOAD_FIELD_TYPE(data) *(ULONG *)(data)
557 #define GET_HTTP_PAYLOAD_FIELD_DATA(data) ((data) + sizeof(ULONG))
559 /* assumes the HTTP packet has been validated */
560 RPC_STATUS
RPCRT4_ParseHttpPrepareHeader1(RpcPktHdr
*header
,
561 unsigned char *data
, ULONG
*field1
)
564 if (header
->http
.flags
!= 0x0)
566 ERR("invalid flags 0x%x\n", header
->http
.flags
);
567 return RPC_S_PROTOCOL_ERROR
;
569 if (header
->http
.num_data_items
!= 1)
571 ERR("invalid number of data items %d\n", header
->http
.num_data_items
);
572 return RPC_S_PROTOCOL_ERROR
;
574 type
= READ_HTTP_PAYLOAD_FIELD_TYPE(data
);
575 if (type
!= 0x00000002)
577 ERR("invalid type 0x%08lx\n", type
);
578 return RPC_S_PROTOCOL_ERROR
;
580 *field1
= *(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
);
584 /* assumes the HTTP packet has been validated */
585 RPC_STATUS
RPCRT4_ParseHttpPrepareHeader2(RpcPktHdr
*header
,
586 unsigned char *data
, ULONG
*field1
,
587 ULONG
*bytes_until_next_packet
,
591 if (header
->http
.flags
!= 0x0)
593 ERR("invalid flags 0x%x\n", header
->http
.flags
);
594 return RPC_S_PROTOCOL_ERROR
;
596 if (header
->http
.num_data_items
!= 3)
598 ERR("invalid number of data items %d\n", header
->http
.num_data_items
);
599 return RPC_S_PROTOCOL_ERROR
;
602 type
= READ_HTTP_PAYLOAD_FIELD_TYPE(data
);
603 if (type
!= 0x00000006)
605 ERR("invalid type for field 1: 0x%08lx\n", type
);
606 return RPC_S_PROTOCOL_ERROR
;
608 *field1
= *(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
);
609 data
= RPCRT4_NextHttpHeaderField(data
);
611 type
= READ_HTTP_PAYLOAD_FIELD_TYPE(data
);
612 if (type
!= 0x00000000)
614 ERR("invalid type for field 2: 0x%08lx\n", type
);
615 return RPC_S_PROTOCOL_ERROR
;
617 *bytes_until_next_packet
= *(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
);
618 data
= RPCRT4_NextHttpHeaderField(data
);
620 type
= READ_HTTP_PAYLOAD_FIELD_TYPE(data
);
621 if (type
!= 0x00000002)
623 ERR("invalid type for field 3: 0x%08lx\n", type
);
624 return RPC_S_PROTOCOL_ERROR
;
626 *field3
= *(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
);
631 RPC_STATUS
RPCRT4_ParseHttpFlowControlHeader(RpcPktHdr
*header
,
632 unsigned char *data
, BOOL server
,
633 ULONG
*bytes_transmitted
,
634 ULONG
*flow_control_increment
,
638 if (header
->http
.flags
!= 0x2)
640 ERR("invalid flags 0x%x\n", header
->http
.flags
);
641 return RPC_S_PROTOCOL_ERROR
;
643 if (header
->http
.num_data_items
!= 2)
645 ERR("invalid number of data items %d\n", header
->http
.num_data_items
);
646 return RPC_S_PROTOCOL_ERROR
;
649 type
= READ_HTTP_PAYLOAD_FIELD_TYPE(data
);
650 if (type
!= 0x0000000d)
652 ERR("invalid type for field 1: 0x%08lx\n", type
);
653 return RPC_S_PROTOCOL_ERROR
;
655 if (*(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
) != (server
? 0x3 : 0x0))
657 ERR("invalid type for 0xd field data: 0x%08lx\n", *(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
));
658 return RPC_S_PROTOCOL_ERROR
;
660 data
= RPCRT4_NextHttpHeaderField(data
);
662 type
= READ_HTTP_PAYLOAD_FIELD_TYPE(data
);
663 if (type
!= 0x00000001)
665 ERR("invalid type for field 2: 0x%08lx\n", type
);
666 return RPC_S_PROTOCOL_ERROR
;
668 *bytes_transmitted
= *(ULONG
*)GET_HTTP_PAYLOAD_FIELD_DATA(data
);
669 *flow_control_increment
= *(ULONG
*)(GET_HTTP_PAYLOAD_FIELD_DATA(data
) + 4);
670 *pipe_uuid
= *(UUID
*)(GET_HTTP_PAYLOAD_FIELD_DATA(data
) + 8);
676 RPC_STATUS
RPCRT4_default_secure_packet(RpcConnection
*Connection
,
677 enum secure_packet_direction dir
,
678 RpcPktHdr
*hdr
, unsigned int hdr_size
,
679 unsigned char *stub_data
, unsigned int stub_data_size
,
680 RpcAuthVerifier
*auth_hdr
,
681 unsigned char *auth_value
, unsigned int auth_value_size
)
683 SecBufferDesc message
;
684 SecBuffer buffers
[4];
685 SECURITY_STATUS sec_status
;
687 message
.ulVersion
= SECBUFFER_VERSION
;
688 message
.cBuffers
= ARRAY_SIZE(buffers
);
689 message
.pBuffers
= buffers
;
691 buffers
[0].cbBuffer
= hdr_size
;
692 buffers
[0].BufferType
= SECBUFFER_DATA
|SECBUFFER_READONLY_WITH_CHECKSUM
;
693 buffers
[0].pvBuffer
= hdr
;
694 buffers
[1].cbBuffer
= stub_data_size
;
695 buffers
[1].BufferType
= SECBUFFER_DATA
;
696 buffers
[1].pvBuffer
= stub_data
;
697 buffers
[2].cbBuffer
= sizeof(*auth_hdr
);
698 buffers
[2].BufferType
= SECBUFFER_DATA
|SECBUFFER_READONLY_WITH_CHECKSUM
;
699 buffers
[2].pvBuffer
= auth_hdr
;
700 buffers
[3].cbBuffer
= auth_value_size
;
701 buffers
[3].BufferType
= SECBUFFER_TOKEN
;
702 buffers
[3].pvBuffer
= auth_value
;
704 if (dir
== SECURE_PACKET_SEND
)
706 if ((auth_hdr
->auth_level
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
) && packet_has_body(hdr
))
708 sec_status
= EncryptMessage(&Connection
->ctx
, 0, &message
, 0 /* FIXME */);
709 if (sec_status
!= SEC_E_OK
)
711 ERR("EncryptMessage failed with 0x%08lx\n", sec_status
);
712 return RPC_S_SEC_PKG_ERROR
;
715 else if (auth_hdr
->auth_level
!= RPC_C_AUTHN_LEVEL_NONE
)
717 sec_status
= MakeSignature(&Connection
->ctx
, 0, &message
, 0 /* FIXME */);
718 if (sec_status
!= SEC_E_OK
)
720 ERR("MakeSignature failed with 0x%08lx\n", sec_status
);
721 return RPC_S_SEC_PKG_ERROR
;
725 else if (dir
== SECURE_PACKET_RECEIVE
)
727 if ((auth_hdr
->auth_level
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
) && packet_has_body(hdr
))
729 sec_status
= DecryptMessage(&Connection
->ctx
, &message
, 0 /* FIXME */, 0);
730 if (sec_status
!= SEC_E_OK
)
732 ERR("DecryptMessage failed with 0x%08lx\n", sec_status
);
733 return RPC_S_SEC_PKG_ERROR
;
736 else if (auth_hdr
->auth_level
!= RPC_C_AUTHN_LEVEL_NONE
)
738 sec_status
= VerifySignature(&Connection
->ctx
, &message
, 0 /* FIXME */, NULL
);
739 if (sec_status
!= SEC_E_OK
)
741 ERR("VerifySignature failed with 0x%08lx\n", sec_status
);
742 return RPC_S_SEC_PKG_ERROR
;
750 /***********************************************************************
751 * RPCRT4_SendWithAuth (internal)
753 * Transmit a packet with authorization data over connection in acceptable fragments.
755 RPC_STATUS
RPCRT4_SendWithAuth(RpcConnection
*Connection
, RpcPktHdr
*Header
,
756 void *Buffer
, unsigned int BufferLength
,
757 const void *Auth
, unsigned int AuthLength
)
766 RPCRT4_SetThreadCurrentConnection(Connection
);
769 /* The packet building functions save the packet header size, so we can use it. */
770 hdr_size
= Header
->common
.frag_len
;
772 Header
->common
.auth_len
= AuthLength
;
773 else if (Connection
->AuthInfo
&& packet_has_auth_verifier(Header
))
775 if ((Connection
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
) && packet_has_body(Header
))
776 Header
->common
.auth_len
= Connection
->encryption_auth_len
;
778 Header
->common
.auth_len
= Connection
->signature_auth_len
;
781 Header
->common
.auth_len
= 0;
782 Header
->common
.flags
|= RPC_FLG_FIRST
;
783 Header
->common
.flags
&= ~RPC_FLG_LAST
;
785 alen
= RPC_AUTH_VERIFIER_LEN(&Header
->common
);
787 while (!(Header
->common
.flags
& RPC_FLG_LAST
)) {
788 unsigned char auth_pad_len
= Header
->common
.auth_len
? ROUND_UP_AMOUNT(BufferLength
, AUTH_ALIGNMENT
) : 0;
789 unsigned int pkt_size
= BufferLength
+ hdr_size
+ alen
+ auth_pad_len
;
791 /* decide if we need to split the packet into fragments */
792 if (pkt_size
<= Connection
->MaxTransmissionSize
) {
793 Header
->common
.flags
|= RPC_FLG_LAST
;
794 Header
->common
.frag_len
= pkt_size
;
797 /* make sure packet payload will be a multiple of 16 */
798 Header
->common
.frag_len
=
799 ((Connection
->MaxTransmissionSize
- hdr_size
- alen
) & ~(AUTH_ALIGNMENT
-1)) +
803 pkt
= calloc(1, Header
->common
.frag_len
);
805 memcpy(pkt
, Header
, hdr_size
);
807 /* fragment consisted of header only and is the last one */
808 if (hdr_size
== Header
->common
.frag_len
)
811 memcpy(pkt
+ hdr_size
, buffer_pos
, Header
->common
.frag_len
- hdr_size
- auth_pad_len
- alen
);
813 /* add the authorization info */
814 if (Header
->common
.auth_len
)
816 RpcAuthVerifier
*auth_hdr
= (RpcAuthVerifier
*)&pkt
[Header
->common
.frag_len
- alen
];
818 auth_hdr
->auth_type
= Connection
->AuthInfo
->AuthnSvc
;
819 auth_hdr
->auth_level
= Connection
->AuthInfo
->AuthnLevel
;
820 auth_hdr
->auth_pad_length
= auth_pad_len
;
821 auth_hdr
->auth_reserved
= 0;
822 /* a unique number... */
823 auth_hdr
->auth_context_id
= Connection
->auth_context_id
;
826 memcpy(auth_hdr
+ 1, Auth
, AuthLength
);
829 status
= rpcrt4_conn_secure_packet(Connection
, SECURE_PACKET_SEND
,
830 (RpcPktHdr
*)pkt
, hdr_size
,
831 pkt
+ hdr_size
, Header
->common
.frag_len
- hdr_size
- alen
,
833 (unsigned char *)(auth_hdr
+ 1), Header
->common
.auth_len
);
834 if (status
!= RPC_S_OK
)
837 RPCRT4_SetThreadCurrentConnection(NULL
);
844 count
= rpcrt4_conn_write(Connection
, pkt
, Header
->common
.frag_len
);
847 WARN("rpcrt4_conn_write failed (auth)\n");
848 RPCRT4_SetThreadCurrentConnection(NULL
);
849 return RPC_S_CALL_FAILED
;
852 buffer_pos
+= Header
->common
.frag_len
- hdr_size
- alen
- auth_pad_len
;
853 BufferLength
-= Header
->common
.frag_len
- hdr_size
- alen
- auth_pad_len
;
854 Header
->common
.flags
&= ~RPC_FLG_FIRST
;
857 RPCRT4_SetThreadCurrentConnection(NULL
);
861 /***********************************************************************
862 * RPCRT4_default_authorize (internal)
864 * Authorize a client connection.
866 RPC_STATUS
RPCRT4_default_authorize(RpcConnection
*conn
, BOOL first_time
,
867 unsigned char *in_buffer
,
868 unsigned int in_size
,
869 unsigned char *out_buffer
,
870 unsigned int *out_size
)
873 SecBufferDesc out_desc
;
874 SecBufferDesc inp_desc
;
875 SecPkgContext_Sizes secctx_sizes
;
876 BOOL continue_needed
;
882 *out_size
= conn
->AuthInfo
->cbMaxToken
;
886 in
.BufferType
= SECBUFFER_TOKEN
;
887 in
.pvBuffer
= in_buffer
;
888 in
.cbBuffer
= in_size
;
890 out
.BufferType
= SECBUFFER_TOKEN
;
891 out
.pvBuffer
= out_buffer
;
892 out
.cbBuffer
= *out_size
;
894 out_desc
.ulVersion
= 0;
895 out_desc
.cBuffers
= 1;
896 out_desc
.pBuffers
= &out
;
898 inp_desc
.ulVersion
= 0;
899 inp_desc
.cBuffers
= 1;
900 inp_desc
.pBuffers
= &in
;
904 context_req
= ASC_REQ_CONNECTION
| ASC_REQ_USE_DCE_STYLE
|
907 if (conn
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_INTEGRITY
)
908 context_req
|= ASC_REQ_INTEGRITY
;
909 else if (conn
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
)
910 context_req
|= ASC_REQ_CONFIDENTIALITY
| ASC_REQ_INTEGRITY
;
912 r
= AcceptSecurityContext(&conn
->AuthInfo
->cred
,
913 first_time
? NULL
: &conn
->ctx
,
914 &inp_desc
, context_req
, SECURITY_NETWORK_DREP
,
916 &out_desc
, &conn
->attr
, &conn
->exp
);
917 if (r
== SEC_E_OK
|| r
== SEC_I_COMPLETE_NEEDED
)
919 /* authorisation done, so nothing more to send */
925 context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
926 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
928 if (conn
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_INTEGRITY
)
929 context_req
|= ISC_REQ_INTEGRITY
;
930 else if (conn
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
)
931 context_req
|= ISC_REQ_CONFIDENTIALITY
| ISC_REQ_INTEGRITY
;
933 r
= InitializeSecurityContextW(&conn
->AuthInfo
->cred
,
934 first_time
? NULL
: &conn
->ctx
,
935 first_time
? conn
->AuthInfo
->server_principal_name
: NULL
,
936 context_req
, 0, SECURITY_NETWORK_DREP
,
937 first_time
? NULL
: &inp_desc
, 0, &conn
->ctx
,
938 &out_desc
, &conn
->attr
, &conn
->exp
);
942 WARN("InitializeSecurityContext failed with error 0x%08lx\n", r
);
946 TRACE("r = 0x%08lx, attr = 0x%08lx\n", r
, conn
->attr
);
947 continue_needed
= ((r
== SEC_I_CONTINUE_NEEDED
) ||
948 (r
== SEC_I_COMPLETE_AND_CONTINUE
));
950 if ((r
== SEC_I_COMPLETE_NEEDED
) || (r
== SEC_I_COMPLETE_AND_CONTINUE
))
952 TRACE("complete needed\n");
953 r
= CompleteAuthToken(&conn
->ctx
, &out_desc
);
956 WARN("CompleteAuthToken failed with error 0x%08lx\n", r
);
961 TRACE("cbBuffer = %ld\n", out
.cbBuffer
);
963 if (!continue_needed
)
965 r
= QueryContextAttributesA(&conn
->ctx
, SECPKG_ATTR_SIZES
, &secctx_sizes
);
968 WARN("QueryContextAttributes failed with error 0x%08lx\n", r
);
971 conn
->signature_auth_len
= secctx_sizes
.cbMaxSignature
;
972 conn
->encryption_auth_len
= secctx_sizes
.cbSecurityTrailer
;
975 *out_size
= out
.cbBuffer
;
980 return ERROR_ACCESS_DENIED
; /* FIXME: is this correct? */
983 /***********************************************************************
984 * RPCRT4_ClientConnectionAuth (internal)
986 RPC_STATUS
RPCRT4_ClientConnectionAuth(RpcConnection
* conn
, BYTE
*challenge
,
991 unsigned char *out_buffer
;
992 unsigned int out_len
= 0;
994 TRACE("challenge %s, %ld bytes\n", challenge
, count
);
996 status
= rpcrt4_conn_authorize(conn
, FALSE
, challenge
, count
, NULL
, &out_len
);
997 if (status
) return status
;
998 out_buffer
= malloc(out_len
);
999 if (!out_buffer
) return RPC_S_OUT_OF_RESOURCES
;
1000 status
= rpcrt4_conn_authorize(conn
, FALSE
, challenge
, count
, out_buffer
, &out_len
);
1001 if (status
) return status
;
1003 resp_hdr
= RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION
);
1006 status
= RPCRT4_SendWithAuth(conn
, resp_hdr
, NULL
, 0, out_buffer
, out_len
);
1008 status
= RPC_S_OUT_OF_RESOURCES
;
1016 /***********************************************************************
1017 * RPCRT4_ServerConnectionAuth (internal)
1019 RPC_STATUS
RPCRT4_ServerConnectionAuth(RpcConnection
* conn
,
1021 RpcAuthVerifier
*auth_data_in
,
1022 ULONG auth_length_in
,
1023 unsigned char **auth_data_out
,
1024 ULONG
*auth_length_out
)
1026 unsigned char *out_buffer
;
1027 unsigned int out_size
;
1032 /* remove any existing authentication information */
1035 RpcAuthInfo_Release(conn
->AuthInfo
);
1036 conn
->AuthInfo
= NULL
;
1038 if (SecIsValidHandle(&conn
->ctx
))
1040 DeleteSecurityContext(&conn
->ctx
);
1041 SecInvalidateHandle(&conn
->ctx
);
1043 if (auth_length_in
>= sizeof(RpcAuthVerifier
))
1049 status
= RPCRT4_ServerGetRegisteredAuthInfo(
1050 auth_data_in
->auth_type
, &cred
, &exp
, &max_token
);
1051 if (status
!= RPC_S_OK
)
1053 ERR("unknown authentication service %u\n", auth_data_in
->auth_type
);
1057 status
= RpcAuthInfo_Create(auth_data_in
->auth_level
,
1058 auth_data_in
->auth_type
, cred
, exp
,
1059 max_token
, NULL
, &conn
->AuthInfo
);
1060 if (status
!= RPC_S_OK
)
1062 FreeCredentialsHandle(&cred
);
1066 /* FIXME: should auth_data_in->auth_context_id be checked in the !start case? */
1067 conn
->auth_context_id
= auth_data_in
->auth_context_id
;
1071 if (auth_length_in
< sizeof(RpcAuthVerifier
))
1074 if (!conn
->AuthInfo
)
1075 /* should have filled in authentication info by now */
1076 return RPC_S_PROTOCOL_ERROR
;
1078 status
= rpcrt4_conn_authorize(
1079 conn
, start
, (unsigned char *)(auth_data_in
+ 1),
1080 auth_length_in
- sizeof(RpcAuthVerifier
), NULL
, &out_size
);
1081 if (status
) return status
;
1083 out_buffer
= malloc(out_size
);
1084 if (!out_buffer
) return RPC_S_OUT_OF_RESOURCES
;
1086 status
= rpcrt4_conn_authorize(
1087 conn
, start
, (unsigned char *)(auth_data_in
+ 1),
1088 auth_length_in
- sizeof(RpcAuthVerifier
), out_buffer
, &out_size
);
1089 if (status
!= RPC_S_OK
)
1095 if (out_size
&& !auth_length_out
)
1097 ERR("expected authentication to be complete but SSP returned data of "
1098 "%u bytes to be sent back to client\n", out_size
);
1100 return RPC_S_SEC_PKG_ERROR
;
1104 *auth_data_out
= out_buffer
;
1105 *auth_length_out
= out_size
;
1111 /***********************************************************************
1112 * RPCRT4_default_is_authorized (internal)
1114 * Has a connection started the process of authorizing with the server?
1116 BOOL
RPCRT4_default_is_authorized(RpcConnection
*Connection
)
1118 return Connection
->AuthInfo
&& SecIsValidHandle(&Connection
->ctx
);
1121 /***********************************************************************
1122 * RPCRT4_default_impersonate_client (internal)
1125 RPC_STATUS
RPCRT4_default_impersonate_client(RpcConnection
*conn
)
1127 SECURITY_STATUS sec_status
;
1129 TRACE("(%p)\n", conn
);
1131 if (!conn
->AuthInfo
|| !SecIsValidHandle(&conn
->ctx
))
1132 return RPC_S_NO_CONTEXT_AVAILABLE
;
1133 sec_status
= ImpersonateSecurityContext(&conn
->ctx
);
1134 if (sec_status
!= SEC_E_OK
)
1135 WARN("ImpersonateSecurityContext returned 0x%08lx\n", sec_status
);
1138 case SEC_E_UNSUPPORTED_FUNCTION
:
1139 return RPC_S_CANNOT_SUPPORT
;
1140 case SEC_E_NO_IMPERSONATION
:
1141 return RPC_S_NO_CONTEXT_AVAILABLE
;
1145 return RPC_S_SEC_PKG_ERROR
;
1149 /***********************************************************************
1150 * RPCRT4_default_revert_to_self (internal)
1153 RPC_STATUS
RPCRT4_default_revert_to_self(RpcConnection
*conn
)
1155 SECURITY_STATUS sec_status
;
1157 TRACE("(%p)\n", conn
);
1159 if (!conn
->AuthInfo
|| !SecIsValidHandle(&conn
->ctx
))
1160 return RPC_S_NO_CONTEXT_AVAILABLE
;
1161 sec_status
= RevertSecurityContext(&conn
->ctx
);
1162 if (sec_status
!= SEC_E_OK
)
1163 WARN("RevertSecurityContext returned 0x%08lx\n", sec_status
);
1166 case SEC_E_UNSUPPORTED_FUNCTION
:
1167 return RPC_S_CANNOT_SUPPORT
;
1168 case SEC_E_NO_IMPERSONATION
:
1169 return RPC_S_NO_CONTEXT_AVAILABLE
;
1173 return RPC_S_SEC_PKG_ERROR
;
1177 /***********************************************************************
1178 * RPCRT4_default_inquire_auth_client (internal)
1180 * Default function to retrieve the authentication details that the client
1181 * is using to call the server.
1183 RPC_STATUS
RPCRT4_default_inquire_auth_client(
1184 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
1185 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
1187 if (!conn
->AuthInfo
) return RPC_S_BINDING_HAS_NO_AUTH
;
1191 FIXME("privs not implemented\n");
1194 if (server_princ_name
)
1196 *server_princ_name
= wcsdup(conn
->AuthInfo
->server_principal_name
);
1197 if (!*server_princ_name
) return ERROR_OUTOFMEMORY
;
1199 if (authn_level
) *authn_level
= conn
->AuthInfo
->AuthnLevel
;
1200 if (authn_svc
) *authn_svc
= conn
->AuthInfo
->AuthnSvc
;
1203 FIXME("authorization service not implemented\n");
1204 *authz_svc
= RPC_C_AUTHZ_NONE
;
1207 FIXME("flags 0x%lx not implemented\n", flags
);
1212 /***********************************************************************
1213 * RPCRT4_Send (internal)
1215 * Transmit a packet over connection in acceptable fragments.
1217 RPC_STATUS
RPCRT4_Send(RpcConnection
*Connection
, RpcPktHdr
*Header
,
1218 void *Buffer
, unsigned int BufferLength
)
1222 if (packet_does_auth_negotiation(Header
) &&
1223 Connection
->AuthInfo
&&
1224 !rpcrt4_conn_is_authorized(Connection
))
1226 unsigned int out_size
= 0;
1227 unsigned char *out_buffer
;
1229 r
= rpcrt4_conn_authorize(Connection
, TRUE
, NULL
, 0, NULL
, &out_size
);
1230 if (r
!= RPC_S_OK
) return r
;
1232 out_buffer
= malloc(out_size
);
1233 if (!out_buffer
) return RPC_S_OUT_OF_RESOURCES
;
1235 /* tack on a negotiate packet */
1236 r
= rpcrt4_conn_authorize(Connection
, TRUE
, NULL
, 0, out_buffer
, &out_size
);
1238 r
= RPCRT4_SendWithAuth(Connection
, Header
, Buffer
, BufferLength
, out_buffer
, out_size
);
1243 r
= RPCRT4_SendWithAuth(Connection
, Header
, Buffer
, BufferLength
, NULL
, 0);
1248 /* validates version and frag_len fields */
1249 RPC_STATUS
RPCRT4_ValidateCommonHeader(const RpcPktCommonHdr
*hdr
)
1253 /* verify if the header really makes sense */
1254 if (hdr
->rpc_ver
!= RPC_VER_MAJOR
||
1255 hdr
->rpc_ver_minor
!= RPC_VER_MINOR
)
1257 WARN("unhandled packet version\n");
1258 return RPC_S_PROTOCOL_ERROR
;
1261 hdr_length
= RPCRT4_GetHeaderSize((const RpcPktHdr
*)hdr
);
1262 if (hdr_length
== 0)
1264 WARN("header length == 0\n");
1265 return RPC_S_PROTOCOL_ERROR
;
1268 if (hdr
->frag_len
< hdr_length
)
1270 WARN("bad frag length %d\n", hdr
->frag_len
);
1271 return RPC_S_PROTOCOL_ERROR
;
1277 /***********************************************************************
1278 * RPCRT4_default_receive_fragment (internal)
1280 * Receive a fragment from a connection.
1282 static RPC_STATUS
RPCRT4_default_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
1287 RpcPktCommonHdr common_hdr
;
1292 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
1294 /* read packet common header */
1295 dwRead
= rpcrt4_conn_read(Connection
, &common_hdr
, sizeof(common_hdr
));
1296 if (dwRead
!= sizeof(common_hdr
)) {
1297 WARN("Short read of header, %ld bytes\n", dwRead
);
1298 status
= RPC_S_CALL_FAILED
;
1302 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
1303 if (status
!= RPC_S_OK
) goto fail
;
1305 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
1306 if (hdr_length
== 0) {
1307 WARN("header length == 0\n");
1308 status
= RPC_S_PROTOCOL_ERROR
;
1312 *Header
= malloc(hdr_length
);
1313 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
1315 /* read the rest of packet header */
1316 dwRead
= rpcrt4_conn_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
1317 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
1318 WARN("bad header length, %ld bytes, hdr_length %ld\n", dwRead
, hdr_length
);
1319 status
= RPC_S_CALL_FAILED
;
1323 if (common_hdr
.frag_len
- hdr_length
)
1325 *Payload
= malloc(common_hdr
.frag_len
- hdr_length
);
1328 status
= RPC_S_OUT_OF_RESOURCES
;
1332 dwRead
= rpcrt4_conn_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
1333 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
1335 WARN("bad data length, %ld/%ld\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
1336 status
= RPC_S_CALL_FAILED
;
1347 if (status
!= RPC_S_OK
) {
1356 static RPC_STATUS
RPCRT4_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
1358 if (Connection
->ops
->receive_fragment
)
1359 return Connection
->ops
->receive_fragment(Connection
, Header
, Payload
);
1361 return RPCRT4_default_receive_fragment(Connection
, Header
, Payload
);
1364 /***********************************************************************
1365 * RPCRT4_ReceiveWithAuth (internal)
1367 * Receive a packet from connection, merge the fragments and return the auth
1370 RPC_STATUS
RPCRT4_ReceiveWithAuth(RpcConnection
*Connection
, RpcPktHdr
**Header
,
1372 unsigned char **auth_data_out
,
1373 ULONG
*auth_length_out
)
1377 unsigned short first_flag
;
1379 ULONG buffer_length
;
1380 ULONG auth_length
= 0;
1381 unsigned char *auth_data
= NULL
;
1382 RpcPktHdr
*CurrentHeader
= NULL
;
1383 void *payload
= NULL
;
1386 pMsg
->Buffer
= NULL
;
1387 if (auth_data_out
) *auth_data_out
= NULL
;
1388 if (auth_length_out
) *auth_length_out
= 0;
1390 TRACE("(%p, %p, %p, %p)\n", Connection
, Header
, pMsg
, auth_data_out
);
1392 RPCRT4_SetThreadCurrentConnection(Connection
);
1394 status
= RPCRT4_receive_fragment(Connection
, Header
, &payload
);
1395 if (status
!= RPC_S_OK
) goto fail
;
1397 hdr_length
= RPCRT4_GetHeaderSize(*Header
);
1399 /* read packet body */
1400 switch ((*Header
)->common
.ptype
) {
1402 pMsg
->BufferLength
= (*Header
)->response
.alloc_hint
;
1405 pMsg
->BufferLength
= (*Header
)->request
.alloc_hint
;
1408 pMsg
->BufferLength
= (*Header
)->common
.frag_len
- hdr_length
- RPC_AUTH_VERIFIER_LEN(&(*Header
)->common
);
1411 TRACE("buffer length = %u\n", pMsg
->BufferLength
);
1413 pMsg
->Buffer
= I_RpcAllocate(pMsg
->BufferLength
);
1416 status
= ERROR_OUTOFMEMORY
;
1420 first_flag
= RPC_FLG_FIRST
;
1421 auth_length
= (*Header
)->common
.auth_len
;
1423 auth_data
= malloc(RPC_AUTH_VERIFIER_LEN(&(*Header
)->common
));
1425 status
= RPC_S_OUT_OF_RESOURCES
;
1429 CurrentHeader
= *Header
;
1433 unsigned int header_auth_len
= RPC_AUTH_VERIFIER_LEN(&CurrentHeader
->common
);
1435 /* verify header fields */
1437 if ((CurrentHeader
->common
.frag_len
< hdr_length
) ||
1438 (CurrentHeader
->common
.frag_len
- hdr_length
< header_auth_len
)) {
1439 WARN("frag_len %d too small for hdr_length %ld and auth_len %d\n",
1440 CurrentHeader
->common
.frag_len
, hdr_length
, CurrentHeader
->common
.auth_len
);
1441 status
= RPC_S_PROTOCOL_ERROR
;
1445 if (CurrentHeader
->common
.auth_len
!= auth_length
) {
1446 WARN("auth_len header field changed from %ld to %d\n",
1447 auth_length
, CurrentHeader
->common
.auth_len
);
1448 status
= RPC_S_PROTOCOL_ERROR
;
1452 if ((CurrentHeader
->common
.flags
& RPC_FLG_FIRST
) != first_flag
) {
1453 TRACE("invalid packet flags\n");
1454 status
= RPC_S_PROTOCOL_ERROR
;
1458 data_length
= CurrentHeader
->common
.frag_len
- hdr_length
- header_auth_len
;
1459 if (data_length
+ buffer_length
> pMsg
->BufferLength
) {
1460 TRACE("allocation hint exceeded, new buffer length = %ld\n",
1461 data_length
+ buffer_length
);
1462 pMsg
->BufferLength
= data_length
+ buffer_length
;
1463 status
= I_RpcReAllocateBuffer(pMsg
);
1464 if (status
!= RPC_S_OK
) goto fail
;
1467 memcpy((unsigned char *)pMsg
->Buffer
+ buffer_length
, payload
, data_length
);
1469 if (header_auth_len
) {
1470 if (header_auth_len
< sizeof(RpcAuthVerifier
) ||
1471 header_auth_len
> RPC_AUTH_VERIFIER_LEN(&(*Header
)->common
)) {
1472 WARN("bad auth verifier length %d\n", header_auth_len
);
1473 status
= RPC_S_PROTOCOL_ERROR
;
1477 /* FIXME: we should accumulate authentication data for the bind,
1478 * bind_ack, alter_context and alter_context_response if necessary.
1479 * however, the details of how this is done is very sketchy in the
1480 * DCE/RPC spec. for all other packet types that have authentication
1481 * verifier data then it is just duplicated in all the fragments */
1482 memcpy(auth_data
, (unsigned char *)payload
+ data_length
, header_auth_len
);
1484 /* these packets are handled specially, not by the generic SecurePacket
1486 if (!packet_does_auth_negotiation(*Header
) && rpcrt4_conn_is_authorized(Connection
))
1488 status
= rpcrt4_conn_secure_packet(Connection
, SECURE_PACKET_RECEIVE
,
1489 CurrentHeader
, hdr_length
,
1490 (unsigned char *)pMsg
->Buffer
+ buffer_length
, data_length
,
1491 (RpcAuthVerifier
*)auth_data
,
1492 auth_data
+ sizeof(RpcAuthVerifier
),
1493 header_auth_len
- sizeof(RpcAuthVerifier
));
1494 if (status
!= RPC_S_OK
) goto fail
;
1498 buffer_length
+= data_length
;
1499 if (!(CurrentHeader
->common
.flags
& RPC_FLG_LAST
)) {
1500 TRACE("next header\n");
1502 if (*Header
!= CurrentHeader
)
1504 free(CurrentHeader
);
1505 CurrentHeader
= NULL
;
1510 status
= RPCRT4_receive_fragment(Connection
, &CurrentHeader
, &payload
);
1511 if (status
!= RPC_S_OK
) goto fail
;
1518 pMsg
->BufferLength
= buffer_length
;
1524 RPCRT4_SetThreadCurrentConnection(NULL
);
1525 if (CurrentHeader
!= *Header
)
1526 free(CurrentHeader
);
1527 if (status
!= RPC_S_OK
) {
1528 I_RpcFree(pMsg
->Buffer
);
1529 pMsg
->Buffer
= NULL
;
1533 if (auth_data_out
&& status
== RPC_S_OK
) {
1534 *auth_length_out
= auth_length
;
1535 *auth_data_out
= auth_data
;
1543 /***********************************************************************
1544 * RPCRT4_Receive (internal)
1546 * Receive a packet from connection and merge the fragments.
1548 static RPC_STATUS
RPCRT4_Receive(RpcConnection
*Connection
, RpcPktHdr
**Header
,
1551 return RPCRT4_ReceiveWithAuth(Connection
, Header
, pMsg
, NULL
, NULL
);
1554 /***********************************************************************
1555 * I_RpcNegotiateTransferSyntax [RPCRT4.@]
1557 * Negotiates the transfer syntax used by a client connection by connecting
1561 * pMsg [I] RPC Message structure.
1562 * pAsync [I] Asynchronous state to set.
1565 * Success: RPC_S_OK.
1566 * Failure: Any error code.
1568 RPC_STATUS WINAPI
I_RpcNegotiateTransferSyntax(PRPC_MESSAGE pMsg
)
1570 RpcBinding
* bind
= pMsg
->Handle
;
1571 RpcConnection
* conn
;
1572 RPC_STATUS status
= RPC_S_OK
;
1574 TRACE("(%p)\n", pMsg
);
1576 if (!bind
|| bind
->server
)
1578 ERR("no binding\n");
1579 return RPC_S_INVALID_BINDING
;
1582 /* if we already have a connection, we don't need to negotiate again */
1583 if (!pMsg
->ReservedForRuntime
)
1585 RPC_CLIENT_INTERFACE
*cif
= pMsg
->RpcInterfaceInformation
;
1586 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
;
1588 if (!bind
->Endpoint
|| !bind
->Endpoint
[0])
1590 TRACE("automatically resolving partially bound binding\n");
1591 status
= RpcEpResolveBinding(bind
, cif
);
1592 if (status
!= RPC_S_OK
) return status
;
1595 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
1596 &cif
->InterfaceId
, NULL
);
1598 if (status
== RPC_S_OK
)
1600 pMsg
->ReservedForRuntime
= conn
;
1601 RPCRT4_AddRefBinding(bind
);
1608 /***********************************************************************
1609 * I_RpcGetBuffer [RPCRT4.@]
1611 * Allocates a buffer for use by I_RpcSend or I_RpcSendReceive and binds to the
1615 * pMsg [I/O] RPC message information.
1618 * Success: RPC_S_OK.
1619 * Failure: RPC_S_INVALID_BINDING if pMsg->Handle is invalid.
1620 * RPC_S_SERVER_UNAVAILABLE if unable to connect to server.
1621 * ERROR_OUTOFMEMORY if buffer allocation failed.
1624 * The pMsg->BufferLength field determines the size of the buffer to allocate,
1627 * Use I_RpcFreeBuffer() to unbind from the server and free the message buffer.
1630 * I_RpcFreeBuffer(), I_RpcSend(), I_RpcReceive(), I_RpcSendReceive().
1632 RPC_STATUS WINAPI
I_RpcGetBuffer(PRPC_MESSAGE pMsg
)
1635 RpcBinding
* bind
= pMsg
->Handle
;
1637 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
1641 ERR("no binding\n");
1642 return RPC_S_INVALID_BINDING
;
1645 pMsg
->Buffer
= I_RpcAllocate(pMsg
->BufferLength
);
1646 TRACE("Buffer=%p\n", pMsg
->Buffer
);
1649 return ERROR_OUTOFMEMORY
;
1653 status
= I_RpcNegotiateTransferSyntax(pMsg
);
1654 if (status
!= RPC_S_OK
)
1655 I_RpcFree(pMsg
->Buffer
);
1663 /***********************************************************************
1664 * I_RpcReAllocateBuffer (internal)
1666 static RPC_STATUS
I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg
)
1668 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
1669 pMsg
->Buffer
= realloc(pMsg
->Buffer
, pMsg
->BufferLength
);
1671 TRACE("Buffer=%p\n", pMsg
->Buffer
);
1672 return pMsg
->Buffer
? RPC_S_OK
: ERROR_OUTOFMEMORY
;
1675 /***********************************************************************
1676 * I_RpcFreeBuffer [RPCRT4.@]
1678 * Frees a buffer allocated by I_RpcGetBuffer or I_RpcReceive and unbinds from
1679 * the server interface.
1682 * pMsg [I/O] RPC message information.
1688 * I_RpcGetBuffer(), I_RpcReceive().
1690 RPC_STATUS WINAPI
I_RpcFreeBuffer(PRPC_MESSAGE pMsg
)
1692 RpcBinding
* bind
= pMsg
->Handle
;
1694 TRACE("(%p) Buffer=%p\n", pMsg
, pMsg
->Buffer
);
1698 ERR("no binding\n");
1699 return RPC_S_INVALID_BINDING
;
1702 if (pMsg
->ReservedForRuntime
)
1704 RpcConnection
*conn
= pMsg
->ReservedForRuntime
;
1705 RPCRT4_CloseBinding(bind
, conn
);
1706 RPCRT4_ReleaseBinding(bind
);
1707 pMsg
->ReservedForRuntime
= NULL
;
1709 I_RpcFree(pMsg
->Buffer
);
1713 static void CALLBACK
async_apc_notifier_proc(ULONG_PTR ulParam
)
1715 RPC_ASYNC_STATE
*state
= (RPC_ASYNC_STATE
*)ulParam
;
1716 state
->u
.APC
.NotificationRoutine(state
, NULL
, state
->Event
);
1719 static DWORD WINAPI
async_notifier_proc(LPVOID p
)
1721 RpcConnection
*conn
= p
;
1722 RPC_ASYNC_STATE
*state
= conn
->async_state
;
1724 if (state
&& conn
->ops
->wait_for_incoming_data(conn
) != -1)
1726 state
->Event
= RpcCallComplete
;
1727 switch (state
->NotificationType
)
1729 case RpcNotificationTypeEvent
:
1730 TRACE("RpcNotificationTypeEvent %p\n", state
->u
.hEvent
);
1731 SetEvent(state
->u
.hEvent
);
1733 case RpcNotificationTypeApc
:
1734 TRACE("RpcNotificationTypeApc %p\n", state
->u
.APC
.hThread
);
1735 QueueUserAPC(async_apc_notifier_proc
, state
->u
.APC
.hThread
, (ULONG_PTR
)state
);
1737 case RpcNotificationTypeIoc
:
1738 TRACE("RpcNotificationTypeIoc %p, 0x%lx, 0x%Ix, %p\n",
1739 state
->u
.IOC
.hIOPort
, state
->u
.IOC
.dwNumberOfBytesTransferred
,
1740 state
->u
.IOC
.dwCompletionKey
, state
->u
.IOC
.lpOverlapped
);
1741 PostQueuedCompletionStatus(state
->u
.IOC
.hIOPort
,
1742 state
->u
.IOC
.dwNumberOfBytesTransferred
,
1743 state
->u
.IOC
.dwCompletionKey
,
1744 state
->u
.IOC
.lpOverlapped
);
1746 case RpcNotificationTypeHwnd
:
1747 TRACE("RpcNotificationTypeHwnd %p 0x%x\n", state
->u
.HWND
.hWnd
,
1749 PostMessageW(state
->u
.HWND
.hWnd
, state
->u
.HWND
.Msg
, 0, 0);
1751 case RpcNotificationTypeCallback
:
1752 TRACE("RpcNotificationTypeCallback %p\n", state
->u
.NotificationRoutine
);
1753 state
->u
.NotificationRoutine(state
, NULL
, state
->Event
);
1755 case RpcNotificationTypeNone
:
1756 TRACE("RpcNotificationTypeNone\n");
1759 FIXME("unknown NotificationType: %d/0x%x\n", state
->NotificationType
, state
->NotificationType
);
1767 /***********************************************************************
1768 * I_RpcSend [RPCRT4.@]
1770 * Sends a message to the server.
1773 * pMsg [I/O] RPC message information.
1779 * The buffer must have been allocated with I_RpcGetBuffer().
1782 * I_RpcGetBuffer(), I_RpcReceive(), I_RpcSendReceive().
1784 RPC_STATUS WINAPI
I_RpcSend(PRPC_MESSAGE pMsg
)
1786 RpcBinding
* bind
= pMsg
->Handle
;
1787 RPC_CLIENT_INTERFACE
*cif
;
1788 RpcConnection
* conn
;
1791 BOOL from_cache
= TRUE
;
1793 TRACE("(%p)\n", pMsg
);
1794 if (!bind
|| bind
->server
|| !pMsg
->ReservedForRuntime
) return RPC_S_INVALID_BINDING
;
1798 conn
= pMsg
->ReservedForRuntime
;
1799 hdr
= RPCRT4_BuildRequestHeader(pMsg
->DataRepresentation
,
1801 pMsg
->ProcNum
& ~RPC_FLAGS_VALID_BIT
,
1804 return ERROR_OUTOFMEMORY
;
1806 hdr
->common
.call_id
= conn
->NextCallId
++;
1807 status
= RPCRT4_Send(conn
, hdr
, pMsg
->Buffer
, pMsg
->BufferLength
);
1809 if (status
== RPC_S_OK
|| conn
->server
|| !from_cache
)
1812 WARN("Send failed, trying to reconnect\n");
1813 cif
= pMsg
->RpcInterfaceInformation
;
1814 RPCRT4_ReleaseConnection(conn
);
1815 pMsg
->ReservedForRuntime
= NULL
;
1816 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
, &cif
->InterfaceId
, &from_cache
);
1817 if (status
!= RPC_S_OK
) break;
1818 pMsg
->ReservedForRuntime
= conn
;
1821 if (status
== RPC_S_OK
&& pMsg
->RpcFlags
& RPC_BUFFER_ASYNC
)
1823 if (!QueueUserWorkItem(async_notifier_proc
, conn
, WT_EXECUTEDEFAULT
| WT_EXECUTELONGFUNCTION
))
1824 status
= RPC_S_OUT_OF_RESOURCES
;
1830 /* is this status something that the server can't recover from? */
1831 static inline BOOL
is_hard_error(RPC_STATUS status
)
1835 case 0: /* user-defined fault */
1836 case ERROR_ACCESS_DENIED
:
1837 case ERROR_INVALID_PARAMETER
:
1838 case RPC_S_PROTOCOL_ERROR
:
1839 case RPC_S_CALL_FAILED
:
1840 case RPC_S_CALL_FAILED_DNE
:
1841 case RPC_S_SEC_PKG_ERROR
:
1848 /***********************************************************************
1849 * I_RpcReceive [RPCRT4.@]
1851 RPC_STATUS WINAPI
I_RpcReceive(PRPC_MESSAGE pMsg
)
1853 RpcBinding
* bind
= pMsg
->Handle
;
1855 RpcPktHdr
*hdr
= NULL
;
1856 RpcConnection
*conn
;
1858 TRACE("(%p)\n", pMsg
);
1859 if (!bind
|| bind
->server
|| !pMsg
->ReservedForRuntime
) return RPC_S_INVALID_BINDING
;
1861 conn
= pMsg
->ReservedForRuntime
;
1862 status
= RPCRT4_Receive(conn
, &hdr
, pMsg
);
1863 if (status
!= RPC_S_OK
) {
1864 WARN("receive failed with error %lx\n", status
);
1868 switch (hdr
->common
.ptype
) {
1872 ERR ("we got fault packet with status 0x%x\n", hdr
->fault
.status
);
1873 status
= NCA2RPC_STATUS(hdr
->fault
.status
);
1874 if (is_hard_error(status
))
1878 WARN("bad packet type %d\n", hdr
->common
.ptype
);
1879 status
= RPC_S_PROTOCOL_ERROR
;
1889 RPCRT4_ReleaseConnection(conn
);
1890 pMsg
->ReservedForRuntime
= NULL
;
1894 /***********************************************************************
1895 * I_RpcSendReceive [RPCRT4.@]
1897 * Sends a message to the server and receives the response.
1900 * pMsg [I/O] RPC message information.
1903 * Success: RPC_S_OK.
1904 * Failure: Any error code.
1907 * The buffer must have been allocated with I_RpcGetBuffer().
1910 * I_RpcGetBuffer(), I_RpcSend(), I_RpcReceive().
1912 RPC_STATUS WINAPI
I_RpcSendReceive(PRPC_MESSAGE pMsg
)
1915 void *original_buffer
;
1917 TRACE("(%p)\n", pMsg
);
1919 original_buffer
= pMsg
->Buffer
;
1920 status
= I_RpcSend(pMsg
);
1921 if (status
== RPC_S_OK
)
1922 status
= I_RpcReceive(pMsg
);
1923 /* free the buffer replaced by a new buffer in I_RpcReceive */
1924 if (status
== RPC_S_OK
)
1925 I_RpcFree(original_buffer
);
1929 /***********************************************************************
1930 * I_RpcAsyncSetHandle [RPCRT4.@]
1932 * Sets the asynchronous state of the handle contained in the RPC message
1936 * pMsg [I] RPC Message structure.
1937 * pAsync [I] Asynchronous state to set.
1940 * Success: RPC_S_OK.
1941 * Failure: Any error code.
1943 RPC_STATUS WINAPI
I_RpcAsyncSetHandle(PRPC_MESSAGE pMsg
, PRPC_ASYNC_STATE pAsync
)
1945 RpcBinding
* bind
= pMsg
->Handle
;
1946 RpcConnection
*conn
;
1948 TRACE("(%p, %p)\n", pMsg
, pAsync
);
1950 if (!bind
|| bind
->server
|| !pMsg
->ReservedForRuntime
) return RPC_S_INVALID_BINDING
;
1952 conn
= pMsg
->ReservedForRuntime
;
1953 conn
->async_state
= pAsync
;
1958 /***********************************************************************
1959 * I_RpcAsyncAbortCall [RPCRT4.@]
1961 * Aborts an asynchronous call.
1964 * pAsync [I] Asynchronous state.
1965 * ExceptionCode [I] Exception code.
1968 * Success: RPC_S_OK.
1969 * Failure: Any error code.
1971 RPC_STATUS WINAPI
I_RpcAsyncAbortCall(PRPC_ASYNC_STATE pAsync
, ULONG ExceptionCode
)
1973 FIXME("(%p, %ld): stub\n", pAsync
, ExceptionCode
);
1974 return RPC_S_INVALID_ASYNC_HANDLE
;