wine.inf: We should not override existing associations.
[wine/hacks.git] / dlls / rpcrt4 / rpc_epmap.c
blobb3c32371c268c1fe5483e0cf8f90c20ed763fcd7
1 /*
2 * RPC endpoint mapper
4 * Copyright 2002 Greg Turner
5 * Copyright 2001 Ove Kåven, TransGaming Technologies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * - actually do things right
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winreg.h"
34 #include "rpc.h"
36 #include "wine/debug.h"
38 #include "rpc_binding.h"
39 #include "epm_towers.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /* The "real" RPC portmapper endpoints that I know of are:
45 * ncadg_ip_udp: 135
46 * ncacn_ip_tcp: 135
47 * ncacn_np: \\pipe\epmapper (?)
48 * ncalrpc: epmapper
50 * If the user's machine ran a DCE RPC daemon, it would
51 * probably be possible to connect to it, but there are many
52 * reasons not to, like:
53 * - the user probably does *not* run one, and probably
54 * shouldn't be forced to run one just for local COM
55 * - very few Unix systems use DCE RPC... if they run a RPC
56 * daemon at all, it's usually Sun RPC
57 * - DCE RPC registrations are persistent and saved on disk,
58 * while MS-RPC registrations are documented as non-persistent
59 * and stored only in RAM, and auto-destroyed when the process
60 * dies (something DCE RPC can't do)
62 * Of course, if the user *did* want to run a DCE RPC daemon anyway,
63 * there would be interoperability advantages, like the possibility
64 * of running a fully functional DCOM server using Wine...
67 /***********************************************************************
68 * RpcEpRegisterA (RPCRT4.@)
70 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
71 UUID_VECTOR *UuidVector, unsigned char *Annotation )
73 RPCSS_NP_MESSAGE msg;
74 RPCSS_NP_REPLY reply;
75 char *vardata_payload, *vp;
76 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
77 unsigned long c;
78 RPC_STATUS rslt = RPC_S_OK;
80 TRACE("(%p,%p,%p,%s)\n", IfSpec, BindingVector, UuidVector, debugstr_a((char*)Annotation));
81 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
82 for (c=0; c<BindingVector->Count; c++) {
83 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
84 TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
85 TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
87 if (UuidVector) {
88 for (c=0; c<UuidVector->Count; c++)
89 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
92 /* FIXME: Do something with annotation. */
94 /* construct the message to rpcss */
95 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_REGISTEREPMSG;
96 msg.message.registerepmsg.iface = If->InterfaceId;
97 msg.message.registerepmsg.no_replace = 0;
99 msg.message.registerepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
100 msg.message.registerepmsg.binding_count = BindingVector->Count;
102 /* calculate vardata payload size */
103 msg.vardata_payload_size = msg.message.registerepmsg.object_count * sizeof(UUID);
104 for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
105 RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
106 msg.vardata_payload_size += strlen(bind->Protseq) + 1;
107 msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
110 /* allocate the payload buffer */
111 vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
112 if (!vardata_payload)
113 return RPC_S_OUT_OF_MEMORY;
115 /* populate the payload data */
116 for (c=0; c < msg.message.registerepmsg.object_count; c++) {
117 CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
118 vp += sizeof(UUID);
121 for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
122 RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
123 unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
124 CopyMemory(vp, bind->Protseq, pslen);
125 vp += pslen;
126 CopyMemory(vp, bind->Endpoint, eplen);
127 vp += eplen;
130 /* send our request */
131 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
132 rslt = RPC_S_OUT_OF_MEMORY;
134 /* free the payload buffer */
135 LocalFree(vardata_payload);
137 return rslt;
140 /***********************************************************************
141 * RpcEpUnregister (RPCRT4.@)
143 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
144 UUID_VECTOR *UuidVector )
146 RPCSS_NP_MESSAGE msg;
147 RPCSS_NP_REPLY reply;
148 char *vardata_payload, *vp;
149 PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
150 unsigned long c;
151 RPC_STATUS rslt = RPC_S_OK;
153 TRACE("(%p,%p,%p)\n", IfSpec, BindingVector, UuidVector);
154 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
155 for (c=0; c<BindingVector->Count; c++) {
156 RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
157 TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
158 TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
160 if (UuidVector) {
161 for (c=0; c<UuidVector->Count; c++)
162 TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
165 /* construct the message to rpcss */
166 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG;
167 msg.message.unregisterepmsg.iface = If->InterfaceId;
169 msg.message.unregisterepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
170 msg.message.unregisterepmsg.binding_count = BindingVector->Count;
172 /* calculate vardata payload size */
173 msg.vardata_payload_size = msg.message.unregisterepmsg.object_count * sizeof(UUID);
174 for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
175 RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
176 msg.vardata_payload_size += strlen(bind->Protseq) + 1;
177 msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
180 /* allocate the payload buffer */
181 vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
182 if (!vardata_payload)
183 return RPC_S_OUT_OF_MEMORY;
185 /* populate the payload data */
186 for (c=0; c < msg.message.unregisterepmsg.object_count; c++) {
187 CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
188 vp += sizeof(UUID);
191 for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
192 RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
193 unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
194 CopyMemory(vp, bind->Protseq, pslen);
195 vp += pslen;
196 CopyMemory(vp, bind->Endpoint, eplen);
197 vp += eplen;
200 /* send our request */
201 if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
202 rslt = RPC_S_OUT_OF_MEMORY;
204 /* free the payload buffer */
205 LocalFree(vardata_payload);
207 return rslt;
210 /***********************************************************************
211 * RpcEpResolveBinding (RPCRT4.@)
213 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
215 RPCSS_NP_MESSAGE msg;
216 RPCSS_NP_REPLY reply;
217 PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
218 RpcBinding* bind = (RpcBinding*)Binding;
220 TRACE("(%p,%p)\n", Binding, IfSpec);
221 TRACE(" protseq=%s\n", debugstr_a(bind->Protseq));
222 TRACE(" obj=%s\n", debugstr_guid(&bind->ObjectUuid));
223 TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
225 /* FIXME: totally untested */
227 /* just return for fully bound handles */
228 if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
229 return RPC_S_OK;
231 /* construct the message to rpcss */
232 msg.message_type = RPCSS_NP_MESSAGE_TYPEID_RESOLVEEPMSG;
233 msg.message.resolveepmsg.iface = If->InterfaceId;
234 msg.message.resolveepmsg.object = bind->ObjectUuid;
236 msg.vardata_payload_size = strlen(bind->Protseq) + 1;
238 /* send the message */
239 if (!RPCRT4_RPCSSOnDemandCall(&msg, bind->Protseq, &reply))
240 return RPC_S_OUT_OF_MEMORY;
242 /* empty-string result means not registered */
243 if (reply.as_string[0] == '\0')
244 return EPT_S_NOT_REGISTERED;
246 /* otherwise we fully bind the handle & return RPC_S_OK */
247 return RPCRT4_ResolveBinding(Binding, reply.as_string);
250 typedef unsigned int unsigned32;
251 typedef struct twr_t
253 unsigned32 tower_length;
254 /* [size_is] */ byte tower_octet_string[ 1 ];
255 } twr_t;
257 RPC_STATUS WINAPI TowerExplode(
258 const twr_t *tower, RPC_SYNTAX_IDENTIFIER *object, RPC_SYNTAX_IDENTIFIER *syntax,
259 char **protseq, char **endpoint, char **address)
261 size_t tower_size;
262 RPC_STATUS status;
263 const unsigned char *p;
264 u_int16 floor_count;
265 const twr_uuid_floor_t *object_floor;
266 const twr_uuid_floor_t *syntax_floor;
268 if (protseq)
269 *protseq = NULL;
270 if (endpoint)
271 *endpoint = NULL;
272 if (address)
273 *address = NULL;
275 tower_size = tower->tower_length;
277 if (tower_size < sizeof(u_int16))
278 return EPT_S_NOT_REGISTERED;
280 p = &tower->tower_octet_string[0];
282 floor_count = *(const u_int16 *)p;
283 p += sizeof(u_int16);
284 tower_size -= sizeof(u_int16);
285 TRACE("floor_count: %d\n", floor_count);
286 /* FIXME: should we do something with the floor count? at the moment we don't */
288 if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
289 return EPT_S_NOT_REGISTERED;
291 object_floor = (const twr_uuid_floor_t *)p;
292 p += sizeof(*object_floor);
293 tower_size -= sizeof(*object_floor);
294 syntax_floor = (const twr_uuid_floor_t *)p;
295 p += sizeof(*syntax_floor);
296 tower_size -= sizeof(*syntax_floor);
298 if ((object_floor->count_lhs != sizeof(object_floor->protid) +
299 sizeof(object_floor->uuid) + sizeof(object_floor->major_version)) ||
300 (object_floor->protid != EPM_PROTOCOL_UUID) ||
301 (object_floor->count_rhs != sizeof(object_floor->minor_version)))
302 return EPT_S_NOT_REGISTERED;
304 if ((syntax_floor->count_lhs != sizeof(syntax_floor->protid) +
305 sizeof(syntax_floor->uuid) + sizeof(syntax_floor->major_version)) ||
306 (syntax_floor->protid != EPM_PROTOCOL_UUID) ||
307 (syntax_floor->count_rhs != sizeof(syntax_floor->minor_version)))
308 return EPT_S_NOT_REGISTERED;
310 status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
311 if ((status == RPC_S_OK) && syntax && object)
313 syntax->SyntaxGUID = syntax_floor->uuid;
314 syntax->SyntaxVersion.MajorVersion = syntax_floor->major_version;
315 syntax->SyntaxVersion.MinorVersion = syntax_floor->minor_version;
316 object->SyntaxGUID = object_floor->uuid;
317 object->SyntaxVersion.MajorVersion = object_floor->major_version;
318 object->SyntaxVersion.MinorVersion = object_floor->minor_version;
320 return status;
323 RPC_STATUS WINAPI TowerConstruct(
324 const RPC_SYNTAX_IDENTIFIER *object, const RPC_SYNTAX_IDENTIFIER *syntax,
325 const char *protseq, const char *endpoint, const char *address,
326 twr_t **tower)
328 size_t tower_size;
329 RPC_STATUS status;
330 unsigned char *p;
331 twr_uuid_floor_t *object_floor;
332 twr_uuid_floor_t *syntax_floor;
334 *tower = NULL;
336 status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
338 if (status != RPC_S_OK)
339 return status;
341 tower_size += sizeof(u_int16) + sizeof(*object_floor) + sizeof(*syntax_floor);
342 *tower = I_RpcAllocate(FIELD_OFFSET(twr_t, tower_octet_string[tower_size]));
343 if (!*tower)
344 return RPC_S_OUT_OF_RESOURCES;
346 (*tower)->tower_length = tower_size;
347 p = &(*tower)->tower_octet_string[0];
348 *(u_int16 *)p = 5; /* number of floors */
349 p += sizeof(u_int16);
350 object_floor = (twr_uuid_floor_t *)p;
351 p += sizeof(*object_floor);
352 syntax_floor = (twr_uuid_floor_t *)p;
353 p += sizeof(*syntax_floor);
355 object_floor->count_lhs = sizeof(object_floor->protid) + sizeof(object_floor->uuid) +
356 sizeof(object_floor->major_version);
357 object_floor->protid = EPM_PROTOCOL_UUID;
358 object_floor->count_rhs = sizeof(object_floor->minor_version);
359 object_floor->uuid = object->SyntaxGUID;
360 object_floor->major_version = object->SyntaxVersion.MajorVersion;
361 object_floor->minor_version = object->SyntaxVersion.MinorVersion;
363 syntax_floor->count_lhs = sizeof(syntax_floor->protid) + sizeof(syntax_floor->uuid) +
364 sizeof(syntax_floor->major_version);
365 syntax_floor->protid = EPM_PROTOCOL_UUID;
366 syntax_floor->count_rhs = sizeof(syntax_floor->minor_version);
367 syntax_floor->uuid = syntax->SyntaxGUID;
368 syntax_floor->major_version = syntax->SyntaxVersion.MajorVersion;
369 syntax_floor->minor_version = syntax->SyntaxVersion.MinorVersion;
371 status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
372 if (status != RPC_S_OK)
374 I_RpcFree(*tower);
375 *tower = NULL;
376 return status;
378 return RPC_S_OK;