Clean up includes of unistd.h, fcntl.h, and sys/stat.h.
[metze/wireshark/wip.git] / ui / gtk / export_sslkeys.c
1 /* export_sslkeys.c
2  *
3  * Export SSL Session Keys dialog
4  * by Sake Blok <sake@euronet.nl> (20110526)
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 USA.
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include <wsutil/file_util.h>
30 #include <wsutil/str_util.h>
31
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34 #if GTK_CHECK_VERSION(3,0,0)
35 # include <gdk/gdkkeysyms-compat.h>
36 #endif
37
38 #include <wsutil/filesystem.h>
39 #include <epan/packet.h>
40 #include <epan/epan_dissect.h>
41 #include <epan/charsets.h>
42 #include <epan/prefs.h>
43
44 #include "ui/alert_box.h"
45 #include "ui/last_open_dir.h"
46 #include "ui/progress_dlg.h"
47 #include "ui/recent.h"
48 #include "ui/simple_dialog.h"
49 #include "ui/ssl_key_export.h"
50 #include "ui/ui_util.h"
51
52 #include "ui/gtk/keys.h"
53 #include "ui/gtk/color_utils.h"
54 #include "ui/gtk/packet_win.h"
55 #include "ui/gtk/file_dlg.h"
56 #include "ui/gtk/gui_utils.h"
57 #include "ui/gtk/gtkglobals.h"
58 #include "ui/gtk/font_utils.h"
59 #include "ui/gtk/webbrowser.h"
60 #include "ui/gtk/main.h"
61 #include "ui/gtk/export_sslkeys.h"
62
63 #ifdef _WIN32
64 #include <gdk/gdkwin32.h>
65 #include <windows.h>
66 #include "ui/win32/file_dlg_win32.h"
67 #endif
68
69 /* save the SSL Session Keys */
70 static gboolean
71 savesslkeys_save_clicked_cb(char *file, gchar *keylist)
72 {
73     int fd;
74
75     fd = ws_open(file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
76     if (fd == -1) {
77         open_failure_alert_box(file, errno, TRUE);
78         return FALSE;
79     }
80     /*
81      * Thanks, Microsoft, for not using size_t for the third argument to
82      * _write().  Presumably this string will be <= 4GiB long....
83      */
84     if (ws_write(fd, keylist, (unsigned int)strlen(keylist)) < 0) {
85         write_failure_alert_box(file, errno);
86         ws_close(fd);
87         return FALSE;
88     }
89     if (ws_close(fd) < 0) {
90         write_failure_alert_box(file, errno);
91         return FALSE;
92     }
93
94     g_free(keylist);
95     return TRUE;
96 }
97
98
99 /* Launch the dialog box to put up the file selection box etc */
100 #ifdef _WIN32
101 void
102 savesslkeys_cb(GtkWidget * w _U_, gpointer data _U_)
103 {
104     win32_export_sslkeys_file(GDK_WINDOW_HWND(gtk_widget_get_window(top_level)));
105     return;
106 }
107 #else
108 static char *
109 gtk_export_sslkeys_file(guint keylist_len)
110 {
111     GtkWidget *savesslkeys_dlg;
112     gchar     *label;
113     GtkWidget *dlg_lb;
114     char      *pathname;
115
116     /*
117      * Build the dialog box we need.
118      */
119     savesslkeys_dlg = file_selection_new("Wireshark: Export SSL Session Keys",
120                                          GTK_WINDOW(top_level),
121                                          FILE_SELECTION_SAVE);
122
123     /* label */
124     label = g_strdup_printf("Will save %u SSL Session %s to specified file.",
125                             keylist_len, plurality(keylist_len, "key", "keys"));
126     dlg_lb = gtk_label_new(label);
127     g_free(label);
128     file_selection_set_extra_widget(savesslkeys_dlg, dlg_lb);
129     gtk_widget_show(dlg_lb);
130
131     pathname = file_selection_run(savesslkeys_dlg);
132     if (pathname == NULL) {
133         /* User cancelled or closed the dialog. */
134         return NULL;
135     }
136
137     /* We've crosed the Rubicon; get rid of the dialog box. */
138     window_destroy(savesslkeys_dlg);
139
140     return pathname;
141 }
142
143 void
144 savesslkeys_cb(GtkWidget * w _U_, gpointer data _U_)
145 {
146     char  *pathname;
147     guint  keylist_len;
148     gchar *keylist;
149
150     keylist_len = ssl_session_key_count();
151     /* don't show up the dialog, if no data has to be saved */
152     if (keylist_len==0) {
153         /* shouldn't happen as the menu item should have been greyed out */
154         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "There are no SSL Session Keys to save.");
155         return;
156     }
157
158     /*
159      * Retrieve the info we need
160      */
161     keylist = ssl_export_sessions();
162
163     /*
164      * Loop until the user either selects a file or gives up.
165      */
166     for (;;) {
167         pathname = gtk_export_sslkeys_file(keylist_len);
168         if (pathname == NULL) {
169             /* User gave up. */
170             break;
171         }
172         if (savesslkeys_save_clicked_cb(pathname, keylist)) {
173             /* We succeeded. */
174             g_free(pathname);
175             break;
176         }
177         /* Dump failed; let the user select another file or give up. */
178         g_free(pathname);
179     }
180 }
181 #endif
182
183 /*
184  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
185  *
186  * Local variables:
187  * c-basic-offset: 4
188  * tab-width: 8
189  * indent-tabs-mode: nil
190  * End:
191  *
192  * vi: set shiftwidth=4 tabstop=8 expandtab:
193  * :indentSize=4:tabSize=8:noTabs=true:
194  */