Use g_strndup() instead of strndup() for better portability.
[claws.git] / src / ssl_manager.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Colin Leroy and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #include "claws-features.h"
21 #endif
22
23 #ifdef USE_GNUTLS
24 #include <gtk/gtk.h>
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gdk/gdkkeysyms.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 "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 *close_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_close_cb        (GtkWidget *widget, gpointer data);
62 static gboolean key_pressed             (GtkWidget *widget, GdkEventKey *event,
63                                          gpointer data);
64 static void ssl_manager_load_certs      (void);
65 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
66                                         GtkTreePath             *path,
67                                         GtkTreeViewColumn       *column,
68                                         gpointer                 data);
69
70 void ssl_manager_open(MainWindow *mainwin)
71 {
72         if (!manager.window)
73                 ssl_manager_create();
74
75         manage_window_set_transient(GTK_WINDOW(manager.window));
76         gtk_widget_grab_focus(manager.close_btn);
77
78         ssl_manager_load_certs();
79
80         gtk_widget_show(manager.window);
81
82 }
83
84 static GtkListStore* ssl_manager_create_data_store(void)
85 {
86         return gtk_list_store_new(N_SSL_MANAGER_COLUMNS,
87                                   G_TYPE_STRING,
88                                   G_TYPE_STRING,
89                                   G_TYPE_POINTER,
90                                   -1);
91 }
92
93 static void ssl_manager_create_list_view_columns(GtkWidget *list_view)
94 {
95         GtkTreeViewColumn *column;
96         GtkCellRenderer *renderer;
97
98         renderer = gtk_cell_renderer_text_new();
99         column = gtk_tree_view_column_new_with_attributes
100                 (_("Server"),
101                  renderer,
102                  "text", SSL_MANAGER_HOST,
103                  NULL);
104         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
105
106         renderer = gtk_cell_renderer_text_new();
107         column = gtk_tree_view_column_new_with_attributes
108                 (_("Port"),
109                  renderer,
110                  "text", SSL_MANAGER_PORT,
111                  NULL);
112         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);          
113 }
114
115 static GtkWidget *ssl_manager_list_view_create  (void)
116 {
117         GtkTreeView *list_view;
118         GtkTreeSelection *selector;
119         GtkTreeModel *model;
120
121         model = GTK_TREE_MODEL(ssl_manager_create_data_store());
122         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
123         g_object_unref(model);  
124         
125         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
126                                              0, GTK_SORT_ASCENDING);
127         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
128         
129         selector = gtk_tree_view_get_selection(list_view);
130         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
131
132         g_signal_connect(G_OBJECT(list_view), "row_activated",
133                          G_CALLBACK(ssl_manager_double_clicked),
134                          list_view);
135
136         /* create the columns */
137         ssl_manager_create_list_view_columns(GTK_WIDGET(list_view));
138
139         return GTK_WIDGET(list_view);
140
141 }
142
143 void ssl_manager_create(void)
144 {
145         GtkWidget *window;
146         GtkWidget *scroll;
147         GtkWidget *hbox1;
148         GtkWidget *vbox1;
149         GtkWidget *certlist;
150         GtkWidget *view_btn;
151         GtkWidget *delete_btn;
152         GtkWidget *close_btn;
153
154         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "ssl_manager");
155         gtk_window_set_title (GTK_WINDOW(window),
156                               _("Saved SSL/TLS certificates"));
157
158         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
159         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
160         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
161         g_signal_connect(G_OBJECT(window), "delete_event",
162                          G_CALLBACK(ssl_manager_close_cb), NULL);
163         g_signal_connect(G_OBJECT(window), "key_press_event",
164                          G_CALLBACK(key_pressed), NULL);
165         MANAGE_WINDOW_SIGNALS_CONNECT (window);
166
167         hbox1 = gtk_hbox_new(FALSE, 6);
168         vbox1 = gtk_vbox_new(FALSE, 0);
169         delete_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
170
171         g_signal_connect(G_OBJECT(delete_btn), "clicked",
172                          G_CALLBACK(ssl_manager_delete_cb), NULL);
173
174         view_btn = gtk_button_new_from_stock(GTK_STOCK_PROPERTIES);
175         g_signal_connect(G_OBJECT(view_btn), "clicked",
176                          G_CALLBACK(ssl_manager_view_cb), NULL);
177
178         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
179         g_signal_connect(G_OBJECT(close_btn), "clicked",
180                          G_CALLBACK(ssl_manager_close_cb), NULL);
181
182         certlist = ssl_manager_list_view_create();
183
184         scroll = gtk_scrolled_window_new (NULL, NULL);
185         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
186                                         GTK_POLICY_NEVER,
187                                         GTK_POLICY_AUTOMATIC);
188         gtk_container_add(GTK_CONTAINER (scroll), certlist);
189
190         gtk_box_pack_start(GTK_BOX(hbox1), scroll, TRUE, TRUE, 0);
191         gtk_box_pack_start(GTK_BOX(hbox1), vbox1, FALSE, FALSE, 0);
192         gtk_box_pack_start(GTK_BOX(vbox1), view_btn, FALSE, FALSE, 4);
193         gtk_box_pack_start(GTK_BOX(vbox1), delete_btn, FALSE, FALSE, 4);
194         gtk_box_pack_end(GTK_BOX(vbox1), close_btn, FALSE, FALSE, 4);
195
196         gtk_widget_show(certlist);
197         gtk_widget_show(scroll);
198         gtk_widget_show(hbox1);
199         gtk_widget_show(vbox1);
200         gtk_widget_show(close_btn);
201         gtk_widget_show(delete_btn);
202         gtk_widget_show(view_btn);
203         gtk_container_add(GTK_CONTAINER (window), hbox1);
204
205         manager.window = window;
206         manager.hbox1 = hbox1;
207         manager.vbox1 = vbox1;
208         manager.certlist = certlist;
209         manager.view_btn = view_btn;
210         manager.delete_btn = delete_btn;
211         manager.close_btn = close_btn;
212
213         gtk_widget_show(window);
214 }
215
216 static gboolean get_serverport(const gchar *str, gchar **server, gchar **port)
217 {
218         const gchar *pos, *prevpos;
219
220         g_return_val_if_fail(str != NULL, FALSE);
221
222         /* We expect 'host.name.port.cert' here, only set
223          * server and port if we find that.
224          * Validity of string in port should be checked by caller. */
225         for (prevpos = str, pos = strstr(str, ".");
226                         pos != NULL;
227                         prevpos = pos, pos = strstr(pos+1, ".")) {
228                 if (!strcmp(pos, ".cert")) {
229                         if (prevpos > str) {
230                                 *server = g_strndup(str, prevpos - str);
231                                 *port = g_strndup(prevpos+1, pos - prevpos - 1);
232                         } else {
233                                 *server = *port = NULL;
234                         }
235
236                         return TRUE;
237                 }
238         }
239
240         return FALSE;
241 }
242
243 static char *get_fingerprint(const char *str)
244 {
245         char *ret = NULL, *tmp = g_strdup(str);
246         char *previous_pos = NULL, *last_pos = NULL;
247
248         if (!strchr(tmp, ':')) {
249                 /* no fingerprint */
250                 if (strstr(tmp, ".cert"))
251                         *(strstr(tmp, ".cert")+1) = '.';
252         }
253
254         while (tmp && (tmp = strstr(tmp,".")) != NULL) {
255                 tmp++;
256                 previous_pos = last_pos;
257                 last_pos = tmp;
258         }
259         if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
260                 ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
261         else
262                 ret = NULL;
263         g_free(tmp);
264         return ret;
265         
266 }
267
268 static void ssl_manager_list_view_insert_cert(GtkWidget *list_view,
269                                                   GtkTreeIter *row_iter,
270                                                   gchar *host, 
271                                                   gchar *port,
272                                                   SSLCertificate *cert) 
273 {
274         GtkTreeIter iter;
275         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
276                                         (GTK_TREE_VIEW(list_view)));
277
278         if (row_iter == NULL) {
279                 /* append new */
280                 gtk_list_store_append(list_store, &iter);
281                 gtk_list_store_set(list_store, &iter,
282                                    SSL_MANAGER_HOST, host,
283                                    SSL_MANAGER_PORT, port,
284                                    SSL_MANAGER_CERT, cert,
285                                    -1);
286         } else {
287                 gtk_list_store_set(list_store, row_iter,
288                                    SSL_MANAGER_HOST, host,
289                                    SSL_MANAGER_PORT, port,
290                                    SSL_MANAGER_CERT, cert,
291                                    -1);
292         }
293 }
294
295 static void ssl_manager_load_certs (void) 
296 {
297         GDir *dir;
298         const gchar *d;
299         GError *error = NULL;
300         gchar *path;
301         int row = 0;
302         GtkListStore *store;
303
304         store = GTK_LIST_STORE(gtk_tree_view_get_model
305                                 (GTK_TREE_VIEW(manager.certlist)));
306
307         gtk_list_store_clear(store);
308
309         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, 
310                           "certs", G_DIR_SEPARATOR_S, NULL);
311
312         if((dir = g_dir_open(path, 0, &error)) == NULL) {
313                 debug_print("couldn't open dir '%s': %s (%d)\n", path,
314                                 error->message, error->code);
315                 g_error_free(error);
316                 return;
317         }
318         
319         while ((d = g_dir_read_name(dir)) != NULL) {
320                 gchar *server = NULL, *port = NULL, *fp = NULL;
321                 SSLCertificate *cert;
322
323                 if(strstr(d, ".cert") != d + (strlen(d) - strlen(".cert"))) 
324                         continue;
325
326                 get_serverport(d, &server, &port);
327                 fp = get_fingerprint(d);
328
329                 if (server != NULL && port != NULL) {
330                         gint portnum = atoi(port);
331                         if (portnum > 0 && portnum <= 65535) {
332                                 cert = ssl_certificate_find(server, portnum, fp);
333                                 ssl_manager_list_view_insert_cert(manager.certlist, NULL,
334                                                 server, port, cert);
335                         }
336                 }
337                 
338                 g_free(server);
339                 g_free(port);
340                 g_free(fp);
341                 row++;
342         }
343         g_dir_close(dir);
344         g_free(path);
345 }
346
347 static void ssl_manager_close(void) 
348 {
349         gtk_widget_hide(manager.window);
350 }
351
352 static void ssl_manager_close_cb(GtkWidget *widget,
353                                  gpointer data) 
354 {
355         ssl_manager_close();
356 }
357
358 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
359 {
360         if (event && event->keyval == GDK_KEY_Escape)
361                 ssl_manager_close();
362         return FALSE;
363 }
364
365 static void ssl_manager_double_clicked(GtkTreeView              *list_view,
366                                         GtkTreePath             *path,
367                                         GtkTreeViewColumn       *column,
368                                         gpointer                 data)
369 {
370         SSLCertificate *cert;
371         GtkTreeIter iter;
372         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
373
374         if (!gtk_tree_model_get_iter(model, &iter, path))
375                 return;
376
377         gtk_tree_model_get(model, &iter, 
378                            SSL_MANAGER_CERT, &cert,
379                            -1);
380
381         if (!cert)
382                 return;
383
384         sslcertwindow_show_cert(cert);
385
386         return;
387 }
388
389
390
391 static void ssl_manager_delete_cb(GtkWidget *widget, 
392                               gpointer data) 
393 {
394         SSLCertificate *cert;
395         int val;
396         GtkTreeIter sel;
397         GtkTreeModel *model;
398
399         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
400                                 (GTK_TREE_VIEW(manager.certlist)),
401                                 &model, &sel))
402                 return;
403         
404         gtk_tree_model_get(model, &sel,
405                            SSL_MANAGER_CERT, &cert,
406                            -1);
407         if (!cert)
408                 return;
409
410         val = alertpanel_full(_("Delete certificate"),
411                               _("Do you really want to delete this certificate?"),
412                               GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
413                               NULL, ALERT_WARNING, G_ALERTDEFAULT);
414
415                              
416         if (val != G_ALERTALTERNATE)
417                 return;
418         
419         ssl_certificate_delete_from_disk(cert);
420         ssl_certificate_destroy(cert);
421         gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
422 }
423
424 static void ssl_manager_view_cb(GtkWidget *widget, 
425                                 gpointer data) 
426 {
427         SSLCertificate *cert;
428         GtkTreeIter sel;
429         GtkTreeModel *model;
430
431         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
432                                 (GTK_TREE_VIEW(manager.certlist)),
433                                 &model, &sel))
434                 return;
435         
436         gtk_tree_model_get(model, &sel,
437                            SSL_MANAGER_CERT, &cert,
438                            -1);
439         if (!cert)
440                 return;
441
442         sslcertwindow_show_cert(cert);
443 }
444 #endif