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 void RPCRT4_stop_listen(BOOL auto_listen
)
745 EnterCriticalSection(&listen_cs
);
746 if (auto_listen
|| (--manual_listen_count
== 0))
748 if (listen_count
!= 0 && --listen_count
== 0) {
749 RpcServerProtseq
*cps
;
752 LeaveCriticalSection(&listen_cs
);
754 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
755 RPCRT4_sync_with_server_thread(cps
);
757 EnterCriticalSection(&listen_cs
);
758 if (listen_done_event
) SetEvent( listen_done_event
);
759 listen_done_event
= 0;
760 LeaveCriticalSection(&listen_cs
);
763 assert(listen_count
>= 0);
765 LeaveCriticalSection(&listen_cs
);
768 static BOOL
RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq
*protseq
, const char *endpoint
)
771 EnterCriticalSection(&protseq
->cs
);
772 for (conn
= protseq
->conn
; conn
; conn
= conn
->Next
)
774 if (!endpoint
|| !strcmp(endpoint
, conn
->Endpoint
))
777 LeaveCriticalSection(&protseq
->cs
);
778 return (conn
!= NULL
);
781 static RPC_STATUS
RPCRT4_use_protseq(RpcServerProtseq
* ps
, const char *endpoint
)
785 EnterCriticalSection(&ps
->cs
);
787 if (RPCRT4_protseq_is_endpoint_registered(ps
, endpoint
))
790 status
= ps
->ops
->open_endpoint(ps
, endpoint
);
792 LeaveCriticalSection(&ps
->cs
);
794 if (status
!= RPC_S_OK
)
799 status
= RPCRT4_start_listen_protseq(ps
, FALSE
);
800 if (status
== RPC_S_OK
)
801 RPCRT4_sync_with_server_thread(ps
);
807 /***********************************************************************
808 * RpcServerInqBindings (RPCRT4.@)
810 RPC_STATUS WINAPI
RpcServerInqBindings( RPC_BINDING_VECTOR
** BindingVector
)
814 RpcServerProtseq
* ps
;
818 TRACE("(*BindingVector == ^%p)\n", *BindingVector
);
820 ERR("(BindingVector == NULL!!?)\n");
822 EnterCriticalSection(&server_cs
);
823 /* count connections */
825 LIST_FOR_EACH_ENTRY(ps
, &protseqs
, RpcServerProtseq
, entry
) {
826 EnterCriticalSection(&ps
->cs
);
827 for (conn
= ps
->conn
; conn
; conn
= conn
->Next
)
829 LeaveCriticalSection(&ps
->cs
);
832 /* export bindings */
833 *BindingVector
= HeapAlloc(GetProcessHeap(), 0,
834 sizeof(RPC_BINDING_VECTOR
) +
835 sizeof(RPC_BINDING_HANDLE
)*(count
-1));
836 (*BindingVector
)->Count
= count
;
838 LIST_FOR_EACH_ENTRY(ps
, &protseqs
, RpcServerProtseq
, entry
) {
839 EnterCriticalSection(&ps
->cs
);
840 for (conn
= ps
->conn
; conn
; conn
= conn
->Next
) {
841 RPCRT4_MakeBinding((RpcBinding
**)&(*BindingVector
)->BindingH
[count
],
845 LeaveCriticalSection(&ps
->cs
);
849 *BindingVector
= NULL
;
850 status
= RPC_S_NO_BINDINGS
;
852 LeaveCriticalSection(&server_cs
);
856 /***********************************************************************
857 * RpcServerUseProtseqEpA (RPCRT4.@)
859 RPC_STATUS WINAPI
RpcServerUseProtseqEpA( RPC_CSTR Protseq
, UINT MaxCalls
, RPC_CSTR Endpoint
, LPVOID SecurityDescriptor
)
863 TRACE( "(%s,%u,%s,%p)\n", Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
);
865 /* This should provide the default behaviour */
866 policy
.Length
= sizeof( policy
);
867 policy
.EndpointFlags
= 0;
870 return RpcServerUseProtseqEpExA( Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
, &policy
);
873 /***********************************************************************
874 * RpcServerUseProtseqEpW (RPCRT4.@)
876 RPC_STATUS WINAPI
RpcServerUseProtseqEpW( RPC_WSTR Protseq
, UINT MaxCalls
, RPC_WSTR Endpoint
, LPVOID SecurityDescriptor
)
880 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq
), MaxCalls
, debugstr_w( Endpoint
), SecurityDescriptor
);
882 /* This should provide the default behaviour */
883 policy
.Length
= sizeof( policy
);
884 policy
.EndpointFlags
= 0;
887 return RpcServerUseProtseqEpExW( Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
, &policy
);
890 /***********************************************************************
891 * alloc_serverprotoseq (internal)
893 * Must be called with server_cs held.
895 static RPC_STATUS
alloc_serverprotoseq(UINT MaxCalls
, const char *Protseq
, RpcServerProtseq
**ps
)
897 const struct protseq_ops
*ops
= rpcrt4_get_protseq_ops(Protseq
);
901 FIXME("protseq %s not supported\n", debugstr_a(Protseq
));
902 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
907 return RPC_S_OUT_OF_RESOURCES
;
908 (*ps
)->MaxCalls
= MaxCalls
;
909 (*ps
)->Protseq
= RPCRT4_strdupA(Protseq
);
913 InitializeCriticalSection(&(*ps
)->cs
);
914 (*ps
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcServerProtseq.cs");
915 (*ps
)->is_listening
= FALSE
;
916 (*ps
)->mgr_mutex
= NULL
;
917 (*ps
)->server_ready_event
= NULL
;
919 list_add_head(&protseqs
, &(*ps
)->entry
);
921 TRACE("new protseq %p created for %s\n", *ps
, Protseq
);
926 /* must be called with server_cs held */
927 static void destroy_serverprotoseq(RpcServerProtseq
*ps
)
929 RPCRT4_strfree(ps
->Protseq
);
930 ps
->cs
.DebugInfo
->Spare
[0] = 0;
931 DeleteCriticalSection(&ps
->cs
);
932 CloseHandle(ps
->mgr_mutex
);
933 CloseHandle(ps
->server_ready_event
);
934 list_remove(&ps
->entry
);
935 HeapFree(GetProcessHeap(), 0, ps
);
938 /* Finds a given protseq or creates a new one if one doesn't already exist */
939 static RPC_STATUS
RPCRT4_get_or_create_serverprotseq(UINT MaxCalls
, const char *Protseq
, RpcServerProtseq
**ps
)
942 RpcServerProtseq
*cps
;
944 EnterCriticalSection(&server_cs
);
946 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
947 if (!strcmp(cps
->Protseq
, Protseq
))
949 TRACE("found existing protseq object for %s\n", Protseq
);
951 LeaveCriticalSection(&server_cs
);
955 status
= alloc_serverprotoseq(MaxCalls
, Protseq
, ps
);
957 LeaveCriticalSection(&server_cs
);
962 /***********************************************************************
963 * RpcServerUseProtseqEpExA (RPCRT4.@)
965 RPC_STATUS WINAPI
RpcServerUseProtseqEpExA( RPC_CSTR Protseq
, UINT MaxCalls
, RPC_CSTR Endpoint
, LPVOID SecurityDescriptor
,
966 PRPC_POLICY lpPolicy
)
968 RpcServerProtseq
* ps
;
971 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq
),
972 MaxCalls
, debugstr_a((const char *)Endpoint
), SecurityDescriptor
,
973 lpPolicy
->Length
, lpPolicy
->EndpointFlags
, lpPolicy
->NICFlags
);
975 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, (const char *)Protseq
, &ps
);
976 if (status
!= RPC_S_OK
)
979 return RPCRT4_use_protseq(ps
, (const char *)Endpoint
);
982 /***********************************************************************
983 * RpcServerUseProtseqEpExW (RPCRT4.@)
985 RPC_STATUS WINAPI
RpcServerUseProtseqEpExW( RPC_WSTR Protseq
, UINT MaxCalls
, RPC_WSTR Endpoint
, LPVOID SecurityDescriptor
,
986 PRPC_POLICY lpPolicy
)
988 RpcServerProtseq
* ps
;
993 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq
), MaxCalls
,
994 debugstr_w( Endpoint
), SecurityDescriptor
,
995 lpPolicy
->Length
, lpPolicy
->EndpointFlags
, lpPolicy
->NICFlags
);
997 ProtseqA
= RPCRT4_strdupWtoA(Protseq
);
998 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, ProtseqA
, &ps
);
999 RPCRT4_strfree(ProtseqA
);
1000 if (status
!= RPC_S_OK
)
1003 EndpointA
= RPCRT4_strdupWtoA(Endpoint
);
1004 status
= RPCRT4_use_protseq(ps
, EndpointA
);
1005 RPCRT4_strfree(EndpointA
);
1009 /***********************************************************************
1010 * RpcServerUseProtseqA (RPCRT4.@)
1012 RPC_STATUS WINAPI
RpcServerUseProtseqA(RPC_CSTR Protseq
, unsigned int MaxCalls
, void *SecurityDescriptor
)
1015 RpcServerProtseq
* ps
;
1017 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq
), MaxCalls
, SecurityDescriptor
);
1019 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, (const char *)Protseq
, &ps
);
1020 if (status
!= RPC_S_OK
)
1023 return RPCRT4_use_protseq(ps
, NULL
);
1026 /***********************************************************************
1027 * RpcServerUseProtseqW (RPCRT4.@)
1029 RPC_STATUS WINAPI
RpcServerUseProtseqW(RPC_WSTR Protseq
, unsigned int MaxCalls
, void *SecurityDescriptor
)
1032 RpcServerProtseq
* ps
;
1035 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq
), MaxCalls
, SecurityDescriptor
);
1037 ProtseqA
= RPCRT4_strdupWtoA(Protseq
);
1038 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, ProtseqA
, &ps
);
1039 RPCRT4_strfree(ProtseqA
);
1040 if (status
!= RPC_S_OK
)
1043 return RPCRT4_use_protseq(ps
, NULL
);
1046 void RPCRT4_destroy_all_protseqs(void)
1048 RpcServerProtseq
*cps
, *cursor2
;
1050 if (listen_count
!= 0)
1053 EnterCriticalSection(&server_cs
);
1054 LIST_FOR_EACH_ENTRY_SAFE(cps
, cursor2
, &protseqs
, RpcServerProtseq
, entry
)
1057 if (listen_count
!= 0)
1058 RPCRT4_sync_with_server_thread(cps
);
1060 destroy_serverprotoseq(cps
);
1062 LeaveCriticalSection(&server_cs
);
1063 DeleteCriticalSection(&server_cs
);
1064 DeleteCriticalSection(&listen_cs
);
1067 /***********************************************************************
1068 * RpcServerRegisterIf (RPCRT4.@)
1070 RPC_STATUS WINAPI
RpcServerRegisterIf( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
)
1072 TRACE("(%p,%s,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
);
1073 return RpcServerRegisterIf2( IfSpec
, MgrTypeUuid
, MgrEpv
, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, (UINT
)-1, NULL
);
1076 /***********************************************************************
1077 * RpcServerRegisterIfEx (RPCRT4.@)
1079 RPC_STATUS WINAPI
RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1080 UINT Flags
, UINT MaxCalls
, RPC_IF_CALLBACK_FN
* IfCallbackFn
)
1082 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
, Flags
, MaxCalls
, IfCallbackFn
);
1083 return RpcServerRegisterIf2( IfSpec
, MgrTypeUuid
, MgrEpv
, Flags
, MaxCalls
, (UINT
)-1, IfCallbackFn
);
1086 /***********************************************************************
1087 * RpcServerRegisterIf2 (RPCRT4.@)
1089 RPC_STATUS WINAPI
RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1090 UINT Flags
, UINT MaxCalls
, UINT MaxRpcSize
, RPC_IF_CALLBACK_FN
* IfCallbackFn
)
1092 PRPC_SERVER_INTERFACE If
= IfSpec
;
1093 RpcServerInterface
* sif
;
1096 TRACE("(%p,%s,%p,%u,%u,%u,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
, Flags
, MaxCalls
,
1097 MaxRpcSize
, IfCallbackFn
);
1098 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
),
1099 If
->InterfaceId
.SyntaxVersion
.MajorVersion
,
1100 If
->InterfaceId
.SyntaxVersion
.MinorVersion
);
1101 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
),
1102 If
->TransferSyntax
.SyntaxVersion
.MajorVersion
,
1103 If
->TransferSyntax
.SyntaxVersion
.MinorVersion
);
1104 TRACE(" dispatch table: %p\n", If
->DispatchTable
);
1105 if (If
->DispatchTable
) {
1106 TRACE(" dispatch table count: %d\n", If
->DispatchTable
->DispatchTableCount
);
1107 for (i
=0; i
<If
->DispatchTable
->DispatchTableCount
; i
++) {
1108 TRACE(" entry %d: %p\n", i
, If
->DispatchTable
->DispatchTable
[i
]);
1110 TRACE(" reserved: %ld\n", If
->DispatchTable
->Reserved
);
1112 TRACE(" protseq endpoint count: %d\n", If
->RpcProtseqEndpointCount
);
1113 TRACE(" default manager epv: %p\n", If
->DefaultManagerEpv
);
1114 TRACE(" interpreter info: %p\n", If
->InterpreterInfo
);
1116 sif
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcServerInterface
));
1119 sif
->MgrTypeUuid
= *MgrTypeUuid
;
1120 sif
->MgrEpv
= MgrEpv
;
1122 memset(&sif
->MgrTypeUuid
, 0, sizeof(UUID
));
1123 sif
->MgrEpv
= If
->DefaultManagerEpv
;
1126 sif
->MaxCalls
= MaxCalls
;
1127 sif
->MaxRpcSize
= MaxRpcSize
;
1128 sif
->IfCallbackFn
= IfCallbackFn
;
1130 EnterCriticalSection(&server_cs
);
1131 list_add_head(&server_interfaces
, &sif
->entry
);
1132 LeaveCriticalSection(&server_cs
);
1134 if (sif
->Flags
& RPC_IF_AUTOLISTEN
)
1135 RPCRT4_start_listen(TRUE
);
1140 /***********************************************************************
1141 * RpcServerUnregisterIf (RPCRT4.@)
1143 RPC_STATUS WINAPI
RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, UINT WaitForCallsToComplete
)
1145 PRPC_SERVER_INTERFACE If
= IfSpec
;
1146 HANDLE event
= NULL
;
1148 BOOL completed
= TRUE
;
1149 RpcServerInterface
*cif
;
1152 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1153 IfSpec
, debugstr_guid(&If
->InterfaceId
.SyntaxGUID
), debugstr_guid(MgrTypeUuid
), WaitForCallsToComplete
);
1155 EnterCriticalSection(&server_cs
);
1156 LIST_FOR_EACH_ENTRY(cif
, &server_interfaces
, RpcServerInterface
, entry
) {
1157 if ((!IfSpec
|| !memcmp(&If
->InterfaceId
, &cif
->If
->InterfaceId
, sizeof(RPC_SYNTAX_IDENTIFIER
))) &&
1158 UuidEqual(MgrTypeUuid
, &cif
->MgrTypeUuid
, &status
)) {
1159 list_remove(&cif
->entry
);
1160 TRACE("unregistering cif %p\n", cif
);
1161 if (cif
->CurrentCalls
) {
1164 if (WaitForCallsToComplete
)
1165 cif
->CallsCompletedEvent
= event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1171 LeaveCriticalSection(&server_cs
);
1174 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid
));
1175 return RPC_S_UNKNOWN_IF
;
1179 HeapFree(GetProcessHeap(), 0, cif
);
1181 /* sif will be freed when the last call is completed, so be careful not to
1182 * touch that memory here as that could happen before we get here */
1183 WaitForSingleObject(event
, INFINITE
);
1190 /***********************************************************************
1191 * RpcServerUnregisterIfEx (RPCRT4.@)
1193 RPC_STATUS WINAPI
RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, int RundownContextHandles
)
1195 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1196 IfSpec
, debugstr_guid(MgrTypeUuid
), RundownContextHandles
);
1201 /***********************************************************************
1202 * RpcObjectSetType (RPCRT4.@)
1205 * ObjUuid [I] "Object" UUID
1206 * TypeUuid [I] "Type" UUID
1209 * RPC_S_OK The call succeeded
1210 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1211 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1213 * Maps "Object" UUIDs to "Type" UUIDs. Passing the nil UUID as the type
1214 * resets the mapping for the specified object UUID to nil (the default).
1215 * The nil object is always associated with the nil type and cannot be
1216 * reassigned. Servers can support multiple implementations on the same
1217 * interface by registering different end-point vectors for the different
1218 * types. There's no need to call this if a server only supports the nil
1219 * type, as is typical.
1221 RPC_STATUS WINAPI
RpcObjectSetType( UUID
* ObjUuid
, UUID
* TypeUuid
)
1223 RpcObjTypeMap
*map
= RpcObjTypeMaps
, *prev
= NULL
;
1226 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid
), debugstr_guid(TypeUuid
));
1227 if ((! ObjUuid
) || UuidIsNil(ObjUuid
, &dummy
)) {
1228 /* nil uuid cannot be remapped */
1229 return RPC_S_INVALID_OBJECT
;
1232 /* find the mapping for this object if there is one ... */
1234 if (! UuidCompare(ObjUuid
, &map
->Object
, &dummy
)) break;
1238 if ((! TypeUuid
) || UuidIsNil(TypeUuid
, &dummy
)) {
1239 /* ... and drop it from the list */
1242 prev
->next
= map
->next
;
1244 RpcObjTypeMaps
= map
->next
;
1245 HeapFree(GetProcessHeap(), 0, map
);
1248 /* ... , fail if we found it ... */
1250 return RPC_S_ALREADY_REGISTERED
;
1251 /* ... otherwise create a new one and add it in. */
1252 map
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap
));
1253 map
->Object
= *ObjUuid
;
1254 map
->Type
= *TypeUuid
;
1257 prev
->next
= map
; /* prev is the last map in the linklist */
1259 RpcObjTypeMaps
= map
;
1265 struct rpc_server_registered_auth_info
1274 RPC_STATUS
RPCRT4_ServerGetRegisteredAuthInfo(
1275 USHORT auth_type
, CredHandle
*cred
, TimeStamp
*exp
, ULONG
*max_token
)
1277 RPC_STATUS status
= RPC_S_UNKNOWN_AUTHN_SERVICE
;
1278 struct rpc_server_registered_auth_info
*auth_info
;
1280 EnterCriticalSection(&server_auth_info_cs
);
1281 LIST_FOR_EACH_ENTRY(auth_info
, &server_registered_auth_info
, struct rpc_server_registered_auth_info
, entry
)
1283 if (auth_info
->auth_type
== auth_type
)
1285 *cred
= auth_info
->cred
;
1286 *exp
= auth_info
->exp
;
1287 *max_token
= auth_info
->max_token
;
1292 LeaveCriticalSection(&server_auth_info_cs
);
1297 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1299 struct rpc_server_registered_auth_info
*auth_info
, *cursor2
;
1301 EnterCriticalSection(&server_auth_info_cs
);
1302 LIST_FOR_EACH_ENTRY_SAFE(auth_info
, cursor2
, &server_registered_auth_info
, struct rpc_server_registered_auth_info
, entry
)
1304 FreeCredentialsHandle(&auth_info
->cred
);
1305 HeapFree(GetProcessHeap(), 0, auth_info
);
1307 LeaveCriticalSection(&server_auth_info_cs
);
1308 DeleteCriticalSection(&server_auth_info_cs
);
1311 /***********************************************************************
1312 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1314 RPC_STATUS WINAPI
RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName
, ULONG AuthnSvc
, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn
,
1317 SECURITY_STATUS sec_status
;
1320 ULONG package_count
;
1322 PSecPkgInfoA packages
;
1324 struct rpc_server_registered_auth_info
*auth_info
;
1326 TRACE("(%s,%u,%p,%p)\n", ServerPrincName
, AuthnSvc
, GetKeyFn
, Arg
);
1328 sec_status
= EnumerateSecurityPackagesA(&package_count
, &packages
);
1329 if (sec_status
!= SEC_E_OK
)
1331 ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n",
1333 return RPC_S_SEC_PKG_ERROR
;
1336 for (i
= 0; i
< package_count
; i
++)
1337 if (packages
[i
].wRPCID
== AuthnSvc
)
1340 if (i
== package_count
)
1342 WARN("unsupported AuthnSvc %u\n", AuthnSvc
);
1343 FreeContextBuffer(packages
);
1344 return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1346 TRACE("found package %s for service %u\n", packages
[i
].Name
,
1348 sec_status
= AcquireCredentialsHandleA((SEC_CHAR
*)ServerPrincName
,
1350 SECPKG_CRED_INBOUND
, NULL
, NULL
,
1351 NULL
, NULL
, &cred
, &exp
);
1352 max_token
= packages
[i
].cbMaxToken
;
1353 FreeContextBuffer(packages
);
1354 if (sec_status
!= SEC_E_OK
)
1355 return RPC_S_SEC_PKG_ERROR
;
1357 auth_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info
));
1360 FreeCredentialsHandle(&cred
);
1361 return RPC_S_OUT_OF_RESOURCES
;
1364 auth_info
->exp
= exp
;
1365 auth_info
->cred
= cred
;
1366 auth_info
->max_token
= max_token
;
1367 auth_info
->auth_type
= AuthnSvc
;
1369 EnterCriticalSection(&server_auth_info_cs
);
1370 list_add_tail(&server_registered_auth_info
, &auth_info
->entry
);
1371 LeaveCriticalSection(&server_auth_info_cs
);
1376 /***********************************************************************
1377 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1379 RPC_STATUS WINAPI
RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName
, ULONG AuthnSvc
, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn
,
1382 SECURITY_STATUS sec_status
;
1385 ULONG package_count
;
1387 PSecPkgInfoW packages
;
1389 struct rpc_server_registered_auth_info
*auth_info
;
1391 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName
), AuthnSvc
, GetKeyFn
, Arg
);
1393 sec_status
= EnumerateSecurityPackagesW(&package_count
, &packages
);
1394 if (sec_status
!= SEC_E_OK
)
1396 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n",
1398 return RPC_S_SEC_PKG_ERROR
;
1401 for (i
= 0; i
< package_count
; i
++)
1402 if (packages
[i
].wRPCID
== AuthnSvc
)
1405 if (i
== package_count
)
1407 WARN("unsupported AuthnSvc %u\n", AuthnSvc
);
1408 FreeContextBuffer(packages
);
1409 return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1411 TRACE("found package %s for service %u\n", debugstr_w(packages
[i
].Name
),
1413 sec_status
= AcquireCredentialsHandleW((SEC_WCHAR
*)ServerPrincName
,
1415 SECPKG_CRED_INBOUND
, NULL
, NULL
,
1416 NULL
, NULL
, &cred
, &exp
);
1417 max_token
= packages
[i
].cbMaxToken
;
1418 FreeContextBuffer(packages
);
1419 if (sec_status
!= SEC_E_OK
)
1420 return RPC_S_SEC_PKG_ERROR
;
1422 auth_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info
));
1425 FreeCredentialsHandle(&cred
);
1426 return RPC_S_OUT_OF_RESOURCES
;
1429 auth_info
->exp
= exp
;
1430 auth_info
->cred
= cred
;
1431 auth_info
->max_token
= max_token
;
1432 auth_info
->auth_type
= AuthnSvc
;
1434 EnterCriticalSection(&server_auth_info_cs
);
1435 list_add_tail(&server_registered_auth_info
, &auth_info
->entry
);
1436 LeaveCriticalSection(&server_auth_info_cs
);
1441 /******************************************************************************
1442 * RpcServerInqDefaultPrincNameA (rpcrt4.@)
1444 RPC_STATUS RPC_ENTRY
RpcServerInqDefaultPrincNameA(ULONG AuthnSvc
, RPC_CSTR
*PrincName
)
1447 RPC_WSTR principalW
;
1449 TRACE("%u, %p\n", AuthnSvc
, PrincName
);
1451 if ((ret
= RpcServerInqDefaultPrincNameW( AuthnSvc
, &principalW
)) == RPC_S_OK
)
1453 if (!(*PrincName
= (RPC_CSTR
)RPCRT4_strdupWtoA( principalW
))) return RPC_S_OUT_OF_MEMORY
;
1454 RpcStringFreeW( &principalW
);
1459 /******************************************************************************
1460 * RpcServerInqDefaultPrincNameW (rpcrt4.@)
1462 RPC_STATUS RPC_ENTRY
RpcServerInqDefaultPrincNameW(ULONG AuthnSvc
, RPC_WSTR
*PrincName
)
1466 FIXME("%u, %p\n", AuthnSvc
, PrincName
);
1468 if (AuthnSvc
!= RPC_C_AUTHN_WINNT
) return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1470 GetUserNameExW( NameSamCompatible
, NULL
, &len
);
1471 if (GetLastError() != ERROR_MORE_DATA
) return RPC_S_INTERNAL_ERROR
;
1473 if (!(*PrincName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1474 return RPC_S_OUT_OF_MEMORY
;
1476 GetUserNameExW( NameSamCompatible
, *PrincName
, &len
);
1480 /***********************************************************************
1481 * RpcServerListen (RPCRT4.@)
1483 RPC_STATUS WINAPI
RpcServerListen( UINT MinimumCallThreads
, UINT MaxCalls
, UINT DontWait
)
1485 RPC_STATUS status
= RPC_S_OK
;
1487 TRACE("(%u,%u,%u)\n", MinimumCallThreads
, MaxCalls
, DontWait
);
1489 if (list_empty(&protseqs
))
1490 return RPC_S_NO_PROTSEQS_REGISTERED
;
1492 status
= RPCRT4_start_listen(FALSE
);
1494 if (DontWait
|| (status
!= RPC_S_OK
)) return status
;
1496 return RpcMgmtWaitServerListen();
1499 /***********************************************************************
1500 * RpcMgmtServerWaitListen (RPCRT4.@)
1502 RPC_STATUS WINAPI
RpcMgmtWaitServerListen( void )
1508 EnterCriticalSection(&listen_cs
);
1511 LeaveCriticalSection(&listen_cs
);
1512 return RPC_S_NOT_LISTENING
;
1514 if (listen_done_event
) {
1515 LeaveCriticalSection(&listen_cs
);
1516 return RPC_S_ALREADY_LISTENING
;
1518 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1519 listen_done_event
= event
;
1521 LeaveCriticalSection(&listen_cs
);
1523 TRACE( "waiting for server calls to finish\n" );
1524 WaitForSingleObject( event
, INFINITE
);
1525 TRACE( "done waiting\n" );
1527 CloseHandle( event
);
1531 /***********************************************************************
1532 * RpcMgmtStopServerListening (RPCRT4.@)
1534 RPC_STATUS WINAPI
RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding
)
1536 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding
);
1539 FIXME("client-side invocation not implemented.\n");
1540 return RPC_S_WRONG_KIND_OF_BINDING
;
1543 RPCRT4_stop_listen(FALSE
);
1548 /***********************************************************************
1549 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1551 RPC_STATUS WINAPI
RpcMgmtEnableIdleCleanup(void)
1553 FIXME("(): stub\n");
1557 /***********************************************************************
1558 * I_RpcServerStartListening (RPCRT4.@)
1560 RPC_STATUS WINAPI
I_RpcServerStartListening( HWND hWnd
)
1562 FIXME( "(%p): stub\n", hWnd
);
1567 /***********************************************************************
1568 * I_RpcServerStopListening (RPCRT4.@)
1570 RPC_STATUS WINAPI
I_RpcServerStopListening( void )
1572 FIXME( "(): stub\n" );
1577 /***********************************************************************
1578 * I_RpcWindowProc (RPCRT4.@)
1580 UINT WINAPI
I_RpcWindowProc( void *hWnd
, UINT Message
, UINT wParam
, ULONG lParam
)
1582 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd
, Message
, wParam
, lParam
);
1587 /***********************************************************************
1588 * RpcMgmtInqIfIds (RPCRT4.@)
1590 RPC_STATUS WINAPI
RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding
, RPC_IF_ID_VECTOR
**IfIdVector
)
1592 FIXME("(%p,%p): stub\n", Binding
, IfIdVector
);
1593 return RPC_S_INVALID_BINDING
;
1596 /***********************************************************************
1597 * RpcMgmtInqStats (RPCRT4.@)
1599 RPC_STATUS WINAPI
RpcMgmtInqStats(RPC_BINDING_HANDLE Binding
, RPC_STATS_VECTOR
**Statistics
)
1601 RPC_STATS_VECTOR
*stats
;
1603 FIXME("(%p,%p)\n", Binding
, Statistics
);
1605 if ((stats
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR
))))
1608 stats
->Stats
[0] = 0;
1609 *Statistics
= stats
;
1612 return RPC_S_OUT_OF_RESOURCES
;
1615 /***********************************************************************
1616 * RpcMgmtStatsVectorFree (RPCRT4.@)
1618 RPC_STATUS WINAPI
RpcMgmtStatsVectorFree(RPC_STATS_VECTOR
**StatsVector
)
1620 FIXME("(%p)\n", StatsVector
);
1624 HeapFree(GetProcessHeap(), 0, *StatsVector
);
1625 *StatsVector
= NULL
;
1630 /***********************************************************************
1631 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1633 RPC_STATUS WINAPI
RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding
, ULONG InquiryType
,
1634 RPC_IF_ID
*IfId
, ULONG VersOption
, UUID
*ObjectUuid
, RPC_EP_INQ_HANDLE
* InquiryContext
)
1636 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1637 Binding
, InquiryType
, IfId
, VersOption
, ObjectUuid
, InquiryContext
);
1638 return RPC_S_INVALID_BINDING
;
1641 /***********************************************************************
1642 * RpcMgmtIsServerListening (RPCRT4.@)
1644 RPC_STATUS WINAPI
RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding
)
1646 RPC_STATUS status
= RPC_S_NOT_LISTENING
;
1648 TRACE("(%p)\n", Binding
);
1650 EnterCriticalSection(&listen_cs
);
1651 if (manual_listen_count
> 0) status
= RPC_S_OK
;
1652 LeaveCriticalSection(&listen_cs
);
1656 /***********************************************************************
1657 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1659 RPC_STATUS WINAPI
RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn
)
1661 FIXME("(%p): stub\n", fn
);
1665 /***********************************************************************
1666 * RpcMgmtSetServerStackSize (RPCRT4.@)
1668 RPC_STATUS WINAPI
RpcMgmtSetServerStackSize(ULONG ThreadStackSize
)
1670 FIXME("(0x%x): stub\n", ThreadStackSize
);
1674 /***********************************************************************
1675 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1677 RPC_BINDING_HANDLE WINAPI
I_RpcGetCurrentCallHandle(void)
1680 return RPCRT4_GetThreadCurrentCallHandle();