b7143edd1e99fa4842a624e4b55cae314bf10520
[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 <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 "gtksctree.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 void ssl_manager_load_certs      (void);
64 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
65                                         GtkTreePath             *path,
66                                         GtkTreeViewColumn       *column,
67                                         gpointer                 data);
68
69 void ssl_manager_open(MainWindow *mainwin)
70 {
71         if (!manager.window)
72                 ssl_manager_create();
73
74         manage_window_set_transient(GTK_WINDOW(manager.window));
75         gtk_widget_grab_focus(manager.close_btn);
76
77         ssl_manager_load_certs();
78
79         gtk_widget_show(manager.window);
80
81 }
82
83 static GtkListStore* ssl_manager_create_data_store(void)
84 {
85         return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
86                                   G_TYPE_STRING,
87                                   G_TYPE_STRING,
88                                   G_TYPE_POINTER,
89                                   -1);
90 }
91
92 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
93 {
94         GtkTreeViewColumn *column;
95         GtkCellRenderer *renderer;
96
97         renderer = gtk_cell_renderer_text_new();
98         column = gtk_tree_view_column_new_with_attributes
99                 (_("Server"),
100                  renderer,
101                  "text", SSL_MANAGER_HOST,
102                  NULL);
103         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
104
105         renderer = gtk_cell_renderer_text_new();
106         column = gtk_tree_view_column_new_with_attributes
107                 (_("Port"),
108                  renderer,
109                  "text", SSL_MANAGER_PORT,
110                  NULL);
111         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
112 }
113
114 static GtkWidget *ssl_manager_list_view_create  (void)
115 {
116         GtkTreeView *list_view;
117         GtkTreeSelection *selector;
118         GtkTreeModel *model;
119
120         model = GTK_TREE_MODEL(ssl_manager_create_data_store());
121         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
122         g_object_unref(model);  
123         
124         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
125                                              0, GTK_SORT_ASCENDING);
126         gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
127         
128         selector = gtk_tree_view_get_selection(list_view);
129         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
130
131         g_signal_connect(G_OBJECT(list_view), "row_activated",
132                          G_CALLBACK(ssl_manager_double_clicked),
133                          list_view);
134
135         /* create the columns */
136         ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
137
138         return GTK_WIDGET(list_view);
139
140 }
141
142 void ssl_manager_create(void) 
143 {
144         GtkWidget *window;
145         GtkWidget *hbox1;
146         GtkWidget *vbox1;
147         GtkWidget *certlist;
148         GtkWidget *view_btn;
149         GtkWidget *delete_btn;
150         GtkWidget *close_btn;
151
152         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
153         gtk_window_set_title (GTK_WINDOW(window),
154                               _("Saved SSL Certificates"));
155
156         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
157         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
158         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
159         g_signal_connect(G_OBJECT(window), "delete_event",
160                          G_CALLBACK(ssl_manager_close_cb), NULL);
161         MANAGE_WINDOW_SIGNALS_CONNECT (window);
162
163         hbox1 = gtk_hbox_new(FALSE, 6);
164         vbox1 = gtk_vbox_new(FALSE, 0);
165         delete_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
166         
167         g_signal_connect(G_OBJECT(delete_btn), "clicked",
168                          G_CALLBACK(ssl_manager_delete_cb), NULL);
169
170         view_btn = gtk_button_new_from_stock(GTK_STOCK_PROPERTIES);
171         g_signal_connect(G_OBJECT(view_btn), "clicked",
172                          G_CALLBACK(ssl_manager_view_cb), NULL);
173
174         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
175         g_signal_connect(G_OBJECT(close_btn), "clicked",
176                          G_CALLBACK(ssl_manager_close_cb), NULL);
177
178         certlist = ssl_manager_list_view_create();
179         
180         gtk_box_pack_start(GTK_BOX(hbox1), certlist, TRUE, TRUE, 0);
181         gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, FALSE, 0);
182         gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 4);
183         gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 4);
184         gtk_box_pack_end(GTK_BOX(vbox1), close_btn, FALSE, FALSE, 4);
185         
186         gtk_widget_show(certlist);
187         gtk_widget_show(hbox1);
188         gtk_widget_show(vbox1);
189         gtk_widget_show(close_btn);
190         gtk_widget_show(delete_btn);
191         gtk_widget_show(view_btn);
192         gtk_container_add(GTK_CONTAINER (window), hbox1);
193
194         manager.window = window;
195         manager.hbox1 = hbox1;
196         manager.vbox1 = vbox1;
197         manager.certlist = certlist;
198         manager.view_btn = view_btn;
199         manager.delete_btn = delete_btn;
200         manager.close_btn = close_btn;
201
202         gtk_widget_show(window);
203                 
204 }
205
206 static char *get_server(char *str)
207 {
208         char *ret = NULL, *tmp = g_strdup(str);
209         char *first_pos = NULL, *last_pos = NULL, *previous_pos = NULL;
210         int previous_dot_pos;
211
212         first_pos = tmp;
213         while ((tmp = strstr(tmp,".")) != NULL) {
214                 *tmp++;
215                 previous_pos = last_pos;
216                 last_pos = tmp;
217         }
218         previous_dot_pos = (previous_pos - first_pos);
219         if (previous_dot_pos - 1 > 0)
220                 ret = g_strndup(first_pos, previous_dot_pos - 1);
221         else 
222                 ret = g_strdup(first_pos);
223         g_free(first_pos);
224         return ret;
225 }
226
227 static char *get_port(char *str)
228 {
229         char *ret = NULL, *tmp = g_strdup(str);
230         char *previous_pos = NULL, *last_pos = NULL;
231
232         while ((tmp = strstr(tmp,".")) != NULL) {
233                 *tmp++;
234                 previous_pos = last_pos;
235                 last_pos = tmp;
236         }
237         if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
238                 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
239         else
240                 ret = g_strdup("0");
241         g_free(tmp);
242         return ret;
243         
244 }
245
246 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
247                                                   GtkTreeIter *row_iter,
248                                                   gchar *host, 
249                                                   gchar *port,
250                                                   SSLCertificate *cert) 
251 {
252         GtkTreeIter iter;
253         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
254                                         (GTK_TREE_VIEW(list_view)));
255
256         if (row_iter == NULL) {
257                 /* append new */
258                 gtk_list_store_append(list_store, &iter);
259                 gtk_list_store_set(list_store, &iter,
260                                    SSL_MANAGER_HOST, host,
261                                    SSL_MANAGER_PORT, port,
262                                    SSL_MANAGER_CERT, cert,
263                                    -1);
264         } else {
265                 gtk_list_store_set(list_store, row_iter,
266                                    SSL_MANAGER_HOST, host,
267                                    SSL_MANAGER_PORT, port,
268                                    SSL_MANAGER_CERT, cert,
269                                    -1);
270         }
271 }
272
273 static void ssl_manager_load_certs (void) 
274 {
275         DIR *dir;
276         struct dirent *d;
277         gchar *path;
278         int row = 0;
279         GtkListStore *store;
280
281         store = GTK_LIST_STORE(gtk_tree_view_get_model
282                                 (GTK_TREE_VIEW(manager.certlist)));
283
284         gtk_list_store_clear(store);
285
286         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
287                           "certs", G_DIR_SEPARATOR_S, NULL);
288
289         if((dir = opendir(path)) == NULL) {
290                 perror("opendir");
291                 return;
292         }
293         
294         while ((d = readdir(dir)) != NULL) {
295                 gchar *server, *port;
296                 SSLCertificate *cert;
297
298                 if(!strstr(d->d_name, ".cert")) 
299                         continue;
300
301                 server = get_server(d->d_name);
302                 port = get_port(d->d_name);
303                 
304                 
305                 cert = ssl_certificate_find_lookup(server, atoi(port), FALSE);
306                 
307                 ssl_manager_list_view_insert_cert(manager.certlist, NULL, 
308                                                   server, port, cert);
309                 
310                 g_free(server);
311                 g_free(port);
312                 row++;
313         }
314         closedir(dir);
315         g_free(path);
316 }
317
318 void ssl_manager_close(void) 
319 {
320         gtk_widget_hide(manager.window);
321 }
322
323 static void ssl_manager_close_cb(GtkWidget *widget,
324                                  gpointer data) 
325 {
326         ssl_manager_close();
327 }
328
329 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
330                                         GtkTreePath             *path,
331                                         GtkTreeViewColumn       *column,
332                                         gpointer                 data)
333 {
334         SSLCertificate *cert;
335         GtkTreeIter iter;
336         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
337
338         if (!gtk_tree_model_get_iter(model, &iter, path))
339                 return;
340
341         gtk_tree_model_get(model, &iter, 
342                            SSL_MANAGER_CERT, &cert,
343                            -1);
344
345         if (!cert)
346                 return;
347
348         sslcertwindow_show_cert(cert);
349
350         return;
351 }
352
353
354
355 static void ssl_manager_delete_cb(GtkWidget *widget, 
356                               gpointer data) 
357 {
358         SSLCertificate *cert;
359         int val;
360         GtkTreeIter sel;
361         GtkTreeModel *model;
362
363         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
364                                 (GTK_TREE_VIEW(manager.certlist)),
365                                 &model, &sel))
366                 return;
367         
368         gtk_tree_model_get(model, &sel,
369                            SSL_MANAGER_CERT, &cert,
370                            -1);
371         if (!cert)
372                 return;
373
374         val = alertpanel_full(_("Delete certificate"),
375                               _("Do you really want to delete this certificate?"),
376                               GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
377                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
378
379                              
380         if (val != G_ALERTALTERNATE)
381                 return;
382         
383         ssl_certificate_delete_from_disk(cert);
384         ssl_certificate_destroy(cert);
385         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
386 }
387
388 static void ssl_manager_view_cb(GtkWidget *widget, 
389                                 gpointer data) 
390 {
391         SSLCertificate *cert;
392         GtkTreeIter sel;
393         GtkTreeModel *model;
394
395         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
396                                 (GTK_TREE_VIEW(manager.certlist)),
397                                 &model, &sel))
398                 return;
399         
400         gtk_tree_model_get(model, &sel,
401                            SSL_MANAGER_CERT, &cert,
402                            -1);
403         if (!cert)
404                 return;
405
406         sslcertwindow_show_cert(cert);
407 }
408 #endif