Synchronize with trunk r58606.
[reactos.git] / dll / win32 / rpcrt4 / rpc_server.c
1 /*
2 * RPC server API
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006-2008 Robert Shearman (for 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 <config.h>
26 //#include "wine/port.h"
27
28 #include <stdarg.h>
29 //#include <stdio.h>
30 //#include <string.h>
31 #include <assert.h>
32
33 #include <windef.h>
34 #include <winbase.h>
35 //#include "winerror.h"
36
37 #include <rpc.h>
38 //#include "rpcndr.h"
39 //#include "excpt.h"
40
41 #include <wine/debug.h>
42 #include <wine/exception.h>
43
44 #include "rpc_server.h"
45 #include "rpc_assoc.h"
46 #include "rpc_message.h"
47 //#include "rpc_defs.h"
48 #include "ncastatus.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
51
52 typedef struct _RpcPacket
53 {
54 struct _RpcConnection* conn;
55 RpcPktHdr* hdr;
56 RPC_MESSAGE* msg;
57 unsigned char *auth_data;
58 ULONG auth_length;
59 } RpcPacket;
60
61 typedef struct _RpcObjTypeMap
62 {
63 /* FIXME: a hash table would be better. */
64 struct _RpcObjTypeMap *next;
65 UUID Object;
66 UUID Type;
67 } RpcObjTypeMap;
68
69 static RpcObjTypeMap *RpcObjTypeMaps;
70
71 /* list of type RpcServerProtseq */
72 static struct list protseqs = LIST_INIT(protseqs);
73 static struct list server_interfaces = LIST_INIT(server_interfaces);
74 static struct list server_registered_auth_info = LIST_INIT(server_registered_auth_info);
75
76 static CRITICAL_SECTION server_cs;
77 static CRITICAL_SECTION_DEBUG server_cs_debug =
78 {
79 0, 0, &server_cs,
80 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
81 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
82 };
83 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
84
85 static CRITICAL_SECTION listen_cs;
86 static CRITICAL_SECTION_DEBUG listen_cs_debug =
87 {
88 0, 0, &listen_cs,
89 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
90 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
91 };
92 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
93
94 static CRITICAL_SECTION server_auth_info_cs;
95 static CRITICAL_SECTION_DEBUG server_auth_info_cs_debug =
96 {
97 0, 0, &server_auth_info_cs,
98 { &server_auth_info_cs_debug.ProcessLocksList, &server_auth_info_cs_debug.ProcessLocksList },
99 0, 0, { (DWORD_PTR)(__FILE__ ": server_auth_info_cs") }
100 };
101 static CRITICAL_SECTION server_auth_info_cs = { &server_auth_info_cs_debug, -1, 0, 0, 0, 0 };
102
103 /* whether the server is currently listening */
104 static BOOL std_listen;
105 /* number of manual listeners (calls to RpcServerListen) */
106 static LONG manual_listen_count;
107 /* total listeners including auto listeners */
108 static LONG listen_count;
109 /* event set once all listening is finished */
110 static HANDLE listen_done_event;
111
112 static UUID uuid_nil;
113
114 static inline RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
115 {
116 RpcObjTypeMap *rslt = RpcObjTypeMaps;
117 RPC_STATUS dummy;
118
119 while (rslt) {
120 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
121 rslt = rslt->next;
122 }
123
124 return rslt;
125 }
126
127 static inline UUID *LookupObjType(UUID *ObjUuid)
128 {
129 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
130 if (map)
131 return &map->Type;
132 else
133 return &uuid_nil;
134 }
135
136 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
137 const RPC_SYNTAX_IDENTIFIER *if_id,
138 const RPC_SYNTAX_IDENTIFIER *transfer_syntax,
139 BOOL check_object)
140 {
141 UUID* MgrType = NULL;
142 RpcServerInterface* cif;
143 RPC_STATUS status;
144
145 if (check_object)
146 MgrType = LookupObjType(object);
147 EnterCriticalSection(&server_cs);
148 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
149 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
150 (!transfer_syntax || !memcmp(transfer_syntax, &cif->If->TransferSyntax, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
151 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
152 std_listen) {
153 InterlockedIncrement(&cif->CurrentCalls);
154 break;
155 }
156 }
157 LeaveCriticalSection(&server_cs);
158 if (&cif->entry == &server_interfaces) cif = NULL;
159 TRACE("returning %p for object %s, if_id { %d.%d %s }\n", cif,
160 debugstr_guid(object), if_id->SyntaxVersion.MajorVersion,
161 if_id->SyntaxVersion.MinorVersion, debugstr_guid(&if_id->SyntaxGUID));
162 return cif;
163 }
164
165 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
166 {
167 if (!InterlockedDecrement(&sif->CurrentCalls) &&
168 sif->Delete) {
169 /* sif must have been removed from server_interfaces before
170 * CallsCompletedEvent is set */
171 if (sif->CallsCompletedEvent)
172 SetEvent(sif->CallsCompletedEvent);
173 HeapFree(GetProcessHeap(), 0, sif);
174 }
175 }
176
177 static RpcPktHdr *handle_bind_error(RpcConnection *conn, RPC_STATUS error)
178 {
179 unsigned int reject_reason;
180 switch (error)
181 {
182 case RPC_S_SERVER_TOO_BUSY:
183 reject_reason = REJECT_TEMPORARY_CONGESTION;
184 break;
185 case ERROR_OUTOFMEMORY:
186 case RPC_S_OUT_OF_RESOURCES:
187 reject_reason = REJECT_LOCAL_LIMIT_EXCEEDED;
188 break;
189 case RPC_S_PROTOCOL_ERROR:
190 reject_reason = REJECT_PROTOCOL_VERSION_NOT_SUPPORTED;
191 break;
192 case RPC_S_UNKNOWN_AUTHN_SERVICE:
193 reject_reason = REJECT_UNKNOWN_AUTHN_SERVICE;
194 break;
195 case ERROR_ACCESS_DENIED:
196 reject_reason = REJECT_INVALID_CHECKSUM;
197 break;
198 default:
199 FIXME("unexpected status value %d\n", error);
200 /* fall through */
201 case RPC_S_INVALID_BOUND:
202 reject_reason = REJECT_REASON_NOT_SPECIFIED;
203 break;
204 }
205 return RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
206 RPC_VER_MAJOR, RPC_VER_MINOR,
207 reject_reason);
208 }
209
210 static RPC_STATUS process_bind_packet_no_send(
211 RpcConnection *conn, RpcPktBindHdr *hdr, RPC_MESSAGE *msg,
212 unsigned char *auth_data, ULONG auth_length, RpcPktHdr **ack_response,
213 unsigned char **auth_data_out, ULONG *auth_length_out)
214 {
215 RPC_STATUS status;
216 RpcContextElement *ctxt_elem;
217 unsigned int i;
218 RpcResult *results;
219
220 /* validate data */
221 for (i = 0, ctxt_elem = msg->Buffer;
222 i < hdr->num_elements;
223 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
224 {
225 if (((char *)ctxt_elem - (char *)msg->Buffer) > msg->BufferLength ||
226 ((char *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes] - (char *)msg->Buffer) > msg->BufferLength)
227 {
228 ERR("inconsistent data in packet - packet length %d, num elements %d\n",
229 msg->BufferLength, hdr->num_elements);
230 return RPC_S_INVALID_BOUND;
231 }
232 }
233
234 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
235 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
236 conn->server_binding)
237 {
238 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
239
240 return RPC_S_INVALID_BOUND;
241 }
242
243 results = HeapAlloc(GetProcessHeap(), 0,
244 hdr->num_elements * sizeof(*results));
245 if (!results)
246 return RPC_S_OUT_OF_RESOURCES;
247
248 for (i = 0, ctxt_elem = (RpcContextElement *)msg->Buffer;
249 i < hdr->num_elements;
250 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
251 {
252 RpcServerInterface* sif = NULL;
253 unsigned int j;
254
255 for (j = 0; !sif && j < ctxt_elem->num_syntaxes; j++)
256 {
257 sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
258 &ctxt_elem->transfer_syntaxes[j], FALSE);
259 if (sif)
260 break;
261 }
262 if (sif)
263 {
264 RPCRT4_release_server_interface(sif);
265 TRACE("accepting bind request on connection %p for %s\n", conn,
266 debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
267 results[i].result = RESULT_ACCEPT;
268 results[i].reason = REASON_NONE;
269 results[i].transfer_syntax = ctxt_elem->transfer_syntaxes[j];
270
271 /* save the interface for later use */
272 /* FIXME: save linked list */
273 conn->ActiveInterface = ctxt_elem->abstract_syntax;
274 }
275 else if ((sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
276 NULL, FALSE)) != NULL)
277 {
278 RPCRT4_release_server_interface(sif);
279 TRACE("not accepting bind request on connection %p for %s - no transfer syntaxes supported\n",
280 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
281 results[i].result = RESULT_PROVIDER_REJECTION;
282 results[i].reason = REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED;
283 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
284 }
285 else
286 {
287 TRACE("not accepting bind request on connection %p for %s - abstract syntax not supported\n",
288 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
289 results[i].result = RESULT_PROVIDER_REJECTION;
290 results[i].reason = REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED;
291 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
292 }
293 }
294
295 /* create temporary binding */
296 status = RPCRT4_MakeBinding(&conn->server_binding, conn);
297 if (status != RPC_S_OK)
298 {
299 HeapFree(GetProcessHeap(), 0, results);
300 return status;
301 }
302
303 status = RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
304 conn->NetworkAddr, conn->Endpoint,
305 conn->NetworkOptions,
306 hdr->assoc_gid,
307 &conn->server_binding->Assoc);
308 if (status != RPC_S_OK)
309 {
310 HeapFree(GetProcessHeap(), 0, results);
311 return status;
312 }
313
314 if (auth_length)
315 {
316 status = RPCRT4_ServerConnectionAuth(conn, TRUE,
317 (RpcAuthVerifier *)auth_data,
318 auth_length, auth_data_out,
319 auth_length_out);
320 if (status != RPC_S_OK)
321 {
322 HeapFree(GetProcessHeap(), 0, results);
323 return status;
324 }
325 }
326
327 *ack_response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
328 RPC_MAX_PACKET_SIZE,
329 RPC_MAX_PACKET_SIZE,
330 conn->server_binding->Assoc->assoc_group_id,
331 conn->Endpoint, hdr->num_elements,
332 results);
333 HeapFree(GetProcessHeap(), 0, results);
334
335 if (*ack_response)
336 conn->MaxTransmissionSize = hdr->max_tsize;
337 else
338 status = RPC_S_OUT_OF_RESOURCES;
339
340 return status;
341 }
342
343 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr,
344 RPC_MESSAGE *msg,
345 unsigned char *auth_data,
346 ULONG auth_length)
347 {
348 RPC_STATUS status;
349 RpcPktHdr *response = NULL;
350 unsigned char *auth_data_out = NULL;
351 ULONG auth_length_out = 0;
352
353 status = process_bind_packet_no_send(conn, hdr, msg, auth_data, auth_length,
354 &response, &auth_data_out,
355 &auth_length_out);
356 if (status != RPC_S_OK)
357 response = handle_bind_error(conn, status);
358 if (response)
359 status = RPCRT4_SendWithAuth(conn, response, NULL, 0, auth_data_out, auth_length_out);
360 else
361 status = ERROR_OUTOFMEMORY;
362 RPCRT4_FreeHeader(response);
363
364 return status;
365 }
366
367
368 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
369 {
370 RPC_STATUS status;
371 RpcPktHdr *response = NULL;
372 RpcServerInterface* sif;
373 RPC_DISPATCH_FUNCTION func;
374 BOOL exception;
375 UUID *object_uuid;
376 NDR_SCONTEXT context_handle;
377 void *buf = msg->Buffer;
378
379 /* fail if the connection isn't bound with an interface */
380 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
381 /* FIXME: should send BindNack instead */
382 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
383 status);
384
385 RPCRT4_Send(conn, response, NULL, 0);
386 RPCRT4_FreeHeader(response);
387 return RPC_S_OK;
388 }
389
390 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
391 object_uuid = (UUID*)(hdr + 1);
392 } else {
393 object_uuid = NULL;
394 }
395
396 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, NULL, TRUE);
397 if (!sif) {
398 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
399 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
400 NCA_S_UNK_IF);
401
402 RPCRT4_Send(conn, response, NULL, 0);
403 RPCRT4_FreeHeader(response);
404 return RPC_S_OK;
405 }
406 msg->RpcInterfaceInformation = sif->If;
407 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
408 msg->ManagerEpv = sif->MgrEpv;
409 if (object_uuid != NULL) {
410 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
411 }
412
413 /* find dispatch function */
414 msg->ProcNum = hdr->opnum;
415 if (sif->Flags & RPC_IF_OLE) {
416 /* native ole32 always gives us a dispatch table with a single entry
417 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
418 func = *sif->If->DispatchTable->DispatchTable;
419 } else {
420 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
421 WARN("invalid procnum (%d/%d)\n", msg->ProcNum, sif->If->DispatchTable->DispatchTableCount);
422 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
423 NCA_S_OP_RNG_ERROR);
424
425 RPCRT4_Send(conn, response, NULL, 0);
426 RPCRT4_FreeHeader(response);
427 }
428 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
429 }
430
431 /* put in the drep. FIXME: is this more universally applicable?
432 perhaps we should move this outward... */
433 msg->DataRepresentation =
434 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
435 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
436
437 exception = FALSE;
438
439 /* dispatch */
440 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
441 __TRY {
442 if (func) func(msg);
443 } __EXCEPT_ALL {
444 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
445 exception = TRUE;
446 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
447 status = ERROR_NOACCESS;
448 else
449 status = GetExceptionCode();
450 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
451 RPC2NCA_STATUS(status));
452 } __ENDTRY
453 RPCRT4_SetThreadCurrentCallHandle(NULL);
454
455 /* release any unmarshalled context handles */
456 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
457 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
458
459 if (!exception)
460 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
461 msg->BufferLength);
462
463 /* send response packet */
464 if (response) {
465 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
466 exception ? 0 : msg->BufferLength);
467 RPCRT4_FreeHeader(response);
468 } else
469 ERR("out of memory\n");
470
471 msg->RpcInterfaceInformation = NULL;
472 RPCRT4_release_server_interface(sif);
473
474 if (msg->Buffer == buf) buf = NULL;
475 TRACE("freeing Buffer=%p\n", buf);
476 I_RpcFree(buf);
477
478 return status;
479 }
480
481 static RPC_STATUS process_auth3_packet(RpcConnection *conn,
482 RpcPktCommonHdr *hdr,
483 RPC_MESSAGE *msg,
484 unsigned char *auth_data,
485 ULONG auth_length)
486 {
487 RPC_STATUS status;
488
489 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
490 !auth_length || msg->BufferLength != 0)
491 status = RPC_S_PROTOCOL_ERROR;
492 else
493 {
494 status = RPCRT4_ServerConnectionAuth(conn, FALSE,
495 (RpcAuthVerifier *)auth_data,
496 auth_length, NULL, NULL);
497 }
498
499 /* FIXME: client doesn't expect a response to this message so must store
500 * status in connection so that fault packet can be returned when next
501 * packet is received */
502
503 return RPC_S_OK;
504 }
505
506 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr,
507 RPC_MESSAGE* msg, unsigned char *auth_data,
508 ULONG auth_length)
509 {
510 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
511
512 switch (hdr->common.ptype) {
513 case PKT_BIND:
514 TRACE("got bind packet\n");
515 process_bind_packet(conn, &hdr->bind, msg, auth_data, auth_length);
516 break;
517
518 case PKT_REQUEST:
519 TRACE("got request packet\n");
520 process_request_packet(conn, &hdr->request, msg);
521 break;
522
523 case PKT_AUTH3:
524 TRACE("got auth3 packet\n");
525 process_auth3_packet(conn, &hdr->common, msg, auth_data, auth_length);
526 break;
527 default:
528 FIXME("unhandled packet type %u\n", hdr->common.ptype);
529 break;
530 }
531
532 /* clean up */
533 I_RpcFree(msg->Buffer);
534 RPCRT4_FreeHeader(hdr);
535 HeapFree(GetProcessHeap(), 0, msg);
536 HeapFree(GetProcessHeap(), 0, auth_data);
537 }
538
539 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
540 {
541 RpcPacket *pkt = the_arg;
542 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg, pkt->auth_data,
543 pkt->auth_length);
544 HeapFree(GetProcessHeap(), 0, pkt);
545 return 0;
546 }
547
548 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
549 {
550 RpcConnection* conn = the_arg;
551 RpcPktHdr *hdr;
552 RPC_MESSAGE *msg;
553 RPC_STATUS status;
554 RpcPacket *packet;
555 unsigned char *auth_data;
556 ULONG auth_length;
557
558 TRACE("(%p)\n", conn);
559
560 for (;;) {
561 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
562 if (!msg) break;
563
564 status = RPCRT4_ReceiveWithAuth(conn, &hdr, msg, &auth_data, &auth_length);
565 if (status != RPC_S_OK) {
566 WARN("receive failed with error %x\n", status);
567 HeapFree(GetProcessHeap(), 0, msg);
568 break;
569 }
570
571 switch (hdr->common.ptype) {
572 case PKT_BIND:
573 TRACE("got bind packet\n");
574
575 status = process_bind_packet(conn, &hdr->bind, msg, auth_data,
576 auth_length);
577 break;
578
579 case PKT_REQUEST:
580 TRACE("got request packet\n");
581
582 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
583 if (!packet) {
584 I_RpcFree(msg->Buffer);
585 RPCRT4_FreeHeader(hdr);
586 HeapFree(GetProcessHeap(), 0, msg);
587 HeapFree(GetProcessHeap(), 0, auth_data);
588 goto exit;
589 }
590 packet->conn = conn;
591 packet->hdr = hdr;
592 packet->msg = msg;
593 packet->auth_data = auth_data;
594 packet->auth_length = auth_length;
595 if (!QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTELONGFUNCTION)) {
596 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
597 HeapFree(GetProcessHeap(), 0, packet);
598 status = RPC_S_OUT_OF_RESOURCES;
599 } else {
600 continue;
601 }
602 break;
603
604 case PKT_AUTH3:
605 TRACE("got auth3 packet\n");
606
607 status = process_auth3_packet(conn, &hdr->common, msg, auth_data,
608 auth_length);
609 break;
610 default:
611 FIXME("unhandled packet type %u\n", hdr->common.ptype);
612 break;
613 }
614
615 I_RpcFree(msg->Buffer);
616 RPCRT4_FreeHeader(hdr);
617 HeapFree(GetProcessHeap(), 0, msg);
618 HeapFree(GetProcessHeap(), 0, auth_data);
619
620 if (status != RPC_S_OK) {
621 WARN("processing packet failed with error %u\n", status);
622 break;
623 }
624 }
625 exit:
626 RPCRT4_DestroyConnection(conn);
627 return 0;
628 }
629
630 void RPCRT4_new_client(RpcConnection* conn)
631 {
632 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
633 if (!thread) {
634 DWORD err = GetLastError();
635 ERR("failed to create thread, error=%08x\n", err);
636 RPCRT4_DestroyConnection(conn);
637 }
638 /* we could set conn->thread, but then we'd have to make the io_thread wait
639 * for that, otherwise the thread might finish, destroy the connection, and
640 * free the memory we'd write to before we did, causing crashes and stuff -
641 * so let's implement that later, when we really need conn->thread */
642
643 CloseHandle( thread );
644 }
645
646 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
647 {
648 int res;
649 unsigned int count;
650 void *objs = NULL;
651 RpcServerProtseq* cps = the_arg;
652 RpcConnection* conn;
653 BOOL set_ready_event = FALSE;
654
655 TRACE("(the_arg == ^%p)\n", the_arg);
656
657 for (;;) {
658 objs = cps->ops->get_wait_array(cps, objs, &count);
659
660 if (set_ready_event)
661 {
662 /* signal to function that changed state that we are now sync'ed */
663 SetEvent(cps->server_ready_event);
664 set_ready_event = FALSE;
665 }
666
667 /* start waiting */
668 res = cps->ops->wait_for_new_connection(cps, count, objs);
669
670 if (res == -1 || (res == 0 && !std_listen))
671 {
672 /* cleanup */
673 cps->ops->free_wait_array(cps, objs);
674 EnterCriticalSection(&cps->cs);
675 for (conn = cps->conn; conn; conn = conn->Next)
676 RPCRT4_CloseConnection(conn);
677 LeaveCriticalSection(&cps->cs);
678
679 if (res == 0 && !std_listen)
680 SetEvent(cps->server_ready_event);
681 break;
682 }
683 else if (res == 0)
684 set_ready_event = TRUE;
685 }
686 return 0;
687 }
688
689 /* tells the server thread that the state has changed and waits for it to
690 * make the changes */
691 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
692 {
693 /* make sure we are the only thread sync'ing the server state, otherwise
694 * there is a race with the server thread setting an older state and setting
695 * the server_ready_event when the new state hasn't yet been applied */
696 WaitForSingleObject(ps->mgr_mutex, INFINITE);
697
698 ps->ops->signal_state_changed(ps);
699
700 /* wait for server thread to make the requested changes before returning */
701 WaitForSingleObject(ps->server_ready_event, INFINITE);
702
703 ReleaseMutex(ps->mgr_mutex);
704 }
705
706 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
707 {
708 RPC_STATUS status = RPC_S_OK;
709 HANDLE server_thread;
710
711 EnterCriticalSection(&listen_cs);
712 if (ps->is_listening) goto done;
713
714 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
715 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
716 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
717 if (!server_thread)
718 {
719 status = RPC_S_OUT_OF_RESOURCES;
720 goto done;
721 }
722 ps->is_listening = TRUE;
723 CloseHandle(server_thread);
724
725 done:
726 LeaveCriticalSection(&listen_cs);
727 return status;
728 }
729
730 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
731 {
732 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
733 RpcServerProtseq *cps;
734
735 TRACE("\n");
736
737 EnterCriticalSection(&listen_cs);
738 if (auto_listen || (manual_listen_count++ == 0))
739 {
740 status = RPC_S_OK;
741 if (++listen_count == 1)
742 std_listen = TRUE;
743 }
744 LeaveCriticalSection(&listen_cs);
745
746 if (std_listen)
747 {
748 EnterCriticalSection(&server_cs);
749 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
750 {
751 status = RPCRT4_start_listen_protseq(cps, TRUE);
752 if (status != RPC_S_OK)
753 break;
754
755 /* make sure server is actually listening on the interface before
756 * returning */
757 RPCRT4_sync_with_server_thread(cps);
758 }
759 LeaveCriticalSection(&server_cs);
760 }
761
762 return status;
763 }
764
765 static void RPCRT4_stop_listen(BOOL auto_listen)
766 {
767 EnterCriticalSection(&listen_cs);
768 if (auto_listen || (--manual_listen_count == 0))
769 {
770 if (listen_count != 0 && --listen_count == 0) {
771 RpcServerProtseq *cps;
772
773 std_listen = FALSE;
774 LeaveCriticalSection(&listen_cs);
775
776 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
777 RPCRT4_sync_with_server_thread(cps);
778
779 EnterCriticalSection(&listen_cs);
780 if (listen_done_event) SetEvent( listen_done_event );
781 listen_done_event = 0;
782 LeaveCriticalSection(&listen_cs);
783 return;
784 }
785 assert(listen_count >= 0);
786 }
787 LeaveCriticalSection(&listen_cs);
788 }
789
790 static BOOL RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq *protseq, const char *endpoint)
791 {
792 RpcConnection *conn;
793 EnterCriticalSection(&protseq->cs);
794 for (conn = protseq->conn; conn; conn = conn->Next)
795 {
796 if (!endpoint || !strcmp(endpoint, conn->Endpoint))
797 break;
798 }
799 LeaveCriticalSection(&protseq->cs);
800 return (conn != NULL);
801 }
802
803 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, const char *endpoint)
804 {
805 RPC_STATUS status;
806
807 EnterCriticalSection(&ps->cs);
808
809 if (RPCRT4_protseq_is_endpoint_registered(ps, endpoint))
810 status = RPC_S_OK;
811 else
812 status = ps->ops->open_endpoint(ps, endpoint);
813
814 LeaveCriticalSection(&ps->cs);
815
816 if (status != RPC_S_OK)
817 return status;
818
819 if (std_listen)
820 {
821 status = RPCRT4_start_listen_protseq(ps, FALSE);
822 if (status == RPC_S_OK)
823 RPCRT4_sync_with_server_thread(ps);
824 }
825
826 return status;
827 }
828
829 /***********************************************************************
830 * RpcServerInqBindings (RPCRT4.@)
831 */
832 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
833 {
834 RPC_STATUS status;
835 DWORD count;
836 RpcServerProtseq* ps;
837 RpcConnection* conn;
838
839 if (BindingVector)
840 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
841 else
842 ERR("(BindingVector == NULL!!?)\n");
843
844 EnterCriticalSection(&server_cs);
845 /* count connections */
846 count = 0;
847 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
848 EnterCriticalSection(&ps->cs);
849 for (conn = ps->conn; conn; conn = conn->Next)
850 count++;
851 LeaveCriticalSection(&ps->cs);
852 }
853 if (count) {
854 /* export bindings */
855 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
856 sizeof(RPC_BINDING_VECTOR) +
857 sizeof(RPC_BINDING_HANDLE)*(count-1));
858 (*BindingVector)->Count = count;
859 count = 0;
860 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
861 EnterCriticalSection(&ps->cs);
862 for (conn = ps->conn; conn; conn = conn->Next) {
863 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
864 conn);
865 count++;
866 }
867 LeaveCriticalSection(&ps->cs);
868 }
869 status = RPC_S_OK;
870 } else {
871 *BindingVector = NULL;
872 status = RPC_S_NO_BINDINGS;
873 }
874 LeaveCriticalSection(&server_cs);
875 return status;
876 }
877
878 /***********************************************************************
879 * RpcServerUseProtseqEpA (RPCRT4.@)
880 */
881 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
882 {
883 RPC_POLICY policy;
884
885 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
886
887 /* This should provide the default behaviour */
888 policy.Length = sizeof( policy );
889 policy.EndpointFlags = 0;
890 policy.NICFlags = 0;
891
892 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
893 }
894
895 /***********************************************************************
896 * RpcServerUseProtseqEpW (RPCRT4.@)
897 */
898 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
899 {
900 RPC_POLICY policy;
901
902 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
903
904 /* This should provide the default behaviour */
905 policy.Length = sizeof( policy );
906 policy.EndpointFlags = 0;
907 policy.NICFlags = 0;
908
909 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
910 }
911
912 /***********************************************************************
913 * alloc_serverprotoseq (internal)
914 *
915 * Must be called with server_cs held.
916 */
917 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
918 {
919 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
920
921 if (!ops)
922 {
923 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
924 return RPC_S_PROTSEQ_NOT_SUPPORTED;
925 }
926
927 *ps = ops->alloc();
928 if (!*ps)
929 return RPC_S_OUT_OF_RESOURCES;
930 (*ps)->MaxCalls = MaxCalls;
931 (*ps)->Protseq = RPCRT4_strdupA(Protseq);
932 (*ps)->ops = ops;
933 (*ps)->MaxCalls = 0;
934 (*ps)->conn = NULL;
935 InitializeCriticalSection(&(*ps)->cs);
936 (*ps)->is_listening = FALSE;
937 (*ps)->mgr_mutex = NULL;
938 (*ps)->server_ready_event = NULL;
939
940 list_add_head(&protseqs, &(*ps)->entry);
941
942 TRACE("new protseq %p created for %s\n", *ps, Protseq);
943
944 return RPC_S_OK;
945 }
946
947 /* must be called with server_cs held */
948 static void destroy_serverprotoseq(RpcServerProtseq *ps)
949 {
950 RPCRT4_strfree(ps->Protseq);
951 DeleteCriticalSection(&ps->cs);
952 CloseHandle(ps->mgr_mutex);
953 CloseHandle(ps->server_ready_event);
954 list_remove(&ps->entry);
955 HeapFree(GetProcessHeap(), 0, ps);
956 }
957
958 /* Finds a given protseq or creates a new one if one doesn't already exist */
959 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
960 {
961 RPC_STATUS status;
962 RpcServerProtseq *cps;
963
964 EnterCriticalSection(&server_cs);
965
966 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
967 if (!strcmp(cps->Protseq, Protseq))
968 {
969 TRACE("found existing protseq object for %s\n", Protseq);
970 *ps = cps;
971 LeaveCriticalSection(&server_cs);
972 return S_OK;
973 }
974
975 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
976
977 LeaveCriticalSection(&server_cs);
978
979 return status;
980 }
981
982 /***********************************************************************
983 * RpcServerUseProtseqEpExA (RPCRT4.@)
984 */
985 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
986 PRPC_POLICY lpPolicy )
987 {
988 RpcServerProtseq* ps;
989 RPC_STATUS status;
990
991 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq),
992 MaxCalls, debugstr_a((const char *)Endpoint), SecurityDescriptor,
993 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
994
995 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
996 if (status != RPC_S_OK)
997 return status;
998
999 return RPCRT4_use_protseq(ps, (const char *)Endpoint);
1000 }
1001
1002 /***********************************************************************
1003 * RpcServerUseProtseqEpExW (RPCRT4.@)
1004 */
1005 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
1006 PRPC_POLICY lpPolicy )
1007 {
1008 RpcServerProtseq* ps;
1009 RPC_STATUS status;
1010 LPSTR ProtseqA;
1011 LPSTR EndpointA;
1012
1013 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq ), MaxCalls,
1014 debugstr_w( Endpoint ), SecurityDescriptor,
1015 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
1016
1017 ProtseqA = RPCRT4_strdupWtoA(Protseq);
1018 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
1019 RPCRT4_strfree(ProtseqA);
1020 if (status != RPC_S_OK)
1021 return status;
1022
1023 EndpointA = RPCRT4_strdupWtoA(Endpoint);
1024 status = RPCRT4_use_protseq(ps, EndpointA);
1025 RPCRT4_strfree(EndpointA);
1026 return status;
1027 }
1028
1029 /***********************************************************************
1030 * RpcServerUseProtseqA (RPCRT4.@)
1031 */
1032 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1033 {
1034 RPC_STATUS status;
1035 RpcServerProtseq* ps;
1036
1037 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
1038
1039 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
1040 if (status != RPC_S_OK)
1041 return status;
1042
1043 return RPCRT4_use_protseq(ps, NULL);
1044 }
1045
1046 /***********************************************************************
1047 * RpcServerUseProtseqW (RPCRT4.@)
1048 */
1049 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1050 {
1051 RPC_STATUS status;
1052 RpcServerProtseq* ps;
1053 LPSTR ProtseqA;
1054
1055 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
1056
1057 ProtseqA = RPCRT4_strdupWtoA(Protseq);
1058 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
1059 RPCRT4_strfree(ProtseqA);
1060 if (status != RPC_S_OK)
1061 return status;
1062
1063 return RPCRT4_use_protseq(ps, NULL);
1064 }
1065
1066 void RPCRT4_destroy_all_protseqs(void)
1067 {
1068 RpcServerProtseq *cps, *cursor2;
1069
1070 if (listen_count != 0)
1071 std_listen = FALSE;
1072
1073 EnterCriticalSection(&server_cs);
1074 LIST_FOR_EACH_ENTRY_SAFE(cps, cursor2, &protseqs, RpcServerProtseq, entry)
1075 {
1076 destroy_serverprotoseq(cps);
1077 }
1078 LeaveCriticalSection(&server_cs);
1079 }
1080
1081 /***********************************************************************
1082 * RpcServerRegisterIf (RPCRT4.@)
1083 */
1084 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
1085 {
1086 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
1087 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
1088 }
1089
1090 /***********************************************************************
1091 * RpcServerRegisterIfEx (RPCRT4.@)
1092 */
1093 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1094 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
1095 {
1096 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
1097 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
1098 }
1099
1100 /***********************************************************************
1101 * RpcServerRegisterIf2 (RPCRT4.@)
1102 */
1103 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1104 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
1105 {
1106 PRPC_SERVER_INTERFACE If = IfSpec;
1107 RpcServerInterface* sif;
1108 unsigned int i;
1109
1110 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
1111 MaxRpcSize, IfCallbackFn);
1112 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
1113 If->InterfaceId.SyntaxVersion.MajorVersion,
1114 If->InterfaceId.SyntaxVersion.MinorVersion);
1115 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
1116 If->TransferSyntax.SyntaxVersion.MajorVersion,
1117 If->TransferSyntax.SyntaxVersion.MinorVersion);
1118 TRACE(" dispatch table: %p\n", If->DispatchTable);
1119 if (If->DispatchTable) {
1120 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
1121 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
1122 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
1123 }
1124 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
1125 }
1126 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
1127 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
1128 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
1129
1130 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
1131 sif->If = If;
1132 if (MgrTypeUuid) {
1133 sif->MgrTypeUuid = *MgrTypeUuid;
1134 sif->MgrEpv = MgrEpv;
1135 } else {
1136 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
1137 sif->MgrEpv = If->DefaultManagerEpv;
1138 }
1139 sif->Flags = Flags;
1140 sif->MaxCalls = MaxCalls;
1141 sif->MaxRpcSize = MaxRpcSize;
1142 sif->IfCallbackFn = IfCallbackFn;
1143
1144 EnterCriticalSection(&server_cs);
1145 list_add_head(&server_interfaces, &sif->entry);
1146 LeaveCriticalSection(&server_cs);
1147
1148 if (sif->Flags & RPC_IF_AUTOLISTEN)
1149 RPCRT4_start_listen(TRUE);
1150
1151 return RPC_S_OK;
1152 }
1153
1154 /***********************************************************************
1155 * RpcServerUnregisterIf (RPCRT4.@)
1156 */
1157 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
1158 {
1159 PRPC_SERVER_INTERFACE If = IfSpec;
1160 HANDLE event = NULL;
1161 BOOL found = FALSE;
1162 BOOL completed = TRUE;
1163 RpcServerInterface *cif;
1164 RPC_STATUS status;
1165
1166 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1167 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
1168
1169 EnterCriticalSection(&server_cs);
1170 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
1171 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
1172 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
1173 list_remove(&cif->entry);
1174 TRACE("unregistering cif %p\n", cif);
1175 if (cif->CurrentCalls) {
1176 completed = FALSE;
1177 cif->Delete = TRUE;
1178 if (WaitForCallsToComplete)
1179 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
1180 }
1181 found = TRUE;
1182 break;
1183 }
1184 }
1185 LeaveCriticalSection(&server_cs);
1186
1187 if (!found) {
1188 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
1189 return RPC_S_UNKNOWN_IF;
1190 }
1191
1192 if (completed)
1193 HeapFree(GetProcessHeap(), 0, cif);
1194 else if (event) {
1195 /* sif will be freed when the last call is completed, so be careful not to
1196 * touch that memory here as that could happen before we get here */
1197 WaitForSingleObject(event, INFINITE);
1198 CloseHandle(event);
1199 }
1200
1201 return RPC_S_OK;
1202 }
1203
1204 /***********************************************************************
1205 * RpcServerUnregisterIfEx (RPCRT4.@)
1206 */
1207 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
1208 {
1209 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1210 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
1211
1212 return RPC_S_OK;
1213 }
1214
1215 /***********************************************************************
1216 * RpcObjectSetType (RPCRT4.@)
1217 *
1218 * PARAMS
1219 * ObjUuid [I] "Object" UUID
1220 * TypeUuid [I] "Type" UUID
1221 *
1222 * RETURNS
1223 * RPC_S_OK The call succeeded
1224 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1225 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1226 *
1227 * Maps "Object" UUIDs to "Type" UUIDs. Passing the nil UUID as the type
1228 * resets the mapping for the specified object UUID to nil (the default).
1229 * The nil object is always associated with the nil type and cannot be
1230 * reassigned. Servers can support multiple implementations on the same
1231 * interface by registering different end-point vectors for the different
1232 * types. There's no need to call this if a server only supports the nil
1233 * type, as is typical.
1234 */
1235 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
1236 {
1237 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
1238 RPC_STATUS dummy;
1239
1240 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
1241 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
1242 /* nil uuid cannot be remapped */
1243 return RPC_S_INVALID_OBJECT;
1244 }
1245
1246 /* find the mapping for this object if there is one ... */
1247 while (map) {
1248 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
1249 prev = map;
1250 map = map->next;
1251 }
1252 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
1253 /* ... and drop it from the list */
1254 if (map) {
1255 if (prev)
1256 prev->next = map->next;
1257 else
1258 RpcObjTypeMaps = map->next;
1259 HeapFree(GetProcessHeap(), 0, map);
1260 }
1261 } else {
1262 /* ... , fail if we found it ... */
1263 if (map)
1264 return RPC_S_ALREADY_REGISTERED;
1265 /* ... otherwise create a new one and add it in. */
1266 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
1267 map->Object = *ObjUuid;
1268 map->Type = *TypeUuid;
1269 map->next = NULL;
1270 if (prev)
1271 prev->next = map; /* prev is the last map in the linklist */
1272 else
1273 RpcObjTypeMaps = map;
1274 }
1275
1276 return RPC_S_OK;
1277 }
1278
1279 struct rpc_server_registered_auth_info
1280 {
1281 struct list entry;
1282 TimeStamp exp;
1283 CredHandle cred;
1284 ULONG max_token;
1285 USHORT auth_type;
1286 };
1287
1288 RPC_STATUS RPCRT4_ServerGetRegisteredAuthInfo(
1289 USHORT auth_type, CredHandle *cred, TimeStamp *exp, ULONG *max_token)
1290 {
1291 RPC_STATUS status = RPC_S_UNKNOWN_AUTHN_SERVICE;
1292 struct rpc_server_registered_auth_info *auth_info;
1293
1294 EnterCriticalSection(&server_auth_info_cs);
1295 LIST_FOR_EACH_ENTRY(auth_info, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1296 {
1297 if (auth_info->auth_type == auth_type)
1298 {
1299 *cred = auth_info->cred;
1300 *exp = auth_info->exp;
1301 *max_token = auth_info->max_token;
1302 status = RPC_S_OK;
1303 break;
1304 }
1305 }
1306 LeaveCriticalSection(&server_auth_info_cs);
1307
1308 return status;
1309 }
1310
1311 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1312 {
1313 struct rpc_server_registered_auth_info *auth_info, *cursor2;
1314
1315 EnterCriticalSection(&server_auth_info_cs);
1316 LIST_FOR_EACH_ENTRY_SAFE(auth_info, cursor2, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1317 {
1318 FreeCredentialsHandle(&auth_info->cred);
1319 HeapFree(GetProcessHeap(), 0, auth_info);
1320 }
1321 LeaveCriticalSection(&server_auth_info_cs);
1322 }
1323
1324 /***********************************************************************
1325 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1326 */
1327 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1328 LPVOID Arg )
1329 {
1330 SECURITY_STATUS sec_status;
1331 CredHandle cred;
1332 TimeStamp exp;
1333 ULONG package_count;
1334 ULONG i;
1335 PSecPkgInfoA packages;
1336 ULONG max_token;
1337 struct rpc_server_registered_auth_info *auth_info;
1338
1339 TRACE("(%s,%u,%p,%p)\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg);
1340
1341 sec_status = EnumerateSecurityPackagesA(&package_count, &packages);
1342 if (sec_status != SEC_E_OK)
1343 {
1344 ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n",
1345 sec_status);
1346 return RPC_S_SEC_PKG_ERROR;
1347 }
1348
1349 for (i = 0; i < package_count; i++)
1350 if (packages[i].wRPCID == AuthnSvc)
1351 break;
1352
1353 if (i == package_count)
1354 {
1355 WARN("unsupported AuthnSvc %u\n", AuthnSvc);
1356 FreeContextBuffer(packages);
1357 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1358 }
1359 TRACE("found package %s for service %u\n", packages[i].Name,
1360 AuthnSvc);
1361 sec_status = AcquireCredentialsHandleA((SEC_CHAR *)ServerPrincName,
1362 packages[i].Name,
1363 SECPKG_CRED_INBOUND, NULL, NULL,
1364 NULL, NULL, &cred, &exp);
1365 max_token = packages[i].cbMaxToken;
1366 FreeContextBuffer(packages);
1367 if (sec_status != SEC_E_OK)
1368 return RPC_S_SEC_PKG_ERROR;
1369
1370 auth_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info));
1371 if (!auth_info)
1372 {
1373 FreeCredentialsHandle(&cred);
1374 return RPC_S_OUT_OF_RESOURCES;
1375 }
1376
1377 auth_info->exp = exp;
1378 auth_info->cred = cred;
1379 auth_info->max_token = max_token;
1380 auth_info->auth_type = AuthnSvc;
1381
1382 EnterCriticalSection(&server_auth_info_cs);
1383 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1384 LeaveCriticalSection(&server_auth_info_cs);
1385
1386 return RPC_S_OK;
1387 }
1388
1389 /***********************************************************************
1390 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1391 */
1392 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1393 LPVOID Arg )
1394 {
1395 SECURITY_STATUS sec_status;
1396 CredHandle cred;
1397 TimeStamp exp;
1398 ULONG package_count;
1399 ULONG i;
1400 PSecPkgInfoW packages;
1401 ULONG max_token;
1402 struct rpc_server_registered_auth_info *auth_info;
1403
1404 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName), AuthnSvc, GetKeyFn, Arg);
1405
1406 sec_status = EnumerateSecurityPackagesW(&package_count, &packages);
1407 if (sec_status != SEC_E_OK)
1408 {
1409 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n",
1410 sec_status);
1411 return RPC_S_SEC_PKG_ERROR;
1412 }
1413
1414 for (i = 0; i < package_count; i++)
1415 if (packages[i].wRPCID == AuthnSvc)
1416 break;
1417
1418 if (i == package_count)
1419 {
1420 WARN("unsupported AuthnSvc %u\n", AuthnSvc);
1421 FreeContextBuffer(packages);
1422 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1423 }
1424 TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name),
1425 AuthnSvc);
1426 sec_status = AcquireCredentialsHandleW((SEC_WCHAR *)ServerPrincName,
1427 packages[i].Name,
1428 SECPKG_CRED_INBOUND, NULL, NULL,
1429 NULL, NULL, &cred, &exp);
1430 max_token = packages[i].cbMaxToken;
1431 FreeContextBuffer(packages);
1432 if (sec_status != SEC_E_OK)
1433 return RPC_S_SEC_PKG_ERROR;
1434
1435 auth_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info));
1436 if (!auth_info)
1437 {
1438 FreeCredentialsHandle(&cred);
1439 return RPC_S_OUT_OF_RESOURCES;
1440 }
1441
1442 auth_info->exp = exp;
1443 auth_info->cred = cred;
1444 auth_info->max_token = max_token;
1445 auth_info->auth_type = AuthnSvc;
1446
1447 EnterCriticalSection(&server_auth_info_cs);
1448 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1449 LeaveCriticalSection(&server_auth_info_cs);
1450
1451 return RPC_S_OK;
1452 }
1453
1454 /***********************************************************************
1455 * RpcServerListen (RPCRT4.@)
1456 */
1457 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1458 {
1459 RPC_STATUS status = RPC_S_OK;
1460
1461 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1462
1463 if (list_empty(&protseqs))
1464 return RPC_S_NO_PROTSEQS_REGISTERED;
1465
1466 status = RPCRT4_start_listen(FALSE);
1467
1468 if (DontWait || (status != RPC_S_OK)) return status;
1469
1470 return RpcMgmtWaitServerListen();
1471 }
1472
1473 /***********************************************************************
1474 * RpcMgmtServerWaitListen (RPCRT4.@)
1475 */
1476 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1477 {
1478 HANDLE event;
1479
1480 TRACE("()\n");
1481
1482 EnterCriticalSection(&listen_cs);
1483
1484 if (!std_listen) {
1485 LeaveCriticalSection(&listen_cs);
1486 return RPC_S_NOT_LISTENING;
1487 }
1488 if (listen_done_event) {
1489 LeaveCriticalSection(&listen_cs);
1490 return RPC_S_ALREADY_LISTENING;
1491 }
1492 event = CreateEventW( NULL, TRUE, FALSE, NULL );
1493 listen_done_event = event;
1494
1495 LeaveCriticalSection(&listen_cs);
1496
1497 TRACE( "waiting for server calls to finish\n" );
1498 WaitForSingleObject( event, INFINITE );
1499 TRACE( "done waiting\n" );
1500
1501 CloseHandle( event );
1502 return RPC_S_OK;
1503 }
1504
1505 /***********************************************************************
1506 * RpcMgmtStopServerListening (RPCRT4.@)
1507 */
1508 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1509 {
1510 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1511
1512 if (Binding) {
1513 FIXME("client-side invocation not implemented.\n");
1514 return RPC_S_WRONG_KIND_OF_BINDING;
1515 }
1516
1517 RPCRT4_stop_listen(FALSE);
1518
1519 return RPC_S_OK;
1520 }
1521
1522 /***********************************************************************
1523 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1524 */
1525 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1526 {
1527 FIXME("(): stub\n");
1528 return RPC_S_OK;
1529 }
1530
1531 /***********************************************************************
1532 * I_RpcServerStartListening (RPCRT4.@)
1533 */
1534 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1535 {
1536 FIXME( "(%p): stub\n", hWnd );
1537
1538 return RPC_S_OK;
1539 }
1540
1541 /***********************************************************************
1542 * I_RpcServerStopListening (RPCRT4.@)
1543 */
1544 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1545 {
1546 FIXME( "(): stub\n" );
1547
1548 return RPC_S_OK;
1549 }
1550
1551 /***********************************************************************
1552 * I_RpcWindowProc (RPCRT4.@)
1553 */
1554 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1555 {
1556 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1557
1558 return 0;
1559 }
1560
1561 /***********************************************************************
1562 * RpcMgmtInqIfIds (RPCRT4.@)
1563 */
1564 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1565 {
1566 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1567 return RPC_S_INVALID_BINDING;
1568 }
1569
1570 /***********************************************************************
1571 * RpcMgmtInqStats (RPCRT4.@)
1572 */
1573 RPC_STATUS WINAPI RpcMgmtInqStats(RPC_BINDING_HANDLE Binding, RPC_STATS_VECTOR **Statistics)
1574 {
1575 RPC_STATS_VECTOR *stats;
1576
1577 FIXME("(%p,%p)\n", Binding, Statistics);
1578
1579 if ((stats = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR))))
1580 {
1581 stats->Count = 1;
1582 stats->Stats[0] = 0;
1583 *Statistics = stats;
1584 return RPC_S_OK;
1585 }
1586 return RPC_S_OUT_OF_RESOURCES;
1587 }
1588
1589 /***********************************************************************
1590 * RpcMgmtStatsVectorFree (RPCRT4.@)
1591 */
1592 RPC_STATUS WINAPI RpcMgmtStatsVectorFree(RPC_STATS_VECTOR **StatsVector)
1593 {
1594 FIXME("(%p)\n", StatsVector);
1595
1596 if (StatsVector)
1597 {
1598 HeapFree(GetProcessHeap(), 0, *StatsVector);
1599 *StatsVector = NULL;
1600 }
1601 return RPC_S_OK;
1602 }
1603
1604 /***********************************************************************
1605 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1606 */
1607 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, ULONG InquiryType,
1608 RPC_IF_ID *IfId, ULONG VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1609 {
1610 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1611 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1612 return RPC_S_INVALID_BINDING;
1613 }
1614
1615 /***********************************************************************
1616 * RpcMgmtIsServerListening (RPCRT4.@)
1617 */
1618 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1619 {
1620 FIXME("(%p): stub\n", Binding);
1621 return RPC_S_INVALID_BINDING;
1622 }
1623
1624 /***********************************************************************
1625 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1626 */
1627 RPC_STATUS WINAPI RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn)
1628 {
1629 FIXME("(%p): stub\n", fn);
1630 return RPC_S_OK;
1631 }
1632
1633 /***********************************************************************
1634 * RpcMgmtSetServerStackSize (RPCRT4.@)
1635 */
1636 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1637 {
1638 FIXME("(0x%x): stub\n", ThreadStackSize);
1639 return RPC_S_OK;
1640 }
1641
1642 /***********************************************************************
1643 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1644 */
1645 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1646 {
1647 TRACE("\n");
1648 return RPCRT4_GetThreadCurrentCallHandle();
1649 }