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
35 #include "wine/debug.h"
37 #include "rpc_binding.h"
39 #include "rpc_message.h"
40 #include "ncastatus.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
44 /* note: the DCE/RPC spec says the alignment amount should be 4, but
45 * MS/RPC servers seem to always use 16 */
46 #define AUTH_ALIGNMENT 16
48 /* gets the amount needed to round a value up to the specified alignment */
49 #define ROUND_UP_AMOUNT(value, alignment) \
50 (((alignment) - (((value) % (alignment)))) % (alignment))
51 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
53 enum secure_packet_direction
59 static RPC_STATUS
I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg
);
61 static DWORD
RPCRT4_GetHeaderSize(const RpcPktHdr
*Header
)
63 static const DWORD header_sizes
[] = {
64 sizeof(Header
->request
), 0, sizeof(Header
->response
),
65 sizeof(Header
->fault
), 0, 0, 0, 0, 0, 0, 0, sizeof(Header
->bind
),
66 sizeof(Header
->bind_ack
), sizeof(Header
->bind_nack
),
71 if (Header
->common
.ptype
< sizeof(header_sizes
) / sizeof(header_sizes
[0])) {
72 ret
= header_sizes
[Header
->common
.ptype
];
74 FIXME("unhandled packet type\n");
75 if (Header
->common
.flags
& RPC_FLG_OBJECT_UUID
)
78 TRACE("invalid packet type\n");
84 static int packet_has_body(const RpcPktHdr
*Header
)
86 return (Header
->common
.ptype
== PKT_FAULT
) ||
87 (Header
->common
.ptype
== PKT_REQUEST
) ||
88 (Header
->common
.ptype
== PKT_RESPONSE
);
91 static int packet_has_auth_verifier(const RpcPktHdr
*Header
)
93 return !(Header
->common
.ptype
== PKT_BIND_NACK
) &&
94 !(Header
->common
.ptype
== PKT_SHUTDOWN
);
97 static VOID
RPCRT4_BuildCommonHeader(RpcPktHdr
*Header
, unsigned char PacketType
,
98 unsigned long DataRepresentation
)
100 Header
->common
.rpc_ver
= RPC_VER_MAJOR
;
101 Header
->common
.rpc_ver_minor
= RPC_VER_MINOR
;
102 Header
->common
.ptype
= PacketType
;
103 Header
->common
.drep
[0] = LOBYTE(LOWORD(DataRepresentation
));
104 Header
->common
.drep
[1] = HIBYTE(LOWORD(DataRepresentation
));
105 Header
->common
.drep
[2] = LOBYTE(HIWORD(DataRepresentation
));
106 Header
->common
.drep
[3] = HIBYTE(HIWORD(DataRepresentation
));
107 Header
->common
.auth_len
= 0;
108 Header
->common
.call_id
= 1;
109 Header
->common
.flags
= 0;
110 /* Flags and fragment length are computed in RPCRT4_Send. */
113 static RpcPktHdr
*RPCRT4_BuildRequestHeader(unsigned long DataRepresentation
,
114 unsigned long BufferLength
,
115 unsigned short ProcNum
,
122 has_object
= (ObjectUuid
!= NULL
&& !UuidIsNil(ObjectUuid
, &status
));
123 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
124 sizeof(header
->request
) + (has_object
? sizeof(UUID
) : 0));
125 if (header
== NULL
) {
129 RPCRT4_BuildCommonHeader(header
, PKT_REQUEST
, DataRepresentation
);
130 header
->common
.frag_len
= sizeof(header
->request
);
131 header
->request
.alloc_hint
= BufferLength
;
132 header
->request
.context_id
= 0;
133 header
->request
.opnum
= ProcNum
;
135 header
->common
.flags
|= RPC_FLG_OBJECT_UUID
;
136 header
->common
.frag_len
+= sizeof(UUID
);
137 memcpy(&header
->request
+ 1, ObjectUuid
, sizeof(UUID
));
143 RpcPktHdr
*RPCRT4_BuildResponseHeader(unsigned long DataRepresentation
,
144 unsigned long BufferLength
)
148 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->response
));
149 if (header
== NULL
) {
153 RPCRT4_BuildCommonHeader(header
, PKT_RESPONSE
, DataRepresentation
);
154 header
->common
.frag_len
= sizeof(header
->response
);
155 header
->response
.alloc_hint
= BufferLength
;
160 RpcPktHdr
*RPCRT4_BuildFaultHeader(unsigned long DataRepresentation
,
165 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->fault
));
166 if (header
== NULL
) {
170 RPCRT4_BuildCommonHeader(header
, PKT_FAULT
, DataRepresentation
);
171 header
->common
.frag_len
= sizeof(header
->fault
);
172 header
->fault
.status
= Status
;
177 RpcPktHdr
*RPCRT4_BuildBindHeader(unsigned long DataRepresentation
,
178 unsigned short MaxTransmissionSize
,
179 unsigned short MaxReceiveSize
,
180 unsigned long AssocGroupId
,
181 const RPC_SYNTAX_IDENTIFIER
*AbstractId
,
182 const RPC_SYNTAX_IDENTIFIER
*TransferId
)
186 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->bind
));
187 if (header
== NULL
) {
191 RPCRT4_BuildCommonHeader(header
, PKT_BIND
, DataRepresentation
);
192 header
->common
.frag_len
= sizeof(header
->bind
);
193 header
->bind
.max_tsize
= MaxTransmissionSize
;
194 header
->bind
.max_rsize
= MaxReceiveSize
;
195 header
->bind
.assoc_gid
= AssocGroupId
;
196 header
->bind
.num_elements
= 1;
197 header
->bind
.num_syntaxes
= 1;
198 memcpy(&header
->bind
.abstract
, AbstractId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
199 memcpy(&header
->bind
.transfer
, TransferId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
204 static RpcPktHdr
*RPCRT4_BuildAuthHeader(unsigned long DataRepresentation
)
208 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
209 sizeof(header
->common
) + 12);
213 RPCRT4_BuildCommonHeader(header
, PKT_AUTH3
, DataRepresentation
);
214 header
->common
.frag_len
= 0x14;
215 header
->common
.auth_len
= 0;
220 RpcPktHdr
*RPCRT4_BuildBindNackHeader(unsigned long DataRepresentation
,
221 unsigned char RpcVersion
,
222 unsigned char RpcVersionMinor
)
226 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(header
->bind_nack
));
227 if (header
== NULL
) {
231 RPCRT4_BuildCommonHeader(header
, PKT_BIND_NACK
, DataRepresentation
);
232 header
->common
.frag_len
= sizeof(header
->bind_nack
);
233 header
->bind_nack
.protocols_count
= 1;
234 header
->bind_nack
.protocols
[0].rpc_ver
= RpcVersion
;
235 header
->bind_nack
.protocols
[0].rpc_ver_minor
= RpcVersionMinor
;
240 RpcPktHdr
*RPCRT4_BuildBindAckHeader(unsigned long DataRepresentation
,
241 unsigned short MaxTransmissionSize
,
242 unsigned short MaxReceiveSize
,
243 LPCSTR ServerAddress
,
244 unsigned long Result
,
245 unsigned long Reason
,
246 const RPC_SYNTAX_IDENTIFIER
*TransferId
)
249 unsigned long header_size
;
250 RpcAddressString
*server_address
;
252 RPC_SYNTAX_IDENTIFIER
*transfer_id
;
254 header_size
= sizeof(header
->bind_ack
) +
255 ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[strlen(ServerAddress
) + 1]), 4) +
257 sizeof(RPC_SYNTAX_IDENTIFIER
);
259 header
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, header_size
);
260 if (header
== NULL
) {
264 RPCRT4_BuildCommonHeader(header
, PKT_BIND_ACK
, DataRepresentation
);
265 header
->common
.frag_len
= header_size
;
266 header
->bind_ack
.max_tsize
= MaxTransmissionSize
;
267 header
->bind_ack
.max_rsize
= MaxReceiveSize
;
268 server_address
= (RpcAddressString
*)(&header
->bind_ack
+ 1);
269 server_address
->length
= strlen(ServerAddress
) + 1;
270 strcpy(server_address
->string
, ServerAddress
);
271 /* results is 4-byte aligned */
272 results
= (RpcResults
*)((ULONG_PTR
)server_address
+ ROUND_UP(FIELD_OFFSET(RpcAddressString
, string
[server_address
->length
]), 4));
273 results
->num_results
= 1;
274 results
->results
[0].result
= Result
;
275 results
->results
[0].reason
= Reason
;
276 transfer_id
= (RPC_SYNTAX_IDENTIFIER
*)(results
+ 1);
277 memcpy(transfer_id
, TransferId
, sizeof(RPC_SYNTAX_IDENTIFIER
));
282 VOID
RPCRT4_FreeHeader(RpcPktHdr
*Header
)
284 HeapFree(GetProcessHeap(), 0, Header
);
287 NCA_STATUS
RPC2NCA_STATUS(RPC_STATUS status
)
291 case ERROR_INVALID_HANDLE
: return NCA_S_FAULT_CONTEXT_MISMATCH
;
292 case ERROR_OUTOFMEMORY
: return NCA_S_FAULT_REMOTE_NO_MEMORY
;
293 case RPC_S_NOT_LISTENING
: return NCA_S_SERVER_TOO_BUSY
;
294 case RPC_S_UNKNOWN_IF
: return NCA_S_UNK_IF
;
295 case RPC_S_SERVER_TOO_BUSY
: return NCA_S_SERVER_TOO_BUSY
;
296 case RPC_S_CALL_FAILED
: return NCA_S_FAULT_UNSPEC
;
297 case RPC_S_CALL_FAILED_DNE
: return NCA_S_MANAGER_NOT_ENTERED
;
298 case RPC_S_PROTOCOL_ERROR
: return NCA_S_PROTO_ERROR
;
299 case RPC_S_UNSUPPORTED_TYPE
: return NCA_S_UNSUPPORTED_TYPE
;
300 case RPC_S_INVALID_TAG
: return NCA_S_FAULT_INVALID_TAG
;
301 case RPC_S_INVALID_BOUND
: return NCA_S_FAULT_INVALID_BOUND
;
302 case RPC_S_PROCNUM_OUT_OF_RANGE
: return NCA_S_OP_RNG_ERROR
;
303 case RPC_X_SS_HANDLES_MISMATCH
: return NCA_S_FAULT_CONTEXT_MISMATCH
;
304 case RPC_S_CALL_CANCELLED
: return NCA_S_FAULT_CANCEL
;
305 case RPC_S_COMM_FAILURE
: return NCA_S_COMM_FAILURE
;
306 case RPC_X_WRONG_PIPE_ORDER
: return NCA_S_FAULT_PIPE_ORDER
;
307 case RPC_X_PIPE_CLOSED
: return NCA_S_FAULT_PIPE_CLOSED
;
308 case RPC_X_PIPE_DISCIPLINE_ERROR
: return NCA_S_FAULT_PIPE_DISCIPLINE
;
309 case RPC_X_PIPE_EMPTY
: return NCA_S_FAULT_PIPE_EMPTY
;
310 case STATUS_FLOAT_DIVIDE_BY_ZERO
: return NCA_S_FAULT_FP_DIV_ZERO
;
311 case STATUS_FLOAT_INVALID_OPERATION
: return NCA_S_FAULT_FP_ERROR
;
312 case STATUS_FLOAT_OVERFLOW
: return NCA_S_FAULT_FP_OVERFLOW
;
313 case STATUS_FLOAT_UNDERFLOW
: return NCA_S_FAULT_FP_UNDERFLOW
;
314 case STATUS_INTEGER_DIVIDE_BY_ZERO
: return NCA_S_FAULT_INT_DIV_BY_ZERO
;
315 case STATUS_INTEGER_OVERFLOW
: return NCA_S_FAULT_INT_OVERFLOW
;
316 default: return status
;
320 RPC_STATUS
NCA2RPC_STATUS(NCA_STATUS status
)
324 case NCA_S_COMM_FAILURE
: return RPC_S_COMM_FAILURE
;
325 case NCA_S_OP_RNG_ERROR
: return RPC_S_PROCNUM_OUT_OF_RANGE
;
326 case NCA_S_UNK_IF
: return RPC_S_UNKNOWN_IF
;
327 case NCA_S_YOU_CRASHED
: return RPC_S_CALL_FAILED
;
328 case NCA_S_PROTO_ERROR
: return RPC_S_PROTOCOL_ERROR
;
329 case NCA_S_OUT_ARGS_TOO_BIG
: return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
330 case NCA_S_SERVER_TOO_BUSY
: return RPC_S_SERVER_TOO_BUSY
;
331 case NCA_S_UNSUPPORTED_TYPE
: return RPC_S_UNSUPPORTED_TYPE
;
332 case NCA_S_FAULT_INT_DIV_BY_ZERO
: return RPC_S_ZERO_DIVIDE
;
333 case NCA_S_FAULT_ADDR_ERROR
: return RPC_S_ADDRESS_ERROR
;
334 case NCA_S_FAULT_FP_DIV_ZERO
: return RPC_S_FP_DIV_ZERO
;
335 case NCA_S_FAULT_FP_UNDERFLOW
: return RPC_S_FP_UNDERFLOW
;
336 case NCA_S_FAULT_FP_OVERFLOW
: return RPC_S_FP_OVERFLOW
;
337 case NCA_S_FAULT_INVALID_TAG
: return RPC_S_INVALID_TAG
;
338 case NCA_S_FAULT_INVALID_BOUND
: return RPC_S_INVALID_BOUND
;
339 case NCA_S_RPC_VERSION_MISMATCH
: return RPC_S_PROTOCOL_ERROR
;
340 case NCA_S_UNSPEC_REJECT
: return RPC_S_CALL_FAILED_DNE
;
341 case NCA_S_BAD_ACTID
: return RPC_S_CALL_FAILED_DNE
;
342 case NCA_S_WHO_ARE_YOU_FAILED
: return RPC_S_CALL_FAILED
;
343 case NCA_S_MANAGER_NOT_ENTERED
: return RPC_S_CALL_FAILED_DNE
;
344 case NCA_S_FAULT_CANCEL
: return RPC_S_CALL_CANCELLED
;
345 case NCA_S_FAULT_ILL_INST
: return RPC_S_ADDRESS_ERROR
;
346 case NCA_S_FAULT_FP_ERROR
: return RPC_S_FP_OVERFLOW
;
347 case NCA_S_FAULT_INT_OVERFLOW
: return RPC_S_ADDRESS_ERROR
;
348 case NCA_S_FAULT_UNSPEC
: return RPC_S_CALL_FAILED
;
349 case NCA_S_FAULT_PIPE_EMPTY
: return RPC_X_PIPE_EMPTY
;
350 case NCA_S_FAULT_PIPE_CLOSED
: return RPC_X_PIPE_CLOSED
;
351 case NCA_S_FAULT_PIPE_ORDER
: return RPC_X_WRONG_PIPE_ORDER
;
352 case NCA_S_FAULT_PIPE_DISCIPLINE
: return RPC_X_PIPE_DISCIPLINE_ERROR
;
353 case NCA_S_FAULT_PIPE_COMM_ERROR
: return RPC_S_COMM_FAILURE
;
354 case NCA_S_FAULT_PIPE_MEMORY
: return ERROR_OUTOFMEMORY
;
355 case NCA_S_FAULT_CONTEXT_MISMATCH
: return ERROR_INVALID_HANDLE
;
356 case NCA_S_FAULT_REMOTE_NO_MEMORY
: return ERROR_NOT_ENOUGH_SERVER_MEMORY
;
357 default: return status
;
361 static RPC_STATUS
RPCRT4_SecurePacket(RpcConnection
*Connection
,
362 enum secure_packet_direction dir
,
363 RpcPktHdr
*hdr
, unsigned int hdr_size
,
364 unsigned char *stub_data
, unsigned int stub_data_size
,
365 RpcAuthVerifier
*auth_hdr
,
366 unsigned char *auth_value
, unsigned int auth_value_size
)
368 SecBufferDesc message
;
369 SecBuffer buffers
[4];
370 SECURITY_STATUS sec_status
;
372 message
.ulVersion
= SECBUFFER_VERSION
;
373 message
.cBuffers
= sizeof(buffers
)/sizeof(buffers
[0]);
374 message
.pBuffers
= buffers
;
376 buffers
[0].cbBuffer
= hdr_size
;
377 buffers
[0].BufferType
= SECBUFFER_DATA
|SECBUFFER_READONLY_WITH_CHECKSUM
;
378 buffers
[0].pvBuffer
= hdr
;
379 buffers
[1].cbBuffer
= stub_data_size
;
380 buffers
[1].BufferType
= SECBUFFER_DATA
;
381 buffers
[1].pvBuffer
= stub_data
;
382 buffers
[2].cbBuffer
= sizeof(*auth_hdr
);
383 buffers
[2].BufferType
= SECBUFFER_DATA
|SECBUFFER_READONLY_WITH_CHECKSUM
;
384 buffers
[2].pvBuffer
= auth_hdr
;
385 buffers
[3].cbBuffer
= auth_value_size
;
386 buffers
[3].BufferType
= SECBUFFER_TOKEN
;
387 buffers
[3].pvBuffer
= auth_value
;
389 if (dir
== SECURE_PACKET_SEND
)
391 if ((auth_hdr
->auth_level
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
) && packet_has_body(hdr
))
393 sec_status
= EncryptMessage(&Connection
->ctx
, 0, &message
, 0 /* FIXME */);
394 if (sec_status
!= SEC_E_OK
)
396 ERR("EncryptMessage failed with 0x%08x\n", sec_status
);
397 return RPC_S_SEC_PKG_ERROR
;
400 else if (auth_hdr
->auth_level
!= RPC_C_AUTHN_LEVEL_NONE
)
402 sec_status
= MakeSignature(&Connection
->ctx
, 0, &message
, 0 /* FIXME */);
403 if (sec_status
!= SEC_E_OK
)
405 ERR("MakeSignature failed with 0x%08x\n", sec_status
);
406 return RPC_S_SEC_PKG_ERROR
;
410 else if (dir
== SECURE_PACKET_RECEIVE
)
412 if ((auth_hdr
->auth_level
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
) && packet_has_body(hdr
))
414 sec_status
= DecryptMessage(&Connection
->ctx
, &message
, 0 /* FIXME */, 0);
415 if (sec_status
!= SEC_E_OK
)
417 ERR("EncryptMessage failed with 0x%08x\n", sec_status
);
418 return RPC_S_SEC_PKG_ERROR
;
421 else if (auth_hdr
->auth_level
!= RPC_C_AUTHN_LEVEL_NONE
)
423 sec_status
= VerifySignature(&Connection
->ctx
, &message
, 0 /* FIXME */, NULL
);
424 if (sec_status
!= SEC_E_OK
)
426 ERR("VerifySignature failed with 0x%08x\n", sec_status
);
427 return RPC_S_SEC_PKG_ERROR
;
435 /***********************************************************************
436 * RPCRT4_SendAuth (internal)
438 * Transmit a packet with authorization data over connection in acceptable fragments.
440 static RPC_STATUS
RPCRT4_SendAuth(RpcConnection
*Connection
, RpcPktHdr
*Header
,
441 void *Buffer
, unsigned int BufferLength
,
442 void *Auth
, unsigned int AuthLength
)
452 /* The packet building functions save the packet header size, so we can use it. */
453 hdr_size
= Header
->common
.frag_len
;
455 Header
->common
.auth_len
= AuthLength
;
456 else if (Connection
->AuthInfo
&& packet_has_auth_verifier(Header
))
458 if ((Connection
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
) && packet_has_body(Header
))
459 Header
->common
.auth_len
= Connection
->encryption_auth_len
;
461 Header
->common
.auth_len
= Connection
->signature_auth_len
;
464 Header
->common
.auth_len
= 0;
465 Header
->common
.flags
|= RPC_FLG_FIRST
;
466 Header
->common
.flags
&= ~RPC_FLG_LAST
;
468 alen
= RPC_AUTH_VERIFIER_LEN(&Header
->common
);
470 while (!(Header
->common
.flags
& RPC_FLG_LAST
)) {
471 unsigned char auth_pad_len
= Header
->common
.auth_len
? ROUND_UP_AMOUNT(BufferLength
, AUTH_ALIGNMENT
) : 0;
472 unsigned int pkt_size
= BufferLength
+ hdr_size
+ alen
+ auth_pad_len
;
474 /* decide if we need to split the packet into fragments */
475 if (pkt_size
<= Connection
->MaxTransmissionSize
) {
476 Header
->common
.flags
|= RPC_FLG_LAST
;
477 Header
->common
.frag_len
= pkt_size
;
480 /* make sure packet payload will be a multiple of 16 */
481 Header
->common
.frag_len
=
482 ((Connection
->MaxTransmissionSize
- hdr_size
- alen
) & ~(AUTH_ALIGNMENT
-1)) +
486 pkt
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, Header
->common
.frag_len
);
488 memcpy(pkt
, Header
, hdr_size
);
490 /* fragment consisted of header only and is the last one */
491 if (hdr_size
== Header
->common
.frag_len
)
494 memcpy(pkt
+ hdr_size
, buffer_pos
, Header
->common
.frag_len
- hdr_size
- auth_pad_len
- alen
);
496 /* add the authorization info */
497 if (Connection
->AuthInfo
&& packet_has_auth_verifier(Header
))
499 RpcAuthVerifier
*auth_hdr
= (RpcAuthVerifier
*)&pkt
[Header
->common
.frag_len
- alen
];
501 auth_hdr
->auth_type
= Connection
->AuthInfo
->AuthnSvc
;
502 auth_hdr
->auth_level
= Connection
->AuthInfo
->AuthnLevel
;
503 auth_hdr
->auth_pad_length
= auth_pad_len
;
504 auth_hdr
->auth_reserved
= 0;
505 /* a unique number... */
506 auth_hdr
->auth_context_id
= (unsigned long)Connection
;
509 memcpy(auth_hdr
+ 1, Auth
, AuthLength
);
512 status
= RPCRT4_SecurePacket(Connection
, SECURE_PACKET_SEND
,
513 (RpcPktHdr
*)pkt
, hdr_size
,
514 pkt
+ hdr_size
, Header
->common
.frag_len
- hdr_size
- alen
,
516 (unsigned char *)(auth_hdr
+ 1), Header
->common
.auth_len
);
517 if (status
!= RPC_S_OK
)
519 HeapFree(GetProcessHeap(), 0, pkt
);
526 count
= rpcrt4_conn_write(Connection
, pkt
, Header
->common
.frag_len
);
527 HeapFree(GetProcessHeap(), 0, pkt
);
529 WARN("rpcrt4_conn_write failed (auth)\n");
530 return RPC_S_PROTOCOL_ERROR
;
533 buffer_pos
+= Header
->common
.frag_len
- hdr_size
- alen
- auth_pad_len
;
534 BufferLength
-= Header
->common
.frag_len
- hdr_size
- alen
- auth_pad_len
;
535 Header
->common
.flags
&= ~RPC_FLG_FIRST
;
541 /***********************************************************************
542 * RPCRT4_ClientAuthorize (internal)
544 * Authorize a client connection. A NULL in param signifies a new connection.
546 static RPC_STATUS
RPCRT4_ClientAuthorize(RpcConnection
*conn
, SecBuffer
*in
,
550 SecBufferDesc out_desc
;
551 SecBufferDesc inp_desc
;
552 SecPkgContext_Sizes secctx_sizes
;
553 BOOL continue_needed
;
554 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
555 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
557 if (conn
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_INTEGRITY
)
558 context_req
|= ISC_REQ_INTEGRITY
;
559 else if (conn
->AuthInfo
->AuthnLevel
== RPC_C_AUTHN_LEVEL_PKT_PRIVACY
)
560 context_req
|= ISC_REQ_CONFIDENTIALITY
| ISC_REQ_INTEGRITY
;
562 out
->BufferType
= SECBUFFER_TOKEN
;
563 out
->cbBuffer
= conn
->AuthInfo
->cbMaxToken
;
564 out
->pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
->cbBuffer
);
565 if (!out
->pvBuffer
) return ERROR_OUTOFMEMORY
;
567 out_desc
.ulVersion
= 0;
568 out_desc
.cBuffers
= 1;
569 out_desc
.pBuffers
= out
;
571 inp_desc
.cBuffers
= 1;
572 inp_desc
.pBuffers
= in
;
573 inp_desc
.ulVersion
= 0;
575 r
= InitializeSecurityContextA(&conn
->AuthInfo
->cred
, in
? &conn
->ctx
: NULL
,
576 NULL
, context_req
, 0, SECURITY_NETWORK_DREP
,
577 in
? &inp_desc
: NULL
, 0, &conn
->ctx
, &out_desc
, &conn
->attr
,
581 WARN("InitializeSecurityContext failed with error 0x%08x\n", r
);
585 TRACE("r = 0x%08x, attr = 0x%08x\n", r
, conn
->attr
);
586 continue_needed
= ((r
== SEC_I_CONTINUE_NEEDED
) ||
587 (r
== SEC_I_COMPLETE_AND_CONTINUE
));
589 if ((r
== SEC_I_COMPLETE_NEEDED
) || (r
== SEC_I_COMPLETE_AND_CONTINUE
))
591 TRACE("complete needed\n");
592 r
= CompleteAuthToken(&conn
->ctx
, &out_desc
);
595 WARN("CompleteAuthToken failed with error 0x%08x\n", r
);
600 TRACE("cbBuffer = %ld\n", out
->cbBuffer
);
602 if (!continue_needed
)
604 r
= QueryContextAttributesA(&conn
->ctx
, SECPKG_ATTR_SIZES
, &secctx_sizes
);
607 WARN("QueryContextAttributes failed with error 0x%08x\n", r
);
610 conn
->signature_auth_len
= secctx_sizes
.cbMaxSignature
;
611 conn
->encryption_auth_len
= secctx_sizes
.cbSecurityTrailer
;
617 HeapFree(GetProcessHeap(), 0, out
->pvBuffer
);
618 out
->pvBuffer
= NULL
;
619 return ERROR_ACCESS_DENIED
; /* FIXME: is this correct? */
622 /***********************************************************************
623 * RPCRT4_AuthorizeBinding (internal)
625 static RPC_STATUS
RPCRT_AuthorizeConnection(RpcConnection
* conn
,
626 BYTE
*challenge
, ULONG count
)
632 TRACE("challenge %s, %d bytes\n", challenge
, count
);
634 inp
.BufferType
= SECBUFFER_TOKEN
;
635 inp
.pvBuffer
= challenge
;
636 inp
.cbBuffer
= count
;
638 status
= RPCRT4_ClientAuthorize(conn
, &inp
, &out
);
639 if (status
) return status
;
641 resp_hdr
= RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION
);
643 return E_OUTOFMEMORY
;
645 status
= RPCRT4_SendAuth(conn
, resp_hdr
, NULL
, 0, out
.pvBuffer
, out
.cbBuffer
);
647 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
648 RPCRT4_FreeHeader(resp_hdr
);
653 /***********************************************************************
654 * RPCRT4_Send (internal)
656 * Transmit a packet over connection in acceptable fragments.
658 RPC_STATUS
RPCRT4_Send(RpcConnection
*Connection
, RpcPktHdr
*Header
,
659 void *Buffer
, unsigned int BufferLength
)
664 if (!Connection
->AuthInfo
|| SecIsValidHandle(&Connection
->ctx
))
666 return RPCRT4_SendAuth(Connection
, Header
, Buffer
, BufferLength
, NULL
, 0);
669 /* tack on a negotiate packet */
670 RPCRT4_ClientAuthorize(Connection
, NULL
, &out
);
671 r
= RPCRT4_SendAuth(Connection
, Header
, Buffer
, BufferLength
, out
.pvBuffer
, out
.cbBuffer
);
672 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
677 /***********************************************************************
678 * RPCRT4_Receive (internal)
680 * Receive a packet from connection and merge the fragments.
682 RPC_STATUS
RPCRT4_Receive(RpcConnection
*Connection
, RpcPktHdr
**Header
,
688 unsigned short first_flag
;
689 unsigned long data_length
;
690 unsigned long buffer_length
;
691 unsigned long auth_length
;
692 unsigned char *auth_data
= NULL
;
693 RpcPktCommonHdr common_hdr
;
697 TRACE("(%p, %p, %p)\n", Connection
, Header
, pMsg
);
699 /* read packet common header */
700 dwRead
= rpcrt4_conn_read(Connection
, &common_hdr
, sizeof(common_hdr
));
701 if (dwRead
!= sizeof(common_hdr
)) {
702 WARN("Short read of header, %d bytes\n", dwRead
);
703 status
= RPC_S_PROTOCOL_ERROR
;
707 /* verify if the header really makes sense */
708 if (common_hdr
.rpc_ver
!= RPC_VER_MAJOR
||
709 common_hdr
.rpc_ver_minor
!= RPC_VER_MINOR
) {
710 WARN("unhandled packet version\n");
711 status
= RPC_S_PROTOCOL_ERROR
;
715 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
716 if (hdr_length
== 0) {
717 WARN("header length == 0\n");
718 status
= RPC_S_PROTOCOL_ERROR
;
722 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
723 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
725 /* read the rest of packet header */
726 dwRead
= rpcrt4_conn_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
727 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
728 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
729 status
= RPC_S_PROTOCOL_ERROR
;
733 /* read packet body */
734 switch (common_hdr
.ptype
) {
736 pMsg
->BufferLength
= (*Header
)->response
.alloc_hint
;
739 pMsg
->BufferLength
= (*Header
)->request
.alloc_hint
;
742 pMsg
->BufferLength
= common_hdr
.frag_len
- hdr_length
- RPC_AUTH_VERIFIER_LEN(&common_hdr
);
745 TRACE("buffer length = %u\n", pMsg
->BufferLength
);
747 status
= I_RpcGetBuffer(pMsg
);
748 if (status
!= RPC_S_OK
) goto fail
;
750 first_flag
= RPC_FLG_FIRST
;
751 auth_length
= common_hdr
.auth_len
;
753 auth_data
= HeapAlloc(GetProcessHeap(), 0, RPC_AUTH_VERIFIER_LEN(&common_hdr
));
755 status
= RPC_S_PROTOCOL_ERROR
;
762 unsigned int header_auth_len
= RPC_AUTH_VERIFIER_LEN(&(*Header
)->common
);
764 /* verify header fields */
766 if (((*Header
)->common
.frag_len
< hdr_length
) ||
767 ((*Header
)->common
.frag_len
- hdr_length
< header_auth_len
)) {
768 WARN("frag_len %d too small for hdr_length %d and auth_len %d\n",
769 (*Header
)->common
.frag_len
, hdr_length
, header_auth_len
);
770 status
= RPC_S_PROTOCOL_ERROR
;
774 if ((*Header
)->common
.auth_len
!= auth_length
) {
775 WARN("auth_len header field changed from %ld to %d\n",
776 auth_length
, (*Header
)->common
.auth_len
);
777 status
= RPC_S_PROTOCOL_ERROR
;
781 if (((*Header
)->common
.flags
& RPC_FLG_FIRST
) != first_flag
) {
782 TRACE("invalid packet flags\n");
783 status
= RPC_S_PROTOCOL_ERROR
;
787 data_length
= (*Header
)->common
.frag_len
- hdr_length
- header_auth_len
;
788 if (data_length
+ buffer_length
> pMsg
->BufferLength
) {
789 TRACE("allocation hint exceeded, new buffer length = %ld\n",
790 data_length
+ buffer_length
);
791 pMsg
->BufferLength
= data_length
+ buffer_length
;
792 status
= I_RpcReAllocateBuffer(pMsg
);
793 if (status
!= RPC_S_OK
) goto fail
;
796 if (data_length
== 0) dwRead
= 0; else
797 dwRead
= rpcrt4_conn_read(Connection
,
798 (unsigned char *)pMsg
->Buffer
+ buffer_length
, data_length
);
799 if (dwRead
!= data_length
) {
800 WARN("bad data length, %d/%ld\n", dwRead
, data_length
);
801 status
= RPC_S_PROTOCOL_ERROR
;
805 if (header_auth_len
) {
806 if (header_auth_len
< sizeof(RpcAuthVerifier
)) {
807 WARN("bad auth verifier length %d\n", header_auth_len
);
808 status
= RPC_S_PROTOCOL_ERROR
;
812 /* FIXME: we should accumulate authentication data for the bind,
813 * bind_ack, alter_context and alter_context_response if necessary.
814 * however, the details of how this is done is very sketchy in the
815 * DCE/RPC spec. for all other packet types that have authentication
816 * verifier data then it is just duplicated in all the fragments */
817 dwRead
= rpcrt4_conn_read(Connection
, auth_data
, header_auth_len
);
818 if (dwRead
!= header_auth_len
) {
819 WARN("bad authentication data length, %d/%d\n", dwRead
,
821 status
= RPC_S_PROTOCOL_ERROR
;
825 /* these packets are handled specially, not by the generic SecurePacket
827 if ((common_hdr
.ptype
!= PKT_BIND
) &&
828 (common_hdr
.ptype
!= PKT_BIND_ACK
) &&
829 (common_hdr
.ptype
!= PKT_AUTH3
))
830 status
= RPCRT4_SecurePacket(Connection
, SECURE_PACKET_RECEIVE
,
832 (unsigned char *)pMsg
->Buffer
+ buffer_length
, data_length
,
833 (RpcAuthVerifier
*)auth_data
,
834 (unsigned char *)auth_data
+ sizeof(RpcAuthVerifier
),
835 header_auth_len
- sizeof(RpcAuthVerifier
));
838 buffer_length
+= data_length
;
839 if (!((*Header
)->common
.flags
& RPC_FLG_LAST
)) {
840 TRACE("next header\n");
842 /* read the header of next packet */
843 dwRead
= rpcrt4_conn_read(Connection
, *Header
, hdr_length
);
844 if (dwRead
!= hdr_length
) {
845 WARN("invalid packet header size (%d)\n", dwRead
);
846 status
= RPC_S_PROTOCOL_ERROR
;
855 pMsg
->BufferLength
= buffer_length
;
857 /* respond to authorization request */
858 if (common_hdr
.ptype
== PKT_BIND_ACK
&& auth_length
> sizeof(RpcAuthVerifier
))
860 status
= RPCRT_AuthorizeConnection(Connection
,
861 auth_data
+ sizeof(RpcAuthVerifier
),
871 if (status
!= RPC_S_OK
) {
872 RPCRT4_FreeHeader(*Header
);
875 HeapFree(GetProcessHeap(), 0, auth_data
);
879 /***********************************************************************
880 * I_RpcGetBuffer [RPCRT4.@]
882 * Allocates a buffer for use by I_RpcSend or I_RpcSendReceive and binds to the
886 * pMsg [I/O] RPC message information.
890 * Failure: RPC_S_INVALID_BINDING if pMsg->Handle is invalid.
891 * RPC_S_SERVER_UNAVAILABLE if unable to connect to server.
892 * ERROR_OUTOFMEMORY if buffer allocation failed.
895 * The pMsg->BufferLength field determines the size of the buffer to allocate,
898 * Use I_RpcFreeBuffer() to unbind from the server and free the message buffer.
901 * I_RpcFreeBuffer(), I_RpcSend(), I_RpcReceive(), I_RpcSendReceive().
903 RPC_STATUS WINAPI
I_RpcGetBuffer(PRPC_MESSAGE pMsg
)
905 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
906 /* FIXME: pfnAllocate? */
907 pMsg
->Buffer
= HeapAlloc(GetProcessHeap(), 0, pMsg
->BufferLength
);
909 TRACE("Buffer=%p\n", pMsg
->Buffer
);
910 /* FIXME: which errors to return? */
911 return pMsg
->Buffer
? S_OK
: E_OUTOFMEMORY
;
914 /***********************************************************************
915 * I_RpcReAllocateBuffer (internal)
917 static RPC_STATUS
I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg
)
919 TRACE("(%p): BufferLength=%d\n", pMsg
, pMsg
->BufferLength
);
920 pMsg
->Buffer
= HeapReAlloc(GetProcessHeap(), 0, pMsg
->Buffer
, pMsg
->BufferLength
);
922 TRACE("Buffer=%p\n", pMsg
->Buffer
);
923 return pMsg
->Buffer
? RPC_S_OK
: RPC_S_OUT_OF_RESOURCES
;
926 /***********************************************************************
927 * I_RpcFreeBuffer [RPCRT4.@]
929 * Frees a buffer allocated by I_RpcGetBuffer or I_RpcReceive and unbinds from
930 * the server interface.
933 * pMsg [I/O] RPC message information.
939 * I_RpcGetBuffer(), I_RpcReceive().
941 RPC_STATUS WINAPI
I_RpcFreeBuffer(PRPC_MESSAGE pMsg
)
943 TRACE("(%p) Buffer=%p\n", pMsg
, pMsg
->Buffer
);
944 /* FIXME: pfnFree? */
945 HeapFree(GetProcessHeap(), 0, pMsg
->Buffer
);
950 /***********************************************************************
951 * I_RpcSend [RPCRT4.@]
953 * Sends a message to the server.
956 * pMsg [I/O] RPC message information.
962 * The buffer must have been allocated with I_RpcGetBuffer().
965 * I_RpcGetBuffer(), I_RpcReceive(), I_RpcSendReceive().
967 RPC_STATUS WINAPI
I_RpcSend(PRPC_MESSAGE pMsg
)
969 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
971 RPC_CLIENT_INTERFACE
* cif
= NULL
;
975 TRACE("(%p)\n", pMsg
);
976 if (!bind
|| bind
->server
) return RPC_S_INVALID_BINDING
;
978 cif
= pMsg
->RpcInterfaceInformation
;
979 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
981 if (!bind
->Endpoint
|| !bind
->Endpoint
[0])
983 TRACE("automatically resolving partially bound binding\n");
984 status
= RpcEpResolveBinding(bind
, cif
);
985 if (status
!= RPC_S_OK
) return status
;
988 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
990 if (status
!= RPC_S_OK
) return status
;
992 hdr
= RPCRT4_BuildRequestHeader(pMsg
->DataRepresentation
,
993 pMsg
->BufferLength
, pMsg
->ProcNum
,
997 RPCRT4_CloseBinding(bind
, conn
);
998 return ERROR_OUTOFMEMORY
;
1000 hdr
->common
.call_id
= conn
->NextCallId
++;
1002 status
= RPCRT4_Send(conn
, hdr
, pMsg
->Buffer
, pMsg
->BufferLength
);
1004 RPCRT4_FreeHeader(hdr
);
1006 /* save the connection, so the response can be read from it */
1007 pMsg
->ReservedForRuntime
= conn
;
1011 /* is this status something that the server can't recover from? */
1012 static inline BOOL
is_hard_error(RPC_STATUS status
)
1016 case 0: /* user-defined fault */
1017 case ERROR_ACCESS_DENIED
:
1018 case ERROR_INVALID_PARAMETER
:
1019 case RPC_S_PROTOCOL_ERROR
:
1020 case RPC_S_CALL_FAILED
:
1021 case RPC_S_CALL_FAILED_DNE
:
1022 case RPC_S_SEC_PKG_ERROR
:
1029 /***********************************************************************
1030 * I_RpcReceive [RPCRT4.@]
1032 RPC_STATUS WINAPI
I_RpcReceive(PRPC_MESSAGE pMsg
)
1034 RpcBinding
* bind
= (RpcBinding
*)pMsg
->Handle
;
1035 RpcConnection
* conn
;
1036 RPC_CLIENT_INTERFACE
* cif
= NULL
;
1037 RPC_SERVER_INTERFACE
* sif
= NULL
;
1039 RpcPktHdr
*hdr
= NULL
;
1041 TRACE("(%p)\n", pMsg
);
1042 if (!bind
) return RPC_S_INVALID_BINDING
;
1044 if (pMsg
->ReservedForRuntime
) {
1045 conn
= pMsg
->ReservedForRuntime
;
1046 pMsg
->ReservedForRuntime
= NULL
;
1049 sif
= pMsg
->RpcInterfaceInformation
;
1050 if (!sif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
1051 status
= RPCRT4_OpenBinding(bind
, &conn
, &sif
->TransferSyntax
,
1054 cif
= pMsg
->RpcInterfaceInformation
;
1055 if (!cif
) return RPC_S_INTERFACE_NOT_FOUND
; /* ? */
1057 if (!bind
->Endpoint
|| !bind
->Endpoint
[0])
1059 TRACE("automatically resolving partially bound binding\n");
1060 status
= RpcEpResolveBinding(bind
, cif
);
1061 if (status
!= RPC_S_OK
) return status
;
1064 status
= RPCRT4_OpenBinding(bind
, &conn
, &cif
->TransferSyntax
,
1067 if (status
!= RPC_S_OK
) return status
;
1070 status
= RPCRT4_Receive(conn
, &hdr
, pMsg
);
1071 if (status
!= RPC_S_OK
) {
1072 WARN("receive failed with error %lx\n", status
);
1076 switch (hdr
->common
.ptype
) {
1079 status
= RPC_S_PROTOCOL_ERROR
;
1084 if (!bind
->server
) {
1085 status
= RPC_S_PROTOCOL_ERROR
;
1090 ERR ("we got fault packet with status 0x%lx\n", hdr
->fault
.status
);
1091 status
= NCA2RPC_STATUS(hdr
->fault
.status
);
1092 if (is_hard_error(status
))
1096 WARN("bad packet type %d\n", hdr
->common
.ptype
);
1097 status
= RPC_S_PROTOCOL_ERROR
;
1102 RPCRT4_CloseBinding(bind
, conn
);
1103 RPCRT4_FreeHeader(hdr
);
1107 RPCRT4_FreeHeader(hdr
);
1108 RPCRT4_DestroyConnection(conn
);
1112 /***********************************************************************
1113 * I_RpcSendReceive [RPCRT4.@]
1115 * Sends a message to the server and receives the response.
1118 * pMsg [I/O] RPC message information.
1121 * Success: RPC_S_OK.
1122 * Failure: Any error code.
1125 * The buffer must have been allocated with I_RpcGetBuffer().
1128 * I_RpcGetBuffer(), I_RpcSend(), I_RpcReceive().
1130 RPC_STATUS WINAPI
I_RpcSendReceive(PRPC_MESSAGE pMsg
)
1133 RPC_MESSAGE original_message
;
1135 TRACE("(%p)\n", pMsg
);
1137 original_message
= *pMsg
;
1138 status
= I_RpcSend(pMsg
);
1139 if (status
== RPC_S_OK
)
1140 status
= I_RpcReceive(pMsg
);
1141 /* free the buffer replaced by a new buffer in I_RpcReceive */
1142 if (status
== RPC_S_OK
)
1143 I_RpcFreeBuffer(&original_message
);