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