* Slap *some* sense into our header inclusions.
[reactos.git] / reactos / dll / win32 / rpcrt4 / rpc_message.c
1 /*
2 * RPC messages
3 *
4 * Copyright 2001-2002 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006 CodeWeavers
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #define _INC_WINDOWS
24
25 #include <stdarg.h>
26 //#include <stdio.h>
27 //#include <string.h>
28
29 #include <windef.h>
30 #include <winbase.h>
31 //#include "winerror.h"
32 #include <winuser.h>
33
34 #include <rpc.h>
35 //#include "rpcndr.h"
36 //#include "rpcdcep.h"
37
38 #include <wine/debug.h>
39
40 #include "rpc_binding.h"
41 //#include "rpc_defs.h"
42 #include "rpc_message.h"
43 #include "ncastatus.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
46
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
50
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))
55
56 static RPC_STATUS I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg);
57
58 DWORD RPCRT4_GetHeaderSize(const RpcPktHdr *Header)
59 {
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)
65 };
66 ULONG ret = 0;
67
68 if (Header->common.ptype < sizeof(header_sizes) / sizeof(header_sizes[0])) {
69 ret = header_sizes[Header->common.ptype];
70 if (ret == 0)
71 FIXME("unhandled packet type %u\n", Header->common.ptype);
72 if (Header->common.flags & RPC_FLG_OBJECT_UUID)
73 ret += sizeof(UUID);
74 } else {
75 WARN("invalid packet type %u\n", Header->common.ptype);
76 }
77
78 return ret;
79 }
80
81 static int packet_has_body(const RpcPktHdr *Header)
82 {
83 return (Header->common.ptype == PKT_FAULT) ||
84 (Header->common.ptype == PKT_REQUEST) ||
85 (Header->common.ptype == PKT_RESPONSE);
86 }
87
88 static int packet_has_auth_verifier(const RpcPktHdr *Header)
89 {
90 return !(Header->common.ptype == PKT_BIND_NACK) &&
91 !(Header->common.ptype == PKT_SHUTDOWN);
92 }
93
94 static int packet_does_auth_negotiation(const RpcPktHdr *Header)
95 {
96 switch (Header->common.ptype)
97 {
98 case PKT_BIND:
99 case PKT_BIND_ACK:
100 case PKT_AUTH3:
101 case PKT_ALTER_CONTEXT:
102 case PKT_ALTER_CONTEXT_RESP:
103 return TRUE;
104 default:
105 return FALSE;
106 }
107 }
108
109 static VOID RPCRT4_BuildCommonHeader(RpcPktHdr *Header, unsigned char PacketType,
110 ULONG DataRepresentation)
111 {
112 Header->common.rpc_ver = RPC_VER_MAJOR;
113 Header->common.rpc_ver_minor = RPC_VER_MINOR;
114 Header->common.ptype = PacketType;
115 Header->common.drep[0] = LOBYTE(LOWORD(DataRepresentation));
116 Header->common.drep[1] = HIBYTE(LOWORD(DataRepresentation));
117 Header->common.drep[2] = LOBYTE(HIWORD(DataRepresentation));
118 Header->common.drep[3] = HIBYTE(HIWORD(DataRepresentation));
119 Header->common.auth_len = 0;
120 Header->common.call_id = 1;
121 Header->common.flags = 0;
122 /* Flags and fragment length are computed in RPCRT4_Send. */
123 }
124
125 static RpcPktHdr *RPCRT4_BuildRequestHeader(ULONG DataRepresentation,
126 ULONG BufferLength,
127 unsigned short ProcNum,
128 UUID *ObjectUuid)
129 {
130 RpcPktHdr *header;
131 BOOL has_object;
132 RPC_STATUS status;
133
134 has_object = (ObjectUuid != NULL && !UuidIsNil(ObjectUuid, &status));
135 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
136 sizeof(header->request) + (has_object ? sizeof(UUID) : 0));
137 if (header == NULL) {
138 return NULL;
139 }
140
141 RPCRT4_BuildCommonHeader(header, PKT_REQUEST, DataRepresentation);
142 header->common.frag_len = sizeof(header->request);
143 header->request.alloc_hint = BufferLength;
144 header->request.context_id = 0;
145 header->request.opnum = ProcNum;
146 if (has_object) {
147 header->common.flags |= RPC_FLG_OBJECT_UUID;
148 header->common.frag_len += sizeof(UUID);
149 memcpy(&header->request + 1, ObjectUuid, sizeof(UUID));
150 }
151
152 return header;
153 }
154
155 RpcPktHdr *RPCRT4_BuildResponseHeader(ULONG DataRepresentation, ULONG BufferLength)
156 {
157 RpcPktHdr *header;
158
159 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->response));
160 if (header == NULL) {
161 return NULL;
162 }
163
164 RPCRT4_BuildCommonHeader(header, PKT_RESPONSE, DataRepresentation);
165 header->common.frag_len = sizeof(header->response);
166 header->response.alloc_hint = BufferLength;
167
168 return header;
169 }
170
171 RpcPktHdr *RPCRT4_BuildFaultHeader(ULONG DataRepresentation, RPC_STATUS Status)
172 {
173 RpcPktHdr *header;
174
175 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->fault));
176 if (header == NULL) {
177 return NULL;
178 }
179
180 RPCRT4_BuildCommonHeader(header, PKT_FAULT, DataRepresentation);
181 header->common.frag_len = sizeof(header->fault);
182 header->fault.status = Status;
183
184 return header;
185 }
186
187 RpcPktHdr *RPCRT4_BuildBindHeader(ULONG DataRepresentation,
188 unsigned short MaxTransmissionSize,
189 unsigned short MaxReceiveSize,
190 ULONG AssocGroupId,
191 const RPC_SYNTAX_IDENTIFIER *AbstractId,
192 const RPC_SYNTAX_IDENTIFIER *TransferId)
193 {
194 RpcPktHdr *header;
195 RpcContextElement *ctxt_elem;
196
197 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
198 sizeof(header->bind) + FIELD_OFFSET(RpcContextElement, transfer_syntaxes[1]));
199 if (header == NULL) {
200 return NULL;
201 }
202 ctxt_elem = (RpcContextElement *)(&header->bind + 1);
203
204 RPCRT4_BuildCommonHeader(header, PKT_BIND, DataRepresentation);
205 header->common.frag_len = sizeof(header->bind) + FIELD_OFFSET(RpcContextElement, transfer_syntaxes[1]);
206 header->bind.max_tsize = MaxTransmissionSize;
207 header->bind.max_rsize = MaxReceiveSize;
208 header->bind.assoc_gid = AssocGroupId;
209 header->bind.num_elements = 1;
210 ctxt_elem->num_syntaxes = 1;
211 ctxt_elem->abstract_syntax = *AbstractId;
212 ctxt_elem->transfer_syntaxes[0] = *TransferId;
213
214 return header;
215 }
216
217 static RpcPktHdr *RPCRT4_BuildAuthHeader(ULONG DataRepresentation)
218 {
219 RpcPktHdr *header;
220
221 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
222 sizeof(header->auth3));
223 if (header == NULL)
224 return NULL;
225
226 RPCRT4_BuildCommonHeader(header, PKT_AUTH3, DataRepresentation);
227 header->common.frag_len = sizeof(header->auth3);
228
229 return header;
230 }
231
232 RpcPktHdr *RPCRT4_BuildBindNackHeader(ULONG DataRepresentation,
233 unsigned char RpcVersion,
234 unsigned char RpcVersionMinor,
235 unsigned short RejectReason)
236 {
237 RpcPktHdr *header;
238
239 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(RpcPktHdr, bind_nack.protocols[1]));
240 if (header == NULL) {
241 return NULL;
242 }
243
244 RPCRT4_BuildCommonHeader(header, PKT_BIND_NACK, DataRepresentation);
245 header->common.frag_len = FIELD_OFFSET(RpcPktHdr, bind_nack.protocols[1]);
246 header->bind_nack.reject_reason = RejectReason;
247 header->bind_nack.protocols_count = 1;
248 header->bind_nack.protocols[0].rpc_ver = RpcVersion;
249 header->bind_nack.protocols[0].rpc_ver_minor = RpcVersionMinor;
250
251 return header;
252 }
253
254 RpcPktHdr *RPCRT4_BuildBindAckHeader(ULONG DataRepresentation,
255 unsigned short MaxTransmissionSize,
256 unsigned short MaxReceiveSize,
257 ULONG AssocGroupId,
258 LPCSTR ServerAddress,
259 unsigned char ResultCount,
260 const RpcResult *Results)
261 {
262 RpcPktHdr *header;
263 ULONG header_size;
264 RpcAddressString *server_address;
265 RpcResultList *results;
266
267 header_size = sizeof(header->bind_ack) +
268 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[strlen(ServerAddress) + 1]), 4) +
269 FIELD_OFFSET(RpcResultList, results[ResultCount]);
270
271 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, header_size);
272 if (header == NULL) {
273 return NULL;
274 }
275
276 RPCRT4_BuildCommonHeader(header, PKT_BIND_ACK, DataRepresentation);
277 header->common.frag_len = header_size;
278 header->bind_ack.max_tsize = MaxTransmissionSize;
279 header->bind_ack.max_rsize = MaxReceiveSize;
280 header->bind_ack.assoc_gid = AssocGroupId;
281 server_address = (RpcAddressString*)(&header->bind_ack + 1);
282 server_address->length = strlen(ServerAddress) + 1;
283 strcpy(server_address->string, ServerAddress);
284 /* results is 4-byte aligned */
285 results = (RpcResultList*)((ULONG_PTR)server_address + ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4));
286 results->num_results = ResultCount;
287 memcpy(&results->results[0], Results, ResultCount * sizeof(*Results));
288
289 return header;
290 }
291
292 RpcPktHdr *RPCRT4_BuildHttpHeader(ULONG DataRepresentation,
293 unsigned short flags,
294 unsigned short num_data_items,
295 unsigned int payload_size)
296 {
297 RpcPktHdr *header;
298
299 header = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(header->http) + payload_size);
300 if (header == NULL) {
301 ERR("failed to allocate memory\n");
302 return NULL;
303 }
304
305 RPCRT4_BuildCommonHeader(header, PKT_HTTP, DataRepresentation);
306 /* since the packet isn't current sent using RPCRT4_Send, set the flags
307 * manually here */
308 header->common.flags = RPC_FLG_FIRST|RPC_FLG_LAST;
309 header->common.call_id = 0;
310 header->common.frag_len = sizeof(header->http) + payload_size;
311 header->http.flags = flags;
312 header->http.num_data_items = num_data_items;
313
314 return header;
315 }
316
317 #define WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, type, value) \
318 do { \
319 *(unsigned int *)(payload) = (type); \
320 (payload) += 4; \
321 *(unsigned int *)(payload) = (value); \
322 (payload) += 4; \
323 } while (0)
324
325 #define WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, type, uuid) \
326 do { \
327 *(unsigned int *)(payload) = (type); \
328 (payload) += 4; \
329 *(UUID *)(payload) = (uuid); \
330 (payload) += sizeof(UUID); \
331 } while (0)
332
333 #define WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted, flow_control_increment, uuid) \
334 do { \
335 *(unsigned int *)(payload) = 0x00000001; \
336 (payload) += 4; \
337 *(unsigned int *)(payload) = (bytes_transmitted); \
338 (payload) += 4; \
339 *(unsigned int *)(payload) = (flow_control_increment); \
340 (payload) += 4; \
341 *(UUID *)(payload) = (uuid); \
342 (payload) += sizeof(UUID); \
343 } while (0)
344
345 RpcPktHdr *RPCRT4_BuildHttpConnectHeader(unsigned short flags, int out_pipe,
346 const UUID *connection_uuid,
347 const UUID *pipe_uuid,
348 const UUID *association_uuid)
349 {
350 RpcPktHdr *header;
351 unsigned int size;
352 char *payload;
353
354 size = 8 + 4 + sizeof(UUID) + 4 + sizeof(UUID) + 8;
355 if (!out_pipe)
356 size += 8 + 4 + sizeof(UUID);
357
358 header = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, flags,
359 out_pipe ? 4 : 6, size);
360 if (!header) return NULL;
361 payload = (char *)(&header->http+1);
362
363 /* FIXME: what does this part of the payload do? */
364 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000006, 0x00000001);
365
366 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x00000003, *connection_uuid);
367 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x00000003, *pipe_uuid);
368
369 if (out_pipe)
370 /* FIXME: what does this part of the payload do? */
371 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000000, 0x00010000);
372 else
373 {
374 /* FIXME: what does this part of the payload do? */
375 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000004, 0x40000000);
376 /* FIXME: what does this part of the payload do? */
377 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x00000005, 0x000493e0);
378
379 WRITE_HTTP_PAYLOAD_FIELD_UUID(payload, 0x0000000c, *association_uuid);
380 }
381
382 return header;
383 }
384
385 RpcPktHdr *RPCRT4_BuildHttpFlowControlHeader(BOOL server, ULONG bytes_transmitted,
386 ULONG flow_control_increment,
387 const UUID *pipe_uuid)
388 {
389 RpcPktHdr *header;
390 char *payload;
391
392 header = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x2, 2,
393 5 * sizeof(ULONG) + sizeof(UUID));
394 if (!header) return NULL;
395 payload = (char *)(&header->http+1);
396
397 WRITE_HTTP_PAYLOAD_FIELD_UINT32(payload, 0x0000000d, (server ? 0x0 : 0x3));
398
399 WRITE_HTTP_PAYLOAD_FIELD_FLOW_CONTROL(payload, bytes_transmitted,
400 flow_control_increment, *pipe_uuid);
401 return header;
402 }
403
404 VOID RPCRT4_FreeHeader(RpcPktHdr *Header)
405 {
406 HeapFree(GetProcessHeap(), 0, Header);
407 }
408
409 NCA_STATUS RPC2NCA_STATUS(RPC_STATUS status)
410 {
411 switch (status)
412 {
413 case ERROR_INVALID_HANDLE: return NCA_S_FAULT_CONTEXT_MISMATCH;
414 case ERROR_OUTOFMEMORY: return NCA_S_FAULT_REMOTE_NO_MEMORY;
415 case RPC_S_NOT_LISTENING: return NCA_S_SERVER_TOO_BUSY;
416 case RPC_S_UNKNOWN_IF: return NCA_S_UNK_IF;
417 case RPC_S_SERVER_TOO_BUSY: return NCA_S_SERVER_TOO_BUSY;
418 case RPC_S_CALL_FAILED: return NCA_S_FAULT_UNSPEC;
419 case RPC_S_CALL_FAILED_DNE: return NCA_S_MANAGER_NOT_ENTERED;
420 case RPC_S_PROTOCOL_ERROR: return NCA_S_PROTO_ERROR;
421 case RPC_S_UNSUPPORTED_TYPE: return NCA_S_UNSUPPORTED_TYPE;
422 case RPC_S_INVALID_TAG: return NCA_S_FAULT_INVALID_TAG;
423 case RPC_S_INVALID_BOUND: return NCA_S_FAULT_INVALID_BOUND;
424 case RPC_S_PROCNUM_OUT_OF_RANGE: return NCA_S_OP_RNG_ERROR;
425 case RPC_X_SS_HANDLES_MISMATCH: return NCA_S_FAULT_CONTEXT_MISMATCH;
426 case RPC_S_CALL_CANCELLED: return NCA_S_FAULT_CANCEL;
427 case RPC_S_COMM_FAILURE: return NCA_S_COMM_FAILURE;
428 case RPC_X_WRONG_PIPE_ORDER: return NCA_S_FAULT_PIPE_ORDER;
429 case RPC_X_PIPE_CLOSED: return NCA_S_FAULT_PIPE_CLOSED;
430 case RPC_X_PIPE_DISCIPLINE_ERROR: return NCA_S_FAULT_PIPE_DISCIPLINE;
431 case RPC_X_PIPE_EMPTY: return NCA_S_FAULT_PIPE_EMPTY;
432 case STATUS_FLOAT_DIVIDE_BY_ZERO: return NCA_S_FAULT_FP_DIV_ZERO;
433 case STATUS_FLOAT_INVALID_OPERATION: return NCA_S_FAULT_FP_ERROR;
434 case STATUS_FLOAT_OVERFLOW: return NCA_S_FAULT_FP_OVERFLOW;
435 case STATUS_FLOAT_UNDERFLOW: return NCA_S_FAULT_FP_UNDERFLOW;
436 case STATUS_INTEGER_DIVIDE_BY_ZERO: return NCA_S_FAULT_INT_DIV_BY_ZERO;
437 case STATUS_INTEGER_OVERFLOW: return NCA_S_FAULT_INT_OVERFLOW;
438 default: return status;
439 }
440 }
441
442 static RPC_STATUS NCA2RPC_STATUS(NCA_STATUS status)
443 {
444 switch (status)
445 {
446 case NCA_S_COMM_FAILURE: return RPC_S_COMM_FAILURE;
447 case NCA_S_OP_RNG_ERROR: return RPC_S_PROCNUM_OUT_OF_RANGE;
448 case NCA_S_UNK_IF: return RPC_S_UNKNOWN_IF;
449 case NCA_S_YOU_CRASHED: return RPC_S_CALL_FAILED;
450 case NCA_S_PROTO_ERROR: return RPC_S_PROTOCOL_ERROR;
451 case NCA_S_OUT_ARGS_TOO_BIG: return ERROR_NOT_ENOUGH_SERVER_MEMORY;
452 case NCA_S_SERVER_TOO_BUSY: return RPC_S_SERVER_TOO_BUSY;
453 case NCA_S_UNSUPPORTED_TYPE: return RPC_S_UNSUPPORTED_TYPE;
454 case NCA_S_FAULT_INT_DIV_BY_ZERO: return RPC_S_ZERO_DIVIDE;
455 case NCA_S_FAULT_ADDR_ERROR: return RPC_S_ADDRESS_ERROR;
456 case NCA_S_FAULT_FP_DIV_ZERO: return RPC_S_FP_DIV_ZERO;
457 case NCA_S_FAULT_FP_UNDERFLOW: return RPC_S_FP_UNDERFLOW;
458 case NCA_S_FAULT_FP_OVERFLOW: return RPC_S_FP_OVERFLOW;
459 case NCA_S_FAULT_INVALID_TAG: return RPC_S_INVALID_TAG;
460 case NCA_S_FAULT_INVALID_BOUND: return RPC_S_INVALID_BOUND;
461 case NCA_S_RPC_VERSION_MISMATCH: return RPC_S_PROTOCOL_ERROR;
462 case NCA_S_UNSPEC_REJECT: return RPC_S_CALL_FAILED_DNE;
463 case NCA_S_BAD_ACTID: return RPC_S_CALL_FAILED_DNE;
464 case NCA_S_WHO_ARE_YOU_FAILED: return RPC_S_CALL_FAILED;
465 case NCA_S_MANAGER_NOT_ENTERED: return RPC_S_CALL_FAILED_DNE;
466 case NCA_S_FAULT_CANCEL: return RPC_S_CALL_CANCELLED;
467 case NCA_S_FAULT_ILL_INST: return RPC_S_ADDRESS_ERROR;
468 case NCA_S_FAULT_FP_ERROR: return RPC_S_FP_OVERFLOW;
469 case NCA_S_FAULT_INT_OVERFLOW: return RPC_S_ADDRESS_ERROR;
470 case NCA_S_FAULT_UNSPEC: return RPC_S_CALL_FAILED;
471 case NCA_S_FAULT_PIPE_EMPTY: return RPC_X_PIPE_EMPTY;
472 case NCA_S_FAULT_PIPE_CLOSED: return RPC_X_PIPE_CLOSED;
473 case NCA_S_FAULT_PIPE_ORDER: return RPC_X_WRONG_PIPE_ORDER;
474 case NCA_S_FAULT_PIPE_DISCIPLINE: return RPC_X_PIPE_DISCIPLINE_ERROR;
475 case NCA_S_FAULT_PIPE_COMM_ERROR: return RPC_S_COMM_FAILURE;
476 case NCA_S_FAULT_PIPE_MEMORY: return ERROR_OUTOFMEMORY;
477 case NCA_S_FAULT_CONTEXT_MISMATCH: return ERROR_INVALID_HANDLE;
478 case NCA_S_FAULT_REMOTE_NO_MEMORY: return ERROR_NOT_ENOUGH_SERVER_MEMORY;
479 default: return status;
480 }
481 }
482
483 /* assumes the common header fields have already been validated */
484 BOOL RPCRT4_IsValidHttpPacket(RpcPktHdr *hdr, unsigned char *data,
485 unsigned short data_len)
486 {
487 unsigned short i;
488 BYTE *p = data;
489
490 for (i = 0; i < hdr->http.num_data_items; i++)
491 {
492 ULONG type;
493
494 if (data_len < sizeof(ULONG))
495 return FALSE;
496
497 type = *(ULONG *)p;
498 p += sizeof(ULONG);
499 data_len -= sizeof(ULONG);
500
501 switch (type)
502 {
503 case 0x3:
504 case 0xc:
505 if (data_len < sizeof(GUID))
506 return FALSE;
507 p += sizeof(GUID);
508 data_len -= sizeof(GUID);
509 break;
510 case 0x0:
511 case 0x2:
512 case 0x4:
513 case 0x5:
514 case 0x6:
515 case 0xd:
516 if (data_len < sizeof(ULONG))
517 return FALSE;
518 p += sizeof(ULONG);
519 data_len -= sizeof(ULONG);
520 break;
521 case 0x1:
522 if (data_len < 24)
523 return FALSE;
524 p += 24;
525 data_len -= 24;
526 break;
527 default:
528 FIXME("unimplemented type 0x%x\n", type);
529 break;
530 }
531 }
532 return TRUE;
533 }
534
535 /* assumes the HTTP packet has been validated */
536 static unsigned char *RPCRT4_NextHttpHeaderField(unsigned char *data)
537 {
538 ULONG type;
539
540 type = *(ULONG *)data;
541 data += sizeof(ULONG);
542
543 switch (type)
544 {
545 case 0x3:
546 case 0xc:
547 return data + sizeof(GUID);
548 case 0x0:
549 case 0x2:
550 case 0x4:
551 case 0x5:
552 case 0x6:
553 case 0xd:
554 return data + sizeof(ULONG);
555 case 0x1:
556 return data + 24;
557 default:
558 FIXME("unimplemented type 0x%x\n", type);
559 return data;
560 }
561 }
562
563 #define READ_HTTP_PAYLOAD_FIELD_TYPE(data) *(ULONG *)(data)
564 #define GET_HTTP_PAYLOAD_FIELD_DATA(data) ((data) + sizeof(ULONG))
565
566 /* assumes the HTTP packet has been validated */
567 RPC_STATUS RPCRT4_ParseHttpPrepareHeader1(RpcPktHdr *header,
568 unsigned char *data, ULONG *field1)
569 {
570 ULONG type;
571 if (header->http.flags != 0x0)
572 {
573 ERR("invalid flags 0x%x\n", header->http.flags);
574 return RPC_S_PROTOCOL_ERROR;
575 }
576 if (header->http.num_data_items != 1)
577 {
578 ERR("invalid number of data items %d\n", header->http.num_data_items);
579 return RPC_S_PROTOCOL_ERROR;
580 }
581 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
582 if (type != 0x00000002)
583 {
584 ERR("invalid type 0x%08x\n", type);
585 return RPC_S_PROTOCOL_ERROR;
586 }
587 *field1 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
588 return RPC_S_OK;
589 }
590
591 /* assumes the HTTP packet has been validated */
592 RPC_STATUS RPCRT4_ParseHttpPrepareHeader2(RpcPktHdr *header,
593 unsigned char *data, ULONG *field1,
594 ULONG *bytes_until_next_packet,
595 ULONG *field3)
596 {
597 ULONG type;
598 if (header->http.flags != 0x0)
599 {
600 ERR("invalid flags 0x%x\n", header->http.flags);
601 return RPC_S_PROTOCOL_ERROR;
602 }
603 if (header->http.num_data_items != 3)
604 {
605 ERR("invalid number of data items %d\n", header->http.num_data_items);
606 return RPC_S_PROTOCOL_ERROR;
607 }
608
609 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
610 if (type != 0x00000006)
611 {
612 ERR("invalid type for field 1: 0x%08x\n", type);
613 return RPC_S_PROTOCOL_ERROR;
614 }
615 *field1 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
616 data = RPCRT4_NextHttpHeaderField(data);
617
618 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
619 if (type != 0x00000000)
620 {
621 ERR("invalid type for field 2: 0x%08x\n", type);
622 return RPC_S_PROTOCOL_ERROR;
623 }
624 *bytes_until_next_packet = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
625 data = RPCRT4_NextHttpHeaderField(data);
626
627 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
628 if (type != 0x00000002)
629 {
630 ERR("invalid type for field 3: 0x%08x\n", type);
631 return RPC_S_PROTOCOL_ERROR;
632 }
633 *field3 = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
634
635 return RPC_S_OK;
636 }
637
638 RPC_STATUS RPCRT4_ParseHttpFlowControlHeader(RpcPktHdr *header,
639 unsigned char *data, BOOL server,
640 ULONG *bytes_transmitted,
641 ULONG *flow_control_increment,
642 UUID *pipe_uuid)
643 {
644 ULONG type;
645 if (header->http.flags != 0x2)
646 {
647 ERR("invalid flags 0x%x\n", header->http.flags);
648 return RPC_S_PROTOCOL_ERROR;
649 }
650 if (header->http.num_data_items != 2)
651 {
652 ERR("invalid number of data items %d\n", header->http.num_data_items);
653 return RPC_S_PROTOCOL_ERROR;
654 }
655
656 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
657 if (type != 0x0000000d)
658 {
659 ERR("invalid type for field 1: 0x%08x\n", type);
660 return RPC_S_PROTOCOL_ERROR;
661 }
662 if (*(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data) != (server ? 0x3 : 0x0))
663 {
664 ERR("invalid type for 0xd field data: 0x%08x\n", *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data));
665 return RPC_S_PROTOCOL_ERROR;
666 }
667 data = RPCRT4_NextHttpHeaderField(data);
668
669 type = READ_HTTP_PAYLOAD_FIELD_TYPE(data);
670 if (type != 0x00000001)
671 {
672 ERR("invalid type for field 2: 0x%08x\n", type);
673 return RPC_S_PROTOCOL_ERROR;
674 }
675 *bytes_transmitted = *(ULONG *)GET_HTTP_PAYLOAD_FIELD_DATA(data);
676 *flow_control_increment = *(ULONG *)(GET_HTTP_PAYLOAD_FIELD_DATA(data) + 4);
677 *pipe_uuid = *(UUID *)(GET_HTTP_PAYLOAD_FIELD_DATA(data) + 8);
678
679 return RPC_S_OK;
680 }
681
682
683 RPC_STATUS RPCRT4_default_secure_packet(RpcConnection *Connection,
684 enum secure_packet_direction dir,
685 RpcPktHdr *hdr, unsigned int hdr_size,
686 unsigned char *stub_data, unsigned int stub_data_size,
687 RpcAuthVerifier *auth_hdr,
688 unsigned char *auth_value, unsigned int auth_value_size)
689 {
690 SecBufferDesc message;
691 SecBuffer buffers[4];
692 SECURITY_STATUS sec_status;
693
694 message.ulVersion = SECBUFFER_VERSION;
695 message.cBuffers = sizeof(buffers)/sizeof(buffers[0]);
696 message.pBuffers = buffers;
697
698 buffers[0].cbBuffer = hdr_size;
699 buffers[0].BufferType = SECBUFFER_DATA|SECBUFFER_READONLY_WITH_CHECKSUM;
700 buffers[0].pvBuffer = hdr;
701 buffers[1].cbBuffer = stub_data_size;
702 buffers[1].BufferType = SECBUFFER_DATA;
703 buffers[1].pvBuffer = stub_data;
704 buffers[2].cbBuffer = sizeof(*auth_hdr);
705 buffers[2].BufferType = SECBUFFER_DATA|SECBUFFER_READONLY_WITH_CHECKSUM;
706 buffers[2].pvBuffer = auth_hdr;
707 buffers[3].cbBuffer = auth_value_size;
708 buffers[3].BufferType = SECBUFFER_TOKEN;
709 buffers[3].pvBuffer = auth_value;
710
711 if (dir == SECURE_PACKET_SEND)
712 {
713 if ((auth_hdr->auth_level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(hdr))
714 {
715 sec_status = EncryptMessage(&Connection->ctx, 0, &message, 0 /* FIXME */);
716 if (sec_status != SEC_E_OK)
717 {
718 ERR("EncryptMessage failed with 0x%08x\n", sec_status);
719 return RPC_S_SEC_PKG_ERROR;
720 }
721 }
722 else if (auth_hdr->auth_level != RPC_C_AUTHN_LEVEL_NONE)
723 {
724 sec_status = MakeSignature(&Connection->ctx, 0, &message, 0 /* FIXME */);
725 if (sec_status != SEC_E_OK)
726 {
727 ERR("MakeSignature failed with 0x%08x\n", sec_status);
728 return RPC_S_SEC_PKG_ERROR;
729 }
730 }
731 }
732 else if (dir == SECURE_PACKET_RECEIVE)
733 {
734 if ((auth_hdr->auth_level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(hdr))
735 {
736 sec_status = DecryptMessage(&Connection->ctx, &message, 0 /* FIXME */, 0);
737 if (sec_status != SEC_E_OK)
738 {
739 ERR("DecryptMessage failed with 0x%08x\n", sec_status);
740 return RPC_S_SEC_PKG_ERROR;
741 }
742 }
743 else if (auth_hdr->auth_level != RPC_C_AUTHN_LEVEL_NONE)
744 {
745 sec_status = VerifySignature(&Connection->ctx, &message, 0 /* FIXME */, NULL);
746 if (sec_status != SEC_E_OK)
747 {
748 ERR("VerifySignature failed with 0x%08x\n", sec_status);
749 return RPC_S_SEC_PKG_ERROR;
750 }
751 }
752 }
753
754 return RPC_S_OK;
755 }
756
757 /***********************************************************************
758 * RPCRT4_SendWithAuth (internal)
759 *
760 * Transmit a packet with authorization data over connection in acceptable fragments.
761 */
762 RPC_STATUS RPCRT4_SendWithAuth(RpcConnection *Connection, RpcPktHdr *Header,
763 void *Buffer, unsigned int BufferLength,
764 const void *Auth, unsigned int AuthLength)
765 {
766 PUCHAR buffer_pos;
767 DWORD hdr_size;
768 LONG count;
769 unsigned char *pkt;
770 LONG alen;
771 RPC_STATUS status;
772
773 RPCRT4_SetThreadCurrentConnection(Connection);
774
775 buffer_pos = Buffer;
776 /* The packet building functions save the packet header size, so we can use it. */
777 hdr_size = Header->common.frag_len;
778 if (AuthLength)
779 Header->common.auth_len = AuthLength;
780 else if (Connection->AuthInfo && packet_has_auth_verifier(Header))
781 {
782 if ((Connection->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) && packet_has_body(Header))
783 Header->common.auth_len = Connection->encryption_auth_len;
784 else
785 Header->common.auth_len = Connection->signature_auth_len;
786 }
787 else
788 Header->common.auth_len = 0;
789 Header->common.flags |= RPC_FLG_FIRST;
790 Header->common.flags &= ~RPC_FLG_LAST;
791
792 alen = RPC_AUTH_VERIFIER_LEN(&Header->common);
793
794 while (!(Header->common.flags & RPC_FLG_LAST)) {
795 unsigned char auth_pad_len = Header->common.auth_len ? ROUND_UP_AMOUNT(BufferLength, AUTH_ALIGNMENT) : 0;
796 unsigned int pkt_size = BufferLength + hdr_size + alen + auth_pad_len;
797
798 /* decide if we need to split the packet into fragments */
799 if (pkt_size <= Connection->MaxTransmissionSize) {
800 Header->common.flags |= RPC_FLG_LAST;
801 Header->common.frag_len = pkt_size;
802 } else {
803 auth_pad_len = 0;
804 /* make sure packet payload will be a multiple of 16 */
805 Header->common.frag_len =
806 ((Connection->MaxTransmissionSize - hdr_size - alen) & ~(AUTH_ALIGNMENT-1)) +
807 hdr_size + alen;
808 }
809
810 pkt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, Header->common.frag_len);
811
812 memcpy(pkt, Header, hdr_size);
813
814 /* fragment consisted of header only and is the last one */
815 if (hdr_size == Header->common.frag_len)
816 goto write;
817
818 memcpy(pkt + hdr_size, buffer_pos, Header->common.frag_len - hdr_size - auth_pad_len - alen);
819
820 /* add the authorization info */
821 if (Header->common.auth_len)
822 {
823 RpcAuthVerifier *auth_hdr = (RpcAuthVerifier *)&pkt[Header->common.frag_len - alen];
824
825 auth_hdr->auth_type = Connection->AuthInfo->AuthnSvc;
826 auth_hdr->auth_level = Connection->AuthInfo->AuthnLevel;
827 auth_hdr->auth_pad_length = auth_pad_len;
828 auth_hdr->auth_reserved = 0;
829 /* a unique number... */
830 auth_hdr->auth_context_id = Connection->auth_context_id;
831
832 if (AuthLength)
833 memcpy(auth_hdr + 1, Auth, AuthLength);
834 else
835 {
836 status = rpcrt4_conn_secure_packet(Connection, SECURE_PACKET_SEND,
837 (RpcPktHdr *)pkt, hdr_size,
838 pkt + hdr_size, Header->common.frag_len - hdr_size - alen,
839 auth_hdr,
840 (unsigned char *)(auth_hdr + 1), Header->common.auth_len);
841 if (status != RPC_S_OK)
842 {
843 HeapFree(GetProcessHeap(), 0, pkt);
844 RPCRT4_SetThreadCurrentConnection(NULL);
845 return status;
846 }
847 }
848 }
849
850 write:
851 count = rpcrt4_conn_write(Connection, pkt, Header->common.frag_len);
852 HeapFree(GetProcessHeap(), 0, pkt);
853 if (count<0) {
854 WARN("rpcrt4_conn_write failed (auth)\n");
855 RPCRT4_SetThreadCurrentConnection(NULL);
856 return RPC_S_CALL_FAILED;
857 }
858
859 buffer_pos += Header->common.frag_len - hdr_size - alen - auth_pad_len;
860 BufferLength -= Header->common.frag_len - hdr_size - alen - auth_pad_len;
861 Header->common.flags &= ~RPC_FLG_FIRST;
862 }
863
864 RPCRT4_SetThreadCurrentConnection(NULL);
865 return RPC_S_OK;
866 }
867
868 /***********************************************************************
869 * RPCRT4_default_authorize (internal)
870 *
871 * Authorize a client connection.
872 */
873 RPC_STATUS RPCRT4_default_authorize(RpcConnection *conn, BOOL first_time,
874 unsigned char *in_buffer,
875 unsigned int in_size,
876 unsigned char *out_buffer,
877 unsigned int *out_size)
878 {
879 SECURITY_STATUS r;
880 SecBufferDesc out_desc;
881 SecBufferDesc inp_desc;
882 SecPkgContext_Sizes secctx_sizes;
883 BOOL continue_needed;
884 ULONG context_req;
885 SecBuffer in, out;
886
887 if (!out_buffer)
888 {
889 *out_size = conn->AuthInfo->cbMaxToken;
890 return RPC_S_OK;
891 }
892
893 in.BufferType = SECBUFFER_TOKEN;
894 in.pvBuffer = in_buffer;
895 in.cbBuffer = in_size;
896
897 out.BufferType = SECBUFFER_TOKEN;
898 out.pvBuffer = out_buffer;
899 out.cbBuffer = *out_size;
900
901 out_desc.ulVersion = 0;
902 out_desc.cBuffers = 1;
903 out_desc.pBuffers = &out;
904
905 inp_desc.ulVersion = 0;
906 inp_desc.cBuffers = 1;
907 inp_desc.pBuffers = &in;
908
909 if (conn->server)
910 {
911 context_req = ASC_REQ_CONNECTION | ASC_REQ_USE_DCE_STYLE |
912 ASC_REQ_DELEGATE;
913
914 if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY)
915 context_req |= ASC_REQ_INTEGRITY;
916 else if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
917 context_req |= ASC_REQ_CONFIDENTIALITY | ASC_REQ_INTEGRITY;
918
919 r = AcceptSecurityContext(&conn->AuthInfo->cred,
920 first_time ? NULL : &conn->ctx,
921 &inp_desc, context_req, SECURITY_NETWORK_DREP,
922 &conn->ctx,
923 &out_desc, &conn->attr, &conn->exp);
924 if (r == SEC_E_OK || r == SEC_I_COMPLETE_NEEDED)
925 {
926 /* authorisation done, so nothing more to send */
927 out.cbBuffer = 0;
928 }
929 }
930 else
931 {
932 context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
933 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
934
935 if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY)
936 context_req |= ISC_REQ_INTEGRITY;
937 else if (conn->AuthInfo->AuthnLevel == RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
938 context_req |= ISC_REQ_CONFIDENTIALITY | ISC_REQ_INTEGRITY;
939
940 r = InitializeSecurityContextW(&conn->AuthInfo->cred,
941 first_time ? NULL: &conn->ctx,
942 first_time ? conn->AuthInfo->server_principal_name : NULL,
943 context_req, 0, SECURITY_NETWORK_DREP,
944 first_time ? NULL : &inp_desc, 0, &conn->ctx,
945 &out_desc, &conn->attr, &conn->exp);
946 }
947 if (FAILED(r))
948 {
949 WARN("InitializeSecurityContext failed with error 0x%08x\n", r);
950 goto failed;
951 }
952
953 TRACE("r = 0x%08x, attr = 0x%08x\n", r, conn->attr);
954 continue_needed = ((r == SEC_I_CONTINUE_NEEDED) ||
955 (r == SEC_I_COMPLETE_AND_CONTINUE));
956
957 if ((r == SEC_I_COMPLETE_NEEDED) || (r == SEC_I_COMPLETE_AND_CONTINUE))
958 {
959 TRACE("complete needed\n");
960 r = CompleteAuthToken(&conn->ctx, &out_desc);
961 if (FAILED(r))
962 {
963 WARN("CompleteAuthToken failed with error 0x%08x\n", r);
964 goto failed;
965 }
966 }
967
968 TRACE("cbBuffer = %d\n", out.cbBuffer);
969
970 if (!continue_needed)
971 {
972 r = QueryContextAttributesA(&conn->ctx, SECPKG_ATTR_SIZES, &secctx_sizes);
973 if (FAILED(r))
974 {
975 WARN("QueryContextAttributes failed with error 0x%08x\n", r);
976 goto failed;
977 }
978 conn->signature_auth_len = secctx_sizes.cbMaxSignature;
979 conn->encryption_auth_len = secctx_sizes.cbSecurityTrailer;
980 }
981
982 *out_size = out.cbBuffer;
983 return RPC_S_OK;
984
985 failed:
986 *out_size = 0;
987 return ERROR_ACCESS_DENIED; /* FIXME: is this correct? */
988 }
989
990 /***********************************************************************
991 * RPCRT4_ClientConnectionAuth (internal)
992 */
993 RPC_STATUS RPCRT4_ClientConnectionAuth(RpcConnection* conn, BYTE *challenge,
994 ULONG count)
995 {
996 RpcPktHdr *resp_hdr;
997 RPC_STATUS status;
998 unsigned char *out_buffer;
999 unsigned int out_len = 0;
1000
1001 TRACE("challenge %s, %d bytes\n", challenge, count);
1002
1003 status = rpcrt4_conn_authorize(conn, FALSE, challenge, count, NULL, &out_len);
1004 if (status) return status;
1005 out_buffer = HeapAlloc(GetProcessHeap(), 0, out_len);
1006 if (!out_buffer) return RPC_S_OUT_OF_RESOURCES;
1007 status = rpcrt4_conn_authorize(conn, FALSE, challenge, count, out_buffer, &out_len);
1008 if (status) return status;
1009
1010 resp_hdr = RPCRT4_BuildAuthHeader(NDR_LOCAL_DATA_REPRESENTATION);
1011
1012 if (resp_hdr)
1013 status = RPCRT4_SendWithAuth(conn, resp_hdr, NULL, 0, out_buffer, out_len);
1014 else
1015 status = RPC_S_OUT_OF_RESOURCES;
1016
1017 HeapFree(GetProcessHeap(), 0, out_buffer);
1018 RPCRT4_FreeHeader(resp_hdr);
1019
1020 return status;
1021 }
1022
1023 /***********************************************************************
1024 * RPCRT4_ServerConnectionAuth (internal)
1025 */
1026 RPC_STATUS RPCRT4_ServerConnectionAuth(RpcConnection* conn,
1027 BOOL start,
1028 RpcAuthVerifier *auth_data_in,
1029 ULONG auth_length_in,
1030 unsigned char **auth_data_out,
1031 ULONG *auth_length_out)
1032 {
1033 unsigned char *out_buffer;
1034 unsigned int out_size;
1035 RPC_STATUS status;
1036
1037 if (start)
1038 {
1039 /* remove any existing authentication information */
1040 if (conn->AuthInfo)
1041 {
1042 RpcAuthInfo_Release(conn->AuthInfo);
1043 conn->AuthInfo = NULL;
1044 }
1045 if (SecIsValidHandle(&conn->ctx))
1046 {
1047 DeleteSecurityContext(&conn->ctx);
1048 SecInvalidateHandle(&conn->ctx);
1049 }
1050 if (auth_length_in >= sizeof(RpcAuthVerifier))
1051 {
1052 CredHandle cred;
1053 TimeStamp exp;
1054 ULONG max_token;
1055
1056 status = RPCRT4_ServerGetRegisteredAuthInfo(
1057 auth_data_in->auth_type, &cred, &exp, &max_token);
1058 if (status != RPC_S_OK)
1059 {
1060 ERR("unknown authentication service %u\n", auth_data_in->auth_type);
1061 return status;
1062 }
1063
1064 status = RpcAuthInfo_Create(auth_data_in->auth_level,
1065 auth_data_in->auth_type, cred, exp,
1066 max_token, NULL, &conn->AuthInfo);
1067 if (status != RPC_S_OK)
1068 return status;
1069
1070 /* FIXME: should auth_data_in->auth_context_id be checked in the !start case? */
1071 conn->auth_context_id = auth_data_in->auth_context_id;
1072 }
1073 }
1074
1075 if (auth_length_in < sizeof(RpcAuthVerifier))
1076 return RPC_S_OK;
1077
1078 if (!conn->AuthInfo)
1079 /* should have filled in authentication info by now */
1080 return RPC_S_PROTOCOL_ERROR;
1081
1082 status = rpcrt4_conn_authorize(
1083 conn, start, (unsigned char *)(auth_data_in + 1),
1084 auth_length_in - sizeof(RpcAuthVerifier), NULL, &out_size);
1085 if (status) return status;
1086
1087 out_buffer = HeapAlloc(GetProcessHeap(), 0, out_size);
1088 if (!out_buffer) return RPC_S_OUT_OF_RESOURCES;
1089
1090 status = rpcrt4_conn_authorize(
1091 conn, start, (unsigned char *)(auth_data_in + 1),
1092 auth_length_in - sizeof(RpcAuthVerifier), out_buffer, &out_size);
1093 if (status != RPC_S_OK)
1094 {
1095 HeapFree(GetProcessHeap(), 0, out_buffer);
1096 return status;
1097 }
1098
1099 if (out_size && !auth_length_out)
1100 {
1101 ERR("expected authentication to be complete but SSP returned data of "
1102 "%u bytes to be sent back to client\n", out_size);
1103 HeapFree(GetProcessHeap(), 0, out_buffer);
1104 return RPC_S_SEC_PKG_ERROR;
1105 }
1106 else
1107 {
1108 *auth_data_out = out_buffer;
1109 *auth_length_out = out_size;
1110 }
1111
1112 return status;
1113 }
1114
1115 /***********************************************************************
1116 * RPCRT4_default_is_authorized (internal)
1117 *
1118 * Has a connection started the process of authorizing with the server?
1119 */
1120 BOOL RPCRT4_default_is_authorized(RpcConnection *Connection)
1121 {
1122 return Connection->AuthInfo && SecIsValidHandle(&Connection->ctx);
1123 }
1124
1125 /***********************************************************************
1126 * RPCRT4_default_impersonate_client (internal)
1127 *
1128 */
1129 RPC_STATUS RPCRT4_default_impersonate_client(RpcConnection *conn)
1130 {
1131 SECURITY_STATUS sec_status;
1132
1133 TRACE("(%p)\n", conn);
1134
1135 if (!conn->AuthInfo || !SecIsValidHandle(&conn->ctx))
1136 return RPC_S_NO_CONTEXT_AVAILABLE;
1137 sec_status = ImpersonateSecurityContext(&conn->ctx);
1138 if (sec_status != SEC_E_OK)
1139 WARN("ImpersonateSecurityContext returned 0x%08x\n", sec_status);
1140 switch (sec_status)
1141 {
1142 case SEC_E_UNSUPPORTED_FUNCTION:
1143 return RPC_S_CANNOT_SUPPORT;
1144 case SEC_E_NO_IMPERSONATION:
1145 return RPC_S_NO_CONTEXT_AVAILABLE;
1146 case SEC_E_OK:
1147 return RPC_S_OK;
1148 default:
1149 return RPC_S_SEC_PKG_ERROR;
1150 }
1151 }
1152
1153 /***********************************************************************
1154 * RPCRT4_default_revert_to_self (internal)
1155 *
1156 */
1157 RPC_STATUS RPCRT4_default_revert_to_self(RpcConnection *conn)
1158 {
1159 SECURITY_STATUS sec_status;
1160
1161 TRACE("(%p)\n", conn);
1162
1163 if (!conn->AuthInfo || !SecIsValidHandle(&conn->ctx))
1164 return RPC_S_NO_CONTEXT_AVAILABLE;
1165 sec_status = RevertSecurityContext(&conn->ctx);
1166 if (sec_status != SEC_E_OK)
1167 WARN("RevertSecurityContext returned 0x%08x\n", sec_status);
1168 switch (sec_status)
1169 {
1170 case SEC_E_UNSUPPORTED_FUNCTION:
1171 return RPC_S_CANNOT_SUPPORT;
1172 case SEC_E_NO_IMPERSONATION:
1173 return RPC_S_NO_CONTEXT_AVAILABLE;
1174 case SEC_E_OK:
1175 return RPC_S_OK;
1176 default:
1177 return RPC_S_SEC_PKG_ERROR;
1178 }
1179 }
1180
1181 /***********************************************************************
1182 * RPCRT4_default_inquire_auth_client (internal)
1183 *
1184 * Default function to retrieve the authentication details that the client
1185 * is using to call the server.
1186 */
1187 RPC_STATUS RPCRT4_default_inquire_auth_client(
1188 RpcConnection *conn, RPC_AUTHZ_HANDLE *privs, RPC_WSTR *server_princ_name,
1189 ULONG *authn_level, ULONG *authn_svc, ULONG *authz_svc, ULONG flags)
1190 {
1191 if (!conn->AuthInfo) return RPC_S_BINDING_HAS_NO_AUTH;
1192
1193 if (privs)
1194 {
1195 FIXME("privs not implemented\n");
1196 *privs = NULL;
1197 }
1198 if (server_princ_name)
1199 {
1200 *server_princ_name = RPCRT4_strdupW(conn->AuthInfo->server_principal_name);
1201 if (!*server_princ_name) return ERROR_OUTOFMEMORY;
1202 }
1203 if (authn_level) *authn_level = conn->AuthInfo->AuthnLevel;
1204 if (authn_svc) *authn_svc = conn->AuthInfo->AuthnSvc;
1205 if (authz_svc)
1206 {
1207 FIXME("authorization service not implemented\n");
1208 *authz_svc = RPC_C_AUTHZ_NONE;
1209 }
1210 if (flags)
1211 FIXME("flags 0x%x not implemented\n", flags);
1212
1213 return RPC_S_OK;
1214 }
1215
1216 /***********************************************************************
1217 * RPCRT4_Send (internal)
1218 *
1219 * Transmit a packet over connection in acceptable fragments.
1220 */
1221 RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
1222 void *Buffer, unsigned int BufferLength)
1223 {
1224 RPC_STATUS r;
1225
1226 if (packet_does_auth_negotiation(Header) &&
1227 Connection->AuthInfo &&
1228 !rpcrt4_conn_is_authorized(Connection))
1229 {
1230 unsigned int out_size = 0;
1231 unsigned char *out_buffer;
1232
1233 r = rpcrt4_conn_authorize(Connection, TRUE, NULL, 0, NULL, &out_size);
1234 if (r != RPC_S_OK) return r;
1235
1236 out_buffer = HeapAlloc(GetProcessHeap(), 0, out_size);
1237 if (!out_buffer) return RPC_S_OUT_OF_RESOURCES;
1238
1239 /* tack on a negotiate packet */
1240 r = rpcrt4_conn_authorize(Connection, TRUE, NULL, 0, out_buffer, &out_size);
1241 if (r == RPC_S_OK)
1242 r = RPCRT4_SendWithAuth(Connection, Header, Buffer, BufferLength, out_buffer, out_size);
1243
1244 HeapFree(GetProcessHeap(), 0, out_buffer);
1245 }
1246 else
1247 r = RPCRT4_SendWithAuth(Connection, Header, Buffer, BufferLength, NULL, 0);
1248
1249 return r;
1250 }
1251
1252 /* validates version and frag_len fields */
1253 RPC_STATUS RPCRT4_ValidateCommonHeader(const RpcPktCommonHdr *hdr)
1254 {
1255 DWORD hdr_length;
1256
1257 /* verify if the header really makes sense */
1258 if (hdr->rpc_ver != RPC_VER_MAJOR ||
1259 hdr->rpc_ver_minor != RPC_VER_MINOR)
1260 {
1261 WARN("unhandled packet version\n");
1262 return RPC_S_PROTOCOL_ERROR;
1263 }
1264
1265 hdr_length = RPCRT4_GetHeaderSize((const RpcPktHdr*)hdr);
1266 if (hdr_length == 0)
1267 {
1268 WARN("header length == 0\n");
1269 return RPC_S_PROTOCOL_ERROR;
1270 }
1271
1272 if (hdr->frag_len < hdr_length)
1273 {
1274 WARN("bad frag length %d\n", hdr->frag_len);
1275 return RPC_S_PROTOCOL_ERROR;
1276 }
1277
1278 return RPC_S_OK;
1279 }
1280
1281 /***********************************************************************
1282 * RPCRT4_default_receive_fragment (internal)
1283 *
1284 * Receive a fragment from a connection.
1285 */
1286 static RPC_STATUS RPCRT4_default_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
1287 {
1288 RPC_STATUS status;
1289 DWORD hdr_length;
1290 LONG dwRead;
1291 RpcPktCommonHdr common_hdr;
1292
1293 *Header = NULL;
1294 *Payload = NULL;
1295
1296 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
1297
1298 /* read packet common header */
1299 dwRead = rpcrt4_conn_read(Connection, &common_hdr, sizeof(common_hdr));
1300 if (dwRead != sizeof(common_hdr)) {
1301 WARN("Short read of header, %d bytes\n", dwRead);
1302 status = RPC_S_CALL_FAILED;
1303 goto fail;
1304 }
1305
1306 status = RPCRT4_ValidateCommonHeader(&common_hdr);
1307 if (status != RPC_S_OK) goto fail;
1308
1309 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
1310 if (hdr_length == 0) {
1311 WARN("header length == 0\n");
1312 status = RPC_S_PROTOCOL_ERROR;
1313 goto fail;
1314 }
1315
1316 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
1317 memcpy(*Header, &common_hdr, sizeof(common_hdr));
1318
1319 /* read the rest of packet header */
1320 dwRead = rpcrt4_conn_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
1321 if (dwRead != hdr_length - sizeof(common_hdr)) {
1322 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
1323 status = RPC_S_CALL_FAILED;
1324 goto fail;
1325 }
1326
1327 if (common_hdr.frag_len - hdr_length)
1328 {
1329 *Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
1330 if (!*Payload)
1331 {
1332 status = RPC_S_OUT_OF_RESOURCES;
1333 goto fail;
1334 }
1335
1336 dwRead = rpcrt4_conn_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
1337 if (dwRead != common_hdr.frag_len - hdr_length)
1338 {
1339 WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
1340 status = RPC_S_CALL_FAILED;
1341 goto fail;
1342 }
1343 }
1344 else
1345 *Payload = NULL;
1346
1347 /* success */
1348 status = RPC_S_OK;
1349
1350 fail:
1351 if (status != RPC_S_OK) {
1352 RPCRT4_FreeHeader(*Header);
1353 *Header = NULL;
1354 HeapFree(GetProcessHeap(), 0, *Payload);
1355 *Payload = NULL;
1356 }
1357 return status;
1358 }
1359
1360 static RPC_STATUS RPCRT4_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
1361 {
1362 if (Connection->ops->receive_fragment)
1363 return Connection->ops->receive_fragment(Connection, Header, Payload);
1364 else
1365 return RPCRT4_default_receive_fragment(Connection, Header, Payload);
1366 }
1367
1368 /***********************************************************************
1369 * RPCRT4_ReceiveWithAuth (internal)
1370 *
1371 * Receive a packet from connection, merge the fragments and return the auth
1372 * data.
1373 */
1374 RPC_STATUS RPCRT4_ReceiveWithAuth(RpcConnection *Connection, RpcPktHdr **Header,
1375 PRPC_MESSAGE pMsg,
1376 unsigned char **auth_data_out,
1377 ULONG *auth_length_out)
1378 {
1379 RPC_STATUS status;
1380 DWORD hdr_length;
1381 unsigned short first_flag;
1382 ULONG data_length;
1383 ULONG buffer_length;
1384 ULONG auth_length = 0;
1385 unsigned char *auth_data = NULL;
1386 RpcPktHdr *CurrentHeader = NULL;
1387 void *payload = NULL;
1388
1389 *Header = NULL;
1390 pMsg->Buffer = NULL;
1391 if (auth_data_out) *auth_data_out = NULL;
1392 if (auth_length_out) *auth_length_out = 0;
1393
1394 TRACE("(%p, %p, %p, %p)\n", Connection, Header, pMsg, auth_data_out);
1395
1396 RPCRT4_SetThreadCurrentConnection(Connection);
1397
1398 status = RPCRT4_receive_fragment(Connection, Header, &payload);
1399 if (status != RPC_S_OK) goto fail;
1400
1401 hdr_length = RPCRT4_GetHeaderSize(*Header);
1402
1403 /* read packet body */
1404 switch ((*Header)->common.ptype) {
1405 case PKT_RESPONSE:
1406 pMsg->BufferLength = (*Header)->response.alloc_hint;
1407 break;
1408 case PKT_REQUEST:
1409 pMsg->BufferLength = (*Header)->request.alloc_hint;
1410 break;
1411 default:
1412 pMsg->BufferLength = (*Header)->common.frag_len - hdr_length - RPC_AUTH_VERIFIER_LEN(&(*Header)->common);
1413 }
1414
1415 TRACE("buffer length = %u\n", pMsg->BufferLength);
1416
1417 pMsg->Buffer = I_RpcAllocate(pMsg->BufferLength);
1418 if (!pMsg->Buffer)
1419 {
1420 status = ERROR_OUTOFMEMORY;
1421 goto fail;
1422 }
1423
1424 first_flag = RPC_FLG_FIRST;
1425 auth_length = (*Header)->common.auth_len;
1426 if (auth_length) {
1427 auth_data = HeapAlloc(GetProcessHeap(), 0, RPC_AUTH_VERIFIER_LEN(&(*Header)->common));
1428 if (!auth_data) {
1429 status = RPC_S_OUT_OF_RESOURCES;
1430 goto fail;
1431 }
1432 }
1433 CurrentHeader = *Header;
1434 buffer_length = 0;
1435 while (TRUE)
1436 {
1437 unsigned int header_auth_len = RPC_AUTH_VERIFIER_LEN(&CurrentHeader->common);
1438
1439 /* verify header fields */
1440
1441 if ((CurrentHeader->common.frag_len < hdr_length) ||
1442 (CurrentHeader->common.frag_len - hdr_length < header_auth_len)) {
1443 WARN("frag_len %d too small for hdr_length %d and auth_len %d\n",
1444 CurrentHeader->common.frag_len, hdr_length, CurrentHeader->common.auth_len);
1445 status = RPC_S_PROTOCOL_ERROR;
1446 goto fail;
1447 }
1448
1449 if (CurrentHeader->common.auth_len != auth_length) {
1450 WARN("auth_len header field changed from %d to %d\n",
1451 auth_length, CurrentHeader->common.auth_len);
1452 status = RPC_S_PROTOCOL_ERROR;
1453 goto fail;
1454 }
1455
1456 if ((CurrentHeader->common.flags & RPC_FLG_FIRST) != first_flag) {
1457 TRACE("invalid packet flags\n");
1458 status = RPC_S_PROTOCOL_ERROR;
1459 goto fail;
1460 }
1461
1462 data_length = CurrentHeader->common.frag_len - hdr_length - header_auth_len;
1463 if (data_length + buffer_length > pMsg->BufferLength) {
1464 TRACE("allocation hint exceeded, new buffer length = %d\n",
1465 data_length + buffer_length);
1466 pMsg->BufferLength = data_length + buffer_length;
1467 status = I_RpcReAllocateBuffer(pMsg);
1468 if (status != RPC_S_OK) goto fail;
1469 }
1470
1471 memcpy((unsigned char *)pMsg->Buffer + buffer_length, payload, data_length);
1472
1473 if (header_auth_len) {
1474 if (header_auth_len < sizeof(RpcAuthVerifier) ||
1475 header_auth_len > RPC_AUTH_VERIFIER_LEN(&(*Header)->common)) {
1476 WARN("bad auth verifier length %d\n", header_auth_len);
1477 status = RPC_S_PROTOCOL_ERROR;
1478 goto fail;
1479 }
1480
1481 /* FIXME: we should accumulate authentication data for the bind,
1482 * bind_ack, alter_context and alter_context_response if necessary.
1483 * however, the details of how this is done is very sketchy in the
1484 * DCE/RPC spec. for all other packet types that have authentication
1485 * verifier data then it is just duplicated in all the fragments */
1486 memcpy(auth_data, (unsigned char *)payload + data_length, header_auth_len);
1487
1488 /* these packets are handled specially, not by the generic SecurePacket
1489 * function */
1490 if (!packet_does_auth_negotiation(*Header) && rpcrt4_conn_is_authorized(Connection))
1491 {
1492 status = rpcrt4_conn_secure_packet(Connection, SECURE_PACKET_RECEIVE,
1493 CurrentHeader, hdr_length,
1494 (unsigned char *)pMsg->Buffer + buffer_length, data_length,
1495 (RpcAuthVerifier *)auth_data,
1496 auth_data + sizeof(RpcAuthVerifier),
1497 header_auth_len - sizeof(RpcAuthVerifier));
1498 if (status != RPC_S_OK) goto fail;
1499 }
1500 }
1501
1502 buffer_length += data_length;
1503 if (!(CurrentHeader->common.flags & RPC_FLG_LAST)) {
1504 TRACE("next header\n");
1505
1506 if (*Header != CurrentHeader)
1507 {
1508 RPCRT4_FreeHeader(CurrentHeader);
1509 CurrentHeader = NULL;
1510 }
1511 HeapFree(GetProcessHeap(), 0, payload);
1512 payload = NULL;
1513
1514 status = RPCRT4_receive_fragment(Connection, &CurrentHeader, &payload);
1515 if (status != RPC_S_OK) goto fail;
1516
1517 first_flag = 0;
1518 } else {
1519 break;
1520 }
1521 }
1522 pMsg->BufferLength = buffer_length;
1523
1524 /* success */
1525 status = RPC_S_OK;
1526
1527 fail:
1528 RPCRT4_SetThreadCurrentConnection(NULL);
1529 if (CurrentHeader != *Header)
1530 RPCRT4_FreeHeader(CurrentHeader);
1531 if (status != RPC_S_OK) {
1532 I_RpcFree(pMsg->Buffer);
1533 pMsg->Buffer = NULL;
1534 RPCRT4_FreeHeader(*Header);
1535 *Header = NULL;
1536 }
1537 if (auth_data_out && status == RPC_S_OK) {
1538 *auth_length_out = auth_length;
1539 *auth_data_out = auth_data;
1540 }
1541 else
1542 HeapFree(GetProcessHeap(), 0, auth_data);
1543 HeapFree(GetProcessHeap(), 0, payload);
1544 return status;
1545 }
1546
1547 /***********************************************************************
1548 * RPCRT4_Receive (internal)
1549 *
1550 * Receive a packet from connection and merge the fragments.
1551 */
1552 static RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
1553 PRPC_MESSAGE pMsg)
1554 {
1555 return RPCRT4_ReceiveWithAuth(Connection, Header, pMsg, NULL, NULL);
1556 }
1557
1558 /***********************************************************************
1559 * I_RpcNegotiateTransferSyntax [RPCRT4.@]
1560 *
1561 * Negotiates the transfer syntax used by a client connection by connecting
1562 * to the server.
1563 *
1564 * PARAMS
1565 * pMsg [I] RPC Message structure.
1566 * pAsync [I] Asynchronous state to set.
1567 *
1568 * RETURNS
1569 * Success: RPC_S_OK.
1570 * Failure: Any error code.
1571 */
1572 RPC_STATUS WINAPI I_RpcNegotiateTransferSyntax(PRPC_MESSAGE pMsg)
1573 {
1574 RpcBinding* bind = pMsg->Handle;
1575 RpcConnection* conn;
1576 RPC_STATUS status = RPC_S_OK;
1577
1578 TRACE("(%p)\n", pMsg);
1579
1580 if (!bind || bind->server)
1581 {
1582 ERR("no binding\n");
1583 return RPC_S_INVALID_BINDING;
1584 }
1585
1586 /* if we already have a connection, we don't need to negotiate again */
1587 if (!pMsg->ReservedForRuntime)
1588 {
1589 RPC_CLIENT_INTERFACE *cif = pMsg->RpcInterfaceInformation;
1590 if (!cif) return RPC_S_INTERFACE_NOT_FOUND;
1591
1592 if (!bind->Endpoint || !bind->Endpoint[0])
1593 {
1594 TRACE("automatically resolving partially bound binding\n");
1595 status = RpcEpResolveBinding(bind, cif);
1596 if (status != RPC_S_OK) return status;
1597 }
1598
1599 status = RPCRT4_OpenBinding(bind, &conn, &cif->TransferSyntax,
1600 &cif->InterfaceId);
1601
1602 if (status == RPC_S_OK)
1603 {
1604 pMsg->ReservedForRuntime = conn;
1605 RPCRT4_AddRefBinding(bind);
1606 }
1607 }
1608
1609 return status;
1610 }
1611
1612 /***********************************************************************
1613 * I_RpcGetBuffer [RPCRT4.@]
1614 *
1615 * Allocates a buffer for use by I_RpcSend or I_RpcSendReceive and binds to the
1616 * server interface.
1617 *
1618 * PARAMS
1619 * pMsg [I/O] RPC message information.
1620 *
1621 * RETURNS
1622 * Success: RPC_S_OK.
1623 * Failure: RPC_S_INVALID_BINDING if pMsg->Handle is invalid.
1624 * RPC_S_SERVER_UNAVAILABLE if unable to connect to server.
1625 * ERROR_OUTOFMEMORY if buffer allocation failed.
1626 *
1627 * NOTES
1628 * The pMsg->BufferLength field determines the size of the buffer to allocate,
1629 * in bytes.
1630 *
1631 * Use I_RpcFreeBuffer() to unbind from the server and free the message buffer.
1632 *
1633 * SEE ALSO
1634 * I_RpcFreeBuffer(), I_RpcSend(), I_RpcReceive(), I_RpcSendReceive().
1635 */
1636 RPC_STATUS WINAPI I_RpcGetBuffer(PRPC_MESSAGE pMsg)
1637 {
1638 RPC_STATUS status;
1639 RpcBinding* bind = pMsg->Handle;
1640
1641 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
1642
1643 if (!bind)
1644 {
1645 ERR("no binding\n");
1646 return RPC_S_INVALID_BINDING;
1647 }
1648
1649 pMsg->Buffer = I_RpcAllocate(pMsg->BufferLength);
1650 TRACE("Buffer=%p\n", pMsg->Buffer);
1651
1652 if (!pMsg->Buffer)
1653 return ERROR_OUTOFMEMORY;
1654
1655 if (!bind->server)
1656 {
1657 status = I_RpcNegotiateTransferSyntax(pMsg);
1658 if (status != RPC_S_OK)
1659 I_RpcFree(pMsg->Buffer);
1660 }
1661 else
1662 status = RPC_S_OK;
1663
1664 return status;
1665 }
1666
1667 /***********************************************************************
1668 * I_RpcReAllocateBuffer (internal)
1669 */
1670 static RPC_STATUS I_RpcReAllocateBuffer(PRPC_MESSAGE pMsg)
1671 {
1672 TRACE("(%p): BufferLength=%d\n", pMsg, pMsg->BufferLength);
1673 pMsg->Buffer = HeapReAlloc(GetProcessHeap(), 0, pMsg->Buffer, pMsg->BufferLength);
1674
1675 TRACE("Buffer=%p\n", pMsg->Buffer);
1676 return pMsg->Buffer ? RPC_S_OK : ERROR_OUTOFMEMORY;
1677 }
1678
1679 /***********************************************************************
1680 * I_RpcFreeBuffer [RPCRT4.@]
1681 *
1682 * Frees a buffer allocated by I_RpcGetBuffer or I_RpcReceive and unbinds from
1683 * the server interface.
1684 *
1685 * PARAMS
1686 * pMsg [I/O] RPC message information.
1687 *
1688 * RETURNS
1689 * RPC_S_OK.
1690 *
1691 * SEE ALSO
1692 * I_RpcGetBuffer(), I_RpcReceive().
1693 */
1694 RPC_STATUS WINAPI I_RpcFreeBuffer(PRPC_MESSAGE pMsg)
1695 {
1696 RpcBinding* bind = pMsg->Handle;
1697
1698 TRACE("(%p) Buffer=%p\n", pMsg, pMsg->Buffer);
1699
1700 if (!bind)
1701 {
1702 ERR("no binding\n");
1703 return RPC_S_INVALID_BINDING;
1704 }
1705
1706 if (pMsg->ReservedForRuntime)
1707 {
1708 RpcConnection *conn = pMsg->ReservedForRuntime;
1709 RPCRT4_CloseBinding(bind, conn);
1710 RPCRT4_ReleaseBinding(bind);
1711 pMsg->ReservedForRuntime = NULL;
1712 }
1713 I_RpcFree(pMsg->Buffer);
1714 return RPC_S_OK;
1715 }
1716
1717 static void CALLBACK async_apc_notifier_proc(ULONG_PTR ulParam)
1718 {
1719 RPC_ASYNC_STATE *state = (RPC_ASYNC_STATE *)ulParam;
1720 state->u.APC.NotificationRoutine(state, NULL, state->Event);
1721 }
1722
1723 static DWORD WINAPI async_notifier_proc(LPVOID p)
1724 {
1725 RpcConnection *conn = p;
1726 RPC_ASYNC_STATE *state = conn->async_state;
1727
1728 if (state && conn->ops->wait_for_incoming_data(conn) != -1)
1729 {
1730 state->Event = RpcCallComplete;
1731 switch (state->NotificationType)
1732 {
1733 case RpcNotificationTypeEvent:
1734 TRACE("RpcNotificationTypeEvent %p\n", state->u.hEvent);
1735 SetEvent(state->u.hEvent);
1736 break;
1737 case RpcNotificationTypeApc:
1738 TRACE("RpcNotificationTypeApc %p\n", state->u.APC.hThread);
1739 QueueUserAPC(async_apc_notifier_proc, state->u.APC.hThread, (ULONG_PTR)state);
1740 break;
1741 case RpcNotificationTypeIoc:
1742 TRACE("RpcNotificationTypeIoc %p, 0x%x, 0x%lx, %p\n",
1743 state->u.IOC.hIOPort, state->u.IOC.dwNumberOfBytesTransferred,
1744 state->u.IOC.dwCompletionKey, state->u.IOC.lpOverlapped);
1745 PostQueuedCompletionStatus(state->u.IOC.hIOPort,
1746 state->u.IOC.dwNumberOfBytesTransferred,
1747 state->u.IOC.dwCompletionKey,
1748 state->u.IOC.lpOverlapped);
1749 break;
1750 case RpcNotificationTypeHwnd:
1751 TRACE("RpcNotificationTypeHwnd %p 0x%x\n", state->u.HWND.hWnd,
1752 state->u.HWND.Msg);
1753 PostMessageW(state->u.HWND.hWnd, state->u.HWND.Msg, 0, 0);
1754 break;
1755 case RpcNotificationTypeCallback:
1756 TRACE("RpcNotificationTypeCallback %p\n", state->u.NotificationRoutine);
1757 state->u.NotificationRoutine(state, NULL, state->Event);
1758 break;
1759 case RpcNotificationTypeNone:
1760 TRACE("RpcNotificationTypeNone\n");
1761 break;
1762 default:
1763 FIXME("unknown NotificationType: %d/0x%x\n", state->NotificationType, state->NotificationType);
1764 break;
1765 }
1766 }
1767
1768 return 0;
1769 }
1770
1771 /***********************************************************************
1772 * I_RpcSend [RPCRT4.@]
1773 *
1774 * Sends a message to the server.
1775 *
1776 * PARAMS
1777 * pMsg [I/O] RPC message information.
1778 *
1779 * RETURNS
1780 * Unknown.
1781 *
1782 * NOTES
1783 * The buffer must have been allocated with I_RpcGetBuffer().
1784 *
1785 * SEE ALSO
1786 * I_RpcGetBuffer(), I_RpcReceive(), I_RpcSendReceive().
1787 */
1788 RPC_STATUS WINAPI I_RpcSend(PRPC_MESSAGE pMsg)
1789 {
1790 RpcBinding* bind = pMsg->Handle;
1791 RpcConnection* conn;
1792 RPC_STATUS status;
1793 RpcPktHdr *hdr;
1794
1795 TRACE("(%p)\n", pMsg);
1796 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1797
1798 conn = pMsg->ReservedForRuntime;
1799
1800 hdr = RPCRT4_BuildRequestHeader(pMsg->DataRepresentation,
1801 pMsg->BufferLength,
1802 pMsg->ProcNum & ~RPC_FLAGS_VALID_BIT,
1803 &bind->ObjectUuid);
1804 if (!hdr)
1805 return ERROR_OUTOFMEMORY;
1806 hdr->common.call_id = conn->NextCallId++;
1807
1808 status = RPCRT4_Send(conn, hdr, pMsg->Buffer, pMsg->BufferLength);
1809
1810 RPCRT4_FreeHeader(hdr);
1811
1812 if (status == RPC_S_OK && pMsg->RpcFlags & RPC_BUFFER_ASYNC)
1813 {
1814 if (!QueueUserWorkItem(async_notifier_proc, conn, WT_EXECUTEDEFAULT | WT_EXECUTELONGFUNCTION))
1815 status = RPC_S_OUT_OF_RESOURCES;
1816 }
1817
1818 return status;
1819 }
1820
1821 /* is this status something that the server can't recover from? */
1822 static inline BOOL is_hard_error(RPC_STATUS status)
1823 {
1824 switch (status)
1825 {
1826 case 0: /* user-defined fault */
1827 case ERROR_ACCESS_DENIED:
1828 case ERROR_INVALID_PARAMETER:
1829 case RPC_S_PROTOCOL_ERROR:
1830 case RPC_S_CALL_FAILED:
1831 case RPC_S_CALL_FAILED_DNE:
1832 case RPC_S_SEC_PKG_ERROR:
1833 return TRUE;
1834 default:
1835 return FALSE;
1836 }
1837 }
1838
1839 /***********************************************************************
1840 * I_RpcReceive [RPCRT4.@]
1841 */
1842 RPC_STATUS WINAPI I_RpcReceive(PRPC_MESSAGE pMsg)
1843 {
1844 RpcBinding* bind = pMsg->Handle;
1845 RPC_STATUS status;
1846 RpcPktHdr *hdr = NULL;
1847 RpcConnection *conn;
1848
1849 TRACE("(%p)\n", pMsg);
1850 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1851
1852 conn = pMsg->ReservedForRuntime;
1853 status = RPCRT4_Receive(conn, &hdr, pMsg);
1854 if (status != RPC_S_OK) {
1855 WARN("receive failed with error %x\n", status);
1856 goto fail;
1857 }
1858
1859 switch (hdr->common.ptype) {
1860 case PKT_RESPONSE:
1861 break;
1862 case PKT_FAULT:
1863 ERR ("we got fault packet with status 0x%x\n", hdr->fault.status);
1864 status = NCA2RPC_STATUS(hdr->fault.status);
1865 if (is_hard_error(status))
1866 goto fail;
1867 break;
1868 default:
1869 WARN("bad packet type %d\n", hdr->common.ptype);
1870 status = RPC_S_PROTOCOL_ERROR;
1871 goto fail;
1872 }
1873
1874 /* success */
1875 RPCRT4_FreeHeader(hdr);
1876 return status;
1877
1878 fail:
1879 RPCRT4_FreeHeader(hdr);
1880 RPCRT4_DestroyConnection(conn);
1881 pMsg->ReservedForRuntime = NULL;
1882 return status;
1883 }
1884
1885 /***********************************************************************
1886 * I_RpcSendReceive [RPCRT4.@]
1887 *
1888 * Sends a message to the server and receives the response.
1889 *
1890 * PARAMS
1891 * pMsg [I/O] RPC message information.
1892 *
1893 * RETURNS
1894 * Success: RPC_S_OK.
1895 * Failure: Any error code.
1896 *
1897 * NOTES
1898 * The buffer must have been allocated with I_RpcGetBuffer().
1899 *
1900 * SEE ALSO
1901 * I_RpcGetBuffer(), I_RpcSend(), I_RpcReceive().
1902 */
1903 RPC_STATUS WINAPI I_RpcSendReceive(PRPC_MESSAGE pMsg)
1904 {
1905 RPC_STATUS status;
1906 void *original_buffer;
1907
1908 TRACE("(%p)\n", pMsg);
1909
1910 original_buffer = pMsg->Buffer;
1911 status = I_RpcSend(pMsg);
1912 if (status == RPC_S_OK)
1913 status = I_RpcReceive(pMsg);
1914 /* free the buffer replaced by a new buffer in I_RpcReceive */
1915 if (status == RPC_S_OK)
1916 I_RpcFree(original_buffer);
1917 return status;
1918 }
1919
1920 /***********************************************************************
1921 * I_RpcAsyncSetHandle [RPCRT4.@]
1922 *
1923 * Sets the asynchronous state of the handle contained in the RPC message
1924 * structure.
1925 *
1926 * PARAMS
1927 * pMsg [I] RPC Message structure.
1928 * pAsync [I] Asynchronous state to set.
1929 *
1930 * RETURNS
1931 * Success: RPC_S_OK.
1932 * Failure: Any error code.
1933 */
1934 RPC_STATUS WINAPI I_RpcAsyncSetHandle(PRPC_MESSAGE pMsg, PRPC_ASYNC_STATE pAsync)
1935 {
1936 RpcBinding* bind = pMsg->Handle;
1937 RpcConnection *conn;
1938
1939 TRACE("(%p, %p)\n", pMsg, pAsync);
1940
1941 if (!bind || bind->server || !pMsg->ReservedForRuntime) return RPC_S_INVALID_BINDING;
1942
1943 conn = pMsg->ReservedForRuntime;
1944 conn->async_state = pAsync;
1945
1946 return RPC_S_OK;
1947 }
1948
1949 /***********************************************************************
1950 * I_RpcAsyncAbortCall [RPCRT4.@]
1951 *
1952 * Aborts an asynchronous call.
1953 *
1954 * PARAMS
1955 * pAsync [I] Asynchronous state.
1956 * ExceptionCode [I] Exception code.
1957 *
1958 * RETURNS
1959 * Success: RPC_S_OK.
1960 * Failure: Any error code.
1961 */
1962 RPC_STATUS WINAPI I_RpcAsyncAbortCall(PRPC_ASYNC_STATE pAsync, ULONG ExceptionCode)
1963 {
1964 FIXME("(%p, %d): stub\n", pAsync, ExceptionCode);
1965 return RPC_S_INVALID_ASYNC_HANDLE;
1966 }