include: Specify calling convention explicitly in idl files where needed.
[wine/hramrach.git] / dlls / rpcrt4 / rpc_epmap.c
blobac7f25bee0e95e9682f16a0b6b504a6213238aa6
1 /*
2 * RPC endpoint mapper
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 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
29 #include "rpc.h"
31 #include "wine/debug.h"
32 #include "wine/exception.h"
34 #include "rpc_binding.h"
35 #include "epm.h"
36 #include "epm_towers.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 /* The "real" RPC portmapper endpoints that I know of are:
42 * ncadg_ip_udp: 135
43 * ncacn_ip_tcp: 135
44 * ncacn_np: \\pipe\epmapper
45 * ncalrpc: epmapper
46 * ncacn_http: 593
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
67 const char *protseq;
68 const char *endpoint;
69 } 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;
81 STARTUPINFOW si;
82 WCHAR cmd[MAX_PATH];
83 static const WCHAR rpcss[] = {'\\','r','p','c','s','s','.','e','x','e',0};
84 BOOL rslt;
85 void *redir;
87 TRACE("\n");
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 );
98 if (rslt)
100 CloseHandle(pi.hProcess);
101 CloseHandle(pi.hThread);
102 Sleep(100);
105 return rslt;
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;
123 RPC_STATUS status;
124 RpcBinding* epm_bind;
125 unsigned int i;
127 if (bind->server)
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;
134 if (!pszEndpoint)
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);
152 TRACE("RPC_S_OK\n");
153 return RPC_S_OK;
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;
170 default:
171 return EXCEPTION_EXECUTE_HANDLER;
175 /***********************************************************************
176 * RpcEpRegisterA (RPCRT4.@)
178 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
179 UUID_VECTOR *UuidVector, RPC_CSTR Annotation )
181 PRPC_SERVER_INTERFACE If = IfSpec;
182 ULONG i;
183 RPC_STATUS status = RPC_S_OK;
184 error_status_t status2;
185 ept_entry_t *entries;
186 handle_t handle;
188 TRACE("(%p,%p,%p,%s)\n", IfSpec, BindingVector, UuidVector, debugstr_a((char*)Annotation));
189 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
190 for (i=0; i<BindingVector->Count; i++) {
191 RpcBinding* bind = BindingVector->BindingH[i];
192 TRACE(" protseq[%d]=%s\n", i, debugstr_a(bind->Protseq));
193 TRACE(" endpoint[%d]=%s\n", i, debugstr_a(bind->Endpoint));
195 if (UuidVector) {
196 for (i=0; i<UuidVector->Count; i++)
197 TRACE(" obj[%d]=%s\n", i, debugstr_guid(UuidVector->Uuid[i]));
200 if (!BindingVector->Count) return RPC_S_OK;
202 entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*entries) * BindingVector->Count * (UuidVector ? UuidVector->Count : 1));
203 if (!entries)
204 return RPC_S_OUT_OF_MEMORY;
206 status = get_epm_handle_server(&handle);
207 if (status != RPC_S_OK)
209 HeapFree(GetProcessHeap(), 0, entries);
210 return status;
213 for (i = 0; i < BindingVector->Count; i++)
215 unsigned j;
216 RpcBinding* bind = BindingVector->BindingH[i];
217 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
219 status = TowerConstruct(&If->InterfaceId, &If->TransferSyntax,
220 bind->Protseq, bind->Endpoint,
221 bind->NetworkAddr,
222 &entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
223 if (status != RPC_S_OK) break;
225 if (UuidVector)
226 memcpy(&entries[i * UuidVector->Count].object, &UuidVector->Uuid[j], sizeof(GUID));
227 else
228 memset(&entries[i].object, 0, sizeof(entries[i].object));
229 if (Annotation)
230 memcpy(entries[i].annotation, Annotation,
231 min(strlen((char *)Annotation) + 1, ept_max_annotation_size));
235 if (status == RPC_S_OK)
237 while (TRUE)
239 __TRY
241 ept_insert(handle, BindingVector->Count * (UuidVector ? UuidVector->Count : 1),
242 entries, TRUE, &status2);
244 __EXCEPT(rpc_filter)
246 status2 = GetExceptionCode();
248 __ENDTRY
249 if (status2 == RPC_S_SERVER_UNAVAILABLE &&
250 is_epm_destination_local(handle))
252 if (start_rpcss())
253 continue;
255 if (status2 != RPC_S_OK)
256 ERR("ept_insert failed with error %d\n", status2);
257 status = status2; /* FIXME: convert status? */
258 break;
261 RpcBindingFree(&handle);
263 for (i = 0; i < BindingVector->Count; i++)
265 unsigned j;
266 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
267 I_RpcFree(entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
270 HeapFree(GetProcessHeap(), 0, entries);
272 return status;
275 /***********************************************************************
276 * RpcEpRegisterW (RPCRT4.@)
278 RPC_STATUS WINAPI RpcEpRegisterW( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
279 UUID_VECTOR *UuidVector, RPC_WSTR Annotation )
281 LPSTR annA = RPCRT4_strdupWtoA(Annotation);
282 RPC_STATUS status;
284 status = RpcEpRegisterA(IfSpec, BindingVector, UuidVector, (RPC_CSTR)annA);
286 HeapFree(GetProcessHeap(), 0, annA);
287 return status;
290 /***********************************************************************
291 * RpcEpUnregister (RPCRT4.@)
293 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
294 UUID_VECTOR *UuidVector )
296 PRPC_SERVER_INTERFACE If = IfSpec;
297 ULONG i;
298 RPC_STATUS status = RPC_S_OK;
299 error_status_t status2;
300 ept_entry_t *entries;
301 handle_t handle;
303 TRACE("(%p,%p,%p)\n", IfSpec, BindingVector, UuidVector);
304 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
305 for (i=0; i<BindingVector->Count; i++) {
306 RpcBinding* bind = BindingVector->BindingH[i];
307 TRACE(" protseq[%d]=%s\n", i, debugstr_a(bind->Protseq));
308 TRACE(" endpoint[%d]=%s\n", i, debugstr_a(bind->Endpoint));
310 if (UuidVector) {
311 for (i=0; i<UuidVector->Count; i++)
312 TRACE(" obj[%d]=%s\n", i, debugstr_guid(UuidVector->Uuid[i]));
315 entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*entries) * BindingVector->Count * (UuidVector ? UuidVector->Count : 1));
316 if (!entries)
317 return RPC_S_OUT_OF_MEMORY;
319 status = get_epm_handle_server(&handle);
320 if (status != RPC_S_OK)
322 HeapFree(GetProcessHeap(), 0, entries);
323 return status;
326 for (i = 0; i < BindingVector->Count; i++)
328 unsigned j;
329 RpcBinding* bind = BindingVector->BindingH[i];
330 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
332 status = TowerConstruct(&If->InterfaceId, &If->TransferSyntax,
333 bind->Protseq, bind->Endpoint,
334 bind->NetworkAddr,
335 &entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
336 if (status != RPC_S_OK) break;
338 if (UuidVector)
339 memcpy(&entries[i * UuidVector->Count + j].object, &UuidVector->Uuid[j], sizeof(GUID));
340 else
341 memset(&entries[i].object, 0, sizeof(entries[i].object));
345 if (status == RPC_S_OK)
347 __TRY
349 ept_insert(handle, BindingVector->Count * (UuidVector ? UuidVector->Count : 1),
350 entries, TRUE, &status2);
352 __EXCEPT(rpc_filter)
354 status2 = GetExceptionCode();
356 __ENDTRY
357 if (status2 == RPC_S_SERVER_UNAVAILABLE)
358 status2 = EPT_S_NOT_REGISTERED;
359 if (status2 != RPC_S_OK)
360 ERR("ept_insert failed with error %d\n", status2);
361 status = status2; /* FIXME: convert status? */
363 RpcBindingFree(&handle);
365 for (i = 0; i < BindingVector->Count; i++)
367 unsigned j;
368 for (j = 0; j < (UuidVector ? UuidVector->Count : 1); j++)
369 I_RpcFree(entries[i*(UuidVector ? UuidVector->Count : 1) + j].tower);
372 HeapFree(GetProcessHeap(), 0, entries);
374 return status;
377 /***********************************************************************
378 * RpcEpResolveBinding (RPCRT4.@)
380 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
382 PRPC_CLIENT_INTERFACE If = IfSpec;
383 RpcBinding* bind = Binding;
384 RPC_STATUS status;
385 error_status_t status2;
386 handle_t handle;
387 ept_lookup_handle_t entry_handle = NULL;
388 twr_t *tower;
389 twr_t *towers[4] = { NULL };
390 unsigned32 num_towers, i;
391 GUID uuid = GUID_NULL;
392 char *resolved_endpoint = NULL;
394 TRACE("(%p,%p)\n", Binding, IfSpec);
395 TRACE(" protseq=%s\n", debugstr_a(bind->Protseq));
396 TRACE(" obj=%s\n", debugstr_guid(&bind->ObjectUuid));
397 TRACE(" networkaddr=%s\n", debugstr_a(bind->NetworkAddr));
398 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
400 /* just return for fully bound handles */
401 if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
402 return RPC_S_OK;
404 status = get_epm_handle_client(Binding, &handle);
405 if (status != RPC_S_OK) return status;
407 status = TowerConstruct(&If->InterfaceId, &If->TransferSyntax, bind->Protseq,
408 ((RpcBinding *)handle)->Endpoint,
409 bind->NetworkAddr, &tower);
410 if (status != RPC_S_OK)
412 WARN("couldn't get tower\n");
413 RpcBindingFree(&handle);
414 return status;
417 while (TRUE)
419 __TRY
421 ept_map(handle, &uuid, tower, &entry_handle, sizeof(towers)/sizeof(towers[0]), &num_towers, towers, &status2);
422 /* FIXME: translate status2? */
424 __EXCEPT(rpc_filter)
426 status2 = GetExceptionCode();
428 __ENDTRY
429 if (status2 == RPC_S_SERVER_UNAVAILABLE &&
430 is_epm_destination_local(handle))
432 if (start_rpcss())
433 continue;
435 break;
438 RpcBindingFree(&handle);
439 I_RpcFree(tower);
441 if (status2 != RPC_S_OK)
443 ERR("ept_map failed for ifid %s, protseq %s, networkaddr %s\n", debugstr_guid(&If->TransferSyntax.SyntaxGUID), bind->Protseq, bind->NetworkAddr);
444 return status2;
447 for (i = 0; i < num_towers; i++)
449 /* only parse the tower if we haven't already found a suitable
450 * endpoint, otherwise just free the tower */
451 if (!resolved_endpoint)
453 status = TowerExplode(towers[i], NULL, NULL, NULL, &resolved_endpoint, NULL);
454 TRACE("status = %d\n", status);
456 I_RpcFree(towers[i]);
459 if (resolved_endpoint)
461 RPCRT4_ResolveBinding(Binding, resolved_endpoint);
462 I_RpcFree(resolved_endpoint);
463 return RPC_S_OK;
466 WARN("couldn't find an endpoint\n");
467 return EPT_S_NOT_REGISTERED;
470 /*****************************************************************************
471 * TowerExplode (RPCRT4.@)
473 RPC_STATUS WINAPI TowerExplode(
474 const twr_t *tower, PRPC_SYNTAX_IDENTIFIER object, PRPC_SYNTAX_IDENTIFIER syntax,
475 char **protseq, char **endpoint, char **address)
477 size_t tower_size;
478 RPC_STATUS status;
479 const unsigned char *p;
480 u_int16 floor_count;
481 const twr_uuid_floor_t *object_floor;
482 const twr_uuid_floor_t *syntax_floor;
484 TRACE("(%p, %p, %p, %p, %p, %p)\n", tower, object, syntax, protseq,
485 endpoint, address);
487 if (protseq)
488 *protseq = NULL;
489 if (endpoint)
490 *endpoint = NULL;
491 if (address)
492 *address = NULL;
494 tower_size = tower->tower_length;
496 if (tower_size < sizeof(u_int16))
497 return EPT_S_NOT_REGISTERED;
499 p = &tower->tower_octet_string[0];
501 floor_count = *(const u_int16 *)p;
502 p += sizeof(u_int16);
503 tower_size -= sizeof(u_int16);
504 TRACE("floor_count: %d\n", floor_count);
505 /* FIXME: should we do something with the floor count? at the moment we don't */
507 if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
508 return EPT_S_NOT_REGISTERED;
510 object_floor = (const twr_uuid_floor_t *)p;
511 p += sizeof(*object_floor);
512 tower_size -= sizeof(*object_floor);
513 syntax_floor = (const twr_uuid_floor_t *)p;
514 p += sizeof(*syntax_floor);
515 tower_size -= sizeof(*syntax_floor);
517 if ((object_floor->count_lhs != sizeof(object_floor->protid) +
518 sizeof(object_floor->uuid) + sizeof(object_floor->major_version)) ||
519 (object_floor->protid != EPM_PROTOCOL_UUID) ||
520 (object_floor->count_rhs != sizeof(object_floor->minor_version)))
521 return EPT_S_NOT_REGISTERED;
523 if ((syntax_floor->count_lhs != sizeof(syntax_floor->protid) +
524 sizeof(syntax_floor->uuid) + sizeof(syntax_floor->major_version)) ||
525 (syntax_floor->protid != EPM_PROTOCOL_UUID) ||
526 (syntax_floor->count_rhs != sizeof(syntax_floor->minor_version)))
527 return EPT_S_NOT_REGISTERED;
529 status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
530 if ((status == RPC_S_OK) && syntax && object)
532 syntax->SyntaxGUID = syntax_floor->uuid;
533 syntax->SyntaxVersion.MajorVersion = syntax_floor->major_version;
534 syntax->SyntaxVersion.MinorVersion = syntax_floor->minor_version;
535 object->SyntaxGUID = object_floor->uuid;
536 object->SyntaxVersion.MajorVersion = object_floor->major_version;
537 object->SyntaxVersion.MinorVersion = object_floor->minor_version;
539 return status;
542 /***********************************************************************
543 * TowerConstruct (RPCRT4.@)
545 RPC_STATUS WINAPI TowerConstruct(
546 const RPC_SYNTAX_IDENTIFIER *object, const RPC_SYNTAX_IDENTIFIER *syntax,
547 const char *protseq, const char *endpoint, const char *address,
548 twr_t **tower)
550 size_t tower_size;
551 RPC_STATUS status;
552 unsigned char *p;
553 twr_uuid_floor_t *object_floor;
554 twr_uuid_floor_t *syntax_floor;
556 TRACE("(%p, %p, %s, %s, %s, %p)\n", object, syntax, debugstr_a(protseq),
557 debugstr_a(endpoint), debugstr_a(address), tower);
559 *tower = NULL;
561 status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
563 if (status != RPC_S_OK)
564 return status;
566 tower_size += sizeof(u_int16) + sizeof(*object_floor) + sizeof(*syntax_floor);
567 *tower = I_RpcAllocate(FIELD_OFFSET(twr_t, tower_octet_string[tower_size]));
568 if (!*tower)
569 return RPC_S_OUT_OF_RESOURCES;
571 (*tower)->tower_length = tower_size;
572 p = &(*tower)->tower_octet_string[0];
573 *(u_int16 *)p = 5; /* number of floors */
574 p += sizeof(u_int16);
575 object_floor = (twr_uuid_floor_t *)p;
576 p += sizeof(*object_floor);
577 syntax_floor = (twr_uuid_floor_t *)p;
578 p += sizeof(*syntax_floor);
580 object_floor->count_lhs = sizeof(object_floor->protid) + sizeof(object_floor->uuid) +
581 sizeof(object_floor->major_version);
582 object_floor->protid = EPM_PROTOCOL_UUID;
583 object_floor->count_rhs = sizeof(object_floor->minor_version);
584 object_floor->uuid = object->SyntaxGUID;
585 object_floor->major_version = object->SyntaxVersion.MajorVersion;
586 object_floor->minor_version = object->SyntaxVersion.MinorVersion;
588 syntax_floor->count_lhs = sizeof(syntax_floor->protid) + sizeof(syntax_floor->uuid) +
589 sizeof(syntax_floor->major_version);
590 syntax_floor->protid = EPM_PROTOCOL_UUID;
591 syntax_floor->count_rhs = sizeof(syntax_floor->minor_version);
592 syntax_floor->uuid = syntax->SyntaxGUID;
593 syntax_floor->major_version = syntax->SyntaxVersion.MajorVersion;
594 syntax_floor->minor_version = syntax->SyntaxVersion.MinorVersion;
596 status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
597 if (status != RPC_S_OK)
599 I_RpcFree(*tower);
600 *tower = NULL;
601 return status;
603 return RPC_S_OK;
606 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
608 return HeapAlloc(GetProcessHeap(), 0, len);
611 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
613 HeapFree(GetProcessHeap(), 0, ptr);