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 { "ncalrpc", "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','.','e','x','e',0};
89 ZeroMemory(&si
, sizeof(STARTUPINFOA
));
90 si
.cb
= sizeof(STARTUPINFOA
);
91 GetSystemDirectoryW( cmd
, MAX_PATH
- sizeof(rpcss
)/sizeof(WCHAR
) );
92 lstrcatW( cmd
, rpcss
);
94 Wow64DisableWow64FsRedirection( &redir
);
95 rslt
= CreateProcessW( cmd
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
96 Wow64RevertWow64FsRedirection( redir
);
100 CloseHandle(pi
.hProcess
);
101 CloseHandle(pi
.hThread
);
108 static inline BOOL
is_epm_destination_local(RPC_BINDING_HANDLE handle
)
110 RpcBinding
*bind
= handle
;
111 const char *protseq
= bind
->Protseq
;
112 const char *network_addr
= bind
->NetworkAddr
;
114 return (!strcmp(protseq
, "ncalrpc") ||
115 (!strcmp(protseq
, "ncacn_np") &&
116 (!network_addr
|| !strcmp(network_addr
, "."))));
119 static RPC_STATUS
get_epm_handle_client(RPC_BINDING_HANDLE handle
, RPC_BINDING_HANDLE
*epm_handle
)
121 RpcBinding
*bind
= handle
;
122 const char * pszEndpoint
= NULL
;
124 RpcBinding
* epm_bind
;
128 return RPC_S_INVALID_BINDING
;
130 for (i
= 0; i
< sizeof(epm_endpoints
)/sizeof(epm_endpoints
[0]); i
++)
131 if (!strcmp(bind
->Protseq
, epm_endpoints
[i
].protseq
))
132 pszEndpoint
= epm_endpoints
[i
].endpoint
;
136 FIXME("no endpoint for the endpoint-mapper found for protseq %s\n", debugstr_a(bind
->Protseq
));
137 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
140 status
= RpcBindingCopy(handle
, epm_handle
);
141 if (status
!= RPC_S_OK
) return status
;
143 epm_bind
= *epm_handle
;
144 if (epm_bind
->AuthInfo
)
146 /* don't bother with authenticating against the EPM by default
147 * (see EnableAuthEpResolution registry value) */
148 RpcAuthInfo_Release(epm_bind
->AuthInfo
);
149 epm_bind
->AuthInfo
= NULL
;
151 RPCRT4_ResolveBinding(epm_bind
, pszEndpoint
);
156 static RPC_STATUS
get_epm_handle_server(RPC_BINDING_HANDLE
*epm_handle
)
158 unsigned char string_binding
[] = "ncacn_np:.[\\\\pipe\\\\epmapper]";
160 return RpcBindingFromStringBindingA(string_binding
, epm_handle
);
163 static LONG WINAPI
rpc_filter(EXCEPTION_POINTERS
*__eptr
)
165 switch (GetExceptionCode())
167 case EXCEPTION_ACCESS_VIOLATION
:
168 case EXCEPTION_ILLEGAL_INSTRUCTION
:
169 return EXCEPTION_CONTINUE_SEARCH
;
171 return EXCEPTION_EXECUTE_HANDLER
;
175 static RPC_STATUS
epm_register( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
176 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
, BOOL replace
)
178 PRPC_SERVER_INTERFACE If
= IfSpec
;
180 RPC_STATUS status
= RPC_S_OK
;
181 error_status_t status2
;
182 ept_entry_t
*entries
;
185 TRACE("(%p,%p,%p,%s) replace=%d\n", IfSpec
, BindingVector
, UuidVector
, debugstr_a((char*)Annotation
), replace
);
186 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
187 for (i
=0; i
<BindingVector
->Count
; i
++) {
188 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
189 TRACE(" protseq[%d]=%s\n", i
, debugstr_a(bind
->Protseq
));
190 TRACE(" endpoint[%d]=%s\n", i
, debugstr_a(bind
->Endpoint
));
193 for (i
=0; i
<UuidVector
->Count
; i
++)
194 TRACE(" obj[%d]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
197 if (!BindingVector
->Count
) return RPC_S_OK
;
199 entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*entries
) * BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1));
201 return RPC_S_OUT_OF_MEMORY
;
203 status
= get_epm_handle_server(&handle
);
204 if (status
!= RPC_S_OK
)
206 HeapFree(GetProcessHeap(), 0, entries
);
210 for (i
= 0; i
< BindingVector
->Count
; i
++)
213 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
214 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
216 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
217 bind
->Protseq
, bind
->Endpoint
,
219 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
220 if (status
!= RPC_S_OK
) break;
223 memcpy(&entries
[i
* UuidVector
->Count
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
225 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
227 memcpy(entries
[i
].annotation
, Annotation
,
228 min(strlen((char *)Annotation
) + 1, ept_max_annotation_size
));
232 if (status
== RPC_S_OK
)
238 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
239 entries
, replace
, &status2
);
243 status2
= GetExceptionCode();
246 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
247 is_epm_destination_local(handle
))
252 if (status2
!= RPC_S_OK
)
253 ERR("ept_insert failed with error %d\n", status2
);
254 status
= status2
; /* FIXME: convert status? */
258 RpcBindingFree(&handle
);
260 for (i
= 0; i
< BindingVector
->Count
; i
++)
263 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
264 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
267 HeapFree(GetProcessHeap(), 0, entries
);
272 /***********************************************************************
273 * RpcEpRegisterA (RPCRT4.@)
275 RPC_STATUS WINAPI
RpcEpRegisterA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
276 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
278 return epm_register(IfSpec
, BindingVector
, UuidVector
, Annotation
, TRUE
);
281 /***********************************************************************
282 * RpcEpRegisterNoReplaceA (RPCRT4.@)
284 RPC_STATUS WINAPI
RpcEpRegisterNoReplaceA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
285 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
287 return epm_register(IfSpec
, BindingVector
, UuidVector
, Annotation
, FALSE
);
290 /***********************************************************************
291 * RpcEpRegisterW (RPCRT4.@)
293 RPC_STATUS WINAPI
RpcEpRegisterW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
294 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
296 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
299 status
= epm_register(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
, TRUE
);
301 HeapFree(GetProcessHeap(), 0, annA
);
305 /***********************************************************************
306 * RpcEpRegisterNoReplaceW (RPCRT4.@)
308 RPC_STATUS WINAPI
RpcEpRegisterNoReplaceW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
309 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
311 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
314 status
= epm_register(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
, FALSE
);
316 HeapFree(GetProcessHeap(), 0, annA
);
320 /***********************************************************************
321 * RpcEpUnregister (RPCRT4.@)
323 RPC_STATUS WINAPI
RpcEpUnregister( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
324 UUID_VECTOR
*UuidVector
)
326 PRPC_SERVER_INTERFACE If
= IfSpec
;
328 RPC_STATUS status
= RPC_S_OK
;
329 error_status_t status2
;
330 ept_entry_t
*entries
;
333 TRACE("(%p,%p,%p)\n", IfSpec
, BindingVector
, UuidVector
);
334 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
335 for (i
=0; i
<BindingVector
->Count
; i
++) {
336 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
337 TRACE(" protseq[%d]=%s\n", i
, debugstr_a(bind
->Protseq
));
338 TRACE(" endpoint[%d]=%s\n", i
, debugstr_a(bind
->Endpoint
));
341 for (i
=0; i
<UuidVector
->Count
; i
++)
342 TRACE(" obj[%d]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
345 entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*entries
) * BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1));
347 return RPC_S_OUT_OF_MEMORY
;
349 status
= get_epm_handle_server(&handle
);
350 if (status
!= RPC_S_OK
)
352 HeapFree(GetProcessHeap(), 0, entries
);
356 for (i
= 0; i
< BindingVector
->Count
; i
++)
359 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
360 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
362 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
363 bind
->Protseq
, bind
->Endpoint
,
365 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
366 if (status
!= RPC_S_OK
) break;
369 memcpy(&entries
[i
* UuidVector
->Count
+ j
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
371 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
375 if (status
== RPC_S_OK
)
379 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
380 entries
, TRUE
, &status2
);
384 status2
= GetExceptionCode();
387 if (status2
== RPC_S_SERVER_UNAVAILABLE
)
388 status2
= EPT_S_NOT_REGISTERED
;
389 if (status2
!= RPC_S_OK
)
390 ERR("ept_insert failed with error %d\n", status2
);
391 status
= status2
; /* FIXME: convert status? */
393 RpcBindingFree(&handle
);
395 for (i
= 0; i
< BindingVector
->Count
; i
++)
398 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
399 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
402 HeapFree(GetProcessHeap(), 0, entries
);
407 /***********************************************************************
408 * RpcEpResolveBinding (RPCRT4.@)
410 RPC_STATUS WINAPI
RpcEpResolveBinding( RPC_BINDING_HANDLE Binding
, RPC_IF_HANDLE IfSpec
)
412 PRPC_CLIENT_INTERFACE If
= IfSpec
;
413 RpcBinding
* bind
= Binding
;
415 error_status_t status2
;
417 ept_lookup_handle_t entry_handle
= NULL
;
419 twr_t
*towers
[4] = { NULL
};
420 unsigned32 num_towers
, i
;
421 GUID uuid
= GUID_NULL
;
422 char *resolved_endpoint
= NULL
;
424 TRACE("(%p,%p)\n", Binding
, IfSpec
);
425 TRACE(" protseq=%s\n", debugstr_a(bind
->Protseq
));
426 TRACE(" obj=%s\n", debugstr_guid(&bind
->ObjectUuid
));
427 TRACE(" networkaddr=%s\n", debugstr_a(bind
->NetworkAddr
));
428 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
430 /* just return for fully bound handles */
431 if (bind
->Endpoint
&& (bind
->Endpoint
[0] != '\0'))
434 status
= get_epm_handle_client(Binding
, &handle
);
435 if (status
!= RPC_S_OK
) return status
;
437 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
, bind
->Protseq
,
438 ((RpcBinding
*)handle
)->Endpoint
,
439 bind
->NetworkAddr
, &tower
);
440 if (status
!= RPC_S_OK
)
442 WARN("couldn't get tower\n");
443 RpcBindingFree(&handle
);
451 ept_map(handle
, &uuid
, tower
, &entry_handle
, sizeof(towers
)/sizeof(towers
[0]), &num_towers
, towers
, &status2
);
452 /* FIXME: translate status2? */
456 status2
= GetExceptionCode();
459 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
460 is_epm_destination_local(handle
))
468 RpcBindingFree(&handle
);
471 if (status2
!= RPC_S_OK
)
473 ERR("ept_map failed for ifid %s, protseq %s, networkaddr %s\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
), bind
->Protseq
, bind
->NetworkAddr
);
477 for (i
= 0; i
< num_towers
; i
++)
479 /* only parse the tower if we haven't already found a suitable
480 * endpoint, otherwise just free the tower */
481 if (!resolved_endpoint
)
483 status
= TowerExplode(towers
[i
], NULL
, NULL
, NULL
, &resolved_endpoint
, NULL
);
484 TRACE("status = %d\n", status
);
486 I_RpcFree(towers
[i
]);
489 if (resolved_endpoint
)
491 RPCRT4_ResolveBinding(Binding
, resolved_endpoint
);
492 I_RpcFree(resolved_endpoint
);
496 WARN("couldn't find an endpoint\n");
497 return EPT_S_NOT_REGISTERED
;
500 /*****************************************************************************
501 * TowerExplode (RPCRT4.@)
503 RPC_STATUS WINAPI
TowerExplode(
504 const twr_t
*tower
, PRPC_SYNTAX_IDENTIFIER object
, PRPC_SYNTAX_IDENTIFIER syntax
,
505 char **protseq
, char **endpoint
, char **address
)
509 const unsigned char *p
;
511 const twr_uuid_floor_t
*object_floor
;
512 const twr_uuid_floor_t
*syntax_floor
;
514 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower
, object
, syntax
, protseq
,
524 tower_size
= tower
->tower_length
;
526 if (tower_size
< sizeof(u_int16
))
527 return EPT_S_NOT_REGISTERED
;
529 p
= &tower
->tower_octet_string
[0];
531 floor_count
= *(const u_int16
*)p
;
532 p
+= sizeof(u_int16
);
533 tower_size
-= sizeof(u_int16
);
534 TRACE("floor_count: %d\n", floor_count
);
535 /* FIXME: should we do something with the floor count? at the moment we don't */
537 if (tower_size
< sizeof(*object_floor
) + sizeof(*syntax_floor
))
538 return EPT_S_NOT_REGISTERED
;
540 object_floor
= (const twr_uuid_floor_t
*)p
;
541 p
+= sizeof(*object_floor
);
542 tower_size
-= sizeof(*object_floor
);
543 syntax_floor
= (const twr_uuid_floor_t
*)p
;
544 p
+= sizeof(*syntax_floor
);
545 tower_size
-= sizeof(*syntax_floor
);
547 if ((object_floor
->count_lhs
!= sizeof(object_floor
->protid
) +
548 sizeof(object_floor
->uuid
) + sizeof(object_floor
->major_version
)) ||
549 (object_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
550 (object_floor
->count_rhs
!= sizeof(object_floor
->minor_version
)))
551 return EPT_S_NOT_REGISTERED
;
553 if ((syntax_floor
->count_lhs
!= sizeof(syntax_floor
->protid
) +
554 sizeof(syntax_floor
->uuid
) + sizeof(syntax_floor
->major_version
)) ||
555 (syntax_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
556 (syntax_floor
->count_rhs
!= sizeof(syntax_floor
->minor_version
)))
557 return EPT_S_NOT_REGISTERED
;
559 status
= RpcTransport_ParseTopOfTower(p
, tower_size
, protseq
, address
, endpoint
);
560 if ((status
== RPC_S_OK
) && syntax
&& object
)
562 syntax
->SyntaxGUID
= syntax_floor
->uuid
;
563 syntax
->SyntaxVersion
.MajorVersion
= syntax_floor
->major_version
;
564 syntax
->SyntaxVersion
.MinorVersion
= syntax_floor
->minor_version
;
565 object
->SyntaxGUID
= object_floor
->uuid
;
566 object
->SyntaxVersion
.MajorVersion
= object_floor
->major_version
;
567 object
->SyntaxVersion
.MinorVersion
= object_floor
->minor_version
;
572 /***********************************************************************
573 * TowerConstruct (RPCRT4.@)
575 RPC_STATUS WINAPI
TowerConstruct(
576 const RPC_SYNTAX_IDENTIFIER
*object
, const RPC_SYNTAX_IDENTIFIER
*syntax
,
577 const char *protseq
, const char *endpoint
, const char *address
,
583 twr_uuid_floor_t
*object_floor
;
584 twr_uuid_floor_t
*syntax_floor
;
586 TRACE("(%p, %p, %s, %s, %s, %p)\n", object
, syntax
, debugstr_a(protseq
),
587 debugstr_a(endpoint
), debugstr_a(address
), tower
);
591 status
= RpcTransport_GetTopOfTower(NULL
, &tower_size
, protseq
, address
, endpoint
);
593 if (status
!= RPC_S_OK
)
596 tower_size
+= sizeof(u_int16
) + sizeof(*object_floor
) + sizeof(*syntax_floor
);
597 *tower
= I_RpcAllocate(FIELD_OFFSET(twr_t
, tower_octet_string
[tower_size
]));
599 return RPC_S_OUT_OF_RESOURCES
;
601 (*tower
)->tower_length
= tower_size
;
602 p
= &(*tower
)->tower_octet_string
[0];
603 *(u_int16
*)p
= 5; /* number of floors */
604 p
+= sizeof(u_int16
);
605 object_floor
= (twr_uuid_floor_t
*)p
;
606 p
+= sizeof(*object_floor
);
607 syntax_floor
= (twr_uuid_floor_t
*)p
;
608 p
+= sizeof(*syntax_floor
);
610 object_floor
->count_lhs
= sizeof(object_floor
->protid
) + sizeof(object_floor
->uuid
) +
611 sizeof(object_floor
->major_version
);
612 object_floor
->protid
= EPM_PROTOCOL_UUID
;
613 object_floor
->count_rhs
= sizeof(object_floor
->minor_version
);
614 object_floor
->uuid
= object
->SyntaxGUID
;
615 object_floor
->major_version
= object
->SyntaxVersion
.MajorVersion
;
616 object_floor
->minor_version
= object
->SyntaxVersion
.MinorVersion
;
618 syntax_floor
->count_lhs
= sizeof(syntax_floor
->protid
) + sizeof(syntax_floor
->uuid
) +
619 sizeof(syntax_floor
->major_version
);
620 syntax_floor
->protid
= EPM_PROTOCOL_UUID
;
621 syntax_floor
->count_rhs
= sizeof(syntax_floor
->minor_version
);
622 syntax_floor
->uuid
= syntax
->SyntaxGUID
;
623 syntax_floor
->major_version
= syntax
->SyntaxVersion
.MajorVersion
;
624 syntax_floor
->minor_version
= syntax
->SyntaxVersion
.MinorVersion
;
626 status
= RpcTransport_GetTopOfTower(p
, &tower_size
, protseq
, address
, endpoint
);
627 if (status
!= RPC_S_OK
)
636 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
638 return HeapAlloc(GetProcessHeap(), 0, len
);
641 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
643 HeapFree(GetProcessHeap(), 0, ptr
);