summarylogtreecommitdiffstats
path: root/passwordmanager.c
blob: 9a8129b0a04dbba0b5e14ea7ac3046c2c563fd27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 Copyright (C) 2011 Giuseppe Borzi <gborzi@ieee.org>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 Derived from formhistory.c extension, copyrighted by
 Alexander Butenko <a.butenka@gmail.com>
 Christian Dywan <christian@twotoasts.de>
*/

#include <midori/midori.h>

#include "config.h"
#include "midori/sokoke.h"

#include <gnome-keyring.h>
#include <glib/gstdio.h>
#include <stdlib.h>

#ifdef G_OS_WIN32
    #define LIBPREFIX ""
#else
    #define LIBPREFIX "lib"
#endif

static GHashTable* log_pass;
static gchar* jspassm;

static gboolean
passwman_prepare_js ()
{
   gchar* passwordmanager;

   if (!g_file_get_contents ("/usr/share/midori/res/passwordmanager.js", \
            &passwordmanager, NULL, NULL))
   {
       return FALSE;
   }
   g_strchomp (passwordmanager);

   jspassm = g_strdup_printf ( "%s", passwordmanager );
   g_strstrip (jspassm);
   g_free (passwordmanager);

   return TRUE;
/*   gchar* data_name;
   gchar* data_path;
   gchar* passwordmanager;
   gchar* file;

   data_name = g_build_filename (PACKAGE_NAME, "res", NULL);
   data_path = sokoke_find_data_filename (data_name,TRUE);
   g_free (data_name);
   file = g_build_filename(data_path,G_DIR_SEPARATOR_S,\
          "passwordmanager.js",NULL);
   if (!g_file_get_contents (file, &passwordmanager, NULL, NULL))
       return FALSE;
   g_strchomp (passwordmanager);

   jspassm = g_strdup_printf ( "%s", passwordmanager );
   g_strstrip (jspassm);
   g_free (data_path);
   g_free (file);
   g_free (passwordmanager);
   return TRUE;*/
}

static gchar*
cleanurl( const gchar *url )
{
    gchar *cleaned, *id;
    gssize il = -1;
    gsize lprt;
    if ( ( id = g_strstr_len(url+8,il,"/") ) != NULL )
    {
        lprt = id - url;
        cleaned = g_strndup(url,lprt);
    }
    else
        cleaned = g_strdup(url);
    return cleaned;
}

static gchar*
passwman_build_js( const gchar *url )
{
    gchar* script, *array;

    if ( log_pass && ( array = g_hash_table_lookup(log_pass,url) ) )
        script = g_strdup_printf("function getValue() { "
                                 "var values = new Array(%s);"
                                 "return values;"
                                 "};"
                                 "%s"
                                 "window.addEventListener('load',"
                                 "function() {PassMan.fillogpass(document);} , true);",
                                 array,
                                 jspassm);
    else
        script = NULL;
    return script;
}

static gboolean
passwman_navigation_decision_cb (WebKitWebView*             web_view,
                                 WebKitWebFrame*            web_frame,
                                 WebKitNetworkRequest*      request,
                                 WebKitWebNavigationAction* action,
                                 WebKitWebPolicyDecision*   decision,
                                 MidoriExtension*           extension)
{
    gchar* url;
    url = cleanurl( webkit_web_view_get_uri(web_view) );
    if ( log_pass )
    {
        gchar *array, *script;
        array = g_hash_table_lookup(log_pass,url);
        if ( array == NULL )
            script = g_strdup_printf ("function getValue() { "
                                      "var values = new Array();"
                                      "return values;"
                                      "};"
                                      "%s"
                                      "PassMan.dologpass(document);"
                                      "Store.dump();",
                                      jspassm);
        else
            script = g_strdup_printf ("function getValue() { "
                                      "var values = new Array(%s);"
                                      "return values;"
                                      "};"
                                      "%s"
                                      "PassMan.dologpass(document);"
                                      "Store.dump();",
                                      array,
                                      jspassm);
        if (webkit_web_navigation_action_get_reason (action) == \
             WEBKIT_WEB_NAVIGATION_REASON_FORM_SUBMITTED)
        {
            JSContextRef js_context = webkit_web_frame_get_global_context (web_frame);
            gchar* value = sokoke_js_script_eval (js_context, script, NULL);
            if (value && *value)
            {
                guint32 item_id;
                g_hash_table_insert(log_pass,g_strdup(url),g_strdup(value));
                if ( g_strcmp0(value,"\"0\"") == 0 )
                    gnome_keyring_set_network_password_sync(NULL,
                                                            "__never_store_here__",
                                                            "midori",
                                                            url,
                                                            NULL,
                                                            NULL,
                                                            NULL,
                                                            0,
                                                            "whatever",
                                                            &item_id);
                else
                {
                    gchar *id;
                    gchar **up;
                    gsize lprt;
                    id = g_strrstr(value,"\",\""); lprt = id-value;
                    id = g_strrstr_len(value,lprt,"\",\""); id += 3;
                    up = g_strsplit(id,"\"",4);
                    gnome_keyring_set_network_password_sync(NULL,
                                                            up[0],
                                                            "midori",
                                                            url,
                                                            NULL,
                                                            NULL,
                                                            NULL,
                                                            0,
                                                            up[2],
                                                            &item_id);
                    g_strfreev(up);
                }

                
            }
        }
        g_free(script);
    }
    g_free(url);
    return FALSE;
}

static void
passwman_window_object_cleared_cb (WebKitWebView*  web_view,
                                   WebKitWebFrame* web_frame,
                                   JSContextRef    js_context,
                                   JSObjectRef     js_window)
{
    gchar* url; gchar* script;
    url = cleanurl( webkit_web_view_get_uri(web_view) );
    script = passwman_build_js (url);
    if ( script ) {
        sokoke_js_script_eval (js_context, script, NULL);
        g_free (script);
    }
    g_free(url);
}

static void
passwman_add_tab_cb (MidoriBrowser*   browser,
                     MidoriView*      view,
                     MidoriExtension* extension)
{
    GtkWidget* web_view = midori_view_get_web_view (view);
    g_signal_connect (web_view, "window-object-cleared",
            G_CALLBACK (passwman_window_object_cleared_cb), NULL);
    g_signal_connect (web_view, "navigation-policy-decision-requested",
        G_CALLBACK (passwman_navigation_decision_cb), extension);
}

static void
passwman_deactivate_cb (MidoriExtension* extension,
                        MidoriBrowser*   browser);

static void
passwman_app_add_browser_cb (MidoriApp*       app,
                             MidoriBrowser*   browser,
                             MidoriExtension* extension)
{
    GList* tabs = midori_browser_get_tabs (browser);
    for (; tabs; tabs = g_list_next (tabs))
            passwman_add_tab_cb (browser, tabs->data, extension);
    g_list_free (tabs);
    g_signal_connect (browser, "add-tab",
            G_CALLBACK (passwman_add_tab_cb), extension);
    g_signal_connect (extension, "deactivate",
            G_CALLBACK (passwman_deactivate_cb), browser);
}

static void
passwman_deactivate_tab (MidoriView*      view,
                         MidoriExtension* extension)
{
    GtkWidget* web_view = midori_view_get_web_view (view);
    g_signal_handlers_disconnect_by_func (
       web_view, passwman_window_object_cleared_cb, extension);
    g_signal_handlers_disconnect_by_func (
       web_view, passwman_navigation_decision_cb, extension);
}

static void
passwman_deactivate_cb(MidoriExtension* extension,
                       MidoriBrowser*   browser)
{
    MidoriApp* app = midori_extension_get_app (extension);

    g_signal_handlers_disconnect_by_func (
       browser, passwman_add_tab_cb, extension);
    g_signal_handlers_disconnect_by_func (
        extension, passwman_deactivate_cb, browser);
    g_signal_handlers_disconnect_by_func (
        app, passwman_app_add_browser_cb, extension);
    GList* tabs = midori_browser_get_tabs (browser);
    for (; tabs; tabs = g_list_next (tabs))
       passwman_deactivate_tab (tabs->data, extension);
    g_list_free (tabs);

    katze_assign (jspassm, NULL);
    if (log_pass)
        g_hash_table_destroy (log_pass);
}

static void
passwman_activate_cb (MidoriExtension* extension,
                      MidoriApp*       app)
{
    KatzeArray* browsers;
    MidoriBrowser* browser;

    log_pass = g_hash_table_new_full (g_str_hash, g_str_equal,
                                      (GDestroyNotify)g_free,
                                      (GDestroyNotify)g_free);
    if ( !jspassm )
        passwman_prepare_js();
    GnomeKeyringResult res; GList *results; results = NULL;
    res = gnome_keyring_find_network_password_sync(NULL,
                                                   "midori",
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   0,
                                                   &results);
    if ( res == GNOME_KEYRING_RESULT_OK ) {
        GList *r; gchar *nlp; gchar *server; gchar *tmp;
        for( r = results; r; r = r->next ) {
            GnomeKeyringNetworkPasswordData* data = \
                             (GnomeKeyringNetworkPasswordData*)r->data;
            server = data->server;
            if ( g_strcmp0(data->user,"__never_store_here__") == 0 )
                nlp = g_strdup("\"0\"");
            else if ( ( tmp = g_hash_table_lookup(log_pass,server) ) )
            {
                if ( g_strcmp0(tmp,"\"0\"") == 0 )
                    nlp = g_strdup("\"0\"");
                else
                {
                    gssize il = -1; gchar *id, *num; gsize lprt, n;
                    id = g_strstr_len(tmp,il,","); lprt = id - tmp -1;
                    num = g_strndup(tmp+1,lprt); n = atoi(num)+1;
                    nlp = g_strdup_printf("\"%i\"%s,\"%s\",\"%s\"",n,id,\
                                          data->user,data->password);
                }
            }
            else
                nlp = g_strdup_printf("\"1\",\"%s\",\"%s\"",data->user,data->password);
            g_hash_table_insert(log_pass,g_strdup(server),nlp);
        }
        gnome_keyring_network_password_list_free(results);
    }
    browsers = katze_object_get_object (app, "browsers");
    KATZE_ARRAY_FOREACH_ITEM (browser, browsers)
        passwman_app_add_browser_cb (app, browser, extension);
    g_signal_connect (app, "add-browser",
        G_CALLBACK (passwman_app_add_browser_cb), extension);

    g_object_unref (browsers);
}

MidoriExtension*
extension_init (void)
{
    gboolean should_init = TRUE;
    const gchar* ver;
    gchar* desc;
    MidoriExtension* extension;

    if ( passwman_prepare_js() && gnome_keyring_is_available() )
    {
        ver = "0.3" MIDORI_VERSION_SUFFIX;
        desc = g_strdup (_("Stores login/password data"));
    }
    else
    {
        desc = g_strdup_printf (_("Not available: %s"),
                                _("Resource files or gnome-keyring"));
        ver = NULL;
        should_init = FALSE;
    }

    extension = g_object_new (MIDORI_TYPE_EXTENSION,
        "name", _("Password Manager"),
        "description", desc,
        "version", ver,
        "authors", "Giuseppe Borzi <gborzi@ieee.org>",
        NULL);
    g_free (desc);

    if (should_init)
        g_signal_connect (extension, "activate",
            G_CALLBACK (passwman_activate_cb), NULL);

    return extension;
}