4 * Copyright 2002 Greg Turner
5 * Copyright 2001 Ove Kåven, TransGaming Technologies
6 * Copyright 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
31 #include "wine/debug.h"
32 #include "wine/exception.h"
34 #include "rpc_binding.h"
36 #include "epm_towers.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 /* The "real" RPC portmapper endpoints that I know of are:
44 * ncacn_np: \\pipe\epmapper
48 * If the user's machine ran a DCE RPC daemon, it would
49 * probably be possible to connect to it, but there are many
50 * reasons not to, like:
51 * - the user probably does *not* run one, and probably
52 * shouldn't be forced to run one just for local COM
53 * - very few Unix systems use DCE RPC... if they run a RPC
54 * daemon at all, it's usually Sun RPC
55 * - DCE RPC registrations are persistent and saved on disk,
56 * while MS-RPC registrations are documented as non-persistent
57 * and stored only in RAM, and auto-destroyed when the process
58 * dies (something DCE RPC can't do)
60 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
61 * there would be interoperability advantages, like the possibility
62 * of running a fully functional DCOM server using Wine...
65 static const struct epm_endpoints
71 { "ncacn_np", "\\pipe\\epmapper" },
72 { "ncacn_ip_tcp", "135" },
73 { "ncacn_ip_udp", "135" },
74 { "ncalprc", "epmapper" },
75 { "ncacn_http", "593" },
78 static BOOL start_rpcss(void)
80 PROCESS_INFORMATION pi;
83 static const WCHAR rpcss[] = {'r','p','c','s','s',0};
88 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
89 ZeroMemory(&si, sizeof(STARTUPINFOA));
90 si.cb = sizeof(STARTUPINFOA);
92 memcpy(cmd, rpcss, sizeof(rpcss));
94 rslt = CreateProcessW(
95 NULL, /* executable */
96 cmd, /* command line */
97 NULL, /* process security attributes */
98 NULL, /* primary thread security attributes */
99 FALSE, /* inherit handles */
100 0, /* creation flags */
101 NULL, /* use parent's environment */
102 NULL, /* use parent's current directory */
103 &si, /* STARTUPINFO pointer */
104 &pi /* PROCESS_INFORMATION */
109 CloseHandle(pi.hProcess);
110 CloseHandle(pi.hThread);
117 static RPC_STATUS get_epm_handle_client(RPC_BINDING_HANDLE handle, RPC_BINDING_HANDLE *epm_handle)
119 RpcBinding *bind = (RpcBinding *)handle;
120 const char * pszEndpoint = NULL;
122 RpcBinding* epm_bind;
126 return RPC_S_INVALID_BINDING;
128 for (i = 0; i < sizeof(epm_endpoints)/sizeof(epm_endpoints[0]); i++)
129 if (!strcmp(bind->Protseq, epm_endpoints[i].protseq))
130 pszEndpoint = epm_endpoints[i].endpoint;
134 FIXME("no endpoint for the endpoint-mapper found for protseq %s\n", debugstr_a(bind->Protseq));
135 return RPC_S_PROTSEQ_NOT_SUPPORTED;
138 status = RpcBindingCopy(handle, epm_handle);
139 if (status != RPC_S_OK) return status;
141 epm_bind = (RpcBinding*)*epm_handle;
142 if (epm_bind->AuthInfo)
144 /* don't bother with authenticating against the EPM by default
145 * (see EnableAuthEpResolution registry value) */
146 RpcAuthInfo_Release(epm_bind->AuthInfo);
147 epm_bind->AuthInfo = NULL;
149 RPCRT4_ResolveBinding(epm_bind, pszEndpoint);
154 static RPC_STATUS get_epm_handle_server(RPC_BINDING_HANDLE *epm_handle)
156 unsigned char string_binding[] = "ncacn_np:.[\\pipe\\epmapper]";
158 return RpcBindingFromStringBindingA(string_binding, epm_handle);
161 static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *__eptr)
163 switch (GetExceptionCode())
165 case EXCEPTION_ACCESS_VIOLATION:
166 case EXCEPTION_ILLEGAL_INSTRUCTION:
167 return EXCEPTION_CONTINUE_SEARCH;
169 return EXCEPTION_EXECUTE_HANDLER;
173 /***********************************************************************
174 * RpcEpRegisterA (RPCRT4.@)
176 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
177 UUID_VECTOR *UuidVector, RPC_CSTR Annotation )
179 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
181 RPC_STATUS status = RPC_S_OK;
182 error_status_t status2;
183 ept_entry_t *entries;
186 TRACE("(%p,%p,%p,%s)\n", IfSpec, BindingVector, UuidVector, debugstr_a((char*)Annotation));
187 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
188 for (i=0; i<BindingVector->Count; i++) {
189 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[i]);
190 TRACE(" protseq[%ld]=%s\n", i, debugstr_a(bind->Protseq));
191 TRACE(" endpoint[%ld]=%s\n", i, debugstr_a(bind->Endpoint));
194 for (i=0; i<UuidVector->Count; i++)
195 TRACE(" obj[%ld]=%s\n", i, debugstr_guid(UuidVector->Uuid[i]));
198 if (!BindingVector->Count) return RPC_S_OK;
200 entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*entries) * BindingVector->Count * (UuidVector ? UuidVector->Count : 1));
202 return RPC_S_OUT_OF_MEMORY;
204 status = get_epm_handle_server(&handle);
205 if (status != RPC_S_OK)
207 HeapFree(GetProcessHeap(), 0, entries);
211 for (i = 0; i < BindingVector->Count; i++)
214 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[i]);
215 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
217 int len = strlen((char *)Annotation);
218 status = TowerConstruct(&If->InterfaceId, &If->TransferSyntax,
219 bind->Protseq, bind->Endpoint,
221 &entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
222 if (status != RPC_S_OK) break;
225 memcpy(&entries[i * UuidVector->Count].object, &UuidVector->Uuid[j], sizeof(GUID));
227 memset(&entries[i].object, 0, sizeof(entries[i].object));
228 memcpy(entries[i].annotation, Annotation, min(len + 1, ept_max_annotation_size));
232 if (status == RPC_S_OK)
238 ept_insert(handle, BindingVector->Count * (UuidVector ? UuidVector->Count : 1),
239 entries, TRUE, &status2);
243 status2 = GetExceptionCode();
246 if (status2 == RPC_S_SERVER_UNAVAILABLE)
251 if (status2 != RPC_S_OK)
252 ERR("ept_insert failed with error %d\n", status2);
253 status = status2; /* FIXME: convert status? */
257 RpcBindingFree(&handle);
259 for (i = 0; i < BindingVector->Count; i++)
262 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
263 I_RpcFree(entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
266 HeapFree(GetProcessHeap(), 0, entries);
271 /***********************************************************************
272 * RpcEpUnregister (RPCRT4.@)
274 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
275 UUID_VECTOR *UuidVector )
277 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
279 RPC_STATUS status = RPC_S_OK;
280 error_status_t status2;
281 ept_entry_t *entries;
284 TRACE("(%p,%p,%p)\n", IfSpec, BindingVector, UuidVector);
285 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
286 for (i=0; i<BindingVector->Count; i++) {
287 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[i]);
288 TRACE(" protseq[%ld]=%s\n", i, debugstr_a(bind->Protseq));
289 TRACE(" endpoint[%ld]=%s\n", i, debugstr_a(bind->Endpoint));
292 for (i=0; i<UuidVector->Count; i++)
293 TRACE(" obj[%ld]=%s\n", i, debugstr_guid(UuidVector->Uuid[i]));
296 entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*entries) * BindingVector->Count * (UuidVector ? UuidVector->Count : 1));
298 return RPC_S_OUT_OF_MEMORY;
300 status = get_epm_handle_server(&handle);
301 if (status != RPC_S_OK)
303 HeapFree(GetProcessHeap(), 0, entries);
307 for (i = 0; i < BindingVector->Count; i++)
310 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[i]);
311 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
313 status = TowerConstruct(&If->InterfaceId, &If->TransferSyntax,
314 bind->Protseq, bind->Endpoint,
316 &entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
317 if (status != RPC_S_OK) break;
320 memcpy(&entries[i * UuidVector->Count + j].object, &UuidVector->Uuid[j], sizeof(GUID));
322 memset(&entries[i].object, 0, sizeof(entries[i].object));
326 if (status == RPC_S_OK)
330 ept_insert(handle, BindingVector->Count * (UuidVector ? UuidVector->Count : 1),
331 entries, TRUE, &status2);
335 status2 = GetExceptionCode();
338 if (status2 == RPC_S_SERVER_UNAVAILABLE)
339 status2 = EPT_S_NOT_REGISTERED;
340 if (status2 != RPC_S_OK)
341 ERR("ept_insert failed with error %d\n", status2);
342 status = status2; /* FIXME: convert status? */
344 RpcBindingFree(&handle);
346 for (i = 0; i < BindingVector->Count; i++)
349 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
350 I_RpcFree(entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
353 HeapFree(GetProcessHeap(), 0, entries);
358 /***********************************************************************
359 * RpcEpResolveBinding (RPCRT4.@)
361 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
363 PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
364 RpcBinding* bind = (RpcBinding*)Binding;
366 error_status_t status2;
368 ept_lookup_handle_t entry_handle = NULL;
370 twr_t *towers[4] = { NULL };
371 unsigned32 num_towers, i;
372 GUID uuid = GUID_NULL;
373 char *resolved_endpoint = NULL;
375 TRACE("(%p,%p)\n", Binding, IfSpec);
376 TRACE(" protseq=%s\n", debugstr_a(bind->Protseq));
377 TRACE(" obj=%s\n", debugstr_guid(&bind->ObjectUuid));
378 TRACE(" networkaddr=%s\n", debugstr_a(bind->NetworkAddr));
379 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
381 /* just return for fully bound handles */
382 if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
385 status = get_epm_handle_client(Binding, &handle);
386 if (status != RPC_S_OK) return status;
388 status = TowerConstruct(&If->InterfaceId, &If->TransferSyntax, bind->Protseq,
389 ((RpcBinding *)handle)->Endpoint,
390 bind->NetworkAddr, &tower);
391 if (status != RPC_S_OK)
393 WARN("couldn't get tower\n");
394 RpcBindingFree(&handle);
402 ept_map(handle, &uuid, tower, &entry_handle, sizeof(towers)/sizeof(towers[0]), &num_towers, towers, &status2);
403 /* FIXME: translate status2? */
407 status2 = GetExceptionCode();
410 if (status2 == RPC_S_SERVER_UNAVAILABLE)
418 RpcBindingFree(&handle);
421 if (status2 != RPC_S_OK)
423 ERR("ept_map failed for ifid %s, protseq %s, networkaddr %s\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID), bind->Protseq, bind->NetworkAddr);
427 for (i = 0; i < num_towers; i++)
429 /* only parse the tower if we haven't already found a suitable
430 * endpoint, otherwise just free the tower */
431 if (!resolved_endpoint)
433 status = TowerExplode(towers[i], NULL, NULL, NULL, &resolved_endpoint, NULL);
434 TRACE("status = %ld\n", status);
436 I_RpcFree(towers[i]);
439 if (resolved_endpoint)
441 RPCRT4_ResolveBinding(Binding, resolved_endpoint);
442 I_RpcFree(resolved_endpoint);
446 WARN("couldn't find an endpoint\n");
447 return EPT_S_NOT_REGISTERED;
450 /*****************************************************************************
451 * TowerExplode (RPCRT4.@)
453 RPC_STATUS WINAPI TowerExplode(
454 const twr_t *tower, PRPC_SYNTAX_IDENTIFIER object, PRPC_SYNTAX_IDENTIFIER syntax,
455 char **protseq, char **endpoint, char **address)
459 const unsigned char *p;
461 const twr_uuid_floor_t *object_floor;
462 const twr_uuid_floor_t *syntax_floor;
464 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower, object, syntax, protseq,
474 tower_size = tower->tower_length;
476 if (tower_size < sizeof(u_int16))
477 return EPT_S_NOT_REGISTERED;
479 p = &tower->tower_octet_string[0];
481 floor_count = *(const u_int16 *)p;
482 p += sizeof(u_int16);
483 tower_size -= sizeof(u_int16);
484 TRACE("floor_count: %d\n", floor_count);
485 /* FIXME: should we do something with the floor count? at the moment we don't */
487 if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
488 return EPT_S_NOT_REGISTERED;
490 object_floor = (const twr_uuid_floor_t *)p;
491 p += sizeof(*object_floor);
492 tower_size -= sizeof(*object_floor);
493 syntax_floor = (const twr_uuid_floor_t *)p;
494 p += sizeof(*syntax_floor);
495 tower_size -= sizeof(*syntax_floor);
497 if ((object_floor->count_lhs != sizeof(object_floor->protid) +
498 sizeof(object_floor->uuid) + sizeof(object_floor->major_version)) ||
499 (object_floor->protid != EPM_PROTOCOL_UUID) ||
500 (object_floor->count_rhs != sizeof(object_floor->minor_version)))
501 return EPT_S_NOT_REGISTERED;
503 if ((syntax_floor->count_lhs != sizeof(syntax_floor->protid) +
504 sizeof(syntax_floor->uuid) + sizeof(syntax_floor->major_version)) ||
505 (syntax_floor->protid != EPM_PROTOCOL_UUID) ||
506 (syntax_floor->count_rhs != sizeof(syntax_floor->minor_version)))
507 return EPT_S_NOT_REGISTERED;
509 status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
510 if ((status == RPC_S_OK) && syntax && object)
512 syntax->SyntaxGUID = syntax_floor->uuid;
513 syntax->SyntaxVersion.MajorVersion = syntax_floor->major_version;
514 syntax->SyntaxVersion.MinorVersion = syntax_floor->minor_version;
515 object->SyntaxGUID = object_floor->uuid;
516 object->SyntaxVersion.MajorVersion = object_floor->major_version;
517 object->SyntaxVersion.MinorVersion = object_floor->minor_version;
522 /***********************************************************************
523 * TowerConstruct (RPCRT4.@)
525 RPC_STATUS WINAPI TowerConstruct(
526 const RPC_SYNTAX_IDENTIFIER *object, const RPC_SYNTAX_IDENTIFIER *syntax,
527 const char *protseq, const char *endpoint, const char *address,
533 twr_uuid_floor_t *object_floor;
534 twr_uuid_floor_t *syntax_floor;
536 TRACE("(%p, %p, %s, %s, %s, %p)\n", object, syntax, debugstr_a(protseq),
537 debugstr_a(endpoint), debugstr_a(address), tower);
541 status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
543 if (status != RPC_S_OK)
546 tower_size += sizeof(u_int16) + sizeof(*object_floor) + sizeof(*syntax_floor);
547 *tower = I_RpcAllocate(FIELD_OFFSET(twr_t, tower_octet_string[tower_size]));
549 return RPC_S_OUT_OF_RESOURCES;
551 (*tower)->tower_length = tower_size;
552 p = &(*tower)->tower_octet_string[0];
553 *(u_int16 *)p = 5; /* number of floors */
554 p += sizeof(u_int16);
555 object_floor = (twr_uuid_floor_t *)p;
556 p += sizeof(*object_floor);
557 syntax_floor = (twr_uuid_floor_t *)p;
558 p += sizeof(*syntax_floor);
560 object_floor->count_lhs = sizeof(object_floor->protid) + sizeof(object_floor->uuid) +
561 sizeof(object_floor->major_version);
562 object_floor->protid = EPM_PROTOCOL_UUID;
563 object_floor->count_rhs = sizeof(object_floor->minor_version);
564 object_floor->uuid = object->SyntaxGUID;
565 object_floor->major_version = object->SyntaxVersion.MajorVersion;
566 object_floor->minor_version = object->SyntaxVersion.MinorVersion;
568 syntax_floor->count_lhs = sizeof(syntax_floor->protid) + sizeof(syntax_floor->uuid) +
569 sizeof(syntax_floor->major_version);
570 syntax_floor->protid = EPM_PROTOCOL_UUID;
571 syntax_floor->count_rhs = sizeof(syntax_floor->minor_version);
572 syntax_floor->uuid = syntax->SyntaxGUID;
573 syntax_floor->major_version = syntax->SyntaxVersion.MajorVersion;
574 syntax_floor->minor_version = syntax->SyntaxVersion.MinorVersion;
576 status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
577 if (status != RPC_S_OK)
586 void __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t len)
588 return HeapAlloc(GetProcessHeap(), 0, len);
591 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
593 HeapFree(GetProcessHeap(), 0, ptr);