48525402da0e122b78e7f36213f195ff478815ff
[metze/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
40
41 static int hf_remunk_opnum = -1;
42
43
44 static int hf_remunk_refs = -1;
45 static int hf_remunk_iids = -1;
46
47 static int hf_remunk_flags = -1;
48 static int hf_remunk_qiresult = -1;
49
50 static gint ett_remunk_reminterfaceref = -1;
51 static int hf_remunk_reminterfaceref = -1;
52 static int hf_remunk_interface_refs = -1;
53 static int hf_remunk_public_refs = -1;
54 static int hf_remunk_private_refs = -1;
55
56
57 static gint ett_remunk_rqi_result = -1;
58
59
60 static gint ett_remunk = -1;
61 static e_uuid_t uuid_remunk = { 0x00000131, 0x0000, 0x0000, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
62 static guint16  ver_remunk = 0;
63 static int proto_remunk = -1;
64
65 /* There is a little bit confusion about the IRemUnknown2 interface UUIDs */
66 /* DCOM documentation tells us: 0x00000142 (7 methods) */
67 /* win2000 registry tells us: 0x00000142 IRemoteQI (4 methods) */
68 /* win2000 registry tells us: 0x00000143 IRemUnknown2 (7 methods) */
69 /* There is some evidence, that the DCOM documentation is wrong, so using 143 for IRemUnknown2 now. */
70
71 static gint ett_remunk2 = -1;
72 static e_uuid_t uuid_remunk2 = { 0x00000143, 0x0000, 0x0000, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
73 static guint16  ver_remunk2 = 0;
74 static int proto_remunk2 = -1;
75
76
77 typedef struct remunk_remqueryinterface_call_s {
78     guint       iid_count;
79     e_uuid_t    *iids;
80 } remunk_remqueryinterface_call_t;
81
82
83 static int
84 dissect_remunk_remqueryinterface_rqst(tvbuff_t *tvb, int offset,
85         packet_info *pinfo, proto_tree *tree, guint8 *drep)
86 {
87         e_uuid_t ipid;
88         guint32 u32Refs;
89         guint16 u16IIDs;
90         guint32 u32ArraySize;
91         guint32 u32ItemIdx;
92         e_uuid_t iid;
93         dcerpc_info *info = (dcerpc_info *) pinfo->private_data;
94         remunk_remqueryinterface_call_t *call;
95
96
97         offset = dissect_dcom_this(tvb, offset, pinfo, tree, drep);
98
99         offset = dissect_dcom_UUID(tvb, offset, pinfo, tree, drep, 
100                         hf_dcom_ipid, &ipid);
101
102         offset = dissect_dcom_DWORD(tvb, offset, pinfo, tree, drep, 
103                         hf_remunk_refs, &u32Refs);
104
105         offset = dissect_dcom_WORD(tvb, offset, pinfo, tree, drep, 
106                         hf_remunk_iids, &u16IIDs);
107
108         offset = dissect_dcom_dcerpc_array_size(tvb, offset, pinfo, tree, drep, 
109                                                 &u32ArraySize);
110
111     /* limit the allocation to a reasonable size */
112     if(u32ArraySize < 100) {
113             call = se_alloc(sizeof(remunk_remqueryinterface_call_t) + u32ArraySize * sizeof(e_uuid_t));
114             call->iid_count = u32ArraySize;
115             call->iids = (e_uuid_t *) (call+1);
116         info->call_data->private_data = call;
117     } else {
118         call = NULL;
119     }
120
121         for (u32ItemIdx = 0; u32ArraySize--; u32ItemIdx++) {
122                 offset = dissect_dcom_append_UUID(tvb, offset,  pinfo, tree, drep,
123                         hf_dcom_iid, "IID", u32ItemIdx+1, &iid);
124         if(call != NULL) {
125                     call->iids[u32ItemIdx] = iid;
126         }
127         }
128
129         return offset;
130 }
131
132
133 static int
134 dissect_remunk_remqueryinterface_resp(tvbuff_t *tvb, int offset,
135         packet_info *pinfo, proto_tree *tree, guint8 *drep)
136 {
137         guint32 u32Pointer;
138         guint32 u32ArraySize;
139         guint32 u32ItemIdx;
140         proto_item *sub_item;
141         proto_tree *sub_tree;
142         guint32 u32HResult;
143         guint32 u32SubStart;
144         e_uuid_t iid;
145         e_uuid_t iid_null = DCERPC_UUID_NULL;
146         dcerpc_info *info = (dcerpc_info *) pinfo->private_data;
147         remunk_remqueryinterface_call_t *call = info->call_data->private_data;
148     guint64 oxid;
149     guint64 oid;
150     e_uuid_t ipid;
151     dcom_interface_t *dcom_if;
152
153
154         offset = dissect_dcom_that(tvb, offset, pinfo, tree, drep);
155
156         offset = dissect_dcom_dcerpc_pointer(tvb, offset, pinfo, tree, drep, 
157                                                 &u32Pointer);
158         offset = dissect_dcom_dcerpc_array_size(tvb, offset, pinfo, tree, drep, 
159                                                 &u32ArraySize);
160
161         u32ItemIdx = 1;
162         while (u32ArraySize--) {
163                 /* add subtree */
164                 sub_item = proto_tree_add_item(tree, hf_remunk_qiresult, tvb, offset, 0, FALSE);
165                 sub_tree = proto_item_add_subtree(sub_item, ett_remunk_rqi_result);
166
167                 /* REMQIRESULT */
168                 offset = dissect_dcom_HRESULT(tvb, offset, pinfo, sub_tree, drep, 
169                                                         &u32HResult);
170                 u32SubStart = offset - 4;
171                 offset = dissect_dcom_dcerpc_pointer(tvb, offset, pinfo, sub_tree, drep, 
172                                                         &u32Pointer);
173
174                 /* try to read the iid from the request */
175                 if(call != NULL && u32ItemIdx <= call->iid_count) {
176                         iid = call->iids[u32ItemIdx-1];
177                 } else {
178                         iid = iid_null;
179                 }
180
181                 /* XXX - this doesn't seem to be dependent on the pointer above?!? */
182                 /*if (u32Pointer) {*/
183                         offset = dissect_dcom_STDOBJREF(tvb, offset, pinfo, sub_tree, drep, 0 /* hfindex */, 
184                 &oxid, &oid, &ipid);
185                 /*}*/
186
187         /* add interface instance to database (we currently only handle IPv4) */
188         if(pinfo->net_src.type == AT_IPv4) {
189             dcom_if = dcom_interface_new(pinfo, 
190                 pinfo->net_src.data,
191                 &iid, oxid, oid, &ipid);
192         }
193
194                 /* update subtree */
195                 proto_item_append_text(sub_item, "[%u]: %s", 
196                         u32ItemIdx,
197                         val_to_str(u32HResult, dcom_hresult_vals, "Unknown (0x%08x)") );
198                 proto_item_set_len(sub_item, offset - u32SubStart);
199
200                 /* update column info now */
201                 if (check_col(pinfo->cinfo, COL_INFO)) {
202                   col_append_fstr(pinfo->cinfo, COL_INFO, " %s[%u]", 
203                           val_to_str(u32HResult, dcom_hresult_vals, "Unknown (0x%08x)"),
204                           u32ItemIdx);
205                 }
206
207                 u32ItemIdx++;
208         }
209
210         /* HRESULT of call */
211         offset = dissect_dcom_HRESULT(tvb, offset, pinfo, tree, drep, 
212                         &u32HResult);
213
214         /* update column info now */
215     if (check_col(pinfo->cinfo, COL_INFO)) {
216       col_append_fstr(pinfo->cinfo, COL_INFO, " -> %s", 
217                   val_to_str(u32HResult, dcom_hresult_vals, "Unknown (0x%08x)"));
218         }
219
220         return offset;
221 }
222
223
224 static int
225 dissect_remunk_remrelease_rqst(tvbuff_t *tvb, int offset,
226         packet_info *pinfo, proto_tree *tree, guint8 *drep)
227 {
228         guint32 u32Pointer;
229         guint32 u32IntRefs;
230         guint32 u32ItemIdx;
231         e_uuid_t ipid;
232         guint32 u32PublicRefs;
233         guint32 u32PrivateRefs;
234         const gchar *pszFormat;
235         proto_item *sub_item;
236         proto_tree *sub_tree;
237         guint32 u32SubStart;
238
239
240     offset = dissect_dcom_this(tvb, offset, pinfo, tree, drep);
241
242         offset = dissect_dcom_dcerpc_pointer(tvb, offset, pinfo, tree, drep, 
243                                                 &u32Pointer);
244
245         offset = dissect_dcom_DWORD(tvb, offset, pinfo, tree, drep, 
246                         hf_remunk_interface_refs, &u32IntRefs);
247
248         /* update column info now */
249     if (check_col(pinfo->cinfo, COL_INFO)) {
250                 if (u32IntRefs) {
251                         col_append_fstr(pinfo->cinfo, COL_INFO, " Cnt=%u Refs=", u32IntRefs);
252                 } else {
253                         col_append_fstr(pinfo->cinfo, COL_INFO, " Cnt=0");
254                 }
255         }
256
257         u32ItemIdx = 1;
258         while (u32IntRefs--) {
259         /* add subtree */
260                 sub_item = proto_tree_add_item(tree, hf_remunk_reminterfaceref, tvb, offset, 0, FALSE);
261                 sub_tree = proto_item_add_subtree(sub_item, ett_remunk_reminterfaceref);
262                 u32SubStart = offset;
263
264                 offset = dissect_dcom_UUID(tvb, offset, pinfo, sub_tree, drep, 
265                                                         hf_dcom_ipid, &ipid);
266
267                 offset = dissect_dcom_DWORD(tvb, offset, pinfo, sub_tree, drep, 
268                                                         hf_remunk_public_refs, &u32PublicRefs);
269
270                 offset = dissect_dcom_DWORD(tvb, offset, pinfo, sub_tree, drep, 
271                                                         hf_remunk_private_refs, &u32PrivateRefs);
272
273                 /* update subtree */
274                 proto_item_append_text(sub_item, "[%u]: IPID=%s, PublicRefs=%u, PrivateRefs=%u", 
275                         u32ItemIdx,
276                         dcom_uuid_to_str(&ipid),
277                         u32PublicRefs, u32PrivateRefs);
278                 proto_item_set_len(sub_item, offset - u32SubStart);
279
280                 /* update column info now */
281                 if (check_col(pinfo->cinfo, COL_INFO)) {
282                         pszFormat = "";
283                         if (u32ItemIdx == 1) {
284                                 pszFormat = "%u-%u";
285                         } else if (u32ItemIdx < 10) {
286                                 pszFormat = ",%u-%u";
287                         } else if (u32ItemIdx == 10) {
288                                 pszFormat = ",...";
289                         }
290                         col_append_fstr(pinfo->cinfo, COL_INFO, pszFormat, u32PublicRefs, u32PrivateRefs);
291                 }
292
293                 u32ItemIdx++;
294         }
295
296         return offset;
297 }
298
299
300 /* sub dissector table of IRemUnknown interface */
301 static dcerpc_sub_dissector remunk_dissectors[] = {
302     { 0, "QueryInterface", NULL, NULL },
303     { 1, "AddRef", NULL, NULL },
304     { 2, "Release", NULL, NULL },
305
306     { 3, "RemQueryInterface", dissect_remunk_remqueryinterface_rqst, dissect_remunk_remqueryinterface_resp },
307         { 4, "RemAddRef", NULL, NULL },
308     { 5, "RemRelease", dissect_remunk_remrelease_rqst, dissect_dcom_simple_resp },
309     { 0, NULL, NULL, NULL }
310 };
311
312 /* sub dissector table of IRemUnknown2 interface */
313 static dcerpc_sub_dissector remunk2_dissectors[] = {
314     { 0, "QueryInterface", NULL, NULL },
315     { 1, "AddRef", NULL, NULL },
316     { 2, "Release", NULL, NULL },
317
318     { 3, "RemQueryInterface", dissect_remunk_remqueryinterface_rqst, dissect_remunk_remqueryinterface_resp },
319         { 4, "RemAddRef", NULL, NULL },
320     { 5, "RemRelease", dissect_remunk_remrelease_rqst, dissect_dcom_simple_resp },
321
322         { 6, "RemQueryInterface2", NULL, NULL },
323     { 0, NULL, NULL, NULL }
324 };
325
326
327
328 void
329 proto_register_remunk (void)
330 {
331         static hf_register_info hf_remunk_rqi_array[] = {
332         { &hf_remunk_opnum,
333             { "Operation", "remunk_opnum", FT_UINT16, BASE_DEC, NULL, 0x0, "Operation", HFILL }},
334
335                 { &hf_remunk_refs,
336                 { "Refs", "remunk_refs", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
337                 { &hf_remunk_iids,
338                 { "IIDs", "remunk_iids", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }},
339                 { &hf_remunk_qiresult,
340                 { "QIResult", "remunk_qiresult", FT_NONE, BASE_DEC, NULL, 0x0, "", HFILL }},
341                 { &hf_remunk_flags,
342                 { "Flags", "remunk_flags",  FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
343                 { &hf_remunk_public_refs,
344                 { "PublicRefs", "remunk_public_refs",  FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
345                 { &hf_remunk_reminterfaceref,
346                 { "RemInterfaceRef", "remunk_reminterfaceref",  FT_NONE, BASE_NONE, NULL, 0x0, "", HFILL }},
347                 { &hf_remunk_interface_refs,
348                 { "InterfaceRefs", "remunk_int_refs",  FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
349                 { &hf_remunk_private_refs,
350                 { "PrivateRefs", "remunk_private_refs",  FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }}
351         };
352
353         static gint *ett_remunk_array[] = {
354                 &ett_remunk,
355                 &ett_remunk_rqi_result,
356                 &ett_remunk2,
357                 &ett_remunk_reminterfaceref
358         };
359         
360         proto_remunk = proto_register_protocol ("IRemUnknown", "IRemUnknown", "remunk");
361         proto_register_field_array (proto_remunk, hf_remunk_rqi_array, array_length (hf_remunk_rqi_array));
362
363         proto_remunk2 = proto_register_protocol ("IRemUnknown2", "IRemUnknown2", "remunk2");
364
365         proto_register_subtree_array (ett_remunk_array, array_length (ett_remunk_array));
366 }
367
368 void
369 proto_reg_handoff_remunk (void)
370 {
371         /* Register the interfaces */
372         dcerpc_init_uuid(proto_remunk, ett_remunk, 
373                 &uuid_remunk, ver_remunk, 
374                 remunk_dissectors, hf_remunk_opnum);
375
376         dcerpc_init_uuid(proto_remunk2, ett_remunk2, 
377                 &uuid_remunk2, ver_remunk2, 
378                 remunk2_dissectors, hf_remunk_opnum);
379 }