436a014ea0176f9d838eeaebb538b522cb0a2127
[reactos.git] / reactos / dll / win32 / rpcrt4 / rpc_server.c
1 /*
2 * RPC server API
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2004 Filip Navara
6 * Copyright 2006-2008 Robert Shearman (for CodeWeavers)
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "precomp.h"
24
25 #include <secext.h>
26
27 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
28
29 typedef struct _RpcPacket
30 {
31 struct _RpcConnection* conn;
32 RpcPktHdr* hdr;
33 RPC_MESSAGE* msg;
34 unsigned char *auth_data;
35 ULONG auth_length;
36 } RpcPacket;
37
38 typedef struct _RpcObjTypeMap
39 {
40 /* FIXME: a hash table would be better. */
41 struct _RpcObjTypeMap *next;
42 UUID Object;
43 UUID Type;
44 } RpcObjTypeMap;
45
46 static RpcObjTypeMap *RpcObjTypeMaps;
47
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);
52
53 static CRITICAL_SECTION server_cs;
54 static CRITICAL_SECTION_DEBUG server_cs_debug =
55 {
56 0, 0, &server_cs,
57 { &server_cs_debug.ProcessLocksList, &server_cs_debug.ProcessLocksList },
58 0, 0, { (DWORD_PTR)(__FILE__ ": server_cs") }
59 };
60 static CRITICAL_SECTION server_cs = { &server_cs_debug, -1, 0, 0, 0, 0 };
61
62 static CRITICAL_SECTION listen_cs;
63 static CRITICAL_SECTION_DEBUG listen_cs_debug =
64 {
65 0, 0, &listen_cs,
66 { &listen_cs_debug.ProcessLocksList, &listen_cs_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": listen_cs") }
68 };
69 static CRITICAL_SECTION listen_cs = { &listen_cs_debug, -1, 0, 0, 0, 0 };
70
71 static CRITICAL_SECTION server_auth_info_cs;
72 static CRITICAL_SECTION_DEBUG server_auth_info_cs_debug =
73 {
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") }
77 };
78 static CRITICAL_SECTION server_auth_info_cs = { &server_auth_info_cs_debug, -1, 0, 0, 0, 0 };
79
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;
88
89 static UUID uuid_nil;
90
91 static inline RpcObjTypeMap *LookupObjTypeMap(UUID *ObjUuid)
92 {
93 RpcObjTypeMap *rslt = RpcObjTypeMaps;
94 RPC_STATUS dummy;
95
96 while (rslt) {
97 if (! UuidCompare(ObjUuid, &rslt->Object, &dummy)) break;
98 rslt = rslt->next;
99 }
100
101 return rslt;
102 }
103
104 static inline UUID *LookupObjType(UUID *ObjUuid)
105 {
106 RpcObjTypeMap *map = LookupObjTypeMap(ObjUuid);
107 if (map)
108 return &map->Type;
109 else
110 return &uuid_nil;
111 }
112
113 static RpcServerInterface* RPCRT4_find_interface(UUID* object,
114 const RPC_SYNTAX_IDENTIFIER *if_id,
115 const RPC_SYNTAX_IDENTIFIER *transfer_syntax,
116 BOOL check_object)
117 {
118 UUID* MgrType = NULL;
119 RpcServerInterface* cif;
120 RPC_STATUS status;
121
122 if (check_object)
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)) &&
129 std_listen) {
130 InterlockedIncrement(&cif->CurrentCalls);
131 break;
132 }
133 }
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));
139 return cif;
140 }
141
142 static void RPCRT4_release_server_interface(RpcServerInterface *sif)
143 {
144 if (!InterlockedDecrement(&sif->CurrentCalls) &&
145 sif->Delete) {
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);
151 }
152 }
153
154 static RpcPktHdr *handle_bind_error(RpcConnection *conn, RPC_STATUS error)
155 {
156 unsigned int reject_reason;
157 switch (error)
158 {
159 case RPC_S_SERVER_TOO_BUSY:
160 reject_reason = REJECT_TEMPORARY_CONGESTION;
161 break;
162 case ERROR_OUTOFMEMORY:
163 case RPC_S_OUT_OF_RESOURCES:
164 reject_reason = REJECT_LOCAL_LIMIT_EXCEEDED;
165 break;
166 case RPC_S_PROTOCOL_ERROR:
167 reject_reason = REJECT_PROTOCOL_VERSION_NOT_SUPPORTED;
168 break;
169 case RPC_S_UNKNOWN_AUTHN_SERVICE:
170 reject_reason = REJECT_UNKNOWN_AUTHN_SERVICE;
171 break;
172 case ERROR_ACCESS_DENIED:
173 reject_reason = REJECT_INVALID_CHECKSUM;
174 break;
175 default:
176 FIXME("unexpected status value %d\n", error);
177 /* fall through */
178 case RPC_S_INVALID_BOUND:
179 reject_reason = REJECT_REASON_NOT_SPECIFIED;
180 break;
181 }
182 return RPCRT4_BuildBindNackHeader(NDR_LOCAL_DATA_REPRESENTATION,
183 RPC_VER_MAJOR, RPC_VER_MINOR,
184 reject_reason);
185 }
186
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)
191 {
192 RPC_STATUS status;
193 RpcContextElement *ctxt_elem;
194 unsigned int i;
195 RpcResult *results;
196
197 /* validate data */
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])
201 {
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)
204 {
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;
208 }
209 }
210
211 if (hdr->max_tsize < RPC_MIN_PACKET_SIZE ||
212 !UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
213 conn->server_binding)
214 {
215 TRACE("packet size less than min size, or active interface syntax guid non-null\n");
216
217 return RPC_S_INVALID_BOUND;
218 }
219
220 results = HeapAlloc(GetProcessHeap(), 0,
221 hdr->num_elements * sizeof(*results));
222 if (!results)
223 return RPC_S_OUT_OF_RESOURCES;
224
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])
228 {
229 RpcServerInterface* sif = NULL;
230 unsigned int j;
231
232 for (j = 0; !sif && j < ctxt_elem->num_syntaxes; j++)
233 {
234 sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
235 &ctxt_elem->transfer_syntaxes[j], FALSE);
236 if (sif)
237 break;
238 }
239 if (sif)
240 {
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];
247
248 /* save the interface for later use */
249 /* FIXME: save linked list */
250 conn->ActiveInterface = ctxt_elem->abstract_syntax;
251 }
252 else if ((sif = RPCRT4_find_interface(NULL, &ctxt_elem->abstract_syntax,
253 NULL, FALSE)) != NULL)
254 {
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));
261 }
262 else
263 {
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));
269 }
270 }
271
272 /* create temporary binding */
273 status = RPCRT4_MakeBinding(&conn->server_binding, conn);
274 if (status != RPC_S_OK)
275 {
276 HeapFree(GetProcessHeap(), 0, results);
277 return status;
278 }
279
280 status = RpcServerAssoc_GetAssociation(rpcrt4_conn_get_name(conn),
281 conn->NetworkAddr, conn->Endpoint,
282 conn->NetworkOptions,
283 hdr->assoc_gid,
284 &conn->server_binding->Assoc);
285 if (status != RPC_S_OK)
286 {
287 HeapFree(GetProcessHeap(), 0, results);
288 return status;
289 }
290
291 if (auth_length)
292 {
293 status = RPCRT4_ServerConnectionAuth(conn, TRUE,
294 (RpcAuthVerifier *)auth_data,
295 auth_length, auth_data_out,
296 auth_length_out);
297 if (status != RPC_S_OK)
298 {
299 HeapFree(GetProcessHeap(), 0, results);
300 return status;
301 }
302 }
303
304 *ack_response = RPCRT4_BuildBindAckHeader(NDR_LOCAL_DATA_REPRESENTATION,
305 RPC_MAX_PACKET_SIZE,
306 RPC_MAX_PACKET_SIZE,
307 conn->server_binding->Assoc->assoc_group_id,
308 conn->Endpoint, hdr->num_elements,
309 results);
310 HeapFree(GetProcessHeap(), 0, results);
311
312 if (*ack_response)
313 conn->MaxTransmissionSize = hdr->max_tsize;
314 else
315 status = RPC_S_OUT_OF_RESOURCES;
316
317 return status;
318 }
319
320 static RPC_STATUS process_bind_packet(RpcConnection *conn, RpcPktBindHdr *hdr,
321 RPC_MESSAGE *msg,
322 unsigned char *auth_data,
323 ULONG auth_length)
324 {
325 RPC_STATUS status;
326 RpcPktHdr *response = NULL;
327 unsigned char *auth_data_out = NULL;
328 ULONG auth_length_out = 0;
329
330 status = process_bind_packet_no_send(conn, hdr, msg, auth_data, auth_length,
331 &response, &auth_data_out,
332 &auth_length_out);
333 if (status != RPC_S_OK)
334 response = handle_bind_error(conn, status);
335 if (response)
336 status = RPCRT4_SendWithAuth(conn, response, NULL, 0, auth_data_out, auth_length_out);
337 else
338 status = ERROR_OUTOFMEMORY;
339 RPCRT4_FreeHeader(response);
340
341 return status;
342 }
343
344
345 static RPC_STATUS process_request_packet(RpcConnection *conn, RpcPktRequestHdr *hdr, RPC_MESSAGE *msg)
346 {
347 RPC_STATUS status;
348 RpcPktHdr *response = NULL;
349 RpcServerInterface* sif;
350 RPC_DISPATCH_FUNCTION func;
351 BOOL exception;
352 UUID *object_uuid;
353 NDR_SCONTEXT context_handle;
354 void *buf = msg->Buffer;
355
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,
360 status);
361
362 RPCRT4_Send(conn, response, NULL, 0);
363 RPCRT4_FreeHeader(response);
364 return RPC_S_OK;
365 }
366
367 if (hdr->common.flags & RPC_FLG_OBJECT_UUID) {
368 object_uuid = (UUID*)(hdr + 1);
369 } else {
370 object_uuid = NULL;
371 }
372
373 sif = RPCRT4_find_interface(object_uuid, &conn->ActiveInterface, NULL, TRUE);
374 if (!sif) {
375 WARN("interface %s no longer registered, returning fault packet\n", debugstr_guid(&conn->ActiveInterface.SyntaxGUID));
376 response = RPCRT4_BuildFaultHeader(NDR_LOCAL_DATA_REPRESENTATION,
377 NCA_S_UNK_IF);
378
379 RPCRT4_Send(conn, response, NULL, 0);
380 RPCRT4_FreeHeader(response);
381 return RPC_S_OK;
382 }
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);
388 }
389
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;
396 } else {
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,
400 NCA_S_OP_RNG_ERROR);
401
402 RPCRT4_Send(conn, response, NULL, 0);
403 RPCRT4_FreeHeader(response);
404 }
405 func = sif->If->DispatchTable->DispatchTable[msg->ProcNum];
406 }
407
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]));
413
414 exception = FALSE;
415
416 /* dispatch */
417 RPCRT4_SetThreadCurrentCallHandle(msg->Handle);
418 __TRY {
419 if (func) func(msg);
420 } __EXCEPT_ALL {
421 WARN("exception caught with code 0x%08x = %d\n", GetExceptionCode(), GetExceptionCode());
422 exception = TRUE;
423 if (GetExceptionCode() == STATUS_ACCESS_VIOLATION)
424 status = ERROR_NOACCESS;
425 else
426 status = GetExceptionCode();
427 response = RPCRT4_BuildFaultHeader(msg->DataRepresentation,
428 RPC2NCA_STATUS(status));
429 } __ENDTRY
430 RPCRT4_SetThreadCurrentCallHandle(NULL);
431
432 /* release any unmarshalled context handles */
433 while ((context_handle = RPCRT4_PopThreadContextHandle()) != NULL)
434 RpcServerAssoc_ReleaseContextHandle(conn->server_binding->Assoc, context_handle, TRUE);
435
436 if (!exception)
437 response = RPCRT4_BuildResponseHeader(msg->DataRepresentation,
438 msg->BufferLength);
439
440 /* send response packet */
441 if (response) {
442 status = RPCRT4_Send(conn, response, exception ? NULL : msg->Buffer,
443 exception ? 0 : msg->BufferLength);
444 RPCRT4_FreeHeader(response);
445 } else
446 ERR("out of memory\n");
447
448 msg->RpcInterfaceInformation = NULL;
449 RPCRT4_release_server_interface(sif);
450
451 if (msg->Buffer == buf) buf = NULL;
452 TRACE("freeing Buffer=%p\n", buf);
453 I_RpcFree(buf);
454
455 return status;
456 }
457
458 static RPC_STATUS process_auth3_packet(RpcConnection *conn,
459 RpcPktCommonHdr *hdr,
460 RPC_MESSAGE *msg,
461 unsigned char *auth_data,
462 ULONG auth_length)
463 {
464 RPC_STATUS status;
465
466 if (UuidIsNil(&conn->ActiveInterface.SyntaxGUID, &status) ||
467 !auth_length || msg->BufferLength != 0)
468 status = RPC_S_PROTOCOL_ERROR;
469 else
470 {
471 status = RPCRT4_ServerConnectionAuth(conn, FALSE,
472 (RpcAuthVerifier *)auth_data,
473 auth_length, NULL, NULL);
474 }
475
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 */
479
480 return RPC_S_OK;
481 }
482
483 static void RPCRT4_process_packet(RpcConnection* conn, RpcPktHdr* hdr,
484 RPC_MESSAGE* msg, unsigned char *auth_data,
485 ULONG auth_length)
486 {
487 msg->Handle = (RPC_BINDING_HANDLE)conn->server_binding;
488
489 switch (hdr->common.ptype) {
490 case PKT_BIND:
491 TRACE("got bind packet\n");
492 process_bind_packet(conn, &hdr->bind, msg, auth_data, auth_length);
493 break;
494
495 case PKT_REQUEST:
496 TRACE("got request packet\n");
497 process_request_packet(conn, &hdr->request, msg);
498 break;
499
500 case PKT_AUTH3:
501 TRACE("got auth3 packet\n");
502 process_auth3_packet(conn, &hdr->common, msg, auth_data, auth_length);
503 break;
504 default:
505 FIXME("unhandled packet type %u\n", hdr->common.ptype);
506 break;
507 }
508
509 /* clean up */
510 I_RpcFree(msg->Buffer);
511 RPCRT4_FreeHeader(hdr);
512 HeapFree(GetProcessHeap(), 0, msg);
513 HeapFree(GetProcessHeap(), 0, auth_data);
514 }
515
516 static DWORD CALLBACK RPCRT4_worker_thread(LPVOID the_arg)
517 {
518 RpcPacket *pkt = the_arg;
519 RPCRT4_process_packet(pkt->conn, pkt->hdr, pkt->msg, pkt->auth_data,
520 pkt->auth_length);
521 RPCRT4_ReleaseConnection(pkt->conn);
522 HeapFree(GetProcessHeap(), 0, pkt);
523 return 0;
524 }
525
526 static DWORD CALLBACK RPCRT4_io_thread(LPVOID the_arg)
527 {
528 RpcConnection* conn = the_arg;
529 RpcPktHdr *hdr;
530 RPC_MESSAGE *msg;
531 RPC_STATUS status;
532 RpcPacket *packet;
533 unsigned char *auth_data;
534 ULONG auth_length;
535
536 TRACE("(%p)\n", conn);
537
538 for (;;) {
539 msg = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RPC_MESSAGE));
540 if (!msg) break;
541
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);
546 break;
547 }
548
549 switch (hdr->common.ptype) {
550 case PKT_BIND:
551 TRACE("got bind packet\n");
552
553 status = process_bind_packet(conn, &hdr->bind, msg, auth_data,
554 auth_length);
555 break;
556
557 case PKT_REQUEST:
558 TRACE("got request packet\n");
559
560 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcPacket));
561 if (!packet) {
562 I_RpcFree(msg->Buffer);
563 RPCRT4_FreeHeader(hdr);
564 HeapFree(GetProcessHeap(), 0, msg);
565 HeapFree(GetProcessHeap(), 0, auth_data);
566 goto exit;
567 }
568 packet->conn = RPCRT4_GrabConnection( conn );
569 packet->hdr = hdr;
570 packet->msg = msg;
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;
577 } else {
578 continue;
579 }
580 break;
581
582 case PKT_AUTH3:
583 TRACE("got auth3 packet\n");
584
585 status = process_auth3_packet(conn, &hdr->common, msg, auth_data,
586 auth_length);
587 break;
588 default:
589 FIXME("unhandled packet type %u\n", hdr->common.ptype);
590 break;
591 }
592
593 I_RpcFree(msg->Buffer);
594 RPCRT4_FreeHeader(hdr);
595 HeapFree(GetProcessHeap(), 0, msg);
596 HeapFree(GetProcessHeap(), 0, auth_data);
597
598 if (status != RPC_S_OK) {
599 WARN("processing packet failed with error %u\n", status);
600 break;
601 }
602 }
603 exit:
604 RPCRT4_ReleaseConnection(conn);
605 return 0;
606 }
607
608 void RPCRT4_new_client(RpcConnection* conn)
609 {
610 HANDLE thread = CreateThread(NULL, 0, RPCRT4_io_thread, conn, 0, NULL);
611 if (!thread) {
612 DWORD err = GetLastError();
613 ERR("failed to create thread, error=%08x\n", err);
614 RPCRT4_ReleaseConnection(conn);
615 }
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 */
620
621 CloseHandle( thread );
622 }
623
624 static DWORD CALLBACK RPCRT4_server_thread(LPVOID the_arg)
625 {
626 int res;
627 unsigned int count;
628 void *objs = NULL;
629 RpcServerProtseq* cps = the_arg;
630 RpcConnection* conn;
631 BOOL set_ready_event = FALSE;
632
633 TRACE("(the_arg == ^%p)\n", the_arg);
634
635 for (;;) {
636 objs = cps->ops->get_wait_array(cps, objs, &count);
637
638 if (set_ready_event)
639 {
640 /* signal to function that changed state that we are now sync'ed */
641 SetEvent(cps->server_ready_event);
642 set_ready_event = FALSE;
643 }
644
645 /* start waiting */
646 res = cps->ops->wait_for_new_connection(cps, count, objs);
647
648 if (res == -1 || (res == 0 && !std_listen))
649 {
650 /* cleanup */
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);
656
657 if (res == 0 && !std_listen)
658 SetEvent(cps->server_ready_event);
659 break;
660 }
661 else if (res == 0)
662 set_ready_event = TRUE;
663 }
664 return 0;
665 }
666
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)
670 {
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);
675
676 ps->ops->signal_state_changed(ps);
677
678 /* wait for server thread to make the requested changes before returning */
679 WaitForSingleObject(ps->server_ready_event, INFINITE);
680
681 ReleaseMutex(ps->mgr_mutex);
682 }
683
684 static RPC_STATUS RPCRT4_start_listen_protseq(RpcServerProtseq *ps, BOOL auto_listen)
685 {
686 RPC_STATUS status = RPC_S_OK;
687 HANDLE server_thread;
688
689 EnterCriticalSection(&listen_cs);
690 if (ps->is_listening) goto done;
691
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);
695 if (!server_thread)
696 {
697 status = RPC_S_OUT_OF_RESOURCES;
698 goto done;
699 }
700 ps->is_listening = TRUE;
701 CloseHandle(server_thread);
702
703 done:
704 LeaveCriticalSection(&listen_cs);
705 return status;
706 }
707
708 static RPC_STATUS RPCRT4_start_listen(BOOL auto_listen)
709 {
710 RPC_STATUS status = RPC_S_ALREADY_LISTENING;
711 RpcServerProtseq *cps;
712
713 TRACE("\n");
714
715 EnterCriticalSection(&listen_cs);
716 if (auto_listen || (manual_listen_count++ == 0))
717 {
718 status = RPC_S_OK;
719 if (++listen_count == 1)
720 std_listen = TRUE;
721 }
722 LeaveCriticalSection(&listen_cs);
723
724 if (std_listen)
725 {
726 EnterCriticalSection(&server_cs);
727 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
728 {
729 status = RPCRT4_start_listen_protseq(cps, TRUE);
730 if (status != RPC_S_OK)
731 break;
732
733 /* make sure server is actually listening on the interface before
734 * returning */
735 RPCRT4_sync_with_server_thread(cps);
736 }
737 LeaveCriticalSection(&server_cs);
738 }
739
740 return status;
741 }
742
743 static void RPCRT4_stop_listen(BOOL auto_listen)
744 {
745 EnterCriticalSection(&listen_cs);
746 if (auto_listen || (--manual_listen_count == 0))
747 {
748 if (listen_count != 0 && --listen_count == 0) {
749 RpcServerProtseq *cps;
750
751 std_listen = FALSE;
752 LeaveCriticalSection(&listen_cs);
753
754 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
755 RPCRT4_sync_with_server_thread(cps);
756
757 EnterCriticalSection(&listen_cs);
758 if (listen_done_event) SetEvent( listen_done_event );
759 listen_done_event = 0;
760 LeaveCriticalSection(&listen_cs);
761 return;
762 }
763 assert(listen_count >= 0);
764 }
765 LeaveCriticalSection(&listen_cs);
766 }
767
768 static BOOL RPCRT4_protseq_is_endpoint_registered(RpcServerProtseq *protseq, const char *endpoint)
769 {
770 RpcConnection *conn;
771 EnterCriticalSection(&protseq->cs);
772 for (conn = protseq->conn; conn; conn = conn->Next)
773 {
774 if (!endpoint || !strcmp(endpoint, conn->Endpoint))
775 break;
776 }
777 LeaveCriticalSection(&protseq->cs);
778 return (conn != NULL);
779 }
780
781 static RPC_STATUS RPCRT4_use_protseq(RpcServerProtseq* ps, const char *endpoint)
782 {
783 RPC_STATUS status;
784
785 EnterCriticalSection(&ps->cs);
786
787 if (RPCRT4_protseq_is_endpoint_registered(ps, endpoint))
788 status = RPC_S_OK;
789 else
790 status = ps->ops->open_endpoint(ps, endpoint);
791
792 LeaveCriticalSection(&ps->cs);
793
794 if (status != RPC_S_OK)
795 return status;
796
797 if (std_listen)
798 {
799 status = RPCRT4_start_listen_protseq(ps, FALSE);
800 if (status == RPC_S_OK)
801 RPCRT4_sync_with_server_thread(ps);
802 }
803
804 return status;
805 }
806
807 /***********************************************************************
808 * RpcServerInqBindings (RPCRT4.@)
809 */
810 RPC_STATUS WINAPI RpcServerInqBindings( RPC_BINDING_VECTOR** BindingVector )
811 {
812 RPC_STATUS status;
813 DWORD count;
814 RpcServerProtseq* ps;
815 RpcConnection* conn;
816
817 if (BindingVector)
818 TRACE("(*BindingVector == ^%p)\n", *BindingVector);
819 else
820 ERR("(BindingVector == NULL!!?)\n");
821
822 EnterCriticalSection(&server_cs);
823 /* count connections */
824 count = 0;
825 LIST_FOR_EACH_ENTRY(ps, &protseqs, RpcServerProtseq, entry) {
826 EnterCriticalSection(&ps->cs);
827 for (conn = ps->conn; conn; conn = conn->Next)
828 count++;
829 LeaveCriticalSection(&ps->cs);
830 }
831 if (count) {
832 /* export bindings */
833 *BindingVector = HeapAlloc(GetProcessHeap(), 0,
834 sizeof(RPC_BINDING_VECTOR) +
835 sizeof(RPC_BINDING_HANDLE)*(count-1));
836 (*BindingVector)->Count = count;
837 count = 0;
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],
842 conn);
843 count++;
844 }
845 LeaveCriticalSection(&ps->cs);
846 }
847 status = RPC_S_OK;
848 } else {
849 *BindingVector = NULL;
850 status = RPC_S_NO_BINDINGS;
851 }
852 LeaveCriticalSection(&server_cs);
853 return status;
854 }
855
856 /***********************************************************************
857 * RpcServerUseProtseqEpA (RPCRT4.@)
858 */
859 RPC_STATUS WINAPI RpcServerUseProtseqEpA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor )
860 {
861 RPC_POLICY policy;
862
863 TRACE( "(%s,%u,%s,%p)\n", Protseq, MaxCalls, Endpoint, SecurityDescriptor );
864
865 /* This should provide the default behaviour */
866 policy.Length = sizeof( policy );
867 policy.EndpointFlags = 0;
868 policy.NICFlags = 0;
869
870 return RpcServerUseProtseqEpExA( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
871 }
872
873 /***********************************************************************
874 * RpcServerUseProtseqEpW (RPCRT4.@)
875 */
876 RPC_STATUS WINAPI RpcServerUseProtseqEpW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor )
877 {
878 RPC_POLICY policy;
879
880 TRACE( "(%s,%u,%s,%p)\n", debugstr_w( Protseq ), MaxCalls, debugstr_w( Endpoint ), SecurityDescriptor );
881
882 /* This should provide the default behaviour */
883 policy.Length = sizeof( policy );
884 policy.EndpointFlags = 0;
885 policy.NICFlags = 0;
886
887 return RpcServerUseProtseqEpExW( Protseq, MaxCalls, Endpoint, SecurityDescriptor, &policy );
888 }
889
890 /***********************************************************************
891 * alloc_serverprotoseq (internal)
892 *
893 * Must be called with server_cs held.
894 */
895 static RPC_STATUS alloc_serverprotoseq(UINT MaxCalls, const char *Protseq, RpcServerProtseq **ps)
896 {
897 const struct protseq_ops *ops = rpcrt4_get_protseq_ops(Protseq);
898
899 if (!ops)
900 {
901 FIXME("protseq %s not supported\n", debugstr_a(Protseq));
902 return RPC_S_PROTSEQ_NOT_SUPPORTED;
903 }
904
905 *ps = ops->alloc();
906 if (!*ps)
907 return RPC_S_OUT_OF_RESOURCES;
908 (*ps)->MaxCalls = MaxCalls;
909 (*ps)->Protseq = RPCRT4_strdupA(Protseq);
910 (*ps)->ops = ops;
911 (*ps)->MaxCalls = 0;
912 (*ps)->conn = NULL;
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;
918
919 list_add_head(&protseqs, &(*ps)->entry);
920
921 TRACE("new protseq %p created for %s\n", *ps, Protseq);
922
923 return RPC_S_OK;
924 }
925
926 /* must be called with server_cs held */
927 static void destroy_serverprotoseq(RpcServerProtseq *ps)
928 {
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);
936 }
937
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)
940 {
941 RPC_STATUS status;
942 RpcServerProtseq *cps;
943
944 EnterCriticalSection(&server_cs);
945
946 LIST_FOR_EACH_ENTRY(cps, &protseqs, RpcServerProtseq, entry)
947 if (!strcmp(cps->Protseq, Protseq))
948 {
949 TRACE("found existing protseq object for %s\n", Protseq);
950 *ps = cps;
951 LeaveCriticalSection(&server_cs);
952 return S_OK;
953 }
954
955 status = alloc_serverprotoseq(MaxCalls, Protseq, ps);
956
957 LeaveCriticalSection(&server_cs);
958
959 return status;
960 }
961
962 /***********************************************************************
963 * RpcServerUseProtseqEpExA (RPCRT4.@)
964 */
965 RPC_STATUS WINAPI RpcServerUseProtseqEpExA( RPC_CSTR Protseq, UINT MaxCalls, RPC_CSTR Endpoint, LPVOID SecurityDescriptor,
966 PRPC_POLICY lpPolicy )
967 {
968 RpcServerProtseq* ps;
969 RPC_STATUS status;
970
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 );
974
975 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
976 if (status != RPC_S_OK)
977 return status;
978
979 return RPCRT4_use_protseq(ps, (const char *)Endpoint);
980 }
981
982 /***********************************************************************
983 * RpcServerUseProtseqEpExW (RPCRT4.@)
984 */
985 RPC_STATUS WINAPI RpcServerUseProtseqEpExW( RPC_WSTR Protseq, UINT MaxCalls, RPC_WSTR Endpoint, LPVOID SecurityDescriptor,
986 PRPC_POLICY lpPolicy )
987 {
988 RpcServerProtseq* ps;
989 RPC_STATUS status;
990 LPSTR ProtseqA;
991 LPSTR EndpointA;
992
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 );
996
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)
1001 return status;
1002
1003 EndpointA = RPCRT4_strdupWtoA(Endpoint);
1004 status = RPCRT4_use_protseq(ps, EndpointA);
1005 RPCRT4_strfree(EndpointA);
1006 return status;
1007 }
1008
1009 /***********************************************************************
1010 * RpcServerUseProtseqA (RPCRT4.@)
1011 */
1012 RPC_STATUS WINAPI RpcServerUseProtseqA(RPC_CSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1013 {
1014 RPC_STATUS status;
1015 RpcServerProtseq* ps;
1016
1017 TRACE("(Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_a((char*)Protseq), MaxCalls, SecurityDescriptor);
1018
1019 status = RPCRT4_get_or_create_serverprotseq(MaxCalls, (const char *)Protseq, &ps);
1020 if (status != RPC_S_OK)
1021 return status;
1022
1023 return RPCRT4_use_protseq(ps, NULL);
1024 }
1025
1026 /***********************************************************************
1027 * RpcServerUseProtseqW (RPCRT4.@)
1028 */
1029 RPC_STATUS WINAPI RpcServerUseProtseqW(RPC_WSTR Protseq, unsigned int MaxCalls, void *SecurityDescriptor)
1030 {
1031 RPC_STATUS status;
1032 RpcServerProtseq* ps;
1033 LPSTR ProtseqA;
1034
1035 TRACE("Protseq == %s, MaxCalls == %d, SecurityDescriptor == ^%p)\n", debugstr_w(Protseq), MaxCalls, SecurityDescriptor);
1036
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)
1041 return status;
1042
1043 return RPCRT4_use_protseq(ps, NULL);
1044 }
1045
1046 void RPCRT4_destroy_all_protseqs(void)
1047 {
1048 RpcServerProtseq *cps, *cursor2;
1049
1050 if (listen_count != 0)
1051 std_listen = FALSE;
1052
1053 EnterCriticalSection(&server_cs);
1054 LIST_FOR_EACH_ENTRY_SAFE(cps, cursor2, &protseqs, RpcServerProtseq, entry)
1055 {
1056 #ifndef __REACTOS__
1057 if (listen_count != 0)
1058 RPCRT4_sync_with_server_thread(cps);
1059 #endif
1060 destroy_serverprotoseq(cps);
1061 }
1062 LeaveCriticalSection(&server_cs);
1063 DeleteCriticalSection(&server_cs);
1064 DeleteCriticalSection(&listen_cs);
1065 }
1066
1067 /***********************************************************************
1068 * RpcServerRegisterIf (RPCRT4.@)
1069 */
1070 RPC_STATUS WINAPI RpcServerRegisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, RPC_MGR_EPV* MgrEpv )
1071 {
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 );
1074 }
1075
1076 /***********************************************************************
1077 * RpcServerRegisterIfEx (RPCRT4.@)
1078 */
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 )
1081 {
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 );
1084 }
1085
1086 /***********************************************************************
1087 * RpcServerRegisterIf2 (RPCRT4.@)
1088 */
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 )
1091 {
1092 PRPC_SERVER_INTERFACE If = IfSpec;
1093 RpcServerInterface* sif;
1094 unsigned int i;
1095
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]);
1109 }
1110 TRACE(" reserved: %ld\n", If->DispatchTable->Reserved);
1111 }
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);
1115
1116 sif = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcServerInterface));
1117 sif->If = If;
1118 if (MgrTypeUuid) {
1119 sif->MgrTypeUuid = *MgrTypeUuid;
1120 sif->MgrEpv = MgrEpv;
1121 } else {
1122 memset(&sif->MgrTypeUuid, 0, sizeof(UUID));
1123 sif->MgrEpv = If->DefaultManagerEpv;
1124 }
1125 sif->Flags = Flags;
1126 sif->MaxCalls = MaxCalls;
1127 sif->MaxRpcSize = MaxRpcSize;
1128 sif->IfCallbackFn = IfCallbackFn;
1129
1130 EnterCriticalSection(&server_cs);
1131 list_add_head(&server_interfaces, &sif->entry);
1132 LeaveCriticalSection(&server_cs);
1133
1134 if (sif->Flags & RPC_IF_AUTOLISTEN)
1135 RPCRT4_start_listen(TRUE);
1136
1137 return RPC_S_OK;
1138 }
1139
1140 /***********************************************************************
1141 * RpcServerUnregisterIf (RPCRT4.@)
1142 */
1143 RPC_STATUS WINAPI RpcServerUnregisterIf( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, UINT WaitForCallsToComplete )
1144 {
1145 PRPC_SERVER_INTERFACE If = IfSpec;
1146 HANDLE event = NULL;
1147 BOOL found = FALSE;
1148 BOOL completed = TRUE;
1149 RpcServerInterface *cif;
1150 RPC_STATUS status;
1151
1152 TRACE("(IfSpec == (RPC_IF_HANDLE)^%p (%s), MgrTypeUuid == %s, WaitForCallsToComplete == %u)\n",
1153 IfSpec, debugstr_guid(&If->InterfaceId.SyntaxGUID), debugstr_guid(MgrTypeUuid), WaitForCallsToComplete);
1154
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) {
1162 completed = FALSE;
1163 cif->Delete = TRUE;
1164 if (WaitForCallsToComplete)
1165 cif->CallsCompletedEvent = event = CreateEventW(NULL, FALSE, FALSE, NULL);
1166 }
1167 found = TRUE;
1168 break;
1169 }
1170 }
1171 LeaveCriticalSection(&server_cs);
1172
1173 if (!found) {
1174 ERR("not found for object %s\n", debugstr_guid(MgrTypeUuid));
1175 return RPC_S_UNKNOWN_IF;
1176 }
1177
1178 if (completed)
1179 HeapFree(GetProcessHeap(), 0, cif);
1180 else if (event) {
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);
1184 CloseHandle(event);
1185 }
1186
1187 return RPC_S_OK;
1188 }
1189
1190 /***********************************************************************
1191 * RpcServerUnregisterIfEx (RPCRT4.@)
1192 */
1193 RPC_STATUS WINAPI RpcServerUnregisterIfEx( RPC_IF_HANDLE IfSpec, UUID* MgrTypeUuid, int RundownContextHandles )
1194 {
1195 FIXME("(IfSpec == (RPC_IF_HANDLE)^%p, MgrTypeUuid == %s, RundownContextHandles == %d): stub\n",
1196 IfSpec, debugstr_guid(MgrTypeUuid), RundownContextHandles);
1197
1198 return RPC_S_OK;
1199 }
1200
1201 /***********************************************************************
1202 * RpcObjectSetType (RPCRT4.@)
1203 *
1204 * PARAMS
1205 * ObjUuid [I] "Object" UUID
1206 * TypeUuid [I] "Type" UUID
1207 *
1208 * RETURNS
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
1212 *
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.
1220 */
1221 RPC_STATUS WINAPI RpcObjectSetType( UUID* ObjUuid, UUID* TypeUuid )
1222 {
1223 RpcObjTypeMap *map = RpcObjTypeMaps, *prev = NULL;
1224 RPC_STATUS dummy;
1225
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;
1230 }
1231
1232 /* find the mapping for this object if there is one ... */
1233 while (map) {
1234 if (! UuidCompare(ObjUuid, &map->Object, &dummy)) break;
1235 prev = map;
1236 map = map->next;
1237 }
1238 if ((! TypeUuid) || UuidIsNil(TypeUuid, &dummy)) {
1239 /* ... and drop it from the list */
1240 if (map) {
1241 if (prev)
1242 prev->next = map->next;
1243 else
1244 RpcObjTypeMaps = map->next;
1245 HeapFree(GetProcessHeap(), 0, map);
1246 }
1247 } else {
1248 /* ... , fail if we found it ... */
1249 if (map)
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;
1255 map->next = NULL;
1256 if (prev)
1257 prev->next = map; /* prev is the last map in the linklist */
1258 else
1259 RpcObjTypeMaps = map;
1260 }
1261
1262 return RPC_S_OK;
1263 }
1264
1265 struct rpc_server_registered_auth_info
1266 {
1267 struct list entry;
1268 TimeStamp exp;
1269 CredHandle cred;
1270 ULONG max_token;
1271 USHORT auth_type;
1272 };
1273
1274 RPC_STATUS RPCRT4_ServerGetRegisteredAuthInfo(
1275 USHORT auth_type, CredHandle *cred, TimeStamp *exp, ULONG *max_token)
1276 {
1277 RPC_STATUS status = RPC_S_UNKNOWN_AUTHN_SERVICE;
1278 struct rpc_server_registered_auth_info *auth_info;
1279
1280 EnterCriticalSection(&server_auth_info_cs);
1281 LIST_FOR_EACH_ENTRY(auth_info, &server_registered_auth_info, struct rpc_server_registered_auth_info, entry)
1282 {
1283 if (auth_info->auth_type == auth_type)
1284 {
1285 *cred = auth_info->cred;
1286 *exp = auth_info->exp;
1287 *max_token = auth_info->max_token;
1288 status = RPC_S_OK;
1289 break;
1290 }
1291 }
1292 LeaveCriticalSection(&server_auth_info_cs);
1293
1294 return status;
1295 }
1296
1297 void RPCRT4_ServerFreeAllRegisteredAuthInfo(void)
1298 {
1299 struct rpc_server_registered_auth_info *auth_info, *cursor2;
1300
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)
1303 {
1304 FreeCredentialsHandle(&auth_info->cred);
1305 HeapFree(GetProcessHeap(), 0, auth_info);
1306 }
1307 LeaveCriticalSection(&server_auth_info_cs);
1308 DeleteCriticalSection(&server_auth_info_cs);
1309 }
1310
1311 /***********************************************************************
1312 * RpcServerRegisterAuthInfoA (RPCRT4.@)
1313 */
1314 RPC_STATUS WINAPI RpcServerRegisterAuthInfoA( RPC_CSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1315 LPVOID Arg )
1316 {
1317 SECURITY_STATUS sec_status;
1318 CredHandle cred;
1319 TimeStamp exp;
1320 ULONG package_count;
1321 ULONG i;
1322 PSecPkgInfoA packages;
1323 ULONG max_token;
1324 struct rpc_server_registered_auth_info *auth_info;
1325
1326 TRACE("(%s,%u,%p,%p)\n", ServerPrincName, AuthnSvc, GetKeyFn, Arg);
1327
1328 sec_status = EnumerateSecurityPackagesA(&package_count, &packages);
1329 if (sec_status != SEC_E_OK)
1330 {
1331 ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n",
1332 sec_status);
1333 return RPC_S_SEC_PKG_ERROR;
1334 }
1335
1336 for (i = 0; i < package_count; i++)
1337 if (packages[i].wRPCID == AuthnSvc)
1338 break;
1339
1340 if (i == package_count)
1341 {
1342 WARN("unsupported AuthnSvc %u\n", AuthnSvc);
1343 FreeContextBuffer(packages);
1344 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1345 }
1346 TRACE("found package %s for service %u\n", packages[i].Name,
1347 AuthnSvc);
1348 sec_status = AcquireCredentialsHandleA((SEC_CHAR *)ServerPrincName,
1349 packages[i].Name,
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;
1356
1357 auth_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info));
1358 if (!auth_info)
1359 {
1360 FreeCredentialsHandle(&cred);
1361 return RPC_S_OUT_OF_RESOURCES;
1362 }
1363
1364 auth_info->exp = exp;
1365 auth_info->cred = cred;
1366 auth_info->max_token = max_token;
1367 auth_info->auth_type = AuthnSvc;
1368
1369 EnterCriticalSection(&server_auth_info_cs);
1370 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1371 LeaveCriticalSection(&server_auth_info_cs);
1372
1373 return RPC_S_OK;
1374 }
1375
1376 /***********************************************************************
1377 * RpcServerRegisterAuthInfoW (RPCRT4.@)
1378 */
1379 RPC_STATUS WINAPI RpcServerRegisterAuthInfoW( RPC_WSTR ServerPrincName, ULONG AuthnSvc, RPC_AUTH_KEY_RETRIEVAL_FN GetKeyFn,
1380 LPVOID Arg )
1381 {
1382 SECURITY_STATUS sec_status;
1383 CredHandle cred;
1384 TimeStamp exp;
1385 ULONG package_count;
1386 ULONG i;
1387 PSecPkgInfoW packages;
1388 ULONG max_token;
1389 struct rpc_server_registered_auth_info *auth_info;
1390
1391 TRACE("(%s,%u,%p,%p)\n", debugstr_w(ServerPrincName), AuthnSvc, GetKeyFn, Arg);
1392
1393 sec_status = EnumerateSecurityPackagesW(&package_count, &packages);
1394 if (sec_status != SEC_E_OK)
1395 {
1396 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n",
1397 sec_status);
1398 return RPC_S_SEC_PKG_ERROR;
1399 }
1400
1401 for (i = 0; i < package_count; i++)
1402 if (packages[i].wRPCID == AuthnSvc)
1403 break;
1404
1405 if (i == package_count)
1406 {
1407 WARN("unsupported AuthnSvc %u\n", AuthnSvc);
1408 FreeContextBuffer(packages);
1409 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1410 }
1411 TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name),
1412 AuthnSvc);
1413 sec_status = AcquireCredentialsHandleW((SEC_WCHAR *)ServerPrincName,
1414 packages[i].Name,
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;
1421
1422 auth_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*auth_info));
1423 if (!auth_info)
1424 {
1425 FreeCredentialsHandle(&cred);
1426 return RPC_S_OUT_OF_RESOURCES;
1427 }
1428
1429 auth_info->exp = exp;
1430 auth_info->cred = cred;
1431 auth_info->max_token = max_token;
1432 auth_info->auth_type = AuthnSvc;
1433
1434 EnterCriticalSection(&server_auth_info_cs);
1435 list_add_tail(&server_registered_auth_info, &auth_info->entry);
1436 LeaveCriticalSection(&server_auth_info_cs);
1437
1438 return RPC_S_OK;
1439 }
1440
1441 /******************************************************************************
1442 * RpcServerInqDefaultPrincNameA (rpcrt4.@)
1443 */
1444 RPC_STATUS RPC_ENTRY RpcServerInqDefaultPrincNameA(ULONG AuthnSvc, RPC_CSTR *PrincName)
1445 {
1446 RPC_STATUS ret;
1447 RPC_WSTR principalW;
1448
1449 TRACE("%u, %p\n", AuthnSvc, PrincName);
1450
1451 if ((ret = RpcServerInqDefaultPrincNameW( AuthnSvc, &principalW )) == RPC_S_OK)
1452 {
1453 if (!(*PrincName = (RPC_CSTR)RPCRT4_strdupWtoA( principalW ))) return RPC_S_OUT_OF_MEMORY;
1454 RpcStringFreeW( &principalW );
1455 }
1456 return ret;
1457 }
1458
1459 /******************************************************************************
1460 * RpcServerInqDefaultPrincNameW (rpcrt4.@)
1461 */
1462 RPC_STATUS RPC_ENTRY RpcServerInqDefaultPrincNameW(ULONG AuthnSvc, RPC_WSTR *PrincName)
1463 {
1464 ULONG len = 0;
1465
1466 FIXME("%u, %p\n", AuthnSvc, PrincName);
1467
1468 if (AuthnSvc != RPC_C_AUTHN_WINNT) return RPC_S_UNKNOWN_AUTHN_SERVICE;
1469
1470 GetUserNameExW( NameSamCompatible, NULL, &len );
1471 if (GetLastError() != ERROR_MORE_DATA) return RPC_S_INTERNAL_ERROR;
1472
1473 if (!(*PrincName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1474 return RPC_S_OUT_OF_MEMORY;
1475
1476 GetUserNameExW( NameSamCompatible, *PrincName, &len );
1477 return RPC_S_OK;
1478 }
1479
1480 /***********************************************************************
1481 * RpcServerListen (RPCRT4.@)
1482 */
1483 RPC_STATUS WINAPI RpcServerListen( UINT MinimumCallThreads, UINT MaxCalls, UINT DontWait )
1484 {
1485 RPC_STATUS status = RPC_S_OK;
1486
1487 TRACE("(%u,%u,%u)\n", MinimumCallThreads, MaxCalls, DontWait);
1488
1489 if (list_empty(&protseqs))
1490 return RPC_S_NO_PROTSEQS_REGISTERED;
1491
1492 status = RPCRT4_start_listen(FALSE);
1493
1494 if (DontWait || (status != RPC_S_OK)) return status;
1495
1496 return RpcMgmtWaitServerListen();
1497 }
1498
1499 /***********************************************************************
1500 * RpcMgmtServerWaitListen (RPCRT4.@)
1501 */
1502 RPC_STATUS WINAPI RpcMgmtWaitServerListen( void )
1503 {
1504 HANDLE event;
1505
1506 TRACE("()\n");
1507
1508 EnterCriticalSection(&listen_cs);
1509
1510 if (!std_listen) {
1511 LeaveCriticalSection(&listen_cs);
1512 return RPC_S_NOT_LISTENING;
1513 }
1514 if (listen_done_event) {
1515 LeaveCriticalSection(&listen_cs);
1516 return RPC_S_ALREADY_LISTENING;
1517 }
1518 event = CreateEventW( NULL, TRUE, FALSE, NULL );
1519 listen_done_event = event;
1520
1521 LeaveCriticalSection(&listen_cs);
1522
1523 TRACE( "waiting for server calls to finish\n" );
1524 WaitForSingleObject( event, INFINITE );
1525 TRACE( "done waiting\n" );
1526
1527 CloseHandle( event );
1528 return RPC_S_OK;
1529 }
1530
1531 /***********************************************************************
1532 * RpcMgmtStopServerListening (RPCRT4.@)
1533 */
1534 RPC_STATUS WINAPI RpcMgmtStopServerListening ( RPC_BINDING_HANDLE Binding )
1535 {
1536 TRACE("(Binding == (RPC_BINDING_HANDLE)^%p)\n", Binding);
1537
1538 if (Binding) {
1539 FIXME("client-side invocation not implemented.\n");
1540 return RPC_S_WRONG_KIND_OF_BINDING;
1541 }
1542
1543 RPCRT4_stop_listen(FALSE);
1544
1545 return RPC_S_OK;
1546 }
1547
1548 /***********************************************************************
1549 * RpcMgmtEnableIdleCleanup (RPCRT4.@)
1550 */
1551 RPC_STATUS WINAPI RpcMgmtEnableIdleCleanup(void)
1552 {
1553 FIXME("(): stub\n");
1554 return RPC_S_OK;
1555 }
1556
1557 /***********************************************************************
1558 * I_RpcServerStartListening (RPCRT4.@)
1559 */
1560 RPC_STATUS WINAPI I_RpcServerStartListening( HWND hWnd )
1561 {
1562 FIXME( "(%p): stub\n", hWnd );
1563
1564 return RPC_S_OK;
1565 }
1566
1567 /***********************************************************************
1568 * I_RpcServerStopListening (RPCRT4.@)
1569 */
1570 RPC_STATUS WINAPI I_RpcServerStopListening( void )
1571 {
1572 FIXME( "(): stub\n" );
1573
1574 return RPC_S_OK;
1575 }
1576
1577 /***********************************************************************
1578 * I_RpcWindowProc (RPCRT4.@)
1579 */
1580 UINT WINAPI I_RpcWindowProc( void *hWnd, UINT Message, UINT wParam, ULONG lParam )
1581 {
1582 FIXME( "(%p,%08x,%08x,%08x): stub\n", hWnd, Message, wParam, lParam );
1583
1584 return 0;
1585 }
1586
1587 /***********************************************************************
1588 * RpcMgmtInqIfIds (RPCRT4.@)
1589 */
1590 RPC_STATUS WINAPI RpcMgmtInqIfIds(RPC_BINDING_HANDLE Binding, RPC_IF_ID_VECTOR **IfIdVector)
1591 {
1592 FIXME("(%p,%p): stub\n", Binding, IfIdVector);
1593 return RPC_S_INVALID_BINDING;
1594 }
1595
1596 /***********************************************************************
1597 * RpcMgmtInqStats (RPCRT4.@)
1598 */
1599 RPC_STATUS WINAPI RpcMgmtInqStats(RPC_BINDING_HANDLE Binding, RPC_STATS_VECTOR **Statistics)
1600 {
1601 RPC_STATS_VECTOR *stats;
1602
1603 FIXME("(%p,%p)\n", Binding, Statistics);
1604
1605 if ((stats = HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_STATS_VECTOR))))
1606 {
1607 stats->Count = 1;
1608 stats->Stats[0] = 0;
1609 *Statistics = stats;
1610 return RPC_S_OK;
1611 }
1612 return RPC_S_OUT_OF_RESOURCES;
1613 }
1614
1615 /***********************************************************************
1616 * RpcMgmtStatsVectorFree (RPCRT4.@)
1617 */
1618 RPC_STATUS WINAPI RpcMgmtStatsVectorFree(RPC_STATS_VECTOR **StatsVector)
1619 {
1620 FIXME("(%p)\n", StatsVector);
1621
1622 if (StatsVector)
1623 {
1624 HeapFree(GetProcessHeap(), 0, *StatsVector);
1625 *StatsVector = NULL;
1626 }
1627 return RPC_S_OK;
1628 }
1629
1630 /***********************************************************************
1631 * RpcMgmtEpEltInqBegin (RPCRT4.@)
1632 */
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)
1635 {
1636 FIXME("(%p,%u,%p,%u,%p,%p): stub\n",
1637 Binding, InquiryType, IfId, VersOption, ObjectUuid, InquiryContext);
1638 return RPC_S_INVALID_BINDING;
1639 }
1640
1641 /***********************************************************************
1642 * RpcMgmtIsServerListening (RPCRT4.@)
1643 */
1644 RPC_STATUS WINAPI RpcMgmtIsServerListening(RPC_BINDING_HANDLE Binding)
1645 {
1646 RPC_STATUS status = RPC_S_NOT_LISTENING;
1647
1648 TRACE("(%p)\n", Binding);
1649
1650 EnterCriticalSection(&listen_cs);
1651 if (manual_listen_count > 0) status = RPC_S_OK;
1652 LeaveCriticalSection(&listen_cs);
1653 return status;
1654 }
1655
1656 /***********************************************************************
1657 * RpcMgmtSetAuthorizationFn (RPCRT4.@)
1658 */
1659 RPC_STATUS WINAPI RpcMgmtSetAuthorizationFn(RPC_MGMT_AUTHORIZATION_FN fn)
1660 {
1661 FIXME("(%p): stub\n", fn);
1662 return RPC_S_OK;
1663 }
1664
1665 /***********************************************************************
1666 * RpcMgmtSetServerStackSize (RPCRT4.@)
1667 */
1668 RPC_STATUS WINAPI RpcMgmtSetServerStackSize(ULONG ThreadStackSize)
1669 {
1670 FIXME("(0x%x): stub\n", ThreadStackSize);
1671 return RPC_S_OK;
1672 }
1673
1674 /***********************************************************************
1675 * I_RpcGetCurrentCallHandle (RPCRT4.@)
1676 */
1677 RPC_BINDING_HANDLE WINAPI I_RpcGetCurrentCallHandle(void)
1678 {
1679 TRACE("\n");
1680 return RPCRT4_GetThreadCurrentCallHandle();
1681 }