Implement saving of the certificate chain, making the offline status
[claws.git] / src / ssl_manager.c
index 3e694225d67e1a0c291723f5d8f7e287d07f28cc..6b2f6650078d9e92e3c0c64a727035a83e47092e 100644 (file)
@@ -1,10 +1,11 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
+ * and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
-#ifdef USE_OPENSSL
-#include <gtk/gtkwidget.h>
+#ifdef USE_GNUTLS
+#include <gtk/gtk.h>
 #include <glib.h>
 #include <glib/gi18n.h>
+#include <gdk/gdkkeysyms.h>
 #include <sys/types.h>
 #include <dirent.h>
 
@@ -32,7 +35,6 @@
 #include "manage_window.h"
 #include "utils.h"
 #include "mainwindow.h"
-#include "gtksctree.h"
 #include "alertpanel.h"
 #include "sslcertwindow.h"
 #include "prefs_common.h"
@@ -59,6 +61,8 @@ static struct SSLManager
 static void ssl_manager_view_cb                (GtkWidget *widget, gpointer data);
 static void ssl_manager_delete_cb      (GtkWidget *widget, gpointer data);
 static void ssl_manager_close_cb       (GtkWidget *widget, gpointer data);
+static gboolean key_pressed            (GtkWidget *widget, GdkEventKey *event,
+                                        gpointer data);
 static void ssl_manager_load_certs     (void);
 static void ssl_manager_double_clicked(GtkTreeView             *list_view,
                                        GtkTreePath             *path,
@@ -122,7 +126,7 @@ static GtkWidget *ssl_manager_list_view_create      (void)
        
        gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
                                              0, GTK_SORT_ASCENDING);
-       gtk_tree_view_set_rules_hint(list_view, prefs_common.enable_rules_hint);
+       gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
        
        selector = gtk_tree_view_get_selection(list_view);
        gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
@@ -148,15 +152,17 @@ void ssl_manager_create(void)
        GtkWidget *delete_btn;
        GtkWidget *close_btn;
 
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "ssl_manager");
        gtk_window_set_title (GTK_WINDOW(window),
-                             _("Saved SSL Certificates"));
+                             _("Saved SSL certificates"));
 
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
        gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(ssl_manager_close_cb), NULL);
+       g_signal_connect(G_OBJECT(window), "key_press_event",
+                        G_CALLBACK(key_pressed), NULL);
        MANAGE_WINDOW_SIGNALS_CONNECT (window);
 
        hbox1 = gtk_hbox_new(FALSE, 6);
@@ -205,16 +211,24 @@ void ssl_manager_create(void)
 static char *get_server(char *str)
 {
        char *ret = NULL, *tmp = g_strdup(str);
-       char *first_pos = NULL, *last_pos = NULL, *previous_pos = NULL;
+       char *first_pos = NULL, *last_pos = NULL;
+       char *previous_pos = NULL, *pre_previous_pos = NULL;
        int previous_dot_pos;
 
+       if (!strchr(tmp, ':')) {
+               /* no fingerprint */
+               if (strstr(tmp, ".cert"))
+                       *(strstr(tmp, ".cert")+1) = '.';
+       }
+
        first_pos = tmp;
-       while ((tmp = strstr(tmp,".")) != NULL) {
-               *tmp++;
+       while (tmp && (tmp = strstr(tmp,".")) != NULL) {
+               tmp++;
+               pre_previous_pos = previous_pos;
                previous_pos = last_pos;
                last_pos = tmp;
        }
-       previous_dot_pos = (previous_pos - first_pos);
+       previous_dot_pos = (pre_previous_pos - first_pos);
        if (previous_dot_pos - 1 > 0)
                ret = g_strndup(first_pos, previous_dot_pos - 1);
        else 
@@ -224,19 +238,52 @@ static char *get_server(char *str)
 }
 
 static char *get_port(char *str)
+{
+       char *ret = NULL, *tmp = g_strdup(str);
+       char *last_pos = NULL;
+       char *previous_pos = NULL, *pre_previous_pos = NULL;
+
+       if (!strchr(tmp, ':')) {
+               /* no fingerprint */
+               if (strstr(tmp, ".cert"))
+                       *(strstr(tmp, ".cert")+1) = '.';
+       }
+
+       while (tmp && (tmp = strstr(tmp,".")) != NULL) {
+               tmp++;
+               pre_previous_pos = previous_pos;
+               previous_pos = last_pos;
+               last_pos = tmp;
+       }
+       if (previous_pos && pre_previous_pos && (int)(previous_pos - pre_previous_pos - 1) > 0)
+               ret = g_strndup(pre_previous_pos, (int)(previous_pos - pre_previous_pos - 1));
+       else
+               ret = g_strdup("0");
+       g_free(tmp);
+       return ret;
+       
+}
+
+static char *get_fingerprint(char *str)
 {
        char *ret = NULL, *tmp = g_strdup(str);
        char *previous_pos = NULL, *last_pos = NULL;
 
-       while ((tmp = strstr(tmp,".")) != NULL) {
-               *tmp++;
+       if (!strchr(tmp, ':')) {
+               /* no fingerprint */
+               if (strstr(tmp, ".cert"))
+                       *(strstr(tmp, ".cert")+1) = '.';
+       }
+
+       while (tmp && (tmp = strstr(tmp,".")) != NULL) {
+               tmp++;
                previous_pos = last_pos;
                last_pos = tmp;
        }
        if (last_pos && previous_pos && (int)(last_pos - previous_pos - 1) > 0)
                ret = g_strndup(previous_pos, (int)(last_pos - previous_pos - 1));
        else
-               ret = g_strdup("0");
+               ret = NULL;
        g_free(tmp);
        return ret;
        
@@ -291,30 +338,31 @@ static void ssl_manager_load_certs (void)
        }
        
        while ((d = readdir(dir)) != NULL) {
-               gchar *server, *port;
+               gchar *server, *port, *fp;
                SSLCertificate *cert;
 
-               if(!strstr(d->d_name, ".cert")) 
+               if(strstr(d->d_name, ".cert") != d->d_name + (strlen(d->d_name) - strlen(".cert"))) 
                        continue;
 
                server = get_server(d->d_name);
                port = get_port(d->d_name);
+               fp = get_fingerprint(d->d_name);
                
-               
-               cert = ssl_certificate_find_lookup(server, atoi(port), FALSE);
-               
+               cert = ssl_certificate_find(server, atoi(port), fp);
+
                ssl_manager_list_view_insert_cert(manager.certlist, NULL, 
                                                  server, port, cert);
                
                g_free(server);
                g_free(port);
+               g_free(fp);
                row++;
        }
        closedir(dir);
        g_free(path);
 }
 
-void ssl_manager_close(void) 
+static void ssl_manager_close(void) 
 {
        gtk_widget_hide(manager.window);
 }
@@ -325,6 +373,13 @@ static void ssl_manager_close_cb(GtkWidget *widget,
        ssl_manager_close();
 }
 
+static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
+{
+       if (event && event->keyval == GDK_KEY_Escape)
+               ssl_manager_close();
+       return FALSE;
+}
+
 static void ssl_manager_double_clicked(GtkTreeView             *list_view,
                                        GtkTreePath             *path,
                                        GtkTreeViewColumn       *column,
@@ -372,11 +427,11 @@ static void ssl_manager_delete_cb(GtkWidget *widget,
 
        val = alertpanel_full(_("Delete certificate"),
                              _("Do you really want to delete this certificate?"),
-                             GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
-                             NULL, ALERT_WARNING, G_ALERTALTERNATE);
+                             GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
+                             NULL, ALERT_WARNING, G_ALERTDEFAULT);
 
                             
-       if (val != G_ALERTDEFAULT)
+       if (val != G_ALERTALTERNATE)
                return;
        
        ssl_certificate_delete_from_disk(cert);