winex11: Remove an unused variable.
[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
33 #include "rpc.h"
34
35 #include "wine/debug.h"
36
37 #include "rpc_binding.h"
38 #include "epm_towers.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 /* The "real" RPC portmapper endpoints that I know of are:
43  *
44  *  ncadg_ip_udp: 135
45  *  ncacn_ip_tcp: 135
46  *  ncacn_np: \\pipe\epmapper (?)
47  *  ncalrpc: epmapper
48  *
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)
60  *
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...
64  */
65
66 /***********************************************************************
67  *             RpcEpRegisterA (RPCRT4.@)
68  */
69 RPC_STATUS WINAPI RpcEpRegisterA( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
70                                   UUID_VECTOR *UuidVector, RPC_CSTR Annotation )
71 {
72   RPCSS_NP_MESSAGE msg;
73   RPCSS_NP_REPLY reply;
74   char *vardata_payload, *vp;
75   PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
76   unsigned long c;
77   RPC_STATUS rslt = RPC_S_OK;
78
79   TRACE("(%p,%p,%p,%s)\n", IfSpec, BindingVector, UuidVector, debugstr_a((char*)Annotation));
80   TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
81   for (c=0; c<BindingVector->Count; c++) {
82     RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
83     TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
84     TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
85   }
86   if (UuidVector) {
87     for (c=0; c<UuidVector->Count; c++)
88       TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
89   }
90
91   /* FIXME: Do something with annotation. */
92
93   /* construct the message to rpcss */
94   msg.message_type = RPCSS_NP_MESSAGE_TYPEID_REGISTEREPMSG;
95   msg.message.registerepmsg.iface = If->InterfaceId;
96   msg.message.registerepmsg.no_replace = 0;
97
98   msg.message.registerepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
99   msg.message.registerepmsg.binding_count = BindingVector->Count;
100
101   /* calculate vardata payload size */
102   msg.vardata_payload_size = msg.message.registerepmsg.object_count * sizeof(UUID);
103   for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
104     RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
105     msg.vardata_payload_size += strlen(bind->Protseq) + 1;
106     msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
107   }
108
109   /* allocate the payload buffer */
110   vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
111   if (!vardata_payload)
112     return RPC_S_OUT_OF_MEMORY;
113
114   /* populate the payload data */
115   for (c=0; c < msg.message.registerepmsg.object_count; c++) {
116     CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
117     vp += sizeof(UUID);
118   }
119
120   for (c=0; c < msg.message.registerepmsg.binding_count; c++) {
121     RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
122     unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
123     CopyMemory(vp, bind->Protseq, pslen);
124     vp += pslen;
125     CopyMemory(vp, bind->Endpoint, eplen);
126     vp += eplen;
127   }
128
129   /* send our request */
130   if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
131     rslt = RPC_S_OUT_OF_MEMORY;
132
133   /* free the payload buffer */
134   LocalFree(vardata_payload);
135
136   return rslt;
137 }
138
139 /***********************************************************************
140  *             RpcEpUnregister (RPCRT4.@)
141  */
142 RPC_STATUS WINAPI RpcEpUnregister( RPC_IF_HANDLE IfSpec, RPC_BINDING_VECTOR *BindingVector,
143                                    UUID_VECTOR *UuidVector )
144 {
145   RPCSS_NP_MESSAGE msg;
146   RPCSS_NP_REPLY reply;
147   char *vardata_payload, *vp;
148   PRPC_SERVER_INTERFACE If = (PRPC_SERVER_INTERFACE)IfSpec;
149   unsigned long c;
150   RPC_STATUS rslt = RPC_S_OK;
151
152   TRACE("(%p,%p,%p)\n", IfSpec, BindingVector, UuidVector);
153   TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
154   for (c=0; c<BindingVector->Count; c++) {
155     RpcBinding* bind = (RpcBinding*)(BindingVector->BindingH[c]);
156     TRACE(" protseq[%ld]=%s\n", c, debugstr_a(bind->Protseq));
157     TRACE(" endpoint[%ld]=%s\n", c, debugstr_a(bind->Endpoint));
158   }
159   if (UuidVector) {
160     for (c=0; c<UuidVector->Count; c++)
161       TRACE(" obj[%ld]=%s\n", c, debugstr_guid(UuidVector->Uuid[c]));
162   }
163
164   /* construct the message to rpcss */
165   msg.message_type = RPCSS_NP_MESSAGE_TYPEID_UNREGISTEREPMSG;
166   msg.message.unregisterepmsg.iface = If->InterfaceId;
167
168   msg.message.unregisterepmsg.object_count = (UuidVector) ? UuidVector->Count : 0;
169   msg.message.unregisterepmsg.binding_count = BindingVector->Count;
170
171   /* calculate vardata payload size */
172   msg.vardata_payload_size = msg.message.unregisterepmsg.object_count * sizeof(UUID);
173   for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
174     RpcBinding *bind = (RpcBinding *)(BindingVector->BindingH[c]);
175     msg.vardata_payload_size += strlen(bind->Protseq) + 1;
176     msg.vardata_payload_size += strlen(bind->Endpoint) + 1;
177   }
178
179   /* allocate the payload buffer */
180   vp = vardata_payload = LocalAlloc(LPTR, msg.vardata_payload_size);
181   if (!vardata_payload)
182     return RPC_S_OUT_OF_MEMORY;
183
184   /* populate the payload data */
185   for (c=0; c < msg.message.unregisterepmsg.object_count; c++) {
186     CopyMemory(vp, UuidVector->Uuid[c], sizeof(UUID));
187     vp += sizeof(UUID);
188   }
189
190   for (c=0; c < msg.message.unregisterepmsg.binding_count; c++) {
191     RpcBinding *bind = (RpcBinding*)(BindingVector->BindingH[c]);
192     unsigned long pslen = strlen(bind->Protseq) + 1, eplen = strlen(bind->Endpoint) + 1;
193     CopyMemory(vp, bind->Protseq, pslen);
194     vp += pslen;
195     CopyMemory(vp, bind->Endpoint, eplen);
196     vp += eplen;
197   }
198
199   /* send our request */
200   if (!RPCRT4_RPCSSOnDemandCall(&msg, vardata_payload, &reply))
201     rslt = RPC_S_OUT_OF_MEMORY;
202
203   /* free the payload buffer */
204   LocalFree(vardata_payload);
205
206   return rslt;
207 }
208
209 /***********************************************************************
210  *             RpcEpResolveBinding (RPCRT4.@)
211  */
212 RPC_STATUS WINAPI RpcEpResolveBinding( RPC_BINDING_HANDLE Binding, RPC_IF_HANDLE IfSpec )
213 {
214   RPCSS_NP_MESSAGE msg;
215   RPCSS_NP_REPLY reply;
216   PRPC_CLIENT_INTERFACE If = (PRPC_CLIENT_INTERFACE)IfSpec;
217   RpcBinding* bind = (RpcBinding*)Binding;
218
219   TRACE("(%p,%p)\n", Binding, IfSpec);
220   TRACE(" protseq=%s\n", debugstr_a(bind->Protseq));
221   TRACE(" obj=%s\n", debugstr_guid(&bind->ObjectUuid));
222   TRACE(" ifid=%s\n", debugstr_guid(&If->InterfaceId.SyntaxGUID));
223
224   /* FIXME: totally untested */
225
226   /* just return for fully bound handles */
227   if (bind->Endpoint && (bind->Endpoint[0] != '\0'))
228     return RPC_S_OK;
229
230   /* construct the message to rpcss */
231   msg.message_type = RPCSS_NP_MESSAGE_TYPEID_RESOLVEEPMSG;
232   msg.message.resolveepmsg.iface = If->InterfaceId;
233   msg.message.resolveepmsg.object = bind->ObjectUuid;
234  
235   msg.vardata_payload_size = strlen(bind->Protseq) + 1;
236
237   /* send the message */
238   if (!RPCRT4_RPCSSOnDemandCall(&msg, bind->Protseq, &reply))
239     return RPC_S_OUT_OF_MEMORY;
240
241   /* empty-string result means not registered */
242   if (reply.as_string[0] == '\0')
243     return EPT_S_NOT_REGISTERED;
244   
245   /* otherwise we fully bind the handle & return RPC_S_OK */
246   return RPCRT4_ResolveBinding(Binding, reply.as_string);
247 }
248
249 typedef unsigned int unsigned32;
250 typedef struct twr_t
251     {
252     unsigned32 tower_length;
253     /* [size_is] */ BYTE tower_octet_string[ 1 ];
254     }   twr_t;
255
256 /***********************************************************************
257  *             TowerExplode (RPCRT4.@)
258  */
259 RPC_STATUS WINAPI TowerExplode(
260     const twr_t *tower, PRPC_SYNTAX_IDENTIFIER object, PRPC_SYNTAX_IDENTIFIER syntax,
261     char **protseq, char **endpoint, char **address)
262 {
263     size_t tower_size;
264     RPC_STATUS status;
265     const unsigned char *p;
266     u_int16 floor_count;
267     const twr_uuid_floor_t *object_floor;
268     const twr_uuid_floor_t *syntax_floor;
269
270     if (protseq)
271         *protseq = NULL;
272     if (endpoint)
273         *endpoint = NULL;
274     if (address)
275         *address = NULL;
276
277     tower_size = tower->tower_length;
278
279     if (tower_size < sizeof(u_int16))
280         return EPT_S_NOT_REGISTERED;
281
282     p = &tower->tower_octet_string[0];
283
284     floor_count = *(const u_int16 *)p;
285     p += sizeof(u_int16);
286     tower_size -= sizeof(u_int16);
287     TRACE("floor_count: %d\n", floor_count);
288     /* FIXME: should we do something with the floor count? at the moment we don't */
289
290     if (tower_size < sizeof(*object_floor) + sizeof(*syntax_floor))
291         return EPT_S_NOT_REGISTERED;
292
293     object_floor = (const twr_uuid_floor_t *)p;
294     p += sizeof(*object_floor);
295     tower_size -= sizeof(*object_floor);
296     syntax_floor = (const twr_uuid_floor_t *)p;
297     p += sizeof(*syntax_floor);
298     tower_size -= sizeof(*syntax_floor);
299
300     if ((object_floor->count_lhs != sizeof(object_floor->protid) +
301         sizeof(object_floor->uuid) + sizeof(object_floor->major_version)) ||
302         (object_floor->protid != EPM_PROTOCOL_UUID) ||
303         (object_floor->count_rhs != sizeof(object_floor->minor_version)))
304         return EPT_S_NOT_REGISTERED;
305
306     if ((syntax_floor->count_lhs != sizeof(syntax_floor->protid) +
307         sizeof(syntax_floor->uuid) + sizeof(syntax_floor->major_version)) ||
308         (syntax_floor->protid != EPM_PROTOCOL_UUID) ||
309         (syntax_floor->count_rhs != sizeof(syntax_floor->minor_version)))
310         return EPT_S_NOT_REGISTERED;
311
312     status = RpcTransport_ParseTopOfTower(p, tower_size, protseq, address, endpoint);
313     if ((status == RPC_S_OK) && syntax && object)
314     {
315         syntax->SyntaxGUID = syntax_floor->uuid;
316         syntax->SyntaxVersion.MajorVersion = syntax_floor->major_version;
317         syntax->SyntaxVersion.MinorVersion = syntax_floor->minor_version;
318         object->SyntaxGUID = object_floor->uuid;
319         object->SyntaxVersion.MajorVersion = object_floor->major_version;
320         object->SyntaxVersion.MinorVersion = object_floor->minor_version;
321     }
322     return status;
323 }
324
325 /***********************************************************************
326  *             TowerConstruct (RPCRT4.@)
327  */
328 RPC_STATUS WINAPI TowerConstruct(
329     const RPC_SYNTAX_IDENTIFIER *object, const RPC_SYNTAX_IDENTIFIER *syntax,
330     const char *protseq, const char *endpoint, const char *address,
331     twr_t **tower)
332 {
333     size_t tower_size;
334     RPC_STATUS status;
335     unsigned char *p;
336     twr_uuid_floor_t *object_floor;
337     twr_uuid_floor_t *syntax_floor;
338
339     *tower = NULL;
340
341     status = RpcTransport_GetTopOfTower(NULL, &tower_size, protseq, address, endpoint);
342
343     if (status != RPC_S_OK)
344         return status;
345
346     tower_size += sizeof(u_int16) + sizeof(*object_floor) + sizeof(*syntax_floor);
347     *tower = I_RpcAllocate(FIELD_OFFSET(twr_t, tower_octet_string[tower_size]));
348     if (!*tower)
349         return RPC_S_OUT_OF_RESOURCES;
350
351     (*tower)->tower_length = tower_size;
352     p = &(*tower)->tower_octet_string[0];
353     *(u_int16 *)p = 5; /* number of floors */
354     p += sizeof(u_int16);
355     object_floor = (twr_uuid_floor_t *)p;
356     p += sizeof(*object_floor);
357     syntax_floor = (twr_uuid_floor_t *)p;
358     p += sizeof(*syntax_floor);
359
360     object_floor->count_lhs = sizeof(object_floor->protid) + sizeof(object_floor->uuid) +
361                               sizeof(object_floor->major_version);
362     object_floor->protid = EPM_PROTOCOL_UUID;
363     object_floor->count_rhs = sizeof(object_floor->minor_version);
364     object_floor->uuid = object->SyntaxGUID;
365     object_floor->major_version = object->SyntaxVersion.MajorVersion;
366     object_floor->minor_version = object->SyntaxVersion.MinorVersion;
367
368     syntax_floor->count_lhs = sizeof(syntax_floor->protid) + sizeof(syntax_floor->uuid) +
369                               sizeof(syntax_floor->major_version);
370     syntax_floor->protid = EPM_PROTOCOL_UUID;
371     syntax_floor->count_rhs = sizeof(syntax_floor->minor_version);
372     syntax_floor->uuid = syntax->SyntaxGUID;
373     syntax_floor->major_version = syntax->SyntaxVersion.MajorVersion;
374     syntax_floor->minor_version = syntax->SyntaxVersion.MinorVersion;
375
376     status = RpcTransport_GetTopOfTower(p, &tower_size, protseq, address, endpoint);
377     if (status != RPC_S_OK)
378     {
379         I_RpcFree(*tower);
380         *tower = NULL;
381         return status;
382     }
383     return RPC_S_OK;
384 }