Implement saving of the certificate chain, making the offline status
[claws.git] / src / ssl_manager.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #ifdef USE_GNUTLS
26 #include <gtk/gtk.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32
33 #include "ssl_manager.h"
34 #include "ssl_certificate.h"
35 #include "manage_window.h"
36 #include "utils.h"
37 #include "mainwindow.h"
38 #include "alertpanel.h"
39 #include "sslcertwindow.h"
40 #include "prefs_common.h"
41
42 enum {
43         SSL_MANAGER_HOST,
44         SSL_MANAGER_PORT,
45         SSL_MANAGER_CERT,
46         N_SSL_MANAGER_COLUMNS
47 };
48
49
50 static struct SSLManager
51 {
52         GtkWidget *window;
53         GtkWidget *hbox1;
54         GtkWidget *vbox1;
55         GtkWidget *certlist;
56         GtkWidget *view_btn;
57         GtkWidget *delete_btn;
58         GtkWidget *close_btn;
59 } manager;
60
61 static void ssl_manager_view_cb         (GtkWidget *widget, gpointer data);
62 static void ssl_manager_delete_cb       (GtkWidget *widget, gpointer data);
63 static void ssl_manager_close_cb        (GtkWidget *widget, gpointer data);
64 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
65                                          gpointer data);
66 static void ssl_manager_load_certs      (void);
67 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
68                                         GtkTreePath             *path,
69                                         GtkTreeViewColumn       *column,
70                                         gpointer                 data);
71
72 void ssl_manager_open(MainWindow *mainwin)
73 {
74         if (!manager.window)
75                 ssl_manager_create();
76
77         manage_window_set_transient(GTK_WINDOW(manager.window));
78         gtk_widget_grab_focus(manager.close_btn);
79
80         ssl_manager_load_certs();
81
82         gtk_widget_show(manager.window);
83
84 }
85
86 static GtkListStore* ssl_manager_create_data_store(void)
87 {
88         return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
89                                   G_TYPE_STRING,
90                                   G_TYPE_STRING,
91                                   G_TYPE_POINTER,
92                                   -1);
93 }
94
95 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
96 {
97         GtkTreeViewColumn *column;
98         GtkCellRenderer *renderer;
99
100         renderer = gtk_cell_renderer_text_new();
101         column = gtk_tree_view_column_new_with_attributes
102                 (_("Server"),
103                  renderer,
104                  "text", SSL_MANAGER_HOST,
105                  NULL);
106         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
107
108         renderer = gtk_cell_renderer_text_new();
109         column = gtk_tree_view_column_new_with_attributes
110                 (_("Port"),
111                  renderer,
112                  "text", SSL_MANAGER_PORT,
113                  NULL);
114         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
115 }
116
117 static GtkWidget *ssl_manager_list_view_create  (void)
118 {
119         GtkTreeView *list_view;
120         GtkTreeSelection *selector;
121         GtkTreeModel *model;
122
123         model = GTK_TREE_MODEL(ssl_manager_create_data_store());
124         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
125         g_object_unref(model);  
126         
127         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
128                                              0, GTK_SORT_ASCENDING);
129         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
130         
131         selector = gtk_tree_view_get_selection(list_view);
132         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
133
134         g_signal_connect(G_OBJECT(list_view), "row_activated",
135                          G_CALLBACK(ssl_manager_double_clicked),
136                          list_view);
137
138         /* create the columns */
139         ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
140
141         return GTK_WIDGET(list_view);
142
143 }
144
145 void ssl_manager_create(void) 
146 {
147         GtkWidget *window;
148         GtkWidget *hbox1;
149         GtkWidget *vbox1;
150         GtkWidget *certlist;
151         GtkWidget *view_btn;
152         GtkWidget *delete_btn;
153         GtkWidget *close_btn;
154
155         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "ssl_manager");
156         gtk_window_set_title (GTK_WINDOW(window),
157                               _("Saved SSL certificates"));
158
159         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
160         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
161         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
162         g_signal_connect(G_OBJECT(window), "delete_event",
163                          G_CALLBACK(ssl_manager_close_cb), NULL);
164         g_signal_connect(G_OBJECT(window), "key_press_event",
165                          G_CALLBACK(key_pressed), NULL);
166         MANAGE_WINDOW_SIGNALS_CONNECT (window);
167
168         hbox1 = gtk_hbox_new(FALSE, 6);
169         vbox1 = gtk_vbox_new(FALSE, 0);
170         delete_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
171         
172         g_signal_connect(G_OBJECT(delete_btn), "clicked",
173                          G_CALLBACK(ssl_manager_delete_cb), NULL);
174
175         view_btn = gtk_button_new_from_stock(GTK_STOCK_PROPERTIES);
176         g_signal_connect(G_OBJECT(view_btn), "clicked",
177                          G_CALLBACK(ssl_manager_view_cb), NULL);
178
179         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
180         g_signal_connect(G_OBJECT(close_btn), "clicked",
181                          G_CALLBACK(ssl_manager_close_cb), NULL);
182
183         certlist = ssl_manager_list_view_create();
184         
185         gtk_box_pack_start(GTK_BOX(hbox1), certlist, TRUE, TRUE, 0);
186         gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, FALSE, 0);
187         gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 4);
188         gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 4);
189         gtk_box_pack_end(GTK_BOX(vbox1), close_btn, FALSE, FALSE, 4);
190         
191         gtk_widget_show(certlist);
192         gtk_widget_show(hbox1);
193         gtk_widget_show(vbox1);
194         gtk_widget_show(close_btn);
195         gtk_widget_show(delete_btn);
196         gtk_widget_show(view_btn);
197         gtk_container_add(GTK_CONTAINER (window), hbox1);
198
199         manager.window = window;
200         manager.hbox1 = hbox1;
201         manager.vbox1 = vbox1;
202         manager.certlist = certlist;
203         manager.view_btn = view_btn;
204         manager.delete_btn = delete_btn;
205         manager.close_btn = close_btn;
206
207         gtk_widget_show(window);
208                 
209 }
210
211 static char *get_server(char *str)
212 {
213         char *ret = NULL, *tmp = g_strdup(str);
214         char *first_pos = NULL, *last_pos = NULL;
215         char *previous_pos = NULL, *pre_previous_pos = NULL;
216         int previous_dot_pos;
217
218         if (!strchr(tmp, ':')) {
219                 /* no fingerprint */
220                 if (strstr(tmp, ".cert"))
221                         *(strstr(tmp, ".cert")+1) = '.';
222         }
223
224         first_pos = tmp;
225         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
226                 tmp++;
227                 pre_previous_pos = previous_pos;
228                 previous_pos = last_pos;
229                 last_pos = tmp;
230         }
231         previous_dot_pos = (pre_previous_pos - first_pos);
232         if (previous_dot_pos - 1 > 0)
233                 ret = g_strndup(first_pos, previous_dot_pos - 1);
234         else 
235                 ret = g_strdup(first_pos);
236         g_free(first_pos);
237         return ret;
238 }
239
240 static char *get_port(char *str)
241 {
242         char *ret = NULL, *tmp = g_strdup(str);
243         char *last_pos = NULL;
244         char *previous_pos = NULL, *pre_previous_pos = NULL;
245
246         if (!strchr(tmp, ':')) {
247                 /* no fingerprint */
248                 if (strstr(tmp, ".cert"))
249                         *(strstr(tmp, ".cert")+1) = '.';
250         }
251
252         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
253                 tmp++;
254                 pre_previous_pos = previous_pos;
255                 previous_pos = last_pos;
256                 last_pos = tmp;
257         }
258         if (previous_pos && pre_previous_pos && (int)(previous_pos - pre_previous_pos - 1) > 0)
259                 ret = g_strndup(pre_previous_pos, (int)(previous_pos - pre_previous_pos - 1));
260         else
261                 ret = g_strdup("0");
262         g_free(tmp);
263         return ret;
264         
265 }
266
267 static char *get_fingerprint(char *str)
268 {
269         char *ret = NULL, *tmp = g_strdup(str);
270         char *previous_pos = NULL, *last_pos = NULL;
271
272         if (!strchr(tmp, ':')) {
273                 /* no fingerprint */
274                 if (strstr(tmp, ".cert"))
275                         *(strstr(tmp, ".cert")+1) = '.';
276         }
277
278         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
279                 tmp++;
280                 previous_pos = last_pos;
281                 last_pos = tmp;
282         }
283         if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
284                 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
285         else
286                 ret = NULL;
287         g_free(tmp);
288         return ret;
289         
290 }
291
292 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
293                                                   GtkTreeIter *row_iter,
294                                                   gchar *host, 
295                                                   gchar *port,
296                                                   SSLCertificate *cert) 
297 {
298         GtkTreeIter iter;
299         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
300                                         (GTK_TREE_VIEW(list_view)));
301
302         if (row_iter == NULL) {
303                 /* append new */
304                 gtk_list_store_append(list_store, &iter);
305                 gtk_list_store_set(list_store, &iter,
306                                    SSL_MANAGER_HOST, host,
307                                    SSL_MANAGER_PORT, port,
308                                    SSL_MANAGER_CERT, cert,
309                                    -1);
310         } else {
311                 gtk_list_store_set(list_store, row_iter,
312                                    SSL_MANAGER_HOST, host,
313                                    SSL_MANAGER_PORT, port,
314                                    SSL_MANAGER_CERT, cert,
315                                    -1);
316         }
317 }
318
319 static void ssl_manager_load_certs (void) 
320 {
321         DIR *dir;
322         struct dirent *d;
323         gchar *path;
324         int row = 0;
325         GtkListStore *store;
326
327         store = GTK_LIST_STORE(gtk_tree_view_get_model
328                                 (GTK_TREE_VIEW(manager.certlist)));
329
330         gtk_list_store_clear(store);
331
332         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
333                           "certs", G_DIR_SEPARATOR_S, NULL);
334
335         if((dir = opendir(path)) == NULL) {
336                 perror("opendir");
337                 return;
338         }
339         
340         while ((d = readdir(dir)) != NULL) {
341                 gchar *server, *port, *fp;
342                 SSLCertificate *cert;
343
344                 if(strstr(d->d_name, ".cert") != d->d_name + (strlen(d->d_name) - strlen(".cert"))) 
345                         continue;
346
347                 server = get_server(d->d_name);
348                 port = get_port(d->d_name);
349                 fp = get_fingerprint(d->d_name);
350                 
351                 cert = ssl_certificate_find(server, atoi(port), fp);
352
353                 ssl_manager_list_view_insert_cert(manager.certlist, NULL, 
354                                                   server, port, cert);
355                 
356                 g_free(server);
357                 g_free(port);
358                 g_free(fp);
359                 row++;
360         }
361         closedir(dir);
362         g_free(path);
363 }
364
365 static void ssl_manager_close(void) 
366 {
367         gtk_widget_hide(manager.window);
368 }
369
370 static void ssl_manager_close_cb(GtkWidget *widget,
371                                  gpointer data) 
372 {
373         ssl_manager_close();
374 }
375
376 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
377 {
378         if (event && event->keyval == GDK_KEY_Escape)
379                 ssl_manager_close();
380         return FALSE;
381 }
382
383 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
384                                         GtkTreePath             *path,
385                                         GtkTreeViewColumn       *column,
386                                         gpointer                 data)
387 {
388         SSLCertificate *cert;
389         GtkTreeIter iter;
390         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
391
392         if (!gtk_tree_model_get_iter(model, &iter, path))
393                 return;
394
395         gtk_tree_model_get(model, &iter, 
396                            SSL_MANAGER_CERT, &cert,
397                            -1);
398
399         if (!cert)
400                 return;
401
402         sslcertwindow_show_cert(cert);
403
404         return;
405 }
406
407
408
409 static void ssl_manager_delete_cb(GtkWidget *widget, 
410                               gpointer data) 
411 {
412         SSLCertificate *cert;
413         int val;
414         GtkTreeIter sel;
415         GtkTreeModel *model;
416
417         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
418                                 (GTK_TREE_VIEW(manager.certlist)),
419                                 &model, &sel))
420                 return;
421         
422         gtk_tree_model_get(model, &sel,
423                            SSL_MANAGER_CERT, &cert,
424                            -1);
425         if (!cert)
426                 return;
427
428         val = alertpanel_full(_("Delete certificate"),
429                               _("Do you really want to delete this certificate?"),
430                               GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
431                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
432
433                              
434         if (val != G_ALERTALTERNATE)
435                 return;
436         
437         ssl_certificate_delete_from_disk(cert);
438         ssl_certificate_destroy(cert);
439         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
440 }
441
442 static void ssl_manager_view_cb(GtkWidget *widget, 
443                                 gpointer data) 
444 {
445         SSLCertificate *cert;
446         GtkTreeIter sel;
447         GtkTreeModel *model;
448
449         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
450                                 (GTK_TREE_VIEW(manager.certlist)),
451                                 &model, &sel))
452                 return;
453         
454         gtk_tree_model_get(model, &sel,
455                            SSL_MANAGER_CERT, &cert,
456                            -1);
457         if (!cert)
458                 return;
459
460         sslcertwindow_show_cert(cert);
461 }
462 #endif