Add scroll to SSL certificates list
[claws.git] / src / ssl_manager.c
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
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #include "claws-features.h"
21 #endif
22
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>
30
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"
39
40 enum {
41         SSL_MANAGER_HOST,
42         SSL_MANAGER_PORT,
43         SSL_MANAGER_CERT,
44         N_SSL_MANAGER_COLUMNS
45 };
46
47
48 static struct SSLManager
49 {
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;
58
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);
69
70 void ssl_manager_open(MainWindow *mainwin)
71 {
72         if (!manager.window)
73                 ssl_manager_create();
74
75         manage_window_set_transient(GTK_WINDOW(manager.window));
76         gtk_widget_grab_focus(manager.close_btn);
77
78         ssl_manager_load_certs();
79
80         gtk_widget_show(manager.window);
81
82 }
83
84 static GtkListStore* ssl_manager_create_data_store(void)
85 {
86         return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
87                                   G_TYPE_STRING,
88                                   G_TYPE_STRING,
89                                   G_TYPE_POINTER,
90                                   -1);
91 }
92
93 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
94 {
95         GtkTreeViewColumn *column;
96         GtkCellRenderer *renderer;
97
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);          
105
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);          
113 }
114
115 static GtkWidget *ssl_manager_list_view_create  (void)
116 {
117         GtkTreeView *list_view;
118         GtkTreeSelection *selector;
119         GtkTreeModel *model;
120
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);  
124         
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);
128         
129         selector = gtk_tree_view_get_selection(list_view);
130         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
131
132         g_signal_connect(G_OBJECT(list_view), "row_activated",
133                          G_CALLBACK(ssl_manager_double_clicked),
134                          list_view);
135
136         /* create the columns */
137         ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
138
139         return GTK_WIDGET(list_view);
140
141 }
142
143 void ssl_manager_create(void)
144 {
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;
153
154         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "ssl_manager");
155         gtk_window_set_title (GTK_WINDOW(window),
156                               _("Saved SSL certificates"));
157
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);
166
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);
170
171         g_signal_connect(G_OBJECT(delete_btn), "clicked",
172                          G_CALLBACK(ssl_manager_delete_cb), NULL);
173
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);
177
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);
181
182         certlist = ssl_manager_list_view_create();
183
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);
189
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);
195
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);
204
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;
212
213         gtk_widget_show(window);
214 }
215
216 static char *get_server(const char *str)
217 {
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;
222
223         if (!strchr(tmp, ':')) {
224                 /* no fingerprint */
225                 if (strstr(tmp, ".cert"))
226                         *(strstr(tmp, ".cert")+1) = '.';
227         }
228
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;
235         }
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;
243 }
244
245 static char *get_port(const char *str)
246 {
247         char *ret = NULL, *tmp = g_strdup(str);
248         char *last_pos = NULL;
249         char *previous_pos = NULL, *pre_previous_pos = NULL;
250
251         if (!strchr(tmp, ':')) {
252                 /* no fingerprint */
253                 if (strstr(tmp, ".cert"))
254                         *(strstr(tmp, ".cert")+1) = '.';
255         }
256
257         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
258                 tmp++;
259                 pre_previous_pos = previous_pos;
260                 previous_pos = last_pos;
261                 last_pos = tmp;
262         }
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;
269         
270 }
271
272 static char *get_fingerprint(const char *str)
273 {
274         char *ret = NULL, *tmp = g_strdup(str);
275         char *previous_pos = NULL, *last_pos = NULL;
276
277         if (!strchr(tmp, ':')) {
278                 /* no fingerprint */
279                 if (strstr(tmp, ".cert"))
280                         *(strstr(tmp, ".cert")+1) = '.';
281         }
282
283         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
284                 tmp++;
285                 previous_pos = last_pos;
286                 last_pos = tmp;
287         }
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;
294         
295 }
296
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) 
302 {
303         GtkTreeIter iter;
304         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
305                                         (GTK_TREE_VIEW(list_view)));
306
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);
321         }
322 }
323
324 static void ssl_manager_load_certs (void) 
325 {
326         GDir *dir;
327         const gchar *d;
328         GError *error = NULL;
329         gchar *path;
330         int row = 0;
331         GtkListStore *store;
332
333         store = GTK_LIST_STORE(gtk_tree_view_get_model
334                                 (GTK_TREE_VIEW(manager.certlist)));
335
336         gtk_list_store_clear(store);
337
338         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
339                           "certs", G_DIR_SEPARATOR_S, NULL);
340
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;
346         }
347         
348         while ((d = g_dir_read_name(dir)) != NULL) {
349                 gchar *server, *port, *fp;
350                 SSLCertificate *cert;
351
352                 if(strstr(d, ".cert") != d + (strlen(d) - strlen(".cert"))) 
353                         continue;
354
355                 server = get_server(d);
356                 port = get_port(d);
357                 fp = get_fingerprint(d);
358                 
359                 cert = ssl_certificate_find(server, atoi(port), fp);
360
361                 ssl_manager_list_view_insert_cert(manager.certlist, NULL, 
362                                                   server, port, cert);
363                 
364                 g_free(server);
365                 g_free(port);
366                 g_free(fp);
367                 row++;
368         }
369         g_dir_close(dir);
370         g_free(path);
371 }
372
373 static void ssl_manager_close(void) 
374 {
375         gtk_widget_hide(manager.window);
376 }
377
378 static void ssl_manager_close_cb(GtkWidget *widget,
379                                  gpointer data) 
380 {
381         ssl_manager_close();
382 }
383
384 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
385 {
386         if (event && event->keyval == GDK_KEY_Escape)
387                 ssl_manager_close();
388         return FALSE;
389 }
390
391 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
392                                         GtkTreePath             *path,
393                                         GtkTreeViewColumn       *column,
394                                         gpointer                 data)
395 {
396         SSLCertificate *cert;
397         GtkTreeIter iter;
398         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
399
400         if (!gtk_tree_model_get_iter(model, &iter, path))
401                 return;
402
403         gtk_tree_model_get(model, &iter, 
404                            SSL_MANAGER_CERT, &cert,
405                            -1);
406
407         if (!cert)
408                 return;
409
410         sslcertwindow_show_cert(cert);
411
412         return;
413 }
414
415
416
417 static void ssl_manager_delete_cb(GtkWidget *widget, 
418                               gpointer data) 
419 {
420         SSLCertificate *cert;
421         int val;
422         GtkTreeIter sel;
423         GtkTreeModel *model;
424
425         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
426                                 (GTK_TREE_VIEW(manager.certlist)),
427                                 &model, &sel))
428                 return;
429         
430         gtk_tree_model_get(model, &sel,
431                            SSL_MANAGER_CERT, &cert,
432                            -1);
433         if (!cert)
434                 return;
435
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);
440
441                              
442         if (val != G_ALERTALTERNATE)
443                 return;
444         
445         ssl_certificate_delete_from_disk(cert);
446         ssl_certificate_destroy(cert);
447         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
448 }
449
450 static void ssl_manager_view_cb(GtkWidget *widget, 
451                                 gpointer data) 
452 {
453         SSLCertificate *cert;
454         GtkTreeIter sel;
455         GtkTreeModel *model;
456
457         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
458                                 (GTK_TREE_VIEW(manager.certlist)),
459                                 &model, &sel))
460                 return;
461         
462         gtk_tree_model_get(model, &sel,
463                            SSL_MANAGER_CERT, &cert,
464                            -1);
465         if (!cert)
466                 return;
467
468         sslcertwindow_show_cert(cert);
469 }
470 #endif