rpcrt4: Use standard Wine list to store connections in RpcServerProtseq.
[wine.git] / dlls / rpcrt4 / rpc_server.c
blob8c3aa0ff0b213972e270fd5666466dd31d525463
1 /*
2 * RPC server API
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006-2008 Robert Shearman (for CodeWeavers)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
35 #include "rpc.h"
36 #include "rpcndr.h"
37 #include "excpt.h"
39 #include "wine/debug.h"
40 #include "wine/exception.h"
42 #include "rpc_server.h"
43 #include "rpc_assoc.h"
44 #include "rpc_message.h"
45 #include "rpc_defs.h"
46 #include "ncastatus.h"
47 #include "secext.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
51 typedef struct _RpcPacket
53 struct _RpcConnection* conn;
54 RpcPktHdr* hdr;
55 RPC_MESSAGE* msg;
56 unsigned char *auth_data;
57 ULONG auth_length;
58 } RpcPacket;
60 typedef struct _RpcObjTypeMap
62 /* FIXME: a hash table would be better. */
63 struct _RpcObjTypeMap *next;
64 UUID Object;
65 UUID Type;
66 } RpcObjTypeMap;
68 static RpcObjTypeMap *RpcObjTypeMaps;
70 /* list of type RpcServerProtseq */
71 static struct list protseqs = LIST_INIT(protseqs);
72 static struct list server_interfaces = LIST_INIT(server_interfaces);
73 static struct list server_registered_auth_info = LIST_INIT(server_registered_auth_info);
75 static CRITICAL_SECTION server_cs;
76 static CRITICAL_SECTION_DEBUG server_cs_debug =
78 0, 0, &server_cs,
79 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
80 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
82 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
84 static CRITICAL_SECTION listen_cs;
85 static CRITICAL_SECTION_DEBUG listen_cs_debug =
87 0, 0, &listen_cs,
88 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
89 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
91 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
93 static CRITICAL_SECTION server_auth_info_cs;
94 static CRITICAL_SECTION_DEBUG server_auth_info_cs_debug =
96 0, 0, &server_auth_info_cs,
97 { &server_auth_info_cs_debug.ProcessLocksList, &server_auth_info_cs_debug.ProcessLocksList },
98 0, 0, { (DWORD_PTR)(__FILE__ ": server_auth_info_cs") }
100 static CRITICAL_SECTION server_auth_info_cs = { &server_auth_info_cs_debug, -1, 0, 0, 0, 0 };
102 /* whether the server is currently listening */
103 static BOOL std_listen;
104 /* total listeners including auto listeners */
105 static LONG listen_count;
106 /* event set once all manual listening is finished */
107 static HANDLE listen_done_event;
109 static UUID uuid_nil;
111 static inline RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
113 RpcObjTypeMap *rslt = RpcObjTypeMaps;
114 RPC_STATUS dummy;
116 while (rslt) {
117 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
118 rslt = rslt->next;
121 return rslt;
124 static inline UUID *LookupObjType(UUID *ObjUuid)
126 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
127 if (map)
128 return &map->Type;
129 else
130 return &uuid_nil;
133 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
134 const RPC_SYNTAX_IDENTIFIER *if_id,
135 const RPC_SYNTAX_IDENTIFIER *transfer_syntax,
136 BOOL check_object)
138 UUID* MgrType = NULL;
139 RpcServerInterface* cif;
140 RPC_STATUS status;
142 if (check_object)
143 MgrType = LookupObjType(object);
144 EnterCriticalSection(&server_cs);
145 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
146 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
147 (!transfer_syntax || !memcmp(transfer_syntax, &cif->If->TransferSyntax, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
148 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
149 std_listen) {
150 InterlockedIncrement(&cif->CurrentCalls);
151 break;
154 LeaveCriticalSection(&server_cs);
155 if (&cif->entry == &server_interfaces) cif = NULL;
156 TRACE("returning %p for object %s, if_id { %d.%d %s }\n", cif,
157 debugstr_guid(object), if_id->SyntaxVersion.MajorVersion,
158 if_id->SyntaxVersion.MinorVersion, debugstr_guid(&if_id->SyntaxGUID));
159 return cif;
162 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
164 if (!InterlockedDecrement(&sif->CurrentCalls) &&
165 sif->Delete) {
166 /* sif must have been removed from server_interfaces before
167 * CallsCompletedEvent is set */
168 if (sif->CallsCompletedEvent)
169 SetEvent(sif->CallsCompletedEvent);
170 HeapFree(GetProcessHeap(), 0, sif);
174 static RpcPktHdr *handle_bind_error(RpcConnection *conn, RPC_STATUS error)
176 unsigned int reject_reason;
177 switch (error)
179 case RPC_S_SERVER_TOO_BUSY:
180 reject_reason = REJECT_TEMPORARY_CONGESTION;
181 break;
182 case ERROR_OUTOFMEMORY:
183 case RPC_S_OUT_OF_RESOURCES:
184 reject_reason = REJECT_LOCAL_LIMIT_EXCEEDED;
185 break;
186 case RPC_S_PROTOCOL_ERROR:
187 reject_reason = REJECT_PROTOCOL_VERSION_NOT_SUPPORTED;
188 break;
189 case RPC_S_UNKNOWN_AUTHN_SERVICE:
190 reject_reason = REJECT_UNKNOWN_AUTHN_SERVICE;
191 break;
192 case ERROR_ACCESS_DENIED:
193 reject_reason = REJECT_INVALID_CHECKSUM;
194 break;
195 default:
196 FIXME("unexpected status value %d\n", error);
197 /* fall through */
198 case RPC_S_INVALID_BOUND:
199 reject_reason = REJECT_REASON_NOT_SPECIFIED;
200 break;
202 return RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
203 RPC_VER_MAJOR, RPC_VER_MINOR,
204 reject_reason);
207 static RPC_STATUS process_bind_packet_no_send(
208 RpcConnection *conn, RpcPktBindHdr *hdr, RPC_MESSAGE *msg,
209 unsigned char *auth_data, ULONG auth_length, RpcPktHdr **ack_response,
210 unsigned char **auth_data_out, ULONG *auth_length_out)
212 RPC_STATUS status;
213 RpcContextElement *ctxt_elem;
214 unsigned int i;
215 RpcResult *results;
217 /* validate data */
218 for (i = 0, ctxt_elem = msg->Buffer;
219 i < hdr->num_elements;
220 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
222 if (((char *)ctxt_elem - (char *)msg->Buffer) > msg->BufferLength ||
223 ((char *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes] - (char *)msg->Buffer) > msg->BufferLength)
225 ERR("inconsistent data in packet - packet length %d, num elements %d\n",
226 msg->BufferLength, hdr->num_elements);
227 return RPC_S_INVALID_BOUND;
231 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
232 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
233 conn->server_binding)
235 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
237 return RPC_S_INVALID_BOUND;
240 results = HeapAlloc(GetProcessHeap(), 0,
241 hdr->num_elements * sizeof(*results));
242 if (!results)
243 return RPC_S_OUT_OF_RESOURCES;
245 for (i = 0, ctxt_elem = (RpcContextElement *)msg->Buffer;
246 i < hdr->num_elements;
247 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
249 RpcServerInterface* sif = NULL;
250 unsigned int j;
252 for (j = 0; !sif && j < ctxt_elem->num_syntaxes; j++)
254 sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
255 &ctxt_elem->transfer_syntaxes[j], FALSE);
256 if (sif)
257 break;
259 if (sif)
261 RPCRT4_release_server_interface(sif);
262 TRACE("accepting bind request on connection %p for %s\n", conn,
263 debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
264 results[i].result = RESULT_ACCEPT;
265 results[i].reason = REASON_NONE;
266 results[i].transfer_syntax = ctxt_elem->transfer_syntaxes[j];
268 /* save the interface for later use */
269 /* FIXME: save linked list */
270 conn->ActiveInterface = ctxt_elem->abstract_syntax;
272 else if ((sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
273 NULL, FALSE)) != NULL)
275 RPCRT4_release_server_interface(sif);
276 TRACE("not accepting bind request on connection %p for %s - no transfer syntaxes supported\n",
277 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
278 results[i].result = RESULT_PROVIDER_REJECTION;
279 results[i].reason = REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED;
280 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
282 else
284 TRACE("not accepting bind request on connection %p for %s - abstract syntax not supported\n",
285 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
286 results[i].result = RESULT_PROVIDER_REJECTION;
287 results[i].reason = REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED;
288 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
292 /* create temporary binding */
293 status = RPCRT4_MakeBinding(&conn->server_binding, conn);
294 if (status != RPC_S_OK)
296 HeapFree(GetProcessHeap(), 0, results);
297 return status;
300 status = RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
301 conn->NetworkAddr, conn->Endpoint,
302 conn->NetworkOptions,
303 hdr->assoc_gid,
304 &conn->server_binding->Assoc);
305 if (status != RPC_S_OK)
307 HeapFree(GetProcessHeap(), 0, results);
308 return status;
311 if (auth_length)
313 status = RPCRT4_ServerConnectionAuth(conn, TRUE,
314 (RpcAuthVerifier *)auth_data,
315 auth_length, auth_data_out,
316 auth_length_out);
317 if (status != RPC_S_OK)
319 HeapFree(GetProcessHeap(), 0, results);
320 return status;
324 *ack_response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
325 RPC_MAX_PACKET_SIZE,
326 RPC_MAX_PACKET_SIZE,
327 conn->server_binding->Assoc->assoc_group_id,
328 conn->Endpoint, hdr->num_elements,
329 results);
330 HeapFree(GetProcessHeap(), 0, results);
332 if (*ack_response)
333 conn->MaxTransmissionSize = hdr->max_tsize;
334 else
335 status = RPC_S_OUT_OF_RESOURCES;
337 return status;
340 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr,
341 RPC_MESSAGE *msg,
342 unsigned char *auth_data,
343 ULONG auth_length)
345 RPC_STATUS status;
346 RpcPktHdr *response = NULL;
347 unsigned char *auth_data_out = NULL;
348 ULONG auth_length_out = 0;
350 status = process_bind_packet_no_send(conn, hdr, msg, auth_data, auth_length,
351 &response, &auth_data_out,
352 &auth_length_out);
353 if (status != RPC_S_OK)
354 response = handle_bind_error(conn, status);
355 if (response)
356 status = RPCRT4_SendWithAuth(conn, response, NULL, 0, auth_data_out, auth_length_out);
357 else
358 status = ERROR_OUTOFMEMORY;
359 RPCRT4_FreeHeader(response);
361 return status;
365 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
367 RPC_STATUS status;
368 RpcPktHdr *response = NULL;
369 RpcServerInterface* sif;
370 RPC_DISPATCH_FUNCTION func;
371 BOOL exception;
372 UUID *object_uuid;
373 NDR_SCONTEXT context_handle;
374 void *buf = msg->Buffer;
376 /* fail if the connection isn't bound with an interface */
377 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
378 /* FIXME: should send BindNack instead */
379 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
380 status);
382 RPCRT4_Send(conn, response, NULL, 0);
383 RPCRT4_FreeHeader(response);
384 return RPC_S_OK;
387 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
388 object_uuid = (UUID*)(hdr + 1);
389 } else {
390 object_uuid = NULL;
393 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, NULL, TRUE);
394 if (!sif) {
395 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
396 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
397 NCA_S_UNK_IF);
399 RPCRT4_Send(conn, response, NULL, 0);
400 RPCRT4_FreeHeader(response);
401 return RPC_S_OK;
403 msg->RpcInterfaceInformation = sif->If;
404 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
405 msg->ManagerEpv = sif->MgrEpv;
406 if (object_uuid != NULL) {
407 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
410 /* find dispatch function */
411 msg->ProcNum = hdr->opnum;
412 if (sif->Flags & RPC_IF_OLE) {
413 /* native ole32 always gives us a dispatch table with a single entry
414 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
415 func = *sif->If->DispatchTable->DispatchTable;
416 } else {
417 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
418 WARN("invalid procnum (%d/%d)\n", msg->ProcNum, sif->If->DispatchTable->DispatchTableCount);
419 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
420 NCA_S_OP_RNG_ERROR);
422 RPCRT4_Send(conn, response, NULL, 0);
423 RPCRT4_FreeHeader(response);
425 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
428 /* put in the drep. FIXME: is this more universally applicable?
429 perhaps we should move this outward... */
430 msg->DataRepresentation =
431 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
432 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
434 exception = FALSE;
436 /* dispatch */
437 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
438 __TRY {
439 if (func) func(msg);
440 } __EXCEPT_ALL {
441 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
442 exception = TRUE;
443 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
444 status = ERROR_NOACCESS;
445 else
446 status = GetExceptionCode();
447 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
448 RPC2NCA_STATUS(status));
449 } __ENDTRY
450 RPCRT4_SetThreadCurrentCallHandle(NULL);
452 /* release any unmarshalled context handles */
453 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
454 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
456 if (!exception)
457 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
458 msg->BufferLength);
460 /* send response packet */
461 if (response) {
462 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
463 exception ? 0 : msg->BufferLength);
464 RPCRT4_FreeHeader(response);
465 } else
466 ERR("out of memory\n");
468 msg->RpcInterfaceInformation = NULL;
469 RPCRT4_release_server_interface(sif);
471 if (msg->Buffer == buf) buf = NULL;
472 TRACE("freeing Buffer=%p\n", buf);
473 I_RpcFree(buf);
475 return status;
478 static RPC_STATUS process_auth3_packet(RpcConnection *conn,
479 RpcPktCommonHdr *hdr,
480 RPC_MESSAGE *msg,
481 unsigned char *auth_data,
482 ULONG auth_length)
484 RPC_STATUS status;
486 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
487 !auth_length || msg->BufferLength != 0)
488 status = RPC_S_PROTOCOL_ERROR;
489 else
491 status = RPCRT4_ServerConnectionAuth(conn, FALSE,
492 (RpcAuthVerifier *)auth_data,
493 auth_length, NULL, NULL);
496 /* FIXME: client doesn't expect a response to this message so must store
497 * status in connection so that fault packet can be returned when next
498 * packet is received */
500 return RPC_S_OK;
503 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr,
504 RPC_MESSAGE* msg, unsigned char *auth_data,
505 ULONG auth_length)
507 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
509 switch (hdr->common.ptype) {
510 case PKT_BIND:
511 TRACE("got bind packet\n");
512 process_bind_packet(conn, &hdr->bind, msg, auth_data, auth_length);
513 break;
515 case PKT_REQUEST:
516 TRACE("got request packet\n");
517 process_request_packet(conn, &hdr->request, msg);
518 break;
520 case PKT_AUTH3:
521 TRACE("got auth3 packet\n");
522 process_auth3_packet(conn, &hdr->common, msg, auth_data, auth_length);
523 break;
524 default:
525 FIXME("unhandled packet type %u\n", hdr->common.ptype);
526 break;
529 /* clean up */
530 I_RpcFree(msg->Buffer);
531 RPCRT4_FreeHeader(hdr);
532 HeapFree(GetProcessHeap(), 0, msg);
533 HeapFree(GetProcessHeap(), 0, auth_data);
536 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
538 RpcPacket *pkt = the_arg;
539 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg, pkt->auth_data,
540 pkt->auth_length);
541 RPCRT4_ReleaseConnection(pkt->conn);
542 HeapFree(GetProcessHeap(), 0, pkt);
543 return 0;
546 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
548 RpcConnection* conn = the_arg;
549 RpcPktHdr *hdr;
550 RPC_MESSAGE *msg;
551 RPC_STATUS status;
552 RpcPacket *packet;
553 unsigned char *auth_data;
554 ULONG auth_length;
556 TRACE("(%p)\n", conn);
558 for (;;) {
559 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
560 if (!msg) break;
562 status = RPCRT4_ReceiveWithAuth(conn, &hdr, msg, &auth_data, &auth_length);
563 if (status != RPC_S_OK) {
564 WARN("receive failed with error %x\n", status);
565 HeapFree(GetProcessHeap(), 0, msg);
566 break;
569 switch (hdr->common.ptype) {
570 case PKT_BIND:
571 TRACE("got bind packet\n");
573 status = process_bind_packet(conn, &hdr->bind, msg, auth_data,
574 auth_length);
575 break;
577 case PKT_REQUEST:
578 TRACE("got request packet\n");
580 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
581 if (!packet) {
582 I_RpcFree(msg->Buffer);
583 RPCRT4_FreeHeader(hdr);
584 HeapFree(GetProcessHeap(), 0, msg);
585 HeapFree(GetProcessHeap(), 0, auth_data);
586 goto exit;
588 packet->conn = RPCRT4_GrabConnection( conn );
589 packet->hdr = hdr;
590 packet->msg = msg;
591 packet->auth_data = auth_data;
592 packet->auth_length = auth_length;
593 if (!QueueUserWorkItem(RPCRT4_worker_thread, packet, WT_EXECUTELONGFUNCTION)) {
594 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
595 HeapFree(GetProcessHeap(), 0, packet);
596 status = RPC_S_OUT_OF_RESOURCES;
597 } else {
598 continue;
600 break;
602 case PKT_AUTH3:
603 TRACE("got auth3 packet\n");
605 status = process_auth3_packet(conn, &hdr->common, msg, auth_data,
606 auth_length);
607 break;
608 default:
609 FIXME("unhandled packet type %u\n", hdr->common.ptype);
610 break;
613 I_RpcFree(msg->Buffer);
614 RPCRT4_FreeHeader(hdr);
615 HeapFree(GetProcessHeap(), 0, msg);
616 HeapFree(GetProcessHeap(), 0, auth_data);
618 if (status != RPC_S_OK) {
619 WARN("processing packet failed with error %u\n", status);
620 break;
623 exit:
624 RPCRT4_ReleaseConnection(conn);
625 return 0;
628 void RPCRT4_new_client(RpcConnection* conn)
630 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
631 if (!thread) {
632 DWORD err = GetLastError();
633 ERR("failed to create thread, error=%08x\n", err);
634 RPCRT4_ReleaseConnection(conn);
636 /* we could set conn->thread, but then we'd have to make the io_thread wait
637 * for that, otherwise the thread might finish, destroy the connection, and
638 * free the memory we'd write to before we did, causing crashes and stuff -
639 * so let's implement that later, when we really need conn->thread */
641 CloseHandle( thread );
644 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
646 int res;
647 unsigned int count;
648 void *objs = NULL;
649 RpcServerProtseq* cps = the_arg;
650 RpcConnection* conn;
651 BOOL set_ready_event = FALSE;
653 TRACE("(the_arg == ^%p)\n", the_arg);
655 for (;;) {
656 objs = cps->ops->get_wait_array(cps, objs, &count);
658 if (set_ready_event)
660 /* signal to function that changed state that we are now sync'ed */
661 SetEvent(cps->server_ready_event);
662 set_ready_event = FALSE;
665 /* start waiting */
666 res = cps->ops->wait_for_new_connection(cps, count, objs);
668 if (res == -1 || (res == 0 && !std_listen))
670 /* cleanup */
671 cps->ops->free_wait_array(cps, objs);
672 EnterCriticalSection(&cps->cs);
673 LIST_FOR_EACH_ENTRY(conn, &cps->connections, RpcConnection, protseq_entry)
674 RPCRT4_CloseConnection(conn);
675 LeaveCriticalSection(&cps->cs);
677 if (res == 0 && !std_listen)
678 SetEvent(cps->server_ready_event);
679 break;
681 else if (res == 0)
682 set_ready_event = TRUE;
684 return 0;
687 /* tells the server thread that the state has changed and waits for it to
688 * make the changes */
689 static void RPCRT4_sync_with_server_thread(RpcServerProtseq *ps)
691 /* make sure we are the only thread sync'ing the server state, otherwise
692 * there is a race with the server thread setting an older state and setting
693 * the server_ready_event when the new state hasn't yet been applied */
694 WaitForSingleObject(ps->mgr_mutex, INFINITE);
696 ps->ops->signal_state_changed(ps);
698 /* wait for server thread to make the requested changes before returning */
699 WaitForSingleObject(ps->server_ready_event, INFINITE);
701 ReleaseMutex(ps->mgr_mutex);
704 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
706 RPC_STATUS status = RPC_S_OK;
707 HANDLE server_thread;
709 EnterCriticalSection(&listen_cs);
710 if (ps->is_listening) goto done;
712 if (!ps->mgr_mutex) ps->mgr_mutex = CreateMutexW(NULL, FALSE, NULL);
713 if (!ps->server_ready_event) ps->server_ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
714 server_thread = CreateThread(NULL, 0, RPCRT4_server_thread, ps, 0, NULL);
715 if (!server_thread)
717 status = RPC_S_OUT_OF_RESOURCES;
718 goto done;
720 ps->is_listening = TRUE;
721 CloseHandle(server_thread);
723 done:
724 LeaveCriticalSection(&listen_cs);
725 return status;
728 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
730 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
731 RpcServerProtseq *cps;
733 TRACE("\n");
735 EnterCriticalSection(&listen_cs);
736 if (auto_listen || !listen_done_event)
738 status = RPC_S_OK;
739 if(!auto_listen)
740 listen_done_event = CreateEventW(NULL, TRUE, FALSE, NULL);
741 if (++listen_count == 1)
742 std_listen = TRUE;
744 LeaveCriticalSection(&listen_cs);
745 if (status) return status;
747 if (std_listen)
749 EnterCriticalSection(&server_cs);
750 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
752 status = RPCRT4_start_listen_protseq(cps, TRUE);
753 if (status != RPC_S_OK)
754 break;
756 /* make sure server is actually listening on the interface before
757 * returning */
758 RPCRT4_sync_with_server_thread(cps);
760 LeaveCriticalSection(&server_cs);
763 return status;
766 static RPC_STATUS RPCRT4_stop_listen(BOOL auto_listen)
768 BOOL stop_listen = FALSE;
769 RPC_STATUS status = RPC_S_OK;
771 EnterCriticalSection(&listen_cs);
772 if (!std_listen && (auto_listen || !listen_done_event))
774 status = RPC_S_NOT_LISTENING;
776 else
778 stop_listen = listen_count != 0 && --listen_count == 0;
779 assert(listen_count >= 0);
780 if (stop_listen)
781 std_listen = FALSE;
783 LeaveCriticalSection(&listen_cs);
785 if (status) return status;
787 if (stop_listen) {
788 RpcServerProtseq *cps;
789 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
790 RPCRT4_sync_with_server_thread(cps);
793 if (!auto_listen)
795 EnterCriticalSection(&listen_cs);
796 SetEvent( listen_done_event );
797 LeaveCriticalSection(&listen_cs);
799 return RPC_S_OK;
802 static BOOL RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq *protseq, const char *endpoint)
804 RpcConnection *conn;
805 BOOL registered = FALSE;
806 EnterCriticalSection(&protseq->cs);
807 LIST_FOR_EACH_ENTRY(conn, &protseq->connections, RpcConnection, protseq_entry) {
808 if (!endpoint || !strcmp(endpoint, conn->Endpoint)) {
809 registered = TRUE;
810 break;
813 LeaveCriticalSection(&protseq->cs);
814 return registered;
817 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, const char *endpoint)
819 RPC_STATUS status;
821 EnterCriticalSection(&ps->cs);
823 if (RPCRT4_protseq_is_endpoint_registered(ps, endpoint))
824 status = RPC_S_OK;
825 else
826 status = ps->ops->open_endpoint(ps, endpoint);
828 LeaveCriticalSection(&ps->cs);
830 if (status != RPC_S_OK)
831 return status;
833 if (std_listen)
835 status = RPCRT4_start_listen_protseq(ps, FALSE);
836 if (status == RPC_S_OK)
837 RPCRT4_sync_with_server_thread(ps);
840 return status;
843 /***********************************************************************
844 * RpcServerInqBindings (RPCRT4.@)
846 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
848 RPC_STATUS status;
849 DWORD count;
850 RpcServerProtseq* ps;
851 RpcConnection* conn;
853 if (BindingVector)
854 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
855 else
856 ERR("(BindingVector == NULL!!?)\n");
858 EnterCriticalSection(&server_cs);
859 /* count connections */
860 count = 0;
861 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
862 EnterCriticalSection(&ps->cs);
863 LIST_FOR_EACH_ENTRY(conn, &ps->connections, RpcConnection, protseq_entry)
864 count++;
865 LeaveCriticalSection(&ps->cs);
867 if (count) {
868 /* export bindings */
869 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
870 sizeof(RPC_BINDING_VECTOR) +
871 sizeof(RPC_BINDING_HANDLE)*(count-1));
872 (*BindingVector)->Count = count;
873 count = 0;
874 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
875 EnterCriticalSection(&ps->cs);
876 LIST_FOR_EACH_ENTRY(conn, &ps->connections, RpcConnection, protseq_entry) {
877 RPCRT4_MakeBinding((RpcBinding**)&(*BindingVector)->BindingH[count],
878 conn);
879 count++;
881 LeaveCriticalSection(&ps->cs);
883 status = RPC_S_OK;
884 } else {
885 *BindingVector = NULL;
886 status = RPC_S_NO_BINDINGS;
888 LeaveCriticalSection(&server_cs);
889 return status;
892 /***********************************************************************
893 * RpcServerUseProtseqEpA (RPCRT4.@)
895 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
897 RPC_POLICY policy;
899 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
901 /* This should provide the default behaviour */
902 policy.Length = sizeof( policy );
903 policy.EndpointFlags = 0;
904 policy.NICFlags = 0;
906 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
909 /***********************************************************************
910 * RpcServerUseProtseqEpW (RPCRT4.@)
912 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
914 RPC_POLICY policy;
916 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
918 /* This should provide the default behaviour */
919 policy.Length = sizeof( policy );
920 policy.EndpointFlags = 0;
921 policy.NICFlags = 0;
923 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
926 /***********************************************************************
927 * alloc_serverprotoseq (internal)
929 * Must be called with server_cs held.
931 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
933 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
935 if (!ops)
937 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
938 return RPC_S_PROTSEQ_NOT_SUPPORTED;
941 *ps = ops->alloc();
942 if (!*ps)
943 return RPC_S_OUT_OF_RESOURCES;
944 (*ps)->MaxCalls = MaxCalls;
945 (*ps)->Protseq = RPCRT4_strdupA(Protseq);
946 (*ps)->ops = ops;
947 (*ps)->MaxCalls = 0;
948 list_init(&(*ps)->connections);
949 InitializeCriticalSection(&(*ps)->cs);
950 (*ps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RpcServerProtseq.cs");
951 (*ps)->is_listening = FALSE;
952 (*ps)->mgr_mutex = NULL;
953 (*ps)->server_ready_event = NULL;
955 list_add_head(&protseqs, &(*ps)->entry);
957 TRACE("new protseq %p created for %s\n", *ps, Protseq);
959 return RPC_S_OK;
962 /* must be called with server_cs held */
963 static void destroy_serverprotoseq(RpcServerProtseq *ps)
965 RPCRT4_strfree(ps->Protseq);
966 ps->cs.DebugInfo->Spare[0] = 0;
967 DeleteCriticalSection(&ps->cs);
968 CloseHandle(ps->mgr_mutex);
969 CloseHandle(ps->server_ready_event);
970 list_remove(&ps->entry);
971 HeapFree(GetProcessHeap(), 0, ps);
974 /* Finds a given protseq or creates a new one if one doesn't already exist */
975 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
977 RPC_STATUS status;
978 RpcServerProtseq *cps;
980 EnterCriticalSection(&server_cs);
982 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
983 if (!strcmp(cps->Protseq, Protseq))
985 TRACE("found existing protseq object for %s\n", Protseq);
986 *ps = cps;
987 LeaveCriticalSection(&server_cs);
988 return S_OK;
991 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
993 LeaveCriticalSection(&server_cs);
995 return status;
998 /***********************************************************************
999 * RpcServerUseProtseqEpExA (RPCRT4.@)
1001 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
1002 PRPC_POLICY lpPolicy )
1004 RpcServerProtseq* ps;
1005 RPC_STATUS status;
1007 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq),
1008 MaxCalls, debugstr_a((const char *)Endpoint), SecurityDescriptor,
1009 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
1011 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
1012 if (status != RPC_S_OK)
1013 return status;
1015 return RPCRT4_use_protseq(ps, (const char *)Endpoint);
1018 /***********************************************************************
1019 * RpcServerUseProtseqEpExW (RPCRT4.@)
1021 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
1022 PRPC_POLICY lpPolicy )
1024 RpcServerProtseq* ps;
1025 RPC_STATUS status;
1026 LPSTR ProtseqA;
1027 LPSTR EndpointA;
1029 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq ), MaxCalls,
1030 debugstr_w( Endpoint ), SecurityDescriptor,
1031 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
1033 ProtseqA = RPCRT4_strdupWtoA(Protseq);
1034 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
1035 RPCRT4_strfree(ProtseqA);
1036 if (status != RPC_S_OK)
1037 return status;
1039 EndpointA = RPCRT4_strdupWtoA(Endpoint);
1040 status = RPCRT4_use_protseq(ps, EndpointA);
1041 RPCRT4_strfree(EndpointA);
1042 return status;
1045 /***********************************************************************
1046 * RpcServerUseProtseqA (RPCRT4.@)
1048 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1050 RPC_STATUS status;
1051 RpcServerProtseq* ps;
1053 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
1055 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
1056 if (status != RPC_S_OK)
1057 return status;
1059 return RPCRT4_use_protseq(ps, NULL);
1062 /***********************************************************************
1063 * RpcServerUseProtseqW (RPCRT4.@)
1065 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1067 RPC_STATUS status;
1068 RpcServerProtseq* ps;
1069 LPSTR ProtseqA;
1071 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
1073 ProtseqA = RPCRT4_strdupWtoA(Protseq);
1074 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
1075 RPCRT4_strfree(ProtseqA);
1076 if (status != RPC_S_OK)
1077 return status;
1079 return RPCRT4_use_protseq(ps, NULL);
1082 void RPCRT4_destroy_all_protseqs(void)
1084 RpcServerProtseq *cps, *cursor2;
1086 if (listen_count != 0)
1087 std_listen = FALSE;
1089 EnterCriticalSection(&server_cs);
1090 LIST_FOR_EACH_ENTRY_SAFE(cps, cursor2, &protseqs, RpcServerProtseq, entry)
1092 if (listen_count != 0)
1093 RPCRT4_sync_with_server_thread(cps);
1094 destroy_serverprotoseq(cps);
1096 LeaveCriticalSection(&server_cs);
1097 DeleteCriticalSection(&server_cs);
1098 DeleteCriticalSection(&listen_cs);
1101 /***********************************************************************
1102 * RpcServerRegisterIf (RPCRT4.@)
1104 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
1106 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
1107 return RpcServerRegisterIf3( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL, NULL );
1110 /***********************************************************************
1111 * RpcServerRegisterIfEx (RPCRT4.@)
1113 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1114 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
1116 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
1117 return RpcServerRegisterIf3( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn, NULL );
1120 /***********************************************************************
1121 * RpcServerRegisterIf2 (RPCRT4.@)
1123 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1124 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
1126 return RpcServerRegisterIf3( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, MaxRpcSize, IfCallbackFn, NULL );
1129 /***********************************************************************
1130 * RpcServerRegisterIf3 (RPCRT4.@)
1132 RPC_STATUS WINAPI RpcServerRegisterIf3( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1133 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn, void* SecurityDescriptor)
1135 PRPC_SERVER_INTERFACE If = IfSpec;
1136 RpcServerInterface* sif;
1137 unsigned int i;
1139 TRACE("(%p,%s,%p,%u,%u,%u,%p,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
1140 MaxRpcSize, IfCallbackFn, SecurityDescriptor);
1142 if (SecurityDescriptor)
1143 FIXME("Unsupported SecurityDescriptor argument.\n");
1145 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
1146 If->InterfaceId.SyntaxVersion.MajorVersion,
1147 If->InterfaceId.SyntaxVersion.MinorVersion);
1148 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
1149 If->TransferSyntax.SyntaxVersion.MajorVersion,
1150 If->TransferSyntax.SyntaxVersion.MinorVersion);
1151 TRACE(" dispatch table: %p\n", If->DispatchTable);
1152 if (If->DispatchTable) {
1153 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
1154 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
1155 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
1157 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
1159 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
1160 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
1161 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
1163 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
1164 sif->If = If;
1165 if (MgrTypeUuid) {
1166 sif->MgrTypeUuid = *MgrTypeUuid;
1167 sif->MgrEpv = MgrEpv;
1168 } else {
1169 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
1170 sif->MgrEpv = If->DefaultManagerEpv;
1172 sif->Flags = Flags;
1173 sif->MaxCalls = MaxCalls;
1174 sif->MaxRpcSize = MaxRpcSize;
1175 sif->IfCallbackFn = IfCallbackFn;
1177 EnterCriticalSection(&server_cs);
1178 list_add_head(&server_interfaces, &sif->entry);
1179 LeaveCriticalSection(&server_cs);
1181 if (sif->Flags & RPC_IF_AUTOLISTEN)
1182 RPCRT4_start_listen(TRUE);
1184 return RPC_S_OK;
1187 /***********************************************************************
1188 * RpcServerUnregisterIf (RPCRT4.@)
1190 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
1192 PRPC_SERVER_INTERFACE If = IfSpec;
1193 HANDLE event = NULL;
1194 BOOL found = FALSE;
1195 BOOL completed = TRUE;
1196 RpcServerInterface *cif;
1197 RPC_STATUS status;
1199 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1200 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
1202 EnterCriticalSection(&server_cs);
1203 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
1204 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
1205 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
1206 list_remove(&cif->entry);
1207 TRACE("unregistering cif %p\n", cif);
1208 if (cif->CurrentCalls) {
1209 completed = FALSE;
1210 cif->Delete = TRUE;
1211 if (WaitForCallsToComplete)
1212 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
1214 found = TRUE;
1215 break;
1218 LeaveCriticalSection(&server_cs);
1220 if (!found) {
1221 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
1222 return RPC_S_UNKNOWN_IF;
1225 if (completed)
1226 HeapFree(GetProcessHeap(), 0, cif);
1227 else if (event) {
1228 /* sif will be freed when the last call is completed, so be careful not to
1229 * touch that memory here as that could happen before we get here */
1230 WaitForSingleObject(event, INFINITE);
1231 CloseHandle(event);
1234 return RPC_S_OK;
1237 /***********************************************************************
1238 * RpcServerUnregisterIfEx (RPCRT4.@)
1240 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
1242 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1243 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
1245 return RPC_S_OK;
1248 /***********************************************************************
1249 * RpcObjectSetType (RPCRT4.@)
1251 * PARAMS
1252 * ObjUuid [I] "Object" UUID
1253 * TypeUuid [I] "Type" UUID
1255 * RETURNS
1256 * RPC_S_OK The call succeeded
1257 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1258 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1260 * Maps "Object" UUIDs to "Type" UUIDs. Passing the nil UUID as the type
1261 * resets the mapping for the specified object UUID to nil (the default).
1262 * The nil object is always associated with the nil type and cannot be
1263 * reassigned. Servers can support multiple implementations on the same
1264 * interface by registering different end-point vectors for the different
1265 * types. There's no need to call this if a server only supports the nil
1266 * type, as is typical.
1268 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
1270 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
1271 RPC_STATUS dummy;
1273 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
1274 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
1275 /* nil uuid cannot be remapped */
1276 return RPC_S_INVALID_OBJECT;
1279 /* find the mapping for this object if there is one ... */
1280 while (map) {
1281 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
1282 prev = map;
1283 map = map->next;
1285 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
1286 /* ... and drop it from the list */
1287 if (map) {
1288 if (prev)
1289 prev->next = map->next;
1290 else
1291 RpcObjTypeMaps = map->next;
1292 HeapFree(GetProcessHeap(), 0, map);
1294 } else {
1295 /* ... , fail if we found it ... */
1296 if (map)
1297 return RPC_S_ALREADY_REGISTERED;
1298 /* ... otherwise create a new one and add it in. */
1299 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
1300 map->Object = *ObjUuid;
1301 map->Type = *TypeUuid;
1302 map->next = NULL;
1303 if (prev)
1304 prev->next = map; /* prev is the last map in the linklist */
1305 else
1306 RpcObjTypeMaps = map;
1309 return RPC_S_OK;
1312 struct rpc_server_registered_auth_info
1314 struct list entry;
1315 USHORT auth_type;
1316 WCHAR *package_name;
1317 WCHAR *principal;
1318 ULONG max_token;
1321 static RPC_STATUS find_security_package(ULONG auth_type, SecPkgInfoW **packages_buf, SecPkgInfoW **ret)
1323 SECURITY_STATUS sec_status;
1324 SecPkgInfoW *packages;
1325 ULONG package_count;
1326 ULONG i;
1328 sec_status = EnumerateSecurityPackagesW(&package_count, &packages);
1329 if (sec_status != SEC_E_OK)
1331 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", sec_status);
1332 return RPC_S_SEC_PKG_ERROR;
1335 for (i = 0; i < package_count; i++)
1336 if (packages[i].wRPCID == auth_type)
1337 break;
1339 if (i == package_count)
1341 WARN("unsupported AuthnSvc %u\n", auth_type);
1342 FreeContextBuffer(packages);
1343 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1346 TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name), auth_type);
1347 *packages_buf = packages;
1348 *ret = packages + i;
1349 return RPC_S_OK;
1352 RPC_STATUS RPCRT4_ServerGetRegisteredAuthInfo(
1353 USHORT auth_type, CredHandle *cred, TimeStamp *exp, ULONG *max_token)
1355 RPC_STATUS status = RPC_S_UNKNOWN_AUTHN_SERVICE;
1356 struct rpc_server_registered_auth_info *auth_info;
1357 SECURITY_STATUS sec_status;
1359 EnterCriticalSection(&server_auth_info_cs);
1360 LIST_FOR_EACH_ENTRY(auth_info, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1362 if (auth_info->auth_type == auth_type)
1364 sec_status = AcquireCredentialsHandleW((SEC_WCHAR *)auth_info->principal, auth_info->package_name,
1365 SECPKG_CRED_INBOUND, NULL, NULL, NULL, NULL,
1366 cred, exp);
1367 if (sec_status != SEC_E_OK)
1369 status = RPC_S_SEC_PKG_ERROR;
1370 break;
1373 *max_token = auth_info->max_token;
1374 status = RPC_S_OK;
1375 break;
1378 LeaveCriticalSection(&server_auth_info_cs);
1380 return status;
1383 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1385 struct rpc_server_registered_auth_info *auth_info, *cursor2;
1387 EnterCriticalSection(&server_auth_info_cs);
1388 LIST_FOR_EACH_ENTRY_SAFE(auth_info, cursor2, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1390 HeapFree(GetProcessHeap(), 0, auth_info->package_name);
1391 HeapFree(GetProcessHeap(), 0, auth_info->principal);
1392 HeapFree(GetProcessHeap(), 0, auth_info);
1394 LeaveCriticalSection(&server_auth_info_cs);
1395 DeleteCriticalSection(&server_auth_info_cs);
1398 /***********************************************************************
1399 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1401 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1402 LPVOID Arg )
1404 WCHAR *principal_name = NULL;
1405 RPC_STATUS status;
1407 TRACE("(%s,%u,%p,%p)\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg);
1409 if(ServerPrincName && !(principal_name = RPCRT4_strdupAtoW((const char*)ServerPrincName)))
1410 return RPC_S_OUT_OF_RESOURCES;
1412 status = RpcServerRegisterAuthInfoW(principal_name, AuthnSvc, GetKeyFn, Arg);
1414 HeapFree(GetProcessHeap(), 0, principal_name);
1415 return status;
1418 /***********************************************************************
1419 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1421 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1422 LPVOID Arg )
1424 struct rpc_server_registered_auth_info *auth_info;
1425 SecPkgInfoW *packages, *package;
1426 WCHAR *package_name;
1427 ULONG max_token;
1428 RPC_STATUS status;
1430 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName), AuthnSvc, GetKeyFn, Arg);
1432 status = find_security_package(AuthnSvc, &packages, &package);
1433 if (status != RPC_S_OK)
1434 return status;
1436 package_name = RPCRT4_strdupW(package->Name);
1437 max_token = package->cbMaxToken;
1438 FreeContextBuffer(packages);
1439 if (!package_name)
1440 return RPC_S_OUT_OF_RESOURCES;
1442 auth_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*auth_info));
1443 if (!auth_info) {
1444 HeapFree(GetProcessHeap(), 0, package_name);
1445 return RPC_S_OUT_OF_RESOURCES;
1448 if (ServerPrincName && !(auth_info->principal = RPCRT4_strdupW(ServerPrincName))) {
1449 HeapFree(GetProcessHeap(), 0, package_name);
1450 HeapFree(GetProcessHeap(), 0, auth_info);
1451 return RPC_S_OUT_OF_RESOURCES;
1454 auth_info->auth_type = AuthnSvc;
1455 auth_info->package_name = package_name;
1456 auth_info->max_token = max_token;
1458 EnterCriticalSection(&server_auth_info_cs);
1459 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1460 LeaveCriticalSection(&server_auth_info_cs);
1462 return RPC_S_OK;
1465 /******************************************************************************
1466 * RpcServerInqDefaultPrincNameA (rpcrt4.@)
1468 RPC_STATUS RPC_ENTRY RpcServerInqDefaultPrincNameA(ULONG AuthnSvc, RPC_CSTR *PrincName)
1470 RPC_STATUS ret;
1471 RPC_WSTR principalW;
1473 TRACE("%u, %p\n", AuthnSvc, PrincName);
1475 if ((ret = RpcServerInqDefaultPrincNameW( AuthnSvc, &principalW )) == RPC_S_OK)
1477 if (!(*PrincName = (RPC_CSTR)RPCRT4_strdupWtoA( principalW ))) return RPC_S_OUT_OF_MEMORY;
1478 RpcStringFreeW( &principalW );
1480 return ret;
1483 /******************************************************************************
1484 * RpcServerInqDefaultPrincNameW (rpcrt4.@)
1486 RPC_STATUS RPC_ENTRY RpcServerInqDefaultPrincNameW(ULONG AuthnSvc, RPC_WSTR *PrincName)
1488 ULONG len = 0;
1490 FIXME("%u, %p\n", AuthnSvc, PrincName);
1492 if (AuthnSvc != RPC_C_AUTHN_WINNT) return RPC_S_UNKNOWN_AUTHN_SERVICE;
1494 GetUserNameExW( NameSamCompatible, NULL, &len );
1495 if (GetLastError() != ERROR_MORE_DATA) return RPC_S_INTERNAL_ERROR;
1497 if (!(*PrincName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1498 return RPC_S_OUT_OF_MEMORY;
1500 GetUserNameExW( NameSamCompatible, *PrincName, &len );
1501 return RPC_S_OK;
1504 /***********************************************************************
1505 * RpcServerListen (RPCRT4.@)
1507 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1509 RPC_STATUS status = RPC_S_OK;
1511 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1513 if (list_empty(&protseqs))
1514 return RPC_S_NO_PROTSEQS_REGISTERED;
1516 status = RPCRT4_start_listen(FALSE);
1518 if (DontWait || (status != RPC_S_OK)) return status;
1520 return RpcMgmtWaitServerListen();
1523 /***********************************************************************
1524 * RpcMgmtServerWaitListen (RPCRT4.@)
1526 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1528 HANDLE event;
1530 TRACE("()\n");
1532 EnterCriticalSection(&listen_cs);
1533 event = listen_done_event;
1534 LeaveCriticalSection(&listen_cs);
1536 if (!event)
1537 return RPC_S_NOT_LISTENING;
1539 TRACE( "waiting for server calls to finish\n" );
1540 WaitForSingleObject( event, INFINITE );
1541 TRACE( "done waiting\n" );
1543 EnterCriticalSection(&listen_cs);
1544 if (listen_done_event == event)
1546 listen_done_event = NULL;
1547 CloseHandle( event );
1549 LeaveCriticalSection(&listen_cs);
1550 return RPC_S_OK;
1553 /***********************************************************************
1554 * RpcMgmtStopServerListening (RPCRT4.@)
1556 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1558 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1560 if (Binding) {
1561 FIXME("client-side invocation not implemented.\n");
1562 return RPC_S_WRONG_KIND_OF_BINDING;
1565 return RPCRT4_stop_listen(FALSE);
1568 /***********************************************************************
1569 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1571 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1573 FIXME("(): stub\n");
1574 return RPC_S_OK;
1577 /***********************************************************************
1578 * I_RpcServerStartListening (RPCRT4.@)
1580 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1582 FIXME( "(%p): stub\n", hWnd );
1584 return RPC_S_OK;
1587 /***********************************************************************
1588 * I_RpcServerStopListening (RPCRT4.@)
1590 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1592 FIXME( "(): stub\n" );
1594 return RPC_S_OK;
1597 /***********************************************************************
1598 * I_RpcWindowProc (RPCRT4.@)
1600 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1602 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1604 return 0;
1607 /***********************************************************************
1608 * RpcMgmtInqIfIds (RPCRT4.@)
1610 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1612 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1613 return RPC_S_INVALID_BINDING;
1616 /***********************************************************************
1617 * RpcMgmtInqStats (RPCRT4.@)
1619 RPC_STATUS WINAPI RpcMgmtInqStats(RPC_BINDING_HANDLE Binding, RPC_STATS_VECTOR **Statistics)
1621 RPC_STATS_VECTOR *stats;
1623 FIXME("(%p,%p)\n", Binding, Statistics);
1625 if ((stats = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR))))
1627 stats->Count = 1;
1628 stats->Stats[0] = 0;
1629 *Statistics = stats;
1630 return RPC_S_OK;
1632 return RPC_S_OUT_OF_RESOURCES;
1635 /***********************************************************************
1636 * RpcMgmtStatsVectorFree (RPCRT4.@)
1638 RPC_STATUS WINAPI RpcMgmtStatsVectorFree(RPC_STATS_VECTOR **StatsVector)
1640 FIXME("(%p)\n", StatsVector);
1642 if (StatsVector)
1644 HeapFree(GetProcessHeap(), 0, *StatsVector);
1645 *StatsVector = NULL;
1647 return RPC_S_OK;
1650 /***********************************************************************
1651 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1653 RPC_STATUS WINAPI RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding, ULONG InquiryType,
1654 RPC_IF_ID *IfId, ULONG VersOption, UUID *ObjectUuid, RPC_EP_INQ_HANDLE* InquiryContext)
1656 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1657 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1658 return RPC_S_INVALID_BINDING;
1661 /***********************************************************************
1662 * RpcMgmtIsServerListening (RPCRT4.@)
1664 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1666 RPC_STATUS status = RPC_S_NOT_LISTENING;
1668 TRACE("(%p)\n", Binding);
1670 if (Binding) {
1671 RpcBinding *rpc_binding = (RpcBinding*)Binding;
1672 status = RPCRT4_IsServerListening(rpc_binding->Protseq, rpc_binding->Endpoint);
1673 }else {
1674 EnterCriticalSection(&listen_cs);
1675 if (listen_done_event && std_listen) status = RPC_S_OK;
1676 LeaveCriticalSection(&listen_cs);
1679 return status;
1682 /***********************************************************************
1683 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1685 RPC_STATUS WINAPI RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn)
1687 FIXME("(%p): stub\n", fn);
1688 return RPC_S_OK;
1691 /***********************************************************************
1692 * RpcMgmtSetServerStackSize (RPCRT4.@)
1694 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1696 FIXME("(0x%x): stub\n", ThreadStackSize);
1697 return RPC_S_OK;
1700 /***********************************************************************
1701 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1703 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1705 TRACE("\n");
1706 return RPCRT4_GetThreadCurrentCallHandle();