2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / ssl_manager.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 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 #endif
23
24 #ifdef USE_GNUTLS
25 #include <gtk/gtk.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31
32 #include "ssl_manager.h"
33 #include "ssl_certificate.h"
34 #include "manage_window.h"
35 #include "utils.h"
36 #include "mainwindow.h"
37 #include "alertpanel.h"
38 #include "sslcertwindow.h"
39 #include "prefs_common.h"
40
41 enum {
42         SSL_MANAGER_HOST,
43         SSL_MANAGER_PORT,
44         SSL_MANAGER_CERT,
45         N_SSL_MANAGER_COLUMNS
46 };
47
48
49 static struct SSLManager
50 {
51         GtkWidget *window;
52         GtkWidget *hbox1;
53         GtkWidget *vbox1;
54         GtkWidget *certlist;
55         GtkWidget *view_btn;
56         GtkWidget *delete_btn;
57         GtkWidget *close_btn;
58 } manager;
59
60 static void ssl_manager_view_cb         (GtkWidget *widget, gpointer data);
61 static void ssl_manager_delete_cb       (GtkWidget *widget, gpointer data);
62 static void ssl_manager_close_cb        (GtkWidget *widget, gpointer data);
63 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
64                                          gpointer data);
65 static void ssl_manager_load_certs      (void);
66 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
67                                         GtkTreePath             *path,
68                                         GtkTreeViewColumn       *column,
69                                         gpointer                 data);
70
71 void ssl_manager_open(MainWindow *mainwin)
72 {
73         if (!manager.window)
74                 ssl_manager_create();
75
76         manage_window_set_transient(GTK_WINDOW(manager.window));
77         gtk_widget_grab_focus(manager.close_btn);
78
79         ssl_manager_load_certs();
80
81         gtk_widget_show(manager.window);
82
83 }
84
85 static GtkListStore* ssl_manager_create_data_store(void)
86 {
87         return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
88                                   G_TYPE_STRING,
89                                   G_TYPE_STRING,
90                                   G_TYPE_POINTER,
91                                   -1);
92 }
93
94 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
95 {
96         GtkTreeViewColumn *column;
97         GtkCellRenderer *renderer;
98
99         renderer = gtk_cell_renderer_text_new();
100         column = gtk_tree_view_column_new_with_attributes
101                 (_("Server"),
102                  renderer,
103                  "text", SSL_MANAGER_HOST,
104                  NULL);
105         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
106
107         renderer = gtk_cell_renderer_text_new();
108         column = gtk_tree_view_column_new_with_attributes
109                 (_("Port"),
110                  renderer,
111                  "text", SSL_MANAGER_PORT,
112                  NULL);
113         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
114 }
115
116 static GtkWidget *ssl_manager_list_view_create  (void)
117 {
118         GtkTreeView *list_view;
119         GtkTreeSelection *selector;
120         GtkTreeModel *model;
121
122         model = GTK_TREE_MODEL(ssl_manager_create_data_store());
123         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
124         g_object_unref(model);  
125         
126         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
127                                              0, GTK_SORT_ASCENDING);
128         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
129         
130         selector = gtk_tree_view_get_selection(list_view);
131         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
132
133         g_signal_connect(G_OBJECT(list_view), "row_activated",
134                          G_CALLBACK(ssl_manager_double_clicked),
135                          list_view);
136
137         /* create the columns */
138         ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
139
140         return GTK_WIDGET(list_view);
141
142 }
143
144 void ssl_manager_create(void) 
145 {
146         GtkWidget *window;
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         gtk_box_pack_start(GTK_BOX(hbox1), certlist, TRUE, TRUE, 0);
185         gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, FALSE, 0);
186         gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 4);
187         gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 4);
188         gtk_box_pack_end(GTK_BOX(vbox1), close_btn, FALSE, FALSE, 4);
189         
190         gtk_widget_show(certlist);
191         gtk_widget_show(hbox1);
192         gtk_widget_show(vbox1);
193         gtk_widget_show(close_btn);
194         gtk_widget_show(delete_btn);
195         gtk_widget_show(view_btn);
196         gtk_container_add(GTK_CONTAINER (window), hbox1);
197
198         manager.window = window;
199         manager.hbox1 = hbox1;
200         manager.vbox1 = vbox1;
201         manager.certlist = certlist;
202         manager.view_btn = view_btn;
203         manager.delete_btn = delete_btn;
204         manager.close_btn = close_btn;
205
206         gtk_widget_show(window);
207                 
208 }
209
210 static char *get_server(char *str)
211 {
212         char *ret = NULL, *tmp = g_strdup(str);
213         char *first_pos = NULL, *last_pos = NULL;
214         char *previous_pos = NULL, *pre_previous_pos = NULL;
215         int previous_dot_pos;
216
217         if (!strchr(tmp, ':')) {
218                 /* no fingerprint */
219                 if (strstr(tmp, ".cert"))
220                         *(strstr(tmp, ".cert")+1) = '.';
221         }
222
223         first_pos = tmp;
224         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
225                 tmp++;
226                 pre_previous_pos = previous_pos;
227                 previous_pos = last_pos;
228                 last_pos = tmp;
229         }
230         previous_dot_pos = (pre_previous_pos - first_pos);
231         if (previous_dot_pos - 1 > 0)
232                 ret = g_strndup(first_pos, previous_dot_pos - 1);
233         else 
234                 ret = g_strdup(first_pos);
235         g_free(first_pos);
236         return ret;
237 }
238
239 static char *get_port(char *str)
240 {
241         char *ret = NULL, *tmp = g_strdup(str);
242         char *last_pos = NULL;
243         char *previous_pos = NULL, *pre_previous_pos = NULL;
244
245         if (!strchr(tmp, ':')) {
246                 /* no fingerprint */
247                 if (strstr(tmp, ".cert"))
248                         *(strstr(tmp, ".cert")+1) = '.';
249         }
250
251         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
252                 tmp++;
253                 pre_previous_pos = previous_pos;
254                 previous_pos = last_pos;
255                 last_pos = tmp;
256         }
257         if (previous_pos && pre_previous_pos && (int)(previous_pos - pre_previous_pos - 1) > 0)
258                 ret = g_strndup(pre_previous_pos, (int)(previous_pos - pre_previous_pos - 1));
259         else
260                 ret = g_strdup("0");
261         g_free(tmp);
262         return ret;
263         
264 }
265
266 static char *get_fingerprint(char *str)
267 {
268         char *ret = NULL, *tmp = g_strdup(str);
269         char *previous_pos = NULL, *last_pos = NULL;
270
271         if (!strchr(tmp, ':')) {
272                 /* no fingerprint */
273                 if (strstr(tmp, ".cert"))
274                         *(strstr(tmp, ".cert")+1) = '.';
275         }
276
277         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
278                 tmp++;
279                 previous_pos = last_pos;
280                 last_pos = tmp;
281         }
282         if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
283                 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
284         else
285                 ret = NULL;
286         g_free(tmp);
287         return ret;
288         
289 }
290
291 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
292                                                   GtkTreeIter *row_iter,
293                                                   gchar *host, 
294                                                   gchar *port,
295                                                   SSLCertificate *cert) 
296 {
297         GtkTreeIter iter;
298         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
299                                         (GTK_TREE_VIEW(list_view)));
300
301         if (row_iter == NULL) {
302                 /* append new */
303                 gtk_list_store_append(list_store, &iter);
304                 gtk_list_store_set(list_store, &iter,
305                                    SSL_MANAGER_HOST, host,
306                                    SSL_MANAGER_PORT, port,
307                                    SSL_MANAGER_CERT, cert,
308                                    -1);
309         } else {
310                 gtk_list_store_set(list_store, row_iter,
311                                    SSL_MANAGER_HOST, host,
312                                    SSL_MANAGER_PORT, port,
313                                    SSL_MANAGER_CERT, cert,
314                                    -1);
315         }
316 }
317
318 static void ssl_manager_load_certs (void) 
319 {
320         DIR *dir;
321         struct dirent *d;
322         gchar *path;
323         int row = 0;
324         GtkListStore *store;
325
326         store = GTK_LIST_STORE(gtk_tree_view_get_model
327                                 (GTK_TREE_VIEW(manager.certlist)));
328
329         gtk_list_store_clear(store);
330
331         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
332                           "certs", G_DIR_SEPARATOR_S, NULL);
333
334         if((dir = opendir(path)) == NULL) {
335                 perror("opendir");
336                 return;
337         }
338         
339         while ((d = readdir(dir)) != NULL) {
340                 gchar *server, *port, *fp;
341                 SSLCertificate *cert;
342
343                 if(!strstr(d->d_name, ".cert")) 
344                         continue;
345
346                 server = get_server(d->d_name);
347                 port = get_port(d->d_name);
348                 fp = get_fingerprint(d->d_name);
349                 
350                 cert = ssl_certificate_find(server, atoi(port), fp);
351
352                 ssl_manager_list_view_insert_cert(manager.certlist, NULL, 
353                                                   server, port, cert);
354                 
355                 g_free(server);
356                 g_free(port);
357                 g_free(fp);
358                 row++;
359         }
360         closedir(dir);
361         g_free(path);
362 }
363
364 static void ssl_manager_close(void) 
365 {
366         gtk_widget_hide(manager.window);
367 }
368
369 static void ssl_manager_close_cb(GtkWidget *widget,
370                                  gpointer data) 
371 {
372         ssl_manager_close();
373 }
374
375 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
376 {
377         if (event && event->keyval == GDK_KEY_Escape)
378                 ssl_manager_close();
379         return FALSE;
380 }
381
382 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
383                                         GtkTreePath             *path,
384                                         GtkTreeViewColumn       *column,
385                                         gpointer                 data)
386 {
387         SSLCertificate *cert;
388         GtkTreeIter iter;
389         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
390
391         if (!gtk_tree_model_get_iter(model, &iter, path))
392                 return;
393
394         gtk_tree_model_get(model, &iter, 
395                            SSL_MANAGER_CERT, &cert,
396                            -1);
397
398         if (!cert)
399                 return;
400
401         sslcertwindow_show_cert(cert);
402
403         return;
404 }
405
406
407
408 static void ssl_manager_delete_cb(GtkWidget *widget, 
409                               gpointer data) 
410 {
411         SSLCertificate *cert;
412         int val;
413         GtkTreeIter sel;
414         GtkTreeModel *model;
415
416         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
417                                 (GTK_TREE_VIEW(manager.certlist)),
418                                 &model, &sel))
419                 return;
420         
421         gtk_tree_model_get(model, &sel,
422                            SSL_MANAGER_CERT, &cert,
423                            -1);
424         if (!cert)
425                 return;
426
427         val = alertpanel_full(_("Delete certificate"),
428                               _("Do you really want to delete this certificate?"),
429                               GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
430                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
431
432                              
433         if (val != G_ALERTALTERNATE)
434                 return;
435         
436         ssl_certificate_delete_from_disk(cert);
437         ssl_certificate_destroy(cert);
438         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
439 }
440
441 static void ssl_manager_view_cb(GtkWidget *widget, 
442                                 gpointer data) 
443 {
444         SSLCertificate *cert;
445         GtkTreeIter sel;
446         GtkTreeModel *model;
447
448         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
449                                 (GTK_TREE_VIEW(manager.certlist)),
450                                 &model, &sel))
451                 return;
452         
453         gtk_tree_model_get(model, &sel,
454                            SSL_MANAGER_CERT, &cert,
455                            -1);
456         if (!cert)
457                 return;
458
459         sslcertwindow_show_cert(cert);
460 }
461 #endif