4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006-2008 Robert Shearman (for CodeWeavers)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
39 #include "wine/debug.h"
40 #include "wine/exception.h"
42 #include "rpc_server.h"
43 #include "rpc_assoc.h"
44 #include "rpc_message.h"
46 #include "ncastatus.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
51 typedef struct _RpcPacket
53 struct _RpcConnection
* conn
;
56 unsigned char *auth_data
;
60 typedef struct _RpcObjTypeMap
62 /* FIXME: a hash table would be better. */
63 struct _RpcObjTypeMap
*next
;
68 static RpcObjTypeMap
*RpcObjTypeMaps
;
70 /* list of type RpcServerProtseq */
71 static struct list protseqs
= LIST_INIT(protseqs
);
72 static struct list server_interfaces
= LIST_INIT(server_interfaces
);
73 static struct list server_registered_auth_info
= LIST_INIT(server_registered_auth_info
);
75 static CRITICAL_SECTION server_cs
;
76 static CRITICAL_SECTION_DEBUG server_cs_debug
=
79 { &server_cs_debug
.ProcessLocksList
, &server_cs_debug
.ProcessLocksList
},
80 0, 0, { (DWORD_PTR
)(__FILE__
": server_cs") }
82 static CRITICAL_SECTION server_cs
= { &server_cs_debug
, -1, 0, 0, 0, 0 };
84 static CRITICAL_SECTION listen_cs
;
85 static CRITICAL_SECTION_DEBUG listen_cs_debug
=
88 { &listen_cs_debug
.ProcessLocksList
, &listen_cs_debug
.ProcessLocksList
},
89 0, 0, { (DWORD_PTR
)(__FILE__
": listen_cs") }
91 static CRITICAL_SECTION listen_cs
= { &listen_cs_debug
, -1, 0, 0, 0, 0 };
93 static CRITICAL_SECTION server_auth_info_cs
;
94 static CRITICAL_SECTION_DEBUG server_auth_info_cs_debug
=
96 0, 0, &server_auth_info_cs
,
97 { &server_auth_info_cs_debug
.ProcessLocksList
, &server_auth_info_cs_debug
.ProcessLocksList
},
98 0, 0, { (DWORD_PTR
)(__FILE__
": server_auth_info_cs") }
100 static CRITICAL_SECTION server_auth_info_cs
= { &server_auth_info_cs_debug
, -1, 0, 0, 0, 0 };
102 /* whether the server is currently listening */
103 static BOOL std_listen
;
104 /* total listeners including auto listeners */
105 static LONG listen_count
;
106 /* event set once all manual listening is finished */
107 static HANDLE listen_done_event
;
109 static UUID uuid_nil
;
111 static inline RpcObjTypeMap
*LookupObjTypeMap(UUID
*ObjUuid
)
113 RpcObjTypeMap
*rslt
= RpcObjTypeMaps
;
117 if (! UuidCompare(ObjUuid
, &rslt
->Object
, &dummy
)) break;
124 static inline UUID
*LookupObjType(UUID
*ObjUuid
)
126 RpcObjTypeMap
*map
= LookupObjTypeMap(ObjUuid
);
133 static RpcServerInterface
* RPCRT4_find_interface(UUID
* object
,
134 const RPC_SYNTAX_IDENTIFIER
*if_id
,
135 const RPC_SYNTAX_IDENTIFIER
*transfer_syntax
,
138 UUID
* MgrType
= NULL
;
139 RpcServerInterface
* cif
;
143 MgrType
= LookupObjType(object
);
144 EnterCriticalSection(&server_cs
);
145 LIST_FOR_EACH_ENTRY(cif
, &server_interfaces
, RpcServerInterface
, entry
) {
146 if (!memcmp(if_id
, &cif
->If
->InterfaceId
, sizeof(RPC_SYNTAX_IDENTIFIER
)) &&
147 (!transfer_syntax
|| !memcmp(transfer_syntax
, &cif
->If
->TransferSyntax
, sizeof(RPC_SYNTAX_IDENTIFIER
))) &&
148 (check_object
== FALSE
|| UuidEqual(MgrType
, &cif
->MgrTypeUuid
, &status
)) &&
150 InterlockedIncrement(&cif
->CurrentCalls
);
154 LeaveCriticalSection(&server_cs
);
155 if (&cif
->entry
== &server_interfaces
) cif
= NULL
;
156 TRACE("returning %p for object %s, if_id { %d.%d %s }\n", cif
,
157 debugstr_guid(object
), if_id
->SyntaxVersion
.MajorVersion
,
158 if_id
->SyntaxVersion
.MinorVersion
, debugstr_guid(&if_id
->SyntaxGUID
));
162 static void RPCRT4_release_server_interface(RpcServerInterface
*sif
)
164 if (!InterlockedDecrement(&sif
->CurrentCalls
) &&
166 /* sif must have been removed from server_interfaces before
167 * CallsCompletedEvent is set */
168 if (sif
->CallsCompletedEvent
)
169 SetEvent(sif
->CallsCompletedEvent
);
170 HeapFree(GetProcessHeap(), 0, sif
);
174 static RpcPktHdr
*handle_bind_error(RpcConnection
*conn
, RPC_STATUS error
)
176 unsigned int reject_reason
;
179 case RPC_S_SERVER_TOO_BUSY
:
180 reject_reason
= REJECT_TEMPORARY_CONGESTION
;
182 case ERROR_OUTOFMEMORY
:
183 case RPC_S_OUT_OF_RESOURCES
:
184 reject_reason
= REJECT_LOCAL_LIMIT_EXCEEDED
;
186 case RPC_S_PROTOCOL_ERROR
:
187 reject_reason
= REJECT_PROTOCOL_VERSION_NOT_SUPPORTED
;
189 case RPC_S_UNKNOWN_AUTHN_SERVICE
:
190 reject_reason
= REJECT_UNKNOWN_AUTHN_SERVICE
;
192 case ERROR_ACCESS_DENIED
:
193 reject_reason
= REJECT_INVALID_CHECKSUM
;
196 FIXME("unexpected status value %d\n", error
);
198 case RPC_S_INVALID_BOUND
:
199 reject_reason
= REJECT_REASON_NOT_SPECIFIED
;
202 return RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION
,
203 RPC_VER_MAJOR
, RPC_VER_MINOR
,
207 static RPC_STATUS
process_bind_packet_no_send(
208 RpcConnection
*conn
, RpcPktBindHdr
*hdr
, RPC_MESSAGE
*msg
,
209 unsigned char *auth_data
, ULONG auth_length
, RpcPktHdr
**ack_response
,
210 unsigned char **auth_data_out
, ULONG
*auth_length_out
)
213 RpcContextElement
*ctxt_elem
;
218 for (i
= 0, ctxt_elem
= msg
->Buffer
;
219 i
< hdr
->num_elements
;
220 i
++, ctxt_elem
= (RpcContextElement
*)&ctxt_elem
->transfer_syntaxes
[ctxt_elem
->num_syntaxes
])
222 if (((char *)ctxt_elem
- (char *)msg
->Buffer
) > msg
->BufferLength
||
223 ((char *)&ctxt_elem
->transfer_syntaxes
[ctxt_elem
->num_syntaxes
] - (char *)msg
->Buffer
) > msg
->BufferLength
)
225 ERR("inconsistent data in packet - packet length %d, num elements %d\n",
226 msg
->BufferLength
, hdr
->num_elements
);
227 return RPC_S_INVALID_BOUND
;
231 if (hdr
->max_tsize
< RPC_MIN_PACKET_SIZE
||
232 !UuidIsNil(&conn
->ActiveInterface
.SyntaxGUID
, &status
) ||
233 conn
->server_binding
)
235 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
237 return RPC_S_INVALID_BOUND
;
240 results
= HeapAlloc(GetProcessHeap(), 0,
241 hdr
->num_elements
* sizeof(*results
));
243 return RPC_S_OUT_OF_RESOURCES
;
245 for (i
= 0, ctxt_elem
= (RpcContextElement
*)msg
->Buffer
;
246 i
< hdr
->num_elements
;
247 i
++, ctxt_elem
= (RpcContextElement
*)&ctxt_elem
->transfer_syntaxes
[ctxt_elem
->num_syntaxes
])
249 RpcServerInterface
* sif
= NULL
;
252 for (j
= 0; !sif
&& j
< ctxt_elem
->num_syntaxes
; j
++)
254 sif
= RPCRT4_find_interface(NULL
, &ctxt_elem
->abstract_syntax
,
255 &ctxt_elem
->transfer_syntaxes
[j
], FALSE
);
261 RPCRT4_release_server_interface(sif
);
262 TRACE("accepting bind request on connection %p for %s\n", conn
,
263 debugstr_guid(&ctxt_elem
->abstract_syntax
.SyntaxGUID
));
264 results
[i
].result
= RESULT_ACCEPT
;
265 results
[i
].reason
= REASON_NONE
;
266 results
[i
].transfer_syntax
= ctxt_elem
->transfer_syntaxes
[j
];
268 /* save the interface for later use */
269 /* FIXME: save linked list */
270 conn
->ActiveInterface
= ctxt_elem
->abstract_syntax
;
272 else if ((sif
= RPCRT4_find_interface(NULL
, &ctxt_elem
->abstract_syntax
,
273 NULL
, FALSE
)) != NULL
)
275 RPCRT4_release_server_interface(sif
);
276 TRACE("not accepting bind request on connection %p for %s - no transfer syntaxes supported\n",
277 conn
, debugstr_guid(&ctxt_elem
->abstract_syntax
.SyntaxGUID
));
278 results
[i
].result
= RESULT_PROVIDER_REJECTION
;
279 results
[i
].reason
= REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED
;
280 memset(&results
[i
].transfer_syntax
, 0, sizeof(results
[i
].transfer_syntax
));
284 TRACE("not accepting bind request on connection %p for %s - abstract syntax not supported\n",
285 conn
, debugstr_guid(&ctxt_elem
->abstract_syntax
.SyntaxGUID
));
286 results
[i
].result
= RESULT_PROVIDER_REJECTION
;
287 results
[i
].reason
= REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED
;
288 memset(&results
[i
].transfer_syntax
, 0, sizeof(results
[i
].transfer_syntax
));
292 /* create temporary binding */
293 status
= RPCRT4_MakeBinding(&conn
->server_binding
, conn
);
294 if (status
!= RPC_S_OK
)
296 HeapFree(GetProcessHeap(), 0, results
);
300 status
= RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn
),
301 conn
->NetworkAddr
, conn
->Endpoint
,
302 conn
->NetworkOptions
,
304 &conn
->server_binding
->Assoc
);
305 if (status
!= RPC_S_OK
)
307 HeapFree(GetProcessHeap(), 0, results
);
313 status
= RPCRT4_ServerConnectionAuth(conn
, TRUE
,
314 (RpcAuthVerifier
*)auth_data
,
315 auth_length
, auth_data_out
,
317 if (status
!= RPC_S_OK
)
319 HeapFree(GetProcessHeap(), 0, results
);
324 *ack_response
= RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION
,
327 conn
->server_binding
->Assoc
->assoc_group_id
,
328 conn
->Endpoint
, hdr
->num_elements
,
330 HeapFree(GetProcessHeap(), 0, results
);
333 conn
->MaxTransmissionSize
= hdr
->max_tsize
;
335 status
= RPC_S_OUT_OF_RESOURCES
;
340 static RPC_STATUS
process_bind_packet(RpcConnection
*conn
, RpcPktBindHdr
*hdr
,
342 unsigned char *auth_data
,
346 RpcPktHdr
*response
= NULL
;
347 unsigned char *auth_data_out
= NULL
;
348 ULONG auth_length_out
= 0;
350 status
= process_bind_packet_no_send(conn
, hdr
, msg
, auth_data
, auth_length
,
351 &response
, &auth_data_out
,
353 if (status
!= RPC_S_OK
)
354 response
= handle_bind_error(conn
, status
);
356 status
= RPCRT4_SendWithAuth(conn
, response
, NULL
, 0, auth_data_out
, auth_length_out
);
358 status
= ERROR_OUTOFMEMORY
;
359 RPCRT4_FreeHeader(response
);
365 static RPC_STATUS
process_request_packet(RpcConnection
*conn
, RpcPktRequestHdr
*hdr
, RPC_MESSAGE
*msg
)
368 RpcPktHdr
*response
= NULL
;
369 RpcServerInterface
* sif
;
370 RPC_DISPATCH_FUNCTION func
;
373 NDR_SCONTEXT context_handle
;
374 void *buf
= msg
->Buffer
;
376 /* fail if the connection isn't bound with an interface */
377 if (UuidIsNil(&conn
->ActiveInterface
.SyntaxGUID
, &status
)) {
378 /* FIXME: should send BindNack instead */
379 response
= RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION
,
382 RPCRT4_Send(conn
, response
, NULL
, 0);
383 RPCRT4_FreeHeader(response
);
387 if (hdr
->common
.flags
& RPC_FLG_OBJECT_UUID
) {
388 object_uuid
= (UUID
*)(hdr
+ 1);
393 sif
= RPCRT4_find_interface(object_uuid
, &conn
->ActiveInterface
, NULL
, TRUE
);
395 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn
->ActiveInterface
.SyntaxGUID
));
396 response
= RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION
,
399 RPCRT4_Send(conn
, response
, NULL
, 0);
400 RPCRT4_FreeHeader(response
);
403 msg
->RpcInterfaceInformation
= sif
->If
;
404 /* copy the endpoint vector from sif to msg so that midl-generated code will use it */
405 msg
->ManagerEpv
= sif
->MgrEpv
;
406 if (object_uuid
!= NULL
) {
407 RPCRT4_SetBindingObject(msg
->Handle
, object_uuid
);
410 /* find dispatch function */
411 msg
->ProcNum
= hdr
->opnum
;
412 if (sif
->Flags
& RPC_IF_OLE
) {
413 /* native ole32 always gives us a dispatch table with a single entry
414 * (I assume that's a wrapper for IRpcStubBuffer::Invoke) */
415 func
= *sif
->If
->DispatchTable
->DispatchTable
;
417 if (msg
->ProcNum
>= sif
->If
->DispatchTable
->DispatchTableCount
) {
418 WARN("invalid procnum (%d/%d)\n", msg
->ProcNum
, sif
->If
->DispatchTable
->DispatchTableCount
);
419 response
= RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION
,
422 RPCRT4_Send(conn
, response
, NULL
, 0);
423 RPCRT4_FreeHeader(response
);
425 func
= sif
->If
->DispatchTable
->DispatchTable
[msg
->ProcNum
];
428 /* put in the drep. FIXME: is this more universally applicable?
429 perhaps we should move this outward... */
430 msg
->DataRepresentation
=
431 MAKELONG( MAKEWORD(hdr
->common
.drep
[0], hdr
->common
.drep
[1]),
432 MAKEWORD(hdr
->common
.drep
[2], hdr
->common
.drep
[3]));
437 RPCRT4_SetThreadCurrentCallHandle(msg
->Handle
);
441 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
443 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION
)
444 status
= ERROR_NOACCESS
;
446 status
= GetExceptionCode();
447 response
= RPCRT4_BuildFaultHeader(msg
->DataRepresentation
,
448 RPC2NCA_STATUS(status
));
450 RPCRT4_SetThreadCurrentCallHandle(NULL
);
452 /* release any unmarshalled context handles */
453 while ((context_handle
= RPCRT4_PopThreadContextHandle()) != NULL
)
454 RpcServerAssoc_ReleaseContextHandle(conn
->server_binding
->Assoc
, context_handle
, TRUE
);
457 response
= RPCRT4_BuildResponseHeader(msg
->DataRepresentation
,
460 /* send response packet */
462 status
= RPCRT4_Send(conn
, response
, exception
? NULL
: msg
->Buffer
,
463 exception
? 0 : msg
->BufferLength
);
464 RPCRT4_FreeHeader(response
);
466 ERR("out of memory\n");
468 msg
->RpcInterfaceInformation
= NULL
;
469 RPCRT4_release_server_interface(sif
);
471 if (msg
->Buffer
== buf
) buf
= NULL
;
472 TRACE("freeing Buffer=%p\n", buf
);
478 static RPC_STATUS
process_auth3_packet(RpcConnection
*conn
,
479 RpcPktCommonHdr
*hdr
,
481 unsigned char *auth_data
,
486 if (UuidIsNil(&conn
->ActiveInterface
.SyntaxGUID
, &status
) ||
487 !auth_length
|| msg
->BufferLength
!= 0)
488 status
= RPC_S_PROTOCOL_ERROR
;
491 status
= RPCRT4_ServerConnectionAuth(conn
, FALSE
,
492 (RpcAuthVerifier
*)auth_data
,
493 auth_length
, NULL
, NULL
);
496 /* FIXME: client doesn't expect a response to this message so must store
497 * status in connection so that fault packet can be returned when next
498 * packet is received */
503 static void RPCRT4_process_packet(RpcConnection
* conn
, RpcPktHdr
* hdr
,
504 RPC_MESSAGE
* msg
, unsigned char *auth_data
,
507 msg
->Handle
= (RPC_BINDING_HANDLE
)conn
->server_binding
;
509 switch (hdr
->common
.ptype
) {
511 TRACE("got bind packet\n");
512 process_bind_packet(conn
, &hdr
->bind
, msg
, auth_data
, auth_length
);
516 TRACE("got request packet\n");
517 process_request_packet(conn
, &hdr
->request
, msg
);
521 TRACE("got auth3 packet\n");
522 process_auth3_packet(conn
, &hdr
->common
, msg
, auth_data
, auth_length
);
525 FIXME("unhandled packet type %u\n", hdr
->common
.ptype
);
530 I_RpcFree(msg
->Buffer
);
531 RPCRT4_FreeHeader(hdr
);
532 HeapFree(GetProcessHeap(), 0, msg
);
533 HeapFree(GetProcessHeap(), 0, auth_data
);
536 static DWORD CALLBACK
RPCRT4_worker_thread(LPVOID the_arg
)
538 RpcPacket
*pkt
= the_arg
;
539 RPCRT4_process_packet(pkt
->conn
, pkt
->hdr
, pkt
->msg
, pkt
->auth_data
,
541 RPCRT4_ReleaseConnection(pkt
->conn
);
542 HeapFree(GetProcessHeap(), 0, pkt
);
546 static DWORD CALLBACK
RPCRT4_io_thread(LPVOID the_arg
)
548 RpcConnection
* conn
= the_arg
;
553 unsigned char *auth_data
;
556 TRACE("(%p)\n", conn
);
559 msg
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RPC_MESSAGE
));
562 status
= RPCRT4_ReceiveWithAuth(conn
, &hdr
, msg
, &auth_data
, &auth_length
);
563 if (status
!= RPC_S_OK
) {
564 WARN("receive failed with error %x\n", status
);
565 HeapFree(GetProcessHeap(), 0, msg
);
569 switch (hdr
->common
.ptype
) {
571 TRACE("got bind packet\n");
573 status
= process_bind_packet(conn
, &hdr
->bind
, msg
, auth_data
,
578 TRACE("got request packet\n");
580 packet
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket
));
582 I_RpcFree(msg
->Buffer
);
583 RPCRT4_FreeHeader(hdr
);
584 HeapFree(GetProcessHeap(), 0, msg
);
585 HeapFree(GetProcessHeap(), 0, auth_data
);
588 packet
->conn
= RPCRT4_GrabConnection( conn
);
591 packet
->auth_data
= auth_data
;
592 packet
->auth_length
= auth_length
;
593 if (!QueueUserWorkItem(RPCRT4_worker_thread
, packet
, WT_EXECUTELONGFUNCTION
)) {
594 ERR("couldn't queue work item for worker thread, error was %d\n", GetLastError());
595 HeapFree(GetProcessHeap(), 0, packet
);
596 status
= RPC_S_OUT_OF_RESOURCES
;
603 TRACE("got auth3 packet\n");
605 status
= process_auth3_packet(conn
, &hdr
->common
, msg
, auth_data
,
609 FIXME("unhandled packet type %u\n", hdr
->common
.ptype
);
613 I_RpcFree(msg
->Buffer
);
614 RPCRT4_FreeHeader(hdr
);
615 HeapFree(GetProcessHeap(), 0, msg
);
616 HeapFree(GetProcessHeap(), 0, auth_data
);
618 if (status
!= RPC_S_OK
) {
619 WARN("processing packet failed with error %u\n", status
);
624 RPCRT4_ReleaseConnection(conn
);
628 void RPCRT4_new_client(RpcConnection
* conn
)
630 HANDLE thread
= CreateThread(NULL
, 0, RPCRT4_io_thread
, conn
, 0, NULL
);
632 DWORD err
= GetLastError();
633 ERR("failed to create thread, error=%08x\n", err
);
634 RPCRT4_ReleaseConnection(conn
);
636 /* we could set conn->thread, but then we'd have to make the io_thread wait
637 * for that, otherwise the thread might finish, destroy the connection, and
638 * free the memory we'd write to before we did, causing crashes and stuff -
639 * so let's implement that later, when we really need conn->thread */
641 CloseHandle( thread
);
644 static DWORD CALLBACK
RPCRT4_server_thread(LPVOID the_arg
)
649 RpcServerProtseq
* cps
= the_arg
;
651 BOOL set_ready_event
= FALSE
;
653 TRACE("(the_arg == ^%p)\n", the_arg
);
656 objs
= cps
->ops
->get_wait_array(cps
, objs
, &count
);
660 /* signal to function that changed state that we are now sync'ed */
661 SetEvent(cps
->server_ready_event
);
662 set_ready_event
= FALSE
;
666 res
= cps
->ops
->wait_for_new_connection(cps
, count
, objs
);
668 if (res
== -1 || (res
== 0 && !std_listen
))
671 cps
->ops
->free_wait_array(cps
, objs
);
675 set_ready_event
= TRUE
;
678 TRACE("closing connections\n");
680 EnterCriticalSection(&cps
->cs
);
681 LIST_FOR_EACH_ENTRY(conn
, &cps
->listeners
, RpcConnection
, protseq_entry
)
682 RPCRT4_CloseConnection(conn
);
683 LIST_FOR_EACH_ENTRY(conn
, &cps
->connections
, RpcConnection
, protseq_entry
)
685 RPCRT4_GrabConnection(conn
);
686 rpcrt4_conn_close_read(conn
);
688 LeaveCriticalSection(&cps
->cs
);
690 if (res
== 0 && !std_listen
)
691 SetEvent(cps
->server_ready_event
);
693 TRACE("waiting for active connections to close\n");
695 EnterCriticalSection(&cps
->cs
);
696 while (!list_empty(&cps
->connections
))
698 conn
= LIST_ENTRY(list_head(&cps
->connections
), RpcConnection
, protseq_entry
);
699 LeaveCriticalSection(&cps
->cs
);
700 rpcrt4_conn_release_and_wait(conn
);
701 EnterCriticalSection(&cps
->cs
);
703 LeaveCriticalSection(&cps
->cs
);
705 EnterCriticalSection(&listen_cs
);
706 CloseHandle(cps
->server_thread
);
707 cps
->server_thread
= NULL
;
708 LeaveCriticalSection(&listen_cs
);
713 /* tells the server thread that the state has changed and waits for it to
714 * make the changes */
715 static void RPCRT4_sync_with_server_thread(RpcServerProtseq
*ps
)
717 /* make sure we are the only thread sync'ing the server state, otherwise
718 * there is a race with the server thread setting an older state and setting
719 * the server_ready_event when the new state hasn't yet been applied */
720 WaitForSingleObject(ps
->mgr_mutex
, INFINITE
);
722 ps
->ops
->signal_state_changed(ps
);
724 /* wait for server thread to make the requested changes before returning */
725 WaitForSingleObject(ps
->server_ready_event
, INFINITE
);
727 ReleaseMutex(ps
->mgr_mutex
);
730 static RPC_STATUS
RPCRT4_start_listen_protseq(RpcServerProtseq
*ps
, BOOL auto_listen
)
732 RPC_STATUS status
= RPC_S_OK
;
734 EnterCriticalSection(&listen_cs
);
735 if (ps
->server_thread
) goto done
;
737 if (!ps
->mgr_mutex
) ps
->mgr_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
738 if (!ps
->server_ready_event
) ps
->server_ready_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
739 ps
->server_thread
= CreateThread(NULL
, 0, RPCRT4_server_thread
, ps
, 0, NULL
);
740 if (!ps
->server_thread
)
741 status
= RPC_S_OUT_OF_RESOURCES
;
744 LeaveCriticalSection(&listen_cs
);
748 static RPC_STATUS
RPCRT4_start_listen(BOOL auto_listen
)
750 RPC_STATUS status
= RPC_S_ALREADY_LISTENING
;
751 RpcServerProtseq
*cps
;
755 EnterCriticalSection(&listen_cs
);
756 if (auto_listen
|| !listen_done_event
)
760 listen_done_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
761 if (++listen_count
== 1)
764 LeaveCriticalSection(&listen_cs
);
765 if (status
) return status
;
769 EnterCriticalSection(&server_cs
);
770 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
772 status
= RPCRT4_start_listen_protseq(cps
, TRUE
);
773 if (status
!= RPC_S_OK
)
776 /* make sure server is actually listening on the interface before
778 RPCRT4_sync_with_server_thread(cps
);
780 LeaveCriticalSection(&server_cs
);
786 static RPC_STATUS
RPCRT4_stop_listen(BOOL auto_listen
)
788 BOOL stop_listen
= FALSE
;
789 RPC_STATUS status
= RPC_S_OK
;
791 EnterCriticalSection(&listen_cs
);
792 if (!std_listen
&& (auto_listen
|| !listen_done_event
))
794 status
= RPC_S_NOT_LISTENING
;
798 stop_listen
= listen_count
!= 0 && --listen_count
== 0;
799 assert(listen_count
>= 0);
803 LeaveCriticalSection(&listen_cs
);
805 if (status
) return status
;
808 RpcServerProtseq
*cps
;
809 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
810 RPCRT4_sync_with_server_thread(cps
);
815 EnterCriticalSection(&listen_cs
);
816 SetEvent( listen_done_event
);
817 LeaveCriticalSection(&listen_cs
);
822 static BOOL
RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq
*protseq
, const char *endpoint
)
825 BOOL registered
= FALSE
;
826 EnterCriticalSection(&protseq
->cs
);
827 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection
, protseq_entry
) {
828 if (!endpoint
|| !strcmp(endpoint
, conn
->Endpoint
)) {
833 LeaveCriticalSection(&protseq
->cs
);
837 static RPC_STATUS
RPCRT4_use_protseq(RpcServerProtseq
* ps
, const char *endpoint
)
841 EnterCriticalSection(&ps
->cs
);
843 if (RPCRT4_protseq_is_endpoint_registered(ps
, endpoint
))
846 status
= ps
->ops
->open_endpoint(ps
, endpoint
);
848 LeaveCriticalSection(&ps
->cs
);
850 if (status
!= RPC_S_OK
)
855 status
= RPCRT4_start_listen_protseq(ps
, FALSE
);
856 if (status
== RPC_S_OK
)
857 RPCRT4_sync_with_server_thread(ps
);
863 /***********************************************************************
864 * RpcServerInqBindings (RPCRT4.@)
866 RPC_STATUS WINAPI
RpcServerInqBindings( RPC_BINDING_VECTOR
** BindingVector
)
870 RpcServerProtseq
* ps
;
874 TRACE("(*BindingVector == ^%p)\n", *BindingVector
);
876 ERR("(BindingVector == NULL!!?)\n");
878 EnterCriticalSection(&server_cs
);
879 /* count connections */
881 LIST_FOR_EACH_ENTRY(ps
, &protseqs
, RpcServerProtseq
, entry
) {
882 EnterCriticalSection(&ps
->cs
);
883 LIST_FOR_EACH_ENTRY(conn
, &ps
->listeners
, RpcConnection
, protseq_entry
)
885 LeaveCriticalSection(&ps
->cs
);
888 /* export bindings */
889 *BindingVector
= HeapAlloc(GetProcessHeap(), 0,
890 sizeof(RPC_BINDING_VECTOR
) +
891 sizeof(RPC_BINDING_HANDLE
)*(count
-1));
892 (*BindingVector
)->Count
= count
;
894 LIST_FOR_EACH_ENTRY(ps
, &protseqs
, RpcServerProtseq
, entry
) {
895 EnterCriticalSection(&ps
->cs
);
896 LIST_FOR_EACH_ENTRY(conn
, &ps
->listeners
, RpcConnection
, protseq_entry
) {
897 RPCRT4_MakeBinding((RpcBinding
**)&(*BindingVector
)->BindingH
[count
],
901 LeaveCriticalSection(&ps
->cs
);
905 *BindingVector
= NULL
;
906 status
= RPC_S_NO_BINDINGS
;
908 LeaveCriticalSection(&server_cs
);
912 /***********************************************************************
913 * RpcServerUseProtseqEpA (RPCRT4.@)
915 RPC_STATUS WINAPI
RpcServerUseProtseqEpA( RPC_CSTR Protseq
, UINT MaxCalls
, RPC_CSTR Endpoint
, LPVOID SecurityDescriptor
)
919 TRACE( "(%s,%u,%s,%p)\n", Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
);
921 /* This should provide the default behaviour */
922 policy
.Length
= sizeof( policy
);
923 policy
.EndpointFlags
= 0;
926 return RpcServerUseProtseqEpExA( Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
, &policy
);
929 /***********************************************************************
930 * RpcServerUseProtseqEpW (RPCRT4.@)
932 RPC_STATUS WINAPI
RpcServerUseProtseqEpW( RPC_WSTR Protseq
, UINT MaxCalls
, RPC_WSTR Endpoint
, LPVOID SecurityDescriptor
)
936 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq
), MaxCalls
, debugstr_w( Endpoint
), SecurityDescriptor
);
938 /* This should provide the default behaviour */
939 policy
.Length
= sizeof( policy
);
940 policy
.EndpointFlags
= 0;
943 return RpcServerUseProtseqEpExW( Protseq
, MaxCalls
, Endpoint
, SecurityDescriptor
, &policy
);
946 /***********************************************************************
947 * alloc_serverprotoseq (internal)
949 * Must be called with server_cs held.
951 static RPC_STATUS
alloc_serverprotoseq(UINT MaxCalls
, const char *Protseq
, RpcServerProtseq
**ps
)
953 const struct protseq_ops
*ops
= rpcrt4_get_protseq_ops(Protseq
);
957 FIXME("protseq %s not supported\n", debugstr_a(Protseq
));
958 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
963 return RPC_S_OUT_OF_RESOURCES
;
964 (*ps
)->MaxCalls
= MaxCalls
;
965 (*ps
)->Protseq
= RPCRT4_strdupA(Protseq
);
967 list_init(&(*ps
)->listeners
);
968 list_init(&(*ps
)->connections
);
969 InitializeCriticalSection(&(*ps
)->cs
);
970 (*ps
)->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcServerProtseq.cs");
972 list_add_head(&protseqs
, &(*ps
)->entry
);
974 TRACE("new protseq %p created for %s\n", *ps
, Protseq
);
979 /* must be called with server_cs held */
980 static void destroy_serverprotoseq(RpcServerProtseq
*ps
)
982 RPCRT4_strfree(ps
->Protseq
);
983 ps
->cs
.DebugInfo
->Spare
[0] = 0;
984 DeleteCriticalSection(&ps
->cs
);
985 CloseHandle(ps
->mgr_mutex
);
986 CloseHandle(ps
->server_ready_event
);
987 list_remove(&ps
->entry
);
988 HeapFree(GetProcessHeap(), 0, ps
);
991 /* Finds a given protseq or creates a new one if one doesn't already exist */
992 static RPC_STATUS
RPCRT4_get_or_create_serverprotseq(UINT MaxCalls
, const char *Protseq
, RpcServerProtseq
**ps
)
995 RpcServerProtseq
*cps
;
997 EnterCriticalSection(&server_cs
);
999 LIST_FOR_EACH_ENTRY(cps
, &protseqs
, RpcServerProtseq
, entry
)
1000 if (!strcmp(cps
->Protseq
, Protseq
))
1002 TRACE("found existing protseq object for %s\n", Protseq
);
1004 LeaveCriticalSection(&server_cs
);
1008 status
= alloc_serverprotoseq(MaxCalls
, Protseq
, ps
);
1010 LeaveCriticalSection(&server_cs
);
1015 /***********************************************************************
1016 * RpcServerUseProtseqEpExA (RPCRT4.@)
1018 RPC_STATUS WINAPI
RpcServerUseProtseqEpExA( RPC_CSTR Protseq
, UINT MaxCalls
, RPC_CSTR Endpoint
, LPVOID SecurityDescriptor
,
1019 PRPC_POLICY lpPolicy
)
1021 RpcServerProtseq
* ps
;
1024 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_a((const char *)Protseq
),
1025 MaxCalls
, debugstr_a((const char *)Endpoint
), SecurityDescriptor
,
1026 lpPolicy
->Length
, lpPolicy
->EndpointFlags
, lpPolicy
->NICFlags
);
1028 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, (const char *)Protseq
, &ps
);
1029 if (status
!= RPC_S_OK
)
1032 return RPCRT4_use_protseq(ps
, (const char *)Endpoint
);
1035 /***********************************************************************
1036 * RpcServerUseProtseqEpExW (RPCRT4.@)
1038 RPC_STATUS WINAPI
RpcServerUseProtseqEpExW( RPC_WSTR Protseq
, UINT MaxCalls
, RPC_WSTR Endpoint
, LPVOID SecurityDescriptor
,
1039 PRPC_POLICY lpPolicy
)
1041 RpcServerProtseq
* ps
;
1046 TRACE("(%s,%u,%s,%p,{%u,%u,%u})\n", debugstr_w( Protseq
), MaxCalls
,
1047 debugstr_w( Endpoint
), SecurityDescriptor
,
1048 lpPolicy
->Length
, lpPolicy
->EndpointFlags
, lpPolicy
->NICFlags
);
1050 ProtseqA
= RPCRT4_strdupWtoA(Protseq
);
1051 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, ProtseqA
, &ps
);
1052 RPCRT4_strfree(ProtseqA
);
1053 if (status
!= RPC_S_OK
)
1056 EndpointA
= RPCRT4_strdupWtoA(Endpoint
);
1057 status
= RPCRT4_use_protseq(ps
, EndpointA
);
1058 RPCRT4_strfree(EndpointA
);
1062 /***********************************************************************
1063 * RpcServerUseProtseqA (RPCRT4.@)
1065 RPC_STATUS WINAPI
RpcServerUseProtseqA(RPC_CSTR Protseq
, unsigned int MaxCalls
, void *SecurityDescriptor
)
1068 RpcServerProtseq
* ps
;
1070 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq
), MaxCalls
, SecurityDescriptor
);
1072 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, (const char *)Protseq
, &ps
);
1073 if (status
!= RPC_S_OK
)
1076 return RPCRT4_use_protseq(ps
, NULL
);
1079 /***********************************************************************
1080 * RpcServerUseProtseqW (RPCRT4.@)
1082 RPC_STATUS WINAPI
RpcServerUseProtseqW(RPC_WSTR Protseq
, unsigned int MaxCalls
, void *SecurityDescriptor
)
1085 RpcServerProtseq
* ps
;
1088 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq
), MaxCalls
, SecurityDescriptor
);
1090 ProtseqA
= RPCRT4_strdupWtoA(Protseq
);
1091 status
= RPCRT4_get_or_create_serverprotseq(MaxCalls
, ProtseqA
, &ps
);
1092 RPCRT4_strfree(ProtseqA
);
1093 if (status
!= RPC_S_OK
)
1096 return RPCRT4_use_protseq(ps
, NULL
);
1099 void RPCRT4_destroy_all_protseqs(void)
1101 RpcServerProtseq
*cps
, *cursor2
;
1103 if (listen_count
!= 0)
1106 EnterCriticalSection(&server_cs
);
1107 LIST_FOR_EACH_ENTRY_SAFE(cps
, cursor2
, &protseqs
, RpcServerProtseq
, entry
)
1109 if (listen_count
!= 0)
1110 RPCRT4_sync_with_server_thread(cps
);
1111 destroy_serverprotoseq(cps
);
1113 LeaveCriticalSection(&server_cs
);
1114 DeleteCriticalSection(&server_cs
);
1115 DeleteCriticalSection(&listen_cs
);
1118 /***********************************************************************
1119 * RpcServerRegisterIf (RPCRT4.@)
1121 RPC_STATUS WINAPI
RpcServerRegisterIf( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
)
1123 TRACE("(%p,%s,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
);
1124 return RpcServerRegisterIf3( IfSpec
, MgrTypeUuid
, MgrEpv
, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT
, (UINT
)-1, NULL
, NULL
);
1127 /***********************************************************************
1128 * RpcServerRegisterIfEx (RPCRT4.@)
1130 RPC_STATUS WINAPI
RpcServerRegisterIfEx( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1131 UINT Flags
, UINT MaxCalls
, RPC_IF_CALLBACK_FN
* IfCallbackFn
)
1133 TRACE("(%p,%s,%p,%u,%u,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
, Flags
, MaxCalls
, IfCallbackFn
);
1134 return RpcServerRegisterIf3( IfSpec
, MgrTypeUuid
, MgrEpv
, Flags
, MaxCalls
, (UINT
)-1, IfCallbackFn
, NULL
);
1137 /***********************************************************************
1138 * RpcServerRegisterIf2 (RPCRT4.@)
1140 RPC_STATUS WINAPI
RpcServerRegisterIf2( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1141 UINT Flags
, UINT MaxCalls
, UINT MaxRpcSize
, RPC_IF_CALLBACK_FN
* IfCallbackFn
)
1143 return RpcServerRegisterIf3( IfSpec
, MgrTypeUuid
, MgrEpv
, Flags
, MaxCalls
, MaxRpcSize
, IfCallbackFn
, NULL
);
1146 /***********************************************************************
1147 * RpcServerRegisterIf3 (RPCRT4.@)
1149 RPC_STATUS WINAPI
RpcServerRegisterIf3( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, RPC_MGR_EPV
* MgrEpv
,
1150 UINT Flags
, UINT MaxCalls
, UINT MaxRpcSize
, RPC_IF_CALLBACK_FN
* IfCallbackFn
, void* SecurityDescriptor
)
1152 PRPC_SERVER_INTERFACE If
= IfSpec
;
1153 RpcServerInterface
* sif
;
1156 TRACE("(%p,%s,%p,%u,%u,%u,%p,%p)\n", IfSpec
, debugstr_guid(MgrTypeUuid
), MgrEpv
, Flags
, MaxCalls
,
1157 MaxRpcSize
, IfCallbackFn
, SecurityDescriptor
);
1159 if (SecurityDescriptor
)
1160 FIXME("Unsupported SecurityDescriptor argument.\n");
1162 TRACE(" interface id: %s %d.%d\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
),
1163 If
->InterfaceId
.SyntaxVersion
.MajorVersion
,
1164 If
->InterfaceId
.SyntaxVersion
.MinorVersion
);
1165 TRACE(" transfer syntax: %s %d.%d\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
),
1166 If
->TransferSyntax
.SyntaxVersion
.MajorVersion
,
1167 If
->TransferSyntax
.SyntaxVersion
.MinorVersion
);
1168 TRACE(" dispatch table: %p\n", If
->DispatchTable
);
1169 if (If
->DispatchTable
) {
1170 TRACE(" dispatch table count: %d\n", If
->DispatchTable
->DispatchTableCount
);
1171 for (i
=0; i
<If
->DispatchTable
->DispatchTableCount
; i
++) {
1172 TRACE(" entry %d: %p\n", i
, If
->DispatchTable
->DispatchTable
[i
]);
1174 TRACE(" reserved: %ld\n", If
->DispatchTable
->Reserved
);
1176 TRACE(" protseq endpoint count: %d\n", If
->RpcProtseqEndpointCount
);
1177 TRACE(" default manager epv: %p\n", If
->DefaultManagerEpv
);
1178 TRACE(" interpreter info: %p\n", If
->InterpreterInfo
);
1180 sif
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcServerInterface
));
1183 sif
->MgrTypeUuid
= *MgrTypeUuid
;
1184 sif
->MgrEpv
= MgrEpv
;
1186 memset(&sif
->MgrTypeUuid
, 0, sizeof(UUID
));
1187 sif
->MgrEpv
= If
->DefaultManagerEpv
;
1190 sif
->MaxCalls
= MaxCalls
;
1191 sif
->MaxRpcSize
= MaxRpcSize
;
1192 sif
->IfCallbackFn
= IfCallbackFn
;
1194 EnterCriticalSection(&server_cs
);
1195 list_add_head(&server_interfaces
, &sif
->entry
);
1196 LeaveCriticalSection(&server_cs
);
1198 if (sif
->Flags
& RPC_IF_AUTOLISTEN
)
1199 RPCRT4_start_listen(TRUE
);
1204 /***********************************************************************
1205 * RpcServerUnregisterIf (RPCRT4.@)
1207 RPC_STATUS WINAPI
RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, UINT WaitForCallsToComplete
)
1209 PRPC_SERVER_INTERFACE If
= IfSpec
;
1210 HANDLE event
= NULL
;
1212 BOOL completed
= TRUE
;
1213 RpcServerInterface
*cif
;
1216 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1217 IfSpec
, debugstr_guid(&If
->InterfaceId
.SyntaxGUID
), debugstr_guid(MgrTypeUuid
), WaitForCallsToComplete
);
1219 EnterCriticalSection(&server_cs
);
1220 LIST_FOR_EACH_ENTRY(cif
, &server_interfaces
, RpcServerInterface
, entry
) {
1221 if ((!IfSpec
|| !memcmp(&If
->InterfaceId
, &cif
->If
->InterfaceId
, sizeof(RPC_SYNTAX_IDENTIFIER
))) &&
1222 UuidEqual(MgrTypeUuid
, &cif
->MgrTypeUuid
, &status
)) {
1223 list_remove(&cif
->entry
);
1224 TRACE("unregistering cif %p\n", cif
);
1225 if (cif
->CurrentCalls
) {
1228 if (WaitForCallsToComplete
)
1229 cif
->CallsCompletedEvent
= event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1235 LeaveCriticalSection(&server_cs
);
1238 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid
));
1239 return RPC_S_UNKNOWN_IF
;
1243 HeapFree(GetProcessHeap(), 0, cif
);
1245 /* sif will be freed when the last call is completed, so be careful not to
1246 * touch that memory here as that could happen before we get here */
1247 WaitForSingleObject(event
, INFINITE
);
1254 /***********************************************************************
1255 * RpcServerUnregisterIfEx (RPCRT4.@)
1257 RPC_STATUS WINAPI
RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec
, UUID
* MgrTypeUuid
, int RundownContextHandles
)
1259 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1260 IfSpec
, debugstr_guid(MgrTypeUuid
), RundownContextHandles
);
1265 /***********************************************************************
1266 * RpcObjectSetType (RPCRT4.@)
1269 * ObjUuid [I] "Object" UUID
1270 * TypeUuid [I] "Type" UUID
1273 * RPC_S_OK The call succeeded
1274 * RPC_S_INVALID_OBJECT The provided object (nil) is not valid
1275 * RPC_S_ALREADY_REGISTERED The provided object is already registered
1277 * Maps "Object" UUIDs to "Type" UUIDs. Passing the nil UUID as the type
1278 * resets the mapping for the specified object UUID to nil (the default).
1279 * The nil object is always associated with the nil type and cannot be
1280 * reassigned. Servers can support multiple implementations on the same
1281 * interface by registering different end-point vectors for the different
1282 * types. There's no need to call this if a server only supports the nil
1283 * type, as is typical.
1285 RPC_STATUS WINAPI
RpcObjectSetType( UUID
* ObjUuid
, UUID
* TypeUuid
)
1287 RpcObjTypeMap
*map
= RpcObjTypeMaps
, *prev
= NULL
;
1290 TRACE("(ObjUUID == %s, TypeUuid == %s).\n", debugstr_guid(ObjUuid
), debugstr_guid(TypeUuid
));
1291 if ((! ObjUuid
) || UuidIsNil(ObjUuid
, &dummy
)) {
1292 /* nil uuid cannot be remapped */
1293 return RPC_S_INVALID_OBJECT
;
1296 /* find the mapping for this object if there is one ... */
1298 if (! UuidCompare(ObjUuid
, &map
->Object
, &dummy
)) break;
1302 if ((! TypeUuid
) || UuidIsNil(TypeUuid
, &dummy
)) {
1303 /* ... and drop it from the list */
1306 prev
->next
= map
->next
;
1308 RpcObjTypeMaps
= map
->next
;
1309 HeapFree(GetProcessHeap(), 0, map
);
1312 /* ... , fail if we found it ... */
1314 return RPC_S_ALREADY_REGISTERED
;
1315 /* ... otherwise create a new one and add it in. */
1316 map
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcObjTypeMap
));
1317 map
->Object
= *ObjUuid
;
1318 map
->Type
= *TypeUuid
;
1321 prev
->next
= map
; /* prev is the last map in the linklist */
1323 RpcObjTypeMaps
= map
;
1329 struct rpc_server_registered_auth_info
1333 WCHAR
*package_name
;
1338 static RPC_STATUS
find_security_package(ULONG auth_type
, SecPkgInfoW
**packages_buf
, SecPkgInfoW
**ret
)
1340 SECURITY_STATUS sec_status
;
1341 SecPkgInfoW
*packages
;
1342 ULONG package_count
;
1345 sec_status
= EnumerateSecurityPackagesW(&package_count
, &packages
);
1346 if (sec_status
!= SEC_E_OK
)
1348 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", sec_status
);
1349 return RPC_S_SEC_PKG_ERROR
;
1352 for (i
= 0; i
< package_count
; i
++)
1353 if (packages
[i
].wRPCID
== auth_type
)
1356 if (i
== package_count
)
1358 WARN("unsupported AuthnSvc %u\n", auth_type
);
1359 FreeContextBuffer(packages
);
1360 return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1363 TRACE("found package %s for service %u\n", debugstr_w(packages
[i
].Name
), auth_type
);
1364 *packages_buf
= packages
;
1365 *ret
= packages
+ i
;
1369 RPC_STATUS
RPCRT4_ServerGetRegisteredAuthInfo(
1370 USHORT auth_type
, CredHandle
*cred
, TimeStamp
*exp
, ULONG
*max_token
)
1372 RPC_STATUS status
= RPC_S_UNKNOWN_AUTHN_SERVICE
;
1373 struct rpc_server_registered_auth_info
*auth_info
;
1374 SECURITY_STATUS sec_status
;
1376 EnterCriticalSection(&server_auth_info_cs
);
1377 LIST_FOR_EACH_ENTRY(auth_info
, &server_registered_auth_info
, struct rpc_server_registered_auth_info
, entry
)
1379 if (auth_info
->auth_type
== auth_type
)
1381 sec_status
= AcquireCredentialsHandleW((SEC_WCHAR
*)auth_info
->principal
, auth_info
->package_name
,
1382 SECPKG_CRED_INBOUND
, NULL
, NULL
, NULL
, NULL
,
1384 if (sec_status
!= SEC_E_OK
)
1386 status
= RPC_S_SEC_PKG_ERROR
;
1390 *max_token
= auth_info
->max_token
;
1395 LeaveCriticalSection(&server_auth_info_cs
);
1400 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1402 struct rpc_server_registered_auth_info
*auth_info
, *cursor2
;
1404 EnterCriticalSection(&server_auth_info_cs
);
1405 LIST_FOR_EACH_ENTRY_SAFE(auth_info
, cursor2
, &server_registered_auth_info
, struct rpc_server_registered_auth_info
, entry
)
1407 HeapFree(GetProcessHeap(), 0, auth_info
->package_name
);
1408 HeapFree(GetProcessHeap(), 0, auth_info
->principal
);
1409 HeapFree(GetProcessHeap(), 0, auth_info
);
1411 LeaveCriticalSection(&server_auth_info_cs
);
1412 DeleteCriticalSection(&server_auth_info_cs
);
1415 /***********************************************************************
1416 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1418 RPC_STATUS WINAPI
RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName
, ULONG AuthnSvc
, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn
,
1421 WCHAR
*principal_name
= NULL
;
1424 TRACE("(%s,%u,%p,%p)\n", ServerPrincName
, AuthnSvc
, GetKeyFn
, Arg
);
1426 if(ServerPrincName
&& !(principal_name
= RPCRT4_strdupAtoW((const char*)ServerPrincName
)))
1427 return RPC_S_OUT_OF_RESOURCES
;
1429 status
= RpcServerRegisterAuthInfoW(principal_name
, AuthnSvc
, GetKeyFn
, Arg
);
1431 HeapFree(GetProcessHeap(), 0, principal_name
);
1435 /***********************************************************************
1436 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1438 RPC_STATUS WINAPI
RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName
, ULONG AuthnSvc
, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn
,
1441 struct rpc_server_registered_auth_info
*auth_info
;
1442 SecPkgInfoW
*packages
, *package
;
1443 WCHAR
*package_name
;
1447 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName
), AuthnSvc
, GetKeyFn
, Arg
);
1449 status
= find_security_package(AuthnSvc
, &packages
, &package
);
1450 if (status
!= RPC_S_OK
)
1453 package_name
= RPCRT4_strdupW(package
->Name
);
1454 max_token
= package
->cbMaxToken
;
1455 FreeContextBuffer(packages
);
1457 return RPC_S_OUT_OF_RESOURCES
;
1459 auth_info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*auth_info
));
1461 HeapFree(GetProcessHeap(), 0, package_name
);
1462 return RPC_S_OUT_OF_RESOURCES
;
1465 if (ServerPrincName
&& !(auth_info
->principal
= RPCRT4_strdupW(ServerPrincName
))) {
1466 HeapFree(GetProcessHeap(), 0, package_name
);
1467 HeapFree(GetProcessHeap(), 0, auth_info
);
1468 return RPC_S_OUT_OF_RESOURCES
;
1471 auth_info
->auth_type
= AuthnSvc
;
1472 auth_info
->package_name
= package_name
;
1473 auth_info
->max_token
= max_token
;
1475 EnterCriticalSection(&server_auth_info_cs
);
1476 list_add_tail(&server_registered_auth_info
, &auth_info
->entry
);
1477 LeaveCriticalSection(&server_auth_info_cs
);
1482 /******************************************************************************
1483 * RpcServerInqDefaultPrincNameA (rpcrt4.@)
1485 RPC_STATUS RPC_ENTRY
RpcServerInqDefaultPrincNameA(ULONG AuthnSvc
, RPC_CSTR
*PrincName
)
1488 RPC_WSTR principalW
;
1490 TRACE("%u, %p\n", AuthnSvc
, PrincName
);
1492 if ((ret
= RpcServerInqDefaultPrincNameW( AuthnSvc
, &principalW
)) == RPC_S_OK
)
1494 if (!(*PrincName
= (RPC_CSTR
)RPCRT4_strdupWtoA( principalW
))) return RPC_S_OUT_OF_MEMORY
;
1495 RpcStringFreeW( &principalW
);
1500 /******************************************************************************
1501 * RpcServerInqDefaultPrincNameW (rpcrt4.@)
1503 RPC_STATUS RPC_ENTRY
RpcServerInqDefaultPrincNameW(ULONG AuthnSvc
, RPC_WSTR
*PrincName
)
1507 FIXME("%u, %p\n", AuthnSvc
, PrincName
);
1509 if (AuthnSvc
!= RPC_C_AUTHN_WINNT
) return RPC_S_UNKNOWN_AUTHN_SERVICE
;
1511 GetUserNameExW( NameSamCompatible
, NULL
, &len
);
1512 if (GetLastError() != ERROR_MORE_DATA
) return RPC_S_INTERNAL_ERROR
;
1514 if (!(*PrincName
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1515 return RPC_S_OUT_OF_MEMORY
;
1517 GetUserNameExW( NameSamCompatible
, *PrincName
, &len
);
1521 /***********************************************************************
1522 * RpcServerListen (RPCRT4.@)
1524 RPC_STATUS WINAPI
RpcServerListen( UINT MinimumCallThreads
, UINT MaxCalls
, UINT DontWait
)
1526 RPC_STATUS status
= RPC_S_OK
;
1528 TRACE("(%u,%u,%u)\n", MinimumCallThreads
, MaxCalls
, DontWait
);
1530 if (list_empty(&protseqs
))
1531 return RPC_S_NO_PROTSEQS_REGISTERED
;
1533 status
= RPCRT4_start_listen(FALSE
);
1535 if (DontWait
|| (status
!= RPC_S_OK
)) return status
;
1537 return RpcMgmtWaitServerListen();
1540 /***********************************************************************
1541 * RpcMgmtServerWaitListen (RPCRT4.@)
1543 RPC_STATUS WINAPI
RpcMgmtWaitServerListen( void )
1545 RpcServerProtseq
*protseq
;
1546 HANDLE event
, wait_thread
;
1550 EnterCriticalSection(&listen_cs
);
1551 event
= listen_done_event
;
1552 LeaveCriticalSection(&listen_cs
);
1555 return RPC_S_NOT_LISTENING
;
1557 TRACE( "waiting for server calls to finish\n" );
1558 WaitForSingleObject( event
, INFINITE
);
1559 TRACE( "done waiting\n" );
1561 EnterCriticalSection(&listen_cs
);
1562 /* wait for server threads to finish */
1569 EnterCriticalSection(&server_cs
);
1570 LIST_FOR_EACH_ENTRY(protseq
, &protseqs
, RpcServerProtseq
, entry
)
1572 if ((wait_thread
= protseq
->server_thread
))
1575 LeaveCriticalSection(&server_cs
);
1579 TRACE("waiting for thread %u\n", GetThreadId(wait_thread
));
1580 LeaveCriticalSection(&listen_cs
);
1581 WaitForSingleObject(wait_thread
, INFINITE
);
1582 EnterCriticalSection(&listen_cs
);
1584 if (listen_done_event
== event
)
1586 listen_done_event
= NULL
;
1587 CloseHandle( event
);
1589 LeaveCriticalSection(&listen_cs
);
1593 /***********************************************************************
1594 * RpcMgmtStopServerListening (RPCRT4.@)
1596 RPC_STATUS WINAPI
RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding
)
1598 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding
);
1601 FIXME("client-side invocation not implemented.\n");
1602 return RPC_S_WRONG_KIND_OF_BINDING
;
1605 return RPCRT4_stop_listen(FALSE
);
1608 /***********************************************************************
1609 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1611 RPC_STATUS WINAPI
RpcMgmtEnableIdleCleanup(void)
1613 FIXME("(): stub\n");
1617 /***********************************************************************
1618 * I_RpcServerStartListening (RPCRT4.@)
1620 RPC_STATUS WINAPI
I_RpcServerStartListening( HWND hWnd
)
1622 FIXME( "(%p): stub\n", hWnd
);
1627 /***********************************************************************
1628 * I_RpcServerStopListening (RPCRT4.@)
1630 RPC_STATUS WINAPI
I_RpcServerStopListening( void )
1632 FIXME( "(): stub\n" );
1637 /***********************************************************************
1638 * I_RpcWindowProc (RPCRT4.@)
1640 UINT WINAPI
I_RpcWindowProc( void *hWnd
, UINT Message
, UINT wParam
, ULONG lParam
)
1642 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd
, Message
, wParam
, lParam
);
1647 /***********************************************************************
1648 * RpcMgmtInqIfIds (RPCRT4.@)
1650 RPC_STATUS WINAPI
RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding
, RPC_IF_ID_VECTOR
**IfIdVector
)
1652 FIXME("(%p,%p): stub\n", Binding
, IfIdVector
);
1653 return RPC_S_INVALID_BINDING
;
1656 /***********************************************************************
1657 * RpcMgmtInqStats (RPCRT4.@)
1659 RPC_STATUS WINAPI
RpcMgmtInqStats(RPC_BINDING_HANDLE Binding
, RPC_STATS_VECTOR
**Statistics
)
1661 RPC_STATS_VECTOR
*stats
;
1663 FIXME("(%p,%p)\n", Binding
, Statistics
);
1665 if ((stats
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR
))))
1668 stats
->Stats
[0] = 0;
1669 *Statistics
= stats
;
1672 return RPC_S_OUT_OF_RESOURCES
;
1675 /***********************************************************************
1676 * RpcMgmtStatsVectorFree (RPCRT4.@)
1678 RPC_STATUS WINAPI
RpcMgmtStatsVectorFree(RPC_STATS_VECTOR
**StatsVector
)
1680 FIXME("(%p)\n", StatsVector
);
1684 HeapFree(GetProcessHeap(), 0, *StatsVector
);
1685 *StatsVector
= NULL
;
1690 /***********************************************************************
1691 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1693 RPC_STATUS WINAPI
RpcMgmtEpEltInqBegin(RPC_BINDING_HANDLE Binding
, ULONG InquiryType
,
1694 RPC_IF_ID
*IfId
, ULONG VersOption
, UUID
*ObjectUuid
, RPC_EP_INQ_HANDLE
* InquiryContext
)
1696 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1697 Binding
, InquiryType
, IfId
, VersOption
, ObjectUuid
, InquiryContext
);
1698 return RPC_S_INVALID_BINDING
;
1701 /***********************************************************************
1702 * RpcMgmtIsServerListening (RPCRT4.@)
1704 RPC_STATUS WINAPI
RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding
)
1706 RPC_STATUS status
= RPC_S_NOT_LISTENING
;
1708 TRACE("(%p)\n", Binding
);
1711 RpcBinding
*rpc_binding
= (RpcBinding
*)Binding
;
1712 status
= RPCRT4_IsServerListening(rpc_binding
->Protseq
, rpc_binding
->Endpoint
);
1714 EnterCriticalSection(&listen_cs
);
1715 if (listen_done_event
&& std_listen
) status
= RPC_S_OK
;
1716 LeaveCriticalSection(&listen_cs
);
1722 /***********************************************************************
1723 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1725 RPC_STATUS WINAPI
RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn
)
1727 FIXME("(%p): stub\n", fn
);
1731 /***********************************************************************
1732 * RpcMgmtSetServerStackSize (RPCRT4.@)
1734 RPC_STATUS WINAPI
RpcMgmtSetServerStackSize(ULONG ThreadStackSize
)
1736 FIXME("(0x%x): stub\n", ThreadStackSize
);
1740 /***********************************************************************
1741 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1743 RPC_BINDING_HANDLE WINAPI
I_RpcGetCurrentCallHandle(void)
1746 return RPCRT4_GetThreadCurrentCallHandle();