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
24 #include "wine/port.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"
46 #include "ncastatus.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
51 typedef struct _RpcPacket
53 struct _RpcConnection* conn;
56 unsigned char *auth_data;
60 typedef struct _RpcObjTypeMap
62 /* FIXME: a hash table would be better. */
63 struct _RpcObjTypeMap *next;
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 =
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 =
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 /* number of manual listeners (calls to RpcServerListen) */
105 static LONG manual_listen_count;
106 /* total listeners including auto listeners */
107 static LONG listen_count;
108 /* event set once all listening is finished */
109 static HANDLE listen_done_event;
111 static UUID uuid_nil;
113 static inline RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
115 RpcObjTypeMap *rslt = RpcObjTypeMaps;
119 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
126 static inline UUID *LookupObjType(UUID *ObjUuid)
128 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
135 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
136 const RPC_SYNTAX_IDENTIFIER *if_id,
137 const RPC_SYNTAX_IDENTIFIER *transfer_syntax,
140 UUID* MgrType = NULL;
141 RpcServerInterface* cif;
145 MgrType = LookupObjType(object);
146 EnterCriticalSection(&server_cs);
147 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
148 if (!memcmp(if_id, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER)) &&
149 (!transfer_syntax || !memcmp(transfer_syntax, &cif->If->TransferSyntax, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
150 (check_object == FALSE || UuidEqual(MgrType, &cif->MgrTypeUuid, &status)) &&
152 InterlockedIncrement(&cif->CurrentCalls);
156 LeaveCriticalSection(&server_cs);
157 if (&cif->entry == &server_interfaces) cif = NULL;
158 TRACE("returning %p for object %s, if_id { %d.%d %s }\n", cif,
159 debugstr_guid(object), if_id->SyntaxVersion.MajorVersion,
160 if_id->SyntaxVersion.MinorVersion, debugstr_guid(&if_id->SyntaxGUID));
164 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
166 if (!InterlockedDecrement(&sif->CurrentCalls) &&
168 /* sif must have been removed from server_interfaces before
169 * CallsCompletedEvent is set */
170 if (sif->CallsCompletedEvent)
171 SetEvent(sif->CallsCompletedEvent);
172 HeapFree(GetProcessHeap(), 0, sif);
176 static RpcPktHdr *handle_bind_error(RpcConnection *conn, RPC_STATUS error)
178 unsigned int reject_reason;
181 case RPC_S_SERVER_TOO_BUSY:
182 reject_reason = REJECT_TEMPORARY_CONGESTION;
184 case ERROR_OUTOFMEMORY:
185 case RPC_S_OUT_OF_RESOURCES:
186 reject_reason = REJECT_LOCAL_LIMIT_EXCEEDED;
188 case RPC_S_PROTOCOL_ERROR:
189 reject_reason = REJECT_PROTOCOL_VERSION_NOT_SUPPORTED;
191 case RPC_S_UNKNOWN_AUTHN_SERVICE:
192 reject_reason = REJECT_UNKNOWN_AUTHN_SERVICE;
194 case ERROR_ACCESS_DENIED:
195 reject_reason = REJECT_INVALID_CHECKSUM;
198 FIXME("unexpected status value %d\n", error);
200 case RPC_S_INVALID_BOUND:
201 reject_reason = REJECT_REASON_NOT_SPECIFIED;
204 return RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
205 RPC_VER_MAJOR, RPC_VER_MINOR,
209 static RPC_STATUS process_bind_packet_no_send(
210 RpcConnection *conn, RpcPktBindHdr *hdr, RPC_MESSAGE *msg,
211 unsigned char *auth_data, ULONG auth_length, RpcPktHdr **ack_response,
212 unsigned char **auth_data_out, ULONG *auth_length_out)
215 RpcContextElement *ctxt_elem;
220 for (i = 0, ctxt_elem = msg->Buffer;
221 i < hdr->num_elements;
222 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
224 if (((char *)ctxt_elem - (char *)msg->Buffer) > msg->BufferLength ||
225 ((char *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes] - (char *)msg->Buffer) > msg->BufferLength)
227 ERR("inconsistent data in packet - packet length %d, num elements %d\n",
228 msg->BufferLength, hdr->num_elements);
229 return RPC_S_INVALID_BOUND;
233 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
234 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
235 conn->server_binding)
237 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
239 return RPC_S_INVALID_BOUND;
242 results = HeapAlloc(GetProcessHeap(), 0,
243 hdr->num_elements * sizeof(*results));
245 return RPC_S_OUT_OF_RESOURCES;
247 for (i = 0, ctxt_elem = (RpcContextElement *)msg->Buffer;
248 i < hdr->num_elements;
249 i++, ctxt_elem = (RpcContextElement *)&ctxt_elem->transfer_syntaxes[ctxt_elem->num_syntaxes])
251 RpcServerInterface* sif = NULL;
254 for (j = 0; !sif && j < ctxt_elem->num_syntaxes; j++)
256 sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
257 &ctxt_elem->transfer_syntaxes[j], FALSE);
263 RPCRT4_release_server_interface(sif);
264 TRACE("accepting bind request on connection %p for %s\n", conn,
265 debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
266 results[i].result = RESULT_ACCEPT;
267 results[i].reason = REASON_NONE;
268 results[i].transfer_syntax = ctxt_elem->transfer_syntaxes[j];
270 /* save the interface for later use */
271 /* FIXME: save linked list */
272 conn->ActiveInterface = ctxt_elem->abstract_syntax;
274 else if ((sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
275 NULL, FALSE)) != NULL)
277 RPCRT4_release_server_interface(sif);
278 TRACE("not accepting bind request on connection %p for %s - no transfer syntaxes supported\n",
279 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
280 results[i].result = RESULT_PROVIDER_REJECTION;
281 results[i].reason = REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED;
282 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
286 TRACE("not accepting bind request on connection %p for %s - abstract syntax not supported\n",
287 conn, debugstr_guid(&ctxt_elem->abstract_syntax.SyntaxGUID));
288 results[i].result = RESULT_PROVIDER_REJECTION;
289 results[i].reason = REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED;
290 memset(&results[i].transfer_syntax, 0, sizeof(results[i].transfer_syntax));
294 /* create temporary binding */
295 status = RPCRT4_MakeBinding(&conn->server_binding, conn);
296 if (status != RPC_S_OK)
298 HeapFree(GetProcessHeap(), 0, results);
302 status = RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
303 conn->NetworkAddr, conn->Endpoint,
304 conn->NetworkOptions,
306 &conn->server_binding->Assoc);
307 if (status != RPC_S_OK)
309 HeapFree(GetProcessHeap(), 0, results);
315 status = RPCRT4_ServerConnectionAuth(conn, TRUE,
316 (RpcAuthVerifier *)auth_data,
317 auth_length, auth_data_out,
319 if (status != RPC_S_OK)
321 HeapFree(GetProcessHeap(), 0, results);
326 *ack_response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
329 conn->server_binding->Assoc->assoc_group_id,
330 conn->Endpoint, hdr->num_elements,
332 HeapFree(GetProcessHeap(), 0, results);
335 conn->MaxTransmissionSize = hdr->max_tsize;
337 status = RPC_S_OUT_OF_RESOURCES;
342 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr,
344 unsigned char *auth_data,
348 RpcPktHdr *response = NULL;
349 unsigned char *auth_data_out = NULL;
350 ULONG auth_length_out = 0;
352 status = process_bind_packet_no_send(conn, hdr, msg, auth_data, auth_length,
353 &response, &auth_data_out,
355 if (status != RPC_S_OK)
356 response = handle_bind_error(conn, status);
358 status = RPCRT4_SendWithAuth(conn, response, NULL, 0, auth_data_out, auth_length_out);
360 status = ERROR_OUTOFMEMORY;
361 RPCRT4_FreeHeader(response);
367 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
370 RpcPktHdr *response = NULL;
371 RpcServerInterface* sif;
372 RPC_DISPATCH_FUNCTION func;
375 NDR_SCONTEXT context_handle;
376 void *buf = msg->Buffer;
378 /* fail if the connection isn't bound with an interface */
379 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status)) {
380 /* FIXME: should send BindNack instead */
381 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
384 RPCRT4_Send(conn, response, NULL, 0);
385 RPCRT4_FreeHeader(response);
389 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
390 object_uuid = (UUID*)(hdr + 1);
395 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, NULL, TRUE);
397 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
398 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
401 RPCRT4_Send(conn, response, NULL, 0);
402 RPCRT4_FreeHeader(response);
405 msg->RpcInterfaceInformation = sif->If;
406 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
407 msg->ManagerEpv = sif->MgrEpv;
408 if (object_uuid != NULL) {
409 RPCRT4_SetBindingObject(msg->Handle, object_uuid);
412 /* find dispatch function */
413 msg->ProcNum = hdr->opnum;
414 if (sif->Flags & RPC_IF_OLE) {
415 /* native ole32 always gives us a dispatch table with a single entry
416 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
417 func = *sif->If->DispatchTable->DispatchTable;
419 if (msg->ProcNum >= sif->If->DispatchTable->DispatchTableCount) {
420 WARN("invalid procnum (%d/%d)\n", msg->ProcNum, sif->If->DispatchTable->DispatchTableCount);
421 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
424 RPCRT4_Send(conn, response, NULL, 0);
425 RPCRT4_FreeHeader(response);
427 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
430 /* put in the drep. FIXME: is this more universally applicable?
431 perhaps we should move this outward... */
432 msg->DataRepresentation =
433 MAKELONG( MAKEWORD(hdr->common.drep[0], hdr->common.drep[1]),
434 MAKEWORD(hdr->common.drep[2], hdr->common.drep[3]));
439 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
443 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
445 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
446 status = ERROR_NOACCESS;
448 status = GetExceptionCode();
449 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
450 RPC2NCA_STATUS(status));
452 RPCRT4_SetThreadCurrentCallHandle(NULL);
454 /* release any unmarshalled context handles */
455 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
456 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
459 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
462 /* send response packet */
464 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
465 exception ? 0 : msg->BufferLength);
466 RPCRT4_FreeHeader(response);
468 ERR("out of memory\n");
470 msg->RpcInterfaceInformation = NULL;
471 RPCRT4_release_server_interface(sif);
473 if (msg->Buffer == buf) buf = NULL;
474 TRACE("freeing Buffer=%p\n", buf);
480 static RPC_STATUS process_auth3_packet(RpcConnection *conn,
481 RpcPktCommonHdr *hdr,
483 unsigned char *auth_data,
488 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
489 !auth_length || msg->BufferLength != 0)
490 status = RPC_S_PROTOCOL_ERROR;
493 status = RPCRT4_ServerConnectionAuth(conn, FALSE,
494 (RpcAuthVerifier *)auth_data,
495 auth_length, NULL, NULL);
498 /* FIXME: client doesn't expect a response to this message so must store
499 * status in connection so that fault packet can be returned when next
500 * packet is received */
505 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr,
506 RPC_MESSAGE* msg, unsigned char *auth_data,
509 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
511 switch (hdr->common.ptype) {
513 TRACE("got bind packet\n");
514 process_bind_packet(conn, &hdr->bind, msg, auth_data, auth_length);
518 TRACE("got request packet\n");
519 process_request_packet(conn, &hdr->request, msg);
523 TRACE("got auth3 packet\n");
524 process_auth3_packet(conn, &hdr->common, msg, auth_data, auth_length);
527 FIXME("unhandled packet type %u\n", hdr->common.ptype);
532 I_RpcFree(msg->Buffer);
533 RPCRT4_FreeHeader(hdr);
534 HeapFree(GetProcessHeap(), 0, msg);
535 HeapFree(GetProcessHeap(), 0, auth_data);
538 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
540 RpcPacket *pkt = the_arg;
541 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg, pkt->auth_data,
543 RPCRT4_ReleaseConnection(pkt->conn);
544 HeapFree(GetProcessHeap(), 0, pkt);
548 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
550 RpcConnection* conn = the_arg;
555 unsigned char *auth_data;
558 TRACE("(%p)\n", conn);
561 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
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);
571 switch (hdr->common.ptype) {
573 TRACE("got bind packet\n");
575 status = process_bind_packet(conn, &hdr->bind, msg, auth_data,
580 TRACE("got request packet\n");
582 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
584 I_RpcFree(msg->Buffer);
585 RPCRT4_FreeHeader(hdr);
586 HeapFree(GetProcessHeap(), 0, msg);
587 HeapFree(GetProcessHeap(), 0, auth_data);
590 packet->conn = RPCRT4_GrabConnection( conn );
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;
605 TRACE("got auth3 packet\n");
607 status = process_auth3_packet(conn, &hdr->common, msg, auth_data,
611 FIXME("unhandled packet type %u\n", hdr->common.ptype);
615 I_RpcFree(msg->Buffer);
616 RPCRT4_FreeHeader(hdr);
617 HeapFree(GetProcessHeap(), 0, msg);
618 HeapFree(GetProcessHeap(), 0, auth_data);
620 if (status != RPC_S_OK) {
621 WARN("processing packet failed with error %u\n", status);
626 RPCRT4_ReleaseConnection(conn);
630 void RPCRT4_new_client(RpcConnection* conn)
632 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
634 DWORD err = GetLastError();
635 ERR("failed to create thread, error=%08x\n", err);
636 RPCRT4_ReleaseConnection(conn);
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 */
643 CloseHandle( thread );
646 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
651 RpcServerProtseq* cps = the_arg;
653 BOOL set_ready_event = FALSE;
655 TRACE("(the_arg == ^%p)\n", the_arg);
658 objs = cps->ops->get_wait_array(cps, objs, &count);
662 /* signal to function that changed state that we are now sync'ed */
663 SetEvent(cps->server_ready_event);
664 set_ready_event = FALSE;
668 res = cps->ops->wait_for_new_connection(cps, count, objs);
670 if (res == -1 || (res == 0 && !std_listen))
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);
679 if (res == 0 && !std_listen)
680 SetEvent(cps->server_ready_event);
684 set_ready_event = TRUE;
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)
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);
698 ps->ops->signal_state_changed(ps);
700 /* wait for server thread to make the requested changes before returning */
701 WaitForSingleObject(ps->server_ready_event, INFINITE);
703 ReleaseMutex(ps->mgr_mutex);
706 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
708 RPC_STATUS status = RPC_S_OK;
709 HANDLE server_thread;
711 EnterCriticalSection(&listen_cs);
712 if (ps->is_listening) goto done;
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);
719 status = RPC_S_OUT_OF_RESOURCES;
722 ps->is_listening = TRUE;
723 CloseHandle(server_thread);
726 LeaveCriticalSection(&listen_cs);
730 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
732 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
733 RpcServerProtseq *cps;
737 EnterCriticalSection(&listen_cs);
738 if (auto_listen || (manual_listen_count++ == 0))
741 if (++listen_count == 1)
744 LeaveCriticalSection(&listen_cs);
748 EnterCriticalSection(&server_cs);
749 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
751 status = RPCRT4_start_listen_protseq(cps, TRUE);
752 if (status != RPC_S_OK)
755 /* make sure server is actually listening on the interface before
757 RPCRT4_sync_with_server_thread(cps);
759 LeaveCriticalSection(&server_cs);
765 static void RPCRT4_stop_listen(BOOL auto_listen)
767 EnterCriticalSection(&listen_cs);
768 if (auto_listen || (--manual_listen_count == 0))
770 if (listen_count != 0 && --listen_count == 0) {
771 RpcServerProtseq *cps;
774 LeaveCriticalSection(&listen_cs);
776 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
777 RPCRT4_sync_with_server_thread(cps);
779 EnterCriticalSection(&listen_cs);
780 if (listen_done_event) SetEvent( listen_done_event );
781 listen_done_event = 0;
782 LeaveCriticalSection(&listen_cs);
785 assert(listen_count >= 0);
787 LeaveCriticalSection(&listen_cs);
790 static BOOL RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq *protseq, const char *endpoint)
793 EnterCriticalSection(&protseq->cs);
794 for (conn = protseq->conn; conn; conn = conn->Next)
796 if (!endpoint || !strcmp(endpoint, conn->Endpoint))
799 LeaveCriticalSection(&protseq->cs);
800 return (conn != NULL);
803 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, const char *endpoint)
807 EnterCriticalSection(&ps->cs);
809 if (RPCRT4_protseq_is_endpoint_registered(ps, endpoint))
812 status = ps->ops->open_endpoint(ps, endpoint);
814 LeaveCriticalSection(&ps->cs);
816 if (status != RPC_S_OK)
821 status = RPCRT4_start_listen_protseq(ps, FALSE);
822 if (status == RPC_S_OK)
823 RPCRT4_sync_with_server_thread(ps);
829 /***********************************************************************
830 * RpcServerInqBindings (RPCRT4.@)
832 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
836 RpcServerProtseq* ps;
840 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
842 ERR("(BindingVector == NULL!!?)\n");
844 EnterCriticalSection(&server_cs);
845 /* count connections */
847 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
848 EnterCriticalSection(&ps->cs);
849 for (conn = ps->conn; conn; conn = conn->Next)
851 LeaveCriticalSection(&ps->cs);
854 /* export bindings */
855 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
856 sizeof(RPC_BINDING_VECTOR) +
857 sizeof(RPC_BINDING_HANDLE)*(count-1));
858 (*BindingVector)->Count = count;
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],
867 LeaveCriticalSection(&ps->cs);
871 *BindingVector = NULL;
872 status = RPC_S_NO_BINDINGS;
874 LeaveCriticalSection(&server_cs);
878 /***********************************************************************
879 * RpcServerUseProtseqEpA (RPCRT4.@)
881 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
885 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
887 /* This should provide the default behaviour */
888 policy.Length = sizeof( policy );
889 policy.EndpointFlags = 0;
892 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
895 /***********************************************************************
896 * RpcServerUseProtseqEpW (RPCRT4.@)
898 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
902 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
904 /* This should provide the default behaviour */
905 policy.Length = sizeof( policy );
906 policy.EndpointFlags = 0;
909 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
912 /***********************************************************************
913 * alloc_serverprotoseq (internal)
915 * Must be called with server_cs held.
917 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
919 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
923 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
924 return RPC_S_PROTSEQ_NOT_SUPPORTED;
929 return RPC_S_OUT_OF_RESOURCES;
930 (*ps)->MaxCalls = MaxCalls;
931 (*ps)->Protseq = RPCRT4_strdupA(Protseq);
935 InitializeCriticalSection(&(*ps)->cs);
936 (*ps)->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": RpcServerProtseq.cs");
937 (*ps)->is_listening = FALSE;
938 (*ps)->mgr_mutex = NULL;
939 (*ps)->server_ready_event = NULL;
941 list_add_head(&protseqs, &(*ps)->entry);
943 TRACE("new protseq %p created for %s\n", *ps, Protseq);
948 /* must be called with server_cs held */
949 static void destroy_serverprotoseq(RpcServerProtseq *ps)
951 RPCRT4_strfree(ps->Protseq);
952 ps->cs.DebugInfo->Spare[0] = 0;
953 DeleteCriticalSection(&ps->cs);
954 CloseHandle(ps->mgr_mutex);
955 CloseHandle(ps->server_ready_event);
956 list_remove(&ps->entry);
957 HeapFree(GetProcessHeap(), 0, ps);
960 /* Finds a given protseq or creates a new one if one doesn't already exist */
961 static RPC_STATUS RPCRT4_get_or_create_serverprotseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
964 RpcServerProtseq *cps;
966 EnterCriticalSection(&server_cs);
968 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
969 if (!strcmp(cps->Protseq, Protseq))
971 TRACE("found existing protseq object for %s\n", Protseq);
973 LeaveCriticalSection(&server_cs);
977 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
979 LeaveCriticalSection(&server_cs);
984 /***********************************************************************
985 * RpcServerUseProtseqEpExA (RPCRT4.@)
987 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
988 PRPC_POLICY lpPolicy )
990 RpcServerProtseq* ps;
993 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq),
994 MaxCalls, debugstr_a((const char *)Endpoint), SecurityDescriptor,
995 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
997 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
998 if (status != RPC_S_OK)
1001 return RPCRT4_use_protseq(ps, (const char *)Endpoint);
1004 /***********************************************************************
1005 * RpcServerUseProtseqEpExW (RPCRT4.@)
1007 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
1008 PRPC_POLICY lpPolicy )
1010 RpcServerProtseq* ps;
1015 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq ), MaxCalls,
1016 debugstr_w( Endpoint ), SecurityDescriptor,
1017 lpPolicy->Length, lpPolicy->EndpointFlags, lpPolicy->NICFlags );
1019 ProtseqA = RPCRT4_strdupWtoA(Protseq);
1020 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
1021 RPCRT4_strfree(ProtseqA);
1022 if (status != RPC_S_OK)
1025 EndpointA = RPCRT4_strdupWtoA(Endpoint);
1026 status = RPCRT4_use_protseq(ps, EndpointA);
1027 RPCRT4_strfree(EndpointA);
1031 /***********************************************************************
1032 * RpcServerUseProtseqA (RPCRT4.@)
1034 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1037 RpcServerProtseq* ps;
1039 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
1041 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
1042 if (status != RPC_S_OK)
1045 return RPCRT4_use_protseq(ps, NULL);
1048 /***********************************************************************
1049 * RpcServerUseProtseqW (RPCRT4.@)
1051 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1054 RpcServerProtseq* ps;
1057 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
1059 ProtseqA = RPCRT4_strdupWtoA(Protseq);
1060 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, ProtseqA, &ps);
1061 RPCRT4_strfree(ProtseqA);
1062 if (status != RPC_S_OK)
1065 return RPCRT4_use_protseq(ps, NULL);
1068 void RPCRT4_destroy_all_protseqs(void)
1070 RpcServerProtseq *cps, *cursor2;
1072 if (listen_count != 0)
1075 EnterCriticalSection(&server_cs);
1076 LIST_FOR_EACH_ENTRY_SAFE(cps, cursor2, &protseqs, RpcServerProtseq, entry)
1078 if (listen_count != 0)
1079 RPCRT4_sync_with_server_thread(cps);
1080 destroy_serverprotoseq(cps);
1082 LeaveCriticalSection(&server_cs);
1083 DeleteCriticalSection(&server_cs);
1084 DeleteCriticalSection(&listen_cs);
1087 /***********************************************************************
1088 * RpcServerRegisterIf (RPCRT4.@)
1090 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
1092 TRACE("(%p,%s,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv);
1093 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, (UINT)-1, NULL );
1096 /***********************************************************************
1097 * RpcServerRegisterIfEx (RPCRT4.@)
1099 RPC_STATUS WINAPI RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1100 UINT Flags, UINT MaxCalls, RPC_IF_CALLBACK_FN* IfCallbackFn )
1102 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls, IfCallbackFn);
1103 return RpcServerRegisterIf2( IfSpec, MgrTypeUuid, MgrEpv, Flags, MaxCalls, (UINT)-1, IfCallbackFn );
1106 /***********************************************************************
1107 * RpcServerRegisterIf2 (RPCRT4.@)
1109 RPC_STATUS WINAPI RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv,
1110 UINT Flags, UINT MaxCalls, UINT MaxRpcSize, RPC_IF_CALLBACK_FN* IfCallbackFn )
1112 PRPC_SERVER_INTERFACE If = IfSpec;
1113 RpcServerInterface* sif;
1116 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec, debugstr_guid(MgrTypeUuid), MgrEpv, Flags, MaxCalls,
1117 MaxRpcSize, IfCallbackFn);
1118 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If->InterfaceId.SyntaxGUID),
1119 If->InterfaceId.SyntaxVersion.MajorVersion,
1120 If->InterfaceId.SyntaxVersion.MinorVersion);
1121 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID),
1122 If->TransferSyntax.SyntaxVersion.MajorVersion,
1123 If->TransferSyntax.SyntaxVersion.MinorVersion);
1124 TRACE(" dispatch table: %p\n", If->DispatchTable);
1125 if (If->DispatchTable) {
1126 TRACE(" dispatch table count: %d\n", If->DispatchTable->DispatchTableCount);
1127 for (i=0; i<If->DispatchTable->DispatchTableCount; i++) {
1128 TRACE(" entry %d: %p\n", i, If->DispatchTable->DispatchTable[i]);
1130 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
1132 TRACE(" protseq endpoint count: %d\n", If->RpcProtseqEndpointCount);
1133 TRACE(" default manager epv: %p\n", If->DefaultManagerEpv);
1134 TRACE(" interpreter info: %p\n", If->InterpreterInfo);
1136 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
1139 sif->MgrTypeUuid = *MgrTypeUuid;
1140 sif->MgrEpv = MgrEpv;
1142 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
1143 sif->MgrEpv = If->DefaultManagerEpv;
1146 sif->MaxCalls = MaxCalls;
1147 sif->MaxRpcSize = MaxRpcSize;
1148 sif->IfCallbackFn = IfCallbackFn;
1150 EnterCriticalSection(&server_cs);
1151 list_add_head(&server_interfaces, &sif->entry);
1152 LeaveCriticalSection(&server_cs);
1154 if (sif->Flags & RPC_IF_AUTOLISTEN)
1155 RPCRT4_start_listen(TRUE);
1160 /***********************************************************************
1161 * RpcServerUnregisterIf (RPCRT4.@)
1163 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
1165 PRPC_SERVER_INTERFACE If = IfSpec;
1166 HANDLE event = NULL;
1168 BOOL completed = TRUE;
1169 RpcServerInterface *cif;
1172 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1173 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
1175 EnterCriticalSection(&server_cs);
1176 LIST_FOR_EACH_ENTRY(cif, &server_interfaces, RpcServerInterface, entry) {
1177 if ((!IfSpec || !memcmp(&If->InterfaceId, &cif->If->InterfaceId, sizeof(RPC_SYNTAX_IDENTIFIER))) &&
1178 UuidEqual(MgrTypeUuid, &cif->MgrTypeUuid, &status)) {
1179 list_remove(&cif->entry);
1180 TRACE("unregistering cif %p\n", cif);
1181 if (cif->CurrentCalls) {
1184 if (WaitForCallsToComplete)
1185 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
1191 LeaveCriticalSection(&server_cs);
1194 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
1195 return RPC_S_UNKNOWN_IF;
1199 HeapFree(GetProcessHeap(), 0, cif);
1201 /* sif will be freed when the last call is completed, so be careful not to
1202 * touch that memory here as that could happen before we get here */
1203 WaitForSingleObject(event, INFINITE);
1210 /***********************************************************************
1211 * RpcServerUnregisterIfEx (RPCRT4.@)
1213 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
1215 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1216 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
1221 /***********************************************************************
1222 * RpcObjectSetType (RPCRT4.@)
1225 * ObjUuid [I] "Object" UUID
1226 * TypeUuid [I] "Type" UUID
1229 * RPC_S_OK The call succeeded
1230 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1231 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1233 * Maps "Object" UUIDs to "Type" UUIDs. Passing the nil UUID as the type
1234 * resets the mapping for the specified object UUID to nil (the default).
1235 * The nil object is always associated with the nil type and cannot be
1236 * reassigned. Servers can support multiple implementations on the same
1237 * interface by registering different end-point vectors for the different
1238 * types. There's no need to call this if a server only supports the nil
1239 * type, as is typical.
1241 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
1243 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
1246 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid), debugstr_guid(TypeUuid));
1247 if ((! ObjUuid) || UuidIsNil(ObjUuid, &dummy)) {
1248 /* nil uuid cannot be remapped */
1249 return RPC_S_INVALID_OBJECT;
1252 /* find the mapping for this object if there is one ... */
1254 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
1258 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
1259 /* ... and drop it from the list */
1262 prev->next = map->next;
1264 RpcObjTypeMaps = map->next;
1265 HeapFree(GetProcessHeap(), 0, map);
1268 /* ... , fail if we found it ... */
1270 return RPC_S_ALREADY_REGISTERED;
1271 /* ... otherwise create a new one and add it in. */
1272 map = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap));
1273 map->Object = *ObjUuid;
1274 map->Type = *TypeUuid;
1277 prev->next = map; /* prev is the last map in the linklist */
1279 RpcObjTypeMaps = map;
1285 struct rpc_server_registered_auth_info
1294 RPC_STATUS RPCRT4_ServerGetRegisteredAuthInfo(
1295 USHORT auth_type, CredHandle *cred, TimeStamp *exp, ULONG *max_token)
1297 RPC_STATUS status = RPC_S_UNKNOWN_AUTHN_SERVICE;
1298 struct rpc_server_registered_auth_info *auth_info;
1300 EnterCriticalSection(&server_auth_info_cs);
1301 LIST_FOR_EACH_ENTRY(auth_info, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1303 if (auth_info->auth_type == auth_type)
1305 *cred = auth_info->cred;
1306 *exp = auth_info->exp;
1307 *max_token = auth_info->max_token;
1312 LeaveCriticalSection(&server_auth_info_cs);
1317 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1319 struct rpc_server_registered_auth_info *auth_info, *cursor2;
1321 EnterCriticalSection(&server_auth_info_cs);
1322 LIST_FOR_EACH_ENTRY_SAFE(auth_info, cursor2, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1324 FreeCredentialsHandle(&auth_info->cred);
1325 HeapFree(GetProcessHeap(), 0, auth_info);
1327 LeaveCriticalSection(&server_auth_info_cs);
1328 DeleteCriticalSection(&server_auth_info_cs);
1331 /***********************************************************************
1332 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1334 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1337 SECURITY_STATUS sec_status;
1340 ULONG package_count;
1342 PSecPkgInfoA packages;
1344 struct rpc_server_registered_auth_info *auth_info;
1346 TRACE("(%s,%u,%p,%p)\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg);
1348 sec_status = EnumerateSecurityPackagesA(&package_count, &packages);
1349 if (sec_status != SEC_E_OK)
1351 ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n",
1353 return RPC_S_SEC_PKG_ERROR;
1356 for (i = 0; i < package_count; i++)
1357 if (packages[i].wRPCID == AuthnSvc)
1360 if (i == package_count)
1362 WARN("unsupported AuthnSvc %u\n", AuthnSvc);
1363 FreeContextBuffer(packages);
1364 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1366 TRACE("found package %s for service %u\n", packages[i].Name,
1368 sec_status = AcquireCredentialsHandleA((SEC_CHAR *)ServerPrincName,
1370 SECPKG_CRED_INBOUND, NULL, NULL,
1371 NULL, NULL, &cred, &exp);
1372 max_token = packages[i].cbMaxToken;
1373 FreeContextBuffer(packages);
1374 if (sec_status != SEC_E_OK)
1375 return RPC_S_SEC_PKG_ERROR;
1377 auth_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info));
1380 FreeCredentialsHandle(&cred);
1381 return RPC_S_OUT_OF_RESOURCES;
1384 auth_info->exp = exp;
1385 auth_info->cred = cred;
1386 auth_info->max_token = max_token;
1387 auth_info->auth_type = AuthnSvc;
1389 EnterCriticalSection(&server_auth_info_cs);
1390 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1391 LeaveCriticalSection(&server_auth_info_cs);
1396 /***********************************************************************
1397 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1399 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1402 SECURITY_STATUS sec_status;
1405 ULONG package_count;
1407 PSecPkgInfoW packages;
1409 struct rpc_server_registered_auth_info *auth_info;
1411 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName), AuthnSvc, GetKeyFn, Arg);
1413 sec_status = EnumerateSecurityPackagesW(&package_count, &packages);
1414 if (sec_status != SEC_E_OK)
1416 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n",
1418 return RPC_S_SEC_PKG_ERROR;
1421 for (i = 0; i < package_count; i++)
1422 if (packages[i].wRPCID == AuthnSvc)
1425 if (i == package_count)
1427 WARN("unsupported AuthnSvc %u\n", AuthnSvc);
1428 FreeContextBuffer(packages);
1429 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1431 TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name),
1433 sec_status = AcquireCredentialsHandleW((SEC_WCHAR *)ServerPrincName,
1435 SECPKG_CRED_INBOUND, NULL, NULL,
1436 NULL, NULL, &cred, &exp);
1437 max_token = packages[i].cbMaxToken;
1438 FreeContextBuffer(packages);
1439 if (sec_status != SEC_E_OK)
1440 return RPC_S_SEC_PKG_ERROR;
1442 auth_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info));
1445 FreeCredentialsHandle(&cred);
1446 return RPC_S_OUT_OF_RESOURCES;
1449 auth_info->exp = exp;
1450 auth_info->cred = cred;
1451 auth_info->max_token = max_token;
1452 auth_info->auth_type = AuthnSvc;
1454 EnterCriticalSection(&server_auth_info_cs);
1455 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1456 LeaveCriticalSection(&server_auth_info_cs);
1461 /******************************************************************************
1462 * RpcServerInqDefaultPrincNameA (rpcrt4.@)
1464 RPC_STATUS RPC_ENTRY RpcServerInqDefaultPrincNameA(ULONG AuthnSvc, RPC_CSTR *PrincName)
1467 RPC_WSTR principalW;
1469 TRACE("%u, %p\n", AuthnSvc, PrincName);
1471 if ((ret = RpcServerInqDefaultPrincNameW( AuthnSvc, &principalW )) == RPC_S_OK)
1473 if (!(*PrincName = (RPC_CSTR)RPCRT4_strdupWtoA( principalW ))) return RPC_S_OUT_OF_MEMORY;
1474 RpcStringFreeW( &principalW );
1479 /******************************************************************************
1480 * RpcServerInqDefaultPrincNameW (rpcrt4.@)
1482 RPC_STATUS RPC_ENTRY RpcServerInqDefaultPrincNameW(ULONG AuthnSvc, RPC_WSTR *PrincName)
1486 FIXME("%u, %p\n", AuthnSvc, PrincName);
1488 if (AuthnSvc != RPC_C_AUTHN_WINNT) return RPC_S_UNKNOWN_AUTHN_SERVICE;
1490 GetUserNameExW( NameSamCompatible, NULL, &len );
1491 if (GetLastError() != ERROR_MORE_DATA) return RPC_S_INTERNAL_ERROR;
1493 if (!(*PrincName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1494 return RPC_S_OUT_OF_MEMORY;
1496 GetUserNameExW( NameSamCompatible, *PrincName, &len );
1500 /***********************************************************************
1501 * RpcServerListen (RPCRT4.@)
1503 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1505 RPC_STATUS status = RPC_S_OK;
1507 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1509 if (list_empty(&protseqs))
1510 return RPC_S_NO_PROTSEQS_REGISTERED;
1512 status = RPCRT4_start_listen(FALSE);
1514 if (DontWait || (status != RPC_S_OK)) return status;
1516 return RpcMgmtWaitServerListen();
1519 /***********************************************************************
1520 * RpcMgmtServerWaitListen (RPCRT4.@)
1522 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1528 EnterCriticalSection(&listen_cs);
1531 LeaveCriticalSection(&listen_cs);
1532 return RPC_S_NOT_LISTENING;
1534 if (listen_done_event) {
1535 LeaveCriticalSection(&listen_cs);
1536 return RPC_S_ALREADY_LISTENING;
1538 event = CreateEventW( NULL, TRUE, FALSE, NULL );
1539 listen_done_event = event;
1541 LeaveCriticalSection(&listen_cs);
1543 TRACE( "waiting for server calls to finish\n" );
1544 WaitForSingleObject( event, INFINITE );
1545 TRACE( "done waiting\n" );
1547 CloseHandle( event );
1551 /***********************************************************************
1552 * RpcMgmtStopServerListening (RPCRT4.@)
1554 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1556 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1559 FIXME("client-side invocation not implemented.\n");
1560 return RPC_S_WRONG_KIND_OF_BINDING;
1563 RPCRT4_stop_listen(FALSE);
1568 /***********************************************************************
1569 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1571 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1573 FIXME("(): stub\n");
1577 /***********************************************************************
1578 * I_RpcServerStartListening (RPCRT4.@)
1580 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1582 FIXME( "(%p): stub\n", hWnd );
1587 /***********************************************************************
1588 * I_RpcServerStopListening (RPCRT4.@)
1590 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1592 FIXME( "(): stub\n" );
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 );
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))))
1628 stats->Stats[0] = 0;
1629 *Statistics = stats;
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);
1644 HeapFree(GetProcessHeap(), 0, *StatsVector);
1645 *StatsVector = NULL;
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 EnterCriticalSection(&listen_cs);
1671 if (manual_listen_count > 0) status = RPC_S_OK;
1672 LeaveCriticalSection(&listen_cs);
1676 /***********************************************************************
1677 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1679 RPC_STATUS WINAPI RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn)
1681 FIXME("(%p): stub\n", fn);
1685 /***********************************************************************
1686 * RpcMgmtSetServerStackSize (RPCRT4.@)
1688 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1690 FIXME("(0x%x): stub\n", ThreadStackSize);
1694 /***********************************************************************
1695 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1697 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1700 return RPCRT4_GetThreadCurrentCallHandle();