]> git.armaanb.net Git - chorizo.git/blobdiff - browser.c
Implement a simple certificate trust store
[chorizo.git] / browser.c
index 04798038e216fcb5487e5c218e4539037712a5e7..08c2b3e2d8fa569f0c2e7028bdbbd5699b2f2559 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -22,6 +22,8 @@ static void changed_download_progress(GObject *, GParamSpec *, gpointer);
 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
 static void changed_title(GObject *, GParamSpec *, gpointer);
 static void changed_uri(GObject *, GParamSpec *, gpointer);
+static gboolean crashed_web_view(WebKitWebView *, gpointer);
+static gboolean crashed_web_view_reload(gpointer);
 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
                               WebKitPolicyDecisionType, gpointer);
 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
@@ -39,6 +41,7 @@ static gboolean keywords_try_search(WebKitWebView *, const gchar *);
 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
 static void search(gpointer, gint);
 static Window tabbed_launch(void);
+static void trust_user_certs(WebKitWebContext *);
 static void usage(void);
 
 
@@ -67,6 +70,7 @@ static gint clients = 0;
 static gboolean cooperative_alone = TRUE;
 static gboolean cooperative_instances = TRUE;
 static int cooperative_pipe_fp = 0;
+static int crash_autoreload_delay = 2;
 static gchar *download_dir = "/tmp";
 static Window embed = 0;
 static gchar *fifo_suffix = "main";
@@ -173,6 +177,8 @@ client_new(const gchar *uri)
                         G_CALLBACK(key_web_view), c);
        g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
                         G_CALLBACK(hover_web_view), c);
+       g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
+                        G_CALLBACK(crashed_web_view), c);
 
        if (!initial_wc_setup_done)
        {
@@ -182,6 +188,8 @@ client_new(const gchar *uri)
                g_signal_connect(G_OBJECT(wc), "download-started",
                                 G_CALLBACK(download_handle_start), NULL);
 
+               trust_user_certs(wc);
+
                initial_wc_setup_done = TRUE;
        }
 
@@ -335,6 +343,29 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
        gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
 }
 
+gboolean
+crashed_web_view(WebKitWebView *web_view, gpointer data)
+{
+       fprintf(stderr, __NAME__": WebView crashed!\n");
+       if (crash_autoreload_delay >= 1)
+       {
+               fprintf(stderr, __NAME__": Reloading WebView in %d seconds.\n",
+                       crash_autoreload_delay);
+               g_timeout_add_seconds(crash_autoreload_delay, crashed_web_view_reload,
+                                     web_view);
+       }
+
+       return TRUE;
+}
+
+gboolean
+crashed_web_view_reload(gpointer data)
+{
+       webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(data));
+
+       return G_SOURCE_REMOVE;
+}
+
 gboolean
 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
               WebKitPolicyDecisionType type, gpointer data)
@@ -479,6 +510,10 @@ grab_environment_configuration(void)
        if (e != NULL)
                accepted_language[0] = g_strdup(e);
 
+       e = g_getenv(__NAME_UPPERCASE__"_CRASH_AUTORELOAD_DELAY");
+       if (e != NULL)
+               crash_autoreload_delay = atoi(e);
+
        e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
        if (e != NULL)
                download_dir = g_strdup(e);
@@ -561,6 +596,7 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
        struct Client *c = (struct Client *)data;
        const gchar *t;
        gchar *f;
+       WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
 
        if (event->type == GDK_KEY_PRESS)
        {
@@ -582,6 +618,9 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
                                        gtk_entry_set_text(GTK_ENTRY(c->location), "/");
                                        gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
                                        return TRUE;
+                               case GDK_KEY_c:  /* reload trusted certs (left hand) */
+                                       trust_user_certs(wc);
+                                       return TRUE;
                        }
                }
                else
@@ -624,6 +663,7 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
        gdouble dx, dy;
        gchar *f;
        gfloat z;
+       WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
 
        if (event->type == GDK_KEY_PRESS)
        {
@@ -666,6 +706,9 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
                                        gtk_entry_set_text(GTK_ENTRY(c->location), "/");
                                        gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
                                        return TRUE;
+                               case GDK_KEY_c:  /* reload trusted certs (left hand) */
+                                       trust_user_certs(wc);
+                                       return TRUE;
                        }
                }
                else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
@@ -850,6 +893,33 @@ tabbed_launch(void)
        return plug_into;
 }
 
+void
+trust_user_certs(WebKitWebContext *wc)
+{
+       GTlsCertificate *cert;
+       const gchar *basedir, *file, *absfile;
+       GDir *dir;
+
+       basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
+       dir = g_dir_open(basedir, 0, NULL);
+       if (dir != NULL)
+       {
+               file = g_dir_read_name(dir);
+               while (file != NULL)
+               {
+                       absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
+                                                  file, NULL);
+                       cert = g_tls_certificate_new_from_file(absfile, NULL);
+                       if (cert == NULL)
+                               fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
+                       else
+                               webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
+                       file = g_dir_read_name(dir);
+               }
+               g_dir_close(dir);
+       }
+}
+
 void
 usage(void)
 {