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
24 #include "epm_towers.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
28 /* The "real" RPC portmapper endpoints that I know of are:
32 * ncacn_np: \\pipe\epmapper
36 * If the user's machine ran a DCE RPC daemon, it would
37 * probably be possible to connect to it, but there are many
38 * reasons not to, like:
39 * - the user probably does *not* run one, and probably
40 * shouldn't be forced to run one just for local COM
41 * - very few Unix systems use DCE RPC... if they run a RPC
42 * daemon at all, it's usually Sun RPC
43 * - DCE RPC registrations are persistent and saved on disk,
44 * while MS-RPC registrations are documented as non-persistent
45 * and stored only in RAM, and auto-destroyed when the process
46 * dies (something DCE RPC can't do)
48 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
49 * there would be interoperability advantages, like the possibility
50 * of running a fully functional DCOM server using Wine...
53 static const struct epm_endpoints
59 { "ncacn_np", "\\pipe\\epmapper" },
60 { "ncacn_ip_tcp", "135" },
61 { "ncacn_ip_udp", "135" },
62 { "ncalrpc", "epmapper" },
63 { "ncacn_http", "593" },
66 static BOOL
start_rpcss(void)
68 PROCESS_INFORMATION pi
;
71 static const WCHAR rpcss
[] = {'\\','r','p','c','s','s','.','e','x','e',0};
77 ZeroMemory(&si
, sizeof(STARTUPINFOA
));
78 si
.cb
= sizeof(STARTUPINFOA
);
79 GetSystemDirectoryW( cmd
, MAX_PATH
- sizeof(rpcss
)/sizeof(WCHAR
) );
80 lstrcatW( cmd
, rpcss
);
82 Wow64DisableWow64FsRedirection( &redir
);
83 rslt
= CreateProcessW( cmd
, cmd
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
);
84 Wow64RevertWow64FsRedirection( redir
);
88 CloseHandle(pi
.hProcess
);
89 CloseHandle(pi
.hThread
);
96 static inline BOOL
is_epm_destination_local(RPC_BINDING_HANDLE handle
)
98 RpcBinding
*bind
= handle
;
99 const char *protseq
= bind
->Protseq
;
100 const char *network_addr
= bind
->NetworkAddr
;
102 return (!strcmp(protseq
, "ncalrpc") ||
103 (!strcmp(protseq
, "ncacn_np") &&
104 (!network_addr
|| !strcmp(network_addr
, "."))));
107 static RPC_STATUS
get_epm_handle_client(RPC_BINDING_HANDLE handle
, RPC_BINDING_HANDLE
*epm_handle
)
109 RpcBinding
*bind
= handle
;
110 const char * pszEndpoint
= NULL
;
112 RpcBinding
* epm_bind
;
116 return RPC_S_INVALID_BINDING
;
118 for (i
= 0; i
< sizeof(epm_endpoints
)/sizeof(epm_endpoints
[0]); i
++)
119 if (!strcmp(bind
->Protseq
, epm_endpoints
[i
].protseq
))
120 pszEndpoint
= epm_endpoints
[i
].endpoint
;
124 FIXME("no endpoint for the endpoint-mapper found for protseq %s\n", debugstr_a(bind
->Protseq
));
125 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
128 status
= RpcBindingCopy(handle
, epm_handle
);
129 if (status
!= RPC_S_OK
) return status
;
131 epm_bind
= *epm_handle
;
132 if (epm_bind
->AuthInfo
)
134 /* don't bother with authenticating against the EPM by default
135 * (see EnableAuthEpResolution registry value) */
136 RpcAuthInfo_Release(epm_bind
->AuthInfo
);
137 epm_bind
->AuthInfo
= NULL
;
139 RPCRT4_ResolveBinding(epm_bind
, pszEndpoint
);
144 static RPC_STATUS
get_epm_handle_server(RPC_BINDING_HANDLE
*epm_handle
)
146 unsigned char string_binding
[] = "ncacn_np:.[\\\\pipe\\\\epmapper]";
148 return RpcBindingFromStringBindingA(string_binding
, epm_handle
);
151 static LONG WINAPI
rpc_filter(EXCEPTION_POINTERS
*__eptr
)
153 switch (__eptr
->ExceptionRecord
->ExceptionCode
)
155 case EXCEPTION_ACCESS_VIOLATION
:
156 case EXCEPTION_ILLEGAL_INSTRUCTION
:
157 return EXCEPTION_CONTINUE_SEARCH
;
159 return EXCEPTION_EXECUTE_HANDLER
;
163 static RPC_STATUS
epm_register( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
164 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
, BOOL replace
)
166 PRPC_SERVER_INTERFACE If
= IfSpec
;
168 RPC_STATUS status
= RPC_S_OK
;
169 error_status_t status2
;
170 ept_entry_t
*entries
;
173 TRACE("(%p,%p,%p,%s) replace=%d\n", IfSpec
, BindingVector
, UuidVector
, debugstr_a((char*)Annotation
), replace
);
174 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
175 for (i
=0; i
<BindingVector
->Count
; i
++) {
176 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
177 TRACE(" protseq[%d]=%s\n", i
, debugstr_a(bind
->Protseq
));
178 TRACE(" endpoint[%d]=%s\n", i
, debugstr_a(bind
->Endpoint
));
181 for (i
=0; i
<UuidVector
->Count
; i
++)
182 TRACE(" obj[%d]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
185 if (!BindingVector
->Count
) return RPC_S_OK
;
187 entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*entries
) * BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1));
189 return RPC_S_OUT_OF_MEMORY
;
191 status
= get_epm_handle_server(&handle
);
192 if (status
!= RPC_S_OK
)
194 HeapFree(GetProcessHeap(), 0, entries
);
198 for (i
= 0; i
< BindingVector
->Count
; i
++)
201 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
202 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
204 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
205 bind
->Protseq
, bind
->Endpoint
,
207 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
208 if (status
!= RPC_S_OK
) break;
211 memcpy(&entries
[i
* UuidVector
->Count
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
213 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
215 memcpy(entries
[i
].annotation
, Annotation
,
216 min(strlen((char *)Annotation
) + 1, ept_max_annotation_size
));
220 if (status
== RPC_S_OK
)
226 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
227 entries
, replace
, &status2
);
231 status2
= GetExceptionCode();
234 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
235 is_epm_destination_local(handle
))
240 if (status2
!= RPC_S_OK
)
241 ERR("ept_insert failed with error %d\n", status2
);
242 status
= status2
; /* FIXME: convert status? */
246 RpcBindingFree(&handle
);
248 for (i
= 0; i
< BindingVector
->Count
; i
++)
251 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
252 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
255 HeapFree(GetProcessHeap(), 0, entries
);
260 /***********************************************************************
261 * RpcEpRegisterA (RPCRT4.@)
263 RPC_STATUS WINAPI
RpcEpRegisterA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
264 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
266 return epm_register(IfSpec
, BindingVector
, UuidVector
, Annotation
, TRUE
);
269 /***********************************************************************
270 * RpcEpRegisterNoReplaceA (RPCRT4.@)
272 RPC_STATUS WINAPI
RpcEpRegisterNoReplaceA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
273 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
275 return epm_register(IfSpec
, BindingVector
, UuidVector
, Annotation
, FALSE
);
278 /***********************************************************************
279 * RpcEpRegisterW (RPCRT4.@)
281 RPC_STATUS WINAPI
RpcEpRegisterW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
282 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
284 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
287 status
= epm_register(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
, TRUE
);
289 HeapFree(GetProcessHeap(), 0, annA
);
293 /***********************************************************************
294 * RpcEpRegisterNoReplaceW (RPCRT4.@)
296 RPC_STATUS WINAPI
RpcEpRegisterNoReplaceW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
297 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
299 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
302 status
= epm_register(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
, FALSE
);
304 HeapFree(GetProcessHeap(), 0, annA
);
308 /***********************************************************************
309 * RpcEpUnregister (RPCRT4.@)
311 RPC_STATUS WINAPI
RpcEpUnregister( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
312 UUID_VECTOR
*UuidVector
)
314 PRPC_SERVER_INTERFACE If
= IfSpec
;
316 RPC_STATUS status
= RPC_S_OK
;
317 error_status_t status2
;
318 ept_entry_t
*entries
;
321 TRACE("(%p,%p,%p)\n", IfSpec
, BindingVector
, UuidVector
);
322 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
323 for (i
=0; i
<BindingVector
->Count
; i
++) {
324 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
325 TRACE(" protseq[%d]=%s\n", i
, debugstr_a(bind
->Protseq
));
326 TRACE(" endpoint[%d]=%s\n", i
, debugstr_a(bind
->Endpoint
));
329 for (i
=0; i
<UuidVector
->Count
; i
++)
330 TRACE(" obj[%d]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
333 entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*entries
) * BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1));
335 return RPC_S_OUT_OF_MEMORY
;
337 status
= get_epm_handle_server(&handle
);
338 if (status
!= RPC_S_OK
)
340 HeapFree(GetProcessHeap(), 0, entries
);
344 for (i
= 0; i
< BindingVector
->Count
; i
++)
347 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
348 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
350 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
351 bind
->Protseq
, bind
->Endpoint
,
353 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
354 if (status
!= RPC_S_OK
) break;
357 memcpy(&entries
[i
* UuidVector
->Count
+ j
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
359 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
363 if (status
== RPC_S_OK
)
367 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
368 entries
, TRUE
, &status2
);
372 status2
= GetExceptionCode();
375 if (status2
== RPC_S_SERVER_UNAVAILABLE
)
376 status2
= EPT_S_NOT_REGISTERED
;
377 if (status2
!= RPC_S_OK
)
378 ERR("ept_insert failed with error %d\n", status2
);
379 status
= status2
; /* FIXME: convert status? */
381 RpcBindingFree(&handle
);
383 for (i
= 0; i
< BindingVector
->Count
; i
++)
386 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
387 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
390 HeapFree(GetProcessHeap(), 0, entries
);
395 /***********************************************************************
396 * RpcEpResolveBinding (RPCRT4.@)
398 RPC_STATUS WINAPI
RpcEpResolveBinding( RPC_BINDING_HANDLE Binding
, RPC_IF_HANDLE IfSpec
)
400 PRPC_CLIENT_INTERFACE If
= IfSpec
;
401 RpcBinding
* bind
= Binding
;
403 error_status_t status2
;
405 ept_lookup_handle_t entry_handle
= NULL
;
407 twr_t
*towers
[4] = { NULL
};
408 unsigned32 num_towers
, i
;
409 GUID uuid
= GUID_NULL
;
410 char *resolved_endpoint
= NULL
;
412 TRACE("(%p,%p)\n", Binding
, IfSpec
);
413 TRACE(" protseq=%s\n", debugstr_a(bind
->Protseq
));
414 TRACE(" obj=%s\n", debugstr_guid(&bind
->ObjectUuid
));
415 TRACE(" networkaddr=%s\n", debugstr_a(bind
->NetworkAddr
));
416 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
418 /* just return for fully bound handles */
419 if (bind
->Endpoint
&& (bind
->Endpoint
[0] != '\0'))
422 status
= get_epm_handle_client(Binding
, &handle
);
423 if (status
!= RPC_S_OK
) return status
;
425 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
, bind
->Protseq
,
426 ((RpcBinding
*)handle
)->Endpoint
,
427 bind
->NetworkAddr
, &tower
);
428 if (status
!= RPC_S_OK
)
430 WARN("couldn't get tower\n");
431 RpcBindingFree(&handle
);
439 ept_map(handle
, &uuid
, tower
, &entry_handle
, sizeof(towers
)/sizeof(towers
[0]), &num_towers
, towers
, &status2
);
440 /* FIXME: translate status2? */
444 status2
= GetExceptionCode();
447 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
448 is_epm_destination_local(handle
))
456 RpcBindingFree(&handle
);
459 if (status2
!= RPC_S_OK
)
461 ERR("ept_map failed for ifid %s, protseq %s, networkaddr %s\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
), bind
->Protseq
, bind
->NetworkAddr
);
465 for (i
= 0; i
< num_towers
; i
++)
467 /* only parse the tower if we haven't already found a suitable
468 * endpoint, otherwise just free the tower */
469 if (!resolved_endpoint
)
471 status
= TowerExplode(towers
[i
], NULL
, NULL
, NULL
, &resolved_endpoint
, NULL
);
472 TRACE("status = %d\n", status
);
474 I_RpcFree(towers
[i
]);
477 if (resolved_endpoint
)
479 RPCRT4_ResolveBinding(Binding
, resolved_endpoint
);
480 I_RpcFree(resolved_endpoint
);
484 WARN("couldn't find an endpoint\n");
485 return EPT_S_NOT_REGISTERED
;
488 /*****************************************************************************
489 * TowerExplode (RPCRT4.@)
491 RPC_STATUS WINAPI
TowerExplode(
492 const twr_t
*tower
, PRPC_SYNTAX_IDENTIFIER object
, PRPC_SYNTAX_IDENTIFIER syntax
,
493 char **protseq
, char **endpoint
, char **address
)
497 const unsigned char *p
;
499 const twr_uuid_floor_t
*object_floor
;
500 const twr_uuid_floor_t
*syntax_floor
;
502 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower
, object
, syntax
, protseq
,
512 tower_size
= tower
->tower_length
;
514 if (tower_size
< sizeof(u_int16
))
515 return EPT_S_NOT_REGISTERED
;
517 p
= &tower
->tower_octet_string
[0];
519 floor_count
= *(const u_int16
*)p
;
520 p
+= sizeof(u_int16
);
521 tower_size
-= sizeof(u_int16
);
522 TRACE("floor_count: %d\n", floor_count
);
523 /* FIXME: should we do something with the floor count? at the moment we don't */
525 if (tower_size
< sizeof(*object_floor
) + sizeof(*syntax_floor
))
526 return EPT_S_NOT_REGISTERED
;
528 object_floor
= (const twr_uuid_floor_t
*)p
;
529 p
+= sizeof(*object_floor
);
530 tower_size
-= sizeof(*object_floor
);
531 syntax_floor
= (const twr_uuid_floor_t
*)p
;
532 p
+= sizeof(*syntax_floor
);
533 tower_size
-= sizeof(*syntax_floor
);
535 if ((object_floor
->count_lhs
!= sizeof(object_floor
->protid
) +
536 sizeof(object_floor
->uuid
) + sizeof(object_floor
->major_version
)) ||
537 (object_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
538 (object_floor
->count_rhs
!= sizeof(object_floor
->minor_version
)))
539 return EPT_S_NOT_REGISTERED
;
541 if ((syntax_floor
->count_lhs
!= sizeof(syntax_floor
->protid
) +
542 sizeof(syntax_floor
->uuid
) + sizeof(syntax_floor
->major_version
)) ||
543 (syntax_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
544 (syntax_floor
->count_rhs
!= sizeof(syntax_floor
->minor_version
)))
545 return EPT_S_NOT_REGISTERED
;
547 status
= RpcTransport_ParseTopOfTower(p
, tower_size
, protseq
, address
, endpoint
);
548 if ((status
== RPC_S_OK
) && syntax
&& object
)
550 syntax
->SyntaxGUID
= syntax_floor
->uuid
;
551 syntax
->SyntaxVersion
.MajorVersion
= syntax_floor
->major_version
;
552 syntax
->SyntaxVersion
.MinorVersion
= syntax_floor
->minor_version
;
553 object
->SyntaxGUID
= object_floor
->uuid
;
554 object
->SyntaxVersion
.MajorVersion
= object_floor
->major_version
;
555 object
->SyntaxVersion
.MinorVersion
= object_floor
->minor_version
;
560 /***********************************************************************
561 * TowerConstruct (RPCRT4.@)
563 RPC_STATUS WINAPI
TowerConstruct(
564 const RPC_SYNTAX_IDENTIFIER
*object
, const RPC_SYNTAX_IDENTIFIER
*syntax
,
565 const char *protseq
, const char *endpoint
, const char *address
,
571 twr_uuid_floor_t
*object_floor
;
572 twr_uuid_floor_t
*syntax_floor
;
574 TRACE("(%p, %p, %s, %s, %s, %p)\n", object
, syntax
, debugstr_a(protseq
),
575 debugstr_a(endpoint
), debugstr_a(address
), tower
);
579 status
= RpcTransport_GetTopOfTower(NULL
, &tower_size
, protseq
, address
, endpoint
);
581 if (status
!= RPC_S_OK
)
584 tower_size
+= sizeof(u_int16
) + sizeof(*object_floor
) + sizeof(*syntax_floor
);
585 *tower
= I_RpcAllocate(FIELD_OFFSET(twr_t
, tower_octet_string
[tower_size
]));
587 return RPC_S_OUT_OF_RESOURCES
;
589 (*tower
)->tower_length
= tower_size
;
590 p
= &(*tower
)->tower_octet_string
[0];
591 *(u_int16
*)p
= 5; /* number of floors */
592 p
+= sizeof(u_int16
);
593 object_floor
= (twr_uuid_floor_t
*)p
;
594 p
+= sizeof(*object_floor
);
595 syntax_floor
= (twr_uuid_floor_t
*)p
;
596 p
+= sizeof(*syntax_floor
);
598 object_floor
->count_lhs
= sizeof(object_floor
->protid
) + sizeof(object_floor
->uuid
) +
599 sizeof(object_floor
->major_version
);
600 object_floor
->protid
= EPM_PROTOCOL_UUID
;
601 object_floor
->count_rhs
= sizeof(object_floor
->minor_version
);
602 object_floor
->uuid
= object
->SyntaxGUID
;
603 object_floor
->major_version
= object
->SyntaxVersion
.MajorVersion
;
604 object_floor
->minor_version
= object
->SyntaxVersion
.MinorVersion
;
606 syntax_floor
->count_lhs
= sizeof(syntax_floor
->protid
) + sizeof(syntax_floor
->uuid
) +
607 sizeof(syntax_floor
->major_version
);
608 syntax_floor
->protid
= EPM_PROTOCOL_UUID
;
609 syntax_floor
->count_rhs
= sizeof(syntax_floor
->minor_version
);
610 syntax_floor
->uuid
= syntax
->SyntaxGUID
;
611 syntax_floor
->major_version
= syntax
->SyntaxVersion
.MajorVersion
;
612 syntax_floor
->minor_version
= syntax
->SyntaxVersion
.MinorVersion
;
614 status
= RpcTransport_GetTopOfTower(p
, &tower_size
, protseq
, address
, endpoint
);
615 if (status
!= RPC_S_OK
)
624 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
626 return HeapAlloc(GetProcessHeap(), 0, len
);
629 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
631 HeapFree(GetProcessHeap(), 0, ptr
);