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