Fix bug #3574: Template addressing
[claws.git] / src / ssl_manager.c
blobd5086a957f4e1153113812eeeb5c0d38677c9a04
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Colin Leroy and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #include "claws-features.h"
21 #endif
23 #ifdef USE_GNUTLS
24 #include <gtk/gtk.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <sys/types.h>
29 #include <dirent.h>
31 #include "ssl_manager.h"
32 #include "ssl_certificate.h"
33 #include "manage_window.h"
34 #include "utils.h"
35 #include "mainwindow.h"
36 #include "alertpanel.h"
37 #include "sslcertwindow.h"
38 #include "prefs_common.h"
40 enum {
41 SSL_MANAGER_HOST,
42 SSL_MANAGER_PORT,
43 SSL_MANAGER_CERT,
44 N_SSL_MANAGER_COLUMNS
48 static struct SSLManager
50 GtkWidget *window;
51 GtkWidget *hbox1;
52 GtkWidget *vbox1;
53 GtkWidget *certlist;
54 GtkWidget *view_btn;
55 GtkWidget *delete_btn;
56 GtkWidget *close_btn;
57 } manager;
59 static void ssl_manager_view_cb (GtkWidget *widget, gpointer data);
60 static void ssl_manager_delete_cb (GtkWidget *widget, gpointer data);
61 static void ssl_manager_close_cb (GtkWidget *widget, gpointer data);
62 static gboolean key_pressed (GtkWidget *widget, GdkEventKey *event,
63 gpointer data);
64 static void ssl_manager_load_certs (void);
65 static void ssl_manager_double_clicked(GtkTreeView *list_view,
66 GtkTreePath *path,
67 GtkTreeViewColumn *column,
68 gpointer data);
70 void ssl_manager_open(MainWindow *mainwin)
72 if (!manager.window)
73 ssl_manager_create();
75 manage_window_set_transient(GTK_WINDOW(manager.window));
76 gtk_widget_grab_focus(manager.close_btn);
78 ssl_manager_load_certs();
80 gtk_widget_show(manager.window);
84 static GtkListStore* ssl_manager_create_data_store(void)
86 return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
87 G_TYPE_STRING,
88 G_TYPE_STRING,
89 G_TYPE_POINTER,
90 -1);
93 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
95 GtkTreeViewColumn *column;
96 GtkCellRenderer *renderer;
98 renderer = gtk_cell_renderer_text_new();
99 column = gtk_tree_view_column_new_with_attributes
100 (_("Server"),
101 renderer,
102 "text", SSL_MANAGER_HOST,
103 NULL);
104 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
106 renderer = gtk_cell_renderer_text_new();
107 column = gtk_tree_view_column_new_with_attributes
108 (_("Port"),
109 renderer,
110 "text", SSL_MANAGER_PORT,
111 NULL);
112 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
115 static GtkWidget *ssl_manager_list_view_create (void)
117 GtkTreeView *list_view;
118 GtkTreeSelection *selector;
119 GtkTreeModel *model;
121 model = GTK_TREE_MODEL(ssl_manager_create_data_store());
122 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
123 g_object_unref(model);
125 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
126 0, GTK_SORT_ASCENDING);
127 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
129 selector = gtk_tree_view_get_selection(list_view);
130 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
132 g_signal_connect(G_OBJECT(list_view), "row_activated",
133 G_CALLBACK(ssl_manager_double_clicked),
134 list_view);
136 /* create the columns */
137 ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
139 return GTK_WIDGET(list_view);
143 void ssl_manager_create(void)
145 GtkWidget *window;
146 GtkWidget *scroll;
147 GtkWidget *hbox1;
148 GtkWidget *vbox1;
149 GtkWidget *certlist;
150 GtkWidget *view_btn;
151 GtkWidget *delete_btn;
152 GtkWidget *close_btn;
154 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "ssl_manager");
155 gtk_window_set_title (GTK_WINDOW(window),
156 _("Saved SSL/TLS certificates"));
158 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
159 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
160 gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
161 g_signal_connect(G_OBJECT(window), "delete_event",
162 G_CALLBACK(ssl_manager_close_cb), NULL);
163 g_signal_connect(G_OBJECT(window), "key_press_event",
164 G_CALLBACK(key_pressed), NULL);
165 MANAGE_WINDOW_SIGNALS_CONNECT (window);
167 hbox1 = gtk_hbox_new(FALSE, 6);
168 vbox1 = gtk_vbox_new(FALSE, 0);
169 delete_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
171 g_signal_connect(G_OBJECT(delete_btn), "clicked",
172 G_CALLBACK(ssl_manager_delete_cb), NULL);
174 view_btn = gtk_button_new_from_stock(GTK_STOCK_PROPERTIES);
175 g_signal_connect(G_OBJECT(view_btn), "clicked",
176 G_CALLBACK(ssl_manager_view_cb), NULL);
178 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
179 g_signal_connect(G_OBJECT(close_btn), "clicked",
180 G_CALLBACK(ssl_manager_close_cb), NULL);
182 certlist = ssl_manager_list_view_create();
184 scroll = gtk_scrolled_window_new (NULL, NULL);
185 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
186 GTK_POLICY_NEVER,
187 GTK_POLICY_AUTOMATIC);
188 gtk_container_add(GTK_CONTAINER (scroll), certlist);
190 gtk_box_pack_start(GTK_BOX(hbox1), scroll, TRUE, TRUE, 0);
191 gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, FALSE, 0);
192 gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 4);
193 gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 4);
194 gtk_box_pack_end(GTK_BOX(vbox1), close_btn, FALSE, FALSE, 4);
196 gtk_widget_show(certlist);
197 gtk_widget_show(scroll);
198 gtk_widget_show(hbox1);
199 gtk_widget_show(vbox1);
200 gtk_widget_show(close_btn);
201 gtk_widget_show(delete_btn);
202 gtk_widget_show(view_btn);
203 gtk_container_add(GTK_CONTAINER (window), hbox1);
205 manager.window = window;
206 manager.hbox1 = hbox1;
207 manager.vbox1 = vbox1;
208 manager.certlist = certlist;
209 manager.view_btn = view_btn;
210 manager.delete_btn = delete_btn;
211 manager.close_btn = close_btn;
213 gtk_widget_show(window);
216 static char *get_server(const char *str)
218 char *ret = NULL, *tmp = g_strdup(str);
219 char *first_pos = NULL, *last_pos = NULL;
220 char *previous_pos = NULL, *pre_previous_pos = NULL;
221 int previous_dot_pos;
223 if (!strchr(tmp, ':')) {
224 /* no fingerprint */
225 if (strstr(tmp, ".cert"))
226 *(strstr(tmp, ".cert")+1) = '.';
229 first_pos = tmp;
230 while (tmp && (tmp = strstr(tmp,".")) != NULL) {
231 tmp++;
232 pre_previous_pos = previous_pos;
233 previous_pos = last_pos;
234 last_pos = tmp;
236 previous_dot_pos = (pre_previous_pos - first_pos);
237 if (previous_dot_pos - 1 > 0)
238 ret = g_strndup(first_pos, previous_dot_pos - 1);
239 else
240 ret = g_strdup(first_pos);
241 g_free(first_pos);
242 return ret;
245 static char *get_port(const char *str)
247 char *ret = NULL, *tmp = g_strdup(str);
248 char *last_pos = NULL;
249 char *previous_pos = NULL, *pre_previous_pos = NULL;
251 if (!strchr(tmp, ':')) {
252 /* no fingerprint */
253 if (strstr(tmp, ".cert"))
254 *(strstr(tmp, ".cert")+1) = '.';
257 while (tmp && (tmp = strstr(tmp,".")) != NULL) {
258 tmp++;
259 pre_previous_pos = previous_pos;
260 previous_pos = last_pos;
261 last_pos = tmp;
263 if (previous_pos && pre_previous_pos && (int)(previous_pos - pre_previous_pos - 1) > 0)
264 ret = g_strndup(pre_previous_pos, (int)(previous_pos - pre_previous_pos - 1));
265 else
266 ret = g_strdup("0");
267 g_free(tmp);
268 return ret;
272 static char *get_fingerprint(const char *str)
274 char *ret = NULL, *tmp = g_strdup(str);
275 char *previous_pos = NULL, *last_pos = NULL;
277 if (!strchr(tmp, ':')) {
278 /* no fingerprint */
279 if (strstr(tmp, ".cert"))
280 *(strstr(tmp, ".cert")+1) = '.';
283 while (tmp && (tmp = strstr(tmp,".")) != NULL) {
284 tmp++;
285 previous_pos = last_pos;
286 last_pos = tmp;
288 if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
289 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
290 else
291 ret = NULL;
292 g_free(tmp);
293 return ret;
297 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
298 GtkTreeIter *row_iter,
299 gchar *host,
300 gchar *port,
301 SSLCertificate *cert)
303 GtkTreeIter iter;
304 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
305 (GTK_TREE_VIEW(list_view)));
307 if (row_iter == NULL) {
308 /* append new */
309 gtk_list_store_append(list_store, &iter);
310 gtk_list_store_set(list_store, &iter,
311 SSL_MANAGER_HOST, host,
312 SSL_MANAGER_PORT, port,
313 SSL_MANAGER_CERT, cert,
314 -1);
315 } else {
316 gtk_list_store_set(list_store, row_iter,
317 SSL_MANAGER_HOST, host,
318 SSL_MANAGER_PORT, port,
319 SSL_MANAGER_CERT, cert,
320 -1);
324 static void ssl_manager_load_certs (void)
326 GDir *dir;
327 const gchar *d;
328 GError *error = NULL;
329 gchar *path;
330 int row = 0;
331 GtkListStore *store;
333 store = GTK_LIST_STORE(gtk_tree_view_get_model
334 (GTK_TREE_VIEW(manager.certlist)));
336 gtk_list_store_clear(store);
338 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
339 "certs", G_DIR_SEPARATOR_S, NULL);
341 if((dir = g_dir_open(path, 0, &error)) == NULL) {
342 debug_print("couldn't open dir '%s': %s (%d)\n", path,
343 error->message, error->code);
344 g_error_free(error);
345 return;
348 while ((d = g_dir_read_name(dir)) != NULL) {
349 gchar *server, *port, *fp;
350 SSLCertificate *cert;
352 if(strstr(d, ".cert") != d + (strlen(d) - strlen(".cert")))
353 continue;
355 server = get_server(d);
356 port = get_port(d);
357 fp = get_fingerprint(d);
359 cert = ssl_certificate_find(server, atoi(port), fp);
361 ssl_manager_list_view_insert_cert(manager.certlist, NULL,
362 server, port, cert);
364 g_free(server);
365 g_free(port);
366 g_free(fp);
367 row++;
369 g_dir_close(dir);
370 g_free(path);
373 static void ssl_manager_close(void)
375 gtk_widget_hide(manager.window);
378 static void ssl_manager_close_cb(GtkWidget *widget,
379 gpointer data)
381 ssl_manager_close();
384 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
386 if (event && event->keyval == GDK_KEY_Escape)
387 ssl_manager_close();
388 return FALSE;
391 static void ssl_manager_double_clicked(GtkTreeView *list_view,
392 GtkTreePath *path,
393 GtkTreeViewColumn *column,
394 gpointer data)
396 SSLCertificate *cert;
397 GtkTreeIter iter;
398 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
400 if (!gtk_tree_model_get_iter(model, &iter, path))
401 return;
403 gtk_tree_model_get(model, &iter,
404 SSL_MANAGER_CERT, &cert,
405 -1);
407 if (!cert)
408 return;
410 sslcertwindow_show_cert(cert);
412 return;
417 static void ssl_manager_delete_cb(GtkWidget *widget,
418 gpointer data)
420 SSLCertificate *cert;
421 int val;
422 GtkTreeIter sel;
423 GtkTreeModel *model;
425 if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
426 (GTK_TREE_VIEW(manager.certlist)),
427 &model, &sel))
428 return;
430 gtk_tree_model_get(model, &sel,
431 SSL_MANAGER_CERT, &cert,
432 -1);
433 if (!cert)
434 return;
436 val = alertpanel_full(_("Delete certificate"),
437 _("Do you really want to delete this certificate?"),
438 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
439 NULL, ALERT_WARNING, G_ALERTDEFAULT);
442 if (val != G_ALERTALTERNATE)
443 return;
445 ssl_certificate_delete_from_disk(cert);
446 ssl_certificate_destroy(cert);
447 gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
450 static void ssl_manager_view_cb(GtkWidget *widget,
451 gpointer data)
453 SSLCertificate *cert;
454 GtkTreeIter sel;
455 GtkTreeModel *model;
457 if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
458 (GTK_TREE_VIEW(manager.certlist)),
459 &model, &sel))
460 return;
462 gtk_tree_model_get(model, &sel,
463 SSL_MANAGER_CERT, &cert,
464 -1);
465 if (!cert)
466 return;
468 sslcertwindow_show_cert(cert);
470 #endif