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