keypress fixes
[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 static struct SSLManager
40 {
41         GtkWidget *window;
42         GtkWidget *hbox1;
43         GtkWidget *vbox1;
44         GtkWidget *certlist;
45         GtkWidget *view_btn;
46         GtkWidget *delete_btn;
47         GtkWidget *ok_btn;
48 } manager;
49
50 static void ssl_manager_view_cb         (GtkWidget *widget, gpointer data);
51 static void ssl_manager_delete_cb       (GtkWidget *widget, gpointer data);
52 static void ssl_manager_ok_cb           (GtkWidget *widget, gpointer data);
53 static void ssl_manager_load_certs      (void);
54
55 void ssl_manager_open(MainWindow *mainwin)
56 {
57         if (!manager.window)
58                 ssl_manager_create();
59
60         manage_window_set_transient(GTK_WINDOW(manager.window));
61         gtk_widget_grab_focus(manager.ok_btn);
62
63         ssl_manager_load_certs();
64
65         gtk_widget_show(manager.window);
66
67 }
68
69 void ssl_manager_create(void) 
70 {
71         GtkWidget *window;
72         GtkWidget *hbox1;
73         GtkWidget *vbox1;
74         GtkWidget *certlist;
75         GtkWidget *view_btn;
76         GtkWidget *delete_btn;
77         GtkWidget *ok_btn;
78         gchar *titles[2];
79
80         window = gtk_window_new (GTK_WINDOW_DIALOG);
81         gtk_window_set_title (GTK_WINDOW(window),
82                               _("Saved SSL Certificates"));
83         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
84         gtk_window_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
85         gtk_window_set_policy (GTK_WINDOW (window), FALSE, TRUE, FALSE);
86         gtk_signal_connect (GTK_OBJECT(window), "delete_event",
87                             GTK_SIGNAL_FUNC(ssl_manager_ok_cb), NULL);
88         MANAGE_WINDOW_SIGNALS_CONNECT (window);
89         
90         hbox1 = gtk_hbox_new(FALSE,2);
91         vbox1 = gtk_vbox_new(FALSE,0);
92         delete_btn = gtk_button_new_with_label(_("Delete"));
93         gtk_signal_connect (GTK_OBJECT(delete_btn), "clicked",
94                             GTK_SIGNAL_FUNC(ssl_manager_delete_cb), NULL);
95         view_btn = gtk_button_new_with_label(_("View"));
96         gtk_signal_connect (GTK_OBJECT(view_btn), "clicked",
97                             GTK_SIGNAL_FUNC(ssl_manager_view_cb), NULL);
98         ok_btn = gtk_button_new_with_label(_("OK"));
99         gtk_signal_connect (GTK_OBJECT(ok_btn), "clicked",
100                             GTK_SIGNAL_FUNC(ssl_manager_ok_cb), NULL);
101         gtk_widget_set_usize(ok_btn, 80, -1);
102         gtk_widget_set_usize(delete_btn, 80, -1);
103         gtk_widget_set_usize(view_btn, 80, -1);
104
105         titles[0] = _("Server");
106         titles[1] = _("Port");
107         certlist = gtk_sctree_new_with_titles(2, 3, titles);
108         gtk_clist_column_titles_show(GTK_CLIST(certlist));
109         gtk_clist_set_column_width(GTK_CLIST(certlist), 0, 220);
110         gtk_clist_set_selection_mode(GTK_CLIST(certlist), GTK_SELECTION_SINGLE);
111         gtk_widget_set_usize(certlist, 300, 200);
112         gtk_signal_connect (GTK_OBJECT(certlist), "open_row",
113                             GTK_SIGNAL_FUNC(ssl_manager_view_cb), NULL);
114         gtk_box_pack_start(GTK_BOX(hbox1), certlist, TRUE, TRUE, 0);
115         gtk_box_pack_start(GTK_BOX(hbox1), vbox1, TRUE, TRUE, 0);
116         gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 0);
117         gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 0);
118         gtk_box_pack_end(GTK_BOX(vbox1), ok_btn, FALSE, FALSE, 0);
119         
120         gtk_widget_show(certlist);
121         gtk_widget_show(hbox1);
122         gtk_widget_show(vbox1);
123         gtk_widget_show(ok_btn);
124         gtk_widget_show(delete_btn);
125         gtk_widget_show(view_btn);
126         gtk_container_add(GTK_CONTAINER (window), hbox1);
127
128         manager.window = window;
129         manager.hbox1 = hbox1;
130         manager.vbox1 = vbox1;
131         manager.certlist = certlist;
132         manager.view_btn = view_btn;
133         manager.delete_btn = delete_btn;
134         manager.ok_btn = ok_btn;
135
136         gtk_widget_show(window);
137                 
138 }
139
140 static char *get_server(char *str)
141 {
142         char *ret = NULL, *tmp = g_strdup(str);
143         char *first_pos = NULL, *last_pos = NULL, *previous_pos = NULL;
144         int previous_dot_pos;
145
146         first_pos = tmp;
147         while ((tmp = strstr(tmp,".")) != NULL) {
148                 *tmp++;
149                 previous_pos = last_pos;
150                 last_pos = tmp;
151         }
152         previous_dot_pos = (previous_pos - first_pos);
153         if (previous_dot_pos - 1 > 0)
154                 ret = g_strndup(first_pos, previous_dot_pos - 1);
155         else 
156                 ret = g_strdup(first_pos);
157         g_free(first_pos);
158         return ret;
159 }
160
161 static char *get_port(char *str)
162 {
163         char *ret = NULL, *tmp = g_strdup(str);
164         char *previous_pos = NULL, *last_pos = NULL;
165
166         while ((tmp = strstr(tmp,".")) != NULL) {
167                 *tmp++;
168                 previous_pos = last_pos;
169                 last_pos = tmp;
170         }
171         if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
172                 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
173         else
174                 ret = g_strdup("0");
175         g_free(tmp);
176         return ret;
177         
178 }
179 static void ssl_manager_load_certs (void) 
180 {
181         DIR *dir;
182         struct dirent *d;
183         gchar *path;
184         int row = 0;
185
186         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
187                           "certs", G_DIR_SEPARATOR_S, NULL);
188
189         gtk_clist_clear(GTK_CLIST(manager.certlist));
190
191         if((dir = opendir(path)) == NULL) {
192                 perror("opendir");
193                 return;
194         }
195         
196         while ((d = readdir(dir)) != NULL) {
197                 gchar *server, *port, *text[2];
198                 SSLCertificate *cert;
199
200                 if(!strstr(d->d_name, ".cert")) 
201                         continue;
202
203                 server = get_server(d->d_name);
204                 port = get_port(d->d_name);
205                 
206                 text[0] = g_strdup(server);
207                 text[1] = g_strdup(port);
208                 gtk_clist_append(GTK_CLIST(manager.certlist), text);
209                 cert = ssl_certificate_find_lookup(server, atoi(port), FALSE);
210                 gtk_clist_set_row_data(GTK_CLIST(manager.certlist), row, cert);
211                 g_free(server);
212                 g_free(port);
213                 g_free(text[0]);
214                 g_free(text[1]);
215                 row++;
216         }
217         closedir(dir);
218         g_free(path);
219 }
220
221 void ssl_manager_close(void) 
222 {
223         gtk_widget_hide(manager.window);
224 }
225
226 static void ssl_manager_ok_cb(GtkWidget *widget, 
227                               gpointer data) 
228 {
229         ssl_manager_close();
230 }
231 static void ssl_manager_view_cb(GtkWidget *widget, 
232                               gpointer data) 
233 {
234         SSLCertificate *cert;
235         GList *rowlist;
236         
237         rowlist = GTK_CLIST(manager.certlist)->selection;
238         if (!rowlist) 
239                 return;
240         
241         cert = gtk_ctree_node_get_row_data
242                         (GTK_CTREE(manager.certlist),
243                          GTK_CTREE_NODE(rowlist->data));
244         
245         if (!cert)
246                 return;
247
248         sslcertwindow_show_cert(cert);
249         
250         
251 }
252 static void ssl_manager_delete_cb(GtkWidget *widget, 
253                               gpointer data) 
254 {
255         SSLCertificate *cert;
256         GList *rowlist;
257         int val;
258         
259         rowlist = GTK_CLIST(manager.certlist)->selection;
260         if (!rowlist) 
261                 return;
262         
263         cert = gtk_ctree_node_get_row_data
264                         (GTK_CTREE(manager.certlist),
265                          GTK_CTREE_NODE(rowlist->data));
266         
267         if (!cert)
268                 return;
269         val = alertpanel(_("Delete certificate"), 
270                              _("Do you really want to delete this certificate?"),
271                              _("Yes"), _("+No"), NULL);
272         if (val != G_ALERTDEFAULT)
273                 return;
274         
275         ssl_certificate_delete_from_disk(cert);
276         ssl_certificate_destroy(cert);
277         gtk_ctree_remove_node(GTK_CTREE(manager.certlist), GTK_CTREE_NODE(rowlist->data));
278 }
279 #endif