0735b2dbbe50598ffc9b05a83c41d8ffdcf1d21e
[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         SSL_MANAGER_STATUS,
45         SSL_MANAGER_EXPIRY,
46         SSL_MANAGER_FONT_WEIGHT,
47         N_SSL_MANAGER_COLUMNS
48 };
49
50
51 static struct SSLManager
52 {
53         GtkWidget *window;
54         GtkWidget *hbox1;
55         GtkWidget *vbox1;
56         GtkWidget *certlist;
57         GtkWidget *view_btn;
58         GtkWidget *delete_btn;
59         GtkWidget *close_btn;
60 } manager;
61
62 static void ssl_manager_view_cb         (GtkWidget *widget, gpointer data);
63 static void ssl_manager_delete_cb       (GtkWidget *widget, gpointer data);
64 static void ssl_manager_close_cb        (GtkWidget *widget, gpointer data);
65 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
66                                          gpointer data);
67 static void ssl_manager_load_certs      (void);
68 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
69                                         GtkTreePath             *path,
70                                         GtkTreeViewColumn       *column,
71                                         gpointer                 data);
72
73 void ssl_manager_open(MainWindow *mainwin)
74 {
75         if (!manager.window)
76                 ssl_manager_create();
77
78         manage_window_set_transient(GTK_WINDOW(manager.window));
79         gtk_widget_grab_focus(manager.close_btn);
80
81         ssl_manager_load_certs();
82
83         gtk_widget_show(manager.window);
84
85 }
86
87 static GtkListStore* ssl_manager_create_data_store(void)
88 {
89         return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
90                                   G_TYPE_STRING,
91                                   G_TYPE_STRING,
92                                   G_TYPE_POINTER,
93                                   G_TYPE_STRING,
94                                   G_TYPE_STRING,
95                                   G_TYPE_INT,
96                                   -1);
97 }
98
99 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
100 {
101         GtkTreeViewColumn *column;
102         GtkCellRenderer *renderer;
103
104         renderer = gtk_cell_renderer_text_new();
105         g_object_set(renderer, "weight", PANGO_WEIGHT_NORMAL,
106                      "weight-set", TRUE, NULL);
107
108         column = gtk_tree_view_column_new_with_attributes
109                 (_("Server"),
110                  renderer,
111                  "text", SSL_MANAGER_HOST,
112                  "weight", SSL_MANAGER_FONT_WEIGHT,
113                  NULL);
114         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
115
116         column = gtk_tree_view_column_new_with_attributes
117                 (_("Port"),
118                  renderer,
119                  "text", SSL_MANAGER_PORT,
120                  NULL);
121         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
122
123         column = gtk_tree_view_column_new_with_attributes
124                 (_("Status"),
125                  renderer,
126                  "text", SSL_MANAGER_STATUS,
127                  NULL);
128         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
129
130         column = gtk_tree_view_column_new_with_attributes
131                 (_("Expiry"),
132                  renderer,
133                  "text", SSL_MANAGER_EXPIRY,
134                  NULL);
135         gtk_tree_view_column_set_attributes
136                 (column, renderer,
137                  "text", SSL_MANAGER_EXPIRY,
138                  NULL);
139         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
140 }
141
142 static GtkWidget *ssl_manager_list_view_create  (void)
143 {
144         GtkTreeView *list_view;
145         GtkTreeSelection *selector;
146         GtkTreeModel *model;
147
148         model = GTK_TREE_MODEL(ssl_manager_create_data_store());
149         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
150         g_object_unref(model);  
151         
152         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
153                                              0, GTK_SORT_ASCENDING);
154         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
155         
156         selector = gtk_tree_view_get_selection(list_view);
157         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
158
159         g_signal_connect(G_OBJECT(list_view), "row_activated",
160                          G_CALLBACK(ssl_manager_double_clicked),
161                          list_view);
162
163         /* create the columns */
164         ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
165
166         return GTK_WIDGET(list_view);
167 }
168
169 /*!
170  *\brief        Save Gtk object size to prefs dataset
171  */
172 static void ssl_manager_size_allocate_cb(GtkWidget *widget,
173                                          GtkAllocation *allocation)
174 {
175         cm_return_if_fail(allocation != NULL);
176
177         prefs_common.sslmanwin_width = allocation->width;
178         prefs_common.sslmanwin_height = allocation->height;
179 }
180
181 void ssl_manager_create(void)
182 {
183         GtkWidget *window;
184         GtkWidget *scroll;
185         GtkWidget *hbox1;
186         GtkWidget *vbox1;
187         GtkWidget *certlist;
188         GtkWidget *view_btn;
189         GtkWidget *delete_btn;
190         GtkWidget *close_btn;
191         static GdkGeometry geometry;
192
193         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "ssl_manager");
194         gtk_window_set_title (GTK_WINDOW(window),
195                               _("Saved SSL/TLS certificates"));
196
197         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
198         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
199         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
200         g_signal_connect(G_OBJECT(window), "delete_event",
201                          G_CALLBACK(ssl_manager_close_cb), NULL);
202         g_signal_connect(G_OBJECT(window), "size_allocate",
203                          G_CALLBACK(ssl_manager_size_allocate_cb), NULL);
204         g_signal_connect(G_OBJECT(window), "key_press_event",
205                          G_CALLBACK(key_pressed), NULL);
206         MANAGE_WINDOW_SIGNALS_CONNECT (window);
207
208         hbox1 = gtk_hbox_new(FALSE, 6);
209         vbox1 = gtk_vbox_new(FALSE, 0);
210         delete_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
211
212         g_signal_connect(G_OBJECT(delete_btn), "clicked",
213                          G_CALLBACK(ssl_manager_delete_cb), NULL);
214
215         view_btn = gtk_button_new_from_stock(GTK_STOCK_PROPERTIES);
216         g_signal_connect(G_OBJECT(view_btn), "clicked",
217                          G_CALLBACK(ssl_manager_view_cb), NULL);
218
219         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
220         g_signal_connect(G_OBJECT(close_btn), "clicked",
221                          G_CALLBACK(ssl_manager_close_cb), NULL);
222
223         certlist = ssl_manager_list_view_create();
224
225         scroll = gtk_scrolled_window_new (NULL, NULL);
226         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
227                                         GTK_POLICY_NEVER,
228                                         GTK_POLICY_AUTOMATIC);
229
230         gtk_container_add(GTK_CONTAINER (scroll), certlist);
231
232         gtk_box_pack_start(GTK_BOX(hbox1), scroll, TRUE, TRUE, 0);
233         gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, FALSE, 0);
234         gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 4);
235         gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 4);
236         gtk_box_pack_end(GTK_BOX(vbox1), close_btn, FALSE, FALSE, 4);
237
238         if (!geometry.min_height) {
239                 geometry.min_width = 700;
240                 geometry.min_height = 250;
241         }
242
243         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
244                                       GDK_HINT_MIN_SIZE);
245         gtk_widget_set_size_request(window, prefs_common.sslmanwin_width,
246                                     prefs_common.sslmanwin_height);
247
248         gtk_widget_show(certlist);
249         gtk_widget_show(scroll);
250         gtk_widget_show(hbox1);
251         gtk_widget_show(vbox1);
252         gtk_widget_show(close_btn);
253         gtk_widget_show(delete_btn);
254         gtk_widget_show(view_btn);
255         gtk_container_add(GTK_CONTAINER (window), hbox1);
256
257         manager.window = window;
258         manager.hbox1 = hbox1;
259         manager.vbox1 = vbox1;
260         manager.certlist = certlist;
261         manager.view_btn = view_btn;
262         manager.delete_btn = delete_btn;
263         manager.close_btn = close_btn;
264
265         gtk_widget_show(window);
266 }
267
268 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
269                                                   GtkTreeIter *row_iter,
270                                                   gchar *host, 
271                                                   gchar *port,
272                                                   SSLCertificate *cert) 
273 {
274         char *sig_status, *exp_date;
275         char buf[100];
276         time_t exp_time_t;
277         struct tm lt;
278         PangoWeight weight = PANGO_WEIGHT_NORMAL;
279         GtkTreeIter iter, *iterptr;
280         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
281                                         (GTK_TREE_VIEW(list_view)));
282
283         g_return_if_fail(cert != NULL);
284
285         exp_time_t = gnutls_x509_crt_get_expiration_time(cert->x509_cert);
286
287         memset(buf, 0, sizeof(buf));
288         if (exp_time_t > 0) {
289                 fast_strftime(buf, sizeof(buf)-1, prefs_common.date_format, localtime_r(&exp_time_t, &lt));
290                 exp_date = (*buf) ? g_strdup(buf):g_strdup("?");
291         } else
292                 exp_date = g_strdup("");
293
294         if (exp_time_t < time(NULL))
295                 weight = PANGO_WEIGHT_BOLD;
296
297         sig_status = ssl_certificate_check_signer(cert, cert->status);
298
299         if (sig_status == NULL)
300                 sig_status = g_strdup_printf(_("Correct%s"),exp_time_t < time(NULL)? _(" (expired)"): "");
301         else {
302                  weight = PANGO_WEIGHT_BOLD;
303                  if (exp_time_t < time(NULL))
304                           sig_status = g_strconcat(sig_status,_(" (expired)"),NULL);
305         }
306
307         if (row_iter == NULL) {
308                 /* append new */
309                 gtk_list_store_append(list_store, &iter);
310                 iterptr = &iter;
311         } else
312                 iterptr = row_iter;
313
314         gtk_list_store_set(list_store, iterptr,
315                            SSL_MANAGER_HOST, host,
316                            SSL_MANAGER_PORT, port,
317                            SSL_MANAGER_CERT, cert,
318                            SSL_MANAGER_STATUS, sig_status,
319                            SSL_MANAGER_EXPIRY, exp_date,
320                            SSL_MANAGER_FONT_WEIGHT, weight,
321                            -1);
322
323         g_free(sig_status);
324         g_free(exp_date);
325 }
326
327 static void ssl_manager_load_certs (void) 
328 {
329         GDir *dir;
330         const gchar *d;
331         GError *error = NULL;
332         gchar *path;
333         int row = 0;
334         GtkListStore *store;
335
336         store = GTK_LIST_STORE(gtk_tree_view_get_model
337                                 (GTK_TREE_VIEW(manager.certlist)));
338
339         gtk_list_store_clear(store);
340
341         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
342                           "certs", G_DIR_SEPARATOR_S, NULL);
343
344         if((dir = g_dir_open(path, 0, &error)) == NULL) {
345                 debug_print("couldn't open dir '%s': %s (%d)\n", path,
346                                 error->message, error->code);
347                 g_error_free(error);
348                 return;
349         }
350         
351         while ((d = g_dir_read_name(dir)) != NULL) {
352                 gchar *server = NULL, *port = NULL, *fp = NULL;
353                 SSLCertificate *cert;
354
355                 if(strstr(d, ".cert") != d + (strlen(d) - strlen(".cert"))) 
356                         continue;
357
358                 if (get_serverportfp_from_filename(d, &server, &port, &fp)) {
359
360                         if (server != NULL && port != NULL) {
361                                 gint portnum = atoi(port);
362                                 if (portnum > 0 && portnum <= 65535) {
363                                         cert = ssl_certificate_find(server, portnum, fp);
364                                         ssl_manager_list_view_insert_cert(manager.certlist, NULL,
365                                                         server, port, cert);
366                                 }
367                         }
368                 
369                         g_free(server);
370                         g_free(port);
371                         g_free(fp);
372                 }
373                 row++;
374         }
375         g_dir_close(dir);
376         g_free(path);
377 }
378
379 static void ssl_manager_close(void) 
380 {
381         gtk_widget_hide(manager.window);
382 }
383
384 static void ssl_manager_close_cb(GtkWidget *widget,
385                                  gpointer data) 
386 {
387         ssl_manager_close();
388 }
389
390 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
391 {
392         if (!event)
393                 return FALSE;
394
395         if (event->keyval == GDK_KEY_Escape)
396                 ssl_manager_close();
397         else if (event->keyval == GDK_KEY_Delete)
398                 ssl_manager_delete_cb(manager.delete_btn, NULL);
399
400         return FALSE;
401 }
402
403 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
404                                         GtkTreePath             *path,
405                                         GtkTreeViewColumn       *column,
406                                         gpointer                 data)
407 {
408         SSLCertificate *cert;
409         GtkTreeIter iter;
410         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
411
412         if (!gtk_tree_model_get_iter(model, &iter, path))
413                 return;
414
415         gtk_tree_model_get(model, &iter, 
416                            SSL_MANAGER_CERT, &cert,
417                            -1);
418
419         if (!cert)
420                 return;
421
422         sslcertwindow_show_cert(cert);
423
424         return;
425 }
426
427
428
429 static void ssl_manager_delete_cb(GtkWidget *widget, 
430                               gpointer data) 
431 {
432         SSLCertificate *cert;
433         int val;
434         GtkTreeIter iter;
435         GtkTreeModel *model;
436
437         cert = gtkut_tree_view_get_selected_pointer(
438                         GTK_TREE_VIEW(manager.certlist), SSL_MANAGER_CERT,
439                         &model, NULL, &iter);
440
441         if (!cert)
442                 return;
443
444         val = alertpanel_full(_("Delete certificate"),
445                               _("Do you really want to delete this certificate?"),
446                               GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST,
447                                                 FALSE, NULL, ALERT_WARNING);
448
449                              
450         if (val != G_ALERTALTERNATE)
451                 return;
452         
453         ssl_certificate_delete_from_disk(cert);
454         ssl_certificate_destroy(cert);
455         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
456 }
457
458 static void ssl_manager_view_cb(GtkWidget *widget, 
459                                 gpointer data) 
460 {
461         SSLCertificate *cert;
462
463         cert = gtkut_tree_view_get_selected_pointer(
464                         GTK_TREE_VIEW(manager.certlist), SSL_MANAGER_CERT,
465                         NULL, NULL, NULL);
466
467         if (!cert)
468                 return;
469
470         sslcertwindow_show_cert(cert);
471 }
472 #endif