change the signature that asn2wrs generates for functions to marm all parameters...
[obnox/wireshark/wip.git] / epan / dissectors / packet-dcom-remunkn.c
1 /* packet-dcom-remunkn.c
2  * Routines for the IRemUnknown interface
3  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 /* see packet-dcom.c for details about DCOM */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <string.h>
33
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include <epan/emem.h>
37 #include "packet-dcerpc.h"
38 #include "packet-dcom.h"
39 #include "guid-utils.h"
40
41
42 static int hf_remunk_opnum = -1;
43
44
45 static int hf_remunk_refs = -1;
46 static int hf_remunk_iids = -1;
47
48 static int hf_remunk_flags = -1;
49 static int hf_remunk_qiresult = -1;
50
51 static gint ett_remunk_reminterfaceref = -1;
52 static int hf_remunk_reminterfaceref = -1;
53 static int hf_remunk_interface_refs = -1;
54 static int hf_remunk_public_refs = -1;
55 static int hf_remunk_private_refs = -1;
56
57
58 static gint ett_remunk_rqi_result = -1;
59
60
61 static gint ett_remunk = -1;
62 static e_uuid_t uuid_remunk = { 0x00000131, 0x0000, 0x0000, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
63 static guint16  ver_remunk = 0;
64 static int proto_remunk = -1;
65
66 static e_uuid_t ipid_remunk = { 0x00000131, 0x1234, 0x5678, { 0xCA, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
67
68 /* There is a little bit confusion about the IRemUnknown2 interface UUIDs */
69 /* DCOM documentation tells us: 0x00000142 (7 methods) */
70 /* win2000 registry tells us: 0x00000142 IRemoteQI (4 methods) */
71 /* win2000 registry tells us: 0x00000143 IRemUnknown2 (7 methods) */
72 /* There is some evidence, that the DCOM documentation is wrong, so using 143 for IRemUnknown2 now. */
73
74 static gint ett_remunk2 = -1;
75 static e_uuid_t uuid_remunk2 = { 0x00000143, 0x0000, 0x0000, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
76 static guint16  ver_remunk2 = 0;
77 static int proto_remunk2 = -1;
78
79
80 typedef struct remunk_remqueryinterface_call_s {
81     guint       iid_count;
82     e_uuid_t    *iids;
83 } remunk_remqueryinterface_call_t;
84
85
86 static int
87 dissect_remunk_remqueryinterface_rqst(tvbuff_t *tvb, int offset,
88         packet_info *pinfo, proto_tree *tree, guint8 *drep)
89 {
90         e_uuid_t ipid;
91         guint32 u32Refs;
92         guint16 u16IIDs;
93         guint32 u32ArraySize;
94         guint32 u32ItemIdx;
95         e_uuid_t iid;
96         dcerpc_info *info = (dcerpc_info *) pinfo->private_data;
97         remunk_remqueryinterface_call_t *call;
98
99
100         offset = dissect_dcom_this(tvb, offset, pinfo, tree, drep);
101
102         offset = dissect_dcom_UUID(tvb, offset, pinfo, tree, drep, 
103                         hf_dcom_ipid, &ipid);
104
105         offset = dissect_dcom_DWORD(tvb, offset, pinfo, tree, drep, 
106                         hf_remunk_refs, &u32Refs);
107
108         offset = dissect_dcom_WORD(tvb, offset, pinfo, tree, drep, 
109                         hf_remunk_iids, &u16IIDs);
110
111         offset = dissect_dcom_dcerpc_array_size(tvb, offset, pinfo, tree, drep, 
112                                                 &u32ArraySize);
113
114     /* limit the allocation to a reasonable size */
115     if(u32ArraySize < 100) {
116             call = se_alloc(sizeof(remunk_remqueryinterface_call_t) + u32ArraySize * sizeof(e_uuid_t));
117             call->iid_count = u32ArraySize;
118             call->iids = (e_uuid_t *) (call+1);
119         info->call_data->private_data = call;
120     } else {
121         call = NULL;
122     }
123
124         for (u32ItemIdx = 0; u32ArraySize--; u32ItemIdx++) {
125                 offset = dissect_dcom_append_UUID(tvb, offset,  pinfo, tree, drep,
126                         hf_dcom_iid, u32ItemIdx+1, &iid);
127         if(call != NULL) {
128                     call->iids[u32ItemIdx] = iid;
129         }
130         }
131
132         return offset;
133 }
134
135
136 static int
137 dissect_remunk_remqueryinterface_resp(tvbuff_t *tvb, int offset,
138         packet_info *pinfo, proto_tree *tree, guint8 *drep)
139 {
140         guint32 u32Pointer;
141         guint32 u32ArraySize;
142         guint32 u32ItemIdx;
143         proto_item *sub_item;
144         proto_tree *sub_tree;
145         guint32 u32HResult;
146         guint32 u32SubStart;
147         e_uuid_t iid;
148         e_uuid_t iid_null = DCERPC_UUID_NULL;
149         dcerpc_info *info = (dcerpc_info *) pinfo->private_data;
150         remunk_remqueryinterface_call_t *call = info->call_data->private_data;
151     guint64 oxid;
152     guint64 oid;
153     e_uuid_t ipid;
154     dcom_interface_t *dcom_if;
155
156
157         offset = dissect_dcom_that(tvb, offset, pinfo, tree, drep);
158
159         offset = dissect_dcom_dcerpc_pointer(tvb, offset, pinfo, tree, drep, 
160                                                 &u32Pointer);
161         offset = dissect_dcom_dcerpc_array_size(tvb, offset, pinfo, tree, drep, 
162                                                 &u32ArraySize);
163
164         u32ItemIdx = 1;
165         while (u32ArraySize--) {
166                 /* add subtree */
167                 sub_item = proto_tree_add_item(tree, hf_remunk_qiresult, tvb, offset, 0, FALSE);
168                 sub_tree = proto_item_add_subtree(sub_item, ett_remunk_rqi_result);
169
170                 /* REMQIRESULT */
171                 offset = dissect_dcom_HRESULT(tvb, offset, pinfo, sub_tree, drep, 
172                                                         &u32HResult);
173                 u32SubStart = offset - 4;
174                 offset = dissect_dcom_dcerpc_pointer(tvb, offset, pinfo, sub_tree, drep, 
175                                                         &u32Pointer);
176
177                 /* try to read the iid from the request */
178                 if(call != NULL && u32ItemIdx <= call->iid_count) {
179                         iid = call->iids[u32ItemIdx-1];
180                 } else {
181                         iid = iid_null;
182                 }
183
184                 /* XXX - this doesn't seem to be dependent on the pointer above?!? */
185                 /*if (u32Pointer) {*/
186                         offset = dissect_dcom_STDOBJREF(tvb, offset, pinfo, sub_tree, drep, 0 /* hfindex */, 
187                 &oxid, &oid, &ipid);
188                 /*}*/
189
190         /* add interface instance to database (we currently only handle IPv4) */
191         if(pinfo->net_src.type == AT_IPv4) {
192             dcom_if = dcom_interface_new(pinfo, 
193                 pinfo->net_src.data,
194                 &iid, oxid, oid, &ipid);
195         }
196
197                 /* update subtree */
198                 proto_item_append_text(sub_item, "[%u]: %s", 
199                         u32ItemIdx,
200                         val_to_str(u32HResult, dcom_hresult_vals, "Unknown (0x%08x)") );
201                 proto_item_set_len(sub_item, offset - u32SubStart);
202
203                 /* update column info now */
204                 if (check_col(pinfo->cinfo, COL_INFO)) {
205                   col_append_fstr(pinfo->cinfo, COL_INFO, " %s[%u]", 
206                           val_to_str(u32HResult, dcom_hresult_vals, "Unknown (0x%08x)"),
207                           u32ItemIdx);
208                 }
209
210                 u32ItemIdx++;
211         }
212
213         /* HRESULT of call */
214         offset = dissect_dcom_HRESULT(tvb, offset, pinfo, tree, drep, 
215                         &u32HResult);
216
217         /* update column info now */
218     if (check_col(pinfo->cinfo, COL_INFO)) {
219       col_append_fstr(pinfo->cinfo, COL_INFO, " -> %s", 
220                   val_to_str(u32HResult, dcom_hresult_vals, "Unknown (0x%08x)"));
221         }
222
223         return offset;
224 }
225
226
227 static int
228 dissect_remunk_remrelease_rqst(tvbuff_t *tvb, int offset,
229         packet_info *pinfo, proto_tree *tree, guint8 *drep)
230 {
231         guint32 u32Pointer;
232         guint32 u32IntRefs;
233         guint32 u32ItemIdx;
234         e_uuid_t ipid;
235         guint32 u32PublicRefs;
236         guint32 u32PrivateRefs;
237         const gchar *pszFormat;
238         proto_item *sub_item;
239         proto_tree *sub_tree;
240         guint32 u32SubStart;
241
242
243     offset = dissect_dcom_this(tvb, offset, pinfo, tree, drep);
244
245         offset = dissect_dcom_dcerpc_pointer(tvb, offset, pinfo, tree, drep, 
246                                                 &u32Pointer);
247
248         offset = dissect_dcom_DWORD(tvb, offset, pinfo, tree, drep, 
249                         hf_remunk_interface_refs, &u32IntRefs);
250
251         /* update column info now */
252     if (check_col(pinfo->cinfo, COL_INFO)) {
253                 if (u32IntRefs) {
254                         col_append_fstr(pinfo->cinfo, COL_INFO, " Cnt=%u Refs=", u32IntRefs);
255                 } else {
256                         col_append_fstr(pinfo->cinfo, COL_INFO, " Cnt=0");
257                 }
258         }
259
260         u32ItemIdx = 1;
261         while (u32IntRefs--) {
262         /* add subtree */
263                 sub_item = proto_tree_add_item(tree, hf_remunk_reminterfaceref, tvb, offset, 0, FALSE);
264                 sub_tree = proto_item_add_subtree(sub_item, ett_remunk_reminterfaceref);
265                 u32SubStart = offset;
266
267                 offset = dissect_dcom_UUID(tvb, offset, pinfo, sub_tree, drep, 
268                                                         hf_dcom_ipid, &ipid);
269
270                 offset = dissect_dcom_DWORD(tvb, offset, pinfo, sub_tree, drep, 
271                                                         hf_remunk_public_refs, &u32PublicRefs);
272
273                 offset = dissect_dcom_DWORD(tvb, offset, pinfo, sub_tree, drep, 
274                                                         hf_remunk_private_refs, &u32PrivateRefs);
275
276                 /* update subtree */
277                 proto_item_append_text(sub_item, "[%u]: IPID=%s, PublicRefs=%u, PrivateRefs=%u", 
278                         u32ItemIdx,
279                         guids_resolve_uuid_to_str(&ipid),
280                         u32PublicRefs, u32PrivateRefs);
281                 proto_item_set_len(sub_item, offset - u32SubStart);
282
283                 /* update column info now */
284                 if (check_col(pinfo->cinfo, COL_INFO)) {
285                         pszFormat = "";
286                         if (u32ItemIdx == 1) {
287                                 pszFormat = "%u-%u";
288                         } else if (u32ItemIdx < 10) {
289                                 pszFormat = ",%u-%u";
290                         } else if (u32ItemIdx == 10) {
291                                 pszFormat = ",...";
292                         }
293                         col_append_fstr(pinfo->cinfo, COL_INFO, pszFormat, u32PublicRefs, u32PrivateRefs);
294                 }
295
296                 u32ItemIdx++;
297         }
298
299         return offset;
300 }
301
302
303 /* sub dissector table of IRemUnknown interface */
304 static dcerpc_sub_dissector remunk_dissectors[] = {
305     { 0, "QueryInterface", NULL, NULL },
306     { 1, "AddRef", NULL, NULL },
307     { 2, "Release", NULL, NULL },
308
309     { 3, "RemQueryInterface", dissect_remunk_remqueryinterface_rqst, dissect_remunk_remqueryinterface_resp },
310         { 4, "RemAddRef", NULL, NULL },
311     { 5, "RemRelease", dissect_remunk_remrelease_rqst, dissect_dcom_simple_resp },
312     { 0, NULL, NULL, NULL }
313 };
314
315 /* sub dissector table of IRemUnknown2 interface */
316 static dcerpc_sub_dissector remunk2_dissectors[] = {
317     { 0, "QueryInterface", NULL, NULL },
318     { 1, "AddRef", NULL, NULL },
319     { 2, "Release", NULL, NULL },
320
321     { 3, "RemQueryInterface", dissect_remunk_remqueryinterface_rqst, dissect_remunk_remqueryinterface_resp },
322         { 4, "RemAddRef", NULL, NULL },
323     { 5, "RemRelease", dissect_remunk_remrelease_rqst, dissect_dcom_simple_resp },
324
325         { 6, "RemQueryInterface2", NULL, NULL },
326     { 0, NULL, NULL, NULL }
327 };
328
329
330
331 void
332 proto_register_remunk (void)
333 {
334         static hf_register_info hf_remunk_rqi_array[] = {
335         { &hf_remunk_opnum,
336             { "Operation", "remunk_opnum", FT_UINT16, BASE_DEC, NULL, 0x0, "Operation", HFILL }},
337
338                 { &hf_remunk_refs,
339                 { "Refs", "remunk_refs", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
340                 { &hf_remunk_iids,
341                 { "IIDs", "remunk_iids", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
342                 { &hf_remunk_qiresult,
343                 { "QIResult", "remunk_qiresult", FT_NONE, BASE_DEC, NULL, 0x0, "", HFILL }},
344                 { &hf_remunk_flags,
345                 { "Flags", "remunk_flags",  FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
346                 { &hf_remunk_public_refs,
347                 { "PublicRefs", "remunk_public_refs",  FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
348                 { &hf_remunk_reminterfaceref,
349                 { "RemInterfaceRef", "remunk_reminterfaceref",  FT_NONE, BASE_NONE, NULL, 0x0, "", HFILL }},
350                 { &hf_remunk_interface_refs,
351                 { "InterfaceRefs", "remunk_int_refs",  FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
352                 { &hf_remunk_private_refs,
353                 { "PrivateRefs", "remunk_private_refs",  FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }}
354         };
355
356         static gint *ett_remunk_array[] = {
357                 &ett_remunk,
358                 &ett_remunk_rqi_result,
359                 &ett_remunk2,
360                 &ett_remunk_reminterfaceref
361         };
362         
363         proto_remunk = proto_register_protocol ("IRemUnknown", "IRemUnknown", "remunk");
364         proto_register_field_array (proto_remunk, hf_remunk_rqi_array, array_length (hf_remunk_rqi_array));
365
366         proto_remunk2 = proto_register_protocol ("IRemUnknown2", "IRemUnknown2", "remunk2");
367
368         proto_register_subtree_array (ett_remunk_array, array_length (ett_remunk_array));
369 }
370
371 void
372 proto_reg_handoff_remunk (void)
373 {
374
375     /* Register the IPID */
376     guids_add_uuid(&ipid_remunk, "IPID-IRemUnknown");
377
378         /* Register the interfaces */
379         dcerpc_init_uuid(proto_remunk, ett_remunk, 
380                 &uuid_remunk, ver_remunk, 
381                 remunk_dissectors, hf_remunk_opnum);
382
383         dcerpc_init_uuid(proto_remunk2, ett_remunk2, 
384                 &uuid_remunk2, ver_remunk2, 
385                 remunk2_dissectors, hf_remunk_opnum);
386 }