rpcrt4: Tests on NdrCStdStubBuffer_Release to show that we shouldn't call Disconnect.
[wine] / dlls / rpcrt4 / rpc_epmap.c
1 /*
2  * RPC endpoint mapper
3  *
4  * Copyright 2002 Greg Turner
5  * Copyright 2001 Ove Kåven, TransGaming Technologies
6  *
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.
11  *
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.
16  *
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
20  *
21  * TODO:
22  *  - actually do things right
23  */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winreg.h"
33
34 #include "rpc.h"
35
36 #include "wine/debug.h"
37
38 #include "rpc_binding.h"
39 #include "epm_towers.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42
43 /* The "real" RPC portmapper endpoints that I know of are:
44  *
45  *  ncadg_ip_udp: 135
46  *  ncacn_ip_tcp: 135
47  *  ncacn_np: \\pipe\epmapper (?)
48  *  ncalrpc: epmapper
49  *
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)
61  *
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...
65  */
66
67 /***********************************************************************
68  *             RpcEpRegisterA (RPCRT4.@)
69  */
70 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
71                                   UUID_VECTOR *UuidVector, unsigned char *Annotation )
72 {
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;
79
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));
86   }
87   if (UuidVector) {
88     for (c=0; c<UuidVector->Count; c++)
89       TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
90   }
91
92   /* FIXME: Do something with annotation. */
93
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;
98
99   msg.message.registerepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
100   msg.message.registerepmsg.binding_count = BindingVector->Count;
101
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;
108   }
109
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;
114
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);
119   }
120
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;
128   }
129
130   /* send our request */
131   if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
132     rslt = RPC_S_OUT_OF_MEMORY;
133
134   /* free the payload buffer */
135   LocalFree(vardata_payload);
136
137   return rslt;
138 }
139
140 /***********************************************************************
141  *             RpcEpUnregister (RPCRT4.@)
142  */
143 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
144                                    UUID_VECTOR *UuidVector )
145 {
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;
152
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));
159   }
160   if (UuidVector) {
161     for (c=0; c<UuidVector->Count; c++)
162       TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
163   }
164
165   /* construct the message to rpcss */
166   msg.message_type = RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG;
167   msg.message.unregisterepmsg.iface = If->InterfaceId;
168
169   msg.message.unregisterepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
170   msg.message.unregisterepmsg.binding_count = BindingVector->Count;
171
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;
178   }
179
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;
184
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);
189   }
190
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;
198   }
199
200   /* send our request */
201   if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
202     rslt = RPC_S_OUT_OF_MEMORY;
203
204   /* free the payload buffer */
205   LocalFree(vardata_payload);
206
207   return rslt;
208 }
209
210 /***********************************************************************
211  *             RpcEpResolveBinding (RPCRT4.@)
212  */
213 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
214 {
215   RPCSS_NP_MESSAGE msg;
216   RPCSS_NP_REPLY reply;
217   PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
218   RpcBinding* bind = (RpcBinding*)Binding;
219
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));
224
225   /* FIXME: totally untested */
226
227   /* just return for fully bound handles */
228   if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
229     return RPC_S_OK;
230
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;
235  
236   msg.vardata_payload_size = strlen(bind->Protseq) + 1;
237
238   /* send the message */
239   if (!RPCRT4_RPCSSOnDemandCall(&msg, bind->Protseq, &reply))
240     return RPC_S_OUT_OF_MEMORY;
241
242   /* empty-string result means not registered */
243   if (reply.as_string[0] == '\0')
244     return EPT_S_NOT_REGISTERED;
245   
246   /* otherwise we fully bind the handle & return RPC_S_OK */
247   return RPCRT4_ResolveBinding(Binding, reply.as_string);
248 }
249
250 typedef unsigned int unsigned32;
251 typedef struct twr_t
252     {
253     unsigned32 tower_length;
254     /* [size_is] */ byte tower_octet_string[ 1 ];
255     }   twr_t;
256
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)
260 {
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;
267
268     if (protseq)
269         *protseq = NULL;
270     if (endpoint)
271         *endpoint = NULL;
272     if (address)
273         *address = NULL;
274
275     tower_size = tower->tower_length;
276
277     if (tower_size < sizeof(u_int16))
278         return EPT_S_NOT_REGISTERED;
279
280     p = &tower->tower_octet_string[0];
281
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 */
287
288     if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
289         return EPT_S_NOT_REGISTERED;
290
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);
297
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;
303
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;
309
310     status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
311     if ((status == RPC_S_OK) && syntax && object)
312     {
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;
319     }
320     return status;
321 }
322
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)
327 {
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;
333
334     *tower = NULL;
335
336     status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
337
338     if (status != RPC_S_OK)
339         return status;
340
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;
345
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);
354
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;
362
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;
370
371     status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
372     if (status != RPC_S_OK)
373     {
374         I_RpcFree(*tower);
375         *tower = NULL;
376         return status;
377     }
378     return RPC_S_OK;
379 }