c80d80350fbf497b602ff1264a652bccf0f4b136
[claws.git] / src / ssl_manager.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  * This file Copyright (C) 2002-2005 Colin Leroy <colin@colino.net>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #ifdef USE_OPENSSL
25 #include <gtk/gtkwidget.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 "gtksctree.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.enable_rules_hint);
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 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
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, *previous_pos = NULL;
215         int previous_dot_pos;
216
217         first_pos = tmp;
218         while ((tmp = strstr(tmp,".")) != NULL) {
219                 *tmp++;
220                 previous_pos = last_pos;
221                 last_pos = tmp;
222         }
223         previous_dot_pos = (previous_pos - first_pos);
224         if (previous_dot_pos - 1 > 0)
225                 ret = g_strndup(first_pos, previous_dot_pos - 1);
226         else 
227                 ret = g_strdup(first_pos);
228         g_free(first_pos);
229         return ret;
230 }
231
232 static char *get_port(char *str)
233 {
234         char *ret = NULL, *tmp = g_strdup(str);
235         char *previous_pos = NULL, *last_pos = NULL;
236
237         while ((tmp = strstr(tmp,".")) != NULL) {
238                 *tmp++;
239                 previous_pos = last_pos;
240                 last_pos = tmp;
241         }
242         if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
243                 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
244         else
245                 ret = g_strdup("0");
246         g_free(tmp);
247         return ret;
248         
249 }
250
251 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
252                                                   GtkTreeIter *row_iter,
253                                                   gchar *host, 
254                                                   gchar *port,
255                                                   SSLCertificate *cert) 
256 {
257         GtkTreeIter iter;
258         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
259                                         (GTK_TREE_VIEW(list_view)));
260
261         if (row_iter == NULL) {
262                 /* append new */
263                 gtk_list_store_append(list_store, &iter);
264                 gtk_list_store_set(list_store, &iter,
265                                    SSL_MANAGER_HOST, host,
266                                    SSL_MANAGER_PORT, port,
267                                    SSL_MANAGER_CERT, cert,
268                                    -1);
269         } else {
270                 gtk_list_store_set(list_store, row_iter,
271                                    SSL_MANAGER_HOST, host,
272                                    SSL_MANAGER_PORT, port,
273                                    SSL_MANAGER_CERT, cert,
274                                    -1);
275         }
276 }
277
278 static void ssl_manager_load_certs (void) 
279 {
280         DIR *dir;
281         struct dirent *d;
282         gchar *path;
283         int row = 0;
284         GtkListStore *store;
285
286         store = GTK_LIST_STORE(gtk_tree_view_get_model
287                                 (GTK_TREE_VIEW(manager.certlist)));
288
289         gtk_list_store_clear(store);
290
291         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
292                           "certs", G_DIR_SEPARATOR_S, NULL);
293
294         if((dir = opendir(path)) == NULL) {
295                 perror("opendir");
296                 return;
297         }
298         
299         while ((d = readdir(dir)) != NULL) {
300                 gchar *server, *port;
301                 SSLCertificate *cert;
302
303                 if(!strstr(d->d_name, ".cert")) 
304                         continue;
305
306                 server = get_server(d->d_name);
307                 port = get_port(d->d_name);
308                 
309                 
310                 cert = ssl_certificate_find_lookup(server, atoi(port), FALSE);
311                 
312                 ssl_manager_list_view_insert_cert(manager.certlist, NULL, 
313                                                   server, port, cert);
314                 
315                 g_free(server);
316                 g_free(port);
317                 row++;
318         }
319         closedir(dir);
320         g_free(path);
321 }
322
323 void ssl_manager_close(void) 
324 {
325         gtk_widget_hide(manager.window);
326 }
327
328 static void ssl_manager_close_cb(GtkWidget *widget,
329                                  gpointer data) 
330 {
331         ssl_manager_close();
332 }
333
334 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
335 {
336         if (event && event->keyval == GDK_Escape)
337                 ssl_manager_close();
338         return FALSE;
339 }
340
341 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
342                                         GtkTreePath             *path,
343                                         GtkTreeViewColumn       *column,
344                                         gpointer                 data)
345 {
346         SSLCertificate *cert;
347         GtkTreeIter iter;
348         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
349
350         if (!gtk_tree_model_get_iter(model, &iter, path))
351                 return;
352
353         gtk_tree_model_get(model, &iter, 
354                            SSL_MANAGER_CERT, &cert,
355                            -1);
356
357         if (!cert)
358                 return;
359
360         sslcertwindow_show_cert(cert);
361
362         return;
363 }
364
365
366
367 static void ssl_manager_delete_cb(GtkWidget *widget, 
368                               gpointer data) 
369 {
370         SSLCertificate *cert;
371         int val;
372         GtkTreeIter sel;
373         GtkTreeModel *model;
374
375         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
376                                 (GTK_TREE_VIEW(manager.certlist)),
377                                 &model, &sel))
378                 return;
379         
380         gtk_tree_model_get(model, &sel,
381                            SSL_MANAGER_CERT, &cert,
382                            -1);
383         if (!cert)
384                 return;
385
386         val = alertpanel_full(_("Delete certificate"),
387                               _("Do you really want to delete this certificate?"),
388                               GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
389                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
390
391                              
392         if (val != G_ALERTALTERNATE)
393                 return;
394         
395         ssl_certificate_delete_from_disk(cert);
396         ssl_certificate_destroy(cert);
397         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
398 }
399
400 static void ssl_manager_view_cb(GtkWidget *widget, 
401                                 gpointer data) 
402 {
403         SSLCertificate *cert;
404         GtkTreeIter sel;
405         GtkTreeModel *model;
406
407         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
408                                 (GTK_TREE_VIEW(manager.certlist)),
409                                 &model, &sel))
410                 return;
411         
412         gtk_tree_model_get(model, &sel,
413                            SSL_MANAGER_CERT, &cert,
414                            -1);
415         if (!cert)
416                 return;
417
418         sslcertwindow_show_cert(cert);
419 }
420 #endif