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