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
23 #define WIN32_NO_STATUS
30 //#include "winerror.h"
34 #include <wine/debug.h>
35 #include <wine/exception.h>
37 #include "rpc_binding.h"
39 #include "epm_towers.h"
42 DEFINE_GUID(GUID_NULL
,0,0,0,0,0,0,0,0,0,0,0);
45 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
47 /* The "real" RPC portmapper endpoints that I know of are:
51 * ncacn_np: \\pipe\epmapper
55 * If the user's machine ran a DCE RPC daemon, it would
56 * probably be possible to connect to it, but there are many
57 * reasons not to, like:
58 * - the user probably does *not* run one, and probably
59 * shouldn't be forced to run one just for local COM
60 * - very few Unix systems use DCE RPC... if they run a RPC
61 * daemon at all, it's usually Sun RPC
62 * - DCE RPC registrations are persistent and saved on disk,
63 * while MS-RPC registrations are documented as non-persistent
64 * and stored only in RAM, and auto-destroyed when the process
65 * dies (something DCE RPC can't do)
67 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
68 * there would be interoperability advantages, like the possibility
69 * of running a fully functional DCOM server using Wine...
72 static const struct epm_endpoints
78 { "ncacn_np", "\\pipe\\epmapper" },
79 { "ncacn_ip_tcp", "135" },
80 { "ncacn_ip_udp", "135" },
81 { "ncalrpc", "epmapper" },
82 { "ncacn_http", "593" },
85 static BOOL
start_rpcss(void)
87 PROCESS_INFORMATION pi
;
90 static const WCHAR rpcss
[] = {'\\','r','p','c','s','s','.','e','x','e',0};
96 ZeroMemory(&si
, sizeof(STARTUPINFOA
));
97 si
.cb
= sizeof(STARTUPINFOA
);
98 GetSystemDirectoryW( cmd
, MAX_PATH
- sizeof(rpcss
)/sizeof(WCHAR
) );
99 lstrcatW( cmd
, rpcss
);
101 Wow64DisableWow64FsRedirection( &redir
);
102 rslt
= CreateProcessW( cmd
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
103 Wow64RevertWow64FsRedirection( redir
);
107 CloseHandle(pi
.hProcess
);
108 CloseHandle(pi
.hThread
);
115 static inline BOOL
is_epm_destination_local(RPC_BINDING_HANDLE handle
)
117 RpcBinding
*bind
= handle
;
118 const char *protseq
= bind
->Protseq
;
119 const char *network_addr
= bind
->NetworkAddr
;
121 return (!strcmp(protseq
, "ncalrpc") ||
122 (!strcmp(protseq
, "ncacn_np") &&
123 (!network_addr
|| !strcmp(network_addr
, "."))));
126 static RPC_STATUS
get_epm_handle_client(RPC_BINDING_HANDLE handle
, RPC_BINDING_HANDLE
*epm_handle
)
128 RpcBinding
*bind
= handle
;
129 const char * pszEndpoint
= NULL
;
131 RpcBinding
* epm_bind
;
135 return RPC_S_INVALID_BINDING
;
137 for (i
= 0; i
< sizeof(epm_endpoints
)/sizeof(epm_endpoints
[0]); i
++)
138 if (!strcmp(bind
->Protseq
, epm_endpoints
[i
].protseq
))
139 pszEndpoint
= epm_endpoints
[i
].endpoint
;
143 FIXME("no endpoint for the endpoint-mapper found for protseq %s\n", debugstr_a(bind
->Protseq
));
144 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
147 status
= RpcBindingCopy(handle
, epm_handle
);
148 if (status
!= RPC_S_OK
) return status
;
150 epm_bind
= *epm_handle
;
151 if (epm_bind
->AuthInfo
)
153 /* don't bother with authenticating against the EPM by default
154 * (see EnableAuthEpResolution registry value) */
155 RpcAuthInfo_Release(epm_bind
->AuthInfo
);
156 epm_bind
->AuthInfo
= NULL
;
158 RPCRT4_ResolveBinding(epm_bind
, pszEndpoint
);
163 static RPC_STATUS
get_epm_handle_server(RPC_BINDING_HANDLE
*epm_handle
)
165 unsigned char string_binding
[] = "ncacn_np:.[\\\\pipe\\\\epmapper]";
167 return RpcBindingFromStringBindingA(string_binding
, epm_handle
);
170 static LONG WINAPI
rpc_filter(EXCEPTION_POINTERS
*__eptr
)
172 switch (__eptr
->ExceptionRecord
->ExceptionCode
)
174 case EXCEPTION_ACCESS_VIOLATION
:
175 case EXCEPTION_ILLEGAL_INSTRUCTION
:
176 return EXCEPTION_CONTINUE_SEARCH
;
178 return EXCEPTION_EXECUTE_HANDLER
;
182 /***********************************************************************
183 * RpcEpRegisterA (RPCRT4.@)
185 RPC_STATUS WINAPI
RpcEpRegisterA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
186 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
188 PRPC_SERVER_INTERFACE If
= IfSpec
;
190 RPC_STATUS status
= RPC_S_OK
;
191 error_status_t status2
;
192 ept_entry_t
*entries
;
195 TRACE("(%p,%p,%p,%s)\n", IfSpec
, BindingVector
, UuidVector
, debugstr_a((char*)Annotation
));
196 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
197 for (i
=0; i
<BindingVector
->Count
; i
++) {
198 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
199 TRACE(" protseq[%d]=%s\n", i
, debugstr_a(bind
->Protseq
));
200 TRACE(" endpoint[%d]=%s\n", i
, debugstr_a(bind
->Endpoint
));
203 for (i
=0; i
<UuidVector
->Count
; i
++)
204 TRACE(" obj[%d]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
207 if (!BindingVector
->Count
) return RPC_S_OK
;
209 entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*entries
) * BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1));
211 return RPC_S_OUT_OF_MEMORY
;
213 status
= get_epm_handle_server(&handle
);
214 if (status
!= RPC_S_OK
)
216 HeapFree(GetProcessHeap(), 0, entries
);
220 for (i
= 0; i
< BindingVector
->Count
; i
++)
223 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
224 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
226 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
227 bind
->Protseq
, bind
->Endpoint
,
229 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
230 if (status
!= RPC_S_OK
) break;
233 memcpy(&entries
[i
* UuidVector
->Count
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
235 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
237 memcpy(entries
[i
].annotation
, Annotation
,
238 min(strlen((char *)Annotation
) + 1, ept_max_annotation_size
));
242 if (status
== RPC_S_OK
)
248 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
249 entries
, TRUE
, &status2
);
253 status2
= GetExceptionCode();
256 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
257 is_epm_destination_local(handle
))
262 if (status2
!= RPC_S_OK
)
263 ERR("ept_insert failed with error %d\n", status2
);
264 status
= status2
; /* FIXME: convert status? */
268 RpcBindingFree(&handle
);
270 for (i
= 0; i
< BindingVector
->Count
; i
++)
273 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
274 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
277 HeapFree(GetProcessHeap(), 0, entries
);
282 /***********************************************************************
283 * RpcEpRegisterW (RPCRT4.@)
285 RPC_STATUS WINAPI
RpcEpRegisterW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
286 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
288 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
291 status
= RpcEpRegisterA(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
);
293 HeapFree(GetProcessHeap(), 0, annA
);
297 /***********************************************************************
298 * RpcEpUnregister (RPCRT4.@)
300 RPC_STATUS WINAPI
RpcEpUnregister( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
301 UUID_VECTOR
*UuidVector
)
303 PRPC_SERVER_INTERFACE If
= IfSpec
;
305 RPC_STATUS status
= RPC_S_OK
;
306 error_status_t status2
;
307 ept_entry_t
*entries
;
310 TRACE("(%p,%p,%p)\n", IfSpec
, BindingVector
, UuidVector
);
311 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
312 for (i
=0; i
<BindingVector
->Count
; i
++) {
313 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
314 TRACE(" protseq[%d]=%s\n", i
, debugstr_a(bind
->Protseq
));
315 TRACE(" endpoint[%d]=%s\n", i
, debugstr_a(bind
->Endpoint
));
318 for (i
=0; i
<UuidVector
->Count
; i
++)
319 TRACE(" obj[%d]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
322 entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*entries
) * BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1));
324 return RPC_S_OUT_OF_MEMORY
;
326 status
= get_epm_handle_server(&handle
);
327 if (status
!= RPC_S_OK
)
329 HeapFree(GetProcessHeap(), 0, entries
);
333 for (i
= 0; i
< BindingVector
->Count
; i
++)
336 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
337 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
339 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
340 bind
->Protseq
, bind
->Endpoint
,
342 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
343 if (status
!= RPC_S_OK
) break;
346 memcpy(&entries
[i
* UuidVector
->Count
+ j
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
348 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
352 if (status
== RPC_S_OK
)
356 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
357 entries
, TRUE
, &status2
);
361 status2
= GetExceptionCode();
364 if (status2
== RPC_S_SERVER_UNAVAILABLE
)
365 status2
= EPT_S_NOT_REGISTERED
;
366 if (status2
!= RPC_S_OK
)
367 ERR("ept_insert failed with error %d\n", status2
);
368 status
= status2
; /* FIXME: convert status? */
370 RpcBindingFree(&handle
);
372 for (i
= 0; i
< BindingVector
->Count
; i
++)
375 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
376 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
379 HeapFree(GetProcessHeap(), 0, entries
);
384 /***********************************************************************
385 * RpcEpResolveBinding (RPCRT4.@)
387 RPC_STATUS WINAPI
RpcEpResolveBinding( RPC_BINDING_HANDLE Binding
, RPC_IF_HANDLE IfSpec
)
389 PRPC_CLIENT_INTERFACE If
= IfSpec
;
390 RpcBinding
* bind
= Binding
;
392 error_status_t status2
;
394 ept_lookup_handle_t entry_handle
= NULL
;
396 twr_t
*towers
[4] = { NULL
};
397 unsigned32 num_towers
, i
;
398 GUID uuid
= GUID_NULL
;
399 char *resolved_endpoint
= NULL
;
401 TRACE("(%p,%p)\n", Binding
, IfSpec
);
402 TRACE(" protseq=%s\n", debugstr_a(bind
->Protseq
));
403 TRACE(" obj=%s\n", debugstr_guid(&bind
->ObjectUuid
));
404 TRACE(" networkaddr=%s\n", debugstr_a(bind
->NetworkAddr
));
405 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
407 /* just return for fully bound handles */
408 if (bind
->Endpoint
&& (bind
->Endpoint
[0] != '\0'))
411 status
= get_epm_handle_client(Binding
, &handle
);
412 if (status
!= RPC_S_OK
) return status
;
414 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
, bind
->Protseq
,
415 ((RpcBinding
*)handle
)->Endpoint
,
416 bind
->NetworkAddr
, &tower
);
417 if (status
!= RPC_S_OK
)
419 WARN("couldn't get tower\n");
420 RpcBindingFree(&handle
);
428 ept_map(handle
, &uuid
, tower
, &entry_handle
, sizeof(towers
)/sizeof(towers
[0]), &num_towers
, towers
, &status2
);
429 /* FIXME: translate status2? */
433 status2
= GetExceptionCode();
436 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
437 is_epm_destination_local(handle
))
445 RpcBindingFree(&handle
);
448 if (status2
!= RPC_S_OK
)
450 ERR("ept_map failed for ifid %s, protseq %s, networkaddr %s\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
), bind
->Protseq
, bind
->NetworkAddr
);
454 for (i
= 0; i
< num_towers
; i
++)
456 /* only parse the tower if we haven't already found a suitable
457 * endpoint, otherwise just free the tower */
458 if (!resolved_endpoint
)
460 status
= TowerExplode(towers
[i
], NULL
, NULL
, NULL
, &resolved_endpoint
, NULL
);
461 TRACE("status = %d\n", status
);
463 I_RpcFree(towers
[i
]);
466 if (resolved_endpoint
)
468 RPCRT4_ResolveBinding(Binding
, resolved_endpoint
);
469 I_RpcFree(resolved_endpoint
);
473 WARN("couldn't find an endpoint\n");
474 return EPT_S_NOT_REGISTERED
;
477 /*****************************************************************************
478 * TowerExplode (RPCRT4.@)
480 RPC_STATUS WINAPI
TowerExplode(
481 const twr_t
*tower
, PRPC_SYNTAX_IDENTIFIER object
, PRPC_SYNTAX_IDENTIFIER syntax
,
482 char **protseq
, char **endpoint
, char **address
)
486 const unsigned char *p
;
488 const twr_uuid_floor_t
*object_floor
;
489 const twr_uuid_floor_t
*syntax_floor
;
491 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower
, object
, syntax
, protseq
,
501 tower_size
= tower
->tower_length
;
503 if (tower_size
< sizeof(u_int16
))
504 return EPT_S_NOT_REGISTERED
;
506 p
= &tower
->tower_octet_string
[0];
508 floor_count
= *(const u_int16
*)p
;
509 p
+= sizeof(u_int16
);
510 tower_size
-= sizeof(u_int16
);
511 TRACE("floor_count: %d\n", floor_count
);
512 /* FIXME: should we do something with the floor count? at the moment we don't */
514 if (tower_size
< sizeof(*object_floor
) + sizeof(*syntax_floor
))
515 return EPT_S_NOT_REGISTERED
;
517 object_floor
= (const twr_uuid_floor_t
*)p
;
518 p
+= sizeof(*object_floor
);
519 tower_size
-= sizeof(*object_floor
);
520 syntax_floor
= (const twr_uuid_floor_t
*)p
;
521 p
+= sizeof(*syntax_floor
);
522 tower_size
-= sizeof(*syntax_floor
);
524 if ((object_floor
->count_lhs
!= sizeof(object_floor
->protid
) +
525 sizeof(object_floor
->uuid
) + sizeof(object_floor
->major_version
)) ||
526 (object_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
527 (object_floor
->count_rhs
!= sizeof(object_floor
->minor_version
)))
528 return EPT_S_NOT_REGISTERED
;
530 if ((syntax_floor
->count_lhs
!= sizeof(syntax_floor
->protid
) +
531 sizeof(syntax_floor
->uuid
) + sizeof(syntax_floor
->major_version
)) ||
532 (syntax_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
533 (syntax_floor
->count_rhs
!= sizeof(syntax_floor
->minor_version
)))
534 return EPT_S_NOT_REGISTERED
;
536 status
= RpcTransport_ParseTopOfTower(p
, tower_size
, protseq
, address
, endpoint
);
537 if ((status
== RPC_S_OK
) && syntax
&& object
)
539 syntax
->SyntaxGUID
= syntax_floor
->uuid
;
540 syntax
->SyntaxVersion
.MajorVersion
= syntax_floor
->major_version
;
541 syntax
->SyntaxVersion
.MinorVersion
= syntax_floor
->minor_version
;
542 object
->SyntaxGUID
= object_floor
->uuid
;
543 object
->SyntaxVersion
.MajorVersion
= object_floor
->major_version
;
544 object
->SyntaxVersion
.MinorVersion
= object_floor
->minor_version
;
549 /***********************************************************************
550 * TowerConstruct (RPCRT4.@)
552 RPC_STATUS WINAPI
TowerConstruct(
553 const RPC_SYNTAX_IDENTIFIER
*object
, const RPC_SYNTAX_IDENTIFIER
*syntax
,
554 const char *protseq
, const char *endpoint
, const char *address
,
560 twr_uuid_floor_t
*object_floor
;
561 twr_uuid_floor_t
*syntax_floor
;
563 TRACE("(%p, %p, %s, %s, %s, %p)\n", object
, syntax
, debugstr_a(protseq
),
564 debugstr_a(endpoint
), debugstr_a(address
), tower
);
568 status
= RpcTransport_GetTopOfTower(NULL
, &tower_size
, protseq
, address
, endpoint
);
570 if (status
!= RPC_S_OK
)
573 tower_size
+= sizeof(u_int16
) + sizeof(*object_floor
) + sizeof(*syntax_floor
);
574 *tower
= I_RpcAllocate(FIELD_OFFSET(twr_t
, tower_octet_string
[tower_size
]));
576 return RPC_S_OUT_OF_RESOURCES
;
578 (*tower
)->tower_length
= tower_size
;
579 p
= &(*tower
)->tower_octet_string
[0];
580 *(u_int16
*)p
= 5; /* number of floors */
581 p
+= sizeof(u_int16
);
582 object_floor
= (twr_uuid_floor_t
*)p
;
583 p
+= sizeof(*object_floor
);
584 syntax_floor
= (twr_uuid_floor_t
*)p
;
585 p
+= sizeof(*syntax_floor
);
587 object_floor
->count_lhs
= sizeof(object_floor
->protid
) + sizeof(object_floor
->uuid
) +
588 sizeof(object_floor
->major_version
);
589 object_floor
->protid
= EPM_PROTOCOL_UUID
;
590 object_floor
->count_rhs
= sizeof(object_floor
->minor_version
);
591 object_floor
->uuid
= object
->SyntaxGUID
;
592 object_floor
->major_version
= object
->SyntaxVersion
.MajorVersion
;
593 object_floor
->minor_version
= object
->SyntaxVersion
.MinorVersion
;
595 syntax_floor
->count_lhs
= sizeof(syntax_floor
->protid
) + sizeof(syntax_floor
->uuid
) +
596 sizeof(syntax_floor
->major_version
);
597 syntax_floor
->protid
= EPM_PROTOCOL_UUID
;
598 syntax_floor
->count_rhs
= sizeof(syntax_floor
->minor_version
);
599 syntax_floor
->uuid
= syntax
->SyntaxGUID
;
600 syntax_floor
->major_version
= syntax
->SyntaxVersion
.MajorVersion
;
601 syntax_floor
->minor_version
= syntax
->SyntaxVersion
.MinorVersion
;
603 status
= RpcTransport_GetTopOfTower(p
, &tower_size
, protseq
, address
, endpoint
);
604 if (status
!= RPC_S_OK
)
613 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
615 return HeapAlloc(GetProcessHeap(), 0, len
);
618 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
620 HeapFree(GetProcessHeap(), 0, ptr
);