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
27 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
29 typedef struct _RpcPacket
31 struct _RpcConnection
* conn
;
34 unsigned char *auth_data
;
38 typedef struct _RpcObjTypeMap
40 /* FIXME: a hash table would be better. */
41 struct _RpcObjTypeMap
*next
;
46 static RpcObjTypeMap
*RpcObjTypeMaps
;
48 /* list of type RpcServerProtseq */
49 static struct list protseqs
= LIST_INIT(protseqs
);
50 static struct list server_interfaces
= LIST_INIT(server_interfaces
);
51 static struct list server_registered_auth_info
= LIST_INIT(server_registered_auth_info
);
53 static CRITICAL_SECTION server_cs
;
54 static CRITICAL_SECTION_DEBUG server_cs_debug
=
57 { &server_cs_debug
.ProcessLocksList
, &server_cs_debug
.ProcessLocksList
},
58 0, 0, { (DWORD_PTR
)(__FILE__
": server_cs") }
60 static CRITICAL_SECTION server_cs
= { &server_cs_debug
, -1, 0, 0, 0, 0 };
62 static CRITICAL_SECTION listen_cs
;
63 static CRITICAL_SECTION_DEBUG listen_cs_debug
=
66 { &listen_cs_debug
.ProcessLocksList
, &listen_cs_debug
.ProcessLocksList
},
67 0, 0, { (DWORD_PTR
)(__FILE__
": listen_cs") }
69 static CRITICAL_SECTION listen_cs
= { &listen_cs_debug
, -1, 0, 0, 0, 0 };
71 static CRITICAL_SECTION server_auth_info_cs
;
72 static CRITICAL_SECTION_DEBUG server_auth_info_cs_debug
=
74 0, 0, &server_auth_info_cs
,
75 { &server_auth_info_cs_debug
.ProcessLocksList
, &server_auth_info_cs_debug
.ProcessLocksList
},
76 0, 0, { (DWORD_PTR
)(__FILE__
": server_auth_info_cs") }
78 static CRITICAL_SECTION server_auth_info_cs
= { &server_auth_info_cs_debug
, -1, 0, 0, 0, 0 };
80 /* whether the server is currently listening */
81 static BOOL std_listen
;
82 /* number of manual listeners (calls to RpcServerListen) */
83 static LONG manual_listen_count
;
84 /* total listeners including auto listeners */
85 static LONG listen_count
;
86 /* event set once all listening is finished */
87 static HANDLE listen_done_event
;
91 static inline RpcObjTypeMap
*LookupObjTypeMap(UUID
*ObjUuid
)
93 RpcObjTypeMap
*rslt
= RpcObjTypeMaps
;
97 if (! UuidCompare(ObjUuid
, &rslt
->Object
, &dummy
)) break;
104 static inline UUID
*LookupObjType(UUID
*ObjUuid
)
106 RpcObjTypeMap
*map
= LookupObjTypeMap(ObjUuid
);
113 static RpcServerInterface
* RPCRT4_find_interface(UUID
* object
,
114 const RPC_SYNTAX_IDENTIFIER
*if_id
,
115 const RPC_SYNTAX_IDENTIFIER
*transfer_syntax
,
118 UUID
* MgrType
= NULL
;
119 RpcServerInterface
* cif
;
123 MgrType
= LookupObjType(object
);
124 EnterCriticalSection(&server_cs
);
125 LIST_FOR_EACH_ENTRY(cif
, &server_interfaces
, RpcServerInterface
, entry
) {
126 if (!memcmp(if_id
, &cif
->If
->InterfaceId
, sizeof(RPC_SYNTAX_IDENTIFIER
)) &&
127 (!transfer_syntax
|| !memcmp(transfer_syntax
, &cif
->If
->TransferSyntax
, sizeof(RPC_SYNTAX_IDENTIFIER
))) &&
128 (check_object
== FALSE
|| UuidEqual(MgrType
, &cif
->MgrTypeUuid
, &status
)) &&
130 InterlockedIncrement(&cif
->CurrentCalls
);
134 LeaveCriticalSection(&server_cs
);
135 if (&cif
->entry
== &server_interfaces
) cif
= NULL
;
136 TRACE("returning %p for object %s, if_id { %d.%d %s }\n", cif
,
137 debugstr_guid(object
), if_id
->SyntaxVersion
.MajorVersion
,
138 if_id
->SyntaxVersion
.MinorVersion
, debugstr_guid(&if_id
->SyntaxGUID
));
142 static void RPCRT4_release_server_interface(RpcServerInterface
*sif
)
144 if (!InterlockedDecrement(&sif
->CurrentCalls
) &&
146 /* sif must have been removed from server_interfaces before
147 * CallsCompletedEvent is set */
148 if (sif
->CallsCompletedEvent
)
149 SetEvent(sif
->CallsCompletedEvent
);
150 HeapFree(GetProcessHeap(), 0, sif
);
154 static RpcPktHdr
*handle_bind_error(RpcConnection
*conn
, RPC_STATUS error
)
156 unsigned int reject_reason
;
159 case RPC_S_SERVER_TOO_BUSY
:
160 reject_reason
= REJECT_TEMPORARY_CONGESTION
;
162 case ERROR_OUTOFMEMORY
:
163 case RPC_S_OUT_OF_RESOURCES
:
164 reject_reason
= REJECT_LOCAL_LIMIT_EXCEEDED
;
166 case RPC_S_PROTOCOL_ERROR
:
167 reject_reason
= REJECT_PROTOCOL_VERSION_NOT_SUPPORTED
;
169 case RPC_S_UNKNOWN_AUTHN_SERVICE
:
170 reject_reason
= REJECT_UNKNOWN_AUTHN_SERVICE
;
172 case ERROR_ACCESS_DENIED
:
173 reject_reason
= REJECT_INVALID_CHECKSUM
;
176 FIXME("unexpected status value %d\n", error
);
178 case RPC_S_INVALID_BOUND
:
179 reject_reason
= REJECT_REASON_NOT_SPECIFIED
;
182 return RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION
,
183 RPC_VER_MAJOR
, RPC_VER_MINOR
,
187 static RPC_STATUS
process_bind_packet_no_send(
188 RpcConnection
*conn
, RpcPktBindHdr
*hdr
, RPC_MESSAGE
*msg
,
189 unsigned char *auth_data
, ULONG auth_length
, RpcPktHdr
**ack_response
,
190 unsigned char **auth_data_out
, ULONG
*auth_length_out
)
193 RpcContextElement
*ctxt_elem
;
198 for (i
= 0, ctxt_elem
= msg
->Buffer
;
199 i
< hdr
->num_elements
;
200 i
++, ctxt_elem
= (RpcContextElement
*)&ctxt_elem
->transfer_syntaxes
[ctxt_elem
->num_syntaxes
])
202 if (((char *)ctxt_elem
- (char *)msg
->Buffer
) > msg
->BufferLength
||
203 ((char *)&ctxt_elem
->transfer_syntaxes
[ctxt_elem
->num_syntaxes
] - (char *)msg
->Buffer
) > msg
->BufferLength
)
205 ERR("inconsistent data in packet - packet length %d, num elements %d\n",
206 msg
->BufferLength
, hdr
->num_elements
);
207 return RPC_S_INVALID_BOUND
;
211 if (hdr
->max_tsize
< RPC_MIN_PACKET_SIZE
||
212 !UuidIsNil(&conn
->ActiveInterface
.SyntaxGUID
, &status
) ||
213 conn
->server_binding
)
215 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
217 return RPC_S_INVALID_BOUND
;
220 results
= HeapAlloc(GetProcessHeap(), 0,
221 hdr
->num_elements
* sizeof(*results
));
223 return RPC_S_OUT_OF_RESOURCES
;
225 for (i
= 0, ctxt_elem
= (RpcContextElement
*)msg
->Buffer
;
226 i
< hdr
->num_elements
;
227 i
++, ctxt_elem
= (RpcContextElement
*)&ctxt_elem
->transfer_syntaxes
[ctxt_elem
->num_syntaxes
])
229 RpcServerInterface
* sif
= NULL
;
232 for (j
= 0; !sif
&& j
< ctxt_elem
->num_syntaxes
; j
++)
234 sif
= RPCRT4_find_interface(NULL
, &ctxt_elem
->abstract_syntax
,
235 &ctxt_elem
->transfer_syntaxes
[j
], FALSE
);
241 RPCRT4_release_server_interface(sif
);
242 TRACE("accepting bind request on connection %p for %s\n", conn
,
243 debugstr_guid(&ctxt_elem
->abstract_syntax
.SyntaxGUID
));
244 results
[i
].result
= RESULT_ACCEPT
;
245 results
[i
].reason
= REASON_NONE
;
246 results
[i
].transfer_syntax
= ctxt_elem
->transfer_syntaxes
[j
];
248 /* save the interface for later use */
249 /* FIXME: save linked list */
250 conn
->ActiveInterface
= ctxt_elem
->abstract_syntax
;
252 else if ((sif
= RPCRT4_find_interface(NULL
, &ctxt_elem
->abstract_syntax
,
253 NULL
, FALSE
)) != NULL
)
255 RPCRT4_release_server_interface(sif
);
256 TRACE("not accepting bind request on connection %p for %s - no transfer syntaxes supported\n",
257 conn
, debugstr_guid(&ctxt_elem
->abstract_syntax
.SyntaxGUID
));
258 results
[i
].result
= RESULT_PROVIDER_REJECTION
;
259 results
[i
].reason
= REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED
;
260 memset(&results
[i
].transfer_syntax
, 0, sizeof(results
[i
].transfer_syntax
));
264 TRACE("not accepting bind request on connection %p for %s - abstract syntax not supported\n",
265 conn
, debugstr_guid(&ctxt_elem
->abstract_syntax
.SyntaxGUID
));
266 results
[i
].result
= RESULT_PROVIDER_REJECTION
;
267 results
[i
].reason
= REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED
;
268 memset(&results
[i
].transfer_syntax
, 0, sizeof(results
[i
].transfer_syntax
));
272 /* create temporary binding */
273 status
= RPCRT4_MakeBinding(&conn
->server_binding
, conn
);
274 if (status
!= RPC_S_OK
)
276 HeapFree(GetProcessHeap(), 0, results
);
280 status
= RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn
),
281 conn
->NetworkAddr
, conn
->Endpoint
,
282 conn
->NetworkOptions
,
284 &conn
->server_binding
->Assoc
);
285 if (status
!= RPC_S_OK
)
287 HeapFree(GetProcessHeap(), 0, results
);
293 status
= RPCRT4_ServerConnectionAuth(conn
, TRUE
,
294 (RpcAuthVerifier
*)auth_data
,
295 auth_length
, auth_data_out
,
297 if (status
!= RPC_S_OK
)
299 HeapFree(GetProcessHeap(), 0, results
);
304 *ack_response
= RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION
,
307 conn
->server_binding
->Assoc
->assoc_group_id
,
308 conn
->Endpoint
, hdr
->num_elements
,
310 HeapFree(GetProcessHeap(), 0, results
);
313 conn
->MaxTransmissionSize
= hdr
->max_tsize
;
315 status
= RPC_S_OUT_OF_RESOURCES
;
320 static RPC_STATUS
process_bind_packet(RpcConnection
*conn
, RpcPktBindHdr
*hdr
,
322 unsigned char *auth_data
,
326 RpcPktHdr
*response
= NULL
;
327 unsigned char *auth_data_out
= NULL
;
328 ULONG auth_length_out
= 0;
330 status
= process_bind_packet_no_send(conn
, hdr
, msg
, auth_data
, auth_length
,
331 &response
, &auth_data_out
,
333 if (status
!= RPC_S_OK
)
334 response
= handle_bind_error(conn
, status
);
336 status
= RPCRT4_SendWithAuth(conn
, response
, NULL
, 0, auth_data_out
, auth_length_out
);
338 status
= ERROR_OUTOFMEMORY
;
339 RPCRT4_FreeHeader(response
);
345 static RPC_STATUS
process_request_packet(RpcConnection
*conn
, RpcPktRequestHdr
*hdr
, RPC_MESSAGE
*msg
)
348 RpcPktHdr
*response
= NULL
;
349 RpcServerInterface
* sif
;
350 RPC_DISPATCH_FUNCTION func
;
353 NDR_SCONTEXT context_handle
;
354 void *buf
= msg
->Buffer
;
356 /* fail if the connection isn't bound with an interface */
357 if (UuidIsNil(&conn
->ActiveInterface
.SyntaxGUID
, &status
)) {
358 /* FIXME: should send BindNack instead */
359 response
= RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION
,
362 RPCRT4_Send(conn
, response
, NULL
, 0);
363 RPCRT4_FreeHeader(response
);
367 if (hdr
->common
.flags
& RPC_FLG_OBJECT_UUID
) {
368 object_uuid
= (UUID
*)(hdr
+ 1);
373 sif
= RPCRT4_find_interface(object_uuid
, &conn
->ActiveInterface
, NULL
, TRUE
);
375 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn
->ActiveInterface
.SyntaxGUID
));
376 response
= RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION
,
379 RPCRT4_Send(conn
, response
, NULL
, 0);
380 RPCRT4_FreeHeader(response
);
383 msg
->RpcInterfaceInformation
= sif
->If
;
384 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
385 msg
->ManagerEpv
= sif
->MgrEpv
;
386 if (object_uuid
!= NULL
) {
387 RPCRT4_SetBindingObject(msg
->Handle
, object_uuid
);
390 /* find dispatch function */
391 msg
->ProcNum
= hdr
->opnum
;
392 if (sif
->Flags
& RPC_IF_OLE
) {
393 /* native ole32 always gives us a dispatch table with a single entry
394 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
395 func
= *sif
->If
->DispatchTable
->DispatchTable
;
397 if (msg
->ProcNum
>= sif
->If
->DispatchTable
->DispatchTableCount
) {
398 WARN("invalid procnum (%d/%d)\n", msg
->ProcNum
, sif
->If
->DispatchTable
->DispatchTableCount
);
399 response
= RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION
,
402 RPCRT4_Send(conn
, response
, NULL
, 0);
403 RPCRT4_FreeHeader(response
);
405 func
= sif
->If
->DispatchTable
->DispatchTable
[msg
->ProcNum
];
408 /* put in the drep. FIXME: is this more universally applicable?
409 perhaps we should move this outward... */
410 msg
->DataRepresentation
=
411 MAKELONG( MAKEWORD(hdr
->common
.drep
[0], hdr
->common
.drep
[1]),
412 MAKEWORD(hdr
->common
.drep
[2], hdr
->common
.drep
[3]));
417 RPCRT4_SetThreadCurrentCallHandle(msg
->Handle
);
421 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
423 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION
)
424 status
= ERROR_NOACCESS
;
426 status
= GetExceptionCode();
427 response
= RPCRT4_BuildFaultHeader(msg
->DataRepresentation
,
428 RPC2NCA_STATUS(status
));
430 RPCRT4_SetThreadCurrentCallHandle(NULL
);
432 /* release any unmarshalled context handles */
433 while ((context_handle
= RPCRT4_PopThreadContextHandle()) != NULL
)
434 RpcServerAssoc_ReleaseContextHandle(conn
->server_binding
->Assoc
, context_handle
, TRUE
);
437 response
= RPCRT4_BuildResponseHeader(msg
->DataRepresentation
,
440 /* send response packet */
442 status
= RPCRT4_Send(conn
, response
, exception
? NULL
: msg
->Buffer
,
443 exception
? 0 : msg
->BufferLength
);
444 RPCRT4_FreeHeader(response
);
446 ERR("out of memory\n");
448 msg
->RpcInterfaceInformation
= NULL
;
449 RPCRT4_release_server_interface(sif
);
451 if (msg
->Buffer
== buf
) buf
= NULL
;
452 TRACE("freeing Buffer=%p\n", buf
);
458 static RPC_STATUS
process_auth3_packet(RpcConnection
*conn
,
459 RpcPktCommonHdr
*hdr
,
461 unsigned char *auth_data
,
466 if (UuidIsNil(&conn
->ActiveInterface
.SyntaxGUID
, &status
) ||
467 !auth_length
|| msg
->BufferLength
!= 0)
468 status
= RPC_S_PROTOCOL_ERROR
;
471 status
= RPCRT4_ServerConnectionAuth(conn
, FALSE
,
472 (RpcAuthVerifier
*)auth_data
,
473 auth_length
, NULL
, NULL
);
476 /* FIXME: client doesn't expect a response to this message so must store
477 * status in connection so that fault packet can be returned when next
478 * packet is received */
483 static void RPCRT4_process_packet(RpcConnection
* conn
, RpcPktHdr
* hdr
,
484 RPC_MESSAGE
* msg
, unsigned char *auth_data
,
487 msg
->Handle
= (RPC_BINDING_HANDLE
)conn
->server_binding
;
489 switch (hdr
->common
.ptype
) {
491 TRACE("got bind packet\n");
492 process_bind_packet(conn
, &hdr
->bind
, msg
, auth_data
, auth_length
);
496 TRACE("got request packet\n");
497 process_request_packet(conn
, &hdr
->request
, msg
);
501 TRACE("got auth3 packet\n");
502 process_auth3_packet(conn
, &hdr
->common
, msg
, auth_data
, auth_length
);
505 FIXME("unhandled packet type %u\n", hdr
->common
.ptype
);
510 I_RpcFree(msg
->Buffer
);
511 RPCRT4_FreeHeader(hdr
);
512 HeapFree(GetProcessHeap(), 0, msg
);
513 HeapFree(GetProcessHeap(), 0, auth_data
);
516 static DWORD CALLBACK
RPCRT4_worker_thread(LPVOID the_arg
)
518 RpcPacket
*pkt
= the_arg
;
519 RPCRT4_process_packet(pkt
->conn
, pkt
->hdr
, pkt
->msg
, pkt
->auth_data
,
521 RPCRT4_ReleaseConnection(pkt
->conn
);
522 HeapFree(GetProcessHeap(), 0, pkt
);
526 static DWORD CALLBACK
RPCRT4_io_thread(LPVOID the_arg
)
528 RpcConnection
* conn
= the_arg
;
533 unsigned char *auth_data
;
536 TRACE("(%p)\n", conn
);
539 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RPC_MESSAGE
));
542 status
= RPCRT4_ReceiveWithAuth(conn
, &hdr
, msg
, &auth_data
, &auth_length
);
543 if (status
!= RPC_S_OK
) {
544 WARN("receive failed with error %x\n", status
);
545 HeapFree(GetProcessHeap(), 0, msg
);
549 switch (hdr
->common
.ptype
) {
551 TRACE("got bind packet\n");
553 status
= process_bind_packet(conn
, &hdr
->bind
, msg
, auth_data
,
558 TRACE("got request packet\n");
560 packet
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket
));
562 I_RpcFree(msg
->Buffer
);
563 RPCRT4_FreeHeader(hdr
);
564 HeapFree(GetProcessHeap(), 0, msg
);
565 HeapFree(GetProcessHeap(), 0, auth_data
);
568 packet
->conn
= RPCRT4_GrabConnection( conn
);
571 packet
->auth_data
= auth_data
;
572 packet
->auth_length
= auth_length
;
573 if (!QueueUserWorkItem(RPCRT4_worker_thread
, packet
, WT_EXECUTELONGFUNCTION
)) {
574 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
575 HeapFree(GetProcessHeap(), 0, packet
);
576 status
= RPC_S_OUT_OF_RESOURCES
;
583 TRACE("got auth3 packet\n");
585 status
= process_auth3_packet(conn
, &hdr
->common
, msg
, auth_data
,
589 FIXME("unhandled packet type %u\n", hdr
->common
.ptype
);
593 I_RpcFree(msg
->Buffer
);
594 RPCRT4_FreeHeader(hdr
);
595 HeapFree(GetProcessHeap(), 0, msg
);
596 HeapFree(GetProcessHeap(), 0, auth_data
);
598 if (status
!= RPC_S_OK
) {
599 WARN("processing packet failed with error %u\n", status
);
604 RPCRT4_ReleaseConnection(conn
);
608 void RPCRT4_new_client(RpcConnection
* conn
)
610 HANDLE thread
= CreateThread(NULL
, 0, RPCRT4_io_thread
, conn
, 0, NULL
);
612 DWORD err
= GetLastError();
613 ERR("failed to create thread, error=%08x\n", err
);
614 RPCRT4_ReleaseConnection(conn
);
616 /* we could set conn->thread, but then we'd have to make the io_thread wait
617 * for that, otherwise the thread might finish, destroy the connection, and
618 * free the memory we'd write to before we did, causing crashes and stuff -
619 * so let's implement that later, when we really need conn->thread */
621 CloseHandle( thread
);
624 static DWORD CALLBACK
RPCRT4_server_thread(LPVOID the_arg
)
629 RpcServerProtseq
* cps
= the_arg
;
631 BOOL set_ready_event
= FALSE
;
633 TRACE("(the_arg == ^%p)\n", the_arg
);
636 objs
= cps
->ops
->get_wait_array(cps
, objs
, &count
);
640 /* signal to function that changed state that we are now sync'ed */
641 SetEvent(cps
->server_ready_event
);
642 set_ready_event
= FALSE
;
646 res
= cps
->ops
->wait_for_new_connection(cps
, count
, objs
);
648 if (res
== -1 || (res
== 0 && !std_listen
))
651 cps
->ops
->free_wait_array(cps
, objs
);
652 EnterCriticalSection(&cps
->cs
);
653 for (conn
= cps
->conn
; conn
; conn
= conn
->Next
)
654 RPCRT4_CloseConnection(conn
);
655 LeaveCriticalSection(&cps
->cs
);
657 if (res
== 0 && !std_listen
)
658 SetEvent(cps
->server_ready_event
);
662 set_ready_event
= TRUE
;
667 /* tells the server thread that the state has changed and waits for it to
668 * make the changes */
669 static void RPCRT4_sync_with_server_thread(RpcServerProtseq
*ps
)
671 /* make sure we are the only thread sync'ing the server state, otherwise
672 * there is a race with the server thread setting an older state and setting
673 * the server_ready_event when the new state hasn't yet been applied */
674 WaitForSingleObject(ps
->mgr_mutex
, INFINITE
);
676 ps
->ops
->signal_state_changed(ps
);
678 /* wait for server thread to make the requested changes before returning */
679 WaitForSingleObject(ps
->server_ready_event
, INFINITE
);
681 ReleaseMutex(ps
->mgr_mutex
);
684 static RPC_STATUS
RPCRT4_start_listen_protseq(RpcServerProtseq
*ps
, BOOL auto_listen
)
686 RPC_STATUS status
= RPC_S_OK
;
687 HANDLE server_thread
;
689 EnterCriticalSection(&listen_cs
);
690 if (ps
->is_listening
) goto done
;
692 if (!ps
->mgr_mutex
) ps
->mgr_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
693 if (!ps
->server_ready_event
) ps
->server_ready_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
694 server_thread
= CreateThread(NULL
, 0, RPCRT4_server_thread
, ps
, 0, NULL
);
697 status
= RPC_S_OUT_OF_RESOURCES
;
700 ps
->is_listening
= TRUE
;
701 CloseHandle(server_thread
);
704 LeaveCriticalSection(&listen_cs
);
708 static RPC_STATUS
RPCRT4_start_listen(BOOL auto_listen
)
710 RPC_STATUS status
= RPC_S_ALREADY_LISTENING
;
711 RpcServerProtseq
*cps
;
715 EnterCriticalSection(&listen_cs
);
716 if (auto_listen
|| (manual_listen_count
++ == 0))
719 if (++listen_count
== 1)
722 LeaveCriticalSection(&listen_cs
);
726 EnterCriticalSection(&server_cs
);
727 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
729 status
= RPCRT4_start_listen_protseq(cps
, TRUE
);
730 if (status
!= RPC_S_OK
)
733 /* make sure server is actually listening on the interface before
735 RPCRT4_sync_with_server_thread(cps
);
737 LeaveCriticalSection(&server_cs
);
743 static RPC_STATUS
RPCRT4_stop_listen(BOOL auto_listen
)
745 RPC_STATUS status
= RPC_S_OK
;
747 EnterCriticalSection(&listen_cs
);
751 status
= RPC_S_NOT_LISTENING
;
755 if (auto_listen
|| (--manual_listen_count
== 0))
757 if (listen_count
!= 0 && --listen_count
== 0) {
758 RpcServerProtseq
*cps
;
761 LeaveCriticalSection(&listen_cs
);
763 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
764 RPCRT4_sync_with_server_thread(cps
);
766 EnterCriticalSection(&listen_cs
);
767 if (listen_done_event
) SetEvent( listen_done_event
);
768 listen_done_event
= 0;
771 assert(listen_count
>= 0);
775 LeaveCriticalSection(&listen_cs
);
779 static BOOL
RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq
*protseq
, const char *endpoint
)
782 EnterCriticalSection(&protseq
->cs
);
783 for (conn
= protseq
->conn
; conn
; conn
= conn
->Next
)
785 if (!endpoint
|| !strcmp(endpoint
, conn
->Endpoint
))
788 LeaveCriticalSection(&protseq
->cs
);
789 return (conn
!= NULL
);
792 static RPC_STATUS
RPCRT4_use_protseq(RpcServerProtseq
* ps
, const char *endpoint
)
796 EnterCriticalSection(&ps
->cs
);
798 if (RPCRT4_protseq_is_endpoint_registered(ps
, endpoint
))
801 status
= ps
->ops
->open_endpoint(ps
, endpoint
);
803 LeaveCriticalSection(&ps
->cs
);
805 if (status
!= RPC_S_OK
)
810 status
= RPCRT4_start_listen_protseq(ps
, FALSE
);
811 if (status
== RPC_S_OK
)
812 RPCRT4_sync_with_server_thread(ps
);
818 /***********************************************************************
819 * RpcServerInqBindings (RPCRT4.@)
821 RPC_STATUS WINAPI
RpcServerInqBindings( RPC_BINDING_VECTOR
** BindingVector
)
825 RpcServerProtseq
* ps
;
829 TRACE("(*BindingVector == ^%p)\n", *BindingVector
);
831 ERR("(BindingVector == NULL!!?)\n");
833 EnterCriticalSection(&server_cs
);
834 /* count connections */
836 LIST_FOR_EACH_ENTRY(ps
, &protseqs
, RpcServerProtseq
, entry
) {
837 EnterCriticalSection(&ps
->cs
);
838 for (conn
= ps
->conn
; conn
; conn
= conn
->Next
)
840 LeaveCriticalSection(&ps
->cs
);
843 /* export bindings */
844 *BindingVector
= HeapAlloc(GetProcessHeap(), 0,
845 sizeof(RPC_BINDING_VECTOR
) +
846 sizeof(RPC_BINDING_HANDLE
)*(count
-1));
847 (*BindingVector
)->Count
= count
;
849 LIST_FOR_EACH_ENTRY(ps
, &protseqs
, RpcServerProtseq
, entry
) {
850 EnterCriticalSection(&ps
->cs
);
851 for (conn
= ps
->conn
; conn
; conn
= conn
->Next
) {
852 RPCRT4_MakeBinding((RpcBinding
**)&(*BindingVector
)->BindingH
[count
],
856 LeaveCriticalSection(&ps
->cs
);
860 *BindingVector
= NULL
;
861 status
= RPC_S_NO_BINDINGS
;
863 LeaveCriticalSection(&server_cs
);
867 /***********************************************************************
868 * RpcServerUseProtseqEpA (RPCRT4.@)
870 RPC_STATUS WINAPI
RpcServerUseProtseqEpA( RPC_CSTR Protseq
, UINT MaxCalls
, RPC_CSTR Endpoint
, LPVOID SecurityDescriptor
)
874 TRACE( "(%s,%u,%s,%p)\n", Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
);
876 /* This should provide the default behaviour */
877 policy
.Length
= sizeof( policy
);
878 policy
.EndpointFlags
= 0;
881 return RpcServerUseProtseqEpExA( Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
, &policy
);
884 /***********************************************************************
885 * RpcServerUseProtseqEpW (RPCRT4.@)
887 RPC_STATUS WINAPI
RpcServerUseProtseqEpW( RPC_WSTR Protseq
, UINT MaxCalls
, RPC_WSTR Endpoint
, LPVOID SecurityDescriptor
)
891 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq
), MaxCalls
, debugstr_w( Endpoint
), SecurityDescriptor
);
893 /* This should provide the default behaviour */
894 policy
.Length
= sizeof( policy
);
895 policy
.EndpointFlags
= 0;
898 return RpcServerUseProtseqEpExW( Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
, &policy
);
901 /***********************************************************************
902 * alloc_serverprotoseq (internal)
904 * Must be called with server_cs held.
906 static RPC_STATUS
alloc_serverprotoseq(UINT MaxCalls
, const char *Protseq
, RpcServerProtseq
**ps
)
908 const struct protseq_ops
*ops
= rpcrt4_get_protseq_ops(Protseq
);
912 FIXME("protseq %s not supported\n", debugstr_a(Protseq
));
913 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
918 return RPC_S_OUT_OF_RESOURCES
;
919 (*ps
)->MaxCalls
= MaxCalls
;
920 (*ps
)->Protseq
= RPCRT4_strdupA(Protseq
);
924 InitializeCriticalSection(&(*ps
)->cs
);
925 (*ps
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcServerProtseq.cs");
926 (*ps
)->is_listening
= FALSE
;
927 (*ps
)->mgr_mutex
= NULL
;
928 (*ps
)->server_ready_event
= NULL
;
930 list_add_head(&protseqs
, &(*ps
)->entry
);
932 TRACE("new protseq %p created for %s\n", *ps
, Protseq
);
937 /* must be called with server_cs held */
938 static void destroy_serverprotoseq(RpcServerProtseq
*ps
)
940 RPCRT4_strfree(ps
->Protseq
);
941 ps
->cs
.DebugInfo
->Spare
[0] = 0;
942 DeleteCriticalSection(&ps
->cs
);
943 CloseHandle(ps
->mgr_mutex
);
944 CloseHandle(ps
->server_ready_event
);
945 list_remove(&ps
->entry
);
946 HeapFree(GetProcessHeap(), 0, ps
);
949 /* Finds a given protseq or creates a new one if one doesn't already exist */
950 static RPC_STATUS
RPCRT4_get_or_create_serverprotseq(UINT MaxCalls
, const char *Protseq
, RpcServerProtseq
**ps
)
953 RpcServerProtseq
*cps
;
955 EnterCriticalSection(&server_cs
);
957 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
958 if (!strcmp(cps
->Protseq
, Protseq
))
960 TRACE("found existing protseq object for %s\n", Protseq
);
962 LeaveCriticalSection(&server_cs
);
966 status
= alloc_serverprotoseq(MaxCalls
, Protseq
, ps
);
968 LeaveCriticalSection(&server_cs
);
973 /***********************************************************************
974 * RpcServerUseProtseqEpExA (RPCRT4.@)
976 RPC_STATUS WINAPI
RpcServerUseProtseqEpExA( RPC_CSTR Protseq
, UINT MaxCalls
, RPC_CSTR Endpoint
, LPVOID SecurityDescriptor
,
977 PRPC_POLICY lpPolicy
)
979 RpcServerProtseq
* ps
;
982 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq
),
983 MaxCalls
, debugstr_a((const char *)Endpoint
), SecurityDescriptor
,
984 lpPolicy
->Length
, lpPolicy
->EndpointFlags
, lpPolicy
->NICFlags
);
986 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, (const char *)Protseq
, &ps
);
987 if (status
!= RPC_S_OK
)
990 return RPCRT4_use_protseq(ps
, (const char *)Endpoint
);
993 /***********************************************************************
994 * RpcServerUseProtseqEpExW (RPCRT4.@)
996 RPC_STATUS WINAPI
RpcServerUseProtseqEpExW( RPC_WSTR Protseq
, UINT MaxCalls
, RPC_WSTR Endpoint
, LPVOID SecurityDescriptor
,
997 PRPC_POLICY lpPolicy
)
999 RpcServerProtseq
* ps
;
1004 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq
), MaxCalls
,
1005 debugstr_w( Endpoint
), SecurityDescriptor
,
1006 lpPolicy
->Length
, lpPolicy
->EndpointFlags
, lpPolicy
->NICFlags
);
1008 ProtseqA
= RPCRT4_strdupWtoA(Protseq
);
1009 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, ProtseqA
, &ps
);
1010 RPCRT4_strfree(ProtseqA
);
1011 if (status
!= RPC_S_OK
)
1014 EndpointA
= RPCRT4_strdupWtoA(Endpoint
);
1015 status
= RPCRT4_use_protseq(ps
, EndpointA
);
1016 RPCRT4_strfree(EndpointA
);
1020 /***********************************************************************
1021 * RpcServerUseProtseqA (RPCRT4.@)
1023 RPC_STATUS WINAPI
RpcServerUseProtseqA(RPC_CSTR Protseq
, unsigned int MaxCalls
, void *SecurityDescriptor
)
1026 RpcServerProtseq
* ps
;
1028 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq
), MaxCalls
, SecurityDescriptor
);
1030 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, (const char *)Protseq
, &ps
);
1031 if (status
!= RPC_S_OK
)
1034 return RPCRT4_use_protseq(ps
, NULL
);
1037 /***********************************************************************
1038 * RpcServerUseProtseqW (RPCRT4.@)
1040 RPC_STATUS WINAPI
RpcServerUseProtseqW(RPC_WSTR Protseq
, unsigned int MaxCalls
, void *SecurityDescriptor
)
1043 RpcServerProtseq
* ps
;
1046 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq
), MaxCalls
, SecurityDescriptor
);
1048 ProtseqA
= RPCRT4_strdupWtoA(Protseq
);
1049 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, ProtseqA
, &ps
);
1050 RPCRT4_strfree(ProtseqA
);
1051 if (status
!= RPC_S_OK
)
1054 return RPCRT4_use_protseq(ps
, NULL
);
1057 void RPCRT4_destroy_all_protseqs(void)
1059 RpcServerProtseq
*cps
, *cursor2
;
1061 if (listen_count
!= 0)
1064 EnterCriticalSection(&server_cs
);
1065 LIST_FOR_EACH_ENTRY_SAFE(cps
, cursor2
, &protseqs
, RpcServerProtseq
, entry
)
1067 if (listen_count
!= 0)
1068 RPCRT4_sync_with_server_thread(cps
);
1069 destroy_serverprotoseq(cps
);
1071 LeaveCriticalSection(&server_cs
);
1072 DeleteCriticalSection(&server_cs
);
1073 DeleteCriticalSection(&listen_cs
);
1076 /***********************************************************************
1077 * RpcServerRegisterIf (RPCRT4.@)
1079 RPC_STATUS WINAPI
RpcServerRegisterIf( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
)
1081 TRACE("(%p,%s,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
);
1082 return RpcServerRegisterIf3( IfSpec
, MgrTypeUuid
, MgrEpv
, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, (UINT
)-1, NULL
, NULL
);
1085 /***********************************************************************
1086 * RpcServerRegisterIfEx (RPCRT4.@)
1088 RPC_STATUS WINAPI
RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1089 UINT Flags
, UINT MaxCalls
, RPC_IF_CALLBACK_FN
* IfCallbackFn
)
1091 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
, Flags
, MaxCalls
, IfCallbackFn
);
1092 return RpcServerRegisterIf3( IfSpec
, MgrTypeUuid
, MgrEpv
, Flags
, MaxCalls
, (UINT
)-1, IfCallbackFn
, NULL
);
1095 /***********************************************************************
1096 * RpcServerRegisterIf2 (RPCRT4.@)
1098 RPC_STATUS WINAPI
RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1099 UINT Flags
, UINT MaxCalls
, UINT MaxRpcSize
, RPC_IF_CALLBACK_FN
* IfCallbackFn
)
1101 return RpcServerRegisterIf3( IfSpec
, MgrTypeUuid
, MgrEpv
, Flags
, MaxCalls
, MaxRpcSize
, IfCallbackFn
, NULL
);
1104 /***********************************************************************
1105 * RpcServerRegisterIf3 (RPCRT4.@)
1107 RPC_STATUS WINAPI
RpcServerRegisterIf3( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1108 UINT Flags
, UINT MaxCalls
, UINT MaxRpcSize
, RPC_IF_CALLBACK_FN
* IfCallbackFn
, void* SecurityDescriptor
)
1110 PRPC_SERVER_INTERFACE If
= IfSpec
;
1111 RpcServerInterface
* sif
;
1114 TRACE("(%p,%s,%p,%u,%u,%u,%p,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
, Flags
, MaxCalls
,
1115 MaxRpcSize
, IfCallbackFn
, SecurityDescriptor
);
1117 if (SecurityDescriptor
)
1118 FIXME("Unsupported SecurityDescriptor argument.\n");
1120 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
),
1121 If
->InterfaceId
.SyntaxVersion
.MajorVersion
,
1122 If
->InterfaceId
.SyntaxVersion
.MinorVersion
);
1123 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
),
1124 If
->TransferSyntax
.SyntaxVersion
.MajorVersion
,
1125 If
->TransferSyntax
.SyntaxVersion
.MinorVersion
);
1126 TRACE(" dispatch table: %p\n", If
->DispatchTable
);
1127 if (If
->DispatchTable
) {
1128 TRACE(" dispatch table count: %d\n", If
->DispatchTable
->DispatchTableCount
);
1129 for (i
=0; i
<If
->DispatchTable
->DispatchTableCount
; i
++) {
1130 TRACE(" entry %d: %p\n", i
, If
->DispatchTable
->DispatchTable
[i
]);
1132 TRACE(" reserved: %ld\n", If
->DispatchTable
->Reserved
);
1134 TRACE(" protseq endpoint count: %d\n", If
->RpcProtseqEndpointCount
);
1135 TRACE(" default manager epv: %p\n", If
->DefaultManagerEpv
);
1136 TRACE(" interpreter info: %p\n", If
->InterpreterInfo
);
1138 sif
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcServerInterface
));
1141 sif
->MgrTypeUuid
= *MgrTypeUuid
;
1142 sif
->MgrEpv
= MgrEpv
;
1144 memset(&sif
->MgrTypeUuid
, 0, sizeof(UUID
));
1145 sif
->MgrEpv
= If
->DefaultManagerEpv
;
1148 sif
->MaxCalls
= MaxCalls
;
1149 sif
->MaxRpcSize
= MaxRpcSize
;
1150 sif
->IfCallbackFn
= IfCallbackFn
;
1152 EnterCriticalSection(&server_cs
);
1153 list_add_head(&server_interfaces
, &sif
->entry
);
1154 LeaveCriticalSection(&server_cs
);
1156 if (sif
->Flags
& RPC_IF_AUTOLISTEN
)
1157 RPCRT4_start_listen(TRUE
);
1162 /***********************************************************************
1163 * RpcServerUnregisterIf (RPCRT4.@)
1165 RPC_STATUS WINAPI
RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, UINT WaitForCallsToComplete
)
1167 PRPC_SERVER_INTERFACE If
= IfSpec
;
1168 HANDLE event
= NULL
;
1170 BOOL completed
= TRUE
;
1171 RpcServerInterface
*cif
;
1174 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1175 IfSpec
, debugstr_guid(&If
->InterfaceId
.SyntaxGUID
), debugstr_guid(MgrTypeUuid
), WaitForCallsToComplete
);
1177 EnterCriticalSection(&server_cs
);
1178 LIST_FOR_EACH_ENTRY(cif
, &server_interfaces
, RpcServerInterface
, entry
) {
1179 if ((!IfSpec
|| !memcmp(&If
->InterfaceId
, &cif
->If
->InterfaceId
, sizeof(RPC_SYNTAX_IDENTIFIER
))) &&
1180 UuidEqual(MgrTypeUuid
, &cif
->MgrTypeUuid
, &status
)) {
1181 list_remove(&cif
->entry
);
1182 TRACE("unregistering cif %p\n", cif
);
1183 if (cif
->CurrentCalls
) {
1186 if (WaitForCallsToComplete
)
1187 cif
->CallsCompletedEvent
= event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1193 LeaveCriticalSection(&server_cs
);
1196 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid
));
1197 return RPC_S_UNKNOWN_IF
;
1201 HeapFree(GetProcessHeap(), 0, cif
);
1203 /* sif will be freed when the last call is completed, so be careful not to
1204 * touch that memory here as that could happen before we get here */
1205 WaitForSingleObject(event
, INFINITE
);
1212 /***********************************************************************
1213 * RpcServerUnregisterIfEx (RPCRT4.@)
1215 RPC_STATUS WINAPI
RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, int RundownContextHandles
)
1217 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1218 IfSpec
, debugstr_guid(MgrTypeUuid
), RundownContextHandles
);
1223 /***********************************************************************
1224 * RpcObjectSetType (RPCRT4.@)
1227 * ObjUuid [I] "Object" UUID
1228 * TypeUuid [I] "Type" UUID
1231 * RPC_S_OK The call succeeded
1232 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1233 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1235 * Maps "Object" UUIDs to "Type" UUIDs. Passing the nil UUID as the type
1236 * resets the mapping for the specified object UUID to nil (the default).
1237 * The nil object is always associated with the nil type and cannot be
1238 * reassigned. Servers can support multiple implementations on the same
1239 * interface by registering different end-point vectors for the different
1240 * types. There's no need to call this if a server only supports the nil
1241 * type, as is typical.
1243 RPC_STATUS WINAPI
RpcObjectSetType( UUID
* ObjUuid
, UUID
* TypeUuid
)
1245 RpcObjTypeMap
*map
= RpcObjTypeMaps
, *prev
= NULL
;
1248 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid
), debugstr_guid(TypeUuid
));
1249 if ((! ObjUuid
) || UuidIsNil(ObjUuid
, &dummy
)) {
1250 /* nil uuid cannot be remapped */
1251 return RPC_S_INVALID_OBJECT
;
1254 /* find the mapping for this object if there is one ... */
1256 if (! UuidCompare(ObjUuid
, &map
->Object
, &dummy
)) break;
1260 if ((! TypeUuid
) || UuidIsNil(TypeUuid
, &dummy
)) {
1261 /* ... and drop it from the list */
1264 prev
->next
= map
->next
;
1266 RpcObjTypeMaps
= map
->next
;
1267 HeapFree(GetProcessHeap(), 0, map
);
1270 /* ... , fail if we found it ... */
1272 return RPC_S_ALREADY_REGISTERED
;
1273 /* ... otherwise create a new one and add it in. */
1274 map
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap
));
1275 map
->Object
= *ObjUuid
;
1276 map
->Type
= *TypeUuid
;
1279 prev
->next
= map
; /* prev is the last map in the linklist */
1281 RpcObjTypeMaps
= map
;
1287 struct rpc_server_registered_auth_info
1298 static RPC_STATUS
find_security_package(ULONG auth_type
, SecPkgInfoW
**packages_buf
, SecPkgInfoW
**ret
)
1300 SECURITY_STATUS sec_status
;
1301 SecPkgInfoW
*packages
;
1302 ULONG package_count
;
1305 sec_status
= EnumerateSecurityPackagesW(&package_count
, &packages
);
1306 if (sec_status
!= SEC_E_OK
)
1308 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", sec_status
);
1309 return RPC_S_SEC_PKG_ERROR
;
1312 for (i
= 0; i
< package_count
; i
++)
1313 if (packages
[i
].wRPCID
== auth_type
)
1316 if (i
== package_count
)
1318 WARN("unsupported AuthnSvc %u\n", auth_type
);
1319 FreeContextBuffer(packages
);
1320 return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1323 TRACE("found package %s for service %u\n", debugstr_w(packages
[i
].Name
), auth_type
);
1324 *packages_buf
= packages
;
1325 *ret
= packages
+ i
;
1329 RPC_STATUS
RPCRT4_ServerGetRegisteredAuthInfo(
1330 USHORT auth_type
, CredHandle
*cred
, TimeStamp
*exp
, ULONG
*max_token
)
1332 RPC_STATUS status
= RPC_S_UNKNOWN_AUTHN_SERVICE
;
1333 struct rpc_server_registered_auth_info
*auth_info
;
1335 EnterCriticalSection(&server_auth_info_cs
);
1336 LIST_FOR_EACH_ENTRY(auth_info
, &server_registered_auth_info
, struct rpc_server_registered_auth_info
, entry
)
1338 if (auth_info
->auth_type
== auth_type
)
1340 if (!auth_info
->cred_acquired
)
1342 SecPkgInfoW
*packages
, *package
;
1343 SECURITY_STATUS sec_status
;
1345 status
= find_security_package(auth_info
->auth_type
, &packages
, &package
);
1346 if (status
!= RPC_S_OK
)
1349 sec_status
= AcquireCredentialsHandleW((SEC_WCHAR
*)auth_info
->principal
, package
->Name
,
1350 SECPKG_CRED_INBOUND
, NULL
, NULL
, NULL
, NULL
,
1351 &auth_info
->cred
, &auth_info
->exp
);
1352 FreeContextBuffer(packages
);
1353 if (sec_status
!= SEC_E_OK
)
1355 status
= RPC_S_SEC_PKG_ERROR
;
1359 auth_info
->cred_acquired
= TRUE
;
1362 *cred
= auth_info
->cred
;
1363 *exp
= auth_info
->exp
;
1364 *max_token
= auth_info
->max_token
;
1369 LeaveCriticalSection(&server_auth_info_cs
);
1374 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1376 struct rpc_server_registered_auth_info
*auth_info
, *cursor2
;
1378 EnterCriticalSection(&server_auth_info_cs
);
1379 LIST_FOR_EACH_ENTRY_SAFE(auth_info
, cursor2
, &server_registered_auth_info
, struct rpc_server_registered_auth_info
, entry
)
1381 if (auth_info
->cred_acquired
)
1382 FreeCredentialsHandle(&auth_info
->cred
);
1383 HeapFree(GetProcessHeap(), 0, auth_info
->principal
);
1384 HeapFree(GetProcessHeap(), 0, auth_info
);
1386 LeaveCriticalSection(&server_auth_info_cs
);
1387 DeleteCriticalSection(&server_auth_info_cs
);
1390 /***********************************************************************
1391 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1393 RPC_STATUS WINAPI
RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName
, ULONG AuthnSvc
, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn
,
1396 WCHAR
*principal_name
= NULL
;
1399 TRACE("(%s,%u,%p,%p)\n", ServerPrincName
, AuthnSvc
, GetKeyFn
, Arg
);
1401 if(ServerPrincName
&& !(principal_name
= RPCRT4_strdupAtoW((const char*)ServerPrincName
)))
1402 return RPC_S_OUT_OF_RESOURCES
;
1404 status
= RpcServerRegisterAuthInfoW(principal_name
, AuthnSvc
, GetKeyFn
, Arg
);
1406 HeapFree(GetProcessHeap(), 0, principal_name
);
1410 /***********************************************************************
1411 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1413 RPC_STATUS WINAPI
RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName
, ULONG AuthnSvc
, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn
,
1416 struct rpc_server_registered_auth_info
*auth_info
;
1417 SecPkgInfoW
*packages
, *package
;
1421 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName
), AuthnSvc
, GetKeyFn
, Arg
);
1423 status
= find_security_package(AuthnSvc
, &packages
, &package
);
1424 if (status
!= RPC_S_OK
)
1427 max_token
= package
->cbMaxToken
;
1428 FreeContextBuffer(packages
);
1430 auth_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info
));
1432 return RPC_S_OUT_OF_RESOURCES
;
1434 if (!ServerPrincName
) {
1435 auth_info
->principal
= NULL
;
1436 }else if (!(auth_info
->principal
= RPCRT4_strdupW(ServerPrincName
))) {
1437 HeapFree(GetProcessHeap(), 0, auth_info
);
1438 return RPC_S_OUT_OF_RESOURCES
;
1441 auth_info
->max_token
= max_token
;
1442 auth_info
->auth_type
= AuthnSvc
;
1444 EnterCriticalSection(&server_auth_info_cs
);
1445 list_add_tail(&server_registered_auth_info
, &auth_info
->entry
);
1446 LeaveCriticalSection(&server_auth_info_cs
);
1451 /******************************************************************************
1452 * RpcServerInqDefaultPrincNameA (rpcrt4.@)
1454 RPC_STATUS RPC_ENTRY
RpcServerInqDefaultPrincNameA(ULONG AuthnSvc
, RPC_CSTR
*PrincName
)
1457 RPC_WSTR principalW
;
1459 TRACE("%u, %p\n", AuthnSvc
, PrincName
);
1461 if ((ret
= RpcServerInqDefaultPrincNameW( AuthnSvc
, &principalW
)) == RPC_S_OK
)
1463 if (!(*PrincName
= (RPC_CSTR
)RPCRT4_strdupWtoA( principalW
))) return RPC_S_OUT_OF_MEMORY
;
1464 RpcStringFreeW( &principalW
);
1469 /******************************************************************************
1470 * RpcServerInqDefaultPrincNameW (rpcrt4.@)
1472 RPC_STATUS RPC_ENTRY
RpcServerInqDefaultPrincNameW(ULONG AuthnSvc
, RPC_WSTR
*PrincName
)
1476 FIXME("%u, %p\n", AuthnSvc
, PrincName
);
1478 if (AuthnSvc
!= RPC_C_AUTHN_WINNT
) return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1480 GetUserNameExW( NameSamCompatible
, NULL
, &len
);
1481 if (GetLastError() != ERROR_MORE_DATA
) return RPC_S_INTERNAL_ERROR
;
1483 if (!(*PrincName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1484 return RPC_S_OUT_OF_MEMORY
;
1486 GetUserNameExW( NameSamCompatible
, *PrincName
, &len
);
1490 /***********************************************************************
1491 * RpcServerListen (RPCRT4.@)
1493 RPC_STATUS WINAPI
RpcServerListen( UINT MinimumCallThreads
, UINT MaxCalls
, UINT DontWait
)
1495 RPC_STATUS status
= RPC_S_OK
;
1497 TRACE("(%u,%u,%u)\n", MinimumCallThreads
, MaxCalls
, DontWait
);
1499 if (list_empty(&protseqs
))
1500 return RPC_S_NO_PROTSEQS_REGISTERED
;
1502 status
= RPCRT4_start_listen(FALSE
);
1504 if (DontWait
|| (status
!= RPC_S_OK
)) return status
;
1506 return RpcMgmtWaitServerListen();
1509 /***********************************************************************
1510 * RpcMgmtServerWaitListen (RPCRT4.@)
1512 RPC_STATUS WINAPI
RpcMgmtWaitServerListen( void )
1518 EnterCriticalSection(&listen_cs
);
1521 LeaveCriticalSection(&listen_cs
);
1522 return RPC_S_NOT_LISTENING
;
1524 if (listen_done_event
) {
1525 LeaveCriticalSection(&listen_cs
);
1526 return RPC_S_ALREADY_LISTENING
;
1528 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1529 listen_done_event
= event
;
1531 LeaveCriticalSection(&listen_cs
);
1533 TRACE( "waiting for server calls to finish\n" );
1534 WaitForSingleObject( event
, INFINITE
);
1535 TRACE( "done waiting\n" );
1537 CloseHandle( event
);
1541 /***********************************************************************
1542 * RpcMgmtStopServerListening (RPCRT4.@)
1544 RPC_STATUS WINAPI
RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding
)
1546 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding
);
1549 FIXME("client-side invocation not implemented.\n");
1550 return RPC_S_WRONG_KIND_OF_BINDING
;
1553 return RPCRT4_stop_listen(FALSE
);
1556 /***********************************************************************
1557 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1559 RPC_STATUS WINAPI
RpcMgmtEnableIdleCleanup(void)
1561 FIXME("(): stub\n");
1565 /***********************************************************************
1566 * I_RpcServerStartListening (RPCRT4.@)
1568 RPC_STATUS WINAPI
I_RpcServerStartListening( HWND hWnd
)
1570 FIXME( "(%p): stub\n", hWnd
);
1575 /***********************************************************************
1576 * I_RpcServerStopListening (RPCRT4.@)
1578 RPC_STATUS WINAPI
I_RpcServerStopListening( void )
1580 FIXME( "(): stub\n" );
1585 /***********************************************************************
1586 * I_RpcWindowProc (RPCRT4.@)
1588 UINT WINAPI
I_RpcWindowProc( void *hWnd
, UINT Message
, UINT wParam
, ULONG lParam
)
1590 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd
, Message
, wParam
, lParam
);
1595 /***********************************************************************
1596 * RpcMgmtInqIfIds (RPCRT4.@)
1598 RPC_STATUS WINAPI
RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding
, RPC_IF_ID_VECTOR
**IfIdVector
)
1600 FIXME("(%p,%p): stub\n", Binding
, IfIdVector
);
1601 return RPC_S_INVALID_BINDING
;
1604 /***********************************************************************
1605 * RpcMgmtInqStats (RPCRT4.@)
1607 RPC_STATUS WINAPI
RpcMgmtInqStats(RPC_BINDING_HANDLE Binding
, RPC_STATS_VECTOR
**Statistics
)
1609 RPC_STATS_VECTOR
*stats
;
1611 FIXME("(%p,%p)\n", Binding
, Statistics
);
1613 if ((stats
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR
))))
1616 stats
->Stats
[0] = 0;
1617 *Statistics
= stats
;
1620 return RPC_S_OUT_OF_RESOURCES
;
1623 /***********************************************************************
1624 * RpcMgmtStatsVectorFree (RPCRT4.@)
1626 RPC_STATUS WINAPI
RpcMgmtStatsVectorFree(RPC_STATS_VECTOR
**StatsVector
)
1628 FIXME("(%p)\n", StatsVector
);
1632 HeapFree(GetProcessHeap(), 0, *StatsVector
);
1633 *StatsVector
= NULL
;
1638 /***********************************************************************
1639 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1641 RPC_STATUS WINAPI
RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding
, ULONG InquiryType
,
1642 RPC_IF_ID
*IfId
, ULONG VersOption
, UUID
*ObjectUuid
, RPC_EP_INQ_HANDLE
* InquiryContext
)
1644 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1645 Binding
, InquiryType
, IfId
, VersOption
, ObjectUuid
, InquiryContext
);
1646 return RPC_S_INVALID_BINDING
;
1649 /***********************************************************************
1650 * RpcMgmtIsServerListening (RPCRT4.@)
1652 RPC_STATUS WINAPI
RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding
)
1654 RPC_STATUS status
= RPC_S_NOT_LISTENING
;
1656 TRACE("(%p)\n", Binding
);
1659 RpcBinding
*rpc_binding
= (RpcBinding
*)Binding
;
1660 status
= RPCRT4_IsServerListening(rpc_binding
->Protseq
, rpc_binding
->Endpoint
);
1662 EnterCriticalSection(&listen_cs
);
1663 if (manual_listen_count
> 0) status
= RPC_S_OK
;
1664 LeaveCriticalSection(&listen_cs
);
1670 /***********************************************************************
1671 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1673 RPC_STATUS WINAPI
RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn
)
1675 FIXME("(%p): stub\n", fn
);
1679 /***********************************************************************
1680 * RpcMgmtSetServerStackSize (RPCRT4.@)
1682 RPC_STATUS WINAPI
RpcMgmtSetServerStackSize(ULONG ThreadStackSize
)
1684 FIXME("(0x%x): stub\n", ThreadStackSize
);
1688 /***********************************************************************
1689 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1691 RPC_BINDING_HANDLE WINAPI
I_RpcGetCurrentCallHandle(void)
1694 return RPCRT4_GetThreadCurrentCallHandle();