Move the common parts of iface_lists.[ch] from ui/gtk/ to ui/. Leave the
[metze/wireshark/wip.git] / ui / gtk / decode_as_dcerpc.c
1 /* decode_as_dcerpc.c
2  *
3  * $Id$
4  *
5  * Routines to modify dcerpc bindings on the fly.
6  *
7  * Copyright 2004 Ulf Lamping
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30
31 #include <epan/packet.h>
32 #include <epan/epan_dissect.h>
33 #include <epan/dissectors/packet-dcerpc.h>
34
35 #include "ui/simple_dialog.h"
36
37 #include "ui/gtk/decode_as_dlg.h"
38 #include "ui/gtk/dlg_utils.h"
39 #include "ui/gtk/gui_utils.h"
40 #include "ui/gtk/decode_as_dcerpc.h"
41
42
43 /**************************************************/
44 /*                Typedefs & Enums                */
45 /**************************************************/
46
47 /* list of dcerpc "Decode As" bindings */
48 GSList *decode_dcerpc_bindings = NULL;
49
50 /**************************************************/
51 /*            Global Functions                    */
52 /**************************************************/
53
54 /* inject one of our bindings into the dcerpc binding table */
55 static void
56 decode_dcerpc_inject_binding(gpointer data, gpointer user_data _U_)
57 {
58     dcerpc_add_conv_to_bind_table((decode_dcerpc_bind_values_t *) data);
59 }
60
61
62 /* inject all of our bindings into the dcerpc binding table */
63 static void
64 decode_dcerpc_inject_bindings(gpointer data _U_) {
65     g_slist_foreach(decode_dcerpc_bindings, decode_dcerpc_inject_binding, NULL /* user_data */);
66 }
67
68
69 /* init this file */
70 void
71 decode_dcerpc_init(void) {
72     GHook*      hook_init_proto;
73
74
75     /* add a hook function to the dcerpc init_protocols hook */
76     hook_init_proto = g_hook_alloc(&dcerpc_hooks_init_protos);
77     hook_init_proto->func = decode_dcerpc_inject_bindings;
78     g_hook_prepend(&dcerpc_hooks_init_protos, hook_init_proto);
79 }
80
81
82 /* clone a binding (uses g_malloc) */
83 static decode_dcerpc_bind_values_t *
84 decode_dcerpc_binding_clone(decode_dcerpc_bind_values_t *binding_in)
85 {
86     decode_dcerpc_bind_values_t *stored_binding;
87
88     stored_binding = g_malloc(sizeof(decode_dcerpc_bind_values_t));
89     *stored_binding = *binding_in;
90     COPY_ADDRESS(&stored_binding->addr_a, &binding_in->addr_a);
91     COPY_ADDRESS(&stored_binding->addr_b, &binding_in->addr_b);
92     stored_binding->ifname = g_string_new(binding_in->ifname->str);
93
94     return stored_binding;
95 }
96
97
98 /* free a binding */
99 void
100 decode_dcerpc_binding_free(void *binding_in)
101 {
102     decode_dcerpc_bind_values_t *binding = binding_in;
103
104     g_free((void *) binding->addr_a.data);
105     g_free((void *) binding->addr_b.data);
106     if(binding->ifname)
107         g_string_free(binding->ifname, TRUE);
108     g_free(binding);
109 }
110
111
112 /* compare two bindings (except the interface related things, e.g. uuid) */
113 static gint
114 decode_dcerpc_binding_cmp(gconstpointer a, gconstpointer b)
115 {
116     const decode_dcerpc_bind_values_t *binding_a = a;
117     const decode_dcerpc_bind_values_t *binding_b = b;
118
119
120     /* don't compare uuid and ver! */
121     if(
122         ADDRESSES_EQUAL(&binding_a->addr_a, &binding_b->addr_a) &&
123         ADDRESSES_EQUAL(&binding_a->addr_b, &binding_b->addr_b) &&
124         binding_a->ptype == binding_b->ptype &&
125         binding_a->port_a == binding_b->port_a &&
126         binding_a->port_b == binding_b->port_b &&
127         binding_a->ctx_id == binding_b->ctx_id &&
128         binding_a->smb_fid == binding_b->smb_fid)
129     {
130         /* equal */
131         return 0;
132     }
133
134     /* unequal */
135     return 1;
136 }
137
138
139 /**************************************************/
140 /*             Show Changed Bindings              */
141 /**************************************************/
142
143
144 /* add a single binding to the Show list */
145 static void
146 decode_dcerpc_add_show_list_single(gpointer data, gpointer user_data)
147 {
148     gchar      string1[20];
149
150
151     decode_dcerpc_bind_values_t *binding = data;
152
153     g_snprintf(string1, sizeof(string1), "ctx_id: %u", binding->ctx_id);
154
155     decode_add_to_show_list (
156         user_data,
157         "DCE-RPC",
158         string1,
159         "-",
160         binding->ifname->str);
161 }
162
163
164 /* add all bindings to the Show list */
165 void
166 decode_dcerpc_add_show_list(gpointer user_data)
167 {
168     g_slist_foreach(decode_dcerpc_bindings, decode_dcerpc_add_show_list_single, user_data);
169 }
170
171
172 /**************************************************/
173 /*         Modify the binding routines            */
174 /**************************************************/
175
176
177 /* removes all bindings */
178 void
179 decode_dcerpc_reset_all(void)
180 {
181     decode_dcerpc_bind_values_t *binding;
182
183     while(decode_dcerpc_bindings) {
184         binding = decode_dcerpc_bindings->data;
185
186         decode_dcerpc_binding_free(binding);
187         decode_dcerpc_bindings = g_slist_remove(
188             decode_dcerpc_bindings,
189             decode_dcerpc_bindings->data);
190     }
191 }
192
193
194 /* remove a binding (looking the same way as the given one) */
195 static void
196 decode_dcerpc_binding_reset(
197 const gchar *table_name _U_,
198 decode_dcerpc_bind_values_t *binding)
199 {
200     GSList *le;
201     decode_dcerpc_bind_values_t *old_binding;
202
203
204     /* find the old binding (if it exists) */
205     le = g_slist_find_custom(decode_dcerpc_bindings,
206                                              binding,
207                                              decode_dcerpc_binding_cmp);
208     if(le == NULL)
209         return;
210
211     old_binding = le->data;
212
213     decode_dcerpc_bindings = g_slist_remove(decode_dcerpc_bindings, le->data);
214
215     g_free((void *) old_binding->addr_a.data);
216     g_free((void *) old_binding->addr_b.data);
217     g_string_free(old_binding->ifname, TRUE);
218     g_free(old_binding);
219 }
220
221
222 /* a binding has changed (remove a previously existing one) */
223 static void
224 decode_dcerpc_binding_change(
225 const gchar *table_name,
226 decode_dcerpc_bind_values_t *binding)
227 {
228
229     decode_dcerpc_bind_values_t *stored_binding;
230
231     /* remove a probably existing old binding */
232     decode_dcerpc_binding_reset(table_name, binding);
233
234     /* clone the new binding and append it to the list */
235     stored_binding = decode_dcerpc_binding_clone(binding);
236     decode_dcerpc_bindings = g_slist_append (decode_dcerpc_bindings, stored_binding);
237 }
238
239
240 /* a binding has changed (add/replace/remove it) */
241 static void
242 decode_change_one_dcerpc_binding(const gchar *table_name, decode_dcerpc_bind_values_t *binding, GtkWidget *list)
243 {
244     dcerpc_uuid_key     *key;
245     gchar              *abbrev;
246     GtkTreeSelection  *selection;
247     GtkTreeModel      *model;
248     GtkTreeIter        iter;
249
250     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
251     if (gtk_tree_selection_get_selected(selection, &model, &iter) == FALSE)
252     {
253         abbrev = NULL;
254         key = NULL;
255     } else {
256         gtk_tree_model_get(model, &iter, E_LIST_S_PROTO_NAME, &abbrev,
257                            E_LIST_S_TABLE+1, &key, -1);
258     }
259
260     if (key == NULL || (abbrev != NULL && strcmp(abbrev, "(default)") == 0) ) {
261         decode_dcerpc_binding_reset(table_name, binding);
262     } else {
263                         binding->ifname = g_string_new(abbrev);
264                         binding->uuid = key->uuid;
265                         binding->ver = key->ver;
266                         decode_dcerpc_binding_change(table_name, binding);
267     }
268     g_free(abbrev);
269 }
270
271
272
273 /**************************************************/
274 /* Action routines for the "Decode As..." dialog  */
275 /*   - called when the OK button pressed          */
276 /**************************************************/
277
278 /*
279  * This routine is called when the user clicks the "OK" button in the
280  * "Decode As..." dialog window and the DCE-RPC page is foremost.
281  * This routine takes care of making any changes requested to the DCE-RPC
282  * binding tables.
283  *
284  * @param notebook_pg A pointer to the "DCE-RPC" notebook page.
285  */
286 static void
287 decode_dcerpc(GtkWidget *notebook_pg)
288 {
289     GtkWidget *list;
290     const gchar *table_name;
291     decode_dcerpc_bind_values_t *binding;
292
293
294     list = g_object_get_data(G_OBJECT(notebook_pg), E_PAGE_LIST);
295     if (requested_action == E_DECODE_NO)
296         gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(list)));
297
298     binding = g_object_get_data(G_OBJECT(notebook_pg), E_PAGE_BINDING);
299
300     /*table_name = g_object_get_data(G_OBJECT(notebook_pg), E_PAGE_TABLE);*/
301     table_name = "DCE-RPC";
302     decode_change_one_dcerpc_binding(table_name, binding, list);
303 }
304
305
306 /**************************************************/
307 /*                  Dialog setup                  */
308 /**************************************************/
309
310
311 /* add an interface to the list */
312 static void
313 decode_dcerpc_add_to_list(gpointer key, gpointer value, gpointer user_data)
314 {
315     /*dcerpc_uuid_key *k = key;*/
316     dcerpc_uuid_value *v = value;
317
318     if(strcmp(v->name, "(none)"))
319         decode_add_to_list("DCE-RPC", v->name, key, user_data);
320 }
321
322
323 /* add all interfaces to the list */
324 static GtkWidget *
325 decode_add_dcerpc_menu (GtkWidget *page, const gchar *table_name _U_)
326 {
327     GtkWidget *scrolled_window;
328     GtkWidget *list;
329
330     decode_list_menu_start(page, &list, &scrolled_window);
331     g_hash_table_foreach(dcerpc_uuids, decode_dcerpc_add_to_list, list);
332     decode_list_menu_finish(list);
333     return(scrolled_window);
334 }
335
336
337 /* add a DCE-RPC page to the notebook */
338 GtkWidget *
339 decode_dcerpc_add_page (packet_info *pinfo)
340 {
341     GtkWidget   *page_hb, *info_vb, *label, *scrolled_window;
342     GString     *gs = g_string_new("");
343     GString     *gs2 = g_string_new("");
344     decode_dcerpc_bind_values_t *binding;
345
346
347     /* clone binding */
348     binding = g_malloc(sizeof(decode_dcerpc_bind_values_t));
349     COPY_ADDRESS(&binding->addr_a, &pinfo->src);
350     COPY_ADDRESS(&binding->addr_b, &pinfo->dst);
351     binding->ptype = pinfo->ptype;
352     binding->port_a = pinfo->srcport;
353     binding->port_b = pinfo->destport;
354     binding->ctx_id = pinfo->dcectxid;
355     binding->smb_fid = dcerpc_get_transport_salt(pinfo);
356     binding->ifname = NULL;
357     /*binding->uuid = NULL;*/
358     binding->ver = 0;
359
360     /* create page content */
361     page_hb = ws_gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5, FALSE);
362     g_object_set_data(G_OBJECT(page_hb), E_PAGE_ACTION, decode_dcerpc);
363     g_object_set_data(G_OBJECT(page_hb), E_PAGE_TABLE, "DCE-RPC");
364     g_object_set_data(G_OBJECT(page_hb), E_PAGE_TITLE, "DCE-RPC");
365     g_object_set_data(G_OBJECT(page_hb), E_PAGE_BINDING, binding);
366
367     info_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 5, FALSE);
368     gtk_box_pack_start(GTK_BOX(page_hb), info_vb, TRUE, TRUE, 0);
369
370     /* Always enabled */
371     label = gtk_label_new("Replace binding between:");
372     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
373
374     switch(binding->ptype) {
375     case(PT_TCP):
376         g_string_printf(gs2, "TCP port");
377         break;
378     case(PT_UDP):
379         g_string_printf(gs2, "UDP port");
380         break;
381     default:
382         g_string_printf(gs2, "Unknown port type");
383     }
384
385     /* XXX - how to print the address binding->addr_a? */
386     g_string_printf(gs, "Address: ToBeDone %s: %u", gs2->str, binding->port_a);
387     label = gtk_label_new(gs->str);
388     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
389
390     label = gtk_label_new("&");
391     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
392
393     /* XXX - how to print the address binding->addr_b? */
394     g_string_printf(gs, "Address: ToBeDone %s: %u", gs2->str, binding->port_b);
395     label = gtk_label_new(gs->str);
396     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
397
398     label = gtk_label_new("&");
399     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
400
401     g_string_printf(gs, "Context ID: %u", binding->ctx_id);
402     label = gtk_label_new(gs->str);
403     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
404
405     label = gtk_label_new("&");
406     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
407     gtk_widget_set_sensitive(label, binding->smb_fid);
408
409     g_string_printf(gs, "SMB FID: %u", binding->smb_fid);
410     label = gtk_label_new(gs->str);
411     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
412     gtk_widget_set_sensitive(label, binding->smb_fid);
413
414     /* Conditionally enabled - only when decoding packets */
415     label = gtk_label_new("with:");
416     gtk_box_pack_start(GTK_BOX(info_vb), label, TRUE, TRUE, 0);
417
418     decode_dimmable = g_slist_prepend(decode_dimmable, label);
419     scrolled_window = decode_add_dcerpc_menu(page_hb, "dcerpc" /*table_name*/);
420     gtk_box_pack_start(GTK_BOX(page_hb), scrolled_window, TRUE, TRUE, 0);
421     decode_dimmable = g_slist_prepend(decode_dimmable, scrolled_window);
422
423     g_string_free(gs, TRUE);
424
425     return(page_hb);
426 }