Cleanup header file define guards.
[obnox/wireshark/wip.git] / gtk / follow_ssl.c
1 /* follow_ssl.c
2  * SSL specific routines for following traffic streams
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
23  * USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29 #include <stdio.h>
30 #include <string.h>
31
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include <ctype.h>
38
39 #include <gtk/gtk.h>
40
41 #include <epan/follow.h>
42 #include <epan/dissectors/packet-ipv6.h>
43 #include <epan/prefs.h>
44 #include <epan/addr_resolv.h>
45 #include <epan/epan_dissect.h>
46 #include <epan/filesystem.h>
47 #include <epan/tap.h>
48
49 #include <../color.h>
50 #include <../globals.h>
51 #include <../alert_box.h>
52 #include <../simple_dialog.h>
53 #include <../util.h>
54
55 #include <gtk/color_utils.h>
56 #include <gtk/main.h>
57 #include <gtk/dlg_utils.h>
58 #include <gtk/file_dlg.h>
59 #include <gtk/keys.h>
60 #include <gtk/gui_utils.h>
61 #include <gtk/font_utils.h>
62 #include "gtk/follow_ssl.h"
63 #include "gtk/follow_stream.h"
64
65 #ifdef SSL_PLUGIN
66 #include "packet-ssl-utils.h"
67 #else
68 #include <epan/dissectors/packet-ssl-utils.h>
69 #endif
70
71
72 typedef struct {
73     gboolean is_server;
74     StringInfo data;
75 } SslDecryptedRecord;
76
77 static int
78 ssl_queue_packet_data(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *ssl)
79 {
80     follow_info_t* follow_info = tapdata;
81     SslDecryptedRecord* rec;
82     SslDataInfo* appl_data;
83     gint total_len;
84     guchar *p;
85     int proto_ssl = (long) ssl;
86     SslPacketInfo* pi = p_get_proto_data(pinfo->fd, proto_ssl);
87
88     /* skip packet without decrypted data payload*/
89     if (!pi || !pi->appl_data)
90         return 0;
91
92     /* compute total length */
93     total_len = 0;
94     appl_data = pi->appl_data;
95     do {
96       total_len += appl_data->plain_data.data_len;
97       appl_data = appl_data->next;
98     } while (appl_data);
99
100     /* compute packet direction */
101     rec = g_malloc(sizeof(SslDecryptedRecord) + total_len);
102
103     if (follow_info->client_port == 0) {
104         follow_info->client_port = pinfo->srcport;
105         COPY_ADDRESS(&follow_info->client_ip, &pinfo->src);
106     }
107     if (ADDRESSES_EQUAL(&follow_info->client_ip, &pinfo->src) &&
108         follow_info->client_port == pinfo->srcport)
109         rec->is_server = 0;
110     else
111         rec->is_server = 1;
112
113     /* update stream counter */
114     follow_info->bytes_written[rec->is_server] += total_len;
115
116     /* extract decrypted data and queue it locally */
117     rec->data.data = (guchar*)(rec + 1);
118     rec->data.data_len = total_len;
119     appl_data = pi->appl_data;
120     p = rec->data.data;
121     do {
122       memcpy(p, appl_data->plain_data.data, appl_data->plain_data.data_len);
123       p += appl_data->plain_data.data_len;
124       appl_data = appl_data->next;
125     } while (appl_data);
126     follow_info->payload = g_list_append(
127         follow_info->payload,rec);
128
129     return 0;
130 }
131
132 extern gboolean
133 packet_is_ssl(epan_dissect_t* edt);
134
135
136 /* Follow the SSL stream, if any, to which the last packet that we called
137    a dissection routine on belongs (this might be the most recently
138    selected packet, or it might be the last packet in the file). */
139 void
140 follow_ssl_stream_cb(GtkWidget * w, gpointer data _U_)
141 {
142     GtkWidget   *filter_te;
143     gchar       *follow_filter;
144     const gchar *previous_filter;
145     int         filter_out_filter_len, previous_filter_len;
146     const char  *hostname0, *hostname1;
147     char        *port0, *port1;
148     gchar       *server_to_client_string = NULL;
149     gchar       *client_to_server_string = NULL;
150     gchar       *both_directions_string = NULL;
151     follow_stats_t stats;
152     follow_info_t *follow_info;
153     GString*    msg;
154
155     /* we got ssl so we can follow */
156     if (!packet_is_ssl(cfile.edt)) {
157         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
158                       "Error following stream.  Please make\n"
159                       "sure you have an SSL packet selected.");
160         return;
161     }
162
163     follow_info = g_new0(follow_info_t, 1);
164     follow_info->follow_type = FOLLOW_SSL;
165
166     /* Create a new filter that matches all packets in the SSL stream,
167        and set the display filter entry accordingly */
168     reset_tcp_reassembly();
169     follow_filter = build_follow_filter(&cfile.edt->pi);
170     if (!follow_filter)
171     {
172         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
173                       "Error creating filter for this stream.\n"
174                       "A network layer header is needed");
175         g_free(follow_info);
176         return;
177     }
178
179     /* Set the display filter entry accordingly */
180     filter_te = g_object_get_data(G_OBJECT(w), E_DFILTER_TE_KEY);
181
182     /* needed in follow_filter_out_stream(), is there a better way? */
183     follow_info->filter_te = filter_te;
184
185     /* save previous filter, const since we're not supposed to alter */
186     previous_filter =
187         (const gchar *)gtk_entry_get_text(GTK_ENTRY(filter_te));
188
189     /* allocate our new filter. API claims g_malloc terminates program on failure */
190     /* my calc for max alloc needed is really +10 but when did a few extra bytes hurt ? */
191     previous_filter_len = previous_filter?(int)strlen(previous_filter):0;
192     filter_out_filter_len = (int)strlen(follow_filter) + previous_filter_len + 16;
193     follow_info->filter_out_filter = (gchar *)g_malloc(filter_out_filter_len);
194
195     /* append the negation */
196     if(previous_filter_len) {
197         g_snprintf(follow_info->filter_out_filter, filter_out_filter_len,
198         "%s and !(%s)", previous_filter, follow_filter);
199     } else {
200         g_snprintf(follow_info->filter_out_filter, filter_out_filter_len,
201         "!(%s)", follow_filter);
202     }
203
204     /* data will be passed via tap callback*/
205     msg = register_tap_listener("ssl", follow_info, follow_filter, 0,
206         NULL, ssl_queue_packet_data, NULL);
207     if (msg)
208     {
209         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
210             "Can't register ssl tap: %s\n",msg->str);
211         g_free(follow_info->filter_out_filter);
212         g_free(follow_info);
213         g_free(follow_filter);
214         return;
215     }
216     gtk_entry_set_text(GTK_ENTRY(filter_te), follow_filter);
217
218     /* Run the display filter so it goes in effect - even if it's the
219        same as the previous display filter. */
220     main_filter_packets(&cfile, follow_filter, TRUE);
221
222     /* Free the filter string, as we're done with it. */
223     g_free(follow_filter);
224
225     remove_tap_listener(follow_info);
226
227     /* Stream to show */
228     follow_stats(&stats);
229
230     if (stats.is_ipv6) {
231             struct e_in6_addr ipaddr;
232             memcpy(&ipaddr, stats.ip_address[0], 16);
233             hostname0 = get_hostname6(&ipaddr);
234             memcpy(&ipaddr, stats.ip_address[0], 16);
235             hostname1 = get_hostname6(&ipaddr);
236     } else {
237             guint32 ipaddr;
238             memcpy(&ipaddr, stats.ip_address[0], 4);
239             hostname0 = get_hostname(ipaddr);
240             memcpy(&ipaddr, stats.ip_address[1], 4);
241             hostname1 = get_hostname(ipaddr);
242     }
243
244     port0 = get_tcp_port(stats.port[0]);
245     port1 = get_tcp_port(stats.port[1]);
246
247     follow_info->is_ipv6 = stats.is_ipv6;
248
249    /* Both Stream Directions */
250     both_directions_string = g_strdup_printf("Entire conversation (%u bytes)", follow_info->bytes_written[0] + follow_info->bytes_written[1]);
251
252     if(follow_info->client_port == stats.port[0]) {
253             server_to_client_string =
254                     g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
255                                     hostname0, port0,
256                                     hostname1, port1,
257                                     follow_info->bytes_written[0]);
258
259             client_to_server_string =
260                     g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
261                                     hostname1, port1,
262                                     hostname0, port0,
263                                     follow_info->bytes_written[1]);
264     } else {
265             server_to_client_string =
266                     g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
267                                     hostname1, port1,
268                                     hostname0, port0,
269                                     follow_info->bytes_written[0]);
270
271             client_to_server_string =
272                     g_strdup_printf("%s:%s --> %s:%s (%u bytes)",
273                                     hostname0, port0,
274                                     hostname1, port1,
275                                     follow_info->bytes_written[1]);
276     }
277
278     follow_stream("Follow SSL Stream", follow_info, both_directions_string,
279                   server_to_client_string, client_to_server_string);
280
281     g_free(both_directions_string);
282     g_free(server_to_client_string);
283     g_free(client_to_server_string);
284 }
285
286 #define FLT_BUF_SIZE 1024
287
288 /*
289  * XXX - the routine pointed to by "print_line_fcn_p" doesn't get handed lines,
290  * it gets handed bufferfuls.  That's fine for "follow_write_raw()"
291  * and "follow_add_to_gtk_text()", but, as "follow_print_text()" calls
292  * the "print_line()" routine from "print.c", and as that routine might
293  * genuinely expect to be handed a line (if, for example, it's using
294  * some OS or desktop environment's printing API, and that API expects
295  * to be handed lines), "follow_print_text()" should probably accumulate
296  * lines in a buffer and hand them "print_line()".  (If there's a
297  * complete line in a buffer - i.e., there's nothing of the line in
298  * the previous buffer or the next buffer - it can just hand that to
299  * "print_line()" after filtering out non-printables, as an
300  * optimization.)
301  *
302  * This might or might not be the reason why C arrays display
303  * correctly but get extra blank lines very other line when printed.
304  */
305 frs_return_t
306 follow_read_ssl_stream(follow_info_t *follow_info,
307                        gboolean (*print_line_fcn_p)(char *, size_t, gboolean, void *),
308                        void *arg)
309 {
310     guint32             global_client_pos = 0, global_server_pos = 0;
311     guint32             server_packet_count = 0;
312     guint32             client_packet_count = 0;
313     guint32             *global_pos;
314     gboolean            skip;
315     GList* cur;
316     frs_return_t        frs_return;
317
318     for (cur = follow_info->payload; cur; cur = g_list_next(cur)) {
319         SslDecryptedRecord* rec = cur->data;
320         skip = FALSE;
321         if (!rec->is_server) {
322             global_pos = &global_client_pos;
323             if (follow_info->show_stream == FROM_SERVER) {
324                 skip = TRUE;
325             }
326         } else {
327             global_pos = &global_server_pos;
328             if (follow_info->show_stream == FROM_CLIENT) {
329                 skip = TRUE;
330             }
331         }
332
333         if (!skip) {
334             size_t nchars = rec->data.data_len;
335             gchar *buffer = g_memdup(rec->data.data, (guint) nchars);
336
337             frs_return = follow_show(follow_info, print_line_fcn_p, buffer, nchars,
338                                      rec->is_server, arg, global_pos,
339                                      &server_packet_count, &client_packet_count);
340             g_free(buffer);
341             if(frs_return == FRS_PRINT_ERROR)
342                     return frs_return;
343         }
344     }
345
346     return FRS_OK;
347 }