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
32 #include "wine/debug.h"
33 #include "wine/exception.h"
35 #include "rpc_binding.h"
37 #include "epm_towers.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
41 /* The "real" RPC portmapper endpoints that I know of are:
45 * ncacn_np: \\pipe\epmapper
49 * If the user's machine ran a DCE RPC daemon, it would
50 * probably be possible to connect to it, but there are many
51 * reasons not to, like:
52 * - the user probably does *not* run one, and probably
53 * shouldn't be forced to run one just for local COM
54 * - very few Unix systems use DCE RPC... if they run a RPC
55 * daemon at all, it's usually Sun RPC
56 * - DCE RPC registrations are persistent and saved on disk,
57 * while MS-RPC registrations are documented as non-persistent
58 * and stored only in RAM, and auto-destroyed when the process
59 * dies (something DCE RPC can't do)
61 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
62 * there would be interoperability advantages, like the possibility
63 * of running a fully functional DCOM server using Wine...
66 static const struct epm_endpoints
72 { "ncacn_np", "\\pipe\\epmapper" },
73 { "ncacn_ip_tcp", "135" },
74 { "ncacn_ip_udp", "135" },
75 { "ncalrpc", "epmapper" },
76 { "ncacn_http", "593" },
79 static BOOL
start_rpcss(void)
81 SC_HANDLE scm
, service
;
82 SERVICE_STATUS_PROCESS status
;
87 if (!(scm
= OpenSCManagerW( NULL
, NULL
, 0 )))
89 ERR( "failed to open service manager\n" );
92 if (!(service
= OpenServiceW( scm
, L
"RpcSs", SERVICE_START
| SERVICE_QUERY_STATUS
)))
94 ERR( "failed to open RpcSs service\n" );
95 CloseServiceHandle( scm
);
98 if (StartServiceW( service
, 0, NULL
) || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
)
100 ULONGLONG start_time
= GetTickCount64();
105 if (!QueryServiceStatusEx( service
, SC_STATUS_PROCESS_INFO
,
106 (BYTE
*)&status
, sizeof(status
), &dummy
))
108 if (status
.dwCurrentState
== SERVICE_RUNNING
)
113 if (GetTickCount64() - start_time
> 30000) break;
116 } while (status
.dwCurrentState
== SERVICE_START_PENDING
);
118 if (status
.dwCurrentState
!= SERVICE_RUNNING
)
119 WARN( "RpcSs failed to start %lu\n", status
.dwCurrentState
);
121 else ERR( "failed to start RpcSs service\n" );
123 CloseServiceHandle( service
);
124 CloseServiceHandle( scm
);
128 static inline BOOL
is_epm_destination_local(RPC_BINDING_HANDLE handle
)
130 RpcBinding
*bind
= handle
;
131 const char *protseq
= bind
->Protseq
;
132 const char *network_addr
= bind
->NetworkAddr
;
134 return (!strcmp(protseq
, "ncalrpc") ||
135 (!strcmp(protseq
, "ncacn_np") &&
136 (!network_addr
|| !strcmp(network_addr
, "."))));
139 static RPC_STATUS
get_epm_handle_client(RPC_BINDING_HANDLE handle
, RPC_BINDING_HANDLE
*epm_handle
)
141 RpcBinding
*bind
= handle
;
142 const char * pszEndpoint
= NULL
;
144 RpcBinding
* epm_bind
;
148 return RPC_S_INVALID_BINDING
;
150 for (i
= 0; i
< ARRAY_SIZE(epm_endpoints
); i
++)
151 if (!strcmp(bind
->Protseq
, epm_endpoints
[i
].protseq
))
152 pszEndpoint
= epm_endpoints
[i
].endpoint
;
156 FIXME("no endpoint for the endpoint-mapper found for protseq %s\n", debugstr_a(bind
->Protseq
));
157 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
160 status
= RpcBindingCopy(handle
, epm_handle
);
161 if (status
!= RPC_S_OK
) return status
;
163 epm_bind
= *epm_handle
;
164 if (epm_bind
->AuthInfo
)
166 /* don't bother with authenticating against the EPM by default
167 * (see EnableAuthEpResolution registry value) */
168 RpcAuthInfo_Release(epm_bind
->AuthInfo
);
169 epm_bind
->AuthInfo
= NULL
;
171 RPCRT4_ResolveBinding(epm_bind
, pszEndpoint
);
176 static RPC_STATUS
get_epm_handle_server(RPC_BINDING_HANDLE
*epm_handle
)
178 unsigned char string_binding
[] = "ncacn_np:.[\\\\pipe\\\\epmapper]";
180 return RpcBindingFromStringBindingA(string_binding
, epm_handle
);
183 static LONG WINAPI
rpc_filter(EXCEPTION_POINTERS
*__eptr
)
185 switch (__eptr
->ExceptionRecord
->ExceptionCode
)
187 case EXCEPTION_ACCESS_VIOLATION
:
188 case EXCEPTION_ILLEGAL_INSTRUCTION
:
189 return EXCEPTION_CONTINUE_SEARCH
;
191 return EXCEPTION_EXECUTE_HANDLER
;
195 static RPC_STATUS
epm_register( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
196 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
, BOOL replace
)
198 PRPC_SERVER_INTERFACE If
= IfSpec
;
200 RPC_STATUS status
= RPC_S_OK
;
201 error_status_t status2
;
202 ept_entry_t
*entries
;
205 TRACE("(%p,%p,%p,%s) replace=%d\n", IfSpec
, BindingVector
, UuidVector
, debugstr_a((char*)Annotation
), replace
);
206 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
207 for (i
=0; i
<BindingVector
->Count
; i
++) {
208 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
209 TRACE(" protseq[%ld]=%s\n", i
, debugstr_a(bind
->Protseq
));
210 TRACE(" endpoint[%ld]=%s\n", i
, debugstr_a(bind
->Endpoint
));
213 for (i
=0; i
<UuidVector
->Count
; i
++)
214 TRACE(" obj[%ld]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
217 if (!BindingVector
->Count
) return RPC_S_OK
;
219 entries
= calloc(BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1), sizeof(*entries
));
221 return RPC_S_OUT_OF_MEMORY
;
223 status
= get_epm_handle_server(&handle
);
224 if (status
!= RPC_S_OK
)
230 for (i
= 0; i
< BindingVector
->Count
; i
++)
233 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
234 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
236 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
237 bind
->Protseq
, bind
->Endpoint
,
239 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
240 if (status
!= RPC_S_OK
) break;
243 memcpy(&entries
[i
* UuidVector
->Count
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
245 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
247 memcpy(entries
[i
].annotation
, Annotation
,
248 min(strlen((char *)Annotation
) + 1, ept_max_annotation_size
));
252 if (status
== RPC_S_OK
)
258 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
259 entries
, replace
, &status2
);
263 status2
= GetExceptionCode();
266 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
267 is_epm_destination_local(handle
))
272 if (status2
!= RPC_S_OK
)
273 ERR("ept_insert failed with error %ld\n", status2
);
274 status
= status2
; /* FIXME: convert status? */
278 RpcBindingFree(&handle
);
280 for (i
= 0; i
< BindingVector
->Count
; i
++)
283 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
284 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
292 /***********************************************************************
293 * RpcEpRegisterA (RPCRT4.@)
295 RPC_STATUS WINAPI
RpcEpRegisterA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
296 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
298 return epm_register(IfSpec
, BindingVector
, UuidVector
, Annotation
, TRUE
);
301 /***********************************************************************
302 * RpcEpRegisterNoReplaceA (RPCRT4.@)
304 RPC_STATUS WINAPI
RpcEpRegisterNoReplaceA( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
305 UUID_VECTOR
*UuidVector
, RPC_CSTR Annotation
)
307 return epm_register(IfSpec
, BindingVector
, UuidVector
, Annotation
, FALSE
);
310 /***********************************************************************
311 * RpcEpRegisterW (RPCRT4.@)
313 RPC_STATUS WINAPI
RpcEpRegisterW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
314 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
316 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
319 status
= epm_register(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
, TRUE
);
325 /***********************************************************************
326 * RpcEpRegisterNoReplaceW (RPCRT4.@)
328 RPC_STATUS WINAPI
RpcEpRegisterNoReplaceW( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
329 UUID_VECTOR
*UuidVector
, RPC_WSTR Annotation
)
331 LPSTR annA
= RPCRT4_strdupWtoA(Annotation
);
334 status
= epm_register(IfSpec
, BindingVector
, UuidVector
, (RPC_CSTR
)annA
, FALSE
);
340 /***********************************************************************
341 * RpcEpUnregister (RPCRT4.@)
343 RPC_STATUS WINAPI
RpcEpUnregister( RPC_IF_HANDLE IfSpec
, RPC_BINDING_VECTOR
*BindingVector
,
344 UUID_VECTOR
*UuidVector
)
346 PRPC_SERVER_INTERFACE If
= IfSpec
;
348 RPC_STATUS status
= RPC_S_OK
;
349 error_status_t status2
;
350 ept_entry_t
*entries
;
353 TRACE("(%p,%p,%p)\n", IfSpec
, BindingVector
, UuidVector
);
354 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
355 for (i
=0; i
<BindingVector
->Count
; i
++) {
356 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
357 TRACE(" protseq[%ld]=%s\n", i
, debugstr_a(bind
->Protseq
));
358 TRACE(" endpoint[%ld]=%s\n", i
, debugstr_a(bind
->Endpoint
));
361 for (i
=0; i
<UuidVector
->Count
; i
++)
362 TRACE(" obj[%ld]=%s\n", i
, debugstr_guid(UuidVector
->Uuid
[i
]));
365 entries
= calloc(BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1), sizeof(*entries
));
367 return RPC_S_OUT_OF_MEMORY
;
369 status
= get_epm_handle_server(&handle
);
370 if (status
!= RPC_S_OK
)
376 for (i
= 0; i
< BindingVector
->Count
; i
++)
379 RpcBinding
* bind
= BindingVector
->BindingH
[i
];
380 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
382 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
,
383 bind
->Protseq
, bind
->Endpoint
,
385 &entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
386 if (status
!= RPC_S_OK
) break;
389 memcpy(&entries
[i
* UuidVector
->Count
+ j
].object
, &UuidVector
->Uuid
[j
], sizeof(GUID
));
391 memset(&entries
[i
].object
, 0, sizeof(entries
[i
].object
));
395 if (status
== RPC_S_OK
)
399 ept_insert(handle
, BindingVector
->Count
* (UuidVector
? UuidVector
->Count
: 1),
400 entries
, TRUE
, &status2
);
404 status2
= GetExceptionCode();
407 if (status2
== RPC_S_SERVER_UNAVAILABLE
)
408 status2
= EPT_S_NOT_REGISTERED
;
409 if (status2
!= RPC_S_OK
)
410 ERR("ept_insert failed with error %ld\n", status2
);
411 status
= status2
; /* FIXME: convert status? */
413 RpcBindingFree(&handle
);
415 for (i
= 0; i
< BindingVector
->Count
; i
++)
418 for (j
= 0; j
< (UuidVector
? UuidVector
->Count
: 1); j
++)
419 I_RpcFree(entries
[i
*(UuidVector
? UuidVector
->Count
: 1) + j
].tower
);
427 /***********************************************************************
428 * RpcEpResolveBinding (RPCRT4.@)
430 RPC_STATUS WINAPI
RpcEpResolveBinding( RPC_BINDING_HANDLE Binding
, RPC_IF_HANDLE IfSpec
)
432 PRPC_CLIENT_INTERFACE If
= IfSpec
;
433 RpcBinding
* bind
= Binding
;
435 error_status_t status2
;
437 ept_lookup_handle_t entry_handle
= NULL
;
439 twr_t
*towers
[4] = { NULL
};
440 unsigned32 num_towers
, i
;
441 GUID uuid
= GUID_NULL
;
442 char *resolved_endpoint
= NULL
;
444 TRACE("(%p,%p)\n", Binding
, IfSpec
);
445 TRACE(" protseq=%s\n", debugstr_a(bind
->Protseq
));
446 TRACE(" obj=%s\n", debugstr_guid(&bind
->ObjectUuid
));
447 TRACE(" networkaddr=%s\n", debugstr_a(bind
->NetworkAddr
));
448 TRACE(" ifid=%s\n", debugstr_guid(&If
->InterfaceId
.SyntaxGUID
));
450 /* just return for fully bound handles */
451 if (bind
->Endpoint
&& (bind
->Endpoint
[0] != '\0'))
454 status
= get_epm_handle_client(Binding
, &handle
);
455 if (status
!= RPC_S_OK
) return status
;
457 status
= TowerConstruct(&If
->InterfaceId
, &If
->TransferSyntax
, bind
->Protseq
,
458 ((RpcBinding
*)handle
)->Endpoint
,
459 bind
->NetworkAddr
, &tower
);
460 if (status
!= RPC_S_OK
)
462 WARN("couldn't get tower\n");
463 RpcBindingFree(&handle
);
471 ept_map(handle
, &uuid
, tower
, &entry_handle
, ARRAY_SIZE(towers
), &num_towers
, towers
, &status2
);
472 /* FIXME: translate status2? */
476 status2
= GetExceptionCode();
479 if (status2
== RPC_S_SERVER_UNAVAILABLE
&&
480 is_epm_destination_local(handle
))
488 RpcBindingFree(&handle
);
491 if (status2
!= RPC_S_OK
)
493 ERR("ept_map failed for ifid %s, protseq %s, networkaddr %s\n", debugstr_guid(&If
->TransferSyntax
.SyntaxGUID
), bind
->Protseq
, bind
->NetworkAddr
);
497 for (i
= 0; i
< num_towers
; i
++)
499 /* only parse the tower if we haven't already found a suitable
500 * endpoint, otherwise just free the tower */
501 if (!resolved_endpoint
)
503 status
= TowerExplode(towers
[i
], NULL
, NULL
, NULL
, &resolved_endpoint
, NULL
);
504 TRACE("status = %ld\n", status
);
506 I_RpcFree(towers
[i
]);
509 if (resolved_endpoint
)
511 RPCRT4_ResolveBinding(Binding
, resolved_endpoint
);
512 I_RpcFree(resolved_endpoint
);
516 WARN("couldn't find an endpoint\n");
517 return EPT_S_NOT_REGISTERED
;
520 /*****************************************************************************
521 * TowerExplode (RPCRT4.@)
523 RPC_STATUS WINAPI
TowerExplode(
524 const twr_t
*tower
, PRPC_SYNTAX_IDENTIFIER object
, PRPC_SYNTAX_IDENTIFIER syntax
,
525 char **protseq
, char **endpoint
, char **address
)
529 const unsigned char *p
;
531 const twr_uuid_floor_t
*object_floor
;
532 const twr_uuid_floor_t
*syntax_floor
;
534 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower
, object
, syntax
, protseq
,
544 tower_size
= tower
->tower_length
;
546 if (tower_size
< sizeof(u_int16
))
547 return EPT_S_NOT_REGISTERED
;
549 p
= &tower
->tower_octet_string
[0];
551 floor_count
= *(const u_int16
*)p
;
552 p
+= sizeof(u_int16
);
553 tower_size
-= sizeof(u_int16
);
554 TRACE("floor_count: %d\n", floor_count
);
555 /* FIXME: should we do something with the floor count? at the moment we don't */
557 if (tower_size
< sizeof(*object_floor
) + sizeof(*syntax_floor
))
558 return EPT_S_NOT_REGISTERED
;
560 object_floor
= (const twr_uuid_floor_t
*)p
;
561 p
+= sizeof(*object_floor
);
562 tower_size
-= sizeof(*object_floor
);
563 syntax_floor
= (const twr_uuid_floor_t
*)p
;
564 p
+= sizeof(*syntax_floor
);
565 tower_size
-= sizeof(*syntax_floor
);
567 if ((object_floor
->count_lhs
!= sizeof(object_floor
->protid
) +
568 sizeof(object_floor
->uuid
) + sizeof(object_floor
->major_version
)) ||
569 (object_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
570 (object_floor
->count_rhs
!= sizeof(object_floor
->minor_version
)))
571 return EPT_S_NOT_REGISTERED
;
573 if ((syntax_floor
->count_lhs
!= sizeof(syntax_floor
->protid
) +
574 sizeof(syntax_floor
->uuid
) + sizeof(syntax_floor
->major_version
)) ||
575 (syntax_floor
->protid
!= EPM_PROTOCOL_UUID
) ||
576 (syntax_floor
->count_rhs
!= sizeof(syntax_floor
->minor_version
)))
577 return EPT_S_NOT_REGISTERED
;
579 status
= RpcTransport_ParseTopOfTower(p
, tower_size
, protseq
, address
, endpoint
);
580 if ((status
== RPC_S_OK
) && syntax
&& object
)
582 syntax
->SyntaxGUID
= syntax_floor
->uuid
;
583 syntax
->SyntaxVersion
.MajorVersion
= syntax_floor
->major_version
;
584 syntax
->SyntaxVersion
.MinorVersion
= syntax_floor
->minor_version
;
585 object
->SyntaxGUID
= object_floor
->uuid
;
586 object
->SyntaxVersion
.MajorVersion
= object_floor
->major_version
;
587 object
->SyntaxVersion
.MinorVersion
= object_floor
->minor_version
;
592 /***********************************************************************
593 * TowerConstruct (RPCRT4.@)
595 RPC_STATUS WINAPI
TowerConstruct(
596 const RPC_SYNTAX_IDENTIFIER
*object
, const RPC_SYNTAX_IDENTIFIER
*syntax
,
597 const char *protseq
, const char *endpoint
, const char *address
,
603 twr_uuid_floor_t
*object_floor
;
604 twr_uuid_floor_t
*syntax_floor
;
606 TRACE("(%p, %p, %s, %s, %s, %p)\n", object
, syntax
, debugstr_a(protseq
),
607 debugstr_a(endpoint
), debugstr_a(address
), tower
);
611 status
= RpcTransport_GetTopOfTower(NULL
, &tower_size
, protseq
, address
, endpoint
);
613 if (status
!= RPC_S_OK
)
616 tower_size
+= sizeof(u_int16
) + sizeof(*object_floor
) + sizeof(*syntax_floor
);
617 *tower
= I_RpcAllocate(FIELD_OFFSET(twr_t
, tower_octet_string
[tower_size
]));
619 return RPC_S_OUT_OF_RESOURCES
;
621 (*tower
)->tower_length
= tower_size
;
622 p
= &(*tower
)->tower_octet_string
[0];
623 *(u_int16
*)p
= 5; /* number of floors */
624 p
+= sizeof(u_int16
);
625 object_floor
= (twr_uuid_floor_t
*)p
;
626 p
+= sizeof(*object_floor
);
627 syntax_floor
= (twr_uuid_floor_t
*)p
;
628 p
+= sizeof(*syntax_floor
);
630 object_floor
->count_lhs
= sizeof(object_floor
->protid
) + sizeof(object_floor
->uuid
) +
631 sizeof(object_floor
->major_version
);
632 object_floor
->protid
= EPM_PROTOCOL_UUID
;
633 object_floor
->count_rhs
= sizeof(object_floor
->minor_version
);
634 object_floor
->uuid
= object
->SyntaxGUID
;
635 object_floor
->major_version
= object
->SyntaxVersion
.MajorVersion
;
636 object_floor
->minor_version
= object
->SyntaxVersion
.MinorVersion
;
638 syntax_floor
->count_lhs
= sizeof(syntax_floor
->protid
) + sizeof(syntax_floor
->uuid
) +
639 sizeof(syntax_floor
->major_version
);
640 syntax_floor
->protid
= EPM_PROTOCOL_UUID
;
641 syntax_floor
->count_rhs
= sizeof(syntax_floor
->minor_version
);
642 syntax_floor
->uuid
= syntax
->SyntaxGUID
;
643 syntax_floor
->major_version
= syntax
->SyntaxVersion
.MajorVersion
;
644 syntax_floor
->minor_version
= syntax
->SyntaxVersion
.MinorVersion
;
646 status
= RpcTransport_GetTopOfTower(p
, &tower_size
, protseq
, address
, endpoint
);
647 if (status
!= RPC_S_OK
)
656 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
661 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)